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

📄 keybertype.c

📁 IBE是一种非对称密码技术
💻 C
字号:
/* Copyright 2003-2006, Voltage Security, all rights reserved.
 */
#include "vibe.h"
#include "environment.h"
#include "base.h"
#include "libctx.h"
#include "keyobj.h"
#include "algid.h"
#include "prikeyder.h"
#include "pubkeyder.h"
#include "errorctx.h"

int VtKeyParamBer (
   VtKeyObject object,
   Pointer info,
   unsigned int flag
   )
{
  int status;
  unsigned int index, count;
  VoltKeyObject *obj = (VoltKeyObject *)object;
  VtSetKeyBerInfo *berInfo = (VtSetKeyBerInfo *)info;
  VoltLibCtx *libCtx = (VoltLibCtx *)(obj->voltObject.libraryCtx);
  Asn1PrivateKeyInfo *priKeyInfo = (Asn1PrivateKeyInfo *)0;
  Asn1SubjectPublicKey *subjPubKey = (Asn1SubjectPublicKey *)0;
  VtDerCoder **ListToUse;
  VtDERCoderArray *coderArray;
  VoltDerCoderInfo coderInfo;
  VOLT_DECLARE_FNCT_LINE (fnctLine) 
  VOLT_DECLARE_ERROR_TYPE (errorType)
  
  do
  {
    VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)

    /* This VtKeyParam can't Get.
     */
    status = VT_ERROR_INVALID_GET;
    VOLT_SET_FNCT_LINE (fnctLine)
    if (flag == VOLT_KEY_GET_TYPE_FLAG)
      break;

    /* Check the flag, it should be VOLT_KEY_SET_TYPE_FLAG.
     */
    status = VT_ERROR_INVALID_TYPE;
    VOLT_SET_FNCT_LINE (fnctLine)
    if (flag != VOLT_KEY_SET_TYPE_FLAG)
      break;

    /* The associated info should be a pointer to VtSetBerInfo.
     */
    status = VT_ERROR_INVALID_ASSOCIATED_INFO;
    VOLT_SET_FNCT_LINE (fnctLine)
    if (info == (Pointer)0)
      break;

    VOLT_SET_FNCT_LINE (fnctLine)
    if ( (berInfo->berEncoding == (unsigned char *)0) ||
         (berInfo->maxEncodingLen == 0) )
      break;

    /* Check the class of the object. It should be 0 (not yet set).
     * This SetType is not allowed when an object is already set.
     */
    status = VT_ERROR_INVALID_SET;
    VOLT_SET_FNCT_LINE (fnctLine)
    if (obj->keyType != 0)
      break;

    /* Use the coders passed in, unless none are given, then use the
     * coders in the libCtx, unless there are none there, in which
     * case, error.
     */
    ListToUse = berInfo->derCoders;
    count = berInfo->derCoderCount;
    if ( (berInfo->derCoders == (VtDerCoder **)0) ||
         (berInfo->derCoderCount == 0) )
    {
      coderArray = (VtDERCoderArray *)VoltGetLibCtxInfo (
        obj->voltObject.libraryCtx, VOLT_LIB_CTX_INFO_TYPE_DER_CODERS);

      status = VT_ERROR_NO_DER_CODERS;
      VOLT_SET_FNCT_LINE (fnctLine)
      if (coderArray == (VtDERCoderArray *)0)
        break;

      ListToUse = coderArray->derCoders;
      count = coderArray->derCoderCount;
    }

    VOLT_SET_ERROR_TYPE (errorType, 0)

    /* Try to decode as a private key.
     */
    status = VoltDecodePriKeyInfoCreate (
      libCtx, berInfo->berEncoding, berInfo->maxEncodingLen, &priKeyInfo);

    /* Set the fields, if they're wrong, we'll change them.
     */
    coderInfo.info.decodeData.type = VOLT_DER_TYPE_PRI_KEY_FLAG;
    coderInfo.info.decodeData.asn1Object = (Pointer)priKeyInfo;
    coderInfo.info.decodeData.info = info;

    /* If the previous call did not work, try it as a public key.
     */
    if (status != 0)
    {
      /* If this doesn't work, we have nothing else to try, give up.
       */      
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VoltDecodeSubjPubKeyCreate (
        libCtx, berInfo->berEncoding, berInfo->maxEncodingLen, &subjPubKey);
      if (status != 0)
        break;

      coderInfo.info.decodeData.type = VOLT_DER_TYPE_PUB_KEY_FLAG;
      coderInfo.info.decodeData.asn1Object = (Pointer)subjPubKey;
      coderInfo.info.decodeData.info = info;
    }

    /* Try all the DerCoder's given, see if one of them knows about
     * this key.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    for (index = 0; index < count; ++index)
    {      
      status = ListToUse[index] (
        &coderInfo, (Pointer)obj, VOLT_DER_TYPE_DECODE_FLAG);

      /* If status is 0, we found the alg, load the DerCoder into the
       * key object. Any error other than VT_ERROR_UNKNOWN_BER pass on.
       */
      if (status == VT_ERROR_UNKNOWN_BER)
        continue;

      if (status == 0)
        obj->DerCoder = (VFnctPointer)(ListToUse[index]);

      break;
    }

  } while (0);

  if (priKeyInfo != (Asn1PrivateKeyInfo *)0)
    Asn1PrivateKeyInfo_free (priKeyInfo);
  if (subjPubKey != (Asn1SubjectPublicKey *)0)
    Asn1SubjectPublicKey_free (subjPubKey);

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

  /* If something went wrong, indicate that this object is not usable.
   */
  obj->keyType = 0;

  VOLT_LOG_ERROR (
    (VtLibCtx)libCtx, status, errorType, 
    fnctLine, "VtKeyParamBer", (char *)0)

  return (status);
}

⌨️ 快捷键说明

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