From d611032e1e5b4e2261625ee924071e9713320837 Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Thu, 4 Dec 2008 18:59:50 +0000 Subject: Implement RSA Sign and Decrypt operations. Not tested. --- ckcapi-key.c | 92 ++++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 64 insertions(+), 28 deletions(-) (limited to 'ckcapi-key.c') diff --git a/ckcapi-key.c b/ckcapi-key.c index 1ce057c..88e769e 100644 --- a/ckcapi-key.c +++ b/ckcapi-key.c @@ -97,6 +97,40 @@ typedef struct _KeyObjectData } KeyObjectData; +static CK_RV +load_key_handle (CkCapiObjectData* objdata, HCRYPTKEY* ret_key) +{ + KeyObjectData* kdata = (KeyObjectData*)objdata; + HCRYPTPROV prov; + HCRYPTKEY key; + DWORD error; + + ASSERT(kdata); + + if(!CryptAcquireContextW(&prov, kdata->prov_info->pwszContainerName, + kdata->prov_info->pwszProvName, + kdata->prov_info->dwProvType, 0)) + { + return ckcapi_winerr_to_ckr(GetLastError()); + } + + if(!CryptGetUserKey(prov, kdata->prov_info->dwKeySpec, &key)) + { + error = GetLastError(); + CryptReleaseContext(prov, 0); + return ckcapi_winerr_to_ckr(error); + } + + if(ret_key) + *ret_key = key; + else + CryptDestroyKey(key); + + CryptReleaseContext(prov, 0); + return CKR_OK; +} + + static CK_RV load_raw_public_key(KeyObjectData* kdata) { @@ -108,7 +142,7 @@ load_raw_public_key(KeyObjectData* kdata) ASSERT(kdata); ASSERT(!kdata->raw_public_key.pbData); - ret = ckcapi_key_object_data_get_handles(&kdata->base, NULL, &key); + ret = load_key_handle(&kdata->base, &key); if(ret != CKR_OK) return ret; @@ -994,40 +1028,42 @@ ckcapi_key_find(CkCapiSession* sess, CK_OBJECT_CLASS cls, return ret; } -CK_RV -ckcapi_key_object_data_get_handles (CkCapiObjectData* objdata, HCRYPTPROV* ret_prov, - HCRYPTKEY* ret_key) +DWORD +ckcapi_key_object_data_get_bits(CkCapiObjectData* objdata) { - KeyObjectData* kdata = (KeyObjectData*)objdata; - HCRYPTPROV prov; - HCRYPTKEY key; - DWORD error; + KeyObjectData* kdata; + PUBLICKEYSTRUC* header; + RSAPUBKEY* pubkey; + CK_RV ret; - ASSERT(kdata); + ASSERT(objdata); - if(!CryptAcquireContextW(&prov, kdata->prov_info->pwszContainerName, - kdata->prov_info->pwszProvName, - kdata->prov_info->dwProvType, 0)) + kdata = (KeyObjectData*)objdata; + + if(!kdata->raw_public_key.pbData) { - return ckcapi_winerr_to_ckr(GetLastError()); + ret = load_raw_public_key(kdata); + if(ret != CKR_OK) + return ret; } - if(!CryptGetUserKey(prov, kdata->prov_info->dwKeySpec, &key)) - { - error = GetLastError(); - CryptReleaseContext(prov, 0); - return ckcapi_winerr_to_ckr(error); - } + header = (PUBLICKEYSTRUC*)kdata->raw_public_key.pbData; + if(!header->bType == PUBLICKEYBLOB) + return 0; - if(ret_key) - *ret_key = key; - else - CryptDestroyKey(key); + pubkey = (RSAPUBKEY*)(header + 1); + if(!pubkey->magic == 0x31415352) + return 0; - if(ret_prov) - *ret_prov = prov; - else - CryptReleaseContext(prov, 0); + return pubkey->bitlen; +} - return CKR_OK; +CRYPT_KEY_PROV_INFO* +ckcapi_key_object_data_get_prov_info(CkCapiObjectData* objdata) +{ + KeyObjectData* kdata; + + ASSERT(objdata); + kdata = (KeyObjectData*)objdata; + return kdata->prov_info; } -- cgit v1.2.3