Create a branch for header work.
[reactos.git] / dll / win32 / rsaenh / rsaenh.c
1 /*
2 * dlls/rsaenh/rsaenh.c
3 * RSAENH - RSA encryption for Wine
4 *
5 * Copyright 2002 TransGaming Technologies (David Hammerton)
6 * Copyright 2004 Mike McCormack for CodeWeavers
7 * Copyright 2004, 2005 Michael Jung
8 * Copyright 2007 Vijay Kiran Kamuju
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25 #include "config.h"
26 #include "wine/port.h"
27 #include "wine/library.h"
28 #include "wine/debug.h"
29
30 #include <stdarg.h>
31 #include <stdio.h>
32
33 #include "windef.h"
34 #include "winbase.h"
35 #include "winreg.h"
36 #include "wincrypt.h"
37 #include "handle.h"
38 #include "implglue.h"
39 #include "objbase.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(crypt);
42
43 /******************************************************************************
44 * CRYPTHASH - hash objects
45 */
46 #define RSAENH_MAGIC_HASH 0x85938417u
47 #define RSAENH_MAX_HASH_SIZE 104
48 #define RSAENH_HASHSTATE_HASHING 1
49 #define RSAENH_HASHSTATE_FINISHED 2
50 typedef struct _RSAENH_TLS1PRF_PARAMS
51 {
52 CRYPT_DATA_BLOB blobLabel;
53 CRYPT_DATA_BLOB blobSeed;
54 } RSAENH_TLS1PRF_PARAMS;
55
56 typedef struct tagCRYPTHASH
57 {
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;
69
70 /******************************************************************************
71 * CRYPTKEY - key objects
72 */
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_MASTERKEY 2
79 typedef struct _RSAENH_SCHANNEL_INFO
80 {
81 SCHANNEL_ALG saEncAlg;
82 SCHANNEL_ALG saMACAlg;
83 CRYPT_DATA_BLOB blobClientRandom;
84 CRYPT_DATA_BLOB blobServerRandom;
85 } RSAENH_SCHANNEL_INFO;
86
87 typedef struct tagCRYPTKEY
88 {
89 OBJECTHDR header;
90 ALG_ID aiAlgid;
91 HCRYPTPROV hProv;
92 DWORD dwMode;
93 DWORD dwModeBits;
94 DWORD dwPermissions;
95 DWORD dwKeyLen;
96 DWORD dwEffectiveKeyLen;
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;
106
107 /******************************************************************************
108 * KEYCONTAINER - key containers
109 */
110 #define RSAENH_PERSONALITY_BASE 0u
111 #define RSAENH_PERSONALITY_STRONG 1u
112 #define RSAENH_PERSONALITY_ENHANCED 2u
113 #define RSAENH_PERSONALITY_SCHANNEL 3u
114 #define RSAENH_PERSONALITY_AES 4u
115
116 #define RSAENH_MAGIC_CONTAINER 0x26384993u
117 typedef struct tagKEYCONTAINER
118 {
119 OBJECTHDR header;
120 DWORD dwFlags;
121 DWORD dwPersonality;
122 DWORD dwEnumAlgsCtr;
123 DWORD dwEnumContainersCtr;
124 CHAR szName[MAX_PATH];
125 CHAR szProvName[MAX_PATH];
126 HCRYPTKEY hKeyExchangeKeyPair;
127 HCRYPTKEY hSignatureKeyPair;
128 } KEYCONTAINER;
129
130 /******************************************************************************
131 * Some magic constants
132 */
133 #define RSAENH_ENCRYPT 1
134 #define RSAENH_DECRYPT 0
135 #define RSAENH_HMAC_DEF_IPAD_CHAR 0x36
136 #define RSAENH_HMAC_DEF_OPAD_CHAR 0x5c
137 #define RSAENH_HMAC_DEF_PAD_LEN 64
138 #define RSAENH_DES_EFFECTIVE_KEYLEN 56
139 #define RSAENH_DES_STORAGE_KEYLEN 64
140 #define RSAENH_3DES112_EFFECTIVE_KEYLEN 112
141 #define RSAENH_3DES112_STORAGE_KEYLEN 128
142 #define RSAENH_3DES_EFFECTIVE_KEYLEN 168
143 #define RSAENH_3DES_STORAGE_KEYLEN 192
144 #define RSAENH_MAGIC_RSA2 0x32415352
145 #define RSAENH_MAGIC_RSA1 0x31415352
146 #define RSAENH_PKC_BLOCKTYPE 0x02
147 #define RSAENH_SSL3_VERSION_MAJOR 3
148 #define RSAENH_SSL3_VERSION_MINOR 0
149 #define RSAENH_TLS1_VERSION_MAJOR 3
150 #define RSAENH_TLS1_VERSION_MINOR 1
151 #define RSAENH_REGKEY "Software\\Wine\\Crypto\\RSA\\%s"
152
153 #define RSAENH_MIN(a,b) ((a)<(b)?(a):(b))
154 /******************************************************************************
155 * aProvEnumAlgsEx - Defines the capabilities of the CSP personalities.
156 */
157 #define RSAENH_MAX_ENUMALGS 24
158 #define RSAENH_PCT1_SSL2_SSL3_TLS1 (CRYPT_FLAG_PCT1|CRYPT_FLAG_SSL2|CRYPT_FLAG_SSL3|CRYPT_FLAG_TLS1)
159 static const PROV_ENUMALGS_EX aProvEnumAlgsEx[5][RSAENH_MAX_ENUMALGS+1] =
160 {
161 {
162 {CALG_RC2, 40, 40, 56,0, 4,"RC2", 24,"RSA Data Security's RC2"},
163 {CALG_RC4, 40, 40, 56,0, 4,"RC4", 24,"RSA Data Security's RC4"},
164 {CALG_DES, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
165 {CALG_SHA, 160,160, 160,CRYPT_FLAG_SIGNING, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
166 {CALG_MD2, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD2", 23,"Message Digest 2 (MD2)"},
167 {CALG_MD4, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD4", 23,"Message Digest 4 (MD4)"},
168 {CALG_MD5, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD5", 23,"Message Digest 5 (MD5)"},
169 {CALG_SSL3_SHAMD5,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
170 {CALG_MAC, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
171 {CALG_RSA_SIGN, 512,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_SIGN",14,"RSA Signature"},
172 {CALG_RSA_KEYX, 512,384, 1024,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_KEYX",17,"RSA Key Exchange"},
173 {CALG_HMAC, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
174 {0, 0, 0, 0,0, 1,"", 1,""}
175 },
176 {
177 {CALG_RC2, 128, 40, 128,0, 4,"RC2", 24,"RSA Data Security's RC2"},
178 {CALG_RC4, 128, 40, 128,0, 4,"RC4", 24,"RSA Data Security's RC4"},
179 {CALG_DES, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
180 {CALG_3DES_112, 112,112, 112,0, 13,"3DES TWO KEY",19,"Two Key Triple DES"},
181 {CALG_3DES, 168,168, 168,0, 5,"3DES", 21,"Three Key Triple DES"},
182 {CALG_SHA, 160,160, 160,CRYPT_FLAG_SIGNING, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
183 {CALG_MD2, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD2", 23,"Message Digest 2 (MD2)"},
184 {CALG_MD4, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD4", 23,"Message Digest 4 (MD4)"},
185 {CALG_MD5, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD5", 23,"Message Digest 5 (MD5)"},
186 {CALG_SSL3_SHAMD5,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
187 {CALG_MAC, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
188 {CALG_RSA_SIGN,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_SIGN",14,"RSA Signature"},
189 {CALG_RSA_KEYX,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_KEYX",17,"RSA Key Exchange"},
190 {CALG_HMAC, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
191 {0, 0, 0, 0,0, 1,"", 1,""}
192 },
193 {
194 {CALG_RC2, 128, 40, 128,0, 4,"RC2", 24,"RSA Data Security's RC2"},
195 {CALG_RC4, 128, 40, 128,0, 4,"RC4", 24,"RSA Data Security's RC4"},
196 {CALG_DES, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
197 {CALG_3DES_112, 112,112, 112,0, 13,"3DES TWO KEY",19,"Two Key Triple DES"},
198 {CALG_3DES, 168,168, 168,0, 5,"3DES", 21,"Three Key Triple DES"},
199 {CALG_SHA, 160,160, 160,CRYPT_FLAG_SIGNING, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
200 {CALG_MD2, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD2", 23,"Message Digest 2 (MD2)"},
201 {CALG_MD4, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD4", 23,"Message Digest 4 (MD4)"},
202 {CALG_MD5, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD5", 23,"Message Digest 5 (MD5)"},
203 {CALG_SSL3_SHAMD5,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
204 {CALG_MAC, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
205 {CALG_RSA_SIGN,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_SIGN",14,"RSA Signature"},
206 {CALG_RSA_KEYX,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_KEYX",17,"RSA Key Exchange"},
207 {CALG_HMAC, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
208 {0, 0, 0, 0,0, 1,"", 1,""}
209 },
210 {
211 {CALG_RC2, 128, 40, 128,RSAENH_PCT1_SSL2_SSL3_TLS1, 4,"RC2", 24,"RSA Data Security's RC2"},
212 {CALG_RC4, 128, 40, 128,RSAENH_PCT1_SSL2_SSL3_TLS1, 4,"RC4", 24,"RSA Data Security's RC4"},
213 {CALG_DES, 56, 56, 56,RSAENH_PCT1_SSL2_SSL3_TLS1, 4,"DES", 31,"Data Encryption Standard (DES)"},
214 {CALG_3DES_112, 112,112, 112,RSAENH_PCT1_SSL2_SSL3_TLS1,13,"3DES TWO KEY",19,"Two Key Triple DES"},
215 {CALG_3DES, 168,168, 168,RSAENH_PCT1_SSL2_SSL3_TLS1, 5,"3DES", 21,"Three Key Triple DES"},
216 {CALG_SHA,160,160,160,CRYPT_FLAG_SIGNING|RSAENH_PCT1_SSL2_SSL3_TLS1,6,"SHA-1",30,"Secure Hash Algorithm (SHA-1)"},
217 {CALG_MD5,128,128,128,CRYPT_FLAG_SIGNING|RSAENH_PCT1_SSL2_SSL3_TLS1,4,"MD5",23,"Message Digest 5 (MD5)"},
218 {CALG_SSL3_SHAMD5,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
219 {CALG_MAC, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
220 {CALG_RSA_SIGN,1024,384,16384,CRYPT_FLAG_SIGNING|RSAENH_PCT1_SSL2_SSL3_TLS1,9,"RSA_SIGN",14,"RSA Signature"},
221 {CALG_RSA_KEYX,1024,384,16384,CRYPT_FLAG_SIGNING|RSAENH_PCT1_SSL2_SSL3_TLS1,9,"RSA_KEYX",17,"RSA Key Exchange"},
222 {CALG_HMAC, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
223 {CALG_PCT1_MASTER,128,128,128,CRYPT_FLAG_PCT1, 12,"PCT1 MASTER",12,"PCT1 Master"},
224 {CALG_SSL2_MASTER,40,40, 192,CRYPT_FLAG_SSL2, 12,"SSL2 MASTER",12,"SSL2 Master"},
225 {CALG_SSL3_MASTER,384,384,384,CRYPT_FLAG_SSL3, 12,"SSL3 MASTER",12,"SSL3 Master"},
226 {CALG_TLS1_MASTER,384,384,384,CRYPT_FLAG_TLS1, 12,"TLS1 MASTER",12,"TLS1 Master"},
227 {CALG_SCHANNEL_MASTER_HASH,0,0,-1,0, 16,"SCH MASTER HASH",21,"SChannel Master Hash"},
228 {CALG_SCHANNEL_MAC_KEY,0,0,-1,0, 12,"SCH MAC KEY",17,"SChannel MAC Key"},
229 {CALG_SCHANNEL_ENC_KEY,0,0,-1,0, 12,"SCH ENC KEY",24,"SChannel Encryption Key"},
230 {CALG_TLS1PRF, 0, 0, -1,0, 9,"TLS1 PRF", 28,"TLS1 Pseudo Random Function"},
231 {0, 0, 0, 0,0, 1,"", 1,""}
232 },
233 {
234 {CALG_RC2, 128, 40, 128,0, 4,"RC2", 24,"RSA Data Security's RC2"},
235 {CALG_RC4, 128, 40, 128,0, 4,"RC4", 24,"RSA Data Security's RC4"},
236 {CALG_DES, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
237 {CALG_3DES_112, 112,112, 112,0, 13,"3DES TWO KEY",19,"Two Key Triple DES"},
238 {CALG_3DES, 168,168, 168,0, 5,"3DES", 21,"Three Key Triple DES"},
239 {CALG_AES, 128,128, 128,0, 4,"AES", 35,"Advanced Encryption Standard (AES)"},
240 {CALG_AES_128, 128,128, 128,0, 8,"AES-128", 39,"Advanced Encryption Standard (AES-128)"},
241 {CALG_AES_192, 192,192, 192,0, 8,"AES-192", 39,"Advanced Encryption Standard (AES-192)"},
242 {CALG_AES_256, 256,256, 256,0, 8,"AES-256", 39,"Advanced Encryption Standard (AES-256)"},
243 {CALG_SHA, 160,160, 160,CRYPT_FLAG_SIGNING, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
244 {CALG_MD2, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD2", 23,"Message Digest 2 (MD2)"},
245 {CALG_MD4, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD4", 23,"Message Digest 4 (MD4)"},
246 {CALG_MD5, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD5", 23,"Message Digest 5 (MD5)"},
247 {CALG_SSL3_SHAMD5,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
248 {CALG_MAC, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
249 {CALG_RSA_SIGN,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_SIGN",14,"RSA Signature"},
250 {CALG_RSA_KEYX,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_KEYX",17,"RSA Key Exchange"},
251 {CALG_HMAC, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
252 {0, 0, 0, 0,0, 1,"", 1,""}
253 }
254 };
255
256 /******************************************************************************
257 * API forward declarations
258 */
259 BOOL WINAPI
260 RSAENH_CPGetKeyParam(
261 HCRYPTPROV hProv,
262 HCRYPTKEY hKey,
263 DWORD dwParam,
264 BYTE *pbData,
265 DWORD *pdwDataLen,
266 DWORD dwFlags
267 );
268
269 BOOL WINAPI
270 RSAENH_CPEncrypt(
271 HCRYPTPROV hProv,
272 HCRYPTKEY hKey,
273 HCRYPTHASH hHash,
274 BOOL Final,
275 DWORD dwFlags,
276 BYTE *pbData,
277 DWORD *pdwDataLen,
278 DWORD dwBufLen
279 );
280
281 BOOL WINAPI
282 RSAENH_CPCreateHash(
283 HCRYPTPROV hProv,
284 ALG_ID Algid,
285 HCRYPTKEY hKey,
286 DWORD dwFlags,
287 HCRYPTHASH *phHash
288 );
289
290 BOOL WINAPI
291 RSAENH_CPSetHashParam(
292 HCRYPTPROV hProv,
293 HCRYPTHASH hHash,
294 DWORD dwParam,
295 BYTE *pbData, DWORD dwFlags
296 );
297
298 BOOL WINAPI
299 RSAENH_CPGetHashParam(
300 HCRYPTPROV hProv,
301 HCRYPTHASH hHash,
302 DWORD dwParam,
303 BYTE *pbData,
304 DWORD *pdwDataLen,
305 DWORD dwFlags
306 );
307
308 BOOL WINAPI
309 RSAENH_CPDestroyHash(
310 HCRYPTPROV hProv,
311 HCRYPTHASH hHash
312 );
313
314 static BOOL crypt_export_key(
315 CRYPTKEY *pCryptKey,
316 HCRYPTKEY hPubKey,
317 DWORD dwBlobType,
318 DWORD dwFlags,
319 BOOL force,
320 BYTE *pbData,
321 DWORD *pdwDataLen
322 );
323
324 static BOOL import_key(
325 HCRYPTPROV hProv,
326 CONST BYTE *pbData,
327 DWORD dwDataLen,
328 HCRYPTKEY hPubKey,
329 DWORD dwFlags,
330 BOOL fStoreKey,
331 HCRYPTKEY *phKey
332 );
333
334 BOOL WINAPI
335 RSAENH_CPHashData(
336 HCRYPTPROV hProv,
337 HCRYPTHASH hHash,
338 CONST BYTE *pbData,
339 DWORD dwDataLen,
340 DWORD dwFlags
341 );
342
343 /******************************************************************************
344 * CSP's handle table (used by all acquired key containers)
345 */
346 static struct handle_table handle_table;
347
348 /******************************************************************************
349 * DllMain (RSAENH.@)
350 *
351 * Initializes and destroys the handle table for the CSP's handles.
352 */
353 int WINAPI DllMain(HINSTANCE hInstance, DWORD fdwReason, PVOID pvReserved)
354 {
355 switch (fdwReason)
356 {
357 case DLL_PROCESS_ATTACH:
358 DisableThreadLibraryCalls(hInstance);
359 init_handle_table(&handle_table);
360 break;
361
362 case DLL_PROCESS_DETACH:
363 destroy_handle_table(&handle_table);
364 break;
365 }
366 return 1;
367 }
368
369 /******************************************************************************
370 * copy_param [Internal]
371 *
372 * Helper function that supports the standard WINAPI protocol for querying data
373 * of dynamic size.
374 *
375 * PARAMS
376 * pbBuffer [O] Buffer where the queried parameter is copied to, if it is large enough.
377 * May be NUL if the required buffer size is to be queried only.
378 * pdwBufferSize [I/O] In: Size of the buffer at pbBuffer
379 * Out: Size of parameter pbParam
380 * pbParam [I] Parameter value.
381 * dwParamSize [I] Size of pbParam
382 *
383 * RETURN
384 * Success: TRUE (pbParam was copied into pbBuffer or pbBuffer is NULL)
385 * Failure: FALSE (pbBuffer is not large enough to hold pbParam). Last error: ERROR_MORE_DATA
386 */
387 static inline BOOL copy_param(
388 BYTE *pbBuffer, DWORD *pdwBufferSize, CONST BYTE *pbParam, DWORD dwParamSize)
389 {
390 if (pbBuffer)
391 {
392 if (dwParamSize > *pdwBufferSize)
393 {
394 SetLastError(ERROR_MORE_DATA);
395 *pdwBufferSize = dwParamSize;
396 return FALSE;
397 }
398 memcpy(pbBuffer, pbParam, dwParamSize);
399 }
400 *pdwBufferSize = dwParamSize;
401 return TRUE;
402 }
403
404 /******************************************************************************
405 * get_algid_info [Internal]
406 *
407 * Query CSP capabilities for a given crypto algorithm.
408 *
409 * PARAMS
410 * hProv [I] Handle to a key container of the CSP whose capabilities are to be queried.
411 * algid [I] Identifier of the crypto algorithm about which information is requested.
412 *
413 * RETURNS
414 * Success: Pointer to a PROV_ENUMALGS_EX struct containing information about the crypto algorithm.
415 * Failure: NULL (algid not supported)
416 */
417 static inline const PROV_ENUMALGS_EX* get_algid_info(HCRYPTPROV hProv, ALG_ID algid) {
418 const PROV_ENUMALGS_EX *iterator;
419 KEYCONTAINER *pKeyContainer;
420
421 if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER, (OBJECTHDR**)&pKeyContainer)) {
422 SetLastError(NTE_BAD_UID);
423 return NULL;
424 }
425
426 for (iterator = aProvEnumAlgsEx[pKeyContainer->dwPersonality]; iterator->aiAlgid; iterator++) {
427 if (iterator->aiAlgid == algid) return iterator;
428 }
429
430 SetLastError(NTE_BAD_ALGID);
431 return NULL;
432 }
433
434 /******************************************************************************
435 * copy_data_blob [Internal]
436 *
437 * deeply copies a DATA_BLOB
438 *
439 * PARAMS
440 * dst [O] That's where the blob will be copied to
441 * src [I] Source blob
442 *
443 * RETURNS
444 * Success: TRUE
445 * Failure: FALSE (GetLastError() == NTE_NO_MEMORY
446 *
447 * NOTES
448 * Use free_data_blob to release resources occupied by copy_data_blob.
449 */
450 static inline BOOL copy_data_blob(PCRYPT_DATA_BLOB dst, CONST PCRYPT_DATA_BLOB src) {
451 dst->pbData = HeapAlloc(GetProcessHeap(), 0, src->cbData);
452 if (!dst->pbData) {
453 SetLastError(NTE_NO_MEMORY);
454 return FALSE;
455 }
456 dst->cbData = src->cbData;
457 memcpy(dst->pbData, src->pbData, src->cbData);
458 return TRUE;
459 }
460
461 /******************************************************************************
462 * concat_data_blobs [Internal]
463 *
464 * Concatenates two blobs
465 *
466 * PARAMS
467 * dst [O] The new blob will be copied here
468 * src1 [I] Prefix blob
469 * src2 [I] Appendix blob
470 *
471 * RETURNS
472 * Success: TRUE
473 * Failure: FALSE (GetLastError() == NTE_NO_MEMORY)
474 *
475 * NOTES
476 * Release resources occupied by concat_data_blobs with free_data_blobs
477 */
478 static inline BOOL concat_data_blobs(PCRYPT_DATA_BLOB dst, CONST PCRYPT_DATA_BLOB src1,
479 CONST PCRYPT_DATA_BLOB src2)
480 {
481 dst->cbData = src1->cbData + src2->cbData;
482 dst->pbData = HeapAlloc(GetProcessHeap(), 0, dst->cbData);
483 if (!dst->pbData) {
484 SetLastError(NTE_NO_MEMORY);
485 return FALSE;
486 }
487 memcpy(dst->pbData, src1->pbData, src1->cbData);
488 memcpy(dst->pbData + src1->cbData, src2->pbData, src2->cbData);
489 return TRUE;
490 }
491
492 /******************************************************************************
493 * free_data_blob [Internal]
494 *
495 * releases resource occupied by a dynamically allocated CRYPT_DATA_BLOB
496 *
497 * PARAMS
498 * pBlob [I] Heap space occupied by pBlob->pbData is released
499 */
500 static inline void free_data_blob(PCRYPT_DATA_BLOB pBlob) {
501 HeapFree(GetProcessHeap(), 0, pBlob->pbData);
502 }
503
504 /******************************************************************************
505 * init_data_blob [Internal]
506 */
507 static inline void init_data_blob(PCRYPT_DATA_BLOB pBlob) {
508 pBlob->pbData = NULL;
509 pBlob->cbData = 0;
510 }
511
512 /******************************************************************************
513 * free_hmac_info [Internal]
514 *
515 * Deeply free an HMAC_INFO struct.
516 *
517 * PARAMS
518 * hmac_info [I] Pointer to the HMAC_INFO struct to be freed.
519 *
520 * NOTES
521 * See Internet RFC 2104 for details on the HMAC algorithm.
522 */
523 static inline void free_hmac_info(PHMAC_INFO hmac_info) {
524 if (!hmac_info) return;
525 HeapFree(GetProcessHeap(), 0, hmac_info->pbInnerString);
526 HeapFree(GetProcessHeap(), 0, hmac_info->pbOuterString);
527 HeapFree(GetProcessHeap(), 0, hmac_info);
528 }
529
530 /******************************************************************************
531 * copy_hmac_info [Internal]
532 *
533 * Deeply copy an HMAC_INFO struct
534 *
535 * PARAMS
536 * dst [O] Pointer to a location where the pointer to the HMAC_INFO copy will be stored.
537 * src [I] Pointer to the HMAC_INFO struct to be copied.
538 *
539 * RETURNS
540 * Success: TRUE
541 * Failure: FALSE
542 *
543 * NOTES
544 * See Internet RFC 2104 for details on the HMAC algorithm.
545 */
546 static BOOL copy_hmac_info(PHMAC_INFO *dst, const HMAC_INFO *src) {
547 if (!src) return FALSE;
548 *dst = HeapAlloc(GetProcessHeap(), 0, sizeof(HMAC_INFO));
549 if (!*dst) return FALSE;
550 **dst = *src;
551 (*dst)->pbInnerString = NULL;
552 (*dst)->pbOuterString = NULL;
553 if ((*dst)->cbInnerString == 0) (*dst)->cbInnerString = RSAENH_HMAC_DEF_PAD_LEN;
554 (*dst)->pbInnerString = HeapAlloc(GetProcessHeap(), 0, (*dst)->cbInnerString);
555 if (!(*dst)->pbInnerString) {
556 free_hmac_info(*dst);
557 return FALSE;
558 }
559 if (src->cbInnerString)
560 memcpy((*dst)->pbInnerString, src->pbInnerString, src->cbInnerString);
561 else
562 memset((*dst)->pbInnerString, RSAENH_HMAC_DEF_IPAD_CHAR, RSAENH_HMAC_DEF_PAD_LEN);
563 if ((*dst)->cbOuterString == 0) (*dst)->cbOuterString = RSAENH_HMAC_DEF_PAD_LEN;
564 (*dst)->pbOuterString = HeapAlloc(GetProcessHeap(), 0, (*dst)->cbOuterString);
565 if (!(*dst)->pbOuterString) {
566 free_hmac_info(*dst);
567 return FALSE;
568 }
569 if (src->cbOuterString)
570 memcpy((*dst)->pbOuterString, src->pbOuterString, src->cbOuterString);
571 else
572 memset((*dst)->pbOuterString, RSAENH_HMAC_DEF_OPAD_CHAR, RSAENH_HMAC_DEF_PAD_LEN);
573 return TRUE;
574 }
575
576 /******************************************************************************
577 * destroy_hash [Internal]
578 *
579 * Destructor for hash objects
580 *
581 * PARAMS
582 * pCryptHash [I] Pointer to the hash object to be destroyed.
583 * Will be invalid after function returns!
584 */
585 static void destroy_hash(OBJECTHDR *pObject)
586 {
587 CRYPTHASH *pCryptHash = (CRYPTHASH*)pObject;
588
589 free_hmac_info(pCryptHash->pHMACInfo);
590 free_data_blob(&pCryptHash->tpPRFParams.blobLabel);
591 free_data_blob(&pCryptHash->tpPRFParams.blobSeed);
592 HeapFree(GetProcessHeap(), 0, pCryptHash);
593 }
594
595 /******************************************************************************
596 * init_hash [Internal]
597 *
598 * Initialize (or reset) a hash object
599 *
600 * PARAMS
601 * pCryptHash [I] The hash object to be initialized.
602 */
603 static inline BOOL init_hash(CRYPTHASH *pCryptHash) {
604 DWORD dwLen;
605
606 switch (pCryptHash->aiAlgid)
607 {
608 case CALG_HMAC:
609 if (pCryptHash->pHMACInfo) {
610 const PROV_ENUMALGS_EX *pAlgInfo;
611
612 pAlgInfo = get_algid_info(pCryptHash->hProv, pCryptHash->pHMACInfo->HashAlgid);
613 if (!pAlgInfo) return FALSE;
614 pCryptHash->dwHashSize = pAlgInfo->dwDefaultLen >> 3;
615 init_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context);
616 update_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
617 pCryptHash->pHMACInfo->pbInnerString,
618 pCryptHash->pHMACInfo->cbInnerString);
619 }
620 return TRUE;
621
622 case CALG_MAC:
623 dwLen = sizeof(DWORD);
624 RSAENH_CPGetKeyParam(pCryptHash->hProv, pCryptHash->hKey, KP_BLOCKLEN,
625 (BYTE*)&pCryptHash->dwHashSize, &dwLen, 0);
626 pCryptHash->dwHashSize >>= 3;
627 return TRUE;
628
629 default:
630 return init_hash_impl(pCryptHash->aiAlgid, &pCryptHash->context);
631 }
632 }
633
634 /******************************************************************************
635 * update_hash [Internal]
636 *
637 * Hashes the given data and updates the hash object's state accordingly
638 *
639 * PARAMS
640 * pCryptHash [I] Hash object to be updated.
641 * pbData [I] Pointer to data stream to be hashed.
642 * dwDataLen [I] Length of data stream.
643 */
644 static inline void update_hash(CRYPTHASH *pCryptHash, CONST BYTE *pbData, DWORD dwDataLen) {
645 BYTE *pbTemp;
646
647 switch (pCryptHash->aiAlgid)
648 {
649 case CALG_HMAC:
650 if (pCryptHash->pHMACInfo)
651 update_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
652 pbData, dwDataLen);
653 break;
654
655 case CALG_MAC:
656 pbTemp = HeapAlloc(GetProcessHeap(), 0, dwDataLen);
657 if (!pbTemp) return;
658 memcpy(pbTemp, pbData, dwDataLen);
659 RSAENH_CPEncrypt(pCryptHash->hProv, pCryptHash->hKey, 0, FALSE, 0,
660 pbTemp, &dwDataLen, dwDataLen);
661 HeapFree(GetProcessHeap(), 0, pbTemp);
662 break;
663
664 default:
665 update_hash_impl(pCryptHash->aiAlgid, &pCryptHash->context, pbData, dwDataLen);
666 }
667 }
668
669 /******************************************************************************
670 * finalize_hash [Internal]
671 *
672 * Finalizes the hash, after all data has been hashed with update_hash.
673 * No additional data can be hashed afterwards until the hash gets initialized again.
674 *
675 * PARAMS
676 * pCryptHash [I] Hash object to be finalized.
677 */
678 static inline void finalize_hash(CRYPTHASH *pCryptHash) {
679 DWORD dwDataLen;
680
681 switch (pCryptHash->aiAlgid)
682 {
683 case CALG_HMAC:
684 if (pCryptHash->pHMACInfo) {
685 BYTE abHashValue[RSAENH_MAX_HASH_SIZE];
686
687 finalize_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
688 pCryptHash->abHashValue);
689 memcpy(abHashValue, pCryptHash->abHashValue, pCryptHash->dwHashSize);
690 init_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context);
691 update_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
692 pCryptHash->pHMACInfo->pbOuterString,
693 pCryptHash->pHMACInfo->cbOuterString);
694 update_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
695 abHashValue, pCryptHash->dwHashSize);
696 finalize_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
697 pCryptHash->abHashValue);
698 }
699 break;
700
701 case CALG_MAC:
702 dwDataLen = 0;
703 RSAENH_CPEncrypt(pCryptHash->hProv, pCryptHash->hKey, 0, TRUE, 0,
704 pCryptHash->abHashValue, &dwDataLen, pCryptHash->dwHashSize);
705 break;
706
707 default:
708 finalize_hash_impl(pCryptHash->aiAlgid, &pCryptHash->context, pCryptHash->abHashValue);
709 }
710 }
711
712 /******************************************************************************
713 * destroy_key [Internal]
714 *
715 * Destructor for key objects
716 *
717 * PARAMS
718 * pCryptKey [I] Pointer to the key object to be destroyed.
719 * Will be invalid after function returns!
720 */
721 static void destroy_key(OBJECTHDR *pObject)
722 {
723 CRYPTKEY *pCryptKey = (CRYPTKEY*)pObject;
724
725 free_key_impl(pCryptKey->aiAlgid, &pCryptKey->context);
726 free_data_blob(&pCryptKey->siSChannelInfo.blobClientRandom);
727 free_data_blob(&pCryptKey->siSChannelInfo.blobServerRandom);
728 HeapFree(GetProcessHeap(), 0, pCryptKey);
729 }
730
731 /******************************************************************************
732 * setup_key [Internal]
733 *
734 * Initialize (or reset) a key object
735 *
736 * PARAMS
737 * pCryptKey [I] The key object to be initialized.
738 */
739 static inline void setup_key(CRYPTKEY *pCryptKey) {
740 pCryptKey->dwState = RSAENH_KEYSTATE_IDLE;
741 memcpy(pCryptKey->abChainVector, pCryptKey->abInitVector, sizeof(pCryptKey->abChainVector));
742 setup_key_impl(pCryptKey->aiAlgid, &pCryptKey->context, pCryptKey->dwKeyLen,
743 pCryptKey->dwEffectiveKeyLen, pCryptKey->dwSaltLen,
744 pCryptKey->abKeyValue);
745 }
746
747 /******************************************************************************
748 * new_key [Internal]
749 *
750 * Creates a new key object without assigning the actual binary key value.
751 * This is done by CPDeriveKey, CPGenKey or CPImportKey, which call this function.
752 *
753 * PARAMS
754 * hProv [I] Handle to the provider to which the created key will belong.
755 * aiAlgid [I] The new key shall use the crypto algorithm idenfied by aiAlgid.
756 * dwFlags [I] Upper 16 bits give the key length.
757 * Lower 16 bits: CRYPT_EXPORTABLE, CRYPT_CREATE_SALT,
758 * CRYPT_NO_SALT
759 * ppCryptKey [O] Pointer to the created key
760 *
761 * RETURNS
762 * Success: Handle to the created key.
763 * Failure: INVALID_HANDLE_VALUE
764 */
765 static HCRYPTKEY new_key(HCRYPTPROV hProv, ALG_ID aiAlgid, DWORD dwFlags, CRYPTKEY **ppCryptKey)
766 {
767 HCRYPTKEY hCryptKey;
768 CRYPTKEY *pCryptKey;
769 DWORD dwKeyLen = HIWORD(dwFlags);
770 const PROV_ENUMALGS_EX *peaAlgidInfo;
771
772 *ppCryptKey = NULL;
773
774 /*
775 * Retrieve the CSP's capabilities for the given ALG_ID value
776 */
777 peaAlgidInfo = get_algid_info(hProv, aiAlgid);
778 if (!peaAlgidInfo) return (HCRYPTKEY)INVALID_HANDLE_VALUE;
779
780 TRACE("alg = %s, dwKeyLen = %d\n", debugstr_a(peaAlgidInfo->szName),
781 dwKeyLen);
782 /*
783 * Assume the default key length, if none is specified explicitly
784 */
785 if (dwKeyLen == 0) dwKeyLen = peaAlgidInfo->dwDefaultLen;
786
787 /*
788 * Check if the requested key length is supported by the current CSP.
789 * Adjust key length's for DES algorithms.
790 */
791 switch (aiAlgid) {
792 case CALG_DES:
793 if (dwKeyLen == RSAENH_DES_EFFECTIVE_KEYLEN) {
794 dwKeyLen = RSAENH_DES_STORAGE_KEYLEN;
795 }
796 if (dwKeyLen != RSAENH_DES_STORAGE_KEYLEN) {
797 SetLastError(NTE_BAD_FLAGS);
798 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
799 }
800 break;
801
802 case CALG_3DES_112:
803 if (dwKeyLen == RSAENH_3DES112_EFFECTIVE_KEYLEN) {
804 dwKeyLen = RSAENH_3DES112_STORAGE_KEYLEN;
805 }
806 if (dwKeyLen != RSAENH_3DES112_STORAGE_KEYLEN) {
807 SetLastError(NTE_BAD_FLAGS);
808 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
809 }
810 break;
811
812 case CALG_3DES:
813 if (dwKeyLen == RSAENH_3DES_EFFECTIVE_KEYLEN) {
814 dwKeyLen = RSAENH_3DES_STORAGE_KEYLEN;
815 }
816 if (dwKeyLen != RSAENH_3DES_STORAGE_KEYLEN) {
817 SetLastError(NTE_BAD_FLAGS);
818 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
819 }
820 break;
821
822 default:
823 if (dwKeyLen % 8 ||
824 dwKeyLen > peaAlgidInfo->dwMaxLen ||
825 dwKeyLen < peaAlgidInfo->dwMinLen)
826 {
827 TRACE("key len %d out of bounds (%d, %d)\n", dwKeyLen,
828 peaAlgidInfo->dwMinLen, peaAlgidInfo->dwMaxLen);
829 SetLastError(NTE_BAD_DATA);
830 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
831 }
832 }
833
834 hCryptKey = new_object(&handle_table, sizeof(CRYPTKEY), RSAENH_MAGIC_KEY,
835 destroy_key, (OBJECTHDR**)&pCryptKey);
836 if (hCryptKey != (HCRYPTKEY)INVALID_HANDLE_VALUE)
837 {
838 pCryptKey->aiAlgid = aiAlgid;
839 pCryptKey->hProv = hProv;
840 pCryptKey->dwModeBits = 0;
841 pCryptKey->dwPermissions = CRYPT_ENCRYPT | CRYPT_DECRYPT | CRYPT_READ | CRYPT_WRITE |
842 CRYPT_MAC;
843 if (dwFlags & CRYPT_EXPORTABLE)
844 pCryptKey->dwPermissions |= CRYPT_EXPORT;
845 pCryptKey->dwKeyLen = dwKeyLen >> 3;
846 pCryptKey->dwEffectiveKeyLen = 0;
847 if ((dwFlags & CRYPT_CREATE_SALT) || (dwKeyLen == 40 && !(dwFlags & CRYPT_NO_SALT)))
848 pCryptKey->dwSaltLen = 16 /*FIXME*/ - pCryptKey->dwKeyLen;
849 else
850 pCryptKey->dwSaltLen = 0;
851 memset(pCryptKey->abKeyValue, 0, sizeof(pCryptKey->abKeyValue));
852 memset(pCryptKey->abInitVector, 0, sizeof(pCryptKey->abInitVector));
853 init_data_blob(&pCryptKey->siSChannelInfo.blobClientRandom);
854 init_data_blob(&pCryptKey->siSChannelInfo.blobServerRandom);
855
856 switch(aiAlgid)
857 {
858 case CALG_PCT1_MASTER:
859 case CALG_SSL2_MASTER:
860 case CALG_SSL3_MASTER:
861 case CALG_TLS1_MASTER:
862 case CALG_RC4:
863 pCryptKey->dwBlockLen = 0;
864 pCryptKey->dwMode = 0;
865 break;
866
867 case CALG_RC2:
868 case CALG_DES:
869 case CALG_3DES_112:
870 case CALG_3DES:
871 pCryptKey->dwBlockLen = 8;
872 pCryptKey->dwMode = CRYPT_MODE_CBC;
873 break;
874
875 case CALG_AES:
876 case CALG_AES_128:
877 case CALG_AES_192:
878 case CALG_AES_256:
879 pCryptKey->dwBlockLen = 16;
880 pCryptKey->dwMode = CRYPT_MODE_ECB;
881 break;
882
883 case CALG_RSA_KEYX:
884 case CALG_RSA_SIGN:
885 pCryptKey->dwBlockLen = dwKeyLen >> 3;
886 pCryptKey->dwMode = 0;
887 break;
888 }
889
890 *ppCryptKey = pCryptKey;
891 }
892
893 return hCryptKey;
894 }
895
896 /******************************************************************************
897 * map_key_spec_to_key_pair_name [Internal]
898 *
899 * Returns the name of the registry value associated with a key spec.
900 *
901 * PARAMS
902 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
903 *
904 * RETURNS
905 * Success: Name of registry value.
906 * Failure: NULL
907 */
908 static LPCSTR map_key_spec_to_key_pair_name(DWORD dwKeySpec)
909 {
910 LPCSTR szValueName;
911
912 switch (dwKeySpec)
913 {
914 case AT_KEYEXCHANGE:
915 szValueName = "KeyExchangeKeyPair";
916 break;
917 case AT_SIGNATURE:
918 szValueName = "SignatureKeyPair";
919 break;
920 default:
921 WARN("invalid key spec %d\n", dwKeySpec);
922 szValueName = NULL;
923 }
924 return szValueName;
925 }
926
927 /******************************************************************************
928 * store_key_pair [Internal]
929 *
930 * Stores a key pair to the registry
931 *
932 * PARAMS
933 * hCryptKey [I] Handle to the key to be stored
934 * hKey [I] Registry key where the key pair is to be stored
935 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
936 * dwFlags [I] Flags for protecting the key
937 */
938 static void store_key_pair(HCRYPTKEY hCryptKey, HKEY hKey, DWORD dwKeySpec, DWORD dwFlags)
939 {
940 LPCSTR szValueName;
941 DATA_BLOB blobIn, blobOut;
942 CRYPTKEY *pKey;
943 DWORD dwLen;
944 BYTE *pbKey;
945
946 if (!(szValueName = map_key_spec_to_key_pair_name(dwKeySpec)))
947 return;
948 if (lookup_handle(&handle_table, hCryptKey, RSAENH_MAGIC_KEY,
949 (OBJECTHDR**)&pKey))
950 {
951 if (crypt_export_key(pKey, 0, PRIVATEKEYBLOB, 0, TRUE, 0, &dwLen))
952 {
953 pbKey = HeapAlloc(GetProcessHeap(), 0, dwLen);
954 if (pbKey)
955 {
956 if (crypt_export_key(pKey, 0, PRIVATEKEYBLOB, 0, TRUE, pbKey,
957 &dwLen))
958 {
959 blobIn.pbData = pbKey;
960 blobIn.cbData = dwLen;
961
962 if (CryptProtectData(&blobIn, NULL, NULL, NULL, NULL,
963 dwFlags, &blobOut))
964 {
965 RegSetValueExA(hKey, szValueName, 0, REG_BINARY,
966 blobOut.pbData, blobOut.cbData);
967 LocalFree(blobOut.pbData);
968 }
969 }
970 HeapFree(GetProcessHeap(), 0, pbKey);
971 }
972 }
973 }
974 }
975
976 /******************************************************************************
977 * map_key_spec_to_permissions_name [Internal]
978 *
979 * Returns the name of the registry value associated with the permissions for
980 * a key spec.
981 *
982 * PARAMS
983 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
984 *
985 * RETURNS
986 * Success: Name of registry value.
987 * Failure: NULL
988 */
989 static LPCSTR map_key_spec_to_permissions_name(DWORD dwKeySpec)
990 {
991 LPCSTR szValueName;
992
993 switch (dwKeySpec)
994 {
995 case AT_KEYEXCHANGE:
996 szValueName = "KeyExchangePermissions";
997 break;
998 case AT_SIGNATURE:
999 szValueName = "SignaturePermissions";
1000 break;
1001 default:
1002 WARN("invalid key spec %d\n", dwKeySpec);
1003 szValueName = NULL;
1004 }
1005 return szValueName;
1006 }
1007
1008 /******************************************************************************
1009 * store_key_permissions [Internal]
1010 *
1011 * Stores a key's permissions to the registry
1012 *
1013 * PARAMS
1014 * hCryptKey [I] Handle to the key whose permissions are to be stored
1015 * hKey [I] Registry key where the key permissions are to be stored
1016 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
1017 */
1018 static void store_key_permissions(HCRYPTKEY hCryptKey, HKEY hKey, DWORD dwKeySpec)
1019 {
1020 LPCSTR szValueName;
1021 CRYPTKEY *pKey;
1022
1023 if (!(szValueName = map_key_spec_to_permissions_name(dwKeySpec)))
1024 return;
1025 if (lookup_handle(&handle_table, hCryptKey, RSAENH_MAGIC_KEY,
1026 (OBJECTHDR**)&pKey))
1027 RegSetValueExA(hKey, szValueName, 0, REG_DWORD,
1028 (BYTE *)&pKey->dwPermissions,
1029 sizeof(pKey->dwPermissions));
1030 }
1031
1032 /******************************************************************************
1033 * create_container_key [Internal]
1034 *
1035 * Creates the registry key for a key container's persistent storage.
1036 *
1037 * PARAMS
1038 * pKeyContainer [I] Pointer to the key container
1039 * sam [I] Desired registry access
1040 * phKey [O] Returned key
1041 */
1042 static BOOL create_container_key(KEYCONTAINER *pKeyContainer, REGSAM sam, HKEY *phKey)
1043 {
1044 CHAR szRSABase[MAX_PATH];
1045 HKEY hRootKey;
1046
1047 sprintf(szRSABase, RSAENH_REGKEY, pKeyContainer->szName);
1048
1049 if (pKeyContainer->dwFlags & CRYPT_MACHINE_KEYSET)
1050 hRootKey = HKEY_LOCAL_MACHINE;
1051 else
1052 hRootKey = HKEY_CURRENT_USER;
1053
1054 /* @@ Wine registry key: HKLM\Software\Wine\Crypto\RSA */
1055 /* @@ Wine registry key: HKCU\Software\Wine\Crypto\RSA */
1056 return RegCreateKeyExA(hRootKey, szRSABase, 0, NULL,
1057 REG_OPTION_NON_VOLATILE, sam, NULL, phKey, NULL)
1058 == ERROR_SUCCESS;
1059 }
1060
1061 /******************************************************************************
1062 * open_container_key [Internal]
1063 *
1064 * Opens a key container's persistent storage for reading.
1065 *
1066 * PARAMS
1067 * pszContainerName [I] Name of the container to be opened. May be the empty
1068 * string if the parent key of all containers is to be
1069 * opened.
1070 * dwFlags [I] Flags indicating which keyset to be opened.
1071 * phKey [O] Returned key
1072 */
1073 static BOOL open_container_key(LPCSTR pszContainerName, DWORD dwFlags, HKEY *phKey)
1074 {
1075 CHAR szRSABase[MAX_PATH];
1076 HKEY hRootKey;
1077
1078 sprintf(szRSABase, RSAENH_REGKEY, pszContainerName);
1079
1080 if (dwFlags & CRYPT_MACHINE_KEYSET)
1081 hRootKey = HKEY_LOCAL_MACHINE;
1082 else
1083 hRootKey = HKEY_CURRENT_USER;
1084
1085 /* @@ Wine registry key: HKLM\Software\Wine\Crypto\RSA */
1086 /* @@ Wine registry key: HKCU\Software\Wine\Crypto\RSA */
1087 return RegOpenKeyExA(hRootKey, szRSABase, 0, KEY_READ, phKey) ==
1088 ERROR_SUCCESS;
1089 }
1090
1091 /******************************************************************************
1092 * delete_container_key [Internal]
1093 *
1094 * Deletes a key container's persistent storage.
1095 *
1096 * PARAMS
1097 * pszContainerName [I] Name of the container to be opened.
1098 * dwFlags [I] Flags indicating which keyset to be opened.
1099 */
1100 static BOOL delete_container_key(LPCSTR pszContainerName, DWORD dwFlags)
1101 {
1102 CHAR szRegKey[MAX_PATH];
1103
1104 if (snprintf(szRegKey, MAX_PATH, RSAENH_REGKEY, pszContainerName) >= MAX_PATH) {
1105 SetLastError(NTE_BAD_KEYSET_PARAM);
1106 return FALSE;
1107 } else {
1108 HKEY hRootKey;
1109 if (dwFlags & CRYPT_MACHINE_KEYSET)
1110 hRootKey = HKEY_LOCAL_MACHINE;
1111 else
1112 hRootKey = HKEY_CURRENT_USER;
1113 if (!RegDeleteKeyA(hRootKey, szRegKey)) {
1114 SetLastError(ERROR_SUCCESS);
1115 return TRUE;
1116 } else {
1117 SetLastError(NTE_BAD_KEYSET);
1118 return FALSE;
1119 }
1120 }
1121 }
1122
1123 /******************************************************************************
1124 * store_key_container_keys [Internal]
1125 *
1126 * Stores key container's keys in a persistent location.
1127 *
1128 * PARAMS
1129 * pKeyContainer [I] Pointer to the key container whose keys are to be saved
1130 */
1131 static void store_key_container_keys(KEYCONTAINER *pKeyContainer)
1132 {
1133 HKEY hKey;
1134 DWORD dwFlags;
1135
1136 /* On WinXP, persistent keys are stored in a file located at:
1137 * $AppData$\\Microsoft\\Crypto\\RSA\\$SID$\\some_hex_string
1138 */
1139
1140 if (pKeyContainer->dwFlags & CRYPT_MACHINE_KEYSET)
1141 dwFlags = CRYPTPROTECT_LOCAL_MACHINE;
1142 else
1143 dwFlags = 0;
1144
1145 if (create_container_key(pKeyContainer, KEY_WRITE, &hKey))
1146 {
1147 store_key_pair(pKeyContainer->hKeyExchangeKeyPair, hKey,
1148 AT_KEYEXCHANGE, dwFlags);
1149 store_key_pair(pKeyContainer->hSignatureKeyPair, hKey,
1150 AT_SIGNATURE, dwFlags);
1151 RegCloseKey(hKey);
1152 }
1153 }
1154
1155 /******************************************************************************
1156 * store_key_container_permissions [Internal]
1157 *
1158 * Stores key container's key permissions in a persistent location.
1159 *
1160 * PARAMS
1161 * pKeyContainer [I] Pointer to the key container whose key permissions are to
1162 * be saved
1163 */
1164 static void store_key_container_permissions(KEYCONTAINER *pKeyContainer)
1165 {
1166 HKEY hKey;
1167 DWORD dwFlags;
1168
1169 /* On WinXP, persistent keys are stored in a file located at:
1170 * $AppData$\\Microsoft\\Crypto\\RSA\\$SID$\\some_hex_string
1171 */
1172
1173 if (pKeyContainer->dwFlags & CRYPT_MACHINE_KEYSET)
1174 dwFlags = CRYPTPROTECT_LOCAL_MACHINE;
1175 else
1176 dwFlags = 0;
1177
1178 if (create_container_key(pKeyContainer, KEY_WRITE, &hKey))
1179 {
1180 store_key_permissions(pKeyContainer->hKeyExchangeKeyPair, hKey,
1181 AT_KEYEXCHANGE);
1182 store_key_permissions(pKeyContainer->hSignatureKeyPair, hKey,
1183 AT_SIGNATURE);
1184 RegCloseKey(hKey);
1185 }
1186 }
1187
1188 /******************************************************************************
1189 * release_key_container_keys [Internal]
1190 *
1191 * Releases key container's keys.
1192 *
1193 * PARAMS
1194 * pKeyContainer [I] Pointer to the key container whose keys are to be released.
1195 */
1196 static void release_key_container_keys(KEYCONTAINER *pKeyContainer)
1197 {
1198 release_handle(&handle_table, pKeyContainer->hKeyExchangeKeyPair,
1199 RSAENH_MAGIC_KEY);
1200 release_handle(&handle_table, pKeyContainer->hSignatureKeyPair,
1201 RSAENH_MAGIC_KEY);
1202 }
1203
1204 /******************************************************************************
1205 * destroy_key_container [Internal]
1206 *
1207 * Destructor for key containers.
1208 *
1209 * PARAMS
1210 * pObjectHdr [I] Pointer to the key container to be destroyed.
1211 */
1212 static void destroy_key_container(OBJECTHDR *pObjectHdr)
1213 {
1214 KEYCONTAINER *pKeyContainer = (KEYCONTAINER*)pObjectHdr;
1215
1216 if (!(pKeyContainer->dwFlags & CRYPT_VERIFYCONTEXT))
1217 {
1218 store_key_container_keys(pKeyContainer);
1219 store_key_container_permissions(pKeyContainer);
1220 release_key_container_keys(pKeyContainer);
1221 }
1222 HeapFree( GetProcessHeap(), 0, pKeyContainer );
1223 }
1224
1225 /******************************************************************************
1226 * new_key_container [Internal]
1227 *
1228 * Create a new key container. The personality (RSA Base, Strong or Enhanced CP)
1229 * of the CSP is determined via the pVTable->pszProvName string.
1230 *
1231 * PARAMS
1232 * pszContainerName [I] Name of the key container.
1233 * pVTable [I] Callback functions and context info provided by the OS
1234 *
1235 * RETURNS
1236 * Success: Handle to the new key container.
1237 * Failure: INVALID_HANDLE_VALUE
1238 */
1239 static HCRYPTPROV new_key_container(PCCH pszContainerName, DWORD dwFlags, const VTableProvStruc *pVTable)
1240 {
1241 KEYCONTAINER *pKeyContainer;
1242 HCRYPTPROV hKeyContainer;
1243
1244 hKeyContainer = new_object(&handle_table, sizeof(KEYCONTAINER), RSAENH_MAGIC_CONTAINER,
1245 destroy_key_container, (OBJECTHDR**)&pKeyContainer);
1246 if (hKeyContainer != (HCRYPTPROV)INVALID_HANDLE_VALUE)
1247 {
1248 lstrcpynA(pKeyContainer->szName, pszContainerName, MAX_PATH);
1249 pKeyContainer->dwFlags = dwFlags;
1250 pKeyContainer->dwEnumAlgsCtr = 0;
1251 pKeyContainer->hKeyExchangeKeyPair = (HCRYPTKEY)INVALID_HANDLE_VALUE;
1252 pKeyContainer->hSignatureKeyPair = (HCRYPTKEY)INVALID_HANDLE_VALUE;
1253 if (pVTable && pVTable->pszProvName) {
1254 lstrcpynA(pKeyContainer->szProvName, pVTable->pszProvName, MAX_PATH);
1255 if (!strcmp(pVTable->pszProvName, MS_DEF_PROV_A)) {
1256 pKeyContainer->dwPersonality = RSAENH_PERSONALITY_BASE;
1257 } else if (!strcmp(pVTable->pszProvName, MS_ENHANCED_PROV_A)) {
1258 pKeyContainer->dwPersonality = RSAENH_PERSONALITY_ENHANCED;
1259 } else if (!strcmp(pVTable->pszProvName, MS_DEF_RSA_SCHANNEL_PROV_A)) {
1260 pKeyContainer->dwPersonality = RSAENH_PERSONALITY_SCHANNEL;
1261 } else if (!strcmp(pVTable->pszProvName, MS_ENH_RSA_AES_PROV_A)) {
1262 pKeyContainer->dwPersonality = RSAENH_PERSONALITY_AES;
1263 } else {
1264 pKeyContainer->dwPersonality = RSAENH_PERSONALITY_STRONG;
1265 }
1266 }
1267
1268 /* The new key container has to be inserted into the CSP immediately
1269 * after creation to be available for CPGetProvParam's PP_ENUMCONTAINERS. */
1270 if (!(dwFlags & CRYPT_VERIFYCONTEXT)) {
1271 HKEY hKey;
1272
1273 if (create_container_key(pKeyContainer, KEY_WRITE, &hKey))
1274 RegCloseKey(hKey);
1275 }
1276 }
1277
1278 return hKeyContainer;
1279 }
1280
1281 /******************************************************************************
1282 * read_key_value [Internal]
1283 *
1284 * Reads a key pair value from the registry
1285 *
1286 * PARAMS
1287 * hKeyContainer [I] Crypt provider to use to import the key
1288 * hKey [I] Registry key from which to read the key pair
1289 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
1290 * dwFlags [I] Flags for unprotecting the key
1291 * phCryptKey [O] Returned key
1292 */
1293 static BOOL read_key_value(HCRYPTPROV hKeyContainer, HKEY hKey, DWORD dwKeySpec, DWORD dwFlags, HCRYPTKEY *phCryptKey)
1294 {
1295 LPCSTR szValueName;
1296 DWORD dwValueType, dwLen;
1297 BYTE *pbKey;
1298 DATA_BLOB blobIn, blobOut;
1299 BOOL ret = FALSE;
1300
1301 if (!(szValueName = map_key_spec_to_key_pair_name(dwKeySpec)))
1302 return FALSE;
1303 if (RegQueryValueExA(hKey, szValueName, 0, &dwValueType, NULL, &dwLen) ==
1304 ERROR_SUCCESS)
1305 {
1306 pbKey = HeapAlloc(GetProcessHeap(), 0, dwLen);
1307 if (pbKey)
1308 {
1309 if (RegQueryValueExA(hKey, szValueName, 0, &dwValueType, pbKey, &dwLen) ==
1310 ERROR_SUCCESS)
1311 {
1312 blobIn.pbData = pbKey;
1313 blobIn.cbData = dwLen;
1314
1315 if (CryptUnprotectData(&blobIn, NULL, NULL, NULL, NULL,
1316 dwFlags, &blobOut))
1317 {
1318 ret = import_key(hKeyContainer, blobOut.pbData, blobOut.cbData, 0, 0,
1319 FALSE, phCryptKey);
1320 LocalFree(blobOut.pbData);
1321 }
1322 }
1323 HeapFree(GetProcessHeap(), 0, pbKey);
1324 }
1325 }
1326 if (ret)
1327 {
1328 CRYPTKEY *pKey;
1329
1330 if (lookup_handle(&handle_table, *phCryptKey, RSAENH_MAGIC_KEY,
1331 (OBJECTHDR**)&pKey))
1332 {
1333 if ((szValueName = map_key_spec_to_permissions_name(dwKeySpec)))
1334 {
1335 dwLen = sizeof(pKey->dwPermissions);
1336 RegQueryValueExA(hKey, szValueName, 0, NULL,
1337 (BYTE *)&pKey->dwPermissions, &dwLen);
1338 }
1339 }
1340 }
1341 return ret;
1342 }
1343
1344 /******************************************************************************
1345 * read_key_container [Internal]
1346 *
1347 * Tries to read the persistent state of the key container (mainly the signature
1348 * and key exchange private keys) given by pszContainerName.
1349 *
1350 * PARAMS
1351 * pszContainerName [I] Name of the key container to read from the registry
1352 * pVTable [I] Pointer to context data provided by the operating system
1353 *
1354 * RETURNS
1355 * Success: Handle to the key container read from the registry
1356 * Failure: INVALID_HANDLE_VALUE
1357 */
1358 static HCRYPTPROV read_key_container(PCHAR pszContainerName, DWORD dwFlags, const VTableProvStruc *pVTable)
1359 {
1360 HKEY hKey;
1361 KEYCONTAINER *pKeyContainer;
1362 HCRYPTPROV hKeyContainer;
1363 HCRYPTKEY hCryptKey;
1364
1365 if (!open_container_key(pszContainerName, dwFlags, &hKey))
1366 {
1367 SetLastError(NTE_BAD_KEYSET);
1368 return (HCRYPTPROV)INVALID_HANDLE_VALUE;
1369 }
1370
1371 hKeyContainer = new_key_container(pszContainerName, dwFlags, pVTable);
1372 if (hKeyContainer != (HCRYPTPROV)INVALID_HANDLE_VALUE)
1373 {
1374 DWORD dwProtectFlags = (dwFlags & CRYPT_MACHINE_KEYSET) ?
1375 CRYPTPROTECT_LOCAL_MACHINE : 0;
1376
1377 if (!lookup_handle(&handle_table, hKeyContainer, RSAENH_MAGIC_CONTAINER,
1378 (OBJECTHDR**)&pKeyContainer))
1379 return (HCRYPTPROV)INVALID_HANDLE_VALUE;
1380
1381 if (read_key_value(hKeyContainer, hKey, AT_KEYEXCHANGE,
1382 dwProtectFlags, &hCryptKey))
1383 pKeyContainer->hKeyExchangeKeyPair = hCryptKey;
1384 if (read_key_value(hKeyContainer, hKey, AT_SIGNATURE,
1385 dwProtectFlags, &hCryptKey))
1386 pKeyContainer->hSignatureKeyPair = hCryptKey;
1387 }
1388
1389 return hKeyContainer;
1390 }
1391
1392 /******************************************************************************
1393 * build_hash_signature [Internal]
1394 *
1395 * Builds a padded version of a hash to match the length of the RSA key modulus.
1396 *
1397 * PARAMS
1398 * pbSignature [O] The padded hash object is stored here.
1399 * dwLen [I] Length of the pbSignature buffer.
1400 * aiAlgid [I] Algorithm identifier of the hash to be padded.
1401 * abHashValue [I] The value of the hash object.
1402 * dwHashLen [I] Length of the hash value.
1403 * dwFlags [I] Selection of padding algorithm.
1404 *
1405 * RETURNS
1406 * Success: TRUE
1407 * Failure: FALSE (NTE_BAD_ALGID)
1408 */
1409 static BOOL build_hash_signature(BYTE *pbSignature, DWORD dwLen, ALG_ID aiAlgid,
1410 CONST BYTE *abHashValue, DWORD dwHashLen, DWORD dwFlags)
1411 {
1412 /* These prefixes are meant to be concatenated with hash values of the
1413 * respective kind to form a PKCS #7 DigestInfo. */
1414 static const struct tagOIDDescriptor {
1415 ALG_ID aiAlgid;
1416 DWORD dwLen;
1417 CONST BYTE abOID[18];
1418 } aOIDDescriptor[5] = {
1419 { CALG_MD2, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1420 0x86, 0xf7, 0x0d, 0x02, 0x02, 0x05, 0x00, 0x04, 0x10 } },
1421 { CALG_MD4, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1422 0x86, 0xf7, 0x0d, 0x02, 0x04, 0x05, 0x00, 0x04, 0x10 } },
1423 { CALG_MD5, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1424 0x86, 0xf7, 0x0d, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10 } },
1425 { CALG_SHA, 15, { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03,
1426 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14 } },
1427 { 0, 0, { 0 } }
1428 };
1429 DWORD dwIdxOID, i, j;
1430
1431 for (dwIdxOID = 0; aOIDDescriptor[dwIdxOID].aiAlgid; dwIdxOID++) {
1432 if (aOIDDescriptor[dwIdxOID].aiAlgid == aiAlgid) break;
1433 }
1434
1435 if (!aOIDDescriptor[dwIdxOID].aiAlgid) {
1436 SetLastError(NTE_BAD_ALGID);
1437 return FALSE;
1438 }
1439
1440 /* Build the padded signature */
1441 if (dwFlags & CRYPT_X931_FORMAT) {
1442 pbSignature[0] = 0x6b;
1443 for (i=1; i < dwLen - dwHashLen - 3; i++) {
1444 pbSignature[i] = 0xbb;
1445 }
1446 pbSignature[i++] = 0xba;
1447 for (j=0; j < dwHashLen; j++, i++) {
1448 pbSignature[i] = abHashValue[j];
1449 }
1450 pbSignature[i++] = 0x33;
1451 pbSignature[i++] = 0xcc;
1452 } else {
1453 pbSignature[0] = 0x00;
1454 pbSignature[1] = 0x01;
1455 if (dwFlags & CRYPT_NOHASHOID) {
1456 for (i=2; i < dwLen - 1 - dwHashLen; i++) {
1457 pbSignature[i] = 0xff;
1458 }
1459 pbSignature[i++] = 0x00;
1460 } else {
1461 for (i=2; i < dwLen - 1 - aOIDDescriptor[dwIdxOID].dwLen - dwHashLen; i++) {
1462 pbSignature[i] = 0xff;
1463 }
1464 pbSignature[i++] = 0x00;
1465 for (j=0; j < aOIDDescriptor[dwIdxOID].dwLen; j++) {
1466 pbSignature[i++] = aOIDDescriptor[dwIdxOID].abOID[j];
1467 }
1468 }
1469 for (j=0; j < dwHashLen; j++) {
1470 pbSignature[i++] = abHashValue[j];
1471 }
1472 }
1473
1474 return TRUE;
1475 }
1476
1477 /******************************************************************************
1478 * tls1_p [Internal]
1479 *
1480 * This is an implementation of the 'P_hash' helper function for TLS1's PRF.
1481 * It is used exclusively by tls1_prf. For details see RFC 2246, chapter 5.
1482 * The pseudo random stream generated by this function is exclusive or'ed with
1483 * the data in pbBuffer.
1484 *
1485 * PARAMS
1486 * hHMAC [I] HMAC object, which will be used in pseudo random generation
1487 * pblobSeed [I] Seed value
1488 * pbBuffer [I/O] Pseudo random stream will be xor'ed to the provided data
1489 * dwBufferLen [I] Number of pseudo random bytes desired
1490 *
1491 * RETURNS
1492 * Success: TRUE
1493 * Failure: FALSE
1494 */
1495 static BOOL tls1_p(HCRYPTHASH hHMAC, CONST PCRYPT_DATA_BLOB pblobSeed, PBYTE pbBuffer, DWORD dwBufferLen)
1496 {
1497 CRYPTHASH *pHMAC;
1498 BYTE abAi[RSAENH_MAX_HASH_SIZE];
1499 DWORD i = 0;
1500
1501 if (!lookup_handle(&handle_table, hHMAC, RSAENH_MAGIC_HASH, (OBJECTHDR**)&pHMAC)) {
1502 SetLastError(NTE_BAD_HASH);
1503 return FALSE;
1504 }
1505
1506 /* compute A_1 = HMAC(seed) */
1507 init_hash(pHMAC);
1508 update_hash(pHMAC, pblobSeed->pbData, pblobSeed->cbData);
1509 finalize_hash(pHMAC);
1510 memcpy(abAi, pHMAC->abHashValue, pHMAC->dwHashSize);
1511
1512 do {
1513 /* compute HMAC(A_i + seed) */
1514 init_hash(pHMAC);
1515 update_hash(pHMAC, abAi, pHMAC->dwHashSize);
1516 update_hash(pHMAC, pblobSeed->pbData, pblobSeed->cbData);
1517 finalize_hash(pHMAC);
1518
1519 /* pseudo random stream := CONCAT_{i=1..n} ( HMAC(A_i + seed) ) */
1520 do {
1521 if (i >= dwBufferLen) break;
1522 pbBuffer[i] ^= pHMAC->abHashValue[i % pHMAC->dwHashSize];
1523 i++;
1524 } while (i % pHMAC->dwHashSize);
1525
1526 /* compute A_{i+1} = HMAC(A_i) */
1527 init_hash(pHMAC);
1528 update_hash(pHMAC, abAi, pHMAC->dwHashSize);
1529 finalize_hash(pHMAC);
1530 memcpy(abAi, pHMAC->abHashValue, pHMAC->dwHashSize);
1531 } while (i < dwBufferLen);
1532
1533 return TRUE;
1534 }
1535
1536 /******************************************************************************
1537 * tls1_prf [Internal]
1538 *
1539 * TLS1 pseudo random function as specified in RFC 2246, chapter 5
1540 *
1541 * PARAMS
1542 * hProv [I] Key container used to compute the pseudo random stream
1543 * hSecret [I] Key that holds the (pre-)master secret
1544 * pblobLabel [I] Descriptive label
1545 * pblobSeed [I] Seed value
1546 * pbBuffer [O] Pseudo random numbers will be stored here
1547 * dwBufferLen [I] Number of pseudo random bytes desired
1548 *
1549 * RETURNS
1550 * Success: TRUE
1551 * Failure: FALSE
1552 */
1553 static BOOL tls1_prf(HCRYPTPROV hProv, HCRYPTPROV hSecret, CONST PCRYPT_DATA_BLOB pblobLabel,
1554 CONST PCRYPT_DATA_BLOB pblobSeed, PBYTE pbBuffer, DWORD dwBufferLen)
1555 {
1556 HMAC_INFO hmacInfo = { 0, NULL, 0, NULL, 0 };
1557 HCRYPTHASH hHMAC = (HCRYPTHASH)INVALID_HANDLE_VALUE;
1558 HCRYPTKEY hHalfSecret = (HCRYPTKEY)INVALID_HANDLE_VALUE;
1559 CRYPTKEY *pHalfSecret, *pSecret;
1560 DWORD dwHalfSecretLen;
1561 BOOL result = FALSE;
1562 CRYPT_DATA_BLOB blobLabelSeed;
1563
1564 TRACE("(hProv=%08lx, hSecret=%08lx, pblobLabel=%p, pblobSeed=%p, pbBuffer=%p, dwBufferLen=%d)\n",
1565 hProv, hSecret, pblobLabel, pblobSeed, pbBuffer, dwBufferLen);
1566
1567 if (!lookup_handle(&handle_table, hSecret, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pSecret)) {
1568 SetLastError(NTE_FAIL);
1569 return FALSE;
1570 }
1571
1572 dwHalfSecretLen = (pSecret->dwKeyLen+1)/2;
1573
1574 /* concatenation of the label and the seed */
1575 if (!concat_data_blobs(&blobLabelSeed, pblobLabel, pblobSeed)) goto exit;
1576
1577 /* zero out the buffer, since two random streams will be xor'ed into it. */
1578 memset(pbBuffer, 0, dwBufferLen);
1579
1580 /* build a 'fake' key, to hold the secret. CALG_SSL2_MASTER is used since it provides
1581 * the biggest range of valid key lengths. */
1582 hHalfSecret = new_key(hProv, CALG_SSL2_MASTER, MAKELONG(0,dwHalfSecretLen*8), &pHalfSecret);
1583 if (hHalfSecret == (HCRYPTKEY)INVALID_HANDLE_VALUE) goto exit;
1584
1585 /* Derive an HMAC_MD5 hash and call the helper function. */
1586 memcpy(pHalfSecret->abKeyValue, pSecret->abKeyValue, dwHalfSecretLen);
1587 if (!RSAENH_CPCreateHash(hProv, CALG_HMAC, hHalfSecret, 0, &hHMAC)) goto exit;
1588 hmacInfo.HashAlgid = CALG_MD5;
1589 if (!RSAENH_CPSetHashParam(hProv, hHMAC, HP_HMAC_INFO, (BYTE*)&hmacInfo, 0)) goto exit;
1590 if (!tls1_p(hHMAC, &blobLabelSeed, pbBuffer, dwBufferLen)) goto exit;
1591
1592 /* Reconfigure to HMAC_SHA hash and call helper function again. */
1593 memcpy(pHalfSecret->abKeyValue, pSecret->abKeyValue + (pSecret->dwKeyLen/2), dwHalfSecretLen);
1594 hmacInfo.HashAlgid = CALG_SHA;
1595 if (!RSAENH_CPSetHashParam(hProv, hHMAC, HP_HMAC_INFO, (BYTE*)&hmacInfo, 0)) goto exit;
1596 if (!tls1_p(hHMAC, &blobLabelSeed, pbBuffer, dwBufferLen)) goto exit;
1597
1598 result = TRUE;
1599 exit:
1600 release_handle(&handle_table, hHalfSecret, RSAENH_MAGIC_KEY);
1601 if (hHMAC != (HCRYPTHASH)INVALID_HANDLE_VALUE) RSAENH_CPDestroyHash(hProv, hHMAC);
1602 free_data_blob(&blobLabelSeed);
1603 return result;
1604 }
1605
1606 /******************************************************************************
1607 * pad_data [Internal]
1608 *
1609 * Helper function for data padding according to PKCS1 #2
1610 *
1611 * PARAMS
1612 * abData [I] The data to be padded
1613 * dwDataLen [I] Length of the data
1614 * abBuffer [O] Padded data will be stored here
1615 * dwBufferLen [I] Length of the buffer (also length of padded data)
1616 * dwFlags [I] Padding format (CRYPT_SSL2_FALLBACK)
1617 *
1618 * RETURN
1619 * Success: TRUE
1620 * Failure: FALSE (NTE_BAD_LEN, too much data to pad)
1621 */
1622 static BOOL pad_data(CONST BYTE *abData, DWORD dwDataLen, BYTE *abBuffer, DWORD dwBufferLen,
1623 DWORD dwFlags)
1624 {
1625 DWORD i;
1626
1627 /* Ensure there is enough space for PKCS1 #2 padding */
1628 if (dwDataLen > dwBufferLen-11) {
1629 SetLastError(NTE_BAD_LEN);
1630 return FALSE;
1631 }
1632
1633 memmove(abBuffer + dwBufferLen - dwDataLen, abData, dwDataLen);
1634
1635 abBuffer[0] = 0x00;
1636 abBuffer[1] = RSAENH_PKC_BLOCKTYPE;
1637 for (i=2; i < dwBufferLen - dwDataLen - 1; i++)
1638 do gen_rand_impl(&abBuffer[i], 1); while (!abBuffer[i]);
1639 if (dwFlags & CRYPT_SSL2_FALLBACK)
1640 for (i-=8; i < dwBufferLen - dwDataLen - 1; i++)
1641 abBuffer[i] = 0x03;
1642 abBuffer[i] = 0x00;
1643
1644 return TRUE;
1645 }
1646
1647 /******************************************************************************
1648 * unpad_data [Internal]
1649 *
1650 * Remove the PKCS1 padding from RSA decrypted data
1651 *
1652 * PARAMS
1653 * abData [I] The padded data
1654 * dwDataLen [I] Length of the padded data
1655 * abBuffer [O] Data without padding will be stored here
1656 * dwBufferLen [I/O] I: Length of the buffer, O: Length of unpadded data
1657 * dwFlags [I] Currently none defined
1658 *
1659 * RETURNS
1660 * Success: TRUE
1661 * Failure: FALSE, (NTE_BAD_DATA, no valid PKCS1 padding or buffer too small)
1662 */
1663 static BOOL unpad_data(CONST BYTE *abData, DWORD dwDataLen, BYTE *abBuffer, DWORD *dwBufferLen,
1664 DWORD dwFlags)
1665 {
1666 DWORD i;
1667
1668 for (i=2; i<dwDataLen; i++)
1669 if (!abData[i])
1670 break;
1671
1672 if ((i == dwDataLen) || (*dwBufferLen < dwDataLen - i - 1) ||
1673 (abData[0] != 0x00) || (abData[1] != RSAENH_PKC_BLOCKTYPE))
1674 {
1675 SetLastError(NTE_BAD_DATA);
1676 return FALSE;
1677 }
1678
1679 *dwBufferLen = dwDataLen - i - 1;
1680 memmove(abBuffer, abData + i + 1, *dwBufferLen);
1681 return TRUE;
1682 }
1683
1684 /******************************************************************************
1685 * CPAcquireContext (RSAENH.@)
1686 *
1687 * Acquire a handle to the key container specified by pszContainer
1688 *
1689 * PARAMS
1690 * phProv [O] Pointer to the location the acquired handle will be written to.
1691 * pszContainer [I] Name of the desired key container. See Notes
1692 * dwFlags [I] Flags. See Notes.
1693 * pVTable [I] Pointer to a PVTableProvStruct containing callbacks.
1694 *
1695 * RETURNS
1696 * Success: TRUE
1697 * Failure: FALSE
1698 *
1699 * NOTES
1700 * If pszContainer is NULL or points to a zero length string the user's login
1701 * name will be used as the key container name.
1702 *
1703 * If the CRYPT_NEW_KEYSET flag is set in dwFlags a new keyset will be created.
1704 * If a keyset with the given name already exists, the function fails and sets
1705 * last error to NTE_EXISTS. If CRYPT_NEW_KEYSET is not set and the specified
1706 * key container does not exist, function fails and sets last error to
1707 * NTE_BAD_KEYSET.
1708 */
1709 BOOL WINAPI RSAENH_CPAcquireContext(HCRYPTPROV *phProv, LPSTR pszContainer,
1710 DWORD dwFlags, PVTableProvStruc pVTable)
1711 {
1712 CHAR szKeyContainerName[MAX_PATH];
1713
1714 TRACE("(phProv=%p, pszContainer=%s, dwFlags=%08x, pVTable=%p)\n", phProv,
1715 debugstr_a(pszContainer), dwFlags, pVTable);
1716
1717 if (pszContainer && *pszContainer)
1718 {
1719 lstrcpynA(szKeyContainerName, pszContainer, MAX_PATH);
1720 }
1721 else
1722 {
1723 DWORD dwLen = sizeof(szKeyContainerName);
1724 if (!GetUserNameA(szKeyContainerName, &dwLen)) return FALSE;
1725 }
1726
1727 switch (dwFlags & (CRYPT_NEWKEYSET|CRYPT_VERIFYCONTEXT|CRYPT_DELETEKEYSET))
1728 {
1729 case 0:
1730 *phProv = read_key_container(szKeyContainerName, dwFlags, pVTable);
1731 break;
1732
1733 case CRYPT_DELETEKEYSET:
1734 return delete_container_key(szKeyContainerName, dwFlags);
1735
1736 case CRYPT_NEWKEYSET:
1737 *phProv = read_key_container(szKeyContainerName, dwFlags, pVTable);
1738 if (*phProv != (HCRYPTPROV)INVALID_HANDLE_VALUE)
1739 {
1740 release_handle(&handle_table, *phProv, RSAENH_MAGIC_CONTAINER);
1741 TRACE("Can't create new keyset, already exists\n");
1742 SetLastError(NTE_EXISTS);
1743 return FALSE;
1744 }
1745 *phProv = new_key_container(szKeyContainerName, dwFlags, pVTable);
1746 break;
1747
1748 case CRYPT_VERIFYCONTEXT|CRYPT_NEWKEYSET:
1749 case CRYPT_VERIFYCONTEXT:
1750 if (pszContainer && *pszContainer) {
1751 TRACE("pszContainer should be empty\n");
1752 SetLastError(NTE_BAD_FLAGS);
1753 return FALSE;
1754 }
1755 *phProv = new_key_container("", dwFlags, pVTable);
1756 break;
1757
1758 default:
1759 *phProv = (HCRYPTPROV)INVALID_HANDLE_VALUE;
1760 SetLastError(NTE_BAD_FLAGS);
1761 return FALSE;
1762 }
1763
1764 if (*phProv != (HCRYPTPROV)INVALID_HANDLE_VALUE) {
1765 SetLastError(ERROR_SUCCESS);
1766 return TRUE;
1767 } else {
1768 return FALSE;
1769 }
1770 }
1771
1772 /******************************************************************************
1773 * CPCreateHash (RSAENH.@)
1774 *
1775 * CPCreateHash creates and initalizes a new hash object.
1776 *
1777 * PARAMS
1778 * hProv [I] Handle to the key container to which the new hash will belong.
1779 * Algid [I] Identifies the hash algorithm, which will be used for the hash.
1780 * hKey [I] Handle to a session key applied for keyed hashes.
1781 * dwFlags [I] Currently no flags defined. Must be zero.
1782 * phHash [O] Points to the location where a handle to the new hash will be stored.
1783 *
1784 * RETURNS
1785 * Success: TRUE
1786 * Failure: FALSE
1787 *
1788 * NOTES
1789 * hKey is a handle to a session key applied in keyed hashes like MAC and HMAC.
1790 * If a normal hash object is to be created (like e.g. MD2 or SHA1) hKey must be zero.
1791 */
1792 BOOL WINAPI RSAENH_CPCreateHash(HCRYPTPROV hProv, ALG_ID Algid, HCRYPTKEY hKey, DWORD dwFlags,
1793 HCRYPTHASH *phHash)
1794 {
1795 CRYPTKEY *pCryptKey;
1796 CRYPTHASH *pCryptHash;
1797 const PROV_ENUMALGS_EX *peaAlgidInfo;
1798
1799 TRACE("(hProv=%08lx, Algid=%08x, hKey=%08lx, dwFlags=%08x, phHash=%p)\n", hProv, Algid, hKey,
1800 dwFlags, phHash);
1801
1802 peaAlgidInfo = get_algid_info(hProv, Algid);
1803 if (!peaAlgidInfo) return FALSE;
1804
1805 if (dwFlags)
1806 {
1807 SetLastError(NTE_BAD_FLAGS);
1808 return FALSE;
1809 }
1810
1811 if (Algid == CALG_MAC || Algid == CALG_HMAC || Algid == CALG_SCHANNEL_MASTER_HASH ||
1812 Algid == CALG_TLS1PRF)
1813 {
1814 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey)) {
1815 SetLastError(NTE_BAD_KEY);
1816 return FALSE;
1817 }
1818
1819 if ((Algid == CALG_MAC) && (GET_ALG_TYPE(pCryptKey->aiAlgid) != ALG_TYPE_BLOCK)) {
1820 SetLastError(NTE_BAD_KEY);
1821 return FALSE;
1822 }
1823
1824 if ((Algid == CALG_SCHANNEL_MASTER_HASH || Algid == CALG_TLS1PRF) &&
1825 (pCryptKey->aiAlgid != CALG_TLS1_MASTER))
1826 {
1827 SetLastError(NTE_BAD_KEY);
1828 return FALSE;
1829 }
1830
1831 if ((Algid == CALG_TLS1PRF) && (pCryptKey->dwState != RSAENH_KEYSTATE_MASTERKEY)) {
1832 SetLastError(NTE_BAD_KEY_STATE);
1833 return FALSE;
1834 }
1835 }
1836
1837 *phHash = new_object(&handle_table, sizeof(CRYPTHASH), RSAENH_MAGIC_HASH,
1838 destroy_hash, (OBJECTHDR**)&pCryptHash);
1839 if (!pCryptHash) return FALSE;
1840
1841 pCryptHash->aiAlgid = Algid;
1842 pCryptHash->hKey = hKey;
1843 pCryptHash->hProv = hProv;
1844 pCryptHash->dwState = RSAENH_HASHSTATE_HASHING;
1845 pCryptHash->pHMACInfo = NULL;
1846 pCryptHash->dwHashSize = peaAlgidInfo->dwDefaultLen >> 3;
1847 init_data_blob(&pCryptHash->tpPRFParams.blobLabel);
1848 init_data_blob(&pCryptHash->tpPRFParams.blobSeed);
1849
1850 if (Algid == CALG_SCHANNEL_MASTER_HASH) {
1851 static const char keyex[] = "key expansion";
1852 BYTE key_expansion[sizeof keyex];
1853 CRYPT_DATA_BLOB blobRandom, blobKeyExpansion = { 13, key_expansion };
1854
1855 memcpy( key_expansion, keyex, sizeof keyex );
1856
1857 if (pCryptKey->dwState != RSAENH_KEYSTATE_MASTERKEY) {
1858 static const char msec[] = "master secret";
1859 BYTE master_secret[sizeof msec];
1860 CRYPT_DATA_BLOB blobLabel = { 13, master_secret };
1861 BYTE abKeyValue[48];
1862
1863 memcpy( master_secret, msec, sizeof msec );
1864
1865 /* See RFC 2246, chapter 8.1 */
1866 if (!concat_data_blobs(&blobRandom,
1867 &pCryptKey->siSChannelInfo.blobClientRandom,
1868 &pCryptKey->siSChannelInfo.blobServerRandom))
1869 {
1870 return FALSE;
1871 }
1872 tls1_prf(hProv, hKey, &blobLabel, &blobRandom, abKeyValue, 48);
1873 pCryptKey->dwState = RSAENH_KEYSTATE_MASTERKEY;
1874 memcpy(pCryptKey->abKeyValue, abKeyValue, 48);
1875 free_data_blob(&blobRandom);
1876 }
1877
1878 /* See RFC 2246, chapter 6.3 */
1879 if (!concat_data_blobs(&blobRandom,
1880 &pCryptKey->siSChannelInfo.blobServerRandom,
1881 &pCryptKey->siSChannelInfo.blobClientRandom))
1882 {
1883 return FALSE;
1884 }
1885 tls1_prf(hProv, hKey, &blobKeyExpansion, &blobRandom, pCryptHash->abHashValue,
1886 RSAENH_MAX_HASH_SIZE);
1887 free_data_blob(&blobRandom);
1888 }
1889
1890 return init_hash(pCryptHash);
1891 }
1892
1893 /******************************************************************************
1894 * CPDestroyHash (RSAENH.@)
1895 *
1896 * Releases the handle to a hash object. The object is destroyed if it's reference
1897 * count reaches zero.
1898 *
1899 * PARAMS
1900 * hProv [I] Handle to the key container to which the hash object belongs.
1901 * hHash [I] Handle to the hash object to be released.
1902 *
1903 * RETURNS
1904 * Success: TRUE
1905 * Failure: FALSE
1906 */
1907 BOOL WINAPI RSAENH_CPDestroyHash(HCRYPTPROV hProv, HCRYPTHASH hHash)
1908 {
1909 TRACE("(hProv=%08lx, hHash=%08lx)\n", hProv, hHash);
1910
1911 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
1912 {
1913 SetLastError(NTE_BAD_UID);
1914 return FALSE;
1915 }
1916
1917 if (!release_handle(&handle_table, hHash, RSAENH_MAGIC_HASH))
1918 {
1919 SetLastError(NTE_BAD_HASH);
1920 return FALSE;
1921 }
1922
1923 return TRUE;
1924 }
1925
1926 /******************************************************************************
1927 * CPDestroyKey (RSAENH.@)
1928 *
1929 * Releases the handle to a key object. The object is destroyed if it's reference
1930 * count reaches zero.
1931 *
1932 * PARAMS
1933 * hProv [I] Handle to the key container to which the key object belongs.
1934 * hKey [I] Handle to the key object to be released.
1935 *
1936 * RETURNS
1937 * Success: TRUE
1938 * Failure: FALSE
1939 */
1940 BOOL WINAPI RSAENH_CPDestroyKey(HCRYPTPROV hProv, HCRYPTKEY hKey)
1941 {
1942 TRACE("(hProv=%08lx, hKey=%08lx)\n", hProv, hKey);
1943
1944 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
1945 {
1946 SetLastError(NTE_BAD_UID);
1947 return FALSE;
1948 }
1949
1950 if (!release_handle(&handle_table, hKey, RSAENH_MAGIC_KEY))
1951 {
1952 SetLastError(NTE_BAD_KEY);
1953 return FALSE;
1954 }
1955
1956 return TRUE;
1957 }
1958
1959 /******************************************************************************
1960 * CPDuplicateHash (RSAENH.@)
1961 *
1962 * Clones a hash object including it's current state.
1963 *
1964 * PARAMS
1965 * hUID [I] Handle to the key container the hash belongs to.
1966 * hHash [I] Handle to the hash object to be cloned.
1967 * pdwReserved [I] Reserved. Must be NULL.
1968 * dwFlags [I] No flags are currently defined. Must be 0.
1969 * phHash [O] Handle to the cloned hash object.
1970 *
1971 * RETURNS
1972 * Success: TRUE.
1973 * Failure: FALSE.
1974 */
1975 BOOL WINAPI RSAENH_CPDuplicateHash(HCRYPTPROV hUID, HCRYPTHASH hHash, DWORD *pdwReserved,
1976 DWORD dwFlags, HCRYPTHASH *phHash)
1977 {
1978 CRYPTHASH *pSrcHash, *pDestHash;
1979
1980 TRACE("(hUID=%08lx, hHash=%08lx, pdwReserved=%p, dwFlags=%08x, phHash=%p)\n", hUID, hHash,
1981 pdwReserved, dwFlags, phHash);
1982
1983 if (!is_valid_handle(&handle_table, hUID, RSAENH_MAGIC_CONTAINER))
1984 {
1985 SetLastError(NTE_BAD_UID);
1986 return FALSE;
1987 }
1988
1989 if (!lookup_handle(&handle_table, hHash, RSAENH_MAGIC_HASH, (OBJECTHDR**)&pSrcHash))
1990 {
1991 SetLastError(NTE_BAD_HASH);
1992 return FALSE;
1993 }
1994
1995 if (!phHash || pdwReserved || dwFlags)
1996 {
1997 SetLastError(ERROR_INVALID_PARAMETER);
1998 return FALSE;
1999 }
2000
2001 *phHash = new_object(&handle_table, sizeof(CRYPTHASH), RSAENH_MAGIC_HASH,
2002 destroy_hash, (OBJECTHDR**)&pDestHash);
2003 if (*phHash != (HCRYPTHASH)INVALID_HANDLE_VALUE)
2004 {
2005 *pDestHash = *pSrcHash;
2006 duplicate_hash_impl(pSrcHash->aiAlgid, &pSrcHash->context, &pDestHash->context);
2007 copy_hmac_info(&pDestHash->pHMACInfo, pSrcHash->pHMACInfo);
2008 copy_data_blob(&pDestHash->tpPRFParams.blobLabel, &pSrcHash->tpPRFParams.blobLabel);
2009 copy_data_blob(&pDestHash->tpPRFParams.blobSeed, &pSrcHash->tpPRFParams.blobSeed);
2010 }
2011
2012 return *phHash != (HCRYPTHASH)INVALID_HANDLE_VALUE;
2013 }
2014
2015 /******************************************************************************
2016 * CPDuplicateKey (RSAENH.@)
2017 *
2018 * Clones a key object including it's current state.
2019 *
2020 * PARAMS
2021 * hUID [I] Handle to the key container the hash belongs to.
2022 * hKey [I] Handle to the key object to be cloned.
2023 * pdwReserved [I] Reserved. Must be NULL.
2024 * dwFlags [I] No flags are currently defined. Must be 0.
2025 * phHash [O] Handle to the cloned key object.
2026 *
2027 * RETURNS
2028 * Success: TRUE.
2029 * Failure: FALSE.
2030 */
2031 BOOL WINAPI RSAENH_CPDuplicateKey(HCRYPTPROV hUID, HCRYPTKEY hKey, DWORD *pdwReserved,
2032 DWORD dwFlags, HCRYPTKEY *phKey)
2033 {
2034 CRYPTKEY *pSrcKey, *pDestKey;
2035
2036 TRACE("(hUID=%08lx, hKey=%08lx, pdwReserved=%p, dwFlags=%08x, phKey=%p)\n", hUID, hKey,
2037 pdwReserved, dwFlags, phKey);
2038
2039 if (!is_valid_handle(&handle_table, hUID, RSAENH_MAGIC_CONTAINER))
2040 {
2041 SetLastError(NTE_BAD_UID);
2042 return FALSE;
2043 }
2044
2045 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pSrcKey))
2046 {
2047 SetLastError(NTE_BAD_KEY);
2048 return FALSE;
2049 }
2050
2051 if (!phKey || pdwReserved || dwFlags)
2052 {
2053 SetLastError(ERROR_INVALID_PARAMETER);
2054 return FALSE;
2055 }
2056
2057 *phKey = new_object(&handle_table, sizeof(CRYPTKEY), RSAENH_MAGIC_KEY, destroy_key,
2058 (OBJECTHDR**)&pDestKey);
2059 if (*phKey != (HCRYPTKEY)INVALID_HANDLE_VALUE)
2060 {
2061 *pDestKey = *pSrcKey;
2062 copy_data_blob(&pDestKey->siSChannelInfo.blobServerRandom,
2063 &pSrcKey->siSChannelInfo.blobServerRandom);
2064 copy_data_blob(&pDestKey->siSChannelInfo.blobClientRandom,
2065 &pSrcKey->siSChannelInfo.blobClientRandom);
2066 duplicate_key_impl(pSrcKey->aiAlgid, &pSrcKey->context, &pDestKey->context);
2067 return TRUE;
2068 }
2069 else
2070 {
2071 return FALSE;
2072 }
2073 }
2074
2075 /******************************************************************************
2076 * CPEncrypt (RSAENH.@)
2077 *
2078 * Encrypt data.
2079 *
2080 * PARAMS
2081 * hProv [I] The key container hKey and hHash belong to.
2082 * hKey [I] The key used to encrypt the data.
2083 * hHash [I] An optional hash object for parallel hashing. See notes.
2084 * Final [I] Indicates if this is the last block of data to encrypt.
2085 * dwFlags [I] Currently no flags defined. Must be zero.
2086 * pbData [I/O] Pointer to the data to encrypt. Encrypted data will also be stored there.
2087 * pdwDataLen [I/O] I: Length of data to encrypt, O: Length of encrypted data.
2088 * dwBufLen [I] Size of the buffer at pbData.
2089 *
2090 * RETURNS
2091 * Success: TRUE.
2092 * Failure: FALSE.
2093 *
2094 * NOTES
2095 * If a hash object handle is provided in hHash, it will be updated with the plaintext.
2096 * This is useful for message signatures.
2097 *
2098 * This function uses the standard WINAPI protocol for querying data of dynamic length.
2099 */
2100 BOOL WINAPI RSAENH_CPEncrypt(HCRYPTPROV hProv, HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final,
2101 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen, DWORD dwBufLen)
2102 {
2103 CRYPTKEY *pCryptKey;
2104 BYTE *in, out[RSAENH_MAX_BLOCK_SIZE], o[RSAENH_MAX_BLOCK_SIZE];
2105 DWORD dwEncryptedLen, i, j, k;
2106
2107 TRACE("(hProv=%08lx, hKey=%08lx, hHash=%08lx, Final=%d, dwFlags=%08x, pbData=%p, "
2108 "pdwDataLen=%p, dwBufLen=%d)\n", hProv, hKey, hHash, Final, dwFlags, pbData, pdwDataLen,
2109 dwBufLen);
2110
2111 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
2112 {
2113 SetLastError(NTE_BAD_UID);
2114 return FALSE;
2115 }
2116
2117 if (dwFlags)
2118 {
2119 SetLastError(NTE_BAD_FLAGS);
2120 return FALSE;
2121 }
2122
2123 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
2124 {
2125 SetLastError(NTE_BAD_KEY);
2126 return FALSE;
2127 }
2128
2129 if (pCryptKey->dwState == RSAENH_KEYSTATE_IDLE)
2130 pCryptKey->dwState = RSAENH_KEYSTATE_ENCRYPTING;
2131
2132 if (pCryptKey->dwState != RSAENH_KEYSTATE_ENCRYPTING)
2133 {
2134 SetLastError(NTE_BAD_DATA);
2135 return FALSE;
2136 }
2137
2138 if (is_valid_handle(&handle_table, hHash, RSAENH_MAGIC_HASH)) {
2139 if (!RSAENH_CPHashData(hProv, hHash, pbData, *pdwDataLen, 0)) return FALSE;
2140 }
2141
2142 if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_BLOCK) {
2143 if (!Final && (*pdwDataLen % pCryptKey->dwBlockLen)) {
2144 SetLastError(NTE_BAD_DATA);
2145 return FALSE;
2146 }
2147
2148 dwEncryptedLen = (*pdwDataLen/pCryptKey->dwBlockLen+(Final?1:0))*pCryptKey->dwBlockLen;
2149
2150 if (pbData == NULL) {
2151 *pdwDataLen = dwEncryptedLen;
2152 return TRUE;
2153 }
2154 else if (dwEncryptedLen > dwBufLen) {
2155 *pdwDataLen = dwEncryptedLen;
2156 SetLastError(ERROR_MORE_DATA);
2157 return FALSE;
2158 }
2159
2160 /* Pad final block with length bytes */
2161 for (i=*pdwDataLen; i<dwEncryptedLen; i++) pbData[i] = dwEncryptedLen - *pdwDataLen;
2162 *pdwDataLen = dwEncryptedLen;
2163
2164 for (i=0, in=pbData; i<*pdwDataLen; i+=pCryptKey->dwBlockLen, in+=pCryptKey->dwBlockLen) {
2165 switch (pCryptKey->dwMode) {
2166 case CRYPT_MODE_ECB:
2167 encrypt_block_impl(pCryptKey->aiAlgid, 0, &pCryptKey->context, in, out,
2168 RSAENH_ENCRYPT);
2169 break;
2170
2171 case CRYPT_MODE_CBC:
2172 for (j=0; j<pCryptKey->dwBlockLen; j++) in[j] ^= pCryptKey->abChainVector[j];
2173 encrypt_block_impl(pCryptKey->aiAlgid, 0, &pCryptKey->context, in, out,
2174 RSAENH_ENCRYPT);
2175 memcpy(pCryptKey->abChainVector, out, pCryptKey->dwBlockLen);
2176 break;
2177
2178 case CRYPT_MODE_CFB:
2179 for (j=0; j<pCryptKey->dwBlockLen; j++) {
2180 encrypt_block_impl(pCryptKey->aiAlgid, 0, &pCryptKey->context,
2181 pCryptKey->abChainVector, o, RSAENH_ENCRYPT);
2182 out[j] = in[j] ^ o[0];
2183 for (k=0; k<pCryptKey->dwBlockLen-1; k++)
2184 pCryptKey->abChainVector[k] = pCryptKey->abChainVector[k+1];
2185 pCryptKey->abChainVector[k] = out[j];
2186 }
2187 break;
2188
2189 default:
2190 SetLastError(NTE_BAD_ALGID);
2191 return FALSE;
2192 }
2193 memcpy(in, out, pCryptKey->dwBlockLen);
2194 }
2195 } else if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_STREAM) {
2196 if (pbData == NULL) {
2197 *pdwDataLen = dwBufLen;
2198 return TRUE;
2199 }
2200 encrypt_stream_impl(pCryptKey->aiAlgid, &pCryptKey->context, pbData, *pdwDataLen);
2201 } else if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_RSA) {
2202 if (pCryptKey->aiAlgid == CALG_RSA_SIGN) {
2203 SetLastError(NTE_BAD_KEY);
2204 return FALSE;
2205 }
2206 if (!pbData) {
2207 *pdwDataLen = pCryptKey->dwBlockLen;
2208 return TRUE;
2209 }
2210 if (dwBufLen < pCryptKey->dwBlockLen) {
2211 SetLastError(ERROR_MORE_DATA);
2212 return FALSE;
2213 }
2214 if (!pad_data(pbData, *pdwDataLen, pbData, pCryptKey->dwBlockLen, dwFlags)) return FALSE;
2215 encrypt_block_impl(pCryptKey->aiAlgid, PK_PUBLIC, &pCryptKey->context, pbData, pbData, RSAENH_ENCRYPT);
2216 *pdwDataLen = pCryptKey->dwBlockLen;
2217 Final = TRUE;
2218 } else {
2219 SetLastError(NTE_BAD_TYPE);
2220 return FALSE;
2221 }
2222
2223 if (Final) setup_key(pCryptKey);
2224
2225 return TRUE;
2226 }
2227
2228 /******************************************************************************
2229 * CPDecrypt (RSAENH.@)
2230 *
2231 * Decrypt data.
2232 *
2233 * PARAMS
2234 * hProv [I] The key container hKey and hHash belong to.
2235 * hKey [I] The key used to decrypt the data.
2236 * hHash [I] An optional hash object for parallel hashing. See notes.
2237 * Final [I] Indicates if this is the last block of data to decrypt.
2238 * dwFlags [I] Currently no flags defined. Must be zero.
2239 * pbData [I/O] Pointer to the data to decrypt. Plaintext will also be stored there.
2240 * pdwDataLen [I/O] I: Length of ciphertext, O: Length of plaintext.
2241 *
2242 * RETURNS
2243 * Success: TRUE.
2244 * Failure: FALSE.
2245 *
2246 * NOTES
2247 * If a hash object handle is provided in hHash, it will be updated with the plaintext.
2248 * This is useful for message signatures.
2249 *
2250 * This function uses the standard WINAPI protocol for querying data of dynamic length.
2251 */
2252 BOOL WINAPI RSAENH_CPDecrypt(HCRYPTPROV hProv, HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final,
2253 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
2254 {
2255 CRYPTKEY *pCryptKey;
2256 BYTE *in, out[RSAENH_MAX_BLOCK_SIZE], o[RSAENH_MAX_BLOCK_SIZE];
2257 DWORD i, j, k;
2258 DWORD dwMax;
2259
2260 TRACE("(hProv=%08lx, hKey=%08lx, hHash=%08lx, Final=%d, dwFlags=%08x, pbData=%p, "
2261 "pdwDataLen=%p)\n", hProv, hKey, hHash, Final, dwFlags, pbData, pdwDataLen);
2262
2263 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
2264 {
2265 SetLastError(NTE_BAD_UID);
2266 return FALSE;
2267 }
2268
2269 if (dwFlags)
2270 {
2271 SetLastError(NTE_BAD_FLAGS);
2272 return FALSE;
2273 }
2274
2275 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
2276 {
2277 SetLastError(NTE_BAD_KEY);
2278 return FALSE;
2279 }
2280
2281 if (pCryptKey->dwState == RSAENH_KEYSTATE_IDLE)
2282 pCryptKey->dwState = RSAENH_KEYSTATE_ENCRYPTING;
2283
2284 if (pCryptKey->dwState != RSAENH_KEYSTATE_ENCRYPTING)
2285 {
2286 SetLastError(NTE_BAD_DATA);
2287 return FALSE;
2288 }
2289
2290 dwMax=*pdwDataLen;
2291
2292 if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_BLOCK) {
2293 for (i=0, in=pbData; i<*pdwDataLen; i+=pCryptKey->dwBlockLen, in+=pCryptKey->dwBlockLen) {
2294 switch (pCryptKey->dwMode) {
2295 case CRYPT_MODE_ECB:
2296 encrypt_block_impl(pCryptKey->aiAlgid, 0, &pCryptKey->context, in, out,
2297 RSAENH_DECRYPT);
2298 break;
2299
2300 case CRYPT_MODE_CBC:
2301 encrypt_block_impl(pCryptKey->aiAlgid, 0, &pCryptKey->context, in, out,
2302 RSAENH_DECRYPT);
2303 for (j=0; j<pCryptKey->dwBlockLen; j++) out[j] ^= pCryptKey->abChainVector[j];
2304 memcpy(pCryptKey->abChainVector, in, pCryptKey->dwBlockLen);
2305 break;
2306
2307 case CRYPT_MODE_CFB:
2308 for (j=0; j<pCryptKey->dwBlockLen; j++) {
2309 encrypt_block_impl(pCryptKey->aiAlgid, 0, &pCryptKey->context,
2310 pCryptKey->abChainVector, o, RSAENH_ENCRYPT);
2311 out[j] = in[j] ^ o[0];
2312 for (k=0; k<pCryptKey->dwBlockLen-1; k++)
2313 pCryptKey->abChainVector[k] = pCryptKey->abChainVector[k+1];
2314 pCryptKey->abChainVector[k] = in[j];
2315 }
2316 break;
2317
2318 default:
2319 SetLastError(NTE_BAD_ALGID);
2320 return FALSE;
2321 }
2322 memcpy(in, out, pCryptKey->dwBlockLen);
2323 }
2324 if (Final) {
2325 if (pbData[*pdwDataLen-1] &&
2326 pbData[*pdwDataLen-1] <= pCryptKey->dwBlockLen &&
2327 pbData[*pdwDataLen-1] <= *pdwDataLen) {
2328 BOOL padOkay = TRUE;
2329
2330 /* check that every bad byte has the same value */
2331 for (i = 1; padOkay && i < pbData[*pdwDataLen-1]; i++)
2332 if (pbData[*pdwDataLen - i - 1] != pbData[*pdwDataLen - 1])
2333 padOkay = FALSE;
2334 if (padOkay)
2335 *pdwDataLen -= pbData[*pdwDataLen-1];
2336 else {
2337 SetLastError(NTE_BAD_DATA);
2338 return FALSE;
2339 }
2340 }
2341 else {
2342 SetLastError(NTE_BAD_DATA);
2343 return FALSE;
2344 }
2345 }
2346
2347 } else if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_STREAM) {
2348 encrypt_stream_impl(pCryptKey->aiAlgid, &pCryptKey->context, pbData, *pdwDataLen);
2349 } else if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_RSA) {
2350 if (pCryptKey->aiAlgid == CALG_RSA_SIGN) {
2351 SetLastError(NTE_BAD_KEY);
2352 return FALSE;
2353 }
2354 encrypt_block_impl(pCryptKey->aiAlgid, PK_PRIVATE, &pCryptKey->context, pbData, pbData, RSAENH_DECRYPT);
2355 if (!unpad_data(pbData, pCryptKey->dwBlockLen, pbData, pdwDataLen, dwFlags)) return FALSE;
2356 Final = TRUE;
2357 } else {
2358 SetLastError(NTE_BAD_TYPE);
2359 return FALSE;
2360 }
2361
2362 if (Final) setup_key(pCryptKey);
2363
2364 if (is_valid_handle(&handle_table, hHash, RSAENH_MAGIC_HASH)) {
2365 if (*pdwDataLen>dwMax ||
2366 !RSAENH_CPHashData(hProv, hHash, pbData, *pdwDataLen, 0)) return FALSE;
2367 }
2368
2369 return TRUE;
2370 }
2371
2372 static BOOL crypt_export_simple(CRYPTKEY *pCryptKey, CRYPTKEY *pPubKey,
2373 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
2374 {
2375 BLOBHEADER *pBlobHeader = (BLOBHEADER*)pbData;
2376 ALG_ID *pAlgid = (ALG_ID*)(pBlobHeader+1);
2377 DWORD dwDataLen;
2378
2379 if (!(GET_ALG_CLASS(pCryptKey->aiAlgid)&(ALG_CLASS_DATA_ENCRYPT|ALG_CLASS_MSG_ENCRYPT))) {
2380 SetLastError(NTE_BAD_KEY); /* FIXME: error code? */
2381 return FALSE;
2382 }
2383
2384 dwDataLen = sizeof(BLOBHEADER) + sizeof(ALG_ID) + pPubKey->dwBlockLen;
2385 if (pbData) {
2386 if (*pdwDataLen < dwDataLen) {
2387 SetLastError(ERROR_MORE_DATA);
2388 *pdwDataLen = dwDataLen;
2389 return FALSE;
2390 }
2391
2392 pBlobHeader->bType = SIMPLEBLOB;
2393 pBlobHeader->bVersion = CUR_BLOB_VERSION;
2394 pBlobHeader->reserved = 0;
2395 pBlobHeader->aiKeyAlg = pCryptKey->aiAlgid;
2396
2397 *pAlgid = pPubKey->aiAlgid;
2398
2399 if (!pad_data(pCryptKey->abKeyValue, pCryptKey->dwKeyLen, (BYTE*)(pAlgid+1),
2400 pPubKey->dwBlockLen, dwFlags))
2401 {
2402 return FALSE;
2403 }
2404
2405 encrypt_block_impl(pPubKey->aiAlgid, PK_PUBLIC, &pPubKey->context, (BYTE*)(pAlgid+1),
2406 (BYTE*)(pAlgid+1), RSAENH_ENCRYPT);
2407 }
2408 *pdwDataLen = dwDataLen;
2409 return TRUE;
2410 }
2411
2412 static BOOL crypt_export_public_key(CRYPTKEY *pCryptKey, BYTE *pbData,
2413 DWORD *pdwDataLen)
2414 {
2415 BLOBHEADER *pBlobHeader = (BLOBHEADER*)pbData;
2416 RSAPUBKEY *pRSAPubKey = (RSAPUBKEY*)(pBlobHeader+1);
2417 DWORD dwDataLen;
2418
2419 if ((pCryptKey->aiAlgid != CALG_RSA_KEYX) && (pCryptKey->aiAlgid != CALG_RSA_SIGN)) {
2420 SetLastError(NTE_BAD_KEY);
2421 return FALSE;
2422 }
2423
2424 dwDataLen = sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) + pCryptKey->dwKeyLen;
2425 if (pbData) {
2426 if (*pdwDataLen < dwDataLen) {
2427 SetLastError(ERROR_MORE_DATA);
2428 *pdwDataLen = dwDataLen;
2429 return FALSE;
2430 }
2431
2432 pBlobHeader->bType = PUBLICKEYBLOB;
2433 pBlobHeader->bVersion = CUR_BLOB_VERSION;
2434 pBlobHeader->reserved = 0;
2435 pBlobHeader->aiKeyAlg = pCryptKey->aiAlgid;
2436
2437 pRSAPubKey->magic = RSAENH_MAGIC_RSA1;
2438 pRSAPubKey->bitlen = pCryptKey->dwKeyLen << 3;
2439
2440 export_public_key_impl((BYTE*)(pRSAPubKey+1), &pCryptKey->context,
2441 pCryptKey->dwKeyLen, &pRSAPubKey->pubexp);
2442 }
2443 *pdwDataLen = dwDataLen;
2444 return TRUE;
2445 }
2446
2447 static BOOL crypt_export_private_key(CRYPTKEY *pCryptKey, BOOL force,
2448 BYTE *pbData, DWORD *pdwDataLen)
2449 {
2450 BLOBHEADER *pBlobHeader = (BLOBHEADER*)pbData;
2451 RSAPUBKEY *pRSAPubKey = (RSAPUBKEY*)(pBlobHeader+1);
2452 DWORD dwDataLen;
2453
2454 if ((pCryptKey->aiAlgid != CALG_RSA_KEYX) && (pCryptKey->aiAlgid != CALG_RSA_SIGN)) {
2455 SetLastError(NTE_BAD_KEY);
2456 return FALSE;
2457 }
2458 if (!force && !(pCryptKey->dwPermissions & CRYPT_EXPORT))
2459 {
2460 SetLastError(NTE_BAD_KEY_STATE);
2461 return FALSE;
2462 }
2463
2464 dwDataLen = sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) +
2465 2 * pCryptKey->dwKeyLen + 5 * ((pCryptKey->dwKeyLen + 1) >> 1);
2466 if (pbData) {
2467 if (*pdwDataLen < dwDataLen) {
2468 SetLastError(ERROR_MORE_DATA);
2469 *pdwDataLen = dwDataLen;
2470 return FALSE;
2471 }
2472
2473 pBlobHeader->bType = PRIVATEKEYBLOB;
2474 pBlobHeader->bVersion = CUR_BLOB_VERSION;
2475 pBlobHeader->reserved = 0;
2476 pBlobHeader->aiKeyAlg = pCryptKey->aiAlgid;
2477
2478 pRSAPubKey->magic = RSAENH_MAGIC_RSA2;
2479 pRSAPubKey->bitlen = pCryptKey->dwKeyLen << 3;
2480
2481 export_private_key_impl((BYTE*)(pRSAPubKey+1), &pCryptKey->context,
2482 pCryptKey->dwKeyLen, &pRSAPubKey->pubexp);
2483 }
2484 *pdwDataLen = dwDataLen;
2485 return TRUE;
2486 }
2487
2488 static BOOL crypt_export_plaintext_key(CRYPTKEY *pCryptKey, BYTE *pbData,
2489 DWORD *pdwDataLen)
2490 {
2491 BLOBHEADER *pBlobHeader = (BLOBHEADER*)pbData;
2492 DWORD *pKeyLen = (DWORD*)(pBlobHeader+1);
2493 BYTE *pbKey = (BYTE*)(pKeyLen+1);
2494 DWORD dwDataLen;
2495
2496 dwDataLen = sizeof(BLOBHEADER) + sizeof(DWORD) + pCryptKey->dwKeyLen;
2497 if (pbData) {
2498 if (*pdwDataLen < dwDataLen) {
2499 SetLastError(ERROR_MORE_DATA);
2500 *pdwDataLen = dwDataLen;
2501 return FALSE;
2502 }
2503
2504 pBlobHeader->bType = PLAINTEXTKEYBLOB;
2505 pBlobHeader->bVersion = CUR_BLOB_VERSION;
2506 pBlobHeader->reserved = 0;
2507 pBlobHeader->aiKeyAlg = pCryptKey->aiAlgid;
2508
2509 *pKeyLen = pCryptKey->dwKeyLen;
2510 memcpy(pbKey, &pCryptKey->abKeyValue, pCryptKey->dwKeyLen);
2511 }
2512 *pdwDataLen = dwDataLen;
2513 return TRUE;
2514 }
2515 /******************************************************************************
2516 * crypt_export_key [Internal]
2517 *
2518 * Export a key into a binary large object (BLOB). Called by CPExportKey and
2519 * by store_key_pair.
2520 *
2521 * PARAMS
2522 * pCryptKey [I] Key to be exported.
2523 * hPubKey [I] Key used to encrypt sensitive BLOB data.
2524 * dwBlobType [I] SIMPLEBLOB, PUBLICKEYBLOB or PRIVATEKEYBLOB.
2525 * dwFlags [I] Currently none defined.
2526 * force [I] If TRUE, the key is written no matter what the key's
2527 * permissions are. Otherwise the key's permissions are
2528 * checked before exporting.
2529 * pbData [O] Pointer to a buffer where the BLOB will be written to.
2530 * pdwDataLen [I/O] I: Size of buffer at pbData, O: Size of BLOB
2531 *
2532 * RETURNS
2533 * Success: TRUE.
2534 * Failure: FALSE.
2535 */
2536 static BOOL crypt_export_key(CRYPTKEY *pCryptKey, HCRYPTKEY hPubKey,
2537 DWORD dwBlobType, DWORD dwFlags, BOOL force,
2538 BYTE *pbData, DWORD *pdwDataLen)
2539 {
2540 CRYPTKEY *pPubKey;
2541
2542 if (dwFlags & CRYPT_SSL2_FALLBACK) {
2543 if (pCryptKey->aiAlgid != CALG_SSL2_MASTER) {
2544 SetLastError(NTE_BAD_KEY);
2545 return FALSE;
2546 }
2547 }
2548
2549 switch ((BYTE)dwBlobType)
2550 {
2551 case SIMPLEBLOB:
2552 if (!lookup_handle(&handle_table, hPubKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pPubKey)){
2553 SetLastError(NTE_BAD_PUBLIC_KEY); /* FIXME: error_code? */
2554 return FALSE;
2555 }
2556 return crypt_export_simple(pCryptKey, pPubKey, dwFlags, pbData,
2557 pdwDataLen);
2558
2559 case PUBLICKEYBLOB:
2560 if (is_valid_handle(&handle_table, hPubKey, RSAENH_MAGIC_KEY)) {
2561 SetLastError(NTE_BAD_KEY); /* FIXME: error code? */
2562 return FALSE;
2563 }
2564
2565 return crypt_export_public_key(pCryptKey, pbData, pdwDataLen);
2566
2567 case PRIVATEKEYBLOB:
2568 return crypt_export_private_key(pCryptKey, force, pbData, pdwDataLen);
2569
2570 case PLAINTEXTKEYBLOB:
2571 return crypt_export_plaintext_key(pCryptKey, pbData, pdwDataLen);
2572
2573 default:
2574 SetLastError(NTE_BAD_TYPE); /* FIXME: error code? */
2575 return FALSE;
2576 }
2577 }
2578
2579 /******************************************************************************
2580 * CPExportKey (RSAENH.@)
2581 *
2582 * Export a key into a binary large object (BLOB).
2583 *
2584 * PARAMS
2585 * hProv [I] Key container from which a key is to be exported.
2586 * hKey [I] Key to be exported.
2587 * hPubKey [I] Key used to encrypt sensitive BLOB data.
2588 * dwBlobType [I] SIMPLEBLOB, PUBLICKEYBLOB or PRIVATEKEYBLOB.
2589 * dwFlags [I] Currently none defined.
2590 * pbData [O] Pointer to a buffer where the BLOB will be written to.
2591 * pdwDataLen [I/O] I: Size of buffer at pbData, O: Size of BLOB
2592 *
2593 * RETURNS
2594 * Success: TRUE.
2595 * Failure: FALSE.
2596 */
2597 BOOL WINAPI RSAENH_CPExportKey(HCRYPTPROV hProv, HCRYPTKEY hKey, HCRYPTKEY hPubKey,
2598 DWORD dwBlobType, DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
2599 {
2600 CRYPTKEY *pCryptKey;
2601
2602 TRACE("(hProv=%08lx, hKey=%08lx, hPubKey=%08lx, dwBlobType=%08x, dwFlags=%08x, pbData=%p,"
2603 "pdwDataLen=%p)\n", hProv, hKey, hPubKey, dwBlobType, dwFlags, pbData, pdwDataLen);
2604
2605 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
2606 {
2607 SetLastError(NTE_BAD_UID);
2608 return FALSE;
2609 }
2610
2611 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
2612 {
2613 SetLastError(NTE_BAD_KEY);
2614 return FALSE;
2615 }
2616
2617 return crypt_export_key(pCryptKey, hPubKey, dwBlobType, dwFlags, FALSE,
2618 pbData, pdwDataLen);
2619 }
2620
2621 /******************************************************************************
2622 * release_and_install_key [Internal]
2623 *
2624 * Release an existing key, if present, and replaces it with a new one.
2625 *
2626 * PARAMS
2627 * hProv [I] Key container into which the key is to be imported.
2628 * src [I] Key which will replace *dest
2629 * dest [I] Points to key to be released and replaced with src
2630 * fStoreKey [I] If TRUE, the newly installed key is stored to the registry.
2631 */
2632 static void release_and_install_key(HCRYPTPROV hProv, HCRYPTKEY src,
2633 HCRYPTKEY *dest, DWORD fStoreKey)
2634 {
2635 RSAENH_CPDestroyKey(hProv, *dest);
2636 copy_handle(&handle_table, src, RSAENH_MAGIC_KEY, dest);
2637 if (fStoreKey)
2638 {
2639 KEYCONTAINER *pKeyContainer;
2640
2641 if (lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER,
2642 (OBJECTHDR**)&pKeyContainer))
2643 {
2644 store_key_container_keys(pKeyContainer);
2645 store_key_container_permissions(pKeyContainer);
2646 }
2647 }
2648 }
2649
2650 /******************************************************************************
2651 * import_private_key [Internal]
2652 *
2653 * Import a BLOB'ed private key into a key container.
2654 *
2655 * PARAMS
2656 * hProv [I] Key container into which the private key is to be imported.
2657 * pbData [I] Pointer to a buffer which holds the private key BLOB.
2658 * dwDataLen [I] Length of data in buffer at pbData.
2659 * dwFlags [I] One of:
2660 * CRYPT_EXPORTABLE: the imported key is marked exportable
2661 * fStoreKey [I] If TRUE, the imported key is stored to the registry.
2662 * phKey [O] Handle to the imported key.
2663 *
2664 *
2665 * NOTES
2666 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2667 * it's a PRIVATEKEYBLOB.
2668 *
2669 * RETURNS
2670 * Success: TRUE.
2671 * Failure: FALSE.
2672 */
2673 static BOOL import_private_key(HCRYPTPROV hProv, CONST BYTE *pbData, DWORD dwDataLen,
2674 DWORD dwFlags, BOOL fStoreKey, HCRYPTKEY *phKey)
2675 {
2676 KEYCONTAINER *pKeyContainer;
2677 CRYPTKEY *pCryptKey;
2678 CONST BLOBHEADER *pBlobHeader = (CONST BLOBHEADER*)pbData;
2679 CONST RSAPUBKEY *pRSAPubKey = (CONST RSAPUBKEY*)(pBlobHeader+1);
2680 BOOL ret;
2681
2682 if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER,
2683 (OBJECTHDR**)&pKeyContainer))
2684 {
2685 SetLastError(NTE_BAD_UID);
2686 return FALSE;
2687 }
2688
2689 if ((dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY)) ||
2690 (pRSAPubKey->magic != RSAENH_MAGIC_RSA2) ||
2691 (dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) +
2692 (2 * pRSAPubKey->bitlen >> 3) + (5 * ((pRSAPubKey->bitlen+8)>>4))))
2693 {
2694 SetLastError(NTE_BAD_DATA);
2695 return FALSE;
2696 }
2697
2698 *phKey = new_key(hProv, pBlobHeader->aiKeyAlg, MAKELONG(0,pRSAPubKey->bitlen), &pCryptKey);
2699 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) return FALSE;
2700 setup_key(pCryptKey);
2701 ret = import_private_key_impl((CONST BYTE*)(pRSAPubKey+1), &pCryptKey->context,
2702 pRSAPubKey->bitlen/8, pRSAPubKey->pubexp);
2703 if (ret) {
2704 if (dwFlags & CRYPT_EXPORTABLE)
2705 pCryptKey->dwPermissions |= CRYPT_EXPORT;
2706 switch (pBlobHeader->aiKeyAlg)
2707 {
2708 case AT_SIGNATURE:
2709 case CALG_RSA_SIGN:
2710 TRACE("installing signing key\n");
2711 release_and_install_key(hProv, *phKey, &pKeyContainer->hSignatureKeyPair,
2712 fStoreKey);
2713 break;
2714 case AT_KEYEXCHANGE:
2715 case CALG_RSA_KEYX:
2716 TRACE("installing key exchange key\n");
2717 release_and_install_key(hProv, *phKey, &pKeyContainer->hKeyExchangeKeyPair,
2718 fStoreKey);
2719 break;
2720 }
2721 }
2722 return ret;
2723 }
2724
2725 /******************************************************************************
2726 * import_public_key [Internal]
2727 *
2728 * Import a BLOB'ed public key into a key container.
2729 *
2730 * PARAMS
2731 * hProv [I] Key container into which the public key is to be imported.
2732 * pbData [I] Pointer to a buffer which holds the public key BLOB.
2733 * dwDataLen [I] Length of data in buffer at pbData.
2734 * dwFlags [I] One of:
2735 * CRYPT_EXPORTABLE: the imported key is marked exportable
2736 * fStoreKey [I] If TRUE, the imported key is stored to the registry.
2737 * phKey [O] Handle to the imported key.
2738 *
2739 *
2740 * NOTES
2741 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2742 * it's a PUBLICKEYBLOB.
2743 *
2744 * RETURNS
2745 * Success: TRUE.
2746 * Failure: FALSE.
2747 */
2748 static BOOL import_public_key(HCRYPTPROV hProv, CONST BYTE *pbData, DWORD dwDataLen,
2749 DWORD dwFlags, BOOL fStoreKey, HCRYPTKEY *phKey)
2750 {
2751 KEYCONTAINER *pKeyContainer;
2752 CRYPTKEY *pCryptKey;
2753 CONST BLOBHEADER *pBlobHeader = (CONST BLOBHEADER*)pbData;
2754 CONST RSAPUBKEY *pRSAPubKey = (CONST RSAPUBKEY*)(pBlobHeader+1);
2755 ALG_ID algID;
2756 BOOL ret;
2757
2758 if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER,
2759 (OBJECTHDR**)&pKeyContainer))
2760 {
2761 SetLastError(NTE_BAD_UID);
2762 return FALSE;
2763 }
2764
2765 if ((dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY)) ||
2766 (pRSAPubKey->magic != RSAENH_MAGIC_RSA1) ||
2767 (dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) + (pRSAPubKey->bitlen >> 3)))
2768 {
2769 SetLastError(NTE_BAD_DATA);
2770 return FALSE;
2771 }
2772
2773 /* Since this is a public key blob, only the public key is
2774 * available, so only signature verification is possible.
2775 */
2776 algID = pBlobHeader->aiKeyAlg;
2777 *phKey = new_key(hProv, algID, MAKELONG(0,pRSAPubKey->bitlen), &pCryptKey);
2778 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) return FALSE;
2779 setup_key(pCryptKey);
2780 ret = import_public_key_impl((CONST BYTE*)(pRSAPubKey+1), &pCryptKey->context,
2781 pRSAPubKey->bitlen >> 3, pRSAPubKey->pubexp);
2782 if (ret) {
2783 if (dwFlags & CRYPT_EXPORTABLE)
2784 pCryptKey->dwPermissions |= CRYPT_EXPORT;
2785 switch (pBlobHeader->aiKeyAlg)
2786 {
2787 case AT_KEYEXCHANGE:
2788 case CALG_RSA_KEYX:
2789 TRACE("installing public key\n");
2790 release_and_install_key(hProv, *phKey, &pKeyContainer->hKeyExchangeKeyPair,
2791 fStoreKey);
2792 break;
2793 }
2794 }
2795 return ret;
2796 }
2797
2798 /******************************************************************************
2799 * import_symmetric_key [Internal]
2800 *
2801 * Import a BLOB'ed symmetric key into a key container.
2802 *
2803 * PARAMS
2804 * hProv [I] Key container into which the symmetric key is to be imported.
2805 * pbData [I] Pointer to a buffer which holds the symmetric key BLOB.
2806 * dwDataLen [I] Length of data in buffer at pbData.
2807 * hPubKey [I] Key used to decrypt sensitive BLOB data.
2808 * dwFlags [I] One of:
2809 * CRYPT_EXPORTABLE: the imported key is marked exportable
2810 * phKey [O] Handle to the imported key.
2811 *
2812 *
2813 * NOTES
2814 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2815 * it's a SIMPLEBLOB.
2816 *
2817 * RETURNS
2818 * Success: TRUE.
2819 * Failure: FALSE.
2820 */
2821 static BOOL import_symmetric_key(HCRYPTPROV hProv, CONST BYTE *pbData,
2822 DWORD dwDataLen, HCRYPTKEY hPubKey,
2823 DWORD dwFlags, HCRYPTKEY *phKey)
2824 {
2825 CRYPTKEY *pCryptKey, *pPubKey;
2826 CONST BLOBHEADER *pBlobHeader = (CONST BLOBHEADER*)pbData;
2827 CONST ALG_ID *pAlgid = (CONST ALG_ID*)(pBlobHeader+1);
2828 CONST BYTE *pbKeyStream = (CONST BYTE*)(pAlgid + 1);
2829 BYTE *pbDecrypted;
2830 DWORD dwKeyLen;
2831
2832 if (!lookup_handle(&handle_table, hPubKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pPubKey) ||
2833 pPubKey->aiAlgid != CALG_RSA_KEYX)
2834 {
2835 SetLastError(NTE_BAD_PUBLIC_KEY); /* FIXME: error code? */
2836 return FALSE;
2837 }
2838
2839 if (dwDataLen < sizeof(BLOBHEADER)+sizeof(ALG_ID)+pPubKey->dwBlockLen)
2840 {
2841 SetLastError(NTE_BAD_DATA); /* FIXME: error code */
2842 return FALSE;
2843 }
2844
2845 pbDecrypted = HeapAlloc(GetProcessHeap(), 0, pPubKey->dwBlockLen);
2846 if (!pbDecrypted) return FALSE;
2847 encrypt_block_impl(pPubKey->aiAlgid, PK_PRIVATE, &pPubKey->context, pbKeyStream, pbDecrypted,
2848 RSAENH_DECRYPT);
2849
2850 dwKeyLen = RSAENH_MAX_KEY_SIZE;
2851 if (!unpad_data(pbDecrypted, pPubKey->dwBlockLen, pbDecrypted, &dwKeyLen, dwFlags)) {
2852 HeapFree(GetProcessHeap(), 0, pbDecrypted);
2853 return FALSE;
2854 }
2855
2856 *phKey = new_key(hProv, pBlobHeader->aiKeyAlg, dwKeyLen<<19, &pCryptKey);
2857 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE)
2858 {
2859 HeapFree(GetProcessHeap(), 0, pbDecrypted);
2860 return FALSE;
2861 }
2862 memcpy(pCryptKey->abKeyValue, pbDecrypted, dwKeyLen);
2863 HeapFree(GetProcessHeap(), 0, pbDecrypted);
2864 setup_key(pCryptKey);
2865 if (dwFlags & CRYPT_EXPORTABLE)
2866 pCryptKey->dwPermissions |= CRYPT_EXPORT;
2867 return TRUE;
2868 }
2869
2870 /******************************************************************************
2871 * import_plaintext_key [Internal]
2872 *
2873 * Import a plaintext key into a key container.
2874 *
2875 * PARAMS
2876 * hProv [I] Key container into which the symmetric key is to be imported.
2877 * pbData [I] Pointer to a buffer which holds the plaintext key BLOB.
2878 * dwDataLen [I] Length of data in buffer at pbData.
2879 * dwFlags [I] One of:
2880 * CRYPT_EXPORTABLE: the imported key is marked exportable
2881 * phKey [O] Handle to the imported key.
2882 *
2883 *
2884 * NOTES
2885 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2886 * it's a PLAINTEXTKEYBLOB.
2887 *
2888 * RETURNS
2889 * Success: TRUE.
2890 * Failure: FALSE.
2891 */
2892 static BOOL import_plaintext_key(HCRYPTPROV hProv, CONST BYTE *pbData,
2893 DWORD dwDataLen, DWORD dwFlags,
2894 HCRYPTKEY *phKey)
2895 {
2896 CRYPTKEY *pCryptKey;
2897 CONST BLOBHEADER *pBlobHeader = (CONST BLOBHEADER*)pbData;
2898 CONST DWORD *pKeyLen = (CONST DWORD *)(pBlobHeader + 1);
2899 CONST BYTE *pbKeyStream = (CONST BYTE*)(pKeyLen + 1);
2900
2901 if (dwDataLen < sizeof(BLOBHEADER)+sizeof(DWORD)+*pKeyLen)
2902 {
2903 SetLastError(NTE_BAD_DATA); /* FIXME: error code */
2904 return FALSE;
2905 }
2906
2907 *phKey = new_key(hProv, pBlobHeader->aiKeyAlg, *pKeyLen<<19, &pCryptKey);
2908 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE)
2909 return FALSE;
2910 memcpy(pCryptKey->abKeyValue, pbKeyStream, *pKeyLen);
2911 setup_key(pCryptKey);
2912 if (dwFlags & CRYPT_EXPORTABLE)
2913 pCryptKey->dwPermissions |= CRYPT_EXPORT;
2914 return TRUE;
2915 }
2916
2917 /******************************************************************************
2918 * import_key [Internal]
2919 *
2920 * Import a BLOB'ed key into a key container, optionally storing the key's
2921 * value to the registry.
2922 *
2923 * PARAMS
2924 * hProv [I] Key container into which the key is to be imported.
2925 * pbData [I] Pointer to a buffer which holds the BLOB.
2926 * dwDataLen [I] Length of data in buffer at pbData.
2927 * hPubKey [I] Key used to decrypt sensitive BLOB data.
2928 * dwFlags [I] One of:
2929 * CRYPT_EXPORTABLE: the imported key is marked exportable
2930 * fStoreKey [I] If TRUE, the imported key is stored to the registry.
2931 * phKey [O] Handle to the imported key.
2932 *
2933 * RETURNS
2934 * Success: TRUE.
2935 * Failure: FALSE.
2936 */
2937 static BOOL import_key(HCRYPTPROV hProv, CONST BYTE *pbData, DWORD dwDataLen,
2938 HCRYPTKEY hPubKey, DWORD dwFlags, BOOL fStoreKey,
2939 HCRYPTKEY *phKey)
2940 {
2941 KEYCONTAINER *pKeyContainer;
2942 CONST BLOBHEADER *pBlobHeader = (CONST BLOBHEADER*)pbData;
2943
2944 if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER,
2945 (OBJECTHDR**)&pKeyContainer))
2946 {
2947 SetLastError(NTE_BAD_UID);
2948 return FALSE;
2949 }
2950
2951 if (dwDataLen < sizeof(BLOBHEADER) ||
2952 pBlobHeader->bVersion != CUR_BLOB_VERSION ||
2953 pBlobHeader->reserved != 0)
2954 {
2955 TRACE("bVersion = %d, reserved = %d\n", pBlobHeader->bVersion,
2956 pBlobHeader->reserved);
2957 SetLastError(NTE_BAD_DATA);
2958 return FALSE;
2959 }
2960
2961 /* If this is a verify-only context, the key is not persisted regardless of
2962 * fStoreKey's original value.
2963 */
2964 fStoreKey = fStoreKey && !(dwFlags & CRYPT_VERIFYCONTEXT);
2965 TRACE("blob type: %x\n", pBlobHeader->bType);
2966 switch (pBlobHeader->bType)
2967 {
2968 case PRIVATEKEYBLOB:
2969 return import_private_key(hProv, pbData, dwDataLen, dwFlags,
2970 fStoreKey, phKey);
2971
2972 case PUBLICKEYBLOB:
2973 return import_public_key(hProv, pbData, dwDataLen, dwFlags,
2974 fStoreKey, phKey);
2975
2976 case SIMPLEBLOB:
2977 return import_symmetric_key(hProv, pbData, dwDataLen, hPubKey,
2978 dwFlags, phKey);
2979
2980 case PLAINTEXTKEYBLOB:
2981 return import_plaintext_key(hProv, pbData, dwDataLen, dwFlags,
2982 phKey);
2983
2984 default:
2985 SetLastError(NTE_BAD_TYPE); /* FIXME: error code? */
2986 return FALSE;
2987 }
2988 }
2989
2990 /******************************************************************************
2991 * CPImportKey (RSAENH.@)
2992 *
2993 * Import a BLOB'ed key into a key container.
2994 *
2995 * PARAMS
2996 * hProv [I] Key container into which the key is to be imported.
2997 * pbData [I] Pointer to a buffer which holds the BLOB.
2998 * dwDataLen [I] Length of data in buffer at pbData.
2999 * hPubKey [I] Key used to decrypt sensitive BLOB data.
3000 * dwFlags [I] One of:
3001 * CRYPT_EXPORTABLE: the imported key is marked exportable
3002 * phKey [O] Handle to the imported key.
3003 *
3004 * RETURNS
3005 * Success: TRUE.
3006 * Failure: FALSE.
3007 */
3008 BOOL WINAPI RSAENH_CPImportKey(HCRYPTPROV hProv, CONST BYTE *pbData, DWORD dwDataLen,
3009 HCRYPTKEY hPubKey, DWORD dwFlags, HCRYPTKEY *phKey)
3010 {
3011 TRACE("(hProv=%08lx, pbData=%p, dwDataLen=%d, hPubKey=%08lx, dwFlags=%08x, phKey=%p)\n",
3012 hProv, pbData, dwDataLen, hPubKey, dwFlags, phKey);
3013
3014 if (dwFlags & CRYPT_IPSEC_HMAC_KEY)
3015 {
3016 FIXME("unimplemented for CRYPT_IPSEC_HMAC_KEY\n");
3017 SetLastError(NTE_BAD_FLAGS);
3018 return FALSE;
3019 }
3020 return import_key(hProv, pbData, dwDataLen, hPubKey, dwFlags, TRUE, phKey);
3021 }
3022
3023 /******************************************************************************
3024 * CPGenKey (RSAENH.@)
3025 *
3026 * Generate a key in the key container
3027 *
3028 * PARAMS
3029 * hProv [I] Key container for which a key is to be generated.
3030 * Algid [I] Crypto algorithm identifier for the key to be generated.
3031 * dwFlags [I] Upper 16 bits: Binary length of key. Lower 16 bits: Flags. See Notes
3032 * phKey [O] Handle to the generated key.
3033 *
3034 * RETURNS
3035 * Success: TRUE.
3036 * Failure: FALSE.
3037 *
3038 * FIXME
3039 * Flags currently not considered.
3040 *
3041 * NOTES
3042 * Private key-exchange- and signature-keys can be generated with Algid AT_KEYEXCHANGE
3043 * and AT_SIGNATURE values.
3044 */
3045 BOOL WINAPI RSAENH_CPGenKey(HCRYPTPROV hProv, ALG_ID Algid, DWORD dwFlags, HCRYPTKEY *phKey)
3046 {
3047 KEYCONTAINER *pKeyContainer;
3048 CRYPTKEY *pCryptKey;
3049
3050 TRACE("(hProv=%08lx, aiAlgid=%d, dwFlags=%08x, phKey=%p)\n", hProv, Algid, dwFlags, phKey);
3051
3052 if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER,
3053 (OBJECTHDR**)&pKeyContainer))
3054 {
3055 /* MSDN: hProv not containing valid context handle */
3056 SetLastError(NTE_BAD_UID);
3057 return FALSE;
3058 }
3059
3060 switch (Algid)
3061 {
3062 case AT_SIGNATURE:
3063 case CALG_RSA_SIGN:
3064 *phKey = new_key(hProv, CALG_RSA_SIGN, dwFlags, &pCryptKey);
3065 if (pCryptKey) {
3066 new_key_impl(pCryptKey->aiAlgid, &pCryptKey->context, pCryptKey->dwKeyLen);
3067 setup_key(pCryptKey);
3068 RSAENH_CPDestroyKey(hProv, pKeyContainer->hSignatureKeyPair);
3069 copy_handle(&handle_table, *phKey, RSAENH_MAGIC_KEY,
3070 &pKeyContainer->hSignatureKeyPair);
3071 }
3072 break;
3073
3074 case AT_KEYEXCHANGE:
3075 case CALG_RSA_KEYX:
3076 *phKey = new_key(hProv, CALG_RSA_KEYX, dwFlags, &pCryptKey);
3077 if (pCryptKey) {
3078 new_key_impl(pCryptKey->aiAlgid, &pCryptKey->context, pCryptKey->dwKeyLen);
3079 setup_key(pCryptKey);
3080 RSAENH_CPDestroyKey(hProv, pKeyContainer->hKeyExchangeKeyPair);
3081 copy_handle(&handle_table, *phKey, RSAENH_MAGIC_KEY,
3082 &pKeyContainer->hKeyExchangeKeyPair);
3083 }
3084 break;
3085
3086 case CALG_RC2:
3087 case CALG_RC4:
3088 case CALG_DES:
3089 case CALG_3DES_112:
3090 case CALG_3DES:
3091 case CALG_AES:
3092 case CALG_AES_128:
3093 case CALG_AES_192:
3094 case CALG_AES_256:
3095 case CALG_PCT1_MASTER:
3096 case CALG_SSL2_MASTER:
3097 case CALG_SSL3_MASTER:
3098 case CALG_TLS1_MASTER:
3099 *phKey = new_key(hProv, Algid, dwFlags, &pCryptKey);
3100 if (pCryptKey) {
3101 gen_rand_impl(pCryptKey->abKeyValue, RSAENH_MAX_KEY_SIZE);
3102 switch (Algid) {
3103 case CALG_SSL3_MASTER:
3104 pCryptKey->abKeyValue[0] = RSAENH_SSL3_VERSION_MAJOR;
3105 pCryptKey->abKeyValue[1] = RSAENH_SSL3_VERSION_MINOR;
3106 break;
3107
3108 case CALG_TLS1_MASTER:
3109 pCryptKey->abKeyValue[0] = RSAENH_TLS1_VERSION_MAJOR;
3110 pCryptKey->abKeyValue[1] = RSAENH_TLS1_VERSION_MINOR;
3111 break;
3112 }
3113 setup_key(pCryptKey);
3114 }
3115 break;
3116
3117 default:
3118 /* MSDN: Algorithm not supported specified by Algid */
3119 SetLastError(NTE_BAD_ALGID);
3120 return FALSE;
3121 }
3122
3123 return *phKey != (HCRYPTKEY)INVALID_HANDLE_VALUE;
3124 }
3125
3126 /******************************************************************************
3127 * CPGenRandom (RSAENH.@)
3128 *
3129 * Generate a random byte stream.
3130 *
3131 * PARAMS
3132 * hProv [I] Key container that is used to generate random bytes.
3133 * dwLen [I] Specifies the number of requested random data bytes.
3134 * pbBuffer [O] Random bytes will be stored here.
3135 *
3136 * RETURNS
3137 * Success: TRUE
3138 * Failure: FALSE
3139 */
3140 BOOL WINAPI RSAENH_CPGenRandom(HCRYPTPROV hProv, DWORD dwLen, BYTE *pbBuffer)
3141 {
3142 TRACE("(hProv=%08lx, dwLen=%d, pbBuffer=%p)\n", hProv, dwLen, pbBuffer);
3143
3144 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
3145 {
3146 /* MSDN: hProv not containing valid context handle */
3147 SetLastError(NTE_BAD_UID);
3148 return FALSE;
3149 }
3150
3151 return gen_rand_impl(pbBuffer, dwLen);
3152 }
3153
3154 /******************************************************************************
3155 * CPGetHashParam (RSAENH.@)
3156 *
3157 * Query parameters of an hash object.
3158 *
3159 * PARAMS
3160 * hProv [I] The kea container, which the hash belongs to.
3161 * hHash [I] The hash object that is to be queried.
3162 * dwParam [I] Specifies the parameter that is to be queried.
3163 * pbData [I] Pointer to the buffer where the parameter value will be stored.
3164 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
3165 * dwFlags [I] None currently defined.
3166 *
3167 * RETURNS
3168 * Success: TRUE
3169 * Failure: FALSE
3170 *
3171 * NOTES
3172 * Valid dwParams are: HP_ALGID, HP_HASHSIZE, HP_HASHVALUE. The hash will be
3173 * finalized if HP_HASHVALUE is queried.
3174 */
3175 BOOL WINAPI RSAENH_CPGetHashParam(HCRYPTPROV hProv, HCRYPTHASH hHash, DWORD dwParam, BYTE *pbData,
3176 DWORD *pdwDataLen, DWORD dwFlags)
3177 {
3178 CRYPTHASH *pCryptHash;
3179
3180 TRACE("(hProv=%08lx, hHash=%08lx, dwParam=%08x, pbData=%p, pdwDataLen=%p, dwFlags=%08x)\n",
3181 hProv, hHash, dwParam, pbData, pdwDataLen, dwFlags);
3182
3183 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
3184 {
3185 SetLastError(NTE_BAD_UID);
3186 return FALSE;
3187 }
3188
3189 if (dwFlags)
3190 {
3191 SetLastError(NTE_BAD_FLAGS);
3192 return FALSE;
3193 }
3194
3195 if (!lookup_handle(&handle_table, hHash, RSAENH_MAGIC_HASH,
3196 (OBJECTHDR**)&pCryptHash))
3197 {
3198 SetLastError(NTE_BAD_HASH);
3199 return FALSE;
3200 }
3201
3202 if (!pdwDataLen)
3203 {
3204 SetLastError(ERROR_INVALID_PARAMETER);
3205 return FALSE;
3206 }
3207
3208 switch (dwParam)
3209 {
3210 case HP_ALGID:
3211 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptHash->aiAlgid,
3212 sizeof(ALG_ID));
3213
3214 case HP_HASHSIZE:
3215 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptHash->dwHashSize,
3216 sizeof(DWORD));
3217
3218 case HP_HASHVAL:
3219 if (pCryptHash->aiAlgid == CALG_TLS1PRF) {
3220 return tls1_prf(hProv, pCryptHash->hKey, &pCryptHash->tpPRFParams.blobLabel,
3221 &pCryptHash->tpPRFParams.blobSeed, pbData, *pdwDataLen);
3222 }
3223
3224 if ( pbData == NULL ) {
3225 *pdwDataLen = pCryptHash->dwHashSize;
3226 return TRUE;
3227 }
3228
3229 if (pbData && (pCryptHash->dwState != RSAENH_HASHSTATE_FINISHED))
3230 {
3231 finalize_hash(pCryptHash);
3232 pCryptHash->dwState = RSAENH_HASHSTATE_FINISHED;
3233 }
3234
3235 return copy_param(pbData, pdwDataLen, pCryptHash->abHashValue,
3236 pCryptHash->dwHashSize);
3237
3238 default:
3239 SetLastError(NTE_BAD_TYPE);
3240 return FALSE;
3241 }
3242 }
3243
3244 /******************************************************************************
3245 * CPSetKeyParam (RSAENH.@)
3246 *
3247 * Set a parameter of a key object
3248 *
3249 * PARAMS
3250 * hProv [I] The key container to which the key belongs.
3251 * hKey [I] The key for which a parameter is to be set.
3252 * dwParam [I] Parameter type. See Notes.
3253 * pbData [I] Pointer to the parameter value.
3254 * dwFlags [I] Currently none defined.
3255 *
3256 * RETURNS
3257 * Success: TRUE.
3258 * Failure: FALSE.
3259 *
3260 * NOTES:
3261 * Defined dwParam types are:
3262 * - KP_MODE: Values MODE_CBC, MODE_ECB, MODE_CFB.
3263 * - KP_MODE_BITS: Shift width for cipher feedback mode. (Currently ignored by MS CSP's)
3264 * - KP_PERMISSIONS: Or'ed combination of CRYPT_ENCRYPT, CRYPT_DECRYPT,
3265 * CRYPT_EXPORT, CRYPT_READ, CRYPT_WRITE, CRYPT_MAC
3266 * - KP_IV: Initialization vector
3267 */
3268 BOOL WINAPI RSAENH_CPSetKeyParam(HCRYPTPROV hProv, HCRYPTKEY hKey, DWORD dwParam, BYTE *pbData,
3269 DWORD dwFlags)
3270 {
3271 CRYPTKEY *pCryptKey;
3272
3273 TRACE("(hProv=%08lx, hKey=%08lx, dwParam=%08x, pbData=%p, dwFlags=%08x)\n", hProv, hKey,
3274 dwParam, pbData, dwFlags);
3275
3276 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
3277 {
3278 SetLastError(NTE_BAD_UID);
3279 return FALSE;
3280 }
3281
3282 if (dwFlags) {
3283 SetLastError(NTE_BAD_FLAGS);
3284 return FALSE;
3285 }
3286
3287 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
3288 {
3289 SetLastError(NTE_BAD_KEY);
3290 return FALSE;
3291 }
3292
3293 switch (dwParam) {
3294 case KP_PADDING:
3295 /* The MS providers only support PKCS5_PADDING */
3296 if (*(DWORD *)pbData != PKCS5_PADDING) {
3297 SetLastError(NTE_BAD_DATA);
3298 return FALSE;
3299 }
3300 return TRUE;
3301
3302 case KP_MODE:
3303 pCryptKey->dwMode = *(DWORD*)pbData;
3304 return TRUE;
3305
3306 case KP_MODE_BITS:
3307 pCryptKey->dwModeBits = *(DWORD*)pbData;
3308 return TRUE;
3309
3310 case KP_PERMISSIONS:
3311 {
3312 DWORD perms = *(DWORD *)pbData;
3313
3314 if ((perms & CRYPT_EXPORT) &&
3315 !(pCryptKey->dwPermissions & CRYPT_EXPORT))
3316 {
3317 SetLastError(NTE_BAD_DATA);
3318 return FALSE;
3319 }
3320 else if (!(perms & CRYPT_EXPORT) &&
3321 (pCryptKey->dwPermissions & CRYPT_EXPORT))
3322 {
3323 /* Clearing the export permission appears to be ignored,
3324 * see tests.
3325 */
3326 perms |= CRYPT_EXPORT;
3327 }
3328 pCryptKey->dwPermissions = perms;
3329 return TRUE;
3330 }
3331
3332 case KP_IV:
3333 memcpy(pCryptKey->abInitVector, pbData, pCryptKey->dwBlockLen);
3334 setup_key(pCryptKey);
3335 return TRUE;
3336
3337 case KP_SALT_EX:
3338 {
3339 CRYPT_INTEGER_BLOB *blob = (CRYPT_INTEGER_BLOB *)pbData;
3340
3341 /* salt length can't be greater than 184 bits = 24 bytes */
3342 if (blob->cbData > 24)
3343 {
3344 SetLastError(NTE_BAD_DATA);
3345 return FALSE;
3346 }
3347 memcpy(pCryptKey->abKeyValue + pCryptKey->dwKeyLen, blob->pbData,
3348 blob->cbData);
3349 pCryptKey->dwSaltLen = blob->cbData;
3350 setup_key(pCryptKey);
3351 return TRUE;
3352 }
3353
3354 case KP_EFFECTIVE_KEYLEN:
3355 switch (pCryptKey->aiAlgid) {
3356 case CALG_RC2:
3357 if (!pbData)
3358 {
3359 SetLastError(ERROR_INVALID_PARAMETER);
3360 return FALSE;
3361 }
3362 else if (!*(DWORD *)pbData || *(DWORD *)pbData > 1024)
3363 {
3364 SetLastError(NTE_BAD_DATA);
3365 return FALSE;
3366 }
3367 else
3368 {
3369 pCryptKey->dwEffectiveKeyLen = *(DWORD *)pbData;
3370 setup_key(pCryptKey);
3371 }
3372 break;
3373 default:
3374 SetLastError(NTE_BAD_TYPE);
3375 return FALSE;
3376 }
3377 return TRUE;
3378
3379 case KP_SCHANNEL_ALG:
3380 switch (((PSCHANNEL_ALG)pbData)->dwUse) {
3381 case SCHANNEL_ENC_KEY:
3382 memcpy(&pCryptKey->siSChannelInfo.saEncAlg, pbData, sizeof(SCHANNEL_ALG));
3383 break;
3384
3385 case SCHANNEL_MAC_KEY:
3386 memcpy(&pCryptKey->siSChannelInfo.saMACAlg, pbData, sizeof(SCHANNEL_ALG));
3387 break;
3388
3389 default:
3390 SetLastError(NTE_FAIL); /* FIXME: error code */
3391 return FALSE;
3392 }
3393 return TRUE;
3394
3395 case KP_CLIENT_RANDOM:
3396 return copy_data_blob(&pCryptKey->siSChannelInfo.blobClientRandom, (PCRYPT_DATA_BLOB)pbData);
3397
3398 case KP_SERVER_RANDOM:
3399 return copy_data_blob(&pCryptKey->siSChannelInfo.blobServerRandom, (PCRYPT_DATA_BLOB)pbData);
3400
3401 default:
3402 SetLastError(NTE_BAD_TYPE);
3403 return FALSE;
3404 }
3405 }
3406
3407 /******************************************************************************
3408 * CPGetKeyParam (RSAENH.@)
3409 *
3410 * Query a key parameter.
3411 *
3412 * PARAMS
3413 * hProv [I] The key container, which the key belongs to.
3414 * hHash [I] The key object that is to be queried.
3415 * dwParam [I] Specifies the parameter that is to be queried.
3416 * pbData [I] Pointer to the buffer where the parameter value will be stored.
3417 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
3418 * dwFlags [I] None currently defined.
3419 *
3420 * RETURNS
3421 * Success: TRUE
3422 * Failure: FALSE
3423 *
3424 * NOTES
3425 * Defined dwParam types are:
3426 * - KP_MODE: Values MODE_CBC, MODE_ECB, MODE_CFB.
3427 * - KP_MODE_BITS: Shift width for cipher feedback mode.
3428 * (Currently ignored by MS CSP's - always eight)
3429 * - KP_PERMISSIONS: Or'ed combination of CRYPT_ENCRYPT, CRYPT_DECRYPT,
3430 * CRYPT_EXPORT, CRYPT_READ, CRYPT_WRITE, CRYPT_MAC
3431 * - KP_IV: Initialization vector.
3432 * - KP_KEYLEN: Bitwidth of the key.
3433 * - KP_BLOCKLEN: Size of a block cipher block.
3434 * - KP_SALT: Salt value.
3435 */
3436 BOOL WINAPI RSAENH_CPGetKeyParam(HCRYPTPROV hProv, HCRYPTKEY hKey, DWORD dwParam, BYTE *pbData,
3437 DWORD *pdwDataLen, DWORD dwFlags)
3438 {
3439 CRYPTKEY *pCryptKey;
3440 DWORD dwValue;
3441
3442 TRACE("(hProv=%08lx, hKey=%08lx, dwParam=%08x, pbData=%p, pdwDataLen=%p dwFlags=%08x)\n",
3443 hProv, hKey, dwParam, pbData, pdwDataLen, dwFlags);
3444
3445 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
3446 {
3447 SetLastError(NTE_BAD_UID);
3448 return FALSE;
3449 }
3450
3451 if (dwFlags) {
3452 SetLastError(NTE_BAD_FLAGS);
3453 return FALSE;
3454 }
3455
3456 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
3457 {
3458 SetLastError(NTE_BAD_KEY);
3459 return FALSE;
3460 }
3461
3462 switch (dwParam)
3463 {
3464 case KP_IV:
3465 return copy_param(pbData, pdwDataLen, pCryptKey->abInitVector,
3466 pCryptKey->dwBlockLen);
3467
3468 case KP_SALT:
3469 return copy_param(pbData, pdwDataLen,
3470 &pCryptKey->abKeyValue[pCryptKey->dwKeyLen], pCryptKey->dwSaltLen);
3471
3472 case KP_PADDING:
3473 dwValue = PKCS5_PADDING;
3474 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwValue, sizeof(DWORD));
3475
3476 case KP_KEYLEN:
3477 dwValue = pCryptKey->dwKeyLen << 3;
3478 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwValue, sizeof(DWORD));
3479
3480 case KP_EFFECTIVE_KEYLEN:
3481 if (pCryptKey->dwEffectiveKeyLen)
3482 dwValue = pCryptKey->dwEffectiveKeyLen;
3483 else
3484 dwValue = pCryptKey->dwKeyLen << 3;
3485 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwValue, sizeof(DWORD));
3486
3487 case KP_BLOCKLEN:
3488 dwValue = pCryptKey->dwBlockLen << 3;
3489 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwValue, sizeof(DWORD));
3490
3491 case KP_MODE:
3492 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptKey->dwMode, sizeof(DWORD));
3493
3494 case KP_MODE_BITS:
3495 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptKey->dwModeBits,
3496 sizeof(DWORD));
3497
3498 case KP_PERMISSIONS:
3499 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptKey->dwPermissions,
3500 sizeof(DWORD));
3501
3502 case KP_ALGID:
3503 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptKey->aiAlgid, sizeof(DWORD));
3504
3505 default:
3506 SetLastError(NTE_BAD_TYPE);
3507 return FALSE;
3508 }
3509 }
3510
3511 /******************************************************************************
3512 * CPGetProvParam (RSAENH.@)
3513 *
3514 * Query a CSP parameter.
3515 *
3516 * PARAMS
3517 * hProv [I] The key container that is to be queried.
3518 * dwParam [I] Specifies the parameter that is to be queried.
3519 * pbData [I] Pointer to the buffer where the parameter value will be stored.
3520 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
3521 * dwFlags [I] CRYPT_FIRST: Start enumeration (for PP_ENUMALGS{_EX}).
3522 *
3523 * RETURNS
3524 * Success: TRUE
3525 * Failure: FALSE
3526 * NOTES:
3527 * Defined dwParam types:
3528 * - PP_CONTAINER: Name of the key container.
3529 * - PP_NAME: Name of the cryptographic service provider.
3530 * - PP_SIG_KEYSIZE_INC: RSA signature keywidth granularity in bits.
3531 * - PP_KEYX_KEYSIZE_INC: RSA key-exchange keywidth granularity in bits.
3532 * - PP_ENUMALGS{_EX}: Query provider capabilities.
3533 */
3534 BOOL WINAPI RSAENH_CPGetProvParam(HCRYPTPROV hProv, DWORD dwParam, BYTE *pbData,
3535 DWORD *pdwDataLen, DWORD dwFlags)
3536 {
3537 KEYCONTAINER *pKeyContainer;
3538 PROV_ENUMALGS provEnumalgs;
3539 DWORD dwTemp;
3540 HKEY hKey;
3541
3542 /* This is for dwParam PP_CRYPT_COUNT_KEY_USE.
3543 * IE6 SP1 asks for it in the 'About' dialog.
3544 * Returning this BLOB seems to satisfy IE. The marked 0x00 seem
3545 * to be 'don't care's. If you know anything more specific about
3546 * this provider parameter, please report to wine-devel@winehq.org */
3547 static CONST BYTE abWTF[96] = {
3548 0xb0, 0x25, 0x63, 0x86, 0x9c, 0xab, 0xb6, 0x37,
3549 0xe8, 0x82, /**/0x00,/**/ 0x72, 0x06, 0xb2, /**/0x00,/**/ 0x3b,
3550 0x60, 0x35, /**/0x00,/**/ 0x3b, 0x88, 0xce, /**/0x00,/**/ 0x82,
3551 0xbc, 0x7a, /**/0x00,/**/ 0xb7, 0x4f, 0x7e, /**/0x00,/**/ 0xde,
3552 0x92, 0xf1, /**/0x00,/**/ 0x83, 0xea, 0x5e, /**/0x00,/**/ 0xc8,
3553 0x12, 0x1e, 0xd4, 0x06, 0xf7, 0x66, /**/0x00,/**/ 0x01,
3554 0x29, 0xa4, /**/0x00,/**/ 0xf8, 0x24, 0x0c, /**/0x00,/**/ 0x33,
3555 0x06, 0x80, /**/0x00,/**/ 0x02, 0x46, 0x0b, /**/0x00,/**/ 0x6d,
3556 0x5b, 0xca, /**/0x00,/**/ 0x9a, 0x10, 0xf0, /**/0x00,/**/ 0x05,
3557 0x19, 0xd0, /**/0x00,/**/ 0x2c, 0xf6, 0x27, /**/0x00,/**/ 0xaa,
3558 0x7c, 0x6f, /**/0x00,/**/ 0xb9, 0xd8, 0x72, /**/0x00,/**/ 0x03,
3559 0xf3, 0x81, /**/0x00,/**/ 0xfa, 0xe8, 0x26, /**/0x00,/**/ 0xca
3560 };
3561
3562 TRACE("(hProv=%08lx, dwParam=%08x, pbData=%p, pdwDataLen=%p, dwFlags=%08x)\n",
3563 hProv, dwParam, pbData, pdwDataLen, dwFlags);
3564
3565 if (!pdwDataLen) {
3566 SetLastError(ERROR_INVALID_PARAMETER);
3567 return FALSE;
3568 }
3569
3570 if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER,
3571 (OBJECTHDR**)&pKeyContainer))
3572 {
3573 /* MSDN: hProv not containing valid context handle */
3574 SetLastError(NTE_BAD_UID);
3575 return FALSE;
3576 }
3577
3578 switch (dwParam)
3579 {
3580 case PP_CONTAINER:
3581 case PP_UNIQUE_CONTAINER:/* MSDN says we can return the same value as PP_CONTAINER */
3582 return copy_param(pbData, pdwDataLen, (CONST BYTE*)pKeyContainer->szName,
3583 strlen(pKeyContainer->szName)+1);
3584
3585 case PP_NAME:
3586 return copy_param(pbData, pdwDataLen, (CONST BYTE*)pKeyContainer->szProvName,
3587 strlen(pKeyContainer->szProvName)+1);
3588
3589 case PP_PROVTYPE:
3590 dwTemp = PROV_RSA_FULL;
3591 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwTemp, sizeof(dwTemp));
3592
3593 case PP_KEYSPEC:
3594 dwTemp = AT_SIGNATURE | AT_KEYEXCHANGE;
3595 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwTemp, sizeof(dwTemp));
3596
3597 case PP_KEYSET_TYPE:
3598 dwTemp = pKeyContainer->dwFlags & CRYPT_MACHINE_KEYSET;
3599 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwTemp, sizeof(dwTemp));
3600
3601 case PP_KEYSTORAGE:
3602 dwTemp = CRYPT_SEC_DESCR;
3603 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwTemp, sizeof(dwTemp));
3604
3605 case PP_SIG_KEYSIZE_INC:
3606 case PP_KEYX_KEYSIZE_INC:
3607 dwTemp = 8;
3608 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwTemp, sizeof(dwTemp));
3609
3610 case PP_IMPTYPE:
3611 dwTemp = CRYPT_IMPL_SOFTWARE;
3612 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwTemp, sizeof(dwTemp));
3613
3614 case PP_VERSION:
3615 dwTemp = 0x00000200;
3616 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwTemp, sizeof(dwTemp));
3617
3618 case PP_ENUMCONTAINERS:
3619 if ((dwFlags & CRYPT_FIRST) == CRYPT_FIRST) pKeyContainer->dwEnumContainersCtr = 0;
3620
3621 if (!pbData) {
3622 *pdwDataLen = (DWORD)MAX_PATH + 1;
3623 return TRUE;
3624 }
3625
3626 if (!open_container_key("", dwFlags, &hKey))
3627 {
3628 SetLastError(ERROR_NO_MORE_ITEMS);
3629 return FALSE;
3630 }
3631
3632 dwTemp = *pdwDataLen;
3633 switch (RegEnumKeyExA(hKey, pKeyContainer->dwEnumContainersCtr, (LPSTR)pbData, &dwTemp,
3634 NULL, NULL, NULL, NULL))
3635 {
3636 case ERROR_MORE_DATA:
3637 *pdwDataLen = (DWORD)MAX_PATH + 1;
3638
3639 case ERROR_SUCCESS:
3640 pKeyContainer->dwEnumContainersCtr++;
3641 RegCloseKey(hKey);
3642 return TRUE;
3643
3644 case ERROR_NO_MORE_ITEMS:
3645 default:
3646 SetLastError(ERROR_NO_MORE_ITEMS);
3647 RegCloseKey(hKey);
3648 return FALSE;
3649 }
3650
3651 case PP_ENUMALGS:
3652 case PP_ENUMALGS_EX:
3653 if (((pKeyContainer->dwEnumAlgsCtr >= RSAENH_MAX_ENUMALGS-1) ||
3654 (!aProvEnumAlgsEx[pKeyContainer->dwPersonality]
3655 [pKeyContainer->dwEnumAlgsCtr+1].aiAlgid)) &&
3656 ((dwFlags & CRYPT_FIRST) != CRYPT_FIRST))
3657 {
3658 SetLastError(ERROR_NO_MORE_ITEMS);
3659 return FALSE;
3660 }
3661
3662 if (dwParam == PP_ENUMALGS) {
3663 if (pbData && (*pdwDataLen >= sizeof(PROV_ENUMALGS)))
3664 pKeyContainer->dwEnumAlgsCtr = ((dwFlags & CRYPT_FIRST) == CRYPT_FIRST) ?
3665 0 : pKeyContainer->dwEnumAlgsCtr+1;
3666
3667 provEnumalgs.aiAlgid = aProvEnumAlgsEx
3668 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr].aiAlgid;
3669 provEnumalgs.dwBitLen = aProvEnumAlgsEx
3670 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr].dwDefaultLen;
3671 provEnumalgs.dwNameLen = aProvEnumAlgsEx
3672 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr].dwNameLen;
3673 memcpy(provEnumalgs.szName, aProvEnumAlgsEx
3674 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr].szName,
3675 20*sizeof(CHAR));
3676
3677 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&provEnumalgs,
3678 sizeof(PROV_ENUMALGS));
3679 } else {
3680 if (pbData && (*pdwDataLen >= sizeof(PROV_ENUMALGS_EX)))
3681 pKeyContainer->dwEnumAlgsCtr = ((dwFlags & CRYPT_FIRST) == CRYPT_FIRST) ?
3682 0 : pKeyContainer->dwEnumAlgsCtr+1;
3683
3684 return copy_param(pbData, pdwDataLen,
3685 (CONST BYTE*)&aProvEnumAlgsEx
3686 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr],
3687 sizeof(PROV_ENUMALGS_EX));
3688 }
3689
3690 case PP_CRYPT_COUNT_KEY_USE: /* Asked for by IE About dialog */
3691 return copy_param(pbData, pdwDataLen, abWTF, sizeof(abWTF));
3692
3693 default:
3694 /* MSDN: Unknown parameter number in dwParam */
3695 SetLastError(NTE_BAD_TYPE);
3696 return FALSE;
3697 }
3698 }
3699
3700 /******************************************************************************
3701 * CPDeriveKey (RSAENH.@)
3702 *
3703 * Derives a key from a hash value.
3704 *
3705 * PARAMS
3706 * hProv [I] Key container for which a key is to be generated.
3707 * Algid [I] Crypto algorithm identifier for the key to be generated.
3708 * hBaseData [I] Hash from whose value the key will be derived.
3709 * dwFlags [I] See Notes.
3710 * phKey [O] The generated key.
3711 *
3712 * RETURNS
3713 * Success: TRUE
3714 * Failure: FALSE
3715 *
3716 * NOTES
3717 * Defined flags:
3718 * - CRYPT_EXPORTABLE: Key can be exported.
3719 * - CRYPT_NO_SALT: No salt is used for 40 bit keys.
3720 * - CRYPT_CREATE_SALT: Use remaining bits as salt value.
3721 */
3722 BOOL WINAPI RSAENH_CPDeriveKey(HCRYPTPROV hProv, ALG_ID Algid, HCRYPTHASH hBaseData,
3723 DWORD dwFlags, HCRYPTKEY *phKey)
3724 {
3725 CRYPTKEY *pCryptKey, *pMasterKey;
3726 CRYPTHASH *pCryptHash;
3727 BYTE abHashValue[RSAENH_MAX_HASH_SIZE*2];
3728 DWORD dwLen;
3729
3730 TRACE("(hProv=%08lx, Algid=%d, hBaseData=%08lx, dwFlags=%08x phKey=%p)\n", hProv, Algid,
3731 hBaseData, dwFlags, phKey);
3732
3733 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
3734 {
3735 SetLastError(NTE_BAD_UID);
3736 return FALSE;
3737 }
3738
3739 if (!lookup_handle(&handle_table, hBaseData, RSAENH_MAGIC_HASH,
3740 (OBJECTHDR**)&pCryptHash))
3741 {
3742 SetLastError(NTE_BAD_HASH);
3743 return FALSE;
3744 }
3745
3746 if (!phKey)
3747 {
3748 SetLastError(ERROR_INVALID_PARAMETER);
3749 return FALSE;
3750 }
3751
3752 switch (GET_ALG_CLASS(Algid))
3753 {
3754 case ALG_CLASS_DATA_ENCRYPT:
3755 *phKey = new_key(hProv, Algid, dwFlags, &pCryptKey);
3756 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) return FALSE;
3757
3758 /*
3759 * We derive the key material from the hash.
3760 * If the hash value is not large enough for the claimed key, we have to construct
3761 * a larger binary value based on the hash. This is documented in MSDN: CryptDeriveKey.
3762 */
3763 dwLen = RSAENH_MAX_HASH_SIZE;
3764 RSAENH_CPGetHashParam(pCryptHash->hProv, hBaseData, HP_HASHVAL, abHashValue, &dwLen, 0);
3765
3766 if (dwLen < pCryptKey->dwKeyLen) {
3767 BYTE pad1[RSAENH_HMAC_DEF_PAD_LEN], pad2[RSAENH_HMAC_DEF_PAD_LEN];
3768 BYTE old_hashval[RSAENH_MAX_HASH_SIZE];
3769 DWORD i;
3770
3771 memcpy(old_hashval, pCryptHash->abHashValue, RSAENH_MAX_HASH_SIZE);
3772
3773 for (i=0; i<RSAENH_HMAC_DEF_PAD_LEN; i++) {
3774 pad1[i] = RSAENH_HMAC_DEF_IPAD_CHAR ^ (i<dwLen ? abHashValue[i] : 0);
3775 pad2[i] = RSAENH_HMAC_DEF_OPAD_CHAR ^ (i<dwLen ? abHashValue[i] : 0);
3776 }
3777
3778 init_hash(pCryptHash);
3779 update_hash(pCryptHash, pad1, RSAENH_HMAC_DEF_PAD_LEN);
3780 finalize_hash(pCryptHash);
3781 memcpy(abHashValue, pCryptHash->abHashValue, pCryptHash->dwHashSize);
3782
3783 init_hash(pCryptHash);
3784 update_hash(pCryptHash, pad2, RSAENH_HMAC_DEF_PAD_LEN);
3785 finalize_hash(pCryptHash);
3786 memcpy(abHashValue+pCryptHash->dwHashSize, pCryptHash->abHashValue,
3787 pCryptHash->dwHashSize);
3788
3789 memcpy(pCryptHash->abHashValue, old_hashval, RSAENH_MAX_HASH_SIZE);
3790 }
3791
3792 memcpy(pCryptKey->abKeyValue, abHashValue,
3793 RSAENH_MIN(pCryptKey->dwKeyLen, sizeof(pCryptKey->abKeyValue)));
3794 break;
3795
3796 case ALG_CLASS_MSG_ENCRYPT:
3797 if (!lookup_handle(&handle_table, pCryptHash->hKey, RSAENH_MAGIC_KEY,
3798 (OBJECTHDR**)&pMasterKey))
3799 {
3800 SetLastError(NTE_FAIL); /* FIXME error code */
3801 return FALSE;
3802 }
3803
3804 switch (Algid)
3805 {
3806 /* See RFC 2246, chapter 6.3 Key calculation */
3807 case CALG_SCHANNEL_ENC_KEY:
3808 *phKey = new_key(hProv, pMasterKey->siSChannelInfo.saEncAlg.Algid,
3809 MAKELONG(LOWORD(dwFlags),pMasterKey->siSChannelInfo.saEncAlg.cBits),
3810 &pCryptKey);
3811 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) return FALSE;
3812 memcpy(pCryptKey->abKeyValue,
3813 pCryptHash->abHashValue + (
3814 2 * (pMasterKey->siSChannelInfo.saMACAlg.cBits / 8) +
3815 ((dwFlags & CRYPT_SERVER) ?
3816 (pMasterKey->siSChannelInfo.saEncAlg.cBits / 8) : 0)),
3817 pMasterKey->siSChannelInfo.saEncAlg.cBits / 8);
3818 memcpy(pCryptKey->abInitVector,
3819 pCryptHash->abHashValue + (
3820 2 * (pMasterKey->siSChannelInfo.saMACAlg.cBits / 8) +
3821 2 * (pMasterKey->siSChannelInfo.saEncAlg.cBits / 8) +
3822 ((dwFlags & CRYPT_SERVER) ? pCryptKey->dwBlockLen : 0)),
3823 pCryptKey->dwBlockLen);
3824 break;
3825
3826 case CALG_SCHANNEL_MAC_KEY:
3827 *phKey = new_key(hProv, Algid,
3828 MAKELONG(LOWORD(dwFlags),pMasterKey->siSChannelInfo.saMACAlg.cBits),
3829 &pCryptKey);
3830 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) return FALSE;
3831 memcpy(pCryptKey->abKeyValue,
3832 pCryptHash->abHashValue + ((dwFlags & CRYPT_SERVER) ?
3833 pMasterKey->siSChannelInfo.saMACAlg.cBits / 8 : 0),
3834 pMasterKey->siSChannelInfo.saMACAlg.cBits / 8);
3835 break;
3836
3837 default:
3838 SetLastError(NTE_BAD_ALGID);
3839 return FALSE;
3840 }
3841 break;
3842
3843 default:
3844 SetLastError(NTE_BAD_ALGID);
3845 return FALSE;
3846 }
3847
3848 setup_key(pCryptKey);
3849 return TRUE;
3850 }
3851
3852 /******************************************************************************
3853 * CPGetUserKey (RSAENH.@)
3854 *
3855 * Returns a handle to the user's private key-exchange- or signature-key.
3856 *
3857 * PARAMS
3858 * hProv [I] The key container from which a user key is requested.
3859 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
3860 * phUserKey [O] Handle to the requested key or INVALID_HANDLE_VALUE in case of failure.
3861 *
3862 * RETURNS
3863 * Success: TRUE.
3864 * Failure: FALSE.
3865 *
3866 * NOTE
3867 * A newly created key container does not contain private user key. Create them with CPGenKey.
3868 */
3869 BOOL WINAPI RSAENH_CPGetUserKey(HCRYPTPROV hProv, DWORD dwKeySpec, HCRYPTKEY *phUserKey)
3870 {
3871 KEYCONTAINER *pKeyContainer;
3872
3873 TRACE("(hProv=%08lx, dwKeySpec=%08x, phUserKey=%p)\n", hProv, dwKeySpec, phUserKey);
3874
3875 if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER,
3876 (OBJECTHDR**)&pKeyContainer))
3877 {
3878 /* MSDN: hProv not containing valid context handle */
3879 SetLastError(NTE_BAD_UID);
3880 return FALSE;
3881 }
3882
3883 switch (dwKeySpec)
3884 {
3885 case AT_KEYEXCHANGE:
3886 copy_handle(&handle_table, pKeyContainer->hKeyExchangeKeyPair, RSAENH_MAGIC_KEY,
3887 phUserKey);
3888 break;
3889
3890 case AT_SIGNATURE:
3891 copy_handle(&handle_table, pKeyContainer->hSignatureKeyPair, RSAENH_MAGIC_KEY,
3892 phUserKey);
3893 break;
3894
3895 default:
3896 *phUserKey = (HCRYPTKEY)INVALID_HANDLE_VALUE;
3897 }
3898
3899 if (*phUserKey == (HCRYPTKEY)INVALID_HANDLE_VALUE)
3900 {
3901 /* MSDN: dwKeySpec parameter specifies nonexistent key */
3902 SetLastError(NTE_NO_KEY);
3903 return FALSE;
3904 }
3905
3906 return TRUE;
3907 }
3908
3909 /******************************************************************************
3910 * CPHashData (RSAENH.@)
3911 *
3912 * Updates a hash object with the given data.
3913 *
3914 * PARAMS
3915 * hProv [I] Key container to which the hash object belongs.
3916 * hHash [I] Hash object which is to be updated.
3917 * pbData [I] Pointer to data with which the hash object is to be updated.
3918 * dwDataLen [I] Length of the data.
3919 * dwFlags [I] Currently none defined.
3920 *
3921 * RETURNS
3922 * Success: TRUE.
3923 * Failure: FALSE.
3924 *
3925 * NOTES
3926 * The actual hash value is queried with CPGetHashParam, which will finalize
3927 * the hash. Updating a finalized hash will fail with a last error NTE_BAD_HASH_STATE.
3928 */
3929 BOOL WINAPI RSAENH_CPHashData(HCRYPTPROV hProv, HCRYPTHASH hHash, CONST BYTE *pbData,
3930 DWORD dwDataLen, DWORD dwFlags)
3931 {
3932 CRYPTHASH *pCryptHash;
3933
3934 TRACE("(hProv=%08lx, hHash=%08lx, pbData=%p, dwDataLen=%d, dwFlags=%08x)\n",
3935 hProv, hHash, pbData, dwDataLen, dwFlags);
3936
3937 if (dwFlags)
3938 {
3939 SetLastError(NTE_BAD_FLAGS);
3940 return FALSE;
3941 }
3942
3943 if (!lookup_handle(&handle_table, hHash, RSAENH_MAGIC_HASH,
3944 (OBJECTHDR**)&pCryptHash))
3945 {
3946 SetLastError(NTE_BAD_HASH);
3947 return FALSE;
3948 }
3949
3950 if (!get_algid_info(hProv, pCryptHash->aiAlgid) || pCryptHash->aiAlgid == CALG_SSL3_SHAMD5)
3951 {
3952 SetLastError(NTE_BAD_ALGID);
3953 return FALSE;
3954 }
3955
3956 if (pCryptHash->dwState != RSAENH_HASHSTATE_HASHING)
3957 {
3958 SetLastError(NTE_BAD_HASH_STATE);
3959 return FALSE;
3960 }
3961
3962 update_hash(pCryptHash, pbData, dwDataLen);
3963 return TRUE;
3964 }
3965
3966 /******************************************************************************
3967 * CPHashSessionKey (RSAENH.@)
3968 *
3969 * Updates a hash object with the binary representation of a symmetric key.
3970 *
3971 * PARAMS
3972 * hProv [I] Key container to which the hash object belongs.
3973 * hHash [I] Hash object which is to be updated.
3974 * hKey [I] The symmetric key, whose binary value will be added to the hash.
3975 * dwFlags [I] CRYPT_LITTLE_ENDIAN, if the binary key value shall be interpreted as little endian.
3976 *
3977 * RETURNS
3978 * Success: TRUE.
3979 * Failure: FALSE.
3980 */
3981 BOOL WINAPI RSAENH_CPHashSessionKey(HCRYPTPROV hProv, HCRYPTHASH hHash, HCRYPTKEY hKey,
3982 DWORD dwFlags)
3983 {
3984 BYTE abKeyValue[RSAENH_MAX_KEY_SIZE], bTemp;
3985 CRYPTKEY *pKey;
3986 DWORD i;
3987
3988 TRACE("(hProv=%08lx, hHash=%08lx, hKey=%08lx, dwFlags=%08x)\n", hProv, hHash, hKey, dwFlags);
3989
3990 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pKey) ||
3991 (GET_ALG_CLASS(pKey->aiAlgid) != ALG_CLASS_DATA_ENCRYPT))
3992 {
3993 SetLastError(NTE_BAD_KEY);
3994 return FALSE;
3995 }
3996
3997 if (dwFlags & ~CRYPT_LITTLE_ENDIAN) {
3998 SetLastError(NTE_BAD_FLAGS);
3999 return FALSE;
4000 }
4001
4002 memcpy(abKeyValue, pKey->abKeyValue, pKey->dwKeyLen);
4003 if (!(dwFlags & CRYPT_LITTLE_ENDIAN)) {
4004 for (i=0; i<pKey->dwKeyLen/2; i++) {
4005 bTemp = abKeyValue[i];
4006 abKeyValue[i] = abKeyValue[pKey->dwKeyLen-i-1];
4007 abKeyValue[pKey->dwKeyLen-i-1] = bTemp;
4008 }
4009 }
4010
4011 return RSAENH_CPHashData(hProv, hHash, abKeyValue, pKey->dwKeyLen, 0);
4012 }
4013
4014 /******************************************************************************
4015 * CPReleaseContext (RSAENH.@)
4016 *
4017 * Release a key container.
4018 *
4019 * PARAMS
4020 * hProv [I] Key container to be released.
4021 * dwFlags [I] Currently none defined.
4022 *
4023 * RETURNS
4024 * Success: TRUE
4025 * Failure: FALSE
4026 */
4027 BOOL WINAPI RSAENH_CPReleaseContext(HCRYPTPROV hProv, DWORD dwFlags)
4028 {
4029 TRACE("(hProv=%08lx, dwFlags=%08x)\n", hProv, dwFlags);
4030
4031 if (!release_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
4032 {
4033 /* MSDN: hProv not containing valid context handle */
4034 SetLastError(NTE_BAD_UID);
4035 return FALSE;
4036 }
4037
4038 if (dwFlags) {
4039 SetLastError(NTE_BAD_FLAGS);
4040 return FALSE;
4041 }
4042
4043 return TRUE;
4044 }
4045
4046 /******************************************************************************
4047 * CPSetHashParam (RSAENH.@)
4048 *
4049 * Set a parameter of a hash object
4050 *
4051 * PARAMS
4052 * hProv [I] The key container to which the key belongs.
4053 * hHash [I] The hash object for which a parameter is to be set.
4054 * dwParam [I] Parameter type. See Notes.
4055 * pbData [I] Pointer to the parameter value.
4056 * dwFlags [I] Currently none defined.
4057 *
4058 * RETURNS
4059 * Success: TRUE.
4060 * Failure: FALSE.
4061 *
4062 * NOTES
4063 * Currently only the HP_HMAC_INFO dwParam type is defined.
4064 * The HMAC_INFO struct will be deep copied into the hash object.
4065 * See Internet RFC 2104 for details on the HMAC algorithm.
4066 */
4067 BOOL WINAPI RSAENH_CPSetHashParam(HCRYPTPROV hProv, HCRYPTHASH hHash, DWORD dwParam,
4068 BYTE *pbData, DWORD dwFlags)
4069 {
4070 CRYPTHASH *pCryptHash;
4071 CRYPTKEY *pCryptKey;
4072 DWORD i;
4073
4074 TRACE("(hProv=%08lx, hHash=%08lx, dwParam=%08x, pbData=%p, dwFlags=%08x)\n",
4075 hProv, hHash, dwParam, pbData, dwFlags);
4076
4077 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
4078 {
4079 SetLastError(NTE_BAD_UID);
4080 return FALSE;
4081 }
4082
4083 if (dwFlags) {
4084 SetLastError(NTE_BAD_FLAGS);
4085 return FALSE;
4086 }
4087
4088 if (!lookup_handle(&handle_table, hHash, RSAENH_MAGIC_HASH,
4089 (OBJECTHDR**)&pCryptHash))
4090 {
4091 SetLastError(NTE_BAD_HASH);
4092 return FALSE;
4093 }
4094
4095 switch (dwParam) {
4096 case HP_HMAC_INFO:
4097 free_hmac_info(pCryptHash->pHMACInfo);
4098 if (!copy_hmac_info(&pCryptHash->pHMACInfo, (PHMAC_INFO)pbData)) return FALSE;
4099
4100 if (!lookup_handle(&handle_table, pCryptHash->hKey, RSAENH_MAGIC_KEY,
4101 (OBJECTHDR**)&pCryptKey))
4102 {
4103 SetLastError(NTE_FAIL); /* FIXME: correct error code? */
4104 return FALSE;
4105 }
4106
4107 for (i=0; i<RSAENH_MIN(pCryptKey->dwKeyLen,pCryptHash->pHMACInfo->cbInnerString); i++) {
4108 pCryptHash->pHMACInfo->pbInnerString[i] ^= pCryptKey->abKeyValue[i];
4109 }
4110 for (i=0; i<RSAENH_MIN(pCryptKey->dwKeyLen,pCryptHash->pHMACInfo->cbOuterString); i++) {
4111 pCryptHash->pHMACInfo->pbOuterString[i] ^= pCryptKey->abKeyValue[i];
4112 }
4113
4114 init_hash(pCryptHash);
4115 return TRUE;
4116
4117 case HP_HASHVAL:
4118 memcpy(pCryptHash->abHashValue, pbData, pCryptHash->dwHashSize);
4119 pCryptHash->dwState = RSAENH_HASHSTATE_FINISHED;
4120 return TRUE;
4121
4122 case HP_TLS1PRF_SEED:
4123 return copy_data_blob(&pCryptHash->tpPRFParams.blobSeed, (PCRYPT_DATA_BLOB)pbData);
4124
4125 case HP_TLS1PRF_LABEL:
4126 return copy_data_blob(&pCryptHash->tpPRFParams.blobLabel, (PCRYPT_DATA_BLOB)pbData);
4127
4128 default:
4129 SetLastError(NTE_BAD_TYPE);
4130 return FALSE;
4131 }
4132 }
4133
4134 /******************************************************************************
4135 * CPSetProvParam (RSAENH.@)
4136 */
4137 BOOL WINAPI RSAENH_CPSetProvParam(HCRYPTPROV hProv, DWORD dwParam, BYTE *pbData, DWORD dwFlags)
4138 {
4139 FIXME("(stub)\n");
4140 return FALSE;
4141 }
4142
4143 /******************************************************************************
4144 * CPSignHash (RSAENH.@)
4145 *
4146 * Sign a hash object
4147 *
4148 * PARAMS
4149 * hProv [I] The key container, to which the hash object belongs.
4150 * hHash [I] The hash object to be signed.
4151 * dwKeySpec [I] AT_SIGNATURE or AT_KEYEXCHANGE: Key used to generate the signature.
4152 * sDescription [I] Should be NULL for security reasons.
4153 * dwFlags [I] 0, CRYPT_NOHASHOID or CRYPT_X931_FORMAT: Format of the signature.
4154 * pbSignature [O] Buffer, to which the signature will be stored. May be NULL to query SigLen.
4155 * pdwSigLen [I/O] Size of the buffer (in), Length of the signature (out)
4156 *
4157 * RETURNS
4158 * Success: TRUE
4159 * Failure: FALSE
4160 */
4161 BOOL WINAPI RSAENH_CPSignHash(HCRYPTPROV hProv, HCRYPTHASH hHash, DWORD dwKeySpec,
4162 LPCWSTR sDescription, DWORD dwFlags, BYTE *pbSignature,
4163 DWORD *pdwSigLen)
4164 {
4165 HCRYPTKEY hCryptKey;
4166 CRYPTKEY *pCryptKey;
4167 DWORD dwHashLen;
4168 BYTE abHashValue[RSAENH_MAX_HASH_SIZE];
4169 ALG_ID aiAlgid;
4170
4171 TRACE("(hProv=%08lx, hHash=%08lx, dwKeySpec=%08x, sDescription=%s, dwFlags=%08x, "
4172 "pbSignature=%p, pdwSigLen=%p)\n", hProv, hHash, dwKeySpec, debugstr_w(sDescription),
4173 dwFlags, pbSignature, pdwSigLen);
4174
4175 if (dwFlags & ~(CRYPT_NOHASHOID|CRYPT_X931_FORMAT)) {
4176 SetLastError(NTE_BAD_FLAGS);
4177 return FALSE;
4178 }
4179
4180 if (!RSAENH_CPGetUserKey(hProv, dwKeySpec, &hCryptKey)) return FALSE;
4181
4182 if (!lookup_handle(&handle_table, hCryptKey, RSAENH_MAGIC_KEY,
4183 (OBJECTHDR**)&pCryptKey))
4184 {
4185 SetLastError(NTE_NO_KEY);
4186 return FALSE;
4187 }
4188
4189 if (!pbSignature) {
4190 *pdwSigLen = pCryptKey->dwKeyLen;
4191 return TRUE;
4192 }
4193 if (pCryptKey->dwKeyLen > *pdwSigLen)
4194 {
4195 SetLastError(ERROR_MORE_DATA);
4196 *pdwSigLen = pCryptKey->dwKeyLen;
4197 return FALSE;
4198 }
4199 *pdwSigLen = pCryptKey->dwKeyLen;
4200
4201 if (sDescription) {
4202 if (!RSAENH_CPHashData(hProv, hHash, (CONST BYTE*)sDescription,
4203 (DWORD)lstrlenW(sDescription)*sizeof(WCHAR), 0))
4204 {
4205 return FALSE;
4206 }
4207 }
4208
4209 dwHashLen = sizeof(DWORD);
4210 if (!RSAENH_CPGetHashParam(hProv, hHash, HP_ALGID, (BYTE*)&aiAlgid, &dwHashLen, 0)) return FALSE;
4211
4212 dwHashLen = RSAENH_MAX_HASH_SIZE;
4213 if (!RSAENH_CPGetHashParam(hProv, hHash, HP_HASHVAL, abHashValue, &dwHashLen, 0)) return FALSE;
4214
4215
4216 if (!build_hash_signature(pbSignature, *pdwSigLen, aiAlgid, abHashValue, dwHashLen, dwFlags)) {
4217 return FALSE;
4218 }
4219
4220 return encrypt_block_impl(pCryptKey->aiAlgid, PK_PRIVATE, &pCryptKey->context, pbSignature, pbSignature, RSAENH_ENCRYPT);
4221 }
4222
4223 /******************************************************************************
4224 * CPVerifySignature (RSAENH.@)
4225 *
4226 * Verify the signature of a hash object.
4227 *
4228 * PARAMS
4229 * hProv [I] The key container, to which the hash belongs.
4230 * hHash [I] The hash for which the signature is verified.
4231 * pbSignature [I] The binary signature.
4232 * dwSigLen [I] Length of the signature BLOB.
4233 * hPubKey [I] Public key used to verify the signature.
4234 * sDescription [I] Should be NULL for security reasons.
4235 * dwFlags [I] 0, CRYPT_NOHASHOID or CRYPT_X931_FORMAT: Format of the signature.
4236 *
4237 * RETURNS
4238 * Success: TRUE (Signature is valid)
4239 * Failure: FALSE (GetLastError() == NTE_BAD_SIGNATURE, if signature is invalid)
4240 */
4241 BOOL WINAPI RSAENH_CPVerifySignature(HCRYPTPROV hProv, HCRYPTHASH hHash, CONST BYTE *pbSignature,
4242 DWORD dwSigLen, HCRYPTKEY hPubKey, LPCWSTR sDescription,
4243 DWORD dwFlags)
4244 {
4245 BYTE *pbConstructed = NULL, *pbDecrypted = NULL;
4246 CRYPTKEY *pCryptKey;
4247 DWORD dwHashLen;
4248 ALG_ID aiAlgid;
4249 BYTE abHashValue[RSAENH_MAX_HASH_SIZE];
4250 BOOL res = FALSE;
4251
4252 TRACE("(hProv=%08lx, hHash=%08lx, pbSignature=%p, dwSigLen=%d, hPubKey=%08lx, sDescription=%s, "
4253 "dwFlags=%08x)\n", hProv, hHash, pbSignature, dwSigLen, hPubKey, debugstr_w(sDescription),
4254 dwFlags);
4255
4256 if (dwFlags & ~(CRYPT_NOHASHOID|CRYPT_X931_FORMAT)) {
4257 SetLastError(NTE_BAD_FLAGS);
4258 return FALSE;
4259 }
4260
4261 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
4262 {
4263 SetLastError(NTE_BAD_UID);
4264 return FALSE;
4265 }
4266
4267 if (!lookup_handle(&handle_table, hPubKey, RSAENH_MAGIC_KEY,
4268 (OBJECTHDR**)&pCryptKey))
4269 {
4270 SetLastError(NTE_BAD_KEY);
4271 return FALSE;
4272 }
4273
4274 /* in Microsoft implementation, the signature length is checked before
4275 * the signature pointer.
4276 */
4277 if (dwSigLen != pCryptKey->dwKeyLen)
4278 {
4279 SetLastError(NTE_BAD_SIGNATURE);
4280 return FALSE;
4281 }
4282
4283 if (!hHash || !pbSignature)
4284 {
4285 SetLastError(ERROR_INVALID_PARAMETER);
4286 return FALSE;
4287 }
4288
4289 if (sDescription) {
4290 if (!RSAENH_CPHashData(hProv, hHash, (CONST BYTE*)sDescription,
4291 (DWORD)lstrlenW(sDescription)*sizeof(WCHAR), 0))
4292 {
4293 return FALSE;
4294 }
4295 }
4296
4297 dwHashLen = sizeof(DWORD);
4298 if (!RSAENH_CPGetHashParam(hProv, hHash, HP_ALGID, (BYTE*)&aiAlgid, &dwHashLen, 0)) return FALSE;
4299
4300 dwHashLen = RSAENH_MAX_HASH_SIZE;
4301 if (!RSAENH_CPGetHashParam(hProv, hHash, HP_HASHVAL, abHashValue, &dwHashLen, 0)) return FALSE;
4302
4303 pbConstructed = HeapAlloc(GetProcessHeap(), 0, dwSigLen);
4304 if (!pbConstructed) {
4305 SetLastError(NTE_NO_MEMORY);
4306 goto cleanup;
4307 }
4308
4309 pbDecrypted = HeapAlloc(GetProcessHeap(), 0, dwSigLen);
4310 if (!pbDecrypted) {
4311 SetLastError(NTE_NO_MEMORY);
4312 goto cleanup;
4313 }
4314
4315 if (!encrypt_block_impl(pCryptKey->aiAlgid, PK_PUBLIC, &pCryptKey->context, pbSignature, pbDecrypted,
4316 RSAENH_DECRYPT))
4317 {
4318 goto cleanup;
4319 }
4320
4321 if (!build_hash_signature(pbConstructed, dwSigLen, aiAlgid, abHashValue, dwHashLen, dwFlags)) {
4322 goto cleanup;
4323 }
4324
4325 if (memcmp(pbDecrypted, pbConstructed, dwSigLen)) {
4326 SetLastError(NTE_BAD_SIGNATURE);
4327 goto cleanup;
4328 }
4329
4330 res = TRUE;
4331 cleanup:
4332 HeapFree(GetProcessHeap(), 0, pbConstructed);
4333 HeapFree(GetProcessHeap(), 0, pbDecrypted);
4334 return res;
4335 }
4336
4337 static const WCHAR szProviderKeys[6][116] = {
4338 { 'S','o','f','t','w','a','r','e','\\',
4339 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
4340 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
4341 'i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ','B','a','s',
4342 'e',' ','C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r',
4343 'o','v','i','d','e','r',' ','v','1','.','0',0 },
4344 { 'S','o','f','t','w','a','r','e','\\',
4345 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
4346 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
4347 'i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ',
4348 'E','n','h','a','n','c','e','d',
4349 ' ','C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r',
4350 'o','v','i','d','e','r',' ','v','1','.','0',0 },
4351 { 'S','o','f','t','w','a','r','e','\\',
4352 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
4353 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
4354 'i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ','S','t','r','o','n','g',
4355 ' ','C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r',
4356 'o','v','i','d','e','r',0 },
4357 { 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
4358 'C','r','y','p','t','o','g','r','a','p','h','y','\\','D','e','f','a','u','l','t','s','\\',
4359 'P','r','o','v','i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ',
4360 'R','S','A',' ','S','C','h','a','n','n','e','l',' ',
4361 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r','o','v','i','d','e','r',0 },
4362 { 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
4363 'C','r','y','p','t','o','g','r','a','p','h','y','\\','D','e','f','a','u','l','t','s','\\',
4364 'P','r','o','v','i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ',
4365 'E','n','h','a','n','c','e','d',' ','R','S','A',' ','a','n','d',' ','A','E','S',' ',
4366 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r','o','v','i','d','e','r',0 },
4367 { 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
4368 'C','r','y','p','t','o','g','r','a','p','h','y','\\','D','e','f','a','u','l','t','s','\\',
4369 'P','r','o','v','i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ',
4370 'E','n','h','a','n','c','e','d',' ','R','S','A',' ','a','n','d',' ','A','E','S',' ',
4371 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r','o','v','i','d','e','r',
4372 ' ','(','P','r','o','t','o','t','y','p','e',')',0 }
4373 };
4374 static const WCHAR szDefaultKeys[3][65] = {
4375 { 'S','o','f','t','w','a','r','e','\\',
4376 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
4377 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
4378 'i','d','e','r',' ','T','y','p','e','s','\\','T','y','p','e',' ','0','0','1',0 },
4379 { 'S','o','f','t','w','a','r','e','\\',
4380 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
4381 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
4382 'i','d','e','r',' ','T','y','p','e','s','\\','T','y','p','e',' ','0','1','2',0 },
4383 { 'S','o','f','t','w','a','r','e','\\',
4384 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
4385 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
4386 'i','d','e','r',' ','T','y','p','e','s','\\','T','y','p','e',' ','0','2','4',0 }
4387 };
4388
4389
4390 /******************************************************************************
4391 * DllRegisterServer (RSAENH.@)
4392 *
4393 * Dll self registration.
4394 *
4395 * PARAMS
4396 *
4397 * RETURNS
4398 * Success: S_OK.
4399 * Failure: != S_OK
4400 *
4401 * NOTES
4402 * Registers the following keys:
4403 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider\
4404 * Microsoft Base Cryptographic Provider v1.0
4405 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider\
4406 * Microsoft Enhanced Cryptographic Provider
4407 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider\
4408 * Microsoft Strong Cryptographpic Provider
4409 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider Types\Type 001
4410 */
4411 HRESULT WINAPI DllRegisterServer(void)
4412 {
4413 HKEY key;
4414 DWORD dp;
4415 long apiRet;
4416 int i;
4417
4418 for (i=0; i<6; i++) {
4419 apiRet = RegCreateKeyExW(HKEY_LOCAL_MACHINE, szProviderKeys[i], 0, NULL,
4420 REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &key, &dp);
4421
4422 if (apiRet == ERROR_SUCCESS)
4423 {
4424 if (dp == REG_CREATED_NEW_KEY)
4425 {
4426 static const WCHAR szImagePath[] = { 'I','m','a','g','e',' ','P','a','t','h',0 };
4427 static const WCHAR szRSABase[] = { 'r','s','a','e','n','h','.','d','l','l',0 };
4428 static const WCHAR szType[] = { 'T','y','p','e',0 };
4429 static const WCHAR szSignature[] = { 'S','i','g','n','a','t','u','r','e',0 };
4430 DWORD type, sign;
4431
4432 switch(i)
4433 {
4434 case 3:
4435 type=PROV_RSA_SCHANNEL;
4436 break;
4437 case 4:
4438 case 5:
4439 type=PROV_RSA_AES;
4440 break;
4441 default:
4442 type=PROV_RSA_FULL;
4443 break;
4444 }
4445 sign = 0xdeadbeef;
4446 RegSetValueExW(key, szImagePath, 0, REG_SZ, (const BYTE *)szRSABase,
4447 (lstrlenW(szRSABase) + 1) * sizeof(WCHAR));
4448 RegSetValueExW(key, szType, 0, REG_DWORD, (LPBYTE)&type, sizeof(type));
4449 RegSetValueExW(key, szSignature, 0, REG_BINARY, (LPBYTE)&sign, sizeof(sign));
4450 }
4451 RegCloseKey(key);
4452 }
4453 }
4454
4455 for (i=0; i<3; i++) {
4456 apiRet = RegCreateKeyExW(HKEY_LOCAL_MACHINE, szDefaultKeys[i], 0, NULL,
4457 REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &key, &dp);
4458 if (apiRet == ERROR_SUCCESS)
4459 {
4460 if (dp == REG_CREATED_NEW_KEY)
4461 {
4462 static const WCHAR szName[] = { 'N','a','m','e',0 };
4463 static const WCHAR szRSAName[3][54] = {
4464 { 'M','i','c','r','o','s','o','f','t',' ',
4465 'E','n','h','a','n','c','e','d',' ',
4466 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ',
4467 'P','r','o','v','i','d','e','r',' ','v','1','.','0',0 },
4468 { 'M','i','c','r','o','s','o','f','t',' ','R','S','A',' ',
4469 'S','C','h','a','n','n','e','l',' ',
4470 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ',
4471 'P','r','o','v','i','d','e','r',0 },
4472 { 'M','i','c','r','o','s','o','f','t',' ','E','n','h','a','n','c','e','d',' ',
4473 'R','S','A',' ','a','n','d',' ','A','E','S',' ',
4474 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ',
4475 'P','r','o','v','i','d','e','r',0 } };
4476 static const WCHAR szTypeName[] = { 'T','y','p','e','N','a','m','e',0 };
4477 static const WCHAR szRSATypeName[3][38] = {
4478 { 'R','S','A',' ','F','u','l','l',' ',
4479 '(','S','i','g','n','a','t','u','r','e',' ','a','n','d',' ',
4480 'K','e','y',' ','E','x','c','h','a','n','g','e',')',0 },
4481 { 'R','S','A',' ','S','C','h','a','n','n','e','l',0 },
4482 { 'R','S','A',' ','F','u','l','l',' ','a','n','d',' ','A','E','S',0 } };
4483
4484 RegSetValueExW(key, szName, 0, REG_SZ,
4485 (const BYTE *)szRSAName[i], lstrlenW(szRSAName[i])*sizeof(WCHAR)+sizeof(WCHAR));
4486 RegSetValueExW(key, szTypeName, 0, REG_SZ,
4487 (const BYTE *)szRSATypeName[i], lstrlenW(szRSATypeName[i])*sizeof(WCHAR)+sizeof(WCHAR));
4488 }
4489 }
4490 RegCloseKey(key);
4491 }
4492
4493 return HRESULT_FROM_WIN32(apiRet);
4494 }
4495
4496 /******************************************************************************
4497 * DllUnregisterServer (RSAENH.@)
4498 *
4499 * Dll self unregistration.
4500 *
4501 * PARAMS
4502 *
4503 * RETURNS
4504 * Success: S_OK
4505 *
4506 * NOTES
4507 * For the relevant keys see DllRegisterServer.
4508 */
4509 HRESULT WINAPI DllUnregisterServer(void)
4510 {
4511 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szProviderKeys[0]);
4512 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szProviderKeys[1]);
4513 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szProviderKeys[2]);
4514 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szProviderKeys[3]);
4515 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szProviderKeys[4]);
4516 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szProviderKeys[5]);
4517 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szDefaultKeys[0]);
4518 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szDefaultKeys[1]);
4519 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szDefaultKeys[2]);
4520 return S_OK;
4521 }