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

📄 zdmwritetype.c

📁 IBE是一种非对称密码技术
💻 C
📖 第 1 页 / 共 2 页
字号:
/* 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 "p7obj.h"
#include "errorctx.h"

int VtZDMImplWrite (
   VtZDMObject *object,
   Pointer info,
   unsigned int flag
   )
{
  int status;
  unsigned int bufferSize;
  VoltZDMObject *zObj = (VoltZDMObject *)(*object);
  VoltSecureMailObject *obj = (VoltSecureMailObject *)0;
  VoltLibCtx *libCtx = (VoltLibCtx *)(zObj->voltObject.libraryCtx);
  VoltSecureMailWriteCtx *writeCtx = (VoltSecureMailWriteCtx *)0;
  unsigned char *buffer = (unsigned char *)0;
  VtItem temp;
  unsigned char newLineChars[2] = { 0x0D, 0x0A };
  unsigned char *beginMsg = VOLT_SECURE_MAIL_BEGIN_MESSAGE;
  unsigned char *beginBlk = VOLT_SECURE_MAIL_BEGIN_BLOCK;
  unsigned char *endBlk = VOLT_SECURE_MAIL_END_BLOCK;
  unsigned char *endMsg = VOLT_SECURE_MAIL_END_MESSAGE;
  VOLT_DECLARE_ERROR_TYPE (errorType)
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    /* Make sure this is being called appropriately.
     */
    VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_TYPE;
    if (flag != VOLT_ZDM_SET_TYPE_FLAG)
      break;

    /* Make sure the object is empty.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_ZDM_OBJ;
    if (zObj->localCtx != (Pointer)0)
      break;

    /* Check the info, we're expecting NULL.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_ASSOCIATED_INFO;
    if (info != (Pointer)0)
      break;

    /* A ZDM object used to be a SecureMail object. However, for ZDMv2,
     * we have a VoltZDMObject. This "old" ZDM Impl has to fit inside
     * the new ZDMObject framework. What we'll do is build the old
     * SecureMail object and place it into the new ZDMObject's localCtx.
     */
    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VoltCreateObject (
      (VtLibCtx)libCtx, (VoltObject **)&obj, sizeof (VoltSecureMailObject),
      VOLT_OBJECT_TYPE_ZDM);
    if (status != 0)
      break;

    /* Build the local ctx.
     */
    bufferSize = sizeof (VoltSecureMailWriteCtx);

    VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_MEMORY;
    buffer = (unsigned char *)Z2Malloc (bufferSize, 0);
    if (buffer == (unsigned char *)0)
      break;
    Z2Memset (buffer, 0, bufferSize);

    writeCtx = (VoltSecureMailWriteCtx *)buffer;

    /* Build the P7 objects.
     */
    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VtCreatePkcs7Object (
      (VtLibCtx)libCtx, VtPkcs7ImplWriteSignedDSA, (Pointer)0,
      &(obj->p7SignedData));
    if (status != 0)
      break;

    VOLT_SET_FNCT_LINE (fnctLine)
    status = VtCreatePkcs7Object (
      (VtLibCtx)libCtx, VtPkcs7ImplWriteEnvelopeIBE, (Pointer)0,
      &(obj->p7EnvelopedData));
    if (status != 0)
      break;

    /* Copy the new line chars and the begin and end messages.
     * We may want to change the new line char later, so set it to the
     * default.
     */
    temp.data = newLineChars;
    temp.len = sizeof (newLineChars);
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VoltCopyItemDataAlloc (
      obj->voltObject.libraryCtx, 0, 0, &temp,
      &(writeCtx->itemArray[VOLT_WRITE_SM_ITEM_NEW_LINE]));
    if (status != 0)
      break;

    /* Place the BEGIN MESSAGE line in the HEADER Item because the ZDM
     * header material will go into the BEGIN_MSG Item.
     */
    temp.data = beginMsg;
    temp.len = Z2Strlen (beginMsg);
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VoltCopyItemDataAlloc (
      obj->voltObject.libraryCtx, 0, 0, &temp,
      &(writeCtx->itemArray[VOLT_WRITE_SM_ITEM_HEADER]));
    if (status != 0)
      break;

    temp.data = beginBlk;
    temp.len = VOLT_SECURE_MAIL_BEGIN_BLOCK_LEN;
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VoltCopyItemDataAlloc (
      obj->voltObject.libraryCtx, 0, 0, &temp,
      &(writeCtx->itemArray[VOLT_WRITE_SM_ITEM_BEGIN_BLK]));
    if (status != 0)
      break;

    temp.data = endBlk;
    temp.len = VOLT_SECURE_MAIL_END_BLOCK_LEN;
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VoltCopyItemDataAlloc (
      obj->voltObject.libraryCtx, 0, 0, &temp,
      &(writeCtx->itemArray[VOLT_WRITE_SM_ITEM_END_BLK]));
    if (status != 0)
      break;

    temp.data = endMsg;
    temp.len = VOLT_SECURE_MAIL_END_MESSAGE_LEN;
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VoltCopyItemDataAlloc (
      obj->voltObject.libraryCtx, 0, 0, &temp,
      &(writeCtx->itemArray[VOLT_WRITE_SM_ITEM_END_MSG]));
    if (status != 0)
      break;

    /* Initialize the format to MAIL, if the caller sets it to
     * ATTACHMENT later, we'll change it.
     */
    obj->formatType = VOLT_MESSAGE_FORMAT_ZDM_MAIL;
    obj->state = VOLT_SECURE_MAIL_STATE_WRITE_SET;
    obj->WriteInit = VoltZDMWriteInit;
    obj->WriteUpdate = VoltSecureMailWriteUpdate;
    obj->WriteFinal = VoltSecureMailWriteFinal;
    obj->GetEncodeDecodeSize = VoltBase64GetEncodeDecodeSize;
    obj->localCtx = (Pointer)buffer;
    obj->LocalCtxDestroy = VoltWriteSecureMailCtxDestroy;

    /* Now store this SecureMail object as the localCtx of the
     * ZDMObject.
     */
    zObj->localCtx = (Pointer)obj;
    zObj->LocalCtxDestroy = VoltOldZDMCtxDestroy;
    zObj->SetParam = VoltOldZDMSetParamWrite;
    zObj->GetParam = VoltOldZDMGetParamWrite;
    zObj->WriteInit = VoltOldZDMWriteInit;
    zObj->WriteUpdate = VoltOldZDMWriteUpdate;
    zObj->WriteFinal = VoltOldZDMWriteFinal;

  } while (0);

  /* If success, we're done.
   */
  if (status == 0)
    return (0);

  /* If error, free what we allocated.
   */
  VoltDestroyOldZDMObject ((VoltSecureMailObject **)&obj);

  VOLT_LOG_ERROR_INFO (
    0, *object, status, 0, errorType,
    (char *)0, "VtZDMImplWrite", fnctLine, (char *)0)

  return (status);
}

int VtZDMImplWriteFromSecureMail (
   VtZDMObject *object,
   Pointer info,
   unsigned int flag
   )
{
  int status;
  unsigned int bufferSize;
  VoltZDMObject *zObj = (VoltZDMObject *)(*object);
  VoltSecureMailObject *obj = (VoltSecureMailObject *)0;
  VoltSecureMailObject *smObj;
  VoltLibCtx *libCtx = (VoltLibCtx *)(zObj->voltObject.libraryCtx);
  VoltZDMFromSecureMailCtx *zdmCtx;
  VoltSecureMailWriteCtx *writeCtx = (VoltSecureMailWriteCtx *)0;
  VoltSecureMailWriteCtx *smWriteCtx;
  VtItem temp;
  unsigned char *buffer = (unsigned char *)0;
  unsigned char *beginMsg = VOLT_SECURE_MAIL_BEGIN_MESSAGE;
  unsigned char *beginBlk = VOLT_SECURE_MAIL_BEGIN_BLOCK;
  unsigned char *endBlk = VOLT_SECURE_MAIL_END_BLOCK;
  unsigned char *endMsg = VOLT_SECURE_MAIL_END_MESSAGE;
  VOLT_DECLARE_ERROR_TYPE (errorType)
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    /* Make sure this is being called appropriately.
     */
    VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_TYPE;
    if (flag != VOLT_ZDM_SET_TYPE_FLAG)
      break;

    /* Make sure the object is empty.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_ZDM_OBJ;
    if (zObj->localCtx != (Pointer)0)
      break;

    /* Check the info, we're expecting a SecureMail object.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_ASSOCIATED_INFO;
    if (info == (Pointer)0)
      break;

    VOLT_SET_FNCT_LINE (fnctLine)
    if (VOLT_OBJECT_TYPE_NOT_EQUAL (info, VOLT_OBJECT_TYPE_SECURE_MAIL))
      break;

    /* The SecureMail object must be initialized at least.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    smObj = (VoltSecureMailObject *)info;
    if ( (smObj->state == 0) ||
         (smObj->state == VOLT_SECURE_MAIL_STATE_WRITE_SET) )
      break;

    smWriteCtx = (VoltSecureMailWriteCtx *)(smObj->localCtx);

    /* A ZDM object used to be a SecureMail object. However, for ZDMv2,
     * we have a VoltZDMObject. This "old" ZDM Impl has to fit inside
     * the new ZDMObject framework. What we'll do is build the old
     * SecureMail object and place it into the new ZDMObject's localCtx.
     */
    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VoltCreateObject (
      (VtLibCtx)libCtx, (VoltObject **)&obj, sizeof (VoltSecureMailObject),
      VOLT_OBJECT_TYPE_ZDM);
    if (status != 0)
      break;

    /* Build the local ctx.
     */
    bufferSize = sizeof (VoltZDMFromSecureMailCtx);

    VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_MEMORY;
    buffer = (unsigned char *)Z2Malloc (bufferSize, 0);
    if (buffer == (unsigned char *)0)
      break;
    Z2Memset (buffer, 0, bufferSize);

    writeCtx = (VoltSecureMailWriteCtx *)buffer;
    zdmCtx = (VoltZDMFromSecureMailCtx *)buffer;

    /* From SecureMail does no encoding of the input data.
     */
    if (obj->base64 != (VtAlgorithmObject)0)
      VtDestroyAlgorithmObject (&(obj->base64));

    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VtCreateAlgorithmObject (
      (VtLibCtx)libCtx, VtAlgorithmImplCopy, (Pointer)0, &(obj->base64));
    if (status != 0)
      break;

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

    /* Whatever the new line char is in the SecureMail object, copy it
     * here.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VoltCopyItemDataAlloc (
      obj->voltObject.libraryCtx, 0, 0,
      &(smWriteCtx->itemArray[VOLT_WRITE_SM_ITEM_NEW_LINE]),
      &(writeCtx->itemArray[VOLT_WRITE_SM_ITEM_NEW_LINE]));
    if (status != 0)
      break;

    /* Place the BEGIN MESSAGE line in the HEADER Item because the ZDM
     * header material will go into the BEGIN_MSG Item.
     */
    temp.data = beginMsg;
    temp.len = Z2Strlen (beginMsg);
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VoltCopyItemDataAlloc (
      obj->voltObject.libraryCtx, 0, 0, &temp,
      &(writeCtx->itemArray[VOLT_WRITE_SM_ITEM_HEADER]));
    if (status != 0)
      break;

    temp.data = beginBlk;
    temp.len = Z2Strlen (beginBlk);
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VoltCopyItemDataAlloc (
      obj->voltObject.libraryCtx, 0, 0, &temp,
      &(writeCtx->itemArray[VOLT_WRITE_SM_ITEM_BEGIN_BLK]));
    if (status != 0)
      break;

    temp.data = endBlk;
    temp.len = Z2Strlen (endBlk);
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VoltCopyItemDataAlloc (
      obj->voltObject.libraryCtx, 0, 0, &temp,
      &(writeCtx->itemArray[VOLT_WRITE_SM_ITEM_END_BLK]));
    if (status != 0)

⌨️ 快捷键说明

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