3 * RSAENH - RSA encryption for Wine
5 * Copyright 2002 TransGaming Technologies (David Hammerton)
6 * Copyright 2004 Mike McCormack for CodeWeavers
7 * Copyright 2004 Michael Jung
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "wine/port.h"
26 #include "wine/library.h"
27 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(crypt);
42 /******************************************************************************
43 * CRYPTHASH - hash objects
45 #define RSAENH_MAGIC_HASH 0x85938417u
46 #define RSAENH_MAX_HASH_SIZE 36
47 #define RSAENH_HASHSTATE_IDLE 0
48 #define RSAENH_HASHSTATE_HASHING 1
49 #define RSAENH_HASHSTATE_FINISHED 2
50 typedef struct tagCRYPTHASH
59 BYTE abHashValue[RSAENH_MAX_HASH_SIZE];
63 /******************************************************************************
64 * CRYPTKEY - key objects
66 #define RSAENH_MAGIC_KEY 0x73620457u
67 #define RSAENH_MAX_KEY_SIZE 24
68 #define RSAENH_MAX_BLOCK_SIZE 24
69 #define RSAENH_KEYSTATE_IDLE 0
70 #define RSAENH_KEYSTATE_ENCRYPTING 1
71 #define RSAENH_KEYSTATE_DECRYPTING 2
72 typedef struct tagCRYPTKEY
85 BYTE abKeyValue[RSAENH_MAX_KEY_SIZE];
86 BYTE abInitVector[RSAENH_MAX_BLOCK_SIZE];
87 BYTE abChainVector[RSAENH_MAX_BLOCK_SIZE];
90 /******************************************************************************
91 * KEYCONTAINER - key containers
93 #define RSAENH_PERSONALITY_BASE 0u
94 #define RSAENH_PERSONALITY_STRONG 1u
95 #define RSAENH_PERSONALITY_ENHANCED 2u
96 #define RSAENH_PERSONALITY_SCHANNEL 3u
98 #define RSAENH_MAGIC_CONTAINER 0x26384993u
99 typedef struct tagKEYCONTAINER
105 CHAR szName[MAX_PATH];
106 CHAR szProvName[MAX_PATH];
107 HCRYPTKEY hKeyExchangeKeyPair;
108 HCRYPTKEY hSignatureKeyPair;
111 /******************************************************************************
112 * Some magic constants
114 #define RSAENH_ENCRYPT 1
115 #define RSAENH_DECRYPT 0
116 #define RSAENH_HMAC_DEF_IPAD_CHAR 0x36
117 #define RSAENH_HMAC_DEF_OPAD_CHAR 0x5c
118 #define RSAENH_HMAC_DEF_PAD_LEN 64
119 #define RSAENH_DES_EFFECTIVE_KEYLEN 56
120 #define RSAENH_DES_STORAGE_KEYLEN 64
121 #define RSAENH_3DES112_EFFECTIVE_KEYLEN 112
122 #define RSAENH_3DES112_STORAGE_KEYLEN 128
123 #define RSAENH_3DES_EFFECTIVE_KEYLEN 168
124 #define RSAENH_3DES_STORAGE_KEYLEN 192
125 #define RSAENH_MAGIC_RSA2 0x32415352
126 #define RSAENH_MAGIC_RSA1 0x31415352
127 #define RSAENH_PKC_BLOCKTYPE 0x02
128 #define RSAENH_REGKEY "Software\\Wine\\Crypto\\RSA\\%s"
130 #define RSAENH_MIN(a,b) ((a)<(b)?(a):(b))
131 /******************************************************************************
132 * aProvEnumAlgsEx - Defines the capabilities of the CSP personalities.
134 #define RSAENH_MAX_ENUMALGS 19
135 #define RSAENH_PCT1_SSL2_SSL3_TLS1 (CRYPT_FLAG_PCT1|CRYPT_FLAG_SSL2|CRYPT_FLAG_SSL3|CRYPT_FLAG_TLS1)
136 PROV_ENUMALGS_EX aProvEnumAlgsEx[4][RSAENH_MAX_ENUMALGS+1] =
139 {CALG_RC2, 40, 40, 56,0, 4,"RC2", 24,"RSA Data Security's RC2"},
140 {CALG_RC4, 40, 40, 56,0, 4,"RC4", 24,"RSA Data Security's RC4"},
141 {CALG_DES, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
142 {CALG_SHA, 160,160, 160,CRYPT_FLAG_SIGNING, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
143 {CALG_MD2, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD2", 23,"Message Digest 2 (MD2)"},
144 {CALG_MD4, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD4", 23,"Message Digest 4 (MD4)"},
145 {CALG_MD5, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD5", 23,"Message Digest 5 (MD5)"},
146 {CALG_SSL3_SHAMD5,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
147 {CALG_MAC, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
148 {CALG_RSA_SIGN, 512,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_SIGN",14,"RSA Signature"},
149 {CALG_RSA_KEYX, 512,384, 1024,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_KEYX",17,"RSA Key Exchange"},
150 {CALG_HMAC, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
151 {0, 0, 0, 0,0, 1,"", 1,""}
154 {CALG_RC2, 128, 40, 128,0, 4,"RC2", 24,"RSA Data Security's RC2"},
155 {CALG_RC4, 128, 40, 128,0, 4,"RC4", 24,"RSA Data Security's RC4"},
156 {CALG_DES, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
157 {CALG_3DES_112, 112,112, 112,0, 13,"3DES TWO KEY",19,"Two Key Triple DES"},
158 {CALG_3DES, 168,168, 168,0, 5,"3DES", 21,"Three Key Triple DES"},
159 {CALG_SHA, 160,160, 160,CRYPT_FLAG_SIGNING, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
160 {CALG_MD2, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD2", 23,"Message Digest 2 (MD2)"},
161 {CALG_MD4, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD4", 23,"Message Digest 4 (MD4)"},
162 {CALG_MD5, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD5", 23,"Message Digest 5 (MD5)"},
163 {CALG_SSL3_SHAMD5,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
164 {CALG_MAC, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
165 {CALG_RSA_SIGN,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_SIGN",14,"RSA Signature"},
166 {CALG_RSA_KEYX,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_KEYX",17,"RSA Key Exchange"},
167 {CALG_HMAC, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
168 {0, 0, 0, 0,0, 1,"", 1,""}
171 {CALG_RC2, 128, 40, 128,0, 4,"RC2", 24,"RSA Data Security's RC2"},
172 {CALG_RC4, 128, 40, 128,0, 4,"RC4", 24,"RSA Data Security's RC4"},
173 {CALG_DES, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
174 {CALG_3DES_112, 112,112, 112,0, 13,"3DES TWO KEY",19,"Two Key Triple DES"},
175 {CALG_3DES, 168,168, 168,0, 5,"3DES", 21,"Three Key Triple DES"},
176 {CALG_SHA, 160,160, 160,CRYPT_FLAG_SIGNING, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
177 {CALG_MD2, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD2", 23,"Message Digest 2 (MD2)"},
178 {CALG_MD4, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD4", 23,"Message Digest 4 (MD4)"},
179 {CALG_MD5, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD5", 23,"Message Digest 5 (MD5)"},
180 {CALG_SSL3_SHAMD5,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
181 {CALG_MAC, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
182 {CALG_RSA_SIGN,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_SIGN",14,"RSA Signature"},
183 {CALG_RSA_KEYX,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_KEYX",17,"RSA Key Exchange"},
184 {CALG_HMAC, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
185 {0, 0, 0, 0,0, 1,"", 1,""}
188 {CALG_RC2, 128, 40, 128,RSAENH_PCT1_SSL2_SSL3_TLS1, 4,"RC2", 24,"RSA Data Security's RC2"},
189 {CALG_RC4, 128, 40, 128,RSAENH_PCT1_SSL2_SSL3_TLS1, 4,"RC4", 24,"RSA Data Security's RC4"},
190 {CALG_DES, 56, 56, 56,RSAENH_PCT1_SSL2_SSL3_TLS1, 4,"DES", 31,"Data Encryption Standard (DES)"},
191 {CALG_3DES_112, 112,112, 112,RSAENH_PCT1_SSL2_SSL3_TLS1,13,"3DES TWO KEY",19,"Two Key Triple DES"},
192 {CALG_3DES, 168,168, 168,RSAENH_PCT1_SSL2_SSL3_TLS1, 5,"3DES", 21,"Three Key Triple DES"},
193 {CALG_SHA,160,160,160,CRYPT_FLAG_SIGNING|RSAENH_PCT1_SSL2_SSL3_TLS1,6,"SHA-1",30,"Secure Hash Algorithm (SHA-1)"},
194 {CALG_MD5,128,128,128,CRYPT_FLAG_SIGNING|RSAENH_PCT1_SSL2_SSL3_TLS1,4,"MD5",23,"Message Digest 5 (MD5)"},
195 {CALG_SSL3_SHAMD5,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
196 {CALG_MAC, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
197 {CALG_RSA_SIGN,1024,384,16384,CRYPT_FLAG_SIGNING|RSAENH_PCT1_SSL2_SSL3_TLS1,9,"RSA_SIGN",14,"RSA Signature"},
198 {CALG_RSA_KEYX,1024,384,16384,CRYPT_FLAG_SIGNING|RSAENH_PCT1_SSL2_SSL3_TLS1,9,"RSA_KEYX",17,"RSA Key Exchange"},
199 {CALG_HMAC, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
200 {CALG_PCT1_MASTER,128,128,128,CRYPT_FLAG_PCT1, 12,"PCT1 MASTER",12,"PCT1 Master"},
201 {CALG_SSL2_MASTER,40,40, 192,CRYPT_FLAG_SSL2, 12,"SSL2 MASTER",12,"SSL2 Master"},
202 {CALG_SSL3_MASTER,384,384,384,CRYPT_FLAG_SSL3, 12,"SSL3 MASTER",12,"SSL3 Master"},
203 {CALG_TLS1_MASTER,384,384,384,CRYPT_FLAG_TLS1, 12,"TLS1 MASTER",12,"TLS1 Master"},
204 {CALG_SCHANNEL_MASTER_HASH,0,0,-1,0, 16,"SCH MASTER HASH",21,"SChannel Master Hash"},
205 {CALG_SCHANNEL_MAC_KEY,0,0,-1,0, 12,"SCH MAC KEY",17,"SChannel MAC Key"},
206 {CALG_SCHANNEL_ENC_KEY,0,0,-1,0, 12,"SCH ENC KEY",24,"SChannel Encryption Key"},
207 {0, 0, 0, 0,0, 1,"", 1,""}
211 /******************************************************************************
212 * API forward declarations
215 RSAENH_CPGetKeyParam(
237 RSAENH_CPGetHashParam(
276 /******************************************************************************
277 * CSP's handle table (used by all acquired key containers)
279 static HANDLETABLE handle_table;
281 /******************************************************************************
284 * Initializes and destroys the handle table for the CSP's handles.
286 int WINAPI DllMain(HINSTANCE hInstance, DWORD fdwReason, PVOID pvReserved)
290 case DLL_PROCESS_ATTACH:
291 init_handle_table(&handle_table);
294 case DLL_PROCESS_DETACH:
295 destroy_handle_table(&handle_table);
301 /******************************************************************************
302 * copy_param [Internal]
304 * Helper function that supports the standard WINAPI protocol for querying data
308 * pbBuffer [O] Buffer where the queried parameter is copied to, if it is large enough.
309 * May be NUL if the required buffer size is to be queried only.
310 * pdwBufferSize [I/O] In: Size of the buffer at pbBuffer
311 * Out: Size of parameter pbParam
312 * pbParam [I] Parameter value.
313 * dwParamSize [I] Size of pbParam
316 * Success: TRUE (pbParam was copied into pbBuffer or pbBuffer is NULL)
317 * Failure: FALSE (pbBuffer is not large enough to hold pbParam). Last error: ERROR_MORE_DATA
319 static inline BOOL copy_param(
320 BYTE *pbBuffer, DWORD *pdwBufferSize, CONST BYTE *pbParam, DWORD dwParamSize)
324 if (dwParamSize > *pdwBufferSize)
326 SetLastError(ERROR_MORE_DATA);
327 *pdwBufferSize = dwParamSize;
330 memcpy(pbBuffer, pbParam, dwParamSize);
332 *pdwBufferSize = dwParamSize;
336 /******************************************************************************
337 * get_algid_info [Internal]
339 * Query CSP capabilities for a given crypto algorithm.
342 * pKeyContainer [I] Pointer to a key container of the CSP whose capabilities are to be queried.
343 * algid [I] Identifier of the crypto algorithm about which information is requested.
346 * Success: Pointer to a PROV_ENUMALGS_EX struct containing information about the crypto algorithm.
347 * Failure: NULL (algid not supported)
349 static inline const PROV_ENUMALGS_EX* get_algid_info(KEYCONTAINER *pKeyContainer, ALG_ID algid) {
350 PROV_ENUMALGS_EX *iterator;
352 for (iterator = aProvEnumAlgsEx[pKeyContainer->dwPersonality]; iterator->aiAlgid; iterator++) {
353 if (iterator->aiAlgid == algid) return iterator;
359 /******************************************************************************
360 * free_hmac_info [Internal]
362 * Deeply free an HMAC_INFO struct.
365 * hmac_info [I] Pointer to the HMAC_INFO struct to be freed.
368 * See Internet RFC 2104 for details on the HMAC algorithm.
370 static inline void free_hmac_info(PHMAC_INFO hmac_info) {
371 if (!hmac_info) return;
372 HeapFree(GetProcessHeap(), 0, hmac_info->pbInnerString);
373 HeapFree(GetProcessHeap(), 0, hmac_info->pbOuterString);
374 HeapFree(GetProcessHeap(), 0, hmac_info);
377 /******************************************************************************
378 * copy_hmac_info [Internal]
380 * Deeply copy an HMAC_INFO struct
383 * dst [O] Pointer to a location where the pointer to the HMAC_INFO copy will be stored.
384 * src [I] Pointer to the HMAC_INFO struct to be copied.
391 * See Internet RFC 2104 for details on the HMAC algorithm.
393 static BOOL copy_hmac_info(PHMAC_INFO *dst, PHMAC_INFO src) {
394 if (!src) return FALSE;
395 *dst = (PHMAC_INFO)HeapAlloc(GetProcessHeap(), 0, sizeof(HMAC_INFO));
396 if (!*dst) return FALSE;
397 memcpy(*dst, src, sizeof(HMAC_INFO));
398 (*dst)->pbInnerString = NULL;
399 (*dst)->pbOuterString = NULL;
400 if ((*dst)->cbInnerString == 0) (*dst)->cbInnerString = RSAENH_HMAC_DEF_PAD_LEN;
401 (*dst)->pbInnerString = (BYTE*)HeapAlloc(GetProcessHeap(), 0, (*dst)->cbInnerString);
402 if (!(*dst)->pbInnerString) {
403 free_hmac_info(*dst);
406 if (src->cbInnerString)
407 memcpy((*dst)->pbInnerString, src->pbInnerString, src->cbInnerString);
409 memset((*dst)->pbInnerString, RSAENH_HMAC_DEF_IPAD_CHAR, RSAENH_HMAC_DEF_PAD_LEN);
410 if ((*dst)->cbOuterString == 0) (*dst)->cbOuterString = RSAENH_HMAC_DEF_PAD_LEN;
411 (*dst)->pbOuterString = (BYTE*)HeapAlloc(GetProcessHeap(), 0, (*dst)->cbOuterString);
412 if (!(*dst)->pbOuterString) {
413 free_hmac_info(*dst);
416 if (src->cbOuterString)
417 memcpy((*dst)->pbOuterString, src->pbOuterString, src->cbOuterString);
419 memset((*dst)->pbOuterString, RSAENH_HMAC_DEF_OPAD_CHAR, RSAENH_HMAC_DEF_PAD_LEN);
423 /******************************************************************************
424 * destroy_hash [Internal]
426 * Destructor for hash objects
429 * pCryptHash [I] Pointer to the hash object to be destroyed.
430 * Will be invalid after function returns!
432 static void destroy_hash(OBJECTHDR *pCryptHash)
434 free_hmac_info(((CRYPTHASH*)pCryptHash)->pHMACInfo);
435 HeapFree(GetProcessHeap(), 0, pCryptHash);
438 /******************************************************************************
439 * init_hash [Internal]
441 * Initialize (or reset) a hash object
444 * pKeyContainer [I] Pointer to the key container the hash object belongs to.
445 * pCryptHash [I] The hash object to be initialized.
447 static inline BOOL init_hash(KEYCONTAINER *pKeyContainer, CRYPTHASH *pCryptHash) {
449 const PROV_ENUMALGS_EX *pAlgInfo;
451 switch (pCryptHash->aiAlgid)
454 if (pCryptHash->pHMACInfo) {
455 pAlgInfo = get_algid_info(pKeyContainer, pCryptHash->pHMACInfo->HashAlgid);
456 if (pAlgInfo) pCryptHash->dwHashSize = pAlgInfo->dwDefaultLen >> 3;
457 return init_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context);
462 dwLen = sizeof(DWORD);
463 RSAENH_CPGetKeyParam(pCryptHash->hProv, pCryptHash->hKey, KP_BLOCKLEN,
464 (BYTE*)&pCryptHash->dwHashSize, &dwLen, 0);
465 pCryptHash->dwHashSize >>= 3;
469 return init_hash_impl(pCryptHash->aiAlgid, &pCryptHash->context);
473 /******************************************************************************
474 * update_hash [Internal]
476 * Hashes the given data and updates the hash object's state accordingly
479 * pCryptHash [I] Hash object to be updated.
480 * pbData [I] Pointer to data stream to be hashed.
481 * dwDataLen [I] Length of data stream.
483 static inline void update_hash(CRYPTHASH *pCryptHash, CONST BYTE *pbData, DWORD dwDataLen) {
486 switch (pCryptHash->aiAlgid)
489 if (pCryptHash->pHMACInfo)
490 update_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
495 pbTemp = (BYTE*)HeapAlloc(GetProcessHeap(), 0, dwDataLen);
497 memcpy(pbTemp, pbData, dwDataLen);
498 RSAENH_CPEncrypt(pCryptHash->hProv, pCryptHash->hKey, (HCRYPTHASH)NULL, FALSE, 0,
499 pbTemp, &dwDataLen, dwDataLen);
500 HeapFree(GetProcessHeap(), 0, pbTemp);
504 update_hash_impl(pCryptHash->aiAlgid, &pCryptHash->context, pbData, dwDataLen);
508 /******************************************************************************
509 * finalize_hash [Internal]
511 * Finalizes the hash, after all data has been hashed with update_hash.
512 * No additional data can be hashed afterwards until the hash gets initialized again.
515 * pCryptHash [I] Hash object to be finalized.
517 static inline void finalize_hash(CRYPTHASH *pCryptHash) {
520 switch (pCryptHash->aiAlgid)
523 if (pCryptHash->pHMACInfo)
524 finalize_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
525 pCryptHash->abHashValue);
530 RSAENH_CPEncrypt(pCryptHash->hProv, pCryptHash->hKey, (HCRYPTHASH)NULL, TRUE, 0,
531 pCryptHash->abHashValue, &dwDataLen, pCryptHash->dwHashSize);
535 finalize_hash_impl(pCryptHash->aiAlgid, &pCryptHash->context, pCryptHash->abHashValue);
539 /******************************************************************************
540 * destroy_key [Internal]
542 * Destructor for key objects
545 * pCryptKey [I] Pointer to the key object to be destroyed.
546 * Will be invalid after function returns!
548 static void destroy_key(OBJECTHDR *pCryptKey)
550 free_key_impl(((CRYPTKEY*)pCryptKey)->aiAlgid, &((CRYPTKEY*)pCryptKey)->context);
551 HeapFree(GetProcessHeap(), 0, pCryptKey);
554 /******************************************************************************
555 * setup_key [Internal]
557 * Initialize (or reset) a key object
560 * pCryptKey [I] The key object to be initialized.
562 static inline void setup_key(CRYPTKEY *pCryptKey) {
563 pCryptKey->dwState = RSAENH_KEYSTATE_IDLE;
564 memcpy(pCryptKey->abChainVector, pCryptKey->abInitVector, sizeof(pCryptKey->abChainVector));
565 setup_key_impl(pCryptKey->aiAlgid, &pCryptKey->context, pCryptKey->dwKeyLen,
566 pCryptKey->dwSaltLen, pCryptKey->abKeyValue);
569 /******************************************************************************
572 * Creates a new key object without assigning the actual binary key value.
573 * This is done by CPDeriveKey, CPGenKey or CPImportKey, which call this function.
576 * hProv [I] Handle to the provider to which the created key will belong.
577 * aiAlgid [I] The new key shall use the crypto algorithm idenfied by aiAlgid.
578 * dwFlags [I] Upper 16 bits give the key length.
579 * Lower 16 bits: CRYPT_CREATE_SALT, CRYPT_NO_SALT
580 * ppCryptKey [O] Pointer to the created key
583 * Success: Handle to the created key.
584 * Failure: INVALID_HANDLE_VALUE
586 static HCRYPTKEY new_key(HCRYPTPROV hProv, ALG_ID aiAlgid, DWORD dwFlags, CRYPTKEY **ppCryptKey)
588 KEYCONTAINER *pKeyContainer;
591 DWORD dwKeyLen = HIWORD(dwFlags);
592 const PROV_ENUMALGS_EX *peaAlgidInfo;
594 if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER, (OBJECTHDR**)&pKeyContainer))
596 SetLastError(NTE_BAD_UID);
597 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
601 * Retrieve the CSP's capabilities for the given ALG_ID value
603 peaAlgidInfo = get_algid_info(pKeyContainer, aiAlgid);
605 SetLastError(NTE_BAD_ALGID);
606 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
610 * Assume the default key length, if none is specified explicitly
612 if (dwKeyLen == 0) dwKeyLen = peaAlgidInfo->dwDefaultLen;
615 * Check if the requested key length is supported by the current CSP.
616 * Adjust key length's for DES algorithms.
620 if (dwKeyLen == RSAENH_DES_EFFECTIVE_KEYLEN) {
621 dwKeyLen = RSAENH_DES_STORAGE_KEYLEN;
623 if (dwKeyLen != RSAENH_DES_STORAGE_KEYLEN) {
624 SetLastError(NTE_BAD_FLAGS);
625 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
630 if (dwKeyLen == RSAENH_3DES112_EFFECTIVE_KEYLEN) {
631 dwKeyLen = RSAENH_3DES112_STORAGE_KEYLEN;
633 if (dwKeyLen != RSAENH_3DES112_STORAGE_KEYLEN) {
634 SetLastError(NTE_BAD_FLAGS);
635 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
640 if (dwKeyLen == RSAENH_3DES_EFFECTIVE_KEYLEN) {
641 dwKeyLen = RSAENH_3DES_STORAGE_KEYLEN;
643 if (dwKeyLen != RSAENH_3DES_STORAGE_KEYLEN) {
644 SetLastError(NTE_BAD_FLAGS);
645 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
651 dwKeyLen > peaAlgidInfo->dwMaxLen ||
652 dwKeyLen < peaAlgidInfo->dwMinLen)
654 SetLastError(NTE_BAD_FLAGS);
655 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
659 hCryptKey = (HCRYPTKEY)new_object(&handle_table, sizeof(CRYPTKEY), RSAENH_MAGIC_KEY,
660 destroy_key, (OBJECTHDR**)&pCryptKey);
661 if (hCryptKey != (HCRYPTKEY)INVALID_HANDLE_VALUE)
663 pCryptKey->aiAlgid = aiAlgid;
664 pCryptKey->hProv = hProv;
665 pCryptKey->dwModeBits = 0;
666 pCryptKey->dwPermissions = CRYPT_ENCRYPT | CRYPT_DECRYPT | CRYPT_READ | CRYPT_WRITE |
668 pCryptKey->dwKeyLen = dwKeyLen >> 3;
669 if ((dwFlags & CRYPT_CREATE_SALT) || (dwKeyLen == 40 && !(dwFlags & CRYPT_NO_SALT)))
670 pCryptKey->dwSaltLen = 16 /*FIXME*/ - pCryptKey->dwKeyLen;
672 pCryptKey->dwSaltLen = 0;
673 memset(pCryptKey->abKeyValue, 0, sizeof(pCryptKey->abKeyValue));
674 memset(pCryptKey->abInitVector, 0, sizeof(pCryptKey->abInitVector));
679 pCryptKey->dwBlockLen = 0;
680 pCryptKey->dwMode = 0;
687 pCryptKey->dwBlockLen = 8;
688 pCryptKey->dwMode = CRYPT_MODE_CBC;
693 pCryptKey->dwBlockLen = dwKeyLen >> 3;
694 pCryptKey->dwMode = 0;
698 *ppCryptKey = pCryptKey;
704 /******************************************************************************
705 * destroy_key_container [Internal]
707 * Destructor for key containers. The user's signature and key exchange private
708 * keys are stored in the registry _IN_PLAINTEXT_.
711 * pObjectHdr [I] Pointer to the key container to be destroyed.
713 static void destroy_key_container(OBJECTHDR *pObjectHdr)
715 KEYCONTAINER *pKeyContainer = (KEYCONTAINER*)pObjectHdr;
717 CHAR szRSABase[MAX_PATH];
722 if (!(pKeyContainer->dwFlags & CRYPT_VERIFYCONTEXT)) {
723 /* On WinXP, persistent keys are stored in a file located at:
724 * $AppData$\\Microsoft\\Crypto\\RSA\\$SID$\\some_hex_string
726 sprintf(szRSABase, RSAENH_REGKEY, pKeyContainer->szName);
728 if (pKeyContainer->dwFlags & CRYPT_MACHINE_KEYSET) {
729 hRootKey = HKEY_LOCAL_MACHINE;
731 hRootKey = HKEY_CURRENT_USER;
734 if (RegCreateKeyExA(hRootKey, szRSABase, 0, NULL, REG_OPTION_NON_VOLATILE,
735 KEY_WRITE, NULL, &hKey, NULL) == ERROR_SUCCESS)
737 if (lookup_handle(&handle_table, pKeyContainer->hKeyExchangeKeyPair, RSAENH_MAGIC_KEY,
740 if (RSAENH_CPExportKey(pKey->hProv, pKeyContainer->hKeyExchangeKeyPair, 0,
741 PRIVATEKEYBLOB, 0, 0, &dwLen))
743 pbKey = (BYTE*)HeapAlloc(GetProcessHeap(), 0, dwLen);
746 if (RSAENH_CPExportKey(pKey->hProv, pKeyContainer->hKeyExchangeKeyPair, 0,
747 PRIVATEKEYBLOB, 0, pbKey, &dwLen))
749 RegSetValueExA(hKey, "KeyExchangeKeyPair", 0, REG_BINARY, pbKey, dwLen);
751 HeapFree(GetProcessHeap(), 0, pbKey);
754 release_handle(&handle_table, (unsigned int)pKeyContainer->hKeyExchangeKeyPair,
758 if (lookup_handle(&handle_table, pKeyContainer->hSignatureKeyPair, RSAENH_MAGIC_KEY,
761 if (RSAENH_CPExportKey(pKey->hProv, pKeyContainer->hSignatureKeyPair, 0,
762 PRIVATEKEYBLOB, 0, 0, &dwLen))
764 pbKey = (BYTE*)HeapAlloc(GetProcessHeap(), 0, dwLen);
767 if (RSAENH_CPExportKey(pKey->hProv, pKeyContainer->hSignatureKeyPair, 0,
768 PRIVATEKEYBLOB, 0, pbKey, &dwLen))
770 RegSetValueExA(hKey, "SignatureKeyPair", 0, REG_BINARY, pbKey, dwLen);
772 HeapFree(GetProcessHeap(), 0, pbKey);
775 release_handle(&handle_table, (unsigned int)pKeyContainer->hSignatureKeyPair,
783 HeapFree( GetProcessHeap(), 0, pKeyContainer );
786 /******************************************************************************
787 * new_key_container [Internal]
789 * Create a new key container. The personality (RSA Base, Strong or Enhanced CP)
790 * of the CSP is determined via the pVTable->pszProvName string.
793 * pszContainerName [I] Name of the key container.
794 * pVTable [I] Callback functions and context info provided by the OS
797 * Success: Handle to the new key container.
798 * Failure: INVALID_HANDLE_VALUE
800 static HCRYPTPROV new_key_container(PCHAR pszContainerName, DWORD dwFlags, PVTableProvStruc pVTable)
802 KEYCONTAINER *pKeyContainer;
803 HCRYPTPROV hKeyContainer;
805 hKeyContainer = (HCRYPTPROV)new_object(&handle_table, sizeof(KEYCONTAINER), RSAENH_MAGIC_CONTAINER,
806 destroy_key_container, (OBJECTHDR**)&pKeyContainer);
807 if (hKeyContainer != (HCRYPTPROV)INVALID_HANDLE_VALUE)
809 strncpy(pKeyContainer->szName, pszContainerName, MAX_PATH);
810 pKeyContainer->szName[MAX_PATH-1] = '\0';
811 pKeyContainer->dwFlags = dwFlags;
812 pKeyContainer->dwEnumAlgsCtr = 0;
813 pKeyContainer->hKeyExchangeKeyPair = (HCRYPTKEY)INVALID_HANDLE_VALUE;
814 pKeyContainer->hSignatureKeyPair = (HCRYPTKEY)INVALID_HANDLE_VALUE;
815 if (pVTable && pVTable->pszProvName) {
816 strncpy(pKeyContainer->szProvName, pVTable->pszProvName, MAX_PATH);
817 pKeyContainer->szProvName[MAX_PATH-1] = '\0';
818 if (!strcmp(pVTable->pszProvName, MS_DEF_PROV_A)) {
819 pKeyContainer->dwPersonality = RSAENH_PERSONALITY_BASE;
820 } else if (!strcmp(pVTable->pszProvName, MS_ENHANCED_PROV_A)) {
821 pKeyContainer->dwPersonality = RSAENH_PERSONALITY_ENHANCED;
822 } else if (!strcmp(pVTable->pszProvName, MS_DEF_RSA_SCHANNEL_PROV_A)) {
823 pKeyContainer->dwPersonality = RSAENH_PERSONALITY_SCHANNEL;
825 pKeyContainer->dwPersonality = RSAENH_PERSONALITY_STRONG;
830 return hKeyContainer;
833 /******************************************************************************
834 * read_key_container [Internal]
836 * Tries to read the persistent state of the key container (mainly the signature
837 * and key exchange private keys) given by pszContainerName.
840 * pszContainerName [I] Name of the key container to read from the registry
841 * pVTable [I] Pointer to context data provided by the operating system
844 * Success: Handle to the key container read from the registry
845 * Failure: INVALID_HANDLE_VALUE
847 static HCRYPTPROV read_key_container(PCHAR pszContainerName, DWORD dwFlags, PVTableProvStruc pVTable)
849 CHAR szRSABase[MAX_PATH];
852 DWORD dwValueType, dwLen;
853 KEYCONTAINER *pKeyContainer;
854 HCRYPTPROV hKeyContainer;
856 sprintf(szRSABase, RSAENH_REGKEY, pszContainerName);
858 if (dwFlags & CRYPT_MACHINE_KEYSET) {
859 hRootKey = HKEY_LOCAL_MACHINE;
861 hRootKey = HKEY_CURRENT_USER;
864 if (RegOpenKeyExA(hRootKey, szRSABase, 0, KEY_READ, &hKey) != ERROR_SUCCESS)
866 SetLastError(NTE_BAD_KEYSET);
867 return (HCRYPTPROV)INVALID_HANDLE_VALUE;
870 hKeyContainer = new_key_container(pszContainerName, dwFlags, pVTable);
871 if (hKeyContainer != (HCRYPTPROV)INVALID_HANDLE_VALUE)
873 if (!lookup_handle(&handle_table, hKeyContainer, RSAENH_MAGIC_CONTAINER,
874 (OBJECTHDR**)&pKeyContainer))
875 return (HCRYPTPROV)INVALID_HANDLE_VALUE;
877 if (RegQueryValueExA(hKey, "KeyExchangeKeyPair", 0, &dwValueType, NULL, &dwLen) ==
880 pbKey = (BYTE*)HeapAlloc(GetProcessHeap(), 0, dwLen);
883 if (RegQueryValueExA(hKey, "KeyExchangeKeyPair", 0, &dwValueType, pbKey, &dwLen) ==
886 RSAENH_CPImportKey(hKeyContainer, pbKey, dwLen, 0, 0,
887 &pKeyContainer->hKeyExchangeKeyPair);
889 HeapFree(GetProcessHeap(), 0, pbKey);
893 if (RegQueryValueExA(hKey, "SignatureKeyPair", 0, &dwValueType, NULL, &dwLen) ==
896 pbKey = (BYTE*)HeapAlloc(GetProcessHeap(), 0, dwLen);
899 if (RegQueryValueExA(hKey, "SignatureKeyPair", 0, &dwValueType, pbKey, &dwLen) ==
902 RSAENH_CPImportKey(hKeyContainer, pbKey, dwLen, 0, 0,
903 &pKeyContainer->hSignatureKeyPair);
905 HeapFree(GetProcessHeap(), 0, pbKey);
910 return hKeyContainer;
913 /******************************************************************************
914 * build_hash_signature [Internal]
916 * Builds a padded version of a hash to match the length of the RSA key modulus.
919 * pbSignature [O] The padded hash object is stored here.
920 * dwLen [I] Length of the pbSignature buffer.
921 * aiAlgid [I] Algorithm identifier of the hash to be padded.
922 * abHashValue [I] The value of the hash object.
923 * dwHashLen [I] Length of the hash value.
924 * dwFlags [I] Selection of padding algorithm.
928 * Failure: FALSE (NTE_BAD_ALGID)
930 static BOOL build_hash_signature(BYTE *pbSignature, DWORD dwLen, ALG_ID aiAlgid,
931 CONST BYTE *abHashValue, DWORD dwHashLen, DWORD dwFlags)
933 /* These prefixes are meant to be concatenated with hash values of the
934 * respective kind to form a PKCS #7 DigestInfo. */
935 static const struct tagOIDDescriptor {
938 CONST BYTE abOID[18];
939 } aOIDDescriptor[5] = {
940 { CALG_MD2, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
941 0x86, 0xf7, 0x0d, 0x02, 0x02, 0x05, 0x00, 0x04, 0x10 } },
942 { CALG_MD4, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
943 0x86, 0xf7, 0x0d, 0x02, 0x04, 0x05, 0x00, 0x04, 0x10 } },
944 { CALG_MD5, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
945 0x86, 0xf7, 0x0d, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10 } },
946 { CALG_SHA, 15, { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03,
947 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14 } },
950 DWORD dwIdxOID, i, j;
952 for (dwIdxOID = 0; aOIDDescriptor[dwIdxOID].aiAlgid; dwIdxOID++) {
953 if (aOIDDescriptor[dwIdxOID].aiAlgid == aiAlgid) break;
956 if (!aOIDDescriptor[dwIdxOID].aiAlgid) {
957 SetLastError(NTE_BAD_ALGID);
961 /* Build the padded signature */
962 if (dwFlags & CRYPT_X931_FORMAT) {
963 pbSignature[0] = 0x6b;
964 for (i=1; i < dwLen - dwHashLen - 3; i++) {
965 pbSignature[i] = 0xbb;
967 pbSignature[i++] = 0xba;
968 for (j=0; j < dwHashLen; j++, i++) {
969 pbSignature[i] = abHashValue[j];
971 pbSignature[i++] = 0x33;
972 pbSignature[i++] = 0xcc;
974 pbSignature[0] = 0x00;
975 pbSignature[1] = 0x01;
976 if (dwFlags & CRYPT_NOHASHOID) {
977 for (i=2; i < dwLen - 1 - dwHashLen; i++) {
978 pbSignature[i] = 0xff;
980 pbSignature[i++] = 0x00;
982 for (i=2; i < dwLen - 1 - aOIDDescriptor[dwIdxOID].dwLen - dwHashLen; i++) {
983 pbSignature[i] = 0xff;
985 pbSignature[i++] = 0x00;
986 for (j=0; j < aOIDDescriptor[dwIdxOID].dwLen; j++) {
987 pbSignature[i++] = aOIDDescriptor[dwIdxOID].abOID[j];
990 for (j=0; j < dwHashLen; j++) {
991 pbSignature[i++] = abHashValue[j];
998 /******************************************************************************
999 * CPAcquireContext (RSAENH.@)
1001 * Acquire a handle to the key container specified by pszContainer
1004 * phProv [O] Pointer to the location the acquired handle will be written to.
1005 * pszContainer [I] Name of the desired key container. See Notes
1006 * dwFlags [I] Flags. See Notes.
1007 * pVTable [I] Pointer to a PVTableProvStruct containing callbacks.
1014 * If pszContainer is NULL or points to a zero length string the user's login
1015 * name will be used as the key container name.
1017 * If the CRYPT_NEW_KEYSET flag is set in dwFlags a new keyset will be created.
1018 * If a keyset with the given name already exists, the function fails and sets
1019 * last error to NTE_EXISTS. If CRYPT_NEW_KEYSET is not set and the specified
1020 * key container does not exist, function fails and sets last error to
1023 BOOL WINAPI RSAENH_CPAcquireContext(HCRYPTPROV *phProv, LPSTR pszContainer,
1024 DWORD dwFlags, PVTableProvStruc pVTable)
1027 CHAR szKeyContainerName[MAX_PATH] = "";
1028 CHAR szRegKey[MAX_PATH];
1030 TRACE("(phProv=%p, pszContainer=%s, dwFlags=%08lx, pVTable=%p)\n", phProv,
1031 debugstr_a(pszContainer), dwFlags, pVTable);
1033 if (pszContainer ? strlen(pszContainer) : 0)
1035 strncpy(szKeyContainerName, pszContainer, MAX_PATH);
1036 szKeyContainerName[MAX_PATH-1] = '\0';
1041 if (!GetUserNameA(szKeyContainerName, &dwLen)) return FALSE;
1044 switch (dwFlags & (CRYPT_NEWKEYSET|CRYPT_VERIFYCONTEXT|CRYPT_DELETEKEYSET))
1047 *phProv = read_key_container(szKeyContainerName, dwFlags, pVTable);
1050 case CRYPT_DELETEKEYSET:
1051 if (snprintf(szRegKey, MAX_PATH, RSAENH_REGKEY, pszContainer) >= MAX_PATH) {
1052 SetLastError(NTE_BAD_KEYSET_PARAM);
1055 RegDeleteKeyA(HKEY_CURRENT_USER, szRegKey);
1056 SetLastError(ERROR_SUCCESS);
1061 case CRYPT_NEWKEYSET:
1062 *phProv = read_key_container(szKeyContainerName, dwFlags, pVTable);
1063 if (*phProv != (HCRYPTPROV)INVALID_HANDLE_VALUE)
1065 release_handle(&handle_table, (unsigned int)*phProv, RSAENH_MAGIC_CONTAINER);
1066 SetLastError(NTE_EXISTS);
1069 *phProv = new_key_container(szKeyContainerName, dwFlags, pVTable);
1072 case CRYPT_VERIFYCONTEXT:
1074 SetLastError(NTE_BAD_FLAGS);
1077 *phProv = new_key_container("", dwFlags, pVTable);
1081 *phProv = (unsigned int)INVALID_HANDLE_VALUE;
1082 SetLastError(NTE_BAD_FLAGS);
1086 if (*phProv != (unsigned int)INVALID_HANDLE_VALUE) {
1087 SetLastError(ERROR_SUCCESS);
1094 /******************************************************************************
1095 * CPCreateHash (RSAENH.@)
1097 * CPCreateHash creates and initalizes a new hash object.
1100 * hProv [I] Handle to the key container to which the new hash will belong.
1101 * Algid [I] Identifies the hash algorithm, which will be used for the hash.
1102 * hKey [I] Handle to a session key applied for keyed hashes.
1103 * dwFlags [I] Currently no flags defined. Must be zero.
1104 * phHash [O] Points to the location where a handle to the new hash will be stored.
1111 * hKey is a handle to a session key applied in keyed hashes like MAC and HMAC.
1112 * If a normal hash object is to be created (like e.g. MD2 or SHA1) hKey must be zero.
1114 BOOL WINAPI RSAENH_CPCreateHash(HCRYPTPROV hProv, ALG_ID Algid, HCRYPTKEY hKey, DWORD dwFlags,
1117 KEYCONTAINER *pKeyContainer;
1118 CRYPTHASH *pCryptHash;
1119 const PROV_ENUMALGS_EX *peaAlgidInfo;
1121 TRACE("(hProv=%08lx, Algid=%08x, hKey=%08lx, dwFlags=%08lx, phHash=%p)\n", hProv, Algid, hKey,
1124 if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER, (OBJECTHDR**)&pKeyContainer))
1126 SetLastError(NTE_BAD_UID);
1130 peaAlgidInfo = get_algid_info(pKeyContainer, Algid);
1133 SetLastError(NTE_BAD_ALGID);
1139 SetLastError(NTE_BAD_FLAGS);
1143 if ((Algid == CALG_MAC || Algid == CALG_HMAC)) {
1144 CRYPTKEY *pCryptKey;
1146 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey)) {
1147 SetLastError(NTE_BAD_KEY);
1151 if ((Algid == CALG_MAC) && (GET_ALG_TYPE(pCryptKey->aiAlgid) != ALG_TYPE_BLOCK)) {
1152 SetLastError(NTE_BAD_KEY);
1157 *phHash = (HCRYPTHASH)new_object(&handle_table, sizeof(CRYPTHASH), RSAENH_MAGIC_HASH,
1158 destroy_hash, (OBJECTHDR**)&pCryptHash);
1159 if (!pCryptHash) return FALSE;
1161 pCryptHash->aiAlgid = Algid;
1162 pCryptHash->hKey = hKey;
1163 pCryptHash->hProv = hProv;
1164 pCryptHash->dwState = RSAENH_HASHSTATE_IDLE;
1165 pCryptHash->pHMACInfo = (PHMAC_INFO)NULL;
1166 pCryptHash->dwHashSize = peaAlgidInfo->dwDefaultLen >> 3;
1168 return init_hash(pKeyContainer, pCryptHash);
1171 /******************************************************************************
1172 * CPDestroyHash (RSAENH.@)
1174 * Releases the handle to a hash object. The object is destroyed if it's reference
1175 * count reaches zero.
1178 * hProv [I] Handle to the key container to which the hash object belongs.
1179 * hHash [I] Handle to the hash object to be released.
1185 BOOL WINAPI RSAENH_CPDestroyHash(HCRYPTPROV hProv, HCRYPTHASH hHash)
1187 TRACE("(hProv=%08lx, hHash=%08lx)\n", hProv, hHash);
1189 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
1191 SetLastError(NTE_BAD_UID);
1195 if (!release_handle(&handle_table, hHash, RSAENH_MAGIC_HASH))
1197 SetLastError(NTE_BAD_HASH);
1204 /******************************************************************************
1205 * CPDestroyKey (RSAENH.@)
1207 * Releases the handle to a key object. The object is destroyed if it's reference
1208 * count reaches zero.
1211 * hProv [I] Handle to the key container to which the key object belongs.
1212 * hKey [I] Handle to the key object to be released.
1218 BOOL WINAPI RSAENH_CPDestroyKey(HCRYPTPROV hProv, HCRYPTKEY hKey)
1220 TRACE("(hProv=%08lx, hKey=%08lx)\n", hProv, hKey);
1222 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
1224 SetLastError(NTE_BAD_UID);
1228 if (!release_handle(&handle_table, hKey, RSAENH_MAGIC_KEY))
1230 SetLastError(NTE_BAD_KEY);
1237 /******************************************************************************
1238 * CPDuplicateHash (RSAENH.@)
1240 * Clones a hash object including it's current state.
1243 * hUID [I] Handle to the key container the hash belongs to.
1244 * hHash [I] Handle to the hash object to be cloned.
1245 * pdwReserved [I] Reserved. Must be NULL.
1246 * dwFlags [I] No flags are currently defined. Must be 0.
1247 * phHash [O] Handle to the cloned hash object.
1253 BOOL WINAPI RSAENH_CPDuplicateHash(HCRYPTPROV hUID, HCRYPTHASH hHash, DWORD *pdwReserved,
1254 DWORD dwFlags, HCRYPTHASH *phHash)
1256 CRYPTHASH *pSrcHash, *pDestHash;
1258 TRACE("(hUID=%08lx, hHash=%08lx, pdwReserved=%p, dwFlags=%08lx, phHash=%p)\n", hUID, hHash,
1259 pdwReserved, dwFlags, phHash);
1261 if (!is_valid_handle(&handle_table, hUID, RSAENH_MAGIC_CONTAINER))
1263 SetLastError(NTE_BAD_UID);
1267 if (!lookup_handle(&handle_table, hHash, RSAENH_MAGIC_HASH, (OBJECTHDR**)&pSrcHash))
1269 SetLastError(NTE_BAD_HASH);
1273 if (!phHash || pdwReserved || dwFlags)
1275 SetLastError(ERROR_INVALID_PARAMETER);
1279 *phHash = (HCRYPTHASH)new_object(&handle_table, sizeof(CRYPTHASH), RSAENH_MAGIC_HASH,
1280 destroy_hash, (OBJECTHDR**)&pDestHash);
1281 if (*phHash != (HCRYPTHASH)INVALID_HANDLE_VALUE)
1283 memcpy(pDestHash, pSrcHash, sizeof(CRYPTHASH));
1284 duplicate_hash_impl(pSrcHash->aiAlgid, &pSrcHash->context, &pDestHash->context);
1285 copy_hmac_info(&pDestHash->pHMACInfo, pSrcHash->pHMACInfo);
1288 return *phHash != (HCRYPTHASH)INVALID_HANDLE_VALUE;
1291 /******************************************************************************
1292 * CPDuplicateKey (RSAENH.@)
1294 * Clones a key object including it's current state.
1297 * hUID [I] Handle to the key container the hash belongs to.
1298 * hKey [I] Handle to the key object to be cloned.
1299 * pdwReserved [I] Reserved. Must be NULL.
1300 * dwFlags [I] No flags are currently defined. Must be 0.
1301 * phHash [O] Handle to the cloned key object.
1307 BOOL WINAPI RSAENH_CPDuplicateKey(HCRYPTPROV hUID, HCRYPTKEY hKey, DWORD *pdwReserved,
1308 DWORD dwFlags, HCRYPTKEY *phKey)
1310 CRYPTKEY *pSrcKey, *pDestKey;
1312 TRACE("(hUID=%08lx, hKey=%08lx, pdwReserved=%p, dwFlags=%08lx, phKey=%p)\n", hUID, hKey,
1313 pdwReserved, dwFlags, phKey);
1315 if (!is_valid_handle(&handle_table, hUID, RSAENH_MAGIC_CONTAINER))
1317 SetLastError(NTE_BAD_UID);
1321 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pSrcKey))
1323 SetLastError(NTE_BAD_KEY);
1327 if (!phKey || pdwReserved || dwFlags)
1329 SetLastError(ERROR_INVALID_PARAMETER);
1333 *phKey = (HCRYPTKEY)new_object(&handle_table, sizeof(CRYPTKEY), RSAENH_MAGIC_KEY, destroy_key,
1334 (OBJECTHDR**)&pDestKey);
1335 if (*phKey != (HCRYPTKEY)INVALID_HANDLE_VALUE)
1337 memcpy(pDestKey, pSrcKey, sizeof(CRYPTKEY));
1338 duplicate_key_impl(pSrcKey->aiAlgid, &pSrcKey->context, &pDestKey->context);
1347 /******************************************************************************
1348 * CPEncrypt (RSAENH.@)
1353 * hProv [I] The key container hKey and hHash belong to.
1354 * hKey [I] The key used to encrypt the data.
1355 * hHash [I] An optional hash object for parallel hashing. See notes.
1356 * Final [I] Indicates if this is the last block of data to encrypt.
1357 * dwFlags [I] Currently no flags defined. Must be zero.
1358 * pbData [I/O] Pointer to the data to encrypt. Encrypted data will also be stored there.
1359 * pdwDataLen [I/O] I: Length of data to encrypt, O: Length of encrypted data.
1360 * dwBufLen [I] Size of the buffer at pbData.
1367 * If a hash object handle is provided in hHash, it will be updated with the plaintext.
1368 * This is useful for message signatures.
1370 * This function uses the standard WINAPI protocol for querying data of dynamic length.
1372 BOOL WINAPI RSAENH_CPEncrypt(HCRYPTPROV hProv, HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final,
1373 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen, DWORD dwBufLen)
1375 CRYPTKEY *pCryptKey;
1376 BYTE *in, out[RSAENH_MAX_BLOCK_SIZE], o[RSAENH_MAX_BLOCK_SIZE];
1377 DWORD dwEncryptedLen, i, j, k;
1379 TRACE("(hProv=%08lx, hKey=%08lx, hHash=%08lx, Final=%d, dwFlags=%08lx, pbData=%p, "
1380 "pdwDataLen=%p, dwBufLen=%ld)\n", hProv, hKey, hHash, Final, dwFlags, pbData, pdwDataLen,
1383 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
1385 SetLastError(NTE_BAD_UID);
1391 SetLastError(NTE_BAD_FLAGS);
1395 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
1397 SetLastError(NTE_BAD_KEY);
1401 if (pCryptKey->dwState == RSAENH_KEYSTATE_IDLE)
1402 pCryptKey->dwState = RSAENH_KEYSTATE_ENCRYPTING;
1404 if (pCryptKey->dwState != RSAENH_KEYSTATE_ENCRYPTING)
1406 SetLastError(NTE_BAD_DATA);
1410 if (is_valid_handle(&handle_table, hHash, RSAENH_MAGIC_HASH)) {
1411 if (!RSAENH_CPHashData(hProv, hHash, pbData, *pdwDataLen, 0)) return FALSE;
1414 if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_BLOCK) {
1415 if (!Final && (*pdwDataLen % pCryptKey->dwBlockLen)) {
1416 SetLastError(NTE_BAD_DATA);
1420 dwEncryptedLen = (*pdwDataLen/pCryptKey->dwBlockLen+(Final?1:0))*pCryptKey->dwBlockLen;
1421 for (i=*pdwDataLen; i<dwEncryptedLen; i++) pbData[i] = dwEncryptedLen - *pdwDataLen;
1422 *pdwDataLen = dwEncryptedLen;
1424 if (*pdwDataLen > dwBufLen)
1426 SetLastError(ERROR_MORE_DATA);
1430 for (i=0, in=pbData; i<*pdwDataLen; i+=pCryptKey->dwBlockLen, in+=pCryptKey->dwBlockLen) {
1431 switch (pCryptKey->dwMode) {
1432 case CRYPT_MODE_ECB:
1433 encrypt_block_impl(pCryptKey->aiAlgid, &pCryptKey->context, in, out,
1437 case CRYPT_MODE_CBC:
1438 for (j=0; j<pCryptKey->dwBlockLen; j++) in[j] ^= pCryptKey->abChainVector[j];
1439 encrypt_block_impl(pCryptKey->aiAlgid, &pCryptKey->context, in, out,
1441 memcpy(pCryptKey->abChainVector, out, pCryptKey->dwBlockLen);
1444 case CRYPT_MODE_CFB:
1445 for (j=0; j<pCryptKey->dwBlockLen; j++) {
1446 encrypt_block_impl(pCryptKey->aiAlgid, &pCryptKey->context,
1447 pCryptKey->abChainVector, o, RSAENH_ENCRYPT);
1448 out[j] = in[j] ^ o[0];
1449 for (k=0; k<pCryptKey->dwBlockLen-1; k++)
1450 pCryptKey->abChainVector[k] = pCryptKey->abChainVector[k+1];
1451 pCryptKey->abChainVector[k] = out[j];
1456 SetLastError(NTE_BAD_ALGID);
1459 memcpy(in, out, pCryptKey->dwBlockLen);
1461 } else if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_STREAM) {
1462 encrypt_stream_impl(pCryptKey->aiAlgid, &pCryptKey->context, pbData, *pdwDataLen);
1465 if (Final) setup_key(pCryptKey);
1470 /******************************************************************************
1471 * CPDecrypt (RSAENH.@)
1476 * hProv [I] The key container hKey and hHash belong to.
1477 * hKey [I] The key used to decrypt the data.
1478 * hHash [I] An optional hash object for parallel hashing. See notes.
1479 * Final [I] Indicates if this is the last block of data to decrypt.
1480 * dwFlags [I] Currently no flags defined. Must be zero.
1481 * pbData [I/O] Pointer to the data to decrypt. Plaintext will also be stored there.
1482 * pdwDataLen [I/O] I: Length of ciphertext, O: Length of plaintext.
1489 * If a hash object handle is provided in hHash, it will be updated with the plaintext.
1490 * This is useful for message signatures.
1492 * This function uses the standard WINAPI protocol for querying data of dynamic length.
1494 BOOL WINAPI RSAENH_CPDecrypt(HCRYPTPROV hProv, HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final,
1495 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
1497 CRYPTKEY *pCryptKey;
1498 BYTE *in, out[RSAENH_MAX_BLOCK_SIZE], o[RSAENH_MAX_BLOCK_SIZE];
1501 TRACE("(hProv=%08lx, hKey=%08lx, hHash=%08lx, Final=%d, dwFlags=%08lx, pbData=%p, "
1502 "pdwDataLen=%p)\n", hProv, hKey, hHash, Final, dwFlags, pbData, pdwDataLen);
1504 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
1506 SetLastError(NTE_BAD_UID);
1512 SetLastError(NTE_BAD_FLAGS);
1516 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
1518 SetLastError(NTE_BAD_KEY);
1522 if (pCryptKey->dwState == RSAENH_KEYSTATE_IDLE)
1523 pCryptKey->dwState = RSAENH_KEYSTATE_DECRYPTING;
1525 if (pCryptKey->dwState != RSAENH_KEYSTATE_DECRYPTING)
1527 SetLastError(NTE_BAD_DATA);
1531 if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_BLOCK) {
1532 for (i=0, in=pbData; i<*pdwDataLen; i+=pCryptKey->dwBlockLen, in+=pCryptKey->dwBlockLen) {
1533 switch (pCryptKey->dwMode) {
1534 case CRYPT_MODE_ECB:
1535 encrypt_block_impl(pCryptKey->aiAlgid, &pCryptKey->context, in, out,
1539 case CRYPT_MODE_CBC:
1540 encrypt_block_impl(pCryptKey->aiAlgid, &pCryptKey->context, in, out,
1542 for (j=0; j<pCryptKey->dwBlockLen; j++) out[j] ^= pCryptKey->abChainVector[j];
1543 memcpy(pCryptKey->abChainVector, in, pCryptKey->dwBlockLen);
1546 case CRYPT_MODE_CFB:
1547 for (j=0; j<pCryptKey->dwBlockLen; j++) {
1548 encrypt_block_impl(pCryptKey->aiAlgid, &pCryptKey->context,
1549 pCryptKey->abChainVector, o, RSAENH_ENCRYPT);
1550 out[j] = in[j] ^ o[0];
1551 for (k=0; k<pCryptKey->dwBlockLen-1; k++)
1552 pCryptKey->abChainVector[k] = pCryptKey->abChainVector[k+1];
1553 pCryptKey->abChainVector[k] = in[j];
1558 SetLastError(NTE_BAD_ALGID);
1561 memcpy(in, out, pCryptKey->dwBlockLen);
1563 if (Final) *pdwDataLen -= pbData[*pdwDataLen-1];
1565 } else if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_STREAM) {
1566 encrypt_stream_impl(pCryptKey->aiAlgid, &pCryptKey->context, pbData, *pdwDataLen);
1567 } else if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_RSA) {
1568 encrypt_block_impl(pCryptKey->aiAlgid, &pCryptKey->context, pbData, pbData, RSAENH_DECRYPT);
1570 if (Final) setup_key(pCryptKey);
1572 if (is_valid_handle(&handle_table, hHash, RSAENH_MAGIC_HASH)) {
1573 if (!RSAENH_CPHashData(hProv, hHash, pbData, *pdwDataLen, 0)) return FALSE;
1579 /******************************************************************************
1580 * CPExportKey (RSAENH.@)
1582 * Export a key into a binary large object (BLOB).
1585 * hProv [I] Key container from which a key is to be exported.
1586 * hKey [I] Key to be exported.
1587 * hPubKey [I] Key used to encrypt sensitive BLOB data.
1588 * dwBlobType [I] SIMPLEBLOB, PUBLICKEYBLOB or PRIVATEKEYBLOB.
1589 * dwFlags [I] Currently none defined.
1590 * pbData [O] Pointer to a buffer where the BLOB will be written to.
1591 * pdwDataLen [I/O] I: Size of buffer at pbData, O: Size of BLOB
1597 BOOL WINAPI RSAENH_CPExportKey(HCRYPTPROV hProv, HCRYPTKEY hKey, HCRYPTKEY hPubKey,
1598 DWORD dwBlobType, DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
1600 CRYPTKEY *pCryptKey, *pPubKey;
1601 BLOBHEADER *pBlobHeader = (BLOBHEADER*)pbData;
1602 RSAPUBKEY *pRSAPubKey = (RSAPUBKEY*)(pBlobHeader+1);
1603 ALG_ID *pAlgid = (ALG_ID*)(pBlobHeader+1);
1607 TRACE("(hProv=%08lx, hKey=%08lx, hPubKey=%08lx, dwBlobType=%08lx, dwFlags=%08lx, pbData=%p,"
1608 "pdwDataLen=%p)\n", hProv, hKey, hPubKey, dwBlobType, dwFlags, pbData, pdwDataLen);
1610 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
1612 SetLastError(NTE_BAD_UID);
1616 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
1618 SetLastError(NTE_BAD_KEY);
1622 switch ((BYTE)dwBlobType)
1625 if (!lookup_handle(&handle_table, hPubKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pPubKey)){
1626 SetLastError(NTE_BAD_PUBLIC_KEY); /* FIXME: error_code? */
1630 if (GET_ALG_CLASS(pCryptKey->aiAlgid) != ALG_CLASS_DATA_ENCRYPT) {
1631 SetLastError(NTE_BAD_KEY); /* FIXME: error code? */
1635 dwDataLen = sizeof(BLOBHEADER) + sizeof(ALG_ID) + pPubKey->dwBlockLen;
1637 if (*pdwDataLen < dwDataLen) {
1638 SetLastError(ERROR_MORE_DATA);
1639 *pdwDataLen = dwDataLen;
1643 pBlobHeader->bType = SIMPLEBLOB;
1644 pBlobHeader->bVersion = CUR_BLOB_VERSION;
1645 pBlobHeader->reserved = 0;
1646 pBlobHeader->aiKeyAlg = pCryptKey->aiAlgid;
1648 *pAlgid = pPubKey->aiAlgid;
1650 pbRawData = (BYTE*)(pAlgid+1);
1651 pbRawData[0] = 0x00;
1652 pbRawData[1] = RSAENH_PKC_BLOCKTYPE;
1653 for (i=2; i < pPubKey->dwBlockLen - pCryptKey->dwKeyLen - 1; i++)
1654 do gen_rand_impl(&pbRawData[i], 1); while (!pbRawData[i]);
1655 pbRawData[i] = 0x00;
1656 for (i=0; i<pCryptKey->dwKeyLen; i++)
1657 pbRawData[pPubKey->dwBlockLen - pCryptKey->dwKeyLen + i] =
1658 pCryptKey->abKeyValue[i];
1660 encrypt_block_impl(pPubKey->aiAlgid, &pPubKey->context, pbRawData, pbRawData,
1663 *pdwDataLen = dwDataLen;
1667 if (is_valid_handle(&handle_table, hPubKey, RSAENH_MAGIC_KEY)) {
1668 SetLastError(NTE_BAD_KEY); /* FIXME: error code? */
1672 if ((pCryptKey->aiAlgid != CALG_RSA_KEYX) && (pCryptKey->aiAlgid != CALG_RSA_SIGN)) {
1673 SetLastError(NTE_BAD_KEY);
1677 dwDataLen = sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) + pCryptKey->dwKeyLen;
1679 if (*pdwDataLen < dwDataLen) {
1680 SetLastError(ERROR_MORE_DATA);
1681 *pdwDataLen = dwDataLen;
1685 pBlobHeader->bType = PUBLICKEYBLOB;
1686 pBlobHeader->bVersion = CUR_BLOB_VERSION;
1687 pBlobHeader->reserved = 0;
1688 pBlobHeader->aiKeyAlg = pCryptKey->aiAlgid;
1690 pRSAPubKey->magic = RSAENH_MAGIC_RSA1;
1691 pRSAPubKey->bitlen = pCryptKey->dwKeyLen << 3;
1693 export_public_key_impl((BYTE*)(pRSAPubKey+1), &pCryptKey->context,
1694 pCryptKey->dwKeyLen, &pRSAPubKey->pubexp);
1696 *pdwDataLen = dwDataLen;
1699 case PRIVATEKEYBLOB:
1700 if ((pCryptKey->aiAlgid != CALG_RSA_KEYX) && (pCryptKey->aiAlgid != CALG_RSA_SIGN)) {
1701 SetLastError(NTE_BAD_KEY);
1705 dwDataLen = sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) +
1706 2 * pCryptKey->dwKeyLen + 5 * ((pCryptKey->dwKeyLen + 1) >> 1);
1708 if (*pdwDataLen < dwDataLen) {
1709 SetLastError(ERROR_MORE_DATA);
1710 *pdwDataLen = dwDataLen;
1714 pBlobHeader->bType = PRIVATEKEYBLOB;
1715 pBlobHeader->bVersion = CUR_BLOB_VERSION;
1716 pBlobHeader->reserved = 0;
1717 pBlobHeader->aiKeyAlg = pCryptKey->aiAlgid;
1719 pRSAPubKey->magic = RSAENH_MAGIC_RSA2;
1720 pRSAPubKey->bitlen = pCryptKey->dwKeyLen << 3;
1722 export_private_key_impl((BYTE*)(pRSAPubKey+1), &pCryptKey->context,
1723 pCryptKey->dwKeyLen, &pRSAPubKey->pubexp);
1725 *pdwDataLen = dwDataLen;
1729 SetLastError(NTE_BAD_TYPE); /* FIXME: error code? */
1734 /******************************************************************************
1735 * CPImportKey (RSAENH.@)
1737 * Import a BLOB'ed key into a key container.
1740 * hProv [I] Key container into which the key is to be imported.
1741 * pbData [I] Pointer to a buffer which holds the BLOB.
1742 * dwDataLen [I] Length of data in buffer at pbData.
1743 * hPubKey [I] Key used to decrypt sensitive BLOB data.
1744 * dwFlags [I] Currently none defined.
1745 * phKey [O] Handle to the imported key.
1751 BOOL WINAPI RSAENH_CPImportKey(HCRYPTPROV hProv, CONST BYTE *pbData, DWORD dwDataLen,
1752 HCRYPTKEY hPubKey, DWORD dwFlags, HCRYPTKEY *phKey)
1754 KEYCONTAINER *pKeyContainer;
1755 CRYPTKEY *pCryptKey, *pPubKey;
1756 CONST BLOBHEADER *pBlobHeader = (CONST BLOBHEADER*)pbData;
1757 CONST RSAPUBKEY *pRSAPubKey = (CONST RSAPUBKEY*)(pBlobHeader+1);
1758 CONST ALG_ID *pAlgid = (CONST ALG_ID*)(pBlobHeader+1);
1759 CONST BYTE *pbKeyStream = (CONST BYTE*)(pAlgid + 1);
1763 TRACE("(hProv=%08lx, pbData=%p, dwDataLen=%ld, hPubKey=%08lx, dwFlags=%08lx, phKey=%p)\n",
1764 hProv, pbData, dwDataLen, hPubKey, dwFlags, phKey);
1766 if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER, (OBJECTHDR**)&pKeyContainer))
1768 SetLastError(NTE_BAD_UID);
1772 if (dwDataLen < sizeof(BLOBHEADER) ||
1773 pBlobHeader->bVersion != CUR_BLOB_VERSION ||
1774 pBlobHeader->reserved != 0)
1776 SetLastError(NTE_BAD_DATA);
1780 switch (pBlobHeader->bType)
1782 case PRIVATEKEYBLOB:
1783 if ((dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY)) ||
1784 (pRSAPubKey->magic != RSAENH_MAGIC_RSA2) ||
1785 (dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) +
1786 (2 * pRSAPubKey->bitlen >> 3) + (5 * ((pRSAPubKey->bitlen+8)>>4))))
1788 SetLastError(NTE_BAD_DATA);
1792 *phKey = new_key(hProv, pBlobHeader->aiKeyAlg, MAKELONG(0,pRSAPubKey->bitlen), &pCryptKey);
1793 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) return FALSE;
1794 setup_key(pCryptKey);
1795 return import_private_key_impl((CONST BYTE*)(pRSAPubKey+1), &pCryptKey->context,
1796 pRSAPubKey->bitlen/8, pRSAPubKey->pubexp);
1799 if ((dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY)) ||
1800 (pRSAPubKey->magic != RSAENH_MAGIC_RSA1) ||
1801 (dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) + (pRSAPubKey->bitlen >> 3)))
1803 SetLastError(NTE_BAD_DATA);
1807 *phKey = new_key(hProv, pBlobHeader->aiKeyAlg, MAKELONG(0,pRSAPubKey->bitlen), &pCryptKey);
1808 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) return FALSE;
1809 setup_key(pCryptKey);
1810 return import_public_key_impl((CONST BYTE*)(pRSAPubKey+1), &pCryptKey->context,
1811 pRSAPubKey->bitlen >> 3, pRSAPubKey->pubexp);
1814 if (!lookup_handle(&handle_table, hPubKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pPubKey) ||
1815 pPubKey->aiAlgid != CALG_RSA_KEYX)
1817 SetLastError(NTE_BAD_PUBLIC_KEY); /* FIXME: error code? */
1821 if (dwDataLen < sizeof(BLOBHEADER)+sizeof(ALG_ID)+pPubKey->dwBlockLen)
1823 SetLastError(NTE_BAD_DATA); /* FIXME: error code */
1827 pbDecrypted = (BYTE*)HeapAlloc(GetProcessHeap(), 0, pPubKey->dwBlockLen);
1828 if (!pbDecrypted) return FALSE;
1829 encrypt_block_impl(pPubKey->aiAlgid, &pPubKey->context, pbKeyStream, pbDecrypted,
1832 for (i=2; i<pPubKey->dwBlockLen && pbDecrypted[i]; i++);
1833 if ((i==pPubKey->dwBlockLen) ||
1834 (pbDecrypted[0] != 0x00) ||
1835 (pbDecrypted[1] != RSAENH_PKC_BLOCKTYPE))
1837 HeapFree(GetProcessHeap(), 0, pbDecrypted);
1838 SetLastError(NTE_BAD_DATA); /* FIXME: error code */
1842 dwKeyLen = pPubKey->dwBlockLen-i-1;
1843 *phKey = new_key(hProv, pBlobHeader->aiKeyAlg, dwKeyLen<<19, &pCryptKey);
1844 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE)
1846 HeapFree(GetProcessHeap(), 0, pbDecrypted);
1849 memcpy(pCryptKey->abKeyValue, pbDecrypted+i+1, dwKeyLen);
1850 HeapFree(GetProcessHeap(), 0, pbDecrypted);
1851 setup_key(pCryptKey);
1855 SetLastError(NTE_BAD_TYPE); /* FIXME: error code? */
1860 /******************************************************************************
1861 * CPGenKey (RSAENH.@)
1863 * Generate a key in the key container
1866 * hProv [I] Key container for which a key is to be generated.
1867 * Algid [I] Crypto algorithm identifier for the key to be generated.
1868 * dwFlags [I] Upper 16 bits: Binary length of key. Lower 16 bits: Flags. See Notes
1869 * phKey [O] Handle to the generated key.
1876 * Flags currently not considered.
1879 * Private key-exchange- and signature-keys can be generated with Algid AT_KEYEXCHANGE
1880 * and AT_SIGNATURE values.
1882 BOOL WINAPI RSAENH_CPGenKey(HCRYPTPROV hProv, ALG_ID Algid, DWORD dwFlags, HCRYPTKEY *phKey)
1884 KEYCONTAINER *pKeyContainer;
1885 CRYPTKEY *pCryptKey;
1887 TRACE("(hProv=%08lx, aiAlgid=%d, dwFlags=%08lx, phKey=%p)\n", hProv, Algid, dwFlags, phKey);
1889 if (!lookup_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER,
1890 (OBJECTHDR**)&pKeyContainer))
1892 /* MSDN: hProv not containing valid context handle */
1893 SetLastError(NTE_BAD_UID);
1901 *phKey = new_key(hProv, CALG_RSA_SIGN, dwFlags, &pCryptKey);
1903 new_key_impl(pCryptKey->aiAlgid, &pCryptKey->context, pCryptKey->dwKeyLen);
1904 setup_key(pCryptKey);
1905 if (Algid == AT_SIGNATURE) {
1906 RSAENH_CPDestroyKey(hProv, pKeyContainer->hSignatureKeyPair);
1907 copy_handle(&handle_table, *phKey, RSAENH_MAGIC_KEY,
1908 (unsigned int*)&pKeyContainer->hSignatureKeyPair);
1913 case AT_KEYEXCHANGE:
1915 *phKey = new_key(hProv, CALG_RSA_KEYX, dwFlags, &pCryptKey);
1917 new_key_impl(pCryptKey->aiAlgid, &pCryptKey->context, pCryptKey->dwKeyLen);
1918 setup_key(pCryptKey);
1919 if (Algid == AT_KEYEXCHANGE) {
1920 RSAENH_CPDestroyKey(hProv, pKeyContainer->hKeyExchangeKeyPair);
1921 copy_handle(&handle_table, *phKey, RSAENH_MAGIC_KEY,
1922 (unsigned int*)&pKeyContainer->hKeyExchangeKeyPair);
1932 *phKey = new_key(hProv, Algid, dwFlags, &pCryptKey);
1934 gen_rand_impl(pCryptKey->abKeyValue, RSAENH_MAX_KEY_SIZE);
1935 setup_key(pCryptKey);
1940 /* MSDN: Algorithm not supported specified by Algid */
1941 SetLastError(NTE_BAD_ALGID);
1945 return *phKey != (unsigned int)INVALID_HANDLE_VALUE;
1948 /******************************************************************************
1949 * CPGenRandom (RSAENH.@)
1951 * Generate a random byte stream.
1954 * hProv [I] Key container that is used to generate random bytes.
1955 * dwLen [I] Specifies the number of requested random data bytes.
1956 * pbBuffer [O] Random bytes will be stored here.
1962 BOOL WINAPI RSAENH_CPGenRandom(HCRYPTPROV hProv, DWORD dwLen, BYTE *pbBuffer)
1964 KEYCONTAINER *pKeyContainer;
1966 TRACE("(hProv=%08lx, dwLen=%ld, pbBuffer=%p)\n", hProv, dwLen, pbBuffer);
1968 if (!lookup_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER,
1969 (OBJECTHDR**)&pKeyContainer))
1971 /* MSDN: hProv not containing valid context handle */
1972 SetLastError(NTE_BAD_UID);
1976 return gen_rand_impl(pbBuffer, dwLen);
1979 /******************************************************************************
1980 * CPGetHashParam (RSAENH.@)
1982 * Query parameters of an hash object.
1985 * hProv [I] The kea container, which the hash belongs to.
1986 * hHash [I] The hash object that is to be queried.
1987 * dwParam [I] Specifies the parameter that is to be queried.
1988 * pbData [I] Pointer to the buffer where the parameter value will be stored.
1989 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
1990 * dwFlags [I] None currently defined.
1997 * Valid dwParams are: HP_ALGID, HP_HASHSIZE, HP_HASHVALUE. The hash will be
1998 * finalized if HP_HASHVALUE is queried.
2000 BOOL WINAPI RSAENH_CPGetHashParam(HCRYPTPROV hProv, HCRYPTHASH hHash, DWORD dwParam, BYTE *pbData,
2001 DWORD *pdwDataLen, DWORD dwFlags)
2003 CRYPTHASH *pCryptHash;
2004 KEYCONTAINER *pKeyContainer;
2005 BYTE abHashValue[RSAENH_MAX_HASH_SIZE];
2007 TRACE("(hProv=%08lx, hHash=%08lx, dwParam=%08lx, pbData=%p, pdwDataLen=%p, dwFlags=%08lx)\n",
2008 hProv, hHash, dwParam, pbData, pdwDataLen, dwFlags);
2010 if (!lookup_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER,
2011 (OBJECTHDR**)&pKeyContainer))
2013 SetLastError(NTE_BAD_UID);
2019 SetLastError(NTE_BAD_FLAGS);
2023 if (!lookup_handle(&handle_table, (unsigned int)hHash, RSAENH_MAGIC_HASH,
2024 (OBJECTHDR**)&pCryptHash))
2026 SetLastError(NTE_BAD_HASH);
2032 SetLastError(ERROR_INVALID_PARAMETER);
2039 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptHash->aiAlgid,
2043 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptHash->dwHashSize,
2047 if (pCryptHash->dwState == RSAENH_HASHSTATE_IDLE) {
2048 SetLastError(NTE_BAD_HASH_STATE);
2052 if (pbData && (pCryptHash->dwState != RSAENH_HASHSTATE_FINISHED))
2054 finalize_hash(pCryptHash);
2055 if (pCryptHash->aiAlgid == CALG_HMAC) {
2056 memcpy(abHashValue, pCryptHash->abHashValue, pCryptHash->dwHashSize);
2057 init_hash(pKeyContainer, pCryptHash);
2058 update_hash(pCryptHash, pCryptHash->pHMACInfo->pbOuterString,
2059 pCryptHash->pHMACInfo->cbOuterString);
2060 update_hash(pCryptHash, abHashValue, pCryptHash->dwHashSize);
2061 finalize_hash(pCryptHash);
2064 pCryptHash->dwState = RSAENH_HASHSTATE_FINISHED;
2067 return copy_param(pbData, pdwDataLen, (CONST BYTE*)pCryptHash->abHashValue,
2068 pCryptHash->dwHashSize);
2071 SetLastError(NTE_BAD_TYPE);
2076 /******************************************************************************
2077 * CPSetKeyParam (RSAENH.@)
2079 * Set a parameter of a key object
2082 * hProv [I] The key container to which the key belongs.
2083 * hKey [I] The key for which a parameter is to be set.
2084 * dwParam [I] Parameter type. See Notes.
2085 * pbData [I] Pointer to the parameter value.
2086 * dwFlags [I] Currently none defined.
2093 * Defined dwParam types are:
2094 * - KP_MODE: Values MODE_CBC, MODE_ECB, MODE_CFB.
2095 * - KP_MODE_BITS: Shift width for cipher feedback mode. (Currently ignored by MS CSP's)
2096 * - KP_PERMISSIONS: Or'ed combination of CRYPT_ENCRYPT, CRYPT_DECRYPT,
2097 * CRYPT_EXPORT, CRYPT_READ, CRYPT_WRITE, CRYPT_MAC
2098 * - KP_IV: Initialization vector
2100 BOOL WINAPI RSAENH_CPSetKeyParam(HCRYPTPROV hProv, HCRYPTKEY hKey, DWORD dwParam, BYTE *pbData,
2103 CRYPTKEY *pCryptKey;
2105 TRACE("(hProv=%08lx, hKey=%08lx, dwParam=%08lx, pbData=%p, dwFlags=%08lx)\n", hProv, hKey,
2106 dwParam, pbData, dwFlags);
2108 if (!is_valid_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER))
2110 SetLastError(NTE_BAD_UID);
2115 SetLastError(NTE_BAD_FLAGS);
2119 if (!lookup_handle(&handle_table, (unsigned int)hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
2121 SetLastError(NTE_BAD_KEY);
2127 pCryptKey->dwMode = *(DWORD*)pbData;
2131 pCryptKey->dwModeBits = *(DWORD*)pbData;
2134 case KP_PERMISSIONS:
2135 pCryptKey->dwPermissions = *(DWORD*)pbData;
2139 memcpy(pCryptKey->abInitVector, pbData, pCryptKey->dwBlockLen);
2143 SetLastError(NTE_BAD_TYPE);
2148 /******************************************************************************
2149 * CPGetKeyParam (RSAENH.@)
2151 * Query a key parameter.
2154 * hProv [I] The key container, which the key belongs to.
2155 * hHash [I] The key object that is to be queried.
2156 * dwParam [I] Specifies the parameter that is to be queried.
2157 * pbData [I] Pointer to the buffer where the parameter value will be stored.
2158 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
2159 * dwFlags [I] None currently defined.
2166 * Defined dwParam types are:
2167 * - KP_MODE: Values MODE_CBC, MODE_ECB, MODE_CFB.
2168 * - KP_MODE_BITS: Shift width for cipher feedback mode.
2169 * (Currently ignored by MS CSP's - always eight)
2170 * - KP_PERMISSIONS: Or'ed combination of CRYPT_ENCRYPT, CRYPT_DECRYPT,
2171 * CRYPT_EXPORT, CRYPT_READ, CRYPT_WRITE, CRYPT_MAC
2172 * - KP_IV: Initialization vector.
2173 * - KP_KEYLEN: Bitwidth of the key.
2174 * - KP_BLOCKLEN: Size of a block cipher block.
2175 * - KP_SALT: Salt value.
2177 BOOL WINAPI RSAENH_CPGetKeyParam(HCRYPTPROV hProv, HCRYPTKEY hKey, DWORD dwParam, BYTE *pbData,
2178 DWORD *pdwDataLen, DWORD dwFlags)
2180 CRYPTKEY *pCryptKey;
2183 TRACE("(hProv=%08lx, hKey=%08lx, dwParam=%08lx, pbData=%p, pdwDataLen=%p dwFlags=%08lx)\n",
2184 hProv, hKey, dwParam, pbData, pdwDataLen, dwFlags);
2186 if (!is_valid_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER))
2188 SetLastError(NTE_BAD_UID);
2193 SetLastError(NTE_BAD_FLAGS);
2197 if (!lookup_handle(&handle_table, (unsigned int)hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
2199 SetLastError(NTE_BAD_KEY);
2206 return copy_param(pbData, pdwDataLen, (CONST BYTE*)pCryptKey->abInitVector,
2207 pCryptKey->dwBlockLen);
2210 return copy_param(pbData, pdwDataLen,
2211 (CONST BYTE*)&pCryptKey->abKeyValue[pCryptKey->dwKeyLen], pCryptKey->dwSaltLen);
2214 dwBitLen = pCryptKey->dwKeyLen << 3;
2215 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwBitLen, sizeof(DWORD));
2218 dwBitLen = pCryptKey->dwBlockLen << 3;
2219 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwBitLen, sizeof(DWORD));
2222 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptKey->dwMode, sizeof(DWORD));
2225 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptKey->dwModeBits,
2228 case KP_PERMISSIONS:
2229 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptKey->dwPermissions,
2233 SetLastError(NTE_BAD_TYPE);
2238 /******************************************************************************
2239 * CPGetProvParam (RSAENH.@)
2241 * Query a CSP parameter.
2244 * hProv [I] The key container that is to be queried.
2245 * dwParam [I] Specifies the parameter that is to be queried.
2246 * pbData [I] Pointer to the buffer where the parameter value will be stored.
2247 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
2248 * dwFlags [I] CRYPT_FIRST: Start enumeration (for PP_ENUMALGS{_EX}).
2254 * Defined dwParam types:
2255 * - PP_CONTAINER: Name of the key container.
2256 * - PP_NAME: Name of the cryptographic service provider.
2257 * - PP_SIG_KEYSIZE_INC: RSA signature keywidth granularity in bits.
2258 * - PP_KEYX_KEYSIZE_INC: RSA key-exchange keywidth granularity in bits.
2259 * - PP_ENUMALGS{_EX}: Query provider capabilities.
2261 BOOL WINAPI RSAENH_CPGetProvParam(HCRYPTPROV hProv, DWORD dwParam, BYTE *pbData,
2262 DWORD *pdwDataLen, DWORD dwFlags)
2264 KEYCONTAINER *pKeyContainer;
2265 PROV_ENUMALGS provEnumalgs;
2268 /* This is for dwParam 41, which does not seem to be documented
2269 * on MSDN. IE6 SP1 asks for it in the 'About' dialog, however.
2270 * Returning this BLOB seems to satisfy IE. The marked 0x00 seem
2271 * to be 'don't care's. If you know anything more specific about
2272 * provider parameter 41, please report to wine-devel@winehq.org */
2273 static CONST BYTE abWTF[96] = {
2274 0xb0, 0x25, 0x63, 0x86, 0x9c, 0xab, 0xb6, 0x37,
2275 0xe8, 0x82, /**/0x00,/**/ 0x72, 0x06, 0xb2, /**/0x00,/**/ 0x3b,
2276 0x60, 0x35, /**/0x00,/**/ 0x3b, 0x88, 0xce, /**/0x00,/**/ 0x82,
2277 0xbc, 0x7a, /**/0x00,/**/ 0xb7, 0x4f, 0x7e, /**/0x00,/**/ 0xde,
2278 0x92, 0xf1, /**/0x00,/**/ 0x83, 0xea, 0x5e, /**/0x00,/**/ 0xc8,
2279 0x12, 0x1e, 0xd4, 0x06, 0xf7, 0x66, /**/0x00,/**/ 0x01,
2280 0x29, 0xa4, /**/0x00,/**/ 0xf8, 0x24, 0x0c, /**/0x00,/**/ 0x33,
2281 0x06, 0x80, /**/0x00,/**/ 0x02, 0x46, 0x0b, /**/0x00,/**/ 0x6d,
2282 0x5b, 0xca, /**/0x00,/**/ 0x9a, 0x10, 0xf0, /**/0x00,/**/ 0x05,
2283 0x19, 0xd0, /**/0x00,/**/ 0x2c, 0xf6, 0x27, /**/0x00,/**/ 0xaa,
2284 0x7c, 0x6f, /**/0x00,/**/ 0xb9, 0xd8, 0x72, /**/0x00,/**/ 0x03,
2285 0xf3, 0x81, /**/0x00,/**/ 0xfa, 0xe8, 0x26, /**/0x00,/**/ 0xca
2288 TRACE("(hProv=%08lx, dwParam=%08lx, pbData=%p, pdwDataLen=%p, dwFlags=%08lx)\n",
2289 hProv, dwParam, pbData, pdwDataLen, dwFlags);
2291 if (!lookup_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER,
2292 (OBJECTHDR**)&pKeyContainer))
2294 /* MSDN: hProv not containing valid context handle */
2295 SetLastError(NTE_BAD_UID);
2302 return copy_param(pbData, pdwDataLen, (CONST BYTE*)pKeyContainer->szName,
2303 strlen(pKeyContainer->szName)+1);
2306 return copy_param(pbData, pdwDataLen, (CONST BYTE*)pKeyContainer->szProvName,
2307 strlen(pKeyContainer->szProvName)+1);
2309 case PP_SIG_KEYSIZE_INC:
2310 case PP_KEYX_KEYSIZE_INC:
2312 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwTemp, sizeof(dwTemp));
2315 case PP_ENUMALGS_EX:
2316 if (((pKeyContainer->dwEnumAlgsCtr >= RSAENH_MAX_ENUMALGS-1) ||
2317 (!aProvEnumAlgsEx[pKeyContainer->dwPersonality]
2318 [pKeyContainer->dwEnumAlgsCtr+1].aiAlgid)) &&
2319 ((dwFlags & CRYPT_FIRST) != CRYPT_FIRST))
2321 SetLastError(ERROR_NO_MORE_ITEMS);
2325 if (dwParam == PP_ENUMALGS) {
2326 if (pbData && (*pdwDataLen >= sizeof(PROV_ENUMALGS)))
2327 pKeyContainer->dwEnumAlgsCtr = ((dwFlags & CRYPT_FIRST) == CRYPT_FIRST) ?
2328 0 : pKeyContainer->dwEnumAlgsCtr+1;
2330 provEnumalgs.aiAlgid = aProvEnumAlgsEx
2331 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr].aiAlgid;
2332 provEnumalgs.dwBitLen = aProvEnumAlgsEx
2333 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr].dwDefaultLen;
2334 provEnumalgs.dwNameLen = aProvEnumAlgsEx
2335 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr].dwNameLen;
2336 memcpy(provEnumalgs.szName, aProvEnumAlgsEx
2337 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr].szName,
2340 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&provEnumalgs,
2341 sizeof(PROV_ENUMALGS));
2343 if (pbData && (*pdwDataLen >= sizeof(PROV_ENUMALGS_EX)))
2344 pKeyContainer->dwEnumAlgsCtr = ((dwFlags & CRYPT_FIRST) == CRYPT_FIRST) ?
2345 0 : pKeyContainer->dwEnumAlgsCtr+1;
2347 return copy_param(pbData, pdwDataLen,
2348 (CONST BYTE*)&aProvEnumAlgsEx
2349 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr],
2350 sizeof(PROV_ENUMALGS_EX));
2353 case 41: /* Undocumented. Asked for by IE About dialog */
2354 return copy_param(pbData, pdwDataLen, abWTF, sizeof(abWTF));
2357 /* MSDN: Unknown parameter number in dwParam */
2358 SetLastError(NTE_BAD_TYPE);
2363 /******************************************************************************
2364 * CPDeriveKey (RSAENH.@)
2366 * Derives a key from a hash value.
2369 * hProv [I] Key container for which a key is to be generated.
2370 * Algid [I] Crypto algorithm identifier for the key to be generated.
2371 * hBaseData [I] Hash from whose value the key will be derived.
2372 * dwFlags [I] See Notes.
2373 * phKey [O] The generated key.
2381 * - CRYPT_EXPORTABLE: Key can be exported.
2382 * - CRYPT_NO_SALT: No salt is used for 40 bit keys.
2383 * - CRYPT_CREATE_SALT: Use remaining bits as salt value.
2385 BOOL WINAPI RSAENH_CPDeriveKey(HCRYPTPROV hProv, ALG_ID Algid, HCRYPTHASH hBaseData,
2386 DWORD dwFlags, HCRYPTKEY *phKey)
2388 KEYCONTAINER *pKeyContainer;
2389 CRYPTKEY *pCryptKey;
2390 CRYPTHASH *pCryptHash;
2391 BYTE abHashValue[RSAENH_MAX_HASH_SIZE*2];
2394 TRACE("(hProv=%08lx, Algid=%d, hBaseData=%08lx, dwFlags=%08lx phKey=%p)\n", hProv, Algid,
2395 hBaseData, dwFlags, phKey);
2397 if (!lookup_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER,
2398 (OBJECTHDR**)&pKeyContainer))
2400 SetLastError(NTE_BAD_UID);
2404 if (!lookup_handle(&handle_table, (unsigned int)hBaseData, RSAENH_MAGIC_HASH,
2405 (OBJECTHDR**)&pCryptHash))
2407 SetLastError(NTE_BAD_HASH);
2413 SetLastError(ERROR_INVALID_PARAMETER);
2417 *phKey = new_key(hProv, Algid, dwFlags, &pCryptKey);
2418 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) return FALSE;
2421 * We derive the key material from the hash.
2422 * If the hash value is not large enough for the claimed key, we have to construct
2423 * a larger binary value based on the hash. This is documented in MSDN: CryptDeriveKey.
2425 dwLen = RSAENH_MAX_HASH_SIZE;
2426 RSAENH_CPGetHashParam(pCryptHash->hProv, hBaseData, HP_HASHVAL, abHashValue, &dwLen, 0);
2428 if (dwLen < pCryptKey->dwKeyLen) {
2429 BYTE pad1[RSAENH_HMAC_DEF_PAD_LEN], pad2[RSAENH_HMAC_DEF_PAD_LEN], old_hashval[RSAENH_MAX_HASH_SIZE];
2432 memcpy(old_hashval, pCryptHash->abHashValue, RSAENH_MAX_HASH_SIZE);
2434 for (i=0; i<RSAENH_HMAC_DEF_PAD_LEN; i++) {
2435 pad1[i] = RSAENH_HMAC_DEF_IPAD_CHAR ^ (i<dwLen ? abHashValue[i] : 0);
2436 pad2[i] = RSAENH_HMAC_DEF_OPAD_CHAR ^ (i<dwLen ? abHashValue[i] : 0);
2439 init_hash(pKeyContainer, pCryptHash);
2440 update_hash(pCryptHash, pad1, RSAENH_HMAC_DEF_PAD_LEN);
2441 finalize_hash(pCryptHash);
2442 memcpy(abHashValue, pCryptHash->abHashValue, pCryptHash->dwHashSize);
2444 init_hash(pKeyContainer, pCryptHash);
2445 update_hash(pCryptHash, pad2, RSAENH_HMAC_DEF_PAD_LEN);
2446 finalize_hash(pCryptHash);
2447 memcpy(abHashValue+pCryptHash->dwHashSize, pCryptHash->abHashValue,
2448 pCryptHash->dwHashSize);
2450 memcpy(pCryptHash->abHashValue, old_hashval, RSAENH_MAX_HASH_SIZE);
2453 memcpy(pCryptKey->abKeyValue, abHashValue,
2454 RSAENH_MIN(pCryptKey->dwKeyLen, sizeof(pCryptKey->abKeyValue)));
2456 setup_key(pCryptKey);
2460 /******************************************************************************
2461 * CPGetUserKey (RSAENH.@)
2463 * Returns a handle to the user's private key-exchange- or signature-key.
2466 * hProv [I] The key container from which a user key is requested.
2467 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
2468 * phUserKey [O] Handle to the requested key or INVALID_HANDLE_VALUE in case of failure.
2475 * A newly created key container does not contain private user key. Create them with CPGenKey.
2477 BOOL WINAPI RSAENH_CPGetUserKey(HCRYPTPROV hProv, DWORD dwKeySpec, HCRYPTKEY *phUserKey)
2479 KEYCONTAINER *pKeyContainer;
2481 TRACE("(hProv=%08lx, dwKeySpec=%08lx, phUserKey=%p)\n", hProv, dwKeySpec, phUserKey);
2483 if (!lookup_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER,
2484 (OBJECTHDR**)&pKeyContainer))
2486 /* MSDN: hProv not containing valid context handle */
2487 SetLastError(NTE_BAD_UID);
2493 case AT_KEYEXCHANGE:
2494 copy_handle(&handle_table, pKeyContainer->hKeyExchangeKeyPair, RSAENH_MAGIC_KEY,
2495 (unsigned int*)phUserKey);
2499 copy_handle(&handle_table, pKeyContainer->hSignatureKeyPair, RSAENH_MAGIC_KEY,
2500 (unsigned int*)phUserKey);
2504 *phUserKey = (HCRYPTKEY)INVALID_HANDLE_VALUE;
2507 if (*phUserKey == (HCRYPTKEY)INVALID_HANDLE_VALUE)
2509 /* MSDN: dwKeySpec parameter specifies non existent key */
2510 SetLastError(NTE_NO_KEY);
2517 /******************************************************************************
2518 * CPHashData (RSAENH.@)
2520 * Updates a hash object with the given data.
2523 * hProv [I] Key container to which the hash object belongs.
2524 * hHash [I] Hash object which is to be updated.
2525 * pbData [I] Pointer to data with which the hash object is to be updated.
2526 * dwDataLen [I] Length of the data.
2527 * dwFlags [I] Currently none defined.
2534 * The actual hash value is queried with CPGetHashParam, which will finalize
2535 * the hash. Updating a finalized hash will fail with a last error NTE_BAD_HASH_STATE.
2537 BOOL WINAPI RSAENH_CPHashData(HCRYPTPROV hProv, HCRYPTHASH hHash, CONST BYTE *pbData,
2538 DWORD dwDataLen, DWORD dwFlags)
2540 CRYPTHASH *pCryptHash;
2541 KEYCONTAINER *pKeyContainer;
2543 TRACE("(hProv=%08lx, hHash=%08lx, pbData=%p, dwDataLen=%ld, dwFlags=%08lx)\n",
2544 hProv, hHash, pbData, dwDataLen, dwFlags);
2546 if (!lookup_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER,
2547 (OBJECTHDR**)&pKeyContainer))
2549 SetLastError(NTE_BAD_UID);
2555 SetLastError(NTE_BAD_FLAGS);
2559 if (!lookup_handle(&handle_table, (unsigned int)hHash, RSAENH_MAGIC_HASH,
2560 (OBJECTHDR**)&pCryptHash))
2562 SetLastError(NTE_BAD_HASH);
2566 if (!get_algid_info(pKeyContainer, pCryptHash->aiAlgid) ||
2567 pCryptHash->aiAlgid == CALG_SSL3_SHAMD5)
2569 SetLastError(NTE_BAD_ALGID);
2573 if (pCryptHash->dwState == RSAENH_HASHSTATE_IDLE)
2574 pCryptHash->dwState = RSAENH_HASHSTATE_HASHING;
2576 if (pCryptHash->dwState != RSAENH_HASHSTATE_HASHING)
2578 SetLastError(NTE_BAD_HASH_STATE);
2582 update_hash(pCryptHash, pbData, dwDataLen);
2586 /******************************************************************************
2587 * CPHashSessionKey (RSAENH.@)
2589 * Updates a hash object with the binary representation of a symmetric key.
2592 * hProv [I] Key container to which the hash object belongs.
2593 * hHash [I] Hash object which is to be updated.
2594 * hKey [I] The symmetric key, whose binary value will be added to the hash.
2595 * dwFlags [I] CRYPT_LITTLE_ENDIAN, if the binary key value shall be interpreted as little endian.
2601 BOOL WINAPI RSAENH_CPHashSessionKey(HCRYPTPROV hProv, HCRYPTHASH hHash, HCRYPTKEY hKey,
2604 BYTE abKeyValue[RSAENH_MAX_KEY_SIZE], bTemp;
2608 TRACE("(hProv=%08lx, hHash=%08lx, hKey=%08lx, dwFlags=%08lx)\n", hProv, hHash, hKey, dwFlags);
2610 if (!lookup_handle(&handle_table, (unsigned int)hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pKey) ||
2611 (GET_ALG_CLASS(pKey->aiAlgid) != ALG_CLASS_DATA_ENCRYPT))
2613 SetLastError(NTE_BAD_KEY);
2617 if (dwFlags & ~CRYPT_LITTLE_ENDIAN) {
2618 SetLastError(NTE_BAD_FLAGS);
2622 memcpy(abKeyValue, pKey->abKeyValue, pKey->dwKeyLen);
2623 if (!(dwFlags & CRYPT_LITTLE_ENDIAN)) {
2624 for (i=0; i<pKey->dwKeyLen/2; i++) {
2625 bTemp = abKeyValue[i];
2626 abKeyValue[i] = abKeyValue[pKey->dwKeyLen-i-1];
2627 abKeyValue[pKey->dwKeyLen-i-1] = bTemp;
2631 return RSAENH_CPHashData(hProv, hHash, abKeyValue, pKey->dwKeyLen, 0);
2634 /******************************************************************************
2635 * CPReleaseContext (RSAENH.@)
2637 * Release a key container.
2640 * hProv [I] Key container to be released.
2641 * dwFlags [I] Currently none defined.
2647 BOOL WINAPI RSAENH_CPReleaseContext(HCRYPTPROV hProv, DWORD dwFlags)
2649 TRACE("(hProv=%08lx, dwFlags=%08lx)\n", hProv, dwFlags);
2651 if (!release_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER))
2653 /* MSDN: hProv not containing valid context handle */
2654 SetLastError(NTE_BAD_UID);
2659 SetLastError(NTE_BAD_FLAGS);
2666 /******************************************************************************
2667 * CPSetHashParam (RSAENH.@)
2669 * Set a parameter of a hash object
2672 * hProv [I] The key container to which the key belongs.
2673 * hHash [I] The hash object for which a parameter is to be set.
2674 * dwParam [I] Parameter type. See Notes.
2675 * pbData [I] Pointer to the parameter value.
2676 * dwFlags [I] Currently none defined.
2683 * Currently only the HP_HMAC_INFO dwParam type is defined.
2684 * The HMAC_INFO struct will be deep copied into the hash object.
2685 * See Internet RFC 2104 for details on the HMAC algorithm.
2687 BOOL WINAPI RSAENH_CPSetHashParam(HCRYPTPROV hProv, HCRYPTHASH hHash, DWORD dwParam,
2688 BYTE *pbData, DWORD dwFlags)
2690 CRYPTHASH *pCryptHash;
2691 CRYPTKEY *pCryptKey;
2692 KEYCONTAINER *pKeyContainer;
2695 TRACE("(hProv=%08lx, hHash=%08lx, dwParam=%08lx, pbData=%p, dwFlags=%08lx)\n",
2696 hProv, hHash, dwParam, pbData, dwFlags);
2698 if (!lookup_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER,
2699 (OBJECTHDR**)&pKeyContainer))
2701 SetLastError(NTE_BAD_UID);
2706 SetLastError(NTE_BAD_FLAGS);
2710 if (!lookup_handle(&handle_table, (unsigned int)hHash, RSAENH_MAGIC_HASH,
2711 (OBJECTHDR**)&pCryptHash))
2713 SetLastError(NTE_BAD_HASH);
2719 free_hmac_info(pCryptHash->pHMACInfo);
2720 if (!copy_hmac_info(&pCryptHash->pHMACInfo, (PHMAC_INFO)pbData)) return FALSE;
2721 init_hash(pKeyContainer, pCryptHash);
2723 if (!lookup_handle(&handle_table, pCryptHash->hKey, RSAENH_MAGIC_KEY,
2724 (OBJECTHDR**)&pCryptKey))
2726 SetLastError(NTE_FAIL); /* FIXME: correct error code? */
2730 for (i=0; i<RSAENH_MIN(pCryptKey->dwKeyLen,pCryptHash->pHMACInfo->cbInnerString); i++) {
2731 pCryptHash->pHMACInfo->pbInnerString[i] ^= pCryptKey->abKeyValue[i];
2733 for (i=0; i<RSAENH_MIN(pCryptKey->dwKeyLen,pCryptHash->pHMACInfo->cbOuterString); i++) {
2734 pCryptHash->pHMACInfo->pbOuterString[i] ^= pCryptKey->abKeyValue[i];
2737 return RSAENH_CPHashData(hProv, hHash, pCryptHash->pHMACInfo->pbInnerString,
2738 pCryptHash->pHMACInfo->cbInnerString, 0);
2741 memcpy(pCryptHash->abHashValue, pbData, pCryptHash->dwHashSize);
2742 pCryptHash->dwState = RSAENH_HASHSTATE_FINISHED;
2746 SetLastError(NTE_BAD_TYPE);
2751 /******************************************************************************
2752 * CPSetProvParam (RSAENH.@)
2754 BOOL WINAPI RSAENH_CPSetProvParam(HCRYPTPROV hProv, DWORD dwParam, BYTE *pbData, DWORD dwFlags)
2760 /******************************************************************************
2761 * CPSignHash (RSAENH.@)
2763 * Sign a hash object
2766 * hProv [I] The key container, to which the hash object belongs.
2767 * hHash [I] The hash object to be signed.
2768 * dwKeySpec [I] AT_SIGNATURE or AT_KEYEXCHANGE: Key used to generate the signature.
2769 * sDescription [I] Should be NULL for security reasons.
2770 * dwFlags [I] 0, CRYPT_NOHASHOID or CRYPT_X931_FORMAT: Format of the signature.
2771 * pbSignature [O] Buffer, to which the signature will be stored. May be NULL to query SigLen.
2772 * pdwSigLen [I/O] Size of the buffer (in), Length of the signature (out)
2778 BOOL WINAPI RSAENH_CPSignHash(HCRYPTPROV hProv, HCRYPTHASH hHash, DWORD dwKeySpec,
2779 LPCWSTR sDescription, DWORD dwFlags, BYTE *pbSignature,
2782 HCRYPTKEY hCryptKey;
2783 CRYPTKEY *pCryptKey;
2785 BYTE abHashValue[RSAENH_MAX_HASH_SIZE];
2788 TRACE("(hProv=%08lx, hHash=%08lx, dwKeySpec=%08lx, sDescription=%s, dwFlags=%08lx, "
2789 "pbSignature=%p, pdwSigLen=%p)\n", hProv, hHash, dwKeySpec, debugstr_w(sDescription),
2790 dwFlags, pbSignature, pdwSigLen);
2792 if (dwFlags & ~(CRYPT_NOHASHOID|CRYPT_X931_FORMAT)) {
2793 SetLastError(NTE_BAD_FLAGS);
2797 if (!RSAENH_CPGetUserKey(hProv, dwKeySpec, &hCryptKey)) return FALSE;
2799 if (!lookup_handle(&handle_table, (unsigned int)hCryptKey, RSAENH_MAGIC_KEY,
2800 (OBJECTHDR**)&pCryptKey))
2802 SetLastError(NTE_NO_KEY);
2807 *pdwSigLen = pCryptKey->dwKeyLen;
2810 if (pCryptKey->dwKeyLen > *pdwSigLen)
2812 SetLastError(ERROR_MORE_DATA);
2813 *pdwSigLen = pCryptKey->dwKeyLen;
2816 *pdwSigLen = pCryptKey->dwKeyLen;
2819 if (!RSAENH_CPHashData(hProv, hHash, (CONST BYTE*)sDescription,
2820 (DWORD)lstrlenW(sDescription)*sizeof(WCHAR), 0))
2826 dwHashLen = sizeof(DWORD);
2827 if (!RSAENH_CPGetHashParam(hProv, hHash, HP_ALGID, (BYTE*)&aiAlgid, &dwHashLen, 0)) return FALSE;
2829 dwHashLen = RSAENH_MAX_HASH_SIZE;
2830 if (!RSAENH_CPGetHashParam(hProv, hHash, HP_HASHVAL, abHashValue, &dwHashLen, 0)) return FALSE;
2833 if (!build_hash_signature(pbSignature, *pdwSigLen, aiAlgid, abHashValue, dwHashLen, dwFlags)) {
2837 return encrypt_block_impl(pCryptKey->aiAlgid, &pCryptKey->context, pbSignature, pbSignature, RSAENH_ENCRYPT);
2840 /******************************************************************************
2841 * CPVerifySignature (RSAENH.@)
2843 * Verify the signature of a hash object.
2846 * hProv [I] The key container, to which the hash belongs.
2847 * hHash [I] The hash for which the signature is verified.
2848 * pbSignature [I] The binary signature.
2849 * dwSigLen [I] Length of the signature BLOB.
2850 * hPubKey [I] Public key used to verify the signature.
2851 * sDescription [I] Should be NULL for security reasons.
2852 * dwFlags [I] 0, CRYPT_NOHASHOID or CRYPT_X931_FORMAT: Format of the signature.
2855 * Success: TRUE (Signature is valid)
2856 * Failure: FALSE (GetLastError() == NTE_BAD_SIGNATURE, if signature is invalid)
2858 BOOL WINAPI RSAENH_CPVerifySignature(HCRYPTPROV hProv, HCRYPTHASH hHash, CONST BYTE *pbSignature,
2859 DWORD dwSigLen, HCRYPTKEY hPubKey, LPCWSTR sDescription,
2862 BYTE *pbConstructed = NULL, *pbDecrypted = NULL;
2863 CRYPTKEY *pCryptKey;
2866 BYTE abHashValue[RSAENH_MAX_HASH_SIZE];
2869 TRACE("(hProv=%08lx, hHash=%08lx, pbSignature=%p, dwSigLen=%ld, hPubKey=%08lx, sDescription=%s, "
2870 "dwFlags=%08lx)\n", hProv, hHash, pbSignature, dwSigLen, hPubKey, debugstr_w(sDescription),
2873 if (dwFlags & ~(CRYPT_NOHASHOID|CRYPT_X931_FORMAT)) {
2874 SetLastError(NTE_BAD_FLAGS);
2878 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
2880 SetLastError(NTE_BAD_UID);
2884 if (!lookup_handle(&handle_table, (unsigned int)hPubKey, RSAENH_MAGIC_KEY,
2885 (OBJECTHDR**)&pCryptKey))
2887 SetLastError(NTE_BAD_KEY);
2892 if (!RSAENH_CPHashData(hProv, hHash, (CONST BYTE*)sDescription,
2893 (DWORD)lstrlenW(sDescription)*sizeof(WCHAR), 0))
2899 dwHashLen = sizeof(DWORD);
2900 if (!RSAENH_CPGetHashParam(hProv, hHash, HP_ALGID, (BYTE*)&aiAlgid, &dwHashLen, 0)) return FALSE;
2902 dwHashLen = RSAENH_MAX_HASH_SIZE;
2903 if (!RSAENH_CPGetHashParam(hProv, hHash, HP_HASHVAL, abHashValue, &dwHashLen, 0)) return FALSE;
2905 pbConstructed = HeapAlloc(GetProcessHeap(), 0, dwSigLen);
2906 if (!pbConstructed) {
2907 SetLastError(NTE_NO_MEMORY);
2911 pbDecrypted = HeapAlloc(GetProcessHeap(), 0, dwSigLen);
2913 SetLastError(NTE_NO_MEMORY);
2917 if (!encrypt_block_impl(pCryptKey->aiAlgid, &pCryptKey->context, pbSignature, pbDecrypted,
2923 if (!build_hash_signature(pbConstructed, dwSigLen, aiAlgid, abHashValue, dwHashLen, dwFlags)) {
2927 if (memcmp(pbDecrypted, pbConstructed, dwSigLen)) {
2928 SetLastError(NTE_BAD_SIGNATURE);
2934 HeapFree(GetProcessHeap(), 0, pbConstructed);
2935 HeapFree(GetProcessHeap(), 0, pbDecrypted);
2939 static const WCHAR szProviderKeys[4][97] = {
2940 { 'S','o','f','t','w','a','r','e','\\',
2941 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
2942 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
2943 'i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ','B','a','s',
2944 'e',' ','C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r',
2945 'o','v','i','d','e','r',' ','v','1','.','0',0 },
2946 { 'S','o','f','t','w','a','r','e','\\',
2947 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
2948 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
2949 'i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ',
2950 'E','n','h','a','n','c','e','d',
2951 ' ','C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r',
2952 'o','v','i','d','e','r',' ','v','1','.','0',0 },
2953 { 'S','o','f','t','w','a','r','e','\\',
2954 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
2955 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
2956 'i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ','S','t','r','o','n','g',
2957 ' ','C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r',
2958 'o','v','i','d','e','r',0 },
2959 { 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
2960 'C','r','y','p','t','o','g','r','a','p','h','y','\\','D','e','f','a','u','l','t','s','\\',
2961 'P','r','o','v','i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ',
2962 'R','S','A',' ','S','C','h','a','n','n','e','l',' ',
2963 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r','o','v','i','d','e','r',0 }
2965 static const WCHAR szDefaultKeys[2][65] = {
2966 { 'S','o','f','t','w','a','r','e','\\',
2967 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
2968 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
2969 'i','d','e','r',' ','T','y','p','e','s','\\','T','y','p','e',' ','0','0','1',0 },
2970 { 'S','o','f','t','w','a','r','e','\\',
2971 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
2972 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
2973 'i','d','e','r',' ','T','y','p','e','s','\\','T','y','p','e',' ','0','1','2',0 }
2977 /******************************************************************************
2978 * DllRegisterServer (RSAENH.@)
2980 * Dll self registration.
2989 * Registers the following keys:
2990 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider\
2991 * Microsoft Base Cryptographic Provider v1.0
2992 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider\
2993 * Microsoft Enhanced Cryptographic Provider
2994 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider\
2995 * Microsoft Strong Cryptographpic Provider
2996 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider Types\Type 001
2998 HRESULT WINAPI RSAENH_DllRegisterServer()
3005 for (i=0; i<4; i++) {
3006 apiRet = RegCreateKeyExW(HKEY_LOCAL_MACHINE, szProviderKeys[i], 0, NULL,
3007 REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &key, &dp);
3009 if (apiRet == ERROR_SUCCESS)
3011 if (dp == REG_CREATED_NEW_KEY)
3013 static const WCHAR szImagePath[] = { 'I','m','a','g','e',' ','P','a','t','h',0 };
3014 static const WCHAR szRSABase[] = { 'r','s','a','e','n','h','.','d','l','l',0 };
3015 static const WCHAR szType[] = { 'T','y','p','e',0 };
3016 static const WCHAR szSignature[] = { 'S','i','g','n','a','t','u','r','e',0 };
3017 DWORD type = (i == 3) ? PROV_RSA_SCHANNEL : PROV_RSA_FULL;
3018 DWORD sign = 0xdeadbeef;
3019 RegSetValueExW(key, szImagePath, 0, REG_SZ, (LPBYTE)szRSABase,
3020 (lstrlenW(szRSABase) + 1) * sizeof(WCHAR));
3021 RegSetValueExW(key, szType, 0, REG_DWORD, (LPBYTE)&type, sizeof(type));
3022 RegSetValueExW(key, szSignature, 0, REG_BINARY, (LPBYTE)&sign, sizeof(sign));
3028 for (i=0; i<2; i++) {
3029 apiRet = RegCreateKeyExW(HKEY_LOCAL_MACHINE, szDefaultKeys[i], 0, NULL,
3030 REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &key, &dp);
3031 if (apiRet == ERROR_SUCCESS)
3033 if (dp == REG_CREATED_NEW_KEY)
3035 static const WCHAR szName[] = { 'N','a','m','e',0 };
3036 static const WCHAR szRSAName[2][46] = {
3037 { 'M','i','c','r','o','s','o','f','t',' ', 'B','a','s','e',' ',
3038 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ',
3039 'P','r','o','v','i','d','e','r',' ','v','1','.','0',0 },
3040 { 'M','i','c','r','o','s','o','f','t',' ','R','S','A',' ',
3041 'S','C','h','a','n','n','e','l',' ',
3042 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ',
3043 'P','r','o','v','i','d','e','r',0 } };
3044 static const WCHAR szTypeName[] = { 'T','y','p','e','N','a','m','e',0 };
3045 static const WCHAR szRSATypeName[2][38] = {
3046 { 'R','S','A',' ','F','u','l','l',' ',
3047 '(','S','i','g','n','a','t','u','r','e',' ','a','n','d',' ',
3048 'K','e','y',' ','E','x','c','h','a','n','g','e',')',0 },
3049 { 'R','S','A',' ','S','C','h','a','n','n','e','l',0 } };
3051 RegSetValueExW(key, szName, 0, REG_SZ, (LPBYTE)szRSAName[i], sizeof(szRSAName));
3052 RegSetValueExW(key, szTypeName, 0, REG_SZ,
3053 (LPBYTE)szRSATypeName[i],sizeof(szRSATypeName));
3059 return HRESULT_FROM_WIN32(apiRet);
3062 /******************************************************************************
3063 * DllUnregisterServer (RSAENH.@)
3065 * Dll self unregistration.
3073 * For the relevant keys see DllRegisterServer.
3075 HRESULT WINAPI RSAENH_DllUnregisterServer()
3077 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szProviderKeys[0]);
3078 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szProviderKeys[1]);
3079 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szProviderKeys[2]);
3080 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szProviderKeys[3]);
3081 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szDefaultKeys[0]);
3082 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szDefaultKeys[1]);