* Sync up to trunk head (r64921).
[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 #include <aclapi.h>
46
47 WINE_DEFAULT_DEBUG_CHANNEL(crypt);
48
49 static HINSTANCE instance;
50
51 /******************************************************************************
52 * CRYPTHASH - hash objects
53 */
54 #define RSAENH_MAGIC_HASH 0x85938417u
55 #define RSAENH_MAX_HASH_SIZE 104
56 #define RSAENH_HASHSTATE_HASHING 1
57 #define RSAENH_HASHSTATE_FINISHED 2
58 typedef struct _RSAENH_TLS1PRF_PARAMS
59 {
60 CRYPT_DATA_BLOB blobLabel;
61 CRYPT_DATA_BLOB blobSeed;
62 } RSAENH_TLS1PRF_PARAMS;
63
64 typedef struct tagCRYPTHASH
65 {
66 OBJECTHDR header;
67 ALG_ID aiAlgid;
68 HCRYPTKEY hKey;
69 HCRYPTPROV hProv;
70 DWORD dwHashSize;
71 DWORD dwState;
72 HASH_CONTEXT context;
73 BYTE abHashValue[RSAENH_MAX_HASH_SIZE];
74 PHMAC_INFO pHMACInfo;
75 RSAENH_TLS1PRF_PARAMS tpPRFParams;
76 } CRYPTHASH;
77
78 /******************************************************************************
79 * CRYPTKEY - key objects
80 */
81 #define RSAENH_MAGIC_KEY 0x73620457u
82 #define RSAENH_MAX_KEY_SIZE 64
83 #define RSAENH_MAX_BLOCK_SIZE 24
84 #define RSAENH_KEYSTATE_IDLE 0
85 #define RSAENH_KEYSTATE_ENCRYPTING 1
86 #define RSAENH_KEYSTATE_MASTERKEY 2
87 typedef struct _RSAENH_SCHANNEL_INFO
88 {
89 SCHANNEL_ALG saEncAlg;
90 SCHANNEL_ALG saMACAlg;
91 CRYPT_DATA_BLOB blobClientRandom;
92 CRYPT_DATA_BLOB blobServerRandom;
93 } RSAENH_SCHANNEL_INFO;
94
95 typedef struct tagCRYPTKEY
96 {
97 OBJECTHDR header;
98 ALG_ID aiAlgid;
99 HCRYPTPROV hProv;
100 DWORD dwMode;
101 DWORD dwModeBits;
102 DWORD dwPermissions;
103 DWORD dwKeyLen;
104 DWORD dwEffectiveKeyLen;
105 DWORD dwSaltLen;
106 DWORD dwBlockLen;
107 DWORD dwState;
108 KEY_CONTEXT context;
109 BYTE abKeyValue[RSAENH_MAX_KEY_SIZE];
110 BYTE abInitVector[RSAENH_MAX_BLOCK_SIZE];
111 BYTE abChainVector[RSAENH_MAX_BLOCK_SIZE];
112 RSAENH_SCHANNEL_INFO siSChannelInfo;
113 CRYPT_DATA_BLOB blobHmacKey;
114 } CRYPTKEY;
115
116 /******************************************************************************
117 * KEYCONTAINER - key containers
118 */
119 #define RSAENH_PERSONALITY_BASE 0u
120 #define RSAENH_PERSONALITY_STRONG 1u
121 #define RSAENH_PERSONALITY_ENHANCED 2u
122 #define RSAENH_PERSONALITY_SCHANNEL 3u
123 #define RSAENH_PERSONALITY_AES 4u
124
125 #define RSAENH_MAGIC_CONTAINER 0x26384993u
126 typedef struct tagKEYCONTAINER
127 {
128 OBJECTHDR header;
129 DWORD dwFlags;
130 DWORD dwPersonality;
131 DWORD dwEnumAlgsCtr;
132 DWORD dwEnumContainersCtr;
133 CHAR szName[MAX_PATH];
134 CHAR szProvName[MAX_PATH];
135 HCRYPTKEY hKeyExchangeKeyPair;
136 HCRYPTKEY hSignatureKeyPair;
137 } KEYCONTAINER;
138
139 /******************************************************************************
140 * Some magic constants
141 */
142 #define RSAENH_ENCRYPT 1
143 #define RSAENH_DECRYPT 0
144 #define RSAENH_HMAC_DEF_IPAD_CHAR 0x36
145 #define RSAENH_HMAC_DEF_OPAD_CHAR 0x5c
146 #define RSAENH_HMAC_DEF_PAD_LEN 64
147 #define RSAENH_HMAC_BLOCK_LEN 64
148 #define RSAENH_DES_EFFECTIVE_KEYLEN 56
149 #define RSAENH_DES_STORAGE_KEYLEN 64
150 #define RSAENH_3DES112_EFFECTIVE_KEYLEN 112
151 #define RSAENH_3DES112_STORAGE_KEYLEN 128
152 #define RSAENH_3DES_EFFECTIVE_KEYLEN 168
153 #define RSAENH_3DES_STORAGE_KEYLEN 192
154 #define RSAENH_MAGIC_RSA2 0x32415352
155 #define RSAENH_MAGIC_RSA1 0x31415352
156 #define RSAENH_PKC_BLOCKTYPE 0x02
157 #define RSAENH_SSL3_VERSION_MAJOR 3
158 #define RSAENH_SSL3_VERSION_MINOR 0
159 #define RSAENH_TLS1_VERSION_MAJOR 3
160 #define RSAENH_TLS1_VERSION_MINOR 1
161 #define RSAENH_REGKEY "Software\\Wine\\Crypto\\RSA\\%s"
162
163 #define RSAENH_MIN(a,b) ((a)<(b)?(a):(b))
164 /******************************************************************************
165 * aProvEnumAlgsEx - Defines the capabilities of the CSP personalities.
166 */
167 #define RSAENH_MAX_ENUMALGS 24
168 #define RSAENH_PCT1_SSL2_SSL3_TLS1 (CRYPT_FLAG_PCT1|CRYPT_FLAG_SSL2|CRYPT_FLAG_SSL3|CRYPT_FLAG_TLS1)
169 static const PROV_ENUMALGS_EX aProvEnumAlgsEx[5][RSAENH_MAX_ENUMALGS+1] =
170 {
171 {
172 {CALG_RC2, 40, 40, 56,0, 4,"RC2", 24,"RSA Data Security's RC2"},
173 {CALG_RC4, 40, 40, 56,0, 4,"RC4", 24,"RSA Data Security's RC4"},
174 {CALG_DES, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
175 {CALG_SHA, 160,160, 160,CRYPT_FLAG_SIGNING, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
176 {CALG_MD2, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD2", 23,"Message Digest 2 (MD2)"},
177 {CALG_MD4, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD4", 23,"Message Digest 4 (MD4)"},
178 {CALG_MD5, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD5", 23,"Message Digest 5 (MD5)"},
179 {CALG_SSL3_SHAMD5,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
180 {CALG_MAC, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
181 {CALG_RSA_SIGN, 512,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_SIGN",14,"RSA Signature"},
182 {CALG_RSA_KEYX, 512,384, 1024,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_KEYX",17,"RSA Key Exchange"},
183 {CALG_HMAC, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
184 {0, 0, 0, 0,0, 1,"", 1,""}
185 },
186 {
187 {CALG_RC2, 128, 40, 128,0, 4,"RC2", 24,"RSA Data Security's RC2"},
188 {CALG_RC4, 128, 40, 128,0, 4,"RC4", 24,"RSA Data Security's RC4"},
189 {CALG_DES, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
190 {CALG_3DES_112, 112,112, 112,0, 13,"3DES TWO KEY",19,"Two Key Triple DES"},
191 {CALG_3DES, 168,168, 168,0, 5,"3DES", 21,"Three Key Triple DES"},
192 {CALG_SHA, 160,160, 160,CRYPT_FLAG_SIGNING, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
193 {CALG_MD2, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD2", 23,"Message Digest 2 (MD2)"},
194 {CALG_MD4, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD4", 23,"Message Digest 4 (MD4)"},
195 {CALG_MD5, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD5", 23,"Message Digest 5 (MD5)"},
196 {CALG_SSL3_SHAMD5,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
197 {CALG_MAC, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
198 {CALG_RSA_SIGN,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_SIGN",14,"RSA Signature"},
199 {CALG_RSA_KEYX,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_KEYX",17,"RSA Key Exchange"},
200 {CALG_HMAC, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
201 {0, 0, 0, 0,0, 1,"", 1,""}
202 },
203 {
204 {CALG_RC2, 128, 40, 128,0, 4,"RC2", 24,"RSA Data Security's RC2"},
205 {CALG_RC4, 128, 40, 128,0, 4,"RC4", 24,"RSA Data Security's RC4"},
206 {CALG_DES, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
207 {CALG_3DES_112, 112,112, 112,0, 13,"3DES TWO KEY",19,"Two Key Triple DES"},
208 {CALG_3DES, 168,168, 168,0, 5,"3DES", 21,"Three Key Triple DES"},
209 {CALG_SHA, 160,160, 160,CRYPT_FLAG_SIGNING, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
210 {CALG_MD2, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD2", 23,"Message Digest 2 (MD2)"},
211 {CALG_MD4, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD4", 23,"Message Digest 4 (MD4)"},
212 {CALG_MD5, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD5", 23,"Message Digest 5 (MD5)"},
213 {CALG_SSL3_SHAMD5,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
214 {CALG_MAC, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
215 {CALG_RSA_SIGN,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_SIGN",14,"RSA Signature"},
216 {CALG_RSA_KEYX,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_KEYX",17,"RSA Key Exchange"},
217 {CALG_HMAC, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
218 {0, 0, 0, 0,0, 1,"", 1,""}
219 },
220 {
221 {CALG_RC2, 128, 40, 128,RSAENH_PCT1_SSL2_SSL3_TLS1, 4,"RC2", 24,"RSA Data Security's RC2"},
222 {CALG_RC4, 128, 40, 128,RSAENH_PCT1_SSL2_SSL3_TLS1, 4,"RC4", 24,"RSA Data Security's RC4"},
223 {CALG_DES, 56, 56, 56,RSAENH_PCT1_SSL2_SSL3_TLS1, 4,"DES", 31,"Data Encryption Standard (DES)"},
224 {CALG_3DES_112, 112,112, 112,RSAENH_PCT1_SSL2_SSL3_TLS1,13,"3DES TWO KEY",19,"Two Key Triple DES"},
225 {CALG_3DES, 168,168, 168,RSAENH_PCT1_SSL2_SSL3_TLS1, 5,"3DES", 21,"Three Key Triple DES"},
226 {CALG_SHA,160,160,160,CRYPT_FLAG_SIGNING|RSAENH_PCT1_SSL2_SSL3_TLS1,6,"SHA-1",30,"Secure Hash Algorithm (SHA-1)"},
227 {CALG_MD5,128,128,128,CRYPT_FLAG_SIGNING|RSAENH_PCT1_SSL2_SSL3_TLS1,4,"MD5",23,"Message Digest 5 (MD5)"},
228 {CALG_SSL3_SHAMD5,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
229 {CALG_MAC, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
230 {CALG_RSA_SIGN,1024,384,16384,CRYPT_FLAG_SIGNING|RSAENH_PCT1_SSL2_SSL3_TLS1,9,"RSA_SIGN",14,"RSA Signature"},
231 {CALG_RSA_KEYX,1024,384,16384,CRYPT_FLAG_SIGNING|RSAENH_PCT1_SSL2_SSL3_TLS1,9,"RSA_KEYX",17,"RSA Key Exchange"},
232 {CALG_HMAC, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
233 {CALG_PCT1_MASTER,128,128,128,CRYPT_FLAG_PCT1, 12,"PCT1 MASTER",12,"PCT1 Master"},
234 {CALG_SSL2_MASTER,40,40, 192,CRYPT_FLAG_SSL2, 12,"SSL2 MASTER",12,"SSL2 Master"},
235 {CALG_SSL3_MASTER,384,384,384,CRYPT_FLAG_SSL3, 12,"SSL3 MASTER",12,"SSL3 Master"},
236 {CALG_TLS1_MASTER,384,384,384,CRYPT_FLAG_TLS1, 12,"TLS1 MASTER",12,"TLS1 Master"},
237 {CALG_SCHANNEL_MASTER_HASH,0,0,-1,0, 16,"SCH MASTER HASH",21,"SChannel Master Hash"},
238 {CALG_SCHANNEL_MAC_KEY,0,0,-1,0, 12,"SCH MAC KEY",17,"SChannel MAC Key"},
239 {CALG_SCHANNEL_ENC_KEY,0,0,-1,0, 12,"SCH ENC KEY",24,"SChannel Encryption Key"},
240 {CALG_TLS1PRF, 0, 0, -1,0, 9,"TLS1 PRF", 28,"TLS1 Pseudo Random Function"},
241 {0, 0, 0, 0,0, 1,"", 1,""}
242 },
243 {
244 {CALG_RC2, 128, 40, 128,0, 4,"RC2", 24,"RSA Data Security's RC2"},
245 {CALG_RC4, 128, 40, 128,0, 4,"RC4", 24,"RSA Data Security's RC4"},
246 {CALG_DES, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
247 {CALG_3DES_112, 112,112, 112,0, 13,"3DES TWO KEY",19,"Two Key Triple DES"},
248 {CALG_3DES, 168,168, 168,0, 5,"3DES", 21,"Three Key Triple DES"},
249 {CALG_AES, 128,128, 128,0, 4,"AES", 35,"Advanced Encryption Standard (AES)"},
250 {CALG_AES_128, 128,128, 128,0, 8,"AES-128", 39,"Advanced Encryption Standard (AES-128)"},
251 {CALG_AES_192, 192,192, 192,0, 8,"AES-192", 39,"Advanced Encryption Standard (AES-192)"},
252 {CALG_AES_256, 256,256, 256,0, 8,"AES-256", 39,"Advanced Encryption Standard (AES-256)"},
253 {CALG_SHA, 160,160, 160,CRYPT_FLAG_SIGNING, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
254 {CALG_SHA_256, 256,256, 256,CRYPT_FLAG_SIGNING, 6,"SHA-256", 30,"Secure Hash Algorithm (SHA-256)"},
255 {CALG_SHA_384, 384,384, 384,CRYPT_FLAG_SIGNING, 6,"SHA-384", 30,"Secure Hash Algorithm (SHA-384)"},
256 {CALG_SHA_512, 512,512, 512,CRYPT_FLAG_SIGNING, 6,"SHA-512", 30,"Secure Hash Algorithm (SHA-512)"},
257 {CALG_MD2, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD2", 23,"Message Digest 2 (MD2)"},
258 {CALG_MD4, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD4", 23,"Message Digest 4 (MD4)"},
259 {CALG_MD5, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD5", 23,"Message Digest 5 (MD5)"},
260 {CALG_SSL3_SHAMD5,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
261 {CALG_MAC, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
262 {CALG_RSA_SIGN,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_SIGN",14,"RSA Signature"},
263 {CALG_RSA_KEYX,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_KEYX",17,"RSA Key Exchange"},
264 {CALG_HMAC, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
265 {0, 0, 0, 0,0, 1,"", 1,""}
266 }
267 };
268
269 /******************************************************************************
270 * API forward declarations
271 */
272 BOOL WINAPI
273 RSAENH_CPGetKeyParam(
274 HCRYPTPROV hProv,
275 HCRYPTKEY hKey,
276 DWORD dwParam,
277 BYTE *pbData,
278 DWORD *pdwDataLen,
279 DWORD dwFlags
280 );
281
282 BOOL WINAPI
283 RSAENH_CPEncrypt(
284 HCRYPTPROV hProv,
285 HCRYPTKEY hKey,
286 HCRYPTHASH hHash,
287 BOOL Final,
288 DWORD dwFlags,
289 BYTE *pbData,
290 DWORD *pdwDataLen,
291 DWORD dwBufLen
292 );
293
294 BOOL WINAPI
295 RSAENH_CPCreateHash(
296 HCRYPTPROV hProv,
297 ALG_ID Algid,
298 HCRYPTKEY hKey,
299 DWORD dwFlags,
300 HCRYPTHASH *phHash
301 );
302
303 BOOL WINAPI
304 RSAENH_CPSetHashParam(
305 HCRYPTPROV hProv,
306 HCRYPTHASH hHash,
307 DWORD dwParam,
308 BYTE *pbData, DWORD dwFlags
309 );
310
311 BOOL WINAPI
312 RSAENH_CPGetHashParam(
313 HCRYPTPROV hProv,
314 HCRYPTHASH hHash,
315 DWORD dwParam,
316 BYTE *pbData,
317 DWORD *pdwDataLen,
318 DWORD dwFlags
319 );
320
321 BOOL WINAPI
322 RSAENH_CPDestroyHash(
323 HCRYPTPROV hProv,
324 HCRYPTHASH hHash
325 );
326
327 static BOOL crypt_export_key(
328 CRYPTKEY *pCryptKey,
329 HCRYPTKEY hPubKey,
330 DWORD dwBlobType,
331 DWORD dwFlags,
332 BOOL force,
333 BYTE *pbData,
334 DWORD *pdwDataLen
335 );
336
337 static BOOL import_key(
338 HCRYPTPROV hProv,
339 const BYTE *pbData,
340 DWORD dwDataLen,
341 HCRYPTKEY hPubKey,
342 DWORD dwFlags,
343 BOOL fStoreKey,
344 HCRYPTKEY *phKey
345 );
346
347 BOOL WINAPI
348 RSAENH_CPHashData(
349 HCRYPTPROV hProv,
350 HCRYPTHASH hHash,
351 const BYTE *pbData,
352 DWORD dwDataLen,
353 DWORD dwFlags
354 );
355
356 /******************************************************************************
357 * CSP's handle table (used by all acquired key containers)
358 */
359 static struct handle_table handle_table;
360
361 /******************************************************************************
362 * DllMain (RSAENH.@)
363 *
364 * Initializes and destroys the handle table for the CSP's handles.
365 */
366 BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD fdwReason, PVOID reserved)
367 {
368 switch (fdwReason)
369 {
370 case DLL_PROCESS_ATTACH:
371 instance = hInstance;
372 DisableThreadLibraryCalls(hInstance);
373 init_handle_table(&handle_table);
374 break;
375
376 case DLL_PROCESS_DETACH:
377 if (reserved) break;
378 destroy_handle_table(&handle_table);
379 break;
380 }
381 return TRUE;
382 }
383
384 /******************************************************************************
385 * copy_param [Internal]
386 *
387 * Helper function that supports the standard WINAPI protocol for querying data
388 * of dynamic size.
389 *
390 * PARAMS
391 * pbBuffer [O] Buffer where the queried parameter is copied to, if it is large enough.
392 * May be NUL if the required buffer size is to be queried only.
393 * pdwBufferSize [I/O] In: Size of the buffer at pbBuffer
394 * Out: Size of parameter pbParam
395 * pbParam [I] Parameter value.
396 * dwParamSize [I] Size of pbParam
397 *
398 * RETURN
399 * Success: TRUE (pbParam was copied into pbBuffer or pbBuffer is NULL)
400 * Failure: FALSE (pbBuffer is not large enough to hold pbParam). Last error: ERROR_MORE_DATA
401 */
402 static inline BOOL copy_param(BYTE *pbBuffer, DWORD *pdwBufferSize, const BYTE *pbParam,
403 DWORD dwParamSize)
404 {
405 if (pbBuffer)
406 {
407 if (dwParamSize > *pdwBufferSize)
408 {
409 SetLastError(ERROR_MORE_DATA);
410 *pdwBufferSize = dwParamSize;
411 return FALSE;
412 }
413 memcpy(pbBuffer, pbParam, dwParamSize);
414 }
415 *pdwBufferSize = dwParamSize;
416 return TRUE;
417 }
418
419 static inline KEYCONTAINER* get_key_container(HCRYPTPROV hProv)
420 {
421 KEYCONTAINER *pKeyContainer;
422
423 if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER,
424 (OBJECTHDR**)&pKeyContainer))
425 {
426 SetLastError(NTE_BAD_UID);
427 return NULL;
428 }
429 return pKeyContainer;
430 }
431
432 /******************************************************************************
433 * get_algid_info [Internal]
434 *
435 * Query CSP capabilities for a given crypto algorithm.
436 *
437 * PARAMS
438 * hProv [I] Handle to a key container of the CSP whose capabilities are to be queried.
439 * algid [I] Identifier of the crypto algorithm about which information is requested.
440 *
441 * RETURNS
442 * Success: Pointer to a PROV_ENUMALGS_EX struct containing information about the crypto algorithm.
443 * Failure: NULL (algid not supported)
444 */
445 static inline const PROV_ENUMALGS_EX* get_algid_info(HCRYPTPROV hProv, ALG_ID algid) {
446 const PROV_ENUMALGS_EX *iterator;
447 KEYCONTAINER *pKeyContainer;
448
449 if (!(pKeyContainer = get_key_container(hProv))) return NULL;
450
451 for (iterator = aProvEnumAlgsEx[pKeyContainer->dwPersonality]; iterator->aiAlgid; iterator++) {
452 if (iterator->aiAlgid == algid) return iterator;
453 }
454
455 SetLastError(NTE_BAD_ALGID);
456 return NULL;
457 }
458
459 /******************************************************************************
460 * copy_data_blob [Internal]
461 *
462 * deeply copies a DATA_BLOB
463 *
464 * PARAMS
465 * dst [O] That's where the blob will be copied to
466 * src [I] Source blob
467 *
468 * RETURNS
469 * Success: TRUE
470 * Failure: FALSE (GetLastError() == NTE_NO_MEMORY
471 *
472 * NOTES
473 * Use free_data_blob to release resources occupied by copy_data_blob.
474 */
475 static inline BOOL copy_data_blob(PCRYPT_DATA_BLOB dst, const PCRYPT_DATA_BLOB src)
476 {
477 dst->pbData = HeapAlloc(GetProcessHeap(), 0, src->cbData);
478 if (!dst->pbData) {
479 SetLastError(NTE_NO_MEMORY);
480 return FALSE;
481 }
482 dst->cbData = src->cbData;
483 memcpy(dst->pbData, src->pbData, src->cbData);
484 return TRUE;
485 }
486
487 /******************************************************************************
488 * concat_data_blobs [Internal]
489 *
490 * Concatenates two blobs
491 *
492 * PARAMS
493 * dst [O] The new blob will be copied here
494 * src1 [I] Prefix blob
495 * src2 [I] Appendix blob
496 *
497 * RETURNS
498 * Success: TRUE
499 * Failure: FALSE (GetLastError() == NTE_NO_MEMORY)
500 *
501 * NOTES
502 * Release resources occupied by concat_data_blobs with free_data_blobs
503 */
504 static inline BOOL concat_data_blobs(PCRYPT_DATA_BLOB dst, const PCRYPT_DATA_BLOB src1,
505 const PCRYPT_DATA_BLOB src2)
506 {
507 dst->cbData = src1->cbData + src2->cbData;
508 dst->pbData = HeapAlloc(GetProcessHeap(), 0, dst->cbData);
509 if (!dst->pbData) {
510 SetLastError(NTE_NO_MEMORY);
511 return FALSE;
512 }
513 memcpy(dst->pbData, src1->pbData, src1->cbData);
514 memcpy(dst->pbData + src1->cbData, src2->pbData, src2->cbData);
515 return TRUE;
516 }
517
518 /******************************************************************************
519 * free_data_blob [Internal]
520 *
521 * releases resource occupied by a dynamically allocated CRYPT_DATA_BLOB
522 *
523 * PARAMS
524 * pBlob [I] Heap space occupied by pBlob->pbData is released
525 */
526 static inline void free_data_blob(PCRYPT_DATA_BLOB pBlob) {
527 HeapFree(GetProcessHeap(), 0, pBlob->pbData);
528 }
529
530 /******************************************************************************
531 * init_data_blob [Internal]
532 */
533 static inline void init_data_blob(PCRYPT_DATA_BLOB pBlob) {
534 pBlob->pbData = NULL;
535 pBlob->cbData = 0;
536 }
537
538 /******************************************************************************
539 * free_hmac_info [Internal]
540 *
541 * Deeply free an HMAC_INFO struct.
542 *
543 * PARAMS
544 * hmac_info [I] Pointer to the HMAC_INFO struct to be freed.
545 *
546 * NOTES
547 * See Internet RFC 2104 for details on the HMAC algorithm.
548 */
549 static inline void free_hmac_info(PHMAC_INFO hmac_info) {
550 if (!hmac_info) return;
551 HeapFree(GetProcessHeap(), 0, hmac_info->pbInnerString);
552 HeapFree(GetProcessHeap(), 0, hmac_info->pbOuterString);
553 HeapFree(GetProcessHeap(), 0, hmac_info);
554 }
555
556 /******************************************************************************
557 * copy_hmac_info [Internal]
558 *
559 * Deeply copy an HMAC_INFO struct
560 *
561 * PARAMS
562 * dst [O] Pointer to a location where the pointer to the HMAC_INFO copy will be stored.
563 * src [I] Pointer to the HMAC_INFO struct to be copied.
564 *
565 * RETURNS
566 * Success: TRUE
567 * Failure: FALSE
568 *
569 * NOTES
570 * See Internet RFC 2104 for details on the HMAC algorithm.
571 */
572 static BOOL copy_hmac_info(PHMAC_INFO *dst, const HMAC_INFO *src) {
573 if (!src) return FALSE;
574 *dst = HeapAlloc(GetProcessHeap(), 0, sizeof(HMAC_INFO));
575 if (!*dst) return FALSE;
576 **dst = *src;
577 (*dst)->pbInnerString = NULL;
578 (*dst)->pbOuterString = NULL;
579 if ((*dst)->cbInnerString == 0) (*dst)->cbInnerString = RSAENH_HMAC_DEF_PAD_LEN;
580 (*dst)->pbInnerString = HeapAlloc(GetProcessHeap(), 0, (*dst)->cbInnerString);
581 if (!(*dst)->pbInnerString) {
582 free_hmac_info(*dst);
583 return FALSE;
584 }
585 if (src->cbInnerString)
586 memcpy((*dst)->pbInnerString, src->pbInnerString, src->cbInnerString);
587 else
588 memset((*dst)->pbInnerString, RSAENH_HMAC_DEF_IPAD_CHAR, RSAENH_HMAC_DEF_PAD_LEN);
589 if ((*dst)->cbOuterString == 0) (*dst)->cbOuterString = RSAENH_HMAC_DEF_PAD_LEN;
590 (*dst)->pbOuterString = HeapAlloc(GetProcessHeap(), 0, (*dst)->cbOuterString);
591 if (!(*dst)->pbOuterString) {
592 free_hmac_info(*dst);
593 return FALSE;
594 }
595 if (src->cbOuterString)
596 memcpy((*dst)->pbOuterString, src->pbOuterString, src->cbOuterString);
597 else
598 memset((*dst)->pbOuterString, RSAENH_HMAC_DEF_OPAD_CHAR, RSAENH_HMAC_DEF_PAD_LEN);
599 return TRUE;
600 }
601
602 /******************************************************************************
603 * destroy_hash [Internal]
604 *
605 * Destructor for hash objects
606 *
607 * PARAMS
608 * pCryptHash [I] Pointer to the hash object to be destroyed.
609 * Will be invalid after function returns!
610 */
611 static void destroy_hash(OBJECTHDR *pObject)
612 {
613 CRYPTHASH *pCryptHash = (CRYPTHASH*)pObject;
614
615 free_hmac_info(pCryptHash->pHMACInfo);
616 free_data_blob(&pCryptHash->tpPRFParams.blobLabel);
617 free_data_blob(&pCryptHash->tpPRFParams.blobSeed);
618 HeapFree(GetProcessHeap(), 0, pCryptHash);
619 }
620
621 /******************************************************************************
622 * init_hash [Internal]
623 *
624 * Initialize (or reset) a hash object
625 *
626 * PARAMS
627 * pCryptHash [I] The hash object to be initialized.
628 */
629 static inline BOOL init_hash(CRYPTHASH *pCryptHash) {
630 DWORD dwLen;
631
632 switch (pCryptHash->aiAlgid)
633 {
634 case CALG_HMAC:
635 if (pCryptHash->pHMACInfo) {
636 const PROV_ENUMALGS_EX *pAlgInfo;
637
638 pAlgInfo = get_algid_info(pCryptHash->hProv, pCryptHash->pHMACInfo->HashAlgid);
639 if (!pAlgInfo) return FALSE;
640 pCryptHash->dwHashSize = pAlgInfo->dwDefaultLen >> 3;
641 init_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context);
642 update_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
643 pCryptHash->pHMACInfo->pbInnerString,
644 pCryptHash->pHMACInfo->cbInnerString);
645 }
646 return TRUE;
647
648 case CALG_MAC:
649 dwLen = sizeof(DWORD);
650 RSAENH_CPGetKeyParam(pCryptHash->hProv, pCryptHash->hKey, KP_BLOCKLEN,
651 (BYTE*)&pCryptHash->dwHashSize, &dwLen, 0);
652 pCryptHash->dwHashSize >>= 3;
653 return TRUE;
654
655 default:
656 return init_hash_impl(pCryptHash->aiAlgid, &pCryptHash->context);
657 }
658 }
659
660 /******************************************************************************
661 * update_hash [Internal]
662 *
663 * Hashes the given data and updates the hash object's state accordingly
664 *
665 * PARAMS
666 * pCryptHash [I] Hash object to be updated.
667 * pbData [I] Pointer to data stream to be hashed.
668 * dwDataLen [I] Length of data stream.
669 */
670 static inline void update_hash(CRYPTHASH *pCryptHash, const BYTE *pbData, DWORD dwDataLen)
671 {
672 BYTE *pbTemp;
673
674 switch (pCryptHash->aiAlgid)
675 {
676 case CALG_HMAC:
677 if (pCryptHash->pHMACInfo)
678 update_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
679 pbData, dwDataLen);
680 break;
681
682 case CALG_MAC:
683 pbTemp = HeapAlloc(GetProcessHeap(), 0, dwDataLen);
684 if (!pbTemp) return;
685 memcpy(pbTemp, pbData, dwDataLen);
686 RSAENH_CPEncrypt(pCryptHash->hProv, pCryptHash->hKey, 0, FALSE, 0,
687 pbTemp, &dwDataLen, dwDataLen);
688 HeapFree(GetProcessHeap(), 0, pbTemp);
689 break;
690
691 default:
692 update_hash_impl(pCryptHash->aiAlgid, &pCryptHash->context, pbData, dwDataLen);
693 }
694 }
695
696 /******************************************************************************
697 * finalize_hash [Internal]
698 *
699 * Finalizes the hash, after all data has been hashed with update_hash.
700 * No additional data can be hashed afterwards until the hash gets initialized again.
701 *
702 * PARAMS
703 * pCryptHash [I] Hash object to be finalized.
704 */
705 static inline void finalize_hash(CRYPTHASH *pCryptHash) {
706 DWORD dwDataLen;
707
708 switch (pCryptHash->aiAlgid)
709 {
710 case CALG_HMAC:
711 if (pCryptHash->pHMACInfo) {
712 BYTE abHashValue[RSAENH_MAX_HASH_SIZE];
713
714 finalize_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
715 pCryptHash->abHashValue);
716 memcpy(abHashValue, pCryptHash->abHashValue, pCryptHash->dwHashSize);
717 init_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context);
718 update_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
719 pCryptHash->pHMACInfo->pbOuterString,
720 pCryptHash->pHMACInfo->cbOuterString);
721 update_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
722 abHashValue, pCryptHash->dwHashSize);
723 finalize_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
724 pCryptHash->abHashValue);
725 }
726 break;
727
728 case CALG_MAC:
729 dwDataLen = 0;
730 RSAENH_CPEncrypt(pCryptHash->hProv, pCryptHash->hKey, 0, TRUE, 0,
731 pCryptHash->abHashValue, &dwDataLen, pCryptHash->dwHashSize);
732 break;
733
734 default:
735 finalize_hash_impl(pCryptHash->aiAlgid, &pCryptHash->context, pCryptHash->abHashValue);
736 }
737 }
738
739 /******************************************************************************
740 * destroy_key [Internal]
741 *
742 * Destructor for key objects
743 *
744 * PARAMS
745 * pCryptKey [I] Pointer to the key object to be destroyed.
746 * Will be invalid after function returns!
747 */
748 static void destroy_key(OBJECTHDR *pObject)
749 {
750 CRYPTKEY *pCryptKey = (CRYPTKEY*)pObject;
751
752 free_key_impl(pCryptKey->aiAlgid, &pCryptKey->context);
753 free_data_blob(&pCryptKey->siSChannelInfo.blobClientRandom);
754 free_data_blob(&pCryptKey->siSChannelInfo.blobServerRandom);
755 free_data_blob(&pCryptKey->blobHmacKey);
756 HeapFree(GetProcessHeap(), 0, pCryptKey);
757 }
758
759 /******************************************************************************
760 * setup_key [Internal]
761 *
762 * Initialize (or reset) a key object
763 *
764 * PARAMS
765 * pCryptKey [I] The key object to be initialized.
766 */
767 static inline void setup_key(CRYPTKEY *pCryptKey) {
768 pCryptKey->dwState = RSAENH_KEYSTATE_IDLE;
769 memcpy(pCryptKey->abChainVector, pCryptKey->abInitVector, sizeof(pCryptKey->abChainVector));
770 setup_key_impl(pCryptKey->aiAlgid, &pCryptKey->context, pCryptKey->dwKeyLen,
771 pCryptKey->dwEffectiveKeyLen, pCryptKey->dwSaltLen,
772 pCryptKey->abKeyValue);
773 }
774
775 /******************************************************************************
776 * new_key [Internal]
777 *
778 * Creates a new key object without assigning the actual binary key value.
779 * This is done by CPDeriveKey, CPGenKey or CPImportKey, which call this function.
780 *
781 * PARAMS
782 * hProv [I] Handle to the provider to which the created key will belong.
783 * aiAlgid [I] The new key shall use the crypto algorithm identified by aiAlgid.
784 * dwFlags [I] Upper 16 bits give the key length.
785 * Lower 16 bits: CRYPT_EXPORTABLE, CRYPT_CREATE_SALT,
786 * CRYPT_NO_SALT
787 * ppCryptKey [O] Pointer to the created key
788 *
789 * RETURNS
790 * Success: Handle to the created key.
791 * Failure: INVALID_HANDLE_VALUE
792 */
793 static HCRYPTKEY new_key(HCRYPTPROV hProv, ALG_ID aiAlgid, DWORD dwFlags, CRYPTKEY **ppCryptKey)
794 {
795 HCRYPTKEY hCryptKey;
796 CRYPTKEY *pCryptKey;
797 DWORD dwKeyLen = HIWORD(dwFlags), bKeyLen = dwKeyLen;
798 const PROV_ENUMALGS_EX *peaAlgidInfo;
799
800 *ppCryptKey = NULL;
801
802 /*
803 * Retrieve the CSP's capabilities for the given ALG_ID value
804 */
805 peaAlgidInfo = get_algid_info(hProv, aiAlgid);
806 if (!peaAlgidInfo) return (HCRYPTKEY)INVALID_HANDLE_VALUE;
807
808 TRACE("alg = %s, dwKeyLen = %d\n", debugstr_a(peaAlgidInfo->szName),
809 dwKeyLen);
810 /*
811 * Assume the default key length, if none is specified explicitly
812 */
813 if (dwKeyLen == 0) dwKeyLen = peaAlgidInfo->dwDefaultLen;
814
815 /*
816 * Check if the requested key length is supported by the current CSP.
817 * Adjust key length's for DES algorithms.
818 */
819 switch (aiAlgid) {
820 case CALG_DES:
821 if (dwKeyLen == RSAENH_DES_EFFECTIVE_KEYLEN) {
822 dwKeyLen = RSAENH_DES_STORAGE_KEYLEN;
823 }
824 if (dwKeyLen != RSAENH_DES_STORAGE_KEYLEN) {
825 SetLastError(NTE_BAD_FLAGS);
826 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
827 }
828 break;
829
830 case CALG_3DES_112:
831 if (dwKeyLen == RSAENH_3DES112_EFFECTIVE_KEYLEN) {
832 dwKeyLen = RSAENH_3DES112_STORAGE_KEYLEN;
833 }
834 if (dwKeyLen != RSAENH_3DES112_STORAGE_KEYLEN) {
835 SetLastError(NTE_BAD_FLAGS);
836 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
837 }
838 break;
839
840 case CALG_3DES:
841 if (dwKeyLen == RSAENH_3DES_EFFECTIVE_KEYLEN) {
842 dwKeyLen = RSAENH_3DES_STORAGE_KEYLEN;
843 }
844 if (dwKeyLen != RSAENH_3DES_STORAGE_KEYLEN) {
845 SetLastError(NTE_BAD_FLAGS);
846 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
847 }
848 break;
849
850 case CALG_HMAC:
851 /* Avoid the key length check for HMAC keys, which have unlimited
852 * length.
853 */
854 break;
855
856 case CALG_AES:
857 if (!bKeyLen)
858 {
859 TRACE("missing key len for CALG_AES\n");
860 SetLastError(NTE_BAD_ALGID);
861 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
862 }
863 /* fall through */
864 default:
865 if (dwKeyLen % 8 ||
866 dwKeyLen > peaAlgidInfo->dwMaxLen ||
867 dwKeyLen < peaAlgidInfo->dwMinLen)
868 {
869 TRACE("key len %d out of bounds (%d, %d)\n", dwKeyLen,
870 peaAlgidInfo->dwMinLen, peaAlgidInfo->dwMaxLen);
871 SetLastError(NTE_BAD_DATA);
872 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
873 }
874 }
875
876 hCryptKey = new_object(&handle_table, sizeof(CRYPTKEY), RSAENH_MAGIC_KEY,
877 destroy_key, (OBJECTHDR**)&pCryptKey);
878 if (hCryptKey != (HCRYPTKEY)INVALID_HANDLE_VALUE)
879 {
880 KEYCONTAINER *pKeyContainer = get_key_container(hProv);
881 pCryptKey->aiAlgid = aiAlgid;
882 pCryptKey->hProv = hProv;
883 pCryptKey->dwModeBits = 0;
884 pCryptKey->dwPermissions = CRYPT_ENCRYPT | CRYPT_DECRYPT | CRYPT_READ | CRYPT_WRITE |
885 CRYPT_MAC;
886 if (dwFlags & CRYPT_EXPORTABLE)
887 pCryptKey->dwPermissions |= CRYPT_EXPORT;
888 pCryptKey->dwKeyLen = dwKeyLen >> 3;
889 pCryptKey->dwEffectiveKeyLen = 0;
890
891 /*
892 * For compatibility reasons a 40 bit key on the Enhanced
893 * provider will not have salt
894 */
895 if (pKeyContainer->dwPersonality == RSAENH_PERSONALITY_ENHANCED
896 && (aiAlgid == CALG_RC2 || aiAlgid == CALG_RC4)
897 && (dwFlags & CRYPT_CREATE_SALT) && dwKeyLen == 40)
898 pCryptKey->dwSaltLen = 0;
899 else if ((dwFlags & CRYPT_CREATE_SALT) || (dwKeyLen == 40 && !(dwFlags & CRYPT_NO_SALT)))
900 pCryptKey->dwSaltLen = 16 /*FIXME*/ - pCryptKey->dwKeyLen;
901 else
902 pCryptKey->dwSaltLen = 0;
903 memset(pCryptKey->abKeyValue, 0, sizeof(pCryptKey->abKeyValue));
904 memset(pCryptKey->abInitVector, 0, sizeof(pCryptKey->abInitVector));
905 memset(&pCryptKey->siSChannelInfo.saEncAlg, 0, sizeof(pCryptKey->siSChannelInfo.saEncAlg));
906 memset(&pCryptKey->siSChannelInfo.saMACAlg, 0, sizeof(pCryptKey->siSChannelInfo.saMACAlg));
907 init_data_blob(&pCryptKey->siSChannelInfo.blobClientRandom);
908 init_data_blob(&pCryptKey->siSChannelInfo.blobServerRandom);
909 init_data_blob(&pCryptKey->blobHmacKey);
910
911 switch(aiAlgid)
912 {
913 case CALG_PCT1_MASTER:
914 case CALG_SSL2_MASTER:
915 case CALG_SSL3_MASTER:
916 case CALG_TLS1_MASTER:
917 case CALG_RC4:
918 pCryptKey->dwBlockLen = 0;
919 pCryptKey->dwMode = 0;
920 break;
921
922 case CALG_RC2:
923 case CALG_DES:
924 case CALG_3DES_112:
925 case CALG_3DES:
926 pCryptKey->dwBlockLen = 8;
927 pCryptKey->dwMode = CRYPT_MODE_CBC;
928 break;
929
930 case CALG_AES:
931 case CALG_AES_128:
932 case CALG_AES_192:
933 case CALG_AES_256:
934 pCryptKey->dwBlockLen = 16;
935 pCryptKey->dwMode = CRYPT_MODE_CBC;
936 break;
937
938 case CALG_RSA_KEYX:
939 case CALG_RSA_SIGN:
940 pCryptKey->dwBlockLen = dwKeyLen >> 3;
941 pCryptKey->dwMode = 0;
942 break;
943
944 case CALG_HMAC:
945 pCryptKey->dwBlockLen = 0;
946 pCryptKey->dwMode = 0;
947 break;
948 }
949
950 *ppCryptKey = pCryptKey;
951 }
952
953 return hCryptKey;
954 }
955
956 /******************************************************************************
957 * map_key_spec_to_key_pair_name [Internal]
958 *
959 * Returns the name of the registry value associated with a key spec.
960 *
961 * PARAMS
962 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
963 *
964 * RETURNS
965 * Success: Name of registry value.
966 * Failure: NULL
967 */
968 static LPCSTR map_key_spec_to_key_pair_name(DWORD dwKeySpec)
969 {
970 LPCSTR szValueName;
971
972 switch (dwKeySpec)
973 {
974 case AT_KEYEXCHANGE:
975 szValueName = "KeyExchangeKeyPair";
976 break;
977 case AT_SIGNATURE:
978 szValueName = "SignatureKeyPair";
979 break;
980 default:
981 WARN("invalid key spec %d\n", dwKeySpec);
982 szValueName = NULL;
983 }
984 return szValueName;
985 }
986
987 /******************************************************************************
988 * store_key_pair [Internal]
989 *
990 * Stores a key pair to the registry
991 *
992 * PARAMS
993 * hCryptKey [I] Handle to the key to be stored
994 * hKey [I] Registry key where the key pair is to be stored
995 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
996 * dwFlags [I] Flags for protecting the key
997 */
998 static void store_key_pair(HCRYPTKEY hCryptKey, HKEY hKey, DWORD dwKeySpec, DWORD dwFlags)
999 {
1000 LPCSTR szValueName;
1001 DATA_BLOB blobIn, blobOut;
1002 CRYPTKEY *pKey;
1003 DWORD dwLen;
1004 BYTE *pbKey;
1005
1006 if (!(szValueName = map_key_spec_to_key_pair_name(dwKeySpec)))
1007 return;
1008 if (lookup_handle(&handle_table, hCryptKey, RSAENH_MAGIC_KEY,
1009 (OBJECTHDR**)&pKey))
1010 {
1011 if (crypt_export_key(pKey, 0, PRIVATEKEYBLOB, 0, TRUE, 0, &dwLen))
1012 {
1013 pbKey = HeapAlloc(GetProcessHeap(), 0, dwLen);
1014 if (pbKey)
1015 {
1016 if (crypt_export_key(pKey, 0, PRIVATEKEYBLOB, 0, TRUE, pbKey,
1017 &dwLen))
1018 {
1019 blobIn.pbData = pbKey;
1020 blobIn.cbData = dwLen;
1021
1022 if (CryptProtectData(&blobIn, NULL, NULL, NULL, NULL,
1023 dwFlags, &blobOut))
1024 {
1025 RegSetValueExA(hKey, szValueName, 0, REG_BINARY,
1026 blobOut.pbData, blobOut.cbData);
1027 LocalFree(blobOut.pbData);
1028 }
1029 }
1030 HeapFree(GetProcessHeap(), 0, pbKey);
1031 }
1032 }
1033 }
1034 }
1035
1036 /******************************************************************************
1037 * map_key_spec_to_permissions_name [Internal]
1038 *
1039 * Returns the name of the registry value associated with the permissions for
1040 * a key spec.
1041 *
1042 * PARAMS
1043 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
1044 *
1045 * RETURNS
1046 * Success: Name of registry value.
1047 * Failure: NULL
1048 */
1049 static LPCSTR map_key_spec_to_permissions_name(DWORD dwKeySpec)
1050 {
1051 LPCSTR szValueName;
1052
1053 switch (dwKeySpec)
1054 {
1055 case AT_KEYEXCHANGE:
1056 szValueName = "KeyExchangePermissions";
1057 break;
1058 case AT_SIGNATURE:
1059 szValueName = "SignaturePermissions";
1060 break;
1061 default:
1062 WARN("invalid key spec %d\n", dwKeySpec);
1063 szValueName = NULL;
1064 }
1065 return szValueName;
1066 }
1067
1068 /******************************************************************************
1069 * store_key_permissions [Internal]
1070 *
1071 * Stores a key's permissions to the registry
1072 *
1073 * PARAMS
1074 * hCryptKey [I] Handle to the key whose permissions are to be stored
1075 * hKey [I] Registry key where the key permissions are to be stored
1076 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
1077 */
1078 static void store_key_permissions(HCRYPTKEY hCryptKey, HKEY hKey, DWORD dwKeySpec)
1079 {
1080 LPCSTR szValueName;
1081 CRYPTKEY *pKey;
1082
1083 if (!(szValueName = map_key_spec_to_permissions_name(dwKeySpec)))
1084 return;
1085 if (lookup_handle(&handle_table, hCryptKey, RSAENH_MAGIC_KEY,
1086 (OBJECTHDR**)&pKey))
1087 RegSetValueExA(hKey, szValueName, 0, REG_DWORD,
1088 (BYTE *)&pKey->dwPermissions,
1089 sizeof(pKey->dwPermissions));
1090 }
1091
1092 /******************************************************************************
1093 * create_container_key [Internal]
1094 *
1095 * Creates the registry key for a key container's persistent storage.
1096 *
1097 * PARAMS
1098 * pKeyContainer [I] Pointer to the key container
1099 * sam [I] Desired registry access
1100 * phKey [O] Returned key
1101 */
1102 static BOOL create_container_key(KEYCONTAINER *pKeyContainer, REGSAM sam, HKEY *phKey)
1103 {
1104 CHAR szRSABase[MAX_PATH];
1105 HKEY hRootKey;
1106
1107 sprintf(szRSABase, RSAENH_REGKEY, pKeyContainer->szName);
1108
1109 if (pKeyContainer->dwFlags & CRYPT_MACHINE_KEYSET)
1110 hRootKey = HKEY_LOCAL_MACHINE;
1111 else
1112 hRootKey = HKEY_CURRENT_USER;
1113
1114 /* @@ Wine registry key: HKLM\Software\Wine\Crypto\RSA */
1115 /* @@ Wine registry key: HKCU\Software\Wine\Crypto\RSA */
1116 return RegCreateKeyExA(hRootKey, szRSABase, 0, NULL,
1117 REG_OPTION_NON_VOLATILE, sam, NULL, phKey, NULL)
1118 == ERROR_SUCCESS;
1119 }
1120
1121 /******************************************************************************
1122 * open_container_key [Internal]
1123 *
1124 * Opens a key container's persistent storage for reading.
1125 *
1126 * PARAMS
1127 * pszContainerName [I] Name of the container to be opened. May be the empty
1128 * string if the parent key of all containers is to be
1129 * opened.
1130 * dwFlags [I] Flags indicating which keyset to be opened.
1131 * phKey [O] Returned key
1132 */
1133 static BOOL open_container_key(LPCSTR pszContainerName, DWORD dwFlags, REGSAM access, HKEY *phKey)
1134 {
1135 CHAR szRSABase[MAX_PATH];
1136 HKEY hRootKey;
1137
1138 sprintf(szRSABase, RSAENH_REGKEY, pszContainerName);
1139
1140 if (dwFlags & CRYPT_MACHINE_KEYSET)
1141 hRootKey = HKEY_LOCAL_MACHINE;
1142 else
1143 hRootKey = HKEY_CURRENT_USER;
1144
1145 /* @@ Wine registry key: HKLM\Software\Wine\Crypto\RSA */
1146 /* @@ Wine registry key: HKCU\Software\Wine\Crypto\RSA */
1147 return RegOpenKeyExA(hRootKey, szRSABase, 0, access, phKey) ==
1148 ERROR_SUCCESS;
1149 }
1150
1151 /******************************************************************************
1152 * delete_container_key [Internal]
1153 *
1154 * Deletes a key container's persistent storage.
1155 *
1156 * PARAMS
1157 * pszContainerName [I] Name of the container to be opened.
1158 * dwFlags [I] Flags indicating which keyset to be opened.
1159 */
1160 static BOOL delete_container_key(LPCSTR pszContainerName, DWORD dwFlags)
1161 {
1162 CHAR szRegKey[MAX_PATH];
1163
1164 if (snprintf(szRegKey, MAX_PATH, RSAENH_REGKEY, pszContainerName) >= MAX_PATH) {
1165 SetLastError(NTE_BAD_KEYSET_PARAM);
1166 return FALSE;
1167 } else {
1168 HKEY hRootKey;
1169 if (dwFlags & CRYPT_MACHINE_KEYSET)
1170 hRootKey = HKEY_LOCAL_MACHINE;
1171 else
1172 hRootKey = HKEY_CURRENT_USER;
1173 if (!RegDeleteKeyA(hRootKey, szRegKey)) {
1174 SetLastError(ERROR_SUCCESS);
1175 return TRUE;
1176 } else {
1177 SetLastError(NTE_BAD_KEYSET);
1178 return FALSE;
1179 }
1180 }
1181 }
1182
1183 /******************************************************************************
1184 * store_key_container_keys [Internal]
1185 *
1186 * Stores key container's keys in a persistent location.
1187 *
1188 * PARAMS
1189 * pKeyContainer [I] Pointer to the key container whose keys are to be saved
1190 */
1191 static void store_key_container_keys(KEYCONTAINER *pKeyContainer)
1192 {
1193 HKEY hKey;
1194 DWORD dwFlags;
1195
1196 /* On WinXP, persistent keys are stored in a file located at:
1197 * $AppData$\\Microsoft\\Crypto\\RSA\\$SID$\\some_hex_string
1198 */
1199
1200 if (pKeyContainer->dwFlags & CRYPT_MACHINE_KEYSET)
1201 dwFlags = CRYPTPROTECT_LOCAL_MACHINE;
1202 else
1203 dwFlags = 0;
1204
1205 if (create_container_key(pKeyContainer, KEY_WRITE, &hKey))
1206 {
1207 store_key_pair(pKeyContainer->hKeyExchangeKeyPair, hKey,
1208 AT_KEYEXCHANGE, dwFlags);
1209 store_key_pair(pKeyContainer->hSignatureKeyPair, hKey,
1210 AT_SIGNATURE, dwFlags);
1211 RegCloseKey(hKey);
1212 }
1213 }
1214
1215 /******************************************************************************
1216 * store_key_container_permissions [Internal]
1217 *
1218 * Stores key container's key permissions in a persistent location.
1219 *
1220 * PARAMS
1221 * pKeyContainer [I] Pointer to the key container whose key permissions are to
1222 * be saved
1223 */
1224 static void store_key_container_permissions(KEYCONTAINER *pKeyContainer)
1225 {
1226 HKEY hKey;
1227
1228 if (create_container_key(pKeyContainer, KEY_WRITE, &hKey))
1229 {
1230 store_key_permissions(pKeyContainer->hKeyExchangeKeyPair, hKey,
1231 AT_KEYEXCHANGE);
1232 store_key_permissions(pKeyContainer->hSignatureKeyPair, hKey,
1233 AT_SIGNATURE);
1234 RegCloseKey(hKey);
1235 }
1236 }
1237
1238 /******************************************************************************
1239 * release_key_container_keys [Internal]
1240 *
1241 * Releases key container's keys.
1242 *
1243 * PARAMS
1244 * pKeyContainer [I] Pointer to the key container whose keys are to be released.
1245 */
1246 static void release_key_container_keys(KEYCONTAINER *pKeyContainer)
1247 {
1248 release_handle(&handle_table, pKeyContainer->hKeyExchangeKeyPair,
1249 RSAENH_MAGIC_KEY);
1250 release_handle(&handle_table, pKeyContainer->hSignatureKeyPair,
1251 RSAENH_MAGIC_KEY);
1252 }
1253
1254 /******************************************************************************
1255 * destroy_key_container [Internal]
1256 *
1257 * Destructor for key containers.
1258 *
1259 * PARAMS
1260 * pObjectHdr [I] Pointer to the key container to be destroyed.
1261 */
1262 static void destroy_key_container(OBJECTHDR *pObjectHdr)
1263 {
1264 KEYCONTAINER *pKeyContainer = (KEYCONTAINER*)pObjectHdr;
1265
1266 if (!(pKeyContainer->dwFlags & CRYPT_VERIFYCONTEXT))
1267 {
1268 store_key_container_keys(pKeyContainer);
1269 store_key_container_permissions(pKeyContainer);
1270 release_key_container_keys(pKeyContainer);
1271 }
1272 else
1273 release_key_container_keys(pKeyContainer);
1274 HeapFree( GetProcessHeap(), 0, pKeyContainer );
1275 }
1276
1277 /******************************************************************************
1278 * new_key_container [Internal]
1279 *
1280 * Create a new key container. The personality (RSA Base, Strong or Enhanced CP)
1281 * of the CSP is determined via the pVTable->pszProvName string.
1282 *
1283 * PARAMS
1284 * pszContainerName [I] Name of the key container.
1285 * pVTable [I] Callback functions and context info provided by the OS
1286 *
1287 * RETURNS
1288 * Success: Handle to the new key container.
1289 * Failure: INVALID_HANDLE_VALUE
1290 */
1291 static HCRYPTPROV new_key_container(PCCH pszContainerName, DWORD dwFlags, const VTableProvStruc *pVTable)
1292 {
1293 KEYCONTAINER *pKeyContainer;
1294 HCRYPTPROV hKeyContainer;
1295
1296 hKeyContainer = new_object(&handle_table, sizeof(KEYCONTAINER), RSAENH_MAGIC_CONTAINER,
1297 destroy_key_container, (OBJECTHDR**)&pKeyContainer);
1298 if (hKeyContainer != (HCRYPTPROV)INVALID_HANDLE_VALUE)
1299 {
1300 lstrcpynA(pKeyContainer->szName, pszContainerName, MAX_PATH);
1301 pKeyContainer->dwFlags = dwFlags;
1302 pKeyContainer->dwEnumAlgsCtr = 0;
1303 pKeyContainer->hKeyExchangeKeyPair = (HCRYPTKEY)INVALID_HANDLE_VALUE;
1304 pKeyContainer->hSignatureKeyPair = (HCRYPTKEY)INVALID_HANDLE_VALUE;
1305 if (pVTable && pVTable->pszProvName) {
1306 lstrcpynA(pKeyContainer->szProvName, pVTable->pszProvName, MAX_PATH);
1307 if (!strcmp(pVTable->pszProvName, MS_DEF_PROV_A)) {
1308 pKeyContainer->dwPersonality = RSAENH_PERSONALITY_BASE;
1309 } else if (!strcmp(pVTable->pszProvName, MS_ENHANCED_PROV_A)) {
1310 pKeyContainer->dwPersonality = RSAENH_PERSONALITY_ENHANCED;
1311 } else if (!strcmp(pVTable->pszProvName, MS_DEF_RSA_SCHANNEL_PROV_A)) {
1312 pKeyContainer->dwPersonality = RSAENH_PERSONALITY_SCHANNEL;
1313 } else if (!strcmp(pVTable->pszProvName, MS_ENH_RSA_AES_PROV_A) ||
1314 !strcmp(pVTable->pszProvName, MS_ENH_RSA_AES_PROV_XP_A)) {
1315 pKeyContainer->dwPersonality = RSAENH_PERSONALITY_AES;
1316 } else {
1317 pKeyContainer->dwPersonality = RSAENH_PERSONALITY_STRONG;
1318 }
1319 }
1320
1321 /* The new key container has to be inserted into the CSP immediately
1322 * after creation to be available for CPGetProvParam's PP_ENUMCONTAINERS. */
1323 if (!(dwFlags & CRYPT_VERIFYCONTEXT)) {
1324 HKEY hKey;
1325
1326 if (create_container_key(pKeyContainer, KEY_WRITE, &hKey))
1327 RegCloseKey(hKey);
1328 }
1329 }
1330
1331 return hKeyContainer;
1332 }
1333
1334 /******************************************************************************
1335 * read_key_value [Internal]
1336 *
1337 * Reads a key pair value from the registry
1338 *
1339 * PARAMS
1340 * hKeyContainer [I] Crypt provider to use to import the key
1341 * hKey [I] Registry key from which to read the key pair
1342 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
1343 * dwFlags [I] Flags for unprotecting the key
1344 * phCryptKey [O] Returned key
1345 */
1346 static BOOL read_key_value(HCRYPTPROV hKeyContainer, HKEY hKey, DWORD dwKeySpec, DWORD dwFlags, HCRYPTKEY *phCryptKey)
1347 {
1348 LPCSTR szValueName;
1349 DWORD dwValueType, dwLen;
1350 BYTE *pbKey;
1351 DATA_BLOB blobIn, blobOut;
1352 BOOL ret = FALSE;
1353
1354 if (!(szValueName = map_key_spec_to_key_pair_name(dwKeySpec)))
1355 return FALSE;
1356 if (RegQueryValueExA(hKey, szValueName, 0, &dwValueType, NULL, &dwLen) ==
1357 ERROR_SUCCESS)
1358 {
1359 pbKey = HeapAlloc(GetProcessHeap(), 0, dwLen);
1360 if (pbKey)
1361 {
1362 if (RegQueryValueExA(hKey, szValueName, 0, &dwValueType, pbKey, &dwLen) ==
1363 ERROR_SUCCESS)
1364 {
1365 blobIn.pbData = pbKey;
1366 blobIn.cbData = dwLen;
1367
1368 if (CryptUnprotectData(&blobIn, NULL, NULL, NULL, NULL,
1369 dwFlags, &blobOut))
1370 {
1371 ret = import_key(hKeyContainer, blobOut.pbData, blobOut.cbData, 0, 0,
1372 FALSE, phCryptKey);
1373 LocalFree(blobOut.pbData);
1374 }
1375 }
1376 HeapFree(GetProcessHeap(), 0, pbKey);
1377 }
1378 }
1379 if (ret)
1380 {
1381 CRYPTKEY *pKey;
1382
1383 if (lookup_handle(&handle_table, *phCryptKey, RSAENH_MAGIC_KEY,
1384 (OBJECTHDR**)&pKey))
1385 {
1386 if ((szValueName = map_key_spec_to_permissions_name(dwKeySpec)))
1387 {
1388 dwLen = sizeof(pKey->dwPermissions);
1389 RegQueryValueExA(hKey, szValueName, 0, NULL,
1390 (BYTE *)&pKey->dwPermissions, &dwLen);
1391 }
1392 }
1393 }
1394 return ret;
1395 }
1396
1397 /******************************************************************************
1398 * read_key_container [Internal]
1399 *
1400 * Tries to read the persistent state of the key container (mainly the signature
1401 * and key exchange private keys) given by pszContainerName.
1402 *
1403 * PARAMS
1404 * pszContainerName [I] Name of the key container to read from the registry
1405 * pVTable [I] Pointer to context data provided by the operating system
1406 *
1407 * RETURNS
1408 * Success: Handle to the key container read from the registry
1409 * Failure: INVALID_HANDLE_VALUE
1410 */
1411 static HCRYPTPROV read_key_container(PCHAR pszContainerName, DWORD dwFlags, const VTableProvStruc *pVTable)
1412 {
1413 HKEY hKey;
1414 KEYCONTAINER *pKeyContainer;
1415 HCRYPTPROV hKeyContainer;
1416 HCRYPTKEY hCryptKey;
1417
1418 if (!open_container_key(pszContainerName, dwFlags, KEY_READ, &hKey))
1419 {
1420 SetLastError(NTE_BAD_KEYSET);
1421 return (HCRYPTPROV)INVALID_HANDLE_VALUE;
1422 }
1423
1424 hKeyContainer = new_key_container(pszContainerName, dwFlags, pVTable);
1425 if (hKeyContainer != (HCRYPTPROV)INVALID_HANDLE_VALUE)
1426 {
1427 DWORD dwProtectFlags = (dwFlags & CRYPT_MACHINE_KEYSET) ?
1428 CRYPTPROTECT_LOCAL_MACHINE : 0;
1429
1430 if (!lookup_handle(&handle_table, hKeyContainer, RSAENH_MAGIC_CONTAINER,
1431 (OBJECTHDR**)&pKeyContainer))
1432 return (HCRYPTPROV)INVALID_HANDLE_VALUE;
1433
1434 /* read_key_value calls import_key, which calls import_private_key,
1435 * which implicitly installs the key value into the appropriate key
1436 * container key. Thus the ref count is incremented twice, once for
1437 * the output key value, and once for the implicit install, and needs
1438 * to be decremented to balance the two.
1439 */
1440 if (read_key_value(hKeyContainer, hKey, AT_KEYEXCHANGE,
1441 dwProtectFlags, &hCryptKey))
1442 release_handle(&handle_table, hCryptKey, RSAENH_MAGIC_KEY);
1443 if (read_key_value(hKeyContainer, hKey, AT_SIGNATURE,
1444 dwProtectFlags, &hCryptKey))
1445 release_handle(&handle_table, hCryptKey, RSAENH_MAGIC_KEY);
1446 }
1447
1448 return hKeyContainer;
1449 }
1450
1451 /******************************************************************************
1452 * build_hash_signature [Internal]
1453 *
1454 * Builds a padded version of a hash to match the length of the RSA key modulus.
1455 *
1456 * PARAMS
1457 * pbSignature [O] The padded hash object is stored here.
1458 * dwLen [I] Length of the pbSignature buffer.
1459 * aiAlgid [I] Algorithm identifier of the hash to be padded.
1460 * abHashValue [I] The value of the hash object.
1461 * dwHashLen [I] Length of the hash value.
1462 * dwFlags [I] Selection of padding algorithm.
1463 *
1464 * RETURNS
1465 * Success: TRUE
1466 * Failure: FALSE (NTE_BAD_ALGID)
1467 */
1468 static BOOL build_hash_signature(BYTE *pbSignature, DWORD dwLen, ALG_ID aiAlgid,
1469 const BYTE *abHashValue, DWORD dwHashLen, DWORD dwFlags)
1470 {
1471 /* These prefixes are meant to be concatenated with hash values of the
1472 * respective kind to form a PKCS #7 DigestInfo. */
1473 static const struct tagOIDDescriptor {
1474 ALG_ID aiAlgid;
1475 DWORD dwLen;
1476 const BYTE abOID[19];
1477 } aOIDDescriptor[] = {
1478 { CALG_MD2, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1479 0x86, 0xf7, 0x0d, 0x02, 0x02, 0x05, 0x00, 0x04, 0x10 } },
1480 { CALG_MD4, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1481 0x86, 0xf7, 0x0d, 0x02, 0x04, 0x05, 0x00, 0x04, 0x10 } },
1482 { CALG_MD5, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1483 0x86, 0xf7, 0x0d, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10 } },
1484 { CALG_SHA, 15, { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03,
1485 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14 } },
1486 { CALG_SHA_256, 19, { 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
1487 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01,
1488 0x05, 0x00, 0x04, 0x20 } },
1489 { CALG_SHA_384, 19, { 0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
1490 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02,
1491 0x05, 0x00, 0x04, 0x30 } },
1492 { CALG_SHA_512, 19, { 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
1493 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03,
1494 0x05, 0x00, 0x04, 0x40 } },
1495 { CALG_SSL3_SHAMD5, 0, { 0 } },
1496 { 0, 0, { 0 } }
1497 };
1498 DWORD dwIdxOID, i, j;
1499
1500 for (dwIdxOID = 0; aOIDDescriptor[dwIdxOID].aiAlgid; dwIdxOID++) {
1501 if (aOIDDescriptor[dwIdxOID].aiAlgid == aiAlgid) break;
1502 }
1503
1504 if (!aOIDDescriptor[dwIdxOID].aiAlgid) {
1505 SetLastError(NTE_BAD_ALGID);
1506 return FALSE;
1507 }
1508
1509 /* Build the padded signature */
1510 if (dwFlags & CRYPT_X931_FORMAT) {
1511 pbSignature[0] = 0x6b;
1512 for (i=1; i < dwLen - dwHashLen - 3; i++) {
1513 pbSignature[i] = 0xbb;
1514 }
1515 pbSignature[i++] = 0xba;
1516 for (j=0; j < dwHashLen; j++, i++) {
1517 pbSignature[i] = abHashValue[j];
1518 }
1519 pbSignature[i++] = 0x33;
1520 pbSignature[i++] = 0xcc;
1521 } else {
1522 pbSignature[0] = 0x00;
1523 pbSignature[1] = 0x01;
1524 if (dwFlags & CRYPT_NOHASHOID) {
1525 for (i=2; i < dwLen - 1 - dwHashLen; i++) {
1526 pbSignature[i] = 0xff;
1527 }
1528 pbSignature[i++] = 0x00;
1529 } else {
1530 for (i=2; i < dwLen - 1 - aOIDDescriptor[dwIdxOID].dwLen - dwHashLen; i++) {
1531 pbSignature[i] = 0xff;
1532 }
1533 pbSignature[i++] = 0x00;
1534 for (j=0; j < aOIDDescriptor[dwIdxOID].dwLen; j++) {
1535 pbSignature[i++] = aOIDDescriptor[dwIdxOID].abOID[j];
1536 }
1537 }
1538 for (j=0; j < dwHashLen; j++) {
1539 pbSignature[i++] = abHashValue[j];
1540 }
1541 }
1542
1543 return TRUE;
1544 }
1545
1546 /******************************************************************************
1547 * tls1_p [Internal]
1548 *
1549 * This is an implementation of the 'P_hash' helper function for TLS1's PRF.
1550 * It is used exclusively by tls1_prf. For details see RFC 2246, chapter 5.
1551 * The pseudo random stream generated by this function is exclusive or'ed with
1552 * the data in pbBuffer.
1553 *
1554 * PARAMS
1555 * hHMAC [I] HMAC object, which will be used in pseudo random generation
1556 * pblobSeed [I] Seed value
1557 * pbBuffer [I/O] Pseudo random stream will be xor'ed to the provided data
1558 * dwBufferLen [I] Number of pseudo random bytes desired
1559 *
1560 * RETURNS
1561 * Success: TRUE
1562 * Failure: FALSE
1563 */
1564 static BOOL tls1_p(HCRYPTHASH hHMAC, const PCRYPT_DATA_BLOB pblobSeed, BYTE *pbBuffer,
1565 DWORD dwBufferLen)
1566 {
1567 CRYPTHASH *pHMAC;
1568 BYTE abAi[RSAENH_MAX_HASH_SIZE];
1569 DWORD i = 0;
1570
1571 if (!lookup_handle(&handle_table, hHMAC, RSAENH_MAGIC_HASH, (OBJECTHDR**)&pHMAC)) {
1572 SetLastError(NTE_BAD_HASH);
1573 return FALSE;
1574 }
1575
1576 /* compute A_1 = HMAC(seed) */
1577 init_hash(pHMAC);
1578 update_hash(pHMAC, pblobSeed->pbData, pblobSeed->cbData);
1579 finalize_hash(pHMAC);
1580 memcpy(abAi, pHMAC->abHashValue, pHMAC->dwHashSize);
1581
1582 do {
1583 /* compute HMAC(A_i + seed) */
1584 init_hash(pHMAC);
1585 update_hash(pHMAC, abAi, pHMAC->dwHashSize);
1586 update_hash(pHMAC, pblobSeed->pbData, pblobSeed->cbData);
1587 finalize_hash(pHMAC);
1588
1589 /* pseudo random stream := CONCAT_{i=1..n} ( HMAC(A_i + seed) ) */
1590 do {
1591 if (i >= dwBufferLen) break;
1592 pbBuffer[i] ^= pHMAC->abHashValue[i % pHMAC->dwHashSize];
1593 i++;
1594 } while (i % pHMAC->dwHashSize);
1595
1596 /* compute A_{i+1} = HMAC(A_i) */
1597 init_hash(pHMAC);
1598 update_hash(pHMAC, abAi, pHMAC->dwHashSize);
1599 finalize_hash(pHMAC);
1600 memcpy(abAi, pHMAC->abHashValue, pHMAC->dwHashSize);
1601 } while (i < dwBufferLen);
1602
1603 return TRUE;
1604 }
1605
1606 /******************************************************************************
1607 * tls1_prf [Internal]
1608 *
1609 * TLS1 pseudo random function as specified in RFC 2246, chapter 5
1610 *
1611 * PARAMS
1612 * hProv [I] Key container used to compute the pseudo random stream
1613 * hSecret [I] Key that holds the (pre-)master secret
1614 * pblobLabel [I] Descriptive label
1615 * pblobSeed [I] Seed value
1616 * pbBuffer [O] Pseudo random numbers will be stored here
1617 * dwBufferLen [I] Number of pseudo random bytes desired
1618 *
1619 * RETURNS
1620 * Success: TRUE
1621 * Failure: FALSE
1622 */
1623 static BOOL tls1_prf(HCRYPTPROV hProv, HCRYPTPROV hSecret, const PCRYPT_DATA_BLOB pblobLabel,
1624 const PCRYPT_DATA_BLOB pblobSeed, BYTE *pbBuffer, DWORD dwBufferLen)
1625 {
1626 HMAC_INFO hmacInfo = { 0, NULL, 0, NULL, 0 };
1627 HCRYPTHASH hHMAC = (HCRYPTHASH)INVALID_HANDLE_VALUE;
1628 HCRYPTKEY hHalfSecret = (HCRYPTKEY)INVALID_HANDLE_VALUE;
1629 CRYPTKEY *pHalfSecret, *pSecret;
1630 DWORD dwHalfSecretLen;
1631 BOOL result = FALSE;
1632 CRYPT_DATA_BLOB blobLabelSeed;
1633
1634 TRACE("(hProv=%08lx, hSecret=%08lx, pblobLabel=%p, pblobSeed=%p, pbBuffer=%p, dwBufferLen=%d)\n",
1635 hProv, hSecret, pblobLabel, pblobSeed, pbBuffer, dwBufferLen);
1636
1637 if (!lookup_handle(&handle_table, hSecret, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pSecret)) {
1638 SetLastError(NTE_FAIL);
1639 return FALSE;
1640 }
1641
1642 dwHalfSecretLen = (pSecret->dwKeyLen+1)/2;
1643
1644 /* concatenation of the label and the seed */
1645 if (!concat_data_blobs(&blobLabelSeed, pblobLabel, pblobSeed)) goto exit;
1646
1647 /* zero out the buffer, since two random streams will be xor'ed into it. */
1648 memset(pbBuffer, 0, dwBufferLen);
1649
1650 /* build a 'fake' key, to hold the secret. CALG_SSL2_MASTER is used since it provides
1651 * the biggest range of valid key lengths. */
1652 hHalfSecret = new_key(hProv, CALG_SSL2_MASTER, MAKELONG(0,dwHalfSecretLen*8), &pHalfSecret);
1653 if (hHalfSecret == (HCRYPTKEY)INVALID_HANDLE_VALUE) goto exit;
1654
1655 /* Derive an HMAC_MD5 hash and call the helper function. */
1656 memcpy(pHalfSecret->abKeyValue, pSecret->abKeyValue, dwHalfSecretLen);
1657 if (!RSAENH_CPCreateHash(hProv, CALG_HMAC, hHalfSecret, 0, &hHMAC)) goto exit;
1658 hmacInfo.HashAlgid = CALG_MD5;
1659 if (!RSAENH_CPSetHashParam(hProv, hHMAC, HP_HMAC_INFO, (BYTE*)&hmacInfo, 0)) goto exit;
1660 if (!tls1_p(hHMAC, &blobLabelSeed, pbBuffer, dwBufferLen)) goto exit;
1661
1662 /* Reconfigure to HMAC_SHA hash and call helper function again. */
1663 memcpy(pHalfSecret->abKeyValue, pSecret->abKeyValue + (pSecret->dwKeyLen/2), dwHalfSecretLen);
1664 hmacInfo.HashAlgid = CALG_SHA;
1665 if (!RSAENH_CPSetHashParam(hProv, hHMAC, HP_HMAC_INFO, (BYTE*)&hmacInfo, 0)) goto exit;
1666 if (!tls1_p(hHMAC, &blobLabelSeed, pbBuffer, dwBufferLen)) goto exit;
1667
1668 result = TRUE;
1669 exit:
1670 release_handle(&handle_table, hHalfSecret, RSAENH_MAGIC_KEY);
1671 if (hHMAC != (HCRYPTHASH)INVALID_HANDLE_VALUE) RSAENH_CPDestroyHash(hProv, hHMAC);
1672 free_data_blob(&blobLabelSeed);
1673 return result;
1674 }
1675
1676 /******************************************************************************
1677 * pad_data [Internal]
1678 *
1679 * Helper function for data padding according to PKCS1 #2
1680 *
1681 * PARAMS
1682 * abData [I] The data to be padded
1683 * dwDataLen [I] Length of the data
1684 * abBuffer [O] Padded data will be stored here
1685 * dwBufferLen [I] Length of the buffer (also length of padded data)
1686 * dwFlags [I] Padding format (CRYPT_SSL2_FALLBACK)
1687 *
1688 * RETURN
1689 * Success: TRUE
1690 * Failure: FALSE (NTE_BAD_LEN, too much data to pad)
1691 */
1692 static BOOL pad_data(const BYTE *abData, DWORD dwDataLen, BYTE *abBuffer, DWORD dwBufferLen,
1693 DWORD dwFlags)
1694 {
1695 DWORD i;
1696
1697 /* Ensure there is enough space for PKCS1 #2 padding */
1698 if (dwDataLen > dwBufferLen-11) {
1699 SetLastError(NTE_BAD_LEN);
1700 return FALSE;
1701 }
1702
1703 memmove(abBuffer + dwBufferLen - dwDataLen, abData, dwDataLen);
1704
1705 abBuffer[0] = 0x00;
1706 abBuffer[1] = RSAENH_PKC_BLOCKTYPE;
1707 for (i=2; i < dwBufferLen - dwDataLen - 1; i++)
1708 do gen_rand_impl(&abBuffer[i], 1); while (!abBuffer[i]);
1709 if (dwFlags & CRYPT_SSL2_FALLBACK)
1710 for (i-=8; i < dwBufferLen - dwDataLen - 1; i++)
1711 abBuffer[i] = 0x03;
1712 abBuffer[i] = 0x00;
1713
1714 return TRUE;
1715 }
1716
1717 /******************************************************************************
1718 * unpad_data [Internal]
1719 *
1720 * Remove the PKCS1 padding from RSA decrypted data
1721 *
1722 * PARAMS
1723 * abData [I] The padded data
1724 * dwDataLen [I] Length of the padded data
1725 * abBuffer [O] Data without padding will be stored here
1726 * dwBufferLen [I/O] I: Length of the buffer, O: Length of unpadded data
1727 * dwFlags [I] Currently none defined
1728 *
1729 * RETURNS
1730 * Success: TRUE
1731 * Failure: FALSE, (NTE_BAD_DATA, no valid PKCS1 padding or buffer too small)
1732 */
1733 static BOOL unpad_data(const BYTE *abData, DWORD dwDataLen, BYTE *abBuffer, DWORD *dwBufferLen,
1734 DWORD dwFlags)
1735 {
1736 DWORD i;
1737
1738 if (dwDataLen < 3)
1739 {
1740 SetLastError(NTE_BAD_DATA);
1741 return FALSE;
1742 }
1743 for (i=2; i<dwDataLen; i++)
1744 if (!abData[i])
1745 break;
1746
1747 if ((i == dwDataLen) || (*dwBufferLen < dwDataLen - i - 1) ||
1748 (abData[0] != 0x00) || (abData[1] != RSAENH_PKC_BLOCKTYPE))
1749 {
1750 SetLastError(NTE_BAD_DATA);
1751 return FALSE;
1752 }
1753
1754 *dwBufferLen = dwDataLen - i - 1;
1755 memmove(abBuffer, abData + i + 1, *dwBufferLen);
1756 return TRUE;
1757 }
1758
1759 /******************************************************************************
1760 * CPAcquireContext (RSAENH.@)
1761 *
1762 * Acquire a handle to the key container specified by pszContainer
1763 *
1764 * PARAMS
1765 * phProv [O] Pointer to the location the acquired handle will be written to.
1766 * pszContainer [I] Name of the desired key container. See Notes
1767 * dwFlags [I] Flags. See Notes.
1768 * pVTable [I] Pointer to a PVTableProvStruct containing callbacks.
1769 *
1770 * RETURNS
1771 * Success: TRUE
1772 * Failure: FALSE
1773 *
1774 * NOTES
1775 * If pszContainer is NULL or points to a zero length string the user's login
1776 * name will be used as the key container name.
1777 *
1778 * If the CRYPT_NEW_KEYSET flag is set in dwFlags a new keyset will be created.
1779 * If a keyset with the given name already exists, the function fails and sets
1780 * last error to NTE_EXISTS. If CRYPT_NEW_KEYSET is not set and the specified
1781 * key container does not exist, function fails and sets last error to
1782 * NTE_BAD_KEYSET.
1783 */
1784 BOOL WINAPI RSAENH_CPAcquireContext(HCRYPTPROV *phProv, LPSTR pszContainer,
1785 DWORD dwFlags, PVTableProvStruc pVTable)
1786 {
1787 CHAR szKeyContainerName[MAX_PATH];
1788
1789 TRACE("(phProv=%p, pszContainer=%s, dwFlags=%08x, pVTable=%p)\n", phProv,
1790 debugstr_a(pszContainer), dwFlags, pVTable);
1791
1792 if (pszContainer && *pszContainer)
1793 {
1794 lstrcpynA(szKeyContainerName, pszContainer, MAX_PATH);
1795 }
1796 else
1797 {
1798 DWORD dwLen = sizeof(szKeyContainerName);
1799 if (!GetUserNameA(szKeyContainerName, &dwLen)) return FALSE;
1800 }
1801
1802 switch (dwFlags & (CRYPT_NEWKEYSET|CRYPT_VERIFYCONTEXT|CRYPT_DELETEKEYSET))
1803 {
1804 case 0:
1805 *phProv = read_key_container(szKeyContainerName, dwFlags, pVTable);
1806 break;
1807
1808 case CRYPT_DELETEKEYSET:
1809 return delete_container_key(szKeyContainerName, dwFlags);
1810
1811 case CRYPT_NEWKEYSET:
1812 *phProv = read_key_container(szKeyContainerName, dwFlags, pVTable);
1813 if (*phProv != (HCRYPTPROV)INVALID_HANDLE_VALUE)
1814 {
1815 release_handle(&handle_table, *phProv, RSAENH_MAGIC_CONTAINER);
1816 TRACE("Can't create new keyset, already exists\n");
1817 SetLastError(NTE_EXISTS);
1818 return FALSE;
1819 }
1820 *phProv = new_key_container(szKeyContainerName, dwFlags, pVTable);
1821 break;
1822
1823 case CRYPT_VERIFYCONTEXT|CRYPT_NEWKEYSET:
1824 case CRYPT_VERIFYCONTEXT:
1825 if (pszContainer && *pszContainer) {
1826 TRACE("pszContainer should be empty\n");
1827 SetLastError(NTE_BAD_FLAGS);
1828 return FALSE;
1829 }
1830 *phProv = new_key_container("", dwFlags, pVTable);
1831 break;
1832
1833 default:
1834 *phProv = (HCRYPTPROV)INVALID_HANDLE_VALUE;
1835 SetLastError(NTE_BAD_FLAGS);
1836 return FALSE;
1837 }
1838
1839 if (*phProv != (HCRYPTPROV)INVALID_HANDLE_VALUE) {
1840 SetLastError(ERROR_SUCCESS);
1841 return TRUE;
1842 } else {
1843 return FALSE;
1844 }
1845 }
1846
1847 /******************************************************************************
1848 * CPCreateHash (RSAENH.@)
1849 *
1850 * CPCreateHash creates and initializes a new hash object.
1851 *
1852 * PARAMS
1853 * hProv [I] Handle to the key container to which the new hash will belong.
1854 * Algid [I] Identifies the hash algorithm, which will be used for the hash.
1855 * hKey [I] Handle to a session key applied for keyed hashes.
1856 * dwFlags [I] Currently no flags defined. Must be zero.
1857 * phHash [O] Points to the location where a handle to the new hash will be stored.
1858 *
1859 * RETURNS
1860 * Success: TRUE
1861 * Failure: FALSE
1862 *
1863 * NOTES
1864 * hKey is a handle to a session key applied in keyed hashes like MAC and HMAC.
1865 * If a normal hash object is to be created (like e.g. MD2 or SHA1) hKey must be zero.
1866 */
1867 BOOL WINAPI RSAENH_CPCreateHash(HCRYPTPROV hProv, ALG_ID Algid, HCRYPTKEY hKey, DWORD dwFlags,
1868 HCRYPTHASH *phHash)
1869 {
1870 CRYPTKEY *pCryptKey;
1871 CRYPTHASH *pCryptHash;
1872 const PROV_ENUMALGS_EX *peaAlgidInfo;
1873
1874 TRACE("(hProv=%08lx, Algid=%08x, hKey=%08lx, dwFlags=%08x, phHash=%p)\n", hProv, Algid, hKey,
1875 dwFlags, phHash);
1876
1877 peaAlgidInfo = get_algid_info(hProv, Algid);
1878 if (!peaAlgidInfo) return FALSE;
1879
1880 if (dwFlags)
1881 {
1882 SetLastError(NTE_BAD_FLAGS);
1883 return FALSE;
1884 }
1885
1886 if (Algid == CALG_MAC || Algid == CALG_HMAC || Algid == CALG_SCHANNEL_MASTER_HASH ||
1887 Algid == CALG_TLS1PRF)
1888 {
1889 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey)) {
1890 SetLastError(NTE_BAD_KEY);
1891 return FALSE;
1892 }
1893
1894 if ((Algid == CALG_MAC) && (GET_ALG_TYPE(pCryptKey->aiAlgid) != ALG_TYPE_BLOCK)) {
1895 SetLastError(NTE_BAD_KEY);
1896 return FALSE;
1897 }
1898
1899 if ((Algid == CALG_SCHANNEL_MASTER_HASH || Algid == CALG_TLS1PRF) &&
1900 (pCryptKey->aiAlgid != CALG_TLS1_MASTER))
1901 {
1902 SetLastError(NTE_BAD_KEY);
1903 return FALSE;
1904 }
1905 if (Algid == CALG_SCHANNEL_MASTER_HASH &&
1906 ((!pCryptKey->siSChannelInfo.blobClientRandom.cbData) ||
1907 (!pCryptKey->siSChannelInfo.blobServerRandom.cbData)))
1908 {
1909 SetLastError(ERROR_INVALID_PARAMETER);
1910 return FALSE;
1911 }
1912
1913 if ((Algid == CALG_TLS1PRF) && (pCryptKey->dwState != RSAENH_KEYSTATE_MASTERKEY)) {
1914 SetLastError(NTE_BAD_KEY_STATE);
1915 return FALSE;
1916 }
1917 }
1918
1919 *phHash = new_object(&handle_table, sizeof(CRYPTHASH), RSAENH_MAGIC_HASH,
1920 destroy_hash, (OBJECTHDR**)&pCryptHash);
1921 if (!pCryptHash) return FALSE;
1922
1923 pCryptHash->aiAlgid = Algid;
1924 pCryptHash->hKey = hKey;
1925 pCryptHash->hProv = hProv;
1926 pCryptHash->dwState = RSAENH_HASHSTATE_HASHING;
1927 pCryptHash->pHMACInfo = NULL;
1928 pCryptHash->dwHashSize = peaAlgidInfo->dwDefaultLen >> 3;
1929 init_data_blob(&pCryptHash->tpPRFParams.blobLabel);
1930 init_data_blob(&pCryptHash->tpPRFParams.blobSeed);
1931
1932 if (Algid == CALG_SCHANNEL_MASTER_HASH) {
1933 static const char keyex[] = "key expansion";
1934 BYTE key_expansion[sizeof keyex];
1935 CRYPT_DATA_BLOB blobRandom, blobKeyExpansion = { 13, key_expansion };
1936
1937 memcpy( key_expansion, keyex, sizeof keyex );
1938
1939 if (pCryptKey->dwState != RSAENH_KEYSTATE_MASTERKEY) {
1940 static const char msec[] = "master secret";
1941 BYTE master_secret[sizeof msec];
1942 CRYPT_DATA_BLOB blobLabel = { 13, master_secret };
1943 BYTE abKeyValue[48];
1944
1945 memcpy( master_secret, msec, sizeof msec );
1946
1947 /* See RFC 2246, chapter 8.1 */
1948 if (!concat_data_blobs(&blobRandom,
1949 &pCryptKey->siSChannelInfo.blobClientRandom,
1950 &pCryptKey->siSChannelInfo.blobServerRandom))
1951 {
1952 return FALSE;
1953 }
1954 tls1_prf(hProv, hKey, &blobLabel, &blobRandom, abKeyValue, 48);
1955 pCryptKey->dwState = RSAENH_KEYSTATE_MASTERKEY;
1956 memcpy(pCryptKey->abKeyValue, abKeyValue, 48);
1957 free_data_blob(&blobRandom);
1958 }
1959
1960 /* See RFC 2246, chapter 6.3 */
1961 if (!concat_data_blobs(&blobRandom,
1962 &pCryptKey->siSChannelInfo.blobServerRandom,
1963 &pCryptKey->siSChannelInfo.blobClientRandom))
1964 {
1965 return FALSE;
1966 }
1967 tls1_prf(hProv, hKey, &blobKeyExpansion, &blobRandom, pCryptHash->abHashValue,
1968 RSAENH_MAX_HASH_SIZE);
1969 free_data_blob(&blobRandom);
1970 }
1971
1972 return init_hash(pCryptHash);
1973 }
1974
1975 /******************************************************************************
1976 * CPDestroyHash (RSAENH.@)
1977 *
1978 * Releases the handle to a hash object. The object is destroyed if its reference
1979 * count reaches zero.
1980 *
1981 * PARAMS
1982 * hProv [I] Handle to the key container to which the hash object belongs.
1983 * hHash [I] Handle to the hash object to be released.
1984 *
1985 * RETURNS
1986 * Success: TRUE
1987 * Failure: FALSE
1988 */
1989 BOOL WINAPI RSAENH_CPDestroyHash(HCRYPTPROV hProv, HCRYPTHASH hHash)
1990 {
1991 TRACE("(hProv=%08lx, hHash=%08lx)\n", hProv, hHash);
1992
1993 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
1994 {
1995 SetLastError(NTE_BAD_UID);
1996 return FALSE;
1997 }
1998
1999 if (!release_handle(&handle_table, hHash, RSAENH_MAGIC_HASH))
2000 {
2001 SetLastError(NTE_BAD_HASH);
2002 return FALSE;
2003 }
2004
2005 return TRUE;
2006 }
2007
2008 /******************************************************************************
2009 * CPDestroyKey (RSAENH.@)
2010 *
2011 * Releases the handle to a key object. The object is destroyed if its reference
2012 * count reaches zero.
2013 *
2014 * PARAMS
2015 * hProv [I] Handle to the key container to which the key object belongs.
2016 * hKey [I] Handle to the key object to be released.
2017 *
2018 * RETURNS
2019 * Success: TRUE
2020 * Failure: FALSE
2021 */
2022 BOOL WINAPI RSAENH_CPDestroyKey(HCRYPTPROV hProv, HCRYPTKEY hKey)
2023 {
2024 TRACE("(hProv=%08lx, hKey=%08lx)\n", hProv, hKey);
2025
2026 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
2027 {
2028 SetLastError(NTE_BAD_UID);
2029 return FALSE;
2030 }
2031
2032 if (!release_handle(&handle_table, hKey, RSAENH_MAGIC_KEY))
2033 {
2034 SetLastError(NTE_BAD_KEY);
2035 return FALSE;
2036 }
2037
2038 return TRUE;
2039 }
2040
2041 /******************************************************************************
2042 * CPDuplicateHash (RSAENH.@)
2043 *
2044 * Clones a hash object including its current state.
2045 *
2046 * PARAMS
2047 * hUID [I] Handle to the key container the hash belongs to.
2048 * hHash [I] Handle to the hash object to be cloned.
2049 * pdwReserved [I] Reserved. Must be NULL.
2050 * dwFlags [I] No flags are currently defined. Must be 0.
2051 * phHash [O] Handle to the cloned hash object.
2052 *
2053 * RETURNS
2054 * Success: TRUE.
2055 * Failure: FALSE.
2056 */
2057 BOOL WINAPI RSAENH_CPDuplicateHash(HCRYPTPROV hUID, HCRYPTHASH hHash, DWORD *pdwReserved,
2058 DWORD dwFlags, HCRYPTHASH *phHash)
2059 {
2060 CRYPTHASH *pSrcHash, *pDestHash;
2061
2062 TRACE("(hUID=%08lx, hHash=%08lx, pdwReserved=%p, dwFlags=%08x, phHash=%p)\n", hUID, hHash,
2063 pdwReserved, dwFlags, phHash);
2064
2065 if (!is_valid_handle(&handle_table, hUID, RSAENH_MAGIC_CONTAINER))
2066 {
2067 SetLastError(NTE_BAD_UID);
2068 return FALSE;
2069 }
2070
2071 if (!lookup_handle(&handle_table, hHash, RSAENH_MAGIC_HASH, (OBJECTHDR**)&pSrcHash))
2072 {
2073 SetLastError(NTE_BAD_HASH);
2074 return FALSE;
2075 }
2076
2077 if (!phHash || pdwReserved || dwFlags)
2078 {
2079 SetLastError(ERROR_INVALID_PARAMETER);
2080 return FALSE;
2081 }
2082
2083 *phHash = new_object(&handle_table, sizeof(CRYPTHASH), RSAENH_MAGIC_HASH,
2084 destroy_hash, (OBJECTHDR**)&pDestHash);
2085 if (*phHash != (HCRYPTHASH)INVALID_HANDLE_VALUE)
2086 {
2087 *pDestHash = *pSrcHash;
2088 duplicate_hash_impl(pSrcHash->aiAlgid, &pSrcHash->context, &pDestHash->context);
2089 copy_hmac_info(&pDestHash->pHMACInfo, pSrcHash->pHMACInfo);
2090 copy_data_blob(&pDestHash->tpPRFParams.blobLabel, &pSrcHash->tpPRFParams.blobLabel);
2091 copy_data_blob(&pDestHash->tpPRFParams.blobSeed, &pSrcHash->tpPRFParams.blobSeed);
2092 }
2093
2094 return *phHash != (HCRYPTHASH)INVALID_HANDLE_VALUE;
2095 }
2096
2097 /******************************************************************************
2098 * CPDuplicateKey (RSAENH.@)
2099 *
2100 * Clones a key object including its current state.
2101 *
2102 * PARAMS
2103 * hUID [I] Handle to the key container the hash belongs to.
2104 * hKey [I] Handle to the key object to be cloned.
2105 * pdwReserved [I] Reserved. Must be NULL.
2106 * dwFlags [I] No flags are currently defined. Must be 0.
2107 * phHash [O] Handle to the cloned key object.
2108 *
2109 * RETURNS
2110 * Success: TRUE.
2111 * Failure: FALSE.
2112 */
2113 BOOL WINAPI RSAENH_CPDuplicateKey(HCRYPTPROV hUID, HCRYPTKEY hKey, DWORD *pdwReserved,
2114 DWORD dwFlags, HCRYPTKEY *phKey)
2115 {
2116 CRYPTKEY *pSrcKey, *pDestKey;
2117
2118 TRACE("(hUID=%08lx, hKey=%08lx, pdwReserved=%p, dwFlags=%08x, phKey=%p)\n", hUID, hKey,
2119 pdwReserved, dwFlags, phKey);
2120
2121 if (!is_valid_handle(&handle_table, hUID, RSAENH_MAGIC_CONTAINER))
2122 {
2123 SetLastError(NTE_BAD_UID);
2124 return FALSE;
2125 }
2126
2127 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pSrcKey))
2128 {
2129 SetLastError(NTE_BAD_KEY);
2130 return FALSE;
2131 }
2132
2133 if (!phKey || pdwReserved || dwFlags)
2134 {
2135 SetLastError(ERROR_INVALID_PARAMETER);
2136 return FALSE;
2137 }
2138
2139 *phKey = new_object(&handle_table, sizeof(CRYPTKEY), RSAENH_MAGIC_KEY, destroy_key,
2140 (OBJECTHDR**)&pDestKey);
2141 if (*phKey != (HCRYPTKEY)INVALID_HANDLE_VALUE)
2142 {
2143 *pDestKey = *pSrcKey;
2144 copy_data_blob(&pDestKey->siSChannelInfo.blobServerRandom,
2145 &pSrcKey->siSChannelInfo.blobServerRandom);
2146 copy_data_blob(&pDestKey->siSChannelInfo.blobClientRandom,
2147 &pSrcKey->siSChannelInfo.blobClientRandom);
2148 duplicate_key_impl(pSrcKey->aiAlgid, &pSrcKey->context, &pDestKey->context);
2149 return TRUE;
2150 }
2151 else
2152 {
2153 return FALSE;
2154 }
2155 }
2156
2157 /******************************************************************************
2158 * CPEncrypt (RSAENH.@)
2159 *
2160 * Encrypt data.
2161 *
2162 * PARAMS
2163 * hProv [I] The key container hKey and hHash belong to.
2164 * hKey [I] The key used to encrypt the data.
2165 * hHash [I] An optional hash object for parallel hashing. See notes.
2166 * Final [I] Indicates if this is the last block of data to encrypt.
2167 * dwFlags [I] Currently no flags defined. Must be zero.
2168 * pbData [I/O] Pointer to the data to encrypt. Encrypted data will also be stored there.
2169 * pdwDataLen [I/O] I: Length of data to encrypt, O: Length of encrypted data.
2170 * dwBufLen [I] Size of the buffer at pbData.
2171 *
2172 * RETURNS
2173 * Success: TRUE.
2174 * Failure: FALSE.
2175 *
2176 * NOTES
2177 * If a hash object handle is provided in hHash, it will be updated with the plaintext.
2178 * This is useful for message signatures.
2179 *
2180 * This function uses the standard WINAPI protocol for querying data of dynamic length.
2181 */
2182 BOOL WINAPI RSAENH_CPEncrypt(HCRYPTPROV hProv, HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final,
2183 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen, DWORD dwBufLen)
2184 {
2185 CRYPTKEY *pCryptKey;
2186 BYTE *in, out[RSAENH_MAX_BLOCK_SIZE], o[RSAENH_MAX_BLOCK_SIZE];
2187 DWORD dwEncryptedLen, i, j, k;
2188
2189 TRACE("(hProv=%08lx, hKey=%08lx, hHash=%08lx, Final=%d, dwFlags=%08x, pbData=%p, "
2190 "pdwDataLen=%p, dwBufLen=%d)\n", hProv, hKey, hHash, Final, dwFlags, pbData, pdwDataLen,
2191 dwBufLen);
2192
2193 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
2194 {
2195 SetLastError(NTE_BAD_UID);
2196 return FALSE;
2197 }
2198
2199 if (dwFlags)
2200 {
2201 SetLastError(NTE_BAD_FLAGS);
2202 return FALSE;
2203 }
2204
2205 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
2206 {
2207 SetLastError(NTE_BAD_KEY);
2208 return FALSE;
2209 }
2210
2211 if (pCryptKey->dwState == RSAENH_KEYSTATE_IDLE)
2212 pCryptKey->dwState = RSAENH_KEYSTATE_ENCRYPTING;
2213
2214 if (pCryptKey->dwState != RSAENH_KEYSTATE_ENCRYPTING)
2215 {
2216 SetLastError(NTE_BAD_DATA);
2217 return FALSE;
2218 }
2219
2220 if (is_valid_handle(&handle_table, hHash, RSAENH_MAGIC_HASH)) {
2221 if (!RSAENH_CPHashData(hProv, hHash, pbData, *pdwDataLen, 0)) return FALSE;
2222 }
2223
2224 if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_BLOCK) {
2225 if (!Final && (*pdwDataLen % pCryptKey->dwBlockLen)) {
2226 SetLastError(NTE_BAD_DATA);
2227 return FALSE;
2228 }
2229
2230 dwEncryptedLen = (*pdwDataLen/pCryptKey->dwBlockLen+(Final?1:0))*pCryptKey->dwBlockLen;
2231
2232 if (pbData == NULL) {
2233 *pdwDataLen = dwEncryptedLen;
2234 return TRUE;
2235 }
2236 else if (dwEncryptedLen > dwBufLen) {
2237 *pdwDataLen = dwEncryptedLen;
2238 SetLastError(ERROR_MORE_DATA);
2239 return FALSE;
2240 }
2241
2242 /* Pad final block with length bytes */
2243 for (i=*pdwDataLen; i<dwEncryptedLen; i++) pbData[i] = dwEncryptedLen - *pdwDataLen;
2244 *pdwDataLen = dwEncryptedLen;
2245
2246 for (i=0, in=pbData; i<*pdwDataLen; i+=pCryptKey->dwBlockLen, in+=pCryptKey->dwBlockLen) {
2247 switch (pCryptKey->dwMode) {
2248 case CRYPT_MODE_ECB:
2249 encrypt_block_impl(pCryptKey->aiAlgid, 0, &pCryptKey->context, in, out,
2250 RSAENH_ENCRYPT);
2251 break;
2252
2253 case CRYPT_MODE_CBC:
2254 for (j=0; j<pCryptKey->dwBlockLen; j++) in[j] ^= pCryptKey->abChainVector[j];
2255 encrypt_block_impl(pCryptKey->aiAlgid, 0, &pCryptKey->context, in, out,
2256 RSAENH_ENCRYPT);
2257 memcpy(pCryptKey->abChainVector, out, pCryptKey->dwBlockLen);
2258 break;
2259
2260 case CRYPT_MODE_CFB:
2261 for (j=0; j<pCryptKey->dwBlockLen; j++) {
2262 encrypt_block_impl(pCryptKey->aiAlgid, 0, &pCryptKey->context,
2263 pCryptKey->abChainVector, o, RSAENH_ENCRYPT);
2264 out[j] = in[j] ^ o[0];
2265 for (k=0; k<pCryptKey->dwBlockLen-1; k++)
2266 pCryptKey->abChainVector[k] = pCryptKey->abChainVector[k+1];
2267 pCryptKey->abChainVector[k] = out[j];
2268 }
2269 break;
2270
2271 default:
2272 SetLastError(NTE_BAD_ALGID);
2273 return FALSE;
2274 }
2275 memcpy(in, out, pCryptKey->dwBlockLen);
2276 }
2277 } else if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_STREAM) {
2278 if (pbData == NULL) {
2279 *pdwDataLen = dwBufLen;
2280 return TRUE;
2281 }
2282 encrypt_stream_impl(pCryptKey->aiAlgid, &pCryptKey->context, pbData, *pdwDataLen);
2283 } else if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_RSA) {
2284 if (pCryptKey->aiAlgid == CALG_RSA_SIGN) {
2285 SetLastError(NTE_BAD_KEY);
2286 return FALSE;
2287 }
2288 if (!pbData) {
2289 *pdwDataLen = pCryptKey->dwBlockLen;
2290 return TRUE;
2291 }
2292 if (dwBufLen < pCryptKey->dwBlockLen) {
2293 SetLastError(ERROR_MORE_DATA);
2294 return FALSE;
2295 }
2296 if (!pad_data(pbData, *pdwDataLen, pbData, pCryptKey->dwBlockLen, dwFlags)) return FALSE;
2297 encrypt_block_impl(pCryptKey->aiAlgid, PK_PUBLIC, &pCryptKey->context, pbData, pbData, RSAENH_ENCRYPT);
2298 *pdwDataLen = pCryptKey->dwBlockLen;
2299 Final = TRUE;
2300 } else {
2301 SetLastError(NTE_BAD_TYPE);
2302 return FALSE;
2303 }
2304
2305 if (Final) setup_key(pCryptKey);
2306
2307 return TRUE;
2308 }
2309
2310 /******************************************************************************
2311 * CPDecrypt (RSAENH.@)
2312 *
2313 * Decrypt data.
2314 *
2315 * PARAMS
2316 * hProv [I] The key container hKey and hHash belong to.
2317 * hKey [I] The key used to decrypt the data.
2318 * hHash [I] An optional hash object for parallel hashing. See notes.
2319 * Final [I] Indicates if this is the last block of data to decrypt.
2320 * dwFlags [I] Currently no flags defined. Must be zero.
2321 * pbData [I/O] Pointer to the data to decrypt. Plaintext will also be stored there.
2322 * pdwDataLen [I/O] I: Length of ciphertext, O: Length of plaintext.
2323 *
2324 * RETURNS
2325 * Success: TRUE.
2326 * Failure: FALSE.
2327 *
2328 * NOTES
2329 * If a hash object handle is provided in hHash, it will be updated with the plaintext.
2330 * This is useful for message signatures.
2331 *
2332 * This function uses the standard WINAPI protocol for querying data of dynamic length.
2333 */
2334 BOOL WINAPI RSAENH_CPDecrypt(HCRYPTPROV hProv, HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final,
2335 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
2336 {
2337 CRYPTKEY *pCryptKey;
2338 BYTE *in, out[RSAENH_MAX_BLOCK_SIZE], o[RSAENH_MAX_BLOCK_SIZE];
2339 DWORD i, j, k;
2340 DWORD dwMax;
2341
2342 TRACE("(hProv=%08lx, hKey=%08lx, hHash=%08lx, Final=%d, dwFlags=%08x, pbData=%p, "
2343 "pdwDataLen=%p)\n", hProv, hKey, hHash, Final, dwFlags, pbData, pdwDataLen);
2344
2345 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
2346 {
2347 SetLastError(NTE_BAD_UID);
2348 return FALSE;
2349 }
2350
2351 if (dwFlags)
2352 {
2353 SetLastError(NTE_BAD_FLAGS);
2354 return FALSE;
2355 }
2356
2357 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
2358 {
2359 SetLastError(NTE_BAD_KEY);
2360 return FALSE;
2361 }
2362
2363 if (pCryptKey->dwState == RSAENH_KEYSTATE_IDLE)
2364 pCryptKey->dwState = RSAENH_KEYSTATE_ENCRYPTING;
2365
2366 if (pCryptKey->dwState != RSAENH_KEYSTATE_ENCRYPTING)
2367 {
2368 SetLastError(NTE_BAD_DATA);
2369 return FALSE;
2370 }
2371
2372 dwMax=*pdwDataLen;
2373
2374 if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_BLOCK) {
2375 for (i=0, in=pbData; i<*pdwDataLen; i+=pCryptKey->dwBlockLen, in+=pCryptKey->dwBlockLen) {
2376 switch (pCryptKey->dwMode) {
2377 case CRYPT_MODE_ECB:
2378 encrypt_block_impl(pCryptKey->aiAlgid, 0, &pCryptKey->context, in, out,
2379 RSAENH_DECRYPT);
2380 break;
2381
2382 case CRYPT_MODE_CBC:
2383 encrypt_block_impl(pCryptKey->aiAlgid, 0, &pCryptKey->context, in, out,
2384 RSAENH_DECRYPT);
2385 for (j=0; j<pCryptKey->dwBlockLen; j++) out[j] ^= pCryptKey->abChainVector[j];
2386 memcpy(pCryptKey->abChainVector, in, pCryptKey->dwBlockLen);
2387 break;
2388
2389 case CRYPT_MODE_CFB:
2390 for (j=0; j<pCryptKey->dwBlockLen; j++) {
2391 encrypt_block_impl(pCryptKey->aiAlgid, 0, &pCryptKey->context,
2392 pCryptKey->abChainVector, o, RSAENH_ENCRYPT);
2393 out[j] = in[j] ^ o[0];
2394 for (k=0; k<pCryptKey->dwBlockLen-1; k++)
2395 pCryptKey->abChainVector[k] = pCryptKey->abChainVector[k+1];
2396 pCryptKey->abChainVector[k] = in[j];
2397 }
2398 break;
2399
2400 default:
2401 SetLastError(NTE_BAD_ALGID);
2402 return FALSE;
2403 }
2404 memcpy(in, out, pCryptKey->dwBlockLen);
2405 }
2406 if (Final) {
2407 if (pbData[*pdwDataLen-1] &&
2408 pbData[*pdwDataLen-1] <= pCryptKey->dwBlockLen &&
2409 pbData[*pdwDataLen-1] <= *pdwDataLen) {
2410 BOOL padOkay = TRUE;
2411
2412 /* check that every bad byte has the same value */
2413 for (i = 1; padOkay && i < pbData[*pdwDataLen-1]; i++)
2414 if (pbData[*pdwDataLen - i - 1] != pbData[*pdwDataLen - 1])
2415 padOkay = FALSE;
2416 if (padOkay)
2417 *pdwDataLen -= pbData[*pdwDataLen-1];
2418 else {
2419 SetLastError(NTE_BAD_DATA);
2420 setup_key(pCryptKey);
2421 return FALSE;
2422 }
2423 }
2424 else {
2425 SetLastError(NTE_BAD_DATA);
2426 setup_key(pCryptKey);
2427 return FALSE;
2428 }
2429 }
2430
2431 } else if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_STREAM) {
2432 encrypt_stream_impl(pCryptKey->aiAlgid, &pCryptKey->context, pbData, *pdwDataLen);
2433 } else if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_RSA) {
2434 if (pCryptKey->aiAlgid == CALG_RSA_SIGN) {
2435 SetLastError(NTE_BAD_KEY);
2436 return FALSE;
2437 }
2438 encrypt_block_impl(pCryptKey->aiAlgid, PK_PRIVATE, &pCryptKey->context, pbData, pbData, RSAENH_DECRYPT);
2439 if (!unpad_data(pbData, pCryptKey->dwBlockLen, pbData, pdwDataLen, dwFlags)) return FALSE;
2440 Final = TRUE;
2441 } else {
2442 SetLastError(NTE_BAD_TYPE);
2443 return FALSE;
2444 }
2445
2446 if (Final) setup_key(pCryptKey);
2447
2448 if (is_valid_handle(&handle_table, hHash, RSAENH_MAGIC_HASH)) {
2449 if (*pdwDataLen>dwMax ||
2450 !RSAENH_CPHashData(hProv, hHash, pbData, *pdwDataLen, 0)) return FALSE;
2451 }
2452
2453 return TRUE;
2454 }
2455
2456 static BOOL crypt_export_simple(CRYPTKEY *pCryptKey, CRYPTKEY *pPubKey,
2457 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
2458 {
2459 BLOBHEADER *pBlobHeader = (BLOBHEADER*)pbData;
2460 ALG_ID *pAlgid = (ALG_ID*)(pBlobHeader+1);
2461 DWORD dwDataLen;
2462
2463 if (!(GET_ALG_CLASS(pCryptKey->aiAlgid)&(ALG_CLASS_DATA_ENCRYPT|ALG_CLASS_MSG_ENCRYPT))) {
2464 SetLastError(NTE_BAD_KEY); /* FIXME: error code? */
2465 return FALSE;
2466 }
2467
2468 dwDataLen = sizeof(BLOBHEADER) + sizeof(ALG_ID) + pPubKey->dwBlockLen;
2469 if (pbData) {
2470 if (*pdwDataLen < dwDataLen) {
2471 SetLastError(ERROR_MORE_DATA);
2472 *pdwDataLen = dwDataLen;
2473 return FALSE;
2474 }
2475
2476 pBlobHeader->bType = SIMPLEBLOB;
2477 pBlobHeader->bVersion = CUR_BLOB_VERSION;
2478 pBlobHeader->reserved = 0;
2479 pBlobHeader->aiKeyAlg = pCryptKey->aiAlgid;
2480
2481 *pAlgid = pPubKey->aiAlgid;
2482
2483 if (!pad_data(pCryptKey->abKeyValue, pCryptKey->dwKeyLen, (BYTE*)(pAlgid+1),
2484 pPubKey->dwBlockLen, dwFlags))
2485 {
2486 return FALSE;
2487 }
2488
2489 encrypt_block_impl(pPubKey->aiAlgid, PK_PUBLIC, &pPubKey->context, (BYTE*)(pAlgid+1),
2490 (BYTE*)(pAlgid+1), RSAENH_ENCRYPT);
2491 }
2492 *pdwDataLen = dwDataLen;
2493 return TRUE;
2494 }
2495
2496 static BOOL crypt_export_public_key(CRYPTKEY *pCryptKey, BYTE *pbData,
2497 DWORD *pdwDataLen)
2498 {
2499 BLOBHEADER *pBlobHeader = (BLOBHEADER*)pbData;
2500 RSAPUBKEY *pRSAPubKey = (RSAPUBKEY*)(pBlobHeader+1);
2501 DWORD dwDataLen;
2502
2503 if ((pCryptKey->aiAlgid != CALG_RSA_KEYX) && (pCryptKey->aiAlgid != CALG_RSA_SIGN)) {
2504 SetLastError(NTE_BAD_KEY);
2505 return FALSE;
2506 }
2507
2508 dwDataLen = sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) + pCryptKey->dwKeyLen;
2509 if (pbData) {
2510 if (*pdwDataLen < dwDataLen) {
2511 SetLastError(ERROR_MORE_DATA);
2512 *pdwDataLen = dwDataLen;
2513 return FALSE;
2514 }
2515
2516 pBlobHeader->bType = PUBLICKEYBLOB;
2517 pBlobHeader->bVersion = CUR_BLOB_VERSION;
2518 pBlobHeader->reserved = 0;
2519 pBlobHeader->aiKeyAlg = pCryptKey->aiAlgid;
2520
2521 pRSAPubKey->magic = RSAENH_MAGIC_RSA1;
2522 pRSAPubKey->bitlen = pCryptKey->dwKeyLen << 3;
2523
2524 export_public_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_private_key(CRYPTKEY *pCryptKey, BOOL force,
2532 BYTE *pbData, DWORD *pdwDataLen)
2533 {
2534 BLOBHEADER *pBlobHeader = (BLOBHEADER*)pbData;
2535 RSAPUBKEY *pRSAPubKey = (RSAPUBKEY*)(pBlobHeader+1);
2536 DWORD dwDataLen;
2537
2538 if ((pCryptKey->aiAlgid != CALG_RSA_KEYX) && (pCryptKey->aiAlgid != CALG_RSA_SIGN)) {
2539 SetLastError(NTE_BAD_KEY);
2540 return FALSE;
2541 }
2542 if (!force && !(pCryptKey->dwPermissions & CRYPT_EXPORT))
2543 {
2544 SetLastError(NTE_BAD_KEY_STATE);
2545 return FALSE;
2546 }
2547
2548 dwDataLen = sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) +
2549 2 * pCryptKey->dwKeyLen + 5 * ((pCryptKey->dwKeyLen + 1) >> 1);
2550 if (pbData) {
2551 if (*pdwDataLen < dwDataLen) {
2552 SetLastError(ERROR_MORE_DATA);
2553 *pdwDataLen = dwDataLen;
2554 return FALSE;
2555 }
2556
2557 pBlobHeader->bType = PRIVATEKEYBLOB;
2558 pBlobHeader->bVersion = CUR_BLOB_VERSION;
2559 pBlobHeader->reserved = 0;
2560 pBlobHeader->aiKeyAlg = pCryptKey->aiAlgid;
2561
2562 pRSAPubKey->magic = RSAENH_MAGIC_RSA2;
2563 pRSAPubKey->bitlen = pCryptKey->dwKeyLen << 3;
2564
2565 export_private_key_impl((BYTE*)(pRSAPubKey+1), &pCryptKey->context,
2566 pCryptKey->dwKeyLen, &pRSAPubKey->pubexp);
2567 }
2568 *pdwDataLen = dwDataLen;
2569 return TRUE;
2570 }
2571
2572 static BOOL crypt_export_plaintext_key(CRYPTKEY *pCryptKey, BYTE *pbData,
2573 DWORD *pdwDataLen)
2574 {
2575 BLOBHEADER *pBlobHeader = (BLOBHEADER*)pbData;
2576 DWORD *pKeyLen = (DWORD*)(pBlobHeader+1);
2577 BYTE *pbKey = (BYTE*)(pKeyLen+1);
2578 DWORD dwDataLen;
2579
2580 dwDataLen = sizeof(BLOBHEADER) + sizeof(DWORD) + pCryptKey->dwKeyLen;
2581 if (pbData) {
2582 if (*pdwDataLen < dwDataLen) {
2583 SetLastError(ERROR_MORE_DATA);
2584 *pdwDataLen = dwDataLen;
2585 return FALSE;
2586 }
2587
2588 pBlobHeader->bType = PLAINTEXTKEYBLOB;
2589 pBlobHeader->bVersion = CUR_BLOB_VERSION;
2590 pBlobHeader->reserved = 0;
2591 pBlobHeader->aiKeyAlg = pCryptKey->aiAlgid;
2592
2593 *pKeyLen = pCryptKey->dwKeyLen;
2594 memcpy(pbKey, pCryptKey->abKeyValue, pCryptKey->dwKeyLen);
2595 }
2596 *pdwDataLen = dwDataLen;
2597 return TRUE;
2598 }
2599 /******************************************************************************
2600 * crypt_export_key [Internal]
2601 *
2602 * Export a key into a binary large object (BLOB). Called by CPExportKey and
2603 * by store_key_pair.
2604 *
2605 * PARAMS
2606 * pCryptKey [I] Key to be exported.
2607 * hPubKey [I] Key used to encrypt sensitive BLOB data.
2608 * dwBlobType [I] SIMPLEBLOB, PUBLICKEYBLOB or PRIVATEKEYBLOB.
2609 * dwFlags [I] Currently none defined.
2610 * force [I] If TRUE, the key is written no matter what the key's
2611 * permissions are. Otherwise the key's permissions are
2612 * checked before exporting.
2613 * pbData [O] Pointer to a buffer where the BLOB will be written to.
2614 * pdwDataLen [I/O] I: Size of buffer at pbData, O: Size of BLOB
2615 *
2616 * RETURNS
2617 * Success: TRUE.
2618 * Failure: FALSE.
2619 */
2620 static BOOL crypt_export_key(CRYPTKEY *pCryptKey, HCRYPTKEY hPubKey,
2621 DWORD dwBlobType, DWORD dwFlags, BOOL force,
2622 BYTE *pbData, DWORD *pdwDataLen)
2623 {
2624 CRYPTKEY *pPubKey;
2625
2626 if (dwFlags & CRYPT_SSL2_FALLBACK) {
2627 if (pCryptKey->aiAlgid != CALG_SSL2_MASTER) {
2628 SetLastError(NTE_BAD_KEY);
2629 return FALSE;
2630 }
2631 }
2632
2633 switch ((BYTE)dwBlobType)
2634 {
2635 case SIMPLEBLOB:
2636 if (!lookup_handle(&handle_table, hPubKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pPubKey)){
2637 SetLastError(NTE_BAD_PUBLIC_KEY); /* FIXME: error_code? */
2638 return FALSE;
2639 }
2640 return crypt_export_simple(pCryptKey, pPubKey, dwFlags, pbData,
2641 pdwDataLen);
2642
2643 case PUBLICKEYBLOB:
2644 if (is_valid_handle(&handle_table, hPubKey, RSAENH_MAGIC_KEY)) {
2645 SetLastError(NTE_BAD_KEY); /* FIXME: error code? */
2646 return FALSE;
2647 }
2648
2649 return crypt_export_public_key(pCryptKey, pbData, pdwDataLen);
2650
2651 case PRIVATEKEYBLOB:
2652 return crypt_export_private_key(pCryptKey, force, pbData, pdwDataLen);
2653
2654 case PLAINTEXTKEYBLOB:
2655 return crypt_export_plaintext_key(pCryptKey, pbData, pdwDataLen);
2656
2657 default:
2658 SetLastError(NTE_BAD_TYPE); /* FIXME: error code? */
2659 return FALSE;
2660 }
2661 }
2662
2663 /******************************************************************************
2664 * CPExportKey (RSAENH.@)
2665 *
2666 * Export a key into a binary large object (BLOB).
2667 *
2668 * PARAMS
2669 * hProv [I] Key container from which a key is to be exported.
2670 * hKey [I] Key to be exported.
2671 * hPubKey [I] Key used to encrypt sensitive BLOB data.
2672 * dwBlobType [I] SIMPLEBLOB, PUBLICKEYBLOB or PRIVATEKEYBLOB.
2673 * dwFlags [I] Currently none defined.
2674 * pbData [O] Pointer to a buffer where the BLOB will be written to.
2675 * pdwDataLen [I/O] I: Size of buffer at pbData, O: Size of BLOB
2676 *
2677 * RETURNS
2678 * Success: TRUE.
2679 * Failure: FALSE.
2680 */
2681 BOOL WINAPI RSAENH_CPExportKey(HCRYPTPROV hProv, HCRYPTKEY hKey, HCRYPTKEY hPubKey,
2682 DWORD dwBlobType, DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
2683 {
2684 CRYPTKEY *pCryptKey;
2685
2686 TRACE("(hProv=%08lx, hKey=%08lx, hPubKey=%08lx, dwBlobType=%08x, dwFlags=%08x, pbData=%p,"
2687 "pdwDataLen=%p)\n", hProv, hKey, hPubKey, dwBlobType, dwFlags, pbData, pdwDataLen);
2688
2689 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
2690 {
2691 SetLastError(NTE_BAD_UID);
2692 return FALSE;
2693 }
2694
2695 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
2696 {
2697 SetLastError(NTE_BAD_KEY);
2698 return FALSE;
2699 }
2700
2701 return crypt_export_key(pCryptKey, hPubKey, dwBlobType, dwFlags, FALSE,
2702 pbData, pdwDataLen);
2703 }
2704
2705 /******************************************************************************
2706 * release_and_install_key [Internal]
2707 *
2708 * Release an existing key, if present, and replaces it with a new one.
2709 *
2710 * PARAMS
2711 * hProv [I] Key container into which the key is to be imported.
2712 * src [I] Key which will replace *dest
2713 * dest [I] Points to key to be released and replaced with src
2714 * fStoreKey [I] If TRUE, the newly installed key is stored to the registry.
2715 */
2716 static void release_and_install_key(HCRYPTPROV hProv, HCRYPTKEY src,
2717 HCRYPTKEY *dest, DWORD fStoreKey)
2718 {
2719 RSAENH_CPDestroyKey(hProv, *dest);
2720 copy_handle(&handle_table, src, RSAENH_MAGIC_KEY, dest);
2721 if (fStoreKey)
2722 {
2723 KEYCONTAINER *pKeyContainer;
2724
2725 if ((pKeyContainer = get_key_container(hProv)))
2726 {
2727 store_key_container_keys(pKeyContainer);
2728 store_key_container_permissions(pKeyContainer);
2729 }
2730 }
2731 }
2732
2733 /******************************************************************************
2734 * import_private_key [Internal]
2735 *
2736 * Import a BLOB'ed private key into a key container.
2737 *
2738 * PARAMS
2739 * hProv [I] Key container into which the private key is to be imported.
2740 * pbData [I] Pointer to a buffer which holds the private key BLOB.
2741 * dwDataLen [I] Length of data in buffer at pbData.
2742 * dwFlags [I] One of:
2743 * CRYPT_EXPORTABLE: the imported key is marked exportable
2744 * fStoreKey [I] If TRUE, the imported key is stored to the registry.
2745 * phKey [O] Handle to the imported key.
2746 *
2747 *
2748 * NOTES
2749 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2750 * it's a PRIVATEKEYBLOB.
2751 *
2752 * RETURNS
2753 * Success: TRUE.
2754 * Failure: FALSE.
2755 */
2756 static BOOL import_private_key(HCRYPTPROV hProv, const BYTE *pbData, DWORD dwDataLen,
2757 DWORD dwFlags, BOOL fStoreKey, HCRYPTKEY *phKey)
2758 {
2759 KEYCONTAINER *pKeyContainer;
2760 CRYPTKEY *pCryptKey;
2761 const BLOBHEADER *pBlobHeader = (const BLOBHEADER*)pbData;
2762 const RSAPUBKEY *pRSAPubKey = (const RSAPUBKEY*)(pBlobHeader+1);
2763 BOOL ret;
2764
2765 if (dwFlags & CRYPT_IPSEC_HMAC_KEY)
2766 {
2767 FIXME("unimplemented for CRYPT_IPSEC_HMAC_KEY\n");
2768 SetLastError(NTE_BAD_FLAGS);
2769 return FALSE;
2770 }
2771 if (!(pKeyContainer = get_key_container(hProv)))
2772 return FALSE;
2773
2774 if ((dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY)))
2775 {
2776 ERR("datalen %d not long enough for a BLOBHEADER + RSAPUBKEY\n",
2777 dwDataLen);
2778 SetLastError(NTE_BAD_DATA);
2779 return FALSE;
2780 }
2781 if (pRSAPubKey->magic != RSAENH_MAGIC_RSA2)
2782 {
2783 ERR("unexpected magic %08x\n", pRSAPubKey->magic);
2784 SetLastError(NTE_BAD_DATA);
2785 return FALSE;
2786 }
2787 if ((dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) +
2788 (pRSAPubKey->bitlen >> 3) + (5 * ((pRSAPubKey->bitlen+8)>>4))))
2789 {
2790 DWORD expectedLen = sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) +
2791 (pRSAPubKey->bitlen >> 3) + (5 * ((pRSAPubKey->bitlen+8)>>4));
2792
2793 ERR("blob too short for pub key: expect %d, got %d\n",
2794 expectedLen, dwDataLen);
2795 SetLastError(NTE_BAD_DATA);
2796 return FALSE;
2797 }
2798
2799 *phKey = new_key(hProv, pBlobHeader->aiKeyAlg, MAKELONG(0,pRSAPubKey->bitlen), &pCryptKey);
2800 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) return FALSE;
2801 setup_key(pCryptKey);
2802 ret = import_private_key_impl((const BYTE*)(pRSAPubKey+1), &pCryptKey->context,
2803 pRSAPubKey->bitlen/8, dwDataLen, pRSAPubKey->pubexp);
2804 if (ret) {
2805 if (dwFlags & CRYPT_EXPORTABLE)
2806 pCryptKey->dwPermissions |= CRYPT_EXPORT;
2807 switch (pBlobHeader->aiKeyAlg)
2808 {
2809 case AT_SIGNATURE:
2810 case CALG_RSA_SIGN:
2811 TRACE("installing signing key\n");
2812 release_and_install_key(hProv, *phKey, &pKeyContainer->hSignatureKeyPair,
2813 fStoreKey);
2814 break;
2815 case AT_KEYEXCHANGE:
2816 case CALG_RSA_KEYX:
2817 TRACE("installing key exchange key\n");
2818 release_and_install_key(hProv, *phKey, &pKeyContainer->hKeyExchangeKeyPair,
2819 fStoreKey);
2820 break;
2821 }
2822 }
2823 return ret;
2824 }
2825
2826 /******************************************************************************
2827 * import_public_key [Internal]
2828 *
2829 * Import a BLOB'ed public key into a key container.
2830 *
2831 * PARAMS
2832 * hProv [I] Key container into which the public key is to be imported.
2833 * pbData [I] Pointer to a buffer which holds the public key BLOB.
2834 * dwDataLen [I] Length of data in buffer at pbData.
2835 * dwFlags [I] One of:
2836 * CRYPT_EXPORTABLE: the imported key is marked exportable
2837 * fStoreKey [I] If TRUE, the imported key is stored to the registry.
2838 * phKey [O] Handle to the imported key.
2839 *
2840 *
2841 * NOTES
2842 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2843 * it's a PUBLICKEYBLOB.
2844 *
2845 * RETURNS
2846 * Success: TRUE.
2847 * Failure: FALSE.
2848 */
2849 static BOOL import_public_key(HCRYPTPROV hProv, const BYTE *pbData, DWORD dwDataLen,
2850 DWORD dwFlags, BOOL fStoreKey, HCRYPTKEY *phKey)
2851 {
2852 KEYCONTAINER *pKeyContainer;
2853 CRYPTKEY *pCryptKey;
2854 const BLOBHEADER *pBlobHeader = (const BLOBHEADER*)pbData;
2855 const RSAPUBKEY *pRSAPubKey = (const RSAPUBKEY*)(pBlobHeader+1);
2856 ALG_ID algID;
2857 BOOL ret;
2858
2859 if (dwFlags & CRYPT_IPSEC_HMAC_KEY)
2860 {
2861 FIXME("unimplemented for CRYPT_IPSEC_HMAC_KEY\n");
2862 SetLastError(NTE_BAD_FLAGS);
2863 return FALSE;
2864 }
2865 if (!(pKeyContainer = get_key_container(hProv)))
2866 return FALSE;
2867
2868 if ((dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY)) ||
2869 (pRSAPubKey->magic != RSAENH_MAGIC_RSA1) ||
2870 (dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) + (pRSAPubKey->bitlen >> 3)))
2871 {
2872 SetLastError(NTE_BAD_DATA);
2873 return FALSE;
2874 }
2875
2876 /* Since this is a public key blob, only the public key is
2877 * available, so only signature verification is possible.
2878 */
2879 algID = pBlobHeader->aiKeyAlg;
2880 *phKey = new_key(hProv, algID, MAKELONG(0,pRSAPubKey->bitlen), &pCryptKey);
2881 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) return FALSE;
2882 setup_key(pCryptKey);
2883 ret = import_public_key_impl((const BYTE*)(pRSAPubKey+1), &pCryptKey->context,
2884 pRSAPubKey->bitlen >> 3, pRSAPubKey->pubexp);
2885 if (ret) {
2886 if (dwFlags & CRYPT_EXPORTABLE)
2887 pCryptKey->dwPermissions |= CRYPT_EXPORT;
2888 switch (pBlobHeader->aiKeyAlg)
2889 {
2890 case AT_KEYEXCHANGE:
2891 case CALG_RSA_KEYX:
2892 TRACE("installing public key\n");
2893 release_and_install_key(hProv, *phKey, &pKeyContainer->hKeyExchangeKeyPair,
2894 fStoreKey);
2895 break;
2896 }
2897 }
2898 return ret;
2899 }
2900
2901 /******************************************************************************
2902 * import_symmetric_key [Internal]
2903 *
2904 * Import a BLOB'ed symmetric key into a key container.
2905 *
2906 * PARAMS
2907 * hProv [I] Key container into which the symmetric key is to be imported.
2908 * pbData [I] Pointer to a buffer which holds the symmetric key BLOB.
2909 * dwDataLen [I] Length of data in buffer at pbData.
2910 * hPubKey [I] Key used to decrypt sensitive BLOB data.
2911 * dwFlags [I] One of:
2912 * CRYPT_EXPORTABLE: the imported key is marked exportable
2913 * phKey [O] Handle to the imported key.
2914 *
2915 *
2916 * NOTES
2917 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2918 * it's a SIMPLEBLOB.
2919 *
2920 * RETURNS
2921 * Success: TRUE.
2922 * Failure: FALSE.
2923 */
2924 static BOOL import_symmetric_key(HCRYPTPROV hProv, const BYTE *pbData, DWORD dwDataLen,
2925 HCRYPTKEY hPubKey, DWORD dwFlags, HCRYPTKEY *phKey)
2926 {
2927 CRYPTKEY *pCryptKey, *pPubKey;
2928 const BLOBHEADER *pBlobHeader = (const BLOBHEADER*)pbData;
2929 const ALG_ID *pAlgid = (const ALG_ID*)(pBlobHeader+1);
2930 const BYTE *pbKeyStream = (const BYTE*)(pAlgid + 1);
2931 BYTE *pbDecrypted;
2932 DWORD dwKeyLen;
2933
2934 if (dwFlags & CRYPT_IPSEC_HMAC_KEY)
2935 {
2936 FIXME("unimplemented for CRYPT_IPSEC_HMAC_KEY\n");
2937 SetLastError(NTE_BAD_FLAGS);
2938 return FALSE;
2939 }
2940 if (!lookup_handle(&handle_table, hPubKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pPubKey) ||
2941 pPubKey->aiAlgid != CALG_RSA_KEYX)
2942 {
2943 SetLastError(NTE_BAD_PUBLIC_KEY); /* FIXME: error code? */
2944 return FALSE;
2945 }
2946
2947 if (dwDataLen < sizeof(BLOBHEADER)+sizeof(ALG_ID)+pPubKey->dwBlockLen)
2948 {
2949 SetLastError(NTE_BAD_DATA); /* FIXME: error code */
2950 return FALSE;
2951 }
2952
2953 pbDecrypted = HeapAlloc(GetProcessHeap(), 0, pPubKey->dwBlockLen);
2954 if (!pbDecrypted) return FALSE;
2955 encrypt_block_impl(pPubKey->aiAlgid, PK_PRIVATE, &pPubKey->context, pbKeyStream, pbDecrypted,
2956 RSAENH_DECRYPT);
2957
2958 dwKeyLen = RSAENH_MAX_KEY_SIZE;
2959 if (!unpad_data(pbDecrypted, pPubKey->dwBlockLen, pbDecrypted, &dwKeyLen, dwFlags)) {
2960 HeapFree(GetProcessHeap(), 0, pbDecrypted);
2961 return FALSE;
2962 }
2963
2964 *phKey = new_key(hProv, pBlobHeader->aiKeyAlg, dwKeyLen<<19, &pCryptKey);
2965 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE)
2966 {
2967 HeapFree(GetProcessHeap(), 0, pbDecrypted);
2968 return FALSE;
2969 }
2970 memcpy(pCryptKey->abKeyValue, pbDecrypted, dwKeyLen);
2971 HeapFree(GetProcessHeap(), 0, pbDecrypted);
2972 setup_key(pCryptKey);
2973 if (dwFlags & CRYPT_EXPORTABLE)
2974 pCryptKey->dwPermissions |= CRYPT_EXPORT;
2975 return TRUE;
2976 }
2977
2978 /******************************************************************************
2979 * import_plaintext_key [Internal]
2980 *
2981 * Import a plaintext key into a key container.
2982 *
2983 * PARAMS
2984 * hProv [I] Key container into which the symmetric key is to be imported.
2985 * pbData [I] Pointer to a buffer which holds the plaintext key BLOB.
2986 * dwDataLen [I] Length of data in buffer at pbData.
2987 * dwFlags [I] One of:
2988 * CRYPT_EXPORTABLE: the imported key is marked exportable
2989 * phKey [O] Handle to the imported key.
2990 *
2991 *
2992 * NOTES
2993 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2994 * it's a PLAINTEXTKEYBLOB.
2995 *
2996 * RETURNS
2997 * Success: TRUE.
2998 * Failure: FALSE.
2999 */
3000 static BOOL import_plaintext_key(HCRYPTPROV hProv, const BYTE *pbData, DWORD dwDataLen,
3001 DWORD dwFlags, HCRYPTKEY *phKey)
3002 {
3003 CRYPTKEY *pCryptKey;
3004 const BLOBHEADER *pBlobHeader = (const BLOBHEADER*)pbData;
3005 const DWORD *pKeyLen = (const DWORD *)(pBlobHeader + 1);
3006 const BYTE *pbKeyStream = (const BYTE*)(pKeyLen + 1);
3007
3008 if (dwDataLen < sizeof(BLOBHEADER)+sizeof(DWORD)+*pKeyLen)
3009 {
3010 SetLastError(NTE_BAD_DATA); /* FIXME: error code */
3011 return FALSE;
3012 }
3013
3014 if (dwFlags & CRYPT_IPSEC_HMAC_KEY)
3015 {
3016 *phKey = new_key(hProv, CALG_HMAC, 0, &pCryptKey);
3017 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE)
3018 return FALSE;
3019 if (*pKeyLen <= RSAENH_MIN(sizeof(pCryptKey->abKeyValue), RSAENH_HMAC_BLOCK_LEN))
3020 {
3021 memcpy(pCryptKey->abKeyValue, pbKeyStream, *pKeyLen);
3022 pCryptKey->dwKeyLen = *pKeyLen;
3023 }
3024 else
3025 {
3026 CRYPT_DATA_BLOB blobHmacKey = { *pKeyLen, (BYTE *)pbKeyStream };
3027
3028 /* In order to initialize an HMAC key, the key material is hashed,
3029 * and the output of the hash function is used as the key material.
3030 * Unfortunately, the way the Crypto API is designed, we don't know
3031 * the hash algorithm yet, so we have to copy the entire key
3032 * material.
3033 */
3034 if (!copy_data_blob(&pCryptKey->blobHmacKey, &blobHmacKey))
3035 {
3036 release_handle(&handle_table, *phKey, RSAENH_MAGIC_KEY);
3037 *phKey = (HCRYPTKEY)INVALID_HANDLE_VALUE;
3038 return FALSE;
3039 }
3040 }
3041 setup_key(pCryptKey);
3042 if (dwFlags & CRYPT_EXPORTABLE)
3043 pCryptKey->dwPermissions |= CRYPT_EXPORT;
3044 }
3045 else
3046 {
3047 *phKey = new_key(hProv, pBlobHeader->aiKeyAlg, *pKeyLen<<19, &pCryptKey);
3048 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE)
3049 return FALSE;
3050 memcpy(pCryptKey->abKeyValue, pbKeyStream, *pKeyLen);
3051 setup_key(pCryptKey);
3052 if (dwFlags & CRYPT_EXPORTABLE)
3053 pCryptKey->dwPermissions |= CRYPT_EXPORT;
3054 }
3055 return TRUE;
3056 }
3057
3058 /******************************************************************************
3059 * import_key [Internal]
3060 *
3061 * Import a BLOB'ed key into a key container, optionally storing the key's
3062 * value to the registry.
3063 *
3064 * PARAMS
3065 * hProv [I] Key container into which the key is to be imported.
3066 * pbData [I] Pointer to a buffer which holds the BLOB.
3067 * dwDataLen [I] Length of data in buffer at pbData.
3068 * hPubKey [I] Key used to decrypt sensitive BLOB data.
3069 * dwFlags [I] One of:
3070 * CRYPT_EXPORTABLE: the imported key is marked exportable
3071 * fStoreKey [I] If TRUE, the imported key is stored to the registry.
3072 * phKey [O] Handle to the imported key.
3073 *
3074 * RETURNS
3075 * Success: TRUE.
3076 * Failure: FALSE.
3077 */
3078 static BOOL import_key(HCRYPTPROV hProv, const BYTE *pbData, DWORD dwDataLen, HCRYPTKEY hPubKey,
3079 DWORD dwFlags, BOOL fStoreKey, HCRYPTKEY *phKey)
3080 {
3081 KEYCONTAINER *pKeyContainer;
3082 const BLOBHEADER *pBlobHeader = (const BLOBHEADER*)pbData;
3083
3084 if (!(pKeyContainer = get_key_container(hProv)))
3085 return FALSE;
3086
3087 if (dwDataLen < sizeof(BLOBHEADER) ||
3088 pBlobHeader->bVersion != CUR_BLOB_VERSION ||
3089 pBlobHeader->reserved != 0)
3090 {
3091 TRACE("bVersion = %d, reserved = %d\n", pBlobHeader->bVersion,
3092 pBlobHeader->reserved);
3093 SetLastError(NTE_BAD_DATA);
3094 return FALSE;
3095 }
3096
3097 /* If this is a verify-only context, the key is not persisted regardless of
3098 * fStoreKey's original value.
3099 */
3100 fStoreKey = fStoreKey && !(dwFlags & CRYPT_VERIFYCONTEXT);
3101 TRACE("blob type: %x\n", pBlobHeader->bType);
3102 switch (pBlobHeader->bType)
3103 {
3104 case PRIVATEKEYBLOB:
3105 return import_private_key(hProv, pbData, dwDataLen, dwFlags,
3106 fStoreKey, phKey);
3107
3108 case PUBLICKEYBLOB:
3109 return import_public_key(hProv, pbData, dwDataLen, dwFlags,
3110 fStoreKey, phKey);
3111
3112 case SIMPLEBLOB:
3113 return import_symmetric_key(hProv, pbData, dwDataLen, hPubKey,
3114 dwFlags, phKey);
3115
3116 case PLAINTEXTKEYBLOB:
3117 return import_plaintext_key(hProv, pbData, dwDataLen, dwFlags,
3118 phKey);
3119
3120 default:
3121 SetLastError(NTE_BAD_TYPE); /* FIXME: error code? */
3122 return FALSE;
3123 }
3124 }
3125
3126 /******************************************************************************
3127 * CPImportKey (RSAENH.@)
3128 *
3129 * Import a BLOB'ed key into a key container.
3130 *
3131 * PARAMS
3132 * hProv [I] Key container into which the key is to be imported.
3133 * pbData [I] Pointer to a buffer which holds the BLOB.
3134 * dwDataLen [I] Length of data in buffer at pbData.
3135 * hPubKey [I] Key used to decrypt sensitive BLOB data.
3136 * dwFlags [I] One of:
3137 * CRYPT_EXPORTABLE: the imported key is marked exportable
3138 * phKey [O] Handle to the imported key.
3139 *
3140 * RETURNS
3141 * Success: TRUE.
3142 * Failure: FALSE.
3143 */
3144 BOOL WINAPI RSAENH_CPImportKey(HCRYPTPROV hProv, const BYTE *pbData, DWORD dwDataLen,
3145 HCRYPTKEY hPubKey, DWORD dwFlags, HCRYPTKEY *phKey)
3146 {
3147 TRACE("(hProv=%08lx, pbData=%p, dwDataLen=%d, hPubKey=%08lx, dwFlags=%08x, phKey=%p)\n",
3148 hProv, pbData, dwDataLen, hPubKey, dwFlags, phKey);
3149
3150 return import_key(hProv, pbData, dwDataLen, hPubKey, dwFlags, TRUE, phKey);
3151 }
3152
3153 /******************************************************************************
3154 * CPGenKey (RSAENH.@)
3155 *
3156 * Generate a key in the key container
3157 *
3158 * PARAMS
3159 * hProv [I] Key container for which a key is to be generated.
3160 * Algid [I] Crypto algorithm identifier for the key to be generated.
3161 * dwFlags [I] Upper 16 bits: Binary length of key. Lower 16 bits: Flags. See Notes
3162 * phKey [O] Handle to the generated key.
3163 *
3164 * RETURNS