usp10: Handle Ligature Substitution Subtable from GSUB.
[wine/testsucceed.git] / dlls / rsaenh / rsaenh.c
blob89bb78616604dea1e93bfff17c30996943501d5e
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"
41 WINE_DEFAULT_DEBUG_CHANNEL(crypt);
43 /******************************************************************************
44 * CRYPTHASH - hash objects
46 #define RSAENH_MAGIC_HASH 0x85938417u
47 #define RSAENH_MAX_HASH_SIZE 104
48 #define RSAENH_HASHSTATE_HASHING 1
49 #define RSAENH_HASHSTATE_FINISHED 2
50 typedef struct _RSAENH_TLS1PRF_PARAMS
52 CRYPT_DATA_BLOB blobLabel;
53 CRYPT_DATA_BLOB blobSeed;
54 } RSAENH_TLS1PRF_PARAMS;
56 typedef struct tagCRYPTHASH
58 OBJECTHDR header;
59 ALG_ID aiAlgid;
60 HCRYPTKEY hKey;
61 HCRYPTPROV hProv;
62 DWORD dwHashSize;
63 DWORD dwState;
64 HASH_CONTEXT context;
65 BYTE abHashValue[RSAENH_MAX_HASH_SIZE];
66 PHMAC_INFO pHMACInfo;
67 RSAENH_TLS1PRF_PARAMS tpPRFParams;
68 } CRYPTHASH;
70 /******************************************************************************
71 * CRYPTKEY - key objects
73 #define RSAENH_MAGIC_KEY 0x73620457u
74 #define RSAENH_MAX_KEY_SIZE 48
75 #define RSAENH_MAX_BLOCK_SIZE 24
76 #define RSAENH_KEYSTATE_IDLE 0
77 #define RSAENH_KEYSTATE_ENCRYPTING 1
78 #define RSAENH_KEYSTATE_MASTERKEY 2
79 typedef struct _RSAENH_SCHANNEL_INFO
81 SCHANNEL_ALG saEncAlg;
82 SCHANNEL_ALG saMACAlg;
83 CRYPT_DATA_BLOB blobClientRandom;
84 CRYPT_DATA_BLOB blobServerRandom;
85 } RSAENH_SCHANNEL_INFO;
87 typedef struct tagCRYPTKEY
89 OBJECTHDR header;
90 ALG_ID aiAlgid;
91 HCRYPTPROV hProv;
92 DWORD dwMode;
93 DWORD dwModeBits;
94 DWORD dwPermissions;
95 DWORD dwKeyLen;
96 DWORD dwEffectiveKeyLen;
97 DWORD dwSaltLen;
98 DWORD dwBlockLen;
99 DWORD dwState;
100 KEY_CONTEXT context;
101 BYTE abKeyValue[RSAENH_MAX_KEY_SIZE];
102 BYTE abInitVector[RSAENH_MAX_BLOCK_SIZE];
103 BYTE abChainVector[RSAENH_MAX_BLOCK_SIZE];
104 RSAENH_SCHANNEL_INFO siSChannelInfo;
105 } CRYPTKEY;
107 /******************************************************************************
108 * KEYCONTAINER - key containers
110 #define RSAENH_PERSONALITY_BASE 0u
111 #define RSAENH_PERSONALITY_STRONG 1u
112 #define RSAENH_PERSONALITY_ENHANCED 2u
113 #define RSAENH_PERSONALITY_SCHANNEL 3u
114 #define RSAENH_PERSONALITY_AES 4u
116 #define RSAENH_MAGIC_CONTAINER 0x26384993u
117 typedef struct tagKEYCONTAINER
119 OBJECTHDR header;
120 DWORD dwFlags;
121 DWORD dwPersonality;
122 DWORD dwEnumAlgsCtr;
123 DWORD dwEnumContainersCtr;
124 CHAR szName[MAX_PATH];
125 CHAR szProvName[MAX_PATH];
126 HCRYPTKEY hKeyExchangeKeyPair;
127 HCRYPTKEY hSignatureKeyPair;
128 } KEYCONTAINER;
130 /******************************************************************************
131 * Some magic constants
133 #define RSAENH_ENCRYPT 1
134 #define RSAENH_DECRYPT 0
135 #define RSAENH_HMAC_DEF_IPAD_CHAR 0x36
136 #define RSAENH_HMAC_DEF_OPAD_CHAR 0x5c
137 #define RSAENH_HMAC_DEF_PAD_LEN 64
138 #define RSAENH_DES_EFFECTIVE_KEYLEN 56
139 #define RSAENH_DES_STORAGE_KEYLEN 64
140 #define RSAENH_3DES112_EFFECTIVE_KEYLEN 112
141 #define RSAENH_3DES112_STORAGE_KEYLEN 128
142 #define RSAENH_3DES_EFFECTIVE_KEYLEN 168
143 #define RSAENH_3DES_STORAGE_KEYLEN 192
144 #define RSAENH_MAGIC_RSA2 0x32415352
145 #define RSAENH_MAGIC_RSA1 0x31415352
146 #define RSAENH_PKC_BLOCKTYPE 0x02
147 #define RSAENH_SSL3_VERSION_MAJOR 3
148 #define RSAENH_SSL3_VERSION_MINOR 0
149 #define RSAENH_TLS1_VERSION_MAJOR 3
150 #define RSAENH_TLS1_VERSION_MINOR 1
151 #define RSAENH_REGKEY "Software\\Wine\\Crypto\\RSA\\%s"
153 #define RSAENH_MIN(a,b) ((a)<(b)?(a):(b))
154 /******************************************************************************
155 * aProvEnumAlgsEx - Defines the capabilities of the CSP personalities.
157 #define RSAENH_MAX_ENUMALGS 24
158 #define RSAENH_PCT1_SSL2_SSL3_TLS1 (CRYPT_FLAG_PCT1|CRYPT_FLAG_SSL2|CRYPT_FLAG_SSL3|CRYPT_FLAG_TLS1)
159 static const PROV_ENUMALGS_EX aProvEnumAlgsEx[5][RSAENH_MAX_ENUMALGS+1] =
162 {CALG_RC2, 40, 40, 56,0, 4,"RC2", 24,"RSA Data Security's RC2"},
163 {CALG_RC4, 40, 40, 56,0, 4,"RC4", 24,"RSA Data Security's RC4"},
164 {CALG_DES, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
165 {CALG_SHA, 160,160, 160,CRYPT_FLAG_SIGNING, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
166 {CALG_MD2, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD2", 23,"Message Digest 2 (MD2)"},
167 {CALG_MD4, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD4", 23,"Message Digest 4 (MD4)"},
168 {CALG_MD5, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD5", 23,"Message Digest 5 (MD5)"},
169 {CALG_SSL3_SHAMD5,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
170 {CALG_MAC, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
171 {CALG_RSA_SIGN, 512,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_SIGN",14,"RSA Signature"},
172 {CALG_RSA_KEYX, 512,384, 1024,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_KEYX",17,"RSA Key Exchange"},
173 {CALG_HMAC, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
174 {0, 0, 0, 0,0, 1,"", 1,""}
177 {CALG_RC2, 128, 40, 128,0, 4,"RC2", 24,"RSA Data Security's RC2"},
178 {CALG_RC4, 128, 40, 128,0, 4,"RC4", 24,"RSA Data Security's RC4"},
179 {CALG_DES, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
180 {CALG_3DES_112, 112,112, 112,0, 13,"3DES TWO KEY",19,"Two Key Triple DES"},
181 {CALG_3DES, 168,168, 168,0, 5,"3DES", 21,"Three Key Triple DES"},
182 {CALG_SHA, 160,160, 160,CRYPT_FLAG_SIGNING, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
183 {CALG_MD2, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD2", 23,"Message Digest 2 (MD2)"},
184 {CALG_MD4, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD4", 23,"Message Digest 4 (MD4)"},
185 {CALG_MD5, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD5", 23,"Message Digest 5 (MD5)"},
186 {CALG_SSL3_SHAMD5,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
187 {CALG_MAC, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
188 {CALG_RSA_SIGN,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_SIGN",14,"RSA Signature"},
189 {CALG_RSA_KEYX,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_KEYX",17,"RSA Key Exchange"},
190 {CALG_HMAC, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
191 {0, 0, 0, 0,0, 1,"", 1,""}
194 {CALG_RC2, 128, 40, 128,0, 4,"RC2", 24,"RSA Data Security's RC2"},
195 {CALG_RC4, 128, 40, 128,0, 4,"RC4", 24,"RSA Data Security's RC4"},
196 {CALG_DES, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
197 {CALG_3DES_112, 112,112, 112,0, 13,"3DES TWO KEY",19,"Two Key Triple DES"},
198 {CALG_3DES, 168,168, 168,0, 5,"3DES", 21,"Three Key Triple DES"},
199 {CALG_SHA, 160,160, 160,CRYPT_FLAG_SIGNING, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
200 {CALG_MD2, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD2", 23,"Message Digest 2 (MD2)"},
201 {CALG_MD4, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD4", 23,"Message Digest 4 (MD4)"},
202 {CALG_MD5, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD5", 23,"Message Digest 5 (MD5)"},
203 {CALG_SSL3_SHAMD5,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
204 {CALG_MAC, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
205 {CALG_RSA_SIGN,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_SIGN",14,"RSA Signature"},
206 {CALG_RSA_KEYX,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_KEYX",17,"RSA Key Exchange"},
207 {CALG_HMAC, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
208 {0, 0, 0, 0,0, 1,"", 1,""}
211 {CALG_RC2, 128, 40, 128,RSAENH_PCT1_SSL2_SSL3_TLS1, 4,"RC2", 24,"RSA Data Security's RC2"},
212 {CALG_RC4, 128, 40, 128,RSAENH_PCT1_SSL2_SSL3_TLS1, 4,"RC4", 24,"RSA Data Security's RC4"},
213 {CALG_DES, 56, 56, 56,RSAENH_PCT1_SSL2_SSL3_TLS1, 4,"DES", 31,"Data Encryption Standard (DES)"},
214 {CALG_3DES_112, 112,112, 112,RSAENH_PCT1_SSL2_SSL3_TLS1,13,"3DES TWO KEY",19,"Two Key Triple DES"},
215 {CALG_3DES, 168,168, 168,RSAENH_PCT1_SSL2_SSL3_TLS1, 5,"3DES", 21,"Three Key Triple DES"},
216 {CALG_SHA,160,160,160,CRYPT_FLAG_SIGNING|RSAENH_PCT1_SSL2_SSL3_TLS1,6,"SHA-1",30,"Secure Hash Algorithm (SHA-1)"},
217 {CALG_MD5,128,128,128,CRYPT_FLAG_SIGNING|RSAENH_PCT1_SSL2_SSL3_TLS1,4,"MD5",23,"Message Digest 5 (MD5)"},
218 {CALG_SSL3_SHAMD5,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
219 {CALG_MAC, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
220 {CALG_RSA_SIGN,1024,384,16384,CRYPT_FLAG_SIGNING|RSAENH_PCT1_SSL2_SSL3_TLS1,9,"RSA_SIGN",14,"RSA Signature"},
221 {CALG_RSA_KEYX,1024,384,16384,CRYPT_FLAG_SIGNING|RSAENH_PCT1_SSL2_SSL3_TLS1,9,"RSA_KEYX",17,"RSA Key Exchange"},
222 {CALG_HMAC, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
223 {CALG_PCT1_MASTER,128,128,128,CRYPT_FLAG_PCT1, 12,"PCT1 MASTER",12,"PCT1 Master"},
224 {CALG_SSL2_MASTER,40,40, 192,CRYPT_FLAG_SSL2, 12,"SSL2 MASTER",12,"SSL2 Master"},
225 {CALG_SSL3_MASTER,384,384,384,CRYPT_FLAG_SSL3, 12,"SSL3 MASTER",12,"SSL3 Master"},
226 {CALG_TLS1_MASTER,384,384,384,CRYPT_FLAG_TLS1, 12,"TLS1 MASTER",12,"TLS1 Master"},
227 {CALG_SCHANNEL_MASTER_HASH,0,0,-1,0, 16,"SCH MASTER HASH",21,"SChannel Master Hash"},
228 {CALG_SCHANNEL_MAC_KEY,0,0,-1,0, 12,"SCH MAC KEY",17,"SChannel MAC Key"},
229 {CALG_SCHANNEL_ENC_KEY,0,0,-1,0, 12,"SCH ENC KEY",24,"SChannel Encryption Key"},
230 {CALG_TLS1PRF, 0, 0, -1,0, 9,"TLS1 PRF", 28,"TLS1 Pseudo Random Function"},
231 {0, 0, 0, 0,0, 1,"", 1,""}
234 {CALG_RC2, 128, 40, 128,0, 4,"RC2", 24,"RSA Data Security's RC2"},
235 {CALG_RC4, 128, 40, 128,0, 4,"RC4", 24,"RSA Data Security's RC4"},
236 {CALG_DES, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
237 {CALG_3DES_112, 112,112, 112,0, 13,"3DES TWO KEY",19,"Two Key Triple DES"},
238 {CALG_3DES, 168,168, 168,0, 5,"3DES", 21,"Three Key Triple DES"},
239 {CALG_AES, 128,128, 128,0, 4,"AES", 35,"Advanced Encryption Standard (AES)"},
240 {CALG_AES_128, 128,128, 128,0, 8,"AES-128", 39,"Advanced Encryption Standard (AES-128)"},
241 {CALG_AES_192, 192,192, 192,0, 8,"AES-192", 39,"Advanced Encryption Standard (AES-192)"},
242 {CALG_AES_256, 256,256, 256,0, 8,"AES-256", 39,"Advanced Encryption Standard (AES-256)"},
243 {CALG_SHA, 160,160, 160,CRYPT_FLAG_SIGNING, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
244 {CALG_SHA_256, 256,256, 256,CRYPT_FLAG_SIGNING, 6,"SHA-256", 30,"Secure Hash Algorithm (SHA-256)"},
245 {CALG_SHA_384, 384,384, 384,CRYPT_FLAG_SIGNING, 6,"SHA-384", 30,"Secure Hash Algorithm (SHA-284)"},
246 {CALG_SHA_512, 512,512, 512,CRYPT_FLAG_SIGNING, 6,"SHA-512", 30,"Secure Hash Algorithm (SHA-512)"},
247 {CALG_MD2, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD2", 23,"Message Digest 2 (MD2)"},
248 {CALG_MD4, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD4", 23,"Message Digest 4 (MD4)"},
249 {CALG_MD5, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD5", 23,"Message Digest 5 (MD5)"},
250 {CALG_SSL3_SHAMD5,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
251 {CALG_MAC, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
252 {CALG_RSA_SIGN,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_SIGN",14,"RSA Signature"},
253 {CALG_RSA_KEYX,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_KEYX",17,"RSA Key Exchange"},
254 {CALG_HMAC, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
255 {0, 0, 0, 0,0, 1,"", 1,""}
259 /******************************************************************************
260 * API forward declarations
262 BOOL WINAPI
263 RSAENH_CPGetKeyParam(
264 HCRYPTPROV hProv,
265 HCRYPTKEY hKey,
266 DWORD dwParam,
267 BYTE *pbData,
268 DWORD *pdwDataLen,
269 DWORD dwFlags
272 BOOL WINAPI
273 RSAENH_CPEncrypt(
274 HCRYPTPROV hProv,
275 HCRYPTKEY hKey,
276 HCRYPTHASH hHash,
277 BOOL Final,
278 DWORD dwFlags,
279 BYTE *pbData,
280 DWORD *pdwDataLen,
281 DWORD dwBufLen
284 BOOL WINAPI
285 RSAENH_CPCreateHash(
286 HCRYPTPROV hProv,
287 ALG_ID Algid,
288 HCRYPTKEY hKey,
289 DWORD dwFlags,
290 HCRYPTHASH *phHash
293 BOOL WINAPI
294 RSAENH_CPSetHashParam(
295 HCRYPTPROV hProv,
296 HCRYPTHASH hHash,
297 DWORD dwParam,
298 BYTE *pbData, DWORD dwFlags
301 BOOL WINAPI
302 RSAENH_CPGetHashParam(
303 HCRYPTPROV hProv,
304 HCRYPTHASH hHash,
305 DWORD dwParam,
306 BYTE *pbData,
307 DWORD *pdwDataLen,
308 DWORD dwFlags
311 BOOL WINAPI
312 RSAENH_CPDestroyHash(
313 HCRYPTPROV hProv,
314 HCRYPTHASH hHash
317 static BOOL crypt_export_key(
318 CRYPTKEY *pCryptKey,
319 HCRYPTKEY hPubKey,
320 DWORD dwBlobType,
321 DWORD dwFlags,
322 BOOL force,
323 BYTE *pbData,
324 DWORD *pdwDataLen
327 static BOOL import_key(
328 HCRYPTPROV hProv,
329 CONST BYTE *pbData,
330 DWORD dwDataLen,
331 HCRYPTKEY hPubKey,
332 DWORD dwFlags,
333 BOOL fStoreKey,
334 HCRYPTKEY *phKey
337 BOOL WINAPI
338 RSAENH_CPHashData(
339 HCRYPTPROV hProv,
340 HCRYPTHASH hHash,
341 CONST BYTE *pbData,
342 DWORD dwDataLen,
343 DWORD dwFlags
346 /******************************************************************************
347 * CSP's handle table (used by all acquired key containers)
349 static struct handle_table handle_table;
351 /******************************************************************************
352 * DllMain (RSAENH.@)
354 * Initializes and destroys the handle table for the CSP's handles.
356 int WINAPI DllMain(HINSTANCE hInstance, DWORD fdwReason, PVOID pvReserved)
358 switch (fdwReason)
360 case DLL_PROCESS_ATTACH:
361 DisableThreadLibraryCalls(hInstance);
362 init_handle_table(&handle_table);
363 break;
365 case DLL_PROCESS_DETACH:
366 destroy_handle_table(&handle_table);
367 break;
369 return 1;
372 /******************************************************************************
373 * copy_param [Internal]
375 * Helper function that supports the standard WINAPI protocol for querying data
376 * of dynamic size.
378 * PARAMS
379 * pbBuffer [O] Buffer where the queried parameter is copied to, if it is large enough.
380 * May be NUL if the required buffer size is to be queried only.
381 * pdwBufferSize [I/O] In: Size of the buffer at pbBuffer
382 * Out: Size of parameter pbParam
383 * pbParam [I] Parameter value.
384 * dwParamSize [I] Size of pbParam
386 * RETURN
387 * Success: TRUE (pbParam was copied into pbBuffer or pbBuffer is NULL)
388 * Failure: FALSE (pbBuffer is not large enough to hold pbParam). Last error: ERROR_MORE_DATA
390 static inline BOOL copy_param(
391 BYTE *pbBuffer, DWORD *pdwBufferSize, CONST BYTE *pbParam, DWORD dwParamSize)
393 if (pbBuffer)
395 if (dwParamSize > *pdwBufferSize)
397 SetLastError(ERROR_MORE_DATA);
398 *pdwBufferSize = dwParamSize;
399 return FALSE;
401 memcpy(pbBuffer, pbParam, dwParamSize);
403 *pdwBufferSize = dwParamSize;
404 return TRUE;
407 /******************************************************************************
408 * get_algid_info [Internal]
410 * Query CSP capabilities for a given crypto algorithm.
412 * PARAMS
413 * hProv [I] Handle to a key container of the CSP whose capabilities are to be queried.
414 * algid [I] Identifier of the crypto algorithm about which information is requested.
416 * RETURNS
417 * Success: Pointer to a PROV_ENUMALGS_EX struct containing information about the crypto algorithm.
418 * Failure: NULL (algid not supported)
420 static inline const PROV_ENUMALGS_EX* get_algid_info(HCRYPTPROV hProv, ALG_ID algid) {
421 const PROV_ENUMALGS_EX *iterator;
422 KEYCONTAINER *pKeyContainer;
424 if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER, (OBJECTHDR**)&pKeyContainer)) {
425 SetLastError(NTE_BAD_UID);
426 return NULL;
429 for (iterator = aProvEnumAlgsEx[pKeyContainer->dwPersonality]; iterator->aiAlgid; iterator++) {
430 if (iterator->aiAlgid == algid) return iterator;
433 SetLastError(NTE_BAD_ALGID);
434 return NULL;
437 /******************************************************************************
438 * copy_data_blob [Internal]
440 * deeply copies a DATA_BLOB
442 * PARAMS
443 * dst [O] That's where the blob will be copied to
444 * src [I] Source blob
446 * RETURNS
447 * Success: TRUE
448 * Failure: FALSE (GetLastError() == NTE_NO_MEMORY
450 * NOTES
451 * Use free_data_blob to release resources occupied by copy_data_blob.
453 static inline BOOL copy_data_blob(PCRYPT_DATA_BLOB dst, CONST PCRYPT_DATA_BLOB src) {
454 dst->pbData = HeapAlloc(GetProcessHeap(), 0, src->cbData);
455 if (!dst->pbData) {
456 SetLastError(NTE_NO_MEMORY);
457 return FALSE;
459 dst->cbData = src->cbData;
460 memcpy(dst->pbData, src->pbData, src->cbData);
461 return TRUE;
464 /******************************************************************************
465 * concat_data_blobs [Internal]
467 * Concatenates two blobs
469 * PARAMS
470 * dst [O] The new blob will be copied here
471 * src1 [I] Prefix blob
472 * src2 [I] Appendix blob
474 * RETURNS
475 * Success: TRUE
476 * Failure: FALSE (GetLastError() == NTE_NO_MEMORY)
478 * NOTES
479 * Release resources occupied by concat_data_blobs with free_data_blobs
481 static inline BOOL concat_data_blobs(PCRYPT_DATA_BLOB dst, CONST PCRYPT_DATA_BLOB src1,
482 CONST PCRYPT_DATA_BLOB src2)
484 dst->cbData = src1->cbData + src2->cbData;
485 dst->pbData = HeapAlloc(GetProcessHeap(), 0, dst->cbData);
486 if (!dst->pbData) {
487 SetLastError(NTE_NO_MEMORY);
488 return FALSE;
490 memcpy(dst->pbData, src1->pbData, src1->cbData);
491 memcpy(dst->pbData + src1->cbData, src2->pbData, src2->cbData);
492 return TRUE;
495 /******************************************************************************
496 * free_data_blob [Internal]
498 * releases resource occupied by a dynamically allocated CRYPT_DATA_BLOB
500 * PARAMS
501 * pBlob [I] Heap space occupied by pBlob->pbData is released
503 static inline void free_data_blob(PCRYPT_DATA_BLOB pBlob) {
504 HeapFree(GetProcessHeap(), 0, pBlob->pbData);
507 /******************************************************************************
508 * init_data_blob [Internal]
510 static inline void init_data_blob(PCRYPT_DATA_BLOB pBlob) {
511 pBlob->pbData = NULL;
512 pBlob->cbData = 0;
515 /******************************************************************************
516 * free_hmac_info [Internal]
518 * Deeply free an HMAC_INFO struct.
520 * PARAMS
521 * hmac_info [I] Pointer to the HMAC_INFO struct to be freed.
523 * NOTES
524 * See Internet RFC 2104 for details on the HMAC algorithm.
526 static inline void free_hmac_info(PHMAC_INFO hmac_info) {
527 if (!hmac_info) return;
528 HeapFree(GetProcessHeap(), 0, hmac_info->pbInnerString);
529 HeapFree(GetProcessHeap(), 0, hmac_info->pbOuterString);
530 HeapFree(GetProcessHeap(), 0, hmac_info);
533 /******************************************************************************
534 * copy_hmac_info [Internal]
536 * Deeply copy an HMAC_INFO struct
538 * PARAMS
539 * dst [O] Pointer to a location where the pointer to the HMAC_INFO copy will be stored.
540 * src [I] Pointer to the HMAC_INFO struct to be copied.
542 * RETURNS
543 * Success: TRUE
544 * Failure: FALSE
546 * NOTES
547 * See Internet RFC 2104 for details on the HMAC algorithm.
549 static BOOL copy_hmac_info(PHMAC_INFO *dst, const HMAC_INFO *src) {
550 if (!src) return FALSE;
551 *dst = HeapAlloc(GetProcessHeap(), 0, sizeof(HMAC_INFO));
552 if (!*dst) return FALSE;
553 **dst = *src;
554 (*dst)->pbInnerString = NULL;
555 (*dst)->pbOuterString = NULL;
556 if ((*dst)->cbInnerString == 0) (*dst)->cbInnerString = RSAENH_HMAC_DEF_PAD_LEN;
557 (*dst)->pbInnerString = HeapAlloc(GetProcessHeap(), 0, (*dst)->cbInnerString);
558 if (!(*dst)->pbInnerString) {
559 free_hmac_info(*dst);
560 return FALSE;
562 if (src->cbInnerString)
563 memcpy((*dst)->pbInnerString, src->pbInnerString, src->cbInnerString);
564 else
565 memset((*dst)->pbInnerString, RSAENH_HMAC_DEF_IPAD_CHAR, RSAENH_HMAC_DEF_PAD_LEN);
566 if ((*dst)->cbOuterString == 0) (*dst)->cbOuterString = RSAENH_HMAC_DEF_PAD_LEN;
567 (*dst)->pbOuterString = HeapAlloc(GetProcessHeap(), 0, (*dst)->cbOuterString);
568 if (!(*dst)->pbOuterString) {
569 free_hmac_info(*dst);
570 return FALSE;
572 if (src->cbOuterString)
573 memcpy((*dst)->pbOuterString, src->pbOuterString, src->cbOuterString);
574 else
575 memset((*dst)->pbOuterString, RSAENH_HMAC_DEF_OPAD_CHAR, RSAENH_HMAC_DEF_PAD_LEN);
576 return TRUE;
579 /******************************************************************************
580 * destroy_hash [Internal]
582 * Destructor for hash objects
584 * PARAMS
585 * pCryptHash [I] Pointer to the hash object to be destroyed.
586 * Will be invalid after function returns!
588 static void destroy_hash(OBJECTHDR *pObject)
590 CRYPTHASH *pCryptHash = (CRYPTHASH*)pObject;
592 free_hmac_info(pCryptHash->pHMACInfo);
593 free_data_blob(&pCryptHash->tpPRFParams.blobLabel);
594 free_data_blob(&pCryptHash->tpPRFParams.blobSeed);
595 HeapFree(GetProcessHeap(), 0, pCryptHash);
598 /******************************************************************************
599 * init_hash [Internal]
601 * Initialize (or reset) a hash object
603 * PARAMS
604 * pCryptHash [I] The hash object to be initialized.
606 static inline BOOL init_hash(CRYPTHASH *pCryptHash) {
607 DWORD dwLen;
609 switch (pCryptHash->aiAlgid)
611 case CALG_HMAC:
612 if (pCryptHash->pHMACInfo) {
613 const PROV_ENUMALGS_EX *pAlgInfo;
615 pAlgInfo = get_algid_info(pCryptHash->hProv, pCryptHash->pHMACInfo->HashAlgid);
616 if (!pAlgInfo) return FALSE;
617 pCryptHash->dwHashSize = pAlgInfo->dwDefaultLen >> 3;
618 init_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context);
619 update_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
620 pCryptHash->pHMACInfo->pbInnerString,
621 pCryptHash->pHMACInfo->cbInnerString);
623 return TRUE;
625 case CALG_MAC:
626 dwLen = sizeof(DWORD);
627 RSAENH_CPGetKeyParam(pCryptHash->hProv, pCryptHash->hKey, KP_BLOCKLEN,
628 (BYTE*)&pCryptHash->dwHashSize, &dwLen, 0);
629 pCryptHash->dwHashSize >>= 3;
630 return TRUE;
632 default:
633 return init_hash_impl(pCryptHash->aiAlgid, &pCryptHash->context);
637 /******************************************************************************
638 * update_hash [Internal]
640 * Hashes the given data and updates the hash object's state accordingly
642 * PARAMS
643 * pCryptHash [I] Hash object to be updated.
644 * pbData [I] Pointer to data stream to be hashed.
645 * dwDataLen [I] Length of data stream.
647 static inline void update_hash(CRYPTHASH *pCryptHash, CONST BYTE *pbData, DWORD dwDataLen) {
648 BYTE *pbTemp;
650 switch (pCryptHash->aiAlgid)
652 case CALG_HMAC:
653 if (pCryptHash->pHMACInfo)
654 update_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
655 pbData, dwDataLen);
656 break;
658 case CALG_MAC:
659 pbTemp = HeapAlloc(GetProcessHeap(), 0, dwDataLen);
660 if (!pbTemp) return;
661 memcpy(pbTemp, pbData, dwDataLen);
662 RSAENH_CPEncrypt(pCryptHash->hProv, pCryptHash->hKey, 0, FALSE, 0,
663 pbTemp, &dwDataLen, dwDataLen);
664 HeapFree(GetProcessHeap(), 0, pbTemp);
665 break;
667 default:
668 update_hash_impl(pCryptHash->aiAlgid, &pCryptHash->context, pbData, dwDataLen);
672 /******************************************************************************
673 * finalize_hash [Internal]
675 * Finalizes the hash, after all data has been hashed with update_hash.
676 * No additional data can be hashed afterwards until the hash gets initialized again.
678 * PARAMS
679 * pCryptHash [I] Hash object to be finalized.
681 static inline void finalize_hash(CRYPTHASH *pCryptHash) {
682 DWORD dwDataLen;
684 switch (pCryptHash->aiAlgid)
686 case CALG_HMAC:
687 if (pCryptHash->pHMACInfo) {
688 BYTE abHashValue[RSAENH_MAX_HASH_SIZE];
690 finalize_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
691 pCryptHash->abHashValue);
692 memcpy(abHashValue, pCryptHash->abHashValue, pCryptHash->dwHashSize);
693 init_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context);
694 update_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
695 pCryptHash->pHMACInfo->pbOuterString,
696 pCryptHash->pHMACInfo->cbOuterString);
697 update_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
698 abHashValue, pCryptHash->dwHashSize);
699 finalize_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
700 pCryptHash->abHashValue);
702 break;
704 case CALG_MAC:
705 dwDataLen = 0;
706 RSAENH_CPEncrypt(pCryptHash->hProv, pCryptHash->hKey, 0, TRUE, 0,
707 pCryptHash->abHashValue, &dwDataLen, pCryptHash->dwHashSize);
708 break;
710 default:
711 finalize_hash_impl(pCryptHash->aiAlgid, &pCryptHash->context, pCryptHash->abHashValue);
715 /******************************************************************************
716 * destroy_key [Internal]
718 * Destructor for key objects
720 * PARAMS
721 * pCryptKey [I] Pointer to the key object to be destroyed.
722 * Will be invalid after function returns!
724 static void destroy_key(OBJECTHDR *pObject)
726 CRYPTKEY *pCryptKey = (CRYPTKEY*)pObject;
728 free_key_impl(pCryptKey->aiAlgid, &pCryptKey->context);
729 free_data_blob(&pCryptKey->siSChannelInfo.blobClientRandom);
730 free_data_blob(&pCryptKey->siSChannelInfo.blobServerRandom);
731 HeapFree(GetProcessHeap(), 0, pCryptKey);
734 /******************************************************************************
735 * setup_key [Internal]
737 * Initialize (or reset) a key object
739 * PARAMS
740 * pCryptKey [I] The key object to be initialized.
742 static inline void setup_key(CRYPTKEY *pCryptKey) {
743 pCryptKey->dwState = RSAENH_KEYSTATE_IDLE;
744 memcpy(pCryptKey->abChainVector, pCryptKey->abInitVector, sizeof(pCryptKey->abChainVector));
745 setup_key_impl(pCryptKey->aiAlgid, &pCryptKey->context, pCryptKey->dwKeyLen,
746 pCryptKey->dwEffectiveKeyLen, pCryptKey->dwSaltLen,
747 pCryptKey->abKeyValue);
750 /******************************************************************************
751 * new_key [Internal]
753 * Creates a new key object without assigning the actual binary key value.
754 * This is done by CPDeriveKey, CPGenKey or CPImportKey, which call this function.
756 * PARAMS
757 * hProv [I] Handle to the provider to which the created key will belong.
758 * aiAlgid [I] The new key shall use the crypto algorithm idenfied by aiAlgid.
759 * dwFlags [I] Upper 16 bits give the key length.
760 * Lower 16 bits: CRYPT_EXPORTABLE, CRYPT_CREATE_SALT,
761 * CRYPT_NO_SALT
762 * ppCryptKey [O] Pointer to the created key
764 * RETURNS
765 * Success: Handle to the created key.
766 * Failure: INVALID_HANDLE_VALUE
768 static HCRYPTKEY new_key(HCRYPTPROV hProv, ALG_ID aiAlgid, DWORD dwFlags, CRYPTKEY **ppCryptKey)
770 HCRYPTKEY hCryptKey;
771 CRYPTKEY *pCryptKey;
772 DWORD dwKeyLen = HIWORD(dwFlags);
773 const PROV_ENUMALGS_EX *peaAlgidInfo;
775 *ppCryptKey = NULL;
778 * Retrieve the CSP's capabilities for the given ALG_ID value
780 peaAlgidInfo = get_algid_info(hProv, aiAlgid);
781 if (!peaAlgidInfo) return (HCRYPTKEY)INVALID_HANDLE_VALUE;
783 TRACE("alg = %s, dwKeyLen = %d\n", debugstr_a(peaAlgidInfo->szName),
784 dwKeyLen);
786 * Assume the default key length, if none is specified explicitly
788 if (dwKeyLen == 0) dwKeyLen = peaAlgidInfo->dwDefaultLen;
791 * Check if the requested key length is supported by the current CSP.
792 * Adjust key length's for DES algorithms.
794 switch (aiAlgid) {
795 case CALG_DES:
796 if (dwKeyLen == RSAENH_DES_EFFECTIVE_KEYLEN) {
797 dwKeyLen = RSAENH_DES_STORAGE_KEYLEN;
799 if (dwKeyLen != RSAENH_DES_STORAGE_KEYLEN) {
800 SetLastError(NTE_BAD_FLAGS);
801 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
803 break;
805 case CALG_3DES_112:
806 if (dwKeyLen == RSAENH_3DES112_EFFECTIVE_KEYLEN) {
807 dwKeyLen = RSAENH_3DES112_STORAGE_KEYLEN;
809 if (dwKeyLen != RSAENH_3DES112_STORAGE_KEYLEN) {
810 SetLastError(NTE_BAD_FLAGS);
811 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
813 break;
815 case CALG_3DES:
816 if (dwKeyLen == RSAENH_3DES_EFFECTIVE_KEYLEN) {
817 dwKeyLen = RSAENH_3DES_STORAGE_KEYLEN;
819 if (dwKeyLen != RSAENH_3DES_STORAGE_KEYLEN) {
820 SetLastError(NTE_BAD_FLAGS);
821 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
823 break;
825 default:
826 if (dwKeyLen % 8 ||
827 dwKeyLen > peaAlgidInfo->dwMaxLen ||
828 dwKeyLen < peaAlgidInfo->dwMinLen)
830 TRACE("key len %d out of bounds (%d, %d)\n", dwKeyLen,
831 peaAlgidInfo->dwMinLen, peaAlgidInfo->dwMaxLen);
832 SetLastError(NTE_BAD_DATA);
833 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
837 hCryptKey = new_object(&handle_table, sizeof(CRYPTKEY), RSAENH_MAGIC_KEY,
838 destroy_key, (OBJECTHDR**)&pCryptKey);
839 if (hCryptKey != (HCRYPTKEY)INVALID_HANDLE_VALUE)
841 pCryptKey->aiAlgid = aiAlgid;
842 pCryptKey->hProv = hProv;
843 pCryptKey->dwModeBits = 0;
844 pCryptKey->dwPermissions = CRYPT_ENCRYPT | CRYPT_DECRYPT | CRYPT_READ | CRYPT_WRITE |
845 CRYPT_MAC;
846 if (dwFlags & CRYPT_EXPORTABLE)
847 pCryptKey->dwPermissions |= CRYPT_EXPORT;
848 pCryptKey->dwKeyLen = dwKeyLen >> 3;
849 pCryptKey->dwEffectiveKeyLen = 0;
850 if ((dwFlags & CRYPT_CREATE_SALT) || (dwKeyLen == 40 && !(dwFlags & CRYPT_NO_SALT)))
851 pCryptKey->dwSaltLen = 16 /*FIXME*/ - pCryptKey->dwKeyLen;
852 else
853 pCryptKey->dwSaltLen = 0;
854 memset(pCryptKey->abKeyValue, 0, sizeof(pCryptKey->abKeyValue));
855 memset(pCryptKey->abInitVector, 0, sizeof(pCryptKey->abInitVector));
856 init_data_blob(&pCryptKey->siSChannelInfo.blobClientRandom);
857 init_data_blob(&pCryptKey->siSChannelInfo.blobServerRandom);
859 switch(aiAlgid)
861 case CALG_PCT1_MASTER:
862 case CALG_SSL2_MASTER:
863 case CALG_SSL3_MASTER:
864 case CALG_TLS1_MASTER:
865 case CALG_RC4:
866 pCryptKey->dwBlockLen = 0;
867 pCryptKey->dwMode = 0;
868 break;
870 case CALG_RC2:
871 case CALG_DES:
872 case CALG_3DES_112:
873 case CALG_3DES:
874 pCryptKey->dwBlockLen = 8;
875 pCryptKey->dwMode = CRYPT_MODE_CBC;
876 break;
878 case CALG_AES:
879 case CALG_AES_128:
880 case CALG_AES_192:
881 case CALG_AES_256:
882 pCryptKey->dwBlockLen = 16;
883 pCryptKey->dwMode = CRYPT_MODE_ECB;
884 break;
886 case CALG_RSA_KEYX:
887 case CALG_RSA_SIGN:
888 pCryptKey->dwBlockLen = dwKeyLen >> 3;
889 pCryptKey->dwMode = 0;
890 break;
893 *ppCryptKey = pCryptKey;
896 return hCryptKey;
899 /******************************************************************************
900 * map_key_spec_to_key_pair_name [Internal]
902 * Returns the name of the registry value associated with a key spec.
904 * PARAMS
905 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
907 * RETURNS
908 * Success: Name of registry value.
909 * Failure: NULL
911 static LPCSTR map_key_spec_to_key_pair_name(DWORD dwKeySpec)
913 LPCSTR szValueName;
915 switch (dwKeySpec)
917 case AT_KEYEXCHANGE:
918 szValueName = "KeyExchangeKeyPair";
919 break;
920 case AT_SIGNATURE:
921 szValueName = "SignatureKeyPair";
922 break;
923 default:
924 WARN("invalid key spec %d\n", dwKeySpec);
925 szValueName = NULL;
927 return szValueName;
930 /******************************************************************************
931 * store_key_pair [Internal]
933 * Stores a key pair to the registry
935 * PARAMS
936 * hCryptKey [I] Handle to the key to be stored
937 * hKey [I] Registry key where the key pair is to be stored
938 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
939 * dwFlags [I] Flags for protecting the key
941 static void store_key_pair(HCRYPTKEY hCryptKey, HKEY hKey, DWORD dwKeySpec, DWORD dwFlags)
943 LPCSTR szValueName;
944 DATA_BLOB blobIn, blobOut;
945 CRYPTKEY *pKey;
946 DWORD dwLen;
947 BYTE *pbKey;
949 if (!(szValueName = map_key_spec_to_key_pair_name(dwKeySpec)))
950 return;
951 if (lookup_handle(&handle_table, hCryptKey, RSAENH_MAGIC_KEY,
952 (OBJECTHDR**)&pKey))
954 if (crypt_export_key(pKey, 0, PRIVATEKEYBLOB, 0, TRUE, 0, &dwLen))
956 pbKey = HeapAlloc(GetProcessHeap(), 0, dwLen);
957 if (pbKey)
959 if (crypt_export_key(pKey, 0, PRIVATEKEYBLOB, 0, TRUE, pbKey,
960 &dwLen))
962 blobIn.pbData = pbKey;
963 blobIn.cbData = dwLen;
965 if (CryptProtectData(&blobIn, NULL, NULL, NULL, NULL,
966 dwFlags, &blobOut))
968 RegSetValueExA(hKey, szValueName, 0, REG_BINARY,
969 blobOut.pbData, blobOut.cbData);
970 LocalFree(blobOut.pbData);
973 HeapFree(GetProcessHeap(), 0, pbKey);
979 /******************************************************************************
980 * map_key_spec_to_permissions_name [Internal]
982 * Returns the name of the registry value associated with the permissions for
983 * a key spec.
985 * PARAMS
986 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
988 * RETURNS
989 * Success: Name of registry value.
990 * Failure: NULL
992 static LPCSTR map_key_spec_to_permissions_name(DWORD dwKeySpec)
994 LPCSTR szValueName;
996 switch (dwKeySpec)
998 case AT_KEYEXCHANGE:
999 szValueName = "KeyExchangePermissions";
1000 break;
1001 case AT_SIGNATURE:
1002 szValueName = "SignaturePermissions";
1003 break;
1004 default:
1005 WARN("invalid key spec %d\n", dwKeySpec);
1006 szValueName = NULL;
1008 return szValueName;
1011 /******************************************************************************
1012 * store_key_permissions [Internal]
1014 * Stores a key's permissions to the registry
1016 * PARAMS
1017 * hCryptKey [I] Handle to the key whose permissions are to be stored
1018 * hKey [I] Registry key where the key permissions are to be stored
1019 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
1021 static void store_key_permissions(HCRYPTKEY hCryptKey, HKEY hKey, DWORD dwKeySpec)
1023 LPCSTR szValueName;
1024 CRYPTKEY *pKey;
1026 if (!(szValueName = map_key_spec_to_permissions_name(dwKeySpec)))
1027 return;
1028 if (lookup_handle(&handle_table, hCryptKey, RSAENH_MAGIC_KEY,
1029 (OBJECTHDR**)&pKey))
1030 RegSetValueExA(hKey, szValueName, 0, REG_DWORD,
1031 (BYTE *)&pKey->dwPermissions,
1032 sizeof(pKey->dwPermissions));
1035 /******************************************************************************
1036 * create_container_key [Internal]
1038 * Creates the registry key for a key container's persistent storage.
1040 * PARAMS
1041 * pKeyContainer [I] Pointer to the key container
1042 * sam [I] Desired registry access
1043 * phKey [O] Returned key
1045 static BOOL create_container_key(KEYCONTAINER *pKeyContainer, REGSAM sam, HKEY *phKey)
1047 CHAR szRSABase[MAX_PATH];
1048 HKEY hRootKey;
1050 sprintf(szRSABase, RSAENH_REGKEY, pKeyContainer->szName);
1052 if (pKeyContainer->dwFlags & CRYPT_MACHINE_KEYSET)
1053 hRootKey = HKEY_LOCAL_MACHINE;
1054 else
1055 hRootKey = HKEY_CURRENT_USER;
1057 /* @@ Wine registry key: HKLM\Software\Wine\Crypto\RSA */
1058 /* @@ Wine registry key: HKCU\Software\Wine\Crypto\RSA */
1059 return RegCreateKeyExA(hRootKey, szRSABase, 0, NULL,
1060 REG_OPTION_NON_VOLATILE, sam, NULL, phKey, NULL)
1061 == ERROR_SUCCESS;
1064 /******************************************************************************
1065 * open_container_key [Internal]
1067 * Opens a key container's persistent storage for reading.
1069 * PARAMS
1070 * pszContainerName [I] Name of the container to be opened. May be the empty
1071 * string if the parent key of all containers is to be
1072 * opened.
1073 * dwFlags [I] Flags indicating which keyset to be opened.
1074 * phKey [O] Returned key
1076 static BOOL open_container_key(LPCSTR pszContainerName, DWORD dwFlags, HKEY *phKey)
1078 CHAR szRSABase[MAX_PATH];
1079 HKEY hRootKey;
1081 sprintf(szRSABase, RSAENH_REGKEY, pszContainerName);
1083 if (dwFlags & CRYPT_MACHINE_KEYSET)
1084 hRootKey = HKEY_LOCAL_MACHINE;
1085 else
1086 hRootKey = HKEY_CURRENT_USER;
1088 /* @@ Wine registry key: HKLM\Software\Wine\Crypto\RSA */
1089 /* @@ Wine registry key: HKCU\Software\Wine\Crypto\RSA */
1090 return RegOpenKeyExA(hRootKey, szRSABase, 0, KEY_READ, phKey) ==
1091 ERROR_SUCCESS;
1094 /******************************************************************************
1095 * delete_container_key [Internal]
1097 * Deletes a key container's persistent storage.
1099 * PARAMS
1100 * pszContainerName [I] Name of the container to be opened.
1101 * dwFlags [I] Flags indicating which keyset to be opened.
1103 static BOOL delete_container_key(LPCSTR pszContainerName, DWORD dwFlags)
1105 CHAR szRegKey[MAX_PATH];
1107 if (snprintf(szRegKey, MAX_PATH, RSAENH_REGKEY, pszContainerName) >= MAX_PATH) {
1108 SetLastError(NTE_BAD_KEYSET_PARAM);
1109 return FALSE;
1110 } else {
1111 HKEY hRootKey;
1112 if (dwFlags & CRYPT_MACHINE_KEYSET)
1113 hRootKey = HKEY_LOCAL_MACHINE;
1114 else
1115 hRootKey = HKEY_CURRENT_USER;
1116 if (!RegDeleteKeyA(hRootKey, szRegKey)) {
1117 SetLastError(ERROR_SUCCESS);
1118 return TRUE;
1119 } else {
1120 SetLastError(NTE_BAD_KEYSET);
1121 return FALSE;
1126 /******************************************************************************
1127 * store_key_container_keys [Internal]
1129 * Stores key container's keys in a persistent location.
1131 * PARAMS
1132 * pKeyContainer [I] Pointer to the key container whose keys are to be saved
1134 static void store_key_container_keys(KEYCONTAINER *pKeyContainer)
1136 HKEY hKey;
1137 DWORD dwFlags;
1139 /* On WinXP, persistent keys are stored in a file located at:
1140 * $AppData$\\Microsoft\\Crypto\\RSA\\$SID$\\some_hex_string
1143 if (pKeyContainer->dwFlags & CRYPT_MACHINE_KEYSET)
1144 dwFlags = CRYPTPROTECT_LOCAL_MACHINE;
1145 else
1146 dwFlags = 0;
1148 if (create_container_key(pKeyContainer, KEY_WRITE, &hKey))
1150 store_key_pair(pKeyContainer->hKeyExchangeKeyPair, hKey,
1151 AT_KEYEXCHANGE, dwFlags);
1152 store_key_pair(pKeyContainer->hSignatureKeyPair, hKey,
1153 AT_SIGNATURE, dwFlags);
1154 RegCloseKey(hKey);
1158 /******************************************************************************
1159 * store_key_container_permissions [Internal]
1161 * Stores key container's key permissions in a persistent location.
1163 * PARAMS
1164 * pKeyContainer [I] Pointer to the key container whose key permissions are to
1165 * be saved
1167 static void store_key_container_permissions(KEYCONTAINER *pKeyContainer)
1169 HKEY hKey;
1170 DWORD dwFlags;
1172 /* On WinXP, persistent keys are stored in a file located at:
1173 * $AppData$\\Microsoft\\Crypto\\RSA\\$SID$\\some_hex_string
1176 if (pKeyContainer->dwFlags & CRYPT_MACHINE_KEYSET)
1177 dwFlags = CRYPTPROTECT_LOCAL_MACHINE;
1178 else
1179 dwFlags = 0;
1181 if (create_container_key(pKeyContainer, KEY_WRITE, &hKey))
1183 store_key_permissions(pKeyContainer->hKeyExchangeKeyPair, hKey,
1184 AT_KEYEXCHANGE);
1185 store_key_permissions(pKeyContainer->hSignatureKeyPair, hKey,
1186 AT_SIGNATURE);
1187 RegCloseKey(hKey);
1191 /******************************************************************************
1192 * release_key_container_keys [Internal]
1194 * Releases key container's keys.
1196 * PARAMS
1197 * pKeyContainer [I] Pointer to the key container whose keys are to be released.
1199 static void release_key_container_keys(KEYCONTAINER *pKeyContainer)
1201 release_handle(&handle_table, pKeyContainer->hKeyExchangeKeyPair,
1202 RSAENH_MAGIC_KEY);
1203 release_handle(&handle_table, pKeyContainer->hSignatureKeyPair,
1204 RSAENH_MAGIC_KEY);
1207 /******************************************************************************
1208 * destroy_key_container [Internal]
1210 * Destructor for key containers.
1212 * PARAMS
1213 * pObjectHdr [I] Pointer to the key container to be destroyed.
1215 static void destroy_key_container(OBJECTHDR *pObjectHdr)
1217 KEYCONTAINER *pKeyContainer = (KEYCONTAINER*)pObjectHdr;
1219 if (!(pKeyContainer->dwFlags & CRYPT_VERIFYCONTEXT))
1221 store_key_container_keys(pKeyContainer);
1222 store_key_container_permissions(pKeyContainer);
1223 release_key_container_keys(pKeyContainer);
1225 else
1226 release_key_container_keys(pKeyContainer);
1227 HeapFree( GetProcessHeap(), 0, pKeyContainer );
1230 /******************************************************************************
1231 * new_key_container [Internal]
1233 * Create a new key container. The personality (RSA Base, Strong or Enhanced CP)
1234 * of the CSP is determined via the pVTable->pszProvName string.
1236 * PARAMS
1237 * pszContainerName [I] Name of the key container.
1238 * pVTable [I] Callback functions and context info provided by the OS
1240 * RETURNS
1241 * Success: Handle to the new key container.
1242 * Failure: INVALID_HANDLE_VALUE
1244 static HCRYPTPROV new_key_container(PCCH pszContainerName, DWORD dwFlags, const VTableProvStruc *pVTable)
1246 KEYCONTAINER *pKeyContainer;
1247 HCRYPTPROV hKeyContainer;
1249 hKeyContainer = new_object(&handle_table, sizeof(KEYCONTAINER), RSAENH_MAGIC_CONTAINER,
1250 destroy_key_container, (OBJECTHDR**)&pKeyContainer);
1251 if (hKeyContainer != (HCRYPTPROV)INVALID_HANDLE_VALUE)
1253 lstrcpynA(pKeyContainer->szName, pszContainerName, MAX_PATH);
1254 pKeyContainer->dwFlags = dwFlags;
1255 pKeyContainer->dwEnumAlgsCtr = 0;
1256 pKeyContainer->hKeyExchangeKeyPair = (HCRYPTKEY)INVALID_HANDLE_VALUE;
1257 pKeyContainer->hSignatureKeyPair = (HCRYPTKEY)INVALID_HANDLE_VALUE;
1258 if (pVTable && pVTable->pszProvName) {
1259 lstrcpynA(pKeyContainer->szProvName, pVTable->pszProvName, MAX_PATH);
1260 if (!strcmp(pVTable->pszProvName, MS_DEF_PROV_A)) {
1261 pKeyContainer->dwPersonality = RSAENH_PERSONALITY_BASE;
1262 } else if (!strcmp(pVTable->pszProvName, MS_ENHANCED_PROV_A)) {
1263 pKeyContainer->dwPersonality = RSAENH_PERSONALITY_ENHANCED;
1264 } else if (!strcmp(pVTable->pszProvName, MS_DEF_RSA_SCHANNEL_PROV_A)) {
1265 pKeyContainer->dwPersonality = RSAENH_PERSONALITY_SCHANNEL;
1266 } else if (!strcmp(pVTable->pszProvName, MS_ENH_RSA_AES_PROV_A)) {
1267 pKeyContainer->dwPersonality = RSAENH_PERSONALITY_AES;
1268 } else {
1269 pKeyContainer->dwPersonality = RSAENH_PERSONALITY_STRONG;
1273 /* The new key container has to be inserted into the CSP immediately
1274 * after creation to be available for CPGetProvParam's PP_ENUMCONTAINERS. */
1275 if (!(dwFlags & CRYPT_VERIFYCONTEXT)) {
1276 HKEY hKey;
1278 if (create_container_key(pKeyContainer, KEY_WRITE, &hKey))
1279 RegCloseKey(hKey);
1283 return hKeyContainer;
1286 /******************************************************************************
1287 * read_key_value [Internal]
1289 * Reads a key pair value from the registry
1291 * PARAMS
1292 * hKeyContainer [I] Crypt provider to use to import the key
1293 * hKey [I] Registry key from which to read the key pair
1294 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
1295 * dwFlags [I] Flags for unprotecting the key
1296 * phCryptKey [O] Returned key
1298 static BOOL read_key_value(HCRYPTPROV hKeyContainer, HKEY hKey, DWORD dwKeySpec, DWORD dwFlags, HCRYPTKEY *phCryptKey)
1300 LPCSTR szValueName;
1301 DWORD dwValueType, dwLen;
1302 BYTE *pbKey;
1303 DATA_BLOB blobIn, blobOut;
1304 BOOL ret = FALSE;
1306 if (!(szValueName = map_key_spec_to_key_pair_name(dwKeySpec)))
1307 return FALSE;
1308 if (RegQueryValueExA(hKey, szValueName, 0, &dwValueType, NULL, &dwLen) ==
1309 ERROR_SUCCESS)
1311 pbKey = HeapAlloc(GetProcessHeap(), 0, dwLen);
1312 if (pbKey)
1314 if (RegQueryValueExA(hKey, szValueName, 0, &dwValueType, pbKey, &dwLen) ==
1315 ERROR_SUCCESS)
1317 blobIn.pbData = pbKey;
1318 blobIn.cbData = dwLen;
1320 if (CryptUnprotectData(&blobIn, NULL, NULL, NULL, NULL,
1321 dwFlags, &blobOut))
1323 ret = import_key(hKeyContainer, blobOut.pbData, blobOut.cbData, 0, 0,
1324 FALSE, phCryptKey);
1325 LocalFree(blobOut.pbData);
1328 HeapFree(GetProcessHeap(), 0, pbKey);
1331 if (ret)
1333 CRYPTKEY *pKey;
1335 if (lookup_handle(&handle_table, *phCryptKey, RSAENH_MAGIC_KEY,
1336 (OBJECTHDR**)&pKey))
1338 if ((szValueName = map_key_spec_to_permissions_name(dwKeySpec)))
1340 dwLen = sizeof(pKey->dwPermissions);
1341 RegQueryValueExA(hKey, szValueName, 0, NULL,
1342 (BYTE *)&pKey->dwPermissions, &dwLen);
1346 return ret;
1349 /******************************************************************************
1350 * read_key_container [Internal]
1352 * Tries to read the persistent state of the key container (mainly the signature
1353 * and key exchange private keys) given by pszContainerName.
1355 * PARAMS
1356 * pszContainerName [I] Name of the key container to read from the registry
1357 * pVTable [I] Pointer to context data provided by the operating system
1359 * RETURNS
1360 * Success: Handle to the key container read from the registry
1361 * Failure: INVALID_HANDLE_VALUE
1363 static HCRYPTPROV read_key_container(PCHAR pszContainerName, DWORD dwFlags, const VTableProvStruc *pVTable)
1365 HKEY hKey;
1366 KEYCONTAINER *pKeyContainer;
1367 HCRYPTPROV hKeyContainer;
1368 HCRYPTKEY hCryptKey;
1370 if (!open_container_key(pszContainerName, dwFlags, &hKey))
1372 SetLastError(NTE_BAD_KEYSET);
1373 return (HCRYPTPROV)INVALID_HANDLE_VALUE;
1376 hKeyContainer = new_key_container(pszContainerName, dwFlags, pVTable);
1377 if (hKeyContainer != (HCRYPTPROV)INVALID_HANDLE_VALUE)
1379 DWORD dwProtectFlags = (dwFlags & CRYPT_MACHINE_KEYSET) ?
1380 CRYPTPROTECT_LOCAL_MACHINE : 0;
1382 if (!lookup_handle(&handle_table, hKeyContainer, RSAENH_MAGIC_CONTAINER,
1383 (OBJECTHDR**)&pKeyContainer))
1384 return (HCRYPTPROV)INVALID_HANDLE_VALUE;
1386 /* read_key_value calls import_key, which calls import_private_key,
1387 * which implicitly installs the key value into the appropriate key
1388 * container key. Thus the ref count is incremented twice, once for
1389 * the output key value, and once for the implicit install, and needs
1390 * to be decremented to balance the two.
1392 if (read_key_value(hKeyContainer, hKey, AT_KEYEXCHANGE,
1393 dwProtectFlags, &hCryptKey))
1394 release_handle(&handle_table, hCryptKey, RSAENH_MAGIC_KEY);
1395 if (read_key_value(hKeyContainer, hKey, AT_SIGNATURE,
1396 dwProtectFlags, &hCryptKey))
1397 release_handle(&handle_table, hCryptKey, RSAENH_MAGIC_KEY);
1400 return hKeyContainer;
1403 /******************************************************************************
1404 * build_hash_signature [Internal]
1406 * Builds a padded version of a hash to match the length of the RSA key modulus.
1408 * PARAMS
1409 * pbSignature [O] The padded hash object is stored here.
1410 * dwLen [I] Length of the pbSignature buffer.
1411 * aiAlgid [I] Algorithm identifier of the hash to be padded.
1412 * abHashValue [I] The value of the hash object.
1413 * dwHashLen [I] Length of the hash value.
1414 * dwFlags [I] Selection of padding algorithm.
1416 * RETURNS
1417 * Success: TRUE
1418 * Failure: FALSE (NTE_BAD_ALGID)
1420 static BOOL build_hash_signature(BYTE *pbSignature, DWORD dwLen, ALG_ID aiAlgid,
1421 CONST BYTE *abHashValue, DWORD dwHashLen, DWORD dwFlags)
1423 /* These prefixes are meant to be concatenated with hash values of the
1424 * respective kind to form a PKCS #7 DigestInfo. */
1425 static const struct tagOIDDescriptor {
1426 ALG_ID aiAlgid;
1427 DWORD dwLen;
1428 CONST BYTE abOID[19];
1429 } aOIDDescriptor[] = {
1430 { CALG_MD2, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1431 0x86, 0xf7, 0x0d, 0x02, 0x02, 0x05, 0x00, 0x04, 0x10 } },
1432 { CALG_MD4, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1433 0x86, 0xf7, 0x0d, 0x02, 0x04, 0x05, 0x00, 0x04, 0x10 } },
1434 { CALG_MD5, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1435 0x86, 0xf7, 0x0d, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10 } },
1436 { CALG_SHA, 15, { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03,
1437 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14 } },
1438 { CALG_SHA_256, 19, { 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
1439 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01,
1440 0x05, 0x00, 0x04, 0x20 } },
1441 { CALG_SHA_384, 19, { 0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
1442 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01,
1443 0x05, 0x00, 0x04, 0x30 } },
1444 { CALG_SHA_384, 19, { 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
1445 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01,
1446 0x05, 0x00, 0x04, 0x40 } },
1447 { CALG_SSL3_SHAMD5, 0, { 0 } },
1448 { 0, 0, { 0 } }
1450 DWORD dwIdxOID, i, j;
1452 for (dwIdxOID = 0; aOIDDescriptor[dwIdxOID].aiAlgid; dwIdxOID++) {
1453 if (aOIDDescriptor[dwIdxOID].aiAlgid == aiAlgid) break;
1456 if (!aOIDDescriptor[dwIdxOID].aiAlgid) {
1457 SetLastError(NTE_BAD_ALGID);
1458 return FALSE;
1461 /* Build the padded signature */
1462 if (dwFlags & CRYPT_X931_FORMAT) {
1463 pbSignature[0] = 0x6b;
1464 for (i=1; i < dwLen - dwHashLen - 3; i++) {
1465 pbSignature[i] = 0xbb;
1467 pbSignature[i++] = 0xba;
1468 for (j=0; j < dwHashLen; j++, i++) {
1469 pbSignature[i] = abHashValue[j];
1471 pbSignature[i++] = 0x33;
1472 pbSignature[i++] = 0xcc;
1473 } else {
1474 pbSignature[0] = 0x00;
1475 pbSignature[1] = 0x01;
1476 if (dwFlags & CRYPT_NOHASHOID) {
1477 for (i=2; i < dwLen - 1 - dwHashLen; i++) {
1478 pbSignature[i] = 0xff;
1480 pbSignature[i++] = 0x00;
1481 } else {
1482 for (i=2; i < dwLen - 1 - aOIDDescriptor[dwIdxOID].dwLen - dwHashLen; i++) {
1483 pbSignature[i] = 0xff;
1485 pbSignature[i++] = 0x00;
1486 for (j=0; j < aOIDDescriptor[dwIdxOID].dwLen; j++) {
1487 pbSignature[i++] = aOIDDescriptor[dwIdxOID].abOID[j];
1490 for (j=0; j < dwHashLen; j++) {
1491 pbSignature[i++] = abHashValue[j];
1495 return TRUE;
1498 /******************************************************************************
1499 * tls1_p [Internal]
1501 * This is an implementation of the 'P_hash' helper function for TLS1's PRF.
1502 * It is used exclusively by tls1_prf. For details see RFC 2246, chapter 5.
1503 * The pseudo random stream generated by this function is exclusive or'ed with
1504 * the data in pbBuffer.
1506 * PARAMS
1507 * hHMAC [I] HMAC object, which will be used in pseudo random generation
1508 * pblobSeed [I] Seed value
1509 * pbBuffer [I/O] Pseudo random stream will be xor'ed to the provided data
1510 * dwBufferLen [I] Number of pseudo random bytes desired
1512 * RETURNS
1513 * Success: TRUE
1514 * Failure: FALSE
1516 static BOOL tls1_p(HCRYPTHASH hHMAC, CONST PCRYPT_DATA_BLOB pblobSeed, PBYTE pbBuffer, DWORD dwBufferLen)
1518 CRYPTHASH *pHMAC;
1519 BYTE abAi[RSAENH_MAX_HASH_SIZE];
1520 DWORD i = 0;
1522 if (!lookup_handle(&handle_table, hHMAC, RSAENH_MAGIC_HASH, (OBJECTHDR**)&pHMAC)) {
1523 SetLastError(NTE_BAD_HASH);
1524 return FALSE;
1527 /* compute A_1 = HMAC(seed) */
1528 init_hash(pHMAC);
1529 update_hash(pHMAC, pblobSeed->pbData, pblobSeed->cbData);
1530 finalize_hash(pHMAC);
1531 memcpy(abAi, pHMAC->abHashValue, pHMAC->dwHashSize);
1533 do {
1534 /* compute HMAC(A_i + seed) */
1535 init_hash(pHMAC);
1536 update_hash(pHMAC, abAi, pHMAC->dwHashSize);
1537 update_hash(pHMAC, pblobSeed->pbData, pblobSeed->cbData);
1538 finalize_hash(pHMAC);
1540 /* pseudo random stream := CONCAT_{i=1..n} ( HMAC(A_i + seed) ) */
1541 do {
1542 if (i >= dwBufferLen) break;
1543 pbBuffer[i] ^= pHMAC->abHashValue[i % pHMAC->dwHashSize];
1544 i++;
1545 } while (i % pHMAC->dwHashSize);
1547 /* compute A_{i+1} = HMAC(A_i) */
1548 init_hash(pHMAC);
1549 update_hash(pHMAC, abAi, pHMAC->dwHashSize);
1550 finalize_hash(pHMAC);
1551 memcpy(abAi, pHMAC->abHashValue, pHMAC->dwHashSize);
1552 } while (i < dwBufferLen);
1554 return TRUE;
1557 /******************************************************************************
1558 * tls1_prf [Internal]
1560 * TLS1 pseudo random function as specified in RFC 2246, chapter 5
1562 * PARAMS
1563 * hProv [I] Key container used to compute the pseudo random stream
1564 * hSecret [I] Key that holds the (pre-)master secret
1565 * pblobLabel [I] Descriptive label
1566 * pblobSeed [I] Seed value
1567 * pbBuffer [O] Pseudo random numbers will be stored here
1568 * dwBufferLen [I] Number of pseudo random bytes desired
1570 * RETURNS
1571 * Success: TRUE
1572 * Failure: FALSE
1574 static BOOL tls1_prf(HCRYPTPROV hProv, HCRYPTPROV hSecret, CONST PCRYPT_DATA_BLOB pblobLabel,
1575 CONST PCRYPT_DATA_BLOB pblobSeed, PBYTE pbBuffer, DWORD dwBufferLen)
1577 HMAC_INFO hmacInfo = { 0, NULL, 0, NULL, 0 };
1578 HCRYPTHASH hHMAC = (HCRYPTHASH)INVALID_HANDLE_VALUE;
1579 HCRYPTKEY hHalfSecret = (HCRYPTKEY)INVALID_HANDLE_VALUE;
1580 CRYPTKEY *pHalfSecret, *pSecret;
1581 DWORD dwHalfSecretLen;
1582 BOOL result = FALSE;
1583 CRYPT_DATA_BLOB blobLabelSeed;
1585 TRACE("(hProv=%08lx, hSecret=%08lx, pblobLabel=%p, pblobSeed=%p, pbBuffer=%p, dwBufferLen=%d)\n",
1586 hProv, hSecret, pblobLabel, pblobSeed, pbBuffer, dwBufferLen);
1588 if (!lookup_handle(&handle_table, hSecret, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pSecret)) {
1589 SetLastError(NTE_FAIL);
1590 return FALSE;
1593 dwHalfSecretLen = (pSecret->dwKeyLen+1)/2;
1595 /* concatenation of the label and the seed */
1596 if (!concat_data_blobs(&blobLabelSeed, pblobLabel, pblobSeed)) goto exit;
1598 /* zero out the buffer, since two random streams will be xor'ed into it. */
1599 memset(pbBuffer, 0, dwBufferLen);
1601 /* build a 'fake' key, to hold the secret. CALG_SSL2_MASTER is used since it provides
1602 * the biggest range of valid key lengths. */
1603 hHalfSecret = new_key(hProv, CALG_SSL2_MASTER, MAKELONG(0,dwHalfSecretLen*8), &pHalfSecret);
1604 if (hHalfSecret == (HCRYPTKEY)INVALID_HANDLE_VALUE) goto exit;
1606 /* Derive an HMAC_MD5 hash and call the helper function. */
1607 memcpy(pHalfSecret->abKeyValue, pSecret->abKeyValue, dwHalfSecretLen);
1608 if (!RSAENH_CPCreateHash(hProv, CALG_HMAC, hHalfSecret, 0, &hHMAC)) goto exit;
1609 hmacInfo.HashAlgid = CALG_MD5;
1610 if (!RSAENH_CPSetHashParam(hProv, hHMAC, HP_HMAC_INFO, (BYTE*)&hmacInfo, 0)) goto exit;
1611 if (!tls1_p(hHMAC, &blobLabelSeed, pbBuffer, dwBufferLen)) goto exit;
1613 /* Reconfigure to HMAC_SHA hash and call helper function again. */
1614 memcpy(pHalfSecret->abKeyValue, pSecret->abKeyValue + (pSecret->dwKeyLen/2), dwHalfSecretLen);
1615 hmacInfo.HashAlgid = CALG_SHA;
1616 if (!RSAENH_CPSetHashParam(hProv, hHMAC, HP_HMAC_INFO, (BYTE*)&hmacInfo, 0)) goto exit;
1617 if (!tls1_p(hHMAC, &blobLabelSeed, pbBuffer, dwBufferLen)) goto exit;
1619 result = TRUE;
1620 exit:
1621 release_handle(&handle_table, hHalfSecret, RSAENH_MAGIC_KEY);
1622 if (hHMAC != (HCRYPTHASH)INVALID_HANDLE_VALUE) RSAENH_CPDestroyHash(hProv, hHMAC);
1623 free_data_blob(&blobLabelSeed);
1624 return result;
1627 /******************************************************************************
1628 * pad_data [Internal]
1630 * Helper function for data padding according to PKCS1 #2
1632 * PARAMS
1633 * abData [I] The data to be padded
1634 * dwDataLen [I] Length of the data
1635 * abBuffer [O] Padded data will be stored here
1636 * dwBufferLen [I] Length of the buffer (also length of padded data)
1637 * dwFlags [I] Padding format (CRYPT_SSL2_FALLBACK)
1639 * RETURN
1640 * Success: TRUE
1641 * Failure: FALSE (NTE_BAD_LEN, too much data to pad)
1643 static BOOL pad_data(CONST BYTE *abData, DWORD dwDataLen, BYTE *abBuffer, DWORD dwBufferLen,
1644 DWORD dwFlags)
1646 DWORD i;
1648 /* Ensure there is enough space for PKCS1 #2 padding */
1649 if (dwDataLen > dwBufferLen-11) {
1650 SetLastError(NTE_BAD_LEN);
1651 return FALSE;
1654 memmove(abBuffer + dwBufferLen - dwDataLen, abData, dwDataLen);
1656 abBuffer[0] = 0x00;
1657 abBuffer[1] = RSAENH_PKC_BLOCKTYPE;
1658 for (i=2; i < dwBufferLen - dwDataLen - 1; i++)
1659 do gen_rand_impl(&abBuffer[i], 1); while (!abBuffer[i]);
1660 if (dwFlags & CRYPT_SSL2_FALLBACK)
1661 for (i-=8; i < dwBufferLen - dwDataLen - 1; i++)
1662 abBuffer[i] = 0x03;
1663 abBuffer[i] = 0x00;
1665 return TRUE;
1668 /******************************************************************************
1669 * unpad_data [Internal]
1671 * Remove the PKCS1 padding from RSA decrypted data
1673 * PARAMS
1674 * abData [I] The padded data
1675 * dwDataLen [I] Length of the padded data
1676 * abBuffer [O] Data without padding will be stored here
1677 * dwBufferLen [I/O] I: Length of the buffer, O: Length of unpadded data
1678 * dwFlags [I] Currently none defined
1680 * RETURNS
1681 * Success: TRUE
1682 * Failure: FALSE, (NTE_BAD_DATA, no valid PKCS1 padding or buffer too small)
1684 static BOOL unpad_data(CONST BYTE *abData, DWORD dwDataLen, BYTE *abBuffer, DWORD *dwBufferLen,
1685 DWORD dwFlags)
1687 DWORD i;
1689 for (i=2; i<dwDataLen; i++)
1690 if (!abData[i])
1691 break;
1693 if ((i == dwDataLen) || (*dwBufferLen < dwDataLen - i - 1) ||
1694 (abData[0] != 0x00) || (abData[1] != RSAENH_PKC_BLOCKTYPE))
1696 SetLastError(NTE_BAD_DATA);
1697 return FALSE;
1700 *dwBufferLen = dwDataLen - i - 1;
1701 memmove(abBuffer, abData + i + 1, *dwBufferLen);
1702 return TRUE;
1705 /******************************************************************************
1706 * CPAcquireContext (RSAENH.@)
1708 * Acquire a handle to the key container specified by pszContainer
1710 * PARAMS
1711 * phProv [O] Pointer to the location the acquired handle will be written to.
1712 * pszContainer [I] Name of the desired key container. See Notes
1713 * dwFlags [I] Flags. See Notes.
1714 * pVTable [I] Pointer to a PVTableProvStruct containing callbacks.
1716 * RETURNS
1717 * Success: TRUE
1718 * Failure: FALSE
1720 * NOTES
1721 * If pszContainer is NULL or points to a zero length string the user's login
1722 * name will be used as the key container name.
1724 * If the CRYPT_NEW_KEYSET flag is set in dwFlags a new keyset will be created.
1725 * If a keyset with the given name already exists, the function fails and sets
1726 * last error to NTE_EXISTS. If CRYPT_NEW_KEYSET is not set and the specified
1727 * key container does not exist, function fails and sets last error to
1728 * NTE_BAD_KEYSET.
1730 BOOL WINAPI RSAENH_CPAcquireContext(HCRYPTPROV *phProv, LPSTR pszContainer,
1731 DWORD dwFlags, PVTableProvStruc pVTable)
1733 CHAR szKeyContainerName[MAX_PATH];
1735 TRACE("(phProv=%p, pszContainer=%s, dwFlags=%08x, pVTable=%p)\n", phProv,
1736 debugstr_a(pszContainer), dwFlags, pVTable);
1738 if (pszContainer && *pszContainer)
1740 lstrcpynA(szKeyContainerName, pszContainer, MAX_PATH);
1742 else
1744 DWORD dwLen = sizeof(szKeyContainerName);
1745 if (!GetUserNameA(szKeyContainerName, &dwLen)) return FALSE;
1748 switch (dwFlags & (CRYPT_NEWKEYSET|CRYPT_VERIFYCONTEXT|CRYPT_DELETEKEYSET))
1750 case 0:
1751 *phProv = read_key_container(szKeyContainerName, dwFlags, pVTable);
1752 break;
1754 case CRYPT_DELETEKEYSET:
1755 return delete_container_key(szKeyContainerName, dwFlags);
1757 case CRYPT_NEWKEYSET:
1758 *phProv = read_key_container(szKeyContainerName, dwFlags, pVTable);
1759 if (*phProv != (HCRYPTPROV)INVALID_HANDLE_VALUE)
1761 release_handle(&handle_table, *phProv, RSAENH_MAGIC_CONTAINER);
1762 TRACE("Can't create new keyset, already exists\n");
1763 SetLastError(NTE_EXISTS);
1764 return FALSE;
1766 *phProv = new_key_container(szKeyContainerName, dwFlags, pVTable);
1767 break;
1769 case CRYPT_VERIFYCONTEXT|CRYPT_NEWKEYSET:
1770 case CRYPT_VERIFYCONTEXT:
1771 if (pszContainer && *pszContainer) {
1772 TRACE("pszContainer should be empty\n");
1773 SetLastError(NTE_BAD_FLAGS);
1774 return FALSE;
1776 *phProv = new_key_container("", dwFlags, pVTable);
1777 break;
1779 default:
1780 *phProv = (HCRYPTPROV)INVALID_HANDLE_VALUE;
1781 SetLastError(NTE_BAD_FLAGS);
1782 return FALSE;
1785 if (*phProv != (HCRYPTPROV)INVALID_HANDLE_VALUE) {
1786 SetLastError(ERROR_SUCCESS);
1787 return TRUE;
1788 } else {
1789 return FALSE;
1793 /******************************************************************************
1794 * CPCreateHash (RSAENH.@)
1796 * CPCreateHash creates and initalizes a new hash object.
1798 * PARAMS
1799 * hProv [I] Handle to the key container to which the new hash will belong.
1800 * Algid [I] Identifies the hash algorithm, which will be used for the hash.
1801 * hKey [I] Handle to a session key applied for keyed hashes.
1802 * dwFlags [I] Currently no flags defined. Must be zero.
1803 * phHash [O] Points to the location where a handle to the new hash will be stored.
1805 * RETURNS
1806 * Success: TRUE
1807 * Failure: FALSE
1809 * NOTES
1810 * hKey is a handle to a session key applied in keyed hashes like MAC and HMAC.
1811 * If a normal hash object is to be created (like e.g. MD2 or SHA1) hKey must be zero.
1813 BOOL WINAPI RSAENH_CPCreateHash(HCRYPTPROV hProv, ALG_ID Algid, HCRYPTKEY hKey, DWORD dwFlags,
1814 HCRYPTHASH *phHash)
1816 CRYPTKEY *pCryptKey;
1817 CRYPTHASH *pCryptHash;
1818 const PROV_ENUMALGS_EX *peaAlgidInfo;
1820 TRACE("(hProv=%08lx, Algid=%08x, hKey=%08lx, dwFlags=%08x, phHash=%p)\n", hProv, Algid, hKey,
1821 dwFlags, phHash);
1823 peaAlgidInfo = get_algid_info(hProv, Algid);
1824 if (!peaAlgidInfo) return FALSE;
1826 if (dwFlags)
1828 SetLastError(NTE_BAD_FLAGS);
1829 return FALSE;
1832 if (Algid == CALG_MAC || Algid == CALG_HMAC || Algid == CALG_SCHANNEL_MASTER_HASH ||
1833 Algid == CALG_TLS1PRF)
1835 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey)) {
1836 SetLastError(NTE_BAD_KEY);
1837 return FALSE;
1840 if ((Algid == CALG_MAC) && (GET_ALG_TYPE(pCryptKey->aiAlgid) != ALG_TYPE_BLOCK)) {
1841 SetLastError(NTE_BAD_KEY);
1842 return FALSE;
1845 if ((Algid == CALG_SCHANNEL_MASTER_HASH || Algid == CALG_TLS1PRF) &&
1846 (pCryptKey->aiAlgid != CALG_TLS1_MASTER))
1848 SetLastError(NTE_BAD_KEY);
1849 return FALSE;
1852 if ((Algid == CALG_TLS1PRF) && (pCryptKey->dwState != RSAENH_KEYSTATE_MASTERKEY)) {
1853 SetLastError(NTE_BAD_KEY_STATE);
1854 return FALSE;
1858 *phHash = new_object(&handle_table, sizeof(CRYPTHASH), RSAENH_MAGIC_HASH,
1859 destroy_hash, (OBJECTHDR**)&pCryptHash);
1860 if (!pCryptHash) return FALSE;
1862 pCryptHash->aiAlgid = Algid;
1863 pCryptHash->hKey = hKey;
1864 pCryptHash->hProv = hProv;
1865 pCryptHash->dwState = RSAENH_HASHSTATE_HASHING;
1866 pCryptHash->pHMACInfo = NULL;
1867 pCryptHash->dwHashSize = peaAlgidInfo->dwDefaultLen >> 3;
1868 init_data_blob(&pCryptHash->tpPRFParams.blobLabel);
1869 init_data_blob(&pCryptHash->tpPRFParams.blobSeed);
1871 if (Algid == CALG_SCHANNEL_MASTER_HASH) {
1872 static const char keyex[] = "key expansion";
1873 BYTE key_expansion[sizeof keyex];
1874 CRYPT_DATA_BLOB blobRandom, blobKeyExpansion = { 13, key_expansion };
1876 memcpy( key_expansion, keyex, sizeof keyex );
1878 if (pCryptKey->dwState != RSAENH_KEYSTATE_MASTERKEY) {
1879 static const char msec[] = "master secret";
1880 BYTE master_secret[sizeof msec];
1881 CRYPT_DATA_BLOB blobLabel = { 13, master_secret };
1882 BYTE abKeyValue[48];
1884 memcpy( master_secret, msec, sizeof msec );
1886 /* See RFC 2246, chapter 8.1 */
1887 if (!concat_data_blobs(&blobRandom,
1888 &pCryptKey->siSChannelInfo.blobClientRandom,
1889 &pCryptKey->siSChannelInfo.blobServerRandom))
1891 return FALSE;
1893 tls1_prf(hProv, hKey, &blobLabel, &blobRandom, abKeyValue, 48);
1894 pCryptKey->dwState = RSAENH_KEYSTATE_MASTERKEY;
1895 memcpy(pCryptKey->abKeyValue, abKeyValue, 48);
1896 free_data_blob(&blobRandom);
1899 /* See RFC 2246, chapter 6.3 */
1900 if (!concat_data_blobs(&blobRandom,
1901 &pCryptKey->siSChannelInfo.blobServerRandom,
1902 &pCryptKey->siSChannelInfo.blobClientRandom))
1904 return FALSE;
1906 tls1_prf(hProv, hKey, &blobKeyExpansion, &blobRandom, pCryptHash->abHashValue,
1907 RSAENH_MAX_HASH_SIZE);
1908 free_data_blob(&blobRandom);
1911 return init_hash(pCryptHash);
1914 /******************************************************************************
1915 * CPDestroyHash (RSAENH.@)
1917 * Releases the handle to a hash object. The object is destroyed if it's reference
1918 * count reaches zero.
1920 * PARAMS
1921 * hProv [I] Handle to the key container to which the hash object belongs.
1922 * hHash [I] Handle to the hash object to be released.
1924 * RETURNS
1925 * Success: TRUE
1926 * Failure: FALSE
1928 BOOL WINAPI RSAENH_CPDestroyHash(HCRYPTPROV hProv, HCRYPTHASH hHash)
1930 TRACE("(hProv=%08lx, hHash=%08lx)\n", hProv, hHash);
1932 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
1934 SetLastError(NTE_BAD_UID);
1935 return FALSE;
1938 if (!release_handle(&handle_table, hHash, RSAENH_MAGIC_HASH))
1940 SetLastError(NTE_BAD_HASH);
1941 return FALSE;
1944 return TRUE;
1947 /******************************************************************************
1948 * CPDestroyKey (RSAENH.@)
1950 * Releases the handle to a key object. The object is destroyed if it's reference
1951 * count reaches zero.
1953 * PARAMS
1954 * hProv [I] Handle to the key container to which the key object belongs.
1955 * hKey [I] Handle to the key object to be released.
1957 * RETURNS
1958 * Success: TRUE
1959 * Failure: FALSE
1961 BOOL WINAPI RSAENH_CPDestroyKey(HCRYPTPROV hProv, HCRYPTKEY hKey)
1963 TRACE("(hProv=%08lx, hKey=%08lx)\n", hProv, hKey);
1965 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
1967 SetLastError(NTE_BAD_UID);
1968 return FALSE;
1971 if (!release_handle(&handle_table, hKey, RSAENH_MAGIC_KEY))
1973 SetLastError(NTE_BAD_KEY);
1974 return FALSE;
1977 return TRUE;
1980 /******************************************************************************
1981 * CPDuplicateHash (RSAENH.@)
1983 * Clones a hash object including it's current state.
1985 * PARAMS
1986 * hUID [I] Handle to the key container the hash belongs to.
1987 * hHash [I] Handle to the hash object to be cloned.
1988 * pdwReserved [I] Reserved. Must be NULL.
1989 * dwFlags [I] No flags are currently defined. Must be 0.
1990 * phHash [O] Handle to the cloned hash object.
1992 * RETURNS
1993 * Success: TRUE.
1994 * Failure: FALSE.
1996 BOOL WINAPI RSAENH_CPDuplicateHash(HCRYPTPROV hUID, HCRYPTHASH hHash, DWORD *pdwReserved,
1997 DWORD dwFlags, HCRYPTHASH *phHash)
1999 CRYPTHASH *pSrcHash, *pDestHash;
2001 TRACE("(hUID=%08lx, hHash=%08lx, pdwReserved=%p, dwFlags=%08x, phHash=%p)\n", hUID, hHash,
2002 pdwReserved, dwFlags, phHash);
2004 if (!is_valid_handle(&handle_table, hUID, RSAENH_MAGIC_CONTAINER))
2006 SetLastError(NTE_BAD_UID);
2007 return FALSE;
2010 if (!lookup_handle(&handle_table, hHash, RSAENH_MAGIC_HASH, (OBJECTHDR**)&pSrcHash))
2012 SetLastError(NTE_BAD_HASH);
2013 return FALSE;
2016 if (!phHash || pdwReserved || dwFlags)
2018 SetLastError(ERROR_INVALID_PARAMETER);
2019 return FALSE;
2022 *phHash = new_object(&handle_table, sizeof(CRYPTHASH), RSAENH_MAGIC_HASH,
2023 destroy_hash, (OBJECTHDR**)&pDestHash);
2024 if (*phHash != (HCRYPTHASH)INVALID_HANDLE_VALUE)
2026 *pDestHash = *pSrcHash;
2027 duplicate_hash_impl(pSrcHash->aiAlgid, &pSrcHash->context, &pDestHash->context);
2028 copy_hmac_info(&pDestHash->pHMACInfo, pSrcHash->pHMACInfo);
2029 copy_data_blob(&pDestHash->tpPRFParams.blobLabel, &pSrcHash->tpPRFParams.blobLabel);
2030 copy_data_blob(&pDestHash->tpPRFParams.blobSeed, &pSrcHash->tpPRFParams.blobSeed);
2033 return *phHash != (HCRYPTHASH)INVALID_HANDLE_VALUE;
2036 /******************************************************************************
2037 * CPDuplicateKey (RSAENH.@)
2039 * Clones a key object including it's current state.
2041 * PARAMS
2042 * hUID [I] Handle to the key container the hash belongs to.
2043 * hKey [I] Handle to the key object to be cloned.
2044 * pdwReserved [I] Reserved. Must be NULL.
2045 * dwFlags [I] No flags are currently defined. Must be 0.
2046 * phHash [O] Handle to the cloned key object.
2048 * RETURNS
2049 * Success: TRUE.
2050 * Failure: FALSE.
2052 BOOL WINAPI RSAENH_CPDuplicateKey(HCRYPTPROV hUID, HCRYPTKEY hKey, DWORD *pdwReserved,
2053 DWORD dwFlags, HCRYPTKEY *phKey)
2055 CRYPTKEY *pSrcKey, *pDestKey;
2057 TRACE("(hUID=%08lx, hKey=%08lx, pdwReserved=%p, dwFlags=%08x, phKey=%p)\n", hUID, hKey,
2058 pdwReserved, dwFlags, phKey);
2060 if (!is_valid_handle(&handle_table, hUID, RSAENH_MAGIC_CONTAINER))
2062 SetLastError(NTE_BAD_UID);
2063 return FALSE;
2066 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pSrcKey))
2068 SetLastError(NTE_BAD_KEY);
2069 return FALSE;
2072 if (!phKey || pdwReserved || dwFlags)
2074 SetLastError(ERROR_INVALID_PARAMETER);
2075 return FALSE;
2078 *phKey = new_object(&handle_table, sizeof(CRYPTKEY), RSAENH_MAGIC_KEY, destroy_key,
2079 (OBJECTHDR**)&pDestKey);
2080 if (*phKey != (HCRYPTKEY)INVALID_HANDLE_VALUE)
2082 *pDestKey = *pSrcKey;
2083 copy_data_blob(&pDestKey->siSChannelInfo.blobServerRandom,
2084 &pSrcKey->siSChannelInfo.blobServerRandom);
2085 copy_data_blob(&pDestKey->siSChannelInfo.blobClientRandom,
2086 &pSrcKey->siSChannelInfo.blobClientRandom);
2087 duplicate_key_impl(pSrcKey->aiAlgid, &pSrcKey->context, &pDestKey->context);
2088 return TRUE;
2090 else
2092 return FALSE;
2096 /******************************************************************************
2097 * CPEncrypt (RSAENH.@)
2099 * Encrypt data.
2101 * PARAMS
2102 * hProv [I] The key container hKey and hHash belong to.
2103 * hKey [I] The key used to encrypt the data.
2104 * hHash [I] An optional hash object for parallel hashing. See notes.
2105 * Final [I] Indicates if this is the last block of data to encrypt.
2106 * dwFlags [I] Currently no flags defined. Must be zero.
2107 * pbData [I/O] Pointer to the data to encrypt. Encrypted data will also be stored there.
2108 * pdwDataLen [I/O] I: Length of data to encrypt, O: Length of encrypted data.
2109 * dwBufLen [I] Size of the buffer at pbData.
2111 * RETURNS
2112 * Success: TRUE.
2113 * Failure: FALSE.
2115 * NOTES
2116 * If a hash object handle is provided in hHash, it will be updated with the plaintext.
2117 * This is useful for message signatures.
2119 * This function uses the standard WINAPI protocol for querying data of dynamic length.
2121 BOOL WINAPI RSAENH_CPEncrypt(HCRYPTPROV hProv, HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final,
2122 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen, DWORD dwBufLen)
2124 CRYPTKEY *pCryptKey;
2125 BYTE *in, out[RSAENH_MAX_BLOCK_SIZE], o[RSAENH_MAX_BLOCK_SIZE];
2126 DWORD dwEncryptedLen, i, j, k;
2128 TRACE("(hProv=%08lx, hKey=%08lx, hHash=%08lx, Final=%d, dwFlags=%08x, pbData=%p, "
2129 "pdwDataLen=%p, dwBufLen=%d)\n", hProv, hKey, hHash, Final, dwFlags, pbData, pdwDataLen,
2130 dwBufLen);
2132 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
2134 SetLastError(NTE_BAD_UID);
2135 return FALSE;
2138 if (dwFlags)
2140 SetLastError(NTE_BAD_FLAGS);
2141 return FALSE;
2144 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
2146 SetLastError(NTE_BAD_KEY);
2147 return FALSE;
2150 if (pCryptKey->dwState == RSAENH_KEYSTATE_IDLE)
2151 pCryptKey->dwState = RSAENH_KEYSTATE_ENCRYPTING;
2153 if (pCryptKey->dwState != RSAENH_KEYSTATE_ENCRYPTING)
2155 SetLastError(NTE_BAD_DATA);
2156 return FALSE;
2159 if (is_valid_handle(&handle_table, hHash, RSAENH_MAGIC_HASH)) {
2160 if (!RSAENH_CPHashData(hProv, hHash, pbData, *pdwDataLen, 0)) return FALSE;
2163 if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_BLOCK) {
2164 if (!Final && (*pdwDataLen % pCryptKey->dwBlockLen)) {
2165 SetLastError(NTE_BAD_DATA);
2166 return FALSE;
2169 dwEncryptedLen = (*pdwDataLen/pCryptKey->dwBlockLen+(Final?1:0))*pCryptKey->dwBlockLen;
2171 if (pbData == NULL) {
2172 *pdwDataLen = dwEncryptedLen;
2173 return TRUE;
2175 else if (dwEncryptedLen > dwBufLen) {
2176 *pdwDataLen = dwEncryptedLen;
2177 SetLastError(ERROR_MORE_DATA);
2178 return FALSE;
2181 /* Pad final block with length bytes */
2182 for (i=*pdwDataLen; i<dwEncryptedLen; i++) pbData[i] = dwEncryptedLen - *pdwDataLen;
2183 *pdwDataLen = dwEncryptedLen;
2185 for (i=0, in=pbData; i<*pdwDataLen; i+=pCryptKey->dwBlockLen, in+=pCryptKey->dwBlockLen) {
2186 switch (pCryptKey->dwMode) {
2187 case CRYPT_MODE_ECB:
2188 encrypt_block_impl(pCryptKey->aiAlgid, 0, &pCryptKey->context, in, out,
2189 RSAENH_ENCRYPT);
2190 break;
2192 case CRYPT_MODE_CBC:
2193 for (j=0; j<pCryptKey->dwBlockLen; j++) in[j] ^= pCryptKey->abChainVector[j];
2194 encrypt_block_impl(pCryptKey->aiAlgid, 0, &pCryptKey->context, in, out,
2195 RSAENH_ENCRYPT);
2196 memcpy(pCryptKey->abChainVector, out, pCryptKey->dwBlockLen);
2197 break;
2199 case CRYPT_MODE_CFB:
2200 for (j=0; j<pCryptKey->dwBlockLen; j++) {
2201 encrypt_block_impl(pCryptKey->aiAlgid, 0, &pCryptKey->context,
2202 pCryptKey->abChainVector, o, RSAENH_ENCRYPT);
2203 out[j] = in[j] ^ o[0];
2204 for (k=0; k<pCryptKey->dwBlockLen-1; k++)
2205 pCryptKey->abChainVector[k] = pCryptKey->abChainVector[k+1];
2206 pCryptKey->abChainVector[k] = out[j];
2208 break;
2210 default:
2211 SetLastError(NTE_BAD_ALGID);
2212 return FALSE;
2214 memcpy(in, out, pCryptKey->dwBlockLen);
2216 } else if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_STREAM) {
2217 if (pbData == NULL) {
2218 *pdwDataLen = dwBufLen;
2219 return TRUE;
2221 encrypt_stream_impl(pCryptKey->aiAlgid, &pCryptKey->context, pbData, *pdwDataLen);
2222 } else if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_RSA) {
2223 if (pCryptKey->aiAlgid == CALG_RSA_SIGN) {
2224 SetLastError(NTE_BAD_KEY);
2225 return FALSE;
2227 if (!pbData) {
2228 *pdwDataLen = pCryptKey->dwBlockLen;
2229 return TRUE;
2231 if (dwBufLen < pCryptKey->dwBlockLen) {
2232 SetLastError(ERROR_MORE_DATA);
2233 return FALSE;
2235 if (!pad_data(pbData, *pdwDataLen, pbData, pCryptKey->dwBlockLen, dwFlags)) return FALSE;
2236 encrypt_block_impl(pCryptKey->aiAlgid, PK_PUBLIC, &pCryptKey->context, pbData, pbData, RSAENH_ENCRYPT);
2237 *pdwDataLen = pCryptKey->dwBlockLen;
2238 Final = TRUE;
2239 } else {
2240 SetLastError(NTE_BAD_TYPE);
2241 return FALSE;
2244 if (Final) setup_key(pCryptKey);
2246 return TRUE;
2249 /******************************************************************************
2250 * CPDecrypt (RSAENH.@)
2252 * Decrypt data.
2254 * PARAMS
2255 * hProv [I] The key container hKey and hHash belong to.
2256 * hKey [I] The key used to decrypt the data.
2257 * hHash [I] An optional hash object for parallel hashing. See notes.
2258 * Final [I] Indicates if this is the last block of data to decrypt.
2259 * dwFlags [I] Currently no flags defined. Must be zero.
2260 * pbData [I/O] Pointer to the data to decrypt. Plaintext will also be stored there.
2261 * pdwDataLen [I/O] I: Length of ciphertext, O: Length of plaintext.
2263 * RETURNS
2264 * Success: TRUE.
2265 * Failure: FALSE.
2267 * NOTES
2268 * If a hash object handle is provided in hHash, it will be updated with the plaintext.
2269 * This is useful for message signatures.
2271 * This function uses the standard WINAPI protocol for querying data of dynamic length.
2273 BOOL WINAPI RSAENH_CPDecrypt(HCRYPTPROV hProv, HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final,
2274 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
2276 CRYPTKEY *pCryptKey;
2277 BYTE *in, out[RSAENH_MAX_BLOCK_SIZE], o[RSAENH_MAX_BLOCK_SIZE];
2278 DWORD i, j, k;
2279 DWORD dwMax;
2281 TRACE("(hProv=%08lx, hKey=%08lx, hHash=%08lx, Final=%d, dwFlags=%08x, pbData=%p, "
2282 "pdwDataLen=%p)\n", hProv, hKey, hHash, Final, dwFlags, pbData, pdwDataLen);
2284 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
2286 SetLastError(NTE_BAD_UID);
2287 return FALSE;
2290 if (dwFlags)
2292 SetLastError(NTE_BAD_FLAGS);
2293 return FALSE;
2296 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
2298 SetLastError(NTE_BAD_KEY);
2299 return FALSE;
2302 if (pCryptKey->dwState == RSAENH_KEYSTATE_IDLE)
2303 pCryptKey->dwState = RSAENH_KEYSTATE_ENCRYPTING;
2305 if (pCryptKey->dwState != RSAENH_KEYSTATE_ENCRYPTING)
2307 SetLastError(NTE_BAD_DATA);
2308 return FALSE;
2311 dwMax=*pdwDataLen;
2313 if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_BLOCK) {
2314 for (i=0, in=pbData; i<*pdwDataLen; i+=pCryptKey->dwBlockLen, in+=pCryptKey->dwBlockLen) {
2315 switch (pCryptKey->dwMode) {
2316 case CRYPT_MODE_ECB:
2317 encrypt_block_impl(pCryptKey->aiAlgid, 0, &pCryptKey->context, in, out,
2318 RSAENH_DECRYPT);
2319 break;
2321 case CRYPT_MODE_CBC:
2322 encrypt_block_impl(pCryptKey->aiAlgid, 0, &pCryptKey->context, in, out,
2323 RSAENH_DECRYPT);
2324 for (j=0; j<pCryptKey->dwBlockLen; j++) out[j] ^= pCryptKey->abChainVector[j];
2325 memcpy(pCryptKey->abChainVector, in, pCryptKey->dwBlockLen);
2326 break;
2328 case CRYPT_MODE_CFB:
2329 for (j=0; j<pCryptKey->dwBlockLen; j++) {
2330 encrypt_block_impl(pCryptKey->aiAlgid, 0, &pCryptKey->context,
2331 pCryptKey->abChainVector, o, RSAENH_ENCRYPT);
2332 out[j] = in[j] ^ o[0];
2333 for (k=0; k<pCryptKey->dwBlockLen-1; k++)
2334 pCryptKey->abChainVector[k] = pCryptKey->abChainVector[k+1];
2335 pCryptKey->abChainVector[k] = in[j];
2337 break;
2339 default:
2340 SetLastError(NTE_BAD_ALGID);
2341 return FALSE;
2343 memcpy(in, out, pCryptKey->dwBlockLen);
2345 if (Final) {
2346 if (pbData[*pdwDataLen-1] &&
2347 pbData[*pdwDataLen-1] <= pCryptKey->dwBlockLen &&
2348 pbData[*pdwDataLen-1] <= *pdwDataLen) {
2349 BOOL padOkay = TRUE;
2351 /* check that every bad byte has the same value */
2352 for (i = 1; padOkay && i < pbData[*pdwDataLen-1]; i++)
2353 if (pbData[*pdwDataLen - i - 1] != pbData[*pdwDataLen - 1])
2354 padOkay = FALSE;
2355 if (padOkay)
2356 *pdwDataLen -= pbData[*pdwDataLen-1];
2357 else {
2358 SetLastError(NTE_BAD_DATA);
2359 return FALSE;
2362 else {
2363 SetLastError(NTE_BAD_DATA);
2364 return FALSE;
2368 } else if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_STREAM) {
2369 encrypt_stream_impl(pCryptKey->aiAlgid, &pCryptKey->context, pbData, *pdwDataLen);
2370 } else if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_RSA) {
2371 if (pCryptKey->aiAlgid == CALG_RSA_SIGN) {
2372 SetLastError(NTE_BAD_KEY);
2373 return FALSE;
2375 encrypt_block_impl(pCryptKey->aiAlgid, PK_PRIVATE, &pCryptKey->context, pbData, pbData, RSAENH_DECRYPT);
2376 if (!unpad_data(pbData, pCryptKey->dwBlockLen, pbData, pdwDataLen, dwFlags)) return FALSE;
2377 Final = TRUE;
2378 } else {
2379 SetLastError(NTE_BAD_TYPE);
2380 return FALSE;
2383 if (Final) setup_key(pCryptKey);
2385 if (is_valid_handle(&handle_table, hHash, RSAENH_MAGIC_HASH)) {
2386 if (*pdwDataLen>dwMax ||
2387 !RSAENH_CPHashData(hProv, hHash, pbData, *pdwDataLen, 0)) return FALSE;
2390 return TRUE;
2393 static BOOL crypt_export_simple(CRYPTKEY *pCryptKey, CRYPTKEY *pPubKey,
2394 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
2396 BLOBHEADER *pBlobHeader = (BLOBHEADER*)pbData;
2397 ALG_ID *pAlgid = (ALG_ID*)(pBlobHeader+1);
2398 DWORD dwDataLen;
2400 if (!(GET_ALG_CLASS(pCryptKey->aiAlgid)&(ALG_CLASS_DATA_ENCRYPT|ALG_CLASS_MSG_ENCRYPT))) {
2401 SetLastError(NTE_BAD_KEY); /* FIXME: error code? */
2402 return FALSE;
2405 dwDataLen = sizeof(BLOBHEADER) + sizeof(ALG_ID) + pPubKey->dwBlockLen;
2406 if (pbData) {
2407 if (*pdwDataLen < dwDataLen) {
2408 SetLastError(ERROR_MORE_DATA);
2409 *pdwDataLen = dwDataLen;
2410 return FALSE;
2413 pBlobHeader->bType = SIMPLEBLOB;
2414 pBlobHeader->bVersion = CUR_BLOB_VERSION;
2415 pBlobHeader->reserved = 0;
2416 pBlobHeader->aiKeyAlg = pCryptKey->aiAlgid;
2418 *pAlgid = pPubKey->aiAlgid;
2420 if (!pad_data(pCryptKey->abKeyValue, pCryptKey->dwKeyLen, (BYTE*)(pAlgid+1),
2421 pPubKey->dwBlockLen, dwFlags))
2423 return FALSE;
2426 encrypt_block_impl(pPubKey->aiAlgid, PK_PUBLIC, &pPubKey->context, (BYTE*)(pAlgid+1),
2427 (BYTE*)(pAlgid+1), RSAENH_ENCRYPT);
2429 *pdwDataLen = dwDataLen;
2430 return TRUE;
2433 static BOOL crypt_export_public_key(CRYPTKEY *pCryptKey, BYTE *pbData,
2434 DWORD *pdwDataLen)
2436 BLOBHEADER *pBlobHeader = (BLOBHEADER*)pbData;
2437 RSAPUBKEY *pRSAPubKey = (RSAPUBKEY*)(pBlobHeader+1);
2438 DWORD dwDataLen;
2440 if ((pCryptKey->aiAlgid != CALG_RSA_KEYX) && (pCryptKey->aiAlgid != CALG_RSA_SIGN)) {
2441 SetLastError(NTE_BAD_KEY);
2442 return FALSE;
2445 dwDataLen = sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) + pCryptKey->dwKeyLen;
2446 if (pbData) {
2447 if (*pdwDataLen < dwDataLen) {
2448 SetLastError(ERROR_MORE_DATA);
2449 *pdwDataLen = dwDataLen;
2450 return FALSE;
2453 pBlobHeader->bType = PUBLICKEYBLOB;
2454 pBlobHeader->bVersion = CUR_BLOB_VERSION;
2455 pBlobHeader->reserved = 0;
2456 pBlobHeader->aiKeyAlg = pCryptKey->aiAlgid;
2458 pRSAPubKey->magic = RSAENH_MAGIC_RSA1;
2459 pRSAPubKey->bitlen = pCryptKey->dwKeyLen << 3;
2461 export_public_key_impl((BYTE*)(pRSAPubKey+1), &pCryptKey->context,
2462 pCryptKey->dwKeyLen, &pRSAPubKey->pubexp);
2464 *pdwDataLen = dwDataLen;
2465 return TRUE;
2468 static BOOL crypt_export_private_key(CRYPTKEY *pCryptKey, BOOL force,
2469 BYTE *pbData, DWORD *pdwDataLen)
2471 BLOBHEADER *pBlobHeader = (BLOBHEADER*)pbData;
2472 RSAPUBKEY *pRSAPubKey = (RSAPUBKEY*)(pBlobHeader+1);
2473 DWORD dwDataLen;
2475 if ((pCryptKey->aiAlgid != CALG_RSA_KEYX) && (pCryptKey->aiAlgid != CALG_RSA_SIGN)) {
2476 SetLastError(NTE_BAD_KEY);
2477 return FALSE;
2479 if (!force && !(pCryptKey->dwPermissions & CRYPT_EXPORT))
2481 SetLastError(NTE_BAD_KEY_STATE);
2482 return FALSE;
2485 dwDataLen = sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) +
2486 2 * pCryptKey->dwKeyLen + 5 * ((pCryptKey->dwKeyLen + 1) >> 1);
2487 if (pbData) {
2488 if (*pdwDataLen < dwDataLen) {
2489 SetLastError(ERROR_MORE_DATA);
2490 *pdwDataLen = dwDataLen;
2491 return FALSE;
2494 pBlobHeader->bType = PRIVATEKEYBLOB;
2495 pBlobHeader->bVersion = CUR_BLOB_VERSION;
2496 pBlobHeader->reserved = 0;
2497 pBlobHeader->aiKeyAlg = pCryptKey->aiAlgid;
2499 pRSAPubKey->magic = RSAENH_MAGIC_RSA2;
2500 pRSAPubKey->bitlen = pCryptKey->dwKeyLen << 3;
2502 export_private_key_impl((BYTE*)(pRSAPubKey+1), &pCryptKey->context,
2503 pCryptKey->dwKeyLen, &pRSAPubKey->pubexp);
2505 *pdwDataLen = dwDataLen;
2506 return TRUE;
2509 static BOOL crypt_export_plaintext_key(CRYPTKEY *pCryptKey, BYTE *pbData,
2510 DWORD *pdwDataLen)
2512 BLOBHEADER *pBlobHeader = (BLOBHEADER*)pbData;
2513 DWORD *pKeyLen = (DWORD*)(pBlobHeader+1);
2514 BYTE *pbKey = (BYTE*)(pKeyLen+1);
2515 DWORD dwDataLen;
2517 dwDataLen = sizeof(BLOBHEADER) + sizeof(DWORD) + pCryptKey->dwKeyLen;
2518 if (pbData) {
2519 if (*pdwDataLen < dwDataLen) {
2520 SetLastError(ERROR_MORE_DATA);
2521 *pdwDataLen = dwDataLen;
2522 return FALSE;
2525 pBlobHeader->bType = PLAINTEXTKEYBLOB;
2526 pBlobHeader->bVersion = CUR_BLOB_VERSION;
2527 pBlobHeader->reserved = 0;
2528 pBlobHeader->aiKeyAlg = pCryptKey->aiAlgid;
2530 *pKeyLen = pCryptKey->dwKeyLen;
2531 memcpy(pbKey, &pCryptKey->abKeyValue, pCryptKey->dwKeyLen);
2533 *pdwDataLen = dwDataLen;
2534 return TRUE;
2536 /******************************************************************************
2537 * crypt_export_key [Internal]
2539 * Export a key into a binary large object (BLOB). Called by CPExportKey and
2540 * by store_key_pair.
2542 * PARAMS
2543 * pCryptKey [I] Key to be exported.
2544 * hPubKey [I] Key used to encrypt sensitive BLOB data.
2545 * dwBlobType [I] SIMPLEBLOB, PUBLICKEYBLOB or PRIVATEKEYBLOB.
2546 * dwFlags [I] Currently none defined.
2547 * force [I] If TRUE, the key is written no matter what the key's
2548 * permissions are. Otherwise the key's permissions are
2549 * checked before exporting.
2550 * pbData [O] Pointer to a buffer where the BLOB will be written to.
2551 * pdwDataLen [I/O] I: Size of buffer at pbData, O: Size of BLOB
2553 * RETURNS
2554 * Success: TRUE.
2555 * Failure: FALSE.
2557 static BOOL crypt_export_key(CRYPTKEY *pCryptKey, HCRYPTKEY hPubKey,
2558 DWORD dwBlobType, DWORD dwFlags, BOOL force,
2559 BYTE *pbData, DWORD *pdwDataLen)
2561 CRYPTKEY *pPubKey;
2563 if (dwFlags & CRYPT_SSL2_FALLBACK) {
2564 if (pCryptKey->aiAlgid != CALG_SSL2_MASTER) {
2565 SetLastError(NTE_BAD_KEY);
2566 return FALSE;
2570 switch ((BYTE)dwBlobType)
2572 case SIMPLEBLOB:
2573 if (!lookup_handle(&handle_table, hPubKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pPubKey)){
2574 SetLastError(NTE_BAD_PUBLIC_KEY); /* FIXME: error_code? */
2575 return FALSE;
2577 return crypt_export_simple(pCryptKey, pPubKey, dwFlags, pbData,
2578 pdwDataLen);
2580 case PUBLICKEYBLOB:
2581 if (is_valid_handle(&handle_table, hPubKey, RSAENH_MAGIC_KEY)) {
2582 SetLastError(NTE_BAD_KEY); /* FIXME: error code? */
2583 return FALSE;
2586 return crypt_export_public_key(pCryptKey, pbData, pdwDataLen);
2588 case PRIVATEKEYBLOB:
2589 return crypt_export_private_key(pCryptKey, force, pbData, pdwDataLen);
2591 case PLAINTEXTKEYBLOB:
2592 return crypt_export_plaintext_key(pCryptKey, pbData, pdwDataLen);
2594 default:
2595 SetLastError(NTE_BAD_TYPE); /* FIXME: error code? */
2596 return FALSE;
2600 /******************************************************************************
2601 * CPExportKey (RSAENH.@)
2603 * Export a key into a binary large object (BLOB).
2605 * PARAMS
2606 * hProv [I] Key container from which a key is to be exported.
2607 * hKey [I] Key to be exported.
2608 * hPubKey [I] Key used to encrypt sensitive BLOB data.
2609 * dwBlobType [I] SIMPLEBLOB, PUBLICKEYBLOB or PRIVATEKEYBLOB.
2610 * dwFlags [I] Currently none defined.
2611 * pbData [O] Pointer to a buffer where the BLOB will be written to.
2612 * pdwDataLen [I/O] I: Size of buffer at pbData, O: Size of BLOB
2614 * RETURNS
2615 * Success: TRUE.
2616 * Failure: FALSE.
2618 BOOL WINAPI RSAENH_CPExportKey(HCRYPTPROV hProv, HCRYPTKEY hKey, HCRYPTKEY hPubKey,
2619 DWORD dwBlobType, DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
2621 CRYPTKEY *pCryptKey;
2623 TRACE("(hProv=%08lx, hKey=%08lx, hPubKey=%08lx, dwBlobType=%08x, dwFlags=%08x, pbData=%p,"
2624 "pdwDataLen=%p)\n", hProv, hKey, hPubKey, dwBlobType, dwFlags, pbData, pdwDataLen);
2626 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
2628 SetLastError(NTE_BAD_UID);
2629 return FALSE;
2632 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
2634 SetLastError(NTE_BAD_KEY);
2635 return FALSE;
2638 return crypt_export_key(pCryptKey, hPubKey, dwBlobType, dwFlags, FALSE,
2639 pbData, pdwDataLen);
2642 /******************************************************************************
2643 * release_and_install_key [Internal]
2645 * Release an existing key, if present, and replaces it with a new one.
2647 * PARAMS
2648 * hProv [I] Key container into which the key is to be imported.
2649 * src [I] Key which will replace *dest
2650 * dest [I] Points to key to be released and replaced with src
2651 * fStoreKey [I] If TRUE, the newly installed key is stored to the registry.
2653 static void release_and_install_key(HCRYPTPROV hProv, HCRYPTKEY src,
2654 HCRYPTKEY *dest, DWORD fStoreKey)
2656 RSAENH_CPDestroyKey(hProv, *dest);
2657 copy_handle(&handle_table, src, RSAENH_MAGIC_KEY, dest);
2658 if (fStoreKey)
2660 KEYCONTAINER *pKeyContainer;
2662 if (lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER,
2663 (OBJECTHDR**)&pKeyContainer))
2665 store_key_container_keys(pKeyContainer);
2666 store_key_container_permissions(pKeyContainer);
2671 /******************************************************************************
2672 * import_private_key [Internal]
2674 * Import a BLOB'ed private key into a key container.
2676 * PARAMS
2677 * hProv [I] Key container into which the private key is to be imported.
2678 * pbData [I] Pointer to a buffer which holds the private key BLOB.
2679 * dwDataLen [I] Length of data in buffer at pbData.
2680 * dwFlags [I] One of:
2681 * CRYPT_EXPORTABLE: the imported key is marked exportable
2682 * fStoreKey [I] If TRUE, the imported key is stored to the registry.
2683 * phKey [O] Handle to the imported key.
2686 * NOTES
2687 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2688 * it's a PRIVATEKEYBLOB.
2690 * RETURNS
2691 * Success: TRUE.
2692 * Failure: FALSE.
2694 static BOOL import_private_key(HCRYPTPROV hProv, CONST BYTE *pbData, DWORD dwDataLen,
2695 DWORD dwFlags, BOOL fStoreKey, HCRYPTKEY *phKey)
2697 KEYCONTAINER *pKeyContainer;
2698 CRYPTKEY *pCryptKey;
2699 CONST BLOBHEADER *pBlobHeader = (CONST BLOBHEADER*)pbData;
2700 CONST RSAPUBKEY *pRSAPubKey = (CONST RSAPUBKEY*)(pBlobHeader+1);
2701 BOOL ret;
2703 if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER,
2704 (OBJECTHDR**)&pKeyContainer))
2706 SetLastError(NTE_BAD_UID);
2707 return FALSE;
2710 if ((dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY)) ||
2711 (pRSAPubKey->magic != RSAENH_MAGIC_RSA2) ||
2712 (dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) +
2713 (2 * pRSAPubKey->bitlen >> 3) + (5 * ((pRSAPubKey->bitlen+8)>>4))))
2715 SetLastError(NTE_BAD_DATA);
2716 return FALSE;
2719 *phKey = new_key(hProv, pBlobHeader->aiKeyAlg, MAKELONG(0,pRSAPubKey->bitlen), &pCryptKey);
2720 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) return FALSE;
2721 setup_key(pCryptKey);
2722 ret = import_private_key_impl((CONST BYTE*)(pRSAPubKey+1), &pCryptKey->context,
2723 pRSAPubKey->bitlen/8, pRSAPubKey->pubexp);
2724 if (ret) {
2725 if (dwFlags & CRYPT_EXPORTABLE)
2726 pCryptKey->dwPermissions |= CRYPT_EXPORT;
2727 switch (pBlobHeader->aiKeyAlg)
2729 case AT_SIGNATURE:
2730 case CALG_RSA_SIGN:
2731 TRACE("installing signing key\n");
2732 release_and_install_key(hProv, *phKey, &pKeyContainer->hSignatureKeyPair,
2733 fStoreKey);
2734 break;
2735 case AT_KEYEXCHANGE:
2736 case CALG_RSA_KEYX:
2737 TRACE("installing key exchange key\n");
2738 release_and_install_key(hProv, *phKey, &pKeyContainer->hKeyExchangeKeyPair,
2739 fStoreKey);
2740 break;
2743 return ret;
2746 /******************************************************************************
2747 * import_public_key [Internal]
2749 * Import a BLOB'ed public key into a key container.
2751 * PARAMS
2752 * hProv [I] Key container into which the public key is to be imported.
2753 * pbData [I] Pointer to a buffer which holds the public key BLOB.
2754 * dwDataLen [I] Length of data in buffer at pbData.
2755 * dwFlags [I] One of:
2756 * CRYPT_EXPORTABLE: the imported key is marked exportable
2757 * fStoreKey [I] If TRUE, the imported key is stored to the registry.
2758 * phKey [O] Handle to the imported key.
2761 * NOTES
2762 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2763 * it's a PUBLICKEYBLOB.
2765 * RETURNS
2766 * Success: TRUE.
2767 * Failure: FALSE.
2769 static BOOL import_public_key(HCRYPTPROV hProv, CONST BYTE *pbData, DWORD dwDataLen,
2770 DWORD dwFlags, BOOL fStoreKey, HCRYPTKEY *phKey)
2772 KEYCONTAINER *pKeyContainer;
2773 CRYPTKEY *pCryptKey;
2774 CONST BLOBHEADER *pBlobHeader = (CONST BLOBHEADER*)pbData;
2775 CONST RSAPUBKEY *pRSAPubKey = (CONST RSAPUBKEY*)(pBlobHeader+1);
2776 ALG_ID algID;
2777 BOOL ret;
2779 if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER,
2780 (OBJECTHDR**)&pKeyContainer))
2782 SetLastError(NTE_BAD_UID);
2783 return FALSE;
2786 if ((dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY)) ||
2787 (pRSAPubKey->magic != RSAENH_MAGIC_RSA1) ||
2788 (dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) + (pRSAPubKey->bitlen >> 3)))
2790 SetLastError(NTE_BAD_DATA);
2791 return FALSE;
2794 /* Since this is a public key blob, only the public key is
2795 * available, so only signature verification is possible.
2797 algID = pBlobHeader->aiKeyAlg;
2798 *phKey = new_key(hProv, algID, MAKELONG(0,pRSAPubKey->bitlen), &pCryptKey);
2799 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) return FALSE;
2800 setup_key(pCryptKey);
2801 ret = import_public_key_impl((CONST BYTE*)(pRSAPubKey+1), &pCryptKey->context,
2802 pRSAPubKey->bitlen >> 3, pRSAPubKey->pubexp);
2803 if (ret) {
2804 if (dwFlags & CRYPT_EXPORTABLE)
2805 pCryptKey->dwPermissions |= CRYPT_EXPORT;
2806 switch (pBlobHeader->aiKeyAlg)
2808 case AT_KEYEXCHANGE:
2809 case CALG_RSA_KEYX:
2810 TRACE("installing public key\n");
2811 release_and_install_key(hProv, *phKey, &pKeyContainer->hKeyExchangeKeyPair,
2812 fStoreKey);
2813 break;
2816 return ret;
2819 /******************************************************************************
2820 * import_symmetric_key [Internal]
2822 * Import a BLOB'ed symmetric key into a key container.
2824 * PARAMS
2825 * hProv [I] Key container into which the symmetric key is to be imported.
2826 * pbData [I] Pointer to a buffer which holds the symmetric key BLOB.
2827 * dwDataLen [I] Length of data in buffer at pbData.
2828 * hPubKey [I] Key used to decrypt sensitive BLOB data.
2829 * dwFlags [I] One of:
2830 * CRYPT_EXPORTABLE: the imported key is marked exportable
2831 * phKey [O] Handle to the imported key.
2834 * NOTES
2835 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2836 * it's a SIMPLEBLOB.
2838 * RETURNS
2839 * Success: TRUE.
2840 * Failure: FALSE.
2842 static BOOL import_symmetric_key(HCRYPTPROV hProv, CONST BYTE *pbData,
2843 DWORD dwDataLen, HCRYPTKEY hPubKey,
2844 DWORD dwFlags, HCRYPTKEY *phKey)
2846 CRYPTKEY *pCryptKey, *pPubKey;
2847 CONST BLOBHEADER *pBlobHeader = (CONST BLOBHEADER*)pbData;
2848 CONST ALG_ID *pAlgid = (CONST ALG_ID*)(pBlobHeader+1);
2849 CONST BYTE *pbKeyStream = (CONST BYTE*)(pAlgid + 1);
2850 BYTE *pbDecrypted;
2851 DWORD dwKeyLen;
2853 if (!lookup_handle(&handle_table, hPubKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pPubKey) ||
2854 pPubKey->aiAlgid != CALG_RSA_KEYX)
2856 SetLastError(NTE_BAD_PUBLIC_KEY); /* FIXME: error code? */
2857 return FALSE;
2860 if (dwDataLen < sizeof(BLOBHEADER)+sizeof(ALG_ID)+pPubKey->dwBlockLen)
2862 SetLastError(NTE_BAD_DATA); /* FIXME: error code */
2863 return FALSE;
2866 pbDecrypted = HeapAlloc(GetProcessHeap(), 0, pPubKey->dwBlockLen);
2867 if (!pbDecrypted) return FALSE;
2868 encrypt_block_impl(pPubKey->aiAlgid, PK_PRIVATE, &pPubKey->context, pbKeyStream, pbDecrypted,
2869 RSAENH_DECRYPT);
2871 dwKeyLen = RSAENH_MAX_KEY_SIZE;
2872 if (!unpad_data(pbDecrypted, pPubKey->dwBlockLen, pbDecrypted, &dwKeyLen, dwFlags)) {
2873 HeapFree(GetProcessHeap(), 0, pbDecrypted);
2874 return FALSE;
2877 *phKey = new_key(hProv, pBlobHeader->aiKeyAlg, dwKeyLen<<19, &pCryptKey);
2878 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE)
2880 HeapFree(GetProcessHeap(), 0, pbDecrypted);
2881 return FALSE;
2883 memcpy(pCryptKey->abKeyValue, pbDecrypted, dwKeyLen);
2884 HeapFree(GetProcessHeap(), 0, pbDecrypted);
2885 setup_key(pCryptKey);
2886 if (dwFlags & CRYPT_EXPORTABLE)
2887 pCryptKey->dwPermissions |= CRYPT_EXPORT;
2888 return TRUE;
2891 /******************************************************************************
2892 * import_plaintext_key [Internal]
2894 * Import a plaintext key into a key container.
2896 * PARAMS
2897 * hProv [I] Key container into which the symmetric key is to be imported.
2898 * pbData [I] Pointer to a buffer which holds the plaintext key BLOB.
2899 * dwDataLen [I] Length of data in buffer at pbData.
2900 * dwFlags [I] One of:
2901 * CRYPT_EXPORTABLE: the imported key is marked exportable
2902 * phKey [O] Handle to the imported key.
2905 * NOTES
2906 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2907 * it's a PLAINTEXTKEYBLOB.
2909 * RETURNS
2910 * Success: TRUE.
2911 * Failure: FALSE.
2913 static BOOL import_plaintext_key(HCRYPTPROV hProv, CONST BYTE *pbData,
2914 DWORD dwDataLen, DWORD dwFlags,
2915 HCRYPTKEY *phKey)
2917 CRYPTKEY *pCryptKey;
2918 CONST BLOBHEADER *pBlobHeader = (CONST BLOBHEADER*)pbData;
2919 CONST DWORD *pKeyLen = (CONST DWORD *)(pBlobHeader + 1);
2920 CONST BYTE *pbKeyStream = (CONST BYTE*)(pKeyLen + 1);
2922 if (dwDataLen < sizeof(BLOBHEADER)+sizeof(DWORD)+*pKeyLen)
2924 SetLastError(NTE_BAD_DATA); /* FIXME: error code */
2925 return FALSE;
2928 *phKey = new_key(hProv, pBlobHeader->aiKeyAlg, *pKeyLen<<19, &pCryptKey);
2929 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE)
2930 return FALSE;
2931 memcpy(pCryptKey->abKeyValue, pbKeyStream, *pKeyLen);
2932 setup_key(pCryptKey);
2933 if (dwFlags & CRYPT_EXPORTABLE)
2934 pCryptKey->dwPermissions |= CRYPT_EXPORT;
2935 return TRUE;
2938 /******************************************************************************
2939 * import_key [Internal]
2941 * Import a BLOB'ed key into a key container, optionally storing the key's
2942 * value to the registry.
2944 * PARAMS
2945 * hProv [I] Key container into which the key is to be imported.
2946 * pbData [I] Pointer to a buffer which holds the BLOB.
2947 * dwDataLen [I] Length of data in buffer at pbData.
2948 * hPubKey [I] Key used to decrypt sensitive BLOB data.
2949 * dwFlags [I] One of:
2950 * CRYPT_EXPORTABLE: the imported key is marked exportable
2951 * fStoreKey [I] If TRUE, the imported key is stored to the registry.
2952 * phKey [O] Handle to the imported key.
2954 * RETURNS
2955 * Success: TRUE.
2956 * Failure: FALSE.
2958 static BOOL import_key(HCRYPTPROV hProv, CONST BYTE *pbData, DWORD dwDataLen,
2959 HCRYPTKEY hPubKey, DWORD dwFlags, BOOL fStoreKey,
2960 HCRYPTKEY *phKey)
2962 KEYCONTAINER *pKeyContainer;
2963 CONST BLOBHEADER *pBlobHeader = (CONST BLOBHEADER*)pbData;
2965 if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER,
2966 (OBJECTHDR**)&pKeyContainer))
2968 SetLastError(NTE_BAD_UID);
2969 return FALSE;
2972 if (dwDataLen < sizeof(BLOBHEADER) ||
2973 pBlobHeader->bVersion != CUR_BLOB_VERSION ||
2974 pBlobHeader->reserved != 0)
2976 TRACE("bVersion = %d, reserved = %d\n", pBlobHeader->bVersion,
2977 pBlobHeader->reserved);
2978 SetLastError(NTE_BAD_DATA);
2979 return FALSE;
2982 /* If this is a verify-only context, the key is not persisted regardless of
2983 * fStoreKey's original value.
2985 fStoreKey = fStoreKey && !(dwFlags & CRYPT_VERIFYCONTEXT);
2986 TRACE("blob type: %x\n", pBlobHeader->bType);
2987 switch (pBlobHeader->bType)
2989 case PRIVATEKEYBLOB:
2990 return import_private_key(hProv, pbData, dwDataLen, dwFlags,
2991 fStoreKey, phKey);
2993 case PUBLICKEYBLOB:
2994 return import_public_key(hProv, pbData, dwDataLen, dwFlags,
2995 fStoreKey, phKey);
2997 case SIMPLEBLOB:
2998 return import_symmetric_key(hProv, pbData, dwDataLen, hPubKey,
2999 dwFlags, phKey);
3001 case PLAINTEXTKEYBLOB:
3002 return import_plaintext_key(hProv, pbData, dwDataLen, dwFlags,
3003 phKey);
3005 default:
3006 SetLastError(NTE_BAD_TYPE); /* FIXME: error code? */
3007 return FALSE;
3011 /******************************************************************************
3012 * CPImportKey (RSAENH.@)
3014 * Import a BLOB'ed key into a key container.
3016 * PARAMS
3017 * hProv [I] Key container into which the key is to be imported.
3018 * pbData [I] Pointer to a buffer which holds the BLOB.
3019 * dwDataLen [I] Length of data in buffer at pbData.
3020 * hPubKey [I] Key used to decrypt sensitive BLOB data.
3021 * dwFlags [I] One of:
3022 * CRYPT_EXPORTABLE: the imported key is marked exportable
3023 * phKey [O] Handle to the imported key.
3025 * RETURNS
3026 * Success: TRUE.
3027 * Failure: FALSE.
3029 BOOL WINAPI RSAENH_CPImportKey(HCRYPTPROV hProv, CONST BYTE *pbData, DWORD dwDataLen,
3030 HCRYPTKEY hPubKey, DWORD dwFlags, HCRYPTKEY *phKey)
3032 TRACE("(hProv=%08lx, pbData=%p, dwDataLen=%d, hPubKey=%08lx, dwFlags=%08x, phKey=%p)\n",
3033 hProv, pbData, dwDataLen, hPubKey, dwFlags, phKey);
3035 if (dwFlags & CRYPT_IPSEC_HMAC_KEY)
3037 FIXME("unimplemented for CRYPT_IPSEC_HMAC_KEY\n");
3038 SetLastError(NTE_BAD_FLAGS);
3039 return FALSE;
3041 return import_key(hProv, pbData, dwDataLen, hPubKey, dwFlags, TRUE, phKey);
3044 /******************************************************************************
3045 * CPGenKey (RSAENH.@)
3047 * Generate a key in the key container
3049 * PARAMS
3050 * hProv [I] Key container for which a key is to be generated.
3051 * Algid [I] Crypto algorithm identifier for the key to be generated.
3052 * dwFlags [I] Upper 16 bits: Binary length of key. Lower 16 bits: Flags. See Notes
3053 * phKey [O] Handle to the generated key.
3055 * RETURNS
3056 * Success: TRUE.
3057 * Failure: FALSE.
3059 * FIXME
3060 * Flags currently not considered.
3062 * NOTES
3063 * Private key-exchange- and signature-keys can be generated with Algid AT_KEYEXCHANGE
3064 * and AT_SIGNATURE values.
3066 BOOL WINAPI RSAENH_CPGenKey(HCRYPTPROV hProv, ALG_ID Algid, DWORD dwFlags, HCRYPTKEY *phKey)
3068 KEYCONTAINER *pKeyContainer;
3069 CRYPTKEY *pCryptKey;
3071 TRACE("(hProv=%08lx, aiAlgid=%d, dwFlags=%08x, phKey=%p)\n", hProv, Algid, dwFlags, phKey);
3073 if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER,
3074 (OBJECTHDR**)&pKeyContainer))
3076 /* MSDN: hProv not containing valid context handle */
3077 SetLastError(NTE_BAD_UID);
3078 return FALSE;
3081 switch (Algid)
3083 case AT_SIGNATURE:
3084 case CALG_RSA_SIGN:
3085 *phKey = new_key(hProv, CALG_RSA_SIGN, dwFlags, &pCryptKey);
3086 if (pCryptKey) {
3087 new_key_impl(pCryptKey->aiAlgid, &pCryptKey->context, pCryptKey->dwKeyLen);
3088 setup_key(pCryptKey);
3089 release_and_install_key(hProv, *phKey,
3090 &pKeyContainer->hSignatureKeyPair,
3091 FALSE);
3093 break;
3095 case AT_KEYEXCHANGE:
3096 case CALG_RSA_KEYX:
3097 *phKey = new_key(hProv, CALG_RSA_KEYX, dwFlags, &pCryptKey);
3098 if (pCryptKey) {
3099 new_key_impl(pCryptKey->aiAlgid, &pCryptKey->context, pCryptKey->dwKeyLen);
3100 setup_key(pCryptKey);
3101 release_and_install_key(hProv, *phKey,
3102 &pKeyContainer->hKeyExchangeKeyPair,
3103 FALSE);
3105 break;
3107 case CALG_RC2:
3108 case CALG_RC4:
3109 case CALG_DES:
3110 case CALG_3DES_112:
3111 case CALG_3DES:
3112 case CALG_AES:
3113 case CALG_AES_128:
3114 case CALG_AES_192:
3115 case CALG_AES_256:
3116 case CALG_PCT1_MASTER:
3117 case CALG_SSL2_MASTER:
3118 case CALG_SSL3_MASTER:
3119 case CALG_TLS1_MASTER:
3120 *phKey = new_key(hProv, Algid, dwFlags, &pCryptKey);
3121 if (pCryptKey) {
3122 gen_rand_impl(pCryptKey->abKeyValue, RSAENH_MAX_KEY_SIZE);
3123 switch (Algid) {
3124 case CALG_SSL3_MASTER:
3125 pCryptKey->abKeyValue[0] = RSAENH_SSL3_VERSION_MAJOR;
3126 pCryptKey->abKeyValue[1] = RSAENH_SSL3_VERSION_MINOR;
3127 break;
3129 case CALG_TLS1_MASTER:
3130 pCryptKey->abKeyValue[0] = RSAENH_TLS1_VERSION_MAJOR;
3131 pCryptKey->abKeyValue[1] = RSAENH_TLS1_VERSION_MINOR;
3132 break;
3134 setup_key(pCryptKey);
3136 break;
3138 default:
3139 /* MSDN: Algorithm not supported specified by Algid */
3140 SetLastError(NTE_BAD_ALGID);
3141 return FALSE;
3144 return *phKey != (HCRYPTKEY)INVALID_HANDLE_VALUE;
3147 /******************************************************************************
3148 * CPGenRandom (RSAENH.@)
3150 * Generate a random byte stream.
3152 * PARAMS
3153 * hProv [I] Key container that is used to generate random bytes.
3154 * dwLen [I] Specifies the number of requested random data bytes.
3155 * pbBuffer [O] Random bytes will be stored here.
3157 * RETURNS
3158 * Success: TRUE
3159 * Failure: FALSE
3161 BOOL WINAPI RSAENH_CPGenRandom(HCRYPTPROV hProv, DWORD dwLen, BYTE *pbBuffer)
3163 TRACE("(hProv=%08lx, dwLen=%d, pbBuffer=%p)\n", hProv, dwLen, pbBuffer);
3165 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
3167 /* MSDN: hProv not containing valid context handle */
3168 SetLastError(NTE_BAD_UID);
3169 return FALSE;
3172 return gen_rand_impl(pbBuffer, dwLen);
3175 /******************************************************************************
3176 * CPGetHashParam (RSAENH.@)
3178 * Query parameters of an hash object.
3180 * PARAMS
3181 * hProv [I] The kea container, which the hash belongs to.
3182 * hHash [I] The hash object that is to be queried.
3183 * dwParam [I] Specifies the parameter that is to be queried.
3184 * pbData [I] Pointer to the buffer where the parameter value will be stored.
3185 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
3186 * dwFlags [I] None currently defined.
3188 * RETURNS
3189 * Success: TRUE
3190 * Failure: FALSE
3192 * NOTES
3193 * Valid dwParams are: HP_ALGID, HP_HASHSIZE, HP_HASHVALUE. The hash will be
3194 * finalized if HP_HASHVALUE is queried.
3196 BOOL WINAPI RSAENH_CPGetHashParam(HCRYPTPROV hProv, HCRYPTHASH hHash, DWORD dwParam, BYTE *pbData,
3197 DWORD *pdwDataLen, DWORD dwFlags)
3199 CRYPTHASH *pCryptHash;
3201 TRACE("(hProv=%08lx, hHash=%08lx, dwParam=%08x, pbData=%p, pdwDataLen=%p, dwFlags=%08x)\n",
3202 hProv, hHash, dwParam, pbData, pdwDataLen, dwFlags);
3204 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
3206 SetLastError(NTE_BAD_UID);
3207 return FALSE;
3210 if (dwFlags)
3212 SetLastError(NTE_BAD_FLAGS);
3213 return FALSE;
3216 if (!lookup_handle(&handle_table, hHash, RSAENH_MAGIC_HASH,
3217 (OBJECTHDR**)&pCryptHash))
3219 SetLastError(NTE_BAD_HASH);
3220 return FALSE;
3223 if (!pdwDataLen)
3225 SetLastError(ERROR_INVALID_PARAMETER);
3226 return FALSE;
3229 switch (dwParam)
3231 case HP_ALGID:
3232 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptHash->aiAlgid,
3233 sizeof(ALG_ID));
3235 case HP_HASHSIZE:
3236 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptHash->dwHashSize,
3237 sizeof(DWORD));
3239 case HP_HASHVAL:
3240 if (pCryptHash->aiAlgid == CALG_TLS1PRF) {
3241 return tls1_prf(hProv, pCryptHash->hKey, &pCryptHash->tpPRFParams.blobLabel,
3242 &pCryptHash->tpPRFParams.blobSeed, pbData, *pdwDataLen);
3245 if ( pbData == NULL ) {
3246 *pdwDataLen = pCryptHash->dwHashSize;
3247 return TRUE;
3250 if (pbData && (pCryptHash->dwState != RSAENH_HASHSTATE_FINISHED))
3252 finalize_hash(pCryptHash);
3253 pCryptHash->dwState = RSAENH_HASHSTATE_FINISHED;
3256 return copy_param(pbData, pdwDataLen, pCryptHash->abHashValue,
3257 pCryptHash->dwHashSize);
3259 default:
3260 SetLastError(NTE_BAD_TYPE);
3261 return FALSE;
3265 /******************************************************************************
3266 * CPSetKeyParam (RSAENH.@)
3268 * Set a parameter of a key object
3270 * PARAMS
3271 * hProv [I] The key container to which the key belongs.
3272 * hKey [I] The key for which a parameter is to be set.
3273 * dwParam [I] Parameter type. See Notes.
3274 * pbData [I] Pointer to the parameter value.
3275 * dwFlags [I] Currently none defined.
3277 * RETURNS
3278 * Success: TRUE.
3279 * Failure: FALSE.
3281 * NOTES:
3282 * Defined dwParam types are:
3283 * - KP_MODE: Values MODE_CBC, MODE_ECB, MODE_CFB.
3284 * - KP_MODE_BITS: Shift width for cipher feedback mode. (Currently ignored by MS CSP's)
3285 * - KP_PERMISSIONS: Or'ed combination of CRYPT_ENCRYPT, CRYPT_DECRYPT,
3286 * CRYPT_EXPORT, CRYPT_READ, CRYPT_WRITE, CRYPT_MAC
3287 * - KP_IV: Initialization vector
3289 BOOL WINAPI RSAENH_CPSetKeyParam(HCRYPTPROV hProv, HCRYPTKEY hKey, DWORD dwParam, BYTE *pbData,
3290 DWORD dwFlags)
3292 CRYPTKEY *pCryptKey;
3294 TRACE("(hProv=%08lx, hKey=%08lx, dwParam=%08x, pbData=%p, dwFlags=%08x)\n", hProv, hKey,
3295 dwParam, pbData, dwFlags);
3297 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
3299 SetLastError(NTE_BAD_UID);
3300 return FALSE;
3303 if (dwFlags) {
3304 SetLastError(NTE_BAD_FLAGS);
3305 return FALSE;
3308 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
3310 SetLastError(NTE_BAD_KEY);
3311 return FALSE;
3314 switch (dwParam) {
3315 case KP_PADDING:
3316 /* The MS providers only support PKCS5_PADDING */
3317 if (*(DWORD *)pbData != PKCS5_PADDING) {
3318 SetLastError(NTE_BAD_DATA);
3319 return FALSE;
3321 return TRUE;
3323 case KP_MODE:
3324 pCryptKey->dwMode = *(DWORD*)pbData;
3325 return TRUE;
3327 case KP_MODE_BITS:
3328 pCryptKey->dwModeBits = *(DWORD*)pbData;
3329 return TRUE;
3331 case KP_PERMISSIONS:
3333 DWORD perms = *(DWORD *)pbData;
3335 if ((perms & CRYPT_EXPORT) &&
3336 !(pCryptKey->dwPermissions & CRYPT_EXPORT))
3338 SetLastError(NTE_BAD_DATA);
3339 return FALSE;
3341 else if (!(perms & CRYPT_EXPORT) &&
3342 (pCryptKey->dwPermissions & CRYPT_EXPORT))
3344 /* Clearing the export permission appears to be ignored,
3345 * see tests.
3347 perms |= CRYPT_EXPORT;
3349 pCryptKey->dwPermissions = perms;
3350 return TRUE;
3353 case KP_IV:
3354 memcpy(pCryptKey->abInitVector, pbData, pCryptKey->dwBlockLen);
3355 setup_key(pCryptKey);
3356 return TRUE;
3358 case KP_SALT:
3359 switch (pCryptKey->aiAlgid) {
3360 case CALG_RC2:
3361 case CALG_RC4:
3362 if (!pbData)
3364 SetLastError(ERROR_INVALID_PARAMETER);
3365 return FALSE;
3367 /* MSDN: the base provider always sets eleven bytes of
3368 * salt value.
3370 memcpy(pCryptKey->abKeyValue + pCryptKey->dwKeyLen,
3371 pbData, 11);
3372 pCryptKey->dwSaltLen = 11;
3373 setup_key(pCryptKey);
3374 /* Strange but true: salt length reset to 0 after setting
3375 * it via KP_SALT.
3377 pCryptKey->dwSaltLen = 0;
3378 break;
3379 default:
3380 SetLastError(NTE_BAD_KEY);
3381 return FALSE;
3383 return TRUE;
3385 case KP_SALT_EX:
3387 CRYPT_INTEGER_BLOB *blob = (CRYPT_INTEGER_BLOB *)pbData;
3389 /* salt length can't be greater than 184 bits = 24 bytes */
3390 if (blob->cbData > 24)
3392 SetLastError(NTE_BAD_DATA);
3393 return FALSE;
3395 memcpy(pCryptKey->abKeyValue + pCryptKey->dwKeyLen, blob->pbData,
3396 blob->cbData);
3397 pCryptKey->dwSaltLen = blob->cbData;
3398 setup_key(pCryptKey);
3399 return TRUE;
3402 case KP_EFFECTIVE_KEYLEN:
3403 switch (pCryptKey->aiAlgid) {
3404 case CALG_RC2:
3405 if (!pbData)
3407 SetLastError(ERROR_INVALID_PARAMETER);
3408 return FALSE;
3410 else if (!*(DWORD *)pbData || *(DWORD *)pbData > 1024)
3412 SetLastError(NTE_BAD_DATA);
3413 return FALSE;
3415 else
3417 pCryptKey->dwEffectiveKeyLen = *(DWORD *)pbData;
3418 setup_key(pCryptKey);
3420 break;
3421 default:
3422 SetLastError(NTE_BAD_TYPE);
3423 return FALSE;
3425 return TRUE;
3427 case KP_SCHANNEL_ALG:
3428 switch (((PSCHANNEL_ALG)pbData)->dwUse) {
3429 case SCHANNEL_ENC_KEY:
3430 memcpy(&pCryptKey->siSChannelInfo.saEncAlg, pbData, sizeof(SCHANNEL_ALG));
3431 break;
3433 case SCHANNEL_MAC_KEY:
3434 memcpy(&pCryptKey->siSChannelInfo.saMACAlg, pbData, sizeof(SCHANNEL_ALG));
3435 break;
3437 default:
3438 SetLastError(NTE_FAIL); /* FIXME: error code */
3439 return FALSE;
3441 return TRUE;
3443 case KP_CLIENT_RANDOM:
3444 return copy_data_blob(&pCryptKey->siSChannelInfo.blobClientRandom, (PCRYPT_DATA_BLOB)pbData);
3446 case KP_SERVER_RANDOM:
3447 return copy_data_blob(&pCryptKey->siSChannelInfo.blobServerRandom, (PCRYPT_DATA_BLOB)pbData);
3449 default:
3450 SetLastError(NTE_BAD_TYPE);
3451 return FALSE;
3455 /******************************************************************************
3456 * CPGetKeyParam (RSAENH.@)
3458 * Query a key parameter.
3460 * PARAMS
3461 * hProv [I] The key container, which the key belongs to.
3462 * hHash [I] The key object that is to be queried.
3463 * dwParam [I] Specifies the parameter that is to be queried.
3464 * pbData [I] Pointer to the buffer where the parameter value will be stored.
3465 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
3466 * dwFlags [I] None currently defined.
3468 * RETURNS
3469 * Success: TRUE
3470 * Failure: FALSE
3472 * NOTES
3473 * Defined dwParam types are:
3474 * - KP_MODE: Values MODE_CBC, MODE_ECB, MODE_CFB.
3475 * - KP_MODE_BITS: Shift width for cipher feedback mode.
3476 * (Currently ignored by MS CSP's - always eight)
3477 * - KP_PERMISSIONS: Or'ed combination of CRYPT_ENCRYPT, CRYPT_DECRYPT,
3478 * CRYPT_EXPORT, CRYPT_READ, CRYPT_WRITE, CRYPT_MAC
3479 * - KP_IV: Initialization vector.
3480 * - KP_KEYLEN: Bitwidth of the key.
3481 * - KP_BLOCKLEN: Size of a block cipher block.
3482 * - KP_SALT: Salt value.
3484 BOOL WINAPI RSAENH_CPGetKeyParam(HCRYPTPROV hProv, HCRYPTKEY hKey, DWORD dwParam, BYTE *pbData,
3485 DWORD *pdwDataLen, DWORD dwFlags)
3487 CRYPTKEY *pCryptKey;
3488 DWORD dwValue;
3490 TRACE("(hProv=%08lx, hKey=%08lx, dwParam=%08x, pbData=%p, pdwDataLen=%p dwFlags=%08x)\n",
3491 hProv, hKey, dwParam, pbData, pdwDataLen, dwFlags);
3493 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
3495 SetLastError(NTE_BAD_UID);
3496 return FALSE;
3499 if (dwFlags) {
3500 SetLastError(NTE_BAD_FLAGS);
3501 return FALSE;
3504 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
3506 SetLastError(NTE_BAD_KEY);
3507 return FALSE;
3510 switch (dwParam)
3512 case KP_IV:
3513 return copy_param(pbData, pdwDataLen, pCryptKey->abInitVector,
3514 pCryptKey->dwBlockLen);
3516 case KP_SALT:
3517 switch (pCryptKey->aiAlgid) {
3518 case CALG_RC2:
3519 case CALG_RC4:
3520 return copy_param(pbData, pdwDataLen,
3521 &pCryptKey->abKeyValue[pCryptKey->dwKeyLen],
3522 pCryptKey->dwSaltLen);
3523 default:
3524 SetLastError(NTE_BAD_KEY);
3525 return FALSE;
3528 case KP_PADDING:
3529 dwValue = PKCS5_PADDING;
3530 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwValue, sizeof(DWORD));
3532 case KP_KEYLEN:
3533 dwValue = pCryptKey->dwKeyLen << 3;
3534 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwValue, sizeof(DWORD));
3536 case KP_EFFECTIVE_KEYLEN:
3537 if (pCryptKey->dwEffectiveKeyLen)
3538 dwValue = pCryptKey->dwEffectiveKeyLen;
3539 else
3540 dwValue = pCryptKey->dwKeyLen << 3;
3541 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwValue, sizeof(DWORD));
3543 case KP_BLOCKLEN:
3544 dwValue = pCryptKey->dwBlockLen << 3;
3545 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwValue, sizeof(DWORD));
3547 case KP_MODE:
3548 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptKey->dwMode, sizeof(DWORD));
3550 case KP_MODE_BITS:
3551 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptKey->dwModeBits,
3552 sizeof(DWORD));
3554 case KP_PERMISSIONS:
3555 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptKey->dwPermissions,
3556 sizeof(DWORD));
3558 case KP_ALGID:
3559 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptKey->aiAlgid, sizeof(DWORD));
3561 default:
3562 SetLastError(NTE_BAD_TYPE);
3563 return FALSE;
3567 /******************************************************************************
3568 * CPGetProvParam (RSAENH.@)
3570 * Query a CSP parameter.
3572 * PARAMS
3573 * hProv [I] The key container that is to be queried.
3574 * dwParam [I] Specifies the parameter that is to be queried.
3575 * pbData [I] Pointer to the buffer where the parameter value will be stored.
3576 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
3577 * dwFlags [I] CRYPT_FIRST: Start enumeration (for PP_ENUMALGS{_EX}).
3579 * RETURNS
3580 * Success: TRUE
3581 * Failure: FALSE
3582 * NOTES:
3583 * Defined dwParam types:
3584 * - PP_CONTAINER: Name of the key container.
3585 * - PP_NAME: Name of the cryptographic service provider.
3586 * - PP_SIG_KEYSIZE_INC: RSA signature keywidth granularity in bits.
3587 * - PP_KEYX_KEYSIZE_INC: RSA key-exchange keywidth granularity in bits.
3588 * - PP_ENUMALGS{_EX}: Query provider capabilities.
3590 BOOL WINAPI RSAENH_CPGetProvParam(HCRYPTPROV hProv, DWORD dwParam, BYTE *pbData,
3591 DWORD *pdwDataLen, DWORD dwFlags)
3593 KEYCONTAINER *pKeyContainer;
3594 PROV_ENUMALGS provEnumalgs;
3595 DWORD dwTemp;
3596 HKEY hKey;
3598 /* This is for dwParam PP_CRYPT_COUNT_KEY_USE.
3599 * IE6 SP1 asks for it in the 'About' dialog.
3600 * Returning this BLOB seems to satisfy IE. The marked 0x00 seem
3601 * to be 'don't care's. If you know anything more specific about
3602 * this provider parameter, please report to wine-devel@winehq.org */
3603 static CONST BYTE abWTF[96] = {
3604 0xb0, 0x25, 0x63, 0x86, 0x9c, 0xab, 0xb6, 0x37,
3605 0xe8, 0x82, /**/0x00,/**/ 0x72, 0x06, 0xb2, /**/0x00,/**/ 0x3b,
3606 0x60, 0x35, /**/0x00,/**/ 0x3b, 0x88, 0xce, /**/0x00,/**/ 0x82,
3607 0xbc, 0x7a, /**/0x00,/**/ 0xb7, 0x4f, 0x7e, /**/0x00,/**/ 0xde,
3608 0x92, 0xf1, /**/0x00,/**/ 0x83, 0xea, 0x5e, /**/0x00,/**/ 0xc8,
3609 0x12, 0x1e, 0xd4, 0x06, 0xf7, 0x66, /**/0x00,/**/ 0x01,
3610 0x29, 0xa4, /**/0x00,/**/ 0xf8, 0x24, 0x0c, /**/0x00,/**/ 0x33,
3611 0x06, 0x80, /**/0x00,/**/ 0x02, 0x46, 0x0b, /**/0x00,/**/ 0x6d,
3612 0x5b, 0xca, /**/0x00,/**/ 0x9a, 0x10, 0xf0, /**/0x00,/**/ 0x05,
3613 0x19, 0xd0, /**/0x00,/**/ 0x2c, 0xf6, 0x27, /**/0x00,/**/ 0xaa,
3614 0x7c, 0x6f, /**/0x00,/**/ 0xb9, 0xd8, 0x72, /**/0x00,/**/ 0x03,
3615 0xf3, 0x81, /**/0x00,/**/ 0xfa, 0xe8, 0x26, /**/0x00,/**/ 0xca
3618 TRACE("(hProv=%08lx, dwParam=%08x, pbData=%p, pdwDataLen=%p, dwFlags=%08x)\n",
3619 hProv, dwParam, pbData, pdwDataLen, dwFlags);
3621 if (!pdwDataLen) {
3622 SetLastError(ERROR_INVALID_PARAMETER);
3623 return FALSE;
3626 if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER,
3627 (OBJECTHDR**)&pKeyContainer))
3629 /* MSDN: hProv not containing valid context handle */
3630 SetLastError(NTE_BAD_UID);
3631 return FALSE;
3634 switch (dwParam)
3636 case PP_CONTAINER:
3637 case PP_UNIQUE_CONTAINER:/* MSDN says we can return the same value as PP_CONTAINER */
3638 return copy_param(pbData, pdwDataLen, (CONST BYTE*)pKeyContainer->szName,
3639 strlen(pKeyContainer->szName)+1);
3641 case PP_NAME:
3642 return copy_param(pbData, pdwDataLen, (CONST BYTE*)pKeyContainer->szProvName,
3643 strlen(pKeyContainer->szProvName)+1);
3645 case PP_PROVTYPE:
3646 dwTemp = PROV_RSA_FULL;
3647 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwTemp, sizeof(dwTemp));
3649 case PP_KEYSPEC:
3650 dwTemp = AT_SIGNATURE | AT_KEYEXCHANGE;
3651 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwTemp, sizeof(dwTemp));
3653 case PP_KEYSET_TYPE:
3654 dwTemp = pKeyContainer->dwFlags & CRYPT_MACHINE_KEYSET;
3655 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwTemp, sizeof(dwTemp));
3657 case PP_KEYSTORAGE:
3658 dwTemp = CRYPT_SEC_DESCR;
3659 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwTemp, sizeof(dwTemp));
3661 case PP_SIG_KEYSIZE_INC:
3662 case PP_KEYX_KEYSIZE_INC:
3663 dwTemp = 8;
3664 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwTemp, sizeof(dwTemp));
3666 case PP_IMPTYPE:
3667 dwTemp = CRYPT_IMPL_SOFTWARE;
3668 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwTemp, sizeof(dwTemp));
3670 case PP_VERSION:
3671 dwTemp = 0x00000200;
3672 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwTemp, sizeof(dwTemp));
3674 case PP_ENUMCONTAINERS:
3675 if ((dwFlags & CRYPT_FIRST) == CRYPT_FIRST) pKeyContainer->dwEnumContainersCtr = 0;
3677 if (!pbData) {
3678 *pdwDataLen = (DWORD)MAX_PATH + 1;
3679 return TRUE;
3682 if (!open_container_key("", dwFlags, &hKey))
3684 SetLastError(ERROR_NO_MORE_ITEMS);
3685 return FALSE;
3688 dwTemp = *pdwDataLen;
3689 switch (RegEnumKeyExA(hKey, pKeyContainer->dwEnumContainersCtr, (LPSTR)pbData, &dwTemp,
3690 NULL, NULL, NULL, NULL))
3692 case ERROR_MORE_DATA:
3693 *pdwDataLen = (DWORD)MAX_PATH + 1;
3695 case ERROR_SUCCESS:
3696 pKeyContainer->dwEnumContainersCtr++;
3697 RegCloseKey(hKey);
3698 return TRUE;
3700 case ERROR_NO_MORE_ITEMS:
3701 default:
3702 SetLastError(ERROR_NO_MORE_ITEMS);
3703 RegCloseKey(hKey);
3704 return FALSE;
3707 case PP_ENUMALGS:
3708 case PP_ENUMALGS_EX:
3709 if (((pKeyContainer->dwEnumAlgsCtr >= RSAENH_MAX_ENUMALGS-1) ||
3710 (!aProvEnumAlgsEx[pKeyContainer->dwPersonality]
3711 [pKeyContainer->dwEnumAlgsCtr+1].aiAlgid)) &&
3712 ((dwFlags & CRYPT_FIRST) != CRYPT_FIRST))
3714 SetLastError(ERROR_NO_MORE_ITEMS);
3715 return FALSE;
3718 if (dwParam == PP_ENUMALGS) {
3719 if (pbData && (*pdwDataLen >= sizeof(PROV_ENUMALGS)))
3720 pKeyContainer->dwEnumAlgsCtr = ((dwFlags & CRYPT_FIRST) == CRYPT_FIRST) ?
3721 0 : pKeyContainer->dwEnumAlgsCtr+1;
3723 provEnumalgs.aiAlgid = aProvEnumAlgsEx
3724 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr].aiAlgid;
3725 provEnumalgs.dwBitLen = aProvEnumAlgsEx
3726 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr].dwDefaultLen;
3727 provEnumalgs.dwNameLen = aProvEnumAlgsEx
3728 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr].dwNameLen;
3729 memcpy(provEnumalgs.szName, aProvEnumAlgsEx
3730 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr].szName,
3731 20*sizeof(CHAR));
3733 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&provEnumalgs,
3734 sizeof(PROV_ENUMALGS));
3735 } else {
3736 if (pbData && (*pdwDataLen >= sizeof(PROV_ENUMALGS_EX)))
3737 pKeyContainer->dwEnumAlgsCtr = ((dwFlags & CRYPT_FIRST) == CRYPT_FIRST) ?
3738 0 : pKeyContainer->dwEnumAlgsCtr+1;
3740 return copy_param(pbData, pdwDataLen,
3741 (CONST BYTE*)&aProvEnumAlgsEx
3742 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr],
3743 sizeof(PROV_ENUMALGS_EX));
3746 case PP_CRYPT_COUNT_KEY_USE: /* Asked for by IE About dialog */
3747 return copy_param(pbData, pdwDataLen, abWTF, sizeof(abWTF));
3749 default:
3750 /* MSDN: Unknown parameter number in dwParam */
3751 SetLastError(NTE_BAD_TYPE);
3752 return FALSE;
3756 /******************************************************************************
3757 * CPDeriveKey (RSAENH.@)
3759 * Derives a key from a hash value.
3761 * PARAMS
3762 * hProv [I] Key container for which a key is to be generated.
3763 * Algid [I] Crypto algorithm identifier for the key to be generated.
3764 * hBaseData [I] Hash from whose value the key will be derived.
3765 * dwFlags [I] See Notes.
3766 * phKey [O] The generated key.
3768 * RETURNS
3769 * Success: TRUE
3770 * Failure: FALSE
3772 * NOTES
3773 * Defined flags:
3774 * - CRYPT_EXPORTABLE: Key can be exported.
3775 * - CRYPT_NO_SALT: No salt is used for 40 bit keys.
3776 * - CRYPT_CREATE_SALT: Use remaining bits as salt value.
3778 BOOL WINAPI RSAENH_CPDeriveKey(HCRYPTPROV hProv, ALG_ID Algid, HCRYPTHASH hBaseData,
3779 DWORD dwFlags, HCRYPTKEY *phKey)
3781 CRYPTKEY *pCryptKey, *pMasterKey;
3782 CRYPTHASH *pCryptHash;
3783 BYTE abHashValue[RSAENH_MAX_HASH_SIZE*2];
3784 DWORD dwLen;
3786 TRACE("(hProv=%08lx, Algid=%d, hBaseData=%08lx, dwFlags=%08x phKey=%p)\n", hProv, Algid,
3787 hBaseData, dwFlags, phKey);
3789 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
3791 SetLastError(NTE_BAD_UID);
3792 return FALSE;
3795 if (!lookup_handle(&handle_table, hBaseData, RSAENH_MAGIC_HASH,
3796 (OBJECTHDR**)&pCryptHash))
3798 SetLastError(NTE_BAD_HASH);
3799 return FALSE;
3802 if (!phKey)
3804 SetLastError(ERROR_INVALID_PARAMETER);
3805 return FALSE;
3808 switch (GET_ALG_CLASS(Algid))
3810 case ALG_CLASS_DATA_ENCRYPT:
3811 *phKey = new_key(hProv, Algid, dwFlags, &pCryptKey);
3812 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) return FALSE;
3815 * We derive the key material from the hash.
3816 * If the hash value is not large enough for the claimed key, we have to construct
3817 * a larger binary value based on the hash. This is documented in MSDN: CryptDeriveKey.
3819 dwLen = RSAENH_MAX_HASH_SIZE;
3820 RSAENH_CPGetHashParam(pCryptHash->hProv, hBaseData, HP_HASHVAL, abHashValue, &dwLen, 0);
3822 if (dwLen < pCryptKey->dwKeyLen) {
3823 BYTE pad1[RSAENH_HMAC_DEF_PAD_LEN], pad2[RSAENH_HMAC_DEF_PAD_LEN];
3824 BYTE old_hashval[RSAENH_MAX_HASH_SIZE];
3825 DWORD i;
3827 memcpy(old_hashval, pCryptHash->abHashValue, RSAENH_MAX_HASH_SIZE);
3829 for (i=0; i<RSAENH_HMAC_DEF_PAD_LEN; i++) {
3830 pad1[i] = RSAENH_HMAC_DEF_IPAD_CHAR ^ (i<dwLen ? abHashValue[i] : 0);
3831 pad2[i] = RSAENH_HMAC_DEF_OPAD_CHAR ^ (i<dwLen ? abHashValue[i] : 0);
3834 init_hash(pCryptHash);
3835 update_hash(pCryptHash, pad1, RSAENH_HMAC_DEF_PAD_LEN);
3836 finalize_hash(pCryptHash);
3837 memcpy(abHashValue, pCryptHash->abHashValue, pCryptHash->dwHashSize);
3839 init_hash(pCryptHash);
3840 update_hash(pCryptHash, pad2, RSAENH_HMAC_DEF_PAD_LEN);
3841 finalize_hash(pCryptHash);
3842 memcpy(abHashValue+pCryptHash->dwHashSize, pCryptHash->abHashValue,
3843 pCryptHash->dwHashSize);
3845 memcpy(pCryptHash->abHashValue, old_hashval, RSAENH_MAX_HASH_SIZE);
3848 memcpy(pCryptKey->abKeyValue, abHashValue,
3849 RSAENH_MIN(pCryptKey->dwKeyLen, sizeof(pCryptKey->abKeyValue)));
3850 break;
3852 case ALG_CLASS_MSG_ENCRYPT:
3853 if (!lookup_handle(&handle_table, pCryptHash->hKey, RSAENH_MAGIC_KEY,
3854 (OBJECTHDR**)&pMasterKey))
3856 SetLastError(NTE_FAIL); /* FIXME error code */
3857 return FALSE;
3860 switch (Algid)
3862 /* See RFC 2246, chapter 6.3 Key calculation */
3863 case CALG_SCHANNEL_ENC_KEY:
3864 *phKey = new_key(hProv, pMasterKey->siSChannelInfo.saEncAlg.Algid,
3865 MAKELONG(LOWORD(dwFlags),pMasterKey->siSChannelInfo.saEncAlg.cBits),
3866 &pCryptKey);
3867 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) return FALSE;
3868 memcpy(pCryptKey->abKeyValue,
3869 pCryptHash->abHashValue + (
3870 2 * (pMasterKey->siSChannelInfo.saMACAlg.cBits / 8) +
3871 ((dwFlags & CRYPT_SERVER) ?
3872 (pMasterKey->siSChannelInfo.saEncAlg.cBits / 8) : 0)),
3873 pMasterKey->siSChannelInfo.saEncAlg.cBits / 8);
3874 memcpy(pCryptKey->abInitVector,
3875 pCryptHash->abHashValue + (
3876 2 * (pMasterKey->siSChannelInfo.saMACAlg.cBits / 8) +
3877 2 * (pMasterKey->siSChannelInfo.saEncAlg.cBits / 8) +
3878 ((dwFlags & CRYPT_SERVER) ? pCryptKey->dwBlockLen : 0)),
3879 pCryptKey->dwBlockLen);
3880 break;
3882 case CALG_SCHANNEL_MAC_KEY:
3883 *phKey = new_key(hProv, Algid,
3884 MAKELONG(LOWORD(dwFlags),pMasterKey->siSChannelInfo.saMACAlg.cBits),
3885 &pCryptKey);
3886 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) return FALSE;
3887 memcpy(pCryptKey->abKeyValue,
3888 pCryptHash->abHashValue + ((dwFlags & CRYPT_SERVER) ?
3889 pMasterKey->siSChannelInfo.saMACAlg.cBits / 8 : 0),
3890 pMasterKey->siSChannelInfo.saMACAlg.cBits / 8);
3891 break;
3893 default:
3894 SetLastError(NTE_BAD_ALGID);
3895 return FALSE;
3897 break;
3899 default:
3900 SetLastError(NTE_BAD_ALGID);
3901 return FALSE;
3904 setup_key(pCryptKey);
3905 return TRUE;
3908 /******************************************************************************
3909 * CPGetUserKey (RSAENH.@)
3911 * Returns a handle to the user's private key-exchange- or signature-key.
3913 * PARAMS
3914 * hProv [I] The key container from which a user key is requested.
3915 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
3916 * phUserKey [O] Handle to the requested key or INVALID_HANDLE_VALUE in case of failure.
3918 * RETURNS
3919 * Success: TRUE.
3920 * Failure: FALSE.
3922 * NOTE
3923 * A newly created key container does not contain private user key. Create them with CPGenKey.
3925 BOOL WINAPI RSAENH_CPGetUserKey(HCRYPTPROV hProv, DWORD dwKeySpec, HCRYPTKEY *phUserKey)
3927 KEYCONTAINER *pKeyContainer;
3929 TRACE("(hProv=%08lx, dwKeySpec=%08x, phUserKey=%p)\n", hProv, dwKeySpec, phUserKey);
3931 if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER,
3932 (OBJECTHDR**)&pKeyContainer))
3934 /* MSDN: hProv not containing valid context handle */
3935 SetLastError(NTE_BAD_UID);
3936 return FALSE;
3939 switch (dwKeySpec)
3941 case AT_KEYEXCHANGE:
3942 copy_handle(&handle_table, pKeyContainer->hKeyExchangeKeyPair, RSAENH_MAGIC_KEY,
3943 phUserKey);
3944 break;
3946 case AT_SIGNATURE:
3947 copy_handle(&handle_table, pKeyContainer->hSignatureKeyPair, RSAENH_MAGIC_KEY,
3948 phUserKey);
3949 break;
3951 default:
3952 *phUserKey = (HCRYPTKEY)INVALID_HANDLE_VALUE;
3955 if (*phUserKey == (HCRYPTKEY)INVALID_HANDLE_VALUE)
3957 /* MSDN: dwKeySpec parameter specifies nonexistent key */
3958 SetLastError(NTE_NO_KEY);
3959 return FALSE;
3962 return TRUE;
3965 /******************************************************************************
3966 * CPHashData (RSAENH.@)
3968 * Updates a hash object with the given data.
3970 * PARAMS
3971 * hProv [I] Key container to which the hash object belongs.
3972 * hHash [I] Hash object which is to be updated.
3973 * pbData [I] Pointer to data with which the hash object is to be updated.
3974 * dwDataLen [I] Length of the data.
3975 * dwFlags [I] Currently none defined.
3977 * RETURNS
3978 * Success: TRUE.
3979 * Failure: FALSE.
3981 * NOTES
3982 * The actual hash value is queried with CPGetHashParam, which will finalize
3983 * the hash. Updating a finalized hash will fail with a last error NTE_BAD_HASH_STATE.
3985 BOOL WINAPI RSAENH_CPHashData(HCRYPTPROV hProv, HCRYPTHASH hHash, CONST BYTE *pbData,
3986 DWORD dwDataLen, DWORD dwFlags)
3988 CRYPTHASH *pCryptHash;
3990 TRACE("(hProv=%08lx, hHash=%08lx, pbData=%p, dwDataLen=%d, dwFlags=%08x)\n",
3991 hProv, hHash, pbData, dwDataLen, dwFlags);
3993 if (dwFlags)
3995 SetLastError(NTE_BAD_FLAGS);
3996 return FALSE;
3999 if (!lookup_handle(&handle_table, hHash, RSAENH_MAGIC_HASH,
4000 (OBJECTHDR**)&pCryptHash))
4002 SetLastError(NTE_BAD_HASH);
4003 return FALSE;
4006 if (!get_algid_info(hProv, pCryptHash->aiAlgid) || pCryptHash->aiAlgid == CALG_SSL3_SHAMD5)
4008 SetLastError(NTE_BAD_ALGID);
4009 return FALSE;
4012 if (pCryptHash->dwState != RSAENH_HASHSTATE_HASHING)
4014 SetLastError(NTE_BAD_HASH_STATE);
4015 return FALSE;
4018 update_hash(pCryptHash, pbData, dwDataLen);
4019 return TRUE;
4022 /******************************************************************************
4023 * CPHashSessionKey (RSAENH.@)
4025 * Updates a hash object with the binary representation of a symmetric key.
4027 * PARAMS
4028 * hProv [I] Key container to which the hash object belongs.
4029 * hHash [I] Hash object which is to be updated.
4030 * hKey [I] The symmetric key, whose binary value will be added to the hash.
4031 * dwFlags [I] CRYPT_LITTLE_ENDIAN, if the binary key value shall be interpreted as little endian.
4033 * RETURNS
4034 * Success: TRUE.
4035 * Failure: FALSE.
4037 BOOL WINAPI RSAENH_CPHashSessionKey(HCRYPTPROV hProv, HCRYPTHASH hHash, HCRYPTKEY hKey,
4038 DWORD dwFlags)
4040 BYTE abKeyValue[RSAENH_MAX_KEY_SIZE], bTemp;
4041 CRYPTKEY *pKey;
4042 DWORD i;
4044 TRACE("(hProv=%08lx, hHash=%08lx, hKey=%08lx, dwFlags=%08x)\n", hProv, hHash, hKey, dwFlags);
4046 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pKey) ||
4047 (GET_ALG_CLASS(pKey->aiAlgid) != ALG_CLASS_DATA_ENCRYPT))
4049 SetLastError(NTE_BAD_KEY);
4050 return FALSE;
4053 if (dwFlags & ~CRYPT_LITTLE_ENDIAN) {
4054 SetLastError(NTE_BAD_FLAGS);
4055 return FALSE;
4058 memcpy(abKeyValue, pKey->abKeyValue, pKey->dwKeyLen);
4059 if (!(dwFlags & CRYPT_LITTLE_ENDIAN)) {
4060 for (i=0; i<pKey->dwKeyLen/2; i++) {
4061 bTemp = abKeyValue[i];
4062 abKeyValue[i] = abKeyValue[pKey->dwKeyLen-i-1];
4063 abKeyValue[pKey->dwKeyLen-i-1] = bTemp;
4067 return RSAENH_CPHashData(hProv, hHash, abKeyValue, pKey->dwKeyLen, 0);
4070 /******************************************************************************
4071 * CPReleaseContext (RSAENH.@)
4073 * Release a key container.
4075 * PARAMS
4076 * hProv [I] Key container to be released.
4077 * dwFlags [I] Currently none defined.
4079 * RETURNS
4080 * Success: TRUE
4081 * Failure: FALSE
4083 BOOL WINAPI RSAENH_CPReleaseContext(HCRYPTPROV hProv, DWORD dwFlags)
4085 TRACE("(hProv=%08lx, dwFlags=%08x)\n", hProv, dwFlags);
4087 if (!release_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
4089 /* MSDN: hProv not containing valid context handle */
4090 SetLastError(NTE_BAD_UID);
4091 return FALSE;
4094 if (dwFlags) {
4095 SetLastError(NTE_BAD_FLAGS);
4096 return FALSE;
4099 return TRUE;
4102 /******************************************************************************
4103 * CPSetHashParam (RSAENH.@)
4105 * Set a parameter of a hash object
4107 * PARAMS
4108 * hProv [I] The key container to which the key belongs.
4109 * hHash [I] The hash object for which a parameter is to be set.
4110 * dwParam [I] Parameter type. See Notes.
4111 * pbData [I] Pointer to the parameter value.
4112 * dwFlags [I] Currently none defined.
4114 * RETURNS
4115 * Success: TRUE.
4116 * Failure: FALSE.
4118 * NOTES
4119 * Currently only the HP_HMAC_INFO dwParam type is defined.
4120 * The HMAC_INFO struct will be deep copied into the hash object.
4121 * See Internet RFC 2104 for details on the HMAC algorithm.
4123 BOOL WINAPI RSAENH_CPSetHashParam(HCRYPTPROV hProv, HCRYPTHASH hHash, DWORD dwParam,
4124 BYTE *pbData, DWORD dwFlags)
4126 CRYPTHASH *pCryptHash;
4127 CRYPTKEY *pCryptKey;
4128 DWORD i;
4130 TRACE("(hProv=%08lx, hHash=%08lx, dwParam=%08x, pbData=%p, dwFlags=%08x)\n",
4131 hProv, hHash, dwParam, pbData, dwFlags);
4133 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
4135 SetLastError(NTE_BAD_UID);
4136 return FALSE;
4139 if (dwFlags) {
4140 SetLastError(NTE_BAD_FLAGS);
4141 return FALSE;
4144 if (!lookup_handle(&handle_table, hHash, RSAENH_MAGIC_HASH,
4145 (OBJECTHDR**)&pCryptHash))
4147 SetLastError(NTE_BAD_HASH);
4148 return FALSE;
4151 switch (dwParam) {
4152 case HP_HMAC_INFO:
4153 free_hmac_info(pCryptHash->pHMACInfo);
4154 if (!copy_hmac_info(&pCryptHash->pHMACInfo, (PHMAC_INFO)pbData)) return FALSE;
4156 if (!lookup_handle(&handle_table, pCryptHash->hKey, RSAENH_MAGIC_KEY,
4157 (OBJECTHDR**)&pCryptKey))
4159 SetLastError(NTE_FAIL); /* FIXME: correct error code? */
4160 return FALSE;
4163 for (i=0; i<RSAENH_MIN(pCryptKey->dwKeyLen,pCryptHash->pHMACInfo->cbInnerString); i++) {
4164 pCryptHash->pHMACInfo->pbInnerString[i] ^= pCryptKey->abKeyValue[i];
4166 for (i=0; i<RSAENH_MIN(pCryptKey->dwKeyLen,pCryptHash->pHMACInfo->cbOuterString); i++) {
4167 pCryptHash->pHMACInfo->pbOuterString[i] ^= pCryptKey->abKeyValue[i];
4170 init_hash(pCryptHash);
4171 return TRUE;
4173 case HP_HASHVAL:
4174 memcpy(pCryptHash->abHashValue, pbData, pCryptHash->dwHashSize);
4175 pCryptHash->dwState = RSAENH_HASHSTATE_FINISHED;
4176 return TRUE;
4178 case HP_TLS1PRF_SEED:
4179 return copy_data_blob(&pCryptHash->tpPRFParams.blobSeed, (PCRYPT_DATA_BLOB)pbData);
4181 case HP_TLS1PRF_LABEL:
4182 return copy_data_blob(&pCryptHash->tpPRFParams.blobLabel, (PCRYPT_DATA_BLOB)pbData);
4184 default:
4185 SetLastError(NTE_BAD_TYPE);
4186 return FALSE;
4190 /******************************************************************************
4191 * CPSetProvParam (RSAENH.@)
4193 BOOL WINAPI RSAENH_CPSetProvParam(HCRYPTPROV hProv, DWORD dwParam, BYTE *pbData, DWORD dwFlags)
4195 FIXME("(stub)\n");
4196 return FALSE;
4199 /******************************************************************************
4200 * CPSignHash (RSAENH.@)
4202 * Sign a hash object
4204 * PARAMS
4205 * hProv [I] The key container, to which the hash object belongs.
4206 * hHash [I] The hash object to be signed.
4207 * dwKeySpec [I] AT_SIGNATURE or AT_KEYEXCHANGE: Key used to generate the signature.
4208 * sDescription [I] Should be NULL for security reasons.
4209 * dwFlags [I] 0, CRYPT_NOHASHOID or CRYPT_X931_FORMAT: Format of the signature.
4210 * pbSignature [O] Buffer, to which the signature will be stored. May be NULL to query SigLen.
4211 * pdwSigLen [I/O] Size of the buffer (in), Length of the signature (out)
4213 * RETURNS
4214 * Success: TRUE
4215 * Failure: FALSE
4217 BOOL WINAPI RSAENH_CPSignHash(HCRYPTPROV hProv, HCRYPTHASH hHash, DWORD dwKeySpec,
4218 LPCWSTR sDescription, DWORD dwFlags, BYTE *pbSignature,
4219 DWORD *pdwSigLen)
4221 HCRYPTKEY hCryptKey = (HCRYPTKEY)INVALID_HANDLE_VALUE;
4222 CRYPTKEY *pCryptKey;
4223 DWORD dwHashLen;
4224 BYTE abHashValue[RSAENH_MAX_HASH_SIZE];
4225 ALG_ID aiAlgid;
4226 BOOL ret = FALSE;
4228 TRACE("(hProv=%08lx, hHash=%08lx, dwKeySpec=%08x, sDescription=%s, dwFlags=%08x, "
4229 "pbSignature=%p, pdwSigLen=%p)\n", hProv, hHash, dwKeySpec, debugstr_w(sDescription),
4230 dwFlags, pbSignature, pdwSigLen);
4232 if (dwFlags & ~(CRYPT_NOHASHOID|CRYPT_X931_FORMAT)) {
4233 SetLastError(NTE_BAD_FLAGS);
4234 return FALSE;
4237 if (!RSAENH_CPGetUserKey(hProv, dwKeySpec, &hCryptKey)) return FALSE;
4239 if (!lookup_handle(&handle_table, hCryptKey, RSAENH_MAGIC_KEY,
4240 (OBJECTHDR**)&pCryptKey))
4242 SetLastError(NTE_NO_KEY);
4243 goto out;
4246 if (!pbSignature) {
4247 *pdwSigLen = pCryptKey->dwKeyLen;
4248 ret = TRUE;
4249 goto out;
4251 if (pCryptKey->dwKeyLen > *pdwSigLen)
4253 SetLastError(ERROR_MORE_DATA);
4254 *pdwSigLen = pCryptKey->dwKeyLen;
4255 goto out;
4257 *pdwSigLen = pCryptKey->dwKeyLen;
4259 if (sDescription) {
4260 if (!RSAENH_CPHashData(hProv, hHash, (CONST BYTE*)sDescription,
4261 (DWORD)lstrlenW(sDescription)*sizeof(WCHAR), 0))
4263 goto out;
4267 dwHashLen = sizeof(DWORD);
4268 if (!RSAENH_CPGetHashParam(hProv, hHash, HP_ALGID, (BYTE*)&aiAlgid, &dwHashLen, 0)) goto out;
4270 dwHashLen = RSAENH_MAX_HASH_SIZE;
4271 if (!RSAENH_CPGetHashParam(hProv, hHash, HP_HASHVAL, abHashValue, &dwHashLen, 0)) goto out;
4274 if (!build_hash_signature(pbSignature, *pdwSigLen, aiAlgid, abHashValue, dwHashLen, dwFlags)) {
4275 goto out;
4278 ret = encrypt_block_impl(pCryptKey->aiAlgid, PK_PRIVATE, &pCryptKey->context, pbSignature, pbSignature, RSAENH_ENCRYPT);
4279 out:
4280 RSAENH_CPDestroyKey(hProv, hCryptKey);
4281 return ret;
4284 /******************************************************************************
4285 * CPVerifySignature (RSAENH.@)
4287 * Verify the signature of a hash object.
4289 * PARAMS
4290 * hProv [I] The key container, to which the hash belongs.
4291 * hHash [I] The hash for which the signature is verified.
4292 * pbSignature [I] The binary signature.
4293 * dwSigLen [I] Length of the signature BLOB.
4294 * hPubKey [I] Public key used to verify the signature.
4295 * sDescription [I] Should be NULL for security reasons.
4296 * dwFlags [I] 0, CRYPT_NOHASHOID or CRYPT_X931_FORMAT: Format of the signature.
4298 * RETURNS
4299 * Success: TRUE (Signature is valid)
4300 * Failure: FALSE (GetLastError() == NTE_BAD_SIGNATURE, if signature is invalid)
4302 BOOL WINAPI RSAENH_CPVerifySignature(HCRYPTPROV hProv, HCRYPTHASH hHash, CONST BYTE *pbSignature,
4303 DWORD dwSigLen, HCRYPTKEY hPubKey, LPCWSTR sDescription,
4304 DWORD dwFlags)
4306 BYTE *pbConstructed = NULL, *pbDecrypted = NULL;
4307 CRYPTKEY *pCryptKey;
4308 DWORD dwHashLen;
4309 ALG_ID aiAlgid;
4310 BYTE abHashValue[RSAENH_MAX_HASH_SIZE];
4311 BOOL res = FALSE;
4313 TRACE("(hProv=%08lx, hHash=%08lx, pbSignature=%p, dwSigLen=%d, hPubKey=%08lx, sDescription=%s, "
4314 "dwFlags=%08x)\n", hProv, hHash, pbSignature, dwSigLen, hPubKey, debugstr_w(sDescription),
4315 dwFlags);
4317 if (dwFlags & ~(CRYPT_NOHASHOID|CRYPT_X931_FORMAT)) {
4318 SetLastError(NTE_BAD_FLAGS);
4319 return FALSE;
4322 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
4324 SetLastError(NTE_BAD_UID);
4325 return FALSE;
4328 if (!lookup_handle(&handle_table, hPubKey, RSAENH_MAGIC_KEY,
4329 (OBJECTHDR**)&pCryptKey))
4331 SetLastError(NTE_BAD_KEY);
4332 return FALSE;
4335 /* in Microsoft implementation, the signature length is checked before
4336 * the signature pointer.
4338 if (dwSigLen != pCryptKey->dwKeyLen)
4340 SetLastError(NTE_BAD_SIGNATURE);
4341 return FALSE;
4344 if (!hHash || !pbSignature)
4346 SetLastError(ERROR_INVALID_PARAMETER);
4347 return FALSE;
4350 if (sDescription) {
4351 if (!RSAENH_CPHashData(hProv, hHash, (CONST BYTE*)sDescription,
4352 (DWORD)lstrlenW(sDescription)*sizeof(WCHAR), 0))
4354 return FALSE;
4358 dwHashLen = sizeof(DWORD);
4359 if (!RSAENH_CPGetHashParam(hProv, hHash, HP_ALGID, (BYTE*)&aiAlgid, &dwHashLen, 0)) return FALSE;
4361 dwHashLen = RSAENH_MAX_HASH_SIZE;
4362 if (!RSAENH_CPGetHashParam(hProv, hHash, HP_HASHVAL, abHashValue, &dwHashLen, 0)) return FALSE;
4364 pbConstructed = HeapAlloc(GetProcessHeap(), 0, dwSigLen);
4365 if (!pbConstructed) {
4366 SetLastError(NTE_NO_MEMORY);
4367 goto cleanup;
4370 pbDecrypted = HeapAlloc(GetProcessHeap(), 0, dwSigLen);
4371 if (!pbDecrypted) {
4372 SetLastError(NTE_NO_MEMORY);
4373 goto cleanup;
4376 if (!encrypt_block_impl(pCryptKey->aiAlgid, PK_PUBLIC, &pCryptKey->context, pbSignature, pbDecrypted,
4377 RSAENH_DECRYPT))
4379 goto cleanup;
4382 if (!build_hash_signature(pbConstructed, dwSigLen, aiAlgid, abHashValue, dwHashLen, dwFlags)) {
4383 goto cleanup;
4386 if (memcmp(pbDecrypted, pbConstructed, dwSigLen)) {
4387 SetLastError(NTE_BAD_SIGNATURE);
4388 goto cleanup;
4391 res = TRUE;
4392 cleanup:
4393 HeapFree(GetProcessHeap(), 0, pbConstructed);
4394 HeapFree(GetProcessHeap(), 0, pbDecrypted);
4395 return res;
4398 static const WCHAR szProviderKeys[6][116] = {
4399 { 'S','o','f','t','w','a','r','e','\\',
4400 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
4401 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
4402 'i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ','B','a','s',
4403 'e',' ','C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r',
4404 'o','v','i','d','e','r',' ','v','1','.','0',0 },
4405 { 'S','o','f','t','w','a','r','e','\\',
4406 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
4407 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
4408 'i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ',
4409 'E','n','h','a','n','c','e','d',
4410 ' ','C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r',
4411 'o','v','i','d','e','r',' ','v','1','.','0',0 },
4412 { 'S','o','f','t','w','a','r','e','\\',
4413 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
4414 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
4415 'i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ','S','t','r','o','n','g',
4416 ' ','C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r',
4417 'o','v','i','d','e','r',0 },
4418 { 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
4419 'C','r','y','p','t','o','g','r','a','p','h','y','\\','D','e','f','a','u','l','t','s','\\',
4420 'P','r','o','v','i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ',
4421 'R','S','A',' ','S','C','h','a','n','n','e','l',' ',
4422 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r','o','v','i','d','e','r',0 },
4423 { 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
4424 'C','r','y','p','t','o','g','r','a','p','h','y','\\','D','e','f','a','u','l','t','s','\\',
4425 'P','r','o','v','i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ',
4426 'E','n','h','a','n','c','e','d',' ','R','S','A',' ','a','n','d',' ','A','E','S',' ',
4427 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r','o','v','i','d','e','r',0 },
4428 { 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
4429 'C','r','y','p','t','o','g','r','a','p','h','y','\\','D','e','f','a','u','l','t','s','\\',
4430 'P','r','o','v','i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ',
4431 'E','n','h','a','n','c','e','d',' ','R','S','A',' ','a','n','d',' ','A','E','S',' ',
4432 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r','o','v','i','d','e','r',
4433 ' ','(','P','r','o','t','o','t','y','p','e',')',0 }
4435 static const WCHAR szDefaultKeys[3][65] = {
4436 { 'S','o','f','t','w','a','r','e','\\',
4437 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
4438 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
4439 'i','d','e','r',' ','T','y','p','e','s','\\','T','y','p','e',' ','0','0','1',0 },
4440 { 'S','o','f','t','w','a','r','e','\\',
4441 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
4442 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
4443 'i','d','e','r',' ','T','y','p','e','s','\\','T','y','p','e',' ','0','1','2',0 },
4444 { 'S','o','f','t','w','a','r','e','\\',
4445 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
4446 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
4447 'i','d','e','r',' ','T','y','p','e','s','\\','T','y','p','e',' ','0','2','4',0 }
4451 /******************************************************************************
4452 * DllRegisterServer (RSAENH.@)
4454 * Dll self registration.
4456 * PARAMS
4458 * RETURNS
4459 * Success: S_OK.
4460 * Failure: != S_OK
4462 * NOTES
4463 * Registers the following keys:
4464 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider\
4465 * Microsoft Base Cryptographic Provider v1.0
4466 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider\
4467 * Microsoft Enhanced Cryptographic Provider
4468 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider\
4469 * Microsoft Strong Cryptographpic Provider
4470 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider Types\Type 001
4472 HRESULT WINAPI DllRegisterServer(void)
4474 HKEY key;
4475 DWORD dp;
4476 long apiRet;
4477 int i;
4479 for (i=0; i<6; i++) {
4480 apiRet = RegCreateKeyExW(HKEY_LOCAL_MACHINE, szProviderKeys[i], 0, NULL,
4481 REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &key, &dp);
4483 if (apiRet == ERROR_SUCCESS)
4485 if (dp == REG_CREATED_NEW_KEY)
4487 static const WCHAR szImagePath[] = { 'I','m','a','g','e',' ','P','a','t','h',0 };
4488 static const WCHAR szRSABase[] = { 'r','s','a','e','n','h','.','d','l','l',0 };
4489 static const WCHAR szType[] = { 'T','y','p','e',0 };
4490 static const WCHAR szSignature[] = { 'S','i','g','n','a','t','u','r','e',0 };
4491 DWORD type, sign;
4493 switch(i)
4495 case 3:
4496 type=PROV_RSA_SCHANNEL;
4497 break;
4498 case 4:
4499 case 5:
4500 type=PROV_RSA_AES;
4501 break;
4502 default:
4503 type=PROV_RSA_FULL;
4504 break;
4506 sign = 0xdeadbeef;
4507 RegSetValueExW(key, szImagePath, 0, REG_SZ, (const BYTE *)szRSABase,
4508 (lstrlenW(szRSABase) + 1) * sizeof(WCHAR));
4509 RegSetValueExW(key, szType, 0, REG_DWORD, (LPBYTE)&type, sizeof(type));
4510 RegSetValueExW(key, szSignature, 0, REG_BINARY, (LPBYTE)&sign, sizeof(sign));
4512 RegCloseKey(key);
4516 for (i=0; i<3; i++) {
4517 apiRet = RegCreateKeyExW(HKEY_LOCAL_MACHINE, szDefaultKeys[i], 0, NULL,
4518 REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &key, &dp);
4519 if (apiRet == ERROR_SUCCESS)
4521 if (dp == REG_CREATED_NEW_KEY)
4523 static const WCHAR szName[] = { 'N','a','m','e',0 };
4524 static const WCHAR szRSAName[3][54] = {
4525 { 'M','i','c','r','o','s','o','f','t',' ',
4526 'E','n','h','a','n','c','e','d',' ',
4527 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ',
4528 'P','r','o','v','i','d','e','r',' ','v','1','.','0',0 },
4529 { 'M','i','c','r','o','s','o','f','t',' ','R','S','A',' ',
4530 'S','C','h','a','n','n','e','l',' ',
4531 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ',
4532 'P','r','o','v','i','d','e','r',0 },
4533 { 'M','i','c','r','o','s','o','f','t',' ','E','n','h','a','n','c','e','d',' ',
4534 'R','S','A',' ','a','n','d',' ','A','E','S',' ',
4535 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ',
4536 'P','r','o','v','i','d','e','r',0 } };
4537 static const WCHAR szTypeName[] = { 'T','y','p','e','N','a','m','e',0 };
4538 static const WCHAR szRSATypeName[3][38] = {
4539 { 'R','S','A',' ','F','u','l','l',' ',
4540 '(','S','i','g','n','a','t','u','r','e',' ','a','n','d',' ',
4541 'K','e','y',' ','E','x','c','h','a','n','g','e',')',0 },
4542 { 'R','S','A',' ','S','C','h','a','n','n','e','l',0 },
4543 { 'R','S','A',' ','F','u','l','l',' ','a','n','d',' ','A','E','S',0 } };
4545 RegSetValueExW(key, szName, 0, REG_SZ,
4546 (const BYTE *)szRSAName[i], lstrlenW(szRSAName[i])*sizeof(WCHAR)+sizeof(WCHAR));
4547 RegSetValueExW(key, szTypeName, 0, REG_SZ,
4548 (const BYTE *)szRSATypeName[i], lstrlenW(szRSATypeName[i])*sizeof(WCHAR)+sizeof(WCHAR));
4551 RegCloseKey(key);
4554 return HRESULT_FROM_WIN32(apiRet);
4557 /******************************************************************************
4558 * DllUnregisterServer (RSAENH.@)
4560 * Dll self unregistration.
4562 * PARAMS
4564 * RETURNS
4565 * Success: S_OK
4567 * NOTES
4568 * For the relevant keys see DllRegisterServer.
4570 HRESULT WINAPI DllUnregisterServer(void)
4572 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szProviderKeys[0]);
4573 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szProviderKeys[1]);
4574 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szProviderKeys[2]);
4575 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szProviderKeys[3]);
4576 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szProviderKeys[4]);
4577 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szProviderKeys[5]);
4578 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szDefaultKeys[0]);
4579 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szDefaultKeys[1]);
4580 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szDefaultKeys[2]);
4581 return S_OK;