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

📄 algid.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 "derhelp.h"
#include "errorctx.h"

/* Set up the OpenSSL ASN.1 template.
 */
ASN1_SEQUENCE (Asn1AlgorithmId) =
{
  ASN1_SIMPLE (Asn1AlgorithmId, oid, Asn1ObjectId),
  ASN1_OPT (Asn1AlgorithmId, params, Asn1Encoded)
} ASN1_SEQUENCE_END (Asn1AlgorithmId);

IMPLEMENT_ASN1_FUNCTIONS (Asn1AlgorithmId)

int VoltEncodeAlgId (
   VoltLibCtx *libCtx,
   unsigned char *oid,
   unsigned int oidLen,
   unsigned char *params,
   unsigned int paramsLen,
   unsigned int nullParamsFlag,
   unsigned char *algId,
   unsigned int bufferSize,
   unsigned int *algIdLen
   )
{
  int status, encodingLen;
  Asn1AlgorithmId *algIdObj = (Asn1AlgorithmId *)0;
  unsigned char *temp;
  unsigned char nullParams[2] = { 5, 0 };
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  if (algId == (unsigned char *)0)
    bufferSize = 0;

  /* If there are no imput params, do nothing or use NULL (05 00)
   * depending on the nullParams arg.
   */
  if (params == (unsigned char *)0)
    paramsLen = 0;
  if (paramsLen == 0)
  {
    params = (unsigned char *)0;
    if (nullParamsFlag != 0)
    {
      params = nullParams;
      paramsLen = 2;
    }
  }
  
  do
  {
    /* Create the object.
     */
    status = VT_ERROR_MEMORY;
    VOLT_SET_FNCT_LINE (fnctLine)   
    algIdObj = Asn1AlgorithmId_new ();
    if (algIdObj == (Asn1AlgorithmId *)0)      
      break;    

    /* Set the fields.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    if (Asn1ObjectId_set (algIdObj->oid, oid, oidLen) != 1)
      break;
    VOLT_SET_FNCT_LINE (fnctLine)
    if (Asn1Encoded_setCreate (&(algIdObj->params), params, paramsLen) != 1)
      break;

    /* Encode with no buffer to get length.
     */
    status = VT_ERROR_INVALID_INPUT;
    VOLT_SET_FNCT_LINE (fnctLine)
    encodingLen = i2d_Asn1AlgorithmId (algIdObj, (unsigned char **)0);
    if (encodingLen == 0)
      break;

    status = VT_ERROR_BUFFER_TOO_SMALL;
    VOLT_SET_FNCT_LINE (fnctLine)
    *algIdLen = (unsigned int)encodingLen;
    if (bufferSize < *algIdLen)
      break;

    /* If the buffer is big enough, encode into it.
     */
    status = VT_ERROR_INVALID_INPUT;
    temp = algId;
    VOLT_SET_FNCT_LINE (fnctLine)
    encodingLen = i2d_Asn1AlgorithmId (algIdObj, &algId);
    if (encodingLen == 0)
      break;

    *algIdLen = (unsigned int)encodingLen;

    status = 0;

  } while (0);

  if (algIdObj != (Asn1AlgorithmId *)0)
    Asn1AlgorithmId_free (algIdObj);

  VOLT_LOG_ERROR_COMPARE (
    status, (VtLibCtx)libCtx, status, VT_ERROR_TYPE_PRIMARY,
    fnctLine, "VoltEncodeAlgId", (char *)0)

  return (status);
}

int VoltDecodeAlgIdCreate (
   VoltLibCtx *libCtx,
   unsigned char *encoding,
   unsigned int maxEncodingLen,
   Asn1AlgorithmId **algId
   )
{
  int status;
  Asn1AlgorithmId *newAlgId = (Asn1AlgorithmId *)0;
  unsigned char *temp;
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    /* Create the object.
     */
    status = VT_ERROR_MEMORY;
    VOLT_SET_FNCT_LINE (fnctLine)
    newAlgId = Asn1AlgorithmId_new ();
    if (newAlgId == (Asn1AlgorithmId *)0)
      break;

    /* Decode.
     */
    status = VT_ERROR_UNKNOWN_BER;
    temp = encoding;
    VOLT_SET_FNCT_LINE (fnctLine)
    d2i_Asn1AlgorithmId (&newAlgId, &temp, maxEncodingLen);

    /* Did it work?
     */
    if (newAlgId == (Asn1AlgorithmId *)0)
      break;

    /* If successful, return the object.
     */
    *algId = newAlgId;
    status = 0;

  } while (0);

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

  /* If there was an error, destroy what we created.
   */
  if (newAlgId != (Asn1AlgorithmId *)0)
    Asn1AlgorithmId_free (newAlgId);

  VOLT_LOG_ERROR(
    (VtLibCtx)libCtx, status, VT_ERROR_TYPE_PRIMARY,
    fnctLine, "VoltDecodeAlgIdCreate", (char *)0)

  return (status);
}

⌨️ 快捷键说明

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