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

📄 readenv.c

📁 IBE是一种非对称密码技术
💻 C
📖 第 1 页 / 共 3 页
字号:

  VOLT_LOG_ERROR_INFO_COMPARE (
    status, 0, pkcs7Obj, status, 0, errorType,
    (char *)0, "VoltP7ReadEnvelopedFinal", fnctLine, (char *)0)

  return (status);
}

static int AddRecipientInfoToList (
   VoltLibCtx *libCtx,
   VoltPkcs7ReadEnvCtx *readCtx,
   unsigned char *recipInfoDer,
   unsigned int recipInfoDerLen
   )
{
  int status;
  unsigned int index, count;
  Asn1RecipientInfo *newRi = (Asn1RecipientInfo *)0;
  VtIdentityObject newId = (VtIdentityObject)0;
  Asn1RecipientInfo **newList = (Asn1RecipientInfo **)0;
  VOLT_DECLARE_ERROR_TYPE (errorType)
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    /* Build an identity object using the identity in the RecipientInfo.
     */
    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VtCreateIdentityObject (
      (VtLibCtx)libCtx, VtIdentityImplMpCtx, (Pointer)(readCtx->mpCtx),
      &newId);
    if (status != 0)
      break;

    VOLT_SET_FNCT_LINE (fnctLine)
    status = VoltDecodeRecipInfoCreate (
      newId, recipInfoDer, recipInfoDerLen, readCtx->base64,
      readCtx->Decoders, readCtx->decoderCount, &index, &newRi);
    if (status != 0)
      break;

    /* Add the Asn1RecipientInfo to the list.
     */
    VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_MEMORY;
    count = readCtx->recipInfoCount + 1;
    newList = (Asn1RecipientInfo **)Z2Malloc (
      count * sizeof (Asn1RecipientInfo *), 0);
    if (newList == (Asn1RecipientInfo **)0)
      break;
    Z2Memset (newList, 0, count * sizeof (Asn1RecipientInfo *));

    /* Copy the old into the new.
     */
    for (index = 0; index < readCtx->recipInfoCount; ++index)
      newList[index] = readCtx->recipInfoList[index];
    newList[index] = newRi;

    /* Add the identity to the identityList.
     */
    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VtAddIdObjectToIdentityList (readCtx->recipList, newId, &index);
    if (status != 0)
      break;

    /* If everything succeeded, get rid of the old recipInfoList and
     * replace it with the new.
     */
    if (readCtx->recipInfoList != (Asn1RecipientInfo **)0)
      Z2Free (readCtx->recipInfoList);
    readCtx->recipInfoList = newList;
    readCtx->recipInfoCount++;

  } while (0);

  /* We added the identity to the recipient list, so we don't need
   * this object any more.
   */
  VtDestroyIdentityObject (&newId);

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

  /* If there was an error, destroy what we created.
   */
  if (newRi != (Asn1RecipientInfo *)0)
    Asn1RecipientInfo_free (newRi);
  if (newList != (Asn1RecipientInfo **)0)
    Z2Free (newList);

  VOLT_LOG_ERROR_INFO (
    libCtx, 0, status, 0, errorType,
    (char *)0, "AddRecipientInfoToList", fnctLine, (char *)0)

  return (status);
}

int VoltDecodeRecipInfoCreate (
   VtIdentityObject idObj,
   unsigned char *recipInfoDer,
   unsigned int recipInfoDerLen,
   VtAlgorithmObject base64,
   VtIdentitySchemaDecode **decoders,
   unsigned int decoderCount,
   unsigned int *arrayIndex,
   Asn1RecipientInfo **asn1RecipInfo
   )
{
  int status;
  unsigned int theTag, lengthLen, valueLen, offset, len;
  UInt32 lenLo, lenHi;
  VoltLibCtx *libCtx =
    (VoltLibCtx *)(((VoltIdentityObject *)idObj)->voltObject.libraryCtx);
  Asn1RecipientInfo *newRi = (Asn1RecipientInfo *)0;
  unsigned char *temp;
  unsigned char *idString = (unsigned char *)0;
  VOLT_DECLARE_ERROR_TYPE (errorType)
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    /* Build a RecipientInfo object out of the encoding.
     */
    VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_MEMORY;
    newRi = Asn1RecipientInfo_new ();
    if (newRi == (Asn1RecipientInfo *)0)
      break;

    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_ENCODING;
    temp = recipInfoDer;
    d2i_Asn1RecipientInfo (&newRi, &temp, recipInfoDerLen);
    if (newRi == (Asn1RecipientInfo *)0)
      break;

    /* Isolate the Base64 encoded identity. It will be the element with
     * the VOLT_PRINT_STRING_TAG.
     */
    offset = 0;
    temp = newRi->issuerSerial->base.data;
    len = (unsigned int)(newRi->issuerSerial->base.length);
    VOLT_SET_ERROR_TYPE (errorType, 0)
    do
    {
      /* Keep decoding TL's until reaching 0C.
       */
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VoltDecodeTagAndLen (
        libCtx, temp + offset, len - offset, &theTag, &lengthLen,
        &lenLo, &lenHi, sizeof (unsigned int));
      if (status != 0)
        break;

      valueLen = (unsigned int)lenLo;

      offset += 1 + lengthLen;

      /* If we found the tag, quit looking.
       */
      if (theTag == VOLT_PRINT_STRING_TAG)
        break;

      /* If we found the OID tag, skip the data.
       */
      if (theTag == VOLT_OID_TAG)
        offset += valueLen;

    } while (1);
    if (status != 0)
      break;

    /* Decode the identity.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VtDecodeInit (base64);
    if (status != 0)
      break;

    /* Allocate a buffer to hold the result. There will be 3 characters
     * output for each 4 input. Maybe a little less, so just allocate a
     * buffer 3/4 the size of the input.
     */
    VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_MEMORY;
    len = (3 * valueLen) / 4;
    idString = (unsigned char *)Z2Malloc (len, 0);
    if (idString == (unsigned char *)0)
      break;

    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VtDecodeFinal (
      base64, (VtRandomObject)0, temp + offset, valueLen, idString, len, &len);
    if (status != 0)
      break;

    /* Now set the identity object using that identity.
     */

    /* If we can't decode this identity, just ignore it. So if the
     * error is INVALID_ENCODING or UNKNOWN_SCHEMA, skip everithing.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VtDecodeIdentity (
      idString, len, decoders, decoderCount, arrayIndex, idObj);
    if (status != 0)
      break;

    *asn1RecipInfo = newRi;

  } while (0);

  if (idString != (unsigned char *)0)
    Z2Free (idString);

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

  /* If there was an error, destroy what we created.
   */
  if (newRi != (Asn1RecipientInfo *)0)
    Asn1RecipientInfo_free (newRi);

  VOLT_LOG_ERROR_INFO (
    0, idObj, status, 0, errorType,
    (char *)0, "VoltDecodeRecipInfoCreate", fnctLine, (char *)0)

    return (status);
}

static int DecryptSessionKeyData (
   VoltLibCtx *libCtx,
   VoltPkcs7Object *obj,
   VoltPkcs7ReadEnvCtx *readCtx
   )
{
  int status;
  unsigned int bufferSize;
  Asn1RecipientInfo *recipInfo;
  VtAlgorithmObject decryptor = (VtAlgorithmObject)0;
  VoltSurrenderCtx *surrCtx = (VoltSurrenderCtx *)0;
  VtSurrenderCallback surrenderCtx;
  VtSetAlgIdInfo algIdInfo;
  VOLT_DECLARE_ERROR_TYPE (errorType)
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  recipInfo = readCtx->recipInfoList[readCtx->chosenRecipient];

  do
  {
    /* Build an algorithm object using the algId in the RecipientInfo.
     */
    algIdInfo.derCoders = readCtx->DerCoders;
    algIdInfo.derCoderCount = readCtx->derCoderCount;
    algIdInfo.berEncoding = recipInfo->keyEncAlg->base.data;
    algIdInfo.maxEncodingLen =
      (unsigned int)(recipInfo->keyEncAlg->base.length);
    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VtCreateAlgorithmObject (
      (VtLibCtx)libCtx, VtAlgorithmImplAlgId, (Pointer)&algIdInfo,
      &decryptor);
    if (status != 0)
      break;

    /* If there's a surrender ctx, pass it on to the decryptor.
     */
    if ( ((obj->voltObject.objectType & VOLT_OBJECT_TYPE_SURRENDER) != 0) &&
         (obj->voltObject.surrenderCtx != (Pointer)0) )
    {
      surrCtx = (VoltSurrenderCtx *)(obj->voltObject.surrenderCtx);
      /* Set the decryption object with the surrender ctx, but don't copy
       * the appData, just copy a reference, so we're still using the
       * P7 object's appData.
       */
      surrenderCtx.Surrender = surrCtx->Surrender;
      surrenderCtx.appData = surrCtx->appData;
      surrenderCtx.AppDataCopy = (VtSurrenderAppDataCopy)0;
      surrenderCtx.AppDataFree = (VtSurrenderAppDataFree)0;
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VtSetAlgorithmParam (
        decryptor, VtAlgorithmParamSurrenderCallback, (Pointer)&surrenderCtx);
      if (status != 0)
        break;
    }

    /* Init with the key we have.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VtDecryptInit (decryptor, readCtx->priKeyRef);
    if (status != 0)
      break;

    /* How big does the output buffer need to be?
     * Don't call this routine, older versions of the toolkit decrypted
     * to get the appropriate size. Compute "by hand". The length will
     * be encryptedKeyLen - primeLen - SHA-1 len. So the max len will
     * be encryptedKeyLen - minPrimeLen - SHA-1 len,
     */
/*    status = VtDecryptFinal (
      decryptor, (VtRandomObject)0, recipInfo->encryptedKey->data,
      (unsigned int)(recipInfo->encryptedKey->length),
      (unsigned char *)0, 0, &bufferSize);
    if (status == 0)
      status = VT_ERROR_GENERAL;
    if (status != VT_ERROR_BUFFER_TOO_SMALL)
      break;
*/
    bufferSize = (unsigned int)(recipInfo->encryptedKey->length) - 20;

    VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_MEMORY;
    readCtx->symKeyData.data = (unsigned char *)Z2Realloc (
      readCtx->symKeyData.data, bufferSize);
    if (readCtx->symKeyData.data == (unsigned char *)0)
      break;

    /* Now decrypt into the buffer.
     */
    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VtDecryptFinal (
      decryptor, (VtRandomObject)0, recipInfo->encryptedKey->data,
      (unsigned int)(recipInfo->encryptedKey->length),
      readCtx->symKeyData.data, bufferSize, &(readCtx->symKeyData.len));
    if (status != 0)
      break;

  } while (0);

  VtDestroyAlgorithmObject (&decryptor);

  VOLT_LOG_ERROR_INFO_COMPARE (
    status, libCtx, obj, status, 0, errorType,
    (char *)0, "DecryptSessionKeyData", fnctLine, (char *)0)

  return (status);
}

⌨️ 快捷键说明

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