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