3 * RSAENH - RSA encryption for Wine
5 * Copyright 2002 TransGaming Technologies (David Hammerton)
6 * Copyright 2004 Mike McCormack for CodeWeavers
7 * Copyright 2004, 2005 Michael Jung
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "wine/port.h"
26 #include "wine/library.h"
27 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(crypt
);
42 /******************************************************************************
43 * CRYPTHASH - hash objects
45 #define RSAENH_MAGIC_HASH 0x85938417u
46 #define RSAENH_MAX_HASH_SIZE 104
47 #define RSAENH_HASHSTATE_HASHING 1
48 #define RSAENH_HASHSTATE_FINISHED 2
49 typedef struct _RSAENH_TLS1PRF_PARAMS
51 CRYPT_DATA_BLOB blobLabel
;
52 CRYPT_DATA_BLOB blobSeed
;
53 } RSAENH_TLS1PRF_PARAMS
;
55 typedef struct tagCRYPTHASH
64 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
];
66 RSAENH_TLS1PRF_PARAMS tpPRFParams
;
69 /******************************************************************************
70 * CRYPTKEY - key objects
72 #define RSAENH_MAGIC_KEY 0x73620457u
73 #define RSAENH_MAX_KEY_SIZE 48
74 #define RSAENH_MAX_BLOCK_SIZE 24
75 #define RSAENH_KEYSTATE_IDLE 0
76 #define RSAENH_KEYSTATE_ENCRYPTING 1
77 #define RSAENH_KEYSTATE_DECRYPTING 2
78 #define RSAENH_KEYSTATE_MASTERKEY 3
79 typedef struct _RSAENH_SCHANNEL_INFO
81 SCHANNEL_ALG saEncAlg
;
82 SCHANNEL_ALG saMACAlg
;
83 CRYPT_DATA_BLOB blobClientRandom
;
84 CRYPT_DATA_BLOB blobServerRandom
;
85 } RSAENH_SCHANNEL_INFO
;
87 typedef struct tagCRYPTKEY
96 DWORD dwEffectiveKeyLen
;
101 BYTE abKeyValue
[RSAENH_MAX_KEY_SIZE
];
102 BYTE abInitVector
[RSAENH_MAX_BLOCK_SIZE
];
103 BYTE abChainVector
[RSAENH_MAX_BLOCK_SIZE
];
104 RSAENH_SCHANNEL_INFO siSChannelInfo
;
107 /******************************************************************************
108 * KEYCONTAINER - key containers
110 #define RSAENH_PERSONALITY_BASE 0u
111 #define RSAENH_PERSONALITY_STRONG 1u
112 #define RSAENH_PERSONALITY_ENHANCED 2u
113 #define RSAENH_PERSONALITY_SCHANNEL 3u
115 #define RSAENH_MAGIC_CONTAINER 0x26384993u
116 typedef struct tagKEYCONTAINER
122 DWORD dwEnumContainersCtr
;
123 CHAR szName
[MAX_PATH
];
124 CHAR szProvName
[MAX_PATH
];
125 HCRYPTKEY hKeyExchangeKeyPair
;
126 HCRYPTKEY hSignatureKeyPair
;
129 /******************************************************************************
130 * Some magic constants
132 #define RSAENH_ENCRYPT 1
133 #define RSAENH_DECRYPT 0
134 #define RSAENH_HMAC_DEF_IPAD_CHAR 0x36
135 #define RSAENH_HMAC_DEF_OPAD_CHAR 0x5c
136 #define RSAENH_HMAC_DEF_PAD_LEN 64
137 #define RSAENH_DES_EFFECTIVE_KEYLEN 56
138 #define RSAENH_DES_STORAGE_KEYLEN 64
139 #define RSAENH_3DES112_EFFECTIVE_KEYLEN 112
140 #define RSAENH_3DES112_STORAGE_KEYLEN 128
141 #define RSAENH_3DES_EFFECTIVE_KEYLEN 168
142 #define RSAENH_3DES_STORAGE_KEYLEN 192
143 #define RSAENH_MAGIC_RSA2 0x32415352
144 #define RSAENH_MAGIC_RSA1 0x31415352
145 #define RSAENH_PKC_BLOCKTYPE 0x02
146 #define RSAENH_SSL3_VERSION_MAJOR 3
147 #define RSAENH_SSL3_VERSION_MINOR 0
148 #define RSAENH_TLS1_VERSION_MAJOR 3
149 #define RSAENH_TLS1_VERSION_MINOR 1
150 #define RSAENH_REGKEY "Software\\Wine\\Crypto\\RSA\\%s"
152 #define RSAENH_MIN(a,b) ((a)<(b)?(a):(b))
153 /******************************************************************************
154 * aProvEnumAlgsEx - Defines the capabilities of the CSP personalities.
156 #define RSAENH_MAX_ENUMALGS 20
157 #define RSAENH_PCT1_SSL2_SSL3_TLS1 (CRYPT_FLAG_PCT1|CRYPT_FLAG_SSL2|CRYPT_FLAG_SSL3|CRYPT_FLAG_TLS1)
158 static const PROV_ENUMALGS_EX aProvEnumAlgsEx
[4][RSAENH_MAX_ENUMALGS
+1] =
161 {CALG_RC2
, 40, 40, 56,0, 4,"RC2", 24,"RSA Data Security's RC2"},
162 {CALG_RC4
, 40, 40, 56,0, 4,"RC4", 24,"RSA Data Security's RC4"},
163 {CALG_DES
, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
164 {CALG_SHA
, 160,160, 160,CRYPT_FLAG_SIGNING
, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
165 {CALG_MD2
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD2", 23,"Message Digest 2 (MD2)"},
166 {CALG_MD4
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD4", 23,"Message Digest 4 (MD4)"},
167 {CALG_MD5
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD5", 23,"Message Digest 5 (MD5)"},
168 {CALG_SSL3_SHAMD5
,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
169 {CALG_MAC
, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
170 {CALG_RSA_SIGN
, 512,384,16384,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_SIGN",14,"RSA Signature"},
171 {CALG_RSA_KEYX
, 512,384, 1024,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_KEYX",17,"RSA Key Exchange"},
172 {CALG_HMAC
, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
173 {0, 0, 0, 0,0, 1,"", 1,""}
176 {CALG_RC2
, 128, 40, 128,0, 4,"RC2", 24,"RSA Data Security's RC2"},
177 {CALG_RC4
, 128, 40, 128,0, 4,"RC4", 24,"RSA Data Security's RC4"},
178 {CALG_DES
, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
179 {CALG_3DES_112
, 112,112, 112,0, 13,"3DES TWO KEY",19,"Two Key Triple DES"},
180 {CALG_3DES
, 168,168, 168,0, 5,"3DES", 21,"Three Key Triple DES"},
181 {CALG_SHA
, 160,160, 160,CRYPT_FLAG_SIGNING
, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
182 {CALG_MD2
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD2", 23,"Message Digest 2 (MD2)"},
183 {CALG_MD4
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD4", 23,"Message Digest 4 (MD4)"},
184 {CALG_MD5
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD5", 23,"Message Digest 5 (MD5)"},
185 {CALG_SSL3_SHAMD5
,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
186 {CALG_MAC
, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
187 {CALG_RSA_SIGN
,1024,384,16384,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_SIGN",14,"RSA Signature"},
188 {CALG_RSA_KEYX
,1024,384,16384,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_KEYX",17,"RSA Key Exchange"},
189 {CALG_HMAC
, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
190 {0, 0, 0, 0,0, 1,"", 1,""}
193 {CALG_RC2
, 128, 40, 128,0, 4,"RC2", 24,"RSA Data Security's RC2"},
194 {CALG_RC4
, 128, 40, 128,0, 4,"RC4", 24,"RSA Data Security's RC4"},
195 {CALG_DES
, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
196 {CALG_3DES_112
, 112,112, 112,0, 13,"3DES TWO KEY",19,"Two Key Triple DES"},
197 {CALG_3DES
, 168,168, 168,0, 5,"3DES", 21,"Three Key Triple DES"},
198 {CALG_SHA
, 160,160, 160,CRYPT_FLAG_SIGNING
, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
199 {CALG_MD2
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD2", 23,"Message Digest 2 (MD2)"},
200 {CALG_MD4
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD4", 23,"Message Digest 4 (MD4)"},
201 {CALG_MD5
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD5", 23,"Message Digest 5 (MD5)"},
202 {CALG_SSL3_SHAMD5
,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
203 {CALG_MAC
, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
204 {CALG_RSA_SIGN
,1024,384,16384,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_SIGN",14,"RSA Signature"},
205 {CALG_RSA_KEYX
,1024,384,16384,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_KEYX",17,"RSA Key Exchange"},
206 {CALG_HMAC
, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
207 {0, 0, 0, 0,0, 1,"", 1,""}
210 {CALG_RC2
, 128, 40, 128,RSAENH_PCT1_SSL2_SSL3_TLS1
, 4,"RC2", 24,"RSA Data Security's RC2"},
211 {CALG_RC4
, 128, 40, 128,RSAENH_PCT1_SSL2_SSL3_TLS1
, 4,"RC4", 24,"RSA Data Security's RC4"},
212 {CALG_DES
, 56, 56, 56,RSAENH_PCT1_SSL2_SSL3_TLS1
, 4,"DES", 31,"Data Encryption Standard (DES)"},
213 {CALG_3DES_112
, 112,112, 112,RSAENH_PCT1_SSL2_SSL3_TLS1
,13,"3DES TWO KEY",19,"Two Key Triple DES"},
214 {CALG_3DES
, 168,168, 168,RSAENH_PCT1_SSL2_SSL3_TLS1
, 5,"3DES", 21,"Three Key Triple DES"},
215 {CALG_SHA
,160,160,160,CRYPT_FLAG_SIGNING
|RSAENH_PCT1_SSL2_SSL3_TLS1
,6,"SHA-1",30,"Secure Hash Algorithm (SHA-1)"},
216 {CALG_MD5
,128,128,128,CRYPT_FLAG_SIGNING
|RSAENH_PCT1_SSL2_SSL3_TLS1
,4,"MD5",23,"Message Digest 5 (MD5)"},
217 {CALG_SSL3_SHAMD5
,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
218 {CALG_MAC
, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
219 {CALG_RSA_SIGN
,1024,384,16384,CRYPT_FLAG_SIGNING
|RSAENH_PCT1_SSL2_SSL3_TLS1
,9,"RSA_SIGN",14,"RSA Signature"},
220 {CALG_RSA_KEYX
,1024,384,16384,CRYPT_FLAG_SIGNING
|RSAENH_PCT1_SSL2_SSL3_TLS1
,9,"RSA_KEYX",17,"RSA Key Exchange"},
221 {CALG_HMAC
, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
222 {CALG_PCT1_MASTER
,128,128,128,CRYPT_FLAG_PCT1
, 12,"PCT1 MASTER",12,"PCT1 Master"},
223 {CALG_SSL2_MASTER
,40,40, 192,CRYPT_FLAG_SSL2
, 12,"SSL2 MASTER",12,"SSL2 Master"},
224 {CALG_SSL3_MASTER
,384,384,384,CRYPT_FLAG_SSL3
, 12,"SSL3 MASTER",12,"SSL3 Master"},
225 {CALG_TLS1_MASTER
,384,384,384,CRYPT_FLAG_TLS1
, 12,"TLS1 MASTER",12,"TLS1 Master"},
226 {CALG_SCHANNEL_MASTER_HASH
,0,0,-1,0, 16,"SCH MASTER HASH",21,"SChannel Master Hash"},
227 {CALG_SCHANNEL_MAC_KEY
,0,0,-1,0, 12,"SCH MAC KEY",17,"SChannel MAC Key"},
228 {CALG_SCHANNEL_ENC_KEY
,0,0,-1,0, 12,"SCH ENC KEY",24,"SChannel Encryption Key"},
229 {CALG_TLS1PRF
, 0, 0, -1,0, 9,"TLS1 PRF", 28,"TLS1 Pseudo Random Function"},
230 {0, 0, 0, 0,0, 1,"", 1,""}
234 /******************************************************************************
235 * API forward declarations
238 RSAENH_CPGetKeyParam(
269 RSAENH_CPSetHashParam(
273 BYTE
*pbData
, DWORD dwFlags
277 RSAENH_CPGetHashParam(
287 RSAENH_CPDestroyHash(
322 /******************************************************************************
323 * CSP's handle table (used by all acquired key containers)
325 static HANDLETABLE handle_table
;
327 /******************************************************************************
330 * Initializes and destroys the handle table for the CSP's handles.
332 int WINAPI
DllMain(HINSTANCE hInstance
, DWORD fdwReason
, PVOID pvReserved
)
336 case DLL_PROCESS_ATTACH
:
337 DisableThreadLibraryCalls(hInstance
);
338 init_handle_table(&handle_table
);
341 case DLL_PROCESS_DETACH
:
342 destroy_handle_table(&handle_table
);
348 /******************************************************************************
349 * copy_param [Internal]
351 * Helper function that supports the standard WINAPI protocol for querying data
355 * pbBuffer [O] Buffer where the queried parameter is copied to, if it is large enough.
356 * May be NUL if the required buffer size is to be queried only.
357 * pdwBufferSize [I/O] In: Size of the buffer at pbBuffer
358 * Out: Size of parameter pbParam
359 * pbParam [I] Parameter value.
360 * dwParamSize [I] Size of pbParam
363 * Success: TRUE (pbParam was copied into pbBuffer or pbBuffer is NULL)
364 * Failure: FALSE (pbBuffer is not large enough to hold pbParam). Last error: ERROR_MORE_DATA
366 static inline BOOL
copy_param(
367 BYTE
*pbBuffer
, DWORD
*pdwBufferSize
, CONST BYTE
*pbParam
, DWORD dwParamSize
)
371 if (dwParamSize
> *pdwBufferSize
)
373 SetLastError(ERROR_MORE_DATA
);
374 *pdwBufferSize
= dwParamSize
;
377 memcpy(pbBuffer
, pbParam
, dwParamSize
);
379 *pdwBufferSize
= dwParamSize
;
383 /******************************************************************************
384 * get_algid_info [Internal]
386 * Query CSP capabilities for a given crypto algorithm.
389 * hProv [I] Handle to a key container of the CSP whose capabilities are to be queried.
390 * algid [I] Identifier of the crypto algorithm about which information is requested.
393 * Success: Pointer to a PROV_ENUMALGS_EX struct containing information about the crypto algorithm.
394 * Failure: NULL (algid not supported)
396 static inline const PROV_ENUMALGS_EX
* get_algid_info(HCRYPTPROV hProv
, ALG_ID algid
) {
397 const PROV_ENUMALGS_EX
*iterator
;
398 KEYCONTAINER
*pKeyContainer
;
400 if (!lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
, (OBJECTHDR
**)&pKeyContainer
)) {
401 SetLastError(NTE_BAD_UID
);
405 for (iterator
= aProvEnumAlgsEx
[pKeyContainer
->dwPersonality
]; iterator
->aiAlgid
; iterator
++) {
406 if (iterator
->aiAlgid
== algid
) return iterator
;
409 SetLastError(NTE_BAD_ALGID
);
413 /******************************************************************************
414 * copy_data_blob [Internal]
416 * deeply copies a DATA_BLOB
419 * dst [O] That's where the blob will be copied to
420 * src [I] Source blob
424 * Failure: FALSE (GetLastError() == NTE_NO_MEMORY
427 * Use free_data_blob to release resources occupied by copy_data_blob.
429 static inline BOOL
copy_data_blob(PCRYPT_DATA_BLOB dst
, CONST PCRYPT_DATA_BLOB src
) {
430 dst
->pbData
= HeapAlloc(GetProcessHeap(), 0, src
->cbData
);
432 SetLastError(NTE_NO_MEMORY
);
435 dst
->cbData
= src
->cbData
;
436 memcpy(dst
->pbData
, src
->pbData
, src
->cbData
);
440 /******************************************************************************
441 * concat_data_blobs [Internal]
443 * Concatenates two blobs
446 * dst [O] The new blob will be copied here
447 * src1 [I] Prefix blob
448 * src2 [I] Appendix blob
452 * Failure: FALSE (GetLastError() == NTE_NO_MEMORY)
455 * Release resources occupied by concat_data_blobs with free_data_blobs
457 static inline BOOL
concat_data_blobs(PCRYPT_DATA_BLOB dst
, CONST PCRYPT_DATA_BLOB src1
,
458 CONST PCRYPT_DATA_BLOB src2
)
460 dst
->cbData
= src1
->cbData
+ src2
->cbData
;
461 dst
->pbData
= HeapAlloc(GetProcessHeap(), 0, dst
->cbData
);
463 SetLastError(NTE_NO_MEMORY
);
466 memcpy(dst
->pbData
, src1
->pbData
, src1
->cbData
);
467 memcpy(dst
->pbData
+ src1
->cbData
, src2
->pbData
, src2
->cbData
);
471 /******************************************************************************
472 * free_data_blob [Internal]
474 * releases resource occupied by a dynamically allocated CRYPT_DATA_BLOB
477 * pBlob [I] Heap space occupied by pBlob->pbData is released
479 static inline void free_data_blob(PCRYPT_DATA_BLOB pBlob
) {
480 HeapFree(GetProcessHeap(), 0, pBlob
->pbData
);
483 /******************************************************************************
484 * init_data_blob [Internal]
486 static inline void init_data_blob(PCRYPT_DATA_BLOB pBlob
) {
487 pBlob
->pbData
= NULL
;
491 /******************************************************************************
492 * free_hmac_info [Internal]
494 * Deeply free an HMAC_INFO struct.
497 * hmac_info [I] Pointer to the HMAC_INFO struct to be freed.
500 * See Internet RFC 2104 for details on the HMAC algorithm.
502 static inline void free_hmac_info(PHMAC_INFO hmac_info
) {
503 if (!hmac_info
) return;
504 HeapFree(GetProcessHeap(), 0, hmac_info
->pbInnerString
);
505 HeapFree(GetProcessHeap(), 0, hmac_info
->pbOuterString
);
506 HeapFree(GetProcessHeap(), 0, hmac_info
);
509 /******************************************************************************
510 * copy_hmac_info [Internal]
512 * Deeply copy an HMAC_INFO struct
515 * dst [O] Pointer to a location where the pointer to the HMAC_INFO copy will be stored.
516 * src [I] Pointer to the HMAC_INFO struct to be copied.
523 * See Internet RFC 2104 for details on the HMAC algorithm.
525 static BOOL
copy_hmac_info(PHMAC_INFO
*dst
, const HMAC_INFO
*src
) {
526 if (!src
) return FALSE
;
527 *dst
= HeapAlloc(GetProcessHeap(), 0, sizeof(HMAC_INFO
));
528 if (!*dst
) return FALSE
;
529 memcpy(*dst
, src
, sizeof(HMAC_INFO
));
530 (*dst
)->pbInnerString
= NULL
;
531 (*dst
)->pbOuterString
= NULL
;
532 if ((*dst
)->cbInnerString
== 0) (*dst
)->cbInnerString
= RSAENH_HMAC_DEF_PAD_LEN
;
533 (*dst
)->pbInnerString
= HeapAlloc(GetProcessHeap(), 0, (*dst
)->cbInnerString
);
534 if (!(*dst
)->pbInnerString
) {
535 free_hmac_info(*dst
);
538 if (src
->cbInnerString
)
539 memcpy((*dst
)->pbInnerString
, src
->pbInnerString
, src
->cbInnerString
);
541 memset((*dst
)->pbInnerString
, RSAENH_HMAC_DEF_IPAD_CHAR
, RSAENH_HMAC_DEF_PAD_LEN
);
542 if ((*dst
)->cbOuterString
== 0) (*dst
)->cbOuterString
= RSAENH_HMAC_DEF_PAD_LEN
;
543 (*dst
)->pbOuterString
= HeapAlloc(GetProcessHeap(), 0, (*dst
)->cbOuterString
);
544 if (!(*dst
)->pbOuterString
) {
545 free_hmac_info(*dst
);
548 if (src
->cbOuterString
)
549 memcpy((*dst
)->pbOuterString
, src
->pbOuterString
, src
->cbOuterString
);
551 memset((*dst
)->pbOuterString
, RSAENH_HMAC_DEF_OPAD_CHAR
, RSAENH_HMAC_DEF_PAD_LEN
);
555 /******************************************************************************
556 * destroy_hash [Internal]
558 * Destructor for hash objects
561 * pCryptHash [I] Pointer to the hash object to be destroyed.
562 * Will be invalid after function returns!
564 static void destroy_hash(OBJECTHDR
*pObject
)
566 CRYPTHASH
*pCryptHash
= (CRYPTHASH
*)pObject
;
568 free_hmac_info(pCryptHash
->pHMACInfo
);
569 free_data_blob(&pCryptHash
->tpPRFParams
.blobLabel
);
570 free_data_blob(&pCryptHash
->tpPRFParams
.blobSeed
);
571 HeapFree(GetProcessHeap(), 0, pCryptHash
);
574 /******************************************************************************
575 * init_hash [Internal]
577 * Initialize (or reset) a hash object
580 * pCryptHash [I] The hash object to be initialized.
582 static inline BOOL
init_hash(CRYPTHASH
*pCryptHash
) {
585 switch (pCryptHash
->aiAlgid
)
588 if (pCryptHash
->pHMACInfo
) {
589 const PROV_ENUMALGS_EX
*pAlgInfo
;
591 pAlgInfo
= get_algid_info(pCryptHash
->hProv
, pCryptHash
->pHMACInfo
->HashAlgid
);
592 if (!pAlgInfo
) return FALSE
;
593 pCryptHash
->dwHashSize
= pAlgInfo
->dwDefaultLen
>> 3;
594 init_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
);
595 update_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
596 pCryptHash
->pHMACInfo
->pbInnerString
,
597 pCryptHash
->pHMACInfo
->cbInnerString
);
602 dwLen
= sizeof(DWORD
);
603 RSAENH_CPGetKeyParam(pCryptHash
->hProv
, pCryptHash
->hKey
, KP_BLOCKLEN
,
604 (BYTE
*)&pCryptHash
->dwHashSize
, &dwLen
, 0);
605 pCryptHash
->dwHashSize
>>= 3;
609 return init_hash_impl(pCryptHash
->aiAlgid
, &pCryptHash
->context
);
613 /******************************************************************************
614 * update_hash [Internal]
616 * Hashes the given data and updates the hash object's state accordingly
619 * pCryptHash [I] Hash object to be updated.
620 * pbData [I] Pointer to data stream to be hashed.
621 * dwDataLen [I] Length of data stream.
623 static inline void update_hash(CRYPTHASH
*pCryptHash
, CONST BYTE
*pbData
, DWORD dwDataLen
) {
626 switch (pCryptHash
->aiAlgid
)
629 if (pCryptHash
->pHMACInfo
)
630 update_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
635 pbTemp
= HeapAlloc(GetProcessHeap(), 0, dwDataLen
);
637 memcpy(pbTemp
, pbData
, dwDataLen
);
638 RSAENH_CPEncrypt(pCryptHash
->hProv
, pCryptHash
->hKey
, (HCRYPTHASH
)NULL
, FALSE
, 0,
639 pbTemp
, &dwDataLen
, dwDataLen
);
640 HeapFree(GetProcessHeap(), 0, pbTemp
);
644 update_hash_impl(pCryptHash
->aiAlgid
, &pCryptHash
->context
, pbData
, dwDataLen
);
648 /******************************************************************************
649 * finalize_hash [Internal]
651 * Finalizes the hash, after all data has been hashed with update_hash.
652 * No additional data can be hashed afterwards until the hash gets initialized again.
655 * pCryptHash [I] Hash object to be finalized.
657 static inline void finalize_hash(CRYPTHASH
*pCryptHash
) {
660 switch (pCryptHash
->aiAlgid
)
663 if (pCryptHash
->pHMACInfo
) {
664 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
];
666 finalize_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
667 pCryptHash
->abHashValue
);
668 memcpy(abHashValue
, pCryptHash
->abHashValue
, pCryptHash
->dwHashSize
);
669 init_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
);
670 update_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
671 pCryptHash
->pHMACInfo
->pbOuterString
,
672 pCryptHash
->pHMACInfo
->cbOuterString
);
673 update_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
674 abHashValue
, pCryptHash
->dwHashSize
);
675 finalize_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
676 pCryptHash
->abHashValue
);
682 RSAENH_CPEncrypt(pCryptHash
->hProv
, pCryptHash
->hKey
, (HCRYPTHASH
)NULL
, TRUE
, 0,
683 pCryptHash
->abHashValue
, &dwDataLen
, pCryptHash
->dwHashSize
);
687 finalize_hash_impl(pCryptHash
->aiAlgid
, &pCryptHash
->context
, pCryptHash
->abHashValue
);
691 /******************************************************************************
692 * destroy_key [Internal]
694 * Destructor for key objects
697 * pCryptKey [I] Pointer to the key object to be destroyed.
698 * Will be invalid after function returns!
700 static void destroy_key(OBJECTHDR
*pObject
)
702 CRYPTKEY
*pCryptKey
= (CRYPTKEY
*)pObject
;
704 free_key_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
);
705 free_data_blob(&pCryptKey
->siSChannelInfo
.blobClientRandom
);
706 free_data_blob(&pCryptKey
->siSChannelInfo
.blobServerRandom
);
707 HeapFree(GetProcessHeap(), 0, pCryptKey
);
710 /******************************************************************************
711 * setup_key [Internal]
713 * Initialize (or reset) a key object
716 * pCryptKey [I] The key object to be initialized.
718 static inline void setup_key(CRYPTKEY
*pCryptKey
) {
719 pCryptKey
->dwState
= RSAENH_KEYSTATE_IDLE
;
720 memcpy(pCryptKey
->abChainVector
, pCryptKey
->abInitVector
, sizeof(pCryptKey
->abChainVector
));
721 setup_key_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pCryptKey
->dwKeyLen
,
722 pCryptKey
->dwEffectiveKeyLen
, pCryptKey
->dwSaltLen
,
723 pCryptKey
->abKeyValue
);
726 /******************************************************************************
729 * Creates a new key object without assigning the actual binary key value.
730 * This is done by CPDeriveKey, CPGenKey or CPImportKey, which call this function.
733 * hProv [I] Handle to the provider to which the created key will belong.
734 * aiAlgid [I] The new key shall use the crypto algorithm idenfied by aiAlgid.
735 * dwFlags [I] Upper 16 bits give the key length.
736 * Lower 16 bits: CRYPT_CREATE_SALT, CRYPT_NO_SALT
737 * ppCryptKey [O] Pointer to the created key
740 * Success: Handle to the created key.
741 * Failure: INVALID_HANDLE_VALUE
743 static HCRYPTKEY
new_key(HCRYPTPROV hProv
, ALG_ID aiAlgid
, DWORD dwFlags
, CRYPTKEY
**ppCryptKey
)
747 DWORD dwKeyLen
= HIWORD(dwFlags
);
748 const PROV_ENUMALGS_EX
*peaAlgidInfo
;
753 * Retrieve the CSP's capabilities for the given ALG_ID value
755 peaAlgidInfo
= get_algid_info(hProv
, aiAlgid
);
756 if (!peaAlgidInfo
) return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
759 * Assume the default key length, if none is specified explicitly
761 if (dwKeyLen
== 0) dwKeyLen
= peaAlgidInfo
->dwDefaultLen
;
764 * Check if the requested key length is supported by the current CSP.
765 * Adjust key length's for DES algorithms.
769 if (dwKeyLen
== RSAENH_DES_EFFECTIVE_KEYLEN
) {
770 dwKeyLen
= RSAENH_DES_STORAGE_KEYLEN
;
772 if (dwKeyLen
!= RSAENH_DES_STORAGE_KEYLEN
) {
773 SetLastError(NTE_BAD_FLAGS
);
774 return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
779 if (dwKeyLen
== RSAENH_3DES112_EFFECTIVE_KEYLEN
) {
780 dwKeyLen
= RSAENH_3DES112_STORAGE_KEYLEN
;
782 if (dwKeyLen
!= RSAENH_3DES112_STORAGE_KEYLEN
) {
783 SetLastError(NTE_BAD_FLAGS
);
784 return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
789 if (dwKeyLen
== RSAENH_3DES_EFFECTIVE_KEYLEN
) {
790 dwKeyLen
= RSAENH_3DES_STORAGE_KEYLEN
;
792 if (dwKeyLen
!= RSAENH_3DES_STORAGE_KEYLEN
) {
793 SetLastError(NTE_BAD_FLAGS
);
794 return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
800 dwKeyLen
> peaAlgidInfo
->dwMaxLen
||
801 dwKeyLen
< peaAlgidInfo
->dwMinLen
)
803 SetLastError(NTE_BAD_FLAGS
);
804 return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
808 hCryptKey
= (HCRYPTKEY
)new_object(&handle_table
, sizeof(CRYPTKEY
), RSAENH_MAGIC_KEY
,
809 destroy_key
, (OBJECTHDR
**)&pCryptKey
);
810 if (hCryptKey
!= (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
812 pCryptKey
->aiAlgid
= aiAlgid
;
813 pCryptKey
->hProv
= hProv
;
814 pCryptKey
->dwModeBits
= 0;
815 pCryptKey
->dwPermissions
= CRYPT_ENCRYPT
| CRYPT_DECRYPT
| CRYPT_READ
| CRYPT_WRITE
|
817 pCryptKey
->dwKeyLen
= dwKeyLen
>> 3;
818 pCryptKey
->dwEffectiveKeyLen
= 0;
819 if ((dwFlags
& CRYPT_CREATE_SALT
) || (dwKeyLen
== 40 && !(dwFlags
& CRYPT_NO_SALT
)))
820 pCryptKey
->dwSaltLen
= 16 /*FIXME*/ - pCryptKey
->dwKeyLen
;
822 pCryptKey
->dwSaltLen
= 0;
823 memset(pCryptKey
->abKeyValue
, 0, sizeof(pCryptKey
->abKeyValue
));
824 memset(pCryptKey
->abInitVector
, 0, sizeof(pCryptKey
->abInitVector
));
825 init_data_blob(&pCryptKey
->siSChannelInfo
.blobClientRandom
);
826 init_data_blob(&pCryptKey
->siSChannelInfo
.blobServerRandom
);
830 case CALG_PCT1_MASTER
:
831 case CALG_SSL2_MASTER
:
832 case CALG_SSL3_MASTER
:
833 case CALG_TLS1_MASTER
:
835 pCryptKey
->dwBlockLen
= 0;
836 pCryptKey
->dwMode
= 0;
843 pCryptKey
->dwBlockLen
= 8;
844 pCryptKey
->dwMode
= CRYPT_MODE_CBC
;
849 pCryptKey
->dwBlockLen
= dwKeyLen
>> 3;
850 pCryptKey
->dwMode
= 0;
854 *ppCryptKey
= pCryptKey
;
860 /******************************************************************************
861 * destroy_key_container [Internal]
863 * Destructor for key containers.
866 * pObjectHdr [I] Pointer to the key container to be destroyed.
868 static void destroy_key_container(OBJECTHDR
*pObjectHdr
)
870 KEYCONTAINER
*pKeyContainer
= (KEYCONTAINER
*)pObjectHdr
;
871 DATA_BLOB blobIn
, blobOut
;
873 CHAR szRSABase
[MAX_PATH
];
878 if (!(pKeyContainer
->dwFlags
& CRYPT_VERIFYCONTEXT
)) {
879 /* On WinXP, persistent keys are stored in a file located at:
880 * $AppData$\\Microsoft\\Crypto\\RSA\\$SID$\\some_hex_string
882 sprintf(szRSABase
, RSAENH_REGKEY
, pKeyContainer
->szName
);
884 if (pKeyContainer
->dwFlags
& CRYPT_MACHINE_KEYSET
) {
885 hRootKey
= HKEY_LOCAL_MACHINE
;
887 hRootKey
= HKEY_CURRENT_USER
;
890 /* @@ Wine registry key: HKLM\Software\Wine\Crypto\RSA */
891 /* @@ Wine registry key: HKCU\Software\Wine\Crypto\RSA */
892 if (RegCreateKeyExA(hRootKey
, szRSABase
, 0, NULL
, REG_OPTION_NON_VOLATILE
,
893 KEY_WRITE
, NULL
, &hKey
, NULL
) == ERROR_SUCCESS
)
895 if (lookup_handle(&handle_table
, pKeyContainer
->hKeyExchangeKeyPair
, RSAENH_MAGIC_KEY
,
898 if (RSAENH_CPExportKey(pKey
->hProv
, pKeyContainer
->hKeyExchangeKeyPair
, 0,
899 PRIVATEKEYBLOB
, 0, 0, &dwLen
))
901 pbKey
= HeapAlloc(GetProcessHeap(), 0, dwLen
);
904 if (RSAENH_CPExportKey(pKey
->hProv
, pKeyContainer
->hKeyExchangeKeyPair
, 0,
905 PRIVATEKEYBLOB
, 0, pbKey
, &dwLen
))
907 blobIn
.pbData
= pbKey
;
908 blobIn
.cbData
= dwLen
;
910 if (CryptProtectData(&blobIn
, NULL
, NULL
, NULL
, NULL
,
911 (pKeyContainer
->dwFlags
& CRYPT_MACHINE_KEYSET
) ?
912 CRYPTPROTECT_LOCAL_MACHINE
: 0,
915 RegSetValueExA(hKey
, "KeyExchangeKeyPair", 0, REG_BINARY
,
916 blobOut
.pbData
, blobOut
.cbData
);
917 HeapFree(GetProcessHeap(), 0, blobOut
.pbData
);
920 HeapFree(GetProcessHeap(), 0, pbKey
);
923 release_handle(&handle_table
, pKeyContainer
->hKeyExchangeKeyPair
,
927 if (lookup_handle(&handle_table
, pKeyContainer
->hSignatureKeyPair
, RSAENH_MAGIC_KEY
,
930 if (RSAENH_CPExportKey(pKey
->hProv
, pKeyContainer
->hSignatureKeyPair
, 0,
931 PRIVATEKEYBLOB
, 0, 0, &dwLen
))
933 pbKey
= HeapAlloc(GetProcessHeap(), 0, dwLen
);
936 if (RSAENH_CPExportKey(pKey
->hProv
, pKeyContainer
->hSignatureKeyPair
, 0,
937 PRIVATEKEYBLOB
, 0, pbKey
, &dwLen
))
939 blobIn
.pbData
= pbKey
;
940 blobIn
.cbData
= dwLen
;
942 if (CryptProtectData(&blobIn
, NULL
, NULL
, NULL
, NULL
,
943 (pKeyContainer
->dwFlags
& CRYPT_MACHINE_KEYSET
) ?
944 CRYPTPROTECT_LOCAL_MACHINE
: 0,
947 RegSetValueExA(hKey
, "SignatureKeyPair", 0, REG_BINARY
,
948 blobOut
.pbData
, blobOut
.cbData
);
949 HeapFree(GetProcessHeap(), 0, blobOut
.pbData
);
952 HeapFree(GetProcessHeap(), 0, pbKey
);
955 release_handle(&handle_table
, pKeyContainer
->hSignatureKeyPair
,
963 HeapFree( GetProcessHeap(), 0, pKeyContainer
);
966 /******************************************************************************
967 * new_key_container [Internal]
969 * Create a new key container. The personality (RSA Base, Strong or Enhanced CP)
970 * of the CSP is determined via the pVTable->pszProvName string.
973 * pszContainerName [I] Name of the key container.
974 * pVTable [I] Callback functions and context info provided by the OS
977 * Success: Handle to the new key container.
978 * Failure: INVALID_HANDLE_VALUE
980 static HCRYPTPROV
new_key_container(PCCH pszContainerName
, DWORD dwFlags
, const VTableProvStruc
*pVTable
)
982 KEYCONTAINER
*pKeyContainer
;
983 HCRYPTPROV hKeyContainer
;
985 hKeyContainer
= (HCRYPTPROV
)new_object(&handle_table
, sizeof(KEYCONTAINER
), RSAENH_MAGIC_CONTAINER
,
986 destroy_key_container
, (OBJECTHDR
**)&pKeyContainer
);
987 if (hKeyContainer
!= (HCRYPTPROV
)INVALID_HANDLE_VALUE
)
989 lstrcpynA(pKeyContainer
->szName
, pszContainerName
, MAX_PATH
);
990 pKeyContainer
->dwFlags
= dwFlags
;
991 pKeyContainer
->dwEnumAlgsCtr
= 0;
992 pKeyContainer
->hKeyExchangeKeyPair
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
993 pKeyContainer
->hSignatureKeyPair
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
994 if (pVTable
&& pVTable
->pszProvName
) {
995 lstrcpynA(pKeyContainer
->szProvName
, pVTable
->pszProvName
, MAX_PATH
);
996 if (!strcmp(pVTable
->pszProvName
, MS_DEF_PROV_A
)) {
997 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_BASE
;
998 } else if (!strcmp(pVTable
->pszProvName
, MS_ENHANCED_PROV_A
)) {
999 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_ENHANCED
;
1000 } else if (!strcmp(pVTable
->pszProvName
, MS_DEF_RSA_SCHANNEL_PROV_A
)) {
1001 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_SCHANNEL
;
1003 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_STRONG
;
1007 /* The new key container has to be inserted into the CSP immediately
1008 * after creation to be available for CPGetProvParam's PP_ENUMCONTAINERS. */
1009 if (!(dwFlags
& CRYPT_VERIFYCONTEXT
)) {
1010 CHAR szRSABase
[MAX_PATH
];
1011 HKEY hRootKey
, hKey
;
1013 sprintf(szRSABase
, RSAENH_REGKEY
, pKeyContainer
->szName
);
1015 if (pKeyContainer
->dwFlags
& CRYPT_MACHINE_KEYSET
) {
1016 hRootKey
= HKEY_LOCAL_MACHINE
;
1018 hRootKey
= HKEY_CURRENT_USER
;
1021 /* @@ Wine registry key: HKLM\Software\Wine\Crypto\RSA */
1022 /* @@ Wine registry key: HKCU\Software\Wine\Crypto\RSA */
1023 RegCreateKeyA(hRootKey
, szRSABase
, &hKey
);
1028 return hKeyContainer
;
1031 /******************************************************************************
1032 * read_key_container [Internal]
1034 * Tries to read the persistent state of the key container (mainly the signature
1035 * and key exchange private keys) given by pszContainerName.
1038 * pszContainerName [I] Name of the key container to read from the registry
1039 * pVTable [I] Pointer to context data provided by the operating system
1042 * Success: Handle to the key container read from the registry
1043 * Failure: INVALID_HANDLE_VALUE
1045 static HCRYPTPROV
read_key_container(PCHAR pszContainerName
, DWORD dwFlags
, const VTableProvStruc
*pVTable
)
1047 CHAR szRSABase
[MAX_PATH
];
1049 HKEY hKey
, hRootKey
;
1050 DWORD dwValueType
, dwLen
;
1051 KEYCONTAINER
*pKeyContainer
;
1052 HCRYPTPROV hKeyContainer
;
1053 DATA_BLOB blobIn
, blobOut
;
1054 HCRYPTKEY hCryptKey
;
1056 sprintf(szRSABase
, RSAENH_REGKEY
, pszContainerName
);
1058 if (dwFlags
& CRYPT_MACHINE_KEYSET
) {
1059 hRootKey
= HKEY_LOCAL_MACHINE
;
1061 hRootKey
= HKEY_CURRENT_USER
;
1064 /* @@ Wine registry key: HKLM\Software\Wine\Crypto\RSA */
1065 /* @@ Wine registry key: HKCU\Software\Wine\Crypto\RSA */
1066 if (RegOpenKeyExA(hRootKey
, szRSABase
, 0, KEY_READ
, &hKey
) != ERROR_SUCCESS
)
1068 SetLastError(NTE_BAD_KEYSET
);
1069 return (HCRYPTPROV
)INVALID_HANDLE_VALUE
;
1072 hKeyContainer
= new_key_container(pszContainerName
, dwFlags
, pVTable
);
1073 if (hKeyContainer
!= (HCRYPTPROV
)INVALID_HANDLE_VALUE
)
1075 if (!lookup_handle(&handle_table
, hKeyContainer
, RSAENH_MAGIC_CONTAINER
,
1076 (OBJECTHDR
**)&pKeyContainer
))
1077 return (HCRYPTPROV
)INVALID_HANDLE_VALUE
;
1079 if (RegQueryValueExA(hKey
, "KeyExchangeKeyPair", 0, &dwValueType
, NULL
, &dwLen
) ==
1082 pbKey
= HeapAlloc(GetProcessHeap(), 0, dwLen
);
1085 if (RegQueryValueExA(hKey
, "KeyExchangeKeyPair", 0, &dwValueType
, pbKey
, &dwLen
) ==
1088 blobIn
.pbData
= pbKey
;
1089 blobIn
.cbData
= dwLen
;
1091 if (CryptUnprotectData(&blobIn
, NULL
, NULL
, NULL
, NULL
,
1092 (dwFlags
& CRYPT_MACHINE_KEYSET
) ? CRYPTPROTECT_LOCAL_MACHINE
: 0, &blobOut
))
1094 if(RSAENH_CPImportKey(hKeyContainer
, blobOut
.pbData
, blobOut
.cbData
, 0, 0,
1096 pKeyContainer
->hKeyExchangeKeyPair
= hCryptKey
;
1097 HeapFree(GetProcessHeap(), 0, blobOut
.pbData
);
1100 HeapFree(GetProcessHeap(), 0, pbKey
);
1104 if (RegQueryValueExA(hKey
, "SignatureKeyPair", 0, &dwValueType
, NULL
, &dwLen
) ==
1107 pbKey
= HeapAlloc(GetProcessHeap(), 0, dwLen
);
1110 if (RegQueryValueExA(hKey
, "SignatureKeyPair", 0, &dwValueType
, pbKey
, &dwLen
) ==
1113 blobIn
.pbData
= pbKey
;
1114 blobIn
.cbData
= dwLen
;
1116 if (CryptUnprotectData(&blobIn
, NULL
, NULL
, NULL
, NULL
,
1117 (dwFlags
& CRYPT_MACHINE_KEYSET
) ? CRYPTPROTECT_LOCAL_MACHINE
: 0, &blobOut
))
1119 if(RSAENH_CPImportKey(hKeyContainer
, blobOut
.pbData
, blobOut
.cbData
, 0, 0,
1121 pKeyContainer
->hSignatureKeyPair
= hCryptKey
;
1122 HeapFree(GetProcessHeap(), 0, blobOut
.pbData
);
1125 HeapFree(GetProcessHeap(), 0, pbKey
);
1130 return hKeyContainer
;
1133 /******************************************************************************
1134 * build_hash_signature [Internal]
1136 * Builds a padded version of a hash to match the length of the RSA key modulus.
1139 * pbSignature [O] The padded hash object is stored here.
1140 * dwLen [I] Length of the pbSignature buffer.
1141 * aiAlgid [I] Algorithm identifier of the hash to be padded.
1142 * abHashValue [I] The value of the hash object.
1143 * dwHashLen [I] Length of the hash value.
1144 * dwFlags [I] Selection of padding algorithm.
1148 * Failure: FALSE (NTE_BAD_ALGID)
1150 static BOOL
build_hash_signature(BYTE
*pbSignature
, DWORD dwLen
, ALG_ID aiAlgid
,
1151 CONST BYTE
*abHashValue
, DWORD dwHashLen
, DWORD dwFlags
)
1153 /* These prefixes are meant to be concatenated with hash values of the
1154 * respective kind to form a PKCS #7 DigestInfo. */
1155 static const struct tagOIDDescriptor
{
1158 CONST BYTE abOID
[18];
1159 } aOIDDescriptor
[5] = {
1160 { CALG_MD2
, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1161 0x86, 0xf7, 0x0d, 0x02, 0x02, 0x05, 0x00, 0x04, 0x10 } },
1162 { CALG_MD4
, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1163 0x86, 0xf7, 0x0d, 0x02, 0x04, 0x05, 0x00, 0x04, 0x10 } },
1164 { CALG_MD5
, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1165 0x86, 0xf7, 0x0d, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10 } },
1166 { CALG_SHA
, 15, { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03,
1167 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14 } },
1170 DWORD dwIdxOID
, i
, j
;
1172 for (dwIdxOID
= 0; aOIDDescriptor
[dwIdxOID
].aiAlgid
; dwIdxOID
++) {
1173 if (aOIDDescriptor
[dwIdxOID
].aiAlgid
== aiAlgid
) break;
1176 if (!aOIDDescriptor
[dwIdxOID
].aiAlgid
) {
1177 SetLastError(NTE_BAD_ALGID
);
1181 /* Build the padded signature */
1182 if (dwFlags
& CRYPT_X931_FORMAT
) {
1183 pbSignature
[0] = 0x6b;
1184 for (i
=1; i
< dwLen
- dwHashLen
- 3; i
++) {
1185 pbSignature
[i
] = 0xbb;
1187 pbSignature
[i
++] = 0xba;
1188 for (j
=0; j
< dwHashLen
; j
++, i
++) {
1189 pbSignature
[i
] = abHashValue
[j
];
1191 pbSignature
[i
++] = 0x33;
1192 pbSignature
[i
++] = 0xcc;
1194 pbSignature
[0] = 0x00;
1195 pbSignature
[1] = 0x01;
1196 if (dwFlags
& CRYPT_NOHASHOID
) {
1197 for (i
=2; i
< dwLen
- 1 - dwHashLen
; i
++) {
1198 pbSignature
[i
] = 0xff;
1200 pbSignature
[i
++] = 0x00;
1202 for (i
=2; i
< dwLen
- 1 - aOIDDescriptor
[dwIdxOID
].dwLen
- dwHashLen
; i
++) {
1203 pbSignature
[i
] = 0xff;
1205 pbSignature
[i
++] = 0x00;
1206 for (j
=0; j
< aOIDDescriptor
[dwIdxOID
].dwLen
; j
++) {
1207 pbSignature
[i
++] = aOIDDescriptor
[dwIdxOID
].abOID
[j
];
1210 for (j
=0; j
< dwHashLen
; j
++) {
1211 pbSignature
[i
++] = abHashValue
[j
];
1218 /******************************************************************************
1221 * This is an implementation of the 'P_hash' helper function for TLS1's PRF.
1222 * It is used exclusively by tls1_prf. For details see RFC 2246, chapter 5.
1223 * The pseudo random stream generated by this function is exclusive or'ed with
1224 * the data in pbBuffer.
1227 * hHMAC [I] HMAC object, which will be used in pseudo random generation
1228 * pblobSeed [I] Seed value
1229 * pbBuffer [I/O] Pseudo random stream will be xor'ed to the provided data
1230 * dwBufferLen [I] Number of pseudo random bytes desired
1236 static BOOL
tls1_p(HCRYPTHASH hHMAC
, CONST PCRYPT_DATA_BLOB pblobSeed
, PBYTE pbBuffer
, DWORD dwBufferLen
)
1239 BYTE abAi
[RSAENH_MAX_HASH_SIZE
];
1242 if (!lookup_handle(&handle_table
, hHMAC
, RSAENH_MAGIC_HASH
, (OBJECTHDR
**)&pHMAC
)) {
1243 SetLastError(NTE_BAD_HASH
);
1247 /* compute A_1 = HMAC(seed) */
1249 update_hash(pHMAC
, pblobSeed
->pbData
, pblobSeed
->cbData
);
1250 finalize_hash(pHMAC
);
1251 memcpy(abAi
, pHMAC
->abHashValue
, pHMAC
->dwHashSize
);
1254 /* compute HMAC(A_i + seed) */
1256 update_hash(pHMAC
, abAi
, pHMAC
->dwHashSize
);
1257 update_hash(pHMAC
, pblobSeed
->pbData
, pblobSeed
->cbData
);
1258 finalize_hash(pHMAC
);
1260 /* pseudo random stream := CONCAT_{i=1..n} ( HMAC(A_i + seed) ) */
1262 if (i
>= dwBufferLen
) break;
1263 pbBuffer
[i
] ^= pHMAC
->abHashValue
[i
% pHMAC
->dwHashSize
];
1265 } while (i
% pHMAC
->dwHashSize
);
1267 /* compute A_{i+1} = HMAC(A_i) */
1269 update_hash(pHMAC
, abAi
, pHMAC
->dwHashSize
);
1270 finalize_hash(pHMAC
);
1271 memcpy(abAi
, pHMAC
->abHashValue
, pHMAC
->dwHashSize
);
1272 } while (i
< dwBufferLen
);
1277 /******************************************************************************
1278 * tls1_prf [Internal]
1280 * TLS1 pseudo random function as specified in RFC 2246, chapter 5
1283 * hProv [I] Key container used to compute the pseudo random stream
1284 * hSecret [I] Key that holds the (pre-)master secret
1285 * pblobLabel [I] Descriptive label
1286 * pblobSeed [I] Seed value
1287 * pbBuffer [O] Pseudo random numbers will be stored here
1288 * dwBufferLen [I] Number of pseudo random bytes desired
1294 static BOOL
tls1_prf(HCRYPTPROV hProv
, HCRYPTPROV hSecret
, CONST PCRYPT_DATA_BLOB pblobLabel
,
1295 CONST PCRYPT_DATA_BLOB pblobSeed
, PBYTE pbBuffer
, DWORD dwBufferLen
)
1297 HMAC_INFO hmacInfo
= { 0, NULL
, 0, NULL
, 0 };
1298 HCRYPTHASH hHMAC
= (HCRYPTHASH
)INVALID_HANDLE_VALUE
;
1299 HCRYPTKEY hHalfSecret
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
1300 CRYPTKEY
*pHalfSecret
, *pSecret
;
1301 DWORD dwHalfSecretLen
;
1302 BOOL result
= FALSE
;
1303 CRYPT_DATA_BLOB blobLabelSeed
;
1305 TRACE("(hProv=%08lx, hSecret=%08lx, pblobLabel=%p, pblobSeed=%p, pbBuffer=%p, dwBufferLen=%d)\n",
1306 hProv
, hSecret
, pblobLabel
, pblobSeed
, pbBuffer
, dwBufferLen
);
1308 if (!lookup_handle(&handle_table
, hSecret
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pSecret
)) {
1309 SetLastError(NTE_FAIL
);
1313 dwHalfSecretLen
= (pSecret
->dwKeyLen
+1)/2;
1315 /* concatenation of the label and the seed */
1316 if (!concat_data_blobs(&blobLabelSeed
, pblobLabel
, pblobSeed
)) goto exit
;
1318 /* zero out the buffer, since two random streams will be xor'ed into it. */
1319 memset(pbBuffer
, 0, dwBufferLen
);
1321 /* build a 'fake' key, to hold the secret. CALG_SSL2_MASTER is used since it provides
1322 * the biggest range of valid key lengths. */
1323 hHalfSecret
= new_key(hProv
, CALG_SSL2_MASTER
, MAKELONG(0,dwHalfSecretLen
*8), &pHalfSecret
);
1324 if (hHalfSecret
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) goto exit
;
1326 /* Derive an HMAC_MD5 hash and call the helper function. */
1327 memcpy(pHalfSecret
->abKeyValue
, pSecret
->abKeyValue
, dwHalfSecretLen
);
1328 if (!RSAENH_CPCreateHash(hProv
, CALG_HMAC
, hHalfSecret
, 0, &hHMAC
)) goto exit
;
1329 hmacInfo
.HashAlgid
= CALG_MD5
;
1330 if (!RSAENH_CPSetHashParam(hProv
, hHMAC
, HP_HMAC_INFO
, (BYTE
*)&hmacInfo
, 0)) goto exit
;
1331 if (!tls1_p(hHMAC
, &blobLabelSeed
, pbBuffer
, dwBufferLen
)) goto exit
;
1333 /* Reconfigure to HMAC_SHA hash and call helper function again. */
1334 memcpy(pHalfSecret
->abKeyValue
, pSecret
->abKeyValue
+ (pSecret
->dwKeyLen
/2), dwHalfSecretLen
);
1335 hmacInfo
.HashAlgid
= CALG_SHA
;
1336 if (!RSAENH_CPSetHashParam(hProv
, hHMAC
, HP_HMAC_INFO
, (BYTE
*)&hmacInfo
, 0)) goto exit
;
1337 if (!tls1_p(hHMAC
, &blobLabelSeed
, pbBuffer
, dwBufferLen
)) goto exit
;
1341 release_handle(&handle_table
, hHalfSecret
, RSAENH_MAGIC_KEY
);
1342 if (hHMAC
!= (HCRYPTHASH
)INVALID_HANDLE_VALUE
) RSAENH_CPDestroyHash(hProv
, hHMAC
);
1343 free_data_blob(&blobLabelSeed
);
1347 /******************************************************************************
1348 * pad_data [Internal]
1350 * Helper function for data padding according to PKCS1 #2
1353 * abData [I] The data to be padded
1354 * dwDataLen [I] Length of the data
1355 * abBuffer [O] Padded data will be stored here
1356 * dwBufferLen [I] Length of the buffer (also length of padded data)
1357 * dwFlags [I] Padding format (CRYPT_SSL2_FALLBACK)
1361 * Failure: FALSE (NTE_BAD_LEN, too much data to pad)
1363 static BOOL
pad_data(CONST BYTE
*abData
, DWORD dwDataLen
, BYTE
*abBuffer
, DWORD dwBufferLen
,
1368 /* Ensure there is enough space for PKCS1 #2 padding */
1369 if (dwDataLen
> dwBufferLen
-11) {
1370 SetLastError(NTE_BAD_LEN
);
1374 memmove(abBuffer
+ dwBufferLen
- dwDataLen
, abData
, dwDataLen
);
1377 abBuffer
[1] = RSAENH_PKC_BLOCKTYPE
;
1378 for (i
=2; i
< dwBufferLen
- dwDataLen
- 1; i
++)
1379 do gen_rand_impl(&abBuffer
[i
], 1); while (!abBuffer
[i
]);
1380 if (dwFlags
& CRYPT_SSL2_FALLBACK
)
1381 for (i
-=8; i
< dwBufferLen
- dwDataLen
- 1; i
++)
1388 /******************************************************************************
1389 * unpad_data [Internal]
1391 * Remove the PKCS1 padding from RSA decrypted data
1394 * abData [I] The padded data
1395 * dwDataLen [I] Length of the padded data
1396 * abBuffer [O] Data without padding will be stored here
1397 * dwBufferLen [I/O] I: Length of the buffer, O: Length of unpadded data
1398 * dwFlags [I] Currently none defined
1402 * Failure: FALSE, (NTE_BAD_DATA, no valid PKCS1 padding or buffer too small)
1404 static BOOL
unpad_data(CONST BYTE
*abData
, DWORD dwDataLen
, BYTE
*abBuffer
, DWORD
*dwBufferLen
,
1409 for (i
=2; i
<dwDataLen
; i
++)
1413 if ((i
== dwDataLen
) || (*dwBufferLen
< dwDataLen
- i
- 1) ||
1414 (abData
[0] != 0x00) || (abData
[1] != RSAENH_PKC_BLOCKTYPE
))
1416 SetLastError(NTE_BAD_DATA
);
1420 *dwBufferLen
= dwDataLen
- i
- 1;
1421 memmove(abBuffer
, abData
+ i
+ 1, *dwBufferLen
);
1425 /******************************************************************************
1426 * CPAcquireContext (RSAENH.@)
1428 * Acquire a handle to the key container specified by pszContainer
1431 * phProv [O] Pointer to the location the acquired handle will be written to.
1432 * pszContainer [I] Name of the desired key container. See Notes
1433 * dwFlags [I] Flags. See Notes.
1434 * pVTable [I] Pointer to a PVTableProvStruct containing callbacks.
1441 * If pszContainer is NULL or points to a zero length string the user's login
1442 * name will be used as the key container name.
1444 * If the CRYPT_NEW_KEYSET flag is set in dwFlags a new keyset will be created.
1445 * If a keyset with the given name already exists, the function fails and sets
1446 * last error to NTE_EXISTS. If CRYPT_NEW_KEYSET is not set and the specified
1447 * key container does not exist, function fails and sets last error to
1450 BOOL WINAPI
RSAENH_CPAcquireContext(HCRYPTPROV
*phProv
, LPSTR pszContainer
,
1451 DWORD dwFlags
, PVTableProvStruc pVTable
)
1453 CHAR szKeyContainerName
[MAX_PATH
];
1454 CHAR szRegKey
[MAX_PATH
];
1456 TRACE("(phProv=%p, pszContainer=%s, dwFlags=%08x, pVTable=%p)\n", phProv
,
1457 debugstr_a(pszContainer
), dwFlags
, pVTable
);
1459 if (pszContainer
&& *pszContainer
)
1461 lstrcpynA(szKeyContainerName
, pszContainer
, MAX_PATH
);
1465 DWORD dwLen
= sizeof(szKeyContainerName
);
1466 if (!GetUserNameA(szKeyContainerName
, &dwLen
)) return FALSE
;
1469 switch (dwFlags
& (CRYPT_NEWKEYSET
|CRYPT_VERIFYCONTEXT
|CRYPT_DELETEKEYSET
))
1472 *phProv
= read_key_container(szKeyContainerName
, dwFlags
, pVTable
);
1475 case CRYPT_DELETEKEYSET
:
1476 if (snprintf(szRegKey
, MAX_PATH
, RSAENH_REGKEY
, szKeyContainerName
) >= MAX_PATH
) {
1477 SetLastError(NTE_BAD_KEYSET_PARAM
);
1481 if (dwFlags
& CRYPT_MACHINE_KEYSET
)
1482 hRootKey
= HKEY_LOCAL_MACHINE
;
1484 hRootKey
= HKEY_CURRENT_USER
;
1485 if (!RegDeleteKeyA(hRootKey
, szRegKey
)) {
1486 SetLastError(ERROR_SUCCESS
);
1489 SetLastError(NTE_BAD_KEYSET
);
1495 case CRYPT_NEWKEYSET
:
1496 *phProv
= read_key_container(szKeyContainerName
, dwFlags
, pVTable
);
1497 if (*phProv
!= (HCRYPTPROV
)INVALID_HANDLE_VALUE
)
1499 release_handle(&handle_table
, *phProv
, RSAENH_MAGIC_CONTAINER
);
1500 TRACE("Can't create new keyset, already exists\n");
1501 SetLastError(NTE_EXISTS
);
1504 *phProv
= new_key_container(szKeyContainerName
, dwFlags
, pVTable
);
1507 case CRYPT_VERIFYCONTEXT
|CRYPT_NEWKEYSET
:
1508 case CRYPT_VERIFYCONTEXT
:
1510 TRACE("pszContainer should be NULL\n");
1511 SetLastError(NTE_BAD_FLAGS
);
1514 *phProv
= new_key_container("", dwFlags
, pVTable
);
1518 *phProv
= (HCRYPTPROV
)INVALID_HANDLE_VALUE
;
1519 SetLastError(NTE_BAD_FLAGS
);
1523 if (*phProv
!= (HCRYPTPROV
)INVALID_HANDLE_VALUE
) {
1524 SetLastError(ERROR_SUCCESS
);
1531 /******************************************************************************
1532 * CPCreateHash (RSAENH.@)
1534 * CPCreateHash creates and initalizes a new hash object.
1537 * hProv [I] Handle to the key container to which the new hash will belong.
1538 * Algid [I] Identifies the hash algorithm, which will be used for the hash.
1539 * hKey [I] Handle to a session key applied for keyed hashes.
1540 * dwFlags [I] Currently no flags defined. Must be zero.
1541 * phHash [O] Points to the location where a handle to the new hash will be stored.
1548 * hKey is a handle to a session key applied in keyed hashes like MAC and HMAC.
1549 * If a normal hash object is to be created (like e.g. MD2 or SHA1) hKey must be zero.
1551 BOOL WINAPI
RSAENH_CPCreateHash(HCRYPTPROV hProv
, ALG_ID Algid
, HCRYPTKEY hKey
, DWORD dwFlags
,
1554 CRYPTKEY
*pCryptKey
;
1555 CRYPTHASH
*pCryptHash
;
1556 const PROV_ENUMALGS_EX
*peaAlgidInfo
;
1558 TRACE("(hProv=%08lx, Algid=%08x, hKey=%08lx, dwFlags=%08x, phHash=%p)\n", hProv
, Algid
, hKey
,
1561 peaAlgidInfo
= get_algid_info(hProv
, Algid
);
1562 if (!peaAlgidInfo
) return FALSE
;
1566 SetLastError(NTE_BAD_FLAGS
);
1570 if (Algid
== CALG_MAC
|| Algid
== CALG_HMAC
|| Algid
== CALG_SCHANNEL_MASTER_HASH
||
1571 Algid
== CALG_TLS1PRF
)
1573 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
)) {
1574 SetLastError(NTE_BAD_KEY
);
1578 if ((Algid
== CALG_MAC
) && (GET_ALG_TYPE(pCryptKey
->aiAlgid
) != ALG_TYPE_BLOCK
)) {
1579 SetLastError(NTE_BAD_KEY
);
1583 if ((Algid
== CALG_SCHANNEL_MASTER_HASH
|| Algid
== CALG_TLS1PRF
) &&
1584 (pCryptKey
->aiAlgid
!= CALG_TLS1_MASTER
))
1586 SetLastError(NTE_BAD_KEY
);
1590 if ((Algid
== CALG_TLS1PRF
) && (pCryptKey
->dwState
!= RSAENH_KEYSTATE_MASTERKEY
)) {
1591 SetLastError(NTE_BAD_KEY_STATE
);
1596 *phHash
= (HCRYPTHASH
)new_object(&handle_table
, sizeof(CRYPTHASH
), RSAENH_MAGIC_HASH
,
1597 destroy_hash
, (OBJECTHDR
**)&pCryptHash
);
1598 if (!pCryptHash
) return FALSE
;
1600 pCryptHash
->aiAlgid
= Algid
;
1601 pCryptHash
->hKey
= hKey
;
1602 pCryptHash
->hProv
= hProv
;
1603 pCryptHash
->dwState
= RSAENH_HASHSTATE_HASHING
;
1604 pCryptHash
->pHMACInfo
= (PHMAC_INFO
)NULL
;
1605 pCryptHash
->dwHashSize
= peaAlgidInfo
->dwDefaultLen
>> 3;
1606 init_data_blob(&pCryptHash
->tpPRFParams
.blobLabel
);
1607 init_data_blob(&pCryptHash
->tpPRFParams
.blobSeed
);
1609 if (Algid
== CALG_SCHANNEL_MASTER_HASH
) {
1610 static const char keyex
[] = "key expansion";
1611 BYTE key_expansion
[sizeof keyex
];
1612 CRYPT_DATA_BLOB blobRandom
, blobKeyExpansion
= { 13, key_expansion
};
1614 memcpy( key_expansion
, keyex
, sizeof keyex
);
1616 if (pCryptKey
->dwState
!= RSAENH_KEYSTATE_MASTERKEY
) {
1617 static const char msec
[] = "master secret";
1618 BYTE master_secret
[sizeof msec
];
1619 CRYPT_DATA_BLOB blobLabel
= { 13, master_secret
};
1620 BYTE abKeyValue
[48];
1622 memcpy( master_secret
, msec
, sizeof msec
);
1624 /* See RFC 2246, chapter 8.1 */
1625 if (!concat_data_blobs(&blobRandom
,
1626 &pCryptKey
->siSChannelInfo
.blobClientRandom
,
1627 &pCryptKey
->siSChannelInfo
.blobServerRandom
))
1631 tls1_prf(hProv
, hKey
, &blobLabel
, &blobRandom
, abKeyValue
, 48);
1632 pCryptKey
->dwState
= RSAENH_KEYSTATE_MASTERKEY
;
1633 memcpy(pCryptKey
->abKeyValue
, abKeyValue
, 48);
1634 free_data_blob(&blobRandom
);
1637 /* See RFC 2246, chapter 6.3 */
1638 if (!concat_data_blobs(&blobRandom
,
1639 &pCryptKey
->siSChannelInfo
.blobServerRandom
,
1640 &pCryptKey
->siSChannelInfo
.blobClientRandom
))
1644 tls1_prf(hProv
, hKey
, &blobKeyExpansion
, &blobRandom
, pCryptHash
->abHashValue
,
1645 RSAENH_MAX_HASH_SIZE
);
1646 free_data_blob(&blobRandom
);
1649 return init_hash(pCryptHash
);
1652 /******************************************************************************
1653 * CPDestroyHash (RSAENH.@)
1655 * Releases the handle to a hash object. The object is destroyed if it's reference
1656 * count reaches zero.
1659 * hProv [I] Handle to the key container to which the hash object belongs.
1660 * hHash [I] Handle to the hash object to be released.
1666 BOOL WINAPI
RSAENH_CPDestroyHash(HCRYPTPROV hProv
, HCRYPTHASH hHash
)
1668 TRACE("(hProv=%08lx, hHash=%08lx)\n", hProv
, hHash
);
1670 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
1672 SetLastError(NTE_BAD_UID
);
1676 if (!release_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
))
1678 SetLastError(NTE_BAD_HASH
);
1685 /******************************************************************************
1686 * CPDestroyKey (RSAENH.@)
1688 * Releases the handle to a key object. The object is destroyed if it's reference
1689 * count reaches zero.
1692 * hProv [I] Handle to the key container to which the key object belongs.
1693 * hKey [I] Handle to the key object to be released.
1699 BOOL WINAPI
RSAENH_CPDestroyKey(HCRYPTPROV hProv
, HCRYPTKEY hKey
)
1701 TRACE("(hProv=%08lx, hKey=%08lx)\n", hProv
, hKey
);
1703 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
1705 SetLastError(NTE_BAD_UID
);
1709 if (!release_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
))
1711 SetLastError(NTE_BAD_KEY
);
1718 /******************************************************************************
1719 * CPDuplicateHash (RSAENH.@)
1721 * Clones a hash object including it's current state.
1724 * hUID [I] Handle to the key container the hash belongs to.
1725 * hHash [I] Handle to the hash object to be cloned.
1726 * pdwReserved [I] Reserved. Must be NULL.
1727 * dwFlags [I] No flags are currently defined. Must be 0.
1728 * phHash [O] Handle to the cloned hash object.
1734 BOOL WINAPI
RSAENH_CPDuplicateHash(HCRYPTPROV hUID
, HCRYPTHASH hHash
, DWORD
*pdwReserved
,
1735 DWORD dwFlags
, HCRYPTHASH
*phHash
)
1737 CRYPTHASH
*pSrcHash
, *pDestHash
;
1739 TRACE("(hUID=%08lx, hHash=%08lx, pdwReserved=%p, dwFlags=%08x, phHash=%p)\n", hUID
, hHash
,
1740 pdwReserved
, dwFlags
, phHash
);
1742 if (!is_valid_handle(&handle_table
, hUID
, RSAENH_MAGIC_CONTAINER
))
1744 SetLastError(NTE_BAD_UID
);
1748 if (!lookup_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
, (OBJECTHDR
**)&pSrcHash
))
1750 SetLastError(NTE_BAD_HASH
);
1754 if (!phHash
|| pdwReserved
|| dwFlags
)
1756 SetLastError(ERROR_INVALID_PARAMETER
);
1760 *phHash
= (HCRYPTHASH
)new_object(&handle_table
, sizeof(CRYPTHASH
), RSAENH_MAGIC_HASH
,
1761 destroy_hash
, (OBJECTHDR
**)&pDestHash
);
1762 if (*phHash
!= (HCRYPTHASH
)INVALID_HANDLE_VALUE
)
1764 memcpy(pDestHash
, pSrcHash
, sizeof(CRYPTHASH
));
1765 duplicate_hash_impl(pSrcHash
->aiAlgid
, &pSrcHash
->context
, &pDestHash
->context
);
1766 copy_hmac_info(&pDestHash
->pHMACInfo
, pSrcHash
->pHMACInfo
);
1767 copy_data_blob(&pDestHash
->tpPRFParams
.blobLabel
, &pSrcHash
->tpPRFParams
.blobLabel
);
1768 copy_data_blob(&pDestHash
->tpPRFParams
.blobSeed
, &pSrcHash
->tpPRFParams
.blobSeed
);
1771 return *phHash
!= (HCRYPTHASH
)INVALID_HANDLE_VALUE
;
1774 /******************************************************************************
1775 * CPDuplicateKey (RSAENH.@)
1777 * Clones a key object including it's current state.
1780 * hUID [I] Handle to the key container the hash belongs to.
1781 * hKey [I] Handle to the key object to be cloned.
1782 * pdwReserved [I] Reserved. Must be NULL.
1783 * dwFlags [I] No flags are currently defined. Must be 0.
1784 * phHash [O] Handle to the cloned key object.
1790 BOOL WINAPI
RSAENH_CPDuplicateKey(HCRYPTPROV hUID
, HCRYPTKEY hKey
, DWORD
*pdwReserved
,
1791 DWORD dwFlags
, HCRYPTKEY
*phKey
)
1793 CRYPTKEY
*pSrcKey
, *pDestKey
;
1795 TRACE("(hUID=%08lx, hKey=%08lx, pdwReserved=%p, dwFlags=%08x, phKey=%p)\n", hUID
, hKey
,
1796 pdwReserved
, dwFlags
, phKey
);
1798 if (!is_valid_handle(&handle_table
, hUID
, RSAENH_MAGIC_CONTAINER
))
1800 SetLastError(NTE_BAD_UID
);
1804 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pSrcKey
))
1806 SetLastError(NTE_BAD_KEY
);
1810 if (!phKey
|| pdwReserved
|| dwFlags
)
1812 SetLastError(ERROR_INVALID_PARAMETER
);
1816 *phKey
= (HCRYPTKEY
)new_object(&handle_table
, sizeof(CRYPTKEY
), RSAENH_MAGIC_KEY
, destroy_key
,
1817 (OBJECTHDR
**)&pDestKey
);
1818 if (*phKey
!= (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
1820 memcpy(pDestKey
, pSrcKey
, sizeof(CRYPTKEY
));
1821 copy_data_blob(&pDestKey
->siSChannelInfo
.blobServerRandom
,
1822 &pSrcKey
->siSChannelInfo
.blobServerRandom
);
1823 copy_data_blob(&pDestKey
->siSChannelInfo
.blobClientRandom
,
1824 &pSrcKey
->siSChannelInfo
.blobClientRandom
);
1825 duplicate_key_impl(pSrcKey
->aiAlgid
, &pSrcKey
->context
, &pDestKey
->context
);
1834 /******************************************************************************
1835 * CPEncrypt (RSAENH.@)
1840 * hProv [I] The key container hKey and hHash belong to.
1841 * hKey [I] The key used to encrypt the data.
1842 * hHash [I] An optional hash object for parallel hashing. See notes.
1843 * Final [I] Indicates if this is the last block of data to encrypt.
1844 * dwFlags [I] Currently no flags defined. Must be zero.
1845 * pbData [I/O] Pointer to the data to encrypt. Encrypted data will also be stored there.
1846 * pdwDataLen [I/O] I: Length of data to encrypt, O: Length of encrypted data.
1847 * dwBufLen [I] Size of the buffer at pbData.
1854 * If a hash object handle is provided in hHash, it will be updated with the plaintext.
1855 * This is useful for message signatures.
1857 * This function uses the standard WINAPI protocol for querying data of dynamic length.
1859 BOOL WINAPI
RSAENH_CPEncrypt(HCRYPTPROV hProv
, HCRYPTKEY hKey
, HCRYPTHASH hHash
, BOOL Final
,
1860 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
, DWORD dwBufLen
)
1862 CRYPTKEY
*pCryptKey
;
1863 BYTE
*in
, out
[RSAENH_MAX_BLOCK_SIZE
], o
[RSAENH_MAX_BLOCK_SIZE
];
1864 DWORD dwEncryptedLen
, i
, j
, k
;
1866 TRACE("(hProv=%08lx, hKey=%08lx, hHash=%08lx, Final=%d, dwFlags=%08x, pbData=%p, "
1867 "pdwDataLen=%p, dwBufLen=%d)\n", hProv
, hKey
, hHash
, Final
, dwFlags
, pbData
, pdwDataLen
,
1870 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
1872 SetLastError(NTE_BAD_UID
);
1878 SetLastError(NTE_BAD_FLAGS
);
1882 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
1884 SetLastError(NTE_BAD_KEY
);
1888 if (pCryptKey
->dwState
== RSAENH_KEYSTATE_IDLE
)
1889 pCryptKey
->dwState
= RSAENH_KEYSTATE_ENCRYPTING
;
1891 if (pCryptKey
->dwState
!= RSAENH_KEYSTATE_ENCRYPTING
)
1893 SetLastError(NTE_BAD_DATA
);
1897 if (is_valid_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
)) {
1898 if (!RSAENH_CPHashData(hProv
, hHash
, pbData
, *pdwDataLen
, 0)) return FALSE
;
1901 if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_BLOCK
) {
1902 if (!Final
&& (*pdwDataLen
% pCryptKey
->dwBlockLen
)) {
1903 SetLastError(NTE_BAD_DATA
);
1907 dwEncryptedLen
= (*pdwDataLen
/pCryptKey
->dwBlockLen
+(Final
?1:0))*pCryptKey
->dwBlockLen
;
1909 if (pbData
== NULL
) {
1910 *pdwDataLen
= dwEncryptedLen
;
1913 else if (dwEncryptedLen
> dwBufLen
) {
1914 *pdwDataLen
= dwEncryptedLen
;
1915 SetLastError(ERROR_MORE_DATA
);
1919 /* Pad final block with length bytes */
1920 for (i
=*pdwDataLen
; i
<dwEncryptedLen
; i
++) pbData
[i
] = dwEncryptedLen
- *pdwDataLen
;
1921 *pdwDataLen
= dwEncryptedLen
;
1923 for (i
=0, in
=pbData
; i
<*pdwDataLen
; i
+=pCryptKey
->dwBlockLen
, in
+=pCryptKey
->dwBlockLen
) {
1924 switch (pCryptKey
->dwMode
) {
1925 case CRYPT_MODE_ECB
:
1926 encrypt_block_impl(pCryptKey
->aiAlgid
, 0, &pCryptKey
->context
, in
, out
,
1930 case CRYPT_MODE_CBC
:
1931 for (j
=0; j
<pCryptKey
->dwBlockLen
; j
++) in
[j
] ^= pCryptKey
->abChainVector
[j
];
1932 encrypt_block_impl(pCryptKey
->aiAlgid
, 0, &pCryptKey
->context
, in
, out
,
1934 memcpy(pCryptKey
->abChainVector
, out
, pCryptKey
->dwBlockLen
);
1937 case CRYPT_MODE_CFB
:
1938 for (j
=0; j
<pCryptKey
->dwBlockLen
; j
++) {
1939 encrypt_block_impl(pCryptKey
->aiAlgid
, 0, &pCryptKey
->context
,
1940 pCryptKey
->abChainVector
, o
, RSAENH_ENCRYPT
);
1941 out
[j
] = in
[j
] ^ o
[0];
1942 for (k
=0; k
<pCryptKey
->dwBlockLen
-1; k
++)
1943 pCryptKey
->abChainVector
[k
] = pCryptKey
->abChainVector
[k
+1];
1944 pCryptKey
->abChainVector
[k
] = out
[j
];
1949 SetLastError(NTE_BAD_ALGID
);
1952 memcpy(in
, out
, pCryptKey
->dwBlockLen
);
1954 } else if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_STREAM
) {
1955 if (pbData
== NULL
) {
1956 *pdwDataLen
= dwBufLen
;
1959 encrypt_stream_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pbData
, *pdwDataLen
);
1960 } else if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_RSA
) {
1961 if (pCryptKey
->aiAlgid
== CALG_RSA_SIGN
) {
1962 SetLastError(NTE_BAD_KEY
);
1966 *pdwDataLen
= pCryptKey
->dwBlockLen
;
1969 if (dwBufLen
< pCryptKey
->dwBlockLen
) {
1970 SetLastError(ERROR_MORE_DATA
);
1973 if (!pad_data(pbData
, *pdwDataLen
, pbData
, pCryptKey
->dwBlockLen
, dwFlags
)) return FALSE
;
1974 encrypt_block_impl(pCryptKey
->aiAlgid
, PK_PUBLIC
, &pCryptKey
->context
, pbData
, pbData
, RSAENH_ENCRYPT
);
1975 *pdwDataLen
= pCryptKey
->dwBlockLen
;
1978 SetLastError(NTE_BAD_TYPE
);
1982 if (Final
) setup_key(pCryptKey
);
1987 /******************************************************************************
1988 * CPDecrypt (RSAENH.@)
1993 * hProv [I] The key container hKey and hHash belong to.
1994 * hKey [I] The key used to decrypt the data.
1995 * hHash [I] An optional hash object for parallel hashing. See notes.
1996 * Final [I] Indicates if this is the last block of data to decrypt.
1997 * dwFlags [I] Currently no flags defined. Must be zero.
1998 * pbData [I/O] Pointer to the data to decrypt. Plaintext will also be stored there.
1999 * pdwDataLen [I/O] I: Length of ciphertext, O: Length of plaintext.
2006 * If a hash object handle is provided in hHash, it will be updated with the plaintext.
2007 * This is useful for message signatures.
2009 * This function uses the standard WINAPI protocol for querying data of dynamic length.
2011 BOOL WINAPI
RSAENH_CPDecrypt(HCRYPTPROV hProv
, HCRYPTKEY hKey
, HCRYPTHASH hHash
, BOOL Final
,
2012 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
)
2014 CRYPTKEY
*pCryptKey
;
2015 BYTE
*in
, out
[RSAENH_MAX_BLOCK_SIZE
], o
[RSAENH_MAX_BLOCK_SIZE
];
2019 TRACE("(hProv=%08lx, hKey=%08lx, hHash=%08lx, Final=%d, dwFlags=%08x, pbData=%p, "
2020 "pdwDataLen=%p)\n", hProv
, hKey
, hHash
, Final
, dwFlags
, pbData
, pdwDataLen
);
2022 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
2024 SetLastError(NTE_BAD_UID
);
2030 SetLastError(NTE_BAD_FLAGS
);
2034 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
2036 SetLastError(NTE_BAD_KEY
);
2040 if (pCryptKey
->dwState
== RSAENH_KEYSTATE_IDLE
)
2041 pCryptKey
->dwState
= RSAENH_KEYSTATE_DECRYPTING
;
2043 if (pCryptKey
->dwState
!= RSAENH_KEYSTATE_DECRYPTING
)
2045 SetLastError(NTE_BAD_DATA
);
2051 if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_BLOCK
) {
2052 for (i
=0, in
=pbData
; i
<*pdwDataLen
; i
+=pCryptKey
->dwBlockLen
, in
+=pCryptKey
->dwBlockLen
) {
2053 switch (pCryptKey
->dwMode
) {
2054 case CRYPT_MODE_ECB
:
2055 encrypt_block_impl(pCryptKey
->aiAlgid
, 0, &pCryptKey
->context
, in
, out
,
2059 case CRYPT_MODE_CBC
:
2060 encrypt_block_impl(pCryptKey
->aiAlgid
, 0, &pCryptKey
->context
, in
, out
,
2062 for (j
=0; j
<pCryptKey
->dwBlockLen
; j
++) out
[j
] ^= pCryptKey
->abChainVector
[j
];
2063 memcpy(pCryptKey
->abChainVector
, in
, pCryptKey
->dwBlockLen
);
2066 case CRYPT_MODE_CFB
:
2067 for (j
=0; j
<pCryptKey
->dwBlockLen
; j
++) {
2068 encrypt_block_impl(pCryptKey
->aiAlgid
, 0, &pCryptKey
->context
,
2069 pCryptKey
->abChainVector
, o
, RSAENH_ENCRYPT
);
2070 out
[j
] = in
[j
] ^ o
[0];
2071 for (k
=0; k
<pCryptKey
->dwBlockLen
-1; k
++)
2072 pCryptKey
->abChainVector
[k
] = pCryptKey
->abChainVector
[k
+1];
2073 pCryptKey
->abChainVector
[k
] = in
[j
];
2078 SetLastError(NTE_BAD_ALGID
);
2081 memcpy(in
, out
, pCryptKey
->dwBlockLen
);
2084 if (pbData
[*pdwDataLen
-1] &&
2085 pbData
[*pdwDataLen
-1] <= pCryptKey
->dwBlockLen
&&
2086 pbData
[*pdwDataLen
-1] < *pdwDataLen
)
2087 *pdwDataLen
-= pbData
[*pdwDataLen
-1];
2089 SetLastError(NTE_BAD_DATA
);
2094 } else if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_STREAM
) {
2095 encrypt_stream_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pbData
, *pdwDataLen
);
2096 } else if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_RSA
) {
2097 if (pCryptKey
->aiAlgid
== CALG_RSA_SIGN
) {
2098 SetLastError(NTE_BAD_KEY
);
2101 encrypt_block_impl(pCryptKey
->aiAlgid
, PK_PRIVATE
, &pCryptKey
->context
, pbData
, pbData
, RSAENH_DECRYPT
);
2102 if (!unpad_data(pbData
, pCryptKey
->dwBlockLen
, pbData
, pdwDataLen
, dwFlags
)) return FALSE
;
2105 SetLastError(NTE_BAD_TYPE
);
2109 if (Final
) setup_key(pCryptKey
);
2111 if (is_valid_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
)) {
2112 if (*pdwDataLen
>dwMax
||
2113 !RSAENH_CPHashData(hProv
, hHash
, pbData
, *pdwDataLen
, 0)) return FALSE
;
2119 /******************************************************************************
2120 * CPExportKey (RSAENH.@)
2122 * Export a key into a binary large object (BLOB).
2125 * hProv [I] Key container from which a key is to be exported.
2126 * hKey [I] Key to be exported.
2127 * hPubKey [I] Key used to encrypt sensitive BLOB data.
2128 * dwBlobType [I] SIMPLEBLOB, PUBLICKEYBLOB or PRIVATEKEYBLOB.
2129 * dwFlags [I] Currently none defined.
2130 * pbData [O] Pointer to a buffer where the BLOB will be written to.
2131 * pdwDataLen [I/O] I: Size of buffer at pbData, O: Size of BLOB
2137 BOOL WINAPI
RSAENH_CPExportKey(HCRYPTPROV hProv
, HCRYPTKEY hKey
, HCRYPTKEY hPubKey
,
2138 DWORD dwBlobType
, DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
)
2140 CRYPTKEY
*pCryptKey
, *pPubKey
;
2141 BLOBHEADER
*pBlobHeader
= (BLOBHEADER
*)pbData
;
2142 RSAPUBKEY
*pRSAPubKey
= (RSAPUBKEY
*)(pBlobHeader
+1);
2143 ALG_ID
*pAlgid
= (ALG_ID
*)(pBlobHeader
+1);
2146 TRACE("(hProv=%08lx, hKey=%08lx, hPubKey=%08lx, dwBlobType=%08x, dwFlags=%08x, pbData=%p,"
2147 "pdwDataLen=%p)\n", hProv
, hKey
, hPubKey
, dwBlobType
, dwFlags
, pbData
, pdwDataLen
);
2149 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
2151 SetLastError(NTE_BAD_UID
);
2155 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
2157 SetLastError(NTE_BAD_KEY
);
2161 if (dwFlags
& CRYPT_SSL2_FALLBACK
) {
2162 if (pCryptKey
->aiAlgid
!= CALG_SSL2_MASTER
) {
2163 SetLastError(NTE_BAD_KEY
);
2168 switch ((BYTE
)dwBlobType
)
2171 if (!lookup_handle(&handle_table
, hPubKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pPubKey
)){
2172 SetLastError(NTE_BAD_PUBLIC_KEY
); /* FIXME: error_code? */
2176 if (!(GET_ALG_CLASS(pCryptKey
->aiAlgid
)&(ALG_CLASS_DATA_ENCRYPT
|ALG_CLASS_MSG_ENCRYPT
))) {
2177 SetLastError(NTE_BAD_KEY
); /* FIXME: error code? */
2181 dwDataLen
= sizeof(BLOBHEADER
) + sizeof(ALG_ID
) + pPubKey
->dwBlockLen
;
2183 if (*pdwDataLen
< dwDataLen
) {
2184 SetLastError(ERROR_MORE_DATA
);
2185 *pdwDataLen
= dwDataLen
;
2189 pBlobHeader
->bType
= SIMPLEBLOB
;
2190 pBlobHeader
->bVersion
= CUR_BLOB_VERSION
;
2191 pBlobHeader
->reserved
= 0;
2192 pBlobHeader
->aiKeyAlg
= pCryptKey
->aiAlgid
;
2194 *pAlgid
= pPubKey
->aiAlgid
;
2196 if (!pad_data(pCryptKey
->abKeyValue
, pCryptKey
->dwKeyLen
, (BYTE
*)(pAlgid
+1),
2197 pPubKey
->dwBlockLen
, dwFlags
))
2202 encrypt_block_impl(pPubKey
->aiAlgid
, PK_PUBLIC
, &pPubKey
->context
, (BYTE
*)(pAlgid
+1),
2203 (BYTE
*)(pAlgid
+1), RSAENH_ENCRYPT
);
2205 *pdwDataLen
= dwDataLen
;
2209 if (is_valid_handle(&handle_table
, hPubKey
, RSAENH_MAGIC_KEY
)) {
2210 SetLastError(NTE_BAD_KEY
); /* FIXME: error code? */
2214 if ((pCryptKey
->aiAlgid
!= CALG_RSA_KEYX
) && (pCryptKey
->aiAlgid
!= CALG_RSA_SIGN
)) {
2215 SetLastError(NTE_BAD_KEY
);
2219 dwDataLen
= sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) + pCryptKey
->dwKeyLen
;
2221 if (*pdwDataLen
< dwDataLen
) {
2222 SetLastError(ERROR_MORE_DATA
);
2223 *pdwDataLen
= dwDataLen
;
2227 pBlobHeader
->bType
= PUBLICKEYBLOB
;
2228 pBlobHeader
->bVersion
= CUR_BLOB_VERSION
;
2229 pBlobHeader
->reserved
= 0;
2230 pBlobHeader
->aiKeyAlg
= pCryptKey
->aiAlgid
;
2232 pRSAPubKey
->magic
= RSAENH_MAGIC_RSA1
;
2233 pRSAPubKey
->bitlen
= pCryptKey
->dwKeyLen
<< 3;
2235 export_public_key_impl((BYTE
*)(pRSAPubKey
+1), &pCryptKey
->context
,
2236 pCryptKey
->dwKeyLen
, &pRSAPubKey
->pubexp
);
2238 *pdwDataLen
= dwDataLen
;
2241 case PRIVATEKEYBLOB
:
2242 if ((pCryptKey
->aiAlgid
!= CALG_RSA_KEYX
) && (pCryptKey
->aiAlgid
!= CALG_RSA_SIGN
)) {
2243 SetLastError(NTE_BAD_KEY
);
2247 dwDataLen
= sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) +
2248 2 * pCryptKey
->dwKeyLen
+ 5 * ((pCryptKey
->dwKeyLen
+ 1) >> 1);
2250 if (*pdwDataLen
< dwDataLen
) {
2251 SetLastError(ERROR_MORE_DATA
);
2252 *pdwDataLen
= dwDataLen
;
2256 pBlobHeader
->bType
= PRIVATEKEYBLOB
;
2257 pBlobHeader
->bVersion
= CUR_BLOB_VERSION
;
2258 pBlobHeader
->reserved
= 0;
2259 pBlobHeader
->aiKeyAlg
= pCryptKey
->aiAlgid
;
2261 pRSAPubKey
->magic
= RSAENH_MAGIC_RSA2
;
2262 pRSAPubKey
->bitlen
= pCryptKey
->dwKeyLen
<< 3;
2264 export_private_key_impl((BYTE
*)(pRSAPubKey
+1), &pCryptKey
->context
,
2265 pCryptKey
->dwKeyLen
, &pRSAPubKey
->pubexp
);
2267 *pdwDataLen
= dwDataLen
;
2271 SetLastError(NTE_BAD_TYPE
); /* FIXME: error code? */
2276 /******************************************************************************
2277 * CPImportKey (RSAENH.@)
2279 * Import a BLOB'ed key into a key container.
2282 * hProv [I] Key container into which the key is to be imported.
2283 * pbData [I] Pointer to a buffer which holds the BLOB.
2284 * dwDataLen [I] Length of data in buffer at pbData.
2285 * hPubKey [I] Key used to decrypt sensitive BLOB data.
2286 * dwFlags [I] Currently none defined.
2287 * phKey [O] Handle to the imported key.
2293 BOOL WINAPI
RSAENH_CPImportKey(HCRYPTPROV hProv
, CONST BYTE
*pbData
, DWORD dwDataLen
,
2294 HCRYPTKEY hPubKey
, DWORD dwFlags
, HCRYPTKEY
*phKey
)
2296 KEYCONTAINER
*pKeyContainer
;
2297 CRYPTKEY
*pCryptKey
, *pPubKey
;
2298 CONST BLOBHEADER
*pBlobHeader
= (CONST BLOBHEADER
*)pbData
;
2299 CONST RSAPUBKEY
*pRSAPubKey
= (CONST RSAPUBKEY
*)(pBlobHeader
+1);
2300 CONST ALG_ID
*pAlgid
= (CONST ALG_ID
*)(pBlobHeader
+1);
2301 CONST BYTE
*pbKeyStream
= (CONST BYTE
*)(pAlgid
+ 1);
2307 TRACE("(hProv=%08lx, pbData=%p, dwDataLen=%d, hPubKey=%08lx, dwFlags=%08x, phKey=%p)\n",
2308 hProv
, pbData
, dwDataLen
, hPubKey
, dwFlags
, phKey
);
2310 if (!lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
,
2311 (OBJECTHDR
**)&pKeyContainer
))
2313 SetLastError(NTE_BAD_UID
);
2317 if (dwDataLen
< sizeof(BLOBHEADER
) ||
2318 pBlobHeader
->bVersion
!= CUR_BLOB_VERSION
||
2319 pBlobHeader
->reserved
!= 0)
2321 SetLastError(NTE_BAD_DATA
);
2325 switch (pBlobHeader
->bType
)
2327 case PRIVATEKEYBLOB
:
2328 if ((dwDataLen
< sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
)) ||
2329 (pRSAPubKey
->magic
!= RSAENH_MAGIC_RSA2
) ||
2330 (dwDataLen
< sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) +
2331 (2 * pRSAPubKey
->bitlen
>> 3) + (5 * ((pRSAPubKey
->bitlen
+8)>>4))))
2333 SetLastError(NTE_BAD_DATA
);
2337 *phKey
= new_key(hProv
, pBlobHeader
->aiKeyAlg
, MAKELONG(0,pRSAPubKey
->bitlen
), &pCryptKey
);
2338 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
2339 setup_key(pCryptKey
);
2340 ret
= import_private_key_impl((CONST BYTE
*)(pRSAPubKey
+1), &pCryptKey
->context
,
2341 pRSAPubKey
->bitlen
/8, pRSAPubKey
->pubexp
);
2343 switch (pBlobHeader
->aiKeyAlg
)
2347 TRACE("installing signing key\n");
2348 RSAENH_CPDestroyKey(hProv
, pKeyContainer
->hSignatureKeyPair
);
2349 copy_handle(&handle_table
, *phKey
, RSAENH_MAGIC_KEY
,
2350 &pKeyContainer
->hSignatureKeyPair
);
2352 case AT_KEYEXCHANGE
:
2354 TRACE("installing key exchange key\n");
2355 RSAENH_CPDestroyKey(hProv
, pKeyContainer
->hKeyExchangeKeyPair
);
2356 copy_handle(&handle_table
, *phKey
, RSAENH_MAGIC_KEY
,
2357 &pKeyContainer
->hKeyExchangeKeyPair
);
2364 if ((dwDataLen
< sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
)) ||
2365 (pRSAPubKey
->magic
!= RSAENH_MAGIC_RSA1
) ||
2366 (dwDataLen
< sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) + (pRSAPubKey
->bitlen
>> 3)))
2368 SetLastError(NTE_BAD_DATA
);
2372 /* Since this is a public key blob, only the public key is
2373 * available, so only signature verification is possible.
2375 algID
= pBlobHeader
->aiKeyAlg
;
2376 *phKey
= new_key(hProv
, algID
, MAKELONG(0,pRSAPubKey
->bitlen
), &pCryptKey
);
2377 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
2378 setup_key(pCryptKey
);
2379 ret
= import_public_key_impl((CONST BYTE
*)(pRSAPubKey
+1), &pCryptKey
->context
,
2380 pRSAPubKey
->bitlen
>> 3, pRSAPubKey
->pubexp
);
2382 switch (pBlobHeader
->aiKeyAlg
)
2384 case AT_KEYEXCHANGE
:
2386 TRACE("installing public key\n");
2387 RSAENH_CPDestroyKey(hProv
, pKeyContainer
->hKeyExchangeKeyPair
);
2388 copy_handle(&handle_table
, *phKey
, RSAENH_MAGIC_KEY
,
2389 &pKeyContainer
->hKeyExchangeKeyPair
);
2396 if (!lookup_handle(&handle_table
, hPubKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pPubKey
) ||
2397 pPubKey
->aiAlgid
!= CALG_RSA_KEYX
)
2399 SetLastError(NTE_BAD_PUBLIC_KEY
); /* FIXME: error code? */
2403 if (dwDataLen
< sizeof(BLOBHEADER
)+sizeof(ALG_ID
)+pPubKey
->dwBlockLen
)
2405 SetLastError(NTE_BAD_DATA
); /* FIXME: error code */
2409 pbDecrypted
= HeapAlloc(GetProcessHeap(), 0, pPubKey
->dwBlockLen
);
2410 if (!pbDecrypted
) return FALSE
;
2411 encrypt_block_impl(pPubKey
->aiAlgid
, PK_PRIVATE
, &pPubKey
->context
, pbKeyStream
, pbDecrypted
,
2414 dwKeyLen
= RSAENH_MAX_KEY_SIZE
;
2415 if (!unpad_data(pbDecrypted
, pPubKey
->dwBlockLen
, pbDecrypted
, &dwKeyLen
, dwFlags
)) {
2416 HeapFree(GetProcessHeap(), 0, pbDecrypted
);
2420 *phKey
= new_key(hProv
, pBlobHeader
->aiKeyAlg
, dwKeyLen
<<19, &pCryptKey
);
2421 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
2423 HeapFree(GetProcessHeap(), 0, pbDecrypted
);
2426 memcpy(pCryptKey
->abKeyValue
, pbDecrypted
, dwKeyLen
);
2427 HeapFree(GetProcessHeap(), 0, pbDecrypted
);
2428 setup_key(pCryptKey
);
2432 SetLastError(NTE_BAD_TYPE
); /* FIXME: error code? */
2437 /******************************************************************************
2438 * CPGenKey (RSAENH.@)
2440 * Generate a key in the key container
2443 * hProv [I] Key container for which a key is to be generated.
2444 * Algid [I] Crypto algorithm identifier for the key to be generated.
2445 * dwFlags [I] Upper 16 bits: Binary length of key. Lower 16 bits: Flags. See Notes
2446 * phKey [O] Handle to the generated key.
2453 * Flags currently not considered.
2456 * Private key-exchange- and signature-keys can be generated with Algid AT_KEYEXCHANGE
2457 * and AT_SIGNATURE values.
2459 BOOL WINAPI
RSAENH_CPGenKey(HCRYPTPROV hProv
, ALG_ID Algid
, DWORD dwFlags
, HCRYPTKEY
*phKey
)
2461 KEYCONTAINER
*pKeyContainer
;
2462 CRYPTKEY
*pCryptKey
;
2464 TRACE("(hProv=%08lx, aiAlgid=%d, dwFlags=%08x, phKey=%p)\n", hProv
, Algid
, dwFlags
, phKey
);
2466 if (!lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
,
2467 (OBJECTHDR
**)&pKeyContainer
))
2469 /* MSDN: hProv not containing valid context handle */
2470 SetLastError(NTE_BAD_UID
);
2478 *phKey
= new_key(hProv
, CALG_RSA_SIGN
, dwFlags
, &pCryptKey
);
2480 new_key_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pCryptKey
->dwKeyLen
);
2481 setup_key(pCryptKey
);
2482 if (Algid
== AT_SIGNATURE
) {
2483 RSAENH_CPDestroyKey(hProv
, pKeyContainer
->hSignatureKeyPair
);
2484 copy_handle(&handle_table
, *phKey
, RSAENH_MAGIC_KEY
,
2485 &pKeyContainer
->hSignatureKeyPair
);
2490 case AT_KEYEXCHANGE
:
2492 *phKey
= new_key(hProv
, CALG_RSA_KEYX
, dwFlags
, &pCryptKey
);
2494 new_key_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pCryptKey
->dwKeyLen
);
2495 setup_key(pCryptKey
);
2496 if (Algid
== AT_KEYEXCHANGE
) {
2497 RSAENH_CPDestroyKey(hProv
, pKeyContainer
->hKeyExchangeKeyPair
);
2498 copy_handle(&handle_table
, *phKey
, RSAENH_MAGIC_KEY
,
2499 &pKeyContainer
->hKeyExchangeKeyPair
);
2509 case CALG_PCT1_MASTER
:
2510 case CALG_SSL2_MASTER
:
2511 case CALG_SSL3_MASTER
:
2512 case CALG_TLS1_MASTER
:
2513 *phKey
= new_key(hProv
, Algid
, dwFlags
, &pCryptKey
);
2515 gen_rand_impl(pCryptKey
->abKeyValue
, RSAENH_MAX_KEY_SIZE
);
2517 case CALG_SSL3_MASTER
:
2518 pCryptKey
->abKeyValue
[0] = RSAENH_SSL3_VERSION_MAJOR
;
2519 pCryptKey
->abKeyValue
[1] = RSAENH_SSL3_VERSION_MINOR
;
2522 case CALG_TLS1_MASTER
:
2523 pCryptKey
->abKeyValue
[0] = RSAENH_TLS1_VERSION_MAJOR
;
2524 pCryptKey
->abKeyValue
[1] = RSAENH_TLS1_VERSION_MINOR
;
2527 setup_key(pCryptKey
);
2532 /* MSDN: Algorithm not supported specified by Algid */
2533 SetLastError(NTE_BAD_ALGID
);
2537 return *phKey
!= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
2540 /******************************************************************************
2541 * CPGenRandom (RSAENH.@)
2543 * Generate a random byte stream.
2546 * hProv [I] Key container that is used to generate random bytes.
2547 * dwLen [I] Specifies the number of requested random data bytes.
2548 * pbBuffer [O] Random bytes will be stored here.
2554 BOOL WINAPI
RSAENH_CPGenRandom(HCRYPTPROV hProv
, DWORD dwLen
, BYTE
*pbBuffer
)
2556 TRACE("(hProv=%08lx, dwLen=%d, pbBuffer=%p)\n", hProv
, dwLen
, pbBuffer
);
2558 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
2560 /* MSDN: hProv not containing valid context handle */
2561 SetLastError(NTE_BAD_UID
);
2565 return gen_rand_impl(pbBuffer
, dwLen
);
2568 /******************************************************************************
2569 * CPGetHashParam (RSAENH.@)
2571 * Query parameters of an hash object.
2574 * hProv [I] The kea container, which the hash belongs to.
2575 * hHash [I] The hash object that is to be queried.
2576 * dwParam [I] Specifies the parameter that is to be queried.
2577 * pbData [I] Pointer to the buffer where the parameter value will be stored.
2578 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
2579 * dwFlags [I] None currently defined.
2586 * Valid dwParams are: HP_ALGID, HP_HASHSIZE, HP_HASHVALUE. The hash will be
2587 * finalized if HP_HASHVALUE is queried.
2589 BOOL WINAPI
RSAENH_CPGetHashParam(HCRYPTPROV hProv
, HCRYPTHASH hHash
, DWORD dwParam
, BYTE
*pbData
,
2590 DWORD
*pdwDataLen
, DWORD dwFlags
)
2592 CRYPTHASH
*pCryptHash
;
2594 TRACE("(hProv=%08lx, hHash=%08lx, dwParam=%08x, pbData=%p, pdwDataLen=%p, dwFlags=%08x)\n",
2595 hProv
, hHash
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
2597 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
2599 SetLastError(NTE_BAD_UID
);
2605 SetLastError(NTE_BAD_FLAGS
);
2609 if (!lookup_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
,
2610 (OBJECTHDR
**)&pCryptHash
))
2612 SetLastError(NTE_BAD_HASH
);
2618 SetLastError(ERROR_INVALID_PARAMETER
);
2625 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&pCryptHash
->aiAlgid
,
2629 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&pCryptHash
->dwHashSize
,
2633 if (pCryptHash
->aiAlgid
== CALG_TLS1PRF
) {
2634 return tls1_prf(hProv
, pCryptHash
->hKey
, &pCryptHash
->tpPRFParams
.blobLabel
,
2635 &pCryptHash
->tpPRFParams
.blobSeed
, pbData
, *pdwDataLen
);
2638 if ( pbData
== NULL
) {
2639 *pdwDataLen
= pCryptHash
->dwHashSize
;
2643 if (pbData
&& (pCryptHash
->dwState
!= RSAENH_HASHSTATE_FINISHED
))
2645 finalize_hash(pCryptHash
);
2646 pCryptHash
->dwState
= RSAENH_HASHSTATE_FINISHED
;
2649 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)pCryptHash
->abHashValue
,
2650 pCryptHash
->dwHashSize
);
2653 SetLastError(NTE_BAD_TYPE
);
2658 /******************************************************************************
2659 * CPSetKeyParam (RSAENH.@)
2661 * Set a parameter of a key object
2664 * hProv [I] The key container to which the key belongs.
2665 * hKey [I] The key for which a parameter is to be set.
2666 * dwParam [I] Parameter type. See Notes.
2667 * pbData [I] Pointer to the parameter value.
2668 * dwFlags [I] Currently none defined.
2675 * Defined dwParam types are:
2676 * - KP_MODE: Values MODE_CBC, MODE_ECB, MODE_CFB.
2677 * - KP_MODE_BITS: Shift width for cipher feedback mode. (Currently ignored by MS CSP's)
2678 * - KP_PERMISSIONS: Or'ed combination of CRYPT_ENCRYPT, CRYPT_DECRYPT,
2679 * CRYPT_EXPORT, CRYPT_READ, CRYPT_WRITE, CRYPT_MAC
2680 * - KP_IV: Initialization vector
2682 BOOL WINAPI
RSAENH_CPSetKeyParam(HCRYPTPROV hProv
, HCRYPTKEY hKey
, DWORD dwParam
, BYTE
*pbData
,
2685 CRYPTKEY
*pCryptKey
;
2687 TRACE("(hProv=%08lx, hKey=%08lx, dwParam=%08x, pbData=%p, dwFlags=%08x)\n", hProv
, hKey
,
2688 dwParam
, pbData
, dwFlags
);
2690 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
2692 SetLastError(NTE_BAD_UID
);
2697 SetLastError(NTE_BAD_FLAGS
);
2701 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
2703 SetLastError(NTE_BAD_KEY
);
2709 pCryptKey
->dwMode
= *(DWORD
*)pbData
;
2713 pCryptKey
->dwModeBits
= *(DWORD
*)pbData
;
2716 case KP_PERMISSIONS
:
2717 pCryptKey
->dwPermissions
= *(DWORD
*)pbData
;
2721 memcpy(pCryptKey
->abInitVector
, pbData
, pCryptKey
->dwBlockLen
);
2722 setup_key(pCryptKey
);
2725 case KP_EFFECTIVE_KEYLEN
:
2726 switch (pCryptKey
->aiAlgid
) {
2730 SetLastError(ERROR_INVALID_PARAMETER
);
2733 else if (!*(DWORD
*)pbData
|| *(DWORD
*)pbData
> 1024)
2735 SetLastError(NTE_BAD_DATA
);
2740 pCryptKey
->dwEffectiveKeyLen
= *(DWORD
*)pbData
;
2741 setup_key(pCryptKey
);
2745 SetLastError(NTE_BAD_TYPE
);
2750 case KP_SCHANNEL_ALG
:
2751 switch (((PSCHANNEL_ALG
)pbData
)->dwUse
) {
2752 case SCHANNEL_ENC_KEY
:
2753 memcpy(&pCryptKey
->siSChannelInfo
.saEncAlg
, pbData
, sizeof(SCHANNEL_ALG
));
2756 case SCHANNEL_MAC_KEY
:
2757 memcpy(&pCryptKey
->siSChannelInfo
.saMACAlg
, pbData
, sizeof(SCHANNEL_ALG
));
2761 SetLastError(NTE_FAIL
); /* FIXME: error code */
2766 case KP_CLIENT_RANDOM
:
2767 return copy_data_blob(&pCryptKey
->siSChannelInfo
.blobClientRandom
, (PCRYPT_DATA_BLOB
)pbData
);
2769 case KP_SERVER_RANDOM
:
2770 return copy_data_blob(&pCryptKey
->siSChannelInfo
.blobServerRandom
, (PCRYPT_DATA_BLOB
)pbData
);
2773 SetLastError(NTE_BAD_TYPE
);
2778 /******************************************************************************
2779 * CPGetKeyParam (RSAENH.@)
2781 * Query a key parameter.
2784 * hProv [I] The key container, which the key belongs to.
2785 * hHash [I] The key object that is to be queried.
2786 * dwParam [I] Specifies the parameter that is to be queried.
2787 * pbData [I] Pointer to the buffer where the parameter value will be stored.
2788 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
2789 * dwFlags [I] None currently defined.
2796 * Defined dwParam types are:
2797 * - KP_MODE: Values MODE_CBC, MODE_ECB, MODE_CFB.
2798 * - KP_MODE_BITS: Shift width for cipher feedback mode.
2799 * (Currently ignored by MS CSP's - always eight)
2800 * - KP_PERMISSIONS: Or'ed combination of CRYPT_ENCRYPT, CRYPT_DECRYPT,
2801 * CRYPT_EXPORT, CRYPT_READ, CRYPT_WRITE, CRYPT_MAC
2802 * - KP_IV: Initialization vector.
2803 * - KP_KEYLEN: Bitwidth of the key.
2804 * - KP_BLOCKLEN: Size of a block cipher block.
2805 * - KP_SALT: Salt value.
2807 BOOL WINAPI
RSAENH_CPGetKeyParam(HCRYPTPROV hProv
, HCRYPTKEY hKey
, DWORD dwParam
, BYTE
*pbData
,
2808 DWORD
*pdwDataLen
, DWORD dwFlags
)
2810 CRYPTKEY
*pCryptKey
;
2813 TRACE("(hProv=%08lx, hKey=%08lx, dwParam=%08x, pbData=%p, pdwDataLen=%p dwFlags=%08x)\n",
2814 hProv
, hKey
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
2816 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
2818 SetLastError(NTE_BAD_UID
);
2823 SetLastError(NTE_BAD_FLAGS
);
2827 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
2829 SetLastError(NTE_BAD_KEY
);
2836 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)pCryptKey
->abInitVector
,
2837 pCryptKey
->dwBlockLen
);
2840 return copy_param(pbData
, pdwDataLen
,
2841 (CONST BYTE
*)&pCryptKey
->abKeyValue
[pCryptKey
->dwKeyLen
], pCryptKey
->dwSaltLen
);
2844 dwBitLen
= pCryptKey
->dwKeyLen
<< 3;
2845 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwBitLen
, sizeof(DWORD
));
2847 case KP_EFFECTIVE_KEYLEN
:
2848 if (pCryptKey
->dwEffectiveKeyLen
)
2849 dwBitLen
= pCryptKey
->dwEffectiveKeyLen
;
2851 dwBitLen
= pCryptKey
->dwKeyLen
<< 3;
2852 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwBitLen
, sizeof(DWORD
));
2855 dwBitLen
= pCryptKey
->dwBlockLen
<< 3;
2856 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwBitLen
, sizeof(DWORD
));
2859 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&pCryptKey
->dwMode
, sizeof(DWORD
));
2862 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&pCryptKey
->dwModeBits
,
2865 case KP_PERMISSIONS
:
2866 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&pCryptKey
->dwPermissions
,
2870 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&pCryptKey
->aiAlgid
, sizeof(DWORD
));
2873 SetLastError(NTE_BAD_TYPE
);
2878 /******************************************************************************
2879 * CPGetProvParam (RSAENH.@)
2881 * Query a CSP parameter.
2884 * hProv [I] The key container that is to be queried.
2885 * dwParam [I] Specifies the parameter that is to be queried.
2886 * pbData [I] Pointer to the buffer where the parameter value will be stored.
2887 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
2888 * dwFlags [I] CRYPT_FIRST: Start enumeration (for PP_ENUMALGS{_EX}).
2894 * Defined dwParam types:
2895 * - PP_CONTAINER: Name of the key container.
2896 * - PP_NAME: Name of the cryptographic service provider.
2897 * - PP_SIG_KEYSIZE_INC: RSA signature keywidth granularity in bits.
2898 * - PP_KEYX_KEYSIZE_INC: RSA key-exchange keywidth granularity in bits.
2899 * - PP_ENUMALGS{_EX}: Query provider capabilities.
2901 BOOL WINAPI
RSAENH_CPGetProvParam(HCRYPTPROV hProv
, DWORD dwParam
, BYTE
*pbData
,
2902 DWORD
*pdwDataLen
, DWORD dwFlags
)
2904 KEYCONTAINER
*pKeyContainer
;
2905 PROV_ENUMALGS provEnumalgs
;
2907 CHAR szRSABase
[MAX_PATH
];
2908 HKEY hKey
, hRootKey
;
2910 /* This is for dwParam 41, which does not seem to be documented
2911 * on MSDN. IE6 SP1 asks for it in the 'About' dialog, however.
2912 * Returning this BLOB seems to satisfy IE. The marked 0x00 seem
2913 * to be 'don't care's. If you know anything more specific about
2914 * provider parameter 41, please report to wine-devel@winehq.org */
2915 static CONST BYTE abWTF
[96] = {
2916 0xb0, 0x25, 0x63, 0x86, 0x9c, 0xab, 0xb6, 0x37,
2917 0xe8, 0x82, /**/0x00,/**/ 0x72, 0x06, 0xb2, /**/0x00,/**/ 0x3b,
2918 0x60, 0x35, /**/0x00,/**/ 0x3b, 0x88, 0xce, /**/0x00,/**/ 0x82,
2919 0xbc, 0x7a, /**/0x00,/**/ 0xb7, 0x4f, 0x7e, /**/0x00,/**/ 0xde,
2920 0x92, 0xf1, /**/0x00,/**/ 0x83, 0xea, 0x5e, /**/0x00,/**/ 0xc8,
2921 0x12, 0x1e, 0xd4, 0x06, 0xf7, 0x66, /**/0x00,/**/ 0x01,
2922 0x29, 0xa4, /**/0x00,/**/ 0xf8, 0x24, 0x0c, /**/0x00,/**/ 0x33,
2923 0x06, 0x80, /**/0x00,/**/ 0x02, 0x46, 0x0b, /**/0x00,/**/ 0x6d,
2924 0x5b, 0xca, /**/0x00,/**/ 0x9a, 0x10, 0xf0, /**/0x00,/**/ 0x05,
2925 0x19, 0xd0, /**/0x00,/**/ 0x2c, 0xf6, 0x27, /**/0x00,/**/ 0xaa,
2926 0x7c, 0x6f, /**/0x00,/**/ 0xb9, 0xd8, 0x72, /**/0x00,/**/ 0x03,
2927 0xf3, 0x81, /**/0x00,/**/ 0xfa, 0xe8, 0x26, /**/0x00,/**/ 0xca
2930 TRACE("(hProv=%08lx, dwParam=%08x, pbData=%p, pdwDataLen=%p, dwFlags=%08x)\n",
2931 hProv
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
2934 SetLastError(ERROR_INVALID_PARAMETER
);
2938 if (!lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
,
2939 (OBJECTHDR
**)&pKeyContainer
))
2941 /* MSDN: hProv not containing valid context handle */
2942 SetLastError(NTE_BAD_UID
);
2949 case PP_UNIQUE_CONTAINER
:/* MSDN says we can return the same value as PP_CONTAINER */
2950 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)pKeyContainer
->szName
,
2951 strlen(pKeyContainer
->szName
)+1);
2954 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)pKeyContainer
->szProvName
,
2955 strlen(pKeyContainer
->szProvName
)+1);
2958 dwTemp
= PROV_RSA_FULL
;
2959 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwTemp
, sizeof(dwTemp
));
2962 dwTemp
= AT_SIGNATURE
| AT_KEYEXCHANGE
;
2963 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwTemp
, sizeof(dwTemp
));
2965 case PP_KEYSET_TYPE
:
2966 dwTemp
= pKeyContainer
->dwFlags
& CRYPT_MACHINE_KEYSET
;
2967 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwTemp
, sizeof(dwTemp
));
2970 dwTemp
= CRYPT_SEC_DESCR
;
2971 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwTemp
, sizeof(dwTemp
));
2973 case PP_SIG_KEYSIZE_INC
:
2974 case PP_KEYX_KEYSIZE_INC
:
2976 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwTemp
, sizeof(dwTemp
));
2979 dwTemp
= CRYPT_IMPL_SOFTWARE
;
2980 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwTemp
, sizeof(dwTemp
));
2983 dwTemp
= 0x00000200;
2984 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwTemp
, sizeof(dwTemp
));
2986 case PP_ENUMCONTAINERS
:
2987 if ((dwFlags
& CRYPT_FIRST
) == CRYPT_FIRST
) pKeyContainer
->dwEnumContainersCtr
= 0;
2990 *pdwDataLen
= (DWORD
)MAX_PATH
+ 1;
2994 sprintf(szRSABase
, RSAENH_REGKEY
, "");
2996 if (dwFlags
& CRYPT_MACHINE_KEYSET
) {
2997 hRootKey
= HKEY_LOCAL_MACHINE
;
2999 hRootKey
= HKEY_CURRENT_USER
;
3002 if (RegOpenKeyExA(hRootKey
, szRSABase
, 0, KEY_READ
, &hKey
) != ERROR_SUCCESS
)
3004 SetLastError(ERROR_NO_MORE_ITEMS
);
3008 dwTemp
= *pdwDataLen
;
3009 switch (RegEnumKeyExA(hKey
, pKeyContainer
->dwEnumContainersCtr
, (LPSTR
)pbData
, &dwTemp
,
3010 NULL
, NULL
, NULL
, NULL
))
3012 case ERROR_MORE_DATA
:
3013 *pdwDataLen
= (DWORD
)MAX_PATH
+ 1;
3016 pKeyContainer
->dwEnumContainersCtr
++;
3020 case ERROR_NO_MORE_ITEMS
:
3022 SetLastError(ERROR_NO_MORE_ITEMS
);
3028 case PP_ENUMALGS_EX
:
3029 if (((pKeyContainer
->dwEnumAlgsCtr
>= RSAENH_MAX_ENUMALGS
-1) ||
3030 (!aProvEnumAlgsEx
[pKeyContainer
->dwPersonality
]
3031 [pKeyContainer
->dwEnumAlgsCtr
+1].aiAlgid
)) &&
3032 ((dwFlags
& CRYPT_FIRST
) != CRYPT_FIRST
))
3034 SetLastError(ERROR_NO_MORE_ITEMS
);
3038 if (dwParam
== PP_ENUMALGS
) {
3039 if (pbData
&& (*pdwDataLen
>= sizeof(PROV_ENUMALGS
)))
3040 pKeyContainer
->dwEnumAlgsCtr
= ((dwFlags
& CRYPT_FIRST
) == CRYPT_FIRST
) ?
3041 0 : pKeyContainer
->dwEnumAlgsCtr
+1;
3043 provEnumalgs
.aiAlgid
= aProvEnumAlgsEx
3044 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
].aiAlgid
;
3045 provEnumalgs
.dwBitLen
= aProvEnumAlgsEx
3046 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
].dwDefaultLen
;
3047 provEnumalgs
.dwNameLen
= aProvEnumAlgsEx
3048 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
].dwNameLen
;
3049 memcpy(provEnumalgs
.szName
, aProvEnumAlgsEx
3050 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
].szName
,
3053 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&provEnumalgs
,
3054 sizeof(PROV_ENUMALGS
));
3056 if (pbData
&& (*pdwDataLen
>= sizeof(PROV_ENUMALGS_EX
)))
3057 pKeyContainer
->dwEnumAlgsCtr
= ((dwFlags
& CRYPT_FIRST
) == CRYPT_FIRST
) ?
3058 0 : pKeyContainer
->dwEnumAlgsCtr
+1;
3060 return copy_param(pbData
, pdwDataLen
,
3061 (CONST BYTE
*)&aProvEnumAlgsEx
3062 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
],
3063 sizeof(PROV_ENUMALGS_EX
));
3066 case 41: /* Undocumented. Asked for by IE About dialog */
3067 return copy_param(pbData
, pdwDataLen
, abWTF
, sizeof(abWTF
));
3070 /* MSDN: Unknown parameter number in dwParam */
3071 SetLastError(NTE_BAD_TYPE
);
3076 /******************************************************************************
3077 * CPDeriveKey (RSAENH.@)
3079 * Derives a key from a hash value.
3082 * hProv [I] Key container for which a key is to be generated.
3083 * Algid [I] Crypto algorithm identifier for the key to be generated.
3084 * hBaseData [I] Hash from whose value the key will be derived.
3085 * dwFlags [I] See Notes.
3086 * phKey [O] The generated key.
3094 * - CRYPT_EXPORTABLE: Key can be exported.
3095 * - CRYPT_NO_SALT: No salt is used for 40 bit keys.
3096 * - CRYPT_CREATE_SALT: Use remaining bits as salt value.
3098 BOOL WINAPI
RSAENH_CPDeriveKey(HCRYPTPROV hProv
, ALG_ID Algid
, HCRYPTHASH hBaseData
,
3099 DWORD dwFlags
, HCRYPTKEY
*phKey
)
3101 CRYPTKEY
*pCryptKey
, *pMasterKey
;
3102 CRYPTHASH
*pCryptHash
;
3103 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
*2];
3106 TRACE("(hProv=%08lx, Algid=%d, hBaseData=%08lx, dwFlags=%08x phKey=%p)\n", hProv
, Algid
,
3107 hBaseData
, dwFlags
, phKey
);
3109 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
3111 SetLastError(NTE_BAD_UID
);
3115 if (!lookup_handle(&handle_table
, hBaseData
, RSAENH_MAGIC_HASH
,
3116 (OBJECTHDR
**)&pCryptHash
))
3118 SetLastError(NTE_BAD_HASH
);
3124 SetLastError(ERROR_INVALID_PARAMETER
);
3128 switch (GET_ALG_CLASS(Algid
))
3130 case ALG_CLASS_DATA_ENCRYPT
:
3131 *phKey
= new_key(hProv
, Algid
, dwFlags
, &pCryptKey
);
3132 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
3135 * We derive the key material from the hash.
3136 * If the hash value is not large enough for the claimed key, we have to construct
3137 * a larger binary value based on the hash. This is documented in MSDN: CryptDeriveKey.
3139 dwLen
= RSAENH_MAX_HASH_SIZE
;
3140 RSAENH_CPGetHashParam(pCryptHash
->hProv
, hBaseData
, HP_HASHVAL
, abHashValue
, &dwLen
, 0);
3142 if (dwLen
< pCryptKey
->dwKeyLen
) {
3143 BYTE pad1
[RSAENH_HMAC_DEF_PAD_LEN
], pad2
[RSAENH_HMAC_DEF_PAD_LEN
];
3144 BYTE old_hashval
[RSAENH_MAX_HASH_SIZE
];
3147 memcpy(old_hashval
, pCryptHash
->abHashValue
, RSAENH_MAX_HASH_SIZE
);
3149 for (i
=0; i
<RSAENH_HMAC_DEF_PAD_LEN
; i
++) {
3150 pad1
[i
] = RSAENH_HMAC_DEF_IPAD_CHAR
^ (i
<dwLen
? abHashValue
[i
] : 0);
3151 pad2
[i
] = RSAENH_HMAC_DEF_OPAD_CHAR
^ (i
<dwLen
? abHashValue
[i
] : 0);
3154 init_hash(pCryptHash
);
3155 update_hash(pCryptHash
, pad1
, RSAENH_HMAC_DEF_PAD_LEN
);
3156 finalize_hash(pCryptHash
);
3157 memcpy(abHashValue
, pCryptHash
->abHashValue
, pCryptHash
->dwHashSize
);
3159 init_hash(pCryptHash
);
3160 update_hash(pCryptHash
, pad2
, RSAENH_HMAC_DEF_PAD_LEN
);
3161 finalize_hash(pCryptHash
);
3162 memcpy(abHashValue
+pCryptHash
->dwHashSize
, pCryptHash
->abHashValue
,
3163 pCryptHash
->dwHashSize
);
3165 memcpy(pCryptHash
->abHashValue
, old_hashval
, RSAENH_MAX_HASH_SIZE
);
3168 memcpy(pCryptKey
->abKeyValue
, abHashValue
,
3169 RSAENH_MIN(pCryptKey
->dwKeyLen
, sizeof(pCryptKey
->abKeyValue
)));
3172 case ALG_CLASS_MSG_ENCRYPT
:
3173 if (!lookup_handle(&handle_table
, pCryptHash
->hKey
, RSAENH_MAGIC_KEY
,
3174 (OBJECTHDR
**)&pMasterKey
))
3176 SetLastError(NTE_FAIL
); /* FIXME error code */
3182 /* See RFC 2246, chapter 6.3 Key calculation */
3183 case CALG_SCHANNEL_ENC_KEY
:
3184 *phKey
= new_key(hProv
, pMasterKey
->siSChannelInfo
.saEncAlg
.Algid
,
3185 MAKELONG(LOWORD(dwFlags
),pMasterKey
->siSChannelInfo
.saEncAlg
.cBits
),
3187 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
3188 memcpy(pCryptKey
->abKeyValue
,
3189 pCryptHash
->abHashValue
+ (
3190 2 * (pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
/ 8) +
3191 ((dwFlags
& CRYPT_SERVER
) ?
3192 (pMasterKey
->siSChannelInfo
.saEncAlg
.cBits
/ 8) : 0)),
3193 pMasterKey
->siSChannelInfo
.saEncAlg
.cBits
/ 8);
3194 memcpy(pCryptKey
->abInitVector
,
3195 pCryptHash
->abHashValue
+ (
3196 2 * (pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
/ 8) +
3197 2 * (pMasterKey
->siSChannelInfo
.saEncAlg
.cBits
/ 8) +
3198 ((dwFlags
& CRYPT_SERVER
) ? pCryptKey
->dwBlockLen
: 0)),
3199 pCryptKey
->dwBlockLen
);
3202 case CALG_SCHANNEL_MAC_KEY
:
3203 *phKey
= new_key(hProv
, Algid
,
3204 MAKELONG(LOWORD(dwFlags
),pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
),
3206 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
3207 memcpy(pCryptKey
->abKeyValue
,
3208 pCryptHash
->abHashValue
+ ((dwFlags
& CRYPT_SERVER
) ?
3209 pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
/ 8 : 0),
3210 pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
/ 8);
3214 SetLastError(NTE_BAD_ALGID
);
3220 SetLastError(NTE_BAD_ALGID
);
3224 setup_key(pCryptKey
);
3228 /******************************************************************************
3229 * CPGetUserKey (RSAENH.@)
3231 * Returns a handle to the user's private key-exchange- or signature-key.
3234 * hProv [I] The key container from which a user key is requested.
3235 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
3236 * phUserKey [O] Handle to the requested key or INVALID_HANDLE_VALUE in case of failure.
3243 * A newly created key container does not contain private user key. Create them with CPGenKey.
3245 BOOL WINAPI
RSAENH_CPGetUserKey(HCRYPTPROV hProv
, DWORD dwKeySpec
, HCRYPTKEY
*phUserKey
)
3247 KEYCONTAINER
*pKeyContainer
;
3249 TRACE("(hProv=%08lx, dwKeySpec=%08x, phUserKey=%p)\n", hProv
, dwKeySpec
, phUserKey
);
3251 if (!lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
,
3252 (OBJECTHDR
**)&pKeyContainer
))
3254 /* MSDN: hProv not containing valid context handle */
3255 SetLastError(NTE_BAD_UID
);
3261 case AT_KEYEXCHANGE
:
3262 copy_handle(&handle_table
, pKeyContainer
->hKeyExchangeKeyPair
, RSAENH_MAGIC_KEY
,
3267 copy_handle(&handle_table
, pKeyContainer
->hSignatureKeyPair
, RSAENH_MAGIC_KEY
,
3272 *phUserKey
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
3275 if (*phUserKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
3277 /* MSDN: dwKeySpec parameter specifies nonexistent key */
3278 SetLastError(NTE_NO_KEY
);
3285 /******************************************************************************
3286 * CPHashData (RSAENH.@)
3288 * Updates a hash object with the given data.
3291 * hProv [I] Key container to which the hash object belongs.
3292 * hHash [I] Hash object which is to be updated.
3293 * pbData [I] Pointer to data with which the hash object is to be updated.
3294 * dwDataLen [I] Length of the data.
3295 * dwFlags [I] Currently none defined.
3302 * The actual hash value is queried with CPGetHashParam, which will finalize
3303 * the hash. Updating a finalized hash will fail with a last error NTE_BAD_HASH_STATE.
3305 BOOL WINAPI
RSAENH_CPHashData(HCRYPTPROV hProv
, HCRYPTHASH hHash
, CONST BYTE
*pbData
,
3306 DWORD dwDataLen
, DWORD dwFlags
)
3308 CRYPTHASH
*pCryptHash
;
3310 TRACE("(hProv=%08lx, hHash=%08lx, pbData=%p, dwDataLen=%d, dwFlags=%08x)\n",
3311 hProv
, hHash
, pbData
, dwDataLen
, dwFlags
);
3315 SetLastError(NTE_BAD_FLAGS
);
3319 if (!lookup_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
,
3320 (OBJECTHDR
**)&pCryptHash
))
3322 SetLastError(NTE_BAD_HASH
);
3326 if (!get_algid_info(hProv
, pCryptHash
->aiAlgid
) || pCryptHash
->aiAlgid
== CALG_SSL3_SHAMD5
)
3328 SetLastError(NTE_BAD_ALGID
);
3332 if (pCryptHash
->dwState
!= RSAENH_HASHSTATE_HASHING
)
3334 SetLastError(NTE_BAD_HASH_STATE
);
3338 update_hash(pCryptHash
, pbData
, dwDataLen
);
3342 /******************************************************************************
3343 * CPHashSessionKey (RSAENH.@)
3345 * Updates a hash object with the binary representation of a symmetric key.
3348 * hProv [I] Key container to which the hash object belongs.
3349 * hHash [I] Hash object which is to be updated.
3350 * hKey [I] The symmetric key, whose binary value will be added to the hash.
3351 * dwFlags [I] CRYPT_LITTLE_ENDIAN, if the binary key value shall be interpreted as little endian.
3357 BOOL WINAPI
RSAENH_CPHashSessionKey(HCRYPTPROV hProv
, HCRYPTHASH hHash
, HCRYPTKEY hKey
,
3360 BYTE abKeyValue
[RSAENH_MAX_KEY_SIZE
], bTemp
;
3364 TRACE("(hProv=%08lx, hHash=%08lx, hKey=%08lx, dwFlags=%08x)\n", hProv
, hHash
, hKey
, dwFlags
);
3366 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pKey
) ||
3367 (GET_ALG_CLASS(pKey
->aiAlgid
) != ALG_CLASS_DATA_ENCRYPT
))
3369 SetLastError(NTE_BAD_KEY
);
3373 if (dwFlags
& ~CRYPT_LITTLE_ENDIAN
) {
3374 SetLastError(NTE_BAD_FLAGS
);
3378 memcpy(abKeyValue
, pKey
->abKeyValue
, pKey
->dwKeyLen
);
3379 if (!(dwFlags
& CRYPT_LITTLE_ENDIAN
)) {
3380 for (i
=0; i
<pKey
->dwKeyLen
/2; i
++) {
3381 bTemp
= abKeyValue
[i
];
3382 abKeyValue
[i
] = abKeyValue
[pKey
->dwKeyLen
-i
-1];
3383 abKeyValue
[pKey
->dwKeyLen
-i
-1] = bTemp
;
3387 return RSAENH_CPHashData(hProv
, hHash
, abKeyValue
, pKey
->dwKeyLen
, 0);
3390 /******************************************************************************
3391 * CPReleaseContext (RSAENH.@)
3393 * Release a key container.
3396 * hProv [I] Key container to be released.
3397 * dwFlags [I] Currently none defined.
3403 BOOL WINAPI
RSAENH_CPReleaseContext(HCRYPTPROV hProv
, DWORD dwFlags
)
3405 TRACE("(hProv=%08lx, dwFlags=%08x)\n", hProv
, dwFlags
);
3407 if (!release_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
3409 /* MSDN: hProv not containing valid context handle */
3410 SetLastError(NTE_BAD_UID
);
3415 SetLastError(NTE_BAD_FLAGS
);
3422 /******************************************************************************
3423 * CPSetHashParam (RSAENH.@)
3425 * Set a parameter of a hash object
3428 * hProv [I] The key container to which the key belongs.
3429 * hHash [I] The hash object for which a parameter is to be set.
3430 * dwParam [I] Parameter type. See Notes.
3431 * pbData [I] Pointer to the parameter value.
3432 * dwFlags [I] Currently none defined.
3439 * Currently only the HP_HMAC_INFO dwParam type is defined.
3440 * The HMAC_INFO struct will be deep copied into the hash object.
3441 * See Internet RFC 2104 for details on the HMAC algorithm.
3443 BOOL WINAPI
RSAENH_CPSetHashParam(HCRYPTPROV hProv
, HCRYPTHASH hHash
, DWORD dwParam
,
3444 BYTE
*pbData
, DWORD dwFlags
)
3446 CRYPTHASH
*pCryptHash
;
3447 CRYPTKEY
*pCryptKey
;
3450 TRACE("(hProv=%08lx, hHash=%08lx, dwParam=%08x, pbData=%p, dwFlags=%08x)\n",
3451 hProv
, hHash
, dwParam
, pbData
, dwFlags
);
3453 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
3455 SetLastError(NTE_BAD_UID
);
3460 SetLastError(NTE_BAD_FLAGS
);
3464 if (!lookup_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
,
3465 (OBJECTHDR
**)&pCryptHash
))
3467 SetLastError(NTE_BAD_HASH
);
3473 free_hmac_info(pCryptHash
->pHMACInfo
);
3474 if (!copy_hmac_info(&pCryptHash
->pHMACInfo
, (PHMAC_INFO
)pbData
)) return FALSE
;
3476 if (!lookup_handle(&handle_table
, pCryptHash
->hKey
, RSAENH_MAGIC_KEY
,
3477 (OBJECTHDR
**)&pCryptKey
))
3479 SetLastError(NTE_FAIL
); /* FIXME: correct error code? */
3483 for (i
=0; i
<RSAENH_MIN(pCryptKey
->dwKeyLen
,pCryptHash
->pHMACInfo
->cbInnerString
); i
++) {
3484 pCryptHash
->pHMACInfo
->pbInnerString
[i
] ^= pCryptKey
->abKeyValue
[i
];
3486 for (i
=0; i
<RSAENH_MIN(pCryptKey
->dwKeyLen
,pCryptHash
->pHMACInfo
->cbOuterString
); i
++) {
3487 pCryptHash
->pHMACInfo
->pbOuterString
[i
] ^= pCryptKey
->abKeyValue
[i
];
3490 init_hash(pCryptHash
);
3494 memcpy(pCryptHash
->abHashValue
, pbData
, pCryptHash
->dwHashSize
);
3495 pCryptHash
->dwState
= RSAENH_HASHSTATE_FINISHED
;
3498 case HP_TLS1PRF_SEED
:
3499 return copy_data_blob(&pCryptHash
->tpPRFParams
.blobSeed
, (PCRYPT_DATA_BLOB
)pbData
);
3501 case HP_TLS1PRF_LABEL
:
3502 return copy_data_blob(&pCryptHash
->tpPRFParams
.blobLabel
, (PCRYPT_DATA_BLOB
)pbData
);
3505 SetLastError(NTE_BAD_TYPE
);
3510 /******************************************************************************
3511 * CPSetProvParam (RSAENH.@)
3513 BOOL WINAPI
RSAENH_CPSetProvParam(HCRYPTPROV hProv
, DWORD dwParam
, BYTE
*pbData
, DWORD dwFlags
)
3519 /******************************************************************************
3520 * CPSignHash (RSAENH.@)
3522 * Sign a hash object
3525 * hProv [I] The key container, to which the hash object belongs.
3526 * hHash [I] The hash object to be signed.
3527 * dwKeySpec [I] AT_SIGNATURE or AT_KEYEXCHANGE: Key used to generate the signature.
3528 * sDescription [I] Should be NULL for security reasons.
3529 * dwFlags [I] 0, CRYPT_NOHASHOID or CRYPT_X931_FORMAT: Format of the signature.
3530 * pbSignature [O] Buffer, to which the signature will be stored. May be NULL to query SigLen.
3531 * pdwSigLen [I/O] Size of the buffer (in), Length of the signature (out)
3537 BOOL WINAPI
RSAENH_CPSignHash(HCRYPTPROV hProv
, HCRYPTHASH hHash
, DWORD dwKeySpec
,
3538 LPCWSTR sDescription
, DWORD dwFlags
, BYTE
*pbSignature
,
3541 HCRYPTKEY hCryptKey
;
3542 CRYPTKEY
*pCryptKey
;
3544 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
];
3547 TRACE("(hProv=%08lx, hHash=%08lx, dwKeySpec=%08x, sDescription=%s, dwFlags=%08x, "
3548 "pbSignature=%p, pdwSigLen=%p)\n", hProv
, hHash
, dwKeySpec
, debugstr_w(sDescription
),
3549 dwFlags
, pbSignature
, pdwSigLen
);
3551 if (dwFlags
& ~(CRYPT_NOHASHOID
|CRYPT_X931_FORMAT
)) {
3552 SetLastError(NTE_BAD_FLAGS
);
3556 if (!RSAENH_CPGetUserKey(hProv
, dwKeySpec
, &hCryptKey
)) return FALSE
;
3558 if (!lookup_handle(&handle_table
, hCryptKey
, RSAENH_MAGIC_KEY
,
3559 (OBJECTHDR
**)&pCryptKey
))
3561 SetLastError(NTE_NO_KEY
);
3566 *pdwSigLen
= pCryptKey
->dwKeyLen
;
3569 if (pCryptKey
->dwKeyLen
> *pdwSigLen
)
3571 SetLastError(ERROR_MORE_DATA
);
3572 *pdwSigLen
= pCryptKey
->dwKeyLen
;
3575 *pdwSigLen
= pCryptKey
->dwKeyLen
;
3578 if (!RSAENH_CPHashData(hProv
, hHash
, (CONST BYTE
*)sDescription
,
3579 (DWORD
)lstrlenW(sDescription
)*sizeof(WCHAR
), 0))
3585 dwHashLen
= sizeof(DWORD
);
3586 if (!RSAENH_CPGetHashParam(hProv
, hHash
, HP_ALGID
, (BYTE
*)&aiAlgid
, &dwHashLen
, 0)) return FALSE
;
3588 dwHashLen
= RSAENH_MAX_HASH_SIZE
;
3589 if (!RSAENH_CPGetHashParam(hProv
, hHash
, HP_HASHVAL
, abHashValue
, &dwHashLen
, 0)) return FALSE
;
3592 if (!build_hash_signature(pbSignature
, *pdwSigLen
, aiAlgid
, abHashValue
, dwHashLen
, dwFlags
)) {
3596 return encrypt_block_impl(pCryptKey
->aiAlgid
, PK_PRIVATE
, &pCryptKey
->context
, pbSignature
, pbSignature
, RSAENH_ENCRYPT
);
3599 /******************************************************************************
3600 * CPVerifySignature (RSAENH.@)
3602 * Verify the signature of a hash object.
3605 * hProv [I] The key container, to which the hash belongs.
3606 * hHash [I] The hash for which the signature is verified.
3607 * pbSignature [I] The binary signature.
3608 * dwSigLen [I] Length of the signature BLOB.
3609 * hPubKey [I] Public key used to verify the signature.
3610 * sDescription [I] Should be NULL for security reasons.
3611 * dwFlags [I] 0, CRYPT_NOHASHOID or CRYPT_X931_FORMAT: Format of the signature.
3614 * Success: TRUE (Signature is valid)
3615 * Failure: FALSE (GetLastError() == NTE_BAD_SIGNATURE, if signature is invalid)
3617 BOOL WINAPI
RSAENH_CPVerifySignature(HCRYPTPROV hProv
, HCRYPTHASH hHash
, CONST BYTE
*pbSignature
,
3618 DWORD dwSigLen
, HCRYPTKEY hPubKey
, LPCWSTR sDescription
,
3621 BYTE
*pbConstructed
= NULL
, *pbDecrypted
= NULL
;
3622 CRYPTKEY
*pCryptKey
;
3625 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
];
3628 TRACE("(hProv=%08lx, hHash=%08lx, pbSignature=%p, dwSigLen=%d, hPubKey=%08lx, sDescription=%s, "
3629 "dwFlags=%08x)\n", hProv
, hHash
, pbSignature
, dwSigLen
, hPubKey
, debugstr_w(sDescription
),
3632 if (dwFlags
& ~(CRYPT_NOHASHOID
|CRYPT_X931_FORMAT
)) {
3633 SetLastError(NTE_BAD_FLAGS
);
3637 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
3639 SetLastError(NTE_BAD_UID
);
3643 if (!lookup_handle(&handle_table
, hPubKey
, RSAENH_MAGIC_KEY
,
3644 (OBJECTHDR
**)&pCryptKey
))
3646 SetLastError(NTE_BAD_KEY
);
3650 /* in Microsoft implementation, the signature length is checked before
3651 * the signature pointer.
3653 if (dwSigLen
!= pCryptKey
->dwKeyLen
)
3655 SetLastError(NTE_BAD_SIGNATURE
);
3659 if (!hHash
|| !pbSignature
)
3661 SetLastError(ERROR_INVALID_PARAMETER
);
3666 if (!RSAENH_CPHashData(hProv
, hHash
, (CONST BYTE
*)sDescription
,
3667 (DWORD
)lstrlenW(sDescription
)*sizeof(WCHAR
), 0))
3673 dwHashLen
= sizeof(DWORD
);
3674 if (!RSAENH_CPGetHashParam(hProv
, hHash
, HP_ALGID
, (BYTE
*)&aiAlgid
, &dwHashLen
, 0)) return FALSE
;
3676 dwHashLen
= RSAENH_MAX_HASH_SIZE
;
3677 if (!RSAENH_CPGetHashParam(hProv
, hHash
, HP_HASHVAL
, abHashValue
, &dwHashLen
, 0)) return FALSE
;
3679 pbConstructed
= HeapAlloc(GetProcessHeap(), 0, dwSigLen
);
3680 if (!pbConstructed
) {
3681 SetLastError(NTE_NO_MEMORY
);
3685 pbDecrypted
= HeapAlloc(GetProcessHeap(), 0, dwSigLen
);
3687 SetLastError(NTE_NO_MEMORY
);
3691 if (!encrypt_block_impl(pCryptKey
->aiAlgid
, PK_PUBLIC
, &pCryptKey
->context
, pbSignature
, pbDecrypted
,
3697 if (!build_hash_signature(pbConstructed
, dwSigLen
, aiAlgid
, abHashValue
, dwHashLen
, dwFlags
)) {
3701 if (memcmp(pbDecrypted
, pbConstructed
, dwSigLen
)) {
3702 SetLastError(NTE_BAD_SIGNATURE
);
3708 HeapFree(GetProcessHeap(), 0, pbConstructed
);
3709 HeapFree(GetProcessHeap(), 0, pbDecrypted
);
3713 static const WCHAR szProviderKeys
[4][97] = {
3714 { 'S','o','f','t','w','a','r','e','\\',
3715 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
3716 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
3717 'i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ','B','a','s',
3718 'e',' ','C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r',
3719 'o','v','i','d','e','r',' ','v','1','.','0',0 },
3720 { 'S','o','f','t','w','a','r','e','\\',
3721 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
3722 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
3723 'i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ',
3724 'E','n','h','a','n','c','e','d',
3725 ' ','C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r',
3726 'o','v','i','d','e','r',' ','v','1','.','0',0 },
3727 { 'S','o','f','t','w','a','r','e','\\',
3728 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
3729 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
3730 'i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ','S','t','r','o','n','g',
3731 ' ','C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r',
3732 'o','v','i','d','e','r',0 },
3733 { 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
3734 'C','r','y','p','t','o','g','r','a','p','h','y','\\','D','e','f','a','u','l','t','s','\\',
3735 'P','r','o','v','i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ',
3736 'R','S','A',' ','S','C','h','a','n','n','e','l',' ',
3737 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r','o','v','i','d','e','r',0 }
3739 static const WCHAR szDefaultKeys
[2][65] = {
3740 { 'S','o','f','t','w','a','r','e','\\',
3741 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
3742 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
3743 'i','d','e','r',' ','T','y','p','e','s','\\','T','y','p','e',' ','0','0','1',0 },
3744 { 'S','o','f','t','w','a','r','e','\\',
3745 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
3746 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
3747 'i','d','e','r',' ','T','y','p','e','s','\\','T','y','p','e',' ','0','1','2',0 }
3751 /******************************************************************************
3752 * DllRegisterServer (RSAENH.@)
3754 * Dll self registration.
3763 * Registers the following keys:
3764 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider\
3765 * Microsoft Base Cryptographic Provider v1.0
3766 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider\
3767 * Microsoft Enhanced Cryptographic Provider
3768 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider\
3769 * Microsoft Strong Cryptographpic Provider
3770 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider Types\Type 001
3772 HRESULT WINAPI
DllRegisterServer(void)
3779 for (i
=0; i
<4; i
++) {
3780 apiRet
= RegCreateKeyExW(HKEY_LOCAL_MACHINE
, szProviderKeys
[i
], 0, NULL
,
3781 REG_OPTION_NON_VOLATILE
, KEY_ALL_ACCESS
, NULL
, &key
, &dp
);
3783 if (apiRet
== ERROR_SUCCESS
)
3785 if (dp
== REG_CREATED_NEW_KEY
)
3787 static const WCHAR szImagePath
[] = { 'I','m','a','g','e',' ','P','a','t','h',0 };
3788 static const WCHAR szRSABase
[] = { 'r','s','a','e','n','h','.','d','l','l',0 };
3789 static const WCHAR szType
[] = { 'T','y','p','e',0 };
3790 static const WCHAR szSignature
[] = { 'S','i','g','n','a','t','u','r','e',0 };
3791 DWORD type
= (i
== 3) ? PROV_RSA_SCHANNEL
: PROV_RSA_FULL
;
3792 DWORD sign
= 0xdeadbeef;
3793 RegSetValueExW(key
, szImagePath
, 0, REG_SZ
, (const BYTE
*)szRSABase
,
3794 (lstrlenW(szRSABase
) + 1) * sizeof(WCHAR
));
3795 RegSetValueExW(key
, szType
, 0, REG_DWORD
, (LPBYTE
)&type
, sizeof(type
));
3796 RegSetValueExW(key
, szSignature
, 0, REG_BINARY
, (LPBYTE
)&sign
, sizeof(sign
));
3802 for (i
=0; i
<2; i
++) {
3803 apiRet
= RegCreateKeyExW(HKEY_LOCAL_MACHINE
, szDefaultKeys
[i
], 0, NULL
,
3804 REG_OPTION_NON_VOLATILE
, KEY_ALL_ACCESS
, NULL
, &key
, &dp
);
3805 if (apiRet
== ERROR_SUCCESS
)
3807 if (dp
== REG_CREATED_NEW_KEY
)
3809 static const WCHAR szName
[] = { 'N','a','m','e',0 };
3810 static const WCHAR szRSAName
[2][46] = {
3811 { 'M','i','c','r','o','s','o','f','t',' ', 'B','a','s','e',' ',
3812 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ',
3813 'P','r','o','v','i','d','e','r',' ','v','1','.','0',0 },
3814 { 'M','i','c','r','o','s','o','f','t',' ','R','S','A',' ',
3815 'S','C','h','a','n','n','e','l',' ',
3816 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ',
3817 'P','r','o','v','i','d','e','r',0 } };
3818 static const WCHAR szTypeName
[] = { 'T','y','p','e','N','a','m','e',0 };
3819 static const WCHAR szRSATypeName
[2][38] = {
3820 { 'R','S','A',' ','F','u','l','l',' ',
3821 '(','S','i','g','n','a','t','u','r','e',' ','a','n','d',' ',
3822 'K','e','y',' ','E','x','c','h','a','n','g','e',')',0 },
3823 { 'R','S','A',' ','S','C','h','a','n','n','e','l',0 } };
3825 RegSetValueExW(key
, szName
, 0, REG_SZ
,
3826 (const BYTE
*)szRSAName
[i
], lstrlenW(szRSAName
[i
])*sizeof(WCHAR
)+sizeof(WCHAR
));
3827 RegSetValueExW(key
, szTypeName
, 0, REG_SZ
,
3828 (const BYTE
*)szRSATypeName
[i
], lstrlenW(szRSATypeName
[i
])*sizeof(WCHAR
)+sizeof(WCHAR
));
3834 return HRESULT_FROM_WIN32(apiRet
);
3837 /******************************************************************************
3838 * DllUnregisterServer (RSAENH.@)
3840 * Dll self unregistration.
3848 * For the relevant keys see DllRegisterServer.
3850 HRESULT WINAPI
DllUnregisterServer(void)
3852 RegDeleteKeyW(HKEY_LOCAL_MACHINE
, szProviderKeys
[0]);
3853 RegDeleteKeyW(HKEY_LOCAL_MACHINE
, szProviderKeys
[1]);
3854 RegDeleteKeyW(HKEY_LOCAL_MACHINE
, szProviderKeys
[2]);
3855 RegDeleteKeyW(HKEY_LOCAL_MACHINE
, szProviderKeys
[3]);
3856 RegDeleteKeyW(HKEY_LOCAL_MACHINE
, szDefaultKeys
[0]);
3857 RegDeleteKeyW(HKEY_LOCAL_MACHINE
, szDefaultKeys
[1]);