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

📄 zdmwrite.c

📁 IBE是一种非对称密码技术
💻 C
📖 第 1 页 / 共 4 页
字号:
/* Copyright 2003-2006, Voltage Security, all rights reserved.
 */
#include "vibe.h"
#include "environment.h"
#include "base.h"
#include "libctx.h"
#include "securemail.h"
#include "zdm.h"
#include "idobj.h"
#include "policy.h"
#include "derhelp.h"
#include "oidlist.h"
#include "errorctx.h"

/* Given a ZDM template, build the header and footer to the ZDM data.
 * <p>This function will allocate two buffers, one to hold the header,
 * another the footer. It is the responsibility of the caller to free
 * that memory.
 * <p>The policy, storage, and transport contexts will almost certainly
 * not be needed. But because this function will call VtEncodeIdentity,
 * include them int the arg list so that the contexts to use are
 * explicitly passed to the Encode function.
 * <p>This routine does no argument checking. It is the responsibility
 * of the caller not to make mistakes.
 *
 * @param libCtx The libCtx to use (to allocate, for instance).
 * @param obj
 * @param writeCtx
 * @param header Where the function will deposit the pointer to the
 * allocated memory containing the header.
 * @param headerLen Where the function will deposit the length of the
 * header.
 * @param footer Where the function will deposit the pointer to the
 * allocated memory containing the footer.
 * @param footerLen Where the function will deposit the length of the
 * footer.
 * @return an int, 0 if the function completed successfully or a
 * non-zero error code.
 */
static int VOLT_CALLING_CONV BuildZDMHeaderFooterAlloc VOLT_PROTO_LIST ((
   VoltLibCtx *libCtx,
   VoltSecureMailObject *obj,
   VoltSecureMailWriteCtx *writeCtx,
   VtPolicyCtx policyCtx,
   VtStorageCtx storageCtx,
   VtTransportCtx transportCtx,
   VtItem *zdmTemplate,
   unsigned char **header,
   unsigned int *headerLen,
   unsigned char **footer,
   unsigned int *footerLen
));

int VoltOldZDMWriteInit (
   VtZDMObject zdmObj,
   VtPolicyCtx policyCtx,
   VtStorageCtx storageCtx,
   VtTransportCtx transportCtx,
   VtRandomObject random
   )
{
  int status;
  VoltZDMObject *zObj = (VoltZDMObject *)zdmObj;
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  VOLT_SET_FNCT_LINE (fnctLine)
  status = VoltZDMWriteInit (
    (VtSecureMailObject)(zObj->localCtx), policyCtx, storageCtx,
    transportCtx, random);

  VOLT_LOG_ERROR_INFO_COMPARE (
    status, 0, zdmObj, status, 0, 0,
    (char *)0, "VoltOldZDMWriteInit", fnctLine, (char *)0)

  return (status);
}

int VoltOldZDMWriteUpdate(
   VtZDMObject zdmObj,
   VtRandomObject random,
   unsigned char *inputData,
   unsigned int inputDataLen,
   unsigned char *message,
   unsigned int bufferSize,
   unsigned int *messageLen
   )
{
  int status;
  VoltZDMObject *zObj = (VoltZDMObject *)zdmObj;
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  VOLT_SET_FNCT_LINE (fnctLine)
  status = VoltSecureMailWriteUpdate (
    (VtSecureMailObject)(zObj->localCtx), random, inputData, inputDataLen,
    message, bufferSize, messageLen);

  VOLT_LOG_ERROR_INFO_COMPARE (
    status, 0, zdmObj, status, 0, 0,
    (char *)0, "VoltOldZDMWriteUpdate", fnctLine, (char *)0)

  return (status);
}

int VoltOldZDMWriteFinal (
   VtZDMObject zdmObj,
   VtRandomObject random,
   unsigned char *inputData,
   unsigned int inputDataLen,
   unsigned char *message,
   unsigned int bufferSize,
   unsigned int *messageLen
   )
{
  int status;
  VoltZDMObject *zObj = (VoltZDMObject *)zdmObj;
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  VOLT_SET_FNCT_LINE (fnctLine)
  status = VoltSecureMailWriteFinal (
    (VtSecureMailObject)(zObj->localCtx), random, inputData, inputDataLen,
    message, bufferSize, messageLen);

  VOLT_LOG_ERROR_INFO_COMPARE (
    status, 0, zdmObj, status, 0, 0,
    (char *)0, "VoltOldZDMWriteFinal", fnctLine, (char *)0)

  return (status);
}

int VoltZDMWriteInit (
   VtSecureMailObject secureMailObj,
   VtPolicyCtx policyCtx,
   VtStorageCtx storageCtx,
   VtTransportCtx transportCtx,
   VtRandomObject random
   )
{
  int status;
  unsigned int index, elementLen, newLineLen, headerLen, footerLen;
  VoltSecureMailObject *obj = (VoltSecureMailObject *)secureMailObj;
  VoltSecureMailWriteCtx *writeCtx = (VoltSecureMailWriteCtx *)(obj->localCtx);
  VoltLibCtx *libCtx = (VoltLibCtx *)(obj->voltObject.libraryCtx);
  VoltPolicyCtx *pCtx;
  unsigned char *header = (unsigned char *)0;
  unsigned char *footer = (unsigned char *)0;
  VtItem *getItem = (VtItem *)0;
  unsigned char *newLine =
    writeCtx->itemArray[VOLT_WRITE_SM_ITEM_NEW_LINE].data;
  char *contentType = VOLT_DEFAULT_CONTENT_TYPE;
  VtBase64Info b64Info;
  VOLT_DECLARE_ERROR_TYPE (errorType)
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  newLineLen = writeCtx->itemArray[VOLT_WRITE_SM_ITEM_NEW_LINE].len;

  do
  {
    /* Get the ZDM template out of the policy ctx. If there is no
     * policy ctx, error.
     */
    VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_PROVIDER_USE;
    if (policyCtx == (VtPolicyCtx)0)
      break;

    pCtx = (VoltPolicyCtx *)policyCtx;

    /* Get the ZDR template.
     */
    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = pCtx->PolicyGetInfoAlloc (
      policyCtx, VOLT_POLICY_GET_ZERO_DOWNLOAD_TEMPLATE,
      (Pointer)0, (Pointer *)&getItem);
    if (status != 0)
      break;

    /* If the policy provider has no template, error.
     */
    VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_ZDM_TEMPLATE;
    if (getItem == (VtItem *)0)
      break;

    /* If this is ZDM, Init subordinate objects. If this is ZDM from
     * SecureMail or SecureFile, there are no subordinate objects.
     */
    if (obj->p7SignedData != (VtPkcs7Object)0)
    {
      b64Info.base64BlockSize = 76;
      b64Info.newLineCharacter = VT_BASE64_NEW_LINE_LF;
      if (writeCtx->itemArray[VOLT_WRITE_SM_ITEM_NEW_LINE].len == 2)
        b64Info.newLineCharacter = VT_BASE64_NEW_LINE_CR_LF;
      b64Info.errorCheck = VT_BASE64_NO_ERROR_CHECK;
      VOLT_SET_ERROR_TYPE (errorType, 0)
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VtCreateAlgorithmObject (
        (VtLibCtx)libCtx, VtAlgorithmImplBase64, (Pointer)&b64Info,
        &(obj->base64));
      if (status != 0)
        break;

      VOLT_SET_FNCT_LINE (fnctLine)
      status = VtPkcs7WriteInit (
        obj->p7SignedData, policyCtx, storageCtx, transportCtx, random);
      if (status != 0)
        break;

      VOLT_SET_FNCT_LINE (fnctLine)
      status = VtPkcs7WriteInit (
        obj->p7EnvelopedData, policyCtx, storageCtx, transportCtx, random);
      if (status != 0)
        break;

      VOLT_SET_FNCT_LINE (fnctLine)
      status = VtEncodeInit (obj->base64);
      if (status != 0)
        break;
    }

    /* If there's no content type, use the default.
     */
    if ( (obj->contentInfo.data == (unsigned char *)0) &&
         (obj->formatType != VOLT_MESSAGE_FORMAT_ZDM_ATTACHMENT) )
    {
      VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VT_ERROR_MEMORY;
      obj->contentInfo.data = (unsigned char *)Z2Malloc (
        VOLT_DEFAULT_CONTENT_TYPE_LEN + 1, 0);
      if (obj->contentInfo.data == (unsigned char *)0)
        break;
      Z2Memcpy (
        obj->contentInfo.data, contentType, VOLT_DEFAULT_CONTENT_TYPE_LEN);
      obj->contentInfo.data[VOLT_DEFAULT_CONTENT_TYPE_LEN] = 0;
      obj->contentInfo.len = VOLT_DEFAULT_CONTENT_TYPE_LEN;
    }

    /* If this is an attachment, get rid of the "regular" headers and
     * footers.
     */
    if (obj->formatType == VOLT_MESSAGE_FORMAT_ZDM_ATTACHMENT)
    {
      for (index = VOLT_WRITE_SM_HEAD_INDEX_START + 1;
           index <= VOLT_WRITE_SM_HEAD_INDEX_END; ++index)
        writeCtx->itemArray[index].len = 0;

      for (index = VOLT_WRITE_SM_FOOT_INDEX_START;
           index <= VOLT_WRITE_SM_FOOT_INDEX_END; ++index)
        writeCtx->itemArray[index].len = 0;
    }

    /* The ZDM headers and footers need info from the encoded sender
     * and recipients, so call this function after P7 Inits, so the
     * objects are ready.
     */
    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = BuildZDMHeaderFooterAlloc (
      libCtx, obj, writeCtx, policyCtx, storageCtx, transportCtx,
      getItem, &header, &headerLen, &footer, &footerLen);
    if (status != 0)
      break;

    /* If this is an attachment, the footer has an extra line return.
     */
    if (obj->formatType == VOLT_MESSAGE_FORMAT_ZDM_ATTACHMENT)
    {
      Z2Memmove (
        writeCtx->itemArray[VOLT_WRITE_SM_ITEM_END_MSG].data,
        writeCtx->itemArray[VOLT_WRITE_SM_ITEM_END_MSG].data + newLineLen,
        writeCtx->itemArray[VOLT_WRITE_SM_ITEM_END_MSG].len - newLineLen);
      writeCtx->itemArray[VOLT_WRITE_SM_ITEM_END_MSG].len -= newLineLen;
    }

    /* How much header and footer data will we be writing out?
     */
    for (index = VOLT_WRITE_SM_HEAD_INDEX_START;
         index <= VOLT_WRITE_SM_HEAD_INDEX_END; ++index)
    {
      /* Add in the length of the actual data to write out.
       * If there is data to write out, add a newLine.
       */
      elementLen = writeCtx->itemArray[index].len;
      if (elementLen != 0)
        elementLen += newLineLen;
      writeCtx->prelimLen += elementLen;
    }

    for (index = VOLT_WRITE_SM_FOOT_INDEX_START;
         index <= VOLT_WRITE_SM_FOOT_INDEX_END; ++index)
    {
      /* Add in the length of the actual data to write out.
       * If there is data to write out, add a newLine.
       */
      elementLen = writeCtx->itemArray[index].len;
      if (elementLen != 0)
        elementLen += newLineLen;
      writeCtx->trailLen += elementLen;
    }

    obj->state = VOLT_SECURE_MAIL_STATE_WRITE_INIT;

  } while (0);

  if (getItem != (VtItem *)0)
    pCtx->PolicyGetInfoFree (policyCtx, (Pointer)getItem);

  if (header != (unsigned char *)0)
    Z2Free (header);
  if (footer != (unsigned char *)0)
    Z2Free (footer);

  VOLT_LOG_ERROR_INFO_COMPARE (
    status, 0, secureMailObj, status, 0, errorType,
    (char *)0, "VoltZDMWriteInit", fnctLine, (char *)0)

  return (status);
}

#define VOLT_ZDM_TEMPLATE_NUM_TERMS  3

typedef struct
{
  int start;
  int finish;
  int index;
} SearchT;

static int VOLT_CALLING_CONV FindSort VOLT_PROTO_LIST ((
   VoltLibCtx *libCtx, char **terms, unsigned int termCount, 
   SearchT *search, int *count, char *templateData
));

static int VOLT_CALLING_CONV AddZDMHeaderFooterData VOLT_PROTO_LIST ((
   VoltLibCtx *libCtx,
   VoltSecureMailObject *obj,
   VoltSecureMailWriteCtx *writeCtx,
   int headerFooter,
   char *theData,
   unsigned int theDataLen
));

static int VOLT_CALLING_CONV BuildZDMDataFromMETA VOLT_PROTO_LIST ((
   VoltLibCtx *libCtx,
   VoltSecureMailObject *obj,
   VoltSecureMailWriteCtx *writeCtx,
   int headerFooter
));

static int VOLT_CALLING_CONV BuildZDMDataFromSCRIPT VOLT_PROTO_LIST ((
   VoltLibCtx *libCtx,
   VoltSecureMailObject *obj,
   VoltSecureMailWriteCtx *writeCtx,
   int headerFooter
));

static int VOLT_CALLING_CONV BuildZDMDataFromFORM VOLT_PROTO_LIST ((
   VoltLibCtx *libCtx,
   VoltSecureMailObject *obj,
   VoltSecureMailWriteCtx *writeCtx,
   VtPolicyCtx policyCtx,
   VtStorageCtx storageCtx,
   VtTransportCtx transportCtx,
   int headerFooter
));

static int BuildZDMHeaderFooterAlloc(
   VoltLibCtx *libCtx,
   VoltSecureMailObject *obj,
   VoltSecureMailWriteCtx *writeCtx,
   VtPolicyCtx policyCtx,
   VtStorageCtx storageCtx,
   VtTransportCtx transportCtx,
   VtItem *zdmTemplate,
   unsigned char **header,
   unsigned int *headerLen,
   unsigned char **footer,
   unsigned int *footerLen
   )
{
  int status, index, indexT, count, headerFooter;
  char *begin, *end;
  SearchT search[VOLT_ZDM_TEMPLATE_NUM_TERMS];
  char *terms[VOLT_ZDM_TEMPLATE_NUM_TERMS] =
    { "$(META)", "$(SCRIPT)", "$(FORM)" };
  VOLT_DECLARE_ERROR_TYPE (errorType)
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  *header = (unsigned char *)0;
  *headerLen = 0;
  *footer = (unsigned char *)0;
  *footerLen = 0;

  headerFooter = 0;

  do
  {
    /* First, find the terms in the template.
     */
    Z2Memset (&search, 0, sizeof (search));
    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = FindSort (
      libCtx, terms, VOLT_ZDM_TEMPLATE_NUM_TERMS,
      search, &count, (char *)(zdmTemplate->data));
    if (status != 0)
      break;

    /* There must be META and FORM.
     */
    VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_ZDM_TEMPLATE;
    if ( (search[0].index == -1) || (search[2].index == -1) )
      break;

    /* Write out the data up to the first tag. Then do that tag. Then
     * write the data after that tag up to the next. Then that tag. And
     * then up to the last tag, that tag and any data after.
     */
    begin = (char *)(zdmTemplate->data);
    for (index = 0; index < count; ++index)
    {
      for (indexT = 0; indexT < VOLT_ZDM_TEMPLATE_NUM_TERMS; ++indexT)
      {
        if (search[indexT].index != index)
          continue;

        end = (char *)(zdmTemplate->data + search[indexT].start);
        break;
      }

      VOLT_SET_ERROR_TYPE (errorType, 0)
      VOLT_SET_FNCT_LINE (fnctLine)
      status = AddZDMHeaderFooterData (
        libCtx, obj, writeCtx, headerFooter,
        begin, (unsigned int)(end - begin));
      if (status != 0)
        break;

      /* Place the data for the tag.
       */
      if (indexT == 0)
      {
        VOLT_SET_FNCT_LINE (fnctLine)
        status = BuildZDMDataFromMETA (libCtx, obj, writeCtx, headerFooter);
      }
      else if (indexT == 1)
      {
        VOLT_SET_FNCT_LINE (fnctLine)
        status = BuildZDMDataFromSCRIPT (libCtx, obj, writeCtx, headerFooter);
      }
      else
      {
        VOLT_SET_FNCT_LINE (fnctLine)
        status = BuildZDMDataFromFORM (
          libCtx, obj, writeCtx, policyCtx, storageCtx, transportCtx, 0);
        headerFooter = 1;
      }
      if (status != 0)
        break;

      begin = (char *)(zdmTemplate->data + search[indexT].finish);
    }
    if (status != 0)
      break;

    end = (char *)(zdmTemplate->data + zdmTemplate->len);
    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = AddZDMHeaderFooterData (
      libCtx, obj, writeCtx, headerFooter, begin, (unsigned int)(end - begin));

  } while (0);

⌨️ 快捷键说明

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