📄 clientstore.c
字号:
/* Copyright 2003-2006, 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"
#include "errorctx.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;
VoltIBEPriKeyData *keyData;
Asn1IBEPrivateKey *asn1Key = (Asn1IBEPrivateKey *)0;
unsigned char keyOid[VoltIBEPriKeyForm1OidBytesLen] =
{ VoltIBEPriKeyForm1OidBytes };
VOLT_DECLARE_ERROR_TYPE (errorType)
VOLT_DECLARE_FNCT_LINE (fnctLine)
/* Get the File Ctx
*/
defStorageCtx = (VoltDefaultStorageCtx *)ctx->localStorageCtx;
fileCtx = (VoltFileCtx *)defStorageCtx->fCtx;
do
{
/* We need the identity encoded.
*/
VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
VOLT_SET_FNCT_LINE (fnctLine)
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 = (VoltIBEPriKeyData *)(keyObj->keyData);
if ((keyObj->keyType & VOLT_KEY_TYPE_MASK_DATA) != VOLT_KEY_TYPE_DATA)
{
VOLT_SET_FNCT_LINE (fnctLine)
if (keyObj->GetKeyData == (VGetKeyData)0)
break;
VOLT_SET_ERROR_TYPE (errorType, 0)
VOLT_SET_FNCT_LINE (fnctLine)
status = keyObj->GetKeyData ((VtKeyObject)keyObj, (Pointer *)&keyData);
if (status != 0)
break;
}
/* Create the template for encoding the key following version 1.
*/
VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_MEMORY;
asn1Key = Asn1IBEPrivateKey_new ();
if (asn1Key == (Asn1IBEPrivateKey *)0)
break;
/* Set the fields.
*/
VOLT_SET_FNCT_LINE (fnctLine)
if (Asn1ObjectId_set (
asn1Key->privateData->format, keyOid,
VoltIBEPriKeyForm1OidBytesLen) != 1)
break;
VOLT_SET_FNCT_LINE (fnctLine)
if (ASN1_OCTET_STRING_set (
asn1Key->privateData->value, keyData->bfKeyInfo.privatePoint.yCoord.data,
keyData->bfKeyInfo.privatePoint.yCoord.len) != 1)
break;
/* Because it's optional, we have to create the pubKey.
*/
VOLT_SET_FNCT_LINE (fnctLine)
asn1Key->pubKey = Asn1Encoded_new ();
if (asn1Key->pubKey == (Asn1Encoded *)0)
break;
VOLT_SET_FNCT_LINE (fnctLine)
if (Asn1Encoded_set (
asn1Key->pubKey, keyData->bfKeyInfo.encodedId.data,
keyData->bfKeyInfo.encodedId.len) != 1)
break;
/* Call encode with no buffer to get the appropriate size.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_INPUT;
encodingLen = i2d_Asn1IBEPrivateKey (asn1Key, (unsigned char **)0);
if (encodingLen == 0)
break;
VOLT_SET_FNCT_LINE (fnctLine)
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.
*/
VOLT_SET_FNCT_LINE (fnctLine)
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.
*/
VOLT_SET_ERROR_TYPE (errorType, 0)
VOLT_SET_FNCT_LINE (fnctLine)
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.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = fileCtx->CtxCreateDirectories(fileCtx, fileName);
if (status != 0)
break;
/* Open this file to write. If it exists, destory the contents.
*/
VOLT_SET_FNCT_LINE (fnctLine)
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)
{
VOLT_SET_FNCT_LINE (fnctLine)
status = defStorageCtx->GetExtraPassword (ctx, &password, &passwordLen);
if (status != 0)
break;
}
/* Store the encoding.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = mIcStoreData (
ctx, buffer, encodingLen, password, passwordLen, fileCtx, fileHandle);
if (status != 0)
break;
/* Store the identity in "idList" file
*/
VOLT_SET_FNCT_LINE (fnctLine)
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);
VOLT_LOG_ERROR_INFO_COMPARE (
status, 0, storageCtx, status, 0, errorType,
(char *)0, "VoltClientStoreIBEPrivateKey", fnctLine, (char *)0)
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;
VOLT_DECLARE_ERROR_TYPE (errorType)
VOLT_DECLARE_FNCT_LINE (fnctLine)
/* 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.
*/
VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
VOLT_SET_FNCT_LINE (fnctLine)
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.
*/
VOLT_SET_ERROR_TYPE (errorType, 0)
VOLT_SET_FNCT_LINE (fnctLine)
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?
*/
VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_MEMORY;
clientDsaKey = Asn1ClientDSAPriKey_new ();
if (clientDsaKey == (Asn1ClientDSAPriKey *)0)
break;
/* Set the fields.
*/
VOLT_SET_FNCT_LINE (fnctLine)
if (ASN1_INTEGER_set (clientDsaKey->version, 0) != 1)
break;
VOLT_SET_FNCT_LINE (fnctLine)
if (ASN1_STRING_set (
clientDsaKey->prime, keyInfo->primeP.data, keyInfo->primeP.len) != 1)
break;
VOLT_SET_FNCT_LINE (fnctLine)
if (ASN1_STRING_set (
clientDsaKey->subprime, keyInfo->subprimeQ.data,
keyInfo->subprimeQ.len) != 1)
break;
VOLT_SET_FNCT_LINE (fnctLine)
if (ASN1_STRING_set (
clientDsaKey->base, keyInfo->baseG.data, keyInfo->baseG.len) != 1)
break;
VOLT_SET_FNCT_LINE (fnctLine)
if (ASN1_STRING_set (
clientDsaKey->pubVal, keyInfo->pubValY.data, keyInfo->pubValY.len) != 1)
break;
VOLT_SET_FNCT_LINE (fnctLine)
if (ASN1_STRING_set (
clientDsaKey->priVal, keyInfo->priValX.data, keyInfo->priValX.len) != 1)
break;
/* How big does the buffer need to be?
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_INPUT;
contentsLen = i2d_Asn1ClientDSAPriKey (clientDsaKey, (unsigned char **)0);
if (contentsLen == 0)
break;
/* Allocate the space.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_MEMORY;
contents = (unsigned char *)Z2Malloc (contentsLen, VOLT_MEMORY_SENSITIVE);
if (contents == (unsigned char *)0)
break;
/* Encode into the buffer.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_INPUT;
temp = contents;
contentsLen = i2d_Asn1ClientDSAPriKey (clientDsaKey, &temp);
if (contentsLen == 0)
break;
/* Get the file name.
*/
VOLT_SET_ERROR_TYPE (errorType, 0)
VOLT_SET_FNCT_LINE (fnctLine)
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.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = fileCtx->CtxCreateDirectories (fileCtx, fileName);
if (status != 0)
break;
/* Open this file to write. If it exists, destory the contents.
*/
VOLT_SET_FNCT_LINE (fnctLine)
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)
{
VOLT_SET_FNCT_LINE (fnctLine)
status = defStorageCtx->GetExtraPassword (ctx, &password, &passwordLen);
if (status != 0)
break;
}
/* Store the encoding.
*/
VOLT_SET_FNCT_LINE (fnctLine)
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);
VOLT_LOG_ERROR_INFO_COMPARE (
status, 0, storageCtx, status, 0, errorType,
(char *)0, "VoltClientStorePrivateSigningKey", fnctLine, (char *)0)
return (status);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -