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

📄 sfwritetype.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"
#include "stringutil.h"

int VtSecureFileImplWrite (
   VtSecureFileObject *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[VOLT_SECURE_MAIL_NEW_LINE_DEFAULT_LEN] =
    VOLT_SECURE_MAIL_NEW_LINE_DEFAULT;
  char *header = VOLT_SECURE_FILE_BEGIN_MSG;

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

    /* Make sure the object is empty.
     */
    status = VT_ERROR_INVALID_SECURE_FILE_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 = VOLT_SECURE_MAIL_NEW_LINE_DEFAULT_LEN;
    status = VoltCopyItemDataAlloc (
      obj->voltObject.libraryCtx, 0, 0, &temp,
      &(writeCtx->itemArray[VOLT_WRITE_SM_ITEM_NEW_LINE]));
    if (status != 0)
      break;

    /* There's only one header in a SecureFile. If it is a file (not an
     * attachment), we'll need it. If it is an attachment, we won't.
     * Put it in just in case.
     */
    status = VT_ERROR_MEMORY;
    writeCtx->itemArray[VOLT_WRITE_SM_ITEM_BEGIN_MSG].data =
      (unsigned char *)Z2Malloc (
      VOLT_SECURE_FILE_BEGIN_MSG_LEN + VOLT_SECURE_MAIL_NEW_LINE_DEFAULT_LEN,
      VOLT_MEMORY_SENSITIVE);
    if (writeCtx->itemArray[VOLT_WRITE_SM_ITEM_BEGIN_MSG].data ==
      (unsigned char *)0)
      break;
    Z2Memcpy (
      writeCtx->itemArray[VOLT_WRITE_SM_ITEM_BEGIN_MSG].data, header,
      VOLT_SECURE_FILE_BEGIN_MSG_LEN);
    writeCtx->itemArray[VOLT_WRITE_SM_ITEM_BEGIN_MSG].len =
      VOLT_SECURE_FILE_BEGIN_MSG_LEN;

    obj->formatType = VOLT_MESSAGE_FORMAT_SECURE_FILE;
    obj->state = VOLT_SECURE_MAIL_STATE_WRITE_SET;
    obj->WriteInit = VoltSecureFileWriteInit;
    obj->WriteUpdate = VoltSecureMailWriteUpdate;
    obj->WriteFinal = VoltSecureMailWriteFinal;
    obj->GetEncodeDecodeSize = VoltCopyGetEncodeDecodeSize;
    obj->localCtx = (Pointer)buffer;
    obj->LocalCtxDestroy = VoltWriteSecureMailCtxDestroy;

    status = 0;

  } 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);
}

int VtSecureFileParamDataLen (
   VtSecureFileObject secureFileObj,
   Pointer info,
   unsigned int flag
   )
{
  int status;
  unsigned int *dataLen;
  VoltSecureMailObject *obj = (VoltSecureMailObject *)secureFileObj;

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

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

    /* The SecureMail object must not be set with dataLen yet.
     */
    status = VT_ERROR_INVALID_SECURE_FILE_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 VtSecureFileParamFileName (
   VtSecureFileObject secureFileObj,
   Pointer info,
   unsigned int flag
   )
{
  int status;
  unsigned int fileNameLen, labelLen, index;
  VoltSecureMailObject *obj = (VoltSecureMailObject *)secureFileObj;
  VoltLibCtx *libCtx = (VoltLibCtx *)(obj->voltObject.libraryCtx);
  VoltSecureMailWriteCtx *writeCtx = (VoltSecureMailWriteCtx *)0;
  VoltSecureMailReadCtx *readCtx = (VoltSecureMailReadCtx *)0;
  Pointer *getInfo;
  VtUtf8StringList *descriptors;
  char *found;
  char *labelName = "file-name=";

  if ((obj->state & VOLT_SECURE_MAIL_STATE_WRITE) != 0)
    writeCtx = (VoltSecureMailWriteCtx *)(obj->localCtx);
  else
    readCtx = (VoltSecureMailReadCtx *)(obj->localCtx);

  do
  {
    if (flag == VOLT_SECURE_FILE_GET_TYPE_FLAG)
    {
      getInfo = (Pointer *)info;

      /* If this is an object to write, the file name is in the
       * writeCtx->fileName.
       */
      status = VT_ERROR_GET_INFO_UNAVAILABLE;
      if (writeCtx != (VoltSecureMailWriteCtx *)0)
      {
        if (writeCtx->fileName.data == (unsigned char *)0)
          break;
        *getInfo = (Pointer)(writeCtx->fileName.data);
        status = 0;
        break;
      }

      /* This is a read object.
       */
      status = VtGetSecureFileParam (
        secureFileObj, VtSecureFileParamContentDescriptors,
        (Pointer *)&descriptors);
      if (status != 0)
        break;

      /* Search through the list, find the string that begins
       * "file-name=".
       */
      labelLen = 10;
      found = (char *)0;
      for (index = 0; index < descriptors->count; ++index)
      {
        found = Strstr (
          descriptors->utf8Strings[index], labelName, (VtLibCtx)libCtx);
        if (found != (char *)0)
          break;
      }

      /* If we went through the list and did not find file-name, the
       * info is unavailable.
       */
      status = VT_ERROR_GET_INFO_UNAVAILABLE;
      if (found == (char *)0)
        break;

      /* Find the semicolon.
       */
      labelName = found + labelLen;
      labelLen = Z2Strlen (labelName);
      found = Strchr (labelName, ';', (VtLibCtx)libCtx);
      if (found != (char *)0)
        labelLen = (unsigned int)found - (unsigned int)labelName;

      /* Store the name in fileName.
       */
      status = VT_ERROR_MEMORY;
      readCtx->fileName.data = (unsigned char *)Z2Realloc (
        readCtx->fileName.data, labelLen + 1);
      if (readCtx->fileName.data == (unsigned char *)0)
        break;

      Z2Memcpy (readCtx->fileName.data, labelName, labelLen);
      readCtx->fileName.data[labelLen] = 0;
      *getInfo = (Pointer)(readCtx->fileName.data);
      status = 0;
      break;
    }

    status = VT_ERROR_INVALID_TYPE;
    if (flag != VOLT_SECURE_FILE_SET_TYPE_FLAG)
      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;

    writeCtx = (VoltSecureMailWriteCtx *)(obj->localCtx);

    /* Make sure the ctx does not yet have the file name set.
     */
    status = VT_ERROR_INVALID_SECURE_FILE_OBJ;
    if (writeCtx->fileName.data != (unsigned char *)0)
      break;

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

    fileNameLen = Z2Strlen (info);

    /* Build a buffer with the file name. Add a NULL-terminating
     * character.
     */
    status = VT_ERROR_MEMORY;
    writeCtx->fileName.data = (unsigned char *)Z2Malloc (fileNameLen + 1, 0);
    if (writeCtx->fileName.data == (unsigned char *)0)
      break;
    Z2Memcpy (writeCtx->fileName.data, info, fileNameLen);
    writeCtx->fileName.data[fileNameLen] = 0;
    writeCtx->fileName.len = fileNameLen;

    status = 0;

  } while (0);

  return (status);
}

int VtSecureFileParamSenderId (
   VtSecureFileObject secureFileObj,
   Pointer info,
   unsigned int flag
   )
{
  int status;
  VoltSecureMailObject *obj = (VoltSecureMailObject *)secureFileObj;
  VoltSecureMailWriteCtx *writeCtx = (VoltSecureMailWriteCtx *)(obj->localCtx);

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

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

    /* The SecureMail object must be set to write.
     */
    status = VT_ERROR_INVALID_SECURE_FILE_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 VtSecureFileParamSenderInfo (
   VtSecureFileObject secureFileObj,
   Pointer info,
   unsigned int flag
   )
{
  int status;
  VoltSecureMailObject *obj = (VoltSecureMailObject *)secureFileObj;
  VoltSecureMailWriteCtx *writeCtx = (VoltSecureMailWriteCtx *)(obj->localCtx);

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

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

    /* The SecureMail object must be set to write.
     */
    status = VT_ERROR_INVALID_SECURE_FILE_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 VtSecureFileParam3DESCBC (
   VtSecureFileObject secureFileObj,
   Pointer info,
   unsigned int flag
   )
{
  int status;
  VoltSecureMailObject *obj = (VoltSecureMailObject *)secureFileObj;

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

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

    /* The SecureMail object must be set to write.
     */
    status = VT_ERROR_INVALID_SECURE_FILE_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 VtSecureFileParamAES128CBC (
   VtSecureFileObject secureFileObj,
   Pointer info,
   unsigned int flag
   )
{
  int status;
  VoltSecureMailObject *obj = (VoltSecureMailObject *)secureFileObj;

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

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

    /* The SecureMail object must be set to write.
     */
    status = VT_ERROR_INVALID_SECURE_FILE_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 VtSecureFileParamNewLineCharacter (
   VtSecureFileObject secureFileObj,
   Pointer info,
   unsigned int flag
   )
{
  int status;
  VoltSecureMailObject *obj = (VoltSecureMailObject *)secureFileObj;
  VoltSecureMailWriteCtx *writeCtx = (VoltSecureMailWriteCtx *)(obj->localCtx);
  unsigned int newLineFlag;

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

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

    /* The SecureMail object must be set to write.
     */
    status = VT_ERROR_INVALID_SECURE_FILE_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 + -