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