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

📄 tokentransport.c

📁 IBE是一种非对称密码技术
💻 C
📖 第 1 页 / 共 2 页
字号:
/* Copyright 2003-2006, Voltage Security, all rights reserved.
 */
#include "vibe.h"
#include "environment.h"
#include "base.h"
#include "libctx.h"
#include "deftrans.h"
#include "vsdistrict.h"
#include "idobj.h"
#include "hmac.h"
#include "distobj.h"
#include "certobj.h"
#include "vstorage.h"
#include "defaultstore.h"
#include "derhelp.h"
#include "oidlist.h"
#include "ibe.h"
#include "ibekeyber.h"
#include "emailschema.h"
#include "tokentrans.h"
#include "icstringutils.h"
#include "surrender.h"
#include "errorctx.h"

/* This function assumes distQualName is not NULL, and that it points
 * to a NULL-terminated string of length > 1 (length not including the
 * NULL terminator).
 */
static int VOLT_CALLING_CONV GetDistrictSecretAlloc VOLT_PROTO_LIST ((
   VoltTransportCtx *transCtx, 
   unsigned char *distQualName, 
   unsigned char *email,
   unsigned char **secret,
   unsigned int *outLen
   ));

#if VOLT_OS != VOLT_WINDOWS_32

#include <ctype.h>

int VtTransportImplDelegatedCurl (
   VtTransportCtx *transportCtx,
   Pointer info,
   unsigned int flag
   )
{
  int status;
  unsigned int bufferSize, offset, pathLen;  
  VoltTransportCtx *ctx = (VoltTransportCtx *)(*transportCtx);
  VoltLibCtx *libCtx = (VoltLibCtx *)(ctx->voltObject.libraryCtx);  
  VoltDelegatedTransCtx *localCtx = (VoltDelegatedTransCtx *)0;
  VtTransportDelegatedInfo *secretArray = (VtTransportDelegatedInfo *)0;
  char *trustStore = (char *)0;
  unsigned char *buffer = (unsigned char *)0;
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    /* Check the flag, it should be VOLT_OBJECT_TYPE_TRANSPORT_CTX.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_TYPE;
    if (flag != VOLT_TRANSPORT_CTX_SET_TYPE_FLAG)
      break; 

    /* The associated info should be a pointer to the trust store.
     */
    pathLen = 0;
    if (info != (Pointer)0)
    {
      trustStore = (char *)info;
      pathLen = Z2Strlen (trustStore);
    }

    /* Allocate space for a local ctx.
     */
    bufferSize = sizeof (VoltDelegatedTransCtx) + pathLen + 1;

    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_MEMORY;
    buffer = (unsigned char *)Z2Malloc (bufferSize, 0);    
    if (buffer == (unsigned char *)0)
      break;
    Z2Memset (buffer, 0, bufferSize);        
    localCtx = (VoltDelegatedTransCtx *)buffer;

    /* Fill in the context fields.
     */    
    offset = sizeof (VoltDelegatedTransCtx);    
    localCtx->defCtx.DoHttp = mDoHTTP;    

    if (trustStore != (char *)0 )
    {
      localCtx->defCtx.trustStore = (char *)(buffer + offset);    
      Z2Memcpy (localCtx->defCtx.trustStore, trustStore, pathLen);
      localCtx->defCtx.trustStore[pathLen] = 0;
    }

    ctx->flag = VOLT_TRANSPORT_FLAG_DELEGATED;
    ctx->timeout = 10000;
    ctx->localCtx = (Pointer)localCtx;
    ctx->LocalCtxDestroy = LocalTokenTransCtxDestroy;
    ctx->DownloadParameters = VoltDefaultDownloadParameters;
    ctx->DownloadKeyAndCert = VoltTokenDownloadKeyAndCert;    
    
    status = 0;

  } while (0);  

  /* If successful, we're done.
   */
  if (status == 0)
    return (0);

  /* If there was an error, destroy what we created.
   */  
  if (buffer != (unsigned char *)0 )
    Z2Free (buffer);
  if (secretArray != (VtTransportDelegatedInfo *)0 )
    Z2Free (secretArray);

  VOLT_LOG_ERROR (
    (VtLibCtx)libCtx, status, VT_ERROR_TYPE_PRIMARY, fnctLine,
    "VtTransportImplDelegatedCurl", (char *)0)

  return (status);
}

#endif

#if VOLT_OS == VOLT_WINDOWS_32

int VtTransportImplDelegatedWinINet (
   VtTransportCtx *transportCtx,
   Pointer info,
   unsigned int flag
   )
{
  int status;
  unsigned int bufferSize;
  VoltTransportCtx *ctx = (VoltTransportCtx *)(*transportCtx);
  VoltLibCtx *libCtx = (VoltLibCtx *)(ctx->voltObject.libraryCtx);
  VoltDelegatedTransCtx *localCtx = (VoltDelegatedTransCtx *)0;
  unsigned char *buffer= (unsigned char *)0;
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    /* Check the flag, it should be VOLT_OBJECT_TYPE_TRANSPORT_CTX.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_TYPE;
    if (flag != VOLT_TRANSPORT_CTX_SET_TYPE_FLAG)
      break; 

    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_ASSOCIATED_INFO;
    if (info != (Pointer)0)
      break;

    bufferSize = sizeof (VoltDelegatedTransCtx);

    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_MEMORY;
    buffer = (unsigned char *)Z2Malloc (bufferSize, 0);    
    if (buffer == (unsigned char *)0)
      break;
    Z2Memset (buffer, 0, bufferSize);        
    localCtx = (VoltDelegatedTransCtx *)buffer;
    buffer = (unsigned char *)0;

    /* Fill in the context fields.
     */            
    localCtx->defCtx.DoHttp = mDoHTTP;    

    ctx->flag = VOLT_TRANSPORT_FLAG_DELEGATED;
    ctx->timeout = 10000;    
    ctx->localCtx = (Pointer)localCtx;
    ctx->LocalCtxDestroy = LocalTokenTransCtxDestroy;
    ctx->DownloadParameters = VoltDefaultDownloadParameters;
    ctx->DownloadKeyAndCert = VoltTokenDownloadKeyAndCert;    
   
    status = 0;

  } while (0);  

  if (buffer != (unsigned char *)0 )
    Z2Free (buffer);

  VOLT_LOG_ERROR_COMPARE (
    status, (VtLibCtx)libCtx, status, VT_ERROR_TYPE_PRIMARY, fnctLine,
    "VtTransportImplDelegatedWinINet", (char *)0)

  return (status);
}

int VtTransportImplDelegatedWinHttp (
   VtTransportCtx *transportCtx,
   Pointer info,
   unsigned int flag
   )
{
  int status;
  unsigned int bufferSize;
  VoltTransportCtx *ctx = (VoltTransportCtx *)(*transportCtx);
  VoltLibCtx *libCtx = (VoltLibCtx *)(ctx->voltObject.libraryCtx);
  VoltDelegatedTransCtx *localCtx = (VoltDelegatedTransCtx *)0;
  unsigned char *buffer= (unsigned char *)0;
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    /* Check the flag, it should be VOLT_OBJECT_TYPE_TRANSPORT_CTX.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_TYPE;
    if (flag != VOLT_TRANSPORT_CTX_SET_TYPE_FLAG)
      break; 

    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_ASSOCIATED_INFO;
    if (info != (Pointer)0)
      break;

    bufferSize = sizeof (VoltDelegatedTransCtx);

    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_MEMORY;
    buffer = (unsigned char *)Z2Malloc (bufferSize, 0);    
    if (buffer == (unsigned char *)0)
      break;
    Z2Memset (buffer, 0, bufferSize);        
    localCtx = (VoltDelegatedTransCtx *)buffer;
    buffer = (unsigned char *)0;

    /* Fill in the context fields.
     */            
    localCtx->defCtx.DoHttp = winHttpDoHTTP;

    ctx->flag = VOLT_TRANSPORT_FLAG_DELEGATED;
    ctx->timeout = 10000;    
    ctx->localCtx = (Pointer)localCtx;
    ctx->LocalCtxDestroy = LocalTokenTransCtxDestroy;
    ctx->DownloadParameters = VoltDefaultDownloadParameters;
    ctx->DownloadKeyAndCert = VoltTokenDownloadKeyAndCert;    

    status = 0;

  } while (0);  

  if (buffer != (unsigned char *)0 )
    Z2Free (buffer);

  VOLT_LOG_ERROR_COMPARE (
    status, (VtLibCtx)libCtx, status, VT_ERROR_TYPE_PRIMARY, fnctLine,
    "VtTransportImplDelegatedWinHttp", (char *)0)

  return (status);
}

#endif

int VtTransportParamDelegatedSecret (
   VtTransportCtx transportCtx,
   Pointer info,
   unsigned int flag
   )
{
  int status;
  unsigned int index, listSize, districtLen, regExpLen; 
  unsigned char currentChar;
  unsigned char *district;
  VoltTransportCtx *ctx = (VoltTransportCtx *)transportCtx;
  VoltLibCtx *libCtx = (VoltLibCtx *)(ctx->voltObject.libraryCtx);
  VoltDelegatedTransCtx *localCtx = (VoltDelegatedTransCtx *)(ctx->localCtx);
  VtDelegatedSecretInfo *inputInfo;
  VtDelegatedSecretInfo **secretArray = (VtDelegatedSecretInfo **)0;
  /* Currently regular expression is always *
   */
  unsigned char *currentRegExp = (unsigned char *)"*" ;
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do 
  {
    /* This TransportParam only does the set operation
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_GET_INFO_UNAVAILABLE;
    if (flag == VOLT_TRANSPORT_CTX_GET_TYPE_FLAG)
      break;

    /* Check the flag, it should be VOLT_TRANSPORT_SET_TYPE_FLAG.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_TYPE;
    if (flag != VOLT_TRANSPORT_CTX_SET_TYPE_FLAG)
      break;

    /* This can only set a transport ctx with the DELEGATED bit in the
     * flag set.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_SET;
    if ((ctx->flag & VOLT_TRANSPORT_FLAG_DELEGATED) == 0)
      break;

    /* The associated info should be a pointer to VtDelegatedSecretInfo.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_ASSOCIATED_INFO;
    if (info == (Pointer)0)
      break;

    inputInfo = (VtDelegatedSecretInfo *)info;

    VOLT_SET_FNCT_LINE (fnctLine)
    if (inputInfo->districtName == (unsigned char *)0)
      break;    
    if ( (inputInfo->sharedSecret.data == (unsigned char *)0) ||
         (inputInfo->sharedSecret.len == 0) )
      break;

    if (inputInfo->regularExpression != (unsigned char *)0)
      currentRegExp = inputInfo->regularExpression;

    districtLen = Z2Strlen (inputInfo->districtName);
    regExpLen = Z2Strlen (currentRegExp);

    /* This is the index of the next entry in the list.
     */
    index = localCtx->numSecrets;

    /* If we have no space left in the list we need to allocate more
     * space to hold the secrets
     */
    if (localCtx->numSecrets >= localCtx->listSize)
    {
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VT_ERROR_MEMORY;
      listSize = localCtx->listSize + 5 ;
      secretArray = (VtDelegatedSecretInfo **)Z2Malloc (
        listSize * sizeof (VtDelegatedSecretInfo *), 0);
      if (secretArray == (VtDelegatedSecretInfo **)0 )
        break;
      Z2Memset (secretArray, 0, listSize * sizeof (VtDelegatedSecretInfo *));

      /* Copy old into new.
       */
      for (index = 0; index < localCtx->listSize; ++index)
        secretArray[index] = localCtx->secretList[index];

      /* Delete the old.
       */
      Z2Free (localCtx->secretList);

      /* Store the new.
       */
      localCtx->secretList = secretArray;
      localCtx->listSize = listSize;
      index = localCtx->numSecrets;
      secretArray = (VtDelegatedSecretInfo **)0;
    }

    /* Build the new VtDelegatedSecretInfo and copy in the inputInfo.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_MEMORY;
    localCtx->secretList[index] = (VtDelegatedSecretInfo *)Z2Malloc (
      sizeof (VtDelegatedSecretInfo), 0);
    if (localCtx->secretList[index] == (VtDelegatedSecretInfo *)0)
      break;
    Z2Memset (localCtx->secretList[index], 0, sizeof (VtDelegatedSecretInfo));

    localCtx->numSecrets++;

    VOLT_SET_FNCT_LINE (fnctLine)
    localCtx->secretList[index]->districtName = (unsigned char *)Z2Malloc (
      districtLen + 1, 0);
    if (localCtx->secretList[index]->districtName == (unsigned char *)0)
      break;
    Z2Memcpy (
      localCtx->secretList[index]->districtName, inputInfo->districtName,
      districtLen + 1);

    district = localCtx->secretList[index]->districtName;

    VOLT_SET_FNCT_LINE (fnctLine)
    localCtx->secretList[index]->regularExpression =
      (unsigned char *)Z2Malloc (regExpLen + 1, 0);
    if (localCtx->secretList[index]->regularExpression == (unsigned char *)0)
      break;
    Z2Memcpy (
      localCtx->secretList[index]->regularExpression,
      inputInfo->regularExpression, regExpLen + 1);

    VOLT_SET_FNCT_LINE (fnctLine)
    localCtx->secretList[index]->sharedSecret.data =
      (unsigned char *)Z2Malloc (
      inputInfo->sharedSecret.len, VOLT_MEMORY_SENSITIVE);
    if (localCtx->secretList[index]->sharedSecret.data == (unsigned char *)0)
      break;
    Z2Memcpy (
      localCtx->secretList[index]->sharedSecret.data,
      inputInfo->sharedSecret.data, inputInfo->sharedSecret.len);
    localCtx->secretList[index]->sharedSecret.len =
      inputInfo->sharedSecret.len;

    /* District names have to be lowercase
     */
    for (index = 0; index < districtLen; ++index)
    {
      currentChar = district[index];

      /* Anything after # is a number so no need to convert to lower
       * case.
       */
      if (currentChar == '#')
        break;

      district[index] = Z2Tolower (currentChar);
    }

    status = 0;

  } while (0);

  /* free all the memory we allocated and return status
   */
  if (secretArray != (VtDelegatedSecretInfo **)0 )
    Z2Free (secretArray);

  VOLT_LOG_ERROR_COMPARE (
    status, (VtLibCtx)libCtx, status, VT_ERROR_TYPE_PRIMARY, fnctLine,
    "VtTransportParamDelegatedSecret", (char *)0)

  return (status) ;  
}

int VoltTokenDownloadKeyAndCert (
   VtTransportCtx transportCtx,
   VtIdentityObject identity,
   VtPolicyCtx policyCtx,
   VtStorageCtx storageCtx,
   VtCertRequestObject certRequest,
   VtCertObject signingCert,
   VtKeyObject ibePriKey,
   Pointer appSpecificData
   )
{
  int status ;
  unsigned int districtNameLen, index;

⌨️ 快捷键说明

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