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

📄 algorithmid.c

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

int VtDerEncodeAlgId (
   VtLibCtx libraryCtx,
   VtDerCoder DerCoder,
   Pointer associatedInfo,
   unsigned char *algId,
   unsigned int bufferSize,
   unsigned int *algIdLen
   )
{
  int status;
  unsigned int bufSize;
  VoltLibCtx *libCtx;
  VoltDerCoderInfo coderInfo;
  VOLT_DECLARE_ERROR_TYPE (errorType)
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  status = 0;
  do
  {
    /* Check the args.
     */
    VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
    VOLT_SET_FNCT_LINE (fnctLine)
    if ( (libraryCtx == (VtLibCtx)0) || (DerCoder == (VtDerCoder *)0) ||
      (algIdLen == (unsigned int *)0) )
    {
      status = VT_ERROR_NULL_ARG;
      break;
    }
    libCtx = (VoltLibCtx *)libraryCtx;
    
    /* Paranoid programming check.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    if (VOLT_LIB_CTX_TYPE_NOT_EQUAL (libCtx) )
    {
      status = VT_ERROR_INVALID_LIB_CTX;
      break;
    }
    bufSize = bufferSize;
    if (algId == (unsigned char *)0)
      bufSize = 0;
    
    /* The DerCoder will actually do the work.
     */
    coderInfo.info.encodeData.libCtx = libCtx;
    coderInfo.info.encodeData.info = associatedInfo;
    coderInfo.info.encodeData.encoding = algId;
    coderInfo.info.encodeData.bufferSize = bufSize;
    coderInfo.info.encodeData.encodingLen = algIdLen;
    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = DerCoder (&coderInfo, (Pointer)0, VOLT_DER_TYPE_ENCODE_FLAG);

  }while (0);

  VOLT_LOG_ERROR_COMPARE (
    status, libraryCtx, status, errorType,
    fnctLine, "VtDerEncodeAlgId", (char *)0)

  return status;
}

int VtDerEncodeKey (
   VtLibCtx libCtx,
   VtDerCoder DerCoder,
   Pointer associatedInfo,
   unsigned char *keyDer,
   unsigned int bufferSize,
   unsigned int *keyDerLen
   )
{
  return (VtDerEncodeAlgId (
    libCtx, DerCoder, associatedInfo, keyDer, bufferSize, keyDerLen));
}

int VtDerEncodeParams (
   VtLibCtx libCtx,
   VtDerCoder DerCoder,
   Pointer associatedInfo,
   unsigned char *paramDer,
   unsigned int bufferSize,
   unsigned int *paramDerLen
   )
{
  return (VtDerEncodeAlgId (
    libCtx, DerCoder, associatedInfo, paramDer, bufferSize, paramDerLen));
}

int VtGetAlgorithmFromBer (
   VtLibCtx libraryCtx,
   unsigned char *encoding,
   unsigned int maxEncodingLen,
   VtDerCoder **DerCoders,
   unsigned int derCoderCount,
   unsigned int *algorithm
   )
{
  unsigned int index;

  return (VoltGetAlgorithmFromBer (
    libraryCtx, encoding, maxEncodingLen, DerCoders, derCoderCount,
    algorithm, &index));
}

int VoltGetAlgorithmFromBer (
   VtLibCtx libraryCtx,
   unsigned char *encoding,
   unsigned int maxEncodingLen,
   VtDerCoder **DerCoders,
   unsigned int derCoderCount,
   unsigned int *algorithm,
   unsigned int *arrayIndex
   )
{
  int status;
  unsigned int index, count, encodingType, theTag, lengthLen, valueLen;
  UInt32 lenLo, lenHi;
  VoltLibCtx *libCtx;
  VtDerCoder **ListToUse;
  VtDERCoderArray *coderArray;
  VoltDerCoderInfo coderInfo;
  VOLT_DECLARE_ERROR_TYPE (errorType)
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    /* Check the args.
     */
    VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_NULL_ARG;
    if ( (libraryCtx == (VtLibCtx)0) ||
         (encoding == (unsigned char *)0) ||
         (maxEncodingLen == 0) ||
         (algorithm == (unsigned int *)0) )
      break;

    /* Paranoid programming check.
     */
    status = VT_ERROR_INVALID_LIB_CTX;
    VOLT_SET_FNCT_LINE (fnctLine)
    if (VOLT_LIB_CTX_TYPE_NOT_EQUAL (libraryCtx))
      break;

    libCtx = (VoltLibCtx *)libraryCtx;
    *algorithm = VT_UNKNOWN_BER;

    /* 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 = DerCoders;
    count = derCoderCount;
    if ( (DerCoders == (VtDerCoder **)0) ||
         (derCoderCount == 0) )
    {
      coderArray = (VtDERCoderArray *)VoltGetLibCtxInfo (
        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;
    }

    /* Search through the given encoding until finding an OID.
     * Everything we search will be one of the following.
     *   1 - SEQUENCE { OID, ... }
     *   2 - SEQUENCE { INT, OID, ... }
     *   3 - SEQUENCE { INT, SEQ { OID, ... }, ... }
     *   4 - SEQUENCE { SEQ { OID, ... }, ... }
     * What we're decoding . . .
     *   1 - algorithm identifier
     *   2 - params
     *   3 - P8 private key
     *   4 - X.509 public key
     */
    encodingType = 0;
    do
    {
      encodingType++;

      VOLT_SET_ERROR_TYPE (errorType, 0)
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VoltDecodeTagAndLen (
        (VoltLibCtx *)libraryCtx, encoding, maxEncodingLen, &theTag,
        &lengthLen, &lenLo, &lenHi, sizeof (unsigned int));
      if (status != 0)
        break;

      valueLen = (unsigned int)lenLo;

      if (theTag == VOLT_OID_TAG)
        break;
      if (theTag == VOLT_INTEGER_TAG)
        encodingType += 1;

      /* The tag is not OID, if it's SEQUENCE, skip the tag and len and
       * move on to the next tag.
       * It the current tag is not SEQUENCE, skip the tag, len, and
       * value to get to the next tag.
       */
      status = VT_ERROR_UNKNOWN_BER;
      VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)      
      if (theTag == VOLT_SEQUENCE_TAG)
      {
        VOLT_SET_FNCT_LINE (fnctLine)
        if (maxEncodingLen < (1 + lengthLen))
          break;

        encoding += 1 + lengthLen;
        maxEncodingLen -= (1 + lengthLen);
      }
      else
      {
        VOLT_SET_FNCT_LINE (fnctLine)
        if (maxEncodingLen < (1 + lengthLen + valueLen))
          break;

        encoding += 1 + lengthLen + valueLen;
        maxEncodingLen -= (1 + lengthLen + valueLen);
      }

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

    /* Check the OID with each of the DerCoder's until finding a match.
     */
    switch (encodingType)
    {
      case 3:
        coderInfo.info.getAlgData.encodingType = VOLT_ENCODING_TYPE_PUB_KEY;
        break;

      case 4:
        coderInfo.info.getAlgData.encodingType = VOLT_ENCODING_TYPE_PARAMS;
        break;

      case 5:
        coderInfo.info.getAlgData.encodingType = VOLT_ENCODING_TYPE_PRI_KEY;
        break;

      default:
        coderInfo.info.getAlgData.encodingType = VOLT_ENCODING_TYPE_ALG_ID;
    }
    coderInfo.info.getAlgData.libCtx = libCtx;
    coderInfo.info.getAlgData.oid = encoding + 1 + lengthLen;
    coderInfo.info.getAlgData.oidLen = valueLen;
    coderInfo.info.getAlgData.algorithm = algorithm;
    VOLT_SET_ERROR_TYPE (errorType, 0)
    for (index = 0; index < count; ++index)
    {
      VOLT_SET_FNCT_LINE (fnctLine)
      status = ListToUse[index] (
        &coderInfo, (Pointer)0, VOLT_DER_TYPE_GET_ALG_FLAG);

        /* If status is 0, we found the alg, we're done. Any error other
         * than VT_ERROR_UNKNOWN_BER pass on.
         */
      if (status != VT_ERROR_UNKNOWN_BER)
        break;
    }

    *arrayIndex = index;

  } while (0);

  VOLT_LOG_ERROR_COMPARE (
    status, libraryCtx, status, errorType,
    fnctLine, "VoltGetAlgorithmFromBer", (char *)0)

  return (status);
}

⌨️ 快捷键说明

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