⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 zdmreadtype.c

📁 IBE是一种非对称密码技术
💻 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 "zdm.h"
#include "errorctx.h"

int VtZDMImplRead (
   VtZDMObject *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;
  VoltZDMObject *zObj = (VoltZDMObject *)(*object);
  VoltSecureMailObject *obj = (VoltSecureMailObject *)0;
  VoltLibCtx *libCtx = (VoltLibCtx *)(zObj->voltObject.libraryCtx);
  VtReadSecureMailInfo *readInfo;
  VtDerCoder **ListToUse = (VtDerCoder **)0;
  VtIdentitySchemaDecode **DecodersToUse = (VtIdentitySchemaDecode **)0;
  VtDERCoderArray *derCoderArray;
  VtSchemaDecodeArray *schemaDecodeArray;
  VtMpIntCtx mpCtxToUse = (VtMpIntCtx)0;
  unsigned char *buffer = (unsigned char *)0;
  VoltSecureMailReadCtx *readCtx = (VoltSecureMailReadCtx *)0;
  VtReadPkcs7Info p7Info;
  VtBase64Info b64Info;
  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_ZDM_SET_TYPE_FLAG)
      break;

    /* Make sure the object is empty.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_ZDM_OBJ;
    if (zObj->localCtx != (Pointer)0)
      break;

    /* Check the info. If there's no info, get it from the libCtx.
     */
    if (info != (Pointer)0)
    {
      readInfo = (VtReadSecureMailInfo *)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 (
        zObj->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 (
        zObj->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 (
        zObj->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)
    if (VOLT_OBJECT_TYPE_NOT_EQUAL (mpCtxToUse, VOLT_OBJECT_TYPE_MP_INT_CTX))
      break;

    /* A ZDM object used to be a SecureMail object. However, for ZDMv2,
     * we have a VoltZDMObject. This "old" ZDM Impl has to fit inside
     * the new ZDMObject framework. What we'll do is build the old
     * SecureMail object and place it into the new ZDMObject's localCtx.
     */
    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VoltCreateObject (
      (VtLibCtx)libCtx, (VoltObject **)&obj, sizeof (VoltSecureMailObject),
      VOLT_OBJECT_TYPE_ZDM);
    if (status != 0)
      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_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_MEMORY;
    bufferSize =
      sizeof (VoltSecureMailReadCtx) +
      (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 (VoltSecureMailReadCtx), pad)
      bufferSize += pad;
#endif
    buffer = (unsigned char *)Z2Malloc (bufferSize, 0);
    if (buffer == (unsigned char *)0)
      break;
    Z2Memset (buffer, 0, bufferSize);

    /* Locate the pointers.
     */
    readCtx = (VoltSecureMailReadCtx *)buffer;
    offset = sizeof (VoltSecureMailReadCtx);
#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;

    /* Initialize the contentMaterialState
     */
    readCtx->contentMaterialState = VOLT_CONTENT_MATERIAL_STATE_NONE;

    /* Build the P7 objects.
     */
    p7Info.derCoders = ListToUse;
    p7Info.derCoderCount = listToUseCount;
    p7Info.decoders = DecodersToUse;
    p7Info.decoderCount = decodersToUseCount;
    p7Info.mpCtx = mpCtxToUse;
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VtCreatePkcs7Object (
      (VtLibCtx)libCtx, VtPkcs7ImplReadSignedData, (Pointer)&p7Info,
      &(obj->p7SignedData));
    if (status != 0)
      break;

    VOLT_SET_FNCT_LINE (fnctLine)
    status = VtCreatePkcs7Object (
      (VtLibCtx)libCtx, VtPkcs7ImplReadEnvelopedData, (Pointer)&p7Info,
      &(obj->p7EnvelopedData));
    if (status != 0)
      break;

    /* Build the Base64 object.
     */
    b64Info.base64BlockSize = 64;
    b64Info.newLineCharacter = VT_BASE64_NEW_LINE_CR_LF;
    b64Info.errorCheck = VT_BASE64_NO_ERROR_CHECK;
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VtCreateAlgorithmObject (
      (VtLibCtx)libCtx, VtAlgorithmImplBase64, (Pointer)&b64Info,
      &(obj->base64));
    if (status != 0)
      break;

    obj->state = VOLT_ZDM_STATE_READ_SET;
    obj->localCtx = (Pointer)readCtx;
    obj->LocalCtxDestroy = VoltReadSecureMailCtxDestroy;
    obj->ReadInit = VoltZDMReadInit;
    obj->ReadUpdate = VoltSecureMailReadUpdate;
    obj->ReadFinal = VoltSecureMailReadFinal;
    obj->Verify = VoltSecureMailVerify;
    obj->GetEncodeDecodeSize = VoltBase64GetEncodeDecodeSize;

    /* Now store this SecureMail object as the localCtx of the
     * ZDMObject.
     */
    zObj->localCtx = (Pointer)obj;
    zObj->LocalCtxDestroy = VoltOldZDMCtxDestroy;
    zObj->SetParam = VoltOldZDMSetParamRead;
    zObj->GetParam = VoltOldZDMGetParamRead;
    zObj->ReadInit = VoltOldZDMReadInit;
    zObj->ReadUpdate = VoltOldZDMReadUpdate;
    zObj->ReadFinal = VoltOldZDMReadFinal;
    zObj->Verify = VoltOldZDMVerify;

    status = 0;

  } while (0);

  /* If success, we're done.
   */
  if (status == 0)
    return (0);

  /* If error, free what we allocated.
   */
  VoltDestroyOldZDMObject ((VoltSecureMailObject **)&obj);

  VOLT_LOG_ERROR_INFO (
    0, *object, status, 0, errorType,
    (char *)0, "VtZDMImplRead", fnctLine, (char *)0)

  return (status);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -