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
26 #include "wine/port.h"
27 #include "wine/library.h"
28 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(crypt
);
44 static HINSTANCE instance
;
46 /******************************************************************************
47 * CRYPTHASH - hash objects
49 #define RSAENH_MAGIC_HASH 0x85938417u
50 #define RSAENH_MAX_HASH_SIZE 104
51 #define RSAENH_HASHSTATE_HASHING 1
52 #define RSAENH_HASHSTATE_FINISHED 2
53 typedef struct _RSAENH_TLS1PRF_PARAMS
55 CRYPT_DATA_BLOB blobLabel
;
56 CRYPT_DATA_BLOB blobSeed
;
57 } RSAENH_TLS1PRF_PARAMS
;
59 typedef struct tagCRYPTHASH
68 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
];
70 RSAENH_TLS1PRF_PARAMS tpPRFParams
;
73 /******************************************************************************
74 * CRYPTKEY - key objects
76 #define RSAENH_MAGIC_KEY 0x73620457u
77 #define RSAENH_MAX_KEY_SIZE 64
78 #define RSAENH_MAX_BLOCK_SIZE 24
79 #define RSAENH_KEYSTATE_IDLE 0
80 #define RSAENH_KEYSTATE_ENCRYPTING 1
81 #define RSAENH_KEYSTATE_MASTERKEY 2
82 typedef struct _RSAENH_SCHANNEL_INFO
84 SCHANNEL_ALG saEncAlg
;
85 SCHANNEL_ALG saMACAlg
;
86 CRYPT_DATA_BLOB blobClientRandom
;
87 CRYPT_DATA_BLOB blobServerRandom
;
88 } RSAENH_SCHANNEL_INFO
;
90 typedef struct tagCRYPTKEY
99 DWORD dwEffectiveKeyLen
;
104 BYTE abKeyValue
[RSAENH_MAX_KEY_SIZE
];
105 BYTE abInitVector
[RSAENH_MAX_BLOCK_SIZE
];
106 BYTE abChainVector
[RSAENH_MAX_BLOCK_SIZE
];
107 RSAENH_SCHANNEL_INFO siSChannelInfo
;
108 CRYPT_DATA_BLOB blobHmacKey
;
111 /******************************************************************************
112 * KEYCONTAINER - key containers
114 #define RSAENH_PERSONALITY_BASE 0u
115 #define RSAENH_PERSONALITY_STRONG 1u
116 #define RSAENH_PERSONALITY_ENHANCED 2u
117 #define RSAENH_PERSONALITY_SCHANNEL 3u
118 #define RSAENH_PERSONALITY_AES 4u
120 #define RSAENH_MAGIC_CONTAINER 0x26384993u
121 typedef struct tagKEYCONTAINER
127 DWORD dwEnumContainersCtr
;
128 CHAR szName
[MAX_PATH
];
129 CHAR szProvName
[MAX_PATH
];
130 HCRYPTKEY hKeyExchangeKeyPair
;
131 HCRYPTKEY hSignatureKeyPair
;
134 /******************************************************************************
135 * Some magic constants
137 #define RSAENH_ENCRYPT 1
138 #define RSAENH_DECRYPT 0
139 #define RSAENH_HMAC_DEF_IPAD_CHAR 0x36
140 #define RSAENH_HMAC_DEF_OPAD_CHAR 0x5c
141 #define RSAENH_HMAC_DEF_PAD_LEN 64
142 #define RSAENH_HMAC_BLOCK_LEN 64
143 #define RSAENH_DES_EFFECTIVE_KEYLEN 56
144 #define RSAENH_DES_STORAGE_KEYLEN 64
145 #define RSAENH_3DES112_EFFECTIVE_KEYLEN 112
146 #define RSAENH_3DES112_STORAGE_KEYLEN 128
147 #define RSAENH_3DES_EFFECTIVE_KEYLEN 168
148 #define RSAENH_3DES_STORAGE_KEYLEN 192
149 #define RSAENH_MAGIC_RSA2 0x32415352
150 #define RSAENH_MAGIC_RSA1 0x31415352
151 #define RSAENH_PKC_BLOCKTYPE 0x02
152 #define RSAENH_SSL3_VERSION_MAJOR 3
153 #define RSAENH_SSL3_VERSION_MINOR 0
154 #define RSAENH_TLS1_VERSION_MAJOR 3
155 #define RSAENH_TLS1_VERSION_MINOR 1
156 #define RSAENH_REGKEY "Software\\Wine\\Crypto\\RSA\\%s"
158 #define RSAENH_MIN(a,b) ((a)<(b)?(a):(b))
159 /******************************************************************************
160 * aProvEnumAlgsEx - Defines the capabilities of the CSP personalities.
162 #define RSAENH_MAX_ENUMALGS 24
163 #define RSAENH_PCT1_SSL2_SSL3_TLS1 (CRYPT_FLAG_PCT1|CRYPT_FLAG_SSL2|CRYPT_FLAG_SSL3|CRYPT_FLAG_TLS1)
164 static const PROV_ENUMALGS_EX aProvEnumAlgsEx
[5][RSAENH_MAX_ENUMALGS
+1] =
167 {CALG_RC2
, 40, 40, 56,0, 4,"RC2", 24,"RSA Data Security's RC2"},
168 {CALG_RC4
, 40, 40, 56,0, 4,"RC4", 24,"RSA Data Security's RC4"},
169 {CALG_DES
, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
170 {CALG_SHA
, 160,160, 160,CRYPT_FLAG_SIGNING
, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
171 {CALG_MD2
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD2", 23,"Message Digest 2 (MD2)"},
172 {CALG_MD4
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD4", 23,"Message Digest 4 (MD4)"},
173 {CALG_MD5
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD5", 23,"Message Digest 5 (MD5)"},
174 {CALG_SSL3_SHAMD5
,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
175 {CALG_MAC
, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
176 {CALG_RSA_SIGN
, 512,384,16384,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_SIGN",14,"RSA Signature"},
177 {CALG_RSA_KEYX
, 512,384, 1024,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_KEYX",17,"RSA Key Exchange"},
178 {CALG_HMAC
, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
179 {0, 0, 0, 0,0, 1,"", 1,""}
182 {CALG_RC2
, 128, 40, 128,0, 4,"RC2", 24,"RSA Data Security's RC2"},
183 {CALG_RC4
, 128, 40, 128,0, 4,"RC4", 24,"RSA Data Security's RC4"},
184 {CALG_DES
, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
185 {CALG_3DES_112
, 112,112, 112,0, 13,"3DES TWO KEY",19,"Two Key Triple DES"},
186 {CALG_3DES
, 168,168, 168,0, 5,"3DES", 21,"Three Key Triple DES"},
187 {CALG_SHA
, 160,160, 160,CRYPT_FLAG_SIGNING
, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
188 {CALG_MD2
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD2", 23,"Message Digest 2 (MD2)"},
189 {CALG_MD4
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD4", 23,"Message Digest 4 (MD4)"},
190 {CALG_MD5
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD5", 23,"Message Digest 5 (MD5)"},
191 {CALG_SSL3_SHAMD5
,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
192 {CALG_MAC
, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
193 {CALG_RSA_SIGN
,1024,384,16384,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_SIGN",14,"RSA Signature"},
194 {CALG_RSA_KEYX
,1024,384,16384,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_KEYX",17,"RSA Key Exchange"},
195 {CALG_HMAC
, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
196 {0, 0, 0, 0,0, 1,"", 1,""}
199 {CALG_RC2
, 128, 40, 128,0, 4,"RC2", 24,"RSA Data Security's RC2"},
200 {CALG_RC4
, 128, 40, 128,0, 4,"RC4", 24,"RSA Data Security's RC4"},
201 {CALG_DES
, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
202 {CALG_3DES_112
, 112,112, 112,0, 13,"3DES TWO KEY",19,"Two Key Triple DES"},
203 {CALG_3DES
, 168,168, 168,0, 5,"3DES", 21,"Three Key Triple DES"},
204 {CALG_SHA
, 160,160, 160,CRYPT_FLAG_SIGNING
, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
205 {CALG_MD2
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD2", 23,"Message Digest 2 (MD2)"},
206 {CALG_MD4
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD4", 23,"Message Digest 4 (MD4)"},
207 {CALG_MD5
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD5", 23,"Message Digest 5 (MD5)"},
208 {CALG_SSL3_SHAMD5
,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
209 {CALG_MAC
, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
210 {CALG_RSA_SIGN
,1024,384,16384,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_SIGN",14,"RSA Signature"},
211 {CALG_RSA_KEYX
,1024,384,16384,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_KEYX",17,"RSA Key Exchange"},
212 {CALG_HMAC
, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
213 {0, 0, 0, 0,0, 1,"", 1,""}
216 {CALG_RC2
, 128, 40, 128,RSAENH_PCT1_SSL2_SSL3_TLS1
, 4,"RC2", 24,"RSA Data Security's RC2"},
217 {CALG_RC4
, 128, 40, 128,RSAENH_PCT1_SSL2_SSL3_TLS1
, 4,"RC4", 24,"RSA Data Security's RC4"},
218 {CALG_DES
, 56, 56, 56,RSAENH_PCT1_SSL2_SSL3_TLS1
, 4,"DES", 31,"Data Encryption Standard (DES)"},
219 {CALG_3DES_112
, 112,112, 112,RSAENH_PCT1_SSL2_SSL3_TLS1
,13,"3DES TWO KEY",19,"Two Key Triple DES"},
220 {CALG_3DES
, 168,168, 168,RSAENH_PCT1_SSL2_SSL3_TLS1
, 5,"3DES", 21,"Three Key Triple DES"},
221 {CALG_SHA
,160,160,160,CRYPT_FLAG_SIGNING
|RSAENH_PCT1_SSL2_SSL3_TLS1
,6,"SHA-1",30,"Secure Hash Algorithm (SHA-1)"},
222 {CALG_MD5
,128,128,128,CRYPT_FLAG_SIGNING
|RSAENH_PCT1_SSL2_SSL3_TLS1
,4,"MD5",23,"Message Digest 5 (MD5)"},
223 {CALG_SSL3_SHAMD5
,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
224 {CALG_MAC
, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
225 {CALG_RSA_SIGN
,1024,384,16384,CRYPT_FLAG_SIGNING
|RSAENH_PCT1_SSL2_SSL3_TLS1
,9,"RSA_SIGN",14,"RSA Signature"},
226 {CALG_RSA_KEYX
,1024,384,16384,CRYPT_FLAG_SIGNING
|RSAENH_PCT1_SSL2_SSL3_TLS1
,9,"RSA_KEYX",17,"RSA Key Exchange"},
227 {CALG_HMAC
, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
228 {CALG_PCT1_MASTER
,128,128,128,CRYPT_FLAG_PCT1
, 12,"PCT1 MASTER",12,"PCT1 Master"},
229 {CALG_SSL2_MASTER
,40,40, 192,CRYPT_FLAG_SSL2
, 12,"SSL2 MASTER",12,"SSL2 Master"},
230 {CALG_SSL3_MASTER
,384,384,384,CRYPT_FLAG_SSL3
, 12,"SSL3 MASTER",12,"SSL3 Master"},
231 {CALG_TLS1_MASTER
,384,384,384,CRYPT_FLAG_TLS1
, 12,"TLS1 MASTER",12,"TLS1 Master"},
232 {CALG_SCHANNEL_MASTER_HASH
,0,0,-1,0, 16,"SCH MASTER HASH",21,"SChannel Master Hash"},
233 {CALG_SCHANNEL_MAC_KEY
,0,0,-1,0, 12,"SCH MAC KEY",17,"SChannel MAC Key"},
234 {CALG_SCHANNEL_ENC_KEY
,0,0,-1,0, 12,"SCH ENC KEY",24,"SChannel Encryption Key"},
235 {CALG_TLS1PRF
, 0, 0, -1,0, 9,"TLS1 PRF", 28,"TLS1 Pseudo Random Function"},
236 {0, 0, 0, 0,0, 1,"", 1,""}
239 {CALG_RC2
, 128, 40, 128,0, 4,"RC2", 24,"RSA Data Security's RC2"},
240 {CALG_RC4
, 128, 40, 128,0, 4,"RC4", 24,"RSA Data Security's RC4"},
241 {CALG_DES
, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
242 {CALG_3DES_112
, 112,112, 112,0, 13,"3DES TWO KEY",19,"Two Key Triple DES"},
243 {CALG_3DES
, 168,168, 168,0, 5,"3DES", 21,"Three Key Triple DES"},
244 {CALG_AES
, 128,128, 128,0, 4,"AES", 35,"Advanced Encryption Standard (AES)"},
245 {CALG_AES_128
, 128,128, 128,0, 8,"AES-128", 39,"Advanced Encryption Standard (AES-128)"},
246 {CALG_AES_192
, 192,192, 192,0, 8,"AES-192", 39,"Advanced Encryption Standard (AES-192)"},
247 {CALG_AES_256
, 256,256, 256,0, 8,"AES-256", 39,"Advanced Encryption Standard (AES-256)"},
248 {CALG_SHA
, 160,160, 160,CRYPT_FLAG_SIGNING
, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
249 {CALG_SHA_256
, 256,256, 256,CRYPT_FLAG_SIGNING
, 6,"SHA-256", 30,"Secure Hash Algorithm (SHA-256)"},
250 {CALG_SHA_384
, 384,384, 384,CRYPT_FLAG_SIGNING
, 6,"SHA-384", 30,"Secure Hash Algorithm (SHA-284)"},
251 {CALG_SHA_512
, 512,512, 512,CRYPT_FLAG_SIGNING
, 6,"SHA-512", 30,"Secure Hash Algorithm (SHA-512)"},
252 {CALG_MD2
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD2", 23,"Message Digest 2 (MD2)"},
253 {CALG_MD4
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD4", 23,"Message Digest 4 (MD4)"},
254 {CALG_MD5
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD5", 23,"Message Digest 5 (MD5)"},
255 {CALG_SSL3_SHAMD5
,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
256 {CALG_MAC
, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
257 {CALG_RSA_SIGN
,1024,384,16384,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_SIGN",14,"RSA Signature"},
258 {CALG_RSA_KEYX
,1024,384,16384,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_KEYX",17,"RSA Key Exchange"},
259 {CALG_HMAC
, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
260 {0, 0, 0, 0,0, 1,"", 1,""}
264 /******************************************************************************
265 * API forward declarations
268 RSAENH_CPGetKeyParam(
299 RSAENH_CPSetHashParam(
303 BYTE
*pbData
, DWORD dwFlags
307 RSAENH_CPGetHashParam(
317 RSAENH_CPDestroyHash(
322 static BOOL
crypt_export_key(
332 static BOOL
import_key(
351 /******************************************************************************
352 * CSP's handle table (used by all acquired key containers)
354 static struct handle_table handle_table
;
356 /******************************************************************************
359 * Initializes and destroys the handle table for the CSP's handles.
361 BOOL WINAPI
DllMain(HINSTANCE hInstance
, DWORD fdwReason
, PVOID reserved
)
365 case DLL_PROCESS_ATTACH
:
366 instance
= hInstance
;
367 DisableThreadLibraryCalls(hInstance
);
368 init_handle_table(&handle_table
);
371 case DLL_PROCESS_DETACH
:
373 destroy_handle_table(&handle_table
);
379 /******************************************************************************
380 * copy_param [Internal]
382 * Helper function that supports the standard WINAPI protocol for querying data
386 * pbBuffer [O] Buffer where the queried parameter is copied to, if it is large enough.
387 * May be NUL if the required buffer size is to be queried only.
388 * pdwBufferSize [I/O] In: Size of the buffer at pbBuffer
389 * Out: Size of parameter pbParam
390 * pbParam [I] Parameter value.
391 * dwParamSize [I] Size of pbParam
394 * Success: TRUE (pbParam was copied into pbBuffer or pbBuffer is NULL)
395 * Failure: FALSE (pbBuffer is not large enough to hold pbParam). Last error: ERROR_MORE_DATA
397 static inline BOOL
copy_param(
398 BYTE
*pbBuffer
, DWORD
*pdwBufferSize
, CONST BYTE
*pbParam
, DWORD dwParamSize
)
402 if (dwParamSize
> *pdwBufferSize
)
404 SetLastError(ERROR_MORE_DATA
);
405 *pdwBufferSize
= dwParamSize
;
408 memcpy(pbBuffer
, pbParam
, dwParamSize
);
410 *pdwBufferSize
= dwParamSize
;
414 /******************************************************************************
415 * get_algid_info [Internal]
417 * Query CSP capabilities for a given crypto algorithm.
420 * hProv [I] Handle to a key container of the CSP whose capabilities are to be queried.
421 * algid [I] Identifier of the crypto algorithm about which information is requested.
424 * Success: Pointer to a PROV_ENUMALGS_EX struct containing information about the crypto algorithm.
425 * Failure: NULL (algid not supported)
427 static inline const PROV_ENUMALGS_EX
* get_algid_info(HCRYPTPROV hProv
, ALG_ID algid
) {
428 const PROV_ENUMALGS_EX
*iterator
;
429 KEYCONTAINER
*pKeyContainer
;
431 if (!lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
, (OBJECTHDR
**)&pKeyContainer
)) {
432 SetLastError(NTE_BAD_UID
);
436 for (iterator
= aProvEnumAlgsEx
[pKeyContainer
->dwPersonality
]; iterator
->aiAlgid
; iterator
++) {
437 if (iterator
->aiAlgid
== algid
) return iterator
;
440 SetLastError(NTE_BAD_ALGID
);
444 /******************************************************************************
445 * copy_data_blob [Internal]
447 * deeply copies a DATA_BLOB
450 * dst [O] That's where the blob will be copied to
451 * src [I] Source blob
455 * Failure: FALSE (GetLastError() == NTE_NO_MEMORY
458 * Use free_data_blob to release resources occupied by copy_data_blob.
460 static inline BOOL
copy_data_blob(PCRYPT_DATA_BLOB dst
, CONST PCRYPT_DATA_BLOB src
) {
461 dst
->pbData
= HeapAlloc(GetProcessHeap(), 0, src
->cbData
);
463 SetLastError(NTE_NO_MEMORY
);
466 dst
->cbData
= src
->cbData
;
467 memcpy(dst
->pbData
, src
->pbData
, src
->cbData
);
471 /******************************************************************************
472 * concat_data_blobs [Internal]
474 * Concatenates two blobs
477 * dst [O] The new blob will be copied here
478 * src1 [I] Prefix blob
479 * src2 [I] Appendix blob
483 * Failure: FALSE (GetLastError() == NTE_NO_MEMORY)
486 * Release resources occupied by concat_data_blobs with free_data_blobs
488 static inline BOOL
concat_data_blobs(PCRYPT_DATA_BLOB dst
, CONST PCRYPT_DATA_BLOB src1
,
489 CONST PCRYPT_DATA_BLOB src2
)
491 dst
->cbData
= src1
->cbData
+ src2
->cbData
;
492 dst
->pbData
= HeapAlloc(GetProcessHeap(), 0, dst
->cbData
);
494 SetLastError(NTE_NO_MEMORY
);
497 memcpy(dst
->pbData
, src1
->pbData
, src1
->cbData
);
498 memcpy(dst
->pbData
+ src1
->cbData
, src2
->pbData
, src2
->cbData
);
502 /******************************************************************************
503 * free_data_blob [Internal]
505 * releases resource occupied by a dynamically allocated CRYPT_DATA_BLOB
508 * pBlob [I] Heap space occupied by pBlob->pbData is released
510 static inline void free_data_blob(PCRYPT_DATA_BLOB pBlob
) {
511 HeapFree(GetProcessHeap(), 0, pBlob
->pbData
);
514 /******************************************************************************
515 * init_data_blob [Internal]
517 static inline void init_data_blob(PCRYPT_DATA_BLOB pBlob
) {
518 pBlob
->pbData
= NULL
;
522 /******************************************************************************
523 * free_hmac_info [Internal]
525 * Deeply free an HMAC_INFO struct.
528 * hmac_info [I] Pointer to the HMAC_INFO struct to be freed.
531 * See Internet RFC 2104 for details on the HMAC algorithm.
533 static inline void free_hmac_info(PHMAC_INFO hmac_info
) {
534 if (!hmac_info
) return;
535 HeapFree(GetProcessHeap(), 0, hmac_info
->pbInnerString
);
536 HeapFree(GetProcessHeap(), 0, hmac_info
->pbOuterString
);
537 HeapFree(GetProcessHeap(), 0, hmac_info
);
540 /******************************************************************************
541 * copy_hmac_info [Internal]
543 * Deeply copy an HMAC_INFO struct
546 * dst [O] Pointer to a location where the pointer to the HMAC_INFO copy will be stored.
547 * src [I] Pointer to the HMAC_INFO struct to be copied.
554 * See Internet RFC 2104 for details on the HMAC algorithm.
556 static BOOL
copy_hmac_info(PHMAC_INFO
*dst
, const HMAC_INFO
*src
) {
557 if (!src
) return FALSE
;
558 *dst
= HeapAlloc(GetProcessHeap(), 0, sizeof(HMAC_INFO
));
559 if (!*dst
) return FALSE
;
561 (*dst
)->pbInnerString
= NULL
;
562 (*dst
)->pbOuterString
= NULL
;
563 if ((*dst
)->cbInnerString
== 0) (*dst
)->cbInnerString
= RSAENH_HMAC_DEF_PAD_LEN
;
564 (*dst
)->pbInnerString
= HeapAlloc(GetProcessHeap(), 0, (*dst
)->cbInnerString
);
565 if (!(*dst
)->pbInnerString
) {
566 free_hmac_info(*dst
);
569 if (src
->cbInnerString
)
570 memcpy((*dst
)->pbInnerString
, src
->pbInnerString
, src
->cbInnerString
);
572 memset((*dst
)->pbInnerString
, RSAENH_HMAC_DEF_IPAD_CHAR
, RSAENH_HMAC_DEF_PAD_LEN
);
573 if ((*dst
)->cbOuterString
== 0) (*dst
)->cbOuterString
= RSAENH_HMAC_DEF_PAD_LEN
;
574 (*dst
)->pbOuterString
= HeapAlloc(GetProcessHeap(), 0, (*dst
)->cbOuterString
);
575 if (!(*dst
)->pbOuterString
) {
576 free_hmac_info(*dst
);
579 if (src
->cbOuterString
)
580 memcpy((*dst
)->pbOuterString
, src
->pbOuterString
, src
->cbOuterString
);
582 memset((*dst
)->pbOuterString
, RSAENH_HMAC_DEF_OPAD_CHAR
, RSAENH_HMAC_DEF_PAD_LEN
);
586 /******************************************************************************
587 * destroy_hash [Internal]
589 * Destructor for hash objects
592 * pCryptHash [I] Pointer to the hash object to be destroyed.
593 * Will be invalid after function returns!
595 static void destroy_hash(OBJECTHDR
*pObject
)
597 CRYPTHASH
*pCryptHash
= (CRYPTHASH
*)pObject
;
599 free_hmac_info(pCryptHash
->pHMACInfo
);
600 free_data_blob(&pCryptHash
->tpPRFParams
.blobLabel
);
601 free_data_blob(&pCryptHash
->tpPRFParams
.blobSeed
);
602 HeapFree(GetProcessHeap(), 0, pCryptHash
);
605 /******************************************************************************
606 * init_hash [Internal]
608 * Initialize (or reset) a hash object
611 * pCryptHash [I] The hash object to be initialized.
613 static inline BOOL
init_hash(CRYPTHASH
*pCryptHash
) {
616 switch (pCryptHash
->aiAlgid
)
619 if (pCryptHash
->pHMACInfo
) {
620 const PROV_ENUMALGS_EX
*pAlgInfo
;
622 pAlgInfo
= get_algid_info(pCryptHash
->hProv
, pCryptHash
->pHMACInfo
->HashAlgid
);
623 if (!pAlgInfo
) return FALSE
;
624 pCryptHash
->dwHashSize
= pAlgInfo
->dwDefaultLen
>> 3;
625 init_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
);
626 update_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
627 pCryptHash
->pHMACInfo
->pbInnerString
,
628 pCryptHash
->pHMACInfo
->cbInnerString
);
633 dwLen
= sizeof(DWORD
);
634 RSAENH_CPGetKeyParam(pCryptHash
->hProv
, pCryptHash
->hKey
, KP_BLOCKLEN
,
635 (BYTE
*)&pCryptHash
->dwHashSize
, &dwLen
, 0);
636 pCryptHash
->dwHashSize
>>= 3;
640 return init_hash_impl(pCryptHash
->aiAlgid
, &pCryptHash
->context
);
644 /******************************************************************************
645 * update_hash [Internal]
647 * Hashes the given data and updates the hash object's state accordingly
650 * pCryptHash [I] Hash object to be updated.
651 * pbData [I] Pointer to data stream to be hashed.
652 * dwDataLen [I] Length of data stream.
654 static inline void update_hash(CRYPTHASH
*pCryptHash
, CONST BYTE
*pbData
, DWORD dwDataLen
) {
657 switch (pCryptHash
->aiAlgid
)
660 if (pCryptHash
->pHMACInfo
)
661 update_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
666 pbTemp
= HeapAlloc(GetProcessHeap(), 0, dwDataLen
);
668 memcpy(pbTemp
, pbData
, dwDataLen
);
669 RSAENH_CPEncrypt(pCryptHash
->hProv
, pCryptHash
->hKey
, 0, FALSE
, 0,
670 pbTemp
, &dwDataLen
, dwDataLen
);
671 HeapFree(GetProcessHeap(), 0, pbTemp
);
675 update_hash_impl(pCryptHash
->aiAlgid
, &pCryptHash
->context
, pbData
, dwDataLen
);
679 /******************************************************************************
680 * finalize_hash [Internal]
682 * Finalizes the hash, after all data has been hashed with update_hash.
683 * No additional data can be hashed afterwards until the hash gets initialized again.
686 * pCryptHash [I] Hash object to be finalized.
688 static inline void finalize_hash(CRYPTHASH
*pCryptHash
) {
691 switch (pCryptHash
->aiAlgid
)
694 if (pCryptHash
->pHMACInfo
) {
695 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
];
697 finalize_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
698 pCryptHash
->abHashValue
);
699 memcpy(abHashValue
, pCryptHash
->abHashValue
, pCryptHash
->dwHashSize
);
700 init_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
);
701 update_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
702 pCryptHash
->pHMACInfo
->pbOuterString
,
703 pCryptHash
->pHMACInfo
->cbOuterString
);
704 update_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
705 abHashValue
, pCryptHash
->dwHashSize
);
706 finalize_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
707 pCryptHash
->abHashValue
);
713 RSAENH_CPEncrypt(pCryptHash
->hProv
, pCryptHash
->hKey
, 0, TRUE
, 0,
714 pCryptHash
->abHashValue
, &dwDataLen
, pCryptHash
->dwHashSize
);
718 finalize_hash_impl(pCryptHash
->aiAlgid
, &pCryptHash
->context
, pCryptHash
->abHashValue
);
722 /******************************************************************************
723 * destroy_key [Internal]
725 * Destructor for key objects
728 * pCryptKey [I] Pointer to the key object to be destroyed.
729 * Will be invalid after function returns!
731 static void destroy_key(OBJECTHDR
*pObject
)
733 CRYPTKEY
*pCryptKey
= (CRYPTKEY
*)pObject
;
735 free_key_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
);
736 free_data_blob(&pCryptKey
->siSChannelInfo
.blobClientRandom
);
737 free_data_blob(&pCryptKey
->siSChannelInfo
.blobServerRandom
);
738 free_data_blob(&pCryptKey
->blobHmacKey
);
739 HeapFree(GetProcessHeap(), 0, pCryptKey
);
742 /******************************************************************************
743 * setup_key [Internal]
745 * Initialize (or reset) a key object
748 * pCryptKey [I] The key object to be initialized.
750 static inline void setup_key(CRYPTKEY
*pCryptKey
) {
751 pCryptKey
->dwState
= RSAENH_KEYSTATE_IDLE
;
752 memcpy(pCryptKey
->abChainVector
, pCryptKey
->abInitVector
, sizeof(pCryptKey
->abChainVector
));
753 setup_key_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pCryptKey
->dwKeyLen
,
754 pCryptKey
->dwEffectiveKeyLen
, pCryptKey
->dwSaltLen
,
755 pCryptKey
->abKeyValue
);
758 /******************************************************************************
761 * Creates a new key object without assigning the actual binary key value.
762 * This is done by CPDeriveKey, CPGenKey or CPImportKey, which call this function.
765 * hProv [I] Handle to the provider to which the created key will belong.
766 * aiAlgid [I] The new key shall use the crypto algorithm identified by aiAlgid.
767 * dwFlags [I] Upper 16 bits give the key length.
768 * Lower 16 bits: CRYPT_EXPORTABLE, CRYPT_CREATE_SALT,
770 * ppCryptKey [O] Pointer to the created key
773 * Success: Handle to the created key.
774 * Failure: INVALID_HANDLE_VALUE
776 static HCRYPTKEY
new_key(HCRYPTPROV hProv
, ALG_ID aiAlgid
, DWORD dwFlags
, CRYPTKEY
**ppCryptKey
)
780 DWORD dwKeyLen
= HIWORD(dwFlags
);
781 const PROV_ENUMALGS_EX
*peaAlgidInfo
;
786 * Retrieve the CSP's capabilities for the given ALG_ID value
788 peaAlgidInfo
= get_algid_info(hProv
, aiAlgid
);
789 if (!peaAlgidInfo
) return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
791 TRACE("alg = %s, dwKeyLen = %d\n", debugstr_a(peaAlgidInfo
->szName
),
794 * Assume the default key length, if none is specified explicitly
796 if (dwKeyLen
== 0) dwKeyLen
= peaAlgidInfo
->dwDefaultLen
;
799 * Check if the requested key length is supported by the current CSP.
800 * Adjust key length's for DES algorithms.
804 if (dwKeyLen
== RSAENH_DES_EFFECTIVE_KEYLEN
) {
805 dwKeyLen
= RSAENH_DES_STORAGE_KEYLEN
;
807 if (dwKeyLen
!= RSAENH_DES_STORAGE_KEYLEN
) {
808 SetLastError(NTE_BAD_FLAGS
);
809 return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
814 if (dwKeyLen
== RSAENH_3DES112_EFFECTIVE_KEYLEN
) {
815 dwKeyLen
= RSAENH_3DES112_STORAGE_KEYLEN
;
817 if (dwKeyLen
!= RSAENH_3DES112_STORAGE_KEYLEN
) {
818 SetLastError(NTE_BAD_FLAGS
);
819 return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
824 if (dwKeyLen
== RSAENH_3DES_EFFECTIVE_KEYLEN
) {
825 dwKeyLen
= RSAENH_3DES_STORAGE_KEYLEN
;
827 if (dwKeyLen
!= RSAENH_3DES_STORAGE_KEYLEN
) {
828 SetLastError(NTE_BAD_FLAGS
);
829 return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
834 /* Avoid the key length check for HMAC keys, which have unlimited
841 dwKeyLen
> peaAlgidInfo
->dwMaxLen
||
842 dwKeyLen
< peaAlgidInfo
->dwMinLen
)
844 TRACE("key len %d out of bounds (%d, %d)\n", dwKeyLen
,
845 peaAlgidInfo
->dwMinLen
, peaAlgidInfo
->dwMaxLen
);
846 SetLastError(NTE_BAD_DATA
);
847 return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
851 hCryptKey
= new_object(&handle_table
, sizeof(CRYPTKEY
), RSAENH_MAGIC_KEY
,
852 destroy_key
, (OBJECTHDR
**)&pCryptKey
);
853 if (hCryptKey
!= (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
855 pCryptKey
->aiAlgid
= aiAlgid
;
856 pCryptKey
->hProv
= hProv
;
857 pCryptKey
->dwModeBits
= 0;
858 pCryptKey
->dwPermissions
= CRYPT_ENCRYPT
| CRYPT_DECRYPT
| CRYPT_READ
| CRYPT_WRITE
|
860 if (dwFlags
& CRYPT_EXPORTABLE
)
861 pCryptKey
->dwPermissions
|= CRYPT_EXPORT
;
862 pCryptKey
->dwKeyLen
= dwKeyLen
>> 3;
863 pCryptKey
->dwEffectiveKeyLen
= 0;
864 if ((dwFlags
& CRYPT_CREATE_SALT
) || (dwKeyLen
== 40 && !(dwFlags
& CRYPT_NO_SALT
)))
865 pCryptKey
->dwSaltLen
= 16 /*FIXME*/ - pCryptKey
->dwKeyLen
;
867 pCryptKey
->dwSaltLen
= 0;
868 memset(pCryptKey
->abKeyValue
, 0, sizeof(pCryptKey
->abKeyValue
));
869 memset(pCryptKey
->abInitVector
, 0, sizeof(pCryptKey
->abInitVector
));
870 memset(&pCryptKey
->siSChannelInfo
.saEncAlg
, 0, sizeof(pCryptKey
->siSChannelInfo
.saEncAlg
));
871 memset(&pCryptKey
->siSChannelInfo
.saMACAlg
, 0, sizeof(pCryptKey
->siSChannelInfo
.saMACAlg
));
872 init_data_blob(&pCryptKey
->siSChannelInfo
.blobClientRandom
);
873 init_data_blob(&pCryptKey
->siSChannelInfo
.blobServerRandom
);
874 init_data_blob(&pCryptKey
->blobHmacKey
);
878 case CALG_PCT1_MASTER
:
879 case CALG_SSL2_MASTER
:
880 case CALG_SSL3_MASTER
:
881 case CALG_TLS1_MASTER
:
883 pCryptKey
->dwBlockLen
= 0;
884 pCryptKey
->dwMode
= 0;
891 pCryptKey
->dwBlockLen
= 8;
892 pCryptKey
->dwMode
= CRYPT_MODE_CBC
;
899 pCryptKey
->dwBlockLen
= 16;
900 pCryptKey
->dwMode
= CRYPT_MODE_ECB
;
905 pCryptKey
->dwBlockLen
= dwKeyLen
>> 3;
906 pCryptKey
->dwMode
= 0;
910 pCryptKey
->dwBlockLen
= 0;
911 pCryptKey
->dwMode
= 0;
915 *ppCryptKey
= pCryptKey
;
921 /******************************************************************************
922 * map_key_spec_to_key_pair_name [Internal]
924 * Returns the name of the registry value associated with a key spec.
927 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
930 * Success: Name of registry value.
933 static LPCSTR
map_key_spec_to_key_pair_name(DWORD dwKeySpec
)
940 szValueName
= "KeyExchangeKeyPair";
943 szValueName
= "SignatureKeyPair";
946 WARN("invalid key spec %d\n", dwKeySpec
);
952 /******************************************************************************
953 * store_key_pair [Internal]
955 * Stores a key pair to the registry
958 * hCryptKey [I] Handle to the key to be stored
959 * hKey [I] Registry key where the key pair is to be stored
960 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
961 * dwFlags [I] Flags for protecting the key
963 static void store_key_pair(HCRYPTKEY hCryptKey
, HKEY hKey
, DWORD dwKeySpec
, DWORD dwFlags
)
966 DATA_BLOB blobIn
, blobOut
;
971 if (!(szValueName
= map_key_spec_to_key_pair_name(dwKeySpec
)))
973 if (lookup_handle(&handle_table
, hCryptKey
, RSAENH_MAGIC_KEY
,
976 if (crypt_export_key(pKey
, 0, PRIVATEKEYBLOB
, 0, TRUE
, 0, &dwLen
))
978 pbKey
= HeapAlloc(GetProcessHeap(), 0, dwLen
);
981 if (crypt_export_key(pKey
, 0, PRIVATEKEYBLOB
, 0, TRUE
, pbKey
,
984 blobIn
.pbData
= pbKey
;
985 blobIn
.cbData
= dwLen
;
987 if (CryptProtectData(&blobIn
, NULL
, NULL
, NULL
, NULL
,
990 RegSetValueExA(hKey
, szValueName
, 0, REG_BINARY
,
991 blobOut
.pbData
, blobOut
.cbData
);
992 LocalFree(blobOut
.pbData
);
995 HeapFree(GetProcessHeap(), 0, pbKey
);
1001 /******************************************************************************
1002 * map_key_spec_to_permissions_name [Internal]
1004 * Returns the name of the registry value associated with the permissions for
1008 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
1011 * Success: Name of registry value.
1014 static LPCSTR
map_key_spec_to_permissions_name(DWORD dwKeySpec
)
1020 case AT_KEYEXCHANGE
:
1021 szValueName
= "KeyExchangePermissions";
1024 szValueName
= "SignaturePermissions";
1027 WARN("invalid key spec %d\n", dwKeySpec
);
1033 /******************************************************************************
1034 * store_key_permissions [Internal]
1036 * Stores a key's permissions to the registry
1039 * hCryptKey [I] Handle to the key whose permissions are to be stored
1040 * hKey [I] Registry key where the key permissions are to be stored
1041 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
1043 static void store_key_permissions(HCRYPTKEY hCryptKey
, HKEY hKey
, DWORD dwKeySpec
)
1048 if (!(szValueName
= map_key_spec_to_permissions_name(dwKeySpec
)))
1050 if (lookup_handle(&handle_table
, hCryptKey
, RSAENH_MAGIC_KEY
,
1051 (OBJECTHDR
**)&pKey
))
1052 RegSetValueExA(hKey
, szValueName
, 0, REG_DWORD
,
1053 (BYTE
*)&pKey
->dwPermissions
,
1054 sizeof(pKey
->dwPermissions
));
1057 /******************************************************************************
1058 * create_container_key [Internal]
1060 * Creates the registry key for a key container's persistent storage.
1063 * pKeyContainer [I] Pointer to the key container
1064 * sam [I] Desired registry access
1065 * phKey [O] Returned key
1067 static BOOL
create_container_key(KEYCONTAINER
*pKeyContainer
, REGSAM sam
, HKEY
*phKey
)
1069 CHAR szRSABase
[MAX_PATH
];
1072 sprintf(szRSABase
, RSAENH_REGKEY
, pKeyContainer
->szName
);
1074 if (pKeyContainer
->dwFlags
& CRYPT_MACHINE_KEYSET
)
1075 hRootKey
= HKEY_LOCAL_MACHINE
;
1077 hRootKey
= HKEY_CURRENT_USER
;
1079 /* @@ Wine registry key: HKLM\Software\Wine\Crypto\RSA */
1080 /* @@ Wine registry key: HKCU\Software\Wine\Crypto\RSA */
1081 return RegCreateKeyExA(hRootKey
, szRSABase
, 0, NULL
,
1082 REG_OPTION_NON_VOLATILE
, sam
, NULL
, phKey
, NULL
)
1086 /******************************************************************************
1087 * open_container_key [Internal]
1089 * Opens a key container's persistent storage for reading.
1092 * pszContainerName [I] Name of the container to be opened. May be the empty
1093 * string if the parent key of all containers is to be
1095 * dwFlags [I] Flags indicating which keyset to be opened.
1096 * phKey [O] Returned key
1098 static BOOL
open_container_key(LPCSTR pszContainerName
, DWORD dwFlags
, HKEY
*phKey
)
1100 CHAR szRSABase
[MAX_PATH
];
1103 sprintf(szRSABase
, RSAENH_REGKEY
, pszContainerName
);
1105 if (dwFlags
& CRYPT_MACHINE_KEYSET
)
1106 hRootKey
= HKEY_LOCAL_MACHINE
;
1108 hRootKey
= HKEY_CURRENT_USER
;
1110 /* @@ Wine registry key: HKLM\Software\Wine\Crypto\RSA */
1111 /* @@ Wine registry key: HKCU\Software\Wine\Crypto\RSA */
1112 return RegOpenKeyExA(hRootKey
, szRSABase
, 0, KEY_READ
, phKey
) ==
1116 /******************************************************************************
1117 * delete_container_key [Internal]
1119 * Deletes a key container's persistent storage.
1122 * pszContainerName [I] Name of the container to be opened.
1123 * dwFlags [I] Flags indicating which keyset to be opened.
1125 static BOOL
delete_container_key(LPCSTR pszContainerName
, DWORD dwFlags
)
1127 CHAR szRegKey
[MAX_PATH
];
1129 if (snprintf(szRegKey
, MAX_PATH
, RSAENH_REGKEY
, pszContainerName
) >= MAX_PATH
) {
1130 SetLastError(NTE_BAD_KEYSET_PARAM
);
1134 if (dwFlags
& CRYPT_MACHINE_KEYSET
)
1135 hRootKey
= HKEY_LOCAL_MACHINE
;
1137 hRootKey
= HKEY_CURRENT_USER
;
1138 if (!RegDeleteKeyA(hRootKey
, szRegKey
)) {
1139 SetLastError(ERROR_SUCCESS
);
1142 SetLastError(NTE_BAD_KEYSET
);
1148 /******************************************************************************
1149 * store_key_container_keys [Internal]
1151 * Stores key container's keys in a persistent location.
1154 * pKeyContainer [I] Pointer to the key container whose keys are to be saved
1156 static void store_key_container_keys(KEYCONTAINER
*pKeyContainer
)
1161 /* On WinXP, persistent keys are stored in a file located at:
1162 * $AppData$\\Microsoft\\Crypto\\RSA\\$SID$\\some_hex_string
1165 if (pKeyContainer
->dwFlags
& CRYPT_MACHINE_KEYSET
)
1166 dwFlags
= CRYPTPROTECT_LOCAL_MACHINE
;
1170 if (create_container_key(pKeyContainer
, KEY_WRITE
, &hKey
))
1172 store_key_pair(pKeyContainer
->hKeyExchangeKeyPair
, hKey
,
1173 AT_KEYEXCHANGE
, dwFlags
);
1174 store_key_pair(pKeyContainer
->hSignatureKeyPair
, hKey
,
1175 AT_SIGNATURE
, dwFlags
);
1180 /******************************************************************************
1181 * store_key_container_permissions [Internal]
1183 * Stores key container's key permissions in a persistent location.
1186 * pKeyContainer [I] Pointer to the key container whose key permissions are to
1189 static void store_key_container_permissions(KEYCONTAINER
*pKeyContainer
)
1193 if (create_container_key(pKeyContainer
, KEY_WRITE
, &hKey
))
1195 store_key_permissions(pKeyContainer
->hKeyExchangeKeyPair
, hKey
,
1197 store_key_permissions(pKeyContainer
->hSignatureKeyPair
, hKey
,
1203 /******************************************************************************
1204 * release_key_container_keys [Internal]
1206 * Releases key container's keys.
1209 * pKeyContainer [I] Pointer to the key container whose keys are to be released.
1211 static void release_key_container_keys(KEYCONTAINER
*pKeyContainer
)
1213 release_handle(&handle_table
, pKeyContainer
->hKeyExchangeKeyPair
,
1215 release_handle(&handle_table
, pKeyContainer
->hSignatureKeyPair
,
1219 /******************************************************************************
1220 * destroy_key_container [Internal]
1222 * Destructor for key containers.
1225 * pObjectHdr [I] Pointer to the key container to be destroyed.
1227 static void destroy_key_container(OBJECTHDR
*pObjectHdr
)
1229 KEYCONTAINER
*pKeyContainer
= (KEYCONTAINER
*)pObjectHdr
;
1231 if (!(pKeyContainer
->dwFlags
& CRYPT_VERIFYCONTEXT
))
1233 store_key_container_keys(pKeyContainer
);
1234 store_key_container_permissions(pKeyContainer
);
1235 release_key_container_keys(pKeyContainer
);
1238 release_key_container_keys(pKeyContainer
);
1239 HeapFree( GetProcessHeap(), 0, pKeyContainer
);
1242 /******************************************************************************
1243 * new_key_container [Internal]
1245 * Create a new key container. The personality (RSA Base, Strong or Enhanced CP)
1246 * of the CSP is determined via the pVTable->pszProvName string.
1249 * pszContainerName [I] Name of the key container.
1250 * pVTable [I] Callback functions and context info provided by the OS
1253 * Success: Handle to the new key container.
1254 * Failure: INVALID_HANDLE_VALUE
1256 static HCRYPTPROV
new_key_container(PCCH pszContainerName
, DWORD dwFlags
, const VTableProvStruc
*pVTable
)
1258 KEYCONTAINER
*pKeyContainer
;
1259 HCRYPTPROV hKeyContainer
;
1261 hKeyContainer
= new_object(&handle_table
, sizeof(KEYCONTAINER
), RSAENH_MAGIC_CONTAINER
,
1262 destroy_key_container
, (OBJECTHDR
**)&pKeyContainer
);
1263 if (hKeyContainer
!= (HCRYPTPROV
)INVALID_HANDLE_VALUE
)
1265 lstrcpynA(pKeyContainer
->szName
, pszContainerName
, MAX_PATH
);
1266 pKeyContainer
->dwFlags
= dwFlags
;
1267 pKeyContainer
->dwEnumAlgsCtr
= 0;
1268 pKeyContainer
->hKeyExchangeKeyPair
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
1269 pKeyContainer
->hSignatureKeyPair
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
1270 if (pVTable
&& pVTable
->pszProvName
) {
1271 lstrcpynA(pKeyContainer
->szProvName
, pVTable
->pszProvName
, MAX_PATH
);
1272 if (!strcmp(pVTable
->pszProvName
, MS_DEF_PROV_A
)) {
1273 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_BASE
;
1274 } else if (!strcmp(pVTable
->pszProvName
, MS_ENHANCED_PROV_A
)) {
1275 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_ENHANCED
;
1276 } else if (!strcmp(pVTable
->pszProvName
, MS_DEF_RSA_SCHANNEL_PROV_A
)) {
1277 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_SCHANNEL
;
1278 } else if (!strcmp(pVTable
->pszProvName
, MS_ENH_RSA_AES_PROV_A
)) {
1279 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_AES
;
1281 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_STRONG
;
1285 /* The new key container has to be inserted into the CSP immediately
1286 * after creation to be available for CPGetProvParam's PP_ENUMCONTAINERS. */
1287 if (!(dwFlags
& CRYPT_VERIFYCONTEXT
)) {
1290 if (create_container_key(pKeyContainer
, KEY_WRITE
, &hKey
))
1295 return hKeyContainer
;
1298 /******************************************************************************
1299 * read_key_value [Internal]
1301 * Reads a key pair value from the registry
1304 * hKeyContainer [I] Crypt provider to use to import the key
1305 * hKey [I] Registry key from which to read the key pair
1306 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
1307 * dwFlags [I] Flags for unprotecting the key
1308 * phCryptKey [O] Returned key
1310 static BOOL
read_key_value(HCRYPTPROV hKeyContainer
, HKEY hKey
, DWORD dwKeySpec
, DWORD dwFlags
, HCRYPTKEY
*phCryptKey
)
1313 DWORD dwValueType
, dwLen
;
1315 DATA_BLOB blobIn
, blobOut
;
1318 if (!(szValueName
= map_key_spec_to_key_pair_name(dwKeySpec
)))
1320 if (RegQueryValueExA(hKey
, szValueName
, 0, &dwValueType
, NULL
, &dwLen
) ==
1323 pbKey
= HeapAlloc(GetProcessHeap(), 0, dwLen
);
1326 if (RegQueryValueExA(hKey
, szValueName
, 0, &dwValueType
, pbKey
, &dwLen
) ==
1329 blobIn
.pbData
= pbKey
;
1330 blobIn
.cbData
= dwLen
;
1332 if (CryptUnprotectData(&blobIn
, NULL
, NULL
, NULL
, NULL
,
1335 ret
= import_key(hKeyContainer
, blobOut
.pbData
, blobOut
.cbData
, 0, 0,
1337 LocalFree(blobOut
.pbData
);
1340 HeapFree(GetProcessHeap(), 0, pbKey
);
1347 if (lookup_handle(&handle_table
, *phCryptKey
, RSAENH_MAGIC_KEY
,
1348 (OBJECTHDR
**)&pKey
))
1350 if ((szValueName
= map_key_spec_to_permissions_name(dwKeySpec
)))
1352 dwLen
= sizeof(pKey
->dwPermissions
);
1353 RegQueryValueExA(hKey
, szValueName
, 0, NULL
,
1354 (BYTE
*)&pKey
->dwPermissions
, &dwLen
);
1361 /******************************************************************************
1362 * read_key_container [Internal]
1364 * Tries to read the persistent state of the key container (mainly the signature
1365 * and key exchange private keys) given by pszContainerName.
1368 * pszContainerName [I] Name of the key container to read from the registry
1369 * pVTable [I] Pointer to context data provided by the operating system
1372 * Success: Handle to the key container read from the registry
1373 * Failure: INVALID_HANDLE_VALUE
1375 static HCRYPTPROV
read_key_container(PCHAR pszContainerName
, DWORD dwFlags
, const VTableProvStruc
*pVTable
)
1378 KEYCONTAINER
*pKeyContainer
;
1379 HCRYPTPROV hKeyContainer
;
1380 HCRYPTKEY hCryptKey
;
1382 if (!open_container_key(pszContainerName
, dwFlags
, &hKey
))
1384 SetLastError(NTE_BAD_KEYSET
);
1385 return (HCRYPTPROV
)INVALID_HANDLE_VALUE
;
1388 hKeyContainer
= new_key_container(pszContainerName
, dwFlags
, pVTable
);
1389 if (hKeyContainer
!= (HCRYPTPROV
)INVALID_HANDLE_VALUE
)
1391 DWORD dwProtectFlags
= (dwFlags
& CRYPT_MACHINE_KEYSET
) ?
1392 CRYPTPROTECT_LOCAL_MACHINE
: 0;
1394 if (!lookup_handle(&handle_table
, hKeyContainer
, RSAENH_MAGIC_CONTAINER
,
1395 (OBJECTHDR
**)&pKeyContainer
))
1396 return (HCRYPTPROV
)INVALID_HANDLE_VALUE
;
1398 /* read_key_value calls import_key, which calls import_private_key,
1399 * which implicitly installs the key value into the appropriate key
1400 * container key. Thus the ref count is incremented twice, once for
1401 * the output key value, and once for the implicit install, and needs
1402 * to be decremented to balance the two.
1404 if (read_key_value(hKeyContainer
, hKey
, AT_KEYEXCHANGE
,
1405 dwProtectFlags
, &hCryptKey
))
1406 release_handle(&handle_table
, hCryptKey
, RSAENH_MAGIC_KEY
);
1407 if (read_key_value(hKeyContainer
, hKey
, AT_SIGNATURE
,
1408 dwProtectFlags
, &hCryptKey
))
1409 release_handle(&handle_table
, hCryptKey
, RSAENH_MAGIC_KEY
);
1412 return hKeyContainer
;
1415 /******************************************************************************
1416 * build_hash_signature [Internal]
1418 * Builds a padded version of a hash to match the length of the RSA key modulus.
1421 * pbSignature [O] The padded hash object is stored here.
1422 * dwLen [I] Length of the pbSignature buffer.
1423 * aiAlgid [I] Algorithm identifier of the hash to be padded.
1424 * abHashValue [I] The value of the hash object.
1425 * dwHashLen [I] Length of the hash value.
1426 * dwFlags [I] Selection of padding algorithm.
1430 * Failure: FALSE (NTE_BAD_ALGID)
1432 static BOOL
build_hash_signature(BYTE
*pbSignature
, DWORD dwLen
, ALG_ID aiAlgid
,
1433 CONST BYTE
*abHashValue
, DWORD dwHashLen
, DWORD dwFlags
)
1435 /* These prefixes are meant to be concatenated with hash values of the
1436 * respective kind to form a PKCS #7 DigestInfo. */
1437 static const struct tagOIDDescriptor
{
1440 CONST BYTE abOID
[19];
1441 } aOIDDescriptor
[] = {
1442 { CALG_MD2
, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1443 0x86, 0xf7, 0x0d, 0x02, 0x02, 0x05, 0x00, 0x04, 0x10 } },
1444 { CALG_MD4
, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1445 0x86, 0xf7, 0x0d, 0x02, 0x04, 0x05, 0x00, 0x04, 0x10 } },
1446 { CALG_MD5
, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1447 0x86, 0xf7, 0x0d, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10 } },
1448 { CALG_SHA
, 15, { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03,
1449 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14 } },
1450 { CALG_SHA_256
, 19, { 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
1451 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01,
1452 0x05, 0x00, 0x04, 0x20 } },
1453 { CALG_SHA_384
, 19, { 0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
1454 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01,
1455 0x05, 0x00, 0x04, 0x30 } },
1456 { CALG_SHA_384
, 19, { 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
1457 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01,
1458 0x05, 0x00, 0x04, 0x40 } },
1459 { CALG_SSL3_SHAMD5
, 0, { 0 } },
1462 DWORD dwIdxOID
, i
, j
;
1464 for (dwIdxOID
= 0; aOIDDescriptor
[dwIdxOID
].aiAlgid
; dwIdxOID
++) {
1465 if (aOIDDescriptor
[dwIdxOID
].aiAlgid
== aiAlgid
) break;
1468 if (!aOIDDescriptor
[dwIdxOID
].aiAlgid
) {
1469 SetLastError(NTE_BAD_ALGID
);
1473 /* Build the padded signature */
1474 if (dwFlags
& CRYPT_X931_FORMAT
) {
1475 pbSignature
[0] = 0x6b;
1476 for (i
=1; i
< dwLen
- dwHashLen
- 3; i
++) {
1477 pbSignature
[i
] = 0xbb;
1479 pbSignature
[i
++] = 0xba;
1480 for (j
=0; j
< dwHashLen
; j
++, i
++) {
1481 pbSignature
[i
] = abHashValue
[j
];
1483 pbSignature
[i
++] = 0x33;
1484 pbSignature
[i
++] = 0xcc;
1486 pbSignature
[0] = 0x00;
1487 pbSignature
[1] = 0x01;
1488 if (dwFlags
& CRYPT_NOHASHOID
) {
1489 for (i
=2; i
< dwLen
- 1 - dwHashLen
; i
++) {
1490 pbSignature
[i
] = 0xff;
1492 pbSignature
[i
++] = 0x00;
1494 for (i
=2; i
< dwLen
- 1 - aOIDDescriptor
[dwIdxOID
].dwLen
- dwHashLen
; i
++) {
1495 pbSignature
[i
] = 0xff;
1497 pbSignature
[i
++] = 0x00;
1498 for (j
=0; j
< aOIDDescriptor
[dwIdxOID
].dwLen
; j
++) {
1499 pbSignature
[i
++] = aOIDDescriptor
[dwIdxOID
].abOID
[j
];
1502 for (j
=0; j
< dwHashLen
; j
++) {
1503 pbSignature
[i
++] = abHashValue
[j
];
1510 /******************************************************************************
1513 * This is an implementation of the 'P_hash' helper function for TLS1's PRF.
1514 * It is used exclusively by tls1_prf. For details see RFC 2246, chapter 5.
1515 * The pseudo random stream generated by this function is exclusive or'ed with
1516 * the data in pbBuffer.
1519 * hHMAC [I] HMAC object, which will be used in pseudo random generation
1520 * pblobSeed [I] Seed value
1521 * pbBuffer [I/O] Pseudo random stream will be xor'ed to the provided data
1522 * dwBufferLen [I] Number of pseudo random bytes desired
1528 static BOOL
tls1_p(HCRYPTHASH hHMAC
, CONST PCRYPT_DATA_BLOB pblobSeed
, PBYTE pbBuffer
, DWORD dwBufferLen
)
1531 BYTE abAi
[RSAENH_MAX_HASH_SIZE
];
1534 if (!lookup_handle(&handle_table
, hHMAC
, RSAENH_MAGIC_HASH
, (OBJECTHDR
**)&pHMAC
)) {
1535 SetLastError(NTE_BAD_HASH
);
1539 /* compute A_1 = HMAC(seed) */
1541 update_hash(pHMAC
, pblobSeed
->pbData
, pblobSeed
->cbData
);
1542 finalize_hash(pHMAC
);
1543 memcpy(abAi
, pHMAC
->abHashValue
, pHMAC
->dwHashSize
);
1546 /* compute HMAC(A_i + seed) */
1548 update_hash(pHMAC
, abAi
, pHMAC
->dwHashSize
);
1549 update_hash(pHMAC
, pblobSeed
->pbData
, pblobSeed
->cbData
);
1550 finalize_hash(pHMAC
);
1552 /* pseudo random stream := CONCAT_{i=1..n} ( HMAC(A_i + seed) ) */
1554 if (i
>= dwBufferLen
) break;
1555 pbBuffer
[i
] ^= pHMAC
->abHashValue
[i
% pHMAC
->dwHashSize
];
1557 } while (i
% pHMAC
->dwHashSize
);
1559 /* compute A_{i+1} = HMAC(A_i) */
1561 update_hash(pHMAC
, abAi
, pHMAC
->dwHashSize
);
1562 finalize_hash(pHMAC
);
1563 memcpy(abAi
, pHMAC
->abHashValue
, pHMAC
->dwHashSize
);
1564 } while (i
< dwBufferLen
);
1569 /******************************************************************************
1570 * tls1_prf [Internal]
1572 * TLS1 pseudo random function as specified in RFC 2246, chapter 5
1575 * hProv [I] Key container used to compute the pseudo random stream
1576 * hSecret [I] Key that holds the (pre-)master secret
1577 * pblobLabel [I] Descriptive label
1578 * pblobSeed [I] Seed value
1579 * pbBuffer [O] Pseudo random numbers will be stored here
1580 * dwBufferLen [I] Number of pseudo random bytes desired
1586 static BOOL
tls1_prf(HCRYPTPROV hProv
, HCRYPTPROV hSecret
, CONST PCRYPT_DATA_BLOB pblobLabel
,
1587 CONST PCRYPT_DATA_BLOB pblobSeed
, PBYTE pbBuffer
, DWORD dwBufferLen
)
1589 HMAC_INFO hmacInfo
= { 0, NULL
, 0, NULL
, 0 };
1590 HCRYPTHASH hHMAC
= (HCRYPTHASH
)INVALID_HANDLE_VALUE
;
1591 HCRYPTKEY hHalfSecret
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
1592 CRYPTKEY
*pHalfSecret
, *pSecret
;
1593 DWORD dwHalfSecretLen
;
1594 BOOL result
= FALSE
;
1595 CRYPT_DATA_BLOB blobLabelSeed
;
1597 TRACE("(hProv=%08lx, hSecret=%08lx, pblobLabel=%p, pblobSeed=%p, pbBuffer=%p, dwBufferLen=%d)\n",
1598 hProv
, hSecret
, pblobLabel
, pblobSeed
, pbBuffer
, dwBufferLen
);
1600 if (!lookup_handle(&handle_table
, hSecret
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pSecret
)) {
1601 SetLastError(NTE_FAIL
);
1605 dwHalfSecretLen
= (pSecret
->dwKeyLen
+1)/2;
1607 /* concatenation of the label and the seed */
1608 if (!concat_data_blobs(&blobLabelSeed
, pblobLabel
, pblobSeed
)) goto exit
;
1610 /* zero out the buffer, since two random streams will be xor'ed into it. */
1611 memset(pbBuffer
, 0, dwBufferLen
);
1613 /* build a 'fake' key, to hold the secret. CALG_SSL2_MASTER is used since it provides
1614 * the biggest range of valid key lengths. */
1615 hHalfSecret
= new_key(hProv
, CALG_SSL2_MASTER
, MAKELONG(0,dwHalfSecretLen
*8), &pHalfSecret
);
1616 if (hHalfSecret
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) goto exit
;
1618 /* Derive an HMAC_MD5 hash and call the helper function. */
1619 memcpy(pHalfSecret
->abKeyValue
, pSecret
->abKeyValue
, dwHalfSecretLen
);
1620 if (!RSAENH_CPCreateHash(hProv
, CALG_HMAC
, hHalfSecret
, 0, &hHMAC
)) goto exit
;
1621 hmacInfo
.HashAlgid
= CALG_MD5
;
1622 if (!RSAENH_CPSetHashParam(hProv
, hHMAC
, HP_HMAC_INFO
, (BYTE
*)&hmacInfo
, 0)) goto exit
;
1623 if (!tls1_p(hHMAC
, &blobLabelSeed
, pbBuffer
, dwBufferLen
)) goto exit
;
1625 /* Reconfigure to HMAC_SHA hash and call helper function again. */
1626 memcpy(pHalfSecret
->abKeyValue
, pSecret
->abKeyValue
+ (pSecret
->dwKeyLen
/2), dwHalfSecretLen
);
1627 hmacInfo
.HashAlgid
= CALG_SHA
;
1628 if (!RSAENH_CPSetHashParam(hProv
, hHMAC
, HP_HMAC_INFO
, (BYTE
*)&hmacInfo
, 0)) goto exit
;
1629 if (!tls1_p(hHMAC
, &blobLabelSeed
, pbBuffer
, dwBufferLen
)) goto exit
;
1633 release_handle(&handle_table
, hHalfSecret
, RSAENH_MAGIC_KEY
);
1634 if (hHMAC
!= (HCRYPTHASH
)INVALID_HANDLE_VALUE
) RSAENH_CPDestroyHash(hProv
, hHMAC
);
1635 free_data_blob(&blobLabelSeed
);
1639 /******************************************************************************
1640 * pad_data [Internal]
1642 * Helper function for data padding according to PKCS1 #2
1645 * abData [I] The data to be padded
1646 * dwDataLen [I] Length of the data
1647 * abBuffer [O] Padded data will be stored here
1648 * dwBufferLen [I] Length of the buffer (also length of padded data)
1649 * dwFlags [I] Padding format (CRYPT_SSL2_FALLBACK)
1653 * Failure: FALSE (NTE_BAD_LEN, too much data to pad)
1655 static BOOL
pad_data(CONST BYTE
*abData
, DWORD dwDataLen
, BYTE
*abBuffer
, DWORD dwBufferLen
,
1660 /* Ensure there is enough space for PKCS1 #2 padding */
1661 if (dwDataLen
> dwBufferLen
-11) {
1662 SetLastError(NTE_BAD_LEN
);
1666 memmove(abBuffer
+ dwBufferLen
- dwDataLen
, abData
, dwDataLen
);
1669 abBuffer
[1] = RSAENH_PKC_BLOCKTYPE
;
1670 for (i
=2; i
< dwBufferLen
- dwDataLen
- 1; i
++)
1671 do gen_rand_impl(&abBuffer
[i
], 1); while (!abBuffer
[i
]);
1672 if (dwFlags
& CRYPT_SSL2_FALLBACK
)
1673 for (i
-=8; i
< dwBufferLen
- dwDataLen
- 1; i
++)
1680 /******************************************************************************
1681 * unpad_data [Internal]
1683 * Remove the PKCS1 padding from RSA decrypted data
1686 * abData [I] The padded data
1687 * dwDataLen [I] Length of the padded data
1688 * abBuffer [O] Data without padding will be stored here
1689 * dwBufferLen [I/O] I: Length of the buffer, O: Length of unpadded data
1690 * dwFlags [I] Currently none defined
1694 * Failure: FALSE, (NTE_BAD_DATA, no valid PKCS1 padding or buffer too small)
1696 static BOOL
unpad_data(CONST BYTE
*abData
, DWORD dwDataLen
, BYTE
*abBuffer
, DWORD
*dwBufferLen
,
1703 SetLastError(NTE_BAD_DATA
);
1706 for (i
=2; i
<dwDataLen
; i
++)
1710 if ((i
== dwDataLen
) || (*dwBufferLen
< dwDataLen
- i
- 1) ||
1711 (abData
[0] != 0x00) || (abData
[1] != RSAENH_PKC_BLOCKTYPE
))
1713 SetLastError(NTE_BAD_DATA
);
1717 *dwBufferLen
= dwDataLen
- i
- 1;
1718 memmove(abBuffer
, abData
+ i
+ 1, *dwBufferLen
);
1722 /******************************************************************************
1723 * CPAcquireContext (RSAENH.@)
1725 * Acquire a handle to the key container specified by pszContainer
1728 * phProv [O] Pointer to the location the acquired handle will be written to.
1729 * pszContainer [I] Name of the desired key container. See Notes
1730 * dwFlags [I] Flags. See Notes.
1731 * pVTable [I] Pointer to a PVTableProvStruct containing callbacks.
1738 * If pszContainer is NULL or points to a zero length string the user's login
1739 * name will be used as the key container name.
1741 * If the CRYPT_NEW_KEYSET flag is set in dwFlags a new keyset will be created.
1742 * If a keyset with the given name already exists, the function fails and sets
1743 * last error to NTE_EXISTS. If CRYPT_NEW_KEYSET is not set and the specified
1744 * key container does not exist, function fails and sets last error to
1747 BOOL WINAPI
RSAENH_CPAcquireContext(HCRYPTPROV
*phProv
, LPSTR pszContainer
,
1748 DWORD dwFlags
, PVTableProvStruc pVTable
)
1750 CHAR szKeyContainerName
[MAX_PATH
];
1752 TRACE("(phProv=%p, pszContainer=%s, dwFlags=%08x, pVTable=%p)\n", phProv
,
1753 debugstr_a(pszContainer
), dwFlags
, pVTable
);
1755 if (pszContainer
&& *pszContainer
)
1757 lstrcpynA(szKeyContainerName
, pszContainer
, MAX_PATH
);
1761 DWORD dwLen
= sizeof(szKeyContainerName
);
1762 if (!GetUserNameA(szKeyContainerName
, &dwLen
)) return FALSE
;
1765 switch (dwFlags
& (CRYPT_NEWKEYSET
|CRYPT_VERIFYCONTEXT
|CRYPT_DELETEKEYSET
))
1768 *phProv
= read_key_container(szKeyContainerName
, dwFlags
, pVTable
);
1771 case CRYPT_DELETEKEYSET
:
1772 return delete_container_key(szKeyContainerName
, dwFlags
);
1774 case CRYPT_NEWKEYSET
:
1775 *phProv
= read_key_container(szKeyContainerName
, dwFlags
, pVTable
);
1776 if (*phProv
!= (HCRYPTPROV
)INVALID_HANDLE_VALUE
)
1778 release_handle(&handle_table
, *phProv
, RSAENH_MAGIC_CONTAINER
);
1779 TRACE("Can't create new keyset, already exists\n");
1780 SetLastError(NTE_EXISTS
);
1783 *phProv
= new_key_container(szKeyContainerName
, dwFlags
, pVTable
);
1786 case CRYPT_VERIFYCONTEXT
|CRYPT_NEWKEYSET
:
1787 case CRYPT_VERIFYCONTEXT
:
1788 if (pszContainer
&& *pszContainer
) {
1789 TRACE("pszContainer should be empty\n");
1790 SetLastError(NTE_BAD_FLAGS
);
1793 *phProv
= new_key_container("", dwFlags
, pVTable
);
1797 *phProv
= (HCRYPTPROV
)INVALID_HANDLE_VALUE
;
1798 SetLastError(NTE_BAD_FLAGS
);
1802 if (*phProv
!= (HCRYPTPROV
)INVALID_HANDLE_VALUE
) {
1803 SetLastError(ERROR_SUCCESS
);
1810 /******************************************************************************
1811 * CPCreateHash (RSAENH.@)
1813 * CPCreateHash creates and initializes a new hash object.
1816 * hProv [I] Handle to the key container to which the new hash will belong.
1817 * Algid [I] Identifies the hash algorithm, which will be used for the hash.
1818 * hKey [I] Handle to a session key applied for keyed hashes.
1819 * dwFlags [I] Currently no flags defined. Must be zero.
1820 * phHash [O] Points to the location where a handle to the new hash will be stored.
1827 * hKey is a handle to a session key applied in keyed hashes like MAC and HMAC.
1828 * If a normal hash object is to be created (like e.g. MD2 or SHA1) hKey must be zero.
1830 BOOL WINAPI
RSAENH_CPCreateHash(HCRYPTPROV hProv
, ALG_ID Algid
, HCRYPTKEY hKey
, DWORD dwFlags
,
1833 CRYPTKEY
*pCryptKey
;
1834 CRYPTHASH
*pCryptHash
;
1835 const PROV_ENUMALGS_EX
*peaAlgidInfo
;
1837 TRACE("(hProv=%08lx, Algid=%08x, hKey=%08lx, dwFlags=%08x, phHash=%p)\n", hProv
, Algid
, hKey
,
1840 peaAlgidInfo
= get_algid_info(hProv
, Algid
);
1841 if (!peaAlgidInfo
) return FALSE
;
1845 SetLastError(NTE_BAD_FLAGS
);
1849 if (Algid
== CALG_MAC
|| Algid
== CALG_HMAC
|| Algid
== CALG_SCHANNEL_MASTER_HASH
||
1850 Algid
== CALG_TLS1PRF
)
1852 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
)) {
1853 SetLastError(NTE_BAD_KEY
);
1857 if ((Algid
== CALG_MAC
) && (GET_ALG_TYPE(pCryptKey
->aiAlgid
) != ALG_TYPE_BLOCK
)) {
1858 SetLastError(NTE_BAD_KEY
);
1862 if ((Algid
== CALG_SCHANNEL_MASTER_HASH
|| Algid
== CALG_TLS1PRF
) &&
1863 (pCryptKey
->aiAlgid
!= CALG_TLS1_MASTER
))
1865 SetLastError(NTE_BAD_KEY
);
1868 if (Algid
== CALG_SCHANNEL_MASTER_HASH
&&
1869 ((!pCryptKey
->siSChannelInfo
.blobClientRandom
.cbData
) ||
1870 (!pCryptKey
->siSChannelInfo
.blobServerRandom
.cbData
)))
1872 SetLastError(ERROR_INVALID_PARAMETER
);
1876 if ((Algid
== CALG_TLS1PRF
) && (pCryptKey
->dwState
!= RSAENH_KEYSTATE_MASTERKEY
)) {
1877 SetLastError(NTE_BAD_KEY_STATE
);
1882 *phHash
= new_object(&handle_table
, sizeof(CRYPTHASH
), RSAENH_MAGIC_HASH
,
1883 destroy_hash
, (OBJECTHDR
**)&pCryptHash
);
1884 if (!pCryptHash
) return FALSE
;
1886 pCryptHash
->aiAlgid
= Algid
;
1887 pCryptHash
->hKey
= hKey
;
1888 pCryptHash
->hProv
= hProv
;
1889 pCryptHash
->dwState
= RSAENH_HASHSTATE_HASHING
;
1890 pCryptHash
->pHMACInfo
= NULL
;
1891 pCryptHash
->dwHashSize
= peaAlgidInfo
->dwDefaultLen
>> 3;
1892 init_data_blob(&pCryptHash
->tpPRFParams
.blobLabel
);
1893 init_data_blob(&pCryptHash
->tpPRFParams
.blobSeed
);
1895 if (Algid
== CALG_SCHANNEL_MASTER_HASH
) {
1896 static const char keyex
[] = "key expansion";
1897 BYTE key_expansion
[sizeof keyex
];
1898 CRYPT_DATA_BLOB blobRandom
, blobKeyExpansion
= { 13, key_expansion
};
1900 memcpy( key_expansion
, keyex
, sizeof keyex
);
1902 if (pCryptKey
->dwState
!= RSAENH_KEYSTATE_MASTERKEY
) {
1903 static const char msec
[] = "master secret";
1904 BYTE master_secret
[sizeof msec
];
1905 CRYPT_DATA_BLOB blobLabel
= { 13, master_secret
};
1906 BYTE abKeyValue
[48];
1908 memcpy( master_secret
, msec
, sizeof msec
);
1910 /* See RFC 2246, chapter 8.1 */
1911 if (!concat_data_blobs(&blobRandom
,
1912 &pCryptKey
->siSChannelInfo
.blobClientRandom
,
1913 &pCryptKey
->siSChannelInfo
.blobServerRandom
))
1917 tls1_prf(hProv
, hKey
, &blobLabel
, &blobRandom
, abKeyValue
, 48);
1918 pCryptKey
->dwState
= RSAENH_KEYSTATE_MASTERKEY
;
1919 memcpy(pCryptKey
->abKeyValue
, abKeyValue
, 48);
1920 free_data_blob(&blobRandom
);
1923 /* See RFC 2246, chapter 6.3 */
1924 if (!concat_data_blobs(&blobRandom
,
1925 &pCryptKey
->siSChannelInfo
.blobServerRandom
,
1926 &pCryptKey
->siSChannelInfo
.blobClientRandom
))
1930 tls1_prf(hProv
, hKey
, &blobKeyExpansion
, &blobRandom
, pCryptHash
->abHashValue
,
1931 RSAENH_MAX_HASH_SIZE
);
1932 free_data_blob(&blobRandom
);
1935 return init_hash(pCryptHash
);
1938 /******************************************************************************
1939 * CPDestroyHash (RSAENH.@)
1941 * Releases the handle to a hash object. The object is destroyed if its reference
1942 * count reaches zero.
1945 * hProv [I] Handle to the key container to which the hash object belongs.
1946 * hHash [I] Handle to the hash object to be released.
1952 BOOL WINAPI
RSAENH_CPDestroyHash(HCRYPTPROV hProv
, HCRYPTHASH hHash
)
1954 TRACE("(hProv=%08lx, hHash=%08lx)\n", hProv
, hHash
);
1956 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
1958 SetLastError(NTE_BAD_UID
);
1962 if (!release_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
))
1964 SetLastError(NTE_BAD_HASH
);
1971 /******************************************************************************
1972 * CPDestroyKey (RSAENH.@)
1974 * Releases the handle to a key object. The object is destroyed if its reference
1975 * count reaches zero.
1978 * hProv [I] Handle to the key container to which the key object belongs.
1979 * hKey [I] Handle to the key object to be released.
1985 BOOL WINAPI
RSAENH_CPDestroyKey(HCRYPTPROV hProv
, HCRYPTKEY hKey
)
1987 TRACE("(hProv=%08lx, hKey=%08lx)\n", hProv
, hKey
);
1989 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
1991 SetLastError(NTE_BAD_UID
);
1995 if (!release_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
))
1997 SetLastError(NTE_BAD_KEY
);
2004 /******************************************************************************
2005 * CPDuplicateHash (RSAENH.@)
2007 * Clones a hash object including its current state.
2010 * hUID [I] Handle to the key container the hash belongs to.
2011 * hHash [I] Handle to the hash object to be cloned.
2012 * pdwReserved [I] Reserved. Must be NULL.
2013 * dwFlags [I] No flags are currently defined. Must be 0.
2014 * phHash [O] Handle to the cloned hash object.
2020 BOOL WINAPI
RSAENH_CPDuplicateHash(HCRYPTPROV hUID
, HCRYPTHASH hHash
, DWORD
*pdwReserved
,
2021 DWORD dwFlags
, HCRYPTHASH
*phHash
)
2023 CRYPTHASH
*pSrcHash
, *pDestHash
;
2025 TRACE("(hUID=%08lx, hHash=%08lx, pdwReserved=%p, dwFlags=%08x, phHash=%p)\n", hUID
, hHash
,
2026 pdwReserved
, dwFlags
, phHash
);
2028 if (!is_valid_handle(&handle_table
, hUID
, RSAENH_MAGIC_CONTAINER
))
2030 SetLastError(NTE_BAD_UID
);
2034 if (!lookup_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
, (OBJECTHDR
**)&pSrcHash
))
2036 SetLastError(NTE_BAD_HASH
);
2040 if (!phHash
|| pdwReserved
|| dwFlags
)
2042 SetLastError(ERROR_INVALID_PARAMETER
);
2046 *phHash
= new_object(&handle_table
, sizeof(CRYPTHASH
), RSAENH_MAGIC_HASH
,
2047 destroy_hash
, (OBJECTHDR
**)&pDestHash
);
2048 if (*phHash
!= (HCRYPTHASH
)INVALID_HANDLE_VALUE
)
2050 *pDestHash
= *pSrcHash
;
2051 duplicate_hash_impl(pSrcHash
->aiAlgid
, &pSrcHash
->context
, &pDestHash
->context
);
2052 copy_hmac_info(&pDestHash
->pHMACInfo
, pSrcHash
->pHMACInfo
);
2053 copy_data_blob(&pDestHash
->tpPRFParams
.blobLabel
, &pSrcHash
->tpPRFParams
.blobLabel
);
2054 copy_data_blob(&pDestHash
->tpPRFParams
.blobSeed
, &pSrcHash
->tpPRFParams
.blobSeed
);
2057 return *phHash
!= (HCRYPTHASH
)INVALID_HANDLE_VALUE
;
2060 /******************************************************************************
2061 * CPDuplicateKey (RSAENH.@)
2063 * Clones a key object including its current state.
2066 * hUID [I] Handle to the key container the hash belongs to.
2067 * hKey [I] Handle to the key object to be cloned.
2068 * pdwReserved [I] Reserved. Must be NULL.
2069 * dwFlags [I] No flags are currently defined. Must be 0.
2070 * phHash [O] Handle to the cloned key object.
2076 BOOL WINAPI
RSAENH_CPDuplicateKey(HCRYPTPROV hUID
, HCRYPTKEY hKey
, DWORD
*pdwReserved
,
2077 DWORD dwFlags
, HCRYPTKEY
*phKey
)
2079 CRYPTKEY
*pSrcKey
, *pDestKey
;
2081 TRACE("(hUID=%08lx, hKey=%08lx, pdwReserved=%p, dwFlags=%08x, phKey=%p)\n", hUID
, hKey
,
2082 pdwReserved
, dwFlags
, phKey
);
2084 if (!is_valid_handle(&handle_table
, hUID
, RSAENH_MAGIC_CONTAINER
))
2086 SetLastError(NTE_BAD_UID
);
2090 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pSrcKey
))
2092 SetLastError(NTE_BAD_KEY
);
2096 if (!phKey
|| pdwReserved
|| dwFlags
)
2098 SetLastError(ERROR_INVALID_PARAMETER
);
2102 *phKey
= new_object(&handle_table
, sizeof(CRYPTKEY
), RSAENH_MAGIC_KEY
, destroy_key
,
2103 (OBJECTHDR
**)&pDestKey
);
2104 if (*phKey
!= (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
2106 *pDestKey
= *pSrcKey
;
2107 copy_data_blob(&pDestKey
->siSChannelInfo
.blobServerRandom
,
2108 &pSrcKey
->siSChannelInfo
.blobServerRandom
);
2109 copy_data_blob(&pDestKey
->siSChannelInfo
.blobClientRandom
,
2110 &pSrcKey
->siSChannelInfo
.blobClientRandom
);
2111 duplicate_key_impl(pSrcKey
->aiAlgid
, &pSrcKey
->context
, &pDestKey
->context
);
2120 /******************************************************************************
2121 * CPEncrypt (RSAENH.@)
2126 * hProv [I] The key container hKey and hHash belong to.
2127 * hKey [I] The key used to encrypt the data.
2128 * hHash [I] An optional hash object for parallel hashing. See notes.
2129 * Final [I] Indicates if this is the last block of data to encrypt.
2130 * dwFlags [I] Currently no flags defined. Must be zero.
2131 * pbData [I/O] Pointer to the data to encrypt. Encrypted data will also be stored there.
2132 * pdwDataLen [I/O] I: Length of data to encrypt, O: Length of encrypted data.
2133 * dwBufLen [I] Size of the buffer at pbData.
2140 * If a hash object handle is provided in hHash, it will be updated with the plaintext.
2141 * This is useful for message signatures.
2143 * This function uses the standard WINAPI protocol for querying data of dynamic length.
2145 BOOL WINAPI
RSAENH_CPEncrypt(HCRYPTPROV hProv
, HCRYPTKEY hKey
, HCRYPTHASH hHash
, BOOL Final
,
2146 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
, DWORD dwBufLen
)
2148 CRYPTKEY
*pCryptKey
;
2149 BYTE
*in
, out
[RSAENH_MAX_BLOCK_SIZE
], o
[RSAENH_MAX_BLOCK_SIZE
];
2150 DWORD dwEncryptedLen
, i
, j
, k
;
2152 TRACE("(hProv=%08lx, hKey=%08lx, hHash=%08lx, Final=%d, dwFlags=%08x, pbData=%p, "
2153 "pdwDataLen=%p, dwBufLen=%d)\n", hProv
, hKey
, hHash
, Final
, dwFlags
, pbData
, pdwDataLen
,
2156 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
2158 SetLastError(NTE_BAD_UID
);
2164 SetLastError(NTE_BAD_FLAGS
);
2168 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
2170 SetLastError(NTE_BAD_KEY
);
2174 if (pCryptKey
->dwState
== RSAENH_KEYSTATE_IDLE
)
2175 pCryptKey
->dwState
= RSAENH_KEYSTATE_ENCRYPTING
;
2177 if (pCryptKey
->dwState
!= RSAENH_KEYSTATE_ENCRYPTING
)
2179 SetLastError(NTE_BAD_DATA
);
2183 if (is_valid_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
)) {
2184 if (!RSAENH_CPHashData(hProv
, hHash
, pbData
, *pdwDataLen
, 0)) return FALSE
;
2187 if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_BLOCK
) {
2188 if (!Final
&& (*pdwDataLen
% pCryptKey
->dwBlockLen
)) {
2189 SetLastError(NTE_BAD_DATA
);
2193 dwEncryptedLen
= (*pdwDataLen
/pCryptKey
->dwBlockLen
+(Final
?1:0))*pCryptKey
->dwBlockLen
;
2195 if (pbData
== NULL
) {
2196 *pdwDataLen
= dwEncryptedLen
;
2199 else if (dwEncryptedLen
> dwBufLen
) {
2200 *pdwDataLen
= dwEncryptedLen
;
2201 SetLastError(ERROR_MORE_DATA
);
2205 /* Pad final block with length bytes */
2206 for (i
=*pdwDataLen
; i
<dwEncryptedLen
; i
++) pbData
[i
] = dwEncryptedLen
- *pdwDataLen
;
2207 *pdwDataLen
= dwEncryptedLen
;
2209 for (i
=0, in
=pbData
; i
<*pdwDataLen
; i
+=pCryptKey
->dwBlockLen
, in
+=pCryptKey
->dwBlockLen
) {
2210 switch (pCryptKey
->dwMode
) {
2211 case CRYPT_MODE_ECB
:
2212 encrypt_block_impl(pCryptKey
->aiAlgid
, 0, &pCryptKey
->context
, in
, out
,
2216 case CRYPT_MODE_CBC
:
2217 for (j
=0; j
<pCryptKey
->dwBlockLen
; j
++) in
[j
] ^= pCryptKey
->abChainVector
[j
];
2218 encrypt_block_impl(pCryptKey
->aiAlgid
, 0, &pCryptKey
->context
, in
, out
,
2220 memcpy(pCryptKey
->abChainVector
, out
, pCryptKey
->dwBlockLen
);
2223 case CRYPT_MODE_CFB
:
2224 for (j
=0; j
<pCryptKey
->dwBlockLen
; j
++) {
2225 encrypt_block_impl(pCryptKey
->aiAlgid
, 0, &pCryptKey
->context
,
2226 pCryptKey
->abChainVector
, o
, RSAENH_ENCRYPT
);
2227 out
[j
] = in
[j
] ^ o
[0];
2228 for (k
=0; k
<pCryptKey
->dwBlockLen
-1; k
++)
2229 pCryptKey
->abChainVector
[k
] = pCryptKey
->abChainVector
[k
+1];
2230 pCryptKey
->abChainVector
[k
] = out
[j
];
2235 SetLastError(NTE_BAD_ALGID
);
2238 memcpy(in
, out
, pCryptKey
->dwBlockLen
);
2240 } else if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_STREAM
) {
2241 if (pbData
== NULL
) {
2242 *pdwDataLen
= dwBufLen
;
2245 encrypt_stream_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pbData
, *pdwDataLen
);
2246 } else if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_RSA
) {
2247 if (pCryptKey
->aiAlgid
== CALG_RSA_SIGN
) {
2248 SetLastError(NTE_BAD_KEY
);
2252 *pdwDataLen
= pCryptKey
->dwBlockLen
;
2255 if (dwBufLen
< pCryptKey
->dwBlockLen
) {
2256 SetLastError(ERROR_MORE_DATA
);
2259 if (!pad_data(pbData
, *pdwDataLen
, pbData
, pCryptKey
->dwBlockLen
, dwFlags
)) return FALSE
;
2260 encrypt_block_impl(pCryptKey
->aiAlgid
, PK_PUBLIC
, &pCryptKey
->context
, pbData
, pbData
, RSAENH_ENCRYPT
);
2261 *pdwDataLen
= pCryptKey
->dwBlockLen
;
2264 SetLastError(NTE_BAD_TYPE
);
2268 if (Final
) setup_key(pCryptKey
);
2273 /******************************************************************************
2274 * CPDecrypt (RSAENH.@)
2279 * hProv [I] The key container hKey and hHash belong to.
2280 * hKey [I] The key used to decrypt the data.
2281 * hHash [I] An optional hash object for parallel hashing. See notes.
2282 * Final [I] Indicates if this is the last block of data to decrypt.
2283 * dwFlags [I] Currently no flags defined. Must be zero.
2284 * pbData [I/O] Pointer to the data to decrypt. Plaintext will also be stored there.
2285 * pdwDataLen [I/O] I: Length of ciphertext, O: Length of plaintext.
2292 * If a hash object handle is provided in hHash, it will be updated with the plaintext.
2293 * This is useful for message signatures.
2295 * This function uses the standard WINAPI protocol for querying data of dynamic length.
2297 BOOL WINAPI
RSAENH_CPDecrypt(HCRYPTPROV hProv
, HCRYPTKEY hKey
, HCRYPTHASH hHash
, BOOL Final
,
2298 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
)
2300 CRYPTKEY
*pCryptKey
;
2301 BYTE
*in
, out
[RSAENH_MAX_BLOCK_SIZE
], o
[RSAENH_MAX_BLOCK_SIZE
];
2305 TRACE("(hProv=%08lx, hKey=%08lx, hHash=%08lx, Final=%d, dwFlags=%08x, pbData=%p, "
2306 "pdwDataLen=%p)\n", hProv
, hKey
, hHash
, Final
, dwFlags
, pbData
, pdwDataLen
);
2308 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
2310 SetLastError(NTE_BAD_UID
);
2316 SetLastError(NTE_BAD_FLAGS
);
2320 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
2322 SetLastError(NTE_BAD_KEY
);
2326 if (pCryptKey
->dwState
== RSAENH_KEYSTATE_IDLE
)
2327 pCryptKey
->dwState
= RSAENH_KEYSTATE_ENCRYPTING
;
2329 if (pCryptKey
->dwState
!= RSAENH_KEYSTATE_ENCRYPTING
)
2331 SetLastError(NTE_BAD_DATA
);
2337 if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_BLOCK
) {
2338 for (i
=0, in
=pbData
; i
<*pdwDataLen
; i
+=pCryptKey
->dwBlockLen
, in
+=pCryptKey
->dwBlockLen
) {
2339 switch (pCryptKey
->dwMode
) {
2340 case CRYPT_MODE_ECB
:
2341 encrypt_block_impl(pCryptKey
->aiAlgid
, 0, &pCryptKey
->context
, in
, out
,
2345 case CRYPT_MODE_CBC
:
2346 encrypt_block_impl(pCryptKey
->aiAlgid
, 0, &pCryptKey
->context
, in
, out
,
2348 for (j
=0; j
<pCryptKey
->dwBlockLen
; j
++) out
[j
] ^= pCryptKey
->abChainVector
[j
];
2349 memcpy(pCryptKey
->abChainVector
, in
, pCryptKey
->dwBlockLen
);
2352 case CRYPT_MODE_CFB
:
2353 for (j
=0; j
<pCryptKey
->dwBlockLen
; j
++) {
2354 encrypt_block_impl(pCryptKey
->aiAlgid
, 0, &pCryptKey
->context
,
2355 pCryptKey
->abChainVector
, o
, RSAENH_ENCRYPT
);
2356 out
[j
] = in
[j
] ^ o
[0];
2357 for (k
=0; k
<pCryptKey
->dwBlockLen
-1; k
++)
2358 pCryptKey
->abChainVector
[k
] = pCryptKey
->abChainVector
[k
+1];
2359 pCryptKey
->abChainVector
[k
] = in
[j
];
2364 SetLastError(NTE_BAD_ALGID
);
2367 memcpy(in
, out
, pCryptKey
->dwBlockLen
);
2370 if (pbData
[*pdwDataLen
-1] &&
2371 pbData
[*pdwDataLen
-1] <= pCryptKey
->dwBlockLen
&&
2372 pbData
[*pdwDataLen
-1] <= *pdwDataLen
) {
2373 BOOL padOkay
= TRUE
;
2375 /* check that every bad byte has the same value */
2376 for (i
= 1; padOkay
&& i
< pbData
[*pdwDataLen
-1]; i
++)
2377 if (pbData
[*pdwDataLen
- i
- 1] != pbData
[*pdwDataLen
- 1])
2380 *pdwDataLen
-= pbData
[*pdwDataLen
-1];
2382 SetLastError(NTE_BAD_DATA
);
2387 SetLastError(NTE_BAD_DATA
);
2392 } else if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_STREAM
) {
2393 encrypt_stream_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pbData
, *pdwDataLen
);
2394 } else if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_RSA
) {
2395 if (pCryptKey
->aiAlgid
== CALG_RSA_SIGN
) {
2396 SetLastError(NTE_BAD_KEY
);
2399 encrypt_block_impl(pCryptKey
->aiAlgid
, PK_PRIVATE
, &pCryptKey
->context
, pbData
, pbData
, RSAENH_DECRYPT
);
2400 if (!unpad_data(pbData
, pCryptKey
->dwBlockLen
, pbData
, pdwDataLen
, dwFlags
)) return FALSE
;
2403 SetLastError(NTE_BAD_TYPE
);
2407 if (Final
) setup_key(pCryptKey
);
2409 if (is_valid_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
)) {
2410 if (*pdwDataLen
>dwMax
||
2411 !RSAENH_CPHashData(hProv
, hHash
, pbData
, *pdwDataLen
, 0)) return FALSE
;
2417 static BOOL
crypt_export_simple(CRYPTKEY
*pCryptKey
, CRYPTKEY
*pPubKey
,
2418 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
)
2420 BLOBHEADER
*pBlobHeader
= (BLOBHEADER
*)pbData
;
2421 ALG_ID
*pAlgid
= (ALG_ID
*)(pBlobHeader
+1);
2424 if (!(GET_ALG_CLASS(pCryptKey
->aiAlgid
)&(ALG_CLASS_DATA_ENCRYPT
|ALG_CLASS_MSG_ENCRYPT
))) {
2425 SetLastError(NTE_BAD_KEY
); /* FIXME: error code? */
2429 dwDataLen
= sizeof(BLOBHEADER
) + sizeof(ALG_ID
) + pPubKey
->dwBlockLen
;
2431 if (*pdwDataLen
< dwDataLen
) {
2432 SetLastError(ERROR_MORE_DATA
);
2433 *pdwDataLen
= dwDataLen
;
2437 pBlobHeader
->bType
= SIMPLEBLOB
;
2438 pBlobHeader
->bVersion
= CUR_BLOB_VERSION
;
2439 pBlobHeader
->reserved
= 0;
2440 pBlobHeader
->aiKeyAlg
= pCryptKey
->aiAlgid
;
2442 *pAlgid
= pPubKey
->aiAlgid
;
2444 if (!pad_data(pCryptKey
->abKeyValue
, pCryptKey
->dwKeyLen
, (BYTE
*)(pAlgid
+1),
2445 pPubKey
->dwBlockLen
, dwFlags
))
2450 encrypt_block_impl(pPubKey
->aiAlgid
, PK_PUBLIC
, &pPubKey
->context
, (BYTE
*)(pAlgid
+1),
2451 (BYTE
*)(pAlgid
+1), RSAENH_ENCRYPT
);
2453 *pdwDataLen
= dwDataLen
;
2457 static BOOL
crypt_export_public_key(CRYPTKEY
*pCryptKey
, BYTE
*pbData
,
2460 BLOBHEADER
*pBlobHeader
= (BLOBHEADER
*)pbData
;
2461 RSAPUBKEY
*pRSAPubKey
= (RSAPUBKEY
*)(pBlobHeader
+1);
2464 if ((pCryptKey
->aiAlgid
!= CALG_RSA_KEYX
) && (pCryptKey
->aiAlgid
!= CALG_RSA_SIGN
)) {
2465 SetLastError(NTE_BAD_KEY
);
2469 dwDataLen
= sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) + pCryptKey
->dwKeyLen
;
2471 if (*pdwDataLen
< dwDataLen
) {
2472 SetLastError(ERROR_MORE_DATA
);
2473 *pdwDataLen
= dwDataLen
;
2477 pBlobHeader
->bType
= PUBLICKEYBLOB
;
2478 pBlobHeader
->bVersion
= CUR_BLOB_VERSION
;
2479 pBlobHeader
->reserved
= 0;
2480 pBlobHeader
->aiKeyAlg
= pCryptKey
->aiAlgid
;
2482 pRSAPubKey
->magic
= RSAENH_MAGIC_RSA1
;
2483 pRSAPubKey
->bitlen
= pCryptKey
->dwKeyLen
<< 3;
2485 export_public_key_impl((BYTE
*)(pRSAPubKey
+1), &pCryptKey
->context
,
2486 pCryptKey
->dwKeyLen
, &pRSAPubKey
->pubexp
);
2488 *pdwDataLen
= dwDataLen
;
2492 static BOOL
crypt_export_private_key(CRYPTKEY
*pCryptKey
, BOOL force
,
2493 BYTE
*pbData
, DWORD
*pdwDataLen
)
2495 BLOBHEADER
*pBlobHeader
= (BLOBHEADER
*)pbData
;
2496 RSAPUBKEY
*pRSAPubKey
= (RSAPUBKEY
*)(pBlobHeader
+1);
2499 if ((pCryptKey
->aiAlgid
!= CALG_RSA_KEYX
) && (pCryptKey
->aiAlgid
!= CALG_RSA_SIGN
)) {
2500 SetLastError(NTE_BAD_KEY
);
2503 if (!force
&& !(pCryptKey
->dwPermissions
& CRYPT_EXPORT
))
2505 SetLastError(NTE_BAD_KEY_STATE
);
2509 dwDataLen
= sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) +
2510 2 * pCryptKey
->dwKeyLen
+ 5 * ((pCryptKey
->dwKeyLen
+ 1) >> 1);
2512 if (*pdwDataLen
< dwDataLen
) {
2513 SetLastError(ERROR_MORE_DATA
);
2514 *pdwDataLen
= dwDataLen
;
2518 pBlobHeader
->bType
= PRIVATEKEYBLOB
;
2519 pBlobHeader
->bVersion
= CUR_BLOB_VERSION
;
2520 pBlobHeader
->reserved
= 0;
2521 pBlobHeader
->aiKeyAlg
= pCryptKey
->aiAlgid
;
2523 pRSAPubKey
->magic
= RSAENH_MAGIC_RSA2
;
2524 pRSAPubKey
->bitlen
= pCryptKey
->dwKeyLen
<< 3;
2526 export_private_key_impl((BYTE
*)(pRSAPubKey
+1), &pCryptKey
->context
,
2527 pCryptKey
->dwKeyLen
, &pRSAPubKey
->pubexp
);
2529 *pdwDataLen
= dwDataLen
;
2533 static BOOL
crypt_export_plaintext_key(CRYPTKEY
*pCryptKey
, BYTE
*pbData
,
2536 BLOBHEADER
*pBlobHeader
= (BLOBHEADER
*)pbData
;
2537 DWORD
*pKeyLen
= (DWORD
*)(pBlobHeader
+1);
2538 BYTE
*pbKey
= (BYTE
*)(pKeyLen
+1);
2541 dwDataLen
= sizeof(BLOBHEADER
) + sizeof(DWORD
) + pCryptKey
->dwKeyLen
;
2543 if (*pdwDataLen
< dwDataLen
) {
2544 SetLastError(ERROR_MORE_DATA
);
2545 *pdwDataLen
= dwDataLen
;
2549 pBlobHeader
->bType
= PLAINTEXTKEYBLOB
;
2550 pBlobHeader
->bVersion
= CUR_BLOB_VERSION
;
2551 pBlobHeader
->reserved
= 0;
2552 pBlobHeader
->aiKeyAlg
= pCryptKey
->aiAlgid
;
2554 *pKeyLen
= pCryptKey
->dwKeyLen
;
2555 memcpy(pbKey
, pCryptKey
->abKeyValue
, pCryptKey
->dwKeyLen
);
2557 *pdwDataLen
= dwDataLen
;
2560 /******************************************************************************
2561 * crypt_export_key [Internal]
2563 * Export a key into a binary large object (BLOB). Called by CPExportKey and
2564 * by store_key_pair.
2567 * pCryptKey [I] Key to be exported.
2568 * hPubKey [I] Key used to encrypt sensitive BLOB data.
2569 * dwBlobType [I] SIMPLEBLOB, PUBLICKEYBLOB or PRIVATEKEYBLOB.
2570 * dwFlags [I] Currently none defined.
2571 * force [I] If TRUE, the key is written no matter what the key's
2572 * permissions are. Otherwise the key's permissions are
2573 * checked before exporting.
2574 * pbData [O] Pointer to a buffer where the BLOB will be written to.
2575 * pdwDataLen [I/O] I: Size of buffer at pbData, O: Size of BLOB
2581 static BOOL
crypt_export_key(CRYPTKEY
*pCryptKey
, HCRYPTKEY hPubKey
,
2582 DWORD dwBlobType
, DWORD dwFlags
, BOOL force
,
2583 BYTE
*pbData
, DWORD
*pdwDataLen
)
2587 if (dwFlags
& CRYPT_SSL2_FALLBACK
) {
2588 if (pCryptKey
->aiAlgid
!= CALG_SSL2_MASTER
) {
2589 SetLastError(NTE_BAD_KEY
);
2594 switch ((BYTE
)dwBlobType
)
2597 if (!lookup_handle(&handle_table
, hPubKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pPubKey
)){
2598 SetLastError(NTE_BAD_PUBLIC_KEY
); /* FIXME: error_code? */
2601 return crypt_export_simple(pCryptKey
, pPubKey
, dwFlags
, pbData
,
2605 if (is_valid_handle(&handle_table
, hPubKey
, RSAENH_MAGIC_KEY
)) {
2606 SetLastError(NTE_BAD_KEY
); /* FIXME: error code? */
2610 return crypt_export_public_key(pCryptKey
, pbData
, pdwDataLen
);
2612 case PRIVATEKEYBLOB
:
2613 return crypt_export_private_key(pCryptKey
, force
, pbData
, pdwDataLen
);
2615 case PLAINTEXTKEYBLOB
:
2616 return crypt_export_plaintext_key(pCryptKey
, pbData
, pdwDataLen
);
2619 SetLastError(NTE_BAD_TYPE
); /* FIXME: error code? */
2624 /******************************************************************************
2625 * CPExportKey (RSAENH.@)
2627 * Export a key into a binary large object (BLOB).
2630 * hProv [I] Key container from which a key is to be exported.
2631 * hKey [I] Key to be exported.
2632 * hPubKey [I] Key used to encrypt sensitive BLOB data.
2633 * dwBlobType [I] SIMPLEBLOB, PUBLICKEYBLOB or PRIVATEKEYBLOB.
2634 * dwFlags [I] Currently none defined.
2635 * pbData [O] Pointer to a buffer where the BLOB will be written to.
2636 * pdwDataLen [I/O] I: Size of buffer at pbData, O: Size of BLOB
2642 BOOL WINAPI
RSAENH_CPExportKey(HCRYPTPROV hProv
, HCRYPTKEY hKey
, HCRYPTKEY hPubKey
,
2643 DWORD dwBlobType
, DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
)
2645 CRYPTKEY
*pCryptKey
;
2647 TRACE("(hProv=%08lx, hKey=%08lx, hPubKey=%08lx, dwBlobType=%08x, dwFlags=%08x, pbData=%p,"
2648 "pdwDataLen=%p)\n", hProv
, hKey
, hPubKey
, dwBlobType
, dwFlags
, pbData
, pdwDataLen
);
2650 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
2652 SetLastError(NTE_BAD_UID
);
2656 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
2658 SetLastError(NTE_BAD_KEY
);
2662 return crypt_export_key(pCryptKey
, hPubKey
, dwBlobType
, dwFlags
, FALSE
,
2663 pbData
, pdwDataLen
);
2666 /******************************************************************************
2667 * release_and_install_key [Internal]
2669 * Release an existing key, if present, and replaces it with a new one.
2672 * hProv [I] Key container into which the key is to be imported.
2673 * src [I] Key which will replace *dest
2674 * dest [I] Points to key to be released and replaced with src
2675 * fStoreKey [I] If TRUE, the newly installed key is stored to the registry.
2677 static void release_and_install_key(HCRYPTPROV hProv
, HCRYPTKEY src
,
2678 HCRYPTKEY
*dest
, DWORD fStoreKey
)
2680 RSAENH_CPDestroyKey(hProv
, *dest
);
2681 copy_handle(&handle_table
, src
, RSAENH_MAGIC_KEY
, dest
);
2684 KEYCONTAINER
*pKeyContainer
;
2686 if (lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
,
2687 (OBJECTHDR
**)&pKeyContainer
))
2689 store_key_container_keys(pKeyContainer
);
2690 store_key_container_permissions(pKeyContainer
);
2695 /******************************************************************************
2696 * import_private_key [Internal]
2698 * Import a BLOB'ed private key into a key container.
2701 * hProv [I] Key container into which the private key is to be imported.
2702 * pbData [I] Pointer to a buffer which holds the private key BLOB.
2703 * dwDataLen [I] Length of data in buffer at pbData.
2704 * dwFlags [I] One of:
2705 * CRYPT_EXPORTABLE: the imported key is marked exportable
2706 * fStoreKey [I] If TRUE, the imported key is stored to the registry.
2707 * phKey [O] Handle to the imported key.
2711 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2712 * it's a PRIVATEKEYBLOB.
2718 static BOOL
import_private_key(HCRYPTPROV hProv
, CONST BYTE
*pbData
, DWORD dwDataLen
,
2719 DWORD dwFlags
, BOOL fStoreKey
, HCRYPTKEY
*phKey
)
2721 KEYCONTAINER
*pKeyContainer
;
2722 CRYPTKEY
*pCryptKey
;
2723 CONST BLOBHEADER
*pBlobHeader
= (CONST BLOBHEADER
*)pbData
;
2724 CONST RSAPUBKEY
*pRSAPubKey
= (CONST RSAPUBKEY
*)(pBlobHeader
+1);
2727 if (dwFlags
& CRYPT_IPSEC_HMAC_KEY
)
2729 FIXME("unimplemented for CRYPT_IPSEC_HMAC_KEY\n");
2730 SetLastError(NTE_BAD_FLAGS
);
2733 if (!lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
,
2734 (OBJECTHDR
**)&pKeyContainer
))
2736 SetLastError(NTE_BAD_UID
);
2740 if ((dwDataLen
< sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
)))
2742 ERR("datalen %d not long enough for a BLOBHEADER + RSAPUBKEY\n",
2744 SetLastError(NTE_BAD_DATA
);
2747 if (pRSAPubKey
->magic
!= RSAENH_MAGIC_RSA2
)
2749 ERR("unexpected magic %08x\n", pRSAPubKey
->magic
);
2750 SetLastError(NTE_BAD_DATA
);
2753 if ((dwDataLen
< sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) +
2754 (pRSAPubKey
->bitlen
>> 3) + (5 * ((pRSAPubKey
->bitlen
+8)>>4))))
2756 DWORD expectedLen
= sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) +
2757 (pRSAPubKey
->bitlen
>> 3) + (5 * ((pRSAPubKey
->bitlen
+8)>>4));
2759 ERR("blob too short for pub key: expect %d, got %d\n",
2760 expectedLen
, dwDataLen
);
2761 SetLastError(NTE_BAD_DATA
);
2765 *phKey
= new_key(hProv
, pBlobHeader
->aiKeyAlg
, MAKELONG(0,pRSAPubKey
->bitlen
), &pCryptKey
);
2766 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
2767 setup_key(pCryptKey
);
2768 ret
= import_private_key_impl((CONST BYTE
*)(pRSAPubKey
+1), &pCryptKey
->context
,
2769 pRSAPubKey
->bitlen
/8, dwDataLen
, pRSAPubKey
->pubexp
);
2771 if (dwFlags
& CRYPT_EXPORTABLE
)
2772 pCryptKey
->dwPermissions
|= CRYPT_EXPORT
;
2773 switch (pBlobHeader
->aiKeyAlg
)
2777 TRACE("installing signing key\n");
2778 release_and_install_key(hProv
, *phKey
, &pKeyContainer
->hSignatureKeyPair
,
2781 case AT_KEYEXCHANGE
:
2783 TRACE("installing key exchange key\n");
2784 release_and_install_key(hProv
, *phKey
, &pKeyContainer
->hKeyExchangeKeyPair
,
2792 /******************************************************************************
2793 * import_public_key [Internal]
2795 * Import a BLOB'ed public key into a key container.
2798 * hProv [I] Key container into which the public key is to be imported.
2799 * pbData [I] Pointer to a buffer which holds the public key BLOB.
2800 * dwDataLen [I] Length of data in buffer at pbData.
2801 * dwFlags [I] One of:
2802 * CRYPT_EXPORTABLE: the imported key is marked exportable
2803 * fStoreKey [I] If TRUE, the imported key is stored to the registry.
2804 * phKey [O] Handle to the imported key.
2808 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2809 * it's a PUBLICKEYBLOB.
2815 static BOOL
import_public_key(HCRYPTPROV hProv
, CONST BYTE
*pbData
, DWORD dwDataLen
,
2816 DWORD dwFlags
, BOOL fStoreKey
, HCRYPTKEY
*phKey
)
2818 KEYCONTAINER
*pKeyContainer
;
2819 CRYPTKEY
*pCryptKey
;
2820 CONST BLOBHEADER
*pBlobHeader
= (CONST BLOBHEADER
*)pbData
;
2821 CONST RSAPUBKEY
*pRSAPubKey
= (CONST RSAPUBKEY
*)(pBlobHeader
+1);
2825 if (dwFlags
& CRYPT_IPSEC_HMAC_KEY
)
2827 FIXME("unimplemented for CRYPT_IPSEC_HMAC_KEY\n");
2828 SetLastError(NTE_BAD_FLAGS
);
2831 if (!lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
,
2832 (OBJECTHDR
**)&pKeyContainer
))
2834 SetLastError(NTE_BAD_UID
);
2838 if ((dwDataLen
< sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
)) ||
2839 (pRSAPubKey
->magic
!= RSAENH_MAGIC_RSA1
) ||
2840 (dwDataLen
< sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) + (pRSAPubKey
->bitlen
>> 3)))
2842 SetLastError(NTE_BAD_DATA
);
2846 /* Since this is a public key blob, only the public key is
2847 * available, so only signature verification is possible.
2849 algID
= pBlobHeader
->aiKeyAlg
;
2850 *phKey
= new_key(hProv
, algID
, MAKELONG(0,pRSAPubKey
->bitlen
), &pCryptKey
);
2851 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
2852 setup_key(pCryptKey
);
2853 ret
= import_public_key_impl((CONST BYTE
*)(pRSAPubKey
+1), &pCryptKey
->context
,
2854 pRSAPubKey
->bitlen
>> 3, pRSAPubKey
->pubexp
);
2856 if (dwFlags
& CRYPT_EXPORTABLE
)
2857 pCryptKey
->dwPermissions
|= CRYPT_EXPORT
;
2858 switch (pBlobHeader
->aiKeyAlg
)
2860 case AT_KEYEXCHANGE
:
2862 TRACE("installing public key\n");
2863 release_and_install_key(hProv
, *phKey
, &pKeyContainer
->hKeyExchangeKeyPair
,
2871 /******************************************************************************
2872 * import_symmetric_key [Internal]
2874 * Import a BLOB'ed symmetric key into a key container.
2877 * hProv [I] Key container into which the symmetric key is to be imported.
2878 * pbData [I] Pointer to a buffer which holds the symmetric key BLOB.
2879 * dwDataLen [I] Length of data in buffer at pbData.
2880 * hPubKey [I] Key used to decrypt sensitive BLOB data.
2881 * dwFlags [I] One of:
2882 * CRYPT_EXPORTABLE: the imported key is marked exportable
2883 * phKey [O] Handle to the imported key.
2887 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2888 * it's a SIMPLEBLOB.
2894 static BOOL
import_symmetric_key(HCRYPTPROV hProv
, CONST BYTE
*pbData
,
2895 DWORD dwDataLen
, HCRYPTKEY hPubKey
,
2896 DWORD dwFlags
, HCRYPTKEY
*phKey
)
2898 CRYPTKEY
*pCryptKey
, *pPubKey
;
2899 CONST BLOBHEADER
*pBlobHeader
= (CONST BLOBHEADER
*)pbData
;
2900 CONST ALG_ID
*pAlgid
= (CONST ALG_ID
*)(pBlobHeader
+1);
2901 CONST BYTE
*pbKeyStream
= (CONST BYTE
*)(pAlgid
+ 1);
2905 if (dwFlags
& CRYPT_IPSEC_HMAC_KEY
)
2907 FIXME("unimplemented for CRYPT_IPSEC_HMAC_KEY\n");
2908 SetLastError(NTE_BAD_FLAGS
);
2911 if (!lookup_handle(&handle_table
, hPubKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pPubKey
) ||
2912 pPubKey
->aiAlgid
!= CALG_RSA_KEYX
)
2914 SetLastError(NTE_BAD_PUBLIC_KEY
); /* FIXME: error code? */
2918 if (dwDataLen
< sizeof(BLOBHEADER
)+sizeof(ALG_ID
)+pPubKey
->dwBlockLen
)
2920 SetLastError(NTE_BAD_DATA
); /* FIXME: error code */
2924 pbDecrypted
= HeapAlloc(GetProcessHeap(), 0, pPubKey
->dwBlockLen
);
2925 if (!pbDecrypted
) return FALSE
;
2926 encrypt_block_impl(pPubKey
->aiAlgid
, PK_PRIVATE
, &pPubKey
->context
, pbKeyStream
, pbDecrypted
,
2929 dwKeyLen
= RSAENH_MAX_KEY_SIZE
;
2930 if (!unpad_data(pbDecrypted
, pPubKey
->dwBlockLen
, pbDecrypted
, &dwKeyLen
, dwFlags
)) {
2931 HeapFree(GetProcessHeap(), 0, pbDecrypted
);
2935 *phKey
= new_key(hProv
, pBlobHeader
->aiKeyAlg
, dwKeyLen
<<19, &pCryptKey
);
2936 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
2938 HeapFree(GetProcessHeap(), 0, pbDecrypted
);
2941 memcpy(pCryptKey
->abKeyValue
, pbDecrypted
, dwKeyLen
);
2942 HeapFree(GetProcessHeap(), 0, pbDecrypted
);
2943 setup_key(pCryptKey
);
2944 if (dwFlags
& CRYPT_EXPORTABLE
)
2945 pCryptKey
->dwPermissions
|= CRYPT_EXPORT
;
2949 /******************************************************************************
2950 * import_plaintext_key [Internal]
2952 * Import a plaintext key into a key container.
2955 * hProv [I] Key container into which the symmetric key is to be imported.
2956 * pbData [I] Pointer to a buffer which holds the plaintext key BLOB.
2957 * dwDataLen [I] Length of data in buffer at pbData.
2958 * dwFlags [I] One of:
2959 * CRYPT_EXPORTABLE: the imported key is marked exportable
2960 * phKey [O] Handle to the imported key.
2964 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2965 * it's a PLAINTEXTKEYBLOB.
2971 static BOOL
import_plaintext_key(HCRYPTPROV hProv
, CONST BYTE
*pbData
,
2972 DWORD dwDataLen
, DWORD dwFlags
,
2975 CRYPTKEY
*pCryptKey
;
2976 CONST BLOBHEADER
*pBlobHeader
= (CONST BLOBHEADER
*)pbData
;
2977 CONST DWORD
*pKeyLen
= (CONST DWORD
*)(pBlobHeader
+ 1);
2978 CONST BYTE
*pbKeyStream
= (CONST BYTE
*)(pKeyLen
+ 1);
2980 if (dwDataLen
< sizeof(BLOBHEADER
)+sizeof(DWORD
)+*pKeyLen
)
2982 SetLastError(NTE_BAD_DATA
); /* FIXME: error code */
2986 if (dwFlags
& CRYPT_IPSEC_HMAC_KEY
)
2988 *phKey
= new_key(hProv
, CALG_HMAC
, 0, &pCryptKey
);
2989 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
2991 if (*pKeyLen
<= RSAENH_MIN(sizeof(pCryptKey
->abKeyValue
), RSAENH_HMAC_BLOCK_LEN
))
2993 memcpy(pCryptKey
->abKeyValue
, pbKeyStream
, *pKeyLen
);
2994 pCryptKey
->dwKeyLen
= *pKeyLen
;
2998 CRYPT_DATA_BLOB blobHmacKey
= { *pKeyLen
, (BYTE
*)pbKeyStream
};
3000 /* In order to initialize an HMAC key, the key material is hashed,
3001 * and the output of the hash function is used as the key material.
3002 * Unfortunately, the way the Crypto API is designed, we don't know
3003 * the hash algorithm yet, so we have to copy the entire key
3006 if (!copy_data_blob(&pCryptKey
->blobHmacKey
, &blobHmacKey
))
3008 release_handle(&handle_table
, *phKey
, RSAENH_MAGIC_KEY
);
3009 *phKey
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
3013 setup_key(pCryptKey
);
3014 if (dwFlags
& CRYPT_EXPORTABLE
)
3015 pCryptKey
->dwPermissions
|= CRYPT_EXPORT
;
3019 *phKey
= new_key(hProv
, pBlobHeader
->aiKeyAlg
, *pKeyLen
<<19, &pCryptKey
);
3020 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
3022 memcpy(pCryptKey
->abKeyValue
, pbKeyStream
, *pKeyLen
);
3023 setup_key(pCryptKey
);
3024 if (dwFlags
& CRYPT_EXPORTABLE
)
3025 pCryptKey
->dwPermissions
|= CRYPT_EXPORT
;
3030 /******************************************************************************
3031 * import_key [Internal]
3033 * Import a BLOB'ed key into a key container, optionally storing the key's
3034 * value to the registry.
3037 * hProv [I] Key container into which the key is to be imported.
3038 * pbData [I] Pointer to a buffer which holds the BLOB.
3039 * dwDataLen [I] Length of data in buffer at pbData.
3040 * hPubKey [I] Key used to decrypt sensitive BLOB data.
3041 * dwFlags [I] One of:
3042 * CRYPT_EXPORTABLE: the imported key is marked exportable
3043 * fStoreKey [I] If TRUE, the imported key is stored to the registry.
3044 * phKey [O] Handle to the imported key.
3050 static BOOL
import_key(HCRYPTPROV hProv
, CONST BYTE
*pbData
, DWORD dwDataLen
,
3051 HCRYPTKEY hPubKey
, DWORD dwFlags
, BOOL fStoreKey
,
3054 KEYCONTAINER
*pKeyContainer
;
3055 CONST BLOBHEADER
*pBlobHeader
= (CONST BLOBHEADER
*)pbData
;
3057 if (!lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
,
3058 (OBJECTHDR
**)&pKeyContainer
))
3060 SetLastError(NTE_BAD_UID
);
3064 if (dwDataLen
< sizeof(BLOBHEADER
) ||
3065 pBlobHeader
->bVersion
!= CUR_BLOB_VERSION
||
3066 pBlobHeader
->reserved
!= 0)
3068 TRACE("bVersion = %d, reserved = %d\n", pBlobHeader
->bVersion
,
3069 pBlobHeader
->reserved
);
3070 SetLastError(NTE_BAD_DATA
);
3074 /* If this is a verify-only context, the key is not persisted regardless of
3075 * fStoreKey's original value.
3077 fStoreKey
= fStoreKey
&& !(dwFlags
& CRYPT_VERIFYCONTEXT
);
3078 TRACE("blob type: %x\n", pBlobHeader
->bType
);
3079 switch (pBlobHeader
->bType
)
3081 case PRIVATEKEYBLOB
:
3082 return import_private_key(hProv
, pbData
, dwDataLen
, dwFlags
,
3086 return import_public_key(hProv
, pbData
, dwDataLen
, dwFlags
,
3090 return import_symmetric_key(hProv
, pbData
, dwDataLen
, hPubKey
,
3093 case PLAINTEXTKEYBLOB
:
3094 return import_plaintext_key(hProv
, pbData
, dwDataLen
, dwFlags
,
3098 SetLastError(NTE_BAD_TYPE
); /* FIXME: error code? */
3103 /******************************************************************************
3104 * CPImportKey (RSAENH.@)
3106 * Import a BLOB'ed key into a key container.
3109 * hProv [I] Key container into which the key is to be imported.
3110 * pbData [I] Pointer to a buffer which holds the BLOB.
3111 * dwDataLen [I] Length of data in buffer at pbData.
3112 * hPubKey [I] Key used to decrypt sensitive BLOB data.
3113 * dwFlags [I] One of:
3114 * CRYPT_EXPORTABLE: the imported key is marked exportable
3115 * phKey [O] Handle to the imported key.
3121 BOOL WINAPI
RSAENH_CPImportKey(HCRYPTPROV hProv
, CONST BYTE
*pbData
, DWORD dwDataLen
,
3122 HCRYPTKEY hPubKey
, DWORD dwFlags
, HCRYPTKEY
*phKey
)
3124 TRACE("(hProv=%08lx, pbData=%p, dwDataLen=%d, hPubKey=%08lx, dwFlags=%08x, phKey=%p)\n",
3125 hProv
, pbData
, dwDataLen
, hPubKey
, dwFlags
, phKey
);
3127 return import_key(hProv
, pbData
, dwDataLen
, hPubKey
, dwFlags
, TRUE
, phKey
);
3130 /******************************************************************************
3131 * CPGenKey (RSAENH.@)
3133 * Generate a key in the key container
3136 * hProv [I] Key container for which a key is to be generated.
3137 * Algid [I] Crypto algorithm identifier for the key to be generated.
3138 * dwFlags [I] Upper 16 bits: Binary length of key. Lower 16 bits: Flags. See Notes
3139 * phKey [O] Handle to the generated key.
3146 * Flags currently not considered.
3149 * Private key-exchange- and signature-keys can be generated with Algid AT_KEYEXCHANGE
3150 * and AT_SIGNATURE values.
3152 BOOL WINAPI
RSAENH_CPGenKey(HCRYPTPROV hProv
, ALG_ID Algid
, DWORD dwFlags
, HCRYPTKEY
*phKey
)
3154 KEYCONTAINER
*pKeyContainer
;
3155 CRYPTKEY
*pCryptKey
;
3157 TRACE("(hProv=%08lx, aiAlgid=%d, dwFlags=%08x, phKey=%p)\n", hProv
, Algid
, dwFlags
, phKey
);
3159 if (!lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
,
3160 (OBJECTHDR
**)&pKeyContainer
))
3162 /* MSDN: hProv not containing valid context handle */
3163 SetLastError(NTE_BAD_UID
);
3171 *phKey
= new_key(hProv
, CALG_RSA_SIGN
, dwFlags
, &pCryptKey
);
3173 new_key_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pCryptKey
->dwKeyLen
);
3174 setup_key(pCryptKey
);
3175 release_and_install_key(hProv
, *phKey
,
3176 &pKeyContainer
->hSignatureKeyPair
,
3181 case AT_KEYEXCHANGE
:
3183 *phKey
= new_key(hProv
, CALG_RSA_KEYX
, dwFlags
, &pCryptKey
);
3185 new_key_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pCryptKey
->dwKeyLen
);
3186 setup_key(pCryptKey
);
3187 release_and_install_key(hProv
, *phKey
,
3188 &pKeyContainer
->hKeyExchangeKeyPair
,
3202 case CALG_PCT1_MASTER
:
3203 case CALG_SSL2_MASTER
:
3204 case CALG_SSL3_MASTER
:
3205 case CALG_TLS1_MASTER
:
3206 *phKey
= new_key(hProv
, Algid
, dwFlags
, &pCryptKey
);
3208 gen_rand_impl(pCryptKey
->abKeyValue
, RSAENH_MAX_KEY_SIZE
);
3210 case CALG_SSL3_MASTER
:
3211 pCryptKey
->abKeyValue
[0] = RSAENH_SSL3_VERSION_MAJOR
;
3212 pCryptKey
->abKeyValue
[1] = RSAENH_SSL3_VERSION_MINOR
;
3215 case CALG_TLS1_MASTER
:
3216 pCryptKey
->abKeyValue
[0] = RSAENH_TLS1_VERSION_MAJOR
;
3217 pCryptKey
->abKeyValue
[1] = RSAENH_TLS1_VERSION_MINOR
;
3220 setup_key(pCryptKey
);
3225 /* MSDN: Algorithm not supported specified by Algid */
3226 SetLastError(NTE_BAD_ALGID
);
3230 return *phKey
!= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
3233 /******************************************************************************
3234 * CPGenRandom (RSAENH.@)
3236 * Generate a random byte stream.
3239 * hProv [I] Key container that is used to generate random bytes.
3240 * dwLen [I] Specifies the number of requested random data bytes.
3241 * pbBuffer [O] Random bytes will be stored here.
3247 BOOL WINAPI
RSAENH_CPGenRandom(HCRYPTPROV hProv
, DWORD dwLen
, BYTE
*pbBuffer
)
3249 TRACE("(hProv=%08lx, dwLen=%d, pbBuffer=%p)\n", hProv
, dwLen
, pbBuffer
);
3251 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
3253 /* MSDN: hProv not containing valid context handle */
3254 SetLastError(NTE_BAD_UID
);
3258 return gen_rand_impl(pbBuffer
, dwLen
);
3261 /******************************************************************************
3262 * CPGetHashParam (RSAENH.@)
3264 * Query parameters of an hash object.
3267 * hProv [I] The kea container, which the hash belongs to.
3268 * hHash [I] The hash object that is to be queried.
3269 * dwParam [I] Specifies the parameter that is to be queried.
3270 * pbData [I] Pointer to the buffer where the parameter value will be stored.
3271 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
3272 * dwFlags [I] None currently defined.
3279 * Valid dwParams are: HP_ALGID, HP_HASHSIZE, HP_HASHVALUE. The hash will be
3280 * finalized if HP_HASHVALUE is queried.
3282 BOOL WINAPI
RSAENH_CPGetHashParam(HCRYPTPROV hProv
, HCRYPTHASH hHash
, DWORD dwParam
, BYTE
*pbData
,
3283 DWORD
*pdwDataLen
, DWORD dwFlags
)
3285 CRYPTHASH
*pCryptHash
;
3287 TRACE("(hProv=%08lx, hHash=%08lx, dwParam=%08x, pbData=%p, pdwDataLen=%p, dwFlags=%08x)\n",
3288 hProv
, hHash
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
3290 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
3292 SetLastError(NTE_BAD_UID
);
3298 SetLastError(NTE_BAD_FLAGS
);
3302 if (!lookup_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
,
3303 (OBJECTHDR
**)&pCryptHash
))
3305 SetLastError(NTE_BAD_HASH
);
3311 SetLastError(ERROR_INVALID_PARAMETER
);
3318 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&pCryptHash
->aiAlgid
,
3322 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&pCryptHash
->dwHashSize
,
3326 if (pCryptHash
->aiAlgid
== CALG_TLS1PRF
) {
3327 return tls1_prf(hProv
, pCryptHash
->hKey
, &pCryptHash
->tpPRFParams
.blobLabel
,
3328 &pCryptHash
->tpPRFParams
.blobSeed
, pbData
, *pdwDataLen
);
3331 if ( pbData
== NULL
) {
3332 *pdwDataLen
= pCryptHash
->dwHashSize
;
3336 if (pbData
&& (pCryptHash
->dwState
!= RSAENH_HASHSTATE_FINISHED
))
3338 finalize_hash(pCryptHash
);
3339 pCryptHash
->dwState
= RSAENH_HASHSTATE_FINISHED
;
3342 return copy_param(pbData
, pdwDataLen
, pCryptHash
->abHashValue
,
3343 pCryptHash
->dwHashSize
);
3346 SetLastError(NTE_BAD_TYPE
);
3351 /******************************************************************************
3352 * CPSetKeyParam (RSAENH.@)
3354 * Set a parameter of a key object
3357 * hProv [I] The key container to which the key belongs.
3358 * hKey [I] The key for which a parameter is to be set.
3359 * dwParam [I] Parameter type. See Notes.
3360 * pbData [I] Pointer to the parameter value.
3361 * dwFlags [I] Currently none defined.
3368 * Defined dwParam types are:
3369 * - KP_MODE: Values MODE_CBC, MODE_ECB, MODE_CFB.
3370 * - KP_MODE_BITS: Shift width for cipher feedback mode. (Currently ignored by MS CSP's)
3371 * - KP_PERMISSIONS: Or'ed combination of CRYPT_ENCRYPT, CRYPT_DECRYPT,
3372 * CRYPT_EXPORT, CRYPT_READ, CRYPT_WRITE, CRYPT_MAC
3373 * - KP_IV: Initialization vector
3375 BOOL WINAPI
RSAENH_CPSetKeyParam(HCRYPTPROV hProv
, HCRYPTKEY hKey
, DWORD dwParam
, BYTE
*pbData
,
3378 CRYPTKEY
*pCryptKey
;
3380 TRACE("(hProv=%08lx, hKey=%08lx, dwParam=%08x, pbData=%p, dwFlags=%08x)\n", hProv
, hKey
,
3381 dwParam
, pbData
, dwFlags
);
3383 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
3385 SetLastError(NTE_BAD_UID
);
3390 SetLastError(NTE_BAD_FLAGS
);
3394 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
3396 SetLastError(NTE_BAD_KEY
);
3402 /* The MS providers only support PKCS5_PADDING */
3403 if (*(DWORD
*)pbData
!= PKCS5_PADDING
) {
3404 SetLastError(NTE_BAD_DATA
);
3410 pCryptKey
->dwMode
= *(DWORD
*)pbData
;
3414 pCryptKey
->dwModeBits
= *(DWORD
*)pbData
;
3417 case KP_PERMISSIONS
:
3419 DWORD perms
= *(DWORD
*)pbData
;
3421 if ((perms
& CRYPT_EXPORT
) &&
3422 !(pCryptKey
->dwPermissions
& CRYPT_EXPORT
))
3424 SetLastError(NTE_BAD_DATA
);
3427 else if (!(perms
& CRYPT_EXPORT
) &&
3428 (pCryptKey
->dwPermissions
& CRYPT_EXPORT
))
3430 /* Clearing the export permission appears to be ignored,
3433 perms
|= CRYPT_EXPORT
;
3435 pCryptKey
->dwPermissions
= perms
;
3440 memcpy(pCryptKey
->abInitVector
, pbData
, pCryptKey
->dwBlockLen
);
3441 setup_key(pCryptKey
);
3445 switch (pCryptKey
->aiAlgid
) {
3450 SetLastError(ERROR_INVALID_PARAMETER
);
3453 /* MSDN: the base provider always sets eleven bytes of
3456 memcpy(pCryptKey
->abKeyValue
+ pCryptKey
->dwKeyLen
,
3458 pCryptKey
->dwSaltLen
= 11;
3459 setup_key(pCryptKey
);
3460 /* Strange but true: salt length reset to 0 after setting
3463 pCryptKey
->dwSaltLen
= 0;
3466 SetLastError(NTE_BAD_KEY
);
3473 CRYPT_INTEGER_BLOB
*blob
= (CRYPT_INTEGER_BLOB
*)pbData
;
3475 /* salt length can't be greater than 184 bits = 24 bytes */
3476 if (blob
->cbData
> 24)
3478 SetLastError(NTE_BAD_DATA
);
3481 memcpy(pCryptKey
->abKeyValue
+ pCryptKey
->dwKeyLen
, blob
->pbData
,
3483 pCryptKey
->dwSaltLen
= blob
->cbData
;
3484 setup_key(pCryptKey
);
3488 case KP_EFFECTIVE_KEYLEN
:
3489 switch (pCryptKey
->aiAlgid
) {
3493 SetLastError(ERROR_INVALID_PARAMETER
);
3496 else if (!*(DWORD
*)pbData
|| *(DWORD
*)pbData
> 1024)
3498 SetLastError(NTE_BAD_DATA
);
3503 pCryptKey
->dwEffectiveKeyLen
= *(DWORD
*)pbData
;
3504 setup_key(pCryptKey
);
3508 SetLastError(NTE_BAD_TYPE
);
3513 case KP_SCHANNEL_ALG
:
3514 switch (((PSCHANNEL_ALG
)pbData
)->dwUse
) {
3515 case SCHANNEL_ENC_KEY
:
3516 memcpy(&pCryptKey
->siSChannelInfo
.saEncAlg
, pbData
, sizeof(SCHANNEL_ALG
));
3519 case SCHANNEL_MAC_KEY
:
3520 memcpy(&pCryptKey
->siSChannelInfo
.saMACAlg
, pbData
, sizeof(SCHANNEL_ALG
));
3524 SetLastError(NTE_FAIL
); /* FIXME: error code */
3529 case KP_CLIENT_RANDOM
:
3530 return copy_data_blob(&pCryptKey
->siSChannelInfo
.blobClientRandom
, (PCRYPT_DATA_BLOB
)pbData
);
3532 case KP_SERVER_RANDOM
:
3533 return copy_data_blob(&pCryptKey
->siSChannelInfo
.blobServerRandom
, (PCRYPT_DATA_BLOB
)pbData
);
3536 SetLastError(NTE_BAD_TYPE
);
3541 /******************************************************************************
3542 * CPGetKeyParam (RSAENH.@)
3544 * Query a key parameter.
3547 * hProv [I] The key container, which the key belongs to.
3548 * hHash [I] The key object that is to be queried.
3549 * dwParam [I] Specifies the parameter that is to be queried.
3550 * pbData [I] Pointer to the buffer where the parameter value will be stored.
3551 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
3552 * dwFlags [I] None currently defined.
3559 * Defined dwParam types are:
3560 * - KP_MODE: Values MODE_CBC, MODE_ECB, MODE_CFB.
3561 * - KP_MODE_BITS: Shift width for cipher feedback mode.
3562 * (Currently ignored by MS CSP's - always eight)
3563 * - KP_PERMISSIONS: Or'ed combination of CRYPT_ENCRYPT, CRYPT_DECRYPT,
3564 * CRYPT_EXPORT, CRYPT_READ, CRYPT_WRITE, CRYPT_MAC
3565 * - KP_IV: Initialization vector.
3566 * - KP_KEYLEN: Bitwidth of the key.
3567 * - KP_BLOCKLEN: Size of a block cipher block.
3568 * - KP_SALT: Salt value.
3570 BOOL WINAPI
RSAENH_CPGetKeyParam(HCRYPTPROV hProv
, HCRYPTKEY hKey
, DWORD dwParam
, BYTE
*pbData
,
3571 DWORD
*pdwDataLen
, DWORD dwFlags
)
3573 CRYPTKEY
*pCryptKey
;
3576 TRACE("(hProv=%08lx, hKey=%08lx, dwParam=%08x, pbData=%p, pdwDataLen=%p dwFlags=%08x)\n",
3577 hProv
, hKey
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
3579 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
3581 SetLastError(NTE_BAD_UID
);
3586 SetLastError(NTE_BAD_FLAGS
);
3590 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
3592 SetLastError(NTE_BAD_KEY
);
3599 return copy_param(pbData
, pdwDataLen
, pCryptKey
->abInitVector
,
3600 pCryptKey
->dwBlockLen
);
3603 switch (pCryptKey
->aiAlgid
) {
3606 return copy_param(pbData
, pdwDataLen
,
3607 &pCryptKey
->abKeyValue
[pCryptKey
->dwKeyLen
],
3608 pCryptKey
->dwSaltLen
);
3610 SetLastError(NTE_BAD_KEY
);
3615 dwValue
= PKCS5_PADDING
;
3616 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwValue
, sizeof(DWORD
));
3619 dwValue
= pCryptKey
->dwKeyLen
<< 3;
3620 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwValue
, sizeof(DWORD
));
3622 case KP_EFFECTIVE_KEYLEN
:
3623 if (pCryptKey
->dwEffectiveKeyLen
)
3624 dwValue
= pCryptKey
->dwEffectiveKeyLen
;
3626 dwValue
= pCryptKey
->dwKeyLen
<< 3;
3627 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwValue
, sizeof(DWORD
));
3630 dwValue
= pCryptKey
->dwBlockLen
<< 3;
3631 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwValue
, sizeof(DWORD
));
3634 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&pCryptKey
->dwMode
, sizeof(DWORD
));
3637 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&pCryptKey
->dwModeBits
,
3640 case KP_PERMISSIONS
:
3641 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&pCryptKey
->dwPermissions
,
3645 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&pCryptKey
->aiAlgid
, sizeof(DWORD
));
3648 SetLastError(NTE_BAD_TYPE
);
3653 /******************************************************************************
3654 * CPGetProvParam (RSAENH.@)
3656 * Query a CSP parameter.
3659 * hProv [I] The key container that is to be queried.
3660 * dwParam [I] Specifies the parameter that is to be queried.
3661 * pbData [I] Pointer to the buffer where the parameter value will be stored.
3662 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
3663 * dwFlags [I] CRYPT_FIRST: Start enumeration (for PP_ENUMALGS{_EX}).
3669 * Defined dwParam types:
3670 * - PP_CONTAINER: Name of the key container.
3671 * - PP_NAME: Name of the cryptographic service provider.
3672 * - PP_SIG_KEYSIZE_INC: RSA signature keywidth granularity in bits.
3673 * - PP_KEYX_KEYSIZE_INC: RSA key-exchange keywidth granularity in bits.
3674 * - PP_ENUMALGS{_EX}: Query provider capabilities.
3676 BOOL WINAPI
RSAENH_CPGetProvParam(HCRYPTPROV hProv
, DWORD dwParam
, BYTE
*pbData
,
3677 DWORD
*pdwDataLen
, DWORD dwFlags
)
3679 KEYCONTAINER
*pKeyContainer
;
3680 PROV_ENUMALGS provEnumalgs
;
3684 /* This is for dwParam PP_CRYPT_COUNT_KEY_USE.
3685 * IE6 SP1 asks for it in the 'About' dialog.
3686 * Returning this BLOB seems to satisfy IE. The marked 0x00 seem
3687 * to be 'don't care's. If you know anything more specific about
3688 * this provider parameter, please report to wine-devel@winehq.org */
3689 static CONST BYTE abWTF
[96] = {
3690 0xb0, 0x25, 0x63, 0x86, 0x9c, 0xab, 0xb6, 0x37,
3691 0xe8, 0x82, /**/0x00,/**/ 0x72, 0x06, 0xb2, /**/0x00,/**/ 0x3b,
3692 0x60, 0x35, /**/0x00,/**/ 0x3b, 0x88, 0xce, /**/0x00,/**/ 0x82,
3693 0xbc, 0x7a, /**/0x00,/**/ 0xb7, 0x4f, 0x7e, /**/0x00,/**/ 0xde,
3694 0x92, 0xf1, /**/0x00,/**/ 0x83, 0xea, 0x5e, /**/0x00,/**/ 0xc8,
3695 0x12, 0x1e, 0xd4, 0x06, 0xf7, 0x66, /**/0x00,/**/ 0x01,
3696 0x29, 0xa4, /**/0x00,/**/ 0xf8, 0x24, 0x0c, /**/0x00,/**/ 0x33,
3697 0x06, 0x80, /**/0x00,/**/ 0x02, 0x46, 0x0b, /**/0x00,/**/ 0x6d,
3698 0x5b, 0xca, /**/0x00,/**/ 0x9a, 0x10, 0xf0, /**/0x00,/**/ 0x05,
3699 0x19, 0xd0, /**/0x00,/**/ 0x2c, 0xf6, 0x27, /**/0x00,/**/ 0xaa,
3700 0x7c, 0x6f, /**/0x00,/**/ 0xb9, 0xd8, 0x72, /**/0x00,/**/ 0x03,
3701 0xf3, 0x81, /**/0x00,/**/ 0xfa, 0xe8, 0x26, /**/0x00,/**/ 0xca
3704 TRACE("(hProv=%08lx, dwParam=%08x, pbData=%p, pdwDataLen=%p, dwFlags=%08x)\n",
3705 hProv
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
3708 SetLastError(ERROR_INVALID_PARAMETER
);
3712 if (!lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
,
3713 (OBJECTHDR
**)&pKeyContainer
))
3715 /* MSDN: hProv not containing valid context handle */
3716 SetLastError(NTE_BAD_UID
);
3723 case PP_UNIQUE_CONTAINER
:/* MSDN says we can return the same value as PP_CONTAINER */
3724 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)pKeyContainer
->szName
,
3725 strlen(pKeyContainer
->szName
)+1);
3728 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)pKeyContainer
->szProvName
,
3729 strlen(pKeyContainer
->szProvName
)+1);
3732 dwTemp
= PROV_RSA_FULL
;
3733 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwTemp
, sizeof(dwTemp
));
3736 dwTemp
= AT_SIGNATURE
| AT_KEYEXCHANGE
;
3737 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwTemp
, sizeof(dwTemp
));
3739 case PP_KEYSET_TYPE
:
3740 dwTemp
= pKeyContainer
->dwFlags
& CRYPT_MACHINE_KEYSET
;
3741 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwTemp
, sizeof(dwTemp
));
3744 dwTemp
= CRYPT_SEC_DESCR
;
3745 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwTemp
, sizeof(dwTemp
));
3747 case PP_SIG_KEYSIZE_INC
:
3748 case PP_KEYX_KEYSIZE_INC
:
3750 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwTemp
, sizeof(dwTemp
));
3753 dwTemp
= CRYPT_IMPL_SOFTWARE
;
3754 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwTemp
, sizeof(dwTemp
));
3757 dwTemp
= 0x00000200;
3758 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwTemp
, sizeof(dwTemp
));
3760 case PP_ENUMCONTAINERS
:
3761 if ((dwFlags
& CRYPT_FIRST
) == CRYPT_FIRST
) pKeyContainer
->dwEnumContainersCtr
= 0;
3764 *pdwDataLen
= (DWORD
)MAX_PATH
+ 1;
3768 if (!open_container_key("", dwFlags
, &hKey
))
3770 SetLastError(ERROR_NO_MORE_ITEMS
);
3774 dwTemp
= *pdwDataLen
;
3775 switch (RegEnumKeyExA(hKey
, pKeyContainer
->dwEnumContainersCtr
, (LPSTR
)pbData
, &dwTemp
,
3776 NULL
, NULL
, NULL
, NULL
))
3778 case ERROR_MORE_DATA
:
3779 *pdwDataLen
= (DWORD
)MAX_PATH
+ 1;
3782 pKeyContainer
->dwEnumContainersCtr
++;
3786 case ERROR_NO_MORE_ITEMS
:
3788 SetLastError(ERROR_NO_MORE_ITEMS
);
3794 case PP_ENUMALGS_EX
:
3795 if (((pKeyContainer
->dwEnumAlgsCtr
>= RSAENH_MAX_ENUMALGS
-1) ||
3796 (!aProvEnumAlgsEx
[pKeyContainer
->dwPersonality
]
3797 [pKeyContainer
->dwEnumAlgsCtr
+1].aiAlgid
)) &&
3798 ((dwFlags
& CRYPT_FIRST
) != CRYPT_FIRST
))
3800 SetLastError(ERROR_NO_MORE_ITEMS
);
3804 if (dwParam
== PP_ENUMALGS
) {
3805 if (pbData
&& (*pdwDataLen
>= sizeof(PROV_ENUMALGS
)))
3806 pKeyContainer
->dwEnumAlgsCtr
= ((dwFlags
& CRYPT_FIRST
) == CRYPT_FIRST
) ?
3807 0 : pKeyContainer
->dwEnumAlgsCtr
+1;
3809 provEnumalgs
.aiAlgid
= aProvEnumAlgsEx
3810 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
].aiAlgid
;
3811 provEnumalgs
.dwBitLen
= aProvEnumAlgsEx
3812 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
].dwDefaultLen
;
3813 provEnumalgs
.dwNameLen
= aProvEnumAlgsEx
3814 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
].dwNameLen
;
3815 memcpy(provEnumalgs
.szName
, aProvEnumAlgsEx
3816 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
].szName
,
3819 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&provEnumalgs
,
3820 sizeof(PROV_ENUMALGS
));
3822 if (pbData
&& (*pdwDataLen
>= sizeof(PROV_ENUMALGS_EX
)))
3823 pKeyContainer
->dwEnumAlgsCtr
= ((dwFlags
& CRYPT_FIRST
) == CRYPT_FIRST
) ?
3824 0 : pKeyContainer
->dwEnumAlgsCtr
+1;
3826 return copy_param(pbData
, pdwDataLen
,
3827 (CONST BYTE
*)&aProvEnumAlgsEx
3828 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
],
3829 sizeof(PROV_ENUMALGS_EX
));
3832 case PP_CRYPT_COUNT_KEY_USE
: /* Asked for by IE About dialog */
3833 return copy_param(pbData
, pdwDataLen
, abWTF
, sizeof(abWTF
));
3836 /* MSDN: Unknown parameter number in dwParam */
3837 SetLastError(NTE_BAD_TYPE
);
3842 /******************************************************************************
3843 * CPDeriveKey (RSAENH.@)
3845 * Derives a key from a hash value.
3848 * hProv [I] Key container for which a key is to be generated.
3849 * Algid [I] Crypto algorithm identifier for the key to be generated.
3850 * hBaseData [I] Hash from whose value the key will be derived.
3851 * dwFlags [I] See Notes.
3852 * phKey [O] The generated key.
3860 * - CRYPT_EXPORTABLE: Key can be exported.
3861 * - CRYPT_NO_SALT: No salt is used for 40 bit keys.
3862 * - CRYPT_CREATE_SALT: Use remaining bits as salt value.
3864 BOOL WINAPI
RSAENH_CPDeriveKey(HCRYPTPROV hProv
, ALG_ID Algid
, HCRYPTHASH hBaseData
,
3865 DWORD dwFlags
, HCRYPTKEY
*phKey
)
3867 CRYPTKEY
*pCryptKey
, *pMasterKey
;
3868 CRYPTHASH
*pCryptHash
;
3869 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
*2];
3872 TRACE("(hProv=%08lx, Algid=%d, hBaseData=%08lx, dwFlags=%08x phKey=%p)\n", hProv
, Algid
,
3873 hBaseData
, dwFlags
, phKey
);
3875 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
3877 SetLastError(NTE_BAD_UID
);
3881 if (!lookup_handle(&handle_table
, hBaseData
, RSAENH_MAGIC_HASH
,
3882 (OBJECTHDR
**)&pCryptHash
))
3884 SetLastError(NTE_BAD_HASH
);
3890 SetLastError(ERROR_INVALID_PARAMETER
);
3894 switch (GET_ALG_CLASS(Algid
))
3896 case ALG_CLASS_DATA_ENCRYPT
:
3897 *phKey
= new_key(hProv
, Algid
, dwFlags
, &pCryptKey
);
3898 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
3901 * We derive the key material from the hash.
3902 * If the hash value is not large enough for the claimed key, we have to construct
3903 * a larger binary value based on the hash. This is documented in MSDN: CryptDeriveKey.
3905 dwLen
= RSAENH_MAX_HASH_SIZE
;
3906 RSAENH_CPGetHashParam(pCryptHash
->hProv
, hBaseData
, HP_HASHVAL
, abHashValue
, &dwLen
, 0);
3908 if (dwLen
< pCryptKey
->dwKeyLen
) {
3909 BYTE pad1
[RSAENH_HMAC_DEF_PAD_LEN
], pad2
[RSAENH_HMAC_DEF_PAD_LEN
];
3910 BYTE old_hashval
[RSAENH_MAX_HASH_SIZE
];
3913 memcpy(old_hashval
, pCryptHash
->abHashValue
, RSAENH_MAX_HASH_SIZE
);
3915 for (i
=0; i
<RSAENH_HMAC_DEF_PAD_LEN
; i
++) {
3916 pad1
[i
] = RSAENH_HMAC_DEF_IPAD_CHAR
^ (i
<dwLen
? abHashValue
[i
] : 0);
3917 pad2
[i
] = RSAENH_HMAC_DEF_OPAD_CHAR
^ (i
<dwLen
? abHashValue
[i
] : 0);
3920 init_hash(pCryptHash
);
3921 update_hash(pCryptHash
, pad1
, RSAENH_HMAC_DEF_PAD_LEN
);
3922 finalize_hash(pCryptHash
);
3923 memcpy(abHashValue
, pCryptHash
->abHashValue
, pCryptHash
->dwHashSize
);
3925 init_hash(pCryptHash
);
3926 update_hash(pCryptHash
, pad2
, RSAENH_HMAC_DEF_PAD_LEN
);
3927 finalize_hash(pCryptHash
);
3928 memcpy(abHashValue
+pCryptHash
->dwHashSize
, pCryptHash
->abHashValue
,
3929 pCryptHash
->dwHashSize
);
3931 memcpy(pCryptHash
->abHashValue
, old_hashval
, RSAENH_MAX_HASH_SIZE
);
3934 memcpy(pCryptKey
->abKeyValue
, abHashValue
,
3935 RSAENH_MIN(pCryptKey
->dwKeyLen
, sizeof(pCryptKey
->abKeyValue
)));
3938 case ALG_CLASS_MSG_ENCRYPT
:
3939 if (!lookup_handle(&handle_table
, pCryptHash
->hKey
, RSAENH_MAGIC_KEY
,
3940 (OBJECTHDR
**)&pMasterKey
))
3942 SetLastError(NTE_FAIL
); /* FIXME error code */
3948 /* See RFC 2246, chapter 6.3 Key calculation */
3949 case CALG_SCHANNEL_ENC_KEY
:
3950 if (!pMasterKey
->siSChannelInfo
.saEncAlg
.Algid
||
3951 !pMasterKey
->siSChannelInfo
.saEncAlg
.cBits
)
3953 SetLastError(NTE_BAD_FLAGS
);
3956 *phKey
= new_key(hProv
, pMasterKey
->siSChannelInfo
.saEncAlg
.Algid
,
3957 MAKELONG(LOWORD(dwFlags
),pMasterKey
->siSChannelInfo
.saEncAlg
.cBits
),
3959 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
3960 memcpy(pCryptKey
->abKeyValue
,
3961 pCryptHash
->abHashValue
+ (
3962 2 * (pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
/ 8) +
3963 ((dwFlags
& CRYPT_SERVER
) ?
3964 (pMasterKey
->siSChannelInfo
.saEncAlg
.cBits
/ 8) : 0)),
3965 pMasterKey
->siSChannelInfo
.saEncAlg
.cBits
/ 8);
3966 memcpy(pCryptKey
->abInitVector
,
3967 pCryptHash
->abHashValue
+ (
3968 2 * (pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
/ 8) +
3969 2 * (pMasterKey
->siSChannelInfo
.saEncAlg
.cBits
/ 8) +
3970 ((dwFlags
& CRYPT_SERVER
) ? pCryptKey
->dwBlockLen
: 0)),
3971 pCryptKey
->dwBlockLen
);
3974 case CALG_SCHANNEL_MAC_KEY
:
3975 *phKey
= new_key(hProv
, Algid
,
3976 MAKELONG(LOWORD(dwFlags
),pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
),
3978 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
3979 memcpy(pCryptKey
->abKeyValue
,
3980 pCryptHash
->abHashValue
+ ((dwFlags
& CRYPT_SERVER
) ?
3981 pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
/ 8 : 0),
3982 pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
/ 8);
3986 SetLastError(NTE_BAD_ALGID
);
3992 SetLastError(NTE_BAD_ALGID
);
3996 setup_key(pCryptKey
);
4000 /******************************************************************************
4001 * CPGetUserKey (RSAENH.@)
4003 * Returns a handle to the user's private key-exchange- or signature-key.
4006 * hProv [I] The key container from which a user key is requested.
4007 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
4008 * phUserKey [O] Handle to the requested key or INVALID_HANDLE_VALUE in case of failure.
4015 * A newly created key container does not contain private user key. Create them with CPGenKey.
4017 BOOL WINAPI
RSAENH_CPGetUserKey(HCRYPTPROV hProv
, DWORD dwKeySpec
, HCRYPTKEY
*phUserKey
)
4019 KEYCONTAINER
*pKeyContainer
;
4021 TRACE("(hProv=%08lx, dwKeySpec=%08x, phUserKey=%p)\n", hProv
, dwKeySpec
, phUserKey
);
4023 if (!lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
,
4024 (OBJECTHDR
**)&pKeyContainer
))
4026 /* MSDN: hProv not containing valid context handle */
4027 SetLastError(NTE_BAD_UID
);
4033 case AT_KEYEXCHANGE
:
4034 copy_handle(&handle_table
, pKeyContainer
->hKeyExchangeKeyPair
, RSAENH_MAGIC_KEY
,
4039 copy_handle(&handle_table
, pKeyContainer
->hSignatureKeyPair
, RSAENH_MAGIC_KEY
,
4044 *phUserKey
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
4047 if (*phUserKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
4049 /* MSDN: dwKeySpec parameter specifies nonexistent key */
4050 SetLastError(NTE_NO_KEY
);
4057 /******************************************************************************
4058 * CPHashData (RSAENH.@)
4060 * Updates a hash object with the given data.
4063 * hProv [I] Key container to which the hash object belongs.
4064 * hHash [I] Hash object which is to be updated.
4065 * pbData [I] Pointer to data with which the hash object is to be updated.
4066 * dwDataLen [I] Length of the data.
4067 * dwFlags [I] Currently none defined.
4074 * The actual hash value is queried with CPGetHashParam, which will finalize
4075 * the hash. Updating a finalized hash will fail with a last error NTE_BAD_HASH_STATE.
4077 BOOL WINAPI
RSAENH_CPHashData(HCRYPTPROV hProv
, HCRYPTHASH hHash
, CONST BYTE
*pbData
,
4078 DWORD dwDataLen
, DWORD dwFlags
)
4080 CRYPTHASH
*pCryptHash
;
4082 TRACE("(hProv=%08lx, hHash=%08lx, pbData=%p, dwDataLen=%d, dwFlags=%08x)\n",
4083 hProv
, hHash
, pbData
, dwDataLen
, dwFlags
);
4087 SetLastError(NTE_BAD_FLAGS
);
4091 if (!lookup_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
,
4092 (OBJECTHDR
**)&pCryptHash
))
4094 SetLastError(NTE_BAD_HASH
);
4098 if (!get_algid_info(hProv
, pCryptHash
->aiAlgid
) || pCryptHash
->aiAlgid
== CALG_SSL3_SHAMD5
)
4100 SetLastError(NTE_BAD_ALGID
);
4104 if (pCryptHash
->dwState
!= RSAENH_HASHSTATE_HASHING
)
4106 SetLastError(NTE_BAD_HASH_STATE
);
4110 update_hash(pCryptHash
, pbData
, dwDataLen
);
4114 /******************************************************************************
4115 * CPHashSessionKey (RSAENH.@)
4117 * Updates a hash object with the binary representation of a symmetric key.
4120 * hProv [I] Key container to which the hash object belongs.
4121 * hHash [I] Hash object which is to be updated.
4122 * hKey [I] The symmetric key, whose binary value will be added to the hash.
4123 * dwFlags [I] CRYPT_LITTLE_ENDIAN, if the binary key value shall be interpreted as little endian.
4129 BOOL WINAPI
RSAENH_CPHashSessionKey(HCRYPTPROV hProv
, HCRYPTHASH hHash
, HCRYPTKEY hKey
,
4132 BYTE abKeyValue
[RSAENH_MAX_KEY_SIZE
], bTemp
;
4136 TRACE("(hProv=%08lx, hHash=%08lx, hKey=%08lx, dwFlags=%08x)\n", hProv
, hHash
, hKey
, dwFlags
);
4138 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pKey
) ||
4139 (GET_ALG_CLASS(pKey
->aiAlgid
) != ALG_CLASS_DATA_ENCRYPT
))
4141 SetLastError(NTE_BAD_KEY
);
4145 if (dwFlags
& ~CRYPT_LITTLE_ENDIAN
) {
4146 SetLastError(NTE_BAD_FLAGS
);
4150 memcpy(abKeyValue
, pKey
->abKeyValue
, pKey
->dwKeyLen
);
4151 if (!(dwFlags
& CRYPT_LITTLE_ENDIAN
)) {
4152 for (i
=0; i
<pKey
->dwKeyLen
/2; i
++) {
4153 bTemp
= abKeyValue
[i
];
4154 abKeyValue
[i
] = abKeyValue
[pKey
->dwKeyLen
-i
-1];
4155 abKeyValue
[pKey
->dwKeyLen
-i
-1] = bTemp
;
4159 return RSAENH_CPHashData(hProv
, hHash
, abKeyValue
, pKey
->dwKeyLen
, 0);
4162 /******************************************************************************
4163 * CPReleaseContext (RSAENH.@)
4165 * Release a key container.
4168 * hProv [I] Key container to be released.
4169 * dwFlags [I] Currently none defined.
4175 BOOL WINAPI
RSAENH_CPReleaseContext(HCRYPTPROV hProv
, DWORD dwFlags
)
4177 TRACE("(hProv=%08lx, dwFlags=%08x)\n", hProv
, dwFlags
);
4179 if (!release_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
4181 /* MSDN: hProv not containing valid context handle */
4182 SetLastError(NTE_BAD_UID
);
4187 SetLastError(NTE_BAD_FLAGS
);
4194 /******************************************************************************
4195 * CPSetHashParam (RSAENH.@)
4197 * Set a parameter of a hash object
4200 * hProv [I] The key container to which the key belongs.
4201 * hHash [I] The hash object for which a parameter is to be set.
4202 * dwParam [I] Parameter type. See Notes.
4203 * pbData [I] Pointer to the parameter value.
4204 * dwFlags [I] Currently none defined.
4211 * Currently only the HP_HMAC_INFO dwParam type is defined.
4212 * The HMAC_INFO struct will be deep copied into the hash object.
4213 * See Internet RFC 2104 for details on the HMAC algorithm.
4215 BOOL WINAPI
RSAENH_CPSetHashParam(HCRYPTPROV hProv
, HCRYPTHASH hHash
, DWORD dwParam
,
4216 BYTE
*pbData
, DWORD dwFlags
)
4218 CRYPTHASH
*pCryptHash
;
4219 CRYPTKEY
*pCryptKey
;
4222 TRACE("(hProv=%08lx, hHash=%08lx, dwParam=%08x, pbData=%p, dwFlags=%08x)\n",
4223 hProv
, hHash
, dwParam
, pbData
, dwFlags
);
4225 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
4227 SetLastError(NTE_BAD_UID
);
4232 SetLastError(NTE_BAD_FLAGS
);
4236 if (!lookup_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
,
4237 (OBJECTHDR
**)&pCryptHash
))
4239 SetLastError(NTE_BAD_HASH
);
4245 free_hmac_info(pCryptHash
->pHMACInfo
);
4246 if (!copy_hmac_info(&pCryptHash
->pHMACInfo
, (PHMAC_INFO
)pbData
)) return FALSE
;
4248 if (!lookup_handle(&handle_table
, pCryptHash
->hKey
, RSAENH_MAGIC_KEY
,
4249 (OBJECTHDR
**)&pCryptKey
))
4251 SetLastError(NTE_FAIL
); /* FIXME: correct error code? */
4255 if (pCryptKey
->aiAlgid
== CALG_HMAC
&& !pCryptKey
->dwKeyLen
) {
4256 HCRYPTHASH hKeyHash
;
4259 if (!RSAENH_CPCreateHash(hProv
, ((PHMAC_INFO
)pbData
)->HashAlgid
, 0, 0,
4262 if (!RSAENH_CPHashData(hProv
, hKeyHash
, pCryptKey
->blobHmacKey
.pbData
,
4263 pCryptKey
->blobHmacKey
.cbData
, 0))
4265 RSAENH_CPDestroyHash(hProv
, hKeyHash
);
4268 keyLen
= sizeof(pCryptKey
->abKeyValue
);
4269 if (!RSAENH_CPGetHashParam(hProv
, hKeyHash
, HP_HASHVAL
, pCryptKey
->abKeyValue
,
4272 RSAENH_CPDestroyHash(hProv
, hKeyHash
);
4275 pCryptKey
->dwKeyLen
= keyLen
;
4276 RSAENH_CPDestroyHash(hProv
, hKeyHash
);
4278 for (i
=0; i
<RSAENH_MIN(pCryptKey
->dwKeyLen
,pCryptHash
->pHMACInfo
->cbInnerString
); i
++) {
4279 pCryptHash
->pHMACInfo
->pbInnerString
[i
] ^= pCryptKey
->abKeyValue
[i
];
4281 for (i
=0; i
<RSAENH_MIN(pCryptKey
->dwKeyLen
,pCryptHash
->pHMACInfo
->cbOuterString
); i
++) {
4282 pCryptHash
->pHMACInfo
->pbOuterString
[i
] ^= pCryptKey
->abKeyValue
[i
];
4285 init_hash(pCryptHash
);
4289 memcpy(pCryptHash
->abHashValue
, pbData
, pCryptHash
->dwHashSize
);
4290 pCryptHash
->dwState
= RSAENH_HASHSTATE_FINISHED
;
4293 case HP_TLS1PRF_SEED
:
4294 return copy_data_blob(&pCryptHash
->tpPRFParams
.blobSeed
, (PCRYPT_DATA_BLOB
)pbData
);
4296 case HP_TLS1PRF_LABEL
:
4297 return copy_data_blob(&pCryptHash
->tpPRFParams
.blobLabel
, (PCRYPT_DATA_BLOB
)pbData
);
4300 SetLastError(NTE_BAD_TYPE
);
4305 /******************************************************************************
4306 * CPSetProvParam (RSAENH.@)
4308 BOOL WINAPI
RSAENH_CPSetProvParam(HCRYPTPROV hProv
, DWORD dwParam
, BYTE
*pbData
, DWORD dwFlags
)
4314 /******************************************************************************
4315 * CPSignHash (RSAENH.@)
4317 * Sign a hash object
4320 * hProv [I] The key container, to which the hash object belongs.
4321 * hHash [I] The hash object to be signed.
4322 * dwKeySpec [I] AT_SIGNATURE or AT_KEYEXCHANGE: Key used to generate the signature.
4323 * sDescription [I] Should be NULL for security reasons.
4324 * dwFlags [I] 0, CRYPT_NOHASHOID or CRYPT_X931_FORMAT: Format of the signature.
4325 * pbSignature [O] Buffer, to which the signature will be stored. May be NULL to query SigLen.
4326 * pdwSigLen [I/O] Size of the buffer (in), Length of the signature (out)
4332 BOOL WINAPI
RSAENH_CPSignHash(HCRYPTPROV hProv
, HCRYPTHASH hHash
, DWORD dwKeySpec
,
4333 LPCWSTR sDescription
, DWORD dwFlags
, BYTE
*pbSignature
,
4336 HCRYPTKEY hCryptKey
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
4337 CRYPTKEY
*pCryptKey
;
4339 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
];
4343 TRACE("(hProv=%08lx, hHash=%08lx, dwKeySpec=%08x, sDescription=%s, dwFlags=%08x, "
4344 "pbSignature=%p, pdwSigLen=%p)\n", hProv
, hHash
, dwKeySpec
, debugstr_w(sDescription
),
4345 dwFlags
, pbSignature
, pdwSigLen
);
4347 if (dwFlags
& ~(CRYPT_NOHASHOID
|CRYPT_X931_FORMAT
)) {
4348 SetLastError(NTE_BAD_FLAGS
);
4352 if (!RSAENH_CPGetUserKey(hProv
, dwKeySpec
, &hCryptKey
)) return FALSE
;
4354 if (!lookup_handle(&handle_table
, hCryptKey
, RSAENH_MAGIC_KEY
,
4355 (OBJECTHDR
**)&pCryptKey
))
4357 SetLastError(NTE_NO_KEY
);
4362 *pdwSigLen
= pCryptKey
->dwKeyLen
;
4366 if (pCryptKey
->dwKeyLen
> *pdwSigLen
)
4368 SetLastError(ERROR_MORE_DATA
);
4369 *pdwSigLen
= pCryptKey
->dwKeyLen
;
4372 *pdwSigLen
= pCryptKey
->dwKeyLen
;
4375 if (!RSAENH_CPHashData(hProv
, hHash
, (CONST BYTE
*)sDescription
,
4376 (DWORD
)lstrlenW(sDescription
)*sizeof(WCHAR
), 0))
4382 dwHashLen
= sizeof(DWORD
);
4383 if (!RSAENH_CPGetHashParam(hProv
, hHash
, HP_ALGID
, (BYTE
*)&aiAlgid
, &dwHashLen
, 0)) goto out
;
4385 dwHashLen
= RSAENH_MAX_HASH_SIZE
;
4386 if (!RSAENH_CPGetHashParam(hProv
, hHash
, HP_HASHVAL
, abHashValue
, &dwHashLen
, 0)) goto out
;
4389 if (!build_hash_signature(pbSignature
, *pdwSigLen
, aiAlgid
, abHashValue
, dwHashLen
, dwFlags
)) {
4393 ret
= encrypt_block_impl(pCryptKey
->aiAlgid
, PK_PRIVATE
, &pCryptKey
->context
, pbSignature
, pbSignature
, RSAENH_ENCRYPT
);
4395 RSAENH_CPDestroyKey(hProv
, hCryptKey
);
4399 /******************************************************************************
4400 * CPVerifySignature (RSAENH.@)
4402 * Verify the signature of a hash object.
4405 * hProv [I] The key container, to which the hash belongs.
4406 * hHash [I] The hash for which the signature is verified.
4407 * pbSignature [I] The binary signature.
4408 * dwSigLen [I] Length of the signature BLOB.
4409 * hPubKey [I] Public key used to verify the signature.
4410 * sDescription [I] Should be NULL for security reasons.
4411 * dwFlags [I] 0, CRYPT_NOHASHOID or CRYPT_X931_FORMAT: Format of the signature.
4414 * Success: TRUE (Signature is valid)
4415 * Failure: FALSE (GetLastError() == NTE_BAD_SIGNATURE, if signature is invalid)
4417 BOOL WINAPI
RSAENH_CPVerifySignature(HCRYPTPROV hProv
, HCRYPTHASH hHash
, CONST BYTE
*pbSignature
,
4418 DWORD dwSigLen
, HCRYPTKEY hPubKey
, LPCWSTR sDescription
,
4421 BYTE
*pbConstructed
= NULL
, *pbDecrypted
= NULL
;
4422 CRYPTKEY
*pCryptKey
;
4425 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
];
4428 TRACE("(hProv=%08lx, hHash=%08lx, pbSignature=%p, dwSigLen=%d, hPubKey=%08lx, sDescription=%s, "
4429 "dwFlags=%08x)\n", hProv
, hHash
, pbSignature
, dwSigLen
, hPubKey
, debugstr_w(sDescription
),
4432 if (dwFlags
& ~(CRYPT_NOHASHOID
|CRYPT_X931_FORMAT
)) {
4433 SetLastError(NTE_BAD_FLAGS
);
4437 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
4439 SetLastError(NTE_BAD_UID
);
4443 if (!lookup_handle(&handle_table
, hPubKey
, RSAENH_MAGIC_KEY
,
4444 (OBJECTHDR
**)&pCryptKey
))
4446 SetLastError(NTE_BAD_KEY
);
4450 /* in Microsoft implementation, the signature length is checked before
4451 * the signature pointer.
4453 if (dwSigLen
!= pCryptKey
->dwKeyLen
)
4455 SetLastError(NTE_BAD_SIGNATURE
);
4459 if (!hHash
|| !pbSignature
)
4461 SetLastError(ERROR_INVALID_PARAMETER
);
4466 if (!RSAENH_CPHashData(hProv
, hHash
, (CONST BYTE
*)sDescription
,
4467 (DWORD
)lstrlenW(sDescription
)*sizeof(WCHAR
), 0))
4473 dwHashLen
= sizeof(DWORD
);
4474 if (!RSAENH_CPGetHashParam(hProv
, hHash
, HP_ALGID
, (BYTE
*)&aiAlgid
, &dwHashLen
, 0)) return FALSE
;
4476 dwHashLen
= RSAENH_MAX_HASH_SIZE
;
4477 if (!RSAENH_CPGetHashParam(hProv
, hHash
, HP_HASHVAL
, abHashValue
, &dwHashLen
, 0)) return FALSE
;
4479 pbConstructed
= HeapAlloc(GetProcessHeap(), 0, dwSigLen
);
4480 if (!pbConstructed
) {
4481 SetLastError(NTE_NO_MEMORY
);
4485 pbDecrypted
= HeapAlloc(GetProcessHeap(), 0, dwSigLen
);
4487 SetLastError(NTE_NO_MEMORY
);
4491 if (!encrypt_block_impl(pCryptKey
->aiAlgid
, PK_PUBLIC
, &pCryptKey
->context
, pbSignature
, pbDecrypted
,
4497 if (build_hash_signature(pbConstructed
, dwSigLen
, aiAlgid
, abHashValue
, dwHashLen
, dwFlags
) &&
4498 !memcmp(pbDecrypted
, pbConstructed
, dwSigLen
)) {
4503 if (!(dwFlags
& CRYPT_NOHASHOID
) &&
4504 build_hash_signature(pbConstructed
, dwSigLen
, aiAlgid
, abHashValue
, dwHashLen
, dwFlags
|CRYPT_NOHASHOID
) &&
4505 !memcmp(pbDecrypted
, pbConstructed
, dwSigLen
)) {
4510 SetLastError(NTE_BAD_SIGNATURE
);
4513 HeapFree(GetProcessHeap(), 0, pbConstructed
);
4514 HeapFree(GetProcessHeap(), 0, pbDecrypted
);
4518 /******************************************************************************
4519 * DllRegisterServer (RSAENH.@)
4521 HRESULT WINAPI
DllRegisterServer(void)
4523 return __wine_register_resources( instance
);
4526 /******************************************************************************
4527 * DllUnregisterServer (RSAENH.@)
4529 HRESULT WINAPI
DllUnregisterServer(void)
4531 return __wine_unregister_resources( instance
);