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

📄 clientstore.c

📁 voltage 公司提供的一个开发Ibe的工具包
💻 C
📖 第 1 页 / 共 4 页
字号:
/* Copyright 2003-2005, Voltage Security, all rights reserved.
 */
#include "vibe.h"
#include "environment.h"
#include "base.h"
#include "libctx.h"
#include "vstorage.h"
#include "defaultstore.h"
#include "idobj.h"
#include "keyobj.h"
#include "ibe.h"
#include "certobj.h"
#include "ibekeyber.h"
#include "prikeyder.h"
#include "distobj.h"
#include "vsdistrict.h"
#include "vsstore.h"
#include "voltfile.h"
#include "vtime.h"

#if VOLT_OS == VOLT_WINDOWS_32
#if VOLT_COMPILER != VOLT_MS_EVC_4_0

#include <windows.h>
#include <tchar.h>
/* Set up the ASN.1 structures for reading a DSA private key in the
 * form used by the original Client software.
 *   SEQUENCE {
 *     INTEGER  version,
 *     INTEGER  prime,
 *     INTEGER  subprime,
 *     INTEGER  base,
 *     INTEGER  pubVal,
 *     INTEGER  priVal
 */
typedef struct
{
  ASN1_INTEGER *version;
  ASN1_INTEGER *prime;
  ASN1_INTEGER *subprime;
  ASN1_INTEGER *base;
  ASN1_INTEGER *pubVal;
  ASN1_INTEGER *priVal;
} Asn1ClientDSAPriKey;

DECLARE_ASN1_FUNCTIONS (Asn1ClientDSAPriKey)

ASN1_SEQUENCE (Asn1ClientDSAPriKey) =
{
  ASN1_SIMPLE (Asn1ClientDSAPriKey, version, ASN1_INTEGER),
  ASN1_SIMPLE (Asn1ClientDSAPriKey, prime, ASN1_INTEGER),
  ASN1_SIMPLE (Asn1ClientDSAPriKey, subprime, ASN1_INTEGER),
  ASN1_SIMPLE (Asn1ClientDSAPriKey, base, ASN1_INTEGER),
  ASN1_SIMPLE (Asn1ClientDSAPriKey, pubVal, ASN1_INTEGER),
  ASN1_SIMPLE (Asn1ClientDSAPriKey, priVal, ASN1_INTEGER)
} ASN1_SEQUENCE_END (Asn1ClientDSAPriKey);

IMPLEMENT_ASN1_FUNCTIONS (Asn1ClientDSAPriKey)

int VoltClientStoreIBEPrivateKey (
   VtStorageCtx storageCtx,
   VtIdentityObject reference,
   VtKeyObject entry
   )
{
  int status, fileNameLen, passwordLen;
  unsigned int encodingLen;
  unsigned char *buffer = (unsigned char *)0;
  unsigned char *temp;
  VoltStorageCtx *ctx = (VoltStorageCtx *)storageCtx;
  VoltLibCtx *libCtx = (VoltLibCtx *)(ctx->voltObject.libraryCtx);
  VoltIdentityObject *idObj = (VoltIdentityObject *)reference;
  VoltKeyObject *keyObj = (VoltKeyObject *)entry;
  VoltFileCtx *fileCtx = (VoltFileCtx *)0;
  unsigned char *fileName = (unsigned char *)0;
  unsigned char *password = (unsigned char *)0;
  VoltFileHandle fileHandle = (VoltFileHandle)0;
  VoltDefaultStorageCtx *defStorageCtx;
  VoltBFType1IBEPriKeyData *keyData;
  Asn1IBEPrivateKey *asn1Key = (Asn1IBEPrivateKey *)0;
  unsigned char keyOid[VoltIBEPriKeyForm1OidBytesLen] =
    { VoltIBEPriKeyForm1OidBytes };

  /* Get the File Ctx
   */
  defStorageCtx = (VoltDefaultStorageCtx *)ctx->localStorageCtx;
  fileCtx = (VoltFileCtx *)defStorageCtx->fCtx;

  do
  {
    /* We need the identity encoded.
     */
    status = VT_ERROR_INVALID_STORAGE_REF;
    if (idObj->encoding.data == (unsigned char *)0)
      break;

    /* We want the IBE private key data, not the P8 full encoding.
     * First, get the key data.
     */
    keyData = (VoltBFType1IBEPriKeyData *)(keyObj->keyData);
    if ((keyObj->keyType & VOLT_KEY_TYPE_MASK_DATA) != VOLT_KEY_TYPE_DATA)
    {
      if (keyObj->GetKeyData == (VGetKeyData)0)
        break;

      status = keyObj->GetKeyData ((VtKeyObject)keyObj, (Pointer *)&keyData);
      if (status != 0)
        break;
    }

    /* Create the template for encoding the key following version 1.
     */
    status = VT_ERROR_MEMORY;
    asn1Key = Asn1IBEPrivateKey_new ();
    if (asn1Key == (Asn1IBEPrivateKey *)0)
      break;

    /* Set the fields.
     */
    if (Asn1ObjectId_set (
      asn1Key->privateData->format, keyOid,
      VoltIBEPriKeyForm1OidBytesLen) != 1)
      break;

    if (ASN1_OCTET_STRING_set (
      asn1Key->privateData->value, keyData->keyInfo.privatePoint.yCoord.data,
      keyData->keyInfo.privatePoint.yCoord.len) != 1)
      break;

    /* Because it's optional, we have to create the pubKey.
     */
    asn1Key->pubKey = Asn1Encoded_new ();
    if (asn1Key->pubKey == (Asn1Encoded *)0)
      break;

    if (Asn1Encoded_set (
      asn1Key->pubKey, keyData->keyInfo.encodedId.data,
      keyData->keyInfo.encodedId.len) != 1)
      break;

    /* Call encode with no buffer to get the appropriate size.
     */
    status = VT_ERROR_INVALID_INPUT;
    encodingLen = i2d_Asn1IBEPrivateKey (asn1Key, (unsigned char **)0);
    if (encodingLen == 0)
      break;

    status = VT_ERROR_MEMORY;
    Z2Free (buffer);
    buffer = (unsigned char *)Z2Malloc (encodingLen, VOLT_MEMORY_SENSITIVE);
    if (buffer == (unsigned char *)0)
      break;

    /* Now encode into the buffer.
     */
    status = VT_ERROR_INVALID_INPUT;
    temp = buffer;
    encodingLen = i2d_Asn1IBEPrivateKey (asn1Key, &temp);
    if (encodingLen == 0)
      break;

    /* Get the file name to store the encoded private key info.
     */
    status = VoltGetFileNameFromValueAlloc (
      ctx, (unsigned char *)0, 0, idObj->encoding.data, idObj->encoding.len,
      VOLT_FILE_NAME_VALUE_TYPE_IBE_PRI_CLIENT, &fileName, &fileNameLen);
    if (status != 0)
      break;

    /* Make sure the directories exist.
     */
    status = fileCtx->CtxCreateDirectories(fileCtx, fileName);
    if (status != 0)
      break;

    /* Open this file to write. If it exists, destory the contents.
     */
    status = fileCtx->CtxOpenFile(
      fileCtx, &fileHandle, fileName, VOLT_FILE_MODE_READ_OVERWRITE, 0600);
    if (status != 0)
      break;
    
    /* Get the password if set
    */
    if (defStorageCtx->GetExtraPassword != (VGetExtraPassword)0)
    {
      status = defStorageCtx->GetExtraPassword (ctx, &password, &passwordLen);
      if (status != 0)
        break;
    }

    /* Store the encoding.
     */
    status = mIcStoreData (
      ctx, buffer, encodingLen, password, passwordLen, fileCtx, fileHandle);
    if (status != 0)
      break;

    /* Store the identity in "idList" file
     */
    status = VoltAddIdentityToIdList (storageCtx, reference);

  } while (0);

  if (password != (unsigned char *)0)
    defStorageCtx->ReleaseExtraPassword (ctx, &password, &passwordLen);
  if (fileHandle != (VoltFileHandle)0)
    fileCtx->CtxCloseFile(fileCtx, &fileHandle);
  if (fileName != (unsigned char *)0)
    Z2Free (fileName);
  if (asn1Key != (Asn1IBEPrivateKey *)0)
    Asn1IBEPrivateKey_free (asn1Key);
  if (buffer != (unsigned char *)0)
    Z2Free (buffer);

  return (status);
}

int VoltClientStorePrivateSigningKey (
   VtStorageCtx storageCtx,
   VtIdentityObject reference,
   VtKeyObject keyObj
   )
{
  int status;
  unsigned int fileNameLen, contentsLen, passwordLen;
  VoltStorageCtx *ctx = (VoltStorageCtx *)storageCtx;
  VoltLibCtx *libCtx = (VoltLibCtx *)(ctx->voltObject.libraryCtx);
  VoltIdentityObject *idObj = (VoltIdentityObject *)reference;
  VoltFileCtx *fileCtx = (VoltFileCtx *)0;
  VoltFileHandle fileHandle = (VoltFileHandle)0;
  VoltDefaultStorageCtx *defStorageCtx;
  unsigned char *password = (unsigned char *)0;
  unsigned char *fileName = (unsigned char *)0;
  unsigned char *contents = (unsigned char *)0;
  unsigned char *temp;
  VtDSAPriKeyInfo *keyInfo;
  Asn1ClientDSAPriKey *clientDsaKey = (Asn1ClientDSAPriKey *)0;

  /* This implementation stores DSA private keys only.
   */
  /* Get the File Ctx
   */
  defStorageCtx = (VoltDefaultStorageCtx *)ctx->localStorageCtx;
  fileCtx = (VoltFileCtx *)defStorageCtx->fCtx;

  do
  {
    /* We need the identity encoded.
     */
    status = VT_ERROR_INVALID_STORAGE_REF;
    if (idObj->encoding.data == (unsigned char *)0)
      break;

    /* Get the key data.
     * Note that this implementation only deals with DSA keys.
     */
    status = VtGetKeyParam (
      keyObj, VtKeyParamDSAPrivate, (Pointer *)&keyInfo);
    if (status != 0)
      break;

    /* The data to store will be the Client DER of the private key.
     * First, how big does the buffer need to be?
     */
    status = VT_ERROR_MEMORY;
    clientDsaKey = Asn1ClientDSAPriKey_new ();
    if (clientDsaKey == (Asn1ClientDSAPriKey *)0)
      break;

    /* Set the fields.
     */
    if (ASN1_INTEGER_set (clientDsaKey->version, 0) != 1)
      break;
    if (ASN1_STRING_set (
      clientDsaKey->prime, keyInfo->primeP.data, keyInfo->primeP.len) != 1)
      break;
    if (ASN1_STRING_set (
      clientDsaKey->subprime, keyInfo->subprimeQ.data,
      keyInfo->subprimeQ.len) != 1)
      break;
    if (ASN1_STRING_set (
      clientDsaKey->base, keyInfo->baseG.data, keyInfo->baseG.len) != 1)
      break;
    if (ASN1_STRING_set (
      clientDsaKey->pubVal, keyInfo->pubValY.data, keyInfo->pubValY.len) != 1)
      break;
    if (ASN1_STRING_set (
      clientDsaKey->priVal, keyInfo->priValX.data, keyInfo->priValX.len) != 1)
      break;

    /* How big does the buffer need to be?
     */
    status = VT_ERROR_INVALID_INPUT;
    contentsLen = i2d_Asn1ClientDSAPriKey (clientDsaKey, (unsigned char **)0);
    if (contentsLen == 0)
      break;

    /* Allocate the space.
     */
    status = VT_ERROR_MEMORY;
    contents = (unsigned char *)Z2Malloc (contentsLen, VOLT_MEMORY_SENSITIVE);
    if (contents == (unsigned char *)0)
      break;

    /* Encode into the buffer.
     */
    status = VT_ERROR_INVALID_INPUT;
    temp = contents;
    contentsLen = i2d_Asn1ClientDSAPriKey (clientDsaKey, &temp);
    if (contentsLen == 0)
      break;

    /* Get the file name.
     */
    status = VoltGetFileNameFromValueAlloc (
      ctx, (unsigned char *)0, 0, idObj->encoding.data, idObj->encoding.len,
      VOLT_FILE_NAME_VALUE_TYPE_SIGN_PRI_CLIENT, &fileName, &fileNameLen);
    if (status != 0)
      break;

    /* Make sure the directories exist.
     */
    status = fileCtx->CtxCreateDirectories (fileCtx, fileName);
    if (status != 0)
      break;

    /* Open this file to write. If it exists, destory the contents.
     */
    status = fileCtx->CtxOpenFile (
      fileCtx, &fileHandle, fileName, VOLT_FILE_MODE_READ_OVERWRITE, 0600);
    if (status != 0)
      break;

    /* Get the client storage password if any
     */
    if (defStorageCtx->GetExtraPassword != (VGetExtraPassword)0)
    {
      status = defStorageCtx->GetExtraPassword (ctx, &password, &passwordLen);
      if (status != 0)
        break;
    }

    /* Store the encoding.
     */
    status = mIcStoreData (
      ctx, contents, contentsLen, password, passwordLen, fileCtx, fileHandle);

  } while (0);

  if (password != (unsigned char *)0)
    defStorageCtx->ReleaseExtraPassword (ctx, &password, &passwordLen);

  if (fileHandle != (VoltFileHandle)0)
    fileCtx->CtxCloseFile (fileCtx, &fileHandle);

  if (clientDsaKey != (Asn1ClientDSAPriKey *)0)
    Asn1ClientDSAPriKey_free (clientDsaKey);

  if (contents != (unsigned char *)0)
    Z2Free (contents);
  if (fileName != (unsigned char *)0)
    Z2Free (fileName);

  return (status);
}

int VoltClientStoreDistrictParameters (
   VtStorageCtx storageCtx,
   VtTime *storeTime,
   VtDistrictObject entry
   )
{
  HKEY paramKey = (HKEY)0;
  HKEY updateKey = (HKEY)0;
  int status, ret;
  unsigned int paramsTextLen;
  VoltStorageCtx *ctx = (VoltStorageCtx *)storageCtx;
  VoltLibCtx *libCtx = (VoltLibCtx *)(ctx->voltObject.libraryCtx);
  VoltDistrictObject *distObj = (VoltDistrictObject *)entry;
  VoltFileCtx *fileCtx = (VoltFileCtx *)0;
  VoltFileHandle fileHandle = (VoltFileHandle)0;
  VoltDefaultStorageCtx *defStorageCtx;
  unsigned char *fileName = (unsigned char *)0;
  unsigned char *password = (unsigned char *)0;
  unsigned char *contents = (unsigned char *)0;
  VtTime *theTime;
  VtTime currentTime;
  VoltTime vTime;

  /* Get the File Ctx
  */
  defStorageCtx = (VoltDefaultStorageCtx *)ctx->localStorageCtx;
  fileCtx = (VoltFileCtx *)defStorageCtx->fCtx;
  theTime = storeTime;

  do
  {
    if (theTime == (VtTime *)0 )
    {
      status = VtGetTime ((VtLibCtx)libCtx, &currentTime);
      if (status != 0)
        break;

      theTime = &currentTime;
    }

    /* We need the district data as the standard params text.
     */
    status = VT_ERROR_ENTRY_NOT_STORED;
    if (distObj->paramsText == (unsigned char *)0)
      break;

    paramsTextLen = Z2Strlen (distObj->paramsText);

    /* Make sure the district object contains a qualified name.
     */
    status = VT_ERROR_INVALID_STORAGE_REF;
    if (distObj->qualDistrictName.data == (unsigned char *)0)
      break;

    /* Get the time as seconds to store in the registry
     */
    status = VoltConvertTimeToSeconds (theTime, &vTime);
    if (status != 0)
      break;

    /* Open the registry keys to store params and param updates time
     */
    status = VT_ERROR_OPEN_REGISTRY_KEY;
    ret = RegCreateKeyEx (
      HKEY_CURRENT_USER, "Software\\Voltage\\VSCOM\\paramUpdates", 0,
			NULL, 0, KEY_WRITE, NULL, &updateKey, NULL);
    if (ret != ERROR_SUCCESS)
      break;
    ret = RegCreateKeyEx (
      HKEY_CURRENT_USER, "Software\\Voltage\\VSCOM\\parameters", 0, 
			NULL, 0, KEY_WRITE, NULL, &paramKey, NULL);
    if (ret != ERROR_SUCCESS)
      break;

    /* Store the values in the registry
     */
    status = VT_ERROR_WRITE_REGISTRY_VALUE ;
    ret = RegSetValueEx(
      updateKey, distObj->qualDistrictName.data, 0, 
      REG_BINARY, (unsigned char *)&vTime, sizeof(vTime) );
    if (ret != ERROR_SUCCESS)
      break;
    ret = RegSetValueEx(
      paramKey, distObj->qualDistrictName.data, 0, 
      REG_SZ, distObj->paramsText, paramsTextLen + 1);
    if (ret != ERROR_SUCCESS)
      break;
    
    status = 0;

  } while (0);

  /* close the registry keys
   */
  if (paramKey != (HKEY)0)
    RegCloseKey(paramKey);
  if (updateKey != (HKEY)0)
    RegCloseKey(updateKey);

  return (status);
}

int VoltClientStoreCurrentDistrict (
   VtStorageCtx storageCtx,
   unsigned char *domainName,
   unsigned char *currentDistrict,
   VtTime *storeTime,
   VtTime *validityStart,
   VtTime *validityEnd
   )
{
  HKEY cdKey = (HKEY)0;
  HKEY updateKey = (HKEY)0;
  char district[256];
  int status, ret, districtLen;
  unsigned int distNameLen;
  VoltStorageCtx *ctx = (VoltStorageCtx *)storageCtx;
  VoltLibCtx *libCtx = (VoltLibCtx *)(ctx->voltObject.libraryCtx);
  VoltDefaultStorageCtx *defStorageCtx =
    (VoltDefaultStorageCtx *)(ctx->localStorageCtx);
  VoltFileCtx *fileCtx = (VoltFileCtx *)(defStorageCtx->fCtx);
  VoltFileHandle fileHandle = (VoltFileHandle)0;
  unsigned char *distName;

⌨️ 快捷键说明

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