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

📄 tokentransport.c

📁 voltage 公司提供的一个开发Ibe的工具包
💻 C
📖 第 1 页 / 共 2 页
字号:
/* Copyright 2003-2005, 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"

static int getDistrictSecretAlloc (
    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;
    }

    /* Initially allocate space for 5 districts and secrets
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_MEMORY;
    secretArray = (VtTransportDelegatedInfo *)Z2Malloc (
      5 * sizeof (VtTransportDelegatedInfo *), VOLT_MEMORY_SENSITIVE);
    if (secretArray == (VtTransportDelegatedInfo *)0 )
      break;

    localCtx->secretList = (VtTransportDelegatedInfo **)secretArray;
    localCtx->listSize = 5;
    localCtx->numSecrets = 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;
  VtFileCtx fileCtx = (VtFileCtx)0;
  VtTransportDelegatedInfo *secretArray = (VtTransportDelegatedInfo *)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;

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

    /* Initially allocate space for 5 districts and secrets
    */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_MEMORY;
    secretArray = (VtTransportDelegatedInfo *)Z2Malloc (
      5 * sizeof (VtTransportDelegatedInfo *), VOLT_MEMORY_SENSITIVE);
    if (secretArray == (VtTransportDelegatedInfo *)0 )
      break;

    localCtx->secretList = (VtTransportDelegatedInfo **)secretArray;
    localCtx->listSize = 5;
    localCtx->numSecrets = 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 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,
    "VtTransportImplDelegatedWinINet", (char *)0)

  return (status);
}

#endif

int VtTransportParamDelegatedSecret (
   VtTransportCtx transportCtx,
   Pointer info,
   unsigned int flag
   )
{
  unsigned int status, i;
  unsigned char ch;
  unsigned int listSize, numSecrets;
  VoltTransportCtx *ctx = (VoltTransportCtx *)transportCtx;
  VoltLibCtx *libCtx = (VoltLibCtx *)(ctx->voltObject.libraryCtx);
  VoltDelegatedTransCtx *localCtx = (VoltDelegatedTransCtx *)ctx->localCtx;
  VtTransportDelegatedInfo *tokenInfo = (VtTransportDelegatedInfo *)0;
  VtTransportDelegatedInfo **secretArray = (VtTransportDelegatedInfo **)0;
  VtTransportDelegatedInfo *secretItem = (VtTransportDelegatedInfo *)0;
  unsigned char *district = (unsigned char *)0;
  unsigned char *secret = (unsigned char *)0;
  unsigned char *regExp = (unsigned char *)0;

  /* Currently regular expression is always *
   */
  unsigned char *currentRegExp = "*" ;
  unsigned int districtLen, secretLen, regExpLen; 
  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 VtTransportDelegatedInfo.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_ASSOCIATED_INFO;
    if (info == (Pointer)0)
      break;
    tokenInfo = (VtTransportDelegatedInfo *)info ;
    if (tokenInfo->qualDistName == (unsigned char *)0 )
      break;    
    if (tokenInfo->sharedSecret.data == (unsigned char *)0 ||
      tokenInfo->sharedSecret.len == 0 )
      break;
    districtLen = Z2Strlen (tokenInfo->qualDistName);
    regExpLen = Z2Strlen (currentRegExp);
    secretLen = tokenInfo->sharedSecret.len ;
    numSecrets = localCtx->numSecrets ;

    /* If we have no space left in the list we need to allocate more
     * 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 = (VtTransportDelegatedInfo **)Z2Malloc (
        listSize * sizeof (VtTransportDelegatedInfo *), 
        VOLT_MEMORY_SENSITIVE);
      if (secretArray == (VtTransportDelegatedInfo **)0 )
        break;
      Z2Memcpy (
        secretArray, localCtx->secretList,
        (localCtx->listSize) * sizeof (VtTransportDelegatedInfo *));
      Z2Free (localCtx->secretList);
      localCtx->secretList = secretArray;
      localCtx->listSize = listSize ;
    }

    /* Now allocate the memory to hold the district and 
     * shared secret info
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_MEMORY;
    secretItem = (VtTransportDelegatedInfo *) Z2Malloc (
      sizeof (VtTransportDelegatedInfo), VOLT_MEMORY_SENSITIVE);
    if (secretItem == (VtTransportDelegatedInfo *) 0 )
      break;

    VOLT_SET_FNCT_LINE (fnctLine)
    district = (unsigned char *) Z2Malloc (districtLen + 1, 0);
    if (district == (unsigned char *)0 )
      break;

    VOLT_SET_FNCT_LINE (fnctLine)
    regExp = (unsigned char *) Z2Malloc (regExpLen + 1 , 0);
    if (regExp == (unsigned char *)0 )
      break;

    VOLT_SET_FNCT_LINE (fnctLine)
    secret = (unsigned char *)Z2Malloc (secretLen , VOLT_MEMORY_SENSITIVE);
    if (secret == (unsigned char *) 0 )
      break;

    Z2Memcpy (district, tokenInfo->qualDistName, districtLen + 1);    
    Z2Memcpy (secret, tokenInfo->sharedSecret.data, secretLen);  

    /* When we implement regular expressions change the line below
     */
    Z2Memcpy (regExp , currentRegExp, regExpLen + 1);

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

      /* Anything after #is a number so don't waste time in converting
       * them to lowercase.
       */
      if (ch == '#')
        break;
      district[i] = Z2Tolower (ch);
    }

    /* set the values in secretItem
     */
    secretItem->qualDistName = district ;
    secretItem->regExp = regExp;
    secretItem->sharedSecret.data = secret ;
    secretItem->sharedSecret.len = secretLen ;

    /* Put this item in the list
     */
    localCtx->secretList[localCtx->numSecrets++] = secretItem;
    status = 0;

  } while (0);

  /* if successful return 
   */
  if (status == 0 )
    return 0;

  /* free all the memory we allocated and return status
   */
  if (secretArray != (VtTransportDelegatedInfo **)0 )
    Z2Free (secretArray);
  if (secretItem != (VtTransportDelegatedInfo *) 0 )
    Z2Free (secretItem);
  if (district != (unsigned char *)0 )
    Z2Free (district);
  if (regExp != (unsigned char *)0 )
    Z2Free (regExp);
  if (secret != (unsigned char *)0 )
    Z2Free (secret);

  VOLT_LOG_ERROR (
    (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
   )

⌨️ 快捷键说明

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