dwrite: Ignore fonts with dot-prefixed names.
[wine.git] / dlls / rsaenh / rsaenh.c
blobebafa0eb3701fad5776dace37e85e17edc4c35a0
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-384)"},
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 static inline KEYCONTAINER* get_key_container(HCRYPTPROV hProv)
417 KEYCONTAINER *pKeyContainer;
419 if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER,
420 (OBJECTHDR**)&pKeyContainer))
422 SetLastError(NTE_BAD_UID);
423 return NULL;
425 return pKeyContainer;
428 /******************************************************************************
429 * get_algid_info [Internal]
431 * Query CSP capabilities for a given crypto algorithm.
433 * PARAMS
434 * hProv [I] Handle to a key container of the CSP whose capabilities are to be queried.
435 * algid [I] Identifier of the crypto algorithm about which information is requested.
437 * RETURNS
438 * Success: Pointer to a PROV_ENUMALGS_EX struct containing information about the crypto algorithm.
439 * Failure: NULL (algid not supported)
441 static inline const PROV_ENUMALGS_EX* get_algid_info(HCRYPTPROV hProv, ALG_ID algid) {
442 const PROV_ENUMALGS_EX *iterator;
443 KEYCONTAINER *pKeyContainer;
445 if (!(pKeyContainer = get_key_container(hProv))) return NULL;
447 for (iterator = aProvEnumAlgsEx[pKeyContainer->dwPersonality]; iterator->aiAlgid; iterator++) {
448 if (iterator->aiAlgid == algid) return iterator;
451 SetLastError(NTE_BAD_ALGID);
452 return NULL;
455 /******************************************************************************
456 * copy_data_blob [Internal]
458 * deeply copies a DATA_BLOB
460 * PARAMS
461 * dst [O] That's where the blob will be copied to
462 * src [I] Source blob
464 * RETURNS
465 * Success: TRUE
466 * Failure: FALSE (GetLastError() == NTE_NO_MEMORY
468 * NOTES
469 * Use free_data_blob to release resources occupied by copy_data_blob.
471 static inline BOOL copy_data_blob(PCRYPT_DATA_BLOB dst, const PCRYPT_DATA_BLOB src)
473 dst->pbData = HeapAlloc(GetProcessHeap(), 0, src->cbData);
474 if (!dst->pbData) {
475 SetLastError(NTE_NO_MEMORY);
476 return FALSE;
478 dst->cbData = src->cbData;
479 memcpy(dst->pbData, src->pbData, src->cbData);
480 return TRUE;
483 /******************************************************************************
484 * concat_data_blobs [Internal]
486 * Concatenates two blobs
488 * PARAMS
489 * dst [O] The new blob will be copied here
490 * src1 [I] Prefix blob
491 * src2 [I] Appendix blob
493 * RETURNS
494 * Success: TRUE
495 * Failure: FALSE (GetLastError() == NTE_NO_MEMORY)
497 * NOTES
498 * Release resources occupied by concat_data_blobs with free_data_blobs
500 static inline BOOL concat_data_blobs(PCRYPT_DATA_BLOB dst, const PCRYPT_DATA_BLOB src1,
501 const PCRYPT_DATA_BLOB src2)
503 dst->cbData = src1->cbData + src2->cbData;
504 dst->pbData = HeapAlloc(GetProcessHeap(), 0, dst->cbData);
505 if (!dst->pbData) {
506 SetLastError(NTE_NO_MEMORY);
507 return FALSE;
509 memcpy(dst->pbData, src1->pbData, src1->cbData);
510 memcpy(dst->pbData + src1->cbData, src2->pbData, src2->cbData);
511 return TRUE;
514 /******************************************************************************
515 * free_data_blob [Internal]
517 * releases resource occupied by a dynamically allocated CRYPT_DATA_BLOB
519 * PARAMS
520 * pBlob [I] Heap space occupied by pBlob->pbData is released
522 static inline void free_data_blob(PCRYPT_DATA_BLOB pBlob) {
523 HeapFree(GetProcessHeap(), 0, pBlob->pbData);
526 /******************************************************************************
527 * init_data_blob [Internal]
529 static inline void init_data_blob(PCRYPT_DATA_BLOB pBlob) {
530 pBlob->pbData = NULL;
531 pBlob->cbData = 0;
534 /******************************************************************************
535 * free_hmac_info [Internal]
537 * Deeply free an HMAC_INFO struct.
539 * PARAMS
540 * hmac_info [I] Pointer to the HMAC_INFO struct to be freed.
542 * NOTES
543 * See Internet RFC 2104 for details on the HMAC algorithm.
545 static inline void free_hmac_info(PHMAC_INFO hmac_info) {
546 if (!hmac_info) return;
547 HeapFree(GetProcessHeap(), 0, hmac_info->pbInnerString);
548 HeapFree(GetProcessHeap(), 0, hmac_info->pbOuterString);
549 HeapFree(GetProcessHeap(), 0, hmac_info);
552 /******************************************************************************
553 * copy_hmac_info [Internal]
555 * Deeply copy an HMAC_INFO struct
557 * PARAMS
558 * dst [O] Pointer to a location where the pointer to the HMAC_INFO copy will be stored.
559 * src [I] Pointer to the HMAC_INFO struct to be copied.
561 * RETURNS
562 * Success: TRUE
563 * Failure: FALSE
565 * NOTES
566 * See Internet RFC 2104 for details on the HMAC algorithm.
568 static BOOL copy_hmac_info(PHMAC_INFO *dst, const HMAC_INFO *src) {
569 if (!src) return FALSE;
570 *dst = HeapAlloc(GetProcessHeap(), 0, sizeof(HMAC_INFO));
571 if (!*dst) return FALSE;
572 **dst = *src;
573 (*dst)->pbInnerString = NULL;
574 (*dst)->pbOuterString = NULL;
575 if ((*dst)->cbInnerString == 0) (*dst)->cbInnerString = RSAENH_HMAC_DEF_PAD_LEN;
576 (*dst)->pbInnerString = HeapAlloc(GetProcessHeap(), 0, (*dst)->cbInnerString);
577 if (!(*dst)->pbInnerString) {
578 free_hmac_info(*dst);
579 return FALSE;
581 if (src->cbInnerString)
582 memcpy((*dst)->pbInnerString, src->pbInnerString, src->cbInnerString);
583 else
584 memset((*dst)->pbInnerString, RSAENH_HMAC_DEF_IPAD_CHAR, RSAENH_HMAC_DEF_PAD_LEN);
585 if ((*dst)->cbOuterString == 0) (*dst)->cbOuterString = RSAENH_HMAC_DEF_PAD_LEN;
586 (*dst)->pbOuterString = HeapAlloc(GetProcessHeap(), 0, (*dst)->cbOuterString);
587 if (!(*dst)->pbOuterString) {
588 free_hmac_info(*dst);
589 return FALSE;
591 if (src->cbOuterString)
592 memcpy((*dst)->pbOuterString, src->pbOuterString, src->cbOuterString);
593 else
594 memset((*dst)->pbOuterString, RSAENH_HMAC_DEF_OPAD_CHAR, RSAENH_HMAC_DEF_PAD_LEN);
595 return TRUE;
598 /******************************************************************************
599 * destroy_hash [Internal]
601 * Destructor for hash objects
603 * PARAMS
604 * pCryptHash [I] Pointer to the hash object to be destroyed.
605 * Will be invalid after function returns!
607 static void destroy_hash(OBJECTHDR *pObject)
609 CRYPTHASH *pCryptHash = (CRYPTHASH*)pObject;
611 free_hmac_info(pCryptHash->pHMACInfo);
612 free_data_blob(&pCryptHash->tpPRFParams.blobLabel);
613 free_data_blob(&pCryptHash->tpPRFParams.blobSeed);
614 HeapFree(GetProcessHeap(), 0, pCryptHash);
617 /******************************************************************************
618 * init_hash [Internal]
620 * Initialize (or reset) a hash object
622 * PARAMS
623 * pCryptHash [I] The hash object to be initialized.
625 static inline BOOL init_hash(CRYPTHASH *pCryptHash) {
626 DWORD dwLen;
628 switch (pCryptHash->aiAlgid)
630 case CALG_HMAC:
631 if (pCryptHash->pHMACInfo) {
632 const PROV_ENUMALGS_EX *pAlgInfo;
634 pAlgInfo = get_algid_info(pCryptHash->hProv, pCryptHash->pHMACInfo->HashAlgid);
635 if (!pAlgInfo) return FALSE;
636 pCryptHash->dwHashSize = pAlgInfo->dwDefaultLen >> 3;
637 init_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context);
638 update_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
639 pCryptHash->pHMACInfo->pbInnerString,
640 pCryptHash->pHMACInfo->cbInnerString);
642 return TRUE;
644 case CALG_MAC:
645 dwLen = sizeof(DWORD);
646 RSAENH_CPGetKeyParam(pCryptHash->hProv, pCryptHash->hKey, KP_BLOCKLEN,
647 (BYTE*)&pCryptHash->dwHashSize, &dwLen, 0);
648 pCryptHash->dwHashSize >>= 3;
649 return TRUE;
651 default:
652 return init_hash_impl(pCryptHash->aiAlgid, &pCryptHash->context);
656 /******************************************************************************
657 * update_hash [Internal]
659 * Hashes the given data and updates the hash object's state accordingly
661 * PARAMS
662 * pCryptHash [I] Hash object to be updated.
663 * pbData [I] Pointer to data stream to be hashed.
664 * dwDataLen [I] Length of data stream.
666 static inline void update_hash(CRYPTHASH *pCryptHash, const BYTE *pbData, DWORD dwDataLen)
668 BYTE *pbTemp;
670 switch (pCryptHash->aiAlgid)
672 case CALG_HMAC:
673 if (pCryptHash->pHMACInfo)
674 update_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
675 pbData, dwDataLen);
676 break;
678 case CALG_MAC:
679 pbTemp = HeapAlloc(GetProcessHeap(), 0, dwDataLen);
680 if (!pbTemp) return;
681 memcpy(pbTemp, pbData, dwDataLen);
682 RSAENH_CPEncrypt(pCryptHash->hProv, pCryptHash->hKey, 0, FALSE, 0,
683 pbTemp, &dwDataLen, dwDataLen);
684 HeapFree(GetProcessHeap(), 0, pbTemp);
685 break;
687 default:
688 update_hash_impl(pCryptHash->aiAlgid, &pCryptHash->context, pbData, dwDataLen);
692 /******************************************************************************
693 * finalize_hash [Internal]
695 * Finalizes the hash, after all data has been hashed with update_hash.
696 * No additional data can be hashed afterwards until the hash gets initialized again.
698 * PARAMS
699 * pCryptHash [I] Hash object to be finalized.
701 static inline void finalize_hash(CRYPTHASH *pCryptHash) {
702 DWORD dwDataLen;
704 switch (pCryptHash->aiAlgid)
706 case CALG_HMAC:
707 if (pCryptHash->pHMACInfo) {
708 BYTE abHashValue[RSAENH_MAX_HASH_SIZE];
710 finalize_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
711 pCryptHash->abHashValue);
712 memcpy(abHashValue, pCryptHash->abHashValue, pCryptHash->dwHashSize);
713 init_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context);
714 update_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
715 pCryptHash->pHMACInfo->pbOuterString,
716 pCryptHash->pHMACInfo->cbOuterString);
717 update_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
718 abHashValue, pCryptHash->dwHashSize);
719 finalize_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
720 pCryptHash->abHashValue);
722 break;
724 case CALG_MAC:
725 dwDataLen = 0;
726 RSAENH_CPEncrypt(pCryptHash->hProv, pCryptHash->hKey, 0, TRUE, 0,
727 pCryptHash->abHashValue, &dwDataLen, pCryptHash->dwHashSize);
728 break;
730 default:
731 finalize_hash_impl(pCryptHash->aiAlgid, &pCryptHash->context, pCryptHash->abHashValue);
735 /******************************************************************************
736 * destroy_key [Internal]
738 * Destructor for key objects
740 * PARAMS
741 * pCryptKey [I] Pointer to the key object to be destroyed.
742 * Will be invalid after function returns!
744 static void destroy_key(OBJECTHDR *pObject)
746 CRYPTKEY *pCryptKey = (CRYPTKEY*)pObject;
748 free_key_impl(pCryptKey->aiAlgid, &pCryptKey->context);
749 free_data_blob(&pCryptKey->siSChannelInfo.blobClientRandom);
750 free_data_blob(&pCryptKey->siSChannelInfo.blobServerRandom);
751 free_data_blob(&pCryptKey->blobHmacKey);
752 HeapFree(GetProcessHeap(), 0, pCryptKey);
755 /******************************************************************************
756 * setup_key [Internal]
758 * Initialize (or reset) a key object
760 * PARAMS
761 * pCryptKey [I] The key object to be initialized.
763 static inline void setup_key(CRYPTKEY *pCryptKey) {
764 pCryptKey->dwState = RSAENH_KEYSTATE_IDLE;
765 memcpy(pCryptKey->abChainVector, pCryptKey->abInitVector, sizeof(pCryptKey->abChainVector));
766 setup_key_impl(pCryptKey->aiAlgid, &pCryptKey->context, pCryptKey->dwKeyLen,
767 pCryptKey->dwEffectiveKeyLen, pCryptKey->dwSaltLen,
768 pCryptKey->abKeyValue);
771 /******************************************************************************
772 * new_key [Internal]
774 * Creates a new key object without assigning the actual binary key value.
775 * This is done by CPDeriveKey, CPGenKey or CPImportKey, which call this function.
777 * PARAMS
778 * hProv [I] Handle to the provider to which the created key will belong.
779 * aiAlgid [I] The new key shall use the crypto algorithm identified by aiAlgid.
780 * dwFlags [I] Upper 16 bits give the key length.
781 * Lower 16 bits: CRYPT_EXPORTABLE, CRYPT_CREATE_SALT,
782 * CRYPT_NO_SALT
783 * ppCryptKey [O] Pointer to the created key
785 * RETURNS
786 * Success: Handle to the created key.
787 * Failure: INVALID_HANDLE_VALUE
789 static HCRYPTKEY new_key(HCRYPTPROV hProv, ALG_ID aiAlgid, DWORD dwFlags, CRYPTKEY **ppCryptKey)
791 HCRYPTKEY hCryptKey;
792 CRYPTKEY *pCryptKey;
793 DWORD dwKeyLen = HIWORD(dwFlags), bKeyLen = dwKeyLen;
794 const PROV_ENUMALGS_EX *peaAlgidInfo;
796 *ppCryptKey = NULL;
799 * Retrieve the CSP's capabilities for the given ALG_ID value
801 peaAlgidInfo = get_algid_info(hProv, aiAlgid);
802 if (!peaAlgidInfo) return (HCRYPTKEY)INVALID_HANDLE_VALUE;
804 TRACE("alg = %s, dwKeyLen = %d\n", debugstr_a(peaAlgidInfo->szName),
805 dwKeyLen);
807 * Assume the default key length, if none is specified explicitly
809 if (dwKeyLen == 0) dwKeyLen = peaAlgidInfo->dwDefaultLen;
812 * Check if the requested key length is supported by the current CSP.
813 * Adjust key length's for DES algorithms.
815 switch (aiAlgid) {
816 case CALG_DES:
817 if (dwKeyLen == RSAENH_DES_EFFECTIVE_KEYLEN) {
818 dwKeyLen = RSAENH_DES_STORAGE_KEYLEN;
820 if (dwKeyLen != RSAENH_DES_STORAGE_KEYLEN) {
821 SetLastError(NTE_BAD_FLAGS);
822 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
824 break;
826 case CALG_3DES_112:
827 if (dwKeyLen == RSAENH_3DES112_EFFECTIVE_KEYLEN) {
828 dwKeyLen = RSAENH_3DES112_STORAGE_KEYLEN;
830 if (dwKeyLen != RSAENH_3DES112_STORAGE_KEYLEN) {
831 SetLastError(NTE_BAD_FLAGS);
832 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
834 break;
836 case CALG_3DES:
837 if (dwKeyLen == RSAENH_3DES_EFFECTIVE_KEYLEN) {
838 dwKeyLen = RSAENH_3DES_STORAGE_KEYLEN;
840 if (dwKeyLen != RSAENH_3DES_STORAGE_KEYLEN) {
841 SetLastError(NTE_BAD_FLAGS);
842 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
844 break;
846 case CALG_HMAC:
847 /* Avoid the key length check for HMAC keys, which have unlimited
848 * length.
850 break;
852 case CALG_AES:
853 if (!bKeyLen)
855 TRACE("missing key len for CALG_AES\n");
856 SetLastError(NTE_BAD_ALGID);
857 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
859 /* fall through */
860 default:
861 if (dwKeyLen % 8 ||
862 dwKeyLen > peaAlgidInfo->dwMaxLen ||
863 dwKeyLen < peaAlgidInfo->dwMinLen)
865 TRACE("key len %d out of bounds (%d, %d)\n", dwKeyLen,
866 peaAlgidInfo->dwMinLen, peaAlgidInfo->dwMaxLen);
867 SetLastError(NTE_BAD_DATA);
868 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
872 hCryptKey = new_object(&handle_table, sizeof(CRYPTKEY), RSAENH_MAGIC_KEY,
873 destroy_key, (OBJECTHDR**)&pCryptKey);
874 if (hCryptKey != (HCRYPTKEY)INVALID_HANDLE_VALUE)
876 KEYCONTAINER *pKeyContainer = get_key_container(hProv);
877 pCryptKey->aiAlgid = aiAlgid;
878 pCryptKey->hProv = hProv;
879 pCryptKey->dwModeBits = 0;
880 pCryptKey->dwPermissions = CRYPT_ENCRYPT | CRYPT_DECRYPT | CRYPT_READ | CRYPT_WRITE |
881 CRYPT_MAC;
882 if (dwFlags & CRYPT_EXPORTABLE)
883 pCryptKey->dwPermissions |= CRYPT_EXPORT;
884 pCryptKey->dwKeyLen = dwKeyLen >> 3;
885 pCryptKey->dwEffectiveKeyLen = 0;
888 * For compatibility reasons a 40 bit key on the Enhanced
889 * provider will not have salt
891 if (pKeyContainer->dwPersonality == RSAENH_PERSONALITY_ENHANCED
892 && (aiAlgid == CALG_RC2 || aiAlgid == CALG_RC4)
893 && (dwFlags & CRYPT_CREATE_SALT) && dwKeyLen == 40)
894 pCryptKey->dwSaltLen = 0;
895 else if ((dwFlags & CRYPT_CREATE_SALT) || (dwKeyLen == 40 && !(dwFlags & CRYPT_NO_SALT)))
896 pCryptKey->dwSaltLen = 16 /*FIXME*/ - pCryptKey->dwKeyLen;
897 else
898 pCryptKey->dwSaltLen = 0;
899 memset(pCryptKey->abKeyValue, 0, sizeof(pCryptKey->abKeyValue));
900 memset(pCryptKey->abInitVector, 0, sizeof(pCryptKey->abInitVector));
901 memset(&pCryptKey->siSChannelInfo.saEncAlg, 0, sizeof(pCryptKey->siSChannelInfo.saEncAlg));
902 memset(&pCryptKey->siSChannelInfo.saMACAlg, 0, sizeof(pCryptKey->siSChannelInfo.saMACAlg));
903 init_data_blob(&pCryptKey->siSChannelInfo.blobClientRandom);
904 init_data_blob(&pCryptKey->siSChannelInfo.blobServerRandom);
905 init_data_blob(&pCryptKey->blobHmacKey);
907 switch(aiAlgid)
909 case CALG_PCT1_MASTER:
910 case CALG_SSL2_MASTER:
911 case CALG_SSL3_MASTER:
912 case CALG_TLS1_MASTER:
913 case CALG_RC4:
914 pCryptKey->dwBlockLen = 0;
915 pCryptKey->dwMode = 0;
916 break;
918 case CALG_RC2:
919 case CALG_DES:
920 case CALG_3DES_112:
921 case CALG_3DES:
922 pCryptKey->dwBlockLen = 8;
923 pCryptKey->dwMode = CRYPT_MODE_CBC;
924 break;
926 case CALG_AES:
927 case CALG_AES_128:
928 case CALG_AES_192:
929 case CALG_AES_256:
930 pCryptKey->dwBlockLen = 16;
931 pCryptKey->dwMode = CRYPT_MODE_CBC;
932 break;
934 case CALG_RSA_KEYX:
935 case CALG_RSA_SIGN:
936 pCryptKey->dwBlockLen = dwKeyLen >> 3;
937 pCryptKey->dwMode = 0;
938 break;
940 case CALG_HMAC:
941 pCryptKey->dwBlockLen = 0;
942 pCryptKey->dwMode = 0;
943 break;
946 *ppCryptKey = pCryptKey;
949 return hCryptKey;
952 /******************************************************************************
953 * map_key_spec_to_key_pair_name [Internal]
955 * Returns the name of the registry value associated with a key spec.
957 * PARAMS
958 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
960 * RETURNS
961 * Success: Name of registry value.
962 * Failure: NULL
964 static LPCSTR map_key_spec_to_key_pair_name(DWORD dwKeySpec)
966 LPCSTR szValueName;
968 switch (dwKeySpec)
970 case AT_KEYEXCHANGE:
971 szValueName = "KeyExchangeKeyPair";
972 break;
973 case AT_SIGNATURE:
974 szValueName = "SignatureKeyPair";
975 break;
976 default:
977 WARN("invalid key spec %d\n", dwKeySpec);
978 szValueName = NULL;
980 return szValueName;
983 /******************************************************************************
984 * store_key_pair [Internal]
986 * Stores a key pair to the registry
988 * PARAMS
989 * hCryptKey [I] Handle to the key to be stored
990 * hKey [I] Registry key where the key pair is to be stored
991 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
992 * dwFlags [I] Flags for protecting the key
994 static void store_key_pair(HCRYPTKEY hCryptKey, HKEY hKey, DWORD dwKeySpec, DWORD dwFlags)
996 LPCSTR szValueName;
997 DATA_BLOB blobIn, blobOut;
998 CRYPTKEY *pKey;
999 DWORD dwLen;
1000 BYTE *pbKey;
1002 if (!(szValueName = map_key_spec_to_key_pair_name(dwKeySpec)))
1003 return;
1004 if (lookup_handle(&handle_table, hCryptKey, RSAENH_MAGIC_KEY,
1005 (OBJECTHDR**)&pKey))
1007 if (crypt_export_key(pKey, 0, PRIVATEKEYBLOB, 0, TRUE, 0, &dwLen))
1009 pbKey = HeapAlloc(GetProcessHeap(), 0, dwLen);
1010 if (pbKey)
1012 if (crypt_export_key(pKey, 0, PRIVATEKEYBLOB, 0, TRUE, pbKey,
1013 &dwLen))
1015 blobIn.pbData = pbKey;
1016 blobIn.cbData = dwLen;
1018 if (CryptProtectData(&blobIn, NULL, NULL, NULL, NULL,
1019 dwFlags, &blobOut))
1021 RegSetValueExA(hKey, szValueName, 0, REG_BINARY,
1022 blobOut.pbData, blobOut.cbData);
1023 LocalFree(blobOut.pbData);
1026 HeapFree(GetProcessHeap(), 0, pbKey);
1032 /******************************************************************************
1033 * map_key_spec_to_permissions_name [Internal]
1035 * Returns the name of the registry value associated with the permissions for
1036 * a key spec.
1038 * PARAMS
1039 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
1041 * RETURNS
1042 * Success: Name of registry value.
1043 * Failure: NULL
1045 static LPCSTR map_key_spec_to_permissions_name(DWORD dwKeySpec)
1047 LPCSTR szValueName;
1049 switch (dwKeySpec)
1051 case AT_KEYEXCHANGE:
1052 szValueName = "KeyExchangePermissions";
1053 break;
1054 case AT_SIGNATURE:
1055 szValueName = "SignaturePermissions";
1056 break;
1057 default:
1058 WARN("invalid key spec %d\n", dwKeySpec);
1059 szValueName = NULL;
1061 return szValueName;
1064 /******************************************************************************
1065 * store_key_permissions [Internal]
1067 * Stores a key's permissions to the registry
1069 * PARAMS
1070 * hCryptKey [I] Handle to the key whose permissions are to be stored
1071 * hKey [I] Registry key where the key permissions are to be stored
1072 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
1074 static void store_key_permissions(HCRYPTKEY hCryptKey, HKEY hKey, DWORD dwKeySpec)
1076 LPCSTR szValueName;
1077 CRYPTKEY *pKey;
1079 if (!(szValueName = map_key_spec_to_permissions_name(dwKeySpec)))
1080 return;
1081 if (lookup_handle(&handle_table, hCryptKey, RSAENH_MAGIC_KEY,
1082 (OBJECTHDR**)&pKey))
1083 RegSetValueExA(hKey, szValueName, 0, REG_DWORD,
1084 (BYTE *)&pKey->dwPermissions,
1085 sizeof(pKey->dwPermissions));
1088 /******************************************************************************
1089 * create_container_key [Internal]
1091 * Creates the registry key for a key container's persistent storage.
1093 * PARAMS
1094 * pKeyContainer [I] Pointer to the key container
1095 * sam [I] Desired registry access
1096 * phKey [O] Returned key
1098 static BOOL create_container_key(KEYCONTAINER *pKeyContainer, REGSAM sam, HKEY *phKey)
1100 CHAR szRSABase[MAX_PATH];
1101 HKEY hRootKey;
1103 sprintf(szRSABase, RSAENH_REGKEY, pKeyContainer->szName);
1105 if (pKeyContainer->dwFlags & CRYPT_MACHINE_KEYSET)
1106 hRootKey = HKEY_LOCAL_MACHINE;
1107 else
1108 hRootKey = HKEY_CURRENT_USER;
1110 /* @@ Wine registry key: HKLM\Software\Wine\Crypto\RSA */
1111 /* @@ Wine registry key: HKCU\Software\Wine\Crypto\RSA */
1112 return RegCreateKeyExA(hRootKey, szRSABase, 0, NULL,
1113 REG_OPTION_NON_VOLATILE, sam, NULL, phKey, NULL)
1114 == ERROR_SUCCESS;
1117 /******************************************************************************
1118 * open_container_key [Internal]
1120 * Opens a key container's persistent storage for reading.
1122 * PARAMS
1123 * pszContainerName [I] Name of the container to be opened. May be the empty
1124 * string if the parent key of all containers is to be
1125 * opened.
1126 * dwFlags [I] Flags indicating which keyset to be opened.
1127 * phKey [O] Returned key
1129 static BOOL open_container_key(LPCSTR pszContainerName, DWORD dwFlags, REGSAM access, HKEY *phKey)
1131 CHAR szRSABase[MAX_PATH];
1132 HKEY hRootKey;
1134 sprintf(szRSABase, RSAENH_REGKEY, pszContainerName);
1136 if (dwFlags & CRYPT_MACHINE_KEYSET)
1137 hRootKey = HKEY_LOCAL_MACHINE;
1138 else
1139 hRootKey = HKEY_CURRENT_USER;
1141 /* @@ Wine registry key: HKLM\Software\Wine\Crypto\RSA */
1142 /* @@ Wine registry key: HKCU\Software\Wine\Crypto\RSA */
1143 return RegOpenKeyExA(hRootKey, szRSABase, 0, access, phKey) ==
1144 ERROR_SUCCESS;
1147 /******************************************************************************
1148 * delete_container_key [Internal]
1150 * Deletes a key container's persistent storage.
1152 * PARAMS
1153 * pszContainerName [I] Name of the container to be opened.
1154 * dwFlags [I] Flags indicating which keyset to be opened.
1156 static BOOL delete_container_key(LPCSTR pszContainerName, DWORD dwFlags)
1158 CHAR szRegKey[MAX_PATH];
1160 if (snprintf(szRegKey, MAX_PATH, RSAENH_REGKEY, pszContainerName) >= MAX_PATH) {
1161 SetLastError(NTE_BAD_KEYSET_PARAM);
1162 return FALSE;
1163 } else {
1164 HKEY hRootKey;
1165 if (dwFlags & CRYPT_MACHINE_KEYSET)
1166 hRootKey = HKEY_LOCAL_MACHINE;
1167 else
1168 hRootKey = HKEY_CURRENT_USER;
1169 if (!RegDeleteKeyA(hRootKey, szRegKey)) {
1170 SetLastError(ERROR_SUCCESS);
1171 return TRUE;
1172 } else {
1173 SetLastError(NTE_BAD_KEYSET);
1174 return FALSE;
1179 /******************************************************************************
1180 * store_key_container_keys [Internal]
1182 * Stores key container's keys in a persistent location.
1184 * PARAMS
1185 * pKeyContainer [I] Pointer to the key container whose keys are to be saved
1187 static void store_key_container_keys(KEYCONTAINER *pKeyContainer)
1189 HKEY hKey;
1190 DWORD dwFlags;
1192 /* On WinXP, persistent keys are stored in a file located at:
1193 * $AppData$\\Microsoft\\Crypto\\RSA\\$SID$\\some_hex_string
1196 if (pKeyContainer->dwFlags & CRYPT_MACHINE_KEYSET)
1197 dwFlags = CRYPTPROTECT_LOCAL_MACHINE;
1198 else
1199 dwFlags = 0;
1201 if (create_container_key(pKeyContainer, KEY_WRITE, &hKey))
1203 store_key_pair(pKeyContainer->hKeyExchangeKeyPair, hKey,
1204 AT_KEYEXCHANGE, dwFlags);
1205 store_key_pair(pKeyContainer->hSignatureKeyPair, hKey,
1206 AT_SIGNATURE, dwFlags);
1207 RegCloseKey(hKey);
1211 /******************************************************************************
1212 * store_key_container_permissions [Internal]
1214 * Stores key container's key permissions in a persistent location.
1216 * PARAMS
1217 * pKeyContainer [I] Pointer to the key container whose key permissions are to
1218 * be saved
1220 static void store_key_container_permissions(KEYCONTAINER *pKeyContainer)
1222 HKEY hKey;
1224 if (create_container_key(pKeyContainer, KEY_WRITE, &hKey))
1226 store_key_permissions(pKeyContainer->hKeyExchangeKeyPair, hKey,
1227 AT_KEYEXCHANGE);
1228 store_key_permissions(pKeyContainer->hSignatureKeyPair, hKey,
1229 AT_SIGNATURE);
1230 RegCloseKey(hKey);
1234 /******************************************************************************
1235 * release_key_container_keys [Internal]
1237 * Releases key container's keys.
1239 * PARAMS
1240 * pKeyContainer [I] Pointer to the key container whose keys are to be released.
1242 static void release_key_container_keys(KEYCONTAINER *pKeyContainer)
1244 release_handle(&handle_table, pKeyContainer->hKeyExchangeKeyPair,
1245 RSAENH_MAGIC_KEY);
1246 release_handle(&handle_table, pKeyContainer->hSignatureKeyPair,
1247 RSAENH_MAGIC_KEY);
1250 /******************************************************************************
1251 * destroy_key_container [Internal]
1253 * Destructor for key containers.
1255 * PARAMS
1256 * pObjectHdr [I] Pointer to the key container to be destroyed.
1258 static void destroy_key_container(OBJECTHDR *pObjectHdr)
1260 KEYCONTAINER *pKeyContainer = (KEYCONTAINER*)pObjectHdr;
1262 if (!(pKeyContainer->dwFlags & CRYPT_VERIFYCONTEXT))
1264 store_key_container_keys(pKeyContainer);
1265 store_key_container_permissions(pKeyContainer);
1266 release_key_container_keys(pKeyContainer);
1268 else
1269 release_key_container_keys(pKeyContainer);
1270 HeapFree( GetProcessHeap(), 0, pKeyContainer );
1273 /******************************************************************************
1274 * new_key_container [Internal]
1276 * Create a new key container. The personality (RSA Base, Strong or Enhanced CP)
1277 * of the CSP is determined via the pVTable->pszProvName string.
1279 * PARAMS
1280 * pszContainerName [I] Name of the key container.
1281 * pVTable [I] Callback functions and context info provided by the OS
1283 * RETURNS
1284 * Success: Handle to the new key container.
1285 * Failure: INVALID_HANDLE_VALUE
1287 static HCRYPTPROV new_key_container(PCCH pszContainerName, DWORD dwFlags, const VTableProvStruc *pVTable)
1289 KEYCONTAINER *pKeyContainer;
1290 HCRYPTPROV hKeyContainer;
1292 hKeyContainer = new_object(&handle_table, sizeof(KEYCONTAINER), RSAENH_MAGIC_CONTAINER,
1293 destroy_key_container, (OBJECTHDR**)&pKeyContainer);
1294 if (hKeyContainer != (HCRYPTPROV)INVALID_HANDLE_VALUE)
1296 lstrcpynA(pKeyContainer->szName, pszContainerName, MAX_PATH);
1297 pKeyContainer->dwFlags = dwFlags;
1298 pKeyContainer->dwEnumAlgsCtr = 0;
1299 pKeyContainer->hKeyExchangeKeyPair = (HCRYPTKEY)INVALID_HANDLE_VALUE;
1300 pKeyContainer->hSignatureKeyPair = (HCRYPTKEY)INVALID_HANDLE_VALUE;
1301 if (pVTable && pVTable->pszProvName) {
1302 lstrcpynA(pKeyContainer->szProvName, pVTable->pszProvName, MAX_PATH);
1303 if (!strcmp(pVTable->pszProvName, MS_DEF_PROV_A)) {
1304 pKeyContainer->dwPersonality = RSAENH_PERSONALITY_BASE;
1305 } else if (!strcmp(pVTable->pszProvName, MS_ENHANCED_PROV_A)) {
1306 pKeyContainer->dwPersonality = RSAENH_PERSONALITY_ENHANCED;
1307 } else if (!strcmp(pVTable->pszProvName, MS_DEF_RSA_SCHANNEL_PROV_A)) {
1308 pKeyContainer->dwPersonality = RSAENH_PERSONALITY_SCHANNEL;
1309 } else if (!strcmp(pVTable->pszProvName, MS_ENH_RSA_AES_PROV_A) ||
1310 !strcmp(pVTable->pszProvName, MS_ENH_RSA_AES_PROV_XP_A)) {
1311 pKeyContainer->dwPersonality = RSAENH_PERSONALITY_AES;
1312 } else {
1313 pKeyContainer->dwPersonality = RSAENH_PERSONALITY_STRONG;
1317 /* The new key container has to be inserted into the CSP immediately
1318 * after creation to be available for CPGetProvParam's PP_ENUMCONTAINERS. */
1319 if (!(dwFlags & CRYPT_VERIFYCONTEXT)) {
1320 HKEY hKey;
1322 if (create_container_key(pKeyContainer, KEY_WRITE, &hKey))
1323 RegCloseKey(hKey);
1327 return hKeyContainer;
1330 /******************************************************************************
1331 * read_key_value [Internal]
1333 * Reads a key pair value from the registry
1335 * PARAMS
1336 * hKeyContainer [I] Crypt provider to use to import the key
1337 * hKey [I] Registry key from which to read the key pair
1338 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
1339 * dwFlags [I] Flags for unprotecting the key
1340 * phCryptKey [O] Returned key
1342 static BOOL read_key_value(HCRYPTPROV hKeyContainer, HKEY hKey, DWORD dwKeySpec, DWORD dwFlags, HCRYPTKEY *phCryptKey)
1344 LPCSTR szValueName;
1345 DWORD dwValueType, dwLen;
1346 BYTE *pbKey;
1347 DATA_BLOB blobIn, blobOut;
1348 BOOL ret = FALSE;
1350 if (!(szValueName = map_key_spec_to_key_pair_name(dwKeySpec)))
1351 return FALSE;
1352 if (RegQueryValueExA(hKey, szValueName, 0, &dwValueType, NULL, &dwLen) ==
1353 ERROR_SUCCESS)
1355 pbKey = HeapAlloc(GetProcessHeap(), 0, dwLen);
1356 if (pbKey)
1358 if (RegQueryValueExA(hKey, szValueName, 0, &dwValueType, pbKey, &dwLen) ==
1359 ERROR_SUCCESS)
1361 blobIn.pbData = pbKey;
1362 blobIn.cbData = dwLen;
1364 if (CryptUnprotectData(&blobIn, NULL, NULL, NULL, NULL,
1365 dwFlags, &blobOut))
1367 ret = import_key(hKeyContainer, blobOut.pbData, blobOut.cbData, 0, 0,
1368 FALSE, phCryptKey);
1369 LocalFree(blobOut.pbData);
1372 HeapFree(GetProcessHeap(), 0, pbKey);
1375 if (ret)
1377 CRYPTKEY *pKey;
1379 if (lookup_handle(&handle_table, *phCryptKey, RSAENH_MAGIC_KEY,
1380 (OBJECTHDR**)&pKey))
1382 if ((szValueName = map_key_spec_to_permissions_name(dwKeySpec)))
1384 dwLen = sizeof(pKey->dwPermissions);
1385 RegQueryValueExA(hKey, szValueName, 0, NULL,
1386 (BYTE *)&pKey->dwPermissions, &dwLen);
1390 return ret;
1393 /******************************************************************************
1394 * read_key_container [Internal]
1396 * Tries to read the persistent state of the key container (mainly the signature
1397 * and key exchange private keys) given by pszContainerName.
1399 * PARAMS
1400 * pszContainerName [I] Name of the key container to read from the registry
1401 * pVTable [I] Pointer to context data provided by the operating system
1403 * RETURNS
1404 * Success: Handle to the key container read from the registry
1405 * Failure: INVALID_HANDLE_VALUE
1407 static HCRYPTPROV read_key_container(PCHAR pszContainerName, DWORD dwFlags, const VTableProvStruc *pVTable)
1409 HKEY hKey;
1410 KEYCONTAINER *pKeyContainer;
1411 HCRYPTPROV hKeyContainer;
1412 HCRYPTKEY hCryptKey;
1414 if (!open_container_key(pszContainerName, dwFlags, KEY_READ, &hKey))
1416 SetLastError(NTE_BAD_KEYSET);
1417 return (HCRYPTPROV)INVALID_HANDLE_VALUE;
1420 hKeyContainer = new_key_container(pszContainerName, dwFlags, pVTable);
1421 if (hKeyContainer != (HCRYPTPROV)INVALID_HANDLE_VALUE)
1423 DWORD dwProtectFlags = (dwFlags & CRYPT_MACHINE_KEYSET) ?
1424 CRYPTPROTECT_LOCAL_MACHINE : 0;
1426 if (!lookup_handle(&handle_table, hKeyContainer, RSAENH_MAGIC_CONTAINER,
1427 (OBJECTHDR**)&pKeyContainer))
1428 return (HCRYPTPROV)INVALID_HANDLE_VALUE;
1430 /* read_key_value calls import_key, which calls import_private_key,
1431 * which implicitly installs the key value into the appropriate key
1432 * container key. Thus the ref count is incremented twice, once for
1433 * the output key value, and once for the implicit install, and needs
1434 * to be decremented to balance the two.
1436 if (read_key_value(hKeyContainer, hKey, AT_KEYEXCHANGE,
1437 dwProtectFlags, &hCryptKey))
1438 release_handle(&handle_table, hCryptKey, RSAENH_MAGIC_KEY);
1439 if (read_key_value(hKeyContainer, hKey, AT_SIGNATURE,
1440 dwProtectFlags, &hCryptKey))
1441 release_handle(&handle_table, hCryptKey, RSAENH_MAGIC_KEY);
1444 return hKeyContainer;
1447 /******************************************************************************
1448 * build_hash_signature [Internal]
1450 * Builds a padded version of a hash to match the length of the RSA key modulus.
1452 * PARAMS
1453 * pbSignature [O] The padded hash object is stored here.
1454 * dwLen [I] Length of the pbSignature buffer.
1455 * aiAlgid [I] Algorithm identifier of the hash to be padded.
1456 * abHashValue [I] The value of the hash object.
1457 * dwHashLen [I] Length of the hash value.
1458 * dwFlags [I] Selection of padding algorithm.
1460 * RETURNS
1461 * Success: TRUE
1462 * Failure: FALSE (NTE_BAD_ALGID)
1464 static BOOL build_hash_signature(BYTE *pbSignature, DWORD dwLen, ALG_ID aiAlgid,
1465 const BYTE *abHashValue, DWORD dwHashLen, DWORD dwFlags)
1467 /* These prefixes are meant to be concatenated with hash values of the
1468 * respective kind to form a PKCS #7 DigestInfo. */
1469 static const struct tagOIDDescriptor {
1470 ALG_ID aiAlgid;
1471 DWORD dwLen;
1472 const BYTE abOID[19];
1473 } aOIDDescriptor[] = {
1474 { CALG_MD2, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1475 0x86, 0xf7, 0x0d, 0x02, 0x02, 0x05, 0x00, 0x04, 0x10 } },
1476 { CALG_MD4, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1477 0x86, 0xf7, 0x0d, 0x02, 0x04, 0x05, 0x00, 0x04, 0x10 } },
1478 { CALG_MD5, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1479 0x86, 0xf7, 0x0d, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10 } },
1480 { CALG_SHA, 15, { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03,
1481 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14 } },
1482 { CALG_SHA_256, 19, { 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
1483 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01,
1484 0x05, 0x00, 0x04, 0x20 } },
1485 { CALG_SHA_384, 19, { 0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
1486 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02,
1487 0x05, 0x00, 0x04, 0x30 } },
1488 { CALG_SHA_512, 19, { 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
1489 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03,
1490 0x05, 0x00, 0x04, 0x40 } },
1491 { CALG_SSL3_SHAMD5, 0, { 0 } },
1492 { 0, 0, { 0 } }
1494 DWORD dwIdxOID, i, j;
1496 for (dwIdxOID = 0; aOIDDescriptor[dwIdxOID].aiAlgid; dwIdxOID++) {
1497 if (aOIDDescriptor[dwIdxOID].aiAlgid == aiAlgid) break;
1500 if (!aOIDDescriptor[dwIdxOID].aiAlgid) {
1501 SetLastError(NTE_BAD_ALGID);
1502 return FALSE;
1505 /* Build the padded signature */
1506 if (dwFlags & CRYPT_X931_FORMAT) {
1507 pbSignature[0] = 0x6b;
1508 for (i=1; i < dwLen - dwHashLen - 3; i++) {
1509 pbSignature[i] = 0xbb;
1511 pbSignature[i++] = 0xba;
1512 for (j=0; j < dwHashLen; j++, i++) {
1513 pbSignature[i] = abHashValue[j];
1515 pbSignature[i++] = 0x33;
1516 pbSignature[i++] = 0xcc;
1517 } else {
1518 pbSignature[0] = 0x00;
1519 pbSignature[1] = 0x01;
1520 if (dwFlags & CRYPT_NOHASHOID) {
1521 for (i=2; i < dwLen - 1 - dwHashLen; i++) {
1522 pbSignature[i] = 0xff;
1524 pbSignature[i++] = 0x00;
1525 } else {
1526 for (i=2; i < dwLen - 1 - aOIDDescriptor[dwIdxOID].dwLen - dwHashLen; i++) {
1527 pbSignature[i] = 0xff;
1529 pbSignature[i++] = 0x00;
1530 for (j=0; j < aOIDDescriptor[dwIdxOID].dwLen; j++) {
1531 pbSignature[i++] = aOIDDescriptor[dwIdxOID].abOID[j];
1534 for (j=0; j < dwHashLen; j++) {
1535 pbSignature[i++] = abHashValue[j];
1539 return TRUE;
1542 /******************************************************************************
1543 * tls1_p [Internal]
1545 * This is an implementation of the 'P_hash' helper function for TLS1's PRF.
1546 * It is used exclusively by tls1_prf. For details see RFC 2246, chapter 5.
1547 * The pseudo random stream generated by this function is exclusive or'ed with
1548 * the data in pbBuffer.
1550 * PARAMS
1551 * hHMAC [I] HMAC object, which will be used in pseudo random generation
1552 * pblobSeed [I] Seed value
1553 * pbBuffer [I/O] Pseudo random stream will be xor'ed to the provided data
1554 * dwBufferLen [I] Number of pseudo random bytes desired
1556 * RETURNS
1557 * Success: TRUE
1558 * Failure: FALSE
1560 static BOOL tls1_p(HCRYPTHASH hHMAC, const PCRYPT_DATA_BLOB pblobSeed, BYTE *pbBuffer,
1561 DWORD dwBufferLen)
1563 CRYPTHASH *pHMAC;
1564 BYTE abAi[RSAENH_MAX_HASH_SIZE];
1565 DWORD i = 0;
1567 if (!lookup_handle(&handle_table, hHMAC, RSAENH_MAGIC_HASH, (OBJECTHDR**)&pHMAC)) {
1568 SetLastError(NTE_BAD_HASH);
1569 return FALSE;
1572 /* compute A_1 = HMAC(seed) */
1573 init_hash(pHMAC);
1574 update_hash(pHMAC, pblobSeed->pbData, pblobSeed->cbData);
1575 finalize_hash(pHMAC);
1576 memcpy(abAi, pHMAC->abHashValue, pHMAC->dwHashSize);
1578 do {
1579 /* compute HMAC(A_i + seed) */
1580 init_hash(pHMAC);
1581 update_hash(pHMAC, abAi, pHMAC->dwHashSize);
1582 update_hash(pHMAC, pblobSeed->pbData, pblobSeed->cbData);
1583 finalize_hash(pHMAC);
1585 /* pseudo random stream := CONCAT_{i=1..n} ( HMAC(A_i + seed) ) */
1586 do {
1587 if (i >= dwBufferLen) break;
1588 pbBuffer[i] ^= pHMAC->abHashValue[i % pHMAC->dwHashSize];
1589 i++;
1590 } while (i % pHMAC->dwHashSize);
1592 /* compute A_{i+1} = HMAC(A_i) */
1593 init_hash(pHMAC);
1594 update_hash(pHMAC, abAi, pHMAC->dwHashSize);
1595 finalize_hash(pHMAC);
1596 memcpy(abAi, pHMAC->abHashValue, pHMAC->dwHashSize);
1597 } while (i < dwBufferLen);
1599 return TRUE;
1602 /******************************************************************************
1603 * tls1_prf [Internal]
1605 * TLS1 pseudo random function as specified in RFC 2246, chapter 5
1607 * PARAMS
1608 * hProv [I] Key container used to compute the pseudo random stream
1609 * hSecret [I] Key that holds the (pre-)master secret
1610 * pblobLabel [I] Descriptive label
1611 * pblobSeed [I] Seed value
1612 * pbBuffer [O] Pseudo random numbers will be stored here
1613 * dwBufferLen [I] Number of pseudo random bytes desired
1615 * RETURNS
1616 * Success: TRUE
1617 * Failure: FALSE
1619 static BOOL tls1_prf(HCRYPTPROV hProv, HCRYPTPROV hSecret, const PCRYPT_DATA_BLOB pblobLabel,
1620 const PCRYPT_DATA_BLOB pblobSeed, BYTE *pbBuffer, DWORD dwBufferLen)
1622 HMAC_INFO hmacInfo = { 0, NULL, 0, NULL, 0 };
1623 HCRYPTHASH hHMAC = (HCRYPTHASH)INVALID_HANDLE_VALUE;
1624 HCRYPTKEY hHalfSecret = (HCRYPTKEY)INVALID_HANDLE_VALUE;
1625 CRYPTKEY *pHalfSecret, *pSecret;
1626 DWORD dwHalfSecretLen;
1627 BOOL result = FALSE;
1628 CRYPT_DATA_BLOB blobLabelSeed;
1630 TRACE("(hProv=%08lx, hSecret=%08lx, pblobLabel=%p, pblobSeed=%p, pbBuffer=%p, dwBufferLen=%d)\n",
1631 hProv, hSecret, pblobLabel, pblobSeed, pbBuffer, dwBufferLen);
1633 if (!lookup_handle(&handle_table, hSecret, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pSecret)) {
1634 SetLastError(NTE_FAIL);
1635 return FALSE;
1638 dwHalfSecretLen = (pSecret->dwKeyLen+1)/2;
1640 /* concatenation of the label and the seed */
1641 if (!concat_data_blobs(&blobLabelSeed, pblobLabel, pblobSeed)) goto exit;
1643 /* zero out the buffer, since two random streams will be xor'ed into it. */
1644 memset(pbBuffer, 0, dwBufferLen);
1646 /* build a 'fake' key, to hold the secret. CALG_SSL2_MASTER is used since it provides
1647 * the biggest range of valid key lengths. */
1648 hHalfSecret = new_key(hProv, CALG_SSL2_MASTER, MAKELONG(0,dwHalfSecretLen*8), &pHalfSecret);
1649 if (hHalfSecret == (HCRYPTKEY)INVALID_HANDLE_VALUE) goto exit;
1651 /* Derive an HMAC_MD5 hash and call the helper function. */
1652 memcpy(pHalfSecret->abKeyValue, pSecret->abKeyValue, dwHalfSecretLen);
1653 if (!RSAENH_CPCreateHash(hProv, CALG_HMAC, hHalfSecret, 0, &hHMAC)) goto exit;
1654 hmacInfo.HashAlgid = CALG_MD5;
1655 if (!RSAENH_CPSetHashParam(hProv, hHMAC, HP_HMAC_INFO, (BYTE*)&hmacInfo, 0)) goto exit;
1656 if (!tls1_p(hHMAC, &blobLabelSeed, pbBuffer, dwBufferLen)) goto exit;
1658 /* Reconfigure to HMAC_SHA hash and call helper function again. */
1659 memcpy(pHalfSecret->abKeyValue, pSecret->abKeyValue + (pSecret->dwKeyLen/2), dwHalfSecretLen);
1660 hmacInfo.HashAlgid = CALG_SHA;
1661 if (!RSAENH_CPSetHashParam(hProv, hHMAC, HP_HMAC_INFO, (BYTE*)&hmacInfo, 0)) goto exit;
1662 if (!tls1_p(hHMAC, &blobLabelSeed, pbBuffer, dwBufferLen)) goto exit;
1664 result = TRUE;
1665 exit:
1666 release_handle(&handle_table, hHalfSecret, RSAENH_MAGIC_KEY);
1667 if (hHMAC != (HCRYPTHASH)INVALID_HANDLE_VALUE) RSAENH_CPDestroyHash(hProv, hHMAC);
1668 free_data_blob(&blobLabelSeed);
1669 return result;
1672 /******************************************************************************
1673 * pad_data [Internal]
1675 * Helper function for data padding according to PKCS1 #2
1677 * PARAMS
1678 * abData [I] The data to be padded
1679 * dwDataLen [I] Length of the data
1680 * abBuffer [O] Padded data will be stored here
1681 * dwBufferLen [I] Length of the buffer (also length of padded data)
1682 * dwFlags [I] Padding format (CRYPT_SSL2_FALLBACK)
1684 * RETURN
1685 * Success: TRUE
1686 * Failure: FALSE (NTE_BAD_LEN, too much data to pad)
1688 static BOOL pad_data(const BYTE *abData, DWORD dwDataLen, BYTE *abBuffer, DWORD dwBufferLen,
1689 DWORD dwFlags)
1691 DWORD i;
1693 /* Ensure there is enough space for PKCS1 #2 padding */
1694 if (dwDataLen > dwBufferLen-11) {
1695 SetLastError(NTE_BAD_LEN);
1696 return FALSE;
1699 memmove(abBuffer + dwBufferLen - dwDataLen, abData, dwDataLen);
1701 abBuffer[0] = 0x00;
1702 abBuffer[1] = RSAENH_PKC_BLOCKTYPE;
1703 for (i=2; i < dwBufferLen - dwDataLen - 1; i++)
1704 do gen_rand_impl(&abBuffer[i], 1); while (!abBuffer[i]);
1705 if (dwFlags & CRYPT_SSL2_FALLBACK)
1706 for (i-=8; i < dwBufferLen - dwDataLen - 1; i++)
1707 abBuffer[i] = 0x03;
1708 abBuffer[i] = 0x00;
1710 return TRUE;
1713 /******************************************************************************
1714 * unpad_data [Internal]
1716 * Remove the PKCS1 padding from RSA decrypted data
1718 * PARAMS
1719 * abData [I] The padded data
1720 * dwDataLen [I] Length of the padded data
1721 * abBuffer [O] Data without padding will be stored here
1722 * dwBufferLen [I/O] I: Length of the buffer, O: Length of unpadded data
1723 * dwFlags [I] Currently none defined
1725 * RETURNS
1726 * Success: TRUE
1727 * Failure: FALSE, (NTE_BAD_DATA, no valid PKCS1 padding or buffer too small)
1729 static BOOL unpad_data(const BYTE *abData, DWORD dwDataLen, BYTE *abBuffer, DWORD *dwBufferLen,
1730 DWORD dwFlags)
1732 DWORD i;
1734 if (dwDataLen < 3)
1736 SetLastError(NTE_BAD_DATA);
1737 return FALSE;
1739 for (i=2; i<dwDataLen; i++)
1740 if (!abData[i])
1741 break;
1743 if ((i == dwDataLen) || (*dwBufferLen < dwDataLen - i - 1) ||
1744 (abData[0] != 0x00) || (abData[1] != RSAENH_PKC_BLOCKTYPE))
1746 SetLastError(NTE_BAD_DATA);
1747 return FALSE;
1750 *dwBufferLen = dwDataLen - i - 1;
1751 memmove(abBuffer, abData + i + 1, *dwBufferLen);
1752 return TRUE;
1755 /******************************************************************************
1756 * CPAcquireContext (RSAENH.@)
1758 * Acquire a handle to the key container specified by pszContainer
1760 * PARAMS
1761 * phProv [O] Pointer to the location the acquired handle will be written to.
1762 * pszContainer [I] Name of the desired key container. See Notes
1763 * dwFlags [I] Flags. See Notes.
1764 * pVTable [I] Pointer to a PVTableProvStruct containing callbacks.
1766 * RETURNS
1767 * Success: TRUE
1768 * Failure: FALSE
1770 * NOTES
1771 * If pszContainer is NULL or points to a zero length string the user's login
1772 * name will be used as the key container name.
1774 * If the CRYPT_NEW_KEYSET flag is set in dwFlags a new keyset will be created.
1775 * If a keyset with the given name already exists, the function fails and sets
1776 * last error to NTE_EXISTS. If CRYPT_NEW_KEYSET is not set and the specified
1777 * key container does not exist, function fails and sets last error to
1778 * NTE_BAD_KEYSET.
1780 BOOL WINAPI RSAENH_CPAcquireContext(HCRYPTPROV *phProv, LPSTR pszContainer,
1781 DWORD dwFlags, PVTableProvStruc pVTable)
1783 CHAR szKeyContainerName[MAX_PATH];
1785 TRACE("(phProv=%p, pszContainer=%s, dwFlags=%08x, pVTable=%p)\n", phProv,
1786 debugstr_a(pszContainer), dwFlags, pVTable);
1788 if (pszContainer && *pszContainer)
1790 lstrcpynA(szKeyContainerName, pszContainer, MAX_PATH);
1792 else
1794 DWORD dwLen = sizeof(szKeyContainerName);
1795 if (!GetUserNameA(szKeyContainerName, &dwLen)) return FALSE;
1798 switch (dwFlags & (CRYPT_NEWKEYSET|CRYPT_VERIFYCONTEXT|CRYPT_DELETEKEYSET))
1800 case 0:
1801 *phProv = read_key_container(szKeyContainerName, dwFlags, pVTable);
1802 break;
1804 case CRYPT_DELETEKEYSET:
1805 return delete_container_key(szKeyContainerName, dwFlags);
1807 case CRYPT_NEWKEYSET:
1808 *phProv = read_key_container(szKeyContainerName, dwFlags, pVTable);
1809 if (*phProv != (HCRYPTPROV)INVALID_HANDLE_VALUE)
1811 release_handle(&handle_table, *phProv, RSAENH_MAGIC_CONTAINER);
1812 TRACE("Can't create new keyset, already exists\n");
1813 SetLastError(NTE_EXISTS);
1814 return FALSE;
1816 *phProv = new_key_container(szKeyContainerName, dwFlags, pVTable);
1817 break;
1819 case CRYPT_VERIFYCONTEXT|CRYPT_NEWKEYSET:
1820 case CRYPT_VERIFYCONTEXT:
1821 if (pszContainer && *pszContainer) {
1822 TRACE("pszContainer should be empty\n");
1823 SetLastError(NTE_BAD_FLAGS);
1824 return FALSE;
1826 *phProv = new_key_container("", dwFlags, pVTable);
1827 break;
1829 default:
1830 *phProv = (HCRYPTPROV)INVALID_HANDLE_VALUE;
1831 SetLastError(NTE_BAD_FLAGS);
1832 return FALSE;
1835 if (*phProv != (HCRYPTPROV)INVALID_HANDLE_VALUE) {
1836 SetLastError(ERROR_SUCCESS);
1837 return TRUE;
1838 } else {
1839 return FALSE;
1843 /******************************************************************************
1844 * CPCreateHash (RSAENH.@)
1846 * CPCreateHash creates and initializes a new hash object.
1848 * PARAMS
1849 * hProv [I] Handle to the key container to which the new hash will belong.
1850 * Algid [I] Identifies the hash algorithm, which will be used for the hash.
1851 * hKey [I] Handle to a session key applied for keyed hashes.
1852 * dwFlags [I] Currently no flags defined. Must be zero.
1853 * phHash [O] Points to the location where a handle to the new hash will be stored.
1855 * RETURNS
1856 * Success: TRUE
1857 * Failure: FALSE
1859 * NOTES
1860 * hKey is a handle to a session key applied in keyed hashes like MAC and HMAC.
1861 * If a normal hash object is to be created (like e.g. MD2 or SHA1) hKey must be zero.
1863 BOOL WINAPI RSAENH_CPCreateHash(HCRYPTPROV hProv, ALG_ID Algid, HCRYPTKEY hKey, DWORD dwFlags,
1864 HCRYPTHASH *phHash)
1866 CRYPTKEY *pCryptKey;
1867 CRYPTHASH *pCryptHash;
1868 const PROV_ENUMALGS_EX *peaAlgidInfo;
1870 TRACE("(hProv=%08lx, Algid=%08x, hKey=%08lx, dwFlags=%08x, phHash=%p)\n", hProv, Algid, hKey,
1871 dwFlags, phHash);
1873 peaAlgidInfo = get_algid_info(hProv, Algid);
1874 if (!peaAlgidInfo) return FALSE;
1876 if (dwFlags)
1878 SetLastError(NTE_BAD_FLAGS);
1879 return FALSE;
1882 if (Algid == CALG_MAC || Algid == CALG_HMAC || Algid == CALG_SCHANNEL_MASTER_HASH ||
1883 Algid == CALG_TLS1PRF)
1885 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey)) {
1886 SetLastError(NTE_BAD_KEY);
1887 return FALSE;
1890 if ((Algid == CALG_MAC) && (GET_ALG_TYPE(pCryptKey->aiAlgid) != ALG_TYPE_BLOCK)) {
1891 SetLastError(NTE_BAD_KEY);
1892 return FALSE;
1895 if ((Algid == CALG_SCHANNEL_MASTER_HASH || Algid == CALG_TLS1PRF) &&
1896 (pCryptKey->aiAlgid != CALG_TLS1_MASTER))
1898 SetLastError(NTE_BAD_KEY);
1899 return FALSE;
1901 if (Algid == CALG_SCHANNEL_MASTER_HASH &&
1902 ((!pCryptKey->siSChannelInfo.blobClientRandom.cbData) ||
1903 (!pCryptKey->siSChannelInfo.blobServerRandom.cbData)))
1905 SetLastError(ERROR_INVALID_PARAMETER);
1906 return FALSE;
1909 if ((Algid == CALG_TLS1PRF) && (pCryptKey->dwState != RSAENH_KEYSTATE_MASTERKEY)) {
1910 SetLastError(NTE_BAD_KEY_STATE);
1911 return FALSE;
1915 *phHash = new_object(&handle_table, sizeof(CRYPTHASH), RSAENH_MAGIC_HASH,
1916 destroy_hash, (OBJECTHDR**)&pCryptHash);
1917 if (!pCryptHash) return FALSE;
1919 pCryptHash->aiAlgid = Algid;
1920 pCryptHash->hKey = hKey;
1921 pCryptHash->hProv = hProv;
1922 pCryptHash->dwState = RSAENH_HASHSTATE_HASHING;
1923 pCryptHash->pHMACInfo = NULL;
1924 pCryptHash->dwHashSize = peaAlgidInfo->dwDefaultLen >> 3;
1925 init_data_blob(&pCryptHash->tpPRFParams.blobLabel);
1926 init_data_blob(&pCryptHash->tpPRFParams.blobSeed);
1928 if (Algid == CALG_SCHANNEL_MASTER_HASH) {
1929 static const char keyex[] = "key expansion";
1930 BYTE key_expansion[sizeof keyex];
1931 CRYPT_DATA_BLOB blobRandom, blobKeyExpansion = { 13, key_expansion };
1933 memcpy( key_expansion, keyex, sizeof keyex );
1935 if (pCryptKey->dwState != RSAENH_KEYSTATE_MASTERKEY) {
1936 static const char msec[] = "master secret";
1937 BYTE master_secret[sizeof msec];
1938 CRYPT_DATA_BLOB blobLabel = { 13, master_secret };
1939 BYTE abKeyValue[48];
1941 memcpy( master_secret, msec, sizeof msec );
1943 /* See RFC 2246, chapter 8.1 */
1944 if (!concat_data_blobs(&blobRandom,
1945 &pCryptKey->siSChannelInfo.blobClientRandom,
1946 &pCryptKey->siSChannelInfo.blobServerRandom))
1948 return FALSE;
1950 tls1_prf(hProv, hKey, &blobLabel, &blobRandom, abKeyValue, 48);
1951 pCryptKey->dwState = RSAENH_KEYSTATE_MASTERKEY;
1952 memcpy(pCryptKey->abKeyValue, abKeyValue, 48);
1953 free_data_blob(&blobRandom);
1956 /* See RFC 2246, chapter 6.3 */
1957 if (!concat_data_blobs(&blobRandom,
1958 &pCryptKey->siSChannelInfo.blobServerRandom,
1959 &pCryptKey->siSChannelInfo.blobClientRandom))
1961 return FALSE;
1963 tls1_prf(hProv, hKey, &blobKeyExpansion, &blobRandom, pCryptHash->abHashValue,
1964 RSAENH_MAX_HASH_SIZE);
1965 free_data_blob(&blobRandom);
1968 return init_hash(pCryptHash);
1971 /******************************************************************************
1972 * CPDestroyHash (RSAENH.@)
1974 * Releases the handle to a hash object. The object is destroyed if its reference
1975 * count reaches zero.
1977 * PARAMS
1978 * hProv [I] Handle to the key container to which the hash object belongs.
1979 * hHash [I] Handle to the hash object to be released.
1981 * RETURNS
1982 * Success: TRUE
1983 * Failure: FALSE
1985 BOOL WINAPI RSAENH_CPDestroyHash(HCRYPTPROV hProv, HCRYPTHASH hHash)
1987 TRACE("(hProv=%08lx, hHash=%08lx)\n", hProv, hHash);
1989 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
1991 SetLastError(NTE_BAD_UID);
1992 return FALSE;
1995 if (!release_handle(&handle_table, hHash, RSAENH_MAGIC_HASH))
1997 SetLastError(NTE_BAD_HASH);
1998 return FALSE;
2001 return TRUE;
2004 /******************************************************************************
2005 * CPDestroyKey (RSAENH.@)
2007 * Releases the handle to a key object. The object is destroyed if its reference
2008 * count reaches zero.
2010 * PARAMS
2011 * hProv [I] Handle to the key container to which the key object belongs.
2012 * hKey [I] Handle to the key object to be released.
2014 * RETURNS
2015 * Success: TRUE
2016 * Failure: FALSE
2018 BOOL WINAPI RSAENH_CPDestroyKey(HCRYPTPROV hProv, HCRYPTKEY hKey)
2020 TRACE("(hProv=%08lx, hKey=%08lx)\n", hProv, hKey);
2022 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
2024 SetLastError(NTE_BAD_UID);
2025 return FALSE;
2028 if (!release_handle(&handle_table, hKey, RSAENH_MAGIC_KEY))
2030 SetLastError(NTE_BAD_KEY);
2031 return FALSE;
2034 return TRUE;
2037 /******************************************************************************
2038 * CPDuplicateHash (RSAENH.@)
2040 * Clones a hash object including its current state.
2042 * PARAMS
2043 * hUID [I] Handle to the key container the hash belongs to.
2044 * hHash [I] Handle to the hash object to be cloned.
2045 * pdwReserved [I] Reserved. Must be NULL.
2046 * dwFlags [I] No flags are currently defined. Must be 0.
2047 * phHash [O] Handle to the cloned hash object.
2049 * RETURNS
2050 * Success: TRUE.
2051 * Failure: FALSE.
2053 BOOL WINAPI RSAENH_CPDuplicateHash(HCRYPTPROV hUID, HCRYPTHASH hHash, DWORD *pdwReserved,
2054 DWORD dwFlags, HCRYPTHASH *phHash)
2056 CRYPTHASH *pSrcHash, *pDestHash;
2058 TRACE("(hUID=%08lx, hHash=%08lx, pdwReserved=%p, dwFlags=%08x, phHash=%p)\n", hUID, hHash,
2059 pdwReserved, dwFlags, phHash);
2061 if (!is_valid_handle(&handle_table, hUID, RSAENH_MAGIC_CONTAINER))
2063 SetLastError(NTE_BAD_UID);
2064 return FALSE;
2067 if (!lookup_handle(&handle_table, hHash, RSAENH_MAGIC_HASH, (OBJECTHDR**)&pSrcHash))
2069 SetLastError(NTE_BAD_HASH);
2070 return FALSE;
2073 if (!phHash || pdwReserved || dwFlags)
2075 SetLastError(ERROR_INVALID_PARAMETER);
2076 return FALSE;
2079 *phHash = new_object(&handle_table, sizeof(CRYPTHASH), RSAENH_MAGIC_HASH,
2080 destroy_hash, (OBJECTHDR**)&pDestHash);
2081 if (*phHash != (HCRYPTHASH)INVALID_HANDLE_VALUE)
2083 *pDestHash = *pSrcHash;
2084 duplicate_hash_impl(pSrcHash->aiAlgid, &pSrcHash->context, &pDestHash->context);
2085 copy_hmac_info(&pDestHash->pHMACInfo, pSrcHash->pHMACInfo);
2086 copy_data_blob(&pDestHash->tpPRFParams.blobLabel, &pSrcHash->tpPRFParams.blobLabel);
2087 copy_data_blob(&pDestHash->tpPRFParams.blobSeed, &pSrcHash->tpPRFParams.blobSeed);
2090 return *phHash != (HCRYPTHASH)INVALID_HANDLE_VALUE;
2093 /******************************************************************************
2094 * CPDuplicateKey (RSAENH.@)
2096 * Clones a key object including its current state.
2098 * PARAMS
2099 * hUID [I] Handle to the key container the hash belongs to.
2100 * hKey [I] Handle to the key object to be cloned.
2101 * pdwReserved [I] Reserved. Must be NULL.
2102 * dwFlags [I] No flags are currently defined. Must be 0.
2103 * phHash [O] Handle to the cloned key object.
2105 * RETURNS
2106 * Success: TRUE.
2107 * Failure: FALSE.
2109 BOOL WINAPI RSAENH_CPDuplicateKey(HCRYPTPROV hUID, HCRYPTKEY hKey, DWORD *pdwReserved,
2110 DWORD dwFlags, HCRYPTKEY *phKey)
2112 CRYPTKEY *pSrcKey, *pDestKey;
2114 TRACE("(hUID=%08lx, hKey=%08lx, pdwReserved=%p, dwFlags=%08x, phKey=%p)\n", hUID, hKey,
2115 pdwReserved, dwFlags, phKey);
2117 if (!is_valid_handle(&handle_table, hUID, RSAENH_MAGIC_CONTAINER))
2119 SetLastError(NTE_BAD_UID);
2120 return FALSE;
2123 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pSrcKey))
2125 SetLastError(NTE_BAD_KEY);
2126 return FALSE;
2129 if (!phKey || pdwReserved || dwFlags)
2131 SetLastError(ERROR_INVALID_PARAMETER);
2132 return FALSE;
2135 *phKey = new_object(&handle_table, sizeof(CRYPTKEY), RSAENH_MAGIC_KEY, destroy_key,
2136 (OBJECTHDR**)&pDestKey);
2137 if (*phKey != (HCRYPTKEY)INVALID_HANDLE_VALUE)
2139 *pDestKey = *pSrcKey;
2140 copy_data_blob(&pDestKey->siSChannelInfo.blobServerRandom,
2141 &pSrcKey->siSChannelInfo.blobServerRandom);
2142 copy_data_blob(&pDestKey->siSChannelInfo.blobClientRandom,
2143 &pSrcKey->siSChannelInfo.blobClientRandom);
2144 duplicate_key_impl(pSrcKey->aiAlgid, &pSrcKey->context, &pDestKey->context);
2145 return TRUE;
2147 else
2149 return FALSE;
2153 /******************************************************************************
2154 * CPEncrypt (RSAENH.@)
2156 * Encrypt data.
2158 * PARAMS
2159 * hProv [I] The key container hKey and hHash belong to.
2160 * hKey [I] The key used to encrypt the data.
2161 * hHash [I] An optional hash object for parallel hashing. See notes.
2162 * Final [I] Indicates if this is the last block of data to encrypt.
2163 * dwFlags [I] Currently no flags defined. Must be zero.
2164 * pbData [I/O] Pointer to the data to encrypt. Encrypted data will also be stored there.
2165 * pdwDataLen [I/O] I: Length of data to encrypt, O: Length of encrypted data.
2166 * dwBufLen [I] Size of the buffer at pbData.
2168 * RETURNS
2169 * Success: TRUE.
2170 * Failure: FALSE.
2172 * NOTES
2173 * If a hash object handle is provided in hHash, it will be updated with the plaintext.
2174 * This is useful for message signatures.
2176 * This function uses the standard WINAPI protocol for querying data of dynamic length.
2178 BOOL WINAPI RSAENH_CPEncrypt(HCRYPTPROV hProv, HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final,
2179 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen, DWORD dwBufLen)
2181 CRYPTKEY *pCryptKey;
2182 BYTE *in, out[RSAENH_MAX_BLOCK_SIZE], o[RSAENH_MAX_BLOCK_SIZE];
2183 DWORD dwEncryptedLen, i, j, k;
2185 TRACE("(hProv=%08lx, hKey=%08lx, hHash=%08lx, Final=%d, dwFlags=%08x, pbData=%p, "
2186 "pdwDataLen=%p, dwBufLen=%d)\n", hProv, hKey, hHash, Final, dwFlags, pbData, pdwDataLen,
2187 dwBufLen);
2189 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
2191 SetLastError(NTE_BAD_UID);
2192 return FALSE;
2195 if (dwFlags)
2197 SetLastError(NTE_BAD_FLAGS);
2198 return FALSE;
2201 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
2203 SetLastError(NTE_BAD_KEY);
2204 return FALSE;
2207 if (pCryptKey->dwState == RSAENH_KEYSTATE_IDLE)
2208 pCryptKey->dwState = RSAENH_KEYSTATE_ENCRYPTING;
2210 if (pCryptKey->dwState != RSAENH_KEYSTATE_ENCRYPTING)
2212 SetLastError(NTE_BAD_DATA);
2213 return FALSE;
2216 if (is_valid_handle(&handle_table, hHash, RSAENH_MAGIC_HASH)) {
2217 if (!RSAENH_CPHashData(hProv, hHash, pbData, *pdwDataLen, 0)) return FALSE;
2220 if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_BLOCK) {
2221 if (!Final && (*pdwDataLen % pCryptKey->dwBlockLen)) {
2222 SetLastError(NTE_BAD_DATA);
2223 return FALSE;
2226 dwEncryptedLen = (*pdwDataLen/pCryptKey->dwBlockLen+(Final?1:0))*pCryptKey->dwBlockLen;
2228 if (pbData == NULL) {
2229 *pdwDataLen = dwEncryptedLen;
2230 return TRUE;
2232 else if (dwEncryptedLen > dwBufLen) {
2233 *pdwDataLen = dwEncryptedLen;
2234 SetLastError(ERROR_MORE_DATA);
2235 return FALSE;
2238 /* Pad final block with length bytes */
2239 for (i=*pdwDataLen; i<dwEncryptedLen; i++) pbData[i] = dwEncryptedLen - *pdwDataLen;
2240 *pdwDataLen = dwEncryptedLen;
2242 for (i=0, in=pbData; i<*pdwDataLen; i+=pCryptKey->dwBlockLen, in+=pCryptKey->dwBlockLen) {
2243 switch (pCryptKey->dwMode) {
2244 case CRYPT_MODE_ECB:
2245 encrypt_block_impl(pCryptKey->aiAlgid, 0, &pCryptKey->context, in, out,
2246 RSAENH_ENCRYPT);
2247 break;
2249 case CRYPT_MODE_CBC:
2250 for (j=0; j<pCryptKey->dwBlockLen; j++) in[j] ^= pCryptKey->abChainVector[j];
2251 encrypt_block_impl(pCryptKey->aiAlgid, 0, &pCryptKey->context, in, out,
2252 RSAENH_ENCRYPT);
2253 memcpy(pCryptKey->abChainVector, out, pCryptKey->dwBlockLen);
2254 break;
2256 case CRYPT_MODE_CFB:
2257 for (j=0; j<pCryptKey->dwBlockLen; j++) {
2258 encrypt_block_impl(pCryptKey->aiAlgid, 0, &pCryptKey->context,
2259 pCryptKey->abChainVector, o, RSAENH_ENCRYPT);
2260 out[j] = in[j] ^ o[0];
2261 for (k=0; k<pCryptKey->dwBlockLen-1; k++)
2262 pCryptKey->abChainVector[k] = pCryptKey->abChainVector[k+1];
2263 pCryptKey->abChainVector[k] = out[j];
2265 break;
2267 default:
2268 SetLastError(NTE_BAD_ALGID);
2269 return FALSE;
2271 memcpy(in, out, pCryptKey->dwBlockLen);
2273 } else if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_STREAM) {
2274 if (pbData == NULL) {
2275 *pdwDataLen = dwBufLen;
2276 return TRUE;
2278 encrypt_stream_impl(pCryptKey->aiAlgid, &pCryptKey->context, pbData, *pdwDataLen);
2279 } else if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_RSA) {
2280 if (pCryptKey->aiAlgid == CALG_RSA_SIGN) {
2281 SetLastError(NTE_BAD_KEY);
2282 return FALSE;
2284 if (!pbData) {
2285 *pdwDataLen = pCryptKey->dwBlockLen;
2286 return TRUE;
2288 if (dwBufLen < pCryptKey->dwBlockLen) {
2289 SetLastError(ERROR_MORE_DATA);
2290 return FALSE;
2292 if (!pad_data(pbData, *pdwDataLen, pbData, pCryptKey->dwBlockLen, dwFlags)) return FALSE;
2293 encrypt_block_impl(pCryptKey->aiAlgid, PK_PUBLIC, &pCryptKey->context, pbData, pbData, RSAENH_ENCRYPT);
2294 *pdwDataLen = pCryptKey->dwBlockLen;
2295 Final = TRUE;
2296 } else {
2297 SetLastError(NTE_BAD_TYPE);
2298 return FALSE;
2301 if (Final) setup_key(pCryptKey);
2303 return TRUE;
2306 /******************************************************************************
2307 * CPDecrypt (RSAENH.@)
2309 * Decrypt data.
2311 * PARAMS
2312 * hProv [I] The key container hKey and hHash belong to.
2313 * hKey [I] The key used to decrypt the data.
2314 * hHash [I] An optional hash object for parallel hashing. See notes.
2315 * Final [I] Indicates if this is the last block of data to decrypt.
2316 * dwFlags [I] Currently no flags defined. Must be zero.
2317 * pbData [I/O] Pointer to the data to decrypt. Plaintext will also be stored there.
2318 * pdwDataLen [I/O] I: Length of ciphertext, O: Length of plaintext.
2320 * RETURNS
2321 * Success: TRUE.
2322 * Failure: FALSE.
2324 * NOTES
2325 * If a hash object handle is provided in hHash, it will be updated with the plaintext.
2326 * This is useful for message signatures.
2328 * This function uses the standard WINAPI protocol for querying data of dynamic length.
2330 BOOL WINAPI RSAENH_CPDecrypt(HCRYPTPROV hProv, HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final,
2331 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
2333 CRYPTKEY *pCryptKey;
2334 BYTE *in, out[RSAENH_MAX_BLOCK_SIZE], o[RSAENH_MAX_BLOCK_SIZE];
2335 DWORD i, j, k;
2336 DWORD dwMax;
2338 TRACE("(hProv=%08lx, hKey=%08lx, hHash=%08lx, Final=%d, dwFlags=%08x, pbData=%p, "
2339 "pdwDataLen=%p)\n", hProv, hKey, hHash, Final, dwFlags, pbData, pdwDataLen);
2341 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
2343 SetLastError(NTE_BAD_UID);
2344 return FALSE;
2347 if (dwFlags)
2349 SetLastError(NTE_BAD_FLAGS);
2350 return FALSE;
2353 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
2355 SetLastError(NTE_BAD_KEY);
2356 return FALSE;
2359 if (pCryptKey->dwState == RSAENH_KEYSTATE_IDLE)
2360 pCryptKey->dwState = RSAENH_KEYSTATE_ENCRYPTING;
2362 if (pCryptKey->dwState != RSAENH_KEYSTATE_ENCRYPTING)
2364 SetLastError(NTE_BAD_DATA);
2365 return FALSE;
2368 dwMax=*pdwDataLen;
2370 if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_BLOCK) {
2371 for (i=0, in=pbData; i<*pdwDataLen; i+=pCryptKey->dwBlockLen, in+=pCryptKey->dwBlockLen) {
2372 switch (pCryptKey->dwMode) {
2373 case CRYPT_MODE_ECB:
2374 encrypt_block_impl(pCryptKey->aiAlgid, 0, &pCryptKey->context, in, out,
2375 RSAENH_DECRYPT);
2376 break;
2378 case CRYPT_MODE_CBC:
2379 encrypt_block_impl(pCryptKey->aiAlgid, 0, &pCryptKey->context, in, out,
2380 RSAENH_DECRYPT);
2381 for (j=0; j<pCryptKey->dwBlockLen; j++) out[j] ^= pCryptKey->abChainVector[j];
2382 memcpy(pCryptKey->abChainVector, in, pCryptKey->dwBlockLen);
2383 break;
2385 case CRYPT_MODE_CFB:
2386 for (j=0; j<pCryptKey->dwBlockLen; j++) {
2387 encrypt_block_impl(pCryptKey->aiAlgid, 0, &pCryptKey->context,
2388 pCryptKey->abChainVector, o, RSAENH_ENCRYPT);
2389 out[j] = in[j] ^ o[0];
2390 for (k=0; k<pCryptKey->dwBlockLen-1; k++)
2391 pCryptKey->abChainVector[k] = pCryptKey->abChainVector[k+1];
2392 pCryptKey->abChainVector[k] = in[j];
2394 break;
2396 default:
2397 SetLastError(NTE_BAD_ALGID);
2398 return FALSE;
2400 memcpy(in, out, pCryptKey->dwBlockLen);
2402 if (Final) {
2403 if (pbData[*pdwDataLen-1] &&
2404 pbData[*pdwDataLen-1] <= pCryptKey->dwBlockLen &&
2405 pbData[*pdwDataLen-1] <= *pdwDataLen) {
2406 BOOL padOkay = TRUE;
2408 /* check that every bad byte has the same value */
2409 for (i = 1; padOkay && i < pbData[*pdwDataLen-1]; i++)
2410 if (pbData[*pdwDataLen - i - 1] != pbData[*pdwDataLen - 1])
2411 padOkay = FALSE;
2412 if (padOkay)
2413 *pdwDataLen -= pbData[*pdwDataLen-1];
2414 else {
2415 SetLastError(NTE_BAD_DATA);
2416 setup_key(pCryptKey);
2417 return FALSE;
2420 else {
2421 SetLastError(NTE_BAD_DATA);
2422 setup_key(pCryptKey);
2423 return FALSE;
2427 } else if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_STREAM) {
2428 encrypt_stream_impl(pCryptKey->aiAlgid, &pCryptKey->context, pbData, *pdwDataLen);
2429 } else if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_RSA) {
2430 if (pCryptKey->aiAlgid == CALG_RSA_SIGN) {
2431 SetLastError(NTE_BAD_KEY);
2432 return FALSE;
2434 encrypt_block_impl(pCryptKey->aiAlgid, PK_PRIVATE, &pCryptKey->context, pbData, pbData, RSAENH_DECRYPT);
2435 if (!unpad_data(pbData, pCryptKey->dwBlockLen, pbData, pdwDataLen, dwFlags)) return FALSE;
2436 Final = TRUE;
2437 } else {
2438 SetLastError(NTE_BAD_TYPE);
2439 return FALSE;
2442 if (Final) setup_key(pCryptKey);
2444 if (is_valid_handle(&handle_table, hHash, RSAENH_MAGIC_HASH)) {
2445 if (*pdwDataLen>dwMax ||
2446 !RSAENH_CPHashData(hProv, hHash, pbData, *pdwDataLen, 0)) return FALSE;
2449 return TRUE;
2452 static BOOL crypt_export_simple(CRYPTKEY *pCryptKey, CRYPTKEY *pPubKey,
2453 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
2455 BLOBHEADER *pBlobHeader = (BLOBHEADER*)pbData;
2456 ALG_ID *pAlgid = (ALG_ID*)(pBlobHeader+1);
2457 DWORD dwDataLen;
2459 if (!(GET_ALG_CLASS(pCryptKey->aiAlgid)&(ALG_CLASS_DATA_ENCRYPT|ALG_CLASS_MSG_ENCRYPT))) {
2460 SetLastError(NTE_BAD_KEY); /* FIXME: error code? */
2461 return FALSE;
2464 dwDataLen = sizeof(BLOBHEADER) + sizeof(ALG_ID) + pPubKey->dwBlockLen;
2465 if (pbData) {
2466 if (*pdwDataLen < dwDataLen) {
2467 SetLastError(ERROR_MORE_DATA);
2468 *pdwDataLen = dwDataLen;
2469 return FALSE;
2472 pBlobHeader->bType = SIMPLEBLOB;
2473 pBlobHeader->bVersion = CUR_BLOB_VERSION;
2474 pBlobHeader->reserved = 0;
2475 pBlobHeader->aiKeyAlg = pCryptKey->aiAlgid;
2477 *pAlgid = pPubKey->aiAlgid;
2479 if (!pad_data(pCryptKey->abKeyValue, pCryptKey->dwKeyLen, (BYTE*)(pAlgid+1),
2480 pPubKey->dwBlockLen, dwFlags))
2482 return FALSE;
2485 encrypt_block_impl(pPubKey->aiAlgid, PK_PUBLIC, &pPubKey->context, (BYTE*)(pAlgid+1),
2486 (BYTE*)(pAlgid+1), RSAENH_ENCRYPT);
2488 *pdwDataLen = dwDataLen;
2489 return TRUE;
2492 static BOOL crypt_export_public_key(CRYPTKEY *pCryptKey, BYTE *pbData,
2493 DWORD *pdwDataLen)
2495 BLOBHEADER *pBlobHeader = (BLOBHEADER*)pbData;
2496 RSAPUBKEY *pRSAPubKey = (RSAPUBKEY*)(pBlobHeader+1);
2497 DWORD dwDataLen;
2499 if ((pCryptKey->aiAlgid != CALG_RSA_KEYX) && (pCryptKey->aiAlgid != CALG_RSA_SIGN)) {
2500 SetLastError(NTE_BAD_KEY);
2501 return FALSE;
2504 dwDataLen = sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) + pCryptKey->dwKeyLen;
2505 if (pbData) {
2506 if (*pdwDataLen < dwDataLen) {
2507 SetLastError(ERROR_MORE_DATA);
2508 *pdwDataLen = dwDataLen;
2509 return FALSE;
2512 pBlobHeader->bType = PUBLICKEYBLOB;
2513 pBlobHeader->bVersion = CUR_BLOB_VERSION;
2514 pBlobHeader->reserved = 0;
2515 pBlobHeader->aiKeyAlg = pCryptKey->aiAlgid;
2517 pRSAPubKey->magic = RSAENH_MAGIC_RSA1;
2518 pRSAPubKey->bitlen = pCryptKey->dwKeyLen << 3;
2520 export_public_key_impl((BYTE*)(pRSAPubKey+1), &pCryptKey->context,
2521 pCryptKey->dwKeyLen, &pRSAPubKey->pubexp);
2523 *pdwDataLen = dwDataLen;
2524 return TRUE;
2527 static BOOL crypt_export_private_key(CRYPTKEY *pCryptKey, BOOL force,
2528 BYTE *pbData, DWORD *pdwDataLen)
2530 BLOBHEADER *pBlobHeader = (BLOBHEADER*)pbData;
2531 RSAPUBKEY *pRSAPubKey = (RSAPUBKEY*)(pBlobHeader+1);
2532 DWORD dwDataLen;
2534 if ((pCryptKey->aiAlgid != CALG_RSA_KEYX) && (pCryptKey->aiAlgid != CALG_RSA_SIGN)) {
2535 SetLastError(NTE_BAD_KEY);
2536 return FALSE;
2538 if (!force && !(pCryptKey->dwPermissions & CRYPT_EXPORT))
2540 SetLastError(NTE_BAD_KEY_STATE);
2541 return FALSE;
2544 dwDataLen = sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) +
2545 2 * pCryptKey->dwKeyLen + 5 * ((pCryptKey->dwKeyLen + 1) >> 1);
2546 if (pbData) {
2547 if (*pdwDataLen < dwDataLen) {
2548 SetLastError(ERROR_MORE_DATA);
2549 *pdwDataLen = dwDataLen;
2550 return FALSE;
2553 pBlobHeader->bType = PRIVATEKEYBLOB;
2554 pBlobHeader->bVersion = CUR_BLOB_VERSION;
2555 pBlobHeader->reserved = 0;
2556 pBlobHeader->aiKeyAlg = pCryptKey->aiAlgid;
2558 pRSAPubKey->magic = RSAENH_MAGIC_RSA2;
2559 pRSAPubKey->bitlen = pCryptKey->dwKeyLen << 3;
2561 export_private_key_impl((BYTE*)(pRSAPubKey+1), &pCryptKey->context,
2562 pCryptKey->dwKeyLen, &pRSAPubKey->pubexp);
2564 *pdwDataLen = dwDataLen;
2565 return TRUE;
2568 static BOOL crypt_export_plaintext_key(CRYPTKEY *pCryptKey, BYTE *pbData,
2569 DWORD *pdwDataLen)
2571 BLOBHEADER *pBlobHeader = (BLOBHEADER*)pbData;
2572 DWORD *pKeyLen = (DWORD*)(pBlobHeader+1);
2573 BYTE *pbKey = (BYTE*)(pKeyLen+1);
2574 DWORD dwDataLen;
2576 dwDataLen = sizeof(BLOBHEADER) + sizeof(DWORD) + pCryptKey->dwKeyLen;
2577 if (pbData) {
2578 if (*pdwDataLen < dwDataLen) {
2579 SetLastError(ERROR_MORE_DATA);
2580 *pdwDataLen = dwDataLen;
2581 return FALSE;
2584 pBlobHeader->bType = PLAINTEXTKEYBLOB;
2585 pBlobHeader->bVersion = CUR_BLOB_VERSION;
2586 pBlobHeader->reserved = 0;
2587 pBlobHeader->aiKeyAlg = pCryptKey->aiAlgid;
2589 *pKeyLen = pCryptKey->dwKeyLen;
2590 memcpy(pbKey, pCryptKey->abKeyValue, pCryptKey->dwKeyLen);
2592 *pdwDataLen = dwDataLen;
2593 return TRUE;
2595 /******************************************************************************
2596 * crypt_export_key [Internal]
2598 * Export a key into a binary large object (BLOB). Called by CPExportKey and
2599 * by store_key_pair.
2601 * PARAMS
2602 * pCryptKey [I] Key to be exported.
2603 * hPubKey [I] Key used to encrypt sensitive BLOB data.
2604 * dwBlobType [I] SIMPLEBLOB, PUBLICKEYBLOB or PRIVATEKEYBLOB.
2605 * dwFlags [I] Currently none defined.
2606 * force [I] If TRUE, the key is written no matter what the key's
2607 * permissions are. Otherwise the key's permissions are
2608 * checked before exporting.
2609 * pbData [O] Pointer to a buffer where the BLOB will be written to.
2610 * pdwDataLen [I/O] I: Size of buffer at pbData, O: Size of BLOB
2612 * RETURNS
2613 * Success: TRUE.
2614 * Failure: FALSE.
2616 static BOOL crypt_export_key(CRYPTKEY *pCryptKey, HCRYPTKEY hPubKey,
2617 DWORD dwBlobType, DWORD dwFlags, BOOL force,
2618 BYTE *pbData, DWORD *pdwDataLen)
2620 CRYPTKEY *pPubKey;
2622 if (dwFlags & CRYPT_SSL2_FALLBACK) {
2623 if (pCryptKey->aiAlgid != CALG_SSL2_MASTER) {
2624 SetLastError(NTE_BAD_KEY);
2625 return FALSE;
2629 switch ((BYTE)dwBlobType)
2631 case SIMPLEBLOB:
2632 if (!lookup_handle(&handle_table, hPubKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pPubKey)){
2633 SetLastError(NTE_BAD_PUBLIC_KEY); /* FIXME: error_code? */
2634 return FALSE;
2636 return crypt_export_simple(pCryptKey, pPubKey, dwFlags, pbData,
2637 pdwDataLen);
2639 case PUBLICKEYBLOB:
2640 if (is_valid_handle(&handle_table, hPubKey, RSAENH_MAGIC_KEY)) {
2641 SetLastError(NTE_BAD_KEY); /* FIXME: error code? */
2642 return FALSE;
2645 return crypt_export_public_key(pCryptKey, pbData, pdwDataLen);
2647 case PRIVATEKEYBLOB:
2648 return crypt_export_private_key(pCryptKey, force, pbData, pdwDataLen);
2650 case PLAINTEXTKEYBLOB:
2651 return crypt_export_plaintext_key(pCryptKey, pbData, pdwDataLen);
2653 default:
2654 SetLastError(NTE_BAD_TYPE); /* FIXME: error code? */
2655 return FALSE;
2659 /******************************************************************************
2660 * CPExportKey (RSAENH.@)
2662 * Export a key into a binary large object (BLOB).
2664 * PARAMS
2665 * hProv [I] Key container from which a key is to be exported.
2666 * hKey [I] Key to be exported.
2667 * hPubKey [I] Key used to encrypt sensitive BLOB data.
2668 * dwBlobType [I] SIMPLEBLOB, PUBLICKEYBLOB or PRIVATEKEYBLOB.
2669 * dwFlags [I] Currently none defined.
2670 * pbData [O] Pointer to a buffer where the BLOB will be written to.
2671 * pdwDataLen [I/O] I: Size of buffer at pbData, O: Size of BLOB
2673 * RETURNS
2674 * Success: TRUE.
2675 * Failure: FALSE.
2677 BOOL WINAPI RSAENH_CPExportKey(HCRYPTPROV hProv, HCRYPTKEY hKey, HCRYPTKEY hPubKey,
2678 DWORD dwBlobType, DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
2680 CRYPTKEY *pCryptKey;
2682 TRACE("(hProv=%08lx, hKey=%08lx, hPubKey=%08lx, dwBlobType=%08x, dwFlags=%08x, pbData=%p,"
2683 "pdwDataLen=%p)\n", hProv, hKey, hPubKey, dwBlobType, dwFlags, pbData, pdwDataLen);
2685 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
2687 SetLastError(NTE_BAD_UID);
2688 return FALSE;
2691 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
2693 SetLastError(NTE_BAD_KEY);
2694 return FALSE;
2697 return crypt_export_key(pCryptKey, hPubKey, dwBlobType, dwFlags, FALSE,
2698 pbData, pdwDataLen);
2701 /******************************************************************************
2702 * release_and_install_key [Internal]
2704 * Release an existing key, if present, and replaces it with a new one.
2706 * PARAMS
2707 * hProv [I] Key container into which the key is to be imported.
2708 * src [I] Key which will replace *dest
2709 * dest [I] Points to key to be released and replaced with src
2710 * fStoreKey [I] If TRUE, the newly installed key is stored to the registry.
2712 static void release_and_install_key(HCRYPTPROV hProv, HCRYPTKEY src,
2713 HCRYPTKEY *dest, DWORD fStoreKey)
2715 RSAENH_CPDestroyKey(hProv, *dest);
2716 copy_handle(&handle_table, src, RSAENH_MAGIC_KEY, dest);
2717 if (fStoreKey)
2719 KEYCONTAINER *pKeyContainer;
2721 if ((pKeyContainer = get_key_container(hProv)))
2723 store_key_container_keys(pKeyContainer);
2724 store_key_container_permissions(pKeyContainer);
2729 /******************************************************************************
2730 * import_private_key [Internal]
2732 * Import a BLOB'ed private key into a key container.
2734 * PARAMS
2735 * hProv [I] Key container into which the private key is to be imported.
2736 * pbData [I] Pointer to a buffer which holds the private key BLOB.
2737 * dwDataLen [I] Length of data in buffer at pbData.
2738 * dwFlags [I] One of:
2739 * CRYPT_EXPORTABLE: the imported key is marked exportable
2740 * fStoreKey [I] If TRUE, the imported key is stored to the registry.
2741 * phKey [O] Handle to the imported key.
2744 * NOTES
2745 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2746 * it's a PRIVATEKEYBLOB.
2748 * RETURNS
2749 * Success: TRUE.
2750 * Failure: FALSE.
2752 static BOOL import_private_key(HCRYPTPROV hProv, const BYTE *pbData, DWORD dwDataLen,
2753 DWORD dwFlags, BOOL fStoreKey, HCRYPTKEY *phKey)
2755 KEYCONTAINER *pKeyContainer;
2756 CRYPTKEY *pCryptKey;
2757 const BLOBHEADER *pBlobHeader = (const BLOBHEADER*)pbData;
2758 const RSAPUBKEY *pRSAPubKey = (const RSAPUBKEY*)(pBlobHeader+1);
2759 BOOL ret;
2761 if (dwFlags & CRYPT_IPSEC_HMAC_KEY)
2763 FIXME("unimplemented for CRYPT_IPSEC_HMAC_KEY\n");
2764 SetLastError(NTE_BAD_FLAGS);
2765 return FALSE;
2767 if (!(pKeyContainer = get_key_container(hProv)))
2768 return FALSE;
2770 if ((dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY)))
2772 ERR("datalen %d not long enough for a BLOBHEADER + RSAPUBKEY\n",
2773 dwDataLen);
2774 SetLastError(NTE_BAD_DATA);
2775 return FALSE;
2777 if (pRSAPubKey->magic != RSAENH_MAGIC_RSA2)
2779 ERR("unexpected magic %08x\n", pRSAPubKey->magic);
2780 SetLastError(NTE_BAD_DATA);
2781 return FALSE;
2783 if ((dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) +
2784 (pRSAPubKey->bitlen >> 3) + (5 * ((pRSAPubKey->bitlen+8)>>4))))
2786 DWORD expectedLen = sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) +
2787 (pRSAPubKey->bitlen >> 3) + (5 * ((pRSAPubKey->bitlen+8)>>4));
2789 ERR("blob too short for pub key: expect %d, got %d\n",
2790 expectedLen, dwDataLen);
2791 SetLastError(NTE_BAD_DATA);
2792 return FALSE;
2795 *phKey = new_key(hProv, pBlobHeader->aiKeyAlg, MAKELONG(0,pRSAPubKey->bitlen), &pCryptKey);
2796 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) return FALSE;
2797 setup_key(pCryptKey);
2798 ret = import_private_key_impl((const BYTE*)(pRSAPubKey+1), &pCryptKey->context,
2799 pRSAPubKey->bitlen/8, dwDataLen, pRSAPubKey->pubexp);
2800 if (ret) {
2801 if (dwFlags & CRYPT_EXPORTABLE)
2802 pCryptKey->dwPermissions |= CRYPT_EXPORT;
2803 switch (pBlobHeader->aiKeyAlg)
2805 case AT_SIGNATURE:
2806 case CALG_RSA_SIGN:
2807 TRACE("installing signing key\n");
2808 release_and_install_key(hProv, *phKey, &pKeyContainer->hSignatureKeyPair,
2809 fStoreKey);
2810 break;
2811 case AT_KEYEXCHANGE:
2812 case CALG_RSA_KEYX:
2813 TRACE("installing key exchange key\n");
2814 release_and_install_key(hProv, *phKey, &pKeyContainer->hKeyExchangeKeyPair,
2815 fStoreKey);
2816 break;
2819 return ret;
2822 /******************************************************************************
2823 * import_public_key [Internal]
2825 * Import a BLOB'ed public key into a key container.
2827 * PARAMS
2828 * hProv [I] Key container into which the public key is to be imported.
2829 * pbData [I] Pointer to a buffer which holds the public key BLOB.
2830 * dwDataLen [I] Length of data in buffer at pbData.
2831 * dwFlags [I] One of:
2832 * CRYPT_EXPORTABLE: the imported key is marked exportable
2833 * fStoreKey [I] If TRUE, the imported key is stored to the registry.
2834 * phKey [O] Handle to the imported key.
2837 * NOTES
2838 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2839 * it's a PUBLICKEYBLOB.
2841 * RETURNS
2842 * Success: TRUE.
2843 * Failure: FALSE.
2845 static BOOL import_public_key(HCRYPTPROV hProv, const BYTE *pbData, DWORD dwDataLen,
2846 DWORD dwFlags, BOOL fStoreKey, HCRYPTKEY *phKey)
2848 KEYCONTAINER *pKeyContainer;
2849 CRYPTKEY *pCryptKey;
2850 const BLOBHEADER *pBlobHeader = (const BLOBHEADER*)pbData;
2851 const RSAPUBKEY *pRSAPubKey = (const RSAPUBKEY*)(pBlobHeader+1);
2852 ALG_ID algID;
2853 BOOL ret;
2855 if (dwFlags & CRYPT_IPSEC_HMAC_KEY)
2857 FIXME("unimplemented for CRYPT_IPSEC_HMAC_KEY\n");
2858 SetLastError(NTE_BAD_FLAGS);
2859 return FALSE;
2861 if (!(pKeyContainer = get_key_container(hProv)))
2862 return FALSE;
2864 if ((dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY)) ||
2865 (pRSAPubKey->magic != RSAENH_MAGIC_RSA1) ||
2866 (dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) + (pRSAPubKey->bitlen >> 3)))
2868 SetLastError(NTE_BAD_DATA);
2869 return FALSE;
2872 /* Since this is a public key blob, only the public key is
2873 * available, so only signature verification is possible.
2875 algID = pBlobHeader->aiKeyAlg;
2876 *phKey = new_key(hProv, algID, MAKELONG(0,pRSAPubKey->bitlen), &pCryptKey);
2877 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) return FALSE;
2878 setup_key(pCryptKey);
2879 ret = import_public_key_impl((const BYTE*)(pRSAPubKey+1), &pCryptKey->context,
2880 pRSAPubKey->bitlen >> 3, pRSAPubKey->pubexp);
2881 if (ret) {
2882 if (dwFlags & CRYPT_EXPORTABLE)
2883 pCryptKey->dwPermissions |= CRYPT_EXPORT;
2884 switch (pBlobHeader->aiKeyAlg)
2886 case AT_KEYEXCHANGE:
2887 case CALG_RSA_KEYX:
2888 TRACE("installing public key\n");
2889 release_and_install_key(hProv, *phKey, &pKeyContainer->hKeyExchangeKeyPair,
2890 fStoreKey);
2891 break;
2894 return ret;
2897 /******************************************************************************
2898 * import_symmetric_key [Internal]
2900 * Import a BLOB'ed symmetric key into a key container.
2902 * PARAMS
2903 * hProv [I] Key container into which the symmetric key is to be imported.
2904 * pbData [I] Pointer to a buffer which holds the symmetric key BLOB.
2905 * dwDataLen [I] Length of data in buffer at pbData.
2906 * hPubKey [I] Key used to decrypt sensitive BLOB data.
2907 * dwFlags [I] One of:
2908 * CRYPT_EXPORTABLE: the imported key is marked exportable
2909 * phKey [O] Handle to the imported key.
2912 * NOTES
2913 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2914 * it's a SIMPLEBLOB.
2916 * RETURNS
2917 * Success: TRUE.
2918 * Failure: FALSE.
2920 static BOOL import_symmetric_key(HCRYPTPROV hProv, const BYTE *pbData, DWORD dwDataLen,
2921 HCRYPTKEY hPubKey, DWORD dwFlags, HCRYPTKEY *phKey)
2923 CRYPTKEY *pCryptKey, *pPubKey;
2924 const BLOBHEADER *pBlobHeader = (const BLOBHEADER*)pbData;
2925 const ALG_ID *pAlgid = (const ALG_ID*)(pBlobHeader+1);
2926 const BYTE *pbKeyStream = (const BYTE*)(pAlgid + 1);
2927 BYTE *pbDecrypted;
2928 DWORD dwKeyLen;
2930 if (dwFlags & CRYPT_IPSEC_HMAC_KEY)
2932 FIXME("unimplemented for CRYPT_IPSEC_HMAC_KEY\n");
2933 SetLastError(NTE_BAD_FLAGS);
2934 return FALSE;
2936 if (!lookup_handle(&handle_table, hPubKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pPubKey) ||
2937 pPubKey->aiAlgid != CALG_RSA_KEYX)
2939 SetLastError(NTE_BAD_PUBLIC_KEY); /* FIXME: error code? */
2940 return FALSE;
2943 if (dwDataLen < sizeof(BLOBHEADER)+sizeof(ALG_ID)+pPubKey->dwBlockLen)
2945 SetLastError(NTE_BAD_DATA); /* FIXME: error code */
2946 return FALSE;
2949 pbDecrypted = HeapAlloc(GetProcessHeap(), 0, pPubKey->dwBlockLen);
2950 if (!pbDecrypted) return FALSE;
2951 encrypt_block_impl(pPubKey->aiAlgid, PK_PRIVATE, &pPubKey->context, pbKeyStream, pbDecrypted,
2952 RSAENH_DECRYPT);
2954 dwKeyLen = RSAENH_MAX_KEY_SIZE;
2955 if (!unpad_data(pbDecrypted, pPubKey->dwBlockLen, pbDecrypted, &dwKeyLen, dwFlags)) {
2956 HeapFree(GetProcessHeap(), 0, pbDecrypted);
2957 return FALSE;
2960 *phKey = new_key(hProv, pBlobHeader->aiKeyAlg, dwKeyLen<<19, &pCryptKey);
2961 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE)
2963 HeapFree(GetProcessHeap(), 0, pbDecrypted);
2964 return FALSE;
2966 memcpy(pCryptKey->abKeyValue, pbDecrypted, dwKeyLen);
2967 HeapFree(GetProcessHeap(), 0, pbDecrypted);
2968 setup_key(pCryptKey);
2969 if (dwFlags & CRYPT_EXPORTABLE)
2970 pCryptKey->dwPermissions |= CRYPT_EXPORT;
2971 return TRUE;
2974 /******************************************************************************
2975 * import_plaintext_key [Internal]
2977 * Import a plaintext key into a key container.
2979 * PARAMS
2980 * hProv [I] Key container into which the symmetric key is to be imported.
2981 * pbData [I] Pointer to a buffer which holds the plaintext key BLOB.
2982 * dwDataLen [I] Length of data in buffer at pbData.
2983 * dwFlags [I] One of:
2984 * CRYPT_EXPORTABLE: the imported key is marked exportable
2985 * phKey [O] Handle to the imported key.
2988 * NOTES
2989 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2990 * it's a PLAINTEXTKEYBLOB.
2992 * RETURNS
2993 * Success: TRUE.
2994 * Failure: FALSE.
2996 static BOOL import_plaintext_key(HCRYPTPROV hProv, const BYTE *pbData, DWORD dwDataLen,
2997 DWORD dwFlags, HCRYPTKEY *phKey)
2999 CRYPTKEY *pCryptKey;
3000 const BLOBHEADER *pBlobHeader = (const BLOBHEADER*)pbData;
3001 const DWORD *pKeyLen = (const DWORD *)(pBlobHeader + 1);
3002 const BYTE *pbKeyStream = (const BYTE*)(pKeyLen + 1);
3004 if (dwDataLen < sizeof(BLOBHEADER)+sizeof(DWORD)+*pKeyLen)
3006 SetLastError(NTE_BAD_DATA); /* FIXME: error code */
3007 return FALSE;
3010 if (dwFlags & CRYPT_IPSEC_HMAC_KEY)
3012 *phKey = new_key(hProv, CALG_HMAC, 0, &pCryptKey);
3013 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE)
3014 return FALSE;
3015 if (*pKeyLen <= RSAENH_MIN(sizeof(pCryptKey->abKeyValue), RSAENH_HMAC_BLOCK_LEN))
3017 memcpy(pCryptKey->abKeyValue, pbKeyStream, *pKeyLen);
3018 pCryptKey->dwKeyLen = *pKeyLen;
3020 else
3022 CRYPT_DATA_BLOB blobHmacKey = { *pKeyLen, (BYTE *)pbKeyStream };
3024 /* In order to initialize an HMAC key, the key material is hashed,
3025 * and the output of the hash function is used as the key material.
3026 * Unfortunately, the way the Crypto API is designed, we don't know
3027 * the hash algorithm yet, so we have to copy the entire key
3028 * material.
3030 if (!copy_data_blob(&pCryptKey->blobHmacKey, &blobHmacKey))
3032 release_handle(&handle_table, *phKey, RSAENH_MAGIC_KEY);
3033 *phKey = (HCRYPTKEY)INVALID_HANDLE_VALUE;
3034 return FALSE;
3037 setup_key(pCryptKey);
3038 if (dwFlags & CRYPT_EXPORTABLE)
3039 pCryptKey->dwPermissions |= CRYPT_EXPORT;
3041 else
3043 *phKey = new_key(hProv, pBlobHeader->aiKeyAlg, *pKeyLen<<19, &pCryptKey);
3044 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE)
3045 return FALSE;
3046 memcpy(pCryptKey->abKeyValue, pbKeyStream, *pKeyLen);
3047 setup_key(pCryptKey);
3048 if (dwFlags & CRYPT_EXPORTABLE)
3049 pCryptKey->dwPermissions |= CRYPT_EXPORT;
3051 return TRUE;
3054 /******************************************************************************
3055 * import_key [Internal]
3057 * Import a BLOB'ed key into a key container, optionally storing the key's
3058 * value to the registry.
3060 * PARAMS
3061 * hProv [I] Key container into which the key is to be imported.
3062 * pbData [I] Pointer to a buffer which holds the BLOB.
3063 * dwDataLen [I] Length of data in buffer at pbData.
3064 * hPubKey [I] Key used to decrypt sensitive BLOB data.
3065 * dwFlags [I] One of:
3066 * CRYPT_EXPORTABLE: the imported key is marked exportable
3067 * fStoreKey [I] If TRUE, the imported key is stored to the registry.
3068 * phKey [O] Handle to the imported key.
3070 * RETURNS
3071 * Success: TRUE.
3072 * Failure: FALSE.
3074 static BOOL import_key(HCRYPTPROV hProv, const BYTE *pbData, DWORD dwDataLen, HCRYPTKEY hPubKey,
3075 DWORD dwFlags, BOOL fStoreKey, HCRYPTKEY *phKey)
3077 KEYCONTAINER *pKeyContainer;
3078 const BLOBHEADER *pBlobHeader = (const BLOBHEADER*)pbData;
3080 if (!(pKeyContainer = get_key_container(hProv)))
3081 return FALSE;
3083 if (dwDataLen < sizeof(BLOBHEADER) ||
3084 pBlobHeader->bVersion != CUR_BLOB_VERSION ||
3085 pBlobHeader->reserved != 0)
3087 TRACE("bVersion = %d, reserved = %d\n", pBlobHeader->bVersion,
3088 pBlobHeader->reserved);
3089 SetLastError(NTE_BAD_DATA);
3090 return FALSE;
3093 /* If this is a verify-only context, the key is not persisted regardless of
3094 * fStoreKey's original value.
3096 fStoreKey = fStoreKey && !(dwFlags & CRYPT_VERIFYCONTEXT);
3097 TRACE("blob type: %x\n", pBlobHeader->bType);
3098 switch (pBlobHeader->bType)
3100 case PRIVATEKEYBLOB:
3101 return import_private_key(hProv, pbData, dwDataLen, dwFlags,
3102 fStoreKey, phKey);
3104 case PUBLICKEYBLOB:
3105 return import_public_key(hProv, pbData, dwDataLen, dwFlags,
3106 fStoreKey, phKey);
3108 case SIMPLEBLOB:
3109 return import_symmetric_key(hProv, pbData, dwDataLen, hPubKey,
3110 dwFlags, phKey);
3112 case PLAINTEXTKEYBLOB:
3113 return import_plaintext_key(hProv, pbData, dwDataLen, dwFlags,
3114 phKey);
3116 default:
3117 SetLastError(NTE_BAD_TYPE); /* FIXME: error code? */
3118 return FALSE;
3122 /******************************************************************************
3123 * CPImportKey (RSAENH.@)
3125 * Import a BLOB'ed key into a key container.
3127 * PARAMS
3128 * hProv [I] Key container into which the key is to be imported.
3129 * pbData [I] Pointer to a buffer which holds the BLOB.
3130 * dwDataLen [I] Length of data in buffer at pbData.
3131 * hPubKey [I] Key used to decrypt sensitive BLOB data.
3132 * dwFlags [I] One of:
3133 * CRYPT_EXPORTABLE: the imported key is marked exportable
3134 * phKey [O] Handle to the imported key.
3136 * RETURNS
3137 * Success: TRUE.
3138 * Failure: FALSE.
3140 BOOL WINAPI RSAENH_CPImportKey(HCRYPTPROV hProv, const BYTE *pbData, DWORD dwDataLen,
3141 HCRYPTKEY hPubKey, DWORD dwFlags, HCRYPTKEY *phKey)
3143 TRACE("(hProv=%08lx, pbData=%p, dwDataLen=%d, hPubKey=%08lx, dwFlags=%08x, phKey=%p)\n",
3144 hProv, pbData, dwDataLen, hPubKey, dwFlags, phKey);
3146 return import_key(hProv, pbData, dwDataLen, hPubKey, dwFlags, TRUE, phKey);
3149 /******************************************************************************
3150 * CPGenKey (RSAENH.@)
3152 * Generate a key in the key container
3154 * PARAMS
3155 * hProv [I] Key container for which a key is to be generated.
3156 * Algid [I] Crypto algorithm identifier for the key to be generated.
3157 * dwFlags [I] Upper 16 bits: Binary length of key. Lower 16 bits: Flags. See Notes
3158 * phKey [O] Handle to the generated key.
3160 * RETURNS
3161 * Success: TRUE.
3162 * Failure: FALSE.
3164 * FIXME
3165 * Flags currently not considered.
3167 * NOTES
3168 * Private key-exchange- and signature-keys can be generated with Algid AT_KEYEXCHANGE
3169 * and AT_SIGNATURE values.
3171 BOOL WINAPI RSAENH_CPGenKey(HCRYPTPROV hProv, ALG_ID Algid, DWORD dwFlags, HCRYPTKEY *phKey)
3173 KEYCONTAINER *pKeyContainer;
3174 CRYPTKEY *pCryptKey;
3176 TRACE("(hProv=%08lx, aiAlgid=%d, dwFlags=%08x, phKey=%p)\n", hProv, Algid, dwFlags, phKey);
3178 if (!(pKeyContainer = get_key_container(hProv)))
3180 /* MSDN: hProv not containing valid context handle */
3181 return FALSE;
3184 switch (Algid)
3186 case AT_SIGNATURE:
3187 case CALG_RSA_SIGN:
3188 *phKey = new_key(hProv, CALG_RSA_SIGN, dwFlags, &pCryptKey);
3189 if (pCryptKey) {
3190 new_key_impl(pCryptKey->aiAlgid, &pCryptKey->context, pCryptKey->dwKeyLen);
3191 setup_key(pCryptKey);
3192 release_and_install_key(hProv, *phKey,
3193 &pKeyContainer->hSignatureKeyPair,
3194 FALSE);
3196 break;
3198 case AT_KEYEXCHANGE:
3199 case CALG_RSA_KEYX:
3200 *phKey = new_key(hProv, CALG_RSA_KEYX, dwFlags, &pCryptKey);
3201 if (pCryptKey) {
3202 new_key_impl(pCryptKey->aiAlgid, &pCryptKey->context, pCryptKey->dwKeyLen);
3203 setup_key(pCryptKey);
3204 release_and_install_key(hProv, *phKey,
3205 &pKeyContainer->hKeyExchangeKeyPair,
3206 FALSE);
3208 break;
3210 case CALG_RC2:
3211 case CALG_RC4:
3212 case CALG_DES:
3213 case CALG_3DES_112:
3214 case CALG_3DES:
3215 case CALG_AES:
3216 case CALG_AES_128:
3217 case CALG_AES_192:
3218 case CALG_AES_256:
3219 case CALG_PCT1_MASTER:
3220 case CALG_SSL2_MASTER:
3221 case CALG_SSL3_MASTER:
3222 case CALG_TLS1_MASTER:
3223 *phKey = new_key(hProv, Algid, dwFlags, &pCryptKey);
3224 if (pCryptKey) {
3225 gen_rand_impl(pCryptKey->abKeyValue, RSAENH_MAX_KEY_SIZE);
3226 switch (Algid) {
3227 case CALG_SSL3_MASTER:
3228 pCryptKey->abKeyValue[0] = RSAENH_SSL3_VERSION_MAJOR;
3229 pCryptKey->abKeyValue[1] = RSAENH_SSL3_VERSION_MINOR;
3230 break;
3232 case CALG_TLS1_MASTER:
3233 pCryptKey->abKeyValue[0] = RSAENH_TLS1_VERSION_MAJOR;
3234 pCryptKey->abKeyValue[1] = RSAENH_TLS1_VERSION_MINOR;
3235 break;
3237 setup_key(pCryptKey);
3239 break;
3241 default:
3242 /* MSDN: Algorithm not supported specified by Algid */
3243 SetLastError(NTE_BAD_ALGID);
3244 return FALSE;
3247 return *phKey != (HCRYPTKEY)INVALID_HANDLE_VALUE;
3250 /******************************************************************************
3251 * CPGenRandom (RSAENH.@)
3253 * Generate a random byte stream.
3255 * PARAMS
3256 * hProv [I] Key container that is used to generate random bytes.
3257 * dwLen [I] Specifies the number of requested random data bytes.
3258 * pbBuffer [O] Random bytes will be stored here.
3260 * RETURNS
3261 * Success: TRUE
3262 * Failure: FALSE
3264 BOOL WINAPI RSAENH_CPGenRandom(HCRYPTPROV hProv, DWORD dwLen, BYTE *pbBuffer)
3266 TRACE("(hProv=%08lx, dwLen=%d, pbBuffer=%p)\n", hProv, dwLen, pbBuffer);
3268 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
3270 /* MSDN: hProv not containing valid context handle */
3271 SetLastError(NTE_BAD_UID);
3272 return FALSE;
3275 return gen_rand_impl(pbBuffer, dwLen);
3278 /******************************************************************************
3279 * CPGetHashParam (RSAENH.@)
3281 * Query parameters of an hash object.
3283 * PARAMS
3284 * hProv [I] The kea container, which the hash belongs to.
3285 * hHash [I] The hash object that is to be queried.
3286 * dwParam [I] Specifies the parameter that is to be queried.
3287 * pbData [I] Pointer to the buffer where the parameter value will be stored.
3288 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
3289 * dwFlags [I] None currently defined.
3291 * RETURNS
3292 * Success: TRUE
3293 * Failure: FALSE
3295 * NOTES
3296 * Valid dwParams are: HP_ALGID, HP_HASHSIZE, HP_HASHVALUE. The hash will be
3297 * finalized if HP_HASHVALUE is queried.
3299 BOOL WINAPI RSAENH_CPGetHashParam(HCRYPTPROV hProv, HCRYPTHASH hHash, DWORD dwParam, BYTE *pbData,
3300 DWORD *pdwDataLen, DWORD dwFlags)
3302 CRYPTHASH *pCryptHash;
3304 TRACE("(hProv=%08lx, hHash=%08lx, dwParam=%08x, pbData=%p, pdwDataLen=%p, dwFlags=%08x)\n",
3305 hProv, hHash, dwParam, pbData, pdwDataLen, dwFlags);
3307 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
3309 SetLastError(NTE_BAD_UID);
3310 return FALSE;
3313 if (dwFlags)
3315 SetLastError(NTE_BAD_FLAGS);
3316 return FALSE;
3319 if (!lookup_handle(&handle_table, hHash, RSAENH_MAGIC_HASH,
3320 (OBJECTHDR**)&pCryptHash))
3322 SetLastError(NTE_BAD_HASH);
3323 return FALSE;
3326 if (!pdwDataLen)
3328 SetLastError(ERROR_INVALID_PARAMETER);
3329 return FALSE;
3332 switch (dwParam)
3334 case HP_ALGID:
3335 return copy_param(pbData, pdwDataLen, (const BYTE*)&pCryptHash->aiAlgid,
3336 sizeof(ALG_ID));
3338 case HP_HASHSIZE:
3339 return copy_param(pbData, pdwDataLen, (const BYTE*)&pCryptHash->dwHashSize,
3340 sizeof(DWORD));
3342 case HP_HASHVAL:
3343 if (pCryptHash->aiAlgid == CALG_TLS1PRF) {
3344 return tls1_prf(hProv, pCryptHash->hKey, &pCryptHash->tpPRFParams.blobLabel,
3345 &pCryptHash->tpPRFParams.blobSeed, pbData, *pdwDataLen);
3348 if ( pbData == NULL ) {
3349 *pdwDataLen = pCryptHash->dwHashSize;
3350 return TRUE;
3353 if (pbData && (pCryptHash->dwState != RSAENH_HASHSTATE_FINISHED))
3355 finalize_hash(pCryptHash);
3356 pCryptHash->dwState = RSAENH_HASHSTATE_FINISHED;
3359 return copy_param(pbData, pdwDataLen, pCryptHash->abHashValue,
3360 pCryptHash->dwHashSize);
3362 default:
3363 SetLastError(NTE_BAD_TYPE);
3364 return FALSE;
3368 /******************************************************************************
3369 * CPSetKeyParam (RSAENH.@)
3371 * Set a parameter of a key object
3373 * PARAMS
3374 * hProv [I] The key container to which the key belongs.
3375 * hKey [I] The key for which a parameter is to be set.
3376 * dwParam [I] Parameter type. See Notes.
3377 * pbData [I] Pointer to the parameter value.
3378 * dwFlags [I] Currently none defined.
3380 * RETURNS
3381 * Success: TRUE.
3382 * Failure: FALSE.
3384 * NOTES:
3385 * Defined dwParam types are:
3386 * - KP_MODE: Values MODE_CBC, MODE_ECB, MODE_CFB.
3387 * - KP_MODE_BITS: Shift width for cipher feedback mode. (Currently ignored by MS CSP's)
3388 * - KP_PERMISSIONS: Or'ed combination of CRYPT_ENCRYPT, CRYPT_DECRYPT,
3389 * CRYPT_EXPORT, CRYPT_READ, CRYPT_WRITE, CRYPT_MAC
3390 * - KP_IV: Initialization vector
3392 BOOL WINAPI RSAENH_CPSetKeyParam(HCRYPTPROV hProv, HCRYPTKEY hKey, DWORD dwParam, BYTE *pbData,
3393 DWORD dwFlags)
3395 CRYPTKEY *pCryptKey;
3397 TRACE("(hProv=%08lx, hKey=%08lx, dwParam=%08x, pbData=%p, dwFlags=%08x)\n", hProv, hKey,
3398 dwParam, pbData, dwFlags);
3400 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
3402 SetLastError(NTE_BAD_UID);
3403 return FALSE;
3406 if (dwFlags) {
3407 SetLastError(NTE_BAD_FLAGS);
3408 return FALSE;
3411 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
3413 SetLastError(NTE_BAD_KEY);
3414 return FALSE;
3417 switch (dwParam) {
3418 case KP_PADDING:
3419 /* The MS providers only support PKCS5_PADDING */
3420 if (*(DWORD *)pbData != PKCS5_PADDING) {
3421 SetLastError(NTE_BAD_DATA);
3422 return FALSE;
3424 return TRUE;
3426 case KP_MODE:
3427 pCryptKey->dwMode = *(DWORD*)pbData;
3428 return TRUE;
3430 case KP_MODE_BITS:
3431 pCryptKey->dwModeBits = *(DWORD*)pbData;
3432 return TRUE;
3434 case KP_PERMISSIONS:
3436 DWORD perms = *(DWORD *)pbData;
3438 if ((perms & CRYPT_EXPORT) &&
3439 !(pCryptKey->dwPermissions & CRYPT_EXPORT))
3441 SetLastError(NTE_BAD_DATA);
3442 return FALSE;
3444 else if (!(perms & CRYPT_EXPORT) &&
3445 (pCryptKey->dwPermissions & CRYPT_EXPORT))
3447 /* Clearing the export permission appears to be ignored,
3448 * see tests.
3450 perms |= CRYPT_EXPORT;
3452 pCryptKey->dwPermissions = perms;
3453 return TRUE;
3456 case KP_IV:
3457 memcpy(pCryptKey->abInitVector, pbData, pCryptKey->dwBlockLen);
3458 setup_key(pCryptKey);
3459 return TRUE;
3461 case KP_SALT:
3462 switch (pCryptKey->aiAlgid) {
3463 case CALG_RC2:
3464 case CALG_RC4:
3466 KEYCONTAINER *pKeyContainer = get_key_container(pCryptKey->hProv);
3467 if (!pbData)
3469 SetLastError(ERROR_INVALID_PARAMETER);
3470 return FALSE;
3472 /* MSDN: the base provider always sets eleven bytes of
3473 * salt value.
3475 memcpy(pCryptKey->abKeyValue + pCryptKey->dwKeyLen,
3476 pbData, 11);
3477 pCryptKey->dwSaltLen = 11;
3478 setup_key(pCryptKey);
3479 /* After setting the salt value if the provider is not base or
3480 * strong the salt length will be reset. */
3481 if (pKeyContainer->dwPersonality != RSAENH_PERSONALITY_BASE &&
3482 pKeyContainer->dwPersonality != RSAENH_PERSONALITY_STRONG)
3483 pCryptKey->dwSaltLen = 0;
3484 break;
3486 default:
3487 SetLastError(NTE_BAD_KEY);
3488 return FALSE;
3490 return TRUE;
3492 case KP_SALT_EX:
3494 CRYPT_INTEGER_BLOB *blob = (CRYPT_INTEGER_BLOB *)pbData;
3496 /* salt length can't be greater than 184 bits = 24 bytes */
3497 if (blob->cbData > 24)
3499 SetLastError(NTE_BAD_DATA);
3500 return FALSE;
3502 memcpy(pCryptKey->abKeyValue + pCryptKey->dwKeyLen, blob->pbData,
3503 blob->cbData);
3504 pCryptKey->dwSaltLen = blob->cbData;
3505 setup_key(pCryptKey);
3506 return TRUE;
3509 case KP_EFFECTIVE_KEYLEN:
3510 switch (pCryptKey->aiAlgid) {
3511 case CALG_RC2:
3513 DWORD keylen, deflen;
3514 BOOL ret = TRUE;
3515 KEYCONTAINER *pKeyContainer = get_key_container(pCryptKey->hProv);
3517 if (!pbData)
3519 SetLastError(ERROR_INVALID_PARAMETER);
3520 return FALSE;
3522 keylen = *(DWORD *)pbData;
3523 if (!keylen || keylen > 1024)
3525 SetLastError(NTE_BAD_DATA);
3526 return FALSE;
3530 * The Base provider will force the key length to default
3531 * and set an error state if a key length different from
3532 * the default is tried.
3534 deflen = aProvEnumAlgsEx[pKeyContainer->dwPersonality]->dwDefaultLen;
3535 if (pKeyContainer->dwPersonality == RSAENH_PERSONALITY_BASE
3536 && keylen != deflen)
3538 keylen = deflen;
3539 SetLastError(NTE_BAD_DATA);
3540 ret = FALSE;
3542 pCryptKey->dwEffectiveKeyLen = keylen;
3543 setup_key(pCryptKey);
3544 return ret;
3546 default:
3547 SetLastError(NTE_BAD_TYPE);
3548 return FALSE;
3550 return TRUE;
3552 case KP_SCHANNEL_ALG:
3553 switch (((PSCHANNEL_ALG)pbData)->dwUse) {
3554 case SCHANNEL_ENC_KEY:
3555 memcpy(&pCryptKey->siSChannelInfo.saEncAlg, pbData, sizeof(SCHANNEL_ALG));
3556 break;
3558 case SCHANNEL_MAC_KEY:
3559 memcpy(&pCryptKey->siSChannelInfo.saMACAlg, pbData, sizeof(SCHANNEL_ALG));
3560 break;
3562 default:
3563 SetLastError(NTE_FAIL); /* FIXME: error code */
3564 return FALSE;
3566 return TRUE;
3568 case KP_CLIENT_RANDOM:
3569 return copy_data_blob(&pCryptKey->siSChannelInfo.blobClientRandom, (PCRYPT_DATA_BLOB)pbData);
3571 case KP_SERVER_RANDOM:
3572 return copy_data_blob(&pCryptKey->siSChannelInfo.blobServerRandom, (PCRYPT_DATA_BLOB)pbData);
3574 default:
3575 SetLastError(NTE_BAD_TYPE);
3576 return FALSE;
3580 /******************************************************************************
3581 * CPGetKeyParam (RSAENH.@)
3583 * Query a key parameter.
3585 * PARAMS
3586 * hProv [I] The key container, which the key belongs to.
3587 * hHash [I] The key object that is to be queried.
3588 * dwParam [I] Specifies the parameter that is to be queried.
3589 * pbData [I] Pointer to the buffer where the parameter value will be stored.
3590 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
3591 * dwFlags [I] None currently defined.
3593 * RETURNS
3594 * Success: TRUE
3595 * Failure: FALSE
3597 * NOTES
3598 * Defined dwParam types are:
3599 * - KP_MODE: Values MODE_CBC, MODE_ECB, MODE_CFB.
3600 * - KP_MODE_BITS: Shift width for cipher feedback mode.
3601 * (Currently ignored by MS CSP's - always eight)
3602 * - KP_PERMISSIONS: Or'ed combination of CRYPT_ENCRYPT, CRYPT_DECRYPT,
3603 * CRYPT_EXPORT, CRYPT_READ, CRYPT_WRITE, CRYPT_MAC
3604 * - KP_IV: Initialization vector.
3605 * - KP_KEYLEN: Bitwidth of the key.
3606 * - KP_BLOCKLEN: Size of a block cipher block.
3607 * - KP_SALT: Salt value.
3609 BOOL WINAPI RSAENH_CPGetKeyParam(HCRYPTPROV hProv, HCRYPTKEY hKey, DWORD dwParam, BYTE *pbData,
3610 DWORD *pdwDataLen, DWORD dwFlags)
3612 CRYPTKEY *pCryptKey;
3613 DWORD dwValue;
3615 TRACE("(hProv=%08lx, hKey=%08lx, dwParam=%08x, pbData=%p, pdwDataLen=%p dwFlags=%08x)\n",
3616 hProv, hKey, dwParam, pbData, pdwDataLen, dwFlags);
3618 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
3620 SetLastError(NTE_BAD_UID);
3621 return FALSE;
3624 if (dwFlags) {
3625 SetLastError(NTE_BAD_FLAGS);
3626 return FALSE;
3629 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
3631 SetLastError(NTE_BAD_KEY);
3632 return FALSE;
3635 switch (dwParam)
3637 case KP_IV:
3638 return copy_param(pbData, pdwDataLen, pCryptKey->abInitVector,
3639 pCryptKey->dwBlockLen);
3641 case KP_SALT:
3642 switch (pCryptKey->aiAlgid) {
3643 case CALG_RC2:
3644 case CALG_RC4:
3645 return copy_param(pbData, pdwDataLen,
3646 &pCryptKey->abKeyValue[pCryptKey->dwKeyLen],
3647 pCryptKey->dwSaltLen);
3648 default:
3649 SetLastError(NTE_BAD_KEY);
3650 return FALSE;
3653 case KP_PADDING:
3654 dwValue = PKCS5_PADDING;
3655 return copy_param(pbData, pdwDataLen, (const BYTE*)&dwValue, sizeof(DWORD));
3657 case KP_KEYLEN:
3658 dwValue = pCryptKey->dwKeyLen << 3;
3659 return copy_param(pbData, pdwDataLen, (const BYTE*)&dwValue, sizeof(DWORD));
3661 case KP_EFFECTIVE_KEYLEN:
3662 if (pCryptKey->dwEffectiveKeyLen)
3663 dwValue = pCryptKey->dwEffectiveKeyLen;
3664 else
3665 dwValue = pCryptKey->dwKeyLen << 3;
3666 return copy_param(pbData, pdwDataLen, (const BYTE*)&dwValue, sizeof(DWORD));
3668 case KP_BLOCKLEN:
3669 dwValue = pCryptKey->dwBlockLen << 3;
3670 return copy_param(pbData, pdwDataLen, (const BYTE*)&dwValue, sizeof(DWORD));
3672 case KP_MODE:
3673 return copy_param(pbData, pdwDataLen, (const BYTE*)&pCryptKey->dwMode, sizeof(DWORD));
3675 case KP_MODE_BITS:
3676 return copy_param(pbData, pdwDataLen, (const BYTE*)&pCryptKey->dwModeBits,
3677 sizeof(DWORD));
3679 case KP_PERMISSIONS:
3680 return copy_param(pbData, pdwDataLen, (const BYTE*)&pCryptKey->dwPermissions,
3681 sizeof(DWORD));
3683 case KP_ALGID:
3684 return copy_param(pbData, pdwDataLen, (const BYTE*)&pCryptKey->aiAlgid, sizeof(DWORD));
3686 default:
3687 SetLastError(NTE_BAD_TYPE);
3688 return FALSE;
3692 /******************************************************************************
3693 * CPGetProvParam (RSAENH.@)
3695 * Query a CSP parameter.
3697 * PARAMS
3698 * hProv [I] The key container that is to be queried.
3699 * dwParam [I] Specifies the parameter that is to be queried.
3700 * pbData [I] Pointer to the buffer where the parameter value will be stored.
3701 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
3702 * dwFlags [I] CRYPT_FIRST: Start enumeration (for PP_ENUMALGS{_EX}).
3704 * RETURNS
3705 * Success: TRUE
3706 * Failure: FALSE
3707 * NOTES:
3708 * Defined dwParam types:
3709 * - PP_CONTAINER: Name of the key container.
3710 * - PP_NAME: Name of the cryptographic service provider.
3711 * - PP_SIG_KEYSIZE_INC: RSA signature keywidth granularity in bits.
3712 * - PP_KEYX_KEYSIZE_INC: RSA key-exchange keywidth granularity in bits.
3713 * - PP_ENUMALGS{_EX}: Query provider capabilities.
3714 * - PP_KEYSET_SEC_DESCR: Retrieve security descriptor on container.
3716 BOOL WINAPI RSAENH_CPGetProvParam(HCRYPTPROV hProv, DWORD dwParam, BYTE *pbData,
3717 DWORD *pdwDataLen, DWORD dwFlags)
3719 KEYCONTAINER *pKeyContainer;
3720 PROV_ENUMALGS provEnumalgs;
3721 DWORD dwTemp;
3722 HKEY hKey;
3724 /* This is for dwParam PP_CRYPT_COUNT_KEY_USE.
3725 * IE6 SP1 asks for it in the 'About' dialog.
3726 * Returning this BLOB seems to satisfy IE. The marked 0x00 seem
3727 * to be 'don't care's. If you know anything more specific about
3728 * this provider parameter, please report to wine-devel@winehq.org */
3729 static const BYTE abWTF[96] = {
3730 0xb0, 0x25, 0x63, 0x86, 0x9c, 0xab, 0xb6, 0x37,
3731 0xe8, 0x82, /**/0x00,/**/ 0x72, 0x06, 0xb2, /**/0x00,/**/ 0x3b,
3732 0x60, 0x35, /**/0x00,/**/ 0x3b, 0x88, 0xce, /**/0x00,/**/ 0x82,
3733 0xbc, 0x7a, /**/0x00,/**/ 0xb7, 0x4f, 0x7e, /**/0x00,/**/ 0xde,
3734 0x92, 0xf1, /**/0x00,/**/ 0x83, 0xea, 0x5e, /**/0x00,/**/ 0xc8,
3735 0x12, 0x1e, 0xd4, 0x06, 0xf7, 0x66, /**/0x00,/**/ 0x01,
3736 0x29, 0xa4, /**/0x00,/**/ 0xf8, 0x24, 0x0c, /**/0x00,/**/ 0x33,
3737 0x06, 0x80, /**/0x00,/**/ 0x02, 0x46, 0x0b, /**/0x00,/**/ 0x6d,
3738 0x5b, 0xca, /**/0x00,/**/ 0x9a, 0x10, 0xf0, /**/0x00,/**/ 0x05,
3739 0x19, 0xd0, /**/0x00,/**/ 0x2c, 0xf6, 0x27, /**/0x00,/**/ 0xaa,
3740 0x7c, 0x6f, /**/0x00,/**/ 0xb9, 0xd8, 0x72, /**/0x00,/**/ 0x03,
3741 0xf3, 0x81, /**/0x00,/**/ 0xfa, 0xe8, 0x26, /**/0x00,/**/ 0xca
3744 TRACE("(hProv=%08lx, dwParam=%08x, pbData=%p, pdwDataLen=%p, dwFlags=%08x)\n",
3745 hProv, dwParam, pbData, pdwDataLen, dwFlags);
3747 if (!pdwDataLen) {
3748 SetLastError(ERROR_INVALID_PARAMETER);
3749 return FALSE;
3752 if (!(pKeyContainer = get_key_container(hProv)))
3754 /* MSDN: hProv not containing valid context handle */
3755 return FALSE;
3758 switch (dwParam)
3760 case PP_CONTAINER:
3761 case PP_UNIQUE_CONTAINER:/* MSDN says we can return the same value as PP_CONTAINER */
3762 return copy_param(pbData, pdwDataLen, (const BYTE*)pKeyContainer->szName,
3763 strlen(pKeyContainer->szName)+1);
3765 case PP_NAME:
3766 return copy_param(pbData, pdwDataLen, (const BYTE*)pKeyContainer->szProvName,
3767 strlen(pKeyContainer->szProvName)+1);
3769 case PP_PROVTYPE:
3770 dwTemp = PROV_RSA_FULL;
3771 return copy_param(pbData, pdwDataLen, (const BYTE*)&dwTemp, sizeof(dwTemp));
3773 case PP_KEYSPEC:
3774 dwTemp = AT_SIGNATURE | AT_KEYEXCHANGE;
3775 return copy_param(pbData, pdwDataLen, (const BYTE*)&dwTemp, sizeof(dwTemp));
3777 case PP_KEYSET_TYPE:
3778 dwTemp = pKeyContainer->dwFlags & CRYPT_MACHINE_KEYSET;
3779 return copy_param(pbData, pdwDataLen, (const BYTE*)&dwTemp, sizeof(dwTemp));
3781 case PP_KEYSTORAGE:
3782 dwTemp = CRYPT_SEC_DESCR;
3783 return copy_param(pbData, pdwDataLen, (const BYTE*)&dwTemp, sizeof(dwTemp));
3785 case PP_SIG_KEYSIZE_INC:
3786 case PP_KEYX_KEYSIZE_INC:
3787 dwTemp = 8;
3788 return copy_param(pbData, pdwDataLen, (const BYTE*)&dwTemp, sizeof(dwTemp));
3790 case PP_IMPTYPE:
3791 dwTemp = CRYPT_IMPL_SOFTWARE;
3792 return copy_param(pbData, pdwDataLen, (const BYTE*)&dwTemp, sizeof(dwTemp));
3794 case PP_VERSION:
3795 dwTemp = 0x00000200;
3796 return copy_param(pbData, pdwDataLen, (const BYTE*)&dwTemp, sizeof(dwTemp));
3798 case PP_ENUMCONTAINERS:
3799 if ((dwFlags & CRYPT_FIRST) == CRYPT_FIRST) pKeyContainer->dwEnumContainersCtr = 0;
3801 if (!pbData) {
3802 *pdwDataLen = (DWORD)MAX_PATH + 1;
3803 return TRUE;
3806 if (!open_container_key("", dwFlags, KEY_READ, &hKey))
3808 SetLastError(ERROR_NO_MORE_ITEMS);
3809 return FALSE;
3812 dwTemp = *pdwDataLen;
3813 switch (RegEnumKeyExA(hKey, pKeyContainer->dwEnumContainersCtr, (LPSTR)pbData, &dwTemp,
3814 NULL, NULL, NULL, NULL))
3816 case ERROR_MORE_DATA:
3817 *pdwDataLen = (DWORD)MAX_PATH + 1;
3819 case ERROR_SUCCESS:
3820 pKeyContainer->dwEnumContainersCtr++;
3821 RegCloseKey(hKey);
3822 return TRUE;
3824 case ERROR_NO_MORE_ITEMS:
3825 default:
3826 SetLastError(ERROR_NO_MORE_ITEMS);
3827 RegCloseKey(hKey);
3828 return FALSE;
3831 case PP_ENUMALGS:
3832 case PP_ENUMALGS_EX:
3833 if (((pKeyContainer->dwEnumAlgsCtr >= RSAENH_MAX_ENUMALGS-1) ||
3834 (!aProvEnumAlgsEx[pKeyContainer->dwPersonality]
3835 [pKeyContainer->dwEnumAlgsCtr+1].aiAlgid)) &&
3836 ((dwFlags & CRYPT_FIRST) != CRYPT_FIRST))
3838 SetLastError(ERROR_NO_MORE_ITEMS);
3839 return FALSE;
3842 if (dwParam == PP_ENUMALGS) {
3843 if (pbData && (*pdwDataLen >= sizeof(PROV_ENUMALGS)))
3844 pKeyContainer->dwEnumAlgsCtr = ((dwFlags & CRYPT_FIRST) == CRYPT_FIRST) ?
3845 0 : pKeyContainer->dwEnumAlgsCtr+1;
3847 provEnumalgs.aiAlgid = aProvEnumAlgsEx
3848 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr].aiAlgid;
3849 provEnumalgs.dwBitLen = aProvEnumAlgsEx
3850 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr].dwDefaultLen;
3851 provEnumalgs.dwNameLen = aProvEnumAlgsEx
3852 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr].dwNameLen;
3853 memcpy(provEnumalgs.szName, aProvEnumAlgsEx
3854 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr].szName,
3855 20*sizeof(CHAR));
3857 return copy_param(pbData, pdwDataLen, (const BYTE*)&provEnumalgs,
3858 sizeof(PROV_ENUMALGS));
3859 } else {
3860 if (pbData && (*pdwDataLen >= sizeof(PROV_ENUMALGS_EX)))
3861 pKeyContainer->dwEnumAlgsCtr = ((dwFlags & CRYPT_FIRST) == CRYPT_FIRST) ?
3862 0 : pKeyContainer->dwEnumAlgsCtr+1;
3864 return copy_param(pbData, pdwDataLen,
3865 (const BYTE*)&aProvEnumAlgsEx
3866 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr],
3867 sizeof(PROV_ENUMALGS_EX));
3870 case PP_CRYPT_COUNT_KEY_USE: /* Asked for by IE About dialog */
3871 return copy_param(pbData, pdwDataLen, abWTF, sizeof(abWTF));
3873 case PP_KEYSET_SEC_DESCR:
3875 SECURITY_DESCRIPTOR *sd;
3876 DWORD err, len, flags = (pKeyContainer->dwFlags & CRYPT_MACHINE_KEYSET);
3878 if (!open_container_key(pKeyContainer->szName, flags, KEY_READ, &hKey))
3880 SetLastError(NTE_BAD_KEYSET);
3881 return FALSE;
3884 err = GetSecurityInfo(hKey, SE_REGISTRY_KEY, dwFlags, NULL, NULL, NULL, NULL, (void **)&sd);
3885 RegCloseKey(hKey);
3886 if (err)
3888 SetLastError(err);
3889 return FALSE;
3892 len = GetSecurityDescriptorLength(sd);
3893 if (*pdwDataLen >= len) memcpy(pbData, sd, len);
3894 else SetLastError(ERROR_INSUFFICIENT_BUFFER);
3895 *pdwDataLen = len;
3897 LocalFree(sd);
3898 return TRUE;
3901 default:
3902 /* MSDN: Unknown parameter number in dwParam */
3903 SetLastError(NTE_BAD_TYPE);
3904 return FALSE;
3908 /******************************************************************************
3909 * CPDeriveKey (RSAENH.@)
3911 * Derives a key from a hash value.
3913 * PARAMS
3914 * hProv [I] Key container for which a key is to be generated.
3915 * Algid [I] Crypto algorithm identifier for the key to be generated.
3916 * hBaseData [I] Hash from whose value the key will be derived.
3917 * dwFlags [I] See Notes.
3918 * phKey [O] The generated key.
3920 * RETURNS
3921 * Success: TRUE
3922 * Failure: FALSE
3924 * NOTES
3925 * Defined flags:
3926 * - CRYPT_EXPORTABLE: Key can be exported.
3927 * - CRYPT_NO_SALT: No salt is used for 40 bit keys.
3928 * - CRYPT_CREATE_SALT: Use remaining bits as salt value.
3930 BOOL WINAPI RSAENH_CPDeriveKey(HCRYPTPROV hProv, ALG_ID Algid, HCRYPTHASH hBaseData,
3931 DWORD dwFlags, HCRYPTKEY *phKey)
3933 CRYPTKEY *pCryptKey, *pMasterKey;
3934 CRYPTHASH *pCryptHash;
3935 BYTE abHashValue[RSAENH_MAX_HASH_SIZE*2];
3936 DWORD dwLen;
3938 TRACE("(hProv=%08lx, Algid=%d, hBaseData=%08lx, dwFlags=%08x phKey=%p)\n", hProv, Algid,
3939 hBaseData, dwFlags, phKey);
3941 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
3943 SetLastError(NTE_BAD_UID);
3944 return FALSE;
3947 if (!lookup_handle(&handle_table, hBaseData, RSAENH_MAGIC_HASH,
3948 (OBJECTHDR**)&pCryptHash))
3950 SetLastError(NTE_BAD_HASH);
3951 return FALSE;
3954 if (!phKey)
3956 SetLastError(ERROR_INVALID_PARAMETER);
3957 return FALSE;
3960 switch (GET_ALG_CLASS(Algid))
3962 case ALG_CLASS_DATA_ENCRYPT:
3964 int need_padding, copy_len;
3965 *phKey = new_key(hProv, Algid, dwFlags, &pCryptKey);
3966 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) return FALSE;
3969 * We derive the key material from the hash.
3970 * If the hash value is not large enough for the claimed key, we have to construct
3971 * a larger binary value based on the hash. This is documented in MSDN: CryptDeriveKey.
3973 dwLen = RSAENH_MAX_HASH_SIZE;
3974 RSAENH_CPGetHashParam(pCryptHash->hProv, hBaseData, HP_HASHVAL, abHashValue, &dwLen, 0);
3977 * The usage of padding seems to vary from algorithm to algorithm.
3978 * For now the only different case found was for AES with 128 bit key.
3980 switch(Algid)
3982 case CALG_AES_128:
3983 /* To reduce the chance of regressions we will only deviate
3984 * from the old behavior for the tested hash lengths */
3985 if (dwLen == 16 || dwLen == 20)
3987 need_padding = 1;
3988 break;
3990 default:
3991 need_padding = dwLen < pCryptKey->dwKeyLen;
3994 copy_len = pCryptKey->dwKeyLen;
3995 if (need_padding)
3997 BYTE pad1[RSAENH_HMAC_DEF_PAD_LEN], pad2[RSAENH_HMAC_DEF_PAD_LEN];
3998 BYTE old_hashval[RSAENH_MAX_HASH_SIZE];
3999 DWORD i;
4001 memcpy(old_hashval, pCryptHash->abHashValue, RSAENH_MAX_HASH_SIZE);
4003 for (i=0; i<RSAENH_HMAC_DEF_PAD_LEN; i++) {
4004 pad1[i] = RSAENH_HMAC_DEF_IPAD_CHAR ^ (i<dwLen ? abHashValue[i] : 0);
4005 pad2[i] = RSAENH_HMAC_DEF_OPAD_CHAR ^ (i<dwLen ? abHashValue[i] : 0);
4008 init_hash(pCryptHash);
4009 update_hash(pCryptHash, pad1, RSAENH_HMAC_DEF_PAD_LEN);
4010 finalize_hash(pCryptHash);
4011 memcpy(abHashValue, pCryptHash->abHashValue, pCryptHash->dwHashSize);
4013 init_hash(pCryptHash);
4014 update_hash(pCryptHash, pad2, RSAENH_HMAC_DEF_PAD_LEN);
4015 finalize_hash(pCryptHash);
4016 memcpy(abHashValue+pCryptHash->dwHashSize, pCryptHash->abHashValue,
4017 pCryptHash->dwHashSize);
4019 memcpy(pCryptHash->abHashValue, old_hashval, RSAENH_MAX_HASH_SIZE);
4022 * Padding was not required, we have more hash than needed.
4023 * Do we need to use the remaining hash as salt?
4025 else if((dwFlags & CRYPT_CREATE_SALT) &&
4026 (Algid == CALG_RC2 || Algid == CALG_RC4))
4028 copy_len += pCryptKey->dwSaltLen;
4031 memcpy(pCryptKey->abKeyValue, abHashValue,
4032 RSAENH_MIN(copy_len, sizeof(pCryptKey->abKeyValue)));
4033 break;
4035 case ALG_CLASS_MSG_ENCRYPT:
4036 if (!lookup_handle(&handle_table, pCryptHash->hKey, RSAENH_MAGIC_KEY,
4037 (OBJECTHDR**)&pMasterKey))
4039 SetLastError(NTE_FAIL); /* FIXME error code */
4040 return FALSE;
4043 switch (Algid)
4045 /* See RFC 2246, chapter 6.3 Key calculation */
4046 case CALG_SCHANNEL_ENC_KEY:
4047 if (!pMasterKey->siSChannelInfo.saEncAlg.Algid ||
4048 !pMasterKey->siSChannelInfo.saEncAlg.cBits)
4050 SetLastError(NTE_BAD_FLAGS);
4051 return FALSE;
4053 *phKey = new_key(hProv, pMasterKey->siSChannelInfo.saEncAlg.Algid,
4054 MAKELONG(LOWORD(dwFlags),pMasterKey->siSChannelInfo.saEncAlg.cBits),
4055 &pCryptKey);
4056 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) return FALSE;
4057 memcpy(pCryptKey->abKeyValue,
4058 pCryptHash->abHashValue + (
4059 2 * (pMasterKey->siSChannelInfo.saMACAlg.cBits / 8) +
4060 ((dwFlags & CRYPT_SERVER) ?
4061 (pMasterKey->siSChannelInfo.saEncAlg.cBits / 8) : 0)),
4062 pMasterKey->siSChannelInfo.saEncAlg.cBits / 8);
4063 memcpy(pCryptKey->abInitVector,
4064 pCryptHash->abHashValue + (
4065 2 * (pMasterKey->siSChannelInfo.saMACAlg.cBits / 8) +
4066 2 * (pMasterKey->siSChannelInfo.saEncAlg.cBits / 8) +
4067 ((dwFlags & CRYPT_SERVER) ? pCryptKey->dwBlockLen : 0)),
4068 pCryptKey->dwBlockLen);
4069 break;
4071 case CALG_SCHANNEL_MAC_KEY:
4072 *phKey = new_key(hProv, Algid,
4073 MAKELONG(LOWORD(dwFlags),pMasterKey->siSChannelInfo.saMACAlg.cBits),
4074 &pCryptKey);
4075 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) return FALSE;
4076 memcpy(pCryptKey->abKeyValue,
4077 pCryptHash->abHashValue + ((dwFlags & CRYPT_SERVER) ?
4078 pMasterKey->siSChannelInfo.saMACAlg.cBits / 8 : 0),
4079 pMasterKey->siSChannelInfo.saMACAlg.cBits / 8);
4080 break;
4082 default:
4083 SetLastError(NTE_BAD_ALGID);
4084 return FALSE;
4086 break;
4088 default:
4089 SetLastError(NTE_BAD_ALGID);
4090 return FALSE;
4093 setup_key(pCryptKey);
4094 return TRUE;
4097 /******************************************************************************
4098 * CPGetUserKey (RSAENH.@)
4100 * Returns a handle to the user's private key-exchange- or signature-key.
4102 * PARAMS
4103 * hProv [I] The key container from which a user key is requested.
4104 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
4105 * phUserKey [O] Handle to the requested key or INVALID_HANDLE_VALUE in case of failure.
4107 * RETURNS
4108 * Success: TRUE.
4109 * Failure: FALSE.
4111 * NOTE
4112 * A newly created key container does not contain private user key. Create them with CPGenKey.
4114 BOOL WINAPI RSAENH_CPGetUserKey(HCRYPTPROV hProv, DWORD dwKeySpec, HCRYPTKEY *phUserKey)
4116 KEYCONTAINER *pKeyContainer;
4118 TRACE("(hProv=%08lx, dwKeySpec=%08x, phUserKey=%p)\n", hProv, dwKeySpec, phUserKey);
4120 if (!(pKeyContainer = get_key_container(hProv)))
4122 /* MSDN: hProv not containing valid context handle */
4123 return FALSE;
4126 switch (dwKeySpec)
4128 case AT_KEYEXCHANGE:
4129 copy_handle(&handle_table, pKeyContainer->hKeyExchangeKeyPair, RSAENH_MAGIC_KEY,
4130 phUserKey);
4131 break;
4133 case AT_SIGNATURE:
4134 copy_handle(&handle_table, pKeyContainer->hSignatureKeyPair, RSAENH_MAGIC_KEY,
4135 phUserKey);
4136 break;
4138 default:
4139 *phUserKey = (HCRYPTKEY)INVALID_HANDLE_VALUE;
4142 if (*phUserKey == (HCRYPTKEY)INVALID_HANDLE_VALUE)
4144 /* MSDN: dwKeySpec parameter specifies nonexistent key */
4145 SetLastError(NTE_NO_KEY);
4146 return FALSE;
4149 return TRUE;
4152 /******************************************************************************
4153 * CPHashData (RSAENH.@)
4155 * Updates a hash object with the given data.
4157 * PARAMS
4158 * hProv [I] Key container to which the hash object belongs.
4159 * hHash [I] Hash object which is to be updated.
4160 * pbData [I] Pointer to data with which the hash object is to be updated.
4161 * dwDataLen [I] Length of the data.
4162 * dwFlags [I] Currently none defined.
4164 * RETURNS
4165 * Success: TRUE.
4166 * Failure: FALSE.
4168 * NOTES
4169 * The actual hash value is queried with CPGetHashParam, which will finalize
4170 * the hash. Updating a finalized hash will fail with a last error NTE_BAD_HASH_STATE.
4172 BOOL WINAPI RSAENH_CPHashData(HCRYPTPROV hProv, HCRYPTHASH hHash, const BYTE *pbData,
4173 DWORD dwDataLen, DWORD dwFlags)
4175 CRYPTHASH *pCryptHash;
4177 TRACE("(hProv=%08lx, hHash=%08lx, pbData=%p, dwDataLen=%d, dwFlags=%08x)\n",
4178 hProv, hHash, pbData, dwDataLen, dwFlags);
4180 if (dwFlags & ~CRYPT_USERDATA)
4182 SetLastError(NTE_BAD_FLAGS);
4183 return FALSE;
4186 if (!lookup_handle(&handle_table, hHash, RSAENH_MAGIC_HASH,
4187 (OBJECTHDR**)&pCryptHash))
4189 SetLastError(NTE_BAD_HASH);
4190 return FALSE;
4193 if (!get_algid_info(hProv, pCryptHash->aiAlgid) || pCryptHash->aiAlgid == CALG_SSL3_SHAMD5)
4195 SetLastError(NTE_BAD_ALGID);
4196 return FALSE;
4199 if (pCryptHash->dwState != RSAENH_HASHSTATE_HASHING)
4201 SetLastError(NTE_BAD_HASH_STATE);
4202 return FALSE;
4205 update_hash(pCryptHash, pbData, dwDataLen);
4206 return TRUE;
4209 /******************************************************************************
4210 * CPHashSessionKey (RSAENH.@)
4212 * Updates a hash object with the binary representation of a symmetric key.
4214 * PARAMS
4215 * hProv [I] Key container to which the hash object belongs.
4216 * hHash [I] Hash object which is to be updated.
4217 * hKey [I] The symmetric key, whose binary value will be added to the hash.
4218 * dwFlags [I] CRYPT_LITTLE_ENDIAN, if the binary key value shall be interpreted as little endian.
4220 * RETURNS
4221 * Success: TRUE.
4222 * Failure: FALSE.
4224 BOOL WINAPI RSAENH_CPHashSessionKey(HCRYPTPROV hProv, HCRYPTHASH hHash, HCRYPTKEY hKey,
4225 DWORD dwFlags)
4227 BYTE abKeyValue[RSAENH_MAX_KEY_SIZE], bTemp;
4228 CRYPTKEY *pKey;
4229 DWORD i;
4231 TRACE("(hProv=%08lx, hHash=%08lx, hKey=%08lx, dwFlags=%08x)\n", hProv, hHash, hKey, dwFlags);
4233 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pKey) ||
4234 (GET_ALG_CLASS(pKey->aiAlgid) != ALG_CLASS_DATA_ENCRYPT))
4236 SetLastError(NTE_BAD_KEY);
4237 return FALSE;
4240 if (dwFlags & ~CRYPT_LITTLE_ENDIAN) {
4241 SetLastError(NTE_BAD_FLAGS);
4242 return FALSE;
4245 memcpy(abKeyValue, pKey->abKeyValue, pKey->dwKeyLen);
4246 if (!(dwFlags & CRYPT_LITTLE_ENDIAN)) {
4247 for (i=0; i<pKey->dwKeyLen/2; i++) {
4248 bTemp = abKeyValue[i];
4249 abKeyValue[i] = abKeyValue[pKey->dwKeyLen-i-1];
4250 abKeyValue[pKey->dwKeyLen-i-1] = bTemp;
4254 return RSAENH_CPHashData(hProv, hHash, abKeyValue, pKey->dwKeyLen, 0);
4257 /******************************************************************************
4258 * CPReleaseContext (RSAENH.@)
4260 * Release a key container.
4262 * PARAMS
4263 * hProv [I] Key container to be released.
4264 * dwFlags [I] Currently none defined.
4266 * RETURNS
4267 * Success: TRUE
4268 * Failure: FALSE
4270 BOOL WINAPI RSAENH_CPReleaseContext(HCRYPTPROV hProv, DWORD dwFlags)
4272 TRACE("(hProv=%08lx, dwFlags=%08x)\n", hProv, dwFlags);
4274 if (!release_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
4276 /* MSDN: hProv not containing valid context handle */
4277 SetLastError(NTE_BAD_UID);
4278 return FALSE;
4281 if (dwFlags) {
4282 SetLastError(NTE_BAD_FLAGS);
4283 return FALSE;
4286 return TRUE;
4289 /******************************************************************************
4290 * CPSetHashParam (RSAENH.@)
4292 * Set a parameter of a hash object
4294 * PARAMS
4295 * hProv [I] The key container to which the key belongs.
4296 * hHash [I] The hash object for which a parameter is to be set.
4297 * dwParam [I] Parameter type. See Notes.
4298 * pbData [I] Pointer to the parameter value.
4299 * dwFlags [I] Currently none defined.
4301 * RETURNS
4302 * Success: TRUE.
4303 * Failure: FALSE.
4305 * NOTES
4306 * Currently only the HP_HMAC_INFO dwParam type is defined.
4307 * The HMAC_INFO struct will be deep copied into the hash object.
4308 * See Internet RFC 2104 for details on the HMAC algorithm.
4310 BOOL WINAPI RSAENH_CPSetHashParam(HCRYPTPROV hProv, HCRYPTHASH hHash, DWORD dwParam,
4311 BYTE *pbData, DWORD dwFlags)
4313 CRYPTHASH *pCryptHash;
4314 CRYPTKEY *pCryptKey;
4315 DWORD i;
4317 TRACE("(hProv=%08lx, hHash=%08lx, dwParam=%08x, pbData=%p, dwFlags=%08x)\n",
4318 hProv, hHash, dwParam, pbData, dwFlags);
4320 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
4322 SetLastError(NTE_BAD_UID);
4323 return FALSE;
4326 if (dwFlags) {
4327 SetLastError(NTE_BAD_FLAGS);
4328 return FALSE;
4331 if (!lookup_handle(&handle_table, hHash, RSAENH_MAGIC_HASH,
4332 (OBJECTHDR**)&pCryptHash))
4334 SetLastError(NTE_BAD_HASH);
4335 return FALSE;
4338 switch (dwParam) {
4339 case HP_HMAC_INFO:
4340 free_hmac_info(pCryptHash->pHMACInfo);
4341 if (!copy_hmac_info(&pCryptHash->pHMACInfo, (PHMAC_INFO)pbData)) return FALSE;
4343 if (!lookup_handle(&handle_table, pCryptHash->hKey, RSAENH_MAGIC_KEY,
4344 (OBJECTHDR**)&pCryptKey))
4346 SetLastError(NTE_FAIL); /* FIXME: correct error code? */
4347 return FALSE;
4350 if (pCryptKey->aiAlgid == CALG_HMAC && !pCryptKey->dwKeyLen) {
4351 HCRYPTHASH hKeyHash;
4352 DWORD keyLen;
4354 if (!RSAENH_CPCreateHash(hProv, ((PHMAC_INFO)pbData)->HashAlgid, 0, 0,
4355 &hKeyHash))
4356 return FALSE;
4357 if (!RSAENH_CPHashData(hProv, hKeyHash, pCryptKey->blobHmacKey.pbData,
4358 pCryptKey->blobHmacKey.cbData, 0))
4360 RSAENH_CPDestroyHash(hProv, hKeyHash);
4361 return FALSE;
4363 keyLen = sizeof(pCryptKey->abKeyValue);
4364 if (!RSAENH_CPGetHashParam(hProv, hKeyHash, HP_HASHVAL, pCryptKey->abKeyValue,
4365 &keyLen, 0))
4367 RSAENH_CPDestroyHash(hProv, hKeyHash);
4368 return FALSE;
4370 pCryptKey->dwKeyLen = keyLen;
4371 RSAENH_CPDestroyHash(hProv, hKeyHash);
4373 for (i=0; i<RSAENH_MIN(pCryptKey->dwKeyLen,pCryptHash->pHMACInfo->cbInnerString); i++) {
4374 pCryptHash->pHMACInfo->pbInnerString[i] ^= pCryptKey->abKeyValue[i];
4376 for (i=0; i<RSAENH_MIN(pCryptKey->dwKeyLen,pCryptHash->pHMACInfo->cbOuterString); i++) {
4377 pCryptHash->pHMACInfo->pbOuterString[i] ^= pCryptKey->abKeyValue[i];
4380 init_hash(pCryptHash);
4381 return TRUE;
4383 case HP_HASHVAL:
4384 memcpy(pCryptHash->abHashValue, pbData, pCryptHash->dwHashSize);
4385 pCryptHash->dwState = RSAENH_HASHSTATE_FINISHED;
4386 return TRUE;
4388 case HP_TLS1PRF_SEED:
4389 return copy_data_blob(&pCryptHash->tpPRFParams.blobSeed, (PCRYPT_DATA_BLOB)pbData);
4391 case HP_TLS1PRF_LABEL:
4392 return copy_data_blob(&pCryptHash->tpPRFParams.blobLabel, (PCRYPT_DATA_BLOB)pbData);
4394 default:
4395 SetLastError(NTE_BAD_TYPE);
4396 return FALSE;
4400 /******************************************************************************
4401 * CPSetProvParam (RSAENH.@)
4403 BOOL WINAPI RSAENH_CPSetProvParam(HCRYPTPROV hProv, DWORD dwParam, BYTE *pbData, DWORD dwFlags)
4405 KEYCONTAINER *pKeyContainer;
4406 HKEY hKey;
4408 TRACE("(hProv=%08lx, dwParam=%08x, pbData=%p, dwFlags=%08x)\n", hProv, dwParam, pbData, dwFlags);
4410 if (!(pKeyContainer = get_key_container(hProv)))
4411 return FALSE;
4413 switch (dwParam)
4415 case PP_KEYSET_SEC_DESCR:
4417 SECURITY_DESCRIPTOR *sd = (SECURITY_DESCRIPTOR *)pbData;
4418 DWORD err, flags = (pKeyContainer->dwFlags & CRYPT_MACHINE_KEYSET);
4419 BOOL def, present;
4420 REGSAM access = WRITE_DAC | WRITE_OWNER | ACCESS_SYSTEM_SECURITY;
4421 PSID owner = NULL, group = NULL;
4422 PACL dacl = NULL, sacl = NULL;
4424 if (!open_container_key(pKeyContainer->szName, flags, access, &hKey))
4426 SetLastError(NTE_BAD_KEYSET);
4427 return FALSE;
4430 if ((dwFlags & OWNER_SECURITY_INFORMATION && !GetSecurityDescriptorOwner(sd, &owner, &def)) ||
4431 (dwFlags & GROUP_SECURITY_INFORMATION && !GetSecurityDescriptorGroup(sd, &group, &def)) ||
4432 (dwFlags & DACL_SECURITY_INFORMATION && !GetSecurityDescriptorDacl(sd, &present, &dacl, &def)) ||
4433 (dwFlags & SACL_SECURITY_INFORMATION && !GetSecurityDescriptorSacl(sd, &present, &sacl, &def)))
4435 RegCloseKey(hKey);
4436 return FALSE;
4439 err = SetSecurityInfo(hKey, SE_REGISTRY_KEY, dwFlags, owner, group, dacl, sacl);
4440 RegCloseKey(hKey);
4441 if (err)
4443 SetLastError(err);
4444 return FALSE;
4446 return TRUE;
4448 default:
4449 FIXME("unimplemented parameter %08x\n", dwParam);
4450 return FALSE;
4454 /******************************************************************************
4455 * CPSignHash (RSAENH.@)
4457 * Sign a hash object
4459 * PARAMS
4460 * hProv [I] The key container, to which the hash object belongs.
4461 * hHash [I] The hash object to be signed.
4462 * dwKeySpec [I] AT_SIGNATURE or AT_KEYEXCHANGE: Key used to generate the signature.
4463 * sDescription [I] Should be NULL for security reasons.
4464 * dwFlags [I] 0, CRYPT_NOHASHOID or CRYPT_X931_FORMAT: Format of the signature.
4465 * pbSignature [O] Buffer, to which the signature will be stored. May be NULL to query SigLen.
4466 * pdwSigLen [I/O] Size of the buffer (in), Length of the signature (out)
4468 * RETURNS
4469 * Success: TRUE
4470 * Failure: FALSE
4472 BOOL WINAPI RSAENH_CPSignHash(HCRYPTPROV hProv, HCRYPTHASH hHash, DWORD dwKeySpec,
4473 LPCWSTR sDescription, DWORD dwFlags, BYTE *pbSignature,
4474 DWORD *pdwSigLen)
4476 HCRYPTKEY hCryptKey = (HCRYPTKEY)INVALID_HANDLE_VALUE;
4477 CRYPTKEY *pCryptKey;
4478 DWORD dwHashLen;
4479 BYTE abHashValue[RSAENH_MAX_HASH_SIZE];
4480 ALG_ID aiAlgid;
4481 BOOL ret = FALSE;
4483 TRACE("(hProv=%08lx, hHash=%08lx, dwKeySpec=%08x, sDescription=%s, dwFlags=%08x, "
4484 "pbSignature=%p, pdwSigLen=%p)\n", hProv, hHash, dwKeySpec, debugstr_w(sDescription),
4485 dwFlags, pbSignature, pdwSigLen);
4487 if (dwFlags & ~(CRYPT_NOHASHOID|CRYPT_X931_FORMAT)) {
4488 SetLastError(NTE_BAD_FLAGS);
4489 return FALSE;
4492 if (!RSAENH_CPGetUserKey(hProv, dwKeySpec, &hCryptKey)) return FALSE;
4494 if (!lookup_handle(&handle_table, hCryptKey, RSAENH_MAGIC_KEY,
4495 (OBJECTHDR**)&pCryptKey))
4497 SetLastError(NTE_NO_KEY);
4498 goto out;
4501 if (!pbSignature) {
4502 *pdwSigLen = pCryptKey->dwKeyLen;
4503 ret = TRUE;
4504 goto out;
4506 if (pCryptKey->dwKeyLen > *pdwSigLen)
4508 SetLastError(ERROR_MORE_DATA);
4509 *pdwSigLen = pCryptKey->dwKeyLen;
4510 goto out;
4512 *pdwSigLen = pCryptKey->dwKeyLen;
4514 if (sDescription) {
4515 if (!RSAENH_CPHashData(hProv, hHash, (const BYTE*)sDescription,
4516 (DWORD)lstrlenW(sDescription)*sizeof(WCHAR), 0))
4518 goto out;
4522 dwHashLen = sizeof(DWORD);
4523 if (!RSAENH_CPGetHashParam(hProv, hHash, HP_ALGID, (BYTE*)&aiAlgid, &dwHashLen, 0)) goto out;
4525 dwHashLen = RSAENH_MAX_HASH_SIZE;
4526 if (!RSAENH_CPGetHashParam(hProv, hHash, HP_HASHVAL, abHashValue, &dwHashLen, 0)) goto out;
4529 if (!build_hash_signature(pbSignature, *pdwSigLen, aiAlgid, abHashValue, dwHashLen, dwFlags)) {
4530 goto out;
4533 ret = encrypt_block_impl(pCryptKey->aiAlgid, PK_PRIVATE, &pCryptKey->context, pbSignature, pbSignature, RSAENH_ENCRYPT);
4534 out:
4535 RSAENH_CPDestroyKey(hProv, hCryptKey);
4536 return ret;
4539 /******************************************************************************
4540 * CPVerifySignature (RSAENH.@)
4542 * Verify the signature of a hash object.
4544 * PARAMS
4545 * hProv [I] The key container, to which the hash belongs.
4546 * hHash [I] The hash for which the signature is verified.
4547 * pbSignature [I] The binary signature.
4548 * dwSigLen [I] Length of the signature BLOB.
4549 * hPubKey [I] Public key used to verify the signature.
4550 * sDescription [I] Should be NULL for security reasons.
4551 * dwFlags [I] 0, CRYPT_NOHASHOID or CRYPT_X931_FORMAT: Format of the signature.
4553 * RETURNS
4554 * Success: TRUE (Signature is valid)
4555 * Failure: FALSE (GetLastError() == NTE_BAD_SIGNATURE, if signature is invalid)
4557 BOOL WINAPI RSAENH_CPVerifySignature(HCRYPTPROV hProv, HCRYPTHASH hHash, const BYTE *pbSignature,
4558 DWORD dwSigLen, HCRYPTKEY hPubKey, LPCWSTR sDescription,
4559 DWORD dwFlags)
4561 BYTE *pbConstructed = NULL, *pbDecrypted = NULL;
4562 CRYPTKEY *pCryptKey;
4563 DWORD dwHashLen;
4564 ALG_ID aiAlgid;
4565 BYTE abHashValue[RSAENH_MAX_HASH_SIZE];
4566 BOOL res = FALSE;
4568 TRACE("(hProv=%08lx, hHash=%08lx, pbSignature=%p, dwSigLen=%d, hPubKey=%08lx, sDescription=%s, "
4569 "dwFlags=%08x)\n", hProv, hHash, pbSignature, dwSigLen, hPubKey, debugstr_w(sDescription),
4570 dwFlags);
4572 if (dwFlags & ~(CRYPT_NOHASHOID|CRYPT_X931_FORMAT)) {
4573 SetLastError(NTE_BAD_FLAGS);
4574 return FALSE;
4577 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
4579 SetLastError(NTE_BAD_UID);
4580 return FALSE;
4583 if (!lookup_handle(&handle_table, hPubKey, RSAENH_MAGIC_KEY,
4584 (OBJECTHDR**)&pCryptKey))
4586 SetLastError(NTE_BAD_KEY);
4587 return FALSE;
4590 /* in Microsoft implementation, the signature length is checked before
4591 * the signature pointer.
4593 if (dwSigLen != pCryptKey->dwKeyLen)
4595 SetLastError(NTE_BAD_SIGNATURE);
4596 return FALSE;
4599 if (!hHash || !pbSignature)
4601 SetLastError(ERROR_INVALID_PARAMETER);
4602 return FALSE;
4605 if (sDescription) {
4606 if (!RSAENH_CPHashData(hProv, hHash, (const BYTE*)sDescription,
4607 (DWORD)lstrlenW(sDescription)*sizeof(WCHAR), 0))
4609 return FALSE;
4613 dwHashLen = sizeof(DWORD);
4614 if (!RSAENH_CPGetHashParam(hProv, hHash, HP_ALGID, (BYTE*)&aiAlgid, &dwHashLen, 0)) return FALSE;
4616 dwHashLen = RSAENH_MAX_HASH_SIZE;
4617 if (!RSAENH_CPGetHashParam(hProv, hHash, HP_HASHVAL, abHashValue, &dwHashLen, 0)) return FALSE;
4619 pbConstructed = HeapAlloc(GetProcessHeap(), 0, dwSigLen);
4620 if (!pbConstructed) {
4621 SetLastError(NTE_NO_MEMORY);
4622 goto cleanup;
4625 pbDecrypted = HeapAlloc(GetProcessHeap(), 0, dwSigLen);
4626 if (!pbDecrypted) {
4627 SetLastError(NTE_NO_MEMORY);
4628 goto cleanup;
4631 if (!encrypt_block_impl(pCryptKey->aiAlgid, PK_PUBLIC, &pCryptKey->context, pbSignature, pbDecrypted,
4632 RSAENH_DECRYPT))
4634 goto cleanup;
4637 if (build_hash_signature(pbConstructed, dwSigLen, aiAlgid, abHashValue, dwHashLen, dwFlags) &&
4638 !memcmp(pbDecrypted, pbConstructed, dwSigLen)) {
4639 res = TRUE;
4640 goto cleanup;
4643 if (!(dwFlags & CRYPT_NOHASHOID) &&
4644 build_hash_signature(pbConstructed, dwSigLen, aiAlgid, abHashValue, dwHashLen, dwFlags|CRYPT_NOHASHOID) &&
4645 !memcmp(pbDecrypted, pbConstructed, dwSigLen)) {
4646 res = TRUE;
4647 goto cleanup;
4650 SetLastError(NTE_BAD_SIGNATURE);
4652 cleanup:
4653 HeapFree(GetProcessHeap(), 0, pbConstructed);
4654 HeapFree(GetProcessHeap(), 0, pbDecrypted);
4655 return res;
4658 /******************************************************************************
4659 * DllRegisterServer (RSAENH.@)
4661 HRESULT WINAPI DllRegisterServer(void)
4663 return __wine_register_resources( instance );
4666 /******************************************************************************
4667 * DllUnregisterServer (RSAENH.@)
4669 HRESULT WINAPI DllUnregisterServer(void)
4671 return __wine_unregister_resources( instance );