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

📄 smgenread.c

📁 IBE是一种非对称密码技术
💻 C
📖 第 1 页 / 共 2 页
字号:
  int status = 0;
  VoltSecureMailGenericReadCtx* readCtx = (VoltSecureMailGenericReadCtx*)0;
  VtLibCtx libCtx = (VtLibCtx)0;
  VtSecureMailObject targetImpl = (VtSecureMailObject)0;
  VoltSecureMailGenericImplInfo* implInfo;
  int implStatus;
  unsigned char* implMessage;
  unsigned int implMessageLength;
  unsigned int implBytesRead;
  unsigned int implOutputDataLen;
  unsigned char* mergedMessage = (unsigned char*)0;
  int succeeded = 0;
  unsigned int i;
  VOLT_DECLARE_FNCT_LINE(fnctLine)
  VOLT_DECLARE_ERROR_TYPE(errorType)
  
  VOLT_SET_ERROR_TYPE(errorType, 0)
  
  VT_ASSERT(secureMailObj != (VtSecureMailObject)0);
  VT_ASSERT(bytesRead != (unsigned int*)0);
  VT_ASSERT(outputDataLen != (unsigned int*)0);
  
  readCtx = (VoltSecureMailGenericReadCtx*)secureMailObj->localCtx;
  VT_ASSERT(readCtx != (VoltSecureMailGenericReadCtx*)0);
  libCtx = secureMailObj->voltObject.libraryCtx;
  VT_ASSERT(libCtx != (VtLibCtx)0);

  *bytesRead = messageLen;
  *outputDataLen = 0;
  
  do
  {
    /* There may be multiple valid impls remaining, so we iterate over the
     * impls and forward the update call to each of them. Since we can only
     * return a single bytesRead and outputDataLen value, we must make sure
     * there are no conflicts in the way the impls want to set these values.
     * For bytesRead we just buffer any unread input so that the generic
     * read impl always returns a bytesRead equal to messageLen.
     * For outputDataLen, there should only be one impl that gets to the
     * point where it starts to generate output, so there shouldn't be a
     * conflict (and if there is, then it's an error).
     */
    for (i = 0; i < readCtx->implCount; i++)
    {
      implInfo = &readCtx->implList[i];
      if (implInfo->secureMailObj != (VtSecureMailObject)0)
      {
        if (implInfo->bufferedMessageLength > 0)
        {
          VT_ASSERT(implInfo->bufferedMessage != (unsigned char*)0);
          VT_ASSERT(mergedMessage == (unsigned char*)0);
          
          implMessageLength = implInfo->bufferedMessageLength + messageLen;
          mergedMessage = Z3Malloc(implMessageLength);
          if (mergedMessage == (unsigned char*)0)
          {
            VOLT_SET_FNCT_LINE(fnctLine)
            VOLT_SET_ERROR_TYPE(errorType, VT_ERROR_TYPE_PRIMARY)
            status = VT_ERROR_MEMORY;
            break;
          }
          
          Z2Memcpy(mergedMessage, implInfo->bufferedMessage, implInfo->bufferedMessageLength);
          Z2Memcpy(mergedMessage + implInfo->bufferedMessageLength, message, messageLen);

          Z2Free(implInfo->bufferedMessage);
          implInfo->bufferedMessage = (unsigned char*)0;
          implInfo->bufferedMessageLength = 0;
          
          implMessage = mergedMessage;
        }
        else
        {
          implMessage = message;
          implMessageLength = messageLen;
        }

        VOLT_SET_FNCT_LINE(fnctLine)
        implStatus = VtSecureMailReadFinal(implInfo->secureMailObj,
          implMessage, implMessageLength, &implBytesRead, outputData,
          bufferSize, &implOutputDataLen);
        
        if ((implStatus == 0) || VoltIsNotFatalError(implStatus))
        {
          /* There should only be one impl that succeeds. */
          if (succeeded)
          {
            VOLT_SET_FNCT_LINE(fnctLine)
            VOLT_SET_ERROR_TYPE(errorType, VT_ERROR_TYPE_PRIMARY)
            status = VT_ERROR_INVALID_SECURE_MAIL_MSG;
            break;
          }
         
          if (implBytesRead < implMessageLength)
          {
            VT_ASSERT(implInfo->bufferedMessage == (unsigned char*)0);
            VT_ASSERT(implInfo->bufferedMessageLength == 0);
            implInfo->bufferedMessage = Z3Malloc(implMessageLength - implBytesRead);
            if (implInfo->bufferedMessage == (unsigned char*)0)
            {
              VOLT_SET_FNCT_LINE(fnctLine)
              VOLT_SET_ERROR_TYPE(errorType, VT_ERROR_TYPE_PRIMARY)
              status = VT_ERROR_MEMORY;
              break;
            }
            implInfo->bufferedMessageLength = implMessageLength - implBytesRead;
            Z2Memcpy(implInfo->bufferedMessage, implMessage + implBytesRead,
              implInfo->bufferedMessageLength);
          }

          *outputDataLen = implOutputDataLen;
          
          succeeded = 1;
          status = implStatus;
        }
        else
        {
          /* If the Final call failed, then just delete the object. */
          VtDestroySecureMailObject(&readCtx->implList[i].secureMailObj);
        }
        
        /* Free up merged message */
        Z2Free(mergedMessage);
        mergedMessage = (unsigned char*)0;
      }
    }
    
    if (!succeeded)
    {
      VOLT_SET_FNCT_LINE(fnctLine)
      VOLT_SET_ERROR_TYPE(errorType, VT_ERROR_TYPE_PRIMARY)
      status = VT_ERROR_INVALID_SECURE_MAIL_MSG;
      break;
    }
    
    if (status == 0)
      secureMailObj->state = VOLT_SECURE_MAIL_STATE_READ_COMPLETE;
  }
  while (0);

  Z2Free(mergedMessage);
  
  VOLT_LOG_ERROR_COMPARE(status, libCtx, status, errorType,
    fnctLine, "VoltSecureMailGenericReadFinal", (unsigned char*)0)
  
  return status;
}

static int VoltSecureMailGenericVerify (
  VtSecureMailObject secureMailObj,
  VtPolicyCtx policyCtx,
  VtStorageCtx storageCtx,
  VtTransportCtx transportCtx,
  VtCertVerifyCtx certVerifyCtx,
  Pointer verifyCtxInfo,
  VtVerifyFailureList vfyFailList,
  unsigned int *verifyResult
)
{
  int status = 0;
  VtLibCtx libCtx = (VtLibCtx)0;
  VtSecureMailObject targetImpl = (VtSecureMailObject)0;
  VOLT_DECLARE_FNCT_LINE(fnctLine)
  VOLT_DECLARE_ERROR_TYPE(errorType)
  
  VOLT_SET_ERROR_TYPE(errorType, 0)
  
  VT_ASSERT(secureMailObj != (VtSecureMailObject)0);
  
  libCtx = secureMailObj->voltObject.libraryCtx;
  VT_ASSERT(libCtx != (VtLibCtx)0);

  /* There should be only a single target impl at this point.
    * If not, then it's an error.
    */
  status = VoltGetTargetObjectForGenericSecureMailReader(
    secureMailObj, &targetImpl);
  if (status == 0)
  {
    VT_ASSERT(targetImpl != (VtSecureMailObject)0);
    
    VOLT_SET_FNCT_LINE(fnctLine)
    status = VtSecureMailVerify(targetImpl, policyCtx, storageCtx,
      transportCtx, certVerifyCtx, verifyCtxInfo, vfyFailList, verifyResult);
  }
  
  VOLT_LOG_ERROR_COMPARE(status, libCtx, status, 0,
    fnctLine, "VoltSecureMailGenericVerify", (unsigned char*)0)
  
  return status;
}

static void VoltSecureMailGenericReadLocalCtxDestroy(
  Pointer obj,
  Pointer ctx
)
{
  VoltObject* voltObj = (VoltObject*) obj;
  VtLibCtx libCtx;
  VoltSecureMailGenericReadCtx* readCtx = (VoltSecureMailGenericReadCtx*) ctx;
  VoltSecureMailGenericImplInfo* info;
  unsigned int i;
  
  if ((obj == (Pointer)0) || (ctx == (Pointer)0))
    return;

  libCtx = (VtLibCtx) voltObj->libraryCtx;

  for (i = 0; i < readCtx->implCount; i++)
  {
    info = &readCtx->implList[i];
    VtDestroySecureMailObject(&info->secureMailObj);
    Z2Free(info->bufferedMessage);
  }
  
  Z2Free(readCtx->implList);
  
  Z2Free(readCtx);
}

int VtSecureMailImplReadGeneric(
   VtSecureMailObject *object,
   Pointer info,
   unsigned int flag
   )
{
  int status = 0;
  VtLibCtx libCtx = (VtLibCtx)0;
  VtSecureMailObject smObj;
  VoltSecureMailGenericReadCtx* readCtx;
  unsigned int implListSize;
  unsigned int i;
  VOLT_DECLARE_FNCT_LINE(fnctLine)
  VOLT_DECLARE_ERROR_TYPE(errorType)

  VOLT_SET_ERROR_TYPE(errorType, VT_ERROR_TYPE_PRIMARY)

  do
  {
    if ((object == (VtSecureMailObject*)0) ||
        (*object == (VtSecureMailObject)0))
    {
      VOLT_SET_FNCT_LINE(fnctLine)
      status = VT_ERROR_INVALID_SECURE_MAIL_OBJ;
      break;
    }
    
    smObj = *object;
    libCtx = smObj->voltObject.libraryCtx;
    VT_ASSERT(libCtx != (VtLibCtx)0);
  
    /* Make sure it's being called correctly */
    if (flag != VOLT_SECURE_MAIL_SET_TYPE_FLAG)
    {
      VOLT_SET_FNCT_LINE(fnctLine)
      status = VT_ERROR_INVALID_TYPE;
      break;
    }
    
    /* Make sure the object is empty. */
    if ((smObj->state != 0) || (smObj->localCtx != (Pointer)0))
    {
      VOLT_SET_FNCT_LINE(fnctLine)
      status = VT_ERROR_INVALID_SECURE_MAIL_OBJ;
      break;
    }
    
    /* Allocate the local context */
    readCtx = (VoltSecureMailGenericReadCtx*)
      Z3Malloc(sizeof(VoltSecureMailGenericReadCtx));
    if (readCtx == (VoltSecureMailGenericReadCtx*)0)
    {
      VOLT_SET_FNCT_LINE(fnctLine)
      status = VT_ERROR_MEMORY;
      break;
    }
    
    Z2Memset(readCtx, 0, sizeof(VoltSecureMailGenericReadCtx));

    readCtx->implCount = sizeof(VoltSecureMailImplList) /
      sizeof(VoltSecureMailImplList[0]);
    implListSize = readCtx->implCount * sizeof(VoltSecureMailGenericImplInfo);
    readCtx->implList = (VoltSecureMailGenericImplInfo*) Z3Malloc(implListSize);
    if (readCtx->implList == (VoltSecureMailGenericImplInfo*)0)
    {
      VOLT_SET_FNCT_LINE(fnctLine)
      status = VT_ERROR_MEMORY;
      break;
    }
    Z2Memset(readCtx->implList, 0, implListSize);
    
    for (i = 0; i < readCtx->implCount; i++)
    {
      VOLT_SET_FNCT_LINE(fnctLine)
      status = VtCreateSecureMailObject(libCtx, VoltSecureMailImplList[i],
        info, &readCtx->implList[i].secureMailObj);
      if (status != 0)
        break;
    }
    
    if (status != 0)
      break;
      
    smObj->formatType = VOLT_MESSAGE_FORMAT_SECURE_MAIL_GENERIC;
    smObj->state = VOLT_SECURE_MAIL_STATE_READ_SET;
    smObj->localCtx = (Pointer)readCtx;
    smObj->LocalCtxDestroy = VoltSecureMailGenericReadLocalCtxDestroy;
    smObj->ReadInit = VoltSecureMailGenericReadInit;
    smObj->ReadUpdate = VoltSecureMailGenericReadUpdate;
    smObj->ReadFinal = VoltSecureMailGenericReadFinal;
    smObj->Verify = VoltSecureMailGenericVerify;
  }
  while (0);

  if (status != 0)
  {
    VoltSecureMailGenericReadLocalCtxDestroy((Pointer)smObj, (Pointer)readCtx);

    VOLT_LOG_ERROR(libCtx, status, errorType,
      fnctLine, "VtSecureMailGenericImplRead", (char *)0)
  }
  
  return status;
}

⌨️ 快捷键说明

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