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

📄 sm2write.c

📁 IBE是一种非对称密码技术
💻 C
📖 第 1 页 / 共 3 页
字号:

  VtDestroyDataNodeObject(&attributesNode);
  VtDestroyStreamObject(&attributesStream);
  
  VOLT_LOG_ERROR_COMPARE(status, libCtx, status, errorType,
    fnctLine, "VoltSecureMail2WriteInit", (unsigned char*)0)

  return status;
}

int VoltSM2FromZDMWriteInit (
   VtSecureMailObject secureMailObj,
   VtPolicyCtx policyCtx,
   VtStorageCtx storageCtx,
   VtTransportCtx transportCtx,
   VtRandomObject random
)
{
  int status;
  VoltSecureMailFromZDMWriteCtx* writeCtx = 
    (VoltSecureMailFromZDMWriteCtx*) secureMailObj->localCtx;
  VoltLibCtx* libCtx = (VoltLibCtx*) secureMailObj->voltObject.libraryCtx;
  unsigned int supportedVersion;
  VOLT_DECLARE_FNCT_LINE(fnctLine)
  
  do
  {
    VOLT_SET_FNCT_LINE(fnctLine)
    status = VtGetZDMParam(writeCtx->zdmObj, VtZDMParamRecipientList,
      (Pointer*)&writeCtx->baseCtx.recipListRef);
    if (status != 0)
      break;
    
    VOLT_SET_FNCT_LINE(fnctLine)
    status = VtGetZDMParam(writeCtx->zdmObj, VtZDMParamSupportedVersion,
      (Pointer*)&supportedVersion);
      
    VOLT_SET_FNCT_LINE(fnctLine)
    status = VoltSecureMail2WriteInitCommon(secureMailObj, policyCtx,
      supportedVersion);
  }
  while (0);
  
  VOLT_LOG_ERROR_COMPARE(status, libCtx, status, 0,
    fnctLine, "VoltSM2FromZDMWriteInit", (unsigned char*)0)

  return status;
}

int VoltSM2FromZDMWriteUpdate (
  VtSecureMailObject secureMailObj,
  VtRandomObject random,
  unsigned char *inputData,
  unsigned int inputDataLen,
  unsigned char *message,
  unsigned int bufferSize,
  unsigned int *messageLen
)
{
  int status = 0;
  VoltSecureMailFromZDMWriteCtx* writeCtx =
    (VoltSecureMailFromZDMWriteCtx*) secureMailObj->localCtx;
  VoltLibCtx* libCtx = (VoltLibCtx*)(secureMailObj->voltObject.libraryCtx);
  unsigned int minBufferSize;
  unsigned char* p;
  unsigned int i;
  VtItem* item;
  VtItem* newLineItem;
  unsigned int inputBufferSize, inputSize;
  unsigned char* inputBuffer = (unsigned char*)0;
  unsigned int messageFormat;
  int final = 0;
  VOLT_DECLARE_FNCT_LINE(fnctLine)
  VOLT_DECLARE_ERROR_TYPE(errorType)
  
  VOLT_SET_ERROR_TYPE(errorType, 0)

  do
  {
    *messageLen = 0;
    
    /* Buffer size must be big enough to hold all of the prelim
     * data and all of the trailing
     */
    minBufferSize =
      (secureMailObj->state == VOLT_SECURE_MAIL_STATE_WRITE_INIT) ?
      writeCtx->baseCtx.prelimLen :
      VOLT_SECURE_MAIL_BASE_64_LINE_LENGTH + 2;
      
    /* Check that the output buffer is big enough */
    if (bufferSize < minBufferSize)
    {
      VOLT_SET_FNCT_LINE(fnctLine)
      VOLT_SET_ERROR_TYPE(errorType, VT_ERROR_TYPE_PRIMARY)
      *messageLen = minBufferSize;
      status = VT_ERROR_BUFFER_TOO_SMALL;
      break;
    }
    
    /* If we're in the init state, then we haven't output the
     * preliminary header text yet, so we run through the
     * header items and copy then over to the output buffer.
     * Then we prepare to output the data from the ZDM object
     * by setting its message format to return the encrypted
     * message body. Finally we update the state.
     */
    if (secureMailObj->state == VOLT_SECURE_MAIL_STATE_WRITE_INIT)
    {
      newLineItem = &writeCtx->baseCtx.itemArray[VOLT_WRITE_SM_ITEM_NEW_LINE];
      p = message;
      for (i = VOLT_WRITE_SM_HEAD_INDEX_START;
          i <= VOLT_WRITE_SM_HEAD_INDEX_END; ++i)
      {
        item = &writeCtx->baseCtx.itemArray[i];
        if (item->len > 0)
        {
          Z2Memcpy(p, item->data, item->len);
          p += item->len;
          Z2Memcpy(p, newLineItem->data, newLineItem->len);
          p += newLineItem->len;
        }
      }
      *messageLen = writeCtx->baseCtx.prelimLen;
      
      messageFormat = VT_MESSAGE_FORMAT_SECURE_MAIL;
    
      VOLT_SET_FNCT_LINE(fnctLine)
      status = VtSetZDMParam(writeCtx->zdmObj, VtZDMParamMessageFormat,
        (Pointer)&messageFormat);
      if (status != 0)
        break;

      secureMailObj->state = VOLT_SECURE_MAIL_STATE_WRITE_UPDATE;
    }
    else if (secureMailObj->state == VOLT_SECURE_MAIL_STATE_WRITE_UPDATE)
    {
      VOLT_SET_FNCT_LINE(fnctLine)
      status = VoltDetermineBase64InputSize(libCtx, secureMailObj->base64,
        bufferSize, &inputBufferSize);
      if (status != 0)
        break;
      
      inputBuffer = (unsigned char*) Z3Malloc(inputBufferSize);
      if (inputBuffer == (unsigned char*)0)
      {
        VOLT_SET_FNCT_LINE(fnctLine)
        VOLT_SET_ERROR_TYPE(errorType, VT_ERROR_TYPE_PRIMARY)
        status = VT_ERROR_MEMORY;
        break;
      }
      
      VOLT_SET_FNCT_LINE(fnctLine)
      status = VtZDMWriteUpdate(writeCtx->zdmObj, (VtRandomObject)0,
        (unsigned char*)0, 0, inputBuffer, inputBufferSize, &inputSize);
      if (status == VT_ERROR_END_OF_STREAM)
      {
        final = 1;
        status = 0;
      }
      if (status != 0)
        break;
      
      if (final)
      {
        VOLT_SET_FNCT_LINE(fnctLine)
        status = VtEncodeFinal(secureMailObj->base64, random,
          (unsigned char*)0,0, message, bufferSize, messageLen);
        if (status != 0)
          break;

        secureMailObj->state = VOLT_SECURE_MAIL_STATE_WRITE_FINAL;
      }
      else
      { 
        VOLT_SET_FNCT_LINE(fnctLine)
        status = VtEncodeUpdate(secureMailObj->base64, random,
          inputBuffer, inputSize, message, bufferSize, messageLen);
        if (status != 0)
          break;
        
        writeCtx->dataConsumed += inputSize;
      }
    }
  }
  while (0);

  Z2Free(inputBuffer);
  
  VOLT_LOG_ERROR_COMPARE(status, libCtx, status, errorType,
    fnctLine, "VoltSM2FromZDMWriteUpdate", (unsigned char*)0)

  return status;
}

/* Implements VSMWriteFinal.
 */
int VoltSM2FromZDMWriteFinal (
   VtSecureMailObject secureMailObj,
   VtRandomObject random,
   unsigned char *inputData,
   unsigned int inputDataLen,
   unsigned char *message,
   unsigned int bufferSize,
   unsigned int *messageLen
)
{ 
  int status = 0;
  VoltSecureMailFromZDMWriteCtx* writeCtx =
    (VoltSecureMailFromZDMWriteCtx*) secureMailObj->localCtx;
  VoltLibCtx* libCtx = (VoltLibCtx*)(secureMailObj->voltObject.libraryCtx);
  unsigned int messageBodySize;
  unsigned int remainingSize = 0;
  unsigned int inputSize;
  unsigned int encodedSize;
  unsigned int updateSize;
  unsigned char* p;
  unsigned int remainingBufferSize;
  unsigned int i;
  VtItem* item;
  VtItem* newLineItem;
  VoltEncodeDecodeSizeInfo encodeDecodeSizeInfo;
  VOLT_DECLARE_FNCT_LINE(fnctLine)
  VOLT_DECLARE_ERROR_TYPE(errorType)
  
  VOLT_SET_ERROR_TYPE(errorType, 0)

  *messageLen = 0;
  
  do
  {
    if (writeCtx->zdmObj == (VtZDMObject)0)
    {
      VOLT_SET_FNCT_LINE(fnctLine)
      status = VT_ERROR_INVALID_ZDM_OBJ;
      break;
    }
    
    switch (secureMailObj->state)
    {
      case VOLT_SECURE_MAIL_STATE_WRITE_INIT:
        remainingSize += writeCtx->baseCtx.prelimLen;
        /* fall through */
      case VOLT_SECURE_MAIL_STATE_WRITE_UPDATE:
        VOLT_SET_FNCT_LINE(fnctLine)
        status = VtGetZDMParam(writeCtx->zdmObj, VtZDMParamMessageBodySize,
          (Pointer*)&messageBodySize);
        if (status != 0)
          break;
        
        inputSize = messageBodySize - writeCtx->dataConsumed;
        
        encodeDecodeSizeInfo.dataToProcess = (unsigned char *)0;
#if VT_64_BIT_LENGTH == 64
        encodeDecodeSizeInfo.dataToProcessLen = (VtUInt64)inputSize;
#else
        encodeDecodeSizeInfo.dataToProcessLen = inputSize;
#endif
        VOLT_SET_FNCT_LINE(fnctLine)
        status = VoltBase64GetEncodeDecodeSize(
          secureMailObj->base64, random, VOLT_CALLER_ENCODE_FINAL,
          &encodeDecodeSizeInfo);
#if VT_64_BIT_LENGTH == 64
        encodedSize = (unsigned int)(encodeDecodeSizeInfo.processedDataLen);
#else
        encodedSize = encodeDecodeSizeInfo.processedDataLen;
#endif
        if (status != VT_ERROR_BUFFER_TOO_SMALL)
        {
          VOLT_SET_FNCT_LINE(fnctLine)
          VOLT_SET_ERROR_TYPE(errorType, VT_ERROR_TYPE_PRIMARY)
          status = VT_ERROR_GENERAL;
          break;
        }
        /* This is sort of a hack, but it simplifies the checks for
         * minimum buffer sizes if we make sure the buffer size we'll
         * pass in to WriteUpdate is always at least big enough to
         * hold a line of base64 output. The edge case is when we're
         * at the end of the message data output, and we still have
         * some buffered base 64 output. If we don't add this extra
         * amount to the buffer size, then if the length of the buffer
         * line length plus the size of the trailing data isn't bigger
         * than the size of a full line of output, then the minimum
         * buffer size check will fail. Adding a little extra to the
         * necessary buffer size avoids this problem.
         */
        encodedSize += (VOLT_SECURE_MAIL_BASE_64_LINE_LENGTH + 2);
        status = 0;
        remainingSize += encodedSize;
        
        /* fall through */
      case VOLT_SECURE_MAIL_STATE_WRITE_FINAL:
        remainingSize += writeCtx->baseCtx.trailLen;
    }
    
    if (status != 0)
      break;
    
    if (remainingSize > bufferSize)
    {
      VOLT_SET_FNCT_LINE(fnctLine)
      VOLT_SET_ERROR_TYPE(errorType, VT_ERROR_TYPE_PRIMARY)
      *messageLen = remainingSize;
      status = VT_ERROR_BUFFER_TOO_SMALL;
      break;
    }
    
    p = message;
    remainingBufferSize = bufferSize;
    
    while (secureMailObj->state != VOLT_SECURE_MAIL_STATE_WRITE_FINAL)
    {
      VOLT_SET_FNCT_LINE(fnctLine)
      status = VoltSM2FromZDMWriteUpdate(secureMailObj, random,
        (unsigned char*)0, 0, p, remainingBufferSize, &updateSize);
      if (status != 0)
        break;
      
      p += updateSize;
      remainingBufferSize -= updateSize;
    }
    
    if (status != 0)
      break;
      
    newLineItem = &writeCtx->baseCtx.itemArray[VOLT_WRITE_SM_ITEM_NEW_LINE];

    for (i = VOLT_WRITE_SM_FOOT_INDEX_START;
         i <= VOLT_WRITE_SM_FOOT_INDEX_END; ++i)
    {
      item = &writeCtx->baseCtx.itemArray[i];
      if (item->len > 0)
      {
        Z2Memcpy(p, item->data, item->len);
        p += item->len;
        Z2Memcpy(p, newLineItem->data, newLineItem->len);
        p += newLineItem->len;
      }
    }
    
    *messageLen = p - message;
  }
  while (0);

  VOLT_LOG_ERROR_COMPARE(status, libCtx, status, 0,
    fnctLine, "VoltSM2FromZDMWriteFinal", (unsigned char*)0)

  return status;
}

⌨️ 快捷键说明

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