wpp: Free the include filename on error (Valgrind).
[wine/multimedia.git] / dlls / rsaenh / rsaenh.c
blobb5ab7a9028eff844f7508c4960ae1633e7f0856b
1 /*
2 * dlls/rsaenh/rsaenh.c
3 * RSAENH - RSA encryption for Wine
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
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.
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.
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
25 #include "config.h"
26 #include "wine/port.h"
27 #include "wine/library.h"
28 #include "wine/debug.h"
30 #include <stdarg.h>
31 #include <stdio.h>
33 #include "windef.h"
34 #include "winbase.h"
35 #include "winreg.h"
36 #include "wincrypt.h"
37 #include "handle.h"
38 #include "implglue.h"
39 #include "objbase.h"
40 #include "rpcproxy.h"
41 #include "aclapi.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(crypt);
45 static HINSTANCE instance;
47 /******************************************************************************
48 * CRYPTHASH - hash objects
50 #define RSAENH_MAGIC_HASH 0x85938417u
51 #define RSAENH_MAX_HASH_SIZE 104
52 #define RSAENH_HASHSTATE_HASHING 1
53 #define RSAENH_HASHSTATE_FINISHED 2
54 typedef struct _RSAENH_TLS1PRF_PARAMS
56 CRYPT_DATA_BLOB blobLabel;
57 CRYPT_DATA_BLOB blobSeed;
58 } RSAENH_TLS1PRF_PARAMS;
60 typedef struct tagCRYPTHASH
62 OBJECTHDR header;
63 ALG_ID aiAlgid;
64 HCRYPTKEY hKey;
65 HCRYPTPROV hProv;
66 DWORD dwHashSize;
67 DWORD dwState;
68 HASH_CONTEXT context;
69 BYTE abHashValue[RSAENH_MAX_HASH_SIZE];
70 PHMAC_INFO pHMACInfo;
71 RSAENH_TLS1PRF_PARAMS tpPRFParams;
72 } CRYPTHASH;
74 /******************************************************************************
75 * CRYPTKEY - key objects
77 #define RSAENH_MAGIC_KEY 0x73620457u
78 #define RSAENH_MAX_KEY_SIZE 64
79 #define RSAENH_MAX_BLOCK_SIZE 24
80 #define RSAENH_KEYSTATE_IDLE 0
81 #define RSAENH_KEYSTATE_ENCRYPTING 1
82 #define RSAENH_KEYSTATE_MASTERKEY 2
83 typedef struct _RSAENH_SCHANNEL_INFO
85 SCHANNEL_ALG saEncAlg;
86 SCHANNEL_ALG saMACAlg;
87 CRYPT_DATA_BLOB blobClientRandom;
88 CRYPT_DATA_BLOB blobServerRandom;
89 } RSAENH_SCHANNEL_INFO;
91 typedef struct tagCRYPTKEY
93 OBJECTHDR header;
94 ALG_ID aiAlgid;
95 HCRYPTPROV hProv;
96 DWORD dwMode;
97 DWORD dwModeBits;
98 DWORD dwPermissions;
99 DWORD dwKeyLen;
100 DWORD dwEffectiveKeyLen;
101 DWORD dwSaltLen;
102 DWORD dwBlockLen;
103 DWORD dwState;
104 KEY_CONTEXT context;
105 BYTE abKeyValue[RSAENH_MAX_KEY_SIZE];
106 BYTE abInitVector[RSAENH_MAX_BLOCK_SIZE];
107 BYTE abChainVector[RSAENH_MAX_BLOCK_SIZE];
108 RSAENH_SCHANNEL_INFO siSChannelInfo;
109 CRYPT_DATA_BLOB blobHmacKey;
110 } CRYPTKEY;
112 /******************************************************************************
113 * KEYCONTAINER - key containers
115 #define RSAENH_PERSONALITY_BASE 0u
116 #define RSAENH_PERSONALITY_STRONG 1u
117 #define RSAENH_PERSONALITY_ENHANCED 2u
118 #define RSAENH_PERSONALITY_SCHANNEL 3u
119 #define RSAENH_PERSONALITY_AES 4u
121 #define RSAENH_MAGIC_CONTAINER 0x26384993u
122 typedef struct tagKEYCONTAINER
124 OBJECTHDR header;
125 DWORD dwFlags;
126 DWORD dwPersonality;
127 DWORD dwEnumAlgsCtr;
128 DWORD dwEnumContainersCtr;
129 CHAR szName[MAX_PATH];
130 CHAR szProvName[MAX_PATH];
131 HCRYPTKEY hKeyExchangeKeyPair;
132 HCRYPTKEY hSignatureKeyPair;
133 } KEYCONTAINER;
135 /******************************************************************************
136 * Some magic constants
138 #define RSAENH_ENCRYPT 1
139 #define RSAENH_DECRYPT 0
140 #define RSAENH_HMAC_DEF_IPAD_CHAR 0x36
141 #define RSAENH_HMAC_DEF_OPAD_CHAR 0x5c
142 #define RSAENH_HMAC_DEF_PAD_LEN 64
143 #define RSAENH_HMAC_BLOCK_LEN 64
144 #define RSAENH_DES_EFFECTIVE_KEYLEN 56
145 #define RSAENH_DES_STORAGE_KEYLEN 64
146 #define RSAENH_3DES112_EFFECTIVE_KEYLEN 112
147 #define RSAENH_3DES112_STORAGE_KEYLEN 128
148 #define RSAENH_3DES_EFFECTIVE_KEYLEN 168
149 #define RSAENH_3DES_STORAGE_KEYLEN 192
150 #define RSAENH_MAGIC_RSA2 0x32415352
151 #define RSAENH_MAGIC_RSA1 0x31415352
152 #define RSAENH_PKC_BLOCKTYPE 0x02
153 #define RSAENH_SSL3_VERSION_MAJOR 3
154 #define RSAENH_SSL3_VERSION_MINOR 0
155 #define RSAENH_TLS1_VERSION_MAJOR 3
156 #define RSAENH_TLS1_VERSION_MINOR 1
157 #define RSAENH_REGKEY "Software\\Wine\\Crypto\\RSA\\%s"
159 #define RSAENH_MIN(a,b) ((a)<(b)?(a):(b))
160 /******************************************************************************
161 * aProvEnumAlgsEx - Defines the capabilities of the CSP personalities.
163 #define RSAENH_MAX_ENUMALGS 24
164 #define RSAENH_PCT1_SSL2_SSL3_TLS1 (CRYPT_FLAG_PCT1|CRYPT_FLAG_SSL2|CRYPT_FLAG_SSL3|CRYPT_FLAG_TLS1)
165 static const PROV_ENUMALGS_EX aProvEnumAlgsEx[5][RSAENH_MAX_ENUMALGS+1] =
168 {CALG_RC2, 40, 40, 56,0, 4,"RC2", 24,"RSA Data Security's RC2"},
169 {CALG_RC4, 40, 40, 56,0, 4,"RC4", 24,"RSA Data Security's RC4"},
170 {CALG_DES, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
171 {CALG_SHA, 160,160, 160,CRYPT_FLAG_SIGNING, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
172 {CALG_MD2, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD2", 23,"Message Digest 2 (MD2)"},
173 {CALG_MD4, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD4", 23,"Message Digest 4 (MD4)"},
174 {CALG_MD5, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD5", 23,"Message Digest 5 (MD5)"},
175 {CALG_SSL3_SHAMD5,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
176 {CALG_MAC, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
177 {CALG_RSA_SIGN, 512,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_SIGN",14,"RSA Signature"},
178 {CALG_RSA_KEYX, 512,384, 1024,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_KEYX",17,"RSA Key Exchange"},
179 {CALG_HMAC, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
180 {0, 0, 0, 0,0, 1,"", 1,""}
183 {CALG_RC2, 128, 40, 128,0, 4,"RC2", 24,"RSA Data Security's RC2"},
184 {CALG_RC4, 128, 40, 128,0, 4,"RC4", 24,"RSA Data Security's RC4"},
185 {CALG_DES, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
186 {CALG_3DES_112, 112,112, 112,0, 13,"3DES TWO KEY",19,"Two Key Triple DES"},
187 {CALG_3DES, 168,168, 168,0, 5,"3DES", 21,"Three Key Triple DES"},
188 {CALG_SHA, 160,160, 160,CRYPT_FLAG_SIGNING, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
189 {CALG_MD2, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD2", 23,"Message Digest 2 (MD2)"},
190 {CALG_MD4, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD4", 23,"Message Digest 4 (MD4)"},
191 {CALG_MD5, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD5", 23,"Message Digest 5 (MD5)"},
192 {CALG_SSL3_SHAMD5,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
193 {CALG_MAC, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
194 {CALG_RSA_SIGN,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_SIGN",14,"RSA Signature"},
195 {CALG_RSA_KEYX,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_KEYX",17,"RSA Key Exchange"},
196 {CALG_HMAC, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
197 {0, 0, 0, 0,0, 1,"", 1,""}
200 {CALG_RC2, 128, 40, 128,0, 4,"RC2", 24,"RSA Data Security's RC2"},
201 {CALG_RC4, 128, 40, 128,0, 4,"RC4", 24,"RSA Data Security's RC4"},
202 {CALG_DES, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
203 {CALG_3DES_112, 112,112, 112,0, 13,"3DES TWO KEY",19,"Two Key Triple DES"},
204 {CALG_3DES, 168,168, 168,0, 5,"3DES", 21,"Three Key Triple DES"},
205 {CALG_SHA, 160,160, 160,CRYPT_FLAG_SIGNING, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
206 {CALG_MD2, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD2", 23,"Message Digest 2 (MD2)"},
207 {CALG_MD4, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD4", 23,"Message Digest 4 (MD4)"},
208 {CALG_MD5, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD5", 23,"Message Digest 5 (MD5)"},
209 {CALG_SSL3_SHAMD5,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
210 {CALG_MAC, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
211 {CALG_RSA_SIGN,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_SIGN",14,"RSA Signature"},
212 {CALG_RSA_KEYX,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_KEYX",17,"RSA Key Exchange"},
213 {CALG_HMAC, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
214 {0, 0, 0, 0,0, 1,"", 1,""}
217 {CALG_RC2, 128, 40, 128,RSAENH_PCT1_SSL2_SSL3_TLS1, 4,"RC2", 24,"RSA Data Security's RC2"},
218 {CALG_RC4, 128, 40, 128,RSAENH_PCT1_SSL2_SSL3_TLS1, 4,"RC4", 24,"RSA Data Security's RC4"},
219 {CALG_DES, 56, 56, 56,RSAENH_PCT1_SSL2_SSL3_TLS1, 4,"DES", 31,"Data Encryption Standard (DES)"},
220 {CALG_3DES_112, 112,112, 112,RSAENH_PCT1_SSL2_SSL3_TLS1,13,"3DES TWO KEY",19,"Two Key Triple DES"},
221 {CALG_3DES, 168,168, 168,RSAENH_PCT1_SSL2_SSL3_TLS1, 5,"3DES", 21,"Three Key Triple DES"},
222 {CALG_SHA,160,160,160,CRYPT_FLAG_SIGNING|RSAENH_PCT1_SSL2_SSL3_TLS1,6,"SHA-1",30,"Secure Hash Algorithm (SHA-1)"},
223 {CALG_MD5,128,128,128,CRYPT_FLAG_SIGNING|RSAENH_PCT1_SSL2_SSL3_TLS1,4,"MD5",23,"Message Digest 5 (MD5)"},
224 {CALG_SSL3_SHAMD5,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
225 {CALG_MAC, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
226 {CALG_RSA_SIGN,1024,384,16384,CRYPT_FLAG_SIGNING|RSAENH_PCT1_SSL2_SSL3_TLS1,9,"RSA_SIGN",14,"RSA Signature"},
227 {CALG_RSA_KEYX,1024,384,16384,CRYPT_FLAG_SIGNING|RSAENH_PCT1_SSL2_SSL3_TLS1,9,"RSA_KEYX",17,"RSA Key Exchange"},
228 {CALG_HMAC, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
229 {CALG_PCT1_MASTER,128,128,128,CRYPT_FLAG_PCT1, 12,"PCT1 MASTER",12,"PCT1 Master"},
230 {CALG_SSL2_MASTER,40,40, 192,CRYPT_FLAG_SSL2, 12,"SSL2 MASTER",12,"SSL2 Master"},
231 {CALG_SSL3_MASTER,384,384,384,CRYPT_FLAG_SSL3, 12,"SSL3 MASTER",12,"SSL3 Master"},
232 {CALG_TLS1_MASTER,384,384,384,CRYPT_FLAG_TLS1, 12,"TLS1 MASTER",12,"TLS1 Master"},
233 {CALG_SCHANNEL_MASTER_HASH,0,0,-1,0, 16,"SCH MASTER HASH",21,"SChannel Master Hash"},
234 {CALG_SCHANNEL_MAC_KEY,0,0,-1,0, 12,"SCH MAC KEY",17,"SChannel MAC Key"},
235 {CALG_SCHANNEL_ENC_KEY,0,0,-1,0, 12,"SCH ENC KEY",24,"SChannel Encryption Key"},
236 {CALG_TLS1PRF, 0, 0, -1,0, 9,"TLS1 PRF", 28,"TLS1 Pseudo Random Function"},
237 {0, 0, 0, 0,0, 1,"", 1,""}
240 {CALG_RC2, 128, 40, 128,0, 4,"RC2", 24,"RSA Data Security's RC2"},
241 {CALG_RC4, 128, 40, 128,0, 4,"RC4", 24,"RSA Data Security's RC4"},
242 {CALG_DES, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
243 {CALG_3DES_112, 112,112, 112,0, 13,"3DES TWO KEY",19,"Two Key Triple DES"},
244 {CALG_3DES, 168,168, 168,0, 5,"3DES", 21,"Three Key Triple DES"},
245 {CALG_AES, 128,128, 128,0, 4,"AES", 35,"Advanced Encryption Standard (AES)"},
246 {CALG_AES_128, 128,128, 128,0, 8,"AES-128", 39,"Advanced Encryption Standard (AES-128)"},
247 {CALG_AES_192, 192,192, 192,0, 8,"AES-192", 39,"Advanced Encryption Standard (AES-192)"},
248 {CALG_AES_256, 256,256, 256,0, 8,"AES-256", 39,"Advanced Encryption Standard (AES-256)"},
249 {CALG_SHA, 160,160, 160,CRYPT_FLAG_SIGNING, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
250 {CALG_SHA_256, 256,256, 256,CRYPT_FLAG_SIGNING, 6,"SHA-256", 30,"Secure Hash Algorithm (SHA-256)"},
251 {CALG_SHA_384, 384,384, 384,CRYPT_FLAG_SIGNING, 6,"SHA-384", 30,"Secure Hash Algorithm (SHA-284)"},
252 {CALG_SHA_512, 512,512, 512,CRYPT_FLAG_SIGNING, 6,"SHA-512", 30,"Secure Hash Algorithm (SHA-512)"},
253 {CALG_MD2, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD2", 23,"Message Digest 2 (MD2)"},
254 {CALG_MD4, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD4", 23,"Message Digest 4 (MD4)"},
255 {CALG_MD5, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD5", 23,"Message Digest 5 (MD5)"},
256 {CALG_SSL3_SHAMD5,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
257 {CALG_MAC, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
258 {CALG_RSA_SIGN,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_SIGN",14,"RSA Signature"},
259 {CALG_RSA_KEYX,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_KEYX",17,"RSA Key Exchange"},
260 {CALG_HMAC, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
261 {0, 0, 0, 0,0, 1,"", 1,""}
265 /******************************************************************************
266 * API forward declarations
268 BOOL WINAPI
269 RSAENH_CPGetKeyParam(
270 HCRYPTPROV hProv,
271 HCRYPTKEY hKey,
272 DWORD dwParam,
273 BYTE *pbData,
274 DWORD *pdwDataLen,
275 DWORD dwFlags
278 BOOL WINAPI
279 RSAENH_CPEncrypt(
280 HCRYPTPROV hProv,
281 HCRYPTKEY hKey,
282 HCRYPTHASH hHash,
283 BOOL Final,
284 DWORD dwFlags,
285 BYTE *pbData,
286 DWORD *pdwDataLen,
287 DWORD dwBufLen
290 BOOL WINAPI
291 RSAENH_CPCreateHash(
292 HCRYPTPROV hProv,
293 ALG_ID Algid,
294 HCRYPTKEY hKey,
295 DWORD dwFlags,
296 HCRYPTHASH *phHash
299 BOOL WINAPI
300 RSAENH_CPSetHashParam(
301 HCRYPTPROV hProv,
302 HCRYPTHASH hHash,
303 DWORD dwParam,
304 BYTE *pbData, DWORD dwFlags
307 BOOL WINAPI
308 RSAENH_CPGetHashParam(
309 HCRYPTPROV hProv,
310 HCRYPTHASH hHash,
311 DWORD dwParam,
312 BYTE *pbData,
313 DWORD *pdwDataLen,
314 DWORD dwFlags
317 BOOL WINAPI
318 RSAENH_CPDestroyHash(
319 HCRYPTPROV hProv,
320 HCRYPTHASH hHash
323 static BOOL crypt_export_key(
324 CRYPTKEY *pCryptKey,
325 HCRYPTKEY hPubKey,
326 DWORD dwBlobType,
327 DWORD dwFlags,
328 BOOL force,
329 BYTE *pbData,
330 DWORD *pdwDataLen
333 static BOOL import_key(
334 HCRYPTPROV hProv,
335 const BYTE *pbData,
336 DWORD dwDataLen,
337 HCRYPTKEY hPubKey,
338 DWORD dwFlags,
339 BOOL fStoreKey,
340 HCRYPTKEY *phKey
343 BOOL WINAPI
344 RSAENH_CPHashData(
345 HCRYPTPROV hProv,
346 HCRYPTHASH hHash,
347 const BYTE *pbData,
348 DWORD dwDataLen,
349 DWORD dwFlags
352 /******************************************************************************
353 * CSP's handle table (used by all acquired key containers)
355 static struct handle_table handle_table;
357 /******************************************************************************
358 * DllMain (RSAENH.@)
360 * Initializes and destroys the handle table for the CSP's handles.
362 BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD fdwReason, PVOID reserved)
364 switch (fdwReason)
366 case DLL_PROCESS_ATTACH:
367 instance = hInstance;
368 DisableThreadLibraryCalls(hInstance);
369 init_handle_table(&handle_table);
370 break;
372 case DLL_PROCESS_DETACH:
373 if (reserved) break;
374 destroy_handle_table(&handle_table);
375 break;
377 return TRUE;
380 /******************************************************************************
381 * copy_param [Internal]
383 * Helper function that supports the standard WINAPI protocol for querying data
384 * of dynamic size.
386 * PARAMS
387 * pbBuffer [O] Buffer where the queried parameter is copied to, if it is large enough.
388 * May be NUL if the required buffer size is to be queried only.
389 * pdwBufferSize [I/O] In: Size of the buffer at pbBuffer
390 * Out: Size of parameter pbParam
391 * pbParam [I] Parameter value.
392 * dwParamSize [I] Size of pbParam
394 * RETURN
395 * Success: TRUE (pbParam was copied into pbBuffer or pbBuffer is NULL)
396 * Failure: FALSE (pbBuffer is not large enough to hold pbParam). Last error: ERROR_MORE_DATA
398 static inline BOOL copy_param(BYTE *pbBuffer, DWORD *pdwBufferSize, const BYTE *pbParam,
399 DWORD dwParamSize)
401 if (pbBuffer)
403 if (dwParamSize > *pdwBufferSize)
405 SetLastError(ERROR_MORE_DATA);
406 *pdwBufferSize = dwParamSize;
407 return FALSE;
409 memcpy(pbBuffer, pbParam, dwParamSize);
411 *pdwBufferSize = dwParamSize;
412 return TRUE;
415 /******************************************************************************
416 * get_algid_info [Internal]
418 * Query CSP capabilities for a given crypto algorithm.
420 * PARAMS
421 * hProv [I] Handle to a key container of the CSP whose capabilities are to be queried.
422 * algid [I] Identifier of the crypto algorithm about which information is requested.
424 * RETURNS
425 * Success: Pointer to a PROV_ENUMALGS_EX struct containing information about the crypto algorithm.
426 * Failure: NULL (algid not supported)
428 static inline const PROV_ENUMALGS_EX* get_algid_info(HCRYPTPROV hProv, ALG_ID algid) {
429 const PROV_ENUMALGS_EX *iterator;
430 KEYCONTAINER *pKeyContainer;
432 if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER, (OBJECTHDR**)&pKeyContainer)) {
433 SetLastError(NTE_BAD_UID);
434 return NULL;
437 for (iterator = aProvEnumAlgsEx[pKeyContainer->dwPersonality]; iterator->aiAlgid; iterator++) {
438 if (iterator->aiAlgid == algid) return iterator;
441 SetLastError(NTE_BAD_ALGID);
442 return NULL;
445 /******************************************************************************
446 * copy_data_blob [Internal]
448 * deeply copies a DATA_BLOB
450 * PARAMS
451 * dst [O] That's where the blob will be copied to
452 * src [I] Source blob
454 * RETURNS
455 * Success: TRUE
456 * Failure: FALSE (GetLastError() == NTE_NO_MEMORY
458 * NOTES
459 * Use free_data_blob to release resources occupied by copy_data_blob.
461 static inline BOOL copy_data_blob(PCRYPT_DATA_BLOB dst, const PCRYPT_DATA_BLOB src)
463 dst->pbData = HeapAlloc(GetProcessHeap(), 0, src->cbData);
464 if (!dst->pbData) {
465 SetLastError(NTE_NO_MEMORY);
466 return FALSE;
468 dst->cbData = src->cbData;
469 memcpy(dst->pbData, src->pbData, src->cbData);
470 return TRUE;
473 /******************************************************************************
474 * concat_data_blobs [Internal]
476 * Concatenates two blobs
478 * PARAMS
479 * dst [O] The new blob will be copied here
480 * src1 [I] Prefix blob
481 * src2 [I] Appendix blob
483 * RETURNS
484 * Success: TRUE
485 * Failure: FALSE (GetLastError() == NTE_NO_MEMORY)
487 * NOTES
488 * Release resources occupied by concat_data_blobs with free_data_blobs
490 static inline BOOL concat_data_blobs(PCRYPT_DATA_BLOB dst, const PCRYPT_DATA_BLOB src1,
491 const PCRYPT_DATA_BLOB src2)
493 dst->cbData = src1->cbData + src2->cbData;
494 dst->pbData = HeapAlloc(GetProcessHeap(), 0, dst->cbData);
495 if (!dst->pbData) {
496 SetLastError(NTE_NO_MEMORY);
497 return FALSE;
499 memcpy(dst->pbData, src1->pbData, src1->cbData);
500 memcpy(dst->pbData + src1->cbData, src2->pbData, src2->cbData);
501 return TRUE;
504 /******************************************************************************
505 * free_data_blob [Internal]
507 * releases resource occupied by a dynamically allocated CRYPT_DATA_BLOB
509 * PARAMS
510 * pBlob [I] Heap space occupied by pBlob->pbData is released
512 static inline void free_data_blob(PCRYPT_DATA_BLOB pBlob) {
513 HeapFree(GetProcessHeap(), 0, pBlob->pbData);
516 /******************************************************************************
517 * init_data_blob [Internal]
519 static inline void init_data_blob(PCRYPT_DATA_BLOB pBlob) {
520 pBlob->pbData = NULL;
521 pBlob->cbData = 0;
524 /******************************************************************************
525 * free_hmac_info [Internal]
527 * Deeply free an HMAC_INFO struct.
529 * PARAMS
530 * hmac_info [I] Pointer to the HMAC_INFO struct to be freed.
532 * NOTES
533 * See Internet RFC 2104 for details on the HMAC algorithm.
535 static inline void free_hmac_info(PHMAC_INFO hmac_info) {
536 if (!hmac_info) return;
537 HeapFree(GetProcessHeap(), 0, hmac_info->pbInnerString);
538 HeapFree(GetProcessHeap(), 0, hmac_info->pbOuterString);
539 HeapFree(GetProcessHeap(), 0, hmac_info);
542 /******************************************************************************
543 * copy_hmac_info [Internal]
545 * Deeply copy an HMAC_INFO struct
547 * PARAMS
548 * dst [O] Pointer to a location where the pointer to the HMAC_INFO copy will be stored.
549 * src [I] Pointer to the HMAC_INFO struct to be copied.
551 * RETURNS
552 * Success: TRUE
553 * Failure: FALSE
555 * NOTES
556 * See Internet RFC 2104 for details on the HMAC algorithm.
558 static BOOL copy_hmac_info(PHMAC_INFO *dst, const HMAC_INFO *src) {
559 if (!src) return FALSE;
560 *dst = HeapAlloc(GetProcessHeap(), 0, sizeof(HMAC_INFO));
561 if (!*dst) return FALSE;
562 **dst = *src;
563 (*dst)->pbInnerString = NULL;
564 (*dst)->pbOuterString = NULL;
565 if ((*dst)->cbInnerString == 0) (*dst)->cbInnerString = RSAENH_HMAC_DEF_PAD_LEN;
566 (*dst)->pbInnerString = HeapAlloc(GetProcessHeap(), 0, (*dst)->cbInnerString);
567 if (!(*dst)->pbInnerString) {
568 free_hmac_info(*dst);
569 return FALSE;
571 if (src->cbInnerString)
572 memcpy((*dst)->pbInnerString, src->pbInnerString, src->cbInnerString);
573 else
574 memset((*dst)->pbInnerString, RSAENH_HMAC_DEF_IPAD_CHAR, RSAENH_HMAC_DEF_PAD_LEN);
575 if ((*dst)->cbOuterString == 0) (*dst)->cbOuterString = RSAENH_HMAC_DEF_PAD_LEN;
576 (*dst)->pbOuterString = HeapAlloc(GetProcessHeap(), 0, (*dst)->cbOuterString);
577 if (!(*dst)->pbOuterString) {
578 free_hmac_info(*dst);
579 return FALSE;
581 if (src->cbOuterString)
582 memcpy((*dst)->pbOuterString, src->pbOuterString, src->cbOuterString);
583 else
584 memset((*dst)->pbOuterString, RSAENH_HMAC_DEF_OPAD_CHAR, RSAENH_HMAC_DEF_PAD_LEN);
585 return TRUE;
588 /******************************************************************************
589 * destroy_hash [Internal]
591 * Destructor for hash objects
593 * PARAMS
594 * pCryptHash [I] Pointer to the hash object to be destroyed.
595 * Will be invalid after function returns!
597 static void destroy_hash(OBJECTHDR *pObject)
599 CRYPTHASH *pCryptHash = (CRYPTHASH*)pObject;
601 free_hmac_info(pCryptHash->pHMACInfo);
602 free_data_blob(&pCryptHash->tpPRFParams.blobLabel);
603 free_data_blob(&pCryptHash->tpPRFParams.blobSeed);
604 HeapFree(GetProcessHeap(), 0, pCryptHash);
607 /******************************************************************************
608 * init_hash [Internal]
610 * Initialize (or reset) a hash object
612 * PARAMS
613 * pCryptHash [I] The hash object to be initialized.
615 static inline BOOL init_hash(CRYPTHASH *pCryptHash) {
616 DWORD dwLen;
618 switch (pCryptHash->aiAlgid)
620 case CALG_HMAC:
621 if (pCryptHash->pHMACInfo) {
622 const PROV_ENUMALGS_EX *pAlgInfo;
624 pAlgInfo = get_algid_info(pCryptHash->hProv, pCryptHash->pHMACInfo->HashAlgid);
625 if (!pAlgInfo) return FALSE;
626 pCryptHash->dwHashSize = pAlgInfo->dwDefaultLen >> 3;
627 init_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context);
628 update_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
629 pCryptHash->pHMACInfo->pbInnerString,
630 pCryptHash->pHMACInfo->cbInnerString);
632 return TRUE;
634 case CALG_MAC:
635 dwLen = sizeof(DWORD);
636 RSAENH_CPGetKeyParam(pCryptHash->hProv, pCryptHash->hKey, KP_BLOCKLEN,
637 (BYTE*)&pCryptHash->dwHashSize, &dwLen, 0);
638 pCryptHash->dwHashSize >>= 3;
639 return TRUE;
641 default:
642 return init_hash_impl(pCryptHash->aiAlgid, &pCryptHash->context);
646 /******************************************************************************
647 * update_hash [Internal]
649 * Hashes the given data and updates the hash object's state accordingly
651 * PARAMS
652 * pCryptHash [I] Hash object to be updated.
653 * pbData [I] Pointer to data stream to be hashed.
654 * dwDataLen [I] Length of data stream.
656 static inline void update_hash(CRYPTHASH *pCryptHash, const BYTE *pbData, DWORD dwDataLen)
658 BYTE *pbTemp;
660 switch (pCryptHash->aiAlgid)
662 case CALG_HMAC:
663 if (pCryptHash->pHMACInfo)
664 update_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
665 pbData, dwDataLen);
666 break;
668 case CALG_MAC:
669 pbTemp = HeapAlloc(GetProcessHeap(), 0, dwDataLen);
670 if (!pbTemp) return;
671 memcpy(pbTemp, pbData, dwDataLen);
672 RSAENH_CPEncrypt(pCryptHash->hProv, pCryptHash->hKey, 0, FALSE, 0,
673 pbTemp, &dwDataLen, dwDataLen);
674 HeapFree(GetProcessHeap(), 0, pbTemp);
675 break;
677 default:
678 update_hash_impl(pCryptHash->aiAlgid, &pCryptHash->context, pbData, dwDataLen);
682 /******************************************************************************
683 * finalize_hash [Internal]
685 * Finalizes the hash, after all data has been hashed with update_hash.
686 * No additional data can be hashed afterwards until the hash gets initialized again.
688 * PARAMS
689 * pCryptHash [I] Hash object to be finalized.
691 static inline void finalize_hash(CRYPTHASH *pCryptHash) {
692 DWORD dwDataLen;
694 switch (pCryptHash->aiAlgid)
696 case CALG_HMAC:
697 if (pCryptHash->pHMACInfo) {
698 BYTE abHashValue[RSAENH_MAX_HASH_SIZE];
700 finalize_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
701 pCryptHash->abHashValue);
702 memcpy(abHashValue, pCryptHash->abHashValue, pCryptHash->dwHashSize);
703 init_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context);
704 update_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
705 pCryptHash->pHMACInfo->pbOuterString,
706 pCryptHash->pHMACInfo->cbOuterString);
707 update_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
708 abHashValue, pCryptHash->dwHashSize);
709 finalize_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
710 pCryptHash->abHashValue);
712 break;
714 case CALG_MAC:
715 dwDataLen = 0;
716 RSAENH_CPEncrypt(pCryptHash->hProv, pCryptHash->hKey, 0, TRUE, 0,
717 pCryptHash->abHashValue, &dwDataLen, pCryptHash->dwHashSize);
718 break;
720 default:
721 finalize_hash_impl(pCryptHash->aiAlgid, &pCryptHash->context, pCryptHash->abHashValue);
725 /******************************************************************************
726 * destroy_key [Internal]
728 * Destructor for key objects
730 * PARAMS
731 * pCryptKey [I] Pointer to the key object to be destroyed.
732 * Will be invalid after function returns!
734 static void destroy_key(OBJECTHDR *pObject)
736 CRYPTKEY *pCryptKey = (CRYPTKEY*)pObject;
738 free_key_impl(pCryptKey->aiAlgid, &pCryptKey->context);
739 free_data_blob(&pCryptKey->siSChannelInfo.blobClientRandom);
740 free_data_blob(&pCryptKey->siSChannelInfo.blobServerRandom);
741 free_data_blob(&pCryptKey->blobHmacKey);
742 HeapFree(GetProcessHeap(), 0, pCryptKey);
745 /******************************************************************************
746 * setup_key [Internal]
748 * Initialize (or reset) a key object
750 * PARAMS
751 * pCryptKey [I] The key object to be initialized.
753 static inline void setup_key(CRYPTKEY *pCryptKey) {
754 pCryptKey->dwState = RSAENH_KEYSTATE_IDLE;
755 memcpy(pCryptKey->abChainVector, pCryptKey->abInitVector, sizeof(pCryptKey->abChainVector));
756 setup_key_impl(pCryptKey->aiAlgid, &pCryptKey->context, pCryptKey->dwKeyLen,
757 pCryptKey->dwEffectiveKeyLen, pCryptKey->dwSaltLen,
758 pCryptKey->abKeyValue);
761 /******************************************************************************
762 * new_key [Internal]
764 * Creates a new key object without assigning the actual binary key value.
765 * This is done by CPDeriveKey, CPGenKey or CPImportKey, which call this function.
767 * PARAMS
768 * hProv [I] Handle to the provider to which the created key will belong.
769 * aiAlgid [I] The new key shall use the crypto algorithm identified by aiAlgid.
770 * dwFlags [I] Upper 16 bits give the key length.
771 * Lower 16 bits: CRYPT_EXPORTABLE, CRYPT_CREATE_SALT,
772 * CRYPT_NO_SALT
773 * ppCryptKey [O] Pointer to the created key
775 * RETURNS
776 * Success: Handle to the created key.
777 * Failure: INVALID_HANDLE_VALUE
779 static HCRYPTKEY new_key(HCRYPTPROV hProv, ALG_ID aiAlgid, DWORD dwFlags, CRYPTKEY **ppCryptKey)
781 HCRYPTKEY hCryptKey;
782 CRYPTKEY *pCryptKey;
783 DWORD dwKeyLen = HIWORD(dwFlags);
784 const PROV_ENUMALGS_EX *peaAlgidInfo;
786 *ppCryptKey = NULL;
789 * Retrieve the CSP's capabilities for the given ALG_ID value
791 peaAlgidInfo = get_algid_info(hProv, aiAlgid);
792 if (!peaAlgidInfo) return (HCRYPTKEY)INVALID_HANDLE_VALUE;
794 TRACE("alg = %s, dwKeyLen = %d\n", debugstr_a(peaAlgidInfo->szName),
795 dwKeyLen);
797 * Assume the default key length, if none is specified explicitly
799 if (dwKeyLen == 0) dwKeyLen = peaAlgidInfo->dwDefaultLen;
802 * Check if the requested key length is supported by the current CSP.
803 * Adjust key length's for DES algorithms.
805 switch (aiAlgid) {
806 case CALG_DES:
807 if (dwKeyLen == RSAENH_DES_EFFECTIVE_KEYLEN) {
808 dwKeyLen = RSAENH_DES_STORAGE_KEYLEN;
810 if (dwKeyLen != RSAENH_DES_STORAGE_KEYLEN) {
811 SetLastError(NTE_BAD_FLAGS);
812 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
814 break;
816 case CALG_3DES_112:
817 if (dwKeyLen == RSAENH_3DES112_EFFECTIVE_KEYLEN) {
818 dwKeyLen = RSAENH_3DES112_STORAGE_KEYLEN;
820 if (dwKeyLen != RSAENH_3DES112_STORAGE_KEYLEN) {
821 SetLastError(NTE_BAD_FLAGS);
822 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
824 break;
826 case CALG_3DES:
827 if (dwKeyLen == RSAENH_3DES_EFFECTIVE_KEYLEN) {
828 dwKeyLen = RSAENH_3DES_STORAGE_KEYLEN;
830 if (dwKeyLen != RSAENH_3DES_STORAGE_KEYLEN) {
831 SetLastError(NTE_BAD_FLAGS);
832 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
834 break;
836 case CALG_HMAC:
837 /* Avoid the key length check for HMAC keys, which have unlimited
838 * length.
840 break;
842 default:
843 if (dwKeyLen % 8 ||
844 dwKeyLen > peaAlgidInfo->dwMaxLen ||
845 dwKeyLen < peaAlgidInfo->dwMinLen)
847 TRACE("key len %d out of bounds (%d, %d)\n", dwKeyLen,
848 peaAlgidInfo->dwMinLen, peaAlgidInfo->dwMaxLen);
849 SetLastError(NTE_BAD_DATA);
850 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
854 hCryptKey = new_object(&handle_table, sizeof(CRYPTKEY), RSAENH_MAGIC_KEY,
855 destroy_key, (OBJECTHDR**)&pCryptKey);
856 if (hCryptKey != (HCRYPTKEY)INVALID_HANDLE_VALUE)
858 pCryptKey->aiAlgid = aiAlgid;
859 pCryptKey->hProv = hProv;
860 pCryptKey->dwModeBits = 0;
861 pCryptKey->dwPermissions = CRYPT_ENCRYPT | CRYPT_DECRYPT | CRYPT_READ | CRYPT_WRITE |
862 CRYPT_MAC;
863 if (dwFlags & CRYPT_EXPORTABLE)
864 pCryptKey->dwPermissions |= CRYPT_EXPORT;
865 pCryptKey->dwKeyLen = dwKeyLen >> 3;
866 pCryptKey->dwEffectiveKeyLen = 0;
867 if ((dwFlags & CRYPT_CREATE_SALT) || (dwKeyLen == 40 && !(dwFlags & CRYPT_NO_SALT)))
868 pCryptKey->dwSaltLen = 16 /*FIXME*/ - pCryptKey->dwKeyLen;
869 else
870 pCryptKey->dwSaltLen = 0;
871 memset(pCryptKey->abKeyValue, 0, sizeof(pCryptKey->abKeyValue));
872 memset(pCryptKey->abInitVector, 0, sizeof(pCryptKey->abInitVector));
873 memset(&pCryptKey->siSChannelInfo.saEncAlg, 0, sizeof(pCryptKey->siSChannelInfo.saEncAlg));
874 memset(&pCryptKey->siSChannelInfo.saMACAlg, 0, sizeof(pCryptKey->siSChannelInfo.saMACAlg));
875 init_data_blob(&pCryptKey->siSChannelInfo.blobClientRandom);
876 init_data_blob(&pCryptKey->siSChannelInfo.blobServerRandom);
877 init_data_blob(&pCryptKey->blobHmacKey);
879 switch(aiAlgid)
881 case CALG_PCT1_MASTER:
882 case CALG_SSL2_MASTER:
883 case CALG_SSL3_MASTER:
884 case CALG_TLS1_MASTER:
885 case CALG_RC4:
886 pCryptKey->dwBlockLen = 0;
887 pCryptKey->dwMode = 0;
888 break;
890 case CALG_RC2:
891 case CALG_DES:
892 case CALG_3DES_112:
893 case CALG_3DES:
894 pCryptKey->dwBlockLen = 8;
895 pCryptKey->dwMode = CRYPT_MODE_CBC;
896 break;
898 case CALG_AES:
899 case CALG_AES_128:
900 case CALG_AES_192:
901 case CALG_AES_256:
902 pCryptKey->dwBlockLen = 16;
903 pCryptKey->dwMode = CRYPT_MODE_ECB;
904 break;
906 case CALG_RSA_KEYX:
907 case CALG_RSA_SIGN:
908 pCryptKey->dwBlockLen = dwKeyLen >> 3;
909 pCryptKey->dwMode = 0;
910 break;
912 case CALG_HMAC:
913 pCryptKey->dwBlockLen = 0;
914 pCryptKey->dwMode = 0;
915 break;
918 *ppCryptKey = pCryptKey;
921 return hCryptKey;
924 /******************************************************************************
925 * map_key_spec_to_key_pair_name [Internal]
927 * Returns the name of the registry value associated with a key spec.
929 * PARAMS
930 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
932 * RETURNS
933 * Success: Name of registry value.
934 * Failure: NULL
936 static LPCSTR map_key_spec_to_key_pair_name(DWORD dwKeySpec)
938 LPCSTR szValueName;
940 switch (dwKeySpec)
942 case AT_KEYEXCHANGE:
943 szValueName = "KeyExchangeKeyPair";
944 break;
945 case AT_SIGNATURE:
946 szValueName = "SignatureKeyPair";
947 break;
948 default:
949 WARN("invalid key spec %d\n", dwKeySpec);
950 szValueName = NULL;
952 return szValueName;
955 /******************************************************************************
956 * store_key_pair [Internal]
958 * Stores a key pair to the registry
960 * PARAMS
961 * hCryptKey [I] Handle to the key to be stored
962 * hKey [I] Registry key where the key pair is to be stored
963 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
964 * dwFlags [I] Flags for protecting the key
966 static void store_key_pair(HCRYPTKEY hCryptKey, HKEY hKey, DWORD dwKeySpec, DWORD dwFlags)
968 LPCSTR szValueName;
969 DATA_BLOB blobIn, blobOut;
970 CRYPTKEY *pKey;
971 DWORD dwLen;
972 BYTE *pbKey;
974 if (!(szValueName = map_key_spec_to_key_pair_name(dwKeySpec)))
975 return;
976 if (lookup_handle(&handle_table, hCryptKey, RSAENH_MAGIC_KEY,
977 (OBJECTHDR**)&pKey))
979 if (crypt_export_key(pKey, 0, PRIVATEKEYBLOB, 0, TRUE, 0, &dwLen))
981 pbKey = HeapAlloc(GetProcessHeap(), 0, dwLen);
982 if (pbKey)
984 if (crypt_export_key(pKey, 0, PRIVATEKEYBLOB, 0, TRUE, pbKey,
985 &dwLen))
987 blobIn.pbData = pbKey;
988 blobIn.cbData = dwLen;
990 if (CryptProtectData(&blobIn, NULL, NULL, NULL, NULL,
991 dwFlags, &blobOut))
993 RegSetValueExA(hKey, szValueName, 0, REG_BINARY,
994 blobOut.pbData, blobOut.cbData);
995 LocalFree(blobOut.pbData);
998 HeapFree(GetProcessHeap(), 0, pbKey);
1004 /******************************************************************************
1005 * map_key_spec_to_permissions_name [Internal]
1007 * Returns the name of the registry value associated with the permissions for
1008 * a key spec.
1010 * PARAMS
1011 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
1013 * RETURNS
1014 * Success: Name of registry value.
1015 * Failure: NULL
1017 static LPCSTR map_key_spec_to_permissions_name(DWORD dwKeySpec)
1019 LPCSTR szValueName;
1021 switch (dwKeySpec)
1023 case AT_KEYEXCHANGE:
1024 szValueName = "KeyExchangePermissions";
1025 break;
1026 case AT_SIGNATURE:
1027 szValueName = "SignaturePermissions";
1028 break;
1029 default:
1030 WARN("invalid key spec %d\n", dwKeySpec);
1031 szValueName = NULL;
1033 return szValueName;
1036 /******************************************************************************
1037 * store_key_permissions [Internal]
1039 * Stores a key's permissions to the registry
1041 * PARAMS
1042 * hCryptKey [I] Handle to the key whose permissions are to be stored
1043 * hKey [I] Registry key where the key permissions are to be stored
1044 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
1046 static void store_key_permissions(HCRYPTKEY hCryptKey, HKEY hKey, DWORD dwKeySpec)
1048 LPCSTR szValueName;
1049 CRYPTKEY *pKey;
1051 if (!(szValueName = map_key_spec_to_permissions_name(dwKeySpec)))
1052 return;
1053 if (lookup_handle(&handle_table, hCryptKey, RSAENH_MAGIC_KEY,
1054 (OBJECTHDR**)&pKey))
1055 RegSetValueExA(hKey, szValueName, 0, REG_DWORD,
1056 (BYTE *)&pKey->dwPermissions,
1057 sizeof(pKey->dwPermissions));
1060 /******************************************************************************
1061 * create_container_key [Internal]
1063 * Creates the registry key for a key container's persistent storage.
1065 * PARAMS
1066 * pKeyContainer [I] Pointer to the key container
1067 * sam [I] Desired registry access
1068 * phKey [O] Returned key
1070 static BOOL create_container_key(KEYCONTAINER *pKeyContainer, REGSAM sam, HKEY *phKey)
1072 CHAR szRSABase[MAX_PATH];
1073 HKEY hRootKey;
1075 sprintf(szRSABase, RSAENH_REGKEY, pKeyContainer->szName);
1077 if (pKeyContainer->dwFlags & CRYPT_MACHINE_KEYSET)
1078 hRootKey = HKEY_LOCAL_MACHINE;
1079 else
1080 hRootKey = HKEY_CURRENT_USER;
1082 /* @@ Wine registry key: HKLM\Software\Wine\Crypto\RSA */
1083 /* @@ Wine registry key: HKCU\Software\Wine\Crypto\RSA */
1084 return RegCreateKeyExA(hRootKey, szRSABase, 0, NULL,
1085 REG_OPTION_NON_VOLATILE, sam, NULL, phKey, NULL)
1086 == ERROR_SUCCESS;
1089 /******************************************************************************
1090 * open_container_key [Internal]
1092 * Opens a key container's persistent storage for reading.
1094 * PARAMS
1095 * pszContainerName [I] Name of the container to be opened. May be the empty
1096 * string if the parent key of all containers is to be
1097 * opened.
1098 * dwFlags [I] Flags indicating which keyset to be opened.
1099 * phKey [O] Returned key
1101 static BOOL open_container_key(LPCSTR pszContainerName, DWORD dwFlags, REGSAM access, HKEY *phKey)
1103 CHAR szRSABase[MAX_PATH];
1104 HKEY hRootKey;
1106 sprintf(szRSABase, RSAENH_REGKEY, pszContainerName);
1108 if (dwFlags & CRYPT_MACHINE_KEYSET)
1109 hRootKey = HKEY_LOCAL_MACHINE;
1110 else
1111 hRootKey = HKEY_CURRENT_USER;
1113 /* @@ Wine registry key: HKLM\Software\Wine\Crypto\RSA */
1114 /* @@ Wine registry key: HKCU\Software\Wine\Crypto\RSA */
1115 return RegOpenKeyExA(hRootKey, szRSABase, 0, access, phKey) ==
1116 ERROR_SUCCESS;
1119 /******************************************************************************
1120 * delete_container_key [Internal]
1122 * Deletes a key container's persistent storage.
1124 * PARAMS
1125 * pszContainerName [I] Name of the container to be opened.
1126 * dwFlags [I] Flags indicating which keyset to be opened.
1128 static BOOL delete_container_key(LPCSTR pszContainerName, DWORD dwFlags)
1130 CHAR szRegKey[MAX_PATH];
1132 if (snprintf(szRegKey, MAX_PATH, RSAENH_REGKEY, pszContainerName) >= MAX_PATH) {
1133 SetLastError(NTE_BAD_KEYSET_PARAM);
1134 return FALSE;
1135 } else {
1136 HKEY hRootKey;
1137 if (dwFlags & CRYPT_MACHINE_KEYSET)
1138 hRootKey = HKEY_LOCAL_MACHINE;
1139 else
1140 hRootKey = HKEY_CURRENT_USER;
1141 if (!RegDeleteKeyA(hRootKey, szRegKey)) {
1142 SetLastError(ERROR_SUCCESS);
1143 return TRUE;
1144 } else {
1145 SetLastError(NTE_BAD_KEYSET);
1146 return FALSE;
1151 /******************************************************************************
1152 * store_key_container_keys [Internal]
1154 * Stores key container's keys in a persistent location.
1156 * PARAMS
1157 * pKeyContainer [I] Pointer to the key container whose keys are to be saved
1159 static void store_key_container_keys(KEYCONTAINER *pKeyContainer)
1161 HKEY hKey;
1162 DWORD dwFlags;
1164 /* On WinXP, persistent keys are stored in a file located at:
1165 * $AppData$\\Microsoft\\Crypto\\RSA\\$SID$\\some_hex_string
1168 if (pKeyContainer->dwFlags & CRYPT_MACHINE_KEYSET)
1169 dwFlags = CRYPTPROTECT_LOCAL_MACHINE;
1170 else
1171 dwFlags = 0;
1173 if (create_container_key(pKeyContainer, KEY_WRITE, &hKey))
1175 store_key_pair(pKeyContainer->hKeyExchangeKeyPair, hKey,
1176 AT_KEYEXCHANGE, dwFlags);
1177 store_key_pair(pKeyContainer->hSignatureKeyPair, hKey,
1178 AT_SIGNATURE, dwFlags);
1179 RegCloseKey(hKey);
1183 /******************************************************************************
1184 * store_key_container_permissions [Internal]
1186 * Stores key container's key permissions in a persistent location.
1188 * PARAMS
1189 * pKeyContainer [I] Pointer to the key container whose key permissions are to
1190 * be saved
1192 static void store_key_container_permissions(KEYCONTAINER *pKeyContainer)
1194 HKEY hKey;
1196 if (create_container_key(pKeyContainer, KEY_WRITE, &hKey))
1198 store_key_permissions(pKeyContainer->hKeyExchangeKeyPair, hKey,
1199 AT_KEYEXCHANGE);
1200 store_key_permissions(pKeyContainer->hSignatureKeyPair, hKey,
1201 AT_SIGNATURE);
1202 RegCloseKey(hKey);
1206 /******************************************************************************
1207 * release_key_container_keys [Internal]
1209 * Releases key container's keys.
1211 * PARAMS
1212 * pKeyContainer [I] Pointer to the key container whose keys are to be released.
1214 static void release_key_container_keys(KEYCONTAINER *pKeyContainer)
1216 release_handle(&handle_table, pKeyContainer->hKeyExchangeKeyPair,
1217 RSAENH_MAGIC_KEY);
1218 release_handle(&handle_table, pKeyContainer->hSignatureKeyPair,
1219 RSAENH_MAGIC_KEY);
1222 /******************************************************************************
1223 * destroy_key_container [Internal]
1225 * Destructor for key containers.
1227 * PARAMS
1228 * pObjectHdr [I] Pointer to the key container to be destroyed.
1230 static void destroy_key_container(OBJECTHDR *pObjectHdr)
1232 KEYCONTAINER *pKeyContainer = (KEYCONTAINER*)pObjectHdr;
1234 if (!(pKeyContainer->dwFlags & CRYPT_VERIFYCONTEXT))
1236 store_key_container_keys(pKeyContainer);
1237 store_key_container_permissions(pKeyContainer);
1238 release_key_container_keys(pKeyContainer);
1240 else
1241 release_key_container_keys(pKeyContainer);
1242 HeapFree( GetProcessHeap(), 0, pKeyContainer );
1245 /******************************************************************************
1246 * new_key_container [Internal]
1248 * Create a new key container. The personality (RSA Base, Strong or Enhanced CP)
1249 * of the CSP is determined via the pVTable->pszProvName string.
1251 * PARAMS
1252 * pszContainerName [I] Name of the key container.
1253 * pVTable [I] Callback functions and context info provided by the OS
1255 * RETURNS
1256 * Success: Handle to the new key container.
1257 * Failure: INVALID_HANDLE_VALUE
1259 static HCRYPTPROV new_key_container(PCCH pszContainerName, DWORD dwFlags, const VTableProvStruc *pVTable)
1261 KEYCONTAINER *pKeyContainer;
1262 HCRYPTPROV hKeyContainer;
1264 hKeyContainer = new_object(&handle_table, sizeof(KEYCONTAINER), RSAENH_MAGIC_CONTAINER,
1265 destroy_key_container, (OBJECTHDR**)&pKeyContainer);
1266 if (hKeyContainer != (HCRYPTPROV)INVALID_HANDLE_VALUE)
1268 lstrcpynA(pKeyContainer->szName, pszContainerName, MAX_PATH);
1269 pKeyContainer->dwFlags = dwFlags;
1270 pKeyContainer->dwEnumAlgsCtr = 0;
1271 pKeyContainer->hKeyExchangeKeyPair = (HCRYPTKEY)INVALID_HANDLE_VALUE;
1272 pKeyContainer->hSignatureKeyPair = (HCRYPTKEY)INVALID_HANDLE_VALUE;
1273 if (pVTable && pVTable->pszProvName) {
1274 lstrcpynA(pKeyContainer->szProvName, pVTable->pszProvName, MAX_PATH);
1275 if (!strcmp(pVTable->pszProvName, MS_DEF_PROV_A)) {
1276 pKeyContainer->dwPersonality = RSAENH_PERSONALITY_BASE;
1277 } else if (!strcmp(pVTable->pszProvName, MS_ENHANCED_PROV_A)) {
1278 pKeyContainer->dwPersonality = RSAENH_PERSONALITY_ENHANCED;
1279 } else if (!strcmp(pVTable->pszProvName, MS_DEF_RSA_SCHANNEL_PROV_A)) {
1280 pKeyContainer->dwPersonality = RSAENH_PERSONALITY_SCHANNEL;
1281 } else if (!strcmp(pVTable->pszProvName, MS_ENH_RSA_AES_PROV_A)) {
1282 pKeyContainer->dwPersonality = RSAENH_PERSONALITY_AES;
1283 } else {
1284 pKeyContainer->dwPersonality = RSAENH_PERSONALITY_STRONG;
1288 /* The new key container has to be inserted into the CSP immediately
1289 * after creation to be available for CPGetProvParam's PP_ENUMCONTAINERS. */
1290 if (!(dwFlags & CRYPT_VERIFYCONTEXT)) {
1291 HKEY hKey;
1293 if (create_container_key(pKeyContainer, KEY_WRITE, &hKey))
1294 RegCloseKey(hKey);
1298 return hKeyContainer;
1301 /******************************************************************************
1302 * read_key_value [Internal]
1304 * Reads a key pair value from the registry
1306 * PARAMS
1307 * hKeyContainer [I] Crypt provider to use to import the key
1308 * hKey [I] Registry key from which to read the key pair
1309 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
1310 * dwFlags [I] Flags for unprotecting the key
1311 * phCryptKey [O] Returned key
1313 static BOOL read_key_value(HCRYPTPROV hKeyContainer, HKEY hKey, DWORD dwKeySpec, DWORD dwFlags, HCRYPTKEY *phCryptKey)
1315 LPCSTR szValueName;
1316 DWORD dwValueType, dwLen;
1317 BYTE *pbKey;
1318 DATA_BLOB blobIn, blobOut;
1319 BOOL ret = FALSE;
1321 if (!(szValueName = map_key_spec_to_key_pair_name(dwKeySpec)))
1322 return FALSE;
1323 if (RegQueryValueExA(hKey, szValueName, 0, &dwValueType, NULL, &dwLen) ==
1324 ERROR_SUCCESS)
1326 pbKey = HeapAlloc(GetProcessHeap(), 0, dwLen);
1327 if (pbKey)
1329 if (RegQueryValueExA(hKey, szValueName, 0, &dwValueType, pbKey, &dwLen) ==
1330 ERROR_SUCCESS)
1332 blobIn.pbData = pbKey;
1333 blobIn.cbData = dwLen;
1335 if (CryptUnprotectData(&blobIn, NULL, NULL, NULL, NULL,
1336 dwFlags, &blobOut))
1338 ret = import_key(hKeyContainer, blobOut.pbData, blobOut.cbData, 0, 0,
1339 FALSE, phCryptKey);
1340 LocalFree(blobOut.pbData);
1343 HeapFree(GetProcessHeap(), 0, pbKey);
1346 if (ret)
1348 CRYPTKEY *pKey;
1350 if (lookup_handle(&handle_table, *phCryptKey, RSAENH_MAGIC_KEY,
1351 (OBJECTHDR**)&pKey))
1353 if ((szValueName = map_key_spec_to_permissions_name(dwKeySpec)))
1355 dwLen = sizeof(pKey->dwPermissions);
1356 RegQueryValueExA(hKey, szValueName, 0, NULL,
1357 (BYTE *)&pKey->dwPermissions, &dwLen);
1361 return ret;
1364 /******************************************************************************
1365 * read_key_container [Internal]
1367 * Tries to read the persistent state of the key container (mainly the signature
1368 * and key exchange private keys) given by pszContainerName.
1370 * PARAMS
1371 * pszContainerName [I] Name of the key container to read from the registry
1372 * pVTable [I] Pointer to context data provided by the operating system
1374 * RETURNS
1375 * Success: Handle to the key container read from the registry
1376 * Failure: INVALID_HANDLE_VALUE
1378 static HCRYPTPROV read_key_container(PCHAR pszContainerName, DWORD dwFlags, const VTableProvStruc *pVTable)
1380 HKEY hKey;
1381 KEYCONTAINER *pKeyContainer;
1382 HCRYPTPROV hKeyContainer;
1383 HCRYPTKEY hCryptKey;
1385 if (!open_container_key(pszContainerName, dwFlags, KEY_READ, &hKey))
1387 SetLastError(NTE_BAD_KEYSET);
1388 return (HCRYPTPROV)INVALID_HANDLE_VALUE;
1391 hKeyContainer = new_key_container(pszContainerName, dwFlags, pVTable);
1392 if (hKeyContainer != (HCRYPTPROV)INVALID_HANDLE_VALUE)
1394 DWORD dwProtectFlags = (dwFlags & CRYPT_MACHINE_KEYSET) ?
1395 CRYPTPROTECT_LOCAL_MACHINE : 0;
1397 if (!lookup_handle(&handle_table, hKeyContainer, RSAENH_MAGIC_CONTAINER,
1398 (OBJECTHDR**)&pKeyContainer))
1399 return (HCRYPTPROV)INVALID_HANDLE_VALUE;
1401 /* read_key_value calls import_key, which calls import_private_key,
1402 * which implicitly installs the key value into the appropriate key
1403 * container key. Thus the ref count is incremented twice, once for
1404 * the output key value, and once for the implicit install, and needs
1405 * to be decremented to balance the two.
1407 if (read_key_value(hKeyContainer, hKey, AT_KEYEXCHANGE,
1408 dwProtectFlags, &hCryptKey))
1409 release_handle(&handle_table, hCryptKey, RSAENH_MAGIC_KEY);
1410 if (read_key_value(hKeyContainer, hKey, AT_SIGNATURE,
1411 dwProtectFlags, &hCryptKey))
1412 release_handle(&handle_table, hCryptKey, RSAENH_MAGIC_KEY);
1415 return hKeyContainer;
1418 /******************************************************************************
1419 * build_hash_signature [Internal]
1421 * Builds a padded version of a hash to match the length of the RSA key modulus.
1423 * PARAMS
1424 * pbSignature [O] The padded hash object is stored here.
1425 * dwLen [I] Length of the pbSignature buffer.
1426 * aiAlgid [I] Algorithm identifier of the hash to be padded.
1427 * abHashValue [I] The value of the hash object.
1428 * dwHashLen [I] Length of the hash value.
1429 * dwFlags [I] Selection of padding algorithm.
1431 * RETURNS
1432 * Success: TRUE
1433 * Failure: FALSE (NTE_BAD_ALGID)
1435 static BOOL build_hash_signature(BYTE *pbSignature, DWORD dwLen, ALG_ID aiAlgid,
1436 const BYTE *abHashValue, DWORD dwHashLen, DWORD dwFlags)
1438 /* These prefixes are meant to be concatenated with hash values of the
1439 * respective kind to form a PKCS #7 DigestInfo. */
1440 static const struct tagOIDDescriptor {
1441 ALG_ID aiAlgid;
1442 DWORD dwLen;
1443 const BYTE abOID[19];
1444 } aOIDDescriptor[] = {
1445 { CALG_MD2, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1446 0x86, 0xf7, 0x0d, 0x02, 0x02, 0x05, 0x00, 0x04, 0x10 } },
1447 { CALG_MD4, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1448 0x86, 0xf7, 0x0d, 0x02, 0x04, 0x05, 0x00, 0x04, 0x10 } },
1449 { CALG_MD5, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1450 0x86, 0xf7, 0x0d, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10 } },
1451 { CALG_SHA, 15, { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03,
1452 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14 } },
1453 { CALG_SHA_256, 19, { 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
1454 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01,
1455 0x05, 0x00, 0x04, 0x20 } },
1456 { CALG_SHA_384, 19, { 0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
1457 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01,
1458 0x05, 0x00, 0x04, 0x30 } },
1459 { CALG_SHA_384, 19, { 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
1460 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01,
1461 0x05, 0x00, 0x04, 0x40 } },
1462 { CALG_SSL3_SHAMD5, 0, { 0 } },
1463 { 0, 0, { 0 } }
1465 DWORD dwIdxOID, i, j;
1467 for (dwIdxOID = 0; aOIDDescriptor[dwIdxOID].aiAlgid; dwIdxOID++) {
1468 if (aOIDDescriptor[dwIdxOID].aiAlgid == aiAlgid) break;
1471 if (!aOIDDescriptor[dwIdxOID].aiAlgid) {
1472 SetLastError(NTE_BAD_ALGID);
1473 return FALSE;
1476 /* Build the padded signature */
1477 if (dwFlags & CRYPT_X931_FORMAT) {
1478 pbSignature[0] = 0x6b;
1479 for (i=1; i < dwLen - dwHashLen - 3; i++) {
1480 pbSignature[i] = 0xbb;
1482 pbSignature[i++] = 0xba;
1483 for (j=0; j < dwHashLen; j++, i++) {
1484 pbSignature[i] = abHashValue[j];
1486 pbSignature[i++] = 0x33;
1487 pbSignature[i++] = 0xcc;
1488 } else {
1489 pbSignature[0] = 0x00;
1490 pbSignature[1] = 0x01;
1491 if (dwFlags & CRYPT_NOHASHOID) {
1492 for (i=2; i < dwLen - 1 - dwHashLen; i++) {
1493 pbSignature[i] = 0xff;
1495 pbSignature[i++] = 0x00;
1496 } else {
1497 for (i=2; i < dwLen - 1 - aOIDDescriptor[dwIdxOID].dwLen - dwHashLen; i++) {
1498 pbSignature[i] = 0xff;
1500 pbSignature[i++] = 0x00;
1501 for (j=0; j < aOIDDescriptor[dwIdxOID].dwLen; j++) {
1502 pbSignature[i++] = aOIDDescriptor[dwIdxOID].abOID[j];
1505 for (j=0; j < dwHashLen; j++) {
1506 pbSignature[i++] = abHashValue[j];
1510 return TRUE;
1513 /******************************************************************************
1514 * tls1_p [Internal]
1516 * This is an implementation of the 'P_hash' helper function for TLS1's PRF.
1517 * It is used exclusively by tls1_prf. For details see RFC 2246, chapter 5.
1518 * The pseudo random stream generated by this function is exclusive or'ed with
1519 * the data in pbBuffer.
1521 * PARAMS
1522 * hHMAC [I] HMAC object, which will be used in pseudo random generation
1523 * pblobSeed [I] Seed value
1524 * pbBuffer [I/O] Pseudo random stream will be xor'ed to the provided data
1525 * dwBufferLen [I] Number of pseudo random bytes desired
1527 * RETURNS
1528 * Success: TRUE
1529 * Failure: FALSE
1531 static BOOL tls1_p(HCRYPTHASH hHMAC, const PCRYPT_DATA_BLOB pblobSeed, BYTE *pbBuffer,
1532 DWORD dwBufferLen)
1534 CRYPTHASH *pHMAC;
1535 BYTE abAi[RSAENH_MAX_HASH_SIZE];
1536 DWORD i = 0;
1538 if (!lookup_handle(&handle_table, hHMAC, RSAENH_MAGIC_HASH, (OBJECTHDR**)&pHMAC)) {
1539 SetLastError(NTE_BAD_HASH);
1540 return FALSE;
1543 /* compute A_1 = HMAC(seed) */
1544 init_hash(pHMAC);
1545 update_hash(pHMAC, pblobSeed->pbData, pblobSeed->cbData);
1546 finalize_hash(pHMAC);
1547 memcpy(abAi, pHMAC->abHashValue, pHMAC->dwHashSize);
1549 do {
1550 /* compute HMAC(A_i + seed) */
1551 init_hash(pHMAC);
1552 update_hash(pHMAC, abAi, pHMAC->dwHashSize);
1553 update_hash(pHMAC, pblobSeed->pbData, pblobSeed->cbData);
1554 finalize_hash(pHMAC);
1556 /* pseudo random stream := CONCAT_{i=1..n} ( HMAC(A_i + seed) ) */
1557 do {
1558 if (i >= dwBufferLen) break;
1559 pbBuffer[i] ^= pHMAC->abHashValue[i % pHMAC->dwHashSize];
1560 i++;
1561 } while (i % pHMAC->dwHashSize);
1563 /* compute A_{i+1} = HMAC(A_i) */
1564 init_hash(pHMAC);
1565 update_hash(pHMAC, abAi, pHMAC->dwHashSize);
1566 finalize_hash(pHMAC);
1567 memcpy(abAi, pHMAC->abHashValue, pHMAC->dwHashSize);
1568 } while (i < dwBufferLen);
1570 return TRUE;
1573 /******************************************************************************
1574 * tls1_prf [Internal]
1576 * TLS1 pseudo random function as specified in RFC 2246, chapter 5
1578 * PARAMS
1579 * hProv [I] Key container used to compute the pseudo random stream
1580 * hSecret [I] Key that holds the (pre-)master secret
1581 * pblobLabel [I] Descriptive label
1582 * pblobSeed [I] Seed value
1583 * pbBuffer [O] Pseudo random numbers will be stored here
1584 * dwBufferLen [I] Number of pseudo random bytes desired
1586 * RETURNS
1587 * Success: TRUE
1588 * Failure: FALSE
1590 static BOOL tls1_prf(HCRYPTPROV hProv, HCRYPTPROV hSecret, const PCRYPT_DATA_BLOB pblobLabel,
1591 const PCRYPT_DATA_BLOB pblobSeed, BYTE *pbBuffer, DWORD dwBufferLen)
1593 HMAC_INFO hmacInfo = { 0, NULL, 0, NULL, 0 };
1594 HCRYPTHASH hHMAC = (HCRYPTHASH)INVALID_HANDLE_VALUE;
1595 HCRYPTKEY hHalfSecret = (HCRYPTKEY)INVALID_HANDLE_VALUE;
1596 CRYPTKEY *pHalfSecret, *pSecret;
1597 DWORD dwHalfSecretLen;
1598 BOOL result = FALSE;
1599 CRYPT_DATA_BLOB blobLabelSeed;
1601 TRACE("(hProv=%08lx, hSecret=%08lx, pblobLabel=%p, pblobSeed=%p, pbBuffer=%p, dwBufferLen=%d)\n",
1602 hProv, hSecret, pblobLabel, pblobSeed, pbBuffer, dwBufferLen);
1604 if (!lookup_handle(&handle_table, hSecret, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pSecret)) {
1605 SetLastError(NTE_FAIL);
1606 return FALSE;
1609 dwHalfSecretLen = (pSecret->dwKeyLen+1)/2;
1611 /* concatenation of the label and the seed */
1612 if (!concat_data_blobs(&blobLabelSeed, pblobLabel, pblobSeed)) goto exit;
1614 /* zero out the buffer, since two random streams will be xor'ed into it. */
1615 memset(pbBuffer, 0, dwBufferLen);
1617 /* build a 'fake' key, to hold the secret. CALG_SSL2_MASTER is used since it provides
1618 * the biggest range of valid key lengths. */
1619 hHalfSecret = new_key(hProv, CALG_SSL2_MASTER, MAKELONG(0,dwHalfSecretLen*8), &pHalfSecret);
1620 if (hHalfSecret == (HCRYPTKEY)INVALID_HANDLE_VALUE) goto exit;
1622 /* Derive an HMAC_MD5 hash and call the helper function. */
1623 memcpy(pHalfSecret->abKeyValue, pSecret->abKeyValue, dwHalfSecretLen);
1624 if (!RSAENH_CPCreateHash(hProv, CALG_HMAC, hHalfSecret, 0, &hHMAC)) goto exit;
1625 hmacInfo.HashAlgid = CALG_MD5;
1626 if (!RSAENH_CPSetHashParam(hProv, hHMAC, HP_HMAC_INFO, (BYTE*)&hmacInfo, 0)) goto exit;
1627 if (!tls1_p(hHMAC, &blobLabelSeed, pbBuffer, dwBufferLen)) goto exit;
1629 /* Reconfigure to HMAC_SHA hash and call helper function again. */
1630 memcpy(pHalfSecret->abKeyValue, pSecret->abKeyValue + (pSecret->dwKeyLen/2), dwHalfSecretLen);
1631 hmacInfo.HashAlgid = CALG_SHA;
1632 if (!RSAENH_CPSetHashParam(hProv, hHMAC, HP_HMAC_INFO, (BYTE*)&hmacInfo, 0)) goto exit;
1633 if (!tls1_p(hHMAC, &blobLabelSeed, pbBuffer, dwBufferLen)) goto exit;
1635 result = TRUE;
1636 exit:
1637 release_handle(&handle_table, hHalfSecret, RSAENH_MAGIC_KEY);
1638 if (hHMAC != (HCRYPTHASH)INVALID_HANDLE_VALUE) RSAENH_CPDestroyHash(hProv, hHMAC);
1639 free_data_blob(&blobLabelSeed);
1640 return result;
1643 /******************************************************************************
1644 * pad_data [Internal]
1646 * Helper function for data padding according to PKCS1 #2
1648 * PARAMS
1649 * abData [I] The data to be padded
1650 * dwDataLen [I] Length of the data
1651 * abBuffer [O] Padded data will be stored here
1652 * dwBufferLen [I] Length of the buffer (also length of padded data)
1653 * dwFlags [I] Padding format (CRYPT_SSL2_FALLBACK)
1655 * RETURN
1656 * Success: TRUE
1657 * Failure: FALSE (NTE_BAD_LEN, too much data to pad)
1659 static BOOL pad_data(const BYTE *abData, DWORD dwDataLen, BYTE *abBuffer, DWORD dwBufferLen,
1660 DWORD dwFlags)
1662 DWORD i;
1664 /* Ensure there is enough space for PKCS1 #2 padding */
1665 if (dwDataLen > dwBufferLen-11) {
1666 SetLastError(NTE_BAD_LEN);
1667 return FALSE;
1670 memmove(abBuffer + dwBufferLen - dwDataLen, abData, dwDataLen);
1672 abBuffer[0] = 0x00;
1673 abBuffer[1] = RSAENH_PKC_BLOCKTYPE;
1674 for (i=2; i < dwBufferLen - dwDataLen - 1; i++)
1675 do gen_rand_impl(&abBuffer[i], 1); while (!abBuffer[i]);
1676 if (dwFlags & CRYPT_SSL2_FALLBACK)
1677 for (i-=8; i < dwBufferLen - dwDataLen - 1; i++)
1678 abBuffer[i] = 0x03;
1679 abBuffer[i] = 0x00;
1681 return TRUE;
1684 /******************************************************************************
1685 * unpad_data [Internal]
1687 * Remove the PKCS1 padding from RSA decrypted data
1689 * PARAMS
1690 * abData [I] The padded data
1691 * dwDataLen [I] Length of the padded data
1692 * abBuffer [O] Data without padding will be stored here
1693 * dwBufferLen [I/O] I: Length of the buffer, O: Length of unpadded data
1694 * dwFlags [I] Currently none defined
1696 * RETURNS
1697 * Success: TRUE
1698 * Failure: FALSE, (NTE_BAD_DATA, no valid PKCS1 padding or buffer too small)
1700 static BOOL unpad_data(const BYTE *abData, DWORD dwDataLen, BYTE *abBuffer, DWORD *dwBufferLen,
1701 DWORD dwFlags)
1703 DWORD i;
1705 if (dwDataLen < 3)
1707 SetLastError(NTE_BAD_DATA);
1708 return FALSE;
1710 for (i=2; i<dwDataLen; i++)
1711 if (!abData[i])
1712 break;
1714 if ((i == dwDataLen) || (*dwBufferLen < dwDataLen - i - 1) ||
1715 (abData[0] != 0x00) || (abData[1] != RSAENH_PKC_BLOCKTYPE))
1717 SetLastError(NTE_BAD_DATA);
1718 return FALSE;
1721 *dwBufferLen = dwDataLen - i - 1;
1722 memmove(abBuffer, abData + i + 1, *dwBufferLen);
1723 return TRUE;
1726 /******************************************************************************
1727 * CPAcquireContext (RSAENH.@)
1729 * Acquire a handle to the key container specified by pszContainer
1731 * PARAMS
1732 * phProv [O] Pointer to the location the acquired handle will be written to.
1733 * pszContainer [I] Name of the desired key container. See Notes
1734 * dwFlags [I] Flags. See Notes.
1735 * pVTable [I] Pointer to a PVTableProvStruct containing callbacks.
1737 * RETURNS
1738 * Success: TRUE
1739 * Failure: FALSE
1741 * NOTES
1742 * If pszContainer is NULL or points to a zero length string the user's login
1743 * name will be used as the key container name.
1745 * If the CRYPT_NEW_KEYSET flag is set in dwFlags a new keyset will be created.
1746 * If a keyset with the given name already exists, the function fails and sets
1747 * last error to NTE_EXISTS. If CRYPT_NEW_KEYSET is not set and the specified
1748 * key container does not exist, function fails and sets last error to
1749 * NTE_BAD_KEYSET.
1751 BOOL WINAPI RSAENH_CPAcquireContext(HCRYPTPROV *phProv, LPSTR pszContainer,
1752 DWORD dwFlags, PVTableProvStruc pVTable)
1754 CHAR szKeyContainerName[MAX_PATH];
1756 TRACE("(phProv=%p, pszContainer=%s, dwFlags=%08x, pVTable=%p)\n", phProv,
1757 debugstr_a(pszContainer), dwFlags, pVTable);
1759 if (pszContainer && *pszContainer)
1761 lstrcpynA(szKeyContainerName, pszContainer, MAX_PATH);
1763 else
1765 DWORD dwLen = sizeof(szKeyContainerName);
1766 if (!GetUserNameA(szKeyContainerName, &dwLen)) return FALSE;
1769 switch (dwFlags & (CRYPT_NEWKEYSET|CRYPT_VERIFYCONTEXT|CRYPT_DELETEKEYSET))
1771 case 0:
1772 *phProv = read_key_container(szKeyContainerName, dwFlags, pVTable);
1773 break;
1775 case CRYPT_DELETEKEYSET:
1776 return delete_container_key(szKeyContainerName, dwFlags);
1778 case CRYPT_NEWKEYSET:
1779 *phProv = read_key_container(szKeyContainerName, dwFlags, pVTable);
1780 if (*phProv != (HCRYPTPROV)INVALID_HANDLE_VALUE)
1782 release_handle(&handle_table, *phProv, RSAENH_MAGIC_CONTAINER);
1783 TRACE("Can't create new keyset, already exists\n");
1784 SetLastError(NTE_EXISTS);
1785 return FALSE;
1787 *phProv = new_key_container(szKeyContainerName, dwFlags, pVTable);
1788 break;
1790 case CRYPT_VERIFYCONTEXT|CRYPT_NEWKEYSET:
1791 case CRYPT_VERIFYCONTEXT:
1792 if (pszContainer && *pszContainer) {
1793 TRACE("pszContainer should be empty\n");
1794 SetLastError(NTE_BAD_FLAGS);
1795 return FALSE;
1797 *phProv = new_key_container("", dwFlags, pVTable);
1798 break;
1800 default:
1801 *phProv = (HCRYPTPROV)INVALID_HANDLE_VALUE;
1802 SetLastError(NTE_BAD_FLAGS);
1803 return FALSE;
1806 if (*phProv != (HCRYPTPROV)INVALID_HANDLE_VALUE) {
1807 SetLastError(ERROR_SUCCESS);
1808 return TRUE;
1809 } else {
1810 return FALSE;
1814 /******************************************************************************
1815 * CPCreateHash (RSAENH.@)
1817 * CPCreateHash creates and initializes a new hash object.
1819 * PARAMS
1820 * hProv [I] Handle to the key container to which the new hash will belong.
1821 * Algid [I] Identifies the hash algorithm, which will be used for the hash.
1822 * hKey [I] Handle to a session key applied for keyed hashes.
1823 * dwFlags [I] Currently no flags defined. Must be zero.
1824 * phHash [O] Points to the location where a handle to the new hash will be stored.
1826 * RETURNS
1827 * Success: TRUE
1828 * Failure: FALSE
1830 * NOTES
1831 * hKey is a handle to a session key applied in keyed hashes like MAC and HMAC.
1832 * If a normal hash object is to be created (like e.g. MD2 or SHA1) hKey must be zero.
1834 BOOL WINAPI RSAENH_CPCreateHash(HCRYPTPROV hProv, ALG_ID Algid, HCRYPTKEY hKey, DWORD dwFlags,
1835 HCRYPTHASH *phHash)
1837 CRYPTKEY *pCryptKey;
1838 CRYPTHASH *pCryptHash;
1839 const PROV_ENUMALGS_EX *peaAlgidInfo;
1841 TRACE("(hProv=%08lx, Algid=%08x, hKey=%08lx, dwFlags=%08x, phHash=%p)\n", hProv, Algid, hKey,
1842 dwFlags, phHash);
1844 peaAlgidInfo = get_algid_info(hProv, Algid);
1845 if (!peaAlgidInfo) return FALSE;
1847 if (dwFlags)
1849 SetLastError(NTE_BAD_FLAGS);
1850 return FALSE;
1853 if (Algid == CALG_MAC || Algid == CALG_HMAC || Algid == CALG_SCHANNEL_MASTER_HASH ||
1854 Algid == CALG_TLS1PRF)
1856 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey)) {
1857 SetLastError(NTE_BAD_KEY);
1858 return FALSE;
1861 if ((Algid == CALG_MAC) && (GET_ALG_TYPE(pCryptKey->aiAlgid) != ALG_TYPE_BLOCK)) {
1862 SetLastError(NTE_BAD_KEY);
1863 return FALSE;
1866 if ((Algid == CALG_SCHANNEL_MASTER_HASH || Algid == CALG_TLS1PRF) &&
1867 (pCryptKey->aiAlgid != CALG_TLS1_MASTER))
1869 SetLastError(NTE_BAD_KEY);
1870 return FALSE;
1872 if (Algid == CALG_SCHANNEL_MASTER_HASH &&
1873 ((!pCryptKey->siSChannelInfo.blobClientRandom.cbData) ||
1874 (!pCryptKey->siSChannelInfo.blobServerRandom.cbData)))
1876 SetLastError(ERROR_INVALID_PARAMETER);
1877 return FALSE;
1880 if ((Algid == CALG_TLS1PRF) && (pCryptKey->dwState != RSAENH_KEYSTATE_MASTERKEY)) {
1881 SetLastError(NTE_BAD_KEY_STATE);
1882 return FALSE;
1886 *phHash = new_object(&handle_table, sizeof(CRYPTHASH), RSAENH_MAGIC_HASH,
1887 destroy_hash, (OBJECTHDR**)&pCryptHash);
1888 if (!pCryptHash) return FALSE;
1890 pCryptHash->aiAlgid = Algid;
1891 pCryptHash->hKey = hKey;
1892 pCryptHash->hProv = hProv;
1893 pCryptHash->dwState = RSAENH_HASHSTATE_HASHING;
1894 pCryptHash->pHMACInfo = NULL;
1895 pCryptHash->dwHashSize = peaAlgidInfo->dwDefaultLen >> 3;
1896 init_data_blob(&pCryptHash->tpPRFParams.blobLabel);
1897 init_data_blob(&pCryptHash->tpPRFParams.blobSeed);
1899 if (Algid == CALG_SCHANNEL_MASTER_HASH) {
1900 static const char keyex[] = "key expansion";
1901 BYTE key_expansion[sizeof keyex];
1902 CRYPT_DATA_BLOB blobRandom, blobKeyExpansion = { 13, key_expansion };
1904 memcpy( key_expansion, keyex, sizeof keyex );
1906 if (pCryptKey->dwState != RSAENH_KEYSTATE_MASTERKEY) {
1907 static const char msec[] = "master secret";
1908 BYTE master_secret[sizeof msec];
1909 CRYPT_DATA_BLOB blobLabel = { 13, master_secret };
1910 BYTE abKeyValue[48];
1912 memcpy( master_secret, msec, sizeof msec );
1914 /* See RFC 2246, chapter 8.1 */
1915 if (!concat_data_blobs(&blobRandom,
1916 &pCryptKey->siSChannelInfo.blobClientRandom,
1917 &pCryptKey->siSChannelInfo.blobServerRandom))
1919 return FALSE;
1921 tls1_prf(hProv, hKey, &blobLabel, &blobRandom, abKeyValue, 48);
1922 pCryptKey->dwState = RSAENH_KEYSTATE_MASTERKEY;
1923 memcpy(pCryptKey->abKeyValue, abKeyValue, 48);
1924 free_data_blob(&blobRandom);
1927 /* See RFC 2246, chapter 6.3 */
1928 if (!concat_data_blobs(&blobRandom,
1929 &pCryptKey->siSChannelInfo.blobServerRandom,
1930 &pCryptKey->siSChannelInfo.blobClientRandom))
1932 return FALSE;
1934 tls1_prf(hProv, hKey, &blobKeyExpansion, &blobRandom, pCryptHash->abHashValue,
1935 RSAENH_MAX_HASH_SIZE);
1936 free_data_blob(&blobRandom);
1939 return init_hash(pCryptHash);
1942 /******************************************************************************
1943 * CPDestroyHash (RSAENH.@)
1945 * Releases the handle to a hash object. The object is destroyed if its reference
1946 * count reaches zero.
1948 * PARAMS
1949 * hProv [I] Handle to the key container to which the hash object belongs.
1950 * hHash [I] Handle to the hash object to be released.
1952 * RETURNS
1953 * Success: TRUE
1954 * Failure: FALSE
1956 BOOL WINAPI RSAENH_CPDestroyHash(HCRYPTPROV hProv, HCRYPTHASH hHash)
1958 TRACE("(hProv=%08lx, hHash=%08lx)\n", hProv, hHash);
1960 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
1962 SetLastError(NTE_BAD_UID);
1963 return FALSE;
1966 if (!release_handle(&handle_table, hHash, RSAENH_MAGIC_HASH))
1968 SetLastError(NTE_BAD_HASH);
1969 return FALSE;
1972 return TRUE;
1975 /******************************************************************************
1976 * CPDestroyKey (RSAENH.@)
1978 * Releases the handle to a key object. The object is destroyed if its reference
1979 * count reaches zero.
1981 * PARAMS
1982 * hProv [I] Handle to the key container to which the key object belongs.
1983 * hKey [I] Handle to the key object to be released.
1985 * RETURNS
1986 * Success: TRUE
1987 * Failure: FALSE
1989 BOOL WINAPI RSAENH_CPDestroyKey(HCRYPTPROV hProv, HCRYPTKEY hKey)
1991 TRACE("(hProv=%08lx, hKey=%08lx)\n", hProv, hKey);
1993 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
1995 SetLastError(NTE_BAD_UID);
1996 return FALSE;
1999 if (!release_handle(&handle_table, hKey, RSAENH_MAGIC_KEY))
2001 SetLastError(NTE_BAD_KEY);
2002 return FALSE;
2005 return TRUE;
2008 /******************************************************************************
2009 * CPDuplicateHash (RSAENH.@)
2011 * Clones a hash object including its current state.
2013 * PARAMS
2014 * hUID [I] Handle to the key container the hash belongs to.
2015 * hHash [I] Handle to the hash object to be cloned.
2016 * pdwReserved [I] Reserved. Must be NULL.
2017 * dwFlags [I] No flags are currently defined. Must be 0.
2018 * phHash [O] Handle to the cloned hash object.
2020 * RETURNS
2021 * Success: TRUE.
2022 * Failure: FALSE.
2024 BOOL WINAPI RSAENH_CPDuplicateHash(HCRYPTPROV hUID, HCRYPTHASH hHash, DWORD *pdwReserved,
2025 DWORD dwFlags, HCRYPTHASH *phHash)
2027 CRYPTHASH *pSrcHash, *pDestHash;
2029 TRACE("(hUID=%08lx, hHash=%08lx, pdwReserved=%p, dwFlags=%08x, phHash=%p)\n", hUID, hHash,
2030 pdwReserved, dwFlags, phHash);
2032 if (!is_valid_handle(&handle_table, hUID, RSAENH_MAGIC_CONTAINER))
2034 SetLastError(NTE_BAD_UID);
2035 return FALSE;
2038 if (!lookup_handle(&handle_table, hHash, RSAENH_MAGIC_HASH, (OBJECTHDR**)&pSrcHash))
2040 SetLastError(NTE_BAD_HASH);
2041 return FALSE;
2044 if (!phHash || pdwReserved || dwFlags)
2046 SetLastError(ERROR_INVALID_PARAMETER);
2047 return FALSE;
2050 *phHash = new_object(&handle_table, sizeof(CRYPTHASH), RSAENH_MAGIC_HASH,
2051 destroy_hash, (OBJECTHDR**)&pDestHash);
2052 if (*phHash != (HCRYPTHASH)INVALID_HANDLE_VALUE)
2054 *pDestHash = *pSrcHash;
2055 duplicate_hash_impl(pSrcHash->aiAlgid, &pSrcHash->context, &pDestHash->context);
2056 copy_hmac_info(&pDestHash->pHMACInfo, pSrcHash->pHMACInfo);
2057 copy_data_blob(&pDestHash->tpPRFParams.blobLabel, &pSrcHash->tpPRFParams.blobLabel);
2058 copy_data_blob(&pDestHash->tpPRFParams.blobSeed, &pSrcHash->tpPRFParams.blobSeed);
2061 return *phHash != (HCRYPTHASH)INVALID_HANDLE_VALUE;
2064 /******************************************************************************
2065 * CPDuplicateKey (RSAENH.@)
2067 * Clones a key object including its current state.
2069 * PARAMS
2070 * hUID [I] Handle to the key container the hash belongs to.
2071 * hKey [I] Handle to the key object to be cloned.
2072 * pdwReserved [I] Reserved. Must be NULL.
2073 * dwFlags [I] No flags are currently defined. Must be 0.
2074 * phHash [O] Handle to the cloned key object.
2076 * RETURNS
2077 * Success: TRUE.
2078 * Failure: FALSE.
2080 BOOL WINAPI RSAENH_CPDuplicateKey(HCRYPTPROV hUID, HCRYPTKEY hKey, DWORD *pdwReserved,
2081 DWORD dwFlags, HCRYPTKEY *phKey)
2083 CRYPTKEY *pSrcKey, *pDestKey;
2085 TRACE("(hUID=%08lx, hKey=%08lx, pdwReserved=%p, dwFlags=%08x, phKey=%p)\n", hUID, hKey,
2086 pdwReserved, dwFlags, phKey);
2088 if (!is_valid_handle(&handle_table, hUID, RSAENH_MAGIC_CONTAINER))
2090 SetLastError(NTE_BAD_UID);
2091 return FALSE;
2094 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pSrcKey))
2096 SetLastError(NTE_BAD_KEY);
2097 return FALSE;
2100 if (!phKey || pdwReserved || dwFlags)
2102 SetLastError(ERROR_INVALID_PARAMETER);
2103 return FALSE;
2106 *phKey = new_object(&handle_table, sizeof(CRYPTKEY), RSAENH_MAGIC_KEY, destroy_key,
2107 (OBJECTHDR**)&pDestKey);
2108 if (*phKey != (HCRYPTKEY)INVALID_HANDLE_VALUE)
2110 *pDestKey = *pSrcKey;
2111 copy_data_blob(&pDestKey->siSChannelInfo.blobServerRandom,
2112 &pSrcKey->siSChannelInfo.blobServerRandom);
2113 copy_data_blob(&pDestKey->siSChannelInfo.blobClientRandom,
2114 &pSrcKey->siSChannelInfo.blobClientRandom);
2115 duplicate_key_impl(pSrcKey->aiAlgid, &pSrcKey->context, &pDestKey->context);
2116 return TRUE;
2118 else
2120 return FALSE;
2124 /******************************************************************************
2125 * CPEncrypt (RSAENH.@)
2127 * Encrypt data.
2129 * PARAMS
2130 * hProv [I] The key container hKey and hHash belong to.
2131 * hKey [I] The key used to encrypt the data.
2132 * hHash [I] An optional hash object for parallel hashing. See notes.
2133 * Final [I] Indicates if this is the last block of data to encrypt.
2134 * dwFlags [I] Currently no flags defined. Must be zero.
2135 * pbData [I/O] Pointer to the data to encrypt. Encrypted data will also be stored there.
2136 * pdwDataLen [I/O] I: Length of data to encrypt, O: Length of encrypted data.
2137 * dwBufLen [I] Size of the buffer at pbData.
2139 * RETURNS
2140 * Success: TRUE.
2141 * Failure: FALSE.
2143 * NOTES
2144 * If a hash object handle is provided in hHash, it will be updated with the plaintext.
2145 * This is useful for message signatures.
2147 * This function uses the standard WINAPI protocol for querying data of dynamic length.
2149 BOOL WINAPI RSAENH_CPEncrypt(HCRYPTPROV hProv, HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final,
2150 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen, DWORD dwBufLen)
2152 CRYPTKEY *pCryptKey;
2153 BYTE *in, out[RSAENH_MAX_BLOCK_SIZE], o[RSAENH_MAX_BLOCK_SIZE];
2154 DWORD dwEncryptedLen, i, j, k;
2156 TRACE("(hProv=%08lx, hKey=%08lx, hHash=%08lx, Final=%d, dwFlags=%08x, pbData=%p, "
2157 "pdwDataLen=%p, dwBufLen=%d)\n", hProv, hKey, hHash, Final, dwFlags, pbData, pdwDataLen,
2158 dwBufLen);
2160 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
2162 SetLastError(NTE_BAD_UID);
2163 return FALSE;
2166 if (dwFlags)
2168 SetLastError(NTE_BAD_FLAGS);
2169 return FALSE;
2172 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
2174 SetLastError(NTE_BAD_KEY);
2175 return FALSE;
2178 if (pCryptKey->dwState == RSAENH_KEYSTATE_IDLE)
2179 pCryptKey->dwState = RSAENH_KEYSTATE_ENCRYPTING;
2181 if (pCryptKey->dwState != RSAENH_KEYSTATE_ENCRYPTING)
2183 SetLastError(NTE_BAD_DATA);
2184 return FALSE;
2187 if (is_valid_handle(&handle_table, hHash, RSAENH_MAGIC_HASH)) {
2188 if (!RSAENH_CPHashData(hProv, hHash, pbData, *pdwDataLen, 0)) return FALSE;
2191 if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_BLOCK) {
2192 if (!Final && (*pdwDataLen % pCryptKey->dwBlockLen)) {
2193 SetLastError(NTE_BAD_DATA);
2194 return FALSE;
2197 dwEncryptedLen = (*pdwDataLen/pCryptKey->dwBlockLen+(Final?1:0))*pCryptKey->dwBlockLen;
2199 if (pbData == NULL) {
2200 *pdwDataLen = dwEncryptedLen;
2201 return TRUE;
2203 else if (dwEncryptedLen > dwBufLen) {
2204 *pdwDataLen = dwEncryptedLen;
2205 SetLastError(ERROR_MORE_DATA);
2206 return FALSE;
2209 /* Pad final block with length bytes */
2210 for (i=*pdwDataLen; i<dwEncryptedLen; i++) pbData[i] = dwEncryptedLen - *pdwDataLen;
2211 *pdwDataLen = dwEncryptedLen;
2213 for (i=0, in=pbData; i<*pdwDataLen; i+=pCryptKey->dwBlockLen, in+=pCryptKey->dwBlockLen) {
2214 switch (pCryptKey->dwMode) {
2215 case CRYPT_MODE_ECB:
2216 encrypt_block_impl(pCryptKey->aiAlgid, 0, &pCryptKey->context, in, out,
2217 RSAENH_ENCRYPT);
2218 break;
2220 case CRYPT_MODE_CBC:
2221 for (j=0; j<pCryptKey->dwBlockLen; j++) in[j] ^= pCryptKey->abChainVector[j];
2222 encrypt_block_impl(pCryptKey->aiAlgid, 0, &pCryptKey->context, in, out,
2223 RSAENH_ENCRYPT);
2224 memcpy(pCryptKey->abChainVector, out, pCryptKey->dwBlockLen);
2225 break;
2227 case CRYPT_MODE_CFB:
2228 for (j=0; j<pCryptKey->dwBlockLen; j++) {
2229 encrypt_block_impl(pCryptKey->aiAlgid, 0, &pCryptKey->context,
2230 pCryptKey->abChainVector, o, RSAENH_ENCRYPT);
2231 out[j] = in[j] ^ o[0];
2232 for (k=0; k<pCryptKey->dwBlockLen-1; k++)
2233 pCryptKey->abChainVector[k] = pCryptKey->abChainVector[k+1];
2234 pCryptKey->abChainVector[k] = out[j];
2236 break;
2238 default:
2239 SetLastError(NTE_BAD_ALGID);
2240 return FALSE;
2242 memcpy(in, out, pCryptKey->dwBlockLen);
2244 } else if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_STREAM) {
2245 if (pbData == NULL) {
2246 *pdwDataLen = dwBufLen;
2247 return TRUE;
2249 encrypt_stream_impl(pCryptKey->aiAlgid, &pCryptKey->context, pbData, *pdwDataLen);
2250 } else if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_RSA) {
2251 if (pCryptKey->aiAlgid == CALG_RSA_SIGN) {
2252 SetLastError(NTE_BAD_KEY);
2253 return FALSE;
2255 if (!pbData) {
2256 *pdwDataLen = pCryptKey->dwBlockLen;
2257 return TRUE;
2259 if (dwBufLen < pCryptKey->dwBlockLen) {
2260 SetLastError(ERROR_MORE_DATA);
2261 return FALSE;
2263 if (!pad_data(pbData, *pdwDataLen, pbData, pCryptKey->dwBlockLen, dwFlags)) return FALSE;
2264 encrypt_block_impl(pCryptKey->aiAlgid, PK_PUBLIC, &pCryptKey->context, pbData, pbData, RSAENH_ENCRYPT);
2265 *pdwDataLen = pCryptKey->dwBlockLen;
2266 Final = TRUE;
2267 } else {
2268 SetLastError(NTE_BAD_TYPE);
2269 return FALSE;
2272 if (Final) setup_key(pCryptKey);
2274 return TRUE;
2277 /******************************************************************************
2278 * CPDecrypt (RSAENH.@)
2280 * Decrypt data.
2282 * PARAMS
2283 * hProv [I] The key container hKey and hHash belong to.
2284 * hKey [I] The key used to decrypt the data.
2285 * hHash [I] An optional hash object for parallel hashing. See notes.
2286 * Final [I] Indicates if this is the last block of data to decrypt.
2287 * dwFlags [I] Currently no flags defined. Must be zero.
2288 * pbData [I/O] Pointer to the data to decrypt. Plaintext will also be stored there.
2289 * pdwDataLen [I/O] I: Length of ciphertext, O: Length of plaintext.
2291 * RETURNS
2292 * Success: TRUE.
2293 * Failure: FALSE.
2295 * NOTES
2296 * If a hash object handle is provided in hHash, it will be updated with the plaintext.
2297 * This is useful for message signatures.
2299 * This function uses the standard WINAPI protocol for querying data of dynamic length.
2301 BOOL WINAPI RSAENH_CPDecrypt(HCRYPTPROV hProv, HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final,
2302 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
2304 CRYPTKEY *pCryptKey;
2305 BYTE *in, out[RSAENH_MAX_BLOCK_SIZE], o[RSAENH_MAX_BLOCK_SIZE];
2306 DWORD i, j, k;
2307 DWORD dwMax;
2309 TRACE("(hProv=%08lx, hKey=%08lx, hHash=%08lx, Final=%d, dwFlags=%08x, pbData=%p, "
2310 "pdwDataLen=%p)\n", hProv, hKey, hHash, Final, dwFlags, pbData, pdwDataLen);
2312 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
2314 SetLastError(NTE_BAD_UID);
2315 return FALSE;
2318 if (dwFlags)
2320 SetLastError(NTE_BAD_FLAGS);
2321 return FALSE;
2324 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
2326 SetLastError(NTE_BAD_KEY);
2327 return FALSE;
2330 if (pCryptKey->dwState == RSAENH_KEYSTATE_IDLE)
2331 pCryptKey->dwState = RSAENH_KEYSTATE_ENCRYPTING;
2333 if (pCryptKey->dwState != RSAENH_KEYSTATE_ENCRYPTING)
2335 SetLastError(NTE_BAD_DATA);
2336 return FALSE;
2339 dwMax=*pdwDataLen;
2341 if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_BLOCK) {
2342 for (i=0, in=pbData; i<*pdwDataLen; i+=pCryptKey->dwBlockLen, in+=pCryptKey->dwBlockLen) {
2343 switch (pCryptKey->dwMode) {
2344 case CRYPT_MODE_ECB:
2345 encrypt_block_impl(pCryptKey->aiAlgid, 0, &pCryptKey->context, in, out,
2346 RSAENH_DECRYPT);
2347 break;
2349 case CRYPT_MODE_CBC:
2350 encrypt_block_impl(pCryptKey->aiAlgid, 0, &pCryptKey->context, in, out,
2351 RSAENH_DECRYPT);
2352 for (j=0; j<pCryptKey->dwBlockLen; j++) out[j] ^= pCryptKey->abChainVector[j];
2353 memcpy(pCryptKey->abChainVector, in, pCryptKey->dwBlockLen);
2354 break;
2356 case CRYPT_MODE_CFB:
2357 for (j=0; j<pCryptKey->dwBlockLen; j++) {
2358 encrypt_block_impl(pCryptKey->aiAlgid, 0, &pCryptKey->context,
2359 pCryptKey->abChainVector, o, RSAENH_ENCRYPT);
2360 out[j] = in[j] ^ o[0];
2361 for (k=0; k<pCryptKey->dwBlockLen-1; k++)
2362 pCryptKey->abChainVector[k] = pCryptKey->abChainVector[k+1];
2363 pCryptKey->abChainVector[k] = in[j];
2365 break;
2367 default:
2368 SetLastError(NTE_BAD_ALGID);
2369 return FALSE;
2371 memcpy(in, out, pCryptKey->dwBlockLen);
2373 if (Final) {
2374 if (pbData[*pdwDataLen-1] &&
2375 pbData[*pdwDataLen-1] <= pCryptKey->dwBlockLen &&
2376 pbData[*pdwDataLen-1] <= *pdwDataLen) {
2377 BOOL padOkay = TRUE;
2379 /* check that every bad byte has the same value */
2380 for (i = 1; padOkay && i < pbData[*pdwDataLen-1]; i++)
2381 if (pbData[*pdwDataLen - i - 1] != pbData[*pdwDataLen - 1])
2382 padOkay = FALSE;
2383 if (padOkay)
2384 *pdwDataLen -= pbData[*pdwDataLen-1];
2385 else {
2386 SetLastError(NTE_BAD_DATA);
2387 setup_key(pCryptKey);
2388 return FALSE;
2391 else {
2392 SetLastError(NTE_BAD_DATA);
2393 setup_key(pCryptKey);
2394 return FALSE;
2398 } else if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_STREAM) {
2399 encrypt_stream_impl(pCryptKey->aiAlgid, &pCryptKey->context, pbData, *pdwDataLen);
2400 } else if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_RSA) {
2401 if (pCryptKey->aiAlgid == CALG_RSA_SIGN) {
2402 SetLastError(NTE_BAD_KEY);
2403 return FALSE;
2405 encrypt_block_impl(pCryptKey->aiAlgid, PK_PRIVATE, &pCryptKey->context, pbData, pbData, RSAENH_DECRYPT);
2406 if (!unpad_data(pbData, pCryptKey->dwBlockLen, pbData, pdwDataLen, dwFlags)) return FALSE;
2407 Final = TRUE;
2408 } else {
2409 SetLastError(NTE_BAD_TYPE);
2410 return FALSE;
2413 if (Final) setup_key(pCryptKey);
2415 if (is_valid_handle(&handle_table, hHash, RSAENH_MAGIC_HASH)) {
2416 if (*pdwDataLen>dwMax ||
2417 !RSAENH_CPHashData(hProv, hHash, pbData, *pdwDataLen, 0)) return FALSE;
2420 return TRUE;
2423 static BOOL crypt_export_simple(CRYPTKEY *pCryptKey, CRYPTKEY *pPubKey,
2424 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
2426 BLOBHEADER *pBlobHeader = (BLOBHEADER*)pbData;
2427 ALG_ID *pAlgid = (ALG_ID*)(pBlobHeader+1);
2428 DWORD dwDataLen;
2430 if (!(GET_ALG_CLASS(pCryptKey->aiAlgid)&(ALG_CLASS_DATA_ENCRYPT|ALG_CLASS_MSG_ENCRYPT))) {
2431 SetLastError(NTE_BAD_KEY); /* FIXME: error code? */
2432 return FALSE;
2435 dwDataLen = sizeof(BLOBHEADER) + sizeof(ALG_ID) + pPubKey->dwBlockLen;
2436 if (pbData) {
2437 if (*pdwDataLen < dwDataLen) {
2438 SetLastError(ERROR_MORE_DATA);
2439 *pdwDataLen = dwDataLen;
2440 return FALSE;
2443 pBlobHeader->bType = SIMPLEBLOB;
2444 pBlobHeader->bVersion = CUR_BLOB_VERSION;
2445 pBlobHeader->reserved = 0;
2446 pBlobHeader->aiKeyAlg = pCryptKey->aiAlgid;
2448 *pAlgid = pPubKey->aiAlgid;
2450 if (!pad_data(pCryptKey->abKeyValue, pCryptKey->dwKeyLen, (BYTE*)(pAlgid+1),
2451 pPubKey->dwBlockLen, dwFlags))
2453 return FALSE;
2456 encrypt_block_impl(pPubKey->aiAlgid, PK_PUBLIC, &pPubKey->context, (BYTE*)(pAlgid+1),
2457 (BYTE*)(pAlgid+1), RSAENH_ENCRYPT);
2459 *pdwDataLen = dwDataLen;
2460 return TRUE;
2463 static BOOL crypt_export_public_key(CRYPTKEY *pCryptKey, BYTE *pbData,
2464 DWORD *pdwDataLen)
2466 BLOBHEADER *pBlobHeader = (BLOBHEADER*)pbData;
2467 RSAPUBKEY *pRSAPubKey = (RSAPUBKEY*)(pBlobHeader+1);
2468 DWORD dwDataLen;
2470 if ((pCryptKey->aiAlgid != CALG_RSA_KEYX) && (pCryptKey->aiAlgid != CALG_RSA_SIGN)) {
2471 SetLastError(NTE_BAD_KEY);
2472 return FALSE;
2475 dwDataLen = sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) + pCryptKey->dwKeyLen;
2476 if (pbData) {
2477 if (*pdwDataLen < dwDataLen) {
2478 SetLastError(ERROR_MORE_DATA);
2479 *pdwDataLen = dwDataLen;
2480 return FALSE;
2483 pBlobHeader->bType = PUBLICKEYBLOB;
2484 pBlobHeader->bVersion = CUR_BLOB_VERSION;
2485 pBlobHeader->reserved = 0;
2486 pBlobHeader->aiKeyAlg = pCryptKey->aiAlgid;
2488 pRSAPubKey->magic = RSAENH_MAGIC_RSA1;
2489 pRSAPubKey->bitlen = pCryptKey->dwKeyLen << 3;
2491 export_public_key_impl((BYTE*)(pRSAPubKey+1), &pCryptKey->context,
2492 pCryptKey->dwKeyLen, &pRSAPubKey->pubexp);
2494 *pdwDataLen = dwDataLen;
2495 return TRUE;
2498 static BOOL crypt_export_private_key(CRYPTKEY *pCryptKey, BOOL force,
2499 BYTE *pbData, DWORD *pdwDataLen)
2501 BLOBHEADER *pBlobHeader = (BLOBHEADER*)pbData;
2502 RSAPUBKEY *pRSAPubKey = (RSAPUBKEY*)(pBlobHeader+1);
2503 DWORD dwDataLen;
2505 if ((pCryptKey->aiAlgid != CALG_RSA_KEYX) && (pCryptKey->aiAlgid != CALG_RSA_SIGN)) {
2506 SetLastError(NTE_BAD_KEY);
2507 return FALSE;
2509 if (!force && !(pCryptKey->dwPermissions & CRYPT_EXPORT))
2511 SetLastError(NTE_BAD_KEY_STATE);
2512 return FALSE;
2515 dwDataLen = sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) +
2516 2 * pCryptKey->dwKeyLen + 5 * ((pCryptKey->dwKeyLen + 1) >> 1);
2517 if (pbData) {
2518 if (*pdwDataLen < dwDataLen) {
2519 SetLastError(ERROR_MORE_DATA);
2520 *pdwDataLen = dwDataLen;
2521 return FALSE;
2524 pBlobHeader->bType = PRIVATEKEYBLOB;
2525 pBlobHeader->bVersion = CUR_BLOB_VERSION;
2526 pBlobHeader->reserved = 0;
2527 pBlobHeader->aiKeyAlg = pCryptKey->aiAlgid;
2529 pRSAPubKey->magic = RSAENH_MAGIC_RSA2;
2530 pRSAPubKey->bitlen = pCryptKey->dwKeyLen << 3;
2532 export_private_key_impl((BYTE*)(pRSAPubKey+1), &pCryptKey->context,
2533 pCryptKey->dwKeyLen, &pRSAPubKey->pubexp);
2535 *pdwDataLen = dwDataLen;
2536 return TRUE;
2539 static BOOL crypt_export_plaintext_key(CRYPTKEY *pCryptKey, BYTE *pbData,
2540 DWORD *pdwDataLen)
2542 BLOBHEADER *pBlobHeader = (BLOBHEADER*)pbData;
2543 DWORD *pKeyLen = (DWORD*)(pBlobHeader+1);
2544 BYTE *pbKey = (BYTE*)(pKeyLen+1);
2545 DWORD dwDataLen;
2547 dwDataLen = sizeof(BLOBHEADER) + sizeof(DWORD) + pCryptKey->dwKeyLen;
2548 if (pbData) {
2549 if (*pdwDataLen < dwDataLen) {
2550 SetLastError(ERROR_MORE_DATA);
2551 *pdwDataLen = dwDataLen;
2552 return FALSE;
2555 pBlobHeader->bType = PLAINTEXTKEYBLOB;
2556 pBlobHeader->bVersion = CUR_BLOB_VERSION;
2557 pBlobHeader->reserved = 0;
2558 pBlobHeader->aiKeyAlg = pCryptKey->aiAlgid;
2560 *pKeyLen = pCryptKey->dwKeyLen;
2561 memcpy(pbKey, pCryptKey->abKeyValue, pCryptKey->dwKeyLen);
2563 *pdwDataLen = dwDataLen;
2564 return TRUE;
2566 /******************************************************************************
2567 * crypt_export_key [Internal]
2569 * Export a key into a binary large object (BLOB). Called by CPExportKey and
2570 * by store_key_pair.
2572 * PARAMS
2573 * pCryptKey [I] Key to be exported.
2574 * hPubKey [I] Key used to encrypt sensitive BLOB data.
2575 * dwBlobType [I] SIMPLEBLOB, PUBLICKEYBLOB or PRIVATEKEYBLOB.
2576 * dwFlags [I] Currently none defined.
2577 * force [I] If TRUE, the key is written no matter what the key's
2578 * permissions are. Otherwise the key's permissions are
2579 * checked before exporting.
2580 * pbData [O] Pointer to a buffer where the BLOB will be written to.
2581 * pdwDataLen [I/O] I: Size of buffer at pbData, O: Size of BLOB
2583 * RETURNS
2584 * Success: TRUE.
2585 * Failure: FALSE.
2587 static BOOL crypt_export_key(CRYPTKEY *pCryptKey, HCRYPTKEY hPubKey,
2588 DWORD dwBlobType, DWORD dwFlags, BOOL force,
2589 BYTE *pbData, DWORD *pdwDataLen)
2591 CRYPTKEY *pPubKey;
2593 if (dwFlags & CRYPT_SSL2_FALLBACK) {
2594 if (pCryptKey->aiAlgid != CALG_SSL2_MASTER) {
2595 SetLastError(NTE_BAD_KEY);
2596 return FALSE;
2600 switch ((BYTE)dwBlobType)
2602 case SIMPLEBLOB:
2603 if (!lookup_handle(&handle_table, hPubKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pPubKey)){
2604 SetLastError(NTE_BAD_PUBLIC_KEY); /* FIXME: error_code? */
2605 return FALSE;
2607 return crypt_export_simple(pCryptKey, pPubKey, dwFlags, pbData,
2608 pdwDataLen);
2610 case PUBLICKEYBLOB:
2611 if (is_valid_handle(&handle_table, hPubKey, RSAENH_MAGIC_KEY)) {
2612 SetLastError(NTE_BAD_KEY); /* FIXME: error code? */
2613 return FALSE;
2616 return crypt_export_public_key(pCryptKey, pbData, pdwDataLen);
2618 case PRIVATEKEYBLOB:
2619 return crypt_export_private_key(pCryptKey, force, pbData, pdwDataLen);
2621 case PLAINTEXTKEYBLOB:
2622 return crypt_export_plaintext_key(pCryptKey, pbData, pdwDataLen);
2624 default:
2625 SetLastError(NTE_BAD_TYPE); /* FIXME: error code? */
2626 return FALSE;
2630 /******************************************************************************
2631 * CPExportKey (RSAENH.@)
2633 * Export a key into a binary large object (BLOB).
2635 * PARAMS
2636 * hProv [I] Key container from which a key is to be exported.
2637 * hKey [I] Key to be exported.
2638 * hPubKey [I] Key used to encrypt sensitive BLOB data.
2639 * dwBlobType [I] SIMPLEBLOB, PUBLICKEYBLOB or PRIVATEKEYBLOB.
2640 * dwFlags [I] Currently none defined.
2641 * pbData [O] Pointer to a buffer where the BLOB will be written to.
2642 * pdwDataLen [I/O] I: Size of buffer at pbData, O: Size of BLOB
2644 * RETURNS
2645 * Success: TRUE.
2646 * Failure: FALSE.
2648 BOOL WINAPI RSAENH_CPExportKey(HCRYPTPROV hProv, HCRYPTKEY hKey, HCRYPTKEY hPubKey,
2649 DWORD dwBlobType, DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
2651 CRYPTKEY *pCryptKey;
2653 TRACE("(hProv=%08lx, hKey=%08lx, hPubKey=%08lx, dwBlobType=%08x, dwFlags=%08x, pbData=%p,"
2654 "pdwDataLen=%p)\n", hProv, hKey, hPubKey, dwBlobType, dwFlags, pbData, pdwDataLen);
2656 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
2658 SetLastError(NTE_BAD_UID);
2659 return FALSE;
2662 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
2664 SetLastError(NTE_BAD_KEY);
2665 return FALSE;
2668 return crypt_export_key(pCryptKey, hPubKey, dwBlobType, dwFlags, FALSE,
2669 pbData, pdwDataLen);
2672 /******************************************************************************
2673 * release_and_install_key [Internal]
2675 * Release an existing key, if present, and replaces it with a new one.
2677 * PARAMS
2678 * hProv [I] Key container into which the key is to be imported.
2679 * src [I] Key which will replace *dest
2680 * dest [I] Points to key to be released and replaced with src
2681 * fStoreKey [I] If TRUE, the newly installed key is stored to the registry.
2683 static void release_and_install_key(HCRYPTPROV hProv, HCRYPTKEY src,
2684 HCRYPTKEY *dest, DWORD fStoreKey)
2686 RSAENH_CPDestroyKey(hProv, *dest);
2687 copy_handle(&handle_table, src, RSAENH_MAGIC_KEY, dest);
2688 if (fStoreKey)
2690 KEYCONTAINER *pKeyContainer;
2692 if (lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER,
2693 (OBJECTHDR**)&pKeyContainer))
2695 store_key_container_keys(pKeyContainer);
2696 store_key_container_permissions(pKeyContainer);
2701 /******************************************************************************
2702 * import_private_key [Internal]
2704 * Import a BLOB'ed private key into a key container.
2706 * PARAMS
2707 * hProv [I] Key container into which the private key is to be imported.
2708 * pbData [I] Pointer to a buffer which holds the private key BLOB.
2709 * dwDataLen [I] Length of data in buffer at pbData.
2710 * dwFlags [I] One of:
2711 * CRYPT_EXPORTABLE: the imported key is marked exportable
2712 * fStoreKey [I] If TRUE, the imported key is stored to the registry.
2713 * phKey [O] Handle to the imported key.
2716 * NOTES
2717 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2718 * it's a PRIVATEKEYBLOB.
2720 * RETURNS
2721 * Success: TRUE.
2722 * Failure: FALSE.
2724 static BOOL import_private_key(HCRYPTPROV hProv, const BYTE *pbData, DWORD dwDataLen,
2725 DWORD dwFlags, BOOL fStoreKey, HCRYPTKEY *phKey)
2727 KEYCONTAINER *pKeyContainer;
2728 CRYPTKEY *pCryptKey;
2729 const BLOBHEADER *pBlobHeader = (const BLOBHEADER*)pbData;
2730 const RSAPUBKEY *pRSAPubKey = (const RSAPUBKEY*)(pBlobHeader+1);
2731 BOOL ret;
2733 if (dwFlags & CRYPT_IPSEC_HMAC_KEY)
2735 FIXME("unimplemented for CRYPT_IPSEC_HMAC_KEY\n");
2736 SetLastError(NTE_BAD_FLAGS);
2737 return FALSE;
2739 if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER,
2740 (OBJECTHDR**)&pKeyContainer))
2742 SetLastError(NTE_BAD_UID);
2743 return FALSE;
2746 if ((dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY)))
2748 ERR("datalen %d not long enough for a BLOBHEADER + RSAPUBKEY\n",
2749 dwDataLen);
2750 SetLastError(NTE_BAD_DATA);
2751 return FALSE;
2753 if (pRSAPubKey->magic != RSAENH_MAGIC_RSA2)
2755 ERR("unexpected magic %08x\n", pRSAPubKey->magic);
2756 SetLastError(NTE_BAD_DATA);
2757 return FALSE;
2759 if ((dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) +
2760 (pRSAPubKey->bitlen >> 3) + (5 * ((pRSAPubKey->bitlen+8)>>4))))
2762 DWORD expectedLen = sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) +
2763 (pRSAPubKey->bitlen >> 3) + (5 * ((pRSAPubKey->bitlen+8)>>4));
2765 ERR("blob too short for pub key: expect %d, got %d\n",
2766 expectedLen, dwDataLen);
2767 SetLastError(NTE_BAD_DATA);
2768 return FALSE;
2771 *phKey = new_key(hProv, pBlobHeader->aiKeyAlg, MAKELONG(0,pRSAPubKey->bitlen), &pCryptKey);
2772 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) return FALSE;
2773 setup_key(pCryptKey);
2774 ret = import_private_key_impl((const BYTE*)(pRSAPubKey+1), &pCryptKey->context,
2775 pRSAPubKey->bitlen/8, dwDataLen, pRSAPubKey->pubexp);
2776 if (ret) {
2777 if (dwFlags & CRYPT_EXPORTABLE)
2778 pCryptKey->dwPermissions |= CRYPT_EXPORT;
2779 switch (pBlobHeader->aiKeyAlg)
2781 case AT_SIGNATURE:
2782 case CALG_RSA_SIGN:
2783 TRACE("installing signing key\n");
2784 release_and_install_key(hProv, *phKey, &pKeyContainer->hSignatureKeyPair,
2785 fStoreKey);
2786 break;
2787 case AT_KEYEXCHANGE:
2788 case CALG_RSA_KEYX:
2789 TRACE("installing key exchange key\n");
2790 release_and_install_key(hProv, *phKey, &pKeyContainer->hKeyExchangeKeyPair,
2791 fStoreKey);
2792 break;
2795 return ret;
2798 /******************************************************************************
2799 * import_public_key [Internal]
2801 * Import a BLOB'ed public key into a key container.
2803 * PARAMS
2804 * hProv [I] Key container into which the public key is to be imported.
2805 * pbData [I] Pointer to a buffer which holds the public key BLOB.
2806 * dwDataLen [I] Length of data in buffer at pbData.
2807 * dwFlags [I] One of:
2808 * CRYPT_EXPORTABLE: the imported key is marked exportable
2809 * fStoreKey [I] If TRUE, the imported key is stored to the registry.
2810 * phKey [O] Handle to the imported key.
2813 * NOTES
2814 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2815 * it's a PUBLICKEYBLOB.
2817 * RETURNS
2818 * Success: TRUE.
2819 * Failure: FALSE.
2821 static BOOL import_public_key(HCRYPTPROV hProv, const BYTE *pbData, DWORD dwDataLen,
2822 DWORD dwFlags, BOOL fStoreKey, HCRYPTKEY *phKey)
2824 KEYCONTAINER *pKeyContainer;
2825 CRYPTKEY *pCryptKey;
2826 const BLOBHEADER *pBlobHeader = (const BLOBHEADER*)pbData;
2827 const RSAPUBKEY *pRSAPubKey = (const RSAPUBKEY*)(pBlobHeader+1);
2828 ALG_ID algID;
2829 BOOL ret;
2831 if (dwFlags & CRYPT_IPSEC_HMAC_KEY)
2833 FIXME("unimplemented for CRYPT_IPSEC_HMAC_KEY\n");
2834 SetLastError(NTE_BAD_FLAGS);
2835 return FALSE;
2837 if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER,
2838 (OBJECTHDR**)&pKeyContainer))
2840 SetLastError(NTE_BAD_UID);
2841 return FALSE;
2844 if ((dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY)) ||
2845 (pRSAPubKey->magic != RSAENH_MAGIC_RSA1) ||
2846 (dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) + (pRSAPubKey->bitlen >> 3)))
2848 SetLastError(NTE_BAD_DATA);
2849 return FALSE;
2852 /* Since this is a public key blob, only the public key is
2853 * available, so only signature verification is possible.
2855 algID = pBlobHeader->aiKeyAlg;
2856 *phKey = new_key(hProv, algID, MAKELONG(0,pRSAPubKey->bitlen), &pCryptKey);
2857 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) return FALSE;
2858 setup_key(pCryptKey);
2859 ret = import_public_key_impl((const BYTE*)(pRSAPubKey+1), &pCryptKey->context,
2860 pRSAPubKey->bitlen >> 3, pRSAPubKey->pubexp);
2861 if (ret) {
2862 if (dwFlags & CRYPT_EXPORTABLE)
2863 pCryptKey->dwPermissions |= CRYPT_EXPORT;
2864 switch (pBlobHeader->aiKeyAlg)
2866 case AT_KEYEXCHANGE:
2867 case CALG_RSA_KEYX:
2868 TRACE("installing public key\n");
2869 release_and_install_key(hProv, *phKey, &pKeyContainer->hKeyExchangeKeyPair,
2870 fStoreKey);
2871 break;
2874 return ret;
2877 /******************************************************************************
2878 * import_symmetric_key [Internal]
2880 * Import a BLOB'ed symmetric key into a key container.
2882 * PARAMS
2883 * hProv [I] Key container into which the symmetric key is to be imported.
2884 * pbData [I] Pointer to a buffer which holds the symmetric key BLOB.
2885 * dwDataLen [I] Length of data in buffer at pbData.
2886 * hPubKey [I] Key used to decrypt sensitive BLOB data.
2887 * dwFlags [I] One of:
2888 * CRYPT_EXPORTABLE: the imported key is marked exportable
2889 * phKey [O] Handle to the imported key.
2892 * NOTES
2893 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2894 * it's a SIMPLEBLOB.
2896 * RETURNS
2897 * Success: TRUE.
2898 * Failure: FALSE.
2900 static BOOL import_symmetric_key(HCRYPTPROV hProv, const BYTE *pbData, DWORD dwDataLen,
2901 HCRYPTKEY hPubKey, DWORD dwFlags, HCRYPTKEY *phKey)
2903 CRYPTKEY *pCryptKey, *pPubKey;
2904 const BLOBHEADER *pBlobHeader = (const BLOBHEADER*)pbData;
2905 const ALG_ID *pAlgid = (const ALG_ID*)(pBlobHeader+1);
2906 const BYTE *pbKeyStream = (const BYTE*)(pAlgid + 1);
2907 BYTE *pbDecrypted;
2908 DWORD dwKeyLen;
2910 if (dwFlags & CRYPT_IPSEC_HMAC_KEY)
2912 FIXME("unimplemented for CRYPT_IPSEC_HMAC_KEY\n");
2913 SetLastError(NTE_BAD_FLAGS);
2914 return FALSE;
2916 if (!lookup_handle(&handle_table, hPubKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pPubKey) ||
2917 pPubKey->aiAlgid != CALG_RSA_KEYX)
2919 SetLastError(NTE_BAD_PUBLIC_KEY); /* FIXME: error code? */
2920 return FALSE;
2923 if (dwDataLen < sizeof(BLOBHEADER)+sizeof(ALG_ID)+pPubKey->dwBlockLen)
2925 SetLastError(NTE_BAD_DATA); /* FIXME: error code */
2926 return FALSE;
2929 pbDecrypted = HeapAlloc(GetProcessHeap(), 0, pPubKey->dwBlockLen);
2930 if (!pbDecrypted) return FALSE;
2931 encrypt_block_impl(pPubKey->aiAlgid, PK_PRIVATE, &pPubKey->context, pbKeyStream, pbDecrypted,
2932 RSAENH_DECRYPT);
2934 dwKeyLen = RSAENH_MAX_KEY_SIZE;
2935 if (!unpad_data(pbDecrypted, pPubKey->dwBlockLen, pbDecrypted, &dwKeyLen, dwFlags)) {
2936 HeapFree(GetProcessHeap(), 0, pbDecrypted);
2937 return FALSE;
2940 *phKey = new_key(hProv, pBlobHeader->aiKeyAlg, dwKeyLen<<19, &pCryptKey);
2941 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE)
2943 HeapFree(GetProcessHeap(), 0, pbDecrypted);
2944 return FALSE;
2946 memcpy(pCryptKey->abKeyValue, pbDecrypted, dwKeyLen);
2947 HeapFree(GetProcessHeap(), 0, pbDecrypted);
2948 setup_key(pCryptKey);
2949 if (dwFlags & CRYPT_EXPORTABLE)
2950 pCryptKey->dwPermissions |= CRYPT_EXPORT;
2951 return TRUE;
2954 /******************************************************************************
2955 * import_plaintext_key [Internal]
2957 * Import a plaintext key into a key container.
2959 * PARAMS
2960 * hProv [I] Key container into which the symmetric key is to be imported.
2961 * pbData [I] Pointer to a buffer which holds the plaintext key BLOB.
2962 * dwDataLen [I] Length of data in buffer at pbData.
2963 * dwFlags [I] One of:
2964 * CRYPT_EXPORTABLE: the imported key is marked exportable
2965 * phKey [O] Handle to the imported key.
2968 * NOTES
2969 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2970 * it's a PLAINTEXTKEYBLOB.
2972 * RETURNS
2973 * Success: TRUE.
2974 * Failure: FALSE.
2976 static BOOL import_plaintext_key(HCRYPTPROV hProv, const BYTE *pbData, DWORD dwDataLen,
2977 DWORD dwFlags, HCRYPTKEY *phKey)
2979 CRYPTKEY *pCryptKey;
2980 const BLOBHEADER *pBlobHeader = (const BLOBHEADER*)pbData;
2981 const DWORD *pKeyLen = (const DWORD *)(pBlobHeader + 1);
2982 const BYTE *pbKeyStream = (const BYTE*)(pKeyLen + 1);
2984 if (dwDataLen < sizeof(BLOBHEADER)+sizeof(DWORD)+*pKeyLen)
2986 SetLastError(NTE_BAD_DATA); /* FIXME: error code */
2987 return FALSE;
2990 if (dwFlags & CRYPT_IPSEC_HMAC_KEY)
2992 *phKey = new_key(hProv, CALG_HMAC, 0, &pCryptKey);
2993 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE)
2994 return FALSE;
2995 if (*pKeyLen <= RSAENH_MIN(sizeof(pCryptKey->abKeyValue), RSAENH_HMAC_BLOCK_LEN))
2997 memcpy(pCryptKey->abKeyValue, pbKeyStream, *pKeyLen);
2998 pCryptKey->dwKeyLen = *pKeyLen;
3000 else
3002 CRYPT_DATA_BLOB blobHmacKey = { *pKeyLen, (BYTE *)pbKeyStream };
3004 /* In order to initialize an HMAC key, the key material is hashed,
3005 * and the output of the hash function is used as the key material.
3006 * Unfortunately, the way the Crypto API is designed, we don't know
3007 * the hash algorithm yet, so we have to copy the entire key
3008 * material.
3010 if (!copy_data_blob(&pCryptKey->blobHmacKey, &blobHmacKey))
3012 release_handle(&handle_table, *phKey, RSAENH_MAGIC_KEY);
3013 *phKey = (HCRYPTKEY)INVALID_HANDLE_VALUE;
3014 return FALSE;
3017 setup_key(pCryptKey);
3018 if (dwFlags & CRYPT_EXPORTABLE)
3019 pCryptKey->dwPermissions |= CRYPT_EXPORT;
3021 else
3023 *phKey = new_key(hProv, pBlobHeader->aiKeyAlg, *pKeyLen<<19, &pCryptKey);
3024 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE)
3025 return FALSE;
3026 memcpy(pCryptKey->abKeyValue, pbKeyStream, *pKeyLen);
3027 setup_key(pCryptKey);
3028 if (dwFlags & CRYPT_EXPORTABLE)
3029 pCryptKey->dwPermissions |= CRYPT_EXPORT;
3031 return TRUE;
3034 /******************************************************************************
3035 * import_key [Internal]
3037 * Import a BLOB'ed key into a key container, optionally storing the key's
3038 * value to the registry.
3040 * PARAMS
3041 * hProv [I] Key container into which the key is to be imported.
3042 * pbData [I] Pointer to a buffer which holds the BLOB.
3043 * dwDataLen [I] Length of data in buffer at pbData.
3044 * hPubKey [I] Key used to decrypt sensitive BLOB data.
3045 * dwFlags [I] One of:
3046 * CRYPT_EXPORTABLE: the imported key is marked exportable
3047 * fStoreKey [I] If TRUE, the imported key is stored to the registry.
3048 * phKey [O] Handle to the imported key.
3050 * RETURNS
3051 * Success: TRUE.
3052 * Failure: FALSE.
3054 static BOOL import_key(HCRYPTPROV hProv, const BYTE *pbData, DWORD dwDataLen, HCRYPTKEY hPubKey,
3055 DWORD dwFlags, BOOL fStoreKey, HCRYPTKEY *phKey)
3057 KEYCONTAINER *pKeyContainer;
3058 const BLOBHEADER *pBlobHeader = (const BLOBHEADER*)pbData;
3060 if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER,
3061 (OBJECTHDR**)&pKeyContainer))
3063 SetLastError(NTE_BAD_UID);
3064 return FALSE;
3067 if (dwDataLen < sizeof(BLOBHEADER) ||
3068 pBlobHeader->bVersion != CUR_BLOB_VERSION ||
3069 pBlobHeader->reserved != 0)
3071 TRACE("bVersion = %d, reserved = %d\n", pBlobHeader->bVersion,
3072 pBlobHeader->reserved);
3073 SetLastError(NTE_BAD_DATA);
3074 return FALSE;
3077 /* If this is a verify-only context, the key is not persisted regardless of
3078 * fStoreKey's original value.
3080 fStoreKey = fStoreKey && !(dwFlags & CRYPT_VERIFYCONTEXT);
3081 TRACE("blob type: %x\n", pBlobHeader->bType);
3082 switch (pBlobHeader->bType)
3084 case PRIVATEKEYBLOB:
3085 return import_private_key(hProv, pbData, dwDataLen, dwFlags,
3086 fStoreKey, phKey);
3088 case PUBLICKEYBLOB:
3089 return import_public_key(hProv, pbData, dwDataLen, dwFlags,
3090 fStoreKey, phKey);
3092 case SIMPLEBLOB:
3093 return import_symmetric_key(hProv, pbData, dwDataLen, hPubKey,
3094 dwFlags, phKey);
3096 case PLAINTEXTKEYBLOB:
3097 return import_plaintext_key(hProv, pbData, dwDataLen, dwFlags,
3098 phKey);
3100 default:
3101 SetLastError(NTE_BAD_TYPE); /* FIXME: error code? */
3102 return FALSE;
3106 /******************************************************************************
3107 * CPImportKey (RSAENH.@)
3109 * Import a BLOB'ed key into a key container.
3111 * PARAMS
3112 * hProv [I] Key container into which the key is to be imported.
3113 * pbData [I] Pointer to a buffer which holds the BLOB.
3114 * dwDataLen [I] Length of data in buffer at pbData.
3115 * hPubKey [I] Key used to decrypt sensitive BLOB data.
3116 * dwFlags [I] One of:
3117 * CRYPT_EXPORTABLE: the imported key is marked exportable
3118 * phKey [O] Handle to the imported key.
3120 * RETURNS
3121 * Success: TRUE.
3122 * Failure: FALSE.
3124 BOOL WINAPI RSAENH_CPImportKey(HCRYPTPROV hProv, const BYTE *pbData, DWORD dwDataLen,
3125 HCRYPTKEY hPubKey, DWORD dwFlags, HCRYPTKEY *phKey)
3127 TRACE("(hProv=%08lx, pbData=%p, dwDataLen=%d, hPubKey=%08lx, dwFlags=%08x, phKey=%p)\n",
3128 hProv, pbData, dwDataLen, hPubKey, dwFlags, phKey);
3130 return import_key(hProv, pbData, dwDataLen, hPubKey, dwFlags, TRUE, phKey);
3133 /******************************************************************************
3134 * CPGenKey (RSAENH.@)
3136 * Generate a key in the key container
3138 * PARAMS
3139 * hProv [I] Key container for which a key is to be generated.
3140 * Algid [I] Crypto algorithm identifier for the key to be generated.
3141 * dwFlags [I] Upper 16 bits: Binary length of key. Lower 16 bits: Flags. See Notes
3142 * phKey [O] Handle to the generated key.
3144 * RETURNS
3145 * Success: TRUE.
3146 * Failure: FALSE.
3148 * FIXME
3149 * Flags currently not considered.
3151 * NOTES
3152 * Private key-exchange- and signature-keys can be generated with Algid AT_KEYEXCHANGE
3153 * and AT_SIGNATURE values.
3155 BOOL WINAPI RSAENH_CPGenKey(HCRYPTPROV hProv, ALG_ID Algid, DWORD dwFlags, HCRYPTKEY *phKey)
3157 KEYCONTAINER *pKeyContainer;
3158 CRYPTKEY *pCryptKey;
3160 TRACE("(hProv=%08lx, aiAlgid=%d, dwFlags=%08x, phKey=%p)\n", hProv, Algid, dwFlags, phKey);
3162 if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER,
3163 (OBJECTHDR**)&pKeyContainer))
3165 /* MSDN: hProv not containing valid context handle */
3166 SetLastError(NTE_BAD_UID);
3167 return FALSE;
3170 switch (Algid)
3172 case AT_SIGNATURE:
3173 case CALG_RSA_SIGN:
3174 *phKey = new_key(hProv, CALG_RSA_SIGN, dwFlags, &pCryptKey);
3175 if (pCryptKey) {
3176 new_key_impl(pCryptKey->aiAlgid, &pCryptKey->context, pCryptKey->dwKeyLen);
3177 setup_key(pCryptKey);
3178 release_and_install_key(hProv, *phKey,
3179 &pKeyContainer->hSignatureKeyPair,
3180 FALSE);
3182 break;
3184 case AT_KEYEXCHANGE:
3185 case CALG_RSA_KEYX:
3186 *phKey = new_key(hProv, CALG_RSA_KEYX, dwFlags, &pCryptKey);
3187 if (pCryptKey) {
3188 new_key_impl(pCryptKey->aiAlgid, &pCryptKey->context, pCryptKey->dwKeyLen);
3189 setup_key(pCryptKey);
3190 release_and_install_key(hProv, *phKey,
3191 &pKeyContainer->hKeyExchangeKeyPair,
3192 FALSE);
3194 break;
3196 case CALG_RC2:
3197 case CALG_RC4:
3198 case CALG_DES:
3199 case CALG_3DES_112:
3200 case CALG_3DES:
3201 case CALG_AES:
3202 case CALG_AES_128:
3203 case CALG_AES_192:
3204 case CALG_AES_256:
3205 case CALG_PCT1_MASTER:
3206 case CALG_SSL2_MASTER:
3207 case CALG_SSL3_MASTER:
3208 case CALG_TLS1_MASTER:
3209 *phKey = new_key(hProv, Algid, dwFlags, &pCryptKey);
3210 if (pCryptKey) {
3211 gen_rand_impl(pCryptKey->abKeyValue, RSAENH_MAX_KEY_SIZE);
3212 switch (Algid) {
3213 case CALG_SSL3_MASTER:
3214 pCryptKey->abKeyValue[0] = RSAENH_SSL3_VERSION_MAJOR;
3215 pCryptKey->abKeyValue[1] = RSAENH_SSL3_VERSION_MINOR;
3216 break;
3218 case CALG_TLS1_MASTER:
3219 pCryptKey->abKeyValue[0] = RSAENH_TLS1_VERSION_MAJOR;
3220 pCryptKey->abKeyValue[1] = RSAENH_TLS1_VERSION_MINOR;
3221 break;
3223 setup_key(pCryptKey);
3225 break;
3227 default:
3228 /* MSDN: Algorithm not supported specified by Algid */
3229 SetLastError(NTE_BAD_ALGID);
3230 return FALSE;
3233 return *phKey != (HCRYPTKEY)INVALID_HANDLE_VALUE;
3236 /******************************************************************************
3237 * CPGenRandom (RSAENH.@)
3239 * Generate a random byte stream.
3241 * PARAMS
3242 * hProv [I] Key container that is used to generate random bytes.
3243 * dwLen [I] Specifies the number of requested random data bytes.
3244 * pbBuffer [O] Random bytes will be stored here.
3246 * RETURNS
3247 * Success: TRUE
3248 * Failure: FALSE
3250 BOOL WINAPI RSAENH_CPGenRandom(HCRYPTPROV hProv, DWORD dwLen, BYTE *pbBuffer)
3252 TRACE("(hProv=%08lx, dwLen=%d, pbBuffer=%p)\n", hProv, dwLen, pbBuffer);
3254 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
3256 /* MSDN: hProv not containing valid context handle */
3257 SetLastError(NTE_BAD_UID);
3258 return FALSE;
3261 return gen_rand_impl(pbBuffer, dwLen);
3264 /******************************************************************************
3265 * CPGetHashParam (RSAENH.@)
3267 * Query parameters of an hash object.
3269 * PARAMS
3270 * hProv [I] The kea container, which the hash belongs to.
3271 * hHash [I] The hash object that is to be queried.
3272 * dwParam [I] Specifies the parameter that is to be queried.
3273 * pbData [I] Pointer to the buffer where the parameter value will be stored.
3274 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
3275 * dwFlags [I] None currently defined.
3277 * RETURNS
3278 * Success: TRUE
3279 * Failure: FALSE
3281 * NOTES
3282 * Valid dwParams are: HP_ALGID, HP_HASHSIZE, HP_HASHVALUE. The hash will be
3283 * finalized if HP_HASHVALUE is queried.
3285 BOOL WINAPI RSAENH_CPGetHashParam(HCRYPTPROV hProv, HCRYPTHASH hHash, DWORD dwParam, BYTE *pbData,
3286 DWORD *pdwDataLen, DWORD dwFlags)
3288 CRYPTHASH *pCryptHash;
3290 TRACE("(hProv=%08lx, hHash=%08lx, dwParam=%08x, pbData=%p, pdwDataLen=%p, dwFlags=%08x)\n",
3291 hProv, hHash, dwParam, pbData, pdwDataLen, dwFlags);
3293 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
3295 SetLastError(NTE_BAD_UID);
3296 return FALSE;
3299 if (dwFlags)
3301 SetLastError(NTE_BAD_FLAGS);
3302 return FALSE;
3305 if (!lookup_handle(&handle_table, hHash, RSAENH_MAGIC_HASH,
3306 (OBJECTHDR**)&pCryptHash))
3308 SetLastError(NTE_BAD_HASH);
3309 return FALSE;
3312 if (!pdwDataLen)
3314 SetLastError(ERROR_INVALID_PARAMETER);
3315 return FALSE;
3318 switch (dwParam)
3320 case HP_ALGID:
3321 return copy_param(pbData, pdwDataLen, (const BYTE*)&pCryptHash->aiAlgid,
3322 sizeof(ALG_ID));
3324 case HP_HASHSIZE:
3325 return copy_param(pbData, pdwDataLen, (const BYTE*)&pCryptHash->dwHashSize,
3326 sizeof(DWORD));
3328 case HP_HASHVAL:
3329 if (pCryptHash->aiAlgid == CALG_TLS1PRF) {
3330 return tls1_prf(hProv, pCryptHash->hKey, &pCryptHash->tpPRFParams.blobLabel,
3331 &pCryptHash->tpPRFParams.blobSeed, pbData, *pdwDataLen);
3334 if ( pbData == NULL ) {
3335 *pdwDataLen = pCryptHash->dwHashSize;
3336 return TRUE;
3339 if (pbData && (pCryptHash->dwState != RSAENH_HASHSTATE_FINISHED))
3341 finalize_hash(pCryptHash);
3342 pCryptHash->dwState = RSAENH_HASHSTATE_FINISHED;
3345 return copy_param(pbData, pdwDataLen, pCryptHash->abHashValue,
3346 pCryptHash->dwHashSize);
3348 default:
3349 SetLastError(NTE_BAD_TYPE);
3350 return FALSE;
3354 /******************************************************************************
3355 * CPSetKeyParam (RSAENH.@)
3357 * Set a parameter of a key object
3359 * PARAMS
3360 * hProv [I] The key container to which the key belongs.
3361 * hKey [I] The key for which a parameter is to be set.
3362 * dwParam [I] Parameter type. See Notes.
3363 * pbData [I] Pointer to the parameter value.
3364 * dwFlags [I] Currently none defined.
3366 * RETURNS
3367 * Success: TRUE.
3368 * Failure: FALSE.
3370 * NOTES:
3371 * Defined dwParam types are:
3372 * - KP_MODE: Values MODE_CBC, MODE_ECB, MODE_CFB.
3373 * - KP_MODE_BITS: Shift width for cipher feedback mode. (Currently ignored by MS CSP's)
3374 * - KP_PERMISSIONS: Or'ed combination of CRYPT_ENCRYPT, CRYPT_DECRYPT,
3375 * CRYPT_EXPORT, CRYPT_READ, CRYPT_WRITE, CRYPT_MAC
3376 * - KP_IV: Initialization vector
3378 BOOL WINAPI RSAENH_CPSetKeyParam(HCRYPTPROV hProv, HCRYPTKEY hKey, DWORD dwParam, BYTE *pbData,
3379 DWORD dwFlags)
3381 CRYPTKEY *pCryptKey;
3383 TRACE("(hProv=%08lx, hKey=%08lx, dwParam=%08x, pbData=%p, dwFlags=%08x)\n", hProv, hKey,
3384 dwParam, pbData, dwFlags);
3386 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
3388 SetLastError(NTE_BAD_UID);
3389 return FALSE;
3392 if (dwFlags) {
3393 SetLastError(NTE_BAD_FLAGS);
3394 return FALSE;
3397 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
3399 SetLastError(NTE_BAD_KEY);
3400 return FALSE;
3403 switch (dwParam) {
3404 case KP_PADDING:
3405 /* The MS providers only support PKCS5_PADDING */
3406 if (*(DWORD *)pbData != PKCS5_PADDING) {
3407 SetLastError(NTE_BAD_DATA);
3408 return FALSE;
3410 return TRUE;
3412 case KP_MODE:
3413 pCryptKey->dwMode = *(DWORD*)pbData;
3414 return TRUE;
3416 case KP_MODE_BITS:
3417 pCryptKey->dwModeBits = *(DWORD*)pbData;
3418 return TRUE;
3420 case KP_PERMISSIONS:
3422 DWORD perms = *(DWORD *)pbData;
3424 if ((perms & CRYPT_EXPORT) &&
3425 !(pCryptKey->dwPermissions & CRYPT_EXPORT))
3427 SetLastError(NTE_BAD_DATA);
3428 return FALSE;
3430 else if (!(perms & CRYPT_EXPORT) &&
3431 (pCryptKey->dwPermissions & CRYPT_EXPORT))
3433 /* Clearing the export permission appears to be ignored,
3434 * see tests.
3436 perms |= CRYPT_EXPORT;
3438 pCryptKey->dwPermissions = perms;
3439 return TRUE;
3442 case KP_IV:
3443 memcpy(pCryptKey->abInitVector, pbData, pCryptKey->dwBlockLen);
3444 setup_key(pCryptKey);
3445 return TRUE;
3447 case KP_SALT:
3448 switch (pCryptKey->aiAlgid) {
3449 case CALG_RC2:
3450 case CALG_RC4:
3451 if (!pbData)
3453 SetLastError(ERROR_INVALID_PARAMETER);
3454 return FALSE;
3456 /* MSDN: the base provider always sets eleven bytes of
3457 * salt value.
3459 memcpy(pCryptKey->abKeyValue + pCryptKey->dwKeyLen,
3460 pbData, 11);
3461 pCryptKey->dwSaltLen = 11;
3462 setup_key(pCryptKey);
3463 /* Strange but true: salt length reset to 0 after setting
3464 * it via KP_SALT.
3466 pCryptKey->dwSaltLen = 0;
3467 break;
3468 default:
3469 SetLastError(NTE_BAD_KEY);
3470 return FALSE;
3472 return TRUE;
3474 case KP_SALT_EX:
3476 CRYPT_INTEGER_BLOB *blob = (CRYPT_INTEGER_BLOB *)pbData;
3478 /* salt length can't be greater than 184 bits = 24 bytes */
3479 if (blob->cbData > 24)
3481 SetLastError(NTE_BAD_DATA);
3482 return FALSE;
3484 memcpy(pCryptKey->abKeyValue + pCryptKey->dwKeyLen, blob->pbData,
3485 blob->cbData);
3486 pCryptKey->dwSaltLen = blob->cbData;
3487 setup_key(pCryptKey);
3488 return TRUE;
3491 case KP_EFFECTIVE_KEYLEN:
3492 switch (pCryptKey->aiAlgid) {
3493 case CALG_RC2:
3494 if (!pbData)
3496 SetLastError(ERROR_INVALID_PARAMETER);
3497 return FALSE;
3499 else if (!*(DWORD *)pbData || *(DWORD *)pbData > 1024)
3501 SetLastError(NTE_BAD_DATA);
3502 return FALSE;
3504 else
3506 pCryptKey->dwEffectiveKeyLen = *(DWORD *)pbData;
3507 setup_key(pCryptKey);
3509 break;
3510 default:
3511 SetLastError(NTE_BAD_TYPE);
3512 return FALSE;
3514 return TRUE;
3516 case KP_SCHANNEL_ALG:
3517 switch (((PSCHANNEL_ALG)pbData)->dwUse) {
3518 case SCHANNEL_ENC_KEY:
3519 memcpy(&pCryptKey->siSChannelInfo.saEncAlg, pbData, sizeof(SCHANNEL_ALG));
3520 break;
3522 case SCHANNEL_MAC_KEY:
3523 memcpy(&pCryptKey->siSChannelInfo.saMACAlg, pbData, sizeof(SCHANNEL_ALG));
3524 break;
3526 default:
3527 SetLastError(NTE_FAIL); /* FIXME: error code */
3528 return FALSE;
3530 return TRUE;
3532 case KP_CLIENT_RANDOM:
3533 return copy_data_blob(&pCryptKey->siSChannelInfo.blobClientRandom, (PCRYPT_DATA_BLOB)pbData);
3535 case KP_SERVER_RANDOM:
3536 return copy_data_blob(&pCryptKey->siSChannelInfo.blobServerRandom, (PCRYPT_DATA_BLOB)pbData);
3538 default:
3539 SetLastError(NTE_BAD_TYPE);
3540 return FALSE;
3544 /******************************************************************************
3545 * CPGetKeyParam (RSAENH.@)
3547 * Query a key parameter.
3549 * PARAMS
3550 * hProv [I] The key container, which the key belongs to.
3551 * hHash [I] The key object that is to be queried.
3552 * dwParam [I] Specifies the parameter that is to be queried.
3553 * pbData [I] Pointer to the buffer where the parameter value will be stored.
3554 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
3555 * dwFlags [I] None currently defined.
3557 * RETURNS
3558 * Success: TRUE
3559 * Failure: FALSE
3561 * NOTES
3562 * Defined dwParam types are:
3563 * - KP_MODE: Values MODE_CBC, MODE_ECB, MODE_CFB.
3564 * - KP_MODE_BITS: Shift width for cipher feedback mode.
3565 * (Currently ignored by MS CSP's - always eight)
3566 * - KP_PERMISSIONS: Or'ed combination of CRYPT_ENCRYPT, CRYPT_DECRYPT,
3567 * CRYPT_EXPORT, CRYPT_READ, CRYPT_WRITE, CRYPT_MAC
3568 * - KP_IV: Initialization vector.
3569 * - KP_KEYLEN: Bitwidth of the key.
3570 * - KP_BLOCKLEN: Size of a block cipher block.
3571 * - KP_SALT: Salt value.
3573 BOOL WINAPI RSAENH_CPGetKeyParam(HCRYPTPROV hProv, HCRYPTKEY hKey, DWORD dwParam, BYTE *pbData,
3574 DWORD *pdwDataLen, DWORD dwFlags)
3576 CRYPTKEY *pCryptKey;
3577 DWORD dwValue;
3579 TRACE("(hProv=%08lx, hKey=%08lx, dwParam=%08x, pbData=%p, pdwDataLen=%p dwFlags=%08x)\n",
3580 hProv, hKey, dwParam, pbData, pdwDataLen, dwFlags);
3582 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
3584 SetLastError(NTE_BAD_UID);
3585 return FALSE;
3588 if (dwFlags) {
3589 SetLastError(NTE_BAD_FLAGS);
3590 return FALSE;
3593 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
3595 SetLastError(NTE_BAD_KEY);
3596 return FALSE;
3599 switch (dwParam)
3601 case KP_IV:
3602 return copy_param(pbData, pdwDataLen, pCryptKey->abInitVector,
3603 pCryptKey->dwBlockLen);
3605 case KP_SALT:
3606 switch (pCryptKey->aiAlgid) {
3607 case CALG_RC2:
3608 case CALG_RC4:
3609 return copy_param(pbData, pdwDataLen,
3610 &pCryptKey->abKeyValue[pCryptKey->dwKeyLen],
3611 pCryptKey->dwSaltLen);
3612 default:
3613 SetLastError(NTE_BAD_KEY);
3614 return FALSE;
3617 case KP_PADDING:
3618 dwValue = PKCS5_PADDING;
3619 return copy_param(pbData, pdwDataLen, (const BYTE*)&dwValue, sizeof(DWORD));
3621 case KP_KEYLEN:
3622 dwValue = pCryptKey->dwKeyLen << 3;
3623 return copy_param(pbData, pdwDataLen, (const BYTE*)&dwValue, sizeof(DWORD));
3625 case KP_EFFECTIVE_KEYLEN:
3626 if (pCryptKey->dwEffectiveKeyLen)
3627 dwValue = pCryptKey->dwEffectiveKeyLen;
3628 else
3629 dwValue = pCryptKey->dwKeyLen << 3;
3630 return copy_param(pbData, pdwDataLen, (const BYTE*)&dwValue, sizeof(DWORD));
3632 case KP_BLOCKLEN:
3633 dwValue = pCryptKey->dwBlockLen << 3;
3634 return copy_param(pbData, pdwDataLen, (const BYTE*)&dwValue, sizeof(DWORD));
3636 case KP_MODE:
3637 return copy_param(pbData, pdwDataLen, (const BYTE*)&pCryptKey->dwMode, sizeof(DWORD));
3639 case KP_MODE_BITS:
3640 return copy_param(pbData, pdwDataLen, (const BYTE*)&pCryptKey->dwModeBits,
3641 sizeof(DWORD));
3643 case KP_PERMISSIONS:
3644 return copy_param(pbData, pdwDataLen, (const BYTE*)&pCryptKey->dwPermissions,
3645 sizeof(DWORD));
3647 case KP_ALGID:
3648 return copy_param(pbData, pdwDataLen, (const BYTE*)&pCryptKey->aiAlgid, sizeof(DWORD));
3650 default:
3651 SetLastError(NTE_BAD_TYPE);
3652 return FALSE;
3656 /******************************************************************************
3657 * CPGetProvParam (RSAENH.@)
3659 * Query a CSP parameter.
3661 * PARAMS
3662 * hProv [I] The key container that is to be queried.
3663 * dwParam [I] Specifies the parameter that is to be queried.
3664 * pbData [I] Pointer to the buffer where the parameter value will be stored.
3665 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
3666 * dwFlags [I] CRYPT_FIRST: Start enumeration (for PP_ENUMALGS{_EX}).
3668 * RETURNS
3669 * Success: TRUE
3670 * Failure: FALSE
3671 * NOTES:
3672 * Defined dwParam types:
3673 * - PP_CONTAINER: Name of the key container.
3674 * - PP_NAME: Name of the cryptographic service provider.
3675 * - PP_SIG_KEYSIZE_INC: RSA signature keywidth granularity in bits.
3676 * - PP_KEYX_KEYSIZE_INC: RSA key-exchange keywidth granularity in bits.
3677 * - PP_ENUMALGS{_EX}: Query provider capabilities.
3678 * - PP_KEYSET_SEC_DESCR: Retrieve security descriptor on container.
3680 BOOL WINAPI RSAENH_CPGetProvParam(HCRYPTPROV hProv, DWORD dwParam, BYTE *pbData,
3681 DWORD *pdwDataLen, DWORD dwFlags)
3683 KEYCONTAINER *pKeyContainer;
3684 PROV_ENUMALGS provEnumalgs;
3685 DWORD dwTemp;
3686 HKEY hKey;
3688 /* This is for dwParam PP_CRYPT_COUNT_KEY_USE.
3689 * IE6 SP1 asks for it in the 'About' dialog.
3690 * Returning this BLOB seems to satisfy IE. The marked 0x00 seem
3691 * to be 'don't care's. If you know anything more specific about
3692 * this provider parameter, please report to wine-devel@winehq.org */
3693 static const BYTE abWTF[96] = {
3694 0xb0, 0x25, 0x63, 0x86, 0x9c, 0xab, 0xb6, 0x37,
3695 0xe8, 0x82, /**/0x00,/**/ 0x72, 0x06, 0xb2, /**/0x00,/**/ 0x3b,
3696 0x60, 0x35, /**/0x00,/**/ 0x3b, 0x88, 0xce, /**/0x00,/**/ 0x82,
3697 0xbc, 0x7a, /**/0x00,/**/ 0xb7, 0x4f, 0x7e, /**/0x00,/**/ 0xde,
3698 0x92, 0xf1, /**/0x00,/**/ 0x83, 0xea, 0x5e, /**/0x00,/**/ 0xc8,
3699 0x12, 0x1e, 0xd4, 0x06, 0xf7, 0x66, /**/0x00,/**/ 0x01,
3700 0x29, 0xa4, /**/0x00,/**/ 0xf8, 0x24, 0x0c, /**/0x00,/**/ 0x33,
3701 0x06, 0x80, /**/0x00,/**/ 0x02, 0x46, 0x0b, /**/0x00,/**/ 0x6d,
3702 0x5b, 0xca, /**/0x00,/**/ 0x9a, 0x10, 0xf0, /**/0x00,/**/ 0x05,
3703 0x19, 0xd0, /**/0x00,/**/ 0x2c, 0xf6, 0x27, /**/0x00,/**/ 0xaa,
3704 0x7c, 0x6f, /**/0x00,/**/ 0xb9, 0xd8, 0x72, /**/0x00,/**/ 0x03,
3705 0xf3, 0x81, /**/0x00,/**/ 0xfa, 0xe8, 0x26, /**/0x00,/**/ 0xca
3708 TRACE("(hProv=%08lx, dwParam=%08x, pbData=%p, pdwDataLen=%p, dwFlags=%08x)\n",
3709 hProv, dwParam, pbData, pdwDataLen, dwFlags);
3711 if (!pdwDataLen) {
3712 SetLastError(ERROR_INVALID_PARAMETER);
3713 return FALSE;
3716 if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER,
3717 (OBJECTHDR**)&pKeyContainer))
3719 /* MSDN: hProv not containing valid context handle */
3720 SetLastError(NTE_BAD_UID);
3721 return FALSE;
3724 switch (dwParam)
3726 case PP_CONTAINER:
3727 case PP_UNIQUE_CONTAINER:/* MSDN says we can return the same value as PP_CONTAINER */
3728 return copy_param(pbData, pdwDataLen, (const BYTE*)pKeyContainer->szName,
3729 strlen(pKeyContainer->szName)+1);
3731 case PP_NAME:
3732 return copy_param(pbData, pdwDataLen, (const BYTE*)pKeyContainer->szProvName,
3733 strlen(pKeyContainer->szProvName)+1);
3735 case PP_PROVTYPE:
3736 dwTemp = PROV_RSA_FULL;
3737 return copy_param(pbData, pdwDataLen, (const BYTE*)&dwTemp, sizeof(dwTemp));
3739 case PP_KEYSPEC:
3740 dwTemp = AT_SIGNATURE | AT_KEYEXCHANGE;
3741 return copy_param(pbData, pdwDataLen, (const BYTE*)&dwTemp, sizeof(dwTemp));
3743 case PP_KEYSET_TYPE:
3744 dwTemp = pKeyContainer->dwFlags & CRYPT_MACHINE_KEYSET;
3745 return copy_param(pbData, pdwDataLen, (const BYTE*)&dwTemp, sizeof(dwTemp));
3747 case PP_KEYSTORAGE:
3748 dwTemp = CRYPT_SEC_DESCR;
3749 return copy_param(pbData, pdwDataLen, (const BYTE*)&dwTemp, sizeof(dwTemp));
3751 case PP_SIG_KEYSIZE_INC:
3752 case PP_KEYX_KEYSIZE_INC:
3753 dwTemp = 8;
3754 return copy_param(pbData, pdwDataLen, (const BYTE*)&dwTemp, sizeof(dwTemp));
3756 case PP_IMPTYPE:
3757 dwTemp = CRYPT_IMPL_SOFTWARE;
3758 return copy_param(pbData, pdwDataLen, (const BYTE*)&dwTemp, sizeof(dwTemp));
3760 case PP_VERSION:
3761 dwTemp = 0x00000200;
3762 return copy_param(pbData, pdwDataLen, (const BYTE*)&dwTemp, sizeof(dwTemp));
3764 case PP_ENUMCONTAINERS:
3765 if ((dwFlags & CRYPT_FIRST) == CRYPT_FIRST) pKeyContainer->dwEnumContainersCtr = 0;
3767 if (!pbData) {
3768 *pdwDataLen = (DWORD)MAX_PATH + 1;
3769 return TRUE;
3772 if (!open_container_key("", dwFlags, KEY_READ, &hKey))
3774 SetLastError(ERROR_NO_MORE_ITEMS);
3775 return FALSE;
3778 dwTemp = *pdwDataLen;
3779 switch (RegEnumKeyExA(hKey, pKeyContainer->dwEnumContainersCtr, (LPSTR)pbData, &dwTemp,
3780 NULL, NULL, NULL, NULL))
3782 case ERROR_MORE_DATA:
3783 *pdwDataLen = (DWORD)MAX_PATH + 1;
3785 case ERROR_SUCCESS:
3786 pKeyContainer->dwEnumContainersCtr++;
3787 RegCloseKey(hKey);
3788 return TRUE;
3790 case ERROR_NO_MORE_ITEMS:
3791 default:
3792 SetLastError(ERROR_NO_MORE_ITEMS);
3793 RegCloseKey(hKey);
3794 return FALSE;
3797 case PP_ENUMALGS:
3798 case PP_ENUMALGS_EX:
3799 if (((pKeyContainer->dwEnumAlgsCtr >= RSAENH_MAX_ENUMALGS-1) ||
3800 (!aProvEnumAlgsEx[pKeyContainer->dwPersonality]
3801 [pKeyContainer->dwEnumAlgsCtr+1].aiAlgid)) &&
3802 ((dwFlags & CRYPT_FIRST) != CRYPT_FIRST))
3804 SetLastError(ERROR_NO_MORE_ITEMS);
3805 return FALSE;
3808 if (dwParam == PP_ENUMALGS) {
3809 if (pbData && (*pdwDataLen >= sizeof(PROV_ENUMALGS)))
3810 pKeyContainer->dwEnumAlgsCtr = ((dwFlags & CRYPT_FIRST) == CRYPT_FIRST) ?
3811 0 : pKeyContainer->dwEnumAlgsCtr+1;
3813 provEnumalgs.aiAlgid = aProvEnumAlgsEx
3814 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr].aiAlgid;
3815 provEnumalgs.dwBitLen = aProvEnumAlgsEx
3816 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr].dwDefaultLen;
3817 provEnumalgs.dwNameLen = aProvEnumAlgsEx
3818 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr].dwNameLen;
3819 memcpy(provEnumalgs.szName, aProvEnumAlgsEx
3820 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr].szName,
3821 20*sizeof(CHAR));
3823 return copy_param(pbData, pdwDataLen, (const BYTE*)&provEnumalgs,
3824 sizeof(PROV_ENUMALGS));
3825 } else {
3826 if (pbData && (*pdwDataLen >= sizeof(PROV_ENUMALGS_EX)))
3827 pKeyContainer->dwEnumAlgsCtr = ((dwFlags & CRYPT_FIRST) == CRYPT_FIRST) ?
3828 0 : pKeyContainer->dwEnumAlgsCtr+1;
3830 return copy_param(pbData, pdwDataLen,
3831 (const BYTE*)&aProvEnumAlgsEx
3832 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr],
3833 sizeof(PROV_ENUMALGS_EX));
3836 case PP_CRYPT_COUNT_KEY_USE: /* Asked for by IE About dialog */
3837 return copy_param(pbData, pdwDataLen, abWTF, sizeof(abWTF));
3839 case PP_KEYSET_SEC_DESCR:
3841 SECURITY_DESCRIPTOR *sd;
3842 DWORD err, len, flags = (pKeyContainer->dwFlags & CRYPT_MACHINE_KEYSET);
3844 if (!open_container_key(pKeyContainer->szName, flags, KEY_READ, &hKey))
3846 SetLastError(NTE_BAD_KEYSET);
3847 return FALSE;
3850 err = GetSecurityInfo(hKey, SE_REGISTRY_KEY, dwFlags, NULL, NULL, NULL, NULL, (void **)&sd);
3851 RegCloseKey(hKey);
3852 if (err)
3854 SetLastError(err);
3855 return FALSE;
3858 len = GetSecurityDescriptorLength(sd);
3859 if (*pdwDataLen >= len) memcpy(pbData, sd, len);
3860 else SetLastError(ERROR_INSUFFICIENT_BUFFER);
3861 *pdwDataLen = len;
3863 LocalFree(sd);
3864 return TRUE;
3867 default:
3868 /* MSDN: Unknown parameter number in dwParam */
3869 SetLastError(NTE_BAD_TYPE);
3870 return FALSE;
3874 /******************************************************************************
3875 * CPDeriveKey (RSAENH.@)
3877 * Derives a key from a hash value.
3879 * PARAMS
3880 * hProv [I] Key container for which a key is to be generated.
3881 * Algid [I] Crypto algorithm identifier for the key to be generated.
3882 * hBaseData [I] Hash from whose value the key will be derived.
3883 * dwFlags [I] See Notes.
3884 * phKey [O] The generated key.
3886 * RETURNS
3887 * Success: TRUE
3888 * Failure: FALSE
3890 * NOTES
3891 * Defined flags:
3892 * - CRYPT_EXPORTABLE: Key can be exported.
3893 * - CRYPT_NO_SALT: No salt is used for 40 bit keys.
3894 * - CRYPT_CREATE_SALT: Use remaining bits as salt value.
3896 BOOL WINAPI RSAENH_CPDeriveKey(HCRYPTPROV hProv, ALG_ID Algid, HCRYPTHASH hBaseData,
3897 DWORD dwFlags, HCRYPTKEY *phKey)
3899 CRYPTKEY *pCryptKey, *pMasterKey;
3900 CRYPTHASH *pCryptHash;
3901 BYTE abHashValue[RSAENH_MAX_HASH_SIZE*2];
3902 DWORD dwLen;
3904 TRACE("(hProv=%08lx, Algid=%d, hBaseData=%08lx, dwFlags=%08x phKey=%p)\n", hProv, Algid,
3905 hBaseData, dwFlags, phKey);
3907 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
3909 SetLastError(NTE_BAD_UID);
3910 return FALSE;
3913 if (!lookup_handle(&handle_table, hBaseData, RSAENH_MAGIC_HASH,
3914 (OBJECTHDR**)&pCryptHash))
3916 SetLastError(NTE_BAD_HASH);
3917 return FALSE;
3920 if (!phKey)
3922 SetLastError(ERROR_INVALID_PARAMETER);
3923 return FALSE;
3926 switch (GET_ALG_CLASS(Algid))
3928 case ALG_CLASS_DATA_ENCRYPT:
3929 *phKey = new_key(hProv, Algid, dwFlags, &pCryptKey);
3930 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) return FALSE;
3933 * We derive the key material from the hash.
3934 * If the hash value is not large enough for the claimed key, we have to construct
3935 * a larger binary value based on the hash. This is documented in MSDN: CryptDeriveKey.
3937 dwLen = RSAENH_MAX_HASH_SIZE;
3938 RSAENH_CPGetHashParam(pCryptHash->hProv, hBaseData, HP_HASHVAL, abHashValue, &dwLen, 0);
3940 if (dwLen < pCryptKey->dwKeyLen) {
3941 BYTE pad1[RSAENH_HMAC_DEF_PAD_LEN], pad2[RSAENH_HMAC_DEF_PAD_LEN];
3942 BYTE old_hashval[RSAENH_MAX_HASH_SIZE];
3943 DWORD i;
3945 memcpy(old_hashval, pCryptHash->abHashValue, RSAENH_MAX_HASH_SIZE);
3947 for (i=0; i<RSAENH_HMAC_DEF_PAD_LEN; i++) {
3948 pad1[i] = RSAENH_HMAC_DEF_IPAD_CHAR ^ (i<dwLen ? abHashValue[i] : 0);
3949 pad2[i] = RSAENH_HMAC_DEF_OPAD_CHAR ^ (i<dwLen ? abHashValue[i] : 0);
3952 init_hash(pCryptHash);
3953 update_hash(pCryptHash, pad1, RSAENH_HMAC_DEF_PAD_LEN);
3954 finalize_hash(pCryptHash);
3955 memcpy(abHashValue, pCryptHash->abHashValue, pCryptHash->dwHashSize);
3957 init_hash(pCryptHash);
3958 update_hash(pCryptHash, pad2, RSAENH_HMAC_DEF_PAD_LEN);
3959 finalize_hash(pCryptHash);
3960 memcpy(abHashValue+pCryptHash->dwHashSize, pCryptHash->abHashValue,
3961 pCryptHash->dwHashSize);
3963 memcpy(pCryptHash->abHashValue, old_hashval, RSAENH_MAX_HASH_SIZE);
3966 memcpy(pCryptKey->abKeyValue, abHashValue,
3967 RSAENH_MIN(pCryptKey->dwKeyLen, sizeof(pCryptKey->abKeyValue)));
3968 break;
3970 case ALG_CLASS_MSG_ENCRYPT:
3971 if (!lookup_handle(&handle_table, pCryptHash->hKey, RSAENH_MAGIC_KEY,
3972 (OBJECTHDR**)&pMasterKey))
3974 SetLastError(NTE_FAIL); /* FIXME error code */
3975 return FALSE;
3978 switch (Algid)
3980 /* See RFC 2246, chapter 6.3 Key calculation */
3981 case CALG_SCHANNEL_ENC_KEY:
3982 if (!pMasterKey->siSChannelInfo.saEncAlg.Algid ||
3983 !pMasterKey->siSChannelInfo.saEncAlg.cBits)
3985 SetLastError(NTE_BAD_FLAGS);
3986 return FALSE;
3988 *phKey = new_key(hProv, pMasterKey->siSChannelInfo.saEncAlg.Algid,
3989 MAKELONG(LOWORD(dwFlags),pMasterKey->siSChannelInfo.saEncAlg.cBits),
3990 &pCryptKey);
3991 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) return FALSE;
3992 memcpy(pCryptKey->abKeyValue,
3993 pCryptHash->abHashValue + (
3994 2 * (pMasterKey->siSChannelInfo.saMACAlg.cBits / 8) +
3995 ((dwFlags & CRYPT_SERVER) ?
3996 (pMasterKey->siSChannelInfo.saEncAlg.cBits / 8) : 0)),
3997 pMasterKey->siSChannelInfo.saEncAlg.cBits / 8);
3998 memcpy(pCryptKey->abInitVector,
3999 pCryptHash->abHashValue + (
4000 2 * (pMasterKey->siSChannelInfo.saMACAlg.cBits / 8) +
4001 2 * (pMasterKey->siSChannelInfo.saEncAlg.cBits / 8) +
4002 ((dwFlags & CRYPT_SERVER) ? pCryptKey->dwBlockLen : 0)),
4003 pCryptKey->dwBlockLen);
4004 break;
4006 case CALG_SCHANNEL_MAC_KEY:
4007 *phKey = new_key(hProv, Algid,
4008 MAKELONG(LOWORD(dwFlags),pMasterKey->siSChannelInfo.saMACAlg.cBits),
4009 &pCryptKey);
4010 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) return FALSE;
4011 memcpy(pCryptKey->abKeyValue,
4012 pCryptHash->abHashValue + ((dwFlags & CRYPT_SERVER) ?
4013 pMasterKey->siSChannelInfo.saMACAlg.cBits / 8 : 0),
4014 pMasterKey->siSChannelInfo.saMACAlg.cBits / 8);
4015 break;
4017 default:
4018 SetLastError(NTE_BAD_ALGID);
4019 return FALSE;
4021 break;
4023 default:
4024 SetLastError(NTE_BAD_ALGID);
4025 return FALSE;
4028 setup_key(pCryptKey);
4029 return TRUE;
4032 /******************************************************************************
4033 * CPGetUserKey (RSAENH.@)
4035 * Returns a handle to the user's private key-exchange- or signature-key.
4037 * PARAMS
4038 * hProv [I] The key container from which a user key is requested.
4039 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
4040 * phUserKey [O] Handle to the requested key or INVALID_HANDLE_VALUE in case of failure.
4042 * RETURNS
4043 * Success: TRUE.
4044 * Failure: FALSE.
4046 * NOTE
4047 * A newly created key container does not contain private user key. Create them with CPGenKey.
4049 BOOL WINAPI RSAENH_CPGetUserKey(HCRYPTPROV hProv, DWORD dwKeySpec, HCRYPTKEY *phUserKey)
4051 KEYCONTAINER *pKeyContainer;
4053 TRACE("(hProv=%08lx, dwKeySpec=%08x, phUserKey=%p)\n", hProv, dwKeySpec, phUserKey);
4055 if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER,
4056 (OBJECTHDR**)&pKeyContainer))
4058 /* MSDN: hProv not containing valid context handle */
4059 SetLastError(NTE_BAD_UID);
4060 return FALSE;
4063 switch (dwKeySpec)
4065 case AT_KEYEXCHANGE:
4066 copy_handle(&handle_table, pKeyContainer->hKeyExchangeKeyPair, RSAENH_MAGIC_KEY,
4067 phUserKey);
4068 break;
4070 case AT_SIGNATURE:
4071 copy_handle(&handle_table, pKeyContainer->hSignatureKeyPair, RSAENH_MAGIC_KEY,
4072 phUserKey);
4073 break;
4075 default:
4076 *phUserKey = (HCRYPTKEY)INVALID_HANDLE_VALUE;
4079 if (*phUserKey == (HCRYPTKEY)INVALID_HANDLE_VALUE)
4081 /* MSDN: dwKeySpec parameter specifies nonexistent key */
4082 SetLastError(NTE_NO_KEY);
4083 return FALSE;
4086 return TRUE;
4089 /******************************************************************************
4090 * CPHashData (RSAENH.@)
4092 * Updates a hash object with the given data.
4094 * PARAMS
4095 * hProv [I] Key container to which the hash object belongs.
4096 * hHash [I] Hash object which is to be updated.
4097 * pbData [I] Pointer to data with which the hash object is to be updated.
4098 * dwDataLen [I] Length of the data.
4099 * dwFlags [I] Currently none defined.
4101 * RETURNS
4102 * Success: TRUE.
4103 * Failure: FALSE.
4105 * NOTES
4106 * The actual hash value is queried with CPGetHashParam, which will finalize
4107 * the hash. Updating a finalized hash will fail with a last error NTE_BAD_HASH_STATE.
4109 BOOL WINAPI RSAENH_CPHashData(HCRYPTPROV hProv, HCRYPTHASH hHash, const BYTE *pbData,
4110 DWORD dwDataLen, DWORD dwFlags)
4112 CRYPTHASH *pCryptHash;
4114 TRACE("(hProv=%08lx, hHash=%08lx, pbData=%p, dwDataLen=%d, dwFlags=%08x)\n",
4115 hProv, hHash, pbData, dwDataLen, dwFlags);
4117 if (dwFlags & ~CRYPT_USERDATA)
4119 SetLastError(NTE_BAD_FLAGS);
4120 return FALSE;
4123 if (!lookup_handle(&handle_table, hHash, RSAENH_MAGIC_HASH,
4124 (OBJECTHDR**)&pCryptHash))
4126 SetLastError(NTE_BAD_HASH);
4127 return FALSE;
4130 if (!get_algid_info(hProv, pCryptHash->aiAlgid) || pCryptHash->aiAlgid == CALG_SSL3_SHAMD5)
4132 SetLastError(NTE_BAD_ALGID);
4133 return FALSE;
4136 if (pCryptHash->dwState != RSAENH_HASHSTATE_HASHING)
4138 SetLastError(NTE_BAD_HASH_STATE);
4139 return FALSE;
4142 update_hash(pCryptHash, pbData, dwDataLen);
4143 return TRUE;
4146 /******************************************************************************
4147 * CPHashSessionKey (RSAENH.@)
4149 * Updates a hash object with the binary representation of a symmetric key.
4151 * PARAMS
4152 * hProv [I] Key container to which the hash object belongs.
4153 * hHash [I] Hash object which is to be updated.
4154 * hKey [I] The symmetric key, whose binary value will be added to the hash.
4155 * dwFlags [I] CRYPT_LITTLE_ENDIAN, if the binary key value shall be interpreted as little endian.
4157 * RETURNS
4158 * Success: TRUE.
4159 * Failure: FALSE.
4161 BOOL WINAPI RSAENH_CPHashSessionKey(HCRYPTPROV hProv, HCRYPTHASH hHash, HCRYPTKEY hKey,
4162 DWORD dwFlags)
4164 BYTE abKeyValue[RSAENH_MAX_KEY_SIZE], bTemp;
4165 CRYPTKEY *pKey;
4166 DWORD i;
4168 TRACE("(hProv=%08lx, hHash=%08lx, hKey=%08lx, dwFlags=%08x)\n", hProv, hHash, hKey, dwFlags);
4170 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pKey) ||
4171 (GET_ALG_CLASS(pKey->aiAlgid) != ALG_CLASS_DATA_ENCRYPT))
4173 SetLastError(NTE_BAD_KEY);
4174 return FALSE;
4177 if (dwFlags & ~CRYPT_LITTLE_ENDIAN) {
4178 SetLastError(NTE_BAD_FLAGS);
4179 return FALSE;
4182 memcpy(abKeyValue, pKey->abKeyValue, pKey->dwKeyLen);
4183 if (!(dwFlags & CRYPT_LITTLE_ENDIAN)) {
4184 for (i=0; i<pKey->dwKeyLen/2; i++) {
4185 bTemp = abKeyValue[i];
4186 abKeyValue[i] = abKeyValue[pKey->dwKeyLen-i-1];
4187 abKeyValue[pKey->dwKeyLen-i-1] = bTemp;
4191 return RSAENH_CPHashData(hProv, hHash, abKeyValue, pKey->dwKeyLen, 0);
4194 /******************************************************************************
4195 * CPReleaseContext (RSAENH.@)
4197 * Release a key container.
4199 * PARAMS
4200 * hProv [I] Key container to be released.
4201 * dwFlags [I] Currently none defined.
4203 * RETURNS
4204 * Success: TRUE
4205 * Failure: FALSE
4207 BOOL WINAPI RSAENH_CPReleaseContext(HCRYPTPROV hProv, DWORD dwFlags)
4209 TRACE("(hProv=%08lx, dwFlags=%08x)\n", hProv, dwFlags);
4211 if (!release_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
4213 /* MSDN: hProv not containing valid context handle */
4214 SetLastError(NTE_BAD_UID);
4215 return FALSE;
4218 if (dwFlags) {
4219 SetLastError(NTE_BAD_FLAGS);
4220 return FALSE;
4223 return TRUE;
4226 /******************************************************************************
4227 * CPSetHashParam (RSAENH.@)
4229 * Set a parameter of a hash object
4231 * PARAMS
4232 * hProv [I] The key container to which the key belongs.
4233 * hHash [I] The hash object for which a parameter is to be set.
4234 * dwParam [I] Parameter type. See Notes.
4235 * pbData [I] Pointer to the parameter value.
4236 * dwFlags [I] Currently none defined.
4238 * RETURNS
4239 * Success: TRUE.
4240 * Failure: FALSE.
4242 * NOTES
4243 * Currently only the HP_HMAC_INFO dwParam type is defined.
4244 * The HMAC_INFO struct will be deep copied into the hash object.
4245 * See Internet RFC 2104 for details on the HMAC algorithm.
4247 BOOL WINAPI RSAENH_CPSetHashParam(HCRYPTPROV hProv, HCRYPTHASH hHash, DWORD dwParam,
4248 BYTE *pbData, DWORD dwFlags)
4250 CRYPTHASH *pCryptHash;
4251 CRYPTKEY *pCryptKey;
4252 DWORD i;
4254 TRACE("(hProv=%08lx, hHash=%08lx, dwParam=%08x, pbData=%p, dwFlags=%08x)\n",
4255 hProv, hHash, dwParam, pbData, dwFlags);
4257 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
4259 SetLastError(NTE_BAD_UID);
4260 return FALSE;
4263 if (dwFlags) {
4264 SetLastError(NTE_BAD_FLAGS);
4265 return FALSE;
4268 if (!lookup_handle(&handle_table, hHash, RSAENH_MAGIC_HASH,
4269 (OBJECTHDR**)&pCryptHash))
4271 SetLastError(NTE_BAD_HASH);
4272 return FALSE;
4275 switch (dwParam) {
4276 case HP_HMAC_INFO:
4277 free_hmac_info(pCryptHash->pHMACInfo);
4278 if (!copy_hmac_info(&pCryptHash->pHMACInfo, (PHMAC_INFO)pbData)) return FALSE;
4280 if (!lookup_handle(&handle_table, pCryptHash->hKey, RSAENH_MAGIC_KEY,
4281 (OBJECTHDR**)&pCryptKey))
4283 SetLastError(NTE_FAIL); /* FIXME: correct error code? */
4284 return FALSE;
4287 if (pCryptKey->aiAlgid == CALG_HMAC && !pCryptKey->dwKeyLen) {
4288 HCRYPTHASH hKeyHash;
4289 DWORD keyLen;
4291 if (!RSAENH_CPCreateHash(hProv, ((PHMAC_INFO)pbData)->HashAlgid, 0, 0,
4292 &hKeyHash))
4293 return FALSE;
4294 if (!RSAENH_CPHashData(hProv, hKeyHash, pCryptKey->blobHmacKey.pbData,
4295 pCryptKey->blobHmacKey.cbData, 0))
4297 RSAENH_CPDestroyHash(hProv, hKeyHash);
4298 return FALSE;
4300 keyLen = sizeof(pCryptKey->abKeyValue);
4301 if (!RSAENH_CPGetHashParam(hProv, hKeyHash, HP_HASHVAL, pCryptKey->abKeyValue,
4302 &keyLen, 0))
4304 RSAENH_CPDestroyHash(hProv, hKeyHash);
4305 return FALSE;
4307 pCryptKey->dwKeyLen = keyLen;
4308 RSAENH_CPDestroyHash(hProv, hKeyHash);
4310 for (i=0; i<RSAENH_MIN(pCryptKey->dwKeyLen,pCryptHash->pHMACInfo->cbInnerString); i++) {
4311 pCryptHash->pHMACInfo->pbInnerString[i] ^= pCryptKey->abKeyValue[i];
4313 for (i=0; i<RSAENH_MIN(pCryptKey->dwKeyLen,pCryptHash->pHMACInfo->cbOuterString); i++) {
4314 pCryptHash->pHMACInfo->pbOuterString[i] ^= pCryptKey->abKeyValue[i];
4317 init_hash(pCryptHash);
4318 return TRUE;
4320 case HP_HASHVAL:
4321 memcpy(pCryptHash->abHashValue, pbData, pCryptHash->dwHashSize);
4322 pCryptHash->dwState = RSAENH_HASHSTATE_FINISHED;
4323 return TRUE;
4325 case HP_TLS1PRF_SEED:
4326 return copy_data_blob(&pCryptHash->tpPRFParams.blobSeed, (PCRYPT_DATA_BLOB)pbData);
4328 case HP_TLS1PRF_LABEL:
4329 return copy_data_blob(&pCryptHash->tpPRFParams.blobLabel, (PCRYPT_DATA_BLOB)pbData);
4331 default:
4332 SetLastError(NTE_BAD_TYPE);
4333 return FALSE;
4337 /******************************************************************************
4338 * CPSetProvParam (RSAENH.@)
4340 BOOL WINAPI RSAENH_CPSetProvParam(HCRYPTPROV hProv, DWORD dwParam, BYTE *pbData, DWORD dwFlags)
4342 KEYCONTAINER *pKeyContainer;
4343 HKEY hKey;
4345 TRACE("(hProv=%08lx, dwParam=%08x, pbData=%p, dwFlags=%08x)\n", hProv, dwParam, pbData, dwFlags);
4347 if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER, (OBJECTHDR **)&pKeyContainer))
4349 SetLastError(NTE_BAD_UID);
4350 return FALSE;
4353 switch (dwParam)
4355 case PP_KEYSET_SEC_DESCR:
4357 SECURITY_DESCRIPTOR *sd = (SECURITY_DESCRIPTOR *)pbData;
4358 DWORD err, flags = (pKeyContainer->dwFlags & CRYPT_MACHINE_KEYSET);
4359 BOOL def, present;
4360 REGSAM access = WRITE_DAC | WRITE_OWNER | ACCESS_SYSTEM_SECURITY;
4361 PSID owner = NULL, group = NULL;
4362 PACL dacl = NULL, sacl = NULL;
4364 if (!open_container_key(pKeyContainer->szName, flags, access, &hKey))
4366 SetLastError(NTE_BAD_KEYSET);
4367 return FALSE;
4370 if ((dwFlags & OWNER_SECURITY_INFORMATION && !GetSecurityDescriptorOwner(sd, &owner, &def)) ||
4371 (dwFlags & GROUP_SECURITY_INFORMATION && !GetSecurityDescriptorGroup(sd, &group, &def)) ||
4372 (dwFlags & DACL_SECURITY_INFORMATION && !GetSecurityDescriptorDacl(sd, &present, &dacl, &def)) ||
4373 (dwFlags & SACL_SECURITY_INFORMATION && !GetSecurityDescriptorSacl(sd, &present, &sacl, &def)))
4375 RegCloseKey(hKey);
4376 return FALSE;
4379 err = SetSecurityInfo(hKey, SE_REGISTRY_KEY, dwFlags, owner, group, dacl, sacl);
4380 RegCloseKey(hKey);
4381 if (err)
4383 SetLastError(err);
4384 return FALSE;
4386 return TRUE;
4388 default:
4389 FIXME("unimplemented parameter %08x\n", dwParam);
4390 return FALSE;
4394 /******************************************************************************
4395 * CPSignHash (RSAENH.@)
4397 * Sign a hash object
4399 * PARAMS
4400 * hProv [I] The key container, to which the hash object belongs.
4401 * hHash [I] The hash object to be signed.
4402 * dwKeySpec [I] AT_SIGNATURE or AT_KEYEXCHANGE: Key used to generate the signature.
4403 * sDescription [I] Should be NULL for security reasons.
4404 * dwFlags [I] 0, CRYPT_NOHASHOID or CRYPT_X931_FORMAT: Format of the signature.
4405 * pbSignature [O] Buffer, to which the signature will be stored. May be NULL to query SigLen.
4406 * pdwSigLen [I/O] Size of the buffer (in), Length of the signature (out)
4408 * RETURNS
4409 * Success: TRUE
4410 * Failure: FALSE
4412 BOOL WINAPI RSAENH_CPSignHash(HCRYPTPROV hProv, HCRYPTHASH hHash, DWORD dwKeySpec,
4413 LPCWSTR sDescription, DWORD dwFlags, BYTE *pbSignature,
4414 DWORD *pdwSigLen)
4416 HCRYPTKEY hCryptKey = (HCRYPTKEY)INVALID_HANDLE_VALUE;
4417 CRYPTKEY *pCryptKey;
4418 DWORD dwHashLen;
4419 BYTE abHashValue[RSAENH_MAX_HASH_SIZE];
4420 ALG_ID aiAlgid;
4421 BOOL ret = FALSE;
4423 TRACE("(hProv=%08lx, hHash=%08lx, dwKeySpec=%08x, sDescription=%s, dwFlags=%08x, "
4424 "pbSignature=%p, pdwSigLen=%p)\n", hProv, hHash, dwKeySpec, debugstr_w(sDescription),
4425 dwFlags, pbSignature, pdwSigLen);
4427 if (dwFlags & ~(CRYPT_NOHASHOID|CRYPT_X931_FORMAT)) {
4428 SetLastError(NTE_BAD_FLAGS);
4429 return FALSE;
4432 if (!RSAENH_CPGetUserKey(hProv, dwKeySpec, &hCryptKey)) return FALSE;
4434 if (!lookup_handle(&handle_table, hCryptKey, RSAENH_MAGIC_KEY,
4435 (OBJECTHDR**)&pCryptKey))
4437 SetLastError(NTE_NO_KEY);
4438 goto out;
4441 if (!pbSignature) {
4442 *pdwSigLen = pCryptKey->dwKeyLen;
4443 ret = TRUE;
4444 goto out;
4446 if (pCryptKey->dwKeyLen > *pdwSigLen)
4448 SetLastError(ERROR_MORE_DATA);
4449 *pdwSigLen = pCryptKey->dwKeyLen;
4450 goto out;
4452 *pdwSigLen = pCryptKey->dwKeyLen;
4454 if (sDescription) {
4455 if (!RSAENH_CPHashData(hProv, hHash, (const BYTE*)sDescription,
4456 (DWORD)lstrlenW(sDescription)*sizeof(WCHAR), 0))
4458 goto out;
4462 dwHashLen = sizeof(DWORD);
4463 if (!RSAENH_CPGetHashParam(hProv, hHash, HP_ALGID, (BYTE*)&aiAlgid, &dwHashLen, 0)) goto out;
4465 dwHashLen = RSAENH_MAX_HASH_SIZE;
4466 if (!RSAENH_CPGetHashParam(hProv, hHash, HP_HASHVAL, abHashValue, &dwHashLen, 0)) goto out;
4469 if (!build_hash_signature(pbSignature, *pdwSigLen, aiAlgid, abHashValue, dwHashLen, dwFlags)) {
4470 goto out;
4473 ret = encrypt_block_impl(pCryptKey->aiAlgid, PK_PRIVATE, &pCryptKey->context, pbSignature, pbSignature, RSAENH_ENCRYPT);
4474 out:
4475 RSAENH_CPDestroyKey(hProv, hCryptKey);
4476 return ret;
4479 /******************************************************************************
4480 * CPVerifySignature (RSAENH.@)
4482 * Verify the signature of a hash object.
4484 * PARAMS
4485 * hProv [I] The key container, to which the hash belongs.
4486 * hHash [I] The hash for which the signature is verified.
4487 * pbSignature [I] The binary signature.
4488 * dwSigLen [I] Length of the signature BLOB.
4489 * hPubKey [I] Public key used to verify the signature.
4490 * sDescription [I] Should be NULL for security reasons.
4491 * dwFlags [I] 0, CRYPT_NOHASHOID or CRYPT_X931_FORMAT: Format of the signature.
4493 * RETURNS
4494 * Success: TRUE (Signature is valid)
4495 * Failure: FALSE (GetLastError() == NTE_BAD_SIGNATURE, if signature is invalid)
4497 BOOL WINAPI RSAENH_CPVerifySignature(HCRYPTPROV hProv, HCRYPTHASH hHash, const BYTE *pbSignature,
4498 DWORD dwSigLen, HCRYPTKEY hPubKey, LPCWSTR sDescription,
4499 DWORD dwFlags)
4501 BYTE *pbConstructed = NULL, *pbDecrypted = NULL;
4502 CRYPTKEY *pCryptKey;
4503 DWORD dwHashLen;
4504 ALG_ID aiAlgid;
4505 BYTE abHashValue[RSAENH_MAX_HASH_SIZE];
4506 BOOL res = FALSE;
4508 TRACE("(hProv=%08lx, hHash=%08lx, pbSignature=%p, dwSigLen=%d, hPubKey=%08lx, sDescription=%s, "
4509 "dwFlags=%08x)\n", hProv, hHash, pbSignature, dwSigLen, hPubKey, debugstr_w(sDescription),
4510 dwFlags);
4512 if (dwFlags & ~(CRYPT_NOHASHOID|CRYPT_X931_FORMAT)) {
4513 SetLastError(NTE_BAD_FLAGS);
4514 return FALSE;
4517 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
4519 SetLastError(NTE_BAD_UID);
4520 return FALSE;
4523 if (!lookup_handle(&handle_table, hPubKey, RSAENH_MAGIC_KEY,
4524 (OBJECTHDR**)&pCryptKey))
4526 SetLastError(NTE_BAD_KEY);
4527 return FALSE;
4530 /* in Microsoft implementation, the signature length is checked before
4531 * the signature pointer.
4533 if (dwSigLen != pCryptKey->dwKeyLen)
4535 SetLastError(NTE_BAD_SIGNATURE);
4536 return FALSE;
4539 if (!hHash || !pbSignature)
4541 SetLastError(ERROR_INVALID_PARAMETER);
4542 return FALSE;
4545 if (sDescription) {
4546 if (!RSAENH_CPHashData(hProv, hHash, (const BYTE*)sDescription,
4547 (DWORD)lstrlenW(sDescription)*sizeof(WCHAR), 0))
4549 return FALSE;
4553 dwHashLen = sizeof(DWORD);
4554 if (!RSAENH_CPGetHashParam(hProv, hHash, HP_ALGID, (BYTE*)&aiAlgid, &dwHashLen, 0)) return FALSE;
4556 dwHashLen = RSAENH_MAX_HASH_SIZE;
4557 if (!RSAENH_CPGetHashParam(hProv, hHash, HP_HASHVAL, abHashValue, &dwHashLen, 0)) return FALSE;
4559 pbConstructed = HeapAlloc(GetProcessHeap(), 0, dwSigLen);
4560 if (!pbConstructed) {
4561 SetLastError(NTE_NO_MEMORY);
4562 goto cleanup;
4565 pbDecrypted = HeapAlloc(GetProcessHeap(), 0, dwSigLen);
4566 if (!pbDecrypted) {
4567 SetLastError(NTE_NO_MEMORY);
4568 goto cleanup;
4571 if (!encrypt_block_impl(pCryptKey->aiAlgid, PK_PUBLIC, &pCryptKey->context, pbSignature, pbDecrypted,
4572 RSAENH_DECRYPT))
4574 goto cleanup;
4577 if (build_hash_signature(pbConstructed, dwSigLen, aiAlgid, abHashValue, dwHashLen, dwFlags) &&
4578 !memcmp(pbDecrypted, pbConstructed, dwSigLen)) {
4579 res = TRUE;
4580 goto cleanup;
4583 if (!(dwFlags & CRYPT_NOHASHOID) &&
4584 build_hash_signature(pbConstructed, dwSigLen, aiAlgid, abHashValue, dwHashLen, dwFlags|CRYPT_NOHASHOID) &&
4585 !memcmp(pbDecrypted, pbConstructed, dwSigLen)) {
4586 res = TRUE;
4587 goto cleanup;
4590 SetLastError(NTE_BAD_SIGNATURE);
4592 cleanup:
4593 HeapFree(GetProcessHeap(), 0, pbConstructed);
4594 HeapFree(GetProcessHeap(), 0, pbDecrypted);
4595 return res;
4598 /******************************************************************************
4599 * DllRegisterServer (RSAENH.@)
4601 HRESULT WINAPI DllRegisterServer(void)
4603 return __wine_register_resources( instance );
4606 /******************************************************************************
4607 * DllUnregisterServer (RSAENH.@)
4609 HRESULT WINAPI DllUnregisterServer(void)
4611 return __wine_unregister_resources( instance );