bb28c999ae60c26a4c923d2be85bdd7d295e6505
[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.
2830 *
2831 * PARAMS
2832 * hProv [I] A CSP.
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 * phKey [O] Handle to the imported key.
2838 *
2839 *
2840 * NOTES
2841 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2842 * it's a PUBLICKEYBLOB.
2843 *
2844 * RETURNS
2845 * Success: TRUE.
2846 * Failure: FALSE.
2847 */
2848 static BOOL import_public_key(HCRYPTPROV hProv, const BYTE *pbData, DWORD dwDataLen,
2849 DWORD dwFlags, HCRYPTKEY *phKey)
2850 {
2851 CRYPTKEY *pCryptKey;
2852 const BLOBHEADER *pBlobHeader = (const BLOBHEADER*)pbData;
2853 const RSAPUBKEY *pRSAPubKey = (const RSAPUBKEY*)(pBlobHeader+1);
2854 ALG_ID algID;
2855 BOOL ret;
2856
2857 if (dwFlags & CRYPT_IPSEC_HMAC_KEY)
2858 {
2859 FIXME("unimplemented for CRYPT_IPSEC_HMAC_KEY\n");
2860 SetLastError(NTE_BAD_FLAGS);
2861 return FALSE;
2862 }
2863
2864 if ((dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY)) ||
2865 (pRSAPubKey->magic != RSAENH_MAGIC_RSA1) ||
2866 (dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) + (pRSAPubKey->bitlen >> 3)))
2867 {
2868 SetLastError(NTE_BAD_DATA);
2869 return FALSE;
2870 }
2871
2872 /* Since this is a public key blob, only the public key is
2873 * available, so only signature verification is possible.
2874 */
2875 algID = pBlobHeader->aiKeyAlg;
2876 *phKey = new_key(hProv, algID, MAKELONG(0,pRSAPubKey->bitlen), &pCryptKey);
2877 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) return FALSE;
2878 setup_key(pCryptKey);
2879 ret = import_public_key_impl((const BYTE*)(pRSAPubKey+1), &pCryptKey->context,
2880 pRSAPubKey->bitlen >> 3, pRSAPubKey->pubexp);
2881 if (ret) {
2882 if (dwFlags & CRYPT_EXPORTABLE)
2883 pCryptKey->dwPermissions |= CRYPT_EXPORT;
2884 }
2885 return ret;
2886 }
2887
2888 /******************************************************************************
2889 * import_symmetric_key [Internal]
2890 *
2891 * Import a BLOB'ed symmetric key into a key container.
2892 *
2893 * PARAMS
2894 * hProv [I] Key container into which the symmetric key is to be imported.
2895 * pbData [I] Pointer to a buffer which holds the symmetric key BLOB.
2896 * dwDataLen [I] Length of data in buffer at pbData.
2897 * hPubKey [I] Key used to decrypt sensitive BLOB data.
2898 * dwFlags [I] One of:
2899 * CRYPT_EXPORTABLE: the imported key is marked exportable
2900 * phKey [O] Handle to the imported key.
2901 *
2902 *
2903 * NOTES
2904 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2905 * it's a SIMPLEBLOB.
2906 *
2907 * RETURNS
2908 * Success: TRUE.
2909 * Failure: FALSE.
2910 */
2911 static BOOL import_symmetric_key(HCRYPTPROV hProv, const BYTE *pbData, DWORD dwDataLen,
2912 HCRYPTKEY hPubKey, DWORD dwFlags, HCRYPTKEY *phKey)
2913 {
2914 CRYPTKEY *pCryptKey, *pPubKey;
2915 const BLOBHEADER *pBlobHeader = (const BLOBHEADER*)pbData;
2916 const ALG_ID *pAlgid = (const ALG_ID*)(pBlobHeader+1);
2917 const BYTE *pbKeyStream = (const BYTE*)(pAlgid + 1);
2918 BYTE *pbDecrypted;
2919 DWORD dwKeyLen;
2920
2921 if (dwFlags & CRYPT_IPSEC_HMAC_KEY)
2922 {
2923 FIXME("unimplemented for CRYPT_IPSEC_HMAC_KEY\n");
2924 SetLastError(NTE_BAD_FLAGS);
2925 return FALSE;
2926 }
2927 if (!lookup_handle(&handle_table, hPubKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pPubKey) ||
2928 pPubKey->aiAlgid != CALG_RSA_KEYX)
2929 {
2930 SetLastError(NTE_BAD_PUBLIC_KEY); /* FIXME: error code? */
2931 return FALSE;
2932 }
2933
2934 if (dwDataLen < sizeof(BLOBHEADER)+sizeof(ALG_ID)+pPubKey->dwBlockLen)
2935 {
2936 SetLastError(NTE_BAD_DATA); /* FIXME: error code */
2937 return FALSE;
2938 }
2939
2940 pbDecrypted = HeapAlloc(GetProcessHeap(), 0, pPubKey->dwBlockLen);
2941 if (!pbDecrypted) return FALSE;
2942 encrypt_block_impl(pPubKey->aiAlgid, PK_PRIVATE, &pPubKey->context, pbKeyStream, pbDecrypted,
2943 RSAENH_DECRYPT);
2944
2945 dwKeyLen = RSAENH_MAX_KEY_SIZE;
2946 if (!unpad_data(pbDecrypted, pPubKey->dwBlockLen, pbDecrypted, &dwKeyLen, dwFlags)) {
2947 HeapFree(GetProcessHeap(), 0, pbDecrypted);
2948 return FALSE;
2949 }
2950
2951 *phKey = new_key(hProv, pBlobHeader->aiKeyAlg, dwKeyLen<<19, &pCryptKey);
2952 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE)
2953 {
2954 HeapFree(GetProcessHeap(), 0, pbDecrypted);
2955 return FALSE;
2956 }
2957 memcpy(pCryptKey->abKeyValue, pbDecrypted, dwKeyLen);
2958 HeapFree(GetProcessHeap(), 0, pbDecrypted);
2959 setup_key(pCryptKey);
2960 if (dwFlags & CRYPT_EXPORTABLE)
2961 pCryptKey->dwPermissions |= CRYPT_EXPORT;
2962 return TRUE;
2963 }
2964
2965 /******************************************************************************
2966 * import_plaintext_key [Internal]
2967 *
2968 * Import a plaintext key into a key container.
2969 *
2970 * PARAMS
2971 * hProv [I] Key container into which the symmetric key is to be imported.
2972 * pbData [I] Pointer to a buffer which holds the plaintext key BLOB.
2973 * dwDataLen [I] Length of data in buffer at pbData.
2974 * dwFlags [I] One of:
2975 * CRYPT_EXPORTABLE: the imported key is marked exportable
2976 * phKey [O] Handle to the imported key.
2977 *
2978 *
2979 * NOTES
2980 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2981 * it's a PLAINTEXTKEYBLOB.
2982 *
2983 * RETURNS
2984 * Success: TRUE.
2985 * Failure: FALSE.
2986 */
2987 static BOOL import_plaintext_key(HCRYPTPROV hProv, const BYTE *pbData, DWORD dwDataLen,
2988 DWORD dwFlags, HCRYPTKEY *phKey)
2989 {
2990 CRYPTKEY *pCryptKey;
2991 const BLOBHEADER *pBlobHeader = (const BLOBHEADER*)pbData;
2992 const DWORD *pKeyLen = (const DWORD *)(pBlobHeader + 1);
2993 const BYTE *pbKeyStream = (const BYTE*)(pKeyLen + 1);
2994
2995 if (dwDataLen < sizeof(BLOBHEADER)+sizeof(DWORD)+*pKeyLen)
2996 {
2997 SetLastError(NTE_BAD_DATA); /* FIXME: error code */
2998 return FALSE;
2999 }
3000
3001 if (dwFlags & CRYPT_IPSEC_HMAC_KEY)
3002 {
3003 *phKey = new_key(hProv, CALG_HMAC, 0, &pCryptKey);
3004 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE)
3005 return FALSE;
3006 if (*pKeyLen <= RSAENH_MIN(sizeof(pCryptKey->abKeyValue), RSAENH_HMAC_BLOCK_LEN))
3007 {
3008 memcpy(pCryptKey->abKeyValue, pbKeyStream, *pKeyLen);
3009 pCryptKey->dwKeyLen = *pKeyLen;
3010 }
3011 else
3012 {
3013 CRYPT_DATA_BLOB blobHmacKey = { *pKeyLen, (BYTE *)pbKeyStream };
3014
3015 /* In order to initialize an HMAC key, the key material is hashed,
3016 * and the output of the hash function is used as the key material.
3017 * Unfortunately, the way the Crypto API is designed, we don't know
3018 * the hash algorithm yet, so we have to copy the entire key
3019 * material.
3020 */
3021 if (!copy_data_blob(&pCryptKey->blobHmacKey, &blobHmacKey))
3022 {
3023 release_handle(&handle_table, *phKey, RSAENH_MAGIC_KEY);
3024 *phKey = (HCRYPTKEY)INVALID_HANDLE_VALUE;
3025 return FALSE;
3026 }
3027 }
3028 setup_key(pCryptKey);
3029 if (dwFlags & CRYPT_EXPORTABLE)
3030 pCryptKey->dwPermissions |= CRYPT_EXPORT;
3031 }
3032 else
3033 {
3034 *phKey = new_key(hProv, pBlobHeader->aiKeyAlg, *pKeyLen<<19, &pCryptKey);
3035 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE)
3036 return FALSE;
3037 memcpy(pCryptKey->abKeyValue, pbKeyStream, *pKeyLen);
3038 setup_key(pCryptKey);
3039 if (dwFlags & CRYPT_EXPORTABLE)
3040 pCryptKey->dwPermissions |= CRYPT_EXPORT;
3041 }
3042 return TRUE;
3043 }
3044
3045 /******************************************************************************
3046 * import_key [Internal]
3047 *
3048 * Import a BLOB'ed key into a key container, optionally storing the key's
3049 * value to the registry.
3050 *
3051 * PARAMS
3052 * hProv [I] Key container into which the key is to be imported.
3053 * pbData [I] Pointer to a buffer which holds the BLOB.
3054 * dwDataLen [I] Length of data in buffer at pbData.
3055 * hPubKey [I] Key used to decrypt sensitive BLOB data.
3056 * dwFlags [I] One of:
3057 * CRYPT_EXPORTABLE: the imported key is marked exportable
3058 * fStoreKey [I] If TRUE, the imported key is stored to the registry.
3059 * phKey [O] Handle to the imported key.
3060 *
3061 * RETURNS
3062 * Success: TRUE.
3063 * Failure: FALSE.
3064 */
3065 static BOOL import_key(HCRYPTPROV hProv, const BYTE *pbData, DWORD dwDataLen, HCRYPTKEY hPubKey,
3066 DWORD dwFlags, BOOL fStoreKey, HCRYPTKEY *phKey)
3067 {
3068 KEYCONTAINER *pKeyContainer;
3069 const BLOBHEADER *pBlobHeader = (const BLOBHEADER*)pbData;
3070
3071 if (!(pKeyContainer = get_key_container(hProv)))
3072 return FALSE;
3073
3074 if (dwDataLen < sizeof(BLOBHEADER) ||
3075 pBlobHeader->bVersion != CUR_BLOB_VERSION ||
3076 pBlobHeader->reserved != 0)
3077 {
3078 TRACE("bVersion = %d, reserved = %d\n", pBlobHeader->bVersion,
3079 pBlobHeader->reserved);
3080 SetLastError(NTE_BAD_DATA);
3081 return FALSE;
3082 }
3083
3084 /* If this is a verify-only context, the key is not persisted regardless of
3085 * fStoreKey's original value.
3086 */
3087 fStoreKey = fStoreKey && !(dwFlags & CRYPT_VERIFYCONTEXT);
3088 TRACE("blob type: %x\n", pBlobHeader->bType);
3089 switch (pBlobHeader->bType)
3090 {
3091 case PRIVATEKEYBLOB:
3092 return import_private_key(hProv, pbData, dwDataLen, dwFlags,
3093 fStoreKey, phKey);
3094
3095 case PUBLICKEYBLOB:
3096 return import_public_key(hProv, pbData, dwDataLen, dwFlags,
3097 phKey);
3098
3099 case SIMPLEBLOB:
3100 return import_symmetric_key(hProv, pbData, dwDataLen, hPubKey,
3101 dwFlags, phKey);
3102
3103 case PLAINTEXTKEYBLOB:
3104 return import_plaintext_key(hProv, pbData, dwDataLen, dwFlags,
3105 phKey);
3106
3107 default:
3108 SetLastError(NTE_BAD_TYPE); /* FIXME: error code? */
3109 return FALSE;
3110 }
3111 }
3112
3113 /******************************************************************************
3114 * CPImportKey (RSAENH.@)
3115 *
3116 * Import a BLOB'ed key into a key container.
3117 *
3118 * PARAMS
3119 * hProv [I] Key container into which the key is to be imported.
3120 * pbData [I] Pointer to a buffer which holds the BLOB.
3121 * dwDataLen [I] Length of data in buffer at pbData.
3122 * hPubKey [I] Key used to decrypt sensitive BLOB data.
3123 * dwFlags [I] One of:
3124 * CRYPT_EXPORTABLE: the imported key is marked exportable
3125 * phKey [O] Handle to the imported key.
3126 *
3127 * RETURNS
3128 * Success: TRUE.
3129 * Failure: FALSE.
3130 */
3131 BOOL WINAPI RSAENH_CPImportKey(HCRYPTPROV hProv, const BYTE *pbData, DWORD dwDataLen,
3132 HCRYPTKEY hPubKey, DWORD dwFlags, HCRYPTKEY *phKey)
3133 {
3134 TRACE("(hProv=%08lx, pbData=%p, dwDataLen=%d, hPubKey=%08lx, dwFlags=%08x, phKey=%p)\n",
3135 hProv, pbData, dwDataLen, hPubKey, dwFlags, phKey);
3136
3137 return import_key(hProv, pbData, dwDataLen, hPubKey, dwFlags, TRUE, phKey);
3138 }
3139
3140 /******************************************************************************
3141 * CPGenKey (RSAENH.@)
3142 *
3143 * Generate a key in the key container
3144 *
3145 * PARAMS
3146 * hProv [I] Key container for which a key is to be generated.
3147 * Algid [I] Crypto algorithm identifier for the key to be generated.
3148 * dwFlags [I] Upper 16 bits: Binary length of key. Lower 16 bits: Flags. See Notes
3149 * phKey [O] Handle to the generated key.
3150 *
3151 * RETURNS
3152 * Success: TRUE.
3153 * Failure: FALSE.
3154 *
3155 * FIXME
3156 * Flags currently not considered.
3157 *
3158 * NOTES
3159 * Private key-exchange- and signature-keys can be generated with Algid AT_KEYEXCHANGE
3160 * and AT_SIGNATURE values.
3161 */
3162 BOOL WINAPI RSAENH_CPGenKey(HCRYPTPROV hProv, ALG_ID Algid, DWORD dwFlags, HCRYPTKEY *phKey)
3163 {
3164 KEYCONTAINER *pKeyContainer;
3165 CRYPTKEY *pCryptKey;
3166
3167 TRACE("(hProv=%08lx, aiAlgid=%d, dwFlags=%08x, phKey=%p)\n", hProv, Algid, dwFlags, phKey);
3168
3169 if (!(pKeyContainer = get_key_container(hProv)))
3170 {
3171 /* MSDN: hProv not containing valid context handle */
3172 return FALSE;
3173 }
3174
3175 switch (Algid)
3176 {
3177 case AT_SIGNATURE:
3178 case CALG_RSA_SIGN:
3179 *phKey = new_key(hProv, CALG_RSA_SIGN, dwFlags, &pCryptKey);
3180 if (pCryptKey) {
3181 new_key_impl(pCryptKey->aiAlgid, &pCryptKey->context, pCryptKey->dwKeyLen);
3182 setup_key(pCryptKey);
3183 release_and_install_key(hProv, *phKey,
3184 &pKeyContainer->hSignatureKeyPair,
3185 FALSE);
3186 }
3187 break;
3188
3189 case AT_KEYEXCHANGE:
3190 case CALG_RSA_KEYX:
3191 *phKey = new_key(hProv, CALG_RSA_KEYX, dwFlags, &pCryptKey);
3192 if (pCryptKey) {
3193 new_key_impl(pCryptKey->aiAlgid, &pCryptKey->context, pCryptKey->dwKeyLen);
3194 setup_key(pCryptKey);
3195 release_and_install_key(hProv, *phKey,
3196 &pKeyContainer->hKeyExchangeKeyPair,
3197 FALSE);
3198 }
3199 break;
3200
3201 case CALG_RC2:
3202 case CALG_RC4:
3203 case CALG_DES:
3204 case CALG_3DES_112:
3205 case CALG_3DES:
3206 case CALG_AES:
3207 case CALG_AES_128:
3208 case CALG_AES_192:
3209 case CALG_AES_256:
3210 case CALG_PCT1_MASTER:
3211 case CALG_SSL2_MASTER:
3212 case CALG_SSL3_MASTER:
3213 case CALG_TLS1_MASTER:
3214 *phKey = new_key(hProv, Algid, dwFlags, &pCryptKey);
3215 if (pCryptKey) {
3216 gen_rand_impl(pCryptKey->abKeyValue, RSAENH_MAX_KEY_SIZE);
3217 switch (Algid) {
3218 case CALG_SSL3_MASTER:
3219 pCryptKey->abKeyValue[0] = RSAENH_SSL3_VERSION_MAJOR;
3220 pCryptKey->abKeyValue[1] = RSAENH_SSL3_VERSION_MINOR;
3221 break;
3222
3223 case CALG_TLS1_MASTER:
3224 pCryptKey->abKeyValue[0] = RSAENH_TLS1_VERSION_MAJOR;
3225 pCryptKey->abKeyValue[1] = RSAENH_TLS1_VERSION_MINOR;
3226 break;
3227 }
3228 setup_key(pCryptKey);
3229 }
3230 break;
3231
3232 default:
3233 /* MSDN: Algorithm not supported specified by Algid */
3234 SetLastError(NTE_BAD_ALGID);
3235 return FALSE;
3236 }
3237
3238 return *phKey != (HCRYPTKEY)INVALID_HANDLE_VALUE;
3239 }
3240
3241 /******************************************************************************
3242 * CPGenRandom (RSAENH.@)
3243 *
3244 * Generate a random byte stream.
3245 *
3246 * PARAMS
3247 * hProv [I] Key container that is used to generate random bytes.
3248 * dwLen [I] Specifies the number of requested random data bytes.
3249 * pbBuffer [O] Random bytes will be stored here.
3250 *
3251 * RETURNS
3252 * Success: TRUE
3253 * Failure: FALSE
3254 */
3255 BOOL WINAPI RSAENH_CPGenRandom(HCRYPTPROV hProv, DWORD dwLen, BYTE *pbBuffer)
3256 {
3257 TRACE("(hProv=%08lx, dwLen=%d, pbBuffer=%p)\n", hProv, dwLen, pbBuffer);
3258
3259 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
3260 {
3261 /* MSDN: hProv not containing valid context handle */
3262 SetLastError(NTE_BAD_UID);
3263 return FALSE;
3264 }
3265
3266 return gen_rand_impl(pbBuffer, dwLen);
3267 }
3268
3269 /******************************************************************************
3270 * CPGetHashParam (RSAENH.@)
3271 *
3272 * Query parameters of an hash object.
3273 *
3274 * PARAMS
3275 * hProv [I] The kea container, which the hash belongs to.
3276 * hHash [I] The hash object that is to be queried.
3277 * dwParam [I] Specifies the parameter that is to be queried.
3278 * pbData [I] Pointer to the buffer where the parameter value will be stored.
3279 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
3280 * dwFlags [I] None currently defined.
3281 *
3282 * RETURNS
3283 * Success: TRUE
3284 * Failure: FALSE
3285 *
3286 * NOTES
3287 * Valid dwParams are: HP_ALGID, HP_HASHSIZE, HP_HASHVALUE. The hash will be
3288 * finalized if HP_HASHVALUE is queried.
3289 */
3290 BOOL WINAPI RSAENH_CPGetHashParam(HCRYPTPROV hProv, HCRYPTHASH hHash, DWORD dwParam, BYTE *pbData,
3291 DWORD *pdwDataLen, DWORD dwFlags)
3292 {
3293 CRYPTHASH *pCryptHash;
3294
3295 TRACE("(hProv=%08lx, hHash=%08lx, dwParam=%08x, pbData=%p, pdwDataLen=%p, dwFlags=%08x)\n",
3296 hProv, hHash, dwParam, pbData, pdwDataLen, dwFlags);
3297
3298 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
3299 {
3300 SetLastError(NTE_BAD_UID);
3301 return FALSE;
3302 }
3303
3304 if (dwFlags)
3305 {
3306 SetLastError(NTE_BAD_FLAGS);
3307 return FALSE;
3308 }
3309
3310 if (!lookup_handle(&handle_table, hHash, RSAENH_MAGIC_HASH,
3311 (OBJECTHDR**)&pCryptHash))
3312 {
3313 SetLastError(NTE_BAD_HASH);
3314 return FALSE;
3315 }
3316
3317 if (!pdwDataLen)
3318 {
3319 SetLastError(ERROR_INVALID_PARAMETER);
3320 return FALSE;
3321 }
3322
3323 switch (dwParam)
3324 {
3325 case HP_ALGID:
3326 return copy_param(pbData, pdwDataLen, (const BYTE*)&pCryptHash->aiAlgid,
3327 sizeof(ALG_ID));
3328
3329 case HP_HASHSIZE:
3330 return copy_param(pbData, pdwDataLen, (const BYTE*)&pCryptHash->dwHashSize,
3331 sizeof(DWORD));
3332
3333 case HP_HASHVAL:
3334 if (pCryptHash->aiAlgid == CALG_TLS1PRF) {
3335 return tls1_prf(hProv, pCryptHash->hKey, &pCryptHash->tpPRFParams.blobLabel,
3336 &pCryptHash->tpPRFParams.blobSeed, pbData, *pdwDataLen);
3337 }
3338
3339 if ( pbData == NULL ) {
3340 *pdwDataLen = pCryptHash->dwHashSize;
3341 return TRUE;
3342 }
3343
3344 if (pbData && (pCryptHash->dwState != RSAENH_HASHSTATE_FINISHED))
3345 {
3346 finalize_hash(pCryptHash);
3347 pCryptHash->dwState = RSAENH_HASHSTATE_FINISHED;
3348 }
3349
3350 return copy_param(pbData, pdwDataLen, pCryptHash->abHashValue,
3351 pCryptHash->dwHashSize);
3352
3353 default:
3354 SetLastError(NTE_BAD_TYPE);
3355 return FALSE;
3356 }
3357 }
3358
3359 /******************************************************************************
3360 * CPSetKeyParam (RSAENH.@)
3361 *
3362 * Set a parameter of a key object
3363 *
3364 * PARAMS
3365 * hProv [I] The key container to which the key belongs.
3366 * hKey [I] The key for which a parameter is to be set.
3367 * dwParam [I] Parameter type. See Notes.
3368 * pbData [I] Pointer to the parameter value.
3369 * dwFlags [I] Currently none defined.
3370 *
3371 * RETURNS
3372 * Success: TRUE.
3373 * Failure: FALSE.
3374 *
3375 * NOTES:
3376 * Defined dwParam types are:
3377 * - KP_MODE: Values MODE_CBC, MODE_ECB, MODE_CFB.
3378 * - KP_MODE_BITS: Shift width for cipher feedback mode. (Currently ignored by MS CSP's)
3379 * - KP_PERMISSIONS: Or'ed combination of CRYPT_ENCRYPT, CRYPT_DECRYPT,
3380 * CRYPT_EXPORT, CRYPT_READ, CRYPT_WRITE, CRYPT_MAC
3381 * - KP_IV: Initialization vector
3382 */
3383 BOOL WINAPI RSAENH_CPSetKeyParam(HCRYPTPROV hProv, HCRYPTKEY hKey, DWORD dwParam, BYTE *pbData,
3384 DWORD dwFlags)
3385 {
3386 CRYPTKEY *pCryptKey;
3387
3388 TRACE("(hProv=%08lx, hKey=%08lx, dwParam=%08x, pbData=%p, dwFlags=%08x)\n", hProv, hKey,
3389 dwParam, pbData, dwFlags);
3390
3391 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
3392 {
3393 SetLastError(NTE_BAD_UID);
3394 return FALSE;
3395 }
3396
3397 if (dwFlags) {
3398 SetLastError(NTE_BAD_FLAGS);
3399 return FALSE;
3400 }
3401
3402 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
3403 {
3404 SetLastError(NTE_BAD_KEY);
3405 return FALSE;
3406 }
3407
3408 switch (dwParam) {
3409 case KP_PADDING:
3410 /* The MS providers only support PKCS5_PADDING */
3411 if (*(DWORD *)pbData != PKCS5_PADDING) {
3412 SetLastError(NTE_BAD_DATA);
3413 return FALSE;
3414 }
3415 return TRUE;
3416
3417 case KP_MODE:
3418 pCryptKey->dwMode = *(DWORD*)pbData;
3419 return TRUE;
3420
3421 case KP_MODE_BITS:
3422 pCryptKey->dwModeBits = *(DWORD*)pbData;
3423 return TRUE;
3424
3425 case KP_PERMISSIONS:
3426 {
3427 DWORD perms = *(DWORD *)pbData;
3428
3429 if ((perms & CRYPT_EXPORT) &&
3430 !(pCryptKey->dwPermissions & CRYPT_EXPORT))
3431 {
3432 SetLastError(NTE_BAD_DATA);
3433 return FALSE;
3434 }
3435 else if (!(perms & CRYPT_EXPORT) &&
3436 (pCryptKey->dwPermissions & CRYPT_EXPORT))
3437 {
3438 /* Clearing the export permission appears to be ignored,
3439 * see tests.
3440 */
3441 perms |= CRYPT_EXPORT;
3442 }
3443 pCryptKey->dwPermissions = perms;
3444 return TRUE;
3445 }
3446
3447 case KP_IV:
3448 memcpy(pCryptKey->abInitVector, pbData, pCryptKey->dwBlockLen);
3449 setup_key(pCryptKey);
3450 return TRUE;
3451
3452 case KP_SALT:
3453 switch (pCryptKey->aiAlgid) {
3454 case CALG_RC2:
3455 case CALG_RC4:
3456 {
3457 KEYCONTAINER *pKeyContainer = get_key_container(pCryptKey->hProv);
3458 if (!pbData)
3459 {
3460 SetLastError(ERROR_INVALID_PARAMETER);
3461 return FALSE;
3462 }
3463 /* MSDN: the base provider always sets eleven bytes of
3464 * salt value.
3465 */
3466 memcpy(pCryptKey->abKeyValue + pCryptKey->dwKeyLen,
3467 pbData, 11);
3468 pCryptKey->dwSaltLen = 11;
3469 setup_key(pCryptKey);
3470 /* After setting the salt value if the provider is not base or
3471 * strong the salt length will be reset. */
3472 if (pKeyContainer->dwPersonality != RSAENH_PERSONALITY_BASE &&
3473 pKeyContainer->dwPersonality != RSAENH_PERSONALITY_STRONG)
3474 pCryptKey->dwSaltLen = 0;
3475 break;
3476 }
3477 default:
3478 SetLastError(NTE_BAD_KEY);
3479 return FALSE;
3480 }
3481 return TRUE;
3482
3483 case KP_SALT_EX:
3484 {
3485 CRYPT_INTEGER_BLOB *blob = (CRYPT_INTEGER_BLOB *)pbData;
3486
3487 /* salt length can't be greater than 184 bits = 24 bytes */
3488 if (blob->cbData > 24)
3489 {
3490 SetLastError(NTE_BAD_DATA);
3491 return FALSE;
3492 }
3493 memcpy(pCryptKey->abKeyValue + pCryptKey->dwKeyLen, blob->pbData,
3494 blob->cbData);
3495 pCryptKey->dwSaltLen = blob->cbData;
3496 setup_key(pCryptKey);
3497 return TRUE;
3498 }
3499
3500 case KP_EFFECTIVE_KEYLEN:
3501 switch (pCryptKey->aiAlgid) {
3502 case CALG_RC2:
3503 {
3504 DWORD keylen, deflen;
3505 BOOL ret = TRUE;
3506 KEYCONTAINER *pKeyContainer = get_key_container(pCryptKey->hProv);
3507
3508 if (!pbData)
3509 {
3510 SetLastError(ERROR_INVALID_PARAMETER);
3511 return FALSE;
3512 }
3513 keylen = *(DWORD *)pbData;
3514 if (!keylen || keylen > 1024)
3515 {
3516 SetLastError(NTE_BAD_DATA);
3517 return FALSE;
3518 }
3519
3520 /*
3521 * The Base provider will force the key length to default
3522 * and set an error state if a key length different from
3523 * the default is tried.
3524 */
3525 deflen = aProvEnumAlgsEx[pKeyContainer->dwPersonality]->dwDefaultLen;
3526 if (pKeyContainer->dwPersonality == RSAENH_PERSONALITY_BASE
3527 && keylen != deflen)
3528 {
3529 keylen = deflen;
3530 SetLastError(NTE_BAD_DATA);
3531 ret = FALSE;
3532 }
3533 pCryptKey->dwEffectiveKeyLen = keylen;
3534 setup_key(pCryptKey);
3535 return ret;
3536 }
3537 default:
3538 SetLastError(NTE_BAD_TYPE);
3539 return FALSE;
3540 }
3541 return TRUE;
3542
3543 case KP_SCHANNEL_ALG:
3544 switch (((PSCHANNEL_ALG)pbData)->dwUse) {
3545 case SCHANNEL_ENC_KEY:
3546 memcpy(&pCryptKey->siSChannelInfo.saEncAlg, pbData, sizeof(SCHANNEL_ALG));
3547 break;
3548
3549 case SCHANNEL_MAC_KEY:
3550 memcpy(&pCryptKey->siSChannelInfo.saMACAlg, pbData, sizeof(SCHANNEL_ALG));
3551 break;
3552
3553 default:
3554 SetLastError(NTE_FAIL); /* FIXME: error code */
3555 return FALSE;
3556 }
3557 return TRUE;
3558
3559 case KP_CLIENT_RANDOM:
3560 return copy_data_blob(&pCryptKey->siSChannelInfo.blobClientRandom, (PCRYPT_DATA_BLOB)pbData);
3561
3562 case KP_SERVER_RANDOM:
3563 return copy_data_blob(&pCryptKey->siSChannelInfo.blobServerRandom, (PCRYPT_DATA_BLOB)pbData);
3564
3565 default:
3566 SetLastError(NTE_BAD_TYPE);
3567 return FALSE;
3568 }
3569 }
3570
3571 /******************************************************************************
3572 * CPGetKeyParam (RSAENH.@)
3573 *
3574 * Query a key parameter.
3575 *
3576 * PARAMS
3577 * hProv [I] The key container, which the key belongs to.
3578 * hHash [I] The key object that is to be queried.
3579 * dwParam [I] Specifies the parameter that is to be queried.
3580 * pbData [I] Pointer to the buffer where the parameter value will be stored.
3581 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
3582 * dwFlags [I] None currently defined.
3583 *
3584 * RETURNS
3585 * Success: TRUE
3586 * Failure: FALSE
3587 *
3588 * NOTES
3589 * Defined dwParam types are:
3590 * - KP_MODE: Values MODE_CBC, MODE_ECB, MODE_CFB.
3591 * - KP_MODE_BITS: Shift width for cipher feedback mode.
3592 * (Currently ignored by MS CSP's - always eight)
3593 * - KP_PERMISSIONS: Or'ed combination of CRYPT_ENCRYPT, CRYPT_DECRYPT,
3594 * CRYPT_EXPORT, CRYPT_READ, CRYPT_WRITE, CRYPT_MAC
3595 * - KP_IV: Initialization vector.
3596 * - KP_KEYLEN: Bitwidth of the key.
3597 * - KP_BLOCKLEN: Size of a block cipher block.
3598 * - KP_SALT: Salt value.
3599 */
3600 BOOL WINAPI RSAENH_CPGetKeyParam(HCRYPTPROV hProv, HCRYPTKEY hKey, DWORD dwParam, BYTE *pbData,
3601 DWORD *pdwDataLen, DWORD dwFlags)
3602 {
3603 CRYPTKEY *pCryptKey;
3604 DWORD dwValue;
3605
3606 TRACE("(hProv=%08lx, hKey=%08lx, dwParam=%08x, pbData=%p, pdwDataLen=%p dwFlags=%08x)\n",
3607 hProv, hKey, dwParam, pbData, pdwDataLen, dwFlags);
3608
3609 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
3610 {
3611 SetLastError(NTE_BAD_UID);
3612 return FALSE;
3613 }
3614
3615 if (dwFlags) {
3616 SetLastError(NTE_BAD_FLAGS);
3617 return FALSE;
3618 }
3619
3620 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
3621 {
3622 SetLastError(NTE_BAD_KEY);
3623 return FALSE;
3624 }
3625
3626 switch (dwParam)
3627 {
3628 case KP_IV:
3629 return copy_param(pbData, pdwDataLen, pCryptKey->abInitVector,
3630 pCryptKey->dwBlockLen);
3631
3632 case KP_SALT:
3633 switch (pCryptKey->aiAlgid) {
3634 case CALG_RC2:
3635 case CALG_RC4:
3636 return copy_param(pbData, pdwDataLen,
3637 &pCryptKey->abKeyValue[pCryptKey->dwKeyLen],
3638 pCryptKey->dwSaltLen);
3639 default:
3640 SetLastError(NTE_BAD_KEY);
3641 return FALSE;
3642 }
3643
3644 case KP_PADDING:
3645 dwValue = PKCS5_PADDING;
3646 return copy_param(pbData, pdwDataLen, (const BYTE*)&dwValue, sizeof(DWORD));
3647
3648 case KP_KEYLEN:
3649 dwValue = pCryptKey->dwKeyLen << 3;
3650 return copy_param(pbData, pdwDataLen, (const BYTE*)&dwValue, sizeof(DWORD));
3651
3652 case KP_EFFECTIVE_KEYLEN:
3653 if (pCryptKey->dwEffectiveKeyLen)
3654 dwValue = pCryptKey->dwEffectiveKeyLen;
3655 else
3656 dwValue = pCryptKey->dwKeyLen << 3;
3657 return copy_param(pbData, pdwDataLen, (const BYTE*)&dwValue, sizeof(DWORD));
3658
3659 case KP_BLOCKLEN:
3660 dwValue = pCryptKey->dwBlockLen << 3;
3661 return copy_param(pbData, pdwDataLen, (const BYTE*)&dwValue, sizeof(DWORD));
3662
3663 case KP_MODE:
3664 return copy_param(pbData, pdwDataLen, (const BYTE*)&pCryptKey->dwMode, sizeof(DWORD));
3665
3666 case KP_MODE_BITS:
3667 return copy_param(pbData, pdwDataLen, (const BYTE*)&pCryptKey->dwModeBits,
3668 sizeof(DWORD));
3669
3670 case KP_PERMISSIONS:
3671 return copy_param(pbData, pdwDataLen, (const BYTE*)&pCryptKey->dwPermissions,
3672 sizeof(DWORD));
3673
3674 case KP_ALGID:
3675 return copy_param(pbData, pdwDataLen, (const BYTE*)&pCryptKey->aiAlgid, sizeof(DWORD));
3676
3677 default:
3678 SetLastError(NTE_BAD_TYPE);
3679 return FALSE;
3680 }
3681 }
3682
3683 /******************************************************************************
3684 * CPGetProvParam (RSAENH.@)
3685 *
3686 * Query a CSP parameter.
3687 *
3688 * PARAMS
3689 * hProv [I] The key container that is to be queried.
3690 * dwParam [I] Specifies the parameter that is to be queried.
3691 * pbData [I] Pointer to the buffer where the parameter value will be stored.
3692 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
3693 * dwFlags [I] CRYPT_FIRST: Start enumeration (for PP_ENUMALGS{_EX}).
3694 *
3695 * RETURNS
3696 * Success: TRUE
3697 * Failure: FALSE
3698 * NOTES:
3699 * Defined dwParam types:
3700 * - PP_CONTAINER: Name of the key container.
3701 * - PP_NAME: Name of the cryptographic service provider.
3702 * - PP_SIG_KEYSIZE_INC: RSA signature keywidth granularity in bits.
3703 * - PP_KEYX_KEYSIZE_INC: RSA key-exchange keywidth granularity in bits.
3704 * - PP_ENUMALGS{_EX}: Query provider capabilities.
3705 * - PP_KEYSET_SEC_DESCR: Retrieve security descriptor on container.
3706 */
3707 BOOL WINAPI RSAENH_CPGetProvParam(HCRYPTPROV hProv, DWORD dwParam, BYTE *pbData,
3708 DWORD *pdwDataLen, DWORD dwFlags)
3709 {
3710 KEYCONTAINER *pKeyContainer;
3711 PROV_ENUMALGS provEnumalgs;
3712 DWORD dwTemp;
3713 HKEY hKey;
3714
3715 /* This is for dwParam PP_CRYPT_COUNT_KEY_USE.
3716 * IE6 SP1 asks for it in the 'About' dialog.
3717 * Returning this BLOB seems to satisfy IE. The marked 0x00 seem
3718 * to be 'don't care's. If you know anything more specific about
3719 * this provider parameter, please report to wine-devel@winehq.org */
3720 static const BYTE abWTF[96] = {
3721 0xb0, 0x25, 0x63, 0x86, 0x9c, 0xab, 0xb6, 0x37,
3722 0xe8, 0x82, /**/0x00,/**/ 0x72, 0x06, 0xb2, /**/0x00,/**/ 0x3b,
3723 0x60, 0x35, /**/0x00,/**/ 0x3b, 0x88, 0xce, /**/0x00,/**/ 0x82,
3724 0xbc, 0x7a, /**/0x00,/**/ 0xb7, 0x4f, 0x7e, /**/0x00,/**/ 0xde,
3725 0x92, 0xf1, /**/0x00,/**/ 0x83, 0xea, 0x5e, /**/0x00,/**/ 0xc8,
3726 0x12, 0x1e, 0xd4, 0x06, 0xf7, 0x66, /**/0x00,/**/ 0x01,
3727 0x29, 0xa4, /**/0x00,/**/ 0xf8, 0x24, 0x0c, /**/0x00,/**/ 0x33,
3728 0x06, 0x80, /**/0x00,/**/ 0x02, 0x46, 0x0b, /**/0x00,/**/ 0x6d,
3729 0x5b, 0xca, /**/0x00,/**/ 0x9a, 0x10, 0xf0, /**/0x00,/**/ 0x05,
3730 0x19, 0xd0, /**/0x00,/**/ 0x2c, 0xf6, 0x27, /**/0x00,/**/ 0xaa,
3731 0x7c, 0x6f, /**/0x00,/**/ 0xb9, 0xd8, 0x72, /**/0x00,/**/ 0x03,
3732 0xf3, 0x81, /**/0x00,/**/ 0xfa, 0xe8, 0x26, /**/0x00,/**/ 0xca
3733 };
3734
3735 TRACE("(hProv=%08lx, dwParam=%08x, pbData=%p, pdwDataLen=%p, dwFlags=%08x)\n",
3736 hProv, dwParam, pbData, pdwDataLen, dwFlags);
3737
3738 if (!pdwDataLen) {
3739 SetLastError(ERROR_INVALID_PARAMETER);
3740 return FALSE;
3741 }
3742
3743 if (!(pKeyContainer = get_key_container(hProv)))
3744 {
3745 /* MSDN: hProv not containing valid context handle */
3746 return FALSE;
3747 }
3748
3749 switch (dwParam)
3750 {
3751 case PP_CONTAINER:
3752 case PP_UNIQUE_CONTAINER:/* MSDN says we can return the same value as PP_CONTAINER */
3753 return copy_param(pbData, pdwDataLen, (const BYTE*)pKeyContainer->szName,
3754 strlen(pKeyContainer->szName)+1);
3755
3756 case PP_NAME:
3757 return copy_param(pbData, pdwDataLen, (const BYTE*)pKeyContainer->szProvName,
3758 strlen(pKeyContainer->szProvName)+1);
3759
3760 case PP_PROVTYPE:
3761 dwTemp = PROV_RSA_FULL;
3762 return copy_param(pbData, pdwDataLen, (const BYTE*)&dwTemp, sizeof(dwTemp));
3763
3764 case PP_KEYSPEC:
3765 dwTemp = AT_SIGNATURE | AT_KEYEXCHANGE;
3766 return copy_param(pbData, pdwDataLen, (const BYTE*)&dwTemp, sizeof(dwTemp));
3767
3768 case PP_KEYSET_TYPE:
3769 dwTemp = pKeyContainer->dwFlags & CRYPT_MACHINE_KEYSET;
3770 return copy_param(pbData, pdwDataLen, (const BYTE*)&dwTemp, sizeof(dwTemp));
3771
3772 case PP_KEYSTORAGE:
3773 dwTemp = CRYPT_SEC_DESCR;
3774 return copy_param(pbData, pdwDataLen, (const BYTE*)&dwTemp, sizeof(dwTemp));
3775
3776 case PP_SIG_KEYSIZE_INC:
3777 case PP_KEYX_KEYSIZE_INC:
3778 dwTemp = 8;
3779 return copy_param(pbData, pdwDataLen, (const BYTE*)&dwTemp, sizeof(dwTemp));
3780
3781 case PP_IMPTYPE:
3782 dwTemp = CRYPT_IMPL_SOFTWARE;
3783 return copy_param(pbData, pdwDataLen, (const BYTE*)&dwTemp, sizeof(dwTemp));
3784
3785 case PP_VERSION:
3786 dwTemp = 0x00000200;
3787 return copy_param(pbData, pdwDataLen, (const BYTE*)&dwTemp, sizeof(dwTemp));
3788
3789 case PP_ENUMCONTAINERS:
3790 if ((dwFlags & CRYPT_FIRST) == CRYPT_FIRST) pKeyContainer->dwEnumContainersCtr = 0;
3791
3792 if (!pbData) {
3793 *pdwDataLen = (DWORD)MAX_PATH + 1;
3794 return TRUE;
3795 }
3796
3797 if (!open_container_key("", dwFlags, KEY_READ, &hKey))
3798 {
3799 SetLastError(ERROR_NO_MORE_ITEMS);
3800 return FALSE;
3801 }
3802
3803 dwTemp = *pdwDataLen;
3804 switch (RegEnumKeyExA(hKey, pKeyContainer->dwEnumContainersCtr, (LPSTR)pbData, &dwTemp,
3805 NULL, NULL, NULL, NULL))
3806 {
3807 case ERROR_MORE_DATA:
3808 *pdwDataLen = (DWORD)MAX_PATH + 1;
3809
3810 case ERROR_SUCCESS:
3811 pKeyContainer->dwEnumContainersCtr++;
3812 RegCloseKey(hKey);
3813 return TRUE;
3814
3815 case ERROR_NO_MORE_ITEMS:
3816 default:
3817 SetLastError(ERROR_NO_MORE_ITEMS);
3818 RegCloseKey(hKey);
3819 return FALSE;
3820 }
3821
3822 case PP_ENUMALGS:
3823 case PP_ENUMALGS_EX:
3824 if (((pKeyContainer->dwEnumAlgsCtr >= RSAENH_MAX_ENUMALGS-1) ||
3825 (!aProvEnumAlgsEx[pKeyContainer->dwPersonality]
3826 [pKeyContainer->dwEnumAlgsCtr+1].aiAlgid)) &&
3827 ((dwFlags & CRYPT_FIRST) != CRYPT_FIRST))
3828 {
3829 SetLastError(ERROR_NO_MORE_ITEMS);
3830 return FALSE;
3831 }
3832
3833 if (dwParam == PP_ENUMALGS) {
3834 if (pbData && (*pdwDataLen >= sizeof(PROV_ENUMALGS)))
3835 pKeyContainer->dwEnumAlgsCtr = ((dwFlags & CRYPT_FIRST) == CRYPT_FIRST) ?
3836 0 : pKeyContainer->dwEnumAlgsCtr+1;
3837
3838 provEnumalgs.aiAlgid = aProvEnumAlgsEx
3839 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr].aiAlgid;
3840 provEnumalgs.dwBitLen = aProvEnumAlgsEx
3841 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr].dwDefaultLen;
3842 provEnumalgs.dwNameLen = aProvEnumAlgsEx
3843 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr].dwNameLen;
3844 memcpy(provEnumalgs.szName, aProvEnumAlgsEx
3845 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr].szName,
3846 20*sizeof(CHAR));
3847
3848 return copy_param(pbData, pdwDataLen, (const BYTE*)&provEnumalgs,
3849 sizeof(PROV_ENUMALGS));
3850 } else {
3851 if (pbData && (*pdwDataLen >= sizeof(PROV_ENUMALGS_EX)))
3852 pKeyContainer->dwEnumAlgsCtr = ((dwFlags & CRYPT_FIRST) == CRYPT_FIRST) ?
3853 0 : pKeyContainer->dwEnumAlgsCtr+1;
3854
3855 return copy_param(pbData, pdwDataLen,
3856 (const BYTE*)&aProvEnumAlgsEx
3857 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr],
3858 sizeof(PROV_ENUMALGS_EX));
3859 }
3860
3861 case PP_CRYPT_COUNT_KEY_USE: /* Asked for by IE About dialog */
3862 return copy_param(pbData, pdwDataLen, abWTF, sizeof(abWTF));
3863
3864 case PP_KEYSET_SEC_DESCR:
3865 {
3866 SECURITY_DESCRIPTOR *sd;
3867 DWORD err, len, flags = (pKeyContainer->dwFlags & CRYPT_MACHINE_KEYSET);
3868
3869 if (!open_container_key(pKeyContainer->szName, flags, KEY_READ, &hKey))
3870 {
3871 SetLastError(NTE_BAD_KEYSET);
3872 return FALSE;
3873 }
3874
3875 err = GetSecurityInfo(hKey, SE_REGISTRY_KEY, dwFlags, NULL, NULL, NULL, NULL, (void **)&sd);
3876 RegCloseKey(hKey);
3877 if (err)
3878 {
3879 SetLastError(err);
3880 return FALSE;
3881 }
3882
3883 len = GetSecurityDescriptorLength(sd);
3884 if (*pdwDataLen >= len) memcpy(pbData, sd, len);
3885 else SetLastError(ERROR_INSUFFICIENT_BUFFER);
3886 *pdwDataLen = len;
3887
3888 LocalFree(sd);
3889 return TRUE;
3890 }
3891
3892 default:
3893 /* MSDN: Unknown parameter number in dwParam */
3894 SetLastError(NTE_BAD_TYPE);
3895 return FALSE;
3896 }
3897 }
3898
3899 /******************************************************************************
3900 * CPDeriveKey (RSAENH.@)
3901 *
3902 * Derives a key from a hash value.
3903 *
3904 * PARAMS
3905 * hProv [I] Key container for which a key is to be generated.
3906 * Algid [I] Crypto algorithm identifier for the key to be generated.
3907 * hBaseData [I] Hash from whose value the key will be derived.
3908 * dwFlags [I] See Notes.
3909 * phKey [O] The generated key.
3910 *
3911 * RETURNS
3912 * Success: TRUE
3913 * Failure: FALSE
3914 *
3915 * NOTES
3916 * Defined flags:
3917 * - CRYPT_EXPORTABLE: Key can be exported.
3918 * - CRYPT_NO_SALT: No salt is used for 40 bit keys.
3919 * - CRYPT_CREATE_SALT: Use remaining bits as salt value.
3920 */
3921 BOOL WINAPI RSAENH_CPDeriveKey(HCRYPTPROV hProv, ALG_ID Algid, HCRYPTHASH hBaseData,
3922 DWORD dwFlags, HCRYPTKEY *phKey)
3923 {
3924 CRYPTKEY *pCryptKey, *pMasterKey;
3925 CRYPTHASH *pCryptHash;
3926 BYTE abHashValue[RSAENH_MAX_HASH_SIZE*2];
3927 DWORD dwLen;
3928
3929 TRACE("(hProv=%08lx, Algid=%d, hBaseData=%08lx, dwFlags=%08x phKey=%p)\n", hProv, Algid,
3930 hBaseData, dwFlags, phKey);
3931
3932 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
3933 {
3934 SetLastError(NTE_BAD_UID);
3935 return FALSE;
3936 }
3937
3938 if (!lookup_handle(&handle_table, hBaseData, RSAENH_MAGIC_HASH,
3939 (OBJECTHDR**)&pCryptHash))
3940 {
3941 SetLastError(NTE_BAD_HASH);
3942 return FALSE;
3943 }
3944
3945 if (!phKey)
3946 {
3947 SetLastError(ERROR_INVALID_PARAMETER);
3948 return FALSE;
3949 }
3950
3951 switch (GET_ALG_CLASS(Algid))
3952 {
3953 case ALG_CLASS_DATA_ENCRYPT:
3954 {
3955 int need_padding, copy_len;
3956 *phKey = new_key(hProv, Algid, dwFlags, &pCryptKey);
3957 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) return FALSE;
3958
3959 /*
3960 * We derive the key material from the hash.
3961 * If the hash value is not large enough for the claimed key, we have to construct
3962 * a larger binary value based on the hash. This is documented in MSDN: CryptDeriveKey.
3963 */
3964 dwLen = RSAENH_MAX_HASH_SIZE;
3965 RSAENH_CPGetHashParam(pCryptHash->hProv, hBaseData, HP_HASHVAL, abHashValue, &dwLen, 0);
3966
3967 /*
3968 * The usage of padding seems to vary from algorithm to algorithm.
3969 * For now the only different case found was for AES with 128 bit key.
3970 */
3971 switch(Algid)
3972 {
3973 case CALG_AES_128:
3974 /* To reduce the chance of regressions we will only deviate
3975 * from the old behavior for the tested hash lengths */
3976 if (dwLen == 16 || dwLen == 20)
3977 {
3978 need_padding = 1;
3979 break;
3980 }
3981 default:
3982 need_padding = dwLen < pCryptKey->dwKeyLen;
3983 }
3984
3985 copy_len = pCryptKey->dwKeyLen;
3986 if (need_padding)
3987 {
3988 BYTE pad1[RSAENH_HMAC_DEF_PAD_LEN], pad2[RSAENH_HMAC_DEF_PAD_LEN];
3989 BYTE old_hashval[RSAENH_MAX_HASH_SIZE];
3990 DWORD i;
3991
3992 memcpy(old_hashval, pCryptHash->abHashValue, RSAENH_MAX_HASH_SIZE);
3993
3994 for (i=0; i<RSAENH_HMAC_DEF_PAD_LEN; i++) {
3995 pad1[i] = RSAENH_HMAC_DEF_IPAD_CHAR ^ (i<dwLen ? abHashValue[i] : 0);
3996 pad2[i] = RSAENH_HMAC_DEF_OPAD_CHAR ^ (i<dwLen ? abHashValue[i] : 0);
3997 }
3998
3999 init_hash(pCryptHash);
4000 update_hash(pCryptHash, pad1, RSAENH_HMAC_DEF_PAD_LEN);
4001 finalize_hash(pCryptHash);
4002 memcpy(abHashValue, pCryptHash->abHashValue, pCryptHash->dwHashSize);
4003
4004 init_hash(pCryptHash);
4005 update_hash(pCryptHash, pad2, RSAENH_HMAC_DEF_PAD_LEN);
4006 finalize_hash(pCryptHash);
4007 memcpy(abHashValue+pCryptHash->dwHashSize, pCryptHash->abHashValue,
4008 pCryptHash->dwHashSize);
4009
4010 memcpy(pCryptHash->abHashValue, old_hashval, RSAENH_MAX_HASH_SIZE);
4011 }
4012 /*
4013 * Padding was not required, we have more hash than needed.
4014 * Do we need to use the remaining hash as salt?
4015 */
4016 else if((dwFlags & CRYPT_CREATE_SALT) &&
4017 (Algid == CALG_RC2 || Algid == CALG_RC4))
4018 {
4019 copy_len += pCryptKey->dwSaltLen;
4020 }
4021
4022 memcpy(pCryptKey->abKeyValue, abHashValue,
4023 RSAENH_MIN(copy_len, sizeof(pCryptKey->abKeyValue)));
4024 break;
4025 }
4026 case ALG_CLASS_MSG_ENCRYPT:
4027 if (!lookup_handle(&handle_table, pCryptHash->hKey, RSAENH_MAGIC_KEY,
4028 (OBJECTHDR**)&pMasterKey))
4029 {
4030 SetLastError(NTE_FAIL); /* FIXME error code */
4031 return FALSE;
4032 }
4033
4034 switch (Algid)
4035 {
4036 /* See RFC 2246, chapter 6.3 Key calculation */
4037 case CALG_SCHANNEL_ENC_KEY:
4038 if (!pMasterKey->siSChannelInfo.saEncAlg.Algid ||
4039 !pMasterKey->siSChannelInfo.saEncAlg.cBits)
4040 {
4041 SetLastError(NTE_BAD_FLAGS);
4042 return FALSE;
4043 }
4044 *phKey = new_key(hProv, pMasterKey->siSChannelInfo.saEncAlg.Algid,
4045 MAKELONG(LOWORD(dwFlags),pMasterKey->siSChannelInfo.saEncAlg.cBits),
4046 &pCryptKey);
4047 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) return FALSE;
4048 memcpy(pCryptKey->abKeyValue,
4049 pCryptHash->abHashValue + (
4050 2 * (pMasterKey->siSChannelInfo.saMACAlg.cBits / 8) +
4051 ((dwFlags & CRYPT_SERVER) ?
4052 (pMasterKey->siSChannelInfo.saEncAlg.cBits / 8) : 0)),
4053 pMasterKey->siSChannelInfo.saEncAlg.cBits / 8);
4054 memcpy(pCryptKey->abInitVector,
4055 pCryptHash->abHashValue + (
4056 2 * (pMasterKey->siSChannelInfo.saMACAlg.cBits / 8) +
4057 2 * (pMasterKey->siSChannelInfo.saEncAlg.cBits / 8) +
4058 ((dwFlags & CRYPT_SERVER) ? pCryptKey->dwBlockLen : 0)),
4059 pCryptKey->dwBlockLen);
4060 break;
4061
4062 case CALG_SCHANNEL_MAC_KEY:
4063 *phKey = new_key(hProv, Algid,
4064 MAKELONG(LOWORD(dwFlags),pMasterKey->siSChannelInfo.saMACAlg.cBits),
4065 &pCryptKey);
4066 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) return FALSE;
4067 memcpy(pCryptKey->abKeyValue,
4068 pCryptHash->abHashValue + ((dwFlags & CRYPT_SERVER) ?
4069 pMasterKey->siSChannelInfo.saMACAlg.cBits / 8 : 0),
4070 pMasterKey->siSChannelInfo.saMACAlg.cBits / 8);
4071 break;
4072
4073 default:
4074 SetLastError(NTE_BAD_ALGID);
4075 return FALSE;
4076 }
4077 break;
4078
4079 default:
4080 SetLastError(NTE_BAD_ALGID);
4081 return FALSE;
4082 }
4083
4084 setup_key(pCryptKey);
4085 return TRUE;
4086 }
4087
4088 /******************************************************************************
4089 * CPGetUserKey (RSAENH.@)
4090 *
4091 * Returns a handle to the user's private key-exchange- or signature-key.
4092 *
4093 * PARAMS
4094 * hProv [I] The key container from which a user key is requested.
4095 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
4096 * phUserKey [O] Handle to the requested key or INVALID_HANDLE_VALUE in case of failure.
4097 *
4098 * RETURNS
4099 * Success: TRUE.
4100 * Failure: FALSE.
4101 *
4102 * NOTE
4103 * A newly created key container does not contain private user key. Create them with CPGenKey.
4104 */
4105 BOOL WINAPI RSAENH_CPGetUserKey(HCRYPTPROV hProv, DWORD dwKeySpec, HCRYPTKEY *phUserKey)
4106 {
4107 KEYCONTAINER *pKeyContainer;
4108
4109 TRACE("(hProv=%08lx, dwKeySpec=%08x, phUserKey=%p)\n", hProv, dwKeySpec, phUserKey);
4110
4111 if (!(pKeyContainer = get_key_container(hProv)))
4112 {
4113 /* MSDN: hProv not containing valid context handle */
4114 return FALSE;
4115 }
4116
4117 switch (dwKeySpec)
4118 {
4119 case AT_KEYEXCHANGE:
4120 copy_handle(&handle_table, pKeyContainer->hKeyExchangeKeyPair, RSAENH_MAGIC_KEY,
4121 phUserKey);
4122 break;
4123
4124 case AT_SIGNATURE:
4125 copy_handle(&handle_table, pKeyContainer->hSignatureKeyPair, RSAENH_MAGIC_KEY,
4126 phUserKey);
4127 break;
4128
4129 default:
4130 *phUserKey = (HCRYPTKEY)INVALID_HANDLE_VALUE;
4131 }
4132
4133 if (*phUserKey == (HCRYPTKEY)INVALID_HANDLE_VALUE)
4134 {
4135 /* MSDN: dwKeySpec parameter specifies nonexistent key */
4136 SetLastError(NTE_NO_KEY);
4137 return FALSE;
4138 }
4139
4140 return TRUE;
4141 }
4142
4143 /******************************************************************************
4144 * CPHashData (RSAENH.@)
4145 *
4146 * Updates a hash object with the given data.
4147 *
4148 * PARAMS
4149 * hProv [I] Key container to which the hash object belongs.
4150 * hHash [I] Hash object which is to be updated.
4151 * pbData [I] Pointer to data with which the hash object is to be updated.
4152 * dwDataLen [I] Length of the data.
4153 * dwFlags [I] Currently none defined.
4154 *
4155 * RETURNS
4156 * Success: TRUE.
4157 * Failure: FALSE.
4158 *
4159 * NOTES
4160 * The actual hash value is queried with CPGetHashParam, which will finalize
4161 * the hash. Updating a finalized hash will fail with a last error NTE_BAD_HASH_STATE.
4162 */
4163 BOOL WINAPI RSAENH_CPHashData(HCRYPTPROV hProv, HCRYPTHASH hHash, const BYTE *pbData,
4164 DWORD dwDataLen, DWORD dwFlags)
4165 {
4166 CRYPTHASH *pCryptHash;
4167
4168 TRACE("(hProv=%08lx, hHash=%08lx, pbData=%p, dwDataLen=%d, dwFlags=%08x)\n",
4169 hProv, hHash, pbData, dwDataLen, dwFlags);
4170
4171 if (dwFlags & ~CRYPT_USERDATA)
4172 {
4173 SetLastError(NTE_BAD_FLAGS);
4174 return FALSE;
4175 }
4176
4177 if (!lookup_handle(&handle_table, hHash, RSAENH_MAGIC_HASH,
4178 (OBJECTHDR**)&pCryptHash))
4179 {
4180 SetLastError(NTE_BAD_HASH);
4181 return FALSE;
4182 }
4183
4184 if (!get_algid_info(hProv, pCryptHash->aiAlgid) || pCryptHash->aiAlgid == CALG_SSL3_SHAMD5)
4185 {
4186 SetLastError(NTE_BAD_ALGID);
4187 return FALSE;
4188 }
4189
4190 if (pCryptHash->dwState != RSAENH_HASHSTATE_HASHING)
4191 {
4192 SetLastError(NTE_BAD_HASH_STATE);
4193 return FALSE;
4194 }
4195
4196 update_hash(pCryptHash, pbData, dwDataLen);
4197 return TRUE;
4198 }
4199
4200 /******************************************************************************
4201 * CPHashSessionKey (RSAENH.@)
4202 *
4203 * Updates a hash object with the binary representation of a symmetric key.
4204 *
4205 * PARAMS
4206 * hProv [I] Key container to which the hash object belongs.
4207 * hHash [I] Hash object which is to be updated.
4208 * hKey [I] The symmetric key, whose binary value will be added to the hash.
4209 * dwFlags [I] CRYPT_LITTLE_ENDIAN, if the binary key value shall be interpreted as little endian.
4210 *
4211 * RETURNS
4212 * Success: TRUE.
4213 * Failure: FALSE.
4214 */
4215 BOOL WINAPI RSAENH_CPHashSessionKey(HCRYPTPROV hProv, HCRYPTHASH hHash, HCRYPTKEY hKey,
4216 DWORD dwFlags)
4217 {
4218 BYTE abKeyValue[RSAENH_MAX_KEY_SIZE], bTemp;
4219 CRYPTKEY *pKey;
4220 DWORD i;
4221
4222 TRACE("(hProv=%08lx, hHash=%08lx, hKey=%08lx, dwFlags=%08x)\n", hProv, hHash, hKey, dwFlags);
4223
4224 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pKey) ||
4225 (GET_ALG_CLASS(pKey->aiAlgid) != ALG_CLASS_DATA_ENCRYPT))
4226 {
4227 SetLastError(NTE_BAD_KEY);
4228 return FALSE;
4229 }
4230
4231 if (dwFlags & ~CRYPT_LITTLE_ENDIAN) {
4232 SetLastError(NTE_BAD_FLAGS);
4233 return FALSE;
4234 }
4235
4236 memcpy(abKeyValue, pKey->abKeyValue, pKey->dwKeyLen);
4237 if (!(dwFlags & CRYPT_LITTLE_ENDIAN)) {
4238 for (i=0; i<pKey->dwKeyLen/2; i++) {
4239 bTemp = abKeyValue[i];
4240 abKeyValue[i] = abKeyValue[pKey->dwKeyLen-i-1];
4241 abKeyValue[pKey->dwKeyLen-i-1] = bTemp;
4242 }
4243 }
4244
4245 return RSAENH_CPHashData(hProv, hHash, abKeyValue, pKey->dwKeyLen, 0);
4246 }
4247
4248 /******************************************************************************
4249 * CPReleaseContext (RSAENH.@)
4250 *
4251 * Release a key container.
4252 *
4253 * PARAMS
4254 * hProv [I] Key container to be released.
4255 * dwFlags [I] Currently none defined.
4256 *
4257 * RETURNS
4258 * Success: TRUE
4259 * Failure: FALSE
4260 */
4261 BOOL WINAPI RSAENH_CPReleaseContext(HCRYPTPROV hProv, DWORD dwFlags)
4262 {
4263 TRACE("(hProv=%08lx, dwFlags=%08x)\n", hProv, dwFlags);
4264
4265 if (!release_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
4266 {
4267 /* MSDN: hProv not containing valid context handle */
4268 SetLastError(NTE_BAD_UID);
4269 return FALSE;
4270 }
4271
4272 if (dwFlags) {
4273 SetLastError(NTE_BAD_FLAGS);
4274 return FALSE;
4275 }
4276
4277 return TRUE;
4278 }
4279
4280 /******************************************************************************
4281 * CPSetHashParam (RSAENH.@)
4282 *
4283 * Set a parameter of a hash object
4284 *
4285 * PARAMS
4286 * hProv [I] The key container to which the key belongs.
4287 * hHash [I] The hash object for which a parameter is to be set.
4288 * dwParam [I] Parameter type. See Notes.
4289 * pbData [I] Pointer to the parameter value.
4290 * dwFlags [I] Currently none defined.
4291 *
4292 * RETURNS
4293 * Success: TRUE.
4294 * Failure: FALSE.
4295 *
4296 * NOTES
4297 * Currently only the HP_HMAC_INFO dwParam type is defined.
4298 * The HMAC_INFO struct will be deep copied into the hash object.
4299 * See Internet RFC 2104 for details on the HMAC algorithm.
4300 */
4301 BOOL WINAPI RSAENH_CPSetHashParam(HCRYPTPROV hProv, HCRYPTHASH hHash, DWORD dwParam,
4302 BYTE *pbData, DWORD dwFlags)
4303 {
4304 CRYPTHASH *pCryptHash;
4305 CRYPTKEY *pCryptKey;
4306 DWORD i;
4307
4308 TRACE("(hProv=%08lx, hHash=%08lx, dwParam=%08x, pbData=%p, dwFlags=%08x)\n",
4309 hProv, hHash, dwParam, pbData, dwFlags);
4310
4311 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
4312 {
4313 SetLastError(NTE_BAD_UID);
4314 return FALSE;
4315 }
4316
4317 if (dwFlags) {
4318 SetLastError(NTE_BAD_FLAGS);
4319 return FALSE;
4320 }
4321
4322 if (!lookup_handle(&handle_table, hHash, RSAENH_MAGIC_HASH,
4323 (OBJECTHDR**)&pCryptHash))
4324 {
4325 SetLastError(NTE_BAD_HASH);
4326 return FALSE;
4327 }
4328
4329 switch (dwParam) {
4330 case HP_HMAC_INFO:
4331 free_hmac_info(pCryptHash->pHMACInfo);
4332 if (!copy_hmac_info(&pCryptHash->pHMACInfo, (PHMAC_INFO)pbData)) return FALSE;
4333
4334 if (!lookup_handle(&handle_table, pCryptHash->hKey, RSAENH_MAGIC_KEY,
4335 (OBJECTHDR**)&pCryptKey))
4336 {
4337 SetLastError(NTE_FAIL); /* FIXME: correct error code? */
4338 return FALSE;
4339 }
4340
4341 if (pCryptKey->aiAlgid == CALG_HMAC && !pCryptKey->dwKeyLen) {
4342 HCRYPTHASH hKeyHash;
4343 DWORD keyLen;
4344
4345 if (!RSAENH_CPCreateHash(hProv, ((PHMAC_INFO)pbData)->HashAlgid, 0, 0,
4346 &hKeyHash))
4347 return FALSE;
4348 if (!RSAENH_CPHashData(hProv, hKeyHash, pCryptKey->blobHmacKey.pbData,
4349 pCryptKey->blobHmacKey.cbData, 0))
4350 {
4351 RSAENH_CPDestroyHash(hProv, hKeyHash);
4352 return FALSE;
4353 }
4354 keyLen = sizeof(pCryptKey->abKeyValue);
4355 if (!RSAENH_CPGetHashParam(hProv, hKeyHash, HP_HASHVAL, pCryptKey->abKeyValue,
4356 &keyLen, 0))
4357 {
4358 RSAENH_CPDestroyHash(hProv, hKeyHash);
4359 return FALSE;
4360 }
4361 pCryptKey->dwKeyLen = keyLen;
4362 RSAENH_CPDestroyHash(hProv, hKeyHash);
4363 }
4364 for (i=0; i<RSAENH_MIN(pCryptKey->dwKeyLen,pCryptHash->pHMACInfo->cbInnerString); i++) {
4365 pCryptHash->pHMACInfo->pbInnerString[i] ^= pCryptKey->abKeyValue[i];
4366 }
4367 for (i=0; i<RSAENH_MIN(pCryptKey->dwKeyLen,pCryptHash->pHMACInfo->cbOuterString); i++) {
4368 pCryptHash->pHMACInfo->pbOuterString[i] ^= pCryptKey->abKeyValue[i];
4369 }
4370
4371 init_hash(pCryptHash);
4372 return TRUE;
4373
4374 case HP_HASHVAL:
4375 memcpy(pCryptHash->abHashValue, pbData, pCryptHash->dwHashSize);
4376 pCryptHash->dwState = RSAENH_HASHSTATE_FINISHED;
4377 return TRUE;
4378
4379 case HP_TLS1PRF_SEED:
4380 return copy_data_blob(&pCryptHash->tpPRFParams.blobSeed, (PCRYPT_DATA_BLOB)pbData);
4381
4382 case HP_TLS1PRF_LABEL:
4383 return copy_data_blob(&pCryptHash->tpPRFParams.blobLabel, (PCRYPT_DATA_BLOB)pbData);
4384
4385 default:
4386 SetLastError(NTE_BAD_TYPE);
4387 return FALSE;
4388 }
4389 }
4390
4391 /******************************************************************************
4392 * CPSetProvParam (RSAENH.@)
4393 */
4394 BOOL WINAPI RSAENH_CPSetProvParam(HCRYPTPROV hProv, DWORD dwParam, BYTE *pbData, DWORD dwFlags)
4395 {
4396 KEYCONTAINER *pKeyContainer;
4397 HKEY hKey;
4398
4399 TRACE("(hProv=%08lx, dwParam=%08x, pbData=%p, dwFlags=%08x)\n", hProv, dwParam, pbData, dwFlags);
4400
4401 if (!(pKeyContainer = get_key_container(hProv)))
4402 return FALSE;
4403
4404 switch (dwParam)
4405 {
4406 case PP_KEYSET_SEC_DESCR:
4407 {
4408 SECURITY_DESCRIPTOR *sd = (SECURITY_DESCRIPTOR *)pbData;
4409 DWORD err, flags = (pKeyContainer->dwFlags & CRYPT_MACHINE_KEYSET);
4410 BOOL def, present;
4411 REGSAM access = WRITE_DAC | WRITE_OWNER | ACCESS_SYSTEM_SECURITY;
4412 PSID owner = NULL, group = NULL;
4413 PACL dacl = NULL, sacl = NULL;
4414
4415 if (!open_container_key(pKeyContainer->szName, flags, access, &hKey))
4416 {
4417 SetLastError(NTE_BAD_KEYSET);
4418 return FALSE;
4419 }
4420
4421 if ((dwFlags & OWNER_SECURITY_INFORMATION && !GetSecurityDescriptorOwner(sd, &owner, &def)) ||
4422 (dwFlags & GROUP_SECURITY_INFORMATION && !GetSecurityDescriptorGroup(sd, &group, &def)) ||
4423 (dwFlags & DACL_SECURITY_INFORMATION && !GetSecurityDescriptorDacl(sd, &present, &dacl, &def)) ||
4424 (dwFlags & SACL_SECURITY_INFORMATION && !GetSecurityDescriptorSacl(sd, &present, &sacl, &def)))
4425 {
4426 RegCloseKey(hKey);
4427 return FALSE;
4428 }
4429
4430 err = SetSecurityInfo(hKey, SE_REGISTRY_KEY, dwFlags, owner, group, dacl, sacl);
4431 RegCloseKey(hKey);
4432 if (err)
4433 {
4434 SetLastError(err);
4435 return FALSE;
4436 }
4437 return TRUE;
4438 }
4439 default:
4440 FIXME("unimplemented parameter %08x\n", dwParam);
4441 return FALSE;
4442 }
4443 }
4444
4445 /******************************************************************************
4446 * CPSignHash (RSAENH.@)
4447 *
4448 * Sign a hash object
4449 *
4450 * PARAMS
4451 * hProv [I] The key container, to which the hash object belongs.
4452 * hHash [I] The hash object to be signed.
4453 * dwKeySpec [I] AT_SIGNATURE or AT_KEYEXCHANGE: Key used to generate the signature.
4454 * sDescription [I] Should be NULL for security reasons.
4455 * dwFlags [I] 0, CRYPT_NOHASHOID or CRYPT_X931_FORMAT: Format of the signature.
4456 * pbSignature [O] Buffer, to which the signature will be stored. May be NULL to query SigLen.
4457 * pdwSigLen [I/O] Size of the buffer (in), Length of the signature (out)
4458 *
4459 * RETURNS
4460 * Success: TRUE
4461 * Failure: FALSE
4462 */
4463 BOOL WINAPI RSAENH_CPSignHash(HCRYPTPROV hProv, HCRYPTHASH hHash, DWORD dwKeySpec,
4464 LPCWSTR sDescription, DWORD dwFlags, BYTE *pbSignature,
4465 DWORD *pdwSigLen)
4466 {
4467 HCRYPTKEY hCryptKey = (HCRYPTKEY)INVALID_HANDLE_VALUE;
4468 CRYPTKEY *pCryptKey;
4469 DWORD dwHashLen;
4470 BYTE abHashValue[RSAENH_MAX_HASH_SIZE];
4471 ALG_ID aiAlgid;
4472 BOOL ret = FALSE;
4473
4474 TRACE("(hProv=%08lx, hHash=%08lx, dwKeySpec=%08x, sDescription=%s, dwFlags=%08x, "
4475 "pbSignature=%p, pdwSigLen=%p)\n", hProv, hHash, dwKeySpec, debugstr_w(sDescription),
4476 dwFlags, pbSignature, pdwSigLen);
4477
4478 if (dwFlags & ~(CRYPT_NOHASHOID|CRYPT_X931_FORMAT)) {
4479 SetLastError(NTE_BAD_FLAGS);
4480 return FALSE;
4481 }
4482
4483 if (!RSAENH_CPGetUserKey(hProv, dwKeySpec, &hCryptKey)) return FALSE;
4484
4485 if (!lookup_handle(&handle_table, hCryptKey, RSAENH_MAGIC_KEY,
4486 (OBJECTHDR**)&pCryptKey))
4487 {
4488 SetLastError(NTE_NO_KEY);
4489 goto out;
4490 }
4491
4492 if (!pbSignature) {
4493 *pdwSigLen = pCryptKey->dwKeyLen;
4494 ret = TRUE;
4495 goto out;
4496 }
4497 if (pCryptKey->dwKeyLen > *pdwSigLen)
4498 {
4499 SetLastError(ERROR_MORE_DATA);
4500 *pdwSigLen = pCryptKey->dwKeyLen;
4501 goto out;
4502 }
4503 *pdwSigLen = pCryptKey->dwKeyLen;
4504
4505 if (sDescription) {
4506 if (!RSAENH_CPHashData(hProv, hHash, (const BYTE*)sDescription,
4507 (DWORD)lstrlenW(sDescription)*sizeof(WCHAR), 0))
4508 {
4509 goto out;
4510 }
4511 }
4512
4513 dwHashLen = sizeof(DWORD);
4514 if (!RSAENH_CPGetHashParam(hProv, hHash, HP_ALGID, (BYTE*)&aiAlgid, &dwHashLen, 0)) goto out;
4515
4516 dwHashLen = RSAENH_MAX_HASH_SIZE;
4517 if (!RSAENH_CPGetHashParam(hProv, hHash, HP_HASHVAL, abHashValue, &dwHashLen, 0)) goto out;
4518
4519
4520 if (!build_hash_signature(pbSignature, *pdwSigLen, aiAlgid, abHashValue, dwHashLen, dwFlags)) {
4521 goto out;
4522 }
4523
4524 ret = encrypt_block_impl(pCryptKey->aiAlgid, PK_PRIVATE, &pCryptKey->context, pbSignature, pbSignature, RSAENH_ENCRYPT);
4525 out:
4526 RSAENH_CPDestroyKey(hProv, hCryptKey);
4527 return ret;
4528 }
4529
4530 /******************************************************************************
4531 * CPVerifySignature (RSAENH.@)
4532 *
4533 * Verify the signature of a hash object.
4534 *
4535 * PARAMS
4536 * hProv [I] The key container, to which the hash belongs.
4537 * hHash [I] The hash for which the signature is verified.
4538 * pbSignature [I] The binary signature.
4539 * dwSigLen [I] Length of the signature BLOB.
4540 * hPubKey [I] Public key used to verify the signature.
4541 * sDescription [I] Should be NULL for security reasons.
4542 * dwFlags [I] 0, CRYPT_NOHASHOID or CRYPT_X931_FORMAT: Format of the signature.
4543 *
4544 * RETURNS
4545 * Success: TRUE (Signature is valid)
4546 * Failure: FALSE (GetLastError() == NTE_BAD_SIGNATURE, if signature is invalid)
4547 */
4548 BOOL WINAPI RSAENH_CPVerifySignature(HCRYPTPROV hProv, HCRYPTHASH hHash, const BYTE *pbSignature,
4549 DWORD dwSigLen, HCRYPTKEY hPubKey, LPCWSTR sDescription,
4550 DWORD dwFlags)
4551 {
4552 BYTE *pbConstructed = NULL, *pbDecrypted = NULL;
4553 CRYPTKEY *pCryptKey;
4554 DWORD dwHashLen;
4555 ALG_ID aiAlgid;
4556 BYTE abHashValue[RSAENH_MAX_HASH_SIZE];
4557 BOOL res = FALSE;
4558
4559 TRACE("(hProv=%08lx, hHash=%08lx, pbSignature=%p, dwSigLen=%d, hPubKey=%08lx, sDescription=%s, "
4560 "dwFlags=%08x)\n", hProv, hHash, pbSignature, dwSigLen, hPubKey, debugstr_w(sDescription),
4561 dwFlags);
4562
4563 if (dwFlags & ~(CRYPT_NOHASHOID|CRYPT_X931_FORMAT)) {
4564 SetLastError(NTE_BAD_FLAGS);
4565 return FALSE;
4566 }
4567
4568 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
4569 {
4570 SetLastError(NTE_BAD_UID);
4571 return FALSE;
4572 }
4573
4574 if (!lookup_handle(&handle_table, hPubKey, RSAENH_MAGIC_KEY,
4575 (OBJECTHDR**)&pCryptKey))
4576 {
4577 SetLastError(NTE_BAD_KEY);
4578 return FALSE;
4579 }
4580
4581 /* in Microsoft implementation, the signature length is checked before
4582 * the signature pointer.
4583 */
4584 if (dwSigLen != pCryptKey->dwKeyLen)
4585 {
4586 SetLastError(NTE_BAD_SIGNATURE);
4587 return FALSE;
4588 }
4589
4590 if (!hHash || !pbSignature)
4591 {
4592 SetLastError(ERROR_INVALID_PARAMETER);
4593 return FALSE;
4594 }
4595
4596 if (sDescription) {
4597 if (!RSAENH_CPHashData(hProv, hHash, (const BYTE*)sDescription,
4598 (DWORD)lstrlenW(sDescription)*sizeof(WCHAR), 0))
4599 {
4600 return FALSE;
4601 }
4602 }
4603
4604 dwHashLen = sizeof(DWORD);
4605 if (!RSAENH_CPGetHashParam(hProv, hHash, HP_ALGID, (BYTE*)&aiAlgid, &dwHashLen, 0)) return FALSE;
4606
4607 dwHashLen = RSAENH_MAX_HASH_SIZE;
4608 if (!RSAENH_CPGetHashParam(hProv, hHash, HP_HASHVAL, abHashValue, &dwHashLen, 0)) return FALSE;
4609
4610 pbConstructed = HeapAlloc(GetProcessHeap(), 0, dwSigLen);
4611 if (!pbConstructed) {
4612 SetLastError(NTE_NO_MEMORY);
4613 goto cleanup;
4614 }
4615
4616 pbDecrypted = HeapAlloc(GetProcessHeap(), 0, dwSigLen);
4617 if (!pbDecrypted) {
4618 SetLastError(NTE_NO_MEMORY);
4619 goto cleanup;
4620 }
4621
4622 if (!encrypt_block_impl(pCryptKey->aiAlgid, PK_PUBLIC, &pCryptKey->context, pbSignature, pbDecrypted,
4623 RSAENH_DECRYPT))
4624 {
4625 goto cleanup;
4626 }
4627
4628 if (build_hash_signature(pbConstructed, dwSigLen, aiAlgid, abHashValue, dwHashLen, dwFlags) &&
4629 !memcmp(pbDecrypted, pbConstructed, dwSigLen)) {
4630 res = TRUE;
4631 goto cleanup;
4632 }
4633
4634 if (!(dwFlags & CRYPT_NOHASHOID) &&
4635 build_hash_signature(pbConstructed, dwSigLen, aiAlgid, abHashValue, dwHashLen, dwFlags|CRYPT_NOHASHOID) &&
4636 !memcmp(pbDecrypted, pbConstructed, dwSigLen)) {
4637 res = TRUE;
4638 goto cleanup;
4639 }
4640
4641 SetLastError(NTE_BAD_SIGNATURE);
4642
4643 cleanup:
4644 HeapFree(GetProcessHeap(), 0, pbConstructed);
4645 HeapFree(GetProcessHeap(), 0, pbDecrypted);
4646 return res;
4647 }
4648
4649 /******************************************************************************
4650 * DllRegisterServer (RSAENH.@)
4651 */
4652 HRESULT WINAPI DllRegisterServer(void)
4653 {
4654 return __wine_register_resources( instance );
4655 }
4656
4657 /******************************************************************************
4658 * DllUnregisterServer (RSAENH.@)
4659 */
4660 HRESULT WINAPI DllUnregisterServer(void)
4661 {
4662 return __wine_unregister_resources( instance );
4663 }