3 * RSAENH - RSA encryption for Wine
5 * Copyright 2002 TransGaming Technologies (David Hammerton)
6 * Copyright 2004 Mike McCormack for CodeWeavers
7 * Copyright 2004, 2005 Michael Jung
8 * Copyright 2007 Vijay Kiran Kamuju
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #define WIN32_NO_STATUS
27 #define COM_NO_WINDOWS_H
30 //#include "wine/port.h"
31 #include <wine/library.h>
32 #include <wine/debug.h>
38 //#include "winbase.h"
40 //#include "wincrypt.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(crypt
);
48 static HINSTANCE instance
;
50 /******************************************************************************
51 * CRYPTHASH - hash objects
53 #define RSAENH_MAGIC_HASH 0x85938417u
54 #define RSAENH_MAX_HASH_SIZE 104
55 #define RSAENH_HASHSTATE_HASHING 1
56 #define RSAENH_HASHSTATE_FINISHED 2
57 typedef struct _RSAENH_TLS1PRF_PARAMS
59 CRYPT_DATA_BLOB blobLabel
;
60 CRYPT_DATA_BLOB blobSeed
;
61 } RSAENH_TLS1PRF_PARAMS
;
63 typedef struct tagCRYPTHASH
72 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
];
74 RSAENH_TLS1PRF_PARAMS tpPRFParams
;
77 /******************************************************************************
78 * CRYPTKEY - key objects
80 #define RSAENH_MAGIC_KEY 0x73620457u
81 #define RSAENH_MAX_KEY_SIZE 64
82 #define RSAENH_MAX_BLOCK_SIZE 24
83 #define RSAENH_KEYSTATE_IDLE 0
84 #define RSAENH_KEYSTATE_ENCRYPTING 1
85 #define RSAENH_KEYSTATE_MASTERKEY 2
86 typedef struct _RSAENH_SCHANNEL_INFO
88 SCHANNEL_ALG saEncAlg
;
89 SCHANNEL_ALG saMACAlg
;
90 CRYPT_DATA_BLOB blobClientRandom
;
91 CRYPT_DATA_BLOB blobServerRandom
;
92 } RSAENH_SCHANNEL_INFO
;
94 typedef struct tagCRYPTKEY
103 DWORD dwEffectiveKeyLen
;
108 BYTE abKeyValue
[RSAENH_MAX_KEY_SIZE
];
109 BYTE abInitVector
[RSAENH_MAX_BLOCK_SIZE
];
110 BYTE abChainVector
[RSAENH_MAX_BLOCK_SIZE
];
111 RSAENH_SCHANNEL_INFO siSChannelInfo
;
112 CRYPT_DATA_BLOB blobHmacKey
;
115 /******************************************************************************
116 * KEYCONTAINER - key containers
118 #define RSAENH_PERSONALITY_BASE 0u
119 #define RSAENH_PERSONALITY_STRONG 1u
120 #define RSAENH_PERSONALITY_ENHANCED 2u
121 #define RSAENH_PERSONALITY_SCHANNEL 3u
122 #define RSAENH_PERSONALITY_AES 4u
124 #define RSAENH_MAGIC_CONTAINER 0x26384993u
125 typedef struct tagKEYCONTAINER
131 DWORD dwEnumContainersCtr
;
132 CHAR szName
[MAX_PATH
];
133 CHAR szProvName
[MAX_PATH
];
134 HCRYPTKEY hKeyExchangeKeyPair
;
135 HCRYPTKEY hSignatureKeyPair
;
138 /******************************************************************************
139 * Some magic constants
141 #define RSAENH_ENCRYPT 1
142 #define RSAENH_DECRYPT 0
143 #define RSAENH_HMAC_DEF_IPAD_CHAR 0x36
144 #define RSAENH_HMAC_DEF_OPAD_CHAR 0x5c
145 #define RSAENH_HMAC_DEF_PAD_LEN 64
146 #define RSAENH_HMAC_BLOCK_LEN 64
147 #define RSAENH_DES_EFFECTIVE_KEYLEN 56
148 #define RSAENH_DES_STORAGE_KEYLEN 64
149 #define RSAENH_3DES112_EFFECTIVE_KEYLEN 112
150 #define RSAENH_3DES112_STORAGE_KEYLEN 128
151 #define RSAENH_3DES_EFFECTIVE_KEYLEN 168
152 #define RSAENH_3DES_STORAGE_KEYLEN 192
153 #define RSAENH_MAGIC_RSA2 0x32415352
154 #define RSAENH_MAGIC_RSA1 0x31415352
155 #define RSAENH_PKC_BLOCKTYPE 0x02
156 #define RSAENH_SSL3_VERSION_MAJOR 3
157 #define RSAENH_SSL3_VERSION_MINOR 0
158 #define RSAENH_TLS1_VERSION_MAJOR 3
159 #define RSAENH_TLS1_VERSION_MINOR 1
160 #define RSAENH_REGKEY "Software\\Wine\\Crypto\\RSA\\%s"
162 #define RSAENH_MIN(a,b) ((a)<(b)?(a):(b))
163 /******************************************************************************
164 * aProvEnumAlgsEx - Defines the capabilities of the CSP personalities.
166 #define RSAENH_MAX_ENUMALGS 24
167 #define RSAENH_PCT1_SSL2_SSL3_TLS1 (CRYPT_FLAG_PCT1|CRYPT_FLAG_SSL2|CRYPT_FLAG_SSL3|CRYPT_FLAG_TLS1)
168 static const PROV_ENUMALGS_EX aProvEnumAlgsEx
[5][RSAENH_MAX_ENUMALGS
+1] =
171 {CALG_RC2
, 40, 40, 56,0, 4,"RC2", 24,"RSA Data Security's RC2"},
172 {CALG_RC4
, 40, 40, 56,0, 4,"RC4", 24,"RSA Data Security's RC4"},
173 {CALG_DES
, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
174 {CALG_SHA
, 160,160, 160,CRYPT_FLAG_SIGNING
, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
175 {CALG_MD2
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD2", 23,"Message Digest 2 (MD2)"},
176 {CALG_MD4
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD4", 23,"Message Digest 4 (MD4)"},
177 {CALG_MD5
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD5", 23,"Message Digest 5 (MD5)"},
178 {CALG_SSL3_SHAMD5
,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
179 {CALG_MAC
, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
180 {CALG_RSA_SIGN
, 512,384,16384,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_SIGN",14,"RSA Signature"},
181 {CALG_RSA_KEYX
, 512,384, 1024,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_KEYX",17,"RSA Key Exchange"},
182 {CALG_HMAC
, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
183 {0, 0, 0, 0,0, 1,"", 1,""}
186 {CALG_RC2
, 128, 40, 128,0, 4,"RC2", 24,"RSA Data Security's RC2"},
187 {CALG_RC4
, 128, 40, 128,0, 4,"RC4", 24,"RSA Data Security's RC4"},
188 {CALG_DES
, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
189 {CALG_3DES_112
, 112,112, 112,0, 13,"3DES TWO KEY",19,"Two Key Triple DES"},
190 {CALG_3DES
, 168,168, 168,0, 5,"3DES", 21,"Three Key Triple DES"},
191 {CALG_SHA
, 160,160, 160,CRYPT_FLAG_SIGNING
, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
192 {CALG_MD2
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD2", 23,"Message Digest 2 (MD2)"},
193 {CALG_MD4
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD4", 23,"Message Digest 4 (MD4)"},
194 {CALG_MD5
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD5", 23,"Message Digest 5 (MD5)"},
195 {CALG_SSL3_SHAMD5
,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
196 {CALG_MAC
, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
197 {CALG_RSA_SIGN
,1024,384,16384,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_SIGN",14,"RSA Signature"},
198 {CALG_RSA_KEYX
,1024,384,16384,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_KEYX",17,"RSA Key Exchange"},
199 {CALG_HMAC
, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
200 {0, 0, 0, 0,0, 1,"", 1,""}
203 {CALG_RC2
, 128, 40, 128,0, 4,"RC2", 24,"RSA Data Security's RC2"},
204 {CALG_RC4
, 128, 40, 128,0, 4,"RC4", 24,"RSA Data Security's RC4"},
205 {CALG_DES
, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
206 {CALG_3DES_112
, 112,112, 112,0, 13,"3DES TWO KEY",19,"Two Key Triple DES"},
207 {CALG_3DES
, 168,168, 168,0, 5,"3DES", 21,"Three Key Triple DES"},
208 {CALG_SHA
, 160,160, 160,CRYPT_FLAG_SIGNING
, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
209 {CALG_MD2
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD2", 23,"Message Digest 2 (MD2)"},
210 {CALG_MD4
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD4", 23,"Message Digest 4 (MD4)"},
211 {CALG_MD5
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD5", 23,"Message Digest 5 (MD5)"},
212 {CALG_SSL3_SHAMD5
,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
213 {CALG_MAC
, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
214 {CALG_RSA_SIGN
,1024,384,16384,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_SIGN",14,"RSA Signature"},
215 {CALG_RSA_KEYX
,1024,384,16384,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_KEYX",17,"RSA Key Exchange"},
216 {CALG_HMAC
, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
217 {0, 0, 0, 0,0, 1,"", 1,""}
220 {CALG_RC2
, 128, 40, 128,RSAENH_PCT1_SSL2_SSL3_TLS1
, 4,"RC2", 24,"RSA Data Security's RC2"},
221 {CALG_RC4
, 128, 40, 128,RSAENH_PCT1_SSL2_SSL3_TLS1
, 4,"RC4", 24,"RSA Data Security's RC4"},
222 {CALG_DES
, 56, 56, 56,RSAENH_PCT1_SSL2_SSL3_TLS1
, 4,"DES", 31,"Data Encryption Standard (DES)"},
223 {CALG_3DES_112
, 112,112, 112,RSAENH_PCT1_SSL2_SSL3_TLS1
,13,"3DES TWO KEY",19,"Two Key Triple DES"},
224 {CALG_3DES
, 168,168, 168,RSAENH_PCT1_SSL2_SSL3_TLS1
, 5,"3DES", 21,"Three Key Triple DES"},
225 {CALG_SHA
,160,160,160,CRYPT_FLAG_SIGNING
|RSAENH_PCT1_SSL2_SSL3_TLS1
,6,"SHA-1",30,"Secure Hash Algorithm (SHA-1)"},
226 {CALG_MD5
,128,128,128,CRYPT_FLAG_SIGNING
|RSAENH_PCT1_SSL2_SSL3_TLS1
,4,"MD5",23,"Message Digest 5 (MD5)"},
227 {CALG_SSL3_SHAMD5
,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
228 {CALG_MAC
, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
229 {CALG_RSA_SIGN
,1024,384,16384,CRYPT_FLAG_SIGNING
|RSAENH_PCT1_SSL2_SSL3_TLS1
,9,"RSA_SIGN",14,"RSA Signature"},
230 {CALG_RSA_KEYX
,1024,384,16384,CRYPT_FLAG_SIGNING
|RSAENH_PCT1_SSL2_SSL3_TLS1
,9,"RSA_KEYX",17,"RSA Key Exchange"},
231 {CALG_HMAC
, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
232 {CALG_PCT1_MASTER
,128,128,128,CRYPT_FLAG_PCT1
, 12,"PCT1 MASTER",12,"PCT1 Master"},
233 {CALG_SSL2_MASTER
,40,40, 192,CRYPT_FLAG_SSL2
, 12,"SSL2 MASTER",12,"SSL2 Master"},
234 {CALG_SSL3_MASTER
,384,384,384,CRYPT_FLAG_SSL3
, 12,"SSL3 MASTER",12,"SSL3 Master"},
235 {CALG_TLS1_MASTER
,384,384,384,CRYPT_FLAG_TLS1
, 12,"TLS1 MASTER",12,"TLS1 Master"},
236 {CALG_SCHANNEL_MASTER_HASH
,0,0,-1,0, 16,"SCH MASTER HASH",21,"SChannel Master Hash"},
237 {CALG_SCHANNEL_MAC_KEY
,0,0,-1,0, 12,"SCH MAC KEY",17,"SChannel MAC Key"},
238 {CALG_SCHANNEL_ENC_KEY
,0,0,-1,0, 12,"SCH ENC KEY",24,"SChannel Encryption Key"},
239 {CALG_TLS1PRF
, 0, 0, -1,0, 9,"TLS1 PRF", 28,"TLS1 Pseudo Random Function"},
240 {0, 0, 0, 0,0, 1,"", 1,""}
243 {CALG_RC2
, 128, 40, 128,0, 4,"RC2", 24,"RSA Data Security's RC2"},
244 {CALG_RC4
, 128, 40, 128,0, 4,"RC4", 24,"RSA Data Security's RC4"},
245 {CALG_DES
, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
246 {CALG_3DES_112
, 112,112, 112,0, 13,"3DES TWO KEY",19,"Two Key Triple DES"},
247 {CALG_3DES
, 168,168, 168,0, 5,"3DES", 21,"Three Key Triple DES"},
248 {CALG_AES
, 128,128, 128,0, 4,"AES", 35,"Advanced Encryption Standard (AES)"},
249 {CALG_AES_128
, 128,128, 128,0, 8,"AES-128", 39,"Advanced Encryption Standard (AES-128)"},
250 {CALG_AES_192
, 192,192, 192,0, 8,"AES-192", 39,"Advanced Encryption Standard (AES-192)"},
251 {CALG_AES_256
, 256,256, 256,0, 8,"AES-256", 39,"Advanced Encryption Standard (AES-256)"},
252 {CALG_SHA
, 160,160, 160,CRYPT_FLAG_SIGNING
, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
253 {CALG_SHA_256
, 256,256, 256,CRYPT_FLAG_SIGNING
, 6,"SHA-256", 30,"Secure Hash Algorithm (SHA-256)"},
254 {CALG_SHA_384
, 384,384, 384,CRYPT_FLAG_SIGNING
, 6,"SHA-384", 30,"Secure Hash Algorithm (SHA-284)"},
255 {CALG_SHA_512
, 512,512, 512,CRYPT_FLAG_SIGNING
, 6,"SHA-512", 30,"Secure Hash Algorithm (SHA-512)"},
256 {CALG_MD2
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD2", 23,"Message Digest 2 (MD2)"},
257 {CALG_MD4
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD4", 23,"Message Digest 4 (MD4)"},
258 {CALG_MD5
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD5", 23,"Message Digest 5 (MD5)"},
259 {CALG_SSL3_SHAMD5
,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
260 {CALG_MAC
, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
261 {CALG_RSA_SIGN
,1024,384,16384,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_SIGN",14,"RSA Signature"},
262 {CALG_RSA_KEYX
,1024,384,16384,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_KEYX",17,"RSA Key Exchange"},
263 {CALG_HMAC
, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
264 {0, 0, 0, 0,0, 1,"", 1,""}
268 /******************************************************************************
269 * API forward declarations
272 RSAENH_CPGetKeyParam(
303 RSAENH_CPSetHashParam(
307 BYTE
*pbData
, DWORD dwFlags
311 RSAENH_CPGetHashParam(
321 RSAENH_CPDestroyHash(
326 static BOOL
crypt_export_key(
336 static BOOL
import_key(
355 /******************************************************************************
356 * CSP's handle table (used by all acquired key containers)
358 static struct handle_table handle_table
;
360 /******************************************************************************
363 * Initializes and destroys the handle table for the CSP's handles.
365 int WINAPI
DllMain(HINSTANCE hInstance
, DWORD fdwReason
, PVOID pvReserved
)
369 case DLL_PROCESS_ATTACH
:
370 instance
= hInstance
;
371 DisableThreadLibraryCalls(hInstance
);
372 init_handle_table(&handle_table
);
375 case DLL_PROCESS_DETACH
:
376 destroy_handle_table(&handle_table
);
382 /******************************************************************************
383 * copy_param [Internal]
385 * Helper function that supports the standard WINAPI protocol for querying data
389 * pbBuffer [O] Buffer where the queried parameter is copied to, if it is large enough.
390 * May be NUL if the required buffer size is to be queried only.
391 * pdwBufferSize [I/O] In: Size of the buffer at pbBuffer
392 * Out: Size of parameter pbParam
393 * pbParam [I] Parameter value.
394 * dwParamSize [I] Size of pbParam
397 * Success: TRUE (pbParam was copied into pbBuffer or pbBuffer is NULL)
398 * Failure: FALSE (pbBuffer is not large enough to hold pbParam). Last error: ERROR_MORE_DATA
400 static inline BOOL
copy_param(
401 BYTE
*pbBuffer
, DWORD
*pdwBufferSize
, CONST BYTE
*pbParam
, DWORD dwParamSize
)
405 if (dwParamSize
> *pdwBufferSize
)
407 SetLastError(ERROR_MORE_DATA
);
408 *pdwBufferSize
= dwParamSize
;
411 memcpy(pbBuffer
, pbParam
, dwParamSize
);
413 *pdwBufferSize
= dwParamSize
;
417 /******************************************************************************
418 * get_algid_info [Internal]
420 * Query CSP capabilities for a given crypto algorithm.
423 * hProv [I] Handle to a key container of the CSP whose capabilities are to be queried.
424 * algid [I] Identifier of the crypto algorithm about which information is requested.
427 * Success: Pointer to a PROV_ENUMALGS_EX struct containing information about the crypto algorithm.
428 * Failure: NULL (algid not supported)
430 static inline const PROV_ENUMALGS_EX
* get_algid_info(HCRYPTPROV hProv
, ALG_ID algid
) {
431 const PROV_ENUMALGS_EX
*iterator
;
432 KEYCONTAINER
*pKeyContainer
;
434 if (!lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
, (OBJECTHDR
**)&pKeyContainer
)) {
435 SetLastError(NTE_BAD_UID
);
439 for (iterator
= aProvEnumAlgsEx
[pKeyContainer
->dwPersonality
]; iterator
->aiAlgid
; iterator
++) {
440 if (iterator
->aiAlgid
== algid
) return iterator
;
443 SetLastError(NTE_BAD_ALGID
);
447 /******************************************************************************
448 * copy_data_blob [Internal]
450 * deeply copies a DATA_BLOB
453 * dst [O] That's where the blob will be copied to
454 * src [I] Source blob
458 * Failure: FALSE (GetLastError() == NTE_NO_MEMORY
461 * Use free_data_blob to release resources occupied by copy_data_blob.
463 static inline BOOL
copy_data_blob(PCRYPT_DATA_BLOB dst
, CONST PCRYPT_DATA_BLOB src
) {
464 dst
->pbData
= HeapAlloc(GetProcessHeap(), 0, src
->cbData
);
466 SetLastError(NTE_NO_MEMORY
);
469 dst
->cbData
= src
->cbData
;
470 memcpy(dst
->pbData
, src
->pbData
, src
->cbData
);
474 /******************************************************************************
475 * concat_data_blobs [Internal]
477 * Concatenates two blobs
480 * dst [O] The new blob will be copied here
481 * src1 [I] Prefix blob
482 * src2 [I] Appendix blob
486 * Failure: FALSE (GetLastError() == NTE_NO_MEMORY)
489 * Release resources occupied by concat_data_blobs with free_data_blobs
491 static inline BOOL
concat_data_blobs(PCRYPT_DATA_BLOB dst
, CONST PCRYPT_DATA_BLOB src1
,
492 CONST PCRYPT_DATA_BLOB src2
)
494 dst
->cbData
= src1
->cbData
+ src2
->cbData
;
495 dst
->pbData
= HeapAlloc(GetProcessHeap(), 0, dst
->cbData
);
497 SetLastError(NTE_NO_MEMORY
);
500 memcpy(dst
->pbData
, src1
->pbData
, src1
->cbData
);
501 memcpy(dst
->pbData
+ src1
->cbData
, src2
->pbData
, src2
->cbData
);
505 /******************************************************************************
506 * free_data_blob [Internal]
508 * releases resource occupied by a dynamically allocated CRYPT_DATA_BLOB
511 * pBlob [I] Heap space occupied by pBlob->pbData is released
513 static inline void free_data_blob(PCRYPT_DATA_BLOB pBlob
) {
514 HeapFree(GetProcessHeap(), 0, pBlob
->pbData
);
517 /******************************************************************************
518 * init_data_blob [Internal]
520 static inline void init_data_blob(PCRYPT_DATA_BLOB pBlob
) {
521 pBlob
->pbData
= NULL
;
525 /******************************************************************************
526 * free_hmac_info [Internal]
528 * Deeply free an HMAC_INFO struct.
531 * hmac_info [I] Pointer to the HMAC_INFO struct to be freed.
534 * See Internet RFC 2104 for details on the HMAC algorithm.
536 static inline void free_hmac_info(PHMAC_INFO hmac_info
) {
537 if (!hmac_info
) return;
538 HeapFree(GetProcessHeap(), 0, hmac_info
->pbInnerString
);
539 HeapFree(GetProcessHeap(), 0, hmac_info
->pbOuterString
);
540 HeapFree(GetProcessHeap(), 0, hmac_info
);
543 /******************************************************************************
544 * copy_hmac_info [Internal]
546 * Deeply copy an HMAC_INFO struct
549 * dst [O] Pointer to a location where the pointer to the HMAC_INFO copy will be stored.
550 * src [I] Pointer to the HMAC_INFO struct to be copied.
557 * See Internet RFC 2104 for details on the HMAC algorithm.
559 static BOOL
copy_hmac_info(PHMAC_INFO
*dst
, const HMAC_INFO
*src
) {
560 if (!src
) return FALSE
;
561 *dst
= HeapAlloc(GetProcessHeap(), 0, sizeof(HMAC_INFO
));
562 if (!*dst
) return FALSE
;
564 (*dst
)->pbInnerString
= NULL
;
565 (*dst
)->pbOuterString
= NULL
;
566 if ((*dst
)->cbInnerString
== 0) (*dst
)->cbInnerString
= RSAENH_HMAC_DEF_PAD_LEN
;
567 (*dst
)->pbInnerString
= HeapAlloc(GetProcessHeap(), 0, (*dst
)->cbInnerString
);
568 if (!(*dst
)->pbInnerString
) {
569 free_hmac_info(*dst
);
572 if (src
->cbInnerString
)
573 memcpy((*dst
)->pbInnerString
, src
->pbInnerString
, src
->cbInnerString
);
575 memset((*dst
)->pbInnerString
, RSAENH_HMAC_DEF_IPAD_CHAR
, RSAENH_HMAC_DEF_PAD_LEN
);
576 if ((*dst
)->cbOuterString
== 0) (*dst
)->cbOuterString
= RSAENH_HMAC_DEF_PAD_LEN
;
577 (*dst
)->pbOuterString
= HeapAlloc(GetProcessHeap(), 0, (*dst
)->cbOuterString
);
578 if (!(*dst
)->pbOuterString
) {
579 free_hmac_info(*dst
);
582 if (src
->cbOuterString
)
583 memcpy((*dst
)->pbOuterString
, src
->pbOuterString
, src
->cbOuterString
);
585 memset((*dst
)->pbOuterString
, RSAENH_HMAC_DEF_OPAD_CHAR
, RSAENH_HMAC_DEF_PAD_LEN
);
589 /******************************************************************************
590 * destroy_hash [Internal]
592 * Destructor for hash objects
595 * pCryptHash [I] Pointer to the hash object to be destroyed.
596 * Will be invalid after function returns!
598 static void destroy_hash(OBJECTHDR
*pObject
)
600 CRYPTHASH
*pCryptHash
= (CRYPTHASH
*)pObject
;
602 free_hmac_info(pCryptHash
->pHMACInfo
);
603 free_data_blob(&pCryptHash
->tpPRFParams
.blobLabel
);
604 free_data_blob(&pCryptHash
->tpPRFParams
.blobSeed
);
605 HeapFree(GetProcessHeap(), 0, pCryptHash
);
608 /******************************************************************************
609 * init_hash [Internal]
611 * Initialize (or reset) a hash object
614 * pCryptHash [I] The hash object to be initialized.
616 static inline BOOL
init_hash(CRYPTHASH
*pCryptHash
) {
619 switch (pCryptHash
->aiAlgid
)
622 if (pCryptHash
->pHMACInfo
) {
623 const PROV_ENUMALGS_EX
*pAlgInfo
;
625 pAlgInfo
= get_algid_info(pCryptHash
->hProv
, pCryptHash
->pHMACInfo
->HashAlgid
);
626 if (!pAlgInfo
) return FALSE
;
627 pCryptHash
->dwHashSize
= pAlgInfo
->dwDefaultLen
>> 3;
628 init_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
);
629 update_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
630 pCryptHash
->pHMACInfo
->pbInnerString
,
631 pCryptHash
->pHMACInfo
->cbInnerString
);
636 dwLen
= sizeof(DWORD
);
637 RSAENH_CPGetKeyParam(pCryptHash
->hProv
, pCryptHash
->hKey
, KP_BLOCKLEN
,
638 (BYTE
*)&pCryptHash
->dwHashSize
, &dwLen
, 0);
639 pCryptHash
->dwHashSize
>>= 3;
643 return init_hash_impl(pCryptHash
->aiAlgid
, &pCryptHash
->context
);
647 /******************************************************************************
648 * update_hash [Internal]
650 * Hashes the given data and updates the hash object's state accordingly
653 * pCryptHash [I] Hash object to be updated.
654 * pbData [I] Pointer to data stream to be hashed.
655 * dwDataLen [I] Length of data stream.
657 static inline void update_hash(CRYPTHASH
*pCryptHash
, CONST BYTE
*pbData
, DWORD dwDataLen
) {
660 switch (pCryptHash
->aiAlgid
)
663 if (pCryptHash
->pHMACInfo
)
664 update_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
669 pbTemp
= HeapAlloc(GetProcessHeap(), 0, dwDataLen
);
671 memcpy(pbTemp
, pbData
, dwDataLen
);
672 RSAENH_CPEncrypt(pCryptHash
->hProv
, pCryptHash
->hKey
, 0, FALSE
, 0,
673 pbTemp
, &dwDataLen
, dwDataLen
);
674 HeapFree(GetProcessHeap(), 0, pbTemp
);
678 update_hash_impl(pCryptHash
->aiAlgid
, &pCryptHash
->context
, pbData
, dwDataLen
);
682 /******************************************************************************
683 * finalize_hash [Internal]
685 * Finalizes the hash, after all data has been hashed with update_hash.
686 * No additional data can be hashed afterwards until the hash gets initialized again.
689 * pCryptHash [I] Hash object to be finalized.
691 static inline void finalize_hash(CRYPTHASH
*pCryptHash
) {
694 switch (pCryptHash
->aiAlgid
)
697 if (pCryptHash
->pHMACInfo
) {
698 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
];
700 finalize_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
701 pCryptHash
->abHashValue
);
702 memcpy(abHashValue
, pCryptHash
->abHashValue
, pCryptHash
->dwHashSize
);
703 init_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
);
704 update_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
705 pCryptHash
->pHMACInfo
->pbOuterString
,
706 pCryptHash
->pHMACInfo
->cbOuterString
);
707 update_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
708 abHashValue
, pCryptHash
->dwHashSize
);
709 finalize_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
710 pCryptHash
->abHashValue
);
716 RSAENH_CPEncrypt(pCryptHash
->hProv
, pCryptHash
->hKey
, 0, TRUE
, 0,
717 pCryptHash
->abHashValue
, &dwDataLen
, pCryptHash
->dwHashSize
);
721 finalize_hash_impl(pCryptHash
->aiAlgid
, &pCryptHash
->context
, pCryptHash
->abHashValue
);
725 /******************************************************************************
726 * destroy_key [Internal]
728 * Destructor for key objects
731 * pCryptKey [I] Pointer to the key object to be destroyed.
732 * Will be invalid after function returns!
734 static void destroy_key(OBJECTHDR
*pObject
)
736 CRYPTKEY
*pCryptKey
= (CRYPTKEY
*)pObject
;
738 free_key_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
);
739 free_data_blob(&pCryptKey
->siSChannelInfo
.blobClientRandom
);
740 free_data_blob(&pCryptKey
->siSChannelInfo
.blobServerRandom
);
741 free_data_blob(&pCryptKey
->blobHmacKey
);
742 HeapFree(GetProcessHeap(), 0, pCryptKey
);
745 /******************************************************************************
746 * setup_key [Internal]
748 * Initialize (or reset) a key object
751 * pCryptKey [I] The key object to be initialized.
753 static inline void setup_key(CRYPTKEY
*pCryptKey
) {
754 pCryptKey
->dwState
= RSAENH_KEYSTATE_IDLE
;
755 memcpy(pCryptKey
->abChainVector
, pCryptKey
->abInitVector
, sizeof(pCryptKey
->abChainVector
));
756 setup_key_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pCryptKey
->dwKeyLen
,
757 pCryptKey
->dwEffectiveKeyLen
, pCryptKey
->dwSaltLen
,
758 pCryptKey
->abKeyValue
);
761 /******************************************************************************
764 * Creates a new key object without assigning the actual binary key value.
765 * This is done by CPDeriveKey, CPGenKey or CPImportKey, which call this function.
768 * hProv [I] Handle to the provider to which the created key will belong.
769 * aiAlgid [I] The new key shall use the crypto algorithm identified by aiAlgid.
770 * dwFlags [I] Upper 16 bits give the key length.
771 * Lower 16 bits: CRYPT_EXPORTABLE, CRYPT_CREATE_SALT,
773 * ppCryptKey [O] Pointer to the created key
776 * Success: Handle to the created key.
777 * Failure: INVALID_HANDLE_VALUE
779 static HCRYPTKEY
new_key(HCRYPTPROV hProv
, ALG_ID aiAlgid
, DWORD dwFlags
, CRYPTKEY
**ppCryptKey
)
783 DWORD dwKeyLen
= HIWORD(dwFlags
);
784 const PROV_ENUMALGS_EX
*peaAlgidInfo
;
789 * Retrieve the CSP's capabilities for the given ALG_ID value
791 peaAlgidInfo
= get_algid_info(hProv
, aiAlgid
);
792 if (!peaAlgidInfo
) return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
794 TRACE("alg = %s, dwKeyLen = %d\n", debugstr_a(peaAlgidInfo
->szName
),
797 * Assume the default key length, if none is specified explicitly
799 if (dwKeyLen
== 0) dwKeyLen
= peaAlgidInfo
->dwDefaultLen
;
802 * Check if the requested key length is supported by the current CSP.
803 * Adjust key length's for DES algorithms.
807 if (dwKeyLen
== RSAENH_DES_EFFECTIVE_KEYLEN
) {
808 dwKeyLen
= RSAENH_DES_STORAGE_KEYLEN
;
810 if (dwKeyLen
!= RSAENH_DES_STORAGE_KEYLEN
) {
811 SetLastError(NTE_BAD_FLAGS
);
812 return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
817 if (dwKeyLen
== RSAENH_3DES112_EFFECTIVE_KEYLEN
) {
818 dwKeyLen
= RSAENH_3DES112_STORAGE_KEYLEN
;
820 if (dwKeyLen
!= RSAENH_3DES112_STORAGE_KEYLEN
) {
821 SetLastError(NTE_BAD_FLAGS
);
822 return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
827 if (dwKeyLen
== RSAENH_3DES_EFFECTIVE_KEYLEN
) {
828 dwKeyLen
= RSAENH_3DES_STORAGE_KEYLEN
;
830 if (dwKeyLen
!= RSAENH_3DES_STORAGE_KEYLEN
) {
831 SetLastError(NTE_BAD_FLAGS
);
832 return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
837 /* Avoid the key length check for HMAC keys, which have unlimited
844 dwKeyLen
> peaAlgidInfo
->dwMaxLen
||
845 dwKeyLen
< peaAlgidInfo
->dwMinLen
)
847 TRACE("key len %d out of bounds (%d, %d)\n", dwKeyLen
,
848 peaAlgidInfo
->dwMinLen
, peaAlgidInfo
->dwMaxLen
);
849 SetLastError(NTE_BAD_DATA
);
850 return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
854 hCryptKey
= new_object(&handle_table
, sizeof(CRYPTKEY
), RSAENH_MAGIC_KEY
,
855 destroy_key
, (OBJECTHDR
**)&pCryptKey
);
856 if (hCryptKey
!= (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
858 pCryptKey
->aiAlgid
= aiAlgid
;
859 pCryptKey
->hProv
= hProv
;
860 pCryptKey
->dwModeBits
= 0;
861 pCryptKey
->dwPermissions
= CRYPT_ENCRYPT
| CRYPT_DECRYPT
| CRYPT_READ
| CRYPT_WRITE
|
863 if (dwFlags
& CRYPT_EXPORTABLE
)
864 pCryptKey
->dwPermissions
|= CRYPT_EXPORT
;
865 pCryptKey
->dwKeyLen
= dwKeyLen
>> 3;
866 pCryptKey
->dwEffectiveKeyLen
= 0;
867 if ((dwFlags
& CRYPT_CREATE_SALT
) || (dwKeyLen
== 40 && !(dwFlags
& CRYPT_NO_SALT
)))
868 pCryptKey
->dwSaltLen
= 16 /*FIXME*/ - pCryptKey
->dwKeyLen
;
870 pCryptKey
->dwSaltLen
= 0;
871 memset(pCryptKey
->abKeyValue
, 0, sizeof(pCryptKey
->abKeyValue
));
872 memset(pCryptKey
->abInitVector
, 0, sizeof(pCryptKey
->abInitVector
));
873 memset(&pCryptKey
->siSChannelInfo
.saEncAlg
, 0, sizeof(pCryptKey
->siSChannelInfo
.saEncAlg
));
874 memset(&pCryptKey
->siSChannelInfo
.saMACAlg
, 0, sizeof(pCryptKey
->siSChannelInfo
.saMACAlg
));
875 init_data_blob(&pCryptKey
->siSChannelInfo
.blobClientRandom
);
876 init_data_blob(&pCryptKey
->siSChannelInfo
.blobServerRandom
);
877 init_data_blob(&pCryptKey
->blobHmacKey
);
881 case CALG_PCT1_MASTER
:
882 case CALG_SSL2_MASTER
:
883 case CALG_SSL3_MASTER
:
884 case CALG_TLS1_MASTER
:
886 pCryptKey
->dwBlockLen
= 0;
887 pCryptKey
->dwMode
= 0;
894 pCryptKey
->dwBlockLen
= 8;
895 pCryptKey
->dwMode
= CRYPT_MODE_CBC
;
902 pCryptKey
->dwBlockLen
= 16;
903 pCryptKey
->dwMode
= CRYPT_MODE_ECB
;
908 pCryptKey
->dwBlockLen
= dwKeyLen
>> 3;
909 pCryptKey
->dwMode
= 0;
913 pCryptKey
->dwBlockLen
= 0;
914 pCryptKey
->dwMode
= 0;
918 *ppCryptKey
= pCryptKey
;
924 /******************************************************************************
925 * map_key_spec_to_key_pair_name [Internal]
927 * Returns the name of the registry value associated with a key spec.
930 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
933 * Success: Name of registry value.
936 static LPCSTR
map_key_spec_to_key_pair_name(DWORD dwKeySpec
)
943 szValueName
= "KeyExchangeKeyPair";
946 szValueName
= "SignatureKeyPair";
949 WARN("invalid key spec %d\n", dwKeySpec
);
955 /******************************************************************************
956 * store_key_pair [Internal]
958 * Stores a key pair to the registry
961 * hCryptKey [I] Handle to the key to be stored
962 * hKey [I] Registry key where the key pair is to be stored
963 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
964 * dwFlags [I] Flags for protecting the key
966 static void store_key_pair(HCRYPTKEY hCryptKey
, HKEY hKey
, DWORD dwKeySpec
, DWORD dwFlags
)
969 DATA_BLOB blobIn
, blobOut
;
974 if (!(szValueName
= map_key_spec_to_key_pair_name(dwKeySpec
)))
976 if (lookup_handle(&handle_table
, hCryptKey
, RSAENH_MAGIC_KEY
,
979 if (crypt_export_key(pKey
, 0, PRIVATEKEYBLOB
, 0, TRUE
, 0, &dwLen
))
981 pbKey
= HeapAlloc(GetProcessHeap(), 0, dwLen
);
984 if (crypt_export_key(pKey
, 0, PRIVATEKEYBLOB
, 0, TRUE
, pbKey
,
987 blobIn
.pbData
= pbKey
;
988 blobIn
.cbData
= dwLen
;
990 if (CryptProtectData(&blobIn
, NULL
, NULL
, NULL
, NULL
,
993 RegSetValueExA(hKey
, szValueName
, 0, REG_BINARY
,
994 blobOut
.pbData
, blobOut
.cbData
);
995 LocalFree(blobOut
.pbData
);
998 HeapFree(GetProcessHeap(), 0, pbKey
);
1004 /******************************************************************************
1005 * map_key_spec_to_permissions_name [Internal]
1007 * Returns the name of the registry value associated with the permissions for
1011 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
1014 * Success: Name of registry value.
1017 static LPCSTR
map_key_spec_to_permissions_name(DWORD dwKeySpec
)
1023 case AT_KEYEXCHANGE
:
1024 szValueName
= "KeyExchangePermissions";
1027 szValueName
= "SignaturePermissions";
1030 WARN("invalid key spec %d\n", dwKeySpec
);
1036 /******************************************************************************
1037 * store_key_permissions [Internal]
1039 * Stores a key's permissions to the registry
1042 * hCryptKey [I] Handle to the key whose permissions are to be stored
1043 * hKey [I] Registry key where the key permissions are to be stored
1044 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
1046 static void store_key_permissions(HCRYPTKEY hCryptKey
, HKEY hKey
, DWORD dwKeySpec
)
1051 if (!(szValueName
= map_key_spec_to_permissions_name(dwKeySpec
)))
1053 if (lookup_handle(&handle_table
, hCryptKey
, RSAENH_MAGIC_KEY
,
1054 (OBJECTHDR
**)&pKey
))
1055 RegSetValueExA(hKey
, szValueName
, 0, REG_DWORD
,
1056 (BYTE
*)&pKey
->dwPermissions
,
1057 sizeof(pKey
->dwPermissions
));
1060 /******************************************************************************
1061 * create_container_key [Internal]
1063 * Creates the registry key for a key container's persistent storage.
1066 * pKeyContainer [I] Pointer to the key container
1067 * sam [I] Desired registry access
1068 * phKey [O] Returned key
1070 static BOOL
create_container_key(KEYCONTAINER
*pKeyContainer
, REGSAM sam
, HKEY
*phKey
)
1072 CHAR szRSABase
[MAX_PATH
];
1075 sprintf(szRSABase
, RSAENH_REGKEY
, pKeyContainer
->szName
);
1077 if (pKeyContainer
->dwFlags
& CRYPT_MACHINE_KEYSET
)
1078 hRootKey
= HKEY_LOCAL_MACHINE
;
1080 hRootKey
= HKEY_CURRENT_USER
;
1082 /* @@ Wine registry key: HKLM\Software\Wine\Crypto\RSA */
1083 /* @@ Wine registry key: HKCU\Software\Wine\Crypto\RSA */
1084 return RegCreateKeyExA(hRootKey
, szRSABase
, 0, NULL
,
1085 REG_OPTION_NON_VOLATILE
, sam
, NULL
, phKey
, NULL
)
1089 /******************************************************************************
1090 * open_container_key [Internal]
1092 * Opens a key container's persistent storage for reading.
1095 * pszContainerName [I] Name of the container to be opened. May be the empty
1096 * string if the parent key of all containers is to be
1098 * dwFlags [I] Flags indicating which keyset to be opened.
1099 * phKey [O] Returned key
1101 static BOOL
open_container_key(LPCSTR pszContainerName
, DWORD dwFlags
, HKEY
*phKey
)
1103 CHAR szRSABase
[MAX_PATH
];
1106 sprintf(szRSABase
, RSAENH_REGKEY
, pszContainerName
);
1108 if (dwFlags
& CRYPT_MACHINE_KEYSET
)
1109 hRootKey
= HKEY_LOCAL_MACHINE
;
1111 hRootKey
= HKEY_CURRENT_USER
;
1113 /* @@ Wine registry key: HKLM\Software\Wine\Crypto\RSA */
1114 /* @@ Wine registry key: HKCU\Software\Wine\Crypto\RSA */
1115 return RegOpenKeyExA(hRootKey
, szRSABase
, 0, KEY_READ
, phKey
) ==
1119 /******************************************************************************
1120 * delete_container_key [Internal]
1122 * Deletes a key container's persistent storage.
1125 * pszContainerName [I] Name of the container to be opened.
1126 * dwFlags [I] Flags indicating which keyset to be opened.
1128 static BOOL
delete_container_key(LPCSTR pszContainerName
, DWORD dwFlags
)
1130 CHAR szRegKey
[MAX_PATH
];
1132 if (snprintf(szRegKey
, MAX_PATH
, RSAENH_REGKEY
, pszContainerName
) >= MAX_PATH
) {
1133 SetLastError(NTE_BAD_KEYSET_PARAM
);
1137 if (dwFlags
& CRYPT_MACHINE_KEYSET
)
1138 hRootKey
= HKEY_LOCAL_MACHINE
;
1140 hRootKey
= HKEY_CURRENT_USER
;
1141 if (!RegDeleteKeyA(hRootKey
, szRegKey
)) {
1142 SetLastError(ERROR_SUCCESS
);
1145 SetLastError(NTE_BAD_KEYSET
);
1151 /******************************************************************************
1152 * store_key_container_keys [Internal]
1154 * Stores key container's keys in a persistent location.
1157 * pKeyContainer [I] Pointer to the key container whose keys are to be saved
1159 static void store_key_container_keys(KEYCONTAINER
*pKeyContainer
)
1164 /* On WinXP, persistent keys are stored in a file located at:
1165 * $AppData$\\Microsoft\\Crypto\\RSA\\$SID$\\some_hex_string
1168 if (pKeyContainer
->dwFlags
& CRYPT_MACHINE_KEYSET
)
1169 dwFlags
= CRYPTPROTECT_LOCAL_MACHINE
;
1173 if (create_container_key(pKeyContainer
, KEY_WRITE
, &hKey
))
1175 store_key_pair(pKeyContainer
->hKeyExchangeKeyPair
, hKey
,
1176 AT_KEYEXCHANGE
, dwFlags
);
1177 store_key_pair(pKeyContainer
->hSignatureKeyPair
, hKey
,
1178 AT_SIGNATURE
, dwFlags
);
1183 /******************************************************************************
1184 * store_key_container_permissions [Internal]
1186 * Stores key container's key permissions in a persistent location.
1189 * pKeyContainer [I] Pointer to the key container whose key permissions are to
1192 static void store_key_container_permissions(KEYCONTAINER
*pKeyContainer
)
1196 if (create_container_key(pKeyContainer
, KEY_WRITE
, &hKey
))
1198 store_key_permissions(pKeyContainer
->hKeyExchangeKeyPair
, hKey
,
1200 store_key_permissions(pKeyContainer
->hSignatureKeyPair
, hKey
,
1206 /******************************************************************************
1207 * release_key_container_keys [Internal]
1209 * Releases key container's keys.
1212 * pKeyContainer [I] Pointer to the key container whose keys are to be released.
1214 static void release_key_container_keys(KEYCONTAINER
*pKeyContainer
)
1216 release_handle(&handle_table
, pKeyContainer
->hKeyExchangeKeyPair
,
1218 release_handle(&handle_table
, pKeyContainer
->hSignatureKeyPair
,
1222 /******************************************************************************
1223 * destroy_key_container [Internal]
1225 * Destructor for key containers.
1228 * pObjectHdr [I] Pointer to the key container to be destroyed.
1230 static void destroy_key_container(OBJECTHDR
*pObjectHdr
)
1232 KEYCONTAINER
*pKeyContainer
= (KEYCONTAINER
*)pObjectHdr
;
1234 if (!(pKeyContainer
->dwFlags
& CRYPT_VERIFYCONTEXT
))
1236 store_key_container_keys(pKeyContainer
);
1237 store_key_container_permissions(pKeyContainer
);
1238 release_key_container_keys(pKeyContainer
);
1241 release_key_container_keys(pKeyContainer
);
1242 HeapFree( GetProcessHeap(), 0, pKeyContainer
);
1245 /******************************************************************************
1246 * new_key_container [Internal]
1248 * Create a new key container. The personality (RSA Base, Strong or Enhanced CP)
1249 * of the CSP is determined via the pVTable->pszProvName string.
1252 * pszContainerName [I] Name of the key container.
1253 * pVTable [I] Callback functions and context info provided by the OS
1256 * Success: Handle to the new key container.
1257 * Failure: INVALID_HANDLE_VALUE
1259 static HCRYPTPROV
new_key_container(PCCH pszContainerName
, DWORD dwFlags
, const VTableProvStruc
*pVTable
)
1261 KEYCONTAINER
*pKeyContainer
;
1262 HCRYPTPROV hKeyContainer
;
1264 hKeyContainer
= new_object(&handle_table
, sizeof(KEYCONTAINER
), RSAENH_MAGIC_CONTAINER
,
1265 destroy_key_container
, (OBJECTHDR
**)&pKeyContainer
);
1266 if (hKeyContainer
!= (HCRYPTPROV
)INVALID_HANDLE_VALUE
)
1268 lstrcpynA(pKeyContainer
->szName
, pszContainerName
, MAX_PATH
);
1269 pKeyContainer
->dwFlags
= dwFlags
;
1270 pKeyContainer
->dwEnumAlgsCtr
= 0;
1271 pKeyContainer
->hKeyExchangeKeyPair
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
1272 pKeyContainer
->hSignatureKeyPair
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
1273 if (pVTable
&& pVTable
->pszProvName
) {
1274 lstrcpynA(pKeyContainer
->szProvName
, pVTable
->pszProvName
, MAX_PATH
);
1275 if (!strcmp(pVTable
->pszProvName
, MS_DEF_PROV_A
)) {
1276 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_BASE
;
1277 } else if (!strcmp(pVTable
->pszProvName
, MS_ENHANCED_PROV_A
)) {
1278 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_ENHANCED
;
1279 } else if (!strcmp(pVTable
->pszProvName
, MS_DEF_RSA_SCHANNEL_PROV_A
)) {
1280 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_SCHANNEL
;
1281 } else if (!strcmp(pVTable
->pszProvName
, MS_ENH_RSA_AES_PROV_A
)) {
1282 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_AES
;
1284 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_STRONG
;
1288 /* The new key container has to be inserted into the CSP immediately
1289 * after creation to be available for CPGetProvParam's PP_ENUMCONTAINERS. */
1290 if (!(dwFlags
& CRYPT_VERIFYCONTEXT
)) {
1293 if (create_container_key(pKeyContainer
, KEY_WRITE
, &hKey
))
1298 return hKeyContainer
;
1301 /******************************************************************************
1302 * read_key_value [Internal]
1304 * Reads a key pair value from the registry
1307 * hKeyContainer [I] Crypt provider to use to import the key
1308 * hKey [I] Registry key from which to read the key pair
1309 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
1310 * dwFlags [I] Flags for unprotecting the key
1311 * phCryptKey [O] Returned key
1313 static BOOL
read_key_value(HCRYPTPROV hKeyContainer
, HKEY hKey
, DWORD dwKeySpec
, DWORD dwFlags
, HCRYPTKEY
*phCryptKey
)
1316 DWORD dwValueType
, dwLen
;
1318 DATA_BLOB blobIn
, blobOut
;
1321 if (!(szValueName
= map_key_spec_to_key_pair_name(dwKeySpec
)))
1323 if (RegQueryValueExA(hKey
, szValueName
, 0, &dwValueType
, NULL
, &dwLen
) ==
1326 pbKey
= HeapAlloc(GetProcessHeap(), 0, dwLen
);
1329 if (RegQueryValueExA(hKey
, szValueName
, 0, &dwValueType
, pbKey
, &dwLen
) ==
1332 blobIn
.pbData
= pbKey
;
1333 blobIn
.cbData
= dwLen
;
1335 if (CryptUnprotectData(&blobIn
, NULL
, NULL
, NULL
, NULL
,
1338 ret
= import_key(hKeyContainer
, blobOut
.pbData
, blobOut
.cbData
, 0, 0,
1340 LocalFree(blobOut
.pbData
);
1343 HeapFree(GetProcessHeap(), 0, pbKey
);
1350 if (lookup_handle(&handle_table
, *phCryptKey
, RSAENH_MAGIC_KEY
,
1351 (OBJECTHDR
**)&pKey
))
1353 if ((szValueName
= map_key_spec_to_permissions_name(dwKeySpec
)))
1355 dwLen
= sizeof(pKey
->dwPermissions
);
1356 RegQueryValueExA(hKey
, szValueName
, 0, NULL
,
1357 (BYTE
*)&pKey
->dwPermissions
, &dwLen
);
1364 /******************************************************************************
1365 * read_key_container [Internal]
1367 * Tries to read the persistent state of the key container (mainly the signature
1368 * and key exchange private keys) given by pszContainerName.
1371 * pszContainerName [I] Name of the key container to read from the registry
1372 * pVTable [I] Pointer to context data provided by the operating system
1375 * Success: Handle to the key container read from the registry
1376 * Failure: INVALID_HANDLE_VALUE
1378 static HCRYPTPROV
read_key_container(PCHAR pszContainerName
, DWORD dwFlags
, const VTableProvStruc
*pVTable
)
1381 KEYCONTAINER
*pKeyContainer
;
1382 HCRYPTPROV hKeyContainer
;
1383 HCRYPTKEY hCryptKey
;
1385 if (!open_container_key(pszContainerName
, dwFlags
, &hKey
))
1387 SetLastError(NTE_BAD_KEYSET
);
1388 return (HCRYPTPROV
)INVALID_HANDLE_VALUE
;
1391 hKeyContainer
= new_key_container(pszContainerName
, dwFlags
, pVTable
);
1392 if (hKeyContainer
!= (HCRYPTPROV
)INVALID_HANDLE_VALUE
)
1394 DWORD dwProtectFlags
= (dwFlags
& CRYPT_MACHINE_KEYSET
) ?
1395 CRYPTPROTECT_LOCAL_MACHINE
: 0;
1397 if (!lookup_handle(&handle_table
, hKeyContainer
, RSAENH_MAGIC_CONTAINER
,
1398 (OBJECTHDR
**)&pKeyContainer
))
1399 return (HCRYPTPROV
)INVALID_HANDLE_VALUE
;
1401 /* read_key_value calls import_key, which calls import_private_key,
1402 * which implicitly installs the key value into the appropriate key
1403 * container key. Thus the ref count is incremented twice, once for
1404 * the output key value, and once for the implicit install, and needs
1405 * to be decremented to balance the two.
1407 if (read_key_value(hKeyContainer
, hKey
, AT_KEYEXCHANGE
,
1408 dwProtectFlags
, &hCryptKey
))
1409 release_handle(&handle_table
, hCryptKey
, RSAENH_MAGIC_KEY
);
1410 if (read_key_value(hKeyContainer
, hKey
, AT_SIGNATURE
,
1411 dwProtectFlags
, &hCryptKey
))
1412 release_handle(&handle_table
, hCryptKey
, RSAENH_MAGIC_KEY
);
1415 return hKeyContainer
;
1418 /******************************************************************************
1419 * build_hash_signature [Internal]
1421 * Builds a padded version of a hash to match the length of the RSA key modulus.
1424 * pbSignature [O] The padded hash object is stored here.
1425 * dwLen [I] Length of the pbSignature buffer.
1426 * aiAlgid [I] Algorithm identifier of the hash to be padded.
1427 * abHashValue [I] The value of the hash object.
1428 * dwHashLen [I] Length of the hash value.
1429 * dwFlags [I] Selection of padding algorithm.
1433 * Failure: FALSE (NTE_BAD_ALGID)
1435 static BOOL
build_hash_signature(BYTE
*pbSignature
, DWORD dwLen
, ALG_ID aiAlgid
,
1436 CONST BYTE
*abHashValue
, DWORD dwHashLen
, DWORD dwFlags
)
1438 /* These prefixes are meant to be concatenated with hash values of the
1439 * respective kind to form a PKCS #7 DigestInfo. */
1440 static const struct tagOIDDescriptor
{
1443 CONST BYTE abOID
[19];
1444 } aOIDDescriptor
[] = {
1445 { CALG_MD2
, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1446 0x86, 0xf7, 0x0d, 0x02, 0x02, 0x05, 0x00, 0x04, 0x10 } },
1447 { CALG_MD4
, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1448 0x86, 0xf7, 0x0d, 0x02, 0x04, 0x05, 0x00, 0x04, 0x10 } },
1449 { CALG_MD5
, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1450 0x86, 0xf7, 0x0d, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10 } },
1451 { CALG_SHA
, 15, { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03,
1452 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14 } },
1453 { CALG_SHA_256
, 19, { 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
1454 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01,
1455 0x05, 0x00, 0x04, 0x20 } },
1456 { CALG_SHA_384
, 19, { 0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
1457 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01,
1458 0x05, 0x00, 0x04, 0x30 } },
1459 { CALG_SHA_384
, 19, { 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
1460 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01,
1461 0x05, 0x00, 0x04, 0x40 } },
1462 { CALG_SSL3_SHAMD5
, 0, { 0 } },
1465 DWORD dwIdxOID
, i
, j
;
1467 for (dwIdxOID
= 0; aOIDDescriptor
[dwIdxOID
].aiAlgid
; dwIdxOID
++) {
1468 if (aOIDDescriptor
[dwIdxOID
].aiAlgid
== aiAlgid
) break;
1471 if (!aOIDDescriptor
[dwIdxOID
].aiAlgid
) {
1472 SetLastError(NTE_BAD_ALGID
);
1476 /* Build the padded signature */
1477 if (dwFlags
& CRYPT_X931_FORMAT
) {
1478 pbSignature
[0] = 0x6b;
1479 for (i
=1; i
< dwLen
- dwHashLen
- 3; i
++) {
1480 pbSignature
[i
] = 0xbb;
1482 pbSignature
[i
++] = 0xba;
1483 for (j
=0; j
< dwHashLen
; j
++, i
++) {
1484 pbSignature
[i
] = abHashValue
[j
];
1486 pbSignature
[i
++] = 0x33;
1487 pbSignature
[i
++] = 0xcc;
1489 pbSignature
[0] = 0x00;
1490 pbSignature
[1] = 0x01;
1491 if (dwFlags
& CRYPT_NOHASHOID
) {
1492 for (i
=2; i
< dwLen
- 1 - dwHashLen
; i
++) {
1493 pbSignature
[i
] = 0xff;
1495 pbSignature
[i
++] = 0x00;
1497 for (i
=2; i
< dwLen
- 1 - aOIDDescriptor
[dwIdxOID
].dwLen
- dwHashLen
; i
++) {
1498 pbSignature
[i
] = 0xff;
1500 pbSignature
[i
++] = 0x00;
1501 for (j
=0; j
< aOIDDescriptor
[dwIdxOID
].dwLen
; j
++) {
1502 pbSignature
[i
++] = aOIDDescriptor
[dwIdxOID
].abOID
[j
];
1505 for (j
=0; j
< dwHashLen
; j
++) {
1506 pbSignature
[i
++] = abHashValue
[j
];
1513 /******************************************************************************
1516 * This is an implementation of the 'P_hash' helper function for TLS1's PRF.
1517 * It is used exclusively by tls1_prf. For details see RFC 2246, chapter 5.
1518 * The pseudo random stream generated by this function is exclusive or'ed with
1519 * the data in pbBuffer.
1522 * hHMAC [I] HMAC object, which will be used in pseudo random generation
1523 * pblobSeed [I] Seed value
1524 * pbBuffer [I/O] Pseudo random stream will be xor'ed to the provided data
1525 * dwBufferLen [I] Number of pseudo random bytes desired
1531 static BOOL
tls1_p(HCRYPTHASH hHMAC
, CONST PCRYPT_DATA_BLOB pblobSeed
, PBYTE pbBuffer
, DWORD dwBufferLen
)
1534 BYTE abAi
[RSAENH_MAX_HASH_SIZE
];
1537 if (!lookup_handle(&handle_table
, hHMAC
, RSAENH_MAGIC_HASH
, (OBJECTHDR
**)&pHMAC
)) {
1538 SetLastError(NTE_BAD_HASH
);
1542 /* compute A_1 = HMAC(seed) */
1544 update_hash(pHMAC
, pblobSeed
->pbData
, pblobSeed
->cbData
);
1545 finalize_hash(pHMAC
);
1546 memcpy(abAi
, pHMAC
->abHashValue
, pHMAC
->dwHashSize
);
1549 /* compute HMAC(A_i + seed) */
1551 update_hash(pHMAC
, abAi
, pHMAC
->dwHashSize
);
1552 update_hash(pHMAC
, pblobSeed
->pbData
, pblobSeed
->cbData
);
1553 finalize_hash(pHMAC
);
1555 /* pseudo random stream := CONCAT_{i=1..n} ( HMAC(A_i + seed) ) */
1557 if (i
>= dwBufferLen
) break;
1558 pbBuffer
[i
] ^= pHMAC
->abHashValue
[i
% pHMAC
->dwHashSize
];
1560 } while (i
% pHMAC
->dwHashSize
);
1562 /* compute A_{i+1} = HMAC(A_i) */
1564 update_hash(pHMAC
, abAi
, pHMAC
->dwHashSize
);
1565 finalize_hash(pHMAC
);
1566 memcpy(abAi
, pHMAC
->abHashValue
, pHMAC
->dwHashSize
);
1567 } while (i
< dwBufferLen
);
1572 /******************************************************************************
1573 * tls1_prf [Internal]
1575 * TLS1 pseudo random function as specified in RFC 2246, chapter 5
1578 * hProv [I] Key container used to compute the pseudo random stream
1579 * hSecret [I] Key that holds the (pre-)master secret
1580 * pblobLabel [I] Descriptive label
1581 * pblobSeed [I] Seed value
1582 * pbBuffer [O] Pseudo random numbers will be stored here
1583 * dwBufferLen [I] Number of pseudo random bytes desired
1589 static BOOL
tls1_prf(HCRYPTPROV hProv
, HCRYPTPROV hSecret
, CONST PCRYPT_DATA_BLOB pblobLabel
,
1590 CONST PCRYPT_DATA_BLOB pblobSeed
, PBYTE pbBuffer
, DWORD dwBufferLen
)
1592 HMAC_INFO hmacInfo
= { 0, NULL
, 0, NULL
, 0 };
1593 HCRYPTHASH hHMAC
= (HCRYPTHASH
)INVALID_HANDLE_VALUE
;
1594 HCRYPTKEY hHalfSecret
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
1595 CRYPTKEY
*pHalfSecret
, *pSecret
;
1596 DWORD dwHalfSecretLen
;
1597 BOOL result
= FALSE
;
1598 CRYPT_DATA_BLOB blobLabelSeed
;
1600 TRACE("(hProv=%08lx, hSecret=%08lx, pblobLabel=%p, pblobSeed=%p, pbBuffer=%p, dwBufferLen=%d)\n",
1601 hProv
, hSecret
, pblobLabel
, pblobSeed
, pbBuffer
, dwBufferLen
);
1603 if (!lookup_handle(&handle_table
, hSecret
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pSecret
)) {
1604 SetLastError(NTE_FAIL
);
1608 dwHalfSecretLen
= (pSecret
->dwKeyLen
+1)/2;
1610 /* concatenation of the label and the seed */
1611 if (!concat_data_blobs(&blobLabelSeed
, pblobLabel
, pblobSeed
)) goto exit
;
1613 /* zero out the buffer, since two random streams will be xor'ed into it. */
1614 memset(pbBuffer
, 0, dwBufferLen
);
1616 /* build a 'fake' key, to hold the secret. CALG_SSL2_MASTER is used since it provides
1617 * the biggest range of valid key lengths. */
1618 hHalfSecret
= new_key(hProv
, CALG_SSL2_MASTER
, MAKELONG(0,dwHalfSecretLen
*8), &pHalfSecret
);
1619 if (hHalfSecret
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) goto exit
;
1621 /* Derive an HMAC_MD5 hash and call the helper function. */
1622 memcpy(pHalfSecret
->abKeyValue
, pSecret
->abKeyValue
, dwHalfSecretLen
);
1623 if (!RSAENH_CPCreateHash(hProv
, CALG_HMAC
, hHalfSecret
, 0, &hHMAC
)) goto exit
;
1624 hmacInfo
.HashAlgid
= CALG_MD5
;
1625 if (!RSAENH_CPSetHashParam(hProv
, hHMAC
, HP_HMAC_INFO
, (BYTE
*)&hmacInfo
, 0)) goto exit
;
1626 if (!tls1_p(hHMAC
, &blobLabelSeed
, pbBuffer
, dwBufferLen
)) goto exit
;
1628 /* Reconfigure to HMAC_SHA hash and call helper function again. */
1629 memcpy(pHalfSecret
->abKeyValue
, pSecret
->abKeyValue
+ (pSecret
->dwKeyLen
/2), dwHalfSecretLen
);
1630 hmacInfo
.HashAlgid
= CALG_SHA
;
1631 if (!RSAENH_CPSetHashParam(hProv
, hHMAC
, HP_HMAC_INFO
, (BYTE
*)&hmacInfo
, 0)) goto exit
;
1632 if (!tls1_p(hHMAC
, &blobLabelSeed
, pbBuffer
, dwBufferLen
)) goto exit
;
1636 release_handle(&handle_table
, hHalfSecret
, RSAENH_MAGIC_KEY
);
1637 if (hHMAC
!= (HCRYPTHASH
)INVALID_HANDLE_VALUE
) RSAENH_CPDestroyHash(hProv
, hHMAC
);
1638 free_data_blob(&blobLabelSeed
);
1642 /******************************************************************************
1643 * pad_data [Internal]
1645 * Helper function for data padding according to PKCS1 #2
1648 * abData [I] The data to be padded
1649 * dwDataLen [I] Length of the data
1650 * abBuffer [O] Padded data will be stored here
1651 * dwBufferLen [I] Length of the buffer (also length of padded data)
1652 * dwFlags [I] Padding format (CRYPT_SSL2_FALLBACK)
1656 * Failure: FALSE (NTE_BAD_LEN, too much data to pad)
1658 static BOOL
pad_data(CONST BYTE
*abData
, DWORD dwDataLen
, BYTE
*abBuffer
, DWORD dwBufferLen
,
1663 /* Ensure there is enough space for PKCS1 #2 padding */
1664 if (dwDataLen
> dwBufferLen
-11) {
1665 SetLastError(NTE_BAD_LEN
);
1669 memmove(abBuffer
+ dwBufferLen
- dwDataLen
, abData
, dwDataLen
);
1672 abBuffer
[1] = RSAENH_PKC_BLOCKTYPE
;
1673 for (i
=2; i
< dwBufferLen
- dwDataLen
- 1; i
++)
1674 do gen_rand_impl(&abBuffer
[i
], 1); while (!abBuffer
[i
]);
1675 if (dwFlags
& CRYPT_SSL2_FALLBACK
)
1676 for (i
-=8; i
< dwBufferLen
- dwDataLen
- 1; i
++)
1683 /******************************************************************************
1684 * unpad_data [Internal]
1686 * Remove the PKCS1 padding from RSA decrypted data
1689 * abData [I] The padded data
1690 * dwDataLen [I] Length of the padded data
1691 * abBuffer [O] Data without padding will be stored here
1692 * dwBufferLen [I/O] I: Length of the buffer, O: Length of unpadded data
1693 * dwFlags [I] Currently none defined
1697 * Failure: FALSE, (NTE_BAD_DATA, no valid PKCS1 padding or buffer too small)
1699 static BOOL
unpad_data(CONST BYTE
*abData
, DWORD dwDataLen
, BYTE
*abBuffer
, DWORD
*dwBufferLen
,
1704 for (i
=2; i
<dwDataLen
; i
++)
1708 if ((i
== dwDataLen
) || (*dwBufferLen
< dwDataLen
- i
- 1) ||
1709 (abData
[0] != 0x00) || (abData
[1] != RSAENH_PKC_BLOCKTYPE
))
1711 SetLastError(NTE_BAD_DATA
);
1715 *dwBufferLen
= dwDataLen
- i
- 1;
1716 memmove(abBuffer
, abData
+ i
+ 1, *dwBufferLen
);
1720 /******************************************************************************
1721 * CPAcquireContext (RSAENH.@)
1723 * Acquire a handle to the key container specified by pszContainer
1726 * phProv [O] Pointer to the location the acquired handle will be written to.
1727 * pszContainer [I] Name of the desired key container. See Notes
1728 * dwFlags [I] Flags. See Notes.
1729 * pVTable [I] Pointer to a PVTableProvStruct containing callbacks.
1736 * If pszContainer is NULL or points to a zero length string the user's login
1737 * name will be used as the key container name.
1739 * If the CRYPT_NEW_KEYSET flag is set in dwFlags a new keyset will be created.
1740 * If a keyset with the given name already exists, the function fails and sets
1741 * last error to NTE_EXISTS. If CRYPT_NEW_KEYSET is not set and the specified
1742 * key container does not exist, function fails and sets last error to
1745 BOOL WINAPI
RSAENH_CPAcquireContext(HCRYPTPROV
*phProv
, LPSTR pszContainer
,
1746 DWORD dwFlags
, PVTableProvStruc pVTable
)
1748 CHAR szKeyContainerName
[MAX_PATH
];
1750 TRACE("(phProv=%p, pszContainer=%s, dwFlags=%08x, pVTable=%p)\n", phProv
,
1751 debugstr_a(pszContainer
), dwFlags
, pVTable
);
1753 if (pszContainer
&& *pszContainer
)
1755 lstrcpynA(szKeyContainerName
, pszContainer
, MAX_PATH
);
1759 DWORD dwLen
= sizeof(szKeyContainerName
);
1760 if (!GetUserNameA(szKeyContainerName
, &dwLen
)) return FALSE
;
1763 switch (dwFlags
& (CRYPT_NEWKEYSET
|CRYPT_VERIFYCONTEXT
|CRYPT_DELETEKEYSET
))
1766 *phProv
= read_key_container(szKeyContainerName
, dwFlags
, pVTable
);
1769 case CRYPT_DELETEKEYSET
:
1770 return delete_container_key(szKeyContainerName
, dwFlags
);
1772 case CRYPT_NEWKEYSET
:
1773 *phProv
= read_key_container(szKeyContainerName
, dwFlags
, pVTable
);
1774 if (*phProv
!= (HCRYPTPROV
)INVALID_HANDLE_VALUE
)
1776 release_handle(&handle_table
, *phProv
, RSAENH_MAGIC_CONTAINER
);
1777 TRACE("Can't create new keyset, already exists\n");
1778 SetLastError(NTE_EXISTS
);
1781 *phProv
= new_key_container(szKeyContainerName
, dwFlags
, pVTable
);
1784 case CRYPT_VERIFYCONTEXT
|CRYPT_NEWKEYSET
:
1785 case CRYPT_VERIFYCONTEXT
:
1786 if (pszContainer
&& *pszContainer
) {
1787 TRACE("pszContainer should be empty\n");
1788 SetLastError(NTE_BAD_FLAGS
);
1791 *phProv
= new_key_container("", dwFlags
, pVTable
);
1795 *phProv
= (HCRYPTPROV
)INVALID_HANDLE_VALUE
;
1796 SetLastError(NTE_BAD_FLAGS
);
1800 if (*phProv
!= (HCRYPTPROV
)INVALID_HANDLE_VALUE
) {
1801 SetLastError(ERROR_SUCCESS
);
1808 /******************************************************************************
1809 * CPCreateHash (RSAENH.@)
1811 * CPCreateHash creates and initializes a new hash object.
1814 * hProv [I] Handle to the key container to which the new hash will belong.
1815 * Algid [I] Identifies the hash algorithm, which will be used for the hash.
1816 * hKey [I] Handle to a session key applied for keyed hashes.
1817 * dwFlags [I] Currently no flags defined. Must be zero.
1818 * phHash [O] Points to the location where a handle to the new hash will be stored.
1825 * hKey is a handle to a session key applied in keyed hashes like MAC and HMAC.
1826 * If a normal hash object is to be created (like e.g. MD2 or SHA1) hKey must be zero.
1828 BOOL WINAPI
RSAENH_CPCreateHash(HCRYPTPROV hProv
, ALG_ID Algid
, HCRYPTKEY hKey
, DWORD dwFlags
,
1831 CRYPTKEY
*pCryptKey
;
1832 CRYPTHASH
*pCryptHash
;
1833 const PROV_ENUMALGS_EX
*peaAlgidInfo
;
1835 TRACE("(hProv=%08lx, Algid=%08x, hKey=%08lx, dwFlags=%08x, phHash=%p)\n", hProv
, Algid
, hKey
,
1838 peaAlgidInfo
= get_algid_info(hProv
, Algid
);
1839 if (!peaAlgidInfo
) return FALSE
;
1843 SetLastError(NTE_BAD_FLAGS
);
1847 if (Algid
== CALG_MAC
|| Algid
== CALG_HMAC
|| Algid
== CALG_SCHANNEL_MASTER_HASH
||
1848 Algid
== CALG_TLS1PRF
)
1850 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
)) {
1851 SetLastError(NTE_BAD_KEY
);
1855 if ((Algid
== CALG_MAC
) && (GET_ALG_TYPE(pCryptKey
->aiAlgid
) != ALG_TYPE_BLOCK
)) {
1856 SetLastError(NTE_BAD_KEY
);
1860 if ((Algid
== CALG_SCHANNEL_MASTER_HASH
|| Algid
== CALG_TLS1PRF
) &&
1861 (pCryptKey
->aiAlgid
!= CALG_TLS1_MASTER
))
1863 SetLastError(NTE_BAD_KEY
);
1866 if (Algid
== CALG_SCHANNEL_MASTER_HASH
&&
1867 ((!pCryptKey
->siSChannelInfo
.blobClientRandom
.cbData
) ||
1868 (!pCryptKey
->siSChannelInfo
.blobServerRandom
.cbData
)))
1870 SetLastError(ERROR_INVALID_PARAMETER
);
1874 if ((Algid
== CALG_TLS1PRF
) && (pCryptKey
->dwState
!= RSAENH_KEYSTATE_MASTERKEY
)) {
1875 SetLastError(NTE_BAD_KEY_STATE
);
1880 *phHash
= new_object(&handle_table
, sizeof(CRYPTHASH
), RSAENH_MAGIC_HASH
,
1881 destroy_hash
, (OBJECTHDR
**)&pCryptHash
);
1882 if (!pCryptHash
) return FALSE
;
1884 pCryptHash
->aiAlgid
= Algid
;
1885 pCryptHash
->hKey
= hKey
;
1886 pCryptHash
->hProv
= hProv
;
1887 pCryptHash
->dwState
= RSAENH_HASHSTATE_HASHING
;
1888 pCryptHash
->pHMACInfo
= NULL
;
1889 pCryptHash
->dwHashSize
= peaAlgidInfo
->dwDefaultLen
>> 3;
1890 init_data_blob(&pCryptHash
->tpPRFParams
.blobLabel
);
1891 init_data_blob(&pCryptHash
->tpPRFParams
.blobSeed
);
1893 if (Algid
== CALG_SCHANNEL_MASTER_HASH
) {
1894 static const char keyex
[] = "key expansion";
1895 BYTE key_expansion
[sizeof keyex
];
1896 CRYPT_DATA_BLOB blobRandom
, blobKeyExpansion
= { 13, key_expansion
};
1898 memcpy( key_expansion
, keyex
, sizeof keyex
);
1900 if (pCryptKey
->dwState
!= RSAENH_KEYSTATE_MASTERKEY
) {
1901 static const char msec
[] = "master secret";
1902 BYTE master_secret
[sizeof msec
];
1903 CRYPT_DATA_BLOB blobLabel
= { 13, master_secret
};
1904 BYTE abKeyValue
[48];
1906 memcpy( master_secret
, msec
, sizeof msec
);
1908 /* See RFC 2246, chapter 8.1 */
1909 if (!concat_data_blobs(&blobRandom
,
1910 &pCryptKey
->siSChannelInfo
.blobClientRandom
,
1911 &pCryptKey
->siSChannelInfo
.blobServerRandom
))
1915 tls1_prf(hProv
, hKey
, &blobLabel
, &blobRandom
, abKeyValue
, 48);
1916 pCryptKey
->dwState
= RSAENH_KEYSTATE_MASTERKEY
;
1917 memcpy(pCryptKey
->abKeyValue
, abKeyValue
, 48);
1918 free_data_blob(&blobRandom
);
1921 /* See RFC 2246, chapter 6.3 */
1922 if (!concat_data_blobs(&blobRandom
,
1923 &pCryptKey
->siSChannelInfo
.blobServerRandom
,
1924 &pCryptKey
->siSChannelInfo
.blobClientRandom
))
1928 tls1_prf(hProv
, hKey
, &blobKeyExpansion
, &blobRandom
, pCryptHash
->abHashValue
,
1929 RSAENH_MAX_HASH_SIZE
);
1930 free_data_blob(&blobRandom
);
1933 return init_hash(pCryptHash
);
1936 /******************************************************************************
1937 * CPDestroyHash (RSAENH.@)
1939 * Releases the handle to a hash object. The object is destroyed if its reference
1940 * count reaches zero.
1943 * hProv [I] Handle to the key container to which the hash object belongs.
1944 * hHash [I] Handle to the hash object to be released.
1950 BOOL WINAPI
RSAENH_CPDestroyHash(HCRYPTPROV hProv
, HCRYPTHASH hHash
)
1952 TRACE("(hProv=%08lx, hHash=%08lx)\n", hProv
, hHash
);
1954 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
1956 SetLastError(NTE_BAD_UID
);
1960 if (!release_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
))
1962 SetLastError(NTE_BAD_HASH
);
1969 /******************************************************************************
1970 * CPDestroyKey (RSAENH.@)
1972 * Releases the handle to a key object. The object is destroyed if its reference
1973 * count reaches zero.
1976 * hProv [I] Handle to the key container to which the key object belongs.
1977 * hKey [I] Handle to the key object to be released.
1983 BOOL WINAPI
RSAENH_CPDestroyKey(HCRYPTPROV hProv
, HCRYPTKEY hKey
)
1985 TRACE("(hProv=%08lx, hKey=%08lx)\n", hProv
, hKey
);
1987 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
1989 SetLastError(NTE_BAD_UID
);
1993 if (!release_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
))
1995 SetLastError(NTE_BAD_KEY
);
2002 /******************************************************************************
2003 * CPDuplicateHash (RSAENH.@)
2005 * Clones a hash object including its current state.
2008 * hUID [I] Handle to the key container the hash belongs to.
2009 * hHash [I] Handle to the hash object to be cloned.
2010 * pdwReserved [I] Reserved. Must be NULL.
2011 * dwFlags [I] No flags are currently defined. Must be 0.
2012 * phHash [O] Handle to the cloned hash object.
2018 BOOL WINAPI
RSAENH_CPDuplicateHash(HCRYPTPROV hUID
, HCRYPTHASH hHash
, DWORD
*pdwReserved
,
2019 DWORD dwFlags
, HCRYPTHASH
*phHash
)
2021 CRYPTHASH
*pSrcHash
, *pDestHash
;
2023 TRACE("(hUID=%08lx, hHash=%08lx, pdwReserved=%p, dwFlags=%08x, phHash=%p)\n", hUID
, hHash
,
2024 pdwReserved
, dwFlags
, phHash
);
2026 if (!is_valid_handle(&handle_table
, hUID
, RSAENH_MAGIC_CONTAINER
))
2028 SetLastError(NTE_BAD_UID
);
2032 if (!lookup_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
, (OBJECTHDR
**)&pSrcHash
))
2034 SetLastError(NTE_BAD_HASH
);
2038 if (!phHash
|| pdwReserved
|| dwFlags
)
2040 SetLastError(ERROR_INVALID_PARAMETER
);
2044 *phHash
= new_object(&handle_table
, sizeof(CRYPTHASH
), RSAENH_MAGIC_HASH
,
2045 destroy_hash
, (OBJECTHDR
**)&pDestHash
);
2046 if (*phHash
!= (HCRYPTHASH
)INVALID_HANDLE_VALUE
)
2048 *pDestHash
= *pSrcHash
;
2049 duplicate_hash_impl(pSrcHash
->aiAlgid
, &pSrcHash
->context
, &pDestHash
->context
);
2050 copy_hmac_info(&pDestHash
->pHMACInfo
, pSrcHash
->pHMACInfo
);
2051 copy_data_blob(&pDestHash
->tpPRFParams
.blobLabel
, &pSrcHash
->tpPRFParams
.blobLabel
);
2052 copy_data_blob(&pDestHash
->tpPRFParams
.blobSeed
, &pSrcHash
->tpPRFParams
.blobSeed
);
2055 return *phHash
!= (HCRYPTHASH
)INVALID_HANDLE_VALUE
;
2058 /******************************************************************************
2059 * CPDuplicateKey (RSAENH.@)
2061 * Clones a key object including its current state.
2064 * hUID [I] Handle to the key container the hash belongs to.
2065 * hKey [I] Handle to the key object to be cloned.
2066 * pdwReserved [I] Reserved. Must be NULL.
2067 * dwFlags [I] No flags are currently defined. Must be 0.
2068 * phHash [O] Handle to the cloned key object.
2074 BOOL WINAPI
RSAENH_CPDuplicateKey(HCRYPTPROV hUID
, HCRYPTKEY hKey
, DWORD
*pdwReserved
,
2075 DWORD dwFlags
, HCRYPTKEY
*phKey
)
2077 CRYPTKEY
*pSrcKey
, *pDestKey
;
2079 TRACE("(hUID=%08lx, hKey=%08lx, pdwReserved=%p, dwFlags=%08x, phKey=%p)\n", hUID
, hKey
,
2080 pdwReserved
, dwFlags
, phKey
);
2082 if (!is_valid_handle(&handle_table
, hUID
, RSAENH_MAGIC_CONTAINER
))
2084 SetLastError(NTE_BAD_UID
);
2088 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pSrcKey
))
2090 SetLastError(NTE_BAD_KEY
);
2094 if (!phKey
|| pdwReserved
|| dwFlags
)
2096 SetLastError(ERROR_INVALID_PARAMETER
);
2100 *phKey
= new_object(&handle_table
, sizeof(CRYPTKEY
), RSAENH_MAGIC_KEY
, destroy_key
,
2101 (OBJECTHDR
**)&pDestKey
);
2102 if (*phKey
!= (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
2104 *pDestKey
= *pSrcKey
;
2105 copy_data_blob(&pDestKey
->siSChannelInfo
.blobServerRandom
,
2106 &pSrcKey
->siSChannelInfo
.blobServerRandom
);
2107 copy_data_blob(&pDestKey
->siSChannelInfo
.blobClientRandom
,
2108 &pSrcKey
->siSChannelInfo
.blobClientRandom
);
2109 duplicate_key_impl(pSrcKey
->aiAlgid
, &pSrcKey
->context
, &pDestKey
->context
);
2118 /******************************************************************************
2119 * CPEncrypt (RSAENH.@)
2124 * hProv [I] The key container hKey and hHash belong to.
2125 * hKey [I] The key used to encrypt the data.
2126 * hHash [I] An optional hash object for parallel hashing. See notes.
2127 * Final [I] Indicates if this is the last block of data to encrypt.
2128 * dwFlags [I] Currently no flags defined. Must be zero.
2129 * pbData [I/O] Pointer to the data to encrypt. Encrypted data will also be stored there.
2130 * pdwDataLen [I/O] I: Length of data to encrypt, O: Length of encrypted data.
2131 * dwBufLen [I] Size of the buffer at pbData.
2138 * If a hash object handle is provided in hHash, it will be updated with the plaintext.
2139 * This is useful for message signatures.
2141 * This function uses the standard WINAPI protocol for querying data of dynamic length.
2143 BOOL WINAPI
RSAENH_CPEncrypt(HCRYPTPROV hProv
, HCRYPTKEY hKey
, HCRYPTHASH hHash
, BOOL Final
,
2144 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
, DWORD dwBufLen
)
2146 CRYPTKEY
*pCryptKey
;
2147 BYTE
*in
, out
[RSAENH_MAX_BLOCK_SIZE
], o
[RSAENH_MAX_BLOCK_SIZE
];
2148 DWORD dwEncryptedLen
, i
, j
, k
;
2150 TRACE("(hProv=%08lx, hKey=%08lx, hHash=%08lx, Final=%d, dwFlags=%08x, pbData=%p, "
2151 "pdwDataLen=%p, dwBufLen=%d)\n", hProv
, hKey
, hHash
, Final
, dwFlags
, pbData
, pdwDataLen
,
2154 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
2156 SetLastError(NTE_BAD_UID
);
2162 SetLastError(NTE_BAD_FLAGS
);
2166 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
2168 SetLastError(NTE_BAD_KEY
);
2172 if (pCryptKey
->dwState
== RSAENH_KEYSTATE_IDLE
)
2173 pCryptKey
->dwState
= RSAENH_KEYSTATE_ENCRYPTING
;
2175 if (pCryptKey
->dwState
!= RSAENH_KEYSTATE_ENCRYPTING
)
2177 SetLastError(NTE_BAD_DATA
);
2181 if (is_valid_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
)) {
2182 if (!RSAENH_CPHashData(hProv
, hHash
, pbData
, *pdwDataLen
, 0)) return FALSE
;
2185 if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_BLOCK
) {
2186 if (!Final
&& (*pdwDataLen
% pCryptKey
->dwBlockLen
)) {
2187 SetLastError(NTE_BAD_DATA
);
2191 dwEncryptedLen
= (*pdwDataLen
/pCryptKey
->dwBlockLen
+(Final
?1:0))*pCryptKey
->dwBlockLen
;
2193 if (pbData
== NULL
) {
2194 *pdwDataLen
= dwEncryptedLen
;
2197 else if (dwEncryptedLen
> dwBufLen
) {
2198 *pdwDataLen
= dwEncryptedLen
;
2199 SetLastError(ERROR_MORE_DATA
);
2203 /* Pad final block with length bytes */
2204 for (i
=*pdwDataLen
; i
<dwEncryptedLen
; i
++) pbData
[i
] = dwEncryptedLen
- *pdwDataLen
;
2205 *pdwDataLen
= dwEncryptedLen
;
2207 for (i
=0, in
=pbData
; i
<*pdwDataLen
; i
+=pCryptKey
->dwBlockLen
, in
+=pCryptKey
->dwBlockLen
) {
2208 switch (pCryptKey
->dwMode
) {
2209 case CRYPT_MODE_ECB
:
2210 encrypt_block_impl(pCryptKey
->aiAlgid
, 0, &pCryptKey
->context
, in
, out
,
2214 case CRYPT_MODE_CBC
:
2215 for (j
=0; j
<pCryptKey
->dwBlockLen
; j
++) in
[j
] ^= pCryptKey
->abChainVector
[j
];
2216 encrypt_block_impl(pCryptKey
->aiAlgid
, 0, &pCryptKey
->context
, in
, out
,
2218 memcpy(pCryptKey
->abChainVector
, out
, pCryptKey
->dwBlockLen
);
2221 case CRYPT_MODE_CFB
:
2222 for (j
=0; j
<pCryptKey
->dwBlockLen
; j
++) {
2223 encrypt_block_impl(pCryptKey
->aiAlgid
, 0, &pCryptKey
->context
,
2224 pCryptKey
->abChainVector
, o
, RSAENH_ENCRYPT
);
2225 out
[j
] = in
[j
] ^ o
[0];
2226 for (k
=0; k
<pCryptKey
->dwBlockLen
-1; k
++)
2227 pCryptKey
->abChainVector
[k
] = pCryptKey
->abChainVector
[k
+1];
2228 pCryptKey
->abChainVector
[k
] = out
[j
];
2233 SetLastError(NTE_BAD_ALGID
);
2236 memcpy(in
, out
, pCryptKey
->dwBlockLen
);
2238 } else if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_STREAM
) {
2239 if (pbData
== NULL
) {
2240 *pdwDataLen
= dwBufLen
;
2243 encrypt_stream_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pbData
, *pdwDataLen
);
2244 } else if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_RSA
) {
2245 if (pCryptKey
->aiAlgid
== CALG_RSA_SIGN
) {
2246 SetLastError(NTE_BAD_KEY
);
2250 *pdwDataLen
= pCryptKey
->dwBlockLen
;
2253 if (dwBufLen
< pCryptKey
->dwBlockLen
) {
2254 SetLastError(ERROR_MORE_DATA
);
2257 if (!pad_data(pbData
, *pdwDataLen
, pbData
, pCryptKey
->dwBlockLen
, dwFlags
)) return FALSE
;
2258 encrypt_block_impl(pCryptKey
->aiAlgid
, PK_PUBLIC
, &pCryptKey
->context
, pbData
, pbData
, RSAENH_ENCRYPT
);
2259 *pdwDataLen
= pCryptKey
->dwBlockLen
;
2262 SetLastError(NTE_BAD_TYPE
);
2266 if (Final
) setup_key(pCryptKey
);
2271 /******************************************************************************
2272 * CPDecrypt (RSAENH.@)
2277 * hProv [I] The key container hKey and hHash belong to.
2278 * hKey [I] The key used to decrypt the data.
2279 * hHash [I] An optional hash object for parallel hashing. See notes.
2280 * Final [I] Indicates if this is the last block of data to decrypt.
2281 * dwFlags [I] Currently no flags defined. Must be zero.
2282 * pbData [I/O] Pointer to the data to decrypt. Plaintext will also be stored there.
2283 * pdwDataLen [I/O] I: Length of ciphertext, O: Length of plaintext.
2290 * If a hash object handle is provided in hHash, it will be updated with the plaintext.
2291 * This is useful for message signatures.
2293 * This function uses the standard WINAPI protocol for querying data of dynamic length.
2295 BOOL WINAPI
RSAENH_CPDecrypt(HCRYPTPROV hProv
, HCRYPTKEY hKey
, HCRYPTHASH hHash
, BOOL Final
,
2296 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
)
2298 CRYPTKEY
*pCryptKey
;
2299 BYTE
*in
, out
[RSAENH_MAX_BLOCK_SIZE
], o
[RSAENH_MAX_BLOCK_SIZE
];
2303 TRACE("(hProv=%08lx, hKey=%08lx, hHash=%08lx, Final=%d, dwFlags=%08x, pbData=%p, "
2304 "pdwDataLen=%p)\n", hProv
, hKey
, hHash
, Final
, dwFlags
, pbData
, pdwDataLen
);
2306 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
2308 SetLastError(NTE_BAD_UID
);
2314 SetLastError(NTE_BAD_FLAGS
);
2318 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
2320 SetLastError(NTE_BAD_KEY
);
2324 if (pCryptKey
->dwState
== RSAENH_KEYSTATE_IDLE
)
2325 pCryptKey
->dwState
= RSAENH_KEYSTATE_ENCRYPTING
;
2327 if (pCryptKey
->dwState
!= RSAENH_KEYSTATE_ENCRYPTING
)
2329 SetLastError(NTE_BAD_DATA
);
2335 if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_BLOCK
) {
2336 for (i
=0, in
=pbData
; i
<*pdwDataLen
; i
+=pCryptKey
->dwBlockLen
, in
+=pCryptKey
->dwBlockLen
) {
2337 switch (pCryptKey
->dwMode
) {
2338 case CRYPT_MODE_ECB
:
2339 encrypt_block_impl(pCryptKey
->aiAlgid
, 0, &pCryptKey
->context
, in
, out
,
2343 case CRYPT_MODE_CBC
:
2344 encrypt_block_impl(pCryptKey
->aiAlgid
, 0, &pCryptKey
->context
, in
, out
,
2346 for (j
=0; j
<pCryptKey
->dwBlockLen
; j
++) out
[j
] ^= pCryptKey
->abChainVector
[j
];
2347 memcpy(pCryptKey
->abChainVector
, in
, pCryptKey
->dwBlockLen
);
2350 case CRYPT_MODE_CFB
:
2351 for (j
=0; j
<pCryptKey
->dwBlockLen
; j
++) {
2352 encrypt_block_impl(pCryptKey
->aiAlgid
, 0, &pCryptKey
->context
,
2353 pCryptKey
->abChainVector
, o
, RSAENH_ENCRYPT
);
2354 out
[j
] = in
[j
] ^ o
[0];
2355 for (k
=0; k
<pCryptKey
->dwBlockLen
-1; k
++)
2356 pCryptKey
->abChainVector
[k
] = pCryptKey
->abChainVector
[k
+1];
2357 pCryptKey
->abChainVector
[k
] = in
[j
];
2362 SetLastError(NTE_BAD_ALGID
);
2365 memcpy(in
, out
, pCryptKey
->dwBlockLen
);
2368 if (pbData
[*pdwDataLen
-1] &&
2369 pbData
[*pdwDataLen
-1] <= pCryptKey
->dwBlockLen
&&
2370 pbData
[*pdwDataLen
-1] <= *pdwDataLen
) {
2371 BOOL padOkay
= TRUE
;
2373 /* check that every bad byte has the same value */
2374 for (i
= 1; padOkay
&& i
< pbData
[*pdwDataLen
-1]; i
++)
2375 if (pbData
[*pdwDataLen
- i
- 1] != pbData
[*pdwDataLen
- 1])
2378 *pdwDataLen
-= pbData
[*pdwDataLen
-1];
2380 SetLastError(NTE_BAD_DATA
);
2385 SetLastError(NTE_BAD_DATA
);
2390 } else if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_STREAM
) {
2391 encrypt_stream_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pbData
, *pdwDataLen
);
2392 } else if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_RSA
) {
2393 if (pCryptKey
->aiAlgid
== CALG_RSA_SIGN
) {
2394 SetLastError(NTE_BAD_KEY
);
2397 encrypt_block_impl(pCryptKey
->aiAlgid
, PK_PRIVATE
, &pCryptKey
->context
, pbData
, pbData
, RSAENH_DECRYPT
);
2398 if (!unpad_data(pbData
, pCryptKey
->dwBlockLen
, pbData
, pdwDataLen
, dwFlags
)) return FALSE
;
2401 SetLastError(NTE_BAD_TYPE
);
2405 if (Final
) setup_key(pCryptKey
);
2407 if (is_valid_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
)) {
2408 if (*pdwDataLen
>dwMax
||
2409 !RSAENH_CPHashData(hProv
, hHash
, pbData
, *pdwDataLen
, 0)) return FALSE
;
2415 static BOOL
crypt_export_simple(CRYPTKEY
*pCryptKey
, CRYPTKEY
*pPubKey
,
2416 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
)
2418 BLOBHEADER
*pBlobHeader
= (BLOBHEADER
*)pbData
;
2419 ALG_ID
*pAlgid
= (ALG_ID
*)(pBlobHeader
+1);
2422 if (!(GET_ALG_CLASS(pCryptKey
->aiAlgid
)&(ALG_CLASS_DATA_ENCRYPT
|ALG_CLASS_MSG_ENCRYPT
))) {
2423 SetLastError(NTE_BAD_KEY
); /* FIXME: error code? */
2427 dwDataLen
= sizeof(BLOBHEADER
) + sizeof(ALG_ID
) + pPubKey
->dwBlockLen
;
2429 if (*pdwDataLen
< dwDataLen
) {
2430 SetLastError(ERROR_MORE_DATA
);
2431 *pdwDataLen
= dwDataLen
;
2435 pBlobHeader
->bType
= SIMPLEBLOB
;
2436 pBlobHeader
->bVersion
= CUR_BLOB_VERSION
;
2437 pBlobHeader
->reserved
= 0;
2438 pBlobHeader
->aiKeyAlg
= pCryptKey
->aiAlgid
;
2440 *pAlgid
= pPubKey
->aiAlgid
;
2442 if (!pad_data(pCryptKey
->abKeyValue
, pCryptKey
->dwKeyLen
, (BYTE
*)(pAlgid
+1),
2443 pPubKey
->dwBlockLen
, dwFlags
))
2448 encrypt_block_impl(pPubKey
->aiAlgid
, PK_PUBLIC
, &pPubKey
->context
, (BYTE
*)(pAlgid
+1),
2449 (BYTE
*)(pAlgid
+1), RSAENH_ENCRYPT
);
2451 *pdwDataLen
= dwDataLen
;
2455 static BOOL
crypt_export_public_key(CRYPTKEY
*pCryptKey
, BYTE
*pbData
,
2458 BLOBHEADER
*pBlobHeader
= (BLOBHEADER
*)pbData
;
2459 RSAPUBKEY
*pRSAPubKey
= (RSAPUBKEY
*)(pBlobHeader
+1);
2462 if ((pCryptKey
->aiAlgid
!= CALG_RSA_KEYX
) && (pCryptKey
->aiAlgid
!= CALG_RSA_SIGN
)) {
2463 SetLastError(NTE_BAD_KEY
);
2467 dwDataLen
= sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) + pCryptKey
->dwKeyLen
;
2469 if (*pdwDataLen
< dwDataLen
) {
2470 SetLastError(ERROR_MORE_DATA
);
2471 *pdwDataLen
= dwDataLen
;
2475 pBlobHeader
->bType
= PUBLICKEYBLOB
;
2476 pBlobHeader
->bVersion
= CUR_BLOB_VERSION
;
2477 pBlobHeader
->reserved
= 0;
2478 pBlobHeader
->aiKeyAlg
= pCryptKey
->aiAlgid
;
2480 pRSAPubKey
->magic
= RSAENH_MAGIC_RSA1
;
2481 pRSAPubKey
->bitlen
= pCryptKey
->dwKeyLen
<< 3;
2483 export_public_key_impl((BYTE
*)(pRSAPubKey
+1), &pCryptKey
->context
,
2484 pCryptKey
->dwKeyLen
, &pRSAPubKey
->pubexp
);
2486 *pdwDataLen
= dwDataLen
;
2490 static BOOL
crypt_export_private_key(CRYPTKEY
*pCryptKey
, BOOL force
,
2491 BYTE
*pbData
, DWORD
*pdwDataLen
)
2493 BLOBHEADER
*pBlobHeader
= (BLOBHEADER
*)pbData
;
2494 RSAPUBKEY
*pRSAPubKey
= (RSAPUBKEY
*)(pBlobHeader
+1);
2497 if ((pCryptKey
->aiAlgid
!= CALG_RSA_KEYX
) && (pCryptKey
->aiAlgid
!= CALG_RSA_SIGN
)) {
2498 SetLastError(NTE_BAD_KEY
);
2501 if (!force
&& !(pCryptKey
->dwPermissions
& CRYPT_EXPORT
))
2503 SetLastError(NTE_BAD_KEY_STATE
);
2507 dwDataLen
= sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) +
2508 2 * pCryptKey
->dwKeyLen
+ 5 * ((pCryptKey
->dwKeyLen
+ 1) >> 1);
2510 if (*pdwDataLen
< dwDataLen
) {
2511 SetLastError(ERROR_MORE_DATA
);
2512 *pdwDataLen
= dwDataLen
;
2516 pBlobHeader
->bType
= PRIVATEKEYBLOB
;
2517 pBlobHeader
->bVersion
= CUR_BLOB_VERSION
;
2518 pBlobHeader
->reserved
= 0;
2519 pBlobHeader
->aiKeyAlg
= pCryptKey
->aiAlgid
;
2521 pRSAPubKey
->magic
= RSAENH_MAGIC_RSA2
;
2522 pRSAPubKey
->bitlen
= pCryptKey
->dwKeyLen
<< 3;
2524 export_private_key_impl((BYTE
*)(pRSAPubKey
+1), &pCryptKey
->context
,
2525 pCryptKey
->dwKeyLen
, &pRSAPubKey
->pubexp
);
2527 *pdwDataLen
= dwDataLen
;
2531 static BOOL
crypt_export_plaintext_key(CRYPTKEY
*pCryptKey
, BYTE
*pbData
,
2534 BLOBHEADER
*pBlobHeader
= (BLOBHEADER
*)pbData
;
2535 DWORD
*pKeyLen
= (DWORD
*)(pBlobHeader
+1);
2536 BYTE
*pbKey
= (BYTE
*)(pKeyLen
+1);
2539 dwDataLen
= sizeof(BLOBHEADER
) + sizeof(DWORD
) + pCryptKey
->dwKeyLen
;
2541 if (*pdwDataLen
< dwDataLen
) {
2542 SetLastError(ERROR_MORE_DATA
);
2543 *pdwDataLen
= dwDataLen
;
2547 pBlobHeader
->bType
= PLAINTEXTKEYBLOB
;
2548 pBlobHeader
->bVersion
= CUR_BLOB_VERSION
;
2549 pBlobHeader
->reserved
= 0;
2550 pBlobHeader
->aiKeyAlg
= pCryptKey
->aiAlgid
;
2552 *pKeyLen
= pCryptKey
->dwKeyLen
;
2553 memcpy(pbKey
, pCryptKey
->abKeyValue
, pCryptKey
->dwKeyLen
);
2555 *pdwDataLen
= dwDataLen
;
2558 /******************************************************************************
2559 * crypt_export_key [Internal]
2561 * Export a key into a binary large object (BLOB). Called by CPExportKey and
2562 * by store_key_pair.
2565 * pCryptKey [I] Key to be exported.
2566 * hPubKey [I] Key used to encrypt sensitive BLOB data.
2567 * dwBlobType [I] SIMPLEBLOB, PUBLICKEYBLOB or PRIVATEKEYBLOB.
2568 * dwFlags [I] Currently none defined.
2569 * force [I] If TRUE, the key is written no matter what the key's
2570 * permissions are. Otherwise the key's permissions are
2571 * checked before exporting.
2572 * pbData [O] Pointer to a buffer where the BLOB will be written to.
2573 * pdwDataLen [I/O] I: Size of buffer at pbData, O: Size of BLOB
2579 static BOOL
crypt_export_key(CRYPTKEY
*pCryptKey
, HCRYPTKEY hPubKey
,
2580 DWORD dwBlobType
, DWORD dwFlags
, BOOL force
,
2581 BYTE
*pbData
, DWORD
*pdwDataLen
)
2585 if (dwFlags
& CRYPT_SSL2_FALLBACK
) {
2586 if (pCryptKey
->aiAlgid
!= CALG_SSL2_MASTER
) {
2587 SetLastError(NTE_BAD_KEY
);
2592 switch ((BYTE
)dwBlobType
)
2595 if (!lookup_handle(&handle_table
, hPubKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pPubKey
)){
2596 SetLastError(NTE_BAD_PUBLIC_KEY
); /* FIXME: error_code? */
2599 return crypt_export_simple(pCryptKey
, pPubKey
, dwFlags
, pbData
,
2603 if (is_valid_handle(&handle_table
, hPubKey
, RSAENH_MAGIC_KEY
)) {
2604 SetLastError(NTE_BAD_KEY
); /* FIXME: error code? */
2608 return crypt_export_public_key(pCryptKey
, pbData
, pdwDataLen
);
2610 case PRIVATEKEYBLOB
:
2611 return crypt_export_private_key(pCryptKey
, force
, pbData
, pdwDataLen
);
2613 case PLAINTEXTKEYBLOB
:
2614 return crypt_export_plaintext_key(pCryptKey
, pbData
, pdwDataLen
);
2617 SetLastError(NTE_BAD_TYPE
); /* FIXME: error code? */
2622 /******************************************************************************
2623 * CPExportKey (RSAENH.@)
2625 * Export a key into a binary large object (BLOB).
2628 * hProv [I] Key container from which a key is to be exported.
2629 * hKey [I] Key to be exported.
2630 * hPubKey [I] Key used to encrypt sensitive BLOB data.
2631 * dwBlobType [I] SIMPLEBLOB, PUBLICKEYBLOB or PRIVATEKEYBLOB.
2632 * dwFlags [I] Currently none defined.
2633 * pbData [O] Pointer to a buffer where the BLOB will be written to.
2634 * pdwDataLen [I/O] I: Size of buffer at pbData, O: Size of BLOB
2640 BOOL WINAPI
RSAENH_CPExportKey(HCRYPTPROV hProv
, HCRYPTKEY hKey
, HCRYPTKEY hPubKey
,
2641 DWORD dwBlobType
, DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
)
2643 CRYPTKEY
*pCryptKey
;
2645 TRACE("(hProv=%08lx, hKey=%08lx, hPubKey=%08lx, dwBlobType=%08x, dwFlags=%08x, pbData=%p,"
2646 "pdwDataLen=%p)\n", hProv
, hKey
, hPubKey
, dwBlobType
, dwFlags
, pbData
, pdwDataLen
);
2648 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
2650 SetLastError(NTE_BAD_UID
);
2654 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
2656 SetLastError(NTE_BAD_KEY
);
2660 return crypt_export_key(pCryptKey
, hPubKey
, dwBlobType
, dwFlags
, FALSE
,
2661 pbData
, pdwDataLen
);
2664 /******************************************************************************
2665 * release_and_install_key [Internal]
2667 * Release an existing key, if present, and replaces it with a new one.
2670 * hProv [I] Key container into which the key is to be imported.
2671 * src [I] Key which will replace *dest
2672 * dest [I] Points to key to be released and replaced with src
2673 * fStoreKey [I] If TRUE, the newly installed key is stored to the registry.
2675 static void release_and_install_key(HCRYPTPROV hProv
, HCRYPTKEY src
,
2676 HCRYPTKEY
*dest
, DWORD fStoreKey
)
2678 RSAENH_CPDestroyKey(hProv
, *dest
);
2679 copy_handle(&handle_table
, src
, RSAENH_MAGIC_KEY
, dest
);
2682 KEYCONTAINER
*pKeyContainer
;
2684 if (lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
,
2685 (OBJECTHDR
**)&pKeyContainer
))
2687 store_key_container_keys(pKeyContainer
);
2688 store_key_container_permissions(pKeyContainer
);
2693 /******************************************************************************
2694 * import_private_key [Internal]
2696 * Import a BLOB'ed private key into a key container.
2699 * hProv [I] Key container into which the private key is to be imported.
2700 * pbData [I] Pointer to a buffer which holds the private key BLOB.
2701 * dwDataLen [I] Length of data in buffer at pbData.
2702 * dwFlags [I] One of:
2703 * CRYPT_EXPORTABLE: the imported key is marked exportable
2704 * fStoreKey [I] If TRUE, the imported key is stored to the registry.
2705 * phKey [O] Handle to the imported key.
2709 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2710 * it's a PRIVATEKEYBLOB.
2716 static BOOL
import_private_key(HCRYPTPROV hProv
, CONST BYTE
*pbData
, DWORD dwDataLen
,
2717 DWORD dwFlags
, BOOL fStoreKey
, HCRYPTKEY
*phKey
)
2719 KEYCONTAINER
*pKeyContainer
;
2720 CRYPTKEY
*pCryptKey
;
2721 CONST BLOBHEADER
*pBlobHeader
= (CONST BLOBHEADER
*)pbData
;
2722 CONST RSAPUBKEY
*pRSAPubKey
= (CONST RSAPUBKEY
*)(pBlobHeader
+1);
2725 if (dwFlags
& CRYPT_IPSEC_HMAC_KEY
)
2727 FIXME("unimplemented for CRYPT_IPSEC_HMAC_KEY\n");
2728 SetLastError(NTE_BAD_FLAGS
);
2731 if (!lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
,
2732 (OBJECTHDR
**)&pKeyContainer
))
2734 SetLastError(NTE_BAD_UID
);
2738 if ((dwDataLen
< sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
)))
2740 ERR("datalen %d not long enough for a BLOBHEADER + RSAPUBKEY\n",
2742 SetLastError(NTE_BAD_DATA
);
2745 if (pRSAPubKey
->magic
!= RSAENH_MAGIC_RSA2
)
2747 ERR("unexpected magic %08x\n", pRSAPubKey
->magic
);
2748 SetLastError(NTE_BAD_DATA
);
2751 if ((dwDataLen
< sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) +
2752 (pRSAPubKey
->bitlen
>> 3) + (5 * ((pRSAPubKey
->bitlen
+8)>>4))))
2754 DWORD expectedLen
= sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) +
2755 (pRSAPubKey
->bitlen
>> 3) + (5 * ((pRSAPubKey
->bitlen
+8)>>4));
2757 ERR("blob too short for pub key: expect %d, got %d\n",
2758 expectedLen
, dwDataLen
);
2759 SetLastError(NTE_BAD_DATA
);
2763 *phKey
= new_key(hProv
, pBlobHeader
->aiKeyAlg
, MAKELONG(0,pRSAPubKey
->bitlen
), &pCryptKey
);
2764 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
2765 setup_key(pCryptKey
);
2766 ret
= import_private_key_impl((CONST BYTE
*)(pRSAPubKey
+1), &pCryptKey
->context
,
2767 pRSAPubKey
->bitlen
/8, dwDataLen
, pRSAPubKey
->pubexp
);
2769 if (dwFlags
& CRYPT_EXPORTABLE
)
2770 pCryptKey
->dwPermissions
|= CRYPT_EXPORT
;
2771 switch (pBlobHeader
->aiKeyAlg
)
2775 TRACE("installing signing key\n");
2776 release_and_install_key(hProv
, *phKey
, &pKeyContainer
->hSignatureKeyPair
,
2779 case AT_KEYEXCHANGE
:
2781 TRACE("installing key exchange key\n");
2782 release_and_install_key(hProv
, *phKey
, &pKeyContainer
->hKeyExchangeKeyPair
,
2790 /******************************************************************************
2791 * import_public_key [Internal]
2793 * Import a BLOB'ed public key into a key container.
2796 * hProv [I] Key container into which the public key is to be imported.
2797 * pbData [I] Pointer to a buffer which holds the public key BLOB.
2798 * dwDataLen [I] Length of data in buffer at pbData.
2799 * dwFlags [I] One of:
2800 * CRYPT_EXPORTABLE: the imported key is marked exportable
2801 * fStoreKey [I] If TRUE, the imported key is stored to the registry.
2802 * phKey [O] Handle to the imported key.
2806 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2807 * it's a PUBLICKEYBLOB.
2813 static BOOL
import_public_key(HCRYPTPROV hProv
, CONST BYTE
*pbData
, DWORD dwDataLen
,
2814 DWORD dwFlags
, BOOL fStoreKey
, HCRYPTKEY
*phKey
)
2816 KEYCONTAINER
*pKeyContainer
;
2817 CRYPTKEY
*pCryptKey
;
2818 CONST BLOBHEADER
*pBlobHeader
= (CONST BLOBHEADER
*)pbData
;
2819 CONST RSAPUBKEY
*pRSAPubKey
= (CONST RSAPUBKEY
*)(pBlobHeader
+1);
2823 if (dwFlags
& CRYPT_IPSEC_HMAC_KEY
)
2825 FIXME("unimplemented for CRYPT_IPSEC_HMAC_KEY\n");
2826 SetLastError(NTE_BAD_FLAGS
);
2829 if (!lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
,
2830 (OBJECTHDR
**)&pKeyContainer
))
2832 SetLastError(NTE_BAD_UID
);
2836 if ((dwDataLen
< sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
)) ||
2837 (pRSAPubKey
->magic
!= RSAENH_MAGIC_RSA1
) ||
2838 (dwDataLen
< sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) + (pRSAPubKey
->bitlen
>> 3)))
2840 SetLastError(NTE_BAD_DATA
);
2844 /* Since this is a public key blob, only the public key is
2845 * available, so only signature verification is possible.
2847 algID
= pBlobHeader
->aiKeyAlg
;
2848 *phKey
= new_key(hProv
, algID
, MAKELONG(0,pRSAPubKey
->bitlen
), &pCryptKey
);
2849 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
2850 setup_key(pCryptKey
);
2851 ret
= import_public_key_impl((CONST BYTE
*)(pRSAPubKey
+1), &pCryptKey
->context
,
2852 pRSAPubKey
->bitlen
>> 3, pRSAPubKey
->pubexp
);
2854 if (dwFlags
& CRYPT_EXPORTABLE
)
2855 pCryptKey
->dwPermissions
|= CRYPT_EXPORT
;
2856 switch (pBlobHeader
->aiKeyAlg
)
2858 case AT_KEYEXCHANGE
:
2860 TRACE("installing public key\n");
2861 release_and_install_key(hProv
, *phKey
, &pKeyContainer
->hKeyExchangeKeyPair
,
2869 /******************************************************************************
2870 * import_symmetric_key [Internal]
2872 * Import a BLOB'ed symmetric key into a key container.
2875 * hProv [I] Key container into which the symmetric key is to be imported.
2876 * pbData [I] Pointer to a buffer which holds the symmetric key BLOB.
2877 * dwDataLen [I] Length of data in buffer at pbData.
2878 * hPubKey [I] Key used to decrypt sensitive BLOB data.
2879 * dwFlags [I] One of:
2880 * CRYPT_EXPORTABLE: the imported key is marked exportable
2881 * phKey [O] Handle to the imported key.
2885 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2886 * it's a SIMPLEBLOB.
2892 static BOOL
import_symmetric_key(HCRYPTPROV hProv
, CONST BYTE
*pbData
,
2893 DWORD dwDataLen
, HCRYPTKEY hPubKey
,
2894 DWORD dwFlags
, HCRYPTKEY
*phKey
)
2896 CRYPTKEY
*pCryptKey
, *pPubKey
;
2897 CONST BLOBHEADER
*pBlobHeader
= (CONST BLOBHEADER
*)pbData
;
2898 CONST ALG_ID
*pAlgid
= (CONST ALG_ID
*)(pBlobHeader
+1);
2899 CONST BYTE
*pbKeyStream
= (CONST BYTE
*)(pAlgid
+ 1);
2903 if (dwFlags
& CRYPT_IPSEC_HMAC_KEY
)
2905 FIXME("unimplemented for CRYPT_IPSEC_HMAC_KEY\n");
2906 SetLastError(NTE_BAD_FLAGS
);
2909 if (!lookup_handle(&handle_table
, hPubKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pPubKey
) ||
2910 pPubKey
->aiAlgid
!= CALG_RSA_KEYX
)
2912 SetLastError(NTE_BAD_PUBLIC_KEY
); /* FIXME: error code? */
2916 if (dwDataLen
< sizeof(BLOBHEADER
)+sizeof(ALG_ID
)+pPubKey
->dwBlockLen
)
2918 SetLastError(NTE_BAD_DATA
); /* FIXME: error code */
2922 pbDecrypted
= HeapAlloc(GetProcessHeap(), 0, pPubKey
->dwBlockLen
);
2923 if (!pbDecrypted
) return FALSE
;
2924 encrypt_block_impl(pPubKey
->aiAlgid
, PK_PRIVATE
, &pPubKey
->context
, pbKeyStream
, pbDecrypted
,
2927 dwKeyLen
= RSAENH_MAX_KEY_SIZE
;
2928 if (!unpad_data(pbDecrypted
, pPubKey
->dwBlockLen
, pbDecrypted
, &dwKeyLen
, dwFlags
)) {
2929 HeapFree(GetProcessHeap(), 0, pbDecrypted
);
2933 *phKey
= new_key(hProv
, pBlobHeader
->aiKeyAlg
, dwKeyLen
<<19, &pCryptKey
);
2934 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
2936 HeapFree(GetProcessHeap(), 0, pbDecrypted
);
2939 memcpy(pCryptKey
->abKeyValue
, pbDecrypted
, dwKeyLen
);
2940 HeapFree(GetProcessHeap(), 0, pbDecrypted
);
2941 setup_key(pCryptKey
);
2942 if (dwFlags
& CRYPT_EXPORTABLE
)
2943 pCryptKey
->dwPermissions
|= CRYPT_EXPORT
;
2947 /******************************************************************************
2948 * import_plaintext_key [Internal]
2950 * Import a plaintext key into a key container.
2953 * hProv [I] Key container into which the symmetric key is to be imported.
2954 * pbData [I] Pointer to a buffer which holds the plaintext key BLOB.
2955 * dwDataLen [I] Length of data in buffer at pbData.
2956 * dwFlags [I] One of:
2957 * CRYPT_EXPORTABLE: the imported key is marked exportable
2958 * phKey [O] Handle to the imported key.
2962 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2963 * it's a PLAINTEXTKEYBLOB.
2969 static BOOL
import_plaintext_key(HCRYPTPROV hProv
, CONST BYTE
*pbData
,
2970 DWORD dwDataLen
, DWORD dwFlags
,
2973 CRYPTKEY
*pCryptKey
;
2974 CONST BLOBHEADER
*pBlobHeader
= (CONST BLOBHEADER
*)pbData
;
2975 CONST DWORD
*pKeyLen
= (CONST DWORD
*)(pBlobHeader
+ 1);
2976 CONST BYTE
*pbKeyStream
= (CONST BYTE
*)(pKeyLen
+ 1);
2978 if (dwDataLen
< sizeof(BLOBHEADER
)+sizeof(DWORD
)+*pKeyLen
)
2980 SetLastError(NTE_BAD_DATA
); /* FIXME: error code */
2984 if (dwFlags
& CRYPT_IPSEC_HMAC_KEY
)
2986 *phKey
= new_key(hProv
, CALG_HMAC
, 0, &pCryptKey
);
2987 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
2989 if (*pKeyLen
<= RSAENH_MIN(sizeof(pCryptKey
->abKeyValue
), RSAENH_HMAC_BLOCK_LEN
))
2991 memcpy(pCryptKey
->abKeyValue
, pbKeyStream
, *pKeyLen
);
2992 pCryptKey
->dwKeyLen
= *pKeyLen
;
2996 CRYPT_DATA_BLOB blobHmacKey
= { *pKeyLen
, (BYTE
*)pbKeyStream
};
2998 /* In order to initialize an HMAC key, the key material is hashed,
2999 * and the output of the hash function is used as the key material.
3000 * Unfortunately, the way the Crypto API is designed, we don't know
3001 * the hash algorithm yet, so we have to copy the entire key
3004 if (!copy_data_blob(&pCryptKey
->blobHmacKey
, &blobHmacKey
))
3006 release_handle(&handle_table
, *phKey
, RSAENH_MAGIC_KEY
);
3007 *phKey
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
3011 setup_key(pCryptKey
);
3012 if (dwFlags
& CRYPT_EXPORTABLE
)
3013 pCryptKey
->dwPermissions
|= CRYPT_EXPORT
;
3017 *phKey
= new_key(hProv
, pBlobHeader
->aiKeyAlg
, *pKeyLen
<<19, &pCryptKey
);
3018 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
3020 memcpy(pCryptKey
->abKeyValue
, pbKeyStream
, *pKeyLen
);
3021 setup_key(pCryptKey
);
3022 if (dwFlags
& CRYPT_EXPORTABLE
)
3023 pCryptKey
->dwPermissions
|= CRYPT_EXPORT
;
3028 /******************************************************************************
3029 * import_key [Internal]
3031 * Import a BLOB'ed key into a key container, optionally storing the key's
3032 * value to the registry.
3035 * hProv [I] Key container into which the key is to be imported.
3036 * pbData [I] Pointer to a buffer which holds the BLOB.
3037 * dwDataLen [I] Length of data in buffer at pbData.
3038 * hPubKey [I] Key used to decrypt sensitive BLOB data.
3039 * dwFlags [I] One of:
3040 * CRYPT_EXPORTABLE: the imported key is marked exportable
3041 * fStoreKey [I] If TRUE, the imported key is stored to the registry.
3042 * phKey [O] Handle to the imported key.
3048 static BOOL
import_key(HCRYPTPROV hProv
, CONST BYTE
*pbData
, DWORD dwDataLen
,
3049 HCRYPTKEY hPubKey
, DWORD dwFlags
, BOOL fStoreKey
,
3052 KEYCONTAINER
*pKeyContainer
;
3053 CONST BLOBHEADER
*pBlobHeader
= (CONST BLOBHEADER
*)pbData
;
3055 if (!lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
,
3056 (OBJECTHDR
**)&pKeyContainer
))
3058 SetLastError(NTE_BAD_UID
);
3062 if (dwDataLen
< sizeof(BLOBHEADER
) ||
3063 pBlobHeader
->bVersion
!= CUR_BLOB_VERSION
||
3064 pBlobHeader
->reserved
!= 0)
3066 TRACE("bVersion = %d, reserved = %d\n", pBlobHeader
->bVersion
,
3067 pBlobHeader
->reserved
);
3068 SetLastError(NTE_BAD_DATA
);
3072 /* If this is a verify-only context, the key is not persisted regardless of
3073 * fStoreKey's original value.
3075 fStoreKey
= fStoreKey
&& !(dwFlags
& CRYPT_VERIFYCONTEXT
);
3076 TRACE("blob type: %x\n", pBlobHeader
->bType
);
3077 switch (pBlobHeader
->bType
)
3079 case PRIVATEKEYBLOB
:
3080 return import_private_key(hProv
, pbData
, dwDataLen
, dwFlags
,
3084 return import_public_key(hProv
, pbData
, dwDataLen
, dwFlags
,
3088 return import_symmetric_key(hProv
, pbData
, dwDataLen
, hPubKey
,
3091 case PLAINTEXTKEYBLOB
:
3092 return import_plaintext_key(hProv
, pbData
, dwDataLen
, dwFlags
,
3096 SetLastError(NTE_BAD_TYPE
); /* FIXME: error code? */
3101 /******************************************************************************
3102 * CPImportKey (RSAENH.@)
3104 * Import a BLOB'ed key into a key container.
3107 * hProv [I] Key container into which the key is to be imported.
3108 * pbData [I] Pointer to a buffer which holds the BLOB.
3109 * dwDataLen [I] Length of data in buffer at pbData.
3110 * hPubKey [I] Key used to decrypt sensitive BLOB data.
3111 * dwFlags [I] One of:
3112 * CRYPT_EXPORTABLE: the imported key is marked exportable
3113 * phKey [O] Handle to the imported key.
3119 BOOL WINAPI
RSAENH_CPImportKey(HCRYPTPROV hProv
, CONST BYTE
*pbData
, DWORD dwDataLen
,
3120 HCRYPTKEY hPubKey
, DWORD dwFlags
, HCRYPTKEY
*phKey
)
3122 TRACE("(hProv=%08lx, pbData=%p, dwDataLen=%d, hPubKey=%08lx, dwFlags=%08x, phKey=%p)\n",
3123 hProv
, pbData
, dwDataLen
, hPubKey
, dwFlags
, phKey
);
3125 return import_key(hProv
, pbData
, dwDataLen
, hPubKey
, dwFlags
, TRUE
, phKey
);
3128 /******************************************************************************
3129 * CPGenKey (RSAENH.@)
3131 * Generate a key in the key container
3134 * hProv [I] Key container for which a key is to be generated.
3135 * Algid [I] Crypto algorithm identifier for the key to be generated.
3136 * dwFlags [I] Upper 16 bits: Binary length of key. Lower 16 bits: Flags. See Notes
3137 * phKey [O] Handle to the generated key.
3144 * Flags currently not considered.
3147 * Private key-exchange- and signature-keys can be generated with Algid AT_KEYEXCHANGE
3148 * and AT_SIGNATURE values.
3150 BOOL WINAPI
RSAENH_CPGenKey(HCRYPTPROV hProv
, ALG_ID Algid
, DWORD dwFlags
, HCRYPTKEY
*phKey
)
3152 KEYCONTAINER
*pKeyContainer
;
3153 CRYPTKEY
*pCryptKey
;
3155 TRACE("(hProv=%08lx, aiAlgid=%d, dwFlags=%08x, phKey=%p)\n", hProv
, Algid
, dwFlags
, phKey
);
3157 if (!lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
,
3158 (OBJECTHDR
**)&pKeyContainer
))
3160 /* MSDN: hProv not containing valid context handle */
3161 SetLastError(NTE_BAD_UID
);
3169 *phKey
= new_key(hProv
, CALG_RSA_SIGN
, dwFlags
, &pCryptKey
);
3171 new_key_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pCryptKey
->dwKeyLen
);
3172 setup_key(pCryptKey
);
3173 release_and_install_key(hProv
, *phKey
,
3174 &pKeyContainer
->hSignatureKeyPair
,
3179 case AT_KEYEXCHANGE
:
3181 *phKey
= new_key(hProv
, CALG_RSA_KEYX
, dwFlags
, &pCryptKey
);
3183 new_key_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pCryptKey
->dwKeyLen
);
3184 setup_key(pCryptKey
);
3185 release_and_install_key(hProv
, *phKey
,
3186 &pKeyContainer
->hKeyExchangeKeyPair
,
3200 case CALG_PCT1_MASTER
:
3201 case CALG_SSL2_MASTER
:
3202 case CALG_SSL3_MASTER
:
3203 case CALG_TLS1_MASTER
:
3204 *phKey
= new_key(hProv
, Algid
, dwFlags
, &pCryptKey
);
3206 gen_rand_impl(pCryptKey
->abKeyValue
, RSAENH_MAX_KEY_SIZE
);
3208 case CALG_SSL3_MASTER
:
3209 pCryptKey
->abKeyValue
[0] = RSAENH_SSL3_VERSION_MAJOR
;
3210 pCryptKey
->abKeyValue
[1] = RSAENH_SSL3_VERSION_MINOR
;
3213 case CALG_TLS1_MASTER
:
3214 pCryptKey
->abKeyValue
[0] = RSAENH_TLS1_VERSION_MAJOR
;
3215 pCryptKey
->abKeyValue
[1] = RSAENH_TLS1_VERSION_MINOR
;
3218 setup_key(pCryptKey
);
3223 /* MSDN: Algorithm not supported specified by Algid */
3224 SetLastError(NTE_BAD_ALGID
);
3228 return *phKey
!= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
3231 /******************************************************************************
3232 * CPGenRandom (RSAENH.@)
3234 * Generate a random byte stream.
3237 * hProv [I] Key container that is used to generate random bytes.
3238 * dwLen [I] Specifies the number of requested random data bytes.
3239 * pbBuffer [O] Random bytes will be stored here.
3245 BOOL WINAPI
RSAENH_CPGenRandom(HCRYPTPROV hProv
, DWORD dwLen
, BYTE
*pbBuffer
)
3247 TRACE("(hProv=%08lx, dwLen=%d, pbBuffer=%p)\n", hProv
, dwLen
, pbBuffer
);
3249 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
3251 /* MSDN: hProv not containing valid context handle */
3252 SetLastError(NTE_BAD_UID
);
3256 return gen_rand_impl(pbBuffer
, dwLen
);
3259 /******************************************************************************
3260 * CPGetHashParam (RSAENH.@)
3262 * Query parameters of an hash object.
3265 * hProv [I] The kea container, which the hash belongs to.
3266 * hHash [I] The hash object that is to be queried.
3267 * dwParam [I] Specifies the parameter that is to be queried.
3268 * pbData [I] Pointer to the buffer where the parameter value will be stored.
3269 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
3270 * dwFlags [I] None currently defined.
3277 * Valid dwParams are: HP_ALGID, HP_HASHSIZE, HP_HASHVALUE. The hash will be
3278 * finalized if HP_HASHVALUE is queried.
3280 BOOL WINAPI
RSAENH_CPGetHashParam(HCRYPTPROV hProv
, HCRYPTHASH hHash
, DWORD dwParam
, BYTE
*pbData
,
3281 DWORD
*pdwDataLen
, DWORD dwFlags
)
3283 CRYPTHASH
*pCryptHash
;
3285 TRACE("(hProv=%08lx, hHash=%08lx, dwParam=%08x, pbData=%p, pdwDataLen=%p, dwFlags=%08x)\n",
3286 hProv
, hHash
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
3288 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
3290 SetLastError(NTE_BAD_UID
);
3296 SetLastError(NTE_BAD_FLAGS
);
3300 if (!lookup_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
,
3301 (OBJECTHDR
**)&pCryptHash
))
3303 SetLastError(NTE_BAD_HASH
);
3309 SetLastError(ERROR_INVALID_PARAMETER
);
3316 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&pCryptHash
->aiAlgid
,
3320 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&pCryptHash
->dwHashSize
,
3324 if (pCryptHash
->aiAlgid
== CALG_TLS1PRF
) {
3325 return tls1_prf(hProv
, pCryptHash
->hKey
, &pCryptHash
->tpPRFParams
.blobLabel
,
3326 &pCryptHash
->tpPRFParams
.blobSeed
, pbData
, *pdwDataLen
);
3329 if ( pbData
== NULL
) {
3330 *pdwDataLen
= pCryptHash
->dwHashSize
;
3334 if (pbData
&& (pCryptHash
->dwState
!= RSAENH_HASHSTATE_FINISHED
))
3336 finalize_hash(pCryptHash
);
3337 pCryptHash
->dwState
= RSAENH_HASHSTATE_FINISHED
;
3340 return copy_param(pbData
, pdwDataLen
, pCryptHash
->abHashValue
,
3341 pCryptHash
->dwHashSize
);
3344 SetLastError(NTE_BAD_TYPE
);
3349 /******************************************************************************
3350 * CPSetKeyParam (RSAENH.@)
3352 * Set a parameter of a key object
3355 * hProv [I] The key container to which the key belongs.
3356 * hKey [I] The key for which a parameter is to be set.
3357 * dwParam [I] Parameter type. See Notes.
3358 * pbData [I] Pointer to the parameter value.
3359 * dwFlags [I] Currently none defined.
3366 * Defined dwParam types are:
3367 * - KP_MODE: Values MODE_CBC, MODE_ECB, MODE_CFB.
3368 * - KP_MODE_BITS: Shift width for cipher feedback mode. (Currently ignored by MS CSP's)
3369 * - KP_PERMISSIONS: Or'ed combination of CRYPT_ENCRYPT, CRYPT_DECRYPT,
3370 * CRYPT_EXPORT, CRYPT_READ, CRYPT_WRITE, CRYPT_MAC
3371 * - KP_IV: Initialization vector
3373 BOOL WINAPI
RSAENH_CPSetKeyParam(HCRYPTPROV hProv
, HCRYPTKEY hKey
, DWORD dwParam
, BYTE
*pbData
,
3376 CRYPTKEY
*pCryptKey
;
3378 TRACE("(hProv=%08lx, hKey=%08lx, dwParam=%08x, pbData=%p, dwFlags=%08x)\n", hProv
, hKey
,
3379 dwParam
, pbData
, dwFlags
);
3381 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
3383 SetLastError(NTE_BAD_UID
);
3388 SetLastError(NTE_BAD_FLAGS
);
3392 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
3394 SetLastError(NTE_BAD_KEY
);
3400 /* The MS providers only support PKCS5_PADDING */
3401 if (*(DWORD
*)pbData
!= PKCS5_PADDING
) {
3402 SetLastError(NTE_BAD_DATA
);
3408 pCryptKey
->dwMode
= *(DWORD
*)pbData
;
3412 pCryptKey
->dwModeBits
= *(DWORD
*)pbData
;
3415 case KP_PERMISSIONS
:
3417 DWORD perms
= *(DWORD
*)pbData
;
3419 if ((perms
& CRYPT_EXPORT
) &&
3420 !(pCryptKey
->dwPermissions
& CRYPT_EXPORT
))
3422 SetLastError(NTE_BAD_DATA
);
3425 else if (!(perms
& CRYPT_EXPORT
) &&
3426 (pCryptKey
->dwPermissions
& CRYPT_EXPORT
))
3428 /* Clearing the export permission appears to be ignored,
3431 perms
|= CRYPT_EXPORT
;
3433 pCryptKey
->dwPermissions
= perms
;
3438 memcpy(pCryptKey
->abInitVector
, pbData
, pCryptKey
->dwBlockLen
);
3439 setup_key(pCryptKey
);
3443 switch (pCryptKey
->aiAlgid
) {
3448 SetLastError(ERROR_INVALID_PARAMETER
);
3451 /* MSDN: the base provider always sets eleven bytes of
3454 memcpy(pCryptKey
->abKeyValue
+ pCryptKey
->dwKeyLen
,
3456 pCryptKey
->dwSaltLen
= 11;
3457 setup_key(pCryptKey
);
3458 /* Strange but true: salt length reset to 0 after setting
3461 pCryptKey
->dwSaltLen
= 0;
3464 SetLastError(NTE_BAD_KEY
);
3471 CRYPT_INTEGER_BLOB
*blob
= (CRYPT_INTEGER_BLOB
*)pbData
;
3473 /* salt length can't be greater than 184 bits = 24 bytes */
3474 if (blob
->cbData
> 24)
3476 SetLastError(NTE_BAD_DATA
);
3479 memcpy(pCryptKey
->abKeyValue
+ pCryptKey
->dwKeyLen
, blob
->pbData
,
3481 pCryptKey
->dwSaltLen
= blob
->cbData
;
3482 setup_key(pCryptKey
);
3486 case KP_EFFECTIVE_KEYLEN
:
3487 switch (pCryptKey
->aiAlgid
) {
3491 SetLastError(ERROR_INVALID_PARAMETER
);
3494 else if (!*(DWORD
*)pbData
|| *(DWORD
*)pbData
> 1024)
3496 SetLastError(NTE_BAD_DATA
);
3501 pCryptKey
->dwEffectiveKeyLen
= *(DWORD
*)pbData
;
3502 setup_key(pCryptKey
);
3506 SetLastError(NTE_BAD_TYPE
);
3511 case KP_SCHANNEL_ALG
:
3512 switch (((PSCHANNEL_ALG
)pbData
)->dwUse
) {
3513 case SCHANNEL_ENC_KEY
:
3514 memcpy(&pCryptKey
->siSChannelInfo
.saEncAlg
, pbData
, sizeof(SCHANNEL_ALG
));
3517 case SCHANNEL_MAC_KEY
:
3518 memcpy(&pCryptKey
->siSChannelInfo
.saMACAlg
, pbData
, sizeof(SCHANNEL_ALG
));
3522 SetLastError(NTE_FAIL
); /* FIXME: error code */
3527 case KP_CLIENT_RANDOM
:
3528 return copy_data_blob(&pCryptKey
->siSChannelInfo
.blobClientRandom
, (PCRYPT_DATA_BLOB
)pbData
);
3530 case KP_SERVER_RANDOM
:
3531 return copy_data_blob(&pCryptKey
->siSChannelInfo
.blobServerRandom
, (PCRYPT_DATA_BLOB
)pbData
);
3534 SetLastError(NTE_BAD_TYPE
);
3539 /******************************************************************************
3540 * CPGetKeyParam (RSAENH.@)
3542 * Query a key parameter.
3545 * hProv [I] The key container, which the key belongs to.
3546 * hHash [I] The key object that is to be queried.
3547 * dwParam [I] Specifies the parameter that is to be queried.
3548 * pbData [I] Pointer to the buffer where the parameter value will be stored.
3549 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
3550 * dwFlags [I] None currently defined.
3557 * Defined dwParam types are:
3558 * - KP_MODE: Values MODE_CBC, MODE_ECB, MODE_CFB.
3559 * - KP_MODE_BITS: Shift width for cipher feedback mode.
3560 * (Currently ignored by MS CSP's - always eight)
3561 * - KP_PERMISSIONS: Or'ed combination of CRYPT_ENCRYPT, CRYPT_DECRYPT,
3562 * CRYPT_EXPORT, CRYPT_READ, CRYPT_WRITE, CRYPT_MAC
3563 * - KP_IV: Initialization vector.
3564 * - KP_KEYLEN: Bitwidth of the key.
3565 * - KP_BLOCKLEN: Size of a block cipher block.
3566 * - KP_SALT: Salt value.
3568 BOOL WINAPI
RSAENH_CPGetKeyParam(HCRYPTPROV hProv
, HCRYPTKEY hKey
, DWORD dwParam
, BYTE
*pbData
,
3569 DWORD
*pdwDataLen
, DWORD dwFlags
)
3571 CRYPTKEY
*pCryptKey
;
3574 TRACE("(hProv=%08lx, hKey=%08lx, dwParam=%08x, pbData=%p, pdwDataLen=%p dwFlags=%08x)\n",
3575 hProv
, hKey
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
3577 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
3579 SetLastError(NTE_BAD_UID
);
3584 SetLastError(NTE_BAD_FLAGS
);
3588 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
3590 SetLastError(NTE_BAD_KEY
);
3597 return copy_param(pbData
, pdwDataLen
, pCryptKey
->abInitVector
,
3598 pCryptKey
->dwBlockLen
);
3601 switch (pCryptKey
->aiAlgid
) {
3604 return copy_param(pbData
, pdwDataLen
,
3605 &pCryptKey
->abKeyValue
[pCryptKey
->dwKeyLen
],
3606 pCryptKey
->dwSaltLen
);
3608 SetLastError(NTE_BAD_KEY
);
3613 dwValue
= PKCS5_PADDING
;
3614 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwValue
, sizeof(DWORD
));
3617 dwValue
= pCryptKey
->dwKeyLen
<< 3;
3618 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwValue
, sizeof(DWORD
));
3620 case KP_EFFECTIVE_KEYLEN
:
3621 if (pCryptKey
->dwEffectiveKeyLen
)
3622 dwValue
= pCryptKey
->dwEffectiveKeyLen
;
3624 dwValue
= pCryptKey
->dwKeyLen
<< 3;
3625 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwValue
, sizeof(DWORD
));
3628 dwValue
= pCryptKey
->dwBlockLen
<< 3;
3629 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwValue
, sizeof(DWORD
));
3632 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&pCryptKey
->dwMode
, sizeof(DWORD
));
3635 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&pCryptKey
->dwModeBits
,
3638 case KP_PERMISSIONS
:
3639 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&pCryptKey
->dwPermissions
,
3643 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&pCryptKey
->aiAlgid
, sizeof(DWORD
));
3646 SetLastError(NTE_BAD_TYPE
);
3651 /******************************************************************************
3652 * CPGetProvParam (RSAENH.@)
3654 * Query a CSP parameter.
3657 * hProv [I] The key container that is to be queried.
3658 * dwParam [I] Specifies the parameter that is to be queried.
3659 * pbData [I] Pointer to the buffer where the parameter value will be stored.
3660 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
3661 * dwFlags [I] CRYPT_FIRST: Start enumeration (for PP_ENUMALGS{_EX}).
3667 * Defined dwParam types:
3668 * - PP_CONTAINER: Name of the key container.
3669 * - PP_NAME: Name of the cryptographic service provider.
3670 * - PP_SIG_KEYSIZE_INC: RSA signature keywidth granularity in bits.
3671 * - PP_KEYX_KEYSIZE_INC: RSA key-exchange keywidth granularity in bits.
3672 * - PP_ENUMALGS{_EX}: Query provider capabilities.
3674 BOOL WINAPI
RSAENH_CPGetProvParam(HCRYPTPROV hProv
, DWORD dwParam
, BYTE
*pbData
,
3675 DWORD
*pdwDataLen
, DWORD dwFlags
)
3677 KEYCONTAINER
*pKeyContainer
;
3678 PROV_ENUMALGS provEnumalgs
;
3682 /* This is for dwParam PP_CRYPT_COUNT_KEY_USE.
3683 * IE6 SP1 asks for it in the 'About' dialog.
3684 * Returning this BLOB seems to satisfy IE. The marked 0x00 seem
3685 * to be 'don't care's. If you know anything more specific about
3686 * this provider parameter, please report to wine-devel@winehq.org */
3687 static CONST BYTE abWTF
[96] = {
3688 0xb0, 0x25, 0x63, 0x86, 0x9c, 0xab, 0xb6, 0x37,
3689 0xe8, 0x82, /**/0x00,/**/ 0x72, 0x06, 0xb2, /**/0x00,/**/ 0x3b,
3690 0x60, 0x35, /**/0x00,/**/ 0x3b, 0x88, 0xce, /**/0x00,/**/ 0x82,
3691 0xbc, 0x7a, /**/0x00,/**/ 0xb7, 0x4f, 0x7e, /**/0x00,/**/ 0xde,
3692 0x92, 0xf1, /**/0x00,/**/ 0x83, 0xea, 0x5e, /**/0x00,/**/ 0xc8,
3693 0x12, 0x1e, 0xd4, 0x06, 0xf7, 0x66, /**/0x00,/**/ 0x01,
3694 0x29, 0xa4, /**/0x00,/**/ 0xf8, 0x24, 0x0c, /**/0x00,/**/ 0x33,
3695 0x06, 0x80, /**/0x00,/**/ 0x02, 0x46, 0x0b, /**/0x00,/**/ 0x6d,
3696 0x5b, 0xca, /**/0x00,/**/ 0x9a, 0x10, 0xf0, /**/0x00,/**/ 0x05,
3697 0x19, 0xd0, /**/0x00,/**/ 0x2c, 0xf6, 0x27, /**/0x00,/**/ 0xaa,
3698 0x7c, 0x6f, /**/0x00,/**/ 0xb9, 0xd8, 0x72, /**/0x00,/**/ 0x03,
3699 0xf3, 0x81, /**/0x00,/**/ 0xfa, 0xe8, 0x26, /**/0x00,/**/ 0xca
3702 TRACE("(hProv=%08lx, dwParam=%08x, pbData=%p, pdwDataLen=%p, dwFlags=%08x)\n",
3703 hProv
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
3706 SetLastError(ERROR_INVALID_PARAMETER
);
3710 if (!lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
,
3711 (OBJECTHDR
**)&pKeyContainer
))
3713 /* MSDN: hProv not containing valid context handle */
3714 SetLastError(NTE_BAD_UID
);
3721 case PP_UNIQUE_CONTAINER
:/* MSDN says we can return the same value as PP_CONTAINER */
3722 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)pKeyContainer
->szName
,
3723 strlen(pKeyContainer
->szName
)+1);
3726 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)pKeyContainer
->szProvName
,
3727 strlen(pKeyContainer
->szProvName
)+1);
3730 dwTemp
= PROV_RSA_FULL
;
3731 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwTemp
, sizeof(dwTemp
));
3734 dwTemp
= AT_SIGNATURE
| AT_KEYEXCHANGE
;
3735 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwTemp
, sizeof(dwTemp
));
3737 case PP_KEYSET_TYPE
:
3738 dwTemp
= pKeyContainer
->dwFlags
& CRYPT_MACHINE_KEYSET
;
3739 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwTemp
, sizeof(dwTemp
));
3742 dwTemp
= CRYPT_SEC_DESCR
;
3743 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwTemp
, sizeof(dwTemp
));
3745 case PP_SIG_KEYSIZE_INC
:
3746 case PP_KEYX_KEYSIZE_INC
:
3748 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwTemp
, sizeof(dwTemp
));
3751 dwTemp
= CRYPT_IMPL_SOFTWARE
;
3752 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwTemp
, sizeof(dwTemp
));
3755 dwTemp
= 0x00000200;
3756 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwTemp
, sizeof(dwTemp
));
3758 case PP_ENUMCONTAINERS
:
3759 if ((dwFlags
& CRYPT_FIRST
) == CRYPT_FIRST
) pKeyContainer
->dwEnumContainersCtr
= 0;
3762 *pdwDataLen
= (DWORD
)MAX_PATH
+ 1;
3766 if (!open_container_key("", dwFlags
, &hKey
))
3768 SetLastError(ERROR_NO_MORE_ITEMS
);
3772 dwTemp
= *pdwDataLen
;
3773 switch (RegEnumKeyExA(hKey
, pKeyContainer
->dwEnumContainersCtr
, (LPSTR
)pbData
, &dwTemp
,
3774 NULL
, NULL
, NULL
, NULL
))
3776 case ERROR_MORE_DATA
:
3777 *pdwDataLen
= (DWORD
)MAX_PATH
+ 1;
3780 pKeyContainer
->dwEnumContainersCtr
++;
3784 case ERROR_NO_MORE_ITEMS
:
3786 SetLastError(ERROR_NO_MORE_ITEMS
);
3792 case PP_ENUMALGS_EX
:
3793 if (((pKeyContainer
->dwEnumAlgsCtr
>= RSAENH_MAX_ENUMALGS
-1) ||
3794 (!aProvEnumAlgsEx
[pKeyContainer
->dwPersonality
]
3795 [pKeyContainer
->dwEnumAlgsCtr
+1].aiAlgid
)) &&
3796 ((dwFlags
& CRYPT_FIRST
) != CRYPT_FIRST
))
3798 SetLastError(ERROR_NO_MORE_ITEMS
);
3802 if (dwParam
== PP_ENUMALGS
) {
3803 if (pbData
&& (*pdwDataLen
>= sizeof(PROV_ENUMALGS
)))
3804 pKeyContainer
->dwEnumAlgsCtr
= ((dwFlags
& CRYPT_FIRST
) == CRYPT_FIRST
) ?
3805 0 : pKeyContainer
->dwEnumAlgsCtr
+1;
3807 provEnumalgs
.aiAlgid
= aProvEnumAlgsEx
3808 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
].aiAlgid
;
3809 provEnumalgs
.dwBitLen
= aProvEnumAlgsEx
3810 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
].dwDefaultLen
;
3811 provEnumalgs
.dwNameLen
= aProvEnumAlgsEx
3812 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
].dwNameLen
;
3813 memcpy(provEnumalgs
.szName
, aProvEnumAlgsEx
3814 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
].szName
,
3817 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&provEnumalgs
,
3818 sizeof(PROV_ENUMALGS
));
3820 if (pbData
&& (*pdwDataLen
>= sizeof(PROV_ENUMALGS_EX
)))
3821 pKeyContainer
->dwEnumAlgsCtr
= ((dwFlags
& CRYPT_FIRST
) == CRYPT_FIRST
) ?
3822 0 : pKeyContainer
->dwEnumAlgsCtr
+1;
3824 return copy_param(pbData
, pdwDataLen
,
3825 (CONST BYTE
*)&aProvEnumAlgsEx
3826 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
],
3827 sizeof(PROV_ENUMALGS_EX
));
3830 case PP_CRYPT_COUNT_KEY_USE
: /* Asked for by IE About dialog */
3831 return copy_param(pbData
, pdwDataLen
, abWTF
, sizeof(abWTF
));
3834 /* MSDN: Unknown parameter number in dwParam */
3835 SetLastError(NTE_BAD_TYPE
);
3840 /******************************************************************************
3841 * CPDeriveKey (RSAENH.@)
3843 * Derives a key from a hash value.
3846 * hProv [I] Key container for which a key is to be generated.
3847 * Algid [I] Crypto algorithm identifier for the key to be generated.
3848 * hBaseData [I] Hash from whose value the key will be derived.
3849 * dwFlags [I] See Notes.
3850 * phKey [O] The generated key.
3858 * - CRYPT_EXPORTABLE: Key can be exported.
3859 * - CRYPT_NO_SALT: No salt is used for 40 bit keys.
3860 * - CRYPT_CREATE_SALT: Use remaining bits as salt value.
3862 BOOL WINAPI
RSAENH_CPDeriveKey(HCRYPTPROV hProv
, ALG_ID Algid
, HCRYPTHASH hBaseData
,
3863 DWORD dwFlags
, HCRYPTKEY
*phKey
)
3865 CRYPTKEY
*pCryptKey
, *pMasterKey
;
3866 CRYPTHASH
*pCryptHash
;
3867 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
*2];
3870 TRACE("(hProv=%08lx, Algid=%d, hBaseData=%08lx, dwFlags=%08x phKey=%p)\n", hProv
, Algid
,
3871 hBaseData
, dwFlags
, phKey
);
3873 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
3875 SetLastError(NTE_BAD_UID
);
3879 if (!lookup_handle(&handle_table
, hBaseData
, RSAENH_MAGIC_HASH
,
3880 (OBJECTHDR
**)&pCryptHash
))
3882 SetLastError(NTE_BAD_HASH
);
3888 SetLastError(ERROR_INVALID_PARAMETER
);
3892 switch (GET_ALG_CLASS(Algid
))
3894 case ALG_CLASS_DATA_ENCRYPT
:
3895 *phKey
= new_key(hProv
, Algid
, dwFlags
, &pCryptKey
);
3896 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
3899 * We derive the key material from the hash.
3900 * If the hash value is not large enough for the claimed key, we have to construct
3901 * a larger binary value based on the hash. This is documented in MSDN: CryptDeriveKey.
3903 dwLen
= RSAENH_MAX_HASH_SIZE
;
3904 RSAENH_CPGetHashParam(pCryptHash
->hProv
, hBaseData
, HP_HASHVAL
, abHashValue
, &dwLen
, 0);
3906 if (dwLen
< pCryptKey
->dwKeyLen
) {
3907 BYTE pad1
[RSAENH_HMAC_DEF_PAD_LEN
], pad2
[RSAENH_HMAC_DEF_PAD_LEN
];
3908 BYTE old_hashval
[RSAENH_MAX_HASH_SIZE
];
3911 memcpy(old_hashval
, pCryptHash
->abHashValue
, RSAENH_MAX_HASH_SIZE
);
3913 for (i
=0; i
<RSAENH_HMAC_DEF_PAD_LEN
; i
++) {
3914 pad1
[i
] = RSAENH_HMAC_DEF_IPAD_CHAR
^ (i
<dwLen
? abHashValue
[i
] : 0);
3915 pad2
[i
] = RSAENH_HMAC_DEF_OPAD_CHAR
^ (i
<dwLen
? abHashValue
[i
] : 0);
3918 init_hash(pCryptHash
);
3919 update_hash(pCryptHash
, pad1
, RSAENH_HMAC_DEF_PAD_LEN
);
3920 finalize_hash(pCryptHash
);
3921 memcpy(abHashValue
, pCryptHash
->abHashValue
, pCryptHash
->dwHashSize
);
3923 init_hash(pCryptHash
);
3924 update_hash(pCryptHash
, pad2
, RSAENH_HMAC_DEF_PAD_LEN
);
3925 finalize_hash(pCryptHash
);
3926 memcpy(abHashValue
+pCryptHash
->dwHashSize
, pCryptHash
->abHashValue
,
3927 pCryptHash
->dwHashSize
);
3929 memcpy(pCryptHash
->abHashValue
, old_hashval
, RSAENH_MAX_HASH_SIZE
);
3932 memcpy(pCryptKey
->abKeyValue
, abHashValue
,
3933 RSAENH_MIN(pCryptKey
->dwKeyLen
, sizeof(pCryptKey
->abKeyValue
)));
3936 case ALG_CLASS_MSG_ENCRYPT
:
3937 if (!lookup_handle(&handle_table
, pCryptHash
->hKey
, RSAENH_MAGIC_KEY
,
3938 (OBJECTHDR
**)&pMasterKey
))
3940 SetLastError(NTE_FAIL
); /* FIXME error code */
3946 /* See RFC 2246, chapter 6.3 Key calculation */
3947 case CALG_SCHANNEL_ENC_KEY
:
3948 if (!pMasterKey
->siSChannelInfo
.saEncAlg
.Algid
||
3949 !pMasterKey
->siSChannelInfo
.saEncAlg
.cBits
)
3951 SetLastError(NTE_BAD_FLAGS
);
3954 *phKey
= new_key(hProv
, pMasterKey
->siSChannelInfo
.saEncAlg
.Algid
,
3955 MAKELONG(LOWORD(dwFlags
),pMasterKey
->siSChannelInfo
.saEncAlg
.cBits
),
3957 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
3958 memcpy(pCryptKey
->abKeyValue
,
3959 pCryptHash
->abHashValue
+ (
3960 2 * (pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
/ 8) +
3961 ((dwFlags
& CRYPT_SERVER
) ?
3962 (pMasterKey
->siSChannelInfo
.saEncAlg
.cBits
/ 8) : 0)),
3963 pMasterKey
->siSChannelInfo
.saEncAlg
.cBits
/ 8);
3964 memcpy(pCryptKey
->abInitVector
,
3965 pCryptHash
->abHashValue
+ (
3966 2 * (pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
/ 8) +
3967 2 * (pMasterKey
->siSChannelInfo
.saEncAlg
.cBits
/ 8) +
3968 ((dwFlags
& CRYPT_SERVER
) ? pCryptKey
->dwBlockLen
: 0)),
3969 pCryptKey
->dwBlockLen
);
3972 case CALG_SCHANNEL_MAC_KEY
:
3973 *phKey
= new_key(hProv
, Algid
,
3974 MAKELONG(LOWORD(dwFlags
),pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
),
3976 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
3977 memcpy(pCryptKey
->abKeyValue
,
3978 pCryptHash
->abHashValue
+ ((dwFlags
& CRYPT_SERVER
) ?
3979 pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
/ 8 : 0),
3980 pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
/ 8);
3984 SetLastError(NTE_BAD_ALGID
);
3990 SetLastError(NTE_BAD_ALGID
);
3994 setup_key(pCryptKey
);
3998 /******************************************************************************
3999 * CPGetUserKey (RSAENH.@)
4001 * Returns a handle to the user's private key-exchange- or signature-key.
4004 * hProv [I] The key container from which a user key is requested.
4005 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
4006 * phUserKey [O] Handle to the requested key or INVALID_HANDLE_VALUE in case of failure.
4013 * A newly created key container does not contain private user key. Create them with CPGenKey.
4015 BOOL WINAPI
RSAENH_CPGetUserKey(HCRYPTPROV hProv
, DWORD dwKeySpec
, HCRYPTKEY
*phUserKey
)
4017 KEYCONTAINER
*pKeyContainer
;
4019 TRACE("(hProv=%08lx, dwKeySpec=%08x, phUserKey=%p)\n", hProv
, dwKeySpec
, phUserKey
);
4021 if (!lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
,
4022 (OBJECTHDR
**)&pKeyContainer
))
4024 /* MSDN: hProv not containing valid context handle */
4025 SetLastError(NTE_BAD_UID
);
4031 case AT_KEYEXCHANGE
:
4032 copy_handle(&handle_table
, pKeyContainer
->hKeyExchangeKeyPair
, RSAENH_MAGIC_KEY
,
4037 copy_handle(&handle_table
, pKeyContainer
->hSignatureKeyPair
, RSAENH_MAGIC_KEY
,
4042 *phUserKey
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
4045 if (*phUserKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
4047 /* MSDN: dwKeySpec parameter specifies nonexistent key */
4048 SetLastError(NTE_NO_KEY
);
4055 /******************************************************************************
4056 * CPHashData (RSAENH.@)
4058 * Updates a hash object with the given data.
4061 * hProv [I] Key container to which the hash object belongs.
4062 * hHash [I] Hash object which is to be updated.
4063 * pbData [I] Pointer to data with which the hash object is to be updated.
4064 * dwDataLen [I] Length of the data.
4065 * dwFlags [I] Currently none defined.
4072 * The actual hash value is queried with CPGetHashParam, which will finalize
4073 * the hash. Updating a finalized hash will fail with a last error NTE_BAD_HASH_STATE.
4075 BOOL WINAPI
RSAENH_CPHashData(HCRYPTPROV hProv
, HCRYPTHASH hHash
, CONST BYTE
*pbData
,
4076 DWORD dwDataLen
, DWORD dwFlags
)
4078 CRYPTHASH
*pCryptHash
;
4080 TRACE("(hProv=%08lx, hHash=%08lx, pbData=%p, dwDataLen=%d, dwFlags=%08x)\n",
4081 hProv
, hHash
, pbData
, dwDataLen
, dwFlags
);
4085 SetLastError(NTE_BAD_FLAGS
);
4089 if (!lookup_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
,
4090 (OBJECTHDR
**)&pCryptHash
))
4092 SetLastError(NTE_BAD_HASH
);
4096 if (!get_algid_info(hProv
, pCryptHash
->aiAlgid
) || pCryptHash
->aiAlgid
== CALG_SSL3_SHAMD5
)
4098 SetLastError(NTE_BAD_ALGID
);
4102 if (pCryptHash
->dwState
!= RSAENH_HASHSTATE_HASHING
)
4104 SetLastError(NTE_BAD_HASH_STATE
);
4108 update_hash(pCryptHash
, pbData
, dwDataLen
);
4112 /******************************************************************************
4113 * CPHashSessionKey (RSAENH.@)
4115 * Updates a hash object with the binary representation of a symmetric key.
4118 * hProv [I] Key container to which the hash object belongs.
4119 * hHash [I] Hash object which is to be updated.
4120 * hKey [I] The symmetric key, whose binary value will be added to the hash.
4121 * dwFlags [I] CRYPT_LITTLE_ENDIAN, if the binary key value shall be interpreted as little endian.
4127 BOOL WINAPI
RSAENH_CPHashSessionKey(HCRYPTPROV hProv
, HCRYPTHASH hHash
, HCRYPTKEY hKey
,
4130 BYTE abKeyValue
[RSAENH_MAX_KEY_SIZE
], bTemp
;
4134 TRACE("(hProv=%08lx, hHash=%08lx, hKey=%08lx, dwFlags=%08x)\n", hProv
, hHash
, hKey
, dwFlags
);
4136 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pKey
) ||
4137 (GET_ALG_CLASS(pKey
->aiAlgid
) != ALG_CLASS_DATA_ENCRYPT
))
4139 SetLastError(NTE_BAD_KEY
);
4143 if (dwFlags
& ~CRYPT_LITTLE_ENDIAN
) {
4144 SetLastError(NTE_BAD_FLAGS
);
4148 memcpy(abKeyValue
, pKey
->abKeyValue
, pKey
->dwKeyLen
);
4149 if (!(dwFlags
& CRYPT_LITTLE_ENDIAN
)) {
4150 for (i
=0; i
<pKey
->dwKeyLen
/2; i
++) {
4151 bTemp
= abKeyValue
[i
];
4152 abKeyValue
[i
] = abKeyValue
[pKey
->dwKeyLen
-i
-1];
4153 abKeyValue
[pKey
->dwKeyLen
-i
-1] = bTemp
;
4157 return RSAENH_CPHashData(hProv
, hHash
, abKeyValue
, pKey
->dwKeyLen
, 0);
4160 /******************************************************************************
4161 * CPReleaseContext (RSAENH.@)
4163 * Release a key container.
4166 * hProv [I] Key container to be released.
4167 * dwFlags [I] Currently none defined.
4173 BOOL WINAPI
RSAENH_CPReleaseContext(HCRYPTPROV hProv
, DWORD dwFlags
)
4175 TRACE("(hProv=%08lx, dwFlags=%08x)\n", hProv
, dwFlags
);
4177 if (!release_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
4179 /* MSDN: hProv not containing valid context handle */
4180 SetLastError(NTE_BAD_UID
);
4185 SetLastError(NTE_BAD_FLAGS
);
4192 /******************************************************************************
4193 * CPSetHashParam (RSAENH.@)
4195 * Set a parameter of a hash object
4198 * hProv [I] The key container to which the key belongs.
4199 * hHash [I] The hash object for which a parameter is to be set.
4200 * dwParam [I] Parameter type. See Notes.
4201 * pbData [I] Pointer to the parameter value.
4202 * dwFlags [I] Currently none defined.
4209 * Currently only the HP_HMAC_INFO dwParam type is defined.
4210 * The HMAC_INFO struct will be deep copied into the hash object.
4211 * See Internet RFC 2104 for details on the HMAC algorithm.
4213 BOOL WINAPI
RSAENH_CPSetHashParam(HCRYPTPROV hProv
, HCRYPTHASH hHash
, DWORD dwParam
,
4214 BYTE
*pbData
, DWORD dwFlags
)
4216 CRYPTHASH
*pCryptHash
;
4217 CRYPTKEY
*pCryptKey
;
4220 TRACE("(hProv=%08lx, hHash=%08lx, dwParam=%08x, pbData=%p, dwFlags=%08x)\n",
4221 hProv
, hHash
, dwParam
, pbData
, dwFlags
);
4223 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
4225 SetLastError(NTE_BAD_UID
);
4230 SetLastError(NTE_BAD_FLAGS
);
4234 if (!lookup_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
,
4235 (OBJECTHDR
**)&pCryptHash
))
4237 SetLastError(NTE_BAD_HASH
);
4243 free_hmac_info(pCryptHash
->pHMACInfo
);
4244 if (!copy_hmac_info(&pCryptHash
->pHMACInfo
, (PHMAC_INFO
)pbData
)) return FALSE
;
4246 if (!lookup_handle(&handle_table
, pCryptHash
->hKey
, RSAENH_MAGIC_KEY
,
4247 (OBJECTHDR
**)&pCryptKey
))
4249 SetLastError(NTE_FAIL
); /* FIXME: correct error code? */
4253 if (pCryptKey
->aiAlgid
== CALG_HMAC
&& !pCryptKey
->dwKeyLen
) {
4254 HCRYPTHASH hKeyHash
;
4257 if (!RSAENH_CPCreateHash(hProv
, ((PHMAC_INFO
)pbData
)->HashAlgid
, 0, 0,
4260 if (!RSAENH_CPHashData(hProv
, hKeyHash
, pCryptKey
->blobHmacKey
.pbData
,
4261 pCryptKey
->blobHmacKey
.cbData
, 0))
4263 RSAENH_CPDestroyHash(hProv
, hKeyHash
);
4266 keyLen
= sizeof(pCryptKey
->abKeyValue
);
4267 if (!RSAENH_CPGetHashParam(hProv
, hKeyHash
, HP_HASHVAL
, pCryptKey
->abKeyValue
,
4270 RSAENH_CPDestroyHash(hProv
, hKeyHash
);
4273 pCryptKey
->dwKeyLen
= keyLen
;
4274 RSAENH_CPDestroyHash(hProv
, hKeyHash
);
4276 for (i
=0; i
<RSAENH_MIN(pCryptKey
->dwKeyLen
,pCryptHash
->pHMACInfo
->cbInnerString
); i
++) {
4277 pCryptHash
->pHMACInfo
->pbInnerString
[i
] ^= pCryptKey
->abKeyValue
[i
];
4279 for (i
=0; i
<RSAENH_MIN(pCryptKey
->dwKeyLen
,pCryptHash
->pHMACInfo
->cbOuterString
); i
++) {
4280 pCryptHash
->pHMACInfo
->pbOuterString
[i
] ^= pCryptKey
->abKeyValue
[i
];
4283 init_hash(pCryptHash
);
4287 memcpy(pCryptHash
->abHashValue
, pbData
, pCryptHash
->dwHashSize
);
4288 pCryptHash
->dwState
= RSAENH_HASHSTATE_FINISHED
;
4291 case HP_TLS1PRF_SEED
:
4292 return copy_data_blob(&pCryptHash
->tpPRFParams
.blobSeed
, (PCRYPT_DATA_BLOB
)pbData
);
4294 case HP_TLS1PRF_LABEL
:
4295 return copy_data_blob(&pCryptHash
->tpPRFParams
.blobLabel
, (PCRYPT_DATA_BLOB
)pbData
);
4298 SetLastError(NTE_BAD_TYPE
);
4303 /******************************************************************************
4304 * CPSetProvParam (RSAENH.@)
4306 BOOL WINAPI
RSAENH_CPSetProvParam(HCRYPTPROV hProv
, DWORD dwParam
, BYTE
*pbData
, DWORD dwFlags
)
4312 /******************************************************************************
4313 * CPSignHash (RSAENH.@)
4315 * Sign a hash object
4318 * hProv [I] The key container, to which the hash object belongs.
4319 * hHash [I] The hash object to be signed.
4320 * dwKeySpec [I] AT_SIGNATURE or AT_KEYEXCHANGE: Key used to generate the signature.
4321 * sDescription [I] Should be NULL for security reasons.
4322 * dwFlags [I] 0, CRYPT_NOHASHOID or CRYPT_X931_FORMAT: Format of the signature.
4323 * pbSignature [O] Buffer, to which the signature will be stored. May be NULL to query SigLen.
4324 * pdwSigLen [I/O] Size of the buffer (in), Length of the signature (out)
4330 BOOL WINAPI
RSAENH_CPSignHash(HCRYPTPROV hProv
, HCRYPTHASH hHash
, DWORD dwKeySpec
,
4331 LPCWSTR sDescription
, DWORD dwFlags
, BYTE
*pbSignature
,
4334 HCRYPTKEY hCryptKey
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
4335 CRYPTKEY
*pCryptKey
;
4337 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
];
4341 TRACE("(hProv=%08lx, hHash=%08lx, dwKeySpec=%08x, sDescription=%s, dwFlags=%08x, "
4342 "pbSignature=%p, pdwSigLen=%p)\n", hProv
, hHash
, dwKeySpec
, debugstr_w(sDescription
),
4343 dwFlags
, pbSignature
, pdwSigLen
);
4345 if (dwFlags
& ~(CRYPT_NOHASHOID
|CRYPT_X931_FORMAT
)) {
4346 SetLastError(NTE_BAD_FLAGS
);
4350 if (!RSAENH_CPGetUserKey(hProv
, dwKeySpec
, &hCryptKey
)) return FALSE
;
4352 if (!lookup_handle(&handle_table
, hCryptKey
, RSAENH_MAGIC_KEY
,
4353 (OBJECTHDR
**)&pCryptKey
))
4355 SetLastError(NTE_NO_KEY
);
4360 *pdwSigLen
= pCryptKey
->dwKeyLen
;
4364 if (pCryptKey
->dwKeyLen
> *pdwSigLen
)
4366 SetLastError(ERROR_MORE_DATA
);
4367 *pdwSigLen
= pCryptKey
->dwKeyLen
;
4370 *pdwSigLen
= pCryptKey
->dwKeyLen
;
4373 if (!RSAENH_CPHashData(hProv
, hHash
, (CONST BYTE
*)sDescription
,
4374 (DWORD
)lstrlenW(sDescription
)*sizeof(WCHAR
), 0))
4380 dwHashLen
= sizeof(DWORD
);
4381 if (!RSAENH_CPGetHashParam(hProv
, hHash
, HP_ALGID
, (BYTE
*)&aiAlgid
, &dwHashLen
, 0)) goto out
;
4383 dwHashLen
= RSAENH_MAX_HASH_SIZE
;
4384 if (!RSAENH_CPGetHashParam(hProv
, hHash
, HP_HASHVAL
, abHashValue
, &dwHashLen
, 0)) goto out
;
4387 if (!build_hash_signature(pbSignature
, *pdwSigLen
, aiAlgid
, abHashValue
, dwHashLen
, dwFlags
)) {
4391 ret
= encrypt_block_impl(pCryptKey
->aiAlgid
, PK_PRIVATE
, &pCryptKey
->context
, pbSignature
, pbSignature
, RSAENH_ENCRYPT
);
4393 RSAENH_CPDestroyKey(hProv
, hCryptKey
);
4397 /******************************************************************************
4398 * CPVerifySignature (RSAENH.@)
4400 * Verify the signature of a hash object.
4403 * hProv [I] The key container, to which the hash belongs.
4404 * hHash [I] The hash for which the signature is verified.
4405 * pbSignature [I] The binary signature.
4406 * dwSigLen [I] Length of the signature BLOB.
4407 * hPubKey [I] Public key used to verify the signature.
4408 * sDescription [I] Should be NULL for security reasons.
4409 * dwFlags [I] 0, CRYPT_NOHASHOID or CRYPT_X931_FORMAT: Format of the signature.
4412 * Success: TRUE (Signature is valid)
4413 * Failure: FALSE (GetLastError() == NTE_BAD_SIGNATURE, if signature is invalid)
4415 BOOL WINAPI
RSAENH_CPVerifySignature(HCRYPTPROV hProv
, HCRYPTHASH hHash
, CONST BYTE
*pbSignature
,
4416 DWORD dwSigLen
, HCRYPTKEY hPubKey
, LPCWSTR sDescription
,
4419 BYTE
*pbConstructed
= NULL
, *pbDecrypted
= NULL
;
4420 CRYPTKEY
*pCryptKey
;
4423 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
];
4426 TRACE("(hProv=%08lx, hHash=%08lx, pbSignature=%p, dwSigLen=%d, hPubKey=%08lx, sDescription=%s, "
4427 "dwFlags=%08x)\n", hProv
, hHash
, pbSignature
, dwSigLen
, hPubKey
, debugstr_w(sDescription
),
4430 if (dwFlags
& ~(CRYPT_NOHASHOID
|CRYPT_X931_FORMAT
)) {
4431 SetLastError(NTE_BAD_FLAGS
);
4435 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
4437 SetLastError(NTE_BAD_UID
);
4441 if (!lookup_handle(&handle_table
, hPubKey
, RSAENH_MAGIC_KEY
,
4442 (OBJECTHDR
**)&pCryptKey
))
4444 SetLastError(NTE_BAD_KEY
);
4448 /* in Microsoft implementation, the signature length is checked before
4449 * the signature pointer.
4451 if (dwSigLen
!= pCryptKey
->dwKeyLen
)
4453 SetLastError(NTE_BAD_SIGNATURE
);
4457 if (!hHash
|| !pbSignature
)
4459 SetLastError(ERROR_INVALID_PARAMETER
);
4464 if (!RSAENH_CPHashData(hProv
, hHash
, (CONST BYTE
*)sDescription
,
4465 (DWORD
)lstrlenW(sDescription
)*sizeof(WCHAR
), 0))
4471 dwHashLen
= sizeof(DWORD
);
4472 if (!RSAENH_CPGetHashParam(hProv
, hHash
, HP_ALGID
, (BYTE
*)&aiAlgid
, &dwHashLen
, 0)) return FALSE
;
4474 dwHashLen
= RSAENH_MAX_HASH_SIZE
;
4475 if (!RSAENH_CPGetHashParam(hProv
, hHash
, HP_HASHVAL
, abHashValue
, &dwHashLen
, 0)) return FALSE
;
4477 pbConstructed
= HeapAlloc(GetProcessHeap(), 0, dwSigLen
);
4478 if (!pbConstructed
) {
4479 SetLastError(NTE_NO_MEMORY
);
4483 pbDecrypted
= HeapAlloc(GetProcessHeap(), 0, dwSigLen
);
4485 SetLastError(NTE_NO_MEMORY
);
4489 if (!encrypt_block_impl(pCryptKey
->aiAlgid
, PK_PUBLIC
, &pCryptKey
->context
, pbSignature
, pbDecrypted
,
4495 if (build_hash_signature(pbConstructed
, dwSigLen
, aiAlgid
, abHashValue
, dwHashLen
, dwFlags
) &&
4496 !memcmp(pbDecrypted
, pbConstructed
, dwSigLen
)) {
4501 if (!(dwFlags
& CRYPT_NOHASHOID
) &&
4502 build_hash_signature(pbConstructed
, dwSigLen
, aiAlgid
, abHashValue
, dwHashLen
, dwFlags
|CRYPT_NOHASHOID
) &&
4503 !memcmp(pbDecrypted
, pbConstructed
, dwSigLen
)) {
4508 SetLastError(NTE_BAD_SIGNATURE
);
4511 HeapFree(GetProcessHeap(), 0, pbConstructed
);
4512 HeapFree(GetProcessHeap(), 0, pbDecrypted
);
4516 /******************************************************************************
4517 * DllRegisterServer (RSAENH.@)
4519 HRESULT WINAPI
DllRegisterServer(void)
4521 return __wine_register_resources( instance
);
4524 /******************************************************************************
4525 * DllUnregisterServer (RSAENH.@)
4527 HRESULT WINAPI
DllUnregisterServer(void)
4529 return __wine_unregister_resources( instance
);