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 int WINAPI
DllMain(HINSTANCE hInstance
, DWORD fdwReason
, PVOID pvReserved
)
365 case DLL_PROCESS_ATTACH
:
366 instance
= hInstance
;
367 DisableThreadLibraryCalls(hInstance
);
368 init_handle_table(&handle_table
);
371 case DLL_PROCESS_DETACH
:
372 destroy_handle_table(&handle_table
);
378 /******************************************************************************
379 * copy_param [Internal]
381 * Helper function that supports the standard WINAPI protocol for querying data
385 * pbBuffer [O] Buffer where the queried parameter is copied to, if it is large enough.
386 * May be NUL if the required buffer size is to be queried only.
387 * pdwBufferSize [I/O] In: Size of the buffer at pbBuffer
388 * Out: Size of parameter pbParam
389 * pbParam [I] Parameter value.
390 * dwParamSize [I] Size of pbParam
393 * Success: TRUE (pbParam was copied into pbBuffer or pbBuffer is NULL)
394 * Failure: FALSE (pbBuffer is not large enough to hold pbParam). Last error: ERROR_MORE_DATA
396 static inline BOOL
copy_param(
397 BYTE
*pbBuffer
, DWORD
*pdwBufferSize
, CONST BYTE
*pbParam
, DWORD dwParamSize
)
401 if (dwParamSize
> *pdwBufferSize
)
403 SetLastError(ERROR_MORE_DATA
);
404 *pdwBufferSize
= dwParamSize
;
407 memcpy(pbBuffer
, pbParam
, dwParamSize
);
409 *pdwBufferSize
= dwParamSize
;
413 /******************************************************************************
414 * get_algid_info [Internal]
416 * Query CSP capabilities for a given crypto algorithm.
419 * hProv [I] Handle to a key container of the CSP whose capabilities are to be queried.
420 * algid [I] Identifier of the crypto algorithm about which information is requested.
423 * Success: Pointer to a PROV_ENUMALGS_EX struct containing information about the crypto algorithm.
424 * Failure: NULL (algid not supported)
426 static inline const PROV_ENUMALGS_EX
* get_algid_info(HCRYPTPROV hProv
, ALG_ID algid
) {
427 const PROV_ENUMALGS_EX
*iterator
;
428 KEYCONTAINER
*pKeyContainer
;
430 if (!lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
, (OBJECTHDR
**)&pKeyContainer
)) {
431 SetLastError(NTE_BAD_UID
);
435 for (iterator
= aProvEnumAlgsEx
[pKeyContainer
->dwPersonality
]; iterator
->aiAlgid
; iterator
++) {
436 if (iterator
->aiAlgid
== algid
) return iterator
;
439 SetLastError(NTE_BAD_ALGID
);
443 /******************************************************************************
444 * copy_data_blob [Internal]
446 * deeply copies a DATA_BLOB
449 * dst [O] That's where the blob will be copied to
450 * src [I] Source blob
454 * Failure: FALSE (GetLastError() == NTE_NO_MEMORY
457 * Use free_data_blob to release resources occupied by copy_data_blob.
459 static inline BOOL
copy_data_blob(PCRYPT_DATA_BLOB dst
, CONST PCRYPT_DATA_BLOB src
) {
460 dst
->pbData
= HeapAlloc(GetProcessHeap(), 0, src
->cbData
);
462 SetLastError(NTE_NO_MEMORY
);
465 dst
->cbData
= src
->cbData
;
466 memcpy(dst
->pbData
, src
->pbData
, src
->cbData
);
470 /******************************************************************************
471 * concat_data_blobs [Internal]
473 * Concatenates two blobs
476 * dst [O] The new blob will be copied here
477 * src1 [I] Prefix blob
478 * src2 [I] Appendix blob
482 * Failure: FALSE (GetLastError() == NTE_NO_MEMORY)
485 * Release resources occupied by concat_data_blobs with free_data_blobs
487 static inline BOOL
concat_data_blobs(PCRYPT_DATA_BLOB dst
, CONST PCRYPT_DATA_BLOB src1
,
488 CONST PCRYPT_DATA_BLOB src2
)
490 dst
->cbData
= src1
->cbData
+ src2
->cbData
;
491 dst
->pbData
= HeapAlloc(GetProcessHeap(), 0, dst
->cbData
);
493 SetLastError(NTE_NO_MEMORY
);
496 memcpy(dst
->pbData
, src1
->pbData
, src1
->cbData
);
497 memcpy(dst
->pbData
+ src1
->cbData
, src2
->pbData
, src2
->cbData
);
501 /******************************************************************************
502 * free_data_blob [Internal]
504 * releases resource occupied by a dynamically allocated CRYPT_DATA_BLOB
507 * pBlob [I] Heap space occupied by pBlob->pbData is released
509 static inline void free_data_blob(PCRYPT_DATA_BLOB pBlob
) {
510 HeapFree(GetProcessHeap(), 0, pBlob
->pbData
);
513 /******************************************************************************
514 * init_data_blob [Internal]
516 static inline void init_data_blob(PCRYPT_DATA_BLOB pBlob
) {
517 pBlob
->pbData
= NULL
;
521 /******************************************************************************
522 * free_hmac_info [Internal]
524 * Deeply free an HMAC_INFO struct.
527 * hmac_info [I] Pointer to the HMAC_INFO struct to be freed.
530 * See Internet RFC 2104 for details on the HMAC algorithm.
532 static inline void free_hmac_info(PHMAC_INFO hmac_info
) {
533 if (!hmac_info
) return;
534 HeapFree(GetProcessHeap(), 0, hmac_info
->pbInnerString
);
535 HeapFree(GetProcessHeap(), 0, hmac_info
->pbOuterString
);
536 HeapFree(GetProcessHeap(), 0, hmac_info
);
539 /******************************************************************************
540 * copy_hmac_info [Internal]
542 * Deeply copy an HMAC_INFO struct
545 * dst [O] Pointer to a location where the pointer to the HMAC_INFO copy will be stored.
546 * src [I] Pointer to the HMAC_INFO struct to be copied.
553 * See Internet RFC 2104 for details on the HMAC algorithm.
555 static BOOL
copy_hmac_info(PHMAC_INFO
*dst
, const HMAC_INFO
*src
) {
556 if (!src
) return FALSE
;
557 *dst
= HeapAlloc(GetProcessHeap(), 0, sizeof(HMAC_INFO
));
558 if (!*dst
) return FALSE
;
560 (*dst
)->pbInnerString
= NULL
;
561 (*dst
)->pbOuterString
= NULL
;
562 if ((*dst
)->cbInnerString
== 0) (*dst
)->cbInnerString
= RSAENH_HMAC_DEF_PAD_LEN
;
563 (*dst
)->pbInnerString
= HeapAlloc(GetProcessHeap(), 0, (*dst
)->cbInnerString
);
564 if (!(*dst
)->pbInnerString
) {
565 free_hmac_info(*dst
);
568 if (src
->cbInnerString
)
569 memcpy((*dst
)->pbInnerString
, src
->pbInnerString
, src
->cbInnerString
);
571 memset((*dst
)->pbInnerString
, RSAENH_HMAC_DEF_IPAD_CHAR
, RSAENH_HMAC_DEF_PAD_LEN
);
572 if ((*dst
)->cbOuterString
== 0) (*dst
)->cbOuterString
= RSAENH_HMAC_DEF_PAD_LEN
;
573 (*dst
)->pbOuterString
= HeapAlloc(GetProcessHeap(), 0, (*dst
)->cbOuterString
);
574 if (!(*dst
)->pbOuterString
) {
575 free_hmac_info(*dst
);
578 if (src
->cbOuterString
)
579 memcpy((*dst
)->pbOuterString
, src
->pbOuterString
, src
->cbOuterString
);
581 memset((*dst
)->pbOuterString
, RSAENH_HMAC_DEF_OPAD_CHAR
, RSAENH_HMAC_DEF_PAD_LEN
);
585 /******************************************************************************
586 * destroy_hash [Internal]
588 * Destructor for hash objects
591 * pCryptHash [I] Pointer to the hash object to be destroyed.
592 * Will be invalid after function returns!
594 static void destroy_hash(OBJECTHDR
*pObject
)
596 CRYPTHASH
*pCryptHash
= (CRYPTHASH
*)pObject
;
598 free_hmac_info(pCryptHash
->pHMACInfo
);
599 free_data_blob(&pCryptHash
->tpPRFParams
.blobLabel
);
600 free_data_blob(&pCryptHash
->tpPRFParams
.blobSeed
);
601 HeapFree(GetProcessHeap(), 0, pCryptHash
);
604 /******************************************************************************
605 * init_hash [Internal]
607 * Initialize (or reset) a hash object
610 * pCryptHash [I] The hash object to be initialized.
612 static inline BOOL
init_hash(CRYPTHASH
*pCryptHash
) {
615 switch (pCryptHash
->aiAlgid
)
618 if (pCryptHash
->pHMACInfo
) {
619 const PROV_ENUMALGS_EX
*pAlgInfo
;
621 pAlgInfo
= get_algid_info(pCryptHash
->hProv
, pCryptHash
->pHMACInfo
->HashAlgid
);
622 if (!pAlgInfo
) return FALSE
;
623 pCryptHash
->dwHashSize
= pAlgInfo
->dwDefaultLen
>> 3;
624 init_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
);
625 update_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
626 pCryptHash
->pHMACInfo
->pbInnerString
,
627 pCryptHash
->pHMACInfo
->cbInnerString
);
632 dwLen
= sizeof(DWORD
);
633 RSAENH_CPGetKeyParam(pCryptHash
->hProv
, pCryptHash
->hKey
, KP_BLOCKLEN
,
634 (BYTE
*)&pCryptHash
->dwHashSize
, &dwLen
, 0);
635 pCryptHash
->dwHashSize
>>= 3;
639 return init_hash_impl(pCryptHash
->aiAlgid
, &pCryptHash
->context
);
643 /******************************************************************************
644 * update_hash [Internal]
646 * Hashes the given data and updates the hash object's state accordingly
649 * pCryptHash [I] Hash object to be updated.
650 * pbData [I] Pointer to data stream to be hashed.
651 * dwDataLen [I] Length of data stream.
653 static inline void update_hash(CRYPTHASH
*pCryptHash
, CONST BYTE
*pbData
, DWORD dwDataLen
) {
656 switch (pCryptHash
->aiAlgid
)
659 if (pCryptHash
->pHMACInfo
)
660 update_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
665 pbTemp
= HeapAlloc(GetProcessHeap(), 0, dwDataLen
);
667 memcpy(pbTemp
, pbData
, dwDataLen
);
668 RSAENH_CPEncrypt(pCryptHash
->hProv
, pCryptHash
->hKey
, 0, FALSE
, 0,
669 pbTemp
, &dwDataLen
, dwDataLen
);
670 HeapFree(GetProcessHeap(), 0, pbTemp
);
674 update_hash_impl(pCryptHash
->aiAlgid
, &pCryptHash
->context
, pbData
, dwDataLen
);
678 /******************************************************************************
679 * finalize_hash [Internal]
681 * Finalizes the hash, after all data has been hashed with update_hash.
682 * No additional data can be hashed afterwards until the hash gets initialized again.
685 * pCryptHash [I] Hash object to be finalized.
687 static inline void finalize_hash(CRYPTHASH
*pCryptHash
) {
690 switch (pCryptHash
->aiAlgid
)
693 if (pCryptHash
->pHMACInfo
) {
694 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
];
696 finalize_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
697 pCryptHash
->abHashValue
);
698 memcpy(abHashValue
, pCryptHash
->abHashValue
, pCryptHash
->dwHashSize
);
699 init_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
);
700 update_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
701 pCryptHash
->pHMACInfo
->pbOuterString
,
702 pCryptHash
->pHMACInfo
->cbOuterString
);
703 update_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
704 abHashValue
, pCryptHash
->dwHashSize
);
705 finalize_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
706 pCryptHash
->abHashValue
);
712 RSAENH_CPEncrypt(pCryptHash
->hProv
, pCryptHash
->hKey
, 0, TRUE
, 0,
713 pCryptHash
->abHashValue
, &dwDataLen
, pCryptHash
->dwHashSize
);
717 finalize_hash_impl(pCryptHash
->aiAlgid
, &pCryptHash
->context
, pCryptHash
->abHashValue
);
721 /******************************************************************************
722 * destroy_key [Internal]
724 * Destructor for key objects
727 * pCryptKey [I] Pointer to the key object to be destroyed.
728 * Will be invalid after function returns!
730 static void destroy_key(OBJECTHDR
*pObject
)
732 CRYPTKEY
*pCryptKey
= (CRYPTKEY
*)pObject
;
734 free_key_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
);
735 free_data_blob(&pCryptKey
->siSChannelInfo
.blobClientRandom
);
736 free_data_blob(&pCryptKey
->siSChannelInfo
.blobServerRandom
);
737 free_data_blob(&pCryptKey
->blobHmacKey
);
738 HeapFree(GetProcessHeap(), 0, pCryptKey
);
741 /******************************************************************************
742 * setup_key [Internal]
744 * Initialize (or reset) a key object
747 * pCryptKey [I] The key object to be initialized.
749 static inline void setup_key(CRYPTKEY
*pCryptKey
) {
750 pCryptKey
->dwState
= RSAENH_KEYSTATE_IDLE
;
751 memcpy(pCryptKey
->abChainVector
, pCryptKey
->abInitVector
, sizeof(pCryptKey
->abChainVector
));
752 setup_key_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pCryptKey
->dwKeyLen
,
753 pCryptKey
->dwEffectiveKeyLen
, pCryptKey
->dwSaltLen
,
754 pCryptKey
->abKeyValue
);
757 /******************************************************************************
760 * Creates a new key object without assigning the actual binary key value.
761 * This is done by CPDeriveKey, CPGenKey or CPImportKey, which call this function.
764 * hProv [I] Handle to the provider to which the created key will belong.
765 * aiAlgid [I] The new key shall use the crypto algorithm identified by aiAlgid.
766 * dwFlags [I] Upper 16 bits give the key length.
767 * Lower 16 bits: CRYPT_EXPORTABLE, CRYPT_CREATE_SALT,
769 * ppCryptKey [O] Pointer to the created key
772 * Success: Handle to the created key.
773 * Failure: INVALID_HANDLE_VALUE
775 static HCRYPTKEY
new_key(HCRYPTPROV hProv
, ALG_ID aiAlgid
, DWORD dwFlags
, CRYPTKEY
**ppCryptKey
)
779 DWORD dwKeyLen
= HIWORD(dwFlags
);
780 const PROV_ENUMALGS_EX
*peaAlgidInfo
;
785 * Retrieve the CSP's capabilities for the given ALG_ID value
787 peaAlgidInfo
= get_algid_info(hProv
, aiAlgid
);
788 if (!peaAlgidInfo
) return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
790 TRACE("alg = %s, dwKeyLen = %d\n", debugstr_a(peaAlgidInfo
->szName
),
793 * Assume the default key length, if none is specified explicitly
795 if (dwKeyLen
== 0) dwKeyLen
= peaAlgidInfo
->dwDefaultLen
;
798 * Check if the requested key length is supported by the current CSP.
799 * Adjust key length's for DES algorithms.
803 if (dwKeyLen
== RSAENH_DES_EFFECTIVE_KEYLEN
) {
804 dwKeyLen
= RSAENH_DES_STORAGE_KEYLEN
;
806 if (dwKeyLen
!= RSAENH_DES_STORAGE_KEYLEN
) {
807 SetLastError(NTE_BAD_FLAGS
);
808 return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
813 if (dwKeyLen
== RSAENH_3DES112_EFFECTIVE_KEYLEN
) {
814 dwKeyLen
= RSAENH_3DES112_STORAGE_KEYLEN
;
816 if (dwKeyLen
!= RSAENH_3DES112_STORAGE_KEYLEN
) {
817 SetLastError(NTE_BAD_FLAGS
);
818 return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
823 if (dwKeyLen
== RSAENH_3DES_EFFECTIVE_KEYLEN
) {
824 dwKeyLen
= RSAENH_3DES_STORAGE_KEYLEN
;
826 if (dwKeyLen
!= RSAENH_3DES_STORAGE_KEYLEN
) {
827 SetLastError(NTE_BAD_FLAGS
);
828 return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
833 /* Avoid the key length check for HMAC keys, which have unlimited
840 dwKeyLen
> peaAlgidInfo
->dwMaxLen
||
841 dwKeyLen
< peaAlgidInfo
->dwMinLen
)
843 TRACE("key len %d out of bounds (%d, %d)\n", dwKeyLen
,
844 peaAlgidInfo
->dwMinLen
, peaAlgidInfo
->dwMaxLen
);
845 SetLastError(NTE_BAD_DATA
);
846 return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
850 hCryptKey
= new_object(&handle_table
, sizeof(CRYPTKEY
), RSAENH_MAGIC_KEY
,
851 destroy_key
, (OBJECTHDR
**)&pCryptKey
);
852 if (hCryptKey
!= (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
854 pCryptKey
->aiAlgid
= aiAlgid
;
855 pCryptKey
->hProv
= hProv
;
856 pCryptKey
->dwModeBits
= 0;
857 pCryptKey
->dwPermissions
= CRYPT_ENCRYPT
| CRYPT_DECRYPT
| CRYPT_READ
| CRYPT_WRITE
|
859 if (dwFlags
& CRYPT_EXPORTABLE
)
860 pCryptKey
->dwPermissions
|= CRYPT_EXPORT
;
861 pCryptKey
->dwKeyLen
= dwKeyLen
>> 3;
862 pCryptKey
->dwEffectiveKeyLen
= 0;
863 if ((dwFlags
& CRYPT_CREATE_SALT
) || (dwKeyLen
== 40 && !(dwFlags
& CRYPT_NO_SALT
)))
864 pCryptKey
->dwSaltLen
= 16 /*FIXME*/ - pCryptKey
->dwKeyLen
;
866 pCryptKey
->dwSaltLen
= 0;
867 memset(pCryptKey
->abKeyValue
, 0, sizeof(pCryptKey
->abKeyValue
));
868 memset(pCryptKey
->abInitVector
, 0, sizeof(pCryptKey
->abInitVector
));
869 memset(&pCryptKey
->siSChannelInfo
.saEncAlg
, 0, sizeof(pCryptKey
->siSChannelInfo
.saEncAlg
));
870 memset(&pCryptKey
->siSChannelInfo
.saMACAlg
, 0, sizeof(pCryptKey
->siSChannelInfo
.saMACAlg
));
871 init_data_blob(&pCryptKey
->siSChannelInfo
.blobClientRandom
);
872 init_data_blob(&pCryptKey
->siSChannelInfo
.blobServerRandom
);
873 init_data_blob(&pCryptKey
->blobHmacKey
);
877 case CALG_PCT1_MASTER
:
878 case CALG_SSL2_MASTER
:
879 case CALG_SSL3_MASTER
:
880 case CALG_TLS1_MASTER
:
882 pCryptKey
->dwBlockLen
= 0;
883 pCryptKey
->dwMode
= 0;
890 pCryptKey
->dwBlockLen
= 8;
891 pCryptKey
->dwMode
= CRYPT_MODE_CBC
;
898 pCryptKey
->dwBlockLen
= 16;
899 pCryptKey
->dwMode
= CRYPT_MODE_ECB
;
904 pCryptKey
->dwBlockLen
= dwKeyLen
>> 3;
905 pCryptKey
->dwMode
= 0;
909 pCryptKey
->dwBlockLen
= 0;
910 pCryptKey
->dwMode
= 0;
914 *ppCryptKey
= pCryptKey
;
920 /******************************************************************************
921 * map_key_spec_to_key_pair_name [Internal]
923 * Returns the name of the registry value associated with a key spec.
926 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
929 * Success: Name of registry value.
932 static LPCSTR
map_key_spec_to_key_pair_name(DWORD dwKeySpec
)
939 szValueName
= "KeyExchangeKeyPair";
942 szValueName
= "SignatureKeyPair";
945 WARN("invalid key spec %d\n", dwKeySpec
);
951 /******************************************************************************
952 * store_key_pair [Internal]
954 * Stores a key pair to the registry
957 * hCryptKey [I] Handle to the key to be stored
958 * hKey [I] Registry key where the key pair is to be stored
959 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
960 * dwFlags [I] Flags for protecting the key
962 static void store_key_pair(HCRYPTKEY hCryptKey
, HKEY hKey
, DWORD dwKeySpec
, DWORD dwFlags
)
965 DATA_BLOB blobIn
, blobOut
;
970 if (!(szValueName
= map_key_spec_to_key_pair_name(dwKeySpec
)))
972 if (lookup_handle(&handle_table
, hCryptKey
, RSAENH_MAGIC_KEY
,
975 if (crypt_export_key(pKey
, 0, PRIVATEKEYBLOB
, 0, TRUE
, 0, &dwLen
))
977 pbKey
= HeapAlloc(GetProcessHeap(), 0, dwLen
);
980 if (crypt_export_key(pKey
, 0, PRIVATEKEYBLOB
, 0, TRUE
, pbKey
,
983 blobIn
.pbData
= pbKey
;
984 blobIn
.cbData
= dwLen
;
986 if (CryptProtectData(&blobIn
, NULL
, NULL
, NULL
, NULL
,
989 RegSetValueExA(hKey
, szValueName
, 0, REG_BINARY
,
990 blobOut
.pbData
, blobOut
.cbData
);
991 LocalFree(blobOut
.pbData
);
994 HeapFree(GetProcessHeap(), 0, pbKey
);
1000 /******************************************************************************
1001 * map_key_spec_to_permissions_name [Internal]
1003 * Returns the name of the registry value associated with the permissions for
1007 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
1010 * Success: Name of registry value.
1013 static LPCSTR
map_key_spec_to_permissions_name(DWORD dwKeySpec
)
1019 case AT_KEYEXCHANGE
:
1020 szValueName
= "KeyExchangePermissions";
1023 szValueName
= "SignaturePermissions";
1026 WARN("invalid key spec %d\n", dwKeySpec
);
1032 /******************************************************************************
1033 * store_key_permissions [Internal]
1035 * Stores a key's permissions to the registry
1038 * hCryptKey [I] Handle to the key whose permissions are to be stored
1039 * hKey [I] Registry key where the key permissions are to be stored
1040 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
1042 static void store_key_permissions(HCRYPTKEY hCryptKey
, HKEY hKey
, DWORD dwKeySpec
)
1047 if (!(szValueName
= map_key_spec_to_permissions_name(dwKeySpec
)))
1049 if (lookup_handle(&handle_table
, hCryptKey
, RSAENH_MAGIC_KEY
,
1050 (OBJECTHDR
**)&pKey
))
1051 RegSetValueExA(hKey
, szValueName
, 0, REG_DWORD
,
1052 (BYTE
*)&pKey
->dwPermissions
,
1053 sizeof(pKey
->dwPermissions
));
1056 /******************************************************************************
1057 * create_container_key [Internal]
1059 * Creates the registry key for a key container's persistent storage.
1062 * pKeyContainer [I] Pointer to the key container
1063 * sam [I] Desired registry access
1064 * phKey [O] Returned key
1066 static BOOL
create_container_key(KEYCONTAINER
*pKeyContainer
, REGSAM sam
, HKEY
*phKey
)
1068 CHAR szRSABase
[MAX_PATH
];
1071 sprintf(szRSABase
, RSAENH_REGKEY
, pKeyContainer
->szName
);
1073 if (pKeyContainer
->dwFlags
& CRYPT_MACHINE_KEYSET
)
1074 hRootKey
= HKEY_LOCAL_MACHINE
;
1076 hRootKey
= HKEY_CURRENT_USER
;
1078 /* @@ Wine registry key: HKLM\Software\Wine\Crypto\RSA */
1079 /* @@ Wine registry key: HKCU\Software\Wine\Crypto\RSA */
1080 return RegCreateKeyExA(hRootKey
, szRSABase
, 0, NULL
,
1081 REG_OPTION_NON_VOLATILE
, sam
, NULL
, phKey
, NULL
)
1085 /******************************************************************************
1086 * open_container_key [Internal]
1088 * Opens a key container's persistent storage for reading.
1091 * pszContainerName [I] Name of the container to be opened. May be the empty
1092 * string if the parent key of all containers is to be
1094 * dwFlags [I] Flags indicating which keyset to be opened.
1095 * phKey [O] Returned key
1097 static BOOL
open_container_key(LPCSTR pszContainerName
, DWORD dwFlags
, HKEY
*phKey
)
1099 CHAR szRSABase
[MAX_PATH
];
1102 sprintf(szRSABase
, RSAENH_REGKEY
, pszContainerName
);
1104 if (dwFlags
& CRYPT_MACHINE_KEYSET
)
1105 hRootKey
= HKEY_LOCAL_MACHINE
;
1107 hRootKey
= HKEY_CURRENT_USER
;
1109 /* @@ Wine registry key: HKLM\Software\Wine\Crypto\RSA */
1110 /* @@ Wine registry key: HKCU\Software\Wine\Crypto\RSA */
1111 return RegOpenKeyExA(hRootKey
, szRSABase
, 0, KEY_READ
, phKey
) ==
1115 /******************************************************************************
1116 * delete_container_key [Internal]
1118 * Deletes a key container's persistent storage.
1121 * pszContainerName [I] Name of the container to be opened.
1122 * dwFlags [I] Flags indicating which keyset to be opened.
1124 static BOOL
delete_container_key(LPCSTR pszContainerName
, DWORD dwFlags
)
1126 CHAR szRegKey
[MAX_PATH
];
1128 if (snprintf(szRegKey
, MAX_PATH
, RSAENH_REGKEY
, pszContainerName
) >= MAX_PATH
) {
1129 SetLastError(NTE_BAD_KEYSET_PARAM
);
1133 if (dwFlags
& CRYPT_MACHINE_KEYSET
)
1134 hRootKey
= HKEY_LOCAL_MACHINE
;
1136 hRootKey
= HKEY_CURRENT_USER
;
1137 if (!RegDeleteKeyA(hRootKey
, szRegKey
)) {
1138 SetLastError(ERROR_SUCCESS
);
1141 SetLastError(NTE_BAD_KEYSET
);
1147 /******************************************************************************
1148 * store_key_container_keys [Internal]
1150 * Stores key container's keys in a persistent location.
1153 * pKeyContainer [I] Pointer to the key container whose keys are to be saved
1155 static void store_key_container_keys(KEYCONTAINER
*pKeyContainer
)
1160 /* On WinXP, persistent keys are stored in a file located at:
1161 * $AppData$\\Microsoft\\Crypto\\RSA\\$SID$\\some_hex_string
1164 if (pKeyContainer
->dwFlags
& CRYPT_MACHINE_KEYSET
)
1165 dwFlags
= CRYPTPROTECT_LOCAL_MACHINE
;
1169 if (create_container_key(pKeyContainer
, KEY_WRITE
, &hKey
))
1171 store_key_pair(pKeyContainer
->hKeyExchangeKeyPair
, hKey
,
1172 AT_KEYEXCHANGE
, dwFlags
);
1173 store_key_pair(pKeyContainer
->hSignatureKeyPair
, hKey
,
1174 AT_SIGNATURE
, dwFlags
);
1179 /******************************************************************************
1180 * store_key_container_permissions [Internal]
1182 * Stores key container's key permissions in a persistent location.
1185 * pKeyContainer [I] Pointer to the key container whose key permissions are to
1188 static void store_key_container_permissions(KEYCONTAINER
*pKeyContainer
)
1192 if (create_container_key(pKeyContainer
, KEY_WRITE
, &hKey
))
1194 store_key_permissions(pKeyContainer
->hKeyExchangeKeyPair
, hKey
,
1196 store_key_permissions(pKeyContainer
->hSignatureKeyPair
, hKey
,
1202 /******************************************************************************
1203 * release_key_container_keys [Internal]
1205 * Releases key container's keys.
1208 * pKeyContainer [I] Pointer to the key container whose keys are to be released.
1210 static void release_key_container_keys(KEYCONTAINER
*pKeyContainer
)
1212 release_handle(&handle_table
, pKeyContainer
->hKeyExchangeKeyPair
,
1214 release_handle(&handle_table
, pKeyContainer
->hSignatureKeyPair
,
1218 /******************************************************************************
1219 * destroy_key_container [Internal]
1221 * Destructor for key containers.
1224 * pObjectHdr [I] Pointer to the key container to be destroyed.
1226 static void destroy_key_container(OBJECTHDR
*pObjectHdr
)
1228 KEYCONTAINER
*pKeyContainer
= (KEYCONTAINER
*)pObjectHdr
;
1230 if (!(pKeyContainer
->dwFlags
& CRYPT_VERIFYCONTEXT
))
1232 store_key_container_keys(pKeyContainer
);
1233 store_key_container_permissions(pKeyContainer
);
1234 release_key_container_keys(pKeyContainer
);
1237 release_key_container_keys(pKeyContainer
);
1238 HeapFree( GetProcessHeap(), 0, pKeyContainer
);
1241 /******************************************************************************
1242 * new_key_container [Internal]
1244 * Create a new key container. The personality (RSA Base, Strong or Enhanced CP)
1245 * of the CSP is determined via the pVTable->pszProvName string.
1248 * pszContainerName [I] Name of the key container.
1249 * pVTable [I] Callback functions and context info provided by the OS
1252 * Success: Handle to the new key container.
1253 * Failure: INVALID_HANDLE_VALUE
1255 static HCRYPTPROV
new_key_container(PCCH pszContainerName
, DWORD dwFlags
, const VTableProvStruc
*pVTable
)
1257 KEYCONTAINER
*pKeyContainer
;
1258 HCRYPTPROV hKeyContainer
;
1260 hKeyContainer
= new_object(&handle_table
, sizeof(KEYCONTAINER
), RSAENH_MAGIC_CONTAINER
,
1261 destroy_key_container
, (OBJECTHDR
**)&pKeyContainer
);
1262 if (hKeyContainer
!= (HCRYPTPROV
)INVALID_HANDLE_VALUE
)
1264 lstrcpynA(pKeyContainer
->szName
, pszContainerName
, MAX_PATH
);
1265 pKeyContainer
->dwFlags
= dwFlags
;
1266 pKeyContainer
->dwEnumAlgsCtr
= 0;
1267 pKeyContainer
->hKeyExchangeKeyPair
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
1268 pKeyContainer
->hSignatureKeyPair
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
1269 if (pVTable
&& pVTable
->pszProvName
) {
1270 lstrcpynA(pKeyContainer
->szProvName
, pVTable
->pszProvName
, MAX_PATH
);
1271 if (!strcmp(pVTable
->pszProvName
, MS_DEF_PROV_A
)) {
1272 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_BASE
;
1273 } else if (!strcmp(pVTable
->pszProvName
, MS_ENHANCED_PROV_A
)) {
1274 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_ENHANCED
;
1275 } else if (!strcmp(pVTable
->pszProvName
, MS_DEF_RSA_SCHANNEL_PROV_A
)) {
1276 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_SCHANNEL
;
1277 } else if (!strcmp(pVTable
->pszProvName
, MS_ENH_RSA_AES_PROV_A
)) {
1278 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_AES
;
1280 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_STRONG
;
1284 /* The new key container has to be inserted into the CSP immediately
1285 * after creation to be available for CPGetProvParam's PP_ENUMCONTAINERS. */
1286 if (!(dwFlags
& CRYPT_VERIFYCONTEXT
)) {
1289 if (create_container_key(pKeyContainer
, KEY_WRITE
, &hKey
))
1294 return hKeyContainer
;
1297 /******************************************************************************
1298 * read_key_value [Internal]
1300 * Reads a key pair value from the registry
1303 * hKeyContainer [I] Crypt provider to use to import the key
1304 * hKey [I] Registry key from which to read the key pair
1305 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
1306 * dwFlags [I] Flags for unprotecting the key
1307 * phCryptKey [O] Returned key
1309 static BOOL
read_key_value(HCRYPTPROV hKeyContainer
, HKEY hKey
, DWORD dwKeySpec
, DWORD dwFlags
, HCRYPTKEY
*phCryptKey
)
1312 DWORD dwValueType
, dwLen
;
1314 DATA_BLOB blobIn
, blobOut
;
1317 if (!(szValueName
= map_key_spec_to_key_pair_name(dwKeySpec
)))
1319 if (RegQueryValueExA(hKey
, szValueName
, 0, &dwValueType
, NULL
, &dwLen
) ==
1322 pbKey
= HeapAlloc(GetProcessHeap(), 0, dwLen
);
1325 if (RegQueryValueExA(hKey
, szValueName
, 0, &dwValueType
, pbKey
, &dwLen
) ==
1328 blobIn
.pbData
= pbKey
;
1329 blobIn
.cbData
= dwLen
;
1331 if (CryptUnprotectData(&blobIn
, NULL
, NULL
, NULL
, NULL
,
1334 ret
= import_key(hKeyContainer
, blobOut
.pbData
, blobOut
.cbData
, 0, 0,
1336 LocalFree(blobOut
.pbData
);
1339 HeapFree(GetProcessHeap(), 0, pbKey
);
1346 if (lookup_handle(&handle_table
, *phCryptKey
, RSAENH_MAGIC_KEY
,
1347 (OBJECTHDR
**)&pKey
))
1349 if ((szValueName
= map_key_spec_to_permissions_name(dwKeySpec
)))
1351 dwLen
= sizeof(pKey
->dwPermissions
);
1352 RegQueryValueExA(hKey
, szValueName
, 0, NULL
,
1353 (BYTE
*)&pKey
->dwPermissions
, &dwLen
);
1360 /******************************************************************************
1361 * read_key_container [Internal]
1363 * Tries to read the persistent state of the key container (mainly the signature
1364 * and key exchange private keys) given by pszContainerName.
1367 * pszContainerName [I] Name of the key container to read from the registry
1368 * pVTable [I] Pointer to context data provided by the operating system
1371 * Success: Handle to the key container read from the registry
1372 * Failure: INVALID_HANDLE_VALUE
1374 static HCRYPTPROV
read_key_container(PCHAR pszContainerName
, DWORD dwFlags
, const VTableProvStruc
*pVTable
)
1377 KEYCONTAINER
*pKeyContainer
;
1378 HCRYPTPROV hKeyContainer
;
1379 HCRYPTKEY hCryptKey
;
1381 if (!open_container_key(pszContainerName
, dwFlags
, &hKey
))
1383 SetLastError(NTE_BAD_KEYSET
);
1384 return (HCRYPTPROV
)INVALID_HANDLE_VALUE
;
1387 hKeyContainer
= new_key_container(pszContainerName
, dwFlags
, pVTable
);
1388 if (hKeyContainer
!= (HCRYPTPROV
)INVALID_HANDLE_VALUE
)
1390 DWORD dwProtectFlags
= (dwFlags
& CRYPT_MACHINE_KEYSET
) ?
1391 CRYPTPROTECT_LOCAL_MACHINE
: 0;
1393 if (!lookup_handle(&handle_table
, hKeyContainer
, RSAENH_MAGIC_CONTAINER
,
1394 (OBJECTHDR
**)&pKeyContainer
))
1395 return (HCRYPTPROV
)INVALID_HANDLE_VALUE
;
1397 /* read_key_value calls import_key, which calls import_private_key,
1398 * which implicitly installs the key value into the appropriate key
1399 * container key. Thus the ref count is incremented twice, once for
1400 * the output key value, and once for the implicit install, and needs
1401 * to be decremented to balance the two.
1403 if (read_key_value(hKeyContainer
, hKey
, AT_KEYEXCHANGE
,
1404 dwProtectFlags
, &hCryptKey
))
1405 release_handle(&handle_table
, hCryptKey
, RSAENH_MAGIC_KEY
);
1406 if (read_key_value(hKeyContainer
, hKey
, AT_SIGNATURE
,
1407 dwProtectFlags
, &hCryptKey
))
1408 release_handle(&handle_table
, hCryptKey
, RSAENH_MAGIC_KEY
);
1411 return hKeyContainer
;
1414 /******************************************************************************
1415 * build_hash_signature [Internal]
1417 * Builds a padded version of a hash to match the length of the RSA key modulus.
1420 * pbSignature [O] The padded hash object is stored here.
1421 * dwLen [I] Length of the pbSignature buffer.
1422 * aiAlgid [I] Algorithm identifier of the hash to be padded.
1423 * abHashValue [I] The value of the hash object.
1424 * dwHashLen [I] Length of the hash value.
1425 * dwFlags [I] Selection of padding algorithm.
1429 * Failure: FALSE (NTE_BAD_ALGID)
1431 static BOOL
build_hash_signature(BYTE
*pbSignature
, DWORD dwLen
, ALG_ID aiAlgid
,
1432 CONST BYTE
*abHashValue
, DWORD dwHashLen
, DWORD dwFlags
)
1434 /* These prefixes are meant to be concatenated with hash values of the
1435 * respective kind to form a PKCS #7 DigestInfo. */
1436 static const struct tagOIDDescriptor
{
1439 CONST BYTE abOID
[19];
1440 } aOIDDescriptor
[] = {
1441 { CALG_MD2
, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1442 0x86, 0xf7, 0x0d, 0x02, 0x02, 0x05, 0x00, 0x04, 0x10 } },
1443 { CALG_MD4
, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1444 0x86, 0xf7, 0x0d, 0x02, 0x04, 0x05, 0x00, 0x04, 0x10 } },
1445 { CALG_MD5
, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1446 0x86, 0xf7, 0x0d, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10 } },
1447 { CALG_SHA
, 15, { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03,
1448 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14 } },
1449 { CALG_SHA_256
, 19, { 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
1450 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01,
1451 0x05, 0x00, 0x04, 0x20 } },
1452 { CALG_SHA_384
, 19, { 0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
1453 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01,
1454 0x05, 0x00, 0x04, 0x30 } },
1455 { CALG_SHA_384
, 19, { 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
1456 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01,
1457 0x05, 0x00, 0x04, 0x40 } },
1458 { CALG_SSL3_SHAMD5
, 0, { 0 } },
1461 DWORD dwIdxOID
, i
, j
;
1463 for (dwIdxOID
= 0; aOIDDescriptor
[dwIdxOID
].aiAlgid
; dwIdxOID
++) {
1464 if (aOIDDescriptor
[dwIdxOID
].aiAlgid
== aiAlgid
) break;
1467 if (!aOIDDescriptor
[dwIdxOID
].aiAlgid
) {
1468 SetLastError(NTE_BAD_ALGID
);
1472 /* Build the padded signature */
1473 if (dwFlags
& CRYPT_X931_FORMAT
) {
1474 pbSignature
[0] = 0x6b;
1475 for (i
=1; i
< dwLen
- dwHashLen
- 3; i
++) {
1476 pbSignature
[i
] = 0xbb;
1478 pbSignature
[i
++] = 0xba;
1479 for (j
=0; j
< dwHashLen
; j
++, i
++) {
1480 pbSignature
[i
] = abHashValue
[j
];
1482 pbSignature
[i
++] = 0x33;
1483 pbSignature
[i
++] = 0xcc;
1485 pbSignature
[0] = 0x00;
1486 pbSignature
[1] = 0x01;
1487 if (dwFlags
& CRYPT_NOHASHOID
) {
1488 for (i
=2; i
< dwLen
- 1 - dwHashLen
; i
++) {
1489 pbSignature
[i
] = 0xff;
1491 pbSignature
[i
++] = 0x00;
1493 for (i
=2; i
< dwLen
- 1 - aOIDDescriptor
[dwIdxOID
].dwLen
- dwHashLen
; i
++) {
1494 pbSignature
[i
] = 0xff;
1496 pbSignature
[i
++] = 0x00;
1497 for (j
=0; j
< aOIDDescriptor
[dwIdxOID
].dwLen
; j
++) {
1498 pbSignature
[i
++] = aOIDDescriptor
[dwIdxOID
].abOID
[j
];
1501 for (j
=0; j
< dwHashLen
; j
++) {
1502 pbSignature
[i
++] = abHashValue
[j
];
1509 /******************************************************************************
1512 * This is an implementation of the 'P_hash' helper function for TLS1's PRF.
1513 * It is used exclusively by tls1_prf. For details see RFC 2246, chapter 5.
1514 * The pseudo random stream generated by this function is exclusive or'ed with
1515 * the data in pbBuffer.
1518 * hHMAC [I] HMAC object, which will be used in pseudo random generation
1519 * pblobSeed [I] Seed value
1520 * pbBuffer [I/O] Pseudo random stream will be xor'ed to the provided data
1521 * dwBufferLen [I] Number of pseudo random bytes desired
1527 static BOOL
tls1_p(HCRYPTHASH hHMAC
, CONST PCRYPT_DATA_BLOB pblobSeed
, PBYTE pbBuffer
, DWORD dwBufferLen
)
1530 BYTE abAi
[RSAENH_MAX_HASH_SIZE
];
1533 if (!lookup_handle(&handle_table
, hHMAC
, RSAENH_MAGIC_HASH
, (OBJECTHDR
**)&pHMAC
)) {
1534 SetLastError(NTE_BAD_HASH
);
1538 /* compute A_1 = HMAC(seed) */
1540 update_hash(pHMAC
, pblobSeed
->pbData
, pblobSeed
->cbData
);
1541 finalize_hash(pHMAC
);
1542 memcpy(abAi
, pHMAC
->abHashValue
, pHMAC
->dwHashSize
);
1545 /* compute HMAC(A_i + seed) */
1547 update_hash(pHMAC
, abAi
, pHMAC
->dwHashSize
);
1548 update_hash(pHMAC
, pblobSeed
->pbData
, pblobSeed
->cbData
);
1549 finalize_hash(pHMAC
);
1551 /* pseudo random stream := CONCAT_{i=1..n} ( HMAC(A_i + seed) ) */
1553 if (i
>= dwBufferLen
) break;
1554 pbBuffer
[i
] ^= pHMAC
->abHashValue
[i
% pHMAC
->dwHashSize
];
1556 } while (i
% pHMAC
->dwHashSize
);
1558 /* compute A_{i+1} = HMAC(A_i) */
1560 update_hash(pHMAC
, abAi
, pHMAC
->dwHashSize
);
1561 finalize_hash(pHMAC
);
1562 memcpy(abAi
, pHMAC
->abHashValue
, pHMAC
->dwHashSize
);
1563 } while (i
< dwBufferLen
);
1568 /******************************************************************************
1569 * tls1_prf [Internal]
1571 * TLS1 pseudo random function as specified in RFC 2246, chapter 5
1574 * hProv [I] Key container used to compute the pseudo random stream
1575 * hSecret [I] Key that holds the (pre-)master secret
1576 * pblobLabel [I] Descriptive label
1577 * pblobSeed [I] Seed value
1578 * pbBuffer [O] Pseudo random numbers will be stored here
1579 * dwBufferLen [I] Number of pseudo random bytes desired
1585 static BOOL
tls1_prf(HCRYPTPROV hProv
, HCRYPTPROV hSecret
, CONST PCRYPT_DATA_BLOB pblobLabel
,
1586 CONST PCRYPT_DATA_BLOB pblobSeed
, PBYTE pbBuffer
, DWORD dwBufferLen
)
1588 HMAC_INFO hmacInfo
= { 0, NULL
, 0, NULL
, 0 };
1589 HCRYPTHASH hHMAC
= (HCRYPTHASH
)INVALID_HANDLE_VALUE
;
1590 HCRYPTKEY hHalfSecret
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
1591 CRYPTKEY
*pHalfSecret
, *pSecret
;
1592 DWORD dwHalfSecretLen
;
1593 BOOL result
= FALSE
;
1594 CRYPT_DATA_BLOB blobLabelSeed
;
1596 TRACE("(hProv=%08lx, hSecret=%08lx, pblobLabel=%p, pblobSeed=%p, pbBuffer=%p, dwBufferLen=%d)\n",
1597 hProv
, hSecret
, pblobLabel
, pblobSeed
, pbBuffer
, dwBufferLen
);
1599 if (!lookup_handle(&handle_table
, hSecret
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pSecret
)) {
1600 SetLastError(NTE_FAIL
);
1604 dwHalfSecretLen
= (pSecret
->dwKeyLen
+1)/2;
1606 /* concatenation of the label and the seed */
1607 if (!concat_data_blobs(&blobLabelSeed
, pblobLabel
, pblobSeed
)) goto exit
;
1609 /* zero out the buffer, since two random streams will be xor'ed into it. */
1610 memset(pbBuffer
, 0, dwBufferLen
);
1612 /* build a 'fake' key, to hold the secret. CALG_SSL2_MASTER is used since it provides
1613 * the biggest range of valid key lengths. */
1614 hHalfSecret
= new_key(hProv
, CALG_SSL2_MASTER
, MAKELONG(0,dwHalfSecretLen
*8), &pHalfSecret
);
1615 if (hHalfSecret
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) goto exit
;
1617 /* Derive an HMAC_MD5 hash and call the helper function. */
1618 memcpy(pHalfSecret
->abKeyValue
, pSecret
->abKeyValue
, dwHalfSecretLen
);
1619 if (!RSAENH_CPCreateHash(hProv
, CALG_HMAC
, hHalfSecret
, 0, &hHMAC
)) goto exit
;
1620 hmacInfo
.HashAlgid
= CALG_MD5
;
1621 if (!RSAENH_CPSetHashParam(hProv
, hHMAC
, HP_HMAC_INFO
, (BYTE
*)&hmacInfo
, 0)) goto exit
;
1622 if (!tls1_p(hHMAC
, &blobLabelSeed
, pbBuffer
, dwBufferLen
)) goto exit
;
1624 /* Reconfigure to HMAC_SHA hash and call helper function again. */
1625 memcpy(pHalfSecret
->abKeyValue
, pSecret
->abKeyValue
+ (pSecret
->dwKeyLen
/2), dwHalfSecretLen
);
1626 hmacInfo
.HashAlgid
= CALG_SHA
;
1627 if (!RSAENH_CPSetHashParam(hProv
, hHMAC
, HP_HMAC_INFO
, (BYTE
*)&hmacInfo
, 0)) goto exit
;
1628 if (!tls1_p(hHMAC
, &blobLabelSeed
, pbBuffer
, dwBufferLen
)) goto exit
;
1632 release_handle(&handle_table
, hHalfSecret
, RSAENH_MAGIC_KEY
);
1633 if (hHMAC
!= (HCRYPTHASH
)INVALID_HANDLE_VALUE
) RSAENH_CPDestroyHash(hProv
, hHMAC
);
1634 free_data_blob(&blobLabelSeed
);
1638 /******************************************************************************
1639 * pad_data [Internal]
1641 * Helper function for data padding according to PKCS1 #2
1644 * abData [I] The data to be padded
1645 * dwDataLen [I] Length of the data
1646 * abBuffer [O] Padded data will be stored here
1647 * dwBufferLen [I] Length of the buffer (also length of padded data)
1648 * dwFlags [I] Padding format (CRYPT_SSL2_FALLBACK)
1652 * Failure: FALSE (NTE_BAD_LEN, too much data to pad)
1654 static BOOL
pad_data(CONST BYTE
*abData
, DWORD dwDataLen
, BYTE
*abBuffer
, DWORD dwBufferLen
,
1659 /* Ensure there is enough space for PKCS1 #2 padding */
1660 if (dwDataLen
> dwBufferLen
-11) {
1661 SetLastError(NTE_BAD_LEN
);
1665 memmove(abBuffer
+ dwBufferLen
- dwDataLen
, abData
, dwDataLen
);
1668 abBuffer
[1] = RSAENH_PKC_BLOCKTYPE
;
1669 for (i
=2; i
< dwBufferLen
- dwDataLen
- 1; i
++)
1670 do gen_rand_impl(&abBuffer
[i
], 1); while (!abBuffer
[i
]);
1671 if (dwFlags
& CRYPT_SSL2_FALLBACK
)
1672 for (i
-=8; i
< dwBufferLen
- dwDataLen
- 1; i
++)
1679 /******************************************************************************
1680 * unpad_data [Internal]
1682 * Remove the PKCS1 padding from RSA decrypted data
1685 * abData [I] The padded data
1686 * dwDataLen [I] Length of the padded data
1687 * abBuffer [O] Data without padding will be stored here
1688 * dwBufferLen [I/O] I: Length of the buffer, O: Length of unpadded data
1689 * dwFlags [I] Currently none defined
1693 * Failure: FALSE, (NTE_BAD_DATA, no valid PKCS1 padding or buffer too small)
1695 static BOOL
unpad_data(CONST BYTE
*abData
, DWORD dwDataLen
, BYTE
*abBuffer
, DWORD
*dwBufferLen
,
1702 SetLastError(NTE_BAD_DATA
);
1705 for (i
=2; i
<dwDataLen
; i
++)
1709 if ((i
== dwDataLen
) || (*dwBufferLen
< dwDataLen
- i
- 1) ||
1710 (abData
[0] != 0x00) || (abData
[1] != RSAENH_PKC_BLOCKTYPE
))
1712 SetLastError(NTE_BAD_DATA
);
1716 *dwBufferLen
= dwDataLen
- i
- 1;
1717 memmove(abBuffer
, abData
+ i
+ 1, *dwBufferLen
);
1721 /******************************************************************************
1722 * CPAcquireContext (RSAENH.@)
1724 * Acquire a handle to the key container specified by pszContainer
1727 * phProv [O] Pointer to the location the acquired handle will be written to.
1728 * pszContainer [I] Name of the desired key container. See Notes
1729 * dwFlags [I] Flags. See Notes.
1730 * pVTable [I] Pointer to a PVTableProvStruct containing callbacks.
1737 * If pszContainer is NULL or points to a zero length string the user's login
1738 * name will be used as the key container name.
1740 * If the CRYPT_NEW_KEYSET flag is set in dwFlags a new keyset will be created.
1741 * If a keyset with the given name already exists, the function fails and sets
1742 * last error to NTE_EXISTS. If CRYPT_NEW_KEYSET is not set and the specified
1743 * key container does not exist, function fails and sets last error to
1746 BOOL WINAPI
RSAENH_CPAcquireContext(HCRYPTPROV
*phProv
, LPSTR pszContainer
,
1747 DWORD dwFlags
, PVTableProvStruc pVTable
)
1749 CHAR szKeyContainerName
[MAX_PATH
];
1751 TRACE("(phProv=%p, pszContainer=%s, dwFlags=%08x, pVTable=%p)\n", phProv
,
1752 debugstr_a(pszContainer
), dwFlags
, pVTable
);
1754 if (pszContainer
&& *pszContainer
)
1756 lstrcpynA(szKeyContainerName
, pszContainer
, MAX_PATH
);
1760 DWORD dwLen
= sizeof(szKeyContainerName
);
1761 if (!GetUserNameA(szKeyContainerName
, &dwLen
)) return FALSE
;
1764 switch (dwFlags
& (CRYPT_NEWKEYSET
|CRYPT_VERIFYCONTEXT
|CRYPT_DELETEKEYSET
))
1767 *phProv
= read_key_container(szKeyContainerName
, dwFlags
, pVTable
);
1770 case CRYPT_DELETEKEYSET
:
1771 return delete_container_key(szKeyContainerName
, dwFlags
);
1773 case CRYPT_NEWKEYSET
:
1774 *phProv
= read_key_container(szKeyContainerName
, dwFlags
, pVTable
);
1775 if (*phProv
!= (HCRYPTPROV
)INVALID_HANDLE_VALUE
)
1777 release_handle(&handle_table
, *phProv
, RSAENH_MAGIC_CONTAINER
);
1778 TRACE("Can't create new keyset, already exists\n");
1779 SetLastError(NTE_EXISTS
);
1782 *phProv
= new_key_container(szKeyContainerName
, dwFlags
, pVTable
);
1785 case CRYPT_VERIFYCONTEXT
|CRYPT_NEWKEYSET
:
1786 case CRYPT_VERIFYCONTEXT
:
1787 if (pszContainer
&& *pszContainer
) {
1788 TRACE("pszContainer should be empty\n");
1789 SetLastError(NTE_BAD_FLAGS
);
1792 *phProv
= new_key_container("", dwFlags
, pVTable
);
1796 *phProv
= (HCRYPTPROV
)INVALID_HANDLE_VALUE
;
1797 SetLastError(NTE_BAD_FLAGS
);
1801 if (*phProv
!= (HCRYPTPROV
)INVALID_HANDLE_VALUE
) {
1802 SetLastError(ERROR_SUCCESS
);
1809 /******************************************************************************
1810 * CPCreateHash (RSAENH.@)
1812 * CPCreateHash creates and initializes a new hash object.
1815 * hProv [I] Handle to the key container to which the new hash will belong.
1816 * Algid [I] Identifies the hash algorithm, which will be used for the hash.
1817 * hKey [I] Handle to a session key applied for keyed hashes.
1818 * dwFlags [I] Currently no flags defined. Must be zero.
1819 * phHash [O] Points to the location where a handle to the new hash will be stored.
1826 * hKey is a handle to a session key applied in keyed hashes like MAC and HMAC.
1827 * If a normal hash object is to be created (like e.g. MD2 or SHA1) hKey must be zero.
1829 BOOL WINAPI
RSAENH_CPCreateHash(HCRYPTPROV hProv
, ALG_ID Algid
, HCRYPTKEY hKey
, DWORD dwFlags
,
1832 CRYPTKEY
*pCryptKey
;
1833 CRYPTHASH
*pCryptHash
;
1834 const PROV_ENUMALGS_EX
*peaAlgidInfo
;
1836 TRACE("(hProv=%08lx, Algid=%08x, hKey=%08lx, dwFlags=%08x, phHash=%p)\n", hProv
, Algid
, hKey
,
1839 peaAlgidInfo
= get_algid_info(hProv
, Algid
);
1840 if (!peaAlgidInfo
) return FALSE
;
1844 SetLastError(NTE_BAD_FLAGS
);
1848 if (Algid
== CALG_MAC
|| Algid
== CALG_HMAC
|| Algid
== CALG_SCHANNEL_MASTER_HASH
||
1849 Algid
== CALG_TLS1PRF
)
1851 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
)) {
1852 SetLastError(NTE_BAD_KEY
);
1856 if ((Algid
== CALG_MAC
) && (GET_ALG_TYPE(pCryptKey
->aiAlgid
) != ALG_TYPE_BLOCK
)) {
1857 SetLastError(NTE_BAD_KEY
);
1861 if ((Algid
== CALG_SCHANNEL_MASTER_HASH
|| Algid
== CALG_TLS1PRF
) &&
1862 (pCryptKey
->aiAlgid
!= CALG_TLS1_MASTER
))
1864 SetLastError(NTE_BAD_KEY
);
1867 if (Algid
== CALG_SCHANNEL_MASTER_HASH
&&
1868 ((!pCryptKey
->siSChannelInfo
.blobClientRandom
.cbData
) ||
1869 (!pCryptKey
->siSChannelInfo
.blobServerRandom
.cbData
)))
1871 SetLastError(ERROR_INVALID_PARAMETER
);
1875 if ((Algid
== CALG_TLS1PRF
) && (pCryptKey
->dwState
!= RSAENH_KEYSTATE_MASTERKEY
)) {
1876 SetLastError(NTE_BAD_KEY_STATE
);
1881 *phHash
= new_object(&handle_table
, sizeof(CRYPTHASH
), RSAENH_MAGIC_HASH
,
1882 destroy_hash
, (OBJECTHDR
**)&pCryptHash
);
1883 if (!pCryptHash
) return FALSE
;
1885 pCryptHash
->aiAlgid
= Algid
;
1886 pCryptHash
->hKey
= hKey
;
1887 pCryptHash
->hProv
= hProv
;
1888 pCryptHash
->dwState
= RSAENH_HASHSTATE_HASHING
;
1889 pCryptHash
->pHMACInfo
= NULL
;
1890 pCryptHash
->dwHashSize
= peaAlgidInfo
->dwDefaultLen
>> 3;
1891 init_data_blob(&pCryptHash
->tpPRFParams
.blobLabel
);
1892 init_data_blob(&pCryptHash
->tpPRFParams
.blobSeed
);
1894 if (Algid
== CALG_SCHANNEL_MASTER_HASH
) {
1895 static const char keyex
[] = "key expansion";
1896 BYTE key_expansion
[sizeof keyex
];
1897 CRYPT_DATA_BLOB blobRandom
, blobKeyExpansion
= { 13, key_expansion
};
1899 memcpy( key_expansion
, keyex
, sizeof keyex
);
1901 if (pCryptKey
->dwState
!= RSAENH_KEYSTATE_MASTERKEY
) {
1902 static const char msec
[] = "master secret";
1903 BYTE master_secret
[sizeof msec
];
1904 CRYPT_DATA_BLOB blobLabel
= { 13, master_secret
};
1905 BYTE abKeyValue
[48];
1907 memcpy( master_secret
, msec
, sizeof msec
);
1909 /* See RFC 2246, chapter 8.1 */
1910 if (!concat_data_blobs(&blobRandom
,
1911 &pCryptKey
->siSChannelInfo
.blobClientRandom
,
1912 &pCryptKey
->siSChannelInfo
.blobServerRandom
))
1916 tls1_prf(hProv
, hKey
, &blobLabel
, &blobRandom
, abKeyValue
, 48);
1917 pCryptKey
->dwState
= RSAENH_KEYSTATE_MASTERKEY
;
1918 memcpy(pCryptKey
->abKeyValue
, abKeyValue
, 48);
1919 free_data_blob(&blobRandom
);
1922 /* See RFC 2246, chapter 6.3 */
1923 if (!concat_data_blobs(&blobRandom
,
1924 &pCryptKey
->siSChannelInfo
.blobServerRandom
,
1925 &pCryptKey
->siSChannelInfo
.blobClientRandom
))
1929 tls1_prf(hProv
, hKey
, &blobKeyExpansion
, &blobRandom
, pCryptHash
->abHashValue
,
1930 RSAENH_MAX_HASH_SIZE
);
1931 free_data_blob(&blobRandom
);
1934 return init_hash(pCryptHash
);
1937 /******************************************************************************
1938 * CPDestroyHash (RSAENH.@)
1940 * Releases the handle to a hash object. The object is destroyed if its reference
1941 * count reaches zero.
1944 * hProv [I] Handle to the key container to which the hash object belongs.
1945 * hHash [I] Handle to the hash object to be released.
1951 BOOL WINAPI
RSAENH_CPDestroyHash(HCRYPTPROV hProv
, HCRYPTHASH hHash
)
1953 TRACE("(hProv=%08lx, hHash=%08lx)\n", hProv
, hHash
);
1955 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
1957 SetLastError(NTE_BAD_UID
);
1961 if (!release_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
))
1963 SetLastError(NTE_BAD_HASH
);
1970 /******************************************************************************
1971 * CPDestroyKey (RSAENH.@)
1973 * Releases the handle to a key object. The object is destroyed if its reference
1974 * count reaches zero.
1977 * hProv [I] Handle to the key container to which the key object belongs.
1978 * hKey [I] Handle to the key object to be released.
1984 BOOL WINAPI
RSAENH_CPDestroyKey(HCRYPTPROV hProv
, HCRYPTKEY hKey
)
1986 TRACE("(hProv=%08lx, hKey=%08lx)\n", hProv
, hKey
);
1988 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
1990 SetLastError(NTE_BAD_UID
);
1994 if (!release_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
))
1996 SetLastError(NTE_BAD_KEY
);
2003 /******************************************************************************
2004 * CPDuplicateHash (RSAENH.@)
2006 * Clones a hash object including its current state.
2009 * hUID [I] Handle to the key container the hash belongs to.
2010 * hHash [I] Handle to the hash object to be cloned.
2011 * pdwReserved [I] Reserved. Must be NULL.
2012 * dwFlags [I] No flags are currently defined. Must be 0.
2013 * phHash [O] Handle to the cloned hash object.
2019 BOOL WINAPI
RSAENH_CPDuplicateHash(HCRYPTPROV hUID
, HCRYPTHASH hHash
, DWORD
*pdwReserved
,
2020 DWORD dwFlags
, HCRYPTHASH
*phHash
)
2022 CRYPTHASH
*pSrcHash
, *pDestHash
;
2024 TRACE("(hUID=%08lx, hHash=%08lx, pdwReserved=%p, dwFlags=%08x, phHash=%p)\n", hUID
, hHash
,
2025 pdwReserved
, dwFlags
, phHash
);
2027 if (!is_valid_handle(&handle_table
, hUID
, RSAENH_MAGIC_CONTAINER
))
2029 SetLastError(NTE_BAD_UID
);
2033 if (!lookup_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
, (OBJECTHDR
**)&pSrcHash
))
2035 SetLastError(NTE_BAD_HASH
);
2039 if (!phHash
|| pdwReserved
|| dwFlags
)
2041 SetLastError(ERROR_INVALID_PARAMETER
);
2045 *phHash
= new_object(&handle_table
, sizeof(CRYPTHASH
), RSAENH_MAGIC_HASH
,
2046 destroy_hash
, (OBJECTHDR
**)&pDestHash
);
2047 if (*phHash
!= (HCRYPTHASH
)INVALID_HANDLE_VALUE
)
2049 *pDestHash
= *pSrcHash
;
2050 duplicate_hash_impl(pSrcHash
->aiAlgid
, &pSrcHash
->context
, &pDestHash
->context
);
2051 copy_hmac_info(&pDestHash
->pHMACInfo
, pSrcHash
->pHMACInfo
);
2052 copy_data_blob(&pDestHash
->tpPRFParams
.blobLabel
, &pSrcHash
->tpPRFParams
.blobLabel
);
2053 copy_data_blob(&pDestHash
->tpPRFParams
.blobSeed
, &pSrcHash
->tpPRFParams
.blobSeed
);
2056 return *phHash
!= (HCRYPTHASH
)INVALID_HANDLE_VALUE
;
2059 /******************************************************************************
2060 * CPDuplicateKey (RSAENH.@)
2062 * Clones a key object including its current state.
2065 * hUID [I] Handle to the key container the hash belongs to.
2066 * hKey [I] Handle to the key object to be cloned.
2067 * pdwReserved [I] Reserved. Must be NULL.
2068 * dwFlags [I] No flags are currently defined. Must be 0.
2069 * phHash [O] Handle to the cloned key object.
2075 BOOL WINAPI
RSAENH_CPDuplicateKey(HCRYPTPROV hUID
, HCRYPTKEY hKey
, DWORD
*pdwReserved
,
2076 DWORD dwFlags
, HCRYPTKEY
*phKey
)
2078 CRYPTKEY
*pSrcKey
, *pDestKey
;
2080 TRACE("(hUID=%08lx, hKey=%08lx, pdwReserved=%p, dwFlags=%08x, phKey=%p)\n", hUID
, hKey
,
2081 pdwReserved
, dwFlags
, phKey
);
2083 if (!is_valid_handle(&handle_table
, hUID
, RSAENH_MAGIC_CONTAINER
))
2085 SetLastError(NTE_BAD_UID
);
2089 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pSrcKey
))
2091 SetLastError(NTE_BAD_KEY
);
2095 if (!phKey
|| pdwReserved
|| dwFlags
)
2097 SetLastError(ERROR_INVALID_PARAMETER
);
2101 *phKey
= new_object(&handle_table
, sizeof(CRYPTKEY
), RSAENH_MAGIC_KEY
, destroy_key
,
2102 (OBJECTHDR
**)&pDestKey
);
2103 if (*phKey
!= (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
2105 *pDestKey
= *pSrcKey
;
2106 copy_data_blob(&pDestKey
->siSChannelInfo
.blobServerRandom
,
2107 &pSrcKey
->siSChannelInfo
.blobServerRandom
);
2108 copy_data_blob(&pDestKey
->siSChannelInfo
.blobClientRandom
,
2109 &pSrcKey
->siSChannelInfo
.blobClientRandom
);
2110 duplicate_key_impl(pSrcKey
->aiAlgid
, &pSrcKey
->context
, &pDestKey
->context
);
2119 /******************************************************************************
2120 * CPEncrypt (RSAENH.@)
2125 * hProv [I] The key container hKey and hHash belong to.
2126 * hKey [I] The key used to encrypt the data.
2127 * hHash [I] An optional hash object for parallel hashing. See notes.
2128 * Final [I] Indicates if this is the last block of data to encrypt.
2129 * dwFlags [I] Currently no flags defined. Must be zero.
2130 * pbData [I/O] Pointer to the data to encrypt. Encrypted data will also be stored there.
2131 * pdwDataLen [I/O] I: Length of data to encrypt, O: Length of encrypted data.
2132 * dwBufLen [I] Size of the buffer at pbData.
2139 * If a hash object handle is provided in hHash, it will be updated with the plaintext.
2140 * This is useful for message signatures.
2142 * This function uses the standard WINAPI protocol for querying data of dynamic length.
2144 BOOL WINAPI
RSAENH_CPEncrypt(HCRYPTPROV hProv
, HCRYPTKEY hKey
, HCRYPTHASH hHash
, BOOL Final
,
2145 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
, DWORD dwBufLen
)
2147 CRYPTKEY
*pCryptKey
;
2148 BYTE
*in
, out
[RSAENH_MAX_BLOCK_SIZE
], o
[RSAENH_MAX_BLOCK_SIZE
];
2149 DWORD dwEncryptedLen
, i
, j
, k
;
2151 TRACE("(hProv=%08lx, hKey=%08lx, hHash=%08lx, Final=%d, dwFlags=%08x, pbData=%p, "
2152 "pdwDataLen=%p, dwBufLen=%d)\n", hProv
, hKey
, hHash
, Final
, dwFlags
, pbData
, pdwDataLen
,
2155 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
2157 SetLastError(NTE_BAD_UID
);
2163 SetLastError(NTE_BAD_FLAGS
);
2167 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
2169 SetLastError(NTE_BAD_KEY
);
2173 if (pCryptKey
->dwState
== RSAENH_KEYSTATE_IDLE
)
2174 pCryptKey
->dwState
= RSAENH_KEYSTATE_ENCRYPTING
;
2176 if (pCryptKey
->dwState
!= RSAENH_KEYSTATE_ENCRYPTING
)
2178 SetLastError(NTE_BAD_DATA
);
2182 if (is_valid_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
)) {
2183 if (!RSAENH_CPHashData(hProv
, hHash
, pbData
, *pdwDataLen
, 0)) return FALSE
;
2186 if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_BLOCK
) {
2187 if (!Final
&& (*pdwDataLen
% pCryptKey
->dwBlockLen
)) {
2188 SetLastError(NTE_BAD_DATA
);
2192 dwEncryptedLen
= (*pdwDataLen
/pCryptKey
->dwBlockLen
+(Final
?1:0))*pCryptKey
->dwBlockLen
;
2194 if (pbData
== NULL
) {
2195 *pdwDataLen
= dwEncryptedLen
;
2198 else if (dwEncryptedLen
> dwBufLen
) {
2199 *pdwDataLen
= dwEncryptedLen
;
2200 SetLastError(ERROR_MORE_DATA
);
2204 /* Pad final block with length bytes */
2205 for (i
=*pdwDataLen
; i
<dwEncryptedLen
; i
++) pbData
[i
] = dwEncryptedLen
- *pdwDataLen
;
2206 *pdwDataLen
= dwEncryptedLen
;
2208 for (i
=0, in
=pbData
; i
<*pdwDataLen
; i
+=pCryptKey
->dwBlockLen
, in
+=pCryptKey
->dwBlockLen
) {
2209 switch (pCryptKey
->dwMode
) {
2210 case CRYPT_MODE_ECB
:
2211 encrypt_block_impl(pCryptKey
->aiAlgid
, 0, &pCryptKey
->context
, in
, out
,
2215 case CRYPT_MODE_CBC
:
2216 for (j
=0; j
<pCryptKey
->dwBlockLen
; j
++) in
[j
] ^= pCryptKey
->abChainVector
[j
];
2217 encrypt_block_impl(pCryptKey
->aiAlgid
, 0, &pCryptKey
->context
, in
, out
,
2219 memcpy(pCryptKey
->abChainVector
, out
, pCryptKey
->dwBlockLen
);
2222 case CRYPT_MODE_CFB
:
2223 for (j
=0; j
<pCryptKey
->dwBlockLen
; j
++) {
2224 encrypt_block_impl(pCryptKey
->aiAlgid
, 0, &pCryptKey
->context
,
2225 pCryptKey
->abChainVector
, o
, RSAENH_ENCRYPT
);
2226 out
[j
] = in
[j
] ^ o
[0];
2227 for (k
=0; k
<pCryptKey
->dwBlockLen
-1; k
++)
2228 pCryptKey
->abChainVector
[k
] = pCryptKey
->abChainVector
[k
+1];
2229 pCryptKey
->abChainVector
[k
] = out
[j
];
2234 SetLastError(NTE_BAD_ALGID
);
2237 memcpy(in
, out
, pCryptKey
->dwBlockLen
);
2239 } else if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_STREAM
) {
2240 if (pbData
== NULL
) {
2241 *pdwDataLen
= dwBufLen
;
2244 encrypt_stream_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pbData
, *pdwDataLen
);
2245 } else if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_RSA
) {
2246 if (pCryptKey
->aiAlgid
== CALG_RSA_SIGN
) {
2247 SetLastError(NTE_BAD_KEY
);
2251 *pdwDataLen
= pCryptKey
->dwBlockLen
;
2254 if (dwBufLen
< pCryptKey
->dwBlockLen
) {
2255 SetLastError(ERROR_MORE_DATA
);
2258 if (!pad_data(pbData
, *pdwDataLen
, pbData
, pCryptKey
->dwBlockLen
, dwFlags
)) return FALSE
;
2259 encrypt_block_impl(pCryptKey
->aiAlgid
, PK_PUBLIC
, &pCryptKey
->context
, pbData
, pbData
, RSAENH_ENCRYPT
);
2260 *pdwDataLen
= pCryptKey
->dwBlockLen
;
2263 SetLastError(NTE_BAD_TYPE
);
2267 if (Final
) setup_key(pCryptKey
);
2272 /******************************************************************************
2273 * CPDecrypt (RSAENH.@)
2278 * hProv [I] The key container hKey and hHash belong to.
2279 * hKey [I] The key used to decrypt the data.
2280 * hHash [I] An optional hash object for parallel hashing. See notes.
2281 * Final [I] Indicates if this is the last block of data to decrypt.
2282 * dwFlags [I] Currently no flags defined. Must be zero.
2283 * pbData [I/O] Pointer to the data to decrypt. Plaintext will also be stored there.
2284 * pdwDataLen [I/O] I: Length of ciphertext, O: Length of plaintext.
2291 * If a hash object handle is provided in hHash, it will be updated with the plaintext.
2292 * This is useful for message signatures.
2294 * This function uses the standard WINAPI protocol for querying data of dynamic length.
2296 BOOL WINAPI
RSAENH_CPDecrypt(HCRYPTPROV hProv
, HCRYPTKEY hKey
, HCRYPTHASH hHash
, BOOL Final
,
2297 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
)
2299 CRYPTKEY
*pCryptKey
;
2300 BYTE
*in
, out
[RSAENH_MAX_BLOCK_SIZE
], o
[RSAENH_MAX_BLOCK_SIZE
];
2304 TRACE("(hProv=%08lx, hKey=%08lx, hHash=%08lx, Final=%d, dwFlags=%08x, pbData=%p, "
2305 "pdwDataLen=%p)\n", hProv
, hKey
, hHash
, Final
, dwFlags
, pbData
, pdwDataLen
);
2307 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
2309 SetLastError(NTE_BAD_UID
);
2315 SetLastError(NTE_BAD_FLAGS
);
2319 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
2321 SetLastError(NTE_BAD_KEY
);
2325 if (pCryptKey
->dwState
== RSAENH_KEYSTATE_IDLE
)
2326 pCryptKey
->dwState
= RSAENH_KEYSTATE_ENCRYPTING
;
2328 if (pCryptKey
->dwState
!= RSAENH_KEYSTATE_ENCRYPTING
)
2330 SetLastError(NTE_BAD_DATA
);
2336 if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_BLOCK
) {
2337 for (i
=0, in
=pbData
; i
<*pdwDataLen
; i
+=pCryptKey
->dwBlockLen
, in
+=pCryptKey
->dwBlockLen
) {
2338 switch (pCryptKey
->dwMode
) {
2339 case CRYPT_MODE_ECB
:
2340 encrypt_block_impl(pCryptKey
->aiAlgid
, 0, &pCryptKey
->context
, in
, out
,
2344 case CRYPT_MODE_CBC
:
2345 encrypt_block_impl(pCryptKey
->aiAlgid
, 0, &pCryptKey
->context
, in
, out
,
2347 for (j
=0; j
<pCryptKey
->dwBlockLen
; j
++) out
[j
] ^= pCryptKey
->abChainVector
[j
];
2348 memcpy(pCryptKey
->abChainVector
, in
, pCryptKey
->dwBlockLen
);
2351 case CRYPT_MODE_CFB
:
2352 for (j
=0; j
<pCryptKey
->dwBlockLen
; j
++) {
2353 encrypt_block_impl(pCryptKey
->aiAlgid
, 0, &pCryptKey
->context
,
2354 pCryptKey
->abChainVector
, o
, RSAENH_ENCRYPT
);
2355 out
[j
] = in
[j
] ^ o
[0];
2356 for (k
=0; k
<pCryptKey
->dwBlockLen
-1; k
++)
2357 pCryptKey
->abChainVector
[k
] = pCryptKey
->abChainVector
[k
+1];
2358 pCryptKey
->abChainVector
[k
] = in
[j
];
2363 SetLastError(NTE_BAD_ALGID
);
2366 memcpy(in
, out
, pCryptKey
->dwBlockLen
);
2369 if (pbData
[*pdwDataLen
-1] &&
2370 pbData
[*pdwDataLen
-1] <= pCryptKey
->dwBlockLen
&&
2371 pbData
[*pdwDataLen
-1] <= *pdwDataLen
) {
2372 BOOL padOkay
= TRUE
;
2374 /* check that every bad byte has the same value */
2375 for (i
= 1; padOkay
&& i
< pbData
[*pdwDataLen
-1]; i
++)
2376 if (pbData
[*pdwDataLen
- i
- 1] != pbData
[*pdwDataLen
- 1])
2379 *pdwDataLen
-= pbData
[*pdwDataLen
-1];
2381 SetLastError(NTE_BAD_DATA
);
2386 SetLastError(NTE_BAD_DATA
);
2391 } else if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_STREAM
) {
2392 encrypt_stream_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pbData
, *pdwDataLen
);
2393 } else if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_RSA
) {
2394 if (pCryptKey
->aiAlgid
== CALG_RSA_SIGN
) {
2395 SetLastError(NTE_BAD_KEY
);
2398 encrypt_block_impl(pCryptKey
->aiAlgid
, PK_PRIVATE
, &pCryptKey
->context
, pbData
, pbData
, RSAENH_DECRYPT
);
2399 if (!unpad_data(pbData
, pCryptKey
->dwBlockLen
, pbData
, pdwDataLen
, dwFlags
)) return FALSE
;
2402 SetLastError(NTE_BAD_TYPE
);
2406 if (Final
) setup_key(pCryptKey
);
2408 if (is_valid_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
)) {
2409 if (*pdwDataLen
>dwMax
||
2410 !RSAENH_CPHashData(hProv
, hHash
, pbData
, *pdwDataLen
, 0)) return FALSE
;
2416 static BOOL
crypt_export_simple(CRYPTKEY
*pCryptKey
, CRYPTKEY
*pPubKey
,
2417 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
)
2419 BLOBHEADER
*pBlobHeader
= (BLOBHEADER
*)pbData
;
2420 ALG_ID
*pAlgid
= (ALG_ID
*)(pBlobHeader
+1);
2423 if (!(GET_ALG_CLASS(pCryptKey
->aiAlgid
)&(ALG_CLASS_DATA_ENCRYPT
|ALG_CLASS_MSG_ENCRYPT
))) {
2424 SetLastError(NTE_BAD_KEY
); /* FIXME: error code? */
2428 dwDataLen
= sizeof(BLOBHEADER
) + sizeof(ALG_ID
) + pPubKey
->dwBlockLen
;
2430 if (*pdwDataLen
< dwDataLen
) {
2431 SetLastError(ERROR_MORE_DATA
);
2432 *pdwDataLen
= dwDataLen
;
2436 pBlobHeader
->bType
= SIMPLEBLOB
;
2437 pBlobHeader
->bVersion
= CUR_BLOB_VERSION
;
2438 pBlobHeader
->reserved
= 0;
2439 pBlobHeader
->aiKeyAlg
= pCryptKey
->aiAlgid
;
2441 *pAlgid
= pPubKey
->aiAlgid
;
2443 if (!pad_data(pCryptKey
->abKeyValue
, pCryptKey
->dwKeyLen
, (BYTE
*)(pAlgid
+1),
2444 pPubKey
->dwBlockLen
, dwFlags
))
2449 encrypt_block_impl(pPubKey
->aiAlgid
, PK_PUBLIC
, &pPubKey
->context
, (BYTE
*)(pAlgid
+1),
2450 (BYTE
*)(pAlgid
+1), RSAENH_ENCRYPT
);
2452 *pdwDataLen
= dwDataLen
;
2456 static BOOL
crypt_export_public_key(CRYPTKEY
*pCryptKey
, BYTE
*pbData
,
2459 BLOBHEADER
*pBlobHeader
= (BLOBHEADER
*)pbData
;
2460 RSAPUBKEY
*pRSAPubKey
= (RSAPUBKEY
*)(pBlobHeader
+1);
2463 if ((pCryptKey
->aiAlgid
!= CALG_RSA_KEYX
) && (pCryptKey
->aiAlgid
!= CALG_RSA_SIGN
)) {
2464 SetLastError(NTE_BAD_KEY
);
2468 dwDataLen
= sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) + pCryptKey
->dwKeyLen
;
2470 if (*pdwDataLen
< dwDataLen
) {
2471 SetLastError(ERROR_MORE_DATA
);
2472 *pdwDataLen
= dwDataLen
;
2476 pBlobHeader
->bType
= PUBLICKEYBLOB
;
2477 pBlobHeader
->bVersion
= CUR_BLOB_VERSION
;
2478 pBlobHeader
->reserved
= 0;
2479 pBlobHeader
->aiKeyAlg
= pCryptKey
->aiAlgid
;
2481 pRSAPubKey
->magic
= RSAENH_MAGIC_RSA1
;
2482 pRSAPubKey
->bitlen
= pCryptKey
->dwKeyLen
<< 3;
2484 export_public_key_impl((BYTE
*)(pRSAPubKey
+1), &pCryptKey
->context
,
2485 pCryptKey
->dwKeyLen
, &pRSAPubKey
->pubexp
);
2487 *pdwDataLen
= dwDataLen
;
2491 static BOOL
crypt_export_private_key(CRYPTKEY
*pCryptKey
, BOOL force
,
2492 BYTE
*pbData
, DWORD
*pdwDataLen
)
2494 BLOBHEADER
*pBlobHeader
= (BLOBHEADER
*)pbData
;
2495 RSAPUBKEY
*pRSAPubKey
= (RSAPUBKEY
*)(pBlobHeader
+1);
2498 if ((pCryptKey
->aiAlgid
!= CALG_RSA_KEYX
) && (pCryptKey
->aiAlgid
!= CALG_RSA_SIGN
)) {
2499 SetLastError(NTE_BAD_KEY
);
2502 if (!force
&& !(pCryptKey
->dwPermissions
& CRYPT_EXPORT
))
2504 SetLastError(NTE_BAD_KEY_STATE
);
2508 dwDataLen
= sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) +
2509 2 * pCryptKey
->dwKeyLen
+ 5 * ((pCryptKey
->dwKeyLen
+ 1) >> 1);
2511 if (*pdwDataLen
< dwDataLen
) {
2512 SetLastError(ERROR_MORE_DATA
);
2513 *pdwDataLen
= dwDataLen
;
2517 pBlobHeader
->bType
= PRIVATEKEYBLOB
;
2518 pBlobHeader
->bVersion
= CUR_BLOB_VERSION
;
2519 pBlobHeader
->reserved
= 0;
2520 pBlobHeader
->aiKeyAlg
= pCryptKey
->aiAlgid
;
2522 pRSAPubKey
->magic
= RSAENH_MAGIC_RSA2
;
2523 pRSAPubKey
->bitlen
= pCryptKey
->dwKeyLen
<< 3;
2525 export_private_key_impl((BYTE
*)(pRSAPubKey
+1), &pCryptKey
->context
,
2526 pCryptKey
->dwKeyLen
, &pRSAPubKey
->pubexp
);
2528 *pdwDataLen
= dwDataLen
;
2532 static BOOL
crypt_export_plaintext_key(CRYPTKEY
*pCryptKey
, BYTE
*pbData
,
2535 BLOBHEADER
*pBlobHeader
= (BLOBHEADER
*)pbData
;
2536 DWORD
*pKeyLen
= (DWORD
*)(pBlobHeader
+1);
2537 BYTE
*pbKey
= (BYTE
*)(pKeyLen
+1);
2540 dwDataLen
= sizeof(BLOBHEADER
) + sizeof(DWORD
) + pCryptKey
->dwKeyLen
;
2542 if (*pdwDataLen
< dwDataLen
) {
2543 SetLastError(ERROR_MORE_DATA
);
2544 *pdwDataLen
= dwDataLen
;
2548 pBlobHeader
->bType
= PLAINTEXTKEYBLOB
;
2549 pBlobHeader
->bVersion
= CUR_BLOB_VERSION
;
2550 pBlobHeader
->reserved
= 0;
2551 pBlobHeader
->aiKeyAlg
= pCryptKey
->aiAlgid
;
2553 *pKeyLen
= pCryptKey
->dwKeyLen
;
2554 memcpy(pbKey
, pCryptKey
->abKeyValue
, pCryptKey
->dwKeyLen
);
2556 *pdwDataLen
= dwDataLen
;
2559 /******************************************************************************
2560 * crypt_export_key [Internal]
2562 * Export a key into a binary large object (BLOB). Called by CPExportKey and
2563 * by store_key_pair.
2566 * pCryptKey [I] Key to be exported.
2567 * hPubKey [I] Key used to encrypt sensitive BLOB data.
2568 * dwBlobType [I] SIMPLEBLOB, PUBLICKEYBLOB or PRIVATEKEYBLOB.
2569 * dwFlags [I] Currently none defined.
2570 * force [I] If TRUE, the key is written no matter what the key's
2571 * permissions are. Otherwise the key's permissions are
2572 * checked before exporting.
2573 * pbData [O] Pointer to a buffer where the BLOB will be written to.
2574 * pdwDataLen [I/O] I: Size of buffer at pbData, O: Size of BLOB
2580 static BOOL
crypt_export_key(CRYPTKEY
*pCryptKey
, HCRYPTKEY hPubKey
,
2581 DWORD dwBlobType
, DWORD dwFlags
, BOOL force
,
2582 BYTE
*pbData
, DWORD
*pdwDataLen
)
2586 if (dwFlags
& CRYPT_SSL2_FALLBACK
) {
2587 if (pCryptKey
->aiAlgid
!= CALG_SSL2_MASTER
) {
2588 SetLastError(NTE_BAD_KEY
);
2593 switch ((BYTE
)dwBlobType
)
2596 if (!lookup_handle(&handle_table
, hPubKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pPubKey
)){
2597 SetLastError(NTE_BAD_PUBLIC_KEY
); /* FIXME: error_code? */
2600 return crypt_export_simple(pCryptKey
, pPubKey
, dwFlags
, pbData
,
2604 if (is_valid_handle(&handle_table
, hPubKey
, RSAENH_MAGIC_KEY
)) {
2605 SetLastError(NTE_BAD_KEY
); /* FIXME: error code? */
2609 return crypt_export_public_key(pCryptKey
, pbData
, pdwDataLen
);
2611 case PRIVATEKEYBLOB
:
2612 return crypt_export_private_key(pCryptKey
, force
, pbData
, pdwDataLen
);
2614 case PLAINTEXTKEYBLOB
:
2615 return crypt_export_plaintext_key(pCryptKey
, pbData
, pdwDataLen
);
2618 SetLastError(NTE_BAD_TYPE
); /* FIXME: error code? */
2623 /******************************************************************************
2624 * CPExportKey (RSAENH.@)
2626 * Export a key into a binary large object (BLOB).
2629 * hProv [I] Key container from which a key is to be exported.
2630 * hKey [I] Key to be exported.
2631 * hPubKey [I] Key used to encrypt sensitive BLOB data.
2632 * dwBlobType [I] SIMPLEBLOB, PUBLICKEYBLOB or PRIVATEKEYBLOB.
2633 * dwFlags [I] Currently none defined.
2634 * pbData [O] Pointer to a buffer where the BLOB will be written to.
2635 * pdwDataLen [I/O] I: Size of buffer at pbData, O: Size of BLOB
2641 BOOL WINAPI
RSAENH_CPExportKey(HCRYPTPROV hProv
, HCRYPTKEY hKey
, HCRYPTKEY hPubKey
,
2642 DWORD dwBlobType
, DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
)
2644 CRYPTKEY
*pCryptKey
;
2646 TRACE("(hProv=%08lx, hKey=%08lx, hPubKey=%08lx, dwBlobType=%08x, dwFlags=%08x, pbData=%p,"
2647 "pdwDataLen=%p)\n", hProv
, hKey
, hPubKey
, dwBlobType
, dwFlags
, pbData
, pdwDataLen
);
2649 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
2651 SetLastError(NTE_BAD_UID
);
2655 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
2657 SetLastError(NTE_BAD_KEY
);
2661 return crypt_export_key(pCryptKey
, hPubKey
, dwBlobType
, dwFlags
, FALSE
,
2662 pbData
, pdwDataLen
);
2665 /******************************************************************************
2666 * release_and_install_key [Internal]
2668 * Release an existing key, if present, and replaces it with a new one.
2671 * hProv [I] Key container into which the key is to be imported.
2672 * src [I] Key which will replace *dest
2673 * dest [I] Points to key to be released and replaced with src
2674 * fStoreKey [I] If TRUE, the newly installed key is stored to the registry.
2676 static void release_and_install_key(HCRYPTPROV hProv
, HCRYPTKEY src
,
2677 HCRYPTKEY
*dest
, DWORD fStoreKey
)
2679 RSAENH_CPDestroyKey(hProv
, *dest
);
2680 copy_handle(&handle_table
, src
, RSAENH_MAGIC_KEY
, dest
);
2683 KEYCONTAINER
*pKeyContainer
;
2685 if (lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
,
2686 (OBJECTHDR
**)&pKeyContainer
))
2688 store_key_container_keys(pKeyContainer
);
2689 store_key_container_permissions(pKeyContainer
);
2694 /******************************************************************************
2695 * import_private_key [Internal]
2697 * Import a BLOB'ed private key into a key container.
2700 * hProv [I] Key container into which the private key is to be imported.
2701 * pbData [I] Pointer to a buffer which holds the private key BLOB.
2702 * dwDataLen [I] Length of data in buffer at pbData.
2703 * dwFlags [I] One of:
2704 * CRYPT_EXPORTABLE: the imported key is marked exportable
2705 * fStoreKey [I] If TRUE, the imported key is stored to the registry.
2706 * phKey [O] Handle to the imported key.
2710 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2711 * it's a PRIVATEKEYBLOB.
2717 static BOOL
import_private_key(HCRYPTPROV hProv
, CONST BYTE
*pbData
, DWORD dwDataLen
,
2718 DWORD dwFlags
, BOOL fStoreKey
, HCRYPTKEY
*phKey
)
2720 KEYCONTAINER
*pKeyContainer
;
2721 CRYPTKEY
*pCryptKey
;
2722 CONST BLOBHEADER
*pBlobHeader
= (CONST BLOBHEADER
*)pbData
;
2723 CONST RSAPUBKEY
*pRSAPubKey
= (CONST RSAPUBKEY
*)(pBlobHeader
+1);
2726 if (dwFlags
& CRYPT_IPSEC_HMAC_KEY
)
2728 FIXME("unimplemented for CRYPT_IPSEC_HMAC_KEY\n");
2729 SetLastError(NTE_BAD_FLAGS
);
2732 if (!lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
,
2733 (OBJECTHDR
**)&pKeyContainer
))
2735 SetLastError(NTE_BAD_UID
);
2739 if ((dwDataLen
< sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
)))
2741 ERR("datalen %d not long enough for a BLOBHEADER + RSAPUBKEY\n",
2743 SetLastError(NTE_BAD_DATA
);
2746 if (pRSAPubKey
->magic
!= RSAENH_MAGIC_RSA2
)
2748 ERR("unexpected magic %08x\n", pRSAPubKey
->magic
);
2749 SetLastError(NTE_BAD_DATA
);
2752 if ((dwDataLen
< sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) +
2753 (pRSAPubKey
->bitlen
>> 3) + (5 * ((pRSAPubKey
->bitlen
+8)>>4))))
2755 DWORD expectedLen
= sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) +
2756 (pRSAPubKey
->bitlen
>> 3) + (5 * ((pRSAPubKey
->bitlen
+8)>>4));
2758 ERR("blob too short for pub key: expect %d, got %d\n",
2759 expectedLen
, dwDataLen
);
2760 SetLastError(NTE_BAD_DATA
);
2764 *phKey
= new_key(hProv
, pBlobHeader
->aiKeyAlg
, MAKELONG(0,pRSAPubKey
->bitlen
), &pCryptKey
);
2765 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
2766 setup_key(pCryptKey
);
2767 ret
= import_private_key_impl((CONST BYTE
*)(pRSAPubKey
+1), &pCryptKey
->context
,
2768 pRSAPubKey
->bitlen
/8, dwDataLen
, pRSAPubKey
->pubexp
);
2770 if (dwFlags
& CRYPT_EXPORTABLE
)
2771 pCryptKey
->dwPermissions
|= CRYPT_EXPORT
;
2772 switch (pBlobHeader
->aiKeyAlg
)
2776 TRACE("installing signing key\n");
2777 release_and_install_key(hProv
, *phKey
, &pKeyContainer
->hSignatureKeyPair
,
2780 case AT_KEYEXCHANGE
:
2782 TRACE("installing key exchange key\n");
2783 release_and_install_key(hProv
, *phKey
, &pKeyContainer
->hKeyExchangeKeyPair
,
2791 /******************************************************************************
2792 * import_public_key [Internal]
2794 * Import a BLOB'ed public key into a key container.
2797 * hProv [I] Key container into which the public key is to be imported.
2798 * pbData [I] Pointer to a buffer which holds the public key BLOB.
2799 * dwDataLen [I] Length of data in buffer at pbData.
2800 * dwFlags [I] One of:
2801 * CRYPT_EXPORTABLE: the imported key is marked exportable
2802 * fStoreKey [I] If TRUE, the imported key is stored to the registry.
2803 * phKey [O] Handle to the imported key.
2807 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2808 * it's a PUBLICKEYBLOB.
2814 static BOOL
import_public_key(HCRYPTPROV hProv
, CONST BYTE
*pbData
, DWORD dwDataLen
,
2815 DWORD dwFlags
, BOOL fStoreKey
, HCRYPTKEY
*phKey
)
2817 KEYCONTAINER
*pKeyContainer
;
2818 CRYPTKEY
*pCryptKey
;
2819 CONST BLOBHEADER
*pBlobHeader
= (CONST BLOBHEADER
*)pbData
;
2820 CONST RSAPUBKEY
*pRSAPubKey
= (CONST RSAPUBKEY
*)(pBlobHeader
+1);
2824 if (dwFlags
& CRYPT_IPSEC_HMAC_KEY
)
2826 FIXME("unimplemented for CRYPT_IPSEC_HMAC_KEY\n");
2827 SetLastError(NTE_BAD_FLAGS
);
2830 if (!lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
,
2831 (OBJECTHDR
**)&pKeyContainer
))
2833 SetLastError(NTE_BAD_UID
);
2837 if ((dwDataLen
< sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
)) ||
2838 (pRSAPubKey
->magic
!= RSAENH_MAGIC_RSA1
) ||
2839 (dwDataLen
< sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) + (pRSAPubKey
->bitlen
>> 3)))
2841 SetLastError(NTE_BAD_DATA
);
2845 /* Since this is a public key blob, only the public key is
2846 * available, so only signature verification is possible.
2848 algID
= pBlobHeader
->aiKeyAlg
;
2849 *phKey
= new_key(hProv
, algID
, MAKELONG(0,pRSAPubKey
->bitlen
), &pCryptKey
);
2850 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
2851 setup_key(pCryptKey
);
2852 ret
= import_public_key_impl((CONST BYTE
*)(pRSAPubKey
+1), &pCryptKey
->context
,
2853 pRSAPubKey
->bitlen
>> 3, pRSAPubKey
->pubexp
);
2855 if (dwFlags
& CRYPT_EXPORTABLE
)
2856 pCryptKey
->dwPermissions
|= CRYPT_EXPORT
;
2857 switch (pBlobHeader
->aiKeyAlg
)
2859 case AT_KEYEXCHANGE
:
2861 TRACE("installing public key\n");
2862 release_and_install_key(hProv
, *phKey
, &pKeyContainer
->hKeyExchangeKeyPair
,
2870 /******************************************************************************
2871 * import_symmetric_key [Internal]
2873 * Import a BLOB'ed symmetric key into a key container.
2876 * hProv [I] Key container into which the symmetric key is to be imported.
2877 * pbData [I] Pointer to a buffer which holds the symmetric key BLOB.
2878 * dwDataLen [I] Length of data in buffer at pbData.
2879 * hPubKey [I] Key used to decrypt sensitive BLOB data.
2880 * dwFlags [I] One of:
2881 * CRYPT_EXPORTABLE: the imported key is marked exportable
2882 * phKey [O] Handle to the imported key.
2886 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2887 * it's a SIMPLEBLOB.
2893 static BOOL
import_symmetric_key(HCRYPTPROV hProv
, CONST BYTE
*pbData
,
2894 DWORD dwDataLen
, HCRYPTKEY hPubKey
,
2895 DWORD dwFlags
, HCRYPTKEY
*phKey
)
2897 CRYPTKEY
*pCryptKey
, *pPubKey
;
2898 CONST BLOBHEADER
*pBlobHeader
= (CONST BLOBHEADER
*)pbData
;
2899 CONST ALG_ID
*pAlgid
= (CONST ALG_ID
*)(pBlobHeader
+1);
2900 CONST BYTE
*pbKeyStream
= (CONST BYTE
*)(pAlgid
+ 1);
2904 if (dwFlags
& CRYPT_IPSEC_HMAC_KEY
)
2906 FIXME("unimplemented for CRYPT_IPSEC_HMAC_KEY\n");
2907 SetLastError(NTE_BAD_FLAGS
);
2910 if (!lookup_handle(&handle_table
, hPubKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pPubKey
) ||
2911 pPubKey
->aiAlgid
!= CALG_RSA_KEYX
)
2913 SetLastError(NTE_BAD_PUBLIC_KEY
); /* FIXME: error code? */
2917 if (dwDataLen
< sizeof(BLOBHEADER
)+sizeof(ALG_ID
)+pPubKey
->dwBlockLen
)
2919 SetLastError(NTE_BAD_DATA
); /* FIXME: error code */
2923 pbDecrypted
= HeapAlloc(GetProcessHeap(), 0, pPubKey
->dwBlockLen
);
2924 if (!pbDecrypted
) return FALSE
;
2925 encrypt_block_impl(pPubKey
->aiAlgid
, PK_PRIVATE
, &pPubKey
->context
, pbKeyStream
, pbDecrypted
,
2928 dwKeyLen
= RSAENH_MAX_KEY_SIZE
;
2929 if (!unpad_data(pbDecrypted
, pPubKey
->dwBlockLen
, pbDecrypted
, &dwKeyLen
, dwFlags
)) {
2930 HeapFree(GetProcessHeap(), 0, pbDecrypted
);
2934 *phKey
= new_key(hProv
, pBlobHeader
->aiKeyAlg
, dwKeyLen
<<19, &pCryptKey
);
2935 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
2937 HeapFree(GetProcessHeap(), 0, pbDecrypted
);
2940 memcpy(pCryptKey
->abKeyValue
, pbDecrypted
, dwKeyLen
);
2941 HeapFree(GetProcessHeap(), 0, pbDecrypted
);
2942 setup_key(pCryptKey
);
2943 if (dwFlags
& CRYPT_EXPORTABLE
)
2944 pCryptKey
->dwPermissions
|= CRYPT_EXPORT
;
2948 /******************************************************************************
2949 * import_plaintext_key [Internal]
2951 * Import a plaintext key into a key container.
2954 * hProv [I] Key container into which the symmetric key is to be imported.
2955 * pbData [I] Pointer to a buffer which holds the plaintext key BLOB.
2956 * dwDataLen [I] Length of data in buffer at pbData.
2957 * dwFlags [I] One of:
2958 * CRYPT_EXPORTABLE: the imported key is marked exportable
2959 * phKey [O] Handle to the imported key.
2963 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2964 * it's a PLAINTEXTKEYBLOB.
2970 static BOOL
import_plaintext_key(HCRYPTPROV hProv
, CONST BYTE
*pbData
,
2971 DWORD dwDataLen
, DWORD dwFlags
,
2974 CRYPTKEY
*pCryptKey
;
2975 CONST BLOBHEADER
*pBlobHeader
= (CONST BLOBHEADER
*)pbData
;
2976 CONST DWORD
*pKeyLen
= (CONST DWORD
*)(pBlobHeader
+ 1);
2977 CONST BYTE
*pbKeyStream
= (CONST BYTE
*)(pKeyLen
+ 1);
2979 if (dwDataLen
< sizeof(BLOBHEADER
)+sizeof(DWORD
)+*pKeyLen
)
2981 SetLastError(NTE_BAD_DATA
); /* FIXME: error code */
2985 if (dwFlags
& CRYPT_IPSEC_HMAC_KEY
)
2987 *phKey
= new_key(hProv
, CALG_HMAC
, 0, &pCryptKey
);
2988 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
2990 if (*pKeyLen
<= RSAENH_MIN(sizeof(pCryptKey
->abKeyValue
), RSAENH_HMAC_BLOCK_LEN
))
2992 memcpy(pCryptKey
->abKeyValue
, pbKeyStream
, *pKeyLen
);
2993 pCryptKey
->dwKeyLen
= *pKeyLen
;
2997 CRYPT_DATA_BLOB blobHmacKey
= { *pKeyLen
, (BYTE
*)pbKeyStream
};
2999 /* In order to initialize an HMAC key, the key material is hashed,
3000 * and the output of the hash function is used as the key material.
3001 * Unfortunately, the way the Crypto API is designed, we don't know
3002 * the hash algorithm yet, so we have to copy the entire key
3005 if (!copy_data_blob(&pCryptKey
->blobHmacKey
, &blobHmacKey
))
3007 release_handle(&handle_table
, *phKey
, RSAENH_MAGIC_KEY
);
3008 *phKey
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
3012 setup_key(pCryptKey
);
3013 if (dwFlags
& CRYPT_EXPORTABLE
)
3014 pCryptKey
->dwPermissions
|= CRYPT_EXPORT
;
3018 *phKey
= new_key(hProv
, pBlobHeader
->aiKeyAlg
, *pKeyLen
<<19, &pCryptKey
);
3019 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
3021 memcpy(pCryptKey
->abKeyValue
, pbKeyStream
, *pKeyLen
);
3022 setup_key(pCryptKey
);
3023 if (dwFlags
& CRYPT_EXPORTABLE
)
3024 pCryptKey
->dwPermissions
|= CRYPT_EXPORT
;
3029 /******************************************************************************
3030 * import_key [Internal]
3032 * Import a BLOB'ed key into a key container, optionally storing the key's
3033 * value to the registry.
3036 * hProv [I] Key container into which the key is to be imported.
3037 * pbData [I] Pointer to a buffer which holds the BLOB.
3038 * dwDataLen [I] Length of data in buffer at pbData.
3039 * hPubKey [I] Key used to decrypt sensitive BLOB data.
3040 * dwFlags [I] One of:
3041 * CRYPT_EXPORTABLE: the imported key is marked exportable
3042 * fStoreKey [I] If TRUE, the imported key is stored to the registry.
3043 * phKey [O] Handle to the imported key.
3049 static BOOL
import_key(HCRYPTPROV hProv
, CONST BYTE
*pbData
, DWORD dwDataLen
,
3050 HCRYPTKEY hPubKey
, DWORD dwFlags
, BOOL fStoreKey
,
3053 KEYCONTAINER
*pKeyContainer
;
3054 CONST BLOBHEADER
*pBlobHeader
= (CONST BLOBHEADER
*)pbData
;
3056 if (!lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
,
3057 (OBJECTHDR
**)&pKeyContainer
))
3059 SetLastError(NTE_BAD_UID
);
3063 if (dwDataLen
< sizeof(BLOBHEADER
) ||
3064 pBlobHeader
->bVersion
!= CUR_BLOB_VERSION
||
3065 pBlobHeader
->reserved
!= 0)
3067 TRACE("bVersion = %d, reserved = %d\n", pBlobHeader
->bVersion
,
3068 pBlobHeader
->reserved
);
3069 SetLastError(NTE_BAD_DATA
);
3073 /* If this is a verify-only context, the key is not persisted regardless of
3074 * fStoreKey's original value.
3076 fStoreKey
= fStoreKey
&& !(dwFlags
& CRYPT_VERIFYCONTEXT
);
3077 TRACE("blob type: %x\n", pBlobHeader
->bType
);
3078 switch (pBlobHeader
->bType
)
3080 case PRIVATEKEYBLOB
:
3081 return import_private_key(hProv
, pbData
, dwDataLen
, dwFlags
,
3085 return import_public_key(hProv
, pbData
, dwDataLen
, dwFlags
,
3089 return import_symmetric_key(hProv
, pbData
, dwDataLen
, hPubKey
,
3092 case PLAINTEXTKEYBLOB
:
3093 return import_plaintext_key(hProv
, pbData
, dwDataLen
, dwFlags
,
3097 SetLastError(NTE_BAD_TYPE
); /* FIXME: error code? */
3102 /******************************************************************************
3103 * CPImportKey (RSAENH.@)
3105 * Import a BLOB'ed key into a key container.
3108 * hProv [I] Key container into which the key is to be imported.
3109 * pbData [I] Pointer to a buffer which holds the BLOB.
3110 * dwDataLen [I] Length of data in buffer at pbData.
3111 * hPubKey [I] Key used to decrypt sensitive BLOB data.
3112 * dwFlags [I] One of:
3113 * CRYPT_EXPORTABLE: the imported key is marked exportable
3114 * phKey [O] Handle to the imported key.
3120 BOOL WINAPI
RSAENH_CPImportKey(HCRYPTPROV hProv
, CONST BYTE
*pbData
, DWORD dwDataLen
,
3121 HCRYPTKEY hPubKey
, DWORD dwFlags
, HCRYPTKEY
*phKey
)
3123 TRACE("(hProv=%08lx, pbData=%p, dwDataLen=%d, hPubKey=%08lx, dwFlags=%08x, phKey=%p)\n",
3124 hProv
, pbData
, dwDataLen
, hPubKey
, dwFlags
, phKey
);
3126 return import_key(hProv
, pbData
, dwDataLen
, hPubKey
, dwFlags
, TRUE
, phKey
);
3129 /******************************************************************************
3130 * CPGenKey (RSAENH.@)
3132 * Generate a key in the key container
3135 * hProv [I] Key container for which a key is to be generated.
3136 * Algid [I] Crypto algorithm identifier for the key to be generated.
3137 * dwFlags [I] Upper 16 bits: Binary length of key. Lower 16 bits: Flags. See Notes
3138 * phKey [O] Handle to the generated key.
3145 * Flags currently not considered.
3148 * Private key-exchange- and signature-keys can be generated with Algid AT_KEYEXCHANGE
3149 * and AT_SIGNATURE values.
3151 BOOL WINAPI
RSAENH_CPGenKey(HCRYPTPROV hProv
, ALG_ID Algid
, DWORD dwFlags
, HCRYPTKEY
*phKey
)
3153 KEYCONTAINER
*pKeyContainer
;
3154 CRYPTKEY
*pCryptKey
;
3156 TRACE("(hProv=%08lx, aiAlgid=%d, dwFlags=%08x, phKey=%p)\n", hProv
, Algid
, dwFlags
, phKey
);
3158 if (!lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
,
3159 (OBJECTHDR
**)&pKeyContainer
))
3161 /* MSDN: hProv not containing valid context handle */
3162 SetLastError(NTE_BAD_UID
);
3170 *phKey
= new_key(hProv
, CALG_RSA_SIGN
, dwFlags
, &pCryptKey
);
3172 new_key_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pCryptKey
->dwKeyLen
);
3173 setup_key(pCryptKey
);
3174 release_and_install_key(hProv
, *phKey
,
3175 &pKeyContainer
->hSignatureKeyPair
,
3180 case AT_KEYEXCHANGE
:
3182 *phKey
= new_key(hProv
, CALG_RSA_KEYX
, dwFlags
, &pCryptKey
);
3184 new_key_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pCryptKey
->dwKeyLen
);
3185 setup_key(pCryptKey
);
3186 release_and_install_key(hProv
, *phKey
,
3187 &pKeyContainer
->hKeyExchangeKeyPair
,
3201 case CALG_PCT1_MASTER
:
3202 case CALG_SSL2_MASTER
:
3203 case CALG_SSL3_MASTER
:
3204 case CALG_TLS1_MASTER
:
3205 *phKey
= new_key(hProv
, Algid
, dwFlags
, &pCryptKey
);
3207 gen_rand_impl(pCryptKey
->abKeyValue
, RSAENH_MAX_KEY_SIZE
);
3209 case CALG_SSL3_MASTER
:
3210 pCryptKey
->abKeyValue
[0] = RSAENH_SSL3_VERSION_MAJOR
;
3211 pCryptKey
->abKeyValue
[1] = RSAENH_SSL3_VERSION_MINOR
;
3214 case CALG_TLS1_MASTER
:
3215 pCryptKey
->abKeyValue
[0] = RSAENH_TLS1_VERSION_MAJOR
;
3216 pCryptKey
->abKeyValue
[1] = RSAENH_TLS1_VERSION_MINOR
;
3219 setup_key(pCryptKey
);
3224 /* MSDN: Algorithm not supported specified by Algid */
3225 SetLastError(NTE_BAD_ALGID
);
3229 return *phKey
!= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
3232 /******************************************************************************
3233 * CPGenRandom (RSAENH.@)
3235 * Generate a random byte stream.
3238 * hProv [I] Key container that is used to generate random bytes.
3239 * dwLen [I] Specifies the number of requested random data bytes.
3240 * pbBuffer [O] Random bytes will be stored here.
3246 BOOL WINAPI
RSAENH_CPGenRandom(HCRYPTPROV hProv
, DWORD dwLen
, BYTE
*pbBuffer
)
3248 TRACE("(hProv=%08lx, dwLen=%d, pbBuffer=%p)\n", hProv
, dwLen
, pbBuffer
);
3250 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
3252 /* MSDN: hProv not containing valid context handle */
3253 SetLastError(NTE_BAD_UID
);
3257 return gen_rand_impl(pbBuffer
, dwLen
);
3260 /******************************************************************************
3261 * CPGetHashParam (RSAENH.@)
3263 * Query parameters of an hash object.
3266 * hProv [I] The kea container, which the hash belongs to.
3267 * hHash [I] The hash object that is to be queried.
3268 * dwParam [I] Specifies the parameter that is to be queried.
3269 * pbData [I] Pointer to the buffer where the parameter value will be stored.
3270 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
3271 * dwFlags [I] None currently defined.
3278 * Valid dwParams are: HP_ALGID, HP_HASHSIZE, HP_HASHVALUE. The hash will be
3279 * finalized if HP_HASHVALUE is queried.
3281 BOOL WINAPI
RSAENH_CPGetHashParam(HCRYPTPROV hProv
, HCRYPTHASH hHash
, DWORD dwParam
, BYTE
*pbData
,
3282 DWORD
*pdwDataLen
, DWORD dwFlags
)
3284 CRYPTHASH
*pCryptHash
;
3286 TRACE("(hProv=%08lx, hHash=%08lx, dwParam=%08x, pbData=%p, pdwDataLen=%p, dwFlags=%08x)\n",
3287 hProv
, hHash
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
3289 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
3291 SetLastError(NTE_BAD_UID
);
3297 SetLastError(NTE_BAD_FLAGS
);
3301 if (!lookup_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
,
3302 (OBJECTHDR
**)&pCryptHash
))
3304 SetLastError(NTE_BAD_HASH
);
3310 SetLastError(ERROR_INVALID_PARAMETER
);
3317 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&pCryptHash
->aiAlgid
,
3321 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&pCryptHash
->dwHashSize
,
3325 if (pCryptHash
->aiAlgid
== CALG_TLS1PRF
) {
3326 return tls1_prf(hProv
, pCryptHash
->hKey
, &pCryptHash
->tpPRFParams
.blobLabel
,
3327 &pCryptHash
->tpPRFParams
.blobSeed
, pbData
, *pdwDataLen
);
3330 if ( pbData
== NULL
) {
3331 *pdwDataLen
= pCryptHash
->dwHashSize
;
3335 if (pbData
&& (pCryptHash
->dwState
!= RSAENH_HASHSTATE_FINISHED
))
3337 finalize_hash(pCryptHash
);
3338 pCryptHash
->dwState
= RSAENH_HASHSTATE_FINISHED
;
3341 return copy_param(pbData
, pdwDataLen
, pCryptHash
->abHashValue
,
3342 pCryptHash
->dwHashSize
);
3345 SetLastError(NTE_BAD_TYPE
);
3350 /******************************************************************************
3351 * CPSetKeyParam (RSAENH.@)
3353 * Set a parameter of a key object
3356 * hProv [I] The key container to which the key belongs.
3357 * hKey [I] The key for which a parameter is to be set.
3358 * dwParam [I] Parameter type. See Notes.
3359 * pbData [I] Pointer to the parameter value.
3360 * dwFlags [I] Currently none defined.
3367 * Defined dwParam types are:
3368 * - KP_MODE: Values MODE_CBC, MODE_ECB, MODE_CFB.
3369 * - KP_MODE_BITS: Shift width for cipher feedback mode. (Currently ignored by MS CSP's)
3370 * - KP_PERMISSIONS: Or'ed combination of CRYPT_ENCRYPT, CRYPT_DECRYPT,
3371 * CRYPT_EXPORT, CRYPT_READ, CRYPT_WRITE, CRYPT_MAC
3372 * - KP_IV: Initialization vector
3374 BOOL WINAPI
RSAENH_CPSetKeyParam(HCRYPTPROV hProv
, HCRYPTKEY hKey
, DWORD dwParam
, BYTE
*pbData
,
3377 CRYPTKEY
*pCryptKey
;
3379 TRACE("(hProv=%08lx, hKey=%08lx, dwParam=%08x, pbData=%p, dwFlags=%08x)\n", hProv
, hKey
,
3380 dwParam
, pbData
, dwFlags
);
3382 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
3384 SetLastError(NTE_BAD_UID
);
3389 SetLastError(NTE_BAD_FLAGS
);
3393 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
3395 SetLastError(NTE_BAD_KEY
);
3401 /* The MS providers only support PKCS5_PADDING */
3402 if (*(DWORD
*)pbData
!= PKCS5_PADDING
) {
3403 SetLastError(NTE_BAD_DATA
);
3409 pCryptKey
->dwMode
= *(DWORD
*)pbData
;
3413 pCryptKey
->dwModeBits
= *(DWORD
*)pbData
;
3416 case KP_PERMISSIONS
:
3418 DWORD perms
= *(DWORD
*)pbData
;
3420 if ((perms
& CRYPT_EXPORT
) &&
3421 !(pCryptKey
->dwPermissions
& CRYPT_EXPORT
))
3423 SetLastError(NTE_BAD_DATA
);
3426 else if (!(perms
& CRYPT_EXPORT
) &&
3427 (pCryptKey
->dwPermissions
& CRYPT_EXPORT
))
3429 /* Clearing the export permission appears to be ignored,
3432 perms
|= CRYPT_EXPORT
;
3434 pCryptKey
->dwPermissions
= perms
;
3439 memcpy(pCryptKey
->abInitVector
, pbData
, pCryptKey
->dwBlockLen
);
3440 setup_key(pCryptKey
);
3444 switch (pCryptKey
->aiAlgid
) {
3449 SetLastError(ERROR_INVALID_PARAMETER
);
3452 /* MSDN: the base provider always sets eleven bytes of
3455 memcpy(pCryptKey
->abKeyValue
+ pCryptKey
->dwKeyLen
,
3457 pCryptKey
->dwSaltLen
= 11;
3458 setup_key(pCryptKey
);
3459 /* Strange but true: salt length reset to 0 after setting
3462 pCryptKey
->dwSaltLen
= 0;
3465 SetLastError(NTE_BAD_KEY
);
3472 CRYPT_INTEGER_BLOB
*blob
= (CRYPT_INTEGER_BLOB
*)pbData
;
3474 /* salt length can't be greater than 184 bits = 24 bytes */
3475 if (blob
->cbData
> 24)
3477 SetLastError(NTE_BAD_DATA
);
3480 memcpy(pCryptKey
->abKeyValue
+ pCryptKey
->dwKeyLen
, blob
->pbData
,
3482 pCryptKey
->dwSaltLen
= blob
->cbData
;
3483 setup_key(pCryptKey
);
3487 case KP_EFFECTIVE_KEYLEN
:
3488 switch (pCryptKey
->aiAlgid
) {
3492 SetLastError(ERROR_INVALID_PARAMETER
);
3495 else if (!*(DWORD
*)pbData
|| *(DWORD
*)pbData
> 1024)
3497 SetLastError(NTE_BAD_DATA
);
3502 pCryptKey
->dwEffectiveKeyLen
= *(DWORD
*)pbData
;
3503 setup_key(pCryptKey
);
3507 SetLastError(NTE_BAD_TYPE
);
3512 case KP_SCHANNEL_ALG
:
3513 switch (((PSCHANNEL_ALG
)pbData
)->dwUse
) {
3514 case SCHANNEL_ENC_KEY
:
3515 memcpy(&pCryptKey
->siSChannelInfo
.saEncAlg
, pbData
, sizeof(SCHANNEL_ALG
));
3518 case SCHANNEL_MAC_KEY
:
3519 memcpy(&pCryptKey
->siSChannelInfo
.saMACAlg
, pbData
, sizeof(SCHANNEL_ALG
));
3523 SetLastError(NTE_FAIL
); /* FIXME: error code */
3528 case KP_CLIENT_RANDOM
:
3529 return copy_data_blob(&pCryptKey
->siSChannelInfo
.blobClientRandom
, (PCRYPT_DATA_BLOB
)pbData
);
3531 case KP_SERVER_RANDOM
:
3532 return copy_data_blob(&pCryptKey
->siSChannelInfo
.blobServerRandom
, (PCRYPT_DATA_BLOB
)pbData
);
3535 SetLastError(NTE_BAD_TYPE
);
3540 /******************************************************************************
3541 * CPGetKeyParam (RSAENH.@)
3543 * Query a key parameter.
3546 * hProv [I] The key container, which the key belongs to.
3547 * hHash [I] The key object that is to be queried.
3548 * dwParam [I] Specifies the parameter that is to be queried.
3549 * pbData [I] Pointer to the buffer where the parameter value will be stored.
3550 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
3551 * dwFlags [I] None currently defined.
3558 * Defined dwParam types are:
3559 * - KP_MODE: Values MODE_CBC, MODE_ECB, MODE_CFB.
3560 * - KP_MODE_BITS: Shift width for cipher feedback mode.
3561 * (Currently ignored by MS CSP's - always eight)
3562 * - KP_PERMISSIONS: Or'ed combination of CRYPT_ENCRYPT, CRYPT_DECRYPT,
3563 * CRYPT_EXPORT, CRYPT_READ, CRYPT_WRITE, CRYPT_MAC
3564 * - KP_IV: Initialization vector.
3565 * - KP_KEYLEN: Bitwidth of the key.
3566 * - KP_BLOCKLEN: Size of a block cipher block.
3567 * - KP_SALT: Salt value.
3569 BOOL WINAPI
RSAENH_CPGetKeyParam(HCRYPTPROV hProv
, HCRYPTKEY hKey
, DWORD dwParam
, BYTE
*pbData
,
3570 DWORD
*pdwDataLen
, DWORD dwFlags
)
3572 CRYPTKEY
*pCryptKey
;
3575 TRACE("(hProv=%08lx, hKey=%08lx, dwParam=%08x, pbData=%p, pdwDataLen=%p dwFlags=%08x)\n",
3576 hProv
, hKey
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
3578 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
3580 SetLastError(NTE_BAD_UID
);
3585 SetLastError(NTE_BAD_FLAGS
);
3589 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
3591 SetLastError(NTE_BAD_KEY
);
3598 return copy_param(pbData
, pdwDataLen
, pCryptKey
->abInitVector
,
3599 pCryptKey
->dwBlockLen
);
3602 switch (pCryptKey
->aiAlgid
) {
3605 return copy_param(pbData
, pdwDataLen
,
3606 &pCryptKey
->abKeyValue
[pCryptKey
->dwKeyLen
],
3607 pCryptKey
->dwSaltLen
);
3609 SetLastError(NTE_BAD_KEY
);
3614 dwValue
= PKCS5_PADDING
;
3615 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwValue
, sizeof(DWORD
));
3618 dwValue
= pCryptKey
->dwKeyLen
<< 3;
3619 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwValue
, sizeof(DWORD
));
3621 case KP_EFFECTIVE_KEYLEN
:
3622 if (pCryptKey
->dwEffectiveKeyLen
)
3623 dwValue
= pCryptKey
->dwEffectiveKeyLen
;
3625 dwValue
= pCryptKey
->dwKeyLen
<< 3;
3626 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwValue
, sizeof(DWORD
));
3629 dwValue
= pCryptKey
->dwBlockLen
<< 3;
3630 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwValue
, sizeof(DWORD
));
3633 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&pCryptKey
->dwMode
, sizeof(DWORD
));
3636 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&pCryptKey
->dwModeBits
,
3639 case KP_PERMISSIONS
:
3640 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&pCryptKey
->dwPermissions
,
3644 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&pCryptKey
->aiAlgid
, sizeof(DWORD
));
3647 SetLastError(NTE_BAD_TYPE
);
3652 /******************************************************************************
3653 * CPGetProvParam (RSAENH.@)
3655 * Query a CSP parameter.
3658 * hProv [I] The key container that is to be queried.
3659 * dwParam [I] Specifies the parameter that is to be queried.
3660 * pbData [I] Pointer to the buffer where the parameter value will be stored.
3661 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
3662 * dwFlags [I] CRYPT_FIRST: Start enumeration (for PP_ENUMALGS{_EX}).
3668 * Defined dwParam types:
3669 * - PP_CONTAINER: Name of the key container.
3670 * - PP_NAME: Name of the cryptographic service provider.
3671 * - PP_SIG_KEYSIZE_INC: RSA signature keywidth granularity in bits.
3672 * - PP_KEYX_KEYSIZE_INC: RSA key-exchange keywidth granularity in bits.
3673 * - PP_ENUMALGS{_EX}: Query provider capabilities.
3675 BOOL WINAPI
RSAENH_CPGetProvParam(HCRYPTPROV hProv
, DWORD dwParam
, BYTE
*pbData
,
3676 DWORD
*pdwDataLen
, DWORD dwFlags
)
3678 KEYCONTAINER
*pKeyContainer
;
3679 PROV_ENUMALGS provEnumalgs
;
3683 /* This is for dwParam PP_CRYPT_COUNT_KEY_USE.
3684 * IE6 SP1 asks for it in the 'About' dialog.
3685 * Returning this BLOB seems to satisfy IE. The marked 0x00 seem
3686 * to be 'don't care's. If you know anything more specific about
3687 * this provider parameter, please report to wine-devel@winehq.org */
3688 static CONST BYTE abWTF
[96] = {
3689 0xb0, 0x25, 0x63, 0x86, 0x9c, 0xab, 0xb6, 0x37,
3690 0xe8, 0x82, /**/0x00,/**/ 0x72, 0x06, 0xb2, /**/0x00,/**/ 0x3b,
3691 0x60, 0x35, /**/0x00,/**/ 0x3b, 0x88, 0xce, /**/0x00,/**/ 0x82,
3692 0xbc, 0x7a, /**/0x00,/**/ 0xb7, 0x4f, 0x7e, /**/0x00,/**/ 0xde,
3693 0x92, 0xf1, /**/0x00,/**/ 0x83, 0xea, 0x5e, /**/0x00,/**/ 0xc8,
3694 0x12, 0x1e, 0xd4, 0x06, 0xf7, 0x66, /**/0x00,/**/ 0x01,
3695 0x29, 0xa4, /**/0x00,/**/ 0xf8, 0x24, 0x0c, /**/0x00,/**/ 0x33,
3696 0x06, 0x80, /**/0x00,/**/ 0x02, 0x46, 0x0b, /**/0x00,/**/ 0x6d,
3697 0x5b, 0xca, /**/0x00,/**/ 0x9a, 0x10, 0xf0, /**/0x00,/**/ 0x05,
3698 0x19, 0xd0, /**/0x00,/**/ 0x2c, 0xf6, 0x27, /**/0x00,/**/ 0xaa,
3699 0x7c, 0x6f, /**/0x00,/**/ 0xb9, 0xd8, 0x72, /**/0x00,/**/ 0x03,
3700 0xf3, 0x81, /**/0x00,/**/ 0xfa, 0xe8, 0x26, /**/0x00,/**/ 0xca
3703 TRACE("(hProv=%08lx, dwParam=%08x, pbData=%p, pdwDataLen=%p, dwFlags=%08x)\n",
3704 hProv
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
3707 SetLastError(ERROR_INVALID_PARAMETER
);
3711 if (!lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
,
3712 (OBJECTHDR
**)&pKeyContainer
))
3714 /* MSDN: hProv not containing valid context handle */
3715 SetLastError(NTE_BAD_UID
);
3722 case PP_UNIQUE_CONTAINER
:/* MSDN says we can return the same value as PP_CONTAINER */
3723 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)pKeyContainer
->szName
,
3724 strlen(pKeyContainer
->szName
)+1);
3727 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)pKeyContainer
->szProvName
,
3728 strlen(pKeyContainer
->szProvName
)+1);
3731 dwTemp
= PROV_RSA_FULL
;
3732 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwTemp
, sizeof(dwTemp
));
3735 dwTemp
= AT_SIGNATURE
| AT_KEYEXCHANGE
;
3736 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwTemp
, sizeof(dwTemp
));
3738 case PP_KEYSET_TYPE
:
3739 dwTemp
= pKeyContainer
->dwFlags
& CRYPT_MACHINE_KEYSET
;
3740 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwTemp
, sizeof(dwTemp
));
3743 dwTemp
= CRYPT_SEC_DESCR
;
3744 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwTemp
, sizeof(dwTemp
));
3746 case PP_SIG_KEYSIZE_INC
:
3747 case PP_KEYX_KEYSIZE_INC
:
3749 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwTemp
, sizeof(dwTemp
));
3752 dwTemp
= CRYPT_IMPL_SOFTWARE
;
3753 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwTemp
, sizeof(dwTemp
));
3756 dwTemp
= 0x00000200;
3757 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwTemp
, sizeof(dwTemp
));
3759 case PP_ENUMCONTAINERS
:
3760 if ((dwFlags
& CRYPT_FIRST
) == CRYPT_FIRST
) pKeyContainer
->dwEnumContainersCtr
= 0;
3763 *pdwDataLen
= (DWORD
)MAX_PATH
+ 1;
3767 if (!open_container_key("", dwFlags
, &hKey
))
3769 SetLastError(ERROR_NO_MORE_ITEMS
);
3773 dwTemp
= *pdwDataLen
;
3774 switch (RegEnumKeyExA(hKey
, pKeyContainer
->dwEnumContainersCtr
, (LPSTR
)pbData
, &dwTemp
,
3775 NULL
, NULL
, NULL
, NULL
))
3777 case ERROR_MORE_DATA
:
3778 *pdwDataLen
= (DWORD
)MAX_PATH
+ 1;
3781 pKeyContainer
->dwEnumContainersCtr
++;
3785 case ERROR_NO_MORE_ITEMS
:
3787 SetLastError(ERROR_NO_MORE_ITEMS
);
3793 case PP_ENUMALGS_EX
:
3794 if (((pKeyContainer
->dwEnumAlgsCtr
>= RSAENH_MAX_ENUMALGS
-1) ||
3795 (!aProvEnumAlgsEx
[pKeyContainer
->dwPersonality
]
3796 [pKeyContainer
->dwEnumAlgsCtr
+1].aiAlgid
)) &&
3797 ((dwFlags
& CRYPT_FIRST
) != CRYPT_FIRST
))
3799 SetLastError(ERROR_NO_MORE_ITEMS
);
3803 if (dwParam
== PP_ENUMALGS
) {
3804 if (pbData
&& (*pdwDataLen
>= sizeof(PROV_ENUMALGS
)))
3805 pKeyContainer
->dwEnumAlgsCtr
= ((dwFlags
& CRYPT_FIRST
) == CRYPT_FIRST
) ?
3806 0 : pKeyContainer
->dwEnumAlgsCtr
+1;
3808 provEnumalgs
.aiAlgid
= aProvEnumAlgsEx
3809 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
].aiAlgid
;
3810 provEnumalgs
.dwBitLen
= aProvEnumAlgsEx
3811 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
].dwDefaultLen
;
3812 provEnumalgs
.dwNameLen
= aProvEnumAlgsEx
3813 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
].dwNameLen
;
3814 memcpy(provEnumalgs
.szName
, aProvEnumAlgsEx
3815 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
].szName
,
3818 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&provEnumalgs
,
3819 sizeof(PROV_ENUMALGS
));
3821 if (pbData
&& (*pdwDataLen
>= sizeof(PROV_ENUMALGS_EX
)))
3822 pKeyContainer
->dwEnumAlgsCtr
= ((dwFlags
& CRYPT_FIRST
) == CRYPT_FIRST
) ?
3823 0 : pKeyContainer
->dwEnumAlgsCtr
+1;
3825 return copy_param(pbData
, pdwDataLen
,
3826 (CONST BYTE
*)&aProvEnumAlgsEx
3827 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
],
3828 sizeof(PROV_ENUMALGS_EX
));
3831 case PP_CRYPT_COUNT_KEY_USE
: /* Asked for by IE About dialog */
3832 return copy_param(pbData
, pdwDataLen
, abWTF
, sizeof(abWTF
));
3835 /* MSDN: Unknown parameter number in dwParam */
3836 SetLastError(NTE_BAD_TYPE
);
3841 /******************************************************************************
3842 * CPDeriveKey (RSAENH.@)
3844 * Derives a key from a hash value.
3847 * hProv [I] Key container for which a key is to be generated.
3848 * Algid [I] Crypto algorithm identifier for the key to be generated.
3849 * hBaseData [I] Hash from whose value the key will be derived.
3850 * dwFlags [I] See Notes.
3851 * phKey [O] The generated key.
3859 * - CRYPT_EXPORTABLE: Key can be exported.
3860 * - CRYPT_NO_SALT: No salt is used for 40 bit keys.
3861 * - CRYPT_CREATE_SALT: Use remaining bits as salt value.
3863 BOOL WINAPI
RSAENH_CPDeriveKey(HCRYPTPROV hProv
, ALG_ID Algid
, HCRYPTHASH hBaseData
,
3864 DWORD dwFlags
, HCRYPTKEY
*phKey
)
3866 CRYPTKEY
*pCryptKey
, *pMasterKey
;
3867 CRYPTHASH
*pCryptHash
;
3868 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
*2];
3871 TRACE("(hProv=%08lx, Algid=%d, hBaseData=%08lx, dwFlags=%08x phKey=%p)\n", hProv
, Algid
,
3872 hBaseData
, dwFlags
, phKey
);
3874 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
3876 SetLastError(NTE_BAD_UID
);
3880 if (!lookup_handle(&handle_table
, hBaseData
, RSAENH_MAGIC_HASH
,
3881 (OBJECTHDR
**)&pCryptHash
))
3883 SetLastError(NTE_BAD_HASH
);
3889 SetLastError(ERROR_INVALID_PARAMETER
);
3893 switch (GET_ALG_CLASS(Algid
))
3895 case ALG_CLASS_DATA_ENCRYPT
:
3896 *phKey
= new_key(hProv
, Algid
, dwFlags
, &pCryptKey
);
3897 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
3900 * We derive the key material from the hash.
3901 * If the hash value is not large enough for the claimed key, we have to construct
3902 * a larger binary value based on the hash. This is documented in MSDN: CryptDeriveKey.
3904 dwLen
= RSAENH_MAX_HASH_SIZE
;
3905 RSAENH_CPGetHashParam(pCryptHash
->hProv
, hBaseData
, HP_HASHVAL
, abHashValue
, &dwLen
, 0);
3907 if (dwLen
< pCryptKey
->dwKeyLen
) {
3908 BYTE pad1
[RSAENH_HMAC_DEF_PAD_LEN
], pad2
[RSAENH_HMAC_DEF_PAD_LEN
];
3909 BYTE old_hashval
[RSAENH_MAX_HASH_SIZE
];
3912 memcpy(old_hashval
, pCryptHash
->abHashValue
, RSAENH_MAX_HASH_SIZE
);
3914 for (i
=0; i
<RSAENH_HMAC_DEF_PAD_LEN
; i
++) {
3915 pad1
[i
] = RSAENH_HMAC_DEF_IPAD_CHAR
^ (i
<dwLen
? abHashValue
[i
] : 0);
3916 pad2
[i
] = RSAENH_HMAC_DEF_OPAD_CHAR
^ (i
<dwLen
? abHashValue
[i
] : 0);
3919 init_hash(pCryptHash
);
3920 update_hash(pCryptHash
, pad1
, RSAENH_HMAC_DEF_PAD_LEN
);
3921 finalize_hash(pCryptHash
);
3922 memcpy(abHashValue
, pCryptHash
->abHashValue
, pCryptHash
->dwHashSize
);
3924 init_hash(pCryptHash
);
3925 update_hash(pCryptHash
, pad2
, RSAENH_HMAC_DEF_PAD_LEN
);
3926 finalize_hash(pCryptHash
);
3927 memcpy(abHashValue
+pCryptHash
->dwHashSize
, pCryptHash
->abHashValue
,
3928 pCryptHash
->dwHashSize
);
3930 memcpy(pCryptHash
->abHashValue
, old_hashval
, RSAENH_MAX_HASH_SIZE
);
3933 memcpy(pCryptKey
->abKeyValue
, abHashValue
,
3934 RSAENH_MIN(pCryptKey
->dwKeyLen
, sizeof(pCryptKey
->abKeyValue
)));
3937 case ALG_CLASS_MSG_ENCRYPT
:
3938 if (!lookup_handle(&handle_table
, pCryptHash
->hKey
, RSAENH_MAGIC_KEY
,
3939 (OBJECTHDR
**)&pMasterKey
))
3941 SetLastError(NTE_FAIL
); /* FIXME error code */
3947 /* See RFC 2246, chapter 6.3 Key calculation */
3948 case CALG_SCHANNEL_ENC_KEY
:
3949 if (!pMasterKey
->siSChannelInfo
.saEncAlg
.Algid
||
3950 !pMasterKey
->siSChannelInfo
.saEncAlg
.cBits
)
3952 SetLastError(NTE_BAD_FLAGS
);
3955 *phKey
= new_key(hProv
, pMasterKey
->siSChannelInfo
.saEncAlg
.Algid
,
3956 MAKELONG(LOWORD(dwFlags
),pMasterKey
->siSChannelInfo
.saEncAlg
.cBits
),
3958 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
3959 memcpy(pCryptKey
->abKeyValue
,
3960 pCryptHash
->abHashValue
+ (
3961 2 * (pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
/ 8) +
3962 ((dwFlags
& CRYPT_SERVER
) ?
3963 (pMasterKey
->siSChannelInfo
.saEncAlg
.cBits
/ 8) : 0)),
3964 pMasterKey
->siSChannelInfo
.saEncAlg
.cBits
/ 8);
3965 memcpy(pCryptKey
->abInitVector
,
3966 pCryptHash
->abHashValue
+ (
3967 2 * (pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
/ 8) +
3968 2 * (pMasterKey
->siSChannelInfo
.saEncAlg
.cBits
/ 8) +
3969 ((dwFlags
& CRYPT_SERVER
) ? pCryptKey
->dwBlockLen
: 0)),
3970 pCryptKey
->dwBlockLen
);
3973 case CALG_SCHANNEL_MAC_KEY
:
3974 *phKey
= new_key(hProv
, Algid
,
3975 MAKELONG(LOWORD(dwFlags
),pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
),
3977 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
3978 memcpy(pCryptKey
->abKeyValue
,
3979 pCryptHash
->abHashValue
+ ((dwFlags
& CRYPT_SERVER
) ?
3980 pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
/ 8 : 0),
3981 pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
/ 8);
3985 SetLastError(NTE_BAD_ALGID
);
3991 SetLastError(NTE_BAD_ALGID
);
3995 setup_key(pCryptKey
);
3999 /******************************************************************************
4000 * CPGetUserKey (RSAENH.@)
4002 * Returns a handle to the user's private key-exchange- or signature-key.
4005 * hProv [I] The key container from which a user key is requested.
4006 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
4007 * phUserKey [O] Handle to the requested key or INVALID_HANDLE_VALUE in case of failure.
4014 * A newly created key container does not contain private user key. Create them with CPGenKey.
4016 BOOL WINAPI
RSAENH_CPGetUserKey(HCRYPTPROV hProv
, DWORD dwKeySpec
, HCRYPTKEY
*phUserKey
)
4018 KEYCONTAINER
*pKeyContainer
;
4020 TRACE("(hProv=%08lx, dwKeySpec=%08x, phUserKey=%p)\n", hProv
, dwKeySpec
, phUserKey
);
4022 if (!lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
,
4023 (OBJECTHDR
**)&pKeyContainer
))
4025 /* MSDN: hProv not containing valid context handle */
4026 SetLastError(NTE_BAD_UID
);
4032 case AT_KEYEXCHANGE
:
4033 copy_handle(&handle_table
, pKeyContainer
->hKeyExchangeKeyPair
, RSAENH_MAGIC_KEY
,
4038 copy_handle(&handle_table
, pKeyContainer
->hSignatureKeyPair
, RSAENH_MAGIC_KEY
,
4043 *phUserKey
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
4046 if (*phUserKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
4048 /* MSDN: dwKeySpec parameter specifies nonexistent key */
4049 SetLastError(NTE_NO_KEY
);
4056 /******************************************************************************
4057 * CPHashData (RSAENH.@)
4059 * Updates a hash object with the given data.
4062 * hProv [I] Key container to which the hash object belongs.
4063 * hHash [I] Hash object which is to be updated.
4064 * pbData [I] Pointer to data with which the hash object is to be updated.
4065 * dwDataLen [I] Length of the data.
4066 * dwFlags [I] Currently none defined.
4073 * The actual hash value is queried with CPGetHashParam, which will finalize
4074 * the hash. Updating a finalized hash will fail with a last error NTE_BAD_HASH_STATE.
4076 BOOL WINAPI
RSAENH_CPHashData(HCRYPTPROV hProv
, HCRYPTHASH hHash
, CONST BYTE
*pbData
,
4077 DWORD dwDataLen
, DWORD dwFlags
)
4079 CRYPTHASH
*pCryptHash
;
4081 TRACE("(hProv=%08lx, hHash=%08lx, pbData=%p, dwDataLen=%d, dwFlags=%08x)\n",
4082 hProv
, hHash
, pbData
, dwDataLen
, dwFlags
);
4086 SetLastError(NTE_BAD_FLAGS
);
4090 if (!lookup_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
,
4091 (OBJECTHDR
**)&pCryptHash
))
4093 SetLastError(NTE_BAD_HASH
);
4097 if (!get_algid_info(hProv
, pCryptHash
->aiAlgid
) || pCryptHash
->aiAlgid
== CALG_SSL3_SHAMD5
)
4099 SetLastError(NTE_BAD_ALGID
);
4103 if (pCryptHash
->dwState
!= RSAENH_HASHSTATE_HASHING
)
4105 SetLastError(NTE_BAD_HASH_STATE
);
4109 update_hash(pCryptHash
, pbData
, dwDataLen
);
4113 /******************************************************************************
4114 * CPHashSessionKey (RSAENH.@)
4116 * Updates a hash object with the binary representation of a symmetric key.
4119 * hProv [I] Key container to which the hash object belongs.
4120 * hHash [I] Hash object which is to be updated.
4121 * hKey [I] The symmetric key, whose binary value will be added to the hash.
4122 * dwFlags [I] CRYPT_LITTLE_ENDIAN, if the binary key value shall be interpreted as little endian.
4128 BOOL WINAPI
RSAENH_CPHashSessionKey(HCRYPTPROV hProv
, HCRYPTHASH hHash
, HCRYPTKEY hKey
,
4131 BYTE abKeyValue
[RSAENH_MAX_KEY_SIZE
], bTemp
;
4135 TRACE("(hProv=%08lx, hHash=%08lx, hKey=%08lx, dwFlags=%08x)\n", hProv
, hHash
, hKey
, dwFlags
);
4137 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pKey
) ||
4138 (GET_ALG_CLASS(pKey
->aiAlgid
) != ALG_CLASS_DATA_ENCRYPT
))
4140 SetLastError(NTE_BAD_KEY
);
4144 if (dwFlags
& ~CRYPT_LITTLE_ENDIAN
) {
4145 SetLastError(NTE_BAD_FLAGS
);
4149 memcpy(abKeyValue
, pKey
->abKeyValue
, pKey
->dwKeyLen
);
4150 if (!(dwFlags
& CRYPT_LITTLE_ENDIAN
)) {
4151 for (i
=0; i
<pKey
->dwKeyLen
/2; i
++) {
4152 bTemp
= abKeyValue
[i
];
4153 abKeyValue
[i
] = abKeyValue
[pKey
->dwKeyLen
-i
-1];
4154 abKeyValue
[pKey
->dwKeyLen
-i
-1] = bTemp
;
4158 return RSAENH_CPHashData(hProv
, hHash
, abKeyValue
, pKey
->dwKeyLen
, 0);
4161 /******************************************************************************
4162 * CPReleaseContext (RSAENH.@)
4164 * Release a key container.
4167 * hProv [I] Key container to be released.
4168 * dwFlags [I] Currently none defined.
4174 BOOL WINAPI
RSAENH_CPReleaseContext(HCRYPTPROV hProv
, DWORD dwFlags
)
4176 TRACE("(hProv=%08lx, dwFlags=%08x)\n", hProv
, dwFlags
);
4178 if (!release_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
4180 /* MSDN: hProv not containing valid context handle */
4181 SetLastError(NTE_BAD_UID
);
4186 SetLastError(NTE_BAD_FLAGS
);
4193 /******************************************************************************
4194 * CPSetHashParam (RSAENH.@)
4196 * Set a parameter of a hash object
4199 * hProv [I] The key container to which the key belongs.
4200 * hHash [I] The hash object for which a parameter is to be set.
4201 * dwParam [I] Parameter type. See Notes.
4202 * pbData [I] Pointer to the parameter value.
4203 * dwFlags [I] Currently none defined.
4210 * Currently only the HP_HMAC_INFO dwParam type is defined.
4211 * The HMAC_INFO struct will be deep copied into the hash object.
4212 * See Internet RFC 2104 for details on the HMAC algorithm.
4214 BOOL WINAPI
RSAENH_CPSetHashParam(HCRYPTPROV hProv
, HCRYPTHASH hHash
, DWORD dwParam
,
4215 BYTE
*pbData
, DWORD dwFlags
)
4217 CRYPTHASH
*pCryptHash
;
4218 CRYPTKEY
*pCryptKey
;
4221 TRACE("(hProv=%08lx, hHash=%08lx, dwParam=%08x, pbData=%p, dwFlags=%08x)\n",
4222 hProv
, hHash
, dwParam
, pbData
, dwFlags
);
4224 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
4226 SetLastError(NTE_BAD_UID
);
4231 SetLastError(NTE_BAD_FLAGS
);
4235 if (!lookup_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
,
4236 (OBJECTHDR
**)&pCryptHash
))
4238 SetLastError(NTE_BAD_HASH
);
4244 free_hmac_info(pCryptHash
->pHMACInfo
);
4245 if (!copy_hmac_info(&pCryptHash
->pHMACInfo
, (PHMAC_INFO
)pbData
)) return FALSE
;
4247 if (!lookup_handle(&handle_table
, pCryptHash
->hKey
, RSAENH_MAGIC_KEY
,
4248 (OBJECTHDR
**)&pCryptKey
))
4250 SetLastError(NTE_FAIL
); /* FIXME: correct error code? */
4254 if (pCryptKey
->aiAlgid
== CALG_HMAC
&& !pCryptKey
->dwKeyLen
) {
4255 HCRYPTHASH hKeyHash
;
4258 if (!RSAENH_CPCreateHash(hProv
, ((PHMAC_INFO
)pbData
)->HashAlgid
, 0, 0,
4261 if (!RSAENH_CPHashData(hProv
, hKeyHash
, pCryptKey
->blobHmacKey
.pbData
,
4262 pCryptKey
->blobHmacKey
.cbData
, 0))
4264 RSAENH_CPDestroyHash(hProv
, hKeyHash
);
4267 keyLen
= sizeof(pCryptKey
->abKeyValue
);
4268 if (!RSAENH_CPGetHashParam(hProv
, hKeyHash
, HP_HASHVAL
, pCryptKey
->abKeyValue
,
4271 RSAENH_CPDestroyHash(hProv
, hKeyHash
);
4274 pCryptKey
->dwKeyLen
= keyLen
;
4275 RSAENH_CPDestroyHash(hProv
, hKeyHash
);
4277 for (i
=0; i
<RSAENH_MIN(pCryptKey
->dwKeyLen
,pCryptHash
->pHMACInfo
->cbInnerString
); i
++) {
4278 pCryptHash
->pHMACInfo
->pbInnerString
[i
] ^= pCryptKey
->abKeyValue
[i
];
4280 for (i
=0; i
<RSAENH_MIN(pCryptKey
->dwKeyLen
,pCryptHash
->pHMACInfo
->cbOuterString
); i
++) {
4281 pCryptHash
->pHMACInfo
->pbOuterString
[i
] ^= pCryptKey
->abKeyValue
[i
];
4284 init_hash(pCryptHash
);
4288 memcpy(pCryptHash
->abHashValue
, pbData
, pCryptHash
->dwHashSize
);
4289 pCryptHash
->dwState
= RSAENH_HASHSTATE_FINISHED
;
4292 case HP_TLS1PRF_SEED
:
4293 return copy_data_blob(&pCryptHash
->tpPRFParams
.blobSeed
, (PCRYPT_DATA_BLOB
)pbData
);
4295 case HP_TLS1PRF_LABEL
:
4296 return copy_data_blob(&pCryptHash
->tpPRFParams
.blobLabel
, (PCRYPT_DATA_BLOB
)pbData
);
4299 SetLastError(NTE_BAD_TYPE
);
4304 /******************************************************************************
4305 * CPSetProvParam (RSAENH.@)
4307 BOOL WINAPI
RSAENH_CPSetProvParam(HCRYPTPROV hProv
, DWORD dwParam
, BYTE
*pbData
, DWORD dwFlags
)
4313 /******************************************************************************
4314 * CPSignHash (RSAENH.@)
4316 * Sign a hash object
4319 * hProv [I] The key container, to which the hash object belongs.
4320 * hHash [I] The hash object to be signed.
4321 * dwKeySpec [I] AT_SIGNATURE or AT_KEYEXCHANGE: Key used to generate the signature.
4322 * sDescription [I] Should be NULL for security reasons.
4323 * dwFlags [I] 0, CRYPT_NOHASHOID or CRYPT_X931_FORMAT: Format of the signature.
4324 * pbSignature [O] Buffer, to which the signature will be stored. May be NULL to query SigLen.
4325 * pdwSigLen [I/O] Size of the buffer (in), Length of the signature (out)
4331 BOOL WINAPI
RSAENH_CPSignHash(HCRYPTPROV hProv
, HCRYPTHASH hHash
, DWORD dwKeySpec
,
4332 LPCWSTR sDescription
, DWORD dwFlags
, BYTE
*pbSignature
,
4335 HCRYPTKEY hCryptKey
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
4336 CRYPTKEY
*pCryptKey
;
4338 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
];
4342 TRACE("(hProv=%08lx, hHash=%08lx, dwKeySpec=%08x, sDescription=%s, dwFlags=%08x, "
4343 "pbSignature=%p, pdwSigLen=%p)\n", hProv
, hHash
, dwKeySpec
, debugstr_w(sDescription
),
4344 dwFlags
, pbSignature
, pdwSigLen
);
4346 if (dwFlags
& ~(CRYPT_NOHASHOID
|CRYPT_X931_FORMAT
)) {
4347 SetLastError(NTE_BAD_FLAGS
);
4351 if (!RSAENH_CPGetUserKey(hProv
, dwKeySpec
, &hCryptKey
)) return FALSE
;
4353 if (!lookup_handle(&handle_table
, hCryptKey
, RSAENH_MAGIC_KEY
,
4354 (OBJECTHDR
**)&pCryptKey
))
4356 SetLastError(NTE_NO_KEY
);
4361 *pdwSigLen
= pCryptKey
->dwKeyLen
;
4365 if (pCryptKey
->dwKeyLen
> *pdwSigLen
)
4367 SetLastError(ERROR_MORE_DATA
);
4368 *pdwSigLen
= pCryptKey
->dwKeyLen
;
4371 *pdwSigLen
= pCryptKey
->dwKeyLen
;
4374 if (!RSAENH_CPHashData(hProv
, hHash
, (CONST BYTE
*)sDescription
,
4375 (DWORD
)lstrlenW(sDescription
)*sizeof(WCHAR
), 0))
4381 dwHashLen
= sizeof(DWORD
);
4382 if (!RSAENH_CPGetHashParam(hProv
, hHash
, HP_ALGID
, (BYTE
*)&aiAlgid
, &dwHashLen
, 0)) goto out
;
4384 dwHashLen
= RSAENH_MAX_HASH_SIZE
;
4385 if (!RSAENH_CPGetHashParam(hProv
, hHash
, HP_HASHVAL
, abHashValue
, &dwHashLen
, 0)) goto out
;
4388 if (!build_hash_signature(pbSignature
, *pdwSigLen
, aiAlgid
, abHashValue
, dwHashLen
, dwFlags
)) {
4392 ret
= encrypt_block_impl(pCryptKey
->aiAlgid
, PK_PRIVATE
, &pCryptKey
->context
, pbSignature
, pbSignature
, RSAENH_ENCRYPT
);
4394 RSAENH_CPDestroyKey(hProv
, hCryptKey
);
4398 /******************************************************************************
4399 * CPVerifySignature (RSAENH.@)
4401 * Verify the signature of a hash object.
4404 * hProv [I] The key container, to which the hash belongs.
4405 * hHash [I] The hash for which the signature is verified.
4406 * pbSignature [I] The binary signature.
4407 * dwSigLen [I] Length of the signature BLOB.
4408 * hPubKey [I] Public key used to verify the signature.
4409 * sDescription [I] Should be NULL for security reasons.
4410 * dwFlags [I] 0, CRYPT_NOHASHOID or CRYPT_X931_FORMAT: Format of the signature.
4413 * Success: TRUE (Signature is valid)
4414 * Failure: FALSE (GetLastError() == NTE_BAD_SIGNATURE, if signature is invalid)
4416 BOOL WINAPI
RSAENH_CPVerifySignature(HCRYPTPROV hProv
, HCRYPTHASH hHash
, CONST BYTE
*pbSignature
,
4417 DWORD dwSigLen
, HCRYPTKEY hPubKey
, LPCWSTR sDescription
,
4420 BYTE
*pbConstructed
= NULL
, *pbDecrypted
= NULL
;
4421 CRYPTKEY
*pCryptKey
;
4424 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
];
4427 TRACE("(hProv=%08lx, hHash=%08lx, pbSignature=%p, dwSigLen=%d, hPubKey=%08lx, sDescription=%s, "
4428 "dwFlags=%08x)\n", hProv
, hHash
, pbSignature
, dwSigLen
, hPubKey
, debugstr_w(sDescription
),
4431 if (dwFlags
& ~(CRYPT_NOHASHOID
|CRYPT_X931_FORMAT
)) {
4432 SetLastError(NTE_BAD_FLAGS
);
4436 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
4438 SetLastError(NTE_BAD_UID
);
4442 if (!lookup_handle(&handle_table
, hPubKey
, RSAENH_MAGIC_KEY
,
4443 (OBJECTHDR
**)&pCryptKey
))
4445 SetLastError(NTE_BAD_KEY
);
4449 /* in Microsoft implementation, the signature length is checked before
4450 * the signature pointer.
4452 if (dwSigLen
!= pCryptKey
->dwKeyLen
)
4454 SetLastError(NTE_BAD_SIGNATURE
);
4458 if (!hHash
|| !pbSignature
)
4460 SetLastError(ERROR_INVALID_PARAMETER
);
4465 if (!RSAENH_CPHashData(hProv
, hHash
, (CONST BYTE
*)sDescription
,
4466 (DWORD
)lstrlenW(sDescription
)*sizeof(WCHAR
), 0))
4472 dwHashLen
= sizeof(DWORD
);
4473 if (!RSAENH_CPGetHashParam(hProv
, hHash
, HP_ALGID
, (BYTE
*)&aiAlgid
, &dwHashLen
, 0)) return FALSE
;
4475 dwHashLen
= RSAENH_MAX_HASH_SIZE
;
4476 if (!RSAENH_CPGetHashParam(hProv
, hHash
, HP_HASHVAL
, abHashValue
, &dwHashLen
, 0)) return FALSE
;
4478 pbConstructed
= HeapAlloc(GetProcessHeap(), 0, dwSigLen
);
4479 if (!pbConstructed
) {
4480 SetLastError(NTE_NO_MEMORY
);
4484 pbDecrypted
= HeapAlloc(GetProcessHeap(), 0, dwSigLen
);
4486 SetLastError(NTE_NO_MEMORY
);
4490 if (!encrypt_block_impl(pCryptKey
->aiAlgid
, PK_PUBLIC
, &pCryptKey
->context
, pbSignature
, pbDecrypted
,
4496 if (build_hash_signature(pbConstructed
, dwSigLen
, aiAlgid
, abHashValue
, dwHashLen
, dwFlags
) &&
4497 !memcmp(pbDecrypted
, pbConstructed
, dwSigLen
)) {
4502 if (!(dwFlags
& CRYPT_NOHASHOID
) &&
4503 build_hash_signature(pbConstructed
, dwSigLen
, aiAlgid
, abHashValue
, dwHashLen
, dwFlags
|CRYPT_NOHASHOID
) &&
4504 !memcmp(pbDecrypted
, pbConstructed
, dwSigLen
)) {
4509 SetLastError(NTE_BAD_SIGNATURE
);
4512 HeapFree(GetProcessHeap(), 0, pbConstructed
);
4513 HeapFree(GetProcessHeap(), 0, pbDecrypted
);
4517 /******************************************************************************
4518 * DllRegisterServer (RSAENH.@)
4520 HRESULT WINAPI
DllRegisterServer(void)
4522 return __wine_register_resources( instance
);
4525 /******************************************************************************
4526 * DllUnregisterServer (RSAENH.@)
4528 HRESULT WINAPI
DllUnregisterServer(void)
4530 return __wine_unregister_resources( instance
);