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

📄 zdm2write.c

📁 IBE是一种非对称密码技术
💻 C
📖 第 1 页 / 共 5 页
字号:
/* 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 "zdm2common.h"
#include "datanode.h"
#include "template.h"
#include "compress.h"
#include "securearchive.h"
#include "sacommon.h"
#include "policy.h"
#include "stringutil.h"
#include "vtime.h"
#include "emailschema.h"
#include "errorctx.h"
#include "vtassert.h"

const char* VoltZDM2InsecureRootAttributeName        = "ZDMInsecureAttributes";
const char* VoltZDM2MessageTypeVariableName          = "MessageType";
const char* VoltZDM2MessageVersionVariableName       = "MessageVersion";
const char* VoltZDM2ZDMLocationVariableName          = "ZDMLocation";
const char* VoltZDM2DesignatedRecipientVariableName  = "DesignatedRecipient";
const char* VoltZDM2NewLineCRLF                      = "\r\n";
const char* VoltZDM2NewLineLF                        = "\n";

#define VOLT_ZDM2_OUTPUT_STAGE_NONE                   0
#define VOLT_ZDM2_OUTPUT_STAGE_HEADER                 1
#define VOLT_ZDM2_OUTPUT_STAGE_BEGIN_SECURE_DATA_TAG  2
#define VOLT_ZDM2_OUTPUT_STAGE_MESSAGE_DATA           3
#define VOLT_ZDM2_OUTPUT_STAGE_END_SECURE_DATA_TAG    4
#define VOLT_ZDM2_OUTPUT_STAGE_MESSAGE_DATA_PADDING   5
#define VOLT_ZDM2_OUTPUT_STAGE_TRAILER                6
#define VOLT_ZDM2_OUTPUT_STAGE_DONE                   7

#define VOLT_ZDM2_BASE_64_LINE_LENGTH     64

typedef struct VoltZDM2RecipientInfo
{
  unsigned char*      mAddress;
  unsigned char*      mZDMLocation;
  int                 mExternal;
} VoltZDM2RecipientInfo;

typedef struct VoltZDM2RecipientInfoList
{
  unsigned int            mCount;
  VoltZDM2RecipientInfo*  mInfoArray;
} VoltZDM2RecipientInfoList;

typedef struct VoltZDM2WriteLocalCtx
{
  VtSecureArchiveObject     mSecureArchive;
  VtStreamObject            mOutputStream;
  int                       mSupportedVersion;
  int                       mQueriedDistrictParams;
  int                       mCurrentEntryType;
  int                       mState;
  int                       mOutputStage;
  int                       mNewLineCharacter;
  const unsigned char*      mNewLineString;
  unsigned int              mOutputOffset;
  unsigned int              mMessageFormat;
  unsigned int              mPaddedSize;
  unsigned char             mPadChar;
  unsigned int              mMessageDataSize;
  unsigned int              mMaxInputFieldSize;
  unsigned int              mInputFieldIndex;
  VoltZDM2RecipientInfoList mRecipientInfoList;
  VtZDMEmailRecipientList   mPrimaryEmailRecipientList;
  VtZDMEmailRecipientList   mCCEmailRecipientList;
  VtStreamObject            mWrapperHeaderStream;
  VtStreamObject            mWrapperTrailerStream;
  unsigned char*            mBeginSecureTag;
  unsigned char*            mEndSecureTag;
  unsigned char*            mMessageID;
  unsigned char*            mDesignatedRecipient;
  unsigned char*            mZDMLocation;
  unsigned char*            mPartialLine;
  VtBufferTypeInfo          mBufferTypeInfo;
  VtDataNodeObject          mTemplateVariables;
  VtCompressObject          mMessageBodyDecompress;
  VtAlgorithmObject         mBase64Encoder;
  VtPolicyCtx               mPolicyCtx;
  VtStorageCtx              mStorageCtx;
  VtTransportCtx            mTransportCtx;
  VtRandomObject            mRandom;
} VoltZDM2WriteLocalCtx;

static void VoltZDM2EmailRecipientListDestroy(
  VtLibCtx libCtx,
  VtZDMEmailRecipientList* recipientList
)
{
  unsigned int i;
  
  for (i = 0; i < recipientList->count; i++)
  {
    Z2Free(recipientList->emailList[i]);
  }
  Z2Free(recipientList->emailList);
  
  recipientList->count = 0;
  recipientList->emailList = (const unsigned char**)0;
}

static int VoltZDM2EmailRecipientListCopy(
  VtLibCtx libCtx,
  VtZDMEmailRecipientList* srcList,
  VtZDMEmailRecipientList* destList
)
{
  int status = 0;
  int length;
  const unsigned char* s1;
  unsigned char* s2;
  unsigned int i;
  VOLT_DECLARE_FNCT_LINE(fnctLine)
  
  VT_ASSERT(libCtx != (VtLibCtx)0);
  VT_ASSERT(srcList != (VtZDMEmailRecipientList*)0);
  VT_ASSERT(destList != (VtZDMEmailRecipientList*)0);
  
  do
  {
    /* Allocate the array of string ptrs for the destination
     * list. Initialize the elements to 0 in case we fail
     * in the middle of copying over the elements.
     */
    length = srcList->count * sizeof(unsigned char*);
    destList->emailList = (const unsigned char**) Z3Malloc(length);
    if (destList->emailList == (const unsigned char**) 0)
    {
      VOLT_SET_FNCT_LINE(fnctLine)
      status = VT_ERROR_MEMORY;
      break;
    }
    Z2Memset(destList->emailList, 0, length);
    
    /* Copy over the strings */
    for (i = 0; i < srcList->count; i++)
    {
      s1 = srcList->emailList[i];
      length = Z2Strlen(s1) + 1;
      s2 = (unsigned char*)Z3Malloc(length);
      if (s2 == (unsigned char*)0)
      {
        VOLT_SET_FNCT_LINE(fnctLine)
        status = VT_ERROR_MEMORY;
        break;
      }
      Z2Memcpy(s2, s1, length);
      destList->emailList[i] = s2;
    }
    
    destList->count = srcList->count;
  }
  while (0);
  
  if (status != 0)
  {
    VoltZDM2EmailRecipientListDestroy(libCtx, destList);
    
    VOLT_LOG_ERROR(libCtx, status, 0, fnctLine,
      "VoltZDMEmalRecipientListCopy", (unsigned char*)0)
  }
  
  return status;
}

static int VoltZDM2EmailRecipientListContains(
  VtLibCtx libCtx,
  const VtZDMEmailRecipientList* list,
  const unsigned char* address
)
{
  unsigned int i;
  int found = 0;
  
  VT_ASSERT(libCtx != 0);
  VT_ASSERT(list != (VtZDMEmailRecipientList*)0);
  VT_ASSERT(address != (const unsigned char*)0);
  
  for (i = 0; i < list->count; i++)
  {
    if (Strcmp((char*)list->emailList[i], (char*)address, libCtx) == 0)
    {
      found = 1;
      break;
    }
  }
  
  return found;
}

static void VoltZDM2RecipientInfoListDestroy(
  VtLibCtx libCtx,
  VoltZDM2RecipientInfoList* recipientInfoList
)
{
  unsigned int i;
  
  for (i = 0; i < recipientInfoList->mCount; i++)
  {
    Z2Free(recipientInfoList->mInfoArray[i].mAddress);
    Z2Free(recipientInfoList->mInfoArray[i].mZDMLocation);
  }
  
  Z2Free(recipientInfoList->mInfoArray);
  
  recipientInfoList->mCount = 0;
  recipientInfoList->mInfoArray = (VoltZDM2RecipientInfo*)0;
}

static int VoltZDM2GenerateMessageID(
  VtLibCtx libCtx,
  VtRandomObject random,
  unsigned char** messageID
)
{
  int status = 0;
  const char* domain = "@voltage.com";
  int domainLength;
  unsigned char randomBytes[16];
  int idLength;
  int i;
  int highDigit, lowDigit;
  unsigned char* p;
  static unsigned char hexChars[] = {
    '0', '1', '2', '3', '4', '5', '6', '7',
    '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
  };
  VOLT_DECLARE_FNCT_LINE(fnctLine)
  
  do
  {
    /* Generate the random bytes for the message ID */
    VOLT_SET_FNCT_LINE(fnctLine)
    status = VtGenerateRandomBytes(random, randomBytes, sizeof(randomBytes));
	  if (status != 0)
	    break;
	  
	  /* Allocate the buffer for the message ID */
	  domainLength = Z2Strlen(domain);
	  idLength = domainLength + (2 * sizeof(randomBytes));
	  *messageID = (unsigned char*) Z3Malloc(idLength + 1);
	  if (*messageID == (unsigned char*)0)
	  {
      VOLT_SET_FNCT_LINE(fnctLine)
	    status = VT_ERROR_MEMORY;
	    break;
	  }
	  
	  /* Convert the random bytes to the corresponding hex string
	  * and append the domain.
	  */
	  for (i = 0, p = *messageID; i < sizeof(randomBytes); i++)
	  {
	    highDigit = randomBytes[i] / 16;
	    lowDigit = randomBytes[i] % 16;
	    *p++ = hexChars[highDigit];
	    *p++ = hexChars[lowDigit];
	  }
	  Z2Memcpy(p, domain, domainLength + 1);
	}
	while (0);
	
	if (status != 0)
	{
	  Z2Free(*messageID);
    
    VOLT_LOG_ERROR(libCtx, status, VT_ERROR_TYPE_PRIMARY, fnctLine,
      "VoltZDM2GenerateMessageID", (unsigned char*)0)
	}
	
	return status;
}

static int VoltZDM2ProcessWrapperTemplate(
  VtZDMObject zdmObj
)
{
  int status = 0;
  VoltZDM2WriteLocalCtx* localCtx;
  VtLibCtx libCtx = (VtLibCtx)0;
  VtItem* templateItem = (VtItem*)0;
  unsigned char* headerEnd;
  unsigned char* trailerStart;
  unsigned char* trailerEnd;
  unsigned int i, j;
  int match = 0;
  char* messageDataName = "${MessageData}";
  unsigned int messageDataNameLength;
  VOLT_DECLARE_FNCT_LINE(fnctLine)
  VOLT_DECLARE_ERROR_TYPE(errorType)
  
  libCtx = zdmObj->voltObject.libraryCtx;
  VT_ASSERT(libCtx != (VtLibCtx)0);
  localCtx = (VoltZDM2WriteLocalCtx*) zdmObj->localCtx;
  VT_ASSERT(localCtx != (VoltZDM2WriteLocalCtx*)0);
  
  VOLT_SET_ERROR_TYPE(errorType, 0)

  do
  {
    VT_ASSERT((localCtx->mMessageFormat & VT_MESSAGE_FORMAT_HTML_WRAPPED) != 0);

    if (localCtx->mPolicyCtx == (VtPolicyCtx)0)
    {
      VOLT_SET_ERROR_TYPE(errorType, VT_ERROR_TYPE_PRIMARY)
      VOLT_SET_FNCT_LINE(fnctLine)
      status = VT_ERROR_NO_POLICY_CTX;
      break;
    }
    
    if ((localCtx->mMessageFormat & VT_MESSAGE_FORMAT_ZDM) != 0)
    {
      VOLT_SET_FNCT_LINE(fnctLine)
      status = localCtx->mPolicyCtx->PolicyGetInfoAlloc (
        localCtx->mPolicyCtx, VOLT_POLICY_GET_ZDM2_TEMPLATE,
        (Pointer)0, (Pointer *)&templateItem);
      if (status != 0)
        break;
      if (templateItem == (VtItem*)0)
      {
        VOLT_SET_ERROR_TYPE(errorType, VT_ERROR_TYPE_PRIMARY)
        VOLT_SET_FNCT_LINE(fnctLine)
        status = VT_ERROR_INVALID_ZDM_TEMPLATE;
        break;
      }
    }
    else
    {
      VOLT_SET_ERROR_TYPE(errorType, VT_ERROR_TYPE_PRIMARY)
      VOLT_SET_FNCT_LINE(fnctLine)
      status = VT_ERROR_INVALID_MESSAGE_FORMAT;
      break;
    }
    
    messageDataNameLength = Z2Strlen(messageDataName);
    
    for (i = 0; i < templateItem->len - messageDataNameLength; i++)
    {
      match = 1;
      for (j = 0; j < messageDataNameLength; j++)
      {
        if (Z2Tolower(templateItem->data[i + j]) !=
          Z2Tolower(messageDataName[j]))
        {
          match = 0;
          break;
        }
      }
      if (match)
        break;
    }
    
    if (!match)
    {
      VOLT_SET_ERROR_TYPE(errorType, VT_ERROR_TYPE_PRIMARY)
      VOLT_SET_FNCT_LINE(fnctLine)
      status = VT_ERROR_NO_MESSAGE_DATA_VAR;
      break;
    }
    
    headerEnd = templateItem->data + i;
    
    trailerStart = headerEnd + Z2Strlen(messageDataName);
    trailerEnd = templateItem->data + templateItem->len;
    
    VtDestroyStreamObject(&localCtx->mWrapperHeaderStream);
    VtDestroyStreamObject(&localCtx->mWrapperTrailerStream);
    
    localCtx->mWrapperHeaderStream = (VtStreamObject)0;
    localCtx->mWrapperTrailerStream = (VtStreamObject)0;
    
    VOLT_SET_FNCT_LINE(fnctLine)
    status = VoltZDM2ExpandTemplate(libCtx, templateItem->data,
      headerEnd - templateItem->data, localCtx->mTemplateVariables,
      &localCtx->mWrapperHeaderStream);
    if (status != 0)
      break;

    VOLT_SET_FNCT_LINE(fnctLine)
    status = VoltZDM2ExpandTemplate(libCtx, trailerStart,
      trailerEnd - trailerStart, localCtx->mTemplateVariables,
      &localCtx->mWrapperTrailerStream);
    if (status != 0)
      break;      
  }
  while (0);
  
  localCtx->mPolicyCtx->PolicyGetInfoFree (localCtx->mPolicyCtx,
    (Pointer)templateItem);
  
  VOLT_LOG_ERROR_COMPARE(status, libCtx, status, errorType, fnctLine,
    "VoltZDM2ProcessWrapperTemplate", (unsigned char*)0)
  
  return status;
}

static int VoltZDM2SetEmailRecipientAttributes(
   VtZDMObject zdmObj,
   VtDataNodeObject recipientsNode,
   const unsigned char* address,
   unsigned int flags
)
{
  int status = 0;
  VoltZDM2WriteLocalCtx* localCtx;
  VtLibCtx libCtx = (VtLibCtx)0;
  VtDataNodeObject recipientNode = (VtDataNodeObject)0;
  VtDataNodeObject dataNode;
  VOLT_DECLARE_FNCT_LINE(fnctLine)

⌨️ 快捷键说明

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