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

📄 sm2write.c

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

#define VOLT_SECURE_MAIL_BASE_64_LINE_LENGTH     64

static int VoltSecureMail2InitMessageAttributes(
  VtSecureMailObject secureMailObj,
  VtDataNodeObject attributesNode,
  const unsigned char* contentType,
  const unsigned char* characterSet,
  const unsigned char* originalCharacterSet
)
{
  int status;
  VtLibCtx libCtx;
  VoltSecureMailWriteCtx* writeCtx;
  VtDataNodeObject node;
  unsigned char version[64];
  VOLT_DECLARE_FNCT_LINE(fnctLine)
  
  VT_ASSERT(secureMailObj != (VtSecureMailObject)0);
  VT_ASSERT(attributesNode != (VtDataNodeObject)0);
  
  libCtx = secureMailObj->voltObject.libraryCtx;
  writeCtx = (VoltSecureMailWriteCtx*) secureMailObj->localCtx;
  
  do
  {
    status = VoltNumToDecimalString(secureMailObj->version,
      version, sizeof(version), libCtx);
    VT_ASSERT(status == 0);

    /* Set the current version */
    VOLT_SET_FNCT_LINE(fnctLine)
    status = VtDataNodeResolveChild(attributesNode,
      VoltZDM2VersionAttributeName, 1, &node);
    if (status != 0)
      break;
    
    VOLT_SET_FNCT_LINE(fnctLine)
    status = VtDataNodeSetStringValue(node, version);
    if (status != 0)
      break;
      
    /* Set the compatible version */
    VOLT_SET_FNCT_LINE(fnctLine)
    status = VtDataNodeResolveChild(attributesNode,
      VoltZDM2CompatibleVersionAttributeName, 1, &node);
    if (status != 0)
      break;
    
    VOLT_SET_FNCT_LINE(fnctLine)
    status = VtDataNodeSetStringValue(node, version);
    if (status != 0)
      break;
    
    /* Set the recipients node. We don't actually populate it with
     * any recipients, because they really should only be used for ZDM
     * messages, but the existing Voltage core code expects it to be
     * there, even for non-ZDM messages, so we need to add an empty
     * element so we're compatible
     */
    VOLT_SET_FNCT_LINE(fnctLine)
    status = VtDataNodeResolveChild(attributesNode,
      VoltZDM2RecipientsAttributeName, 1, &node);
    if (status != 0)
      break;

    if (contentType == (unsigned char*)0)
      contentType = VoltZDM2DefaultContentType;
      
    VOLT_SET_FNCT_LINE(fnctLine)
    status = VtDataNodeResolveChild(attributesNode,
      VoltZDM2ContentTypeAttributeName, 1, &node);
    if (status != 0)
      break;
    
    VOLT_SET_FNCT_LINE(fnctLine)
    status = VtDataNodeSetStringValue(node, contentType);
    if (status != 0)
      break;
    
    if (characterSet == (unsigned char*)0)
      characterSet = VoltZDM2DefaultCharacterSet;
      
    VOLT_SET_FNCT_LINE(fnctLine)
    status = VtDataNodeResolveChild(attributesNode,
      VoltZDM2CharacterSetAttributeName, 1, &node);
    if (status != 0)
      break;
    
    VOLT_SET_FNCT_LINE(fnctLine)
    status = VtDataNodeSetStringValue(node, characterSet);
    if (status != 0)
      break;
    
    if (originalCharacterSet == (unsigned char*)0)
      originalCharacterSet = VoltZDM2DefaultCharacterSet;
      
    VOLT_SET_FNCT_LINE(fnctLine)
    status = VtDataNodeResolveChild(attributesNode,
      VoltZDM2OriginalCharacterSetAttributeName, 1, &node);
    if (status != 0)
      break;
    
    VOLT_SET_FNCT_LINE(fnctLine)
    status = VtDataNodeSetStringValue(node, originalCharacterSet);
    if (status != 0)
      break;
  }
  while (0);

  VOLT_LOG_ERROR_COMPARE(status, libCtx, status, 0,
    fnctLine, "VoltSecureMail2InitMessageAttributes", (unsigned char*)0)
  
  return status;
}

int VoltSecureMail2InitTemplateVariables(
  VtSecureMailObject secureMailObj,
  VtDataNodeObject attributesNode,
  const unsigned char* messageHeader,
  const unsigned char* messageStartTag,
  const unsigned char* messageEndTag,
  const unsigned char* charset
)
{
  int status;
  VtLibCtx libCtx;
  VtDataNodeObject node;
  VOLT_DECLARE_FNCT_LINE(fnctLine)
  
  VT_ASSERT(secureMailObj != (VtSecureMailObject)0);
  VT_ASSERT(attributesNode != (VtDataNodeObject)0);
  
  libCtx = secureMailObj->voltObject.libraryCtx;
  
  do
  {
    /* Set the message header */
    VOLT_SET_FNCT_LINE(fnctLine)
    status = VtDataNodeResolveChild(attributesNode,
      VoltZDM2HeaderVariableName, 1, &node);
    if (status != 0)
      break;
    
    VOLT_SET_FNCT_LINE(fnctLine)
    status = VtDataNodeSetStringValue(node, messageHeader);
    if (status != 0)
      break;
      
    /* Set the message start tag */
    VOLT_SET_FNCT_LINE(fnctLine)
    status = VtDataNodeResolveChild(attributesNode,
      VoltZDM2MessageStartTagVariableName, 1, &node);
    if (status != 0)
      break;
    
    VOLT_SET_FNCT_LINE(fnctLine)
    status = VtDataNodeSetStringValue(node, messageStartTag);
    if (status != 0)
      break;

    /* Set the message end tag */
    VOLT_SET_FNCT_LINE(fnctLine)
    status = VtDataNodeResolveChild(attributesNode,
      VoltZDM2MessageEndTagVariableName, 1, &node);
    if (status != 0)
      break;
    
    VOLT_SET_FNCT_LINE(fnctLine)
    status = VtDataNodeSetStringValue(node, messageEndTag);
    if (status != 0)
      break;
      
    /* Set the charset */
    VOLT_SET_FNCT_LINE(fnctLine)
    status = VtDataNodeResolveChild(attributesNode,
      VoltZDM2CharSetVariableName, 1, &node);
    if (status != 0)
      break;
    
    if (charset == (const unsigned char*)0)
      charset = VoltZDM2DefaultCharacterSet;
      
    VOLT_SET_FNCT_LINE(fnctLine)
    status = VtDataNodeSetStringValue(node, charset);
    if (status != 0)
      break;    
  }
  while (0);

  VOLT_LOG_ERROR_COMPARE(status, libCtx, status, 0,
    fnctLine, "VoltSecureMail2InitTemplateVariables", (unsigned char*)0)
  
  return status;
}

static int VoltSecureMail2ExpandTemplate(
  VtLibCtx libCtx,
  unsigned char* templateText,
  unsigned int templateLength,
  VtDataNodeObject templateVariables,
  VtItem* item
)
{
  int status;
  VtStreamObject outputStream = (VtStreamObject)0;
  VtStreamSize outputSize, readSize;
  VOLT_DECLARE_FNCT_LINE(fnctLine)
  VOLT_DECLARE_ERROR_TYPE(errorType)
  
  VOLT_SET_ERROR_TYPE(errorType, 0)

  do
  {
    VOLT_SET_FNCT_LINE(fnctLine)
    status = VoltZDM2ExpandTemplate(libCtx, templateText,
      templateLength, templateVariables, &outputStream);
    if (status != 0)
      break;

    VOLT_SET_FNCT_LINE(fnctLine)
    status = VtStreamGetSize(outputStream, &outputSize);
    if (status != 0)
      break;
    
    VOLT_SET_FNCT_LINE(fnctLine)
    status = VtStreamSetPosition(outputStream, 0, VT_STREAM_FROM_START);
    if (status != 0)
      break;
    
    item->data = (unsigned char*) Z3Malloc(outputSize);
    item->len = (unsigned int) outputSize;
    
    VOLT_SET_FNCT_LINE(fnctLine)
    status = VtStreamRead(outputStream, item->data, item->len, &readSize);
    if (item->len != (unsigned int)readSize)
    {
      VOLT_SET_FNCT_LINE(fnctLine)
      VOLT_SET_ERROR_TYPE(errorType, VT_ERROR_TYPE_PRIMARY)
      status = VT_ERROR_STREAM_READ;
    }
    if (status != 0)
      break;
  }
  while (0);

  VtDestroyStreamObject(&outputStream);
  
  VOLT_LOG_ERROR_COMPARE(status, libCtx, status, errorType,
    fnctLine, "VoltSecureMail2ExpandTemplate", (unsigned char*)0)
  
  return status;
}

#if 0
int VoltSecureMail2EncloseInHTMLComment(
  VtLibCtx libCtx,
  unsigned char* str,
  unsigned char** commentedStr
)
{
  int status = 0;
  unsigned char commentStart[] = "<!--";
  unsigned char commentEnd[] = "-->";
  unsigned int commentStartLength = sizeof(commentStart) - 1;
  unsigned int commentEndLength = sizeof(commentEnd) - 1;
  unsigned int strLength;
  unsigned char* p;
  VOLT_DECLARE_FNCT_LINE(fnctLine)

  VT_ASSERT(str != (unsigned char*)0);
  VT_ASSERT(commentedStr != (unsigned char**)0);
  
  do
  {
    strLength = Z2Strlen(str);
    
    p = (unsigned char*)
      Z3Malloc(commentStartLength + strLength + commentEndLength + 1);
    if (p == (unsigned char*)0)
    {
      VOLT_SET_FNCT_LINE(fnctLine)
      status = VT_ERROR_MEMORY;
      break;
    }
    *commentedStr = p;
    Z2Memcpy(p, commentStart, commentStartLength);
    p += commentStartLength;
    Z2Memcpy(p, str, strLength);
    p += strLength;
    Z2Memcpy(p, commentEnd, commentEndLength);
    p += commentEndLength;
    *p = 0;
  }
  while (0);

  VOLT_LOG_ERROR_COMPARE(status, libCtx, status, VT_ERROR_TYPE_PRIMARY,
    fnctLine, "VoltSecureMail2EncloseInHTMLComment", (unsigned char*)0)
  
  return status;
}
#endif

static int VoltSecureMail2WriteInitCommon(
  VtSecureMailObject secureMailObj,
  VtPolicyCtx policyCtx,
  unsigned int version
)
{
  int status;
  unsigned int i, elementLen, newLineLen;
  VoltSecureMailWriteCtx* writeCtx = (VoltSecureMailWriteCtx*)(secureMailObj->localCtx);
  VoltLibCtx* libCtx = (VoltLibCtx*)(secureMailObj->voltObject.libraryCtx);
  VtBase64Info b64Info;
  int useHTMLWrapping;
  unsigned char* messageStartTag = (unsigned char*)0;
  unsigned char* secureBlockStartTag = (unsigned char*)0;
  unsigned char* secureBlockEndTag = (unsigned char*)0;
  unsigned char* messageEndTag = (unsigned char*)0;
  unsigned char* messageHeader = (unsigned char*)0;
  VtItem* messageHeaderItem = (VtItem*)0;
  VtItem* templateItem = (VtItem*)0;
  VtItem temp;
  VtDataNodeObject templateVariablesNode = (VtDataNodeObject)0;
  unsigned char* headerText;
  unsigned int headerLength;
  unsigned char* trailerText;
  unsigned int trailerLength;
  VOLT_DECLARE_FNCT_LINE(fnctLine)
  VOLT_DECLARE_ERROR_TYPE(errorType)
  
  VOLT_SET_ERROR_TYPE(errorType, 0)

  do

⌨️ 快捷键说明

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