3 * RSAENH - RSA encryption for Wine
5 * Copyright 2002 TransGaming Technologies (David Hammerton)
6 * Copyright 2004 Mike McCormack for CodeWeavers
7 * Copyright 2004, 2005 Michael Jung
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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_IDLE 0
48 #define RSAENH_HASHSTATE_HASHING 1
49 #define RSAENH_HASHSTATE_FINISHED 2
50 typedef struct _RSAENH_TLS1PRF_PARAMS
52 CRYPT_DATA_BLOB blobLabel
;
53 CRYPT_DATA_BLOB blobSeed
;
54 } RSAENH_TLS1PRF_PARAMS
;
56 typedef struct tagCRYPTHASH
65 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
];
67 RSAENH_TLS1PRF_PARAMS tpPRFParams
;
70 /******************************************************************************
71 * CRYPTKEY - key objects
73 #define RSAENH_MAGIC_KEY 0x73620457u
74 #define RSAENH_MAX_KEY_SIZE 48
75 #define RSAENH_MAX_BLOCK_SIZE 24
76 #define RSAENH_KEYSTATE_IDLE 0
77 #define RSAENH_KEYSTATE_ENCRYPTING 1
78 #define RSAENH_KEYSTATE_DECRYPTING 2
79 #define RSAENH_KEYSTATE_MASTERKEY 3
80 typedef struct _RSAENH_SCHANNEL_INFO
82 SCHANNEL_ALG saEncAlg
;
83 SCHANNEL_ALG saMACAlg
;
84 CRYPT_DATA_BLOB blobClientRandom
;
85 CRYPT_DATA_BLOB blobServerRandom
;
86 } RSAENH_SCHANNEL_INFO
;
88 typedef struct tagCRYPTKEY
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 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 init_handle_table(&handle_table
);
340 case DLL_PROCESS_DETACH
:
341 destroy_handle_table(&handle_table
);
347 /******************************************************************************
348 * copy_param [Internal]
350 * Helper function that supports the standard WINAPI protocol for querying data
354 * pbBuffer [O] Buffer where the queried parameter is copied to, if it is large enough.
355 * May be NUL if the required buffer size is to be queried only.
356 * pdwBufferSize [I/O] In: Size of the buffer at pbBuffer
357 * Out: Size of parameter pbParam
358 * pbParam [I] Parameter value.
359 * dwParamSize [I] Size of pbParam
362 * Success: TRUE (pbParam was copied into pbBuffer or pbBuffer is NULL)
363 * Failure: FALSE (pbBuffer is not large enough to hold pbParam). Last error: ERROR_MORE_DATA
365 static inline BOOL
copy_param(
366 BYTE
*pbBuffer
, DWORD
*pdwBufferSize
, CONST BYTE
*pbParam
, DWORD dwParamSize
)
370 if (dwParamSize
> *pdwBufferSize
)
372 SetLastError(ERROR_MORE_DATA
);
373 *pdwBufferSize
= dwParamSize
;
376 memcpy(pbBuffer
, pbParam
, dwParamSize
);
378 *pdwBufferSize
= dwParamSize
;
382 /******************************************************************************
383 * get_algid_info [Internal]
385 * Query CSP capabilities for a given crypto algorithm.
388 * hProv [I] Handle to a key container of the CSP whose capabilities are to be queried.
389 * algid [I] Identifier of the crypto algorithm about which information is requested.
392 * Success: Pointer to a PROV_ENUMALGS_EX struct containing information about the crypto algorithm.
393 * Failure: NULL (algid not supported)
395 static inline const PROV_ENUMALGS_EX
* get_algid_info(HCRYPTPROV hProv
, ALG_ID algid
) {
396 PROV_ENUMALGS_EX
*iterator
;
397 KEYCONTAINER
*pKeyContainer
;
399 if (!lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
, (OBJECTHDR
**)&pKeyContainer
)) {
400 SetLastError(NTE_BAD_UID
);
404 for (iterator
= aProvEnumAlgsEx
[pKeyContainer
->dwPersonality
]; iterator
->aiAlgid
; iterator
++) {
405 if (iterator
->aiAlgid
== algid
) return iterator
;
408 SetLastError(NTE_BAD_ALGID
);
412 /******************************************************************************
413 * copy_data_blob [Internal]
415 * deeply copies a DATA_BLOB
418 * dst [O] That's where the blob will be copied to
419 * src [I] Source blob
423 * Failure: FALSE (GetLastError() == NTE_NO_MEMORY
426 * Use free_data_blob to release resources occupied by copy_data_blob.
428 static inline BOOL
copy_data_blob(PCRYPT_DATA_BLOB dst
, CONST PCRYPT_DATA_BLOB src
) {
429 dst
->pbData
= (BYTE
*)HeapAlloc(GetProcessHeap(), 0, src
->cbData
);
431 SetLastError(NTE_NO_MEMORY
);
434 dst
->cbData
= src
->cbData
;
435 memcpy(dst
->pbData
, src
->pbData
, src
->cbData
);
439 /******************************************************************************
440 * concat_data_blobs [Internal]
442 * Concatenates two blobs
445 * dst [O] The new blob will be copied here
446 * src1 [I] Prefix blob
447 * src2 [I] Appendix blob
451 * Failure: FALSE (GetLastError() == NTE_NO_MEMORY)
454 * Release resources occupied by concat_data_blobs with free_data_blobs
456 static inline BOOL
concat_data_blobs(PCRYPT_DATA_BLOB dst
, CONST PCRYPT_DATA_BLOB src1
,
457 CONST PCRYPT_DATA_BLOB src2
)
459 dst
->cbData
= src1
->cbData
+ src2
->cbData
;
460 dst
->pbData
= (BYTE
*)HeapAlloc(GetProcessHeap(), 0, dst
->cbData
);
462 SetLastError(NTE_NO_MEMORY
);
465 memcpy(dst
->pbData
, src1
->pbData
, src1
->cbData
);
466 memcpy(dst
->pbData
+ src1
->cbData
, src2
->pbData
, src2
->cbData
);
470 /******************************************************************************
471 * free_data_blob [Internal]
473 * releases resource occupied by a dynamically allocated CRYPT_DATA_BLOB
476 * pBlob [I] Heap space occupied by pBlob->pbData is released
478 static inline void free_data_blob(PCRYPT_DATA_BLOB pBlob
) {
479 HeapFree(GetProcessHeap(), 0, pBlob
->pbData
);
482 /******************************************************************************
483 * init_data_blob [Internal]
485 static inline void init_data_blob(PCRYPT_DATA_BLOB pBlob
) {
486 pBlob
->pbData
= NULL
;
490 /******************************************************************************
491 * free_hmac_info [Internal]
493 * Deeply free an HMAC_INFO struct.
496 * hmac_info [I] Pointer to the HMAC_INFO struct to be freed.
499 * See Internet RFC 2104 for details on the HMAC algorithm.
501 static inline void free_hmac_info(PHMAC_INFO hmac_info
) {
502 if (!hmac_info
) return;
503 HeapFree(GetProcessHeap(), 0, hmac_info
->pbInnerString
);
504 HeapFree(GetProcessHeap(), 0, hmac_info
->pbOuterString
);
505 HeapFree(GetProcessHeap(), 0, hmac_info
);
508 /******************************************************************************
509 * copy_hmac_info [Internal]
511 * Deeply copy an HMAC_INFO struct
514 * dst [O] Pointer to a location where the pointer to the HMAC_INFO copy will be stored.
515 * src [I] Pointer to the HMAC_INFO struct to be copied.
522 * See Internet RFC 2104 for details on the HMAC algorithm.
524 static BOOL
copy_hmac_info(PHMAC_INFO
*dst
, PHMAC_INFO src
) {
525 if (!src
) return FALSE
;
526 *dst
= (PHMAC_INFO
)HeapAlloc(GetProcessHeap(), 0, sizeof(HMAC_INFO
));
527 if (!*dst
) return FALSE
;
528 memcpy(*dst
, src
, sizeof(HMAC_INFO
));
529 (*dst
)->pbInnerString
= NULL
;
530 (*dst
)->pbOuterString
= NULL
;
531 if ((*dst
)->cbInnerString
== 0) (*dst
)->cbInnerString
= RSAENH_HMAC_DEF_PAD_LEN
;
532 (*dst
)->pbInnerString
= (BYTE
*)HeapAlloc(GetProcessHeap(), 0, (*dst
)->cbInnerString
);
533 if (!(*dst
)->pbInnerString
) {
534 free_hmac_info(*dst
);
537 if (src
->cbInnerString
)
538 memcpy((*dst
)->pbInnerString
, src
->pbInnerString
, src
->cbInnerString
);
540 memset((*dst
)->pbInnerString
, RSAENH_HMAC_DEF_IPAD_CHAR
, RSAENH_HMAC_DEF_PAD_LEN
);
541 if ((*dst
)->cbOuterString
== 0) (*dst
)->cbOuterString
= RSAENH_HMAC_DEF_PAD_LEN
;
542 (*dst
)->pbOuterString
= (BYTE
*)HeapAlloc(GetProcessHeap(), 0, (*dst
)->cbOuterString
);
543 if (!(*dst
)->pbOuterString
) {
544 free_hmac_info(*dst
);
547 if (src
->cbOuterString
)
548 memcpy((*dst
)->pbOuterString
, src
->pbOuterString
, src
->cbOuterString
);
550 memset((*dst
)->pbOuterString
, RSAENH_HMAC_DEF_OPAD_CHAR
, RSAENH_HMAC_DEF_PAD_LEN
);
554 /******************************************************************************
555 * destroy_hash [Internal]
557 * Destructor for hash objects
560 * pCryptHash [I] Pointer to the hash object to be destroyed.
561 * Will be invalid after function returns!
563 static void destroy_hash(OBJECTHDR
*pObject
)
565 CRYPTHASH
*pCryptHash
= (CRYPTHASH
*)pObject
;
567 free_hmac_info(pCryptHash
->pHMACInfo
);
568 free_data_blob(&pCryptHash
->tpPRFParams
.blobLabel
);
569 free_data_blob(&pCryptHash
->tpPRFParams
.blobSeed
);
570 HeapFree(GetProcessHeap(), 0, pCryptHash
);
573 /******************************************************************************
574 * init_hash [Internal]
576 * Initialize (or reset) a hash object
579 * pCryptHash [I] The hash object to be initialized.
581 static inline BOOL
init_hash(CRYPTHASH
*pCryptHash
) {
584 switch (pCryptHash
->aiAlgid
)
587 if (pCryptHash
->pHMACInfo
) {
588 const PROV_ENUMALGS_EX
*pAlgInfo
;
590 pAlgInfo
= get_algid_info(pCryptHash
->hProv
, pCryptHash
->pHMACInfo
->HashAlgid
);
591 if (!pAlgInfo
) return FALSE
;
592 pCryptHash
->dwHashSize
= pAlgInfo
->dwDefaultLen
>> 3;
593 init_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
);
594 update_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
595 pCryptHash
->pHMACInfo
->pbInnerString
,
596 pCryptHash
->pHMACInfo
->cbInnerString
);
601 dwLen
= sizeof(DWORD
);
602 RSAENH_CPGetKeyParam(pCryptHash
->hProv
, pCryptHash
->hKey
, KP_BLOCKLEN
,
603 (BYTE
*)&pCryptHash
->dwHashSize
, &dwLen
, 0);
604 pCryptHash
->dwHashSize
>>= 3;
608 return init_hash_impl(pCryptHash
->aiAlgid
, &pCryptHash
->context
);
612 /******************************************************************************
613 * update_hash [Internal]
615 * Hashes the given data and updates the hash object's state accordingly
618 * pCryptHash [I] Hash object to be updated.
619 * pbData [I] Pointer to data stream to be hashed.
620 * dwDataLen [I] Length of data stream.
622 static inline void update_hash(CRYPTHASH
*pCryptHash
, CONST BYTE
*pbData
, DWORD dwDataLen
) {
625 switch (pCryptHash
->aiAlgid
)
628 if (pCryptHash
->pHMACInfo
)
629 update_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
634 pbTemp
= (BYTE
*)HeapAlloc(GetProcessHeap(), 0, dwDataLen
);
636 memcpy(pbTemp
, pbData
, dwDataLen
);
637 RSAENH_CPEncrypt(pCryptHash
->hProv
, pCryptHash
->hKey
, (HCRYPTHASH
)NULL
, FALSE
, 0,
638 pbTemp
, &dwDataLen
, dwDataLen
);
639 HeapFree(GetProcessHeap(), 0, pbTemp
);
643 update_hash_impl(pCryptHash
->aiAlgid
, &pCryptHash
->context
, pbData
, dwDataLen
);
647 /******************************************************************************
648 * finalize_hash [Internal]
650 * Finalizes the hash, after all data has been hashed with update_hash.
651 * No additional data can be hashed afterwards until the hash gets initialized again.
654 * pCryptHash [I] Hash object to be finalized.
656 static inline void finalize_hash(CRYPTHASH
*pCryptHash
) {
659 switch (pCryptHash
->aiAlgid
)
662 if (pCryptHash
->pHMACInfo
) {
663 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
];
665 finalize_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
666 pCryptHash
->abHashValue
);
667 memcpy(abHashValue
, pCryptHash
->abHashValue
, pCryptHash
->dwHashSize
);
668 init_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
);
669 update_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
670 pCryptHash
->pHMACInfo
->pbOuterString
,
671 pCryptHash
->pHMACInfo
->cbOuterString
);
672 update_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
673 abHashValue
, pCryptHash
->dwHashSize
);
674 finalize_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
675 pCryptHash
->abHashValue
);
681 RSAENH_CPEncrypt(pCryptHash
->hProv
, pCryptHash
->hKey
, (HCRYPTHASH
)NULL
, TRUE
, 0,
682 pCryptHash
->abHashValue
, &dwDataLen
, pCryptHash
->dwHashSize
);
686 finalize_hash_impl(pCryptHash
->aiAlgid
, &pCryptHash
->context
, pCryptHash
->abHashValue
);
690 /******************************************************************************
691 * destroy_key [Internal]
693 * Destructor for key objects
696 * pCryptKey [I] Pointer to the key object to be destroyed.
697 * Will be invalid after function returns!
699 static void destroy_key(OBJECTHDR
*pObject
)
701 CRYPTKEY
*pCryptKey
= (CRYPTKEY
*)pObject
;
703 free_key_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
);
704 free_data_blob(&pCryptKey
->siSChannelInfo
.blobClientRandom
);
705 free_data_blob(&pCryptKey
->siSChannelInfo
.blobServerRandom
);
706 HeapFree(GetProcessHeap(), 0, pCryptKey
);
709 /******************************************************************************
710 * setup_key [Internal]
712 * Initialize (or reset) a key object
715 * pCryptKey [I] The key object to be initialized.
717 static inline void setup_key(CRYPTKEY
*pCryptKey
) {
718 pCryptKey
->dwState
= RSAENH_KEYSTATE_IDLE
;
719 memcpy(pCryptKey
->abChainVector
, pCryptKey
->abInitVector
, sizeof(pCryptKey
->abChainVector
));
720 setup_key_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pCryptKey
->dwKeyLen
,
721 pCryptKey
->dwSaltLen
, pCryptKey
->abKeyValue
);
724 /******************************************************************************
727 * Creates a new key object without assigning the actual binary key value.
728 * This is done by CPDeriveKey, CPGenKey or CPImportKey, which call this function.
731 * hProv [I] Handle to the provider to which the created key will belong.
732 * aiAlgid [I] The new key shall use the crypto algorithm idenfied by aiAlgid.
733 * dwFlags [I] Upper 16 bits give the key length.
734 * Lower 16 bits: CRYPT_CREATE_SALT, CRYPT_NO_SALT
735 * ppCryptKey [O] Pointer to the created key
738 * Success: Handle to the created key.
739 * Failure: INVALID_HANDLE_VALUE
741 static HCRYPTKEY
new_key(HCRYPTPROV hProv
, ALG_ID aiAlgid
, DWORD dwFlags
, CRYPTKEY
**ppCryptKey
)
745 DWORD dwKeyLen
= HIWORD(dwFlags
);
746 const PROV_ENUMALGS_EX
*peaAlgidInfo
;
751 * Retrieve the CSP's capabilities for the given ALG_ID value
753 peaAlgidInfo
= get_algid_info(hProv
, aiAlgid
);
754 if (!peaAlgidInfo
) return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
757 * Assume the default key length, if none is specified explicitly
759 if (dwKeyLen
== 0) dwKeyLen
= peaAlgidInfo
->dwDefaultLen
;
762 * Check if the requested key length is supported by the current CSP.
763 * Adjust key length's for DES algorithms.
767 if (dwKeyLen
== RSAENH_DES_EFFECTIVE_KEYLEN
) {
768 dwKeyLen
= RSAENH_DES_STORAGE_KEYLEN
;
770 if (dwKeyLen
!= RSAENH_DES_STORAGE_KEYLEN
) {
771 SetLastError(NTE_BAD_FLAGS
);
772 return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
777 if (dwKeyLen
== RSAENH_3DES112_EFFECTIVE_KEYLEN
) {
778 dwKeyLen
= RSAENH_3DES112_STORAGE_KEYLEN
;
780 if (dwKeyLen
!= RSAENH_3DES112_STORAGE_KEYLEN
) {
781 SetLastError(NTE_BAD_FLAGS
);
782 return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
787 if (dwKeyLen
== RSAENH_3DES_EFFECTIVE_KEYLEN
) {
788 dwKeyLen
= RSAENH_3DES_STORAGE_KEYLEN
;
790 if (dwKeyLen
!= RSAENH_3DES_STORAGE_KEYLEN
) {
791 SetLastError(NTE_BAD_FLAGS
);
792 return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
798 dwKeyLen
> peaAlgidInfo
->dwMaxLen
||
799 dwKeyLen
< peaAlgidInfo
->dwMinLen
)
801 SetLastError(NTE_BAD_FLAGS
);
802 return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
806 hCryptKey
= (HCRYPTKEY
)new_object(&handle_table
, sizeof(CRYPTKEY
), RSAENH_MAGIC_KEY
,
807 destroy_key
, (OBJECTHDR
**)&pCryptKey
);
808 if (hCryptKey
!= (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
810 pCryptKey
->aiAlgid
= aiAlgid
;
811 pCryptKey
->hProv
= hProv
;
812 pCryptKey
->dwModeBits
= 0;
813 pCryptKey
->dwPermissions
= CRYPT_ENCRYPT
| CRYPT_DECRYPT
| CRYPT_READ
| CRYPT_WRITE
|
815 pCryptKey
->dwKeyLen
= dwKeyLen
>> 3;
816 if ((dwFlags
& CRYPT_CREATE_SALT
) || (dwKeyLen
== 40 && !(dwFlags
& CRYPT_NO_SALT
)))
817 pCryptKey
->dwSaltLen
= 16 /*FIXME*/ - pCryptKey
->dwKeyLen
;
819 pCryptKey
->dwSaltLen
= 0;
820 memset(pCryptKey
->abKeyValue
, 0, sizeof(pCryptKey
->abKeyValue
));
821 memset(pCryptKey
->abInitVector
, 0, sizeof(pCryptKey
->abInitVector
));
822 init_data_blob(&pCryptKey
->siSChannelInfo
.blobClientRandom
);
823 init_data_blob(&pCryptKey
->siSChannelInfo
.blobServerRandom
);
827 case CALG_PCT1_MASTER
:
828 case CALG_SSL2_MASTER
:
829 case CALG_SSL3_MASTER
:
830 case CALG_TLS1_MASTER
:
832 pCryptKey
->dwBlockLen
= 0;
833 pCryptKey
->dwMode
= 0;
840 pCryptKey
->dwBlockLen
= 8;
841 pCryptKey
->dwMode
= CRYPT_MODE_CBC
;
846 pCryptKey
->dwBlockLen
= dwKeyLen
>> 3;
847 pCryptKey
->dwMode
= 0;
851 *ppCryptKey
= pCryptKey
;
857 /******************************************************************************
858 * destroy_key_container [Internal]
860 * Destructor for key containers. The user's signature and key exchange private
861 * keys are stored in the registry _IN_PLAINTEXT_.
864 * pObjectHdr [I] Pointer to the key container to be destroyed.
866 static void destroy_key_container(OBJECTHDR
*pObjectHdr
)
868 KEYCONTAINER
*pKeyContainer
= (KEYCONTAINER
*)pObjectHdr
;
870 CHAR szRSABase
[MAX_PATH
];
875 if (!(pKeyContainer
->dwFlags
& CRYPT_VERIFYCONTEXT
)) {
876 /* On WinXP, persistent keys are stored in a file located at:
877 * $AppData$\\Microsoft\\Crypto\\RSA\\$SID$\\some_hex_string
879 sprintf(szRSABase
, RSAENH_REGKEY
, pKeyContainer
->szName
);
881 if (pKeyContainer
->dwFlags
& CRYPT_MACHINE_KEYSET
) {
882 hRootKey
= HKEY_LOCAL_MACHINE
;
884 hRootKey
= HKEY_CURRENT_USER
;
887 if (RegCreateKeyExA(hRootKey
, szRSABase
, 0, NULL
, REG_OPTION_NON_VOLATILE
,
888 KEY_WRITE
, NULL
, &hKey
, NULL
) == ERROR_SUCCESS
)
890 if (lookup_handle(&handle_table
, pKeyContainer
->hKeyExchangeKeyPair
, RSAENH_MAGIC_KEY
,
893 if (RSAENH_CPExportKey(pKey
->hProv
, pKeyContainer
->hKeyExchangeKeyPair
, 0,
894 PRIVATEKEYBLOB
, 0, 0, &dwLen
))
896 pbKey
= (BYTE
*)HeapAlloc(GetProcessHeap(), 0, dwLen
);
899 if (RSAENH_CPExportKey(pKey
->hProv
, pKeyContainer
->hKeyExchangeKeyPair
, 0,
900 PRIVATEKEYBLOB
, 0, pbKey
, &dwLen
))
902 RegSetValueExA(hKey
, "KeyExchangeKeyPair", 0, REG_BINARY
, pbKey
, dwLen
);
904 HeapFree(GetProcessHeap(), 0, pbKey
);
907 release_handle(&handle_table
, (unsigned int)pKeyContainer
->hKeyExchangeKeyPair
,
911 if (lookup_handle(&handle_table
, pKeyContainer
->hSignatureKeyPair
, RSAENH_MAGIC_KEY
,
914 if (RSAENH_CPExportKey(pKey
->hProv
, pKeyContainer
->hSignatureKeyPair
, 0,
915 PRIVATEKEYBLOB
, 0, 0, &dwLen
))
917 pbKey
= (BYTE
*)HeapAlloc(GetProcessHeap(), 0, dwLen
);
920 if (RSAENH_CPExportKey(pKey
->hProv
, pKeyContainer
->hSignatureKeyPair
, 0,
921 PRIVATEKEYBLOB
, 0, pbKey
, &dwLen
))
923 RegSetValueExA(hKey
, "SignatureKeyPair", 0, REG_BINARY
, pbKey
, dwLen
);
925 HeapFree(GetProcessHeap(), 0, pbKey
);
928 release_handle(&handle_table
, (unsigned int)pKeyContainer
->hSignatureKeyPair
,
936 HeapFree( GetProcessHeap(), 0, pKeyContainer
);
939 /******************************************************************************
940 * new_key_container [Internal]
942 * Create a new key container. The personality (RSA Base, Strong or Enhanced CP)
943 * of the CSP is determined via the pVTable->pszProvName string.
946 * pszContainerName [I] Name of the key container.
947 * pVTable [I] Callback functions and context info provided by the OS
950 * Success: Handle to the new key container.
951 * Failure: INVALID_HANDLE_VALUE
953 static HCRYPTPROV
new_key_container(PCHAR pszContainerName
, DWORD dwFlags
, PVTableProvStruc pVTable
)
955 KEYCONTAINER
*pKeyContainer
;
956 HCRYPTPROV hKeyContainer
;
958 hKeyContainer
= (HCRYPTPROV
)new_object(&handle_table
, sizeof(KEYCONTAINER
), RSAENH_MAGIC_CONTAINER
,
959 destroy_key_container
, (OBJECTHDR
**)&pKeyContainer
);
960 if (hKeyContainer
!= (HCRYPTPROV
)INVALID_HANDLE_VALUE
)
962 strncpy(pKeyContainer
->szName
, pszContainerName
, MAX_PATH
);
963 pKeyContainer
->szName
[MAX_PATH
-1] = '\0';
964 pKeyContainer
->dwFlags
= dwFlags
;
965 pKeyContainer
->dwEnumAlgsCtr
= 0;
966 pKeyContainer
->hKeyExchangeKeyPair
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
967 pKeyContainer
->hSignatureKeyPair
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
968 if (pVTable
&& pVTable
->pszProvName
) {
969 strncpy(pKeyContainer
->szProvName
, pVTable
->pszProvName
, MAX_PATH
);
970 pKeyContainer
->szProvName
[MAX_PATH
-1] = '\0';
971 if (!strcmp(pVTable
->pszProvName
, MS_DEF_PROV_A
)) {
972 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_BASE
;
973 } else if (!strcmp(pVTable
->pszProvName
, MS_ENHANCED_PROV_A
)) {
974 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_ENHANCED
;
975 } else if (!strcmp(pVTable
->pszProvName
, MS_DEF_RSA_SCHANNEL_PROV_A
)) {
976 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_SCHANNEL
;
978 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_STRONG
;
982 /* The new key container has to be inserted into the CSP immediately
983 * after creation to be available for CPGetProvParam's PP_ENUMCONTAINERS. */
984 if (!(dwFlags
& CRYPT_VERIFYCONTEXT
)) {
985 BYTE szRSABase
[MAX_PATH
];
988 sprintf(szRSABase
, RSAENH_REGKEY
, pKeyContainer
->szName
);
990 if (pKeyContainer
->dwFlags
& CRYPT_MACHINE_KEYSET
) {
991 hRootKey
= HKEY_LOCAL_MACHINE
;
993 hRootKey
= HKEY_CURRENT_USER
;
996 RegCreateKeyA(hRootKey
, szRSABase
, &hKey
);
1001 return hKeyContainer
;
1004 /******************************************************************************
1005 * read_key_container [Internal]
1007 * Tries to read the persistent state of the key container (mainly the signature
1008 * and key exchange private keys) given by pszContainerName.
1011 * pszContainerName [I] Name of the key container to read from the registry
1012 * pVTable [I] Pointer to context data provided by the operating system
1015 * Success: Handle to the key container read from the registry
1016 * Failure: INVALID_HANDLE_VALUE
1018 static HCRYPTPROV
read_key_container(PCHAR pszContainerName
, DWORD dwFlags
, PVTableProvStruc pVTable
)
1020 CHAR szRSABase
[MAX_PATH
];
1022 HKEY hKey
, hRootKey
;
1023 DWORD dwValueType
, dwLen
;
1024 KEYCONTAINER
*pKeyContainer
;
1025 HCRYPTPROV hKeyContainer
;
1027 sprintf(szRSABase
, RSAENH_REGKEY
, pszContainerName
);
1029 if (dwFlags
& CRYPT_MACHINE_KEYSET
) {
1030 hRootKey
= HKEY_LOCAL_MACHINE
;
1032 hRootKey
= HKEY_CURRENT_USER
;
1035 if (RegOpenKeyExA(hRootKey
, szRSABase
, 0, KEY_READ
, &hKey
) != ERROR_SUCCESS
)
1037 SetLastError(NTE_BAD_KEYSET
);
1038 return (HCRYPTPROV
)INVALID_HANDLE_VALUE
;
1041 hKeyContainer
= new_key_container(pszContainerName
, dwFlags
, pVTable
);
1042 if (hKeyContainer
!= (HCRYPTPROV
)INVALID_HANDLE_VALUE
)
1044 if (!lookup_handle(&handle_table
, hKeyContainer
, RSAENH_MAGIC_CONTAINER
,
1045 (OBJECTHDR
**)&pKeyContainer
))
1046 return (HCRYPTPROV
)INVALID_HANDLE_VALUE
;
1048 if (RegQueryValueExA(hKey
, "KeyExchangeKeyPair", 0, &dwValueType
, NULL
, &dwLen
) ==
1051 pbKey
= (BYTE
*)HeapAlloc(GetProcessHeap(), 0, dwLen
);
1054 if (RegQueryValueExA(hKey
, "KeyExchangeKeyPair", 0, &dwValueType
, pbKey
, &dwLen
) ==
1057 RSAENH_CPImportKey(hKeyContainer
, pbKey
, dwLen
, 0, 0,
1058 &pKeyContainer
->hKeyExchangeKeyPair
);
1060 HeapFree(GetProcessHeap(), 0, pbKey
);
1064 if (RegQueryValueExA(hKey
, "SignatureKeyPair", 0, &dwValueType
, NULL
, &dwLen
) ==
1067 pbKey
= (BYTE
*)HeapAlloc(GetProcessHeap(), 0, dwLen
);
1070 if (RegQueryValueExA(hKey
, "SignatureKeyPair", 0, &dwValueType
, pbKey
, &dwLen
) ==
1073 RSAENH_CPImportKey(hKeyContainer
, pbKey
, dwLen
, 0, 0,
1074 &pKeyContainer
->hSignatureKeyPair
);
1076 HeapFree(GetProcessHeap(), 0, pbKey
);
1081 return hKeyContainer
;
1084 /******************************************************************************
1085 * build_hash_signature [Internal]
1087 * Builds a padded version of a hash to match the length of the RSA key modulus.
1090 * pbSignature [O] The padded hash object is stored here.
1091 * dwLen [I] Length of the pbSignature buffer.
1092 * aiAlgid [I] Algorithm identifier of the hash to be padded.
1093 * abHashValue [I] The value of the hash object.
1094 * dwHashLen [I] Length of the hash value.
1095 * dwFlags [I] Selection of padding algorithm.
1099 * Failure: FALSE (NTE_BAD_ALGID)
1101 static BOOL
build_hash_signature(BYTE
*pbSignature
, DWORD dwLen
, ALG_ID aiAlgid
,
1102 CONST BYTE
*abHashValue
, DWORD dwHashLen
, DWORD dwFlags
)
1104 /* These prefixes are meant to be concatenated with hash values of the
1105 * respective kind to form a PKCS #7 DigestInfo. */
1106 static const struct tagOIDDescriptor
{
1109 CONST BYTE abOID
[18];
1110 } aOIDDescriptor
[5] = {
1111 { CALG_MD2
, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1112 0x86, 0xf7, 0x0d, 0x02, 0x02, 0x05, 0x00, 0x04, 0x10 } },
1113 { CALG_MD4
, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1114 0x86, 0xf7, 0x0d, 0x02, 0x04, 0x05, 0x00, 0x04, 0x10 } },
1115 { CALG_MD5
, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1116 0x86, 0xf7, 0x0d, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10 } },
1117 { CALG_SHA
, 15, { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03,
1118 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14 } },
1121 DWORD dwIdxOID
, i
, j
;
1123 for (dwIdxOID
= 0; aOIDDescriptor
[dwIdxOID
].aiAlgid
; dwIdxOID
++) {
1124 if (aOIDDescriptor
[dwIdxOID
].aiAlgid
== aiAlgid
) break;
1127 if (!aOIDDescriptor
[dwIdxOID
].aiAlgid
) {
1128 SetLastError(NTE_BAD_ALGID
);
1132 /* Build the padded signature */
1133 if (dwFlags
& CRYPT_X931_FORMAT
) {
1134 pbSignature
[0] = 0x6b;
1135 for (i
=1; i
< dwLen
- dwHashLen
- 3; i
++) {
1136 pbSignature
[i
] = 0xbb;
1138 pbSignature
[i
++] = 0xba;
1139 for (j
=0; j
< dwHashLen
; j
++, i
++) {
1140 pbSignature
[i
] = abHashValue
[j
];
1142 pbSignature
[i
++] = 0x33;
1143 pbSignature
[i
++] = 0xcc;
1145 pbSignature
[0] = 0x00;
1146 pbSignature
[1] = 0x01;
1147 if (dwFlags
& CRYPT_NOHASHOID
) {
1148 for (i
=2; i
< dwLen
- 1 - dwHashLen
; i
++) {
1149 pbSignature
[i
] = 0xff;
1151 pbSignature
[i
++] = 0x00;
1153 for (i
=2; i
< dwLen
- 1 - aOIDDescriptor
[dwIdxOID
].dwLen
- dwHashLen
; i
++) {
1154 pbSignature
[i
] = 0xff;
1156 pbSignature
[i
++] = 0x00;
1157 for (j
=0; j
< aOIDDescriptor
[dwIdxOID
].dwLen
; j
++) {
1158 pbSignature
[i
++] = aOIDDescriptor
[dwIdxOID
].abOID
[j
];
1161 for (j
=0; j
< dwHashLen
; j
++) {
1162 pbSignature
[i
++] = abHashValue
[j
];
1169 /******************************************************************************
1172 * This is an implementation of the 'P_hash' helper function for TLS1's PRF.
1173 * It is used exclusively by tls1_prf. For details see RFC 2246, chapter 5.
1174 * The pseudo random stream generated by this function is exclusive or'ed with
1175 * the data in pbBuffer.
1178 * hHMAC [I] HMAC object, which will be used in pseudo random generation
1179 * pblobSeed [I] Seed value
1180 * pbBuffer [I/O] Pseudo random stream will be xor'ed to the provided data
1181 * dwBufferLen [I] Number of pseudo random bytes desired
1187 static BOOL
tls1_p(HCRYPTHASH hHMAC
, CONST PCRYPT_DATA_BLOB pblobSeed
, PBYTE pbBuffer
, DWORD dwBufferLen
)
1190 BYTE abAi
[RSAENH_MAX_HASH_SIZE
];
1193 if (!lookup_handle(&handle_table
, hHMAC
, RSAENH_MAGIC_HASH
, (OBJECTHDR
**)&pHMAC
)) {
1194 SetLastError(NTE_BAD_HASH
);
1198 /* compute A_1 = HMAC(seed) */
1200 update_hash(pHMAC
, pblobSeed
->pbData
, pblobSeed
->cbData
);
1201 finalize_hash(pHMAC
);
1202 memcpy(abAi
, pHMAC
->abHashValue
, pHMAC
->dwHashSize
);
1205 /* compute HMAC(A_i + seed) */
1207 update_hash(pHMAC
, abAi
, pHMAC
->dwHashSize
);
1208 update_hash(pHMAC
, pblobSeed
->pbData
, pblobSeed
->cbData
);
1209 finalize_hash(pHMAC
);
1211 /* pseudo random stream := CONCAT_{i=1..n} ( HMAC(A_i + seed) ) */
1213 if (i
>= dwBufferLen
) break;
1214 pbBuffer
[i
] ^= pHMAC
->abHashValue
[i
% pHMAC
->dwHashSize
];
1216 } while (i
% pHMAC
->dwHashSize
);
1218 /* compute A_{i+1} = HMAC(A_i) */
1220 update_hash(pHMAC
, abAi
, pHMAC
->dwHashSize
);
1221 finalize_hash(pHMAC
);
1222 memcpy(abAi
, pHMAC
->abHashValue
, pHMAC
->dwHashSize
);
1223 } while (i
< dwBufferLen
);
1228 /******************************************************************************
1229 * tls1_prf [Internal]
1231 * TLS1 pseudo random function as specified in RFC 2246, chapter 5
1234 * hProv [I] Key container used to compute the pseudo random stream
1235 * hSecret [I] Key that holds the (pre-)master secret
1236 * pblobLabel [I] Descriptive label
1237 * pblobSeed [I] Seed value
1238 * pbBuffer [O] Pseudo random numbers will be stored here
1239 * dwBufferLen [I] Number of pseudo random bytes desired
1245 static BOOL
tls1_prf(HCRYPTPROV hProv
, HCRYPTPROV hSecret
, CONST PCRYPT_DATA_BLOB pblobLabel
,
1246 CONST PCRYPT_DATA_BLOB pblobSeed
, PBYTE pbBuffer
, DWORD dwBufferLen
)
1248 HMAC_INFO hmacInfo
= { 0, NULL
, 0, NULL
, 0 };
1249 HCRYPTHASH hHMAC
= (HCRYPTHASH
)INVALID_HANDLE_VALUE
;
1250 HCRYPTKEY hHalfSecret
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
1251 CRYPTKEY
*pHalfSecret
, *pSecret
;
1252 DWORD dwHalfSecretLen
;
1253 BOOL result
= FALSE
;
1254 CRYPT_DATA_BLOB blobLabelSeed
;
1256 TRACE("(hProv=%08lx, hSecret=%08lx, pblobLabel=%p, pblobSeed=%p, pbBuffer=%p, dwBufferLen=%ld)\n",
1257 hProv
, hSecret
, pblobLabel
, pblobSeed
, pbBuffer
, dwBufferLen
);
1259 if (!lookup_handle(&handle_table
, hSecret
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pSecret
)) {
1260 SetLastError(NTE_FAIL
);
1264 dwHalfSecretLen
= (pSecret
->dwKeyLen
+1)/2;
1266 /* concatenation of the label and the seed */
1267 if (!concat_data_blobs(&blobLabelSeed
, pblobLabel
, pblobSeed
)) goto exit
;
1269 /* zero out the buffer, since two random streams will be xor'ed into it. */
1270 memset(pbBuffer
, 0, dwBufferLen
);
1272 /* build a 'fake' key, to hold the secret. CALG_SSL2_MASTER is used since it provides
1273 * the biggest range of valid key lengths. */
1274 hHalfSecret
= new_key(hProv
, CALG_SSL2_MASTER
, MAKELONG(0,dwHalfSecretLen
*8), &pHalfSecret
);
1275 if (hHalfSecret
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) goto exit
;
1277 /* Derive an HMAC_MD5 hash and call the helper function. */
1278 memcpy(pHalfSecret
->abKeyValue
, pSecret
->abKeyValue
, dwHalfSecretLen
);
1279 if (!RSAENH_CPCreateHash(hProv
, CALG_HMAC
, hHalfSecret
, 0, &hHMAC
)) goto exit
;
1280 hmacInfo
.HashAlgid
= CALG_MD5
;
1281 if (!RSAENH_CPSetHashParam(hProv
, hHMAC
, HP_HMAC_INFO
, (BYTE
*)&hmacInfo
, 0)) goto exit
;
1282 if (!tls1_p(hHMAC
, &blobLabelSeed
, pbBuffer
, dwBufferLen
)) goto exit
;
1284 /* Reconfigure to HMAC_SHA hash and call helper function again. */
1285 memcpy(pHalfSecret
->abKeyValue
, pSecret
->abKeyValue
+ (pSecret
->dwKeyLen
/2), dwHalfSecretLen
);
1286 hmacInfo
.HashAlgid
= CALG_SHA
;
1287 if (!RSAENH_CPSetHashParam(hProv
, hHMAC
, HP_HMAC_INFO
, (BYTE
*)&hmacInfo
, 0)) goto exit
;
1288 if (!tls1_p(hHMAC
, &blobLabelSeed
, pbBuffer
, dwBufferLen
)) goto exit
;
1292 release_handle(&handle_table
, hHalfSecret
, RSAENH_MAGIC_KEY
);
1293 if (hHMAC
!= (HCRYPTHASH
)INVALID_HANDLE_VALUE
) RSAENH_CPDestroyHash(hProv
, hHMAC
);
1294 free_data_blob(&blobLabelSeed
);
1298 /******************************************************************************
1299 * pad_data [Internal]
1301 * Helper function for data padding according to PKCS1 #2
1304 * abData [I] The data to be padded
1305 * dwDataLen [I] Length of the data
1306 * abBuffer [O] Padded data will be stored here
1307 * dwBufferLen [I] Length of the buffer (also length of padded data)
1308 * dwFlags [I] Padding format (CRYPT_SSL2_FALLBACK)
1312 * Failure: FALSE (NTE_BAD_LEN, too much data to pad)
1314 static BOOL
pad_data(CONST BYTE
*abData
, DWORD dwDataLen
, BYTE
*abBuffer
, DWORD dwBufferLen
,
1319 /* Ensure there is enough space for PKCS1 #2 padding */
1320 if (dwDataLen
> dwBufferLen
-11) {
1321 SetLastError(NTE_BAD_LEN
);
1325 memmove(abBuffer
+ dwBufferLen
- dwDataLen
, abData
, dwDataLen
);
1328 abBuffer
[1] = RSAENH_PKC_BLOCKTYPE
;
1329 for (i
=2; i
< dwBufferLen
- dwDataLen
- 1; i
++)
1330 do gen_rand_impl(&abBuffer
[i
], 1); while (!abBuffer
[i
]);
1331 if (dwFlags
& CRYPT_SSL2_FALLBACK
)
1332 for (i
-=8; i
< dwBufferLen
- dwDataLen
- 1; i
++)
1339 /******************************************************************************
1340 * unpad_data [Internal]
1342 * Remove the PKCS1 padding from RSA decrypted data
1345 * abData [I] The padded data
1346 * dwDataLen [I] Length of the padded data
1347 * abBuffer [O] Data without padding will be stored here
1348 * dwBufferLen [I/O] I: Length of the buffer, O: Length of unpadded data
1349 * dwFlags [I] Currently none defined
1353 * Failure: FALSE, (NTE_BAD_DATA, no valid PKCS1 padding or buffer too small)
1355 static BOOL
unpad_data(CONST BYTE
*abData
, DWORD dwDataLen
, BYTE
*abBuffer
, DWORD
*dwBufferLen
,
1360 for (i
=2; i
<dwDataLen
; i
++)
1364 if ((i
== dwDataLen
) || (*dwBufferLen
< dwDataLen
- i
- 1) ||
1365 (abData
[0] != 0x00) || (abData
[1] != RSAENH_PKC_BLOCKTYPE
))
1367 SetLastError(NTE_BAD_DATA
);
1371 *dwBufferLen
= dwDataLen
- i
- 1;
1372 memmove(abBuffer
, abData
+ i
+ 1, *dwBufferLen
);
1376 /******************************************************************************
1377 * CPAcquireContext (RSAENH.@)
1379 * Acquire a handle to the key container specified by pszContainer
1382 * phProv [O] Pointer to the location the acquired handle will be written to.
1383 * pszContainer [I] Name of the desired key container. See Notes
1384 * dwFlags [I] Flags. See Notes.
1385 * pVTable [I] Pointer to a PVTableProvStruct containing callbacks.
1392 * If pszContainer is NULL or points to a zero length string the user's login
1393 * name will be used as the key container name.
1395 * If the CRYPT_NEW_KEYSET flag is set in dwFlags a new keyset will be created.
1396 * If a keyset with the given name already exists, the function fails and sets
1397 * last error to NTE_EXISTS. If CRYPT_NEW_KEYSET is not set and the specified
1398 * key container does not exist, function fails and sets last error to
1401 BOOL WINAPI
RSAENH_CPAcquireContext(HCRYPTPROV
*phProv
, LPSTR pszContainer
,
1402 DWORD dwFlags
, PVTableProvStruc pVTable
)
1405 CHAR szKeyContainerName
[MAX_PATH
] = "";
1406 CHAR szRegKey
[MAX_PATH
];
1408 TRACE("(phProv=%p, pszContainer=%s, dwFlags=%08lx, pVTable=%p)\n", phProv
,
1409 debugstr_a(pszContainer
), dwFlags
, pVTable
);
1411 if (pszContainer
? strlen(pszContainer
) : 0)
1413 strncpy(szKeyContainerName
, pszContainer
, MAX_PATH
);
1414 szKeyContainerName
[MAX_PATH
-1] = '\0';
1419 if (!GetUserNameA(szKeyContainerName
, &dwLen
)) return FALSE
;
1422 switch (dwFlags
& (CRYPT_NEWKEYSET
|CRYPT_VERIFYCONTEXT
|CRYPT_DELETEKEYSET
))
1425 *phProv
= read_key_container(szKeyContainerName
, dwFlags
, pVTable
);
1428 case CRYPT_DELETEKEYSET
:
1429 if (snprintf(szRegKey
, MAX_PATH
, RSAENH_REGKEY
, pszContainer
) >= MAX_PATH
) {
1430 SetLastError(NTE_BAD_KEYSET_PARAM
);
1433 RegDeleteKeyA(HKEY_CURRENT_USER
, szRegKey
);
1434 SetLastError(ERROR_SUCCESS
);
1439 case CRYPT_NEWKEYSET
:
1440 *phProv
= read_key_container(szKeyContainerName
, dwFlags
, pVTable
);
1441 if (*phProv
!= (HCRYPTPROV
)INVALID_HANDLE_VALUE
)
1443 release_handle(&handle_table
, (unsigned int)*phProv
, RSAENH_MAGIC_CONTAINER
);
1444 SetLastError(NTE_EXISTS
);
1447 *phProv
= new_key_container(szKeyContainerName
, dwFlags
, pVTable
);
1450 case CRYPT_VERIFYCONTEXT
:
1452 SetLastError(NTE_BAD_FLAGS
);
1455 *phProv
= new_key_container("", dwFlags
, pVTable
);
1459 *phProv
= (unsigned int)INVALID_HANDLE_VALUE
;
1460 SetLastError(NTE_BAD_FLAGS
);
1464 if (*phProv
!= (unsigned int)INVALID_HANDLE_VALUE
) {
1465 SetLastError(ERROR_SUCCESS
);
1472 /******************************************************************************
1473 * CPCreateHash (RSAENH.@)
1475 * CPCreateHash creates and initalizes a new hash object.
1478 * hProv [I] Handle to the key container to which the new hash will belong.
1479 * Algid [I] Identifies the hash algorithm, which will be used for the hash.
1480 * hKey [I] Handle to a session key applied for keyed hashes.
1481 * dwFlags [I] Currently no flags defined. Must be zero.
1482 * phHash [O] Points to the location where a handle to the new hash will be stored.
1489 * hKey is a handle to a session key applied in keyed hashes like MAC and HMAC.
1490 * If a normal hash object is to be created (like e.g. MD2 or SHA1) hKey must be zero.
1492 BOOL WINAPI
RSAENH_CPCreateHash(HCRYPTPROV hProv
, ALG_ID Algid
, HCRYPTKEY hKey
, DWORD dwFlags
,
1495 CRYPTKEY
*pCryptKey
;
1496 CRYPTHASH
*pCryptHash
;
1497 const PROV_ENUMALGS_EX
*peaAlgidInfo
;
1499 TRACE("(hProv=%08lx, Algid=%08x, hKey=%08lx, dwFlags=%08lx, phHash=%p)\n", hProv
, Algid
, hKey
,
1502 peaAlgidInfo
= get_algid_info(hProv
, Algid
);
1503 if (!peaAlgidInfo
) return FALSE
;
1507 SetLastError(NTE_BAD_FLAGS
);
1511 if (Algid
== CALG_MAC
|| Algid
== CALG_HMAC
|| Algid
== CALG_SCHANNEL_MASTER_HASH
||
1512 Algid
== CALG_TLS1PRF
)
1514 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
)) {
1515 SetLastError(NTE_BAD_KEY
);
1519 if ((Algid
== CALG_MAC
) && (GET_ALG_TYPE(pCryptKey
->aiAlgid
) != ALG_TYPE_BLOCK
)) {
1520 SetLastError(NTE_BAD_KEY
);
1524 if ((Algid
== CALG_SCHANNEL_MASTER_HASH
|| Algid
== CALG_TLS1PRF
) &&
1525 (pCryptKey
->aiAlgid
!= CALG_TLS1_MASTER
))
1527 SetLastError(NTE_BAD_KEY
);
1531 if ((Algid
== CALG_TLS1PRF
) && (pCryptKey
->dwState
!= RSAENH_KEYSTATE_MASTERKEY
)) {
1532 SetLastError(NTE_BAD_KEY_STATE
);
1537 *phHash
= (HCRYPTHASH
)new_object(&handle_table
, sizeof(CRYPTHASH
), RSAENH_MAGIC_HASH
,
1538 destroy_hash
, (OBJECTHDR
**)&pCryptHash
);
1539 if (!pCryptHash
) return FALSE
;
1541 pCryptHash
->aiAlgid
= Algid
;
1542 pCryptHash
->hKey
= hKey
;
1543 pCryptHash
->hProv
= hProv
;
1544 pCryptHash
->dwState
= RSAENH_HASHSTATE_IDLE
;
1545 pCryptHash
->pHMACInfo
= (PHMAC_INFO
)NULL
;
1546 pCryptHash
->dwHashSize
= peaAlgidInfo
->dwDefaultLen
>> 3;
1547 init_data_blob(&pCryptHash
->tpPRFParams
.blobLabel
);
1548 init_data_blob(&pCryptHash
->tpPRFParams
.blobSeed
);
1550 if (Algid
== CALG_SCHANNEL_MASTER_HASH
) {
1551 CRYPT_DATA_BLOB blobRandom
, blobKeyExpansion
= { 13, "key expansion" };
1553 if (pCryptKey
->dwState
!= RSAENH_KEYSTATE_MASTERKEY
) {
1554 CRYPT_DATA_BLOB blobLabel
= { 13, "master secret" };
1555 BYTE abKeyValue
[48];
1557 /* See RFC 2246, chapter 8.1 */
1558 if (!concat_data_blobs(&blobRandom
,
1559 &pCryptKey
->siSChannelInfo
.blobClientRandom
,
1560 &pCryptKey
->siSChannelInfo
.blobServerRandom
))
1564 tls1_prf(hProv
, hKey
, &blobLabel
, &blobRandom
, abKeyValue
, 48);
1565 pCryptKey
->dwState
= RSAENH_KEYSTATE_MASTERKEY
;
1566 memcpy(pCryptKey
->abKeyValue
, abKeyValue
, 48);
1567 free_data_blob(&blobRandom
);
1570 /* See RFC 2246, chapter 6.3 */
1571 if (!concat_data_blobs(&blobRandom
,
1572 &pCryptKey
->siSChannelInfo
.blobServerRandom
,
1573 &pCryptKey
->siSChannelInfo
.blobClientRandom
))
1577 tls1_prf(hProv
, hKey
, &blobKeyExpansion
, &blobRandom
, pCryptHash
->abHashValue
,
1578 RSAENH_MAX_HASH_SIZE
);
1579 free_data_blob(&blobRandom
);
1582 return init_hash(pCryptHash
);
1585 /******************************************************************************
1586 * CPDestroyHash (RSAENH.@)
1588 * Releases the handle to a hash object. The object is destroyed if it's reference
1589 * count reaches zero.
1592 * hProv [I] Handle to the key container to which the hash object belongs.
1593 * hHash [I] Handle to the hash object to be released.
1599 BOOL WINAPI
RSAENH_CPDestroyHash(HCRYPTPROV hProv
, HCRYPTHASH hHash
)
1601 TRACE("(hProv=%08lx, hHash=%08lx)\n", hProv
, hHash
);
1603 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
1605 SetLastError(NTE_BAD_UID
);
1609 if (!release_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
))
1611 SetLastError(NTE_BAD_HASH
);
1618 /******************************************************************************
1619 * CPDestroyKey (RSAENH.@)
1621 * Releases the handle to a key object. The object is destroyed if it's reference
1622 * count reaches zero.
1625 * hProv [I] Handle to the key container to which the key object belongs.
1626 * hKey [I] Handle to the key object to be released.
1632 BOOL WINAPI
RSAENH_CPDestroyKey(HCRYPTPROV hProv
, HCRYPTKEY hKey
)
1634 TRACE("(hProv=%08lx, hKey=%08lx)\n", hProv
, hKey
);
1636 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
1638 SetLastError(NTE_BAD_UID
);
1642 if (!release_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
))
1644 SetLastError(NTE_BAD_KEY
);
1651 /******************************************************************************
1652 * CPDuplicateHash (RSAENH.@)
1654 * Clones a hash object including it's current state.
1657 * hUID [I] Handle to the key container the hash belongs to.
1658 * hHash [I] Handle to the hash object to be cloned.
1659 * pdwReserved [I] Reserved. Must be NULL.
1660 * dwFlags [I] No flags are currently defined. Must be 0.
1661 * phHash [O] Handle to the cloned hash object.
1667 BOOL WINAPI
RSAENH_CPDuplicateHash(HCRYPTPROV hUID
, HCRYPTHASH hHash
, DWORD
*pdwReserved
,
1668 DWORD dwFlags
, HCRYPTHASH
*phHash
)
1670 CRYPTHASH
*pSrcHash
, *pDestHash
;
1672 TRACE("(hUID=%08lx, hHash=%08lx, pdwReserved=%p, dwFlags=%08lx, phHash=%p)\n", hUID
, hHash
,
1673 pdwReserved
, dwFlags
, phHash
);
1675 if (!is_valid_handle(&handle_table
, hUID
, RSAENH_MAGIC_CONTAINER
))
1677 SetLastError(NTE_BAD_UID
);
1681 if (!lookup_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
, (OBJECTHDR
**)&pSrcHash
))
1683 SetLastError(NTE_BAD_HASH
);
1687 if (!phHash
|| pdwReserved
|| dwFlags
)
1689 SetLastError(ERROR_INVALID_PARAMETER
);
1693 *phHash
= (HCRYPTHASH
)new_object(&handle_table
, sizeof(CRYPTHASH
), RSAENH_MAGIC_HASH
,
1694 destroy_hash
, (OBJECTHDR
**)&pDestHash
);
1695 if (*phHash
!= (HCRYPTHASH
)INVALID_HANDLE_VALUE
)
1697 memcpy(pDestHash
, pSrcHash
, sizeof(CRYPTHASH
));
1698 duplicate_hash_impl(pSrcHash
->aiAlgid
, &pSrcHash
->context
, &pDestHash
->context
);
1699 copy_hmac_info(&pDestHash
->pHMACInfo
, pSrcHash
->pHMACInfo
);
1700 copy_data_blob(&pDestHash
->tpPRFParams
.blobLabel
, &pSrcHash
->tpPRFParams
.blobLabel
);
1701 copy_data_blob(&pDestHash
->tpPRFParams
.blobSeed
, &pSrcHash
->tpPRFParams
.blobSeed
);
1704 return *phHash
!= (HCRYPTHASH
)INVALID_HANDLE_VALUE
;
1707 /******************************************************************************
1708 * CPDuplicateKey (RSAENH.@)
1710 * Clones a key object including it's current state.
1713 * hUID [I] Handle to the key container the hash belongs to.
1714 * hKey [I] Handle to the key object to be cloned.
1715 * pdwReserved [I] Reserved. Must be NULL.
1716 * dwFlags [I] No flags are currently defined. Must be 0.
1717 * phHash [O] Handle to the cloned key object.
1723 BOOL WINAPI
RSAENH_CPDuplicateKey(HCRYPTPROV hUID
, HCRYPTKEY hKey
, DWORD
*pdwReserved
,
1724 DWORD dwFlags
, HCRYPTKEY
*phKey
)
1726 CRYPTKEY
*pSrcKey
, *pDestKey
;
1728 TRACE("(hUID=%08lx, hKey=%08lx, pdwReserved=%p, dwFlags=%08lx, phKey=%p)\n", hUID
, hKey
,
1729 pdwReserved
, dwFlags
, phKey
);
1731 if (!is_valid_handle(&handle_table
, hUID
, RSAENH_MAGIC_CONTAINER
))
1733 SetLastError(NTE_BAD_UID
);
1737 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pSrcKey
))
1739 SetLastError(NTE_BAD_KEY
);
1743 if (!phKey
|| pdwReserved
|| dwFlags
)
1745 SetLastError(ERROR_INVALID_PARAMETER
);
1749 *phKey
= (HCRYPTKEY
)new_object(&handle_table
, sizeof(CRYPTKEY
), RSAENH_MAGIC_KEY
, destroy_key
,
1750 (OBJECTHDR
**)&pDestKey
);
1751 if (*phKey
!= (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
1753 memcpy(pDestKey
, pSrcKey
, sizeof(CRYPTKEY
));
1754 copy_data_blob(&pDestKey
->siSChannelInfo
.blobServerRandom
,
1755 &pSrcKey
->siSChannelInfo
.blobServerRandom
);
1756 copy_data_blob(&pDestKey
->siSChannelInfo
.blobClientRandom
,
1757 &pSrcKey
->siSChannelInfo
.blobClientRandom
);
1758 duplicate_key_impl(pSrcKey
->aiAlgid
, &pSrcKey
->context
, &pDestKey
->context
);
1767 /******************************************************************************
1768 * CPEncrypt (RSAENH.@)
1773 * hProv [I] The key container hKey and hHash belong to.
1774 * hKey [I] The key used to encrypt the data.
1775 * hHash [I] An optional hash object for parallel hashing. See notes.
1776 * Final [I] Indicates if this is the last block of data to encrypt.
1777 * dwFlags [I] Currently no flags defined. Must be zero.
1778 * pbData [I/O] Pointer to the data to encrypt. Encrypted data will also be stored there.
1779 * pdwDataLen [I/O] I: Length of data to encrypt, O: Length of encrypted data.
1780 * dwBufLen [I] Size of the buffer at pbData.
1787 * If a hash object handle is provided in hHash, it will be updated with the plaintext.
1788 * This is useful for message signatures.
1790 * This function uses the standard WINAPI protocol for querying data of dynamic length.
1792 BOOL WINAPI
RSAENH_CPEncrypt(HCRYPTPROV hProv
, HCRYPTKEY hKey
, HCRYPTHASH hHash
, BOOL Final
,
1793 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
, DWORD dwBufLen
)
1795 CRYPTKEY
*pCryptKey
;
1796 BYTE
*in
, out
[RSAENH_MAX_BLOCK_SIZE
], o
[RSAENH_MAX_BLOCK_SIZE
];
1797 DWORD dwEncryptedLen
, i
, j
, k
;
1799 TRACE("(hProv=%08lx, hKey=%08lx, hHash=%08lx, Final=%d, dwFlags=%08lx, pbData=%p, "
1800 "pdwDataLen=%p, dwBufLen=%ld)\n", hProv
, hKey
, hHash
, Final
, dwFlags
, pbData
, pdwDataLen
,
1803 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
1805 SetLastError(NTE_BAD_UID
);
1811 SetLastError(NTE_BAD_FLAGS
);
1815 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
1817 SetLastError(NTE_BAD_KEY
);
1821 if (pCryptKey
->dwState
== RSAENH_KEYSTATE_IDLE
)
1822 pCryptKey
->dwState
= RSAENH_KEYSTATE_ENCRYPTING
;
1824 if (pCryptKey
->dwState
!= RSAENH_KEYSTATE_ENCRYPTING
)
1826 SetLastError(NTE_BAD_DATA
);
1830 if (is_valid_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
)) {
1831 if (!RSAENH_CPHashData(hProv
, hHash
, pbData
, *pdwDataLen
, 0)) return FALSE
;
1834 if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_BLOCK
) {
1835 if (!Final
&& (*pdwDataLen
% pCryptKey
->dwBlockLen
)) {
1836 SetLastError(NTE_BAD_DATA
);
1840 dwEncryptedLen
= (*pdwDataLen
/pCryptKey
->dwBlockLen
+(Final
?1:0))*pCryptKey
->dwBlockLen
;
1841 for (i
=*pdwDataLen
; i
<dwEncryptedLen
; i
++) pbData
[i
] = dwEncryptedLen
- *pdwDataLen
;
1842 *pdwDataLen
= dwEncryptedLen
;
1844 if (*pdwDataLen
> dwBufLen
)
1846 SetLastError(ERROR_MORE_DATA
);
1850 for (i
=0, in
=pbData
; i
<*pdwDataLen
; i
+=pCryptKey
->dwBlockLen
, in
+=pCryptKey
->dwBlockLen
) {
1851 switch (pCryptKey
->dwMode
) {
1852 case CRYPT_MODE_ECB
:
1853 encrypt_block_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, in
, out
,
1857 case CRYPT_MODE_CBC
:
1858 for (j
=0; j
<pCryptKey
->dwBlockLen
; j
++) in
[j
] ^= pCryptKey
->abChainVector
[j
];
1859 encrypt_block_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, in
, out
,
1861 memcpy(pCryptKey
->abChainVector
, out
, pCryptKey
->dwBlockLen
);
1864 case CRYPT_MODE_CFB
:
1865 for (j
=0; j
<pCryptKey
->dwBlockLen
; j
++) {
1866 encrypt_block_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
,
1867 pCryptKey
->abChainVector
, o
, RSAENH_ENCRYPT
);
1868 out
[j
] = in
[j
] ^ o
[0];
1869 for (k
=0; k
<pCryptKey
->dwBlockLen
-1; k
++)
1870 pCryptKey
->abChainVector
[k
] = pCryptKey
->abChainVector
[k
+1];
1871 pCryptKey
->abChainVector
[k
] = out
[j
];
1876 SetLastError(NTE_BAD_ALGID
);
1879 memcpy(in
, out
, pCryptKey
->dwBlockLen
);
1881 } else if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_STREAM
) {
1882 encrypt_stream_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pbData
, *pdwDataLen
);
1883 } else if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_RSA
) {
1884 if (pCryptKey
->aiAlgid
== CALG_RSA_SIGN
) {
1885 SetLastError(NTE_BAD_KEY
);
1888 if (dwBufLen
< pCryptKey
->dwBlockLen
) {
1889 SetLastError(ERROR_MORE_DATA
);
1892 if (!pad_data(pbData
, *pdwDataLen
, pbData
, pCryptKey
->dwBlockLen
, dwFlags
)) return FALSE
;
1893 encrypt_block_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pbData
, pbData
, RSAENH_ENCRYPT
);
1894 *pdwDataLen
= pCryptKey
->dwBlockLen
;
1897 SetLastError(NTE_BAD_TYPE
);
1901 if (Final
) setup_key(pCryptKey
);
1906 /******************************************************************************
1907 * CPDecrypt (RSAENH.@)
1912 * hProv [I] The key container hKey and hHash belong to.
1913 * hKey [I] The key used to decrypt the data.
1914 * hHash [I] An optional hash object for parallel hashing. See notes.
1915 * Final [I] Indicates if this is the last block of data to decrypt.
1916 * dwFlags [I] Currently no flags defined. Must be zero.
1917 * pbData [I/O] Pointer to the data to decrypt. Plaintext will also be stored there.
1918 * pdwDataLen [I/O] I: Length of ciphertext, O: Length of plaintext.
1925 * If a hash object handle is provided in hHash, it will be updated with the plaintext.
1926 * This is useful for message signatures.
1928 * This function uses the standard WINAPI protocol for querying data of dynamic length.
1930 BOOL WINAPI
RSAENH_CPDecrypt(HCRYPTPROV hProv
, HCRYPTKEY hKey
, HCRYPTHASH hHash
, BOOL Final
,
1931 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
)
1933 CRYPTKEY
*pCryptKey
;
1934 BYTE
*in
, out
[RSAENH_MAX_BLOCK_SIZE
], o
[RSAENH_MAX_BLOCK_SIZE
];
1937 TRACE("(hProv=%08lx, hKey=%08lx, hHash=%08lx, Final=%d, dwFlags=%08lx, pbData=%p, "
1938 "pdwDataLen=%p)\n", hProv
, hKey
, hHash
, Final
, dwFlags
, pbData
, pdwDataLen
);
1940 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
1942 SetLastError(NTE_BAD_UID
);
1948 SetLastError(NTE_BAD_FLAGS
);
1952 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
1954 SetLastError(NTE_BAD_KEY
);
1958 if (pCryptKey
->dwState
== RSAENH_KEYSTATE_IDLE
)
1959 pCryptKey
->dwState
= RSAENH_KEYSTATE_DECRYPTING
;
1961 if (pCryptKey
->dwState
!= RSAENH_KEYSTATE_DECRYPTING
)
1963 SetLastError(NTE_BAD_DATA
);
1967 if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_BLOCK
) {
1968 for (i
=0, in
=pbData
; i
<*pdwDataLen
; i
+=pCryptKey
->dwBlockLen
, in
+=pCryptKey
->dwBlockLen
) {
1969 switch (pCryptKey
->dwMode
) {
1970 case CRYPT_MODE_ECB
:
1971 encrypt_block_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, in
, out
,
1975 case CRYPT_MODE_CBC
:
1976 encrypt_block_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, in
, out
,
1978 for (j
=0; j
<pCryptKey
->dwBlockLen
; j
++) out
[j
] ^= pCryptKey
->abChainVector
[j
];
1979 memcpy(pCryptKey
->abChainVector
, in
, pCryptKey
->dwBlockLen
);
1982 case CRYPT_MODE_CFB
:
1983 for (j
=0; j
<pCryptKey
->dwBlockLen
; j
++) {
1984 encrypt_block_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
,
1985 pCryptKey
->abChainVector
, o
, RSAENH_ENCRYPT
);
1986 out
[j
] = in
[j
] ^ o
[0];
1987 for (k
=0; k
<pCryptKey
->dwBlockLen
-1; k
++)
1988 pCryptKey
->abChainVector
[k
] = pCryptKey
->abChainVector
[k
+1];
1989 pCryptKey
->abChainVector
[k
] = in
[j
];
1994 SetLastError(NTE_BAD_ALGID
);
1997 memcpy(in
, out
, pCryptKey
->dwBlockLen
);
1999 if (Final
) *pdwDataLen
-= pbData
[*pdwDataLen
-1];
2001 } else if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_STREAM
) {
2002 encrypt_stream_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pbData
, *pdwDataLen
);
2003 } else if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_RSA
) {
2004 if (pCryptKey
->aiAlgid
== CALG_RSA_SIGN
) {
2005 SetLastError(NTE_BAD_KEY
);
2008 encrypt_block_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pbData
, pbData
, RSAENH_DECRYPT
);
2009 if (!unpad_data(pbData
, pCryptKey
->dwBlockLen
, pbData
, pdwDataLen
, dwFlags
)) return FALSE
;
2012 SetLastError(NTE_BAD_TYPE
);
2016 if (Final
) setup_key(pCryptKey
);
2018 if (is_valid_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
)) {
2019 if (!RSAENH_CPHashData(hProv
, hHash
, pbData
, *pdwDataLen
, 0)) return FALSE
;
2025 /******************************************************************************
2026 * CPExportKey (RSAENH.@)
2028 * Export a key into a binary large object (BLOB).
2031 * hProv [I] Key container from which a key is to be exported.
2032 * hKey [I] Key to be exported.
2033 * hPubKey [I] Key used to encrypt sensitive BLOB data.
2034 * dwBlobType [I] SIMPLEBLOB, PUBLICKEYBLOB or PRIVATEKEYBLOB.
2035 * dwFlags [I] Currently none defined.
2036 * pbData [O] Pointer to a buffer where the BLOB will be written to.
2037 * pdwDataLen [I/O] I: Size of buffer at pbData, O: Size of BLOB
2043 BOOL WINAPI
RSAENH_CPExportKey(HCRYPTPROV hProv
, HCRYPTKEY hKey
, HCRYPTKEY hPubKey
,
2044 DWORD dwBlobType
, DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
)
2046 CRYPTKEY
*pCryptKey
, *pPubKey
;
2047 BLOBHEADER
*pBlobHeader
= (BLOBHEADER
*)pbData
;
2048 RSAPUBKEY
*pRSAPubKey
= (RSAPUBKEY
*)(pBlobHeader
+1);
2049 ALG_ID
*pAlgid
= (ALG_ID
*)(pBlobHeader
+1);
2052 TRACE("(hProv=%08lx, hKey=%08lx, hPubKey=%08lx, dwBlobType=%08lx, dwFlags=%08lx, pbData=%p,"
2053 "pdwDataLen=%p)\n", hProv
, hKey
, hPubKey
, dwBlobType
, dwFlags
, pbData
, pdwDataLen
);
2055 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
2057 SetLastError(NTE_BAD_UID
);
2061 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
2063 SetLastError(NTE_BAD_KEY
);
2067 if (dwFlags
& CRYPT_SSL2_FALLBACK
) {
2068 if (pCryptKey
->aiAlgid
!= CALG_SSL2_MASTER
) {
2069 SetLastError(NTE_BAD_KEY
);
2074 switch ((BYTE
)dwBlobType
)
2077 if (!lookup_handle(&handle_table
, hPubKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pPubKey
)){
2078 SetLastError(NTE_BAD_PUBLIC_KEY
); /* FIXME: error_code? */
2082 if (!(GET_ALG_CLASS(pCryptKey
->aiAlgid
)&(ALG_CLASS_DATA_ENCRYPT
|ALG_CLASS_MSG_ENCRYPT
))) {
2083 SetLastError(NTE_BAD_KEY
); /* FIXME: error code? */
2087 dwDataLen
= sizeof(BLOBHEADER
) + sizeof(ALG_ID
) + pPubKey
->dwBlockLen
;
2089 if (*pdwDataLen
< dwDataLen
) {
2090 SetLastError(ERROR_MORE_DATA
);
2091 *pdwDataLen
= dwDataLen
;
2095 pBlobHeader
->bType
= SIMPLEBLOB
;
2096 pBlobHeader
->bVersion
= CUR_BLOB_VERSION
;
2097 pBlobHeader
->reserved
= 0;
2098 pBlobHeader
->aiKeyAlg
= pCryptKey
->aiAlgid
;
2100 *pAlgid
= pPubKey
->aiAlgid
;
2102 if (!pad_data(pCryptKey
->abKeyValue
, pCryptKey
->dwKeyLen
, (BYTE
*)(pAlgid
+1),
2103 pPubKey
->dwBlockLen
, dwFlags
))
2108 encrypt_block_impl(pPubKey
->aiAlgid
, &pPubKey
->context
, (BYTE
*)(pAlgid
+1),
2109 (BYTE
*)(pAlgid
+1), RSAENH_ENCRYPT
);
2111 *pdwDataLen
= dwDataLen
;
2115 if (is_valid_handle(&handle_table
, hPubKey
, RSAENH_MAGIC_KEY
)) {
2116 SetLastError(NTE_BAD_KEY
); /* FIXME: error code? */
2120 if ((pCryptKey
->aiAlgid
!= CALG_RSA_KEYX
) && (pCryptKey
->aiAlgid
!= CALG_RSA_SIGN
)) {
2121 SetLastError(NTE_BAD_KEY
);
2125 dwDataLen
= sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) + pCryptKey
->dwKeyLen
;
2127 if (*pdwDataLen
< dwDataLen
) {
2128 SetLastError(ERROR_MORE_DATA
);
2129 *pdwDataLen
= dwDataLen
;
2133 pBlobHeader
->bType
= PUBLICKEYBLOB
;
2134 pBlobHeader
->bVersion
= CUR_BLOB_VERSION
;
2135 pBlobHeader
->reserved
= 0;
2136 pBlobHeader
->aiKeyAlg
= pCryptKey
->aiAlgid
;
2138 pRSAPubKey
->magic
= RSAENH_MAGIC_RSA1
;
2139 pRSAPubKey
->bitlen
= pCryptKey
->dwKeyLen
<< 3;
2141 export_public_key_impl((BYTE
*)(pRSAPubKey
+1), &pCryptKey
->context
,
2142 pCryptKey
->dwKeyLen
, &pRSAPubKey
->pubexp
);
2144 *pdwDataLen
= dwDataLen
;
2147 case PRIVATEKEYBLOB
:
2148 if ((pCryptKey
->aiAlgid
!= CALG_RSA_KEYX
) && (pCryptKey
->aiAlgid
!= CALG_RSA_SIGN
)) {
2149 SetLastError(NTE_BAD_KEY
);
2153 dwDataLen
= sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) +
2154 2 * pCryptKey
->dwKeyLen
+ 5 * ((pCryptKey
->dwKeyLen
+ 1) >> 1);
2156 if (*pdwDataLen
< dwDataLen
) {
2157 SetLastError(ERROR_MORE_DATA
);
2158 *pdwDataLen
= dwDataLen
;
2162 pBlobHeader
->bType
= PRIVATEKEYBLOB
;
2163 pBlobHeader
->bVersion
= CUR_BLOB_VERSION
;
2164 pBlobHeader
->reserved
= 0;
2165 pBlobHeader
->aiKeyAlg
= pCryptKey
->aiAlgid
;
2167 pRSAPubKey
->magic
= RSAENH_MAGIC_RSA2
;
2168 pRSAPubKey
->bitlen
= pCryptKey
->dwKeyLen
<< 3;
2170 export_private_key_impl((BYTE
*)(pRSAPubKey
+1), &pCryptKey
->context
,
2171 pCryptKey
->dwKeyLen
, &pRSAPubKey
->pubexp
);
2173 *pdwDataLen
= dwDataLen
;
2177 SetLastError(NTE_BAD_TYPE
); /* FIXME: error code? */
2182 /******************************************************************************
2183 * CPImportKey (RSAENH.@)
2185 * Import a BLOB'ed key into a key container.
2188 * hProv [I] Key container into which the key is to be imported.
2189 * pbData [I] Pointer to a buffer which holds the BLOB.
2190 * dwDataLen [I] Length of data in buffer at pbData.
2191 * hPubKey [I] Key used to decrypt sensitive BLOB data.
2192 * dwFlags [I] Currently none defined.
2193 * phKey [O] Handle to the imported key.
2199 BOOL WINAPI
RSAENH_CPImportKey(HCRYPTPROV hProv
, CONST BYTE
*pbData
, DWORD dwDataLen
,
2200 HCRYPTKEY hPubKey
, DWORD dwFlags
, HCRYPTKEY
*phKey
)
2202 CRYPTKEY
*pCryptKey
, *pPubKey
;
2203 CONST BLOBHEADER
*pBlobHeader
= (CONST BLOBHEADER
*)pbData
;
2204 CONST RSAPUBKEY
*pRSAPubKey
= (CONST RSAPUBKEY
*)(pBlobHeader
+1);
2205 CONST ALG_ID
*pAlgid
= (CONST ALG_ID
*)(pBlobHeader
+1);
2206 CONST BYTE
*pbKeyStream
= (CONST BYTE
*)(pAlgid
+ 1);
2210 TRACE("(hProv=%08lx, pbData=%p, dwDataLen=%ld, hPubKey=%08lx, dwFlags=%08lx, phKey=%p)\n",
2211 hProv
, pbData
, dwDataLen
, hPubKey
, dwFlags
, phKey
);
2213 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
2215 SetLastError(NTE_BAD_UID
);
2219 if (dwDataLen
< sizeof(BLOBHEADER
) ||
2220 pBlobHeader
->bVersion
!= CUR_BLOB_VERSION
||
2221 pBlobHeader
->reserved
!= 0)
2223 SetLastError(NTE_BAD_DATA
);
2227 switch (pBlobHeader
->bType
)
2229 case PRIVATEKEYBLOB
:
2230 if ((dwDataLen
< sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
)) ||
2231 (pRSAPubKey
->magic
!= RSAENH_MAGIC_RSA2
) ||
2232 (dwDataLen
< sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) +
2233 (2 * pRSAPubKey
->bitlen
>> 3) + (5 * ((pRSAPubKey
->bitlen
+8)>>4))))
2235 SetLastError(NTE_BAD_DATA
);
2239 *phKey
= new_key(hProv
, pBlobHeader
->aiKeyAlg
, MAKELONG(0,pRSAPubKey
->bitlen
), &pCryptKey
);
2240 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
2241 setup_key(pCryptKey
);
2242 return import_private_key_impl((CONST BYTE
*)(pRSAPubKey
+1), &pCryptKey
->context
,
2243 pRSAPubKey
->bitlen
/8, pRSAPubKey
->pubexp
);
2246 if ((dwDataLen
< sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
)) ||
2247 (pRSAPubKey
->magic
!= RSAENH_MAGIC_RSA1
) ||
2248 (dwDataLen
< sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) + (pRSAPubKey
->bitlen
>> 3)))
2250 SetLastError(NTE_BAD_DATA
);
2254 *phKey
= new_key(hProv
, pBlobHeader
->aiKeyAlg
, MAKELONG(0,pRSAPubKey
->bitlen
), &pCryptKey
);
2255 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
2256 setup_key(pCryptKey
);
2257 return import_public_key_impl((CONST BYTE
*)(pRSAPubKey
+1), &pCryptKey
->context
,
2258 pRSAPubKey
->bitlen
>> 3, pRSAPubKey
->pubexp
);
2261 if (!lookup_handle(&handle_table
, hPubKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pPubKey
) ||
2262 pPubKey
->aiAlgid
!= CALG_RSA_KEYX
)
2264 SetLastError(NTE_BAD_PUBLIC_KEY
); /* FIXME: error code? */
2268 if (dwDataLen
< sizeof(BLOBHEADER
)+sizeof(ALG_ID
)+pPubKey
->dwBlockLen
)
2270 SetLastError(NTE_BAD_DATA
); /* FIXME: error code */
2274 pbDecrypted
= (BYTE
*)HeapAlloc(GetProcessHeap(), 0, pPubKey
->dwBlockLen
);
2275 if (!pbDecrypted
) return FALSE
;
2276 encrypt_block_impl(pPubKey
->aiAlgid
, &pPubKey
->context
, pbKeyStream
, pbDecrypted
,
2279 dwKeyLen
= RSAENH_MAX_KEY_SIZE
;
2280 if (!unpad_data(pbDecrypted
, pPubKey
->dwBlockLen
, pbDecrypted
, &dwKeyLen
, dwFlags
)) {
2281 HeapFree(GetProcessHeap(), 0, pbDecrypted
);
2285 *phKey
= new_key(hProv
, pBlobHeader
->aiKeyAlg
, dwKeyLen
<<19, &pCryptKey
);
2286 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
2288 HeapFree(GetProcessHeap(), 0, pbDecrypted
);
2291 memcpy(pCryptKey
->abKeyValue
, pbDecrypted
, dwKeyLen
);
2292 HeapFree(GetProcessHeap(), 0, pbDecrypted
);
2293 setup_key(pCryptKey
);
2297 SetLastError(NTE_BAD_TYPE
); /* FIXME: error code? */
2302 /******************************************************************************
2303 * CPGenKey (RSAENH.@)
2305 * Generate a key in the key container
2308 * hProv [I] Key container for which a key is to be generated.
2309 * Algid [I] Crypto algorithm identifier for the key to be generated.
2310 * dwFlags [I] Upper 16 bits: Binary length of key. Lower 16 bits: Flags. See Notes
2311 * phKey [O] Handle to the generated key.
2318 * Flags currently not considered.
2321 * Private key-exchange- and signature-keys can be generated with Algid AT_KEYEXCHANGE
2322 * and AT_SIGNATURE values.
2324 BOOL WINAPI
RSAENH_CPGenKey(HCRYPTPROV hProv
, ALG_ID Algid
, DWORD dwFlags
, HCRYPTKEY
*phKey
)
2326 KEYCONTAINER
*pKeyContainer
;
2327 CRYPTKEY
*pCryptKey
;
2329 TRACE("(hProv=%08lx, aiAlgid=%d, dwFlags=%08lx, phKey=%p)\n", hProv
, Algid
, dwFlags
, phKey
);
2331 if (!lookup_handle(&handle_table
, (unsigned int)hProv
, RSAENH_MAGIC_CONTAINER
,
2332 (OBJECTHDR
**)&pKeyContainer
))
2334 /* MSDN: hProv not containing valid context handle */
2335 SetLastError(NTE_BAD_UID
);
2343 *phKey
= new_key(hProv
, CALG_RSA_SIGN
, dwFlags
, &pCryptKey
);
2345 new_key_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pCryptKey
->dwKeyLen
);
2346 setup_key(pCryptKey
);
2347 if (Algid
== AT_SIGNATURE
) {
2348 RSAENH_CPDestroyKey(hProv
, pKeyContainer
->hSignatureKeyPair
);
2349 copy_handle(&handle_table
, *phKey
, RSAENH_MAGIC_KEY
,
2350 (unsigned int*)&pKeyContainer
->hSignatureKeyPair
);
2355 case AT_KEYEXCHANGE
:
2357 *phKey
= new_key(hProv
, CALG_RSA_KEYX
, dwFlags
, &pCryptKey
);
2359 new_key_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pCryptKey
->dwKeyLen
);
2360 setup_key(pCryptKey
);
2361 if (Algid
== AT_KEYEXCHANGE
) {
2362 RSAENH_CPDestroyKey(hProv
, pKeyContainer
->hKeyExchangeKeyPair
);
2363 copy_handle(&handle_table
, *phKey
, RSAENH_MAGIC_KEY
,
2364 (unsigned int*)&pKeyContainer
->hKeyExchangeKeyPair
);
2374 case CALG_PCT1_MASTER
:
2375 case CALG_SSL2_MASTER
:
2376 case CALG_SSL3_MASTER
:
2377 case CALG_TLS1_MASTER
:
2378 *phKey
= new_key(hProv
, Algid
, dwFlags
, &pCryptKey
);
2380 gen_rand_impl(pCryptKey
->abKeyValue
, RSAENH_MAX_KEY_SIZE
);
2382 case CALG_SSL3_MASTER
:
2383 pCryptKey
->abKeyValue
[0] = RSAENH_SSL3_VERSION_MAJOR
;
2384 pCryptKey
->abKeyValue
[1] = RSAENH_SSL3_VERSION_MINOR
;
2387 case CALG_TLS1_MASTER
:
2388 pCryptKey
->abKeyValue
[0] = RSAENH_TLS1_VERSION_MAJOR
;
2389 pCryptKey
->abKeyValue
[1] = RSAENH_TLS1_VERSION_MINOR
;
2392 setup_key(pCryptKey
);
2397 /* MSDN: Algorithm not supported specified by Algid */
2398 SetLastError(NTE_BAD_ALGID
);
2402 return *phKey
!= (unsigned int)INVALID_HANDLE_VALUE
;
2405 /******************************************************************************
2406 * CPGenRandom (RSAENH.@)
2408 * Generate a random byte stream.
2411 * hProv [I] Key container that is used to generate random bytes.
2412 * dwLen [I] Specifies the number of requested random data bytes.
2413 * pbBuffer [O] Random bytes will be stored here.
2419 BOOL WINAPI
RSAENH_CPGenRandom(HCRYPTPROV hProv
, DWORD dwLen
, BYTE
*pbBuffer
)
2421 TRACE("(hProv=%08lx, dwLen=%ld, pbBuffer=%p)\n", hProv
, dwLen
, pbBuffer
);
2423 if (!is_valid_handle(&handle_table
, (unsigned int)hProv
, RSAENH_MAGIC_CONTAINER
))
2425 /* MSDN: hProv not containing valid context handle */
2426 SetLastError(NTE_BAD_UID
);
2430 return gen_rand_impl(pbBuffer
, dwLen
);
2433 /******************************************************************************
2434 * CPGetHashParam (RSAENH.@)
2436 * Query parameters of an hash object.
2439 * hProv [I] The kea container, which the hash belongs to.
2440 * hHash [I] The hash object that is to be queried.
2441 * dwParam [I] Specifies the parameter that is to be queried.
2442 * pbData [I] Pointer to the buffer where the parameter value will be stored.
2443 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
2444 * dwFlags [I] None currently defined.
2451 * Valid dwParams are: HP_ALGID, HP_HASHSIZE, HP_HASHVALUE. The hash will be
2452 * finalized if HP_HASHVALUE is queried.
2454 BOOL WINAPI
RSAENH_CPGetHashParam(HCRYPTPROV hProv
, HCRYPTHASH hHash
, DWORD dwParam
, BYTE
*pbData
,
2455 DWORD
*pdwDataLen
, DWORD dwFlags
)
2457 CRYPTHASH
*pCryptHash
;
2459 TRACE("(hProv=%08lx, hHash=%08lx, dwParam=%08lx, pbData=%p, pdwDataLen=%p, dwFlags=%08lx)\n",
2460 hProv
, hHash
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
2462 if (!is_valid_handle(&handle_table
, (unsigned int)hProv
, RSAENH_MAGIC_CONTAINER
))
2464 SetLastError(NTE_BAD_UID
);
2470 SetLastError(NTE_BAD_FLAGS
);
2474 if (!lookup_handle(&handle_table
, (unsigned int)hHash
, RSAENH_MAGIC_HASH
,
2475 (OBJECTHDR
**)&pCryptHash
))
2477 SetLastError(NTE_BAD_HASH
);
2483 SetLastError(ERROR_INVALID_PARAMETER
);
2490 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&pCryptHash
->aiAlgid
,
2494 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&pCryptHash
->dwHashSize
,
2498 if (pCryptHash
->aiAlgid
== CALG_TLS1PRF
) {
2499 return tls1_prf(hProv
, pCryptHash
->hKey
, &pCryptHash
->tpPRFParams
.blobLabel
,
2500 &pCryptHash
->tpPRFParams
.blobSeed
, pbData
, *pdwDataLen
);
2503 if (pCryptHash
->dwState
== RSAENH_HASHSTATE_IDLE
) {
2504 SetLastError(NTE_BAD_HASH_STATE
);
2508 if (pbData
&& (pCryptHash
->dwState
!= RSAENH_HASHSTATE_FINISHED
))
2510 finalize_hash(pCryptHash
);
2511 pCryptHash
->dwState
= RSAENH_HASHSTATE_FINISHED
;
2514 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)pCryptHash
->abHashValue
,
2515 pCryptHash
->dwHashSize
);
2518 SetLastError(NTE_BAD_TYPE
);
2523 /******************************************************************************
2524 * CPSetKeyParam (RSAENH.@)
2526 * Set a parameter of a key object
2529 * hProv [I] The key container to which the key belongs.
2530 * hKey [I] The key for which a parameter is to be set.
2531 * dwParam [I] Parameter type. See Notes.
2532 * pbData [I] Pointer to the parameter value.
2533 * dwFlags [I] Currently none defined.
2540 * Defined dwParam types are:
2541 * - KP_MODE: Values MODE_CBC, MODE_ECB, MODE_CFB.
2542 * - KP_MODE_BITS: Shift width for cipher feedback mode. (Currently ignored by MS CSP's)
2543 * - KP_PERMISSIONS: Or'ed combination of CRYPT_ENCRYPT, CRYPT_DECRYPT,
2544 * CRYPT_EXPORT, CRYPT_READ, CRYPT_WRITE, CRYPT_MAC
2545 * - KP_IV: Initialization vector
2547 BOOL WINAPI
RSAENH_CPSetKeyParam(HCRYPTPROV hProv
, HCRYPTKEY hKey
, DWORD dwParam
, BYTE
*pbData
,
2550 CRYPTKEY
*pCryptKey
;
2552 TRACE("(hProv=%08lx, hKey=%08lx, dwParam=%08lx, pbData=%p, dwFlags=%08lx)\n", hProv
, hKey
,
2553 dwParam
, pbData
, dwFlags
);
2555 if (!is_valid_handle(&handle_table
, (unsigned int)hProv
, RSAENH_MAGIC_CONTAINER
))
2557 SetLastError(NTE_BAD_UID
);
2562 SetLastError(NTE_BAD_FLAGS
);
2566 if (!lookup_handle(&handle_table
, (unsigned int)hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
2568 SetLastError(NTE_BAD_KEY
);
2574 pCryptKey
->dwMode
= *(DWORD
*)pbData
;
2578 pCryptKey
->dwModeBits
= *(DWORD
*)pbData
;
2581 case KP_PERMISSIONS
:
2582 pCryptKey
->dwPermissions
= *(DWORD
*)pbData
;
2586 memcpy(pCryptKey
->abInitVector
, pbData
, pCryptKey
->dwBlockLen
);
2589 case KP_SCHANNEL_ALG
:
2590 switch (((PSCHANNEL_ALG
)pbData
)->dwUse
) {
2591 case SCHANNEL_ENC_KEY
:
2592 memcpy(&pCryptKey
->siSChannelInfo
.saEncAlg
, pbData
, sizeof(SCHANNEL_ALG
));
2595 case SCHANNEL_MAC_KEY
:
2596 memcpy(&pCryptKey
->siSChannelInfo
.saMACAlg
, pbData
, sizeof(SCHANNEL_ALG
));
2600 SetLastError(NTE_FAIL
); /* FIXME: error code */
2605 case KP_CLIENT_RANDOM
:
2606 return copy_data_blob(&pCryptKey
->siSChannelInfo
.blobClientRandom
, (PCRYPT_DATA_BLOB
)pbData
);
2608 case KP_SERVER_RANDOM
:
2609 return copy_data_blob(&pCryptKey
->siSChannelInfo
.blobServerRandom
, (PCRYPT_DATA_BLOB
)pbData
);
2612 SetLastError(NTE_BAD_TYPE
);
2617 /******************************************************************************
2618 * CPGetKeyParam (RSAENH.@)
2620 * Query a key parameter.
2623 * hProv [I] The key container, which the key belongs to.
2624 * hHash [I] The key object that is to be queried.
2625 * dwParam [I] Specifies the parameter that is to be queried.
2626 * pbData [I] Pointer to the buffer where the parameter value will be stored.
2627 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
2628 * dwFlags [I] None currently defined.
2635 * Defined dwParam types are:
2636 * - KP_MODE: Values MODE_CBC, MODE_ECB, MODE_CFB.
2637 * - KP_MODE_BITS: Shift width for cipher feedback mode.
2638 * (Currently ignored by MS CSP's - always eight)
2639 * - KP_PERMISSIONS: Or'ed combination of CRYPT_ENCRYPT, CRYPT_DECRYPT,
2640 * CRYPT_EXPORT, CRYPT_READ, CRYPT_WRITE, CRYPT_MAC
2641 * - KP_IV: Initialization vector.
2642 * - KP_KEYLEN: Bitwidth of the key.
2643 * - KP_BLOCKLEN: Size of a block cipher block.
2644 * - KP_SALT: Salt value.
2646 BOOL WINAPI
RSAENH_CPGetKeyParam(HCRYPTPROV hProv
, HCRYPTKEY hKey
, DWORD dwParam
, BYTE
*pbData
,
2647 DWORD
*pdwDataLen
, DWORD dwFlags
)
2649 CRYPTKEY
*pCryptKey
;
2652 TRACE("(hProv=%08lx, hKey=%08lx, dwParam=%08lx, pbData=%p, pdwDataLen=%p dwFlags=%08lx)\n",
2653 hProv
, hKey
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
2655 if (!is_valid_handle(&handle_table
, (unsigned int)hProv
, RSAENH_MAGIC_CONTAINER
))
2657 SetLastError(NTE_BAD_UID
);
2662 SetLastError(NTE_BAD_FLAGS
);
2666 if (!lookup_handle(&handle_table
, (unsigned int)hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
2668 SetLastError(NTE_BAD_KEY
);
2675 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)pCryptKey
->abInitVector
,
2676 pCryptKey
->dwBlockLen
);
2679 return copy_param(pbData
, pdwDataLen
,
2680 (CONST BYTE
*)&pCryptKey
->abKeyValue
[pCryptKey
->dwKeyLen
], pCryptKey
->dwSaltLen
);
2683 dwBitLen
= pCryptKey
->dwKeyLen
<< 3;
2684 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwBitLen
, sizeof(DWORD
));
2687 dwBitLen
= pCryptKey
->dwBlockLen
<< 3;
2688 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwBitLen
, sizeof(DWORD
));
2691 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&pCryptKey
->dwMode
, sizeof(DWORD
));
2694 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&pCryptKey
->dwModeBits
,
2697 case KP_PERMISSIONS
:
2698 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&pCryptKey
->dwPermissions
,
2702 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&pCryptKey
->aiAlgid
, sizeof(DWORD
));
2705 SetLastError(NTE_BAD_TYPE
);
2710 /******************************************************************************
2711 * CPGetProvParam (RSAENH.@)
2713 * Query a CSP parameter.
2716 * hProv [I] The key container that is to be queried.
2717 * dwParam [I] Specifies the parameter that is to be queried.
2718 * pbData [I] Pointer to the buffer where the parameter value will be stored.
2719 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
2720 * dwFlags [I] CRYPT_FIRST: Start enumeration (for PP_ENUMALGS{_EX}).
2726 * Defined dwParam types:
2727 * - PP_CONTAINER: Name of the key container.
2728 * - PP_NAME: Name of the cryptographic service provider.
2729 * - PP_SIG_KEYSIZE_INC: RSA signature keywidth granularity in bits.
2730 * - PP_KEYX_KEYSIZE_INC: RSA key-exchange keywidth granularity in bits.
2731 * - PP_ENUMALGS{_EX}: Query provider capabilities.
2733 BOOL WINAPI
RSAENH_CPGetProvParam(HCRYPTPROV hProv
, DWORD dwParam
, BYTE
*pbData
,
2734 DWORD
*pdwDataLen
, DWORD dwFlags
)
2736 KEYCONTAINER
*pKeyContainer
;
2737 PROV_ENUMALGS provEnumalgs
;
2739 BYTE szRSABase
[MAX_PATH
];
2740 HKEY hKey
, hRootKey
;
2742 /* This is for dwParam 41, which does not seem to be documented
2743 * on MSDN. IE6 SP1 asks for it in the 'About' dialog, however.
2744 * Returning this BLOB seems to satisfy IE. The marked 0x00 seem
2745 * to be 'don't care's. If you know anything more specific about
2746 * provider parameter 41, please report to wine-devel@winehq.org */
2747 static CONST BYTE abWTF
[96] = {
2748 0xb0, 0x25, 0x63, 0x86, 0x9c, 0xab, 0xb6, 0x37,
2749 0xe8, 0x82, /**/0x00,/**/ 0x72, 0x06, 0xb2, /**/0x00,/**/ 0x3b,
2750 0x60, 0x35, /**/0x00,/**/ 0x3b, 0x88, 0xce, /**/0x00,/**/ 0x82,
2751 0xbc, 0x7a, /**/0x00,/**/ 0xb7, 0x4f, 0x7e, /**/0x00,/**/ 0xde,
2752 0x92, 0xf1, /**/0x00,/**/ 0x83, 0xea, 0x5e, /**/0x00,/**/ 0xc8,
2753 0x12, 0x1e, 0xd4, 0x06, 0xf7, 0x66, /**/0x00,/**/ 0x01,
2754 0x29, 0xa4, /**/0x00,/**/ 0xf8, 0x24, 0x0c, /**/0x00,/**/ 0x33,
2755 0x06, 0x80, /**/0x00,/**/ 0x02, 0x46, 0x0b, /**/0x00,/**/ 0x6d,
2756 0x5b, 0xca, /**/0x00,/**/ 0x9a, 0x10, 0xf0, /**/0x00,/**/ 0x05,
2757 0x19, 0xd0, /**/0x00,/**/ 0x2c, 0xf6, 0x27, /**/0x00,/**/ 0xaa,
2758 0x7c, 0x6f, /**/0x00,/**/ 0xb9, 0xd8, 0x72, /**/0x00,/**/ 0x03,
2759 0xf3, 0x81, /**/0x00,/**/ 0xfa, 0xe8, 0x26, /**/0x00,/**/ 0xca
2762 TRACE("(hProv=%08lx, dwParam=%08lx, pbData=%p, pdwDataLen=%p, dwFlags=%08lx)\n",
2763 hProv
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
2765 if (!lookup_handle(&handle_table
, (unsigned int)hProv
, RSAENH_MAGIC_CONTAINER
,
2766 (OBJECTHDR
**)&pKeyContainer
))
2768 /* MSDN: hProv not containing valid context handle */
2769 SetLastError(NTE_BAD_UID
);
2776 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)pKeyContainer
->szName
,
2777 strlen(pKeyContainer
->szName
)+1);
2780 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)pKeyContainer
->szProvName
,
2781 strlen(pKeyContainer
->szProvName
)+1);
2783 case PP_SIG_KEYSIZE_INC
:
2784 case PP_KEYX_KEYSIZE_INC
:
2786 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwTemp
, sizeof(dwTemp
));
2788 case PP_ENUMCONTAINERS
:
2789 if ((dwFlags
& CRYPT_FIRST
) == CRYPT_FIRST
) pKeyContainer
->dwEnumContainersCtr
= 0;
2792 *pdwDataLen
= (DWORD
)MAX_PATH
+ 1;
2796 sprintf(szRSABase
, RSAENH_REGKEY
, "");
2798 if (dwFlags
& CRYPT_MACHINE_KEYSET
) {
2799 hRootKey
= HKEY_LOCAL_MACHINE
;
2801 hRootKey
= HKEY_CURRENT_USER
;
2804 if (RegOpenKeyExA(hRootKey
, szRSABase
, 0, KEY_READ
, &hKey
) != ERROR_SUCCESS
)
2806 SetLastError(ERROR_NO_MORE_ITEMS
);
2810 dwTemp
= *pdwDataLen
;
2811 switch (RegEnumKeyExA(hKey
, pKeyContainer
->dwEnumContainersCtr
, pbData
, &dwTemp
,
2812 NULL
, NULL
, NULL
, NULL
))
2814 case ERROR_MORE_DATA
:
2815 *pdwDataLen
= (DWORD
)MAX_PATH
+ 1;
2818 pKeyContainer
->dwEnumContainersCtr
++;
2822 case ERROR_NO_MORE_ITEMS
:
2824 SetLastError(ERROR_NO_MORE_ITEMS
);
2830 case PP_ENUMALGS_EX
:
2831 if (((pKeyContainer
->dwEnumAlgsCtr
>= RSAENH_MAX_ENUMALGS
-1) ||
2832 (!aProvEnumAlgsEx
[pKeyContainer
->dwPersonality
]
2833 [pKeyContainer
->dwEnumAlgsCtr
+1].aiAlgid
)) &&
2834 ((dwFlags
& CRYPT_FIRST
) != CRYPT_FIRST
))
2836 SetLastError(ERROR_NO_MORE_ITEMS
);
2840 if (dwParam
== PP_ENUMALGS
) {
2841 if (pbData
&& (*pdwDataLen
>= sizeof(PROV_ENUMALGS
)))
2842 pKeyContainer
->dwEnumAlgsCtr
= ((dwFlags
& CRYPT_FIRST
) == CRYPT_FIRST
) ?
2843 0 : pKeyContainer
->dwEnumAlgsCtr
+1;
2845 provEnumalgs
.aiAlgid
= aProvEnumAlgsEx
2846 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
].aiAlgid
;
2847 provEnumalgs
.dwBitLen
= aProvEnumAlgsEx
2848 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
].dwDefaultLen
;
2849 provEnumalgs
.dwNameLen
= aProvEnumAlgsEx
2850 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
].dwNameLen
;
2851 memcpy(provEnumalgs
.szName
, aProvEnumAlgsEx
2852 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
].szName
,
2855 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&provEnumalgs
,
2856 sizeof(PROV_ENUMALGS
));
2858 if (pbData
&& (*pdwDataLen
>= sizeof(PROV_ENUMALGS_EX
)))
2859 pKeyContainer
->dwEnumAlgsCtr
= ((dwFlags
& CRYPT_FIRST
) == CRYPT_FIRST
) ?
2860 0 : pKeyContainer
->dwEnumAlgsCtr
+1;
2862 return copy_param(pbData
, pdwDataLen
,
2863 (CONST BYTE
*)&aProvEnumAlgsEx
2864 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
],
2865 sizeof(PROV_ENUMALGS_EX
));
2868 case 41: /* Undocumented. Asked for by IE About dialog */
2869 return copy_param(pbData
, pdwDataLen
, abWTF
, sizeof(abWTF
));
2872 /* MSDN: Unknown parameter number in dwParam */
2873 SetLastError(NTE_BAD_TYPE
);
2878 /******************************************************************************
2879 * CPDeriveKey (RSAENH.@)
2881 * Derives a key from a hash value.
2884 * hProv [I] Key container for which a key is to be generated.
2885 * Algid [I] Crypto algorithm identifier for the key to be generated.
2886 * hBaseData [I] Hash from whose value the key will be derived.
2887 * dwFlags [I] See Notes.
2888 * phKey [O] The generated key.
2896 * - CRYPT_EXPORTABLE: Key can be exported.
2897 * - CRYPT_NO_SALT: No salt is used for 40 bit keys.
2898 * - CRYPT_CREATE_SALT: Use remaining bits as salt value.
2900 BOOL WINAPI
RSAENH_CPDeriveKey(HCRYPTPROV hProv
, ALG_ID Algid
, HCRYPTHASH hBaseData
,
2901 DWORD dwFlags
, HCRYPTKEY
*phKey
)
2903 CRYPTKEY
*pCryptKey
, *pMasterKey
;
2904 CRYPTHASH
*pCryptHash
;
2905 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
*2];
2908 TRACE("(hProv=%08lx, Algid=%d, hBaseData=%08lx, dwFlags=%08lx phKey=%p)\n", hProv
, Algid
,
2909 hBaseData
, dwFlags
, phKey
);
2911 if (!is_valid_handle(&handle_table
, (unsigned int)hProv
, RSAENH_MAGIC_CONTAINER
))
2913 SetLastError(NTE_BAD_UID
);
2917 if (!lookup_handle(&handle_table
, (unsigned int)hBaseData
, RSAENH_MAGIC_HASH
,
2918 (OBJECTHDR
**)&pCryptHash
))
2920 SetLastError(NTE_BAD_HASH
);
2926 SetLastError(ERROR_INVALID_PARAMETER
);
2930 switch (GET_ALG_CLASS(Algid
))
2932 case ALG_CLASS_DATA_ENCRYPT
:
2933 *phKey
= new_key(hProv
, Algid
, dwFlags
, &pCryptKey
);
2934 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
2937 * We derive the key material from the hash.
2938 * If the hash value is not large enough for the claimed key, we have to construct
2939 * a larger binary value based on the hash. This is documented in MSDN: CryptDeriveKey.
2941 dwLen
= RSAENH_MAX_HASH_SIZE
;
2942 RSAENH_CPGetHashParam(pCryptHash
->hProv
, hBaseData
, HP_HASHVAL
, abHashValue
, &dwLen
, 0);
2944 if (dwLen
< pCryptKey
->dwKeyLen
) {
2945 BYTE pad1
[RSAENH_HMAC_DEF_PAD_LEN
], pad2
[RSAENH_HMAC_DEF_PAD_LEN
];
2946 BYTE old_hashval
[RSAENH_MAX_HASH_SIZE
];
2949 memcpy(old_hashval
, pCryptHash
->abHashValue
, RSAENH_MAX_HASH_SIZE
);
2951 for (i
=0; i
<RSAENH_HMAC_DEF_PAD_LEN
; i
++) {
2952 pad1
[i
] = RSAENH_HMAC_DEF_IPAD_CHAR
^ (i
<dwLen
? abHashValue
[i
] : 0);
2953 pad2
[i
] = RSAENH_HMAC_DEF_OPAD_CHAR
^ (i
<dwLen
? abHashValue
[i
] : 0);
2956 init_hash(pCryptHash
);
2957 update_hash(pCryptHash
, pad1
, RSAENH_HMAC_DEF_PAD_LEN
);
2958 finalize_hash(pCryptHash
);
2959 memcpy(abHashValue
, pCryptHash
->abHashValue
, pCryptHash
->dwHashSize
);
2961 init_hash(pCryptHash
);
2962 update_hash(pCryptHash
, pad2
, RSAENH_HMAC_DEF_PAD_LEN
);
2963 finalize_hash(pCryptHash
);
2964 memcpy(abHashValue
+pCryptHash
->dwHashSize
, pCryptHash
->abHashValue
,
2965 pCryptHash
->dwHashSize
);
2967 memcpy(pCryptHash
->abHashValue
, old_hashval
, RSAENH_MAX_HASH_SIZE
);
2970 memcpy(pCryptKey
->abKeyValue
, abHashValue
,
2971 RSAENH_MIN(pCryptKey
->dwKeyLen
, sizeof(pCryptKey
->abKeyValue
)));
2974 case ALG_CLASS_MSG_ENCRYPT
:
2975 if (!lookup_handle(&handle_table
, pCryptHash
->hKey
, RSAENH_MAGIC_KEY
,
2976 (OBJECTHDR
**)&pMasterKey
))
2978 SetLastError(NTE_FAIL
); /* FIXME error code */
2984 /* See RFC 2246, chapter 6.3 Key calculation */
2985 case CALG_SCHANNEL_ENC_KEY
:
2986 *phKey
= new_key(hProv
, pMasterKey
->siSChannelInfo
.saEncAlg
.Algid
,
2987 MAKELONG(LOWORD(dwFlags
),pMasterKey
->siSChannelInfo
.saEncAlg
.cBits
),
2989 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
2990 memcpy(pCryptKey
->abKeyValue
,
2991 pCryptHash
->abHashValue
+ (
2992 2 * (pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
/ 8) +
2993 ((dwFlags
& CRYPT_SERVER
) ?
2994 (pMasterKey
->siSChannelInfo
.saEncAlg
.cBits
/ 8) : 0)),
2995 pMasterKey
->siSChannelInfo
.saEncAlg
.cBits
/ 8);
2996 memcpy(pCryptKey
->abInitVector
,
2997 pCryptHash
->abHashValue
+ (
2998 2 * (pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
/ 8) +
2999 2 * (pMasterKey
->siSChannelInfo
.saEncAlg
.cBits
/ 8) +
3000 ((dwFlags
& CRYPT_SERVER
) ? pCryptKey
->dwBlockLen
: 0)),
3001 pCryptKey
->dwBlockLen
);
3004 case CALG_SCHANNEL_MAC_KEY
:
3005 *phKey
= new_key(hProv
, Algid
,
3006 MAKELONG(LOWORD(dwFlags
),pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
),
3008 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
3009 memcpy(pCryptKey
->abKeyValue
,
3010 pCryptHash
->abHashValue
+ ((dwFlags
& CRYPT_SERVER
) ?
3011 pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
/ 8 : 0),
3012 pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
/ 8);
3016 SetLastError(NTE_BAD_ALGID
);
3022 SetLastError(NTE_BAD_ALGID
);
3026 setup_key(pCryptKey
);
3030 /******************************************************************************
3031 * CPGetUserKey (RSAENH.@)
3033 * Returns a handle to the user's private key-exchange- or signature-key.
3036 * hProv [I] The key container from which a user key is requested.
3037 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
3038 * phUserKey [O] Handle to the requested key or INVALID_HANDLE_VALUE in case of failure.
3045 * A newly created key container does not contain private user key. Create them with CPGenKey.
3047 BOOL WINAPI
RSAENH_CPGetUserKey(HCRYPTPROV hProv
, DWORD dwKeySpec
, HCRYPTKEY
*phUserKey
)
3049 KEYCONTAINER
*pKeyContainer
;
3051 TRACE("(hProv=%08lx, dwKeySpec=%08lx, phUserKey=%p)\n", hProv
, dwKeySpec
, phUserKey
);
3053 if (!lookup_handle(&handle_table
, (unsigned int)hProv
, RSAENH_MAGIC_CONTAINER
,
3054 (OBJECTHDR
**)&pKeyContainer
))
3056 /* MSDN: hProv not containing valid context handle */
3057 SetLastError(NTE_BAD_UID
);
3063 case AT_KEYEXCHANGE
:
3064 copy_handle(&handle_table
, pKeyContainer
->hKeyExchangeKeyPair
, RSAENH_MAGIC_KEY
,
3065 (unsigned int*)phUserKey
);
3069 copy_handle(&handle_table
, pKeyContainer
->hSignatureKeyPair
, RSAENH_MAGIC_KEY
,
3070 (unsigned int*)phUserKey
);
3074 *phUserKey
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
3077 if (*phUserKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
3079 /* MSDN: dwKeySpec parameter specifies nonexistent key */
3080 SetLastError(NTE_NO_KEY
);
3087 /******************************************************************************
3088 * CPHashData (RSAENH.@)
3090 * Updates a hash object with the given data.
3093 * hProv [I] Key container to which the hash object belongs.
3094 * hHash [I] Hash object which is to be updated.
3095 * pbData [I] Pointer to data with which the hash object is to be updated.
3096 * dwDataLen [I] Length of the data.
3097 * dwFlags [I] Currently none defined.
3104 * The actual hash value is queried with CPGetHashParam, which will finalize
3105 * the hash. Updating a finalized hash will fail with a last error NTE_BAD_HASH_STATE.
3107 BOOL WINAPI
RSAENH_CPHashData(HCRYPTPROV hProv
, HCRYPTHASH hHash
, CONST BYTE
*pbData
,
3108 DWORD dwDataLen
, DWORD dwFlags
)
3110 CRYPTHASH
*pCryptHash
;
3112 TRACE("(hProv=%08lx, hHash=%08lx, pbData=%p, dwDataLen=%ld, dwFlags=%08lx)\n",
3113 hProv
, hHash
, pbData
, dwDataLen
, dwFlags
);
3117 SetLastError(NTE_BAD_FLAGS
);
3121 if (!lookup_handle(&handle_table
, (unsigned int)hHash
, RSAENH_MAGIC_HASH
,
3122 (OBJECTHDR
**)&pCryptHash
))
3124 SetLastError(NTE_BAD_HASH
);
3128 if (!get_algid_info(hProv
, pCryptHash
->aiAlgid
) || pCryptHash
->aiAlgid
== CALG_SSL3_SHAMD5
)
3130 SetLastError(NTE_BAD_ALGID
);
3134 if (pCryptHash
->dwState
== RSAENH_HASHSTATE_IDLE
)
3135 pCryptHash
->dwState
= RSAENH_HASHSTATE_HASHING
;
3137 if (pCryptHash
->dwState
!= RSAENH_HASHSTATE_HASHING
)
3139 SetLastError(NTE_BAD_HASH_STATE
);
3143 update_hash(pCryptHash
, pbData
, dwDataLen
);
3147 /******************************************************************************
3148 * CPHashSessionKey (RSAENH.@)
3150 * Updates a hash object with the binary representation of a symmetric key.
3153 * hProv [I] Key container to which the hash object belongs.
3154 * hHash [I] Hash object which is to be updated.
3155 * hKey [I] The symmetric key, whose binary value will be added to the hash.
3156 * dwFlags [I] CRYPT_LITTLE_ENDIAN, if the binary key value shall be interpreted as little endian.
3162 BOOL WINAPI
RSAENH_CPHashSessionKey(HCRYPTPROV hProv
, HCRYPTHASH hHash
, HCRYPTKEY hKey
,
3165 BYTE abKeyValue
[RSAENH_MAX_KEY_SIZE
], bTemp
;
3169 TRACE("(hProv=%08lx, hHash=%08lx, hKey=%08lx, dwFlags=%08lx)\n", hProv
, hHash
, hKey
, dwFlags
);
3171 if (!lookup_handle(&handle_table
, (unsigned int)hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pKey
) ||
3172 (GET_ALG_CLASS(pKey
->aiAlgid
) != ALG_CLASS_DATA_ENCRYPT
))
3174 SetLastError(NTE_BAD_KEY
);
3178 if (dwFlags
& ~CRYPT_LITTLE_ENDIAN
) {
3179 SetLastError(NTE_BAD_FLAGS
);
3183 memcpy(abKeyValue
, pKey
->abKeyValue
, pKey
->dwKeyLen
);
3184 if (!(dwFlags
& CRYPT_LITTLE_ENDIAN
)) {
3185 for (i
=0; i
<pKey
->dwKeyLen
/2; i
++) {
3186 bTemp
= abKeyValue
[i
];
3187 abKeyValue
[i
] = abKeyValue
[pKey
->dwKeyLen
-i
-1];
3188 abKeyValue
[pKey
->dwKeyLen
-i
-1] = bTemp
;
3192 return RSAENH_CPHashData(hProv
, hHash
, abKeyValue
, pKey
->dwKeyLen
, 0);
3195 /******************************************************************************
3196 * CPReleaseContext (RSAENH.@)
3198 * Release a key container.
3201 * hProv [I] Key container to be released.
3202 * dwFlags [I] Currently none defined.
3208 BOOL WINAPI
RSAENH_CPReleaseContext(HCRYPTPROV hProv
, DWORD dwFlags
)
3210 TRACE("(hProv=%08lx, dwFlags=%08lx)\n", hProv
, dwFlags
);
3212 if (!release_handle(&handle_table
, (unsigned int)hProv
, RSAENH_MAGIC_CONTAINER
))
3214 /* MSDN: hProv not containing valid context handle */
3215 SetLastError(NTE_BAD_UID
);
3220 SetLastError(NTE_BAD_FLAGS
);
3227 /******************************************************************************
3228 * CPSetHashParam (RSAENH.@)
3230 * Set a parameter of a hash object
3233 * hProv [I] The key container to which the key belongs.
3234 * hHash [I] The hash object for which a parameter is to be set.
3235 * dwParam [I] Parameter type. See Notes.
3236 * pbData [I] Pointer to the parameter value.
3237 * dwFlags [I] Currently none defined.
3244 * Currently only the HP_HMAC_INFO dwParam type is defined.
3245 * The HMAC_INFO struct will be deep copied into the hash object.
3246 * See Internet RFC 2104 for details on the HMAC algorithm.
3248 BOOL WINAPI
RSAENH_CPSetHashParam(HCRYPTPROV hProv
, HCRYPTHASH hHash
, DWORD dwParam
,
3249 BYTE
*pbData
, DWORD dwFlags
)
3251 CRYPTHASH
*pCryptHash
;
3252 CRYPTKEY
*pCryptKey
;
3255 TRACE("(hProv=%08lx, hHash=%08lx, dwParam=%08lx, pbData=%p, dwFlags=%08lx)\n",
3256 hProv
, hHash
, dwParam
, pbData
, dwFlags
);
3258 if (!is_valid_handle(&handle_table
, (unsigned int)hProv
, RSAENH_MAGIC_CONTAINER
))
3260 SetLastError(NTE_BAD_UID
);
3265 SetLastError(NTE_BAD_FLAGS
);
3269 if (!lookup_handle(&handle_table
, (unsigned int)hHash
, RSAENH_MAGIC_HASH
,
3270 (OBJECTHDR
**)&pCryptHash
))
3272 SetLastError(NTE_BAD_HASH
);
3278 free_hmac_info(pCryptHash
->pHMACInfo
);
3279 if (!copy_hmac_info(&pCryptHash
->pHMACInfo
, (PHMAC_INFO
)pbData
)) return FALSE
;
3281 if (!lookup_handle(&handle_table
, pCryptHash
->hKey
, RSAENH_MAGIC_KEY
,
3282 (OBJECTHDR
**)&pCryptKey
))
3284 SetLastError(NTE_FAIL
); /* FIXME: correct error code? */
3288 for (i
=0; i
<RSAENH_MIN(pCryptKey
->dwKeyLen
,pCryptHash
->pHMACInfo
->cbInnerString
); i
++) {
3289 pCryptHash
->pHMACInfo
->pbInnerString
[i
] ^= pCryptKey
->abKeyValue
[i
];
3291 for (i
=0; i
<RSAENH_MIN(pCryptKey
->dwKeyLen
,pCryptHash
->pHMACInfo
->cbOuterString
); i
++) {
3292 pCryptHash
->pHMACInfo
->pbOuterString
[i
] ^= pCryptKey
->abKeyValue
[i
];
3295 init_hash(pCryptHash
);
3299 memcpy(pCryptHash
->abHashValue
, pbData
, pCryptHash
->dwHashSize
);
3300 pCryptHash
->dwState
= RSAENH_HASHSTATE_FINISHED
;
3303 case HP_TLS1PRF_SEED
:
3304 return copy_data_blob(&pCryptHash
->tpPRFParams
.blobSeed
, (PCRYPT_DATA_BLOB
)pbData
);
3306 case HP_TLS1PRF_LABEL
:
3307 return copy_data_blob(&pCryptHash
->tpPRFParams
.blobLabel
, (PCRYPT_DATA_BLOB
)pbData
);
3310 SetLastError(NTE_BAD_TYPE
);
3315 /******************************************************************************
3316 * CPSetProvParam (RSAENH.@)
3318 BOOL WINAPI
RSAENH_CPSetProvParam(HCRYPTPROV hProv
, DWORD dwParam
, BYTE
*pbData
, DWORD dwFlags
)
3324 /******************************************************************************
3325 * CPSignHash (RSAENH.@)
3327 * Sign a hash object
3330 * hProv [I] The key container, to which the hash object belongs.
3331 * hHash [I] The hash object to be signed.
3332 * dwKeySpec [I] AT_SIGNATURE or AT_KEYEXCHANGE: Key used to generate the signature.
3333 * sDescription [I] Should be NULL for security reasons.
3334 * dwFlags [I] 0, CRYPT_NOHASHOID or CRYPT_X931_FORMAT: Format of the signature.
3335 * pbSignature [O] Buffer, to which the signature will be stored. May be NULL to query SigLen.
3336 * pdwSigLen [I/O] Size of the buffer (in), Length of the signature (out)
3342 BOOL WINAPI
RSAENH_CPSignHash(HCRYPTPROV hProv
, HCRYPTHASH hHash
, DWORD dwKeySpec
,
3343 LPCWSTR sDescription
, DWORD dwFlags
, BYTE
*pbSignature
,
3346 HCRYPTKEY hCryptKey
;
3347 CRYPTKEY
*pCryptKey
;
3349 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
];
3352 TRACE("(hProv=%08lx, hHash=%08lx, dwKeySpec=%08lx, sDescription=%s, dwFlags=%08lx, "
3353 "pbSignature=%p, pdwSigLen=%p)\n", hProv
, hHash
, dwKeySpec
, debugstr_w(sDescription
),
3354 dwFlags
, pbSignature
, pdwSigLen
);
3356 if (dwFlags
& ~(CRYPT_NOHASHOID
|CRYPT_X931_FORMAT
)) {
3357 SetLastError(NTE_BAD_FLAGS
);
3361 if (!RSAENH_CPGetUserKey(hProv
, dwKeySpec
, &hCryptKey
)) return FALSE
;
3363 if (!lookup_handle(&handle_table
, (unsigned int)hCryptKey
, RSAENH_MAGIC_KEY
,
3364 (OBJECTHDR
**)&pCryptKey
))
3366 SetLastError(NTE_NO_KEY
);
3371 *pdwSigLen
= pCryptKey
->dwKeyLen
;
3374 if (pCryptKey
->dwKeyLen
> *pdwSigLen
)
3376 SetLastError(ERROR_MORE_DATA
);
3377 *pdwSigLen
= pCryptKey
->dwKeyLen
;
3380 *pdwSigLen
= pCryptKey
->dwKeyLen
;
3383 if (!RSAENH_CPHashData(hProv
, hHash
, (CONST BYTE
*)sDescription
,
3384 (DWORD
)lstrlenW(sDescription
)*sizeof(WCHAR
), 0))
3390 dwHashLen
= sizeof(DWORD
);
3391 if (!RSAENH_CPGetHashParam(hProv
, hHash
, HP_ALGID
, (BYTE
*)&aiAlgid
, &dwHashLen
, 0)) return FALSE
;
3393 dwHashLen
= RSAENH_MAX_HASH_SIZE
;
3394 if (!RSAENH_CPGetHashParam(hProv
, hHash
, HP_HASHVAL
, abHashValue
, &dwHashLen
, 0)) return FALSE
;
3397 if (!build_hash_signature(pbSignature
, *pdwSigLen
, aiAlgid
, abHashValue
, dwHashLen
, dwFlags
)) {
3401 return encrypt_block_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pbSignature
, pbSignature
, RSAENH_ENCRYPT
);
3404 /******************************************************************************
3405 * CPVerifySignature (RSAENH.@)
3407 * Verify the signature of a hash object.
3410 * hProv [I] The key container, to which the hash belongs.
3411 * hHash [I] The hash for which the signature is verified.
3412 * pbSignature [I] The binary signature.
3413 * dwSigLen [I] Length of the signature BLOB.
3414 * hPubKey [I] Public key used to verify the signature.
3415 * sDescription [I] Should be NULL for security reasons.
3416 * dwFlags [I] 0, CRYPT_NOHASHOID or CRYPT_X931_FORMAT: Format of the signature.
3419 * Success: TRUE (Signature is valid)
3420 * Failure: FALSE (GetLastError() == NTE_BAD_SIGNATURE, if signature is invalid)
3422 BOOL WINAPI
RSAENH_CPVerifySignature(HCRYPTPROV hProv
, HCRYPTHASH hHash
, CONST BYTE
*pbSignature
,
3423 DWORD dwSigLen
, HCRYPTKEY hPubKey
, LPCWSTR sDescription
,
3426 BYTE
*pbConstructed
= NULL
, *pbDecrypted
= NULL
;
3427 CRYPTKEY
*pCryptKey
;
3430 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
];
3433 TRACE("(hProv=%08lx, hHash=%08lx, pbSignature=%p, dwSigLen=%ld, hPubKey=%08lx, sDescription=%s, "
3434 "dwFlags=%08lx)\n", hProv
, hHash
, pbSignature
, dwSigLen
, hPubKey
, debugstr_w(sDescription
),
3437 if (dwFlags
& ~(CRYPT_NOHASHOID
|CRYPT_X931_FORMAT
)) {
3438 SetLastError(NTE_BAD_FLAGS
);
3442 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
3444 SetLastError(NTE_BAD_UID
);
3448 if (!lookup_handle(&handle_table
, (unsigned int)hPubKey
, RSAENH_MAGIC_KEY
,
3449 (OBJECTHDR
**)&pCryptKey
))
3451 SetLastError(NTE_BAD_KEY
);
3456 if (!RSAENH_CPHashData(hProv
, hHash
, (CONST BYTE
*)sDescription
,
3457 (DWORD
)lstrlenW(sDescription
)*sizeof(WCHAR
), 0))
3463 dwHashLen
= sizeof(DWORD
);
3464 if (!RSAENH_CPGetHashParam(hProv
, hHash
, HP_ALGID
, (BYTE
*)&aiAlgid
, &dwHashLen
, 0)) return FALSE
;
3466 dwHashLen
= RSAENH_MAX_HASH_SIZE
;
3467 if (!RSAENH_CPGetHashParam(hProv
, hHash
, HP_HASHVAL
, abHashValue
, &dwHashLen
, 0)) return FALSE
;
3469 pbConstructed
= HeapAlloc(GetProcessHeap(), 0, dwSigLen
);
3470 if (!pbConstructed
) {
3471 SetLastError(NTE_NO_MEMORY
);
3475 pbDecrypted
= HeapAlloc(GetProcessHeap(), 0, dwSigLen
);
3477 SetLastError(NTE_NO_MEMORY
);
3481 if (!encrypt_block_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pbSignature
, pbDecrypted
,
3487 if (!build_hash_signature(pbConstructed
, dwSigLen
, aiAlgid
, abHashValue
, dwHashLen
, dwFlags
)) {
3491 if (memcmp(pbDecrypted
, pbConstructed
, dwSigLen
)) {
3492 SetLastError(NTE_BAD_SIGNATURE
);
3498 HeapFree(GetProcessHeap(), 0, pbConstructed
);
3499 HeapFree(GetProcessHeap(), 0, pbDecrypted
);
3503 static const WCHAR szProviderKeys
[4][97] = {
3504 { 'S','o','f','t','w','a','r','e','\\',
3505 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
3506 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
3507 'i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ','B','a','s',
3508 'e',' ','C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r',
3509 'o','v','i','d','e','r',' ','v','1','.','0',0 },
3510 { 'S','o','f','t','w','a','r','e','\\',
3511 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
3512 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
3513 'i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ',
3514 'E','n','h','a','n','c','e','d',
3515 ' ','C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r',
3516 'o','v','i','d','e','r',' ','v','1','.','0',0 },
3517 { 'S','o','f','t','w','a','r','e','\\',
3518 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
3519 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
3520 'i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ','S','t','r','o','n','g',
3521 ' ','C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r',
3522 'o','v','i','d','e','r',0 },
3523 { 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
3524 'C','r','y','p','t','o','g','r','a','p','h','y','\\','D','e','f','a','u','l','t','s','\\',
3525 'P','r','o','v','i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ',
3526 'R','S','A',' ','S','C','h','a','n','n','e','l',' ',
3527 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r','o','v','i','d','e','r',0 }
3529 static const WCHAR szDefaultKeys
[2][65] = {
3530 { 'S','o','f','t','w','a','r','e','\\',
3531 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
3532 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
3533 'i','d','e','r',' ','T','y','p','e','s','\\','T','y','p','e',' ','0','0','1',0 },
3534 { 'S','o','f','t','w','a','r','e','\\',
3535 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
3536 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
3537 'i','d','e','r',' ','T','y','p','e','s','\\','T','y','p','e',' ','0','1','2',0 }
3541 /******************************************************************************
3542 * DllRegisterServer (RSAENH.@)
3544 * Dll self registration.
3553 * Registers the following keys:
3554 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider\
3555 * Microsoft Base Cryptographic Provider v1.0
3556 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider\
3557 * Microsoft Enhanced Cryptographic Provider
3558 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider\
3559 * Microsoft Strong Cryptographpic Provider
3560 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider Types\Type 001
3562 HRESULT WINAPI
RSAENH_DllRegisterServer()
3569 for (i
=0; i
<4; i
++) {
3570 apiRet
= RegCreateKeyExW(HKEY_LOCAL_MACHINE
, szProviderKeys
[i
], 0, NULL
,
3571 REG_OPTION_NON_VOLATILE
, KEY_ALL_ACCESS
, NULL
, &key
, &dp
);
3573 if (apiRet
== ERROR_SUCCESS
)
3575 if (dp
== REG_CREATED_NEW_KEY
)
3577 static const WCHAR szImagePath
[] = { 'I','m','a','g','e',' ','P','a','t','h',0 };
3578 static const WCHAR szRSABase
[] = { 'r','s','a','e','n','h','.','d','l','l',0 };
3579 static const WCHAR szType
[] = { 'T','y','p','e',0 };
3580 static const WCHAR szSignature
[] = { 'S','i','g','n','a','t','u','r','e',0 };
3581 DWORD type
= (i
== 3) ? PROV_RSA_SCHANNEL
: PROV_RSA_FULL
;
3582 DWORD sign
= 0xdeadbeef;
3583 RegSetValueExW(key
, szImagePath
, 0, REG_SZ
, (LPBYTE
)szRSABase
,
3584 (lstrlenW(szRSABase
) + 1) * sizeof(WCHAR
));
3585 RegSetValueExW(key
, szType
, 0, REG_DWORD
, (LPBYTE
)&type
, sizeof(type
));
3586 RegSetValueExW(key
, szSignature
, 0, REG_BINARY
, (LPBYTE
)&sign
, sizeof(sign
));
3592 for (i
=0; i
<2; i
++) {
3593 apiRet
= RegCreateKeyExW(HKEY_LOCAL_MACHINE
, szDefaultKeys
[i
], 0, NULL
,
3594 REG_OPTION_NON_VOLATILE
, KEY_ALL_ACCESS
, NULL
, &key
, &dp
);
3595 if (apiRet
== ERROR_SUCCESS
)
3597 if (dp
== REG_CREATED_NEW_KEY
)
3599 static const WCHAR szName
[] = { 'N','a','m','e',0 };
3600 static const WCHAR szRSAName
[2][46] = {
3601 { 'M','i','c','r','o','s','o','f','t',' ', 'B','a','s','e',' ',
3602 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ',
3603 'P','r','o','v','i','d','e','r',' ','v','1','.','0',0 },
3604 { 'M','i','c','r','o','s','o','f','t',' ','R','S','A',' ',
3605 'S','C','h','a','n','n','e','l',' ',
3606 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ',
3607 'P','r','o','v','i','d','e','r',0 } };
3608 static const WCHAR szTypeName
[] = { 'T','y','p','e','N','a','m','e',0 };
3609 static const WCHAR szRSATypeName
[2][38] = {
3610 { 'R','S','A',' ','F','u','l','l',' ',
3611 '(','S','i','g','n','a','t','u','r','e',' ','a','n','d',' ',
3612 'K','e','y',' ','E','x','c','h','a','n','g','e',')',0 },
3613 { 'R','S','A',' ','S','C','h','a','n','n','e','l',0 } };
3615 RegSetValueExW(key
, szName
, 0, REG_SZ
, (LPBYTE
)szRSAName
[i
], sizeof(szRSAName
));
3616 RegSetValueExW(key
, szTypeName
, 0, REG_SZ
,
3617 (LPBYTE
)szRSATypeName
[i
],sizeof(szRSATypeName
));
3623 return HRESULT_FROM_WIN32(apiRet
);
3626 /******************************************************************************
3627 * DllUnregisterServer (RSAENH.@)
3629 * Dll self unregistration.
3637 * For the relevant keys see DllRegisterServer.
3639 HRESULT WINAPI
RSAENH_DllUnregisterServer()
3641 RegDeleteKeyW(HKEY_LOCAL_MACHINE
, szProviderKeys
[0]);
3642 RegDeleteKeyW(HKEY_LOCAL_MACHINE
, szProviderKeys
[1]);
3643 RegDeleteKeyW(HKEY_LOCAL_MACHINE
, szProviderKeys
[2]);
3644 RegDeleteKeyW(HKEY_LOCAL_MACHINE
, szProviderKeys
[3]);
3645 RegDeleteKeyW(HKEY_LOCAL_MACHINE
, szDefaultKeys
[0]);
3646 RegDeleteKeyW(HKEY_LOCAL_MACHINE
, szDefaultKeys
[1]);