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

📄 sm2read.c

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

#define VOLT_SECURE_MAIL_WRAPPER_TYPE_UNKNOWN         1
#define VOLT_SECURE_MAIL_WRAPPER_TYPE_PLAIN           2
#define VOLT_SECURE_MAIL_WRAPPER_TYPE_HTML            3
#define VOLT_SECURE_MAIL_WRAPPER_TYPE_INVALID         4

int VoltSecureMail2ReadInit (
  VtSecureMailObject secureMailObj
)
{
  int status = 0;
  VoltSecureMail2ReadCtx* readCtx = (VoltSecureMail2ReadCtx*)0;
  VtLibCtx libCtx = (VtLibCtx)0;
  VOLT_DECLARE_FNCT_LINE(fnctLine)
  VOLT_DECLARE_ERROR_TYPE(errorType)
  
  VOLT_SET_ERROR_TYPE(errorType, 0)
  
  VT_ASSERT(secureMailObj != (VtSecureMailObject)0);
  
  readCtx = (VoltSecureMail2ReadCtx*)secureMailObj->localCtx;
  VT_ASSERT(readCtx != (VoltSecureMail2ReadCtx*)0);
  libCtx = secureMailObj->voltObject.libraryCtx;
  VT_ASSERT(libCtx != (VtLibCtx)0);
  
  do
  {
    /* The state must be VOLT_SECURE_MAIL_STATE_READ_SET, if not,
     * we're not allowed to call ReadInit.
     */
    if (secureMailObj->state != VOLT_SECURE_MAIL_STATE_READ_SET)
    {
      VOLT_SET_ERROR_TYPE(errorType, VT_ERROR_TYPE_PRIMARY)
      VOLT_SET_FNCT_LINE(fnctLine)
      status = VT_ERROR_INVALID_CALL_ORDER;
      break;
    }
    
    VOLT_SET_FNCT_LINE(fnctLine)
    status = VtPkcs7ReadInit(secureMailObj->p7SignedData);
    if (status != 0)
      break;

    VOLT_SET_FNCT_LINE(fnctLine)
    status = VtPkcs7ReadInit(secureMailObj->p7EnvelopedData);
    if (status != 0)
      break;

    VOLT_SET_FNCT_LINE(fnctLine)
    status = VtDecodeInit(secureMailObj->base64);
    if (status != 0)
      break;

    readCtx->wrapperType = VOLT_SECURE_MAIL_WRAPPER_TYPE_UNKNOWN;
    secureMailObj->state = VOLT_SECURE_MAIL_STATE_READ_HTML;
  }
  while (0);
  
  VOLT_LOG_ERROR_COMPARE(status, libCtx, status, errorType,
    fnctLine, "VoltSecureMail2ReadInit", (unsigned char*)0)
  
  return status;
}

static const unsigned char* VoltSecureMail2SkipQuotedString(
  const unsigned char* p,
  const unsigned char* end,
  unsigned char quoteChar
)
{
  unsigned char c;
  
  VT_ASSERT(p != (unsigned char*)0);
  VT_ASSERT(end != (unsigned char*)0);
  
  while (p < end)
  {
    c = *p++;
    
    if (c == quoteChar)
    {
      break;
    }
    else if (p == end)
    {
      p = (unsigned char*)0;
      break;
    }
    else if (c == '\\')
    {
      p++;
    }
  }
  
  return p;
}

static const unsigned char* VoltSecureMail2SkipHTMLTag(
  const unsigned char* p,
  const unsigned char* end
)
{
  unsigned char c;
  const unsigned char* tagEnd = (unsigned char*)0;
  
  VT_ASSERT(p != (unsigned char*)0);
  VT_ASSERT(end != (unsigned char*)0);
  
  while ((p != (unsigned char*)0) && (p < end))
  {
    c = *p++;
    
    if (c == '>')
    {
      tagEnd = p;
      break;
    }
    else if ((c == '\'') || (c == '\"'))
    {
      p = VoltSecureMail2SkipQuotedString(p, end, c);
    }
  }
  
  return tagEnd;
}

static const unsigned char* VoltSecureMail2SkipWhitespace(
  const unsigned char* p,
  const unsigned char* end
)
{
  unsigned char c;
  
  VT_ASSERT(p != (unsigned char*)0);
  VT_ASSERT(end != (unsigned char*)0);
  
  while (p < end)
  {
    c = *p;
    if ((c != ' ') && (c != '\t') && (c != '\n') && (c != '\r'))
      break;
    p++;
  }
  
  return p;
}

static const unsigned char* VoltSecureMail2SkipHTMLComment(
  const unsigned char* p,
  const unsigned char* end
)
{
  VT_ASSERT(p != (unsigned char*)0);
  VT_ASSERT(end != (unsigned char*)0);
  
  for (;;)
  {
    if (p + 3 > end)
    {
      p = (unsigned char*)0;
      break;
    }
    
    if ((p[0] == '-') && (p[1] == '-') && (p[2] == '>'))
    {
      p += 3;
      break;
    }
    
    p++;
  }
  
  return p;
}

static int VoltSecureMail2MatchTagName(
  const unsigned char* p,
  const unsigned char* end,
  const unsigned char* tagName,
  VtLibCtx libCtx
)
{
  const unsigned char* startTagName;
  unsigned int tagLength;
  unsigned char c;
  int isAlnum;
  int tagMatches;
  
  VT_ASSERT(p != (unsigned char*)0);
  VT_ASSERT(end != (unsigned char*)0);
  VT_ASSERT(tagName != (unsigned char*)0);
  
  tagLength = Z2Strlen(tagName);
  
  p = VoltSecureMail2SkipWhitespace(p, end);
  startTagName = p;
  
  while (p < end)
  {
    c = *p;
    isAlnum = ((c >= 'a') && (c <= 'z')) ||
      ((c >= 'A') && (c <= 'Z')) || ((c >= '0') && (c <= '9'));
    if (!isAlnum)
      break;
    p++;
  }
  
  tagMatches = (VoltCaseInsensitiveCompareBuffers(startTagName,
    p - startTagName, tagName, tagLength, libCtx) == 0);
  
  return tagMatches;
}


static int VoltSecureMail2FindHTML(
  VtSecureMailObject secureMailObj,
  const unsigned char* input,
  unsigned int inputLength,
  unsigned int* inputRead,
  unsigned int* pendingInputLength
)
{
  int status = 0;
  VoltSecureMail2ReadCtx* readCtx = (VoltSecureMail2ReadCtx*)0;
  VtLibCtx libCtx = (VtLibCtx)0;
  const unsigned char* p;
  const unsigned char* end;
  const unsigned char* tagStart;
  int foundHTML;
  VOLT_DECLARE_FNCT_LINE(fnctLine)
  
  VT_ASSERT(secureMailObj != (VtSecureMailObject)0);
  VT_ASSERT(inputRead != (unsigned int*)0);
  VT_ASSERT(pendingInputLength != (unsigned int*)0);

  *pendingInputLength = 0;
  *inputRead = inputLength;
  
  if (inputLength == 0)
    return 0;
    
  readCtx = (VoltSecureMail2ReadCtx*)secureMailObj->localCtx;
  VT_ASSERT(readCtx != (VoltSecureMail2ReadCtx*)0);
  libCtx = secureMailObj->voltObject.libraryCtx;
  VT_ASSERT(libCtx != (VtLibCtx)0);
  
  p = input;
  end = input + inputLength;
  
  do
  {
    p = VoltSecureMail2SkipWhitespace(p, end);
    
    if (p == end)
      break;
    
    if (*p != '<')
    {
      readCtx->wrapperType = VOLT_SECURE_MAIL_WRAPPER_TYPE_PLAIN;
      secureMailObj->state = VOLT_SECURE_MAIL_STATE_READ_HEADER_1;
      break;
    }
    
    tagStart = p;
    p++;
    
    if (p == end)
    {
      p = (unsigned char*)0;
    }
    else if (*p == '?')
    {
      p = VoltSecureMail2SkipHTMLTag(p, end);
    }
    else if (*p == '!')
    {
      if ((p + 2 < end) && (p[1] == '-') && (p[2] == '-'))
      {
        p += 3;
        p = VoltSecureMail2SkipHTMLComment(p, end);
      }
      else
      {
        p = VoltSecureMail2SkipHTMLTag(p, end);
      }
    }
    else if (VoltSecureMail2MatchTagName(p, end, "meta", libCtx))
    {
      p = VoltSecureMail2SkipHTMLTag(p, end);
    }
    else
    {
      foundHTML = VoltSecureMail2MatchTagName(p, end, "html", libCtx);
      p = VoltSecureMail2SkipHTMLTag(p, end);
      if (p != (unsigned char*)0)
      {
        if (foundHTML)
        {
          secureMailObj->state = VOLT_SECURE_MAIL_STATE_READ_BODY;
        }
        else
        {
          readCtx->wrapperType = VOLT_SECURE_MAIL_WRAPPER_TYPE_INVALID;
          secureMailObj->state = VOLT_SECURE_MAIL_STATE_READ_COMPLETE;
          VOLT_SET_FNCT_LINE(fnctLine)
          status = VT_ERROR_INVALID_SECURE_MAIL_MSG;
        }
        break;
      }
    }
  }
  while (p != (unsigned char*)0);

  if (p != (unsigned char*)0)
  {
    *inputRead = p - input;
  }
  else
  {
    VT_ASSERT(tagStart != (unsigned char*)0);
    *pendingInputLength = end - tagStart;
  }
  
  VOLT_LOG_ERROR_COMPARE(status, libCtx, status, VT_ERROR_TYPE_PRIMARY,
    fnctLine, "VoltSecureMail2FindHTML", (unsigned char*)0)
  
  return status;
}

static void VoltSecureMail2FindHTMLBody(
  VtSecureMailObject secureMailObj,
  const unsigned char *input,
  unsigned int inputLength,
  unsigned int* inputRead,
  unsigned int* pendingInputLength
)
{
  VoltSecureMail2ReadCtx* readCtx = (VoltSecureMail2ReadCtx*)0;
  VtLibCtx libCtx = (VtLibCtx)0;
  const unsigned char* p;
  const unsigned char* end;
  const unsigned char* tagStart;
  unsigned char c;
  int isBody;

  VT_ASSERT(secureMailObj != (VtSecureMailObject)0);
  VT_ASSERT(inputRead != (unsigned int*)0);
  VT_ASSERT(pendingInputLength != (unsigned int*)0);
  
  readCtx = (VoltSecureMail2ReadCtx*)secureMailObj->localCtx;
  VT_ASSERT(readCtx != (VoltSecureMail2ReadCtx*)0);
  libCtx = secureMailObj->voltObject.libraryCtx;
  VT_ASSERT(libCtx != (VtLibCtx)0);
  
  *inputRead = inputLength;
  *pendingInputLength = 0;
  
  if (inputLength == 0)
    return;
    
  p = input;
  end = input + inputLength;
  
  while ((p != (unsigned char*)0) && (p < end))
  {
    c = *p;
    
    if (c == '<')
    {
      tagStart = p++;
      
      if ((p + 2 < end) && (p[0] == '!') && (p[1] == '-') && (p[2] == '-'))
      {
        p = VoltSecureMail2SkipHTMLComment(p + 3, end);
      }
      else
      {
        isBody = VoltSecureMail2MatchTagName(p, end, "body", libCtx);
        p = VoltSecureMail2SkipHTMLTag(p, end);
        if (p == (unsigned char*)0)
          break;
        if (isBody)
        {
          readCtx->wrapperType = VOLT_SECURE_MAIL_WRAPPER_TYPE_HTML;
          secureMailObj->state = VOLT_SECURE_MAIL_STATE_READ_HEADER_1;
          break;
        }
      }
    }
    else
    {
      p++;
    }
  }
  
  if (p != (unsigned char*)0)
  {
    *inputRead = p - input;
  }
  else
  {
    VT_ASSERT(tagStart != (unsigned char*)0);
    *pendingInputLength = end - tagStart;
  }
}

static int VoltSecureMail2FindMessageStart(
  VtSecureMailObject secureMailObj,
  const unsigned char *input,
  unsigned int inputLength,
  unsigned int* inputRead,
  unsigned int* pendingInputLength
)
{
  int status = 0;
  VoltSecureMail2ReadCtx* readCtx = (VoltSecureMail2ReadCtx*)0;
  VtLibCtx libCtx = (VtLibCtx)0;
  int isHTML;
  int foundTag;
  unsigned char* tag = (unsigned char*)0;
  unsigned int maxTagLength;
  unsigned int tagLength;
  const unsigned char* p;
  const unsigned char* end;
  int i;
  VOLT_DECLARE_FNCT_LINE(fnctLine)

⌨️ 快捷键说明

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