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

📄 dsakeyber.c

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

    case VOLT_DER_TYPE_ENCODE_FLAG:
      /* If the flag is ENCODE, return the DSA Public Key inside an
       * X.509 SubjectPublicKey.
       */

      /* Check the args.
       */
      VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VT_ERROR_NON_NULL_ARG;
      if (object != (Pointer)0)
        break;

      /* We need a place to drop the length.
       */
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VT_ERROR_NULL_ARG;
      if (encodeData->encodingLen == (unsigned int *)0)
        break;

      /* The info should be a key object.
       */
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VT_ERROR_NULL_ARG;
      if (encodeData->info == (Pointer)0)
        break;

      obj = (VoltKeyObject *)(encodeData->info);

      /* We need the key data.
       */
      VOLT_SET_ERROR_TYPE (errorType, 0)
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VtGetKeyParam (
        (VtKeyObject)obj, VtKeyParamDSAPublic, (Pointer *)&dsaKeyInfo);
      if (status != 0)
        break;

      /* If the previous call worked, we have a valid key object.
       */
      libCtx = (VoltLibCtx *)(obj->voltObject.libraryCtx);

      bufferSize = encodeData->bufferSize;
      if (encodeData->encoding == (unsigned char *)0)
        bufferSize = 0;

      /* Create the encoding of the params. The dsaKeyInfo will look
       * like paramInfo when cast.
       */
      VOLT_SET_FNCT_LINE (fnctLine)
      status = EncodeDsaParamsAlloc (
        obj, (VtDSAParamInfo *)dsaKeyInfo, &paramsDer, &paramsDerLen);
      if (status != 0)
        break;

      /* Build the encoding of the SubjectPublicKey. For DSA private it
       * is an INTEGER, the public value.
       */
      VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VT_ERROR_MEMORY;
      value = ASN1_INTEGER_new ();
      if (value == (ASN1_INTEGER *)0)
        break;

      VOLT_SET_FNCT_LINE (fnctLine)
      if (ASN1_STRING_set (
        value, dsaKeyInfo->pubValY.data, dsaKeyInfo->pubValY.len) != 1)
        break;

      /* The length of the encoding will likely be pubValY.len plus 4
       * bytes: 02 81 81 00 <pubValY>.
       * To be safe, allocate space for the pubValY and for 7 extra
       * bytes: 02 84 len len len len 00 <pubValY>. This will probably
       * never happen in the real world, but it saves coordinating code
       * to max lengths of prime.
       */
      VOLT_SET_FNCT_LINE (fnctLine)
      keyValue = (unsigned char *)Z2Malloc (dsaKeyInfo->pubValY.len + 7, 0);
      if (keyValue == (unsigned char *)0)
        break;

      /* Encode.
       */
      VOLT_SET_FNCT_LINE (fnctLine)
      temp = keyValue;
      keyValueLen = i2d_ASN1_INTEGER (value, &temp);
      if (keyValueLen == 0)
        break;

      /* Encode SubjectPublicKey
       */
      VOLT_SET_ERROR_TYPE (errorType, 0)
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VoltEncodeSubjectPublicKeyDer (
        libCtx, dsaKeyOid, VoltDsaKeyOidBytesLen, paramsDer, paramsDerLen,
        keyValue, keyValueLen, encodeData->encoding, bufferSize,
        encodeData->encodingLen);

      break;

    case VOLT_DER_TYPE_DECODE_FLAG:
      /* If the flag is decode, set the key object with the public key
       * info.
       */
      VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
      VOLT_SET_FNCT_LINE (fnctLine)
      if (object == (Pointer)0)
          return VT_ERROR_NULL_ARG;
      /* Check the args, the type should be VOLT_DER_TYPE_PUB_KEY_FLAG.
       */
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VT_ERROR_UNKNOWN_BER;
      if (decodeData->type != VOLT_DER_TYPE_PUB_KEY_FLAG)
        break;

      /* Make sure this is the algId for DSA key.
       */
      VOLT_SET_FNCT_LINE (fnctLine)
      subjPubKey = (Asn1SubjectPublicKey *)(decodeData->asn1Object);
      if (subjPubKey->algId->oid->base.length != VoltDsaKeyOidBytesLen)
        break;

      VOLT_SET_FNCT_LINE (fnctLine)
      libCtx = (VoltLibCtx *)(obj->voltObject.libraryCtx);
      if (Z2Memcmp (
        subjPubKey->algId->oid->base.data, dsaKeyOid,
        VoltDsaKeyOidBytesLen) != 0)
        break;

      /* Decode the params if they're in the algID.
       */
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VT_ERROR_MEMORY;
      dsaParams = Asn1DsaParams_new ();
      if (dsaParams == (Asn1DsaParams *)0)
        break;

      /* Decode.
       */
      temp = subjPubKey->algId->params->base.data;
      d2i_Asn1DsaParams (
        &dsaParams, &temp, subjPubKey->algId->params->base.length);

      /* Did it work? If not look for the pub val and params to be in
       * the key portion of the subjectPublicKey.
       */
      if (dsaParams == (Asn1DsaParams *)0)
      {
        VOLT_SET_FNCT_LINE (fnctLine)
        status = VT_ERROR_MEMORY;
        dsaPubAndParams = Asn1DsaPubAndParams_new ();
        if (dsaPubAndParams == (Asn1DsaPubAndParams *)0)
          break;

        /* Decode.
         */
        VOLT_SET_FNCT_LINE (fnctLine)
        status = VT_ERROR_UNKNOWN_BER;
        temp = subjPubKey->encodedKey->data;
        d2i_Asn1DsaPubAndParams (
          &dsaPubAndParams, &temp, subjPubKey->encodedKey->length);

        /* If this didn't work, we're out of options.
         */
        if (dsaPubAndParams == (Asn1DsaPubAndParams *)0)
          break;

        /* Build the struct needed to set a key object,
         */
        dsaPubKeyInfo.primeP.data = dsaPubAndParams->prime->data;
        dsaPubKeyInfo.primeP.len = dsaPubAndParams->prime->length;
        dsaPubKeyInfo.subprimeQ.data = dsaPubAndParams->subprime->data;
        dsaPubKeyInfo.subprimeQ.len = dsaPubAndParams->subprime->length;
        dsaPubKeyInfo.baseG.data = dsaPubAndParams->base->data;
        dsaPubKeyInfo.baseG.len = dsaPubAndParams->base->length;
        dsaPubKeyInfo.pubValY.data = dsaPubAndParams->pubVal->data;
        dsaPubKeyInfo.pubValY.len = dsaPubAndParams->pubVal->length;
      }
      else
      {
        /* The params were in the AlgID, the encodedKey in the subjPubKey
         * should be an INTEGER.
         */
        VOLT_SET_FNCT_LINE (fnctLine)
        status = VT_ERROR_MEMORY;
        value = ASN1_INTEGER_new ();
        if (value == (ASN1_INTEGER *)0)
          break;

        VOLT_SET_FNCT_LINE (fnctLine)
        status = VT_ERROR_UNKNOWN_BER;
        temp = subjPubKey->encodedKey->data;
        d2i_ASN1_INTEGER (&value, &temp, subjPubKey->encodedKey->length);

        /* Did it work?
         */
        VOLT_SET_FNCT_LINE (fnctLine)
        if (value == (ASN1_INTEGER *)0)
          break;

        /* Build the struct needed to set a key object,
         */
        dsaPubKeyInfo.primeP.data = dsaParams->prime->data;
        dsaPubKeyInfo.primeP.len = dsaParams->prime->length;
        dsaPubKeyInfo.subprimeQ.data = dsaParams->subprime->data;
        dsaPubKeyInfo.subprimeQ.len = dsaParams->subprime->length;
        dsaPubKeyInfo.baseG.data = dsaParams->base->data;
        dsaPubKeyInfo.baseG.len = dsaParams->base->length;
        dsaPubKeyInfo.pubValY.data = value->data;
        dsaPubKeyInfo.pubValY.len = value->length;
      }

      setBerInfo = (VtSetKeyBerInfo *)(decodeData->info);

      VOLT_SET_ERROR_TYPE (errorType, 0)
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VtSetKeyParam (
        (VtKeyObject)obj, VtKeyParamDSAPublic, (Pointer)&dsaPubKeyInfo);

      break;

    case VOLT_DER_TYPE_GET_ALG_FLAG:
      /* If the flag is get alg, check the input to see if it's the
       * DSA key OID.
       */

      /* Check the args.
       */
      VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VT_ERROR_NON_NULL_ARG;
      if (object != (Pointer)0)
        break;

      VOLT_SET_FNCT_LINE (fnctLine)
      status = VT_ERROR_NULL_ARG;
      if ( (getAlgData->algorithm == (unsigned int *)0) ||
           (getAlgData->oid == (unsigned char *)0) )
        break;

      VOLT_SET_FNCT_LINE (fnctLine)
      status = VT_ERROR_UNKNOWN_BER;
      if (getAlgData->encodingType != VOLT_ENCODING_TYPE_PUB_KEY)
        break;

      VOLT_SET_FNCT_LINE (fnctLine)
      if (getAlgData->oidLen != VoltDsaKeyOidBytesLen)
        break;

      VOLT_SET_FNCT_LINE (fnctLine)
      libCtx = getAlgData->libCtx;
      if (Z2Memcmp (
        getAlgData->oid, dsaKeyOid, VoltDsaKeyOidBytesLen) != 0)
        break;

      /* The OID matches, the algorithm is DSA public key.
       */
      *(getAlgData->algorithm) = VT_KEY_BER_DSA_PUBLIC;
      getAlgData->SymKeyParam = (VtKeyParam *)0;
      getAlgData->DigestImpl = (VtAlgorithmImpl *)0;

      status = 0;
  }

  if (keyValue != (unsigned char *)0)
    Z2Free (keyValue);
  if (paramsDer != (unsigned char *)0)
    Z2Free (paramsDer);
  if (value != (ASN1_INTEGER *)0)
    ASN1_INTEGER_free (value);
  if (dsaParams != (Asn1DsaParams *)0)
    Asn1DsaParams_free (dsaParams);
  if (dsaPubAndParams != (Asn1DsaPubAndParams *)0)
    Asn1DsaPubAndParams_free (dsaPubAndParams);

  VOLT_LOG_ERROR_INFO_COMPARE (
    status, 0, object, status, 0, errorType,
    (char *)0, "VtDerCoderDSAPublicKey", fnctLine, (char *)0)

  return (status);
}

static int EncodeDsaParamsAlloc (
   VoltKeyObject *obj,
   VtDSAParamInfo *paramInfo,
   unsigned char **encoding,
   unsigned int *encodingLen
   )
{
  int status;
  unsigned int bufferSize;
  VoltLibCtx *libCtx = (VoltLibCtx *)(obj->voltObject.libraryCtx);
  unsigned char *buffer = (unsigned char *)0;
  unsigned char *temp;
  Asn1DsaParams *params = (Asn1DsaParams *)0;
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    /* Create the template.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_MEMORY;
    params = Asn1DsaParams_new ();
    if (params == (Asn1DsaParams *)0)
      break;

    /* Set the fields.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    if (ASN1_STRING_set (
      params->prime, paramInfo->primeP.data, paramInfo->primeP.len) != 1)
      break;

    VOLT_SET_FNCT_LINE (fnctLine)
    if (ASN1_STRING_set (
      params->subprime, paramInfo->subprimeQ.data,
      paramInfo->subprimeQ.len) != 1)
      break;

    VOLT_SET_FNCT_LINE (fnctLine)
    if (ASN1_STRING_set (
      params->base, paramInfo->baseG.data, paramInfo->baseG.len) != 1)
      break;

    /* How big does the buffer need to be?
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_INPUT;
    bufferSize = i2d_Asn1DsaParams (params, (unsigned char **)0);
    if (bufferSize == 0)
      break;

    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_MEMORY;
    buffer = (unsigned char *)Z2Malloc (bufferSize, 0);
    if (buffer == (unsigned char *)0)
      break;

    /* This time file the buffer.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_INPUT;
    temp = buffer;
    bufferSize = i2d_Asn1DsaParams (params, &temp);
    if (bufferSize == 0)
      break;

    /* If this succeeded, return the buffer.
     */
    *encoding = buffer;
    *encodingLen = bufferSize;

    status = 0;

  } while (0);

  if (params != (Asn1DsaParams *)0)
    Asn1DsaParams_free (params);

  /* If successful, return 0.
   */
  if (status == 0)
    return (0);

  /* If something went wrong, free up the memory.
   */
  if (buffer != (unsigned char *)0)
    Z2Free (buffer);

  VOLT_LOG_ERROR_INFO (
    0, obj, status, 0, VT_ERROR_TYPE_PRIMARY,
    (char *)0, "EncodeDsaParamsAlloc", fnctLine, (char *)0)

  return (status);
}

⌨️ 快捷键说明

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