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

📄 sm2read.c

📁 IBE是一种非对称密码技术
💻 C
📖 第 1 页 / 共 4 页
字号:
      else
      {
        VOLT_SET_FNCT_LINE(fnctLine)
        status = VtPkcs7ReadUpdate(secureMailObj->p7SignedData,
          signedData + dataRead, signedDataLength - dataRead, &dataRead,
          data + readCtx->pendingHeadersLength, newDataLength, &newDataLength);
      }
      
      if (status != 0)
        break;
      
      dataLength = readCtx->pendingHeadersLength + newDataLength;
      
      Z2Free(readCtx->pendingHeaders);
      readCtx->pendingHeaders = (unsigned char*)0;
      readCtx->pendingHeadersLength = 0;
      
      p = data;
      end = data + dataLength;
      
      if (!readCtx->processedHeaders)
      {
        VOLT_SET_FNCT_LINE(fnctLine)
        status = VoltSecureMail2ParseHeaders(secureMailObj,
          p, end - p, &dataRead);
        if (status != 0)
          break;
          
        if (!readCtx->processedHeaders)
        {
          if (final)
          {
            VOLT_SET_ERROR_TYPE(errorType, VT_ERROR_TYPE_PRIMARY)
            VOLT_SET_FNCT_LINE(fnctLine)
            status = VT_ERROR_INVALID_SECURE_MAIL_MSG;
            break;
          }
          VT_ASSERT(readCtx->pendingHeaders == (unsigned char*)0);
          readCtx->pendingHeaders = data;
          readCtx->pendingHeadersLength = dataLength;
          data = (unsigned char*)0;
          break;
        }
        
        p += dataRead;
      }
      
      dataLength = end - p;
      
      *outputLength = (dataLength < outputBufferSize) ?
        dataLength : outputBufferSize;
      
      Z2Memcpy(output, p, *outputLength);
      
      if (dataLength > *outputLength)
      {
        readCtx->pendingOutput = (unsigned char*)
          Z3Malloc(dataLength - *outputLength);
        if (readCtx->pendingOutput == (unsigned char*)0)
        {
          VOLT_SET_ERROR_TYPE(errorType, VT_ERROR_TYPE_PRIMARY)
          VOLT_SET_FNCT_LINE(fnctLine)
          status = VT_ERROR_MEMORY;
          break;
        }
        readCtx->pendingOutputLength = dataLength - *outputLength;
        Z2Memcpy(readCtx->pendingOutput, p + *outputLength,
          readCtx->pendingOutputLength);
      }
    }
    
    if (final)
    {
      secureMailObj->state = VOLT_SECURE_MAIL_STATE_READ_FOOTER_1;
      if (!readCtx->processedHeaders)
        status = VT_ERROR_INVALID_ZDM_MSG;
    }
  }
  while (0);
  
  Z2Free(envelopedData);
  Z2Free(signedData);
  Z2Free(data);
  
  VOLT_LOG_ERROR_COMPARE(status, libCtx, status, errorType,
    fnctLine, "VoltSecureMail2ReadBody", (unsigned char*)0)
  
  return status;
}

int VoltSecureMail2ReadUpdate(
  VtSecureMailObject secureMailObj,
  unsigned char *message,
  unsigned int messageLen,
  unsigned int *bytesRead,
  unsigned char *outputData,
  unsigned int bufferSize,
  unsigned int *outputDataLen
)
{
  int status = 0;
  VoltSecureMail2ReadCtx* readCtx = (VoltSecureMail2ReadCtx*)0;
  VtLibCtx libCtx = (VtLibCtx)0;
  unsigned char* mergedInput = (unsigned char*)0;
  unsigned char* input;
  unsigned int inputLength;
  unsigned char* p;
  unsigned char* q;
  unsigned int remainingSize;
  unsigned int inputRead;
  //unsigned int outputWritten;
  unsigned char* end;
  //unsigned char* tag = (unsigned char*)0;
  unsigned int pendingInputLength = 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);
  
  *bytesRead = 0;
  *outputDataLen = 0;
  
  do
  {
    if (readCtx->wrapperType == VOLT_SECURE_MAIL_WRAPPER_TYPE_INVALID)
    {
      VOLT_SET_ERROR_TYPE(errorType, VT_ERROR_TYPE_PRIMARY)
      VOLT_SET_FNCT_LINE(fnctLine)
      status = VT_ERROR_INVALID_ZDM_MSG;
      break;
    }
    
    /* Initialize out internal output buffer info to be the entire
     * output buffer passed in by the caller. The effective output
     * buffer may be shrunk if we've got any pending output data
     * that needs to be processed.
     */
    q = outputData;
    remainingSize = bufferSize;
    
    /* Handle any pending output. To simplify the logic and to
     * ensure that the pending output buffer doesn't keep growing,
     * we return a BUFFER_TOO_SMALL error if the output buffer
     * isn't big enough to handle all of the pending output data.
     */
    if (readCtx->pendingOutputLength > 0)
    {
      VT_ASSERT(readCtx->pendingOutput != (unsigned char*)0);
      
      if (readCtx->pendingOutputLength > bufferSize)
      {
        VOLT_SET_ERROR_TYPE(errorType, VT_ERROR_TYPE_PRIMARY)
        VOLT_SET_FNCT_LINE(fnctLine)
        status = VT_ERROR_BUFFER_TOO_SMALL;
        break;
      }
      Z2Memcpy(outputData, readCtx->pendingOutput,
        readCtx->pendingOutputLength);
      *outputDataLen += readCtx->pendingOutputLength;
      q += readCtx->pendingOutputLength;
      remainingSize -= readCtx->pendingOutputLength;
      Z2Free(readCtx->pendingOutput);
      readCtx->pendingOutput = (unsigned char*)0;
      readCtx->pendingOutputLength = 0;
    }
    
    /* If there's any pending input (i.e. some overlap from
     * the previous call to ReadUpdate, then we create a new
     * buffer to merge the pending input with the new input.
     */
    if (readCtx->pendingInput != (unsigned char*)0)
    {
      VT_ASSERT(readCtx->pendingInputLength > 0);
      
      mergedInput = (unsigned char*)
        Z3Malloc(readCtx->pendingInputLength + messageLen);
      if (mergedInput == (unsigned char*)0)
      {
        VOLT_SET_ERROR_TYPE(errorType, VT_ERROR_TYPE_PRIMARY)
        VOLT_SET_FNCT_LINE(fnctLine)
        status = VT_ERROR_MEMORY;
        break;
      }
      Z2Memcpy(mergedInput, readCtx->pendingInput,
        readCtx->pendingInputLength);
      Z2Memcpy(mergedInput + readCtx->pendingInputLength,
        message, messageLen);
      
      input = mergedInput;
      inputLength = readCtx->pendingInputLength + messageLen;
      
      Z2Free(readCtx->pendingInput);
      readCtx->pendingInput = (unsigned char*)0;
      readCtx->pendingInputLength = 0;
    }
    else
    {
      input = message;
      inputLength = messageLen;
    }
    
    p = input;
    end = input + inputLength;

    /* Check for the HTML overlay by looking for the HTML start tag */
    if (secureMailObj->state == VOLT_SECURE_MAIL_STATE_READ_HTML)
    {
      status = VoltSecureMail2FindHTML(secureMailObj, p, end - p,
        &inputRead, &pendingInputLength);
      p += inputRead;
    }
    
    /* We've found the HTML tag - now look for the start of the body */
    if (secureMailObj->state == VOLT_SECURE_MAIL_STATE_READ_BODY)
    {
      VoltSecureMail2FindHTMLBody(secureMailObj, p, end - p,
        &inputRead, &pendingInputLength);
      p += inputRead;
    }
    
    if (secureMailObj->state == VOLT_SECURE_MAIL_STATE_READ_HEADER_1)
    {
      VT_ASSERT(readCtx->wrapperType != VOLT_SECURE_MAIL_WRAPPER_TYPE_UNKNOWN);

      VOLT_SET_FNCT_LINE(fnctLine)
      status = VoltSecureMail2FindMessageStart(secureMailObj, p, end - p,
        &inputRead, &pendingInputLength);
      p += inputRead;
    }
    
    if (secureMailObj->state == VOLT_SECURE_MAIL_STATE_READ_HEADER_2)
    {
      VOLT_SET_FNCT_LINE(fnctLine)
      status = VoltSecureMail2FindSecureStart(secureMailObj, p, end - p,
        &inputRead, &pendingInputLength);
      p += inputRead;
    }
    
    if (secureMailObj->state == VOLT_SECURE_MAIL_STATE_READ_B64)
    {
      VOLT_SET_FNCT_LINE(fnctLine)
      status = VoltSecureMail2ReadBody(secureMailObj, p, end - p, &inputRead,
        q, remainingSize, outputDataLen);
      p += inputRead;
    }
    
    if (secureMailObj->state == VOLT_SECURE_MAIL_STATE_READ_FOOTER_1)
    {
      VOLT_SET_FNCT_LINE(fnctLine)
      status = VoltSecureMail2ReadSecureEnd(secureMailObj, p, end - p,
        &inputRead, &pendingInputLength);
      p += inputRead;
    }

    if (secureMailObj->state == VOLT_SECURE_MAIL_STATE_READ_COMMENT_END)
    {
      VOLT_SET_FNCT_LINE(fnctLine)
      status = VoltSecureMail2ReadCommentEnd(secureMailObj, p, end - p,
        &inputRead, &pendingInputLength);
      p += inputRead;
    }

    if (secureMailObj->state == VOLT_SECURE_MAIL_STATE_READ_FOOTER_2)
    {
      VOLT_SET_FNCT_LINE(fnctLine)
      status = VoltSecureMail2ReadMessageEnd(secureMailObj, p, end - p,
        &inputRead, &pendingInputLength);
      p += inputRead;
    }

    if (secureMailObj->state == VOLT_SECURE_MAIL_STATE_READ_COMPLETE)
    {
      p = end;
    }
    
    *bytesRead = messageLen - (end - p);
    
    if (status != 0)
      break;
      
    /* Update the pending input buffer */
    if (pendingInputLength > 0)
    {
      VT_ASSERT(readCtx->pendingInput == (unsigned char*)0);
      VT_ASSERT(readCtx->pendingInputLength == 0);
      
      readCtx->pendingInput = (unsigned char*) Z3Malloc(pendingInputLength);
      if (readCtx->pendingInput == (unsigned char*)0)
      {
        VOLT_SET_ERROR_TYPE(errorType, VT_ERROR_TYPE_PRIMARY)
        VOLT_SET_FNCT_LINE(fnctLine)
        status = VT_ERROR_MEMORY;
        break;
      }
      Z2Memcpy(readCtx->pendingInput,
        input + inputLength - pendingInputLength,
        pendingInputLength);
      readCtx->pendingInputLength = pendingInputLength;
    }
  }
  while (0);
  
  Z2Free(mergedInput);
  
  VOLT_LOG_ERROR_COMPARE(status, libCtx, status, errorType,
    fnctLine, "VoltSecureMail2ReadUpdate", (unsigned char*)0)
  
  return status;
}

int VoltSecureMail2ReadFinal (
  VtSecureMailObject secureMailObj,
  unsigned char *message,
  unsigned int messageLen,
  unsigned int *bytesRead,
  unsigned char *outputData,
  unsigned int bufferSize,
  unsigned int *outputDataLen
)
{
  int status = 0;
  VoltSecureMail2ReadCtx* readCtx = (VoltSecureMail2ReadCtx*)0;
  VtLibCtx libCtx = (VtLibCtx)0;
  unsigned char* mergedOutput = (unsigned char*)0;
  unsigned int mergedOutputLength;
  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
  {
    *bytesRead = 0;
    *outputDataLen = 0;
    
    VOLT_SET_FNCT_LINE(fnctLine)
    status = VoltSecureMail2ReadUpdate(secureMailObj, message,
      messageLen, bytesRead, outputData, bufferSize, outputDataLen);
    if (status != 0)
      break;
    
    /* This is the final call, so if we haven't reached the complete
     * state, then it wasn't a valid SecureMail message.
     */
    if (secureMailObj->state != VOLT_SECURE_MAIL_STATE_READ_COMPLETE)
    {
      VOLT_SET_FNCT_LINE(fnctLine)
      VOLT_SET_ERROR_TYPE(errorType, VT_ERROR_TYPE_PRIMARY)
      status = VT_ERROR_INVALID_SECURE_MAIL_MSG;
      break;
    }
    
    /* We've got to finish writing out everything with the ReadFinal
     * call (or else return VT_ERROR_BUFFER_TOO_SMALL), so if there's
     * any pending output after the update call (i.e. if the output
     * buffer passed in to ReadFinal wasn't big enough), then we
     * need to return VT_ERROR_BUFFER_TOO_SMALL. In that case we
     * expand the pending output buffer to include the data that
     * ReadUpdate put in the output buffer, set *outputDataLen to
     * zero, and return VT_ERROR_BUFFER_TOO_SMALL.
     */
    if (readCtx->pendingOutputLength > 0)
    {
      if (*outputDataLen > 0)
      {
        mergedOutputLength = *outputDataLen + readCtx->pendingOutputLength;
        mergedOutput = (unsigned char*) Z3Malloc(mergedOutputLength);
        if (mergedOutput == (unsigned char*)0)
        {
          VOLT_SET_FNCT_LINE(fnctLine)
          status = VT_ERROR_MEMORY;
          break;
        }
        Z2Memcpy(mergedOutput, outputData, *outputDataLen);
        Z2Memcpy(mergedOutput + *outputDataLen, readCtx->pendingOutput,
          readCtx->pendingOutputLength);
        Z2Free(readCtx->pendingOutput);
        readCtx->pendingOutput = mergedOutput;
        readCtx->pendingOutputLength = mergedOutputLength;
      }
      *outputDataLen = readCtx->pendingOutputLength;
      VOLT_SET_FNCT_LINE(fnctLine)
      VOLT_SET_ERROR_TYPE(errorType, VT_ERROR_TYPE_PRIMARY)
      status = VT_ERROR_BUFFER_TOO_SMALL;
    }
  }
  while (0);
  
  VOLT_LOG_ERROR_COMPARE(status, libCtx, status, errorType,
    fnctLine, "VoltSecureMail2ReadFinal", (unsigned char*)0)
  
  return status;
}

int VoltSecureMail2Verify (
  VtSecureMailObject secureMailObj,
  VtPolicyCtx policyCtx,
  VtStorageCtx storageCtx,
  VtTransportCtx transportCtx,
  VtCertVerifyCtx certVerifyCtx,
  Pointer verifyCtxInfo,
  VtVerifyFailureList vfyFailList,
  unsigned int *verifyResult
)
{
  int status;
  VtLibCtx libCtx;
  VOLT_DECLARE_FNCT_LINE(fnctLine)
  
  VT_ASSERT(secureMailObj != (VtSecureMailObject)0);
  
  libCtx = secureMailObj->voltObject.libraryCtx;
  
  VOLT_SET_FNCT_LINE(fnctLine)
  status = VtPkcs7VerifyAll(secureMailObj->p7SignedData,
    policyCtx, storageCtx, transportCtx, certVerifyCtx,
    verifyCtxInfo, vfyFailList, verifyResult);

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

⌨️ 快捷键说明

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