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"
43 WINE_DEFAULT_DEBUG_CHANNEL(crypt
);
45 static HINSTANCE instance
;
47 /******************************************************************************
48 * CRYPTHASH - hash objects
50 #define RSAENH_MAGIC_HASH 0x85938417u
51 #define RSAENH_MAX_HASH_SIZE 104
52 #define RSAENH_HASHSTATE_HASHING 1
53 #define RSAENH_HASHSTATE_FINISHED 2
54 typedef struct _RSAENH_TLS1PRF_PARAMS
56 CRYPT_DATA_BLOB blobLabel
;
57 CRYPT_DATA_BLOB blobSeed
;
58 } RSAENH_TLS1PRF_PARAMS
;
60 typedef struct tagCRYPTHASH
69 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
];
71 RSAENH_TLS1PRF_PARAMS tpPRFParams
;
74 /******************************************************************************
75 * CRYPTKEY - key objects
77 #define RSAENH_MAGIC_KEY 0x73620457u
78 #define RSAENH_MAX_KEY_SIZE 64
79 #define RSAENH_MAX_BLOCK_SIZE 24
80 #define RSAENH_KEYSTATE_IDLE 0
81 #define RSAENH_KEYSTATE_ENCRYPTING 1
82 #define RSAENH_KEYSTATE_MASTERKEY 2
83 typedef struct _RSAENH_SCHANNEL_INFO
85 SCHANNEL_ALG saEncAlg
;
86 SCHANNEL_ALG saMACAlg
;
87 CRYPT_DATA_BLOB blobClientRandom
;
88 CRYPT_DATA_BLOB blobServerRandom
;
89 } RSAENH_SCHANNEL_INFO
;
91 typedef struct tagCRYPTKEY
100 DWORD dwEffectiveKeyLen
;
105 BYTE abKeyValue
[RSAENH_MAX_KEY_SIZE
];
106 BYTE abInitVector
[RSAENH_MAX_BLOCK_SIZE
];
107 BYTE abChainVector
[RSAENH_MAX_BLOCK_SIZE
];
108 RSAENH_SCHANNEL_INFO siSChannelInfo
;
109 CRYPT_DATA_BLOB blobHmacKey
;
112 /******************************************************************************
113 * KEYCONTAINER - key containers
115 #define RSAENH_PERSONALITY_BASE 0u
116 #define RSAENH_PERSONALITY_STRONG 1u
117 #define RSAENH_PERSONALITY_ENHANCED 2u
118 #define RSAENH_PERSONALITY_SCHANNEL 3u
119 #define RSAENH_PERSONALITY_AES 4u
121 #define RSAENH_MAGIC_CONTAINER 0x26384993u
122 typedef struct tagKEYCONTAINER
128 DWORD dwEnumContainersCtr
;
129 CHAR szName
[MAX_PATH
];
130 CHAR szProvName
[MAX_PATH
];
131 HCRYPTKEY hKeyExchangeKeyPair
;
132 HCRYPTKEY hSignatureKeyPair
;
135 /******************************************************************************
136 * Some magic constants
138 #define RSAENH_ENCRYPT 1
139 #define RSAENH_DECRYPT 0
140 #define RSAENH_HMAC_DEF_IPAD_CHAR 0x36
141 #define RSAENH_HMAC_DEF_OPAD_CHAR 0x5c
142 #define RSAENH_HMAC_DEF_PAD_LEN 64
143 #define RSAENH_HMAC_BLOCK_LEN 64
144 #define RSAENH_DES_EFFECTIVE_KEYLEN 56
145 #define RSAENH_DES_STORAGE_KEYLEN 64
146 #define RSAENH_3DES112_EFFECTIVE_KEYLEN 112
147 #define RSAENH_3DES112_STORAGE_KEYLEN 128
148 #define RSAENH_3DES_EFFECTIVE_KEYLEN 168
149 #define RSAENH_3DES_STORAGE_KEYLEN 192
150 #define RSAENH_MAGIC_RSA2 0x32415352
151 #define RSAENH_MAGIC_RSA1 0x31415352
152 #define RSAENH_PKC_BLOCKTYPE 0x02
153 #define RSAENH_SSL3_VERSION_MAJOR 3
154 #define RSAENH_SSL3_VERSION_MINOR 0
155 #define RSAENH_TLS1_VERSION_MAJOR 3
156 #define RSAENH_TLS1_VERSION_MINOR 1
157 #define RSAENH_REGKEY "Software\\Wine\\Crypto\\RSA\\%s"
159 #define RSAENH_MIN(a,b) ((a)<(b)?(a):(b))
160 /******************************************************************************
161 * aProvEnumAlgsEx - Defines the capabilities of the CSP personalities.
163 #define RSAENH_MAX_ENUMALGS 24
164 #define RSAENH_PCT1_SSL2_SSL3_TLS1 (CRYPT_FLAG_PCT1|CRYPT_FLAG_SSL2|CRYPT_FLAG_SSL3|CRYPT_FLAG_TLS1)
165 static const PROV_ENUMALGS_EX aProvEnumAlgsEx
[5][RSAENH_MAX_ENUMALGS
+1] =
168 {CALG_RC2
, 40, 40, 56,0, 4,"RC2", 24,"RSA Data Security's RC2"},
169 {CALG_RC4
, 40, 40, 56,0, 4,"RC4", 24,"RSA Data Security's RC4"},
170 {CALG_DES
, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
171 {CALG_SHA
, 160,160, 160,CRYPT_FLAG_SIGNING
, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
172 {CALG_MD2
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD2", 23,"Message Digest 2 (MD2)"},
173 {CALG_MD4
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD4", 23,"Message Digest 4 (MD4)"},
174 {CALG_MD5
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD5", 23,"Message Digest 5 (MD5)"},
175 {CALG_SSL3_SHAMD5
,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
176 {CALG_MAC
, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
177 {CALG_RSA_SIGN
, 512,384,16384,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_SIGN",14,"RSA Signature"},
178 {CALG_RSA_KEYX
, 512,384, 1024,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_KEYX",17,"RSA Key Exchange"},
179 {CALG_HMAC
, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
180 {0, 0, 0, 0,0, 1,"", 1,""}
183 {CALG_RC2
, 128, 40, 128,0, 4,"RC2", 24,"RSA Data Security's RC2"},
184 {CALG_RC4
, 128, 40, 128,0, 4,"RC4", 24,"RSA Data Security's RC4"},
185 {CALG_DES
, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
186 {CALG_3DES_112
, 112,112, 112,0, 13,"3DES TWO KEY",19,"Two Key Triple DES"},
187 {CALG_3DES
, 168,168, 168,0, 5,"3DES", 21,"Three Key Triple DES"},
188 {CALG_SHA
, 160,160, 160,CRYPT_FLAG_SIGNING
, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
189 {CALG_MD2
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD2", 23,"Message Digest 2 (MD2)"},
190 {CALG_MD4
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD4", 23,"Message Digest 4 (MD4)"},
191 {CALG_MD5
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD5", 23,"Message Digest 5 (MD5)"},
192 {CALG_SSL3_SHAMD5
,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
193 {CALG_MAC
, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
194 {CALG_RSA_SIGN
,1024,384,16384,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_SIGN",14,"RSA Signature"},
195 {CALG_RSA_KEYX
,1024,384,16384,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_KEYX",17,"RSA Key Exchange"},
196 {CALG_HMAC
, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
197 {0, 0, 0, 0,0, 1,"", 1,""}
200 {CALG_RC2
, 128, 40, 128,0, 4,"RC2", 24,"RSA Data Security's RC2"},
201 {CALG_RC4
, 128, 40, 128,0, 4,"RC4", 24,"RSA Data Security's RC4"},
202 {CALG_DES
, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
203 {CALG_3DES_112
, 112,112, 112,0, 13,"3DES TWO KEY",19,"Two Key Triple DES"},
204 {CALG_3DES
, 168,168, 168,0, 5,"3DES", 21,"Three Key Triple DES"},
205 {CALG_SHA
, 160,160, 160,CRYPT_FLAG_SIGNING
, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
206 {CALG_MD2
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD2", 23,"Message Digest 2 (MD2)"},
207 {CALG_MD4
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD4", 23,"Message Digest 4 (MD4)"},
208 {CALG_MD5
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD5", 23,"Message Digest 5 (MD5)"},
209 {CALG_SSL3_SHAMD5
,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
210 {CALG_MAC
, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
211 {CALG_RSA_SIGN
,1024,384,16384,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_SIGN",14,"RSA Signature"},
212 {CALG_RSA_KEYX
,1024,384,16384,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_KEYX",17,"RSA Key Exchange"},
213 {CALG_HMAC
, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
214 {0, 0, 0, 0,0, 1,"", 1,""}
217 {CALG_RC2
, 128, 40, 128,RSAENH_PCT1_SSL2_SSL3_TLS1
, 4,"RC2", 24,"RSA Data Security's RC2"},
218 {CALG_RC4
, 128, 40, 128,RSAENH_PCT1_SSL2_SSL3_TLS1
, 4,"RC4", 24,"RSA Data Security's RC4"},
219 {CALG_DES
, 56, 56, 56,RSAENH_PCT1_SSL2_SSL3_TLS1
, 4,"DES", 31,"Data Encryption Standard (DES)"},
220 {CALG_3DES_112
, 112,112, 112,RSAENH_PCT1_SSL2_SSL3_TLS1
,13,"3DES TWO KEY",19,"Two Key Triple DES"},
221 {CALG_3DES
, 168,168, 168,RSAENH_PCT1_SSL2_SSL3_TLS1
, 5,"3DES", 21,"Three Key Triple DES"},
222 {CALG_SHA
,160,160,160,CRYPT_FLAG_SIGNING
|RSAENH_PCT1_SSL2_SSL3_TLS1
,6,"SHA-1",30,"Secure Hash Algorithm (SHA-1)"},
223 {CALG_MD5
,128,128,128,CRYPT_FLAG_SIGNING
|RSAENH_PCT1_SSL2_SSL3_TLS1
,4,"MD5",23,"Message Digest 5 (MD5)"},
224 {CALG_SSL3_SHAMD5
,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
225 {CALG_MAC
, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
226 {CALG_RSA_SIGN
,1024,384,16384,CRYPT_FLAG_SIGNING
|RSAENH_PCT1_SSL2_SSL3_TLS1
,9,"RSA_SIGN",14,"RSA Signature"},
227 {CALG_RSA_KEYX
,1024,384,16384,CRYPT_FLAG_SIGNING
|RSAENH_PCT1_SSL2_SSL3_TLS1
,9,"RSA_KEYX",17,"RSA Key Exchange"},
228 {CALG_HMAC
, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
229 {CALG_PCT1_MASTER
,128,128,128,CRYPT_FLAG_PCT1
, 12,"PCT1 MASTER",12,"PCT1 Master"},
230 {CALG_SSL2_MASTER
,40,40, 192,CRYPT_FLAG_SSL2
, 12,"SSL2 MASTER",12,"SSL2 Master"},
231 {CALG_SSL3_MASTER
,384,384,384,CRYPT_FLAG_SSL3
, 12,"SSL3 MASTER",12,"SSL3 Master"},
232 {CALG_TLS1_MASTER
,384,384,384,CRYPT_FLAG_TLS1
, 12,"TLS1 MASTER",12,"TLS1 Master"},
233 {CALG_SCHANNEL_MASTER_HASH
,0,0,-1,0, 16,"SCH MASTER HASH",21,"SChannel Master Hash"},
234 {CALG_SCHANNEL_MAC_KEY
,0,0,-1,0, 12,"SCH MAC KEY",17,"SChannel MAC Key"},
235 {CALG_SCHANNEL_ENC_KEY
,0,0,-1,0, 12,"SCH ENC KEY",24,"SChannel Encryption Key"},
236 {CALG_TLS1PRF
, 0, 0, -1,0, 9,"TLS1 PRF", 28,"TLS1 Pseudo Random Function"},
237 {0, 0, 0, 0,0, 1,"", 1,""}
240 {CALG_RC2
, 128, 40, 128,0, 4,"RC2", 24,"RSA Data Security's RC2"},
241 {CALG_RC4
, 128, 40, 128,0, 4,"RC4", 24,"RSA Data Security's RC4"},
242 {CALG_DES
, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
243 {CALG_3DES_112
, 112,112, 112,0, 13,"3DES TWO KEY",19,"Two Key Triple DES"},
244 {CALG_3DES
, 168,168, 168,0, 5,"3DES", 21,"Three Key Triple DES"},
245 {CALG_AES
, 128,128, 128,0, 4,"AES", 35,"Advanced Encryption Standard (AES)"},
246 {CALG_AES_128
, 128,128, 128,0, 8,"AES-128", 39,"Advanced Encryption Standard (AES-128)"},
247 {CALG_AES_192
, 192,192, 192,0, 8,"AES-192", 39,"Advanced Encryption Standard (AES-192)"},
248 {CALG_AES_256
, 256,256, 256,0, 8,"AES-256", 39,"Advanced Encryption Standard (AES-256)"},
249 {CALG_SHA
, 160,160, 160,CRYPT_FLAG_SIGNING
, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
250 {CALG_SHA_256
, 256,256, 256,CRYPT_FLAG_SIGNING
, 6,"SHA-256", 30,"Secure Hash Algorithm (SHA-256)"},
251 {CALG_SHA_384
, 384,384, 384,CRYPT_FLAG_SIGNING
, 6,"SHA-384", 30,"Secure Hash Algorithm (SHA-284)"},
252 {CALG_SHA_512
, 512,512, 512,CRYPT_FLAG_SIGNING
, 6,"SHA-512", 30,"Secure Hash Algorithm (SHA-512)"},
253 {CALG_MD2
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD2", 23,"Message Digest 2 (MD2)"},
254 {CALG_MD4
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD4", 23,"Message Digest 4 (MD4)"},
255 {CALG_MD5
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD5", 23,"Message Digest 5 (MD5)"},
256 {CALG_SSL3_SHAMD5
,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
257 {CALG_MAC
, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
258 {CALG_RSA_SIGN
,1024,384,16384,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_SIGN",14,"RSA Signature"},
259 {CALG_RSA_KEYX
,1024,384,16384,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_KEYX",17,"RSA Key Exchange"},
260 {CALG_HMAC
, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
261 {0, 0, 0, 0,0, 1,"", 1,""}
265 /******************************************************************************
266 * API forward declarations
269 RSAENH_CPGetKeyParam(
300 RSAENH_CPSetHashParam(
304 BYTE
*pbData
, DWORD dwFlags
308 RSAENH_CPGetHashParam(
318 RSAENH_CPDestroyHash(
323 static BOOL
crypt_export_key(
333 static BOOL
import_key(
352 /******************************************************************************
353 * CSP's handle table (used by all acquired key containers)
355 static struct handle_table handle_table
;
357 /******************************************************************************
360 * Initializes and destroys the handle table for the CSP's handles.
362 BOOL WINAPI
DllMain(HINSTANCE hInstance
, DWORD fdwReason
, PVOID reserved
)
366 case DLL_PROCESS_ATTACH
:
367 instance
= hInstance
;
368 DisableThreadLibraryCalls(hInstance
);
369 init_handle_table(&handle_table
);
372 case DLL_PROCESS_DETACH
:
374 destroy_handle_table(&handle_table
);
380 /******************************************************************************
381 * copy_param [Internal]
383 * Helper function that supports the standard WINAPI protocol for querying data
387 * pbBuffer [O] Buffer where the queried parameter is copied to, if it is large enough.
388 * May be NUL if the required buffer size is to be queried only.
389 * pdwBufferSize [I/O] In: Size of the buffer at pbBuffer
390 * Out: Size of parameter pbParam
391 * pbParam [I] Parameter value.
392 * dwParamSize [I] Size of pbParam
395 * Success: TRUE (pbParam was copied into pbBuffer or pbBuffer is NULL)
396 * Failure: FALSE (pbBuffer is not large enough to hold pbParam). Last error: ERROR_MORE_DATA
398 static inline BOOL
copy_param(BYTE
*pbBuffer
, DWORD
*pdwBufferSize
, const BYTE
*pbParam
,
403 if (dwParamSize
> *pdwBufferSize
)
405 SetLastError(ERROR_MORE_DATA
);
406 *pdwBufferSize
= dwParamSize
;
409 memcpy(pbBuffer
, pbParam
, dwParamSize
);
411 *pdwBufferSize
= dwParamSize
;
415 /******************************************************************************
416 * get_algid_info [Internal]
418 * Query CSP capabilities for a given crypto algorithm.
421 * hProv [I] Handle to a key container of the CSP whose capabilities are to be queried.
422 * algid [I] Identifier of the crypto algorithm about which information is requested.
425 * Success: Pointer to a PROV_ENUMALGS_EX struct containing information about the crypto algorithm.
426 * Failure: NULL (algid not supported)
428 static inline const PROV_ENUMALGS_EX
* get_algid_info(HCRYPTPROV hProv
, ALG_ID algid
) {
429 const PROV_ENUMALGS_EX
*iterator
;
430 KEYCONTAINER
*pKeyContainer
;
432 if (!lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
, (OBJECTHDR
**)&pKeyContainer
)) {
433 SetLastError(NTE_BAD_UID
);
437 for (iterator
= aProvEnumAlgsEx
[pKeyContainer
->dwPersonality
]; iterator
->aiAlgid
; iterator
++) {
438 if (iterator
->aiAlgid
== algid
) return iterator
;
441 SetLastError(NTE_BAD_ALGID
);
445 /******************************************************************************
446 * copy_data_blob [Internal]
448 * deeply copies a DATA_BLOB
451 * dst [O] That's where the blob will be copied to
452 * src [I] Source blob
456 * Failure: FALSE (GetLastError() == NTE_NO_MEMORY
459 * Use free_data_blob to release resources occupied by copy_data_blob.
461 static inline BOOL
copy_data_blob(PCRYPT_DATA_BLOB dst
, const PCRYPT_DATA_BLOB src
)
463 dst
->pbData
= HeapAlloc(GetProcessHeap(), 0, src
->cbData
);
465 SetLastError(NTE_NO_MEMORY
);
468 dst
->cbData
= src
->cbData
;
469 memcpy(dst
->pbData
, src
->pbData
, src
->cbData
);
473 /******************************************************************************
474 * concat_data_blobs [Internal]
476 * Concatenates two blobs
479 * dst [O] The new blob will be copied here
480 * src1 [I] Prefix blob
481 * src2 [I] Appendix blob
485 * Failure: FALSE (GetLastError() == NTE_NO_MEMORY)
488 * Release resources occupied by concat_data_blobs with free_data_blobs
490 static inline BOOL
concat_data_blobs(PCRYPT_DATA_BLOB dst
, const PCRYPT_DATA_BLOB src1
,
491 const PCRYPT_DATA_BLOB src2
)
493 dst
->cbData
= src1
->cbData
+ src2
->cbData
;
494 dst
->pbData
= HeapAlloc(GetProcessHeap(), 0, dst
->cbData
);
496 SetLastError(NTE_NO_MEMORY
);
499 memcpy(dst
->pbData
, src1
->pbData
, src1
->cbData
);
500 memcpy(dst
->pbData
+ src1
->cbData
, src2
->pbData
, src2
->cbData
);
504 /******************************************************************************
505 * free_data_blob [Internal]
507 * releases resource occupied by a dynamically allocated CRYPT_DATA_BLOB
510 * pBlob [I] Heap space occupied by pBlob->pbData is released
512 static inline void free_data_blob(PCRYPT_DATA_BLOB pBlob
) {
513 HeapFree(GetProcessHeap(), 0, pBlob
->pbData
);
516 /******************************************************************************
517 * init_data_blob [Internal]
519 static inline void init_data_blob(PCRYPT_DATA_BLOB pBlob
) {
520 pBlob
->pbData
= NULL
;
524 /******************************************************************************
525 * free_hmac_info [Internal]
527 * Deeply free an HMAC_INFO struct.
530 * hmac_info [I] Pointer to the HMAC_INFO struct to be freed.
533 * See Internet RFC 2104 for details on the HMAC algorithm.
535 static inline void free_hmac_info(PHMAC_INFO hmac_info
) {
536 if (!hmac_info
) return;
537 HeapFree(GetProcessHeap(), 0, hmac_info
->pbInnerString
);
538 HeapFree(GetProcessHeap(), 0, hmac_info
->pbOuterString
);
539 HeapFree(GetProcessHeap(), 0, hmac_info
);
542 /******************************************************************************
543 * copy_hmac_info [Internal]
545 * Deeply copy an HMAC_INFO struct
548 * dst [O] Pointer to a location where the pointer to the HMAC_INFO copy will be stored.
549 * src [I] Pointer to the HMAC_INFO struct to be copied.
556 * See Internet RFC 2104 for details on the HMAC algorithm.
558 static BOOL
copy_hmac_info(PHMAC_INFO
*dst
, const HMAC_INFO
*src
) {
559 if (!src
) return FALSE
;
560 *dst
= HeapAlloc(GetProcessHeap(), 0, sizeof(HMAC_INFO
));
561 if (!*dst
) return FALSE
;
563 (*dst
)->pbInnerString
= NULL
;
564 (*dst
)->pbOuterString
= NULL
;
565 if ((*dst
)->cbInnerString
== 0) (*dst
)->cbInnerString
= RSAENH_HMAC_DEF_PAD_LEN
;
566 (*dst
)->pbInnerString
= HeapAlloc(GetProcessHeap(), 0, (*dst
)->cbInnerString
);
567 if (!(*dst
)->pbInnerString
) {
568 free_hmac_info(*dst
);
571 if (src
->cbInnerString
)
572 memcpy((*dst
)->pbInnerString
, src
->pbInnerString
, src
->cbInnerString
);
574 memset((*dst
)->pbInnerString
, RSAENH_HMAC_DEF_IPAD_CHAR
, RSAENH_HMAC_DEF_PAD_LEN
);
575 if ((*dst
)->cbOuterString
== 0) (*dst
)->cbOuterString
= RSAENH_HMAC_DEF_PAD_LEN
;
576 (*dst
)->pbOuterString
= HeapAlloc(GetProcessHeap(), 0, (*dst
)->cbOuterString
);
577 if (!(*dst
)->pbOuterString
) {
578 free_hmac_info(*dst
);
581 if (src
->cbOuterString
)
582 memcpy((*dst
)->pbOuterString
, src
->pbOuterString
, src
->cbOuterString
);
584 memset((*dst
)->pbOuterString
, RSAENH_HMAC_DEF_OPAD_CHAR
, RSAENH_HMAC_DEF_PAD_LEN
);
588 /******************************************************************************
589 * destroy_hash [Internal]
591 * Destructor for hash objects
594 * pCryptHash [I] Pointer to the hash object to be destroyed.
595 * Will be invalid after function returns!
597 static void destroy_hash(OBJECTHDR
*pObject
)
599 CRYPTHASH
*pCryptHash
= (CRYPTHASH
*)pObject
;
601 free_hmac_info(pCryptHash
->pHMACInfo
);
602 free_data_blob(&pCryptHash
->tpPRFParams
.blobLabel
);
603 free_data_blob(&pCryptHash
->tpPRFParams
.blobSeed
);
604 HeapFree(GetProcessHeap(), 0, pCryptHash
);
607 /******************************************************************************
608 * init_hash [Internal]
610 * Initialize (or reset) a hash object
613 * pCryptHash [I] The hash object to be initialized.
615 static inline BOOL
init_hash(CRYPTHASH
*pCryptHash
) {
618 switch (pCryptHash
->aiAlgid
)
621 if (pCryptHash
->pHMACInfo
) {
622 const PROV_ENUMALGS_EX
*pAlgInfo
;
624 pAlgInfo
= get_algid_info(pCryptHash
->hProv
, pCryptHash
->pHMACInfo
->HashAlgid
);
625 if (!pAlgInfo
) return FALSE
;
626 pCryptHash
->dwHashSize
= pAlgInfo
->dwDefaultLen
>> 3;
627 init_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
);
628 update_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
629 pCryptHash
->pHMACInfo
->pbInnerString
,
630 pCryptHash
->pHMACInfo
->cbInnerString
);
635 dwLen
= sizeof(DWORD
);
636 RSAENH_CPGetKeyParam(pCryptHash
->hProv
, pCryptHash
->hKey
, KP_BLOCKLEN
,
637 (BYTE
*)&pCryptHash
->dwHashSize
, &dwLen
, 0);
638 pCryptHash
->dwHashSize
>>= 3;
642 return init_hash_impl(pCryptHash
->aiAlgid
, &pCryptHash
->context
);
646 /******************************************************************************
647 * update_hash [Internal]
649 * Hashes the given data and updates the hash object's state accordingly
652 * pCryptHash [I] Hash object to be updated.
653 * pbData [I] Pointer to data stream to be hashed.
654 * dwDataLen [I] Length of data stream.
656 static inline void update_hash(CRYPTHASH
*pCryptHash
, const BYTE
*pbData
, DWORD dwDataLen
)
660 switch (pCryptHash
->aiAlgid
)
663 if (pCryptHash
->pHMACInfo
)
664 update_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
669 pbTemp
= HeapAlloc(GetProcessHeap(), 0, dwDataLen
);
671 memcpy(pbTemp
, pbData
, dwDataLen
);
672 RSAENH_CPEncrypt(pCryptHash
->hProv
, pCryptHash
->hKey
, 0, FALSE
, 0,
673 pbTemp
, &dwDataLen
, dwDataLen
);
674 HeapFree(GetProcessHeap(), 0, pbTemp
);
678 update_hash_impl(pCryptHash
->aiAlgid
, &pCryptHash
->context
, pbData
, dwDataLen
);
682 /******************************************************************************
683 * finalize_hash [Internal]
685 * Finalizes the hash, after all data has been hashed with update_hash.
686 * No additional data can be hashed afterwards until the hash gets initialized again.
689 * pCryptHash [I] Hash object to be finalized.
691 static inline void finalize_hash(CRYPTHASH
*pCryptHash
) {
694 switch (pCryptHash
->aiAlgid
)
697 if (pCryptHash
->pHMACInfo
) {
698 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
];
700 finalize_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
701 pCryptHash
->abHashValue
);
702 memcpy(abHashValue
, pCryptHash
->abHashValue
, pCryptHash
->dwHashSize
);
703 init_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
);
704 update_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
705 pCryptHash
->pHMACInfo
->pbOuterString
,
706 pCryptHash
->pHMACInfo
->cbOuterString
);
707 update_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
708 abHashValue
, pCryptHash
->dwHashSize
);
709 finalize_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
710 pCryptHash
->abHashValue
);
716 RSAENH_CPEncrypt(pCryptHash
->hProv
, pCryptHash
->hKey
, 0, TRUE
, 0,
717 pCryptHash
->abHashValue
, &dwDataLen
, pCryptHash
->dwHashSize
);
721 finalize_hash_impl(pCryptHash
->aiAlgid
, &pCryptHash
->context
, pCryptHash
->abHashValue
);
725 /******************************************************************************
726 * destroy_key [Internal]
728 * Destructor for key objects
731 * pCryptKey [I] Pointer to the key object to be destroyed.
732 * Will be invalid after function returns!
734 static void destroy_key(OBJECTHDR
*pObject
)
736 CRYPTKEY
*pCryptKey
= (CRYPTKEY
*)pObject
;
738 free_key_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
);
739 free_data_blob(&pCryptKey
->siSChannelInfo
.blobClientRandom
);
740 free_data_blob(&pCryptKey
->siSChannelInfo
.blobServerRandom
);
741 free_data_blob(&pCryptKey
->blobHmacKey
);
742 HeapFree(GetProcessHeap(), 0, pCryptKey
);
745 /******************************************************************************
746 * setup_key [Internal]
748 * Initialize (or reset) a key object
751 * pCryptKey [I] The key object to be initialized.
753 static inline void setup_key(CRYPTKEY
*pCryptKey
) {
754 pCryptKey
->dwState
= RSAENH_KEYSTATE_IDLE
;
755 memcpy(pCryptKey
->abChainVector
, pCryptKey
->abInitVector
, sizeof(pCryptKey
->abChainVector
));
756 setup_key_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pCryptKey
->dwKeyLen
,
757 pCryptKey
->dwEffectiveKeyLen
, pCryptKey
->dwSaltLen
,
758 pCryptKey
->abKeyValue
);
761 /******************************************************************************
764 * Creates a new key object without assigning the actual binary key value.
765 * This is done by CPDeriveKey, CPGenKey or CPImportKey, which call this function.
768 * hProv [I] Handle to the provider to which the created key will belong.
769 * aiAlgid [I] The new key shall use the crypto algorithm identified by aiAlgid.
770 * dwFlags [I] Upper 16 bits give the key length.
771 * Lower 16 bits: CRYPT_EXPORTABLE, CRYPT_CREATE_SALT,
773 * ppCryptKey [O] Pointer to the created key
776 * Success: Handle to the created key.
777 * Failure: INVALID_HANDLE_VALUE
779 static HCRYPTKEY
new_key(HCRYPTPROV hProv
, ALG_ID aiAlgid
, DWORD dwFlags
, CRYPTKEY
**ppCryptKey
)
783 DWORD dwKeyLen
= HIWORD(dwFlags
), bKeyLen
= dwKeyLen
;
784 const PROV_ENUMALGS_EX
*peaAlgidInfo
;
789 * Retrieve the CSP's capabilities for the given ALG_ID value
791 peaAlgidInfo
= get_algid_info(hProv
, aiAlgid
);
792 if (!peaAlgidInfo
) return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
794 TRACE("alg = %s, dwKeyLen = %d\n", debugstr_a(peaAlgidInfo
->szName
),
797 * Assume the default key length, if none is specified explicitly
799 if (dwKeyLen
== 0) dwKeyLen
= peaAlgidInfo
->dwDefaultLen
;
802 * Check if the requested key length is supported by the current CSP.
803 * Adjust key length's for DES algorithms.
807 if (dwKeyLen
== RSAENH_DES_EFFECTIVE_KEYLEN
) {
808 dwKeyLen
= RSAENH_DES_STORAGE_KEYLEN
;
810 if (dwKeyLen
!= RSAENH_DES_STORAGE_KEYLEN
) {
811 SetLastError(NTE_BAD_FLAGS
);
812 return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
817 if (dwKeyLen
== RSAENH_3DES112_EFFECTIVE_KEYLEN
) {
818 dwKeyLen
= RSAENH_3DES112_STORAGE_KEYLEN
;
820 if (dwKeyLen
!= RSAENH_3DES112_STORAGE_KEYLEN
) {
821 SetLastError(NTE_BAD_FLAGS
);
822 return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
827 if (dwKeyLen
== RSAENH_3DES_EFFECTIVE_KEYLEN
) {
828 dwKeyLen
= RSAENH_3DES_STORAGE_KEYLEN
;
830 if (dwKeyLen
!= RSAENH_3DES_STORAGE_KEYLEN
) {
831 SetLastError(NTE_BAD_FLAGS
);
832 return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
837 /* Avoid the key length check for HMAC keys, which have unlimited
845 TRACE("missing key len for CALG_AES\n");
846 SetLastError(NTE_BAD_ALGID
);
847 return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
852 dwKeyLen
> peaAlgidInfo
->dwMaxLen
||
853 dwKeyLen
< peaAlgidInfo
->dwMinLen
)
855 TRACE("key len %d out of bounds (%d, %d)\n", dwKeyLen
,
856 peaAlgidInfo
->dwMinLen
, peaAlgidInfo
->dwMaxLen
);
857 SetLastError(NTE_BAD_DATA
);
858 return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
862 hCryptKey
= new_object(&handle_table
, sizeof(CRYPTKEY
), RSAENH_MAGIC_KEY
,
863 destroy_key
, (OBJECTHDR
**)&pCryptKey
);
864 if (hCryptKey
!= (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
866 pCryptKey
->aiAlgid
= aiAlgid
;
867 pCryptKey
->hProv
= hProv
;
868 pCryptKey
->dwModeBits
= 0;
869 pCryptKey
->dwPermissions
= CRYPT_ENCRYPT
| CRYPT_DECRYPT
| CRYPT_READ
| CRYPT_WRITE
|
871 if (dwFlags
& CRYPT_EXPORTABLE
)
872 pCryptKey
->dwPermissions
|= CRYPT_EXPORT
;
873 pCryptKey
->dwKeyLen
= dwKeyLen
>> 3;
874 pCryptKey
->dwEffectiveKeyLen
= 0;
875 if ((dwFlags
& CRYPT_CREATE_SALT
) || (dwKeyLen
== 40 && !(dwFlags
& CRYPT_NO_SALT
)))
876 pCryptKey
->dwSaltLen
= 16 /*FIXME*/ - pCryptKey
->dwKeyLen
;
878 pCryptKey
->dwSaltLen
= 0;
879 memset(pCryptKey
->abKeyValue
, 0, sizeof(pCryptKey
->abKeyValue
));
880 memset(pCryptKey
->abInitVector
, 0, sizeof(pCryptKey
->abInitVector
));
881 memset(&pCryptKey
->siSChannelInfo
.saEncAlg
, 0, sizeof(pCryptKey
->siSChannelInfo
.saEncAlg
));
882 memset(&pCryptKey
->siSChannelInfo
.saMACAlg
, 0, sizeof(pCryptKey
->siSChannelInfo
.saMACAlg
));
883 init_data_blob(&pCryptKey
->siSChannelInfo
.blobClientRandom
);
884 init_data_blob(&pCryptKey
->siSChannelInfo
.blobServerRandom
);
885 init_data_blob(&pCryptKey
->blobHmacKey
);
889 case CALG_PCT1_MASTER
:
890 case CALG_SSL2_MASTER
:
891 case CALG_SSL3_MASTER
:
892 case CALG_TLS1_MASTER
:
894 pCryptKey
->dwBlockLen
= 0;
895 pCryptKey
->dwMode
= 0;
902 pCryptKey
->dwBlockLen
= 8;
903 pCryptKey
->dwMode
= CRYPT_MODE_CBC
;
910 pCryptKey
->dwBlockLen
= 16;
911 pCryptKey
->dwMode
= CRYPT_MODE_CBC
;
916 pCryptKey
->dwBlockLen
= dwKeyLen
>> 3;
917 pCryptKey
->dwMode
= 0;
921 pCryptKey
->dwBlockLen
= 0;
922 pCryptKey
->dwMode
= 0;
926 *ppCryptKey
= pCryptKey
;
932 /******************************************************************************
933 * map_key_spec_to_key_pair_name [Internal]
935 * Returns the name of the registry value associated with a key spec.
938 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
941 * Success: Name of registry value.
944 static LPCSTR
map_key_spec_to_key_pair_name(DWORD dwKeySpec
)
951 szValueName
= "KeyExchangeKeyPair";
954 szValueName
= "SignatureKeyPair";
957 WARN("invalid key spec %d\n", dwKeySpec
);
963 /******************************************************************************
964 * store_key_pair [Internal]
966 * Stores a key pair to the registry
969 * hCryptKey [I] Handle to the key to be stored
970 * hKey [I] Registry key where the key pair is to be stored
971 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
972 * dwFlags [I] Flags for protecting the key
974 static void store_key_pair(HCRYPTKEY hCryptKey
, HKEY hKey
, DWORD dwKeySpec
, DWORD dwFlags
)
977 DATA_BLOB blobIn
, blobOut
;
982 if (!(szValueName
= map_key_spec_to_key_pair_name(dwKeySpec
)))
984 if (lookup_handle(&handle_table
, hCryptKey
, RSAENH_MAGIC_KEY
,
987 if (crypt_export_key(pKey
, 0, PRIVATEKEYBLOB
, 0, TRUE
, 0, &dwLen
))
989 pbKey
= HeapAlloc(GetProcessHeap(), 0, dwLen
);
992 if (crypt_export_key(pKey
, 0, PRIVATEKEYBLOB
, 0, TRUE
, pbKey
,
995 blobIn
.pbData
= pbKey
;
996 blobIn
.cbData
= dwLen
;
998 if (CryptProtectData(&blobIn
, NULL
, NULL
, NULL
, NULL
,
1001 RegSetValueExA(hKey
, szValueName
, 0, REG_BINARY
,
1002 blobOut
.pbData
, blobOut
.cbData
);
1003 LocalFree(blobOut
.pbData
);
1006 HeapFree(GetProcessHeap(), 0, pbKey
);
1012 /******************************************************************************
1013 * map_key_spec_to_permissions_name [Internal]
1015 * Returns the name of the registry value associated with the permissions for
1019 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
1022 * Success: Name of registry value.
1025 static LPCSTR
map_key_spec_to_permissions_name(DWORD dwKeySpec
)
1031 case AT_KEYEXCHANGE
:
1032 szValueName
= "KeyExchangePermissions";
1035 szValueName
= "SignaturePermissions";
1038 WARN("invalid key spec %d\n", dwKeySpec
);
1044 /******************************************************************************
1045 * store_key_permissions [Internal]
1047 * Stores a key's permissions to the registry
1050 * hCryptKey [I] Handle to the key whose permissions are to be stored
1051 * hKey [I] Registry key where the key permissions are to be stored
1052 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
1054 static void store_key_permissions(HCRYPTKEY hCryptKey
, HKEY hKey
, DWORD dwKeySpec
)
1059 if (!(szValueName
= map_key_spec_to_permissions_name(dwKeySpec
)))
1061 if (lookup_handle(&handle_table
, hCryptKey
, RSAENH_MAGIC_KEY
,
1062 (OBJECTHDR
**)&pKey
))
1063 RegSetValueExA(hKey
, szValueName
, 0, REG_DWORD
,
1064 (BYTE
*)&pKey
->dwPermissions
,
1065 sizeof(pKey
->dwPermissions
));
1068 /******************************************************************************
1069 * create_container_key [Internal]
1071 * Creates the registry key for a key container's persistent storage.
1074 * pKeyContainer [I] Pointer to the key container
1075 * sam [I] Desired registry access
1076 * phKey [O] Returned key
1078 static BOOL
create_container_key(KEYCONTAINER
*pKeyContainer
, REGSAM sam
, HKEY
*phKey
)
1080 CHAR szRSABase
[MAX_PATH
];
1083 sprintf(szRSABase
, RSAENH_REGKEY
, pKeyContainer
->szName
);
1085 if (pKeyContainer
->dwFlags
& CRYPT_MACHINE_KEYSET
)
1086 hRootKey
= HKEY_LOCAL_MACHINE
;
1088 hRootKey
= HKEY_CURRENT_USER
;
1090 /* @@ Wine registry key: HKLM\Software\Wine\Crypto\RSA */
1091 /* @@ Wine registry key: HKCU\Software\Wine\Crypto\RSA */
1092 return RegCreateKeyExA(hRootKey
, szRSABase
, 0, NULL
,
1093 REG_OPTION_NON_VOLATILE
, sam
, NULL
, phKey
, NULL
)
1097 /******************************************************************************
1098 * open_container_key [Internal]
1100 * Opens a key container's persistent storage for reading.
1103 * pszContainerName [I] Name of the container to be opened. May be the empty
1104 * string if the parent key of all containers is to be
1106 * dwFlags [I] Flags indicating which keyset to be opened.
1107 * phKey [O] Returned key
1109 static BOOL
open_container_key(LPCSTR pszContainerName
, DWORD dwFlags
, REGSAM access
, HKEY
*phKey
)
1111 CHAR szRSABase
[MAX_PATH
];
1114 sprintf(szRSABase
, RSAENH_REGKEY
, pszContainerName
);
1116 if (dwFlags
& CRYPT_MACHINE_KEYSET
)
1117 hRootKey
= HKEY_LOCAL_MACHINE
;
1119 hRootKey
= HKEY_CURRENT_USER
;
1121 /* @@ Wine registry key: HKLM\Software\Wine\Crypto\RSA */
1122 /* @@ Wine registry key: HKCU\Software\Wine\Crypto\RSA */
1123 return RegOpenKeyExA(hRootKey
, szRSABase
, 0, access
, phKey
) ==
1127 /******************************************************************************
1128 * delete_container_key [Internal]
1130 * Deletes a key container's persistent storage.
1133 * pszContainerName [I] Name of the container to be opened.
1134 * dwFlags [I] Flags indicating which keyset to be opened.
1136 static BOOL
delete_container_key(LPCSTR pszContainerName
, DWORD dwFlags
)
1138 CHAR szRegKey
[MAX_PATH
];
1140 if (snprintf(szRegKey
, MAX_PATH
, RSAENH_REGKEY
, pszContainerName
) >= MAX_PATH
) {
1141 SetLastError(NTE_BAD_KEYSET_PARAM
);
1145 if (dwFlags
& CRYPT_MACHINE_KEYSET
)
1146 hRootKey
= HKEY_LOCAL_MACHINE
;
1148 hRootKey
= HKEY_CURRENT_USER
;
1149 if (!RegDeleteKeyA(hRootKey
, szRegKey
)) {
1150 SetLastError(ERROR_SUCCESS
);
1153 SetLastError(NTE_BAD_KEYSET
);
1159 /******************************************************************************
1160 * store_key_container_keys [Internal]
1162 * Stores key container's keys in a persistent location.
1165 * pKeyContainer [I] Pointer to the key container whose keys are to be saved
1167 static void store_key_container_keys(KEYCONTAINER
*pKeyContainer
)
1172 /* On WinXP, persistent keys are stored in a file located at:
1173 * $AppData$\\Microsoft\\Crypto\\RSA\\$SID$\\some_hex_string
1176 if (pKeyContainer
->dwFlags
& CRYPT_MACHINE_KEYSET
)
1177 dwFlags
= CRYPTPROTECT_LOCAL_MACHINE
;
1181 if (create_container_key(pKeyContainer
, KEY_WRITE
, &hKey
))
1183 store_key_pair(pKeyContainer
->hKeyExchangeKeyPair
, hKey
,
1184 AT_KEYEXCHANGE
, dwFlags
);
1185 store_key_pair(pKeyContainer
->hSignatureKeyPair
, hKey
,
1186 AT_SIGNATURE
, dwFlags
);
1191 /******************************************************************************
1192 * store_key_container_permissions [Internal]
1194 * Stores key container's key permissions in a persistent location.
1197 * pKeyContainer [I] Pointer to the key container whose key permissions are to
1200 static void store_key_container_permissions(KEYCONTAINER
*pKeyContainer
)
1204 if (create_container_key(pKeyContainer
, KEY_WRITE
, &hKey
))
1206 store_key_permissions(pKeyContainer
->hKeyExchangeKeyPair
, hKey
,
1208 store_key_permissions(pKeyContainer
->hSignatureKeyPair
, hKey
,
1214 /******************************************************************************
1215 * release_key_container_keys [Internal]
1217 * Releases key container's keys.
1220 * pKeyContainer [I] Pointer to the key container whose keys are to be released.
1222 static void release_key_container_keys(KEYCONTAINER
*pKeyContainer
)
1224 release_handle(&handle_table
, pKeyContainer
->hKeyExchangeKeyPair
,
1226 release_handle(&handle_table
, pKeyContainer
->hSignatureKeyPair
,
1230 /******************************************************************************
1231 * destroy_key_container [Internal]
1233 * Destructor for key containers.
1236 * pObjectHdr [I] Pointer to the key container to be destroyed.
1238 static void destroy_key_container(OBJECTHDR
*pObjectHdr
)
1240 KEYCONTAINER
*pKeyContainer
= (KEYCONTAINER
*)pObjectHdr
;
1242 if (!(pKeyContainer
->dwFlags
& CRYPT_VERIFYCONTEXT
))
1244 store_key_container_keys(pKeyContainer
);
1245 store_key_container_permissions(pKeyContainer
);
1246 release_key_container_keys(pKeyContainer
);
1249 release_key_container_keys(pKeyContainer
);
1250 HeapFree( GetProcessHeap(), 0, pKeyContainer
);
1253 /******************************************************************************
1254 * new_key_container [Internal]
1256 * Create a new key container. The personality (RSA Base, Strong or Enhanced CP)
1257 * of the CSP is determined via the pVTable->pszProvName string.
1260 * pszContainerName [I] Name of the key container.
1261 * pVTable [I] Callback functions and context info provided by the OS
1264 * Success: Handle to the new key container.
1265 * Failure: INVALID_HANDLE_VALUE
1267 static HCRYPTPROV
new_key_container(PCCH pszContainerName
, DWORD dwFlags
, const VTableProvStruc
*pVTable
)
1269 KEYCONTAINER
*pKeyContainer
;
1270 HCRYPTPROV hKeyContainer
;
1272 hKeyContainer
= new_object(&handle_table
, sizeof(KEYCONTAINER
), RSAENH_MAGIC_CONTAINER
,
1273 destroy_key_container
, (OBJECTHDR
**)&pKeyContainer
);
1274 if (hKeyContainer
!= (HCRYPTPROV
)INVALID_HANDLE_VALUE
)
1276 lstrcpynA(pKeyContainer
->szName
, pszContainerName
, MAX_PATH
);
1277 pKeyContainer
->dwFlags
= dwFlags
;
1278 pKeyContainer
->dwEnumAlgsCtr
= 0;
1279 pKeyContainer
->hKeyExchangeKeyPair
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
1280 pKeyContainer
->hSignatureKeyPair
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
1281 if (pVTable
&& pVTable
->pszProvName
) {
1282 lstrcpynA(pKeyContainer
->szProvName
, pVTable
->pszProvName
, MAX_PATH
);
1283 if (!strcmp(pVTable
->pszProvName
, MS_DEF_PROV_A
)) {
1284 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_BASE
;
1285 } else if (!strcmp(pVTable
->pszProvName
, MS_ENHANCED_PROV_A
)) {
1286 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_ENHANCED
;
1287 } else if (!strcmp(pVTable
->pszProvName
, MS_DEF_RSA_SCHANNEL_PROV_A
)) {
1288 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_SCHANNEL
;
1289 } else if (!strcmp(pVTable
->pszProvName
, MS_ENH_RSA_AES_PROV_A
)) {
1290 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_AES
;
1292 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_STRONG
;
1296 /* The new key container has to be inserted into the CSP immediately
1297 * after creation to be available for CPGetProvParam's PP_ENUMCONTAINERS. */
1298 if (!(dwFlags
& CRYPT_VERIFYCONTEXT
)) {
1301 if (create_container_key(pKeyContainer
, KEY_WRITE
, &hKey
))
1306 return hKeyContainer
;
1309 /******************************************************************************
1310 * read_key_value [Internal]
1312 * Reads a key pair value from the registry
1315 * hKeyContainer [I] Crypt provider to use to import the key
1316 * hKey [I] Registry key from which to read the key pair
1317 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
1318 * dwFlags [I] Flags for unprotecting the key
1319 * phCryptKey [O] Returned key
1321 static BOOL
read_key_value(HCRYPTPROV hKeyContainer
, HKEY hKey
, DWORD dwKeySpec
, DWORD dwFlags
, HCRYPTKEY
*phCryptKey
)
1324 DWORD dwValueType
, dwLen
;
1326 DATA_BLOB blobIn
, blobOut
;
1329 if (!(szValueName
= map_key_spec_to_key_pair_name(dwKeySpec
)))
1331 if (RegQueryValueExA(hKey
, szValueName
, 0, &dwValueType
, NULL
, &dwLen
) ==
1334 pbKey
= HeapAlloc(GetProcessHeap(), 0, dwLen
);
1337 if (RegQueryValueExA(hKey
, szValueName
, 0, &dwValueType
, pbKey
, &dwLen
) ==
1340 blobIn
.pbData
= pbKey
;
1341 blobIn
.cbData
= dwLen
;
1343 if (CryptUnprotectData(&blobIn
, NULL
, NULL
, NULL
, NULL
,
1346 ret
= import_key(hKeyContainer
, blobOut
.pbData
, blobOut
.cbData
, 0, 0,
1348 LocalFree(blobOut
.pbData
);
1351 HeapFree(GetProcessHeap(), 0, pbKey
);
1358 if (lookup_handle(&handle_table
, *phCryptKey
, RSAENH_MAGIC_KEY
,
1359 (OBJECTHDR
**)&pKey
))
1361 if ((szValueName
= map_key_spec_to_permissions_name(dwKeySpec
)))
1363 dwLen
= sizeof(pKey
->dwPermissions
);
1364 RegQueryValueExA(hKey
, szValueName
, 0, NULL
,
1365 (BYTE
*)&pKey
->dwPermissions
, &dwLen
);
1372 /******************************************************************************
1373 * read_key_container [Internal]
1375 * Tries to read the persistent state of the key container (mainly the signature
1376 * and key exchange private keys) given by pszContainerName.
1379 * pszContainerName [I] Name of the key container to read from the registry
1380 * pVTable [I] Pointer to context data provided by the operating system
1383 * Success: Handle to the key container read from the registry
1384 * Failure: INVALID_HANDLE_VALUE
1386 static HCRYPTPROV
read_key_container(PCHAR pszContainerName
, DWORD dwFlags
, const VTableProvStruc
*pVTable
)
1389 KEYCONTAINER
*pKeyContainer
;
1390 HCRYPTPROV hKeyContainer
;
1391 HCRYPTKEY hCryptKey
;
1393 if (!open_container_key(pszContainerName
, dwFlags
, KEY_READ
, &hKey
))
1395 SetLastError(NTE_BAD_KEYSET
);
1396 return (HCRYPTPROV
)INVALID_HANDLE_VALUE
;
1399 hKeyContainer
= new_key_container(pszContainerName
, dwFlags
, pVTable
);
1400 if (hKeyContainer
!= (HCRYPTPROV
)INVALID_HANDLE_VALUE
)
1402 DWORD dwProtectFlags
= (dwFlags
& CRYPT_MACHINE_KEYSET
) ?
1403 CRYPTPROTECT_LOCAL_MACHINE
: 0;
1405 if (!lookup_handle(&handle_table
, hKeyContainer
, RSAENH_MAGIC_CONTAINER
,
1406 (OBJECTHDR
**)&pKeyContainer
))
1407 return (HCRYPTPROV
)INVALID_HANDLE_VALUE
;
1409 /* read_key_value calls import_key, which calls import_private_key,
1410 * which implicitly installs the key value into the appropriate key
1411 * container key. Thus the ref count is incremented twice, once for
1412 * the output key value, and once for the implicit install, and needs
1413 * to be decremented to balance the two.
1415 if (read_key_value(hKeyContainer
, hKey
, AT_KEYEXCHANGE
,
1416 dwProtectFlags
, &hCryptKey
))
1417 release_handle(&handle_table
, hCryptKey
, RSAENH_MAGIC_KEY
);
1418 if (read_key_value(hKeyContainer
, hKey
, AT_SIGNATURE
,
1419 dwProtectFlags
, &hCryptKey
))
1420 release_handle(&handle_table
, hCryptKey
, RSAENH_MAGIC_KEY
);
1423 return hKeyContainer
;
1426 /******************************************************************************
1427 * build_hash_signature [Internal]
1429 * Builds a padded version of a hash to match the length of the RSA key modulus.
1432 * pbSignature [O] The padded hash object is stored here.
1433 * dwLen [I] Length of the pbSignature buffer.
1434 * aiAlgid [I] Algorithm identifier of the hash to be padded.
1435 * abHashValue [I] The value of the hash object.
1436 * dwHashLen [I] Length of the hash value.
1437 * dwFlags [I] Selection of padding algorithm.
1441 * Failure: FALSE (NTE_BAD_ALGID)
1443 static BOOL
build_hash_signature(BYTE
*pbSignature
, DWORD dwLen
, ALG_ID aiAlgid
,
1444 const BYTE
*abHashValue
, DWORD dwHashLen
, DWORD dwFlags
)
1446 /* These prefixes are meant to be concatenated with hash values of the
1447 * respective kind to form a PKCS #7 DigestInfo. */
1448 static const struct tagOIDDescriptor
{
1451 const BYTE abOID
[19];
1452 } aOIDDescriptor
[] = {
1453 { CALG_MD2
, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1454 0x86, 0xf7, 0x0d, 0x02, 0x02, 0x05, 0x00, 0x04, 0x10 } },
1455 { CALG_MD4
, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1456 0x86, 0xf7, 0x0d, 0x02, 0x04, 0x05, 0x00, 0x04, 0x10 } },
1457 { CALG_MD5
, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1458 0x86, 0xf7, 0x0d, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10 } },
1459 { CALG_SHA
, 15, { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03,
1460 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14 } },
1461 { CALG_SHA_256
, 19, { 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
1462 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01,
1463 0x05, 0x00, 0x04, 0x20 } },
1464 { CALG_SHA_384
, 19, { 0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
1465 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01,
1466 0x05, 0x00, 0x04, 0x30 } },
1467 { CALG_SHA_384
, 19, { 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
1468 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01,
1469 0x05, 0x00, 0x04, 0x40 } },
1470 { CALG_SSL3_SHAMD5
, 0, { 0 } },
1473 DWORD dwIdxOID
, i
, j
;
1475 for (dwIdxOID
= 0; aOIDDescriptor
[dwIdxOID
].aiAlgid
; dwIdxOID
++) {
1476 if (aOIDDescriptor
[dwIdxOID
].aiAlgid
== aiAlgid
) break;
1479 if (!aOIDDescriptor
[dwIdxOID
].aiAlgid
) {
1480 SetLastError(NTE_BAD_ALGID
);
1484 /* Build the padded signature */
1485 if (dwFlags
& CRYPT_X931_FORMAT
) {
1486 pbSignature
[0] = 0x6b;
1487 for (i
=1; i
< dwLen
- dwHashLen
- 3; i
++) {
1488 pbSignature
[i
] = 0xbb;
1490 pbSignature
[i
++] = 0xba;
1491 for (j
=0; j
< dwHashLen
; j
++, i
++) {
1492 pbSignature
[i
] = abHashValue
[j
];
1494 pbSignature
[i
++] = 0x33;
1495 pbSignature
[i
++] = 0xcc;
1497 pbSignature
[0] = 0x00;
1498 pbSignature
[1] = 0x01;
1499 if (dwFlags
& CRYPT_NOHASHOID
) {
1500 for (i
=2; i
< dwLen
- 1 - dwHashLen
; i
++) {
1501 pbSignature
[i
] = 0xff;
1503 pbSignature
[i
++] = 0x00;
1505 for (i
=2; i
< dwLen
- 1 - aOIDDescriptor
[dwIdxOID
].dwLen
- dwHashLen
; i
++) {
1506 pbSignature
[i
] = 0xff;
1508 pbSignature
[i
++] = 0x00;
1509 for (j
=0; j
< aOIDDescriptor
[dwIdxOID
].dwLen
; j
++) {
1510 pbSignature
[i
++] = aOIDDescriptor
[dwIdxOID
].abOID
[j
];
1513 for (j
=0; j
< dwHashLen
; j
++) {
1514 pbSignature
[i
++] = abHashValue
[j
];
1521 /******************************************************************************
1524 * This is an implementation of the 'P_hash' helper function for TLS1's PRF.
1525 * It is used exclusively by tls1_prf. For details see RFC 2246, chapter 5.
1526 * The pseudo random stream generated by this function is exclusive or'ed with
1527 * the data in pbBuffer.
1530 * hHMAC [I] HMAC object, which will be used in pseudo random generation
1531 * pblobSeed [I] Seed value
1532 * pbBuffer [I/O] Pseudo random stream will be xor'ed to the provided data
1533 * dwBufferLen [I] Number of pseudo random bytes desired
1539 static BOOL
tls1_p(HCRYPTHASH hHMAC
, const PCRYPT_DATA_BLOB pblobSeed
, BYTE
*pbBuffer
,
1543 BYTE abAi
[RSAENH_MAX_HASH_SIZE
];
1546 if (!lookup_handle(&handle_table
, hHMAC
, RSAENH_MAGIC_HASH
, (OBJECTHDR
**)&pHMAC
)) {
1547 SetLastError(NTE_BAD_HASH
);
1551 /* compute A_1 = HMAC(seed) */
1553 update_hash(pHMAC
, pblobSeed
->pbData
, pblobSeed
->cbData
);
1554 finalize_hash(pHMAC
);
1555 memcpy(abAi
, pHMAC
->abHashValue
, pHMAC
->dwHashSize
);
1558 /* compute HMAC(A_i + seed) */
1560 update_hash(pHMAC
, abAi
, pHMAC
->dwHashSize
);
1561 update_hash(pHMAC
, pblobSeed
->pbData
, pblobSeed
->cbData
);
1562 finalize_hash(pHMAC
);
1564 /* pseudo random stream := CONCAT_{i=1..n} ( HMAC(A_i + seed) ) */
1566 if (i
>= dwBufferLen
) break;
1567 pbBuffer
[i
] ^= pHMAC
->abHashValue
[i
% pHMAC
->dwHashSize
];
1569 } while (i
% pHMAC
->dwHashSize
);
1571 /* compute A_{i+1} = HMAC(A_i) */
1573 update_hash(pHMAC
, abAi
, pHMAC
->dwHashSize
);
1574 finalize_hash(pHMAC
);
1575 memcpy(abAi
, pHMAC
->abHashValue
, pHMAC
->dwHashSize
);
1576 } while (i
< dwBufferLen
);
1581 /******************************************************************************
1582 * tls1_prf [Internal]
1584 * TLS1 pseudo random function as specified in RFC 2246, chapter 5
1587 * hProv [I] Key container used to compute the pseudo random stream
1588 * hSecret [I] Key that holds the (pre-)master secret
1589 * pblobLabel [I] Descriptive label
1590 * pblobSeed [I] Seed value
1591 * pbBuffer [O] Pseudo random numbers will be stored here
1592 * dwBufferLen [I] Number of pseudo random bytes desired
1598 static BOOL
tls1_prf(HCRYPTPROV hProv
, HCRYPTPROV hSecret
, const PCRYPT_DATA_BLOB pblobLabel
,
1599 const PCRYPT_DATA_BLOB pblobSeed
, BYTE
*pbBuffer
, DWORD dwBufferLen
)
1601 HMAC_INFO hmacInfo
= { 0, NULL
, 0, NULL
, 0 };
1602 HCRYPTHASH hHMAC
= (HCRYPTHASH
)INVALID_HANDLE_VALUE
;
1603 HCRYPTKEY hHalfSecret
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
1604 CRYPTKEY
*pHalfSecret
, *pSecret
;
1605 DWORD dwHalfSecretLen
;
1606 BOOL result
= FALSE
;
1607 CRYPT_DATA_BLOB blobLabelSeed
;
1609 TRACE("(hProv=%08lx, hSecret=%08lx, pblobLabel=%p, pblobSeed=%p, pbBuffer=%p, dwBufferLen=%d)\n",
1610 hProv
, hSecret
, pblobLabel
, pblobSeed
, pbBuffer
, dwBufferLen
);
1612 if (!lookup_handle(&handle_table
, hSecret
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pSecret
)) {
1613 SetLastError(NTE_FAIL
);
1617 dwHalfSecretLen
= (pSecret
->dwKeyLen
+1)/2;
1619 /* concatenation of the label and the seed */
1620 if (!concat_data_blobs(&blobLabelSeed
, pblobLabel
, pblobSeed
)) goto exit
;
1622 /* zero out the buffer, since two random streams will be xor'ed into it. */
1623 memset(pbBuffer
, 0, dwBufferLen
);
1625 /* build a 'fake' key, to hold the secret. CALG_SSL2_MASTER is used since it provides
1626 * the biggest range of valid key lengths. */
1627 hHalfSecret
= new_key(hProv
, CALG_SSL2_MASTER
, MAKELONG(0,dwHalfSecretLen
*8), &pHalfSecret
);
1628 if (hHalfSecret
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) goto exit
;
1630 /* Derive an HMAC_MD5 hash and call the helper function. */
1631 memcpy(pHalfSecret
->abKeyValue
, pSecret
->abKeyValue
, dwHalfSecretLen
);
1632 if (!RSAENH_CPCreateHash(hProv
, CALG_HMAC
, hHalfSecret
, 0, &hHMAC
)) goto exit
;
1633 hmacInfo
.HashAlgid
= CALG_MD5
;
1634 if (!RSAENH_CPSetHashParam(hProv
, hHMAC
, HP_HMAC_INFO
, (BYTE
*)&hmacInfo
, 0)) goto exit
;
1635 if (!tls1_p(hHMAC
, &blobLabelSeed
, pbBuffer
, dwBufferLen
)) goto exit
;
1637 /* Reconfigure to HMAC_SHA hash and call helper function again. */
1638 memcpy(pHalfSecret
->abKeyValue
, pSecret
->abKeyValue
+ (pSecret
->dwKeyLen
/2), dwHalfSecretLen
);
1639 hmacInfo
.HashAlgid
= CALG_SHA
;
1640 if (!RSAENH_CPSetHashParam(hProv
, hHMAC
, HP_HMAC_INFO
, (BYTE
*)&hmacInfo
, 0)) goto exit
;
1641 if (!tls1_p(hHMAC
, &blobLabelSeed
, pbBuffer
, dwBufferLen
)) goto exit
;
1645 release_handle(&handle_table
, hHalfSecret
, RSAENH_MAGIC_KEY
);
1646 if (hHMAC
!= (HCRYPTHASH
)INVALID_HANDLE_VALUE
) RSAENH_CPDestroyHash(hProv
, hHMAC
);
1647 free_data_blob(&blobLabelSeed
);
1651 /******************************************************************************
1652 * pad_data [Internal]
1654 * Helper function for data padding according to PKCS1 #2
1657 * abData [I] The data to be padded
1658 * dwDataLen [I] Length of the data
1659 * abBuffer [O] Padded data will be stored here
1660 * dwBufferLen [I] Length of the buffer (also length of padded data)
1661 * dwFlags [I] Padding format (CRYPT_SSL2_FALLBACK)
1665 * Failure: FALSE (NTE_BAD_LEN, too much data to pad)
1667 static BOOL
pad_data(const BYTE
*abData
, DWORD dwDataLen
, BYTE
*abBuffer
, DWORD dwBufferLen
,
1672 /* Ensure there is enough space for PKCS1 #2 padding */
1673 if (dwDataLen
> dwBufferLen
-11) {
1674 SetLastError(NTE_BAD_LEN
);
1678 memmove(abBuffer
+ dwBufferLen
- dwDataLen
, abData
, dwDataLen
);
1681 abBuffer
[1] = RSAENH_PKC_BLOCKTYPE
;
1682 for (i
=2; i
< dwBufferLen
- dwDataLen
- 1; i
++)
1683 do gen_rand_impl(&abBuffer
[i
], 1); while (!abBuffer
[i
]);
1684 if (dwFlags
& CRYPT_SSL2_FALLBACK
)
1685 for (i
-=8; i
< dwBufferLen
- dwDataLen
- 1; i
++)
1692 /******************************************************************************
1693 * unpad_data [Internal]
1695 * Remove the PKCS1 padding from RSA decrypted data
1698 * abData [I] The padded data
1699 * dwDataLen [I] Length of the padded data
1700 * abBuffer [O] Data without padding will be stored here
1701 * dwBufferLen [I/O] I: Length of the buffer, O: Length of unpadded data
1702 * dwFlags [I] Currently none defined
1706 * Failure: FALSE, (NTE_BAD_DATA, no valid PKCS1 padding or buffer too small)
1708 static BOOL
unpad_data(const BYTE
*abData
, DWORD dwDataLen
, BYTE
*abBuffer
, DWORD
*dwBufferLen
,
1715 SetLastError(NTE_BAD_DATA
);
1718 for (i
=2; i
<dwDataLen
; i
++)
1722 if ((i
== dwDataLen
) || (*dwBufferLen
< dwDataLen
- i
- 1) ||
1723 (abData
[0] != 0x00) || (abData
[1] != RSAENH_PKC_BLOCKTYPE
))
1725 SetLastError(NTE_BAD_DATA
);
1729 *dwBufferLen
= dwDataLen
- i
- 1;
1730 memmove(abBuffer
, abData
+ i
+ 1, *dwBufferLen
);
1734 /******************************************************************************
1735 * CPAcquireContext (RSAENH.@)
1737 * Acquire a handle to the key container specified by pszContainer
1740 * phProv [O] Pointer to the location the acquired handle will be written to.
1741 * pszContainer [I] Name of the desired key container. See Notes
1742 * dwFlags [I] Flags. See Notes.
1743 * pVTable [I] Pointer to a PVTableProvStruct containing callbacks.
1750 * If pszContainer is NULL or points to a zero length string the user's login
1751 * name will be used as the key container name.
1753 * If the CRYPT_NEW_KEYSET flag is set in dwFlags a new keyset will be created.
1754 * If a keyset with the given name already exists, the function fails and sets
1755 * last error to NTE_EXISTS. If CRYPT_NEW_KEYSET is not set and the specified
1756 * key container does not exist, function fails and sets last error to
1759 BOOL WINAPI
RSAENH_CPAcquireContext(HCRYPTPROV
*phProv
, LPSTR pszContainer
,
1760 DWORD dwFlags
, PVTableProvStruc pVTable
)
1762 CHAR szKeyContainerName
[MAX_PATH
];
1764 TRACE("(phProv=%p, pszContainer=%s, dwFlags=%08x, pVTable=%p)\n", phProv
,
1765 debugstr_a(pszContainer
), dwFlags
, pVTable
);
1767 if (pszContainer
&& *pszContainer
)
1769 lstrcpynA(szKeyContainerName
, pszContainer
, MAX_PATH
);
1773 DWORD dwLen
= sizeof(szKeyContainerName
);
1774 if (!GetUserNameA(szKeyContainerName
, &dwLen
)) return FALSE
;
1777 switch (dwFlags
& (CRYPT_NEWKEYSET
|CRYPT_VERIFYCONTEXT
|CRYPT_DELETEKEYSET
))
1780 *phProv
= read_key_container(szKeyContainerName
, dwFlags
, pVTable
);
1783 case CRYPT_DELETEKEYSET
:
1784 return delete_container_key(szKeyContainerName
, dwFlags
);
1786 case CRYPT_NEWKEYSET
:
1787 *phProv
= read_key_container(szKeyContainerName
, dwFlags
, pVTable
);
1788 if (*phProv
!= (HCRYPTPROV
)INVALID_HANDLE_VALUE
)
1790 release_handle(&handle_table
, *phProv
, RSAENH_MAGIC_CONTAINER
);
1791 TRACE("Can't create new keyset, already exists\n");
1792 SetLastError(NTE_EXISTS
);
1795 *phProv
= new_key_container(szKeyContainerName
, dwFlags
, pVTable
);
1798 case CRYPT_VERIFYCONTEXT
|CRYPT_NEWKEYSET
:
1799 case CRYPT_VERIFYCONTEXT
:
1800 if (pszContainer
&& *pszContainer
) {
1801 TRACE("pszContainer should be empty\n");
1802 SetLastError(NTE_BAD_FLAGS
);
1805 *phProv
= new_key_container("", dwFlags
, pVTable
);
1809 *phProv
= (HCRYPTPROV
)INVALID_HANDLE_VALUE
;
1810 SetLastError(NTE_BAD_FLAGS
);
1814 if (*phProv
!= (HCRYPTPROV
)INVALID_HANDLE_VALUE
) {
1815 SetLastError(ERROR_SUCCESS
);
1822 /******************************************************************************
1823 * CPCreateHash (RSAENH.@)
1825 * CPCreateHash creates and initializes a new hash object.
1828 * hProv [I] Handle to the key container to which the new hash will belong.
1829 * Algid [I] Identifies the hash algorithm, which will be used for the hash.
1830 * hKey [I] Handle to a session key applied for keyed hashes.
1831 * dwFlags [I] Currently no flags defined. Must be zero.
1832 * phHash [O] Points to the location where a handle to the new hash will be stored.
1839 * hKey is a handle to a session key applied in keyed hashes like MAC and HMAC.
1840 * If a normal hash object is to be created (like e.g. MD2 or SHA1) hKey must be zero.
1842 BOOL WINAPI
RSAENH_CPCreateHash(HCRYPTPROV hProv
, ALG_ID Algid
, HCRYPTKEY hKey
, DWORD dwFlags
,
1845 CRYPTKEY
*pCryptKey
;
1846 CRYPTHASH
*pCryptHash
;
1847 const PROV_ENUMALGS_EX
*peaAlgidInfo
;
1849 TRACE("(hProv=%08lx, Algid=%08x, hKey=%08lx, dwFlags=%08x, phHash=%p)\n", hProv
, Algid
, hKey
,
1852 peaAlgidInfo
= get_algid_info(hProv
, Algid
);
1853 if (!peaAlgidInfo
) return FALSE
;
1857 SetLastError(NTE_BAD_FLAGS
);
1861 if (Algid
== CALG_MAC
|| Algid
== CALG_HMAC
|| Algid
== CALG_SCHANNEL_MASTER_HASH
||
1862 Algid
== CALG_TLS1PRF
)
1864 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
)) {
1865 SetLastError(NTE_BAD_KEY
);
1869 if ((Algid
== CALG_MAC
) && (GET_ALG_TYPE(pCryptKey
->aiAlgid
) != ALG_TYPE_BLOCK
)) {
1870 SetLastError(NTE_BAD_KEY
);
1874 if ((Algid
== CALG_SCHANNEL_MASTER_HASH
|| Algid
== CALG_TLS1PRF
) &&
1875 (pCryptKey
->aiAlgid
!= CALG_TLS1_MASTER
))
1877 SetLastError(NTE_BAD_KEY
);
1880 if (Algid
== CALG_SCHANNEL_MASTER_HASH
&&
1881 ((!pCryptKey
->siSChannelInfo
.blobClientRandom
.cbData
) ||
1882 (!pCryptKey
->siSChannelInfo
.blobServerRandom
.cbData
)))
1884 SetLastError(ERROR_INVALID_PARAMETER
);
1888 if ((Algid
== CALG_TLS1PRF
) && (pCryptKey
->dwState
!= RSAENH_KEYSTATE_MASTERKEY
)) {
1889 SetLastError(NTE_BAD_KEY_STATE
);
1894 *phHash
= new_object(&handle_table
, sizeof(CRYPTHASH
), RSAENH_MAGIC_HASH
,
1895 destroy_hash
, (OBJECTHDR
**)&pCryptHash
);
1896 if (!pCryptHash
) return FALSE
;
1898 pCryptHash
->aiAlgid
= Algid
;
1899 pCryptHash
->hKey
= hKey
;
1900 pCryptHash
->hProv
= hProv
;
1901 pCryptHash
->dwState
= RSAENH_HASHSTATE_HASHING
;
1902 pCryptHash
->pHMACInfo
= NULL
;
1903 pCryptHash
->dwHashSize
= peaAlgidInfo
->dwDefaultLen
>> 3;
1904 init_data_blob(&pCryptHash
->tpPRFParams
.blobLabel
);
1905 init_data_blob(&pCryptHash
->tpPRFParams
.blobSeed
);
1907 if (Algid
== CALG_SCHANNEL_MASTER_HASH
) {
1908 static const char keyex
[] = "key expansion";
1909 BYTE key_expansion
[sizeof keyex
];
1910 CRYPT_DATA_BLOB blobRandom
, blobKeyExpansion
= { 13, key_expansion
};
1912 memcpy( key_expansion
, keyex
, sizeof keyex
);
1914 if (pCryptKey
->dwState
!= RSAENH_KEYSTATE_MASTERKEY
) {
1915 static const char msec
[] = "master secret";
1916 BYTE master_secret
[sizeof msec
];
1917 CRYPT_DATA_BLOB blobLabel
= { 13, master_secret
};
1918 BYTE abKeyValue
[48];
1920 memcpy( master_secret
, msec
, sizeof msec
);
1922 /* See RFC 2246, chapter 8.1 */
1923 if (!concat_data_blobs(&blobRandom
,
1924 &pCryptKey
->siSChannelInfo
.blobClientRandom
,
1925 &pCryptKey
->siSChannelInfo
.blobServerRandom
))
1929 tls1_prf(hProv
, hKey
, &blobLabel
, &blobRandom
, abKeyValue
, 48);
1930 pCryptKey
->dwState
= RSAENH_KEYSTATE_MASTERKEY
;
1931 memcpy(pCryptKey
->abKeyValue
, abKeyValue
, 48);
1932 free_data_blob(&blobRandom
);
1935 /* See RFC 2246, chapter 6.3 */
1936 if (!concat_data_blobs(&blobRandom
,
1937 &pCryptKey
->siSChannelInfo
.blobServerRandom
,
1938 &pCryptKey
->siSChannelInfo
.blobClientRandom
))
1942 tls1_prf(hProv
, hKey
, &blobKeyExpansion
, &blobRandom
, pCryptHash
->abHashValue
,
1943 RSAENH_MAX_HASH_SIZE
);
1944 free_data_blob(&blobRandom
);
1947 return init_hash(pCryptHash
);
1950 /******************************************************************************
1951 * CPDestroyHash (RSAENH.@)
1953 * Releases the handle to a hash object. The object is destroyed if its reference
1954 * count reaches zero.
1957 * hProv [I] Handle to the key container to which the hash object belongs.
1958 * hHash [I] Handle to the hash object to be released.
1964 BOOL WINAPI
RSAENH_CPDestroyHash(HCRYPTPROV hProv
, HCRYPTHASH hHash
)
1966 TRACE("(hProv=%08lx, hHash=%08lx)\n", hProv
, hHash
);
1968 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
1970 SetLastError(NTE_BAD_UID
);
1974 if (!release_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
))
1976 SetLastError(NTE_BAD_HASH
);
1983 /******************************************************************************
1984 * CPDestroyKey (RSAENH.@)
1986 * Releases the handle to a key object. The object is destroyed if its reference
1987 * count reaches zero.
1990 * hProv [I] Handle to the key container to which the key object belongs.
1991 * hKey [I] Handle to the key object to be released.
1997 BOOL WINAPI
RSAENH_CPDestroyKey(HCRYPTPROV hProv
, HCRYPTKEY hKey
)
1999 TRACE("(hProv=%08lx, hKey=%08lx)\n", hProv
, hKey
);
2001 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
2003 SetLastError(NTE_BAD_UID
);
2007 if (!release_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
))
2009 SetLastError(NTE_BAD_KEY
);
2016 /******************************************************************************
2017 * CPDuplicateHash (RSAENH.@)
2019 * Clones a hash object including its current state.
2022 * hUID [I] Handle to the key container the hash belongs to.
2023 * hHash [I] Handle to the hash object to be cloned.
2024 * pdwReserved [I] Reserved. Must be NULL.
2025 * dwFlags [I] No flags are currently defined. Must be 0.
2026 * phHash [O] Handle to the cloned hash object.
2032 BOOL WINAPI
RSAENH_CPDuplicateHash(HCRYPTPROV hUID
, HCRYPTHASH hHash
, DWORD
*pdwReserved
,
2033 DWORD dwFlags
, HCRYPTHASH
*phHash
)
2035 CRYPTHASH
*pSrcHash
, *pDestHash
;
2037 TRACE("(hUID=%08lx, hHash=%08lx, pdwReserved=%p, dwFlags=%08x, phHash=%p)\n", hUID
, hHash
,
2038 pdwReserved
, dwFlags
, phHash
);
2040 if (!is_valid_handle(&handle_table
, hUID
, RSAENH_MAGIC_CONTAINER
))
2042 SetLastError(NTE_BAD_UID
);
2046 if (!lookup_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
, (OBJECTHDR
**)&pSrcHash
))
2048 SetLastError(NTE_BAD_HASH
);
2052 if (!phHash
|| pdwReserved
|| dwFlags
)
2054 SetLastError(ERROR_INVALID_PARAMETER
);
2058 *phHash
= new_object(&handle_table
, sizeof(CRYPTHASH
), RSAENH_MAGIC_HASH
,
2059 destroy_hash
, (OBJECTHDR
**)&pDestHash
);
2060 if (*phHash
!= (HCRYPTHASH
)INVALID_HANDLE_VALUE
)
2062 *pDestHash
= *pSrcHash
;
2063 duplicate_hash_impl(pSrcHash
->aiAlgid
, &pSrcHash
->context
, &pDestHash
->context
);
2064 copy_hmac_info(&pDestHash
->pHMACInfo
, pSrcHash
->pHMACInfo
);
2065 copy_data_blob(&pDestHash
->tpPRFParams
.blobLabel
, &pSrcHash
->tpPRFParams
.blobLabel
);
2066 copy_data_blob(&pDestHash
->tpPRFParams
.blobSeed
, &pSrcHash
->tpPRFParams
.blobSeed
);
2069 return *phHash
!= (HCRYPTHASH
)INVALID_HANDLE_VALUE
;
2072 /******************************************************************************
2073 * CPDuplicateKey (RSAENH.@)
2075 * Clones a key object including its current state.
2078 * hUID [I] Handle to the key container the hash belongs to.
2079 * hKey [I] Handle to the key object to be cloned.
2080 * pdwReserved [I] Reserved. Must be NULL.
2081 * dwFlags [I] No flags are currently defined. Must be 0.
2082 * phHash [O] Handle to the cloned key object.
2088 BOOL WINAPI
RSAENH_CPDuplicateKey(HCRYPTPROV hUID
, HCRYPTKEY hKey
, DWORD
*pdwReserved
,
2089 DWORD dwFlags
, HCRYPTKEY
*phKey
)
2091 CRYPTKEY
*pSrcKey
, *pDestKey
;
2093 TRACE("(hUID=%08lx, hKey=%08lx, pdwReserved=%p, dwFlags=%08x, phKey=%p)\n", hUID
, hKey
,
2094 pdwReserved
, dwFlags
, phKey
);
2096 if (!is_valid_handle(&handle_table
, hUID
, RSAENH_MAGIC_CONTAINER
))
2098 SetLastError(NTE_BAD_UID
);
2102 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pSrcKey
))
2104 SetLastError(NTE_BAD_KEY
);
2108 if (!phKey
|| pdwReserved
|| dwFlags
)
2110 SetLastError(ERROR_INVALID_PARAMETER
);
2114 *phKey
= new_object(&handle_table
, sizeof(CRYPTKEY
), RSAENH_MAGIC_KEY
, destroy_key
,
2115 (OBJECTHDR
**)&pDestKey
);
2116 if (*phKey
!= (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
2118 *pDestKey
= *pSrcKey
;
2119 copy_data_blob(&pDestKey
->siSChannelInfo
.blobServerRandom
,
2120 &pSrcKey
->siSChannelInfo
.blobServerRandom
);
2121 copy_data_blob(&pDestKey
->siSChannelInfo
.blobClientRandom
,
2122 &pSrcKey
->siSChannelInfo
.blobClientRandom
);
2123 duplicate_key_impl(pSrcKey
->aiAlgid
, &pSrcKey
->context
, &pDestKey
->context
);
2132 /******************************************************************************
2133 * CPEncrypt (RSAENH.@)
2138 * hProv [I] The key container hKey and hHash belong to.
2139 * hKey [I] The key used to encrypt the data.
2140 * hHash [I] An optional hash object for parallel hashing. See notes.
2141 * Final [I] Indicates if this is the last block of data to encrypt.
2142 * dwFlags [I] Currently no flags defined. Must be zero.
2143 * pbData [I/O] Pointer to the data to encrypt. Encrypted data will also be stored there.
2144 * pdwDataLen [I/O] I: Length of data to encrypt, O: Length of encrypted data.
2145 * dwBufLen [I] Size of the buffer at pbData.
2152 * If a hash object handle is provided in hHash, it will be updated with the plaintext.
2153 * This is useful for message signatures.
2155 * This function uses the standard WINAPI protocol for querying data of dynamic length.
2157 BOOL WINAPI
RSAENH_CPEncrypt(HCRYPTPROV hProv
, HCRYPTKEY hKey
, HCRYPTHASH hHash
, BOOL Final
,
2158 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
, DWORD dwBufLen
)
2160 CRYPTKEY
*pCryptKey
;
2161 BYTE
*in
, out
[RSAENH_MAX_BLOCK_SIZE
], o
[RSAENH_MAX_BLOCK_SIZE
];
2162 DWORD dwEncryptedLen
, i
, j
, k
;
2164 TRACE("(hProv=%08lx, hKey=%08lx, hHash=%08lx, Final=%d, dwFlags=%08x, pbData=%p, "
2165 "pdwDataLen=%p, dwBufLen=%d)\n", hProv
, hKey
, hHash
, Final
, dwFlags
, pbData
, pdwDataLen
,
2168 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
2170 SetLastError(NTE_BAD_UID
);
2176 SetLastError(NTE_BAD_FLAGS
);
2180 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
2182 SetLastError(NTE_BAD_KEY
);
2186 if (pCryptKey
->dwState
== RSAENH_KEYSTATE_IDLE
)
2187 pCryptKey
->dwState
= RSAENH_KEYSTATE_ENCRYPTING
;
2189 if (pCryptKey
->dwState
!= RSAENH_KEYSTATE_ENCRYPTING
)
2191 SetLastError(NTE_BAD_DATA
);
2195 if (is_valid_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
)) {
2196 if (!RSAENH_CPHashData(hProv
, hHash
, pbData
, *pdwDataLen
, 0)) return FALSE
;
2199 if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_BLOCK
) {
2200 if (!Final
&& (*pdwDataLen
% pCryptKey
->dwBlockLen
)) {
2201 SetLastError(NTE_BAD_DATA
);
2205 dwEncryptedLen
= (*pdwDataLen
/pCryptKey
->dwBlockLen
+(Final
?1:0))*pCryptKey
->dwBlockLen
;
2207 if (pbData
== NULL
) {
2208 *pdwDataLen
= dwEncryptedLen
;
2211 else if (dwEncryptedLen
> dwBufLen
) {
2212 *pdwDataLen
= dwEncryptedLen
;
2213 SetLastError(ERROR_MORE_DATA
);
2217 /* Pad final block with length bytes */
2218 for (i
=*pdwDataLen
; i
<dwEncryptedLen
; i
++) pbData
[i
] = dwEncryptedLen
- *pdwDataLen
;
2219 *pdwDataLen
= dwEncryptedLen
;
2221 for (i
=0, in
=pbData
; i
<*pdwDataLen
; i
+=pCryptKey
->dwBlockLen
, in
+=pCryptKey
->dwBlockLen
) {
2222 switch (pCryptKey
->dwMode
) {
2223 case CRYPT_MODE_ECB
:
2224 encrypt_block_impl(pCryptKey
->aiAlgid
, 0, &pCryptKey
->context
, in
, out
,
2228 case CRYPT_MODE_CBC
:
2229 for (j
=0; j
<pCryptKey
->dwBlockLen
; j
++) in
[j
] ^= pCryptKey
->abChainVector
[j
];
2230 encrypt_block_impl(pCryptKey
->aiAlgid
, 0, &pCryptKey
->context
, in
, out
,
2232 memcpy(pCryptKey
->abChainVector
, out
, pCryptKey
->dwBlockLen
);
2235 case CRYPT_MODE_CFB
:
2236 for (j
=0; j
<pCryptKey
->dwBlockLen
; j
++) {
2237 encrypt_block_impl(pCryptKey
->aiAlgid
, 0, &pCryptKey
->context
,
2238 pCryptKey
->abChainVector
, o
, RSAENH_ENCRYPT
);
2239 out
[j
] = in
[j
] ^ o
[0];
2240 for (k
=0; k
<pCryptKey
->dwBlockLen
-1; k
++)
2241 pCryptKey
->abChainVector
[k
] = pCryptKey
->abChainVector
[k
+1];
2242 pCryptKey
->abChainVector
[k
] = out
[j
];
2247 SetLastError(NTE_BAD_ALGID
);
2250 memcpy(in
, out
, pCryptKey
->dwBlockLen
);
2252 } else if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_STREAM
) {
2253 if (pbData
== NULL
) {
2254 *pdwDataLen
= dwBufLen
;
2257 encrypt_stream_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pbData
, *pdwDataLen
);
2258 } else if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_RSA
) {
2259 if (pCryptKey
->aiAlgid
== CALG_RSA_SIGN
) {
2260 SetLastError(NTE_BAD_KEY
);
2264 *pdwDataLen
= pCryptKey
->dwBlockLen
;
2267 if (dwBufLen
< pCryptKey
->dwBlockLen
) {
2268 SetLastError(ERROR_MORE_DATA
);
2271 if (!pad_data(pbData
, *pdwDataLen
, pbData
, pCryptKey
->dwBlockLen
, dwFlags
)) return FALSE
;
2272 encrypt_block_impl(pCryptKey
->aiAlgid
, PK_PUBLIC
, &pCryptKey
->context
, pbData
, pbData
, RSAENH_ENCRYPT
);
2273 *pdwDataLen
= pCryptKey
->dwBlockLen
;
2276 SetLastError(NTE_BAD_TYPE
);
2280 if (Final
) setup_key(pCryptKey
);
2285 /******************************************************************************
2286 * CPDecrypt (RSAENH.@)
2291 * hProv [I] The key container hKey and hHash belong to.
2292 * hKey [I] The key used to decrypt the data.
2293 * hHash [I] An optional hash object for parallel hashing. See notes.
2294 * Final [I] Indicates if this is the last block of data to decrypt.
2295 * dwFlags [I] Currently no flags defined. Must be zero.
2296 * pbData [I/O] Pointer to the data to decrypt. Plaintext will also be stored there.
2297 * pdwDataLen [I/O] I: Length of ciphertext, O: Length of plaintext.
2304 * If a hash object handle is provided in hHash, it will be updated with the plaintext.
2305 * This is useful for message signatures.
2307 * This function uses the standard WINAPI protocol for querying data of dynamic length.
2309 BOOL WINAPI
RSAENH_CPDecrypt(HCRYPTPROV hProv
, HCRYPTKEY hKey
, HCRYPTHASH hHash
, BOOL Final
,
2310 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
)
2312 CRYPTKEY
*pCryptKey
;
2313 BYTE
*in
, out
[RSAENH_MAX_BLOCK_SIZE
], o
[RSAENH_MAX_BLOCK_SIZE
];
2317 TRACE("(hProv=%08lx, hKey=%08lx, hHash=%08lx, Final=%d, dwFlags=%08x, pbData=%p, "
2318 "pdwDataLen=%p)\n", hProv
, hKey
, hHash
, Final
, dwFlags
, pbData
, pdwDataLen
);
2320 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
2322 SetLastError(NTE_BAD_UID
);
2328 SetLastError(NTE_BAD_FLAGS
);
2332 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
2334 SetLastError(NTE_BAD_KEY
);
2338 if (pCryptKey
->dwState
== RSAENH_KEYSTATE_IDLE
)
2339 pCryptKey
->dwState
= RSAENH_KEYSTATE_ENCRYPTING
;
2341 if (pCryptKey
->dwState
!= RSAENH_KEYSTATE_ENCRYPTING
)
2343 SetLastError(NTE_BAD_DATA
);
2349 if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_BLOCK
) {
2350 for (i
=0, in
=pbData
; i
<*pdwDataLen
; i
+=pCryptKey
->dwBlockLen
, in
+=pCryptKey
->dwBlockLen
) {
2351 switch (pCryptKey
->dwMode
) {
2352 case CRYPT_MODE_ECB
:
2353 encrypt_block_impl(pCryptKey
->aiAlgid
, 0, &pCryptKey
->context
, in
, out
,
2357 case CRYPT_MODE_CBC
:
2358 encrypt_block_impl(pCryptKey
->aiAlgid
, 0, &pCryptKey
->context
, in
, out
,
2360 for (j
=0; j
<pCryptKey
->dwBlockLen
; j
++) out
[j
] ^= pCryptKey
->abChainVector
[j
];
2361 memcpy(pCryptKey
->abChainVector
, in
, pCryptKey
->dwBlockLen
);
2364 case CRYPT_MODE_CFB
:
2365 for (j
=0; j
<pCryptKey
->dwBlockLen
; j
++) {
2366 encrypt_block_impl(pCryptKey
->aiAlgid
, 0, &pCryptKey
->context
,
2367 pCryptKey
->abChainVector
, o
, RSAENH_ENCRYPT
);
2368 out
[j
] = in
[j
] ^ o
[0];
2369 for (k
=0; k
<pCryptKey
->dwBlockLen
-1; k
++)
2370 pCryptKey
->abChainVector
[k
] = pCryptKey
->abChainVector
[k
+1];
2371 pCryptKey
->abChainVector
[k
] = in
[j
];
2376 SetLastError(NTE_BAD_ALGID
);
2379 memcpy(in
, out
, pCryptKey
->dwBlockLen
);
2382 if (pbData
[*pdwDataLen
-1] &&
2383 pbData
[*pdwDataLen
-1] <= pCryptKey
->dwBlockLen
&&
2384 pbData
[*pdwDataLen
-1] <= *pdwDataLen
) {
2385 BOOL padOkay
= TRUE
;
2387 /* check that every bad byte has the same value */
2388 for (i
= 1; padOkay
&& i
< pbData
[*pdwDataLen
-1]; i
++)
2389 if (pbData
[*pdwDataLen
- i
- 1] != pbData
[*pdwDataLen
- 1])
2392 *pdwDataLen
-= pbData
[*pdwDataLen
-1];
2394 SetLastError(NTE_BAD_DATA
);
2395 setup_key(pCryptKey
);
2400 SetLastError(NTE_BAD_DATA
);
2401 setup_key(pCryptKey
);
2406 } else if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_STREAM
) {
2407 encrypt_stream_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pbData
, *pdwDataLen
);
2408 } else if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_RSA
) {
2409 if (pCryptKey
->aiAlgid
== CALG_RSA_SIGN
) {
2410 SetLastError(NTE_BAD_KEY
);
2413 encrypt_block_impl(pCryptKey
->aiAlgid
, PK_PRIVATE
, &pCryptKey
->context
, pbData
, pbData
, RSAENH_DECRYPT
);
2414 if (!unpad_data(pbData
, pCryptKey
->dwBlockLen
, pbData
, pdwDataLen
, dwFlags
)) return FALSE
;
2417 SetLastError(NTE_BAD_TYPE
);
2421 if (Final
) setup_key(pCryptKey
);
2423 if (is_valid_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
)) {
2424 if (*pdwDataLen
>dwMax
||
2425 !RSAENH_CPHashData(hProv
, hHash
, pbData
, *pdwDataLen
, 0)) return FALSE
;
2431 static BOOL
crypt_export_simple(CRYPTKEY
*pCryptKey
, CRYPTKEY
*pPubKey
,
2432 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
)
2434 BLOBHEADER
*pBlobHeader
= (BLOBHEADER
*)pbData
;
2435 ALG_ID
*pAlgid
= (ALG_ID
*)(pBlobHeader
+1);
2438 if (!(GET_ALG_CLASS(pCryptKey
->aiAlgid
)&(ALG_CLASS_DATA_ENCRYPT
|ALG_CLASS_MSG_ENCRYPT
))) {
2439 SetLastError(NTE_BAD_KEY
); /* FIXME: error code? */
2443 dwDataLen
= sizeof(BLOBHEADER
) + sizeof(ALG_ID
) + pPubKey
->dwBlockLen
;
2445 if (*pdwDataLen
< dwDataLen
) {
2446 SetLastError(ERROR_MORE_DATA
);
2447 *pdwDataLen
= dwDataLen
;
2451 pBlobHeader
->bType
= SIMPLEBLOB
;
2452 pBlobHeader
->bVersion
= CUR_BLOB_VERSION
;
2453 pBlobHeader
->reserved
= 0;
2454 pBlobHeader
->aiKeyAlg
= pCryptKey
->aiAlgid
;
2456 *pAlgid
= pPubKey
->aiAlgid
;
2458 if (!pad_data(pCryptKey
->abKeyValue
, pCryptKey
->dwKeyLen
, (BYTE
*)(pAlgid
+1),
2459 pPubKey
->dwBlockLen
, dwFlags
))
2464 encrypt_block_impl(pPubKey
->aiAlgid
, PK_PUBLIC
, &pPubKey
->context
, (BYTE
*)(pAlgid
+1),
2465 (BYTE
*)(pAlgid
+1), RSAENH_ENCRYPT
);
2467 *pdwDataLen
= dwDataLen
;
2471 static BOOL
crypt_export_public_key(CRYPTKEY
*pCryptKey
, BYTE
*pbData
,
2474 BLOBHEADER
*pBlobHeader
= (BLOBHEADER
*)pbData
;
2475 RSAPUBKEY
*pRSAPubKey
= (RSAPUBKEY
*)(pBlobHeader
+1);
2478 if ((pCryptKey
->aiAlgid
!= CALG_RSA_KEYX
) && (pCryptKey
->aiAlgid
!= CALG_RSA_SIGN
)) {
2479 SetLastError(NTE_BAD_KEY
);
2483 dwDataLen
= sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) + pCryptKey
->dwKeyLen
;
2485 if (*pdwDataLen
< dwDataLen
) {
2486 SetLastError(ERROR_MORE_DATA
);
2487 *pdwDataLen
= dwDataLen
;
2491 pBlobHeader
->bType
= PUBLICKEYBLOB
;
2492 pBlobHeader
->bVersion
= CUR_BLOB_VERSION
;
2493 pBlobHeader
->reserved
= 0;
2494 pBlobHeader
->aiKeyAlg
= pCryptKey
->aiAlgid
;
2496 pRSAPubKey
->magic
= RSAENH_MAGIC_RSA1
;
2497 pRSAPubKey
->bitlen
= pCryptKey
->dwKeyLen
<< 3;
2499 export_public_key_impl((BYTE
*)(pRSAPubKey
+1), &pCryptKey
->context
,
2500 pCryptKey
->dwKeyLen
, &pRSAPubKey
->pubexp
);
2502 *pdwDataLen
= dwDataLen
;
2506 static BOOL
crypt_export_private_key(CRYPTKEY
*pCryptKey
, BOOL force
,
2507 BYTE
*pbData
, DWORD
*pdwDataLen
)
2509 BLOBHEADER
*pBlobHeader
= (BLOBHEADER
*)pbData
;
2510 RSAPUBKEY
*pRSAPubKey
= (RSAPUBKEY
*)(pBlobHeader
+1);
2513 if ((pCryptKey
->aiAlgid
!= CALG_RSA_KEYX
) && (pCryptKey
->aiAlgid
!= CALG_RSA_SIGN
)) {
2514 SetLastError(NTE_BAD_KEY
);
2517 if (!force
&& !(pCryptKey
->dwPermissions
& CRYPT_EXPORT
))
2519 SetLastError(NTE_BAD_KEY_STATE
);
2523 dwDataLen
= sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) +
2524 2 * pCryptKey
->dwKeyLen
+ 5 * ((pCryptKey
->dwKeyLen
+ 1) >> 1);
2526 if (*pdwDataLen
< dwDataLen
) {
2527 SetLastError(ERROR_MORE_DATA
);
2528 *pdwDataLen
= dwDataLen
;
2532 pBlobHeader
->bType
= PRIVATEKEYBLOB
;
2533 pBlobHeader
->bVersion
= CUR_BLOB_VERSION
;
2534 pBlobHeader
->reserved
= 0;
2535 pBlobHeader
->aiKeyAlg
= pCryptKey
->aiAlgid
;
2537 pRSAPubKey
->magic
= RSAENH_MAGIC_RSA2
;
2538 pRSAPubKey
->bitlen
= pCryptKey
->dwKeyLen
<< 3;
2540 export_private_key_impl((BYTE
*)(pRSAPubKey
+1), &pCryptKey
->context
,
2541 pCryptKey
->dwKeyLen
, &pRSAPubKey
->pubexp
);
2543 *pdwDataLen
= dwDataLen
;
2547 static BOOL
crypt_export_plaintext_key(CRYPTKEY
*pCryptKey
, BYTE
*pbData
,
2550 BLOBHEADER
*pBlobHeader
= (BLOBHEADER
*)pbData
;
2551 DWORD
*pKeyLen
= (DWORD
*)(pBlobHeader
+1);
2552 BYTE
*pbKey
= (BYTE
*)(pKeyLen
+1);
2555 dwDataLen
= sizeof(BLOBHEADER
) + sizeof(DWORD
) + pCryptKey
->dwKeyLen
;
2557 if (*pdwDataLen
< dwDataLen
) {
2558 SetLastError(ERROR_MORE_DATA
);
2559 *pdwDataLen
= dwDataLen
;
2563 pBlobHeader
->bType
= PLAINTEXTKEYBLOB
;
2564 pBlobHeader
->bVersion
= CUR_BLOB_VERSION
;
2565 pBlobHeader
->reserved
= 0;
2566 pBlobHeader
->aiKeyAlg
= pCryptKey
->aiAlgid
;
2568 *pKeyLen
= pCryptKey
->dwKeyLen
;
2569 memcpy(pbKey
, pCryptKey
->abKeyValue
, pCryptKey
->dwKeyLen
);
2571 *pdwDataLen
= dwDataLen
;
2574 /******************************************************************************
2575 * crypt_export_key [Internal]
2577 * Export a key into a binary large object (BLOB). Called by CPExportKey and
2578 * by store_key_pair.
2581 * pCryptKey [I] Key to be exported.
2582 * hPubKey [I] Key used to encrypt sensitive BLOB data.
2583 * dwBlobType [I] SIMPLEBLOB, PUBLICKEYBLOB or PRIVATEKEYBLOB.
2584 * dwFlags [I] Currently none defined.
2585 * force [I] If TRUE, the key is written no matter what the key's
2586 * permissions are. Otherwise the key's permissions are
2587 * checked before exporting.
2588 * pbData [O] Pointer to a buffer where the BLOB will be written to.
2589 * pdwDataLen [I/O] I: Size of buffer at pbData, O: Size of BLOB
2595 static BOOL
crypt_export_key(CRYPTKEY
*pCryptKey
, HCRYPTKEY hPubKey
,
2596 DWORD dwBlobType
, DWORD dwFlags
, BOOL force
,
2597 BYTE
*pbData
, DWORD
*pdwDataLen
)
2601 if (dwFlags
& CRYPT_SSL2_FALLBACK
) {
2602 if (pCryptKey
->aiAlgid
!= CALG_SSL2_MASTER
) {
2603 SetLastError(NTE_BAD_KEY
);
2608 switch ((BYTE
)dwBlobType
)
2611 if (!lookup_handle(&handle_table
, hPubKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pPubKey
)){
2612 SetLastError(NTE_BAD_PUBLIC_KEY
); /* FIXME: error_code? */
2615 return crypt_export_simple(pCryptKey
, pPubKey
, dwFlags
, pbData
,
2619 if (is_valid_handle(&handle_table
, hPubKey
, RSAENH_MAGIC_KEY
)) {
2620 SetLastError(NTE_BAD_KEY
); /* FIXME: error code? */
2624 return crypt_export_public_key(pCryptKey
, pbData
, pdwDataLen
);
2626 case PRIVATEKEYBLOB
:
2627 return crypt_export_private_key(pCryptKey
, force
, pbData
, pdwDataLen
);
2629 case PLAINTEXTKEYBLOB
:
2630 return crypt_export_plaintext_key(pCryptKey
, pbData
, pdwDataLen
);
2633 SetLastError(NTE_BAD_TYPE
); /* FIXME: error code? */
2638 /******************************************************************************
2639 * CPExportKey (RSAENH.@)
2641 * Export a key into a binary large object (BLOB).
2644 * hProv [I] Key container from which a key is to be exported.
2645 * hKey [I] Key to be exported.
2646 * hPubKey [I] Key used to encrypt sensitive BLOB data.
2647 * dwBlobType [I] SIMPLEBLOB, PUBLICKEYBLOB or PRIVATEKEYBLOB.
2648 * dwFlags [I] Currently none defined.
2649 * pbData [O] Pointer to a buffer where the BLOB will be written to.
2650 * pdwDataLen [I/O] I: Size of buffer at pbData, O: Size of BLOB
2656 BOOL WINAPI
RSAENH_CPExportKey(HCRYPTPROV hProv
, HCRYPTKEY hKey
, HCRYPTKEY hPubKey
,
2657 DWORD dwBlobType
, DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
)
2659 CRYPTKEY
*pCryptKey
;
2661 TRACE("(hProv=%08lx, hKey=%08lx, hPubKey=%08lx, dwBlobType=%08x, dwFlags=%08x, pbData=%p,"
2662 "pdwDataLen=%p)\n", hProv
, hKey
, hPubKey
, dwBlobType
, dwFlags
, pbData
, pdwDataLen
);
2664 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
2666 SetLastError(NTE_BAD_UID
);
2670 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
2672 SetLastError(NTE_BAD_KEY
);
2676 return crypt_export_key(pCryptKey
, hPubKey
, dwBlobType
, dwFlags
, FALSE
,
2677 pbData
, pdwDataLen
);
2680 /******************************************************************************
2681 * release_and_install_key [Internal]
2683 * Release an existing key, if present, and replaces it with a new one.
2686 * hProv [I] Key container into which the key is to be imported.
2687 * src [I] Key which will replace *dest
2688 * dest [I] Points to key to be released and replaced with src
2689 * fStoreKey [I] If TRUE, the newly installed key is stored to the registry.
2691 static void release_and_install_key(HCRYPTPROV hProv
, HCRYPTKEY src
,
2692 HCRYPTKEY
*dest
, DWORD fStoreKey
)
2694 RSAENH_CPDestroyKey(hProv
, *dest
);
2695 copy_handle(&handle_table
, src
, RSAENH_MAGIC_KEY
, dest
);
2698 KEYCONTAINER
*pKeyContainer
;
2700 if (lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
,
2701 (OBJECTHDR
**)&pKeyContainer
))
2703 store_key_container_keys(pKeyContainer
);
2704 store_key_container_permissions(pKeyContainer
);
2709 /******************************************************************************
2710 * import_private_key [Internal]
2712 * Import a BLOB'ed private key into a key container.
2715 * hProv [I] Key container into which the private key is to be imported.
2716 * pbData [I] Pointer to a buffer which holds the private key BLOB.
2717 * dwDataLen [I] Length of data in buffer at pbData.
2718 * dwFlags [I] One of:
2719 * CRYPT_EXPORTABLE: the imported key is marked exportable
2720 * fStoreKey [I] If TRUE, the imported key is stored to the registry.
2721 * phKey [O] Handle to the imported key.
2725 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2726 * it's a PRIVATEKEYBLOB.
2732 static BOOL
import_private_key(HCRYPTPROV hProv
, const BYTE
*pbData
, DWORD dwDataLen
,
2733 DWORD dwFlags
, BOOL fStoreKey
, HCRYPTKEY
*phKey
)
2735 KEYCONTAINER
*pKeyContainer
;
2736 CRYPTKEY
*pCryptKey
;
2737 const BLOBHEADER
*pBlobHeader
= (const BLOBHEADER
*)pbData
;
2738 const RSAPUBKEY
*pRSAPubKey
= (const RSAPUBKEY
*)(pBlobHeader
+1);
2741 if (dwFlags
& CRYPT_IPSEC_HMAC_KEY
)
2743 FIXME("unimplemented for CRYPT_IPSEC_HMAC_KEY\n");
2744 SetLastError(NTE_BAD_FLAGS
);
2747 if (!lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
,
2748 (OBJECTHDR
**)&pKeyContainer
))
2750 SetLastError(NTE_BAD_UID
);
2754 if ((dwDataLen
< sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
)))
2756 ERR("datalen %d not long enough for a BLOBHEADER + RSAPUBKEY\n",
2758 SetLastError(NTE_BAD_DATA
);
2761 if (pRSAPubKey
->magic
!= RSAENH_MAGIC_RSA2
)
2763 ERR("unexpected magic %08x\n", pRSAPubKey
->magic
);
2764 SetLastError(NTE_BAD_DATA
);
2767 if ((dwDataLen
< sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) +
2768 (pRSAPubKey
->bitlen
>> 3) + (5 * ((pRSAPubKey
->bitlen
+8)>>4))))
2770 DWORD expectedLen
= sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) +
2771 (pRSAPubKey
->bitlen
>> 3) + (5 * ((pRSAPubKey
->bitlen
+8)>>4));
2773 ERR("blob too short for pub key: expect %d, got %d\n",
2774 expectedLen
, dwDataLen
);
2775 SetLastError(NTE_BAD_DATA
);
2779 *phKey
= new_key(hProv
, pBlobHeader
->aiKeyAlg
, MAKELONG(0,pRSAPubKey
->bitlen
), &pCryptKey
);
2780 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
2781 setup_key(pCryptKey
);
2782 ret
= import_private_key_impl((const BYTE
*)(pRSAPubKey
+1), &pCryptKey
->context
,
2783 pRSAPubKey
->bitlen
/8, dwDataLen
, pRSAPubKey
->pubexp
);
2785 if (dwFlags
& CRYPT_EXPORTABLE
)
2786 pCryptKey
->dwPermissions
|= CRYPT_EXPORT
;
2787 switch (pBlobHeader
->aiKeyAlg
)
2791 TRACE("installing signing key\n");
2792 release_and_install_key(hProv
, *phKey
, &pKeyContainer
->hSignatureKeyPair
,
2795 case AT_KEYEXCHANGE
:
2797 TRACE("installing key exchange key\n");
2798 release_and_install_key(hProv
, *phKey
, &pKeyContainer
->hKeyExchangeKeyPair
,
2806 /******************************************************************************
2807 * import_public_key [Internal]
2809 * Import a BLOB'ed public key into a key container.
2812 * hProv [I] Key container into which the public key is to be imported.
2813 * pbData [I] Pointer to a buffer which holds the public key BLOB.
2814 * dwDataLen [I] Length of data in buffer at pbData.
2815 * dwFlags [I] One of:
2816 * CRYPT_EXPORTABLE: the imported key is marked exportable
2817 * fStoreKey [I] If TRUE, the imported key is stored to the registry.
2818 * phKey [O] Handle to the imported key.
2822 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2823 * it's a PUBLICKEYBLOB.
2829 static BOOL
import_public_key(HCRYPTPROV hProv
, const BYTE
*pbData
, DWORD dwDataLen
,
2830 DWORD dwFlags
, BOOL fStoreKey
, HCRYPTKEY
*phKey
)
2832 KEYCONTAINER
*pKeyContainer
;
2833 CRYPTKEY
*pCryptKey
;
2834 const BLOBHEADER
*pBlobHeader
= (const BLOBHEADER
*)pbData
;
2835 const RSAPUBKEY
*pRSAPubKey
= (const RSAPUBKEY
*)(pBlobHeader
+1);
2839 if (dwFlags
& CRYPT_IPSEC_HMAC_KEY
)
2841 FIXME("unimplemented for CRYPT_IPSEC_HMAC_KEY\n");
2842 SetLastError(NTE_BAD_FLAGS
);
2845 if (!lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
,
2846 (OBJECTHDR
**)&pKeyContainer
))
2848 SetLastError(NTE_BAD_UID
);
2852 if ((dwDataLen
< sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
)) ||
2853 (pRSAPubKey
->magic
!= RSAENH_MAGIC_RSA1
) ||
2854 (dwDataLen
< sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) + (pRSAPubKey
->bitlen
>> 3)))
2856 SetLastError(NTE_BAD_DATA
);
2860 /* Since this is a public key blob, only the public key is
2861 * available, so only signature verification is possible.
2863 algID
= pBlobHeader
->aiKeyAlg
;
2864 *phKey
= new_key(hProv
, algID
, MAKELONG(0,pRSAPubKey
->bitlen
), &pCryptKey
);
2865 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
2866 setup_key(pCryptKey
);
2867 ret
= import_public_key_impl((const BYTE
*)(pRSAPubKey
+1), &pCryptKey
->context
,
2868 pRSAPubKey
->bitlen
>> 3, pRSAPubKey
->pubexp
);
2870 if (dwFlags
& CRYPT_EXPORTABLE
)
2871 pCryptKey
->dwPermissions
|= CRYPT_EXPORT
;
2872 switch (pBlobHeader
->aiKeyAlg
)
2874 case AT_KEYEXCHANGE
:
2876 TRACE("installing public key\n");
2877 release_and_install_key(hProv
, *phKey
, &pKeyContainer
->hKeyExchangeKeyPair
,
2885 /******************************************************************************
2886 * import_symmetric_key [Internal]
2888 * Import a BLOB'ed symmetric key into a key container.
2891 * hProv [I] Key container into which the symmetric key is to be imported.
2892 * pbData [I] Pointer to a buffer which holds the symmetric key BLOB.
2893 * dwDataLen [I] Length of data in buffer at pbData.
2894 * hPubKey [I] Key used to decrypt sensitive BLOB data.
2895 * dwFlags [I] One of:
2896 * CRYPT_EXPORTABLE: the imported key is marked exportable
2897 * phKey [O] Handle to the imported key.
2901 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2902 * it's a SIMPLEBLOB.
2908 static BOOL
import_symmetric_key(HCRYPTPROV hProv
, const BYTE
*pbData
, DWORD dwDataLen
,
2909 HCRYPTKEY hPubKey
, DWORD dwFlags
, HCRYPTKEY
*phKey
)
2911 CRYPTKEY
*pCryptKey
, *pPubKey
;
2912 const BLOBHEADER
*pBlobHeader
= (const BLOBHEADER
*)pbData
;
2913 const ALG_ID
*pAlgid
= (const ALG_ID
*)(pBlobHeader
+1);
2914 const BYTE
*pbKeyStream
= (const BYTE
*)(pAlgid
+ 1);
2918 if (dwFlags
& CRYPT_IPSEC_HMAC_KEY
)
2920 FIXME("unimplemented for CRYPT_IPSEC_HMAC_KEY\n");
2921 SetLastError(NTE_BAD_FLAGS
);
2924 if (!lookup_handle(&handle_table
, hPubKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pPubKey
) ||
2925 pPubKey
->aiAlgid
!= CALG_RSA_KEYX
)
2927 SetLastError(NTE_BAD_PUBLIC_KEY
); /* FIXME: error code? */
2931 if (dwDataLen
< sizeof(BLOBHEADER
)+sizeof(ALG_ID
)+pPubKey
->dwBlockLen
)
2933 SetLastError(NTE_BAD_DATA
); /* FIXME: error code */
2937 pbDecrypted
= HeapAlloc(GetProcessHeap(), 0, pPubKey
->dwBlockLen
);
2938 if (!pbDecrypted
) return FALSE
;
2939 encrypt_block_impl(pPubKey
->aiAlgid
, PK_PRIVATE
, &pPubKey
->context
, pbKeyStream
, pbDecrypted
,
2942 dwKeyLen
= RSAENH_MAX_KEY_SIZE
;
2943 if (!unpad_data(pbDecrypted
, pPubKey
->dwBlockLen
, pbDecrypted
, &dwKeyLen
, dwFlags
)) {
2944 HeapFree(GetProcessHeap(), 0, pbDecrypted
);
2948 *phKey
= new_key(hProv
, pBlobHeader
->aiKeyAlg
, dwKeyLen
<<19, &pCryptKey
);
2949 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
2951 HeapFree(GetProcessHeap(), 0, pbDecrypted
);
2954 memcpy(pCryptKey
->abKeyValue
, pbDecrypted
, dwKeyLen
);
2955 HeapFree(GetProcessHeap(), 0, pbDecrypted
);
2956 setup_key(pCryptKey
);
2957 if (dwFlags
& CRYPT_EXPORTABLE
)
2958 pCryptKey
->dwPermissions
|= CRYPT_EXPORT
;
2962 /******************************************************************************
2963 * import_plaintext_key [Internal]
2965 * Import a plaintext key into a key container.
2968 * hProv [I] Key container into which the symmetric key is to be imported.
2969 * pbData [I] Pointer to a buffer which holds the plaintext key BLOB.
2970 * dwDataLen [I] Length of data in buffer at pbData.
2971 * dwFlags [I] One of:
2972 * CRYPT_EXPORTABLE: the imported key is marked exportable
2973 * phKey [O] Handle to the imported key.
2977 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2978 * it's a PLAINTEXTKEYBLOB.
2984 static BOOL
import_plaintext_key(HCRYPTPROV hProv
, const BYTE
*pbData
, DWORD dwDataLen
,
2985 DWORD dwFlags
, HCRYPTKEY
*phKey
)
2987 CRYPTKEY
*pCryptKey
;
2988 const BLOBHEADER
*pBlobHeader
= (const BLOBHEADER
*)pbData
;
2989 const DWORD
*pKeyLen
= (const DWORD
*)(pBlobHeader
+ 1);
2990 const BYTE
*pbKeyStream
= (const BYTE
*)(pKeyLen
+ 1);
2992 if (dwDataLen
< sizeof(BLOBHEADER
)+sizeof(DWORD
)+*pKeyLen
)
2994 SetLastError(NTE_BAD_DATA
); /* FIXME: error code */
2998 if (dwFlags
& CRYPT_IPSEC_HMAC_KEY
)
3000 *phKey
= new_key(hProv
, CALG_HMAC
, 0, &pCryptKey
);
3001 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
3003 if (*pKeyLen
<= RSAENH_MIN(sizeof(pCryptKey
->abKeyValue
), RSAENH_HMAC_BLOCK_LEN
))
3005 memcpy(pCryptKey
->abKeyValue
, pbKeyStream
, *pKeyLen
);
3006 pCryptKey
->dwKeyLen
= *pKeyLen
;
3010 CRYPT_DATA_BLOB blobHmacKey
= { *pKeyLen
, (BYTE
*)pbKeyStream
};
3012 /* In order to initialize an HMAC key, the key material is hashed,
3013 * and the output of the hash function is used as the key material.
3014 * Unfortunately, the way the Crypto API is designed, we don't know
3015 * the hash algorithm yet, so we have to copy the entire key
3018 if (!copy_data_blob(&pCryptKey
->blobHmacKey
, &blobHmacKey
))
3020 release_handle(&handle_table
, *phKey
, RSAENH_MAGIC_KEY
);
3021 *phKey
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
3025 setup_key(pCryptKey
);
3026 if (dwFlags
& CRYPT_EXPORTABLE
)
3027 pCryptKey
->dwPermissions
|= CRYPT_EXPORT
;
3031 *phKey
= new_key(hProv
, pBlobHeader
->aiKeyAlg
, *pKeyLen
<<19, &pCryptKey
);
3032 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
3034 memcpy(pCryptKey
->abKeyValue
, pbKeyStream
, *pKeyLen
);
3035 setup_key(pCryptKey
);
3036 if (dwFlags
& CRYPT_EXPORTABLE
)
3037 pCryptKey
->dwPermissions
|= CRYPT_EXPORT
;
3042 /******************************************************************************
3043 * import_key [Internal]
3045 * Import a BLOB'ed key into a key container, optionally storing the key's
3046 * value to the registry.
3049 * hProv [I] Key container into which the key is to be imported.
3050 * pbData [I] Pointer to a buffer which holds the BLOB.
3051 * dwDataLen [I] Length of data in buffer at pbData.
3052 * hPubKey [I] Key used to decrypt sensitive BLOB data.
3053 * dwFlags [I] One of:
3054 * CRYPT_EXPORTABLE: the imported key is marked exportable
3055 * fStoreKey [I] If TRUE, the imported key is stored to the registry.
3056 * phKey [O] Handle to the imported key.
3062 static BOOL
import_key(HCRYPTPROV hProv
, const BYTE
*pbData
, DWORD dwDataLen
, HCRYPTKEY hPubKey
,
3063 DWORD dwFlags
, BOOL fStoreKey
, HCRYPTKEY
*phKey
)
3065 KEYCONTAINER
*pKeyContainer
;
3066 const BLOBHEADER
*pBlobHeader
= (const BLOBHEADER
*)pbData
;
3068 if (!lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
,
3069 (OBJECTHDR
**)&pKeyContainer
))
3071 SetLastError(NTE_BAD_UID
);
3075 if (dwDataLen
< sizeof(BLOBHEADER
) ||
3076 pBlobHeader
->bVersion
!= CUR_BLOB_VERSION
||
3077 pBlobHeader
->reserved
!= 0)
3079 TRACE("bVersion = %d, reserved = %d\n", pBlobHeader
->bVersion
,
3080 pBlobHeader
->reserved
);
3081 SetLastError(NTE_BAD_DATA
);
3085 /* If this is a verify-only context, the key is not persisted regardless of
3086 * fStoreKey's original value.
3088 fStoreKey
= fStoreKey
&& !(dwFlags
& CRYPT_VERIFYCONTEXT
);
3089 TRACE("blob type: %x\n", pBlobHeader
->bType
);
3090 switch (pBlobHeader
->bType
)
3092 case PRIVATEKEYBLOB
:
3093 return import_private_key(hProv
, pbData
, dwDataLen
, dwFlags
,
3097 return import_public_key(hProv
, pbData
, dwDataLen
, dwFlags
,
3101 return import_symmetric_key(hProv
, pbData
, dwDataLen
, hPubKey
,
3104 case PLAINTEXTKEYBLOB
:
3105 return import_plaintext_key(hProv
, pbData
, dwDataLen
, dwFlags
,
3109 SetLastError(NTE_BAD_TYPE
); /* FIXME: error code? */
3114 /******************************************************************************
3115 * CPImportKey (RSAENH.@)
3117 * Import a BLOB'ed key into a key container.
3120 * hProv [I] Key container into which the key is to be imported.
3121 * pbData [I] Pointer to a buffer which holds the BLOB.
3122 * dwDataLen [I] Length of data in buffer at pbData.
3123 * hPubKey [I] Key used to decrypt sensitive BLOB data.
3124 * dwFlags [I] One of:
3125 * CRYPT_EXPORTABLE: the imported key is marked exportable
3126 * phKey [O] Handle to the imported key.
3132 BOOL WINAPI
RSAENH_CPImportKey(HCRYPTPROV hProv
, const BYTE
*pbData
, DWORD dwDataLen
,
3133 HCRYPTKEY hPubKey
, DWORD dwFlags
, HCRYPTKEY
*phKey
)
3135 TRACE("(hProv=%08lx, pbData=%p, dwDataLen=%d, hPubKey=%08lx, dwFlags=%08x, phKey=%p)\n",
3136 hProv
, pbData
, dwDataLen
, hPubKey
, dwFlags
, phKey
);
3138 return import_key(hProv
, pbData
, dwDataLen
, hPubKey
, dwFlags
, TRUE
, phKey
);
3141 /******************************************************************************
3142 * CPGenKey (RSAENH.@)
3144 * Generate a key in the key container
3147 * hProv [I] Key container for which a key is to be generated.
3148 * Algid [I] Crypto algorithm identifier for the key to be generated.
3149 * dwFlags [I] Upper 16 bits: Binary length of key. Lower 16 bits: Flags. See Notes
3150 * phKey [O] Handle to the generated key.
3157 * Flags currently not considered.
3160 * Private key-exchange- and signature-keys can be generated with Algid AT_KEYEXCHANGE
3161 * and AT_SIGNATURE values.
3163 BOOL WINAPI
RSAENH_CPGenKey(HCRYPTPROV hProv
, ALG_ID Algid
, DWORD dwFlags
, HCRYPTKEY
*phKey
)
3165 KEYCONTAINER
*pKeyContainer
;
3166 CRYPTKEY
*pCryptKey
;
3168 TRACE("(hProv=%08lx, aiAlgid=%d, dwFlags=%08x, phKey=%p)\n", hProv
, Algid
, dwFlags
, phKey
);
3170 if (!lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
,
3171 (OBJECTHDR
**)&pKeyContainer
))
3173 /* MSDN: hProv not containing valid context handle */
3174 SetLastError(NTE_BAD_UID
);
3182 *phKey
= new_key(hProv
, CALG_RSA_SIGN
, 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
->hSignatureKeyPair
,
3192 case AT_KEYEXCHANGE
:
3194 *phKey
= new_key(hProv
, CALG_RSA_KEYX
, dwFlags
, &pCryptKey
);
3196 new_key_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pCryptKey
->dwKeyLen
);
3197 setup_key(pCryptKey
);
3198 release_and_install_key(hProv
, *phKey
,
3199 &pKeyContainer
->hKeyExchangeKeyPair
,
3213 case CALG_PCT1_MASTER
:
3214 case CALG_SSL2_MASTER
:
3215 case CALG_SSL3_MASTER
:
3216 case CALG_TLS1_MASTER
:
3217 *phKey
= new_key(hProv
, Algid
, dwFlags
, &pCryptKey
);
3219 gen_rand_impl(pCryptKey
->abKeyValue
, RSAENH_MAX_KEY_SIZE
);
3221 case CALG_SSL3_MASTER
:
3222 pCryptKey
->abKeyValue
[0] = RSAENH_SSL3_VERSION_MAJOR
;
3223 pCryptKey
->abKeyValue
[1] = RSAENH_SSL3_VERSION_MINOR
;
3226 case CALG_TLS1_MASTER
:
3227 pCryptKey
->abKeyValue
[0] = RSAENH_TLS1_VERSION_MAJOR
;
3228 pCryptKey
->abKeyValue
[1] = RSAENH_TLS1_VERSION_MINOR
;
3231 setup_key(pCryptKey
);
3236 /* MSDN: Algorithm not supported specified by Algid */
3237 SetLastError(NTE_BAD_ALGID
);
3241 return *phKey
!= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
3244 /******************************************************************************
3245 * CPGenRandom (RSAENH.@)
3247 * Generate a random byte stream.
3250 * hProv [I] Key container that is used to generate random bytes.
3251 * dwLen [I] Specifies the number of requested random data bytes.
3252 * pbBuffer [O] Random bytes will be stored here.
3258 BOOL WINAPI
RSAENH_CPGenRandom(HCRYPTPROV hProv
, DWORD dwLen
, BYTE
*pbBuffer
)
3260 TRACE("(hProv=%08lx, dwLen=%d, pbBuffer=%p)\n", hProv
, dwLen
, pbBuffer
);
3262 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
3264 /* MSDN: hProv not containing valid context handle */
3265 SetLastError(NTE_BAD_UID
);
3269 return gen_rand_impl(pbBuffer
, dwLen
);
3272 /******************************************************************************
3273 * CPGetHashParam (RSAENH.@)
3275 * Query parameters of an hash object.
3278 * hProv [I] The kea container, which the hash belongs to.
3279 * hHash [I] The hash object that is to be queried.
3280 * dwParam [I] Specifies the parameter that is to be queried.
3281 * pbData [I] Pointer to the buffer where the parameter value will be stored.
3282 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
3283 * dwFlags [I] None currently defined.
3290 * Valid dwParams are: HP_ALGID, HP_HASHSIZE, HP_HASHVALUE. The hash will be
3291 * finalized if HP_HASHVALUE is queried.
3293 BOOL WINAPI
RSAENH_CPGetHashParam(HCRYPTPROV hProv
, HCRYPTHASH hHash
, DWORD dwParam
, BYTE
*pbData
,
3294 DWORD
*pdwDataLen
, DWORD dwFlags
)
3296 CRYPTHASH
*pCryptHash
;
3298 TRACE("(hProv=%08lx, hHash=%08lx, dwParam=%08x, pbData=%p, pdwDataLen=%p, dwFlags=%08x)\n",
3299 hProv
, hHash
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
3301 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
3303 SetLastError(NTE_BAD_UID
);
3309 SetLastError(NTE_BAD_FLAGS
);
3313 if (!lookup_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
,
3314 (OBJECTHDR
**)&pCryptHash
))
3316 SetLastError(NTE_BAD_HASH
);
3322 SetLastError(ERROR_INVALID_PARAMETER
);
3329 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&pCryptHash
->aiAlgid
,
3333 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&pCryptHash
->dwHashSize
,
3337 if (pCryptHash
->aiAlgid
== CALG_TLS1PRF
) {
3338 return tls1_prf(hProv
, pCryptHash
->hKey
, &pCryptHash
->tpPRFParams
.blobLabel
,
3339 &pCryptHash
->tpPRFParams
.blobSeed
, pbData
, *pdwDataLen
);
3342 if ( pbData
== NULL
) {
3343 *pdwDataLen
= pCryptHash
->dwHashSize
;
3347 if (pbData
&& (pCryptHash
->dwState
!= RSAENH_HASHSTATE_FINISHED
))
3349 finalize_hash(pCryptHash
);
3350 pCryptHash
->dwState
= RSAENH_HASHSTATE_FINISHED
;
3353 return copy_param(pbData
, pdwDataLen
, pCryptHash
->abHashValue
,
3354 pCryptHash
->dwHashSize
);
3357 SetLastError(NTE_BAD_TYPE
);
3362 /******************************************************************************
3363 * CPSetKeyParam (RSAENH.@)
3365 * Set a parameter of a key object
3368 * hProv [I] The key container to which the key belongs.
3369 * hKey [I] The key for which a parameter is to be set.
3370 * dwParam [I] Parameter type. See Notes.
3371 * pbData [I] Pointer to the parameter value.
3372 * dwFlags [I] Currently none defined.
3379 * Defined dwParam types are:
3380 * - KP_MODE: Values MODE_CBC, MODE_ECB, MODE_CFB.
3381 * - KP_MODE_BITS: Shift width for cipher feedback mode. (Currently ignored by MS CSP's)
3382 * - KP_PERMISSIONS: Or'ed combination of CRYPT_ENCRYPT, CRYPT_DECRYPT,
3383 * CRYPT_EXPORT, CRYPT_READ, CRYPT_WRITE, CRYPT_MAC
3384 * - KP_IV: Initialization vector
3386 BOOL WINAPI
RSAENH_CPSetKeyParam(HCRYPTPROV hProv
, HCRYPTKEY hKey
, DWORD dwParam
, BYTE
*pbData
,
3389 CRYPTKEY
*pCryptKey
;
3391 TRACE("(hProv=%08lx, hKey=%08lx, dwParam=%08x, pbData=%p, dwFlags=%08x)\n", hProv
, hKey
,
3392 dwParam
, pbData
, dwFlags
);
3394 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
3396 SetLastError(NTE_BAD_UID
);
3401 SetLastError(NTE_BAD_FLAGS
);
3405 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
3407 SetLastError(NTE_BAD_KEY
);
3413 /* The MS providers only support PKCS5_PADDING */
3414 if (*(DWORD
*)pbData
!= PKCS5_PADDING
) {
3415 SetLastError(NTE_BAD_DATA
);
3421 pCryptKey
->dwMode
= *(DWORD
*)pbData
;
3425 pCryptKey
->dwModeBits
= *(DWORD
*)pbData
;
3428 case KP_PERMISSIONS
:
3430 DWORD perms
= *(DWORD
*)pbData
;
3432 if ((perms
& CRYPT_EXPORT
) &&
3433 !(pCryptKey
->dwPermissions
& CRYPT_EXPORT
))
3435 SetLastError(NTE_BAD_DATA
);
3438 else if (!(perms
& CRYPT_EXPORT
) &&
3439 (pCryptKey
->dwPermissions
& CRYPT_EXPORT
))
3441 /* Clearing the export permission appears to be ignored,
3444 perms
|= CRYPT_EXPORT
;
3446 pCryptKey
->dwPermissions
= perms
;
3451 memcpy(pCryptKey
->abInitVector
, pbData
, pCryptKey
->dwBlockLen
);
3452 setup_key(pCryptKey
);
3456 switch (pCryptKey
->aiAlgid
) {
3461 SetLastError(ERROR_INVALID_PARAMETER
);
3464 /* MSDN: the base provider always sets eleven bytes of
3467 memcpy(pCryptKey
->abKeyValue
+ pCryptKey
->dwKeyLen
,
3469 pCryptKey
->dwSaltLen
= 11;
3470 setup_key(pCryptKey
);
3471 /* Strange but true: salt length reset to 0 after setting
3474 pCryptKey
->dwSaltLen
= 0;
3477 SetLastError(NTE_BAD_KEY
);
3484 CRYPT_INTEGER_BLOB
*blob
= (CRYPT_INTEGER_BLOB
*)pbData
;
3486 /* salt length can't be greater than 184 bits = 24 bytes */
3487 if (blob
->cbData
> 24)
3489 SetLastError(NTE_BAD_DATA
);
3492 memcpy(pCryptKey
->abKeyValue
+ pCryptKey
->dwKeyLen
, blob
->pbData
,
3494 pCryptKey
->dwSaltLen
= blob
->cbData
;
3495 setup_key(pCryptKey
);
3499 case KP_EFFECTIVE_KEYLEN
:
3500 switch (pCryptKey
->aiAlgid
) {
3504 SetLastError(ERROR_INVALID_PARAMETER
);
3507 else if (!*(DWORD
*)pbData
|| *(DWORD
*)pbData
> 1024)
3509 SetLastError(NTE_BAD_DATA
);
3514 pCryptKey
->dwEffectiveKeyLen
= *(DWORD
*)pbData
;
3515 setup_key(pCryptKey
);
3519 SetLastError(NTE_BAD_TYPE
);
3524 case KP_SCHANNEL_ALG
:
3525 switch (((PSCHANNEL_ALG
)pbData
)->dwUse
) {
3526 case SCHANNEL_ENC_KEY
:
3527 memcpy(&pCryptKey
->siSChannelInfo
.saEncAlg
, pbData
, sizeof(SCHANNEL_ALG
));
3530 case SCHANNEL_MAC_KEY
:
3531 memcpy(&pCryptKey
->siSChannelInfo
.saMACAlg
, pbData
, sizeof(SCHANNEL_ALG
));
3535 SetLastError(NTE_FAIL
); /* FIXME: error code */
3540 case KP_CLIENT_RANDOM
:
3541 return copy_data_blob(&pCryptKey
->siSChannelInfo
.blobClientRandom
, (PCRYPT_DATA_BLOB
)pbData
);
3543 case KP_SERVER_RANDOM
:
3544 return copy_data_blob(&pCryptKey
->siSChannelInfo
.blobServerRandom
, (PCRYPT_DATA_BLOB
)pbData
);
3547 SetLastError(NTE_BAD_TYPE
);
3552 /******************************************************************************
3553 * CPGetKeyParam (RSAENH.@)
3555 * Query a key parameter.
3558 * hProv [I] The key container, which the key belongs to.
3559 * hHash [I] The key object that is to be queried.
3560 * dwParam [I] Specifies the parameter that is to be queried.
3561 * pbData [I] Pointer to the buffer where the parameter value will be stored.
3562 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
3563 * dwFlags [I] None currently defined.
3570 * Defined dwParam types are:
3571 * - KP_MODE: Values MODE_CBC, MODE_ECB, MODE_CFB.
3572 * - KP_MODE_BITS: Shift width for cipher feedback mode.
3573 * (Currently ignored by MS CSP's - always eight)
3574 * - KP_PERMISSIONS: Or'ed combination of CRYPT_ENCRYPT, CRYPT_DECRYPT,
3575 * CRYPT_EXPORT, CRYPT_READ, CRYPT_WRITE, CRYPT_MAC
3576 * - KP_IV: Initialization vector.
3577 * - KP_KEYLEN: Bitwidth of the key.
3578 * - KP_BLOCKLEN: Size of a block cipher block.
3579 * - KP_SALT: Salt value.
3581 BOOL WINAPI
RSAENH_CPGetKeyParam(HCRYPTPROV hProv
, HCRYPTKEY hKey
, DWORD dwParam
, BYTE
*pbData
,
3582 DWORD
*pdwDataLen
, DWORD dwFlags
)
3584 CRYPTKEY
*pCryptKey
;
3587 TRACE("(hProv=%08lx, hKey=%08lx, dwParam=%08x, pbData=%p, pdwDataLen=%p dwFlags=%08x)\n",
3588 hProv
, hKey
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
3590 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
3592 SetLastError(NTE_BAD_UID
);
3597 SetLastError(NTE_BAD_FLAGS
);
3601 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
3603 SetLastError(NTE_BAD_KEY
);
3610 return copy_param(pbData
, pdwDataLen
, pCryptKey
->abInitVector
,
3611 pCryptKey
->dwBlockLen
);
3614 switch (pCryptKey
->aiAlgid
) {
3617 return copy_param(pbData
, pdwDataLen
,
3618 &pCryptKey
->abKeyValue
[pCryptKey
->dwKeyLen
],
3619 pCryptKey
->dwSaltLen
);
3621 SetLastError(NTE_BAD_KEY
);
3626 dwValue
= PKCS5_PADDING
;
3627 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&dwValue
, sizeof(DWORD
));
3630 dwValue
= pCryptKey
->dwKeyLen
<< 3;
3631 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&dwValue
, sizeof(DWORD
));
3633 case KP_EFFECTIVE_KEYLEN
:
3634 if (pCryptKey
->dwEffectiveKeyLen
)
3635 dwValue
= pCryptKey
->dwEffectiveKeyLen
;
3637 dwValue
= pCryptKey
->dwKeyLen
<< 3;
3638 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&dwValue
, sizeof(DWORD
));
3641 dwValue
= pCryptKey
->dwBlockLen
<< 3;
3642 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&dwValue
, sizeof(DWORD
));
3645 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&pCryptKey
->dwMode
, sizeof(DWORD
));
3648 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&pCryptKey
->dwModeBits
,
3651 case KP_PERMISSIONS
:
3652 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&pCryptKey
->dwPermissions
,
3656 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&pCryptKey
->aiAlgid
, sizeof(DWORD
));
3659 SetLastError(NTE_BAD_TYPE
);
3664 /******************************************************************************
3665 * CPGetProvParam (RSAENH.@)
3667 * Query a CSP parameter.
3670 * hProv [I] The key container that is to be queried.
3671 * dwParam [I] Specifies the parameter that is to be queried.
3672 * pbData [I] Pointer to the buffer where the parameter value will be stored.
3673 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
3674 * dwFlags [I] CRYPT_FIRST: Start enumeration (for PP_ENUMALGS{_EX}).
3680 * Defined dwParam types:
3681 * - PP_CONTAINER: Name of the key container.
3682 * - PP_NAME: Name of the cryptographic service provider.
3683 * - PP_SIG_KEYSIZE_INC: RSA signature keywidth granularity in bits.
3684 * - PP_KEYX_KEYSIZE_INC: RSA key-exchange keywidth granularity in bits.
3685 * - PP_ENUMALGS{_EX}: Query provider capabilities.
3686 * - PP_KEYSET_SEC_DESCR: Retrieve security descriptor on container.
3688 BOOL WINAPI
RSAENH_CPGetProvParam(HCRYPTPROV hProv
, DWORD dwParam
, BYTE
*pbData
,
3689 DWORD
*pdwDataLen
, DWORD dwFlags
)
3691 KEYCONTAINER
*pKeyContainer
;
3692 PROV_ENUMALGS provEnumalgs
;
3696 /* This is for dwParam PP_CRYPT_COUNT_KEY_USE.
3697 * IE6 SP1 asks for it in the 'About' dialog.
3698 * Returning this BLOB seems to satisfy IE. The marked 0x00 seem
3699 * to be 'don't care's. If you know anything more specific about
3700 * this provider parameter, please report to wine-devel@winehq.org */
3701 static const BYTE abWTF
[96] = {
3702 0xb0, 0x25, 0x63, 0x86, 0x9c, 0xab, 0xb6, 0x37,
3703 0xe8, 0x82, /**/0x00,/**/ 0x72, 0x06, 0xb2, /**/0x00,/**/ 0x3b,
3704 0x60, 0x35, /**/0x00,/**/ 0x3b, 0x88, 0xce, /**/0x00,/**/ 0x82,
3705 0xbc, 0x7a, /**/0x00,/**/ 0xb7, 0x4f, 0x7e, /**/0x00,/**/ 0xde,
3706 0x92, 0xf1, /**/0x00,/**/ 0x83, 0xea, 0x5e, /**/0x00,/**/ 0xc8,
3707 0x12, 0x1e, 0xd4, 0x06, 0xf7, 0x66, /**/0x00,/**/ 0x01,
3708 0x29, 0xa4, /**/0x00,/**/ 0xf8, 0x24, 0x0c, /**/0x00,/**/ 0x33,
3709 0x06, 0x80, /**/0x00,/**/ 0x02, 0x46, 0x0b, /**/0x00,/**/ 0x6d,
3710 0x5b, 0xca, /**/0x00,/**/ 0x9a, 0x10, 0xf0, /**/0x00,/**/ 0x05,
3711 0x19, 0xd0, /**/0x00,/**/ 0x2c, 0xf6, 0x27, /**/0x00,/**/ 0xaa,
3712 0x7c, 0x6f, /**/0x00,/**/ 0xb9, 0xd8, 0x72, /**/0x00,/**/ 0x03,
3713 0xf3, 0x81, /**/0x00,/**/ 0xfa, 0xe8, 0x26, /**/0x00,/**/ 0xca
3716 TRACE("(hProv=%08lx, dwParam=%08x, pbData=%p, pdwDataLen=%p, dwFlags=%08x)\n",
3717 hProv
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
3720 SetLastError(ERROR_INVALID_PARAMETER
);
3724 if (!lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
,
3725 (OBJECTHDR
**)&pKeyContainer
))
3727 /* MSDN: hProv not containing valid context handle */
3728 SetLastError(NTE_BAD_UID
);
3735 case PP_UNIQUE_CONTAINER
:/* MSDN says we can return the same value as PP_CONTAINER */
3736 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)pKeyContainer
->szName
,
3737 strlen(pKeyContainer
->szName
)+1);
3740 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)pKeyContainer
->szProvName
,
3741 strlen(pKeyContainer
->szProvName
)+1);
3744 dwTemp
= PROV_RSA_FULL
;
3745 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&dwTemp
, sizeof(dwTemp
));
3748 dwTemp
= AT_SIGNATURE
| AT_KEYEXCHANGE
;
3749 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&dwTemp
, sizeof(dwTemp
));
3751 case PP_KEYSET_TYPE
:
3752 dwTemp
= pKeyContainer
->dwFlags
& CRYPT_MACHINE_KEYSET
;
3753 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&dwTemp
, sizeof(dwTemp
));
3756 dwTemp
= CRYPT_SEC_DESCR
;
3757 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&dwTemp
, sizeof(dwTemp
));
3759 case PP_SIG_KEYSIZE_INC
:
3760 case PP_KEYX_KEYSIZE_INC
:
3762 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&dwTemp
, sizeof(dwTemp
));
3765 dwTemp
= CRYPT_IMPL_SOFTWARE
;
3766 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&dwTemp
, sizeof(dwTemp
));
3769 dwTemp
= 0x00000200;
3770 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&dwTemp
, sizeof(dwTemp
));
3772 case PP_ENUMCONTAINERS
:
3773 if ((dwFlags
& CRYPT_FIRST
) == CRYPT_FIRST
) pKeyContainer
->dwEnumContainersCtr
= 0;
3776 *pdwDataLen
= (DWORD
)MAX_PATH
+ 1;
3780 if (!open_container_key("", dwFlags
, KEY_READ
, &hKey
))
3782 SetLastError(ERROR_NO_MORE_ITEMS
);
3786 dwTemp
= *pdwDataLen
;
3787 switch (RegEnumKeyExA(hKey
, pKeyContainer
->dwEnumContainersCtr
, (LPSTR
)pbData
, &dwTemp
,
3788 NULL
, NULL
, NULL
, NULL
))
3790 case ERROR_MORE_DATA
:
3791 *pdwDataLen
= (DWORD
)MAX_PATH
+ 1;
3794 pKeyContainer
->dwEnumContainersCtr
++;
3798 case ERROR_NO_MORE_ITEMS
:
3800 SetLastError(ERROR_NO_MORE_ITEMS
);
3806 case PP_ENUMALGS_EX
:
3807 if (((pKeyContainer
->dwEnumAlgsCtr
>= RSAENH_MAX_ENUMALGS
-1) ||
3808 (!aProvEnumAlgsEx
[pKeyContainer
->dwPersonality
]
3809 [pKeyContainer
->dwEnumAlgsCtr
+1].aiAlgid
)) &&
3810 ((dwFlags
& CRYPT_FIRST
) != CRYPT_FIRST
))
3812 SetLastError(ERROR_NO_MORE_ITEMS
);
3816 if (dwParam
== PP_ENUMALGS
) {
3817 if (pbData
&& (*pdwDataLen
>= sizeof(PROV_ENUMALGS
)))
3818 pKeyContainer
->dwEnumAlgsCtr
= ((dwFlags
& CRYPT_FIRST
) == CRYPT_FIRST
) ?
3819 0 : pKeyContainer
->dwEnumAlgsCtr
+1;
3821 provEnumalgs
.aiAlgid
= aProvEnumAlgsEx
3822 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
].aiAlgid
;
3823 provEnumalgs
.dwBitLen
= aProvEnumAlgsEx
3824 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
].dwDefaultLen
;
3825 provEnumalgs
.dwNameLen
= aProvEnumAlgsEx
3826 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
].dwNameLen
;
3827 memcpy(provEnumalgs
.szName
, aProvEnumAlgsEx
3828 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
].szName
,
3831 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&provEnumalgs
,
3832 sizeof(PROV_ENUMALGS
));
3834 if (pbData
&& (*pdwDataLen
>= sizeof(PROV_ENUMALGS_EX
)))
3835 pKeyContainer
->dwEnumAlgsCtr
= ((dwFlags
& CRYPT_FIRST
) == CRYPT_FIRST
) ?
3836 0 : pKeyContainer
->dwEnumAlgsCtr
+1;
3838 return copy_param(pbData
, pdwDataLen
,
3839 (const BYTE
*)&aProvEnumAlgsEx
3840 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
],
3841 sizeof(PROV_ENUMALGS_EX
));
3844 case PP_CRYPT_COUNT_KEY_USE
: /* Asked for by IE About dialog */
3845 return copy_param(pbData
, pdwDataLen
, abWTF
, sizeof(abWTF
));
3847 case PP_KEYSET_SEC_DESCR
:
3849 SECURITY_DESCRIPTOR
*sd
;
3850 DWORD err
, len
, flags
= (pKeyContainer
->dwFlags
& CRYPT_MACHINE_KEYSET
);
3852 if (!open_container_key(pKeyContainer
->szName
, flags
, KEY_READ
, &hKey
))
3854 SetLastError(NTE_BAD_KEYSET
);
3858 err
= GetSecurityInfo(hKey
, SE_REGISTRY_KEY
, dwFlags
, NULL
, NULL
, NULL
, NULL
, (void **)&sd
);
3866 len
= GetSecurityDescriptorLength(sd
);
3867 if (*pdwDataLen
>= len
) memcpy(pbData
, sd
, len
);
3868 else SetLastError(ERROR_INSUFFICIENT_BUFFER
);
3876 /* MSDN: Unknown parameter number in dwParam */
3877 SetLastError(NTE_BAD_TYPE
);
3882 /******************************************************************************
3883 * CPDeriveKey (RSAENH.@)
3885 * Derives a key from a hash value.
3888 * hProv [I] Key container for which a key is to be generated.
3889 * Algid [I] Crypto algorithm identifier for the key to be generated.
3890 * hBaseData [I] Hash from whose value the key will be derived.
3891 * dwFlags [I] See Notes.
3892 * phKey [O] The generated key.
3900 * - CRYPT_EXPORTABLE: Key can be exported.
3901 * - CRYPT_NO_SALT: No salt is used for 40 bit keys.
3902 * - CRYPT_CREATE_SALT: Use remaining bits as salt value.
3904 BOOL WINAPI
RSAENH_CPDeriveKey(HCRYPTPROV hProv
, ALG_ID Algid
, HCRYPTHASH hBaseData
,
3905 DWORD dwFlags
, HCRYPTKEY
*phKey
)
3907 CRYPTKEY
*pCryptKey
, *pMasterKey
;
3908 CRYPTHASH
*pCryptHash
;
3909 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
*2];
3912 TRACE("(hProv=%08lx, Algid=%d, hBaseData=%08lx, dwFlags=%08x phKey=%p)\n", hProv
, Algid
,
3913 hBaseData
, dwFlags
, phKey
);
3915 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
3917 SetLastError(NTE_BAD_UID
);
3921 if (!lookup_handle(&handle_table
, hBaseData
, RSAENH_MAGIC_HASH
,
3922 (OBJECTHDR
**)&pCryptHash
))
3924 SetLastError(NTE_BAD_HASH
);
3930 SetLastError(ERROR_INVALID_PARAMETER
);
3934 switch (GET_ALG_CLASS(Algid
))
3936 case ALG_CLASS_DATA_ENCRYPT
:
3939 *phKey
= new_key(hProv
, Algid
, dwFlags
, &pCryptKey
);
3940 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
3943 * We derive the key material from the hash.
3944 * If the hash value is not large enough for the claimed key, we have to construct
3945 * a larger binary value based on the hash. This is documented in MSDN: CryptDeriveKey.
3947 dwLen
= RSAENH_MAX_HASH_SIZE
;
3948 RSAENH_CPGetHashParam(pCryptHash
->hProv
, hBaseData
, HP_HASHVAL
, abHashValue
, &dwLen
, 0);
3951 * The usage of padding seems to vary from algorithm to algorithm.
3952 * For now the only different case found was for AES with 128 bit key.
3957 /* To reduce the chance of regressions we will only deviate
3958 * from the old behavior for the tested hash lengths */
3959 if (dwLen
== 16 || dwLen
== 20)
3965 need_padding
= dwLen
< pCryptKey
->dwKeyLen
;
3969 BYTE pad1
[RSAENH_HMAC_DEF_PAD_LEN
], pad2
[RSAENH_HMAC_DEF_PAD_LEN
];
3970 BYTE old_hashval
[RSAENH_MAX_HASH_SIZE
];
3973 memcpy(old_hashval
, pCryptHash
->abHashValue
, RSAENH_MAX_HASH_SIZE
);
3975 for (i
=0; i
<RSAENH_HMAC_DEF_PAD_LEN
; i
++) {
3976 pad1
[i
] = RSAENH_HMAC_DEF_IPAD_CHAR
^ (i
<dwLen
? abHashValue
[i
] : 0);
3977 pad2
[i
] = RSAENH_HMAC_DEF_OPAD_CHAR
^ (i
<dwLen
? abHashValue
[i
] : 0);
3980 init_hash(pCryptHash
);
3981 update_hash(pCryptHash
, pad1
, RSAENH_HMAC_DEF_PAD_LEN
);
3982 finalize_hash(pCryptHash
);
3983 memcpy(abHashValue
, pCryptHash
->abHashValue
, pCryptHash
->dwHashSize
);
3985 init_hash(pCryptHash
);
3986 update_hash(pCryptHash
, pad2
, RSAENH_HMAC_DEF_PAD_LEN
);
3987 finalize_hash(pCryptHash
);
3988 memcpy(abHashValue
+pCryptHash
->dwHashSize
, pCryptHash
->abHashValue
,
3989 pCryptHash
->dwHashSize
);
3991 memcpy(pCryptHash
->abHashValue
, old_hashval
, RSAENH_MAX_HASH_SIZE
);
3994 memcpy(pCryptKey
->abKeyValue
, abHashValue
,
3995 RSAENH_MIN(pCryptKey
->dwKeyLen
, sizeof(pCryptKey
->abKeyValue
)));
3998 case ALG_CLASS_MSG_ENCRYPT
:
3999 if (!lookup_handle(&handle_table
, pCryptHash
->hKey
, RSAENH_MAGIC_KEY
,
4000 (OBJECTHDR
**)&pMasterKey
))
4002 SetLastError(NTE_FAIL
); /* FIXME error code */
4008 /* See RFC 2246, chapter 6.3 Key calculation */
4009 case CALG_SCHANNEL_ENC_KEY
:
4010 if (!pMasterKey
->siSChannelInfo
.saEncAlg
.Algid
||
4011 !pMasterKey
->siSChannelInfo
.saEncAlg
.cBits
)
4013 SetLastError(NTE_BAD_FLAGS
);
4016 *phKey
= new_key(hProv
, pMasterKey
->siSChannelInfo
.saEncAlg
.Algid
,
4017 MAKELONG(LOWORD(dwFlags
),pMasterKey
->siSChannelInfo
.saEncAlg
.cBits
),
4019 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
4020 memcpy(pCryptKey
->abKeyValue
,
4021 pCryptHash
->abHashValue
+ (
4022 2 * (pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
/ 8) +
4023 ((dwFlags
& CRYPT_SERVER
) ?
4024 (pMasterKey
->siSChannelInfo
.saEncAlg
.cBits
/ 8) : 0)),
4025 pMasterKey
->siSChannelInfo
.saEncAlg
.cBits
/ 8);
4026 memcpy(pCryptKey
->abInitVector
,
4027 pCryptHash
->abHashValue
+ (
4028 2 * (pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
/ 8) +
4029 2 * (pMasterKey
->siSChannelInfo
.saEncAlg
.cBits
/ 8) +
4030 ((dwFlags
& CRYPT_SERVER
) ? pCryptKey
->dwBlockLen
: 0)),
4031 pCryptKey
->dwBlockLen
);
4034 case CALG_SCHANNEL_MAC_KEY
:
4035 *phKey
= new_key(hProv
, Algid
,
4036 MAKELONG(LOWORD(dwFlags
),pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
),
4038 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
4039 memcpy(pCryptKey
->abKeyValue
,
4040 pCryptHash
->abHashValue
+ ((dwFlags
& CRYPT_SERVER
) ?
4041 pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
/ 8 : 0),
4042 pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
/ 8);
4046 SetLastError(NTE_BAD_ALGID
);
4052 SetLastError(NTE_BAD_ALGID
);
4056 setup_key(pCryptKey
);
4060 /******************************************************************************
4061 * CPGetUserKey (RSAENH.@)
4063 * Returns a handle to the user's private key-exchange- or signature-key.
4066 * hProv [I] The key container from which a user key is requested.
4067 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
4068 * phUserKey [O] Handle to the requested key or INVALID_HANDLE_VALUE in case of failure.
4075 * A newly created key container does not contain private user key. Create them with CPGenKey.
4077 BOOL WINAPI
RSAENH_CPGetUserKey(HCRYPTPROV hProv
, DWORD dwKeySpec
, HCRYPTKEY
*phUserKey
)
4079 KEYCONTAINER
*pKeyContainer
;
4081 TRACE("(hProv=%08lx, dwKeySpec=%08x, phUserKey=%p)\n", hProv
, dwKeySpec
, phUserKey
);
4083 if (!lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
,
4084 (OBJECTHDR
**)&pKeyContainer
))
4086 /* MSDN: hProv not containing valid context handle */
4087 SetLastError(NTE_BAD_UID
);
4093 case AT_KEYEXCHANGE
:
4094 copy_handle(&handle_table
, pKeyContainer
->hKeyExchangeKeyPair
, RSAENH_MAGIC_KEY
,
4099 copy_handle(&handle_table
, pKeyContainer
->hSignatureKeyPair
, RSAENH_MAGIC_KEY
,
4104 *phUserKey
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
4107 if (*phUserKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
4109 /* MSDN: dwKeySpec parameter specifies nonexistent key */
4110 SetLastError(NTE_NO_KEY
);
4117 /******************************************************************************
4118 * CPHashData (RSAENH.@)
4120 * Updates a hash object with the given data.
4123 * hProv [I] Key container to which the hash object belongs.
4124 * hHash [I] Hash object which is to be updated.
4125 * pbData [I] Pointer to data with which the hash object is to be updated.
4126 * dwDataLen [I] Length of the data.
4127 * dwFlags [I] Currently none defined.
4134 * The actual hash value is queried with CPGetHashParam, which will finalize
4135 * the hash. Updating a finalized hash will fail with a last error NTE_BAD_HASH_STATE.
4137 BOOL WINAPI
RSAENH_CPHashData(HCRYPTPROV hProv
, HCRYPTHASH hHash
, const BYTE
*pbData
,
4138 DWORD dwDataLen
, DWORD dwFlags
)
4140 CRYPTHASH
*pCryptHash
;
4142 TRACE("(hProv=%08lx, hHash=%08lx, pbData=%p, dwDataLen=%d, dwFlags=%08x)\n",
4143 hProv
, hHash
, pbData
, dwDataLen
, dwFlags
);
4145 if (dwFlags
& ~CRYPT_USERDATA
)
4147 SetLastError(NTE_BAD_FLAGS
);
4151 if (!lookup_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
,
4152 (OBJECTHDR
**)&pCryptHash
))
4154 SetLastError(NTE_BAD_HASH
);
4158 if (!get_algid_info(hProv
, pCryptHash
->aiAlgid
) || pCryptHash
->aiAlgid
== CALG_SSL3_SHAMD5
)
4160 SetLastError(NTE_BAD_ALGID
);
4164 if (pCryptHash
->dwState
!= RSAENH_HASHSTATE_HASHING
)
4166 SetLastError(NTE_BAD_HASH_STATE
);
4170 update_hash(pCryptHash
, pbData
, dwDataLen
);
4174 /******************************************************************************
4175 * CPHashSessionKey (RSAENH.@)
4177 * Updates a hash object with the binary representation of a symmetric key.
4180 * hProv [I] Key container to which the hash object belongs.
4181 * hHash [I] Hash object which is to be updated.
4182 * hKey [I] The symmetric key, whose binary value will be added to the hash.
4183 * dwFlags [I] CRYPT_LITTLE_ENDIAN, if the binary key value shall be interpreted as little endian.
4189 BOOL WINAPI
RSAENH_CPHashSessionKey(HCRYPTPROV hProv
, HCRYPTHASH hHash
, HCRYPTKEY hKey
,
4192 BYTE abKeyValue
[RSAENH_MAX_KEY_SIZE
], bTemp
;
4196 TRACE("(hProv=%08lx, hHash=%08lx, hKey=%08lx, dwFlags=%08x)\n", hProv
, hHash
, hKey
, dwFlags
);
4198 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pKey
) ||
4199 (GET_ALG_CLASS(pKey
->aiAlgid
) != ALG_CLASS_DATA_ENCRYPT
))
4201 SetLastError(NTE_BAD_KEY
);
4205 if (dwFlags
& ~CRYPT_LITTLE_ENDIAN
) {
4206 SetLastError(NTE_BAD_FLAGS
);
4210 memcpy(abKeyValue
, pKey
->abKeyValue
, pKey
->dwKeyLen
);
4211 if (!(dwFlags
& CRYPT_LITTLE_ENDIAN
)) {
4212 for (i
=0; i
<pKey
->dwKeyLen
/2; i
++) {
4213 bTemp
= abKeyValue
[i
];
4214 abKeyValue
[i
] = abKeyValue
[pKey
->dwKeyLen
-i
-1];
4215 abKeyValue
[pKey
->dwKeyLen
-i
-1] = bTemp
;
4219 return RSAENH_CPHashData(hProv
, hHash
, abKeyValue
, pKey
->dwKeyLen
, 0);
4222 /******************************************************************************
4223 * CPReleaseContext (RSAENH.@)
4225 * Release a key container.
4228 * hProv [I] Key container to be released.
4229 * dwFlags [I] Currently none defined.
4235 BOOL WINAPI
RSAENH_CPReleaseContext(HCRYPTPROV hProv
, DWORD dwFlags
)
4237 TRACE("(hProv=%08lx, dwFlags=%08x)\n", hProv
, dwFlags
);
4239 if (!release_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
4241 /* MSDN: hProv not containing valid context handle */
4242 SetLastError(NTE_BAD_UID
);
4247 SetLastError(NTE_BAD_FLAGS
);
4254 /******************************************************************************
4255 * CPSetHashParam (RSAENH.@)
4257 * Set a parameter of a hash object
4260 * hProv [I] The key container to which the key belongs.
4261 * hHash [I] The hash object for which a parameter is to be set.
4262 * dwParam [I] Parameter type. See Notes.
4263 * pbData [I] Pointer to the parameter value.
4264 * dwFlags [I] Currently none defined.
4271 * Currently only the HP_HMAC_INFO dwParam type is defined.
4272 * The HMAC_INFO struct will be deep copied into the hash object.
4273 * See Internet RFC 2104 for details on the HMAC algorithm.
4275 BOOL WINAPI
RSAENH_CPSetHashParam(HCRYPTPROV hProv
, HCRYPTHASH hHash
, DWORD dwParam
,
4276 BYTE
*pbData
, DWORD dwFlags
)
4278 CRYPTHASH
*pCryptHash
;
4279 CRYPTKEY
*pCryptKey
;
4282 TRACE("(hProv=%08lx, hHash=%08lx, dwParam=%08x, pbData=%p, dwFlags=%08x)\n",
4283 hProv
, hHash
, dwParam
, pbData
, dwFlags
);
4285 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
4287 SetLastError(NTE_BAD_UID
);
4292 SetLastError(NTE_BAD_FLAGS
);
4296 if (!lookup_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
,
4297 (OBJECTHDR
**)&pCryptHash
))
4299 SetLastError(NTE_BAD_HASH
);
4305 free_hmac_info(pCryptHash
->pHMACInfo
);
4306 if (!copy_hmac_info(&pCryptHash
->pHMACInfo
, (PHMAC_INFO
)pbData
)) return FALSE
;
4308 if (!lookup_handle(&handle_table
, pCryptHash
->hKey
, RSAENH_MAGIC_KEY
,
4309 (OBJECTHDR
**)&pCryptKey
))
4311 SetLastError(NTE_FAIL
); /* FIXME: correct error code? */
4315 if (pCryptKey
->aiAlgid
== CALG_HMAC
&& !pCryptKey
->dwKeyLen
) {
4316 HCRYPTHASH hKeyHash
;
4319 if (!RSAENH_CPCreateHash(hProv
, ((PHMAC_INFO
)pbData
)->HashAlgid
, 0, 0,
4322 if (!RSAENH_CPHashData(hProv
, hKeyHash
, pCryptKey
->blobHmacKey
.pbData
,
4323 pCryptKey
->blobHmacKey
.cbData
, 0))
4325 RSAENH_CPDestroyHash(hProv
, hKeyHash
);
4328 keyLen
= sizeof(pCryptKey
->abKeyValue
);
4329 if (!RSAENH_CPGetHashParam(hProv
, hKeyHash
, HP_HASHVAL
, pCryptKey
->abKeyValue
,
4332 RSAENH_CPDestroyHash(hProv
, hKeyHash
);
4335 pCryptKey
->dwKeyLen
= keyLen
;
4336 RSAENH_CPDestroyHash(hProv
, hKeyHash
);
4338 for (i
=0; i
<RSAENH_MIN(pCryptKey
->dwKeyLen
,pCryptHash
->pHMACInfo
->cbInnerString
); i
++) {
4339 pCryptHash
->pHMACInfo
->pbInnerString
[i
] ^= pCryptKey
->abKeyValue
[i
];
4341 for (i
=0; i
<RSAENH_MIN(pCryptKey
->dwKeyLen
,pCryptHash
->pHMACInfo
->cbOuterString
); i
++) {
4342 pCryptHash
->pHMACInfo
->pbOuterString
[i
] ^= pCryptKey
->abKeyValue
[i
];
4345 init_hash(pCryptHash
);
4349 memcpy(pCryptHash
->abHashValue
, pbData
, pCryptHash
->dwHashSize
);
4350 pCryptHash
->dwState
= RSAENH_HASHSTATE_FINISHED
;
4353 case HP_TLS1PRF_SEED
:
4354 return copy_data_blob(&pCryptHash
->tpPRFParams
.blobSeed
, (PCRYPT_DATA_BLOB
)pbData
);
4356 case HP_TLS1PRF_LABEL
:
4357 return copy_data_blob(&pCryptHash
->tpPRFParams
.blobLabel
, (PCRYPT_DATA_BLOB
)pbData
);
4360 SetLastError(NTE_BAD_TYPE
);
4365 /******************************************************************************
4366 * CPSetProvParam (RSAENH.@)
4368 BOOL WINAPI
RSAENH_CPSetProvParam(HCRYPTPROV hProv
, DWORD dwParam
, BYTE
*pbData
, DWORD dwFlags
)
4370 KEYCONTAINER
*pKeyContainer
;
4373 TRACE("(hProv=%08lx, dwParam=%08x, pbData=%p, dwFlags=%08x)\n", hProv
, dwParam
, pbData
, dwFlags
);
4375 if (!lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
, (OBJECTHDR
**)&pKeyContainer
))
4377 SetLastError(NTE_BAD_UID
);
4383 case PP_KEYSET_SEC_DESCR
:
4385 SECURITY_DESCRIPTOR
*sd
= (SECURITY_DESCRIPTOR
*)pbData
;
4386 DWORD err
, flags
= (pKeyContainer
->dwFlags
& CRYPT_MACHINE_KEYSET
);
4388 REGSAM access
= WRITE_DAC
| WRITE_OWNER
| ACCESS_SYSTEM_SECURITY
;
4389 PSID owner
= NULL
, group
= NULL
;
4390 PACL dacl
= NULL
, sacl
= NULL
;
4392 if (!open_container_key(pKeyContainer
->szName
, flags
, access
, &hKey
))
4394 SetLastError(NTE_BAD_KEYSET
);
4398 if ((dwFlags
& OWNER_SECURITY_INFORMATION
&& !GetSecurityDescriptorOwner(sd
, &owner
, &def
)) ||
4399 (dwFlags
& GROUP_SECURITY_INFORMATION
&& !GetSecurityDescriptorGroup(sd
, &group
, &def
)) ||
4400 (dwFlags
& DACL_SECURITY_INFORMATION
&& !GetSecurityDescriptorDacl(sd
, &present
, &dacl
, &def
)) ||
4401 (dwFlags
& SACL_SECURITY_INFORMATION
&& !GetSecurityDescriptorSacl(sd
, &present
, &sacl
, &def
)))
4407 err
= SetSecurityInfo(hKey
, SE_REGISTRY_KEY
, dwFlags
, owner
, group
, dacl
, sacl
);
4417 FIXME("unimplemented parameter %08x\n", dwParam
);
4422 /******************************************************************************
4423 * CPSignHash (RSAENH.@)
4425 * Sign a hash object
4428 * hProv [I] The key container, to which the hash object belongs.
4429 * hHash [I] The hash object to be signed.
4430 * dwKeySpec [I] AT_SIGNATURE or AT_KEYEXCHANGE: Key used to generate the signature.
4431 * sDescription [I] Should be NULL for security reasons.
4432 * dwFlags [I] 0, CRYPT_NOHASHOID or CRYPT_X931_FORMAT: Format of the signature.
4433 * pbSignature [O] Buffer, to which the signature will be stored. May be NULL to query SigLen.
4434 * pdwSigLen [I/O] Size of the buffer (in), Length of the signature (out)
4440 BOOL WINAPI
RSAENH_CPSignHash(HCRYPTPROV hProv
, HCRYPTHASH hHash
, DWORD dwKeySpec
,
4441 LPCWSTR sDescription
, DWORD dwFlags
, BYTE
*pbSignature
,
4444 HCRYPTKEY hCryptKey
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
4445 CRYPTKEY
*pCryptKey
;
4447 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
];
4451 TRACE("(hProv=%08lx, hHash=%08lx, dwKeySpec=%08x, sDescription=%s, dwFlags=%08x, "
4452 "pbSignature=%p, pdwSigLen=%p)\n", hProv
, hHash
, dwKeySpec
, debugstr_w(sDescription
),
4453 dwFlags
, pbSignature
, pdwSigLen
);
4455 if (dwFlags
& ~(CRYPT_NOHASHOID
|CRYPT_X931_FORMAT
)) {
4456 SetLastError(NTE_BAD_FLAGS
);
4460 if (!RSAENH_CPGetUserKey(hProv
, dwKeySpec
, &hCryptKey
)) return FALSE
;
4462 if (!lookup_handle(&handle_table
, hCryptKey
, RSAENH_MAGIC_KEY
,
4463 (OBJECTHDR
**)&pCryptKey
))
4465 SetLastError(NTE_NO_KEY
);
4470 *pdwSigLen
= pCryptKey
->dwKeyLen
;
4474 if (pCryptKey
->dwKeyLen
> *pdwSigLen
)
4476 SetLastError(ERROR_MORE_DATA
);
4477 *pdwSigLen
= pCryptKey
->dwKeyLen
;
4480 *pdwSigLen
= pCryptKey
->dwKeyLen
;
4483 if (!RSAENH_CPHashData(hProv
, hHash
, (const BYTE
*)sDescription
,
4484 (DWORD
)lstrlenW(sDescription
)*sizeof(WCHAR
), 0))
4490 dwHashLen
= sizeof(DWORD
);
4491 if (!RSAENH_CPGetHashParam(hProv
, hHash
, HP_ALGID
, (BYTE
*)&aiAlgid
, &dwHashLen
, 0)) goto out
;
4493 dwHashLen
= RSAENH_MAX_HASH_SIZE
;
4494 if (!RSAENH_CPGetHashParam(hProv
, hHash
, HP_HASHVAL
, abHashValue
, &dwHashLen
, 0)) goto out
;
4497 if (!build_hash_signature(pbSignature
, *pdwSigLen
, aiAlgid
, abHashValue
, dwHashLen
, dwFlags
)) {
4501 ret
= encrypt_block_impl(pCryptKey
->aiAlgid
, PK_PRIVATE
, &pCryptKey
->context
, pbSignature
, pbSignature
, RSAENH_ENCRYPT
);
4503 RSAENH_CPDestroyKey(hProv
, hCryptKey
);
4507 /******************************************************************************
4508 * CPVerifySignature (RSAENH.@)
4510 * Verify the signature of a hash object.
4513 * hProv [I] The key container, to which the hash belongs.
4514 * hHash [I] The hash for which the signature is verified.
4515 * pbSignature [I] The binary signature.
4516 * dwSigLen [I] Length of the signature BLOB.
4517 * hPubKey [I] Public key used to verify the signature.
4518 * sDescription [I] Should be NULL for security reasons.
4519 * dwFlags [I] 0, CRYPT_NOHASHOID or CRYPT_X931_FORMAT: Format of the signature.
4522 * Success: TRUE (Signature is valid)
4523 * Failure: FALSE (GetLastError() == NTE_BAD_SIGNATURE, if signature is invalid)
4525 BOOL WINAPI
RSAENH_CPVerifySignature(HCRYPTPROV hProv
, HCRYPTHASH hHash
, const BYTE
*pbSignature
,
4526 DWORD dwSigLen
, HCRYPTKEY hPubKey
, LPCWSTR sDescription
,
4529 BYTE
*pbConstructed
= NULL
, *pbDecrypted
= NULL
;
4530 CRYPTKEY
*pCryptKey
;
4533 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
];
4536 TRACE("(hProv=%08lx, hHash=%08lx, pbSignature=%p, dwSigLen=%d, hPubKey=%08lx, sDescription=%s, "
4537 "dwFlags=%08x)\n", hProv
, hHash
, pbSignature
, dwSigLen
, hPubKey
, debugstr_w(sDescription
),
4540 if (dwFlags
& ~(CRYPT_NOHASHOID
|CRYPT_X931_FORMAT
)) {
4541 SetLastError(NTE_BAD_FLAGS
);
4545 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
4547 SetLastError(NTE_BAD_UID
);
4551 if (!lookup_handle(&handle_table
, hPubKey
, RSAENH_MAGIC_KEY
,
4552 (OBJECTHDR
**)&pCryptKey
))
4554 SetLastError(NTE_BAD_KEY
);
4558 /* in Microsoft implementation, the signature length is checked before
4559 * the signature pointer.
4561 if (dwSigLen
!= pCryptKey
->dwKeyLen
)
4563 SetLastError(NTE_BAD_SIGNATURE
);
4567 if (!hHash
|| !pbSignature
)
4569 SetLastError(ERROR_INVALID_PARAMETER
);
4574 if (!RSAENH_CPHashData(hProv
, hHash
, (const BYTE
*)sDescription
,
4575 (DWORD
)lstrlenW(sDescription
)*sizeof(WCHAR
), 0))
4581 dwHashLen
= sizeof(DWORD
);
4582 if (!RSAENH_CPGetHashParam(hProv
, hHash
, HP_ALGID
, (BYTE
*)&aiAlgid
, &dwHashLen
, 0)) return FALSE
;
4584 dwHashLen
= RSAENH_MAX_HASH_SIZE
;
4585 if (!RSAENH_CPGetHashParam(hProv
, hHash
, HP_HASHVAL
, abHashValue
, &dwHashLen
, 0)) return FALSE
;
4587 pbConstructed
= HeapAlloc(GetProcessHeap(), 0, dwSigLen
);
4588 if (!pbConstructed
) {
4589 SetLastError(NTE_NO_MEMORY
);
4593 pbDecrypted
= HeapAlloc(GetProcessHeap(), 0, dwSigLen
);
4595 SetLastError(NTE_NO_MEMORY
);
4599 if (!encrypt_block_impl(pCryptKey
->aiAlgid
, PK_PUBLIC
, &pCryptKey
->context
, pbSignature
, pbDecrypted
,
4605 if (build_hash_signature(pbConstructed
, dwSigLen
, aiAlgid
, abHashValue
, dwHashLen
, dwFlags
) &&
4606 !memcmp(pbDecrypted
, pbConstructed
, dwSigLen
)) {
4611 if (!(dwFlags
& CRYPT_NOHASHOID
) &&
4612 build_hash_signature(pbConstructed
, dwSigLen
, aiAlgid
, abHashValue
, dwHashLen
, dwFlags
|CRYPT_NOHASHOID
) &&
4613 !memcmp(pbDecrypted
, pbConstructed
, dwSigLen
)) {
4618 SetLastError(NTE_BAD_SIGNATURE
);
4621 HeapFree(GetProcessHeap(), 0, pbConstructed
);
4622 HeapFree(GetProcessHeap(), 0, pbDecrypted
);
4626 /******************************************************************************
4627 * DllRegisterServer (RSAENH.@)
4629 HRESULT WINAPI
DllRegisterServer(void)
4631 return __wine_register_resources( instance
);
4634 /******************************************************************************
4635 * DllUnregisterServer (RSAENH.@)
4637 HRESULT WINAPI
DllUnregisterServer(void)
4639 return __wine_unregister_resources( instance
);