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

📄 p7read.c

📁 IBE是一种非对称密码技术
💻 C
📖 第 1 页 / 共 2 页
字号:
/* Copyright 2003-2006, Voltage Security, all rights reserved.
 */
#include "vibe.h"
#include "environment.h"
#include "base.h"
#include "libctx.h"
#include "p7obj.h"
#include "derhelp.h"
#include "oidlist.h"
#include "reciplist.h"
#include "errorctx.h"

ASN1_SEQUENCE (Asn1Pkcs7Message) =
{
  ASN1_SIMPLE (Asn1Pkcs7Message, contentType, Asn1ObjectId),
  ASN1_EXP (Asn1Pkcs7Message, content, Asn1Encoded, 0)
} ASN1_SEQUENCE_END (Asn1Pkcs7Message);

IMPLEMENT_ASN1_FUNCTIONS (Asn1Pkcs7Message)

ASN1_SEQUENCE (Asn1RecipientInfo) =
{
  ASN1_SIMPLE (Asn1RecipientInfo, version, ASN1_INTEGER),
  ASN1_SIMPLE (Asn1RecipientInfo, issuerSerial, Asn1Encoded),
  ASN1_SIMPLE (Asn1RecipientInfo, keyEncAlg, Asn1Encoded),
  ASN1_SIMPLE (Asn1RecipientInfo, encryptedKey, ASN1_OCTET_STRING)
} ASN1_SEQUENCE_END (Asn1RecipientInfo);

IMPLEMENT_ASN1_FUNCTIONS (Asn1RecipientInfo)

ASN1_SEQUENCE (Asn1RecipInfoExtract) =
{
  ASN1_SIMPLE (Asn1RecipInfoExtract, version, ASN1_INTEGER),
  ASN1_SET_OF (Asn1RecipInfoExtract, recipInfo, Asn1RecipientInfo),
  ASN1_SIMPLE (Asn1RecipInfoExtract, ignore1, Asn1Encoded),
  ASN1_OPT (Asn1RecipInfoExtract, ignore2, Asn1Encoded),
  ASN1_OPT (Asn1RecipInfoExtract, ignore3, Asn1Encoded),
  ASN1_OPT (Asn1RecipInfoExtract, ignore4, Asn1Encoded),
  ASN1_OPT (Asn1RecipInfoExtract, ignore5, Asn1Encoded),
} ASN1_SEQUENCE_END (Asn1RecipInfoExtract);

IMPLEMENT_ASN1_FUNCTIONS (Asn1RecipInfoExtract)

/* Extract the issuerSerial from the recipInfo, it should contain the
 * encoded identity, Base64 encoded. Build an Identity object from the
 * encoded identity and add it to the identity list.
 * <p>If the recipient info does not contain an encoded identity, this
 * function does nothing and returns 0 (no error).
 *
 * @param p7Obj The P7 object involved in this transaction.
 * @param recipients The identityList to which the identity will be
 * added.
 * @param recipInfo The struct containing the issuerSerial.
 * @param decoders An array of VtIdentitySchemaDecode's used to decode
 * the encoded identity.
 * @param decoderCount The number of decoders in the array.
 * @return an int, 0 if the function completed successfully or a
 * non-zero error code.
 */
int VOLT_CALLING_CONV AddIdFromIssuerSerial VOLT_PROTO_LIST ((
   VoltLibCtx *libCtx,
   VtIdentityList recipients,
   Asn1RecipientInfo *recipInfo,
   VtIdentitySchemaDecode **decoders,
   unsigned int decoderCount
));

int VoltReadP7ContentType (
   VoltLibCtx *libCtx,
   unsigned char *message,
   unsigned int messageLen,
   unsigned int *contentType
   )
{
  int status;
  unsigned int bytesRead, offset, oidLastByte;
  unsigned char p7DataOid[VoltP7DataOidBytesLen] = { VoltP7DataOidBytes };
  VoltDerElement derElement;
  VOLT_DECLARE_ERROR_TYPE (errorType)
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  *contentType = 0;
  Z2Memset (&derElement, 0, sizeof (derElement));

  do
  {
    /* Collect the SEQUENCE and OID of a P7 message.
     * This call will collect an element. We'll "cheat" by setting the
     * explicitTag to the SEQUENCE tag. That way the function will
     * collect the SEQUENCE and the first thing after the SEQUENCE,
     * which is the OID.
     */
    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VoltGetNextDerElement (
      libCtx, message, messageLen, VOLT_SEQUENCE_TAG, VOLT_OID_TAG, 1,
      &derElement, &bytesRead);
    if (status != 0)
      break;

    VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_INPUT_LENGTH;
    if (derElement.complete == 0)
      break;

    /* Check the OID. All P7 contentType's are the same length.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_ENCODING;
    offset = (unsigned int)(derElement.elementLen - derElement.valueLen);
    if (derElement.valueLen != VoltP7DataOidBytesLen)
      break;

    /* Check all but the last byte of the OID, all P7 contentType's
     * have the same first bytes.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    if (Z2Memcmp (
      derElement.element + offset, p7DataOid, VoltP7DataOidBytesLen - 1) != 0)
      break;

    /* Look at the last byte of OID.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    offset = derElement.elementLen - 1;
    oidLastByte = (unsigned int)(derElement.element[offset]);
    if ( (oidLastByte < VOLT_P7_OID_BYTE_DATA) ||
         (oidLastByte > VOLT_P7_OID_BYTE_ENCRYPTED_DATA) )
      break;

    *contentType = oidLastByte;

    status = 0;

  } while (0);

  if (derElement.element != (unsigned char *)0)
    Z2Free (derElement.element);

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

  return (status);
}

int VoltReadP7Recipients (
   VoltLibCtx *libCtx,
   Asn1Pkcs7Message *p7Msg,
   unsigned int contentType,
   VtIdentitySchemaDecode **decoders,
   unsigned int decoderCount,
   VtIdentityList recipients
   )
{
  int status, count, index;
  unsigned char *temp;
  Asn1RecipInfoExtract *extractor;
  Asn1RecipientInfo *nextEntry;
  VOLT_DECLARE_ERROR_TYPE (errorType)
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    /* Use the Asn1RecipInfoExtract object to extract the recipients.
     */
    VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_MEMORY;
    extractor = Asn1RecipInfoExtract_new ();
    if (extractor == (Asn1RecipInfoExtract *)0)
      break;

    Asn1SetObjectCopyFlag (
      extractor->ignore1, VOLT_ASN1_COPY_REFERENCE);

    /* If this is SignedAndEnvelopedData, create the other ignore
     * Asn1Encoded's.
     */
    if (contentType != VT_PKCS7_ENVELOPED_DATA)
    {
      VOLT_SET_FNCT_LINE (fnctLine)
      extractor->ignore2 = Asn1Encoded_new ();
      if (extractor->ignore2 == (Asn1Encoded *)0)
        break;

      VOLT_SET_FNCT_LINE (fnctLine)
      extractor->ignore3 = Asn1Encoded_new ();
      if (extractor->ignore3 == (Asn1Encoded *)0)
        break;

      VOLT_SET_FNCT_LINE (fnctLine)
      extractor->ignore4 = Asn1Encoded_new ();
      if (extractor->ignore4 == (Asn1Encoded *)0)
        break;

      VOLT_SET_FNCT_LINE (fnctLine)
      extractor->ignore5 = Asn1Encoded_new ();
      if (extractor->ignore5 == (Asn1Encoded *)0)
        break;

      Asn1SetObjectCopyFlag (
        extractor->ignore2, VOLT_ASN1_COPY_REFERENCE);
      Asn1SetObjectCopyFlag (
        extractor->ignore3, VOLT_ASN1_COPY_REFERENCE);
      Asn1SetObjectCopyFlag (
        extractor->ignore4, VOLT_ASN1_COPY_REFERENCE);
      Asn1SetObjectCopyFlag (
        extractor->ignore5, VOLT_ASN1_COPY_REFERENCE);
    }

    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_UNKNOWN_BER;
    temp = p7Msg->content->base.data;
    d2i_Asn1RecipInfoExtract (&extractor, &temp, p7Msg->content->base.length);
    if (extractor == (Asn1RecipInfoExtract *)0)
      break;

    /* We now have a stack of RecipientInfo's. Each of the issuerSerial
     * fields contains the identity.
     */
    count = sk_num (extractor->recipInfo);

    status = 0;
    VOLT_SET_ERROR_TYPE (errorType, 0)
    for (index = 0; index < count; ++index)
    {
      nextEntry = (Asn1RecipientInfo *)sk_value (extractor->recipInfo, index);
      VOLT_SET_FNCT_LINE (fnctLine)
      status = AddIdFromIssuerSerial (
        libCtx, recipients, nextEntry, decoders, decoderCount);
      if (status != 0)
        break;
    }

  } while (0);

  if (extractor != (Asn1RecipInfoExtract *)0)
    Asn1RecipInfoExtract_free (extractor);

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

  return (status);
}

int AddIdFromIssuerSerial (
   VoltLibCtx *libCtx,
   VtIdentityList recipients,
   Asn1RecipientInfo *recipInfo,
   VtIdentitySchemaDecode **decoders,
   unsigned int decoderCount
   )
{
  int status;
  unsigned int encodingLen, schemaType, index;
  VtIdentityObject idObj = (VtIdentityObject)0;
  unsigned char *encoding = (unsigned char *)0;
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    /* Get the encoded ID out of the RecipientInfo.
     * If the RecipientInfo does not contain an encoded ID, just return
     * 0 (we didn't add an ID to the IdentityList, but that's not an
     * error we need to halt production over).
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VoltGetEncodedIdFromRecipInfoAlloc (
      libCtx, recipInfo, &encoding, &encodingLen);
    if (status != 0)
    {

⌨️ 快捷键说明

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