rsaenh: Split CPExportKey into helper functions for each export type.

This commit is contained in:
Juan Lang 2009-01-28 20:52:22 -08:00 committed by Alexandre Julliard
parent 0eb9ae17fe
commit 8265569e99
1 changed files with 114 additions and 89 deletions

View File

@ -2207,63 +2207,13 @@ BOOL WINAPI RSAENH_CPDecrypt(HCRYPTPROV hProv, HCRYPTKEY hKey, HCRYPTHASH hHash,
return TRUE;
}
/******************************************************************************
* CPExportKey (RSAENH.@)
*
* Export a key into a binary large object (BLOB).
*
* PARAMS
* hProv [I] Key container from which a key is to be exported.
* hKey [I] Key to be exported.
* hPubKey [I] Key used to encrypt sensitive BLOB data.
* dwBlobType [I] SIMPLEBLOB, PUBLICKEYBLOB or PRIVATEKEYBLOB.
* dwFlags [I] Currently none defined.
* pbData [O] Pointer to a buffer where the BLOB will be written to.
* pdwDataLen [I/O] I: Size of buffer at pbData, O: Size of BLOB
*
* RETURNS
* Success: TRUE.
* Failure: FALSE.
*/
BOOL WINAPI RSAENH_CPExportKey(HCRYPTPROV hProv, HCRYPTKEY hKey, HCRYPTKEY hPubKey,
DWORD dwBlobType, DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
static BOOL crypt_export_simple(CRYPTKEY *pCryptKey, CRYPTKEY *pPubKey,
DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
{
CRYPTKEY *pCryptKey, *pPubKey;
BLOBHEADER *pBlobHeader = (BLOBHEADER*)pbData;
RSAPUBKEY *pRSAPubKey = (RSAPUBKEY*)(pBlobHeader+1);
ALG_ID *pAlgid = (ALG_ID*)(pBlobHeader+1);
DWORD dwDataLen;
TRACE("(hProv=%08lx, hKey=%08lx, hPubKey=%08lx, dwBlobType=%08x, dwFlags=%08x, pbData=%p,"
"pdwDataLen=%p)\n", hProv, hKey, hPubKey, dwBlobType, dwFlags, pbData, pdwDataLen);
if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
{
SetLastError(NTE_BAD_UID);
return FALSE;
}
if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
{
SetLastError(NTE_BAD_KEY);
return FALSE;
}
if (dwFlags & CRYPT_SSL2_FALLBACK) {
if (pCryptKey->aiAlgid != CALG_SSL2_MASTER) {
SetLastError(NTE_BAD_KEY);
return FALSE;
}
}
switch ((BYTE)dwBlobType)
{
case SIMPLEBLOB:
if (!lookup_handle(&handle_table, hPubKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pPubKey)){
SetLastError(NTE_BAD_PUBLIC_KEY); /* FIXME: error_code? */
return FALSE;
}
if (!(GET_ALG_CLASS(pCryptKey->aiAlgid)&(ALG_CLASS_DATA_ENCRYPT|ALG_CLASS_MSG_ENCRYPT))) {
SetLastError(NTE_BAD_KEY); /* FIXME: error code? */
return FALSE;
@ -2295,12 +2245,14 @@ BOOL WINAPI RSAENH_CPExportKey(HCRYPTPROV hProv, HCRYPTKEY hKey, HCRYPTKEY hPubK
}
*pdwDataLen = dwDataLen;
return TRUE;
}
case PUBLICKEYBLOB:
if (is_valid_handle(&handle_table, hPubKey, RSAENH_MAGIC_KEY)) {
SetLastError(NTE_BAD_KEY); /* FIXME: error code? */
return FALSE;
}
static BOOL crypt_export_public_key(CRYPTKEY *pCryptKey, BYTE *pbData,
DWORD *pdwDataLen)
{
BLOBHEADER *pBlobHeader = (BLOBHEADER*)pbData;
RSAPUBKEY *pRSAPubKey = (RSAPUBKEY*)(pBlobHeader+1);
DWORD dwDataLen;
if ((pCryptKey->aiAlgid != CALG_RSA_KEYX) && (pCryptKey->aiAlgid != CALG_RSA_SIGN)) {
SetLastError(NTE_BAD_KEY);
@ -2328,8 +2280,15 @@ BOOL WINAPI RSAENH_CPExportKey(HCRYPTPROV hProv, HCRYPTKEY hKey, HCRYPTKEY hPubK
}
*pdwDataLen = dwDataLen;
return TRUE;
}
static BOOL crypt_export_private_key(CRYPTKEY *pCryptKey, BYTE *pbData,
DWORD *pdwDataLen)
{
BLOBHEADER *pBlobHeader = (BLOBHEADER*)pbData;
RSAPUBKEY *pRSAPubKey = (RSAPUBKEY*)(pBlobHeader+1);
DWORD dwDataLen;
case PRIVATEKEYBLOB:
if ((pCryptKey->aiAlgid != CALG_RSA_KEYX) && (pCryptKey->aiAlgid != CALG_RSA_SIGN)) {
SetLastError(NTE_BAD_KEY);
return FALSE;
@ -2357,6 +2316,72 @@ BOOL WINAPI RSAENH_CPExportKey(HCRYPTPROV hProv, HCRYPTKEY hKey, HCRYPTKEY hPubK
}
*pdwDataLen = dwDataLen;
return TRUE;
}
/******************************************************************************
* CPExportKey (RSAENH.@)
*
* Export a key into a binary large object (BLOB).
*
* PARAMS
* hProv [I] Key container from which a key is to be exported.
* hKey [I] Key to be exported.
* hPubKey [I] Key used to encrypt sensitive BLOB data.
* dwBlobType [I] SIMPLEBLOB, PUBLICKEYBLOB or PRIVATEKEYBLOB.
* dwFlags [I] Currently none defined.
* pbData [O] Pointer to a buffer where the BLOB will be written to.
* pdwDataLen [I/O] I: Size of buffer at pbData, O: Size of BLOB
*
* RETURNS
* Success: TRUE.
* Failure: FALSE.
*/
BOOL WINAPI RSAENH_CPExportKey(HCRYPTPROV hProv, HCRYPTKEY hKey, HCRYPTKEY hPubKey,
DWORD dwBlobType, DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
{
CRYPTKEY *pCryptKey, *pPubKey;
TRACE("(hProv=%08lx, hKey=%08lx, hPubKey=%08lx, dwBlobType=%08x, dwFlags=%08x, pbData=%p,"
"pdwDataLen=%p)\n", hProv, hKey, hPubKey, dwBlobType, dwFlags, pbData, pdwDataLen);
if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
{
SetLastError(NTE_BAD_UID);
return FALSE;
}
if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
{
SetLastError(NTE_BAD_KEY);
return FALSE;
}
if (dwFlags & CRYPT_SSL2_FALLBACK) {
if (pCryptKey->aiAlgid != CALG_SSL2_MASTER) {
SetLastError(NTE_BAD_KEY);
return FALSE;
}
}
switch ((BYTE)dwBlobType)
{
case SIMPLEBLOB:
if (!lookup_handle(&handle_table, hPubKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pPubKey)){
SetLastError(NTE_BAD_PUBLIC_KEY); /* FIXME: error_code? */
return FALSE;
}
return crypt_export_simple(pCryptKey, pPubKey, dwFlags, pbData, pdwDataLen);
case PUBLICKEYBLOB:
if (is_valid_handle(&handle_table, hPubKey, RSAENH_MAGIC_KEY)) {
SetLastError(NTE_BAD_KEY); /* FIXME: error code? */
return FALSE;
}
return crypt_export_public_key(pCryptKey, pbData, pdwDataLen);
case PRIVATEKEYBLOB:
return crypt_export_private_key(pCryptKey, pbData, pdwDataLen);
default:
SetLastError(NTE_BAD_TYPE); /* FIXME: error code? */