Initialize extensions after correct context is created.
[wine/multimedia.git] / dlls / rsaenh / rsaenh.c
blobe378598c1000a30eb8a0bd2eeb600d3c8339531c
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, 2005 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 104
47 #define RSAENH_HASHSTATE_IDLE 0
48 #define RSAENH_HASHSTATE_HASHING 1
49 #define RSAENH_HASHSTATE_FINISHED 2
50 typedef struct _RSAENH_TLS1PRF_PARAMS
52 CRYPT_DATA_BLOB blobLabel;
53 CRYPT_DATA_BLOB blobSeed;
54 } RSAENH_TLS1PRF_PARAMS;
56 typedef struct tagCRYPTHASH
58 OBJECTHDR header;
59 ALG_ID aiAlgid;
60 HCRYPTKEY hKey;
61 HCRYPTPROV hProv;
62 DWORD dwHashSize;
63 DWORD dwState;
64 HASH_CONTEXT context;
65 BYTE abHashValue[RSAENH_MAX_HASH_SIZE];
66 PHMAC_INFO pHMACInfo;
67 RSAENH_TLS1PRF_PARAMS tpPRFParams;
68 } CRYPTHASH;
70 /******************************************************************************
71 * CRYPTKEY - key objects
73 #define RSAENH_MAGIC_KEY 0x73620457u
74 #define RSAENH_MAX_KEY_SIZE 48
75 #define RSAENH_MAX_BLOCK_SIZE 24
76 #define RSAENH_KEYSTATE_IDLE 0
77 #define RSAENH_KEYSTATE_ENCRYPTING 1
78 #define RSAENH_KEYSTATE_DECRYPTING 2
79 #define RSAENH_KEYSTATE_MASTERKEY 3
80 typedef struct _RSAENH_SCHANNEL_INFO
82 SCHANNEL_ALG saEncAlg;
83 SCHANNEL_ALG saMACAlg;
84 CRYPT_DATA_BLOB blobClientRandom;
85 CRYPT_DATA_BLOB blobServerRandom;
86 } RSAENH_SCHANNEL_INFO;
88 typedef struct tagCRYPTKEY
90 OBJECTHDR header;
91 ALG_ID aiAlgid;
92 HCRYPTPROV hProv;
93 DWORD dwMode;
94 DWORD dwModeBits;
95 DWORD dwPermissions;
96 DWORD dwKeyLen;
97 DWORD dwSaltLen;
98 DWORD dwBlockLen;
99 DWORD dwState;
100 KEY_CONTEXT context;
101 BYTE abKeyValue[RSAENH_MAX_KEY_SIZE];
102 BYTE abInitVector[RSAENH_MAX_BLOCK_SIZE];
103 BYTE abChainVector[RSAENH_MAX_BLOCK_SIZE];
104 RSAENH_SCHANNEL_INFO siSChannelInfo;
105 } CRYPTKEY;
107 /******************************************************************************
108 * KEYCONTAINER - key containers
110 #define RSAENH_PERSONALITY_BASE 0u
111 #define RSAENH_PERSONALITY_STRONG 1u
112 #define RSAENH_PERSONALITY_ENHANCED 2u
113 #define RSAENH_PERSONALITY_SCHANNEL 3u
115 #define RSAENH_MAGIC_CONTAINER 0x26384993u
116 typedef struct tagKEYCONTAINER
118 OBJECTHDR header;
119 DWORD dwFlags;
120 DWORD dwPersonality;
121 DWORD dwEnumAlgsCtr;
122 DWORD dwEnumContainersCtr;
123 CHAR szName[MAX_PATH];
124 CHAR szProvName[MAX_PATH];
125 HCRYPTKEY hKeyExchangeKeyPair;
126 HCRYPTKEY hSignatureKeyPair;
127 } KEYCONTAINER;
129 /******************************************************************************
130 * Some magic constants
132 #define RSAENH_ENCRYPT 1
133 #define RSAENH_DECRYPT 0
134 #define RSAENH_HMAC_DEF_IPAD_CHAR 0x36
135 #define RSAENH_HMAC_DEF_OPAD_CHAR 0x5c
136 #define RSAENH_HMAC_DEF_PAD_LEN 64
137 #define RSAENH_DES_EFFECTIVE_KEYLEN 56
138 #define RSAENH_DES_STORAGE_KEYLEN 64
139 #define RSAENH_3DES112_EFFECTIVE_KEYLEN 112
140 #define RSAENH_3DES112_STORAGE_KEYLEN 128
141 #define RSAENH_3DES_EFFECTIVE_KEYLEN 168
142 #define RSAENH_3DES_STORAGE_KEYLEN 192
143 #define RSAENH_MAGIC_RSA2 0x32415352
144 #define RSAENH_MAGIC_RSA1 0x31415352
145 #define RSAENH_PKC_BLOCKTYPE 0x02
146 #define RSAENH_SSL3_VERSION_MAJOR 3
147 #define RSAENH_SSL3_VERSION_MINOR 0
148 #define RSAENH_TLS1_VERSION_MAJOR 3
149 #define RSAENH_TLS1_VERSION_MINOR 1
150 #define RSAENH_REGKEY "Software\\Wine\\Crypto\\RSA\\%s"
152 #define RSAENH_MIN(a,b) ((a)<(b)?(a):(b))
153 /******************************************************************************
154 * aProvEnumAlgsEx - Defines the capabilities of the CSP personalities.
156 #define RSAENH_MAX_ENUMALGS 20
157 #define RSAENH_PCT1_SSL2_SSL3_TLS1 (CRYPT_FLAG_PCT1|CRYPT_FLAG_SSL2|CRYPT_FLAG_SSL3|CRYPT_FLAG_TLS1)
158 PROV_ENUMALGS_EX aProvEnumAlgsEx[4][RSAENH_MAX_ENUMALGS+1] =
161 {CALG_RC2, 40, 40, 56,0, 4,"RC2", 24,"RSA Data Security's RC2"},
162 {CALG_RC4, 40, 40, 56,0, 4,"RC4", 24,"RSA Data Security's RC4"},
163 {CALG_DES, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
164 {CALG_SHA, 160,160, 160,CRYPT_FLAG_SIGNING, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
165 {CALG_MD2, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD2", 23,"Message Digest 2 (MD2)"},
166 {CALG_MD4, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD4", 23,"Message Digest 4 (MD4)"},
167 {CALG_MD5, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD5", 23,"Message Digest 5 (MD5)"},
168 {CALG_SSL3_SHAMD5,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
169 {CALG_MAC, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
170 {CALG_RSA_SIGN, 512,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_SIGN",14,"RSA Signature"},
171 {CALG_RSA_KEYX, 512,384, 1024,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_KEYX",17,"RSA Key Exchange"},
172 {CALG_HMAC, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
173 {0, 0, 0, 0,0, 1,"", 1,""}
176 {CALG_RC2, 128, 40, 128,0, 4,"RC2", 24,"RSA Data Security's RC2"},
177 {CALG_RC4, 128, 40, 128,0, 4,"RC4", 24,"RSA Data Security's RC4"},
178 {CALG_DES, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
179 {CALG_3DES_112, 112,112, 112,0, 13,"3DES TWO KEY",19,"Two Key Triple DES"},
180 {CALG_3DES, 168,168, 168,0, 5,"3DES", 21,"Three Key Triple DES"},
181 {CALG_SHA, 160,160, 160,CRYPT_FLAG_SIGNING, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
182 {CALG_MD2, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD2", 23,"Message Digest 2 (MD2)"},
183 {CALG_MD4, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD4", 23,"Message Digest 4 (MD4)"},
184 {CALG_MD5, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD5", 23,"Message Digest 5 (MD5)"},
185 {CALG_SSL3_SHAMD5,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
186 {CALG_MAC, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
187 {CALG_RSA_SIGN,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_SIGN",14,"RSA Signature"},
188 {CALG_RSA_KEYX,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_KEYX",17,"RSA Key Exchange"},
189 {CALG_HMAC, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
190 {0, 0, 0, 0,0, 1,"", 1,""}
193 {CALG_RC2, 128, 40, 128,0, 4,"RC2", 24,"RSA Data Security's RC2"},
194 {CALG_RC4, 128, 40, 128,0, 4,"RC4", 24,"RSA Data Security's RC4"},
195 {CALG_DES, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
196 {CALG_3DES_112, 112,112, 112,0, 13,"3DES TWO KEY",19,"Two Key Triple DES"},
197 {CALG_3DES, 168,168, 168,0, 5,"3DES", 21,"Three Key Triple DES"},
198 {CALG_SHA, 160,160, 160,CRYPT_FLAG_SIGNING, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
199 {CALG_MD2, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD2", 23,"Message Digest 2 (MD2)"},
200 {CALG_MD4, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD4", 23,"Message Digest 4 (MD4)"},
201 {CALG_MD5, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD5", 23,"Message Digest 5 (MD5)"},
202 {CALG_SSL3_SHAMD5,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
203 {CALG_MAC, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
204 {CALG_RSA_SIGN,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_SIGN",14,"RSA Signature"},
205 {CALG_RSA_KEYX,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_KEYX",17,"RSA Key Exchange"},
206 {CALG_HMAC, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
207 {0, 0, 0, 0,0, 1,"", 1,""}
210 {CALG_RC2, 128, 40, 128,RSAENH_PCT1_SSL2_SSL3_TLS1, 4,"RC2", 24,"RSA Data Security's RC2"},
211 {CALG_RC4, 128, 40, 128,RSAENH_PCT1_SSL2_SSL3_TLS1, 4,"RC4", 24,"RSA Data Security's RC4"},
212 {CALG_DES, 56, 56, 56,RSAENH_PCT1_SSL2_SSL3_TLS1, 4,"DES", 31,"Data Encryption Standard (DES)"},
213 {CALG_3DES_112, 112,112, 112,RSAENH_PCT1_SSL2_SSL3_TLS1,13,"3DES TWO KEY",19,"Two Key Triple DES"},
214 {CALG_3DES, 168,168, 168,RSAENH_PCT1_SSL2_SSL3_TLS1, 5,"3DES", 21,"Three Key Triple DES"},
215 {CALG_SHA,160,160,160,CRYPT_FLAG_SIGNING|RSAENH_PCT1_SSL2_SSL3_TLS1,6,"SHA-1",30,"Secure Hash Algorithm (SHA-1)"},
216 {CALG_MD5,128,128,128,CRYPT_FLAG_SIGNING|RSAENH_PCT1_SSL2_SSL3_TLS1,4,"MD5",23,"Message Digest 5 (MD5)"},
217 {CALG_SSL3_SHAMD5,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
218 {CALG_MAC, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
219 {CALG_RSA_SIGN,1024,384,16384,CRYPT_FLAG_SIGNING|RSAENH_PCT1_SSL2_SSL3_TLS1,9,"RSA_SIGN",14,"RSA Signature"},
220 {CALG_RSA_KEYX,1024,384,16384,CRYPT_FLAG_SIGNING|RSAENH_PCT1_SSL2_SSL3_TLS1,9,"RSA_KEYX",17,"RSA Key Exchange"},
221 {CALG_HMAC, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
222 {CALG_PCT1_MASTER,128,128,128,CRYPT_FLAG_PCT1, 12,"PCT1 MASTER",12,"PCT1 Master"},
223 {CALG_SSL2_MASTER,40,40, 192,CRYPT_FLAG_SSL2, 12,"SSL2 MASTER",12,"SSL2 Master"},
224 {CALG_SSL3_MASTER,384,384,384,CRYPT_FLAG_SSL3, 12,"SSL3 MASTER",12,"SSL3 Master"},
225 {CALG_TLS1_MASTER,384,384,384,CRYPT_FLAG_TLS1, 12,"TLS1 MASTER",12,"TLS1 Master"},
226 {CALG_SCHANNEL_MASTER_HASH,0,0,-1,0, 16,"SCH MASTER HASH",21,"SChannel Master Hash"},
227 {CALG_SCHANNEL_MAC_KEY,0,0,-1,0, 12,"SCH MAC KEY",17,"SChannel MAC Key"},
228 {CALG_SCHANNEL_ENC_KEY,0,0,-1,0, 12,"SCH ENC KEY",24,"SChannel Encryption Key"},
229 {CALG_TLS1PRF, 0, 0, -1,0, 9,"TLS1 PRF", 28,"TLS1 Pseudo Random Function"},
230 {0, 0, 0, 0,0, 1,"", 1,""}
234 /******************************************************************************
235 * API forward declarations
237 BOOL WINAPI
238 RSAENH_CPGetKeyParam(
239 HCRYPTPROV hProv,
240 HCRYPTKEY hKey,
241 DWORD dwParam,
242 BYTE *pbData,
243 DWORD *pdwDataLen,
244 DWORD dwFlags
247 BOOL WINAPI
248 RSAENH_CPEncrypt(
249 HCRYPTPROV hProv,
250 HCRYPTKEY hKey,
251 HCRYPTHASH hHash,
252 BOOL Final,
253 DWORD dwFlags,
254 BYTE *pbData,
255 DWORD *pdwDataLen,
256 DWORD dwBufLen
259 BOOL WINAPI
260 RSAENH_CPCreateHash(
261 HCRYPTPROV hProv,
262 ALG_ID Algid,
263 HCRYPTKEY hKey,
264 DWORD dwFlags,
265 HCRYPTHASH *phHash
268 BOOL WINAPI
269 RSAENH_CPSetHashParam(
270 HCRYPTPROV hProv,
271 HCRYPTHASH hHash,
272 DWORD dwParam,
273 BYTE *pbData, DWORD dwFlags
276 BOOL WINAPI
277 RSAENH_CPGetHashParam(
278 HCRYPTPROV hProv,
279 HCRYPTHASH hHash,
280 DWORD dwParam,
281 BYTE *pbData,
282 DWORD *pdwDataLen,
283 DWORD dwFlags
286 BOOL WINAPI
287 RSAENH_CPDestroyHash(
288 HCRYPTPROV hProv,
289 HCRYPTHASH hHash
292 BOOL WINAPI
293 RSAENH_CPExportKey(
294 HCRYPTPROV hProv,
295 HCRYPTKEY hKey,
296 HCRYPTKEY hPubKey,
297 DWORD dwBlobType,
298 DWORD dwFlags,
299 BYTE *pbData,
300 DWORD *pdwDataLen
303 BOOL WINAPI
304 RSAENH_CPImportKey(
305 HCRYPTPROV hProv,
306 CONST BYTE *pbData,
307 DWORD dwDataLen,
308 HCRYPTKEY hPubKey,
309 DWORD dwFlags,
310 HCRYPTKEY *phKey
313 BOOL WINAPI
314 RSAENH_CPHashData(
315 HCRYPTPROV hProv,
316 HCRYPTHASH hHash,
317 CONST BYTE *pbData,
318 DWORD dwDataLen,
319 DWORD dwFlags
322 /******************************************************************************
323 * CSP's handle table (used by all acquired key containers)
325 static HANDLETABLE handle_table;
327 /******************************************************************************
328 * DllMain (RSAENH.@)
330 * Initializes and destroys the handle table for the CSP's handles.
332 int WINAPI DllMain(HINSTANCE hInstance, DWORD fdwReason, PVOID pvReserved)
334 switch (fdwReason)
336 case DLL_PROCESS_ATTACH:
337 init_handle_table(&handle_table);
338 break;
340 case DLL_PROCESS_DETACH:
341 destroy_handle_table(&handle_table);
342 break;
344 return 1;
347 /******************************************************************************
348 * copy_param [Internal]
350 * Helper function that supports the standard WINAPI protocol for querying data
351 * of dynamic size.
353 * PARAMS
354 * pbBuffer [O] Buffer where the queried parameter is copied to, if it is large enough.
355 * May be NUL if the required buffer size is to be queried only.
356 * pdwBufferSize [I/O] In: Size of the buffer at pbBuffer
357 * Out: Size of parameter pbParam
358 * pbParam [I] Parameter value.
359 * dwParamSize [I] Size of pbParam
361 * RETURN
362 * Success: TRUE (pbParam was copied into pbBuffer or pbBuffer is NULL)
363 * Failure: FALSE (pbBuffer is not large enough to hold pbParam). Last error: ERROR_MORE_DATA
365 static inline BOOL copy_param(
366 BYTE *pbBuffer, DWORD *pdwBufferSize, CONST BYTE *pbParam, DWORD dwParamSize)
368 if (pbBuffer)
370 if (dwParamSize > *pdwBufferSize)
372 SetLastError(ERROR_MORE_DATA);
373 *pdwBufferSize = dwParamSize;
374 return FALSE;
376 memcpy(pbBuffer, pbParam, dwParamSize);
378 *pdwBufferSize = dwParamSize;
379 return TRUE;
382 /******************************************************************************
383 * get_algid_info [Internal]
385 * Query CSP capabilities for a given crypto algorithm.
387 * PARAMS
388 * hProv [I] Handle to a key container of the CSP whose capabilities are to be queried.
389 * algid [I] Identifier of the crypto algorithm about which information is requested.
391 * RETURNS
392 * Success: Pointer to a PROV_ENUMALGS_EX struct containing information about the crypto algorithm.
393 * Failure: NULL (algid not supported)
395 static inline const PROV_ENUMALGS_EX* get_algid_info(HCRYPTPROV hProv, ALG_ID algid) {
396 PROV_ENUMALGS_EX *iterator;
397 KEYCONTAINER *pKeyContainer;
399 if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER, (OBJECTHDR**)&pKeyContainer)) {
400 SetLastError(NTE_BAD_UID);
401 return NULL;
404 for (iterator = aProvEnumAlgsEx[pKeyContainer->dwPersonality]; iterator->aiAlgid; iterator++) {
405 if (iterator->aiAlgid == algid) return iterator;
408 SetLastError(NTE_BAD_ALGID);
409 return NULL;
412 /******************************************************************************
413 * copy_data_blob [Internal]
415 * deeply copies a DATA_BLOB
417 * PARAMS
418 * dst [O] That's where the blob will be copied to
419 * src [I] Source blob
421 * RETURNS
422 * Success: TRUE
423 * Failure: FALSE (GetLastError() == NTE_NO_MEMORY
425 * NOTES
426 * Use free_data_blob to release resources occupied by copy_data_blob.
428 static inline BOOL copy_data_blob(PCRYPT_DATA_BLOB dst, CONST PCRYPT_DATA_BLOB src) {
429 dst->pbData = HeapAlloc(GetProcessHeap(), 0, src->cbData);
430 if (!dst->pbData) {
431 SetLastError(NTE_NO_MEMORY);
432 return FALSE;
434 dst->cbData = src->cbData;
435 memcpy(dst->pbData, src->pbData, src->cbData);
436 return TRUE;
439 /******************************************************************************
440 * concat_data_blobs [Internal]
442 * Concatenates two blobs
444 * PARAMS
445 * dst [O] The new blob will be copied here
446 * src1 [I] Prefix blob
447 * src2 [I] Appendix blob
449 * RETURNS
450 * Success: TRUE
451 * Failure: FALSE (GetLastError() == NTE_NO_MEMORY)
453 * NOTES
454 * Release resources occupied by concat_data_blobs with free_data_blobs
456 static inline BOOL concat_data_blobs(PCRYPT_DATA_BLOB dst, CONST PCRYPT_DATA_BLOB src1,
457 CONST PCRYPT_DATA_BLOB src2)
459 dst->cbData = src1->cbData + src2->cbData;
460 dst->pbData = HeapAlloc(GetProcessHeap(), 0, dst->cbData);
461 if (!dst->pbData) {
462 SetLastError(NTE_NO_MEMORY);
463 return FALSE;
465 memcpy(dst->pbData, src1->pbData, src1->cbData);
466 memcpy(dst->pbData + src1->cbData, src2->pbData, src2->cbData);
467 return TRUE;
470 /******************************************************************************
471 * free_data_blob [Internal]
473 * releases resource occupied by a dynamically allocated CRYPT_DATA_BLOB
475 * PARAMS
476 * pBlob [I] Heap space occupied by pBlob->pbData is released
478 static inline void free_data_blob(PCRYPT_DATA_BLOB pBlob) {
479 HeapFree(GetProcessHeap(), 0, pBlob->pbData);
482 /******************************************************************************
483 * init_data_blob [Internal]
485 static inline void init_data_blob(PCRYPT_DATA_BLOB pBlob) {
486 pBlob->pbData = NULL;
487 pBlob->cbData = 0;
490 /******************************************************************************
491 * free_hmac_info [Internal]
493 * Deeply free an HMAC_INFO struct.
495 * PARAMS
496 * hmac_info [I] Pointer to the HMAC_INFO struct to be freed.
498 * NOTES
499 * See Internet RFC 2104 for details on the HMAC algorithm.
501 static inline void free_hmac_info(PHMAC_INFO hmac_info) {
502 if (!hmac_info) return;
503 HeapFree(GetProcessHeap(), 0, hmac_info->pbInnerString);
504 HeapFree(GetProcessHeap(), 0, hmac_info->pbOuterString);
505 HeapFree(GetProcessHeap(), 0, hmac_info);
508 /******************************************************************************
509 * copy_hmac_info [Internal]
511 * Deeply copy an HMAC_INFO struct
513 * PARAMS
514 * dst [O] Pointer to a location where the pointer to the HMAC_INFO copy will be stored.
515 * src [I] Pointer to the HMAC_INFO struct to be copied.
517 * RETURNS
518 * Success: TRUE
519 * Failure: FALSE
521 * NOTES
522 * See Internet RFC 2104 for details on the HMAC algorithm.
524 static BOOL copy_hmac_info(PHMAC_INFO *dst, PHMAC_INFO src) {
525 if (!src) return FALSE;
526 *dst = HeapAlloc(GetProcessHeap(), 0, sizeof(HMAC_INFO));
527 if (!*dst) return FALSE;
528 memcpy(*dst, src, sizeof(HMAC_INFO));
529 (*dst)->pbInnerString = NULL;
530 (*dst)->pbOuterString = NULL;
531 if ((*dst)->cbInnerString == 0) (*dst)->cbInnerString = RSAENH_HMAC_DEF_PAD_LEN;
532 (*dst)->pbInnerString = HeapAlloc(GetProcessHeap(), 0, (*dst)->cbInnerString);
533 if (!(*dst)->pbInnerString) {
534 free_hmac_info(*dst);
535 return FALSE;
537 if (src->cbInnerString)
538 memcpy((*dst)->pbInnerString, src->pbInnerString, src->cbInnerString);
539 else
540 memset((*dst)->pbInnerString, RSAENH_HMAC_DEF_IPAD_CHAR, RSAENH_HMAC_DEF_PAD_LEN);
541 if ((*dst)->cbOuterString == 0) (*dst)->cbOuterString = RSAENH_HMAC_DEF_PAD_LEN;
542 (*dst)->pbOuterString = HeapAlloc(GetProcessHeap(), 0, (*dst)->cbOuterString);
543 if (!(*dst)->pbOuterString) {
544 free_hmac_info(*dst);
545 return FALSE;
547 if (src->cbOuterString)
548 memcpy((*dst)->pbOuterString, src->pbOuterString, src->cbOuterString);
549 else
550 memset((*dst)->pbOuterString, RSAENH_HMAC_DEF_OPAD_CHAR, RSAENH_HMAC_DEF_PAD_LEN);
551 return TRUE;
554 /******************************************************************************
555 * destroy_hash [Internal]
557 * Destructor for hash objects
559 * PARAMS
560 * pCryptHash [I] Pointer to the hash object to be destroyed.
561 * Will be invalid after function returns!
563 static void destroy_hash(OBJECTHDR *pObject)
565 CRYPTHASH *pCryptHash = (CRYPTHASH*)pObject;
567 free_hmac_info(pCryptHash->pHMACInfo);
568 free_data_blob(&pCryptHash->tpPRFParams.blobLabel);
569 free_data_blob(&pCryptHash->tpPRFParams.blobSeed);
570 HeapFree(GetProcessHeap(), 0, pCryptHash);
573 /******************************************************************************
574 * init_hash [Internal]
576 * Initialize (or reset) a hash object
578 * PARAMS
579 * pCryptHash [I] The hash object to be initialized.
581 static inline BOOL init_hash(CRYPTHASH *pCryptHash) {
582 DWORD dwLen;
584 switch (pCryptHash->aiAlgid)
586 case CALG_HMAC:
587 if (pCryptHash->pHMACInfo) {
588 const PROV_ENUMALGS_EX *pAlgInfo;
590 pAlgInfo = get_algid_info(pCryptHash->hProv, pCryptHash->pHMACInfo->HashAlgid);
591 if (!pAlgInfo) return FALSE;
592 pCryptHash->dwHashSize = pAlgInfo->dwDefaultLen >> 3;
593 init_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context);
594 update_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
595 pCryptHash->pHMACInfo->pbInnerString,
596 pCryptHash->pHMACInfo->cbInnerString);
598 return TRUE;
600 case CALG_MAC:
601 dwLen = sizeof(DWORD);
602 RSAENH_CPGetKeyParam(pCryptHash->hProv, pCryptHash->hKey, KP_BLOCKLEN,
603 (BYTE*)&pCryptHash->dwHashSize, &dwLen, 0);
604 pCryptHash->dwHashSize >>= 3;
605 return TRUE;
607 default:
608 return init_hash_impl(pCryptHash->aiAlgid, &pCryptHash->context);
612 /******************************************************************************
613 * update_hash [Internal]
615 * Hashes the given data and updates the hash object's state accordingly
617 * PARAMS
618 * pCryptHash [I] Hash object to be updated.
619 * pbData [I] Pointer to data stream to be hashed.
620 * dwDataLen [I] Length of data stream.
622 static inline void update_hash(CRYPTHASH *pCryptHash, CONST BYTE *pbData, DWORD dwDataLen) {
623 BYTE *pbTemp;
625 switch (pCryptHash->aiAlgid)
627 case CALG_HMAC:
628 if (pCryptHash->pHMACInfo)
629 update_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
630 pbData, dwDataLen);
631 break;
633 case CALG_MAC:
634 pbTemp = HeapAlloc(GetProcessHeap(), 0, dwDataLen);
635 if (!pbTemp) return;
636 memcpy(pbTemp, pbData, dwDataLen);
637 RSAENH_CPEncrypt(pCryptHash->hProv, pCryptHash->hKey, (HCRYPTHASH)NULL, FALSE, 0,
638 pbTemp, &dwDataLen, dwDataLen);
639 HeapFree(GetProcessHeap(), 0, pbTemp);
640 break;
642 default:
643 update_hash_impl(pCryptHash->aiAlgid, &pCryptHash->context, pbData, dwDataLen);
647 /******************************************************************************
648 * finalize_hash [Internal]
650 * Finalizes the hash, after all data has been hashed with update_hash.
651 * No additional data can be hashed afterwards until the hash gets initialized again.
653 * PARAMS
654 * pCryptHash [I] Hash object to be finalized.
656 static inline void finalize_hash(CRYPTHASH *pCryptHash) {
657 DWORD dwDataLen;
659 switch (pCryptHash->aiAlgid)
661 case CALG_HMAC:
662 if (pCryptHash->pHMACInfo) {
663 BYTE abHashValue[RSAENH_MAX_HASH_SIZE];
665 finalize_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
666 pCryptHash->abHashValue);
667 memcpy(abHashValue, pCryptHash->abHashValue, pCryptHash->dwHashSize);
668 init_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context);
669 update_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
670 pCryptHash->pHMACInfo->pbOuterString,
671 pCryptHash->pHMACInfo->cbOuterString);
672 update_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
673 abHashValue, pCryptHash->dwHashSize);
674 finalize_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
675 pCryptHash->abHashValue);
677 break;
679 case CALG_MAC:
680 dwDataLen = 0;
681 RSAENH_CPEncrypt(pCryptHash->hProv, pCryptHash->hKey, (HCRYPTHASH)NULL, TRUE, 0,
682 pCryptHash->abHashValue, &dwDataLen, pCryptHash->dwHashSize);
683 break;
685 default:
686 finalize_hash_impl(pCryptHash->aiAlgid, &pCryptHash->context, pCryptHash->abHashValue);
690 /******************************************************************************
691 * destroy_key [Internal]
693 * Destructor for key objects
695 * PARAMS
696 * pCryptKey [I] Pointer to the key object to be destroyed.
697 * Will be invalid after function returns!
699 static void destroy_key(OBJECTHDR *pObject)
701 CRYPTKEY *pCryptKey = (CRYPTKEY*)pObject;
703 free_key_impl(pCryptKey->aiAlgid, &pCryptKey->context);
704 free_data_blob(&pCryptKey->siSChannelInfo.blobClientRandom);
705 free_data_blob(&pCryptKey->siSChannelInfo.blobServerRandom);
706 HeapFree(GetProcessHeap(), 0, pCryptKey);
709 /******************************************************************************
710 * setup_key [Internal]
712 * Initialize (or reset) a key object
714 * PARAMS
715 * pCryptKey [I] The key object to be initialized.
717 static inline void setup_key(CRYPTKEY *pCryptKey) {
718 pCryptKey->dwState = RSAENH_KEYSTATE_IDLE;
719 memcpy(pCryptKey->abChainVector, pCryptKey->abInitVector, sizeof(pCryptKey->abChainVector));
720 setup_key_impl(pCryptKey->aiAlgid, &pCryptKey->context, pCryptKey->dwKeyLen,
721 pCryptKey->dwSaltLen, pCryptKey->abKeyValue);
724 /******************************************************************************
725 * new_key [Internal]
727 * Creates a new key object without assigning the actual binary key value.
728 * This is done by CPDeriveKey, CPGenKey or CPImportKey, which call this function.
730 * PARAMS
731 * hProv [I] Handle to the provider to which the created key will belong.
732 * aiAlgid [I] The new key shall use the crypto algorithm idenfied by aiAlgid.
733 * dwFlags [I] Upper 16 bits give the key length.
734 * Lower 16 bits: CRYPT_CREATE_SALT, CRYPT_NO_SALT
735 * ppCryptKey [O] Pointer to the created key
737 * RETURNS
738 * Success: Handle to the created key.
739 * Failure: INVALID_HANDLE_VALUE
741 static HCRYPTKEY new_key(HCRYPTPROV hProv, ALG_ID aiAlgid, DWORD dwFlags, CRYPTKEY **ppCryptKey)
743 HCRYPTKEY hCryptKey;
744 CRYPTKEY *pCryptKey;
745 DWORD dwKeyLen = HIWORD(dwFlags);
746 const PROV_ENUMALGS_EX *peaAlgidInfo;
748 *ppCryptKey = NULL;
751 * Retrieve the CSP's capabilities for the given ALG_ID value
753 peaAlgidInfo = get_algid_info(hProv, aiAlgid);
754 if (!peaAlgidInfo) return (HCRYPTKEY)INVALID_HANDLE_VALUE;
757 * Assume the default key length, if none is specified explicitly
759 if (dwKeyLen == 0) dwKeyLen = peaAlgidInfo->dwDefaultLen;
762 * Check if the requested key length is supported by the current CSP.
763 * Adjust key length's for DES algorithms.
765 switch (aiAlgid) {
766 case CALG_DES:
767 if (dwKeyLen == RSAENH_DES_EFFECTIVE_KEYLEN) {
768 dwKeyLen = RSAENH_DES_STORAGE_KEYLEN;
770 if (dwKeyLen != RSAENH_DES_STORAGE_KEYLEN) {
771 SetLastError(NTE_BAD_FLAGS);
772 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
774 break;
776 case CALG_3DES_112:
777 if (dwKeyLen == RSAENH_3DES112_EFFECTIVE_KEYLEN) {
778 dwKeyLen = RSAENH_3DES112_STORAGE_KEYLEN;
780 if (dwKeyLen != RSAENH_3DES112_STORAGE_KEYLEN) {
781 SetLastError(NTE_BAD_FLAGS);
782 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
784 break;
786 case CALG_3DES:
787 if (dwKeyLen == RSAENH_3DES_EFFECTIVE_KEYLEN) {
788 dwKeyLen = RSAENH_3DES_STORAGE_KEYLEN;
790 if (dwKeyLen != RSAENH_3DES_STORAGE_KEYLEN) {
791 SetLastError(NTE_BAD_FLAGS);
792 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
794 break;
796 default:
797 if (dwKeyLen % 8 ||
798 dwKeyLen > peaAlgidInfo->dwMaxLen ||
799 dwKeyLen < peaAlgidInfo->dwMinLen)
801 SetLastError(NTE_BAD_FLAGS);
802 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
806 hCryptKey = (HCRYPTKEY)new_object(&handle_table, sizeof(CRYPTKEY), RSAENH_MAGIC_KEY,
807 destroy_key, (OBJECTHDR**)&pCryptKey);
808 if (hCryptKey != (HCRYPTKEY)INVALID_HANDLE_VALUE)
810 pCryptKey->aiAlgid = aiAlgid;
811 pCryptKey->hProv = hProv;
812 pCryptKey->dwModeBits = 0;
813 pCryptKey->dwPermissions = CRYPT_ENCRYPT | CRYPT_DECRYPT | CRYPT_READ | CRYPT_WRITE |
814 CRYPT_MAC;
815 pCryptKey->dwKeyLen = dwKeyLen >> 3;
816 if ((dwFlags & CRYPT_CREATE_SALT) || (dwKeyLen == 40 && !(dwFlags & CRYPT_NO_SALT)))
817 pCryptKey->dwSaltLen = 16 /*FIXME*/ - pCryptKey->dwKeyLen;
818 else
819 pCryptKey->dwSaltLen = 0;
820 memset(pCryptKey->abKeyValue, 0, sizeof(pCryptKey->abKeyValue));
821 memset(pCryptKey->abInitVector, 0, sizeof(pCryptKey->abInitVector));
822 init_data_blob(&pCryptKey->siSChannelInfo.blobClientRandom);
823 init_data_blob(&pCryptKey->siSChannelInfo.blobServerRandom);
825 switch(aiAlgid)
827 case CALG_PCT1_MASTER:
828 case CALG_SSL2_MASTER:
829 case CALG_SSL3_MASTER:
830 case CALG_TLS1_MASTER:
831 case CALG_RC4:
832 pCryptKey->dwBlockLen = 0;
833 pCryptKey->dwMode = 0;
834 break;
836 case CALG_RC2:
837 case CALG_DES:
838 case CALG_3DES_112:
839 case CALG_3DES:
840 pCryptKey->dwBlockLen = 8;
841 pCryptKey->dwMode = CRYPT_MODE_CBC;
842 break;
844 case CALG_RSA_KEYX:
845 case CALG_RSA_SIGN:
846 pCryptKey->dwBlockLen = dwKeyLen >> 3;
847 pCryptKey->dwMode = 0;
848 break;
851 *ppCryptKey = pCryptKey;
854 return hCryptKey;
857 /******************************************************************************
858 * destroy_key_container [Internal]
860 * Destructor for key containers.
862 * PARAMS
863 * pObjectHdr [I] Pointer to the key container to be destroyed.
865 static void destroy_key_container(OBJECTHDR *pObjectHdr)
867 KEYCONTAINER *pKeyContainer = (KEYCONTAINER*)pObjectHdr;
868 DATA_BLOB blobIn, blobOut;
869 CRYPTKEY *pKey;
870 CHAR szRSABase[MAX_PATH];
871 HKEY hKey, hRootKey;
872 DWORD dwLen;
873 BYTE *pbKey;
875 if (!(pKeyContainer->dwFlags & CRYPT_VERIFYCONTEXT)) {
876 /* On WinXP, persistent keys are stored in a file located at:
877 * $AppData$\\Microsoft\\Crypto\\RSA\\$SID$\\some_hex_string
879 sprintf(szRSABase, RSAENH_REGKEY, pKeyContainer->szName);
881 if (pKeyContainer->dwFlags & CRYPT_MACHINE_KEYSET) {
882 hRootKey = HKEY_LOCAL_MACHINE;
883 } else {
884 hRootKey = HKEY_CURRENT_USER;
887 /* @@ Wine registry key: HKLM\Software\Wine\Crypto\RSA */
888 /* @@ Wine registry key: HKCU\Software\Wine\Crypto\RSA */
889 if (RegCreateKeyExA(hRootKey, szRSABase, 0, NULL, REG_OPTION_NON_VOLATILE,
890 KEY_WRITE, NULL, &hKey, NULL) == ERROR_SUCCESS)
892 if (lookup_handle(&handle_table, pKeyContainer->hKeyExchangeKeyPair, RSAENH_MAGIC_KEY,
893 (OBJECTHDR**)&pKey))
895 if (RSAENH_CPExportKey(pKey->hProv, pKeyContainer->hKeyExchangeKeyPair, 0,
896 PRIVATEKEYBLOB, 0, 0, &dwLen))
898 pbKey = HeapAlloc(GetProcessHeap(), 0, dwLen);
899 if (pbKey)
901 if (RSAENH_CPExportKey(pKey->hProv, pKeyContainer->hKeyExchangeKeyPair, 0,
902 PRIVATEKEYBLOB, 0, pbKey, &dwLen))
904 blobIn.pbData = pbKey;
905 blobIn.cbData = dwLen;
907 if (CryptProtectData(&blobIn, NULL, NULL, NULL, NULL,
908 (pKeyContainer->dwFlags & CRYPT_MACHINE_KEYSET) ?
909 CRYPTPROTECT_LOCAL_MACHINE : 0,
910 &blobOut))
912 RegSetValueExA(hKey, "KeyExchangeKeyPair", 0, REG_BINARY,
913 blobOut.pbData, blobOut.cbData);
914 HeapFree(GetProcessHeap(), 0, blobOut.pbData);
917 HeapFree(GetProcessHeap(), 0, pbKey);
920 release_handle(&handle_table, (unsigned int)pKeyContainer->hKeyExchangeKeyPair,
921 RSAENH_MAGIC_KEY);
924 if (lookup_handle(&handle_table, pKeyContainer->hSignatureKeyPair, RSAENH_MAGIC_KEY,
925 (OBJECTHDR**)&pKey))
927 if (RSAENH_CPExportKey(pKey->hProv, pKeyContainer->hSignatureKeyPair, 0,
928 PRIVATEKEYBLOB, 0, 0, &dwLen))
930 pbKey = HeapAlloc(GetProcessHeap(), 0, dwLen);
931 if (pbKey)
933 if (RSAENH_CPExportKey(pKey->hProv, pKeyContainer->hSignatureKeyPair, 0,
934 PRIVATEKEYBLOB, 0, pbKey, &dwLen))
936 blobIn.pbData = pbKey;
937 blobIn.cbData = dwLen;
939 if (CryptProtectData(&blobIn, NULL, NULL, NULL, NULL,
940 (pKeyContainer->dwFlags & CRYPT_MACHINE_KEYSET) ?
941 CRYPTPROTECT_LOCAL_MACHINE : 0,
942 &blobOut))
944 RegSetValueExA(hKey, "SignatureKeyPair", 0, REG_BINARY,
945 blobOut.pbData, blobOut.cbData);
946 HeapFree(GetProcessHeap(), 0, blobOut.pbData);
949 HeapFree(GetProcessHeap(), 0, pbKey);
952 release_handle(&handle_table, (unsigned int)pKeyContainer->hSignatureKeyPair,
953 RSAENH_MAGIC_KEY);
956 RegCloseKey(hKey);
960 HeapFree( GetProcessHeap(), 0, pKeyContainer );
963 /******************************************************************************
964 * new_key_container [Internal]
966 * Create a new key container. The personality (RSA Base, Strong or Enhanced CP)
967 * of the CSP is determined via the pVTable->pszProvName string.
969 * PARAMS
970 * pszContainerName [I] Name of the key container.
971 * pVTable [I] Callback functions and context info provided by the OS
973 * RETURNS
974 * Success: Handle to the new key container.
975 * Failure: INVALID_HANDLE_VALUE
977 static HCRYPTPROV new_key_container(PCCH pszContainerName, DWORD dwFlags, PVTableProvStruc pVTable)
979 KEYCONTAINER *pKeyContainer;
980 HCRYPTPROV hKeyContainer;
982 hKeyContainer = (HCRYPTPROV)new_object(&handle_table, sizeof(KEYCONTAINER), RSAENH_MAGIC_CONTAINER,
983 destroy_key_container, (OBJECTHDR**)&pKeyContainer);
984 if (hKeyContainer != (HCRYPTPROV)INVALID_HANDLE_VALUE)
986 lstrcpynA(pKeyContainer->szName, pszContainerName, MAX_PATH);
987 pKeyContainer->dwFlags = dwFlags;
988 pKeyContainer->dwEnumAlgsCtr = 0;
989 pKeyContainer->hKeyExchangeKeyPair = (HCRYPTKEY)INVALID_HANDLE_VALUE;
990 pKeyContainer->hSignatureKeyPair = (HCRYPTKEY)INVALID_HANDLE_VALUE;
991 if (pVTable && pVTable->pszProvName) {
992 lstrcpynA(pKeyContainer->szProvName, pVTable->pszProvName, MAX_PATH);
993 if (!strcmp(pVTable->pszProvName, MS_DEF_PROV_A)) {
994 pKeyContainer->dwPersonality = RSAENH_PERSONALITY_BASE;
995 } else if (!strcmp(pVTable->pszProvName, MS_ENHANCED_PROV_A)) {
996 pKeyContainer->dwPersonality = RSAENH_PERSONALITY_ENHANCED;
997 } else if (!strcmp(pVTable->pszProvName, MS_DEF_RSA_SCHANNEL_PROV_A)) {
998 pKeyContainer->dwPersonality = RSAENH_PERSONALITY_SCHANNEL;
999 } else {
1000 pKeyContainer->dwPersonality = RSAENH_PERSONALITY_STRONG;
1004 /* The new key container has to be inserted into the CSP immediately
1005 * after creation to be available for CPGetProvParam's PP_ENUMCONTAINERS. */
1006 if (!(dwFlags & CRYPT_VERIFYCONTEXT)) {
1007 BYTE szRSABase[MAX_PATH];
1008 HKEY hRootKey, hKey;
1010 sprintf(szRSABase, RSAENH_REGKEY, pKeyContainer->szName);
1012 if (pKeyContainer->dwFlags & CRYPT_MACHINE_KEYSET) {
1013 hRootKey = HKEY_LOCAL_MACHINE;
1014 } else {
1015 hRootKey = HKEY_CURRENT_USER;
1018 /* @@ Wine registry key: HKLM\Software\Wine\Crypto\RSA */
1019 /* @@ Wine registry key: HKCU\Software\Wine\Crypto\RSA */
1020 RegCreateKeyA(hRootKey, szRSABase, &hKey);
1021 RegCloseKey(hKey);
1025 return hKeyContainer;
1028 /******************************************************************************
1029 * read_key_container [Internal]
1031 * Tries to read the persistent state of the key container (mainly the signature
1032 * and key exchange private keys) given by pszContainerName.
1034 * PARAMS
1035 * pszContainerName [I] Name of the key container to read from the registry
1036 * pVTable [I] Pointer to context data provided by the operating system
1038 * RETURNS
1039 * Success: Handle to the key container read from the registry
1040 * Failure: INVALID_HANDLE_VALUE
1042 static HCRYPTPROV read_key_container(PCHAR pszContainerName, DWORD dwFlags, PVTableProvStruc pVTable)
1044 CHAR szRSABase[MAX_PATH];
1045 BYTE *pbKey;
1046 HKEY hKey, hRootKey;
1047 DWORD dwValueType, dwLen;
1048 KEYCONTAINER *pKeyContainer;
1049 HCRYPTPROV hKeyContainer;
1050 DATA_BLOB blobIn, blobOut;
1052 sprintf(szRSABase, RSAENH_REGKEY, pszContainerName);
1054 if (dwFlags & CRYPT_MACHINE_KEYSET) {
1055 hRootKey = HKEY_LOCAL_MACHINE;
1056 } else {
1057 hRootKey = HKEY_CURRENT_USER;
1060 /* @@ Wine registry key: HKLM\Software\Wine\Crypto\RSA */
1061 /* @@ Wine registry key: HKCU\Software\Wine\Crypto\RSA */
1062 if (RegOpenKeyExA(hRootKey, szRSABase, 0, KEY_READ, &hKey) != ERROR_SUCCESS)
1064 SetLastError(NTE_BAD_KEYSET);
1065 return (HCRYPTPROV)INVALID_HANDLE_VALUE;
1068 hKeyContainer = new_key_container(pszContainerName, dwFlags, pVTable);
1069 if (hKeyContainer != (HCRYPTPROV)INVALID_HANDLE_VALUE)
1071 if (!lookup_handle(&handle_table, hKeyContainer, RSAENH_MAGIC_CONTAINER,
1072 (OBJECTHDR**)&pKeyContainer))
1073 return (HCRYPTPROV)INVALID_HANDLE_VALUE;
1075 if (RegQueryValueExA(hKey, "KeyExchangeKeyPair", 0, &dwValueType, NULL, &dwLen) ==
1076 ERROR_SUCCESS)
1078 pbKey = HeapAlloc(GetProcessHeap(), 0, dwLen);
1079 if (pbKey)
1081 if (RegQueryValueExA(hKey, "KeyExchangeKeyPair", 0, &dwValueType, pbKey, &dwLen) ==
1082 ERROR_SUCCESS)
1084 blobIn.pbData = pbKey;
1085 blobIn.cbData = dwLen;
1087 if (CryptUnprotectData(&blobIn, NULL, NULL, NULL, NULL,
1088 (dwFlags & CRYPT_MACHINE_KEYSET) ? CRYPTPROTECT_LOCAL_MACHINE : 0, &blobOut))
1090 RSAENH_CPImportKey(hKeyContainer, blobOut.pbData, blobOut.cbData, 0, 0,
1091 &pKeyContainer->hKeyExchangeKeyPair);
1092 HeapFree(GetProcessHeap(), 0, blobOut.pbData);
1095 HeapFree(GetProcessHeap(), 0, pbKey);
1099 if (RegQueryValueExA(hKey, "SignatureKeyPair", 0, &dwValueType, NULL, &dwLen) ==
1100 ERROR_SUCCESS)
1102 pbKey = HeapAlloc(GetProcessHeap(), 0, dwLen);
1103 if (pbKey)
1105 if (RegQueryValueExA(hKey, "SignatureKeyPair", 0, &dwValueType, pbKey, &dwLen) ==
1106 ERROR_SUCCESS)
1108 blobIn.pbData = pbKey;
1109 blobIn.cbData = dwLen;
1111 if (CryptUnprotectData(&blobIn, NULL, NULL, NULL, NULL,
1112 (dwFlags & CRYPT_MACHINE_KEYSET) ? CRYPTPROTECT_LOCAL_MACHINE : 0, &blobOut))
1114 RSAENH_CPImportKey(hKeyContainer, blobOut.pbData, blobOut.cbData, 0, 0,
1115 &pKeyContainer->hSignatureKeyPair);
1116 HeapFree(GetProcessHeap(), 0, blobOut.pbData);
1119 HeapFree(GetProcessHeap(), 0, pbKey);
1124 return hKeyContainer;
1127 /******************************************************************************
1128 * build_hash_signature [Internal]
1130 * Builds a padded version of a hash to match the length of the RSA key modulus.
1132 * PARAMS
1133 * pbSignature [O] The padded hash object is stored here.
1134 * dwLen [I] Length of the pbSignature buffer.
1135 * aiAlgid [I] Algorithm identifier of the hash to be padded.
1136 * abHashValue [I] The value of the hash object.
1137 * dwHashLen [I] Length of the hash value.
1138 * dwFlags [I] Selection of padding algorithm.
1140 * RETURNS
1141 * Success: TRUE
1142 * Failure: FALSE (NTE_BAD_ALGID)
1144 static BOOL build_hash_signature(BYTE *pbSignature, DWORD dwLen, ALG_ID aiAlgid,
1145 CONST BYTE *abHashValue, DWORD dwHashLen, DWORD dwFlags)
1147 /* These prefixes are meant to be concatenated with hash values of the
1148 * respective kind to form a PKCS #7 DigestInfo. */
1149 static const struct tagOIDDescriptor {
1150 ALG_ID aiAlgid;
1151 DWORD dwLen;
1152 CONST BYTE abOID[18];
1153 } aOIDDescriptor[5] = {
1154 { CALG_MD2, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1155 0x86, 0xf7, 0x0d, 0x02, 0x02, 0x05, 0x00, 0x04, 0x10 } },
1156 { CALG_MD4, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1157 0x86, 0xf7, 0x0d, 0x02, 0x04, 0x05, 0x00, 0x04, 0x10 } },
1158 { CALG_MD5, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1159 0x86, 0xf7, 0x0d, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10 } },
1160 { CALG_SHA, 15, { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03,
1161 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14 } },
1162 { 0, 0, {} }
1164 DWORD dwIdxOID, i, j;
1166 for (dwIdxOID = 0; aOIDDescriptor[dwIdxOID].aiAlgid; dwIdxOID++) {
1167 if (aOIDDescriptor[dwIdxOID].aiAlgid == aiAlgid) break;
1170 if (!aOIDDescriptor[dwIdxOID].aiAlgid) {
1171 SetLastError(NTE_BAD_ALGID);
1172 return FALSE;
1175 /* Build the padded signature */
1176 if (dwFlags & CRYPT_X931_FORMAT) {
1177 pbSignature[0] = 0x6b;
1178 for (i=1; i < dwLen - dwHashLen - 3; i++) {
1179 pbSignature[i] = 0xbb;
1181 pbSignature[i++] = 0xba;
1182 for (j=0; j < dwHashLen; j++, i++) {
1183 pbSignature[i] = abHashValue[j];
1185 pbSignature[i++] = 0x33;
1186 pbSignature[i++] = 0xcc;
1187 } else {
1188 pbSignature[0] = 0x00;
1189 pbSignature[1] = 0x01;
1190 if (dwFlags & CRYPT_NOHASHOID) {
1191 for (i=2; i < dwLen - 1 - dwHashLen; i++) {
1192 pbSignature[i] = 0xff;
1194 pbSignature[i++] = 0x00;
1195 } else {
1196 for (i=2; i < dwLen - 1 - aOIDDescriptor[dwIdxOID].dwLen - dwHashLen; i++) {
1197 pbSignature[i] = 0xff;
1199 pbSignature[i++] = 0x00;
1200 for (j=0; j < aOIDDescriptor[dwIdxOID].dwLen; j++) {
1201 pbSignature[i++] = aOIDDescriptor[dwIdxOID].abOID[j];
1204 for (j=0; j < dwHashLen; j++) {
1205 pbSignature[i++] = abHashValue[j];
1209 return TRUE;
1212 /******************************************************************************
1213 * tls1_p [Internal]
1215 * This is an implementation of the 'P_hash' helper function for TLS1's PRF.
1216 * It is used exclusively by tls1_prf. For details see RFC 2246, chapter 5.
1217 * The pseudo random stream generated by this function is exclusive or'ed with
1218 * the data in pbBuffer.
1220 * PARAMS
1221 * hHMAC [I] HMAC object, which will be used in pseudo random generation
1222 * pblobSeed [I] Seed value
1223 * pbBuffer [I/O] Pseudo random stream will be xor'ed to the provided data
1224 * dwBufferLen [I] Number of pseudo random bytes desired
1226 * RETURNS
1227 * Success: TRUE
1228 * Failure: FALSE
1230 static BOOL tls1_p(HCRYPTHASH hHMAC, CONST PCRYPT_DATA_BLOB pblobSeed, PBYTE pbBuffer, DWORD dwBufferLen)
1232 CRYPTHASH *pHMAC;
1233 BYTE abAi[RSAENH_MAX_HASH_SIZE];
1234 DWORD i = 0;
1236 if (!lookup_handle(&handle_table, hHMAC, RSAENH_MAGIC_HASH, (OBJECTHDR**)&pHMAC)) {
1237 SetLastError(NTE_BAD_HASH);
1238 return FALSE;
1241 /* compute A_1 = HMAC(seed) */
1242 init_hash(pHMAC);
1243 update_hash(pHMAC, pblobSeed->pbData, pblobSeed->cbData);
1244 finalize_hash(pHMAC);
1245 memcpy(abAi, pHMAC->abHashValue, pHMAC->dwHashSize);
1247 do {
1248 /* compute HMAC(A_i + seed) */
1249 init_hash(pHMAC);
1250 update_hash(pHMAC, abAi, pHMAC->dwHashSize);
1251 update_hash(pHMAC, pblobSeed->pbData, pblobSeed->cbData);
1252 finalize_hash(pHMAC);
1254 /* pseudo random stream := CONCAT_{i=1..n} ( HMAC(A_i + seed) ) */
1255 do {
1256 if (i >= dwBufferLen) break;
1257 pbBuffer[i] ^= pHMAC->abHashValue[i % pHMAC->dwHashSize];
1258 i++;
1259 } while (i % pHMAC->dwHashSize);
1261 /* compute A_{i+1} = HMAC(A_i) */
1262 init_hash(pHMAC);
1263 update_hash(pHMAC, abAi, pHMAC->dwHashSize);
1264 finalize_hash(pHMAC);
1265 memcpy(abAi, pHMAC->abHashValue, pHMAC->dwHashSize);
1266 } while (i < dwBufferLen);
1268 return TRUE;
1271 /******************************************************************************
1272 * tls1_prf [Internal]
1274 * TLS1 pseudo random function as specified in RFC 2246, chapter 5
1276 * PARAMS
1277 * hProv [I] Key container used to compute the pseudo random stream
1278 * hSecret [I] Key that holds the (pre-)master secret
1279 * pblobLabel [I] Descriptive label
1280 * pblobSeed [I] Seed value
1281 * pbBuffer [O] Pseudo random numbers will be stored here
1282 * dwBufferLen [I] Number of pseudo random bytes desired
1284 * RETURNS
1285 * Success: TRUE
1286 * Failure: FALSE
1288 static BOOL tls1_prf(HCRYPTPROV hProv, HCRYPTPROV hSecret, CONST PCRYPT_DATA_BLOB pblobLabel,
1289 CONST PCRYPT_DATA_BLOB pblobSeed, PBYTE pbBuffer, DWORD dwBufferLen)
1291 HMAC_INFO hmacInfo = { 0, NULL, 0, NULL, 0 };
1292 HCRYPTHASH hHMAC = (HCRYPTHASH)INVALID_HANDLE_VALUE;
1293 HCRYPTKEY hHalfSecret = (HCRYPTKEY)INVALID_HANDLE_VALUE;
1294 CRYPTKEY *pHalfSecret, *pSecret;
1295 DWORD dwHalfSecretLen;
1296 BOOL result = FALSE;
1297 CRYPT_DATA_BLOB blobLabelSeed;
1299 TRACE("(hProv=%08lx, hSecret=%08lx, pblobLabel=%p, pblobSeed=%p, pbBuffer=%p, dwBufferLen=%ld)\n",
1300 hProv, hSecret, pblobLabel, pblobSeed, pbBuffer, dwBufferLen);
1302 if (!lookup_handle(&handle_table, hSecret, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pSecret)) {
1303 SetLastError(NTE_FAIL);
1304 return FALSE;
1307 dwHalfSecretLen = (pSecret->dwKeyLen+1)/2;
1309 /* concatenation of the label and the seed */
1310 if (!concat_data_blobs(&blobLabelSeed, pblobLabel, pblobSeed)) goto exit;
1312 /* zero out the buffer, since two random streams will be xor'ed into it. */
1313 memset(pbBuffer, 0, dwBufferLen);
1315 /* build a 'fake' key, to hold the secret. CALG_SSL2_MASTER is used since it provides
1316 * the biggest range of valid key lengths. */
1317 hHalfSecret = new_key(hProv, CALG_SSL2_MASTER, MAKELONG(0,dwHalfSecretLen*8), &pHalfSecret);
1318 if (hHalfSecret == (HCRYPTKEY)INVALID_HANDLE_VALUE) goto exit;
1320 /* Derive an HMAC_MD5 hash and call the helper function. */
1321 memcpy(pHalfSecret->abKeyValue, pSecret->abKeyValue, dwHalfSecretLen);
1322 if (!RSAENH_CPCreateHash(hProv, CALG_HMAC, hHalfSecret, 0, &hHMAC)) goto exit;
1323 hmacInfo.HashAlgid = CALG_MD5;
1324 if (!RSAENH_CPSetHashParam(hProv, hHMAC, HP_HMAC_INFO, (BYTE*)&hmacInfo, 0)) goto exit;
1325 if (!tls1_p(hHMAC, &blobLabelSeed, pbBuffer, dwBufferLen)) goto exit;
1327 /* Reconfigure to HMAC_SHA hash and call helper function again. */
1328 memcpy(pHalfSecret->abKeyValue, pSecret->abKeyValue + (pSecret->dwKeyLen/2), dwHalfSecretLen);
1329 hmacInfo.HashAlgid = CALG_SHA;
1330 if (!RSAENH_CPSetHashParam(hProv, hHMAC, HP_HMAC_INFO, (BYTE*)&hmacInfo, 0)) goto exit;
1331 if (!tls1_p(hHMAC, &blobLabelSeed, pbBuffer, dwBufferLen)) goto exit;
1333 result = TRUE;
1334 exit:
1335 release_handle(&handle_table, hHalfSecret, RSAENH_MAGIC_KEY);
1336 if (hHMAC != (HCRYPTHASH)INVALID_HANDLE_VALUE) RSAENH_CPDestroyHash(hProv, hHMAC);
1337 free_data_blob(&blobLabelSeed);
1338 return result;
1341 /******************************************************************************
1342 * pad_data [Internal]
1344 * Helper function for data padding according to PKCS1 #2
1346 * PARAMS
1347 * abData [I] The data to be padded
1348 * dwDataLen [I] Length of the data
1349 * abBuffer [O] Padded data will be stored here
1350 * dwBufferLen [I] Length of the buffer (also length of padded data)
1351 * dwFlags [I] Padding format (CRYPT_SSL2_FALLBACK)
1353 * RETURN
1354 * Success: TRUE
1355 * Failure: FALSE (NTE_BAD_LEN, too much data to pad)
1357 static BOOL pad_data(CONST BYTE *abData, DWORD dwDataLen, BYTE *abBuffer, DWORD dwBufferLen,
1358 DWORD dwFlags)
1360 DWORD i;
1362 /* Ensure there is enough space for PKCS1 #2 padding */
1363 if (dwDataLen > dwBufferLen-11) {
1364 SetLastError(NTE_BAD_LEN);
1365 return FALSE;
1368 memmove(abBuffer + dwBufferLen - dwDataLen, abData, dwDataLen);
1370 abBuffer[0] = 0x00;
1371 abBuffer[1] = RSAENH_PKC_BLOCKTYPE;
1372 for (i=2; i < dwBufferLen - dwDataLen - 1; i++)
1373 do gen_rand_impl(&abBuffer[i], 1); while (!abBuffer[i]);
1374 if (dwFlags & CRYPT_SSL2_FALLBACK)
1375 for (i-=8; i < dwBufferLen - dwDataLen - 1; i++)
1376 abBuffer[i] = 0x03;
1377 abBuffer[i] = 0x00;
1379 return TRUE;
1382 /******************************************************************************
1383 * unpad_data [Internal]
1385 * Remove the PKCS1 padding from RSA decrypted data
1387 * PARAMS
1388 * abData [I] The padded data
1389 * dwDataLen [I] Length of the padded data
1390 * abBuffer [O] Data without padding will be stored here
1391 * dwBufferLen [I/O] I: Length of the buffer, O: Length of unpadded data
1392 * dwFlags [I] Currently none defined
1394 * RETURNS
1395 * Success: TRUE
1396 * Failure: FALSE, (NTE_BAD_DATA, no valid PKCS1 padding or buffer too small)
1398 static BOOL unpad_data(CONST BYTE *abData, DWORD dwDataLen, BYTE *abBuffer, DWORD *dwBufferLen,
1399 DWORD dwFlags)
1401 DWORD i;
1403 for (i=2; i<dwDataLen; i++)
1404 if (!abData[i])
1405 break;
1407 if ((i == dwDataLen) || (*dwBufferLen < dwDataLen - i - 1) ||
1408 (abData[0] != 0x00) || (abData[1] != RSAENH_PKC_BLOCKTYPE))
1410 SetLastError(NTE_BAD_DATA);
1411 return FALSE;
1414 *dwBufferLen = dwDataLen - i - 1;
1415 memmove(abBuffer, abData + i + 1, *dwBufferLen);
1416 return TRUE;
1419 /******************************************************************************
1420 * CPAcquireContext (RSAENH.@)
1422 * Acquire a handle to the key container specified by pszContainer
1424 * PARAMS
1425 * phProv [O] Pointer to the location the acquired handle will be written to.
1426 * pszContainer [I] Name of the desired key container. See Notes
1427 * dwFlags [I] Flags. See Notes.
1428 * pVTable [I] Pointer to a PVTableProvStruct containing callbacks.
1430 * RETURNS
1431 * Success: TRUE
1432 * Failure: FALSE
1434 * NOTES
1435 * If pszContainer is NULL or points to a zero length string the user's login
1436 * name will be used as the key container name.
1438 * If the CRYPT_NEW_KEYSET flag is set in dwFlags a new keyset will be created.
1439 * If a keyset with the given name already exists, the function fails and sets
1440 * last error to NTE_EXISTS. If CRYPT_NEW_KEYSET is not set and the specified
1441 * key container does not exist, function fails and sets last error to
1442 * NTE_BAD_KEYSET.
1444 BOOL WINAPI RSAENH_CPAcquireContext(HCRYPTPROV *phProv, LPSTR pszContainer,
1445 DWORD dwFlags, PVTableProvStruc pVTable)
1447 CHAR szKeyContainerName[MAX_PATH];
1448 CHAR szRegKey[MAX_PATH];
1450 TRACE("(phProv=%p, pszContainer=%s, dwFlags=%08lx, pVTable=%p)\n", phProv,
1451 debugstr_a(pszContainer), dwFlags, pVTable);
1453 if (pszContainer && *pszContainer)
1455 lstrcpynA(szKeyContainerName, pszContainer, MAX_PATH);
1457 else
1459 DWORD dwLen = sizeof(szKeyContainerName);
1460 if (!GetUserNameA(szKeyContainerName, &dwLen)) return FALSE;
1463 switch (dwFlags & (CRYPT_NEWKEYSET|CRYPT_VERIFYCONTEXT|CRYPT_DELETEKEYSET))
1465 case 0:
1466 *phProv = read_key_container(szKeyContainerName, dwFlags, pVTable);
1467 break;
1469 case CRYPT_DELETEKEYSET:
1470 if (snprintf(szRegKey, MAX_PATH, RSAENH_REGKEY, pszContainer) >= MAX_PATH) {
1471 SetLastError(NTE_BAD_KEYSET_PARAM);
1472 return FALSE;
1473 } else {
1474 RegDeleteKeyA(HKEY_CURRENT_USER, szRegKey);
1475 SetLastError(ERROR_SUCCESS);
1476 return TRUE;
1478 break;
1480 case CRYPT_NEWKEYSET:
1481 *phProv = read_key_container(szKeyContainerName, dwFlags, pVTable);
1482 if (*phProv != (HCRYPTPROV)INVALID_HANDLE_VALUE)
1484 release_handle(&handle_table, (unsigned int)*phProv, RSAENH_MAGIC_CONTAINER);
1485 SetLastError(NTE_EXISTS);
1486 return FALSE;
1488 *phProv = new_key_container(szKeyContainerName, dwFlags, pVTable);
1489 break;
1491 case CRYPT_VERIFYCONTEXT:
1492 if (pszContainer) {
1493 SetLastError(NTE_BAD_FLAGS);
1494 return FALSE;
1496 *phProv = new_key_container("", dwFlags, pVTable);
1497 break;
1499 default:
1500 *phProv = (unsigned int)INVALID_HANDLE_VALUE;
1501 SetLastError(NTE_BAD_FLAGS);
1502 return FALSE;
1505 if (*phProv != (unsigned int)INVALID_HANDLE_VALUE) {
1506 SetLastError(ERROR_SUCCESS);
1507 return TRUE;
1508 } else {
1509 return FALSE;
1513 /******************************************************************************
1514 * CPCreateHash (RSAENH.@)
1516 * CPCreateHash creates and initalizes a new hash object.
1518 * PARAMS
1519 * hProv [I] Handle to the key container to which the new hash will belong.
1520 * Algid [I] Identifies the hash algorithm, which will be used for the hash.
1521 * hKey [I] Handle to a session key applied for keyed hashes.
1522 * dwFlags [I] Currently no flags defined. Must be zero.
1523 * phHash [O] Points to the location where a handle to the new hash will be stored.
1525 * RETURNS
1526 * Success: TRUE
1527 * Failure: FALSE
1529 * NOTES
1530 * hKey is a handle to a session key applied in keyed hashes like MAC and HMAC.
1531 * If a normal hash object is to be created (like e.g. MD2 or SHA1) hKey must be zero.
1533 BOOL WINAPI RSAENH_CPCreateHash(HCRYPTPROV hProv, ALG_ID Algid, HCRYPTKEY hKey, DWORD dwFlags,
1534 HCRYPTHASH *phHash)
1536 CRYPTKEY *pCryptKey;
1537 CRYPTHASH *pCryptHash;
1538 const PROV_ENUMALGS_EX *peaAlgidInfo;
1540 TRACE("(hProv=%08lx, Algid=%08x, hKey=%08lx, dwFlags=%08lx, phHash=%p)\n", hProv, Algid, hKey,
1541 dwFlags, phHash);
1543 peaAlgidInfo = get_algid_info(hProv, Algid);
1544 if (!peaAlgidInfo) return FALSE;
1546 if (dwFlags)
1548 SetLastError(NTE_BAD_FLAGS);
1549 return FALSE;
1552 if (Algid == CALG_MAC || Algid == CALG_HMAC || Algid == CALG_SCHANNEL_MASTER_HASH ||
1553 Algid == CALG_TLS1PRF)
1555 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey)) {
1556 SetLastError(NTE_BAD_KEY);
1557 return FALSE;
1560 if ((Algid == CALG_MAC) && (GET_ALG_TYPE(pCryptKey->aiAlgid) != ALG_TYPE_BLOCK)) {
1561 SetLastError(NTE_BAD_KEY);
1562 return FALSE;
1565 if ((Algid == CALG_SCHANNEL_MASTER_HASH || Algid == CALG_TLS1PRF) &&
1566 (pCryptKey->aiAlgid != CALG_TLS1_MASTER))
1568 SetLastError(NTE_BAD_KEY);
1569 return FALSE;
1572 if ((Algid == CALG_TLS1PRF) && (pCryptKey->dwState != RSAENH_KEYSTATE_MASTERKEY)) {
1573 SetLastError(NTE_BAD_KEY_STATE);
1574 return FALSE;
1578 *phHash = (HCRYPTHASH)new_object(&handle_table, sizeof(CRYPTHASH), RSAENH_MAGIC_HASH,
1579 destroy_hash, (OBJECTHDR**)&pCryptHash);
1580 if (!pCryptHash) return FALSE;
1582 pCryptHash->aiAlgid = Algid;
1583 pCryptHash->hKey = hKey;
1584 pCryptHash->hProv = hProv;
1585 pCryptHash->dwState = RSAENH_HASHSTATE_IDLE;
1586 pCryptHash->pHMACInfo = (PHMAC_INFO)NULL;
1587 pCryptHash->dwHashSize = peaAlgidInfo->dwDefaultLen >> 3;
1588 init_data_blob(&pCryptHash->tpPRFParams.blobLabel);
1589 init_data_blob(&pCryptHash->tpPRFParams.blobSeed);
1591 if (Algid == CALG_SCHANNEL_MASTER_HASH) {
1592 CRYPT_DATA_BLOB blobRandom, blobKeyExpansion = { 13, "key expansion" };
1594 if (pCryptKey->dwState != RSAENH_KEYSTATE_MASTERKEY) {
1595 CRYPT_DATA_BLOB blobLabel = { 13, "master secret" };
1596 BYTE abKeyValue[48];
1598 /* See RFC 2246, chapter 8.1 */
1599 if (!concat_data_blobs(&blobRandom,
1600 &pCryptKey->siSChannelInfo.blobClientRandom,
1601 &pCryptKey->siSChannelInfo.blobServerRandom))
1603 return FALSE;
1605 tls1_prf(hProv, hKey, &blobLabel, &blobRandom, abKeyValue, 48);
1606 pCryptKey->dwState = RSAENH_KEYSTATE_MASTERKEY;
1607 memcpy(pCryptKey->abKeyValue, abKeyValue, 48);
1608 free_data_blob(&blobRandom);
1611 /* See RFC 2246, chapter 6.3 */
1612 if (!concat_data_blobs(&blobRandom,
1613 &pCryptKey->siSChannelInfo.blobServerRandom,
1614 &pCryptKey->siSChannelInfo.blobClientRandom))
1616 return FALSE;
1618 tls1_prf(hProv, hKey, &blobKeyExpansion, &blobRandom, pCryptHash->abHashValue,
1619 RSAENH_MAX_HASH_SIZE);
1620 free_data_blob(&blobRandom);
1623 return init_hash(pCryptHash);
1626 /******************************************************************************
1627 * CPDestroyHash (RSAENH.@)
1629 * Releases the handle to a hash object. The object is destroyed if it's reference
1630 * count reaches zero.
1632 * PARAMS
1633 * hProv [I] Handle to the key container to which the hash object belongs.
1634 * hHash [I] Handle to the hash object to be released.
1636 * RETURNS
1637 * Success: TRUE
1638 * Failure: FALSE
1640 BOOL WINAPI RSAENH_CPDestroyHash(HCRYPTPROV hProv, HCRYPTHASH hHash)
1642 TRACE("(hProv=%08lx, hHash=%08lx)\n", hProv, hHash);
1644 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
1646 SetLastError(NTE_BAD_UID);
1647 return FALSE;
1650 if (!release_handle(&handle_table, hHash, RSAENH_MAGIC_HASH))
1652 SetLastError(NTE_BAD_HASH);
1653 return FALSE;
1656 return TRUE;
1659 /******************************************************************************
1660 * CPDestroyKey (RSAENH.@)
1662 * Releases the handle to a key object. The object is destroyed if it's reference
1663 * count reaches zero.
1665 * PARAMS
1666 * hProv [I] Handle to the key container to which the key object belongs.
1667 * hKey [I] Handle to the key object to be released.
1669 * RETURNS
1670 * Success: TRUE
1671 * Failure: FALSE
1673 BOOL WINAPI RSAENH_CPDestroyKey(HCRYPTPROV hProv, HCRYPTKEY hKey)
1675 TRACE("(hProv=%08lx, hKey=%08lx)\n", hProv, hKey);
1677 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
1679 SetLastError(NTE_BAD_UID);
1680 return FALSE;
1683 if (!release_handle(&handle_table, hKey, RSAENH_MAGIC_KEY))
1685 SetLastError(NTE_BAD_KEY);
1686 return FALSE;
1689 return TRUE;
1692 /******************************************************************************
1693 * CPDuplicateHash (RSAENH.@)
1695 * Clones a hash object including it's current state.
1697 * PARAMS
1698 * hUID [I] Handle to the key container the hash belongs to.
1699 * hHash [I] Handle to the hash object to be cloned.
1700 * pdwReserved [I] Reserved. Must be NULL.
1701 * dwFlags [I] No flags are currently defined. Must be 0.
1702 * phHash [O] Handle to the cloned hash object.
1704 * RETURNS
1705 * Success: TRUE.
1706 * Failure: FALSE.
1708 BOOL WINAPI RSAENH_CPDuplicateHash(HCRYPTPROV hUID, HCRYPTHASH hHash, DWORD *pdwReserved,
1709 DWORD dwFlags, HCRYPTHASH *phHash)
1711 CRYPTHASH *pSrcHash, *pDestHash;
1713 TRACE("(hUID=%08lx, hHash=%08lx, pdwReserved=%p, dwFlags=%08lx, phHash=%p)\n", hUID, hHash,
1714 pdwReserved, dwFlags, phHash);
1716 if (!is_valid_handle(&handle_table, hUID, RSAENH_MAGIC_CONTAINER))
1718 SetLastError(NTE_BAD_UID);
1719 return FALSE;
1722 if (!lookup_handle(&handle_table, hHash, RSAENH_MAGIC_HASH, (OBJECTHDR**)&pSrcHash))
1724 SetLastError(NTE_BAD_HASH);
1725 return FALSE;
1728 if (!phHash || pdwReserved || dwFlags)
1730 SetLastError(ERROR_INVALID_PARAMETER);
1731 return FALSE;
1734 *phHash = (HCRYPTHASH)new_object(&handle_table, sizeof(CRYPTHASH), RSAENH_MAGIC_HASH,
1735 destroy_hash, (OBJECTHDR**)&pDestHash);
1736 if (*phHash != (HCRYPTHASH)INVALID_HANDLE_VALUE)
1738 memcpy(pDestHash, pSrcHash, sizeof(CRYPTHASH));
1739 duplicate_hash_impl(pSrcHash->aiAlgid, &pSrcHash->context, &pDestHash->context);
1740 copy_hmac_info(&pDestHash->pHMACInfo, pSrcHash->pHMACInfo);
1741 copy_data_blob(&pDestHash->tpPRFParams.blobLabel, &pSrcHash->tpPRFParams.blobLabel);
1742 copy_data_blob(&pDestHash->tpPRFParams.blobSeed, &pSrcHash->tpPRFParams.blobSeed);
1745 return *phHash != (HCRYPTHASH)INVALID_HANDLE_VALUE;
1748 /******************************************************************************
1749 * CPDuplicateKey (RSAENH.@)
1751 * Clones a key object including it's current state.
1753 * PARAMS
1754 * hUID [I] Handle to the key container the hash belongs to.
1755 * hKey [I] Handle to the key object to be cloned.
1756 * pdwReserved [I] Reserved. Must be NULL.
1757 * dwFlags [I] No flags are currently defined. Must be 0.
1758 * phHash [O] Handle to the cloned key object.
1760 * RETURNS
1761 * Success: TRUE.
1762 * Failure: FALSE.
1764 BOOL WINAPI RSAENH_CPDuplicateKey(HCRYPTPROV hUID, HCRYPTKEY hKey, DWORD *pdwReserved,
1765 DWORD dwFlags, HCRYPTKEY *phKey)
1767 CRYPTKEY *pSrcKey, *pDestKey;
1769 TRACE("(hUID=%08lx, hKey=%08lx, pdwReserved=%p, dwFlags=%08lx, phKey=%p)\n", hUID, hKey,
1770 pdwReserved, dwFlags, phKey);
1772 if (!is_valid_handle(&handle_table, hUID, RSAENH_MAGIC_CONTAINER))
1774 SetLastError(NTE_BAD_UID);
1775 return FALSE;
1778 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pSrcKey))
1780 SetLastError(NTE_BAD_KEY);
1781 return FALSE;
1784 if (!phKey || pdwReserved || dwFlags)
1786 SetLastError(ERROR_INVALID_PARAMETER);
1787 return FALSE;
1790 *phKey = (HCRYPTKEY)new_object(&handle_table, sizeof(CRYPTKEY), RSAENH_MAGIC_KEY, destroy_key,
1791 (OBJECTHDR**)&pDestKey);
1792 if (*phKey != (HCRYPTKEY)INVALID_HANDLE_VALUE)
1794 memcpy(pDestKey, pSrcKey, sizeof(CRYPTKEY));
1795 copy_data_blob(&pDestKey->siSChannelInfo.blobServerRandom,
1796 &pSrcKey->siSChannelInfo.blobServerRandom);
1797 copy_data_blob(&pDestKey->siSChannelInfo.blobClientRandom,
1798 &pSrcKey->siSChannelInfo.blobClientRandom);
1799 duplicate_key_impl(pSrcKey->aiAlgid, &pSrcKey->context, &pDestKey->context);
1800 return TRUE;
1802 else
1804 return FALSE;
1808 /******************************************************************************
1809 * CPEncrypt (RSAENH.@)
1811 * Encrypt data.
1813 * PARAMS
1814 * hProv [I] The key container hKey and hHash belong to.
1815 * hKey [I] The key used to encrypt the data.
1816 * hHash [I] An optional hash object for parallel hashing. See notes.
1817 * Final [I] Indicates if this is the last block of data to encrypt.
1818 * dwFlags [I] Currently no flags defined. Must be zero.
1819 * pbData [I/O] Pointer to the data to encrypt. Encrypted data will also be stored there.
1820 * pdwDataLen [I/O] I: Length of data to encrypt, O: Length of encrypted data.
1821 * dwBufLen [I] Size of the buffer at pbData.
1823 * RETURNS
1824 * Success: TRUE.
1825 * Failure: FALSE.
1827 * NOTES
1828 * If a hash object handle is provided in hHash, it will be updated with the plaintext.
1829 * This is useful for message signatures.
1831 * This function uses the standard WINAPI protocol for querying data of dynamic length.
1833 BOOL WINAPI RSAENH_CPEncrypt(HCRYPTPROV hProv, HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final,
1834 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen, DWORD dwBufLen)
1836 CRYPTKEY *pCryptKey;
1837 BYTE *in, out[RSAENH_MAX_BLOCK_SIZE], o[RSAENH_MAX_BLOCK_SIZE];
1838 DWORD dwEncryptedLen, i, j, k;
1840 TRACE("(hProv=%08lx, hKey=%08lx, hHash=%08lx, Final=%d, dwFlags=%08lx, pbData=%p, "
1841 "pdwDataLen=%p, dwBufLen=%ld)\n", hProv, hKey, hHash, Final, dwFlags, pbData, pdwDataLen,
1842 dwBufLen);
1844 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
1846 SetLastError(NTE_BAD_UID);
1847 return FALSE;
1850 if (dwFlags)
1852 SetLastError(NTE_BAD_FLAGS);
1853 return FALSE;
1856 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
1858 SetLastError(NTE_BAD_KEY);
1859 return FALSE;
1862 if (pCryptKey->dwState == RSAENH_KEYSTATE_IDLE)
1863 pCryptKey->dwState = RSAENH_KEYSTATE_ENCRYPTING;
1865 if (pCryptKey->dwState != RSAENH_KEYSTATE_ENCRYPTING)
1867 SetLastError(NTE_BAD_DATA);
1868 return FALSE;
1871 if (is_valid_handle(&handle_table, hHash, RSAENH_MAGIC_HASH)) {
1872 if (!RSAENH_CPHashData(hProv, hHash, pbData, *pdwDataLen, 0)) return FALSE;
1875 if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_BLOCK) {
1876 if (!Final && (*pdwDataLen % pCryptKey->dwBlockLen)) {
1877 SetLastError(NTE_BAD_DATA);
1878 return FALSE;
1881 dwEncryptedLen = (*pdwDataLen/pCryptKey->dwBlockLen+(Final?1:0))*pCryptKey->dwBlockLen;
1882 for (i=*pdwDataLen; i<dwEncryptedLen && i<dwBufLen; i++) pbData[i] = dwEncryptedLen - *pdwDataLen;
1883 *pdwDataLen = dwEncryptedLen;
1885 if (*pdwDataLen > dwBufLen)
1887 SetLastError(ERROR_MORE_DATA);
1888 return FALSE;
1891 for (i=0, in=pbData; i<*pdwDataLen; i+=pCryptKey->dwBlockLen, in+=pCryptKey->dwBlockLen) {
1892 switch (pCryptKey->dwMode) {
1893 case CRYPT_MODE_ECB:
1894 encrypt_block_impl(pCryptKey->aiAlgid, &pCryptKey->context, in, out,
1895 RSAENH_ENCRYPT);
1896 break;
1898 case CRYPT_MODE_CBC:
1899 for (j=0; j<pCryptKey->dwBlockLen; j++) in[j] ^= pCryptKey->abChainVector[j];
1900 encrypt_block_impl(pCryptKey->aiAlgid, &pCryptKey->context, in, out,
1901 RSAENH_ENCRYPT);
1902 memcpy(pCryptKey->abChainVector, out, pCryptKey->dwBlockLen);
1903 break;
1905 case CRYPT_MODE_CFB:
1906 for (j=0; j<pCryptKey->dwBlockLen; j++) {
1907 encrypt_block_impl(pCryptKey->aiAlgid, &pCryptKey->context,
1908 pCryptKey->abChainVector, o, RSAENH_ENCRYPT);
1909 out[j] = in[j] ^ o[0];
1910 for (k=0; k<pCryptKey->dwBlockLen-1; k++)
1911 pCryptKey->abChainVector[k] = pCryptKey->abChainVector[k+1];
1912 pCryptKey->abChainVector[k] = out[j];
1914 break;
1916 default:
1917 SetLastError(NTE_BAD_ALGID);
1918 return FALSE;
1920 memcpy(in, out, pCryptKey->dwBlockLen);
1922 } else if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_STREAM) {
1923 encrypt_stream_impl(pCryptKey->aiAlgid, &pCryptKey->context, pbData, *pdwDataLen);
1924 } else if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_RSA) {
1925 if (pCryptKey->aiAlgid == CALG_RSA_SIGN) {
1926 SetLastError(NTE_BAD_KEY);
1927 return FALSE;
1929 if (dwBufLen < pCryptKey->dwBlockLen) {
1930 SetLastError(ERROR_MORE_DATA);
1931 return FALSE;
1933 if (!pad_data(pbData, *pdwDataLen, pbData, pCryptKey->dwBlockLen, dwFlags)) return FALSE;
1934 encrypt_block_impl(pCryptKey->aiAlgid, &pCryptKey->context, pbData, pbData, RSAENH_ENCRYPT);
1935 *pdwDataLen = pCryptKey->dwBlockLen;
1936 Final = TRUE;
1937 } else {
1938 SetLastError(NTE_BAD_TYPE);
1939 return FALSE;
1942 if (Final) setup_key(pCryptKey);
1944 return TRUE;
1947 /******************************************************************************
1948 * CPDecrypt (RSAENH.@)
1950 * Decrypt data.
1952 * PARAMS
1953 * hProv [I] The key container hKey and hHash belong to.
1954 * hKey [I] The key used to decrypt the data.
1955 * hHash [I] An optional hash object for parallel hashing. See notes.
1956 * Final [I] Indicates if this is the last block of data to decrypt.
1957 * dwFlags [I] Currently no flags defined. Must be zero.
1958 * pbData [I/O] Pointer to the data to decrypt. Plaintext will also be stored there.
1959 * pdwDataLen [I/O] I: Length of ciphertext, O: Length of plaintext.
1961 * RETURNS
1962 * Success: TRUE.
1963 * Failure: FALSE.
1965 * NOTES
1966 * If a hash object handle is provided in hHash, it will be updated with the plaintext.
1967 * This is useful for message signatures.
1969 * This function uses the standard WINAPI protocol for querying data of dynamic length.
1971 BOOL WINAPI RSAENH_CPDecrypt(HCRYPTPROV hProv, HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final,
1972 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
1974 CRYPTKEY *pCryptKey;
1975 BYTE *in, out[RSAENH_MAX_BLOCK_SIZE], o[RSAENH_MAX_BLOCK_SIZE];
1976 DWORD i, j, k;
1977 DWORD dwMax;
1979 TRACE("(hProv=%08lx, hKey=%08lx, hHash=%08lx, Final=%d, dwFlags=%08lx, pbData=%p, "
1980 "pdwDataLen=%p)\n", hProv, hKey, hHash, Final, dwFlags, pbData, pdwDataLen);
1982 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
1984 SetLastError(NTE_BAD_UID);
1985 return FALSE;
1988 if (dwFlags)
1990 SetLastError(NTE_BAD_FLAGS);
1991 return FALSE;
1994 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
1996 SetLastError(NTE_BAD_KEY);
1997 return FALSE;
2000 if (pCryptKey->dwState == RSAENH_KEYSTATE_IDLE)
2001 pCryptKey->dwState = RSAENH_KEYSTATE_DECRYPTING;
2003 if (pCryptKey->dwState != RSAENH_KEYSTATE_DECRYPTING)
2005 SetLastError(NTE_BAD_DATA);
2006 return FALSE;
2009 dwMax=*pdwDataLen;
2011 if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_BLOCK) {
2012 for (i=0, in=pbData; i<*pdwDataLen; i+=pCryptKey->dwBlockLen, in+=pCryptKey->dwBlockLen) {
2013 switch (pCryptKey->dwMode) {
2014 case CRYPT_MODE_ECB:
2015 encrypt_block_impl(pCryptKey->aiAlgid, &pCryptKey->context, in, out,
2016 RSAENH_DECRYPT);
2017 break;
2019 case CRYPT_MODE_CBC:
2020 encrypt_block_impl(pCryptKey->aiAlgid, &pCryptKey->context, in, out,
2021 RSAENH_DECRYPT);
2022 for (j=0; j<pCryptKey->dwBlockLen; j++) out[j] ^= pCryptKey->abChainVector[j];
2023 memcpy(pCryptKey->abChainVector, in, pCryptKey->dwBlockLen);
2024 break;
2026 case CRYPT_MODE_CFB:
2027 for (j=0; j<pCryptKey->dwBlockLen; j++) {
2028 encrypt_block_impl(pCryptKey->aiAlgid, &pCryptKey->context,
2029 pCryptKey->abChainVector, o, RSAENH_ENCRYPT);
2030 out[j] = in[j] ^ o[0];
2031 for (k=0; k<pCryptKey->dwBlockLen-1; k++)
2032 pCryptKey->abChainVector[k] = pCryptKey->abChainVector[k+1];
2033 pCryptKey->abChainVector[k] = in[j];
2035 break;
2037 default:
2038 SetLastError(NTE_BAD_ALGID);
2039 return FALSE;
2041 memcpy(in, out, pCryptKey->dwBlockLen);
2043 if (Final) *pdwDataLen -= pbData[*pdwDataLen-1];
2045 } else if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_STREAM) {
2046 encrypt_stream_impl(pCryptKey->aiAlgid, &pCryptKey->context, pbData, *pdwDataLen);
2047 } else if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_RSA) {
2048 if (pCryptKey->aiAlgid == CALG_RSA_SIGN) {
2049 SetLastError(NTE_BAD_KEY);
2050 return FALSE;
2052 encrypt_block_impl(pCryptKey->aiAlgid, &pCryptKey->context, pbData, pbData, RSAENH_DECRYPT);
2053 if (!unpad_data(pbData, pCryptKey->dwBlockLen, pbData, pdwDataLen, dwFlags)) return FALSE;
2054 Final = TRUE;
2055 } else {
2056 SetLastError(NTE_BAD_TYPE);
2057 return FALSE;
2060 if (Final) setup_key(pCryptKey);
2062 if (is_valid_handle(&handle_table, hHash, RSAENH_MAGIC_HASH)) {
2063 if (*pdwDataLen>dwMax ||
2064 !RSAENH_CPHashData(hProv, hHash, pbData, *pdwDataLen, 0)) return FALSE;
2067 return TRUE;
2070 /******************************************************************************
2071 * CPExportKey (RSAENH.@)
2073 * Export a key into a binary large object (BLOB).
2075 * PARAMS
2076 * hProv [I] Key container from which a key is to be exported.
2077 * hKey [I] Key to be exported.
2078 * hPubKey [I] Key used to encrypt sensitive BLOB data.
2079 * dwBlobType [I] SIMPLEBLOB, PUBLICKEYBLOB or PRIVATEKEYBLOB.
2080 * dwFlags [I] Currently none defined.
2081 * pbData [O] Pointer to a buffer where the BLOB will be written to.
2082 * pdwDataLen [I/O] I: Size of buffer at pbData, O: Size of BLOB
2084 * RETURNS
2085 * Success: TRUE.
2086 * Failure: FALSE.
2088 BOOL WINAPI RSAENH_CPExportKey(HCRYPTPROV hProv, HCRYPTKEY hKey, HCRYPTKEY hPubKey,
2089 DWORD dwBlobType, DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
2091 CRYPTKEY *pCryptKey, *pPubKey;
2092 BLOBHEADER *pBlobHeader = (BLOBHEADER*)pbData;
2093 RSAPUBKEY *pRSAPubKey = (RSAPUBKEY*)(pBlobHeader+1);
2094 ALG_ID *pAlgid = (ALG_ID*)(pBlobHeader+1);
2095 DWORD dwDataLen;
2097 TRACE("(hProv=%08lx, hKey=%08lx, hPubKey=%08lx, dwBlobType=%08lx, dwFlags=%08lx, pbData=%p,"
2098 "pdwDataLen=%p)\n", hProv, hKey, hPubKey, dwBlobType, dwFlags, pbData, pdwDataLen);
2100 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
2102 SetLastError(NTE_BAD_UID);
2103 return FALSE;
2106 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
2108 SetLastError(NTE_BAD_KEY);
2109 return FALSE;
2112 if (dwFlags & CRYPT_SSL2_FALLBACK) {
2113 if (pCryptKey->aiAlgid != CALG_SSL2_MASTER) {
2114 SetLastError(NTE_BAD_KEY);
2115 return FALSE;
2119 switch ((BYTE)dwBlobType)
2121 case SIMPLEBLOB:
2122 if (!lookup_handle(&handle_table, hPubKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pPubKey)){
2123 SetLastError(NTE_BAD_PUBLIC_KEY); /* FIXME: error_code? */
2124 return FALSE;
2127 if (!(GET_ALG_CLASS(pCryptKey->aiAlgid)&(ALG_CLASS_DATA_ENCRYPT|ALG_CLASS_MSG_ENCRYPT))) {
2128 SetLastError(NTE_BAD_KEY); /* FIXME: error code? */
2129 return FALSE;
2132 dwDataLen = sizeof(BLOBHEADER) + sizeof(ALG_ID) + pPubKey->dwBlockLen;
2133 if (pbData) {
2134 if (*pdwDataLen < dwDataLen) {
2135 SetLastError(ERROR_MORE_DATA);
2136 *pdwDataLen = dwDataLen;
2137 return FALSE;
2140 pBlobHeader->bType = SIMPLEBLOB;
2141 pBlobHeader->bVersion = CUR_BLOB_VERSION;
2142 pBlobHeader->reserved = 0;
2143 pBlobHeader->aiKeyAlg = pCryptKey->aiAlgid;
2145 *pAlgid = pPubKey->aiAlgid;
2147 if (!pad_data(pCryptKey->abKeyValue, pCryptKey->dwKeyLen, (BYTE*)(pAlgid+1),
2148 pPubKey->dwBlockLen, dwFlags))
2150 return FALSE;
2153 encrypt_block_impl(pPubKey->aiAlgid, &pPubKey->context, (BYTE*)(pAlgid+1),
2154 (BYTE*)(pAlgid+1), RSAENH_ENCRYPT);
2156 *pdwDataLen = dwDataLen;
2157 return TRUE;
2159 case PUBLICKEYBLOB:
2160 if (is_valid_handle(&handle_table, hPubKey, RSAENH_MAGIC_KEY)) {
2161 SetLastError(NTE_BAD_KEY); /* FIXME: error code? */
2162 return FALSE;
2165 if ((pCryptKey->aiAlgid != CALG_RSA_KEYX) && (pCryptKey->aiAlgid != CALG_RSA_SIGN)) {
2166 SetLastError(NTE_BAD_KEY);
2167 return FALSE;
2170 dwDataLen = sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) + pCryptKey->dwKeyLen;
2171 if (pbData) {
2172 if (*pdwDataLen < dwDataLen) {
2173 SetLastError(ERROR_MORE_DATA);
2174 *pdwDataLen = dwDataLen;
2175 return FALSE;
2178 pBlobHeader->bType = PUBLICKEYBLOB;
2179 pBlobHeader->bVersion = CUR_BLOB_VERSION;
2180 pBlobHeader->reserved = 0;
2181 pBlobHeader->aiKeyAlg = pCryptKey->aiAlgid;
2183 pRSAPubKey->magic = RSAENH_MAGIC_RSA1;
2184 pRSAPubKey->bitlen = pCryptKey->dwKeyLen << 3;
2186 export_public_key_impl((BYTE*)(pRSAPubKey+1), &pCryptKey->context,
2187 pCryptKey->dwKeyLen, &pRSAPubKey->pubexp);
2189 *pdwDataLen = dwDataLen;
2190 return TRUE;
2192 case PRIVATEKEYBLOB:
2193 if ((pCryptKey->aiAlgid != CALG_RSA_KEYX) && (pCryptKey->aiAlgid != CALG_RSA_SIGN)) {
2194 SetLastError(NTE_BAD_KEY);
2195 return FALSE;
2198 dwDataLen = sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) +
2199 2 * pCryptKey->dwKeyLen + 5 * ((pCryptKey->dwKeyLen + 1) >> 1);
2200 if (pbData) {
2201 if (*pdwDataLen < dwDataLen) {
2202 SetLastError(ERROR_MORE_DATA);
2203 *pdwDataLen = dwDataLen;
2204 return FALSE;
2207 pBlobHeader->bType = PRIVATEKEYBLOB;
2208 pBlobHeader->bVersion = CUR_BLOB_VERSION;
2209 pBlobHeader->reserved = 0;
2210 pBlobHeader->aiKeyAlg = pCryptKey->aiAlgid;
2212 pRSAPubKey->magic = RSAENH_MAGIC_RSA2;
2213 pRSAPubKey->bitlen = pCryptKey->dwKeyLen << 3;
2215 export_private_key_impl((BYTE*)(pRSAPubKey+1), &pCryptKey->context,
2216 pCryptKey->dwKeyLen, &pRSAPubKey->pubexp);
2218 *pdwDataLen = dwDataLen;
2219 return TRUE;
2221 default:
2222 SetLastError(NTE_BAD_TYPE); /* FIXME: error code? */
2223 return FALSE;
2227 /******************************************************************************
2228 * CPImportKey (RSAENH.@)
2230 * Import a BLOB'ed key into a key container.
2232 * PARAMS
2233 * hProv [I] Key container into which the key is to be imported.
2234 * pbData [I] Pointer to a buffer which holds the BLOB.
2235 * dwDataLen [I] Length of data in buffer at pbData.
2236 * hPubKey [I] Key used to decrypt sensitive BLOB data.
2237 * dwFlags [I] Currently none defined.
2238 * phKey [O] Handle to the imported key.
2240 * RETURNS
2241 * Success: TRUE.
2242 * Failure: FALSE.
2244 BOOL WINAPI RSAENH_CPImportKey(HCRYPTPROV hProv, CONST BYTE *pbData, DWORD dwDataLen,
2245 HCRYPTKEY hPubKey, DWORD dwFlags, HCRYPTKEY *phKey)
2247 CRYPTKEY *pCryptKey, *pPubKey;
2248 CONST BLOBHEADER *pBlobHeader = (CONST BLOBHEADER*)pbData;
2249 CONST RSAPUBKEY *pRSAPubKey = (CONST RSAPUBKEY*)(pBlobHeader+1);
2250 CONST ALG_ID *pAlgid = (CONST ALG_ID*)(pBlobHeader+1);
2251 CONST BYTE *pbKeyStream = (CONST BYTE*)(pAlgid + 1);
2252 BYTE *pbDecrypted;
2253 DWORD dwKeyLen;
2255 TRACE("(hProv=%08lx, pbData=%p, dwDataLen=%ld, hPubKey=%08lx, dwFlags=%08lx, phKey=%p)\n",
2256 hProv, pbData, dwDataLen, hPubKey, dwFlags, phKey);
2258 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
2260 SetLastError(NTE_BAD_UID);
2261 return FALSE;
2264 if (dwDataLen < sizeof(BLOBHEADER) ||
2265 pBlobHeader->bVersion != CUR_BLOB_VERSION ||
2266 pBlobHeader->reserved != 0)
2268 SetLastError(NTE_BAD_DATA);
2269 return FALSE;
2272 switch (pBlobHeader->bType)
2274 case PRIVATEKEYBLOB:
2275 if ((dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY)) ||
2276 (pRSAPubKey->magic != RSAENH_MAGIC_RSA2) ||
2277 (dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) +
2278 (2 * pRSAPubKey->bitlen >> 3) + (5 * ((pRSAPubKey->bitlen+8)>>4))))
2280 SetLastError(NTE_BAD_DATA);
2281 return FALSE;
2284 *phKey = new_key(hProv, pBlobHeader->aiKeyAlg, MAKELONG(0,pRSAPubKey->bitlen), &pCryptKey);
2285 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) return FALSE;
2286 setup_key(pCryptKey);
2287 return import_private_key_impl((CONST BYTE*)(pRSAPubKey+1), &pCryptKey->context,
2288 pRSAPubKey->bitlen/8, pRSAPubKey->pubexp);
2290 case PUBLICKEYBLOB:
2291 if ((dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY)) ||
2292 (pRSAPubKey->magic != RSAENH_MAGIC_RSA1) ||
2293 (dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) + (pRSAPubKey->bitlen >> 3)))
2295 SetLastError(NTE_BAD_DATA);
2296 return FALSE;
2299 *phKey = new_key(hProv, pBlobHeader->aiKeyAlg, MAKELONG(0,pRSAPubKey->bitlen), &pCryptKey);
2300 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) return FALSE;
2301 setup_key(pCryptKey);
2302 return import_public_key_impl((CONST BYTE*)(pRSAPubKey+1), &pCryptKey->context,
2303 pRSAPubKey->bitlen >> 3, pRSAPubKey->pubexp);
2305 case SIMPLEBLOB:
2306 if (!lookup_handle(&handle_table, hPubKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pPubKey) ||
2307 pPubKey->aiAlgid != CALG_RSA_KEYX)
2309 SetLastError(NTE_BAD_PUBLIC_KEY); /* FIXME: error code? */
2310 return FALSE;
2313 if (dwDataLen < sizeof(BLOBHEADER)+sizeof(ALG_ID)+pPubKey->dwBlockLen)
2315 SetLastError(NTE_BAD_DATA); /* FIXME: error code */
2316 return FALSE;
2319 pbDecrypted = HeapAlloc(GetProcessHeap(), 0, pPubKey->dwBlockLen);
2320 if (!pbDecrypted) return FALSE;
2321 encrypt_block_impl(pPubKey->aiAlgid, &pPubKey->context, pbKeyStream, pbDecrypted,
2322 RSAENH_DECRYPT);
2324 dwKeyLen = RSAENH_MAX_KEY_SIZE;
2325 if (!unpad_data(pbDecrypted, pPubKey->dwBlockLen, pbDecrypted, &dwKeyLen, dwFlags)) {
2326 HeapFree(GetProcessHeap(), 0, pbDecrypted);
2327 return FALSE;
2330 *phKey = new_key(hProv, pBlobHeader->aiKeyAlg, dwKeyLen<<19, &pCryptKey);
2331 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE)
2333 HeapFree(GetProcessHeap(), 0, pbDecrypted);
2334 return FALSE;
2336 memcpy(pCryptKey->abKeyValue, pbDecrypted, dwKeyLen);
2337 HeapFree(GetProcessHeap(), 0, pbDecrypted);
2338 setup_key(pCryptKey);
2339 return TRUE;
2341 default:
2342 SetLastError(NTE_BAD_TYPE); /* FIXME: error code? */
2343 return FALSE;
2347 /******************************************************************************
2348 * CPGenKey (RSAENH.@)
2350 * Generate a key in the key container
2352 * PARAMS
2353 * hProv [I] Key container for which a key is to be generated.
2354 * Algid [I] Crypto algorithm identifier for the key to be generated.
2355 * dwFlags [I] Upper 16 bits: Binary length of key. Lower 16 bits: Flags. See Notes
2356 * phKey [O] Handle to the generated key.
2358 * RETURNS
2359 * Success: TRUE.
2360 * Failure: FALSE.
2362 * FIXME
2363 * Flags currently not considered.
2365 * NOTES
2366 * Private key-exchange- and signature-keys can be generated with Algid AT_KEYEXCHANGE
2367 * and AT_SIGNATURE values.
2369 BOOL WINAPI RSAENH_CPGenKey(HCRYPTPROV hProv, ALG_ID Algid, DWORD dwFlags, HCRYPTKEY *phKey)
2371 KEYCONTAINER *pKeyContainer;
2372 CRYPTKEY *pCryptKey;
2374 TRACE("(hProv=%08lx, aiAlgid=%d, dwFlags=%08lx, phKey=%p)\n", hProv, Algid, dwFlags, phKey);
2376 if (!lookup_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER,
2377 (OBJECTHDR**)&pKeyContainer))
2379 /* MSDN: hProv not containing valid context handle */
2380 SetLastError(NTE_BAD_UID);
2381 return FALSE;
2384 switch (Algid)
2386 case AT_SIGNATURE:
2387 case CALG_RSA_SIGN:
2388 *phKey = new_key(hProv, CALG_RSA_SIGN, dwFlags, &pCryptKey);
2389 if (pCryptKey) {
2390 new_key_impl(pCryptKey->aiAlgid, &pCryptKey->context, pCryptKey->dwKeyLen);
2391 setup_key(pCryptKey);
2392 if (Algid == AT_SIGNATURE) {
2393 RSAENH_CPDestroyKey(hProv, pKeyContainer->hSignatureKeyPair);
2394 copy_handle(&handle_table, *phKey, RSAENH_MAGIC_KEY,
2395 (unsigned int*)&pKeyContainer->hSignatureKeyPair);
2398 break;
2400 case AT_KEYEXCHANGE:
2401 case CALG_RSA_KEYX:
2402 *phKey = new_key(hProv, CALG_RSA_KEYX, dwFlags, &pCryptKey);
2403 if (pCryptKey) {
2404 new_key_impl(pCryptKey->aiAlgid, &pCryptKey->context, pCryptKey->dwKeyLen);
2405 setup_key(pCryptKey);
2406 if (Algid == AT_KEYEXCHANGE) {
2407 RSAENH_CPDestroyKey(hProv, pKeyContainer->hKeyExchangeKeyPair);
2408 copy_handle(&handle_table, *phKey, RSAENH_MAGIC_KEY,
2409 (unsigned int*)&pKeyContainer->hKeyExchangeKeyPair);
2412 break;
2414 case CALG_RC2:
2415 case CALG_RC4:
2416 case CALG_DES:
2417 case CALG_3DES_112:
2418 case CALG_3DES:
2419 case CALG_PCT1_MASTER:
2420 case CALG_SSL2_MASTER:
2421 case CALG_SSL3_MASTER:
2422 case CALG_TLS1_MASTER:
2423 *phKey = new_key(hProv, Algid, dwFlags, &pCryptKey);
2424 if (pCryptKey) {
2425 gen_rand_impl(pCryptKey->abKeyValue, RSAENH_MAX_KEY_SIZE);
2426 switch (Algid) {
2427 case CALG_SSL3_MASTER:
2428 pCryptKey->abKeyValue[0] = RSAENH_SSL3_VERSION_MAJOR;
2429 pCryptKey->abKeyValue[1] = RSAENH_SSL3_VERSION_MINOR;
2430 break;
2432 case CALG_TLS1_MASTER:
2433 pCryptKey->abKeyValue[0] = RSAENH_TLS1_VERSION_MAJOR;
2434 pCryptKey->abKeyValue[1] = RSAENH_TLS1_VERSION_MINOR;
2435 break;
2437 setup_key(pCryptKey);
2439 break;
2441 default:
2442 /* MSDN: Algorithm not supported specified by Algid */
2443 SetLastError(NTE_BAD_ALGID);
2444 return FALSE;
2447 return *phKey != (unsigned int)INVALID_HANDLE_VALUE;
2450 /******************************************************************************
2451 * CPGenRandom (RSAENH.@)
2453 * Generate a random byte stream.
2455 * PARAMS
2456 * hProv [I] Key container that is used to generate random bytes.
2457 * dwLen [I] Specifies the number of requested random data bytes.
2458 * pbBuffer [O] Random bytes will be stored here.
2460 * RETURNS
2461 * Success: TRUE
2462 * Failure: FALSE
2464 BOOL WINAPI RSAENH_CPGenRandom(HCRYPTPROV hProv, DWORD dwLen, BYTE *pbBuffer)
2466 TRACE("(hProv=%08lx, dwLen=%ld, pbBuffer=%p)\n", hProv, dwLen, pbBuffer);
2468 if (!is_valid_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER))
2470 /* MSDN: hProv not containing valid context handle */
2471 SetLastError(NTE_BAD_UID);
2472 return FALSE;
2475 return gen_rand_impl(pbBuffer, dwLen);
2478 /******************************************************************************
2479 * CPGetHashParam (RSAENH.@)
2481 * Query parameters of an hash object.
2483 * PARAMS
2484 * hProv [I] The kea container, which the hash belongs to.
2485 * hHash [I] The hash object that is to be queried.
2486 * dwParam [I] Specifies the parameter that is to be queried.
2487 * pbData [I] Pointer to the buffer where the parameter value will be stored.
2488 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
2489 * dwFlags [I] None currently defined.
2491 * RETURNS
2492 * Success: TRUE
2493 * Failure: FALSE
2495 * NOTES
2496 * Valid dwParams are: HP_ALGID, HP_HASHSIZE, HP_HASHVALUE. The hash will be
2497 * finalized if HP_HASHVALUE is queried.
2499 BOOL WINAPI RSAENH_CPGetHashParam(HCRYPTPROV hProv, HCRYPTHASH hHash, DWORD dwParam, BYTE *pbData,
2500 DWORD *pdwDataLen, DWORD dwFlags)
2502 CRYPTHASH *pCryptHash;
2504 TRACE("(hProv=%08lx, hHash=%08lx, dwParam=%08lx, pbData=%p, pdwDataLen=%p, dwFlags=%08lx)\n",
2505 hProv, hHash, dwParam, pbData, pdwDataLen, dwFlags);
2507 if (!is_valid_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER))
2509 SetLastError(NTE_BAD_UID);
2510 return FALSE;
2513 if (dwFlags)
2515 SetLastError(NTE_BAD_FLAGS);
2516 return FALSE;
2519 if (!lookup_handle(&handle_table, (unsigned int)hHash, RSAENH_MAGIC_HASH,
2520 (OBJECTHDR**)&pCryptHash))
2522 SetLastError(NTE_BAD_HASH);
2523 return FALSE;
2526 if (!pdwDataLen)
2528 SetLastError(ERROR_INVALID_PARAMETER);
2529 return FALSE;
2532 switch (dwParam)
2534 case HP_ALGID:
2535 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptHash->aiAlgid,
2536 sizeof(ALG_ID));
2538 case HP_HASHSIZE:
2539 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptHash->dwHashSize,
2540 sizeof(DWORD));
2542 case HP_HASHVAL:
2543 if (pCryptHash->aiAlgid == CALG_TLS1PRF) {
2544 return tls1_prf(hProv, pCryptHash->hKey, &pCryptHash->tpPRFParams.blobLabel,
2545 &pCryptHash->tpPRFParams.blobSeed, pbData, *pdwDataLen);
2548 if (pCryptHash->dwState == RSAENH_HASHSTATE_IDLE) {
2549 SetLastError(NTE_BAD_HASH_STATE);
2550 return FALSE;
2553 if (pbData && (pCryptHash->dwState != RSAENH_HASHSTATE_FINISHED))
2555 finalize_hash(pCryptHash);
2556 pCryptHash->dwState = RSAENH_HASHSTATE_FINISHED;
2559 return copy_param(pbData, pdwDataLen, (CONST BYTE*)pCryptHash->abHashValue,
2560 pCryptHash->dwHashSize);
2562 default:
2563 SetLastError(NTE_BAD_TYPE);
2564 return FALSE;
2568 /******************************************************************************
2569 * CPSetKeyParam (RSAENH.@)
2571 * Set a parameter of a key object
2573 * PARAMS
2574 * hProv [I] The key container to which the key belongs.
2575 * hKey [I] The key for which a parameter is to be set.
2576 * dwParam [I] Parameter type. See Notes.
2577 * pbData [I] Pointer to the parameter value.
2578 * dwFlags [I] Currently none defined.
2580 * RETURNS
2581 * Success: TRUE.
2582 * Failure: FALSE.
2584 * NOTES:
2585 * Defined dwParam types are:
2586 * - KP_MODE: Values MODE_CBC, MODE_ECB, MODE_CFB.
2587 * - KP_MODE_BITS: Shift width for cipher feedback mode. (Currently ignored by MS CSP's)
2588 * - KP_PERMISSIONS: Or'ed combination of CRYPT_ENCRYPT, CRYPT_DECRYPT,
2589 * CRYPT_EXPORT, CRYPT_READ, CRYPT_WRITE, CRYPT_MAC
2590 * - KP_IV: Initialization vector
2592 BOOL WINAPI RSAENH_CPSetKeyParam(HCRYPTPROV hProv, HCRYPTKEY hKey, DWORD dwParam, BYTE *pbData,
2593 DWORD dwFlags)
2595 CRYPTKEY *pCryptKey;
2597 TRACE("(hProv=%08lx, hKey=%08lx, dwParam=%08lx, pbData=%p, dwFlags=%08lx)\n", hProv, hKey,
2598 dwParam, pbData, dwFlags);
2600 if (!is_valid_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER))
2602 SetLastError(NTE_BAD_UID);
2603 return FALSE;
2606 if (dwFlags) {
2607 SetLastError(NTE_BAD_FLAGS);
2608 return FALSE;
2611 if (!lookup_handle(&handle_table, (unsigned int)hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
2613 SetLastError(NTE_BAD_KEY);
2614 return FALSE;
2617 switch (dwParam) {
2618 case KP_MODE:
2619 pCryptKey->dwMode = *(DWORD*)pbData;
2620 return TRUE;
2622 case KP_MODE_BITS:
2623 pCryptKey->dwModeBits = *(DWORD*)pbData;
2624 return TRUE;
2626 case KP_PERMISSIONS:
2627 pCryptKey->dwPermissions = *(DWORD*)pbData;
2628 return TRUE;
2630 case KP_IV:
2631 memcpy(pCryptKey->abInitVector, pbData, pCryptKey->dwBlockLen);
2632 return TRUE;
2634 case KP_SCHANNEL_ALG:
2635 switch (((PSCHANNEL_ALG)pbData)->dwUse) {
2636 case SCHANNEL_ENC_KEY:
2637 memcpy(&pCryptKey->siSChannelInfo.saEncAlg, pbData, sizeof(SCHANNEL_ALG));
2638 break;
2640 case SCHANNEL_MAC_KEY:
2641 memcpy(&pCryptKey->siSChannelInfo.saMACAlg, pbData, sizeof(SCHANNEL_ALG));
2642 break;
2644 default:
2645 SetLastError(NTE_FAIL); /* FIXME: error code */
2646 return FALSE;
2648 return TRUE;
2650 case KP_CLIENT_RANDOM:
2651 return copy_data_blob(&pCryptKey->siSChannelInfo.blobClientRandom, (PCRYPT_DATA_BLOB)pbData);
2653 case KP_SERVER_RANDOM:
2654 return copy_data_blob(&pCryptKey->siSChannelInfo.blobServerRandom, (PCRYPT_DATA_BLOB)pbData);
2656 default:
2657 SetLastError(NTE_BAD_TYPE);
2658 return FALSE;
2662 /******************************************************************************
2663 * CPGetKeyParam (RSAENH.@)
2665 * Query a key parameter.
2667 * PARAMS
2668 * hProv [I] The key container, which the key belongs to.
2669 * hHash [I] The key object that is to be queried.
2670 * dwParam [I] Specifies the parameter that is to be queried.
2671 * pbData [I] Pointer to the buffer where the parameter value will be stored.
2672 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
2673 * dwFlags [I] None currently defined.
2675 * RETURNS
2676 * Success: TRUE
2677 * Failure: FALSE
2679 * NOTES
2680 * Defined dwParam types are:
2681 * - KP_MODE: Values MODE_CBC, MODE_ECB, MODE_CFB.
2682 * - KP_MODE_BITS: Shift width for cipher feedback mode.
2683 * (Currently ignored by MS CSP's - always eight)
2684 * - KP_PERMISSIONS: Or'ed combination of CRYPT_ENCRYPT, CRYPT_DECRYPT,
2685 * CRYPT_EXPORT, CRYPT_READ, CRYPT_WRITE, CRYPT_MAC
2686 * - KP_IV: Initialization vector.
2687 * - KP_KEYLEN: Bitwidth of the key.
2688 * - KP_BLOCKLEN: Size of a block cipher block.
2689 * - KP_SALT: Salt value.
2691 BOOL WINAPI RSAENH_CPGetKeyParam(HCRYPTPROV hProv, HCRYPTKEY hKey, DWORD dwParam, BYTE *pbData,
2692 DWORD *pdwDataLen, DWORD dwFlags)
2694 CRYPTKEY *pCryptKey;
2695 DWORD dwBitLen;
2697 TRACE("(hProv=%08lx, hKey=%08lx, dwParam=%08lx, pbData=%p, pdwDataLen=%p dwFlags=%08lx)\n",
2698 hProv, hKey, dwParam, pbData, pdwDataLen, dwFlags);
2700 if (!is_valid_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER))
2702 SetLastError(NTE_BAD_UID);
2703 return FALSE;
2706 if (dwFlags) {
2707 SetLastError(NTE_BAD_FLAGS);
2708 return FALSE;
2711 if (!lookup_handle(&handle_table, (unsigned int)hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
2713 SetLastError(NTE_BAD_KEY);
2714 return FALSE;
2717 switch (dwParam)
2719 case KP_IV:
2720 return copy_param(pbData, pdwDataLen, (CONST BYTE*)pCryptKey->abInitVector,
2721 pCryptKey->dwBlockLen);
2723 case KP_SALT:
2724 return copy_param(pbData, pdwDataLen,
2725 (CONST BYTE*)&pCryptKey->abKeyValue[pCryptKey->dwKeyLen], pCryptKey->dwSaltLen);
2727 case KP_KEYLEN:
2728 dwBitLen = pCryptKey->dwKeyLen << 3;
2729 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwBitLen, sizeof(DWORD));
2731 case KP_BLOCKLEN:
2732 dwBitLen = pCryptKey->dwBlockLen << 3;
2733 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwBitLen, sizeof(DWORD));
2735 case KP_MODE:
2736 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptKey->dwMode, sizeof(DWORD));
2738 case KP_MODE_BITS:
2739 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptKey->dwModeBits,
2740 sizeof(DWORD));
2742 case KP_PERMISSIONS:
2743 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptKey->dwPermissions,
2744 sizeof(DWORD));
2746 case KP_ALGID:
2747 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptKey->aiAlgid, sizeof(DWORD));
2749 default:
2750 SetLastError(NTE_BAD_TYPE);
2751 return FALSE;
2755 /******************************************************************************
2756 * CPGetProvParam (RSAENH.@)
2758 * Query a CSP parameter.
2760 * PARAMS
2761 * hProv [I] The key container that is to be queried.
2762 * dwParam [I] Specifies the parameter that is to be queried.
2763 * pbData [I] Pointer to the buffer where the parameter value will be stored.
2764 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
2765 * dwFlags [I] CRYPT_FIRST: Start enumeration (for PP_ENUMALGS{_EX}).
2767 * RETURNS
2768 * Success: TRUE
2769 * Failure: FALSE
2770 * NOTES:
2771 * Defined dwParam types:
2772 * - PP_CONTAINER: Name of the key container.
2773 * - PP_NAME: Name of the cryptographic service provider.
2774 * - PP_SIG_KEYSIZE_INC: RSA signature keywidth granularity in bits.
2775 * - PP_KEYX_KEYSIZE_INC: RSA key-exchange keywidth granularity in bits.
2776 * - PP_ENUMALGS{_EX}: Query provider capabilities.
2778 BOOL WINAPI RSAENH_CPGetProvParam(HCRYPTPROV hProv, DWORD dwParam, BYTE *pbData,
2779 DWORD *pdwDataLen, DWORD dwFlags)
2781 KEYCONTAINER *pKeyContainer;
2782 PROV_ENUMALGS provEnumalgs;
2783 DWORD dwTemp;
2784 BYTE szRSABase[MAX_PATH];
2785 HKEY hKey, hRootKey;
2787 /* This is for dwParam 41, which does not seem to be documented
2788 * on MSDN. IE6 SP1 asks for it in the 'About' dialog, however.
2789 * Returning this BLOB seems to satisfy IE. The marked 0x00 seem
2790 * to be 'don't care's. If you know anything more specific about
2791 * provider parameter 41, please report to wine-devel@winehq.org */
2792 static CONST BYTE abWTF[96] = {
2793 0xb0, 0x25, 0x63, 0x86, 0x9c, 0xab, 0xb6, 0x37,
2794 0xe8, 0x82, /**/0x00,/**/ 0x72, 0x06, 0xb2, /**/0x00,/**/ 0x3b,
2795 0x60, 0x35, /**/0x00,/**/ 0x3b, 0x88, 0xce, /**/0x00,/**/ 0x82,
2796 0xbc, 0x7a, /**/0x00,/**/ 0xb7, 0x4f, 0x7e, /**/0x00,/**/ 0xde,
2797 0x92, 0xf1, /**/0x00,/**/ 0x83, 0xea, 0x5e, /**/0x00,/**/ 0xc8,
2798 0x12, 0x1e, 0xd4, 0x06, 0xf7, 0x66, /**/0x00,/**/ 0x01,
2799 0x29, 0xa4, /**/0x00,/**/ 0xf8, 0x24, 0x0c, /**/0x00,/**/ 0x33,
2800 0x06, 0x80, /**/0x00,/**/ 0x02, 0x46, 0x0b, /**/0x00,/**/ 0x6d,
2801 0x5b, 0xca, /**/0x00,/**/ 0x9a, 0x10, 0xf0, /**/0x00,/**/ 0x05,
2802 0x19, 0xd0, /**/0x00,/**/ 0x2c, 0xf6, 0x27, /**/0x00,/**/ 0xaa,
2803 0x7c, 0x6f, /**/0x00,/**/ 0xb9, 0xd8, 0x72, /**/0x00,/**/ 0x03,
2804 0xf3, 0x81, /**/0x00,/**/ 0xfa, 0xe8, 0x26, /**/0x00,/**/ 0xca
2807 TRACE("(hProv=%08lx, dwParam=%08lx, pbData=%p, pdwDataLen=%p, dwFlags=%08lx)\n",
2808 hProv, dwParam, pbData, pdwDataLen, dwFlags);
2810 if (!pdwDataLen) {
2811 SetLastError(ERROR_INVALID_PARAMETER);
2812 return FALSE;
2815 if (!lookup_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER,
2816 (OBJECTHDR**)&pKeyContainer))
2818 /* MSDN: hProv not containing valid context handle */
2819 SetLastError(NTE_BAD_UID);
2820 return FALSE;
2823 switch (dwParam)
2825 case PP_CONTAINER:
2826 return copy_param(pbData, pdwDataLen, (CONST BYTE*)pKeyContainer->szName,
2827 strlen(pKeyContainer->szName)+1);
2829 case PP_NAME:
2830 return copy_param(pbData, pdwDataLen, (CONST BYTE*)pKeyContainer->szProvName,
2831 strlen(pKeyContainer->szProvName)+1);
2833 case PP_SIG_KEYSIZE_INC:
2834 case PP_KEYX_KEYSIZE_INC:
2835 dwTemp = 8;
2836 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwTemp, sizeof(dwTemp));
2838 case PP_IMPTYPE:
2839 dwTemp = CRYPT_IMPL_SOFTWARE;
2840 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwTemp, sizeof(dwTemp));
2842 case PP_VERSION:
2843 dwTemp = 0x00000200;
2844 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwTemp, sizeof(dwTemp));
2846 case PP_ENUMCONTAINERS:
2847 if ((dwFlags & CRYPT_FIRST) == CRYPT_FIRST) pKeyContainer->dwEnumContainersCtr = 0;
2849 if (!pbData) {
2850 *pdwDataLen = (DWORD)MAX_PATH + 1;
2851 return TRUE;
2854 sprintf(szRSABase, RSAENH_REGKEY, "");
2856 if (dwFlags & CRYPT_MACHINE_KEYSET) {
2857 hRootKey = HKEY_LOCAL_MACHINE;
2858 } else {
2859 hRootKey = HKEY_CURRENT_USER;
2862 if (RegOpenKeyExA(hRootKey, szRSABase, 0, KEY_READ, &hKey) != ERROR_SUCCESS)
2864 SetLastError(ERROR_NO_MORE_ITEMS);
2865 return FALSE;
2868 dwTemp = *pdwDataLen;
2869 switch (RegEnumKeyExA(hKey, pKeyContainer->dwEnumContainersCtr, pbData, &dwTemp,
2870 NULL, NULL, NULL, NULL))
2872 case ERROR_MORE_DATA:
2873 *pdwDataLen = (DWORD)MAX_PATH + 1;
2875 case ERROR_SUCCESS:
2876 pKeyContainer->dwEnumContainersCtr++;
2877 RegCloseKey(hKey);
2878 return TRUE;
2880 case ERROR_NO_MORE_ITEMS:
2881 default:
2882 SetLastError(ERROR_NO_MORE_ITEMS);
2883 RegCloseKey(hKey);
2884 return FALSE;
2887 case PP_ENUMALGS:
2888 case PP_ENUMALGS_EX:
2889 if (((pKeyContainer->dwEnumAlgsCtr >= RSAENH_MAX_ENUMALGS-1) ||
2890 (!aProvEnumAlgsEx[pKeyContainer->dwPersonality]
2891 [pKeyContainer->dwEnumAlgsCtr+1].aiAlgid)) &&
2892 ((dwFlags & CRYPT_FIRST) != CRYPT_FIRST))
2894 SetLastError(ERROR_NO_MORE_ITEMS);
2895 return FALSE;
2898 if (dwParam == PP_ENUMALGS) {
2899 if (pbData && (*pdwDataLen >= sizeof(PROV_ENUMALGS)))
2900 pKeyContainer->dwEnumAlgsCtr = ((dwFlags & CRYPT_FIRST) == CRYPT_FIRST) ?
2901 0 : pKeyContainer->dwEnumAlgsCtr+1;
2903 provEnumalgs.aiAlgid = aProvEnumAlgsEx
2904 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr].aiAlgid;
2905 provEnumalgs.dwBitLen = aProvEnumAlgsEx
2906 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr].dwDefaultLen;
2907 provEnumalgs.dwNameLen = aProvEnumAlgsEx
2908 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr].dwNameLen;
2909 memcpy(provEnumalgs.szName, aProvEnumAlgsEx
2910 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr].szName,
2911 20*sizeof(CHAR));
2913 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&provEnumalgs,
2914 sizeof(PROV_ENUMALGS));
2915 } else {
2916 if (pbData && (*pdwDataLen >= sizeof(PROV_ENUMALGS_EX)))
2917 pKeyContainer->dwEnumAlgsCtr = ((dwFlags & CRYPT_FIRST) == CRYPT_FIRST) ?
2918 0 : pKeyContainer->dwEnumAlgsCtr+1;
2920 return copy_param(pbData, pdwDataLen,
2921 (CONST BYTE*)&aProvEnumAlgsEx
2922 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr],
2923 sizeof(PROV_ENUMALGS_EX));
2926 case 41: /* Undocumented. Asked for by IE About dialog */
2927 return copy_param(pbData, pdwDataLen, abWTF, sizeof(abWTF));
2929 default:
2930 /* MSDN: Unknown parameter number in dwParam */
2931 SetLastError(NTE_BAD_TYPE);
2932 return FALSE;
2936 /******************************************************************************
2937 * CPDeriveKey (RSAENH.@)
2939 * Derives a key from a hash value.
2941 * PARAMS
2942 * hProv [I] Key container for which a key is to be generated.
2943 * Algid [I] Crypto algorithm identifier for the key to be generated.
2944 * hBaseData [I] Hash from whose value the key will be derived.
2945 * dwFlags [I] See Notes.
2946 * phKey [O] The generated key.
2948 * RETURNS
2949 * Success: TRUE
2950 * Failure: FALSE
2952 * NOTES
2953 * Defined flags:
2954 * - CRYPT_EXPORTABLE: Key can be exported.
2955 * - CRYPT_NO_SALT: No salt is used for 40 bit keys.
2956 * - CRYPT_CREATE_SALT: Use remaining bits as salt value.
2958 BOOL WINAPI RSAENH_CPDeriveKey(HCRYPTPROV hProv, ALG_ID Algid, HCRYPTHASH hBaseData,
2959 DWORD dwFlags, HCRYPTKEY *phKey)
2961 CRYPTKEY *pCryptKey, *pMasterKey;
2962 CRYPTHASH *pCryptHash;
2963 BYTE abHashValue[RSAENH_MAX_HASH_SIZE*2];
2964 DWORD dwLen;
2966 TRACE("(hProv=%08lx, Algid=%d, hBaseData=%08lx, dwFlags=%08lx phKey=%p)\n", hProv, Algid,
2967 hBaseData, dwFlags, phKey);
2969 if (!is_valid_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER))
2971 SetLastError(NTE_BAD_UID);
2972 return FALSE;
2975 if (!lookup_handle(&handle_table, (unsigned int)hBaseData, RSAENH_MAGIC_HASH,
2976 (OBJECTHDR**)&pCryptHash))
2978 SetLastError(NTE_BAD_HASH);
2979 return FALSE;
2982 if (!phKey)
2984 SetLastError(ERROR_INVALID_PARAMETER);
2985 return FALSE;
2988 switch (GET_ALG_CLASS(Algid))
2990 case ALG_CLASS_DATA_ENCRYPT:
2991 *phKey = new_key(hProv, Algid, dwFlags, &pCryptKey);
2992 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) return FALSE;
2995 * We derive the key material from the hash.
2996 * If the hash value is not large enough for the claimed key, we have to construct
2997 * a larger binary value based on the hash. This is documented in MSDN: CryptDeriveKey.
2999 dwLen = RSAENH_MAX_HASH_SIZE;
3000 RSAENH_CPGetHashParam(pCryptHash->hProv, hBaseData, HP_HASHVAL, abHashValue, &dwLen, 0);
3002 if (dwLen < pCryptKey->dwKeyLen) {
3003 BYTE pad1[RSAENH_HMAC_DEF_PAD_LEN], pad2[RSAENH_HMAC_DEF_PAD_LEN];
3004 BYTE old_hashval[RSAENH_MAX_HASH_SIZE];
3005 DWORD i;
3007 memcpy(old_hashval, pCryptHash->abHashValue, RSAENH_MAX_HASH_SIZE);
3009 for (i=0; i<RSAENH_HMAC_DEF_PAD_LEN; i++) {
3010 pad1[i] = RSAENH_HMAC_DEF_IPAD_CHAR ^ (i<dwLen ? abHashValue[i] : 0);
3011 pad2[i] = RSAENH_HMAC_DEF_OPAD_CHAR ^ (i<dwLen ? abHashValue[i] : 0);
3014 init_hash(pCryptHash);
3015 update_hash(pCryptHash, pad1, RSAENH_HMAC_DEF_PAD_LEN);
3016 finalize_hash(pCryptHash);
3017 memcpy(abHashValue, pCryptHash->abHashValue, pCryptHash->dwHashSize);
3019 init_hash(pCryptHash);
3020 update_hash(pCryptHash, pad2, RSAENH_HMAC_DEF_PAD_LEN);
3021 finalize_hash(pCryptHash);
3022 memcpy(abHashValue+pCryptHash->dwHashSize, pCryptHash->abHashValue,
3023 pCryptHash->dwHashSize);
3025 memcpy(pCryptHash->abHashValue, old_hashval, RSAENH_MAX_HASH_SIZE);
3028 memcpy(pCryptKey->abKeyValue, abHashValue,
3029 RSAENH_MIN(pCryptKey->dwKeyLen, sizeof(pCryptKey->abKeyValue)));
3030 break;
3032 case ALG_CLASS_MSG_ENCRYPT:
3033 if (!lookup_handle(&handle_table, pCryptHash->hKey, RSAENH_MAGIC_KEY,
3034 (OBJECTHDR**)&pMasterKey))
3036 SetLastError(NTE_FAIL); /* FIXME error code */
3037 return FALSE;
3040 switch (Algid)
3042 /* See RFC 2246, chapter 6.3 Key calculation */
3043 case CALG_SCHANNEL_ENC_KEY:
3044 *phKey = new_key(hProv, pMasterKey->siSChannelInfo.saEncAlg.Algid,
3045 MAKELONG(LOWORD(dwFlags),pMasterKey->siSChannelInfo.saEncAlg.cBits),
3046 &pCryptKey);
3047 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) return FALSE;
3048 memcpy(pCryptKey->abKeyValue,
3049 pCryptHash->abHashValue + (
3050 2 * (pMasterKey->siSChannelInfo.saMACAlg.cBits / 8) +
3051 ((dwFlags & CRYPT_SERVER) ?
3052 (pMasterKey->siSChannelInfo.saEncAlg.cBits / 8) : 0)),
3053 pMasterKey->siSChannelInfo.saEncAlg.cBits / 8);
3054 memcpy(pCryptKey->abInitVector,
3055 pCryptHash->abHashValue + (
3056 2 * (pMasterKey->siSChannelInfo.saMACAlg.cBits / 8) +
3057 2 * (pMasterKey->siSChannelInfo.saEncAlg.cBits / 8) +
3058 ((dwFlags & CRYPT_SERVER) ? pCryptKey->dwBlockLen : 0)),
3059 pCryptKey->dwBlockLen);
3060 break;
3062 case CALG_SCHANNEL_MAC_KEY:
3063 *phKey = new_key(hProv, Algid,
3064 MAKELONG(LOWORD(dwFlags),pMasterKey->siSChannelInfo.saMACAlg.cBits),
3065 &pCryptKey);
3066 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) return FALSE;
3067 memcpy(pCryptKey->abKeyValue,
3068 pCryptHash->abHashValue + ((dwFlags & CRYPT_SERVER) ?
3069 pMasterKey->siSChannelInfo.saMACAlg.cBits / 8 : 0),
3070 pMasterKey->siSChannelInfo.saMACAlg.cBits / 8);
3071 break;
3073 default:
3074 SetLastError(NTE_BAD_ALGID);
3075 return FALSE;
3077 break;
3079 default:
3080 SetLastError(NTE_BAD_ALGID);
3081 return FALSE;
3084 setup_key(pCryptKey);
3085 return TRUE;
3088 /******************************************************************************
3089 * CPGetUserKey (RSAENH.@)
3091 * Returns a handle to the user's private key-exchange- or signature-key.
3093 * PARAMS
3094 * hProv [I] The key container from which a user key is requested.
3095 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
3096 * phUserKey [O] Handle to the requested key or INVALID_HANDLE_VALUE in case of failure.
3098 * RETURNS
3099 * Success: TRUE.
3100 * Failure: FALSE.
3102 * NOTE
3103 * A newly created key container does not contain private user key. Create them with CPGenKey.
3105 BOOL WINAPI RSAENH_CPGetUserKey(HCRYPTPROV hProv, DWORD dwKeySpec, HCRYPTKEY *phUserKey)
3107 KEYCONTAINER *pKeyContainer;
3109 TRACE("(hProv=%08lx, dwKeySpec=%08lx, phUserKey=%p)\n", hProv, dwKeySpec, phUserKey);
3111 if (!lookup_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER,
3112 (OBJECTHDR**)&pKeyContainer))
3114 /* MSDN: hProv not containing valid context handle */
3115 SetLastError(NTE_BAD_UID);
3116 return FALSE;
3119 switch (dwKeySpec)
3121 case AT_KEYEXCHANGE:
3122 copy_handle(&handle_table, pKeyContainer->hKeyExchangeKeyPair, RSAENH_MAGIC_KEY,
3123 (unsigned int*)phUserKey);
3124 break;
3126 case AT_SIGNATURE:
3127 copy_handle(&handle_table, pKeyContainer->hSignatureKeyPair, RSAENH_MAGIC_KEY,
3128 (unsigned int*)phUserKey);
3129 break;
3131 default:
3132 *phUserKey = (HCRYPTKEY)INVALID_HANDLE_VALUE;
3135 if (*phUserKey == (HCRYPTKEY)INVALID_HANDLE_VALUE)
3137 /* MSDN: dwKeySpec parameter specifies nonexistent key */
3138 SetLastError(NTE_NO_KEY);
3139 return FALSE;
3142 return TRUE;
3145 /******************************************************************************
3146 * CPHashData (RSAENH.@)
3148 * Updates a hash object with the given data.
3150 * PARAMS
3151 * hProv [I] Key container to which the hash object belongs.
3152 * hHash [I] Hash object which is to be updated.
3153 * pbData [I] Pointer to data with which the hash object is to be updated.
3154 * dwDataLen [I] Length of the data.
3155 * dwFlags [I] Currently none defined.
3157 * RETURNS
3158 * Success: TRUE.
3159 * Failure: FALSE.
3161 * NOTES
3162 * The actual hash value is queried with CPGetHashParam, which will finalize
3163 * the hash. Updating a finalized hash will fail with a last error NTE_BAD_HASH_STATE.
3165 BOOL WINAPI RSAENH_CPHashData(HCRYPTPROV hProv, HCRYPTHASH hHash, CONST BYTE *pbData,
3166 DWORD dwDataLen, DWORD dwFlags)
3168 CRYPTHASH *pCryptHash;
3170 TRACE("(hProv=%08lx, hHash=%08lx, pbData=%p, dwDataLen=%ld, dwFlags=%08lx)\n",
3171 hProv, hHash, pbData, dwDataLen, dwFlags);
3173 if (dwFlags)
3175 SetLastError(NTE_BAD_FLAGS);
3176 return FALSE;
3179 if (!lookup_handle(&handle_table, (unsigned int)hHash, RSAENH_MAGIC_HASH,
3180 (OBJECTHDR**)&pCryptHash))
3182 SetLastError(NTE_BAD_HASH);
3183 return FALSE;
3186 if (!get_algid_info(hProv, pCryptHash->aiAlgid) || pCryptHash->aiAlgid == CALG_SSL3_SHAMD5)
3188 SetLastError(NTE_BAD_ALGID);
3189 return FALSE;
3192 if (pCryptHash->dwState == RSAENH_HASHSTATE_IDLE)
3193 pCryptHash->dwState = RSAENH_HASHSTATE_HASHING;
3195 if (pCryptHash->dwState != RSAENH_HASHSTATE_HASHING)
3197 SetLastError(NTE_BAD_HASH_STATE);
3198 return FALSE;
3201 update_hash(pCryptHash, pbData, dwDataLen);
3202 return TRUE;
3205 /******************************************************************************
3206 * CPHashSessionKey (RSAENH.@)
3208 * Updates a hash object with the binary representation of a symmetric key.
3210 * PARAMS
3211 * hProv [I] Key container to which the hash object belongs.
3212 * hHash [I] Hash object which is to be updated.
3213 * hKey [I] The symmetric key, whose binary value will be added to the hash.
3214 * dwFlags [I] CRYPT_LITTLE_ENDIAN, if the binary key value shall be interpreted as little endian.
3216 * RETURNS
3217 * Success: TRUE.
3218 * Failure: FALSE.
3220 BOOL WINAPI RSAENH_CPHashSessionKey(HCRYPTPROV hProv, HCRYPTHASH hHash, HCRYPTKEY hKey,
3221 DWORD dwFlags)
3223 BYTE abKeyValue[RSAENH_MAX_KEY_SIZE], bTemp;
3224 CRYPTKEY *pKey;
3225 DWORD i;
3227 TRACE("(hProv=%08lx, hHash=%08lx, hKey=%08lx, dwFlags=%08lx)\n", hProv, hHash, hKey, dwFlags);
3229 if (!lookup_handle(&handle_table, (unsigned int)hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pKey) ||
3230 (GET_ALG_CLASS(pKey->aiAlgid) != ALG_CLASS_DATA_ENCRYPT))
3232 SetLastError(NTE_BAD_KEY);
3233 return FALSE;
3236 if (dwFlags & ~CRYPT_LITTLE_ENDIAN) {
3237 SetLastError(NTE_BAD_FLAGS);
3238 return FALSE;
3241 memcpy(abKeyValue, pKey->abKeyValue, pKey->dwKeyLen);
3242 if (!(dwFlags & CRYPT_LITTLE_ENDIAN)) {
3243 for (i=0; i<pKey->dwKeyLen/2; i++) {
3244 bTemp = abKeyValue[i];
3245 abKeyValue[i] = abKeyValue[pKey->dwKeyLen-i-1];
3246 abKeyValue[pKey->dwKeyLen-i-1] = bTemp;
3250 return RSAENH_CPHashData(hProv, hHash, abKeyValue, pKey->dwKeyLen, 0);
3253 /******************************************************************************
3254 * CPReleaseContext (RSAENH.@)
3256 * Release a key container.
3258 * PARAMS
3259 * hProv [I] Key container to be released.
3260 * dwFlags [I] Currently none defined.
3262 * RETURNS
3263 * Success: TRUE
3264 * Failure: FALSE
3266 BOOL WINAPI RSAENH_CPReleaseContext(HCRYPTPROV hProv, DWORD dwFlags)
3268 TRACE("(hProv=%08lx, dwFlags=%08lx)\n", hProv, dwFlags);
3270 if (!release_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER))
3272 /* MSDN: hProv not containing valid context handle */
3273 SetLastError(NTE_BAD_UID);
3274 return FALSE;
3277 if (dwFlags) {
3278 SetLastError(NTE_BAD_FLAGS);
3279 return FALSE;
3282 return TRUE;
3285 /******************************************************************************
3286 * CPSetHashParam (RSAENH.@)
3288 * Set a parameter of a hash object
3290 * PARAMS
3291 * hProv [I] The key container to which the key belongs.
3292 * hHash [I] The hash object for which a parameter is to be set.
3293 * dwParam [I] Parameter type. See Notes.
3294 * pbData [I] Pointer to the parameter value.
3295 * dwFlags [I] Currently none defined.
3297 * RETURNS
3298 * Success: TRUE.
3299 * Failure: FALSE.
3301 * NOTES
3302 * Currently only the HP_HMAC_INFO dwParam type is defined.
3303 * The HMAC_INFO struct will be deep copied into the hash object.
3304 * See Internet RFC 2104 for details on the HMAC algorithm.
3306 BOOL WINAPI RSAENH_CPSetHashParam(HCRYPTPROV hProv, HCRYPTHASH hHash, DWORD dwParam,
3307 BYTE *pbData, DWORD dwFlags)
3309 CRYPTHASH *pCryptHash;
3310 CRYPTKEY *pCryptKey;
3311 int i;
3313 TRACE("(hProv=%08lx, hHash=%08lx, dwParam=%08lx, pbData=%p, dwFlags=%08lx)\n",
3314 hProv, hHash, dwParam, pbData, dwFlags);
3316 if (!is_valid_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER))
3318 SetLastError(NTE_BAD_UID);
3319 return FALSE;
3322 if (dwFlags) {
3323 SetLastError(NTE_BAD_FLAGS);
3324 return FALSE;
3327 if (!lookup_handle(&handle_table, (unsigned int)hHash, RSAENH_MAGIC_HASH,
3328 (OBJECTHDR**)&pCryptHash))
3330 SetLastError(NTE_BAD_HASH);
3331 return FALSE;
3334 switch (dwParam) {
3335 case HP_HMAC_INFO:
3336 free_hmac_info(pCryptHash->pHMACInfo);
3337 if (!copy_hmac_info(&pCryptHash->pHMACInfo, (PHMAC_INFO)pbData)) return FALSE;
3339 if (!lookup_handle(&handle_table, pCryptHash->hKey, RSAENH_MAGIC_KEY,
3340 (OBJECTHDR**)&pCryptKey))
3342 SetLastError(NTE_FAIL); /* FIXME: correct error code? */
3343 return FALSE;
3346 for (i=0; i<RSAENH_MIN(pCryptKey->dwKeyLen,pCryptHash->pHMACInfo->cbInnerString); i++) {
3347 pCryptHash->pHMACInfo->pbInnerString[i] ^= pCryptKey->abKeyValue[i];
3349 for (i=0; i<RSAENH_MIN(pCryptKey->dwKeyLen,pCryptHash->pHMACInfo->cbOuterString); i++) {
3350 pCryptHash->pHMACInfo->pbOuterString[i] ^= pCryptKey->abKeyValue[i];
3353 init_hash(pCryptHash);
3354 return TRUE;
3356 case HP_HASHVAL:
3357 memcpy(pCryptHash->abHashValue, pbData, pCryptHash->dwHashSize);
3358 pCryptHash->dwState = RSAENH_HASHSTATE_FINISHED;
3359 return TRUE;
3361 case HP_TLS1PRF_SEED:
3362 return copy_data_blob(&pCryptHash->tpPRFParams.blobSeed, (PCRYPT_DATA_BLOB)pbData);
3364 case HP_TLS1PRF_LABEL:
3365 return copy_data_blob(&pCryptHash->tpPRFParams.blobLabel, (PCRYPT_DATA_BLOB)pbData);
3367 default:
3368 SetLastError(NTE_BAD_TYPE);
3369 return FALSE;
3373 /******************************************************************************
3374 * CPSetProvParam (RSAENH.@)
3376 BOOL WINAPI RSAENH_CPSetProvParam(HCRYPTPROV hProv, DWORD dwParam, BYTE *pbData, DWORD dwFlags)
3378 FIXME("(stub)\n");
3379 return FALSE;
3382 /******************************************************************************
3383 * CPSignHash (RSAENH.@)
3385 * Sign a hash object
3387 * PARAMS
3388 * hProv [I] The key container, to which the hash object belongs.
3389 * hHash [I] The hash object to be signed.
3390 * dwKeySpec [I] AT_SIGNATURE or AT_KEYEXCHANGE: Key used to generate the signature.
3391 * sDescription [I] Should be NULL for security reasons.
3392 * dwFlags [I] 0, CRYPT_NOHASHOID or CRYPT_X931_FORMAT: Format of the signature.
3393 * pbSignature [O] Buffer, to which the signature will be stored. May be NULL to query SigLen.
3394 * pdwSigLen [I/O] Size of the buffer (in), Length of the signature (out)
3396 * RETURNS
3397 * Success: TRUE
3398 * Failure: FALSE
3400 BOOL WINAPI RSAENH_CPSignHash(HCRYPTPROV hProv, HCRYPTHASH hHash, DWORD dwKeySpec,
3401 LPCWSTR sDescription, DWORD dwFlags, BYTE *pbSignature,
3402 DWORD *pdwSigLen)
3404 HCRYPTKEY hCryptKey;
3405 CRYPTKEY *pCryptKey;
3406 DWORD dwHashLen;
3407 BYTE abHashValue[RSAENH_MAX_HASH_SIZE];
3408 ALG_ID aiAlgid;
3410 TRACE("(hProv=%08lx, hHash=%08lx, dwKeySpec=%08lx, sDescription=%s, dwFlags=%08lx, "
3411 "pbSignature=%p, pdwSigLen=%p)\n", hProv, hHash, dwKeySpec, debugstr_w(sDescription),
3412 dwFlags, pbSignature, pdwSigLen);
3414 if (dwFlags & ~(CRYPT_NOHASHOID|CRYPT_X931_FORMAT)) {
3415 SetLastError(NTE_BAD_FLAGS);
3416 return FALSE;
3419 if (!RSAENH_CPGetUserKey(hProv, dwKeySpec, &hCryptKey)) return FALSE;
3421 if (!lookup_handle(&handle_table, (unsigned int)hCryptKey, RSAENH_MAGIC_KEY,
3422 (OBJECTHDR**)&pCryptKey))
3424 SetLastError(NTE_NO_KEY);
3425 return FALSE;
3428 if (!pbSignature) {
3429 *pdwSigLen = pCryptKey->dwKeyLen;
3430 return TRUE;
3432 if (pCryptKey->dwKeyLen > *pdwSigLen)
3434 SetLastError(ERROR_MORE_DATA);
3435 *pdwSigLen = pCryptKey->dwKeyLen;
3436 return FALSE;
3438 *pdwSigLen = pCryptKey->dwKeyLen;
3440 if (sDescription) {
3441 if (!RSAENH_CPHashData(hProv, hHash, (CONST BYTE*)sDescription,
3442 (DWORD)lstrlenW(sDescription)*sizeof(WCHAR), 0))
3444 return FALSE;
3448 dwHashLen = sizeof(DWORD);
3449 if (!RSAENH_CPGetHashParam(hProv, hHash, HP_ALGID, (BYTE*)&aiAlgid, &dwHashLen, 0)) return FALSE;
3451 dwHashLen = RSAENH_MAX_HASH_SIZE;
3452 if (!RSAENH_CPGetHashParam(hProv, hHash, HP_HASHVAL, abHashValue, &dwHashLen, 0)) return FALSE;
3455 if (!build_hash_signature(pbSignature, *pdwSigLen, aiAlgid, abHashValue, dwHashLen, dwFlags)) {
3456 return FALSE;
3459 return encrypt_block_impl(pCryptKey->aiAlgid, &pCryptKey->context, pbSignature, pbSignature, RSAENH_ENCRYPT);
3462 /******************************************************************************
3463 * CPVerifySignature (RSAENH.@)
3465 * Verify the signature of a hash object.
3467 * PARAMS
3468 * hProv [I] The key container, to which the hash belongs.
3469 * hHash [I] The hash for which the signature is verified.
3470 * pbSignature [I] The binary signature.
3471 * dwSigLen [I] Length of the signature BLOB.
3472 * hPubKey [I] Public key used to verify the signature.
3473 * sDescription [I] Should be NULL for security reasons.
3474 * dwFlags [I] 0, CRYPT_NOHASHOID or CRYPT_X931_FORMAT: Format of the signature.
3476 * RETURNS
3477 * Success: TRUE (Signature is valid)
3478 * Failure: FALSE (GetLastError() == NTE_BAD_SIGNATURE, if signature is invalid)
3480 BOOL WINAPI RSAENH_CPVerifySignature(HCRYPTPROV hProv, HCRYPTHASH hHash, CONST BYTE *pbSignature,
3481 DWORD dwSigLen, HCRYPTKEY hPubKey, LPCWSTR sDescription,
3482 DWORD dwFlags)
3484 BYTE *pbConstructed = NULL, *pbDecrypted = NULL;
3485 CRYPTKEY *pCryptKey;
3486 DWORD dwHashLen;
3487 ALG_ID aiAlgid;
3488 BYTE abHashValue[RSAENH_MAX_HASH_SIZE];
3489 BOOL res = FALSE;
3491 TRACE("(hProv=%08lx, hHash=%08lx, pbSignature=%p, dwSigLen=%ld, hPubKey=%08lx, sDescription=%s, "
3492 "dwFlags=%08lx)\n", hProv, hHash, pbSignature, dwSigLen, hPubKey, debugstr_w(sDescription),
3493 dwFlags);
3495 if (dwFlags & ~(CRYPT_NOHASHOID|CRYPT_X931_FORMAT)) {
3496 SetLastError(NTE_BAD_FLAGS);
3497 return FALSE;
3500 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
3502 SetLastError(NTE_BAD_UID);
3503 return FALSE;
3506 if (!lookup_handle(&handle_table, (unsigned int)hPubKey, RSAENH_MAGIC_KEY,
3507 (OBJECTHDR**)&pCryptKey))
3509 SetLastError(NTE_BAD_KEY);
3510 return FALSE;
3513 if (sDescription) {
3514 if (!RSAENH_CPHashData(hProv, hHash, (CONST BYTE*)sDescription,
3515 (DWORD)lstrlenW(sDescription)*sizeof(WCHAR), 0))
3517 return FALSE;
3521 dwHashLen = sizeof(DWORD);
3522 if (!RSAENH_CPGetHashParam(hProv, hHash, HP_ALGID, (BYTE*)&aiAlgid, &dwHashLen, 0)) return FALSE;
3524 dwHashLen = RSAENH_MAX_HASH_SIZE;
3525 if (!RSAENH_CPGetHashParam(hProv, hHash, HP_HASHVAL, abHashValue, &dwHashLen, 0)) return FALSE;
3527 pbConstructed = HeapAlloc(GetProcessHeap(), 0, dwSigLen);
3528 if (!pbConstructed) {
3529 SetLastError(NTE_NO_MEMORY);
3530 goto cleanup;
3533 pbDecrypted = HeapAlloc(GetProcessHeap(), 0, dwSigLen);
3534 if (!pbDecrypted) {
3535 SetLastError(NTE_NO_MEMORY);
3536 goto cleanup;
3539 if (!encrypt_block_impl(pCryptKey->aiAlgid, &pCryptKey->context, pbSignature, pbDecrypted,
3540 RSAENH_DECRYPT))
3542 goto cleanup;
3545 if (!build_hash_signature(pbConstructed, dwSigLen, aiAlgid, abHashValue, dwHashLen, dwFlags)) {
3546 goto cleanup;
3549 if (memcmp(pbDecrypted, pbConstructed, dwSigLen)) {
3550 SetLastError(NTE_BAD_SIGNATURE);
3551 goto cleanup;
3554 res = TRUE;
3555 cleanup:
3556 HeapFree(GetProcessHeap(), 0, pbConstructed);
3557 HeapFree(GetProcessHeap(), 0, pbDecrypted);
3558 return res;
3561 static const WCHAR szProviderKeys[4][97] = {
3562 { 'S','o','f','t','w','a','r','e','\\',
3563 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
3564 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
3565 'i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ','B','a','s',
3566 'e',' ','C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r',
3567 'o','v','i','d','e','r',' ','v','1','.','0',0 },
3568 { 'S','o','f','t','w','a','r','e','\\',
3569 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
3570 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
3571 'i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ',
3572 'E','n','h','a','n','c','e','d',
3573 ' ','C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r',
3574 'o','v','i','d','e','r',' ','v','1','.','0',0 },
3575 { 'S','o','f','t','w','a','r','e','\\',
3576 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
3577 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
3578 'i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ','S','t','r','o','n','g',
3579 ' ','C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r',
3580 'o','v','i','d','e','r',0 },
3581 { 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
3582 'C','r','y','p','t','o','g','r','a','p','h','y','\\','D','e','f','a','u','l','t','s','\\',
3583 'P','r','o','v','i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ',
3584 'R','S','A',' ','S','C','h','a','n','n','e','l',' ',
3585 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r','o','v','i','d','e','r',0 }
3587 static const WCHAR szDefaultKeys[2][65] = {
3588 { 'S','o','f','t','w','a','r','e','\\',
3589 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
3590 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
3591 'i','d','e','r',' ','T','y','p','e','s','\\','T','y','p','e',' ','0','0','1',0 },
3592 { 'S','o','f','t','w','a','r','e','\\',
3593 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
3594 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
3595 'i','d','e','r',' ','T','y','p','e','s','\\','T','y','p','e',' ','0','1','2',0 }
3599 /******************************************************************************
3600 * DllRegisterServer (RSAENH.@)
3602 * Dll self registration.
3604 * PARAMS
3606 * RETURNS
3607 * Success: S_OK.
3608 * Failure: != S_OK
3610 * NOTES
3611 * Registers the following keys:
3612 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider\
3613 * Microsoft Base Cryptographic Provider v1.0
3614 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider\
3615 * Microsoft Enhanced Cryptographic Provider
3616 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider\
3617 * Microsoft Strong Cryptographpic Provider
3618 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider Types\Type 001
3620 HRESULT WINAPI DllRegisterServer()
3622 HKEY key;
3623 DWORD dp;
3624 long apiRet;
3625 int i;
3627 for (i=0; i<4; i++) {
3628 apiRet = RegCreateKeyExW(HKEY_LOCAL_MACHINE, szProviderKeys[i], 0, NULL,
3629 REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &key, &dp);
3631 if (apiRet == ERROR_SUCCESS)
3633 if (dp == REG_CREATED_NEW_KEY)
3635 static const WCHAR szImagePath[] = { 'I','m','a','g','e',' ','P','a','t','h',0 };
3636 static const WCHAR szRSABase[] = { 'r','s','a','e','n','h','.','d','l','l',0 };
3637 static const WCHAR szType[] = { 'T','y','p','e',0 };
3638 static const WCHAR szSignature[] = { 'S','i','g','n','a','t','u','r','e',0 };
3639 DWORD type = (i == 3) ? PROV_RSA_SCHANNEL : PROV_RSA_FULL;
3640 DWORD sign = 0xdeadbeef;
3641 RegSetValueExW(key, szImagePath, 0, REG_SZ, (LPBYTE)szRSABase,
3642 (lstrlenW(szRSABase) + 1) * sizeof(WCHAR));
3643 RegSetValueExW(key, szType, 0, REG_DWORD, (LPBYTE)&type, sizeof(type));
3644 RegSetValueExW(key, szSignature, 0, REG_BINARY, (LPBYTE)&sign, sizeof(sign));
3646 RegCloseKey(key);
3650 for (i=0; i<2; i++) {
3651 apiRet = RegCreateKeyExW(HKEY_LOCAL_MACHINE, szDefaultKeys[i], 0, NULL,
3652 REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &key, &dp);
3653 if (apiRet == ERROR_SUCCESS)
3655 if (dp == REG_CREATED_NEW_KEY)
3657 static const WCHAR szName[] = { 'N','a','m','e',0 };
3658 static const WCHAR szRSAName[2][46] = {
3659 { 'M','i','c','r','o','s','o','f','t',' ', 'B','a','s','e',' ',
3660 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ',
3661 'P','r','o','v','i','d','e','r',' ','v','1','.','0',0 },
3662 { 'M','i','c','r','o','s','o','f','t',' ','R','S','A',' ',
3663 'S','C','h','a','n','n','e','l',' ',
3664 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ',
3665 'P','r','o','v','i','d','e','r',0 } };
3666 static const WCHAR szTypeName[] = { 'T','y','p','e','N','a','m','e',0 };
3667 static const WCHAR szRSATypeName[2][38] = {
3668 { 'R','S','A',' ','F','u','l','l',' ',
3669 '(','S','i','g','n','a','t','u','r','e',' ','a','n','d',' ',
3670 'K','e','y',' ','E','x','c','h','a','n','g','e',')',0 },
3671 { 'R','S','A',' ','S','C','h','a','n','n','e','l',0 } };
3673 RegSetValueExW(key, szName, 0, REG_SZ, (LPBYTE)szRSAName[i], sizeof(szRSAName));
3674 RegSetValueExW(key, szTypeName, 0, REG_SZ,
3675 (LPBYTE)szRSATypeName[i],sizeof(szRSATypeName));
3678 RegCloseKey(key);
3681 return HRESULT_FROM_WIN32(apiRet);
3684 /******************************************************************************
3685 * DllUnregisterServer (RSAENH.@)
3687 * Dll self unregistration.
3689 * PARAMS
3691 * RETURNS
3692 * Success: S_OK
3694 * NOTES
3695 * For the relevant keys see DllRegisterServer.
3697 HRESULT WINAPI DllUnregisterServer()
3699 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szProviderKeys[0]);
3700 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szProviderKeys[1]);
3701 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szProviderKeys[2]);
3702 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szProviderKeys[3]);
3703 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szDefaultKeys[0]);
3704 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szDefaultKeys[1]);
3705 return S_OK;