📄 sfwritetype.c
字号:
/* Copyright 2003-2006, Voltage Security, all rights reserved.
*/
#include "vibe.h"
#include "environment.h"
#include "base.h"
#include "libctx.h"
#include "securemail.h"
#include "p7obj.h"
#include "stringutil.h"
#include "errorctx.h"
int VtSecureFileImplWrite (
VtSecureFileObject *object,
Pointer info,
unsigned int flag
)
{
int status;
unsigned int bufferSize;
VoltSecureMailObject *obj = (VoltSecureMailObject *)(*object);
VoltLibCtx *libCtx = (VoltLibCtx *)(obj->voltObject.libraryCtx);
VoltSecureMailWriteCtx *writeCtx = (VoltSecureMailWriteCtx *)0;
unsigned char *buffer = (unsigned char *)0;
VtItem temp;
unsigned char newLineChars[VOLT_SECURE_MAIL_NEW_LINE_DEFAULT_LEN] =
VOLT_SECURE_MAIL_NEW_LINE_DEFAULT;
char *header = VOLT_SECURE_FILE_BEGIN_MSG;
VOLT_DECLARE_ERROR_TYPE (errorType)
VOLT_DECLARE_FNCT_LINE (fnctLine)
do
{
/* Make sure this is being called appropriately.
*/
VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_TYPE;
if (flag != VOLT_SECURE_FILE_SET_TYPE_FLAG)
break;
/* Make sure the object is empty.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_SECURE_FILE_OBJ;
if ( (obj->state != 0) || (obj->localCtx != (Pointer)0) )
break;
/* Check the info, we're expecting NULL.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_ASSOCIATED_INFO;
if (info != (Pointer)0)
break;
/* Build the local ctx.
*/
bufferSize = sizeof (VoltSecureMailWriteCtx);
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_MEMORY;
buffer = (unsigned char *)Z2Malloc (bufferSize, 0);
if (buffer == (unsigned char *)0)
break;
Z2Memset (buffer, 0, bufferSize);
writeCtx = (VoltSecureMailWriteCtx *)buffer;
/* Build the P7 objects.
*/
VOLT_SET_ERROR_TYPE (errorType, 0)
VOLT_SET_FNCT_LINE (fnctLine)
status = VtCreatePkcs7Object (
(VtLibCtx)libCtx, VtPkcs7ImplWriteSignedDSA, (Pointer)0,
&(obj->p7SignedData));
if (status != 0)
break;
VOLT_SET_FNCT_LINE (fnctLine)
status = VtCreatePkcs7Object (
(VtLibCtx)libCtx, VtPkcs7ImplWriteEnvelopeIBE, (Pointer)0,
&(obj->p7EnvelopedData));
if (status != 0)
break;
/* Copy the new line chars and the begin and end messages.
* We may want to change the new line char later, so set it to the
* default.
*/
temp.data = newLineChars;
temp.len = VOLT_SECURE_MAIL_NEW_LINE_DEFAULT_LEN;
VOLT_SET_FNCT_LINE (fnctLine)
status = VoltCopyItemDataAlloc (
obj->voltObject.libraryCtx, 0, 0, &temp,
&(writeCtx->itemArray[VOLT_WRITE_SM_ITEM_NEW_LINE]));
if (status != 0)
break;
/* There's only one header in a SecureFile. If it is a file (not an
* attachment), we'll need it. If it is an attachment, we won't.
* Put it in just in case.
*/
VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_MEMORY;
writeCtx->itemArray[VOLT_WRITE_SM_ITEM_BEGIN_MSG].data =
(unsigned char *)Z2Malloc (
VOLT_SECURE_FILE_BEGIN_MSG_LEN + VOLT_SECURE_MAIL_NEW_LINE_DEFAULT_LEN,
VOLT_MEMORY_SENSITIVE);
if (writeCtx->itemArray[VOLT_WRITE_SM_ITEM_BEGIN_MSG].data ==
(unsigned char *)0)
break;
Z2Memcpy (
writeCtx->itemArray[VOLT_WRITE_SM_ITEM_BEGIN_MSG].data, header,
VOLT_SECURE_FILE_BEGIN_MSG_LEN);
writeCtx->itemArray[VOLT_WRITE_SM_ITEM_BEGIN_MSG].len =
VOLT_SECURE_FILE_BEGIN_MSG_LEN;
obj->formatType = VOLT_MESSAGE_FORMAT_SECURE_FILE;
obj->state = VOLT_SECURE_MAIL_STATE_WRITE_SET;
obj->WriteInit = VoltSecureFileWriteInit;
obj->WriteUpdate = VoltSecureMailWriteUpdate;
obj->WriteFinal = VoltSecureMailWriteFinal;
obj->GetEncodeDecodeSize = VoltCopyGetEncodeDecodeSize;
obj->localCtx = (Pointer)buffer;
obj->LocalCtxDestroy = VoltWriteSecureMailCtxDestroy;
status = 0;
} while (0);
/* If success, we're done.
*/
if (status == 0)
return (0);
/* If error, free what we allocated.
*/
VoltWriteSecureMailCtxDestroy ((Pointer)obj, (Pointer)writeCtx);
VOLT_LOG_ERROR_INFO (
0, *object, status, 0, errorType,
(char *)0, "VtSecureFileImplWrite", fnctLine, (char *)0)
return (status);
}
int VtSecureFileParamDataLen (
VtSecureFileObject secureFileObj,
Pointer info,
unsigned int flag
)
{
int status;
unsigned int *dataLen;
VoltSecureMailObject *obj = (VoltSecureMailObject *)secureFileObj;
VOLT_DECLARE_FNCT_LINE (fnctLine)
do
{
/* This Param cannot get info.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_GET;
if (flag == VOLT_SECURE_FILE_GET_TYPE_FLAG)
break;
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_TYPE;
if (flag != VOLT_SECURE_FILE_SET_TYPE_FLAG)
break;
/* The SecureMail object must be built to write, an not yet be set
* with dataLen.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_SECURE_FILE_OBJ;
if ((obj->state & VOLT_SECURE_MAIL_STATE_WRITE) == 0)
break;
VOLT_SET_FNCT_LINE (fnctLine)
if (obj->dataLen != 0)
break;
#if VT_64_BIT_LENGTH == 64
if (obj->dataLen64 != (VtUInt64)0)
break;
#endif
/* The state must be VOLT_SECURE_MAIL_STATE_WRITE_SET.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_CALL_ORDER;
if (obj->state != VOLT_SECURE_MAIL_STATE_WRITE_SET)
break;
/* The info should be an unsigned int.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_ASSOCIATED_INFO;
if (info == (Pointer)0)
break;
dataLen = (unsigned int *)info;
obj->dataLen = *dataLen;
#if VT_64_BIT_LENGTH == 64
obj->dataLen64 = (VtUInt64)(obj->dataLen);
#endif
status = 0;
} while (0);
VOLT_LOG_ERROR_INFO_COMPARE (
status, 0, secureFileObj, status, 0, VT_ERROR_TYPE_PRIMARY,
(char *)0, "VtSecureFileParamDataLen", fnctLine, (char *)0)
return (status);
}
#if VT_64_BIT_LENGTH == 64
int VtSecureFileParamDataLen64 (
VtSecureFileObject secureFileObj,
Pointer info,
unsigned int flag
)
{
int status;
VtUInt64 *dataLen;
VoltSecureMailObject *obj = (VoltSecureMailObject *)secureFileObj;
VOLT_DECLARE_FNCT_LINE (fnctLine)
do
{
/* This Param cannot get info.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_GET;
if (flag == VOLT_SECURE_FILE_GET_TYPE_FLAG)
break;
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_TYPE;
if (flag != VOLT_SECURE_FILE_SET_TYPE_FLAG)
break;
/* The SecureMail object must be built to write, an not yet be set
* with dataLen.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_SECURE_FILE_OBJ;
if ((obj->state & VOLT_SECURE_MAIL_STATE_WRITE) == 0)
break;
VOLT_SET_FNCT_LINE (fnctLine)
if (obj->dataLen != 0)
break;
if (obj->dataLen64 != (VtUInt64)0)
break;
/* The state must be VOLT_SECURE_MAIL_STATE_WRITE_SET.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_CALL_ORDER;
if (obj->state != VOLT_SECURE_MAIL_STATE_WRITE_SET)
break;
/* The info should be a VtUInt64
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_ASSOCIATED_INFO;
if (info == (Pointer)0)
break;
dataLen = (VtUInt64 *)info;
obj->dataLen64 = *dataLen;
status = 0;
} while (0);
VOLT_LOG_ERROR_INFO_COMPARE (
status, 0, secureFileObj, status, 0, VT_ERROR_TYPE_PRIMARY,
(char *)0, "VtSecureFileParamDataLen64", fnctLine, (char *)0)
return (status);
}
#endif
int VtSecureFileParamFileName (
VtSecureFileObject secureFileObj,
Pointer info,
unsigned int flag
)
{
int status;
unsigned int fileNameLen, labelLen, index;
VoltSecureMailObject *obj = (VoltSecureMailObject *)secureFileObj;
VoltLibCtx *libCtx = (VoltLibCtx *)(obj->voltObject.libraryCtx);
VoltSecureMailWriteCtx *writeCtx = (VoltSecureMailWriteCtx *)0;
VoltSecureMailReadCtx *readCtx = (VoltSecureMailReadCtx *)0;
Pointer *getInfo;
VtUtf8StringList *descriptors;
char *found;
char *labelName = "file-name=";
VOLT_DECLARE_ERROR_TYPE (errorType)
VOLT_DECLARE_FNCT_LINE (fnctLine)
if ((obj->state & VOLT_SECURE_MAIL_STATE_WRITE) != 0)
writeCtx = (VoltSecureMailWriteCtx *)(obj->localCtx);
else
readCtx = (VoltSecureMailReadCtx *)(obj->localCtx);
do
{
if (flag == VOLT_SECURE_FILE_GET_TYPE_FLAG)
{
getInfo = (Pointer *)info;
/* If this is an object to write, the file name is in the
* writeCtx->fileName.
*/
VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_GET_INFO_UNAVAILABLE;
if (writeCtx != (VoltSecureMailWriteCtx *)0)
{
if (writeCtx->fileName.data == (unsigned char *)0)
break;
*getInfo = (Pointer)(writeCtx->fileName.data);
status = 0;
break;
}
/* This is a read object.
*/
VOLT_SET_ERROR_TYPE (errorType, 0)
VOLT_SET_FNCT_LINE (fnctLine)
status = VtGetSecureFileParam (
secureFileObj, VtSecureFileParamContentDescriptors,
(Pointer *)&descriptors);
if (status != 0)
break;
/* Search through the list, find the string that begins
* "file-name=".
*/
labelLen = 10;
found = (char *)0;
for (index = 0; index < descriptors->count; ++index)
{
found = Strstr (
descriptors->utf8Strings[index], labelName, (VtLibCtx)libCtx);
if (found != (char *)0)
break;
}
/* If we went through the list and did not find file-name, the
* info is unavailable.
*/
VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_GET_INFO_UNAVAILABLE;
if (found == (char *)0)
break;
/* Find the semicolon.
*/
labelName = found + labelLen;
labelLen = Z2Strlen (labelName);
found = Strchr (labelName, ';', (VtLibCtx)libCtx);
if (found != (char *)0)
labelLen = (unsigned int)found - (unsigned int)labelName;
/* Store the name in fileName.
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -