3 * RSAENH - RSA encryption for Wine
5 * Copyright 2002 TransGaming Technologies (David Hammerton)
6 * Copyright 2004 Mike McCormack for CodeWeavers
7 * Copyright 2004, 2005 Michael Jung
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "wine/port.h"
26 #include "wine/library.h"
27 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(crypt);
42 /******************************************************************************
43 * CRYPTHASH - hash objects
45 #define RSAENH_MAGIC_HASH 0x85938417u
46 #define RSAENH_MAX_HASH_SIZE 104
47 #define RSAENH_HASHSTATE_HASHING 1
48 #define RSAENH_HASHSTATE_FINISHED 2
49 typedef struct _RSAENH_TLS1PRF_PARAMS
51 CRYPT_DATA_BLOB blobLabel;
52 CRYPT_DATA_BLOB blobSeed;
53 } RSAENH_TLS1PRF_PARAMS;
55 typedef struct tagCRYPTHASH
64 BYTE abHashValue[RSAENH_MAX_HASH_SIZE];
66 RSAENH_TLS1PRF_PARAMS tpPRFParams;
69 /******************************************************************************
70 * CRYPTKEY - key objects
72 #define RSAENH_MAGIC_KEY 0x73620457u
73 #define RSAENH_MAX_KEY_SIZE 48
74 #define RSAENH_MAX_BLOCK_SIZE 24
75 #define RSAENH_KEYSTATE_IDLE 0
76 #define RSAENH_KEYSTATE_ENCRYPTING 1
77 #define RSAENH_KEYSTATE_DECRYPTING 2
78 #define RSAENH_KEYSTATE_MASTERKEY 3
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
115 #define RSAENH_MAGIC_CONTAINER 0x26384993u
116 typedef struct tagKEYCONTAINER
122 DWORD dwEnumContainersCtr;
123 CHAR szName[MAX_PATH];
124 CHAR szProvName[MAX_PATH];
125 HCRYPTKEY hKeyExchangeKeyPair;
126 HCRYPTKEY hSignatureKeyPair;
129 /******************************************************************************
130 * Some magic constants
132 #define RSAENH_ENCRYPT 1
133 #define RSAENH_DECRYPT 0
134 #define RSAENH_HMAC_DEF_IPAD_CHAR 0x36
135 #define RSAENH_HMAC_DEF_OPAD_CHAR 0x5c
136 #define RSAENH_HMAC_DEF_PAD_LEN 64
137 #define RSAENH_DES_EFFECTIVE_KEYLEN 56
138 #define RSAENH_DES_STORAGE_KEYLEN 64
139 #define RSAENH_3DES112_EFFECTIVE_KEYLEN 112
140 #define RSAENH_3DES112_STORAGE_KEYLEN 128
141 #define RSAENH_3DES_EFFECTIVE_KEYLEN 168
142 #define RSAENH_3DES_STORAGE_KEYLEN 192
143 #define RSAENH_MAGIC_RSA2 0x32415352
144 #define RSAENH_MAGIC_RSA1 0x31415352
145 #define RSAENH_PKC_BLOCKTYPE 0x02
146 #define RSAENH_SSL3_VERSION_MAJOR 3
147 #define RSAENH_SSL3_VERSION_MINOR 0
148 #define RSAENH_TLS1_VERSION_MAJOR 3
149 #define RSAENH_TLS1_VERSION_MINOR 1
150 #define RSAENH_REGKEY "Software\\Wine\\Crypto\\RSA\\%s"
152 #define RSAENH_MIN(a,b) ((a)<(b)?(a):(b))
153 /******************************************************************************
154 * aProvEnumAlgsEx - Defines the capabilities of the CSP personalities.
156 #define RSAENH_MAX_ENUMALGS 20
157 #define RSAENH_PCT1_SSL2_SSL3_TLS1 (CRYPT_FLAG_PCT1|CRYPT_FLAG_SSL2|CRYPT_FLAG_SSL3|CRYPT_FLAG_TLS1)
158 static const PROV_ENUMALGS_EX aProvEnumAlgsEx[4][RSAENH_MAX_ENUMALGS+1] =
161 {CALG_RC2, 40, 40, 56,0, 4,"RC2", 24,"RSA Data Security's RC2"},
162 {CALG_RC4, 40, 40, 56,0, 4,"RC4", 24,"RSA Data Security's RC4"},
163 {CALG_DES, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
164 {CALG_SHA, 160,160, 160,CRYPT_FLAG_SIGNING, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
165 {CALG_MD2, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD2", 23,"Message Digest 2 (MD2)"},
166 {CALG_MD4, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD4", 23,"Message Digest 4 (MD4)"},
167 {CALG_MD5, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD5", 23,"Message Digest 5 (MD5)"},
168 {CALG_SSL3_SHAMD5,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
169 {CALG_MAC, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
170 {CALG_RSA_SIGN, 512,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_SIGN",14,"RSA Signature"},
171 {CALG_RSA_KEYX, 512,384, 1024,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_KEYX",17,"RSA Key Exchange"},
172 {CALG_HMAC, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
173 {0, 0, 0, 0,0, 1,"", 1,""}
176 {CALG_RC2, 128, 40, 128,0, 4,"RC2", 24,"RSA Data Security's RC2"},
177 {CALG_RC4, 128, 40, 128,0, 4,"RC4", 24,"RSA Data Security's RC4"},
178 {CALG_DES, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
179 {CALG_3DES_112, 112,112, 112,0, 13,"3DES TWO KEY",19,"Two Key Triple DES"},
180 {CALG_3DES, 168,168, 168,0, 5,"3DES", 21,"Three Key Triple DES"},
181 {CALG_SHA, 160,160, 160,CRYPT_FLAG_SIGNING, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
182 {CALG_MD2, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD2", 23,"Message Digest 2 (MD2)"},
183 {CALG_MD4, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD4", 23,"Message Digest 4 (MD4)"},
184 {CALG_MD5, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD5", 23,"Message Digest 5 (MD5)"},
185 {CALG_SSL3_SHAMD5,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
186 {CALG_MAC, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
187 {CALG_RSA_SIGN,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_SIGN",14,"RSA Signature"},
188 {CALG_RSA_KEYX,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_KEYX",17,"RSA Key Exchange"},
189 {CALG_HMAC, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
190 {0, 0, 0, 0,0, 1,"", 1,""}
193 {CALG_RC2, 128, 40, 128,0, 4,"RC2", 24,"RSA Data Security's RC2"},
194 {CALG_RC4, 128, 40, 128,0, 4,"RC4", 24,"RSA Data Security's RC4"},
195 {CALG_DES, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
196 {CALG_3DES_112, 112,112, 112,0, 13,"3DES TWO KEY",19,"Two Key Triple DES"},
197 {CALG_3DES, 168,168, 168,0, 5,"3DES", 21,"Three Key Triple DES"},
198 {CALG_SHA, 160,160, 160,CRYPT_FLAG_SIGNING, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
199 {CALG_MD2, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD2", 23,"Message Digest 2 (MD2)"},
200 {CALG_MD4, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD4", 23,"Message Digest 4 (MD4)"},
201 {CALG_MD5, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD5", 23,"Message Digest 5 (MD5)"},
202 {CALG_SSL3_SHAMD5,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
203 {CALG_MAC, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
204 {CALG_RSA_SIGN,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_SIGN",14,"RSA Signature"},
205 {CALG_RSA_KEYX,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_KEYX",17,"RSA Key Exchange"},
206 {CALG_HMAC, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
207 {0, 0, 0, 0,0, 1,"", 1,""}
210 {CALG_RC2, 128, 40, 128,RSAENH_PCT1_SSL2_SSL3_TLS1, 4,"RC2", 24,"RSA Data Security's RC2"},
211 {CALG_RC4, 128, 40, 128,RSAENH_PCT1_SSL2_SSL3_TLS1, 4,"RC4", 24,"RSA Data Security's RC4"},
212 {CALG_DES, 56, 56, 56,RSAENH_PCT1_SSL2_SSL3_TLS1, 4,"DES", 31,"Data Encryption Standard (DES)"},
213 {CALG_3DES_112, 112,112, 112,RSAENH_PCT1_SSL2_SSL3_TLS1,13,"3DES TWO KEY",19,"Two Key Triple DES"},
214 {CALG_3DES, 168,168, 168,RSAENH_PCT1_SSL2_SSL3_TLS1, 5,"3DES", 21,"Three Key Triple DES"},
215 {CALG_SHA,160,160,160,CRYPT_FLAG_SIGNING|RSAENH_PCT1_SSL2_SSL3_TLS1,6,"SHA-1",30,"Secure Hash Algorithm (SHA-1)"},
216 {CALG_MD5,128,128,128,CRYPT_FLAG_SIGNING|RSAENH_PCT1_SSL2_SSL3_TLS1,4,"MD5",23,"Message Digest 5 (MD5)"},
217 {CALG_SSL3_SHAMD5,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
218 {CALG_MAC, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
219 {CALG_RSA_SIGN,1024,384,16384,CRYPT_FLAG_SIGNING|RSAENH_PCT1_SSL2_SSL3_TLS1,9,"RSA_SIGN",14,"RSA Signature"},
220 {CALG_RSA_KEYX,1024,384,16384,CRYPT_FLAG_SIGNING|RSAENH_PCT1_SSL2_SSL3_TLS1,9,"RSA_KEYX",17,"RSA Key Exchange"},
221 {CALG_HMAC, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
222 {CALG_PCT1_MASTER,128,128,128,CRYPT_FLAG_PCT1, 12,"PCT1 MASTER",12,"PCT1 Master"},
223 {CALG_SSL2_MASTER,40,40, 192,CRYPT_FLAG_SSL2, 12,"SSL2 MASTER",12,"SSL2 Master"},
224 {CALG_SSL3_MASTER,384,384,384,CRYPT_FLAG_SSL3, 12,"SSL3 MASTER",12,"SSL3 Master"},
225 {CALG_TLS1_MASTER,384,384,384,CRYPT_FLAG_TLS1, 12,"TLS1 MASTER",12,"TLS1 Master"},
226 {CALG_SCHANNEL_MASTER_HASH,0,0,-1,0, 16,"SCH MASTER HASH",21,"SChannel Master Hash"},
227 {CALG_SCHANNEL_MAC_KEY,0,0,-1,0, 12,"SCH MAC KEY",17,"SChannel MAC Key"},
228 {CALG_SCHANNEL_ENC_KEY,0,0,-1,0, 12,"SCH ENC KEY",24,"SChannel Encryption Key"},
229 {CALG_TLS1PRF, 0, 0, -1,0, 9,"TLS1 PRF", 28,"TLS1 Pseudo Random Function"},
230 {0, 0, 0, 0,0, 1,"", 1,""}
234 /******************************************************************************
235 * API forward declarations
238 RSAENH_CPGetKeyParam(
269 RSAENH_CPSetHashParam(
273 BYTE *pbData, DWORD dwFlags
277 RSAENH_CPGetHashParam(
287 RSAENH_CPDestroyHash(
322 /******************************************************************************
323 * CSP's handle table (used by all acquired key containers)
325 static HANDLETABLE handle_table;
327 /******************************************************************************
330 * Initializes and destroys the handle table for the CSP's handles.
332 int WINAPI DllMain(HINSTANCE hInstance, DWORD fdwReason, PVOID pvReserved)
336 case DLL_PROCESS_ATTACH:
337 DisableThreadLibraryCalls(hInstance);
338 init_handle_table(&handle_table);
341 case DLL_PROCESS_DETACH:
342 destroy_handle_table(&handle_table);
348 /******************************************************************************
349 * copy_param [Internal]
351 * Helper function that supports the standard WINAPI protocol for querying data
355 * pbBuffer [O] Buffer where the queried parameter is copied to, if it is large enough.
356 * May be NUL if the required buffer size is to be queried only.
357 * pdwBufferSize [I/O] In: Size of the buffer at pbBuffer
358 * Out: Size of parameter pbParam
359 * pbParam [I] Parameter value.
360 * dwParamSize [I] Size of pbParam
363 * Success: TRUE (pbParam was copied into pbBuffer or pbBuffer is NULL)
364 * Failure: FALSE (pbBuffer is not large enough to hold pbParam). Last error: ERROR_MORE_DATA
366 static inline BOOL copy_param(
367 BYTE *pbBuffer, DWORD *pdwBufferSize, CONST BYTE *pbParam, DWORD dwParamSize)
371 if (dwParamSize > *pdwBufferSize)
373 SetLastError(ERROR_MORE_DATA);
374 *pdwBufferSize = dwParamSize;
377 memcpy(pbBuffer, pbParam, dwParamSize);
379 *pdwBufferSize = dwParamSize;
383 /******************************************************************************
384 * get_algid_info [Internal]
386 * Query CSP capabilities for a given crypto algorithm.
389 * hProv [I] Handle to a key container of the CSP whose capabilities are to be queried.
390 * algid [I] Identifier of the crypto algorithm about which information is requested.
393 * Success: Pointer to a PROV_ENUMALGS_EX struct containing information about the crypto algorithm.
394 * Failure: NULL (algid not supported)
396 static inline const PROV_ENUMALGS_EX* get_algid_info(HCRYPTPROV hProv, ALG_ID algid) {
397 const PROV_ENUMALGS_EX *iterator;
398 KEYCONTAINER *pKeyContainer;
400 if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER, (OBJECTHDR**)&pKeyContainer)) {
401 SetLastError(NTE_BAD_UID);
405 for (iterator = aProvEnumAlgsEx[pKeyContainer->dwPersonality]; iterator->aiAlgid; iterator++) {
406 if (iterator->aiAlgid == algid) return iterator;
409 SetLastError(NTE_BAD_ALGID);
413 /******************************************************************************
414 * copy_data_blob [Internal]
416 * deeply copies a DATA_BLOB
419 * dst [O] That's where the blob will be copied to
420 * src [I] Source blob
424 * Failure: FALSE (GetLastError() == NTE_NO_MEMORY
427 * Use free_data_blob to release resources occupied by copy_data_blob.
429 static inline BOOL copy_data_blob(PCRYPT_DATA_BLOB dst, CONST PCRYPT_DATA_BLOB src) {
430 dst->pbData = HeapAlloc(GetProcessHeap(), 0, src->cbData);
432 SetLastError(NTE_NO_MEMORY);
435 dst->cbData = src->cbData;
436 memcpy(dst->pbData, src->pbData, src->cbData);
440 /******************************************************************************
441 * concat_data_blobs [Internal]
443 * Concatenates two blobs
446 * dst [O] The new blob will be copied here
447 * src1 [I] Prefix blob
448 * src2 [I] Appendix blob
452 * Failure: FALSE (GetLastError() == NTE_NO_MEMORY)
455 * Release resources occupied by concat_data_blobs with free_data_blobs
457 static inline BOOL concat_data_blobs(PCRYPT_DATA_BLOB dst, CONST PCRYPT_DATA_BLOB src1,
458 CONST PCRYPT_DATA_BLOB src2)
460 dst->cbData = src1->cbData + src2->cbData;
461 dst->pbData = HeapAlloc(GetProcessHeap(), 0, dst->cbData);
463 SetLastError(NTE_NO_MEMORY);
466 memcpy(dst->pbData, src1->pbData, src1->cbData);
467 memcpy(dst->pbData + src1->cbData, src2->pbData, src2->cbData);
471 /******************************************************************************
472 * free_data_blob [Internal]
474 * releases resource occupied by a dynamically allocated CRYPT_DATA_BLOB
477 * pBlob [I] Heap space occupied by pBlob->pbData is released
479 static inline void free_data_blob(PCRYPT_DATA_BLOB pBlob) {
480 HeapFree(GetProcessHeap(), 0, pBlob->pbData);
483 /******************************************************************************
484 * init_data_blob [Internal]
486 static inline void init_data_blob(PCRYPT_DATA_BLOB pBlob) {
487 pBlob->pbData = NULL;
491 /******************************************************************************
492 * free_hmac_info [Internal]
494 * Deeply free an HMAC_INFO struct.
497 * hmac_info [I] Pointer to the HMAC_INFO struct to be freed.
500 * See Internet RFC 2104 for details on the HMAC algorithm.
502 static inline void free_hmac_info(PHMAC_INFO hmac_info) {
503 if (!hmac_info) return;
504 HeapFree(GetProcessHeap(), 0, hmac_info->pbInnerString);
505 HeapFree(GetProcessHeap(), 0, hmac_info->pbOuterString);
506 HeapFree(GetProcessHeap(), 0, hmac_info);
509 /******************************************************************************
510 * copy_hmac_info [Internal]
512 * Deeply copy an HMAC_INFO struct
515 * dst [O] Pointer to a location where the pointer to the HMAC_INFO copy will be stored.
516 * src [I] Pointer to the HMAC_INFO struct to be copied.
523 * See Internet RFC 2104 for details on the HMAC algorithm.
525 static BOOL copy_hmac_info(PHMAC_INFO *dst, const HMAC_INFO *src) {
526 if (!src) return FALSE;
527 *dst = HeapAlloc(GetProcessHeap(), 0, sizeof(HMAC_INFO));
528 if (!*dst) return FALSE;
529 memcpy(*dst, src, sizeof(HMAC_INFO));
530 (*dst)->pbInnerString = NULL;
531 (*dst)->pbOuterString = NULL;
532 if ((*dst)->cbInnerString == 0) (*dst)->cbInnerString = RSAENH_HMAC_DEF_PAD_LEN;
533 (*dst)->pbInnerString = HeapAlloc(GetProcessHeap(), 0, (*dst)->cbInnerString);
534 if (!(*dst)->pbInnerString) {
535 free_hmac_info(*dst);
538 if (src->cbInnerString)
539 memcpy((*dst)->pbInnerString, src->pbInnerString, src->cbInnerString);
541 memset((*dst)->pbInnerString, RSAENH_HMAC_DEF_IPAD_CHAR, RSAENH_HMAC_DEF_PAD_LEN);
542 if ((*dst)->cbOuterString == 0) (*dst)->cbOuterString = RSAENH_HMAC_DEF_PAD_LEN;
543 (*dst)->pbOuterString = HeapAlloc(GetProcessHeap(), 0, (*dst)->cbOuterString);
544 if (!(*dst)->pbOuterString) {
545 free_hmac_info(*dst);
548 if (src->cbOuterString)
549 memcpy((*dst)->pbOuterString, src->pbOuterString, src->cbOuterString);
551 memset((*dst)->pbOuterString, RSAENH_HMAC_DEF_OPAD_CHAR, RSAENH_HMAC_DEF_PAD_LEN);
555 /******************************************************************************
556 * destroy_hash [Internal]
558 * Destructor for hash objects
561 * pCryptHash [I] Pointer to the hash object to be destroyed.
562 * Will be invalid after function returns!
564 static void destroy_hash(OBJECTHDR *pObject)
566 CRYPTHASH *pCryptHash = (CRYPTHASH*)pObject;
568 free_hmac_info(pCryptHash->pHMACInfo);
569 free_data_blob(&pCryptHash->tpPRFParams.blobLabel);
570 free_data_blob(&pCryptHash->tpPRFParams.blobSeed);
571 HeapFree(GetProcessHeap(), 0, pCryptHash);
574 /******************************************************************************
575 * init_hash [Internal]
577 * Initialize (or reset) a hash object
580 * pCryptHash [I] The hash object to be initialized.
582 static inline BOOL init_hash(CRYPTHASH *pCryptHash) {
585 switch (pCryptHash->aiAlgid)
588 if (pCryptHash->pHMACInfo) {
589 const PROV_ENUMALGS_EX *pAlgInfo;
591 pAlgInfo = get_algid_info(pCryptHash->hProv, pCryptHash->pHMACInfo->HashAlgid);
592 if (!pAlgInfo) return FALSE;
593 pCryptHash->dwHashSize = pAlgInfo->dwDefaultLen >> 3;
594 init_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context);
595 update_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
596 pCryptHash->pHMACInfo->pbInnerString,
597 pCryptHash->pHMACInfo->cbInnerString);
602 dwLen = sizeof(DWORD);
603 RSAENH_CPGetKeyParam(pCryptHash->hProv, pCryptHash->hKey, KP_BLOCKLEN,
604 (BYTE*)&pCryptHash->dwHashSize, &dwLen, 0);
605 pCryptHash->dwHashSize >>= 3;
609 return init_hash_impl(pCryptHash->aiAlgid, &pCryptHash->context);
613 /******************************************************************************
614 * update_hash [Internal]
616 * Hashes the given data and updates the hash object's state accordingly
619 * pCryptHash [I] Hash object to be updated.
620 * pbData [I] Pointer to data stream to be hashed.
621 * dwDataLen [I] Length of data stream.
623 static inline void update_hash(CRYPTHASH *pCryptHash, CONST BYTE *pbData, DWORD dwDataLen) {
626 switch (pCryptHash->aiAlgid)
629 if (pCryptHash->pHMACInfo)
630 update_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
635 pbTemp = HeapAlloc(GetProcessHeap(), 0, dwDataLen);
637 memcpy(pbTemp, pbData, dwDataLen);
638 RSAENH_CPEncrypt(pCryptHash->hProv, pCryptHash->hKey, (HCRYPTHASH)NULL, FALSE, 0,
639 pbTemp, &dwDataLen, dwDataLen);
640 HeapFree(GetProcessHeap(), 0, pbTemp);
644 update_hash_impl(pCryptHash->aiAlgid, &pCryptHash->context, pbData, dwDataLen);
648 /******************************************************************************
649 * finalize_hash [Internal]
651 * Finalizes the hash, after all data has been hashed with update_hash.
652 * No additional data can be hashed afterwards until the hash gets initialized again.
655 * pCryptHash [I] Hash object to be finalized.
657 static inline void finalize_hash(CRYPTHASH *pCryptHash) {
660 switch (pCryptHash->aiAlgid)
663 if (pCryptHash->pHMACInfo) {
664 BYTE abHashValue[RSAENH_MAX_HASH_SIZE];
666 finalize_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
667 pCryptHash->abHashValue);
668 memcpy(abHashValue, pCryptHash->abHashValue, pCryptHash->dwHashSize);
669 init_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context);
670 update_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
671 pCryptHash->pHMACInfo->pbOuterString,
672 pCryptHash->pHMACInfo->cbOuterString);
673 update_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
674 abHashValue, pCryptHash->dwHashSize);
675 finalize_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
676 pCryptHash->abHashValue);
682 RSAENH_CPEncrypt(pCryptHash->hProv, pCryptHash->hKey, (HCRYPTHASH)NULL, TRUE, 0,
683 pCryptHash->abHashValue, &dwDataLen, pCryptHash->dwHashSize);
687 finalize_hash_impl(pCryptHash->aiAlgid, &pCryptHash->context, pCryptHash->abHashValue);
691 /******************************************************************************
692 * destroy_key [Internal]
694 * Destructor for key objects
697 * pCryptKey [I] Pointer to the key object to be destroyed.
698 * Will be invalid after function returns!
700 static void destroy_key(OBJECTHDR *pObject)
702 CRYPTKEY *pCryptKey = (CRYPTKEY*)pObject;
704 free_key_impl(pCryptKey->aiAlgid, &pCryptKey->context);
705 free_data_blob(&pCryptKey->siSChannelInfo.blobClientRandom);
706 free_data_blob(&pCryptKey->siSChannelInfo.blobServerRandom);
707 HeapFree(GetProcessHeap(), 0, pCryptKey);
710 /******************************************************************************
711 * setup_key [Internal]
713 * Initialize (or reset) a key object
716 * pCryptKey [I] The key object to be initialized.
718 static inline void setup_key(CRYPTKEY *pCryptKey) {
719 pCryptKey->dwState = RSAENH_KEYSTATE_IDLE;
720 memcpy(pCryptKey->abChainVector, pCryptKey->abInitVector, sizeof(pCryptKey->abChainVector));
721 setup_key_impl(pCryptKey->aiAlgid, &pCryptKey->context, pCryptKey->dwKeyLen,
722 pCryptKey->dwEffectiveKeyLen, pCryptKey->dwSaltLen,
723 pCryptKey->abKeyValue);
726 /******************************************************************************
729 * Creates a new key object without assigning the actual binary key value.
730 * This is done by CPDeriveKey, CPGenKey or CPImportKey, which call this function.
733 * hProv [I] Handle to the provider to which the created key will belong.
734 * aiAlgid [I] The new key shall use the crypto algorithm idenfied by aiAlgid.
735 * dwFlags [I] Upper 16 bits give the key length.
736 * Lower 16 bits: CRYPT_CREATE_SALT, CRYPT_NO_SALT
737 * ppCryptKey [O] Pointer to the created key
740 * Success: Handle to the created key.
741 * Failure: INVALID_HANDLE_VALUE
743 static HCRYPTKEY new_key(HCRYPTPROV hProv, ALG_ID aiAlgid, DWORD dwFlags, CRYPTKEY **ppCryptKey)
747 DWORD dwKeyLen = HIWORD(dwFlags);
748 const PROV_ENUMALGS_EX *peaAlgidInfo;
753 * Retrieve the CSP's capabilities for the given ALG_ID value
755 peaAlgidInfo = get_algid_info(hProv, aiAlgid);
756 if (!peaAlgidInfo) return (HCRYPTKEY)INVALID_HANDLE_VALUE;
759 * Assume the default key length, if none is specified explicitly
761 if (dwKeyLen == 0) dwKeyLen = peaAlgidInfo->dwDefaultLen;
764 * Check if the requested key length is supported by the current CSP.
765 * Adjust key length's for DES algorithms.
769 if (dwKeyLen == RSAENH_DES_EFFECTIVE_KEYLEN) {
770 dwKeyLen = RSAENH_DES_STORAGE_KEYLEN;
772 if (dwKeyLen != RSAENH_DES_STORAGE_KEYLEN) {
773 SetLastError(NTE_BAD_FLAGS);
774 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
779 if (dwKeyLen == RSAENH_3DES112_EFFECTIVE_KEYLEN) {
780 dwKeyLen = RSAENH_3DES112_STORAGE_KEYLEN;
782 if (dwKeyLen != RSAENH_3DES112_STORAGE_KEYLEN) {
783 SetLastError(NTE_BAD_FLAGS);
784 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
789 if (dwKeyLen == RSAENH_3DES_EFFECTIVE_KEYLEN) {
790 dwKeyLen = RSAENH_3DES_STORAGE_KEYLEN;
792 if (dwKeyLen != RSAENH_3DES_STORAGE_KEYLEN) {
793 SetLastError(NTE_BAD_FLAGS);
794 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
800 dwKeyLen > peaAlgidInfo->dwMaxLen ||
801 dwKeyLen < peaAlgidInfo->dwMinLen)
803 SetLastError(NTE_BAD_FLAGS);
804 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
808 hCryptKey = (HCRYPTKEY)new_object(&handle_table, sizeof(CRYPTKEY), RSAENH_MAGIC_KEY,
809 destroy_key, (OBJECTHDR**)&pCryptKey);
810 if (hCryptKey != (HCRYPTKEY)INVALID_HANDLE_VALUE)
812 pCryptKey->aiAlgid = aiAlgid;
813 pCryptKey->hProv = hProv;
814 pCryptKey->dwModeBits = 0;
815 pCryptKey->dwPermissions = CRYPT_ENCRYPT | CRYPT_DECRYPT | CRYPT_READ | CRYPT_WRITE |
817 pCryptKey->dwKeyLen = dwKeyLen >> 3;
818 pCryptKey->dwEffectiveKeyLen = 0;
819 if ((dwFlags & CRYPT_CREATE_SALT) || (dwKeyLen == 40 && !(dwFlags & CRYPT_NO_SALT)))
820 pCryptKey->dwSaltLen = 16 /*FIXME*/ - pCryptKey->dwKeyLen;
822 pCryptKey->dwSaltLen = 0;
823 memset(pCryptKey->abKeyValue, 0, sizeof(pCryptKey->abKeyValue));
824 memset(pCryptKey->abInitVector, 0, sizeof(pCryptKey->abInitVector));
825 init_data_blob(&pCryptKey->siSChannelInfo.blobClientRandom);
826 init_data_blob(&pCryptKey->siSChannelInfo.blobServerRandom);
830 case CALG_PCT1_MASTER:
831 case CALG_SSL2_MASTER:
832 case CALG_SSL3_MASTER:
833 case CALG_TLS1_MASTER:
835 pCryptKey->dwBlockLen = 0;
836 pCryptKey->dwMode = 0;
843 pCryptKey->dwBlockLen = 8;
844 pCryptKey->dwMode = CRYPT_MODE_CBC;
849 pCryptKey->dwBlockLen = dwKeyLen >> 3;
850 pCryptKey->dwMode = 0;
854 *ppCryptKey = pCryptKey;
860 /******************************************************************************
861 * store_key_pair [Internal]
863 * Stores a key pair to the registry
866 * hCryptKey [I] Handle to the key to be stored
867 * hKey [I] Registry key where the key pair is to be stored
868 * szValueName [I] Registry value where key pair's value is to be stored
869 * dwFlags [I] Flags for protecting the key
871 static void store_key_pair(HCRYPTKEY hCryptKey, HKEY hKey, LPCSTR szValueName, DWORD dwFlags)
873 DATA_BLOB blobIn, blobOut;
878 if (lookup_handle(&handle_table, hCryptKey, RSAENH_MAGIC_KEY,
881 if (RSAENH_CPExportKey(pKey->hProv, hCryptKey, 0, PRIVATEKEYBLOB, 0, 0,
884 pbKey = HeapAlloc(GetProcessHeap(), 0, dwLen);
887 if (RSAENH_CPExportKey(pKey->hProv, hCryptKey, 0,
888 PRIVATEKEYBLOB, 0, pbKey, &dwLen))
890 blobIn.pbData = pbKey;
891 blobIn.cbData = dwLen;
893 if (CryptProtectData(&blobIn, NULL, NULL, NULL, NULL,
896 RegSetValueExA(hKey, szValueName, 0, REG_BINARY,
897 blobOut.pbData, blobOut.cbData);
898 LocalFree(blobOut.pbData);
901 HeapFree(GetProcessHeap(), 0, pbKey);
904 release_handle(&handle_table, hCryptKey, RSAENH_MAGIC_KEY);
908 /******************************************************************************
909 * create_container_key [Internal]
911 * Creates the registry key for a key container's persistent storage.
914 * pKeyContainer [I] Pointer to the key container
915 * sam [I] Desired registry access
916 * phKey [O] Returned key
918 static BOOL create_container_key(KEYCONTAINER *pKeyContainer, REGSAM sam, HKEY *phKey)
920 CHAR szRSABase[MAX_PATH];
923 sprintf(szRSABase, RSAENH_REGKEY, pKeyContainer->szName);
925 if (pKeyContainer->dwFlags & CRYPT_MACHINE_KEYSET)
926 hRootKey = HKEY_LOCAL_MACHINE;
928 hRootKey = HKEY_CURRENT_USER;
930 /* @@ Wine registry key: HKLM\Software\Wine\Crypto\RSA */
931 /* @@ Wine registry key: HKCU\Software\Wine\Crypto\RSA */
932 return RegCreateKeyExA(hRootKey, szRSABase, 0, NULL,
933 REG_OPTION_NON_VOLATILE, sam, NULL, phKey, NULL)
937 /******************************************************************************
938 * open_container_key [Internal]
940 * Opens a key container's persistent storage for reading.
943 * pszContainerName [I] Name of the container to be opened. May be the empty
944 * string if the parent key of all containers is to be
946 * dwFlags [I] Flags indicating which keyset to be opened.
947 * phKey [O] Returned key
949 static BOOL open_container_key(LPCSTR pszContainerName, DWORD dwFlags, HKEY *phKey)
951 CHAR szRSABase[MAX_PATH];
954 sprintf(szRSABase, RSAENH_REGKEY, pszContainerName);
956 if (dwFlags & CRYPT_MACHINE_KEYSET)
957 hRootKey = HKEY_LOCAL_MACHINE;
959 hRootKey = HKEY_CURRENT_USER;
961 /* @@ Wine registry key: HKLM\Software\Wine\Crypto\RSA */
962 /* @@ Wine registry key: HKCU\Software\Wine\Crypto\RSA */
963 return RegOpenKeyExA(hRootKey, szRSABase, 0, KEY_READ, phKey) ==
967 /******************************************************************************
968 * delete_container_key [Internal]
970 * Deletes a key container's persistent storage.
973 * pszContainerName [I] Name of the container to be opened.
974 * dwFlags [I] Flags indicating which keyset to be opened.
976 static BOOL delete_container_key(LPCSTR pszContainerName, DWORD dwFlags)
978 CHAR szRegKey[MAX_PATH];
980 if (snprintf(szRegKey, MAX_PATH, RSAENH_REGKEY, pszContainerName) >= MAX_PATH) {
981 SetLastError(NTE_BAD_KEYSET_PARAM);
985 if (dwFlags & CRYPT_MACHINE_KEYSET)
986 hRootKey = HKEY_LOCAL_MACHINE;
988 hRootKey = HKEY_CURRENT_USER;
989 if (!RegDeleteKeyA(hRootKey, szRegKey)) {
990 SetLastError(ERROR_SUCCESS);
993 SetLastError(NTE_BAD_KEYSET);
999 /******************************************************************************
1000 * store_key_container_keys [Internal]
1002 * Stores key container's keys in a persistent location.
1005 * pKeyContainer [I] Pointer to the key container whose keys are to be saved
1007 static void store_key_container_keys(KEYCONTAINER *pKeyContainer)
1012 /* On WinXP, persistent keys are stored in a file located at:
1013 * $AppData$\\Microsoft\\Crypto\\RSA\\$SID$\\some_hex_string
1016 if (pKeyContainer->dwFlags & CRYPT_MACHINE_KEYSET)
1017 dwFlags = CRYPTPROTECT_LOCAL_MACHINE;
1021 if (create_container_key(pKeyContainer, KEY_WRITE, &hKey))
1023 store_key_pair(pKeyContainer->hKeyExchangeKeyPair, hKey,
1024 "KeyExchangeKeyPair", dwFlags);
1025 store_key_pair(pKeyContainer->hSignatureKeyPair, hKey,
1026 "SignatureKeyPair", dwFlags);
1031 /******************************************************************************
1032 * destroy_key_container [Internal]
1034 * Destructor for key containers.
1037 * pObjectHdr [I] Pointer to the key container to be destroyed.
1039 static void destroy_key_container(OBJECTHDR *pObjectHdr)
1041 KEYCONTAINER *pKeyContainer = (KEYCONTAINER*)pObjectHdr;
1043 if (!(pKeyContainer->dwFlags & CRYPT_VERIFYCONTEXT))
1044 store_key_container_keys(pKeyContainer);
1045 HeapFree( GetProcessHeap(), 0, pKeyContainer );
1048 /******************************************************************************
1049 * new_key_container [Internal]
1051 * Create a new key container. The personality (RSA Base, Strong or Enhanced CP)
1052 * of the CSP is determined via the pVTable->pszProvName string.
1055 * pszContainerName [I] Name of the key container.
1056 * pVTable [I] Callback functions and context info provided by the OS
1059 * Success: Handle to the new key container.
1060 * Failure: INVALID_HANDLE_VALUE
1062 static HCRYPTPROV new_key_container(PCCH pszContainerName, DWORD dwFlags, const VTableProvStruc *pVTable)
1064 KEYCONTAINER *pKeyContainer;
1065 HCRYPTPROV hKeyContainer;
1067 hKeyContainer = (HCRYPTPROV)new_object(&handle_table, sizeof(KEYCONTAINER), RSAENH_MAGIC_CONTAINER,
1068 destroy_key_container, (OBJECTHDR**)&pKeyContainer);
1069 if (hKeyContainer != (HCRYPTPROV)INVALID_HANDLE_VALUE)
1071 lstrcpynA(pKeyContainer->szName, pszContainerName, MAX_PATH);
1072 pKeyContainer->dwFlags = dwFlags;
1073 pKeyContainer->dwEnumAlgsCtr = 0;
1074 pKeyContainer->hKeyExchangeKeyPair = (HCRYPTKEY)INVALID_HANDLE_VALUE;
1075 pKeyContainer->hSignatureKeyPair = (HCRYPTKEY)INVALID_HANDLE_VALUE;
1076 if (pVTable && pVTable->pszProvName) {
1077 lstrcpynA(pKeyContainer->szProvName, pVTable->pszProvName, MAX_PATH);
1078 if (!strcmp(pVTable->pszProvName, MS_DEF_PROV_A)) {
1079 pKeyContainer->dwPersonality = RSAENH_PERSONALITY_BASE;
1080 } else if (!strcmp(pVTable->pszProvName, MS_ENHANCED_PROV_A)) {
1081 pKeyContainer->dwPersonality = RSAENH_PERSONALITY_ENHANCED;
1082 } else if (!strcmp(pVTable->pszProvName, MS_DEF_RSA_SCHANNEL_PROV_A)) {
1083 pKeyContainer->dwPersonality = RSAENH_PERSONALITY_SCHANNEL;
1085 pKeyContainer->dwPersonality = RSAENH_PERSONALITY_STRONG;
1089 /* The new key container has to be inserted into the CSP immediately
1090 * after creation to be available for CPGetProvParam's PP_ENUMCONTAINERS. */
1091 if (!(dwFlags & CRYPT_VERIFYCONTEXT)) {
1094 if (create_container_key(pKeyContainer, KEY_WRITE, &hKey))
1099 return hKeyContainer;
1102 /******************************************************************************
1103 * read_key_value [Internal]
1105 * Reads a key pair value from the registry
1108 * hKeyContainer [I] Crypt provider to use to import the key
1109 * hKey [I] Registry key from which to read the key pair
1110 * szValueName [I] Registry value from which to read the key pair's value
1111 * dwFlags [I] Flags for unprotecting the key
1112 * phCryptKey [O] Returned key
1114 static BOOL read_key_value(HCRYPTPROV hKeyContainer, HKEY hKey, LPCSTR szValueName, DWORD dwFlags, HCRYPTKEY *phCryptKey)
1116 DWORD dwValueType, dwLen;
1118 DATA_BLOB blobIn, blobOut;
1121 if (RegQueryValueExA(hKey, szValueName, 0, &dwValueType, NULL, &dwLen) ==
1124 pbKey = HeapAlloc(GetProcessHeap(), 0, dwLen);
1127 if (RegQueryValueExA(hKey, szValueName, 0, &dwValueType, pbKey, &dwLen) ==
1130 blobIn.pbData = pbKey;
1131 blobIn.cbData = dwLen;
1133 if (CryptUnprotectData(&blobIn, NULL, NULL, NULL, NULL,
1136 ret = RSAENH_CPImportKey(hKeyContainer, blobOut.pbData, blobOut.cbData, 0, 0,
1138 LocalFree(blobOut.pbData);
1141 HeapFree(GetProcessHeap(), 0, pbKey);
1147 /******************************************************************************
1148 * read_key_container [Internal]
1150 * Tries to read the persistent state of the key container (mainly the signature
1151 * and key exchange private keys) given by pszContainerName.
1154 * pszContainerName [I] Name of the key container to read from the registry
1155 * pVTable [I] Pointer to context data provided by the operating system
1158 * Success: Handle to the key container read from the registry
1159 * Failure: INVALID_HANDLE_VALUE
1161 static HCRYPTPROV read_key_container(PCHAR pszContainerName, DWORD dwFlags, const VTableProvStruc *pVTable)
1164 KEYCONTAINER *pKeyContainer;
1165 HCRYPTPROV hKeyContainer;
1166 HCRYPTKEY hCryptKey;
1168 if (!open_container_key(pszContainerName, dwFlags, &hKey))
1170 SetLastError(NTE_BAD_KEYSET);
1171 return (HCRYPTPROV)INVALID_HANDLE_VALUE;
1174 hKeyContainer = new_key_container(pszContainerName, dwFlags, pVTable);
1175 if (hKeyContainer != (HCRYPTPROV)INVALID_HANDLE_VALUE)
1177 DWORD dwProtectFlags = (dwFlags & CRYPT_MACHINE_KEYSET) ?
1178 CRYPTPROTECT_LOCAL_MACHINE : 0;
1180 if (!lookup_handle(&handle_table, hKeyContainer, RSAENH_MAGIC_CONTAINER,
1181 (OBJECTHDR**)&pKeyContainer))
1182 return (HCRYPTPROV)INVALID_HANDLE_VALUE;
1184 if (read_key_value(hKeyContainer, hKey, "KeyExchangeKeyPair",
1185 dwProtectFlags, &hCryptKey))
1186 pKeyContainer->hKeyExchangeKeyPair = hCryptKey;
1187 if (read_key_value(hKeyContainer, hKey, "SignatureKeyPair",
1188 dwProtectFlags, &hCryptKey))
1189 pKeyContainer->hSignatureKeyPair = hCryptKey;
1192 return hKeyContainer;
1195 /******************************************************************************
1196 * build_hash_signature [Internal]
1198 * Builds a padded version of a hash to match the length of the RSA key modulus.
1201 * pbSignature [O] The padded hash object is stored here.
1202 * dwLen [I] Length of the pbSignature buffer.
1203 * aiAlgid [I] Algorithm identifier of the hash to be padded.
1204 * abHashValue [I] The value of the hash object.
1205 * dwHashLen [I] Length of the hash value.
1206 * dwFlags [I] Selection of padding algorithm.
1210 * Failure: FALSE (NTE_BAD_ALGID)
1212 static BOOL build_hash_signature(BYTE *pbSignature, DWORD dwLen, ALG_ID aiAlgid,
1213 CONST BYTE *abHashValue, DWORD dwHashLen, DWORD dwFlags)
1215 /* These prefixes are meant to be concatenated with hash values of the
1216 * respective kind to form a PKCS #7 DigestInfo. */
1217 static const struct tagOIDDescriptor {
1220 CONST BYTE abOID[18];
1221 } aOIDDescriptor[5] = {
1222 { CALG_MD2, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1223 0x86, 0xf7, 0x0d, 0x02, 0x02, 0x05, 0x00, 0x04, 0x10 } },
1224 { CALG_MD4, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1225 0x86, 0xf7, 0x0d, 0x02, 0x04, 0x05, 0x00, 0x04, 0x10 } },
1226 { CALG_MD5, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1227 0x86, 0xf7, 0x0d, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10 } },
1228 { CALG_SHA, 15, { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03,
1229 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14 } },
1232 DWORD dwIdxOID, i, j;
1234 for (dwIdxOID = 0; aOIDDescriptor[dwIdxOID].aiAlgid; dwIdxOID++) {
1235 if (aOIDDescriptor[dwIdxOID].aiAlgid == aiAlgid) break;
1238 if (!aOIDDescriptor[dwIdxOID].aiAlgid) {
1239 SetLastError(NTE_BAD_ALGID);
1243 /* Build the padded signature */
1244 if (dwFlags & CRYPT_X931_FORMAT) {
1245 pbSignature[0] = 0x6b;
1246 for (i=1; i < dwLen - dwHashLen - 3; i++) {
1247 pbSignature[i] = 0xbb;
1249 pbSignature[i++] = 0xba;
1250 for (j=0; j < dwHashLen; j++, i++) {
1251 pbSignature[i] = abHashValue[j];
1253 pbSignature[i++] = 0x33;
1254 pbSignature[i++] = 0xcc;
1256 pbSignature[0] = 0x00;
1257 pbSignature[1] = 0x01;
1258 if (dwFlags & CRYPT_NOHASHOID) {
1259 for (i=2; i < dwLen - 1 - dwHashLen; i++) {
1260 pbSignature[i] = 0xff;
1262 pbSignature[i++] = 0x00;
1264 for (i=2; i < dwLen - 1 - aOIDDescriptor[dwIdxOID].dwLen - dwHashLen; i++) {
1265 pbSignature[i] = 0xff;
1267 pbSignature[i++] = 0x00;
1268 for (j=0; j < aOIDDescriptor[dwIdxOID].dwLen; j++) {
1269 pbSignature[i++] = aOIDDescriptor[dwIdxOID].abOID[j];
1272 for (j=0; j < dwHashLen; j++) {
1273 pbSignature[i++] = abHashValue[j];
1280 /******************************************************************************
1283 * This is an implementation of the 'P_hash' helper function for TLS1's PRF.
1284 * It is used exclusively by tls1_prf. For details see RFC 2246, chapter 5.
1285 * The pseudo random stream generated by this function is exclusive or'ed with
1286 * the data in pbBuffer.
1289 * hHMAC [I] HMAC object, which will be used in pseudo random generation
1290 * pblobSeed [I] Seed value
1291 * pbBuffer [I/O] Pseudo random stream will be xor'ed to the provided data
1292 * dwBufferLen [I] Number of pseudo random bytes desired
1298 static BOOL tls1_p(HCRYPTHASH hHMAC, CONST PCRYPT_DATA_BLOB pblobSeed, PBYTE pbBuffer, DWORD dwBufferLen)
1301 BYTE abAi[RSAENH_MAX_HASH_SIZE];
1304 if (!lookup_handle(&handle_table, hHMAC, RSAENH_MAGIC_HASH, (OBJECTHDR**)&pHMAC)) {
1305 SetLastError(NTE_BAD_HASH);
1309 /* compute A_1 = HMAC(seed) */
1311 update_hash(pHMAC, pblobSeed->pbData, pblobSeed->cbData);
1312 finalize_hash(pHMAC);
1313 memcpy(abAi, pHMAC->abHashValue, pHMAC->dwHashSize);
1316 /* compute HMAC(A_i + seed) */
1318 update_hash(pHMAC, abAi, pHMAC->dwHashSize);
1319 update_hash(pHMAC, pblobSeed->pbData, pblobSeed->cbData);
1320 finalize_hash(pHMAC);
1322 /* pseudo random stream := CONCAT_{i=1..n} ( HMAC(A_i + seed) ) */
1324 if (i >= dwBufferLen) break;
1325 pbBuffer[i] ^= pHMAC->abHashValue[i % pHMAC->dwHashSize];
1327 } while (i % pHMAC->dwHashSize);
1329 /* compute A_{i+1} = HMAC(A_i) */
1331 update_hash(pHMAC, abAi, pHMAC->dwHashSize);
1332 finalize_hash(pHMAC);
1333 memcpy(abAi, pHMAC->abHashValue, pHMAC->dwHashSize);
1334 } while (i < dwBufferLen);
1339 /******************************************************************************
1340 * tls1_prf [Internal]
1342 * TLS1 pseudo random function as specified in RFC 2246, chapter 5
1345 * hProv [I] Key container used to compute the pseudo random stream
1346 * hSecret [I] Key that holds the (pre-)master secret
1347 * pblobLabel [I] Descriptive label
1348 * pblobSeed [I] Seed value
1349 * pbBuffer [O] Pseudo random numbers will be stored here
1350 * dwBufferLen [I] Number of pseudo random bytes desired
1356 static BOOL tls1_prf(HCRYPTPROV hProv, HCRYPTPROV hSecret, CONST PCRYPT_DATA_BLOB pblobLabel,
1357 CONST PCRYPT_DATA_BLOB pblobSeed, PBYTE pbBuffer, DWORD dwBufferLen)
1359 HMAC_INFO hmacInfo = { 0, NULL, 0, NULL, 0 };
1360 HCRYPTHASH hHMAC = (HCRYPTHASH)INVALID_HANDLE_VALUE;
1361 HCRYPTKEY hHalfSecret = (HCRYPTKEY)INVALID_HANDLE_VALUE;
1362 CRYPTKEY *pHalfSecret, *pSecret;
1363 DWORD dwHalfSecretLen;
1364 BOOL result = FALSE;
1365 CRYPT_DATA_BLOB blobLabelSeed;
1367 TRACE("(hProv=%08lx, hSecret=%08lx, pblobLabel=%p, pblobSeed=%p, pbBuffer=%p, dwBufferLen=%d)\n",
1368 hProv, hSecret, pblobLabel, pblobSeed, pbBuffer, dwBufferLen);
1370 if (!lookup_handle(&handle_table, hSecret, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pSecret)) {
1371 SetLastError(NTE_FAIL);
1375 dwHalfSecretLen = (pSecret->dwKeyLen+1)/2;
1377 /* concatenation of the label and the seed */
1378 if (!concat_data_blobs(&blobLabelSeed, pblobLabel, pblobSeed)) goto exit;
1380 /* zero out the buffer, since two random streams will be xor'ed into it. */
1381 memset(pbBuffer, 0, dwBufferLen);
1383 /* build a 'fake' key, to hold the secret. CALG_SSL2_MASTER is used since it provides
1384 * the biggest range of valid key lengths. */
1385 hHalfSecret = new_key(hProv, CALG_SSL2_MASTER, MAKELONG(0,dwHalfSecretLen*8), &pHalfSecret);
1386 if (hHalfSecret == (HCRYPTKEY)INVALID_HANDLE_VALUE) goto exit;
1388 /* Derive an HMAC_MD5 hash and call the helper function. */
1389 memcpy(pHalfSecret->abKeyValue, pSecret->abKeyValue, dwHalfSecretLen);
1390 if (!RSAENH_CPCreateHash(hProv, CALG_HMAC, hHalfSecret, 0, &hHMAC)) goto exit;
1391 hmacInfo.HashAlgid = CALG_MD5;
1392 if (!RSAENH_CPSetHashParam(hProv, hHMAC, HP_HMAC_INFO, (BYTE*)&hmacInfo, 0)) goto exit;
1393 if (!tls1_p(hHMAC, &blobLabelSeed, pbBuffer, dwBufferLen)) goto exit;
1395 /* Reconfigure to HMAC_SHA hash and call helper function again. */
1396 memcpy(pHalfSecret->abKeyValue, pSecret->abKeyValue + (pSecret->dwKeyLen/2), dwHalfSecretLen);
1397 hmacInfo.HashAlgid = CALG_SHA;
1398 if (!RSAENH_CPSetHashParam(hProv, hHMAC, HP_HMAC_INFO, (BYTE*)&hmacInfo, 0)) goto exit;
1399 if (!tls1_p(hHMAC, &blobLabelSeed, pbBuffer, dwBufferLen)) goto exit;
1403 release_handle(&handle_table, hHalfSecret, RSAENH_MAGIC_KEY);
1404 if (hHMAC != (HCRYPTHASH)INVALID_HANDLE_VALUE) RSAENH_CPDestroyHash(hProv, hHMAC);
1405 free_data_blob(&blobLabelSeed);
1409 /******************************************************************************
1410 * pad_data [Internal]
1412 * Helper function for data padding according to PKCS1 #2
1415 * abData [I] The data to be padded
1416 * dwDataLen [I] Length of the data
1417 * abBuffer [O] Padded data will be stored here
1418 * dwBufferLen [I] Length of the buffer (also length of padded data)
1419 * dwFlags [I] Padding format (CRYPT_SSL2_FALLBACK)
1423 * Failure: FALSE (NTE_BAD_LEN, too much data to pad)
1425 static BOOL pad_data(CONST BYTE *abData, DWORD dwDataLen, BYTE *abBuffer, DWORD dwBufferLen,
1430 /* Ensure there is enough space for PKCS1 #2 padding */
1431 if (dwDataLen > dwBufferLen-11) {
1432 SetLastError(NTE_BAD_LEN);
1436 memmove(abBuffer + dwBufferLen - dwDataLen, abData, dwDataLen);
1439 abBuffer[1] = RSAENH_PKC_BLOCKTYPE;
1440 for (i=2; i < dwBufferLen - dwDataLen - 1; i++)
1441 do gen_rand_impl(&abBuffer[i], 1); while (!abBuffer[i]);
1442 if (dwFlags & CRYPT_SSL2_FALLBACK)
1443 for (i-=8; i < dwBufferLen - dwDataLen - 1; i++)
1450 /******************************************************************************
1451 * unpad_data [Internal]
1453 * Remove the PKCS1 padding from RSA decrypted data
1456 * abData [I] The padded data
1457 * dwDataLen [I] Length of the padded data
1458 * abBuffer [O] Data without padding will be stored here
1459 * dwBufferLen [I/O] I: Length of the buffer, O: Length of unpadded data
1460 * dwFlags [I] Currently none defined
1464 * Failure: FALSE, (NTE_BAD_DATA, no valid PKCS1 padding or buffer too small)
1466 static BOOL unpad_data(CONST BYTE *abData, DWORD dwDataLen, BYTE *abBuffer, DWORD *dwBufferLen,
1471 for (i=2; i<dwDataLen; i++)
1475 if ((i == dwDataLen) || (*dwBufferLen < dwDataLen - i - 1) ||
1476 (abData[0] != 0x00) || (abData[1] != RSAENH_PKC_BLOCKTYPE))
1478 SetLastError(NTE_BAD_DATA);
1482 *dwBufferLen = dwDataLen - i - 1;
1483 memmove(abBuffer, abData + i + 1, *dwBufferLen);
1487 /******************************************************************************
1488 * CPAcquireContext (RSAENH.@)
1490 * Acquire a handle to the key container specified by pszContainer
1493 * phProv [O] Pointer to the location the acquired handle will be written to.
1494 * pszContainer [I] Name of the desired key container. See Notes
1495 * dwFlags [I] Flags. See Notes.
1496 * pVTable [I] Pointer to a PVTableProvStruct containing callbacks.
1503 * If pszContainer is NULL or points to a zero length string the user's login
1504 * name will be used as the key container name.
1506 * If the CRYPT_NEW_KEYSET flag is set in dwFlags a new keyset will be created.
1507 * If a keyset with the given name already exists, the function fails and sets
1508 * last error to NTE_EXISTS. If CRYPT_NEW_KEYSET is not set and the specified
1509 * key container does not exist, function fails and sets last error to
1512 BOOL WINAPI RSAENH_CPAcquireContext(HCRYPTPROV *phProv, LPSTR pszContainer,
1513 DWORD dwFlags, PVTableProvStruc pVTable)
1515 CHAR szKeyContainerName[MAX_PATH];
1517 TRACE("(phProv=%p, pszContainer=%s, dwFlags=%08x, pVTable=%p)\n", phProv,
1518 debugstr_a(pszContainer), dwFlags, pVTable);
1520 if (pszContainer && *pszContainer)
1522 lstrcpynA(szKeyContainerName, pszContainer, MAX_PATH);
1526 DWORD dwLen = sizeof(szKeyContainerName);
1527 if (!GetUserNameA(szKeyContainerName, &dwLen)) return FALSE;
1530 switch (dwFlags & (CRYPT_NEWKEYSET|CRYPT_VERIFYCONTEXT|CRYPT_DELETEKEYSET))
1533 *phProv = read_key_container(szKeyContainerName, dwFlags, pVTable);
1536 case CRYPT_DELETEKEYSET:
1537 return delete_container_key(szKeyContainerName, dwFlags);
1539 case CRYPT_NEWKEYSET:
1540 *phProv = read_key_container(szKeyContainerName, dwFlags, pVTable);
1541 if (*phProv != (HCRYPTPROV)INVALID_HANDLE_VALUE)
1543 release_handle(&handle_table, *phProv, RSAENH_MAGIC_CONTAINER);
1544 TRACE("Can't create new keyset, already exists\n");
1545 SetLastError(NTE_EXISTS);
1548 *phProv = new_key_container(szKeyContainerName, dwFlags, pVTable);
1551 case CRYPT_VERIFYCONTEXT|CRYPT_NEWKEYSET:
1552 case CRYPT_VERIFYCONTEXT:
1554 TRACE("pszContainer should be NULL\n");
1555 SetLastError(NTE_BAD_FLAGS);
1558 *phProv = new_key_container("", dwFlags, pVTable);
1562 *phProv = (HCRYPTPROV)INVALID_HANDLE_VALUE;
1563 SetLastError(NTE_BAD_FLAGS);
1567 if (*phProv != (HCRYPTPROV)INVALID_HANDLE_VALUE) {
1568 SetLastError(ERROR_SUCCESS);
1575 /******************************************************************************
1576 * CPCreateHash (RSAENH.@)
1578 * CPCreateHash creates and initalizes a new hash object.
1581 * hProv [I] Handle to the key container to which the new hash will belong.
1582 * Algid [I] Identifies the hash algorithm, which will be used for the hash.
1583 * hKey [I] Handle to a session key applied for keyed hashes.
1584 * dwFlags [I] Currently no flags defined. Must be zero.
1585 * phHash [O] Points to the location where a handle to the new hash will be stored.
1592 * hKey is a handle to a session key applied in keyed hashes like MAC and HMAC.
1593 * If a normal hash object is to be created (like e.g. MD2 or SHA1) hKey must be zero.
1595 BOOL WINAPI RSAENH_CPCreateHash(HCRYPTPROV hProv, ALG_ID Algid, HCRYPTKEY hKey, DWORD dwFlags,
1598 CRYPTKEY *pCryptKey;
1599 CRYPTHASH *pCryptHash;
1600 const PROV_ENUMALGS_EX *peaAlgidInfo;
1602 TRACE("(hProv=%08lx, Algid=%08x, hKey=%08lx, dwFlags=%08x, phHash=%p)\n", hProv, Algid, hKey,
1605 peaAlgidInfo = get_algid_info(hProv, Algid);
1606 if (!peaAlgidInfo) return FALSE;
1610 SetLastError(NTE_BAD_FLAGS);
1614 if (Algid == CALG_MAC || Algid == CALG_HMAC || Algid == CALG_SCHANNEL_MASTER_HASH ||
1615 Algid == CALG_TLS1PRF)
1617 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey)) {
1618 SetLastError(NTE_BAD_KEY);
1622 if ((Algid == CALG_MAC) && (GET_ALG_TYPE(pCryptKey->aiAlgid) != ALG_TYPE_BLOCK)) {
1623 SetLastError(NTE_BAD_KEY);
1627 if ((Algid == CALG_SCHANNEL_MASTER_HASH || Algid == CALG_TLS1PRF) &&
1628 (pCryptKey->aiAlgid != CALG_TLS1_MASTER))
1630 SetLastError(NTE_BAD_KEY);
1634 if ((Algid == CALG_TLS1PRF) && (pCryptKey->dwState != RSAENH_KEYSTATE_MASTERKEY)) {
1635 SetLastError(NTE_BAD_KEY_STATE);
1640 *phHash = (HCRYPTHASH)new_object(&handle_table, sizeof(CRYPTHASH), RSAENH_MAGIC_HASH,
1641 destroy_hash, (OBJECTHDR**)&pCryptHash);
1642 if (!pCryptHash) return FALSE;
1644 pCryptHash->aiAlgid = Algid;
1645 pCryptHash->hKey = hKey;
1646 pCryptHash->hProv = hProv;
1647 pCryptHash->dwState = RSAENH_HASHSTATE_HASHING;
1648 pCryptHash->pHMACInfo = (PHMAC_INFO)NULL;
1649 pCryptHash->dwHashSize = peaAlgidInfo->dwDefaultLen >> 3;
1650 init_data_blob(&pCryptHash->tpPRFParams.blobLabel);
1651 init_data_blob(&pCryptHash->tpPRFParams.blobSeed);
1653 if (Algid == CALG_SCHANNEL_MASTER_HASH) {
1654 static const char keyex[] = "key expansion";
1655 BYTE key_expansion[sizeof keyex];
1656 CRYPT_DATA_BLOB blobRandom, blobKeyExpansion = { 13, key_expansion };
1658 memcpy( key_expansion, keyex, sizeof keyex );
1660 if (pCryptKey->dwState != RSAENH_KEYSTATE_MASTERKEY) {
1661 static const char msec[] = "master secret";
1662 BYTE master_secret[sizeof msec];
1663 CRYPT_DATA_BLOB blobLabel = { 13, master_secret };
1664 BYTE abKeyValue[48];
1666 memcpy( master_secret, msec, sizeof msec );
1668 /* See RFC 2246, chapter 8.1 */
1669 if (!concat_data_blobs(&blobRandom,
1670 &pCryptKey->siSChannelInfo.blobClientRandom,
1671 &pCryptKey->siSChannelInfo.blobServerRandom))
1675 tls1_prf(hProv, hKey, &blobLabel, &blobRandom, abKeyValue, 48);
1676 pCryptKey->dwState = RSAENH_KEYSTATE_MASTERKEY;
1677 memcpy(pCryptKey->abKeyValue, abKeyValue, 48);
1678 free_data_blob(&blobRandom);
1681 /* See RFC 2246, chapter 6.3 */
1682 if (!concat_data_blobs(&blobRandom,
1683 &pCryptKey->siSChannelInfo.blobServerRandom,
1684 &pCryptKey->siSChannelInfo.blobClientRandom))
1688 tls1_prf(hProv, hKey, &blobKeyExpansion, &blobRandom, pCryptHash->abHashValue,
1689 RSAENH_MAX_HASH_SIZE);
1690 free_data_blob(&blobRandom);
1693 return init_hash(pCryptHash);
1696 /******************************************************************************
1697 * CPDestroyHash (RSAENH.@)
1699 * Releases the handle to a hash object. The object is destroyed if it's reference
1700 * count reaches zero.
1703 * hProv [I] Handle to the key container to which the hash object belongs.
1704 * hHash [I] Handle to the hash object to be released.
1710 BOOL WINAPI RSAENH_CPDestroyHash(HCRYPTPROV hProv, HCRYPTHASH hHash)
1712 TRACE("(hProv=%08lx, hHash=%08lx)\n", hProv, hHash);
1714 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
1716 SetLastError(NTE_BAD_UID);
1720 if (!release_handle(&handle_table, hHash, RSAENH_MAGIC_HASH))
1722 SetLastError(NTE_BAD_HASH);
1729 /******************************************************************************
1730 * CPDestroyKey (RSAENH.@)
1732 * Releases the handle to a key object. The object is destroyed if it's reference
1733 * count reaches zero.
1736 * hProv [I] Handle to the key container to which the key object belongs.
1737 * hKey [I] Handle to the key object to be released.
1743 BOOL WINAPI RSAENH_CPDestroyKey(HCRYPTPROV hProv, HCRYPTKEY hKey)
1745 TRACE("(hProv=%08lx, hKey=%08lx)\n", hProv, hKey);
1747 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
1749 SetLastError(NTE_BAD_UID);
1753 if (!release_handle(&handle_table, hKey, RSAENH_MAGIC_KEY))
1755 SetLastError(NTE_BAD_KEY);
1762 /******************************************************************************
1763 * CPDuplicateHash (RSAENH.@)
1765 * Clones a hash object including it's current state.
1768 * hUID [I] Handle to the key container the hash belongs to.
1769 * hHash [I] Handle to the hash object to be cloned.
1770 * pdwReserved [I] Reserved. Must be NULL.
1771 * dwFlags [I] No flags are currently defined. Must be 0.
1772 * phHash [O] Handle to the cloned hash object.
1778 BOOL WINAPI RSAENH_CPDuplicateHash(HCRYPTPROV hUID, HCRYPTHASH hHash, DWORD *pdwReserved,
1779 DWORD dwFlags, HCRYPTHASH *phHash)
1781 CRYPTHASH *pSrcHash, *pDestHash;
1783 TRACE("(hUID=%08lx, hHash=%08lx, pdwReserved=%p, dwFlags=%08x, phHash=%p)\n", hUID, hHash,
1784 pdwReserved, dwFlags, phHash);
1786 if (!is_valid_handle(&handle_table, hUID, RSAENH_MAGIC_CONTAINER))
1788 SetLastError(NTE_BAD_UID);
1792 if (!lookup_handle(&handle_table, hHash, RSAENH_MAGIC_HASH, (OBJECTHDR**)&pSrcHash))
1794 SetLastError(NTE_BAD_HASH);
1798 if (!phHash || pdwReserved || dwFlags)
1800 SetLastError(ERROR_INVALID_PARAMETER);
1804 *phHash = (HCRYPTHASH)new_object(&handle_table, sizeof(CRYPTHASH), RSAENH_MAGIC_HASH,
1805 destroy_hash, (OBJECTHDR**)&pDestHash);
1806 if (*phHash != (HCRYPTHASH)INVALID_HANDLE_VALUE)
1808 memcpy(pDestHash, pSrcHash, sizeof(CRYPTHASH));
1809 duplicate_hash_impl(pSrcHash->aiAlgid, &pSrcHash->context, &pDestHash->context);
1810 copy_hmac_info(&pDestHash->pHMACInfo, pSrcHash->pHMACInfo);
1811 copy_data_blob(&pDestHash->tpPRFParams.blobLabel, &pSrcHash->tpPRFParams.blobLabel);
1812 copy_data_blob(&pDestHash->tpPRFParams.blobSeed, &pSrcHash->tpPRFParams.blobSeed);
1815 return *phHash != (HCRYPTHASH)INVALID_HANDLE_VALUE;
1818 /******************************************************************************
1819 * CPDuplicateKey (RSAENH.@)
1821 * Clones a key object including it's current state.
1824 * hUID [I] Handle to the key container the hash belongs to.
1825 * hKey [I] Handle to the key object to be cloned.
1826 * pdwReserved [I] Reserved. Must be NULL.
1827 * dwFlags [I] No flags are currently defined. Must be 0.
1828 * phHash [O] Handle to the cloned key object.
1834 BOOL WINAPI RSAENH_CPDuplicateKey(HCRYPTPROV hUID, HCRYPTKEY hKey, DWORD *pdwReserved,
1835 DWORD dwFlags, HCRYPTKEY *phKey)
1837 CRYPTKEY *pSrcKey, *pDestKey;
1839 TRACE("(hUID=%08lx, hKey=%08lx, pdwReserved=%p, dwFlags=%08x, phKey=%p)\n", hUID, hKey,
1840 pdwReserved, dwFlags, phKey);
1842 if (!is_valid_handle(&handle_table, hUID, RSAENH_MAGIC_CONTAINER))
1844 SetLastError(NTE_BAD_UID);
1848 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pSrcKey))
1850 SetLastError(NTE_BAD_KEY);
1854 if (!phKey || pdwReserved || dwFlags)
1856 SetLastError(ERROR_INVALID_PARAMETER);
1860 *phKey = (HCRYPTKEY)new_object(&handle_table, sizeof(CRYPTKEY), RSAENH_MAGIC_KEY, destroy_key,
1861 (OBJECTHDR**)&pDestKey);
1862 if (*phKey != (HCRYPTKEY)INVALID_HANDLE_VALUE)
1864 memcpy(pDestKey, pSrcKey, sizeof(CRYPTKEY));
1865 copy_data_blob(&pDestKey->siSChannelInfo.blobServerRandom,
1866 &pSrcKey->siSChannelInfo.blobServerRandom);
1867 copy_data_blob(&pDestKey->siSChannelInfo.blobClientRandom,
1868 &pSrcKey->siSChannelInfo.blobClientRandom);
1869 duplicate_key_impl(pSrcKey->aiAlgid, &pSrcKey->context, &pDestKey->context);
1878 /******************************************************************************
1879 * CPEncrypt (RSAENH.@)
1884 * hProv [I] The key container hKey and hHash belong to.
1885 * hKey [I] The key used to encrypt the data.
1886 * hHash [I] An optional hash object for parallel hashing. See notes.
1887 * Final [I] Indicates if this is the last block of data to encrypt.
1888 * dwFlags [I] Currently no flags defined. Must be zero.
1889 * pbData [I/O] Pointer to the data to encrypt. Encrypted data will also be stored there.
1890 * pdwDataLen [I/O] I: Length of data to encrypt, O: Length of encrypted data.
1891 * dwBufLen [I] Size of the buffer at pbData.
1898 * If a hash object handle is provided in hHash, it will be updated with the plaintext.
1899 * This is useful for message signatures.
1901 * This function uses the standard WINAPI protocol for querying data of dynamic length.
1903 BOOL WINAPI RSAENH_CPEncrypt(HCRYPTPROV hProv, HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final,
1904 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen, DWORD dwBufLen)
1906 CRYPTKEY *pCryptKey;
1907 BYTE *in, out[RSAENH_MAX_BLOCK_SIZE], o[RSAENH_MAX_BLOCK_SIZE];
1908 DWORD dwEncryptedLen, i, j, k;
1910 TRACE("(hProv=%08lx, hKey=%08lx, hHash=%08lx, Final=%d, dwFlags=%08x, pbData=%p, "
1911 "pdwDataLen=%p, dwBufLen=%d)\n", hProv, hKey, hHash, Final, dwFlags, pbData, pdwDataLen,
1914 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
1916 SetLastError(NTE_BAD_UID);
1922 SetLastError(NTE_BAD_FLAGS);
1926 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
1928 SetLastError(NTE_BAD_KEY);
1932 if (pCryptKey->dwState == RSAENH_KEYSTATE_IDLE)
1933 pCryptKey->dwState = RSAENH_KEYSTATE_ENCRYPTING;
1935 if (pCryptKey->dwState != RSAENH_KEYSTATE_ENCRYPTING)
1937 SetLastError(NTE_BAD_DATA);
1941 if (is_valid_handle(&handle_table, hHash, RSAENH_MAGIC_HASH)) {
1942 if (!RSAENH_CPHashData(hProv, hHash, pbData, *pdwDataLen, 0)) return FALSE;
1945 if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_BLOCK) {
1946 if (!Final && (*pdwDataLen % pCryptKey->dwBlockLen)) {
1947 SetLastError(NTE_BAD_DATA);
1951 dwEncryptedLen = (*pdwDataLen/pCryptKey->dwBlockLen+(Final?1:0))*pCryptKey->dwBlockLen;
1953 if (pbData == NULL) {
1954 *pdwDataLen = dwEncryptedLen;
1957 else if (dwEncryptedLen > dwBufLen) {
1958 *pdwDataLen = dwEncryptedLen;
1959 SetLastError(ERROR_MORE_DATA);
1963 /* Pad final block with length bytes */
1964 for (i=*pdwDataLen; i<dwEncryptedLen; i++) pbData[i] = dwEncryptedLen - *pdwDataLen;
1965 *pdwDataLen = dwEncryptedLen;
1967 for (i=0, in=pbData; i<*pdwDataLen; i+=pCryptKey->dwBlockLen, in+=pCryptKey->dwBlockLen) {
1968 switch (pCryptKey->dwMode) {
1969 case CRYPT_MODE_ECB:
1970 encrypt_block_impl(pCryptKey->aiAlgid, 0, &pCryptKey->context, in, out,
1974 case CRYPT_MODE_CBC:
1975 for (j=0; j<pCryptKey->dwBlockLen; j++) in[j] ^= pCryptKey->abChainVector[j];
1976 encrypt_block_impl(pCryptKey->aiAlgid, 0, &pCryptKey->context, in, out,
1978 memcpy(pCryptKey->abChainVector, out, pCryptKey->dwBlockLen);
1981 case CRYPT_MODE_CFB:
1982 for (j=0; j<pCryptKey->dwBlockLen; j++) {
1983 encrypt_block_impl(pCryptKey->aiAlgid, 0, &pCryptKey->context,
1984 pCryptKey->abChainVector, o, RSAENH_ENCRYPT);
1985 out[j] = in[j] ^ o[0];
1986 for (k=0; k<pCryptKey->dwBlockLen-1; k++)
1987 pCryptKey->abChainVector[k] = pCryptKey->abChainVector[k+1];
1988 pCryptKey->abChainVector[k] = out[j];
1993 SetLastError(NTE_BAD_ALGID);
1996 memcpy(in, out, pCryptKey->dwBlockLen);
1998 } else if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_STREAM) {
1999 if (pbData == NULL) {
2000 *pdwDataLen = dwBufLen;
2003 encrypt_stream_impl(pCryptKey->aiAlgid, &pCryptKey->context, pbData, *pdwDataLen);
2004 } else if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_RSA) {
2005 if (pCryptKey->aiAlgid == CALG_RSA_SIGN) {
2006 SetLastError(NTE_BAD_KEY);
2010 *pdwDataLen = pCryptKey->dwBlockLen;
2013 if (dwBufLen < pCryptKey->dwBlockLen) {
2014 SetLastError(ERROR_MORE_DATA);
2017 if (!pad_data(pbData, *pdwDataLen, pbData, pCryptKey->dwBlockLen, dwFlags)) return FALSE;
2018 encrypt_block_impl(pCryptKey->aiAlgid, PK_PUBLIC, &pCryptKey->context, pbData, pbData, RSAENH_ENCRYPT);
2019 *pdwDataLen = pCryptKey->dwBlockLen;
2022 SetLastError(NTE_BAD_TYPE);
2026 if (Final) setup_key(pCryptKey);
2031 /******************************************************************************
2032 * CPDecrypt (RSAENH.@)
2037 * hProv [I] The key container hKey and hHash belong to.
2038 * hKey [I] The key used to decrypt the data.
2039 * hHash [I] An optional hash object for parallel hashing. See notes.
2040 * Final [I] Indicates if this is the last block of data to decrypt.
2041 * dwFlags [I] Currently no flags defined. Must be zero.
2042 * pbData [I/O] Pointer to the data to decrypt. Plaintext will also be stored there.
2043 * pdwDataLen [I/O] I: Length of ciphertext, O: Length of plaintext.
2050 * If a hash object handle is provided in hHash, it will be updated with the plaintext.
2051 * This is useful for message signatures.
2053 * This function uses the standard WINAPI protocol for querying data of dynamic length.
2055 BOOL WINAPI RSAENH_CPDecrypt(HCRYPTPROV hProv, HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final,
2056 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
2058 CRYPTKEY *pCryptKey;
2059 BYTE *in, out[RSAENH_MAX_BLOCK_SIZE], o[RSAENH_MAX_BLOCK_SIZE];
2063 TRACE("(hProv=%08lx, hKey=%08lx, hHash=%08lx, Final=%d, dwFlags=%08x, pbData=%p, "
2064 "pdwDataLen=%p)\n", hProv, hKey, hHash, Final, dwFlags, pbData, pdwDataLen);
2066 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
2068 SetLastError(NTE_BAD_UID);
2074 SetLastError(NTE_BAD_FLAGS);
2078 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
2080 SetLastError(NTE_BAD_KEY);
2084 if (pCryptKey->dwState == RSAENH_KEYSTATE_IDLE)
2085 pCryptKey->dwState = RSAENH_KEYSTATE_DECRYPTING;
2087 if (pCryptKey->dwState != RSAENH_KEYSTATE_DECRYPTING)
2089 SetLastError(NTE_BAD_DATA);
2095 if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_BLOCK) {
2096 for (i=0, in=pbData; i<*pdwDataLen; i+=pCryptKey->dwBlockLen, in+=pCryptKey->dwBlockLen) {
2097 switch (pCryptKey->dwMode) {
2098 case CRYPT_MODE_ECB:
2099 encrypt_block_impl(pCryptKey->aiAlgid, 0, &pCryptKey->context, in, out,
2103 case CRYPT_MODE_CBC:
2104 encrypt_block_impl(pCryptKey->aiAlgid, 0, &pCryptKey->context, in, out,
2106 for (j=0; j<pCryptKey->dwBlockLen; j++) out[j] ^= pCryptKey->abChainVector[j];
2107 memcpy(pCryptKey->abChainVector, in, pCryptKey->dwBlockLen);
2110 case CRYPT_MODE_CFB:
2111 for (j=0; j<pCryptKey->dwBlockLen; j++) {
2112 encrypt_block_impl(pCryptKey->aiAlgid, 0, &pCryptKey->context,
2113 pCryptKey->abChainVector, o, RSAENH_ENCRYPT);
2114 out[j] = in[j] ^ o[0];
2115 for (k=0; k<pCryptKey->dwBlockLen-1; k++)
2116 pCryptKey->abChainVector[k] = pCryptKey->abChainVector[k+1];
2117 pCryptKey->abChainVector[k] = in[j];
2122 SetLastError(NTE_BAD_ALGID);
2125 memcpy(in, out, pCryptKey->dwBlockLen);
2128 if (pbData[*pdwDataLen-1] &&
2129 pbData[*pdwDataLen-1] <= pCryptKey->dwBlockLen &&
2130 pbData[*pdwDataLen-1] < *pdwDataLen) {
2131 BOOL padOkay = TRUE;
2133 /* check that every bad byte has the same value */
2134 for (i = 1; padOkay && i < pbData[*pdwDataLen-1]; i++)
2135 if (pbData[*pdwDataLen - i - 1] != pbData[*pdwDataLen - 1])
2138 *pdwDataLen -= pbData[*pdwDataLen-1];
2140 SetLastError(NTE_BAD_DATA);
2145 SetLastError(NTE_BAD_DATA);
2150 } else if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_STREAM) {
2151 encrypt_stream_impl(pCryptKey->aiAlgid, &pCryptKey->context, pbData, *pdwDataLen);
2152 } else if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_RSA) {
2153 if (pCryptKey->aiAlgid == CALG_RSA_SIGN) {
2154 SetLastError(NTE_BAD_KEY);
2157 encrypt_block_impl(pCryptKey->aiAlgid, PK_PRIVATE, &pCryptKey->context, pbData, pbData, RSAENH_DECRYPT);
2158 if (!unpad_data(pbData, pCryptKey->dwBlockLen, pbData, pdwDataLen, dwFlags)) return FALSE;
2161 SetLastError(NTE_BAD_TYPE);
2165 if (Final) setup_key(pCryptKey);
2167 if (is_valid_handle(&handle_table, hHash, RSAENH_MAGIC_HASH)) {
2168 if (*pdwDataLen>dwMax ||
2169 !RSAENH_CPHashData(hProv, hHash, pbData, *pdwDataLen, 0)) return FALSE;
2175 /******************************************************************************
2176 * CPExportKey (RSAENH.@)
2178 * Export a key into a binary large object (BLOB).
2181 * hProv [I] Key container from which a key is to be exported.
2182 * hKey [I] Key to be exported.
2183 * hPubKey [I] Key used to encrypt sensitive BLOB data.
2184 * dwBlobType [I] SIMPLEBLOB, PUBLICKEYBLOB or PRIVATEKEYBLOB.
2185 * dwFlags [I] Currently none defined.
2186 * pbData [O] Pointer to a buffer where the BLOB will be written to.
2187 * pdwDataLen [I/O] I: Size of buffer at pbData, O: Size of BLOB
2193 BOOL WINAPI RSAENH_CPExportKey(HCRYPTPROV hProv, HCRYPTKEY hKey, HCRYPTKEY hPubKey,
2194 DWORD dwBlobType, DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
2196 CRYPTKEY *pCryptKey, *pPubKey;
2197 BLOBHEADER *pBlobHeader = (BLOBHEADER*)pbData;
2198 RSAPUBKEY *pRSAPubKey = (RSAPUBKEY*)(pBlobHeader+1);
2199 ALG_ID *pAlgid = (ALG_ID*)(pBlobHeader+1);
2202 TRACE("(hProv=%08lx, hKey=%08lx, hPubKey=%08lx, dwBlobType=%08x, dwFlags=%08x, pbData=%p,"
2203 "pdwDataLen=%p)\n", hProv, hKey, hPubKey, dwBlobType, dwFlags, pbData, pdwDataLen);
2205 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
2207 SetLastError(NTE_BAD_UID);
2211 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
2213 SetLastError(NTE_BAD_KEY);
2217 if (dwFlags & CRYPT_SSL2_FALLBACK) {
2218 if (pCryptKey->aiAlgid != CALG_SSL2_MASTER) {
2219 SetLastError(NTE_BAD_KEY);
2224 switch ((BYTE)dwBlobType)
2227 if (!lookup_handle(&handle_table, hPubKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pPubKey)){
2228 SetLastError(NTE_BAD_PUBLIC_KEY); /* FIXME: error_code? */
2232 if (!(GET_ALG_CLASS(pCryptKey->aiAlgid)&(ALG_CLASS_DATA_ENCRYPT|ALG_CLASS_MSG_ENCRYPT))) {
2233 SetLastError(NTE_BAD_KEY); /* FIXME: error code? */
2237 dwDataLen = sizeof(BLOBHEADER) + sizeof(ALG_ID) + pPubKey->dwBlockLen;
2239 if (*pdwDataLen < dwDataLen) {
2240 SetLastError(ERROR_MORE_DATA);
2241 *pdwDataLen = dwDataLen;
2245 pBlobHeader->bType = SIMPLEBLOB;
2246 pBlobHeader->bVersion = CUR_BLOB_VERSION;
2247 pBlobHeader->reserved = 0;
2248 pBlobHeader->aiKeyAlg = pCryptKey->aiAlgid;
2250 *pAlgid = pPubKey->aiAlgid;
2252 if (!pad_data(pCryptKey->abKeyValue, pCryptKey->dwKeyLen, (BYTE*)(pAlgid+1),
2253 pPubKey->dwBlockLen, dwFlags))
2258 encrypt_block_impl(pPubKey->aiAlgid, PK_PUBLIC, &pPubKey->context, (BYTE*)(pAlgid+1),
2259 (BYTE*)(pAlgid+1), RSAENH_ENCRYPT);
2261 *pdwDataLen = dwDataLen;
2265 if (is_valid_handle(&handle_table, hPubKey, RSAENH_MAGIC_KEY)) {
2266 SetLastError(NTE_BAD_KEY); /* FIXME: error code? */
2270 if ((pCryptKey->aiAlgid != CALG_RSA_KEYX) && (pCryptKey->aiAlgid != CALG_RSA_SIGN)) {
2271 SetLastError(NTE_BAD_KEY);
2275 dwDataLen = sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) + pCryptKey->dwKeyLen;
2277 if (*pdwDataLen < dwDataLen) {
2278 SetLastError(ERROR_MORE_DATA);
2279 *pdwDataLen = dwDataLen;
2283 pBlobHeader->bType = PUBLICKEYBLOB;
2284 pBlobHeader->bVersion = CUR_BLOB_VERSION;
2285 pBlobHeader->reserved = 0;
2286 pBlobHeader->aiKeyAlg = pCryptKey->aiAlgid;
2288 pRSAPubKey->magic = RSAENH_MAGIC_RSA1;
2289 pRSAPubKey->bitlen = pCryptKey->dwKeyLen << 3;
2291 export_public_key_impl((BYTE*)(pRSAPubKey+1), &pCryptKey->context,
2292 pCryptKey->dwKeyLen, &pRSAPubKey->pubexp);
2294 *pdwDataLen = dwDataLen;
2297 case PRIVATEKEYBLOB:
2298 if ((pCryptKey->aiAlgid != CALG_RSA_KEYX) && (pCryptKey->aiAlgid != CALG_RSA_SIGN)) {
2299 SetLastError(NTE_BAD_KEY);
2303 dwDataLen = sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) +
2304 2 * pCryptKey->dwKeyLen + 5 * ((pCryptKey->dwKeyLen + 1) >> 1);
2306 if (*pdwDataLen < dwDataLen) {
2307 SetLastError(ERROR_MORE_DATA);
2308 *pdwDataLen = dwDataLen;
2312 pBlobHeader->bType = PRIVATEKEYBLOB;
2313 pBlobHeader->bVersion = CUR_BLOB_VERSION;
2314 pBlobHeader->reserved = 0;
2315 pBlobHeader->aiKeyAlg = pCryptKey->aiAlgid;
2317 pRSAPubKey->magic = RSAENH_MAGIC_RSA2;
2318 pRSAPubKey->bitlen = pCryptKey->dwKeyLen << 3;
2320 export_private_key_impl((BYTE*)(pRSAPubKey+1), &pCryptKey->context,
2321 pCryptKey->dwKeyLen, &pRSAPubKey->pubexp);
2323 *pdwDataLen = dwDataLen;
2327 SetLastError(NTE_BAD_TYPE); /* FIXME: error code? */
2332 /******************************************************************************
2333 * CPImportKey (RSAENH.@)
2335 * Import a BLOB'ed key into a key container.
2338 * hProv [I] Key container into which the key is to be imported.
2339 * pbData [I] Pointer to a buffer which holds the BLOB.
2340 * dwDataLen [I] Length of data in buffer at pbData.
2341 * hPubKey [I] Key used to decrypt sensitive BLOB data.
2342 * dwFlags [I] Currently none defined.
2343 * phKey [O] Handle to the imported key.
2349 BOOL WINAPI RSAENH_CPImportKey(HCRYPTPROV hProv, CONST BYTE *pbData, DWORD dwDataLen,
2350 HCRYPTKEY hPubKey, DWORD dwFlags, HCRYPTKEY *phKey)
2352 KEYCONTAINER *pKeyContainer;
2353 CRYPTKEY *pCryptKey, *pPubKey;
2354 CONST BLOBHEADER *pBlobHeader = (CONST BLOBHEADER*)pbData;
2355 CONST RSAPUBKEY *pRSAPubKey = (CONST RSAPUBKEY*)(pBlobHeader+1);
2356 CONST ALG_ID *pAlgid = (CONST ALG_ID*)(pBlobHeader+1);
2357 CONST BYTE *pbKeyStream = (CONST BYTE*)(pAlgid + 1);
2363 TRACE("(hProv=%08lx, pbData=%p, dwDataLen=%d, hPubKey=%08lx, dwFlags=%08x, phKey=%p)\n",
2364 hProv, pbData, dwDataLen, hPubKey, dwFlags, phKey);
2366 if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER,
2367 (OBJECTHDR**)&pKeyContainer))
2369 SetLastError(NTE_BAD_UID);
2373 if (dwDataLen < sizeof(BLOBHEADER) ||
2374 pBlobHeader->bVersion != CUR_BLOB_VERSION ||
2375 pBlobHeader->reserved != 0)
2377 SetLastError(NTE_BAD_DATA);
2381 switch (pBlobHeader->bType)
2383 case PRIVATEKEYBLOB:
2384 if ((dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY)) ||
2385 (pRSAPubKey->magic != RSAENH_MAGIC_RSA2) ||
2386 (dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) +
2387 (2 * pRSAPubKey->bitlen >> 3) + (5 * ((pRSAPubKey->bitlen+8)>>4))))
2389 SetLastError(NTE_BAD_DATA);
2393 *phKey = new_key(hProv, pBlobHeader->aiKeyAlg, MAKELONG(0,pRSAPubKey->bitlen), &pCryptKey);
2394 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) return FALSE;
2395 setup_key(pCryptKey);
2396 ret = import_private_key_impl((CONST BYTE*)(pRSAPubKey+1), &pCryptKey->context,
2397 pRSAPubKey->bitlen/8, pRSAPubKey->pubexp);
2399 switch (pBlobHeader->aiKeyAlg)
2403 TRACE("installing signing key\n");
2404 RSAENH_CPDestroyKey(hProv, pKeyContainer->hSignatureKeyPair);
2405 copy_handle(&handle_table, *phKey, RSAENH_MAGIC_KEY,
2406 &pKeyContainer->hSignatureKeyPair);
2408 case AT_KEYEXCHANGE:
2410 TRACE("installing key exchange key\n");
2411 RSAENH_CPDestroyKey(hProv, pKeyContainer->hKeyExchangeKeyPair);
2412 copy_handle(&handle_table, *phKey, RSAENH_MAGIC_KEY,
2413 &pKeyContainer->hKeyExchangeKeyPair);
2420 if ((dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY)) ||
2421 (pRSAPubKey->magic != RSAENH_MAGIC_RSA1) ||
2422 (dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) + (pRSAPubKey->bitlen >> 3)))
2424 SetLastError(NTE_BAD_DATA);
2428 /* Since this is a public key blob, only the public key is
2429 * available, so only signature verification is possible.
2431 algID = pBlobHeader->aiKeyAlg;
2432 *phKey = new_key(hProv, algID, MAKELONG(0,pRSAPubKey->bitlen), &pCryptKey);
2433 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) return FALSE;
2434 setup_key(pCryptKey);
2435 ret = import_public_key_impl((CONST BYTE*)(pRSAPubKey+1), &pCryptKey->context,
2436 pRSAPubKey->bitlen >> 3, pRSAPubKey->pubexp);
2438 switch (pBlobHeader->aiKeyAlg)
2440 case AT_KEYEXCHANGE:
2442 TRACE("installing public key\n");
2443 RSAENH_CPDestroyKey(hProv, pKeyContainer->hKeyExchangeKeyPair);
2444 copy_handle(&handle_table, *phKey, RSAENH_MAGIC_KEY,
2445 &pKeyContainer->hKeyExchangeKeyPair);
2452 if (!lookup_handle(&handle_table, hPubKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pPubKey) ||
2453 pPubKey->aiAlgid != CALG_RSA_KEYX)
2455 SetLastError(NTE_BAD_PUBLIC_KEY); /* FIXME: error code? */
2459 if (dwDataLen < sizeof(BLOBHEADER)+sizeof(ALG_ID)+pPubKey->dwBlockLen)
2461 SetLastError(NTE_BAD_DATA); /* FIXME: error code */
2465 pbDecrypted = HeapAlloc(GetProcessHeap(), 0, pPubKey->dwBlockLen);
2466 if (!pbDecrypted) return FALSE;
2467 encrypt_block_impl(pPubKey->aiAlgid, PK_PRIVATE, &pPubKey->context, pbKeyStream, pbDecrypted,
2470 dwKeyLen = RSAENH_MAX_KEY_SIZE;
2471 if (!unpad_data(pbDecrypted, pPubKey->dwBlockLen, pbDecrypted, &dwKeyLen, dwFlags)) {
2472 HeapFree(GetProcessHeap(), 0, pbDecrypted);
2476 *phKey = new_key(hProv, pBlobHeader->aiKeyAlg, dwKeyLen<<19, &pCryptKey);
2477 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE)
2479 HeapFree(GetProcessHeap(), 0, pbDecrypted);
2482 memcpy(pCryptKey->abKeyValue, pbDecrypted, dwKeyLen);
2483 HeapFree(GetProcessHeap(), 0, pbDecrypted);
2484 setup_key(pCryptKey);
2488 SetLastError(NTE_BAD_TYPE); /* FIXME: error code? */
2493 /******************************************************************************
2494 * CPGenKey (RSAENH.@)
2496 * Generate a key in the key container
2499 * hProv [I] Key container for which a key is to be generated.
2500 * Algid [I] Crypto algorithm identifier for the key to be generated.
2501 * dwFlags [I] Upper 16 bits: Binary length of key. Lower 16 bits: Flags. See Notes
2502 * phKey [O] Handle to the generated key.
2509 * Flags currently not considered.
2512 * Private key-exchange- and signature-keys can be generated with Algid AT_KEYEXCHANGE
2513 * and AT_SIGNATURE values.
2515 BOOL WINAPI RSAENH_CPGenKey(HCRYPTPROV hProv, ALG_ID Algid, DWORD dwFlags, HCRYPTKEY *phKey)
2517 KEYCONTAINER *pKeyContainer;
2518 CRYPTKEY *pCryptKey;
2520 TRACE("(hProv=%08lx, aiAlgid=%d, dwFlags=%08x, phKey=%p)\n", hProv, Algid, dwFlags, phKey);
2522 if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER,
2523 (OBJECTHDR**)&pKeyContainer))
2525 /* MSDN: hProv not containing valid context handle */
2526 SetLastError(NTE_BAD_UID);
2534 *phKey = new_key(hProv, CALG_RSA_SIGN, dwFlags, &pCryptKey);
2536 new_key_impl(pCryptKey->aiAlgid, &pCryptKey->context, pCryptKey->dwKeyLen);
2537 setup_key(pCryptKey);
2538 if (Algid == AT_SIGNATURE) {
2539 RSAENH_CPDestroyKey(hProv, pKeyContainer->hSignatureKeyPair);
2540 copy_handle(&handle_table, *phKey, RSAENH_MAGIC_KEY,
2541 &pKeyContainer->hSignatureKeyPair);
2546 case AT_KEYEXCHANGE:
2548 *phKey = new_key(hProv, CALG_RSA_KEYX, dwFlags, &pCryptKey);
2550 new_key_impl(pCryptKey->aiAlgid, &pCryptKey->context, pCryptKey->dwKeyLen);
2551 setup_key(pCryptKey);
2552 if (Algid == AT_KEYEXCHANGE) {
2553 RSAENH_CPDestroyKey(hProv, pKeyContainer->hKeyExchangeKeyPair);
2554 copy_handle(&handle_table, *phKey, RSAENH_MAGIC_KEY,
2555 &pKeyContainer->hKeyExchangeKeyPair);
2565 case CALG_PCT1_MASTER:
2566 case CALG_SSL2_MASTER:
2567 case CALG_SSL3_MASTER:
2568 case CALG_TLS1_MASTER:
2569 *phKey = new_key(hProv, Algid, dwFlags, &pCryptKey);
2571 gen_rand_impl(pCryptKey->abKeyValue, RSAENH_MAX_KEY_SIZE);
2573 case CALG_SSL3_MASTER:
2574 pCryptKey->abKeyValue[0] = RSAENH_SSL3_VERSION_MAJOR;
2575 pCryptKey->abKeyValue[1] = RSAENH_SSL3_VERSION_MINOR;
2578 case CALG_TLS1_MASTER:
2579 pCryptKey->abKeyValue[0] = RSAENH_TLS1_VERSION_MAJOR;
2580 pCryptKey->abKeyValue[1] = RSAENH_TLS1_VERSION_MINOR;
2583 setup_key(pCryptKey);
2588 /* MSDN: Algorithm not supported specified by Algid */
2589 SetLastError(NTE_BAD_ALGID);
2593 return *phKey != (HCRYPTKEY)INVALID_HANDLE_VALUE;
2596 /******************************************************************************
2597 * CPGenRandom (RSAENH.@)
2599 * Generate a random byte stream.
2602 * hProv [I] Key container that is used to generate random bytes.
2603 * dwLen [I] Specifies the number of requested random data bytes.
2604 * pbBuffer [O] Random bytes will be stored here.
2610 BOOL WINAPI RSAENH_CPGenRandom(HCRYPTPROV hProv, DWORD dwLen, BYTE *pbBuffer)
2612 TRACE("(hProv=%08lx, dwLen=%d, pbBuffer=%p)\n", hProv, dwLen, pbBuffer);
2614 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
2616 /* MSDN: hProv not containing valid context handle */
2617 SetLastError(NTE_BAD_UID);
2621 return gen_rand_impl(pbBuffer, dwLen);
2624 /******************************************************************************
2625 * CPGetHashParam (RSAENH.@)
2627 * Query parameters of an hash object.
2630 * hProv [I] The kea container, which the hash belongs to.
2631 * hHash [I] The hash object that is to be queried.
2632 * dwParam [I] Specifies the parameter that is to be queried.
2633 * pbData [I] Pointer to the buffer where the parameter value will be stored.
2634 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
2635 * dwFlags [I] None currently defined.
2642 * Valid dwParams are: HP_ALGID, HP_HASHSIZE, HP_HASHVALUE. The hash will be
2643 * finalized if HP_HASHVALUE is queried.
2645 BOOL WINAPI RSAENH_CPGetHashParam(HCRYPTPROV hProv, HCRYPTHASH hHash, DWORD dwParam, BYTE *pbData,
2646 DWORD *pdwDataLen, DWORD dwFlags)
2648 CRYPTHASH *pCryptHash;
2650 TRACE("(hProv=%08lx, hHash=%08lx, dwParam=%08x, pbData=%p, pdwDataLen=%p, dwFlags=%08x)\n",
2651 hProv, hHash, dwParam, pbData, pdwDataLen, dwFlags);
2653 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
2655 SetLastError(NTE_BAD_UID);
2661 SetLastError(NTE_BAD_FLAGS);
2665 if (!lookup_handle(&handle_table, hHash, RSAENH_MAGIC_HASH,
2666 (OBJECTHDR**)&pCryptHash))
2668 SetLastError(NTE_BAD_HASH);
2674 SetLastError(ERROR_INVALID_PARAMETER);
2681 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptHash->aiAlgid,
2685 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptHash->dwHashSize,
2689 if (pCryptHash->aiAlgid == CALG_TLS1PRF) {
2690 return tls1_prf(hProv, pCryptHash->hKey, &pCryptHash->tpPRFParams.blobLabel,
2691 &pCryptHash->tpPRFParams.blobSeed, pbData, *pdwDataLen);
2694 if ( pbData == NULL ) {
2695 *pdwDataLen = pCryptHash->dwHashSize;
2699 if (pbData && (pCryptHash->dwState != RSAENH_HASHSTATE_FINISHED))
2701 finalize_hash(pCryptHash);
2702 pCryptHash->dwState = RSAENH_HASHSTATE_FINISHED;
2705 return copy_param(pbData, pdwDataLen, (CONST BYTE*)pCryptHash->abHashValue,
2706 pCryptHash->dwHashSize);
2709 SetLastError(NTE_BAD_TYPE);
2714 /******************************************************************************
2715 * CPSetKeyParam (RSAENH.@)
2717 * Set a parameter of a key object
2720 * hProv [I] The key container to which the key belongs.
2721 * hKey [I] The key for which a parameter is to be set.
2722 * dwParam [I] Parameter type. See Notes.
2723 * pbData [I] Pointer to the parameter value.
2724 * dwFlags [I] Currently none defined.
2731 * Defined dwParam types are:
2732 * - KP_MODE: Values MODE_CBC, MODE_ECB, MODE_CFB.
2733 * - KP_MODE_BITS: Shift width for cipher feedback mode. (Currently ignored by MS CSP's)
2734 * - KP_PERMISSIONS: Or'ed combination of CRYPT_ENCRYPT, CRYPT_DECRYPT,
2735 * CRYPT_EXPORT, CRYPT_READ, CRYPT_WRITE, CRYPT_MAC
2736 * - KP_IV: Initialization vector
2738 BOOL WINAPI RSAENH_CPSetKeyParam(HCRYPTPROV hProv, HCRYPTKEY hKey, DWORD dwParam, BYTE *pbData,
2741 CRYPTKEY *pCryptKey;
2743 TRACE("(hProv=%08lx, hKey=%08lx, dwParam=%08x, pbData=%p, dwFlags=%08x)\n", hProv, hKey,
2744 dwParam, pbData, dwFlags);
2746 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
2748 SetLastError(NTE_BAD_UID);
2753 SetLastError(NTE_BAD_FLAGS);
2757 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
2759 SetLastError(NTE_BAD_KEY);
2765 pCryptKey->dwMode = *(DWORD*)pbData;
2769 pCryptKey->dwModeBits = *(DWORD*)pbData;
2772 case KP_PERMISSIONS:
2773 pCryptKey->dwPermissions = *(DWORD*)pbData;
2777 memcpy(pCryptKey->abInitVector, pbData, pCryptKey->dwBlockLen);
2778 setup_key(pCryptKey);
2781 case KP_EFFECTIVE_KEYLEN:
2782 switch (pCryptKey->aiAlgid) {
2786 SetLastError(ERROR_INVALID_PARAMETER);
2789 else if (!*(DWORD *)pbData || *(DWORD *)pbData > 1024)
2791 SetLastError(NTE_BAD_DATA);
2796 pCryptKey->dwEffectiveKeyLen = *(DWORD *)pbData;
2797 setup_key(pCryptKey);
2801 SetLastError(NTE_BAD_TYPE);
2806 case KP_SCHANNEL_ALG:
2807 switch (((PSCHANNEL_ALG)pbData)->dwUse) {
2808 case SCHANNEL_ENC_KEY:
2809 memcpy(&pCryptKey->siSChannelInfo.saEncAlg, pbData, sizeof(SCHANNEL_ALG));
2812 case SCHANNEL_MAC_KEY:
2813 memcpy(&pCryptKey->siSChannelInfo.saMACAlg, pbData, sizeof(SCHANNEL_ALG));
2817 SetLastError(NTE_FAIL); /* FIXME: error code */
2822 case KP_CLIENT_RANDOM:
2823 return copy_data_blob(&pCryptKey->siSChannelInfo.blobClientRandom, (PCRYPT_DATA_BLOB)pbData);
2825 case KP_SERVER_RANDOM:
2826 return copy_data_blob(&pCryptKey->siSChannelInfo.blobServerRandom, (PCRYPT_DATA_BLOB)pbData);
2829 SetLastError(NTE_BAD_TYPE);
2834 /******************************************************************************
2835 * CPGetKeyParam (RSAENH.@)
2837 * Query a key parameter.
2840 * hProv [I] The key container, which the key belongs to.
2841 * hHash [I] The key object that is to be queried.
2842 * dwParam [I] Specifies the parameter that is to be queried.
2843 * pbData [I] Pointer to the buffer where the parameter value will be stored.
2844 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
2845 * dwFlags [I] None currently defined.
2852 * Defined dwParam types are:
2853 * - KP_MODE: Values MODE_CBC, MODE_ECB, MODE_CFB.
2854 * - KP_MODE_BITS: Shift width for cipher feedback mode.
2855 * (Currently ignored by MS CSP's - always eight)
2856 * - KP_PERMISSIONS: Or'ed combination of CRYPT_ENCRYPT, CRYPT_DECRYPT,
2857 * CRYPT_EXPORT, CRYPT_READ, CRYPT_WRITE, CRYPT_MAC
2858 * - KP_IV: Initialization vector.
2859 * - KP_KEYLEN: Bitwidth of the key.
2860 * - KP_BLOCKLEN: Size of a block cipher block.
2861 * - KP_SALT: Salt value.
2863 BOOL WINAPI RSAENH_CPGetKeyParam(HCRYPTPROV hProv, HCRYPTKEY hKey, DWORD dwParam, BYTE *pbData,
2864 DWORD *pdwDataLen, DWORD dwFlags)
2866 CRYPTKEY *pCryptKey;
2869 TRACE("(hProv=%08lx, hKey=%08lx, dwParam=%08x, pbData=%p, pdwDataLen=%p dwFlags=%08x)\n",
2870 hProv, hKey, dwParam, pbData, pdwDataLen, dwFlags);
2872 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
2874 SetLastError(NTE_BAD_UID);
2879 SetLastError(NTE_BAD_FLAGS);
2883 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
2885 SetLastError(NTE_BAD_KEY);
2892 return copy_param(pbData, pdwDataLen, (CONST BYTE*)pCryptKey->abInitVector,
2893 pCryptKey->dwBlockLen);
2896 return copy_param(pbData, pdwDataLen,
2897 (CONST BYTE*)&pCryptKey->abKeyValue[pCryptKey->dwKeyLen], pCryptKey->dwSaltLen);
2900 dwBitLen = pCryptKey->dwKeyLen << 3;
2901 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwBitLen, sizeof(DWORD));
2903 case KP_EFFECTIVE_KEYLEN:
2904 if (pCryptKey->dwEffectiveKeyLen)
2905 dwBitLen = pCryptKey->dwEffectiveKeyLen;
2907 dwBitLen = pCryptKey->dwKeyLen << 3;
2908 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwBitLen, sizeof(DWORD));
2911 dwBitLen = pCryptKey->dwBlockLen << 3;
2912 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwBitLen, sizeof(DWORD));
2915 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptKey->dwMode, sizeof(DWORD));
2918 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptKey->dwModeBits,
2921 case KP_PERMISSIONS:
2922 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptKey->dwPermissions,
2926 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptKey->aiAlgid, sizeof(DWORD));
2929 SetLastError(NTE_BAD_TYPE);
2934 /******************************************************************************
2935 * CPGetProvParam (RSAENH.@)
2937 * Query a CSP parameter.
2940 * hProv [I] The key container that is to be queried.
2941 * dwParam [I] Specifies the parameter that is to be queried.
2942 * pbData [I] Pointer to the buffer where the parameter value will be stored.
2943 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
2944 * dwFlags [I] CRYPT_FIRST: Start enumeration (for PP_ENUMALGS{_EX}).
2950 * Defined dwParam types:
2951 * - PP_CONTAINER: Name of the key container.
2952 * - PP_NAME: Name of the cryptographic service provider.
2953 * - PP_SIG_KEYSIZE_INC: RSA signature keywidth granularity in bits.
2954 * - PP_KEYX_KEYSIZE_INC: RSA key-exchange keywidth granularity in bits.
2955 * - PP_ENUMALGS{_EX}: Query provider capabilities.
2957 BOOL WINAPI RSAENH_CPGetProvParam(HCRYPTPROV hProv, DWORD dwParam, BYTE *pbData,
2958 DWORD *pdwDataLen, DWORD dwFlags)
2960 KEYCONTAINER *pKeyContainer;
2961 PROV_ENUMALGS provEnumalgs;
2965 /* This is for dwParam 41, which does not seem to be documented
2966 * on MSDN. IE6 SP1 asks for it in the 'About' dialog, however.
2967 * Returning this BLOB seems to satisfy IE. The marked 0x00 seem
2968 * to be 'don't care's. If you know anything more specific about
2969 * provider parameter 41, please report to wine-devel@winehq.org */
2970 static CONST BYTE abWTF[96] = {
2971 0xb0, 0x25, 0x63, 0x86, 0x9c, 0xab, 0xb6, 0x37,
2972 0xe8, 0x82, /**/0x00,/**/ 0x72, 0x06, 0xb2, /**/0x00,/**/ 0x3b,
2973 0x60, 0x35, /**/0x00,/**/ 0x3b, 0x88, 0xce, /**/0x00,/**/ 0x82,
2974 0xbc, 0x7a, /**/0x00,/**/ 0xb7, 0x4f, 0x7e, /**/0x00,/**/ 0xde,
2975 0x92, 0xf1, /**/0x00,/**/ 0x83, 0xea, 0x5e, /**/0x00,/**/ 0xc8,
2976 0x12, 0x1e, 0xd4, 0x06, 0xf7, 0x66, /**/0x00,/**/ 0x01,
2977 0x29, 0xa4, /**/0x00,/**/ 0xf8, 0x24, 0x0c, /**/0x00,/**/ 0x33,
2978 0x06, 0x80, /**/0x00,/**/ 0x02, 0x46, 0x0b, /**/0x00,/**/ 0x6d,
2979 0x5b, 0xca, /**/0x00,/**/ 0x9a, 0x10, 0xf0, /**/0x00,/**/ 0x05,
2980 0x19, 0xd0, /**/0x00,/**/ 0x2c, 0xf6, 0x27, /**/0x00,/**/ 0xaa,
2981 0x7c, 0x6f, /**/0x00,/**/ 0xb9, 0xd8, 0x72, /**/0x00,/**/ 0x03,
2982 0xf3, 0x81, /**/0x00,/**/ 0xfa, 0xe8, 0x26, /**/0x00,/**/ 0xca
2985 TRACE("(hProv=%08lx, dwParam=%08x, pbData=%p, pdwDataLen=%p, dwFlags=%08x)\n",
2986 hProv, dwParam, pbData, pdwDataLen, dwFlags);
2989 SetLastError(ERROR_INVALID_PARAMETER);
2993 if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER,
2994 (OBJECTHDR**)&pKeyContainer))
2996 /* MSDN: hProv not containing valid context handle */
2997 SetLastError(NTE_BAD_UID);
3004 case PP_UNIQUE_CONTAINER:/* MSDN says we can return the same value as PP_CONTAINER */
3005 return copy_param(pbData, pdwDataLen, (CONST BYTE*)pKeyContainer->szName,
3006 strlen(pKeyContainer->szName)+1);
3009 return copy_param(pbData, pdwDataLen, (CONST BYTE*)pKeyContainer->szProvName,
3010 strlen(pKeyContainer->szProvName)+1);
3013 dwTemp = PROV_RSA_FULL;
3014 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwTemp, sizeof(dwTemp));
3017 dwTemp = AT_SIGNATURE | AT_KEYEXCHANGE;
3018 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwTemp, sizeof(dwTemp));
3020 case PP_KEYSET_TYPE:
3021 dwTemp = pKeyContainer->dwFlags & CRYPT_MACHINE_KEYSET;
3022 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwTemp, sizeof(dwTemp));
3025 dwTemp = CRYPT_SEC_DESCR;
3026 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwTemp, sizeof(dwTemp));
3028 case PP_SIG_KEYSIZE_INC:
3029 case PP_KEYX_KEYSIZE_INC:
3031 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwTemp, sizeof(dwTemp));
3034 dwTemp = CRYPT_IMPL_SOFTWARE;
3035 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwTemp, sizeof(dwTemp));
3038 dwTemp = 0x00000200;
3039 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwTemp, sizeof(dwTemp));
3041 case PP_ENUMCONTAINERS:
3042 if ((dwFlags & CRYPT_FIRST) == CRYPT_FIRST) pKeyContainer->dwEnumContainersCtr = 0;
3045 *pdwDataLen = (DWORD)MAX_PATH + 1;
3049 if (!open_container_key("", dwFlags, &hKey))
3051 SetLastError(ERROR_NO_MORE_ITEMS);
3055 dwTemp = *pdwDataLen;
3056 switch (RegEnumKeyExA(hKey, pKeyContainer->dwEnumContainersCtr, (LPSTR)pbData, &dwTemp,
3057 NULL, NULL, NULL, NULL))
3059 case ERROR_MORE_DATA:
3060 *pdwDataLen = (DWORD)MAX_PATH + 1;
3063 pKeyContainer->dwEnumContainersCtr++;
3067 case ERROR_NO_MORE_ITEMS:
3069 SetLastError(ERROR_NO_MORE_ITEMS);
3075 case PP_ENUMALGS_EX:
3076 if (((pKeyContainer->dwEnumAlgsCtr >= RSAENH_MAX_ENUMALGS-1) ||
3077 (!aProvEnumAlgsEx[pKeyContainer->dwPersonality]
3078 [pKeyContainer->dwEnumAlgsCtr+1].aiAlgid)) &&
3079 ((dwFlags & CRYPT_FIRST) != CRYPT_FIRST))
3081 SetLastError(ERROR_NO_MORE_ITEMS);
3085 if (dwParam == PP_ENUMALGS) {
3086 if (pbData && (*pdwDataLen >= sizeof(PROV_ENUMALGS)))
3087 pKeyContainer->dwEnumAlgsCtr = ((dwFlags & CRYPT_FIRST) == CRYPT_FIRST) ?
3088 0 : pKeyContainer->dwEnumAlgsCtr+1;
3090 provEnumalgs.aiAlgid = aProvEnumAlgsEx
3091 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr].aiAlgid;
3092 provEnumalgs.dwBitLen = aProvEnumAlgsEx
3093 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr].dwDefaultLen;
3094 provEnumalgs.dwNameLen = aProvEnumAlgsEx
3095 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr].dwNameLen;
3096 memcpy(provEnumalgs.szName, aProvEnumAlgsEx
3097 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr].szName,
3100 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&provEnumalgs,
3101 sizeof(PROV_ENUMALGS));
3103 if (pbData && (*pdwDataLen >= sizeof(PROV_ENUMALGS_EX)))
3104 pKeyContainer->dwEnumAlgsCtr = ((dwFlags & CRYPT_FIRST) == CRYPT_FIRST) ?
3105 0 : pKeyContainer->dwEnumAlgsCtr+1;
3107 return copy_param(pbData, pdwDataLen,
3108 (CONST BYTE*)&aProvEnumAlgsEx
3109 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr],
3110 sizeof(PROV_ENUMALGS_EX));
3113 case 41: /* Undocumented. Asked for by IE About dialog */
3114 return copy_param(pbData, pdwDataLen, abWTF, sizeof(abWTF));
3117 /* MSDN: Unknown parameter number in dwParam */
3118 SetLastError(NTE_BAD_TYPE);
3123 /******************************************************************************
3124 * CPDeriveKey (RSAENH.@)
3126 * Derives a key from a hash value.
3129 * hProv [I] Key container for which a key is to be generated.
3130 * Algid [I] Crypto algorithm identifier for the key to be generated.
3131 * hBaseData [I] Hash from whose value the key will be derived.
3132 * dwFlags [I] See Notes.
3133 * phKey [O] The generated key.
3141 * - CRYPT_EXPORTABLE: Key can be exported.
3142 * - CRYPT_NO_SALT: No salt is used for 40 bit keys.
3143 * - CRYPT_CREATE_SALT: Use remaining bits as salt value.
3145 BOOL WINAPI RSAENH_CPDeriveKey(HCRYPTPROV hProv, ALG_ID Algid, HCRYPTHASH hBaseData,
3146 DWORD dwFlags, HCRYPTKEY *phKey)
3148 CRYPTKEY *pCryptKey, *pMasterKey;
3149 CRYPTHASH *pCryptHash;
3150 BYTE abHashValue[RSAENH_MAX_HASH_SIZE*2];
3153 TRACE("(hProv=%08lx, Algid=%d, hBaseData=%08lx, dwFlags=%08x phKey=%p)\n", hProv, Algid,
3154 hBaseData, dwFlags, phKey);
3156 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
3158 SetLastError(NTE_BAD_UID);
3162 if (!lookup_handle(&handle_table, hBaseData, RSAENH_MAGIC_HASH,
3163 (OBJECTHDR**)&pCryptHash))
3165 SetLastError(NTE_BAD_HASH);
3171 SetLastError(ERROR_INVALID_PARAMETER);
3175 switch (GET_ALG_CLASS(Algid))
3177 case ALG_CLASS_DATA_ENCRYPT:
3178 *phKey = new_key(hProv, Algid, dwFlags, &pCryptKey);
3179 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) return FALSE;
3182 * We derive the key material from the hash.
3183 * If the hash value is not large enough for the claimed key, we have to construct
3184 * a larger binary value based on the hash. This is documented in MSDN: CryptDeriveKey.
3186 dwLen = RSAENH_MAX_HASH_SIZE;
3187 RSAENH_CPGetHashParam(pCryptHash->hProv, hBaseData, HP_HASHVAL, abHashValue, &dwLen, 0);
3189 if (dwLen < pCryptKey->dwKeyLen) {
3190 BYTE pad1[RSAENH_HMAC_DEF_PAD_LEN], pad2[RSAENH_HMAC_DEF_PAD_LEN];
3191 BYTE old_hashval[RSAENH_MAX_HASH_SIZE];
3194 memcpy(old_hashval, pCryptHash->abHashValue, RSAENH_MAX_HASH_SIZE);
3196 for (i=0; i<RSAENH_HMAC_DEF_PAD_LEN; i++) {
3197 pad1[i] = RSAENH_HMAC_DEF_IPAD_CHAR ^ (i<dwLen ? abHashValue[i] : 0);
3198 pad2[i] = RSAENH_HMAC_DEF_OPAD_CHAR ^ (i<dwLen ? abHashValue[i] : 0);
3201 init_hash(pCryptHash);
3202 update_hash(pCryptHash, pad1, RSAENH_HMAC_DEF_PAD_LEN);
3203 finalize_hash(pCryptHash);
3204 memcpy(abHashValue, pCryptHash->abHashValue, pCryptHash->dwHashSize);
3206 init_hash(pCryptHash);
3207 update_hash(pCryptHash, pad2, RSAENH_HMAC_DEF_PAD_LEN);
3208 finalize_hash(pCryptHash);
3209 memcpy(abHashValue+pCryptHash->dwHashSize, pCryptHash->abHashValue,
3210 pCryptHash->dwHashSize);
3212 memcpy(pCryptHash->abHashValue, old_hashval, RSAENH_MAX_HASH_SIZE);
3215 memcpy(pCryptKey->abKeyValue, abHashValue,
3216 RSAENH_MIN(pCryptKey->dwKeyLen, sizeof(pCryptKey->abKeyValue)));
3219 case ALG_CLASS_MSG_ENCRYPT:
3220 if (!lookup_handle(&handle_table, pCryptHash->hKey, RSAENH_MAGIC_KEY,
3221 (OBJECTHDR**)&pMasterKey))
3223 SetLastError(NTE_FAIL); /* FIXME error code */
3229 /* See RFC 2246, chapter 6.3 Key calculation */
3230 case CALG_SCHANNEL_ENC_KEY:
3231 *phKey = new_key(hProv, pMasterKey->siSChannelInfo.saEncAlg.Algid,
3232 MAKELONG(LOWORD(dwFlags),pMasterKey->siSChannelInfo.saEncAlg.cBits),
3234 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) return FALSE;
3235 memcpy(pCryptKey->abKeyValue,
3236 pCryptHash->abHashValue + (
3237 2 * (pMasterKey->siSChannelInfo.saMACAlg.cBits / 8) +
3238 ((dwFlags & CRYPT_SERVER) ?
3239 (pMasterKey->siSChannelInfo.saEncAlg.cBits / 8) : 0)),
3240 pMasterKey->siSChannelInfo.saEncAlg.cBits / 8);
3241 memcpy(pCryptKey->abInitVector,
3242 pCryptHash->abHashValue + (
3243 2 * (pMasterKey->siSChannelInfo.saMACAlg.cBits / 8) +
3244 2 * (pMasterKey->siSChannelInfo.saEncAlg.cBits / 8) +
3245 ((dwFlags & CRYPT_SERVER) ? pCryptKey->dwBlockLen : 0)),
3246 pCryptKey->dwBlockLen);
3249 case CALG_SCHANNEL_MAC_KEY:
3250 *phKey = new_key(hProv, Algid,
3251 MAKELONG(LOWORD(dwFlags),pMasterKey->siSChannelInfo.saMACAlg.cBits),
3253 if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE) return FALSE;
3254 memcpy(pCryptKey->abKeyValue,
3255 pCryptHash->abHashValue + ((dwFlags & CRYPT_SERVER) ?
3256 pMasterKey->siSChannelInfo.saMACAlg.cBits / 8 : 0),
3257 pMasterKey->siSChannelInfo.saMACAlg.cBits / 8);
3261 SetLastError(NTE_BAD_ALGID);
3267 SetLastError(NTE_BAD_ALGID);
3271 setup_key(pCryptKey);
3275 /******************************************************************************
3276 * CPGetUserKey (RSAENH.@)
3278 * Returns a handle to the user's private key-exchange- or signature-key.
3281 * hProv [I] The key container from which a user key is requested.
3282 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
3283 * phUserKey [O] Handle to the requested key or INVALID_HANDLE_VALUE in case of failure.
3290 * A newly created key container does not contain private user key. Create them with CPGenKey.
3292 BOOL WINAPI RSAENH_CPGetUserKey(HCRYPTPROV hProv, DWORD dwKeySpec, HCRYPTKEY *phUserKey)
3294 KEYCONTAINER *pKeyContainer;
3296 TRACE("(hProv=%08lx, dwKeySpec=%08x, phUserKey=%p)\n", hProv, dwKeySpec, phUserKey);
3298 if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER,
3299 (OBJECTHDR**)&pKeyContainer))
3301 /* MSDN: hProv not containing valid context handle */
3302 SetLastError(NTE_BAD_UID);
3308 case AT_KEYEXCHANGE:
3309 copy_handle(&handle_table, pKeyContainer->hKeyExchangeKeyPair, RSAENH_MAGIC_KEY,
3314 copy_handle(&handle_table, pKeyContainer->hSignatureKeyPair, RSAENH_MAGIC_KEY,
3319 *phUserKey = (HCRYPTKEY)INVALID_HANDLE_VALUE;
3322 if (*phUserKey == (HCRYPTKEY)INVALID_HANDLE_VALUE)
3324 /* MSDN: dwKeySpec parameter specifies nonexistent key */
3325 SetLastError(NTE_NO_KEY);
3332 /******************************************************************************
3333 * CPHashData (RSAENH.@)
3335 * Updates a hash object with the given data.
3338 * hProv [I] Key container to which the hash object belongs.
3339 * hHash [I] Hash object which is to be updated.
3340 * pbData [I] Pointer to data with which the hash object is to be updated.
3341 * dwDataLen [I] Length of the data.
3342 * dwFlags [I] Currently none defined.
3349 * The actual hash value is queried with CPGetHashParam, which will finalize
3350 * the hash. Updating a finalized hash will fail with a last error NTE_BAD_HASH_STATE.
3352 BOOL WINAPI RSAENH_CPHashData(HCRYPTPROV hProv, HCRYPTHASH hHash, CONST BYTE *pbData,
3353 DWORD dwDataLen, DWORD dwFlags)
3355 CRYPTHASH *pCryptHash;
3357 TRACE("(hProv=%08lx, hHash=%08lx, pbData=%p, dwDataLen=%d, dwFlags=%08x)\n",
3358 hProv, hHash, pbData, dwDataLen, dwFlags);
3362 SetLastError(NTE_BAD_FLAGS);
3366 if (!lookup_handle(&handle_table, hHash, RSAENH_MAGIC_HASH,
3367 (OBJECTHDR**)&pCryptHash))
3369 SetLastError(NTE_BAD_HASH);
3373 if (!get_algid_info(hProv, pCryptHash->aiAlgid) || pCryptHash->aiAlgid == CALG_SSL3_SHAMD5)
3375 SetLastError(NTE_BAD_ALGID);
3379 if (pCryptHash->dwState != RSAENH_HASHSTATE_HASHING)
3381 SetLastError(NTE_BAD_HASH_STATE);
3385 update_hash(pCryptHash, pbData, dwDataLen);
3389 /******************************************************************************
3390 * CPHashSessionKey (RSAENH.@)
3392 * Updates a hash object with the binary representation of a symmetric key.
3395 * hProv [I] Key container to which the hash object belongs.
3396 * hHash [I] Hash object which is to be updated.
3397 * hKey [I] The symmetric key, whose binary value will be added to the hash.
3398 * dwFlags [I] CRYPT_LITTLE_ENDIAN, if the binary key value shall be interpreted as little endian.
3404 BOOL WINAPI RSAENH_CPHashSessionKey(HCRYPTPROV hProv, HCRYPTHASH hHash, HCRYPTKEY hKey,
3407 BYTE abKeyValue[RSAENH_MAX_KEY_SIZE], bTemp;
3411 TRACE("(hProv=%08lx, hHash=%08lx, hKey=%08lx, dwFlags=%08x)\n", hProv, hHash, hKey, dwFlags);
3413 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pKey) ||
3414 (GET_ALG_CLASS(pKey->aiAlgid) != ALG_CLASS_DATA_ENCRYPT))
3416 SetLastError(NTE_BAD_KEY);
3420 if (dwFlags & ~CRYPT_LITTLE_ENDIAN) {
3421 SetLastError(NTE_BAD_FLAGS);
3425 memcpy(abKeyValue, pKey->abKeyValue, pKey->dwKeyLen);
3426 if (!(dwFlags & CRYPT_LITTLE_ENDIAN)) {
3427 for (i=0; i<pKey->dwKeyLen/2; i++) {
3428 bTemp = abKeyValue[i];
3429 abKeyValue[i] = abKeyValue[pKey->dwKeyLen-i-1];
3430 abKeyValue[pKey->dwKeyLen-i-1] = bTemp;
3434 return RSAENH_CPHashData(hProv, hHash, abKeyValue, pKey->dwKeyLen, 0);
3437 /******************************************************************************
3438 * CPReleaseContext (RSAENH.@)
3440 * Release a key container.
3443 * hProv [I] Key container to be released.
3444 * dwFlags [I] Currently none defined.
3450 BOOL WINAPI RSAENH_CPReleaseContext(HCRYPTPROV hProv, DWORD dwFlags)
3452 TRACE("(hProv=%08lx, dwFlags=%08x)\n", hProv, dwFlags);
3454 if (!release_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
3456 /* MSDN: hProv not containing valid context handle */
3457 SetLastError(NTE_BAD_UID);
3462 SetLastError(NTE_BAD_FLAGS);
3469 /******************************************************************************
3470 * CPSetHashParam (RSAENH.@)
3472 * Set a parameter of a hash object
3475 * hProv [I] The key container to which the key belongs.
3476 * hHash [I] The hash object for which a parameter is to be set.
3477 * dwParam [I] Parameter type. See Notes.
3478 * pbData [I] Pointer to the parameter value.
3479 * dwFlags [I] Currently none defined.
3486 * Currently only the HP_HMAC_INFO dwParam type is defined.
3487 * The HMAC_INFO struct will be deep copied into the hash object.
3488 * See Internet RFC 2104 for details on the HMAC algorithm.
3490 BOOL WINAPI RSAENH_CPSetHashParam(HCRYPTPROV hProv, HCRYPTHASH hHash, DWORD dwParam,
3491 BYTE *pbData, DWORD dwFlags)
3493 CRYPTHASH *pCryptHash;
3494 CRYPTKEY *pCryptKey;
3497 TRACE("(hProv=%08lx, hHash=%08lx, dwParam=%08x, pbData=%p, dwFlags=%08x)\n",
3498 hProv, hHash, dwParam, pbData, dwFlags);
3500 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
3502 SetLastError(NTE_BAD_UID);
3507 SetLastError(NTE_BAD_FLAGS);
3511 if (!lookup_handle(&handle_table, hHash, RSAENH_MAGIC_HASH,
3512 (OBJECTHDR**)&pCryptHash))
3514 SetLastError(NTE_BAD_HASH);
3520 free_hmac_info(pCryptHash->pHMACInfo);
3521 if (!copy_hmac_info(&pCryptHash->pHMACInfo, (PHMAC_INFO)pbData)) return FALSE;
3523 if (!lookup_handle(&handle_table, pCryptHash->hKey, RSAENH_MAGIC_KEY,
3524 (OBJECTHDR**)&pCryptKey))
3526 SetLastError(NTE_FAIL); /* FIXME: correct error code? */
3530 for (i=0; i<RSAENH_MIN(pCryptKey->dwKeyLen,pCryptHash->pHMACInfo->cbInnerString); i++) {
3531 pCryptHash->pHMACInfo->pbInnerString[i] ^= pCryptKey->abKeyValue[i];
3533 for (i=0; i<RSAENH_MIN(pCryptKey->dwKeyLen,pCryptHash->pHMACInfo->cbOuterString); i++) {
3534 pCryptHash->pHMACInfo->pbOuterString[i] ^= pCryptKey->abKeyValue[i];
3537 init_hash(pCryptHash);
3541 memcpy(pCryptHash->abHashValue, pbData, pCryptHash->dwHashSize);
3542 pCryptHash->dwState = RSAENH_HASHSTATE_FINISHED;
3545 case HP_TLS1PRF_SEED:
3546 return copy_data_blob(&pCryptHash->tpPRFParams.blobSeed, (PCRYPT_DATA_BLOB)pbData);
3548 case HP_TLS1PRF_LABEL:
3549 return copy_data_blob(&pCryptHash->tpPRFParams.blobLabel, (PCRYPT_DATA_BLOB)pbData);
3552 SetLastError(NTE_BAD_TYPE);
3557 /******************************************************************************
3558 * CPSetProvParam (RSAENH.@)
3560 BOOL WINAPI RSAENH_CPSetProvParam(HCRYPTPROV hProv, DWORD dwParam, BYTE *pbData, DWORD dwFlags)
3566 /******************************************************************************
3567 * CPSignHash (RSAENH.@)
3569 * Sign a hash object
3572 * hProv [I] The key container, to which the hash object belongs.
3573 * hHash [I] The hash object to be signed.
3574 * dwKeySpec [I] AT_SIGNATURE or AT_KEYEXCHANGE: Key used to generate the signature.
3575 * sDescription [I] Should be NULL for security reasons.
3576 * dwFlags [I] 0, CRYPT_NOHASHOID or CRYPT_X931_FORMAT: Format of the signature.
3577 * pbSignature [O] Buffer, to which the signature will be stored. May be NULL to query SigLen.
3578 * pdwSigLen [I/O] Size of the buffer (in), Length of the signature (out)
3584 BOOL WINAPI RSAENH_CPSignHash(HCRYPTPROV hProv, HCRYPTHASH hHash, DWORD dwKeySpec,
3585 LPCWSTR sDescription, DWORD dwFlags, BYTE *pbSignature,
3588 HCRYPTKEY hCryptKey;
3589 CRYPTKEY *pCryptKey;
3591 BYTE abHashValue[RSAENH_MAX_HASH_SIZE];
3594 TRACE("(hProv=%08lx, hHash=%08lx, dwKeySpec=%08x, sDescription=%s, dwFlags=%08x, "
3595 "pbSignature=%p, pdwSigLen=%p)\n", hProv, hHash, dwKeySpec, debugstr_w(sDescription),
3596 dwFlags, pbSignature, pdwSigLen);
3598 if (dwFlags & ~(CRYPT_NOHASHOID|CRYPT_X931_FORMAT)) {
3599 SetLastError(NTE_BAD_FLAGS);
3603 if (!RSAENH_CPGetUserKey(hProv, dwKeySpec, &hCryptKey)) return FALSE;
3605 if (!lookup_handle(&handle_table, hCryptKey, RSAENH_MAGIC_KEY,
3606 (OBJECTHDR**)&pCryptKey))
3608 SetLastError(NTE_NO_KEY);
3613 *pdwSigLen = pCryptKey->dwKeyLen;
3616 if (pCryptKey->dwKeyLen > *pdwSigLen)
3618 SetLastError(ERROR_MORE_DATA);
3619 *pdwSigLen = pCryptKey->dwKeyLen;
3622 *pdwSigLen = pCryptKey->dwKeyLen;
3625 if (!RSAENH_CPHashData(hProv, hHash, (CONST BYTE*)sDescription,
3626 (DWORD)lstrlenW(sDescription)*sizeof(WCHAR), 0))
3632 dwHashLen = sizeof(DWORD);
3633 if (!RSAENH_CPGetHashParam(hProv, hHash, HP_ALGID, (BYTE*)&aiAlgid, &dwHashLen, 0)) return FALSE;
3635 dwHashLen = RSAENH_MAX_HASH_SIZE;
3636 if (!RSAENH_CPGetHashParam(hProv, hHash, HP_HASHVAL, abHashValue, &dwHashLen, 0)) return FALSE;
3639 if (!build_hash_signature(pbSignature, *pdwSigLen, aiAlgid, abHashValue, dwHashLen, dwFlags)) {
3643 return encrypt_block_impl(pCryptKey->aiAlgid, PK_PRIVATE, &pCryptKey->context, pbSignature, pbSignature, RSAENH_ENCRYPT);
3646 /******************************************************************************
3647 * CPVerifySignature (RSAENH.@)
3649 * Verify the signature of a hash object.
3652 * hProv [I] The key container, to which the hash belongs.
3653 * hHash [I] The hash for which the signature is verified.
3654 * pbSignature [I] The binary signature.
3655 * dwSigLen [I] Length of the signature BLOB.
3656 * hPubKey [I] Public key used to verify the signature.
3657 * sDescription [I] Should be NULL for security reasons.
3658 * dwFlags [I] 0, CRYPT_NOHASHOID or CRYPT_X931_FORMAT: Format of the signature.
3661 * Success: TRUE (Signature is valid)
3662 * Failure: FALSE (GetLastError() == NTE_BAD_SIGNATURE, if signature is invalid)
3664 BOOL WINAPI RSAENH_CPVerifySignature(HCRYPTPROV hProv, HCRYPTHASH hHash, CONST BYTE *pbSignature,
3665 DWORD dwSigLen, HCRYPTKEY hPubKey, LPCWSTR sDescription,
3668 BYTE *pbConstructed = NULL, *pbDecrypted = NULL;
3669 CRYPTKEY *pCryptKey;
3672 BYTE abHashValue[RSAENH_MAX_HASH_SIZE];
3675 TRACE("(hProv=%08lx, hHash=%08lx, pbSignature=%p, dwSigLen=%d, hPubKey=%08lx, sDescription=%s, "
3676 "dwFlags=%08x)\n", hProv, hHash, pbSignature, dwSigLen, hPubKey, debugstr_w(sDescription),
3679 if (dwFlags & ~(CRYPT_NOHASHOID|CRYPT_X931_FORMAT)) {
3680 SetLastError(NTE_BAD_FLAGS);
3684 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
3686 SetLastError(NTE_BAD_UID);
3690 if (!lookup_handle(&handle_table, hPubKey, RSAENH_MAGIC_KEY,
3691 (OBJECTHDR**)&pCryptKey))
3693 SetLastError(NTE_BAD_KEY);
3697 /* in Microsoft implementation, the signature length is checked before
3698 * the signature pointer.
3700 if (dwSigLen != pCryptKey->dwKeyLen)
3702 SetLastError(NTE_BAD_SIGNATURE);
3706 if (!hHash || !pbSignature)
3708 SetLastError(ERROR_INVALID_PARAMETER);
3713 if (!RSAENH_CPHashData(hProv, hHash, (CONST BYTE*)sDescription,
3714 (DWORD)lstrlenW(sDescription)*sizeof(WCHAR), 0))
3720 dwHashLen = sizeof(DWORD);
3721 if (!RSAENH_CPGetHashParam(hProv, hHash, HP_ALGID, (BYTE*)&aiAlgid, &dwHashLen, 0)) return FALSE;
3723 dwHashLen = RSAENH_MAX_HASH_SIZE;
3724 if (!RSAENH_CPGetHashParam(hProv, hHash, HP_HASHVAL, abHashValue, &dwHashLen, 0)) return FALSE;
3726 pbConstructed = HeapAlloc(GetProcessHeap(), 0, dwSigLen);
3727 if (!pbConstructed) {
3728 SetLastError(NTE_NO_MEMORY);
3732 pbDecrypted = HeapAlloc(GetProcessHeap(), 0, dwSigLen);
3734 SetLastError(NTE_NO_MEMORY);
3738 if (!encrypt_block_impl(pCryptKey->aiAlgid, PK_PUBLIC, &pCryptKey->context, pbSignature, pbDecrypted,
3744 if (!build_hash_signature(pbConstructed, dwSigLen, aiAlgid, abHashValue, dwHashLen, dwFlags)) {
3748 if (memcmp(pbDecrypted, pbConstructed, dwSigLen)) {
3749 SetLastError(NTE_BAD_SIGNATURE);
3755 HeapFree(GetProcessHeap(), 0, pbConstructed);
3756 HeapFree(GetProcessHeap(), 0, pbDecrypted);
3760 static const WCHAR szProviderKeys[4][97] = {
3761 { 'S','o','f','t','w','a','r','e','\\',
3762 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
3763 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
3764 'i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ','B','a','s',
3765 'e',' ','C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r',
3766 'o','v','i','d','e','r',' ','v','1','.','0',0 },
3767 { 'S','o','f','t','w','a','r','e','\\',
3768 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
3769 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
3770 'i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ',
3771 'E','n','h','a','n','c','e','d',
3772 ' ','C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r',
3773 'o','v','i','d','e','r',' ','v','1','.','0',0 },
3774 { 'S','o','f','t','w','a','r','e','\\',
3775 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
3776 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
3777 'i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ','S','t','r','o','n','g',
3778 ' ','C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r',
3779 'o','v','i','d','e','r',0 },
3780 { 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
3781 'C','r','y','p','t','o','g','r','a','p','h','y','\\','D','e','f','a','u','l','t','s','\\',
3782 'P','r','o','v','i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ',
3783 'R','S','A',' ','S','C','h','a','n','n','e','l',' ',
3784 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r','o','v','i','d','e','r',0 }
3786 static const WCHAR szDefaultKeys[2][65] = {
3787 { 'S','o','f','t','w','a','r','e','\\',
3788 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
3789 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
3790 'i','d','e','r',' ','T','y','p','e','s','\\','T','y','p','e',' ','0','0','1',0 },
3791 { 'S','o','f','t','w','a','r','e','\\',
3792 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
3793 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
3794 'i','d','e','r',' ','T','y','p','e','s','\\','T','y','p','e',' ','0','1','2',0 }
3798 /******************************************************************************
3799 * DllRegisterServer (RSAENH.@)
3801 * Dll self registration.
3810 * Registers the following keys:
3811 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider\
3812 * Microsoft Base Cryptographic Provider v1.0
3813 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider\
3814 * Microsoft Enhanced Cryptographic Provider
3815 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider\
3816 * Microsoft Strong Cryptographpic Provider
3817 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider Types\Type 001
3819 HRESULT WINAPI DllRegisterServer(void)
3826 for (i=0; i<4; i++) {
3827 apiRet = RegCreateKeyExW(HKEY_LOCAL_MACHINE, szProviderKeys[i], 0, NULL,
3828 REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &key, &dp);
3830 if (apiRet == ERROR_SUCCESS)
3832 if (dp == REG_CREATED_NEW_KEY)
3834 static const WCHAR szImagePath[] = { 'I','m','a','g','e',' ','P','a','t','h',0 };
3835 static const WCHAR szRSABase[] = { 'r','s','a','e','n','h','.','d','l','l',0 };
3836 static const WCHAR szType[] = { 'T','y','p','e',0 };
3837 static const WCHAR szSignature[] = { 'S','i','g','n','a','t','u','r','e',0 };
3838 DWORD type = (i == 3) ? PROV_RSA_SCHANNEL : PROV_RSA_FULL;
3839 DWORD sign = 0xdeadbeef;
3840 RegSetValueExW(key, szImagePath, 0, REG_SZ, (const BYTE *)szRSABase,
3841 (lstrlenW(szRSABase) + 1) * sizeof(WCHAR));
3842 RegSetValueExW(key, szType, 0, REG_DWORD, (LPBYTE)&type, sizeof(type));
3843 RegSetValueExW(key, szSignature, 0, REG_BINARY, (LPBYTE)&sign, sizeof(sign));
3849 for (i=0; i<2; i++) {
3850 apiRet = RegCreateKeyExW(HKEY_LOCAL_MACHINE, szDefaultKeys[i], 0, NULL,
3851 REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &key, &dp);
3852 if (apiRet == ERROR_SUCCESS)
3854 if (dp == REG_CREATED_NEW_KEY)
3856 static const WCHAR szName[] = { 'N','a','m','e',0 };
3857 static const WCHAR szRSAName[2][46] = {
3858 { 'M','i','c','r','o','s','o','f','t',' ', 'B','a','s','e',' ',
3859 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ',
3860 'P','r','o','v','i','d','e','r',' ','v','1','.','0',0 },
3861 { 'M','i','c','r','o','s','o','f','t',' ','R','S','A',' ',
3862 'S','C','h','a','n','n','e','l',' ',
3863 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ',
3864 'P','r','o','v','i','d','e','r',0 } };
3865 static const WCHAR szTypeName[] = { 'T','y','p','e','N','a','m','e',0 };
3866 static const WCHAR szRSATypeName[2][38] = {
3867 { 'R','S','A',' ','F','u','l','l',' ',
3868 '(','S','i','g','n','a','t','u','r','e',' ','a','n','d',' ',
3869 'K','e','y',' ','E','x','c','h','a','n','g','e',')',0 },
3870 { 'R','S','A',' ','S','C','h','a','n','n','e','l',0 } };
3872 RegSetValueExW(key, szName, 0, REG_SZ,
3873 (const BYTE *)szRSAName[i], lstrlenW(szRSAName[i])*sizeof(WCHAR)+sizeof(WCHAR));
3874 RegSetValueExW(key, szTypeName, 0, REG_SZ,
3875 (const BYTE *)szRSATypeName[i], lstrlenW(szRSATypeName[i])*sizeof(WCHAR)+sizeof(WCHAR));
3881 return HRESULT_FROM_WIN32(apiRet);
3884 /******************************************************************************
3885 * DllUnregisterServer (RSAENH.@)
3887 * Dll self unregistration.
3895 * For the relevant keys see DllRegisterServer.
3897 HRESULT WINAPI DllUnregisterServer(void)
3899 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szProviderKeys[0]);
3900 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szProviderKeys[1]);
3901 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szProviderKeys[2]);
3902 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szProviderKeys[3]);
3903 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szDefaultKeys[0]);
3904 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szDefaultKeys[1]);