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

📄 encodeid.c

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

/* Set up the OpenSSL ASN.1 templates.
 */
ASN1_SEQUENCE (Asn1Identity) =
{
  ASN1_OPT (Asn1Identity, version, ASN1_INTEGER),
  ASN1_SEQUENCE_OF_OPT (Asn1Identity, profiles, Asn1ObjectId),
  ASN1_SIMPLE (Asn1Identity, district, ASN1_UTF8STRING),
  ASN1_SIMPLE (Asn1Identity, schema, Asn1Encoded)
} ASN1_SEQUENCE_END (Asn1Identity);

IMPLEMENT_ASN1_FUNCTIONS (Asn1Identity)

int VoltEncodeIdentity (
   VoltIdentityObject *idObj,
   unsigned int version,
   unsigned char *encoding,
   unsigned int bufferSize,
   unsigned int *encodingLen
   )
{
  int status, asn1Ret;
  unsigned int index, totalLen, schemaEncodingLen;
  VoltLibCtx *libCtx = (VoltLibCtx *)(idObj->voltObject.libraryCtx);
  VoltDistrictObject *distObj = (VoltDistrictObject *)(idObj->district);
  VtItem *distOid;
  VoltIdentitySchema *schema = idObj->schema;
  unsigned char *schemaEncoding = (unsigned char *)0;
  Asn1ObjectId *oid = (Asn1ObjectId *)0;
  ASN1_INTEGER *vers = (ASN1_INTEGER *)0;
  STACK *profiles = (STACK *)0;
  Asn1Identity *ident = (Asn1Identity *)0;
  unsigned char *temp;
  VOLT_DECLARE_ERROR_TYPE (errorType)
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  *encodingLen = 0;

  /* If the encoding is already there, free it.
   */
  if (idObj->encoding.data != (unsigned char *)0)
    Z2Free (idObj->encoding.data);
  idObj->encoding.data = (unsigned char *)0;
  idObj->encoding.len = 0;
  idObj->encodingVersion = 0;

  do
  {
    /* Does the district support the schema in the id object? Go through
     * the district's list of supported schemas.
     */
    for (index = 0; index < distObj->keySchemas.count; ++index)
    {
      distOid = &(distObj->keySchemas.oids[index]);

      /* If the OID's match, use the given schema.
       */
      if (schema->oid.len != distOid->len)
        continue;

      if (Z2Memcmp (schema->oid.data, distOid->data, distOid->len) == 0)
        break;
    }

    /* Did we run through all the supported schemas and not find a match?
     */
    VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_NO_MATCHING_SCHEMA;
    if (index >= distObj->keySchemas.count)
      break;

    /* Encode the schema.
     */
    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = schema->EncodeSchemaAlloc (
      (VtIdentityObject)idObj, (Pointer)schema, version,
      &schemaEncoding, &schemaEncodingLen);
    if (status != 0)
      break;

    /* Create the struct.
     */
    VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_MEMORY;
    ident = Asn1Identity_new ();
    if (ident == (Asn1Identity *)0)
      break;

    /* Set the district name.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    asn1Ret = ASN1_STRING_set (
      ident->district, distObj->qualDistrictName.data,
      distObj->qualDistrictName.len);
    if (asn1Ret != 1)
      break;

    /* Set the schema.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    asn1Ret = Asn1Encoded_set (
      ident->schema, schemaEncoding, schemaEncodingLen);
    if (asn1Ret != 1)
      break;

    if (version == VT_ENCODE_IBCS_2_V_1)
    {
      /* Version 1 exercises the option on the profiles, create the
       * object inside the Identity template.
       * Although the definition says this is a SEQUENCE OF, the
       * defintion in v1 is confined to one OID. Hence, just build one
       * ASN1_OBJECT.
       * We know the schema is email, otherwise the encodeSchema
       * function would have returned an error.
       */
      VOLT_SET_FNCT_LINE (fnctLine)
      oid = Asn1ObjectId_new ();
      if (oid == (Asn1ObjectId *)0)
        break;

      VOLT_SET_FNCT_LINE (fnctLine)
      asn1Ret = Asn1ObjectId_set (oid, schema->oid.data, schema->oid.len);
      if (asn1Ret != 1)
        break;

      profiles = sk_new_null ();
      if (profiles == (STACK *)0)
        break;

      VOLT_SET_FNCT_LINE (fnctLine)
      status = VT_ERROR_INVALID_INPUT;
      asn1Ret = sk_push (profiles, (char *)oid);
      if (asn1Ret == 0)
        break;

      ident->profiles = profiles;
    }
    else
    {
      /* Version 2 uses the version number.
       */
      VOLT_SET_FNCT_LINE (fnctLine)
      vers = ASN1_INTEGER_new ();
      if (vers == (ASN1_INTEGER *)0)
        break;
      ASN1_INTEGER_set (vers, VT_ENCODE_IBCS_2_V_2);

      ident->version = vers;
    }

    /* How big does the buffer need to be?
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_INPUT;
    totalLen = (unsigned int)i2d_Asn1Identity (ident, (unsigned char **)0);
    if (totalLen == 0)
      break;

    /* Allocate the space.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_MEMORY;
    idObj->encoding.data = (unsigned char *)Z2Malloc (totalLen, 0);
    if (idObj->encoding.data == (unsigned char *)0)
      break;

    /* Encode into the buffer.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_INPUT;
    temp = idObj->encoding.data;
    totalLen = (unsigned int)i2d_Asn1Identity (ident, &temp);
    if (totalLen == 0)
      break;

    idObj->encoding.len = totalLen;
    idObj->encodingVersion = version;

    /* Is the supplied buffer big enough?
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_BUFFER_TOO_SMALL;
    *encodingLen = totalLen;
    if (bufferSize < totalLen)
      break;

    Z2Memcpy (encoding, idObj->encoding.data, idObj->encoding.len);
    status = 0;

  } while (0);

  if (schemaEncoding != (unsigned char *)0)
    Z2Free (schemaEncoding);

  if (oid != (Asn1ObjectId *)0)
    Asn1ObjectId_free (oid);

  if (profiles != (STACK *)0)
  {
    sk_zero (profiles);
    sk_free (profiles);
  }
  if (vers != (ASN1_INTEGER *)0)
    ASN1_INTEGER_free (vers);

  if (ident != (Asn1Identity *)0)
  {
    ident->version = (ASN1_INTEGER *)0;
    ident->profiles = (STACK *)0;

    Asn1Identity_free (ident);
  }

  VOLT_LOG_ERROR_INFO_COMPARE (
    status, 0, idObj, status, 0, errorType,
    (char *)0, "VoltEncodeIdentity", fnctLine, (char *)0)

  return (status);
}

void VoltIsSameIdentity(
   VtIdentityObject id1,
   VtIdentityObject id2,
   unsigned int *compareResult
   )
{
  VoltIdentityObject *obj1;
  VoltIdentityObject *obj2;
  VoltLibCtx *libCtx;

  *compareResult = 0;

  if ( (id1 == (VtIdentityObject)0) || (id2 == (VtIdentityObject)0) )
    return;

  if (VOLT_OBJECT_TYPE_NOT_EQUAL (id1, VOLT_OBJECT_TYPE_IDENTITY))
    return;
  if (VOLT_OBJECT_TYPE_NOT_EQUAL (id2, VOLT_OBJECT_TYPE_IDENTITY))
    return;

  obj1 = (VoltIdentityObject *)id1;
  obj2 = (VoltIdentityObject *)id2;

  /* This function compares encoded ID's only.
   */
  if ( (obj1->encoding.data == (unsigned char *)0) ||
       (obj2->encoding.data == (unsigned char *)0) )
    return;

  libCtx = (VoltLibCtx *)(obj1->voltObject.libraryCtx);

  if (obj1->encoding.len != obj2->encoding.len)
    return;

  if (Z2Memcmp (
    obj1->encoding.data, obj2->encoding.data, obj1->encoding.len) != 0)
    return;

  *compareResult = 1;
}

⌨️ 快捷键说明

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