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

📄 smparams.c

📁 voltage 公司提供的一个开发Ibe的工具包
💻 C
字号:
/* Copyright 2003-2005, Voltage Security, all rights reserved.
 */
#include "vibe.h"
#include "environment.h"
#include "base.h"
#include "libctx.h"
#include "securemail.h"
#include "stringutil.h"

int VtSecureMailParamRecipientList (
   VtSecureMailObject secureMailObj,
   Pointer info,
   unsigned int flag
   )
{
  int status;
  VoltSecureMailObject *obj = (VoltSecureMailObject *)secureMailObj;
  VoltSecureMailWriteCtx *writeCtx;

  if (flag == VOLT_SECURE_MAIL_SET_TYPE_FLAG)
  {
    /* The SecureMail object must be set to write.
     */
    if (obj->state != VOLT_SECURE_MAIL_STATE_WRITE_SET)
      return (VT_ERROR_INVALID_SECURE_MAIL_OBJ);

    /* Set the enveloper with this list.
     */
    status = VtSetPkcs7Param (
      obj->p7EnvelopedData, VtPkcs7ParamRecipientList, info);
    if (status != 0)
      return (status);

    /* We need a reference to the recipient list.
     */
    writeCtx = (VoltSecureMailWriteCtx *)(obj->localCtx);
    return (VtGetPkcs7Param (
      obj->p7EnvelopedData, VtPkcs7ParamRecipientList,
      (Pointer *)&(writeCtx->recipListRef)));
  }

  if (flag == VOLT_SECURE_MAIL_GET_TYPE_FLAG)
  {
    /* If getting, the SecureMail object must be set to read.
     */
    if ((obj->state & VOLT_SECURE_MAIL_STATE_READ) == 0)
      return (VT_ERROR_INVALID_SECURE_MAIL_OBJ);

    /* Get the list out of the enveloper.
     */
    return (VtGetPkcs7Param (
      obj->p7EnvelopedData, VtPkcs7ParamRecipientList, (Pointer *)info));
  }

  /* If we get this far, the flag was not appropriate.
   */
  return (VT_ERROR_INVALID_TYPE);
}

int VtSecureMailParamRecipientIndex (
   VtSecureMailObject secureMailObj,
   Pointer info,
   unsigned int flag
   )
{
  VoltSecureMailObject *obj = (VoltSecureMailObject *)secureMailObj;

  /* This Param cannot Get info.
   */
  if (flag == VOLT_SECURE_MAIL_GET_TYPE_FLAG)
    return (VT_ERROR_INVALID_GET);

  if (flag != VOLT_SECURE_MAIL_SET_TYPE_FLAG)
    return (VT_ERROR_INVALID_TYPE);

  /* The SecureMail object must be set to read.
   */
  if ((obj->state & VOLT_SECURE_MAIL_STATE_READ) == 0)
    return (VT_ERROR_INVALID_SECURE_MAIL_OBJ);

  /* Set the enveloper with this list.
   */
  return (VtSetPkcs7Param (
    obj->p7EnvelopedData, VtPkcs7ParamRecipientIndex, info));
}

int VtSecureMailParamContentType (
   VtSecureMailObject secureMailObj,
   Pointer info,
   unsigned int flag
   )
{
  int status;
  unsigned int index, valueLen, labelLen, currentLen;
  Pointer *getInfo;
  VoltSecureMailObject *obj = (VoltSecureMailObject *)secureMailObj;
  VoltLibCtx *libCtx = (VoltLibCtx *)(obj->voltObject.libraryCtx);
  VoltSecureMailWriteCtx* writeCtx;
  VoltSecureMail2ReadCtx* readCtx;
  VtUtf8StringList *descriptors;
  char *label = "content-type: ";

  labelLen = 14;

  do
  {
    if (flag == VOLT_SECURE_MAIL_SET_TYPE_FLAG)
    {
      /* The SecureMail object must be set to write.
       */
      status = VT_ERROR_INVALID_SECURE_MAIL_OBJ;
      if (obj->state != VOLT_SECURE_MAIL_STATE_WRITE_SET)
        break;

      /* The info must be a UTF-8 string.
       */
      status = VT_ERROR_INVALID_ASSOCIATED_INFO;
      if (info == (Pointer)0)
        break;

      if (obj->formatType == VOLT_MESSAGE_FORMAT_SECURE_MAIL_2)
      {
        writeCtx = (VoltSecureMailWriteCtx*) secureMailObj->localCtx;
        
        /* Free up existing string */
        if (writeCtx->contentType != (unsigned char*)0);
        {
          Z2Free(writeCtx->contentType);
          writeCtx->contentType = (unsigned char*)0;
        }
        
        /* Copy to writeCtx */
        status = Strdup(info, (char**)&writeCtx->contentType, libCtx);
      }
      else
      {
        /* If there's already a content-type in the contentInfo, error.
        */
        status = VT_ERROR_INVALID_SECURE_MAIL_OBJ;
        if (obj->contentInfo.data != (unsigned char *)0)
          break;

        /* How long is the input value.
        */
        valueLen = Z2Strlen (info);

        /* Copy it into the object.
        */
        status = VT_ERROR_MEMORY;
        obj->contentInfo.data = (unsigned char *)Z2Malloc (valueLen + 1, 0);
        if (obj->contentInfo.data == (unsigned char *)0)
          break;
        Z2Memcpy (obj->contentInfo.data, info, valueLen + 1);

        /* We'll write out everything except the NULL-terminating
        * character.
        */
        obj->contentInfo.len = valueLen;
      }
      
      status = 0;
      break;
    }

    if (flag == VOLT_SECURE_MAIL_GET_TYPE_FLAG)
    {
      status = VT_ERROR_INVALID_SECURE_MAIL_OBJ;
      if ((obj->state & VOLT_SECURE_MAIL_STATE_READ) == 0)
        break;
        
      if (obj->formatType == VOLT_MESSAGE_FORMAT_SECURE_MAIL_2)
      {
        readCtx = (VoltSecureMail2ReadCtx*) secureMailObj->localCtx;

        status = VT_ERROR_GET_INFO_UNAVAILABLE;
        if (readCtx->contentType == (unsigned char*)0)
          break;
          
        *(Pointer*)info = readCtx->contentType;
      }
      else
      {
        /* Get all the content descriptors.
        */
        status = VtGetSecureMailParam (
          secureMailObj, VtSecureMailParamContentDescriptors,
          (Pointer *)&descriptors);
        if (status != 0)
          break;

        /* Search through the list, find the string that begins
        * "content-type:".
        */
        for (index = 0; index < descriptors->count; ++index)
        {
          currentLen = Z2Strlen (descriptors->utf8Strings[index]);
          if (currentLen < labelLen)
            continue;

          if (Z2Memcmp (descriptors->utf8Strings[index], label, labelLen) == 0)
            break;
        }

        /* If we found a match, we broke out of the for loop early. If we
        * went through the entire list, there was no match.
        */
        status = VT_ERROR_GET_INFO_UNAVAILABLE;
        if (index >= descriptors->count)
          break;

        getInfo = (Pointer *)info;
        *getInfo = (Pointer)(descriptors->utf8Strings[index] + labelLen);
      }
      
      status = 0;
      break;
    }

    /* If we get this far, the flag was not appropriate.
     */
    status = VT_ERROR_INVALID_TYPE;

  } while (0);

  return (status);
}

int VtSecureMailParamContentDescriptors (
   VtSecureMailObject secureMailObj,
   Pointer info,
   unsigned int flag
   )
{
  int status;
  unsigned int bufferSize, index;
  Pointer *getInfo;
  VoltSecureMailObject *obj = (VoltSecureMailObject *)secureMailObj;
  VoltSecureMailReadCtx *readCtx = (VoltSecureMailReadCtx *)(obj->localCtx);
  VoltLibCtx *libCtx = (VoltLibCtx *)(obj->voltObject.libraryCtx);
  VoltContentMaterial *currentElement;

  do
  {
    /* This param can GetParam only.
     */
    status = VT_ERROR_INVALID_SET;
    if (flag == VOLT_SECURE_MAIL_SET_TYPE_FLAG)
      break;

    status = VT_ERROR_INVALID_TYPE;
    if (flag != VOLT_SECURE_MAIL_GET_TYPE_FLAG)
      break;

    getInfo = (Pointer *)info;

    /* Make sure this object is set to read SecureMail and that the
     * content descriptors have been read.
     */
    status = VT_ERROR_INVALID_SECURE_MAIL_OBJ;
    if (readCtx->contentMaterialState != VOLT_CONTENT_MATERIAL_STATE_COMPLETE)
      break;

    /* If the list is not yet computed, compute it now.
     */
    if (readCtx->contentDescriptors.count != readCtx->contentMaterialCount)
    {
      /* The buffer size is the size of an array, we'll just fill it
       * with the pointers to the data already copied.
       */
      status = VT_ERROR_MEMORY;
      bufferSize = readCtx->contentMaterialCount * sizeof (unsigned char *);
      readCtx->contentDescriptors.utf8Strings = (unsigned char **)Z2Realloc (
        readCtx->contentDescriptors.utf8Strings, bufferSize);
      if (readCtx->contentDescriptors.utf8Strings == (unsigned char **)0)
        break;
      Z2Memset (readCtx->contentDescriptors.utf8Strings, 0, bufferSize);
      readCtx->contentDescriptors.count = readCtx->contentMaterialCount;

      /* Load up all the addresses.
       */
      currentElement = readCtx->contentMaterial;
      for (index = 0; index < readCtx->contentMaterialCount; ++index)
      {
        readCtx->contentDescriptors.utf8Strings[index] =
          currentElement->material.data;

        currentElement = (VoltContentMaterial *)(currentElement->nextElement);
      }
    }

    *getInfo = (Pointer)&(readCtx->contentDescriptors);

    status = 0;
  } while (0);

  return (status);
}

int VtSecureMailParamSignerList (
   VtSecureMailObject secureMailObj,
   Pointer info,
   unsigned int flag
   )
{
  int status;
  Pointer *getInfo;
  VoltSecureMailObject *obj = (VoltSecureMailObject *)secureMailObj;

  do
  {
    /* This param can GetParam only.
     */
    status = VT_ERROR_INVALID_SET;
    if (flag == VOLT_SECURE_MAIL_SET_TYPE_FLAG)
      break;

    status = VT_ERROR_INVALID_TYPE;
    if (flag != VOLT_SECURE_MAIL_GET_TYPE_FLAG)
      break;

    getInfo = (Pointer *)info;

    /* The object must be done reading a message.
     */
    status = VT_ERROR_GET_INFO_UNAVAILABLE;
    if (obj->state != VOLT_SECURE_MAIL_STATE_READ_COMPLETE)
      break;

    /* Get the info out of the P7 object.
     */
    status = VtGetPkcs7Param (
      obj->p7SignedData, VtPkcs7ParamSignerList, getInfo);

  } while (0);

  return (status);
}

int VtSecureMailParamMessageTime (
   VtSecureMailObject secureMailObj,
   Pointer info,
   unsigned int flag
   )
{
  int status;
  Pointer *getInfo;
  VoltSecureMailObject *obj = (VoltSecureMailObject *)secureMailObj;

  do
  {
    /* This param can GetParam only.
     */
    status = VT_ERROR_INVALID_SET;
    if (flag == VOLT_SECURE_MAIL_SET_TYPE_FLAG)
      break;

    status = VT_ERROR_INVALID_TYPE;
    if (flag != VOLT_SECURE_MAIL_GET_TYPE_FLAG)
      break;

    getInfo = (Pointer *)info;

    /* The object must be done reading a message.
     */
    status = VT_ERROR_GET_INFO_UNAVAILABLE;
    if (obj->state != VOLT_SECURE_MAIL_STATE_READ_COMPLETE)
      break;

    /* Get the info out of the P7 object.
     */
    status = VtGetPkcs7Param (
      obj->p7SignedData, VtPkcs7ParamSigningTime, getInfo);

  } while (0);

  return (status);
}

⌨️ 快捷键说明

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