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

📄 sawrite.c

📁 IBE是一种非对称密码技术
💻 C
📖 第 1 页 / 共 5 页
字号:
  VOLT_LOG_ERROR_COMPARE(status, libCtx, status, errorType,
    fnctLine, "VoltSecureArchiveFinishCompressing", (unsigned char*)0)
    
  return status;
}

static int VoltSecureArchiveEncryptStream(
  VtSecureArchiveObject secureArchiveObj,
  VtStreamObject inputStream,
  unsigned int* encryptedSize
)
{
  int status = 0;
  VtLibCtx libCtx = (VtLibCtx)0;
  VoltSecureArchiveWriteLocalCtx* localCtx;
  VtPkcs7Object p7SignedData = (VtPkcs7Object)0;
  VtPkcs7Object p7EnvelopedData = (VtPkcs7Object)0;
  VtPkcs7SignerInfo signerInfo;
  unsigned int signedSize;
  VtStreamSize streamSize;
  VtStreamSize readSize;
  unsigned char* inputDataBuffer = (unsigned char*)0;
  unsigned char* signedDataBuffer = (unsigned char*)0;
  unsigned char* envelopedDataBuffer = (unsigned char*)0;
  unsigned int signedDataBufferSize = 0;
  unsigned int signedDataSize;
  unsigned int envelopedDataBufferSize = 0;
  unsigned int envelopedDataSize;
  int final = 0;
  VoltSurrenderCtx* surrenderCtx;
  VtSurrenderCallback surrenderCallback;
  VOLT_DECLARE_FNCT_LINE(fnctLine)
  VOLT_DECLARE_ERROR_TYPE(errorType)
  
  VT_ASSERT(secureArchiveObj != (VoltSecureArchiveObject*)0);
  VT_ASSERT(inputStream != (VtStreamObject)0);
  
  libCtx = secureArchiveObj->voltObject.libraryCtx;
  VT_ASSERT(libCtx != (VtLibCtx)0);
  localCtx = (VoltSecureArchiveWriteLocalCtx*) secureArchiveObj->localCtx;
  VT_ASSERT(localCtx != (VoltSecureArchiveWriteLocalCtx*)0);

  VT_ASSERT(localCtx->mArchiveObject != (VtArchiveObject)0);
  
  if (encryptedSize != (unsigned int*)0)
    *encryptedSize = 0;
    
  do
  {
    VOLT_SET_ERROR_TYPE(errorType, 0)

    inputDataBuffer = Z3Malloc(VOLT_TEMP_BUFFER_SIZE);
    if (inputDataBuffer == (unsigned char*)0)
    {
      VOLT_SET_ERROR_TYPE(errorType, VT_ERROR_TYPE_PRIMARY)
      VOLT_SET_FNCT_LINE(fnctLine)
      status = VT_ERROR_MEMORY;
      break;
    }
    
    status = VtStreamSetPosition(inputStream, 0, VT_STREAM_FROM_START);
    if (status != 0)
      break;
      
    status = VtStreamGetSize(inputStream, &streamSize);
    if (status != 0)
      break;
    
    /* Create and initialize the PKCS7 signing object  */
    VOLT_SET_FNCT_LINE(fnctLine)
    status = VtCreatePkcs7Object(libCtx, VtPkcs7ImplWriteSignedDSA,
      (Pointer)0, &p7SignedData);
    if (status != 0)
      break;

    VT_ASSERT(p7SignedData != (VtPkcs7Object)0);
    
    surrenderCtx = (VoltSurrenderCtx*) secureArchiveObj->voltObject.surrenderCtx;
    
    if (surrenderCtx != (VoltSurrenderCtx*)0)
    {
      surrenderCallback.appData = surrenderCtx->appData;
      surrenderCallback.AppDataCopy = surrenderCtx->AppDataCopy;
      surrenderCallback.AppDataFree = surrenderCtx->AppDataFree;
      surrenderCallback.Surrender = surrenderCtx->Surrender;
      
      VOLT_SET_FNCT_LINE(fnctLine)
      status = VtSetPkcs7Param(p7SignedData, VtPkcs7ParamSurrenderCallback,
        (Pointer)&surrenderCallback);
      if (status != 0)
        break;
    }

    if (localCtx->mSignerID == (VtIdentityObject)0)
    {
      VOLT_SET_ERROR_TYPE(errorType, VT_ERROR_TYPE_PRIMARY)
      VOLT_SET_FNCT_LINE(fnctLine)
      status = VT_ERROR_INVALID_ID_OBJ;
      break;
    }
    
    if ((localCtx->mSignerPrivateKey != (VtKeyObject)0) &&
        (localCtx->mSignerCert != (VtCertObject)0))
    {
      signerInfo.privateKey = localCtx->mSignerPrivateKey;
      signerInfo.signerCert = localCtx->mSignerCert;
      signerInfo.signerId = localCtx->mSignerID;
      
      VOLT_SET_FNCT_LINE(fnctLine)
      status = VtSetPkcs7Param(p7SignedData, VtPkcs7ParamSignerInfo,
        (Pointer)&signerInfo);
      if (status != 0)
        break;
    }
    else
    {
      VOLT_SET_FNCT_LINE(fnctLine)
      status = VtSetPkcs7Param(p7SignedData, VtPkcs7ParamSignerId,
        (Pointer)localCtx->mSignerID);
      if (status != 0)
        break;
    }

    VOLT_SET_FNCT_LINE(fnctLine)
    status = VtSetPkcs7Param(p7SignedData, VtPkcs7ParamDataLen,
      (Pointer)&streamSize);
    if (status != 0)
      break;
    
    VOLT_SET_FNCT_LINE(fnctLine)
    status = VtPkcs7WriteInit(p7SignedData, localCtx->mPolicyCtx,
      localCtx->mStorageCtx, localCtx->mTransportCtx, localCtx->mRandom);
    if (status != 0)
      break;

    /* Call WriteFinal to determine how big the output from the signing
     * object will be so that we can set the data length for the
     * enveloping object
     */
    VOLT_SET_FNCT_LINE(fnctLine)
    status = VtPkcs7WriteFinal(p7SignedData, localCtx->mRandom,
      (unsigned char *)0, streamSize, (unsigned char *)0, 0, &signedSize);
    if (status == 0)
      status = VT_ERROR_GENERAL;
    if (status != VT_ERROR_BUFFER_TOO_SMALL)
      break;
        
    /* Create the PKCS7 objects for enveloping and initialize it */
    VOLT_SET_FNCT_LINE(fnctLine)
    status = VtCreatePkcs7Object(libCtx, VtPkcs7ImplWriteEnvelopeIBE,
      (Pointer)0, &p7EnvelopedData);
    if (status != 0)
      break;

    VT_ASSERT(p7EnvelopedData != (VtPkcs7Object)0);
    
    if (surrenderCtx != (VoltSurrenderCtx*)0)
    {
      VOLT_SET_FNCT_LINE(fnctLine)
      status = VtSetPkcs7Param(p7EnvelopedData, VtPkcs7ParamSurrenderCallback,
        (Pointer)&surrenderCallback);
      if (status != 0)
        break;
    }

    if (localCtx->mRecipientList != (VtIdentityList)0)
    {
      VOLT_SET_FNCT_LINE(fnctLine)
      status = VtSetPkcs7Param(p7EnvelopedData, VtPkcs7ParamRecipientList,
        (Pointer)localCtx->mRecipientList);
      if (status != 0)
        break;

      /* Set it to use either 3DES or AES for the symmetric encryption */
      VOLT_SET_FNCT_LINE(fnctLine)
      status = VtSetPkcs7Param(p7EnvelopedData, localCtx->mEncryptionParam,
        (Pointer)0);
      if (status != 0)
        break;
    }
    else
    {
      VOLT_SET_FNCT_LINE(fnctLine)
      status = VtSetPkcs7Param(p7EnvelopedData, VtPkcs7ParamRecipientInfoList,
        (Pointer)localCtx->mRecipientInfoList);
      if (status != 0)
        break;
    }
    
    VOLT_SET_FNCT_LINE(fnctLine)
    status = VtSetPkcs7Param(p7EnvelopedData, VtPkcs7ParamDataLen,
      (Pointer)&signedSize);
    if (status != 0)
      break;
    
    VOLT_SET_FNCT_LINE(fnctLine)
    status = VtPkcs7WriteInit(p7EnvelopedData, localCtx->mPolicyCtx,
      localCtx->mStorageCtx, localCtx->mTransportCtx, localCtx->mRandom);
    if (status != 0)
      break;
    
    do
    {
      /* Read the input data from the stream */
      VOLT_SET_FNCT_LINE(fnctLine)
      status = VtStreamRead(inputStream, inputDataBuffer,
        VOLT_TEMP_BUFFER_SIZE, &readSize);
      if (status == VT_ERROR_END_OF_STREAM)
      {
        final = 1;
        status = 0;
      }
      if (status != 0)
        break;
      
      if (final)
      {
        VOLT_SET_FNCT_LINE(fnctLine)
        status = VtPkcs7WriteFinal(p7SignedData, localCtx->mRandom,
          (unsigned char*)0, 0, (unsigned char*)0, 0, &signedDataSize);
        if (status == 0)
          status = VT_ERROR_GENERAL;
        if (status != VT_ERROR_BUFFER_TOO_SMALL)
          break;
      }
      else
      {
        /* Figure out how big the signed data output buffer needs to be.
        * This call is expected to return VT_ERROR_BUFFER_TOO_SMALL.
        */
        VOLT_SET_FNCT_LINE(fnctLine)
        status = VtPkcs7WriteUpdate(p7SignedData, localCtx->mRandom,
          inputDataBuffer, readSize, (unsigned char*)0, 0, &signedDataSize);
        if (status == 0)
          status = VT_ERROR_GENERAL;
        if (status != VT_ERROR_BUFFER_TOO_SMALL)
          break;
      }
      
      /* Reallocate the signed data buffer if it's too small */
      if (signedDataSize > signedDataBufferSize)
      {
        Z2Free(signedDataBuffer);
        signedDataBuffer = (unsigned char*) Z3Malloc(signedDataSize);
        if (signedDataBuffer == (unsigned char*)0)
        {
          VOLT_SET_ERROR_TYPE(errorType, VT_ERROR_TYPE_PRIMARY)
          VOLT_SET_FNCT_LINE(fnctLine)
          status = VT_ERROR_MEMORY;
          break;
        }
        signedDataBufferSize = signedDataSize;
      }
      
      /* Now update the signed data for real */
      if (final)
      {
        VOLT_SET_FNCT_LINE(fnctLine)
        status = VtPkcs7WriteFinal(p7SignedData, localCtx->mRandom,
          (unsigned char*)0, 0, signedDataBuffer, signedDataBufferSize,
          &signedDataSize);
        if (status != 0)
          break;
      }
      else
      {
        VOLT_SET_FNCT_LINE(fnctLine)
        status = VtPkcs7WriteUpdate(p7SignedData, localCtx->mRandom, inputDataBuffer,
          readSize, signedDataBuffer, signedDataBufferSize, &signedDataSize);
        if (status != 0)
          break;
      }
      
      /* Figure out how big the enveloped data output buffer needs to be.
      * This call is expected to return VT_ERROR_BUFFER_TOO_SMALL.
      */
      if (final)
      {
        VOLT_SET_FNCT_LINE(fnctLine)
        status = VtPkcs7WriteFinal(p7EnvelopedData, localCtx->mRandom,
          signedDataBuffer, signedDataSize, (unsigned char*)0, 0,
          &envelopedDataSize);
        if (status == 0)
          status = VT_ERROR_GENERAL;
        if (status != VT_ERROR_BUFFER_TOO_SMALL)
          break;
      }
      else
      {
        VOLT_SET_FNCT_LINE(fnctLine)
        status = VtPkcs7WriteUpdate(p7EnvelopedData, localCtx->mRandom,
          signedDataBuffer, signedDataSize, (unsigned char*)0, 0, &envelopedDataSize);
        if (status == 0)
          status = VT_ERROR_GENERAL;
        if (status != VT_ERROR_BUFFER_TOO_SMALL)
          break;
      }
      
      /* Reallocate the enveloped data buffer if it's too small */
      if (envelopedDataSize > envelopedDataBufferSize)
      {
        Z2Free(envelopedDataBuffer);
        envelopedDataBuffer = (unsigned char*) Z3Malloc(envelopedDataSize);
        if (envelopedDataBuffer == (unsigned char*)0)
        {
          VOLT_SET_ERROR_TYPE(errorType, VT_ERROR_TYPE_PRIMARY)
          VOLT_SET_FNCT_LINE(fnctLine)
          status = VT_ERROR_MEMORY;
          break;
        }
        envelopedDataBufferSize = envelopedDataSize;
      }
      
      /* Now update for real */
      if (final)
      {
        VOLT_SET_FNCT_LINE(fnctLine)
        status = VtPkcs7WriteFinal(p7EnvelopedData, localCtx->mRandom,
          signedDataBuffer, signedDataSize, envelopedDataBuffer,
          envelopedDataBufferSize, &envelopedDataSize);
        if (status != 0)
          break;
      }
      else
      {
        VOLT_SET_FNCT_LINE(fnctLine)
        status = VtPkcs7WriteUpdate(p7EnvelopedData, localCtx->mRandom,
          signedDataBuffer, signedDataSize, envelopedDataBuffer,
          envelopedDataBufferSize, &envelopedDataSize);
        if (status != 0)
          break;
      }
      
      /* Write the enveloped data to the archive. We assume that
       * the archive has already been set with the appropriate
       * current entry to write to.
       */
      status = VtArchiveWriteUpdate(localCtx->mArchiveObject,
        envelopedDataBuffer, envelopedDataSize, (unsigned char*)0,
        0, (unsigned int*)0);
      if (status != 0)
        break;
      
      if (encryptedSize != (unsigned int*)0)
        *encryptedSize += envelopedDataSize;
    }
    while (!final);
  }
  while (0);
  
  Z2Free(inputDataBuffer);
  Z2Free(signedDataBuffer);
  Z2Free(envelopedDataBuffer);

  VtDestroyPkcs7Object(&p7SignedData);
  VtDestroyPkcs7Object(&p7EnvelopedData);

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

static int VoltSecureArchiveWriteCurrentEntry(
  VtSecureArchiveObject secureArchiveObj
)
{
  int status = 0;
  VtLibCtx libCtx = (VtLibCtx)0;
  VoltSecureArchiveWriteLocalCtx* localCtx;
  VtStreamSize entrySize;
  VtStreamSize readSize;
  VoltSecureArchiveEntryInfo* secureEntryInfo;
  unsigned char* buffer = (unsigned char*)0;
  unsigned char entryName[100];
  int prefixLength;
  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)
    
    if (localCtx->mCurrentEntryType == VT_SECURE_ARCHIVE_CURRENT_ENTRY_SECURE)
    {
      secureEntryInfo = localCtx->mSecureEntryInfo +
        localCtx->mSecureEntryCount - 1;
      
      /* Finish compressing if necessary */
      if (localCtx->mCompressionEnabled)
      {
        VOLT_SET_FNCT_LINE(fnctLine)
        status = VoltSecureArchiveFinishCompressing(secureArchiveObj);
        if (status != 0)
          break;
      }
      
      VOLT_SET_FNCT_LINE(fnctLine)
      status = VtStreamGetSize(localCtx->mCurrentEntryStream, &entrySize);
      if (status != 0)

⌨️ 快捷键说明

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