📄 clientstore.c
字号:
unsigned char *fileName = (unsigned char *)0;
unsigned char *contents = (unsigned char *)0;
VtTime currentTime;
VtTime *theTime;
VoltTime vTime;
time_t nullTime = 0;
theTime = storeTime;
status = 0;
if (domainName == (unsigned char *)0)
return 0;
do
{
if (theTime == (VtTime *)0 )
{
status = VtGetTime ((VtLibCtx)libCtx, ¤tTime);
if (status != 0)
break;
theTime = ¤tTime;
}
/* Get the time as seconds to store in the registry
*/
status = VoltConvertTimeToSeconds (theTime, &vTime);
if (status != 0)
break;
/* Open the registry keys for distric and district updates
*/
status = VT_ERROR_OPEN_REGISTRY_KEY;
ret = RegCreateKeyEx (
HKEY_CURRENT_USER, "Software\\Voltage\\VSCOM\\cdUpdates", 0,
NULL, 0, KEY_WRITE, NULL, &updateKey, NULL);
if (ret != ERROR_SUCCESS)
break;
ret = RegCreateKeyEx (
HKEY_CURRENT_USER, "Software\\Voltage\\VSCOM\\currentDistricts", 0,
NULL, 0, KEY_WRITE, NULL, &cdKey, NULL);
if (ret != ERROR_SUCCESS)
break;
/* If currentDistrict is NULL it means we are storing NULL for district.
* The information that the district doesn't exist.
*/
if (currentDistrict == (unsigned char *)0)
{
status = VT_ERROR_WRITE_REGISTRY_VALUE ;
ret = RegSetValueEx(
cdKey, domainName, 0, REG_BINARY, (unsigned char *)&nullTime, sizeof (nullTime));
if (ret != ERROR_SUCCESS)
break;
goto updateKey;
}
/* Now we have a valid district. See if a current district
* newer than the district being stored already exists.
*/
distName = currentDistrict;
distNameLen = Z2Strlen (distName);
districtLen = 256;
/* Make sure we have the valid dates for validityStart and validityEnd
*/
status = VT_ERROR_INVALID_STORAGE_ENTRY;
if ( (validityStart->month == 0) ||
(validityEnd->month == 0) )
break;
ret = RegQueryValueEx (
cdKey, domainName, NULL, NULL, (unsigned char *)district, &districtLen);
if (ret == ERROR_SUCCESS)
{
if (Z2Memcmp (district, distName, distNameLen) > 0)
{
/* we already have a newer district. don't do anything.
*/
status = 0;
break;
}
}
/* We either don't have a current district for this domain or we have
* an older district. So we need to write the new district.
*/
status = VT_ERROR_WRITE_REGISTRY_VALUE ;
ret = RegSetValueEx(
cdKey, domainName, 0, REG_SZ, distName, distNameLen + 1);
if (ret != ERROR_SUCCESS)
break;
/* Now write the update key
*/
updateKey :
status = VT_ERROR_WRITE_REGISTRY_VALUE ;
ret = RegSetValueEx(
updateKey, domainName, 0, REG_BINARY, (unsigned char *)&vTime, sizeof(vTime) );
if (ret != ERROR_SUCCESS)
break;
status = 0;
} while (0);
if (cdKey != (HKEY)0)
RegCloseKey(cdKey);
if (updateKey != (HKEY)0)
RegCloseKey(updateKey);
return (status);
}
int VoltClientStorePublicSigningCert (
VtStorageCtx storageCtx,
VtIdentityObject reference,
VtItem *cert
)
{
int status;
unsigned int fileNameLen;
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 *fileName = (unsigned char *)0;
/* 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 file name.
*/
status = VoltGetFileNameFromValueAlloc (
ctx, (unsigned char *)0, 0, idObj->encoding.data, idObj->encoding.len,
VOLT_FILE_NAME_VALUE_TYPE_CERT_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;
/* Signing certs are stored unencrypted by client.
*/
status = fileCtx->CtxWriteFile (
fileCtx, fileHandle, cert->data, cert->len);
} while (0);
if (fileHandle != (VoltFileHandle)0)
fileCtx->CtxCloseFile (fileCtx, &fileHandle);
if (fileName != (unsigned char *)0)
Z2Free (fileName);
return (status);
}
int VoltClientStoreAuthToken (
VtStorageCtx storageCtx,
VtDistrictObject reference,
unsigned char *authToken
)
{
return VoltStoreAuthTokenForDistrict (
storageCtx, reference, authToken,
VOLT_FILE_NAME_VALUE_TYPE_IBE_TOKEN_CLIENT);
}
int VoltClientGetDistrictParameterCount (
VtStorageCtx storageCtx,
VtDistrictObject reference,
unsigned int *count
)
{
HKEY paramKey = (HKEY)0;
DWORD valueCount, paramLen;
int status, ret;
VoltStorageCtx *ctx = (VoltStorageCtx *)storageCtx;
VoltLibCtx *libCtx = (VoltLibCtx *)(ctx->voltObject.libraryCtx);
VtDistrictObject tempObject = (VtDistrictObject)0;
VoltDistrictObject *obj = (VoltDistrictObject *)reference;
VtFileNameList *nameList = (VtFileNameList *)0;
do
{
status = VT_ERROR_OPEN_REGISTRY_KEY;
ret = RegOpenKey(
HKEY_CURRENT_USER,
_T("Software\\Voltage\\VSCOM\\parameters"), ¶mKey);
/* If the registry key doesn't exist it doesn't mean an error.
* we don't have any entries so count is 0 in that case.
*/
if (ret == ERROR_FILE_NOT_FOUND)
{
*count = 0;
status = 0;
break;
}
/* Any other error pass on
*/
if (ret != ERROR_SUCCESS)
break;
/* If there's no reference, return the count of all parameter sets.
*/
if (reference == (VtDistrictObject)0)
{
status = VT_ERROR_READ_REGISTRY_VALUE ;
ret = RegQueryInfoKey (
paramKey, NULL, NULL, NULL, NULL, NULL,
NULL, &valueCount, NULL, NULL, NULL, NULL);
if (ret != ERROR_SUCCESS)
break;
*count = valueCount;
status = 0;
break;
}
/* If there's no qualified name, this provider can't come up with
* an answer.
*/
status = VT_ERROR_INVALID_STORAGE_REF;
if (obj->qualDistrictName.data == (unsigned char *)0)
break;
/* If there is a value in registry for given district
* we have got parameters otherwise not
*/
status = VT_ERROR_READ_REGISTRY_VALUE;
ret = RegQueryValueEx (
paramKey, obj->qualDistrictName.data, NULL, NULL, NULL, ¶mLen);
if (ret == ERROR_FILE_NOT_FOUND)
{
*count = 0;
status = 0;
break;
}
if (ret != ERROR_SUCCESS)
break;
/* If we are here, We were able to retrieve the registry value.
* Make sure its not empty.
*/
status = 0;
*count = 1;
if (paramLen <= 1)
*count = 0;
} while (0);
if (paramKey != (HKEY)0)
RegCloseKey (paramKey);
return (status);
}
int VoltClientGetPrivateSigningKeyCount (
VtStorageCtx storageCtx,
VtIdentityObject reference,
unsigned int *count
)
{
return (VoltGetEntryCountFromIdentity (
storageCtx, reference, count, VOLT_STORE_DIR_SIGN_KEYS_CLIENT,
VOLT_STORE_DIR_SIGN_KEYS_CLIENT_LEN,
VOLT_FILE_NAME_VALUE_TYPE_SIGN_PRI_CLIENT) );
}
int VoltClientGetIBEPrivateKeyCount (
VtStorageCtx storageCtx,
VtIdentityObject reference,
unsigned int *count
)
{
return (VoltGetEntryCountFromIdentity (
storageCtx, reference, count, VOLT_STORE_DIR_IBE_KEYS_CLIENT,
VOLT_STORE_DIR_IBE_KEYS_CLIENT_LEN,
VOLT_FILE_NAME_VALUE_TYPE_IBE_PRI_CLIENT) );
}
int VoltClientGetPublicSigningCertCount (
VtStorageCtx storageCtx,
VtIdentityObject reference,
unsigned int *count
)
{
return (VoltGetEntryCountFromIdentity (
storageCtx, reference, count, VOLT_STORE_DIR_SIGN_CERTS_CLIENT,
VOLT_STORE_DIR_SIGN_CERTS_CLIENT_LEN, VOLT_FILE_NAME_VALUE_TYPE_CERT_CLIENT));
}
int VoltClientGetAuthTokenCount (
VtStorageCtx storageCtx,
VtDistrictObject reference,
unsigned int *count
)
{
return VoltGetAuthTokenCountForDistrict (
storageCtx, reference, count, VOLT_FILE_NAME_VALUE_TYPE_IBE_TOKEN_CLIENT);
}
int VoltClientRetrieveNewAuthTokens (
VtStorageCtx storageCtx,
VtDistrictObject reference,
VoltAuthTokenListObject *tokenListObj,
unsigned int *newTokenCount
)
{
return VoltRetrieveNewAuthTokensForDistrict (
storageCtx, reference, tokenListObj,
newTokenCount,VOLT_FILE_NAME_VALUE_TYPE_IBE_TOKEN_CLIENT);
}
int VoltClientRetrieveIBEPrivateKey (
VtStorageCtx storageCtx,
VtIdentityObject reference,
VtPolicyCtx policyCtx,
unsigned int index,
VtKeyObject keyObj
)
{
int status;
unsigned int fileNameLen, contentsLen, distNameLen, bufferSize;
unsigned int passwordLen;
VoltStorageCtx *ctx = (VoltStorageCtx *)storageCtx;
VoltLibCtx *libCtx = (VoltLibCtx *)(ctx->voltObject.libraryCtx);
VoltDefaultStorageCtx *defStorageCtx =
(VoltDefaultStorageCtx *)(ctx->localStorageCtx);
VoltFileCtx *fileCtx = (VoltFileCtx *)(defStorageCtx->fCtx);
VoltIdentityObject *idObj = (VoltIdentityObject *)reference;
VtDistrictObject district = (VtDistrictObject)0;
VoltDistrictObject *distObj;
VoltParameterObject *paramObj;
VoltKeyObject *key = (VoltKeyObject *)keyObj;
unsigned char *password = (unsigned char *)0;
unsigned char *fileName = (unsigned char *)0;
unsigned char *backupName = (unsigned char *)0;
unsigned char *nameToUse;
unsigned char *contents = (unsigned char *)0;
unsigned char *distName = (unsigned char *)0;
VoltFileHandle fileHandle = (VoltFileHandle)0;
VtFileNameList *nameList = (VtFileNameList *)0;
Asn1IBEPrivateKey *asn1PriKey = (Asn1IBEPrivateKey *)0;
VtBFType1IBEPoint *ibePoint = (VtBFType1IBEPoint *)0;
VtBFType1IBEPriKeyInfo priKeyInfo;
VtTime timeOfStore;
VtDerCoder *derCoders[1] = { VtDerCoderBFType1IBEPrivateKey };
unsigned int derCoderCount = 1;
VtSetKeyBerInfo berInfo;
Z2Memset (&berInfo, 0, sizeof (VtSetKeyBerInfo));
Z2Memset (&priKeyInfo, 0, sizeof (VtBFType1IBEPriKeyInfo));
do
{
/* If the keyObj is already set, error.
*/
status = VT_ERROR_INVALID_KEY_OBJ;
if (key->keyType != 0)
break;
/* If there's no reference, get whatever value is at the index.
*/
if (reference == (VtIdentityObject)0)
{
status = VoltGetStorageFileListAlloc (
ctx, VOLT_STORE_DIR_IBE_KEYS_CLIENT,
VOLT_STORE_DIR_IBE_KEYS_CLIENT_LEN, 0, &nameList);
if (status != 0)
break;
status = VT_ERROR_ENTRY_NOT_FOUND;
if (nameList == (VtFileNameList *)0)
break;
if (index >= nameList->nameCount)
break;
nameToUse = nameList->nameList[index];
}
else
{
/* We need the identity encoded.
*/
status = VT_ERROR_INVALID_STORAGE_REF;
if (idObj->encoding.data == (unsigned char *)0)
break;
/* Get the file name as if it's in the toolkit directory.
*/
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;
nameToUse = fileName;
}
/* Try to open the file containing the IBE private key.
*/
status = fileCtx->CtxOpenFile (
fileCtx, &fileHandle, nameToUse, VOLT_FILE_MODE_READ_ONLY, 0);
/* If the file doesn't exist the error return should be
* VT_ERROR_ENTRY_NOT_FOUND.
*/
if (status != 0)
{
if (status == VT_ERROR_FILE_DOES_NOT_EXIST)
status = VT_ERROR_ENTRY_NOT_FOUND;
break;
}
/* Get the client storage password if any
*/
if (defStorageCtx->GetExtraPassword != (VGetExtraPassword)0)
{
status = defStorageCtx->GetExtraPassword (ctx, &password, &passwordLen);
if (status != 0)
break;
}
/* Get the contents of the file.
*/
status = mIcLoadDataAlloc (
ctx, fileCtx, fileHandle, password, passwordLen, &contents, &contentsLen);
if (status != 0)
break;
/* Decode the file contents as the ASN1 private key
*/
status = VoltDecodeIBEPriKeyDataCreate (
libCtx, contents, contentsLen, &asn1PriKey);
if (status != 0)
break;
/* We need the district params. If we don't have an idObject with
* a district object, get the params. If not, get them out of
* storage
*/
paramObj = (VoltParameterObject *)0;
if (idObj != (VoltIdentityObject *)0)
{
if (idObj->district != (VtDistrictObject)0)
{
distObj = (VoltDistrictObject *)(idObj->district);
if (distObj->paramObj != (VtParameterObject)0)
paramObj = (VoltParameterObject *)(distObj->paramObj);
}
}
if (paramObj == (VoltParameterObject *)0)
{
/* Get a district object based on the encoded Id in the private
* key.
*/
status = VtDecodeIdentityDistrict (
(VtLibCtx)libCtx, asn1PriKey->pubKey->base.data,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -