📄 readenvtype.c
字号:
/* Copyright 2003-2006, Voltage Security, all rights reserved.
*/
#include "vibe.h"
#include "environment.h"
#include "base.h"
#include "libctx.h"
#include "p7obj.h"
#include "idobj.h"
#include "derhelp.h"
#include "oidlist.h"
#include "errorctx.h"
int VtPkcs7ImplReadEnvelopedData (
VtPkcs7Object *object,
Pointer info,
unsigned int flag
)
{
int status;
#if VOLT_ALIGNMENT != 1
unsigned int pad;
#endif
unsigned int index, bufferSize, offset;
unsigned int listToUseCount = 0, decodersToUseCount = 0;
VoltPkcs7Object *obj = (VoltPkcs7Object *)(*object);
VoltLibCtx *libCtx = (VoltLibCtx *)(obj->voltObject.libraryCtx);
VtReadPkcs7Info *readInfo;
VtDerCoder **ListToUse = (VtDerCoder **)0;
VtIdentitySchemaDecode **DecodersToUse = (VtIdentitySchemaDecode **)0;
VtDERCoderArray *derCoderArray;
VtSchemaDecodeArray *schemaDecodeArray;
VtMpIntCtx mpCtxToUse = (VtMpIntCtx)0;
unsigned char *buffer = (unsigned char *)0;
VoltPkcs7ReadEnvCtx *readCtx;
VOLT_DECLARE_ERROR_TYPE (errorType)
VOLT_DECLARE_FNCT_LINE (fnctLine)
do
{
VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_TYPE;
if (flag != VOLT_PKCS7_SET_TYPE_FLAG)
break;
/* Make sure the object is empty.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_P7_OBJ;
if ( (obj->contentType != 0) || (obj->localCtx != (Pointer)0) )
break;
/* Check the info. If there's no info, get it from the libCtx.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_ASSOCIATED_INFO;
if (info != (Pointer)0)
{
readInfo = (VtReadPkcs7Info *)info;
ListToUse = readInfo->derCoders;
listToUseCount = readInfo->derCoderCount;
DecodersToUse = readInfo->decoders;
decodersToUseCount = readInfo->decoderCount;
mpCtxToUse = readInfo->mpCtx;
}
/* If we don't have any DerCoder's get the ones in the libCtx. If
* there are none there, break, that's an error.
*/
if ( (ListToUse == (VtDerCoder **)0) || (listToUseCount == 0) )
{
derCoderArray = (VtDERCoderArray *)VoltGetLibCtxInfo (
obj->voltObject.libraryCtx, VOLT_LIB_CTX_INFO_TYPE_DER_CODERS);
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_NO_DER_CODERS;
if (derCoderArray == (VtDERCoderArray *)0)
break;
ListToUse = derCoderArray->derCoders;
listToUseCount = derCoderArray->derCoderCount;
}
/* If we don't have any SchemaDecode's get the ones in the libCtx.
* If there are none there, break, that's an error.
*/
if ( (DecodersToUse == (VtIdentitySchemaDecode **)0) ||
(decodersToUseCount == 0) )
{
schemaDecodeArray = (VtSchemaDecodeArray *)VoltGetLibCtxInfo (
obj->voltObject.libraryCtx, VOLT_LIB_CTX_INFO_TYPE_SCHEMA_DECODES);
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_NO_SCHEMA_DECODERS;
if (schemaDecodeArray == (VtSchemaDecodeArray *)0)
break;
DecodersToUse = schemaDecodeArray->decoders;
decodersToUseCount = schemaDecodeArray->decoderCount;
}
/* If we don't have an mpCtx, get one from the libCtx. If there is
* not one there, break, that's an error.
*/
if (mpCtxToUse == (VtMpIntCtx)0)
{
mpCtxToUse = (VtMpIntCtx)VoltGetLibCtxInfo (
obj->voltObject.libraryCtx, VOLT_LIB_CTX_INFO_TYPE_MP_CTX);
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_NO_MATH_LIBRARY;
if (mpCtxToUse == (VtMpIntCtx)0)
break;
}
/* Paranoid programming check.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_MP_INT_CTX;
if (VOLT_OBJECT_TYPE_NOT_EQUAL (mpCtxToUse, VOLT_OBJECT_TYPE_MP_INT_CTX))
break;
/* Build a buffer big enough to hold the readCtx, the coder array,
* and the schema decode's. Assume that the array of DerCoder
* pointers will not throw off the pad.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_MEMORY;
bufferSize =
sizeof (VoltPkcs7ReadEnvCtx) + (listToUseCount * sizeof (VtDerCoder *)) +
decodersToUseCount * sizeof (VtIdentitySchemaDecode *);
#if VOLT_ALIGNMENT != 1
/* If the alignment is 1, there's no need to pad. If not, compute
* the pad length.
*/
VOLT_COMPUTE_ALIGN_PAD (
VOLT_ALIGNMENT, sizeof (VoltPkcs7ReadEnvCtx), pad)
bufferSize += pad;
#endif
buffer = (unsigned char *)Z2Malloc (bufferSize, 0);
if (buffer == (unsigned char *)0)
break;
Z2Memset (buffer, 0, bufferSize);
/* Locate the pointers.
*/
readCtx = (VoltPkcs7ReadEnvCtx *)buffer;
offset = sizeof (VoltPkcs7ReadEnvCtx);
#if VOLT_ALIGNMENT != 1
offset += pad;
#endif
/* Copy the DerCoders array.
*/
readCtx->DerCoders = (VtDerCoder **)(buffer + offset);
for (index = 0; index < listToUseCount; ++index)
readCtx->DerCoders[index] = ListToUse[index];
readCtx->derCoderCount = listToUseCount;
/* Copy the schemaDecode's.
*/
offset += listToUseCount * sizeof (VtDerCoder *);
readCtx->Decoders = (VtIdentitySchemaDecode **)(buffer + offset);
for (index = 0; index < decodersToUseCount; ++index)
readCtx->Decoders[index] = DecodersToUse[index];
readCtx->decoderCount = decodersToUseCount;
/* Clone the mpCtx.
*/
VOLT_SET_ERROR_TYPE (errorType, 0)
VOLT_SET_FNCT_LINE (fnctLine)
status = VtCloneObject (
(Pointer)mpCtxToUse, (Pointer *)&(readCtx->mpCtx));
if (status != 0)
break;
/* This must be initialized to a negative number so we know it has
* not been determined yet.
*/
readCtx->chosenRecipient = -1;
obj->state = VOLT_P7_STATE_ENV_READ_SET;
obj->localCtx = (Pointer)readCtx;
obj->LocalCtxDestroy = VoltReadEnvCtxDestroy;
obj->contentType = VOLT_PKCS7_ENVELOPED_DATA_READ;
obj->ReadInit = VoltP7ReadEnvelopedInit;
obj->ReadUpdate = VoltP7ReadEnvelopedUpdate;
obj->ReadFinal = VoltP7ReadEnvelopedFinal;
status = 0;
} while (0);
/* If success, we're done.
*/
if (status == 0)
return (0);
/* If there was an error, destroy what we created.
*/
if (buffer != (unsigned char *)0)
Z2Free (buffer);
VOLT_LOG_ERROR_INFO (
0, *object, status, 0, errorType,
(char *)0, "VtPkcs7ImplReadEnvelopedData", fnctLine, (char *)0)
return (status);
}
void VoltReadEnvCtxDestroy (
Pointer obj,
Pointer ctx
)
{
unsigned int index;
VoltObject *voltObj = (VoltObject *)obj;
VoltLibCtx *libCtx;
VoltPkcs7ReadEnvCtx *readCtx = (VoltPkcs7ReadEnvCtx *)ctx;
/* Anything to destroy?
*/
if ( (obj == (Pointer)0) || (ctx == (Pointer)0) )
return;
libCtx = (VoltLibCtx *)(voltObj->libraryCtx);
if (readCtx->symKeyData.data != (unsigned char *)0)
Z2Free (readCtx->symKeyData.data);
if (readCtx->symAlgID != (unsigned char *)0)
Z2Free (readCtx->symAlgID);
if (readCtx->currentElement.element != (unsigned char *)0)
Z2Free (readCtx->currentElement.element);
for (index = 0; index < readCtx->recipInfoCount; ++index)
{
if (readCtx->recipInfoList[index] != (Asn1RecipientInfo *)0)
Asn1RecipientInfo_free (readCtx->recipInfoList[index]);
}
if (readCtx->recipInfoList != (Asn1RecipientInfo **)0)
Z2Free (readCtx->recipInfoList);
VtDestroyMpIntCtx (&(readCtx->mpCtx));
VtDestroyIdentityList (&(readCtx->recipList));
VtDestroyAlgorithmObject (&(readCtx->decryptor));
VtDestroyAlgorithmObject (&(readCtx->base64));
VtDestroyKeyObject (&(readCtx->priKey));
VtDestroyIdentityObject (&(readCtx->specifiedIdentity));
VtDestroyRecipientInfoList (&(readCtx->rInfoList));
Z2Free (ctx);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -