cmd: Fix a comment.
[wine/multimedia.git] / dlls / rsaenh / rsaenh.c
blobdb58e442bd70971b04dcbfb7f860c71baeae4f74
1 /*
2 * dlls/rsaenh/rsaenh.c
3 * RSAENH - RSA encryption for Wine
5 * Copyright 2002 TransGaming Technologies (David Hammerton)
6 * Copyright 2004 Mike McCormack for CodeWeavers
7 * Copyright 2004, 2005 Michael Jung
8 * Copyright 2007 Vijay Kiran Kamuju
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "config.h"
26 #include "wine/port.h"
27 #include "wine/library.h"
28 #include "wine/debug.h"
30 #include <stdarg.h>
31 #include <stdio.h>
33 #include "windef.h"
34 #include "winbase.h"
35 #include "winreg.h"
36 #include "wincrypt.h"
37 #include "handle.h"
38 #include "implglue.h"
39 #include "objbase.h"
40 #include "rpcproxy.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(crypt);
44 static HINSTANCE instance;
46 /******************************************************************************
47 * CRYPTHASH - hash objects
49 #define RSAENH_MAGIC_HASH 0x85938417u
50 #define RSAENH_MAX_HASH_SIZE 104
51 #define RSAENH_HASHSTATE_HASHING 1
52 #define RSAENH_HASHSTATE_FINISHED 2
53 typedef struct _RSAENH_TLS1PRF_PARAMS
55 CRYPT_DATA_BLOB blobLabel;
56 CRYPT_DATA_BLOB blobSeed;
57 } RSAENH_TLS1PRF_PARAMS;
59 typedef struct tagCRYPTHASH
61 OBJECTHDR header;
62 ALG_ID aiAlgid;
63 HCRYPTKEY hKey;
64 HCRYPTPROV hProv;
65 DWORD dwHashSize;
66 DWORD dwState;
67 HASH_CONTEXT context;
68 BYTE abHashValue[RSAENH_MAX_HASH_SIZE];
69 PHMAC_INFO pHMACInfo;
70 RSAENH_TLS1PRF_PARAMS tpPRFParams;
71 } CRYPTHASH;
73 /******************************************************************************
74 * CRYPTKEY - key objects
76 #define RSAENH_MAGIC_KEY 0x73620457u
77 #define RSAENH_MAX_KEY_SIZE 64
78 #define RSAENH_MAX_BLOCK_SIZE 24
79 #define RSAENH_KEYSTATE_IDLE 0
80 #define RSAENH_KEYSTATE_ENCRYPTING 1
81 #define RSAENH_KEYSTATE_MASTERKEY 2
82 typedef struct _RSAENH_SCHANNEL_INFO
84 SCHANNEL_ALG saEncAlg;
85 SCHANNEL_ALG saMACAlg;
86 CRYPT_DATA_BLOB blobClientRandom;
87 CRYPT_DATA_BLOB blobServerRandom;
88 } RSAENH_SCHANNEL_INFO;
90 typedef struct tagCRYPTKEY
92 OBJECTHDR header;
93 ALG_ID aiAlgid;
94 HCRYPTPROV hProv;
95 DWORD dwMode;
96 DWORD dwModeBits;
97 DWORD dwPermissions;
98 DWORD dwKeyLen;
99 DWORD dwEffectiveKeyLen;
100 DWORD dwSaltLen;
101 DWORD dwBlockLen;
102 DWORD dwState;
103 KEY_CONTEXT context;
104 BYTE abKeyValue[RSAENH_MAX_KEY_SIZE];
105 BYTE abInitVector[RSAENH_MAX_BLOCK_SIZE];
106 BYTE abChainVector[RSAENH_MAX_BLOCK_SIZE];
107 RSAENH_SCHANNEL_INFO siSChannelInfo;
108 CRYPT_DATA_BLOB blobHmacKey;
109 } CRYPTKEY;
111 /******************************************************************************
112 * KEYCONTAINER - key containers
114 #define RSAENH_PERSONALITY_BASE 0u
115 #define RSAENH_PERSONALITY_STRONG 1u
116 #define RSAENH_PERSONALITY_ENHANCED 2u
117 #define RSAENH_PERSONALITY_SCHANNEL 3u
118 #define RSAENH_PERSONALITY_AES 4u
120 #define RSAENH_MAGIC_CONTAINER 0x26384993u
121 typedef struct tagKEYCONTAINER
123 OBJECTHDR header;
124 DWORD dwFlags;
125 DWORD dwPersonality;
126 DWORD dwEnumAlgsCtr;
127 DWORD dwEnumContainersCtr;
128 CHAR szName[MAX_PATH];
129 CHAR szProvName[MAX_PATH];
130 HCRYPTKEY hKeyExchangeKeyPair;
131 HCRYPTKEY hSignatureKeyPair;
132 } KEYCONTAINER;
134 /******************************************************************************
135 * Some magic constants
137 #define RSAENH_ENCRYPT 1
138 #define RSAENH_DECRYPT 0
139 #define RSAENH_HMAC_DEF_IPAD_CHAR 0x36
140 #define RSAENH_HMAC_DEF_OPAD_CHAR 0x5c
141 #define RSAENH_HMAC_DEF_PAD_LEN 64
142 #define RSAENH_HMAC_BLOCK_LEN 64
143 #define RSAENH_DES_EFFECTIVE_KEYLEN 56
144 #define RSAENH_DES_STORAGE_KEYLEN 64
145 #define RSAENH_3DES112_EFFECTIVE_KEYLEN 112
146 #define RSAENH_3DES112_STORAGE_KEYLEN 128
147 #define RSAENH_3DES_EFFECTIVE_KEYLEN 168
148 #define RSAENH_3DES_STORAGE_KEYLEN 192
149 #define RSAENH_MAGIC_RSA2 0x32415352
150 #define RSAENH_MAGIC_RSA1 0x31415352
151 #define RSAENH_PKC_BLOCKTYPE 0x02
152 #define RSAENH_SSL3_VERSION_MAJOR 3
153 #define RSAENH_SSL3_VERSION_MINOR 0
154 #define RSAENH_TLS1_VERSION_MAJOR 3
155 #define RSAENH_TLS1_VERSION_MINOR 1
156 #define RSAENH_REGKEY "Software\\Wine\\Crypto\\RSA\\%s"
158 #define RSAENH_MIN(a,b) ((a)<(b)?(a):(b))
159 /******************************************************************************
160 * aProvEnumAlgsEx - Defines the capabilities of the CSP personalities.
162 #define RSAENH_MAX_ENUMALGS 24
163 #define RSAENH_PCT1_SSL2_SSL3_TLS1 (CRYPT_FLAG_PCT1|CRYPT_FLAG_SSL2|CRYPT_FLAG_SSL3|CRYPT_FLAG_TLS1)
164 static const PROV_ENUMALGS_EX aProvEnumAlgsEx[5][RSAENH_MAX_ENUMALGS+1] =
167 {CALG_RC2, 40, 40, 56,0, 4,"RC2", 24,"RSA Data Security's RC2"},
168 {CALG_RC4, 40, 40, 56,0, 4,"RC4", 24,"RSA Data Security's RC4"},
169 {CALG_DES, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
170 {CALG_SHA, 160,160, 160,CRYPT_FLAG_SIGNING, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
171 {CALG_MD2, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD2", 23,"Message Digest 2 (MD2)"},
172 {CALG_MD4, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD4", 23,"Message Digest 4 (MD4)"},
173 {CALG_MD5, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD5", 23,"Message Digest 5 (MD5)"},
174 {CALG_SSL3_SHAMD5,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
175 {CALG_MAC, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
176 {CALG_RSA_SIGN, 512,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_SIGN",14,"RSA Signature"},
177 {CALG_RSA_KEYX, 512,384, 1024,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_KEYX",17,"RSA Key Exchange"},
178 {CALG_HMAC, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
179 {0, 0, 0, 0,0, 1,"", 1,""}
182 {CALG_RC2, 128, 40, 128,0, 4,"RC2", 24,"RSA Data Security's RC2"},
183 {CALG_RC4, 128, 40, 128,0, 4,"RC4", 24,"RSA Data Security's RC4"},
184 {CALG_DES, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
185 {CALG_3DES_112, 112,112, 112,0, 13,"3DES TWO KEY",19,"Two Key Triple DES"},
186 {CALG_3DES, 168,168, 168,0, 5,"3DES", 21,"Three Key Triple DES"},
187 {CALG_SHA, 160,160, 160,CRYPT_FLAG_SIGNING, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
188 {CALG_MD2, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD2", 23,"Message Digest 2 (MD2)"},
189 {CALG_MD4, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD4", 23,"Message Digest 4 (MD4)"},
190 {CALG_MD5, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD5", 23,"Message Digest 5 (MD5)"},
191 {CALG_SSL3_SHAMD5,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
192 {CALG_MAC, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
193 {CALG_RSA_SIGN,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_SIGN",14,"RSA Signature"},
194 {CALG_RSA_KEYX,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_KEYX",17,"RSA Key Exchange"},
195 {CALG_HMAC, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
196 {0, 0, 0, 0,0, 1,"", 1,""}
199 {CALG_RC2, 128, 40, 128,0, 4,"RC2", 24,"RSA Data Security's RC2"},
200 {CALG_RC4, 128, 40, 128,0, 4,"RC4", 24,"RSA Data Security's RC4"},
201 {CALG_DES, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
202 {CALG_3DES_112, 112,112, 112,0, 13,"3DES TWO KEY",19,"Two Key Triple DES"},
203 {CALG_3DES, 168,168, 168,0, 5,"3DES", 21,"Three Key Triple DES"},
204 {CALG_SHA, 160,160, 160,CRYPT_FLAG_SIGNING, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
205 {CALG_MD2, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD2", 23,"Message Digest 2 (MD2)"},
206 {CALG_MD4, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD4", 23,"Message Digest 4 (MD4)"},
207 {CALG_MD5, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD5", 23,"Message Digest 5 (MD5)"},
208 {CALG_SSL3_SHAMD5,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
209 {CALG_MAC, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
210 {CALG_RSA_SIGN,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_SIGN",14,"RSA Signature"},
211 {CALG_RSA_KEYX,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_KEYX",17,"RSA Key Exchange"},
212 {CALG_HMAC, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
213 {0, 0, 0, 0,0, 1,"", 1,""}
216 {CALG_RC2, 128, 40, 128,RSAENH_PCT1_SSL2_SSL3_TLS1, 4,"RC2", 24,"RSA Data Security's RC2"},
217 {CALG_RC4, 128, 40, 128,RSAENH_PCT1_SSL2_SSL3_TLS1, 4,"RC4", 24,"RSA Data Security's RC4"},
218 {CALG_DES, 56, 56, 56,RSAENH_PCT1_SSL2_SSL3_TLS1, 4,"DES", 31,"Data Encryption Standard (DES)"},
219 {CALG_3DES_112, 112,112, 112,RSAENH_PCT1_SSL2_SSL3_TLS1,13,"3DES TWO KEY",19,"Two Key Triple DES"},
220 {CALG_3DES, 168,168, 168,RSAENH_PCT1_SSL2_SSL3_TLS1, 5,"3DES", 21,"Three Key Triple DES"},
221 {CALG_SHA,160,160,160,CRYPT_FLAG_SIGNING|RSAENH_PCT1_SSL2_SSL3_TLS1,6,"SHA-1",30,"Secure Hash Algorithm (SHA-1)"},
222 {CALG_MD5,128,128,128,CRYPT_FLAG_SIGNING|RSAENH_PCT1_SSL2_SSL3_TLS1,4,"MD5",23,"Message Digest 5 (MD5)"},
223 {CALG_SSL3_SHAMD5,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
224 {CALG_MAC, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
225 {CALG_RSA_SIGN,1024,384,16384,CRYPT_FLAG_SIGNING|RSAENH_PCT1_SSL2_SSL3_TLS1,9,"RSA_SIGN",14,"RSA Signature"},
226 {CALG_RSA_KEYX,1024,384,16384,CRYPT_FLAG_SIGNING|RSAENH_PCT1_SSL2_SSL3_TLS1,9,"RSA_KEYX",17,"RSA Key Exchange"},
227 {CALG_HMAC, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
228 {CALG_PCT1_MASTER,128,128,128,CRYPT_FLAG_PCT1, 12,"PCT1 MASTER",12,"PCT1 Master"},
229 {CALG_SSL2_MASTER,40,40, 192,CRYPT_FLAG_SSL2, 12,"SSL2 MASTER",12,"SSL2 Master"},
230 {CALG_SSL3_MASTER,384,384,384,CRYPT_FLAG_SSL3, 12,"SSL3 MASTER",12,"SSL3 Master"},
231 {CALG_TLS1_MASTER,384,384,384,CRYPT_FLAG_TLS1, 12,"TLS1 MASTER",12,"TLS1 Master"},
232 {CALG_SCHANNEL_MASTER_HASH,0,0,-1,0, 16,"SCH MASTER HASH",21,"SChannel Master Hash"},
233 {CALG_SCHANNEL_MAC_KEY,0,0,-1,0, 12,"SCH MAC KEY",17,"SChannel MAC Key"},
234 {CALG_SCHANNEL_ENC_KEY,0,0,-1,0, 12,"SCH ENC KEY",24,"SChannel Encryption Key"},
235 {CALG_TLS1PRF, 0, 0, -1,0, 9,"TLS1 PRF", 28,"TLS1 Pseudo Random Function"},
236 {0, 0, 0, 0,0, 1,"", 1,""}
239 {CALG_RC2, 128, 40, 128,0, 4,"RC2", 24,"RSA Data Security's RC2"},
240 {CALG_RC4, 128, 40, 128,0, 4,"RC4", 24,"RSA Data Security's RC4"},
241 {CALG_DES, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
242 {CALG_3DES_112, 112,112, 112,0, 13,"3DES TWO KEY",19,"Two Key Triple DES"},
243 {CALG_3DES, 168,168, 168,0, 5,"3DES", 21,"Three Key Triple DES"},
244 {CALG_AES, 128,128, 128,0, 4,"AES", 35,"Advanced Encryption Standard (AES)"},
245 {CALG_AES_128, 128,128, 128,0, 8,"AES-128", 39,"Advanced Encryption Standard (AES-128)"},
246 {CALG_AES_192, 192,192, 192,0, 8,"AES-192", 39,"Advanced Encryption Standard (AES-192)"},
247 {CALG_AES_256, 256,256, 256,0, 8,"AES-256", 39,"Advanced Encryption Standard (AES-256)"},
248 {CALG_SHA, 160,160, 160,CRYPT_FLAG_SIGNING, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
249 {CALG_SHA_256, 256,256, 256,CRYPT_FLAG_SIGNING, 6,"SHA-256", 30,"Secure Hash Algorithm (SHA-256)"},
250 {CALG_SHA_384, 384,384, 384,CRYPT_FLAG_SIGNING, 6,"SHA-384", 30,"Secure Hash Algorithm (SHA-284)"},
251 {CALG_SHA_512, 512,512, 512,CRYPT_FLAG_SIGNING, 6,"SHA-512", 30,"Secure Hash Algorithm (SHA-512)"},
252 {CALG_MD2, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD2", 23,"Message Digest 2 (MD2)"},
253 {CALG_MD4, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD4", 23,"Message Digest 4 (MD4)"},
254 {CALG_MD5, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD5", 23,"Message Digest 5 (MD5)"},
255 {CALG_SSL3_SHAMD5,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
256 {CALG_MAC, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
257 {CALG_RSA_SIGN,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_SIGN",14,"RSA Signature"},
258 {CALG_RSA_KEYX,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_KEYX",17,"RSA Key Exchange"},
259 {CALG_HMAC, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
260 {0, 0, 0, 0,0, 1,"", 1,""}
264 /******************************************************************************
265 * API forward declarations
267 BOOL WINAPI
268 RSAENH_CPGetKeyParam(
269 HCRYPTPROV hProv,
270 HCRYPTKEY hKey,
271 DWORD dwParam,
272 BYTE *pbData,
273 DWORD *pdwDataLen,
274 DWORD dwFlags
277 BOOL WINAPI
278 RSAENH_CPEncrypt(
279 HCRYPTPROV hProv,
280 HCRYPTKEY hKey,
281 HCRYPTHASH hHash,
282 BOOL Final,
283 DWORD dwFlags,
284 BYTE *pbData,
285 DWORD *pdwDataLen,
286 DWORD dwBufLen
289 BOOL WINAPI
290 RSAENH_CPCreateHash(
291 HCRYPTPROV hProv,
292 ALG_ID Algid,
293 HCRYPTKEY hKey,
294 DWORD dwFlags,
295 HCRYPTHASH *phHash
298 BOOL WINAPI
299 RSAENH_CPSetHashParam(
300 HCRYPTPROV hProv,
301 HCRYPTHASH hHash,
302 DWORD dwParam,
303 BYTE *pbData, DWORD dwFlags
306 BOOL WINAPI
307 RSAENH_CPGetHashParam(
308 HCRYPTPROV hProv,
309 HCRYPTHASH hHash,
310 DWORD dwParam,
311 BYTE *pbData,
312 DWORD *pdwDataLen,
313 DWORD dwFlags
316 BOOL WINAPI
317 RSAENH_CPDestroyHash(
318 HCRYPTPROV hProv,
319 HCRYPTHASH hHash
322 static BOOL crypt_export_key(
323 CRYPTKEY *pCryptKey,
324 HCRYPTKEY hPubKey,
325 DWORD dwBlobType,
326 DWORD dwFlags,
327 BOOL force,
328 BYTE *pbData,
329 DWORD *pdwDataLen
332 static BOOL import_key(
333 HCRYPTPROV hProv,
334 CONST BYTE *pbData,
335 DWORD dwDataLen,
336 HCRYPTKEY hPubKey,
337 DWORD dwFlags,
338 BOOL fStoreKey,
339 HCRYPTKEY *phKey
342 BOOL WINAPI
343 RSAENH_CPHashData(
344 HCRYPTPROV hProv,
345 HCRYPTHASH hHash,
346 CONST BYTE *pbData,
347 DWORD dwDataLen,
348 DWORD dwFlags
351 /******************************************************************************
352 * CSP's handle table (used by all acquired key containers)
354 static struct handle_table handle_table;
356 /******************************************************************************
357 * DllMain (RSAENH.@)
359 * Initializes and destroys the handle table for the CSP's handles.
361 int WINAPI DllMain(HINSTANCE hInstance, DWORD fdwReason, PVOID pvReserved)
363 switch (fdwReason)
365 case DLL_PROCESS_ATTACH:
366 instance = hInstance;
367 DisableThreadLibraryCalls(hInstance);
368 init_handle_table(&handle_table);
369 break;
371 case DLL_PROCESS_DETACH:
372 destroy_handle_table(&handle_table);
373 break;
375 return 1;
378 /******************************************************************************
379 * copy_param [Internal]
381 * Helper function that supports the standard WINAPI protocol for querying data
382 * of dynamic size.
384 * PARAMS
385 * pbBuffer [O] Buffer where the queried parameter is copied to, if it is large enough.
386 * May be NUL if the required buffer size is to be queried only.
387 * pdwBufferSize [I/O] In: Size of the buffer at pbBuffer
388 * Out: Size of parameter pbParam
389 * pbParam [I] Parameter value.
390 * dwParamSize [I] Size of pbParam
392 * RETURN
393 * Success: TRUE (pbParam was copied into pbBuffer or pbBuffer is NULL)
394 * Failure: FALSE (pbBuffer is not large enough to hold pbParam). Last error: ERROR_MORE_DATA
396 static inline BOOL copy_param(
397 BYTE *pbBuffer, DWORD *pdwBufferSize, CONST BYTE *pbParam, DWORD dwParamSize)
399 if (pbBuffer)
401 if (dwParamSize > *pdwBufferSize)
403 SetLastError(ERROR_MORE_DATA);
404 *pdwBufferSize = dwParamSize;
405 return FALSE;
407 memcpy(pbBuffer, pbParam, dwParamSize);
409 *pdwBufferSize = dwParamSize;
410 return TRUE;
413 /******************************************************************************
414 * get_algid_info [Internal]
416 * Query CSP capabilities for a given crypto algorithm.
418 * PARAMS
419 * hProv [I] Handle to a key container of the CSP whose capabilities are to be queried.
420 * algid [I] Identifier of the crypto algorithm about which information is requested.
422 * RETURNS
423 * Success: Pointer to a PROV_ENUMALGS_EX struct containing information about the crypto algorithm.
424 * Failure: NULL (algid not supported)
426 static inline const PROV_ENUMALGS_EX* get_algid_info(HCRYPTPROV hProv, ALG_ID algid) {
427 const PROV_ENUMALGS_EX *iterator;
428 KEYCONTAINER *pKeyContainer;
430 if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER, (OBJECTHDR**)&pKeyContainer)) {
431 SetLastError(NTE_BAD_UID);
432 return NULL;
435 for (iterator = aProvEnumAlgsEx[pKeyContainer->dwPersonality]; iterator->aiAlgid; iterator++) {
436 if (iterator->aiAlgid == algid) return iterator;
439 SetLastError(NTE_BAD_ALGID);
440 return NULL;
443 /******************************************************************************
444 * copy_data_blob [Internal]
446 * deeply copies a DATA_BLOB
448 * PARAMS
449 * dst [O] That's where the blob will be copied to
450 * src [I] Source blob
452 * RETURNS
453 * Success: TRUE
454 * Failure: FALSE (GetLastError() == NTE_NO_MEMORY
456 * NOTES
457 * Use free_data_blob to release resources occupied by copy_data_blob.
459 static inline BOOL copy_data_blob(PCRYPT_DATA_BLOB dst, CONST PCRYPT_DATA_BLOB src) {
460 dst->pbData = HeapAlloc(GetProcessHeap(), 0, src->cbData);
461 if (!dst->pbData) {
462 SetLastError(NTE_NO_MEMORY);
463 return FALSE;
465 dst->cbData = src->cbData;
466 memcpy(dst->pbData, src->pbData, src->cbData);
467 return TRUE;
470 /******************************************************************************
471 * concat_data_blobs [Internal]
473 * Concatenates two blobs
475 * PARAMS
476 * dst [O] The new blob will be copied here
477 * src1 [I] Prefix blob
478 * src2 [I] Appendix blob
480 * RETURNS
481 * Success: TRUE
482 * Failure: FALSE (GetLastError() == NTE_NO_MEMORY)
484 * NOTES
485 * Release resources occupied by concat_data_blobs with free_data_blobs
487 static inline BOOL concat_data_blobs(PCRYPT_DATA_BLOB dst, CONST PCRYPT_DATA_BLOB src1,
488 CONST PCRYPT_DATA_BLOB src2)
490 dst->cbData = src1->cbData + src2->cbData;
491 dst->pbData = HeapAlloc(GetProcessHeap(), 0, dst->cbData);
492 if (!dst->pbData) {
493 SetLastError(NTE_NO_MEMORY);
494 return FALSE;
496 memcpy(dst->pbData, src1->pbData, src1->cbData);
497 memcpy(dst->pbData + src1->cbData, src2->pbData, src2->cbData);
498 return TRUE;
501 /******************************************************************************
502 * free_data_blob [Internal]
504 * releases resource occupied by a dynamically allocated CRYPT_DATA_BLOB
506 * PARAMS
507 * pBlob [I] Heap space occupied by pBlob->pbData is released
509 static inline void free_data_blob(PCRYPT_DATA_BLOB pBlob) {
510 HeapFree(GetProcessHeap(), 0, pBlob->pbData);
513 /******************************************************************************
514 * init_data_blob [Internal]
516 static inline void init_data_blob(PCRYPT_DATA_BLOB pBlob) {
517 pBlob->pbData = NULL;
518 pBlob->cbData = 0;
521 /******************************************************************************
522 * free_hmac_info [Internal]
524 * Deeply free an HMAC_INFO struct.
526 * PARAMS
527 * hmac_info [I] Pointer to the HMAC_INFO struct to be freed.
529 * NOTES
530 * See Internet RFC 2104 for details on the HMAC algorithm.
532 static inline void free_hmac_info(PHMAC_INFO hmac_info) {
533 if (!hmac_info) return;
534 HeapFree(GetProcessHeap(), 0, hmac_info->pbInnerString);
535 HeapFree(GetProcessHeap(), 0, hmac_info->pbOuterString);
536 HeapFree(GetProcessHeap(), 0, hmac_info);
539 /******************************************************************************
540 * copy_hmac_info [Internal]
542 * Deeply copy an HMAC_INFO struct
544 * PARAMS
545 * dst [O] Pointer to a location where the pointer to the HMAC_INFO copy will be stored.
546 * src [I] Pointer to the HMAC_INFO struct to be copied.
548 * RETURNS
549 * Success: TRUE
550 * Failure: FALSE
552 * NOTES
553 * See Internet RFC 2104 for details on the HMAC algorithm.
555 static BOOL copy_hmac_info(PHMAC_INFO *dst, const HMAC_INFO *src) {
556 if (!src) return FALSE;
557 *dst = HeapAlloc(GetProcessHeap(), 0, sizeof(HMAC_INFO));
558 if (!*dst) return FALSE;
559 **dst = *src;
560 (*dst)->pbInnerString = NULL;
561 (*dst)->pbOuterString = NULL;
562 if ((*dst)->cbInnerString == 0) (*dst)->cbInnerString = RSAENH_HMAC_DEF_PAD_LEN;
563 (*dst)->pbInnerString = HeapAlloc(GetProcessHeap(), 0, (*dst)->cbInnerString);
564 if (!(*dst)->pbInnerString) {
565 free_hmac_info(*dst);
566 return FALSE;
568 if (src->cbInnerString)
569 memcpy((*dst)->pbInnerString, src->pbInnerString, src->cbInnerString);
570 else
571 memset((*dst)->pbInnerString, RSAENH_HMAC_DEF_IPAD_CHAR, RSAENH_HMAC_DEF_PAD_LEN);
572 if ((*dst)->cbOuterString == 0) (*dst)->cbOuterString = RSAENH_HMAC_DEF_PAD_LEN;
573 (*dst)->pbOuterString = HeapAlloc(GetProcessHeap(), 0, (*dst)->cbOuterString);
574 if (!(*dst)->pbOuterString) {
575 free_hmac_info(*dst);
576 return FALSE;
578 if (src->cbOuterString)
579 memcpy((*dst)->pbOuterString, src->pbOuterString, src->cbOuterString);
580 else
581 memset((*dst)->pbOuterString, RSAENH_HMAC_DEF_OPAD_CHAR, RSAENH_HMAC_DEF_PAD_LEN);
582 return TRUE;
585 /******************************************************************************
586 * destroy_hash [Internal]
588 * Destructor for hash objects
590 * PARAMS
591 * pCryptHash [I] Pointer to the hash object to be destroyed.
592 * Will be invalid after function returns!
594 static void destroy_hash(OBJECTHDR *pObject)
596 CRYPTHASH *pCryptHash = (CRYPTHASH*)pObject;
598 free_hmac_info(pCryptHash->pHMACInfo);
599 free_data_blob(&pCryptHash->tpPRFParams.blobLabel);
600 free_data_blob(&pCryptHash->tpPRFParams.blobSeed);
601 HeapFree(GetProcessHeap(), 0, pCryptHash);
604 /******************************************************************************
605 * init_hash [Internal]
607 * Initialize (or reset) a hash object
609 * PARAMS
610 * pCryptHash [I] The hash object to be initialized.
612 static inline BOOL init_hash(CRYPTHASH *pCryptHash) {
613 DWORD dwLen;
615 switch (pCryptHash->aiAlgid)
617 case CALG_HMAC:
618 if (pCryptHash->pHMACInfo) {
619 const PROV_ENUMALGS_EX *pAlgInfo;
621 pAlgInfo = get_algid_info(pCryptHash->hProv, pCryptHash->pHMACInfo->HashAlgid);
622 if (!pAlgInfo) return FALSE;
623 pCryptHash->dwHashSize = pAlgInfo->dwDefaultLen >> 3;
624 init_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context);
625 update_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
626 pCryptHash->pHMACInfo->pbInnerString,
627 pCryptHash->pHMACInfo->cbInnerString);
629 return TRUE;
631 case CALG_MAC:
632 dwLen = sizeof(DWORD);
633 RSAENH_CPGetKeyParam(pCryptHash->hProv, pCryptHash->hKey, KP_BLOCKLEN,
634 (BYTE*)&pCryptHash->dwHashSize, &dwLen, 0);
635 pCryptHash->dwHashSize >>= 3;
636 return TRUE;
638 default:
639 return init_hash_impl(pCryptHash->aiAlgid, &pCryptHash->context);
643 /******************************************************************************
644 * update_hash [Internal]
646 * Hashes the given data and updates the hash object's state accordingly
648 * PARAMS
649 * pCryptHash [I] Hash object to be updated.
650 * pbData [I] Pointer to data stream to be hashed.
651 * dwDataLen [I] Length of data stream.
653 static inline void update_hash(CRYPTHASH *pCryptHash, CONST BYTE *pbData, DWORD dwDataLen) {
654 BYTE *pbTemp;
656 switch (pCryptHash->aiAlgid)
658 case CALG_HMAC:
659 if (pCryptHash->pHMACInfo)
660 update_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
661 pbData, dwDataLen);
662 break;
664 case CALG_MAC:
665 pbTemp = HeapAlloc(GetProcessHeap(), 0, dwDataLen);
666 if (!pbTemp) return;
667 memcpy(pbTemp, pbData, dwDataLen);
668 RSAENH_CPEncrypt(pCryptHash->hProv, pCryptHash->hKey, 0, FALSE, 0,
669 pbTemp, &dwDataLen, dwDataLen);
670 HeapFree(GetProcessHeap(), 0, pbTemp);
671 break;
673 default:
674 update_hash_impl(pCryptHash->aiAlgid, &pCryptHash->context, pbData, dwDataLen);
678 /******************************************************************************
679 * finalize_hash [Internal]
681 * Finalizes the hash, after all data has been hashed with update_hash.
682 * No additional data can be hashed afterwards until the hash gets initialized again.
684 * PARAMS
685 * pCryptHash [I] Hash object to be finalized.
687 static inline void finalize_hash(CRYPTHASH *pCryptHash) {
688 DWORD dwDataLen;
690 switch (pCryptHash->aiAlgid)
692 case CALG_HMAC:
693 if (pCryptHash->pHMACInfo) {
694 BYTE abHashValue[RSAENH_MAX_HASH_SIZE];
696 finalize_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
697 pCryptHash->abHashValue);
698 memcpy(abHashValue, pCryptHash->abHashValue, pCryptHash->dwHashSize);
699 init_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context);
700 update_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
701 pCryptHash->pHMACInfo->pbOuterString,
702 pCryptHash->pHMACInfo->cbOuterString);
703 update_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
704 abHashValue, pCryptHash->dwHashSize);
705 finalize_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
706 pCryptHash->abHashValue);
708 break;
710 case CALG_MAC:
711 dwDataLen = 0;
712 RSAENH_CPEncrypt(pCryptHash->hProv, pCryptHash->hKey, 0, TRUE, 0,
713 pCryptHash->abHashValue, &dwDataLen, pCryptHash->dwHashSize);
714 break;
716 default:
717 finalize_hash_impl(pCryptHash->aiAlgid, &pCryptHash->context, pCryptHash->abHashValue);
721 /******************************************************************************
722 * destroy_key [Internal]
724 * Destructor for key objects
726 * PARAMS
727 * pCryptKey [I] Pointer to the key object to be destroyed.
728 * Will be invalid after function returns!
730 static void destroy_key(OBJECTHDR *pObject)
732 CRYPTKEY *pCryptKey = (CRYPTKEY*)pObject;
734 free_key_impl(pCryptKey->aiAlgid, &pCryptKey->context);
735 free_data_blob(&pCryptKey->siSChannelInfo.blobClientRandom);
736 free_data_blob(&pCryptKey->siSChannelInfo.blobServerRandom);
737 free_data_blob(&pCryptKey->blobHmacKey);
738 HeapFree(GetProcessHeap(), 0, pCryptKey);
741 /******************************************************************************
742 * setup_key [Internal]
744 * Initialize (or reset) a key object
746 * PARAMS
747 * pCryptKey [I] The key object to be initialized.
749 static inline void setup_key(CRYPTKEY *pCryptKey) {
750 pCryptKey->dwState = RSAENH_KEYSTATE_IDLE;
751 memcpy(pCryptKey->abChainVector, pCryptKey->abInitVector, sizeof(pCryptKey->abChainVector));
752 setup_key_impl(pCryptKey->aiAlgid, &pCryptKey->context, pCryptKey->dwKeyLen,
753 pCryptKey->dwEffectiveKeyLen, pCryptKey->dwSaltLen,
754 pCryptKey->abKeyValue);
757 /******************************************************************************
758 * new_key [Internal]
760 * Creates a new key object without assigning the actual binary key value.
761 * This is done by CPDeriveKey, CPGenKey or CPImportKey, which call this function.
763 * PARAMS
764 * hProv [I] Handle to the provider to which the created key will belong.
765 * aiAlgid [I] The new key shall use the crypto algorithm identified by aiAlgid.
766 * dwFlags [I] Upper 16 bits give the key length.
767 * Lower 16 bits: CRYPT_EXPORTABLE, CRYPT_CREATE_SALT,
768 * CRYPT_NO_SALT
769 * ppCryptKey [O] Pointer to the created key
771 * RETURNS
772 * Success: Handle to the created key.
773 * Failure: INVALID_HANDLE_VALUE
775 static HCRYPTKEY new_key(HCRYPTPROV hProv, ALG_ID aiAlgid, DWORD dwFlags, CRYPTKEY **ppCryptKey)
777 HCRYPTKEY hCryptKey;
778 CRYPTKEY *pCryptKey;
779 DWORD dwKeyLen = HIWORD(dwFlags);
780 const PROV_ENUMALGS_EX *peaAlgidInfo;
782 *ppCryptKey = NULL;
785 * Retrieve the CSP's capabilities for the given ALG_ID value
787 peaAlgidInfo = get_algid_info(hProv, aiAlgid);
788 if (!peaAlgidInfo) return (HCRYPTKEY)INVALID_HANDLE_VALUE;
790 TRACE("alg = %s, dwKeyLen = %d\n", debugstr_a(peaAlgidInfo->szName),
791 dwKeyLen);
793 * Assume the default key length, if none is specified explicitly
795 if (dwKeyLen == 0) dwKeyLen = peaAlgidInfo->dwDefaultLen;
798 * Check if the requested key length is supported by the current CSP.
799 * Adjust key length's for DES algorithms.
801 switch (aiAlgid) {
802 case CALG_DES:
803 if (dwKeyLen == RSAENH_DES_EFFECTIVE_KEYLEN) {
804 dwKeyLen = RSAENH_DES_STORAGE_KEYLEN;
806 if (dwKeyLen != RSAENH_DES_STORAGE_KEYLEN) {
807 SetLastError(NTE_BAD_FLAGS);
808 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
810 break;
812 case CALG_3DES_112:
813 if (dwKeyLen == RSAENH_3DES112_EFFECTIVE_KEYLEN) {
814 dwKeyLen = RSAENH_3DES112_STORAGE_KEYLEN;
816 if (dwKeyLen != RSAENH_3DES112_STORAGE_KEYLEN) {
817 SetLastError(NTE_BAD_FLAGS);
818 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
820 break;
822 case CALG_3DES:
823 if (dwKeyLen == RSAENH_3DES_EFFECTIVE_KEYLEN) {
824 dwKeyLen = RSAENH_3DES_STORAGE_KEYLEN;
826 if (dwKeyLen != RSAENH_3DES_STORAGE_KEYLEN) {
827 SetLastError(NTE_BAD_FLAGS);
828 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
830 break;
832 case CALG_HMAC:
833 /* Avoid the key length check for HMAC keys, which have unlimited
834 * length.
836 break;
838 default:
839 if (dwKeyLen % 8 ||
840 dwKeyLen > peaAlgidInfo->dwMaxLen ||
841 dwKeyLen < peaAlgidInfo->dwMinLen)
843 TRACE("key len %d out of bounds (%d, %d)\n", dwKeyLen,
844 peaAlgidInfo->dwMinLen, peaAlgidInfo->dwMaxLen);
845 SetLastError(NTE_BAD_DATA);
846 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
850 hCryptKey = new_object(&handle_table, sizeof(CRYPTKEY), RSAENH_MAGIC_KEY,
851 destroy_key, (OBJECTHDR**)&pCryptKey);
852 if (hCryptKey != (HCRYPTKEY)INVALID_HANDLE_VALUE)
854 pCryptKey->aiAlgid = aiAlgid;
855 pCryptKey->hProv = hProv;
856 pCryptKey->dwModeBits = 0;
857 pCryptKey->dwPermissions = CRYPT_ENCRYPT | CRYPT_DECRYPT | CRYPT_READ | CRYPT_WRITE |
858 CRYPT_MAC;
859 if (dwFlags & CRYPT_EXPORTABLE)
860 pCryptKey->dwPermissions |= CRYPT_EXPORT;
861 pCryptKey->dwKeyLen = dwKeyLen >> 3;
862 pCryptKey->dwEffectiveKeyLen = 0;
863 if ((dwFlags & CRYPT_CREATE_SALT) || (dwKeyLen == 40 && !(dwFlags & CRYPT_NO_SALT)))
864 pCryptKey->dwSaltLen = 16 /*FIXME*/ - pCryptKey->dwKeyLen;
865 else
866 pCryptKey->dwSaltLen = 0;
867 memset(pCryptKey->abKeyValue, 0, sizeof(pCryptKey->abKeyValue));
868 memset(pCryptKey->abInitVector, 0, sizeof(pCryptKey->abInitVector));
869 memset(&pCryptKey->siSChannelInfo.saEncAlg, 0, sizeof(pCryptKey->siSChannelInfo.saEncAlg));
870 memset(&pCryptKey->siSChannelInfo.saMACAlg, 0, sizeof(pCryptKey->siSChannelInfo.saMACAlg));
871 init_data_blob(&pCryptKey->siSChannelInfo.blobClientRandom);
872 init_data_blob(&pCryptKey->siSChannelInfo.blobServerRandom);
873 init_data_blob(&pCryptKey->blobHmacKey);
875 switch(aiAlgid)
877 case CALG_PCT1_MASTER:
878 case CALG_SSL2_MASTER:
879 case CALG_SSL3_MASTER:
880 case CALG_TLS1_MASTER:
881 case CALG_RC4:
882 pCryptKey->dwBlockLen = 0;
883 pCryptKey->dwMode = 0;
884 break;
886 case CALG_RC2:
887 case CALG_DES:
888 case CALG_3DES_112:
889 case CALG_3DES:
890 pCryptKey->dwBlockLen = 8;
891 pCryptKey->dwMode = CRYPT_MODE_CBC;
892 break;
894 case CALG_AES:
895 case CALG_AES_128:
896 case CALG_AES_192:
897 case CALG_AES_256:
898 pCryptKey->dwBlockLen = 16;
899 pCryptKey->dwMode = CRYPT_MODE_ECB;
900 break;
902 case CALG_RSA_KEYX:
903 case CALG_RSA_SIGN:
904 pCryptKey->dwBlockLen = dwKeyLen >> 3;
905 pCryptKey->dwMode = 0;
906 break;
908 case CALG_HMAC:
909 pCryptKey->dwBlockLen = 0;
910 pCryptKey->dwMode = 0;
911 break;
914 *ppCryptKey = pCryptKey;
917 return hCryptKey;
920 /******************************************************************************
921 * map_key_spec_to_key_pair_name [Internal]
923 * Returns the name of the registry value associated with a key spec.
925 * PARAMS
926 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
928 * RETURNS
929 * Success: Name of registry value.
930 * Failure: NULL
932 static LPCSTR map_key_spec_to_key_pair_name(DWORD dwKeySpec)
934 LPCSTR szValueName;
936 switch (dwKeySpec)
938 case AT_KEYEXCHANGE:
939 szValueName = "KeyExchangeKeyPair";
940 break;
941 case AT_SIGNATURE:
942 szValueName = "SignatureKeyPair";
943 break;
944 default:
945 WARN("invalid key spec %d\n", dwKeySpec);
946 szValueName = NULL;
948 return szValueName;
951 /******************************************************************************
952 * store_key_pair [Internal]
954 * Stores a key pair to the registry
956 * PARAMS
957 * hCryptKey [I] Handle to the key to be stored
958 * hKey [I] Registry key where the key pair is to be stored
959 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
960 * dwFlags [I] Flags for protecting the key
962 static void store_key_pair(HCRYPTKEY hCryptKey, HKEY hKey, DWORD dwKeySpec, DWORD dwFlags)
964 LPCSTR szValueName;
965 DATA_BLOB blobIn, blobOut;
966 CRYPTKEY *pKey;
967 DWORD dwLen;
968 BYTE *pbKey;
970 if (!(szValueName = map_key_spec_to_key_pair_name(dwKeySpec)))
971 return;
972 if (lookup_handle(&handle_table, hCryptKey, RSAENH_MAGIC_KEY,
973 (OBJECTHDR**)&pKey))
975 if (crypt_export_key(pKey, 0, PRIVATEKEYBLOB, 0, TRUE, 0, &dwLen))
977 pbKey = HeapAlloc(GetProcessHeap(), 0, dwLen);
978 if (pbKey)
980 if (crypt_export_key(pKey, 0, PRIVATEKEYBLOB, 0, TRUE, pbKey,
981 &dwLen))
983 blobIn.pbData = pbKey;
984 blobIn.cbData = dwLen;
986 if (CryptProtectData(&blobIn, NULL, NULL, NULL, NULL,
987 dwFlags, &blobOut))
989 RegSetValueExA(hKey, szValueName, 0, REG_BINARY,
990 blobOut.pbData, blobOut.cbData);
991 LocalFree(blobOut.pbData);
994 HeapFree(GetProcessHeap(), 0, pbKey);
1000 /******************************************************************************
1001 * map_key_spec_to_permissions_name [Internal]
1003 * Returns the name of the registry value associated with the permissions for
1004 * a key spec.
1006 * PARAMS
1007 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
1009 * RETURNS
1010 * Success: Name of registry value.
1011 * Failure: NULL
1013 static LPCSTR map_key_spec_to_permissions_name(DWORD dwKeySpec)
1015 LPCSTR szValueName;
1017 switch (dwKeySpec)
1019 case AT_KEYEXCHANGE:
1020 szValueName = "KeyExchangePermissions";
1021 break;
1022 case AT_SIGNATURE:
1023 szValueName = "SignaturePermissions";
1024 break;
1025 default:
1026 WARN("invalid key spec %d\n", dwKeySpec);
1027 szValueName = NULL;
1029 return szValueName;
1032 /******************************************************************************
1033 * store_key_permissions [Internal]
1035 * Stores a key's permissions to the registry
1037 * PARAMS
1038 * hCryptKey [I] Handle to the key whose permissions are to be stored
1039 * hKey [I] Registry key where the key permissions are to be stored
1040 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
1042 static void store_key_permissions(HCRYPTKEY hCryptKey, HKEY hKey, DWORD dwKeySpec)
1044 LPCSTR szValueName;
1045 CRYPTKEY *pKey;
1047 if (!(szValueName = map_key_spec_to_permissions_name(dwKeySpec)))
1048 return;
1049 if (lookup_handle(&handle_table, hCryptKey, RSAENH_MAGIC_KEY,
1050 (OBJECTHDR**)&pKey))
1051 RegSetValueExA(hKey, szValueName, 0, REG_DWORD,
1052 (BYTE *)&pKey->dwPermissions,
1053 sizeof(pKey->dwPermissions));
1056 /******************************************************************************
1057 * create_container_key [Internal]
1059 * Creates the registry key for a key container's persistent storage.
1061 * PARAMS
1062 * pKeyContainer [I] Pointer to the key container
1063 * sam [I] Desired registry access
1064 * phKey [O] Returned key
1066 static BOOL create_container_key(KEYCONTAINER *pKeyContainer, REGSAM sam, HKEY *phKey)
1068 CHAR szRSABase[MAX_PATH];
1069 HKEY hRootKey;
1071 sprintf(szRSABase, RSAENH_REGKEY, pKeyContainer->szName);
1073 if (pKeyContainer->dwFlags & CRYPT_MACHINE_KEYSET)
1074 hRootKey = HKEY_LOCAL_MACHINE;
1075 else
1076 hRootKey = HKEY_CURRENT_USER;
1078 /* @@ Wine registry key: HKLM\Software\Wine\Crypto\RSA */
1079 /* @@ Wine registry key: HKCU\Software\Wine\Crypto\RSA */
1080 return RegCreateKeyExA(hRootKey, szRSABase, 0, NULL,
1081 REG_OPTION_NON_VOLATILE, sam, NULL, phKey, NULL)
1082 == ERROR_SUCCESS;
1085 /******************************************************************************
1086 * open_container_key [Internal]
1088 * Opens a key container's persistent storage for reading.
1090 * PARAMS
1091 * pszContainerName [I] Name of the container to be opened. May be the empty
1092 * string if the parent key of all containers is to be
1093 * opened.
1094 * dwFlags [I] Flags indicating which keyset to be opened.
1095 * phKey [O] Returned key
1097 static BOOL open_container_key(LPCSTR pszContainerName, DWORD dwFlags, HKEY *phKey)
1099 CHAR szRSABase[MAX_PATH];
1100 HKEY hRootKey;
1102 sprintf(szRSABase, RSAENH_REGKEY, pszContainerName);
1104 if (dwFlags & CRYPT_MACHINE_KEYSET)
1105 hRootKey = HKEY_LOCAL_MACHINE;
1106 else
1107 hRootKey = HKEY_CURRENT_USER;
1109 /* @@ Wine registry key: HKLM\Software\Wine\Crypto\RSA */
1110 /* @@ Wine registry key: HKCU\Software\Wine\Crypto\RSA */
1111 return RegOpenKeyExA(hRootKey, szRSABase, 0, KEY_READ, phKey) ==
1112 ERROR_SUCCESS;
1115 /******************************************************************************
1116 * delete_container_key [Internal]
1118 * Deletes a key container's persistent storage.
1120 * PARAMS
1121 * pszContainerName [I] Name of the container to be opened.
1122 * dwFlags [I] Flags indicating which keyset to be opened.
1124 static BOOL delete_container_key(LPCSTR pszContainerName, DWORD dwFlags)
1126 CHAR szRegKey[MAX_PATH];
1128 if (snprintf(szRegKey, MAX_PATH, RSAENH_REGKEY, pszContainerName) >= MAX_PATH) {
1129 SetLastError(NTE_BAD_KEYSET_PARAM);
1130 return FALSE;
1131 } else {
1132 HKEY hRootKey;
1133 if (dwFlags & CRYPT_MACHINE_KEYSET)
1134 hRootKey = HKEY_LOCAL_MACHINE;
1135 else
1136 hRootKey = HKEY_CURRENT_USER;
1137 if (!RegDeleteKeyA(hRootKey, szRegKey)) {
1138 SetLastError(ERROR_SUCCESS);
1139 return TRUE;
1140 } else {
1141 SetLastError(NTE_BAD_KEYSET);
1142 return FALSE;
1147 /******************************************************************************
1148 * store_key_container_keys [Internal]
1150 * Stores key container's keys in a persistent location.
1152 * PARAMS
1153 * pKeyContainer [I] Pointer to the key container whose keys are to be saved
1155 static void store_key_container_keys(KEYCONTAINER *pKeyContainer)
1157 HKEY hKey;
1158 DWORD dwFlags;
1160 /* On WinXP, persistent keys are stored in a file located at:
1161 * $AppData$\\Microsoft\\Crypto\\RSA\\$SID$\\some_hex_string
1164 if (pKeyContainer->dwFlags & CRYPT_MACHINE_KEYSET)
1165 dwFlags = CRYPTPROTECT_LOCAL_MACHINE;
1166 else
1167 dwFlags = 0;
1169 if (create_container_key(pKeyContainer, KEY_WRITE, &hKey))
1171 store_key_pair(pKeyContainer->hKeyExchangeKeyPair, hKey,
1172 AT_KEYEXCHANGE, dwFlags);
1173 store_key_pair(pKeyContainer->hSignatureKeyPair, hKey,
1174 AT_SIGNATURE, dwFlags);
1175 RegCloseKey(hKey);
1179 /******************************************************************************
1180 * store_key_container_permissions [Internal]
1182 * Stores key container's key permissions in a persistent location.
1184 * PARAMS
1185 * pKeyContainer [I] Pointer to the key container whose key permissions are to
1186 * be saved
1188 static void store_key_container_permissions(KEYCONTAINER *pKeyContainer)
1190 HKEY hKey;
1192 if (create_container_key(pKeyContainer, KEY_WRITE, &hKey))
1194 store_key_permissions(pKeyContainer->hKeyExchangeKeyPair, hKey,
1195 AT_KEYEXCHANGE);
1196 store_key_permissions(pKeyContainer->hSignatureKeyPair, hKey,
1197 AT_SIGNATURE);
1198 RegCloseKey(hKey);
1202 /******************************************************************************
1203 * release_key_container_keys [Internal]
1205 * Releases key container's keys.
1207 * PARAMS
1208 * pKeyContainer [I] Pointer to the key container whose keys are to be released.
1210 static void release_key_container_keys(KEYCONTAINER *pKeyContainer)
1212 release_handle(&handle_table, pKeyContainer->hKeyExchangeKeyPair,
1213 RSAENH_MAGIC_KEY);
1214 release_handle(&handle_table, pKeyContainer->hSignatureKeyPair,
1215 RSAENH_MAGIC_KEY);
1218 /******************************************************************************
1219 * destroy_key_container [Internal]
1221 * Destructor for key containers.
1223 * PARAMS
1224 * pObjectHdr [I] Pointer to the key container to be destroyed.
1226 static void destroy_key_container(OBJECTHDR *pObjectHdr)
1228 KEYCONTAINER *pKeyContainer = (KEYCONTAINER*)pObjectHdr;
1230 if (!(pKeyContainer->dwFlags & CRYPT_VERIFYCONTEXT))
1232 store_key_container_keys(pKeyContainer);
1233 store_key_container_permissions(pKeyContainer);
1234 release_key_container_keys(pKeyContainer);
1236 else
1237 release_key_container_keys(pKeyContainer);
1238 HeapFree( GetProcessHeap(), 0, pKeyContainer );
1241 /******************************************************************************
1242 * new_key_container [Internal]
1244 * Create a new key container. The personality (RSA Base, Strong or Enhanced CP)
1245 * of the CSP is determined via the pVTable->pszProvName string.
1247 * PARAMS
1248 * pszContainerName [I] Name of the key container.
1249 * pVTable [I] Callback functions and context info provided by the OS
1251 * RETURNS
1252 * Success: Handle to the new key container.
1253 * Failure: INVALID_HANDLE_VALUE
1255 static HCRYPTPROV new_key_container(PCCH pszContainerName, DWORD dwFlags, const VTableProvStruc *pVTable)
1257 KEYCONTAINER *pKeyContainer;
1258 HCRYPTPROV hKeyContainer;
1260 hKeyContainer = new_object(&handle_table, sizeof(KEYCONTAINER), RSAENH_MAGIC_CONTAINER,
1261 destroy_key_container, (OBJECTHDR**)&pKeyContainer);
1262 if (hKeyContainer != (HCRYPTPROV)INVALID_HANDLE_VALUE)
1264 lstrcpynA(pKeyContainer->szName, pszContainerName, MAX_PATH);
1265 pKeyContainer->dwFlags = dwFlags;
1266 pKeyContainer->dwEnumAlgsCtr = 0;
1267 pKeyContainer->hKeyExchangeKeyPair = (HCRYPTKEY)INVALID_HANDLE_VALUE;
1268 pKeyContainer->hSignatureKeyPair = (HCRYPTKEY)INVALID_HANDLE_VALUE;
1269 if (pVTable && pVTable->pszProvName) {
1270 lstrcpynA(pKeyContainer->szProvName, pVTable->pszProvName, MAX_PATH);
1271 if (!strcmp(pVTable->pszProvName, MS_DEF_PROV_A)) {
1272 pKeyContainer->dwPersonality = RSAENH_PERSONALITY_BASE;
1273 } else if (!strcmp(pVTable->pszProvName, MS_ENHANCED_PROV_A)) {
1274 pKeyContainer->dwPersonality = RSAENH_PERSONALITY_ENHANCED;
1275 } else if (!strcmp(pVTable->pszProvName, MS_DEF_RSA_SCHANNEL_PROV_A)) {
1276 pKeyContainer->dwPersonality = RSAENH_PERSONALITY_SCHANNEL;
1277 } else if (!strcmp(pVTable->pszProvName, MS_ENH_RSA_AES_PROV_A)) {
1278 pKeyContainer->dwPersonality = RSAENH_PERSONALITY_AES;
1279 } else {
1280 pKeyContainer->dwPersonality = RSAENH_PERSONALITY_STRONG;
1284 /* The new key container has to be inserted into the CSP immediately
1285 * after creation to be available for CPGetProvParam's PP_ENUMCONTAINERS. */
1286 if (!(dwFlags & CRYPT_VERIFYCONTEXT)) {
1287 HKEY hKey;
1289 if (create_container_key(pKeyContainer, KEY_WRITE, &hKey))
1290 RegCloseKey(hKey);
1294 return hKeyContainer;
1297 /******************************************************************************
1298 * read_key_value [Internal]
1300 * Reads a key pair value from the registry
1302 * PARAMS
1303 * hKeyContainer [I] Crypt provider to use to import the key
1304 * hKey [I] Registry key from which to read the key pair
1305 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
1306 * dwFlags [I] Flags for unprotecting the key
1307 * phCryptKey [O] Returned key
1309 static BOOL read_key_value(HCRYPTPROV hKeyContainer, HKEY hKey, DWORD dwKeySpec, DWORD dwFlags, HCRYPTKEY *phCryptKey)
1311 LPCSTR szValueName;
1312 DWORD dwValueType, dwLen;
1313 BYTE *pbKey;
1314 DATA_BLOB blobIn, blobOut;
1315 BOOL ret = FALSE;
1317 if (!(szValueName = map_key_spec_to_key_pair_name(dwKeySpec)))
1318 return FALSE;
1319 if (RegQueryValueExA(hKey, szValueName, 0, &dwValueType, NULL, &dwLen) ==
1320 ERROR_SUCCESS)
1322 pbKey = HeapAlloc(GetProcessHeap(), 0, dwLen);
1323 if (pbKey)
1325 if (RegQueryValueExA(hKey, szValueName, 0, &dwValueType, pbKey, &dwLen) ==
1326 ERROR_SUCCESS)
1328 blobIn.pbData = pbKey;
1329 blobIn.cbData = dwLen;
1331 if (CryptUnprotectData(&blobIn, NULL, NULL, NULL, NULL,
1332 dwFlags, &blobOut))
1334 ret = import_key(hKeyContainer, blobOut.pbData, blobOut.cbData, 0, 0,
1335 FALSE, phCryptKey);
1336 LocalFree(blobOut.pbData);
1339 HeapFree(GetProcessHeap(), 0, pbKey);
1342 if (ret)
1344 CRYPTKEY *pKey;
1346 if (lookup_handle(&handle_table, *phCryptKey, RSAENH_MAGIC_KEY,
1347 (OBJECTHDR**)&pKey))
1349 if ((szValueName = map_key_spec_to_permissions_name(dwKeySpec)))
1351 dwLen = sizeof(pKey->dwPermissions);
1352 RegQueryValueExA(hKey, szValueName, 0, NULL,
1353 (BYTE *)&pKey->dwPermissions, &dwLen);
1357 return ret;
1360 /******************************************************************************
1361 * read_key_container [Internal]
1363 * Tries to read the persistent state of the key container (mainly the signature
1364 * and key exchange private keys) given by pszContainerName.
1366 * PARAMS
1367 * pszContainerName [I] Name of the key container to read from the registry
1368 * pVTable [I] Pointer to context data provided by the operating system
1370 * RETURNS
1371 * Success: Handle to the key container read from the registry
1372 * Failure: INVALID_HANDLE_VALUE
1374 static HCRYPTPROV read_key_container(PCHAR pszContainerName, DWORD dwFlags, const VTableProvStruc *pVTable)
1376 HKEY hKey;
1377 KEYCONTAINER *pKeyContainer;
1378 HCRYPTPROV hKeyContainer;
1379 HCRYPTKEY hCryptKey;
1381 if (!open_container_key(pszContainerName, dwFlags, &hKey))
1383 SetLastError(NTE_BAD_KEYSET);
1384 return (HCRYPTPROV)INVALID_HANDLE_VALUE;
1387 hKeyContainer = new_key_container(pszContainerName, dwFlags, pVTable);
1388 if (hKeyContainer != (HCRYPTPROV)INVALID_HANDLE_VALUE)
1390 DWORD dwProtectFlags = (dwFlags & CRYPT_MACHINE_KEYSET) ?
1391 CRYPTPROTECT_LOCAL_MACHINE : 0;
1393 if (!lookup_handle(&handle_table, hKeyContainer, RSAENH_MAGIC_CONTAINER,
1394 (OBJECTHDR**)&pKeyContainer))
1395 return (HCRYPTPROV)INVALID_HANDLE_VALUE;
1397 /* read_key_value calls import_key, which calls import_private_key,
1398 * which implicitly installs the key value into the appropriate key
1399 * container key. Thus the ref count is incremented twice, once for
1400 * the output key value, and once for the implicit install, and needs
1401 * to be decremented to balance the two.
1403 if (read_key_value(hKeyContainer, hKey, AT_KEYEXCHANGE,
1404 dwProtectFlags, &hCryptKey))
1405 release_handle(&handle_table, hCryptKey, RSAENH_MAGIC_KEY);
1406 if (read_key_value(hKeyContainer, hKey, AT_SIGNATURE,
1407 dwProtectFlags, &hCryptKey))
1408 release_handle(&handle_table, hCryptKey, RSAENH_MAGIC_KEY);
1411 return hKeyContainer;
1414 /******************************************************************************
1415 * build_hash_signature [Internal]
1417 * Builds a padded version of a hash to match the length of the RSA key modulus.
1419 * PARAMS
1420 * pbSignature [O] The padded hash object is stored here.
1421 * dwLen [I] Length of the pbSignature buffer.
1422 * aiAlgid [I] Algorithm identifier of the hash to be padded.
1423 * abHashValue [I] The value of the hash object.
1424 * dwHashLen [I] Length of the hash value.
1425 * dwFlags [I] Selection of padding algorithm.
1427 * RETURNS
1428 * Success: TRUE
1429 * Failure: FALSE (NTE_BAD_ALGID)
1431 static BOOL build_hash_signature(BYTE *pbSignature, DWORD dwLen, ALG_ID aiAlgid,
1432 CONST BYTE *abHashValue, DWORD dwHashLen, DWORD dwFlags)
1434 /* These prefixes are meant to be concatenated with hash values of the
1435 * respective kind to form a PKCS #7 DigestInfo. */
1436 static const struct tagOIDDescriptor {
1437 ALG_ID aiAlgid;
1438 DWORD dwLen;
1439 CONST BYTE abOID[19];
1440 } aOIDDescriptor[] = {
1441 { CALG_MD2, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1442 0x86, 0xf7, 0x0d, 0x02, 0x02, 0x05, 0x00, 0x04, 0x10 } },
1443 { CALG_MD4, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1444 0x86, 0xf7, 0x0d, 0x02, 0x04, 0x05, 0x00, 0x04, 0x10 } },
1445 { CALG_MD5, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1446 0x86, 0xf7, 0x0d, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10 } },
1447 { CALG_SHA, 15, { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03,
1448 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14 } },
1449 { CALG_SHA_256, 19, { 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
1450 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01,
1451 0x05, 0x00, 0x04, 0x20 } },
1452 { CALG_SHA_384, 19, { 0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
1453 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01,
1454 0x05, 0x00, 0x04, 0x30 } },
1455 { CALG_SHA_384, 19, { 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
1456 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01,
1457 0x05, 0x00, 0x04, 0x40 } },
1458 { CALG_SSL3_SHAMD5, 0, { 0 } },
1459 { 0, 0, { 0 } }
1461 DWORD dwIdxOID, i, j;
1463 for (dwIdxOID = 0; aOIDDescriptor[dwIdxOID].aiAlgid; dwIdxOID++) {
1464 if (aOIDDescriptor[dwIdxOID].aiAlgid == aiAlgid) break;
1467 if (!aOIDDescriptor[dwIdxOID].aiAlgid) {
1468 SetLastError(NTE_BAD_ALGID);
1469 return FALSE;
1472 /* Build the padded signature */
1473 if (dwFlags & CRYPT_X931_FORMAT) {
1474 pbSignature[0] = 0x6b;
1475 for (i=1; i < dwLen - dwHashLen - 3; i++) {
1476 pbSignature[i] = 0xbb;
1478 pbSignature[i++] = 0xba;
1479 for (j=0; j < dwHashLen; j++, i++) {
1480 pbSignature[i] = abHashValue[j];
1482 pbSignature[i++] = 0x33;
1483 pbSignature[i++] = 0xcc;
1484 } else {
1485 pbSignature[0] = 0x00;
1486 pbSignature[1] = 0x01;
1487 if (dwFlags & CRYPT_NOHASHOID) {
1488 for (i=2; i < dwLen - 1 - dwHashLen; i++) {
1489 pbSignature[i] = 0xff;
1491 pbSignature[i++] = 0x00;
1492 } else {
1493 for (i=2; i < dwLen - 1 - aOIDDescriptor[dwIdxOID].dwLen - dwHashLen; i++) {
1494 pbSignature[i] = 0xff;
1496 pbSignature[i++] = 0x00;
1497 for (j=0; j < aOIDDescriptor[dwIdxOID].dwLen; j++) {
1498 pbSignature[i++] = aOIDDescriptor[dwIdxOID].abOID[j];
1501 for (j=0; j < dwHashLen; j++) {
1502 pbSignature[i++] = abHashValue[j];
1506 return TRUE;
1509 /******************************************************************************
1510 * tls1_p [Internal]
1512 * This is an implementation of the 'P_hash' helper function for TLS1's PRF.
1513 * It is used exclusively by tls1_prf. For details see RFC 2246, chapter 5.
1514 * The pseudo random stream generated by this function is exclusive or'ed with
1515 * the data in pbBuffer.
1517 * PARAMS
1518 * hHMAC [I] HMAC object, which will be used in pseudo random generation
1519 * pblobSeed [I] Seed value
1520 * pbBuffer [I/O] Pseudo random stream will be xor'ed to the provided data
1521 * dwBufferLen [I] Number of pseudo random bytes desired
1523 * RETURNS
1524 * Success: TRUE
1525 * Failure: FALSE
1527 static BOOL tls1_p(HCRYPTHASH hHMAC, CONST PCRYPT_DATA_BLOB pblobSeed, PBYTE pbBuffer, DWORD dwBufferLen)
1529 CRYPTHASH *pHMAC;
1530 BYTE abAi[RSAENH_MAX_HASH_SIZE];
1531 DWORD i = 0;
1533 if (!lookup_handle(&handle_table, hHMAC, RSAENH_MAGIC_HASH, (OBJECTHDR**)&pHMAC)) {
1534 SetLastError(NTE_BAD_HASH);
1535 return FALSE;
1538 /* compute A_1 = HMAC(seed) */
1539 init_hash(pHMAC);
1540 update_hash(pHMAC, pblobSeed->pbData, pblobSeed->cbData);
1541 finalize_hash(pHMAC);
1542 memcpy(abAi, pHMAC->abHashValue, pHMAC->dwHashSize);
1544 do {
1545 /* compute HMAC(A_i + seed) */
1546 init_hash(pHMAC);
1547 update_hash(pHMAC, abAi, pHMAC->dwHashSize);
1548 update_hash(pHMAC, pblobSeed->pbData, pblobSeed->cbData);
1549 finalize_hash(pHMAC);
1551 /* pseudo random stream := CONCAT_{i=1..n} ( HMAC(A_i + seed) ) */
1552 do {
1553 if (i >= dwBufferLen) break;
1554 pbBuffer[i] ^= pHMAC->abHashValue[i % pHMAC->dwHashSize];
1555 i++;
1556 } while (i % pHMAC->dwHashSize);
1558 /* compute A_{i+1} = HMAC(A_i) */
1559 init_hash(pHMAC);
1560 update_hash(pHMAC, abAi, pHMAC->dwHashSize);
1561 finalize_hash(pHMAC);
1562 memcpy(abAi, pHMAC->abHashValue, pHMAC->dwHashSize);
1563 } while (i < dwBufferLen);
1565 return TRUE;
1568 /******************************************************************************
1569 * tls1_prf [Internal]
1571 * TLS1 pseudo random function as specified in RFC 2246, chapter 5
1573 * PARAMS
1574 * hProv [I] Key container used to compute the pseudo random stream
1575 * hSecret [I] Key that holds the (pre-)master secret
1576 * pblobLabel [I] Descriptive label
1577 * pblobSeed [I] Seed value
1578 * pbBuffer [O] Pseudo random numbers will be stored here
1579 * dwBufferLen [I] Number of pseudo random bytes desired
1581 * RETURNS
1582 * Success: TRUE
1583 * Failure: FALSE
1585 static BOOL tls1_prf(HCRYPTPROV hProv, HCRYPTPROV hSecret, CONST PCRYPT_DATA_BLOB pblobLabel,
1586 CONST PCRYPT_DATA_BLOB pblobSeed, PBYTE pbBuffer, DWORD dwBufferLen)
1588 HMAC_INFO hmacInfo = { 0, NULL, 0, NULL, 0 };
1589 HCRYPTHASH hHMAC = (HCRYPTHASH)INVALID_HANDLE_VALUE;
1590 HCRYPTKEY hHalfSecret = (HCRYPTKEY)INVALID_HANDLE_VALUE;
1591 CRYPTKEY *pHalfSecret, *pSecret;
1592 DWORD dwHalfSecretLen;
1593 BOOL result = FALSE;
1594 CRYPT_DATA_BLOB blobLabelSeed;
1596 TRACE("(hProv=%08lx, hSecret=%08lx, pblobLabel=%p, pblobSeed=%p, pbBuffer=%p, dwBufferLen=%d)\n",
1597 hProv, hSecret, pblobLabel, pblobSeed, pbBuffer, dwBufferLen);
1599 if (!lookup_handle(&handle_table, hSecret, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pSecret)) {
1600 SetLastError(NTE_FAIL);
1601 return FALSE;
1604 dwHalfSecretLen = (pSecret->dwKeyLen+1)/2;
1606 /* concatenation of the label and the seed */
1607 if (!concat_data_blobs(&blobLabelSeed, pblobLabel, pblobSeed)) goto exit;
1609 /* zero out the buffer, since two random streams will be xor'ed into it. */
1610 memset(pbBuffer, 0, dwBufferLen);
1612 /* build a 'fake' key, to hold the secret. CALG_SSL2_MASTER is used since it provides
1613 * the biggest range of valid key lengths. */
1614 hHalfSecret = new_key(hProv, CALG_SSL2_MASTER, MAKELONG(0,dwHalfSecretLen*8), &pHalfSecret);
1615 if (hHalfSecret == (HCRYPTKEY)INVALID_HANDLE_VALUE) goto exit;
1617 /* Derive an HMAC_MD5 hash and call the helper function. */
1618 memcpy(pHalfSecret->abKeyValue, pSecret->abKeyValue, dwHalfSecretLen);
1619 if (!RSAENH_CPCreateHash(hProv, CALG_HMAC, hHalfSecret, 0, &hHMAC)) goto exit;
1620 hmacInfo.HashAlgid = CALG_MD5;
1621 if (!RSAENH_CPSetHashParam(hProv, hHMAC, HP_HMAC_INFO, (BYTE*)&hmacInfo, 0)) goto exit;
1622 if (!tls1_p(hHMAC, &blobLabelSeed, pbBuffer, dwBufferLen)) goto exit;
1624 /* Reconfigure to HMAC_SHA hash and call helper function again. */
1625 memcpy(pHalfSecret->abKeyValue, pSecret->abKeyValue + (pSecret->dwKeyLen/2), dwHalfSecretLen);
1626 hmacInfo.HashAlgid = CALG_SHA;
1627 if (!RSAENH_CPSetHashParam(hProv, hHMAC, HP_HMAC_INFO, (BYTE*)&hmacInfo, 0)) goto exit;
1628 if (!tls1_p(hHMAC, &blobLabelSeed, pbBuffer, dwBufferLen)) goto exit;
1630 result = TRUE;
1631 exit:
1632 release_handle(&handle_table, hHalfSecret, RSAENH_MAGIC_KEY);
1633 if (hHMAC != (HCRYPTHASH)INVALID_HANDLE_VALUE) RSAENH_CPDestroyHash(hProv, hHMAC);
1634 free_data_blob(&blobLabelSeed);
1635 return result;
1638 /******************************************************************************
1639 * pad_data [Internal]
1641 * Helper function for data padding according to PKCS1 #2
1643 * PARAMS
1644 * abData [I] The data to be padded
1645 * dwDataLen [I] Length of the data
1646 * abBuffer [O] Padded data will be stored here
1647 * dwBufferLen [I] Length of the buffer (also length of padded data)
1648 * dwFlags [I] Padding format (CRYPT_SSL2_FALLBACK)
1650 * RETURN
1651 * Success: TRUE
1652 * Failure: FALSE (NTE_BAD_LEN, too much data to pad)
1654 static BOOL pad_data(CONST BYTE *abData, DWORD dwDataLen, BYTE *abBuffer, DWORD dwBufferLen,
1655 DWORD dwFlags)
1657 DWORD i;
1659 /* Ensure there is enough space for PKCS1 #2 padding */
1660 if (dwDataLen > dwBufferLen-11) {
1661 SetLastError(NTE_BAD_LEN);
1662 return FALSE;
1665 memmove(abBuffer + dwBufferLen - dwDataLen, abData, dwDataLen);
1667 abBuffer[0] = 0x00;
1668 abBuffer[1] = RSAENH_PKC_BLOCKTYPE;
1669 for (i=2; i < dwBufferLen - dwDataLen - 1; i++)
1670 do gen_rand_impl(&abBuffer[i], 1); while (!abBuffer[i]);
1671 if (dwFlags & CRYPT_SSL2_FALLBACK)
1672 for (i-=8; i < dwBufferLen - dwDataLen - 1; i++)
1673 abBuffer[i] = 0x03;
1674 abBuffer[i] = 0x00;
1676 return TRUE;
1679 /******************************************************************************
1680 * unpad_data [Internal]
1682 * Remove the PKCS1 padding from RSA decrypted data
1684 * PARAMS
1685 * abData [I] The padded data
1686 * dwDataLen [I] Length of the padded data
1687 * abBuffer [O] Data without padding will be stored here
1688 * dwBufferLen [I/O] I: Length of the buffer, O: Length of unpadded data
1689 * dwFlags [I] Currently none defined
1691 * RETURNS
1692 * Success: TRUE
1693 * Failure: FALSE, (NTE_BAD_DATA, no valid PKCS1 padding or buffer too small)
1695 static BOOL unpad_data(CONST BYTE *abData, DWORD dwDataLen, BYTE *abBuffer, DWORD *dwBufferLen,
1696 DWORD dwFlags)
1698 DWORD i;
1700 for (i=2; i<dwDataLen; i++)
1701 if (!abData[i])
1702 break;
1704 if ((i == dwDataLen) || (*dwBufferLen < dwDataLen - i - 1) ||
1705 (abData[0] != 0x00) || (abData[1] != RSAENH_PKC_BLOCKTYPE))
1707 SetLastError(NTE_BAD_DATA);
1708 return FALSE;
1711 *dwBufferLen = dwDataLen - i - 1;
1712 memmove(abBuffer, abData + i + 1, *dwBufferLen);
1713 return TRUE;
1716 /******************************************************************************
1717 * CPAcquireContext (RSAENH.@)
1719 * Acquire a handle to the key container specified by pszContainer
1721 * PARAMS
1722 * phProv [O] Pointer to the location the acquired handle will be written to.
1723 * pszContainer [I] Name of the desired key container. See Notes
1724 * dwFlags [I] Flags. See Notes.
1725 * pVTable [I] Pointer to a PVTableProvStruct containing callbacks.
1727 * RETURNS
1728 * Success: TRUE
1729 * Failure: FALSE
1731 * NOTES
1732 * If pszContainer is NULL or points to a zero length string the user's login
1733 * name will be used as the key container name.
1735 * If the CRYPT_NEW_KEYSET flag is set in dwFlags a new keyset will be created.
1736 * If a keyset with the given name already exists, the function fails and sets
1737 * last error to NTE_EXISTS. If CRYPT_NEW_KEYSET is not set and the specified
1738 * key container does not exist, function fails and sets last error to
1739 * NTE_BAD_KEYSET.
1741 BOOL WINAPI RSAENH_CPAcquireContext(HCRYPTPROV *phProv, LPSTR pszContainer,
1742 DWORD dwFlags, PVTableProvStruc pVTable)
1744 CHAR szKeyContainerName[MAX_PATH];
1746 TRACE("(phProv=%p, pszContainer=%s, dwFlags=%08x, pVTable=%p)\n", phProv,
1747 debugstr_a(pszContainer), dwFlags, pVTable);
1749 if (pszContainer && *pszContainer)
1751 lstrcpynA(szKeyContainerName, pszContainer, MAX_PATH);
1753 else
1755 DWORD dwLen = sizeof(szKeyContainerName);
1756 if (!GetUserNameA(szKeyContainerName, &dwLen)) return FALSE;
1759 switch (dwFlags & (CRYPT_NEWKEYSET|CRYPT_VERIFYCONTEXT|CRYPT_DELETEKEYSET))
1761 case 0:
1762 *phProv = read_key_container(szKeyContainerName, dwFlags, pVTable);
1763 break;
1765 case CRYPT_DELETEKEYSET:
1766 return delete_container_key(szKeyContainerName, dwFlags);
1768 case CRYPT_NEWKEYSET:
1769 *phProv = read_key_container(szKeyContainerName, dwFlags, pVTable);
1770 if (*phProv != (HCRYPTPROV)INVALID_HANDLE_VALUE)
1772 release_handle(&handle_table, *phProv, RSAENH_MAGIC_CONTAINER);
1773 TRACE("Can't create new keyset, already exists\n");
1774 SetLastError(NTE_EXISTS);
1775 return FALSE;
1777 *phProv = new_key_container(szKeyContainerName, dwFlags, pVTable);
1778 break;
1780 case CRYPT_VERIFYCONTEXT|CRYPT_NEWKEYSET:
1781 case CRYPT_VERIFYCONTEXT:
1782 if (pszContainer && *pszContainer) {
1783 TRACE("pszContainer should be empty\n");
1784 SetLastError(NTE_BAD_FLAGS);
1785 return FALSE;
1787 *phProv = new_key_container("", dwFlags, pVTable);
1788 break;
1790 default:
1791 *phProv = (HCRYPTPROV)INVALID_HANDLE_VALUE;
1792 SetLastError(NTE_BAD_FLAGS);
1793 return FALSE;
1796 if (*phProv != (HCRYPTPROV)INVALID_HANDLE_VALUE) {
1797 SetLastError(ERROR_SUCCESS);
1798 return TRUE;
1799 } else {
1800 return FALSE;
1804 /******************************************************************************
1805 * CPCreateHash (RSAENH.@)
1807 * CPCreateHash creates and initializes a new hash object.
1809 * PARAMS
1810 * hProv [I] Handle to the key container to which the new hash will belong.
1811 * Algid [I] Identifies the hash algorithm, which will be used for the hash.
1812 * hKey [I] Handle to a session key applied for keyed hashes.
1813 * dwFlags [I] Currently no flags defined. Must be zero.
1814 * phHash [O] Points to the location where a handle to the new hash will be stored.
1816 * RETURNS
1817 * Success: TRUE
1818 * Failure: FALSE
1820 * NOTES
1821 * hKey is a handle to a session key applied in keyed hashes like MAC and HMAC.
1822 * If a normal hash object is to be created (like e.g. MD2 or SHA1) hKey must be zero.
1824 BOOL WINAPI RSAENH_CPCreateHash(HCRYPTPROV hProv, ALG_ID Algid, HCRYPTKEY hKey, DWORD dwFlags,
1825 HCRYPTHASH *phHash)
1827 CRYPTKEY *pCryptKey;
1828 CRYPTHASH *pCryptHash;
1829 const PROV_ENUMALGS_EX *peaAlgidInfo;
1831 TRACE("(hProv=%08lx, Algid=%08x, hKey=%08lx, dwFlags=%08x, phHash=%p)\n", hProv, Algid, hKey,
1832 dwFlags, phHash);
1834 peaAlgidInfo = get_algid_info(hProv, Algid);
1835 if (!peaAlgidInfo) return FALSE;
1837 if (dwFlags)
1839 SetLastError(NTE_BAD_FLAGS);
1840 return FALSE;
1843 if (Algid == CALG_MAC || Algid == CALG_HMAC || Algid == CALG_SCHANNEL_MASTER_HASH ||
1844 Algid == CALG_TLS1PRF)
1846 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey)) {
1847 SetLastError(NTE_BAD_KEY);
1848 return FALSE;
1851 if ((Algid == CALG_MAC) && (GET_ALG_TYPE(pCryptKey->aiAlgid) != ALG_TYPE_BLOCK)) {
1852 SetLastError(NTE_BAD_KEY);
1853 return FALSE;
1856 if ((Algid == CALG_SCHANNEL_MASTER_HASH || Algid == CALG_TLS1PRF) &&
1857 (pCryptKey->aiAlgid != CALG_TLS1_MASTER))
1859 SetLastError(NTE_BAD_KEY);
1860 return FALSE;
1862 if (Algid == CALG_SCHANNEL_MASTER_HASH &&
1863 ((!pCryptKey->siSChannelInfo.blobClientRandom.cbData) ||
1864 (!pCryptKey->siSChannelInfo.blobServerRandom.cbData)))
1866 SetLastError(ERROR_INVALID_PARAMETER);
1867 return FALSE;
1870 if ((Algid == CALG_TLS1PRF) && (pCryptKey->dwState != RSAENH_KEYSTATE_MASTERKEY)) {
1871 SetLastError(NTE_BAD_KEY_STATE);
1872 return FALSE;
1876 *phHash = new_object(&handle_table, sizeof(CRYPTHASH), RSAENH_MAGIC_HASH,
1877 destroy_hash, (OBJECTHDR**)&pCryptHash);
1878 if (!pCryptHash) return FALSE;
1880 pCryptHash->aiAlgid = Algid;
1881 pCryptHash->hKey = hKey;
1882 pCryptHash->hProv = hProv;
1883 pCryptHash->dwState = RSAENH_HASHSTATE_HASHING;
1884 pCryptHash->pHMACInfo = NULL;
1885 pCryptHash->dwHashSize = peaAlgidInfo->dwDefaultLen >> 3;
1886 init_data_blob(&pCryptHash->tpPRFParams.blobLabel);
1887 init_data_blob(&pCryptHash->tpPRFParams.blobSeed);
1889 if (Algid == CALG_SCHANNEL_MASTER_HASH) {
1890 static const char keyex[] = "key expansion";
1891 BYTE key_expansion[sizeof keyex];
1892 CRYPT_DATA_BLOB blobRandom, blobKeyExpansion = { 13, key_expansion };
1894 memcpy( key_expansion, keyex, sizeof keyex );
1896 if (pCryptKey->dwState != RSAENH_KEYSTATE_MASTERKEY) {
1897 static const char msec[] = "master secret";
1898 BYTE master_secret[sizeof msec];
1899 CRYPT_DATA_BLOB blobLabel = { 13, master_secret };
1900 BYTE abKeyValue[48];
1902 memcpy( master_secret, msec, sizeof msec );
1904 /* See RFC 2246, chapter 8.1 */
1905 if (!concat_data_blobs(&blobRandom,
1906 &pCryptKey->siSChannelInfo.blobClientRandom,
1907 &pCryptKey->siSChannelInfo.blobServerRandom))
1909 return FALSE;
1911 tls1_prf(hProv, hKey, &blobLabel, &blobRandom, abKeyValue, 48);
1912 pCryptKey->dwState = RSAENH_KEYSTATE_MASTERKEY;
1913 memcpy(pCryptKey->abKeyValue, abKeyValue, 48);
1914 free_data_blob(&blobRandom);
1917 /* See RFC 2246, chapter 6.3 */
1918 if (!concat_data_blobs(&blobRandom,
1919 &pCryptKey->siSChannelInfo.blobServerRandom,
1920 &pCryptKey->siSChannelInfo.blobClientRandom))
1922 return FALSE;
1924 tls1_prf(hProv, hKey, &blobKeyExpansion, &blobRandom, pCryptHash->abHashValue,
1925 RSAENH_MAX_HASH_SIZE);
1926 free_data_blob(&blobRandom);
1929 return init_hash(pCryptHash);
1932 /******************************************************************************
1933 * CPDestroyHash (RSAENH.@)
1935 * Releases the handle to a hash object. The object is destroyed if its reference
1936 * count reaches zero.
1938 * PARAMS
1939 * hProv [I] Handle to the key container to which the hash object belongs.
1940 * hHash [I] Handle to the hash object to be released.
1942 * RETURNS
1943 * Success: TRUE
1944 * Failure: FALSE
1946 BOOL WINAPI RSAENH_CPDestroyHash(HCRYPTPROV hProv, HCRYPTHASH hHash)
1948 TRACE("(hProv=%08lx, hHash=%08lx)\n", hProv, hHash);
1950 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
1952 SetLastError(NTE_BAD_UID);
1953 return FALSE;
1956 if (!release_handle(&handle_table, hHash, RSAENH_MAGIC_HASH))
1958 SetLastError(NTE_BAD_HASH);
1959 return FALSE;
1962 return TRUE;
1965 /******************************************************************************
1966 * CPDestroyKey (RSAENH.@)
1968 * Releases the handle to a key object. The object is destroyed if its reference
1969 * count reaches zero.
1971 * PARAMS
1972 * hProv [I] Handle to the key container to which the key object belongs.
1973 * hKey [I] Handle to the key object to be released.
1975 * RETURNS
1976 * Success: TRUE
1977 * Failure: FALSE
1979 BOOL WINAPI RSAENH_CPDestroyKey(HCRYPTPROV hProv, HCRYPTKEY hKey)
1981 TRACE("(hProv=%08lx, hKey=%08lx)\n", hProv, hKey);
1983 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
1985 SetLastError(NTE_BAD_UID);
1986 return FALSE;
1989 if (!release_handle(&handle_table, hKey, RSAENH_MAGIC_KEY))
1991 SetLastError(NTE_BAD_KEY);
1992 return FALSE;
1995 return TRUE;
1998 /******************************************************************************
1999 * CPDuplicateHash (RSAENH.@)
2001 * Clones a hash object including its current state.
2003 * PARAMS
2004 * hUID [I] Handle to the key container the hash belongs to.
2005 * hHash [I] Handle to the hash object to be cloned.
2006 * pdwReserved [I] Reserved. Must be NULL.
2007 * dwFlags [I] No flags are currently defined. Must be 0.
2008 * phHash [O] Handle to the cloned hash object.
2010 * RETURNS
2011 * Success: TRUE.
2012 * Failure: FALSE.
2014 BOOL WINAPI RSAENH_CPDuplicateHash(HCRYPTPROV hUID, HCRYPTHASH hHash, DWORD *pdwReserved,
2015 DWORD dwFlags, HCRYPTHASH *phHash)
2017 CRYPTHASH *pSrcHash, *pDestHash;
2019 TRACE("(hUID=%08lx, hHash=%08lx, pdwReserved=%p, dwFlags=%08x, phHash=%p)\n", hUID, hHash,
2020 pdwReserved, dwFlags, phHash);
2022 if (!is_valid_handle(&handle_table, hUID, RSAENH_MAGIC_CONTAINER))
2024 SetLastError(NTE_BAD_UID);
2025 return FALSE;
2028 if (!lookup_handle(&handle_table, hHash, RSAENH_MAGIC_HASH, (OBJECTHDR**)&pSrcHash))
2030 SetLastError(NTE_BAD_HASH);
2031 return FALSE;
2034 if (!phHash || pdwReserved || dwFlags)
2036 SetLastError(ERROR_INVALID_PARAMETER);
2037 return FALSE;
2040 *phHash = new_object(&handle_table, sizeof(CRYPTHASH), RSAENH_MAGIC_HASH,
2041 destroy_hash, (OBJECTHDR**)&pDestHash);
2042 if (*phHash != (HCRYPTHASH)INVALID_HANDLE_VALUE)
2044 *pDestHash = *pSrcHash;
2045 duplicate_hash_impl(pSrcHash->aiAlgid, &pSrcHash->context, &pDestHash->context);
2046 copy_hmac_info(&pDestHash->pHMACInfo, pSrcHash->pHMACInfo);
2047 copy_data_blob(&pDestHash->tpPRFParams.blobLabel, &pSrcHash->tpPRFParams.blobLabel);
2048 copy_data_blob(&pDestHash->tpPRFParams.blobSeed, &pSrcHash->tpPRFParams.blobSeed);
2051 return *phHash != (HCRYPTHASH)INVALID_HANDLE_VALUE;
2054 /******************************************************************************
2055 * CPDuplicateKey (RSAENH.@)
2057 * Clones a key object including its current state.
2059 * PARAMS
2060 * hUID [I] Handle to the key container the hash belongs to.
2061 * hKey [I] Handle to the key object to be cloned.
2062 * pdwReserved [I] Reserved. Must be NULL.
2063 * dwFlags [I] No flags are currently defined. Must be 0.
2064 * phHash [O] Handle to the cloned key object.
2066 * RETURNS
2067 * Success: TRUE.
2068 * Failure: FALSE.
2070 BOOL WINAPI RSAENH_CPDuplicateKey(HCRYPTPROV hUID, HCRYPTKEY hKey, DWORD *pdwReserved,
2071 DWORD dwFlags, HCRYPTKEY *phKey)
2073 CRYPTKEY *pSrcKey, *pDestKey;
2075 TRACE("(hUID=%08lx, hKey=%08lx, pdwReserved=%p, dwFlags=%08x, phKey=%p)\n", hUID, hKey,
2076 pdwReserved, dwFlags, phKey);
2078 if (!is_valid_handle(&handle_table, hUID, RSAENH_MAGIC_CONTAINER))
2080 SetLastError(NTE_BAD_UID);
2081 return FALSE;
2084 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pSrcKey))
2086 SetLastError(NTE_BAD_KEY);
2087 return FALSE;
2090 if (!phKey || pdwReserved || dwFlags)
2092 SetLastError(ERROR_INVALID_PARAMETER);
2093 return FALSE;
2096 *phKey = new_object(&handle_table, sizeof(CRYPTKEY), RSAENH_MAGIC_KEY, destroy_key,
2097 (OBJECTHDR**)&pDestKey);
2098 if (*phKey != (HCRYPTKEY)INVALID_HANDLE_VALUE)
2100 *pDestKey = *pSrcKey;
2101 copy_data_blob(&pDestKey->siSChannelInfo.blobServerRandom,
2102 &pSrcKey->siSChannelInfo.blobServerRandom);
2103 copy_data_blob(&pDestKey->siSChannelInfo.blobClientRandom,
2104 &pSrcKey->siSChannelInfo.blobClientRandom);
2105 duplicate_key_impl(pSrcKey->aiAlgid, &pSrcKey->context, &pDestKey->context);
2106 return TRUE;
2108 else
2110 return FALSE;
2114 /******************************************************************************
2115 * CPEncrypt (RSAENH.@)
2117 * Encrypt data.
2119 * PARAMS
2120 * hProv [I] The key container hKey and hHash belong to.
2121 * hKey [I] The key used to encrypt the data.
2122 * hHash [I] An optional hash object for parallel hashing. See notes.
2123 * Final [I] Indicates if this is the last block of data to encrypt.
2124 * dwFlags [I] Currently no flags defined. Must be zero.
2125 * pbData [I/O] Pointer to the data to encrypt. Encrypted data will also be stored there.
2126 * pdwDataLen [I/O] I: Length of data to encrypt, O: Length of encrypted data.
2127 * dwBufLen [I] Size of the buffer at pbData.
2129 * RETURNS
2130 * Success: TRUE.
2131 * Failure: FALSE.
2133 * NOTES
2134 * If a hash object handle is provided in hHash, it will be updated with the plaintext.
2135 * This is useful for message signatures.
2137 * This function uses the standard WINAPI protocol for querying data of dynamic length.
2139 BOOL WINAPI RSAENH_CPEncrypt(HCRYPTPROV hProv, HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final,
2140 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen, DWORD dwBufLen)
2142 CRYPTKEY *pCryptKey;
2143 BYTE *in, out[RSAENH_MAX_BLOCK_SIZE], o[RSAENH_MAX_BLOCK_SIZE];
2144 DWORD dwEncryptedLen, i, j, k;
2146 TRACE("(hProv=%08lx, hKey=%08lx, hHash=%08lx, Final=%d, dwFlags=%08x, pbData=%p, "
2147 "pdwDataLen=%p, dwBufLen=%d)\n", hProv, hKey, hHash, Final, dwFlags, pbData, pdwDataLen,
2148 dwBufLen);
2150 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
2152 SetLastError(NTE_BAD_UID);
2153 return FALSE;
2156 if (dwFlags)
2158 SetLastError(NTE_BAD_FLAGS);
2159 return FALSE;
2162 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
2164 SetLastError(NTE_BAD_KEY);
2165 return FALSE;
2168 if (pCryptKey->dwState == RSAENH_KEYSTATE_IDLE)
2169 pCryptKey->dwState = RSAENH_KEYSTATE_ENCRYPTING;
2171 if (pCryptKey->dwState != RSAENH_KEYSTATE_ENCRYPTING)
2173 SetLastError(NTE_BAD_DATA);
2174 return FALSE;
2177 if (is_valid_handle(&handle_table, hHash, RSAENH_MAGIC_HASH)) {
2178 if (!RSAENH_CPHashData(hProv, hHash, pbData, *pdwDataLen, 0)) return FALSE;
2181 if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_BLOCK) {
2182 if (!Final && (*pdwDataLen % pCryptKey->dwBlockLen)) {
2183 SetLastError(NTE_BAD_DATA);
2184 return FALSE;
2187 dwEncryptedLen = (*pdwDataLen/pCryptKey->dwBlockLen+(Final?1:0))*pCryptKey->dwBlockLen;
2189 if (pbData == NULL) {
2190 *pdwDataLen = dwEncryptedLen;
2191 return TRUE;
2193 else if (dwEncryptedLen > dwBufLen) {
2194 *pdwDataLen = dwEncryptedLen;
2195 SetLastError(ERROR_MORE_DATA);
2196 return FALSE;
2199 /* Pad final block with length bytes */
2200 for (i=*pdwDataLen; i<dwEncryptedLen; i++) pbData[i] = dwEncryptedLen - *pdwDataLen;
2201 *pdwDataLen = dwEncryptedLen;
2203 for (i=0, in=pbData; i<*pdwDataLen; i+=pCryptKey->dwBlockLen, in+=pCryptKey->dwBlockLen) {
2204 switch (pCryptKey->dwMode) {
2205 case CRYPT_MODE_ECB:
2206 encrypt_block_impl(pCryptKey->aiAlgid, 0, &pCryptKey->context, in, out,
2207 RSAENH_ENCRYPT);
2208 break;
2210 case CRYPT_MODE_CBC:
2211 for (j=0; j<pCryptKey->dwBlockLen; j++) in[j] ^= pCryptKey->abChainVector[j];
2212 encrypt_block_impl(pCryptKey->aiAlgid, 0, &pCryptKey->context, in, out,
2213 RSAENH_ENCRYPT);
2214 memcpy(pCryptKey->abChainVector, out, pCryptKey->dwBlockLen);
2215 break;
2217 case CRYPT_MODE_CFB:
2218 for (j=0; j<pCryptKey->dwBlockLen; j++) {
2219 encrypt_block_impl(pCryptKey->aiAlgid, 0, &pCryptKey->context,
2220 pCryptKey->abChainVector, o, RSAENH_ENCRYPT);
2221 out[j] = in[j] ^ o[0];
2222 for (k=0; k<pCryptKey->dwBlockLen-1; k++)
2223 pCryptKey->abChainVector[k] = pCryptKey->abChainVector[k+1];
2224 pCryptKey->abChainVector[k] = out[j];
2226 break;
2228 default:
2229 SetLastError(NTE_BAD_ALGID);
2230 return FALSE;
2232 memcpy(in, out, pCryptKey->dwBlockLen);
2234 } else if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_STREAM) {
2235 if (pbData == NULL) {
2236 *pdwDataLen = dwBufLen;
2237 return TRUE;
2239 encrypt_stream_impl(pCryptKey->aiAlgid, &pCryptKey->context, pbData, *pdwDataLen);
2240 } else if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_RSA) {
2241 if (pCryptKey->aiAlgid == CALG_RSA_SIGN) {
2242 SetLastError(NTE_BAD_KEY);
2243 return FALSE;
2245 if (!pbData) {
2246 *pdwDataLen = pCryptKey->dwBlockLen;
2247 return TRUE;
2249 if (dwBufLen < pCryptKey->dwBlockLen) {
2250 SetLastError(ERROR_MORE_DATA);
2251 return FALSE;
2253 if (!pad_data(pbData, *pdwDataLen, pbData, pCryptKey->dwBlockLen, dwFlags)) return FALSE;
2254 encrypt_block_impl(pCryptKey->aiAlgid, PK_PUBLIC, &pCryptKey->context, pbData, pbData, RSAENH_ENCRYPT);
2255 *pdwDataLen = pCryptKey->dwBlockLen;
2256 Final = TRUE;
2257 } else {
2258 SetLastError(NTE_BAD_TYPE);
2259 return FALSE;
2262 if (Final) setup_key(pCryptKey);
2264 return TRUE;
2267 /******************************************************************************
2268 * CPDecrypt (RSAENH.@)
2270 * Decrypt data.
2272 * PARAMS
2273 * hProv [I] The key container hKey and hHash belong to.
2274 * hKey [I] The key used to decrypt the data.
2275 * hHash [I] An optional hash object for parallel hashing. See notes.
2276 * Final [I] Indicates if this is the last block of data to decrypt.
2277 * dwFlags [I] Currently no flags defined. Must be zero.
2278 * pbData [I/O] Pointer to the data to decrypt. Plaintext will also be stored there.
2279 * pdwDataLen [I/O] I: Length of ciphertext, O: Length of plaintext.
2281 * RETURNS
2282 * Success: TRUE.
2283 * Failure: FALSE.
2285 * NOTES
2286 * If a hash object handle is provided in hHash, it will be updated with the plaintext.
2287 * This is useful for message signatures.
2289 * This function uses the standard WINAPI protocol for querying data of dynamic length.
2291 BOOL WINAPI RSAENH_CPDecrypt(HCRYPTPROV hProv, HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final,
2292 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
2294 CRYPTKEY *pCryptKey;
2295 BYTE *in, out[RSAENH_MAX_BLOCK_SIZE], o[RSAENH_MAX_BLOCK_SIZE];
2296 DWORD i, j, k;
2297 DWORD dwMax;
2299 TRACE("(hProv=%08lx, hKey=%08lx, hHash=%08lx, Final=%d, dwFlags=%08x, pbData=%p, "
2300 "pdwDataLen=%p)\n", hProv, hKey, hHash, Final, dwFlags, pbData, pdwDataLen);
2302 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
2304 SetLastError(NTE_BAD_UID);
2305 return FALSE;
2308 if (dwFlags)
2310 SetLastError(NTE_BAD_FLAGS);
2311 return FALSE;
2314 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
2316 SetLastError(NTE_BAD_KEY);
2317 return FALSE;
2320 if (pCryptKey->dwState == RSAENH_KEYSTATE_IDLE)
2321 pCryptKey->dwState = RSAENH_KEYSTATE_ENCRYPTING;
2323 if (pCryptKey->dwState != RSAENH_KEYSTATE_ENCRYPTING)
2325 SetLastError(NTE_BAD_DATA);
2326 return FALSE;
2329 dwMax=*pdwDataLen;
2331 if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_BLOCK) {
2332 for (i=0, in=pbData; i<*pdwDataLen; i+=pCryptKey->dwBlockLen, in+=pCryptKey->dwBlockLen) {
2333 switch (pCryptKey->dwMode) {
2334 case CRYPT_MODE_ECB:
2335 encrypt_block_impl(pCryptKey->aiAlgid, 0, &pCryptKey->context, in, out,
2336 RSAENH_DECRYPT);
2337 break;
2339 case CRYPT_MODE_CBC:
2340 encrypt_block_impl(pCryptKey->aiAlgid, 0, &pCryptKey->context, in, out,
2341 RSAENH_DECRYPT);
2342 for (j=0; j<pCryptKey->dwBlockLen; j++) out[j] ^= pCryptKey->abChainVector[j];
2343 memcpy(pCryptKey->abChainVector, in, pCryptKey->dwBlockLen);
2344 break;
2346 case CRYPT_MODE_CFB:
2347 for (j=0; j<pCryptKey->dwBlockLen; j++) {
2348 encrypt_block_impl(pCryptKey->aiAlgid, 0, &pCryptKey->context,
2349 pCryptKey->abChainVector, o, RSAENH_ENCRYPT);
2350 out[j] = in[j] ^ o[0];
2351 for (k=0; k<pCryptKey->dwBlockLen-1; k++)
2352 pCryptKey->abChainVector[k] = pCryptKey->abChainVector[k+1];
2353 pCryptKey->abChainVector[k] = in[j];
2355 break;
2357 default:
2358 SetLastError(NTE_BAD_ALGID);
2359 return FALSE;
2361 memcpy(in, out, pCryptKey->dwBlockLen);
2363 if (Final) {
2364 if (pbData[*pdwDataLen-1] &&
2365 pbData[*pdwDataLen-1] <= pCryptKey->dwBlockLen &&
2366 pbData[*pdwDataLen-1] <= *pdwDataLen) {
2367 BOOL padOkay = TRUE;
2369 /* check that every bad byte has the same value */
2370 for (i = 1; padOkay && i < pbData[*pdwDataLen-1]; i++)
2371 if (pbData[*pdwDataLen - i - 1] != pbData[*pdwDataLen - 1])
2372 padOkay = FALSE;
2373 if (padOkay)
2374 *pdwDataLen -= pbData[*pdwDataLen-1];
2375 else {
2376 SetLastError(NTE_BAD_DATA);
2377 return FALSE;
2380 else {
2381 SetLastError(NTE_BAD_DATA);
2382 return FALSE;
2386 } else if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_STREAM) {
2387 encrypt_stream_impl(pCryptKey->aiAlgid, &pCryptKey->context, pbData, *pdwDataLen);
2388 } else if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_RSA) {
2389 if (pCryptKey->aiAlgid == CALG_RSA_SIGN) {
2390 SetLastError(NTE_BAD_KEY);
2391 return FALSE;
2393 encrypt_block_impl(pCryptKey->aiAlgid, PK_PRIVATE, &pCryptKey->context, pbData, pbData, RSAENH_DECRYPT);
2394 if (!unpad_data(pbData, pCryptKey->dwBlockLen, pbData, pdwDataLen, dwFlags)) return FALSE;
2395 Final = TRUE;
2396 } else {
2397 SetLastError(NTE_BAD_TYPE);
2398 return FALSE;
2401 if (Final) setup_key(pCryptKey);
2403 if (is_valid_handle(&handle_table, hHash, RSAENH_MAGIC_HASH)) {
2404 if (*pdwDataLen>dwMax ||
2405 !RSAENH_CPHashData(hProv, hHash, pbData, *pdwDataLen, 0)) return FALSE;
2408 return TRUE;
2411 static BOOL crypt_export_simple(CRYPTKEY *pCryptKey, CRYPTKEY *pPubKey,
2412 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
2414 BLOBHEADER *pBlobHeader = (BLOBHEADER*)pbData;
2415 ALG_ID *pAlgid = (ALG_ID*)(pBlobHeader+1);
2416 DWORD dwDataLen;
2418 if (!(GET_ALG_CLASS(pCryptKey->aiAlgid)&(ALG_CLASS_DATA_ENCRYPT|ALG_CLASS_MSG_ENCRYPT))) {
2419 SetLastError(NTE_BAD_KEY); /* FIXME: error code? */
2420 return FALSE;
2423 dwDataLen = sizeof(BLOBHEADER) + sizeof(ALG_ID) + pPubKey->dwBlockLen;
2424 if (pbData) {
2425 if (*pdwDataLen < dwDataLen) {
2426 SetLastError(ERROR_MORE_DATA);
2427 *pdwDataLen = dwDataLen;
2428 return FALSE;
2431 pBlobHeader->bType = SIMPLEBLOB;
2432 pBlobHeader->bVersion = CUR_BLOB_VERSION;
2433 pBlobHeader->reserved = 0;
2434 pBlobHeader->aiKeyAlg = pCryptKey->aiAlgid;
2436 *pAlgid = pPubKey->aiAlgid;
2438 if (!pad_data(pCryptKey->abKeyValue, pCryptKey->dwKeyLen, (BYTE*)(pAlgid+1),
2439 pPubKey->dwBlockLen, dwFlags))
2441 return FALSE;
2444 encrypt_block_impl(pPubKey->aiAlgid, PK_PUBLIC, &pPubKey->context, (BYTE*)(pAlgid+1),
2445 (BYTE*)(pAlgid+1), RSAENH_ENCRYPT);
2447 *pdwDataLen = dwDataLen;
2448 return TRUE;
2451 static BOOL crypt_export_public_key(CRYPTKEY *pCryptKey, BYTE *pbData,
2452 DWORD *pdwDataLen)
2454 BLOBHEADER *pBlobHeader = (BLOBHEADER*)pbData;
2455 RSAPUBKEY *pRSAPubKey = (RSAPUBKEY*)(pBlobHeader+1);
2456 DWORD dwDataLen;
2458 if ((pCryptKey->aiAlgid != CALG_RSA_KEYX) && (pCryptKey->aiAlgid != CALG_RSA_SIGN)) {
2459 SetLastError(NTE_BAD_KEY);
2460 return FALSE;
2463 dwDataLen = sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) + pCryptKey->dwKeyLen;
2464 if (pbData) {
2465 if (*pdwDataLen < dwDataLen) {
2466 SetLastError(ERROR_MORE_DATA);
2467 *pdwDataLen = dwDataLen;
2468 return FALSE;
2471 pBlobHeader->bType = PUBLICKEYBLOB;
2472 pBlobHeader->bVersion = CUR_BLOB_VERSION;
2473 pBlobHeader->reserved = 0;
2474 pBlobHeader->aiKeyAlg = pCryptKey->aiAlgid;
2476 pRSAPubKey->magic = RSAENH_MAGIC_RSA1;
2477 pRSAPubKey->bitlen = pCryptKey->dwKeyLen << 3;
2479 export_public_key_impl((BYTE*)(pRSAPubKey+1), &pCryptKey->context,
2480 pCryptKey->dwKeyLen, &pRSAPubKey->pubexp);
2482 *pdwDataLen = dwDataLen;
2483 return TRUE;
2486 static BOOL crypt_export_private_key(CRYPTKEY *pCryptKey, BOOL force,
2487 BYTE *pbData, DWORD *pdwDataLen)
2489 BLOBHEADER *pBlobHeader = (BLOBHEADER*)pbData;
2490 RSAPUBKEY *pRSAPubKey = (RSAPUBKEY*)(pBlobHeader+1);
2491 DWORD dwDataLen;
2493 if ((pCryptKey->aiAlgid != CALG_RSA_KEYX) && (pCryptKey->aiAlgid != CALG_RSA_SIGN)) {
2494 SetLastError(NTE_BAD_KEY);
2495 return FALSE;
2497 if (!force && !(pCryptKey->dwPermissions & CRYPT_EXPORT))
2499 SetLastError(NTE_BAD_KEY_STATE);
2500 return FALSE;
2503 dwDataLen = sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) +
2504 2 * pCryptKey->dwKeyLen + 5 * ((pCryptKey->dwKeyLen + 1) >> 1);
2505 if (pbData) {
2506 if (*pdwDataLen < dwDataLen) {
2507 SetLastError(ERROR_MORE_DATA);
2508 *pdwDataLen = dwDataLen;
2509 return FALSE;
2512 pBlobHeader->bType = PRIVATEKEYBLOB;
2513 pBlobHeader->bVersion = CUR_BLOB_VERSION;
2514 pBlobHeader->reserved = 0;
2515 pBlobHeader->aiKeyAlg = pCryptKey->aiAlgid;
2517 pRSAPubKey->magic = RSAENH_MAGIC_RSA2;
2518 pRSAPubKey->bitlen = pCryptKey->dwKeyLen << 3;
2520 export_private_key_impl((BYTE*)(pRSAPubKey+1), &pCryptKey->context,
2521 pCryptKey->dwKeyLen, &pRSAPubKey->pubexp);
2523 *pdwDataLen = dwDataLen;
2524 return TRUE;
2527 static BOOL crypt_export_plaintext_key(CRYPTKEY *pCryptKey, BYTE *pbData,
2528 DWORD *pdwDataLen)
2530 BLOBHEADER *pBlobHeader = (BLOBHEADER*)pbData;
2531 DWORD *pKeyLen = (DWORD*)(pBlobHeader+1);
2532 BYTE *pbKey = (BYTE*)(pKeyLen+1);
2533 DWORD dwDataLen;
2535 dwDataLen = sizeof(BLOBHEADER) + sizeof(DWORD) + pCryptKey->dwKeyLen;
2536 if (pbData) {
2537 if (*pdwDataLen < dwDataLen) {
2538 SetLastError(ERROR_MORE_DATA);
2539 *pdwDataLen = dwDataLen;
2540 return FALSE;
2543 pBlobHeader->bType = PLAINTEXTKEYBLOB;
2544 pBlobHeader->bVersion = CUR_BLOB_VERSION;
2545 pBlobHeader->reserved = 0;
2546 pBlobHeader->aiKeyAlg = pCryptKey->aiAlgid;
2548 *pKeyLen = pCryptKey->dwKeyLen;
2549 memcpy(pbKey, &pCryptKey->abKeyValue, pCryptKey->dwKeyLen);
2551 *pdwDataLen = dwDataLen;
2552 return TRUE;
2554 /******************************************************************************
2555 * crypt_export_key [Internal]
2557 * Export a key into a binary large object (BLOB). Called by CPExportKey and
2558 * by store_key_pair.
2560 * PARAMS
2561 * pCryptKey [I] Key to be exported.
2562 * hPubKey [I] Key used to encrypt sensitive BLOB data.
2563 * dwBlobType [I] SIMPLEBLOB, PUBLICKEYBLOB or PRIVATEKEYBLOB.
2564 * dwFlags [I] Currently none defined.
2565 * force [I] If TRUE, the key is written no matter what the key's
2566 * permissions are. Otherwise the key's permissions are
2567 * checked before exporting.
2568 * pbData [O] Pointer to a buffer where the BLOB will be written to.
2569 * pdwDataLen [I/O] I: Size of buffer at pbData, O: Size of BLOB
2571 * RETURNS
2572 * Success: TRUE.
2573 * Failure: FALSE.
2575 static BOOL crypt_export_key(CRYPTKEY *pCryptKey, HCRYPTKEY hPubKey,
2576 DWORD dwBlobType, DWORD dwFlags, BOOL force,
2577 BYTE *pbData, DWORD *pdwDataLen)
2579 CRYPTKEY *pPubKey;
2581 if (dwFlags & CRYPT_SSL2_FALLBACK) {
2582 if (pCryptKey->aiAlgid != CALG_SSL2_MASTER) {
2583 SetLastError(NTE_BAD_KEY);
2584 return FALSE;
2588 switch ((BYTE)dwBlobType)
2590 case SIMPLEBLOB:
2591 if (!lookup_handle(&handle_table, hPubKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pPubKey)){
2592 SetLastError(NTE_BAD_PUBLIC_KEY); /* FIXME: error_code? */
2593 return FALSE;
2595 return crypt_export_simple(pCryptKey, pPubKey, dwFlags, pbData,
2596 pdwDataLen);
2598 case PUBLICKEYBLOB:
2599 if (is_valid_handle(&handle_table, hPubKey, RSAENH_MAGIC_KEY)) {
2600 SetLastError(NTE_BAD_KEY); /* FIXME: error code? */
2601 return FALSE;
2604 return crypt_export_public_key(pCryptKey, pbData, pdwDataLen);
2606 case PRIVATEKEYBLOB:
2607 return crypt_export_private_key(pCryptKey, force, pbData, pdwDataLen);
2609 case PLAINTEXTKEYBLOB:
2610 return crypt_export_plaintext_key(pCryptKey, pbData, pdwDataLen);
2612 default:
2613 SetLastError(NTE_BAD_TYPE); /* FIXME: error code? */
2614 return FALSE;
2618 /******************************************************************************
2619 * CPExportKey (RSAENH.@)
2621 * Export a key into a binary large object (BLOB).
2623 * PARAMS
2624 * hProv [I] Key container from which a key is to be exported.
2625 * hKey [I] Key to be exported.
2626 * hPubKey [I] Key used to encrypt sensitive BLOB data.
2627 * dwBlobType [I] SIMPLEBLOB, PUBLICKEYBLOB or PRIVATEKEYBLOB.
2628 * dwFlags [I] Currently none defined.
2629 * pbData [O] Pointer to a buffer where the BLOB will be written to.
2630 * pdwDataLen [I/O] I: Size of buffer at pbData, O: Size of BLOB
2632 * RETURNS
2633 * Success: TRUE.
2634 * Failure: FALSE.
2636 BOOL WINAPI RSAENH_CPExportKey(HCRYPTPROV hProv, HCRYPTKEY hKey, HCRYPTKEY hPubKey,
2637 DWORD dwBlobType, DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
2639 CRYPTKEY *pCryptKey;
2641 TRACE("(hProv=%08lx, hKey=%08lx, hPubKey=%08lx, dwBlobType=%08x, dwFlags=%08x, pbData=%p,"
2642 "pdwDataLen=%p)\n", hProv, hKey, hPubKey, dwBlobType, dwFlags, pbData, pdwDataLen);
2644 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
2646 SetLastError(NTE_BAD_UID);
2647 return FALSE;
2650 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
2652 SetLastError(NTE_BAD_KEY);
2653 return FALSE;
2656 return crypt_export_key(pCryptKey, hPubKey, dwBlobType, dwFlags, FALSE,
2657 pbData, pdwDataLen);
2660 /******************************************************************************
2661 * release_and_install_key [Internal]
2663 * Release an existing key, if present, and replaces it with a new one.
2665 * PARAMS
2666 * hProv [I] Key container into which the key is to be imported.
2667 * src [I] Key which will replace *dest
2668 * dest [I] Points to key to be released and replaced with src
2669 * fStoreKey [I] If TRUE, the newly installed key is stored to the registry.
2671 static void release_and_install_key(HCRYPTPROV hProv, HCRYPTKEY src,
2672 HCRYPTKEY *dest, DWORD fStoreKey)
2674 RSAENH_CPDestroyKey(hProv, *dest);
2675 copy_handle(&handle_table, src, RSAENH_MAGIC_KEY, dest);
2676 if (fStoreKey)
2678 KEYCONTAINER *pKeyContainer;
2680 if (lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER,
2681 (OBJECTHDR**)&pKeyContainer))
2683 store_key_container_keys(pKeyContainer);
2684 store_key_container_permissions(pKeyContainer);
2689 /******************************************************************************
2690 * import_private_key [Internal]
2692 * Import a BLOB'ed private key into a key container.
2694 * PARAMS
2695 * hProv [I] Key container into which the private key is to be imported.
2696 * pbData [I] Pointer to a buffer which holds the private key BLOB.
2697 * dwDataLen [I] Length of data in buffer at pbData.
2698 * dwFlags [I] One of:
2699 * CRYPT_EXPORTABLE: the imported key is marked exportable
2700 * fStoreKey [I] If TRUE, the imported key is stored to the registry.
2701 * phKey [O] Handle to the imported key.
2704 * NOTES
2705 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2706 * it's a PRIVATEKEYBLOB.
2708 * RETURNS
2709 * Success: TRUE.
2710 * Failure: FALSE.
2712 static BOOL import_private_key(HCRYPTPROV hProv, CONST BYTE *pbData, DWORD dwDataLen,
2713 DWORD dwFlags, BOOL fStoreKey, HCRYPTKEY *phKey)
2715 KEYCONTAINER *pKeyContainer;
2716 CRYPTKEY *pCryptKey;
2717 CONST BLOBHEADER *pBlobHeader = (CONST BLOBHEADER*)pbData;
2718 CONST RSAPUBKEY *pRSAPubKey = (CONST RSAPUBKEY*)(pBlobHeader+1);
2719 BOOL ret;
2721 if (dwFlags & CRYPT_IPSEC_HMAC_KEY)
2723 FIXME("unimplemented for CRYPT_IPSEC_HMAC_KEY\n");
2724 SetLastError(NTE_BAD_FLAGS);
2725 return FALSE;
2727 if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER,
2728 (OBJECTHDR**)&pKeyContainer))
2730 SetLastError(NTE_BAD_UID);
2731 return FALSE;
2734 if ((dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY)) ||
2735 (pRSAPubKey->magic != RSAENH_MAGIC_RSA2) ||
2736 (dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) +
2737 (2 * pRSAPubKey->bitlen >> 3) + (5 * ((pRSAPubKey->bitlen+8)>>4))))
2739 SetLastError(NTE_BAD_DATA);
2740 return FALSE;
2743 *phKey = new_key(hProv, pBlobHeader->aiKeyAlg, MAKELONG(0,pRSAPubKey->bitlen), &pCryptKey);
2744 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) return FALSE;
2745 setup_key(pCryptKey);
2746 ret = import_private_key_impl((CONST BYTE*)(pRSAPubKey+1), &pCryptKey->context,
2747 pRSAPubKey->bitlen/8, pRSAPubKey->pubexp);
2748 if (ret) {
2749 if (dwFlags & CRYPT_EXPORTABLE)
2750 pCryptKey->dwPermissions |= CRYPT_EXPORT;
2751 switch (pBlobHeader->aiKeyAlg)
2753 case AT_SIGNATURE:
2754 case CALG_RSA_SIGN:
2755 TRACE("installing signing key\n");
2756 release_and_install_key(hProv, *phKey, &pKeyContainer->hSignatureKeyPair,
2757 fStoreKey);
2758 break;
2759 case AT_KEYEXCHANGE:
2760 case CALG_RSA_KEYX:
2761 TRACE("installing key exchange key\n");
2762 release_and_install_key(hProv, *phKey, &pKeyContainer->hKeyExchangeKeyPair,
2763 fStoreKey);
2764 break;
2767 return ret;
2770 /******************************************************************************
2771 * import_public_key [Internal]
2773 * Import a BLOB'ed public key into a key container.
2775 * PARAMS
2776 * hProv [I] Key container into which the public key is to be imported.
2777 * pbData [I] Pointer to a buffer which holds the public key BLOB.
2778 * dwDataLen [I] Length of data in buffer at pbData.
2779 * dwFlags [I] One of:
2780 * CRYPT_EXPORTABLE: the imported key is marked exportable
2781 * fStoreKey [I] If TRUE, the imported key is stored to the registry.
2782 * phKey [O] Handle to the imported key.
2785 * NOTES
2786 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2787 * it's a PUBLICKEYBLOB.
2789 * RETURNS
2790 * Success: TRUE.
2791 * Failure: FALSE.
2793 static BOOL import_public_key(HCRYPTPROV hProv, CONST BYTE *pbData, DWORD dwDataLen,
2794 DWORD dwFlags, BOOL fStoreKey, HCRYPTKEY *phKey)
2796 KEYCONTAINER *pKeyContainer;
2797 CRYPTKEY *pCryptKey;
2798 CONST BLOBHEADER *pBlobHeader = (CONST BLOBHEADER*)pbData;
2799 CONST RSAPUBKEY *pRSAPubKey = (CONST RSAPUBKEY*)(pBlobHeader+1);
2800 ALG_ID algID;
2801 BOOL ret;
2803 if (dwFlags & CRYPT_IPSEC_HMAC_KEY)
2805 FIXME("unimplemented for CRYPT_IPSEC_HMAC_KEY\n");
2806 SetLastError(NTE_BAD_FLAGS);
2807 return FALSE;
2809 if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER,
2810 (OBJECTHDR**)&pKeyContainer))
2812 SetLastError(NTE_BAD_UID);
2813 return FALSE;
2816 if ((dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY)) ||
2817 (pRSAPubKey->magic != RSAENH_MAGIC_RSA1) ||
2818 (dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) + (pRSAPubKey->bitlen >> 3)))
2820 SetLastError(NTE_BAD_DATA);
2821 return FALSE;
2824 /* Since this is a public key blob, only the public key is
2825 * available, so only signature verification is possible.
2827 algID = pBlobHeader->aiKeyAlg;
2828 *phKey = new_key(hProv, algID, MAKELONG(0,pRSAPubKey->bitlen), &pCryptKey);
2829 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) return FALSE;
2830 setup_key(pCryptKey);
2831 ret = import_public_key_impl((CONST BYTE*)(pRSAPubKey+1), &pCryptKey->context,
2832 pRSAPubKey->bitlen >> 3, pRSAPubKey->pubexp);
2833 if (ret) {
2834 if (dwFlags & CRYPT_EXPORTABLE)
2835 pCryptKey->dwPermissions |= CRYPT_EXPORT;
2836 switch (pBlobHeader->aiKeyAlg)
2838 case AT_KEYEXCHANGE:
2839 case CALG_RSA_KEYX:
2840 TRACE("installing public key\n");
2841 release_and_install_key(hProv, *phKey, &pKeyContainer->hKeyExchangeKeyPair,
2842 fStoreKey);
2843 break;
2846 return ret;
2849 /******************************************************************************
2850 * import_symmetric_key [Internal]
2852 * Import a BLOB'ed symmetric key into a key container.
2854 * PARAMS
2855 * hProv [I] Key container into which the symmetric key is to be imported.
2856 * pbData [I] Pointer to a buffer which holds the symmetric key BLOB.
2857 * dwDataLen [I] Length of data in buffer at pbData.
2858 * hPubKey [I] Key used to decrypt sensitive BLOB data.
2859 * dwFlags [I] One of:
2860 * CRYPT_EXPORTABLE: the imported key is marked exportable
2861 * phKey [O] Handle to the imported key.
2864 * NOTES
2865 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2866 * it's a SIMPLEBLOB.
2868 * RETURNS
2869 * Success: TRUE.
2870 * Failure: FALSE.
2872 static BOOL import_symmetric_key(HCRYPTPROV hProv, CONST BYTE *pbData,
2873 DWORD dwDataLen, HCRYPTKEY hPubKey,
2874 DWORD dwFlags, HCRYPTKEY *phKey)
2876 CRYPTKEY *pCryptKey, *pPubKey;
2877 CONST BLOBHEADER *pBlobHeader = (CONST BLOBHEADER*)pbData;
2878 CONST ALG_ID *pAlgid = (CONST ALG_ID*)(pBlobHeader+1);
2879 CONST BYTE *pbKeyStream = (CONST BYTE*)(pAlgid + 1);
2880 BYTE *pbDecrypted;
2881 DWORD dwKeyLen;
2883 if (dwFlags & CRYPT_IPSEC_HMAC_KEY)
2885 FIXME("unimplemented for CRYPT_IPSEC_HMAC_KEY\n");
2886 SetLastError(NTE_BAD_FLAGS);
2887 return FALSE;
2889 if (!lookup_handle(&handle_table, hPubKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pPubKey) ||
2890 pPubKey->aiAlgid != CALG_RSA_KEYX)
2892 SetLastError(NTE_BAD_PUBLIC_KEY); /* FIXME: error code? */
2893 return FALSE;
2896 if (dwDataLen < sizeof(BLOBHEADER)+sizeof(ALG_ID)+pPubKey->dwBlockLen)
2898 SetLastError(NTE_BAD_DATA); /* FIXME: error code */
2899 return FALSE;
2902 pbDecrypted = HeapAlloc(GetProcessHeap(), 0, pPubKey->dwBlockLen);
2903 if (!pbDecrypted) return FALSE;
2904 encrypt_block_impl(pPubKey->aiAlgid, PK_PRIVATE, &pPubKey->context, pbKeyStream, pbDecrypted,
2905 RSAENH_DECRYPT);
2907 dwKeyLen = RSAENH_MAX_KEY_SIZE;
2908 if (!unpad_data(pbDecrypted, pPubKey->dwBlockLen, pbDecrypted, &dwKeyLen, dwFlags)) {
2909 HeapFree(GetProcessHeap(), 0, pbDecrypted);
2910 return FALSE;
2913 *phKey = new_key(hProv, pBlobHeader->aiKeyAlg, dwKeyLen<<19, &pCryptKey);
2914 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE)
2916 HeapFree(GetProcessHeap(), 0, pbDecrypted);
2917 return FALSE;
2919 memcpy(pCryptKey->abKeyValue, pbDecrypted, dwKeyLen);
2920 HeapFree(GetProcessHeap(), 0, pbDecrypted);
2921 setup_key(pCryptKey);
2922 if (dwFlags & CRYPT_EXPORTABLE)
2923 pCryptKey->dwPermissions |= CRYPT_EXPORT;
2924 return TRUE;
2927 /******************************************************************************
2928 * import_plaintext_key [Internal]
2930 * Import a plaintext key into a key container.
2932 * PARAMS
2933 * hProv [I] Key container into which the symmetric key is to be imported.
2934 * pbData [I] Pointer to a buffer which holds the plaintext key BLOB.
2935 * dwDataLen [I] Length of data in buffer at pbData.
2936 * dwFlags [I] One of:
2937 * CRYPT_EXPORTABLE: the imported key is marked exportable
2938 * phKey [O] Handle to the imported key.
2941 * NOTES
2942 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2943 * it's a PLAINTEXTKEYBLOB.
2945 * RETURNS
2946 * Success: TRUE.
2947 * Failure: FALSE.
2949 static BOOL import_plaintext_key(HCRYPTPROV hProv, CONST BYTE *pbData,
2950 DWORD dwDataLen, DWORD dwFlags,
2951 HCRYPTKEY *phKey)
2953 CRYPTKEY *pCryptKey;
2954 CONST BLOBHEADER *pBlobHeader = (CONST BLOBHEADER*)pbData;
2955 CONST DWORD *pKeyLen = (CONST DWORD *)(pBlobHeader + 1);
2956 CONST BYTE *pbKeyStream = (CONST BYTE*)(pKeyLen + 1);
2958 if (dwDataLen < sizeof(BLOBHEADER)+sizeof(DWORD)+*pKeyLen)
2960 SetLastError(NTE_BAD_DATA); /* FIXME: error code */
2961 return FALSE;
2964 if (dwFlags & CRYPT_IPSEC_HMAC_KEY)
2966 *phKey = new_key(hProv, CALG_HMAC, 0, &pCryptKey);
2967 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE)
2968 return FALSE;
2969 if (*pKeyLen <= RSAENH_MIN(sizeof(pCryptKey->abKeyValue), RSAENH_HMAC_BLOCK_LEN))
2971 memcpy(pCryptKey->abKeyValue, pbKeyStream, *pKeyLen);
2972 pCryptKey->dwKeyLen = *pKeyLen;
2974 else
2976 CRYPT_DATA_BLOB blobHmacKey = { *pKeyLen, (BYTE *)pbKeyStream };
2978 /* In order to initialize an HMAC key, the key material is hashed,
2979 * and the output of the hash function is used as the key material.
2980 * Unfortunately, the way the Crypto API is designed, we don't know
2981 * the hash algorithm yet, so we have to copy the entire key
2982 * material.
2984 if (!copy_data_blob(&pCryptKey->blobHmacKey, &blobHmacKey))
2986 release_handle(&handle_table, *phKey, RSAENH_MAGIC_KEY);
2987 *phKey = (HCRYPTKEY)INVALID_HANDLE_VALUE;
2988 return FALSE;
2991 setup_key(pCryptKey);
2992 if (dwFlags & CRYPT_EXPORTABLE)
2993 pCryptKey->dwPermissions |= CRYPT_EXPORT;
2995 else
2997 *phKey = new_key(hProv, pBlobHeader->aiKeyAlg, *pKeyLen<<19, &pCryptKey);
2998 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE)
2999 return FALSE;
3000 memcpy(pCryptKey->abKeyValue, pbKeyStream, *pKeyLen);
3001 setup_key(pCryptKey);
3002 if (dwFlags & CRYPT_EXPORTABLE)
3003 pCryptKey->dwPermissions |= CRYPT_EXPORT;
3005 return TRUE;
3008 /******************************************************************************
3009 * import_key [Internal]
3011 * Import a BLOB'ed key into a key container, optionally storing the key's
3012 * value to the registry.
3014 * PARAMS
3015 * hProv [I] Key container into which the key is to be imported.
3016 * pbData [I] Pointer to a buffer which holds the BLOB.
3017 * dwDataLen [I] Length of data in buffer at pbData.
3018 * hPubKey [I] Key used to decrypt sensitive BLOB data.
3019 * dwFlags [I] One of:
3020 * CRYPT_EXPORTABLE: the imported key is marked exportable
3021 * fStoreKey [I] If TRUE, the imported key is stored to the registry.
3022 * phKey [O] Handle to the imported key.
3024 * RETURNS
3025 * Success: TRUE.
3026 * Failure: FALSE.
3028 static BOOL import_key(HCRYPTPROV hProv, CONST BYTE *pbData, DWORD dwDataLen,
3029 HCRYPTKEY hPubKey, DWORD dwFlags, BOOL fStoreKey,
3030 HCRYPTKEY *phKey)
3032 KEYCONTAINER *pKeyContainer;
3033 CONST BLOBHEADER *pBlobHeader = (CONST BLOBHEADER*)pbData;
3035 if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER,
3036 (OBJECTHDR**)&pKeyContainer))
3038 SetLastError(NTE_BAD_UID);
3039 return FALSE;
3042 if (dwDataLen < sizeof(BLOBHEADER) ||
3043 pBlobHeader->bVersion != CUR_BLOB_VERSION ||
3044 pBlobHeader->reserved != 0)
3046 TRACE("bVersion = %d, reserved = %d\n", pBlobHeader->bVersion,
3047 pBlobHeader->reserved);
3048 SetLastError(NTE_BAD_DATA);
3049 return FALSE;
3052 /* If this is a verify-only context, the key is not persisted regardless of
3053 * fStoreKey's original value.
3055 fStoreKey = fStoreKey && !(dwFlags & CRYPT_VERIFYCONTEXT);
3056 TRACE("blob type: %x\n", pBlobHeader->bType);
3057 switch (pBlobHeader->bType)
3059 case PRIVATEKEYBLOB:
3060 return import_private_key(hProv, pbData, dwDataLen, dwFlags,
3061 fStoreKey, phKey);
3063 case PUBLICKEYBLOB:
3064 return import_public_key(hProv, pbData, dwDataLen, dwFlags,
3065 fStoreKey, phKey);
3067 case SIMPLEBLOB:
3068 return import_symmetric_key(hProv, pbData, dwDataLen, hPubKey,
3069 dwFlags, phKey);
3071 case PLAINTEXTKEYBLOB:
3072 return import_plaintext_key(hProv, pbData, dwDataLen, dwFlags,
3073 phKey);
3075 default:
3076 SetLastError(NTE_BAD_TYPE); /* FIXME: error code? */
3077 return FALSE;
3081 /******************************************************************************
3082 * CPImportKey (RSAENH.@)
3084 * Import a BLOB'ed key into a key container.
3086 * PARAMS
3087 * hProv [I] Key container into which the key is to be imported.
3088 * pbData [I] Pointer to a buffer which holds the BLOB.
3089 * dwDataLen [I] Length of data in buffer at pbData.
3090 * hPubKey [I] Key used to decrypt sensitive BLOB data.
3091 * dwFlags [I] One of:
3092 * CRYPT_EXPORTABLE: the imported key is marked exportable
3093 * phKey [O] Handle to the imported key.
3095 * RETURNS
3096 * Success: TRUE.
3097 * Failure: FALSE.
3099 BOOL WINAPI RSAENH_CPImportKey(HCRYPTPROV hProv, CONST BYTE *pbData, DWORD dwDataLen,
3100 HCRYPTKEY hPubKey, DWORD dwFlags, HCRYPTKEY *phKey)
3102 TRACE("(hProv=%08lx, pbData=%p, dwDataLen=%d, hPubKey=%08lx, dwFlags=%08x, phKey=%p)\n",
3103 hProv, pbData, dwDataLen, hPubKey, dwFlags, phKey);
3105 return import_key(hProv, pbData, dwDataLen, hPubKey, dwFlags, TRUE, phKey);
3108 /******************************************************************************
3109 * CPGenKey (RSAENH.@)
3111 * Generate a key in the key container
3113 * PARAMS
3114 * hProv [I] Key container for which a key is to be generated.
3115 * Algid [I] Crypto algorithm identifier for the key to be generated.
3116 * dwFlags [I] Upper 16 bits: Binary length of key. Lower 16 bits: Flags. See Notes
3117 * phKey [O] Handle to the generated key.
3119 * RETURNS
3120 * Success: TRUE.
3121 * Failure: FALSE.
3123 * FIXME
3124 * Flags currently not considered.
3126 * NOTES
3127 * Private key-exchange- and signature-keys can be generated with Algid AT_KEYEXCHANGE
3128 * and AT_SIGNATURE values.
3130 BOOL WINAPI RSAENH_CPGenKey(HCRYPTPROV hProv, ALG_ID Algid, DWORD dwFlags, HCRYPTKEY *phKey)
3132 KEYCONTAINER *pKeyContainer;
3133 CRYPTKEY *pCryptKey;
3135 TRACE("(hProv=%08lx, aiAlgid=%d, dwFlags=%08x, phKey=%p)\n", hProv, Algid, dwFlags, phKey);
3137 if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER,
3138 (OBJECTHDR**)&pKeyContainer))
3140 /* MSDN: hProv not containing valid context handle */
3141 SetLastError(NTE_BAD_UID);
3142 return FALSE;
3145 switch (Algid)
3147 case AT_SIGNATURE:
3148 case CALG_RSA_SIGN:
3149 *phKey = new_key(hProv, CALG_RSA_SIGN, dwFlags, &pCryptKey);
3150 if (pCryptKey) {
3151 new_key_impl(pCryptKey->aiAlgid, &pCryptKey->context, pCryptKey->dwKeyLen);
3152 setup_key(pCryptKey);
3153 release_and_install_key(hProv, *phKey,
3154 &pKeyContainer->hSignatureKeyPair,
3155 FALSE);
3157 break;
3159 case AT_KEYEXCHANGE:
3160 case CALG_RSA_KEYX:
3161 *phKey = new_key(hProv, CALG_RSA_KEYX, dwFlags, &pCryptKey);
3162 if (pCryptKey) {
3163 new_key_impl(pCryptKey->aiAlgid, &pCryptKey->context, pCryptKey->dwKeyLen);
3164 setup_key(pCryptKey);
3165 release_and_install_key(hProv, *phKey,
3166 &pKeyContainer->hKeyExchangeKeyPair,
3167 FALSE);
3169 break;
3171 case CALG_RC2:
3172 case CALG_RC4:
3173 case CALG_DES:
3174 case CALG_3DES_112:
3175 case CALG_3DES:
3176 case CALG_AES:
3177 case CALG_AES_128:
3178 case CALG_AES_192:
3179 case CALG_AES_256:
3180 case CALG_PCT1_MASTER:
3181 case CALG_SSL2_MASTER:
3182 case CALG_SSL3_MASTER:
3183 case CALG_TLS1_MASTER:
3184 *phKey = new_key(hProv, Algid, dwFlags, &pCryptKey);
3185 if (pCryptKey) {
3186 gen_rand_impl(pCryptKey->abKeyValue, RSAENH_MAX_KEY_SIZE);
3187 switch (Algid) {
3188 case CALG_SSL3_MASTER:
3189 pCryptKey->abKeyValue[0] = RSAENH_SSL3_VERSION_MAJOR;
3190 pCryptKey->abKeyValue[1] = RSAENH_SSL3_VERSION_MINOR;
3191 break;
3193 case CALG_TLS1_MASTER:
3194 pCryptKey->abKeyValue[0] = RSAENH_TLS1_VERSION_MAJOR;
3195 pCryptKey->abKeyValue[1] = RSAENH_TLS1_VERSION_MINOR;
3196 break;
3198 setup_key(pCryptKey);
3200 break;
3202 default:
3203 /* MSDN: Algorithm not supported specified by Algid */
3204 SetLastError(NTE_BAD_ALGID);
3205 return FALSE;
3208 return *phKey != (HCRYPTKEY)INVALID_HANDLE_VALUE;
3211 /******************************************************************************
3212 * CPGenRandom (RSAENH.@)
3214 * Generate a random byte stream.
3216 * PARAMS
3217 * hProv [I] Key container that is used to generate random bytes.
3218 * dwLen [I] Specifies the number of requested random data bytes.
3219 * pbBuffer [O] Random bytes will be stored here.
3221 * RETURNS
3222 * Success: TRUE
3223 * Failure: FALSE
3225 BOOL WINAPI RSAENH_CPGenRandom(HCRYPTPROV hProv, DWORD dwLen, BYTE *pbBuffer)
3227 TRACE("(hProv=%08lx, dwLen=%d, pbBuffer=%p)\n", hProv, dwLen, pbBuffer);
3229 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
3231 /* MSDN: hProv not containing valid context handle */
3232 SetLastError(NTE_BAD_UID);
3233 return FALSE;
3236 return gen_rand_impl(pbBuffer, dwLen);
3239 /******************************************************************************
3240 * CPGetHashParam (RSAENH.@)
3242 * Query parameters of an hash object.
3244 * PARAMS
3245 * hProv [I] The kea container, which the hash belongs to.
3246 * hHash [I] The hash object that is to be queried.
3247 * dwParam [I] Specifies the parameter that is to be queried.
3248 * pbData [I] Pointer to the buffer where the parameter value will be stored.
3249 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
3250 * dwFlags [I] None currently defined.
3252 * RETURNS
3253 * Success: TRUE
3254 * Failure: FALSE
3256 * NOTES
3257 * Valid dwParams are: HP_ALGID, HP_HASHSIZE, HP_HASHVALUE. The hash will be
3258 * finalized if HP_HASHVALUE is queried.
3260 BOOL WINAPI RSAENH_CPGetHashParam(HCRYPTPROV hProv, HCRYPTHASH hHash, DWORD dwParam, BYTE *pbData,
3261 DWORD *pdwDataLen, DWORD dwFlags)
3263 CRYPTHASH *pCryptHash;
3265 TRACE("(hProv=%08lx, hHash=%08lx, dwParam=%08x, pbData=%p, pdwDataLen=%p, dwFlags=%08x)\n",
3266 hProv, hHash, dwParam, pbData, pdwDataLen, dwFlags);
3268 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
3270 SetLastError(NTE_BAD_UID);
3271 return FALSE;
3274 if (dwFlags)
3276 SetLastError(NTE_BAD_FLAGS);
3277 return FALSE;
3280 if (!lookup_handle(&handle_table, hHash, RSAENH_MAGIC_HASH,
3281 (OBJECTHDR**)&pCryptHash))
3283 SetLastError(NTE_BAD_HASH);
3284 return FALSE;
3287 if (!pdwDataLen)
3289 SetLastError(ERROR_INVALID_PARAMETER);
3290 return FALSE;
3293 switch (dwParam)
3295 case HP_ALGID:
3296 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptHash->aiAlgid,
3297 sizeof(ALG_ID));
3299 case HP_HASHSIZE:
3300 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptHash->dwHashSize,
3301 sizeof(DWORD));
3303 case HP_HASHVAL:
3304 if (pCryptHash->aiAlgid == CALG_TLS1PRF) {
3305 return tls1_prf(hProv, pCryptHash->hKey, &pCryptHash->tpPRFParams.blobLabel,
3306 &pCryptHash->tpPRFParams.blobSeed, pbData, *pdwDataLen);
3309 if ( pbData == NULL ) {
3310 *pdwDataLen = pCryptHash->dwHashSize;
3311 return TRUE;
3314 if (pbData && (pCryptHash->dwState != RSAENH_HASHSTATE_FINISHED))
3316 finalize_hash(pCryptHash);
3317 pCryptHash->dwState = RSAENH_HASHSTATE_FINISHED;
3320 return copy_param(pbData, pdwDataLen, pCryptHash->abHashValue,
3321 pCryptHash->dwHashSize);
3323 default:
3324 SetLastError(NTE_BAD_TYPE);
3325 return FALSE;
3329 /******************************************************************************
3330 * CPSetKeyParam (RSAENH.@)
3332 * Set a parameter of a key object
3334 * PARAMS
3335 * hProv [I] The key container to which the key belongs.
3336 * hKey [I] The key for which a parameter is to be set.
3337 * dwParam [I] Parameter type. See Notes.
3338 * pbData [I] Pointer to the parameter value.
3339 * dwFlags [I] Currently none defined.
3341 * RETURNS
3342 * Success: TRUE.
3343 * Failure: FALSE.
3345 * NOTES:
3346 * Defined dwParam types are:
3347 * - KP_MODE: Values MODE_CBC, MODE_ECB, MODE_CFB.
3348 * - KP_MODE_BITS: Shift width for cipher feedback mode. (Currently ignored by MS CSP's)
3349 * - KP_PERMISSIONS: Or'ed combination of CRYPT_ENCRYPT, CRYPT_DECRYPT,
3350 * CRYPT_EXPORT, CRYPT_READ, CRYPT_WRITE, CRYPT_MAC
3351 * - KP_IV: Initialization vector
3353 BOOL WINAPI RSAENH_CPSetKeyParam(HCRYPTPROV hProv, HCRYPTKEY hKey, DWORD dwParam, BYTE *pbData,
3354 DWORD dwFlags)
3356 CRYPTKEY *pCryptKey;
3358 TRACE("(hProv=%08lx, hKey=%08lx, dwParam=%08x, pbData=%p, dwFlags=%08x)\n", hProv, hKey,
3359 dwParam, pbData, dwFlags);
3361 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
3363 SetLastError(NTE_BAD_UID);
3364 return FALSE;
3367 if (dwFlags) {
3368 SetLastError(NTE_BAD_FLAGS);
3369 return FALSE;
3372 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
3374 SetLastError(NTE_BAD_KEY);
3375 return FALSE;
3378 switch (dwParam) {
3379 case KP_PADDING:
3380 /* The MS providers only support PKCS5_PADDING */
3381 if (*(DWORD *)pbData != PKCS5_PADDING) {
3382 SetLastError(NTE_BAD_DATA);
3383 return FALSE;
3385 return TRUE;
3387 case KP_MODE:
3388 pCryptKey->dwMode = *(DWORD*)pbData;
3389 return TRUE;
3391 case KP_MODE_BITS:
3392 pCryptKey->dwModeBits = *(DWORD*)pbData;
3393 return TRUE;
3395 case KP_PERMISSIONS:
3397 DWORD perms = *(DWORD *)pbData;
3399 if ((perms & CRYPT_EXPORT) &&
3400 !(pCryptKey->dwPermissions & CRYPT_EXPORT))
3402 SetLastError(NTE_BAD_DATA);
3403 return FALSE;
3405 else if (!(perms & CRYPT_EXPORT) &&
3406 (pCryptKey->dwPermissions & CRYPT_EXPORT))
3408 /* Clearing the export permission appears to be ignored,
3409 * see tests.
3411 perms |= CRYPT_EXPORT;
3413 pCryptKey->dwPermissions = perms;
3414 return TRUE;
3417 case KP_IV:
3418 memcpy(pCryptKey->abInitVector, pbData, pCryptKey->dwBlockLen);
3419 setup_key(pCryptKey);
3420 return TRUE;
3422 case KP_SALT:
3423 switch (pCryptKey->aiAlgid) {
3424 case CALG_RC2:
3425 case CALG_RC4:
3426 if (!pbData)
3428 SetLastError(ERROR_INVALID_PARAMETER);
3429 return FALSE;
3431 /* MSDN: the base provider always sets eleven bytes of
3432 * salt value.
3434 memcpy(pCryptKey->abKeyValue + pCryptKey->dwKeyLen,
3435 pbData, 11);
3436 pCryptKey->dwSaltLen = 11;
3437 setup_key(pCryptKey);
3438 /* Strange but true: salt length reset to 0 after setting
3439 * it via KP_SALT.
3441 pCryptKey->dwSaltLen = 0;
3442 break;
3443 default:
3444 SetLastError(NTE_BAD_KEY);
3445 return FALSE;
3447 return TRUE;
3449 case KP_SALT_EX:
3451 CRYPT_INTEGER_BLOB *blob = (CRYPT_INTEGER_BLOB *)pbData;
3453 /* salt length can't be greater than 184 bits = 24 bytes */
3454 if (blob->cbData > 24)
3456 SetLastError(NTE_BAD_DATA);
3457 return FALSE;
3459 memcpy(pCryptKey->abKeyValue + pCryptKey->dwKeyLen, blob->pbData,
3460 blob->cbData);
3461 pCryptKey->dwSaltLen = blob->cbData;
3462 setup_key(pCryptKey);
3463 return TRUE;
3466 case KP_EFFECTIVE_KEYLEN:
3467 switch (pCryptKey->aiAlgid) {
3468 case CALG_RC2:
3469 if (!pbData)
3471 SetLastError(ERROR_INVALID_PARAMETER);
3472 return FALSE;
3474 else if (!*(DWORD *)pbData || *(DWORD *)pbData > 1024)
3476 SetLastError(NTE_BAD_DATA);
3477 return FALSE;
3479 else
3481 pCryptKey->dwEffectiveKeyLen = *(DWORD *)pbData;
3482 setup_key(pCryptKey);
3484 break;
3485 default:
3486 SetLastError(NTE_BAD_TYPE);
3487 return FALSE;
3489 return TRUE;
3491 case KP_SCHANNEL_ALG:
3492 switch (((PSCHANNEL_ALG)pbData)->dwUse) {
3493 case SCHANNEL_ENC_KEY:
3494 memcpy(&pCryptKey->siSChannelInfo.saEncAlg, pbData, sizeof(SCHANNEL_ALG));
3495 break;
3497 case SCHANNEL_MAC_KEY:
3498 memcpy(&pCryptKey->siSChannelInfo.saMACAlg, pbData, sizeof(SCHANNEL_ALG));
3499 break;
3501 default:
3502 SetLastError(NTE_FAIL); /* FIXME: error code */
3503 return FALSE;
3505 return TRUE;
3507 case KP_CLIENT_RANDOM:
3508 return copy_data_blob(&pCryptKey->siSChannelInfo.blobClientRandom, (PCRYPT_DATA_BLOB)pbData);
3510 case KP_SERVER_RANDOM:
3511 return copy_data_blob(&pCryptKey->siSChannelInfo.blobServerRandom, (PCRYPT_DATA_BLOB)pbData);
3513 default:
3514 SetLastError(NTE_BAD_TYPE);
3515 return FALSE;
3519 /******************************************************************************
3520 * CPGetKeyParam (RSAENH.@)
3522 * Query a key parameter.
3524 * PARAMS
3525 * hProv [I] The key container, which the key belongs to.
3526 * hHash [I] The key object that is to be queried.
3527 * dwParam [I] Specifies the parameter that is to be queried.
3528 * pbData [I] Pointer to the buffer where the parameter value will be stored.
3529 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
3530 * dwFlags [I] None currently defined.
3532 * RETURNS
3533 * Success: TRUE
3534 * Failure: FALSE
3536 * NOTES
3537 * Defined dwParam types are:
3538 * - KP_MODE: Values MODE_CBC, MODE_ECB, MODE_CFB.
3539 * - KP_MODE_BITS: Shift width for cipher feedback mode.
3540 * (Currently ignored by MS CSP's - always eight)
3541 * - KP_PERMISSIONS: Or'ed combination of CRYPT_ENCRYPT, CRYPT_DECRYPT,
3542 * CRYPT_EXPORT, CRYPT_READ, CRYPT_WRITE, CRYPT_MAC
3543 * - KP_IV: Initialization vector.
3544 * - KP_KEYLEN: Bitwidth of the key.
3545 * - KP_BLOCKLEN: Size of a block cipher block.
3546 * - KP_SALT: Salt value.
3548 BOOL WINAPI RSAENH_CPGetKeyParam(HCRYPTPROV hProv, HCRYPTKEY hKey, DWORD dwParam, BYTE *pbData,
3549 DWORD *pdwDataLen, DWORD dwFlags)
3551 CRYPTKEY *pCryptKey;
3552 DWORD dwValue;
3554 TRACE("(hProv=%08lx, hKey=%08lx, dwParam=%08x, pbData=%p, pdwDataLen=%p dwFlags=%08x)\n",
3555 hProv, hKey, dwParam, pbData, pdwDataLen, dwFlags);
3557 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
3559 SetLastError(NTE_BAD_UID);
3560 return FALSE;
3563 if (dwFlags) {
3564 SetLastError(NTE_BAD_FLAGS);
3565 return FALSE;
3568 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
3570 SetLastError(NTE_BAD_KEY);
3571 return FALSE;
3574 switch (dwParam)
3576 case KP_IV:
3577 return copy_param(pbData, pdwDataLen, pCryptKey->abInitVector,
3578 pCryptKey->dwBlockLen);
3580 case KP_SALT:
3581 switch (pCryptKey->aiAlgid) {
3582 case CALG_RC2:
3583 case CALG_RC4:
3584 return copy_param(pbData, pdwDataLen,
3585 &pCryptKey->abKeyValue[pCryptKey->dwKeyLen],
3586 pCryptKey->dwSaltLen);
3587 default:
3588 SetLastError(NTE_BAD_KEY);
3589 return FALSE;
3592 case KP_PADDING:
3593 dwValue = PKCS5_PADDING;
3594 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwValue, sizeof(DWORD));
3596 case KP_KEYLEN:
3597 dwValue = pCryptKey->dwKeyLen << 3;
3598 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwValue, sizeof(DWORD));
3600 case KP_EFFECTIVE_KEYLEN:
3601 if (pCryptKey->dwEffectiveKeyLen)
3602 dwValue = pCryptKey->dwEffectiveKeyLen;
3603 else
3604 dwValue = pCryptKey->dwKeyLen << 3;
3605 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwValue, sizeof(DWORD));
3607 case KP_BLOCKLEN:
3608 dwValue = pCryptKey->dwBlockLen << 3;
3609 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwValue, sizeof(DWORD));
3611 case KP_MODE:
3612 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptKey->dwMode, sizeof(DWORD));
3614 case KP_MODE_BITS:
3615 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptKey->dwModeBits,
3616 sizeof(DWORD));
3618 case KP_PERMISSIONS:
3619 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptKey->dwPermissions,
3620 sizeof(DWORD));
3622 case KP_ALGID:
3623 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptKey->aiAlgid, sizeof(DWORD));
3625 default:
3626 SetLastError(NTE_BAD_TYPE);
3627 return FALSE;
3631 /******************************************************************************
3632 * CPGetProvParam (RSAENH.@)
3634 * Query a CSP parameter.
3636 * PARAMS
3637 * hProv [I] The key container that is to be queried.
3638 * dwParam [I] Specifies the parameter that is to be queried.
3639 * pbData [I] Pointer to the buffer where the parameter value will be stored.
3640 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
3641 * dwFlags [I] CRYPT_FIRST: Start enumeration (for PP_ENUMALGS{_EX}).
3643 * RETURNS
3644 * Success: TRUE
3645 * Failure: FALSE
3646 * NOTES:
3647 * Defined dwParam types:
3648 * - PP_CONTAINER: Name of the key container.
3649 * - PP_NAME: Name of the cryptographic service provider.
3650 * - PP_SIG_KEYSIZE_INC: RSA signature keywidth granularity in bits.
3651 * - PP_KEYX_KEYSIZE_INC: RSA key-exchange keywidth granularity in bits.
3652 * - PP_ENUMALGS{_EX}: Query provider capabilities.
3654 BOOL WINAPI RSAENH_CPGetProvParam(HCRYPTPROV hProv, DWORD dwParam, BYTE *pbData,
3655 DWORD *pdwDataLen, DWORD dwFlags)
3657 KEYCONTAINER *pKeyContainer;
3658 PROV_ENUMALGS provEnumalgs;
3659 DWORD dwTemp;
3660 HKEY hKey;
3662 /* This is for dwParam PP_CRYPT_COUNT_KEY_USE.
3663 * IE6 SP1 asks for it in the 'About' dialog.
3664 * Returning this BLOB seems to satisfy IE. The marked 0x00 seem
3665 * to be 'don't care's. If you know anything more specific about
3666 * this provider parameter, please report to wine-devel@winehq.org */
3667 static CONST BYTE abWTF[96] = {
3668 0xb0, 0x25, 0x63, 0x86, 0x9c, 0xab, 0xb6, 0x37,
3669 0xe8, 0x82, /**/0x00,/**/ 0x72, 0x06, 0xb2, /**/0x00,/**/ 0x3b,
3670 0x60, 0x35, /**/0x00,/**/ 0x3b, 0x88, 0xce, /**/0x00,/**/ 0x82,
3671 0xbc, 0x7a, /**/0x00,/**/ 0xb7, 0x4f, 0x7e, /**/0x00,/**/ 0xde,
3672 0x92, 0xf1, /**/0x00,/**/ 0x83, 0xea, 0x5e, /**/0x00,/**/ 0xc8,
3673 0x12, 0x1e, 0xd4, 0x06, 0xf7, 0x66, /**/0x00,/**/ 0x01,
3674 0x29, 0xa4, /**/0x00,/**/ 0xf8, 0x24, 0x0c, /**/0x00,/**/ 0x33,
3675 0x06, 0x80, /**/0x00,/**/ 0x02, 0x46, 0x0b, /**/0x00,/**/ 0x6d,
3676 0x5b, 0xca, /**/0x00,/**/ 0x9a, 0x10, 0xf0, /**/0x00,/**/ 0x05,
3677 0x19, 0xd0, /**/0x00,/**/ 0x2c, 0xf6, 0x27, /**/0x00,/**/ 0xaa,
3678 0x7c, 0x6f, /**/0x00,/**/ 0xb9, 0xd8, 0x72, /**/0x00,/**/ 0x03,
3679 0xf3, 0x81, /**/0x00,/**/ 0xfa, 0xe8, 0x26, /**/0x00,/**/ 0xca
3682 TRACE("(hProv=%08lx, dwParam=%08x, pbData=%p, pdwDataLen=%p, dwFlags=%08x)\n",
3683 hProv, dwParam, pbData, pdwDataLen, dwFlags);
3685 if (!pdwDataLen) {
3686 SetLastError(ERROR_INVALID_PARAMETER);
3687 return FALSE;
3690 if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER,
3691 (OBJECTHDR**)&pKeyContainer))
3693 /* MSDN: hProv not containing valid context handle */
3694 SetLastError(NTE_BAD_UID);
3695 return FALSE;
3698 switch (dwParam)
3700 case PP_CONTAINER:
3701 case PP_UNIQUE_CONTAINER:/* MSDN says we can return the same value as PP_CONTAINER */
3702 return copy_param(pbData, pdwDataLen, (CONST BYTE*)pKeyContainer->szName,
3703 strlen(pKeyContainer->szName)+1);
3705 case PP_NAME:
3706 return copy_param(pbData, pdwDataLen, (CONST BYTE*)pKeyContainer->szProvName,
3707 strlen(pKeyContainer->szProvName)+1);
3709 case PP_PROVTYPE:
3710 dwTemp = PROV_RSA_FULL;
3711 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwTemp, sizeof(dwTemp));
3713 case PP_KEYSPEC:
3714 dwTemp = AT_SIGNATURE | AT_KEYEXCHANGE;
3715 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwTemp, sizeof(dwTemp));
3717 case PP_KEYSET_TYPE:
3718 dwTemp = pKeyContainer->dwFlags & CRYPT_MACHINE_KEYSET;
3719 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwTemp, sizeof(dwTemp));
3721 case PP_KEYSTORAGE:
3722 dwTemp = CRYPT_SEC_DESCR;
3723 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwTemp, sizeof(dwTemp));
3725 case PP_SIG_KEYSIZE_INC:
3726 case PP_KEYX_KEYSIZE_INC:
3727 dwTemp = 8;
3728 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwTemp, sizeof(dwTemp));
3730 case PP_IMPTYPE:
3731 dwTemp = CRYPT_IMPL_SOFTWARE;
3732 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwTemp, sizeof(dwTemp));
3734 case PP_VERSION:
3735 dwTemp = 0x00000200;
3736 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwTemp, sizeof(dwTemp));
3738 case PP_ENUMCONTAINERS:
3739 if ((dwFlags & CRYPT_FIRST) == CRYPT_FIRST) pKeyContainer->dwEnumContainersCtr = 0;
3741 if (!pbData) {
3742 *pdwDataLen = (DWORD)MAX_PATH + 1;
3743 return TRUE;
3746 if (!open_container_key("", dwFlags, &hKey))
3748 SetLastError(ERROR_NO_MORE_ITEMS);
3749 return FALSE;
3752 dwTemp = *pdwDataLen;
3753 switch (RegEnumKeyExA(hKey, pKeyContainer->dwEnumContainersCtr, (LPSTR)pbData, &dwTemp,
3754 NULL, NULL, NULL, NULL))
3756 case ERROR_MORE_DATA:
3757 *pdwDataLen = (DWORD)MAX_PATH + 1;
3759 case ERROR_SUCCESS:
3760 pKeyContainer->dwEnumContainersCtr++;
3761 RegCloseKey(hKey);
3762 return TRUE;
3764 case ERROR_NO_MORE_ITEMS:
3765 default:
3766 SetLastError(ERROR_NO_MORE_ITEMS);
3767 RegCloseKey(hKey);
3768 return FALSE;
3771 case PP_ENUMALGS:
3772 case PP_ENUMALGS_EX:
3773 if (((pKeyContainer->dwEnumAlgsCtr >= RSAENH_MAX_ENUMALGS-1) ||
3774 (!aProvEnumAlgsEx[pKeyContainer->dwPersonality]
3775 [pKeyContainer->dwEnumAlgsCtr+1].aiAlgid)) &&
3776 ((dwFlags & CRYPT_FIRST) != CRYPT_FIRST))
3778 SetLastError(ERROR_NO_MORE_ITEMS);
3779 return FALSE;
3782 if (dwParam == PP_ENUMALGS) {
3783 if (pbData && (*pdwDataLen >= sizeof(PROV_ENUMALGS)))
3784 pKeyContainer->dwEnumAlgsCtr = ((dwFlags & CRYPT_FIRST) == CRYPT_FIRST) ?
3785 0 : pKeyContainer->dwEnumAlgsCtr+1;
3787 provEnumalgs.aiAlgid = aProvEnumAlgsEx
3788 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr].aiAlgid;
3789 provEnumalgs.dwBitLen = aProvEnumAlgsEx
3790 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr].dwDefaultLen;
3791 provEnumalgs.dwNameLen = aProvEnumAlgsEx
3792 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr].dwNameLen;
3793 memcpy(provEnumalgs.szName, aProvEnumAlgsEx
3794 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr].szName,
3795 20*sizeof(CHAR));
3797 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&provEnumalgs,
3798 sizeof(PROV_ENUMALGS));
3799 } else {
3800 if (pbData && (*pdwDataLen >= sizeof(PROV_ENUMALGS_EX)))
3801 pKeyContainer->dwEnumAlgsCtr = ((dwFlags & CRYPT_FIRST) == CRYPT_FIRST) ?
3802 0 : pKeyContainer->dwEnumAlgsCtr+1;
3804 return copy_param(pbData, pdwDataLen,
3805 (CONST BYTE*)&aProvEnumAlgsEx
3806 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr],
3807 sizeof(PROV_ENUMALGS_EX));
3810 case PP_CRYPT_COUNT_KEY_USE: /* Asked for by IE About dialog */
3811 return copy_param(pbData, pdwDataLen, abWTF, sizeof(abWTF));
3813 default:
3814 /* MSDN: Unknown parameter number in dwParam */
3815 SetLastError(NTE_BAD_TYPE);
3816 return FALSE;
3820 /******************************************************************************
3821 * CPDeriveKey (RSAENH.@)
3823 * Derives a key from a hash value.
3825 * PARAMS
3826 * hProv [I] Key container for which a key is to be generated.
3827 * Algid [I] Crypto algorithm identifier for the key to be generated.
3828 * hBaseData [I] Hash from whose value the key will be derived.
3829 * dwFlags [I] See Notes.
3830 * phKey [O] The generated key.
3832 * RETURNS
3833 * Success: TRUE
3834 * Failure: FALSE
3836 * NOTES
3837 * Defined flags:
3838 * - CRYPT_EXPORTABLE: Key can be exported.
3839 * - CRYPT_NO_SALT: No salt is used for 40 bit keys.
3840 * - CRYPT_CREATE_SALT: Use remaining bits as salt value.
3842 BOOL WINAPI RSAENH_CPDeriveKey(HCRYPTPROV hProv, ALG_ID Algid, HCRYPTHASH hBaseData,
3843 DWORD dwFlags, HCRYPTKEY *phKey)
3845 CRYPTKEY *pCryptKey, *pMasterKey;
3846 CRYPTHASH *pCryptHash;
3847 BYTE abHashValue[RSAENH_MAX_HASH_SIZE*2];
3848 DWORD dwLen;
3850 TRACE("(hProv=%08lx, Algid=%d, hBaseData=%08lx, dwFlags=%08x phKey=%p)\n", hProv, Algid,
3851 hBaseData, dwFlags, phKey);
3853 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
3855 SetLastError(NTE_BAD_UID);
3856 return FALSE;
3859 if (!lookup_handle(&handle_table, hBaseData, RSAENH_MAGIC_HASH,
3860 (OBJECTHDR**)&pCryptHash))
3862 SetLastError(NTE_BAD_HASH);
3863 return FALSE;
3866 if (!phKey)
3868 SetLastError(ERROR_INVALID_PARAMETER);
3869 return FALSE;
3872 switch (GET_ALG_CLASS(Algid))
3874 case ALG_CLASS_DATA_ENCRYPT:
3875 *phKey = new_key(hProv, Algid, dwFlags, &pCryptKey);
3876 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) return FALSE;
3879 * We derive the key material from the hash.
3880 * If the hash value is not large enough for the claimed key, we have to construct
3881 * a larger binary value based on the hash. This is documented in MSDN: CryptDeriveKey.
3883 dwLen = RSAENH_MAX_HASH_SIZE;
3884 RSAENH_CPGetHashParam(pCryptHash->hProv, hBaseData, HP_HASHVAL, abHashValue, &dwLen, 0);
3886 if (dwLen < pCryptKey->dwKeyLen) {
3887 BYTE pad1[RSAENH_HMAC_DEF_PAD_LEN], pad2[RSAENH_HMAC_DEF_PAD_LEN];
3888 BYTE old_hashval[RSAENH_MAX_HASH_SIZE];
3889 DWORD i;
3891 memcpy(old_hashval, pCryptHash->abHashValue, RSAENH_MAX_HASH_SIZE);
3893 for (i=0; i<RSAENH_HMAC_DEF_PAD_LEN; i++) {
3894 pad1[i] = RSAENH_HMAC_DEF_IPAD_CHAR ^ (i<dwLen ? abHashValue[i] : 0);
3895 pad2[i] = RSAENH_HMAC_DEF_OPAD_CHAR ^ (i<dwLen ? abHashValue[i] : 0);
3898 init_hash(pCryptHash);
3899 update_hash(pCryptHash, pad1, RSAENH_HMAC_DEF_PAD_LEN);
3900 finalize_hash(pCryptHash);
3901 memcpy(abHashValue, pCryptHash->abHashValue, pCryptHash->dwHashSize);
3903 init_hash(pCryptHash);
3904 update_hash(pCryptHash, pad2, RSAENH_HMAC_DEF_PAD_LEN);
3905 finalize_hash(pCryptHash);
3906 memcpy(abHashValue+pCryptHash->dwHashSize, pCryptHash->abHashValue,
3907 pCryptHash->dwHashSize);
3909 memcpy(pCryptHash->abHashValue, old_hashval, RSAENH_MAX_HASH_SIZE);
3912 memcpy(pCryptKey->abKeyValue, abHashValue,
3913 RSAENH_MIN(pCryptKey->dwKeyLen, sizeof(pCryptKey->abKeyValue)));
3914 break;
3916 case ALG_CLASS_MSG_ENCRYPT:
3917 if (!lookup_handle(&handle_table, pCryptHash->hKey, RSAENH_MAGIC_KEY,
3918 (OBJECTHDR**)&pMasterKey))
3920 SetLastError(NTE_FAIL); /* FIXME error code */
3921 return FALSE;
3924 switch (Algid)
3926 /* See RFC 2246, chapter 6.3 Key calculation */
3927 case CALG_SCHANNEL_ENC_KEY:
3928 if (!pMasterKey->siSChannelInfo.saEncAlg.Algid ||
3929 !pMasterKey->siSChannelInfo.saEncAlg.cBits)
3931 SetLastError(NTE_BAD_FLAGS);
3932 return FALSE;
3934 *phKey = new_key(hProv, pMasterKey->siSChannelInfo.saEncAlg.Algid,
3935 MAKELONG(LOWORD(dwFlags),pMasterKey->siSChannelInfo.saEncAlg.cBits),
3936 &pCryptKey);
3937 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) return FALSE;
3938 memcpy(pCryptKey->abKeyValue,
3939 pCryptHash->abHashValue + (
3940 2 * (pMasterKey->siSChannelInfo.saMACAlg.cBits / 8) +
3941 ((dwFlags & CRYPT_SERVER) ?
3942 (pMasterKey->siSChannelInfo.saEncAlg.cBits / 8) : 0)),
3943 pMasterKey->siSChannelInfo.saEncAlg.cBits / 8);
3944 memcpy(pCryptKey->abInitVector,
3945 pCryptHash->abHashValue + (
3946 2 * (pMasterKey->siSChannelInfo.saMACAlg.cBits / 8) +
3947 2 * (pMasterKey->siSChannelInfo.saEncAlg.cBits / 8) +
3948 ((dwFlags & CRYPT_SERVER) ? pCryptKey->dwBlockLen : 0)),
3949 pCryptKey->dwBlockLen);
3950 break;
3952 case CALG_SCHANNEL_MAC_KEY:
3953 *phKey = new_key(hProv, Algid,
3954 MAKELONG(LOWORD(dwFlags),pMasterKey->siSChannelInfo.saMACAlg.cBits),
3955 &pCryptKey);
3956 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) return FALSE;
3957 memcpy(pCryptKey->abKeyValue,
3958 pCryptHash->abHashValue + ((dwFlags & CRYPT_SERVER) ?
3959 pMasterKey->siSChannelInfo.saMACAlg.cBits / 8 : 0),
3960 pMasterKey->siSChannelInfo.saMACAlg.cBits / 8);
3961 break;
3963 default:
3964 SetLastError(NTE_BAD_ALGID);
3965 return FALSE;
3967 break;
3969 default:
3970 SetLastError(NTE_BAD_ALGID);
3971 return FALSE;
3974 setup_key(pCryptKey);
3975 return TRUE;
3978 /******************************************************************************
3979 * CPGetUserKey (RSAENH.@)
3981 * Returns a handle to the user's private key-exchange- or signature-key.
3983 * PARAMS
3984 * hProv [I] The key container from which a user key is requested.
3985 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
3986 * phUserKey [O] Handle to the requested key or INVALID_HANDLE_VALUE in case of failure.
3988 * RETURNS
3989 * Success: TRUE.
3990 * Failure: FALSE.
3992 * NOTE
3993 * A newly created key container does not contain private user key. Create them with CPGenKey.
3995 BOOL WINAPI RSAENH_CPGetUserKey(HCRYPTPROV hProv, DWORD dwKeySpec, HCRYPTKEY *phUserKey)
3997 KEYCONTAINER *pKeyContainer;
3999 TRACE("(hProv=%08lx, dwKeySpec=%08x, phUserKey=%p)\n", hProv, dwKeySpec, phUserKey);
4001 if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER,
4002 (OBJECTHDR**)&pKeyContainer))
4004 /* MSDN: hProv not containing valid context handle */
4005 SetLastError(NTE_BAD_UID);
4006 return FALSE;
4009 switch (dwKeySpec)
4011 case AT_KEYEXCHANGE:
4012 copy_handle(&handle_table, pKeyContainer->hKeyExchangeKeyPair, RSAENH_MAGIC_KEY,
4013 phUserKey);
4014 break;
4016 case AT_SIGNATURE:
4017 copy_handle(&handle_table, pKeyContainer->hSignatureKeyPair, RSAENH_MAGIC_KEY,
4018 phUserKey);
4019 break;
4021 default:
4022 *phUserKey = (HCRYPTKEY)INVALID_HANDLE_VALUE;
4025 if (*phUserKey == (HCRYPTKEY)INVALID_HANDLE_VALUE)
4027 /* MSDN: dwKeySpec parameter specifies nonexistent key */
4028 SetLastError(NTE_NO_KEY);
4029 return FALSE;
4032 return TRUE;
4035 /******************************************************************************
4036 * CPHashData (RSAENH.@)
4038 * Updates a hash object with the given data.
4040 * PARAMS
4041 * hProv [I] Key container to which the hash object belongs.
4042 * hHash [I] Hash object which is to be updated.
4043 * pbData [I] Pointer to data with which the hash object is to be updated.
4044 * dwDataLen [I] Length of the data.
4045 * dwFlags [I] Currently none defined.
4047 * RETURNS
4048 * Success: TRUE.
4049 * Failure: FALSE.
4051 * NOTES
4052 * The actual hash value is queried with CPGetHashParam, which will finalize
4053 * the hash. Updating a finalized hash will fail with a last error NTE_BAD_HASH_STATE.
4055 BOOL WINAPI RSAENH_CPHashData(HCRYPTPROV hProv, HCRYPTHASH hHash, CONST BYTE *pbData,
4056 DWORD dwDataLen, DWORD dwFlags)
4058 CRYPTHASH *pCryptHash;
4060 TRACE("(hProv=%08lx, hHash=%08lx, pbData=%p, dwDataLen=%d, dwFlags=%08x)\n",
4061 hProv, hHash, pbData, dwDataLen, dwFlags);
4063 if (dwFlags)
4065 SetLastError(NTE_BAD_FLAGS);
4066 return FALSE;
4069 if (!lookup_handle(&handle_table, hHash, RSAENH_MAGIC_HASH,
4070 (OBJECTHDR**)&pCryptHash))
4072 SetLastError(NTE_BAD_HASH);
4073 return FALSE;
4076 if (!get_algid_info(hProv, pCryptHash->aiAlgid) || pCryptHash->aiAlgid == CALG_SSL3_SHAMD5)
4078 SetLastError(NTE_BAD_ALGID);
4079 return FALSE;
4082 if (pCryptHash->dwState != RSAENH_HASHSTATE_HASHING)
4084 SetLastError(NTE_BAD_HASH_STATE);
4085 return FALSE;
4088 update_hash(pCryptHash, pbData, dwDataLen);
4089 return TRUE;
4092 /******************************************************************************
4093 * CPHashSessionKey (RSAENH.@)
4095 * Updates a hash object with the binary representation of a symmetric key.
4097 * PARAMS
4098 * hProv [I] Key container to which the hash object belongs.
4099 * hHash [I] Hash object which is to be updated.
4100 * hKey [I] The symmetric key, whose binary value will be added to the hash.
4101 * dwFlags [I] CRYPT_LITTLE_ENDIAN, if the binary key value shall be interpreted as little endian.
4103 * RETURNS
4104 * Success: TRUE.
4105 * Failure: FALSE.
4107 BOOL WINAPI RSAENH_CPHashSessionKey(HCRYPTPROV hProv, HCRYPTHASH hHash, HCRYPTKEY hKey,
4108 DWORD dwFlags)
4110 BYTE abKeyValue[RSAENH_MAX_KEY_SIZE], bTemp;
4111 CRYPTKEY *pKey;
4112 DWORD i;
4114 TRACE("(hProv=%08lx, hHash=%08lx, hKey=%08lx, dwFlags=%08x)\n", hProv, hHash, hKey, dwFlags);
4116 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pKey) ||
4117 (GET_ALG_CLASS(pKey->aiAlgid) != ALG_CLASS_DATA_ENCRYPT))
4119 SetLastError(NTE_BAD_KEY);
4120 return FALSE;
4123 if (dwFlags & ~CRYPT_LITTLE_ENDIAN) {
4124 SetLastError(NTE_BAD_FLAGS);
4125 return FALSE;
4128 memcpy(abKeyValue, pKey->abKeyValue, pKey->dwKeyLen);
4129 if (!(dwFlags & CRYPT_LITTLE_ENDIAN)) {
4130 for (i=0; i<pKey->dwKeyLen/2; i++) {
4131 bTemp = abKeyValue[i];
4132 abKeyValue[i] = abKeyValue[pKey->dwKeyLen-i-1];
4133 abKeyValue[pKey->dwKeyLen-i-1] = bTemp;
4137 return RSAENH_CPHashData(hProv, hHash, abKeyValue, pKey->dwKeyLen, 0);
4140 /******************************************************************************
4141 * CPReleaseContext (RSAENH.@)
4143 * Release a key container.
4145 * PARAMS
4146 * hProv [I] Key container to be released.
4147 * dwFlags [I] Currently none defined.
4149 * RETURNS
4150 * Success: TRUE
4151 * Failure: FALSE
4153 BOOL WINAPI RSAENH_CPReleaseContext(HCRYPTPROV hProv, DWORD dwFlags)
4155 TRACE("(hProv=%08lx, dwFlags=%08x)\n", hProv, dwFlags);
4157 if (!release_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
4159 /* MSDN: hProv not containing valid context handle */
4160 SetLastError(NTE_BAD_UID);
4161 return FALSE;
4164 if (dwFlags) {
4165 SetLastError(NTE_BAD_FLAGS);
4166 return FALSE;
4169 return TRUE;
4172 /******************************************************************************
4173 * CPSetHashParam (RSAENH.@)
4175 * Set a parameter of a hash object
4177 * PARAMS
4178 * hProv [I] The key container to which the key belongs.
4179 * hHash [I] The hash object for which a parameter is to be set.
4180 * dwParam [I] Parameter type. See Notes.
4181 * pbData [I] Pointer to the parameter value.
4182 * dwFlags [I] Currently none defined.
4184 * RETURNS
4185 * Success: TRUE.
4186 * Failure: FALSE.
4188 * NOTES
4189 * Currently only the HP_HMAC_INFO dwParam type is defined.
4190 * The HMAC_INFO struct will be deep copied into the hash object.
4191 * See Internet RFC 2104 for details on the HMAC algorithm.
4193 BOOL WINAPI RSAENH_CPSetHashParam(HCRYPTPROV hProv, HCRYPTHASH hHash, DWORD dwParam,
4194 BYTE *pbData, DWORD dwFlags)
4196 CRYPTHASH *pCryptHash;
4197 CRYPTKEY *pCryptKey;
4198 DWORD i;
4200 TRACE("(hProv=%08lx, hHash=%08lx, dwParam=%08x, pbData=%p, dwFlags=%08x)\n",
4201 hProv, hHash, dwParam, pbData, dwFlags);
4203 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
4205 SetLastError(NTE_BAD_UID);
4206 return FALSE;
4209 if (dwFlags) {
4210 SetLastError(NTE_BAD_FLAGS);
4211 return FALSE;
4214 if (!lookup_handle(&handle_table, hHash, RSAENH_MAGIC_HASH,
4215 (OBJECTHDR**)&pCryptHash))
4217 SetLastError(NTE_BAD_HASH);
4218 return FALSE;
4221 switch (dwParam) {
4222 case HP_HMAC_INFO:
4223 free_hmac_info(pCryptHash->pHMACInfo);
4224 if (!copy_hmac_info(&pCryptHash->pHMACInfo, (PHMAC_INFO)pbData)) return FALSE;
4226 if (!lookup_handle(&handle_table, pCryptHash->hKey, RSAENH_MAGIC_KEY,
4227 (OBJECTHDR**)&pCryptKey))
4229 SetLastError(NTE_FAIL); /* FIXME: correct error code? */
4230 return FALSE;
4233 if (pCryptKey->aiAlgid == CALG_HMAC && !pCryptKey->dwKeyLen) {
4234 HCRYPTHASH hKeyHash;
4235 DWORD keyLen;
4237 if (!RSAENH_CPCreateHash(hProv, ((PHMAC_INFO)pbData)->HashAlgid, 0, 0,
4238 &hKeyHash))
4239 return FALSE;
4240 if (!RSAENH_CPHashData(hProv, hKeyHash, pCryptKey->blobHmacKey.pbData,
4241 pCryptKey->blobHmacKey.cbData, 0))
4243 RSAENH_CPDestroyHash(hProv, hKeyHash);
4244 return FALSE;
4246 keyLen = sizeof(pCryptKey->abKeyValue);
4247 if (!RSAENH_CPGetHashParam(hProv, hKeyHash, HP_HASHVAL, pCryptKey->abKeyValue,
4248 &keyLen, 0))
4250 RSAENH_CPDestroyHash(hProv, hKeyHash);
4251 return FALSE;
4253 pCryptKey->dwKeyLen = keyLen;
4254 RSAENH_CPDestroyHash(hProv, hKeyHash);
4256 for (i=0; i<RSAENH_MIN(pCryptKey->dwKeyLen,pCryptHash->pHMACInfo->cbInnerString); i++) {
4257 pCryptHash->pHMACInfo->pbInnerString[i] ^= pCryptKey->abKeyValue[i];
4259 for (i=0; i<RSAENH_MIN(pCryptKey->dwKeyLen,pCryptHash->pHMACInfo->cbOuterString); i++) {
4260 pCryptHash->pHMACInfo->pbOuterString[i] ^= pCryptKey->abKeyValue[i];
4263 init_hash(pCryptHash);
4264 return TRUE;
4266 case HP_HASHVAL:
4267 memcpy(pCryptHash->abHashValue, pbData, pCryptHash->dwHashSize);
4268 pCryptHash->dwState = RSAENH_HASHSTATE_FINISHED;
4269 return TRUE;
4271 case HP_TLS1PRF_SEED:
4272 return copy_data_blob(&pCryptHash->tpPRFParams.blobSeed, (PCRYPT_DATA_BLOB)pbData);
4274 case HP_TLS1PRF_LABEL:
4275 return copy_data_blob(&pCryptHash->tpPRFParams.blobLabel, (PCRYPT_DATA_BLOB)pbData);
4277 default:
4278 SetLastError(NTE_BAD_TYPE);
4279 return FALSE;
4283 /******************************************************************************
4284 * CPSetProvParam (RSAENH.@)
4286 BOOL WINAPI RSAENH_CPSetProvParam(HCRYPTPROV hProv, DWORD dwParam, BYTE *pbData, DWORD dwFlags)
4288 FIXME("(stub)\n");
4289 return FALSE;
4292 /******************************************************************************
4293 * CPSignHash (RSAENH.@)
4295 * Sign a hash object
4297 * PARAMS
4298 * hProv [I] The key container, to which the hash object belongs.
4299 * hHash [I] The hash object to be signed.
4300 * dwKeySpec [I] AT_SIGNATURE or AT_KEYEXCHANGE: Key used to generate the signature.
4301 * sDescription [I] Should be NULL for security reasons.
4302 * dwFlags [I] 0, CRYPT_NOHASHOID or CRYPT_X931_FORMAT: Format of the signature.
4303 * pbSignature [O] Buffer, to which the signature will be stored. May be NULL to query SigLen.
4304 * pdwSigLen [I/O] Size of the buffer (in), Length of the signature (out)
4306 * RETURNS
4307 * Success: TRUE
4308 * Failure: FALSE
4310 BOOL WINAPI RSAENH_CPSignHash(HCRYPTPROV hProv, HCRYPTHASH hHash, DWORD dwKeySpec,
4311 LPCWSTR sDescription, DWORD dwFlags, BYTE *pbSignature,
4312 DWORD *pdwSigLen)
4314 HCRYPTKEY hCryptKey = (HCRYPTKEY)INVALID_HANDLE_VALUE;
4315 CRYPTKEY *pCryptKey;
4316 DWORD dwHashLen;
4317 BYTE abHashValue[RSAENH_MAX_HASH_SIZE];
4318 ALG_ID aiAlgid;
4319 BOOL ret = FALSE;
4321 TRACE("(hProv=%08lx, hHash=%08lx, dwKeySpec=%08x, sDescription=%s, dwFlags=%08x, "
4322 "pbSignature=%p, pdwSigLen=%p)\n", hProv, hHash, dwKeySpec, debugstr_w(sDescription),
4323 dwFlags, pbSignature, pdwSigLen);
4325 if (dwFlags & ~(CRYPT_NOHASHOID|CRYPT_X931_FORMAT)) {
4326 SetLastError(NTE_BAD_FLAGS);
4327 return FALSE;
4330 if (!RSAENH_CPGetUserKey(hProv, dwKeySpec, &hCryptKey)) return FALSE;
4332 if (!lookup_handle(&handle_table, hCryptKey, RSAENH_MAGIC_KEY,
4333 (OBJECTHDR**)&pCryptKey))
4335 SetLastError(NTE_NO_KEY);
4336 goto out;
4339 if (!pbSignature) {
4340 *pdwSigLen = pCryptKey->dwKeyLen;
4341 ret = TRUE;
4342 goto out;
4344 if (pCryptKey->dwKeyLen > *pdwSigLen)
4346 SetLastError(ERROR_MORE_DATA);
4347 *pdwSigLen = pCryptKey->dwKeyLen;
4348 goto out;
4350 *pdwSigLen = pCryptKey->dwKeyLen;
4352 if (sDescription) {
4353 if (!RSAENH_CPHashData(hProv, hHash, (CONST BYTE*)sDescription,
4354 (DWORD)lstrlenW(sDescription)*sizeof(WCHAR), 0))
4356 goto out;
4360 dwHashLen = sizeof(DWORD);
4361 if (!RSAENH_CPGetHashParam(hProv, hHash, HP_ALGID, (BYTE*)&aiAlgid, &dwHashLen, 0)) goto out;
4363 dwHashLen = RSAENH_MAX_HASH_SIZE;
4364 if (!RSAENH_CPGetHashParam(hProv, hHash, HP_HASHVAL, abHashValue, &dwHashLen, 0)) goto out;
4367 if (!build_hash_signature(pbSignature, *pdwSigLen, aiAlgid, abHashValue, dwHashLen, dwFlags)) {
4368 goto out;
4371 ret = encrypt_block_impl(pCryptKey->aiAlgid, PK_PRIVATE, &pCryptKey->context, pbSignature, pbSignature, RSAENH_ENCRYPT);
4372 out:
4373 RSAENH_CPDestroyKey(hProv, hCryptKey);
4374 return ret;
4377 /******************************************************************************
4378 * CPVerifySignature (RSAENH.@)
4380 * Verify the signature of a hash object.
4382 * PARAMS
4383 * hProv [I] The key container, to which the hash belongs.
4384 * hHash [I] The hash for which the signature is verified.
4385 * pbSignature [I] The binary signature.
4386 * dwSigLen [I] Length of the signature BLOB.
4387 * hPubKey [I] Public key used to verify the signature.
4388 * sDescription [I] Should be NULL for security reasons.
4389 * dwFlags [I] 0, CRYPT_NOHASHOID or CRYPT_X931_FORMAT: Format of the signature.
4391 * RETURNS
4392 * Success: TRUE (Signature is valid)
4393 * Failure: FALSE (GetLastError() == NTE_BAD_SIGNATURE, if signature is invalid)
4395 BOOL WINAPI RSAENH_CPVerifySignature(HCRYPTPROV hProv, HCRYPTHASH hHash, CONST BYTE *pbSignature,
4396 DWORD dwSigLen, HCRYPTKEY hPubKey, LPCWSTR sDescription,
4397 DWORD dwFlags)
4399 BYTE *pbConstructed = NULL, *pbDecrypted = NULL;
4400 CRYPTKEY *pCryptKey;
4401 DWORD dwHashLen;
4402 ALG_ID aiAlgid;
4403 BYTE abHashValue[RSAENH_MAX_HASH_SIZE];
4404 BOOL res = FALSE;
4406 TRACE("(hProv=%08lx, hHash=%08lx, pbSignature=%p, dwSigLen=%d, hPubKey=%08lx, sDescription=%s, "
4407 "dwFlags=%08x)\n", hProv, hHash, pbSignature, dwSigLen, hPubKey, debugstr_w(sDescription),
4408 dwFlags);
4410 if (dwFlags & ~(CRYPT_NOHASHOID|CRYPT_X931_FORMAT)) {
4411 SetLastError(NTE_BAD_FLAGS);
4412 return FALSE;
4415 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
4417 SetLastError(NTE_BAD_UID);
4418 return FALSE;
4421 if (!lookup_handle(&handle_table, hPubKey, RSAENH_MAGIC_KEY,
4422 (OBJECTHDR**)&pCryptKey))
4424 SetLastError(NTE_BAD_KEY);
4425 return FALSE;
4428 /* in Microsoft implementation, the signature length is checked before
4429 * the signature pointer.
4431 if (dwSigLen != pCryptKey->dwKeyLen)
4433 SetLastError(NTE_BAD_SIGNATURE);
4434 return FALSE;
4437 if (!hHash || !pbSignature)
4439 SetLastError(ERROR_INVALID_PARAMETER);
4440 return FALSE;
4443 if (sDescription) {
4444 if (!RSAENH_CPHashData(hProv, hHash, (CONST BYTE*)sDescription,
4445 (DWORD)lstrlenW(sDescription)*sizeof(WCHAR), 0))
4447 return FALSE;
4451 dwHashLen = sizeof(DWORD);
4452 if (!RSAENH_CPGetHashParam(hProv, hHash, HP_ALGID, (BYTE*)&aiAlgid, &dwHashLen, 0)) return FALSE;
4454 dwHashLen = RSAENH_MAX_HASH_SIZE;
4455 if (!RSAENH_CPGetHashParam(hProv, hHash, HP_HASHVAL, abHashValue, &dwHashLen, 0)) return FALSE;
4457 pbConstructed = HeapAlloc(GetProcessHeap(), 0, dwSigLen);
4458 if (!pbConstructed) {
4459 SetLastError(NTE_NO_MEMORY);
4460 goto cleanup;
4463 pbDecrypted = HeapAlloc(GetProcessHeap(), 0, dwSigLen);
4464 if (!pbDecrypted) {
4465 SetLastError(NTE_NO_MEMORY);
4466 goto cleanup;
4469 if (!encrypt_block_impl(pCryptKey->aiAlgid, PK_PUBLIC, &pCryptKey->context, pbSignature, pbDecrypted,
4470 RSAENH_DECRYPT))
4472 goto cleanup;
4475 if (build_hash_signature(pbConstructed, dwSigLen, aiAlgid, abHashValue, dwHashLen, dwFlags) &&
4476 !memcmp(pbDecrypted, pbConstructed, dwSigLen)) {
4477 res = TRUE;
4478 goto cleanup;
4481 if (!(dwFlags & CRYPT_NOHASHOID) &&
4482 build_hash_signature(pbConstructed, dwSigLen, aiAlgid, abHashValue, dwHashLen, dwFlags|CRYPT_NOHASHOID) &&
4483 !memcmp(pbDecrypted, pbConstructed, dwSigLen)) {
4484 res = TRUE;
4485 goto cleanup;
4488 SetLastError(NTE_BAD_SIGNATURE);
4490 cleanup:
4491 HeapFree(GetProcessHeap(), 0, pbConstructed);
4492 HeapFree(GetProcessHeap(), 0, pbDecrypted);
4493 return res;
4496 static const WCHAR szProviderKeys[6][116] = {
4497 { 'S','o','f','t','w','a','r','e','\\',
4498 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
4499 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
4500 'i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ','B','a','s',
4501 'e',' ','C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r',
4502 'o','v','i','d','e','r',' ','v','1','.','0',0 },
4503 { 'S','o','f','t','w','a','r','e','\\',
4504 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
4505 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
4506 'i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ',
4507 'E','n','h','a','n','c','e','d',
4508 ' ','C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r',
4509 'o','v','i','d','e','r',' ','v','1','.','0',0 },
4510 { 'S','o','f','t','w','a','r','e','\\',
4511 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
4512 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
4513 'i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ','S','t','r','o','n','g',
4514 ' ','C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r',
4515 'o','v','i','d','e','r',0 },
4516 { 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
4517 'C','r','y','p','t','o','g','r','a','p','h','y','\\','D','e','f','a','u','l','t','s','\\',
4518 'P','r','o','v','i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ',
4519 'R','S','A',' ','S','C','h','a','n','n','e','l',' ',
4520 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r','o','v','i','d','e','r',0 },
4521 { 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
4522 'C','r','y','p','t','o','g','r','a','p','h','y','\\','D','e','f','a','u','l','t','s','\\',
4523 'P','r','o','v','i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ',
4524 'E','n','h','a','n','c','e','d',' ','R','S','A',' ','a','n','d',' ','A','E','S',' ',
4525 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r','o','v','i','d','e','r',0 },
4526 { 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
4527 'C','r','y','p','t','o','g','r','a','p','h','y','\\','D','e','f','a','u','l','t','s','\\',
4528 'P','r','o','v','i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ',
4529 'E','n','h','a','n','c','e','d',' ','R','S','A',' ','a','n','d',' ','A','E','S',' ',
4530 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r','o','v','i','d','e','r',
4531 ' ','(','P','r','o','t','o','t','y','p','e',')',0 }
4533 static const WCHAR szDefaultKeys[3][65] = {
4534 { 'S','o','f','t','w','a','r','e','\\',
4535 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
4536 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
4537 'i','d','e','r',' ','T','y','p','e','s','\\','T','y','p','e',' ','0','0','1',0 },
4538 { 'S','o','f','t','w','a','r','e','\\',
4539 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
4540 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
4541 'i','d','e','r',' ','T','y','p','e','s','\\','T','y','p','e',' ','0','1','2',0 },
4542 { 'S','o','f','t','w','a','r','e','\\',
4543 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
4544 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
4545 'i','d','e','r',' ','T','y','p','e','s','\\','T','y','p','e',' ','0','2','4',0 }
4549 /******************************************************************************
4550 * DllRegisterServer (RSAENH.@)
4552 HRESULT WINAPI DllRegisterServer(void)
4554 return __wine_register_resources( instance );
4557 /******************************************************************************
4558 * DllUnregisterServer (RSAENH.@)
4560 HRESULT WINAPI DllUnregisterServer(void)
4562 return __wine_unregister_resources( instance );