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

📄 smwritetype.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 "p7obj.h"

int VtSecureMailImplWrite (
   VtSecureMailObject *object,
   Pointer info,
   unsigned int flag
   )
{
  int status;
  unsigned int bufferSize;
  VoltSecureMailObject *obj = (VoltSecureMailObject *)(*object);
  VoltLibCtx *libCtx = (VoltLibCtx *)(obj->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;

  do
  {
    /* Make sure this is being called appropriately.
     */
    status = VT_ERROR_INVALID_TYPE;
    if (flag != VOLT_SECURE_MAIL_SET_TYPE_FLAG)
      break;

    /* Make sure the object is empty.
     */
    status = VT_ERROR_INVALID_SECURE_MAIL_OBJ;
    if ( (obj->state != 0) || (obj->localCtx != (Pointer)0) )
      break;

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

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

    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.
     */
    status = VtCreatePkcs7Object (
      (VtLibCtx)libCtx, VtPkcs7ImplWriteSignedDSA, (Pointer)0,
      &(obj->p7SignedData));
    if (status != 0)
      break;

    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);
    status = VoltCopyItemDataAlloc (
      obj->voltObject.libraryCtx, 0, 0, &temp,
      &(writeCtx->itemArray[VOLT_WRITE_SM_ITEM_NEW_LINE]));
    if (status != 0)
      break;

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

    temp.data = beginBlk;
    temp.len = Z2Strlen (beginBlk);
    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);
    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 = Z2Strlen (endMsg);
    status = VoltCopyItemDataAlloc (
      obj->voltObject.libraryCtx, 0, 0, &temp,
      &(writeCtx->itemArray[VOLT_WRITE_SM_ITEM_END_MSG]));
    if (status != 0)
      break;

    obj->formatType = VOLT_MESSAGE_FORMAT_SECURE_MAIL;
    obj->state = VOLT_SECURE_MAIL_STATE_WRITE_SET;
    obj->WriteInit = VoltSecureMailWriteInit;
    obj->WriteUpdate = VoltSecureMailWriteUpdate;
    obj->WriteFinal = VoltSecureMailWriteFinal;
    obj->GetEncodeDecodeSize = VoltBase64GetEncodeDecodeSize;
    obj->localCtx = (Pointer)buffer;
    obj->LocalCtxDestroy = VoltWriteSecureMailCtxDestroy;

  } while (0);

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

  /* If error, free what we allocated.
   */
  VoltWriteSecureMailCtxDestroy ((Pointer)obj, (Pointer)writeCtx);

  return (status);
}

void VoltWriteSecureMailCtxDestroy (
   Pointer obj,
   Pointer ctx
   )
{
  unsigned int index;
  VoltObject *voltObj = (VoltObject *)obj;
  VoltLibCtx *libCtx;
  VoltSecureMailWriteCtx *writeCtx;

  /* Anything to destroy?
   */
  if ( (obj == (Pointer)0) || (ctx == (Pointer)0) )
    return;

  libCtx = (VoltLibCtx *)(voltObj->libraryCtx);

  writeCtx = (VoltSecureMailWriteCtx *)ctx;

  /* Free memory in all the VtItem's.
   */
  for (index = 0; index < VOLT_WRITE_SM_ITEM_COUNT; ++index)
  {
    if (writeCtx->itemArray[index].data != (unsigned char *)0)
      Z2Free (writeCtx->itemArray[index].data);
  }

  if (writeCtx->fileName.data != (unsigned char *)0)
    Z2Free (writeCtx->fileName.data);

  Z2Free(writeCtx->contentType);
  Z2Free(writeCtx->characterSet);
  Z2Free(writeCtx->originalCharacterSet);
  Z2Free(writeCtx->templateCharset);
  
  Z2Free (ctx);
}

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

  do
  {
    /* This Param cannot get info.
     */
    status = VT_ERROR_INVALID_GET;
    if (flag == VOLT_SECURE_MAIL_GET_TYPE_FLAG)
      break;

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

    /* The SecureMail object must not be set with dataLen yet.
     */
    status = VT_ERROR_INVALID_SECURE_MAIL_OBJ;
    if (obj->dataLen != 0)
      break;

    /* The state must be VOLT_SECURE_MAIL_STATE_WRITE_SET.
     */
    status = VT_ERROR_INVALID_CALL_ORDER;
    if (obj->state != VOLT_SECURE_MAIL_STATE_WRITE_SET)
      break;

    /* The info should be an unsigned int.
     */
    status = VT_ERROR_INVALID_ASSOCIATED_INFO;
    if (info == (Pointer)0)
      break;

    dataLen = (unsigned int *)info;

    obj->dataLen = *dataLen;

    status = 0;

  } while (0);

  return (status);
}

int VtSecureMailParamSenderId (
   VtSecureMailObject secureMailObj,
   Pointer info,
   unsigned int flag
   )
{
  int status;
  VoltSecureMailObject *obj = (VoltSecureMailObject *)secureMailObj;
  VoltSecureMailWriteCtx *writeCtx = (VoltSecureMailWriteCtx *)(obj->localCtx);

  do
  {
    /* This Param cannot get info.
     */
    status = VT_ERROR_INVALID_GET;
    if (flag == VOLT_SECURE_MAIL_GET_TYPE_FLAG)
      break;

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

    /* 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;

    /* There can be only one sender.
     */
    if (writeCtx->senderFlag != 0)
      break;

    /* Send this identity object on to the p7Signer. This will check
     * the validity of the associatedInfo.
     */
    status = VtSetPkcs7Param (obj->p7SignedData, VtPkcs7ParamSignerId, info);
    if (status != 0)
      break;

    writeCtx->senderFlag = 1;

    /* Get a reference to the sender ID.
     */
    status = VoltGetSignerIdObjRef (
      obj->p7SignedData, 0, (VtIdentityObject *)&(writeCtx->senderIdRef));
    if (status == VT_ERROR_NO_ID_AT_INDEX)
      status = VT_ERROR_INVALID_ASSOCIATED_INFO;

  } while (0);

  return (status);
}

int VtSecureMailParamSenderInfo (
   VtSecureMailObject secureMailObj,
   Pointer info,
   unsigned int flag
   )
{
  int status;
  VoltSecureMailObject *obj = (VoltSecureMailObject *)secureMailObj;
  VoltSecureMailWriteCtx *writeCtx = (VoltSecureMailWriteCtx *)(obj->localCtx);

  do
  {
    /* This Param cannot get info.
     */
    status = VT_ERROR_INVALID_GET;
    if (flag == VOLT_SECURE_MAIL_GET_TYPE_FLAG)
      break;

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

    /* 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;

    /* There can be only one sender.
     */
    if (writeCtx->senderFlag != 0)
      break;

    /* Send this sender info on to the p7Signer. This will check
     * the validity of the associatedInfo.
     */
    status = VtSetPkcs7Param (obj->p7SignedData, VtPkcs7ParamSignerInfo, info);
    if (status != 0)
      break;

    writeCtx->senderFlag = 1;

    /* Get a reference to the signer's ID object, if there is one,
     */
    status = VoltGetSignerIdObjRef (
      obj->p7SignedData, 0, (VtIdentityObject *)&(writeCtx->senderIdRef));
    if (status == VT_ERROR_NO_ID_AT_INDEX)
      status = 0;

  } while (0);

  return (status);
}

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

  do
  {
    /* This Param cannot get info.
     */
    status = VT_ERROR_INVALID_GET;
    if (flag == VOLT_SECURE_MAIL_GET_TYPE_FLAG)
      break;

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

    /* 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;

    /* Set the enveloper with Triple-DES.
     */
    status = VtSetPkcs7Param (
      obj->p7EnvelopedData, VtPkcs7ParamEnv3DESCBC, info);

  } while (0);

  return (status);
}

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

  do
  {
    /* This Param cannot get info.
     */
    status = VT_ERROR_INVALID_GET;
    if (flag == VOLT_SECURE_MAIL_GET_TYPE_FLAG)
      break;

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

    /* 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;

    /* Set the enveloper with AES.
     */
    status = VtSetPkcs7Param (
      obj->p7EnvelopedData, VtPkcs7ParamEnvAES128CBC, info);

  } while (0);

  return (status);
}

int VtSecureMailParamNewLineCharacter (
   VtSecureMailObject secureMailObj,
   Pointer info,
   unsigned int flag
   )
{
  int status;
  VoltSecureMailObject *obj = (VoltSecureMailObject *)secureMailObj;
  VoltSecureMailWriteCtx *writeCtx = (VoltSecureMailWriteCtx *)(obj->localCtx);
  unsigned int newLineFlag;

  do
  {
    /* This Param cannot get info.
     */
    status = VT_ERROR_INVALID_GET;
    if (flag == VOLT_SECURE_MAIL_GET_TYPE_FLAG)
      break;

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

    /* 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 associated info is a pointer to an unsigned int.
     */
    status = VT_ERROR_INVALID_ASSOCIATED_INFO;
    if (info == (Pointer)0)
      break;

    newLineFlag = *((unsigned int *)info);
    if ( (newLineFlag != VT_SECURE_MAIL_NEW_LINE_LF) &&
         (newLineFlag != VT_SECURE_MAIL_NEW_LINE_CR_LF) )
      break;

    /* We initialized the buffer holding the new line chars to be
     * CR_LF. So there's enough space in there to hold whatever is
     * requested.
     */
    status = 0;
    if (newLineFlag == VT_SECURE_MAIL_NEW_LINE_CR_LF)
    {
      writeCtx->itemArray[VOLT_WRITE_SM_ITEM_NEW_LINE].data[0] = 0x0d;
      writeCtx->itemArray[VOLT_WRITE_SM_ITEM_NEW_LINE].data[1] = 0x0a;
      writeCtx->itemArray[VOLT_WRITE_SM_ITEM_NEW_LINE].len = 2;
      break;
    }

    /* The caller wants LF.
     */
    writeCtx->itemArray[VOLT_WRITE_SM_ITEM_NEW_LINE].data[0] = 0x0a;
    writeCtx->itemArray[VOLT_WRITE_SM_ITEM_NEW_LINE].len = 1;

  } while (0);

  return (status);
}

⌨️ 快捷键说明

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