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

📄 sawrite.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 "securearchive.h"
#include "recipinfo.h"
#include "errorctx.h"
#include "vtassert.h"
#include "datanode.h"
#include "compress.h"
#include "archive.h"
#include "sacommon.h"
#include "stringutil.h"
#include "surrender.h"

const char* VoltSecureArchiveRootAttributeName    = "ZDMAttributes";

typedef struct VoltSecureArchiveEntryInfo
{
  unsigned char*    mName;
  int               mCompression;
  unsigned long     mSize;
  unsigned long     mCompressedSize;
  VtTime            mModDate;
  int               mModDateSet;
} VoltSecureArchiveEntryInfo;

typedef struct VoltSecureArchiveWriteLocalCtx
{
  VtIdentityObject              mSignerID;
  VtKeyObject                   mSignerPrivateKey;
  VtCertObject                  mSignerCert;
  VtIdentityList                mRecipientList;
  VtRecipientInfoList           mRecipientInfoList;
  VtPkcs7Param*                 mEncryptionParam;
  int                           mCompressionEnabled;
  VtCompressObject              mCompressObject;
  VtArchiveObject               mArchiveObject;
  VtFileCtx                     mFileCtx;
  VtBufferTypeInfo              mBufferTypeInfo;
  int                           mCurrentEntryType;
  int                           mOutputType;
  unsigned int                  mEncryptedIndexSize;
  VtStreamObject                mCurrentEntryStream;
  VtStreamObject                mIndexDataStream;
  VtStreamObject                mOutputStream;
  int                           mDestroyOutputStream;
  int                           mSecureEntryCount;
  int                           mSecureEntryMax;
  VoltSecureArchiveEntryInfo*   mSecureEntryInfo;
  unsigned char*                mInsecureEntryName;
  VtDataNodeObject              mAttributes;
  VtPolicyCtx                   mPolicyCtx;
  VtStorageCtx                  mStorageCtx;
  VtTransportCtx                mTransportCtx;
  VtRandomObject                mRandom;
} VoltSecureArchiveWriteLocalCtx;

void VoltSecureArchiveWriteLocalCtxDestroy(Pointer obj, Pointer ctx)
{
  VoltSecureArchiveObject* archiveObj = (VoltSecureArchiveObject*)obj;
  VoltSecureArchiveWriteLocalCtx* localCtx;
  VtLibCtx libCtx = (VtLibCtx)0;
  VoltSecureArchiveEntryInfo* entry;
  int i;
  
  if ((obj == (Pointer)0) || (ctx == (Pointer)0))
    return;
  
  localCtx = (VoltSecureArchiveWriteLocalCtx*)ctx;
  VT_ASSERT(localCtx != (VoltSecureArchiveWriteLocalCtx*)0);
  libCtx = archiveObj->voltObject.libraryCtx;
  VT_ASSERT(libCtx != (VtLibCtx)0);
  
  VtDestroyIdentityObject(&localCtx->mSignerID);
  VtDestroyKeyObject(&localCtx->mSignerPrivateKey);
  VtDestroyCertObject(&localCtx->mSignerCert);
  VtDestroyIdentityList(&localCtx->mRecipientList);
  VtDestroyRecipientInfoList(&localCtx->mRecipientInfoList);
  VtDestroyCompressObject(&localCtx->mCompressObject);
  VtDestroyArchiveObject(&localCtx->mArchiveObject);
  VtDestroyStreamObject(&localCtx->mCurrentEntryStream);
  VtDestroyStreamObject(&localCtx->mIndexDataStream);

  if (localCtx->mDestroyOutputStream)
  {
    VtDestroyStreamObject(&localCtx->mOutputStream);
  }
  
  for (i = 0; i < localCtx->mSecureEntryCount; i++)
  {
    entry = &localCtx->mSecureEntryInfo[i];
    Z2Free(entry->mName);
  }
  
  Z2Free(localCtx->mInsecureEntryName);
  
  VtDestroyDataNodeObject(&localCtx->mAttributes);
  
  Z2Free(localCtx->mSecureEntryInfo);
  
  Z2Free(localCtx);
}

static int VoltSecureArchiveSetEntryAttribute(
  VtSecureArchiveObject secureArchiveObj,
  int entryIndex,
  const unsigned char* name,
  const unsigned char* value
)
{
  int status = 0;
  VtLibCtx libCtx = (VtLibCtx)0;
  VoltSecureArchiveWriteLocalCtx* localCtx;
  unsigned char expression[1000];
  //unsigned char* p;
 // int length;
  VtDataNodeObject dataNode = (VtDataNodeObject)0;
  VOLT_DECLARE_FNCT_LINE(fnctLine)
  VOLT_DECLARE_ERROR_DESC(description)

  VOLT_SET_ERROR_DESC(description, 0)

  VT_ASSERT(secureArchiveObj != (VoltSecureArchiveObject*)0);
  
  libCtx = secureArchiveObj->voltObject.libraryCtx;
  VT_ASSERT(libCtx != (VtLibCtx)0);
  localCtx = (VoltSecureArchiveWriteLocalCtx*) secureArchiveObj->localCtx;
  VT_ASSERT(localCtx != (VoltSecureArchiveWriteLocalCtx*)0);
  VT_ASSERT(localCtx->mAttributes != (VtDataNodeObject)0);
  
  do
  {
#if 0
    /* Copy over the top-level entry array node name to the
     * expression. And the opening bracket for the array index.
     */
    length = Z2Strlen(VoltSecureArchiveEntryListAttributeName);
    Z2Memcpy(expression, VoltSecureArchiveEntryListAttributeName, length);
    p = expression + length;
    *p++ = '[';
    
    /* Convert the entryIndex to a string in the num buffer. We
     * work backwards in the buffer, starting with the null
     * terminator.
     */
    VoltNumToDecimalString(entryIndex, p);
    length = Z2Strlen(p);
    p += length;
    
    *p++ = ']';
    *p++ = '.';
    
    /* Now append the entry-level expression. Currently this
     * should just be a simple name, but in theory it could be
     * an expression (i.e. with nested elements). We also do a
     * check for the length of the name passed in to make sure
     * we won't overflow the buffer we allocated on the stack.
     */
    length = Z2Strlen(name);
    if (length >= (expression + sizeof(expression) - p))
    {
      VOLT_SET_FNCT_LINE(fnctLine)
      status = VT_ERROR_BUFFER_TOO_SMALL;
      break;
    }
    Z2Memcpy(p, name, length);
    p += length;
    *p++ = 0;
#else
    VOLT_SET_FNCT_LINE(fnctLine)
    status = VoltSecureArchiveFormatEntryExpression(libCtx,
      entryIndex, name, expression, sizeof(expression));
    if (status != 0)
      break;
#endif
    
    /* Now we can resolve the expression relative to the root
     * attributes node.
     */
    status = VtDataNodeResolveChild(localCtx->mAttributes,
      expression, 1, &dataNode);
    if (status != 0)
    {
      VOLT_SET_FNCT_LINE(fnctLine)
      VOLT_SET_ERROR_DESC(description, (unsigned char*)name)
      status = VT_ERROR_SECURE_ARCHIVE_ATTR_NAME;
      break;
    }
    
    VT_ASSERT(dataNode != (VtDataNodeObject)0);
    
    /* And now we can set the value */
    status = VtDataNodeSetStringValue(dataNode, value);
    if (status != 0)
    {
      VOLT_SET_FNCT_LINE(fnctLine)
      status = VT_ERROR_SECURE_ARCHIVE_SET_ATTR;
      break;
    }
  }
  while (0);
  
  VOLT_LOG_ERROR_COMPARE(status, libCtx, status, 0, fnctLine,
    "VoltSecureArchiveSetSecureEntryAttribute", description)
    
  return status;
}

static int VoltSecureArchiveUpdateAttributes(
  VtSecureArchiveObject secureArchiveObj
)
{
  int status = 0;
  VtLibCtx libCtx = (VtLibCtx)0;
  VoltSecureArchiveWriteLocalCtx* localCtx;
  int i, j;
  unsigned char buffer[100];
  unsigned char attributeName[100];
  int modDateNameLength;
  VoltSecureArchiveEntryInfo* entry;
  VOLT_DECLARE_FNCT_LINE(fnctLine)

  VT_ASSERT(secureArchiveObj != (VoltSecureArchiveObject*)0);
  
  libCtx = secureArchiveObj->voltObject.libraryCtx;
  VT_ASSERT(libCtx != (VtLibCtx)0);
  localCtx = (VoltSecureArchiveWriteLocalCtx*) secureArchiveObj->localCtx;
  VT_ASSERT(localCtx != (VoltSecureArchiveWriteLocalCtx*)0);
  VT_ASSERT(localCtx->mAttributes != (VtDataNodeObject)0);
  
  do
  {
    for (i = 0; i < localCtx->mSecureEntryCount; i++)
    {
      entry = localCtx->mSecureEntryInfo + i;
      
      VOLT_SET_FNCT_LINE(fnctLine)
      status = VoltSecureArchiveSetEntryAttribute(secureArchiveObj,
        i, VoltSecureArchiveNameAttributeName, entry->mName);
      if (status != 0)
        break;

      VOLT_SET_FNCT_LINE(fnctLine)
      status = VoltNumToDecimalString(entry->mSize,
        buffer, sizeof(buffer), libCtx);
      if (status != 0)
        break;
        
      VOLT_SET_FNCT_LINE(fnctLine)
      status = VoltSecureArchiveSetEntryAttribute(secureArchiveObj,
        i, VoltSecureArchiveSizeAttributeName, buffer);
      if (status != 0)
        break;

      VOLT_SET_FNCT_LINE(fnctLine)
      status = VoltNumToDecimalString(entry->mCompression, buffer,
        sizeof(buffer), libCtx);
      if (status != 0)
        break;
        
      VOLT_SET_FNCT_LINE(fnctLine)
      status = VoltSecureArchiveSetEntryAttribute(secureArchiveObj,
        i, VoltSecureArchiveCompressionAttributeName, buffer);
      if (status != 0)
        break;

      if (entry->mCompression != 0)
      {
        VOLT_SET_FNCT_LINE(fnctLine)
        status = VoltNumToDecimalString(entry->mCompressedSize, buffer,
          sizeof(buffer), libCtx);
        if (status != 0)
          break;
          
        VOLT_SET_FNCT_LINE(fnctLine)
        status = VoltSecureArchiveSetEntryAttribute(secureArchiveObj,
          i, VoltSecureArchiveCompressedSizeAttributeName, buffer);
        if (status != 0)
          break;
      }
      
      if (entry->mModDateSet)
      {
        const unsigned char* dateNames[6] = {
          VoltSecureArchiveYearAttributeName,
          VoltSecureArchiveMonthAttributeName,
          VoltSecureArchiveDayAttributeName,
          VoltSecureArchiveHourAttributeName,
          VoltSecureArchiveMinuteAttributeName,
          VoltSecureArchiveSecondAttributeName
        };
        int dateValues[6] = {
          entry->mModDate.year,
          entry->mModDate.month,
          entry->mModDate.day,
          entry->mModDate.hour,
          entry->mModDate.minute,
          entry->mModDate.second
        };
        
        modDateNameLength = Z2Strlen(VoltSecureArchiveModDateAttributeName);
        
        for (j = 0; j < 6; j++)
        {
          Strcpy(attributeName, (char*) VoltSecureArchiveModDateAttributeName,
            libCtx);
          attributeName[modDateNameLength] = '.';
          Strcpy(attributeName + modDateNameLength + 1,
            (char*) dateNames[j], libCtx);
          
          VOLT_SET_FNCT_LINE(fnctLine)
          status = VoltNumToDecimalString(dateValues[j], buffer,
            sizeof(buffer), libCtx);
          if (status != 0)
            break;
            
          VOLT_SET_FNCT_LINE(fnctLine)
          status = VoltSecureArchiveSetEntryAttribute(secureArchiveObj,
            i, attributeName, buffer);
          if (status != 0)
            break;
        }
        if (status != 0)
          break;
      }
    }
  }
  while (0);

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

static int VoltSecureArchiveFinishCompressing(
  VtSecureArchiveObject secureArchiveObj
)
{
  int status = 0;
  VtLibCtx libCtx = (VtLibCtx)0;
  VoltSecureArchiveWriteLocalCtx* localCtx;
  unsigned char* buffer = (unsigned char*)0;
  unsigned int compressedBytes;
  VOLT_DECLARE_FNCT_LINE(fnctLine)
  VOLT_DECLARE_ERROR_TYPE(errorType)

  VT_ASSERT(secureArchiveObj != (VoltSecureArchiveObject*)0);
  
  libCtx = secureArchiveObj->voltObject.libraryCtx;
  VT_ASSERT(libCtx != (VtLibCtx)0);
  localCtx = (VoltSecureArchiveWriteLocalCtx*) secureArchiveObj->localCtx;
  VT_ASSERT(localCtx != (VoltSecureArchiveWriteLocalCtx*)0);
  
  do
  {
    VOLT_SET_ERROR_TYPE(errorType, 0)

    buffer = Z3Malloc(VOLT_TEMP_BUFFER_SIZE);
    if (buffer == (unsigned char*)0)
    {
      VOLT_SET_ERROR_TYPE(errorType, VT_ERROR_TYPE_PRIMARY)
      VOLT_SET_FNCT_LINE(fnctLine)
      status = VT_ERROR_MEMORY;
      break;
    }
    
    for (;;)
    {
      VOLT_SET_FNCT_LINE(fnctLine)
      status = VtCompressUpdate(localCtx->mCompressObject,
        VT_COMPRESS_FLUSH, (unsigned char*)0, 0, (unsigned int*)0,
        buffer, VOLT_TEMP_BUFFER_SIZE, &compressedBytes);
      if ((status != 0) || (compressedBytes == 0))
        break;
      
      VOLT_SET_FNCT_LINE(fnctLine)
      status = VtStreamWrite(localCtx->mCurrentEntryStream,
        buffer, compressedBytes);
      if (status != 0)
        break;
    }
    
    if (status != 0)
      break;

    VOLT_SET_FNCT_LINE(fnctLine)
    status = VtCompressFinal(localCtx->mCompressObject,
      buffer, VOLT_TEMP_BUFFER_SIZE, &compressedBytes);
    if (status != 0)
      break;

    VtDestroyCompressObject(&localCtx->mCompressObject);
    
    VOLT_SET_FNCT_LINE(fnctLine)
    status = VtStreamWrite(localCtx->mCurrentEntryStream,
      buffer, compressedBytes);
    if (status != 0)
      break;
  }
  while (0);
  
  Z2Free(buffer);

⌨️ 快捷键说明

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