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

📄 readsign.c

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

    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_ENCODING;
    temp = encoding;
    d2i_Asn1SignerInfo (&newSi, &temp, encodingLen);
    if (newSi == (Asn1SignerInfo *)0)
      break;

    readCtx->signerInfos[index].signerInfo = newSi;

    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = DecodeAuthAttributesAlloc (
      libCtx, newSi, &(readCtx->signerInfos[index].authAttrs),
      &(readCtx->signerInfos[index].authAttrsCount));
    if (status != 0)
      break;

    /* Get the time attribute and convert it to VtTime.
     */
    authAttrs = readCtx->signerInfos[index].authAttrs;
    authAttrsCount = readCtx->signerInfos[index].authAttrsCount;

    for (indexA = 0; indexA < authAttrsCount; ++indexA)
    {
      if (authAttrs[indexA]->attrType->base.length !=
        VoltP9AtSignTimeOidBytesLen)
        continue;

      if (Z2Memcmp (
        authAttrs[indexA]->attrType->base.data, signTimeOid,
        VoltP9AtSignTimeOidBytesLen) != 0)
        continue;

      attrValue = (Asn1Encoded *)sk_value (authAttrs[indexA]->attrValues, 0);
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VoltDecodeTagAndLen (
        libCtx, attrValue->base.data, (unsigned int)(attrValue->base.length),
        &theTag, &lengthLen, &lenLo, &lenHi, sizeof (unsigned int));
      if (status != 0)
        break;

      valueLen = (unsigned int)lenLo;

      if ( (theTag == VOLT_UTC_TIME_TAG) && (valueLen == VOLT_UTC_LEN) )
      {
        VOLT_SET_FNCT_LINE (fnctLine)
        status = VoltConvertUTCToVtTime (
          libCtx, attrValue->base.data + 1 + lengthLen,
          &(readCtx->signerInfos[index].signingTime));
        break;
      }
      else if ( (theTag == VOLT_GEN_TIME_TAG) && (valueLen == VOLT_GEN_LEN) )
      {
        VOLT_SET_FNCT_LINE (fnctLine)
        status = VoltConvertGenTimeToVtTime (
          libCtx, attrValue->base.data + 1 + lengthLen, valueLen,
          &(readCtx->signerInfos[index].signingTime));
        break;
      }
      else
      {
        VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
        VOLT_SET_FNCT_LINE (fnctLine)
        status = VT_ERROR_INVALID_INPUT;
        break;
      }
    }

  } while (0);

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

  return (status);
}

static int DecodeAuthAttributesAlloc (
   VoltLibCtx *libCtx,
   Asn1SignerInfo *signerInfo,
   Asn1P9Attribute ***attributes,
   unsigned int *attributeCount
   )
{
  int status;
  unsigned int encodingLen, tempLen, theTag, lengthLen, valueLen, count, index;
  UInt32 lenLo, lenHi;
  Asn1P9Attribute *newAttr = (Asn1P9Attribute *)0;
  Asn1P9Attribute **authAttrs = (Asn1P9Attribute **)0;
  unsigned char *encoding = signerInfo->authAttributes->base.data;
  unsigned char *temp;
  VOLT_DECLARE_ERROR_TYPE (errorType)
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  *attributes = (Asn1P9Attribute **)0;
  *attributeCount = 0;
  encodingLen = (unsigned int)(signerInfo->authAttributes->base.length);

  do
  {
    /* Move the pointer to the first attribute.
     */
    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VoltDecodeTagAndLen (
      libCtx, encoding, encodingLen, &theTag, &lengthLen, &lenLo, &lenHi,
      sizeof (unsigned int));
    if (status != 0)
      break;

    valueLen = (unsigned int)lenLo;

    encoding += (1 + lengthLen);
    encodingLen -= (1 + lengthLen);
    temp = encoding;
    tempLen = encodingLen;

    /* Verify the lengths.
     */
    if (valueLen == 0)
      break;

    VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_ENCODING;
    if (tempLen < valueLen)
      break;

    /* Make sure we're only looking at the encoding.
     */
    tempLen = valueLen;

    /* Count the attributes.
     */
    count = 0;
    do
    {
      /* Get the stats on this attribute.
       */
      VOLT_SET_ERROR_TYPE (errorType, 0)
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VoltDecodeTagAndLen (
        libCtx, temp, tempLen, &theTag, &lengthLen, &lenLo, &lenHi,
        sizeof (unsigned int));
      if (status != 0)
        break;

      valueLen = (unsigned int)lenLo;

      /* Valid stats?
       */
      VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VT_ERROR_INVALID_ENCODING;
      if (tempLen < (valueLen + lengthLen + 1))
        break;

      /* Move to the next attribute.
       */
      temp += (valueLen + lengthLen + 1);
      tempLen -= (valueLen + lengthLen + 1);
      status = 0;
      count++;
    } while (tempLen > 0);
    if (status != 0)
      break;

    /* Allocate the array.
     */
    VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_MEMORY;
    authAttrs = (Asn1P9Attribute **)Z2Malloc (
      count * sizeof (Asn1P9Attribute *), 0);
    if (authAttrs == (Asn1P9Attribute **)0)
      break;
    Z2Memset (authAttrs, 0, count * sizeof (Asn1P9Attribute *));

    /* Run through the attributes again, this time creating an object
     * and decoding.
     */
    for (index = 0; index < count; ++index)
    {
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VT_ERROR_MEMORY;
      newAttr = Asn1P9Attribute_new ();
      if (newAttr == (Asn1P9Attribute *)0)
        break;

      /* We want just the attribute.
       */
      VOLT_SET_ERROR_TYPE (errorType, 0)
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VoltDecodeTagAndLen (
        libCtx, encoding, encodingLen, &theTag, &lengthLen, &lenLo, &lenHi,
        sizeof (unsigned int));
      if (status != 0)
        break;

      valueLen = (unsigned int)lenLo;

      /* Decode, the length check is a sanity check, if the loop that
       * counted the number of attributes was correct, this will never
       * fail.
       */
      VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VT_ERROR_INVALID_ENCODING;
      if (encodingLen < (valueLen + lengthLen + 1))
        break;

      VOLT_SET_FNCT_LINE (fnctLine)
      temp = encoding;
      d2i_Asn1P9Attribute (&newAttr, &temp, (valueLen + lengthLen + 1));
      if (newAttr == (Asn1P9Attribute *)0)
        break;

      encoding += (valueLen + lengthLen + 1);
      encodingLen -= (valueLen + lengthLen + 1);
      authAttrs[index] = newAttr;
      newAttr = (Asn1P9Attribute *)0;
      status = 0;
    }
    if (status != 0)
      break;

    *attributes = authAttrs;
    *attributeCount = count;

  } while (0);

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

  /* If there was an error, destroy what we created, it won't be
   * returned.
   */
  if (newAttr != (Asn1P9Attribute *)0)
    Asn1P9Attribute_free (newAttr);

  if (authAttrs != (Asn1P9Attribute **)0)
  {
    for (index = 0; index < count; ++index)
    {
      if (authAttrs[index] != (Asn1P9Attribute *)0)
        Asn1P9Attribute_free (authAttrs[index]);
    }
    Z2Free (authAttrs);
  }

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

  return (status);
}

static int VerifyAuthAttribute (
   VoltLibCtx *libCtx,
   unsigned int flag,
   Asn1P9Attribute **attributes,
   unsigned int attributeCount,
   unsigned char *baseValue,
   unsigned int baseValueLen,
   VtVerifyFailureList vfyFailList,
   unsigned int *verifyResult
   )
{
  int status;
  unsigned int theOidLen, theTag;
  unsigned int index, count, lengthLen, valueLen, reason;
  UInt32 lenLo, lenHi;
  Asn1Encoded *value;
  unsigned char *theOid;
  unsigned char contentTypeOid[VoltP9AtContentTypeOidBytesLen] =
    { VoltP9AtContentTypeOidBytes };
  unsigned char digestOid[VoltP9AtDigestOidBytesLen] =
    { VoltP9AtDigestOidBytes };
  VOLT_DECLARE_ERROR_TYPE (errorType)
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    /* Figure out what we're looking for.
     */
    if (flag == VOLT_P9_ATTRIBUTE_CONTENT_TYPE)
    {
      theOid = contentTypeOid;
      theOidLen = VoltP9AtContentTypeOidBytesLen;
      theTag = 6;
      reason = VT_VFY_FAIL_REASON_P7_ATTR_CONTENT_TYPE;
    }
    else if (flag == VOLT_P9_ATTRIBUTE_DIGEST)
    {
      theOid = digestOid;
      theOidLen = VoltP9AtDigestOidBytesLen;
      theTag = 4;
      reason = VT_VFY_FAIL_REASON_P7_ATTR_DIGEST;
    }
    else
    {
      VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VT_ERROR_INVALID_ENCODING;
      reason = VT_VFY_FAIL_REASON_UNKNOWN_VALUE;
      break;
    }

    /* First, find the appropriate attribute.
     */
    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = FindAuthAttribute (
      libCtx, theOid, theOidLen, attributes, attributeCount, &index);
    if (status != 0)
      break;

    /* There should be one attribute value.
     */
    VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_ENCODING;
    count = sk_num (attributes[index]->attrValues);
    if (count != 1)
      break;

    /* Get the Asn1Encoded.
     */
    value = (Asn1Encoded *)sk_value (attributes[index]->attrValues, 0);

    /* The value should be an atomic unit.
     */
    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VoltDecodeTagAndLen (
      libCtx, value->base.data, (unsigned int)(value->base.length),
      &theTag, &lengthLen, &lenLo, &lenHi, sizeof (unsigned int));
    if (status != 0)
      break;

    valueLen = (unsigned int)lenLo;

    VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_ENCODING;
    if (value->base.length < (int)(valueLen + lengthLen + 1))
      break;
    if (value->base.data[0] != theTag)
      break;

    /* Compare the data.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    if (valueLen != baseValueLen)
      break;

    if (Z2Memcmp (
      value->base.data + (lengthLen + 1), baseValue, valueLen) != 0)
      break;

    /* It matches, return success.
     */
    status = 0;

  } while (0);

  if (status == 0)
    return (0);

  /* There was some error, the verification fails. Ignore the status we
   * had, that just told us the verification failed.
   */
  *verifyResult = 0;

  VOLT_SET_ERROR_TYPE (errorType, 0)
  VOLT_SET_FNCT_LINE (fnctLine)
  status = VoltAddVerifyListEntry (
    (VoltVerifyFailureList *)vfyFailList, reason, (VtCertObject)0);

  return (status);
}

static int FindAuthAttribute (
   VoltLibCtx *libCtx,
   unsigned char *oid,
   unsigned int oidLen,
   Asn1P9Attribute **attributes,
   unsigned int attributeCount,
   unsigned int *index
   )
{
  unsigned int indexA;
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  *index = 0;

  for (indexA = 0; indexA < attributeCount; ++indexA)
  {
    if (attributes[indexA]->attrType->base.length != (int)oidLen)
      continue;
    if (Z2Memcmp (
      attributes[indexA]->attrType->base.data, oid, oidLen) != 0)
      continue;

    /* We found the match, return the index.
     */
    *index = indexA;
    return (0);
  }

  /* If the program reaches this point it found no match, return the
   * error.
   */
  VOLT_SET_FNCT_LINE (fnctLine)
  VOLT_LOG_ERROR_INFO (
    libCtx, 0, VT_ERROR_INVALID_ENCODING, 0, VT_ERROR_TYPE_PRIMARY,
    (char *)0, "FindAuthAttribute", fnctLine, (char *)0)

  return (VT_ERROR_INVALID_ENCODING);
}

⌨️ 快捷键说明

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