msi: Clean trailing and leading spaces from path segments.
[wine/multimedia.git] / dlls / rsaenh / rsaenh.c
blob2e9b70158802773880b2efa4399c7108b045d646
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
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "config.h"
25 #include "wine/port.h"
26 #include "wine/library.h"
27 #include "wine/debug.h"
29 #include <stdarg.h>
30 #include <stdio.h>
32 #include "windef.h"
33 #include "winbase.h"
34 #include "winreg.h"
35 #include "wincrypt.h"
36 #include "lmcons.h"
37 #include "handle.h"
38 #include "implglue.h"
39 #include "objbase.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(crypt);
43 /******************************************************************************
44 * CRYPTHASH - hash objects
46 #define RSAENH_MAGIC_HASH 0x85938417u
47 #define RSAENH_MAX_HASH_SIZE 104
48 #define RSAENH_HASHSTATE_IDLE 0
49 #define RSAENH_HASHSTATE_HASHING 1
50 #define RSAENH_HASHSTATE_FINISHED 2
51 typedef struct _RSAENH_TLS1PRF_PARAMS
53 CRYPT_DATA_BLOB blobLabel;
54 CRYPT_DATA_BLOB blobSeed;
55 } RSAENH_TLS1PRF_PARAMS;
57 typedef struct tagCRYPTHASH
59 OBJECTHDR header;
60 ALG_ID aiAlgid;
61 HCRYPTKEY hKey;
62 HCRYPTPROV hProv;
63 DWORD dwHashSize;
64 DWORD dwState;
65 HASH_CONTEXT context;
66 BYTE abHashValue[RSAENH_MAX_HASH_SIZE];
67 PHMAC_INFO pHMACInfo;
68 RSAENH_TLS1PRF_PARAMS tpPRFParams;
69 } CRYPTHASH;
71 /******************************************************************************
72 * CRYPTKEY - key objects
74 #define RSAENH_MAGIC_KEY 0x73620457u
75 #define RSAENH_MAX_KEY_SIZE 48
76 #define RSAENH_MAX_BLOCK_SIZE 24
77 #define RSAENH_KEYSTATE_IDLE 0
78 #define RSAENH_KEYSTATE_ENCRYPTING 1
79 #define RSAENH_KEYSTATE_DECRYPTING 2
80 #define RSAENH_KEYSTATE_MASTERKEY 3
81 typedef struct _RSAENH_SCHANNEL_INFO
83 SCHANNEL_ALG saEncAlg;
84 SCHANNEL_ALG saMACAlg;
85 CRYPT_DATA_BLOB blobClientRandom;
86 CRYPT_DATA_BLOB blobServerRandom;
87 } RSAENH_SCHANNEL_INFO;
89 typedef struct tagCRYPTKEY
91 OBJECTHDR header;
92 ALG_ID aiAlgid;
93 HCRYPTPROV hProv;
94 DWORD dwMode;
95 DWORD dwModeBits;
96 DWORD dwPermissions;
97 DWORD dwKeyLen;
98 DWORD dwSaltLen;
99 DWORD dwBlockLen;
100 DWORD dwState;
101 KEY_CONTEXT context;
102 BYTE abKeyValue[RSAENH_MAX_KEY_SIZE];
103 BYTE abInitVector[RSAENH_MAX_BLOCK_SIZE];
104 BYTE abChainVector[RSAENH_MAX_BLOCK_SIZE];
105 RSAENH_SCHANNEL_INFO siSChannelInfo;
106 } CRYPTKEY;
108 /******************************************************************************
109 * KEYCONTAINER - key containers
111 #define RSAENH_PERSONALITY_BASE 0u
112 #define RSAENH_PERSONALITY_STRONG 1u
113 #define RSAENH_PERSONALITY_ENHANCED 2u
114 #define RSAENH_PERSONALITY_SCHANNEL 3u
116 #define RSAENH_MAGIC_CONTAINER 0x26384993u
117 typedef struct tagKEYCONTAINER
119 OBJECTHDR header;
120 DWORD dwFlags;
121 DWORD dwPersonality;
122 DWORD dwEnumAlgsCtr;
123 DWORD dwEnumContainersCtr;
124 CHAR szName[MAX_PATH];
125 CHAR szProvName[MAX_PATH];
126 HCRYPTKEY hKeyExchangeKeyPair;
127 HCRYPTKEY hSignatureKeyPair;
128 } KEYCONTAINER;
130 /******************************************************************************
131 * Some magic constants
133 #define RSAENH_ENCRYPT 1
134 #define RSAENH_DECRYPT 0
135 #define RSAENH_HMAC_DEF_IPAD_CHAR 0x36
136 #define RSAENH_HMAC_DEF_OPAD_CHAR 0x5c
137 #define RSAENH_HMAC_DEF_PAD_LEN 64
138 #define RSAENH_DES_EFFECTIVE_KEYLEN 56
139 #define RSAENH_DES_STORAGE_KEYLEN 64
140 #define RSAENH_3DES112_EFFECTIVE_KEYLEN 112
141 #define RSAENH_3DES112_STORAGE_KEYLEN 128
142 #define RSAENH_3DES_EFFECTIVE_KEYLEN 168
143 #define RSAENH_3DES_STORAGE_KEYLEN 192
144 #define RSAENH_MAGIC_RSA2 0x32415352
145 #define RSAENH_MAGIC_RSA1 0x31415352
146 #define RSAENH_PKC_BLOCKTYPE 0x02
147 #define RSAENH_SSL3_VERSION_MAJOR 3
148 #define RSAENH_SSL3_VERSION_MINOR 0
149 #define RSAENH_TLS1_VERSION_MAJOR 3
150 #define RSAENH_TLS1_VERSION_MINOR 1
151 #define RSAENH_REGKEY "Software\\Wine\\Crypto\\RSA\\%s"
153 #define RSAENH_MIN(a,b) ((a)<(b)?(a):(b))
154 /******************************************************************************
155 * aProvEnumAlgsEx - Defines the capabilities of the CSP personalities.
157 #define RSAENH_MAX_ENUMALGS 20
158 #define RSAENH_PCT1_SSL2_SSL3_TLS1 (CRYPT_FLAG_PCT1|CRYPT_FLAG_SSL2|CRYPT_FLAG_SSL3|CRYPT_FLAG_TLS1)
159 PROV_ENUMALGS_EX aProvEnumAlgsEx[4][RSAENH_MAX_ENUMALGS+1] =
162 {CALG_RC2, 40, 40, 56,0, 4,"RC2", 24,"RSA Data Security's RC2"},
163 {CALG_RC4, 40, 40, 56,0, 4,"RC4", 24,"RSA Data Security's RC4"},
164 {CALG_DES, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
165 {CALG_SHA, 160,160, 160,CRYPT_FLAG_SIGNING, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
166 {CALG_MD2, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD2", 23,"Message Digest 2 (MD2)"},
167 {CALG_MD4, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD4", 23,"Message Digest 4 (MD4)"},
168 {CALG_MD5, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD5", 23,"Message Digest 5 (MD5)"},
169 {CALG_SSL3_SHAMD5,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
170 {CALG_MAC, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
171 {CALG_RSA_SIGN, 512,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_SIGN",14,"RSA Signature"},
172 {CALG_RSA_KEYX, 512,384, 1024,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_KEYX",17,"RSA Key Exchange"},
173 {CALG_HMAC, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
174 {0, 0, 0, 0,0, 1,"", 1,""}
177 {CALG_RC2, 128, 40, 128,0, 4,"RC2", 24,"RSA Data Security's RC2"},
178 {CALG_RC4, 128, 40, 128,0, 4,"RC4", 24,"RSA Data Security's RC4"},
179 {CALG_DES, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
180 {CALG_3DES_112, 112,112, 112,0, 13,"3DES TWO KEY",19,"Two Key Triple DES"},
181 {CALG_3DES, 168,168, 168,0, 5,"3DES", 21,"Three Key Triple DES"},
182 {CALG_SHA, 160,160, 160,CRYPT_FLAG_SIGNING, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
183 {CALG_MD2, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD2", 23,"Message Digest 2 (MD2)"},
184 {CALG_MD4, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD4", 23,"Message Digest 4 (MD4)"},
185 {CALG_MD5, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD5", 23,"Message Digest 5 (MD5)"},
186 {CALG_SSL3_SHAMD5,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
187 {CALG_MAC, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
188 {CALG_RSA_SIGN,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_SIGN",14,"RSA Signature"},
189 {CALG_RSA_KEYX,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_KEYX",17,"RSA Key Exchange"},
190 {CALG_HMAC, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
191 {0, 0, 0, 0,0, 1,"", 1,""}
194 {CALG_RC2, 128, 40, 128,0, 4,"RC2", 24,"RSA Data Security's RC2"},
195 {CALG_RC4, 128, 40, 128,0, 4,"RC4", 24,"RSA Data Security's RC4"},
196 {CALG_DES, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
197 {CALG_3DES_112, 112,112, 112,0, 13,"3DES TWO KEY",19,"Two Key Triple DES"},
198 {CALG_3DES, 168,168, 168,0, 5,"3DES", 21,"Three Key Triple DES"},
199 {CALG_SHA, 160,160, 160,CRYPT_FLAG_SIGNING, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
200 {CALG_MD2, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD2", 23,"Message Digest 2 (MD2)"},
201 {CALG_MD4, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD4", 23,"Message Digest 4 (MD4)"},
202 {CALG_MD5, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD5", 23,"Message Digest 5 (MD5)"},
203 {CALG_SSL3_SHAMD5,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
204 {CALG_MAC, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
205 {CALG_RSA_SIGN,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_SIGN",14,"RSA Signature"},
206 {CALG_RSA_KEYX,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_KEYX",17,"RSA Key Exchange"},
207 {CALG_HMAC, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
208 {0, 0, 0, 0,0, 1,"", 1,""}
211 {CALG_RC2, 128, 40, 128,RSAENH_PCT1_SSL2_SSL3_TLS1, 4,"RC2", 24,"RSA Data Security's RC2"},
212 {CALG_RC4, 128, 40, 128,RSAENH_PCT1_SSL2_SSL3_TLS1, 4,"RC4", 24,"RSA Data Security's RC4"},
213 {CALG_DES, 56, 56, 56,RSAENH_PCT1_SSL2_SSL3_TLS1, 4,"DES", 31,"Data Encryption Standard (DES)"},
214 {CALG_3DES_112, 112,112, 112,RSAENH_PCT1_SSL2_SSL3_TLS1,13,"3DES TWO KEY",19,"Two Key Triple DES"},
215 {CALG_3DES, 168,168, 168,RSAENH_PCT1_SSL2_SSL3_TLS1, 5,"3DES", 21,"Three Key Triple DES"},
216 {CALG_SHA,160,160,160,CRYPT_FLAG_SIGNING|RSAENH_PCT1_SSL2_SSL3_TLS1,6,"SHA-1",30,"Secure Hash Algorithm (SHA-1)"},
217 {CALG_MD5,128,128,128,CRYPT_FLAG_SIGNING|RSAENH_PCT1_SSL2_SSL3_TLS1,4,"MD5",23,"Message Digest 5 (MD5)"},
218 {CALG_SSL3_SHAMD5,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
219 {CALG_MAC, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
220 {CALG_RSA_SIGN,1024,384,16384,CRYPT_FLAG_SIGNING|RSAENH_PCT1_SSL2_SSL3_TLS1,9,"RSA_SIGN",14,"RSA Signature"},
221 {CALG_RSA_KEYX,1024,384,16384,CRYPT_FLAG_SIGNING|RSAENH_PCT1_SSL2_SSL3_TLS1,9,"RSA_KEYX",17,"RSA Key Exchange"},
222 {CALG_HMAC, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
223 {CALG_PCT1_MASTER,128,128,128,CRYPT_FLAG_PCT1, 12,"PCT1 MASTER",12,"PCT1 Master"},
224 {CALG_SSL2_MASTER,40,40, 192,CRYPT_FLAG_SSL2, 12,"SSL2 MASTER",12,"SSL2 Master"},
225 {CALG_SSL3_MASTER,384,384,384,CRYPT_FLAG_SSL3, 12,"SSL3 MASTER",12,"SSL3 Master"},
226 {CALG_TLS1_MASTER,384,384,384,CRYPT_FLAG_TLS1, 12,"TLS1 MASTER",12,"TLS1 Master"},
227 {CALG_SCHANNEL_MASTER_HASH,0,0,-1,0, 16,"SCH MASTER HASH",21,"SChannel Master Hash"},
228 {CALG_SCHANNEL_MAC_KEY,0,0,-1,0, 12,"SCH MAC KEY",17,"SChannel MAC Key"},
229 {CALG_SCHANNEL_ENC_KEY,0,0,-1,0, 12,"SCH ENC KEY",24,"SChannel Encryption Key"},
230 {CALG_TLS1PRF, 0, 0, -1,0, 9,"TLS1 PRF", 28,"TLS1 Pseudo Random Function"},
231 {0, 0, 0, 0,0, 1,"", 1,""}
235 /******************************************************************************
236 * API forward declarations
238 BOOL WINAPI
239 RSAENH_CPGetKeyParam(
240 HCRYPTPROV hProv,
241 HCRYPTKEY hKey,
242 DWORD dwParam,
243 BYTE *pbData,
244 DWORD *pdwDataLen,
245 DWORD dwFlags
248 BOOL WINAPI
249 RSAENH_CPEncrypt(
250 HCRYPTPROV hProv,
251 HCRYPTKEY hKey,
252 HCRYPTHASH hHash,
253 BOOL Final,
254 DWORD dwFlags,
255 BYTE *pbData,
256 DWORD *pdwDataLen,
257 DWORD dwBufLen
260 BOOL WINAPI
261 RSAENH_CPCreateHash(
262 HCRYPTPROV hProv,
263 ALG_ID Algid,
264 HCRYPTKEY hKey,
265 DWORD dwFlags,
266 HCRYPTHASH *phHash
269 BOOL WINAPI
270 RSAENH_CPSetHashParam(
271 HCRYPTPROV hProv,
272 HCRYPTHASH hHash,
273 DWORD dwParam,
274 BYTE *pbData, DWORD dwFlags
277 BOOL WINAPI
278 RSAENH_CPGetHashParam(
279 HCRYPTPROV hProv,
280 HCRYPTHASH hHash,
281 DWORD dwParam,
282 BYTE *pbData,
283 DWORD *pdwDataLen,
284 DWORD dwFlags
287 BOOL WINAPI
288 RSAENH_CPDestroyHash(
289 HCRYPTPROV hProv,
290 HCRYPTHASH hHash
293 BOOL WINAPI
294 RSAENH_CPExportKey(
295 HCRYPTPROV hProv,
296 HCRYPTKEY hKey,
297 HCRYPTKEY hPubKey,
298 DWORD dwBlobType,
299 DWORD dwFlags,
300 BYTE *pbData,
301 DWORD *pdwDataLen
304 BOOL WINAPI
305 RSAENH_CPImportKey(
306 HCRYPTPROV hProv,
307 CONST BYTE *pbData,
308 DWORD dwDataLen,
309 HCRYPTKEY hPubKey,
310 DWORD dwFlags,
311 HCRYPTKEY *phKey
314 BOOL WINAPI
315 RSAENH_CPHashData(
316 HCRYPTPROV hProv,
317 HCRYPTHASH hHash,
318 CONST BYTE *pbData,
319 DWORD dwDataLen,
320 DWORD dwFlags
323 /******************************************************************************
324 * CSP's handle table (used by all acquired key containers)
326 static HANDLETABLE handle_table;
328 /******************************************************************************
329 * DllMain (RSAENH.@)
331 * Initializes and destroys the handle table for the CSP's handles.
333 int WINAPI DllMain(HINSTANCE hInstance, DWORD fdwReason, PVOID pvReserved)
335 switch (fdwReason)
337 case DLL_PROCESS_ATTACH:
338 init_handle_table(&handle_table);
339 break;
341 case DLL_PROCESS_DETACH:
342 destroy_handle_table(&handle_table);
343 break;
345 return 1;
348 /******************************************************************************
349 * copy_param [Internal]
351 * Helper function that supports the standard WINAPI protocol for querying data
352 * of dynamic size.
354 * PARAMS
355 * pbBuffer [O] Buffer where the queried parameter is copied to, if it is large enough.
356 * May be NUL if the required buffer size is to be queried only.
357 * pdwBufferSize [I/O] In: Size of the buffer at pbBuffer
358 * Out: Size of parameter pbParam
359 * pbParam [I] Parameter value.
360 * dwParamSize [I] Size of pbParam
362 * RETURN
363 * Success: TRUE (pbParam was copied into pbBuffer or pbBuffer is NULL)
364 * Failure: FALSE (pbBuffer is not large enough to hold pbParam). Last error: ERROR_MORE_DATA
366 static inline BOOL copy_param(
367 BYTE *pbBuffer, DWORD *pdwBufferSize, CONST BYTE *pbParam, DWORD dwParamSize)
369 if (pbBuffer)
371 if (dwParamSize > *pdwBufferSize)
373 SetLastError(ERROR_MORE_DATA);
374 *pdwBufferSize = dwParamSize;
375 return FALSE;
377 memcpy(pbBuffer, pbParam, dwParamSize);
379 *pdwBufferSize = dwParamSize;
380 return TRUE;
383 /******************************************************************************
384 * get_algid_info [Internal]
386 * Query CSP capabilities for a given crypto algorithm.
388 * PARAMS
389 * hProv [I] Handle to a key container of the CSP whose capabilities are to be queried.
390 * algid [I] Identifier of the crypto algorithm about which information is requested.
392 * RETURNS
393 * Success: Pointer to a PROV_ENUMALGS_EX struct containing information about the crypto algorithm.
394 * Failure: NULL (algid not supported)
396 static inline const PROV_ENUMALGS_EX* get_algid_info(HCRYPTPROV hProv, ALG_ID algid) {
397 PROV_ENUMALGS_EX *iterator;
398 KEYCONTAINER *pKeyContainer;
400 if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER, (OBJECTHDR**)&pKeyContainer)) {
401 SetLastError(NTE_BAD_UID);
402 return NULL;
405 for (iterator = aProvEnumAlgsEx[pKeyContainer->dwPersonality]; iterator->aiAlgid; iterator++) {
406 if (iterator->aiAlgid == algid) return iterator;
409 SetLastError(NTE_BAD_ALGID);
410 return NULL;
413 /******************************************************************************
414 * copy_data_blob [Internal]
416 * deeply copies a DATA_BLOB
418 * PARAMS
419 * dst [O] That's where the blob will be copied to
420 * src [I] Source blob
422 * RETURNS
423 * Success: TRUE
424 * Failure: FALSE (GetLastError() == NTE_NO_MEMORY
426 * NOTES
427 * Use free_data_blob to release resources occupied by copy_data_blob.
429 static inline BOOL copy_data_blob(PCRYPT_DATA_BLOB dst, CONST PCRYPT_DATA_BLOB src) {
430 dst->pbData = HeapAlloc(GetProcessHeap(), 0, src->cbData);
431 if (!dst->pbData) {
432 SetLastError(NTE_NO_MEMORY);
433 return FALSE;
435 dst->cbData = src->cbData;
436 memcpy(dst->pbData, src->pbData, src->cbData);
437 return TRUE;
440 /******************************************************************************
441 * concat_data_blobs [Internal]
443 * Concatenates two blobs
445 * PARAMS
446 * dst [O] The new blob will be copied here
447 * src1 [I] Prefix blob
448 * src2 [I] Appendix blob
450 * RETURNS
451 * Success: TRUE
452 * Failure: FALSE (GetLastError() == NTE_NO_MEMORY)
454 * NOTES
455 * Release resources occupied by concat_data_blobs with free_data_blobs
457 static inline BOOL concat_data_blobs(PCRYPT_DATA_BLOB dst, CONST PCRYPT_DATA_BLOB src1,
458 CONST PCRYPT_DATA_BLOB src2)
460 dst->cbData = src1->cbData + src2->cbData;
461 dst->pbData = HeapAlloc(GetProcessHeap(), 0, dst->cbData);
462 if (!dst->pbData) {
463 SetLastError(NTE_NO_MEMORY);
464 return FALSE;
466 memcpy(dst->pbData, src1->pbData, src1->cbData);
467 memcpy(dst->pbData + src1->cbData, src2->pbData, src2->cbData);
468 return TRUE;
471 /******************************************************************************
472 * free_data_blob [Internal]
474 * releases resource occupied by a dynamically allocated CRYPT_DATA_BLOB
476 * PARAMS
477 * pBlob [I] Heap space occupied by pBlob->pbData is released
479 static inline void free_data_blob(PCRYPT_DATA_BLOB pBlob) {
480 HeapFree(GetProcessHeap(), 0, pBlob->pbData);
483 /******************************************************************************
484 * init_data_blob [Internal]
486 static inline void init_data_blob(PCRYPT_DATA_BLOB pBlob) {
487 pBlob->pbData = NULL;
488 pBlob->cbData = 0;
491 /******************************************************************************
492 * free_hmac_info [Internal]
494 * Deeply free an HMAC_INFO struct.
496 * PARAMS
497 * hmac_info [I] Pointer to the HMAC_INFO struct to be freed.
499 * NOTES
500 * See Internet RFC 2104 for details on the HMAC algorithm.
502 static inline void free_hmac_info(PHMAC_INFO hmac_info) {
503 if (!hmac_info) return;
504 HeapFree(GetProcessHeap(), 0, hmac_info->pbInnerString);
505 HeapFree(GetProcessHeap(), 0, hmac_info->pbOuterString);
506 HeapFree(GetProcessHeap(), 0, hmac_info);
509 /******************************************************************************
510 * copy_hmac_info [Internal]
512 * Deeply copy an HMAC_INFO struct
514 * PARAMS
515 * dst [O] Pointer to a location where the pointer to the HMAC_INFO copy will be stored.
516 * src [I] Pointer to the HMAC_INFO struct to be copied.
518 * RETURNS
519 * Success: TRUE
520 * Failure: FALSE
522 * NOTES
523 * See Internet RFC 2104 for details on the HMAC algorithm.
525 static BOOL copy_hmac_info(PHMAC_INFO *dst, PHMAC_INFO src) {
526 if (!src) return FALSE;
527 *dst = HeapAlloc(GetProcessHeap(), 0, sizeof(HMAC_INFO));
528 if (!*dst) return FALSE;
529 memcpy(*dst, src, sizeof(HMAC_INFO));
530 (*dst)->pbInnerString = NULL;
531 (*dst)->pbOuterString = NULL;
532 if ((*dst)->cbInnerString == 0) (*dst)->cbInnerString = RSAENH_HMAC_DEF_PAD_LEN;
533 (*dst)->pbInnerString = HeapAlloc(GetProcessHeap(), 0, (*dst)->cbInnerString);
534 if (!(*dst)->pbInnerString) {
535 free_hmac_info(*dst);
536 return FALSE;
538 if (src->cbInnerString)
539 memcpy((*dst)->pbInnerString, src->pbInnerString, src->cbInnerString);
540 else
541 memset((*dst)->pbInnerString, RSAENH_HMAC_DEF_IPAD_CHAR, RSAENH_HMAC_DEF_PAD_LEN);
542 if ((*dst)->cbOuterString == 0) (*dst)->cbOuterString = RSAENH_HMAC_DEF_PAD_LEN;
543 (*dst)->pbOuterString = HeapAlloc(GetProcessHeap(), 0, (*dst)->cbOuterString);
544 if (!(*dst)->pbOuterString) {
545 free_hmac_info(*dst);
546 return FALSE;
548 if (src->cbOuterString)
549 memcpy((*dst)->pbOuterString, src->pbOuterString, src->cbOuterString);
550 else
551 memset((*dst)->pbOuterString, RSAENH_HMAC_DEF_OPAD_CHAR, RSAENH_HMAC_DEF_PAD_LEN);
552 return TRUE;
555 /******************************************************************************
556 * destroy_hash [Internal]
558 * Destructor for hash objects
560 * PARAMS
561 * pCryptHash [I] Pointer to the hash object to be destroyed.
562 * Will be invalid after function returns!
564 static void destroy_hash(OBJECTHDR *pObject)
566 CRYPTHASH *pCryptHash = (CRYPTHASH*)pObject;
568 free_hmac_info(pCryptHash->pHMACInfo);
569 free_data_blob(&pCryptHash->tpPRFParams.blobLabel);
570 free_data_blob(&pCryptHash->tpPRFParams.blobSeed);
571 HeapFree(GetProcessHeap(), 0, pCryptHash);
574 /******************************************************************************
575 * init_hash [Internal]
577 * Initialize (or reset) a hash object
579 * PARAMS
580 * pCryptHash [I] The hash object to be initialized.
582 static inline BOOL init_hash(CRYPTHASH *pCryptHash) {
583 DWORD dwLen;
585 switch (pCryptHash->aiAlgid)
587 case CALG_HMAC:
588 if (pCryptHash->pHMACInfo) {
589 const PROV_ENUMALGS_EX *pAlgInfo;
591 pAlgInfo = get_algid_info(pCryptHash->hProv, pCryptHash->pHMACInfo->HashAlgid);
592 if (!pAlgInfo) return FALSE;
593 pCryptHash->dwHashSize = pAlgInfo->dwDefaultLen >> 3;
594 init_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context);
595 update_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
596 pCryptHash->pHMACInfo->pbInnerString,
597 pCryptHash->pHMACInfo->cbInnerString);
599 return TRUE;
601 case CALG_MAC:
602 dwLen = sizeof(DWORD);
603 RSAENH_CPGetKeyParam(pCryptHash->hProv, pCryptHash->hKey, KP_BLOCKLEN,
604 (BYTE*)&pCryptHash->dwHashSize, &dwLen, 0);
605 pCryptHash->dwHashSize >>= 3;
606 return TRUE;
608 default:
609 return init_hash_impl(pCryptHash->aiAlgid, &pCryptHash->context);
613 /******************************************************************************
614 * update_hash [Internal]
616 * Hashes the given data and updates the hash object's state accordingly
618 * PARAMS
619 * pCryptHash [I] Hash object to be updated.
620 * pbData [I] Pointer to data stream to be hashed.
621 * dwDataLen [I] Length of data stream.
623 static inline void update_hash(CRYPTHASH *pCryptHash, CONST BYTE *pbData, DWORD dwDataLen) {
624 BYTE *pbTemp;
626 switch (pCryptHash->aiAlgid)
628 case CALG_HMAC:
629 if (pCryptHash->pHMACInfo)
630 update_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
631 pbData, dwDataLen);
632 break;
634 case CALG_MAC:
635 pbTemp = HeapAlloc(GetProcessHeap(), 0, dwDataLen);
636 if (!pbTemp) return;
637 memcpy(pbTemp, pbData, dwDataLen);
638 RSAENH_CPEncrypt(pCryptHash->hProv, pCryptHash->hKey, (HCRYPTHASH)NULL, FALSE, 0,
639 pbTemp, &dwDataLen, dwDataLen);
640 HeapFree(GetProcessHeap(), 0, pbTemp);
641 break;
643 default:
644 update_hash_impl(pCryptHash->aiAlgid, &pCryptHash->context, pbData, dwDataLen);
648 /******************************************************************************
649 * finalize_hash [Internal]
651 * Finalizes the hash, after all data has been hashed with update_hash.
652 * No additional data can be hashed afterwards until the hash gets initialized again.
654 * PARAMS
655 * pCryptHash [I] Hash object to be finalized.
657 static inline void finalize_hash(CRYPTHASH *pCryptHash) {
658 DWORD dwDataLen;
660 switch (pCryptHash->aiAlgid)
662 case CALG_HMAC:
663 if (pCryptHash->pHMACInfo) {
664 BYTE abHashValue[RSAENH_MAX_HASH_SIZE];
666 finalize_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
667 pCryptHash->abHashValue);
668 memcpy(abHashValue, pCryptHash->abHashValue, pCryptHash->dwHashSize);
669 init_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context);
670 update_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
671 pCryptHash->pHMACInfo->pbOuterString,
672 pCryptHash->pHMACInfo->cbOuterString);
673 update_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
674 abHashValue, pCryptHash->dwHashSize);
675 finalize_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
676 pCryptHash->abHashValue);
678 break;
680 case CALG_MAC:
681 dwDataLen = 0;
682 RSAENH_CPEncrypt(pCryptHash->hProv, pCryptHash->hKey, (HCRYPTHASH)NULL, TRUE, 0,
683 pCryptHash->abHashValue, &dwDataLen, pCryptHash->dwHashSize);
684 break;
686 default:
687 finalize_hash_impl(pCryptHash->aiAlgid, &pCryptHash->context, pCryptHash->abHashValue);
691 /******************************************************************************
692 * destroy_key [Internal]
694 * Destructor for key objects
696 * PARAMS
697 * pCryptKey [I] Pointer to the key object to be destroyed.
698 * Will be invalid after function returns!
700 static void destroy_key(OBJECTHDR *pObject)
702 CRYPTKEY *pCryptKey = (CRYPTKEY*)pObject;
704 free_key_impl(pCryptKey->aiAlgid, &pCryptKey->context);
705 free_data_blob(&pCryptKey->siSChannelInfo.blobClientRandom);
706 free_data_blob(&pCryptKey->siSChannelInfo.blobServerRandom);
707 HeapFree(GetProcessHeap(), 0, pCryptKey);
710 /******************************************************************************
711 * setup_key [Internal]
713 * Initialize (or reset) a key object
715 * PARAMS
716 * pCryptKey [I] The key object to be initialized.
718 static inline void setup_key(CRYPTKEY *pCryptKey) {
719 pCryptKey->dwState = RSAENH_KEYSTATE_IDLE;
720 memcpy(pCryptKey->abChainVector, pCryptKey->abInitVector, sizeof(pCryptKey->abChainVector));
721 setup_key_impl(pCryptKey->aiAlgid, &pCryptKey->context, pCryptKey->dwKeyLen,
722 pCryptKey->dwSaltLen, pCryptKey->abKeyValue);
725 /******************************************************************************
726 * new_key [Internal]
728 * Creates a new key object without assigning the actual binary key value.
729 * This is done by CPDeriveKey, CPGenKey or CPImportKey, which call this function.
731 * PARAMS
732 * hProv [I] Handle to the provider to which the created key will belong.
733 * aiAlgid [I] The new key shall use the crypto algorithm idenfied by aiAlgid.
734 * dwFlags [I] Upper 16 bits give the key length.
735 * Lower 16 bits: CRYPT_CREATE_SALT, CRYPT_NO_SALT
736 * ppCryptKey [O] Pointer to the created key
738 * RETURNS
739 * Success: Handle to the created key.
740 * Failure: INVALID_HANDLE_VALUE
742 static HCRYPTKEY new_key(HCRYPTPROV hProv, ALG_ID aiAlgid, DWORD dwFlags, CRYPTKEY **ppCryptKey)
744 HCRYPTKEY hCryptKey;
745 CRYPTKEY *pCryptKey;
746 DWORD dwKeyLen = HIWORD(dwFlags);
747 const PROV_ENUMALGS_EX *peaAlgidInfo;
749 *ppCryptKey = NULL;
752 * Retrieve the CSP's capabilities for the given ALG_ID value
754 peaAlgidInfo = get_algid_info(hProv, aiAlgid);
755 if (!peaAlgidInfo) return (HCRYPTKEY)INVALID_HANDLE_VALUE;
758 * Assume the default key length, if none is specified explicitly
760 if (dwKeyLen == 0) dwKeyLen = peaAlgidInfo->dwDefaultLen;
763 * Check if the requested key length is supported by the current CSP.
764 * Adjust key length's for DES algorithms.
766 switch (aiAlgid) {
767 case CALG_DES:
768 if (dwKeyLen == RSAENH_DES_EFFECTIVE_KEYLEN) {
769 dwKeyLen = RSAENH_DES_STORAGE_KEYLEN;
771 if (dwKeyLen != RSAENH_DES_STORAGE_KEYLEN) {
772 SetLastError(NTE_BAD_FLAGS);
773 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
775 break;
777 case CALG_3DES_112:
778 if (dwKeyLen == RSAENH_3DES112_EFFECTIVE_KEYLEN) {
779 dwKeyLen = RSAENH_3DES112_STORAGE_KEYLEN;
781 if (dwKeyLen != RSAENH_3DES112_STORAGE_KEYLEN) {
782 SetLastError(NTE_BAD_FLAGS);
783 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
785 break;
787 case CALG_3DES:
788 if (dwKeyLen == RSAENH_3DES_EFFECTIVE_KEYLEN) {
789 dwKeyLen = RSAENH_3DES_STORAGE_KEYLEN;
791 if (dwKeyLen != RSAENH_3DES_STORAGE_KEYLEN) {
792 SetLastError(NTE_BAD_FLAGS);
793 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
795 break;
797 default:
798 if (dwKeyLen % 8 ||
799 dwKeyLen > peaAlgidInfo->dwMaxLen ||
800 dwKeyLen < peaAlgidInfo->dwMinLen)
802 SetLastError(NTE_BAD_FLAGS);
803 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
807 hCryptKey = (HCRYPTKEY)new_object(&handle_table, sizeof(CRYPTKEY), RSAENH_MAGIC_KEY,
808 destroy_key, (OBJECTHDR**)&pCryptKey);
809 if (hCryptKey != (HCRYPTKEY)INVALID_HANDLE_VALUE)
811 pCryptKey->aiAlgid = aiAlgid;
812 pCryptKey->hProv = hProv;
813 pCryptKey->dwModeBits = 0;
814 pCryptKey->dwPermissions = CRYPT_ENCRYPT | CRYPT_DECRYPT | CRYPT_READ | CRYPT_WRITE |
815 CRYPT_MAC;
816 pCryptKey->dwKeyLen = dwKeyLen >> 3;
817 if ((dwFlags & CRYPT_CREATE_SALT) || (dwKeyLen == 40 && !(dwFlags & CRYPT_NO_SALT)))
818 pCryptKey->dwSaltLen = 16 /*FIXME*/ - pCryptKey->dwKeyLen;
819 else
820 pCryptKey->dwSaltLen = 0;
821 memset(pCryptKey->abKeyValue, 0, sizeof(pCryptKey->abKeyValue));
822 memset(pCryptKey->abInitVector, 0, sizeof(pCryptKey->abInitVector));
823 init_data_blob(&pCryptKey->siSChannelInfo.blobClientRandom);
824 init_data_blob(&pCryptKey->siSChannelInfo.blobServerRandom);
826 switch(aiAlgid)
828 case CALG_PCT1_MASTER:
829 case CALG_SSL2_MASTER:
830 case CALG_SSL3_MASTER:
831 case CALG_TLS1_MASTER:
832 case CALG_RC4:
833 pCryptKey->dwBlockLen = 0;
834 pCryptKey->dwMode = 0;
835 break;
837 case CALG_RC2:
838 case CALG_DES:
839 case CALG_3DES_112:
840 case CALG_3DES:
841 pCryptKey->dwBlockLen = 8;
842 pCryptKey->dwMode = CRYPT_MODE_CBC;
843 break;
845 case CALG_RSA_KEYX:
846 case CALG_RSA_SIGN:
847 pCryptKey->dwBlockLen = dwKeyLen >> 3;
848 pCryptKey->dwMode = 0;
849 break;
852 *ppCryptKey = pCryptKey;
855 return hCryptKey;
858 /******************************************************************************
859 * destroy_key_container [Internal]
861 * Destructor for key containers.
863 * PARAMS
864 * pObjectHdr [I] Pointer to the key container to be destroyed.
866 static void destroy_key_container(OBJECTHDR *pObjectHdr)
868 KEYCONTAINER *pKeyContainer = (KEYCONTAINER*)pObjectHdr;
869 DATA_BLOB blobIn, blobOut;
870 CRYPTKEY *pKey;
871 CHAR szRSABase[MAX_PATH];
872 HKEY hKey, hRootKey;
873 DWORD dwLen;
874 BYTE *pbKey;
876 if (!(pKeyContainer->dwFlags & CRYPT_VERIFYCONTEXT)) {
877 /* On WinXP, persistent keys are stored in a file located at:
878 * $AppData$\\Microsoft\\Crypto\\RSA\\$SID$\\some_hex_string
880 sprintf(szRSABase, RSAENH_REGKEY, pKeyContainer->szName);
882 if (pKeyContainer->dwFlags & CRYPT_MACHINE_KEYSET) {
883 hRootKey = HKEY_LOCAL_MACHINE;
884 } else {
885 hRootKey = HKEY_CURRENT_USER;
888 /* @@ Wine registry key: HKLM\Software\Wine\Crypto\RSA */
889 /* @@ Wine registry key: HKCU\Software\Wine\Crypto\RSA */
890 if (RegCreateKeyExA(hRootKey, szRSABase, 0, NULL, REG_OPTION_NON_VOLATILE,
891 KEY_WRITE, NULL, &hKey, NULL) == ERROR_SUCCESS)
893 if (lookup_handle(&handle_table, pKeyContainer->hKeyExchangeKeyPair, RSAENH_MAGIC_KEY,
894 (OBJECTHDR**)&pKey))
896 if (RSAENH_CPExportKey(pKey->hProv, pKeyContainer->hKeyExchangeKeyPair, 0,
897 PRIVATEKEYBLOB, 0, 0, &dwLen))
899 pbKey = HeapAlloc(GetProcessHeap(), 0, dwLen);
900 if (pbKey)
902 if (RSAENH_CPExportKey(pKey->hProv, pKeyContainer->hKeyExchangeKeyPair, 0,
903 PRIVATEKEYBLOB, 0, pbKey, &dwLen))
905 blobIn.pbData = pbKey;
906 blobIn.cbData = dwLen;
908 if (CryptProtectData(&blobIn, NULL, NULL, NULL, NULL,
909 (pKeyContainer->dwFlags & CRYPT_MACHINE_KEYSET) ?
910 CRYPTPROTECT_LOCAL_MACHINE : 0,
911 &blobOut))
913 RegSetValueExA(hKey, "KeyExchangeKeyPair", 0, REG_BINARY,
914 blobOut.pbData, blobOut.cbData);
915 HeapFree(GetProcessHeap(), 0, blobOut.pbData);
918 HeapFree(GetProcessHeap(), 0, pbKey);
921 release_handle(&handle_table, (unsigned int)pKeyContainer->hKeyExchangeKeyPair,
922 RSAENH_MAGIC_KEY);
925 if (lookup_handle(&handle_table, pKeyContainer->hSignatureKeyPair, RSAENH_MAGIC_KEY,
926 (OBJECTHDR**)&pKey))
928 if (RSAENH_CPExportKey(pKey->hProv, pKeyContainer->hSignatureKeyPair, 0,
929 PRIVATEKEYBLOB, 0, 0, &dwLen))
931 pbKey = HeapAlloc(GetProcessHeap(), 0, dwLen);
932 if (pbKey)
934 if (RSAENH_CPExportKey(pKey->hProv, pKeyContainer->hSignatureKeyPair, 0,
935 PRIVATEKEYBLOB, 0, pbKey, &dwLen))
937 blobIn.pbData = pbKey;
938 blobIn.cbData = dwLen;
940 if (CryptProtectData(&blobIn, NULL, NULL, NULL, NULL,
941 (pKeyContainer->dwFlags & CRYPT_MACHINE_KEYSET) ?
942 CRYPTPROTECT_LOCAL_MACHINE : 0,
943 &blobOut))
945 RegSetValueExA(hKey, "SignatureKeyPair", 0, REG_BINARY,
946 blobOut.pbData, blobOut.cbData);
947 HeapFree(GetProcessHeap(), 0, blobOut.pbData);
950 HeapFree(GetProcessHeap(), 0, pbKey);
953 release_handle(&handle_table, (unsigned int)pKeyContainer->hSignatureKeyPair,
954 RSAENH_MAGIC_KEY);
957 RegCloseKey(hKey);
961 HeapFree( GetProcessHeap(), 0, pKeyContainer );
964 /******************************************************************************
965 * new_key_container [Internal]
967 * Create a new key container. The personality (RSA Base, Strong or Enhanced CP)
968 * of the CSP is determined via the pVTable->pszProvName string.
970 * PARAMS
971 * pszContainerName [I] Name of the key container.
972 * pVTable [I] Callback functions and context info provided by the OS
974 * RETURNS
975 * Success: Handle to the new key container.
976 * Failure: INVALID_HANDLE_VALUE
978 static HCRYPTPROV new_key_container(PCCH pszContainerName, DWORD dwFlags, PVTableProvStruc pVTable)
980 KEYCONTAINER *pKeyContainer;
981 HCRYPTPROV hKeyContainer;
983 hKeyContainer = (HCRYPTPROV)new_object(&handle_table, sizeof(KEYCONTAINER), RSAENH_MAGIC_CONTAINER,
984 destroy_key_container, (OBJECTHDR**)&pKeyContainer);
985 if (hKeyContainer != (HCRYPTPROV)INVALID_HANDLE_VALUE)
987 lstrcpynA(pKeyContainer->szName, pszContainerName, MAX_PATH);
988 pKeyContainer->dwFlags = dwFlags;
989 pKeyContainer->dwEnumAlgsCtr = 0;
990 pKeyContainer->hKeyExchangeKeyPair = (HCRYPTKEY)INVALID_HANDLE_VALUE;
991 pKeyContainer->hSignatureKeyPair = (HCRYPTKEY)INVALID_HANDLE_VALUE;
992 if (pVTable && pVTable->pszProvName) {
993 lstrcpynA(pKeyContainer->szProvName, pVTable->pszProvName, MAX_PATH);
994 if (!strcmp(pVTable->pszProvName, MS_DEF_PROV_A)) {
995 pKeyContainer->dwPersonality = RSAENH_PERSONALITY_BASE;
996 } else if (!strcmp(pVTable->pszProvName, MS_ENHANCED_PROV_A)) {
997 pKeyContainer->dwPersonality = RSAENH_PERSONALITY_ENHANCED;
998 } else if (!strcmp(pVTable->pszProvName, MS_DEF_RSA_SCHANNEL_PROV_A)) {
999 pKeyContainer->dwPersonality = RSAENH_PERSONALITY_SCHANNEL;
1000 } else {
1001 pKeyContainer->dwPersonality = RSAENH_PERSONALITY_STRONG;
1005 /* The new key container has to be inserted into the CSP immediately
1006 * after creation to be available for CPGetProvParam's PP_ENUMCONTAINERS. */
1007 if (!(dwFlags & CRYPT_VERIFYCONTEXT)) {
1008 CHAR szRSABase[MAX_PATH];
1009 HKEY hRootKey, hKey;
1011 sprintf(szRSABase, RSAENH_REGKEY, pKeyContainer->szName);
1013 if (pKeyContainer->dwFlags & CRYPT_MACHINE_KEYSET) {
1014 hRootKey = HKEY_LOCAL_MACHINE;
1015 } else {
1016 hRootKey = HKEY_CURRENT_USER;
1019 /* @@ Wine registry key: HKLM\Software\Wine\Crypto\RSA */
1020 /* @@ Wine registry key: HKCU\Software\Wine\Crypto\RSA */
1021 RegCreateKeyA(hRootKey, szRSABase, &hKey);
1022 RegCloseKey(hKey);
1026 return hKeyContainer;
1029 /******************************************************************************
1030 * read_key_container [Internal]
1032 * Tries to read the persistent state of the key container (mainly the signature
1033 * and key exchange private keys) given by pszContainerName.
1035 * PARAMS
1036 * pszContainerName [I] Name of the key container to read from the registry
1037 * pVTable [I] Pointer to context data provided by the operating system
1039 * RETURNS
1040 * Success: Handle to the key container read from the registry
1041 * Failure: INVALID_HANDLE_VALUE
1043 static HCRYPTPROV read_key_container(PCHAR pszContainerName, DWORD dwFlags, PVTableProvStruc pVTable)
1045 CHAR szRSABase[MAX_PATH];
1046 BYTE *pbKey;
1047 HKEY hKey, hRootKey;
1048 DWORD dwValueType, dwLen;
1049 KEYCONTAINER *pKeyContainer;
1050 HCRYPTPROV hKeyContainer;
1051 DATA_BLOB blobIn, blobOut;
1053 sprintf(szRSABase, RSAENH_REGKEY, pszContainerName);
1055 if (dwFlags & CRYPT_MACHINE_KEYSET) {
1056 hRootKey = HKEY_LOCAL_MACHINE;
1057 } else {
1058 hRootKey = HKEY_CURRENT_USER;
1061 /* @@ Wine registry key: HKLM\Software\Wine\Crypto\RSA */
1062 /* @@ Wine registry key: HKCU\Software\Wine\Crypto\RSA */
1063 if (RegOpenKeyExA(hRootKey, szRSABase, 0, KEY_READ, &hKey) != ERROR_SUCCESS)
1065 SetLastError(NTE_BAD_KEYSET);
1066 return (HCRYPTPROV)INVALID_HANDLE_VALUE;
1069 hKeyContainer = new_key_container(pszContainerName, dwFlags, pVTable);
1070 if (hKeyContainer != (HCRYPTPROV)INVALID_HANDLE_VALUE)
1072 if (!lookup_handle(&handle_table, hKeyContainer, RSAENH_MAGIC_CONTAINER,
1073 (OBJECTHDR**)&pKeyContainer))
1074 return (HCRYPTPROV)INVALID_HANDLE_VALUE;
1076 if (RegQueryValueExA(hKey, "KeyExchangeKeyPair", 0, &dwValueType, NULL, &dwLen) ==
1077 ERROR_SUCCESS)
1079 pbKey = HeapAlloc(GetProcessHeap(), 0, dwLen);
1080 if (pbKey)
1082 if (RegQueryValueExA(hKey, "KeyExchangeKeyPair", 0, &dwValueType, pbKey, &dwLen) ==
1083 ERROR_SUCCESS)
1085 blobIn.pbData = pbKey;
1086 blobIn.cbData = dwLen;
1088 if (CryptUnprotectData(&blobIn, NULL, NULL, NULL, NULL,
1089 (dwFlags & CRYPT_MACHINE_KEYSET) ? CRYPTPROTECT_LOCAL_MACHINE : 0, &blobOut))
1091 RSAENH_CPImportKey(hKeyContainer, blobOut.pbData, blobOut.cbData, 0, 0,
1092 &pKeyContainer->hKeyExchangeKeyPair);
1093 HeapFree(GetProcessHeap(), 0, blobOut.pbData);
1096 HeapFree(GetProcessHeap(), 0, pbKey);
1100 if (RegQueryValueExA(hKey, "SignatureKeyPair", 0, &dwValueType, NULL, &dwLen) ==
1101 ERROR_SUCCESS)
1103 pbKey = HeapAlloc(GetProcessHeap(), 0, dwLen);
1104 if (pbKey)
1106 if (RegQueryValueExA(hKey, "SignatureKeyPair", 0, &dwValueType, pbKey, &dwLen) ==
1107 ERROR_SUCCESS)
1109 blobIn.pbData = pbKey;
1110 blobIn.cbData = dwLen;
1112 if (CryptUnprotectData(&blobIn, NULL, NULL, NULL, NULL,
1113 (dwFlags & CRYPT_MACHINE_KEYSET) ? CRYPTPROTECT_LOCAL_MACHINE : 0, &blobOut))
1115 RSAENH_CPImportKey(hKeyContainer, blobOut.pbData, blobOut.cbData, 0, 0,
1116 &pKeyContainer->hSignatureKeyPair);
1117 HeapFree(GetProcessHeap(), 0, blobOut.pbData);
1120 HeapFree(GetProcessHeap(), 0, pbKey);
1125 return hKeyContainer;
1128 /******************************************************************************
1129 * build_hash_signature [Internal]
1131 * Builds a padded version of a hash to match the length of the RSA key modulus.
1133 * PARAMS
1134 * pbSignature [O] The padded hash object is stored here.
1135 * dwLen [I] Length of the pbSignature buffer.
1136 * aiAlgid [I] Algorithm identifier of the hash to be padded.
1137 * abHashValue [I] The value of the hash object.
1138 * dwHashLen [I] Length of the hash value.
1139 * dwFlags [I] Selection of padding algorithm.
1141 * RETURNS
1142 * Success: TRUE
1143 * Failure: FALSE (NTE_BAD_ALGID)
1145 static BOOL build_hash_signature(BYTE *pbSignature, DWORD dwLen, ALG_ID aiAlgid,
1146 CONST BYTE *abHashValue, DWORD dwHashLen, DWORD dwFlags)
1148 /* These prefixes are meant to be concatenated with hash values of the
1149 * respective kind to form a PKCS #7 DigestInfo. */
1150 static const struct tagOIDDescriptor {
1151 ALG_ID aiAlgid;
1152 DWORD dwLen;
1153 CONST BYTE abOID[18];
1154 } aOIDDescriptor[5] = {
1155 { CALG_MD2, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1156 0x86, 0xf7, 0x0d, 0x02, 0x02, 0x05, 0x00, 0x04, 0x10 } },
1157 { CALG_MD4, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1158 0x86, 0xf7, 0x0d, 0x02, 0x04, 0x05, 0x00, 0x04, 0x10 } },
1159 { CALG_MD5, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1160 0x86, 0xf7, 0x0d, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10 } },
1161 { CALG_SHA, 15, { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03,
1162 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14 } },
1163 { 0, 0, {} }
1165 DWORD dwIdxOID, i, j;
1167 for (dwIdxOID = 0; aOIDDescriptor[dwIdxOID].aiAlgid; dwIdxOID++) {
1168 if (aOIDDescriptor[dwIdxOID].aiAlgid == aiAlgid) break;
1171 if (!aOIDDescriptor[dwIdxOID].aiAlgid) {
1172 SetLastError(NTE_BAD_ALGID);
1173 return FALSE;
1176 /* Build the padded signature */
1177 if (dwFlags & CRYPT_X931_FORMAT) {
1178 pbSignature[0] = 0x6b;
1179 for (i=1; i < dwLen - dwHashLen - 3; i++) {
1180 pbSignature[i] = 0xbb;
1182 pbSignature[i++] = 0xba;
1183 for (j=0; j < dwHashLen; j++, i++) {
1184 pbSignature[i] = abHashValue[j];
1186 pbSignature[i++] = 0x33;
1187 pbSignature[i++] = 0xcc;
1188 } else {
1189 pbSignature[0] = 0x00;
1190 pbSignature[1] = 0x01;
1191 if (dwFlags & CRYPT_NOHASHOID) {
1192 for (i=2; i < dwLen - 1 - dwHashLen; i++) {
1193 pbSignature[i] = 0xff;
1195 pbSignature[i++] = 0x00;
1196 } else {
1197 for (i=2; i < dwLen - 1 - aOIDDescriptor[dwIdxOID].dwLen - dwHashLen; i++) {
1198 pbSignature[i] = 0xff;
1200 pbSignature[i++] = 0x00;
1201 for (j=0; j < aOIDDescriptor[dwIdxOID].dwLen; j++) {
1202 pbSignature[i++] = aOIDDescriptor[dwIdxOID].abOID[j];
1205 for (j=0; j < dwHashLen; j++) {
1206 pbSignature[i++] = abHashValue[j];
1210 return TRUE;
1213 /******************************************************************************
1214 * tls1_p [Internal]
1216 * This is an implementation of the 'P_hash' helper function for TLS1's PRF.
1217 * It is used exclusively by tls1_prf. For details see RFC 2246, chapter 5.
1218 * The pseudo random stream generated by this function is exclusive or'ed with
1219 * the data in pbBuffer.
1221 * PARAMS
1222 * hHMAC [I] HMAC object, which will be used in pseudo random generation
1223 * pblobSeed [I] Seed value
1224 * pbBuffer [I/O] Pseudo random stream will be xor'ed to the provided data
1225 * dwBufferLen [I] Number of pseudo random bytes desired
1227 * RETURNS
1228 * Success: TRUE
1229 * Failure: FALSE
1231 static BOOL tls1_p(HCRYPTHASH hHMAC, CONST PCRYPT_DATA_BLOB pblobSeed, PBYTE pbBuffer, DWORD dwBufferLen)
1233 CRYPTHASH *pHMAC;
1234 BYTE abAi[RSAENH_MAX_HASH_SIZE];
1235 DWORD i = 0;
1237 if (!lookup_handle(&handle_table, hHMAC, RSAENH_MAGIC_HASH, (OBJECTHDR**)&pHMAC)) {
1238 SetLastError(NTE_BAD_HASH);
1239 return FALSE;
1242 /* compute A_1 = HMAC(seed) */
1243 init_hash(pHMAC);
1244 update_hash(pHMAC, pblobSeed->pbData, pblobSeed->cbData);
1245 finalize_hash(pHMAC);
1246 memcpy(abAi, pHMAC->abHashValue, pHMAC->dwHashSize);
1248 do {
1249 /* compute HMAC(A_i + seed) */
1250 init_hash(pHMAC);
1251 update_hash(pHMAC, abAi, pHMAC->dwHashSize);
1252 update_hash(pHMAC, pblobSeed->pbData, pblobSeed->cbData);
1253 finalize_hash(pHMAC);
1255 /* pseudo random stream := CONCAT_{i=1..n} ( HMAC(A_i + seed) ) */
1256 do {
1257 if (i >= dwBufferLen) break;
1258 pbBuffer[i] ^= pHMAC->abHashValue[i % pHMAC->dwHashSize];
1259 i++;
1260 } while (i % pHMAC->dwHashSize);
1262 /* compute A_{i+1} = HMAC(A_i) */
1263 init_hash(pHMAC);
1264 update_hash(pHMAC, abAi, pHMAC->dwHashSize);
1265 finalize_hash(pHMAC);
1266 memcpy(abAi, pHMAC->abHashValue, pHMAC->dwHashSize);
1267 } while (i < dwBufferLen);
1269 return TRUE;
1272 /******************************************************************************
1273 * tls1_prf [Internal]
1275 * TLS1 pseudo random function as specified in RFC 2246, chapter 5
1277 * PARAMS
1278 * hProv [I] Key container used to compute the pseudo random stream
1279 * hSecret [I] Key that holds the (pre-)master secret
1280 * pblobLabel [I] Descriptive label
1281 * pblobSeed [I] Seed value
1282 * pbBuffer [O] Pseudo random numbers will be stored here
1283 * dwBufferLen [I] Number of pseudo random bytes desired
1285 * RETURNS
1286 * Success: TRUE
1287 * Failure: FALSE
1289 static BOOL tls1_prf(HCRYPTPROV hProv, HCRYPTPROV hSecret, CONST PCRYPT_DATA_BLOB pblobLabel,
1290 CONST PCRYPT_DATA_BLOB pblobSeed, PBYTE pbBuffer, DWORD dwBufferLen)
1292 HMAC_INFO hmacInfo = { 0, NULL, 0, NULL, 0 };
1293 HCRYPTHASH hHMAC = (HCRYPTHASH)INVALID_HANDLE_VALUE;
1294 HCRYPTKEY hHalfSecret = (HCRYPTKEY)INVALID_HANDLE_VALUE;
1295 CRYPTKEY *pHalfSecret, *pSecret;
1296 DWORD dwHalfSecretLen;
1297 BOOL result = FALSE;
1298 CRYPT_DATA_BLOB blobLabelSeed;
1300 TRACE("(hProv=%08lx, hSecret=%08lx, pblobLabel=%p, pblobSeed=%p, pbBuffer=%p, dwBufferLen=%ld)\n",
1301 hProv, hSecret, pblobLabel, pblobSeed, pbBuffer, dwBufferLen);
1303 if (!lookup_handle(&handle_table, hSecret, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pSecret)) {
1304 SetLastError(NTE_FAIL);
1305 return FALSE;
1308 dwHalfSecretLen = (pSecret->dwKeyLen+1)/2;
1310 /* concatenation of the label and the seed */
1311 if (!concat_data_blobs(&blobLabelSeed, pblobLabel, pblobSeed)) goto exit;
1313 /* zero out the buffer, since two random streams will be xor'ed into it. */
1314 memset(pbBuffer, 0, dwBufferLen);
1316 /* build a 'fake' key, to hold the secret. CALG_SSL2_MASTER is used since it provides
1317 * the biggest range of valid key lengths. */
1318 hHalfSecret = new_key(hProv, CALG_SSL2_MASTER, MAKELONG(0,dwHalfSecretLen*8), &pHalfSecret);
1319 if (hHalfSecret == (HCRYPTKEY)INVALID_HANDLE_VALUE) goto exit;
1321 /* Derive an HMAC_MD5 hash and call the helper function. */
1322 memcpy(pHalfSecret->abKeyValue, pSecret->abKeyValue, dwHalfSecretLen);
1323 if (!RSAENH_CPCreateHash(hProv, CALG_HMAC, hHalfSecret, 0, &hHMAC)) goto exit;
1324 hmacInfo.HashAlgid = CALG_MD5;
1325 if (!RSAENH_CPSetHashParam(hProv, hHMAC, HP_HMAC_INFO, (BYTE*)&hmacInfo, 0)) goto exit;
1326 if (!tls1_p(hHMAC, &blobLabelSeed, pbBuffer, dwBufferLen)) goto exit;
1328 /* Reconfigure to HMAC_SHA hash and call helper function again. */
1329 memcpy(pHalfSecret->abKeyValue, pSecret->abKeyValue + (pSecret->dwKeyLen/2), dwHalfSecretLen);
1330 hmacInfo.HashAlgid = CALG_SHA;
1331 if (!RSAENH_CPSetHashParam(hProv, hHMAC, HP_HMAC_INFO, (BYTE*)&hmacInfo, 0)) goto exit;
1332 if (!tls1_p(hHMAC, &blobLabelSeed, pbBuffer, dwBufferLen)) goto exit;
1334 result = TRUE;
1335 exit:
1336 release_handle(&handle_table, hHalfSecret, RSAENH_MAGIC_KEY);
1337 if (hHMAC != (HCRYPTHASH)INVALID_HANDLE_VALUE) RSAENH_CPDestroyHash(hProv, hHMAC);
1338 free_data_blob(&blobLabelSeed);
1339 return result;
1342 /******************************************************************************
1343 * pad_data [Internal]
1345 * Helper function for data padding according to PKCS1 #2
1347 * PARAMS
1348 * abData [I] The data to be padded
1349 * dwDataLen [I] Length of the data
1350 * abBuffer [O] Padded data will be stored here
1351 * dwBufferLen [I] Length of the buffer (also length of padded data)
1352 * dwFlags [I] Padding format (CRYPT_SSL2_FALLBACK)
1354 * RETURN
1355 * Success: TRUE
1356 * Failure: FALSE (NTE_BAD_LEN, too much data to pad)
1358 static BOOL pad_data(CONST BYTE *abData, DWORD dwDataLen, BYTE *abBuffer, DWORD dwBufferLen,
1359 DWORD dwFlags)
1361 DWORD i;
1363 /* Ensure there is enough space for PKCS1 #2 padding */
1364 if (dwDataLen > dwBufferLen-11) {
1365 SetLastError(NTE_BAD_LEN);
1366 return FALSE;
1369 memmove(abBuffer + dwBufferLen - dwDataLen, abData, dwDataLen);
1371 abBuffer[0] = 0x00;
1372 abBuffer[1] = RSAENH_PKC_BLOCKTYPE;
1373 for (i=2; i < dwBufferLen - dwDataLen - 1; i++)
1374 do gen_rand_impl(&abBuffer[i], 1); while (!abBuffer[i]);
1375 if (dwFlags & CRYPT_SSL2_FALLBACK)
1376 for (i-=8; i < dwBufferLen - dwDataLen - 1; i++)
1377 abBuffer[i] = 0x03;
1378 abBuffer[i] = 0x00;
1380 return TRUE;
1383 /******************************************************************************
1384 * unpad_data [Internal]
1386 * Remove the PKCS1 padding from RSA decrypted data
1388 * PARAMS
1389 * abData [I] The padded data
1390 * dwDataLen [I] Length of the padded data
1391 * abBuffer [O] Data without padding will be stored here
1392 * dwBufferLen [I/O] I: Length of the buffer, O: Length of unpadded data
1393 * dwFlags [I] Currently none defined
1395 * RETURNS
1396 * Success: TRUE
1397 * Failure: FALSE, (NTE_BAD_DATA, no valid PKCS1 padding or buffer too small)
1399 static BOOL unpad_data(CONST BYTE *abData, DWORD dwDataLen, BYTE *abBuffer, DWORD *dwBufferLen,
1400 DWORD dwFlags)
1402 DWORD i;
1404 for (i=2; i<dwDataLen; i++)
1405 if (!abData[i])
1406 break;
1408 if ((i == dwDataLen) || (*dwBufferLen < dwDataLen - i - 1) ||
1409 (abData[0] != 0x00) || (abData[1] != RSAENH_PKC_BLOCKTYPE))
1411 SetLastError(NTE_BAD_DATA);
1412 return FALSE;
1415 *dwBufferLen = dwDataLen - i - 1;
1416 memmove(abBuffer, abData + i + 1, *dwBufferLen);
1417 return TRUE;
1420 /******************************************************************************
1421 * CPAcquireContext (RSAENH.@)
1423 * Acquire a handle to the key container specified by pszContainer
1425 * PARAMS
1426 * phProv [O] Pointer to the location the acquired handle will be written to.
1427 * pszContainer [I] Name of the desired key container. See Notes
1428 * dwFlags [I] Flags. See Notes.
1429 * pVTable [I] Pointer to a PVTableProvStruct containing callbacks.
1431 * RETURNS
1432 * Success: TRUE
1433 * Failure: FALSE
1435 * NOTES
1436 * If pszContainer is NULL or points to a zero length string the user's login
1437 * name will be used as the key container name.
1439 * If the CRYPT_NEW_KEYSET flag is set in dwFlags a new keyset will be created.
1440 * If a keyset with the given name already exists, the function fails and sets
1441 * last error to NTE_EXISTS. If CRYPT_NEW_KEYSET is not set and the specified
1442 * key container does not exist, function fails and sets last error to
1443 * NTE_BAD_KEYSET.
1445 BOOL WINAPI RSAENH_CPAcquireContext(HCRYPTPROV *phProv, LPSTR pszContainer,
1446 DWORD dwFlags, PVTableProvStruc pVTable)
1448 CHAR szKeyContainerName[MAX_PATH];
1449 CHAR szRegKey[MAX_PATH];
1451 TRACE("(phProv=%p, pszContainer=%s, dwFlags=%08lx, pVTable=%p)\n", phProv,
1452 debugstr_a(pszContainer), dwFlags, pVTable);
1454 if (pszContainer && *pszContainer)
1456 lstrcpynA(szKeyContainerName, pszContainer, MAX_PATH);
1458 else
1460 DWORD dwLen = sizeof(szKeyContainerName);
1461 if (!GetUserNameA(szKeyContainerName, &dwLen)) return FALSE;
1464 switch (dwFlags & (CRYPT_NEWKEYSET|CRYPT_VERIFYCONTEXT|CRYPT_DELETEKEYSET))
1466 case 0:
1467 *phProv = read_key_container(szKeyContainerName, dwFlags, pVTable);
1468 break;
1470 case CRYPT_DELETEKEYSET:
1471 if (snprintf(szRegKey, MAX_PATH, RSAENH_REGKEY, pszContainer) >= MAX_PATH) {
1472 SetLastError(NTE_BAD_KEYSET_PARAM);
1473 return FALSE;
1474 } else {
1475 RegDeleteKeyA(HKEY_CURRENT_USER, szRegKey);
1476 SetLastError(ERROR_SUCCESS);
1477 return TRUE;
1479 break;
1481 case CRYPT_NEWKEYSET:
1482 *phProv = read_key_container(szKeyContainerName, dwFlags, pVTable);
1483 if (*phProv != (HCRYPTPROV)INVALID_HANDLE_VALUE)
1485 release_handle(&handle_table, (unsigned int)*phProv, RSAENH_MAGIC_CONTAINER);
1486 SetLastError(NTE_EXISTS);
1487 return FALSE;
1489 *phProv = new_key_container(szKeyContainerName, dwFlags, pVTable);
1490 break;
1492 case CRYPT_VERIFYCONTEXT:
1493 if (pszContainer) {
1494 SetLastError(NTE_BAD_FLAGS);
1495 return FALSE;
1497 *phProv = new_key_container("", dwFlags, pVTable);
1498 break;
1500 default:
1501 *phProv = (unsigned int)INVALID_HANDLE_VALUE;
1502 SetLastError(NTE_BAD_FLAGS);
1503 return FALSE;
1506 if (*phProv != (unsigned int)INVALID_HANDLE_VALUE) {
1507 SetLastError(ERROR_SUCCESS);
1508 return TRUE;
1509 } else {
1510 return FALSE;
1514 /******************************************************************************
1515 * CPCreateHash (RSAENH.@)
1517 * CPCreateHash creates and initalizes a new hash object.
1519 * PARAMS
1520 * hProv [I] Handle to the key container to which the new hash will belong.
1521 * Algid [I] Identifies the hash algorithm, which will be used for the hash.
1522 * hKey [I] Handle to a session key applied for keyed hashes.
1523 * dwFlags [I] Currently no flags defined. Must be zero.
1524 * phHash [O] Points to the location where a handle to the new hash will be stored.
1526 * RETURNS
1527 * Success: TRUE
1528 * Failure: FALSE
1530 * NOTES
1531 * hKey is a handle to a session key applied in keyed hashes like MAC and HMAC.
1532 * If a normal hash object is to be created (like e.g. MD2 or SHA1) hKey must be zero.
1534 BOOL WINAPI RSAENH_CPCreateHash(HCRYPTPROV hProv, ALG_ID Algid, HCRYPTKEY hKey, DWORD dwFlags,
1535 HCRYPTHASH *phHash)
1537 CRYPTKEY *pCryptKey;
1538 CRYPTHASH *pCryptHash;
1539 const PROV_ENUMALGS_EX *peaAlgidInfo;
1541 TRACE("(hProv=%08lx, Algid=%08x, hKey=%08lx, dwFlags=%08lx, phHash=%p)\n", hProv, Algid, hKey,
1542 dwFlags, phHash);
1544 peaAlgidInfo = get_algid_info(hProv, Algid);
1545 if (!peaAlgidInfo) return FALSE;
1547 if (dwFlags)
1549 SetLastError(NTE_BAD_FLAGS);
1550 return FALSE;
1553 if (Algid == CALG_MAC || Algid == CALG_HMAC || Algid == CALG_SCHANNEL_MASTER_HASH ||
1554 Algid == CALG_TLS1PRF)
1556 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey)) {
1557 SetLastError(NTE_BAD_KEY);
1558 return FALSE;
1561 if ((Algid == CALG_MAC) && (GET_ALG_TYPE(pCryptKey->aiAlgid) != ALG_TYPE_BLOCK)) {
1562 SetLastError(NTE_BAD_KEY);
1563 return FALSE;
1566 if ((Algid == CALG_SCHANNEL_MASTER_HASH || Algid == CALG_TLS1PRF) &&
1567 (pCryptKey->aiAlgid != CALG_TLS1_MASTER))
1569 SetLastError(NTE_BAD_KEY);
1570 return FALSE;
1573 if ((Algid == CALG_TLS1PRF) && (pCryptKey->dwState != RSAENH_KEYSTATE_MASTERKEY)) {
1574 SetLastError(NTE_BAD_KEY_STATE);
1575 return FALSE;
1579 *phHash = (HCRYPTHASH)new_object(&handle_table, sizeof(CRYPTHASH), RSAENH_MAGIC_HASH,
1580 destroy_hash, (OBJECTHDR**)&pCryptHash);
1581 if (!pCryptHash) return FALSE;
1583 pCryptHash->aiAlgid = Algid;
1584 pCryptHash->hKey = hKey;
1585 pCryptHash->hProv = hProv;
1586 pCryptHash->dwState = RSAENH_HASHSTATE_IDLE;
1587 pCryptHash->pHMACInfo = (PHMAC_INFO)NULL;
1588 pCryptHash->dwHashSize = peaAlgidInfo->dwDefaultLen >> 3;
1589 init_data_blob(&pCryptHash->tpPRFParams.blobLabel);
1590 init_data_blob(&pCryptHash->tpPRFParams.blobSeed);
1592 if (Algid == CALG_SCHANNEL_MASTER_HASH) {
1593 static const char keyex[] = "key expansion";
1594 BYTE key_expansion[sizeof keyex];
1595 CRYPT_DATA_BLOB blobRandom, blobKeyExpansion = { 13, key_expansion };
1597 memcpy( key_expansion, keyex, sizeof keyex );
1599 if (pCryptKey->dwState != RSAENH_KEYSTATE_MASTERKEY) {
1600 static const char msec[] = "master secret";
1601 BYTE master_secret[sizeof msec];
1602 CRYPT_DATA_BLOB blobLabel = { 13, master_secret };
1603 BYTE abKeyValue[48];
1605 memcpy( master_secret, msec, sizeof msec );
1607 /* See RFC 2246, chapter 8.1 */
1608 if (!concat_data_blobs(&blobRandom,
1609 &pCryptKey->siSChannelInfo.blobClientRandom,
1610 &pCryptKey->siSChannelInfo.blobServerRandom))
1612 return FALSE;
1614 tls1_prf(hProv, hKey, &blobLabel, &blobRandom, abKeyValue, 48);
1615 pCryptKey->dwState = RSAENH_KEYSTATE_MASTERKEY;
1616 memcpy(pCryptKey->abKeyValue, abKeyValue, 48);
1617 free_data_blob(&blobRandom);
1620 /* See RFC 2246, chapter 6.3 */
1621 if (!concat_data_blobs(&blobRandom,
1622 &pCryptKey->siSChannelInfo.blobServerRandom,
1623 &pCryptKey->siSChannelInfo.blobClientRandom))
1625 return FALSE;
1627 tls1_prf(hProv, hKey, &blobKeyExpansion, &blobRandom, pCryptHash->abHashValue,
1628 RSAENH_MAX_HASH_SIZE);
1629 free_data_blob(&blobRandom);
1632 return init_hash(pCryptHash);
1635 /******************************************************************************
1636 * CPDestroyHash (RSAENH.@)
1638 * Releases the handle to a hash object. The object is destroyed if it's reference
1639 * count reaches zero.
1641 * PARAMS
1642 * hProv [I] Handle to the key container to which the hash object belongs.
1643 * hHash [I] Handle to the hash object to be released.
1645 * RETURNS
1646 * Success: TRUE
1647 * Failure: FALSE
1649 BOOL WINAPI RSAENH_CPDestroyHash(HCRYPTPROV hProv, HCRYPTHASH hHash)
1651 TRACE("(hProv=%08lx, hHash=%08lx)\n", hProv, hHash);
1653 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
1655 SetLastError(NTE_BAD_UID);
1656 return FALSE;
1659 if (!release_handle(&handle_table, hHash, RSAENH_MAGIC_HASH))
1661 SetLastError(NTE_BAD_HASH);
1662 return FALSE;
1665 return TRUE;
1668 /******************************************************************************
1669 * CPDestroyKey (RSAENH.@)
1671 * Releases the handle to a key object. The object is destroyed if it's reference
1672 * count reaches zero.
1674 * PARAMS
1675 * hProv [I] Handle to the key container to which the key object belongs.
1676 * hKey [I] Handle to the key object to be released.
1678 * RETURNS
1679 * Success: TRUE
1680 * Failure: FALSE
1682 BOOL WINAPI RSAENH_CPDestroyKey(HCRYPTPROV hProv, HCRYPTKEY hKey)
1684 TRACE("(hProv=%08lx, hKey=%08lx)\n", hProv, hKey);
1686 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
1688 SetLastError(NTE_BAD_UID);
1689 return FALSE;
1692 if (!release_handle(&handle_table, hKey, RSAENH_MAGIC_KEY))
1694 SetLastError(NTE_BAD_KEY);
1695 return FALSE;
1698 return TRUE;
1701 /******************************************************************************
1702 * CPDuplicateHash (RSAENH.@)
1704 * Clones a hash object including it's current state.
1706 * PARAMS
1707 * hUID [I] Handle to the key container the hash belongs to.
1708 * hHash [I] Handle to the hash object to be cloned.
1709 * pdwReserved [I] Reserved. Must be NULL.
1710 * dwFlags [I] No flags are currently defined. Must be 0.
1711 * phHash [O] Handle to the cloned hash object.
1713 * RETURNS
1714 * Success: TRUE.
1715 * Failure: FALSE.
1717 BOOL WINAPI RSAENH_CPDuplicateHash(HCRYPTPROV hUID, HCRYPTHASH hHash, DWORD *pdwReserved,
1718 DWORD dwFlags, HCRYPTHASH *phHash)
1720 CRYPTHASH *pSrcHash, *pDestHash;
1722 TRACE("(hUID=%08lx, hHash=%08lx, pdwReserved=%p, dwFlags=%08lx, phHash=%p)\n", hUID, hHash,
1723 pdwReserved, dwFlags, phHash);
1725 if (!is_valid_handle(&handle_table, hUID, RSAENH_MAGIC_CONTAINER))
1727 SetLastError(NTE_BAD_UID);
1728 return FALSE;
1731 if (!lookup_handle(&handle_table, hHash, RSAENH_MAGIC_HASH, (OBJECTHDR**)&pSrcHash))
1733 SetLastError(NTE_BAD_HASH);
1734 return FALSE;
1737 if (!phHash || pdwReserved || dwFlags)
1739 SetLastError(ERROR_INVALID_PARAMETER);
1740 return FALSE;
1743 *phHash = (HCRYPTHASH)new_object(&handle_table, sizeof(CRYPTHASH), RSAENH_MAGIC_HASH,
1744 destroy_hash, (OBJECTHDR**)&pDestHash);
1745 if (*phHash != (HCRYPTHASH)INVALID_HANDLE_VALUE)
1747 memcpy(pDestHash, pSrcHash, sizeof(CRYPTHASH));
1748 duplicate_hash_impl(pSrcHash->aiAlgid, &pSrcHash->context, &pDestHash->context);
1749 copy_hmac_info(&pDestHash->pHMACInfo, pSrcHash->pHMACInfo);
1750 copy_data_blob(&pDestHash->tpPRFParams.blobLabel, &pSrcHash->tpPRFParams.blobLabel);
1751 copy_data_blob(&pDestHash->tpPRFParams.blobSeed, &pSrcHash->tpPRFParams.blobSeed);
1754 return *phHash != (HCRYPTHASH)INVALID_HANDLE_VALUE;
1757 /******************************************************************************
1758 * CPDuplicateKey (RSAENH.@)
1760 * Clones a key object including it's current state.
1762 * PARAMS
1763 * hUID [I] Handle to the key container the hash belongs to.
1764 * hKey [I] Handle to the key object to be cloned.
1765 * pdwReserved [I] Reserved. Must be NULL.
1766 * dwFlags [I] No flags are currently defined. Must be 0.
1767 * phHash [O] Handle to the cloned key object.
1769 * RETURNS
1770 * Success: TRUE.
1771 * Failure: FALSE.
1773 BOOL WINAPI RSAENH_CPDuplicateKey(HCRYPTPROV hUID, HCRYPTKEY hKey, DWORD *pdwReserved,
1774 DWORD dwFlags, HCRYPTKEY *phKey)
1776 CRYPTKEY *pSrcKey, *pDestKey;
1778 TRACE("(hUID=%08lx, hKey=%08lx, pdwReserved=%p, dwFlags=%08lx, phKey=%p)\n", hUID, hKey,
1779 pdwReserved, dwFlags, phKey);
1781 if (!is_valid_handle(&handle_table, hUID, RSAENH_MAGIC_CONTAINER))
1783 SetLastError(NTE_BAD_UID);
1784 return FALSE;
1787 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pSrcKey))
1789 SetLastError(NTE_BAD_KEY);
1790 return FALSE;
1793 if (!phKey || pdwReserved || dwFlags)
1795 SetLastError(ERROR_INVALID_PARAMETER);
1796 return FALSE;
1799 *phKey = (HCRYPTKEY)new_object(&handle_table, sizeof(CRYPTKEY), RSAENH_MAGIC_KEY, destroy_key,
1800 (OBJECTHDR**)&pDestKey);
1801 if (*phKey != (HCRYPTKEY)INVALID_HANDLE_VALUE)
1803 memcpy(pDestKey, pSrcKey, sizeof(CRYPTKEY));
1804 copy_data_blob(&pDestKey->siSChannelInfo.blobServerRandom,
1805 &pSrcKey->siSChannelInfo.blobServerRandom);
1806 copy_data_blob(&pDestKey->siSChannelInfo.blobClientRandom,
1807 &pSrcKey->siSChannelInfo.blobClientRandom);
1808 duplicate_key_impl(pSrcKey->aiAlgid, &pSrcKey->context, &pDestKey->context);
1809 return TRUE;
1811 else
1813 return FALSE;
1817 /******************************************************************************
1818 * CPEncrypt (RSAENH.@)
1820 * Encrypt data.
1822 * PARAMS
1823 * hProv [I] The key container hKey and hHash belong to.
1824 * hKey [I] The key used to encrypt the data.
1825 * hHash [I] An optional hash object for parallel hashing. See notes.
1826 * Final [I] Indicates if this is the last block of data to encrypt.
1827 * dwFlags [I] Currently no flags defined. Must be zero.
1828 * pbData [I/O] Pointer to the data to encrypt. Encrypted data will also be stored there.
1829 * pdwDataLen [I/O] I: Length of data to encrypt, O: Length of encrypted data.
1830 * dwBufLen [I] Size of the buffer at pbData.
1832 * RETURNS
1833 * Success: TRUE.
1834 * Failure: FALSE.
1836 * NOTES
1837 * If a hash object handle is provided in hHash, it will be updated with the plaintext.
1838 * This is useful for message signatures.
1840 * This function uses the standard WINAPI protocol for querying data of dynamic length.
1842 BOOL WINAPI RSAENH_CPEncrypt(HCRYPTPROV hProv, HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final,
1843 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen, DWORD dwBufLen)
1845 CRYPTKEY *pCryptKey;
1846 BYTE *in, out[RSAENH_MAX_BLOCK_SIZE], o[RSAENH_MAX_BLOCK_SIZE];
1847 DWORD dwEncryptedLen, i, j, k;
1849 TRACE("(hProv=%08lx, hKey=%08lx, hHash=%08lx, Final=%d, dwFlags=%08lx, pbData=%p, "
1850 "pdwDataLen=%p, dwBufLen=%ld)\n", hProv, hKey, hHash, Final, dwFlags, pbData, pdwDataLen,
1851 dwBufLen);
1853 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
1855 SetLastError(NTE_BAD_UID);
1856 return FALSE;
1859 if (dwFlags)
1861 SetLastError(NTE_BAD_FLAGS);
1862 return FALSE;
1865 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
1867 SetLastError(NTE_BAD_KEY);
1868 return FALSE;
1871 if (pCryptKey->dwState == RSAENH_KEYSTATE_IDLE)
1872 pCryptKey->dwState = RSAENH_KEYSTATE_ENCRYPTING;
1874 if (pCryptKey->dwState != RSAENH_KEYSTATE_ENCRYPTING)
1876 SetLastError(NTE_BAD_DATA);
1877 return FALSE;
1880 if (is_valid_handle(&handle_table, hHash, RSAENH_MAGIC_HASH)) {
1881 if (!RSAENH_CPHashData(hProv, hHash, pbData, *pdwDataLen, 0)) return FALSE;
1884 if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_BLOCK) {
1885 if (!Final && (*pdwDataLen % pCryptKey->dwBlockLen)) {
1886 SetLastError(NTE_BAD_DATA);
1887 return FALSE;
1890 dwEncryptedLen = (*pdwDataLen/pCryptKey->dwBlockLen+(Final?1:0))*pCryptKey->dwBlockLen;
1891 for (i=*pdwDataLen; i<dwEncryptedLen && i<dwBufLen; i++) pbData[i] = dwEncryptedLen - *pdwDataLen;
1892 *pdwDataLen = dwEncryptedLen;
1894 if (*pdwDataLen > dwBufLen)
1896 SetLastError(ERROR_MORE_DATA);
1897 return FALSE;
1900 for (i=0, in=pbData; i<*pdwDataLen; i+=pCryptKey->dwBlockLen, in+=pCryptKey->dwBlockLen) {
1901 switch (pCryptKey->dwMode) {
1902 case CRYPT_MODE_ECB:
1903 encrypt_block_impl(pCryptKey->aiAlgid, &pCryptKey->context, in, out,
1904 RSAENH_ENCRYPT);
1905 break;
1907 case CRYPT_MODE_CBC:
1908 for (j=0; j<pCryptKey->dwBlockLen; j++) in[j] ^= pCryptKey->abChainVector[j];
1909 encrypt_block_impl(pCryptKey->aiAlgid, &pCryptKey->context, in, out,
1910 RSAENH_ENCRYPT);
1911 memcpy(pCryptKey->abChainVector, out, pCryptKey->dwBlockLen);
1912 break;
1914 case CRYPT_MODE_CFB:
1915 for (j=0; j<pCryptKey->dwBlockLen; j++) {
1916 encrypt_block_impl(pCryptKey->aiAlgid, &pCryptKey->context,
1917 pCryptKey->abChainVector, o, RSAENH_ENCRYPT);
1918 out[j] = in[j] ^ o[0];
1919 for (k=0; k<pCryptKey->dwBlockLen-1; k++)
1920 pCryptKey->abChainVector[k] = pCryptKey->abChainVector[k+1];
1921 pCryptKey->abChainVector[k] = out[j];
1923 break;
1925 default:
1926 SetLastError(NTE_BAD_ALGID);
1927 return FALSE;
1929 memcpy(in, out, pCryptKey->dwBlockLen);
1931 } else if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_STREAM) {
1932 encrypt_stream_impl(pCryptKey->aiAlgid, &pCryptKey->context, pbData, *pdwDataLen);
1933 } else if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_RSA) {
1934 if (pCryptKey->aiAlgid == CALG_RSA_SIGN) {
1935 SetLastError(NTE_BAD_KEY);
1936 return FALSE;
1938 if (dwBufLen < pCryptKey->dwBlockLen) {
1939 SetLastError(ERROR_MORE_DATA);
1940 return FALSE;
1942 if (!pad_data(pbData, *pdwDataLen, pbData, pCryptKey->dwBlockLen, dwFlags)) return FALSE;
1943 encrypt_block_impl(pCryptKey->aiAlgid, &pCryptKey->context, pbData, pbData, RSAENH_ENCRYPT);
1944 *pdwDataLen = pCryptKey->dwBlockLen;
1945 Final = TRUE;
1946 } else {
1947 SetLastError(NTE_BAD_TYPE);
1948 return FALSE;
1951 if (Final) setup_key(pCryptKey);
1953 return TRUE;
1956 /******************************************************************************
1957 * CPDecrypt (RSAENH.@)
1959 * Decrypt data.
1961 * PARAMS
1962 * hProv [I] The key container hKey and hHash belong to.
1963 * hKey [I] The key used to decrypt the data.
1964 * hHash [I] An optional hash object for parallel hashing. See notes.
1965 * Final [I] Indicates if this is the last block of data to decrypt.
1966 * dwFlags [I] Currently no flags defined. Must be zero.
1967 * pbData [I/O] Pointer to the data to decrypt. Plaintext will also be stored there.
1968 * pdwDataLen [I/O] I: Length of ciphertext, O: Length of plaintext.
1970 * RETURNS
1971 * Success: TRUE.
1972 * Failure: FALSE.
1974 * NOTES
1975 * If a hash object handle is provided in hHash, it will be updated with the plaintext.
1976 * This is useful for message signatures.
1978 * This function uses the standard WINAPI protocol for querying data of dynamic length.
1980 BOOL WINAPI RSAENH_CPDecrypt(HCRYPTPROV hProv, HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final,
1981 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
1983 CRYPTKEY *pCryptKey;
1984 BYTE *in, out[RSAENH_MAX_BLOCK_SIZE], o[RSAENH_MAX_BLOCK_SIZE];
1985 DWORD i, j, k;
1986 DWORD dwMax;
1988 TRACE("(hProv=%08lx, hKey=%08lx, hHash=%08lx, Final=%d, dwFlags=%08lx, pbData=%p, "
1989 "pdwDataLen=%p)\n", hProv, hKey, hHash, Final, dwFlags, pbData, pdwDataLen);
1991 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
1993 SetLastError(NTE_BAD_UID);
1994 return FALSE;
1997 if (dwFlags)
1999 SetLastError(NTE_BAD_FLAGS);
2000 return FALSE;
2003 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
2005 SetLastError(NTE_BAD_KEY);
2006 return FALSE;
2009 if (pCryptKey->dwState == RSAENH_KEYSTATE_IDLE)
2010 pCryptKey->dwState = RSAENH_KEYSTATE_DECRYPTING;
2012 if (pCryptKey->dwState != RSAENH_KEYSTATE_DECRYPTING)
2014 SetLastError(NTE_BAD_DATA);
2015 return FALSE;
2018 dwMax=*pdwDataLen;
2020 if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_BLOCK) {
2021 for (i=0, in=pbData; i<*pdwDataLen; i+=pCryptKey->dwBlockLen, in+=pCryptKey->dwBlockLen) {
2022 switch (pCryptKey->dwMode) {
2023 case CRYPT_MODE_ECB:
2024 encrypt_block_impl(pCryptKey->aiAlgid, &pCryptKey->context, in, out,
2025 RSAENH_DECRYPT);
2026 break;
2028 case CRYPT_MODE_CBC:
2029 encrypt_block_impl(pCryptKey->aiAlgid, &pCryptKey->context, in, out,
2030 RSAENH_DECRYPT);
2031 for (j=0; j<pCryptKey->dwBlockLen; j++) out[j] ^= pCryptKey->abChainVector[j];
2032 memcpy(pCryptKey->abChainVector, in, pCryptKey->dwBlockLen);
2033 break;
2035 case CRYPT_MODE_CFB:
2036 for (j=0; j<pCryptKey->dwBlockLen; j++) {
2037 encrypt_block_impl(pCryptKey->aiAlgid, &pCryptKey->context,
2038 pCryptKey->abChainVector, o, RSAENH_ENCRYPT);
2039 out[j] = in[j] ^ o[0];
2040 for (k=0; k<pCryptKey->dwBlockLen-1; k++)
2041 pCryptKey->abChainVector[k] = pCryptKey->abChainVector[k+1];
2042 pCryptKey->abChainVector[k] = in[j];
2044 break;
2046 default:
2047 SetLastError(NTE_BAD_ALGID);
2048 return FALSE;
2050 memcpy(in, out, pCryptKey->dwBlockLen);
2052 if (Final) *pdwDataLen -= pbData[*pdwDataLen-1];
2054 } else if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_STREAM) {
2055 encrypt_stream_impl(pCryptKey->aiAlgid, &pCryptKey->context, pbData, *pdwDataLen);
2056 } else if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_RSA) {
2057 if (pCryptKey->aiAlgid == CALG_RSA_SIGN) {
2058 SetLastError(NTE_BAD_KEY);
2059 return FALSE;
2061 encrypt_block_impl(pCryptKey->aiAlgid, &pCryptKey->context, pbData, pbData, RSAENH_DECRYPT);
2062 if (!unpad_data(pbData, pCryptKey->dwBlockLen, pbData, pdwDataLen, dwFlags)) return FALSE;
2063 Final = TRUE;
2064 } else {
2065 SetLastError(NTE_BAD_TYPE);
2066 return FALSE;
2069 if (Final) setup_key(pCryptKey);
2071 if (is_valid_handle(&handle_table, hHash, RSAENH_MAGIC_HASH)) {
2072 if (*pdwDataLen>dwMax ||
2073 !RSAENH_CPHashData(hProv, hHash, pbData, *pdwDataLen, 0)) return FALSE;
2076 return TRUE;
2079 /******************************************************************************
2080 * CPExportKey (RSAENH.@)
2082 * Export a key into a binary large object (BLOB).
2084 * PARAMS
2085 * hProv [I] Key container from which a key is to be exported.
2086 * hKey [I] Key to be exported.
2087 * hPubKey [I] Key used to encrypt sensitive BLOB data.
2088 * dwBlobType [I] SIMPLEBLOB, PUBLICKEYBLOB or PRIVATEKEYBLOB.
2089 * dwFlags [I] Currently none defined.
2090 * pbData [O] Pointer to a buffer where the BLOB will be written to.
2091 * pdwDataLen [I/O] I: Size of buffer at pbData, O: Size of BLOB
2093 * RETURNS
2094 * Success: TRUE.
2095 * Failure: FALSE.
2097 BOOL WINAPI RSAENH_CPExportKey(HCRYPTPROV hProv, HCRYPTKEY hKey, HCRYPTKEY hPubKey,
2098 DWORD dwBlobType, DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
2100 CRYPTKEY *pCryptKey, *pPubKey;
2101 BLOBHEADER *pBlobHeader = (BLOBHEADER*)pbData;
2102 RSAPUBKEY *pRSAPubKey = (RSAPUBKEY*)(pBlobHeader+1);
2103 ALG_ID *pAlgid = (ALG_ID*)(pBlobHeader+1);
2104 DWORD dwDataLen;
2106 TRACE("(hProv=%08lx, hKey=%08lx, hPubKey=%08lx, dwBlobType=%08lx, dwFlags=%08lx, pbData=%p,"
2107 "pdwDataLen=%p)\n", hProv, hKey, hPubKey, dwBlobType, dwFlags, pbData, pdwDataLen);
2109 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
2111 SetLastError(NTE_BAD_UID);
2112 return FALSE;
2115 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
2117 SetLastError(NTE_BAD_KEY);
2118 return FALSE;
2121 if (dwFlags & CRYPT_SSL2_FALLBACK) {
2122 if (pCryptKey->aiAlgid != CALG_SSL2_MASTER) {
2123 SetLastError(NTE_BAD_KEY);
2124 return FALSE;
2128 switch ((BYTE)dwBlobType)
2130 case SIMPLEBLOB:
2131 if (!lookup_handle(&handle_table, hPubKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pPubKey)){
2132 SetLastError(NTE_BAD_PUBLIC_KEY); /* FIXME: error_code? */
2133 return FALSE;
2136 if (!(GET_ALG_CLASS(pCryptKey->aiAlgid)&(ALG_CLASS_DATA_ENCRYPT|ALG_CLASS_MSG_ENCRYPT))) {
2137 SetLastError(NTE_BAD_KEY); /* FIXME: error code? */
2138 return FALSE;
2141 dwDataLen = sizeof(BLOBHEADER) + sizeof(ALG_ID) + pPubKey->dwBlockLen;
2142 if (pbData) {
2143 if (*pdwDataLen < dwDataLen) {
2144 SetLastError(ERROR_MORE_DATA);
2145 *pdwDataLen = dwDataLen;
2146 return FALSE;
2149 pBlobHeader->bType = SIMPLEBLOB;
2150 pBlobHeader->bVersion = CUR_BLOB_VERSION;
2151 pBlobHeader->reserved = 0;
2152 pBlobHeader->aiKeyAlg = pCryptKey->aiAlgid;
2154 *pAlgid = pPubKey->aiAlgid;
2156 if (!pad_data(pCryptKey->abKeyValue, pCryptKey->dwKeyLen, (BYTE*)(pAlgid+1),
2157 pPubKey->dwBlockLen, dwFlags))
2159 return FALSE;
2162 encrypt_block_impl(pPubKey->aiAlgid, &pPubKey->context, (BYTE*)(pAlgid+1),
2163 (BYTE*)(pAlgid+1), RSAENH_ENCRYPT);
2165 *pdwDataLen = dwDataLen;
2166 return TRUE;
2168 case PUBLICKEYBLOB:
2169 if (is_valid_handle(&handle_table, hPubKey, RSAENH_MAGIC_KEY)) {
2170 SetLastError(NTE_BAD_KEY); /* FIXME: error code? */
2171 return FALSE;
2174 if ((pCryptKey->aiAlgid != CALG_RSA_KEYX) && (pCryptKey->aiAlgid != CALG_RSA_SIGN)) {
2175 SetLastError(NTE_BAD_KEY);
2176 return FALSE;
2179 dwDataLen = sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) + pCryptKey->dwKeyLen;
2180 if (pbData) {
2181 if (*pdwDataLen < dwDataLen) {
2182 SetLastError(ERROR_MORE_DATA);
2183 *pdwDataLen = dwDataLen;
2184 return FALSE;
2187 pBlobHeader->bType = PUBLICKEYBLOB;
2188 pBlobHeader->bVersion = CUR_BLOB_VERSION;
2189 pBlobHeader->reserved = 0;
2190 pBlobHeader->aiKeyAlg = pCryptKey->aiAlgid;
2192 pRSAPubKey->magic = RSAENH_MAGIC_RSA1;
2193 pRSAPubKey->bitlen = pCryptKey->dwKeyLen << 3;
2195 export_public_key_impl((BYTE*)(pRSAPubKey+1), &pCryptKey->context,
2196 pCryptKey->dwKeyLen, &pRSAPubKey->pubexp);
2198 *pdwDataLen = dwDataLen;
2199 return TRUE;
2201 case PRIVATEKEYBLOB:
2202 if ((pCryptKey->aiAlgid != CALG_RSA_KEYX) && (pCryptKey->aiAlgid != CALG_RSA_SIGN)) {
2203 SetLastError(NTE_BAD_KEY);
2204 return FALSE;
2207 dwDataLen = sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) +
2208 2 * pCryptKey->dwKeyLen + 5 * ((pCryptKey->dwKeyLen + 1) >> 1);
2209 if (pbData) {
2210 if (*pdwDataLen < dwDataLen) {
2211 SetLastError(ERROR_MORE_DATA);
2212 *pdwDataLen = dwDataLen;
2213 return FALSE;
2216 pBlobHeader->bType = PRIVATEKEYBLOB;
2217 pBlobHeader->bVersion = CUR_BLOB_VERSION;
2218 pBlobHeader->reserved = 0;
2219 pBlobHeader->aiKeyAlg = pCryptKey->aiAlgid;
2221 pRSAPubKey->magic = RSAENH_MAGIC_RSA2;
2222 pRSAPubKey->bitlen = pCryptKey->dwKeyLen << 3;
2224 export_private_key_impl((BYTE*)(pRSAPubKey+1), &pCryptKey->context,
2225 pCryptKey->dwKeyLen, &pRSAPubKey->pubexp);
2227 *pdwDataLen = dwDataLen;
2228 return TRUE;
2230 default:
2231 SetLastError(NTE_BAD_TYPE); /* FIXME: error code? */
2232 return FALSE;
2236 /******************************************************************************
2237 * CPImportKey (RSAENH.@)
2239 * Import a BLOB'ed key into a key container.
2241 * PARAMS
2242 * hProv [I] Key container into which the key is to be imported.
2243 * pbData [I] Pointer to a buffer which holds the BLOB.
2244 * dwDataLen [I] Length of data in buffer at pbData.
2245 * hPubKey [I] Key used to decrypt sensitive BLOB data.
2246 * dwFlags [I] Currently none defined.
2247 * phKey [O] Handle to the imported key.
2249 * RETURNS
2250 * Success: TRUE.
2251 * Failure: FALSE.
2253 BOOL WINAPI RSAENH_CPImportKey(HCRYPTPROV hProv, CONST BYTE *pbData, DWORD dwDataLen,
2254 HCRYPTKEY hPubKey, DWORD dwFlags, HCRYPTKEY *phKey)
2256 CRYPTKEY *pCryptKey, *pPubKey;
2257 CONST BLOBHEADER *pBlobHeader = (CONST BLOBHEADER*)pbData;
2258 CONST RSAPUBKEY *pRSAPubKey = (CONST RSAPUBKEY*)(pBlobHeader+1);
2259 CONST ALG_ID *pAlgid = (CONST ALG_ID*)(pBlobHeader+1);
2260 CONST BYTE *pbKeyStream = (CONST BYTE*)(pAlgid + 1);
2261 ALG_ID algID;
2262 BYTE *pbDecrypted;
2263 DWORD dwKeyLen;
2265 TRACE("(hProv=%08lx, pbData=%p, dwDataLen=%ld, hPubKey=%08lx, dwFlags=%08lx, phKey=%p)\n",
2266 hProv, pbData, dwDataLen, hPubKey, dwFlags, phKey);
2268 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
2270 SetLastError(NTE_BAD_UID);
2271 return FALSE;
2274 if (dwDataLen < sizeof(BLOBHEADER) ||
2275 pBlobHeader->bVersion != CUR_BLOB_VERSION ||
2276 pBlobHeader->reserved != 0)
2278 SetLastError(NTE_BAD_DATA);
2279 return FALSE;
2282 switch (pBlobHeader->bType)
2284 case PRIVATEKEYBLOB:
2285 if ((dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY)) ||
2286 (pRSAPubKey->magic != RSAENH_MAGIC_RSA2) ||
2287 (dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) +
2288 (2 * pRSAPubKey->bitlen >> 3) + (5 * ((pRSAPubKey->bitlen+8)>>4))))
2290 SetLastError(NTE_BAD_DATA);
2291 return FALSE;
2294 *phKey = new_key(hProv, pBlobHeader->aiKeyAlg, MAKELONG(0,pRSAPubKey->bitlen), &pCryptKey);
2295 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) return FALSE;
2296 setup_key(pCryptKey);
2297 return import_private_key_impl((CONST BYTE*)(pRSAPubKey+1), &pCryptKey->context,
2298 pRSAPubKey->bitlen/8, pRSAPubKey->pubexp);
2300 case PUBLICKEYBLOB:
2301 if ((dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY)) ||
2302 (pRSAPubKey->magic != RSAENH_MAGIC_RSA1) ||
2303 (dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) + (pRSAPubKey->bitlen >> 3)))
2305 SetLastError(NTE_BAD_DATA);
2306 return FALSE;
2309 /* Since this is a public key blob, only the public key is
2310 * available, so only signature verification is possible.
2312 algID = pBlobHeader->aiKeyAlg;
2313 if (algID == CALG_RSA_KEYX)
2314 algID = CALG_RSA_SIGN;
2315 *phKey = new_key(hProv, algID, MAKELONG(0,pRSAPubKey->bitlen), &pCryptKey);
2316 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) return FALSE;
2317 setup_key(pCryptKey);
2318 return import_public_key_impl((CONST BYTE*)(pRSAPubKey+1), &pCryptKey->context,
2319 pRSAPubKey->bitlen >> 3, pRSAPubKey->pubexp);
2321 case SIMPLEBLOB:
2322 if (!lookup_handle(&handle_table, hPubKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pPubKey) ||
2323 pPubKey->aiAlgid != CALG_RSA_KEYX)
2325 SetLastError(NTE_BAD_PUBLIC_KEY); /* FIXME: error code? */
2326 return FALSE;
2329 if (dwDataLen < sizeof(BLOBHEADER)+sizeof(ALG_ID)+pPubKey->dwBlockLen)
2331 SetLastError(NTE_BAD_DATA); /* FIXME: error code */
2332 return FALSE;
2335 pbDecrypted = HeapAlloc(GetProcessHeap(), 0, pPubKey->dwBlockLen);
2336 if (!pbDecrypted) return FALSE;
2337 encrypt_block_impl(pPubKey->aiAlgid, &pPubKey->context, pbKeyStream, pbDecrypted,
2338 RSAENH_DECRYPT);
2340 dwKeyLen = RSAENH_MAX_KEY_SIZE;
2341 if (!unpad_data(pbDecrypted, pPubKey->dwBlockLen, pbDecrypted, &dwKeyLen, dwFlags)) {
2342 HeapFree(GetProcessHeap(), 0, pbDecrypted);
2343 return FALSE;
2346 *phKey = new_key(hProv, pBlobHeader->aiKeyAlg, dwKeyLen<<19, &pCryptKey);
2347 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE)
2349 HeapFree(GetProcessHeap(), 0, pbDecrypted);
2350 return FALSE;
2352 memcpy(pCryptKey->abKeyValue, pbDecrypted, dwKeyLen);
2353 HeapFree(GetProcessHeap(), 0, pbDecrypted);
2354 setup_key(pCryptKey);
2355 return TRUE;
2357 default:
2358 SetLastError(NTE_BAD_TYPE); /* FIXME: error code? */
2359 return FALSE;
2363 /******************************************************************************
2364 * CPGenKey (RSAENH.@)
2366 * Generate a key in the key container
2368 * PARAMS
2369 * hProv [I] Key container for which a key is to be generated.
2370 * Algid [I] Crypto algorithm identifier for the key to be generated.
2371 * dwFlags [I] Upper 16 bits: Binary length of key. Lower 16 bits: Flags. See Notes
2372 * phKey [O] Handle to the generated key.
2374 * RETURNS
2375 * Success: TRUE.
2376 * Failure: FALSE.
2378 * FIXME
2379 * Flags currently not considered.
2381 * NOTES
2382 * Private key-exchange- and signature-keys can be generated with Algid AT_KEYEXCHANGE
2383 * and AT_SIGNATURE values.
2385 BOOL WINAPI RSAENH_CPGenKey(HCRYPTPROV hProv, ALG_ID Algid, DWORD dwFlags, HCRYPTKEY *phKey)
2387 KEYCONTAINER *pKeyContainer;
2388 CRYPTKEY *pCryptKey;
2390 TRACE("(hProv=%08lx, aiAlgid=%d, dwFlags=%08lx, phKey=%p)\n", hProv, Algid, dwFlags, phKey);
2392 if (!lookup_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER,
2393 (OBJECTHDR**)&pKeyContainer))
2395 /* MSDN: hProv not containing valid context handle */
2396 SetLastError(NTE_BAD_UID);
2397 return FALSE;
2400 switch (Algid)
2402 case AT_SIGNATURE:
2403 case CALG_RSA_SIGN:
2404 *phKey = new_key(hProv, CALG_RSA_SIGN, dwFlags, &pCryptKey);
2405 if (pCryptKey) {
2406 new_key_impl(pCryptKey->aiAlgid, &pCryptKey->context, pCryptKey->dwKeyLen);
2407 setup_key(pCryptKey);
2408 if (Algid == AT_SIGNATURE) {
2409 RSAENH_CPDestroyKey(hProv, pKeyContainer->hSignatureKeyPair);
2410 copy_handle(&handle_table, *phKey, RSAENH_MAGIC_KEY,
2411 (unsigned int*)&pKeyContainer->hSignatureKeyPair);
2414 break;
2416 case AT_KEYEXCHANGE:
2417 case CALG_RSA_KEYX:
2418 *phKey = new_key(hProv, CALG_RSA_KEYX, dwFlags, &pCryptKey);
2419 if (pCryptKey) {
2420 new_key_impl(pCryptKey->aiAlgid, &pCryptKey->context, pCryptKey->dwKeyLen);
2421 setup_key(pCryptKey);
2422 if (Algid == AT_KEYEXCHANGE) {
2423 RSAENH_CPDestroyKey(hProv, pKeyContainer->hKeyExchangeKeyPair);
2424 copy_handle(&handle_table, *phKey, RSAENH_MAGIC_KEY,
2425 (unsigned int*)&pKeyContainer->hKeyExchangeKeyPair);
2428 break;
2430 case CALG_RC2:
2431 case CALG_RC4:
2432 case CALG_DES:
2433 case CALG_3DES_112:
2434 case CALG_3DES:
2435 case CALG_PCT1_MASTER:
2436 case CALG_SSL2_MASTER:
2437 case CALG_SSL3_MASTER:
2438 case CALG_TLS1_MASTER:
2439 *phKey = new_key(hProv, Algid, dwFlags, &pCryptKey);
2440 if (pCryptKey) {
2441 gen_rand_impl(pCryptKey->abKeyValue, RSAENH_MAX_KEY_SIZE);
2442 switch (Algid) {
2443 case CALG_SSL3_MASTER:
2444 pCryptKey->abKeyValue[0] = RSAENH_SSL3_VERSION_MAJOR;
2445 pCryptKey->abKeyValue[1] = RSAENH_SSL3_VERSION_MINOR;
2446 break;
2448 case CALG_TLS1_MASTER:
2449 pCryptKey->abKeyValue[0] = RSAENH_TLS1_VERSION_MAJOR;
2450 pCryptKey->abKeyValue[1] = RSAENH_TLS1_VERSION_MINOR;
2451 break;
2453 setup_key(pCryptKey);
2455 break;
2457 default:
2458 /* MSDN: Algorithm not supported specified by Algid */
2459 SetLastError(NTE_BAD_ALGID);
2460 return FALSE;
2463 return *phKey != (unsigned int)INVALID_HANDLE_VALUE;
2466 /******************************************************************************
2467 * CPGenRandom (RSAENH.@)
2469 * Generate a random byte stream.
2471 * PARAMS
2472 * hProv [I] Key container that is used to generate random bytes.
2473 * dwLen [I] Specifies the number of requested random data bytes.
2474 * pbBuffer [O] Random bytes will be stored here.
2476 * RETURNS
2477 * Success: TRUE
2478 * Failure: FALSE
2480 BOOL WINAPI RSAENH_CPGenRandom(HCRYPTPROV hProv, DWORD dwLen, BYTE *pbBuffer)
2482 TRACE("(hProv=%08lx, dwLen=%ld, pbBuffer=%p)\n", hProv, dwLen, pbBuffer);
2484 if (!is_valid_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER))
2486 /* MSDN: hProv not containing valid context handle */
2487 SetLastError(NTE_BAD_UID);
2488 return FALSE;
2491 return gen_rand_impl(pbBuffer, dwLen);
2494 /******************************************************************************
2495 * CPGetHashParam (RSAENH.@)
2497 * Query parameters of an hash object.
2499 * PARAMS
2500 * hProv [I] The kea container, which the hash belongs to.
2501 * hHash [I] The hash object that is to be queried.
2502 * dwParam [I] Specifies the parameter that is to be queried.
2503 * pbData [I] Pointer to the buffer where the parameter value will be stored.
2504 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
2505 * dwFlags [I] None currently defined.
2507 * RETURNS
2508 * Success: TRUE
2509 * Failure: FALSE
2511 * NOTES
2512 * Valid dwParams are: HP_ALGID, HP_HASHSIZE, HP_HASHVALUE. The hash will be
2513 * finalized if HP_HASHVALUE is queried.
2515 BOOL WINAPI RSAENH_CPGetHashParam(HCRYPTPROV hProv, HCRYPTHASH hHash, DWORD dwParam, BYTE *pbData,
2516 DWORD *pdwDataLen, DWORD dwFlags)
2518 CRYPTHASH *pCryptHash;
2520 TRACE("(hProv=%08lx, hHash=%08lx, dwParam=%08lx, pbData=%p, pdwDataLen=%p, dwFlags=%08lx)\n",
2521 hProv, hHash, dwParam, pbData, pdwDataLen, dwFlags);
2523 if (!is_valid_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER))
2525 SetLastError(NTE_BAD_UID);
2526 return FALSE;
2529 if (dwFlags)
2531 SetLastError(NTE_BAD_FLAGS);
2532 return FALSE;
2535 if (!lookup_handle(&handle_table, (unsigned int)hHash, RSAENH_MAGIC_HASH,
2536 (OBJECTHDR**)&pCryptHash))
2538 SetLastError(NTE_BAD_HASH);
2539 return FALSE;
2542 if (!pdwDataLen)
2544 SetLastError(ERROR_INVALID_PARAMETER);
2545 return FALSE;
2548 switch (dwParam)
2550 case HP_ALGID:
2551 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptHash->aiAlgid,
2552 sizeof(ALG_ID));
2554 case HP_HASHSIZE:
2555 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptHash->dwHashSize,
2556 sizeof(DWORD));
2558 case HP_HASHVAL:
2559 if (pCryptHash->aiAlgid == CALG_TLS1PRF) {
2560 return tls1_prf(hProv, pCryptHash->hKey, &pCryptHash->tpPRFParams.blobLabel,
2561 &pCryptHash->tpPRFParams.blobSeed, pbData, *pdwDataLen);
2564 if (pCryptHash->dwState == RSAENH_HASHSTATE_IDLE) {
2565 SetLastError(NTE_BAD_HASH_STATE);
2566 return FALSE;
2569 if (pbData && (pCryptHash->dwState != RSAENH_HASHSTATE_FINISHED))
2571 finalize_hash(pCryptHash);
2572 pCryptHash->dwState = RSAENH_HASHSTATE_FINISHED;
2575 return copy_param(pbData, pdwDataLen, (CONST BYTE*)pCryptHash->abHashValue,
2576 pCryptHash->dwHashSize);
2578 default:
2579 SetLastError(NTE_BAD_TYPE);
2580 return FALSE;
2584 /******************************************************************************
2585 * CPSetKeyParam (RSAENH.@)
2587 * Set a parameter of a key object
2589 * PARAMS
2590 * hProv [I] The key container to which the key belongs.
2591 * hKey [I] The key for which a parameter is to be set.
2592 * dwParam [I] Parameter type. See Notes.
2593 * pbData [I] Pointer to the parameter value.
2594 * dwFlags [I] Currently none defined.
2596 * RETURNS
2597 * Success: TRUE.
2598 * Failure: FALSE.
2600 * NOTES:
2601 * Defined dwParam types are:
2602 * - KP_MODE: Values MODE_CBC, MODE_ECB, MODE_CFB.
2603 * - KP_MODE_BITS: Shift width for cipher feedback mode. (Currently ignored by MS CSP's)
2604 * - KP_PERMISSIONS: Or'ed combination of CRYPT_ENCRYPT, CRYPT_DECRYPT,
2605 * CRYPT_EXPORT, CRYPT_READ, CRYPT_WRITE, CRYPT_MAC
2606 * - KP_IV: Initialization vector
2608 BOOL WINAPI RSAENH_CPSetKeyParam(HCRYPTPROV hProv, HCRYPTKEY hKey, DWORD dwParam, BYTE *pbData,
2609 DWORD dwFlags)
2611 CRYPTKEY *pCryptKey;
2613 TRACE("(hProv=%08lx, hKey=%08lx, dwParam=%08lx, pbData=%p, dwFlags=%08lx)\n", hProv, hKey,
2614 dwParam, pbData, dwFlags);
2616 if (!is_valid_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER))
2618 SetLastError(NTE_BAD_UID);
2619 return FALSE;
2622 if (dwFlags) {
2623 SetLastError(NTE_BAD_FLAGS);
2624 return FALSE;
2627 if (!lookup_handle(&handle_table, (unsigned int)hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
2629 SetLastError(NTE_BAD_KEY);
2630 return FALSE;
2633 switch (dwParam) {
2634 case KP_MODE:
2635 pCryptKey->dwMode = *(DWORD*)pbData;
2636 return TRUE;
2638 case KP_MODE_BITS:
2639 pCryptKey->dwModeBits = *(DWORD*)pbData;
2640 return TRUE;
2642 case KP_PERMISSIONS:
2643 pCryptKey->dwPermissions = *(DWORD*)pbData;
2644 return TRUE;
2646 case KP_IV:
2647 memcpy(pCryptKey->abInitVector, pbData, pCryptKey->dwBlockLen);
2648 return TRUE;
2650 case KP_SCHANNEL_ALG:
2651 switch (((PSCHANNEL_ALG)pbData)->dwUse) {
2652 case SCHANNEL_ENC_KEY:
2653 memcpy(&pCryptKey->siSChannelInfo.saEncAlg, pbData, sizeof(SCHANNEL_ALG));
2654 break;
2656 case SCHANNEL_MAC_KEY:
2657 memcpy(&pCryptKey->siSChannelInfo.saMACAlg, pbData, sizeof(SCHANNEL_ALG));
2658 break;
2660 default:
2661 SetLastError(NTE_FAIL); /* FIXME: error code */
2662 return FALSE;
2664 return TRUE;
2666 case KP_CLIENT_RANDOM:
2667 return copy_data_blob(&pCryptKey->siSChannelInfo.blobClientRandom, (PCRYPT_DATA_BLOB)pbData);
2669 case KP_SERVER_RANDOM:
2670 return copy_data_blob(&pCryptKey->siSChannelInfo.blobServerRandom, (PCRYPT_DATA_BLOB)pbData);
2672 default:
2673 SetLastError(NTE_BAD_TYPE);
2674 return FALSE;
2678 /******************************************************************************
2679 * CPGetKeyParam (RSAENH.@)
2681 * Query a key parameter.
2683 * PARAMS
2684 * hProv [I] The key container, which the key belongs to.
2685 * hHash [I] The key object that is to be queried.
2686 * dwParam [I] Specifies the parameter that is to be queried.
2687 * pbData [I] Pointer to the buffer where the parameter value will be stored.
2688 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
2689 * dwFlags [I] None currently defined.
2691 * RETURNS
2692 * Success: TRUE
2693 * Failure: FALSE
2695 * NOTES
2696 * Defined dwParam types are:
2697 * - KP_MODE: Values MODE_CBC, MODE_ECB, MODE_CFB.
2698 * - KP_MODE_BITS: Shift width for cipher feedback mode.
2699 * (Currently ignored by MS CSP's - always eight)
2700 * - KP_PERMISSIONS: Or'ed combination of CRYPT_ENCRYPT, CRYPT_DECRYPT,
2701 * CRYPT_EXPORT, CRYPT_READ, CRYPT_WRITE, CRYPT_MAC
2702 * - KP_IV: Initialization vector.
2703 * - KP_KEYLEN: Bitwidth of the key.
2704 * - KP_BLOCKLEN: Size of a block cipher block.
2705 * - KP_SALT: Salt value.
2707 BOOL WINAPI RSAENH_CPGetKeyParam(HCRYPTPROV hProv, HCRYPTKEY hKey, DWORD dwParam, BYTE *pbData,
2708 DWORD *pdwDataLen, DWORD dwFlags)
2710 CRYPTKEY *pCryptKey;
2711 DWORD dwBitLen;
2713 TRACE("(hProv=%08lx, hKey=%08lx, dwParam=%08lx, pbData=%p, pdwDataLen=%p dwFlags=%08lx)\n",
2714 hProv, hKey, dwParam, pbData, pdwDataLen, dwFlags);
2716 if (!is_valid_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER))
2718 SetLastError(NTE_BAD_UID);
2719 return FALSE;
2722 if (dwFlags) {
2723 SetLastError(NTE_BAD_FLAGS);
2724 return FALSE;
2727 if (!lookup_handle(&handle_table, (unsigned int)hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
2729 SetLastError(NTE_BAD_KEY);
2730 return FALSE;
2733 switch (dwParam)
2735 case KP_IV:
2736 return copy_param(pbData, pdwDataLen, (CONST BYTE*)pCryptKey->abInitVector,
2737 pCryptKey->dwBlockLen);
2739 case KP_SALT:
2740 return copy_param(pbData, pdwDataLen,
2741 (CONST BYTE*)&pCryptKey->abKeyValue[pCryptKey->dwKeyLen], pCryptKey->dwSaltLen);
2743 case KP_KEYLEN:
2744 dwBitLen = pCryptKey->dwKeyLen << 3;
2745 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwBitLen, sizeof(DWORD));
2747 case KP_BLOCKLEN:
2748 dwBitLen = pCryptKey->dwBlockLen << 3;
2749 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwBitLen, sizeof(DWORD));
2751 case KP_MODE:
2752 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptKey->dwMode, sizeof(DWORD));
2754 case KP_MODE_BITS:
2755 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptKey->dwModeBits,
2756 sizeof(DWORD));
2758 case KP_PERMISSIONS:
2759 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptKey->dwPermissions,
2760 sizeof(DWORD));
2762 case KP_ALGID:
2763 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptKey->aiAlgid, sizeof(DWORD));
2765 default:
2766 SetLastError(NTE_BAD_TYPE);
2767 return FALSE;
2771 /******************************************************************************
2772 * CPGetProvParam (RSAENH.@)
2774 * Query a CSP parameter.
2776 * PARAMS
2777 * hProv [I] The key container that is to be queried.
2778 * dwParam [I] Specifies the parameter that is to be queried.
2779 * pbData [I] Pointer to the buffer where the parameter value will be stored.
2780 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
2781 * dwFlags [I] CRYPT_FIRST: Start enumeration (for PP_ENUMALGS{_EX}).
2783 * RETURNS
2784 * Success: TRUE
2785 * Failure: FALSE
2786 * NOTES:
2787 * Defined dwParam types:
2788 * - PP_CONTAINER: Name of the key container.
2789 * - PP_NAME: Name of the cryptographic service provider.
2790 * - PP_SIG_KEYSIZE_INC: RSA signature keywidth granularity in bits.
2791 * - PP_KEYX_KEYSIZE_INC: RSA key-exchange keywidth granularity in bits.
2792 * - PP_ENUMALGS{_EX}: Query provider capabilities.
2794 BOOL WINAPI RSAENH_CPGetProvParam(HCRYPTPROV hProv, DWORD dwParam, BYTE *pbData,
2795 DWORD *pdwDataLen, DWORD dwFlags)
2797 KEYCONTAINER *pKeyContainer;
2798 PROV_ENUMALGS provEnumalgs;
2799 DWORD dwTemp;
2800 CHAR szRSABase[MAX_PATH];
2801 HKEY hKey, hRootKey;
2803 /* This is for dwParam 41, which does not seem to be documented
2804 * on MSDN. IE6 SP1 asks for it in the 'About' dialog, however.
2805 * Returning this BLOB seems to satisfy IE. The marked 0x00 seem
2806 * to be 'don't care's. If you know anything more specific about
2807 * provider parameter 41, please report to wine-devel@winehq.org */
2808 static CONST BYTE abWTF[96] = {
2809 0xb0, 0x25, 0x63, 0x86, 0x9c, 0xab, 0xb6, 0x37,
2810 0xe8, 0x82, /**/0x00,/**/ 0x72, 0x06, 0xb2, /**/0x00,/**/ 0x3b,
2811 0x60, 0x35, /**/0x00,/**/ 0x3b, 0x88, 0xce, /**/0x00,/**/ 0x82,
2812 0xbc, 0x7a, /**/0x00,/**/ 0xb7, 0x4f, 0x7e, /**/0x00,/**/ 0xde,
2813 0x92, 0xf1, /**/0x00,/**/ 0x83, 0xea, 0x5e, /**/0x00,/**/ 0xc8,
2814 0x12, 0x1e, 0xd4, 0x06, 0xf7, 0x66, /**/0x00,/**/ 0x01,
2815 0x29, 0xa4, /**/0x00,/**/ 0xf8, 0x24, 0x0c, /**/0x00,/**/ 0x33,
2816 0x06, 0x80, /**/0x00,/**/ 0x02, 0x46, 0x0b, /**/0x00,/**/ 0x6d,
2817 0x5b, 0xca, /**/0x00,/**/ 0x9a, 0x10, 0xf0, /**/0x00,/**/ 0x05,
2818 0x19, 0xd0, /**/0x00,/**/ 0x2c, 0xf6, 0x27, /**/0x00,/**/ 0xaa,
2819 0x7c, 0x6f, /**/0x00,/**/ 0xb9, 0xd8, 0x72, /**/0x00,/**/ 0x03,
2820 0xf3, 0x81, /**/0x00,/**/ 0xfa, 0xe8, 0x26, /**/0x00,/**/ 0xca
2823 TRACE("(hProv=%08lx, dwParam=%08lx, pbData=%p, pdwDataLen=%p, dwFlags=%08lx)\n",
2824 hProv, dwParam, pbData, pdwDataLen, dwFlags);
2826 if (!pdwDataLen) {
2827 SetLastError(ERROR_INVALID_PARAMETER);
2828 return FALSE;
2831 if (!lookup_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER,
2832 (OBJECTHDR**)&pKeyContainer))
2834 /* MSDN: hProv not containing valid context handle */
2835 SetLastError(NTE_BAD_UID);
2836 return FALSE;
2839 switch (dwParam)
2841 case PP_CONTAINER:
2842 return copy_param(pbData, pdwDataLen, (CONST BYTE*)pKeyContainer->szName,
2843 strlen(pKeyContainer->szName)+1);
2845 case PP_NAME:
2846 return copy_param(pbData, pdwDataLen, (CONST BYTE*)pKeyContainer->szProvName,
2847 strlen(pKeyContainer->szProvName)+1);
2849 case PP_SIG_KEYSIZE_INC:
2850 case PP_KEYX_KEYSIZE_INC:
2851 dwTemp = 8;
2852 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwTemp, sizeof(dwTemp));
2854 case PP_IMPTYPE:
2855 dwTemp = CRYPT_IMPL_SOFTWARE;
2856 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwTemp, sizeof(dwTemp));
2858 case PP_VERSION:
2859 dwTemp = 0x00000200;
2860 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwTemp, sizeof(dwTemp));
2862 case PP_ENUMCONTAINERS:
2863 if ((dwFlags & CRYPT_FIRST) == CRYPT_FIRST) pKeyContainer->dwEnumContainersCtr = 0;
2865 if (!pbData) {
2866 *pdwDataLen = (DWORD)MAX_PATH + 1;
2867 return TRUE;
2870 sprintf(szRSABase, RSAENH_REGKEY, "");
2872 if (dwFlags & CRYPT_MACHINE_KEYSET) {
2873 hRootKey = HKEY_LOCAL_MACHINE;
2874 } else {
2875 hRootKey = HKEY_CURRENT_USER;
2878 if (RegOpenKeyExA(hRootKey, szRSABase, 0, KEY_READ, &hKey) != ERROR_SUCCESS)
2880 SetLastError(ERROR_NO_MORE_ITEMS);
2881 return FALSE;
2884 dwTemp = *pdwDataLen;
2885 switch (RegEnumKeyExA(hKey, pKeyContainer->dwEnumContainersCtr, (LPSTR)pbData, &dwTemp,
2886 NULL, NULL, NULL, NULL))
2888 case ERROR_MORE_DATA:
2889 *pdwDataLen = (DWORD)MAX_PATH + 1;
2891 case ERROR_SUCCESS:
2892 pKeyContainer->dwEnumContainersCtr++;
2893 RegCloseKey(hKey);
2894 return TRUE;
2896 case ERROR_NO_MORE_ITEMS:
2897 default:
2898 SetLastError(ERROR_NO_MORE_ITEMS);
2899 RegCloseKey(hKey);
2900 return FALSE;
2903 case PP_ENUMALGS:
2904 case PP_ENUMALGS_EX:
2905 if (((pKeyContainer->dwEnumAlgsCtr >= RSAENH_MAX_ENUMALGS-1) ||
2906 (!aProvEnumAlgsEx[pKeyContainer->dwPersonality]
2907 [pKeyContainer->dwEnumAlgsCtr+1].aiAlgid)) &&
2908 ((dwFlags & CRYPT_FIRST) != CRYPT_FIRST))
2910 SetLastError(ERROR_NO_MORE_ITEMS);
2911 return FALSE;
2914 if (dwParam == PP_ENUMALGS) {
2915 if (pbData && (*pdwDataLen >= sizeof(PROV_ENUMALGS)))
2916 pKeyContainer->dwEnumAlgsCtr = ((dwFlags & CRYPT_FIRST) == CRYPT_FIRST) ?
2917 0 : pKeyContainer->dwEnumAlgsCtr+1;
2919 provEnumalgs.aiAlgid = aProvEnumAlgsEx
2920 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr].aiAlgid;
2921 provEnumalgs.dwBitLen = aProvEnumAlgsEx
2922 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr].dwDefaultLen;
2923 provEnumalgs.dwNameLen = aProvEnumAlgsEx
2924 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr].dwNameLen;
2925 memcpy(provEnumalgs.szName, aProvEnumAlgsEx
2926 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr].szName,
2927 20*sizeof(CHAR));
2929 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&provEnumalgs,
2930 sizeof(PROV_ENUMALGS));
2931 } else {
2932 if (pbData && (*pdwDataLen >= sizeof(PROV_ENUMALGS_EX)))
2933 pKeyContainer->dwEnumAlgsCtr = ((dwFlags & CRYPT_FIRST) == CRYPT_FIRST) ?
2934 0 : pKeyContainer->dwEnumAlgsCtr+1;
2936 return copy_param(pbData, pdwDataLen,
2937 (CONST BYTE*)&aProvEnumAlgsEx
2938 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr],
2939 sizeof(PROV_ENUMALGS_EX));
2942 case 41: /* Undocumented. Asked for by IE About dialog */
2943 return copy_param(pbData, pdwDataLen, abWTF, sizeof(abWTF));
2945 default:
2946 /* MSDN: Unknown parameter number in dwParam */
2947 SetLastError(NTE_BAD_TYPE);
2948 return FALSE;
2952 /******************************************************************************
2953 * CPDeriveKey (RSAENH.@)
2955 * Derives a key from a hash value.
2957 * PARAMS
2958 * hProv [I] Key container for which a key is to be generated.
2959 * Algid [I] Crypto algorithm identifier for the key to be generated.
2960 * hBaseData [I] Hash from whose value the key will be derived.
2961 * dwFlags [I] See Notes.
2962 * phKey [O] The generated key.
2964 * RETURNS
2965 * Success: TRUE
2966 * Failure: FALSE
2968 * NOTES
2969 * Defined flags:
2970 * - CRYPT_EXPORTABLE: Key can be exported.
2971 * - CRYPT_NO_SALT: No salt is used for 40 bit keys.
2972 * - CRYPT_CREATE_SALT: Use remaining bits as salt value.
2974 BOOL WINAPI RSAENH_CPDeriveKey(HCRYPTPROV hProv, ALG_ID Algid, HCRYPTHASH hBaseData,
2975 DWORD dwFlags, HCRYPTKEY *phKey)
2977 CRYPTKEY *pCryptKey, *pMasterKey;
2978 CRYPTHASH *pCryptHash;
2979 BYTE abHashValue[RSAENH_MAX_HASH_SIZE*2];
2980 DWORD dwLen;
2982 TRACE("(hProv=%08lx, Algid=%d, hBaseData=%08lx, dwFlags=%08lx phKey=%p)\n", hProv, Algid,
2983 hBaseData, dwFlags, phKey);
2985 if (!is_valid_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER))
2987 SetLastError(NTE_BAD_UID);
2988 return FALSE;
2991 if (!lookup_handle(&handle_table, (unsigned int)hBaseData, RSAENH_MAGIC_HASH,
2992 (OBJECTHDR**)&pCryptHash))
2994 SetLastError(NTE_BAD_HASH);
2995 return FALSE;
2998 if (!phKey)
3000 SetLastError(ERROR_INVALID_PARAMETER);
3001 return FALSE;
3004 switch (GET_ALG_CLASS(Algid))
3006 case ALG_CLASS_DATA_ENCRYPT:
3007 *phKey = new_key(hProv, Algid, dwFlags, &pCryptKey);
3008 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) return FALSE;
3011 * We derive the key material from the hash.
3012 * If the hash value is not large enough for the claimed key, we have to construct
3013 * a larger binary value based on the hash. This is documented in MSDN: CryptDeriveKey.
3015 dwLen = RSAENH_MAX_HASH_SIZE;
3016 RSAENH_CPGetHashParam(pCryptHash->hProv, hBaseData, HP_HASHVAL, abHashValue, &dwLen, 0);
3018 if (dwLen < pCryptKey->dwKeyLen) {
3019 BYTE pad1[RSAENH_HMAC_DEF_PAD_LEN], pad2[RSAENH_HMAC_DEF_PAD_LEN];
3020 BYTE old_hashval[RSAENH_MAX_HASH_SIZE];
3021 DWORD i;
3023 memcpy(old_hashval, pCryptHash->abHashValue, RSAENH_MAX_HASH_SIZE);
3025 for (i=0; i<RSAENH_HMAC_DEF_PAD_LEN; i++) {
3026 pad1[i] = RSAENH_HMAC_DEF_IPAD_CHAR ^ (i<dwLen ? abHashValue[i] : 0);
3027 pad2[i] = RSAENH_HMAC_DEF_OPAD_CHAR ^ (i<dwLen ? abHashValue[i] : 0);
3030 init_hash(pCryptHash);
3031 update_hash(pCryptHash, pad1, RSAENH_HMAC_DEF_PAD_LEN);
3032 finalize_hash(pCryptHash);
3033 memcpy(abHashValue, pCryptHash->abHashValue, pCryptHash->dwHashSize);
3035 init_hash(pCryptHash);
3036 update_hash(pCryptHash, pad2, RSAENH_HMAC_DEF_PAD_LEN);
3037 finalize_hash(pCryptHash);
3038 memcpy(abHashValue+pCryptHash->dwHashSize, pCryptHash->abHashValue,
3039 pCryptHash->dwHashSize);
3041 memcpy(pCryptHash->abHashValue, old_hashval, RSAENH_MAX_HASH_SIZE);
3044 memcpy(pCryptKey->abKeyValue, abHashValue,
3045 RSAENH_MIN(pCryptKey->dwKeyLen, sizeof(pCryptKey->abKeyValue)));
3046 break;
3048 case ALG_CLASS_MSG_ENCRYPT:
3049 if (!lookup_handle(&handle_table, pCryptHash->hKey, RSAENH_MAGIC_KEY,
3050 (OBJECTHDR**)&pMasterKey))
3052 SetLastError(NTE_FAIL); /* FIXME error code */
3053 return FALSE;
3056 switch (Algid)
3058 /* See RFC 2246, chapter 6.3 Key calculation */
3059 case CALG_SCHANNEL_ENC_KEY:
3060 *phKey = new_key(hProv, pMasterKey->siSChannelInfo.saEncAlg.Algid,
3061 MAKELONG(LOWORD(dwFlags),pMasterKey->siSChannelInfo.saEncAlg.cBits),
3062 &pCryptKey);
3063 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) return FALSE;
3064 memcpy(pCryptKey->abKeyValue,
3065 pCryptHash->abHashValue + (
3066 2 * (pMasterKey->siSChannelInfo.saMACAlg.cBits / 8) +
3067 ((dwFlags & CRYPT_SERVER) ?
3068 (pMasterKey->siSChannelInfo.saEncAlg.cBits / 8) : 0)),
3069 pMasterKey->siSChannelInfo.saEncAlg.cBits / 8);
3070 memcpy(pCryptKey->abInitVector,
3071 pCryptHash->abHashValue + (
3072 2 * (pMasterKey->siSChannelInfo.saMACAlg.cBits / 8) +
3073 2 * (pMasterKey->siSChannelInfo.saEncAlg.cBits / 8) +
3074 ((dwFlags & CRYPT_SERVER) ? pCryptKey->dwBlockLen : 0)),
3075 pCryptKey->dwBlockLen);
3076 break;
3078 case CALG_SCHANNEL_MAC_KEY:
3079 *phKey = new_key(hProv, Algid,
3080 MAKELONG(LOWORD(dwFlags),pMasterKey->siSChannelInfo.saMACAlg.cBits),
3081 &pCryptKey);
3082 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) return FALSE;
3083 memcpy(pCryptKey->abKeyValue,
3084 pCryptHash->abHashValue + ((dwFlags & CRYPT_SERVER) ?
3085 pMasterKey->siSChannelInfo.saMACAlg.cBits / 8 : 0),
3086 pMasterKey->siSChannelInfo.saMACAlg.cBits / 8);
3087 break;
3089 default:
3090 SetLastError(NTE_BAD_ALGID);
3091 return FALSE;
3093 break;
3095 default:
3096 SetLastError(NTE_BAD_ALGID);
3097 return FALSE;
3100 setup_key(pCryptKey);
3101 return TRUE;
3104 /******************************************************************************
3105 * CPGetUserKey (RSAENH.@)
3107 * Returns a handle to the user's private key-exchange- or signature-key.
3109 * PARAMS
3110 * hProv [I] The key container from which a user key is requested.
3111 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
3112 * phUserKey [O] Handle to the requested key or INVALID_HANDLE_VALUE in case of failure.
3114 * RETURNS
3115 * Success: TRUE.
3116 * Failure: FALSE.
3118 * NOTE
3119 * A newly created key container does not contain private user key. Create them with CPGenKey.
3121 BOOL WINAPI RSAENH_CPGetUserKey(HCRYPTPROV hProv, DWORD dwKeySpec, HCRYPTKEY *phUserKey)
3123 KEYCONTAINER *pKeyContainer;
3125 TRACE("(hProv=%08lx, dwKeySpec=%08lx, phUserKey=%p)\n", hProv, dwKeySpec, phUserKey);
3127 if (!lookup_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER,
3128 (OBJECTHDR**)&pKeyContainer))
3130 /* MSDN: hProv not containing valid context handle */
3131 SetLastError(NTE_BAD_UID);
3132 return FALSE;
3135 switch (dwKeySpec)
3137 case AT_KEYEXCHANGE:
3138 copy_handle(&handle_table, pKeyContainer->hKeyExchangeKeyPair, RSAENH_MAGIC_KEY,
3139 (unsigned int*)phUserKey);
3140 break;
3142 case AT_SIGNATURE:
3143 copy_handle(&handle_table, pKeyContainer->hSignatureKeyPair, RSAENH_MAGIC_KEY,
3144 (unsigned int*)phUserKey);
3145 break;
3147 default:
3148 *phUserKey = (HCRYPTKEY)INVALID_HANDLE_VALUE;
3151 if (*phUserKey == (HCRYPTKEY)INVALID_HANDLE_VALUE)
3153 /* MSDN: dwKeySpec parameter specifies nonexistent key */
3154 SetLastError(NTE_NO_KEY);
3155 return FALSE;
3158 return TRUE;
3161 /******************************************************************************
3162 * CPHashData (RSAENH.@)
3164 * Updates a hash object with the given data.
3166 * PARAMS
3167 * hProv [I] Key container to which the hash object belongs.
3168 * hHash [I] Hash object which is to be updated.
3169 * pbData [I] Pointer to data with which the hash object is to be updated.
3170 * dwDataLen [I] Length of the data.
3171 * dwFlags [I] Currently none defined.
3173 * RETURNS
3174 * Success: TRUE.
3175 * Failure: FALSE.
3177 * NOTES
3178 * The actual hash value is queried with CPGetHashParam, which will finalize
3179 * the hash. Updating a finalized hash will fail with a last error NTE_BAD_HASH_STATE.
3181 BOOL WINAPI RSAENH_CPHashData(HCRYPTPROV hProv, HCRYPTHASH hHash, CONST BYTE *pbData,
3182 DWORD dwDataLen, DWORD dwFlags)
3184 CRYPTHASH *pCryptHash;
3186 TRACE("(hProv=%08lx, hHash=%08lx, pbData=%p, dwDataLen=%ld, dwFlags=%08lx)\n",
3187 hProv, hHash, pbData, dwDataLen, dwFlags);
3189 if (dwFlags)
3191 SetLastError(NTE_BAD_FLAGS);
3192 return FALSE;
3195 if (!lookup_handle(&handle_table, (unsigned int)hHash, RSAENH_MAGIC_HASH,
3196 (OBJECTHDR**)&pCryptHash))
3198 SetLastError(NTE_BAD_HASH);
3199 return FALSE;
3202 if (!get_algid_info(hProv, pCryptHash->aiAlgid) || pCryptHash->aiAlgid == CALG_SSL3_SHAMD5)
3204 SetLastError(NTE_BAD_ALGID);
3205 return FALSE;
3208 if (pCryptHash->dwState == RSAENH_HASHSTATE_IDLE)
3209 pCryptHash->dwState = RSAENH_HASHSTATE_HASHING;
3211 if (pCryptHash->dwState != RSAENH_HASHSTATE_HASHING)
3213 SetLastError(NTE_BAD_HASH_STATE);
3214 return FALSE;
3217 update_hash(pCryptHash, pbData, dwDataLen);
3218 return TRUE;
3221 /******************************************************************************
3222 * CPHashSessionKey (RSAENH.@)
3224 * Updates a hash object with the binary representation of a symmetric key.
3226 * PARAMS
3227 * hProv [I] Key container to which the hash object belongs.
3228 * hHash [I] Hash object which is to be updated.
3229 * hKey [I] The symmetric key, whose binary value will be added to the hash.
3230 * dwFlags [I] CRYPT_LITTLE_ENDIAN, if the binary key value shall be interpreted as little endian.
3232 * RETURNS
3233 * Success: TRUE.
3234 * Failure: FALSE.
3236 BOOL WINAPI RSAENH_CPHashSessionKey(HCRYPTPROV hProv, HCRYPTHASH hHash, HCRYPTKEY hKey,
3237 DWORD dwFlags)
3239 BYTE abKeyValue[RSAENH_MAX_KEY_SIZE], bTemp;
3240 CRYPTKEY *pKey;
3241 DWORD i;
3243 TRACE("(hProv=%08lx, hHash=%08lx, hKey=%08lx, dwFlags=%08lx)\n", hProv, hHash, hKey, dwFlags);
3245 if (!lookup_handle(&handle_table, (unsigned int)hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pKey) ||
3246 (GET_ALG_CLASS(pKey->aiAlgid) != ALG_CLASS_DATA_ENCRYPT))
3248 SetLastError(NTE_BAD_KEY);
3249 return FALSE;
3252 if (dwFlags & ~CRYPT_LITTLE_ENDIAN) {
3253 SetLastError(NTE_BAD_FLAGS);
3254 return FALSE;
3257 memcpy(abKeyValue, pKey->abKeyValue, pKey->dwKeyLen);
3258 if (!(dwFlags & CRYPT_LITTLE_ENDIAN)) {
3259 for (i=0; i<pKey->dwKeyLen/2; i++) {
3260 bTemp = abKeyValue[i];
3261 abKeyValue[i] = abKeyValue[pKey->dwKeyLen-i-1];
3262 abKeyValue[pKey->dwKeyLen-i-1] = bTemp;
3266 return RSAENH_CPHashData(hProv, hHash, abKeyValue, pKey->dwKeyLen, 0);
3269 /******************************************************************************
3270 * CPReleaseContext (RSAENH.@)
3272 * Release a key container.
3274 * PARAMS
3275 * hProv [I] Key container to be released.
3276 * dwFlags [I] Currently none defined.
3278 * RETURNS
3279 * Success: TRUE
3280 * Failure: FALSE
3282 BOOL WINAPI RSAENH_CPReleaseContext(HCRYPTPROV hProv, DWORD dwFlags)
3284 TRACE("(hProv=%08lx, dwFlags=%08lx)\n", hProv, dwFlags);
3286 if (!release_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER))
3288 /* MSDN: hProv not containing valid context handle */
3289 SetLastError(NTE_BAD_UID);
3290 return FALSE;
3293 if (dwFlags) {
3294 SetLastError(NTE_BAD_FLAGS);
3295 return FALSE;
3298 return TRUE;
3301 /******************************************************************************
3302 * CPSetHashParam (RSAENH.@)
3304 * Set a parameter of a hash object
3306 * PARAMS
3307 * hProv [I] The key container to which the key belongs.
3308 * hHash [I] The hash object for which a parameter is to be set.
3309 * dwParam [I] Parameter type. See Notes.
3310 * pbData [I] Pointer to the parameter value.
3311 * dwFlags [I] Currently none defined.
3313 * RETURNS
3314 * Success: TRUE.
3315 * Failure: FALSE.
3317 * NOTES
3318 * Currently only the HP_HMAC_INFO dwParam type is defined.
3319 * The HMAC_INFO struct will be deep copied into the hash object.
3320 * See Internet RFC 2104 for details on the HMAC algorithm.
3322 BOOL WINAPI RSAENH_CPSetHashParam(HCRYPTPROV hProv, HCRYPTHASH hHash, DWORD dwParam,
3323 BYTE *pbData, DWORD dwFlags)
3325 CRYPTHASH *pCryptHash;
3326 CRYPTKEY *pCryptKey;
3327 int i;
3329 TRACE("(hProv=%08lx, hHash=%08lx, dwParam=%08lx, pbData=%p, dwFlags=%08lx)\n",
3330 hProv, hHash, dwParam, pbData, dwFlags);
3332 if (!is_valid_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER))
3334 SetLastError(NTE_BAD_UID);
3335 return FALSE;
3338 if (dwFlags) {
3339 SetLastError(NTE_BAD_FLAGS);
3340 return FALSE;
3343 if (!lookup_handle(&handle_table, (unsigned int)hHash, RSAENH_MAGIC_HASH,
3344 (OBJECTHDR**)&pCryptHash))
3346 SetLastError(NTE_BAD_HASH);
3347 return FALSE;
3350 switch (dwParam) {
3351 case HP_HMAC_INFO:
3352 free_hmac_info(pCryptHash->pHMACInfo);
3353 if (!copy_hmac_info(&pCryptHash->pHMACInfo, (PHMAC_INFO)pbData)) return FALSE;
3355 if (!lookup_handle(&handle_table, pCryptHash->hKey, RSAENH_MAGIC_KEY,
3356 (OBJECTHDR**)&pCryptKey))
3358 SetLastError(NTE_FAIL); /* FIXME: correct error code? */
3359 return FALSE;
3362 for (i=0; i<RSAENH_MIN(pCryptKey->dwKeyLen,pCryptHash->pHMACInfo->cbInnerString); i++) {
3363 pCryptHash->pHMACInfo->pbInnerString[i] ^= pCryptKey->abKeyValue[i];
3365 for (i=0; i<RSAENH_MIN(pCryptKey->dwKeyLen,pCryptHash->pHMACInfo->cbOuterString); i++) {
3366 pCryptHash->pHMACInfo->pbOuterString[i] ^= pCryptKey->abKeyValue[i];
3369 init_hash(pCryptHash);
3370 return TRUE;
3372 case HP_HASHVAL:
3373 memcpy(pCryptHash->abHashValue, pbData, pCryptHash->dwHashSize);
3374 pCryptHash->dwState = RSAENH_HASHSTATE_FINISHED;
3375 return TRUE;
3377 case HP_TLS1PRF_SEED:
3378 return copy_data_blob(&pCryptHash->tpPRFParams.blobSeed, (PCRYPT_DATA_BLOB)pbData);
3380 case HP_TLS1PRF_LABEL:
3381 return copy_data_blob(&pCryptHash->tpPRFParams.blobLabel, (PCRYPT_DATA_BLOB)pbData);
3383 default:
3384 SetLastError(NTE_BAD_TYPE);
3385 return FALSE;
3389 /******************************************************************************
3390 * CPSetProvParam (RSAENH.@)
3392 BOOL WINAPI RSAENH_CPSetProvParam(HCRYPTPROV hProv, DWORD dwParam, BYTE *pbData, DWORD dwFlags)
3394 FIXME("(stub)\n");
3395 return FALSE;
3398 /******************************************************************************
3399 * CPSignHash (RSAENH.@)
3401 * Sign a hash object
3403 * PARAMS
3404 * hProv [I] The key container, to which the hash object belongs.
3405 * hHash [I] The hash object to be signed.
3406 * dwKeySpec [I] AT_SIGNATURE or AT_KEYEXCHANGE: Key used to generate the signature.
3407 * sDescription [I] Should be NULL for security reasons.
3408 * dwFlags [I] 0, CRYPT_NOHASHOID or CRYPT_X931_FORMAT: Format of the signature.
3409 * pbSignature [O] Buffer, to which the signature will be stored. May be NULL to query SigLen.
3410 * pdwSigLen [I/O] Size of the buffer (in), Length of the signature (out)
3412 * RETURNS
3413 * Success: TRUE
3414 * Failure: FALSE
3416 BOOL WINAPI RSAENH_CPSignHash(HCRYPTPROV hProv, HCRYPTHASH hHash, DWORD dwKeySpec,
3417 LPCWSTR sDescription, DWORD dwFlags, BYTE *pbSignature,
3418 DWORD *pdwSigLen)
3420 HCRYPTKEY hCryptKey;
3421 CRYPTKEY *pCryptKey;
3422 DWORD dwHashLen;
3423 BYTE abHashValue[RSAENH_MAX_HASH_SIZE];
3424 ALG_ID aiAlgid;
3426 TRACE("(hProv=%08lx, hHash=%08lx, dwKeySpec=%08lx, sDescription=%s, dwFlags=%08lx, "
3427 "pbSignature=%p, pdwSigLen=%p)\n", hProv, hHash, dwKeySpec, debugstr_w(sDescription),
3428 dwFlags, pbSignature, pdwSigLen);
3430 if (dwFlags & ~(CRYPT_NOHASHOID|CRYPT_X931_FORMAT)) {
3431 SetLastError(NTE_BAD_FLAGS);
3432 return FALSE;
3435 if (!RSAENH_CPGetUserKey(hProv, dwKeySpec, &hCryptKey)) return FALSE;
3437 if (!lookup_handle(&handle_table, (unsigned int)hCryptKey, RSAENH_MAGIC_KEY,
3438 (OBJECTHDR**)&pCryptKey))
3440 SetLastError(NTE_NO_KEY);
3441 return FALSE;
3444 if (!pbSignature) {
3445 *pdwSigLen = pCryptKey->dwKeyLen;
3446 return TRUE;
3448 if (pCryptKey->dwKeyLen > *pdwSigLen)
3450 SetLastError(ERROR_MORE_DATA);
3451 *pdwSigLen = pCryptKey->dwKeyLen;
3452 return FALSE;
3454 *pdwSigLen = pCryptKey->dwKeyLen;
3456 if (sDescription) {
3457 if (!RSAENH_CPHashData(hProv, hHash, (CONST BYTE*)sDescription,
3458 (DWORD)lstrlenW(sDescription)*sizeof(WCHAR), 0))
3460 return FALSE;
3464 dwHashLen = sizeof(DWORD);
3465 if (!RSAENH_CPGetHashParam(hProv, hHash, HP_ALGID, (BYTE*)&aiAlgid, &dwHashLen, 0)) return FALSE;
3467 dwHashLen = RSAENH_MAX_HASH_SIZE;
3468 if (!RSAENH_CPGetHashParam(hProv, hHash, HP_HASHVAL, abHashValue, &dwHashLen, 0)) return FALSE;
3471 if (!build_hash_signature(pbSignature, *pdwSigLen, aiAlgid, abHashValue, dwHashLen, dwFlags)) {
3472 return FALSE;
3475 return encrypt_block_impl(pCryptKey->aiAlgid, &pCryptKey->context, pbSignature, pbSignature, RSAENH_ENCRYPT);
3478 /******************************************************************************
3479 * CPVerifySignature (RSAENH.@)
3481 * Verify the signature of a hash object.
3483 * PARAMS
3484 * hProv [I] The key container, to which the hash belongs.
3485 * hHash [I] The hash for which the signature is verified.
3486 * pbSignature [I] The binary signature.
3487 * dwSigLen [I] Length of the signature BLOB.
3488 * hPubKey [I] Public key used to verify the signature.
3489 * sDescription [I] Should be NULL for security reasons.
3490 * dwFlags [I] 0, CRYPT_NOHASHOID or CRYPT_X931_FORMAT: Format of the signature.
3492 * RETURNS
3493 * Success: TRUE (Signature is valid)
3494 * Failure: FALSE (GetLastError() == NTE_BAD_SIGNATURE, if signature is invalid)
3496 BOOL WINAPI RSAENH_CPVerifySignature(HCRYPTPROV hProv, HCRYPTHASH hHash, CONST BYTE *pbSignature,
3497 DWORD dwSigLen, HCRYPTKEY hPubKey, LPCWSTR sDescription,
3498 DWORD dwFlags)
3500 BYTE *pbConstructed = NULL, *pbDecrypted = NULL;
3501 CRYPTKEY *pCryptKey;
3502 DWORD dwHashLen;
3503 ALG_ID aiAlgid;
3504 BYTE abHashValue[RSAENH_MAX_HASH_SIZE];
3505 BOOL res = FALSE;
3507 TRACE("(hProv=%08lx, hHash=%08lx, pbSignature=%p, dwSigLen=%ld, hPubKey=%08lx, sDescription=%s, "
3508 "dwFlags=%08lx)\n", hProv, hHash, pbSignature, dwSigLen, hPubKey, debugstr_w(sDescription),
3509 dwFlags);
3511 if (dwFlags & ~(CRYPT_NOHASHOID|CRYPT_X931_FORMAT)) {
3512 SetLastError(NTE_BAD_FLAGS);
3513 return FALSE;
3516 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
3518 SetLastError(NTE_BAD_UID);
3519 return FALSE;
3522 if (!lookup_handle(&handle_table, (unsigned int)hPubKey, RSAENH_MAGIC_KEY,
3523 (OBJECTHDR**)&pCryptKey))
3525 SetLastError(NTE_BAD_KEY);
3526 return FALSE;
3529 if (sDescription) {
3530 if (!RSAENH_CPHashData(hProv, hHash, (CONST BYTE*)sDescription,
3531 (DWORD)lstrlenW(sDescription)*sizeof(WCHAR), 0))
3533 return FALSE;
3537 dwHashLen = sizeof(DWORD);
3538 if (!RSAENH_CPGetHashParam(hProv, hHash, HP_ALGID, (BYTE*)&aiAlgid, &dwHashLen, 0)) return FALSE;
3540 dwHashLen = RSAENH_MAX_HASH_SIZE;
3541 if (!RSAENH_CPGetHashParam(hProv, hHash, HP_HASHVAL, abHashValue, &dwHashLen, 0)) return FALSE;
3543 pbConstructed = HeapAlloc(GetProcessHeap(), 0, dwSigLen);
3544 if (!pbConstructed) {
3545 SetLastError(NTE_NO_MEMORY);
3546 goto cleanup;
3549 pbDecrypted = HeapAlloc(GetProcessHeap(), 0, dwSigLen);
3550 if (!pbDecrypted) {
3551 SetLastError(NTE_NO_MEMORY);
3552 goto cleanup;
3555 if (!encrypt_block_impl(pCryptKey->aiAlgid, &pCryptKey->context, pbSignature, pbDecrypted,
3556 RSAENH_DECRYPT))
3558 goto cleanup;
3561 if (!build_hash_signature(pbConstructed, dwSigLen, aiAlgid, abHashValue, dwHashLen, dwFlags)) {
3562 goto cleanup;
3565 if (memcmp(pbDecrypted, pbConstructed, dwSigLen)) {
3566 SetLastError(NTE_BAD_SIGNATURE);
3567 goto cleanup;
3570 res = TRUE;
3571 cleanup:
3572 HeapFree(GetProcessHeap(), 0, pbConstructed);
3573 HeapFree(GetProcessHeap(), 0, pbDecrypted);
3574 return res;
3577 static const WCHAR szProviderKeys[4][97] = {
3578 { 'S','o','f','t','w','a','r','e','\\',
3579 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
3580 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
3581 'i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ','B','a','s',
3582 'e',' ','C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r',
3583 'o','v','i','d','e','r',' ','v','1','.','0',0 },
3584 { 'S','o','f','t','w','a','r','e','\\',
3585 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
3586 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
3587 'i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ',
3588 'E','n','h','a','n','c','e','d',
3589 ' ','C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r',
3590 'o','v','i','d','e','r',' ','v','1','.','0',0 },
3591 { 'S','o','f','t','w','a','r','e','\\',
3592 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
3593 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
3594 'i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ','S','t','r','o','n','g',
3595 ' ','C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r',
3596 'o','v','i','d','e','r',0 },
3597 { 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
3598 'C','r','y','p','t','o','g','r','a','p','h','y','\\','D','e','f','a','u','l','t','s','\\',
3599 'P','r','o','v','i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ',
3600 'R','S','A',' ','S','C','h','a','n','n','e','l',' ',
3601 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r','o','v','i','d','e','r',0 }
3603 static const WCHAR szDefaultKeys[2][65] = {
3604 { 'S','o','f','t','w','a','r','e','\\',
3605 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
3606 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
3607 'i','d','e','r',' ','T','y','p','e','s','\\','T','y','p','e',' ','0','0','1',0 },
3608 { 'S','o','f','t','w','a','r','e','\\',
3609 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
3610 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
3611 'i','d','e','r',' ','T','y','p','e','s','\\','T','y','p','e',' ','0','1','2',0 }
3615 /******************************************************************************
3616 * DllRegisterServer (RSAENH.@)
3618 * Dll self registration.
3620 * PARAMS
3622 * RETURNS
3623 * Success: S_OK.
3624 * Failure: != S_OK
3626 * NOTES
3627 * Registers the following keys:
3628 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider\
3629 * Microsoft Base Cryptographic Provider v1.0
3630 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider\
3631 * Microsoft Enhanced Cryptographic Provider
3632 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider\
3633 * Microsoft Strong Cryptographpic Provider
3634 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider Types\Type 001
3636 HRESULT WINAPI DllRegisterServer(void)
3638 HKEY key;
3639 DWORD dp;
3640 long apiRet;
3641 int i;
3643 for (i=0; i<4; i++) {
3644 apiRet = RegCreateKeyExW(HKEY_LOCAL_MACHINE, szProviderKeys[i], 0, NULL,
3645 REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &key, &dp);
3647 if (apiRet == ERROR_SUCCESS)
3649 if (dp == REG_CREATED_NEW_KEY)
3651 static const WCHAR szImagePath[] = { 'I','m','a','g','e',' ','P','a','t','h',0 };
3652 static const WCHAR szRSABase[] = { 'r','s','a','e','n','h','.','d','l','l',0 };
3653 static const WCHAR szType[] = { 'T','y','p','e',0 };
3654 static const WCHAR szSignature[] = { 'S','i','g','n','a','t','u','r','e',0 };
3655 DWORD type = (i == 3) ? PROV_RSA_SCHANNEL : PROV_RSA_FULL;
3656 DWORD sign = 0xdeadbeef;
3657 RegSetValueExW(key, szImagePath, 0, REG_SZ, (LPBYTE)szRSABase,
3658 (lstrlenW(szRSABase) + 1) * sizeof(WCHAR));
3659 RegSetValueExW(key, szType, 0, REG_DWORD, (LPBYTE)&type, sizeof(type));
3660 RegSetValueExW(key, szSignature, 0, REG_BINARY, (LPBYTE)&sign, sizeof(sign));
3662 RegCloseKey(key);
3666 for (i=0; i<2; i++) {
3667 apiRet = RegCreateKeyExW(HKEY_LOCAL_MACHINE, szDefaultKeys[i], 0, NULL,
3668 REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &key, &dp);
3669 if (apiRet == ERROR_SUCCESS)
3671 if (dp == REG_CREATED_NEW_KEY)
3673 static const WCHAR szName[] = { 'N','a','m','e',0 };
3674 static const WCHAR szRSAName[2][46] = {
3675 { 'M','i','c','r','o','s','o','f','t',' ', 'B','a','s','e',' ',
3676 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ',
3677 'P','r','o','v','i','d','e','r',' ','v','1','.','0',0 },
3678 { 'M','i','c','r','o','s','o','f','t',' ','R','S','A',' ',
3679 'S','C','h','a','n','n','e','l',' ',
3680 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ',
3681 'P','r','o','v','i','d','e','r',0 } };
3682 static const WCHAR szTypeName[] = { 'T','y','p','e','N','a','m','e',0 };
3683 static const WCHAR szRSATypeName[2][38] = {
3684 { 'R','S','A',' ','F','u','l','l',' ',
3685 '(','S','i','g','n','a','t','u','r','e',' ','a','n','d',' ',
3686 'K','e','y',' ','E','x','c','h','a','n','g','e',')',0 },
3687 { 'R','S','A',' ','S','C','h','a','n','n','e','l',0 } };
3689 RegSetValueExW(key, szName, 0, REG_SZ, (LPBYTE)szRSAName[i], sizeof(szRSAName));
3690 RegSetValueExW(key, szTypeName, 0, REG_SZ,
3691 (LPBYTE)szRSATypeName[i],sizeof(szRSATypeName));
3694 RegCloseKey(key);
3697 return HRESULT_FROM_WIN32(apiRet);
3700 /******************************************************************************
3701 * DllUnregisterServer (RSAENH.@)
3703 * Dll self unregistration.
3705 * PARAMS
3707 * RETURNS
3708 * Success: S_OK
3710 * NOTES
3711 * For the relevant keys see DllRegisterServer.
3713 HRESULT WINAPI DllUnregisterServer(void)
3715 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szProviderKeys[0]);
3716 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szProviderKeys[1]);
3717 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szProviderKeys[2]);
3718 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szProviderKeys[3]);
3719 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szDefaultKeys[0]);
3720 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szDefaultKeys[1]);
3721 return S_OK;