rsaenh: Split CPImportKey into a helper function for each type of key supported.

This commit is contained in:
Juan Lang 2009-01-28 21:47:53 -08:00 committed by Alexandre Julliard
parent 30764edd12
commit 17da3004ef
1 changed files with 219 additions and 109 deletions

View File

@ -2460,40 +2460,36 @@ static void release_and_install_key(HCRYPTPROV hProv, HCRYPTKEY src,
}
/******************************************************************************
* CPImportKey (RSAENH.@)
* import_private_key [Internal]
*
* Import a BLOB'ed key into a key container.
* Import a BLOB'ed private key into a key container.
*
* PARAMS
* hProv [I] Key container into which the key is to be imported.
* pbData [I] Pointer to a buffer which holds the BLOB.
* hProv [I] Key container into which the private key is to be imported.
* pbData [I] Pointer to a buffer which holds the private key BLOB.
* dwDataLen [I] Length of data in buffer at pbData.
* hPubKey [I] Key used to decrypt sensitive BLOB data.
* dwFlags [I] One of:
* CRYPT_EXPORTABLE: the imported key is marked exportable
* phKey [O] Handle to the imported key.
*
*
* NOTES
* Assumes the caller has already checked the BLOBHEADER at pbData to ensure
* it's a PRIVATEKEYBLOB.
*
* RETURNS
* Success: TRUE.
* Failure: FALSE.
*/
BOOL WINAPI RSAENH_CPImportKey(HCRYPTPROV hProv, CONST BYTE *pbData, DWORD dwDataLen,
HCRYPTKEY hPubKey, DWORD dwFlags, HCRYPTKEY *phKey)
static BOOL import_private_key(HCRYPTPROV hProv, CONST BYTE *pbData, DWORD dwDataLen,
DWORD dwFlags, HCRYPTKEY *phKey)
{
KEYCONTAINER *pKeyContainer;
CRYPTKEY *pCryptKey, *pPubKey;
CRYPTKEY *pCryptKey;
CONST BLOBHEADER *pBlobHeader = (CONST BLOBHEADER*)pbData;
CONST RSAPUBKEY *pRSAPubKey = (CONST RSAPUBKEY*)(pBlobHeader+1);
CONST ALG_ID *pAlgid = (CONST ALG_ID*)(pBlobHeader+1);
CONST BYTE *pbKeyStream = (CONST BYTE*)(pAlgid + 1);
ALG_ID algID;
BYTE *pbDecrypted;
DWORD dwKeyLen;
BOOL ret;
TRACE("(hProv=%08lx, pbData=%p, dwDataLen=%d, hPubKey=%08lx, dwFlags=%08x, phKey=%p)\n",
hProv, pbData, dwDataLen, hPubKey, dwFlags, phKey);
if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER,
(OBJECTHDR**)&pKeyContainer))
{
@ -2501,17 +2497,6 @@ BOOL WINAPI RSAENH_CPImportKey(HCRYPTPROV hProv, CONST BYTE *pbData, DWORD dwDat
return FALSE;
}
if (dwDataLen < sizeof(BLOBHEADER) ||
pBlobHeader->bVersion != CUR_BLOB_VERSION ||
pBlobHeader->reserved != 0)
{
SetLastError(NTE_BAD_DATA);
return FALSE;
}
switch (pBlobHeader->bType)
{
case PRIVATEKEYBLOB:
if ((dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY)) ||
(pRSAPubKey->magic != RSAENH_MAGIC_RSA2) ||
(dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) +
@ -2534,20 +2519,57 @@ BOOL WINAPI RSAENH_CPImportKey(HCRYPTPROV hProv, CONST BYTE *pbData, DWORD dwDat
case AT_SIGNATURE:
case CALG_RSA_SIGN:
TRACE("installing signing key\n");
release_and_install_key(hProv, *phKey,
&pKeyContainer->hSignatureKeyPair);
release_and_install_key(hProv, *phKey, &pKeyContainer->hSignatureKeyPair);
break;
case AT_KEYEXCHANGE:
case CALG_RSA_KEYX:
TRACE("installing key exchange key\n");
release_and_install_key(hProv, *phKey,
&pKeyContainer->hKeyExchangeKeyPair);
release_and_install_key(hProv, *phKey, &pKeyContainer->hKeyExchangeKeyPair);
break;
}
}
return ret;
}
/******************************************************************************
* import_public_key [Internal]
*
* Import a BLOB'ed public key into a key container.
*
* PARAMS
* hProv [I] Key container into which the public key is to be imported.
* pbData [I] Pointer to a buffer which holds the public key BLOB.
* dwDataLen [I] Length of data in buffer at pbData.
* dwFlags [I] One of:
* CRYPT_EXPORTABLE: the imported key is marked exportable
* phKey [O] Handle to the imported key.
*
*
* NOTES
* Assumes the caller has already checked the BLOBHEADER at pbData to ensure
* it's a PUBLICKEYBLOB.
*
* RETURNS
* Success: TRUE.
* Failure: FALSE.
*/
static BOOL import_public_key(HCRYPTPROV hProv, CONST BYTE *pbData, DWORD dwDataLen,
DWORD dwFlags, HCRYPTKEY *phKey)
{
KEYCONTAINER *pKeyContainer;
CRYPTKEY *pCryptKey;
CONST BLOBHEADER *pBlobHeader = (CONST BLOBHEADER*)pbData;
CONST RSAPUBKEY *pRSAPubKey = (CONST RSAPUBKEY*)(pBlobHeader+1);
ALG_ID algID;
BOOL ret;
if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER,
(OBJECTHDR**)&pKeyContainer))
{
SetLastError(NTE_BAD_UID);
return FALSE;
}
case PUBLICKEYBLOB:
if ((dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY)) ||
(pRSAPubKey->magic != RSAENH_MAGIC_RSA1) ||
(dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) + (pRSAPubKey->bitlen >> 3)))
@ -2573,14 +2595,47 @@ BOOL WINAPI RSAENH_CPImportKey(HCRYPTPROV hProv, CONST BYTE *pbData, DWORD dwDat
case AT_KEYEXCHANGE:
case CALG_RSA_KEYX:
TRACE("installing public key\n");
release_and_install_key(hProv, *phKey,
&pKeyContainer->hKeyExchangeKeyPair);
release_and_install_key(hProv, *phKey, &pKeyContainer->hKeyExchangeKeyPair);
break;
}
}
return ret;
}
/******************************************************************************
* import_symmetric_key [Internal]
*
* Import a BLOB'ed symmetric key into a key container.
*
* PARAMS
* hProv [I] Key container into which the symmetric key is to be imported.
* pbData [I] Pointer to a buffer which holds the symmetric key BLOB.
* dwDataLen [I] Length of data in buffer at pbData.
* hPubKey [I] Key used to decrypt sensitive BLOB data.
* dwFlags [I] One of:
* CRYPT_EXPORTABLE: the imported key is marked exportable
* phKey [O] Handle to the imported key.
*
*
* NOTES
* Assumes the caller has already checked the BLOBHEADER at pbData to ensure
* it's a SIMPLEBLOB.
*
* RETURNS
* Success: TRUE.
* Failure: FALSE.
*/
static BOOL import_symmetric_key(HCRYPTPROV hProv, CONST BYTE *pbData,
DWORD dwDataLen, HCRYPTKEY hPubKey,
DWORD dwFlags, HCRYPTKEY *phKey)
{
CRYPTKEY *pCryptKey, *pPubKey;
CONST BLOBHEADER *pBlobHeader = (CONST BLOBHEADER*)pbData;
CONST ALG_ID *pAlgid = (CONST ALG_ID*)(pBlobHeader+1);
CONST BYTE *pbKeyStream = (CONST BYTE*)(pAlgid + 1);
BYTE *pbDecrypted;
DWORD dwKeyLen;
case SIMPLEBLOB:
if (!lookup_handle(&handle_table, hPubKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pPubKey) ||
pPubKey->aiAlgid != CALG_RSA_KEYX)
{
@ -2617,6 +2672,61 @@ BOOL WINAPI RSAENH_CPImportKey(HCRYPTPROV hProv, CONST BYTE *pbData, DWORD dwDat
if (dwFlags & CRYPT_EXPORTABLE)
pCryptKey->dwPermissions |= CRYPT_EXPORT;
return TRUE;
}
/******************************************************************************
* CPImportKey (RSAENH.@)
*
* Import a BLOB'ed key into a key container.
*
* PARAMS
* hProv [I] Key container into which the key is to be imported.
* pbData [I] Pointer to a buffer which holds the BLOB.
* dwDataLen [I] Length of data in buffer at pbData.
* hPubKey [I] Key used to decrypt sensitive BLOB data.
* dwFlags [I] One of:
* CRYPT_EXPORTABLE: the imported key is marked exportable
* phKey [O] Handle to the imported key.
*
* RETURNS
* Success: TRUE.
* Failure: FALSE.
*/
BOOL WINAPI RSAENH_CPImportKey(HCRYPTPROV hProv, CONST BYTE *pbData, DWORD dwDataLen,
HCRYPTKEY hPubKey, DWORD dwFlags, HCRYPTKEY *phKey)
{
KEYCONTAINER *pKeyContainer;
CONST BLOBHEADER *pBlobHeader = (CONST BLOBHEADER*)pbData;
TRACE("(hProv=%08lx, pbData=%p, dwDataLen=%d, hPubKey=%08lx, dwFlags=%08x, phKey=%p)\n",
hProv, pbData, dwDataLen, hPubKey, dwFlags, phKey);
if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER,
(OBJECTHDR**)&pKeyContainer))
{
SetLastError(NTE_BAD_UID);
return FALSE;
}
if (dwDataLen < sizeof(BLOBHEADER) ||
pBlobHeader->bVersion != CUR_BLOB_VERSION ||
pBlobHeader->reserved != 0)
{
SetLastError(NTE_BAD_DATA);
return FALSE;
}
switch (pBlobHeader->bType)
{
case PRIVATEKEYBLOB:
return import_private_key(hProv, pbData, dwDataLen, dwFlags, phKey);
case PUBLICKEYBLOB:
return import_public_key(hProv, pbData, dwDataLen, dwFlags, phKey);
case SIMPLEBLOB:
return import_symmetric_key(hProv, pbData, dwDataLen, hPubKey,
dwFlags, phKey);
default:
SetLastError(NTE_BAD_TYPE); /* FIXME: error code? */