3 * RSAENH - RSA encryption for Wine
5 * Copyright 2002 TransGaming Technologies (David Hammerton)
6 * Copyright 2004 Mike McCormack for CodeWeavers
7 * Copyright 2004, 2005 Michael Jung
8 * Copyright 2007 Vijay Kiran Kamuju
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "wine/port.h"
27 #include "wine/library.h"
28 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(crypt);
43 /******************************************************************************
44 * CRYPTHASH - hash objects
46 #define RSAENH_MAGIC_HASH 0x85938417u
47 #define RSAENH_MAX_HASH_SIZE 104
48 #define RSAENH_HASHSTATE_HASHING 1
49 #define RSAENH_HASHSTATE_FINISHED 2
50 typedef struct _RSAENH_TLS1PRF_PARAMS
52 CRYPT_DATA_BLOB blobLabel;
53 CRYPT_DATA_BLOB blobSeed;
54 } RSAENH_TLS1PRF_PARAMS;
56 typedef struct tagCRYPTHASH
65 BYTE abHashValue[RSAENH_MAX_HASH_SIZE];
67 RSAENH_TLS1PRF_PARAMS tpPRFParams;
70 /******************************************************************************
71 * CRYPTKEY - key objects
73 #define RSAENH_MAGIC_KEY 0x73620457u
74 #define RSAENH_MAX_KEY_SIZE 48
75 #define RSAENH_MAX_BLOCK_SIZE 24
76 #define RSAENH_KEYSTATE_IDLE 0
77 #define RSAENH_KEYSTATE_ENCRYPTING 1
78 #define RSAENH_KEYSTATE_MASTERKEY 2
79 typedef struct _RSAENH_SCHANNEL_INFO
81 SCHANNEL_ALG saEncAlg;
82 SCHANNEL_ALG saMACAlg;
83 CRYPT_DATA_BLOB blobClientRandom;
84 CRYPT_DATA_BLOB blobServerRandom;
85 } RSAENH_SCHANNEL_INFO;
87 typedef struct tagCRYPTKEY
96 DWORD dwEffectiveKeyLen;
101 BYTE abKeyValue[RSAENH_MAX_KEY_SIZE];
102 BYTE abInitVector[RSAENH_MAX_BLOCK_SIZE];
103 BYTE abChainVector[RSAENH_MAX_BLOCK_SIZE];
104 RSAENH_SCHANNEL_INFO siSChannelInfo;
107 /******************************************************************************
108 * KEYCONTAINER - key containers
110 #define RSAENH_PERSONALITY_BASE 0u
111 #define RSAENH_PERSONALITY_STRONG 1u
112 #define RSAENH_PERSONALITY_ENHANCED 2u
113 #define RSAENH_PERSONALITY_SCHANNEL 3u
114 #define RSAENH_PERSONALITY_AES 4u
116 #define RSAENH_MAGIC_CONTAINER 0x26384993u
117 typedef struct tagKEYCONTAINER
123 DWORD dwEnumContainersCtr;
124 CHAR szName[MAX_PATH];
125 CHAR szProvName[MAX_PATH];
126 HCRYPTKEY hKeyExchangeKeyPair;
127 HCRYPTKEY hSignatureKeyPair;
130 /******************************************************************************
131 * Some magic constants
133 #define RSAENH_ENCRYPT 1
134 #define RSAENH_DECRYPT 0
135 #define RSAENH_HMAC_DEF_IPAD_CHAR 0x36
136 #define RSAENH_HMAC_DEF_OPAD_CHAR 0x5c
137 #define RSAENH_HMAC_DEF_PAD_LEN 64
138 #define RSAENH_DES_EFFECTIVE_KEYLEN 56
139 #define RSAENH_DES_STORAGE_KEYLEN 64
140 #define RSAENH_3DES112_EFFECTIVE_KEYLEN 112
141 #define RSAENH_3DES112_STORAGE_KEYLEN 128
142 #define RSAENH_3DES_EFFECTIVE_KEYLEN 168
143 #define RSAENH_3DES_STORAGE_KEYLEN 192
144 #define RSAENH_MAGIC_RSA2 0x32415352
145 #define RSAENH_MAGIC_RSA1 0x31415352
146 #define RSAENH_PKC_BLOCKTYPE 0x02
147 #define RSAENH_SSL3_VERSION_MAJOR 3
148 #define RSAENH_SSL3_VERSION_MINOR 0
149 #define RSAENH_TLS1_VERSION_MAJOR 3
150 #define RSAENH_TLS1_VERSION_MINOR 1
151 #define RSAENH_REGKEY "Software\\Wine\\Crypto\\RSA\\%s"
153 #define RSAENH_MIN(a,b) ((a)<(b)?(a):(b))
154 /******************************************************************************
155 * aProvEnumAlgsEx - Defines the capabilities of the CSP personalities.
157 #define RSAENH_MAX_ENUMALGS 24
158 #define RSAENH_PCT1_SSL2_SSL3_TLS1 (CRYPT_FLAG_PCT1|CRYPT_FLAG_SSL2|CRYPT_FLAG_SSL3|CRYPT_FLAG_TLS1)
159 static const PROV_ENUMALGS_EX aProvEnumAlgsEx[5][RSAENH_MAX_ENUMALGS+1] =
162 {CALG_RC2, 40, 40, 56,0, 4,"RC2", 24,"RSA Data Security's RC2"},
163 {CALG_RC4, 40, 40, 56,0, 4,"RC4", 24,"RSA Data Security's RC4"},
164 {CALG_DES, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
165 {CALG_SHA, 160,160, 160,CRYPT_FLAG_SIGNING, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
166 {CALG_MD2, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD2", 23,"Message Digest 2 (MD2)"},
167 {CALG_MD4, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD4", 23,"Message Digest 4 (MD4)"},
168 {CALG_MD5, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD5", 23,"Message Digest 5 (MD5)"},
169 {CALG_SSL3_SHAMD5,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
170 {CALG_MAC, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
171 {CALG_RSA_SIGN, 512,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_SIGN",14,"RSA Signature"},
172 {CALG_RSA_KEYX, 512,384, 1024,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_KEYX",17,"RSA Key Exchange"},
173 {CALG_HMAC, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
174 {0, 0, 0, 0,0, 1,"", 1,""}
177 {CALG_RC2, 128, 40, 128,0, 4,"RC2", 24,"RSA Data Security's RC2"},
178 {CALG_RC4, 128, 40, 128,0, 4,"RC4", 24,"RSA Data Security's RC4"},
179 {CALG_DES, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
180 {CALG_3DES_112, 112,112, 112,0, 13,"3DES TWO KEY",19,"Two Key Triple DES"},
181 {CALG_3DES, 168,168, 168,0, 5,"3DES", 21,"Three Key Triple DES"},
182 {CALG_SHA, 160,160, 160,CRYPT_FLAG_SIGNING, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
183 {CALG_MD2, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD2", 23,"Message Digest 2 (MD2)"},
184 {CALG_MD4, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD4", 23,"Message Digest 4 (MD4)"},
185 {CALG_MD5, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD5", 23,"Message Digest 5 (MD5)"},
186 {CALG_SSL3_SHAMD5,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
187 {CALG_MAC, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
188 {CALG_RSA_SIGN,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_SIGN",14,"RSA Signature"},
189 {CALG_RSA_KEYX,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_KEYX",17,"RSA Key Exchange"},
190 {CALG_HMAC, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
191 {0, 0, 0, 0,0, 1,"", 1,""}
194 {CALG_RC2, 128, 40, 128,0, 4,"RC2", 24,"RSA Data Security's RC2"},
195 {CALG_RC4, 128, 40, 128,0, 4,"RC4", 24,"RSA Data Security's RC4"},
196 {CALG_DES, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
197 {CALG_3DES_112, 112,112, 112,0, 13,"3DES TWO KEY",19,"Two Key Triple DES"},
198 {CALG_3DES, 168,168, 168,0, 5,"3DES", 21,"Three Key Triple DES"},
199 {CALG_SHA, 160,160, 160,CRYPT_FLAG_SIGNING, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
200 {CALG_MD2, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD2", 23,"Message Digest 2 (MD2)"},
201 {CALG_MD4, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD4", 23,"Message Digest 4 (MD4)"},
202 {CALG_MD5, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD5", 23,"Message Digest 5 (MD5)"},
203 {CALG_SSL3_SHAMD5,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
204 {CALG_MAC, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
205 {CALG_RSA_SIGN,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_SIGN",14,"RSA Signature"},
206 {CALG_RSA_KEYX,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_KEYX",17,"RSA Key Exchange"},
207 {CALG_HMAC, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
208 {0, 0, 0, 0,0, 1,"", 1,""}
211 {CALG_RC2, 128, 40, 128,RSAENH_PCT1_SSL2_SSL3_TLS1, 4,"RC2", 24,"RSA Data Security's RC2"},
212 {CALG_RC4, 128, 40, 128,RSAENH_PCT1_SSL2_SSL3_TLS1, 4,"RC4", 24,"RSA Data Security's RC4"},
213 {CALG_DES, 56, 56, 56,RSAENH_PCT1_SSL2_SSL3_TLS1, 4,"DES", 31,"Data Encryption Standard (DES)"},
214 {CALG_3DES_112, 112,112, 112,RSAENH_PCT1_SSL2_SSL3_TLS1,13,"3DES TWO KEY",19,"Two Key Triple DES"},
215 {CALG_3DES, 168,168, 168,RSAENH_PCT1_SSL2_SSL3_TLS1, 5,"3DES", 21,"Three Key Triple DES"},
216 {CALG_SHA,160,160,160,CRYPT_FLAG_SIGNING|RSAENH_PCT1_SSL2_SSL3_TLS1,6,"SHA-1",30,"Secure Hash Algorithm (SHA-1)"},
217 {CALG_MD5,128,128,128,CRYPT_FLAG_SIGNING|RSAENH_PCT1_SSL2_SSL3_TLS1,4,"MD5",23,"Message Digest 5 (MD5)"},
218 {CALG_SSL3_SHAMD5,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
219 {CALG_MAC, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
220 {CALG_RSA_SIGN,1024,384,16384,CRYPT_FLAG_SIGNING|RSAENH_PCT1_SSL2_SSL3_TLS1,9,"RSA_SIGN",14,"RSA Signature"},
221 {CALG_RSA_KEYX,1024,384,16384,CRYPT_FLAG_SIGNING|RSAENH_PCT1_SSL2_SSL3_TLS1,9,"RSA_KEYX",17,"RSA Key Exchange"},
222 {CALG_HMAC, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
223 {CALG_PCT1_MASTER,128,128,128,CRYPT_FLAG_PCT1, 12,"PCT1 MASTER",12,"PCT1 Master"},
224 {CALG_SSL2_MASTER,40,40, 192,CRYPT_FLAG_SSL2, 12,"SSL2 MASTER",12,"SSL2 Master"},
225 {CALG_SSL3_MASTER,384,384,384,CRYPT_FLAG_SSL3, 12,"SSL3 MASTER",12,"SSL3 Master"},
226 {CALG_TLS1_MASTER,384,384,384,CRYPT_FLAG_TLS1, 12,"TLS1 MASTER",12,"TLS1 Master"},
227 {CALG_SCHANNEL_MASTER_HASH,0,0,-1,0, 16,"SCH MASTER HASH",21,"SChannel Master Hash"},
228 {CALG_SCHANNEL_MAC_KEY,0,0,-1,0, 12,"SCH MAC KEY",17,"SChannel MAC Key"},
229 {CALG_SCHANNEL_ENC_KEY,0,0,-1,0, 12,"SCH ENC KEY",24,"SChannel Encryption Key"},
230 {CALG_TLS1PRF, 0, 0, -1,0, 9,"TLS1 PRF", 28,"TLS1 Pseudo Random Function"},
231 {0, 0, 0, 0,0, 1,"", 1,""}
234 {CALG_RC2, 128, 40, 128,0, 4,"RC2", 24,"RSA Data Security's RC2"},
235 {CALG_RC4, 128, 40, 128,0, 4,"RC4", 24,"RSA Data Security's RC4"},
236 {CALG_DES, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
237 {CALG_3DES_112, 112,112, 112,0, 13,"3DES TWO KEY",19,"Two Key Triple DES"},
238 {CALG_3DES, 168,168, 168,0, 5,"3DES", 21,"Three Key Triple DES"},
239 {CALG_AES, 128,128, 128,0, 4,"AES", 35,"Advanced Encryption Standard (AES)"},
240 {CALG_AES_128, 128,128, 128,0, 8,"AES-128", 39,"Advanced Encryption Standard (AES-128)"},
241 {CALG_AES_192, 192,192, 192,0, 8,"AES-192", 39,"Advanced Encryption Standard (AES-192)"},
242 {CALG_AES_256, 256,256, 256,0, 8,"AES-256", 39,"Advanced Encryption Standard (AES-256)"},
243 {CALG_SHA, 160,160, 160,CRYPT_FLAG_SIGNING, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
244 {CALG_MD2, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD2", 23,"Message Digest 2 (MD2)"},
245 {CALG_MD4, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD4", 23,"Message Digest 4 (MD4)"},
246 {CALG_MD5, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD5", 23,"Message Digest 5 (MD5)"},
247 {CALG_SSL3_SHAMD5,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
248 {CALG_MAC, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
249 {CALG_RSA_SIGN,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_SIGN",14,"RSA Signature"},
250 {CALG_RSA_KEYX,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_KEYX",17,"RSA Key Exchange"},
251 {CALG_HMAC, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
252 {0, 0, 0, 0,0, 1,"", 1,""}
256 /******************************************************************************
257 * API forward declarations
260 RSAENH_CPGetKeyParam(
291 RSAENH_CPSetHashParam(
295 BYTE *pbData, DWORD dwFlags
299 RSAENH_CPGetHashParam(
309 RSAENH_CPDestroyHash(
344 /******************************************************************************
345 * CSP's handle table (used by all acquired key containers)
347 static HANDLETABLE handle_table;
349 /******************************************************************************
352 * Initializes and destroys the handle table for the CSP's handles.
354 int WINAPI DllMain(HINSTANCE hInstance, DWORD fdwReason, PVOID pvReserved)
358 case DLL_PROCESS_ATTACH:
359 DisableThreadLibraryCalls(hInstance);
360 init_handle_table(&handle_table);
363 case DLL_PROCESS_DETACH:
364 destroy_handle_table(&handle_table);
370 /******************************************************************************
371 * copy_param [Internal]
373 * Helper function that supports the standard WINAPI protocol for querying data
377 * pbBuffer [O] Buffer where the queried parameter is copied to, if it is large enough.
378 * May be NUL if the required buffer size is to be queried only.
379 * pdwBufferSize [I/O] In: Size of the buffer at pbBuffer
380 * Out: Size of parameter pbParam
381 * pbParam [I] Parameter value.
382 * dwParamSize [I] Size of pbParam
385 * Success: TRUE (pbParam was copied into pbBuffer or pbBuffer is NULL)
386 * Failure: FALSE (pbBuffer is not large enough to hold pbParam). Last error: ERROR_MORE_DATA
388 static inline BOOL copy_param(
389 BYTE *pbBuffer, DWORD *pdwBufferSize, CONST BYTE *pbParam, DWORD dwParamSize)
393 if (dwParamSize > *pdwBufferSize)
395 SetLastError(ERROR_MORE_DATA);
396 *pdwBufferSize = dwParamSize;
399 memcpy(pbBuffer, pbParam, dwParamSize);
401 *pdwBufferSize = dwParamSize;
405 /******************************************************************************
406 * get_algid_info [Internal]
408 * Query CSP capabilities for a given crypto algorithm.
411 * hProv [I] Handle to a key container of the CSP whose capabilities are to be queried.
412 * algid [I] Identifier of the crypto algorithm about which information is requested.
415 * Success: Pointer to a PROV_ENUMALGS_EX struct containing information about the crypto algorithm.
416 * Failure: NULL (algid not supported)
418 static inline const PROV_ENUMALGS_EX* get_algid_info(HCRYPTPROV hProv, ALG_ID algid) {
419 const PROV_ENUMALGS_EX *iterator;
420 KEYCONTAINER *pKeyContainer;
422 if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER, (OBJECTHDR**)&pKeyContainer)) {
423 SetLastError(NTE_BAD_UID);
427 for (iterator = aProvEnumAlgsEx[pKeyContainer->dwPersonality]; iterator->aiAlgid; iterator++) {
428 if (iterator->aiAlgid == algid) return iterator;
431 SetLastError(NTE_BAD_ALGID);
435 /******************************************************************************
436 * copy_data_blob [Internal]
438 * deeply copies a DATA_BLOB
441 * dst [O] That's where the blob will be copied to
442 * src [I] Source blob
446 * Failure: FALSE (GetLastError() == NTE_NO_MEMORY
449 * Use free_data_blob to release resources occupied by copy_data_blob.
451 static inline BOOL copy_data_blob(PCRYPT_DATA_BLOB dst, CONST PCRYPT_DATA_BLOB src) {
452 dst->pbData = HeapAlloc(GetProcessHeap(), 0, src->cbData);
454 SetLastError(NTE_NO_MEMORY);
457 dst->cbData = src->cbData;
458 memcpy(dst->pbData, src->pbData, src->cbData);
462 /******************************************************************************
463 * concat_data_blobs [Internal]
465 * Concatenates two blobs
468 * dst [O] The new blob will be copied here
469 * src1 [I] Prefix blob
470 * src2 [I] Appendix blob
474 * Failure: FALSE (GetLastError() == NTE_NO_MEMORY)
477 * Release resources occupied by concat_data_blobs with free_data_blobs
479 static inline BOOL concat_data_blobs(PCRYPT_DATA_BLOB dst, CONST PCRYPT_DATA_BLOB src1,
480 CONST PCRYPT_DATA_BLOB src2)
482 dst->cbData = src1->cbData + src2->cbData;
483 dst->pbData = HeapAlloc(GetProcessHeap(), 0, dst->cbData);
485 SetLastError(NTE_NO_MEMORY);
488 memcpy(dst->pbData, src1->pbData, src1->cbData);
489 memcpy(dst->pbData + src1->cbData, src2->pbData, src2->cbData);
493 /******************************************************************************
494 * free_data_blob [Internal]
496 * releases resource occupied by a dynamically allocated CRYPT_DATA_BLOB
499 * pBlob [I] Heap space occupied by pBlob->pbData is released
501 static inline void free_data_blob(PCRYPT_DATA_BLOB pBlob) {
502 HeapFree(GetProcessHeap(), 0, pBlob->pbData);
505 /******************************************************************************
506 * init_data_blob [Internal]
508 static inline void init_data_blob(PCRYPT_DATA_BLOB pBlob) {
509 pBlob->pbData = NULL;
513 /******************************************************************************
514 * free_hmac_info [Internal]
516 * Deeply free an HMAC_INFO struct.
519 * hmac_info [I] Pointer to the HMAC_INFO struct to be freed.
522 * See Internet RFC 2104 for details on the HMAC algorithm.
524 static inline void free_hmac_info(PHMAC_INFO hmac_info) {
525 if (!hmac_info) return;
526 HeapFree(GetProcessHeap(), 0, hmac_info->pbInnerString);
527 HeapFree(GetProcessHeap(), 0, hmac_info->pbOuterString);
528 HeapFree(GetProcessHeap(), 0, hmac_info);
531 /******************************************************************************
532 * copy_hmac_info [Internal]
534 * Deeply copy an HMAC_INFO struct
537 * dst [O] Pointer to a location where the pointer to the HMAC_INFO copy will be stored.
538 * src [I] Pointer to the HMAC_INFO struct to be copied.
545 * See Internet RFC 2104 for details on the HMAC algorithm.
547 static BOOL copy_hmac_info(PHMAC_INFO *dst, const HMAC_INFO *src) {
548 if (!src) return FALSE;
549 *dst = HeapAlloc(GetProcessHeap(), 0, sizeof(HMAC_INFO));
550 if (!*dst) return FALSE;
552 (*dst)->pbInnerString = NULL;
553 (*dst)->pbOuterString = NULL;
554 if ((*dst)->cbInnerString == 0) (*dst)->cbInnerString = RSAENH_HMAC_DEF_PAD_LEN;
555 (*dst)->pbInnerString = HeapAlloc(GetProcessHeap(), 0, (*dst)->cbInnerString);
556 if (!(*dst)->pbInnerString) {
557 free_hmac_info(*dst);
560 if (src->cbInnerString)
561 memcpy((*dst)->pbInnerString, src->pbInnerString, src->cbInnerString);
563 memset((*dst)->pbInnerString, RSAENH_HMAC_DEF_IPAD_CHAR, RSAENH_HMAC_DEF_PAD_LEN);
564 if ((*dst)->cbOuterString == 0) (*dst)->cbOuterString = RSAENH_HMAC_DEF_PAD_LEN;
565 (*dst)->pbOuterString = HeapAlloc(GetProcessHeap(), 0, (*dst)->cbOuterString);
566 if (!(*dst)->pbOuterString) {
567 free_hmac_info(*dst);
570 if (src->cbOuterString)
571 memcpy((*dst)->pbOuterString, src->pbOuterString, src->cbOuterString);
573 memset((*dst)->pbOuterString, RSAENH_HMAC_DEF_OPAD_CHAR, RSAENH_HMAC_DEF_PAD_LEN);
577 /******************************************************************************
578 * destroy_hash [Internal]
580 * Destructor for hash objects
583 * pCryptHash [I] Pointer to the hash object to be destroyed.
584 * Will be invalid after function returns!
586 static void destroy_hash(OBJECTHDR *pObject)
588 CRYPTHASH *pCryptHash = (CRYPTHASH*)pObject;
590 free_hmac_info(pCryptHash->pHMACInfo);
591 free_data_blob(&pCryptHash->tpPRFParams.blobLabel);
592 free_data_blob(&pCryptHash->tpPRFParams.blobSeed);
593 HeapFree(GetProcessHeap(), 0, pCryptHash);
596 /******************************************************************************
597 * init_hash [Internal]
599 * Initialize (or reset) a hash object
602 * pCryptHash [I] The hash object to be initialized.
604 static inline BOOL init_hash(CRYPTHASH *pCryptHash) {
607 switch (pCryptHash->aiAlgid)
610 if (pCryptHash->pHMACInfo) {
611 const PROV_ENUMALGS_EX *pAlgInfo;
613 pAlgInfo = get_algid_info(pCryptHash->hProv, pCryptHash->pHMACInfo->HashAlgid);
614 if (!pAlgInfo) return FALSE;
615 pCryptHash->dwHashSize = pAlgInfo->dwDefaultLen >> 3;
616 init_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context);
617 update_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
618 pCryptHash->pHMACInfo->pbInnerString,
619 pCryptHash->pHMACInfo->cbInnerString);
624 dwLen = sizeof(DWORD);
625 RSAENH_CPGetKeyParam(pCryptHash->hProv, pCryptHash->hKey, KP_BLOCKLEN,
626 (BYTE*)&pCryptHash->dwHashSize, &dwLen, 0);
627 pCryptHash->dwHashSize >>= 3;
631 return init_hash_impl(pCryptHash->aiAlgid, &pCryptHash->context);
635 /******************************************************************************
636 * update_hash [Internal]
638 * Hashes the given data and updates the hash object's state accordingly
641 * pCryptHash [I] Hash object to be updated.
642 * pbData [I] Pointer to data stream to be hashed.
643 * dwDataLen [I] Length of data stream.
645 static inline void update_hash(CRYPTHASH *pCryptHash, CONST BYTE *pbData, DWORD dwDataLen) {
648 switch (pCryptHash->aiAlgid)
651 if (pCryptHash->pHMACInfo)
652 update_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
657 pbTemp = HeapAlloc(GetProcessHeap(), 0, dwDataLen);
659 memcpy(pbTemp, pbData, dwDataLen);
660 RSAENH_CPEncrypt(pCryptHash->hProv, pCryptHash->hKey, 0, FALSE, 0,
661 pbTemp, &dwDataLen, dwDataLen);
662 HeapFree(GetProcessHeap(), 0, pbTemp);
666 update_hash_impl(pCryptHash->aiAlgid, &pCryptHash->context, pbData, dwDataLen);
670 /******************************************************************************
671 * finalize_hash [Internal]
673 * Finalizes the hash, after all data has been hashed with update_hash.
674 * No additional data can be hashed afterwards until the hash gets initialized again.
677 * pCryptHash [I] Hash object to be finalized.
679 static inline void finalize_hash(CRYPTHASH *pCryptHash) {
682 switch (pCryptHash->aiAlgid)
685 if (pCryptHash->pHMACInfo) {
686 BYTE abHashValue[RSAENH_MAX_HASH_SIZE];
688 finalize_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
689 pCryptHash->abHashValue);
690 memcpy(abHashValue, pCryptHash->abHashValue, pCryptHash->dwHashSize);
691 init_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context);
692 update_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
693 pCryptHash->pHMACInfo->pbOuterString,
694 pCryptHash->pHMACInfo->cbOuterString);
695 update_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
696 abHashValue, pCryptHash->dwHashSize);
697 finalize_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
698 pCryptHash->abHashValue);
704 RSAENH_CPEncrypt(pCryptHash->hProv, pCryptHash->hKey, 0, TRUE, 0,
705 pCryptHash->abHashValue, &dwDataLen, pCryptHash->dwHashSize);
709 finalize_hash_impl(pCryptHash->aiAlgid, &pCryptHash->context, pCryptHash->abHashValue);
713 /******************************************************************************
714 * destroy_key [Internal]
716 * Destructor for key objects
719 * pCryptKey [I] Pointer to the key object to be destroyed.
720 * Will be invalid after function returns!
722 static void destroy_key(OBJECTHDR *pObject)
724 CRYPTKEY *pCryptKey = (CRYPTKEY*)pObject;
726 free_key_impl(pCryptKey->aiAlgid, &pCryptKey->context);
727 free_data_blob(&pCryptKey->siSChannelInfo.blobClientRandom);
728 free_data_blob(&pCryptKey->siSChannelInfo.blobServerRandom);
729 HeapFree(GetProcessHeap(), 0, pCryptKey);
732 /******************************************************************************
733 * setup_key [Internal]
735 * Initialize (or reset) a key object
738 * pCryptKey [I] The key object to be initialized.
740 static inline void setup_key(CRYPTKEY *pCryptKey) {
741 pCryptKey->dwState = RSAENH_KEYSTATE_IDLE;
742 memcpy(pCryptKey->abChainVector, pCryptKey->abInitVector, sizeof(pCryptKey->abChainVector));
743 setup_key_impl(pCryptKey->aiAlgid, &pCryptKey->context, pCryptKey->dwKeyLen,
744 pCryptKey->dwEffectiveKeyLen, pCryptKey->dwSaltLen,
745 pCryptKey->abKeyValue);
748 /******************************************************************************
751 * Creates a new key object without assigning the actual binary key value.
752 * This is done by CPDeriveKey, CPGenKey or CPImportKey, which call this function.
755 * hProv [I] Handle to the provider to which the created key will belong.
756 * aiAlgid [I] The new key shall use the crypto algorithm idenfied by aiAlgid.
757 * dwFlags [I] Upper 16 bits give the key length.
758 * Lower 16 bits: CRYPT_CREATE_SALT, CRYPT_NO_SALT
759 * ppCryptKey [O] Pointer to the created key
762 * Success: Handle to the created key.
763 * Failure: INVALID_HANDLE_VALUE
765 static HCRYPTKEY new_key(HCRYPTPROV hProv, ALG_ID aiAlgid, DWORD dwFlags, CRYPTKEY **ppCryptKey)
769 DWORD dwKeyLen = HIWORD(dwFlags);
770 const PROV_ENUMALGS_EX *peaAlgidInfo;
775 * Retrieve the CSP's capabilities for the given ALG_ID value
777 peaAlgidInfo = get_algid_info(hProv, aiAlgid);
778 if (!peaAlgidInfo) return (HCRYPTKEY)INVALID_HANDLE_VALUE;
781 * Assume the default key length, if none is specified explicitly
783 if (dwKeyLen == 0) dwKeyLen = peaAlgidInfo->dwDefaultLen;
786 * Check if the requested key length is supported by the current CSP.
787 * Adjust key length's for DES algorithms.
791 if (dwKeyLen == RSAENH_DES_EFFECTIVE_KEYLEN) {
792 dwKeyLen = RSAENH_DES_STORAGE_KEYLEN;
794 if (dwKeyLen != RSAENH_DES_STORAGE_KEYLEN) {
795 SetLastError(NTE_BAD_FLAGS);
796 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
801 if (dwKeyLen == RSAENH_3DES112_EFFECTIVE_KEYLEN) {
802 dwKeyLen = RSAENH_3DES112_STORAGE_KEYLEN;
804 if (dwKeyLen != RSAENH_3DES112_STORAGE_KEYLEN) {
805 SetLastError(NTE_BAD_FLAGS);
806 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
811 if (dwKeyLen == RSAENH_3DES_EFFECTIVE_KEYLEN) {
812 dwKeyLen = RSAENH_3DES_STORAGE_KEYLEN;
814 if (dwKeyLen != RSAENH_3DES_STORAGE_KEYLEN) {
815 SetLastError(NTE_BAD_FLAGS);
816 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
822 dwKeyLen > peaAlgidInfo->dwMaxLen ||
823 dwKeyLen < peaAlgidInfo->dwMinLen)
825 SetLastError(NTE_BAD_FLAGS);
826 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
830 hCryptKey = new_object(&handle_table, sizeof(CRYPTKEY), RSAENH_MAGIC_KEY,
831 destroy_key, (OBJECTHDR**)&pCryptKey);
832 if (hCryptKey != (HCRYPTKEY)INVALID_HANDLE_VALUE)
834 pCryptKey->aiAlgid = aiAlgid;
835 pCryptKey->hProv = hProv;
836 pCryptKey->dwModeBits = 0;
837 pCryptKey->dwPermissions = CRYPT_ENCRYPT | CRYPT_DECRYPT | CRYPT_READ | CRYPT_WRITE |
839 pCryptKey->dwKeyLen = dwKeyLen >> 3;
840 pCryptKey->dwEffectiveKeyLen = 0;
841 if ((dwFlags & CRYPT_CREATE_SALT) || (dwKeyLen == 40 && !(dwFlags & CRYPT_NO_SALT)))
842 pCryptKey->dwSaltLen = 16 /*FIXME*/ - pCryptKey->dwKeyLen;
844 pCryptKey->dwSaltLen = 0;
845 memset(pCryptKey->abKeyValue, 0, sizeof(pCryptKey->abKeyValue));
846 memset(pCryptKey->abInitVector, 0, sizeof(pCryptKey->abInitVector));
847 init_data_blob(&pCryptKey->siSChannelInfo.blobClientRandom);
848 init_data_blob(&pCryptKey->siSChannelInfo.blobServerRandom);
852 case CALG_PCT1_MASTER:
853 case CALG_SSL2_MASTER:
854 case CALG_SSL3_MASTER:
855 case CALG_TLS1_MASTER:
857 pCryptKey->dwBlockLen = 0;
858 pCryptKey->dwMode = 0;
865 pCryptKey->dwBlockLen = 8;
866 pCryptKey->dwMode = CRYPT_MODE_CBC;
873 pCryptKey->dwBlockLen = 16;
874 pCryptKey->dwMode = CRYPT_MODE_ECB;
879 pCryptKey->dwBlockLen = dwKeyLen >> 3;
880 pCryptKey->dwMode = 0;
884 *ppCryptKey = pCryptKey;
890 /******************************************************************************
891 * store_key_pair [Internal]
893 * Stores a key pair to the registry
896 * hCryptKey [I] Handle to the key to be stored
897 * hKey [I] Registry key where the key pair is to be stored
898 * szValueName [I] Registry value where key pair's value is to be stored
899 * dwFlags [I] Flags for protecting the key
901 static void store_key_pair(HCRYPTKEY hCryptKey, HKEY hKey, LPCSTR szValueName, DWORD dwFlags)
903 DATA_BLOB blobIn, blobOut;
908 if (lookup_handle(&handle_table, hCryptKey, RSAENH_MAGIC_KEY,
911 if (RSAENH_CPExportKey(pKey->hProv, hCryptKey, 0, PRIVATEKEYBLOB, 0, 0,
914 pbKey = HeapAlloc(GetProcessHeap(), 0, dwLen);
917 if (RSAENH_CPExportKey(pKey->hProv, hCryptKey, 0,
918 PRIVATEKEYBLOB, 0, pbKey, &dwLen))
920 blobIn.pbData = pbKey;
921 blobIn.cbData = dwLen;
923 if (CryptProtectData(&blobIn, NULL, NULL, NULL, NULL,
926 RegSetValueExA(hKey, szValueName, 0, REG_BINARY,
927 blobOut.pbData, blobOut.cbData);
928 LocalFree(blobOut.pbData);
931 HeapFree(GetProcessHeap(), 0, pbKey);
934 release_handle(&handle_table, hCryptKey, RSAENH_MAGIC_KEY);
938 /******************************************************************************
939 * create_container_key [Internal]
941 * Creates the registry key for a key container's persistent storage.
944 * pKeyContainer [I] Pointer to the key container
945 * sam [I] Desired registry access
946 * phKey [O] Returned key
948 static BOOL create_container_key(KEYCONTAINER *pKeyContainer, REGSAM sam, HKEY *phKey)
950 CHAR szRSABase[MAX_PATH];
953 sprintf(szRSABase, RSAENH_REGKEY, pKeyContainer->szName);
955 if (pKeyContainer->dwFlags & CRYPT_MACHINE_KEYSET)
956 hRootKey = HKEY_LOCAL_MACHINE;
958 hRootKey = HKEY_CURRENT_USER;
960 /* @@ Wine registry key: HKLM\Software\Wine\Crypto\RSA */
961 /* @@ Wine registry key: HKCU\Software\Wine\Crypto\RSA */
962 return RegCreateKeyExA(hRootKey, szRSABase, 0, NULL,
963 REG_OPTION_NON_VOLATILE, sam, NULL, phKey, NULL)
967 /******************************************************************************
968 * open_container_key [Internal]
970 * Opens a key container's persistent storage for reading.
973 * pszContainerName [I] Name of the container to be opened. May be the empty
974 * string if the parent key of all containers is to be
976 * dwFlags [I] Flags indicating which keyset to be opened.
977 * phKey [O] Returned key
979 static BOOL open_container_key(LPCSTR pszContainerName, DWORD dwFlags, HKEY *phKey)
981 CHAR szRSABase[MAX_PATH];
984 sprintf(szRSABase, RSAENH_REGKEY, pszContainerName);
986 if (dwFlags & CRYPT_MACHINE_KEYSET)
987 hRootKey = HKEY_LOCAL_MACHINE;
989 hRootKey = HKEY_CURRENT_USER;
991 /* @@ Wine registry key: HKLM\Software\Wine\Crypto\RSA */
992 /* @@ Wine registry key: HKCU\Software\Wine\Crypto\RSA */
993 return RegOpenKeyExA(hRootKey, szRSABase, 0, KEY_READ, phKey) ==
997 /******************************************************************************
998 * delete_container_key [Internal]
1000 * Deletes a key container's persistent storage.
1003 * pszContainerName [I] Name of the container to be opened.
1004 * dwFlags [I] Flags indicating which keyset to be opened.
1006 static BOOL delete_container_key(LPCSTR pszContainerName, DWORD dwFlags)
1008 CHAR szRegKey[MAX_PATH];
1010 if (snprintf(szRegKey, MAX_PATH, RSAENH_REGKEY, pszContainerName) >= MAX_PATH) {
1011 SetLastError(NTE_BAD_KEYSET_PARAM);
1015 if (dwFlags & CRYPT_MACHINE_KEYSET)
1016 hRootKey = HKEY_LOCAL_MACHINE;
1018 hRootKey = HKEY_CURRENT_USER;
1019 if (!RegDeleteKeyA(hRootKey, szRegKey)) {
1020 SetLastError(ERROR_SUCCESS);
1023 SetLastError(NTE_BAD_KEYSET);
1029 /******************************************************************************
1030 * store_key_container_keys [Internal]
1032 * Stores key container's keys in a persistent location.
1035 * pKeyContainer [I] Pointer to the key container whose keys are to be saved
1037 static void store_key_container_keys(KEYCONTAINER *pKeyContainer)
1042 /* On WinXP, persistent keys are stored in a file located at:
1043 * $AppData$\\Microsoft\\Crypto\\RSA\\$SID$\\some_hex_string
1046 if (pKeyContainer->dwFlags & CRYPT_MACHINE_KEYSET)
1047 dwFlags = CRYPTPROTECT_LOCAL_MACHINE;
1051 if (create_container_key(pKeyContainer, KEY_WRITE, &hKey))
1053 store_key_pair(pKeyContainer->hKeyExchangeKeyPair, hKey,
1054 "KeyExchangeKeyPair", dwFlags);
1055 store_key_pair(pKeyContainer->hSignatureKeyPair, hKey,
1056 "SignatureKeyPair", dwFlags);
1061 /******************************************************************************
1062 * destroy_key_container [Internal]
1064 * Destructor for key containers.
1067 * pObjectHdr [I] Pointer to the key container to be destroyed.
1069 static void destroy_key_container(OBJECTHDR *pObjectHdr)
1071 KEYCONTAINER *pKeyContainer = (KEYCONTAINER*)pObjectHdr;
1073 if (!(pKeyContainer->dwFlags & CRYPT_VERIFYCONTEXT))
1074 store_key_container_keys(pKeyContainer);
1075 HeapFree( GetProcessHeap(), 0, pKeyContainer );
1078 /******************************************************************************
1079 * new_key_container [Internal]
1081 * Create a new key container. The personality (RSA Base, Strong or Enhanced CP)
1082 * of the CSP is determined via the pVTable->pszProvName string.
1085 * pszContainerName [I] Name of the key container.
1086 * pVTable [I] Callback functions and context info provided by the OS
1089 * Success: Handle to the new key container.
1090 * Failure: INVALID_HANDLE_VALUE
1092 static HCRYPTPROV new_key_container(PCCH pszContainerName, DWORD dwFlags, const VTableProvStruc *pVTable)
1094 KEYCONTAINER *pKeyContainer;
1095 HCRYPTPROV hKeyContainer;
1097 hKeyContainer = new_object(&handle_table, sizeof(KEYCONTAINER), RSAENH_MAGIC_CONTAINER,
1098 destroy_key_container, (OBJECTHDR**)&pKeyContainer);
1099 if (hKeyContainer != (HCRYPTPROV)INVALID_HANDLE_VALUE)
1101 lstrcpynA(pKeyContainer->szName, pszContainerName, MAX_PATH);
1102 pKeyContainer->dwFlags = dwFlags;
1103 pKeyContainer->dwEnumAlgsCtr = 0;
1104 pKeyContainer->hKeyExchangeKeyPair = (HCRYPTKEY)INVALID_HANDLE_VALUE;
1105 pKeyContainer->hSignatureKeyPair = (HCRYPTKEY)INVALID_HANDLE_VALUE;
1106 if (pVTable && pVTable->pszProvName) {
1107 lstrcpynA(pKeyContainer->szProvName, pVTable->pszProvName, MAX_PATH);
1108 if (!strcmp(pVTable->pszProvName, MS_DEF_PROV_A)) {
1109 pKeyContainer->dwPersonality = RSAENH_PERSONALITY_BASE;
1110 } else if (!strcmp(pVTable->pszProvName, MS_ENHANCED_PROV_A)) {
1111 pKeyContainer->dwPersonality = RSAENH_PERSONALITY_ENHANCED;
1112 } else if (!strcmp(pVTable->pszProvName, MS_DEF_RSA_SCHANNEL_PROV_A)) {
1113 pKeyContainer->dwPersonality = RSAENH_PERSONALITY_SCHANNEL;
1114 } else if (!strcmp(pVTable->pszProvName, MS_ENH_RSA_AES_PROV_A)) {
1115 pKeyContainer->dwPersonality = RSAENH_PERSONALITY_AES;
1117 pKeyContainer->dwPersonality = RSAENH_PERSONALITY_STRONG;
1121 /* The new key container has to be inserted into the CSP immediately
1122 * after creation to be available for CPGetProvParam's PP_ENUMCONTAINERS. */
1123 if (!(dwFlags & CRYPT_VERIFYCONTEXT)) {
1126 if (create_container_key(pKeyContainer, KEY_WRITE, &hKey))
1131 return hKeyContainer;
1134 /******************************************************************************
1135 * read_key_value [Internal]
1137 * Reads a key pair value from the registry
1140 * hKeyContainer [I] Crypt provider to use to import the key
1141 * hKey [I] Registry key from which to read the key pair
1142 * szValueName [I] Registry value from which to read the key pair's value
1143 * dwFlags [I] Flags for unprotecting the key
1144 * phCryptKey [O] Returned key
1146 static BOOL read_key_value(HCRYPTPROV hKeyContainer, HKEY hKey, LPCSTR szValueName, DWORD dwFlags, HCRYPTKEY *phCryptKey)
1148 DWORD dwValueType, dwLen;
1150 DATA_BLOB blobIn, blobOut;
1153 if (RegQueryValueExA(hKey, szValueName, 0, &dwValueType, NULL, &dwLen) ==
1156 pbKey = HeapAlloc(GetProcessHeap(), 0, dwLen);
1159 if (RegQueryValueExA(hKey, szValueName, 0, &dwValueType, pbKey, &dwLen) ==
1162 blobIn.pbData = pbKey;
1163 blobIn.cbData = dwLen;
1165 if (CryptUnprotectData(&blobIn, NULL, NULL, NULL, NULL,
1168 ret = RSAENH_CPImportKey(hKeyContainer, blobOut.pbData, blobOut.cbData, 0, 0,
1170 LocalFree(blobOut.pbData);
1173 HeapFree(GetProcessHeap(), 0, pbKey);
1179 /******************************************************************************
1180 * read_key_container [Internal]
1182 * Tries to read the persistent state of the key container (mainly the signature
1183 * and key exchange private keys) given by pszContainerName.
1186 * pszContainerName [I] Name of the key container to read from the registry
1187 * pVTable [I] Pointer to context data provided by the operating system
1190 * Success: Handle to the key container read from the registry
1191 * Failure: INVALID_HANDLE_VALUE
1193 static HCRYPTPROV read_key_container(PCHAR pszContainerName, DWORD dwFlags, const VTableProvStruc *pVTable)
1196 KEYCONTAINER *pKeyContainer;
1197 HCRYPTPROV hKeyContainer;
1198 HCRYPTKEY hCryptKey;
1200 if (!open_container_key(pszContainerName, dwFlags, &hKey))
1202 SetLastError(NTE_BAD_KEYSET);
1203 return (HCRYPTPROV)INVALID_HANDLE_VALUE;
1206 hKeyContainer = new_key_container(pszContainerName, dwFlags, pVTable);
1207 if (hKeyContainer != (HCRYPTPROV)INVALID_HANDLE_VALUE)
1209 DWORD dwProtectFlags = (dwFlags & CRYPT_MACHINE_KEYSET) ?
1210 CRYPTPROTECT_LOCAL_MACHINE : 0;
1212 if (!lookup_handle(&handle_table, hKeyContainer, RSAENH_MAGIC_CONTAINER,
1213 (OBJECTHDR**)&pKeyContainer))
1214 return (HCRYPTPROV)INVALID_HANDLE_VALUE;
1216 if (read_key_value(hKeyContainer, hKey, "KeyExchangeKeyPair",
1217 dwProtectFlags, &hCryptKey))
1218 pKeyContainer->hKeyExchangeKeyPair = hCryptKey;
1219 if (read_key_value(hKeyContainer, hKey, "SignatureKeyPair",
1220 dwProtectFlags, &hCryptKey))
1221 pKeyContainer->hSignatureKeyPair = hCryptKey;
1224 return hKeyContainer;
1227 /******************************************************************************
1228 * build_hash_signature [Internal]
1230 * Builds a padded version of a hash to match the length of the RSA key modulus.
1233 * pbSignature [O] The padded hash object is stored here.
1234 * dwLen [I] Length of the pbSignature buffer.
1235 * aiAlgid [I] Algorithm identifier of the hash to be padded.
1236 * abHashValue [I] The value of the hash object.
1237 * dwHashLen [I] Length of the hash value.
1238 * dwFlags [I] Selection of padding algorithm.
1242 * Failure: FALSE (NTE_BAD_ALGID)
1244 static BOOL build_hash_signature(BYTE *pbSignature, DWORD dwLen, ALG_ID aiAlgid,
1245 CONST BYTE *abHashValue, DWORD dwHashLen, DWORD dwFlags)
1247 /* These prefixes are meant to be concatenated with hash values of the
1248 * respective kind to form a PKCS #7 DigestInfo. */
1249 static const struct tagOIDDescriptor {
1252 CONST BYTE abOID[18];
1253 } aOIDDescriptor[5] = {
1254 { CALG_MD2, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1255 0x86, 0xf7, 0x0d, 0x02, 0x02, 0x05, 0x00, 0x04, 0x10 } },
1256 { CALG_MD4, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1257 0x86, 0xf7, 0x0d, 0x02, 0x04, 0x05, 0x00, 0x04, 0x10 } },
1258 { CALG_MD5, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1259 0x86, 0xf7, 0x0d, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10 } },
1260 { CALG_SHA, 15, { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03,
1261 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14 } },
1264 DWORD dwIdxOID, i, j;
1266 for (dwIdxOID = 0; aOIDDescriptor[dwIdxOID].aiAlgid; dwIdxOID++) {
1267 if (aOIDDescriptor[dwIdxOID].aiAlgid == aiAlgid) break;
1270 if (!aOIDDescriptor[dwIdxOID].aiAlgid) {
1271 SetLastError(NTE_BAD_ALGID);
1275 /* Build the padded signature */
1276 if (dwFlags & CRYPT_X931_FORMAT) {
1277 pbSignature[0] = 0x6b;
1278 for (i=1; i < dwLen - dwHashLen - 3; i++) {
1279 pbSignature[i] = 0xbb;
1281 pbSignature[i++] = 0xba;
1282 for (j=0; j < dwHashLen; j++, i++) {
1283 pbSignature[i] = abHashValue[j];
1285 pbSignature[i++] = 0x33;
1286 pbSignature[i++] = 0xcc;
1288 pbSignature[0] = 0x00;
1289 pbSignature[1] = 0x01;
1290 if (dwFlags & CRYPT_NOHASHOID) {
1291 for (i=2; i < dwLen - 1 - dwHashLen; i++) {
1292 pbSignature[i] = 0xff;
1294 pbSignature[i++] = 0x00;
1296 for (i=2; i < dwLen - 1 - aOIDDescriptor[dwIdxOID].dwLen - dwHashLen; i++) {
1297 pbSignature[i] = 0xff;
1299 pbSignature[i++] = 0x00;
1300 for (j=0; j < aOIDDescriptor[dwIdxOID].dwLen; j++) {
1301 pbSignature[i++] = aOIDDescriptor[dwIdxOID].abOID[j];
1304 for (j=0; j < dwHashLen; j++) {
1305 pbSignature[i++] = abHashValue[j];
1312 /******************************************************************************
1315 * This is an implementation of the 'P_hash' helper function for TLS1's PRF.
1316 * It is used exclusively by tls1_prf. For details see RFC 2246, chapter 5.
1317 * The pseudo random stream generated by this function is exclusive or'ed with
1318 * the data in pbBuffer.
1321 * hHMAC [I] HMAC object, which will be used in pseudo random generation
1322 * pblobSeed [I] Seed value
1323 * pbBuffer [I/O] Pseudo random stream will be xor'ed to the provided data
1324 * dwBufferLen [I] Number of pseudo random bytes desired
1330 static BOOL tls1_p(HCRYPTHASH hHMAC, CONST PCRYPT_DATA_BLOB pblobSeed, PBYTE pbBuffer, DWORD dwBufferLen)
1333 BYTE abAi[RSAENH_MAX_HASH_SIZE];
1336 if (!lookup_handle(&handle_table, hHMAC, RSAENH_MAGIC_HASH, (OBJECTHDR**)&pHMAC)) {
1337 SetLastError(NTE_BAD_HASH);
1341 /* compute A_1 = HMAC(seed) */
1343 update_hash(pHMAC, pblobSeed->pbData, pblobSeed->cbData);
1344 finalize_hash(pHMAC);
1345 memcpy(abAi, pHMAC->abHashValue, pHMAC->dwHashSize);
1348 /* compute HMAC(A_i + seed) */
1350 update_hash(pHMAC, abAi, pHMAC->dwHashSize);
1351 update_hash(pHMAC, pblobSeed->pbData, pblobSeed->cbData);
1352 finalize_hash(pHMAC);
1354 /* pseudo random stream := CONCAT_{i=1..n} ( HMAC(A_i + seed) ) */
1356 if (i >= dwBufferLen) break;
1357 pbBuffer[i] ^= pHMAC->abHashValue[i % pHMAC->dwHashSize];
1359 } while (i % pHMAC->dwHashSize);
1361 /* compute A_{i+1} = HMAC(A_i) */
1363 update_hash(pHMAC, abAi, pHMAC->dwHashSize);
1364 finalize_hash(pHMAC);
1365 memcpy(abAi, pHMAC->abHashValue, pHMAC->dwHashSize);
1366 } while (i < dwBufferLen);
1371 /******************************************************************************
1372 * tls1_prf [Internal]
1374 * TLS1 pseudo random function as specified in RFC 2246, chapter 5
1377 * hProv [I] Key container used to compute the pseudo random stream
1378 * hSecret [I] Key that holds the (pre-)master secret
1379 * pblobLabel [I] Descriptive label
1380 * pblobSeed [I] Seed value
1381 * pbBuffer [O] Pseudo random numbers will be stored here
1382 * dwBufferLen [I] Number of pseudo random bytes desired
1388 static BOOL tls1_prf(HCRYPTPROV hProv, HCRYPTPROV hSecret, CONST PCRYPT_DATA_BLOB pblobLabel,
1389 CONST PCRYPT_DATA_BLOB pblobSeed, PBYTE pbBuffer, DWORD dwBufferLen)
1391 HMAC_INFO hmacInfo = { 0, NULL, 0, NULL, 0 };
1392 HCRYPTHASH hHMAC = (HCRYPTHASH)INVALID_HANDLE_VALUE;
1393 HCRYPTKEY hHalfSecret = (HCRYPTKEY)INVALID_HANDLE_VALUE;
1394 CRYPTKEY *pHalfSecret, *pSecret;
1395 DWORD dwHalfSecretLen;
1396 BOOL result = FALSE;
1397 CRYPT_DATA_BLOB blobLabelSeed;
1399 TRACE("(hProv=%08lx, hSecret=%08lx, pblobLabel=%p, pblobSeed=%p, pbBuffer=%p, dwBufferLen=%d)\n",
1400 hProv, hSecret, pblobLabel, pblobSeed, pbBuffer, dwBufferLen);
1402 if (!lookup_handle(&handle_table, hSecret, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pSecret)) {
1403 SetLastError(NTE_FAIL);
1407 dwHalfSecretLen = (pSecret->dwKeyLen+1)/2;
1409 /* concatenation of the label and the seed */
1410 if (!concat_data_blobs(&blobLabelSeed, pblobLabel, pblobSeed)) goto exit;
1412 /* zero out the buffer, since two random streams will be xor'ed into it. */
1413 memset(pbBuffer, 0, dwBufferLen);
1415 /* build a 'fake' key, to hold the secret. CALG_SSL2_MASTER is used since it provides
1416 * the biggest range of valid key lengths. */
1417 hHalfSecret = new_key(hProv, CALG_SSL2_MASTER, MAKELONG(0,dwHalfSecretLen*8), &pHalfSecret);
1418 if (hHalfSecret == (HCRYPTKEY)INVALID_HANDLE_VALUE) goto exit;
1420 /* Derive an HMAC_MD5 hash and call the helper function. */
1421 memcpy(pHalfSecret->abKeyValue, pSecret->abKeyValue, dwHalfSecretLen);
1422 if (!RSAENH_CPCreateHash(hProv, CALG_HMAC, hHalfSecret, 0, &hHMAC)) goto exit;
1423 hmacInfo.HashAlgid = CALG_MD5;
1424 if (!RSAENH_CPSetHashParam(hProv, hHMAC, HP_HMAC_INFO, (BYTE*)&hmacInfo, 0)) goto exit;
1425 if (!tls1_p(hHMAC, &blobLabelSeed, pbBuffer, dwBufferLen)) goto exit;
1427 /* Reconfigure to HMAC_SHA hash and call helper function again. */
1428 memcpy(pHalfSecret->abKeyValue, pSecret->abKeyValue + (pSecret->dwKeyLen/2), dwHalfSecretLen);
1429 hmacInfo.HashAlgid = CALG_SHA;
1430 if (!RSAENH_CPSetHashParam(hProv, hHMAC, HP_HMAC_INFO, (BYTE*)&hmacInfo, 0)) goto exit;
1431 if (!tls1_p(hHMAC, &blobLabelSeed, pbBuffer, dwBufferLen)) goto exit;
1435 release_handle(&handle_table, hHalfSecret, RSAENH_MAGIC_KEY);
1436 if (hHMAC != (HCRYPTHASH)INVALID_HANDLE_VALUE) RSAENH_CPDestroyHash(hProv, hHMAC);
1437 free_data_blob(&blobLabelSeed);
1441 /******************************************************************************
1442 * pad_data [Internal]
1444 * Helper function for data padding according to PKCS1 #2
1447 * abData [I] The data to be padded
1448 * dwDataLen [I] Length of the data
1449 * abBuffer [O] Padded data will be stored here
1450 * dwBufferLen [I] Length of the buffer (also length of padded data)
1451 * dwFlags [I] Padding format (CRYPT_SSL2_FALLBACK)
1455 * Failure: FALSE (NTE_BAD_LEN, too much data to pad)
1457 static BOOL pad_data(CONST BYTE *abData, DWORD dwDataLen, BYTE *abBuffer, DWORD dwBufferLen,
1462 /* Ensure there is enough space for PKCS1 #2 padding */
1463 if (dwDataLen > dwBufferLen-11) {
1464 SetLastError(NTE_BAD_LEN);
1468 memmove(abBuffer + dwBufferLen - dwDataLen, abData, dwDataLen);
1471 abBuffer[1] = RSAENH_PKC_BLOCKTYPE;
1472 for (i=2; i < dwBufferLen - dwDataLen - 1; i++)
1473 do gen_rand_impl(&abBuffer[i], 1); while (!abBuffer[i]);
1474 if (dwFlags & CRYPT_SSL2_FALLBACK)
1475 for (i-=8; i < dwBufferLen - dwDataLen - 1; i++)
1482 /******************************************************************************
1483 * unpad_data [Internal]
1485 * Remove the PKCS1 padding from RSA decrypted data
1488 * abData [I] The padded data
1489 * dwDataLen [I] Length of the padded data
1490 * abBuffer [O] Data without padding will be stored here
1491 * dwBufferLen [I/O] I: Length of the buffer, O: Length of unpadded data
1492 * dwFlags [I] Currently none defined
1496 * Failure: FALSE, (NTE_BAD_DATA, no valid PKCS1 padding or buffer too small)
1498 static BOOL unpad_data(CONST BYTE *abData, DWORD dwDataLen, BYTE *abBuffer, DWORD *dwBufferLen,
1503 for (i=2; i<dwDataLen; i++)
1507 if ((i == dwDataLen) || (*dwBufferLen < dwDataLen - i - 1) ||
1508 (abData[0] != 0x00) || (abData[1] != RSAENH_PKC_BLOCKTYPE))
1510 SetLastError(NTE_BAD_DATA);
1514 *dwBufferLen = dwDataLen - i - 1;
1515 memmove(abBuffer, abData + i + 1, *dwBufferLen);
1519 /******************************************************************************
1520 * CPAcquireContext (RSAENH.@)
1522 * Acquire a handle to the key container specified by pszContainer
1525 * phProv [O] Pointer to the location the acquired handle will be written to.
1526 * pszContainer [I] Name of the desired key container. See Notes
1527 * dwFlags [I] Flags. See Notes.
1528 * pVTable [I] Pointer to a PVTableProvStruct containing callbacks.
1535 * If pszContainer is NULL or points to a zero length string the user's login
1536 * name will be used as the key container name.
1538 * If the CRYPT_NEW_KEYSET flag is set in dwFlags a new keyset will be created.
1539 * If a keyset with the given name already exists, the function fails and sets
1540 * last error to NTE_EXISTS. If CRYPT_NEW_KEYSET is not set and the specified
1541 * key container does not exist, function fails and sets last error to
1544 BOOL WINAPI RSAENH_CPAcquireContext(HCRYPTPROV *phProv, LPSTR pszContainer,
1545 DWORD dwFlags, PVTableProvStruc pVTable)
1547 CHAR szKeyContainerName[MAX_PATH];
1549 TRACE("(phProv=%p, pszContainer=%s, dwFlags=%08x, pVTable=%p)\n", phProv,
1550 debugstr_a(pszContainer), dwFlags, pVTable);
1552 if (pszContainer && *pszContainer)
1554 lstrcpynA(szKeyContainerName, pszContainer, MAX_PATH);
1558 DWORD dwLen = sizeof(szKeyContainerName);
1559 if (!GetUserNameA(szKeyContainerName, &dwLen)) return FALSE;
1562 switch (dwFlags & (CRYPT_NEWKEYSET|CRYPT_VERIFYCONTEXT|CRYPT_DELETEKEYSET))
1565 *phProv = read_key_container(szKeyContainerName, dwFlags, pVTable);
1568 case CRYPT_DELETEKEYSET:
1569 return delete_container_key(szKeyContainerName, dwFlags);
1571 case CRYPT_NEWKEYSET:
1572 *phProv = read_key_container(szKeyContainerName, dwFlags, pVTable);
1573 if (*phProv != (HCRYPTPROV)INVALID_HANDLE_VALUE)
1575 release_handle(&handle_table, *phProv, RSAENH_MAGIC_CONTAINER);
1576 TRACE("Can't create new keyset, already exists\n");
1577 SetLastError(NTE_EXISTS);
1580 *phProv = new_key_container(szKeyContainerName, dwFlags, pVTable);
1583 case CRYPT_VERIFYCONTEXT|CRYPT_NEWKEYSET:
1584 case CRYPT_VERIFYCONTEXT:
1585 if (pszContainer && *pszContainer) {
1586 TRACE("pszContainer should be empty\n");
1587 SetLastError(NTE_BAD_FLAGS);
1590 *phProv = new_key_container("", dwFlags, pVTable);
1594 *phProv = (HCRYPTPROV)INVALID_HANDLE_VALUE;
1595 SetLastError(NTE_BAD_FLAGS);
1599 if (*phProv != (HCRYPTPROV)INVALID_HANDLE_VALUE) {
1600 SetLastError(ERROR_SUCCESS);
1607 /******************************************************************************
1608 * CPCreateHash (RSAENH.@)
1610 * CPCreateHash creates and initalizes a new hash object.
1613 * hProv [I] Handle to the key container to which the new hash will belong.
1614 * Algid [I] Identifies the hash algorithm, which will be used for the hash.
1615 * hKey [I] Handle to a session key applied for keyed hashes.
1616 * dwFlags [I] Currently no flags defined. Must be zero.
1617 * phHash [O] Points to the location where a handle to the new hash will be stored.
1624 * hKey is a handle to a session key applied in keyed hashes like MAC and HMAC.
1625 * If a normal hash object is to be created (like e.g. MD2 or SHA1) hKey must be zero.
1627 BOOL WINAPI RSAENH_CPCreateHash(HCRYPTPROV hProv, ALG_ID Algid, HCRYPTKEY hKey, DWORD dwFlags,
1630 CRYPTKEY *pCryptKey;
1631 CRYPTHASH *pCryptHash;
1632 const PROV_ENUMALGS_EX *peaAlgidInfo;
1634 TRACE("(hProv=%08lx, Algid=%08x, hKey=%08lx, dwFlags=%08x, phHash=%p)\n", hProv, Algid, hKey,
1637 peaAlgidInfo = get_algid_info(hProv, Algid);
1638 if (!peaAlgidInfo) return FALSE;
1642 SetLastError(NTE_BAD_FLAGS);
1646 if (Algid == CALG_MAC || Algid == CALG_HMAC || Algid == CALG_SCHANNEL_MASTER_HASH ||
1647 Algid == CALG_TLS1PRF)
1649 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey)) {
1650 SetLastError(NTE_BAD_KEY);
1654 if ((Algid == CALG_MAC) && (GET_ALG_TYPE(pCryptKey->aiAlgid) != ALG_TYPE_BLOCK)) {
1655 SetLastError(NTE_BAD_KEY);
1659 if ((Algid == CALG_SCHANNEL_MASTER_HASH || Algid == CALG_TLS1PRF) &&
1660 (pCryptKey->aiAlgid != CALG_TLS1_MASTER))
1662 SetLastError(NTE_BAD_KEY);
1666 if ((Algid == CALG_TLS1PRF) && (pCryptKey->dwState != RSAENH_KEYSTATE_MASTERKEY)) {
1667 SetLastError(NTE_BAD_KEY_STATE);
1672 *phHash = new_object(&handle_table, sizeof(CRYPTHASH), RSAENH_MAGIC_HASH,
1673 destroy_hash, (OBJECTHDR**)&pCryptHash);
1674 if (!pCryptHash) return FALSE;
1676 pCryptHash->aiAlgid = Algid;
1677 pCryptHash->hKey = hKey;
1678 pCryptHash->hProv = hProv;
1679 pCryptHash->dwState = RSAENH_HASHSTATE_HASHING;
1680 pCryptHash->pHMACInfo = NULL;
1681 pCryptHash->dwHashSize = peaAlgidInfo->dwDefaultLen >> 3;
1682 init_data_blob(&pCryptHash->tpPRFParams.blobLabel);
1683 init_data_blob(&pCryptHash->tpPRFParams.blobSeed);
1685 if (Algid == CALG_SCHANNEL_MASTER_HASH) {
1686 static const char keyex[] = "key expansion";
1687 BYTE key_expansion[sizeof keyex];
1688 CRYPT_DATA_BLOB blobRandom, blobKeyExpansion = { 13, key_expansion };
1690 memcpy( key_expansion, keyex, sizeof keyex );
1692 if (pCryptKey->dwState != RSAENH_KEYSTATE_MASTERKEY) {
1693 static const char msec[] = "master secret";
1694 BYTE master_secret[sizeof msec];
1695 CRYPT_DATA_BLOB blobLabel = { 13, master_secret };
1696 BYTE abKeyValue[48];
1698 memcpy( master_secret, msec, sizeof msec );
1700 /* See RFC 2246, chapter 8.1 */
1701 if (!concat_data_blobs(&blobRandom,
1702 &pCryptKey->siSChannelInfo.blobClientRandom,
1703 &pCryptKey->siSChannelInfo.blobServerRandom))
1707 tls1_prf(hProv, hKey, &blobLabel, &blobRandom, abKeyValue, 48);
1708 pCryptKey->dwState = RSAENH_KEYSTATE_MASTERKEY;
1709 memcpy(pCryptKey->abKeyValue, abKeyValue, 48);
1710 free_data_blob(&blobRandom);
1713 /* See RFC 2246, chapter 6.3 */
1714 if (!concat_data_blobs(&blobRandom,
1715 &pCryptKey->siSChannelInfo.blobServerRandom,
1716 &pCryptKey->siSChannelInfo.blobClientRandom))
1720 tls1_prf(hProv, hKey, &blobKeyExpansion, &blobRandom, pCryptHash->abHashValue,
1721 RSAENH_MAX_HASH_SIZE);
1722 free_data_blob(&blobRandom);
1725 return init_hash(pCryptHash);
1728 /******************************************************************************
1729 * CPDestroyHash (RSAENH.@)
1731 * Releases the handle to a hash object. The object is destroyed if it's reference
1732 * count reaches zero.
1735 * hProv [I] Handle to the key container to which the hash object belongs.
1736 * hHash [I] Handle to the hash object to be released.
1742 BOOL WINAPI RSAENH_CPDestroyHash(HCRYPTPROV hProv, HCRYPTHASH hHash)
1744 TRACE("(hProv=%08lx, hHash=%08lx)\n", hProv, hHash);
1746 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
1748 SetLastError(NTE_BAD_UID);
1752 if (!release_handle(&handle_table, hHash, RSAENH_MAGIC_HASH))
1754 SetLastError(NTE_BAD_HASH);
1761 /******************************************************************************
1762 * CPDestroyKey (RSAENH.@)
1764 * Releases the handle to a key object. The object is destroyed if it's reference
1765 * count reaches zero.
1768 * hProv [I] Handle to the key container to which the key object belongs.
1769 * hKey [I] Handle to the key object to be released.
1775 BOOL WINAPI RSAENH_CPDestroyKey(HCRYPTPROV hProv, HCRYPTKEY hKey)
1777 TRACE("(hProv=%08lx, hKey=%08lx)\n", hProv, hKey);
1779 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
1781 SetLastError(NTE_BAD_UID);
1785 if (!release_handle(&handle_table, hKey, RSAENH_MAGIC_KEY))
1787 SetLastError(NTE_BAD_KEY);
1794 /******************************************************************************
1795 * CPDuplicateHash (RSAENH.@)
1797 * Clones a hash object including it's current state.
1800 * hUID [I] Handle to the key container the hash belongs to.
1801 * hHash [I] Handle to the hash object to be cloned.
1802 * pdwReserved [I] Reserved. Must be NULL.
1803 * dwFlags [I] No flags are currently defined. Must be 0.
1804 * phHash [O] Handle to the cloned hash object.
1810 BOOL WINAPI RSAENH_CPDuplicateHash(HCRYPTPROV hUID, HCRYPTHASH hHash, DWORD *pdwReserved,
1811 DWORD dwFlags, HCRYPTHASH *phHash)
1813 CRYPTHASH *pSrcHash, *pDestHash;
1815 TRACE("(hUID=%08lx, hHash=%08lx, pdwReserved=%p, dwFlags=%08x, phHash=%p)\n", hUID, hHash,
1816 pdwReserved, dwFlags, phHash);
1818 if (!is_valid_handle(&handle_table, hUID, RSAENH_MAGIC_CONTAINER))
1820 SetLastError(NTE_BAD_UID);
1824 if (!lookup_handle(&handle_table, hHash, RSAENH_MAGIC_HASH, (OBJECTHDR**)&pSrcHash))
1826 SetLastError(NTE_BAD_HASH);
1830 if (!phHash || pdwReserved || dwFlags)
1832 SetLastError(ERROR_INVALID_PARAMETER);
1836 *phHash = new_object(&handle_table, sizeof(CRYPTHASH), RSAENH_MAGIC_HASH,
1837 destroy_hash, (OBJECTHDR**)&pDestHash);
1838 if (*phHash != (HCRYPTHASH)INVALID_HANDLE_VALUE)
1840 *pDestHash = *pSrcHash;
1841 duplicate_hash_impl(pSrcHash->aiAlgid, &pSrcHash->context, &pDestHash->context);
1842 copy_hmac_info(&pDestHash->pHMACInfo, pSrcHash->pHMACInfo);
1843 copy_data_blob(&pDestHash->tpPRFParams.blobLabel, &pSrcHash->tpPRFParams.blobLabel);
1844 copy_data_blob(&pDestHash->tpPRFParams.blobSeed, &pSrcHash->tpPRFParams.blobSeed);
1847 return *phHash != (HCRYPTHASH)INVALID_HANDLE_VALUE;
1850 /******************************************************************************
1851 * CPDuplicateKey (RSAENH.@)
1853 * Clones a key object including it's current state.
1856 * hUID [I] Handle to the key container the hash belongs to.
1857 * hKey [I] Handle to the key object to be cloned.
1858 * pdwReserved [I] Reserved. Must be NULL.
1859 * dwFlags [I] No flags are currently defined. Must be 0.
1860 * phHash [O] Handle to the cloned key object.
1866 BOOL WINAPI RSAENH_CPDuplicateKey(HCRYPTPROV hUID, HCRYPTKEY hKey, DWORD *pdwReserved,
1867 DWORD dwFlags, HCRYPTKEY *phKey)
1869 CRYPTKEY *pSrcKey, *pDestKey;
1871 TRACE("(hUID=%08lx, hKey=%08lx, pdwReserved=%p, dwFlags=%08x, phKey=%p)\n", hUID, hKey,
1872 pdwReserved, dwFlags, phKey);
1874 if (!is_valid_handle(&handle_table, hUID, RSAENH_MAGIC_CONTAINER))
1876 SetLastError(NTE_BAD_UID);
1880 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pSrcKey))
1882 SetLastError(NTE_BAD_KEY);
1886 if (!phKey || pdwReserved || dwFlags)
1888 SetLastError(ERROR_INVALID_PARAMETER);
1892 *phKey = new_object(&handle_table, sizeof(CRYPTKEY), RSAENH_MAGIC_KEY, destroy_key,
1893 (OBJECTHDR**)&pDestKey);
1894 if (*phKey != (HCRYPTKEY)INVALID_HANDLE_VALUE)
1896 *pDestKey = *pSrcKey;
1897 copy_data_blob(&pDestKey->siSChannelInfo.blobServerRandom,
1898 &pSrcKey->siSChannelInfo.blobServerRandom);
1899 copy_data_blob(&pDestKey->siSChannelInfo.blobClientRandom,
1900 &pSrcKey->siSChannelInfo.blobClientRandom);
1901 duplicate_key_impl(pSrcKey->aiAlgid, &pSrcKey->context, &pDestKey->context);
1910 /******************************************************************************
1911 * CPEncrypt (RSAENH.@)
1916 * hProv [I] The key container hKey and hHash belong to.
1917 * hKey [I] The key used to encrypt the data.
1918 * hHash [I] An optional hash object for parallel hashing. See notes.
1919 * Final [I] Indicates if this is the last block of data to encrypt.
1920 * dwFlags [I] Currently no flags defined. Must be zero.
1921 * pbData [I/O] Pointer to the data to encrypt. Encrypted data will also be stored there.
1922 * pdwDataLen [I/O] I: Length of data to encrypt, O: Length of encrypted data.
1923 * dwBufLen [I] Size of the buffer at pbData.
1930 * If a hash object handle is provided in hHash, it will be updated with the plaintext.
1931 * This is useful for message signatures.
1933 * This function uses the standard WINAPI protocol for querying data of dynamic length.
1935 BOOL WINAPI RSAENH_CPEncrypt(HCRYPTPROV hProv, HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final,
1936 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen, DWORD dwBufLen)
1938 CRYPTKEY *pCryptKey;
1939 BYTE *in, out[RSAENH_MAX_BLOCK_SIZE], o[RSAENH_MAX_BLOCK_SIZE];
1940 DWORD dwEncryptedLen, i, j, k;
1942 TRACE("(hProv=%08lx, hKey=%08lx, hHash=%08lx, Final=%d, dwFlags=%08x, pbData=%p, "
1943 "pdwDataLen=%p, dwBufLen=%d)\n", hProv, hKey, hHash, Final, dwFlags, pbData, pdwDataLen,
1946 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
1948 SetLastError(NTE_BAD_UID);
1954 SetLastError(NTE_BAD_FLAGS);
1958 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
1960 SetLastError(NTE_BAD_KEY);
1964 if (pCryptKey->dwState == RSAENH_KEYSTATE_IDLE)
1965 pCryptKey->dwState = RSAENH_KEYSTATE_ENCRYPTING;
1967 if (pCryptKey->dwState != RSAENH_KEYSTATE_ENCRYPTING)
1969 SetLastError(NTE_BAD_DATA);
1973 if (is_valid_handle(&handle_table, hHash, RSAENH_MAGIC_HASH)) {
1974 if (!RSAENH_CPHashData(hProv, hHash, pbData, *pdwDataLen, 0)) return FALSE;
1977 if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_BLOCK) {
1978 if (!Final && (*pdwDataLen % pCryptKey->dwBlockLen)) {
1979 SetLastError(NTE_BAD_DATA);
1983 dwEncryptedLen = (*pdwDataLen/pCryptKey->dwBlockLen+(Final?1:0))*pCryptKey->dwBlockLen;
1985 if (pbData == NULL) {
1986 *pdwDataLen = dwEncryptedLen;
1989 else if (dwEncryptedLen > dwBufLen) {
1990 *pdwDataLen = dwEncryptedLen;
1991 SetLastError(ERROR_MORE_DATA);
1995 /* Pad final block with length bytes */
1996 for (i=*pdwDataLen; i<dwEncryptedLen; i++) pbData[i] = dwEncryptedLen - *pdwDataLen;
1997 *pdwDataLen = dwEncryptedLen;
1999 for (i=0, in=pbData; i<*pdwDataLen; i+=pCryptKey->dwBlockLen, in+=pCryptKey->dwBlockLen) {
2000 switch (pCryptKey->dwMode) {
2001 case CRYPT_MODE_ECB:
2002 encrypt_block_impl(pCryptKey->aiAlgid, 0, &pCryptKey->context, in, out,
2006 case CRYPT_MODE_CBC:
2007 for (j=0; j<pCryptKey->dwBlockLen; j++) in[j] ^= pCryptKey->abChainVector[j];
2008 encrypt_block_impl(pCryptKey->aiAlgid, 0, &pCryptKey->context, in, out,
2010 memcpy(pCryptKey->abChainVector, out, pCryptKey->dwBlockLen);
2013 case CRYPT_MODE_CFB:
2014 for (j=0; j<pCryptKey->dwBlockLen; j++) {
2015 encrypt_block_impl(pCryptKey->aiAlgid, 0, &pCryptKey->context,
2016 pCryptKey->abChainVector, o, RSAENH_ENCRYPT);
2017 out[j] = in[j] ^ o[0];
2018 for (k=0; k<pCryptKey->dwBlockLen-1; k++)
2019 pCryptKey->abChainVector[k] = pCryptKey->abChainVector[k+1];
2020 pCryptKey->abChainVector[k] = out[j];
2025 SetLastError(NTE_BAD_ALGID);
2028 memcpy(in, out, pCryptKey->dwBlockLen);
2030 } else if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_STREAM) {
2031 if (pbData == NULL) {
2032 *pdwDataLen = dwBufLen;
2035 encrypt_stream_impl(pCryptKey->aiAlgid, &pCryptKey->context, pbData, *pdwDataLen);
2036 } else if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_RSA) {
2037 if (pCryptKey->aiAlgid == CALG_RSA_SIGN) {
2038 SetLastError(NTE_BAD_KEY);
2042 *pdwDataLen = pCryptKey->dwBlockLen;
2045 if (dwBufLen < pCryptKey->dwBlockLen) {
2046 SetLastError(ERROR_MORE_DATA);
2049 if (!pad_data(pbData, *pdwDataLen, pbData, pCryptKey->dwBlockLen, dwFlags)) return FALSE;
2050 encrypt_block_impl(pCryptKey->aiAlgid, PK_PUBLIC, &pCryptKey->context, pbData, pbData, RSAENH_ENCRYPT);
2051 *pdwDataLen = pCryptKey->dwBlockLen;
2054 SetLastError(NTE_BAD_TYPE);
2058 if (Final) setup_key(pCryptKey);
2063 /******************************************************************************
2064 * CPDecrypt (RSAENH.@)
2069 * hProv [I] The key container hKey and hHash belong to.
2070 * hKey [I] The key used to decrypt the data.
2071 * hHash [I] An optional hash object for parallel hashing. See notes.
2072 * Final [I] Indicates if this is the last block of data to decrypt.
2073 * dwFlags [I] Currently no flags defined. Must be zero.
2074 * pbData [I/O] Pointer to the data to decrypt. Plaintext will also be stored there.
2075 * pdwDataLen [I/O] I: Length of ciphertext, O: Length of plaintext.
2082 * If a hash object handle is provided in hHash, it will be updated with the plaintext.
2083 * This is useful for message signatures.
2085 * This function uses the standard WINAPI protocol for querying data of dynamic length.
2087 BOOL WINAPI RSAENH_CPDecrypt(HCRYPTPROV hProv, HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final,
2088 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
2090 CRYPTKEY *pCryptKey;
2091 BYTE *in, out[RSAENH_MAX_BLOCK_SIZE], o[RSAENH_MAX_BLOCK_SIZE];
2095 TRACE("(hProv=%08lx, hKey=%08lx, hHash=%08lx, Final=%d, dwFlags=%08x, pbData=%p, "
2096 "pdwDataLen=%p)\n", hProv, hKey, hHash, Final, dwFlags, pbData, pdwDataLen);
2098 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
2100 SetLastError(NTE_BAD_UID);
2106 SetLastError(NTE_BAD_FLAGS);
2110 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
2112 SetLastError(NTE_BAD_KEY);
2116 if (pCryptKey->dwState == RSAENH_KEYSTATE_IDLE)
2117 pCryptKey->dwState = RSAENH_KEYSTATE_ENCRYPTING;
2119 if (pCryptKey->dwState != RSAENH_KEYSTATE_ENCRYPTING)
2121 SetLastError(NTE_BAD_DATA);
2127 if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_BLOCK) {
2128 for (i=0, in=pbData; i<*pdwDataLen; i+=pCryptKey->dwBlockLen, in+=pCryptKey->dwBlockLen) {
2129 switch (pCryptKey->dwMode) {
2130 case CRYPT_MODE_ECB:
2131 encrypt_block_impl(pCryptKey->aiAlgid, 0, &pCryptKey->context, in, out,
2135 case CRYPT_MODE_CBC:
2136 encrypt_block_impl(pCryptKey->aiAlgid, 0, &pCryptKey->context, in, out,
2138 for (j=0; j<pCryptKey->dwBlockLen; j++) out[j] ^= pCryptKey->abChainVector[j];
2139 memcpy(pCryptKey->abChainVector, in, pCryptKey->dwBlockLen);
2142 case CRYPT_MODE_CFB:
2143 for (j=0; j<pCryptKey->dwBlockLen; j++) {
2144 encrypt_block_impl(pCryptKey->aiAlgid, 0, &pCryptKey->context,
2145 pCryptKey->abChainVector, o, RSAENH_ENCRYPT);
2146 out[j] = in[j] ^ o[0];
2147 for (k=0; k<pCryptKey->dwBlockLen-1; k++)
2148 pCryptKey->abChainVector[k] = pCryptKey->abChainVector[k+1];
2149 pCryptKey->abChainVector[k] = in[j];
2154 SetLastError(NTE_BAD_ALGID);
2157 memcpy(in, out, pCryptKey->dwBlockLen);
2160 if (pbData[*pdwDataLen-1] &&
2161 pbData[*pdwDataLen-1] <= pCryptKey->dwBlockLen &&
2162 pbData[*pdwDataLen-1] < *pdwDataLen) {
2163 BOOL padOkay = TRUE;
2165 /* check that every bad byte has the same value */
2166 for (i = 1; padOkay && i < pbData[*pdwDataLen-1]; i++)
2167 if (pbData[*pdwDataLen - i - 1] != pbData[*pdwDataLen - 1])
2170 *pdwDataLen -= pbData[*pdwDataLen-1];
2172 SetLastError(NTE_BAD_DATA);
2177 SetLastError(NTE_BAD_DATA);
2182 } else if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_STREAM) {
2183 encrypt_stream_impl(pCryptKey->aiAlgid, &pCryptKey->context, pbData, *pdwDataLen);
2184 } else if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_RSA) {
2185 if (pCryptKey->aiAlgid == CALG_RSA_SIGN) {
2186 SetLastError(NTE_BAD_KEY);
2189 encrypt_block_impl(pCryptKey->aiAlgid, PK_PRIVATE, &pCryptKey->context, pbData, pbData, RSAENH_DECRYPT);
2190 if (!unpad_data(pbData, pCryptKey->dwBlockLen, pbData, pdwDataLen, dwFlags)) return FALSE;
2193 SetLastError(NTE_BAD_TYPE);
2197 if (Final) setup_key(pCryptKey);
2199 if (is_valid_handle(&handle_table, hHash, RSAENH_MAGIC_HASH)) {
2200 if (*pdwDataLen>dwMax ||
2201 !RSAENH_CPHashData(hProv, hHash, pbData, *pdwDataLen, 0)) return FALSE;
2207 /******************************************************************************
2208 * CPExportKey (RSAENH.@)
2210 * Export a key into a binary large object (BLOB).
2213 * hProv [I] Key container from which a key is to be exported.
2214 * hKey [I] Key to be exported.
2215 * hPubKey [I] Key used to encrypt sensitive BLOB data.
2216 * dwBlobType [I] SIMPLEBLOB, PUBLICKEYBLOB or PRIVATEKEYBLOB.
2217 * dwFlags [I] Currently none defined.
2218 * pbData [O] Pointer to a buffer where the BLOB will be written to.
2219 * pdwDataLen [I/O] I: Size of buffer at pbData, O: Size of BLOB
2225 BOOL WINAPI RSAENH_CPExportKey(HCRYPTPROV hProv, HCRYPTKEY hKey, HCRYPTKEY hPubKey,
2226 DWORD dwBlobType, DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
2228 CRYPTKEY *pCryptKey, *pPubKey;
2229 BLOBHEADER *pBlobHeader = (BLOBHEADER*)pbData;
2230 RSAPUBKEY *pRSAPubKey = (RSAPUBKEY*)(pBlobHeader+1);
2231 ALG_ID *pAlgid = (ALG_ID*)(pBlobHeader+1);
2234 TRACE("(hProv=%08lx, hKey=%08lx, hPubKey=%08lx, dwBlobType=%08x, dwFlags=%08x, pbData=%p,"
2235 "pdwDataLen=%p)\n", hProv, hKey, hPubKey, dwBlobType, dwFlags, pbData, pdwDataLen);
2237 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
2239 SetLastError(NTE_BAD_UID);
2243 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
2245 SetLastError(NTE_BAD_KEY);
2249 if (dwFlags & CRYPT_SSL2_FALLBACK) {
2250 if (pCryptKey->aiAlgid != CALG_SSL2_MASTER) {
2251 SetLastError(NTE_BAD_KEY);
2256 switch ((BYTE)dwBlobType)
2259 if (!lookup_handle(&handle_table, hPubKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pPubKey)){
2260 SetLastError(NTE_BAD_PUBLIC_KEY); /* FIXME: error_code? */
2264 if (!(GET_ALG_CLASS(pCryptKey->aiAlgid)&(ALG_CLASS_DATA_ENCRYPT|ALG_CLASS_MSG_ENCRYPT))) {
2265 SetLastError(NTE_BAD_KEY); /* FIXME: error code? */
2269 dwDataLen = sizeof(BLOBHEADER) + sizeof(ALG_ID) + pPubKey->dwBlockLen;
2271 if (*pdwDataLen < dwDataLen) {
2272 SetLastError(ERROR_MORE_DATA);
2273 *pdwDataLen = dwDataLen;
2277 pBlobHeader->bType = SIMPLEBLOB;
2278 pBlobHeader->bVersion = CUR_BLOB_VERSION;
2279 pBlobHeader->reserved = 0;
2280 pBlobHeader->aiKeyAlg = pCryptKey->aiAlgid;
2282 *pAlgid = pPubKey->aiAlgid;
2284 if (!pad_data(pCryptKey->abKeyValue, pCryptKey->dwKeyLen, (BYTE*)(pAlgid+1),
2285 pPubKey->dwBlockLen, dwFlags))
2290 encrypt_block_impl(pPubKey->aiAlgid, PK_PUBLIC, &pPubKey->context, (BYTE*)(pAlgid+1),
2291 (BYTE*)(pAlgid+1), RSAENH_ENCRYPT);
2293 *pdwDataLen = dwDataLen;
2297 if (is_valid_handle(&handle_table, hPubKey, RSAENH_MAGIC_KEY)) {
2298 SetLastError(NTE_BAD_KEY); /* FIXME: error code? */
2302 if ((pCryptKey->aiAlgid != CALG_RSA_KEYX) && (pCryptKey->aiAlgid != CALG_RSA_SIGN)) {
2303 SetLastError(NTE_BAD_KEY);
2307 dwDataLen = sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) + pCryptKey->dwKeyLen;
2309 if (*pdwDataLen < dwDataLen) {
2310 SetLastError(ERROR_MORE_DATA);
2311 *pdwDataLen = dwDataLen;
2315 pBlobHeader->bType = PUBLICKEYBLOB;
2316 pBlobHeader->bVersion = CUR_BLOB_VERSION;
2317 pBlobHeader->reserved = 0;
2318 pBlobHeader->aiKeyAlg = pCryptKey->aiAlgid;
2320 pRSAPubKey->magic = RSAENH_MAGIC_RSA1;
2321 pRSAPubKey->bitlen = pCryptKey->dwKeyLen << 3;
2323 export_public_key_impl((BYTE*)(pRSAPubKey+1), &pCryptKey->context,
2324 pCryptKey->dwKeyLen, &pRSAPubKey->pubexp);
2326 *pdwDataLen = dwDataLen;
2329 case PRIVATEKEYBLOB:
2330 if ((pCryptKey->aiAlgid != CALG_RSA_KEYX) && (pCryptKey->aiAlgid != CALG_RSA_SIGN)) {
2331 SetLastError(NTE_BAD_KEY);
2335 dwDataLen = sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) +
2336 2 * pCryptKey->dwKeyLen + 5 * ((pCryptKey->dwKeyLen + 1) >> 1);
2338 if (*pdwDataLen < dwDataLen) {
2339 SetLastError(ERROR_MORE_DATA);
2340 *pdwDataLen = dwDataLen;
2344 pBlobHeader->bType = PRIVATEKEYBLOB;
2345 pBlobHeader->bVersion = CUR_BLOB_VERSION;
2346 pBlobHeader->reserved = 0;
2347 pBlobHeader->aiKeyAlg = pCryptKey->aiAlgid;
2349 pRSAPubKey->magic = RSAENH_MAGIC_RSA2;
2350 pRSAPubKey->bitlen = pCryptKey->dwKeyLen << 3;
2352 export_private_key_impl((BYTE*)(pRSAPubKey+1), &pCryptKey->context,
2353 pCryptKey->dwKeyLen, &pRSAPubKey->pubexp);
2355 *pdwDataLen = dwDataLen;
2359 SetLastError(NTE_BAD_TYPE); /* FIXME: error code? */
2364 /******************************************************************************
2365 * CPImportKey (RSAENH.@)
2367 * Import a BLOB'ed key into a key container.
2370 * hProv [I] Key container into which the key is to be imported.
2371 * pbData [I] Pointer to a buffer which holds the BLOB.
2372 * dwDataLen [I] Length of data in buffer at pbData.
2373 * hPubKey [I] Key used to decrypt sensitive BLOB data.
2374 * dwFlags [I] Currently none defined.
2375 * phKey [O] Handle to the imported key.
2381 BOOL WINAPI RSAENH_CPImportKey(HCRYPTPROV hProv, CONST BYTE *pbData, DWORD dwDataLen,
2382 HCRYPTKEY hPubKey, DWORD dwFlags, HCRYPTKEY *phKey)
2384 KEYCONTAINER *pKeyContainer;
2385 CRYPTKEY *pCryptKey, *pPubKey;
2386 CONST BLOBHEADER *pBlobHeader = (CONST BLOBHEADER*)pbData;
2387 CONST RSAPUBKEY *pRSAPubKey = (CONST RSAPUBKEY*)(pBlobHeader+1);
2388 CONST ALG_ID *pAlgid = (CONST ALG_ID*)(pBlobHeader+1);
2389 CONST BYTE *pbKeyStream = (CONST BYTE*)(pAlgid + 1);
2395 TRACE("(hProv=%08lx, pbData=%p, dwDataLen=%d, hPubKey=%08lx, dwFlags=%08x, phKey=%p)\n",
2396 hProv, pbData, dwDataLen, hPubKey, dwFlags, phKey);
2398 if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER,
2399 (OBJECTHDR**)&pKeyContainer))
2401 SetLastError(NTE_BAD_UID);
2405 if (dwDataLen < sizeof(BLOBHEADER) ||
2406 pBlobHeader->bVersion != CUR_BLOB_VERSION ||
2407 pBlobHeader->reserved != 0)
2409 SetLastError(NTE_BAD_DATA);
2413 switch (pBlobHeader->bType)
2415 case PRIVATEKEYBLOB:
2416 if ((dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY)) ||
2417 (pRSAPubKey->magic != RSAENH_MAGIC_RSA2) ||
2418 (dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) +
2419 (2 * pRSAPubKey->bitlen >> 3) + (5 * ((pRSAPubKey->bitlen+8)>>4))))
2421 SetLastError(NTE_BAD_DATA);
2425 *phKey = new_key(hProv, pBlobHeader->aiKeyAlg, MAKELONG(0,pRSAPubKey->bitlen), &pCryptKey);
2426 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) return FALSE;
2427 setup_key(pCryptKey);
2428 ret = import_private_key_impl((CONST BYTE*)(pRSAPubKey+1), &pCryptKey->context,
2429 pRSAPubKey->bitlen/8, pRSAPubKey->pubexp);
2431 switch (pBlobHeader->aiKeyAlg)
2435 TRACE("installing signing key\n");
2436 RSAENH_CPDestroyKey(hProv, pKeyContainer->hSignatureKeyPair);
2437 copy_handle(&handle_table, *phKey, RSAENH_MAGIC_KEY,
2438 &pKeyContainer->hSignatureKeyPair);
2440 case AT_KEYEXCHANGE:
2442 TRACE("installing key exchange key\n");
2443 RSAENH_CPDestroyKey(hProv, pKeyContainer->hKeyExchangeKeyPair);
2444 copy_handle(&handle_table, *phKey, RSAENH_MAGIC_KEY,
2445 &pKeyContainer->hKeyExchangeKeyPair);
2452 if ((dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY)) ||
2453 (pRSAPubKey->magic != RSAENH_MAGIC_RSA1) ||
2454 (dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) + (pRSAPubKey->bitlen >> 3)))
2456 SetLastError(NTE_BAD_DATA);
2460 /* Since this is a public key blob, only the public key is
2461 * available, so only signature verification is possible.
2463 algID = pBlobHeader->aiKeyAlg;
2464 *phKey = new_key(hProv, algID, MAKELONG(0,pRSAPubKey->bitlen), &pCryptKey);
2465 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) return FALSE;
2466 setup_key(pCryptKey);
2467 ret = import_public_key_impl((CONST BYTE*)(pRSAPubKey+1), &pCryptKey->context,
2468 pRSAPubKey->bitlen >> 3, pRSAPubKey->pubexp);
2470 switch (pBlobHeader->aiKeyAlg)
2472 case AT_KEYEXCHANGE:
2474 TRACE("installing public key\n");
2475 RSAENH_CPDestroyKey(hProv, pKeyContainer->hKeyExchangeKeyPair);
2476 copy_handle(&handle_table, *phKey, RSAENH_MAGIC_KEY,
2477 &pKeyContainer->hKeyExchangeKeyPair);
2484 if (!lookup_handle(&handle_table, hPubKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pPubKey) ||
2485 pPubKey->aiAlgid != CALG_RSA_KEYX)
2487 SetLastError(NTE_BAD_PUBLIC_KEY); /* FIXME: error code? */
2491 if (dwDataLen < sizeof(BLOBHEADER)+sizeof(ALG_ID)+pPubKey->dwBlockLen)
2493 SetLastError(NTE_BAD_DATA); /* FIXME: error code */
2497 pbDecrypted = HeapAlloc(GetProcessHeap(), 0, pPubKey->dwBlockLen);
2498 if (!pbDecrypted) return FALSE;
2499 encrypt_block_impl(pPubKey->aiAlgid, PK_PRIVATE, &pPubKey->context, pbKeyStream, pbDecrypted,
2502 dwKeyLen = RSAENH_MAX_KEY_SIZE;
2503 if (!unpad_data(pbDecrypted, pPubKey->dwBlockLen, pbDecrypted, &dwKeyLen, dwFlags)) {
2504 HeapFree(GetProcessHeap(), 0, pbDecrypted);
2508 *phKey = new_key(hProv, pBlobHeader->aiKeyAlg, dwKeyLen<<19, &pCryptKey);
2509 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE)
2511 HeapFree(GetProcessHeap(), 0, pbDecrypted);
2514 memcpy(pCryptKey->abKeyValue, pbDecrypted, dwKeyLen);
2515 HeapFree(GetProcessHeap(), 0, pbDecrypted);
2516 setup_key(pCryptKey);
2520 SetLastError(NTE_BAD_TYPE); /* FIXME: error code? */
2525 /******************************************************************************
2526 * CPGenKey (RSAENH.@)
2528 * Generate a key in the key container
2531 * hProv [I] Key container for which a key is to be generated.
2532 * Algid [I] Crypto algorithm identifier for the key to be generated.
2533 * dwFlags [I] Upper 16 bits: Binary length of key. Lower 16 bits: Flags. See Notes
2534 * phKey [O] Handle to the generated key.
2541 * Flags currently not considered.
2544 * Private key-exchange- and signature-keys can be generated with Algid AT_KEYEXCHANGE
2545 * and AT_SIGNATURE values.
2547 BOOL WINAPI RSAENH_CPGenKey(HCRYPTPROV hProv, ALG_ID Algid, DWORD dwFlags, HCRYPTKEY *phKey)
2549 KEYCONTAINER *pKeyContainer;
2550 CRYPTKEY *pCryptKey;
2552 TRACE("(hProv=%08lx, aiAlgid=%d, dwFlags=%08x, phKey=%p)\n", hProv, Algid, dwFlags, phKey);
2554 if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER,
2555 (OBJECTHDR**)&pKeyContainer))
2557 /* MSDN: hProv not containing valid context handle */
2558 SetLastError(NTE_BAD_UID);
2566 *phKey = new_key(hProv, CALG_RSA_SIGN, dwFlags, &pCryptKey);
2568 new_key_impl(pCryptKey->aiAlgid, &pCryptKey->context, pCryptKey->dwKeyLen);
2569 setup_key(pCryptKey);
2570 if (Algid == AT_SIGNATURE) {
2571 RSAENH_CPDestroyKey(hProv, pKeyContainer->hSignatureKeyPair);
2572 copy_handle(&handle_table, *phKey, RSAENH_MAGIC_KEY,
2573 &pKeyContainer->hSignatureKeyPair);
2578 case AT_KEYEXCHANGE:
2580 *phKey = new_key(hProv, CALG_RSA_KEYX, dwFlags, &pCryptKey);
2582 new_key_impl(pCryptKey->aiAlgid, &pCryptKey->context, pCryptKey->dwKeyLen);
2583 setup_key(pCryptKey);
2584 if (Algid == AT_KEYEXCHANGE) {
2585 RSAENH_CPDestroyKey(hProv, pKeyContainer->hKeyExchangeKeyPair);
2586 copy_handle(&handle_table, *phKey, RSAENH_MAGIC_KEY,
2587 &pKeyContainer->hKeyExchangeKeyPair);
2601 case CALG_PCT1_MASTER:
2602 case CALG_SSL2_MASTER:
2603 case CALG_SSL3_MASTER:
2604 case CALG_TLS1_MASTER:
2605 *phKey = new_key(hProv, Algid, dwFlags, &pCryptKey);
2607 gen_rand_impl(pCryptKey->abKeyValue, RSAENH_MAX_KEY_SIZE);
2609 case CALG_SSL3_MASTER:
2610 pCryptKey->abKeyValue[0] = RSAENH_SSL3_VERSION_MAJOR;
2611 pCryptKey->abKeyValue[1] = RSAENH_SSL3_VERSION_MINOR;
2614 case CALG_TLS1_MASTER:
2615 pCryptKey->abKeyValue[0] = RSAENH_TLS1_VERSION_MAJOR;
2616 pCryptKey->abKeyValue[1] = RSAENH_TLS1_VERSION_MINOR;
2619 setup_key(pCryptKey);
2624 /* MSDN: Algorithm not supported specified by Algid */
2625 SetLastError(NTE_BAD_ALGID);
2629 return *phKey != (HCRYPTKEY)INVALID_HANDLE_VALUE;
2632 /******************************************************************************
2633 * CPGenRandom (RSAENH.@)
2635 * Generate a random byte stream.
2638 * hProv [I] Key container that is used to generate random bytes.
2639 * dwLen [I] Specifies the number of requested random data bytes.
2640 * pbBuffer [O] Random bytes will be stored here.
2646 BOOL WINAPI RSAENH_CPGenRandom(HCRYPTPROV hProv, DWORD dwLen, BYTE *pbBuffer)
2648 TRACE("(hProv=%08lx, dwLen=%d, pbBuffer=%p)\n", hProv, dwLen, pbBuffer);
2650 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
2652 /* MSDN: hProv not containing valid context handle */
2653 SetLastError(NTE_BAD_UID);
2657 return gen_rand_impl(pbBuffer, dwLen);
2660 /******************************************************************************
2661 * CPGetHashParam (RSAENH.@)
2663 * Query parameters of an hash object.
2666 * hProv [I] The kea container, which the hash belongs to.
2667 * hHash [I] The hash object that is to be queried.
2668 * dwParam [I] Specifies the parameter that is to be queried.
2669 * pbData [I] Pointer to the buffer where the parameter value will be stored.
2670 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
2671 * dwFlags [I] None currently defined.
2678 * Valid dwParams are: HP_ALGID, HP_HASHSIZE, HP_HASHVALUE. The hash will be
2679 * finalized if HP_HASHVALUE is queried.
2681 BOOL WINAPI RSAENH_CPGetHashParam(HCRYPTPROV hProv, HCRYPTHASH hHash, DWORD dwParam, BYTE *pbData,
2682 DWORD *pdwDataLen, DWORD dwFlags)
2684 CRYPTHASH *pCryptHash;
2686 TRACE("(hProv=%08lx, hHash=%08lx, dwParam=%08x, pbData=%p, pdwDataLen=%p, dwFlags=%08x)\n",
2687 hProv, hHash, dwParam, pbData, pdwDataLen, dwFlags);
2689 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
2691 SetLastError(NTE_BAD_UID);
2697 SetLastError(NTE_BAD_FLAGS);
2701 if (!lookup_handle(&handle_table, hHash, RSAENH_MAGIC_HASH,
2702 (OBJECTHDR**)&pCryptHash))
2704 SetLastError(NTE_BAD_HASH);
2710 SetLastError(ERROR_INVALID_PARAMETER);
2717 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptHash->aiAlgid,
2721 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptHash->dwHashSize,
2725 if (pCryptHash->aiAlgid == CALG_TLS1PRF) {
2726 return tls1_prf(hProv, pCryptHash->hKey, &pCryptHash->tpPRFParams.blobLabel,
2727 &pCryptHash->tpPRFParams.blobSeed, pbData, *pdwDataLen);
2730 if ( pbData == NULL ) {
2731 *pdwDataLen = pCryptHash->dwHashSize;
2735 if (pbData && (pCryptHash->dwState != RSAENH_HASHSTATE_FINISHED))
2737 finalize_hash(pCryptHash);
2738 pCryptHash->dwState = RSAENH_HASHSTATE_FINISHED;
2741 return copy_param(pbData, pdwDataLen, (CONST BYTE*)pCryptHash->abHashValue,
2742 pCryptHash->dwHashSize);
2745 SetLastError(NTE_BAD_TYPE);
2750 /******************************************************************************
2751 * CPSetKeyParam (RSAENH.@)
2753 * Set a parameter of a key object
2756 * hProv [I] The key container to which the key belongs.
2757 * hKey [I] The key for which a parameter is to be set.
2758 * dwParam [I] Parameter type. See Notes.
2759 * pbData [I] Pointer to the parameter value.
2760 * dwFlags [I] Currently none defined.
2767 * Defined dwParam types are:
2768 * - KP_MODE: Values MODE_CBC, MODE_ECB, MODE_CFB.
2769 * - KP_MODE_BITS: Shift width for cipher feedback mode. (Currently ignored by MS CSP's)
2770 * - KP_PERMISSIONS: Or'ed combination of CRYPT_ENCRYPT, CRYPT_DECRYPT,
2771 * CRYPT_EXPORT, CRYPT_READ, CRYPT_WRITE, CRYPT_MAC
2772 * - KP_IV: Initialization vector
2774 BOOL WINAPI RSAENH_CPSetKeyParam(HCRYPTPROV hProv, HCRYPTKEY hKey, DWORD dwParam, BYTE *pbData,
2777 CRYPTKEY *pCryptKey;
2779 TRACE("(hProv=%08lx, hKey=%08lx, dwParam=%08x, pbData=%p, dwFlags=%08x)\n", hProv, hKey,
2780 dwParam, pbData, dwFlags);
2782 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
2784 SetLastError(NTE_BAD_UID);
2789 SetLastError(NTE_BAD_FLAGS);
2793 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
2795 SetLastError(NTE_BAD_KEY);
2801 /* The MS providers only support PKCS5_PADDING */
2802 if (*(DWORD *)pbData != PKCS5_PADDING) {
2803 SetLastError(NTE_BAD_DATA);
2809 pCryptKey->dwMode = *(DWORD*)pbData;
2813 pCryptKey->dwModeBits = *(DWORD*)pbData;
2816 case KP_PERMISSIONS:
2817 pCryptKey->dwPermissions = *(DWORD*)pbData;
2821 memcpy(pCryptKey->abInitVector, pbData, pCryptKey->dwBlockLen);
2822 setup_key(pCryptKey);
2827 CRYPT_INTEGER_BLOB *blob = (CRYPT_INTEGER_BLOB *)pbData;
2829 /* salt length can't be greater than 184 bits = 24 bytes */
2830 if (blob->cbData > 24)
2832 SetLastError(NTE_BAD_DATA);
2835 memcpy(pCryptKey->abKeyValue + pCryptKey->dwKeyLen, blob->pbData,
2837 pCryptKey->dwSaltLen = blob->cbData;
2838 setup_key(pCryptKey);
2842 case KP_EFFECTIVE_KEYLEN:
2843 switch (pCryptKey->aiAlgid) {
2847 SetLastError(ERROR_INVALID_PARAMETER);
2850 else if (!*(DWORD *)pbData || *(DWORD *)pbData > 1024)
2852 SetLastError(NTE_BAD_DATA);
2857 pCryptKey->dwEffectiveKeyLen = *(DWORD *)pbData;
2858 setup_key(pCryptKey);
2862 SetLastError(NTE_BAD_TYPE);
2867 case KP_SCHANNEL_ALG:
2868 switch (((PSCHANNEL_ALG)pbData)->dwUse) {
2869 case SCHANNEL_ENC_KEY:
2870 memcpy(&pCryptKey->siSChannelInfo.saEncAlg, pbData, sizeof(SCHANNEL_ALG));
2873 case SCHANNEL_MAC_KEY:
2874 memcpy(&pCryptKey->siSChannelInfo.saMACAlg, pbData, sizeof(SCHANNEL_ALG));
2878 SetLastError(NTE_FAIL); /* FIXME: error code */
2883 case KP_CLIENT_RANDOM:
2884 return copy_data_blob(&pCryptKey->siSChannelInfo.blobClientRandom, (PCRYPT_DATA_BLOB)pbData);
2886 case KP_SERVER_RANDOM:
2887 return copy_data_blob(&pCryptKey->siSChannelInfo.blobServerRandom, (PCRYPT_DATA_BLOB)pbData);
2890 SetLastError(NTE_BAD_TYPE);
2895 /******************************************************************************
2896 * CPGetKeyParam (RSAENH.@)
2898 * Query a key parameter.
2901 * hProv [I] The key container, which the key belongs to.
2902 * hHash [I] The key object that is to be queried.
2903 * dwParam [I] Specifies the parameter that is to be queried.
2904 * pbData [I] Pointer to the buffer where the parameter value will be stored.
2905 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
2906 * dwFlags [I] None currently defined.
2913 * Defined dwParam types are:
2914 * - KP_MODE: Values MODE_CBC, MODE_ECB, MODE_CFB.
2915 * - KP_MODE_BITS: Shift width for cipher feedback mode.
2916 * (Currently ignored by MS CSP's - always eight)
2917 * - KP_PERMISSIONS: Or'ed combination of CRYPT_ENCRYPT, CRYPT_DECRYPT,
2918 * CRYPT_EXPORT, CRYPT_READ, CRYPT_WRITE, CRYPT_MAC
2919 * - KP_IV: Initialization vector.
2920 * - KP_KEYLEN: Bitwidth of the key.
2921 * - KP_BLOCKLEN: Size of a block cipher block.
2922 * - KP_SALT: Salt value.
2924 BOOL WINAPI RSAENH_CPGetKeyParam(HCRYPTPROV hProv, HCRYPTKEY hKey, DWORD dwParam, BYTE *pbData,
2925 DWORD *pdwDataLen, DWORD dwFlags)
2927 CRYPTKEY *pCryptKey;
2930 TRACE("(hProv=%08lx, hKey=%08lx, dwParam=%08x, pbData=%p, pdwDataLen=%p dwFlags=%08x)\n",
2931 hProv, hKey, dwParam, pbData, pdwDataLen, dwFlags);
2933 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
2935 SetLastError(NTE_BAD_UID);
2940 SetLastError(NTE_BAD_FLAGS);
2944 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
2946 SetLastError(NTE_BAD_KEY);
2953 return copy_param(pbData, pdwDataLen, (CONST BYTE*)pCryptKey->abInitVector,
2954 pCryptKey->dwBlockLen);
2957 return copy_param(pbData, pdwDataLen,
2958 (CONST BYTE*)&pCryptKey->abKeyValue[pCryptKey->dwKeyLen], pCryptKey->dwSaltLen);
2961 dwValue = PKCS5_PADDING;
2962 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwValue, sizeof(DWORD));
2965 dwValue = pCryptKey->dwKeyLen << 3;
2966 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwValue, sizeof(DWORD));
2968 case KP_EFFECTIVE_KEYLEN:
2969 if (pCryptKey->dwEffectiveKeyLen)
2970 dwValue = pCryptKey->dwEffectiveKeyLen;
2972 dwValue = pCryptKey->dwKeyLen << 3;
2973 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwValue, sizeof(DWORD));
2976 dwValue = pCryptKey->dwBlockLen << 3;
2977 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwValue, sizeof(DWORD));
2980 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptKey->dwMode, sizeof(DWORD));
2983 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptKey->dwModeBits,
2986 case KP_PERMISSIONS:
2987 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptKey->dwPermissions,
2991 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptKey->aiAlgid, sizeof(DWORD));
2994 SetLastError(NTE_BAD_TYPE);
2999 /******************************************************************************
3000 * CPGetProvParam (RSAENH.@)
3002 * Query a CSP parameter.
3005 * hProv [I] The key container that is to be queried.
3006 * dwParam [I] Specifies the parameter that is to be queried.
3007 * pbData [I] Pointer to the buffer where the parameter value will be stored.
3008 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
3009 * dwFlags [I] CRYPT_FIRST: Start enumeration (for PP_ENUMALGS{_EX}).
3015 * Defined dwParam types:
3016 * - PP_CONTAINER: Name of the key container.
3017 * - PP_NAME: Name of the cryptographic service provider.
3018 * - PP_SIG_KEYSIZE_INC: RSA signature keywidth granularity in bits.
3019 * - PP_KEYX_KEYSIZE_INC: RSA key-exchange keywidth granularity in bits.
3020 * - PP_ENUMALGS{_EX}: Query provider capabilities.
3022 BOOL WINAPI RSAENH_CPGetProvParam(HCRYPTPROV hProv, DWORD dwParam, BYTE *pbData,
3023 DWORD *pdwDataLen, DWORD dwFlags)
3025 KEYCONTAINER *pKeyContainer;
3026 PROV_ENUMALGS provEnumalgs;
3030 /* This is for dwParam PP_CRYPT_COUNT_KEY_USE.
3031 * IE6 SP1 asks for it in the 'About' dialog.
3032 * Returning this BLOB seems to satisfy IE. The marked 0x00 seem
3033 * to be 'don't care's. If you know anything more specific about
3034 * this provider parameter, please report to wine-devel@winehq.org */
3035 static CONST BYTE abWTF[96] = {
3036 0xb0, 0x25, 0x63, 0x86, 0x9c, 0xab, 0xb6, 0x37,
3037 0xe8, 0x82, /**/0x00,/**/ 0x72, 0x06, 0xb2, /**/0x00,/**/ 0x3b,
3038 0x60, 0x35, /**/0x00,/**/ 0x3b, 0x88, 0xce, /**/0x00,/**/ 0x82,
3039 0xbc, 0x7a, /**/0x00,/**/ 0xb7, 0x4f, 0x7e, /**/0x00,/**/ 0xde,
3040 0x92, 0xf1, /**/0x00,/**/ 0x83, 0xea, 0x5e, /**/0x00,/**/ 0xc8,
3041 0x12, 0x1e, 0xd4, 0x06, 0xf7, 0x66, /**/0x00,/**/ 0x01,
3042 0x29, 0xa4, /**/0x00,/**/ 0xf8, 0x24, 0x0c, /**/0x00,/**/ 0x33,
3043 0x06, 0x80, /**/0x00,/**/ 0x02, 0x46, 0x0b, /**/0x00,/**/ 0x6d,
3044 0x5b, 0xca, /**/0x00,/**/ 0x9a, 0x10, 0xf0, /**/0x00,/**/ 0x05,
3045 0x19, 0xd0, /**/0x00,/**/ 0x2c, 0xf6, 0x27, /**/0x00,/**/ 0xaa,
3046 0x7c, 0x6f, /**/0x00,/**/ 0xb9, 0xd8, 0x72, /**/0x00,/**/ 0x03,
3047 0xf3, 0x81, /**/0x00,/**/ 0xfa, 0xe8, 0x26, /**/0x00,/**/ 0xca
3050 TRACE("(hProv=%08lx, dwParam=%08x, pbData=%p, pdwDataLen=%p, dwFlags=%08x)\n",
3051 hProv, dwParam, pbData, pdwDataLen, dwFlags);
3054 SetLastError(ERROR_INVALID_PARAMETER);
3058 if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER,
3059 (OBJECTHDR**)&pKeyContainer))
3061 /* MSDN: hProv not containing valid context handle */
3062 SetLastError(NTE_BAD_UID);
3069 case PP_UNIQUE_CONTAINER:/* MSDN says we can return the same value as PP_CONTAINER */
3070 return copy_param(pbData, pdwDataLen, (CONST BYTE*)pKeyContainer->szName,
3071 strlen(pKeyContainer->szName)+1);
3074 return copy_param(pbData, pdwDataLen, (CONST BYTE*)pKeyContainer->szProvName,
3075 strlen(pKeyContainer->szProvName)+1);
3078 dwTemp = PROV_RSA_FULL;
3079 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwTemp, sizeof(dwTemp));
3082 dwTemp = AT_SIGNATURE | AT_KEYEXCHANGE;
3083 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwTemp, sizeof(dwTemp));
3085 case PP_KEYSET_TYPE:
3086 dwTemp = pKeyContainer->dwFlags & CRYPT_MACHINE_KEYSET;
3087 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwTemp, sizeof(dwTemp));
3090 dwTemp = CRYPT_SEC_DESCR;
3091 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwTemp, sizeof(dwTemp));
3093 case PP_SIG_KEYSIZE_INC:
3094 case PP_KEYX_KEYSIZE_INC:
3096 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwTemp, sizeof(dwTemp));
3099 dwTemp = CRYPT_IMPL_SOFTWARE;
3100 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwTemp, sizeof(dwTemp));
3103 dwTemp = 0x00000200;
3104 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwTemp, sizeof(dwTemp));
3106 case PP_ENUMCONTAINERS:
3107 if ((dwFlags & CRYPT_FIRST) == CRYPT_FIRST) pKeyContainer->dwEnumContainersCtr = 0;
3110 *pdwDataLen = (DWORD)MAX_PATH + 1;
3114 if (!open_container_key("", dwFlags, &hKey))
3116 SetLastError(ERROR_NO_MORE_ITEMS);
3120 dwTemp = *pdwDataLen;
3121 switch (RegEnumKeyExA(hKey, pKeyContainer->dwEnumContainersCtr, (LPSTR)pbData, &dwTemp,
3122 NULL, NULL, NULL, NULL))
3124 case ERROR_MORE_DATA:
3125 *pdwDataLen = (DWORD)MAX_PATH + 1;
3128 pKeyContainer->dwEnumContainersCtr++;
3132 case ERROR_NO_MORE_ITEMS:
3134 SetLastError(ERROR_NO_MORE_ITEMS);
3140 case PP_ENUMALGS_EX:
3141 if (((pKeyContainer->dwEnumAlgsCtr >= RSAENH_MAX_ENUMALGS-1) ||
3142 (!aProvEnumAlgsEx[pKeyContainer->dwPersonality]
3143 [pKeyContainer->dwEnumAlgsCtr+1].aiAlgid)) &&
3144 ((dwFlags & CRYPT_FIRST) != CRYPT_FIRST))
3146 SetLastError(ERROR_NO_MORE_ITEMS);
3150 if (dwParam == PP_ENUMALGS) {
3151 if (pbData && (*pdwDataLen >= sizeof(PROV_ENUMALGS)))
3152 pKeyContainer->dwEnumAlgsCtr = ((dwFlags & CRYPT_FIRST) == CRYPT_FIRST) ?
3153 0 : pKeyContainer->dwEnumAlgsCtr+1;
3155 provEnumalgs.aiAlgid = aProvEnumAlgsEx
3156 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr].aiAlgid;
3157 provEnumalgs.dwBitLen = aProvEnumAlgsEx
3158 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr].dwDefaultLen;
3159 provEnumalgs.dwNameLen = aProvEnumAlgsEx
3160 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr].dwNameLen;
3161 memcpy(provEnumalgs.szName, aProvEnumAlgsEx
3162 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr].szName,
3165 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&provEnumalgs,
3166 sizeof(PROV_ENUMALGS));
3168 if (pbData && (*pdwDataLen >= sizeof(PROV_ENUMALGS_EX)))
3169 pKeyContainer->dwEnumAlgsCtr = ((dwFlags & CRYPT_FIRST) == CRYPT_FIRST) ?
3170 0 : pKeyContainer->dwEnumAlgsCtr+1;
3172 return copy_param(pbData, pdwDataLen,
3173 (CONST BYTE*)&aProvEnumAlgsEx
3174 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr],
3175 sizeof(PROV_ENUMALGS_EX));
3178 case PP_CRYPT_COUNT_KEY_USE: /* Asked for by IE About dialog */
3179 return copy_param(pbData, pdwDataLen, abWTF, sizeof(abWTF));
3182 /* MSDN: Unknown parameter number in dwParam */
3183 SetLastError(NTE_BAD_TYPE);
3188 /******************************************************************************
3189 * CPDeriveKey (RSAENH.@)
3191 * Derives a key from a hash value.
3194 * hProv [I] Key container for which a key is to be generated.
3195 * Algid [I] Crypto algorithm identifier for the key to be generated.
3196 * hBaseData [I] Hash from whose value the key will be derived.
3197 * dwFlags [I] See Notes.
3198 * phKey [O] The generated key.
3206 * - CRYPT_EXPORTABLE: Key can be exported.
3207 * - CRYPT_NO_SALT: No salt is used for 40 bit keys.
3208 * - CRYPT_CREATE_SALT: Use remaining bits as salt value.
3210 BOOL WINAPI RSAENH_CPDeriveKey(HCRYPTPROV hProv, ALG_ID Algid, HCRYPTHASH hBaseData,
3211 DWORD dwFlags, HCRYPTKEY *phKey)
3213 CRYPTKEY *pCryptKey, *pMasterKey;
3214 CRYPTHASH *pCryptHash;
3215 BYTE abHashValue[RSAENH_MAX_HASH_SIZE*2];
3218 TRACE("(hProv=%08lx, Algid=%d, hBaseData=%08lx, dwFlags=%08x phKey=%p)\n", hProv, Algid,
3219 hBaseData, dwFlags, phKey);
3221 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
3223 SetLastError(NTE_BAD_UID);
3227 if (!lookup_handle(&handle_table, hBaseData, RSAENH_MAGIC_HASH,
3228 (OBJECTHDR**)&pCryptHash))
3230 SetLastError(NTE_BAD_HASH);
3236 SetLastError(ERROR_INVALID_PARAMETER);
3240 switch (GET_ALG_CLASS(Algid))
3242 case ALG_CLASS_DATA_ENCRYPT:
3243 *phKey = new_key(hProv, Algid, dwFlags, &pCryptKey);
3244 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) return FALSE;
3247 * We derive the key material from the hash.
3248 * If the hash value is not large enough for the claimed key, we have to construct
3249 * a larger binary value based on the hash. This is documented in MSDN: CryptDeriveKey.
3251 dwLen = RSAENH_MAX_HASH_SIZE;
3252 RSAENH_CPGetHashParam(pCryptHash->hProv, hBaseData, HP_HASHVAL, abHashValue, &dwLen, 0);
3254 if (dwLen < pCryptKey->dwKeyLen) {
3255 BYTE pad1[RSAENH_HMAC_DEF_PAD_LEN], pad2[RSAENH_HMAC_DEF_PAD_LEN];
3256 BYTE old_hashval[RSAENH_MAX_HASH_SIZE];
3259 memcpy(old_hashval, pCryptHash->abHashValue, RSAENH_MAX_HASH_SIZE);
3261 for (i=0; i<RSAENH_HMAC_DEF_PAD_LEN; i++) {
3262 pad1[i] = RSAENH_HMAC_DEF_IPAD_CHAR ^ (i<dwLen ? abHashValue[i] : 0);
3263 pad2[i] = RSAENH_HMAC_DEF_OPAD_CHAR ^ (i<dwLen ? abHashValue[i] : 0);
3266 init_hash(pCryptHash);
3267 update_hash(pCryptHash, pad1, RSAENH_HMAC_DEF_PAD_LEN);
3268 finalize_hash(pCryptHash);
3269 memcpy(abHashValue, pCryptHash->abHashValue, pCryptHash->dwHashSize);
3271 init_hash(pCryptHash);
3272 update_hash(pCryptHash, pad2, RSAENH_HMAC_DEF_PAD_LEN);
3273 finalize_hash(pCryptHash);
3274 memcpy(abHashValue+pCryptHash->dwHashSize, pCryptHash->abHashValue,
3275 pCryptHash->dwHashSize);
3277 memcpy(pCryptHash->abHashValue, old_hashval, RSAENH_MAX_HASH_SIZE);
3280 memcpy(pCryptKey->abKeyValue, abHashValue,
3281 RSAENH_MIN(pCryptKey->dwKeyLen, sizeof(pCryptKey->abKeyValue)));
3284 case ALG_CLASS_MSG_ENCRYPT:
3285 if (!lookup_handle(&handle_table, pCryptHash->hKey, RSAENH_MAGIC_KEY,
3286 (OBJECTHDR**)&pMasterKey))
3288 SetLastError(NTE_FAIL); /* FIXME error code */
3294 /* See RFC 2246, chapter 6.3 Key calculation */
3295 case CALG_SCHANNEL_ENC_KEY:
3296 *phKey = new_key(hProv, pMasterKey->siSChannelInfo.saEncAlg.Algid,
3297 MAKELONG(LOWORD(dwFlags),pMasterKey->siSChannelInfo.saEncAlg.cBits),
3299 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) return FALSE;
3300 memcpy(pCryptKey->abKeyValue,
3301 pCryptHash->abHashValue + (
3302 2 * (pMasterKey->siSChannelInfo.saMACAlg.cBits / 8) +
3303 ((dwFlags & CRYPT_SERVER) ?
3304 (pMasterKey->siSChannelInfo.saEncAlg.cBits / 8) : 0)),
3305 pMasterKey->siSChannelInfo.saEncAlg.cBits / 8);
3306 memcpy(pCryptKey->abInitVector,
3307 pCryptHash->abHashValue + (
3308 2 * (pMasterKey->siSChannelInfo.saMACAlg.cBits / 8) +
3309 2 * (pMasterKey->siSChannelInfo.saEncAlg.cBits / 8) +
3310 ((dwFlags & CRYPT_SERVER) ? pCryptKey->dwBlockLen : 0)),
3311 pCryptKey->dwBlockLen);
3314 case CALG_SCHANNEL_MAC_KEY:
3315 *phKey = new_key(hProv, Algid,
3316 MAKELONG(LOWORD(dwFlags),pMasterKey->siSChannelInfo.saMACAlg.cBits),
3318 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) return FALSE;
3319 memcpy(pCryptKey->abKeyValue,
3320 pCryptHash->abHashValue + ((dwFlags & CRYPT_SERVER) ?
3321 pMasterKey->siSChannelInfo.saMACAlg.cBits / 8 : 0),
3322 pMasterKey->siSChannelInfo.saMACAlg.cBits / 8);
3326 SetLastError(NTE_BAD_ALGID);
3332 SetLastError(NTE_BAD_ALGID);
3336 setup_key(pCryptKey);
3340 /******************************************************************************
3341 * CPGetUserKey (RSAENH.@)
3343 * Returns a handle to the user's private key-exchange- or signature-key.
3346 * hProv [I] The key container from which a user key is requested.
3347 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
3348 * phUserKey [O] Handle to the requested key or INVALID_HANDLE_VALUE in case of failure.
3355 * A newly created key container does not contain private user key. Create them with CPGenKey.
3357 BOOL WINAPI RSAENH_CPGetUserKey(HCRYPTPROV hProv, DWORD dwKeySpec, HCRYPTKEY *phUserKey)
3359 KEYCONTAINER *pKeyContainer;
3361 TRACE("(hProv=%08lx, dwKeySpec=%08x, phUserKey=%p)\n", hProv, dwKeySpec, phUserKey);
3363 if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER,
3364 (OBJECTHDR**)&pKeyContainer))
3366 /* MSDN: hProv not containing valid context handle */
3367 SetLastError(NTE_BAD_UID);
3373 case AT_KEYEXCHANGE:
3374 copy_handle(&handle_table, pKeyContainer->hKeyExchangeKeyPair, RSAENH_MAGIC_KEY,
3379 copy_handle(&handle_table, pKeyContainer->hSignatureKeyPair, RSAENH_MAGIC_KEY,
3384 *phUserKey = (HCRYPTKEY)INVALID_HANDLE_VALUE;
3387 if (*phUserKey == (HCRYPTKEY)INVALID_HANDLE_VALUE)
3389 /* MSDN: dwKeySpec parameter specifies nonexistent key */
3390 SetLastError(NTE_NO_KEY);
3397 /******************************************************************************
3398 * CPHashData (RSAENH.@)
3400 * Updates a hash object with the given data.
3403 * hProv [I] Key container to which the hash object belongs.
3404 * hHash [I] Hash object which is to be updated.
3405 * pbData [I] Pointer to data with which the hash object is to be updated.
3406 * dwDataLen [I] Length of the data.
3407 * dwFlags [I] Currently none defined.
3414 * The actual hash value is queried with CPGetHashParam, which will finalize
3415 * the hash. Updating a finalized hash will fail with a last error NTE_BAD_HASH_STATE.
3417 BOOL WINAPI RSAENH_CPHashData(HCRYPTPROV hProv, HCRYPTHASH hHash, CONST BYTE *pbData,
3418 DWORD dwDataLen, DWORD dwFlags)
3420 CRYPTHASH *pCryptHash;
3422 TRACE("(hProv=%08lx, hHash=%08lx, pbData=%p, dwDataLen=%d, dwFlags=%08x)\n",
3423 hProv, hHash, pbData, dwDataLen, dwFlags);
3427 SetLastError(NTE_BAD_FLAGS);
3431 if (!lookup_handle(&handle_table, hHash, RSAENH_MAGIC_HASH,
3432 (OBJECTHDR**)&pCryptHash))
3434 SetLastError(NTE_BAD_HASH);
3438 if (!get_algid_info(hProv, pCryptHash->aiAlgid) || pCryptHash->aiAlgid == CALG_SSL3_SHAMD5)
3440 SetLastError(NTE_BAD_ALGID);
3444 if (pCryptHash->dwState != RSAENH_HASHSTATE_HASHING)
3446 SetLastError(NTE_BAD_HASH_STATE);
3450 update_hash(pCryptHash, pbData, dwDataLen);
3454 /******************************************************************************
3455 * CPHashSessionKey (RSAENH.@)
3457 * Updates a hash object with the binary representation of a symmetric key.
3460 * hProv [I] Key container to which the hash object belongs.
3461 * hHash [I] Hash object which is to be updated.
3462 * hKey [I] The symmetric key, whose binary value will be added to the hash.
3463 * dwFlags [I] CRYPT_LITTLE_ENDIAN, if the binary key value shall be interpreted as little endian.
3469 BOOL WINAPI RSAENH_CPHashSessionKey(HCRYPTPROV hProv, HCRYPTHASH hHash, HCRYPTKEY hKey,
3472 BYTE abKeyValue[RSAENH_MAX_KEY_SIZE], bTemp;
3476 TRACE("(hProv=%08lx, hHash=%08lx, hKey=%08lx, dwFlags=%08x)\n", hProv, hHash, hKey, dwFlags);
3478 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pKey) ||
3479 (GET_ALG_CLASS(pKey->aiAlgid) != ALG_CLASS_DATA_ENCRYPT))
3481 SetLastError(NTE_BAD_KEY);
3485 if (dwFlags & ~CRYPT_LITTLE_ENDIAN) {
3486 SetLastError(NTE_BAD_FLAGS);
3490 memcpy(abKeyValue, pKey->abKeyValue, pKey->dwKeyLen);
3491 if (!(dwFlags & CRYPT_LITTLE_ENDIAN)) {
3492 for (i=0; i<pKey->dwKeyLen/2; i++) {
3493 bTemp = abKeyValue[i];
3494 abKeyValue[i] = abKeyValue[pKey->dwKeyLen-i-1];
3495 abKeyValue[pKey->dwKeyLen-i-1] = bTemp;
3499 return RSAENH_CPHashData(hProv, hHash, abKeyValue, pKey->dwKeyLen, 0);
3502 /******************************************************************************
3503 * CPReleaseContext (RSAENH.@)
3505 * Release a key container.
3508 * hProv [I] Key container to be released.
3509 * dwFlags [I] Currently none defined.
3515 BOOL WINAPI RSAENH_CPReleaseContext(HCRYPTPROV hProv, DWORD dwFlags)
3517 TRACE("(hProv=%08lx, dwFlags=%08x)\n", hProv, dwFlags);
3519 if (!release_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
3521 /* MSDN: hProv not containing valid context handle */
3522 SetLastError(NTE_BAD_UID);
3527 SetLastError(NTE_BAD_FLAGS);
3534 /******************************************************************************
3535 * CPSetHashParam (RSAENH.@)
3537 * Set a parameter of a hash object
3540 * hProv [I] The key container to which the key belongs.
3541 * hHash [I] The hash object for which a parameter is to be set.
3542 * dwParam [I] Parameter type. See Notes.
3543 * pbData [I] Pointer to the parameter value.
3544 * dwFlags [I] Currently none defined.
3551 * Currently only the HP_HMAC_INFO dwParam type is defined.
3552 * The HMAC_INFO struct will be deep copied into the hash object.
3553 * See Internet RFC 2104 for details on the HMAC algorithm.
3555 BOOL WINAPI RSAENH_CPSetHashParam(HCRYPTPROV hProv, HCRYPTHASH hHash, DWORD dwParam,
3556 BYTE *pbData, DWORD dwFlags)
3558 CRYPTHASH *pCryptHash;
3559 CRYPTKEY *pCryptKey;
3562 TRACE("(hProv=%08lx, hHash=%08lx, dwParam=%08x, pbData=%p, dwFlags=%08x)\n",
3563 hProv, hHash, dwParam, pbData, dwFlags);
3565 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
3567 SetLastError(NTE_BAD_UID);
3572 SetLastError(NTE_BAD_FLAGS);
3576 if (!lookup_handle(&handle_table, hHash, RSAENH_MAGIC_HASH,
3577 (OBJECTHDR**)&pCryptHash))
3579 SetLastError(NTE_BAD_HASH);
3585 free_hmac_info(pCryptHash->pHMACInfo);
3586 if (!copy_hmac_info(&pCryptHash->pHMACInfo, (PHMAC_INFO)pbData)) return FALSE;
3588 if (!lookup_handle(&handle_table, pCryptHash->hKey, RSAENH_MAGIC_KEY,
3589 (OBJECTHDR**)&pCryptKey))
3591 SetLastError(NTE_FAIL); /* FIXME: correct error code? */
3595 for (i=0; i<RSAENH_MIN(pCryptKey->dwKeyLen,pCryptHash->pHMACInfo->cbInnerString); i++) {
3596 pCryptHash->pHMACInfo->pbInnerString[i] ^= pCryptKey->abKeyValue[i];
3598 for (i=0; i<RSAENH_MIN(pCryptKey->dwKeyLen,pCryptHash->pHMACInfo->cbOuterString); i++) {
3599 pCryptHash->pHMACInfo->pbOuterString[i] ^= pCryptKey->abKeyValue[i];
3602 init_hash(pCryptHash);
3606 memcpy(pCryptHash->abHashValue, pbData, pCryptHash->dwHashSize);
3607 pCryptHash->dwState = RSAENH_HASHSTATE_FINISHED;
3610 case HP_TLS1PRF_SEED:
3611 return copy_data_blob(&pCryptHash->tpPRFParams.blobSeed, (PCRYPT_DATA_BLOB)pbData);
3613 case HP_TLS1PRF_LABEL:
3614 return copy_data_blob(&pCryptHash->tpPRFParams.blobLabel, (PCRYPT_DATA_BLOB)pbData);
3617 SetLastError(NTE_BAD_TYPE);
3622 /******************************************************************************
3623 * CPSetProvParam (RSAENH.@)
3625 BOOL WINAPI RSAENH_CPSetProvParam(HCRYPTPROV hProv, DWORD dwParam, BYTE *pbData, DWORD dwFlags)
3631 /******************************************************************************
3632 * CPSignHash (RSAENH.@)
3634 * Sign a hash object
3637 * hProv [I] The key container, to which the hash object belongs.
3638 * hHash [I] The hash object to be signed.
3639 * dwKeySpec [I] AT_SIGNATURE or AT_KEYEXCHANGE: Key used to generate the signature.
3640 * sDescription [I] Should be NULL for security reasons.
3641 * dwFlags [I] 0, CRYPT_NOHASHOID or CRYPT_X931_FORMAT: Format of the signature.
3642 * pbSignature [O] Buffer, to which the signature will be stored. May be NULL to query SigLen.
3643 * pdwSigLen [I/O] Size of the buffer (in), Length of the signature (out)
3649 BOOL WINAPI RSAENH_CPSignHash(HCRYPTPROV hProv, HCRYPTHASH hHash, DWORD dwKeySpec,
3650 LPCWSTR sDescription, DWORD dwFlags, BYTE *pbSignature,
3653 HCRYPTKEY hCryptKey;
3654 CRYPTKEY *pCryptKey;
3656 BYTE abHashValue[RSAENH_MAX_HASH_SIZE];
3659 TRACE("(hProv=%08lx, hHash=%08lx, dwKeySpec=%08x, sDescription=%s, dwFlags=%08x, "
3660 "pbSignature=%p, pdwSigLen=%p)\n", hProv, hHash, dwKeySpec, debugstr_w(sDescription),
3661 dwFlags, pbSignature, pdwSigLen);
3663 if (dwFlags & ~(CRYPT_NOHASHOID|CRYPT_X931_FORMAT)) {
3664 SetLastError(NTE_BAD_FLAGS);
3668 if (!RSAENH_CPGetUserKey(hProv, dwKeySpec, &hCryptKey)) return FALSE;
3670 if (!lookup_handle(&handle_table, hCryptKey, RSAENH_MAGIC_KEY,
3671 (OBJECTHDR**)&pCryptKey))
3673 SetLastError(NTE_NO_KEY);
3678 *pdwSigLen = pCryptKey->dwKeyLen;
3681 if (pCryptKey->dwKeyLen > *pdwSigLen)
3683 SetLastError(ERROR_MORE_DATA);
3684 *pdwSigLen = pCryptKey->dwKeyLen;
3687 *pdwSigLen = pCryptKey->dwKeyLen;
3690 if (!RSAENH_CPHashData(hProv, hHash, (CONST BYTE*)sDescription,
3691 (DWORD)lstrlenW(sDescription)*sizeof(WCHAR), 0))
3697 dwHashLen = sizeof(DWORD);
3698 if (!RSAENH_CPGetHashParam(hProv, hHash, HP_ALGID, (BYTE*)&aiAlgid, &dwHashLen, 0)) return FALSE;
3700 dwHashLen = RSAENH_MAX_HASH_SIZE;
3701 if (!RSAENH_CPGetHashParam(hProv, hHash, HP_HASHVAL, abHashValue, &dwHashLen, 0)) return FALSE;
3704 if (!build_hash_signature(pbSignature, *pdwSigLen, aiAlgid, abHashValue, dwHashLen, dwFlags)) {
3708 return encrypt_block_impl(pCryptKey->aiAlgid, PK_PRIVATE, &pCryptKey->context, pbSignature, pbSignature, RSAENH_ENCRYPT);
3711 /******************************************************************************
3712 * CPVerifySignature (RSAENH.@)
3714 * Verify the signature of a hash object.
3717 * hProv [I] The key container, to which the hash belongs.
3718 * hHash [I] The hash for which the signature is verified.
3719 * pbSignature [I] The binary signature.
3720 * dwSigLen [I] Length of the signature BLOB.
3721 * hPubKey [I] Public key used to verify the signature.
3722 * sDescription [I] Should be NULL for security reasons.
3723 * dwFlags [I] 0, CRYPT_NOHASHOID or CRYPT_X931_FORMAT: Format of the signature.
3726 * Success: TRUE (Signature is valid)
3727 * Failure: FALSE (GetLastError() == NTE_BAD_SIGNATURE, if signature is invalid)
3729 BOOL WINAPI RSAENH_CPVerifySignature(HCRYPTPROV hProv, HCRYPTHASH hHash, CONST BYTE *pbSignature,
3730 DWORD dwSigLen, HCRYPTKEY hPubKey, LPCWSTR sDescription,
3733 BYTE *pbConstructed = NULL, *pbDecrypted = NULL;
3734 CRYPTKEY *pCryptKey;
3737 BYTE abHashValue[RSAENH_MAX_HASH_SIZE];
3740 TRACE("(hProv=%08lx, hHash=%08lx, pbSignature=%p, dwSigLen=%d, hPubKey=%08lx, sDescription=%s, "
3741 "dwFlags=%08x)\n", hProv, hHash, pbSignature, dwSigLen, hPubKey, debugstr_w(sDescription),
3744 if (dwFlags & ~(CRYPT_NOHASHOID|CRYPT_X931_FORMAT)) {
3745 SetLastError(NTE_BAD_FLAGS);
3749 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
3751 SetLastError(NTE_BAD_UID);
3755 if (!lookup_handle(&handle_table, hPubKey, RSAENH_MAGIC_KEY,
3756 (OBJECTHDR**)&pCryptKey))
3758 SetLastError(NTE_BAD_KEY);
3762 /* in Microsoft implementation, the signature length is checked before
3763 * the signature pointer.
3765 if (dwSigLen != pCryptKey->dwKeyLen)
3767 SetLastError(NTE_BAD_SIGNATURE);
3771 if (!hHash || !pbSignature)
3773 SetLastError(ERROR_INVALID_PARAMETER);
3778 if (!RSAENH_CPHashData(hProv, hHash, (CONST BYTE*)sDescription,
3779 (DWORD)lstrlenW(sDescription)*sizeof(WCHAR), 0))
3785 dwHashLen = sizeof(DWORD);
3786 if (!RSAENH_CPGetHashParam(hProv, hHash, HP_ALGID, (BYTE*)&aiAlgid, &dwHashLen, 0)) return FALSE;
3788 dwHashLen = RSAENH_MAX_HASH_SIZE;
3789 if (!RSAENH_CPGetHashParam(hProv, hHash, HP_HASHVAL, abHashValue, &dwHashLen, 0)) return FALSE;
3791 pbConstructed = HeapAlloc(GetProcessHeap(), 0, dwSigLen);
3792 if (!pbConstructed) {
3793 SetLastError(NTE_NO_MEMORY);
3797 pbDecrypted = HeapAlloc(GetProcessHeap(), 0, dwSigLen);
3799 SetLastError(NTE_NO_MEMORY);
3803 if (!encrypt_block_impl(pCryptKey->aiAlgid, PK_PUBLIC, &pCryptKey->context, pbSignature, pbDecrypted,
3809 if (!build_hash_signature(pbConstructed, dwSigLen, aiAlgid, abHashValue, dwHashLen, dwFlags)) {
3813 if (memcmp(pbDecrypted, pbConstructed, dwSigLen)) {
3814 SetLastError(NTE_BAD_SIGNATURE);
3820 HeapFree(GetProcessHeap(), 0, pbConstructed);
3821 HeapFree(GetProcessHeap(), 0, pbDecrypted);
3825 static const WCHAR szProviderKeys[6][116] = {
3826 { 'S','o','f','t','w','a','r','e','\\',
3827 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
3828 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
3829 'i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ','B','a','s',
3830 'e',' ','C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r',
3831 'o','v','i','d','e','r',' ','v','1','.','0',0 },
3832 { 'S','o','f','t','w','a','r','e','\\',
3833 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
3834 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
3835 'i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ',
3836 'E','n','h','a','n','c','e','d',
3837 ' ','C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r',
3838 'o','v','i','d','e','r',' ','v','1','.','0',0 },
3839 { 'S','o','f','t','w','a','r','e','\\',
3840 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
3841 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
3842 'i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ','S','t','r','o','n','g',
3843 ' ','C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r',
3844 'o','v','i','d','e','r',0 },
3845 { 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
3846 'C','r','y','p','t','o','g','r','a','p','h','y','\\','D','e','f','a','u','l','t','s','\\',
3847 'P','r','o','v','i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ',
3848 'R','S','A',' ','S','C','h','a','n','n','e','l',' ',
3849 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r','o','v','i','d','e','r',0 },
3850 { 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
3851 'C','r','y','p','t','o','g','r','a','p','h','y','\\','D','e','f','a','u','l','t','s','\\',
3852 'P','r','o','v','i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ',
3853 'E','n','h','a','n','c','e','d',' ','R','S','A',' ','a','n','d',' ','A','E','S',' ',
3854 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r','o','v','i','d','e','r',0 },
3855 { 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
3856 'C','r','y','p','t','o','g','r','a','p','h','y','\\','D','e','f','a','u','l','t','s','\\',
3857 'P','r','o','v','i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ',
3858 'E','n','h','a','n','c','e','d',' ','R','S','A',' ','a','n','d',' ','A','E','S',' ',
3859 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r','o','v','i','d','e','r',
3860 ' ','(','P','r','o','t','o','t','y','p','e',')',0 }
3862 static const WCHAR szDefaultKeys[3][65] = {
3863 { 'S','o','f','t','w','a','r','e','\\',
3864 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
3865 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
3866 'i','d','e','r',' ','T','y','p','e','s','\\','T','y','p','e',' ','0','0','1',0 },
3867 { 'S','o','f','t','w','a','r','e','\\',
3868 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
3869 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
3870 'i','d','e','r',' ','T','y','p','e','s','\\','T','y','p','e',' ','0','1','2',0 },
3871 { 'S','o','f','t','w','a','r','e','\\',
3872 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
3873 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
3874 'i','d','e','r',' ','T','y','p','e','s','\\','T','y','p','e',' ','0','2','4',0 }
3878 /******************************************************************************
3879 * DllRegisterServer (RSAENH.@)
3881 * Dll self registration.
3890 * Registers the following keys:
3891 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider\
3892 * Microsoft Base Cryptographic Provider v1.0
3893 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider\
3894 * Microsoft Enhanced Cryptographic Provider
3895 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider\
3896 * Microsoft Strong Cryptographpic Provider
3897 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider Types\Type 001
3899 HRESULT WINAPI DllRegisterServer(void)
3906 for (i=0; i<6; i++) {
3907 apiRet = RegCreateKeyExW(HKEY_LOCAL_MACHINE, szProviderKeys[i], 0, NULL,
3908 REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &key, &dp);
3910 if (apiRet == ERROR_SUCCESS)
3912 if (dp == REG_CREATED_NEW_KEY)
3914 static const WCHAR szImagePath[] = { 'I','m','a','g','e',' ','P','a','t','h',0 };
3915 static const WCHAR szRSABase[] = { 'r','s','a','e','n','h','.','d','l','l',0 };
3916 static const WCHAR szType[] = { 'T','y','p','e',0 };
3917 static const WCHAR szSignature[] = { 'S','i','g','n','a','t','u','r','e',0 };
3923 type=PROV_RSA_SCHANNEL;
3934 RegSetValueExW(key, szImagePath, 0, REG_SZ, (const BYTE *)szRSABase,
3935 (lstrlenW(szRSABase) + 1) * sizeof(WCHAR));
3936 RegSetValueExW(key, szType, 0, REG_DWORD, (LPBYTE)&type, sizeof(type));
3937 RegSetValueExW(key, szSignature, 0, REG_BINARY, (LPBYTE)&sign, sizeof(sign));
3943 for (i=0; i<3; i++) {
3944 apiRet = RegCreateKeyExW(HKEY_LOCAL_MACHINE, szDefaultKeys[i], 0, NULL,
3945 REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &key, &dp);
3946 if (apiRet == ERROR_SUCCESS)
3948 if (dp == REG_CREATED_NEW_KEY)
3950 static const WCHAR szName[] = { 'N','a','m','e',0 };
3951 static const WCHAR szRSAName[3][54] = {
3952 { 'M','i','c','r','o','s','o','f','t',' ', 'B','a','s','e',' ',
3953 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ',
3954 'P','r','o','v','i','d','e','r',' ','v','1','.','0',0 },
3955 { 'M','i','c','r','o','s','o','f','t',' ','R','S','A',' ',
3956 'S','C','h','a','n','n','e','l',' ',
3957 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ',
3958 'P','r','o','v','i','d','e','r',0 },
3959 { 'M','i','c','r','o','s','o','f','t',' ','E','n','h','a','n','c','e','d',' ',
3960 'R','S','A',' ','a','n','d',' ','A','E','S',' ',
3961 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ',
3962 'P','r','o','v','i','d','e','r',0 } };
3963 static const WCHAR szTypeName[] = { 'T','y','p','e','N','a','m','e',0 };
3964 static const WCHAR szRSATypeName[3][38] = {
3965 { 'R','S','A',' ','F','u','l','l',' ',
3966 '(','S','i','g','n','a','t','u','r','e',' ','a','n','d',' ',
3967 'K','e','y',' ','E','x','c','h','a','n','g','e',')',0 },
3968 { 'R','S','A',' ','S','C','h','a','n','n','e','l',0 },
3969 { 'R','S','A',' ','F','u','l','l',' ','a','n','d',' ','A','E','S',0 } };
3971 RegSetValueExW(key, szName, 0, REG_SZ,
3972 (const BYTE *)szRSAName[i], lstrlenW(szRSAName[i])*sizeof(WCHAR)+sizeof(WCHAR));
3973 RegSetValueExW(key, szTypeName, 0, REG_SZ,
3974 (const BYTE *)szRSATypeName[i], lstrlenW(szRSATypeName[i])*sizeof(WCHAR)+sizeof(WCHAR));
3980 return HRESULT_FROM_WIN32(apiRet);
3983 /******************************************************************************
3984 * DllUnregisterServer (RSAENH.@)
3986 * Dll self unregistration.
3994 * For the relevant keys see DllRegisterServer.
3996 HRESULT WINAPI DllUnregisterServer(void)
3998 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szProviderKeys[0]);
3999 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szProviderKeys[1]);
4000 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szProviderKeys[2]);
4001 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szProviderKeys[3]);
4002 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szProviderKeys[4]);
4003 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szProviderKeys[5]);
4004 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szDefaultKeys[0]);
4005 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szDefaultKeys[1]);
4006 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szDefaultKeys[2]);