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"
41 WINE_DEFAULT_DEBUG_CHANNEL(crypt
);
43 /******************************************************************************
44 * CRYPTHASH - hash objects
46 #define RSAENH_MAGIC_HASH 0x85938417u
47 #define RSAENH_MAX_HASH_SIZE 104
48 #define RSAENH_HASHSTATE_IDLE 0
49 #define RSAENH_HASHSTATE_HASHING 1
50 #define RSAENH_HASHSTATE_FINISHED 2
51 typedef struct _RSAENH_TLS1PRF_PARAMS
53 CRYPT_DATA_BLOB blobLabel
;
54 CRYPT_DATA_BLOB blobSeed
;
55 } RSAENH_TLS1PRF_PARAMS
;
57 typedef struct tagCRYPTHASH
66 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
];
68 RSAENH_TLS1PRF_PARAMS tpPRFParams
;
71 /******************************************************************************
72 * CRYPTKEY - key objects
74 #define RSAENH_MAGIC_KEY 0x73620457u
75 #define RSAENH_MAX_KEY_SIZE 48
76 #define RSAENH_MAX_BLOCK_SIZE 24
77 #define RSAENH_KEYSTATE_IDLE 0
78 #define RSAENH_KEYSTATE_ENCRYPTING 1
79 #define RSAENH_KEYSTATE_DECRYPTING 2
80 #define RSAENH_KEYSTATE_MASTERKEY 3
81 typedef struct _RSAENH_SCHANNEL_INFO
83 SCHANNEL_ALG saEncAlg
;
84 SCHANNEL_ALG saMACAlg
;
85 CRYPT_DATA_BLOB blobClientRandom
;
86 CRYPT_DATA_BLOB blobServerRandom
;
87 } RSAENH_SCHANNEL_INFO
;
89 typedef struct tagCRYPTKEY
102 BYTE abKeyValue
[RSAENH_MAX_KEY_SIZE
];
103 BYTE abInitVector
[RSAENH_MAX_BLOCK_SIZE
];
104 BYTE abChainVector
[RSAENH_MAX_BLOCK_SIZE
];
105 RSAENH_SCHANNEL_INFO siSChannelInfo
;
108 /******************************************************************************
109 * KEYCONTAINER - key containers
111 #define RSAENH_PERSONALITY_BASE 0u
112 #define RSAENH_PERSONALITY_STRONG 1u
113 #define RSAENH_PERSONALITY_ENHANCED 2u
114 #define RSAENH_PERSONALITY_SCHANNEL 3u
116 #define RSAENH_MAGIC_CONTAINER 0x26384993u
117 typedef struct tagKEYCONTAINER
123 DWORD dwEnumContainersCtr
;
124 CHAR szName
[MAX_PATH
];
125 CHAR szProvName
[MAX_PATH
];
126 HCRYPTKEY hKeyExchangeKeyPair
;
127 HCRYPTKEY hSignatureKeyPair
;
130 /******************************************************************************
131 * Some magic constants
133 #define RSAENH_ENCRYPT 1
134 #define RSAENH_DECRYPT 0
135 #define RSAENH_HMAC_DEF_IPAD_CHAR 0x36
136 #define RSAENH_HMAC_DEF_OPAD_CHAR 0x5c
137 #define RSAENH_HMAC_DEF_PAD_LEN 64
138 #define RSAENH_DES_EFFECTIVE_KEYLEN 56
139 #define RSAENH_DES_STORAGE_KEYLEN 64
140 #define RSAENH_3DES112_EFFECTIVE_KEYLEN 112
141 #define RSAENH_3DES112_STORAGE_KEYLEN 128
142 #define RSAENH_3DES_EFFECTIVE_KEYLEN 168
143 #define RSAENH_3DES_STORAGE_KEYLEN 192
144 #define RSAENH_MAGIC_RSA2 0x32415352
145 #define RSAENH_MAGIC_RSA1 0x31415352
146 #define RSAENH_PKC_BLOCKTYPE 0x02
147 #define RSAENH_SSL3_VERSION_MAJOR 3
148 #define RSAENH_SSL3_VERSION_MINOR 0
149 #define RSAENH_TLS1_VERSION_MAJOR 3
150 #define RSAENH_TLS1_VERSION_MINOR 1
151 #define RSAENH_REGKEY "Software\\Wine\\Crypto\\RSA\\%s"
153 #define RSAENH_MIN(a,b) ((a)<(b)?(a):(b))
154 /******************************************************************************
155 * aProvEnumAlgsEx - Defines the capabilities of the CSP personalities.
157 #define RSAENH_MAX_ENUMALGS 20
158 #define RSAENH_PCT1_SSL2_SSL3_TLS1 (CRYPT_FLAG_PCT1|CRYPT_FLAG_SSL2|CRYPT_FLAG_SSL3|CRYPT_FLAG_TLS1)
159 PROV_ENUMALGS_EX aProvEnumAlgsEx
[4][RSAENH_MAX_ENUMALGS
+1] =
162 {CALG_RC2
, 40, 40, 56,0, 4,"RC2", 24,"RSA Data Security's RC2"},
163 {CALG_RC4
, 40, 40, 56,0, 4,"RC4", 24,"RSA Data Security's RC4"},
164 {CALG_DES
, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
165 {CALG_SHA
, 160,160, 160,CRYPT_FLAG_SIGNING
, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
166 {CALG_MD2
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD2", 23,"Message Digest 2 (MD2)"},
167 {CALG_MD4
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD4", 23,"Message Digest 4 (MD4)"},
168 {CALG_MD5
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD5", 23,"Message Digest 5 (MD5)"},
169 {CALG_SSL3_SHAMD5
,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
170 {CALG_MAC
, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
171 {CALG_RSA_SIGN
, 512,384,16384,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_SIGN",14,"RSA Signature"},
172 {CALG_RSA_KEYX
, 512,384, 1024,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_KEYX",17,"RSA Key Exchange"},
173 {CALG_HMAC
, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
174 {0, 0, 0, 0,0, 1,"", 1,""}
177 {CALG_RC2
, 128, 40, 128,0, 4,"RC2", 24,"RSA Data Security's RC2"},
178 {CALG_RC4
, 128, 40, 128,0, 4,"RC4", 24,"RSA Data Security's RC4"},
179 {CALG_DES
, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
180 {CALG_3DES_112
, 112,112, 112,0, 13,"3DES TWO KEY",19,"Two Key Triple DES"},
181 {CALG_3DES
, 168,168, 168,0, 5,"3DES", 21,"Three Key Triple DES"},
182 {CALG_SHA
, 160,160, 160,CRYPT_FLAG_SIGNING
, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
183 {CALG_MD2
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD2", 23,"Message Digest 2 (MD2)"},
184 {CALG_MD4
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD4", 23,"Message Digest 4 (MD4)"},
185 {CALG_MD5
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD5", 23,"Message Digest 5 (MD5)"},
186 {CALG_SSL3_SHAMD5
,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
187 {CALG_MAC
, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
188 {CALG_RSA_SIGN
,1024,384,16384,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_SIGN",14,"RSA Signature"},
189 {CALG_RSA_KEYX
,1024,384,16384,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_KEYX",17,"RSA Key Exchange"},
190 {CALG_HMAC
, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
191 {0, 0, 0, 0,0, 1,"", 1,""}
194 {CALG_RC2
, 128, 40, 128,0, 4,"RC2", 24,"RSA Data Security's RC2"},
195 {CALG_RC4
, 128, 40, 128,0, 4,"RC4", 24,"RSA Data Security's RC4"},
196 {CALG_DES
, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
197 {CALG_3DES_112
, 112,112, 112,0, 13,"3DES TWO KEY",19,"Two Key Triple DES"},
198 {CALG_3DES
, 168,168, 168,0, 5,"3DES", 21,"Three Key Triple DES"},
199 {CALG_SHA
, 160,160, 160,CRYPT_FLAG_SIGNING
, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
200 {CALG_MD2
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD2", 23,"Message Digest 2 (MD2)"},
201 {CALG_MD4
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD4", 23,"Message Digest 4 (MD4)"},
202 {CALG_MD5
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD5", 23,"Message Digest 5 (MD5)"},
203 {CALG_SSL3_SHAMD5
,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
204 {CALG_MAC
, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
205 {CALG_RSA_SIGN
,1024,384,16384,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_SIGN",14,"RSA Signature"},
206 {CALG_RSA_KEYX
,1024,384,16384,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_KEYX",17,"RSA Key Exchange"},
207 {CALG_HMAC
, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
208 {0, 0, 0, 0,0, 1,"", 1,""}
211 {CALG_RC2
, 128, 40, 128,RSAENH_PCT1_SSL2_SSL3_TLS1
, 4,"RC2", 24,"RSA Data Security's RC2"},
212 {CALG_RC4
, 128, 40, 128,RSAENH_PCT1_SSL2_SSL3_TLS1
, 4,"RC4", 24,"RSA Data Security's RC4"},
213 {CALG_DES
, 56, 56, 56,RSAENH_PCT1_SSL2_SSL3_TLS1
, 4,"DES", 31,"Data Encryption Standard (DES)"},
214 {CALG_3DES_112
, 112,112, 112,RSAENH_PCT1_SSL2_SSL3_TLS1
,13,"3DES TWO KEY",19,"Two Key Triple DES"},
215 {CALG_3DES
, 168,168, 168,RSAENH_PCT1_SSL2_SSL3_TLS1
, 5,"3DES", 21,"Three Key Triple DES"},
216 {CALG_SHA
,160,160,160,CRYPT_FLAG_SIGNING
|RSAENH_PCT1_SSL2_SSL3_TLS1
,6,"SHA-1",30,"Secure Hash Algorithm (SHA-1)"},
217 {CALG_MD5
,128,128,128,CRYPT_FLAG_SIGNING
|RSAENH_PCT1_SSL2_SSL3_TLS1
,4,"MD5",23,"Message Digest 5 (MD5)"},
218 {CALG_SSL3_SHAMD5
,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
219 {CALG_MAC
, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
220 {CALG_RSA_SIGN
,1024,384,16384,CRYPT_FLAG_SIGNING
|RSAENH_PCT1_SSL2_SSL3_TLS1
,9,"RSA_SIGN",14,"RSA Signature"},
221 {CALG_RSA_KEYX
,1024,384,16384,CRYPT_FLAG_SIGNING
|RSAENH_PCT1_SSL2_SSL3_TLS1
,9,"RSA_KEYX",17,"RSA Key Exchange"},
222 {CALG_HMAC
, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
223 {CALG_PCT1_MASTER
,128,128,128,CRYPT_FLAG_PCT1
, 12,"PCT1 MASTER",12,"PCT1 Master"},
224 {CALG_SSL2_MASTER
,40,40, 192,CRYPT_FLAG_SSL2
, 12,"SSL2 MASTER",12,"SSL2 Master"},
225 {CALG_SSL3_MASTER
,384,384,384,CRYPT_FLAG_SSL3
, 12,"SSL3 MASTER",12,"SSL3 Master"},
226 {CALG_TLS1_MASTER
,384,384,384,CRYPT_FLAG_TLS1
, 12,"TLS1 MASTER",12,"TLS1 Master"},
227 {CALG_SCHANNEL_MASTER_HASH
,0,0,-1,0, 16,"SCH MASTER HASH",21,"SChannel Master Hash"},
228 {CALG_SCHANNEL_MAC_KEY
,0,0,-1,0, 12,"SCH MAC KEY",17,"SChannel MAC Key"},
229 {CALG_SCHANNEL_ENC_KEY
,0,0,-1,0, 12,"SCH ENC KEY",24,"SChannel Encryption Key"},
230 {CALG_TLS1PRF
, 0, 0, -1,0, 9,"TLS1 PRF", 28,"TLS1 Pseudo Random Function"},
231 {0, 0, 0, 0,0, 1,"", 1,""}
235 /******************************************************************************
236 * API forward declarations
239 RSAENH_CPGetKeyParam(
270 RSAENH_CPSetHashParam(
274 BYTE
*pbData
, DWORD dwFlags
278 RSAENH_CPGetHashParam(
288 RSAENH_CPDestroyHash(
323 /******************************************************************************
324 * CSP's handle table (used by all acquired key containers)
326 static HANDLETABLE handle_table
;
328 /******************************************************************************
331 * Initializes and destroys the handle table for the CSP's handles.
333 int WINAPI
DllMain(HINSTANCE hInstance
, DWORD fdwReason
, PVOID pvReserved
)
337 case DLL_PROCESS_ATTACH
:
338 DisableThreadLibraryCalls(hInstance
);
339 init_handle_table(&handle_table
);
342 case DLL_PROCESS_DETACH
:
343 destroy_handle_table(&handle_table
);
349 /******************************************************************************
350 * copy_param [Internal]
352 * Helper function that supports the standard WINAPI protocol for querying data
356 * pbBuffer [O] Buffer where the queried parameter is copied to, if it is large enough.
357 * May be NUL if the required buffer size is to be queried only.
358 * pdwBufferSize [I/O] In: Size of the buffer at pbBuffer
359 * Out: Size of parameter pbParam
360 * pbParam [I] Parameter value.
361 * dwParamSize [I] Size of pbParam
364 * Success: TRUE (pbParam was copied into pbBuffer or pbBuffer is NULL)
365 * Failure: FALSE (pbBuffer is not large enough to hold pbParam). Last error: ERROR_MORE_DATA
367 static inline BOOL
copy_param(
368 BYTE
*pbBuffer
, DWORD
*pdwBufferSize
, CONST BYTE
*pbParam
, DWORD dwParamSize
)
372 if (dwParamSize
> *pdwBufferSize
)
374 SetLastError(ERROR_MORE_DATA
);
375 *pdwBufferSize
= dwParamSize
;
378 memcpy(pbBuffer
, pbParam
, dwParamSize
);
380 *pdwBufferSize
= dwParamSize
;
384 /******************************************************************************
385 * get_algid_info [Internal]
387 * Query CSP capabilities for a given crypto algorithm.
390 * hProv [I] Handle to a key container of the CSP whose capabilities are to be queried.
391 * algid [I] Identifier of the crypto algorithm about which information is requested.
394 * Success: Pointer to a PROV_ENUMALGS_EX struct containing information about the crypto algorithm.
395 * Failure: NULL (algid not supported)
397 static inline const PROV_ENUMALGS_EX
* get_algid_info(HCRYPTPROV hProv
, ALG_ID algid
) {
398 PROV_ENUMALGS_EX
*iterator
;
399 KEYCONTAINER
*pKeyContainer
;
401 if (!lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
, (OBJECTHDR
**)&pKeyContainer
)) {
402 SetLastError(NTE_BAD_UID
);
406 for (iterator
= aProvEnumAlgsEx
[pKeyContainer
->dwPersonality
]; iterator
->aiAlgid
; iterator
++) {
407 if (iterator
->aiAlgid
== algid
) return iterator
;
410 SetLastError(NTE_BAD_ALGID
);
414 /******************************************************************************
415 * copy_data_blob [Internal]
417 * deeply copies a DATA_BLOB
420 * dst [O] That's where the blob will be copied to
421 * src [I] Source blob
425 * Failure: FALSE (GetLastError() == NTE_NO_MEMORY
428 * Use free_data_blob to release resources occupied by copy_data_blob.
430 static inline BOOL
copy_data_blob(PCRYPT_DATA_BLOB dst
, CONST PCRYPT_DATA_BLOB src
) {
431 dst
->pbData
= HeapAlloc(GetProcessHeap(), 0, src
->cbData
);
433 SetLastError(NTE_NO_MEMORY
);
436 dst
->cbData
= src
->cbData
;
437 memcpy(dst
->pbData
, src
->pbData
, src
->cbData
);
441 /******************************************************************************
442 * concat_data_blobs [Internal]
444 * Concatenates two blobs
447 * dst [O] The new blob will be copied here
448 * src1 [I] Prefix blob
449 * src2 [I] Appendix blob
453 * Failure: FALSE (GetLastError() == NTE_NO_MEMORY)
456 * Release resources occupied by concat_data_blobs with free_data_blobs
458 static inline BOOL
concat_data_blobs(PCRYPT_DATA_BLOB dst
, CONST PCRYPT_DATA_BLOB src1
,
459 CONST PCRYPT_DATA_BLOB src2
)
461 dst
->cbData
= src1
->cbData
+ src2
->cbData
;
462 dst
->pbData
= HeapAlloc(GetProcessHeap(), 0, dst
->cbData
);
464 SetLastError(NTE_NO_MEMORY
);
467 memcpy(dst
->pbData
, src1
->pbData
, src1
->cbData
);
468 memcpy(dst
->pbData
+ src1
->cbData
, src2
->pbData
, src2
->cbData
);
472 /******************************************************************************
473 * free_data_blob [Internal]
475 * releases resource occupied by a dynamically allocated CRYPT_DATA_BLOB
478 * pBlob [I] Heap space occupied by pBlob->pbData is released
480 static inline void free_data_blob(PCRYPT_DATA_BLOB pBlob
) {
481 HeapFree(GetProcessHeap(), 0, pBlob
->pbData
);
484 /******************************************************************************
485 * init_data_blob [Internal]
487 static inline void init_data_blob(PCRYPT_DATA_BLOB pBlob
) {
488 pBlob
->pbData
= NULL
;
492 /******************************************************************************
493 * free_hmac_info [Internal]
495 * Deeply free an HMAC_INFO struct.
498 * hmac_info [I] Pointer to the HMAC_INFO struct to be freed.
501 * See Internet RFC 2104 for details on the HMAC algorithm.
503 static inline void free_hmac_info(PHMAC_INFO hmac_info
) {
504 if (!hmac_info
) return;
505 HeapFree(GetProcessHeap(), 0, hmac_info
->pbInnerString
);
506 HeapFree(GetProcessHeap(), 0, hmac_info
->pbOuterString
);
507 HeapFree(GetProcessHeap(), 0, hmac_info
);
510 /******************************************************************************
511 * copy_hmac_info [Internal]
513 * Deeply copy an HMAC_INFO struct
516 * dst [O] Pointer to a location where the pointer to the HMAC_INFO copy will be stored.
517 * src [I] Pointer to the HMAC_INFO struct to be copied.
524 * See Internet RFC 2104 for details on the HMAC algorithm.
526 static BOOL
copy_hmac_info(PHMAC_INFO
*dst
, PHMAC_INFO src
) {
527 if (!src
) return FALSE
;
528 *dst
= HeapAlloc(GetProcessHeap(), 0, sizeof(HMAC_INFO
));
529 if (!*dst
) return FALSE
;
530 memcpy(*dst
, src
, sizeof(HMAC_INFO
));
531 (*dst
)->pbInnerString
= NULL
;
532 (*dst
)->pbOuterString
= NULL
;
533 if ((*dst
)->cbInnerString
== 0) (*dst
)->cbInnerString
= RSAENH_HMAC_DEF_PAD_LEN
;
534 (*dst
)->pbInnerString
= HeapAlloc(GetProcessHeap(), 0, (*dst
)->cbInnerString
);
535 if (!(*dst
)->pbInnerString
) {
536 free_hmac_info(*dst
);
539 if (src
->cbInnerString
)
540 memcpy((*dst
)->pbInnerString
, src
->pbInnerString
, src
->cbInnerString
);
542 memset((*dst
)->pbInnerString
, RSAENH_HMAC_DEF_IPAD_CHAR
, RSAENH_HMAC_DEF_PAD_LEN
);
543 if ((*dst
)->cbOuterString
== 0) (*dst
)->cbOuterString
= RSAENH_HMAC_DEF_PAD_LEN
;
544 (*dst
)->pbOuterString
= HeapAlloc(GetProcessHeap(), 0, (*dst
)->cbOuterString
);
545 if (!(*dst
)->pbOuterString
) {
546 free_hmac_info(*dst
);
549 if (src
->cbOuterString
)
550 memcpy((*dst
)->pbOuterString
, src
->pbOuterString
, src
->cbOuterString
);
552 memset((*dst
)->pbOuterString
, RSAENH_HMAC_DEF_OPAD_CHAR
, RSAENH_HMAC_DEF_PAD_LEN
);
556 /******************************************************************************
557 * destroy_hash [Internal]
559 * Destructor for hash objects
562 * pCryptHash [I] Pointer to the hash object to be destroyed.
563 * Will be invalid after function returns!
565 static void destroy_hash(OBJECTHDR
*pObject
)
567 CRYPTHASH
*pCryptHash
= (CRYPTHASH
*)pObject
;
569 free_hmac_info(pCryptHash
->pHMACInfo
);
570 free_data_blob(&pCryptHash
->tpPRFParams
.blobLabel
);
571 free_data_blob(&pCryptHash
->tpPRFParams
.blobSeed
);
572 HeapFree(GetProcessHeap(), 0, pCryptHash
);
575 /******************************************************************************
576 * init_hash [Internal]
578 * Initialize (or reset) a hash object
581 * pCryptHash [I] The hash object to be initialized.
583 static inline BOOL
init_hash(CRYPTHASH
*pCryptHash
) {
586 switch (pCryptHash
->aiAlgid
)
589 if (pCryptHash
->pHMACInfo
) {
590 const PROV_ENUMALGS_EX
*pAlgInfo
;
592 pAlgInfo
= get_algid_info(pCryptHash
->hProv
, pCryptHash
->pHMACInfo
->HashAlgid
);
593 if (!pAlgInfo
) return FALSE
;
594 pCryptHash
->dwHashSize
= pAlgInfo
->dwDefaultLen
>> 3;
595 init_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
);
596 update_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
597 pCryptHash
->pHMACInfo
->pbInnerString
,
598 pCryptHash
->pHMACInfo
->cbInnerString
);
603 dwLen
= sizeof(DWORD
);
604 RSAENH_CPGetKeyParam(pCryptHash
->hProv
, pCryptHash
->hKey
, KP_BLOCKLEN
,
605 (BYTE
*)&pCryptHash
->dwHashSize
, &dwLen
, 0);
606 pCryptHash
->dwHashSize
>>= 3;
610 return init_hash_impl(pCryptHash
->aiAlgid
, &pCryptHash
->context
);
614 /******************************************************************************
615 * update_hash [Internal]
617 * Hashes the given data and updates the hash object's state accordingly
620 * pCryptHash [I] Hash object to be updated.
621 * pbData [I] Pointer to data stream to be hashed.
622 * dwDataLen [I] Length of data stream.
624 static inline void update_hash(CRYPTHASH
*pCryptHash
, CONST BYTE
*pbData
, DWORD dwDataLen
) {
627 switch (pCryptHash
->aiAlgid
)
630 if (pCryptHash
->pHMACInfo
)
631 update_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
636 pbTemp
= HeapAlloc(GetProcessHeap(), 0, dwDataLen
);
638 memcpy(pbTemp
, pbData
, dwDataLen
);
639 RSAENH_CPEncrypt(pCryptHash
->hProv
, pCryptHash
->hKey
, (HCRYPTHASH
)NULL
, FALSE
, 0,
640 pbTemp
, &dwDataLen
, dwDataLen
);
641 HeapFree(GetProcessHeap(), 0, pbTemp
);
645 update_hash_impl(pCryptHash
->aiAlgid
, &pCryptHash
->context
, pbData
, dwDataLen
);
649 /******************************************************************************
650 * finalize_hash [Internal]
652 * Finalizes the hash, after all data has been hashed with update_hash.
653 * No additional data can be hashed afterwards until the hash gets initialized again.
656 * pCryptHash [I] Hash object to be finalized.
658 static inline void finalize_hash(CRYPTHASH
*pCryptHash
) {
661 switch (pCryptHash
->aiAlgid
)
664 if (pCryptHash
->pHMACInfo
) {
665 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
];
667 finalize_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
668 pCryptHash
->abHashValue
);
669 memcpy(abHashValue
, pCryptHash
->abHashValue
, pCryptHash
->dwHashSize
);
670 init_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
);
671 update_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
672 pCryptHash
->pHMACInfo
->pbOuterString
,
673 pCryptHash
->pHMACInfo
->cbOuterString
);
674 update_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
675 abHashValue
, pCryptHash
->dwHashSize
);
676 finalize_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
677 pCryptHash
->abHashValue
);
683 RSAENH_CPEncrypt(pCryptHash
->hProv
, pCryptHash
->hKey
, (HCRYPTHASH
)NULL
, TRUE
, 0,
684 pCryptHash
->abHashValue
, &dwDataLen
, pCryptHash
->dwHashSize
);
688 finalize_hash_impl(pCryptHash
->aiAlgid
, &pCryptHash
->context
, pCryptHash
->abHashValue
);
692 /******************************************************************************
693 * destroy_key [Internal]
695 * Destructor for key objects
698 * pCryptKey [I] Pointer to the key object to be destroyed.
699 * Will be invalid after function returns!
701 static void destroy_key(OBJECTHDR
*pObject
)
703 CRYPTKEY
*pCryptKey
= (CRYPTKEY
*)pObject
;
705 free_key_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
);
706 free_data_blob(&pCryptKey
->siSChannelInfo
.blobClientRandom
);
707 free_data_blob(&pCryptKey
->siSChannelInfo
.blobServerRandom
);
708 HeapFree(GetProcessHeap(), 0, pCryptKey
);
711 /******************************************************************************
712 * setup_key [Internal]
714 * Initialize (or reset) a key object
717 * pCryptKey [I] The key object to be initialized.
719 static inline void setup_key(CRYPTKEY
*pCryptKey
) {
720 pCryptKey
->dwState
= RSAENH_KEYSTATE_IDLE
;
721 memcpy(pCryptKey
->abChainVector
, pCryptKey
->abInitVector
, sizeof(pCryptKey
->abChainVector
));
722 setup_key_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pCryptKey
->dwKeyLen
,
723 pCryptKey
->dwSaltLen
, 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 if ((dwFlags
& CRYPT_CREATE_SALT
) || (dwKeyLen
== 40 && !(dwFlags
& CRYPT_NO_SALT
)))
819 pCryptKey
->dwSaltLen
= 16 /*FIXME*/ - pCryptKey
->dwKeyLen
;
821 pCryptKey
->dwSaltLen
= 0;
822 memset(pCryptKey
->abKeyValue
, 0, sizeof(pCryptKey
->abKeyValue
));
823 memset(pCryptKey
->abInitVector
, 0, sizeof(pCryptKey
->abInitVector
));
824 init_data_blob(&pCryptKey
->siSChannelInfo
.blobClientRandom
);
825 init_data_blob(&pCryptKey
->siSChannelInfo
.blobServerRandom
);
829 case CALG_PCT1_MASTER
:
830 case CALG_SSL2_MASTER
:
831 case CALG_SSL3_MASTER
:
832 case CALG_TLS1_MASTER
:
834 pCryptKey
->dwBlockLen
= 0;
835 pCryptKey
->dwMode
= 0;
842 pCryptKey
->dwBlockLen
= 8;
843 pCryptKey
->dwMode
= CRYPT_MODE_CBC
;
848 pCryptKey
->dwBlockLen
= dwKeyLen
>> 3;
849 pCryptKey
->dwMode
= 0;
853 *ppCryptKey
= pCryptKey
;
859 /******************************************************************************
860 * destroy_key_container [Internal]
862 * Destructor for key containers.
865 * pObjectHdr [I] Pointer to the key container to be destroyed.
867 static void destroy_key_container(OBJECTHDR
*pObjectHdr
)
869 KEYCONTAINER
*pKeyContainer
= (KEYCONTAINER
*)pObjectHdr
;
870 DATA_BLOB blobIn
, blobOut
;
872 CHAR szRSABase
[MAX_PATH
];
877 if (!(pKeyContainer
->dwFlags
& CRYPT_VERIFYCONTEXT
)) {
878 /* On WinXP, persistent keys are stored in a file located at:
879 * $AppData$\\Microsoft\\Crypto\\RSA\\$SID$\\some_hex_string
881 sprintf(szRSABase
, RSAENH_REGKEY
, pKeyContainer
->szName
);
883 if (pKeyContainer
->dwFlags
& CRYPT_MACHINE_KEYSET
) {
884 hRootKey
= HKEY_LOCAL_MACHINE
;
886 hRootKey
= HKEY_CURRENT_USER
;
889 /* @@ Wine registry key: HKLM\Software\Wine\Crypto\RSA */
890 /* @@ Wine registry key: HKCU\Software\Wine\Crypto\RSA */
891 if (RegCreateKeyExA(hRootKey
, szRSABase
, 0, NULL
, REG_OPTION_NON_VOLATILE
,
892 KEY_WRITE
, NULL
, &hKey
, NULL
) == ERROR_SUCCESS
)
894 if (lookup_handle(&handle_table
, pKeyContainer
->hKeyExchangeKeyPair
, RSAENH_MAGIC_KEY
,
897 if (RSAENH_CPExportKey(pKey
->hProv
, pKeyContainer
->hKeyExchangeKeyPair
, 0,
898 PRIVATEKEYBLOB
, 0, 0, &dwLen
))
900 pbKey
= HeapAlloc(GetProcessHeap(), 0, dwLen
);
903 if (RSAENH_CPExportKey(pKey
->hProv
, pKeyContainer
->hKeyExchangeKeyPair
, 0,
904 PRIVATEKEYBLOB
, 0, pbKey
, &dwLen
))
906 blobIn
.pbData
= pbKey
;
907 blobIn
.cbData
= dwLen
;
909 if (CryptProtectData(&blobIn
, NULL
, NULL
, NULL
, NULL
,
910 (pKeyContainer
->dwFlags
& CRYPT_MACHINE_KEYSET
) ?
911 CRYPTPROTECT_LOCAL_MACHINE
: 0,
914 RegSetValueExA(hKey
, "KeyExchangeKeyPair", 0, REG_BINARY
,
915 blobOut
.pbData
, blobOut
.cbData
);
916 HeapFree(GetProcessHeap(), 0, blobOut
.pbData
);
919 HeapFree(GetProcessHeap(), 0, pbKey
);
922 release_handle(&handle_table
, (unsigned int)pKeyContainer
->hKeyExchangeKeyPair
,
926 if (lookup_handle(&handle_table
, pKeyContainer
->hSignatureKeyPair
, RSAENH_MAGIC_KEY
,
929 if (RSAENH_CPExportKey(pKey
->hProv
, pKeyContainer
->hSignatureKeyPair
, 0,
930 PRIVATEKEYBLOB
, 0, 0, &dwLen
))
932 pbKey
= HeapAlloc(GetProcessHeap(), 0, dwLen
);
935 if (RSAENH_CPExportKey(pKey
->hProv
, pKeyContainer
->hSignatureKeyPair
, 0,
936 PRIVATEKEYBLOB
, 0, pbKey
, &dwLen
))
938 blobIn
.pbData
= pbKey
;
939 blobIn
.cbData
= dwLen
;
941 if (CryptProtectData(&blobIn
, NULL
, NULL
, NULL
, NULL
,
942 (pKeyContainer
->dwFlags
& CRYPT_MACHINE_KEYSET
) ?
943 CRYPTPROTECT_LOCAL_MACHINE
: 0,
946 RegSetValueExA(hKey
, "SignatureKeyPair", 0, REG_BINARY
,
947 blobOut
.pbData
, blobOut
.cbData
);
948 HeapFree(GetProcessHeap(), 0, blobOut
.pbData
);
951 HeapFree(GetProcessHeap(), 0, pbKey
);
954 release_handle(&handle_table
, (unsigned int)pKeyContainer
->hSignatureKeyPair
,
962 HeapFree( GetProcessHeap(), 0, pKeyContainer
);
965 /******************************************************************************
966 * new_key_container [Internal]
968 * Create a new key container. The personality (RSA Base, Strong or Enhanced CP)
969 * of the CSP is determined via the pVTable->pszProvName string.
972 * pszContainerName [I] Name of the key container.
973 * pVTable [I] Callback functions and context info provided by the OS
976 * Success: Handle to the new key container.
977 * Failure: INVALID_HANDLE_VALUE
979 static HCRYPTPROV
new_key_container(PCCH pszContainerName
, DWORD dwFlags
, PVTableProvStruc pVTable
)
981 KEYCONTAINER
*pKeyContainer
;
982 HCRYPTPROV hKeyContainer
;
984 hKeyContainer
= (HCRYPTPROV
)new_object(&handle_table
, sizeof(KEYCONTAINER
), RSAENH_MAGIC_CONTAINER
,
985 destroy_key_container
, (OBJECTHDR
**)&pKeyContainer
);
986 if (hKeyContainer
!= (HCRYPTPROV
)INVALID_HANDLE_VALUE
)
988 lstrcpynA(pKeyContainer
->szName
, pszContainerName
, MAX_PATH
);
989 pKeyContainer
->dwFlags
= dwFlags
;
990 pKeyContainer
->dwEnumAlgsCtr
= 0;
991 pKeyContainer
->hKeyExchangeKeyPair
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
992 pKeyContainer
->hSignatureKeyPair
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
993 if (pVTable
&& pVTable
->pszProvName
) {
994 lstrcpynA(pKeyContainer
->szProvName
, pVTable
->pszProvName
, MAX_PATH
);
995 if (!strcmp(pVTable
->pszProvName
, MS_DEF_PROV_A
)) {
996 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_BASE
;
997 } else if (!strcmp(pVTable
->pszProvName
, MS_ENHANCED_PROV_A
)) {
998 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_ENHANCED
;
999 } else if (!strcmp(pVTable
->pszProvName
, MS_DEF_RSA_SCHANNEL_PROV_A
)) {
1000 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_SCHANNEL
;
1002 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_STRONG
;
1006 /* The new key container has to be inserted into the CSP immediately
1007 * after creation to be available for CPGetProvParam's PP_ENUMCONTAINERS. */
1008 if (!(dwFlags
& CRYPT_VERIFYCONTEXT
)) {
1009 CHAR szRSABase
[MAX_PATH
];
1010 HKEY hRootKey
, hKey
;
1012 sprintf(szRSABase
, RSAENH_REGKEY
, pKeyContainer
->szName
);
1014 if (pKeyContainer
->dwFlags
& CRYPT_MACHINE_KEYSET
) {
1015 hRootKey
= HKEY_LOCAL_MACHINE
;
1017 hRootKey
= HKEY_CURRENT_USER
;
1020 /* @@ Wine registry key: HKLM\Software\Wine\Crypto\RSA */
1021 /* @@ Wine registry key: HKCU\Software\Wine\Crypto\RSA */
1022 RegCreateKeyA(hRootKey
, szRSABase
, &hKey
);
1027 return hKeyContainer
;
1030 /******************************************************************************
1031 * read_key_container [Internal]
1033 * Tries to read the persistent state of the key container (mainly the signature
1034 * and key exchange private keys) given by pszContainerName.
1037 * pszContainerName [I] Name of the key container to read from the registry
1038 * pVTable [I] Pointer to context data provided by the operating system
1041 * Success: Handle to the key container read from the registry
1042 * Failure: INVALID_HANDLE_VALUE
1044 static HCRYPTPROV
read_key_container(PCHAR pszContainerName
, DWORD dwFlags
, PVTableProvStruc pVTable
)
1046 CHAR szRSABase
[MAX_PATH
];
1048 HKEY hKey
, hRootKey
;
1049 DWORD dwValueType
, dwLen
;
1050 KEYCONTAINER
*pKeyContainer
;
1051 HCRYPTPROV hKeyContainer
;
1052 DATA_BLOB blobIn
, blobOut
;
1054 sprintf(szRSABase
, RSAENH_REGKEY
, pszContainerName
);
1056 if (dwFlags
& CRYPT_MACHINE_KEYSET
) {
1057 hRootKey
= HKEY_LOCAL_MACHINE
;
1059 hRootKey
= HKEY_CURRENT_USER
;
1062 /* @@ Wine registry key: HKLM\Software\Wine\Crypto\RSA */
1063 /* @@ Wine registry key: HKCU\Software\Wine\Crypto\RSA */
1064 if (RegOpenKeyExA(hRootKey
, szRSABase
, 0, KEY_READ
, &hKey
) != ERROR_SUCCESS
)
1066 SetLastError(NTE_BAD_KEYSET
);
1067 return (HCRYPTPROV
)INVALID_HANDLE_VALUE
;
1070 hKeyContainer
= new_key_container(pszContainerName
, dwFlags
, pVTable
);
1071 if (hKeyContainer
!= (HCRYPTPROV
)INVALID_HANDLE_VALUE
)
1073 if (!lookup_handle(&handle_table
, hKeyContainer
, RSAENH_MAGIC_CONTAINER
,
1074 (OBJECTHDR
**)&pKeyContainer
))
1075 return (HCRYPTPROV
)INVALID_HANDLE_VALUE
;
1077 if (RegQueryValueExA(hKey
, "KeyExchangeKeyPair", 0, &dwValueType
, NULL
, &dwLen
) ==
1080 pbKey
= HeapAlloc(GetProcessHeap(), 0, dwLen
);
1083 if (RegQueryValueExA(hKey
, "KeyExchangeKeyPair", 0, &dwValueType
, pbKey
, &dwLen
) ==
1086 blobIn
.pbData
= pbKey
;
1087 blobIn
.cbData
= dwLen
;
1089 if (CryptUnprotectData(&blobIn
, NULL
, NULL
, NULL
, NULL
,
1090 (dwFlags
& CRYPT_MACHINE_KEYSET
) ? CRYPTPROTECT_LOCAL_MACHINE
: 0, &blobOut
))
1092 RSAENH_CPImportKey(hKeyContainer
, blobOut
.pbData
, blobOut
.cbData
, 0, 0,
1093 &pKeyContainer
->hKeyExchangeKeyPair
);
1094 HeapFree(GetProcessHeap(), 0, blobOut
.pbData
);
1097 HeapFree(GetProcessHeap(), 0, pbKey
);
1101 if (RegQueryValueExA(hKey
, "SignatureKeyPair", 0, &dwValueType
, NULL
, &dwLen
) ==
1104 pbKey
= HeapAlloc(GetProcessHeap(), 0, dwLen
);
1107 if (RegQueryValueExA(hKey
, "SignatureKeyPair", 0, &dwValueType
, pbKey
, &dwLen
) ==
1110 blobIn
.pbData
= pbKey
;
1111 blobIn
.cbData
= dwLen
;
1113 if (CryptUnprotectData(&blobIn
, NULL
, NULL
, NULL
, NULL
,
1114 (dwFlags
& CRYPT_MACHINE_KEYSET
) ? CRYPTPROTECT_LOCAL_MACHINE
: 0, &blobOut
))
1116 RSAENH_CPImportKey(hKeyContainer
, blobOut
.pbData
, blobOut
.cbData
, 0, 0,
1117 &pKeyContainer
->hSignatureKeyPair
);
1118 HeapFree(GetProcessHeap(), 0, blobOut
.pbData
);
1121 HeapFree(GetProcessHeap(), 0, pbKey
);
1126 return hKeyContainer
;
1129 /******************************************************************************
1130 * build_hash_signature [Internal]
1132 * Builds a padded version of a hash to match the length of the RSA key modulus.
1135 * pbSignature [O] The padded hash object is stored here.
1136 * dwLen [I] Length of the pbSignature buffer.
1137 * aiAlgid [I] Algorithm identifier of the hash to be padded.
1138 * abHashValue [I] The value of the hash object.
1139 * dwHashLen [I] Length of the hash value.
1140 * dwFlags [I] Selection of padding algorithm.
1144 * Failure: FALSE (NTE_BAD_ALGID)
1146 static BOOL
build_hash_signature(BYTE
*pbSignature
, DWORD dwLen
, ALG_ID aiAlgid
,
1147 CONST BYTE
*abHashValue
, DWORD dwHashLen
, DWORD dwFlags
)
1149 /* These prefixes are meant to be concatenated with hash values of the
1150 * respective kind to form a PKCS #7 DigestInfo. */
1151 static const struct tagOIDDescriptor
{
1154 CONST BYTE abOID
[18];
1155 } aOIDDescriptor
[5] = {
1156 { CALG_MD2
, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1157 0x86, 0xf7, 0x0d, 0x02, 0x02, 0x05, 0x00, 0x04, 0x10 } },
1158 { CALG_MD4
, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1159 0x86, 0xf7, 0x0d, 0x02, 0x04, 0x05, 0x00, 0x04, 0x10 } },
1160 { CALG_MD5
, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1161 0x86, 0xf7, 0x0d, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10 } },
1162 { CALG_SHA
, 15, { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03,
1163 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14 } },
1166 DWORD dwIdxOID
, i
, j
;
1168 for (dwIdxOID
= 0; aOIDDescriptor
[dwIdxOID
].aiAlgid
; dwIdxOID
++) {
1169 if (aOIDDescriptor
[dwIdxOID
].aiAlgid
== aiAlgid
) break;
1172 if (!aOIDDescriptor
[dwIdxOID
].aiAlgid
) {
1173 SetLastError(NTE_BAD_ALGID
);
1177 /* Build the padded signature */
1178 if (dwFlags
& CRYPT_X931_FORMAT
) {
1179 pbSignature
[0] = 0x6b;
1180 for (i
=1; i
< dwLen
- dwHashLen
- 3; i
++) {
1181 pbSignature
[i
] = 0xbb;
1183 pbSignature
[i
++] = 0xba;
1184 for (j
=0; j
< dwHashLen
; j
++, i
++) {
1185 pbSignature
[i
] = abHashValue
[j
];
1187 pbSignature
[i
++] = 0x33;
1188 pbSignature
[i
++] = 0xcc;
1190 pbSignature
[0] = 0x00;
1191 pbSignature
[1] = 0x01;
1192 if (dwFlags
& CRYPT_NOHASHOID
) {
1193 for (i
=2; i
< dwLen
- 1 - dwHashLen
; i
++) {
1194 pbSignature
[i
] = 0xff;
1196 pbSignature
[i
++] = 0x00;
1198 for (i
=2; i
< dwLen
- 1 - aOIDDescriptor
[dwIdxOID
].dwLen
- dwHashLen
; i
++) {
1199 pbSignature
[i
] = 0xff;
1201 pbSignature
[i
++] = 0x00;
1202 for (j
=0; j
< aOIDDescriptor
[dwIdxOID
].dwLen
; j
++) {
1203 pbSignature
[i
++] = aOIDDescriptor
[dwIdxOID
].abOID
[j
];
1206 for (j
=0; j
< dwHashLen
; j
++) {
1207 pbSignature
[i
++] = abHashValue
[j
];
1214 /******************************************************************************
1217 * This is an implementation of the 'P_hash' helper function for TLS1's PRF.
1218 * It is used exclusively by tls1_prf. For details see RFC 2246, chapter 5.
1219 * The pseudo random stream generated by this function is exclusive or'ed with
1220 * the data in pbBuffer.
1223 * hHMAC [I] HMAC object, which will be used in pseudo random generation
1224 * pblobSeed [I] Seed value
1225 * pbBuffer [I/O] Pseudo random stream will be xor'ed to the provided data
1226 * dwBufferLen [I] Number of pseudo random bytes desired
1232 static BOOL
tls1_p(HCRYPTHASH hHMAC
, CONST PCRYPT_DATA_BLOB pblobSeed
, PBYTE pbBuffer
, DWORD dwBufferLen
)
1235 BYTE abAi
[RSAENH_MAX_HASH_SIZE
];
1238 if (!lookup_handle(&handle_table
, hHMAC
, RSAENH_MAGIC_HASH
, (OBJECTHDR
**)&pHMAC
)) {
1239 SetLastError(NTE_BAD_HASH
);
1243 /* compute A_1 = HMAC(seed) */
1245 update_hash(pHMAC
, pblobSeed
->pbData
, pblobSeed
->cbData
);
1246 finalize_hash(pHMAC
);
1247 memcpy(abAi
, pHMAC
->abHashValue
, pHMAC
->dwHashSize
);
1250 /* compute HMAC(A_i + seed) */
1252 update_hash(pHMAC
, abAi
, pHMAC
->dwHashSize
);
1253 update_hash(pHMAC
, pblobSeed
->pbData
, pblobSeed
->cbData
);
1254 finalize_hash(pHMAC
);
1256 /* pseudo random stream := CONCAT_{i=1..n} ( HMAC(A_i + seed) ) */
1258 if (i
>= dwBufferLen
) break;
1259 pbBuffer
[i
] ^= pHMAC
->abHashValue
[i
% pHMAC
->dwHashSize
];
1261 } while (i
% pHMAC
->dwHashSize
);
1263 /* compute A_{i+1} = HMAC(A_i) */
1265 update_hash(pHMAC
, abAi
, pHMAC
->dwHashSize
);
1266 finalize_hash(pHMAC
);
1267 memcpy(abAi
, pHMAC
->abHashValue
, pHMAC
->dwHashSize
);
1268 } while (i
< dwBufferLen
);
1273 /******************************************************************************
1274 * tls1_prf [Internal]
1276 * TLS1 pseudo random function as specified in RFC 2246, chapter 5
1279 * hProv [I] Key container used to compute the pseudo random stream
1280 * hSecret [I] Key that holds the (pre-)master secret
1281 * pblobLabel [I] Descriptive label
1282 * pblobSeed [I] Seed value
1283 * pbBuffer [O] Pseudo random numbers will be stored here
1284 * dwBufferLen [I] Number of pseudo random bytes desired
1290 static BOOL
tls1_prf(HCRYPTPROV hProv
, HCRYPTPROV hSecret
, CONST PCRYPT_DATA_BLOB pblobLabel
,
1291 CONST PCRYPT_DATA_BLOB pblobSeed
, PBYTE pbBuffer
, DWORD dwBufferLen
)
1293 HMAC_INFO hmacInfo
= { 0, NULL
, 0, NULL
, 0 };
1294 HCRYPTHASH hHMAC
= (HCRYPTHASH
)INVALID_HANDLE_VALUE
;
1295 HCRYPTKEY hHalfSecret
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
1296 CRYPTKEY
*pHalfSecret
, *pSecret
;
1297 DWORD dwHalfSecretLen
;
1298 BOOL result
= FALSE
;
1299 CRYPT_DATA_BLOB blobLabelSeed
;
1301 TRACE("(hProv=%08lx, hSecret=%08lx, pblobLabel=%p, pblobSeed=%p, pbBuffer=%p, dwBufferLen=%ld)\n",
1302 hProv
, hSecret
, pblobLabel
, pblobSeed
, pbBuffer
, dwBufferLen
);
1304 if (!lookup_handle(&handle_table
, hSecret
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pSecret
)) {
1305 SetLastError(NTE_FAIL
);
1309 dwHalfSecretLen
= (pSecret
->dwKeyLen
+1)/2;
1311 /* concatenation of the label and the seed */
1312 if (!concat_data_blobs(&blobLabelSeed
, pblobLabel
, pblobSeed
)) goto exit
;
1314 /* zero out the buffer, since two random streams will be xor'ed into it. */
1315 memset(pbBuffer
, 0, dwBufferLen
);
1317 /* build a 'fake' key, to hold the secret. CALG_SSL2_MASTER is used since it provides
1318 * the biggest range of valid key lengths. */
1319 hHalfSecret
= new_key(hProv
, CALG_SSL2_MASTER
, MAKELONG(0,dwHalfSecretLen
*8), &pHalfSecret
);
1320 if (hHalfSecret
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) goto exit
;
1322 /* Derive an HMAC_MD5 hash and call the helper function. */
1323 memcpy(pHalfSecret
->abKeyValue
, pSecret
->abKeyValue
, dwHalfSecretLen
);
1324 if (!RSAENH_CPCreateHash(hProv
, CALG_HMAC
, hHalfSecret
, 0, &hHMAC
)) goto exit
;
1325 hmacInfo
.HashAlgid
= CALG_MD5
;
1326 if (!RSAENH_CPSetHashParam(hProv
, hHMAC
, HP_HMAC_INFO
, (BYTE
*)&hmacInfo
, 0)) goto exit
;
1327 if (!tls1_p(hHMAC
, &blobLabelSeed
, pbBuffer
, dwBufferLen
)) goto exit
;
1329 /* Reconfigure to HMAC_SHA hash and call helper function again. */
1330 memcpy(pHalfSecret
->abKeyValue
, pSecret
->abKeyValue
+ (pSecret
->dwKeyLen
/2), dwHalfSecretLen
);
1331 hmacInfo
.HashAlgid
= CALG_SHA
;
1332 if (!RSAENH_CPSetHashParam(hProv
, hHMAC
, HP_HMAC_INFO
, (BYTE
*)&hmacInfo
, 0)) goto exit
;
1333 if (!tls1_p(hHMAC
, &blobLabelSeed
, pbBuffer
, dwBufferLen
)) goto exit
;
1337 release_handle(&handle_table
, hHalfSecret
, RSAENH_MAGIC_KEY
);
1338 if (hHMAC
!= (HCRYPTHASH
)INVALID_HANDLE_VALUE
) RSAENH_CPDestroyHash(hProv
, hHMAC
);
1339 free_data_blob(&blobLabelSeed
);
1343 /******************************************************************************
1344 * pad_data [Internal]
1346 * Helper function for data padding according to PKCS1 #2
1349 * abData [I] The data to be padded
1350 * dwDataLen [I] Length of the data
1351 * abBuffer [O] Padded data will be stored here
1352 * dwBufferLen [I] Length of the buffer (also length of padded data)
1353 * dwFlags [I] Padding format (CRYPT_SSL2_FALLBACK)
1357 * Failure: FALSE (NTE_BAD_LEN, too much data to pad)
1359 static BOOL
pad_data(CONST BYTE
*abData
, DWORD dwDataLen
, BYTE
*abBuffer
, DWORD dwBufferLen
,
1364 /* Ensure there is enough space for PKCS1 #2 padding */
1365 if (dwDataLen
> dwBufferLen
-11) {
1366 SetLastError(NTE_BAD_LEN
);
1370 memmove(abBuffer
+ dwBufferLen
- dwDataLen
, abData
, dwDataLen
);
1373 abBuffer
[1] = RSAENH_PKC_BLOCKTYPE
;
1374 for (i
=2; i
< dwBufferLen
- dwDataLen
- 1; i
++)
1375 do gen_rand_impl(&abBuffer
[i
], 1); while (!abBuffer
[i
]);
1376 if (dwFlags
& CRYPT_SSL2_FALLBACK
)
1377 for (i
-=8; i
< dwBufferLen
- dwDataLen
- 1; i
++)
1384 /******************************************************************************
1385 * unpad_data [Internal]
1387 * Remove the PKCS1 padding from RSA decrypted data
1390 * abData [I] The padded data
1391 * dwDataLen [I] Length of the padded data
1392 * abBuffer [O] Data without padding will be stored here
1393 * dwBufferLen [I/O] I: Length of the buffer, O: Length of unpadded data
1394 * dwFlags [I] Currently none defined
1398 * Failure: FALSE, (NTE_BAD_DATA, no valid PKCS1 padding or buffer too small)
1400 static BOOL
unpad_data(CONST BYTE
*abData
, DWORD dwDataLen
, BYTE
*abBuffer
, DWORD
*dwBufferLen
,
1405 for (i
=2; i
<dwDataLen
; i
++)
1409 if ((i
== dwDataLen
) || (*dwBufferLen
< dwDataLen
- i
- 1) ||
1410 (abData
[0] != 0x00) || (abData
[1] != RSAENH_PKC_BLOCKTYPE
))
1412 SetLastError(NTE_BAD_DATA
);
1416 *dwBufferLen
= dwDataLen
- i
- 1;
1417 memmove(abBuffer
, abData
+ i
+ 1, *dwBufferLen
);
1421 /******************************************************************************
1422 * CPAcquireContext (RSAENH.@)
1424 * Acquire a handle to the key container specified by pszContainer
1427 * phProv [O] Pointer to the location the acquired handle will be written to.
1428 * pszContainer [I] Name of the desired key container. See Notes
1429 * dwFlags [I] Flags. See Notes.
1430 * pVTable [I] Pointer to a PVTableProvStruct containing callbacks.
1437 * If pszContainer is NULL or points to a zero length string the user's login
1438 * name will be used as the key container name.
1440 * If the CRYPT_NEW_KEYSET flag is set in dwFlags a new keyset will be created.
1441 * If a keyset with the given name already exists, the function fails and sets
1442 * last error to NTE_EXISTS. If CRYPT_NEW_KEYSET is not set and the specified
1443 * key container does not exist, function fails and sets last error to
1446 BOOL WINAPI
RSAENH_CPAcquireContext(HCRYPTPROV
*phProv
, LPSTR pszContainer
,
1447 DWORD dwFlags
, PVTableProvStruc pVTable
)
1449 CHAR szKeyContainerName
[MAX_PATH
];
1450 CHAR szRegKey
[MAX_PATH
];
1452 TRACE("(phProv=%p, pszContainer=%s, dwFlags=%08lx, pVTable=%p)\n", phProv
,
1453 debugstr_a(pszContainer
), dwFlags
, pVTable
);
1455 if (pszContainer
&& *pszContainer
)
1457 lstrcpynA(szKeyContainerName
, pszContainer
, MAX_PATH
);
1461 DWORD dwLen
= sizeof(szKeyContainerName
);
1462 if (!GetUserNameA(szKeyContainerName
, &dwLen
)) return FALSE
;
1465 switch (dwFlags
& (CRYPT_NEWKEYSET
|CRYPT_VERIFYCONTEXT
|CRYPT_DELETEKEYSET
))
1468 *phProv
= read_key_container(szKeyContainerName
, dwFlags
, pVTable
);
1471 case CRYPT_DELETEKEYSET
:
1472 if (snprintf(szRegKey
, MAX_PATH
, RSAENH_REGKEY
, pszContainer
) >= MAX_PATH
) {
1473 SetLastError(NTE_BAD_KEYSET_PARAM
);
1476 RegDeleteKeyA(HKEY_CURRENT_USER
, szRegKey
);
1477 SetLastError(ERROR_SUCCESS
);
1482 case CRYPT_NEWKEYSET
:
1483 *phProv
= read_key_container(szKeyContainerName
, dwFlags
, pVTable
);
1484 if (*phProv
!= (HCRYPTPROV
)INVALID_HANDLE_VALUE
)
1486 release_handle(&handle_table
, (unsigned int)*phProv
, RSAENH_MAGIC_CONTAINER
);
1487 SetLastError(NTE_EXISTS
);
1490 *phProv
= new_key_container(szKeyContainerName
, dwFlags
, pVTable
);
1493 case CRYPT_VERIFYCONTEXT
:
1495 SetLastError(NTE_BAD_FLAGS
);
1498 *phProv
= new_key_container("", dwFlags
, pVTable
);
1502 *phProv
= (unsigned int)INVALID_HANDLE_VALUE
;
1503 SetLastError(NTE_BAD_FLAGS
);
1507 if (*phProv
!= (unsigned int)INVALID_HANDLE_VALUE
) {
1508 SetLastError(ERROR_SUCCESS
);
1515 /******************************************************************************
1516 * CPCreateHash (RSAENH.@)
1518 * CPCreateHash creates and initalizes a new hash object.
1521 * hProv [I] Handle to the key container to which the new hash will belong.
1522 * Algid [I] Identifies the hash algorithm, which will be used for the hash.
1523 * hKey [I] Handle to a session key applied for keyed hashes.
1524 * dwFlags [I] Currently no flags defined. Must be zero.
1525 * phHash [O] Points to the location where a handle to the new hash will be stored.
1532 * hKey is a handle to a session key applied in keyed hashes like MAC and HMAC.
1533 * If a normal hash object is to be created (like e.g. MD2 or SHA1) hKey must be zero.
1535 BOOL WINAPI
RSAENH_CPCreateHash(HCRYPTPROV hProv
, ALG_ID Algid
, HCRYPTKEY hKey
, DWORD dwFlags
,
1538 CRYPTKEY
*pCryptKey
;
1539 CRYPTHASH
*pCryptHash
;
1540 const PROV_ENUMALGS_EX
*peaAlgidInfo
;
1542 TRACE("(hProv=%08lx, Algid=%08x, hKey=%08lx, dwFlags=%08lx, phHash=%p)\n", hProv
, Algid
, hKey
,
1545 peaAlgidInfo
= get_algid_info(hProv
, Algid
);
1546 if (!peaAlgidInfo
) return FALSE
;
1550 SetLastError(NTE_BAD_FLAGS
);
1554 if (Algid
== CALG_MAC
|| Algid
== CALG_HMAC
|| Algid
== CALG_SCHANNEL_MASTER_HASH
||
1555 Algid
== CALG_TLS1PRF
)
1557 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
)) {
1558 SetLastError(NTE_BAD_KEY
);
1562 if ((Algid
== CALG_MAC
) && (GET_ALG_TYPE(pCryptKey
->aiAlgid
) != ALG_TYPE_BLOCK
)) {
1563 SetLastError(NTE_BAD_KEY
);
1567 if ((Algid
== CALG_SCHANNEL_MASTER_HASH
|| Algid
== CALG_TLS1PRF
) &&
1568 (pCryptKey
->aiAlgid
!= CALG_TLS1_MASTER
))
1570 SetLastError(NTE_BAD_KEY
);
1574 if ((Algid
== CALG_TLS1PRF
) && (pCryptKey
->dwState
!= RSAENH_KEYSTATE_MASTERKEY
)) {
1575 SetLastError(NTE_BAD_KEY_STATE
);
1580 *phHash
= (HCRYPTHASH
)new_object(&handle_table
, sizeof(CRYPTHASH
), RSAENH_MAGIC_HASH
,
1581 destroy_hash
, (OBJECTHDR
**)&pCryptHash
);
1582 if (!pCryptHash
) return FALSE
;
1584 pCryptHash
->aiAlgid
= Algid
;
1585 pCryptHash
->hKey
= hKey
;
1586 pCryptHash
->hProv
= hProv
;
1587 pCryptHash
->dwState
= RSAENH_HASHSTATE_IDLE
;
1588 pCryptHash
->pHMACInfo
= (PHMAC_INFO
)NULL
;
1589 pCryptHash
->dwHashSize
= peaAlgidInfo
->dwDefaultLen
>> 3;
1590 init_data_blob(&pCryptHash
->tpPRFParams
.blobLabel
);
1591 init_data_blob(&pCryptHash
->tpPRFParams
.blobSeed
);
1593 if (Algid
== CALG_SCHANNEL_MASTER_HASH
) {
1594 static const char keyex
[] = "key expansion";
1595 BYTE key_expansion
[sizeof keyex
];
1596 CRYPT_DATA_BLOB blobRandom
, blobKeyExpansion
= { 13, key_expansion
};
1598 memcpy( key_expansion
, keyex
, sizeof keyex
);
1600 if (pCryptKey
->dwState
!= RSAENH_KEYSTATE_MASTERKEY
) {
1601 static const char msec
[] = "master secret";
1602 BYTE master_secret
[sizeof msec
];
1603 CRYPT_DATA_BLOB blobLabel
= { 13, master_secret
};
1604 BYTE abKeyValue
[48];
1606 memcpy( master_secret
, msec
, sizeof msec
);
1608 /* See RFC 2246, chapter 8.1 */
1609 if (!concat_data_blobs(&blobRandom
,
1610 &pCryptKey
->siSChannelInfo
.blobClientRandom
,
1611 &pCryptKey
->siSChannelInfo
.blobServerRandom
))
1615 tls1_prf(hProv
, hKey
, &blobLabel
, &blobRandom
, abKeyValue
, 48);
1616 pCryptKey
->dwState
= RSAENH_KEYSTATE_MASTERKEY
;
1617 memcpy(pCryptKey
->abKeyValue
, abKeyValue
, 48);
1618 free_data_blob(&blobRandom
);
1621 /* See RFC 2246, chapter 6.3 */
1622 if (!concat_data_blobs(&blobRandom
,
1623 &pCryptKey
->siSChannelInfo
.blobServerRandom
,
1624 &pCryptKey
->siSChannelInfo
.blobClientRandom
))
1628 tls1_prf(hProv
, hKey
, &blobKeyExpansion
, &blobRandom
, pCryptHash
->abHashValue
,
1629 RSAENH_MAX_HASH_SIZE
);
1630 free_data_blob(&blobRandom
);
1633 return init_hash(pCryptHash
);
1636 /******************************************************************************
1637 * CPDestroyHash (RSAENH.@)
1639 * Releases the handle to a hash object. The object is destroyed if it's reference
1640 * count reaches zero.
1643 * hProv [I] Handle to the key container to which the hash object belongs.
1644 * hHash [I] Handle to the hash object to be released.
1650 BOOL WINAPI
RSAENH_CPDestroyHash(HCRYPTPROV hProv
, HCRYPTHASH hHash
)
1652 TRACE("(hProv=%08lx, hHash=%08lx)\n", hProv
, hHash
);
1654 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
1656 SetLastError(NTE_BAD_UID
);
1660 if (!release_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
))
1662 SetLastError(NTE_BAD_HASH
);
1669 /******************************************************************************
1670 * CPDestroyKey (RSAENH.@)
1672 * Releases the handle to a key object. The object is destroyed if it's reference
1673 * count reaches zero.
1676 * hProv [I] Handle to the key container to which the key object belongs.
1677 * hKey [I] Handle to the key object to be released.
1683 BOOL WINAPI
RSAENH_CPDestroyKey(HCRYPTPROV hProv
, HCRYPTKEY hKey
)
1685 TRACE("(hProv=%08lx, hKey=%08lx)\n", hProv
, hKey
);
1687 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
1689 SetLastError(NTE_BAD_UID
);
1693 if (!release_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
))
1695 SetLastError(NTE_BAD_KEY
);
1702 /******************************************************************************
1703 * CPDuplicateHash (RSAENH.@)
1705 * Clones a hash object including it's current state.
1708 * hUID [I] Handle to the key container the hash belongs to.
1709 * hHash [I] Handle to the hash object to be cloned.
1710 * pdwReserved [I] Reserved. Must be NULL.
1711 * dwFlags [I] No flags are currently defined. Must be 0.
1712 * phHash [O] Handle to the cloned hash object.
1718 BOOL WINAPI
RSAENH_CPDuplicateHash(HCRYPTPROV hUID
, HCRYPTHASH hHash
, DWORD
*pdwReserved
,
1719 DWORD dwFlags
, HCRYPTHASH
*phHash
)
1721 CRYPTHASH
*pSrcHash
, *pDestHash
;
1723 TRACE("(hUID=%08lx, hHash=%08lx, pdwReserved=%p, dwFlags=%08lx, phHash=%p)\n", hUID
, hHash
,
1724 pdwReserved
, dwFlags
, phHash
);
1726 if (!is_valid_handle(&handle_table
, hUID
, RSAENH_MAGIC_CONTAINER
))
1728 SetLastError(NTE_BAD_UID
);
1732 if (!lookup_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
, (OBJECTHDR
**)&pSrcHash
))
1734 SetLastError(NTE_BAD_HASH
);
1738 if (!phHash
|| pdwReserved
|| dwFlags
)
1740 SetLastError(ERROR_INVALID_PARAMETER
);
1744 *phHash
= (HCRYPTHASH
)new_object(&handle_table
, sizeof(CRYPTHASH
), RSAENH_MAGIC_HASH
,
1745 destroy_hash
, (OBJECTHDR
**)&pDestHash
);
1746 if (*phHash
!= (HCRYPTHASH
)INVALID_HANDLE_VALUE
)
1748 memcpy(pDestHash
, pSrcHash
, sizeof(CRYPTHASH
));
1749 duplicate_hash_impl(pSrcHash
->aiAlgid
, &pSrcHash
->context
, &pDestHash
->context
);
1750 copy_hmac_info(&pDestHash
->pHMACInfo
, pSrcHash
->pHMACInfo
);
1751 copy_data_blob(&pDestHash
->tpPRFParams
.blobLabel
, &pSrcHash
->tpPRFParams
.blobLabel
);
1752 copy_data_blob(&pDestHash
->tpPRFParams
.blobSeed
, &pSrcHash
->tpPRFParams
.blobSeed
);
1755 return *phHash
!= (HCRYPTHASH
)INVALID_HANDLE_VALUE
;
1758 /******************************************************************************
1759 * CPDuplicateKey (RSAENH.@)
1761 * Clones a key object including it's current state.
1764 * hUID [I] Handle to the key container the hash belongs to.
1765 * hKey [I] Handle to the key object to be cloned.
1766 * pdwReserved [I] Reserved. Must be NULL.
1767 * dwFlags [I] No flags are currently defined. Must be 0.
1768 * phHash [O] Handle to the cloned key object.
1774 BOOL WINAPI
RSAENH_CPDuplicateKey(HCRYPTPROV hUID
, HCRYPTKEY hKey
, DWORD
*pdwReserved
,
1775 DWORD dwFlags
, HCRYPTKEY
*phKey
)
1777 CRYPTKEY
*pSrcKey
, *pDestKey
;
1779 TRACE("(hUID=%08lx, hKey=%08lx, pdwReserved=%p, dwFlags=%08lx, phKey=%p)\n", hUID
, hKey
,
1780 pdwReserved
, dwFlags
, phKey
);
1782 if (!is_valid_handle(&handle_table
, hUID
, RSAENH_MAGIC_CONTAINER
))
1784 SetLastError(NTE_BAD_UID
);
1788 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pSrcKey
))
1790 SetLastError(NTE_BAD_KEY
);
1794 if (!phKey
|| pdwReserved
|| dwFlags
)
1796 SetLastError(ERROR_INVALID_PARAMETER
);
1800 *phKey
= (HCRYPTKEY
)new_object(&handle_table
, sizeof(CRYPTKEY
), RSAENH_MAGIC_KEY
, destroy_key
,
1801 (OBJECTHDR
**)&pDestKey
);
1802 if (*phKey
!= (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
1804 memcpy(pDestKey
, pSrcKey
, sizeof(CRYPTKEY
));
1805 copy_data_blob(&pDestKey
->siSChannelInfo
.blobServerRandom
,
1806 &pSrcKey
->siSChannelInfo
.blobServerRandom
);
1807 copy_data_blob(&pDestKey
->siSChannelInfo
.blobClientRandom
,
1808 &pSrcKey
->siSChannelInfo
.blobClientRandom
);
1809 duplicate_key_impl(pSrcKey
->aiAlgid
, &pSrcKey
->context
, &pDestKey
->context
);
1818 /******************************************************************************
1819 * CPEncrypt (RSAENH.@)
1824 * hProv [I] The key container hKey and hHash belong to.
1825 * hKey [I] The key used to encrypt the data.
1826 * hHash [I] An optional hash object for parallel hashing. See notes.
1827 * Final [I] Indicates if this is the last block of data to encrypt.
1828 * dwFlags [I] Currently no flags defined. Must be zero.
1829 * pbData [I/O] Pointer to the data to encrypt. Encrypted data will also be stored there.
1830 * pdwDataLen [I/O] I: Length of data to encrypt, O: Length of encrypted data.
1831 * dwBufLen [I] Size of the buffer at pbData.
1838 * If a hash object handle is provided in hHash, it will be updated with the plaintext.
1839 * This is useful for message signatures.
1841 * This function uses the standard WINAPI protocol for querying data of dynamic length.
1843 BOOL WINAPI
RSAENH_CPEncrypt(HCRYPTPROV hProv
, HCRYPTKEY hKey
, HCRYPTHASH hHash
, BOOL Final
,
1844 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
, DWORD dwBufLen
)
1846 CRYPTKEY
*pCryptKey
;
1847 BYTE
*in
, out
[RSAENH_MAX_BLOCK_SIZE
], o
[RSAENH_MAX_BLOCK_SIZE
];
1848 DWORD dwEncryptedLen
, i
, j
, k
;
1850 TRACE("(hProv=%08lx, hKey=%08lx, hHash=%08lx, Final=%d, dwFlags=%08lx, pbData=%p, "
1851 "pdwDataLen=%p, dwBufLen=%ld)\n", hProv
, hKey
, hHash
, Final
, dwFlags
, pbData
, pdwDataLen
,
1854 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
1856 SetLastError(NTE_BAD_UID
);
1862 SetLastError(NTE_BAD_FLAGS
);
1866 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
1868 SetLastError(NTE_BAD_KEY
);
1872 if (pCryptKey
->dwState
== RSAENH_KEYSTATE_IDLE
)
1873 pCryptKey
->dwState
= RSAENH_KEYSTATE_ENCRYPTING
;
1875 if (pCryptKey
->dwState
!= RSAENH_KEYSTATE_ENCRYPTING
)
1877 SetLastError(NTE_BAD_DATA
);
1881 if (is_valid_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
)) {
1882 if (!RSAENH_CPHashData(hProv
, hHash
, pbData
, *pdwDataLen
, 0)) return FALSE
;
1885 if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_BLOCK
) {
1886 if (!Final
&& (*pdwDataLen
% pCryptKey
->dwBlockLen
)) {
1887 SetLastError(NTE_BAD_DATA
);
1891 dwEncryptedLen
= (*pdwDataLen
/pCryptKey
->dwBlockLen
+(Final
?1:0))*pCryptKey
->dwBlockLen
;
1893 if (pbData
== NULL
) {
1894 *pdwDataLen
= dwEncryptedLen
;
1898 for (i
=*pdwDataLen
; i
<dwEncryptedLen
&& i
<dwBufLen
; i
++) pbData
[i
] = dwEncryptedLen
- *pdwDataLen
;
1899 *pdwDataLen
= dwEncryptedLen
;
1901 if (*pdwDataLen
> dwBufLen
)
1903 SetLastError(ERROR_MORE_DATA
);
1907 for (i
=0, in
=pbData
; i
<*pdwDataLen
; i
+=pCryptKey
->dwBlockLen
, in
+=pCryptKey
->dwBlockLen
) {
1908 switch (pCryptKey
->dwMode
) {
1909 case CRYPT_MODE_ECB
:
1910 encrypt_block_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, in
, out
,
1914 case CRYPT_MODE_CBC
:
1915 for (j
=0; j
<pCryptKey
->dwBlockLen
; j
++) in
[j
] ^= pCryptKey
->abChainVector
[j
];
1916 encrypt_block_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, in
, out
,
1918 memcpy(pCryptKey
->abChainVector
, out
, pCryptKey
->dwBlockLen
);
1921 case CRYPT_MODE_CFB
:
1922 for (j
=0; j
<pCryptKey
->dwBlockLen
; j
++) {
1923 encrypt_block_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
,
1924 pCryptKey
->abChainVector
, o
, RSAENH_ENCRYPT
);
1925 out
[j
] = in
[j
] ^ o
[0];
1926 for (k
=0; k
<pCryptKey
->dwBlockLen
-1; k
++)
1927 pCryptKey
->abChainVector
[k
] = pCryptKey
->abChainVector
[k
+1];
1928 pCryptKey
->abChainVector
[k
] = out
[j
];
1933 SetLastError(NTE_BAD_ALGID
);
1936 memcpy(in
, out
, pCryptKey
->dwBlockLen
);
1938 } else if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_STREAM
) {
1939 encrypt_stream_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pbData
, *pdwDataLen
);
1940 } else if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_RSA
) {
1941 if (pCryptKey
->aiAlgid
== CALG_RSA_SIGN
) {
1942 SetLastError(NTE_BAD_KEY
);
1945 if (dwBufLen
< pCryptKey
->dwBlockLen
) {
1946 SetLastError(ERROR_MORE_DATA
);
1949 if (!pad_data(pbData
, *pdwDataLen
, pbData
, pCryptKey
->dwBlockLen
, dwFlags
)) return FALSE
;
1950 encrypt_block_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pbData
, pbData
, RSAENH_ENCRYPT
);
1951 *pdwDataLen
= pCryptKey
->dwBlockLen
;
1954 SetLastError(NTE_BAD_TYPE
);
1958 if (Final
) setup_key(pCryptKey
);
1963 /******************************************************************************
1964 * CPDecrypt (RSAENH.@)
1969 * hProv [I] The key container hKey and hHash belong to.
1970 * hKey [I] The key used to decrypt the data.
1971 * hHash [I] An optional hash object for parallel hashing. See notes.
1972 * Final [I] Indicates if this is the last block of data to decrypt.
1973 * dwFlags [I] Currently no flags defined. Must be zero.
1974 * pbData [I/O] Pointer to the data to decrypt. Plaintext will also be stored there.
1975 * pdwDataLen [I/O] I: Length of ciphertext, O: Length of plaintext.
1982 * If a hash object handle is provided in hHash, it will be updated with the plaintext.
1983 * This is useful for message signatures.
1985 * This function uses the standard WINAPI protocol for querying data of dynamic length.
1987 BOOL WINAPI
RSAENH_CPDecrypt(HCRYPTPROV hProv
, HCRYPTKEY hKey
, HCRYPTHASH hHash
, BOOL Final
,
1988 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
)
1990 CRYPTKEY
*pCryptKey
;
1991 BYTE
*in
, out
[RSAENH_MAX_BLOCK_SIZE
], o
[RSAENH_MAX_BLOCK_SIZE
];
1995 TRACE("(hProv=%08lx, hKey=%08lx, hHash=%08lx, Final=%d, dwFlags=%08lx, pbData=%p, "
1996 "pdwDataLen=%p)\n", hProv
, hKey
, hHash
, Final
, dwFlags
, pbData
, pdwDataLen
);
1998 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
2000 SetLastError(NTE_BAD_UID
);
2006 SetLastError(NTE_BAD_FLAGS
);
2010 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
2012 SetLastError(NTE_BAD_KEY
);
2016 if (pCryptKey
->dwState
== RSAENH_KEYSTATE_IDLE
)
2017 pCryptKey
->dwState
= RSAENH_KEYSTATE_DECRYPTING
;
2019 if (pCryptKey
->dwState
!= RSAENH_KEYSTATE_DECRYPTING
)
2021 SetLastError(NTE_BAD_DATA
);
2027 if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_BLOCK
) {
2028 for (i
=0, in
=pbData
; i
<*pdwDataLen
; i
+=pCryptKey
->dwBlockLen
, in
+=pCryptKey
->dwBlockLen
) {
2029 switch (pCryptKey
->dwMode
) {
2030 case CRYPT_MODE_ECB
:
2031 encrypt_block_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, in
, out
,
2035 case CRYPT_MODE_CBC
:
2036 encrypt_block_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, in
, out
,
2038 for (j
=0; j
<pCryptKey
->dwBlockLen
; j
++) out
[j
] ^= pCryptKey
->abChainVector
[j
];
2039 memcpy(pCryptKey
->abChainVector
, in
, pCryptKey
->dwBlockLen
);
2042 case CRYPT_MODE_CFB
:
2043 for (j
=0; j
<pCryptKey
->dwBlockLen
; j
++) {
2044 encrypt_block_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
,
2045 pCryptKey
->abChainVector
, o
, RSAENH_ENCRYPT
);
2046 out
[j
] = in
[j
] ^ o
[0];
2047 for (k
=0; k
<pCryptKey
->dwBlockLen
-1; k
++)
2048 pCryptKey
->abChainVector
[k
] = pCryptKey
->abChainVector
[k
+1];
2049 pCryptKey
->abChainVector
[k
] = in
[j
];
2054 SetLastError(NTE_BAD_ALGID
);
2057 memcpy(in
, out
, pCryptKey
->dwBlockLen
);
2059 if (Final
) *pdwDataLen
-= pbData
[*pdwDataLen
-1];
2061 } else if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_STREAM
) {
2062 encrypt_stream_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pbData
, *pdwDataLen
);
2063 } else if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_RSA
) {
2064 if (pCryptKey
->aiAlgid
== CALG_RSA_SIGN
) {
2065 SetLastError(NTE_BAD_KEY
);
2068 encrypt_block_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pbData
, pbData
, RSAENH_DECRYPT
);
2069 if (!unpad_data(pbData
, pCryptKey
->dwBlockLen
, pbData
, pdwDataLen
, dwFlags
)) return FALSE
;
2072 SetLastError(NTE_BAD_TYPE
);
2076 if (Final
) setup_key(pCryptKey
);
2078 if (is_valid_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
)) {
2079 if (*pdwDataLen
>dwMax
||
2080 !RSAENH_CPHashData(hProv
, hHash
, pbData
, *pdwDataLen
, 0)) return FALSE
;
2086 /******************************************************************************
2087 * CPExportKey (RSAENH.@)
2089 * Export a key into a binary large object (BLOB).
2092 * hProv [I] Key container from which a key is to be exported.
2093 * hKey [I] Key to be exported.
2094 * hPubKey [I] Key used to encrypt sensitive BLOB data.
2095 * dwBlobType [I] SIMPLEBLOB, PUBLICKEYBLOB or PRIVATEKEYBLOB.
2096 * dwFlags [I] Currently none defined.
2097 * pbData [O] Pointer to a buffer where the BLOB will be written to.
2098 * pdwDataLen [I/O] I: Size of buffer at pbData, O: Size of BLOB
2104 BOOL WINAPI
RSAENH_CPExportKey(HCRYPTPROV hProv
, HCRYPTKEY hKey
, HCRYPTKEY hPubKey
,
2105 DWORD dwBlobType
, DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
)
2107 CRYPTKEY
*pCryptKey
, *pPubKey
;
2108 BLOBHEADER
*pBlobHeader
= (BLOBHEADER
*)pbData
;
2109 RSAPUBKEY
*pRSAPubKey
= (RSAPUBKEY
*)(pBlobHeader
+1);
2110 ALG_ID
*pAlgid
= (ALG_ID
*)(pBlobHeader
+1);
2113 TRACE("(hProv=%08lx, hKey=%08lx, hPubKey=%08lx, dwBlobType=%08lx, dwFlags=%08lx, pbData=%p,"
2114 "pdwDataLen=%p)\n", hProv
, hKey
, hPubKey
, dwBlobType
, dwFlags
, pbData
, pdwDataLen
);
2116 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
2118 SetLastError(NTE_BAD_UID
);
2122 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
2124 SetLastError(NTE_BAD_KEY
);
2128 if (dwFlags
& CRYPT_SSL2_FALLBACK
) {
2129 if (pCryptKey
->aiAlgid
!= CALG_SSL2_MASTER
) {
2130 SetLastError(NTE_BAD_KEY
);
2135 switch ((BYTE
)dwBlobType
)
2138 if (!lookup_handle(&handle_table
, hPubKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pPubKey
)){
2139 SetLastError(NTE_BAD_PUBLIC_KEY
); /* FIXME: error_code? */
2143 if (!(GET_ALG_CLASS(pCryptKey
->aiAlgid
)&(ALG_CLASS_DATA_ENCRYPT
|ALG_CLASS_MSG_ENCRYPT
))) {
2144 SetLastError(NTE_BAD_KEY
); /* FIXME: error code? */
2148 dwDataLen
= sizeof(BLOBHEADER
) + sizeof(ALG_ID
) + pPubKey
->dwBlockLen
;
2150 if (*pdwDataLen
< dwDataLen
) {
2151 SetLastError(ERROR_MORE_DATA
);
2152 *pdwDataLen
= dwDataLen
;
2156 pBlobHeader
->bType
= SIMPLEBLOB
;
2157 pBlobHeader
->bVersion
= CUR_BLOB_VERSION
;
2158 pBlobHeader
->reserved
= 0;
2159 pBlobHeader
->aiKeyAlg
= pCryptKey
->aiAlgid
;
2161 *pAlgid
= pPubKey
->aiAlgid
;
2163 if (!pad_data(pCryptKey
->abKeyValue
, pCryptKey
->dwKeyLen
, (BYTE
*)(pAlgid
+1),
2164 pPubKey
->dwBlockLen
, dwFlags
))
2169 encrypt_block_impl(pPubKey
->aiAlgid
, &pPubKey
->context
, (BYTE
*)(pAlgid
+1),
2170 (BYTE
*)(pAlgid
+1), RSAENH_ENCRYPT
);
2172 *pdwDataLen
= dwDataLen
;
2176 if (is_valid_handle(&handle_table
, hPubKey
, RSAENH_MAGIC_KEY
)) {
2177 SetLastError(NTE_BAD_KEY
); /* FIXME: error code? */
2181 if ((pCryptKey
->aiAlgid
!= CALG_RSA_KEYX
) && (pCryptKey
->aiAlgid
!= CALG_RSA_SIGN
)) {
2182 SetLastError(NTE_BAD_KEY
);
2186 dwDataLen
= sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) + pCryptKey
->dwKeyLen
;
2188 if (*pdwDataLen
< dwDataLen
) {
2189 SetLastError(ERROR_MORE_DATA
);
2190 *pdwDataLen
= dwDataLen
;
2194 pBlobHeader
->bType
= PUBLICKEYBLOB
;
2195 pBlobHeader
->bVersion
= CUR_BLOB_VERSION
;
2196 pBlobHeader
->reserved
= 0;
2197 pBlobHeader
->aiKeyAlg
= pCryptKey
->aiAlgid
;
2199 pRSAPubKey
->magic
= RSAENH_MAGIC_RSA1
;
2200 pRSAPubKey
->bitlen
= pCryptKey
->dwKeyLen
<< 3;
2202 export_public_key_impl((BYTE
*)(pRSAPubKey
+1), &pCryptKey
->context
,
2203 pCryptKey
->dwKeyLen
, &pRSAPubKey
->pubexp
);
2205 *pdwDataLen
= dwDataLen
;
2208 case PRIVATEKEYBLOB
:
2209 if ((pCryptKey
->aiAlgid
!= CALG_RSA_KEYX
) && (pCryptKey
->aiAlgid
!= CALG_RSA_SIGN
)) {
2210 SetLastError(NTE_BAD_KEY
);
2214 dwDataLen
= sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) +
2215 2 * pCryptKey
->dwKeyLen
+ 5 * ((pCryptKey
->dwKeyLen
+ 1) >> 1);
2217 if (*pdwDataLen
< dwDataLen
) {
2218 SetLastError(ERROR_MORE_DATA
);
2219 *pdwDataLen
= dwDataLen
;
2223 pBlobHeader
->bType
= PRIVATEKEYBLOB
;
2224 pBlobHeader
->bVersion
= CUR_BLOB_VERSION
;
2225 pBlobHeader
->reserved
= 0;
2226 pBlobHeader
->aiKeyAlg
= pCryptKey
->aiAlgid
;
2228 pRSAPubKey
->magic
= RSAENH_MAGIC_RSA2
;
2229 pRSAPubKey
->bitlen
= pCryptKey
->dwKeyLen
<< 3;
2231 export_private_key_impl((BYTE
*)(pRSAPubKey
+1), &pCryptKey
->context
,
2232 pCryptKey
->dwKeyLen
, &pRSAPubKey
->pubexp
);
2234 *pdwDataLen
= dwDataLen
;
2238 SetLastError(NTE_BAD_TYPE
); /* FIXME: error code? */
2243 /******************************************************************************
2244 * CPImportKey (RSAENH.@)
2246 * Import a BLOB'ed key into a key container.
2249 * hProv [I] Key container into which the key is to be imported.
2250 * pbData [I] Pointer to a buffer which holds the BLOB.
2251 * dwDataLen [I] Length of data in buffer at pbData.
2252 * hPubKey [I] Key used to decrypt sensitive BLOB data.
2253 * dwFlags [I] Currently none defined.
2254 * phKey [O] Handle to the imported key.
2260 BOOL WINAPI
RSAENH_CPImportKey(HCRYPTPROV hProv
, CONST BYTE
*pbData
, DWORD dwDataLen
,
2261 HCRYPTKEY hPubKey
, DWORD dwFlags
, HCRYPTKEY
*phKey
)
2263 CRYPTKEY
*pCryptKey
, *pPubKey
;
2264 CONST BLOBHEADER
*pBlobHeader
= (CONST BLOBHEADER
*)pbData
;
2265 CONST RSAPUBKEY
*pRSAPubKey
= (CONST RSAPUBKEY
*)(pBlobHeader
+1);
2266 CONST ALG_ID
*pAlgid
= (CONST ALG_ID
*)(pBlobHeader
+1);
2267 CONST BYTE
*pbKeyStream
= (CONST BYTE
*)(pAlgid
+ 1);
2272 TRACE("(hProv=%08lx, pbData=%p, dwDataLen=%ld, hPubKey=%08lx, dwFlags=%08lx, phKey=%p)\n",
2273 hProv
, pbData
, dwDataLen
, hPubKey
, dwFlags
, phKey
);
2275 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
2277 SetLastError(NTE_BAD_UID
);
2281 if (dwDataLen
< sizeof(BLOBHEADER
) ||
2282 pBlobHeader
->bVersion
!= CUR_BLOB_VERSION
||
2283 pBlobHeader
->reserved
!= 0)
2285 SetLastError(NTE_BAD_DATA
);
2289 switch (pBlobHeader
->bType
)
2291 case PRIVATEKEYBLOB
:
2292 if ((dwDataLen
< sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
)) ||
2293 (pRSAPubKey
->magic
!= RSAENH_MAGIC_RSA2
) ||
2294 (dwDataLen
< sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) +
2295 (2 * pRSAPubKey
->bitlen
>> 3) + (5 * ((pRSAPubKey
->bitlen
+8)>>4))))
2297 SetLastError(NTE_BAD_DATA
);
2301 *phKey
= new_key(hProv
, pBlobHeader
->aiKeyAlg
, MAKELONG(0,pRSAPubKey
->bitlen
), &pCryptKey
);
2302 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
2303 setup_key(pCryptKey
);
2304 return import_private_key_impl((CONST BYTE
*)(pRSAPubKey
+1), &pCryptKey
->context
,
2305 pRSAPubKey
->bitlen
/8, pRSAPubKey
->pubexp
);
2308 if ((dwDataLen
< sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
)) ||
2309 (pRSAPubKey
->magic
!= RSAENH_MAGIC_RSA1
) ||
2310 (dwDataLen
< sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) + (pRSAPubKey
->bitlen
>> 3)))
2312 SetLastError(NTE_BAD_DATA
);
2316 /* Since this is a public key blob, only the public key is
2317 * available, so only signature verification is possible.
2319 algID
= pBlobHeader
->aiKeyAlg
;
2320 if (algID
== CALG_RSA_KEYX
)
2321 algID
= CALG_RSA_SIGN
;
2322 *phKey
= new_key(hProv
, algID
, MAKELONG(0,pRSAPubKey
->bitlen
), &pCryptKey
);
2323 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
2324 setup_key(pCryptKey
);
2325 return import_public_key_impl((CONST BYTE
*)(pRSAPubKey
+1), &pCryptKey
->context
,
2326 pRSAPubKey
->bitlen
>> 3, pRSAPubKey
->pubexp
);
2329 if (!lookup_handle(&handle_table
, hPubKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pPubKey
) ||
2330 pPubKey
->aiAlgid
!= CALG_RSA_KEYX
)
2332 SetLastError(NTE_BAD_PUBLIC_KEY
); /* FIXME: error code? */
2336 if (dwDataLen
< sizeof(BLOBHEADER
)+sizeof(ALG_ID
)+pPubKey
->dwBlockLen
)
2338 SetLastError(NTE_BAD_DATA
); /* FIXME: error code */
2342 pbDecrypted
= HeapAlloc(GetProcessHeap(), 0, pPubKey
->dwBlockLen
);
2343 if (!pbDecrypted
) return FALSE
;
2344 encrypt_block_impl(pPubKey
->aiAlgid
, &pPubKey
->context
, pbKeyStream
, pbDecrypted
,
2347 dwKeyLen
= RSAENH_MAX_KEY_SIZE
;
2348 if (!unpad_data(pbDecrypted
, pPubKey
->dwBlockLen
, pbDecrypted
, &dwKeyLen
, dwFlags
)) {
2349 HeapFree(GetProcessHeap(), 0, pbDecrypted
);
2353 *phKey
= new_key(hProv
, pBlobHeader
->aiKeyAlg
, dwKeyLen
<<19, &pCryptKey
);
2354 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
2356 HeapFree(GetProcessHeap(), 0, pbDecrypted
);
2359 memcpy(pCryptKey
->abKeyValue
, pbDecrypted
, dwKeyLen
);
2360 HeapFree(GetProcessHeap(), 0, pbDecrypted
);
2361 setup_key(pCryptKey
);
2365 SetLastError(NTE_BAD_TYPE
); /* FIXME: error code? */
2370 /******************************************************************************
2371 * CPGenKey (RSAENH.@)
2373 * Generate a key in the key container
2376 * hProv [I] Key container for which a key is to be generated.
2377 * Algid [I] Crypto algorithm identifier for the key to be generated.
2378 * dwFlags [I] Upper 16 bits: Binary length of key. Lower 16 bits: Flags. See Notes
2379 * phKey [O] Handle to the generated key.
2386 * Flags currently not considered.
2389 * Private key-exchange- and signature-keys can be generated with Algid AT_KEYEXCHANGE
2390 * and AT_SIGNATURE values.
2392 BOOL WINAPI
RSAENH_CPGenKey(HCRYPTPROV hProv
, ALG_ID Algid
, DWORD dwFlags
, HCRYPTKEY
*phKey
)
2394 KEYCONTAINER
*pKeyContainer
;
2395 CRYPTKEY
*pCryptKey
;
2397 TRACE("(hProv=%08lx, aiAlgid=%d, dwFlags=%08lx, phKey=%p)\n", hProv
, Algid
, dwFlags
, phKey
);
2399 if (!lookup_handle(&handle_table
, (unsigned int)hProv
, RSAENH_MAGIC_CONTAINER
,
2400 (OBJECTHDR
**)&pKeyContainer
))
2402 /* MSDN: hProv not containing valid context handle */
2403 SetLastError(NTE_BAD_UID
);
2411 *phKey
= new_key(hProv
, CALG_RSA_SIGN
, dwFlags
, &pCryptKey
);
2413 new_key_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pCryptKey
->dwKeyLen
);
2414 setup_key(pCryptKey
);
2415 if (Algid
== AT_SIGNATURE
) {
2416 RSAENH_CPDestroyKey(hProv
, pKeyContainer
->hSignatureKeyPair
);
2417 copy_handle(&handle_table
, *phKey
, RSAENH_MAGIC_KEY
,
2418 (unsigned int*)&pKeyContainer
->hSignatureKeyPair
);
2423 case AT_KEYEXCHANGE
:
2425 *phKey
= new_key(hProv
, CALG_RSA_KEYX
, dwFlags
, &pCryptKey
);
2427 new_key_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pCryptKey
->dwKeyLen
);
2428 setup_key(pCryptKey
);
2429 if (Algid
== AT_KEYEXCHANGE
) {
2430 RSAENH_CPDestroyKey(hProv
, pKeyContainer
->hKeyExchangeKeyPair
);
2431 copy_handle(&handle_table
, *phKey
, RSAENH_MAGIC_KEY
,
2432 (unsigned int*)&pKeyContainer
->hKeyExchangeKeyPair
);
2442 case CALG_PCT1_MASTER
:
2443 case CALG_SSL2_MASTER
:
2444 case CALG_SSL3_MASTER
:
2445 case CALG_TLS1_MASTER
:
2446 *phKey
= new_key(hProv
, Algid
, dwFlags
, &pCryptKey
);
2448 gen_rand_impl(pCryptKey
->abKeyValue
, RSAENH_MAX_KEY_SIZE
);
2450 case CALG_SSL3_MASTER
:
2451 pCryptKey
->abKeyValue
[0] = RSAENH_SSL3_VERSION_MAJOR
;
2452 pCryptKey
->abKeyValue
[1] = RSAENH_SSL3_VERSION_MINOR
;
2455 case CALG_TLS1_MASTER
:
2456 pCryptKey
->abKeyValue
[0] = RSAENH_TLS1_VERSION_MAJOR
;
2457 pCryptKey
->abKeyValue
[1] = RSAENH_TLS1_VERSION_MINOR
;
2460 setup_key(pCryptKey
);
2465 /* MSDN: Algorithm not supported specified by Algid */
2466 SetLastError(NTE_BAD_ALGID
);
2470 return *phKey
!= (unsigned int)INVALID_HANDLE_VALUE
;
2473 /******************************************************************************
2474 * CPGenRandom (RSAENH.@)
2476 * Generate a random byte stream.
2479 * hProv [I] Key container that is used to generate random bytes.
2480 * dwLen [I] Specifies the number of requested random data bytes.
2481 * pbBuffer [O] Random bytes will be stored here.
2487 BOOL WINAPI
RSAENH_CPGenRandom(HCRYPTPROV hProv
, DWORD dwLen
, BYTE
*pbBuffer
)
2489 TRACE("(hProv=%08lx, dwLen=%ld, pbBuffer=%p)\n", hProv
, dwLen
, pbBuffer
);
2491 if (!is_valid_handle(&handle_table
, (unsigned int)hProv
, RSAENH_MAGIC_CONTAINER
))
2493 /* MSDN: hProv not containing valid context handle */
2494 SetLastError(NTE_BAD_UID
);
2498 return gen_rand_impl(pbBuffer
, dwLen
);
2501 /******************************************************************************
2502 * CPGetHashParam (RSAENH.@)
2504 * Query parameters of an hash object.
2507 * hProv [I] The kea container, which the hash belongs to.
2508 * hHash [I] The hash object that is to be queried.
2509 * dwParam [I] Specifies the parameter that is to be queried.
2510 * pbData [I] Pointer to the buffer where the parameter value will be stored.
2511 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
2512 * dwFlags [I] None currently defined.
2519 * Valid dwParams are: HP_ALGID, HP_HASHSIZE, HP_HASHVALUE. The hash will be
2520 * finalized if HP_HASHVALUE is queried.
2522 BOOL WINAPI
RSAENH_CPGetHashParam(HCRYPTPROV hProv
, HCRYPTHASH hHash
, DWORD dwParam
, BYTE
*pbData
,
2523 DWORD
*pdwDataLen
, DWORD dwFlags
)
2525 CRYPTHASH
*pCryptHash
;
2527 TRACE("(hProv=%08lx, hHash=%08lx, dwParam=%08lx, pbData=%p, pdwDataLen=%p, dwFlags=%08lx)\n",
2528 hProv
, hHash
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
2530 if (!is_valid_handle(&handle_table
, (unsigned int)hProv
, RSAENH_MAGIC_CONTAINER
))
2532 SetLastError(NTE_BAD_UID
);
2538 SetLastError(NTE_BAD_FLAGS
);
2542 if (!lookup_handle(&handle_table
, (unsigned int)hHash
, RSAENH_MAGIC_HASH
,
2543 (OBJECTHDR
**)&pCryptHash
))
2545 SetLastError(NTE_BAD_HASH
);
2551 SetLastError(ERROR_INVALID_PARAMETER
);
2558 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&pCryptHash
->aiAlgid
,
2562 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&pCryptHash
->dwHashSize
,
2566 if (pCryptHash
->aiAlgid
== CALG_TLS1PRF
) {
2567 return tls1_prf(hProv
, pCryptHash
->hKey
, &pCryptHash
->tpPRFParams
.blobLabel
,
2568 &pCryptHash
->tpPRFParams
.blobSeed
, pbData
, *pdwDataLen
);
2571 if ( pbData
== NULL
) {
2572 *pdwDataLen
= pCryptHash
->dwHashSize
;
2576 if (pCryptHash
->dwState
== RSAENH_HASHSTATE_IDLE
) {
2577 SetLastError(NTE_BAD_HASH_STATE
);
2581 if (pbData
&& (pCryptHash
->dwState
!= RSAENH_HASHSTATE_FINISHED
))
2583 finalize_hash(pCryptHash
);
2584 pCryptHash
->dwState
= RSAENH_HASHSTATE_FINISHED
;
2587 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)pCryptHash
->abHashValue
,
2588 pCryptHash
->dwHashSize
);
2591 SetLastError(NTE_BAD_TYPE
);
2596 /******************************************************************************
2597 * CPSetKeyParam (RSAENH.@)
2599 * Set a parameter of a key object
2602 * hProv [I] The key container to which the key belongs.
2603 * hKey [I] The key for which a parameter is to be set.
2604 * dwParam [I] Parameter type. See Notes.
2605 * pbData [I] Pointer to the parameter value.
2606 * dwFlags [I] Currently none defined.
2613 * Defined dwParam types are:
2614 * - KP_MODE: Values MODE_CBC, MODE_ECB, MODE_CFB.
2615 * - KP_MODE_BITS: Shift width for cipher feedback mode. (Currently ignored by MS CSP's)
2616 * - KP_PERMISSIONS: Or'ed combination of CRYPT_ENCRYPT, CRYPT_DECRYPT,
2617 * CRYPT_EXPORT, CRYPT_READ, CRYPT_WRITE, CRYPT_MAC
2618 * - KP_IV: Initialization vector
2620 BOOL WINAPI
RSAENH_CPSetKeyParam(HCRYPTPROV hProv
, HCRYPTKEY hKey
, DWORD dwParam
, BYTE
*pbData
,
2623 CRYPTKEY
*pCryptKey
;
2625 TRACE("(hProv=%08lx, hKey=%08lx, dwParam=%08lx, pbData=%p, dwFlags=%08lx)\n", hProv
, hKey
,
2626 dwParam
, pbData
, dwFlags
);
2628 if (!is_valid_handle(&handle_table
, (unsigned int)hProv
, RSAENH_MAGIC_CONTAINER
))
2630 SetLastError(NTE_BAD_UID
);
2635 SetLastError(NTE_BAD_FLAGS
);
2639 if (!lookup_handle(&handle_table
, (unsigned int)hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
2641 SetLastError(NTE_BAD_KEY
);
2647 pCryptKey
->dwMode
= *(DWORD
*)pbData
;
2651 pCryptKey
->dwModeBits
= *(DWORD
*)pbData
;
2654 case KP_PERMISSIONS
:
2655 pCryptKey
->dwPermissions
= *(DWORD
*)pbData
;
2659 memcpy(pCryptKey
->abInitVector
, pbData
, pCryptKey
->dwBlockLen
);
2662 case KP_SCHANNEL_ALG
:
2663 switch (((PSCHANNEL_ALG
)pbData
)->dwUse
) {
2664 case SCHANNEL_ENC_KEY
:
2665 memcpy(&pCryptKey
->siSChannelInfo
.saEncAlg
, pbData
, sizeof(SCHANNEL_ALG
));
2668 case SCHANNEL_MAC_KEY
:
2669 memcpy(&pCryptKey
->siSChannelInfo
.saMACAlg
, pbData
, sizeof(SCHANNEL_ALG
));
2673 SetLastError(NTE_FAIL
); /* FIXME: error code */
2678 case KP_CLIENT_RANDOM
:
2679 return copy_data_blob(&pCryptKey
->siSChannelInfo
.blobClientRandom
, (PCRYPT_DATA_BLOB
)pbData
);
2681 case KP_SERVER_RANDOM
:
2682 return copy_data_blob(&pCryptKey
->siSChannelInfo
.blobServerRandom
, (PCRYPT_DATA_BLOB
)pbData
);
2685 SetLastError(NTE_BAD_TYPE
);
2690 /******************************************************************************
2691 * CPGetKeyParam (RSAENH.@)
2693 * Query a key parameter.
2696 * hProv [I] The key container, which the key belongs to.
2697 * hHash [I] The key object that is to be queried.
2698 * dwParam [I] Specifies the parameter that is to be queried.
2699 * pbData [I] Pointer to the buffer where the parameter value will be stored.
2700 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
2701 * dwFlags [I] None currently defined.
2708 * Defined dwParam types are:
2709 * - KP_MODE: Values MODE_CBC, MODE_ECB, MODE_CFB.
2710 * - KP_MODE_BITS: Shift width for cipher feedback mode.
2711 * (Currently ignored by MS CSP's - always eight)
2712 * - KP_PERMISSIONS: Or'ed combination of CRYPT_ENCRYPT, CRYPT_DECRYPT,
2713 * CRYPT_EXPORT, CRYPT_READ, CRYPT_WRITE, CRYPT_MAC
2714 * - KP_IV: Initialization vector.
2715 * - KP_KEYLEN: Bitwidth of the key.
2716 * - KP_BLOCKLEN: Size of a block cipher block.
2717 * - KP_SALT: Salt value.
2719 BOOL WINAPI
RSAENH_CPGetKeyParam(HCRYPTPROV hProv
, HCRYPTKEY hKey
, DWORD dwParam
, BYTE
*pbData
,
2720 DWORD
*pdwDataLen
, DWORD dwFlags
)
2722 CRYPTKEY
*pCryptKey
;
2725 TRACE("(hProv=%08lx, hKey=%08lx, dwParam=%08lx, pbData=%p, pdwDataLen=%p dwFlags=%08lx)\n",
2726 hProv
, hKey
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
2728 if (!is_valid_handle(&handle_table
, (unsigned int)hProv
, RSAENH_MAGIC_CONTAINER
))
2730 SetLastError(NTE_BAD_UID
);
2735 SetLastError(NTE_BAD_FLAGS
);
2739 if (!lookup_handle(&handle_table
, (unsigned int)hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
2741 SetLastError(NTE_BAD_KEY
);
2748 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)pCryptKey
->abInitVector
,
2749 pCryptKey
->dwBlockLen
);
2752 return copy_param(pbData
, pdwDataLen
,
2753 (CONST BYTE
*)&pCryptKey
->abKeyValue
[pCryptKey
->dwKeyLen
], pCryptKey
->dwSaltLen
);
2756 dwBitLen
= pCryptKey
->dwKeyLen
<< 3;
2757 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwBitLen
, sizeof(DWORD
));
2760 dwBitLen
= pCryptKey
->dwBlockLen
<< 3;
2761 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwBitLen
, sizeof(DWORD
));
2764 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&pCryptKey
->dwMode
, sizeof(DWORD
));
2767 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&pCryptKey
->dwModeBits
,
2770 case KP_PERMISSIONS
:
2771 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&pCryptKey
->dwPermissions
,
2775 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&pCryptKey
->aiAlgid
, sizeof(DWORD
));
2778 SetLastError(NTE_BAD_TYPE
);
2783 /******************************************************************************
2784 * CPGetProvParam (RSAENH.@)
2786 * Query a CSP parameter.
2789 * hProv [I] The key container that is to be queried.
2790 * dwParam [I] Specifies the parameter that is to be queried.
2791 * pbData [I] Pointer to the buffer where the parameter value will be stored.
2792 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
2793 * dwFlags [I] CRYPT_FIRST: Start enumeration (for PP_ENUMALGS{_EX}).
2799 * Defined dwParam types:
2800 * - PP_CONTAINER: Name of the key container.
2801 * - PP_NAME: Name of the cryptographic service provider.
2802 * - PP_SIG_KEYSIZE_INC: RSA signature keywidth granularity in bits.
2803 * - PP_KEYX_KEYSIZE_INC: RSA key-exchange keywidth granularity in bits.
2804 * - PP_ENUMALGS{_EX}: Query provider capabilities.
2806 BOOL WINAPI
RSAENH_CPGetProvParam(HCRYPTPROV hProv
, DWORD dwParam
, BYTE
*pbData
,
2807 DWORD
*pdwDataLen
, DWORD dwFlags
)
2809 KEYCONTAINER
*pKeyContainer
;
2810 PROV_ENUMALGS provEnumalgs
;
2812 CHAR szRSABase
[MAX_PATH
];
2813 HKEY hKey
, hRootKey
;
2815 /* This is for dwParam 41, which does not seem to be documented
2816 * on MSDN. IE6 SP1 asks for it in the 'About' dialog, however.
2817 * Returning this BLOB seems to satisfy IE. The marked 0x00 seem
2818 * to be 'don't care's. If you know anything more specific about
2819 * provider parameter 41, please report to wine-devel@winehq.org */
2820 static CONST BYTE abWTF
[96] = {
2821 0xb0, 0x25, 0x63, 0x86, 0x9c, 0xab, 0xb6, 0x37,
2822 0xe8, 0x82, /**/0x00,/**/ 0x72, 0x06, 0xb2, /**/0x00,/**/ 0x3b,
2823 0x60, 0x35, /**/0x00,/**/ 0x3b, 0x88, 0xce, /**/0x00,/**/ 0x82,
2824 0xbc, 0x7a, /**/0x00,/**/ 0xb7, 0x4f, 0x7e, /**/0x00,/**/ 0xde,
2825 0x92, 0xf1, /**/0x00,/**/ 0x83, 0xea, 0x5e, /**/0x00,/**/ 0xc8,
2826 0x12, 0x1e, 0xd4, 0x06, 0xf7, 0x66, /**/0x00,/**/ 0x01,
2827 0x29, 0xa4, /**/0x00,/**/ 0xf8, 0x24, 0x0c, /**/0x00,/**/ 0x33,
2828 0x06, 0x80, /**/0x00,/**/ 0x02, 0x46, 0x0b, /**/0x00,/**/ 0x6d,
2829 0x5b, 0xca, /**/0x00,/**/ 0x9a, 0x10, 0xf0, /**/0x00,/**/ 0x05,
2830 0x19, 0xd0, /**/0x00,/**/ 0x2c, 0xf6, 0x27, /**/0x00,/**/ 0xaa,
2831 0x7c, 0x6f, /**/0x00,/**/ 0xb9, 0xd8, 0x72, /**/0x00,/**/ 0x03,
2832 0xf3, 0x81, /**/0x00,/**/ 0xfa, 0xe8, 0x26, /**/0x00,/**/ 0xca
2835 TRACE("(hProv=%08lx, dwParam=%08lx, pbData=%p, pdwDataLen=%p, dwFlags=%08lx)\n",
2836 hProv
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
2839 SetLastError(ERROR_INVALID_PARAMETER
);
2843 if (!lookup_handle(&handle_table
, (unsigned int)hProv
, RSAENH_MAGIC_CONTAINER
,
2844 (OBJECTHDR
**)&pKeyContainer
))
2846 /* MSDN: hProv not containing valid context handle */
2847 SetLastError(NTE_BAD_UID
);
2854 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)pKeyContainer
->szName
,
2855 strlen(pKeyContainer
->szName
)+1);
2858 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)pKeyContainer
->szProvName
,
2859 strlen(pKeyContainer
->szProvName
)+1);
2861 case PP_SIG_KEYSIZE_INC
:
2862 case PP_KEYX_KEYSIZE_INC
:
2864 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwTemp
, sizeof(dwTemp
));
2867 dwTemp
= CRYPT_IMPL_SOFTWARE
;
2868 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwTemp
, sizeof(dwTemp
));
2871 dwTemp
= 0x00000200;
2872 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwTemp
, sizeof(dwTemp
));
2874 case PP_ENUMCONTAINERS
:
2875 if ((dwFlags
& CRYPT_FIRST
) == CRYPT_FIRST
) pKeyContainer
->dwEnumContainersCtr
= 0;
2878 *pdwDataLen
= (DWORD
)MAX_PATH
+ 1;
2882 sprintf(szRSABase
, RSAENH_REGKEY
, "");
2884 if (dwFlags
& CRYPT_MACHINE_KEYSET
) {
2885 hRootKey
= HKEY_LOCAL_MACHINE
;
2887 hRootKey
= HKEY_CURRENT_USER
;
2890 if (RegOpenKeyExA(hRootKey
, szRSABase
, 0, KEY_READ
, &hKey
) != ERROR_SUCCESS
)
2892 SetLastError(ERROR_NO_MORE_ITEMS
);
2896 dwTemp
= *pdwDataLen
;
2897 switch (RegEnumKeyExA(hKey
, pKeyContainer
->dwEnumContainersCtr
, (LPSTR
)pbData
, &dwTemp
,
2898 NULL
, NULL
, NULL
, NULL
))
2900 case ERROR_MORE_DATA
:
2901 *pdwDataLen
= (DWORD
)MAX_PATH
+ 1;
2904 pKeyContainer
->dwEnumContainersCtr
++;
2908 case ERROR_NO_MORE_ITEMS
:
2910 SetLastError(ERROR_NO_MORE_ITEMS
);
2916 case PP_ENUMALGS_EX
:
2917 if (((pKeyContainer
->dwEnumAlgsCtr
>= RSAENH_MAX_ENUMALGS
-1) ||
2918 (!aProvEnumAlgsEx
[pKeyContainer
->dwPersonality
]
2919 [pKeyContainer
->dwEnumAlgsCtr
+1].aiAlgid
)) &&
2920 ((dwFlags
& CRYPT_FIRST
) != CRYPT_FIRST
))
2922 SetLastError(ERROR_NO_MORE_ITEMS
);
2926 if (dwParam
== PP_ENUMALGS
) {
2927 if (pbData
&& (*pdwDataLen
>= sizeof(PROV_ENUMALGS
)))
2928 pKeyContainer
->dwEnumAlgsCtr
= ((dwFlags
& CRYPT_FIRST
) == CRYPT_FIRST
) ?
2929 0 : pKeyContainer
->dwEnumAlgsCtr
+1;
2931 provEnumalgs
.aiAlgid
= aProvEnumAlgsEx
2932 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
].aiAlgid
;
2933 provEnumalgs
.dwBitLen
= aProvEnumAlgsEx
2934 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
].dwDefaultLen
;
2935 provEnumalgs
.dwNameLen
= aProvEnumAlgsEx
2936 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
].dwNameLen
;
2937 memcpy(provEnumalgs
.szName
, aProvEnumAlgsEx
2938 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
].szName
,
2941 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&provEnumalgs
,
2942 sizeof(PROV_ENUMALGS
));
2944 if (pbData
&& (*pdwDataLen
>= sizeof(PROV_ENUMALGS_EX
)))
2945 pKeyContainer
->dwEnumAlgsCtr
= ((dwFlags
& CRYPT_FIRST
) == CRYPT_FIRST
) ?
2946 0 : pKeyContainer
->dwEnumAlgsCtr
+1;
2948 return copy_param(pbData
, pdwDataLen
,
2949 (CONST BYTE
*)&aProvEnumAlgsEx
2950 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
],
2951 sizeof(PROV_ENUMALGS_EX
));
2954 case 41: /* Undocumented. Asked for by IE About dialog */
2955 return copy_param(pbData
, pdwDataLen
, abWTF
, sizeof(abWTF
));
2958 /* MSDN: Unknown parameter number in dwParam */
2959 SetLastError(NTE_BAD_TYPE
);
2964 /******************************************************************************
2965 * CPDeriveKey (RSAENH.@)
2967 * Derives a key from a hash value.
2970 * hProv [I] Key container for which a key is to be generated.
2971 * Algid [I] Crypto algorithm identifier for the key to be generated.
2972 * hBaseData [I] Hash from whose value the key will be derived.
2973 * dwFlags [I] See Notes.
2974 * phKey [O] The generated key.
2982 * - CRYPT_EXPORTABLE: Key can be exported.
2983 * - CRYPT_NO_SALT: No salt is used for 40 bit keys.
2984 * - CRYPT_CREATE_SALT: Use remaining bits as salt value.
2986 BOOL WINAPI
RSAENH_CPDeriveKey(HCRYPTPROV hProv
, ALG_ID Algid
, HCRYPTHASH hBaseData
,
2987 DWORD dwFlags
, HCRYPTKEY
*phKey
)
2989 CRYPTKEY
*pCryptKey
, *pMasterKey
;
2990 CRYPTHASH
*pCryptHash
;
2991 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
*2];
2994 TRACE("(hProv=%08lx, Algid=%d, hBaseData=%08lx, dwFlags=%08lx phKey=%p)\n", hProv
, Algid
,
2995 hBaseData
, dwFlags
, phKey
);
2997 if (!is_valid_handle(&handle_table
, (unsigned int)hProv
, RSAENH_MAGIC_CONTAINER
))
2999 SetLastError(NTE_BAD_UID
);
3003 if (!lookup_handle(&handle_table
, (unsigned int)hBaseData
, RSAENH_MAGIC_HASH
,
3004 (OBJECTHDR
**)&pCryptHash
))
3006 SetLastError(NTE_BAD_HASH
);
3012 SetLastError(ERROR_INVALID_PARAMETER
);
3016 switch (GET_ALG_CLASS(Algid
))
3018 case ALG_CLASS_DATA_ENCRYPT
:
3019 *phKey
= new_key(hProv
, Algid
, dwFlags
, &pCryptKey
);
3020 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
3023 * We derive the key material from the hash.
3024 * If the hash value is not large enough for the claimed key, we have to construct
3025 * a larger binary value based on the hash. This is documented in MSDN: CryptDeriveKey.
3027 dwLen
= RSAENH_MAX_HASH_SIZE
;
3028 RSAENH_CPGetHashParam(pCryptHash
->hProv
, hBaseData
, HP_HASHVAL
, abHashValue
, &dwLen
, 0);
3030 if (dwLen
< pCryptKey
->dwKeyLen
) {
3031 BYTE pad1
[RSAENH_HMAC_DEF_PAD_LEN
], pad2
[RSAENH_HMAC_DEF_PAD_LEN
];
3032 BYTE old_hashval
[RSAENH_MAX_HASH_SIZE
];
3035 memcpy(old_hashval
, pCryptHash
->abHashValue
, RSAENH_MAX_HASH_SIZE
);
3037 for (i
=0; i
<RSAENH_HMAC_DEF_PAD_LEN
; i
++) {
3038 pad1
[i
] = RSAENH_HMAC_DEF_IPAD_CHAR
^ (i
<dwLen
? abHashValue
[i
] : 0);
3039 pad2
[i
] = RSAENH_HMAC_DEF_OPAD_CHAR
^ (i
<dwLen
? abHashValue
[i
] : 0);
3042 init_hash(pCryptHash
);
3043 update_hash(pCryptHash
, pad1
, RSAENH_HMAC_DEF_PAD_LEN
);
3044 finalize_hash(pCryptHash
);
3045 memcpy(abHashValue
, pCryptHash
->abHashValue
, pCryptHash
->dwHashSize
);
3047 init_hash(pCryptHash
);
3048 update_hash(pCryptHash
, pad2
, RSAENH_HMAC_DEF_PAD_LEN
);
3049 finalize_hash(pCryptHash
);
3050 memcpy(abHashValue
+pCryptHash
->dwHashSize
, pCryptHash
->abHashValue
,
3051 pCryptHash
->dwHashSize
);
3053 memcpy(pCryptHash
->abHashValue
, old_hashval
, RSAENH_MAX_HASH_SIZE
);
3056 memcpy(pCryptKey
->abKeyValue
, abHashValue
,
3057 RSAENH_MIN(pCryptKey
->dwKeyLen
, sizeof(pCryptKey
->abKeyValue
)));
3060 case ALG_CLASS_MSG_ENCRYPT
:
3061 if (!lookup_handle(&handle_table
, pCryptHash
->hKey
, RSAENH_MAGIC_KEY
,
3062 (OBJECTHDR
**)&pMasterKey
))
3064 SetLastError(NTE_FAIL
); /* FIXME error code */
3070 /* See RFC 2246, chapter 6.3 Key calculation */
3071 case CALG_SCHANNEL_ENC_KEY
:
3072 *phKey
= new_key(hProv
, pMasterKey
->siSChannelInfo
.saEncAlg
.Algid
,
3073 MAKELONG(LOWORD(dwFlags
),pMasterKey
->siSChannelInfo
.saEncAlg
.cBits
),
3075 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
3076 memcpy(pCryptKey
->abKeyValue
,
3077 pCryptHash
->abHashValue
+ (
3078 2 * (pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
/ 8) +
3079 ((dwFlags
& CRYPT_SERVER
) ?
3080 (pMasterKey
->siSChannelInfo
.saEncAlg
.cBits
/ 8) : 0)),
3081 pMasterKey
->siSChannelInfo
.saEncAlg
.cBits
/ 8);
3082 memcpy(pCryptKey
->abInitVector
,
3083 pCryptHash
->abHashValue
+ (
3084 2 * (pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
/ 8) +
3085 2 * (pMasterKey
->siSChannelInfo
.saEncAlg
.cBits
/ 8) +
3086 ((dwFlags
& CRYPT_SERVER
) ? pCryptKey
->dwBlockLen
: 0)),
3087 pCryptKey
->dwBlockLen
);
3090 case CALG_SCHANNEL_MAC_KEY
:
3091 *phKey
= new_key(hProv
, Algid
,
3092 MAKELONG(LOWORD(dwFlags
),pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
),
3094 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
3095 memcpy(pCryptKey
->abKeyValue
,
3096 pCryptHash
->abHashValue
+ ((dwFlags
& CRYPT_SERVER
) ?
3097 pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
/ 8 : 0),
3098 pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
/ 8);
3102 SetLastError(NTE_BAD_ALGID
);
3108 SetLastError(NTE_BAD_ALGID
);
3112 setup_key(pCryptKey
);
3116 /******************************************************************************
3117 * CPGetUserKey (RSAENH.@)
3119 * Returns a handle to the user's private key-exchange- or signature-key.
3122 * hProv [I] The key container from which a user key is requested.
3123 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
3124 * phUserKey [O] Handle to the requested key or INVALID_HANDLE_VALUE in case of failure.
3131 * A newly created key container does not contain private user key. Create them with CPGenKey.
3133 BOOL WINAPI
RSAENH_CPGetUserKey(HCRYPTPROV hProv
, DWORD dwKeySpec
, HCRYPTKEY
*phUserKey
)
3135 KEYCONTAINER
*pKeyContainer
;
3137 TRACE("(hProv=%08lx, dwKeySpec=%08lx, phUserKey=%p)\n", hProv
, dwKeySpec
, phUserKey
);
3139 if (!lookup_handle(&handle_table
, (unsigned int)hProv
, RSAENH_MAGIC_CONTAINER
,
3140 (OBJECTHDR
**)&pKeyContainer
))
3142 /* MSDN: hProv not containing valid context handle */
3143 SetLastError(NTE_BAD_UID
);
3149 case AT_KEYEXCHANGE
:
3150 copy_handle(&handle_table
, pKeyContainer
->hKeyExchangeKeyPair
, RSAENH_MAGIC_KEY
,
3151 (unsigned int*)phUserKey
);
3155 copy_handle(&handle_table
, pKeyContainer
->hSignatureKeyPair
, RSAENH_MAGIC_KEY
,
3156 (unsigned int*)phUserKey
);
3160 *phUserKey
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
3163 if (*phUserKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
3165 /* MSDN: dwKeySpec parameter specifies nonexistent key */
3166 SetLastError(NTE_NO_KEY
);
3173 /******************************************************************************
3174 * CPHashData (RSAENH.@)
3176 * Updates a hash object with the given data.
3179 * hProv [I] Key container to which the hash object belongs.
3180 * hHash [I] Hash object which is to be updated.
3181 * pbData [I] Pointer to data with which the hash object is to be updated.
3182 * dwDataLen [I] Length of the data.
3183 * dwFlags [I] Currently none defined.
3190 * The actual hash value is queried with CPGetHashParam, which will finalize
3191 * the hash. Updating a finalized hash will fail with a last error NTE_BAD_HASH_STATE.
3193 BOOL WINAPI
RSAENH_CPHashData(HCRYPTPROV hProv
, HCRYPTHASH hHash
, CONST BYTE
*pbData
,
3194 DWORD dwDataLen
, DWORD dwFlags
)
3196 CRYPTHASH
*pCryptHash
;
3198 TRACE("(hProv=%08lx, hHash=%08lx, pbData=%p, dwDataLen=%ld, dwFlags=%08lx)\n",
3199 hProv
, hHash
, pbData
, dwDataLen
, dwFlags
);
3203 SetLastError(NTE_BAD_FLAGS
);
3207 if (!lookup_handle(&handle_table
, (unsigned int)hHash
, RSAENH_MAGIC_HASH
,
3208 (OBJECTHDR
**)&pCryptHash
))
3210 SetLastError(NTE_BAD_HASH
);
3214 if (!get_algid_info(hProv
, pCryptHash
->aiAlgid
) || pCryptHash
->aiAlgid
== CALG_SSL3_SHAMD5
)
3216 SetLastError(NTE_BAD_ALGID
);
3220 if (pCryptHash
->dwState
== RSAENH_HASHSTATE_IDLE
)
3221 pCryptHash
->dwState
= RSAENH_HASHSTATE_HASHING
;
3223 if (pCryptHash
->dwState
!= RSAENH_HASHSTATE_HASHING
)
3225 SetLastError(NTE_BAD_HASH_STATE
);
3229 update_hash(pCryptHash
, pbData
, dwDataLen
);
3233 /******************************************************************************
3234 * CPHashSessionKey (RSAENH.@)
3236 * Updates a hash object with the binary representation of a symmetric key.
3239 * hProv [I] Key container to which the hash object belongs.
3240 * hHash [I] Hash object which is to be updated.
3241 * hKey [I] The symmetric key, whose binary value will be added to the hash.
3242 * dwFlags [I] CRYPT_LITTLE_ENDIAN, if the binary key value shall be interpreted as little endian.
3248 BOOL WINAPI
RSAENH_CPHashSessionKey(HCRYPTPROV hProv
, HCRYPTHASH hHash
, HCRYPTKEY hKey
,
3251 BYTE abKeyValue
[RSAENH_MAX_KEY_SIZE
], bTemp
;
3255 TRACE("(hProv=%08lx, hHash=%08lx, hKey=%08lx, dwFlags=%08lx)\n", hProv
, hHash
, hKey
, dwFlags
);
3257 if (!lookup_handle(&handle_table
, (unsigned int)hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pKey
) ||
3258 (GET_ALG_CLASS(pKey
->aiAlgid
) != ALG_CLASS_DATA_ENCRYPT
))
3260 SetLastError(NTE_BAD_KEY
);
3264 if (dwFlags
& ~CRYPT_LITTLE_ENDIAN
) {
3265 SetLastError(NTE_BAD_FLAGS
);
3269 memcpy(abKeyValue
, pKey
->abKeyValue
, pKey
->dwKeyLen
);
3270 if (!(dwFlags
& CRYPT_LITTLE_ENDIAN
)) {
3271 for (i
=0; i
<pKey
->dwKeyLen
/2; i
++) {
3272 bTemp
= abKeyValue
[i
];
3273 abKeyValue
[i
] = abKeyValue
[pKey
->dwKeyLen
-i
-1];
3274 abKeyValue
[pKey
->dwKeyLen
-i
-1] = bTemp
;
3278 return RSAENH_CPHashData(hProv
, hHash
, abKeyValue
, pKey
->dwKeyLen
, 0);
3281 /******************************************************************************
3282 * CPReleaseContext (RSAENH.@)
3284 * Release a key container.
3287 * hProv [I] Key container to be released.
3288 * dwFlags [I] Currently none defined.
3294 BOOL WINAPI
RSAENH_CPReleaseContext(HCRYPTPROV hProv
, DWORD dwFlags
)
3296 TRACE("(hProv=%08lx, dwFlags=%08lx)\n", hProv
, dwFlags
);
3298 if (!release_handle(&handle_table
, (unsigned int)hProv
, RSAENH_MAGIC_CONTAINER
))
3300 /* MSDN: hProv not containing valid context handle */
3301 SetLastError(NTE_BAD_UID
);
3306 SetLastError(NTE_BAD_FLAGS
);
3313 /******************************************************************************
3314 * CPSetHashParam (RSAENH.@)
3316 * Set a parameter of a hash object
3319 * hProv [I] The key container to which the key belongs.
3320 * hHash [I] The hash object for which a parameter is to be set.
3321 * dwParam [I] Parameter type. See Notes.
3322 * pbData [I] Pointer to the parameter value.
3323 * dwFlags [I] Currently none defined.
3330 * Currently only the HP_HMAC_INFO dwParam type is defined.
3331 * The HMAC_INFO struct will be deep copied into the hash object.
3332 * See Internet RFC 2104 for details on the HMAC algorithm.
3334 BOOL WINAPI
RSAENH_CPSetHashParam(HCRYPTPROV hProv
, HCRYPTHASH hHash
, DWORD dwParam
,
3335 BYTE
*pbData
, DWORD dwFlags
)
3337 CRYPTHASH
*pCryptHash
;
3338 CRYPTKEY
*pCryptKey
;
3341 TRACE("(hProv=%08lx, hHash=%08lx, dwParam=%08lx, pbData=%p, dwFlags=%08lx)\n",
3342 hProv
, hHash
, dwParam
, pbData
, dwFlags
);
3344 if (!is_valid_handle(&handle_table
, (unsigned int)hProv
, RSAENH_MAGIC_CONTAINER
))
3346 SetLastError(NTE_BAD_UID
);
3351 SetLastError(NTE_BAD_FLAGS
);
3355 if (!lookup_handle(&handle_table
, (unsigned int)hHash
, RSAENH_MAGIC_HASH
,
3356 (OBJECTHDR
**)&pCryptHash
))
3358 SetLastError(NTE_BAD_HASH
);
3364 free_hmac_info(pCryptHash
->pHMACInfo
);
3365 if (!copy_hmac_info(&pCryptHash
->pHMACInfo
, (PHMAC_INFO
)pbData
)) return FALSE
;
3367 if (!lookup_handle(&handle_table
, pCryptHash
->hKey
, RSAENH_MAGIC_KEY
,
3368 (OBJECTHDR
**)&pCryptKey
))
3370 SetLastError(NTE_FAIL
); /* FIXME: correct error code? */
3374 for (i
=0; i
<RSAENH_MIN(pCryptKey
->dwKeyLen
,pCryptHash
->pHMACInfo
->cbInnerString
); i
++) {
3375 pCryptHash
->pHMACInfo
->pbInnerString
[i
] ^= pCryptKey
->abKeyValue
[i
];
3377 for (i
=0; i
<RSAENH_MIN(pCryptKey
->dwKeyLen
,pCryptHash
->pHMACInfo
->cbOuterString
); i
++) {
3378 pCryptHash
->pHMACInfo
->pbOuterString
[i
] ^= pCryptKey
->abKeyValue
[i
];
3381 init_hash(pCryptHash
);
3385 memcpy(pCryptHash
->abHashValue
, pbData
, pCryptHash
->dwHashSize
);
3386 pCryptHash
->dwState
= RSAENH_HASHSTATE_FINISHED
;
3389 case HP_TLS1PRF_SEED
:
3390 return copy_data_blob(&pCryptHash
->tpPRFParams
.blobSeed
, (PCRYPT_DATA_BLOB
)pbData
);
3392 case HP_TLS1PRF_LABEL
:
3393 return copy_data_blob(&pCryptHash
->tpPRFParams
.blobLabel
, (PCRYPT_DATA_BLOB
)pbData
);
3396 SetLastError(NTE_BAD_TYPE
);
3401 /******************************************************************************
3402 * CPSetProvParam (RSAENH.@)
3404 BOOL WINAPI
RSAENH_CPSetProvParam(HCRYPTPROV hProv
, DWORD dwParam
, BYTE
*pbData
, DWORD dwFlags
)
3410 /******************************************************************************
3411 * CPSignHash (RSAENH.@)
3413 * Sign a hash object
3416 * hProv [I] The key container, to which the hash object belongs.
3417 * hHash [I] The hash object to be signed.
3418 * dwKeySpec [I] AT_SIGNATURE or AT_KEYEXCHANGE: Key used to generate the signature.
3419 * sDescription [I] Should be NULL for security reasons.
3420 * dwFlags [I] 0, CRYPT_NOHASHOID or CRYPT_X931_FORMAT: Format of the signature.
3421 * pbSignature [O] Buffer, to which the signature will be stored. May be NULL to query SigLen.
3422 * pdwSigLen [I/O] Size of the buffer (in), Length of the signature (out)
3428 BOOL WINAPI
RSAENH_CPSignHash(HCRYPTPROV hProv
, HCRYPTHASH hHash
, DWORD dwKeySpec
,
3429 LPCWSTR sDescription
, DWORD dwFlags
, BYTE
*pbSignature
,
3432 HCRYPTKEY hCryptKey
;
3433 CRYPTKEY
*pCryptKey
;
3435 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
];
3438 TRACE("(hProv=%08lx, hHash=%08lx, dwKeySpec=%08lx, sDescription=%s, dwFlags=%08lx, "
3439 "pbSignature=%p, pdwSigLen=%p)\n", hProv
, hHash
, dwKeySpec
, debugstr_w(sDescription
),
3440 dwFlags
, pbSignature
, pdwSigLen
);
3442 if (dwFlags
& ~(CRYPT_NOHASHOID
|CRYPT_X931_FORMAT
)) {
3443 SetLastError(NTE_BAD_FLAGS
);
3447 if (!RSAENH_CPGetUserKey(hProv
, dwKeySpec
, &hCryptKey
)) return FALSE
;
3449 if (!lookup_handle(&handle_table
, (unsigned int)hCryptKey
, RSAENH_MAGIC_KEY
,
3450 (OBJECTHDR
**)&pCryptKey
))
3452 SetLastError(NTE_NO_KEY
);
3457 *pdwSigLen
= pCryptKey
->dwKeyLen
;
3460 if (pCryptKey
->dwKeyLen
> *pdwSigLen
)
3462 SetLastError(ERROR_MORE_DATA
);
3463 *pdwSigLen
= pCryptKey
->dwKeyLen
;
3466 *pdwSigLen
= pCryptKey
->dwKeyLen
;
3469 if (!RSAENH_CPHashData(hProv
, hHash
, (CONST BYTE
*)sDescription
,
3470 (DWORD
)lstrlenW(sDescription
)*sizeof(WCHAR
), 0))
3476 dwHashLen
= sizeof(DWORD
);
3477 if (!RSAENH_CPGetHashParam(hProv
, hHash
, HP_ALGID
, (BYTE
*)&aiAlgid
, &dwHashLen
, 0)) return FALSE
;
3479 dwHashLen
= RSAENH_MAX_HASH_SIZE
;
3480 if (!RSAENH_CPGetHashParam(hProv
, hHash
, HP_HASHVAL
, abHashValue
, &dwHashLen
, 0)) return FALSE
;
3483 if (!build_hash_signature(pbSignature
, *pdwSigLen
, aiAlgid
, abHashValue
, dwHashLen
, dwFlags
)) {
3487 return encrypt_block_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pbSignature
, pbSignature
, RSAENH_ENCRYPT
);
3490 /******************************************************************************
3491 * CPVerifySignature (RSAENH.@)
3493 * Verify the signature of a hash object.
3496 * hProv [I] The key container, to which the hash belongs.
3497 * hHash [I] The hash for which the signature is verified.
3498 * pbSignature [I] The binary signature.
3499 * dwSigLen [I] Length of the signature BLOB.
3500 * hPubKey [I] Public key used to verify the signature.
3501 * sDescription [I] Should be NULL for security reasons.
3502 * dwFlags [I] 0, CRYPT_NOHASHOID or CRYPT_X931_FORMAT: Format of the signature.
3505 * Success: TRUE (Signature is valid)
3506 * Failure: FALSE (GetLastError() == NTE_BAD_SIGNATURE, if signature is invalid)
3508 BOOL WINAPI
RSAENH_CPVerifySignature(HCRYPTPROV hProv
, HCRYPTHASH hHash
, CONST BYTE
*pbSignature
,
3509 DWORD dwSigLen
, HCRYPTKEY hPubKey
, LPCWSTR sDescription
,
3512 BYTE
*pbConstructed
= NULL
, *pbDecrypted
= NULL
;
3513 CRYPTKEY
*pCryptKey
;
3516 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
];
3519 TRACE("(hProv=%08lx, hHash=%08lx, pbSignature=%p, dwSigLen=%ld, hPubKey=%08lx, sDescription=%s, "
3520 "dwFlags=%08lx)\n", hProv
, hHash
, pbSignature
, dwSigLen
, hPubKey
, debugstr_w(sDescription
),
3523 if (dwFlags
& ~(CRYPT_NOHASHOID
|CRYPT_X931_FORMAT
)) {
3524 SetLastError(NTE_BAD_FLAGS
);
3528 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
3530 SetLastError(NTE_BAD_UID
);
3534 if (!lookup_handle(&handle_table
, (unsigned int)hPubKey
, RSAENH_MAGIC_KEY
,
3535 (OBJECTHDR
**)&pCryptKey
))
3537 SetLastError(NTE_BAD_KEY
);
3542 if (!RSAENH_CPHashData(hProv
, hHash
, (CONST BYTE
*)sDescription
,
3543 (DWORD
)lstrlenW(sDescription
)*sizeof(WCHAR
), 0))
3549 dwHashLen
= sizeof(DWORD
);
3550 if (!RSAENH_CPGetHashParam(hProv
, hHash
, HP_ALGID
, (BYTE
*)&aiAlgid
, &dwHashLen
, 0)) return FALSE
;
3552 dwHashLen
= RSAENH_MAX_HASH_SIZE
;
3553 if (!RSAENH_CPGetHashParam(hProv
, hHash
, HP_HASHVAL
, abHashValue
, &dwHashLen
, 0)) return FALSE
;
3555 pbConstructed
= HeapAlloc(GetProcessHeap(), 0, dwSigLen
);
3556 if (!pbConstructed
) {
3557 SetLastError(NTE_NO_MEMORY
);
3561 pbDecrypted
= HeapAlloc(GetProcessHeap(), 0, dwSigLen
);
3563 SetLastError(NTE_NO_MEMORY
);
3567 if (!encrypt_block_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pbSignature
, pbDecrypted
,
3573 if (!build_hash_signature(pbConstructed
, dwSigLen
, aiAlgid
, abHashValue
, dwHashLen
, dwFlags
)) {
3577 if (memcmp(pbDecrypted
, pbConstructed
, dwSigLen
)) {
3578 SetLastError(NTE_BAD_SIGNATURE
);
3584 HeapFree(GetProcessHeap(), 0, pbConstructed
);
3585 HeapFree(GetProcessHeap(), 0, pbDecrypted
);
3589 static const WCHAR szProviderKeys
[4][97] = {
3590 { 'S','o','f','t','w','a','r','e','\\',
3591 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
3592 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
3593 'i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ','B','a','s',
3594 'e',' ','C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r',
3595 'o','v','i','d','e','r',' ','v','1','.','0',0 },
3596 { 'S','o','f','t','w','a','r','e','\\',
3597 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
3598 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
3599 'i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ',
3600 'E','n','h','a','n','c','e','d',
3601 ' ','C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r',
3602 'o','v','i','d','e','r',' ','v','1','.','0',0 },
3603 { 'S','o','f','t','w','a','r','e','\\',
3604 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
3605 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
3606 'i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ','S','t','r','o','n','g',
3607 ' ','C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r',
3608 'o','v','i','d','e','r',0 },
3609 { 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
3610 'C','r','y','p','t','o','g','r','a','p','h','y','\\','D','e','f','a','u','l','t','s','\\',
3611 'P','r','o','v','i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ',
3612 'R','S','A',' ','S','C','h','a','n','n','e','l',' ',
3613 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r','o','v','i','d','e','r',0 }
3615 static const WCHAR szDefaultKeys
[2][65] = {
3616 { 'S','o','f','t','w','a','r','e','\\',
3617 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
3618 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
3619 'i','d','e','r',' ','T','y','p','e','s','\\','T','y','p','e',' ','0','0','1',0 },
3620 { 'S','o','f','t','w','a','r','e','\\',
3621 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
3622 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
3623 'i','d','e','r',' ','T','y','p','e','s','\\','T','y','p','e',' ','0','1','2',0 }
3627 /******************************************************************************
3628 * DllRegisterServer (RSAENH.@)
3630 * Dll self registration.
3639 * Registers the following keys:
3640 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider\
3641 * Microsoft Base Cryptographic Provider v1.0
3642 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider\
3643 * Microsoft Enhanced Cryptographic Provider
3644 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider\
3645 * Microsoft Strong Cryptographpic Provider
3646 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider Types\Type 001
3648 HRESULT WINAPI
DllRegisterServer(void)
3655 for (i
=0; i
<4; i
++) {
3656 apiRet
= RegCreateKeyExW(HKEY_LOCAL_MACHINE
, szProviderKeys
[i
], 0, NULL
,
3657 REG_OPTION_NON_VOLATILE
, KEY_ALL_ACCESS
, NULL
, &key
, &dp
);
3659 if (apiRet
== ERROR_SUCCESS
)
3661 if (dp
== REG_CREATED_NEW_KEY
)
3663 static const WCHAR szImagePath
[] = { 'I','m','a','g','e',' ','P','a','t','h',0 };
3664 static const WCHAR szRSABase
[] = { 'r','s','a','e','n','h','.','d','l','l',0 };
3665 static const WCHAR szType
[] = { 'T','y','p','e',0 };
3666 static const WCHAR szSignature
[] = { 'S','i','g','n','a','t','u','r','e',0 };
3667 DWORD type
= (i
== 3) ? PROV_RSA_SCHANNEL
: PROV_RSA_FULL
;
3668 DWORD sign
= 0xdeadbeef;
3669 RegSetValueExW(key
, szImagePath
, 0, REG_SZ
, (LPBYTE
)szRSABase
,
3670 (lstrlenW(szRSABase
) + 1) * sizeof(WCHAR
));
3671 RegSetValueExW(key
, szType
, 0, REG_DWORD
, (LPBYTE
)&type
, sizeof(type
));
3672 RegSetValueExW(key
, szSignature
, 0, REG_BINARY
, (LPBYTE
)&sign
, sizeof(sign
));
3678 for (i
=0; i
<2; i
++) {
3679 apiRet
= RegCreateKeyExW(HKEY_LOCAL_MACHINE
, szDefaultKeys
[i
], 0, NULL
,
3680 REG_OPTION_NON_VOLATILE
, KEY_ALL_ACCESS
, NULL
, &key
, &dp
);
3681 if (apiRet
== ERROR_SUCCESS
)
3683 if (dp
== REG_CREATED_NEW_KEY
)
3685 static const WCHAR szName
[] = { 'N','a','m','e',0 };
3686 static const WCHAR szRSAName
[2][46] = {
3687 { 'M','i','c','r','o','s','o','f','t',' ', 'B','a','s','e',' ',
3688 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ',
3689 'P','r','o','v','i','d','e','r',' ','v','1','.','0',0 },
3690 { 'M','i','c','r','o','s','o','f','t',' ','R','S','A',' ',
3691 'S','C','h','a','n','n','e','l',' ',
3692 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ',
3693 'P','r','o','v','i','d','e','r',0 } };
3694 static const WCHAR szTypeName
[] = { 'T','y','p','e','N','a','m','e',0 };
3695 static const WCHAR szRSATypeName
[2][38] = {
3696 { 'R','S','A',' ','F','u','l','l',' ',
3697 '(','S','i','g','n','a','t','u','r','e',' ','a','n','d',' ',
3698 'K','e','y',' ','E','x','c','h','a','n','g','e',')',0 },
3699 { 'R','S','A',' ','S','C','h','a','n','n','e','l',0 } };
3701 RegSetValueExW(key
, szName
, 0, REG_SZ
,
3702 (LPBYTE
)szRSAName
[i
], lstrlenW(szRSAName
[i
])*sizeof(WCHAR
)+sizeof(WCHAR
));
3703 RegSetValueExW(key
, szTypeName
, 0, REG_SZ
,
3704 (LPBYTE
)szRSATypeName
[i
], lstrlenW(szRSATypeName
[i
])*sizeof(WCHAR
)+sizeof(WCHAR
));
3710 return HRESULT_FROM_WIN32(apiRet
);
3713 /******************************************************************************
3714 * DllUnregisterServer (RSAENH.@)
3716 * Dll self unregistration.
3724 * For the relevant keys see DllRegisterServer.
3726 HRESULT WINAPI
DllUnregisterServer(void)
3728 RegDeleteKeyW(HKEY_LOCAL_MACHINE
, szProviderKeys
[0]);
3729 RegDeleteKeyW(HKEY_LOCAL_MACHINE
, szProviderKeys
[1]);
3730 RegDeleteKeyW(HKEY_LOCAL_MACHINE
, szProviderKeys
[2]);
3731 RegDeleteKeyW(HKEY_LOCAL_MACHINE
, szProviderKeys
[3]);
3732 RegDeleteKeyW(HKEY_LOCAL_MACHINE
, szDefaultKeys
[0]);
3733 RegDeleteKeyW(HKEY_LOCAL_MACHINE
, szDefaultKeys
[1]);