Implemented DllCanUnloadNow.
[wine/multimedia.git] / dlls / rsaenh / rsaenh.c
blobbcaafbee4b1a8bfba596e996e285f2f46eda42c9
1 /*
2 * dlls/rsaenh/rsaenh.c
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
24 #include "config.h"
25 #include "wine/port.h"
26 #include "wine/library.h"
27 #include "wine/debug.h"
29 #include <stdarg.h>
30 #include <stdio.h>
32 #include "windef.h"
33 #include "winbase.h"
34 #include "winreg.h"
35 #include "wincrypt.h"
36 #include "lmcons.h"
37 #include "handle.h"
38 #include "implglue.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
52 OBJECTHDR header;
53 ALG_ID aiAlgid;
54 HCRYPTKEY hKey;
55 HCRYPTPROV hProv;
56 DWORD dwHashSize;
57 DWORD dwState;
58 HASH_CONTEXT context;
59 BYTE abHashValue[RSAENH_MAX_HASH_SIZE];
60 PHMAC_INFO pHMACInfo;
61 } CRYPTHASH;
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
74 OBJECTHDR header;
75 ALG_ID aiAlgid;
76 HCRYPTPROV hProv;
77 DWORD dwMode;
78 DWORD dwModeBits;
79 DWORD dwPermissions;
80 DWORD dwKeyLen;
81 DWORD dwSaltLen;
82 DWORD dwBlockLen;
83 DWORD dwState;
84 KEY_CONTEXT context;
85 BYTE abKeyValue[RSAENH_MAX_KEY_SIZE];
86 BYTE abInitVector[RSAENH_MAX_BLOCK_SIZE];
87 BYTE abChainVector[RSAENH_MAX_BLOCK_SIZE];
88 } CRYPTKEY;
90 /******************************************************************************
91 * KEYCONTAINER - key containers
93 #define RSAENH_PERSONALITY_BASE 0u
94 #define RSAENH_PERSONALITY_STRONG 1u
95 #define RSAENH_PERSONALITY_ENHANCED 2u
97 #define RSAENH_MAGIC_CONTAINER 0x26384993u
98 typedef struct tagKEYCONTAINER
100 OBJECTHDR header;
101 DWORD dwFlags;
102 DWORD dwPersonality;
103 DWORD dwEnumAlgsCtr;
104 CHAR szName[MAX_PATH];
105 CHAR szProvName[MAX_PATH];
106 HCRYPTKEY hKeyExchangeKeyPair;
107 HCRYPTKEY hSignatureKeyPair;
108 } KEYCONTAINER;
110 /******************************************************************************
111 * Some magic constants
113 #define RSAENH_ENCRYPT 1
114 #define RSAENH_DECRYPT 0
115 #define RSAENH_HMAC_DEF_IPAD_CHAR 0x36
116 #define RSAENH_HMAC_DEF_OPAD_CHAR 0x5c
117 #define RSAENH_HMAC_DEF_PAD_LEN 64
118 #define RSAENH_DES_EFFECTIVE_KEYLEN 56
119 #define RSAENH_DES_STORAGE_KEYLEN 64
120 #define RSAENH_3DES112_EFFECTIVE_KEYLEN 112
121 #define RSAENH_3DES112_STORAGE_KEYLEN 128
122 #define RSAENH_3DES_EFFECTIVE_KEYLEN 168
123 #define RSAENH_3DES_STORAGE_KEYLEN 192
124 #define RSAENH_MAGIC_RSA2 0x32415352
125 #define RSAENH_MAGIC_RSA1 0x31415352
126 #define RSAENH_PKC_BLOCKTYPE 0x02
127 #define RSAENH_REGKEY "Software\\Wine\\Crypto\\RSA\\%s"
129 #define RSAENH_MIN(a,b) ((a)<(b)?(a):(b))
130 /******************************************************************************
131 * aProvEnumAlgsEx - Defines the capabilities of the CSP personalities.
133 #define RSAENH_MAX_ENUMALGS 14
134 PROV_ENUMALGS_EX aProvEnumAlgsEx[3][RSAENH_MAX_ENUMALGS+1] =
137 {CALG_RC2, 40, 40, 56,0, 4,"RC2", 24,"RSA Data Security's RC2"},
138 {CALG_RC4, 40, 40, 56,0, 4,"RC4", 24,"RSA Data Security's RC4"},
139 {CALG_DES, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
140 {CALG_SHA, 160,160, 160,CRYPT_FLAG_SIGNING, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
141 {CALG_MD2, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD2", 27,"MD2 Message Digest 2 (MD2)"},
142 {CALG_MD4, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD4", 27,"MD4 Message Digest 4 (MD4)"},
143 {CALG_MD5, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD5", 27,"MD5 Message Digest 5 (MD5)"},
144 {CALG_SSL3_SHAMD5,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
145 {CALG_MAC, 0, 0, 0,0, 4,"MAC", 27,"Message Authentication Code"},
146 {CALG_RSA_SIGN, 512,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_SIGN",14,"RSA Signature"},
147 {CALG_RSA_KEYX, 512,384, 1024,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_KEYX",18,"RSA Key Exchange"},
148 {CALG_HMAC, 0, 0, 0,0, 5,"HMAC", 23,"HMAC Hugo's MAC (HMAC)"},
149 {0, 0, 0, 0,0, 1,"", 1,""}
152 {CALG_RC2, 128, 40, 128,0, 4,"RC2", 24,"RSA Data Security's RC2"},
153 {CALG_RC4, 128, 40, 128,0, 4,"RC4", 24,"RSA Data Security's RC4"},
154 {CALG_DES, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
155 {CALG_3DES_112, 112,112, 112,0, 13,"3DES TWO KEY",19,"Two Key Triple DES"},
156 {CALG_3DES, 168,168, 168,0, 5,"3DES", 21,"Three Key Triple DES"},
157 {CALG_SHA, 160,160, 160,CRYPT_FLAG_SIGNING, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
158 {CALG_MD2, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD2", 27,"MD2 Message Digest 2 (MD2)"},
159 {CALG_MD4, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD4", 27,"MD4 Message Digest 4 (MD4)"},
160 {CALG_MD5, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD5", 27,"MD5 Message Digest 5 (MD5)"},
161 {CALG_SSL3_SHAMD5,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
162 {CALG_MAC, 0, 0, 0,0, 4,"MAC", 27,"Message Authentication Code"},
163 {CALG_RSA_SIGN,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_SIGN",14,"RSA Signature"},
164 {CALG_RSA_KEYX,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_KEYX",18,"RSA Key Exchange"},
165 {CALG_HMAC, 0, 0, 0,0, 5,"HMAC", 23,"HMAC Hugo's MAC (HMAC)"},
166 {0, 0, 0, 0,0, 1,"", 1,""}
169 {CALG_RC2, 128, 40, 128,0, 4,"RC2", 24,"RSA Data Security's RC2"},
170 {CALG_RC4, 128, 40, 128,0, 4,"RC4", 24,"RSA Data Security's RC4"},
171 {CALG_DES, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
172 {CALG_3DES_112, 112,112, 112,0, 13,"3DES TWO KEY",19,"Two Key Triple DES"},
173 {CALG_3DES, 168,168, 168,0, 5,"3DES", 21,"Three Key Triple DES"},
174 {CALG_SHA, 160,160, 160,CRYPT_FLAG_SIGNING, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
175 {CALG_MD2, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD2", 27,"MD2 Message Digest 2 (MD2)"},
176 {CALG_MD4, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD4", 27,"MD4 Message Digest 4 (MD4)"},
177 {CALG_MD5, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD5", 27,"MD5 Message Digest 5 (MD5)"},
178 {CALG_SSL3_SHAMD5,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
179 {CALG_MAC, 0, 0, 0,0, 4,"MAC", 27,"Message Authentication Code"},
180 {CALG_RSA_SIGN,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_SIGN",14,"RSA Signature"},
181 {CALG_RSA_KEYX,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_KEYX",18,"RSA Key Exchange"},
182 {CALG_HMAC, 0, 0, 0,0, 5,"HMAC", 23,"HMAC Hugo's MAC (HMAC)"},
183 {0, 0, 0, 0,0, 1,"", 1,""}
187 /******************************************************************************
188 * API forward declarations
190 BOOL WINAPI
191 RSAENH_CPGetKeyParam(
192 HCRYPTPROV hProv,
193 HCRYPTKEY hKey,
194 DWORD dwParam,
195 BYTE *pbData,
196 DWORD *pdwDataLen,
197 DWORD dwFlags
200 BOOL WINAPI
201 RSAENH_CPEncrypt(
202 HCRYPTPROV hProv,
203 HCRYPTKEY hKey,
204 HCRYPTHASH hHash,
205 BOOL Final,
206 DWORD dwFlags,
207 BYTE *pbData,
208 DWORD *pdwDataLen,
209 DWORD dwBufLen
212 BOOL WINAPI
213 RSAENH_CPGetHashParam(
214 HCRYPTPROV hProv,
215 HCRYPTHASH hHash,
216 DWORD dwParam,
217 BYTE *pbData,
218 DWORD *pdwDataLen,
219 DWORD dwFlags
222 BOOL WINAPI
223 RSAENH_CPExportKey(
224 HCRYPTPROV hProv,
225 HCRYPTKEY hKey,
226 HCRYPTKEY hPubKey,
227 DWORD dwBlobType,
228 DWORD dwFlags,
229 BYTE *pbData,
230 DWORD *pdwDataLen
233 BOOL WINAPI
234 RSAENH_CPImportKey(
235 HCRYPTPROV hProv,
236 CONST BYTE *pbData,
237 DWORD dwDataLen,
238 HCRYPTKEY hPubKey,
239 DWORD dwFlags,
240 HCRYPTKEY *phKey
243 BOOL WINAPI
244 RSAENH_CPHashData(
245 HCRYPTPROV hProv,
246 HCRYPTHASH hHash,
247 CONST BYTE *pbData,
248 DWORD dwDataLen,
249 DWORD dwFlags
252 /******************************************************************************
253 * CSP's handle table (used by all acquired key containers)
255 static HANDLETABLE handle_table;
257 /******************************************************************************
258 * DllMain (RSAENH.@)
260 * Initializes and destroys the handle table for the CSP's handles.
262 int WINAPI DllMain(HINSTANCE hInstance, DWORD fdwReason, PVOID pvReserved)
264 switch (fdwReason)
266 case DLL_PROCESS_ATTACH:
267 init_handle_table(&handle_table);
268 break;
270 case DLL_PROCESS_DETACH:
271 destroy_handle_table(&handle_table);
272 break;
274 return 1;
277 /******************************************************************************
278 * copy_param [Internal]
280 * Helper function that supports the standard WINAPI protocol for querying data
281 * of dynamic size.
283 * PARAMS
284 * pbBuffer [O] Buffer where the queried parameter is copied to, if it is large enough.
285 * May be NUL if the required buffer size is to be queried only.
286 * pdwBufferSize [I/O] In: Size of the buffer at pbBuffer
287 * Out: Size of parameter pbParam
288 * pbParam [I] Parameter value.
289 * dwParamSize [I] Size of pbParam
291 * RETURN
292 * Success: TRUE (pbParam was copied into pbBuffer or pbBuffer is NULL)
293 * Failure: FALSE (pbBuffer is not large enough to hold pbParam). Last error: ERROR_MORE_DATA
295 static inline BOOL copy_param(
296 BYTE *pbBuffer, DWORD *pdwBufferSize, CONST BYTE *pbParam, DWORD dwParamSize)
298 if (pbBuffer)
300 if (dwParamSize > *pdwBufferSize)
302 SetLastError(ERROR_MORE_DATA);
303 *pdwBufferSize = dwParamSize;
304 return FALSE;
306 memcpy(pbBuffer, pbParam, dwParamSize);
308 *pdwBufferSize = dwParamSize;
309 return TRUE;
312 /******************************************************************************
313 * get_algid_info [Internal]
315 * Query CSP capabilities for a given crypto algorithm.
317 * PARAMS
318 * pKeyContainer [I] Pointer to a key container of the CSP whose capabilities are to be queried.
319 * algid [I] Identifier of the crypto algorithm about which information is requested.
321 * RETURNS
322 * Success: Pointer to a PROV_ENUMALGS_EX struct containing information about the crypto algorithm.
323 * Failure: NULL (algid not supported)
325 static inline const PROV_ENUMALGS_EX* get_algid_info(KEYCONTAINER *pKeyContainer, ALG_ID algid) {
326 PROV_ENUMALGS_EX *iterator;
328 for (iterator = aProvEnumAlgsEx[pKeyContainer->dwPersonality]; iterator->aiAlgid; iterator++) {
329 if (iterator->aiAlgid == algid) return iterator;
332 return NULL;
335 /******************************************************************************
336 * free_hmac_info [Internal]
338 * Deeply free an HMAC_INFO struct.
340 * PARAMS
341 * hmac_info [I] Pointer to the HMAC_INFO struct to be freed.
343 * NOTES
344 * See Internet RFC 2104 for details on the HMAC algorithm.
346 static inline void free_hmac_info(PHMAC_INFO hmac_info) {
347 if (!hmac_info) return;
348 HeapFree(GetProcessHeap(), 0, hmac_info->pbInnerString);
349 HeapFree(GetProcessHeap(), 0, hmac_info->pbOuterString);
350 HeapFree(GetProcessHeap(), 0, hmac_info);
353 /******************************************************************************
354 * copy_hmac_info [Internal]
356 * Deeply copy an HMAC_INFO struct
358 * PARAMS
359 * dst [O] Pointer to a location where the pointer to the HMAC_INFO copy will be stored.
360 * src [I] Pointer to the HMAC_INFO struct to be copied.
362 * RETURNS
363 * Success: TRUE
364 * Failure: FALSE
366 * NOTES
367 * See Internet RFC 2104 for details on the HMAC algorithm.
369 static BOOL copy_hmac_info(PHMAC_INFO *dst, PHMAC_INFO src) {
370 if (!src) return FALSE;
371 *dst = (PHMAC_INFO)HeapAlloc(GetProcessHeap(), 0, sizeof(HMAC_INFO));
372 if (!*dst) return FALSE;
373 memcpy(*dst, src, sizeof(HMAC_INFO));
374 (*dst)->pbInnerString = NULL;
375 (*dst)->pbOuterString = NULL;
376 if ((*dst)->cbInnerString == 0) (*dst)->cbInnerString = RSAENH_HMAC_DEF_PAD_LEN;
377 (*dst)->pbInnerString = (BYTE*)HeapAlloc(GetProcessHeap(), 0, (*dst)->cbInnerString);
378 if (!(*dst)->pbInnerString) {
379 free_hmac_info(*dst);
380 return FALSE;
382 if (src->cbInnerString)
383 memcpy((*dst)->pbInnerString, src->pbInnerString, src->cbInnerString);
384 else
385 memset((*dst)->pbInnerString, RSAENH_HMAC_DEF_IPAD_CHAR, RSAENH_HMAC_DEF_PAD_LEN);
386 if ((*dst)->cbOuterString == 0) (*dst)->cbOuterString = RSAENH_HMAC_DEF_PAD_LEN;
387 (*dst)->pbOuterString = (BYTE*)HeapAlloc(GetProcessHeap(), 0, (*dst)->cbOuterString);
388 if (!(*dst)->pbOuterString) {
389 free_hmac_info(*dst);
390 return FALSE;
392 if (src->cbOuterString)
393 memcpy((*dst)->pbOuterString, src->pbOuterString, src->cbOuterString);
394 else
395 memset((*dst)->pbOuterString, RSAENH_HMAC_DEF_OPAD_CHAR, RSAENH_HMAC_DEF_PAD_LEN);
396 return TRUE;
399 /******************************************************************************
400 * destroy_hash [Internal]
402 * Destructor for hash objects
404 * PARAMS
405 * pCryptHash [I] Pointer to the hash object to be destroyed.
406 * Will be invalid after function returns!
408 static void destroy_hash(OBJECTHDR *pCryptHash)
410 free_hmac_info(((CRYPTHASH*)pCryptHash)->pHMACInfo);
411 HeapFree(GetProcessHeap(), 0, pCryptHash);
414 /******************************************************************************
415 * init_hash [Internal]
417 * Initialize (or reset) a hash object
419 * PARAMS
420 * pKeyContainer [I] Pointer to the key container the hash object belongs to.
421 * pCryptHash [I] The hash object to be initialized.
423 static inline BOOL init_hash(KEYCONTAINER *pKeyContainer, CRYPTHASH *pCryptHash) {
424 DWORD dwLen;
425 const PROV_ENUMALGS_EX *pAlgInfo;
427 switch (pCryptHash->aiAlgid)
429 case CALG_HMAC:
430 if (pCryptHash->pHMACInfo) {
431 pAlgInfo = get_algid_info(pKeyContainer, pCryptHash->pHMACInfo->HashAlgid);
432 if (pAlgInfo) pCryptHash->dwHashSize = pAlgInfo->dwDefaultLen >> 3;
433 return init_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context);
435 return TRUE;
437 case CALG_MAC:
438 dwLen = sizeof(DWORD);
439 RSAENH_CPGetKeyParam(pCryptHash->hProv, pCryptHash->hKey, KP_BLOCKLEN,
440 (BYTE*)&pCryptHash->dwHashSize, &dwLen, 0);
441 pCryptHash->dwHashSize >>= 3;
442 return TRUE;
444 default:
445 return init_hash_impl(pCryptHash->aiAlgid, &pCryptHash->context);
449 /******************************************************************************
450 * update_hash [Internal]
452 * Hashes the given data and updates the hash object's state accordingly
454 * PARAMS
455 * pCryptHash [I] Hash object to be updated.
456 * pbData [I] Pointer to data stream to be hashed.
457 * dwDataLen [I] Length of data stream.
459 static inline void update_hash(CRYPTHASH *pCryptHash, CONST BYTE *pbData, DWORD dwDataLen) {
460 BYTE *pbTemp;
462 switch (pCryptHash->aiAlgid)
464 case CALG_HMAC:
465 if (pCryptHash->pHMACInfo)
466 update_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
467 pbData, dwDataLen);
468 break;
470 case CALG_MAC:
471 pbTemp = (BYTE*)HeapAlloc(GetProcessHeap(), 0, dwDataLen);
472 if (!pbTemp) return;
473 memcpy(pbTemp, pbData, dwDataLen);
474 RSAENH_CPEncrypt(pCryptHash->hProv, pCryptHash->hKey, (HCRYPTHASH)NULL, FALSE, 0,
475 pbTemp, &dwDataLen, dwDataLen);
476 HeapFree(GetProcessHeap(), 0, pbTemp);
477 break;
479 default:
480 update_hash_impl(pCryptHash->aiAlgid, &pCryptHash->context, pbData, dwDataLen);
484 /******************************************************************************
485 * finalize_hash [Internal]
487 * Finalizes the hash, after all data has been hashed with update_hash.
488 * No additional data can be hashed afterwards until the hash gets initialized again.
490 * PARAMS
491 * pCryptHash [I] Hash object to be finalized.
493 static inline void finalize_hash(CRYPTHASH *pCryptHash) {
494 DWORD dwDataLen;
496 switch (pCryptHash->aiAlgid)
498 case CALG_HMAC:
499 if (pCryptHash->pHMACInfo)
500 finalize_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
501 pCryptHash->abHashValue);
502 break;
504 case CALG_MAC:
505 dwDataLen = 0;
506 RSAENH_CPEncrypt(pCryptHash->hProv, pCryptHash->hKey, (HCRYPTHASH)NULL, TRUE, 0,
507 pCryptHash->abHashValue, &dwDataLen, pCryptHash->dwHashSize);
508 break;
510 default:
511 finalize_hash_impl(pCryptHash->aiAlgid, &pCryptHash->context, pCryptHash->abHashValue);
515 /******************************************************************************
516 * destroy_key [Internal]
518 * Destructor for key objects
520 * PARAMS
521 * pCryptKey [I] Pointer to the key object to be destroyed.
522 * Will be invalid after function returns!
524 static void destroy_key(OBJECTHDR *pCryptKey)
526 free_key_impl(((CRYPTKEY*)pCryptKey)->aiAlgid, &((CRYPTKEY*)pCryptKey)->context);
527 HeapFree(GetProcessHeap(), 0, pCryptKey);
530 /******************************************************************************
531 * setup_key [Internal]
533 * Initialize (or reset) a key object
535 * PARAMS
536 * pCryptKey [I] The key object to be initialized.
538 static inline void setup_key(CRYPTKEY *pCryptKey) {
539 pCryptKey->dwState = RSAENH_KEYSTATE_IDLE;
540 memcpy(pCryptKey->abChainVector, pCryptKey->abInitVector, sizeof(pCryptKey->abChainVector));
541 setup_key_impl(pCryptKey->aiAlgid, &pCryptKey->context, pCryptKey->dwKeyLen,
542 pCryptKey->dwSaltLen, pCryptKey->abKeyValue);
545 /******************************************************************************
546 * new_key [Internal]
548 * Creates a new key object without assigning the actual binary key value.
549 * This is done by CPDeriveKey, CPGenKey or CPImportKey, which call this function.
551 * PARAMS
552 * hProv [I] Handle to the provider to which the created key will belong.
553 * aiAlgid [I] The new key shall use the crypto algorithm idenfied by aiAlgid.
554 * dwFlags [I] Upper 16 bits give the key length.
555 * Lower 16 bits: CRYPT_CREATE_SALT, CRYPT_NO_SALT
556 * ppCryptKey [O] Pointer to the created key
558 * RETURNS
559 * Success: Handle to the created key.
560 * Failure: INVALID_HANDLE_VALUE
562 static HCRYPTKEY new_key(HCRYPTPROV hProv, ALG_ID aiAlgid, DWORD dwFlags, CRYPTKEY **ppCryptKey)
564 KEYCONTAINER *pKeyContainer;
565 HCRYPTKEY hCryptKey;
566 CRYPTKEY *pCryptKey;
567 DWORD dwKeyLen = HIWORD(dwFlags);
568 const PROV_ENUMALGS_EX *peaAlgidInfo;
570 if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER, (OBJECTHDR**)&pKeyContainer))
572 SetLastError(NTE_BAD_UID);
573 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
577 * Retrieve the CSP's capabilities for the given ALG_ID value
579 peaAlgidInfo = get_algid_info(pKeyContainer, aiAlgid);
580 if (!peaAlgidInfo) {
581 SetLastError(NTE_BAD_ALGID);
582 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
586 * Assume the default key length, if none is specified explicitly
588 if (dwKeyLen == 0) dwKeyLen = peaAlgidInfo->dwDefaultLen;
591 * Check if the requested key length is supported by the current CSP.
592 * Adjust key length's for DES algorithms.
594 switch (aiAlgid) {
595 case CALG_DES:
596 if (dwKeyLen == RSAENH_DES_EFFECTIVE_KEYLEN) {
597 dwKeyLen = RSAENH_DES_STORAGE_KEYLEN;
599 if (dwKeyLen != RSAENH_DES_STORAGE_KEYLEN) {
600 SetLastError(NTE_BAD_FLAGS);
601 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
603 break;
605 case CALG_3DES_112:
606 if (dwKeyLen == RSAENH_3DES112_EFFECTIVE_KEYLEN) {
607 dwKeyLen = RSAENH_3DES112_STORAGE_KEYLEN;
609 if (dwKeyLen != RSAENH_3DES112_STORAGE_KEYLEN) {
610 SetLastError(NTE_BAD_FLAGS);
611 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
613 break;
615 case CALG_3DES:
616 if (dwKeyLen == RSAENH_3DES_EFFECTIVE_KEYLEN) {
617 dwKeyLen = RSAENH_3DES_STORAGE_KEYLEN;
619 if (dwKeyLen != RSAENH_3DES_STORAGE_KEYLEN) {
620 SetLastError(NTE_BAD_FLAGS);
621 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
623 break;
625 default:
626 if (dwKeyLen % 8 ||
627 dwKeyLen > peaAlgidInfo->dwMaxLen ||
628 dwKeyLen < peaAlgidInfo->dwMinLen)
630 SetLastError(NTE_BAD_FLAGS);
631 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
635 hCryptKey = (HCRYPTKEY)new_object(&handle_table, sizeof(CRYPTKEY), RSAENH_MAGIC_KEY,
636 destroy_key, (OBJECTHDR**)&pCryptKey);
637 if (hCryptKey != (HCRYPTKEY)INVALID_HANDLE_VALUE)
639 pCryptKey->aiAlgid = aiAlgid;
640 pCryptKey->hProv = hProv;
641 pCryptKey->dwModeBits = 0;
642 pCryptKey->dwPermissions = CRYPT_ENCRYPT | CRYPT_DECRYPT | CRYPT_READ | CRYPT_WRITE |
643 CRYPT_MAC;
644 pCryptKey->dwKeyLen = dwKeyLen >> 3;
645 if ((dwFlags & CRYPT_CREATE_SALT) || (dwKeyLen == 40 && !(dwFlags & CRYPT_NO_SALT)))
646 pCryptKey->dwSaltLen = 16 /*FIXME*/ - pCryptKey->dwKeyLen;
647 else
648 pCryptKey->dwSaltLen = 0;
649 memset(pCryptKey->abKeyValue, 0, sizeof(pCryptKey->abKeyValue));
650 memset(pCryptKey->abInitVector, 0, sizeof(pCryptKey->abInitVector));
652 switch(aiAlgid)
654 case CALG_RC4:
655 pCryptKey->dwBlockLen = 0;
656 pCryptKey->dwMode = 0;
657 break;
659 case CALG_RC2:
660 case CALG_DES:
661 case CALG_3DES_112:
662 case CALG_3DES:
663 pCryptKey->dwBlockLen = 8;
664 pCryptKey->dwMode = CRYPT_MODE_CBC;
665 break;
667 case CALG_RSA_KEYX:
668 case CALG_RSA_SIGN:
669 pCryptKey->dwBlockLen = dwKeyLen >> 3;
670 pCryptKey->dwMode = 0;
671 break;
674 *ppCryptKey = pCryptKey;
677 return hCryptKey;
680 /******************************************************************************
681 * destroy_key_container [Internal]
683 * Destructor for key containers. The user's signature and key exchange private
684 * keys are stored in the registry _IN_PLAINTEXT_.
686 * PARAMS
687 * pObjectHdr [I] Pointer to the key container to be destroyed.
689 static void destroy_key_container(OBJECTHDR *pObjectHdr)
691 KEYCONTAINER *pKeyContainer = (KEYCONTAINER*)pObjectHdr;
692 CRYPTKEY *pKey;
693 CHAR szRSABase[MAX_PATH];
694 HKEY hKey;
695 DWORD dwLen;
696 BYTE *pbKey;
698 if (!(pKeyContainer->dwFlags & CRYPT_VERIFYCONTEXT)) {
699 /* On WinXP, persistent keys are stored in a file located at:
700 * $AppData$\\Microsoft\\Crypto\\RSA\\$SID$\\some_hex_string
702 sprintf(szRSABase, RSAENH_REGKEY, pKeyContainer->szName);
704 if (RegCreateKeyExA(HKEY_CURRENT_USER, szRSABase, 0, NULL, REG_OPTION_NON_VOLATILE,
705 KEY_WRITE, NULL, &hKey, NULL) == ERROR_SUCCESS)
707 if (lookup_handle(&handle_table, pKeyContainer->hKeyExchangeKeyPair, RSAENH_MAGIC_KEY,
708 (OBJECTHDR**)&pKey))
710 if (RSAENH_CPExportKey(pKey->hProv, pKeyContainer->hKeyExchangeKeyPair, 0,
711 PRIVATEKEYBLOB, 0, 0, &dwLen))
713 pbKey = (BYTE*)HeapAlloc(GetProcessHeap(), 0, dwLen);
714 if (pbKey)
716 if (RSAENH_CPExportKey(pKey->hProv, pKeyContainer->hKeyExchangeKeyPair, 0,
717 PRIVATEKEYBLOB, 0, pbKey, &dwLen))
719 RegSetValueExA(hKey, "KeyExchangeKeyPair", 0, REG_BINARY, pbKey, dwLen);
721 HeapFree(GetProcessHeap(), 0, pbKey);
724 release_handle(&handle_table, (unsigned int)pKeyContainer->hKeyExchangeKeyPair,
725 RSAENH_MAGIC_KEY);
728 if (lookup_handle(&handle_table, pKeyContainer->hSignatureKeyPair, RSAENH_MAGIC_KEY,
729 (OBJECTHDR**)&pKey))
731 if (RSAENH_CPExportKey(pKey->hProv, pKeyContainer->hSignatureKeyPair, 0,
732 PRIVATEKEYBLOB, 0, 0, &dwLen))
734 pbKey = (BYTE*)HeapAlloc(GetProcessHeap(), 0, dwLen);
735 if (pbKey)
737 if (RSAENH_CPExportKey(pKey->hProv, pKeyContainer->hSignatureKeyPair, 0,
738 PRIVATEKEYBLOB, 0, pbKey, &dwLen))
740 RegSetValueExA(hKey, "SignatureKeyPair", 0, REG_BINARY, pbKey, dwLen);
742 HeapFree(GetProcessHeap(), 0, pbKey);
745 release_handle(&handle_table, (unsigned int)pKeyContainer->hSignatureKeyPair,
746 RSAENH_MAGIC_KEY);
749 RegCloseKey(hKey);
753 HeapFree( GetProcessHeap(), 0, pKeyContainer );
756 /******************************************************************************
757 * new_key_container [Internal]
759 * Create a new key container. The personality (RSA Base, Strong or Enhanced CP)
760 * of the CSP is determined via the pVTable->pszProvName string.
762 * PARAMS
763 * pszContainerName [I] Name of the key container.
764 * pVTable [I] Callback functions and context info provided by the OS
766 * RETURNS
767 * Success: Handle to the new key container.
768 * Failure: INVALID_HANDLE_VALUE
770 static HCRYPTPROV new_key_container(PCHAR pszContainerName, DWORD dwFlags, PVTableProvStruc pVTable)
772 KEYCONTAINER *pKeyContainer;
773 HCRYPTPROV hKeyContainer;
775 hKeyContainer = (HCRYPTPROV)new_object(&handle_table, sizeof(KEYCONTAINER), RSAENH_MAGIC_CONTAINER,
776 destroy_key_container, (OBJECTHDR**)&pKeyContainer);
777 if (hKeyContainer != (HCRYPTPROV)INVALID_HANDLE_VALUE)
779 strncpy(pKeyContainer->szName, pszContainerName, MAX_PATH);
780 pKeyContainer->szName[MAX_PATH-1] = '\0';
781 pKeyContainer->dwFlags = dwFlags;
782 pKeyContainer->dwEnumAlgsCtr = 0;
783 pKeyContainer->hKeyExchangeKeyPair = (HCRYPTKEY)INVALID_HANDLE_VALUE;
784 pKeyContainer->hSignatureKeyPair = (HCRYPTKEY)INVALID_HANDLE_VALUE;
785 if (pVTable && pVTable->pszProvName) {
786 strncpy(pKeyContainer->szProvName, pVTable->pszProvName, MAX_PATH);
787 pKeyContainer->szProvName[MAX_PATH-1] = '\0';
788 if (!strcmp(pVTable->pszProvName, MS_DEF_PROV_A)) {
789 pKeyContainer->dwPersonality = RSAENH_PERSONALITY_BASE;
790 } else if (!strcmp(pVTable->pszProvName, MS_ENHANCED_PROV_A)) {
791 pKeyContainer->dwPersonality = RSAENH_PERSONALITY_ENHANCED;
792 } else {
793 pKeyContainer->dwPersonality = RSAENH_PERSONALITY_STRONG;
798 return hKeyContainer;
801 /******************************************************************************
802 * read_key_container [Internal]
804 * Tries to read the persistent state of the key container (mainly the signature
805 * and key exchange private keys) given by pszContainerName.
807 * PARAMS
808 * pszContainerName [I] Name of the key container to read from the registry
809 * pVTable [I] Pointer to context data provided by the operating system
811 * RETURNS
812 * Success: Handle to the key container read from the registry
813 * Failure: INVALID_HANDLE_VALUE
815 static HCRYPTPROV read_key_container(PCHAR pszContainerName, DWORD dwFlags, PVTableProvStruc pVTable)
817 CHAR szRSABase[MAX_PATH];
818 BYTE *pbKey;
819 HKEY hKey;
820 DWORD dwValueType, dwLen;
821 KEYCONTAINER *pKeyContainer;
822 HCRYPTPROV hKeyContainer;
824 sprintf(szRSABase, RSAENH_REGKEY, pszContainerName);
826 if (RegOpenKeyExA(HKEY_CURRENT_USER, szRSABase, 0, KEY_READ, &hKey) != ERROR_SUCCESS)
828 SetLastError(NTE_BAD_KEYSET);
829 return (HCRYPTPROV)INVALID_HANDLE_VALUE;
832 hKeyContainer = new_key_container(pszContainerName, dwFlags, pVTable);
833 if (hKeyContainer != (HCRYPTPROV)INVALID_HANDLE_VALUE)
835 if (!lookup_handle(&handle_table, hKeyContainer, RSAENH_MAGIC_CONTAINER,
836 (OBJECTHDR**)&pKeyContainer))
837 return (HCRYPTPROV)INVALID_HANDLE_VALUE;
839 if (RegQueryValueExA(hKey, "KeyExchangeKeyPair", 0, &dwValueType, NULL, &dwLen) ==
840 ERROR_SUCCESS)
842 pbKey = (BYTE*)HeapAlloc(GetProcessHeap(), 0, dwLen);
843 if (pbKey)
845 if (RegQueryValueExA(hKey, "KeyExchangeKeyPair", 0, &dwValueType, pbKey, &dwLen) ==
846 ERROR_SUCCESS)
848 RSAENH_CPImportKey(hKeyContainer, pbKey, dwLen, 0, 0,
849 &pKeyContainer->hKeyExchangeKeyPair);
851 HeapFree(GetProcessHeap(), 0, pbKey);
855 if (RegQueryValueExA(hKey, "SignatureKeyPair", 0, &dwValueType, NULL, &dwLen) ==
856 ERROR_SUCCESS)
858 pbKey = (BYTE*)HeapAlloc(GetProcessHeap(), 0, dwLen);
859 if (pbKey)
861 if (RegQueryValueExA(hKey, "SignatureKeyPair", 0, &dwValueType, pbKey, &dwLen) ==
862 ERROR_SUCCESS)
864 RSAENH_CPImportKey(hKeyContainer, pbKey, dwLen, 0, 0,
865 &pKeyContainer->hSignatureKeyPair);
867 HeapFree(GetProcessHeap(), 0, pbKey);
872 return hKeyContainer;
875 /******************************************************************************
876 * build_hash_signature [Internal]
878 * Builds a padded version of a hash to match the length of the RSA key modulus.
880 * PARAMS
881 * pbSignature [O] The padded hash object is stored here.
882 * dwLen [I] Length of the pbSignature buffer.
883 * aiAlgid [I] Algorithm identifier of the hash to be padded.
884 * abHashValue [I] The value of the hash object.
885 * dwHashLen [I] Length of the hash value.
886 * dwFlags [I] Selection of padding algorithm.
888 * RETURNS
889 * Success: TRUE
890 * Failure: FALSE (NTE_BAD_ALGID)
892 static BOOL build_hash_signature(BYTE *pbSignature, DWORD dwLen, ALG_ID aiAlgid,
893 CONST BYTE *abHashValue, DWORD dwHashLen, DWORD dwFlags)
895 /* These prefixes are meant to be concatenated with hash values of the
896 * respective kind to form a PKCS #7 DigestInfo. */
897 static const struct tagOIDDescriptor {
898 ALG_ID aiAlgid;
899 DWORD dwLen;
900 CONST BYTE abOID[18];
901 } aOIDDescriptor[5] = {
902 { CALG_MD2, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
903 0x86, 0xf7, 0x0d, 0x02, 0x02, 0x05, 0x00, 0x04, 0x10 } },
904 { CALG_MD4, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
905 0x86, 0xf7, 0x0d, 0x02, 0x04, 0x05, 0x00, 0x04, 0x10 } },
906 { CALG_MD5, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
907 0x86, 0xf7, 0x0d, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10 } },
908 { CALG_SHA, 15, { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03,
909 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14 } },
910 { 0, 0, {} }
912 DWORD dwIdxOID, i, j;
914 for (dwIdxOID = 0; aOIDDescriptor[dwIdxOID].aiAlgid; dwIdxOID++) {
915 if (aOIDDescriptor[dwIdxOID].aiAlgid == aiAlgid) break;
918 if (!aOIDDescriptor[dwIdxOID].aiAlgid) {
919 SetLastError(NTE_BAD_ALGID);
920 return FALSE;
923 /* Build the padded signature */
924 if (dwFlags & CRYPT_X931_FORMAT) {
925 pbSignature[0] = 0x6b;
926 for (i=1; i < dwLen - dwHashLen - 3; i++) {
927 pbSignature[i] = 0xbb;
929 pbSignature[i++] = 0xba;
930 for (j=0; j < dwHashLen; j++, i++) {
931 pbSignature[i] = abHashValue[j];
933 pbSignature[i++] = 0x33;
934 pbSignature[i++] = 0xcc;
935 } else {
936 pbSignature[0] = 0x00;
937 pbSignature[1] = 0x01;
938 if (dwFlags & CRYPT_NOHASHOID) {
939 for (i=2; i < dwLen - 1 - dwHashLen; i++) {
940 pbSignature[i] = 0xff;
942 pbSignature[i++] = 0x00;
943 } else {
944 for (i=2; i < dwLen - 1 - aOIDDescriptor[dwIdxOID].dwLen - dwHashLen; i++) {
945 pbSignature[i] = 0xff;
947 pbSignature[i++] = 0x00;
948 for (j=0; j < aOIDDescriptor[dwIdxOID].dwLen; j++) {
949 pbSignature[i++] = aOIDDescriptor[dwIdxOID].abOID[j];
952 for (j=0; j < dwHashLen; j++) {
953 pbSignature[i++] = abHashValue[j];
957 return TRUE;
960 /******************************************************************************
961 * CPAcquireContext (RSAENH.@)
963 * Acquire a handle to the key container specified by pszContainer
965 * PARAMS
966 * phProv [O] Pointer to the location the acquired handle will be written to.
967 * pszContainer [I] Name of the desired key container. See Notes
968 * dwFlags [I] Flags. See Notes.
969 * pVTable [I] Pointer to a PVTableProvStruct containing callbacks.
971 * RETURNS
972 * Success: TRUE
973 * Failure: FALSE
975 * NOTES
976 * If pszContainer is NULL or points to a zero length string the user's login
977 * name will be used as the key container name.
979 * If the CRYPT_NEW_KEYSET flag is set in dwFlags a new keyset will be created.
980 * If a keyset with the given name already exists, the function fails and sets
981 * last error to NTE_EXISTS. If CRYPT_NEW_KEYSET is not set and the specified
982 * key container does not exist, function fails and sets last error to
983 * NTE_BAD_KEYSET.
985 BOOL WINAPI RSAENH_CPAcquireContext(HCRYPTPROV *phProv, LPSTR pszContainer,
986 DWORD dwFlags, PVTableProvStruc pVTable)
988 DWORD dwLen;
989 CHAR szKeyContainerName[MAX_PATH] = "";
990 CHAR szRegKey[MAX_PATH];
992 TRACE("(phProv=%p, pszContainer=%s, dwFlags=%08lx, pVTable=%p)\n", phProv,
993 debugstr_a(pszContainer), dwFlags, pVTable);
995 SetLastError(ERROR_SUCCESS);
997 if (pszContainer ? strlen(pszContainer) : 0)
999 strncpy(szKeyContainerName, pszContainer, MAX_PATH);
1000 szKeyContainerName[MAX_PATH-1] = '\0';
1002 else
1004 dwLen = MAX_PATH;
1005 if (!GetUserNameA(szKeyContainerName, &dwLen)) return FALSE;
1008 switch (dwFlags & (CRYPT_NEWKEYSET|CRYPT_VERIFYCONTEXT|CRYPT_DELETEKEYSET))
1010 case 0:
1011 *phProv = read_key_container(szKeyContainerName, dwFlags, pVTable);
1012 break;
1014 case CRYPT_DELETEKEYSET:
1015 if (snprintf(szRegKey, MAX_PATH, RSAENH_REGKEY, pszContainer) >= MAX_PATH) {
1016 SetLastError(NTE_BAD_KEYSET_PARAM);
1017 return FALSE;
1018 } else {
1019 RegDeleteKeyA(HKEY_CURRENT_USER, szRegKey);
1020 return TRUE;
1022 break;
1024 case CRYPT_NEWKEYSET:
1025 *phProv = read_key_container(szKeyContainerName, dwFlags, pVTable);
1026 if (*phProv != (HCRYPTPROV)INVALID_HANDLE_VALUE)
1028 release_handle(&handle_table, (unsigned int)*phProv, RSAENH_MAGIC_CONTAINER);
1029 SetLastError(NTE_EXISTS);
1030 return FALSE;
1032 *phProv = new_key_container(szKeyContainerName, dwFlags, pVTable);
1033 break;
1035 case CRYPT_VERIFYCONTEXT:
1036 if (pszContainer) {
1037 SetLastError(NTE_BAD_FLAGS);
1038 return FALSE;
1040 *phProv = new_key_container("", dwFlags, pVTable);
1041 break;
1043 default:
1044 *phProv = (unsigned int)INVALID_HANDLE_VALUE;
1045 SetLastError(NTE_BAD_FLAGS);
1046 return FALSE;
1049 return *phProv != (unsigned int)INVALID_HANDLE_VALUE;
1052 /******************************************************************************
1053 * CPCreateHash (RSAENH.@)
1055 * CPCreateHash creates and initalizes a new hash object.
1057 * PARAMS
1058 * hProv [I] Handle to the key container to which the new hash will belong.
1059 * Algid [I] Identifies the hash algorithm, which will be used for the hash.
1060 * hKey [I] Handle to a session key applied for keyed hashes.
1061 * dwFlags [I] Currently no flags defined. Must be zero.
1062 * phHash [O] Points to the location where a handle to the new hash will be stored.
1064 * RETURNS
1065 * Success: TRUE
1066 * Failure: FALSE
1068 * NOTES
1069 * hKey is a handle to a session key applied in keyed hashes like MAC and HMAC.
1070 * If a normal hash object is to be created (like e.g. MD2 or SHA1) hKey must be zero.
1072 BOOL WINAPI RSAENH_CPCreateHash(HCRYPTPROV hProv, ALG_ID Algid, HCRYPTKEY hKey, DWORD dwFlags,
1073 HCRYPTHASH *phHash)
1075 KEYCONTAINER *pKeyContainer;
1076 CRYPTHASH *pCryptHash;
1077 const PROV_ENUMALGS_EX *peaAlgidInfo;
1079 TRACE("(hProv=%08lx, Algid=%08x, hKey=%08lx, dwFlags=%08lx, phHash=%p)\n", hProv, Algid, hKey,
1080 dwFlags, phHash);
1082 if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER, (OBJECTHDR**)&pKeyContainer))
1084 SetLastError(NTE_BAD_UID);
1085 return FALSE;
1088 peaAlgidInfo = get_algid_info(pKeyContainer, Algid);
1089 if (!peaAlgidInfo)
1091 SetLastError(NTE_BAD_ALGID);
1092 return FALSE;
1095 if (dwFlags)
1097 SetLastError(NTE_BAD_FLAGS);
1098 return FALSE;
1101 if ((Algid == CALG_MAC || Algid == CALG_HMAC)) {
1102 CRYPTKEY *pCryptKey;
1104 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey)) {
1105 SetLastError(NTE_BAD_KEY);
1106 return FALSE;
1109 if ((Algid == CALG_MAC) && (GET_ALG_TYPE(pCryptKey->aiAlgid) != ALG_TYPE_BLOCK)) {
1110 SetLastError(NTE_BAD_KEY);
1111 return FALSE;
1115 *phHash = (HCRYPTHASH)new_object(&handle_table, sizeof(CRYPTHASH), RSAENH_MAGIC_HASH,
1116 destroy_hash, (OBJECTHDR**)&pCryptHash);
1117 if (!pCryptHash) return FALSE;
1119 pCryptHash->aiAlgid = Algid;
1120 pCryptHash->hKey = hKey;
1121 pCryptHash->hProv = hProv;
1122 pCryptHash->dwState = RSAENH_HASHSTATE_IDLE;
1123 pCryptHash->pHMACInfo = (PHMAC_INFO)NULL;
1124 pCryptHash->dwHashSize = peaAlgidInfo->dwDefaultLen >> 3;
1126 return init_hash(pKeyContainer, pCryptHash);
1129 /******************************************************************************
1130 * CPDestroyHash (RSAENH.@)
1132 * Releases the handle to a hash object. The object is destroyed if it's reference
1133 * count reaches zero.
1135 * PARAMS
1136 * hProv [I] Handle to the key container to which the hash object belongs.
1137 * hHash [I] Handle to the hash object to be released.
1139 * RETURNS
1140 * Success: TRUE
1141 * Failure: FALSE
1143 BOOL WINAPI RSAENH_CPDestroyHash(HCRYPTPROV hProv, HCRYPTHASH hHash)
1145 TRACE("(hProv=%08lx, hHash=%08lx)\n", hProv, hHash);
1147 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
1149 SetLastError(NTE_BAD_UID);
1150 return FALSE;
1153 if (!release_handle(&handle_table, hHash, RSAENH_MAGIC_HASH))
1155 SetLastError(NTE_BAD_HASH);
1156 return FALSE;
1159 return TRUE;
1162 /******************************************************************************
1163 * CPDestroyKey (RSAENH.@)
1165 * Releases the handle to a key object. The object is destroyed if it's reference
1166 * count reaches zero.
1168 * PARAMS
1169 * hProv [I] Handle to the key container to which the key object belongs.
1170 * hKey [I] Handle to the key object to be released.
1172 * RETURNS
1173 * Success: TRUE
1174 * Failure: FALSE
1176 BOOL WINAPI RSAENH_CPDestroyKey(HCRYPTPROV hProv, HCRYPTKEY hKey)
1178 TRACE("(hProv=%08lx, hKey=%08lx)\n", hProv, hKey);
1180 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
1182 SetLastError(NTE_BAD_UID);
1183 return FALSE;
1186 if (!release_handle(&handle_table, hKey, RSAENH_MAGIC_KEY))
1188 SetLastError(NTE_BAD_KEY);
1189 return FALSE;
1192 return TRUE;
1195 /******************************************************************************
1196 * CPDuplicateHash (RSAENH.@)
1198 * Clones a hash object including it's current state.
1200 * PARAMS
1201 * hUID [I] Handle to the key container the hash belongs to.
1202 * hHash [I] Handle to the hash object to be cloned.
1203 * pdwReserved [I] Reserved. Must be NULL.
1204 * dwFlags [I] No flags are currently defined. Must be 0.
1205 * phHash [O] Handle to the cloned hash object.
1207 * RETURNS
1208 * Success: TRUE.
1209 * Failure: FALSE.
1211 BOOL WINAPI RSAENH_CPDuplicateHash(HCRYPTPROV hUID, HCRYPTHASH hHash, DWORD *pdwReserved,
1212 DWORD dwFlags, HCRYPTHASH *phHash)
1214 CRYPTHASH *pSrcHash, *pDestHash;
1216 TRACE("(hUID=%08lx, hHash=%08lx, pdwReserved=%p, dwFlags=%08lx, phHash=%p)\n", hUID, hHash,
1217 pdwReserved, dwFlags, phHash);
1219 if (!is_valid_handle(&handle_table, hUID, RSAENH_MAGIC_CONTAINER))
1221 SetLastError(NTE_BAD_UID);
1222 return FALSE;
1225 if (!lookup_handle(&handle_table, hHash, RSAENH_MAGIC_HASH, (OBJECTHDR**)&pSrcHash))
1227 SetLastError(NTE_BAD_HASH);
1228 return FALSE;
1231 if (!phHash || pdwReserved || dwFlags)
1233 SetLastError(ERROR_INVALID_PARAMETER);
1234 return FALSE;
1237 *phHash = (HCRYPTHASH)new_object(&handle_table, sizeof(CRYPTHASH), RSAENH_MAGIC_HASH,
1238 destroy_hash, (OBJECTHDR**)&pDestHash);
1239 if (*phHash != (HCRYPTHASH)INVALID_HANDLE_VALUE)
1241 memcpy(pDestHash, pSrcHash, sizeof(CRYPTHASH));
1242 duplicate_hash_impl(pSrcHash->aiAlgid, &pSrcHash->context, &pDestHash->context);
1243 copy_hmac_info(&pDestHash->pHMACInfo, pSrcHash->pHMACInfo);
1246 return *phHash != (HCRYPTHASH)INVALID_HANDLE_VALUE;
1249 /******************************************************************************
1250 * CPDuplicateKey (RSAENH.@)
1252 * Clones a key object including it's current state.
1254 * PARAMS
1255 * hUID [I] Handle to the key container the hash belongs to.
1256 * hKey [I] Handle to the key object to be cloned.
1257 * pdwReserved [I] Reserved. Must be NULL.
1258 * dwFlags [I] No flags are currently defined. Must be 0.
1259 * phHash [O] Handle to the cloned key object.
1261 * RETURNS
1262 * Success: TRUE.
1263 * Failure: FALSE.
1265 BOOL WINAPI RSAENH_CPDuplicateKey(HCRYPTPROV hUID, HCRYPTKEY hKey, DWORD *pdwReserved,
1266 DWORD dwFlags, HCRYPTKEY *phKey)
1268 CRYPTKEY *pSrcKey, *pDestKey;
1270 TRACE("(hUID=%08lx, hKey=%08lx, pdwReserved=%p, dwFlags=%08lx, phKey=%p)\n", hUID, hKey,
1271 pdwReserved, dwFlags, phKey);
1273 if (!is_valid_handle(&handle_table, hUID, RSAENH_MAGIC_CONTAINER))
1275 SetLastError(NTE_BAD_UID);
1276 return FALSE;
1279 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pSrcKey))
1281 SetLastError(NTE_BAD_KEY);
1282 return FALSE;
1285 if (!phKey || pdwReserved || dwFlags)
1287 SetLastError(ERROR_INVALID_PARAMETER);
1288 return FALSE;
1291 *phKey = (HCRYPTKEY)new_object(&handle_table, sizeof(CRYPTKEY), RSAENH_MAGIC_KEY, destroy_key,
1292 (OBJECTHDR**)&pDestKey);
1293 if (*phKey != (HCRYPTKEY)INVALID_HANDLE_VALUE)
1295 memcpy(pDestKey, pSrcKey, sizeof(CRYPTKEY));
1296 duplicate_key_impl(pSrcKey->aiAlgid, &pSrcKey->context, &pDestKey->context);
1297 return TRUE;
1299 else
1301 return FALSE;
1305 /******************************************************************************
1306 * CPEncrypt (RSAENH.@)
1308 * Encrypt data.
1310 * PARAMS
1311 * hProv [I] The key container hKey and hHash belong to.
1312 * hKey [I] The key used to encrypt the data.
1313 * hHash [I] An optional hash object for parallel hashing. See notes.
1314 * Final [I] Indicates if this is the last block of data to encrypt.
1315 * dwFlags [I] Currently no flags defined. Must be zero.
1316 * pbData [I/O] Pointer to the data to encrypt. Encrypted data will also be stored there.
1317 * pdwDataLen [I/O] I: Length of data to encrypt, O: Length of encrypted data.
1318 * dwBufLen [I] Size of the buffer at pbData.
1320 * RETURNS
1321 * Success: TRUE.
1322 * Failure: FALSE.
1324 * NOTES
1325 * If a hash object handle is provided in hHash, it will be updated with the plaintext.
1326 * This is useful for message signatures.
1328 * This function uses the standard WINAPI protocol for querying data of dynamic length.
1330 BOOL WINAPI RSAENH_CPEncrypt(HCRYPTPROV hProv, HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final,
1331 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen, DWORD dwBufLen)
1333 CRYPTKEY *pCryptKey;
1334 BYTE *in, out[RSAENH_MAX_BLOCK_SIZE], o[RSAENH_MAX_BLOCK_SIZE];
1335 DWORD dwEncryptedLen, i, j, k;
1337 TRACE("(hProv=%08lx, hKey=%08lx, hHash=%08lx, Final=%d, dwFlags=%08lx, pbData=%p, "
1338 "pdwDataLen=%p, dwBufLen=%ld)\n", hProv, hKey, hHash, Final, dwFlags, pbData, pdwDataLen,
1339 dwBufLen);
1341 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
1343 SetLastError(NTE_BAD_UID);
1344 return FALSE;
1347 if (dwFlags)
1349 SetLastError(NTE_BAD_FLAGS);
1350 return FALSE;
1353 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
1355 SetLastError(NTE_BAD_KEY);
1356 return FALSE;
1359 if (pCryptKey->dwState == RSAENH_KEYSTATE_IDLE)
1360 pCryptKey->dwState = RSAENH_KEYSTATE_ENCRYPTING;
1362 if (pCryptKey->dwState != RSAENH_KEYSTATE_ENCRYPTING)
1364 SetLastError(NTE_BAD_DATA);
1365 return FALSE;
1368 if (is_valid_handle(&handle_table, hHash, RSAENH_MAGIC_HASH)) {
1369 if (!RSAENH_CPHashData(hProv, hHash, pbData, *pdwDataLen, 0)) return FALSE;
1372 if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_BLOCK) {
1373 if (!Final && (*pdwDataLen % pCryptKey->dwBlockLen)) {
1374 SetLastError(NTE_BAD_DATA);
1375 return FALSE;
1378 dwEncryptedLen = (*pdwDataLen/pCryptKey->dwBlockLen+(Final?1:0))*pCryptKey->dwBlockLen;
1379 for (i=*pdwDataLen; i<dwEncryptedLen; i++) pbData[i] = dwEncryptedLen - *pdwDataLen;
1380 *pdwDataLen = dwEncryptedLen;
1382 if (*pdwDataLen > dwBufLen)
1384 SetLastError(ERROR_MORE_DATA);
1385 return FALSE;
1388 for (i=0, in=pbData; i<*pdwDataLen; i+=pCryptKey->dwBlockLen, in+=pCryptKey->dwBlockLen) {
1389 switch (pCryptKey->dwMode) {
1390 case CRYPT_MODE_ECB:
1391 encrypt_block_impl(pCryptKey->aiAlgid, &pCryptKey->context, in, out,
1392 RSAENH_ENCRYPT);
1393 break;
1395 case CRYPT_MODE_CBC:
1396 for (j=0; j<pCryptKey->dwBlockLen; j++) in[j] ^= pCryptKey->abChainVector[j];
1397 encrypt_block_impl(pCryptKey->aiAlgid, &pCryptKey->context, in, out,
1398 RSAENH_ENCRYPT);
1399 memcpy(pCryptKey->abChainVector, out, pCryptKey->dwBlockLen);
1400 break;
1402 case CRYPT_MODE_CFB:
1403 for (j=0; j<pCryptKey->dwBlockLen; j++) {
1404 encrypt_block_impl(pCryptKey->aiAlgid, &pCryptKey->context,
1405 pCryptKey->abChainVector, o, RSAENH_ENCRYPT);
1406 out[j] = in[j] ^ o[0];
1407 for (k=0; k<pCryptKey->dwBlockLen-1; k++)
1408 pCryptKey->abChainVector[k] = pCryptKey->abChainVector[k+1];
1409 pCryptKey->abChainVector[k] = out[j];
1411 break;
1413 default:
1414 SetLastError(NTE_BAD_ALGID);
1415 return FALSE;
1417 memcpy(in, out, pCryptKey->dwBlockLen);
1419 } else if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_STREAM) {
1420 encrypt_stream_impl(pCryptKey->aiAlgid, &pCryptKey->context, pbData, *pdwDataLen);
1423 if (Final) setup_key(pCryptKey);
1425 return TRUE;
1428 /******************************************************************************
1429 * CPDecrypt (RSAENH.@)
1431 * Decrypt data.
1433 * PARAMS
1434 * hProv [I] The key container hKey and hHash belong to.
1435 * hKey [I] The key used to decrypt the data.
1436 * hHash [I] An optional hash object for parallel hashing. See notes.
1437 * Final [I] Indicates if this is the last block of data to decrypt.
1438 * dwFlags [I] Currently no flags defined. Must be zero.
1439 * pbData [I/O] Pointer to the data to decrypt. Plaintext will also be stored there.
1440 * pdwDataLen [I/O] I: Length of ciphertext, O: Length of plaintext.
1442 * RETURNS
1443 * Success: TRUE.
1444 * Failure: FALSE.
1446 * NOTES
1447 * If a hash object handle is provided in hHash, it will be updated with the plaintext.
1448 * This is useful for message signatures.
1450 * This function uses the standard WINAPI protocol for querying data of dynamic length.
1452 BOOL WINAPI RSAENH_CPDecrypt(HCRYPTPROV hProv, HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final,
1453 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
1455 CRYPTKEY *pCryptKey;
1456 BYTE *in, out[RSAENH_MAX_BLOCK_SIZE], o[RSAENH_MAX_BLOCK_SIZE];
1457 DWORD i, j, k;
1459 TRACE("(hProv=%08lx, hKey=%08lx, hHash=%08lx, Final=%d, dwFlags=%08lx, pbData=%p, "
1460 "pdwDataLen=%p)\n", hProv, hKey, hHash, Final, dwFlags, pbData, pdwDataLen);
1462 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
1464 SetLastError(NTE_BAD_UID);
1465 return FALSE;
1468 if (dwFlags)
1470 SetLastError(NTE_BAD_FLAGS);
1471 return FALSE;
1474 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
1476 SetLastError(NTE_BAD_KEY);
1477 return FALSE;
1480 if (pCryptKey->dwState == RSAENH_KEYSTATE_IDLE)
1481 pCryptKey->dwState = RSAENH_KEYSTATE_DECRYPTING;
1483 if (pCryptKey->dwState != RSAENH_KEYSTATE_DECRYPTING)
1485 SetLastError(NTE_BAD_DATA);
1486 return FALSE;
1489 if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_BLOCK) {
1490 for (i=0, in=pbData; i<*pdwDataLen; i+=pCryptKey->dwBlockLen, in+=pCryptKey->dwBlockLen) {
1491 switch (pCryptKey->dwMode) {
1492 case CRYPT_MODE_ECB:
1493 encrypt_block_impl(pCryptKey->aiAlgid, &pCryptKey->context, in, out,
1494 RSAENH_DECRYPT);
1495 break;
1497 case CRYPT_MODE_CBC:
1498 encrypt_block_impl(pCryptKey->aiAlgid, &pCryptKey->context, in, out,
1499 RSAENH_DECRYPT);
1500 for (j=0; j<pCryptKey->dwBlockLen; j++) out[j] ^= pCryptKey->abChainVector[j];
1501 memcpy(pCryptKey->abChainVector, in, pCryptKey->dwBlockLen);
1502 break;
1504 case CRYPT_MODE_CFB:
1505 for (j=0; j<pCryptKey->dwBlockLen; j++) {
1506 encrypt_block_impl(pCryptKey->aiAlgid, &pCryptKey->context,
1507 pCryptKey->abChainVector, o, RSAENH_ENCRYPT);
1508 out[j] = in[j] ^ o[0];
1509 for (k=0; k<pCryptKey->dwBlockLen-1; k++)
1510 pCryptKey->abChainVector[k] = pCryptKey->abChainVector[k+1];
1511 pCryptKey->abChainVector[k] = in[j];
1513 break;
1515 default:
1516 SetLastError(NTE_BAD_ALGID);
1517 return FALSE;
1519 memcpy(in, out, pCryptKey->dwBlockLen);
1521 if (Final) *pdwDataLen -= pbData[*pdwDataLen-1];
1523 } else if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_STREAM) {
1524 encrypt_stream_impl(pCryptKey->aiAlgid, &pCryptKey->context, pbData, *pdwDataLen);
1525 } else if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_RSA) {
1526 encrypt_block_impl(pCryptKey->aiAlgid, &pCryptKey->context, pbData, pbData, RSAENH_DECRYPT);
1528 if (Final) setup_key(pCryptKey);
1530 if (is_valid_handle(&handle_table, hHash, RSAENH_MAGIC_HASH)) {
1531 if (!RSAENH_CPHashData(hProv, hHash, pbData, *pdwDataLen, 0)) return FALSE;
1534 return TRUE;
1537 /******************************************************************************
1538 * CPExportKey (RSAENH.@)
1540 * Export a key into a binary large object (BLOB).
1542 * PARAMS
1543 * hProv [I] Key container from which a key is to be exported.
1544 * hKey [I] Key to be exported.
1545 * hPubKey [I] Key used to encrypt sensitive BLOB data.
1546 * dwBlobType [I] SIMPLEBLOB, PUBLICKEYBLOB or PRIVATEKEYBLOB.
1547 * dwFlags [I] Currently none defined.
1548 * pbData [O] Pointer to a buffer where the BLOB will be written to.
1549 * pdwDataLen [I/O] I: Size of buffer at pbData, O: Size of BLOB
1551 * RETURNS
1552 * Success: TRUE.
1553 * Failure: FALSE.
1555 BOOL WINAPI RSAENH_CPExportKey(HCRYPTPROV hProv, HCRYPTKEY hKey, HCRYPTKEY hPubKey,
1556 DWORD dwBlobType, DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
1558 CRYPTKEY *pCryptKey, *pPubKey;
1559 BLOBHEADER *pBlobHeader = (BLOBHEADER*)pbData;
1560 RSAPUBKEY *pRSAPubKey = (RSAPUBKEY*)(pBlobHeader+1);
1561 ALG_ID *pAlgid = (ALG_ID*)(pBlobHeader+1);
1562 DWORD dwDataLen, i;
1563 BYTE *pbRawData;
1565 TRACE("(hProv=%08lx, hKey=%08lx, hPubKey=%08lx, dwBlobType=%08lx, dwFlags=%08lx, pbData=%p,"
1566 "pdwDataLen=%p)\n", hProv, hKey, hPubKey, dwBlobType, dwFlags, pbData, pdwDataLen);
1568 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
1570 SetLastError(NTE_BAD_UID);
1571 return FALSE;
1574 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
1576 SetLastError(NTE_BAD_KEY);
1577 return FALSE;
1580 switch ((BYTE)dwBlobType)
1582 case SIMPLEBLOB:
1583 if (!lookup_handle(&handle_table, hPubKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pPubKey)){
1584 SetLastError(NTE_BAD_PUBLIC_KEY); /* FIXME: error_code? */
1585 return FALSE;
1588 if (GET_ALG_CLASS(pCryptKey->aiAlgid) != ALG_CLASS_DATA_ENCRYPT) {
1589 SetLastError(NTE_BAD_KEY); /* FIXME: error code? */
1590 return FALSE;
1593 dwDataLen = sizeof(BLOBHEADER) + sizeof(ALG_ID) + pPubKey->dwBlockLen;
1594 if (pbData) {
1595 if (*pdwDataLen < dwDataLen) {
1596 SetLastError(ERROR_MORE_DATA);
1597 *pdwDataLen = dwDataLen;
1598 return FALSE;
1601 pBlobHeader->bType = SIMPLEBLOB;
1602 pBlobHeader->bVersion = CUR_BLOB_VERSION;
1603 pBlobHeader->reserved = 0;
1604 pBlobHeader->aiKeyAlg = pCryptKey->aiAlgid;
1606 *pAlgid = pPubKey->aiAlgid;
1608 pbRawData = (BYTE*)(pAlgid+1);
1609 pbRawData[0] = 0x00;
1610 pbRawData[1] = RSAENH_PKC_BLOCKTYPE;
1611 for (i=2; i < pPubKey->dwBlockLen - pCryptKey->dwKeyLen - 1; i++)
1612 do gen_rand_impl(&pbRawData[i], 1); while (!pbRawData[i]);
1613 pbRawData[i] = 0x00;
1614 for (i=0; i<pCryptKey->dwKeyLen; i++)
1615 pbRawData[pPubKey->dwBlockLen - pCryptKey->dwKeyLen + i] =
1616 pCryptKey->abKeyValue[i];
1618 encrypt_block_impl(pPubKey->aiAlgid, &pPubKey->context, pbRawData, pbRawData,
1619 RSAENH_ENCRYPT);
1621 *pdwDataLen = dwDataLen;
1622 return TRUE;
1624 case PUBLICKEYBLOB:
1625 if (is_valid_handle(&handle_table, hPubKey, RSAENH_MAGIC_KEY)) {
1626 SetLastError(NTE_BAD_KEY); /* FIXME: error code? */
1627 return FALSE;
1630 if ((pCryptKey->aiAlgid != CALG_RSA_KEYX) && (pCryptKey->aiAlgid != CALG_RSA_SIGN)) {
1631 SetLastError(NTE_BAD_KEY);
1632 return FALSE;
1635 dwDataLen = sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) + pCryptKey->dwKeyLen;
1636 if (pbData) {
1637 if (*pdwDataLen < dwDataLen) {
1638 SetLastError(ERROR_MORE_DATA);
1639 *pdwDataLen = dwDataLen;
1640 return FALSE;
1643 pBlobHeader->bType = PUBLICKEYBLOB;
1644 pBlobHeader->bVersion = CUR_BLOB_VERSION;
1645 pBlobHeader->reserved = 0;
1646 pBlobHeader->aiKeyAlg = pCryptKey->aiAlgid;
1648 pRSAPubKey->magic = RSAENH_MAGIC_RSA1;
1649 pRSAPubKey->bitlen = pCryptKey->dwKeyLen << 3;
1651 export_public_key_impl((BYTE*)(pRSAPubKey+1), &pCryptKey->context,
1652 pCryptKey->dwKeyLen, &pRSAPubKey->pubexp);
1654 *pdwDataLen = dwDataLen;
1655 return TRUE;
1657 case PRIVATEKEYBLOB:
1658 if ((pCryptKey->aiAlgid != CALG_RSA_KEYX) && (pCryptKey->aiAlgid != CALG_RSA_SIGN)) {
1659 SetLastError(NTE_BAD_KEY);
1660 return FALSE;
1663 dwDataLen = sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) +
1664 2 * pCryptKey->dwKeyLen + 5 * ((pCryptKey->dwKeyLen + 1) >> 1);
1665 if (pbData) {
1666 if (*pdwDataLen < dwDataLen) {
1667 SetLastError(ERROR_MORE_DATA);
1668 *pdwDataLen = dwDataLen;
1669 return FALSE;
1672 pBlobHeader->bType = PRIVATEKEYBLOB;
1673 pBlobHeader->bVersion = CUR_BLOB_VERSION;
1674 pBlobHeader->reserved = 0;
1675 pBlobHeader->aiKeyAlg = pCryptKey->aiAlgid;
1677 pRSAPubKey->magic = RSAENH_MAGIC_RSA2;
1678 pRSAPubKey->bitlen = pCryptKey->dwKeyLen << 3;
1680 export_private_key_impl((BYTE*)(pRSAPubKey+1), &pCryptKey->context,
1681 pCryptKey->dwKeyLen, &pRSAPubKey->pubexp);
1683 *pdwDataLen = dwDataLen;
1684 return TRUE;
1686 default:
1687 SetLastError(NTE_BAD_TYPE); /* FIXME: error code? */
1688 return FALSE;
1692 /******************************************************************************
1693 * CPImportKey (RSAENH.@)
1695 * Import a BLOB'ed key into a key container.
1697 * PARAMS
1698 * hProv [I] Key container into which the key is to be imported.
1699 * pbData [I] Pointer to a buffer which holds the BLOB.
1700 * dwDataLen [I] Length of data in buffer at pbData.
1701 * hPubKey [I] Key used to decrypt sensitive BLOB data.
1702 * dwFlags [I] Currently none defined.
1703 * phKey [O] Handle to the imported key.
1705 * RETURNS
1706 * Success: TRUE.
1707 * Failure: FALSE.
1709 BOOL WINAPI RSAENH_CPImportKey(HCRYPTPROV hProv, CONST BYTE *pbData, DWORD dwDataLen,
1710 HCRYPTKEY hPubKey, DWORD dwFlags, HCRYPTKEY *phKey)
1712 KEYCONTAINER *pKeyContainer;
1713 CRYPTKEY *pCryptKey, *pPubKey;
1714 CONST BLOBHEADER *pBlobHeader = (CONST BLOBHEADER*)pbData;
1715 CONST RSAPUBKEY *pRSAPubKey = (CONST RSAPUBKEY*)(pBlobHeader+1);
1716 CONST ALG_ID *pAlgid = (CONST ALG_ID*)(pBlobHeader+1);
1717 CONST BYTE *pbKeyStream = (CONST BYTE*)(pAlgid + 1);
1718 BYTE *pbDecrypted;
1719 DWORD dwKeyLen, i;
1721 TRACE("(hProv=%08lx, pbData=%p, dwDataLen=%ld, hPubKey=%08lx, dwFlags=%08lx, phKey=%p)\n",
1722 hProv, pbData, dwDataLen, hPubKey, dwFlags, phKey);
1724 if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER, (OBJECTHDR**)&pKeyContainer))
1726 SetLastError(NTE_BAD_UID);
1727 return FALSE;
1730 if (dwDataLen < sizeof(BLOBHEADER) ||
1731 pBlobHeader->bVersion != CUR_BLOB_VERSION ||
1732 pBlobHeader->reserved != 0)
1734 SetLastError(NTE_BAD_DATA);
1735 return FALSE;
1738 switch (pBlobHeader->bType)
1740 case PRIVATEKEYBLOB:
1741 if ((dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY)) ||
1742 (pRSAPubKey->magic != RSAENH_MAGIC_RSA2) ||
1743 (dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) +
1744 (2 * pRSAPubKey->bitlen >> 3) + (5 * ((pRSAPubKey->bitlen+8)>>4))))
1746 SetLastError(NTE_BAD_DATA);
1747 return FALSE;
1750 *phKey = new_key(hProv, pBlobHeader->aiKeyAlg, MAKELONG(0,pRSAPubKey->bitlen), &pCryptKey);
1751 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) return FALSE;
1752 setup_key(pCryptKey);
1753 return import_private_key_impl((CONST BYTE*)(pRSAPubKey+1), &pCryptKey->context,
1754 pRSAPubKey->bitlen/8, pRSAPubKey->pubexp);
1756 case PUBLICKEYBLOB:
1757 if ((dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY)) ||
1758 (pRSAPubKey->magic != RSAENH_MAGIC_RSA1) ||
1759 (dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) + (pRSAPubKey->bitlen >> 3)))
1761 SetLastError(NTE_BAD_DATA);
1762 return FALSE;
1765 *phKey = new_key(hProv, pBlobHeader->aiKeyAlg, MAKELONG(0,pRSAPubKey->bitlen), &pCryptKey);
1766 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) return FALSE;
1767 setup_key(pCryptKey);
1768 return import_public_key_impl((CONST BYTE*)(pRSAPubKey+1), &pCryptKey->context,
1769 pRSAPubKey->bitlen >> 3, pRSAPubKey->pubexp);
1771 case SIMPLEBLOB:
1772 if (!lookup_handle(&handle_table, hPubKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pPubKey) ||
1773 pPubKey->aiAlgid != CALG_RSA_KEYX)
1775 SetLastError(NTE_BAD_PUBLIC_KEY); /* FIXME: error code? */
1776 return FALSE;
1779 if (dwDataLen < sizeof(BLOBHEADER)+sizeof(ALG_ID)+pPubKey->dwBlockLen)
1781 SetLastError(NTE_BAD_DATA); /* FIXME: error code */
1782 return FALSE;
1785 pbDecrypted = (BYTE*)HeapAlloc(GetProcessHeap(), 0, pPubKey->dwBlockLen);
1786 if (!pbDecrypted) return FALSE;
1787 encrypt_block_impl(pPubKey->aiAlgid, &pPubKey->context, pbKeyStream, pbDecrypted,
1788 RSAENH_DECRYPT);
1790 for (i=2; i<pPubKey->dwBlockLen && pbDecrypted[i]; i++);
1791 if ((i==pPubKey->dwBlockLen) ||
1792 (pbDecrypted[0] != 0x00) ||
1793 (pbDecrypted[1] != RSAENH_PKC_BLOCKTYPE))
1795 HeapFree(GetProcessHeap(), 0, pbDecrypted);
1796 SetLastError(NTE_BAD_DATA); /* FIXME: error code */
1797 return FALSE;
1800 dwKeyLen = pPubKey->dwBlockLen-i-1;
1801 *phKey = new_key(hProv, pBlobHeader->aiKeyAlg, dwKeyLen<<19, &pCryptKey);
1802 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE)
1804 HeapFree(GetProcessHeap(), 0, pbDecrypted);
1805 return FALSE;
1807 memcpy(pCryptKey->abKeyValue, pbDecrypted+i+1, dwKeyLen);
1808 HeapFree(GetProcessHeap(), 0, pbDecrypted);
1809 setup_key(pCryptKey);
1810 return TRUE;
1812 default:
1813 SetLastError(NTE_BAD_TYPE); /* FIXME: error code? */
1814 return FALSE;
1818 /******************************************************************************
1819 * CPGenKey (RSAENH.@)
1821 * Generate a key in the key container
1823 * PARAMS
1824 * hProv [I] Key container for which a key is to be generated.
1825 * Algid [I] Crypto algorithm identifier for the key to be generated.
1826 * dwFlags [I] Upper 16 bits: Binary length of key. Lower 16 bits: Flags. See Notes
1827 * phKey [O] Handle to the generated key.
1829 * RETURNS
1830 * Success: TRUE.
1831 * Failure: FALSE.
1833 * FIXME
1834 * Flags currently not considered.
1836 * NOTES
1837 * Private key-exchange- and signature-keys can be generated with Algid AT_KEYEXCHANGE
1838 * and AT_SIGNATURE values.
1840 BOOL WINAPI RSAENH_CPGenKey(HCRYPTPROV hProv, ALG_ID Algid, DWORD dwFlags, HCRYPTKEY *phKey)
1842 KEYCONTAINER *pKeyContainer;
1843 CRYPTKEY *pCryptKey;
1845 TRACE("(hProv=%08lx, aiAlgid=%d, dwFlags=%08lx, phKey=%p)\n", hProv, Algid, dwFlags, phKey);
1847 if (!lookup_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER,
1848 (OBJECTHDR**)&pKeyContainer))
1850 /* MSDN: hProv not containing valid context handle */
1851 SetLastError(NTE_BAD_UID);
1852 return FALSE;
1855 switch (Algid)
1857 case AT_SIGNATURE:
1858 case CALG_RSA_SIGN:
1859 *phKey = new_key(hProv, CALG_RSA_SIGN, dwFlags, &pCryptKey);
1860 if (pCryptKey) {
1861 new_key_impl(pCryptKey->aiAlgid, &pCryptKey->context, pCryptKey->dwKeyLen);
1862 setup_key(pCryptKey);
1863 if (Algid == AT_SIGNATURE) {
1864 RSAENH_CPDestroyKey(hProv, pKeyContainer->hSignatureKeyPair);
1865 copy_handle(&handle_table, *phKey, RSAENH_MAGIC_KEY,
1866 (unsigned int*)&pKeyContainer->hSignatureKeyPair);
1869 break;
1871 case AT_KEYEXCHANGE:
1872 case CALG_RSA_KEYX:
1873 *phKey = new_key(hProv, CALG_RSA_KEYX, dwFlags, &pCryptKey);
1874 if (pCryptKey) {
1875 new_key_impl(pCryptKey->aiAlgid, &pCryptKey->context, pCryptKey->dwKeyLen);
1876 setup_key(pCryptKey);
1877 if (Algid == AT_KEYEXCHANGE) {
1878 RSAENH_CPDestroyKey(hProv, pKeyContainer->hKeyExchangeKeyPair);
1879 copy_handle(&handle_table, *phKey, RSAENH_MAGIC_KEY,
1880 (unsigned int*)&pKeyContainer->hKeyExchangeKeyPair);
1883 break;
1885 case CALG_RC2:
1886 case CALG_RC4:
1887 case CALG_DES:
1888 case CALG_3DES_112:
1889 case CALG_3DES:
1890 *phKey = new_key(hProv, Algid, dwFlags, &pCryptKey);
1891 if (pCryptKey) {
1892 gen_rand_impl(pCryptKey->abKeyValue, RSAENH_MAX_KEY_SIZE);
1893 setup_key(pCryptKey);
1895 break;
1897 default:
1898 /* MSDN: Algorithm not supported specified by Algid */
1899 SetLastError(NTE_BAD_ALGID);
1900 return FALSE;
1903 return *phKey != (unsigned int)INVALID_HANDLE_VALUE;
1906 /******************************************************************************
1907 * CPGenRandom (RSAENH.@)
1909 * Generate a random byte stream.
1911 * PARAMS
1912 * hProv [I] Key container that is used to generate random bytes.
1913 * dwLen [I] Specifies the number of requested random data bytes.
1914 * pbBuffer [O] Random bytes will be stored here.
1916 * RETURNS
1917 * Success: TRUE
1918 * Failure: FALSE
1920 BOOL WINAPI RSAENH_CPGenRandom(HCRYPTPROV hProv, DWORD dwLen, BYTE *pbBuffer)
1922 KEYCONTAINER *pKeyContainer;
1924 TRACE("(hProv=%08lx, dwLen=%ld, pbBuffer=%p)\n", hProv, dwLen, pbBuffer);
1926 if (!lookup_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER,
1927 (OBJECTHDR**)&pKeyContainer))
1929 /* MSDN: hProv not containing valid context handle */
1930 SetLastError(NTE_BAD_UID);
1931 return FALSE;
1934 return gen_rand_impl(pbBuffer, dwLen);
1937 /******************************************************************************
1938 * CPGetHashParam (RSAENH.@)
1940 * Query parameters of an hash object.
1942 * PARAMS
1943 * hProv [I] The kea container, which the hash belongs to.
1944 * hHash [I] The hash object that is to be queried.
1945 * dwParam [I] Specifies the parameter that is to be queried.
1946 * pbData [I] Pointer to the buffer where the parameter value will be stored.
1947 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
1948 * dwFlags [I] None currently defined.
1950 * RETURNS
1951 * Success: TRUE
1952 * Failure: FALSE
1954 * NOTES
1955 * Valid dwParams are: HP_ALGID, HP_HASHSIZE, HP_HASHVALUE. The hash will be
1956 * finalized if HP_HASHVALUE is queried.
1958 BOOL WINAPI RSAENH_CPGetHashParam(HCRYPTPROV hProv, HCRYPTHASH hHash, DWORD dwParam, BYTE *pbData,
1959 DWORD *pdwDataLen, DWORD dwFlags)
1961 CRYPTHASH *pCryptHash;
1962 KEYCONTAINER *pKeyContainer;
1963 BYTE abHashValue[RSAENH_MAX_HASH_SIZE];
1965 TRACE("(hProv=%08lx, hHash=%08lx, dwParam=%08lx, pbData=%p, pdwDataLen=%p, dwFlags=%08lx)\n",
1966 hProv, hHash, dwParam, pbData, pdwDataLen, dwFlags);
1968 if (!lookup_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER,
1969 (OBJECTHDR**)&pKeyContainer))
1971 SetLastError(NTE_BAD_UID);
1972 return FALSE;
1975 if (dwFlags)
1977 SetLastError(NTE_BAD_FLAGS);
1978 return FALSE;
1981 if (!lookup_handle(&handle_table, (unsigned int)hHash, RSAENH_MAGIC_HASH,
1982 (OBJECTHDR**)&pCryptHash))
1984 SetLastError(NTE_BAD_HASH);
1985 return FALSE;
1988 if (!pdwDataLen)
1990 SetLastError(ERROR_INVALID_PARAMETER);
1991 return FALSE;
1994 switch (dwParam)
1996 case HP_ALGID:
1997 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptHash->aiAlgid,
1998 sizeof(ALG_ID));
2000 case HP_HASHSIZE:
2001 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptHash->dwHashSize,
2002 sizeof(DWORD));
2004 case HP_HASHVAL:
2005 if (pCryptHash->dwState == RSAENH_HASHSTATE_IDLE) {
2006 SetLastError(NTE_BAD_HASH_STATE);
2007 return FALSE;
2010 if (pbData && (pCryptHash->dwState != RSAENH_HASHSTATE_FINISHED))
2012 finalize_hash(pCryptHash);
2013 if (pCryptHash->aiAlgid == CALG_HMAC) {
2014 memcpy(abHashValue, pCryptHash->abHashValue, pCryptHash->dwHashSize);
2015 init_hash(pKeyContainer, pCryptHash);
2016 update_hash(pCryptHash, pCryptHash->pHMACInfo->pbOuterString,
2017 pCryptHash->pHMACInfo->cbOuterString);
2018 update_hash(pCryptHash, abHashValue, pCryptHash->dwHashSize);
2019 finalize_hash(pCryptHash);
2022 pCryptHash->dwState = RSAENH_HASHSTATE_FINISHED;
2025 return copy_param(pbData, pdwDataLen, (CONST BYTE*)pCryptHash->abHashValue,
2026 pCryptHash->dwHashSize);
2028 default:
2029 SetLastError(NTE_BAD_TYPE);
2030 return FALSE;
2034 /******************************************************************************
2035 * CPSetKeyParam (RSAENH.@)
2037 * Set a parameter of a key object
2039 * PARAMS
2040 * hProv [I] The key container to which the key belongs.
2041 * hKey [I] The key for which a parameter is to be set.
2042 * dwParam [I] Parameter type. See Notes.
2043 * pbData [I] Pointer to the parameter value.
2044 * dwFlags [I] Currently none defined.
2046 * RETURNS
2047 * Success: TRUE.
2048 * Failure: FALSE.
2050 * NOTES:
2051 * Defined dwParam types are:
2052 * - KP_MODE: Values MODE_CBC, MODE_ECB, MODE_CFB.
2053 * - KP_MODE_BITS: Shift width for cipher feedback mode. (Currently ignored by MS CSP's)
2054 * - KP_PERMISSIONS: Or'ed combination of CRYPT_ENCRYPT, CRYPT_DECRYPT,
2055 * CRYPT_EXPORT, CRYPT_READ, CRYPT_WRITE, CRYPT_MAC
2056 * - KP_IV: Initialization vector
2058 BOOL WINAPI RSAENH_CPSetKeyParam(HCRYPTPROV hProv, HCRYPTKEY hKey, DWORD dwParam, BYTE *pbData,
2059 DWORD dwFlags)
2061 CRYPTKEY *pCryptKey;
2063 TRACE("(hProv=%08lx, hKey=%08lx, dwParam=%08lx, pbData=%p, dwFlags=%08lx)\n", hProv, hKey,
2064 dwParam, pbData, dwFlags);
2066 if (!is_valid_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER))
2068 SetLastError(NTE_BAD_UID);
2069 return FALSE;
2072 if (dwFlags) {
2073 SetLastError(NTE_BAD_FLAGS);
2074 return FALSE;
2077 if (!lookup_handle(&handle_table, (unsigned int)hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
2079 SetLastError(NTE_BAD_KEY);
2080 return FALSE;
2083 switch (dwParam) {
2084 case KP_MODE:
2085 pCryptKey->dwMode = *(DWORD*)pbData;
2086 return TRUE;
2088 case KP_MODE_BITS:
2089 pCryptKey->dwModeBits = *(DWORD*)pbData;
2090 return TRUE;
2092 case KP_PERMISSIONS:
2093 pCryptKey->dwPermissions = *(DWORD*)pbData;
2094 return TRUE;
2096 case KP_IV:
2097 memcpy(pCryptKey->abInitVector, pbData, pCryptKey->dwBlockLen);
2098 return TRUE;
2100 default:
2101 SetLastError(NTE_BAD_TYPE);
2102 return FALSE;
2106 /******************************************************************************
2107 * CPGetKeyParam (RSAENH.@)
2109 * Query a key parameter.
2111 * PARAMS
2112 * hProv [I] The key container, which the key belongs to.
2113 * hHash [I] The key object that is to be queried.
2114 * dwParam [I] Specifies the parameter that is to be queried.
2115 * pbData [I] Pointer to the buffer where the parameter value will be stored.
2116 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
2117 * dwFlags [I] None currently defined.
2119 * RETURNS
2120 * Success: TRUE
2121 * Failure: FALSE
2123 * NOTES
2124 * Defined dwParam types are:
2125 * - KP_MODE: Values MODE_CBC, MODE_ECB, MODE_CFB.
2126 * - KP_MODE_BITS: Shift width for cipher feedback mode.
2127 * (Currently ignored by MS CSP's - always eight)
2128 * - KP_PERMISSIONS: Or'ed combination of CRYPT_ENCRYPT, CRYPT_DECRYPT,
2129 * CRYPT_EXPORT, CRYPT_READ, CRYPT_WRITE, CRYPT_MAC
2130 * - KP_IV: Initialization vector.
2131 * - KP_KEYLEN: Bitwidth of the key.
2132 * - KP_BLOCKLEN: Size of a block cipher block.
2133 * - KP_SALT: Salt value.
2135 BOOL WINAPI RSAENH_CPGetKeyParam(HCRYPTPROV hProv, HCRYPTKEY hKey, DWORD dwParam, BYTE *pbData,
2136 DWORD *pdwDataLen, DWORD dwFlags)
2138 CRYPTKEY *pCryptKey;
2139 DWORD dwBitLen;
2141 TRACE("(hProv=%08lx, hKey=%08lx, dwParam=%08lx, pbData=%p, pdwDataLen=%p dwFlags=%08lx)\n",
2142 hProv, hKey, dwParam, pbData, pdwDataLen, dwFlags);
2144 if (!is_valid_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER))
2146 SetLastError(NTE_BAD_UID);
2147 return FALSE;
2150 if (dwFlags) {
2151 SetLastError(NTE_BAD_FLAGS);
2152 return FALSE;
2155 if (!lookup_handle(&handle_table, (unsigned int)hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
2157 SetLastError(NTE_BAD_KEY);
2158 return FALSE;
2161 switch (dwParam)
2163 case KP_IV:
2164 return copy_param(pbData, pdwDataLen, (CONST BYTE*)pCryptKey->abInitVector,
2165 pCryptKey->dwBlockLen);
2167 case KP_SALT:
2168 return copy_param(pbData, pdwDataLen,
2169 (CONST BYTE*)&pCryptKey->abKeyValue[pCryptKey->dwKeyLen], pCryptKey->dwSaltLen);
2171 case KP_KEYLEN:
2172 dwBitLen = pCryptKey->dwKeyLen << 3;
2173 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwBitLen, sizeof(DWORD));
2175 case KP_BLOCKLEN:
2176 dwBitLen = pCryptKey->dwBlockLen << 3;
2177 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwBitLen, sizeof(DWORD));
2179 case KP_MODE:
2180 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptKey->dwMode, sizeof(DWORD));
2182 case KP_MODE_BITS:
2183 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptKey->dwModeBits,
2184 sizeof(DWORD));
2186 case KP_PERMISSIONS:
2187 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptKey->dwPermissions,
2188 sizeof(DWORD));
2190 default:
2191 SetLastError(NTE_BAD_TYPE);
2192 return FALSE;
2196 /******************************************************************************
2197 * CPGetProvParam (RSAENH.@)
2199 * Query a CSP parameter.
2201 * PARAMS
2202 * hProv [I] The key container that is to be queried.
2203 * dwParam [I] Specifies the parameter that is to be queried.
2204 * pbData [I] Pointer to the buffer where the parameter value will be stored.
2205 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
2206 * dwFlags [I] CRYPT_FIRST: Start enumeration (for PP_ENUMALGS{_EX}).
2208 * RETURNS
2209 * Success: TRUE
2210 * Failure: FALSE
2211 * NOTES:
2212 * Defined dwParam types:
2213 * - PP_CONTAINER: Name of the key container.
2214 * - PP_NAME: Name of the cryptographic service provider.
2215 * - PP_SIG_KEYSIZE_INC: RSA signature keywidth granularity in bits.
2216 * - PP_KEYX_KEYSIZE_INC: RSA key-exchange keywidth granularity in bits.
2217 * - PP_ENUMALGS{_EX}: Query provider capabilities.
2219 BOOL WINAPI RSAENH_CPGetProvParam(HCRYPTPROV hProv, DWORD dwParam, BYTE *pbData,
2220 DWORD *pdwDataLen, DWORD dwFlags)
2222 KEYCONTAINER *pKeyContainer;
2223 PROV_ENUMALGS provEnumalgs;
2224 DWORD dwTemp;
2226 /* This is for dwParam 41, which does not seem to be documented
2227 * on MSDN. IE6 SP1 asks for it in the 'About' dialog, however.
2228 * Returning this BLOB seems to satisfy IE. The marked 0x00 seem
2229 * to be 'don't care's. If you know anything more specific about
2230 * provider parameter 41, please report to wine-devel@winehq.org */
2231 static CONST BYTE abWTF[96] = {
2232 0xb0, 0x25, 0x63, 0x86, 0x9c, 0xab, 0xb6, 0x37,
2233 0xe8, 0x82, /**/0x00,/**/ 0x72, 0x06, 0xb2, /**/0x00,/**/ 0x3b,
2234 0x60, 0x35, /**/0x00,/**/ 0x3b, 0x88, 0xce, /**/0x00,/**/ 0x82,
2235 0xbc, 0x7a, /**/0x00,/**/ 0xb7, 0x4f, 0x7e, /**/0x00,/**/ 0xde,
2236 0x92, 0xf1, /**/0x00,/**/ 0x83, 0xea, 0x5e, /**/0x00,/**/ 0xc8,
2237 0x12, 0x1e, 0xd4, 0x06, 0xf7, 0x66, /**/0x00,/**/ 0x01,
2238 0x29, 0xa4, /**/0x00,/**/ 0xf8, 0x24, 0x0c, /**/0x00,/**/ 0x33,
2239 0x06, 0x80, /**/0x00,/**/ 0x02, 0x46, 0x0b, /**/0x00,/**/ 0x6d,
2240 0x5b, 0xca, /**/0x00,/**/ 0x9a, 0x10, 0xf0, /**/0x00,/**/ 0x05,
2241 0x19, 0xd0, /**/0x00,/**/ 0x2c, 0xf6, 0x27, /**/0x00,/**/ 0xaa,
2242 0x7c, 0x6f, /**/0x00,/**/ 0xb9, 0xd8, 0x72, /**/0x00,/**/ 0x03,
2243 0xf3, 0x81, /**/0x00,/**/ 0xfa, 0xe8, 0x26, /**/0x00,/**/ 0xca
2246 TRACE("(hProv=%08lx, dwParam=%08lx, pbData=%p, pdwDataLen=%p, dwFlags=%08lx)\n",
2247 hProv, dwParam, pbData, pdwDataLen, dwFlags);
2249 if (!lookup_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER,
2250 (OBJECTHDR**)&pKeyContainer))
2252 /* MSDN: hProv not containing valid context handle */
2253 SetLastError(NTE_BAD_UID);
2254 return FALSE;
2257 switch (dwParam)
2259 case PP_CONTAINER:
2260 return copy_param(pbData, pdwDataLen, (CONST BYTE*)pKeyContainer->szName,
2261 strlen(pKeyContainer->szName)+1);
2263 case PP_NAME:
2264 return copy_param(pbData, pdwDataLen, (CONST BYTE*)pKeyContainer->szProvName,
2265 strlen(pKeyContainer->szProvName)+1);
2267 case PP_SIG_KEYSIZE_INC:
2268 case PP_KEYX_KEYSIZE_INC:
2269 dwTemp = 8;
2270 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwTemp, sizeof(dwTemp));
2272 case PP_ENUMALGS:
2273 case PP_ENUMALGS_EX:
2274 if (((pKeyContainer->dwEnumAlgsCtr >= RSAENH_MAX_ENUMALGS-1) ||
2275 (!aProvEnumAlgsEx[pKeyContainer->dwPersonality]
2276 [pKeyContainer->dwEnumAlgsCtr+1].aiAlgid)) &&
2277 ((dwFlags & CRYPT_FIRST) != CRYPT_FIRST))
2279 SetLastError(ERROR_NO_MORE_ITEMS);
2280 return FALSE;
2283 if (dwParam == PP_ENUMALGS) {
2284 if (pbData && (*pdwDataLen >= sizeof(PROV_ENUMALGS)))
2285 pKeyContainer->dwEnumAlgsCtr = ((dwFlags & CRYPT_FIRST) == CRYPT_FIRST) ?
2286 0 : pKeyContainer->dwEnumAlgsCtr+1;
2288 provEnumalgs.aiAlgid = aProvEnumAlgsEx
2289 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr].aiAlgid;
2290 provEnumalgs.dwBitLen = aProvEnumAlgsEx
2291 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr].dwDefaultLen;
2292 provEnumalgs.dwNameLen = aProvEnumAlgsEx
2293 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr].dwNameLen;
2294 memcpy(provEnumalgs.szName, aProvEnumAlgsEx
2295 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr].szName,
2296 20*sizeof(CHAR));
2298 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&provEnumalgs,
2299 sizeof(PROV_ENUMALGS));
2300 } else {
2301 if (pbData && (*pdwDataLen >= sizeof(PROV_ENUMALGS_EX)))
2302 pKeyContainer->dwEnumAlgsCtr = ((dwFlags & CRYPT_FIRST) == CRYPT_FIRST) ?
2303 0 : pKeyContainer->dwEnumAlgsCtr+1;
2305 return copy_param(pbData, pdwDataLen,
2306 (CONST BYTE*)&aProvEnumAlgsEx
2307 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr],
2308 sizeof(PROV_ENUMALGS_EX));
2311 case 41: /* Undocumented. Asked for by IE About dialog */
2312 return copy_param(pbData, pdwDataLen, abWTF, sizeof(abWTF));
2314 default:
2315 /* MSDN: Unknown parameter number in dwParam */
2316 SetLastError(NTE_BAD_TYPE);
2317 return FALSE;
2319 return FALSE;
2322 /******************************************************************************
2323 * CPDeriveKey (RSAENH.@)
2325 * Derives a key from a hash value.
2327 * PARAMS
2328 * hProv [I] Key container for which a key is to be generated.
2329 * Algid [I] Crypto algorithm identifier for the key to be generated.
2330 * hBaseData [I] Hash from whose value the key will be derived.
2331 * dwFlags [I] See Notes.
2332 * phKey [O] The generated key.
2334 * RETURNS
2335 * Success: TRUE
2336 * Failure: FALSE
2338 * NOTES
2339 * Defined flags:
2340 * - CRYPT_EXPORTABLE: Key can be exported.
2341 * - CRYPT_NO_SALT: No salt is used for 40 bit keys.
2342 * - CRYPT_CREATE_SALT: Use remaining bits as salt value.
2344 BOOL WINAPI RSAENH_CPDeriveKey(HCRYPTPROV hProv, ALG_ID Algid, HCRYPTHASH hBaseData,
2345 DWORD dwFlags, HCRYPTKEY *phKey)
2347 KEYCONTAINER *pKeyContainer;
2348 CRYPTKEY *pCryptKey;
2349 CRYPTHASH *pCryptHash;
2350 BYTE abHashValue[RSAENH_MAX_HASH_SIZE*2];
2351 DWORD dwLen;
2353 TRACE("(hProv=%08lx, Algid=%d, hBaseData=%08lx, dwFlags=%08lx phKey=%p)\n", hProv, Algid,
2354 hBaseData, dwFlags, phKey);
2356 if (!lookup_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER,
2357 (OBJECTHDR**)&pKeyContainer))
2359 SetLastError(NTE_BAD_UID);
2360 return FALSE;
2363 if (!lookup_handle(&handle_table, (unsigned int)hBaseData, RSAENH_MAGIC_HASH,
2364 (OBJECTHDR**)&pCryptHash))
2366 SetLastError(NTE_BAD_HASH);
2367 return FALSE;
2370 if (!phKey)
2372 SetLastError(ERROR_INVALID_PARAMETER);
2373 return FALSE;
2376 *phKey = new_key(hProv, Algid, dwFlags, &pCryptKey);
2377 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) return FALSE;
2380 * We derive the key material from the hash.
2381 * If the hash value is not large enough for the claimed key, we have to construct
2382 * a larger binary value based on the hash. This is documented in MSDN: CryptDeriveKey.
2384 dwLen = RSAENH_MAX_HASH_SIZE;
2385 RSAENH_CPGetHashParam(pCryptHash->hProv, hBaseData, HP_HASHVAL, abHashValue, &dwLen, 0);
2387 if (dwLen < pCryptKey->dwKeyLen) {
2388 BYTE pad1[RSAENH_HMAC_DEF_PAD_LEN], pad2[RSAENH_HMAC_DEF_PAD_LEN], old_hashval[RSAENH_MAX_HASH_SIZE];
2389 DWORD i;
2391 memcpy(old_hashval, pCryptHash->abHashValue, RSAENH_MAX_HASH_SIZE);
2393 for (i=0; i<RSAENH_HMAC_DEF_PAD_LEN; i++) {
2394 pad1[i] = RSAENH_HMAC_DEF_IPAD_CHAR ^ (i<dwLen ? abHashValue[i] : 0);
2395 pad2[i] = RSAENH_HMAC_DEF_OPAD_CHAR ^ (i<dwLen ? abHashValue[i] : 0);
2398 init_hash(pKeyContainer, pCryptHash);
2399 update_hash(pCryptHash, pad1, RSAENH_HMAC_DEF_PAD_LEN);
2400 finalize_hash(pCryptHash);
2401 memcpy(abHashValue, pCryptHash->abHashValue, pCryptHash->dwHashSize);
2403 init_hash(pKeyContainer, pCryptHash);
2404 update_hash(pCryptHash, pad2, RSAENH_HMAC_DEF_PAD_LEN);
2405 finalize_hash(pCryptHash);
2406 memcpy(abHashValue+pCryptHash->dwHashSize, pCryptHash->abHashValue,
2407 pCryptHash->dwHashSize);
2409 memcpy(pCryptHash->abHashValue, old_hashval, RSAENH_MAX_HASH_SIZE);
2412 memcpy(pCryptKey->abKeyValue, abHashValue,
2413 RSAENH_MIN(pCryptKey->dwKeyLen, sizeof(pCryptKey->abKeyValue)));
2415 setup_key(pCryptKey);
2416 return TRUE;
2419 /******************************************************************************
2420 * CPGetUserKey (RSAENH.@)
2422 * Returns a handle to the user's private key-exchange- or signature-key.
2424 * PARAMS
2425 * hProv [I] The key container from which a user key is requested.
2426 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
2427 * phUserKey [O] Handle to the requested key or INVALID_HANDLE_VALUE in case of failure.
2429 * RETURNS
2430 * Success: TRUE.
2431 * Failure: FALSE.
2433 * NOTE
2434 * A newly created key container does not contain private user key. Create them with CPGenKey.
2436 BOOL WINAPI RSAENH_CPGetUserKey(HCRYPTPROV hProv, DWORD dwKeySpec, HCRYPTKEY *phUserKey)
2438 KEYCONTAINER *pKeyContainer;
2440 TRACE("(hProv=%08lx, dwKeySpec=%08lx, phUserKey=%p)\n", hProv, dwKeySpec, phUserKey);
2442 if (!lookup_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER,
2443 (OBJECTHDR**)&pKeyContainer))
2445 /* MSDN: hProv not containing valid context handle */
2446 SetLastError(NTE_BAD_UID);
2447 return FALSE;
2450 switch (dwKeySpec)
2452 case AT_KEYEXCHANGE:
2453 copy_handle(&handle_table, pKeyContainer->hKeyExchangeKeyPair, RSAENH_MAGIC_KEY,
2454 (unsigned int*)phUserKey);
2455 break;
2457 case AT_SIGNATURE:
2458 copy_handle(&handle_table, pKeyContainer->hSignatureKeyPair, RSAENH_MAGIC_KEY,
2459 (unsigned int*)phUserKey);
2460 break;
2462 default:
2463 *phUserKey = (HCRYPTKEY)INVALID_HANDLE_VALUE;
2466 if (*phUserKey == (HCRYPTKEY)INVALID_HANDLE_VALUE)
2468 /* MSDN: dwKeySpec parameter specifies non existent key */
2469 SetLastError(NTE_NO_KEY);
2470 return FALSE;
2473 return TRUE;
2476 /******************************************************************************
2477 * CPHashData (RSAENH.@)
2479 * Updates a hash object with the given data.
2481 * PARAMS
2482 * hProv [I] Key container to which the hash object belongs.
2483 * hHash [I] Hash object which is to be updated.
2484 * pbData [I] Pointer to data with which the hash object is to be updated.
2485 * dwDataLen [I] Length of the data.
2486 * dwFlags [I] Currently none defined.
2488 * RETURNS
2489 * Success: TRUE.
2490 * Failure: FALSE.
2492 * NOTES
2493 * The actual hash value is queried with CPGetHashParam, which will finalize
2494 * the hash. Updating a finalized hash will fail with a last error NTE_BAD_HASH_STATE.
2496 BOOL WINAPI RSAENH_CPHashData(HCRYPTPROV hProv, HCRYPTHASH hHash, CONST BYTE *pbData,
2497 DWORD dwDataLen, DWORD dwFlags)
2499 CRYPTHASH *pCryptHash;
2500 KEYCONTAINER *pKeyContainer;
2502 TRACE("(hProv=%08lx, hHash=%08lx, pbData=%p, dwDataLen=%ld, dwFlags=%08lx)\n",
2503 hProv, hHash, pbData, dwDataLen, dwFlags);
2505 if (!lookup_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER,
2506 (OBJECTHDR**)&pKeyContainer))
2508 SetLastError(NTE_BAD_UID);
2509 return FALSE;
2512 if (dwFlags)
2514 SetLastError(NTE_BAD_FLAGS);
2515 return FALSE;
2518 if (!lookup_handle(&handle_table, (unsigned int)hHash, RSAENH_MAGIC_HASH,
2519 (OBJECTHDR**)&pCryptHash))
2521 SetLastError(NTE_BAD_HASH);
2522 return FALSE;
2525 if (!get_algid_info(pKeyContainer, pCryptHash->aiAlgid) ||
2526 pCryptHash->aiAlgid == CALG_SSL3_SHAMD5)
2528 SetLastError(NTE_BAD_ALGID);
2529 return FALSE;
2532 if (pCryptHash->dwState == RSAENH_HASHSTATE_IDLE)
2533 pCryptHash->dwState = RSAENH_HASHSTATE_HASHING;
2535 if (pCryptHash->dwState != RSAENH_HASHSTATE_HASHING)
2537 SetLastError(NTE_BAD_HASH_STATE);
2538 return FALSE;
2541 update_hash(pCryptHash, pbData, dwDataLen);
2542 return TRUE;
2545 /******************************************************************************
2546 * CPHashSessionKey (RSAENH.@)
2548 * Updates a hash object with the binary representation of a symmetric key.
2550 * PARAMS
2551 * hProv [I] Key container to which the hash object belongs.
2552 * hHash [I] Hash object which is to be updated.
2553 * hKey [I] The symmetric key, whose binary value will be added to the hash.
2554 * dwFlags [I] CRYPT_LITTLE_ENDIAN, if the binary key value shall be interpreted as little endian.
2556 * RETURNS
2557 * Success: TRUE.
2558 * Failure: FALSE.
2560 BOOL WINAPI RSAENH_CPHashSessionKey(HCRYPTPROV hProv, HCRYPTHASH hHash, HCRYPTKEY hKey,
2561 DWORD dwFlags)
2563 BYTE abKeyValue[RSAENH_MAX_KEY_SIZE], bTemp;
2564 CRYPTKEY *pKey;
2565 DWORD i;
2567 TRACE("(hProv=%08lx, hHash=%08lx, hKey=%08lx, dwFlags=%08lx)\n", hProv, hHash, hKey, dwFlags);
2569 if (!lookup_handle(&handle_table, (unsigned int)hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pKey) ||
2570 (GET_ALG_CLASS(pKey->aiAlgid) != ALG_CLASS_DATA_ENCRYPT))
2572 SetLastError(NTE_BAD_KEY);
2573 return FALSE;
2576 if (dwFlags & ~CRYPT_LITTLE_ENDIAN) {
2577 SetLastError(NTE_BAD_FLAGS);
2578 return FALSE;
2581 memcpy(abKeyValue, pKey->abKeyValue, pKey->dwKeyLen);
2582 if (!(dwFlags & CRYPT_LITTLE_ENDIAN)) {
2583 for (i=0; i<pKey->dwKeyLen/2; i++) {
2584 bTemp = abKeyValue[i];
2585 abKeyValue[i] = abKeyValue[pKey->dwKeyLen-i-1];
2586 abKeyValue[pKey->dwKeyLen-i-1] = bTemp;
2590 return RSAENH_CPHashData(hProv, hHash, abKeyValue, pKey->dwKeyLen, 0);
2593 /******************************************************************************
2594 * CPReleaseContext (RSAENH.@)
2596 * Release a key container.
2598 * PARAMS
2599 * hProv [I] Key container to be released.
2600 * dwFlags [I] Currently none defined.
2602 * RETURNS
2603 * Success: TRUE
2604 * Failure: FALSE
2606 BOOL WINAPI RSAENH_CPReleaseContext(HCRYPTPROV hProv, DWORD dwFlags)
2608 TRACE("(hProv=%08lx, dwFlags=%08lx)\n", hProv, dwFlags);
2610 if (!release_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER))
2612 /* MSDN: hProv not containing valid context handle */
2613 SetLastError(NTE_BAD_UID);
2614 return FALSE;
2617 return TRUE;
2620 /******************************************************************************
2621 * CPSetHashParam (RSAENH.@)
2623 * Set a parameter of a hash object
2625 * PARAMS
2626 * hProv [I] The key container to which the key belongs.
2627 * hHash [I] The hash object for which a parameter is to be set.
2628 * dwParam [I] Parameter type. See Notes.
2629 * pbData [I] Pointer to the parameter value.
2630 * dwFlags [I] Currently none defined.
2632 * RETURNS
2633 * Success: TRUE.
2634 * Failure: FALSE.
2636 * NOTES
2637 * Currently only the HP_HMAC_INFO dwParam type is defined.
2638 * The HMAC_INFO struct will be deep copied into the hash object.
2639 * See Internet RFC 2104 for details on the HMAC algorithm.
2641 BOOL WINAPI RSAENH_CPSetHashParam(HCRYPTPROV hProv, HCRYPTHASH hHash, DWORD dwParam,
2642 BYTE *pbData, DWORD dwFlags)
2644 CRYPTHASH *pCryptHash;
2645 CRYPTKEY *pCryptKey;
2646 KEYCONTAINER *pKeyContainer;
2647 int i;
2649 TRACE("(hProv=%08lx, hHash=%08lx, dwParam=%08lx, pbData=%p, dwFlags=%08lx)\n",
2650 hProv, hHash, dwParam, pbData, dwFlags);
2652 if (!lookup_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER,
2653 (OBJECTHDR**)&pKeyContainer))
2655 SetLastError(NTE_BAD_UID);
2656 return FALSE;
2659 if (dwFlags) {
2660 SetLastError(NTE_BAD_FLAGS);
2661 return FALSE;
2664 if (!lookup_handle(&handle_table, (unsigned int)hHash, RSAENH_MAGIC_HASH,
2665 (OBJECTHDR**)&pCryptHash))
2667 SetLastError(NTE_BAD_HASH);
2668 return FALSE;
2671 switch (dwParam) {
2672 case HP_HMAC_INFO:
2673 free_hmac_info(pCryptHash->pHMACInfo);
2674 if (!copy_hmac_info(&pCryptHash->pHMACInfo, (PHMAC_INFO)pbData)) return FALSE;
2675 init_hash(pKeyContainer, pCryptHash);
2677 if (!lookup_handle(&handle_table, pCryptHash->hKey, RSAENH_MAGIC_KEY,
2678 (OBJECTHDR**)&pCryptKey))
2680 SetLastError(NTE_FAIL); /* FIXME: correct error code? */
2681 return FALSE;
2684 for (i=0; i<RSAENH_MIN(pCryptKey->dwKeyLen,pCryptHash->pHMACInfo->cbInnerString); i++) {
2685 pCryptHash->pHMACInfo->pbInnerString[i] ^= pCryptKey->abKeyValue[i];
2687 for (i=0; i<RSAENH_MIN(pCryptKey->dwKeyLen,pCryptHash->pHMACInfo->cbOuterString); i++) {
2688 pCryptHash->pHMACInfo->pbOuterString[i] ^= pCryptKey->abKeyValue[i];
2691 return RSAENH_CPHashData(hProv, hHash, pCryptHash->pHMACInfo->pbInnerString,
2692 pCryptHash->pHMACInfo->cbInnerString, 0);
2694 case HP_HASHVAL:
2695 memcpy(pCryptHash->abHashValue, pbData, pCryptHash->dwHashSize);
2696 pCryptHash->dwState = RSAENH_HASHSTATE_FINISHED;
2697 return TRUE;
2699 default:
2700 SetLastError(NTE_BAD_TYPE);
2701 return FALSE;
2705 /******************************************************************************
2706 * CPSetProvParam (RSAENH.@)
2708 BOOL WINAPI RSAENH_CPSetProvParam(HCRYPTPROV hProv, DWORD dwParam, BYTE *pbData, DWORD dwFlags)
2710 FIXME("(stub)\n");
2711 return FALSE;
2714 /******************************************************************************
2715 * CPSignHash (RSAENH.@)
2717 * Sign a hash object
2719 * PARAMS
2720 * hProv [I] The key container, to which the hash object belongs.
2721 * hHash [I] The hash object to be signed.
2722 * dwKeySpec [I] AT_SIGNATURE or AT_KEYEXCHANGE: Key used to generate the signature.
2723 * sDescription [I] Should be NULL for security reasons.
2724 * dwFlags [I] 0, CRYPT_NOHASHOID or CRYPT_X931_FORMAT: Format of the signature.
2725 * pbSignature [O] Buffer, to which the signature will be stored. May be NULL to query SigLen.
2726 * pdwSigLen [I/O] Size of the buffer (in), Length of the signature (out)
2728 * RETURNS
2729 * Success: TRUE
2730 * Failure: FALSE
2732 BOOL WINAPI RSAENH_CPSignHash(HCRYPTPROV hProv, HCRYPTHASH hHash, DWORD dwKeySpec,
2733 LPCWSTR sDescription, DWORD dwFlags, BYTE *pbSignature,
2734 DWORD *pdwSigLen)
2736 HCRYPTKEY hCryptKey;
2737 CRYPTKEY *pCryptKey;
2738 DWORD dwHashLen;
2739 BYTE abHashValue[RSAENH_MAX_HASH_SIZE];
2740 ALG_ID aiAlgid;
2742 TRACE("(hProv=%08lx, hHash=%08lx, dwKeySpec=%08lx, sDescription=%s, dwFlags=%08lx, "
2743 "pbSignature=%p, pdwSigLen=%p)\n", hProv, hHash, dwKeySpec, debugstr_w(sDescription),
2744 dwFlags, pbSignature, pdwSigLen);
2746 if (dwFlags & ~(CRYPT_NOHASHOID|CRYPT_X931_FORMAT)) {
2747 SetLastError(NTE_BAD_FLAGS);
2748 return FALSE;
2751 if (!RSAENH_CPGetUserKey(hProv, dwKeySpec, &hCryptKey)) return FALSE;
2753 if (!lookup_handle(&handle_table, (unsigned int)hCryptKey, RSAENH_MAGIC_KEY,
2754 (OBJECTHDR**)&pCryptKey))
2756 SetLastError(NTE_NO_KEY);
2757 return FALSE;
2760 if (!pbSignature) {
2761 *pdwSigLen = pCryptKey->dwKeyLen;
2762 return TRUE;
2764 if (pCryptKey->dwKeyLen > *pdwSigLen)
2766 SetLastError(ERROR_MORE_DATA);
2767 *pdwSigLen = pCryptKey->dwKeyLen;
2768 return FALSE;
2770 *pdwSigLen = pCryptKey->dwKeyLen;
2772 if (sDescription) {
2773 if (!RSAENH_CPHashData(hProv, hHash, (CONST BYTE*)sDescription,
2774 (DWORD)lstrlenW(sDescription)*sizeof(WCHAR), 0))
2776 return FALSE;
2780 dwHashLen = sizeof(DWORD);
2781 if (!RSAENH_CPGetHashParam(hProv, hHash, HP_ALGID, (BYTE*)&aiAlgid, &dwHashLen, 0)) return FALSE;
2783 dwHashLen = RSAENH_MAX_HASH_SIZE;
2784 if (!RSAENH_CPGetHashParam(hProv, hHash, HP_HASHVAL, abHashValue, &dwHashLen, 0)) return FALSE;
2787 if (!build_hash_signature(pbSignature, *pdwSigLen, aiAlgid, abHashValue, dwHashLen, dwFlags)) {
2788 return FALSE;
2791 return encrypt_block_impl(pCryptKey->aiAlgid, &pCryptKey->context, pbSignature, pbSignature, RSAENH_ENCRYPT);
2794 /******************************************************************************
2795 * CPVerifySignature (RSAENH.@)
2797 * Verify the signature of a hash object.
2799 * PARAMS
2800 * hProv [I] The key container, to which the hash belongs.
2801 * hHash [I] The hash for which the signature is verified.
2802 * pbSignature [I] The binary signature.
2803 * dwSigLen [I] Length of the signature BLOB.
2804 * hPubKey [I] Public key used to verify the signature.
2805 * sDescription [I] Should be NULL for security reasons.
2806 * dwFlags [I] 0, CRYPT_NOHASHOID or CRYPT_X931_FORMAT: Format of the signature.
2808 * RETURNS
2809 * Success: TRUE (Signature is valid)
2810 * Failure: FALSE (GetLastError() == NTE_BAD_SIGNATURE, if signature is invalid)
2812 BOOL WINAPI RSAENH_CPVerifySignature(HCRYPTPROV hProv, HCRYPTHASH hHash, CONST BYTE *pbSignature,
2813 DWORD dwSigLen, HCRYPTKEY hPubKey, LPCWSTR sDescription,
2814 DWORD dwFlags)
2816 BYTE *pbConstructed = NULL, *pbDecrypted = NULL;
2817 CRYPTKEY *pCryptKey;
2818 DWORD dwHashLen;
2819 ALG_ID aiAlgid;
2820 BYTE abHashValue[RSAENH_MAX_HASH_SIZE];
2821 BOOL res = FALSE;
2823 TRACE("(hProv=%08lx, hHash=%08lx, pbSignature=%p, dwSigLen=%ld, hPubKey=%08lx, sDescription=%s, "
2824 "dwFlags=%08lx)\n", hProv, hHash, pbSignature, dwSigLen, hPubKey, debugstr_w(sDescription),
2825 dwFlags);
2827 if (dwFlags & ~(CRYPT_NOHASHOID|CRYPT_X931_FORMAT)) {
2828 SetLastError(NTE_BAD_FLAGS);
2829 return FALSE;
2832 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
2834 SetLastError(NTE_BAD_UID);
2835 return FALSE;
2838 if (!lookup_handle(&handle_table, (unsigned int)hPubKey, RSAENH_MAGIC_KEY,
2839 (OBJECTHDR**)&pCryptKey))
2841 SetLastError(NTE_BAD_KEY);
2842 return FALSE;
2845 if (sDescription) {
2846 if (!RSAENH_CPHashData(hProv, hHash, (CONST BYTE*)sDescription,
2847 (DWORD)lstrlenW(sDescription)*sizeof(WCHAR), 0))
2849 return FALSE;
2853 dwHashLen = sizeof(DWORD);
2854 if (!RSAENH_CPGetHashParam(hProv, hHash, HP_ALGID, (BYTE*)&aiAlgid, &dwHashLen, 0)) return FALSE;
2856 dwHashLen = RSAENH_MAX_HASH_SIZE;
2857 if (!RSAENH_CPGetHashParam(hProv, hHash, HP_HASHVAL, abHashValue, &dwHashLen, 0)) return FALSE;
2859 pbConstructed = HeapAlloc(GetProcessHeap(), 0, dwSigLen);
2860 if (!pbConstructed) {
2861 SetLastError(NTE_NO_MEMORY);
2862 goto cleanup;
2865 pbDecrypted = HeapAlloc(GetProcessHeap(), 0, dwSigLen);
2866 if (!pbDecrypted) {
2867 SetLastError(NTE_NO_MEMORY);
2868 goto cleanup;
2871 if (!encrypt_block_impl(pCryptKey->aiAlgid, &pCryptKey->context, pbSignature, pbDecrypted,
2872 RSAENH_DECRYPT))
2874 goto cleanup;
2877 if (!build_hash_signature(pbConstructed, dwSigLen, aiAlgid, abHashValue, dwHashLen, dwFlags)) {
2878 goto cleanup;
2881 if (memcmp(pbDecrypted, pbConstructed, dwSigLen)) {
2882 SetLastError(NTE_BAD_SIGNATURE);
2883 goto cleanup;
2886 res = TRUE;
2887 cleanup:
2888 HeapFree(GetProcessHeap(), 0, pbConstructed);
2889 HeapFree(GetProcessHeap(), 0, pbDecrypted);
2890 return res;
2893 static const WCHAR szProviderKeys[3][97] = {
2894 { 'S','o','f','t','w','a','r','e','\\',
2895 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
2896 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
2897 'i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ','B','a','s',
2898 'e',' ','C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r',
2899 'o','v','i','d','e','r',' ','v','1','.','0',0 },
2900 { 'S','o','f','t','w','a','r','e','\\',
2901 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
2902 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
2903 'i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ',
2904 'E','n','h','a','n','c','e','d',
2905 ' ','C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r',
2906 'o','v','i','d','e','r',' ','v','1','.','0',0 },
2907 { 'S','o','f','t','w','a','r','e','\\',
2908 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
2909 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
2910 'i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ','S','t','r','o','n','g',
2911 ' ','C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r',
2912 'o','v','i','d','e','r',0 }
2914 static const WCHAR szDefaultKey[] = { 'S','o','f','t','w','a','r','e','\\',
2915 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
2916 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
2917 'i','d','e','r',' ','T','y','p','e','s','\\','T','y','p','e',' ','0','0','1',0};
2919 /******************************************************************************
2920 * DllRegisterServer (RSAENH.@)
2922 * Dll self registration.
2924 * PARAMS
2926 * RETURNS
2927 * Success: S_OK.
2928 * Failure: != S_OK
2930 * NOTES
2931 * Registers the following keys:
2932 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider\
2933 * Microsoft Base Cryptographic Provider v1.0
2934 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider\
2935 * Microsoft Enhanced Cryptographic Provider
2936 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider\
2937 * Microsoft Strong Cryptographpic Provider
2938 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider Types\Type 001
2940 HRESULT WINAPI RSAENH_DllRegisterServer()
2942 HKEY key;
2943 DWORD dp;
2944 long apiRet;
2945 int i;
2947 for (i=0; i<3; i++) {
2948 apiRet = RegCreateKeyExW(HKEY_LOCAL_MACHINE, szProviderKeys[i], 0, NULL,
2949 REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &key, &dp);
2951 if (apiRet == ERROR_SUCCESS)
2953 if (dp == REG_CREATED_NEW_KEY)
2955 static const WCHAR szImagePath[] = { 'I','m','a','g','e',' ','P','a','t','h',0 };
2956 static const WCHAR szRSABase[] = { 'r','s','a','e','n','h','.','d','l','l',0 };
2957 static const WCHAR szType[] = { 'T','y','p','e',0 };
2958 static const WCHAR szSignature[] = { 'S','i','g','n','a','t','u','r','e',0 };
2959 DWORD type = 1;
2960 DWORD sign = 0xdeadbeef;
2961 RegSetValueExW(key, szImagePath, 0, REG_SZ, (LPBYTE)szRSABase,
2962 (lstrlenW(szRSABase) + 1) * sizeof(WCHAR));
2963 RegSetValueExW(key, szType, 0, REG_DWORD, (LPBYTE)&type, sizeof(type));
2964 RegSetValueExW(key, szSignature, 0, REG_BINARY, (LPBYTE)&sign, sizeof(sign));
2966 RegCloseKey(key);
2969 if (apiRet == ERROR_SUCCESS)
2970 apiRet = RegCreateKeyExW(HKEY_LOCAL_MACHINE, szDefaultKey, 0, NULL, REG_OPTION_NON_VOLATILE,
2971 KEY_ALL_ACCESS, NULL, &key, &dp);
2972 if (apiRet == ERROR_SUCCESS)
2974 if (dp == REG_CREATED_NEW_KEY)
2976 static const WCHAR szName[] = { 'N','a','m','e',0 };
2977 static const WCHAR szRSAName[] = {
2978 'M','i','c','r','o','s','o','f','t',' ','S','t','r','o','n','g',' ',
2979 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ',
2980 'P','r','o','v','i','d','e','r',0 };
2981 static const WCHAR szTypeName[] = { 'T','y','p','e','N','a','m','e',0 };
2982 static const WCHAR szRSATypeName[] = {
2983 'R','S','A',' ','F','u','l','l',' ',
2984 '(','S','i','g','n','a','t','u','r','e',' ','a','n','d',' ',
2985 'K','e','y',' ','E','x','c','h','a','n','g','e',')',0 };
2987 RegSetValueExW(key, szName, 0, REG_SZ, (LPBYTE)szRSAName, sizeof(szRSAName));
2988 RegSetValueExW(key, szTypeName, 0, REG_SZ, (LPBYTE)szRSATypeName,sizeof(szRSATypeName));
2990 RegCloseKey(key);
2992 return HRESULT_FROM_WIN32(apiRet);
2995 /******************************************************************************
2996 * DllUnregisterServer (RSAENH.@)
2998 * Dll self unregistration.
3000 * PARAMS
3002 * RETURNS
3003 * Success: S_OK
3005 * NOTES
3006 * For the relevant keys see DllRegisterServer.
3008 HRESULT WINAPI RSAENH_DllUnregisterServer()
3010 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szProviderKeys[0]);
3011 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szProviderKeys[1]);
3012 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szProviderKeys[2]);
3013 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szDefaultKey);
3014 return S_OK;