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"
41 WINE_DEFAULT_DEBUG_CHANNEL(crypt
);
43 /******************************************************************************
44 * CRYPTHASH - hash objects
46 #define RSAENH_MAGIC_HASH 0x85938417u
47 #define RSAENH_MAX_HASH_SIZE 104
48 #define RSAENH_HASHSTATE_HASHING 1
49 #define RSAENH_HASHSTATE_FINISHED 2
50 typedef struct _RSAENH_TLS1PRF_PARAMS
52 CRYPT_DATA_BLOB blobLabel
;
53 CRYPT_DATA_BLOB blobSeed
;
54 } RSAENH_TLS1PRF_PARAMS
;
56 typedef struct tagCRYPTHASH
65 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
];
67 RSAENH_TLS1PRF_PARAMS tpPRFParams
;
70 /******************************************************************************
71 * CRYPTKEY - key objects
73 #define RSAENH_MAGIC_KEY 0x73620457u
74 #define RSAENH_MAX_KEY_SIZE 48
75 #define RSAENH_MAX_BLOCK_SIZE 24
76 #define RSAENH_KEYSTATE_IDLE 0
77 #define RSAENH_KEYSTATE_ENCRYPTING 1
78 #define RSAENH_KEYSTATE_MASTERKEY 2
79 typedef struct _RSAENH_SCHANNEL_INFO
81 SCHANNEL_ALG saEncAlg
;
82 SCHANNEL_ALG saMACAlg
;
83 CRYPT_DATA_BLOB blobClientRandom
;
84 CRYPT_DATA_BLOB blobServerRandom
;
85 } RSAENH_SCHANNEL_INFO
;
87 typedef struct tagCRYPTKEY
96 DWORD dwEffectiveKeyLen
;
101 BYTE abKeyValue
[RSAENH_MAX_KEY_SIZE
];
102 BYTE abInitVector
[RSAENH_MAX_BLOCK_SIZE
];
103 BYTE abChainVector
[RSAENH_MAX_BLOCK_SIZE
];
104 RSAENH_SCHANNEL_INFO siSChannelInfo
;
107 /******************************************************************************
108 * KEYCONTAINER - key containers
110 #define RSAENH_PERSONALITY_BASE 0u
111 #define RSAENH_PERSONALITY_STRONG 1u
112 #define RSAENH_PERSONALITY_ENHANCED 2u
113 #define RSAENH_PERSONALITY_SCHANNEL 3u
114 #define RSAENH_PERSONALITY_AES 4u
116 #define RSAENH_MAGIC_CONTAINER 0x26384993u
117 typedef struct tagKEYCONTAINER
123 DWORD dwEnumContainersCtr
;
124 CHAR szName
[MAX_PATH
];
125 CHAR szProvName
[MAX_PATH
];
126 HCRYPTKEY hKeyExchangeKeyPair
;
127 HCRYPTKEY hSignatureKeyPair
;
130 /******************************************************************************
131 * Some magic constants
133 #define RSAENH_ENCRYPT 1
134 #define RSAENH_DECRYPT 0
135 #define RSAENH_HMAC_DEF_IPAD_CHAR 0x36
136 #define RSAENH_HMAC_DEF_OPAD_CHAR 0x5c
137 #define RSAENH_HMAC_DEF_PAD_LEN 64
138 #define RSAENH_DES_EFFECTIVE_KEYLEN 56
139 #define RSAENH_DES_STORAGE_KEYLEN 64
140 #define RSAENH_3DES112_EFFECTIVE_KEYLEN 112
141 #define RSAENH_3DES112_STORAGE_KEYLEN 128
142 #define RSAENH_3DES_EFFECTIVE_KEYLEN 168
143 #define RSAENH_3DES_STORAGE_KEYLEN 192
144 #define RSAENH_MAGIC_RSA2 0x32415352
145 #define RSAENH_MAGIC_RSA1 0x31415352
146 #define RSAENH_PKC_BLOCKTYPE 0x02
147 #define RSAENH_SSL3_VERSION_MAJOR 3
148 #define RSAENH_SSL3_VERSION_MINOR 0
149 #define RSAENH_TLS1_VERSION_MAJOR 3
150 #define RSAENH_TLS1_VERSION_MINOR 1
151 #define RSAENH_REGKEY "Software\\Wine\\Crypto\\RSA\\%s"
153 #define RSAENH_MIN(a,b) ((a)<(b)?(a):(b))
154 /******************************************************************************
155 * aProvEnumAlgsEx - Defines the capabilities of the CSP personalities.
157 #define RSAENH_MAX_ENUMALGS 24
158 #define RSAENH_PCT1_SSL2_SSL3_TLS1 (CRYPT_FLAG_PCT1|CRYPT_FLAG_SSL2|CRYPT_FLAG_SSL3|CRYPT_FLAG_TLS1)
159 static const PROV_ENUMALGS_EX aProvEnumAlgsEx
[5][RSAENH_MAX_ENUMALGS
+1] =
162 {CALG_RC2
, 40, 40, 56,0, 4,"RC2", 24,"RSA Data Security's RC2"},
163 {CALG_RC4
, 40, 40, 56,0, 4,"RC4", 24,"RSA Data Security's RC4"},
164 {CALG_DES
, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
165 {CALG_SHA
, 160,160, 160,CRYPT_FLAG_SIGNING
, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
166 {CALG_MD2
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD2", 23,"Message Digest 2 (MD2)"},
167 {CALG_MD4
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD4", 23,"Message Digest 4 (MD4)"},
168 {CALG_MD5
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD5", 23,"Message Digest 5 (MD5)"},
169 {CALG_SSL3_SHAMD5
,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
170 {CALG_MAC
, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
171 {CALG_RSA_SIGN
, 512,384,16384,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_SIGN",14,"RSA Signature"},
172 {CALG_RSA_KEYX
, 512,384, 1024,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_KEYX",17,"RSA Key Exchange"},
173 {CALG_HMAC
, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
174 {0, 0, 0, 0,0, 1,"", 1,""}
177 {CALG_RC2
, 128, 40, 128,0, 4,"RC2", 24,"RSA Data Security's RC2"},
178 {CALG_RC4
, 128, 40, 128,0, 4,"RC4", 24,"RSA Data Security's RC4"},
179 {CALG_DES
, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
180 {CALG_3DES_112
, 112,112, 112,0, 13,"3DES TWO KEY",19,"Two Key Triple DES"},
181 {CALG_3DES
, 168,168, 168,0, 5,"3DES", 21,"Three Key Triple DES"},
182 {CALG_SHA
, 160,160, 160,CRYPT_FLAG_SIGNING
, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
183 {CALG_MD2
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD2", 23,"Message Digest 2 (MD2)"},
184 {CALG_MD4
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD4", 23,"Message Digest 4 (MD4)"},
185 {CALG_MD5
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD5", 23,"Message Digest 5 (MD5)"},
186 {CALG_SSL3_SHAMD5
,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
187 {CALG_MAC
, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
188 {CALG_RSA_SIGN
,1024,384,16384,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_SIGN",14,"RSA Signature"},
189 {CALG_RSA_KEYX
,1024,384,16384,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_KEYX",17,"RSA Key Exchange"},
190 {CALG_HMAC
, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
191 {0, 0, 0, 0,0, 1,"", 1,""}
194 {CALG_RC2
, 128, 40, 128,0, 4,"RC2", 24,"RSA Data Security's RC2"},
195 {CALG_RC4
, 128, 40, 128,0, 4,"RC4", 24,"RSA Data Security's RC4"},
196 {CALG_DES
, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
197 {CALG_3DES_112
, 112,112, 112,0, 13,"3DES TWO KEY",19,"Two Key Triple DES"},
198 {CALG_3DES
, 168,168, 168,0, 5,"3DES", 21,"Three Key Triple DES"},
199 {CALG_SHA
, 160,160, 160,CRYPT_FLAG_SIGNING
, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
200 {CALG_MD2
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD2", 23,"Message Digest 2 (MD2)"},
201 {CALG_MD4
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD4", 23,"Message Digest 4 (MD4)"},
202 {CALG_MD5
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD5", 23,"Message Digest 5 (MD5)"},
203 {CALG_SSL3_SHAMD5
,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
204 {CALG_MAC
, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
205 {CALG_RSA_SIGN
,1024,384,16384,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_SIGN",14,"RSA Signature"},
206 {CALG_RSA_KEYX
,1024,384,16384,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_KEYX",17,"RSA Key Exchange"},
207 {CALG_HMAC
, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
208 {0, 0, 0, 0,0, 1,"", 1,""}
211 {CALG_RC2
, 128, 40, 128,RSAENH_PCT1_SSL2_SSL3_TLS1
, 4,"RC2", 24,"RSA Data Security's RC2"},
212 {CALG_RC4
, 128, 40, 128,RSAENH_PCT1_SSL2_SSL3_TLS1
, 4,"RC4", 24,"RSA Data Security's RC4"},
213 {CALG_DES
, 56, 56, 56,RSAENH_PCT1_SSL2_SSL3_TLS1
, 4,"DES", 31,"Data Encryption Standard (DES)"},
214 {CALG_3DES_112
, 112,112, 112,RSAENH_PCT1_SSL2_SSL3_TLS1
,13,"3DES TWO KEY",19,"Two Key Triple DES"},
215 {CALG_3DES
, 168,168, 168,RSAENH_PCT1_SSL2_SSL3_TLS1
, 5,"3DES", 21,"Three Key Triple DES"},
216 {CALG_SHA
,160,160,160,CRYPT_FLAG_SIGNING
|RSAENH_PCT1_SSL2_SSL3_TLS1
,6,"SHA-1",30,"Secure Hash Algorithm (SHA-1)"},
217 {CALG_MD5
,128,128,128,CRYPT_FLAG_SIGNING
|RSAENH_PCT1_SSL2_SSL3_TLS1
,4,"MD5",23,"Message Digest 5 (MD5)"},
218 {CALG_SSL3_SHAMD5
,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
219 {CALG_MAC
, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
220 {CALG_RSA_SIGN
,1024,384,16384,CRYPT_FLAG_SIGNING
|RSAENH_PCT1_SSL2_SSL3_TLS1
,9,"RSA_SIGN",14,"RSA Signature"},
221 {CALG_RSA_KEYX
,1024,384,16384,CRYPT_FLAG_SIGNING
|RSAENH_PCT1_SSL2_SSL3_TLS1
,9,"RSA_KEYX",17,"RSA Key Exchange"},
222 {CALG_HMAC
, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
223 {CALG_PCT1_MASTER
,128,128,128,CRYPT_FLAG_PCT1
, 12,"PCT1 MASTER",12,"PCT1 Master"},
224 {CALG_SSL2_MASTER
,40,40, 192,CRYPT_FLAG_SSL2
, 12,"SSL2 MASTER",12,"SSL2 Master"},
225 {CALG_SSL3_MASTER
,384,384,384,CRYPT_FLAG_SSL3
, 12,"SSL3 MASTER",12,"SSL3 Master"},
226 {CALG_TLS1_MASTER
,384,384,384,CRYPT_FLAG_TLS1
, 12,"TLS1 MASTER",12,"TLS1 Master"},
227 {CALG_SCHANNEL_MASTER_HASH
,0,0,-1,0, 16,"SCH MASTER HASH",21,"SChannel Master Hash"},
228 {CALG_SCHANNEL_MAC_KEY
,0,0,-1,0, 12,"SCH MAC KEY",17,"SChannel MAC Key"},
229 {CALG_SCHANNEL_ENC_KEY
,0,0,-1,0, 12,"SCH ENC KEY",24,"SChannel Encryption Key"},
230 {CALG_TLS1PRF
, 0, 0, -1,0, 9,"TLS1 PRF", 28,"TLS1 Pseudo Random Function"},
231 {0, 0, 0, 0,0, 1,"", 1,""}
234 {CALG_RC2
, 128, 40, 128,0, 4,"RC2", 24,"RSA Data Security's RC2"},
235 {CALG_RC4
, 128, 40, 128,0, 4,"RC4", 24,"RSA Data Security's RC4"},
236 {CALG_DES
, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
237 {CALG_3DES_112
, 112,112, 112,0, 13,"3DES TWO KEY",19,"Two Key Triple DES"},
238 {CALG_3DES
, 168,168, 168,0, 5,"3DES", 21,"Three Key Triple DES"},
239 {CALG_AES
, 128,128, 128,0, 4,"AES", 35,"Advanced Encryption Standard (AES)"},
240 {CALG_AES_128
, 128,128, 128,0, 8,"AES-128", 39,"Advanced Encryption Standard (AES-128)"},
241 {CALG_AES_192
, 192,192, 192,0, 8,"AES-192", 39,"Advanced Encryption Standard (AES-192)"},
242 {CALG_AES_256
, 256,256, 256,0, 8,"AES-256", 39,"Advanced Encryption Standard (AES-256)"},
243 {CALG_SHA
, 160,160, 160,CRYPT_FLAG_SIGNING
, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
244 {CALG_MD2
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD2", 23,"Message Digest 2 (MD2)"},
245 {CALG_MD4
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD4", 23,"Message Digest 4 (MD4)"},
246 {CALG_MD5
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD5", 23,"Message Digest 5 (MD5)"},
247 {CALG_SSL3_SHAMD5
,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
248 {CALG_MAC
, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
249 {CALG_RSA_SIGN
,1024,384,16384,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_SIGN",14,"RSA Signature"},
250 {CALG_RSA_KEYX
,1024,384,16384,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_KEYX",17,"RSA Key Exchange"},
251 {CALG_HMAC
, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
252 {0, 0, 0, 0,0, 1,"", 1,""}
256 /******************************************************************************
257 * API forward declarations
260 RSAENH_CPGetKeyParam(
291 RSAENH_CPSetHashParam(
295 BYTE
*pbData
, DWORD dwFlags
299 RSAENH_CPGetHashParam(
309 RSAENH_CPDestroyHash(
314 static BOOL
crypt_export_key(
324 static BOOL
import_key(
343 /******************************************************************************
344 * CSP's handle table (used by all acquired key containers)
346 static struct handle_table handle_table
;
348 /******************************************************************************
351 * Initializes and destroys the handle table for the CSP's handles.
353 int WINAPI
DllMain(HINSTANCE hInstance
, DWORD fdwReason
, PVOID pvReserved
)
357 case DLL_PROCESS_ATTACH
:
358 DisableThreadLibraryCalls(hInstance
);
359 init_handle_table(&handle_table
);
362 case DLL_PROCESS_DETACH
:
363 destroy_handle_table(&handle_table
);
369 /******************************************************************************
370 * copy_param [Internal]
372 * Helper function that supports the standard WINAPI protocol for querying data
376 * pbBuffer [O] Buffer where the queried parameter is copied to, if it is large enough.
377 * May be NUL if the required buffer size is to be queried only.
378 * pdwBufferSize [I/O] In: Size of the buffer at pbBuffer
379 * Out: Size of parameter pbParam
380 * pbParam [I] Parameter value.
381 * dwParamSize [I] Size of pbParam
384 * Success: TRUE (pbParam was copied into pbBuffer or pbBuffer is NULL)
385 * Failure: FALSE (pbBuffer is not large enough to hold pbParam). Last error: ERROR_MORE_DATA
387 static inline BOOL
copy_param(
388 BYTE
*pbBuffer
, DWORD
*pdwBufferSize
, CONST BYTE
*pbParam
, DWORD dwParamSize
)
392 if (dwParamSize
> *pdwBufferSize
)
394 SetLastError(ERROR_MORE_DATA
);
395 *pdwBufferSize
= dwParamSize
;
398 memcpy(pbBuffer
, pbParam
, dwParamSize
);
400 *pdwBufferSize
= dwParamSize
;
404 /******************************************************************************
405 * get_algid_info [Internal]
407 * Query CSP capabilities for a given crypto algorithm.
410 * hProv [I] Handle to a key container of the CSP whose capabilities are to be queried.
411 * algid [I] Identifier of the crypto algorithm about which information is requested.
414 * Success: Pointer to a PROV_ENUMALGS_EX struct containing information about the crypto algorithm.
415 * Failure: NULL (algid not supported)
417 static inline const PROV_ENUMALGS_EX
* get_algid_info(HCRYPTPROV hProv
, ALG_ID algid
) {
418 const PROV_ENUMALGS_EX
*iterator
;
419 KEYCONTAINER
*pKeyContainer
;
421 if (!lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
, (OBJECTHDR
**)&pKeyContainer
)) {
422 SetLastError(NTE_BAD_UID
);
426 for (iterator
= aProvEnumAlgsEx
[pKeyContainer
->dwPersonality
]; iterator
->aiAlgid
; iterator
++) {
427 if (iterator
->aiAlgid
== algid
) return iterator
;
430 SetLastError(NTE_BAD_ALGID
);
434 /******************************************************************************
435 * copy_data_blob [Internal]
437 * deeply copies a DATA_BLOB
440 * dst [O] That's where the blob will be copied to
441 * src [I] Source blob
445 * Failure: FALSE (GetLastError() == NTE_NO_MEMORY
448 * Use free_data_blob to release resources occupied by copy_data_blob.
450 static inline BOOL
copy_data_blob(PCRYPT_DATA_BLOB dst
, CONST PCRYPT_DATA_BLOB src
) {
451 dst
->pbData
= HeapAlloc(GetProcessHeap(), 0, src
->cbData
);
453 SetLastError(NTE_NO_MEMORY
);
456 dst
->cbData
= src
->cbData
;
457 memcpy(dst
->pbData
, src
->pbData
, src
->cbData
);
461 /******************************************************************************
462 * concat_data_blobs [Internal]
464 * Concatenates two blobs
467 * dst [O] The new blob will be copied here
468 * src1 [I] Prefix blob
469 * src2 [I] Appendix blob
473 * Failure: FALSE (GetLastError() == NTE_NO_MEMORY)
476 * Release resources occupied by concat_data_blobs with free_data_blobs
478 static inline BOOL
concat_data_blobs(PCRYPT_DATA_BLOB dst
, CONST PCRYPT_DATA_BLOB src1
,
479 CONST PCRYPT_DATA_BLOB src2
)
481 dst
->cbData
= src1
->cbData
+ src2
->cbData
;
482 dst
->pbData
= HeapAlloc(GetProcessHeap(), 0, dst
->cbData
);
484 SetLastError(NTE_NO_MEMORY
);
487 memcpy(dst
->pbData
, src1
->pbData
, src1
->cbData
);
488 memcpy(dst
->pbData
+ src1
->cbData
, src2
->pbData
, src2
->cbData
);
492 /******************************************************************************
493 * free_data_blob [Internal]
495 * releases resource occupied by a dynamically allocated CRYPT_DATA_BLOB
498 * pBlob [I] Heap space occupied by pBlob->pbData is released
500 static inline void free_data_blob(PCRYPT_DATA_BLOB pBlob
) {
501 HeapFree(GetProcessHeap(), 0, pBlob
->pbData
);
504 /******************************************************************************
505 * init_data_blob [Internal]
507 static inline void init_data_blob(PCRYPT_DATA_BLOB pBlob
) {
508 pBlob
->pbData
= NULL
;
512 /******************************************************************************
513 * free_hmac_info [Internal]
515 * Deeply free an HMAC_INFO struct.
518 * hmac_info [I] Pointer to the HMAC_INFO struct to be freed.
521 * See Internet RFC 2104 for details on the HMAC algorithm.
523 static inline void free_hmac_info(PHMAC_INFO hmac_info
) {
524 if (!hmac_info
) return;
525 HeapFree(GetProcessHeap(), 0, hmac_info
->pbInnerString
);
526 HeapFree(GetProcessHeap(), 0, hmac_info
->pbOuterString
);
527 HeapFree(GetProcessHeap(), 0, hmac_info
);
530 /******************************************************************************
531 * copy_hmac_info [Internal]
533 * Deeply copy an HMAC_INFO struct
536 * dst [O] Pointer to a location where the pointer to the HMAC_INFO copy will be stored.
537 * src [I] Pointer to the HMAC_INFO struct to be copied.
544 * See Internet RFC 2104 for details on the HMAC algorithm.
546 static BOOL
copy_hmac_info(PHMAC_INFO
*dst
, const HMAC_INFO
*src
) {
547 if (!src
) return FALSE
;
548 *dst
= HeapAlloc(GetProcessHeap(), 0, sizeof(HMAC_INFO
));
549 if (!*dst
) return FALSE
;
551 (*dst
)->pbInnerString
= NULL
;
552 (*dst
)->pbOuterString
= NULL
;
553 if ((*dst
)->cbInnerString
== 0) (*dst
)->cbInnerString
= RSAENH_HMAC_DEF_PAD_LEN
;
554 (*dst
)->pbInnerString
= HeapAlloc(GetProcessHeap(), 0, (*dst
)->cbInnerString
);
555 if (!(*dst
)->pbInnerString
) {
556 free_hmac_info(*dst
);
559 if (src
->cbInnerString
)
560 memcpy((*dst
)->pbInnerString
, src
->pbInnerString
, src
->cbInnerString
);
562 memset((*dst
)->pbInnerString
, RSAENH_HMAC_DEF_IPAD_CHAR
, RSAENH_HMAC_DEF_PAD_LEN
);
563 if ((*dst
)->cbOuterString
== 0) (*dst
)->cbOuterString
= RSAENH_HMAC_DEF_PAD_LEN
;
564 (*dst
)->pbOuterString
= HeapAlloc(GetProcessHeap(), 0, (*dst
)->cbOuterString
);
565 if (!(*dst
)->pbOuterString
) {
566 free_hmac_info(*dst
);
569 if (src
->cbOuterString
)
570 memcpy((*dst
)->pbOuterString
, src
->pbOuterString
, src
->cbOuterString
);
572 memset((*dst
)->pbOuterString
, RSAENH_HMAC_DEF_OPAD_CHAR
, RSAENH_HMAC_DEF_PAD_LEN
);
576 /******************************************************************************
577 * destroy_hash [Internal]
579 * Destructor for hash objects
582 * pCryptHash [I] Pointer to the hash object to be destroyed.
583 * Will be invalid after function returns!
585 static void destroy_hash(OBJECTHDR
*pObject
)
587 CRYPTHASH
*pCryptHash
= (CRYPTHASH
*)pObject
;
589 free_hmac_info(pCryptHash
->pHMACInfo
);
590 free_data_blob(&pCryptHash
->tpPRFParams
.blobLabel
);
591 free_data_blob(&pCryptHash
->tpPRFParams
.blobSeed
);
592 HeapFree(GetProcessHeap(), 0, pCryptHash
);
595 /******************************************************************************
596 * init_hash [Internal]
598 * Initialize (or reset) a hash object
601 * pCryptHash [I] The hash object to be initialized.
603 static inline BOOL
init_hash(CRYPTHASH
*pCryptHash
) {
606 switch (pCryptHash
->aiAlgid
)
609 if (pCryptHash
->pHMACInfo
) {
610 const PROV_ENUMALGS_EX
*pAlgInfo
;
612 pAlgInfo
= get_algid_info(pCryptHash
->hProv
, pCryptHash
->pHMACInfo
->HashAlgid
);
613 if (!pAlgInfo
) return FALSE
;
614 pCryptHash
->dwHashSize
= pAlgInfo
->dwDefaultLen
>> 3;
615 init_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
);
616 update_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
617 pCryptHash
->pHMACInfo
->pbInnerString
,
618 pCryptHash
->pHMACInfo
->cbInnerString
);
623 dwLen
= sizeof(DWORD
);
624 RSAENH_CPGetKeyParam(pCryptHash
->hProv
, pCryptHash
->hKey
, KP_BLOCKLEN
,
625 (BYTE
*)&pCryptHash
->dwHashSize
, &dwLen
, 0);
626 pCryptHash
->dwHashSize
>>= 3;
630 return init_hash_impl(pCryptHash
->aiAlgid
, &pCryptHash
->context
);
634 /******************************************************************************
635 * update_hash [Internal]
637 * Hashes the given data and updates the hash object's state accordingly
640 * pCryptHash [I] Hash object to be updated.
641 * pbData [I] Pointer to data stream to be hashed.
642 * dwDataLen [I] Length of data stream.
644 static inline void update_hash(CRYPTHASH
*pCryptHash
, CONST BYTE
*pbData
, DWORD dwDataLen
) {
647 switch (pCryptHash
->aiAlgid
)
650 if (pCryptHash
->pHMACInfo
)
651 update_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
656 pbTemp
= HeapAlloc(GetProcessHeap(), 0, dwDataLen
);
658 memcpy(pbTemp
, pbData
, dwDataLen
);
659 RSAENH_CPEncrypt(pCryptHash
->hProv
, pCryptHash
->hKey
, 0, FALSE
, 0,
660 pbTemp
, &dwDataLen
, dwDataLen
);
661 HeapFree(GetProcessHeap(), 0, pbTemp
);
665 update_hash_impl(pCryptHash
->aiAlgid
, &pCryptHash
->context
, pbData
, dwDataLen
);
669 /******************************************************************************
670 * finalize_hash [Internal]
672 * Finalizes the hash, after all data has been hashed with update_hash.
673 * No additional data can be hashed afterwards until the hash gets initialized again.
676 * pCryptHash [I] Hash object to be finalized.
678 static inline void finalize_hash(CRYPTHASH
*pCryptHash
) {
681 switch (pCryptHash
->aiAlgid
)
684 if (pCryptHash
->pHMACInfo
) {
685 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
];
687 finalize_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
688 pCryptHash
->abHashValue
);
689 memcpy(abHashValue
, pCryptHash
->abHashValue
, pCryptHash
->dwHashSize
);
690 init_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
);
691 update_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
692 pCryptHash
->pHMACInfo
->pbOuterString
,
693 pCryptHash
->pHMACInfo
->cbOuterString
);
694 update_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
695 abHashValue
, pCryptHash
->dwHashSize
);
696 finalize_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
697 pCryptHash
->abHashValue
);
703 RSAENH_CPEncrypt(pCryptHash
->hProv
, pCryptHash
->hKey
, 0, TRUE
, 0,
704 pCryptHash
->abHashValue
, &dwDataLen
, pCryptHash
->dwHashSize
);
708 finalize_hash_impl(pCryptHash
->aiAlgid
, &pCryptHash
->context
, pCryptHash
->abHashValue
);
712 /******************************************************************************
713 * destroy_key [Internal]
715 * Destructor for key objects
718 * pCryptKey [I] Pointer to the key object to be destroyed.
719 * Will be invalid after function returns!
721 static void destroy_key(OBJECTHDR
*pObject
)
723 CRYPTKEY
*pCryptKey
= (CRYPTKEY
*)pObject
;
725 free_key_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
);
726 free_data_blob(&pCryptKey
->siSChannelInfo
.blobClientRandom
);
727 free_data_blob(&pCryptKey
->siSChannelInfo
.blobServerRandom
);
728 HeapFree(GetProcessHeap(), 0, pCryptKey
);
731 /******************************************************************************
732 * setup_key [Internal]
734 * Initialize (or reset) a key object
737 * pCryptKey [I] The key object to be initialized.
739 static inline void setup_key(CRYPTKEY
*pCryptKey
) {
740 pCryptKey
->dwState
= RSAENH_KEYSTATE_IDLE
;
741 memcpy(pCryptKey
->abChainVector
, pCryptKey
->abInitVector
, sizeof(pCryptKey
->abChainVector
));
742 setup_key_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pCryptKey
->dwKeyLen
,
743 pCryptKey
->dwEffectiveKeyLen
, pCryptKey
->dwSaltLen
,
744 pCryptKey
->abKeyValue
);
747 /******************************************************************************
750 * Creates a new key object without assigning the actual binary key value.
751 * This is done by CPDeriveKey, CPGenKey or CPImportKey, which call this function.
754 * hProv [I] Handle to the provider to which the created key will belong.
755 * aiAlgid [I] The new key shall use the crypto algorithm idenfied by aiAlgid.
756 * dwFlags [I] Upper 16 bits give the key length.
757 * Lower 16 bits: CRYPT_EXPORTABLE, CRYPT_CREATE_SALT,
759 * ppCryptKey [O] Pointer to the created key
762 * Success: Handle to the created key.
763 * Failure: INVALID_HANDLE_VALUE
765 static HCRYPTKEY
new_key(HCRYPTPROV hProv
, ALG_ID aiAlgid
, DWORD dwFlags
, CRYPTKEY
**ppCryptKey
)
769 DWORD dwKeyLen
= HIWORD(dwFlags
);
770 const PROV_ENUMALGS_EX
*peaAlgidInfo
;
775 * Retrieve the CSP's capabilities for the given ALG_ID value
777 peaAlgidInfo
= get_algid_info(hProv
, aiAlgid
);
778 if (!peaAlgidInfo
) return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
781 * Assume the default key length, if none is specified explicitly
783 if (dwKeyLen
== 0) dwKeyLen
= peaAlgidInfo
->dwDefaultLen
;
786 * Check if the requested key length is supported by the current CSP.
787 * Adjust key length's for DES algorithms.
791 if (dwKeyLen
== RSAENH_DES_EFFECTIVE_KEYLEN
) {
792 dwKeyLen
= RSAENH_DES_STORAGE_KEYLEN
;
794 if (dwKeyLen
!= RSAENH_DES_STORAGE_KEYLEN
) {
795 SetLastError(NTE_BAD_FLAGS
);
796 return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
801 if (dwKeyLen
== RSAENH_3DES112_EFFECTIVE_KEYLEN
) {
802 dwKeyLen
= RSAENH_3DES112_STORAGE_KEYLEN
;
804 if (dwKeyLen
!= RSAENH_3DES112_STORAGE_KEYLEN
) {
805 SetLastError(NTE_BAD_FLAGS
);
806 return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
811 if (dwKeyLen
== RSAENH_3DES_EFFECTIVE_KEYLEN
) {
812 dwKeyLen
= RSAENH_3DES_STORAGE_KEYLEN
;
814 if (dwKeyLen
!= RSAENH_3DES_STORAGE_KEYLEN
) {
815 SetLastError(NTE_BAD_FLAGS
);
816 return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
822 dwKeyLen
> peaAlgidInfo
->dwMaxLen
||
823 dwKeyLen
< peaAlgidInfo
->dwMinLen
)
825 SetLastError(NTE_BAD_FLAGS
);
826 return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
830 hCryptKey
= new_object(&handle_table
, sizeof(CRYPTKEY
), RSAENH_MAGIC_KEY
,
831 destroy_key
, (OBJECTHDR
**)&pCryptKey
);
832 if (hCryptKey
!= (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
834 pCryptKey
->aiAlgid
= aiAlgid
;
835 pCryptKey
->hProv
= hProv
;
836 pCryptKey
->dwModeBits
= 0;
837 pCryptKey
->dwPermissions
= CRYPT_ENCRYPT
| CRYPT_DECRYPT
| CRYPT_READ
| CRYPT_WRITE
|
839 if (dwFlags
& CRYPT_EXPORTABLE
)
840 pCryptKey
->dwPermissions
|= CRYPT_EXPORT
;
841 pCryptKey
->dwKeyLen
= dwKeyLen
>> 3;
842 pCryptKey
->dwEffectiveKeyLen
= 0;
843 if ((dwFlags
& CRYPT_CREATE_SALT
) || (dwKeyLen
== 40 && !(dwFlags
& CRYPT_NO_SALT
)))
844 pCryptKey
->dwSaltLen
= 16 /*FIXME*/ - pCryptKey
->dwKeyLen
;
846 pCryptKey
->dwSaltLen
= 0;
847 memset(pCryptKey
->abKeyValue
, 0, sizeof(pCryptKey
->abKeyValue
));
848 memset(pCryptKey
->abInitVector
, 0, sizeof(pCryptKey
->abInitVector
));
849 init_data_blob(&pCryptKey
->siSChannelInfo
.blobClientRandom
);
850 init_data_blob(&pCryptKey
->siSChannelInfo
.blobServerRandom
);
854 case CALG_PCT1_MASTER
:
855 case CALG_SSL2_MASTER
:
856 case CALG_SSL3_MASTER
:
857 case CALG_TLS1_MASTER
:
859 pCryptKey
->dwBlockLen
= 0;
860 pCryptKey
->dwMode
= 0;
867 pCryptKey
->dwBlockLen
= 8;
868 pCryptKey
->dwMode
= CRYPT_MODE_CBC
;
875 pCryptKey
->dwBlockLen
= 16;
876 pCryptKey
->dwMode
= CRYPT_MODE_ECB
;
881 pCryptKey
->dwBlockLen
= dwKeyLen
>> 3;
882 pCryptKey
->dwMode
= 0;
886 *ppCryptKey
= pCryptKey
;
892 /******************************************************************************
893 * map_key_spec_to_key_pair_name [Internal]
895 * Returns the name of the registry value associated with a key spec.
898 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
901 * Success: Name of registry value.
904 static LPCSTR
map_key_spec_to_key_pair_name(DWORD dwKeySpec
)
911 szValueName
= "KeyExchangeKeyPair";
914 szValueName
= "SignatureKeyPair";
917 WARN("invalid key spec %d\n", dwKeySpec
);
923 /******************************************************************************
924 * store_key_pair [Internal]
926 * Stores a key pair to the registry
929 * hCryptKey [I] Handle to the key to be stored
930 * hKey [I] Registry key where the key pair is to be stored
931 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
932 * dwFlags [I] Flags for protecting the key
934 static void store_key_pair(HCRYPTKEY hCryptKey
, HKEY hKey
, DWORD dwKeySpec
, DWORD dwFlags
)
937 DATA_BLOB blobIn
, blobOut
;
942 if (!(szValueName
= map_key_spec_to_key_pair_name(dwKeySpec
)))
944 if (lookup_handle(&handle_table
, hCryptKey
, RSAENH_MAGIC_KEY
,
947 if (crypt_export_key(pKey
, 0, PRIVATEKEYBLOB
, 0, TRUE
, 0, &dwLen
))
949 pbKey
= HeapAlloc(GetProcessHeap(), 0, dwLen
);
952 if (crypt_export_key(pKey
, 0, PRIVATEKEYBLOB
, 0, TRUE
, pbKey
,
955 blobIn
.pbData
= pbKey
;
956 blobIn
.cbData
= dwLen
;
958 if (CryptProtectData(&blobIn
, NULL
, NULL
, NULL
, NULL
,
961 RegSetValueExA(hKey
, szValueName
, 0, REG_BINARY
,
962 blobOut
.pbData
, blobOut
.cbData
);
963 LocalFree(blobOut
.pbData
);
966 HeapFree(GetProcessHeap(), 0, pbKey
);
972 /******************************************************************************
973 * map_key_spec_to_permissions_name [Internal]
975 * Returns the name of the registry value associated with the permissions for
979 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
982 * Success: Name of registry value.
985 static LPCSTR
map_key_spec_to_permissions_name(DWORD dwKeySpec
)
992 szValueName
= "KeyExchangePermissions";
995 szValueName
= "SignaturePermissions";
998 WARN("invalid key spec %d\n", dwKeySpec
);
1004 /******************************************************************************
1005 * store_key_permissions [Internal]
1007 * Stores a key's permissions to the registry
1010 * hCryptKey [I] Handle to the key whose permissions are to be stored
1011 * hKey [I] Registry key where the key permissions are to be stored
1012 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
1014 static void store_key_permissions(HCRYPTKEY hCryptKey
, HKEY hKey
, DWORD dwKeySpec
)
1019 if (!(szValueName
= map_key_spec_to_permissions_name(dwKeySpec
)))
1021 if (lookup_handle(&handle_table
, hCryptKey
, RSAENH_MAGIC_KEY
,
1022 (OBJECTHDR
**)&pKey
))
1023 RegSetValueExA(hKey
, szValueName
, 0, REG_DWORD
,
1024 (BYTE
*)&pKey
->dwPermissions
,
1025 sizeof(pKey
->dwPermissions
));
1028 /******************************************************************************
1029 * create_container_key [Internal]
1031 * Creates the registry key for a key container's persistent storage.
1034 * pKeyContainer [I] Pointer to the key container
1035 * sam [I] Desired registry access
1036 * phKey [O] Returned key
1038 static BOOL
create_container_key(KEYCONTAINER
*pKeyContainer
, REGSAM sam
, HKEY
*phKey
)
1040 CHAR szRSABase
[MAX_PATH
];
1043 sprintf(szRSABase
, RSAENH_REGKEY
, pKeyContainer
->szName
);
1045 if (pKeyContainer
->dwFlags
& CRYPT_MACHINE_KEYSET
)
1046 hRootKey
= HKEY_LOCAL_MACHINE
;
1048 hRootKey
= HKEY_CURRENT_USER
;
1050 /* @@ Wine registry key: HKLM\Software\Wine\Crypto\RSA */
1051 /* @@ Wine registry key: HKCU\Software\Wine\Crypto\RSA */
1052 return RegCreateKeyExA(hRootKey
, szRSABase
, 0, NULL
,
1053 REG_OPTION_NON_VOLATILE
, sam
, NULL
, phKey
, NULL
)
1057 /******************************************************************************
1058 * open_container_key [Internal]
1060 * Opens a key container's persistent storage for reading.
1063 * pszContainerName [I] Name of the container to be opened. May be the empty
1064 * string if the parent key of all containers is to be
1066 * dwFlags [I] Flags indicating which keyset to be opened.
1067 * phKey [O] Returned key
1069 static BOOL
open_container_key(LPCSTR pszContainerName
, DWORD dwFlags
, HKEY
*phKey
)
1071 CHAR szRSABase
[MAX_PATH
];
1074 sprintf(szRSABase
, RSAENH_REGKEY
, pszContainerName
);
1076 if (dwFlags
& CRYPT_MACHINE_KEYSET
)
1077 hRootKey
= HKEY_LOCAL_MACHINE
;
1079 hRootKey
= HKEY_CURRENT_USER
;
1081 /* @@ Wine registry key: HKLM\Software\Wine\Crypto\RSA */
1082 /* @@ Wine registry key: HKCU\Software\Wine\Crypto\RSA */
1083 return RegOpenKeyExA(hRootKey
, szRSABase
, 0, KEY_READ
, phKey
) ==
1087 /******************************************************************************
1088 * delete_container_key [Internal]
1090 * Deletes a key container's persistent storage.
1093 * pszContainerName [I] Name of the container to be opened.
1094 * dwFlags [I] Flags indicating which keyset to be opened.
1096 static BOOL
delete_container_key(LPCSTR pszContainerName
, DWORD dwFlags
)
1098 CHAR szRegKey
[MAX_PATH
];
1100 if (snprintf(szRegKey
, MAX_PATH
, RSAENH_REGKEY
, pszContainerName
) >= MAX_PATH
) {
1101 SetLastError(NTE_BAD_KEYSET_PARAM
);
1105 if (dwFlags
& CRYPT_MACHINE_KEYSET
)
1106 hRootKey
= HKEY_LOCAL_MACHINE
;
1108 hRootKey
= HKEY_CURRENT_USER
;
1109 if (!RegDeleteKeyA(hRootKey
, szRegKey
)) {
1110 SetLastError(ERROR_SUCCESS
);
1113 SetLastError(NTE_BAD_KEYSET
);
1119 /******************************************************************************
1120 * store_key_container_keys [Internal]
1122 * Stores key container's keys in a persistent location.
1125 * pKeyContainer [I] Pointer to the key container whose keys are to be saved
1127 static void store_key_container_keys(KEYCONTAINER
*pKeyContainer
)
1132 /* On WinXP, persistent keys are stored in a file located at:
1133 * $AppData$\\Microsoft\\Crypto\\RSA\\$SID$\\some_hex_string
1136 if (pKeyContainer
->dwFlags
& CRYPT_MACHINE_KEYSET
)
1137 dwFlags
= CRYPTPROTECT_LOCAL_MACHINE
;
1141 if (create_container_key(pKeyContainer
, KEY_WRITE
, &hKey
))
1143 store_key_pair(pKeyContainer
->hKeyExchangeKeyPair
, hKey
,
1144 AT_KEYEXCHANGE
, dwFlags
);
1145 store_key_pair(pKeyContainer
->hSignatureKeyPair
, hKey
,
1146 AT_SIGNATURE
, dwFlags
);
1151 /******************************************************************************
1152 * store_key_container_permissions [Internal]
1154 * Stores key container's key permissions in a persistent location.
1157 * pKeyContainer [I] Pointer to the key container whose key permissions are to
1160 static void store_key_container_permissions(KEYCONTAINER
*pKeyContainer
)
1165 /* On WinXP, persistent keys are stored in a file located at:
1166 * $AppData$\\Microsoft\\Crypto\\RSA\\$SID$\\some_hex_string
1169 if (pKeyContainer
->dwFlags
& CRYPT_MACHINE_KEYSET
)
1170 dwFlags
= CRYPTPROTECT_LOCAL_MACHINE
;
1174 if (create_container_key(pKeyContainer
, KEY_WRITE
, &hKey
))
1176 store_key_permissions(pKeyContainer
->hKeyExchangeKeyPair
, hKey
,
1178 store_key_permissions(pKeyContainer
->hSignatureKeyPair
, hKey
,
1184 /******************************************************************************
1185 * release_key_container_keys [Internal]
1187 * Releases key container's keys.
1190 * pKeyContainer [I] Pointer to the key container whose keys are to be released.
1192 static void release_key_container_keys(KEYCONTAINER
*pKeyContainer
)
1194 release_handle(&handle_table
, pKeyContainer
->hKeyExchangeKeyPair
,
1196 release_handle(&handle_table
, pKeyContainer
->hSignatureKeyPair
,
1200 /******************************************************************************
1201 * destroy_key_container [Internal]
1203 * Destructor for key containers.
1206 * pObjectHdr [I] Pointer to the key container to be destroyed.
1208 static void destroy_key_container(OBJECTHDR
*pObjectHdr
)
1210 KEYCONTAINER
*pKeyContainer
= (KEYCONTAINER
*)pObjectHdr
;
1212 if (!(pKeyContainer
->dwFlags
& CRYPT_VERIFYCONTEXT
))
1214 store_key_container_keys(pKeyContainer
);
1215 store_key_container_permissions(pKeyContainer
);
1216 release_key_container_keys(pKeyContainer
);
1218 HeapFree( GetProcessHeap(), 0, pKeyContainer
);
1221 /******************************************************************************
1222 * new_key_container [Internal]
1224 * Create a new key container. The personality (RSA Base, Strong or Enhanced CP)
1225 * of the CSP is determined via the pVTable->pszProvName string.
1228 * pszContainerName [I] Name of the key container.
1229 * pVTable [I] Callback functions and context info provided by the OS
1232 * Success: Handle to the new key container.
1233 * Failure: INVALID_HANDLE_VALUE
1235 static HCRYPTPROV
new_key_container(PCCH pszContainerName
, DWORD dwFlags
, const VTableProvStruc
*pVTable
)
1237 KEYCONTAINER
*pKeyContainer
;
1238 HCRYPTPROV hKeyContainer
;
1240 hKeyContainer
= new_object(&handle_table
, sizeof(KEYCONTAINER
), RSAENH_MAGIC_CONTAINER
,
1241 destroy_key_container
, (OBJECTHDR
**)&pKeyContainer
);
1242 if (hKeyContainer
!= (HCRYPTPROV
)INVALID_HANDLE_VALUE
)
1244 lstrcpynA(pKeyContainer
->szName
, pszContainerName
, MAX_PATH
);
1245 pKeyContainer
->dwFlags
= dwFlags
;
1246 pKeyContainer
->dwEnumAlgsCtr
= 0;
1247 pKeyContainer
->hKeyExchangeKeyPair
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
1248 pKeyContainer
->hSignatureKeyPair
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
1249 if (pVTable
&& pVTable
->pszProvName
) {
1250 lstrcpynA(pKeyContainer
->szProvName
, pVTable
->pszProvName
, MAX_PATH
);
1251 if (!strcmp(pVTable
->pszProvName
, MS_DEF_PROV_A
)) {
1252 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_BASE
;
1253 } else if (!strcmp(pVTable
->pszProvName
, MS_ENHANCED_PROV_A
)) {
1254 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_ENHANCED
;
1255 } else if (!strcmp(pVTable
->pszProvName
, MS_DEF_RSA_SCHANNEL_PROV_A
)) {
1256 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_SCHANNEL
;
1257 } else if (!strcmp(pVTable
->pszProvName
, MS_ENH_RSA_AES_PROV_A
)) {
1258 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_AES
;
1260 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_STRONG
;
1264 /* The new key container has to be inserted into the CSP immediately
1265 * after creation to be available for CPGetProvParam's PP_ENUMCONTAINERS. */
1266 if (!(dwFlags
& CRYPT_VERIFYCONTEXT
)) {
1269 if (create_container_key(pKeyContainer
, KEY_WRITE
, &hKey
))
1274 return hKeyContainer
;
1277 /******************************************************************************
1278 * read_key_value [Internal]
1280 * Reads a key pair value from the registry
1283 * hKeyContainer [I] Crypt provider to use to import the key
1284 * hKey [I] Registry key from which to read the key pair
1285 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
1286 * dwFlags [I] Flags for unprotecting the key
1287 * phCryptKey [O] Returned key
1289 static BOOL
read_key_value(HCRYPTPROV hKeyContainer
, HKEY hKey
, DWORD dwKeySpec
, DWORD dwFlags
, HCRYPTKEY
*phCryptKey
)
1292 DWORD dwValueType
, dwLen
;
1294 DATA_BLOB blobIn
, blobOut
;
1297 if (!(szValueName
= map_key_spec_to_key_pair_name(dwKeySpec
)))
1299 if (RegQueryValueExA(hKey
, szValueName
, 0, &dwValueType
, NULL
, &dwLen
) ==
1302 pbKey
= HeapAlloc(GetProcessHeap(), 0, dwLen
);
1305 if (RegQueryValueExA(hKey
, szValueName
, 0, &dwValueType
, pbKey
, &dwLen
) ==
1308 blobIn
.pbData
= pbKey
;
1309 blobIn
.cbData
= dwLen
;
1311 if (CryptUnprotectData(&blobIn
, NULL
, NULL
, NULL
, NULL
,
1314 ret
= import_key(hKeyContainer
, blobOut
.pbData
, blobOut
.cbData
, 0, 0,
1316 LocalFree(blobOut
.pbData
);
1319 HeapFree(GetProcessHeap(), 0, pbKey
);
1326 if (lookup_handle(&handle_table
, *phCryptKey
, RSAENH_MAGIC_KEY
,
1327 (OBJECTHDR
**)&pKey
))
1329 if ((szValueName
= map_key_spec_to_permissions_name(dwKeySpec
)))
1331 dwLen
= sizeof(pKey
->dwPermissions
);
1332 RegQueryValueExA(hKey
, szValueName
, 0, NULL
,
1333 (BYTE
*)&pKey
->dwPermissions
, &dwLen
);
1340 /******************************************************************************
1341 * read_key_container [Internal]
1343 * Tries to read the persistent state of the key container (mainly the signature
1344 * and key exchange private keys) given by pszContainerName.
1347 * pszContainerName [I] Name of the key container to read from the registry
1348 * pVTable [I] Pointer to context data provided by the operating system
1351 * Success: Handle to the key container read from the registry
1352 * Failure: INVALID_HANDLE_VALUE
1354 static HCRYPTPROV
read_key_container(PCHAR pszContainerName
, DWORD dwFlags
, const VTableProvStruc
*pVTable
)
1357 KEYCONTAINER
*pKeyContainer
;
1358 HCRYPTPROV hKeyContainer
;
1359 HCRYPTKEY hCryptKey
;
1361 if (!open_container_key(pszContainerName
, dwFlags
, &hKey
))
1363 SetLastError(NTE_BAD_KEYSET
);
1364 return (HCRYPTPROV
)INVALID_HANDLE_VALUE
;
1367 hKeyContainer
= new_key_container(pszContainerName
, dwFlags
, pVTable
);
1368 if (hKeyContainer
!= (HCRYPTPROV
)INVALID_HANDLE_VALUE
)
1370 DWORD dwProtectFlags
= (dwFlags
& CRYPT_MACHINE_KEYSET
) ?
1371 CRYPTPROTECT_LOCAL_MACHINE
: 0;
1373 if (!lookup_handle(&handle_table
, hKeyContainer
, RSAENH_MAGIC_CONTAINER
,
1374 (OBJECTHDR
**)&pKeyContainer
))
1375 return (HCRYPTPROV
)INVALID_HANDLE_VALUE
;
1377 if (read_key_value(hKeyContainer
, hKey
, AT_KEYEXCHANGE
,
1378 dwProtectFlags
, &hCryptKey
))
1379 pKeyContainer
->hKeyExchangeKeyPair
= hCryptKey
;
1380 if (read_key_value(hKeyContainer
, hKey
, AT_SIGNATURE
,
1381 dwProtectFlags
, &hCryptKey
))
1382 pKeyContainer
->hSignatureKeyPair
= hCryptKey
;
1385 return hKeyContainer
;
1388 /******************************************************************************
1389 * build_hash_signature [Internal]
1391 * Builds a padded version of a hash to match the length of the RSA key modulus.
1394 * pbSignature [O] The padded hash object is stored here.
1395 * dwLen [I] Length of the pbSignature buffer.
1396 * aiAlgid [I] Algorithm identifier of the hash to be padded.
1397 * abHashValue [I] The value of the hash object.
1398 * dwHashLen [I] Length of the hash value.
1399 * dwFlags [I] Selection of padding algorithm.
1403 * Failure: FALSE (NTE_BAD_ALGID)
1405 static BOOL
build_hash_signature(BYTE
*pbSignature
, DWORD dwLen
, ALG_ID aiAlgid
,
1406 CONST BYTE
*abHashValue
, DWORD dwHashLen
, DWORD dwFlags
)
1408 /* These prefixes are meant to be concatenated with hash values of the
1409 * respective kind to form a PKCS #7 DigestInfo. */
1410 static const struct tagOIDDescriptor
{
1413 CONST BYTE abOID
[18];
1414 } aOIDDescriptor
[5] = {
1415 { CALG_MD2
, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1416 0x86, 0xf7, 0x0d, 0x02, 0x02, 0x05, 0x00, 0x04, 0x10 } },
1417 { CALG_MD4
, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1418 0x86, 0xf7, 0x0d, 0x02, 0x04, 0x05, 0x00, 0x04, 0x10 } },
1419 { CALG_MD5
, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1420 0x86, 0xf7, 0x0d, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10 } },
1421 { CALG_SHA
, 15, { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03,
1422 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14 } },
1425 DWORD dwIdxOID
, i
, j
;
1427 for (dwIdxOID
= 0; aOIDDescriptor
[dwIdxOID
].aiAlgid
; dwIdxOID
++) {
1428 if (aOIDDescriptor
[dwIdxOID
].aiAlgid
== aiAlgid
) break;
1431 if (!aOIDDescriptor
[dwIdxOID
].aiAlgid
) {
1432 SetLastError(NTE_BAD_ALGID
);
1436 /* Build the padded signature */
1437 if (dwFlags
& CRYPT_X931_FORMAT
) {
1438 pbSignature
[0] = 0x6b;
1439 for (i
=1; i
< dwLen
- dwHashLen
- 3; i
++) {
1440 pbSignature
[i
] = 0xbb;
1442 pbSignature
[i
++] = 0xba;
1443 for (j
=0; j
< dwHashLen
; j
++, i
++) {
1444 pbSignature
[i
] = abHashValue
[j
];
1446 pbSignature
[i
++] = 0x33;
1447 pbSignature
[i
++] = 0xcc;
1449 pbSignature
[0] = 0x00;
1450 pbSignature
[1] = 0x01;
1451 if (dwFlags
& CRYPT_NOHASHOID
) {
1452 for (i
=2; i
< dwLen
- 1 - dwHashLen
; i
++) {
1453 pbSignature
[i
] = 0xff;
1455 pbSignature
[i
++] = 0x00;
1457 for (i
=2; i
< dwLen
- 1 - aOIDDescriptor
[dwIdxOID
].dwLen
- dwHashLen
; i
++) {
1458 pbSignature
[i
] = 0xff;
1460 pbSignature
[i
++] = 0x00;
1461 for (j
=0; j
< aOIDDescriptor
[dwIdxOID
].dwLen
; j
++) {
1462 pbSignature
[i
++] = aOIDDescriptor
[dwIdxOID
].abOID
[j
];
1465 for (j
=0; j
< dwHashLen
; j
++) {
1466 pbSignature
[i
++] = abHashValue
[j
];
1473 /******************************************************************************
1476 * This is an implementation of the 'P_hash' helper function for TLS1's PRF.
1477 * It is used exclusively by tls1_prf. For details see RFC 2246, chapter 5.
1478 * The pseudo random stream generated by this function is exclusive or'ed with
1479 * the data in pbBuffer.
1482 * hHMAC [I] HMAC object, which will be used in pseudo random generation
1483 * pblobSeed [I] Seed value
1484 * pbBuffer [I/O] Pseudo random stream will be xor'ed to the provided data
1485 * dwBufferLen [I] Number of pseudo random bytes desired
1491 static BOOL
tls1_p(HCRYPTHASH hHMAC
, CONST PCRYPT_DATA_BLOB pblobSeed
, PBYTE pbBuffer
, DWORD dwBufferLen
)
1494 BYTE abAi
[RSAENH_MAX_HASH_SIZE
];
1497 if (!lookup_handle(&handle_table
, hHMAC
, RSAENH_MAGIC_HASH
, (OBJECTHDR
**)&pHMAC
)) {
1498 SetLastError(NTE_BAD_HASH
);
1502 /* compute A_1 = HMAC(seed) */
1504 update_hash(pHMAC
, pblobSeed
->pbData
, pblobSeed
->cbData
);
1505 finalize_hash(pHMAC
);
1506 memcpy(abAi
, pHMAC
->abHashValue
, pHMAC
->dwHashSize
);
1509 /* compute HMAC(A_i + seed) */
1511 update_hash(pHMAC
, abAi
, pHMAC
->dwHashSize
);
1512 update_hash(pHMAC
, pblobSeed
->pbData
, pblobSeed
->cbData
);
1513 finalize_hash(pHMAC
);
1515 /* pseudo random stream := CONCAT_{i=1..n} ( HMAC(A_i + seed) ) */
1517 if (i
>= dwBufferLen
) break;
1518 pbBuffer
[i
] ^= pHMAC
->abHashValue
[i
% pHMAC
->dwHashSize
];
1520 } while (i
% pHMAC
->dwHashSize
);
1522 /* compute A_{i+1} = HMAC(A_i) */
1524 update_hash(pHMAC
, abAi
, pHMAC
->dwHashSize
);
1525 finalize_hash(pHMAC
);
1526 memcpy(abAi
, pHMAC
->abHashValue
, pHMAC
->dwHashSize
);
1527 } while (i
< dwBufferLen
);
1532 /******************************************************************************
1533 * tls1_prf [Internal]
1535 * TLS1 pseudo random function as specified in RFC 2246, chapter 5
1538 * hProv [I] Key container used to compute the pseudo random stream
1539 * hSecret [I] Key that holds the (pre-)master secret
1540 * pblobLabel [I] Descriptive label
1541 * pblobSeed [I] Seed value
1542 * pbBuffer [O] Pseudo random numbers will be stored here
1543 * dwBufferLen [I] Number of pseudo random bytes desired
1549 static BOOL
tls1_prf(HCRYPTPROV hProv
, HCRYPTPROV hSecret
, CONST PCRYPT_DATA_BLOB pblobLabel
,
1550 CONST PCRYPT_DATA_BLOB pblobSeed
, PBYTE pbBuffer
, DWORD dwBufferLen
)
1552 HMAC_INFO hmacInfo
= { 0, NULL
, 0, NULL
, 0 };
1553 HCRYPTHASH hHMAC
= (HCRYPTHASH
)INVALID_HANDLE_VALUE
;
1554 HCRYPTKEY hHalfSecret
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
1555 CRYPTKEY
*pHalfSecret
, *pSecret
;
1556 DWORD dwHalfSecretLen
;
1557 BOOL result
= FALSE
;
1558 CRYPT_DATA_BLOB blobLabelSeed
;
1560 TRACE("(hProv=%08lx, hSecret=%08lx, pblobLabel=%p, pblobSeed=%p, pbBuffer=%p, dwBufferLen=%d)\n",
1561 hProv
, hSecret
, pblobLabel
, pblobSeed
, pbBuffer
, dwBufferLen
);
1563 if (!lookup_handle(&handle_table
, hSecret
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pSecret
)) {
1564 SetLastError(NTE_FAIL
);
1568 dwHalfSecretLen
= (pSecret
->dwKeyLen
+1)/2;
1570 /* concatenation of the label and the seed */
1571 if (!concat_data_blobs(&blobLabelSeed
, pblobLabel
, pblobSeed
)) goto exit
;
1573 /* zero out the buffer, since two random streams will be xor'ed into it. */
1574 memset(pbBuffer
, 0, dwBufferLen
);
1576 /* build a 'fake' key, to hold the secret. CALG_SSL2_MASTER is used since it provides
1577 * the biggest range of valid key lengths. */
1578 hHalfSecret
= new_key(hProv
, CALG_SSL2_MASTER
, MAKELONG(0,dwHalfSecretLen
*8), &pHalfSecret
);
1579 if (hHalfSecret
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) goto exit
;
1581 /* Derive an HMAC_MD5 hash and call the helper function. */
1582 memcpy(pHalfSecret
->abKeyValue
, pSecret
->abKeyValue
, dwHalfSecretLen
);
1583 if (!RSAENH_CPCreateHash(hProv
, CALG_HMAC
, hHalfSecret
, 0, &hHMAC
)) goto exit
;
1584 hmacInfo
.HashAlgid
= CALG_MD5
;
1585 if (!RSAENH_CPSetHashParam(hProv
, hHMAC
, HP_HMAC_INFO
, (BYTE
*)&hmacInfo
, 0)) goto exit
;
1586 if (!tls1_p(hHMAC
, &blobLabelSeed
, pbBuffer
, dwBufferLen
)) goto exit
;
1588 /* Reconfigure to HMAC_SHA hash and call helper function again. */
1589 memcpy(pHalfSecret
->abKeyValue
, pSecret
->abKeyValue
+ (pSecret
->dwKeyLen
/2), dwHalfSecretLen
);
1590 hmacInfo
.HashAlgid
= CALG_SHA
;
1591 if (!RSAENH_CPSetHashParam(hProv
, hHMAC
, HP_HMAC_INFO
, (BYTE
*)&hmacInfo
, 0)) goto exit
;
1592 if (!tls1_p(hHMAC
, &blobLabelSeed
, pbBuffer
, dwBufferLen
)) goto exit
;
1596 release_handle(&handle_table
, hHalfSecret
, RSAENH_MAGIC_KEY
);
1597 if (hHMAC
!= (HCRYPTHASH
)INVALID_HANDLE_VALUE
) RSAENH_CPDestroyHash(hProv
, hHMAC
);
1598 free_data_blob(&blobLabelSeed
);
1602 /******************************************************************************
1603 * pad_data [Internal]
1605 * Helper function for data padding according to PKCS1 #2
1608 * abData [I] The data to be padded
1609 * dwDataLen [I] Length of the data
1610 * abBuffer [O] Padded data will be stored here
1611 * dwBufferLen [I] Length of the buffer (also length of padded data)
1612 * dwFlags [I] Padding format (CRYPT_SSL2_FALLBACK)
1616 * Failure: FALSE (NTE_BAD_LEN, too much data to pad)
1618 static BOOL
pad_data(CONST BYTE
*abData
, DWORD dwDataLen
, BYTE
*abBuffer
, DWORD dwBufferLen
,
1623 /* Ensure there is enough space for PKCS1 #2 padding */
1624 if (dwDataLen
> dwBufferLen
-11) {
1625 SetLastError(NTE_BAD_LEN
);
1629 memmove(abBuffer
+ dwBufferLen
- dwDataLen
, abData
, dwDataLen
);
1632 abBuffer
[1] = RSAENH_PKC_BLOCKTYPE
;
1633 for (i
=2; i
< dwBufferLen
- dwDataLen
- 1; i
++)
1634 do gen_rand_impl(&abBuffer
[i
], 1); while (!abBuffer
[i
]);
1635 if (dwFlags
& CRYPT_SSL2_FALLBACK
)
1636 for (i
-=8; i
< dwBufferLen
- dwDataLen
- 1; i
++)
1643 /******************************************************************************
1644 * unpad_data [Internal]
1646 * Remove the PKCS1 padding from RSA decrypted data
1649 * abData [I] The padded data
1650 * dwDataLen [I] Length of the padded data
1651 * abBuffer [O] Data without padding will be stored here
1652 * dwBufferLen [I/O] I: Length of the buffer, O: Length of unpadded data
1653 * dwFlags [I] Currently none defined
1657 * Failure: FALSE, (NTE_BAD_DATA, no valid PKCS1 padding or buffer too small)
1659 static BOOL
unpad_data(CONST BYTE
*abData
, DWORD dwDataLen
, BYTE
*abBuffer
, DWORD
*dwBufferLen
,
1664 for (i
=2; i
<dwDataLen
; i
++)
1668 if ((i
== dwDataLen
) || (*dwBufferLen
< dwDataLen
- i
- 1) ||
1669 (abData
[0] != 0x00) || (abData
[1] != RSAENH_PKC_BLOCKTYPE
))
1671 SetLastError(NTE_BAD_DATA
);
1675 *dwBufferLen
= dwDataLen
- i
- 1;
1676 memmove(abBuffer
, abData
+ i
+ 1, *dwBufferLen
);
1680 /******************************************************************************
1681 * CPAcquireContext (RSAENH.@)
1683 * Acquire a handle to the key container specified by pszContainer
1686 * phProv [O] Pointer to the location the acquired handle will be written to.
1687 * pszContainer [I] Name of the desired key container. See Notes
1688 * dwFlags [I] Flags. See Notes.
1689 * pVTable [I] Pointer to a PVTableProvStruct containing callbacks.
1696 * If pszContainer is NULL or points to a zero length string the user's login
1697 * name will be used as the key container name.
1699 * If the CRYPT_NEW_KEYSET flag is set in dwFlags a new keyset will be created.
1700 * If a keyset with the given name already exists, the function fails and sets
1701 * last error to NTE_EXISTS. If CRYPT_NEW_KEYSET is not set and the specified
1702 * key container does not exist, function fails and sets last error to
1705 BOOL WINAPI
RSAENH_CPAcquireContext(HCRYPTPROV
*phProv
, LPSTR pszContainer
,
1706 DWORD dwFlags
, PVTableProvStruc pVTable
)
1708 CHAR szKeyContainerName
[MAX_PATH
];
1710 TRACE("(phProv=%p, pszContainer=%s, dwFlags=%08x, pVTable=%p)\n", phProv
,
1711 debugstr_a(pszContainer
), dwFlags
, pVTable
);
1713 if (pszContainer
&& *pszContainer
)
1715 lstrcpynA(szKeyContainerName
, pszContainer
, MAX_PATH
);
1719 DWORD dwLen
= sizeof(szKeyContainerName
);
1720 if (!GetUserNameA(szKeyContainerName
, &dwLen
)) return FALSE
;
1723 switch (dwFlags
& (CRYPT_NEWKEYSET
|CRYPT_VERIFYCONTEXT
|CRYPT_DELETEKEYSET
))
1726 *phProv
= read_key_container(szKeyContainerName
, dwFlags
, pVTable
);
1729 case CRYPT_DELETEKEYSET
:
1730 return delete_container_key(szKeyContainerName
, dwFlags
);
1732 case CRYPT_NEWKEYSET
:
1733 *phProv
= read_key_container(szKeyContainerName
, dwFlags
, pVTable
);
1734 if (*phProv
!= (HCRYPTPROV
)INVALID_HANDLE_VALUE
)
1736 release_handle(&handle_table
, *phProv
, RSAENH_MAGIC_CONTAINER
);
1737 TRACE("Can't create new keyset, already exists\n");
1738 SetLastError(NTE_EXISTS
);
1741 *phProv
= new_key_container(szKeyContainerName
, dwFlags
, pVTable
);
1744 case CRYPT_VERIFYCONTEXT
|CRYPT_NEWKEYSET
:
1745 case CRYPT_VERIFYCONTEXT
:
1746 if (pszContainer
&& *pszContainer
) {
1747 TRACE("pszContainer should be empty\n");
1748 SetLastError(NTE_BAD_FLAGS
);
1751 *phProv
= new_key_container("", dwFlags
, pVTable
);
1755 *phProv
= (HCRYPTPROV
)INVALID_HANDLE_VALUE
;
1756 SetLastError(NTE_BAD_FLAGS
);
1760 if (*phProv
!= (HCRYPTPROV
)INVALID_HANDLE_VALUE
) {
1761 SetLastError(ERROR_SUCCESS
);
1768 /******************************************************************************
1769 * CPCreateHash (RSAENH.@)
1771 * CPCreateHash creates and initalizes a new hash object.
1774 * hProv [I] Handle to the key container to which the new hash will belong.
1775 * Algid [I] Identifies the hash algorithm, which will be used for the hash.
1776 * hKey [I] Handle to a session key applied for keyed hashes.
1777 * dwFlags [I] Currently no flags defined. Must be zero.
1778 * phHash [O] Points to the location where a handle to the new hash will be stored.
1785 * hKey is a handle to a session key applied in keyed hashes like MAC and HMAC.
1786 * If a normal hash object is to be created (like e.g. MD2 or SHA1) hKey must be zero.
1788 BOOL WINAPI
RSAENH_CPCreateHash(HCRYPTPROV hProv
, ALG_ID Algid
, HCRYPTKEY hKey
, DWORD dwFlags
,
1791 CRYPTKEY
*pCryptKey
;
1792 CRYPTHASH
*pCryptHash
;
1793 const PROV_ENUMALGS_EX
*peaAlgidInfo
;
1795 TRACE("(hProv=%08lx, Algid=%08x, hKey=%08lx, dwFlags=%08x, phHash=%p)\n", hProv
, Algid
, hKey
,
1798 peaAlgidInfo
= get_algid_info(hProv
, Algid
);
1799 if (!peaAlgidInfo
) return FALSE
;
1803 SetLastError(NTE_BAD_FLAGS
);
1807 if (Algid
== CALG_MAC
|| Algid
== CALG_HMAC
|| Algid
== CALG_SCHANNEL_MASTER_HASH
||
1808 Algid
== CALG_TLS1PRF
)
1810 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
)) {
1811 SetLastError(NTE_BAD_KEY
);
1815 if ((Algid
== CALG_MAC
) && (GET_ALG_TYPE(pCryptKey
->aiAlgid
) != ALG_TYPE_BLOCK
)) {
1816 SetLastError(NTE_BAD_KEY
);
1820 if ((Algid
== CALG_SCHANNEL_MASTER_HASH
|| Algid
== CALG_TLS1PRF
) &&
1821 (pCryptKey
->aiAlgid
!= CALG_TLS1_MASTER
))
1823 SetLastError(NTE_BAD_KEY
);
1827 if ((Algid
== CALG_TLS1PRF
) && (pCryptKey
->dwState
!= RSAENH_KEYSTATE_MASTERKEY
)) {
1828 SetLastError(NTE_BAD_KEY_STATE
);
1833 *phHash
= new_object(&handle_table
, sizeof(CRYPTHASH
), RSAENH_MAGIC_HASH
,
1834 destroy_hash
, (OBJECTHDR
**)&pCryptHash
);
1835 if (!pCryptHash
) return FALSE
;
1837 pCryptHash
->aiAlgid
= Algid
;
1838 pCryptHash
->hKey
= hKey
;
1839 pCryptHash
->hProv
= hProv
;
1840 pCryptHash
->dwState
= RSAENH_HASHSTATE_HASHING
;
1841 pCryptHash
->pHMACInfo
= NULL
;
1842 pCryptHash
->dwHashSize
= peaAlgidInfo
->dwDefaultLen
>> 3;
1843 init_data_blob(&pCryptHash
->tpPRFParams
.blobLabel
);
1844 init_data_blob(&pCryptHash
->tpPRFParams
.blobSeed
);
1846 if (Algid
== CALG_SCHANNEL_MASTER_HASH
) {
1847 static const char keyex
[] = "key expansion";
1848 BYTE key_expansion
[sizeof keyex
];
1849 CRYPT_DATA_BLOB blobRandom
, blobKeyExpansion
= { 13, key_expansion
};
1851 memcpy( key_expansion
, keyex
, sizeof keyex
);
1853 if (pCryptKey
->dwState
!= RSAENH_KEYSTATE_MASTERKEY
) {
1854 static const char msec
[] = "master secret";
1855 BYTE master_secret
[sizeof msec
];
1856 CRYPT_DATA_BLOB blobLabel
= { 13, master_secret
};
1857 BYTE abKeyValue
[48];
1859 memcpy( master_secret
, msec
, sizeof msec
);
1861 /* See RFC 2246, chapter 8.1 */
1862 if (!concat_data_blobs(&blobRandom
,
1863 &pCryptKey
->siSChannelInfo
.blobClientRandom
,
1864 &pCryptKey
->siSChannelInfo
.blobServerRandom
))
1868 tls1_prf(hProv
, hKey
, &blobLabel
, &blobRandom
, abKeyValue
, 48);
1869 pCryptKey
->dwState
= RSAENH_KEYSTATE_MASTERKEY
;
1870 memcpy(pCryptKey
->abKeyValue
, abKeyValue
, 48);
1871 free_data_blob(&blobRandom
);
1874 /* See RFC 2246, chapter 6.3 */
1875 if (!concat_data_blobs(&blobRandom
,
1876 &pCryptKey
->siSChannelInfo
.blobServerRandom
,
1877 &pCryptKey
->siSChannelInfo
.blobClientRandom
))
1881 tls1_prf(hProv
, hKey
, &blobKeyExpansion
, &blobRandom
, pCryptHash
->abHashValue
,
1882 RSAENH_MAX_HASH_SIZE
);
1883 free_data_blob(&blobRandom
);
1886 return init_hash(pCryptHash
);
1889 /******************************************************************************
1890 * CPDestroyHash (RSAENH.@)
1892 * Releases the handle to a hash object. The object is destroyed if it's reference
1893 * count reaches zero.
1896 * hProv [I] Handle to the key container to which the hash object belongs.
1897 * hHash [I] Handle to the hash object to be released.
1903 BOOL WINAPI
RSAENH_CPDestroyHash(HCRYPTPROV hProv
, HCRYPTHASH hHash
)
1905 TRACE("(hProv=%08lx, hHash=%08lx)\n", hProv
, hHash
);
1907 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
1909 SetLastError(NTE_BAD_UID
);
1913 if (!release_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
))
1915 SetLastError(NTE_BAD_HASH
);
1922 /******************************************************************************
1923 * CPDestroyKey (RSAENH.@)
1925 * Releases the handle to a key object. The object is destroyed if it's reference
1926 * count reaches zero.
1929 * hProv [I] Handle to the key container to which the key object belongs.
1930 * hKey [I] Handle to the key object to be released.
1936 BOOL WINAPI
RSAENH_CPDestroyKey(HCRYPTPROV hProv
, HCRYPTKEY hKey
)
1938 TRACE("(hProv=%08lx, hKey=%08lx)\n", hProv
, hKey
);
1940 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
1942 SetLastError(NTE_BAD_UID
);
1946 if (!release_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
))
1948 SetLastError(NTE_BAD_KEY
);
1955 /******************************************************************************
1956 * CPDuplicateHash (RSAENH.@)
1958 * Clones a hash object including it's current state.
1961 * hUID [I] Handle to the key container the hash belongs to.
1962 * hHash [I] Handle to the hash object to be cloned.
1963 * pdwReserved [I] Reserved. Must be NULL.
1964 * dwFlags [I] No flags are currently defined. Must be 0.
1965 * phHash [O] Handle to the cloned hash object.
1971 BOOL WINAPI
RSAENH_CPDuplicateHash(HCRYPTPROV hUID
, HCRYPTHASH hHash
, DWORD
*pdwReserved
,
1972 DWORD dwFlags
, HCRYPTHASH
*phHash
)
1974 CRYPTHASH
*pSrcHash
, *pDestHash
;
1976 TRACE("(hUID=%08lx, hHash=%08lx, pdwReserved=%p, dwFlags=%08x, phHash=%p)\n", hUID
, hHash
,
1977 pdwReserved
, dwFlags
, phHash
);
1979 if (!is_valid_handle(&handle_table
, hUID
, RSAENH_MAGIC_CONTAINER
))
1981 SetLastError(NTE_BAD_UID
);
1985 if (!lookup_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
, (OBJECTHDR
**)&pSrcHash
))
1987 SetLastError(NTE_BAD_HASH
);
1991 if (!phHash
|| pdwReserved
|| dwFlags
)
1993 SetLastError(ERROR_INVALID_PARAMETER
);
1997 *phHash
= new_object(&handle_table
, sizeof(CRYPTHASH
), RSAENH_MAGIC_HASH
,
1998 destroy_hash
, (OBJECTHDR
**)&pDestHash
);
1999 if (*phHash
!= (HCRYPTHASH
)INVALID_HANDLE_VALUE
)
2001 *pDestHash
= *pSrcHash
;
2002 duplicate_hash_impl(pSrcHash
->aiAlgid
, &pSrcHash
->context
, &pDestHash
->context
);
2003 copy_hmac_info(&pDestHash
->pHMACInfo
, pSrcHash
->pHMACInfo
);
2004 copy_data_blob(&pDestHash
->tpPRFParams
.blobLabel
, &pSrcHash
->tpPRFParams
.blobLabel
);
2005 copy_data_blob(&pDestHash
->tpPRFParams
.blobSeed
, &pSrcHash
->tpPRFParams
.blobSeed
);
2008 return *phHash
!= (HCRYPTHASH
)INVALID_HANDLE_VALUE
;
2011 /******************************************************************************
2012 * CPDuplicateKey (RSAENH.@)
2014 * Clones a key object including it's current state.
2017 * hUID [I] Handle to the key container the hash belongs to.
2018 * hKey [I] Handle to the key object to be cloned.
2019 * pdwReserved [I] Reserved. Must be NULL.
2020 * dwFlags [I] No flags are currently defined. Must be 0.
2021 * phHash [O] Handle to the cloned key object.
2027 BOOL WINAPI
RSAENH_CPDuplicateKey(HCRYPTPROV hUID
, HCRYPTKEY hKey
, DWORD
*pdwReserved
,
2028 DWORD dwFlags
, HCRYPTKEY
*phKey
)
2030 CRYPTKEY
*pSrcKey
, *pDestKey
;
2032 TRACE("(hUID=%08lx, hKey=%08lx, pdwReserved=%p, dwFlags=%08x, phKey=%p)\n", hUID
, hKey
,
2033 pdwReserved
, dwFlags
, phKey
);
2035 if (!is_valid_handle(&handle_table
, hUID
, RSAENH_MAGIC_CONTAINER
))
2037 SetLastError(NTE_BAD_UID
);
2041 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pSrcKey
))
2043 SetLastError(NTE_BAD_KEY
);
2047 if (!phKey
|| pdwReserved
|| dwFlags
)
2049 SetLastError(ERROR_INVALID_PARAMETER
);
2053 *phKey
= new_object(&handle_table
, sizeof(CRYPTKEY
), RSAENH_MAGIC_KEY
, destroy_key
,
2054 (OBJECTHDR
**)&pDestKey
);
2055 if (*phKey
!= (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
2057 *pDestKey
= *pSrcKey
;
2058 copy_data_blob(&pDestKey
->siSChannelInfo
.blobServerRandom
,
2059 &pSrcKey
->siSChannelInfo
.blobServerRandom
);
2060 copy_data_blob(&pDestKey
->siSChannelInfo
.blobClientRandom
,
2061 &pSrcKey
->siSChannelInfo
.blobClientRandom
);
2062 duplicate_key_impl(pSrcKey
->aiAlgid
, &pSrcKey
->context
, &pDestKey
->context
);
2071 /******************************************************************************
2072 * CPEncrypt (RSAENH.@)
2077 * hProv [I] The key container hKey and hHash belong to.
2078 * hKey [I] The key used to encrypt the data.
2079 * hHash [I] An optional hash object for parallel hashing. See notes.
2080 * Final [I] Indicates if this is the last block of data to encrypt.
2081 * dwFlags [I] Currently no flags defined. Must be zero.
2082 * pbData [I/O] Pointer to the data to encrypt. Encrypted data will also be stored there.
2083 * pdwDataLen [I/O] I: Length of data to encrypt, O: Length of encrypted data.
2084 * dwBufLen [I] Size of the buffer at pbData.
2091 * If a hash object handle is provided in hHash, it will be updated with the plaintext.
2092 * This is useful for message signatures.
2094 * This function uses the standard WINAPI protocol for querying data of dynamic length.
2096 BOOL WINAPI
RSAENH_CPEncrypt(HCRYPTPROV hProv
, HCRYPTKEY hKey
, HCRYPTHASH hHash
, BOOL Final
,
2097 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
, DWORD dwBufLen
)
2099 CRYPTKEY
*pCryptKey
;
2100 BYTE
*in
, out
[RSAENH_MAX_BLOCK_SIZE
], o
[RSAENH_MAX_BLOCK_SIZE
];
2101 DWORD dwEncryptedLen
, i
, j
, k
;
2103 TRACE("(hProv=%08lx, hKey=%08lx, hHash=%08lx, Final=%d, dwFlags=%08x, pbData=%p, "
2104 "pdwDataLen=%p, dwBufLen=%d)\n", hProv
, hKey
, hHash
, Final
, dwFlags
, pbData
, pdwDataLen
,
2107 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
2109 SetLastError(NTE_BAD_UID
);
2115 SetLastError(NTE_BAD_FLAGS
);
2119 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
2121 SetLastError(NTE_BAD_KEY
);
2125 if (pCryptKey
->dwState
== RSAENH_KEYSTATE_IDLE
)
2126 pCryptKey
->dwState
= RSAENH_KEYSTATE_ENCRYPTING
;
2128 if (pCryptKey
->dwState
!= RSAENH_KEYSTATE_ENCRYPTING
)
2130 SetLastError(NTE_BAD_DATA
);
2134 if (is_valid_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
)) {
2135 if (!RSAENH_CPHashData(hProv
, hHash
, pbData
, *pdwDataLen
, 0)) return FALSE
;
2138 if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_BLOCK
) {
2139 if (!Final
&& (*pdwDataLen
% pCryptKey
->dwBlockLen
)) {
2140 SetLastError(NTE_BAD_DATA
);
2144 dwEncryptedLen
= (*pdwDataLen
/pCryptKey
->dwBlockLen
+(Final
?1:0))*pCryptKey
->dwBlockLen
;
2146 if (pbData
== NULL
) {
2147 *pdwDataLen
= dwEncryptedLen
;
2150 else if (dwEncryptedLen
> dwBufLen
) {
2151 *pdwDataLen
= dwEncryptedLen
;
2152 SetLastError(ERROR_MORE_DATA
);
2156 /* Pad final block with length bytes */
2157 for (i
=*pdwDataLen
; i
<dwEncryptedLen
; i
++) pbData
[i
] = dwEncryptedLen
- *pdwDataLen
;
2158 *pdwDataLen
= dwEncryptedLen
;
2160 for (i
=0, in
=pbData
; i
<*pdwDataLen
; i
+=pCryptKey
->dwBlockLen
, in
+=pCryptKey
->dwBlockLen
) {
2161 switch (pCryptKey
->dwMode
) {
2162 case CRYPT_MODE_ECB
:
2163 encrypt_block_impl(pCryptKey
->aiAlgid
, 0, &pCryptKey
->context
, in
, out
,
2167 case CRYPT_MODE_CBC
:
2168 for (j
=0; j
<pCryptKey
->dwBlockLen
; j
++) in
[j
] ^= pCryptKey
->abChainVector
[j
];
2169 encrypt_block_impl(pCryptKey
->aiAlgid
, 0, &pCryptKey
->context
, in
, out
,
2171 memcpy(pCryptKey
->abChainVector
, out
, pCryptKey
->dwBlockLen
);
2174 case CRYPT_MODE_CFB
:
2175 for (j
=0; j
<pCryptKey
->dwBlockLen
; j
++) {
2176 encrypt_block_impl(pCryptKey
->aiAlgid
, 0, &pCryptKey
->context
,
2177 pCryptKey
->abChainVector
, o
, RSAENH_ENCRYPT
);
2178 out
[j
] = in
[j
] ^ o
[0];
2179 for (k
=0; k
<pCryptKey
->dwBlockLen
-1; k
++)
2180 pCryptKey
->abChainVector
[k
] = pCryptKey
->abChainVector
[k
+1];
2181 pCryptKey
->abChainVector
[k
] = out
[j
];
2186 SetLastError(NTE_BAD_ALGID
);
2189 memcpy(in
, out
, pCryptKey
->dwBlockLen
);
2191 } else if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_STREAM
) {
2192 if (pbData
== NULL
) {
2193 *pdwDataLen
= dwBufLen
;
2196 encrypt_stream_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pbData
, *pdwDataLen
);
2197 } else if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_RSA
) {
2198 if (pCryptKey
->aiAlgid
== CALG_RSA_SIGN
) {
2199 SetLastError(NTE_BAD_KEY
);
2203 *pdwDataLen
= pCryptKey
->dwBlockLen
;
2206 if (dwBufLen
< pCryptKey
->dwBlockLen
) {
2207 SetLastError(ERROR_MORE_DATA
);
2210 if (!pad_data(pbData
, *pdwDataLen
, pbData
, pCryptKey
->dwBlockLen
, dwFlags
)) return FALSE
;
2211 encrypt_block_impl(pCryptKey
->aiAlgid
, PK_PUBLIC
, &pCryptKey
->context
, pbData
, pbData
, RSAENH_ENCRYPT
);
2212 *pdwDataLen
= pCryptKey
->dwBlockLen
;
2215 SetLastError(NTE_BAD_TYPE
);
2219 if (Final
) setup_key(pCryptKey
);
2224 /******************************************************************************
2225 * CPDecrypt (RSAENH.@)
2230 * hProv [I] The key container hKey and hHash belong to.
2231 * hKey [I] The key used to decrypt the data.
2232 * hHash [I] An optional hash object for parallel hashing. See notes.
2233 * Final [I] Indicates if this is the last block of data to decrypt.
2234 * dwFlags [I] Currently no flags defined. Must be zero.
2235 * pbData [I/O] Pointer to the data to decrypt. Plaintext will also be stored there.
2236 * pdwDataLen [I/O] I: Length of ciphertext, O: Length of plaintext.
2243 * If a hash object handle is provided in hHash, it will be updated with the plaintext.
2244 * This is useful for message signatures.
2246 * This function uses the standard WINAPI protocol for querying data of dynamic length.
2248 BOOL WINAPI
RSAENH_CPDecrypt(HCRYPTPROV hProv
, HCRYPTKEY hKey
, HCRYPTHASH hHash
, BOOL Final
,
2249 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
)
2251 CRYPTKEY
*pCryptKey
;
2252 BYTE
*in
, out
[RSAENH_MAX_BLOCK_SIZE
], o
[RSAENH_MAX_BLOCK_SIZE
];
2256 TRACE("(hProv=%08lx, hKey=%08lx, hHash=%08lx, Final=%d, dwFlags=%08x, pbData=%p, "
2257 "pdwDataLen=%p)\n", hProv
, hKey
, hHash
, Final
, dwFlags
, pbData
, pdwDataLen
);
2259 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
2261 SetLastError(NTE_BAD_UID
);
2267 SetLastError(NTE_BAD_FLAGS
);
2271 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
2273 SetLastError(NTE_BAD_KEY
);
2277 if (pCryptKey
->dwState
== RSAENH_KEYSTATE_IDLE
)
2278 pCryptKey
->dwState
= RSAENH_KEYSTATE_ENCRYPTING
;
2280 if (pCryptKey
->dwState
!= RSAENH_KEYSTATE_ENCRYPTING
)
2282 SetLastError(NTE_BAD_DATA
);
2288 if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_BLOCK
) {
2289 for (i
=0, in
=pbData
; i
<*pdwDataLen
; i
+=pCryptKey
->dwBlockLen
, in
+=pCryptKey
->dwBlockLen
) {
2290 switch (pCryptKey
->dwMode
) {
2291 case CRYPT_MODE_ECB
:
2292 encrypt_block_impl(pCryptKey
->aiAlgid
, 0, &pCryptKey
->context
, in
, out
,
2296 case CRYPT_MODE_CBC
:
2297 encrypt_block_impl(pCryptKey
->aiAlgid
, 0, &pCryptKey
->context
, in
, out
,
2299 for (j
=0; j
<pCryptKey
->dwBlockLen
; j
++) out
[j
] ^= pCryptKey
->abChainVector
[j
];
2300 memcpy(pCryptKey
->abChainVector
, in
, pCryptKey
->dwBlockLen
);
2303 case CRYPT_MODE_CFB
:
2304 for (j
=0; j
<pCryptKey
->dwBlockLen
; j
++) {
2305 encrypt_block_impl(pCryptKey
->aiAlgid
, 0, &pCryptKey
->context
,
2306 pCryptKey
->abChainVector
, o
, RSAENH_ENCRYPT
);
2307 out
[j
] = in
[j
] ^ o
[0];
2308 for (k
=0; k
<pCryptKey
->dwBlockLen
-1; k
++)
2309 pCryptKey
->abChainVector
[k
] = pCryptKey
->abChainVector
[k
+1];
2310 pCryptKey
->abChainVector
[k
] = in
[j
];
2315 SetLastError(NTE_BAD_ALGID
);
2318 memcpy(in
, out
, pCryptKey
->dwBlockLen
);
2321 if (pbData
[*pdwDataLen
-1] &&
2322 pbData
[*pdwDataLen
-1] <= pCryptKey
->dwBlockLen
&&
2323 pbData
[*pdwDataLen
-1] < *pdwDataLen
) {
2324 BOOL padOkay
= TRUE
;
2326 /* check that every bad byte has the same value */
2327 for (i
= 1; padOkay
&& i
< pbData
[*pdwDataLen
-1]; i
++)
2328 if (pbData
[*pdwDataLen
- i
- 1] != pbData
[*pdwDataLen
- 1])
2331 *pdwDataLen
-= pbData
[*pdwDataLen
-1];
2333 SetLastError(NTE_BAD_DATA
);
2338 SetLastError(NTE_BAD_DATA
);
2343 } else if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_STREAM
) {
2344 encrypt_stream_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pbData
, *pdwDataLen
);
2345 } else if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_RSA
) {
2346 if (pCryptKey
->aiAlgid
== CALG_RSA_SIGN
) {
2347 SetLastError(NTE_BAD_KEY
);
2350 encrypt_block_impl(pCryptKey
->aiAlgid
, PK_PRIVATE
, &pCryptKey
->context
, pbData
, pbData
, RSAENH_DECRYPT
);
2351 if (!unpad_data(pbData
, pCryptKey
->dwBlockLen
, pbData
, pdwDataLen
, dwFlags
)) return FALSE
;
2354 SetLastError(NTE_BAD_TYPE
);
2358 if (Final
) setup_key(pCryptKey
);
2360 if (is_valid_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
)) {
2361 if (*pdwDataLen
>dwMax
||
2362 !RSAENH_CPHashData(hProv
, hHash
, pbData
, *pdwDataLen
, 0)) return FALSE
;
2368 static BOOL
crypt_export_simple(CRYPTKEY
*pCryptKey
, CRYPTKEY
*pPubKey
,
2369 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
)
2371 BLOBHEADER
*pBlobHeader
= (BLOBHEADER
*)pbData
;
2372 ALG_ID
*pAlgid
= (ALG_ID
*)(pBlobHeader
+1);
2375 if (!(GET_ALG_CLASS(pCryptKey
->aiAlgid
)&(ALG_CLASS_DATA_ENCRYPT
|ALG_CLASS_MSG_ENCRYPT
))) {
2376 SetLastError(NTE_BAD_KEY
); /* FIXME: error code? */
2380 dwDataLen
= sizeof(BLOBHEADER
) + sizeof(ALG_ID
) + pPubKey
->dwBlockLen
;
2382 if (*pdwDataLen
< dwDataLen
) {
2383 SetLastError(ERROR_MORE_DATA
);
2384 *pdwDataLen
= dwDataLen
;
2388 pBlobHeader
->bType
= SIMPLEBLOB
;
2389 pBlobHeader
->bVersion
= CUR_BLOB_VERSION
;
2390 pBlobHeader
->reserved
= 0;
2391 pBlobHeader
->aiKeyAlg
= pCryptKey
->aiAlgid
;
2393 *pAlgid
= pPubKey
->aiAlgid
;
2395 if (!pad_data(pCryptKey
->abKeyValue
, pCryptKey
->dwKeyLen
, (BYTE
*)(pAlgid
+1),
2396 pPubKey
->dwBlockLen
, dwFlags
))
2401 encrypt_block_impl(pPubKey
->aiAlgid
, PK_PUBLIC
, &pPubKey
->context
, (BYTE
*)(pAlgid
+1),
2402 (BYTE
*)(pAlgid
+1), RSAENH_ENCRYPT
);
2404 *pdwDataLen
= dwDataLen
;
2408 static BOOL
crypt_export_public_key(CRYPTKEY
*pCryptKey
, BYTE
*pbData
,
2411 BLOBHEADER
*pBlobHeader
= (BLOBHEADER
*)pbData
;
2412 RSAPUBKEY
*pRSAPubKey
= (RSAPUBKEY
*)(pBlobHeader
+1);
2415 if ((pCryptKey
->aiAlgid
!= CALG_RSA_KEYX
) && (pCryptKey
->aiAlgid
!= CALG_RSA_SIGN
)) {
2416 SetLastError(NTE_BAD_KEY
);
2420 dwDataLen
= sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) + pCryptKey
->dwKeyLen
;
2422 if (*pdwDataLen
< dwDataLen
) {
2423 SetLastError(ERROR_MORE_DATA
);
2424 *pdwDataLen
= dwDataLen
;
2428 pBlobHeader
->bType
= PUBLICKEYBLOB
;
2429 pBlobHeader
->bVersion
= CUR_BLOB_VERSION
;
2430 pBlobHeader
->reserved
= 0;
2431 pBlobHeader
->aiKeyAlg
= pCryptKey
->aiAlgid
;
2433 pRSAPubKey
->magic
= RSAENH_MAGIC_RSA1
;
2434 pRSAPubKey
->bitlen
= pCryptKey
->dwKeyLen
<< 3;
2436 export_public_key_impl((BYTE
*)(pRSAPubKey
+1), &pCryptKey
->context
,
2437 pCryptKey
->dwKeyLen
, &pRSAPubKey
->pubexp
);
2439 *pdwDataLen
= dwDataLen
;
2443 static BOOL
crypt_export_private_key(CRYPTKEY
*pCryptKey
, BOOL force
,
2444 BYTE
*pbData
, DWORD
*pdwDataLen
)
2446 BLOBHEADER
*pBlobHeader
= (BLOBHEADER
*)pbData
;
2447 RSAPUBKEY
*pRSAPubKey
= (RSAPUBKEY
*)(pBlobHeader
+1);
2450 if ((pCryptKey
->aiAlgid
!= CALG_RSA_KEYX
) && (pCryptKey
->aiAlgid
!= CALG_RSA_SIGN
)) {
2451 SetLastError(NTE_BAD_KEY
);
2454 if (!force
&& !(pCryptKey
->dwPermissions
& CRYPT_EXPORT
))
2456 SetLastError(NTE_BAD_KEY_STATE
);
2460 dwDataLen
= sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) +
2461 2 * pCryptKey
->dwKeyLen
+ 5 * ((pCryptKey
->dwKeyLen
+ 1) >> 1);
2463 if (*pdwDataLen
< dwDataLen
) {
2464 SetLastError(ERROR_MORE_DATA
);
2465 *pdwDataLen
= dwDataLen
;
2469 pBlobHeader
->bType
= PRIVATEKEYBLOB
;
2470 pBlobHeader
->bVersion
= CUR_BLOB_VERSION
;
2471 pBlobHeader
->reserved
= 0;
2472 pBlobHeader
->aiKeyAlg
= pCryptKey
->aiAlgid
;
2474 pRSAPubKey
->magic
= RSAENH_MAGIC_RSA2
;
2475 pRSAPubKey
->bitlen
= pCryptKey
->dwKeyLen
<< 3;
2477 export_private_key_impl((BYTE
*)(pRSAPubKey
+1), &pCryptKey
->context
,
2478 pCryptKey
->dwKeyLen
, &pRSAPubKey
->pubexp
);
2480 *pdwDataLen
= dwDataLen
;
2484 /******************************************************************************
2485 * crypt_export_key [Internal]
2487 * Export a key into a binary large object (BLOB). Called by CPExportKey and
2488 * by store_key_pair.
2491 * pCryptKey [I] Key to be exported.
2492 * hPubKey [I] Key used to encrypt sensitive BLOB data.
2493 * dwBlobType [I] SIMPLEBLOB, PUBLICKEYBLOB or PRIVATEKEYBLOB.
2494 * dwFlags [I] Currently none defined.
2495 * force [I] If TRUE, the key is written no matter what the key's
2496 * permissions are. Otherwise the key's permissions are
2497 * checked before exporting.
2498 * pbData [O] Pointer to a buffer where the BLOB will be written to.
2499 * pdwDataLen [I/O] I: Size of buffer at pbData, O: Size of BLOB
2505 static BOOL
crypt_export_key(CRYPTKEY
*pCryptKey
, HCRYPTKEY hPubKey
,
2506 DWORD dwBlobType
, DWORD dwFlags
, BOOL force
,
2507 BYTE
*pbData
, DWORD
*pdwDataLen
)
2511 if (dwFlags
& CRYPT_SSL2_FALLBACK
) {
2512 if (pCryptKey
->aiAlgid
!= CALG_SSL2_MASTER
) {
2513 SetLastError(NTE_BAD_KEY
);
2518 switch ((BYTE
)dwBlobType
)
2521 if (!lookup_handle(&handle_table
, hPubKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pPubKey
)){
2522 SetLastError(NTE_BAD_PUBLIC_KEY
); /* FIXME: error_code? */
2525 return crypt_export_simple(pCryptKey
, pPubKey
, dwFlags
, pbData
,
2529 if (is_valid_handle(&handle_table
, hPubKey
, RSAENH_MAGIC_KEY
)) {
2530 SetLastError(NTE_BAD_KEY
); /* FIXME: error code? */
2534 return crypt_export_public_key(pCryptKey
, pbData
, pdwDataLen
);
2536 case PRIVATEKEYBLOB
:
2537 return crypt_export_private_key(pCryptKey
, force
, pbData
, pdwDataLen
);
2540 SetLastError(NTE_BAD_TYPE
); /* FIXME: error code? */
2545 /******************************************************************************
2546 * CPExportKey (RSAENH.@)
2548 * Export a key into a binary large object (BLOB).
2551 * hProv [I] Key container from which a key is to be exported.
2552 * hKey [I] Key to be exported.
2553 * hPubKey [I] Key used to encrypt sensitive BLOB data.
2554 * dwBlobType [I] SIMPLEBLOB, PUBLICKEYBLOB or PRIVATEKEYBLOB.
2555 * dwFlags [I] Currently none defined.
2556 * pbData [O] Pointer to a buffer where the BLOB will be written to.
2557 * pdwDataLen [I/O] I: Size of buffer at pbData, O: Size of BLOB
2563 BOOL WINAPI
RSAENH_CPExportKey(HCRYPTPROV hProv
, HCRYPTKEY hKey
, HCRYPTKEY hPubKey
,
2564 DWORD dwBlobType
, DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
)
2566 CRYPTKEY
*pCryptKey
;
2568 TRACE("(hProv=%08lx, hKey=%08lx, hPubKey=%08lx, dwBlobType=%08x, dwFlags=%08x, pbData=%p,"
2569 "pdwDataLen=%p)\n", hProv
, hKey
, hPubKey
, dwBlobType
, dwFlags
, pbData
, pdwDataLen
);
2571 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
2573 SetLastError(NTE_BAD_UID
);
2577 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
2579 SetLastError(NTE_BAD_KEY
);
2583 return crypt_export_key(pCryptKey
, hPubKey
, dwBlobType
, dwFlags
, FALSE
,
2584 pbData
, pdwDataLen
);
2587 /******************************************************************************
2588 * release_and_install_key [Internal]
2590 * Release an existing key, if present, and replaces it with a new one.
2593 * hProv [I] Key container into which the key is to be imported.
2594 * src [I] Key which will replace *dest
2595 * dest [I] Points to key to be released and replaced with src
2596 * fStoreKey [I] If TRUE, the newly installed key is stored to the registry.
2598 static void release_and_install_key(HCRYPTPROV hProv
, HCRYPTKEY src
,
2599 HCRYPTKEY
*dest
, DWORD fStoreKey
)
2601 RSAENH_CPDestroyKey(hProv
, *dest
);
2602 copy_handle(&handle_table
, src
, RSAENH_MAGIC_KEY
, dest
);
2605 KEYCONTAINER
*pKeyContainer
;
2607 if (lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
,
2608 (OBJECTHDR
**)&pKeyContainer
))
2610 store_key_container_keys(pKeyContainer
);
2611 store_key_container_permissions(pKeyContainer
);
2616 /******************************************************************************
2617 * import_private_key [Internal]
2619 * Import a BLOB'ed private key into a key container.
2622 * hProv [I] Key container into which the private key is to be imported.
2623 * pbData [I] Pointer to a buffer which holds the private key BLOB.
2624 * dwDataLen [I] Length of data in buffer at pbData.
2625 * dwFlags [I] One of:
2626 * CRYPT_EXPORTABLE: the imported key is marked exportable
2627 * fStoreKey [I] If TRUE, the imported key is stored to the registry.
2628 * phKey [O] Handle to the imported key.
2632 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2633 * it's a PRIVATEKEYBLOB.
2639 static BOOL
import_private_key(HCRYPTPROV hProv
, CONST BYTE
*pbData
, DWORD dwDataLen
,
2640 DWORD dwFlags
, BOOL fStoreKey
, HCRYPTKEY
*phKey
)
2642 KEYCONTAINER
*pKeyContainer
;
2643 CRYPTKEY
*pCryptKey
;
2644 CONST BLOBHEADER
*pBlobHeader
= (CONST BLOBHEADER
*)pbData
;
2645 CONST RSAPUBKEY
*pRSAPubKey
= (CONST RSAPUBKEY
*)(pBlobHeader
+1);
2648 if (!lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
,
2649 (OBJECTHDR
**)&pKeyContainer
))
2651 SetLastError(NTE_BAD_UID
);
2655 if ((dwDataLen
< sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
)) ||
2656 (pRSAPubKey
->magic
!= RSAENH_MAGIC_RSA2
) ||
2657 (dwDataLen
< sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) +
2658 (2 * pRSAPubKey
->bitlen
>> 3) + (5 * ((pRSAPubKey
->bitlen
+8)>>4))))
2660 SetLastError(NTE_BAD_DATA
);
2664 *phKey
= new_key(hProv
, pBlobHeader
->aiKeyAlg
, MAKELONG(0,pRSAPubKey
->bitlen
), &pCryptKey
);
2665 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
2666 setup_key(pCryptKey
);
2667 ret
= import_private_key_impl((CONST BYTE
*)(pRSAPubKey
+1), &pCryptKey
->context
,
2668 pRSAPubKey
->bitlen
/8, pRSAPubKey
->pubexp
);
2670 if (dwFlags
& CRYPT_EXPORTABLE
)
2671 pCryptKey
->dwPermissions
|= CRYPT_EXPORT
;
2672 switch (pBlobHeader
->aiKeyAlg
)
2676 TRACE("installing signing key\n");
2677 release_and_install_key(hProv
, *phKey
, &pKeyContainer
->hSignatureKeyPair
,
2680 case AT_KEYEXCHANGE
:
2682 TRACE("installing key exchange key\n");
2683 release_and_install_key(hProv
, *phKey
, &pKeyContainer
->hKeyExchangeKeyPair
,
2691 /******************************************************************************
2692 * import_public_key [Internal]
2694 * Import a BLOB'ed public key into a key container.
2697 * hProv [I] Key container into which the public key is to be imported.
2698 * pbData [I] Pointer to a buffer which holds the public key BLOB.
2699 * dwDataLen [I] Length of data in buffer at pbData.
2700 * dwFlags [I] One of:
2701 * CRYPT_EXPORTABLE: the imported key is marked exportable
2702 * fStoreKey [I] If TRUE, the imported key is stored to the registry.
2703 * phKey [O] Handle to the imported key.
2707 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2708 * it's a PUBLICKEYBLOB.
2714 static BOOL
import_public_key(HCRYPTPROV hProv
, CONST BYTE
*pbData
, DWORD dwDataLen
,
2715 DWORD dwFlags
, BOOL fStoreKey
, HCRYPTKEY
*phKey
)
2717 KEYCONTAINER
*pKeyContainer
;
2718 CRYPTKEY
*pCryptKey
;
2719 CONST BLOBHEADER
*pBlobHeader
= (CONST BLOBHEADER
*)pbData
;
2720 CONST RSAPUBKEY
*pRSAPubKey
= (CONST RSAPUBKEY
*)(pBlobHeader
+1);
2724 if (!lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
,
2725 (OBJECTHDR
**)&pKeyContainer
))
2727 SetLastError(NTE_BAD_UID
);
2731 if ((dwDataLen
< sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
)) ||
2732 (pRSAPubKey
->magic
!= RSAENH_MAGIC_RSA1
) ||
2733 (dwDataLen
< sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) + (pRSAPubKey
->bitlen
>> 3)))
2735 SetLastError(NTE_BAD_DATA
);
2739 /* Since this is a public key blob, only the public key is
2740 * available, so only signature verification is possible.
2742 algID
= pBlobHeader
->aiKeyAlg
;
2743 *phKey
= new_key(hProv
, algID
, MAKELONG(0,pRSAPubKey
->bitlen
), &pCryptKey
);
2744 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
2745 setup_key(pCryptKey
);
2746 ret
= import_public_key_impl((CONST BYTE
*)(pRSAPubKey
+1), &pCryptKey
->context
,
2747 pRSAPubKey
->bitlen
>> 3, pRSAPubKey
->pubexp
);
2749 if (dwFlags
& CRYPT_EXPORTABLE
)
2750 pCryptKey
->dwPermissions
|= CRYPT_EXPORT
;
2751 switch (pBlobHeader
->aiKeyAlg
)
2753 case AT_KEYEXCHANGE
:
2755 TRACE("installing public key\n");
2756 release_and_install_key(hProv
, *phKey
, &pKeyContainer
->hKeyExchangeKeyPair
,
2764 /******************************************************************************
2765 * import_symmetric_key [Internal]
2767 * Import a BLOB'ed symmetric key into a key container.
2770 * hProv [I] Key container into which the symmetric key is to be imported.
2771 * pbData [I] Pointer to a buffer which holds the symmetric key BLOB.
2772 * dwDataLen [I] Length of data in buffer at pbData.
2773 * hPubKey [I] Key used to decrypt sensitive BLOB data.
2774 * dwFlags [I] One of:
2775 * CRYPT_EXPORTABLE: the imported key is marked exportable
2776 * phKey [O] Handle to the imported key.
2780 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2781 * it's a SIMPLEBLOB.
2787 static BOOL
import_symmetric_key(HCRYPTPROV hProv
, CONST BYTE
*pbData
,
2788 DWORD dwDataLen
, HCRYPTKEY hPubKey
,
2789 DWORD dwFlags
, HCRYPTKEY
*phKey
)
2791 CRYPTKEY
*pCryptKey
, *pPubKey
;
2792 CONST BLOBHEADER
*pBlobHeader
= (CONST BLOBHEADER
*)pbData
;
2793 CONST ALG_ID
*pAlgid
= (CONST ALG_ID
*)(pBlobHeader
+1);
2794 CONST BYTE
*pbKeyStream
= (CONST BYTE
*)(pAlgid
+ 1);
2798 if (!lookup_handle(&handle_table
, hPubKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pPubKey
) ||
2799 pPubKey
->aiAlgid
!= CALG_RSA_KEYX
)
2801 SetLastError(NTE_BAD_PUBLIC_KEY
); /* FIXME: error code? */
2805 if (dwDataLen
< sizeof(BLOBHEADER
)+sizeof(ALG_ID
)+pPubKey
->dwBlockLen
)
2807 SetLastError(NTE_BAD_DATA
); /* FIXME: error code */
2811 pbDecrypted
= HeapAlloc(GetProcessHeap(), 0, pPubKey
->dwBlockLen
);
2812 if (!pbDecrypted
) return FALSE
;
2813 encrypt_block_impl(pPubKey
->aiAlgid
, PK_PRIVATE
, &pPubKey
->context
, pbKeyStream
, pbDecrypted
,
2816 dwKeyLen
= RSAENH_MAX_KEY_SIZE
;
2817 if (!unpad_data(pbDecrypted
, pPubKey
->dwBlockLen
, pbDecrypted
, &dwKeyLen
, dwFlags
)) {
2818 HeapFree(GetProcessHeap(), 0, pbDecrypted
);
2822 *phKey
= new_key(hProv
, pBlobHeader
->aiKeyAlg
, dwKeyLen
<<19, &pCryptKey
);
2823 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
2825 HeapFree(GetProcessHeap(), 0, pbDecrypted
);
2828 memcpy(pCryptKey
->abKeyValue
, pbDecrypted
, dwKeyLen
);
2829 HeapFree(GetProcessHeap(), 0, pbDecrypted
);
2830 setup_key(pCryptKey
);
2831 if (dwFlags
& CRYPT_EXPORTABLE
)
2832 pCryptKey
->dwPermissions
|= CRYPT_EXPORT
;
2836 /******************************************************************************
2837 * import_key [Internal]
2839 * Import a BLOB'ed key into a key container, optionally storing the key's
2840 * value to the registry.
2843 * hProv [I] Key container into which the key is to be imported.
2844 * pbData [I] Pointer to a buffer which holds the BLOB.
2845 * dwDataLen [I] Length of data in buffer at pbData.
2846 * hPubKey [I] Key used to decrypt sensitive BLOB data.
2847 * dwFlags [I] One of:
2848 * CRYPT_EXPORTABLE: the imported key is marked exportable
2849 * fStoreKey [I] If TRUE, the imported key is stored to the registry.
2850 * phKey [O] Handle to the imported key.
2856 static BOOL
import_key(HCRYPTPROV hProv
, CONST BYTE
*pbData
, DWORD dwDataLen
,
2857 HCRYPTKEY hPubKey
, DWORD dwFlags
, BOOL fStoreKey
,
2860 KEYCONTAINER
*pKeyContainer
;
2861 CONST BLOBHEADER
*pBlobHeader
= (CONST BLOBHEADER
*)pbData
;
2863 if (!lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
,
2864 (OBJECTHDR
**)&pKeyContainer
))
2866 SetLastError(NTE_BAD_UID
);
2870 if (dwDataLen
< sizeof(BLOBHEADER
) ||
2871 pBlobHeader
->bVersion
!= CUR_BLOB_VERSION
||
2872 pBlobHeader
->reserved
!= 0)
2874 SetLastError(NTE_BAD_DATA
);
2878 /* If this is a verify-only context, the key is not persisted regardless of
2879 * fStoreKey's original value.
2881 fStoreKey
= fStoreKey
&& !(dwFlags
& CRYPT_VERIFYCONTEXT
);
2882 switch (pBlobHeader
->bType
)
2884 case PRIVATEKEYBLOB
:
2885 return import_private_key(hProv
, pbData
, dwDataLen
, dwFlags
,
2889 return import_public_key(hProv
, pbData
, dwDataLen
, dwFlags
,
2893 return import_symmetric_key(hProv
, pbData
, dwDataLen
, hPubKey
,
2897 SetLastError(NTE_BAD_TYPE
); /* FIXME: error code? */
2902 /******************************************************************************
2903 * CPImportKey (RSAENH.@)
2905 * Import a BLOB'ed key into a key container.
2908 * hProv [I] Key container into which the key is to be imported.
2909 * pbData [I] Pointer to a buffer which holds the BLOB.
2910 * dwDataLen [I] Length of data in buffer at pbData.
2911 * hPubKey [I] Key used to decrypt sensitive BLOB data.
2912 * dwFlags [I] One of:
2913 * CRYPT_EXPORTABLE: the imported key is marked exportable
2914 * phKey [O] Handle to the imported key.
2920 BOOL WINAPI
RSAENH_CPImportKey(HCRYPTPROV hProv
, CONST BYTE
*pbData
, DWORD dwDataLen
,
2921 HCRYPTKEY hPubKey
, DWORD dwFlags
, HCRYPTKEY
*phKey
)
2923 TRACE("(hProv=%08lx, pbData=%p, dwDataLen=%d, hPubKey=%08lx, dwFlags=%08x, phKey=%p)\n",
2924 hProv
, pbData
, dwDataLen
, hPubKey
, dwFlags
, phKey
);
2926 return import_key(hProv
, pbData
, dwDataLen
, hPubKey
, dwFlags
, TRUE
, phKey
);
2929 /******************************************************************************
2930 * CPGenKey (RSAENH.@)
2932 * Generate a key in the key container
2935 * hProv [I] Key container for which a key is to be generated.
2936 * Algid [I] Crypto algorithm identifier for the key to be generated.
2937 * dwFlags [I] Upper 16 bits: Binary length of key. Lower 16 bits: Flags. See Notes
2938 * phKey [O] Handle to the generated key.
2945 * Flags currently not considered.
2948 * Private key-exchange- and signature-keys can be generated with Algid AT_KEYEXCHANGE
2949 * and AT_SIGNATURE values.
2951 BOOL WINAPI
RSAENH_CPGenKey(HCRYPTPROV hProv
, ALG_ID Algid
, DWORD dwFlags
, HCRYPTKEY
*phKey
)
2953 KEYCONTAINER
*pKeyContainer
;
2954 CRYPTKEY
*pCryptKey
;
2956 TRACE("(hProv=%08lx, aiAlgid=%d, dwFlags=%08x, phKey=%p)\n", hProv
, Algid
, dwFlags
, phKey
);
2958 if (!lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
,
2959 (OBJECTHDR
**)&pKeyContainer
))
2961 /* MSDN: hProv not containing valid context handle */
2962 SetLastError(NTE_BAD_UID
);
2970 *phKey
= new_key(hProv
, CALG_RSA_SIGN
, dwFlags
, &pCryptKey
);
2972 new_key_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pCryptKey
->dwKeyLen
);
2973 setup_key(pCryptKey
);
2974 if (Algid
== AT_SIGNATURE
) {
2975 RSAENH_CPDestroyKey(hProv
, pKeyContainer
->hSignatureKeyPair
);
2976 copy_handle(&handle_table
, *phKey
, RSAENH_MAGIC_KEY
,
2977 &pKeyContainer
->hSignatureKeyPair
);
2982 case AT_KEYEXCHANGE
:
2984 *phKey
= new_key(hProv
, CALG_RSA_KEYX
, dwFlags
, &pCryptKey
);
2986 new_key_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pCryptKey
->dwKeyLen
);
2987 setup_key(pCryptKey
);
2988 if (Algid
== AT_KEYEXCHANGE
) {
2989 RSAENH_CPDestroyKey(hProv
, pKeyContainer
->hKeyExchangeKeyPair
);
2990 copy_handle(&handle_table
, *phKey
, RSAENH_MAGIC_KEY
,
2991 &pKeyContainer
->hKeyExchangeKeyPair
);
3005 case CALG_PCT1_MASTER
:
3006 case CALG_SSL2_MASTER
:
3007 case CALG_SSL3_MASTER
:
3008 case CALG_TLS1_MASTER
:
3009 *phKey
= new_key(hProv
, Algid
, dwFlags
, &pCryptKey
);
3011 gen_rand_impl(pCryptKey
->abKeyValue
, RSAENH_MAX_KEY_SIZE
);
3013 case CALG_SSL3_MASTER
:
3014 pCryptKey
->abKeyValue
[0] = RSAENH_SSL3_VERSION_MAJOR
;
3015 pCryptKey
->abKeyValue
[1] = RSAENH_SSL3_VERSION_MINOR
;
3018 case CALG_TLS1_MASTER
:
3019 pCryptKey
->abKeyValue
[0] = RSAENH_TLS1_VERSION_MAJOR
;
3020 pCryptKey
->abKeyValue
[1] = RSAENH_TLS1_VERSION_MINOR
;
3023 setup_key(pCryptKey
);
3028 /* MSDN: Algorithm not supported specified by Algid */
3029 SetLastError(NTE_BAD_ALGID
);
3033 return *phKey
!= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
3036 /******************************************************************************
3037 * CPGenRandom (RSAENH.@)
3039 * Generate a random byte stream.
3042 * hProv [I] Key container that is used to generate random bytes.
3043 * dwLen [I] Specifies the number of requested random data bytes.
3044 * pbBuffer [O] Random bytes will be stored here.
3050 BOOL WINAPI
RSAENH_CPGenRandom(HCRYPTPROV hProv
, DWORD dwLen
, BYTE
*pbBuffer
)
3052 TRACE("(hProv=%08lx, dwLen=%d, pbBuffer=%p)\n", hProv
, dwLen
, pbBuffer
);
3054 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
3056 /* MSDN: hProv not containing valid context handle */
3057 SetLastError(NTE_BAD_UID
);
3061 return gen_rand_impl(pbBuffer
, dwLen
);
3064 /******************************************************************************
3065 * CPGetHashParam (RSAENH.@)
3067 * Query parameters of an hash object.
3070 * hProv [I] The kea container, which the hash belongs to.
3071 * hHash [I] The hash object that is to be queried.
3072 * dwParam [I] Specifies the parameter that is to be queried.
3073 * pbData [I] Pointer to the buffer where the parameter value will be stored.
3074 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
3075 * dwFlags [I] None currently defined.
3082 * Valid dwParams are: HP_ALGID, HP_HASHSIZE, HP_HASHVALUE. The hash will be
3083 * finalized if HP_HASHVALUE is queried.
3085 BOOL WINAPI
RSAENH_CPGetHashParam(HCRYPTPROV hProv
, HCRYPTHASH hHash
, DWORD dwParam
, BYTE
*pbData
,
3086 DWORD
*pdwDataLen
, DWORD dwFlags
)
3088 CRYPTHASH
*pCryptHash
;
3090 TRACE("(hProv=%08lx, hHash=%08lx, dwParam=%08x, pbData=%p, pdwDataLen=%p, dwFlags=%08x)\n",
3091 hProv
, hHash
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
3093 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
3095 SetLastError(NTE_BAD_UID
);
3101 SetLastError(NTE_BAD_FLAGS
);
3105 if (!lookup_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
,
3106 (OBJECTHDR
**)&pCryptHash
))
3108 SetLastError(NTE_BAD_HASH
);
3114 SetLastError(ERROR_INVALID_PARAMETER
);
3121 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&pCryptHash
->aiAlgid
,
3125 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&pCryptHash
->dwHashSize
,
3129 if (pCryptHash
->aiAlgid
== CALG_TLS1PRF
) {
3130 return tls1_prf(hProv
, pCryptHash
->hKey
, &pCryptHash
->tpPRFParams
.blobLabel
,
3131 &pCryptHash
->tpPRFParams
.blobSeed
, pbData
, *pdwDataLen
);
3134 if ( pbData
== NULL
) {
3135 *pdwDataLen
= pCryptHash
->dwHashSize
;
3139 if (pbData
&& (pCryptHash
->dwState
!= RSAENH_HASHSTATE_FINISHED
))
3141 finalize_hash(pCryptHash
);
3142 pCryptHash
->dwState
= RSAENH_HASHSTATE_FINISHED
;
3145 return copy_param(pbData
, pdwDataLen
, pCryptHash
->abHashValue
,
3146 pCryptHash
->dwHashSize
);
3149 SetLastError(NTE_BAD_TYPE
);
3154 /******************************************************************************
3155 * CPSetKeyParam (RSAENH.@)
3157 * Set a parameter of a key object
3160 * hProv [I] The key container to which the key belongs.
3161 * hKey [I] The key for which a parameter is to be set.
3162 * dwParam [I] Parameter type. See Notes.
3163 * pbData [I] Pointer to the parameter value.
3164 * dwFlags [I] Currently none defined.
3171 * Defined dwParam types are:
3172 * - KP_MODE: Values MODE_CBC, MODE_ECB, MODE_CFB.
3173 * - KP_MODE_BITS: Shift width for cipher feedback mode. (Currently ignored by MS CSP's)
3174 * - KP_PERMISSIONS: Or'ed combination of CRYPT_ENCRYPT, CRYPT_DECRYPT,
3175 * CRYPT_EXPORT, CRYPT_READ, CRYPT_WRITE, CRYPT_MAC
3176 * - KP_IV: Initialization vector
3178 BOOL WINAPI
RSAENH_CPSetKeyParam(HCRYPTPROV hProv
, HCRYPTKEY hKey
, DWORD dwParam
, BYTE
*pbData
,
3181 CRYPTKEY
*pCryptKey
;
3183 TRACE("(hProv=%08lx, hKey=%08lx, dwParam=%08x, pbData=%p, dwFlags=%08x)\n", hProv
, hKey
,
3184 dwParam
, pbData
, dwFlags
);
3186 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
3188 SetLastError(NTE_BAD_UID
);
3193 SetLastError(NTE_BAD_FLAGS
);
3197 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
3199 SetLastError(NTE_BAD_KEY
);
3205 /* The MS providers only support PKCS5_PADDING */
3206 if (*(DWORD
*)pbData
!= PKCS5_PADDING
) {
3207 SetLastError(NTE_BAD_DATA
);
3213 pCryptKey
->dwMode
= *(DWORD
*)pbData
;
3217 pCryptKey
->dwModeBits
= *(DWORD
*)pbData
;
3220 case KP_PERMISSIONS
:
3222 DWORD perms
= *(DWORD
*)pbData
;
3224 if ((perms
& CRYPT_EXPORT
) &&
3225 !(pCryptKey
->dwPermissions
& CRYPT_EXPORT
))
3227 SetLastError(NTE_BAD_DATA
);
3230 else if (!(perms
& CRYPT_EXPORT
) &&
3231 (pCryptKey
->dwPermissions
& CRYPT_EXPORT
))
3233 /* Clearing the export permission appears to be ignored,
3236 perms
|= CRYPT_EXPORT
;
3238 pCryptKey
->dwPermissions
= perms
;
3243 memcpy(pCryptKey
->abInitVector
, pbData
, pCryptKey
->dwBlockLen
);
3244 setup_key(pCryptKey
);
3249 CRYPT_INTEGER_BLOB
*blob
= (CRYPT_INTEGER_BLOB
*)pbData
;
3251 /* salt length can't be greater than 184 bits = 24 bytes */
3252 if (blob
->cbData
> 24)
3254 SetLastError(NTE_BAD_DATA
);
3257 memcpy(pCryptKey
->abKeyValue
+ pCryptKey
->dwKeyLen
, blob
->pbData
,
3259 pCryptKey
->dwSaltLen
= blob
->cbData
;
3260 setup_key(pCryptKey
);
3264 case KP_EFFECTIVE_KEYLEN
:
3265 switch (pCryptKey
->aiAlgid
) {
3269 SetLastError(ERROR_INVALID_PARAMETER
);
3272 else if (!*(DWORD
*)pbData
|| *(DWORD
*)pbData
> 1024)
3274 SetLastError(NTE_BAD_DATA
);
3279 pCryptKey
->dwEffectiveKeyLen
= *(DWORD
*)pbData
;
3280 setup_key(pCryptKey
);
3284 SetLastError(NTE_BAD_TYPE
);
3289 case KP_SCHANNEL_ALG
:
3290 switch (((PSCHANNEL_ALG
)pbData
)->dwUse
) {
3291 case SCHANNEL_ENC_KEY
:
3292 memcpy(&pCryptKey
->siSChannelInfo
.saEncAlg
, pbData
, sizeof(SCHANNEL_ALG
));
3295 case SCHANNEL_MAC_KEY
:
3296 memcpy(&pCryptKey
->siSChannelInfo
.saMACAlg
, pbData
, sizeof(SCHANNEL_ALG
));
3300 SetLastError(NTE_FAIL
); /* FIXME: error code */
3305 case KP_CLIENT_RANDOM
:
3306 return copy_data_blob(&pCryptKey
->siSChannelInfo
.blobClientRandom
, (PCRYPT_DATA_BLOB
)pbData
);
3308 case KP_SERVER_RANDOM
:
3309 return copy_data_blob(&pCryptKey
->siSChannelInfo
.blobServerRandom
, (PCRYPT_DATA_BLOB
)pbData
);
3312 SetLastError(NTE_BAD_TYPE
);
3317 /******************************************************************************
3318 * CPGetKeyParam (RSAENH.@)
3320 * Query a key parameter.
3323 * hProv [I] The key container, which the key belongs to.
3324 * hHash [I] The key object that is to be queried.
3325 * dwParam [I] Specifies the parameter that is to be queried.
3326 * pbData [I] Pointer to the buffer where the parameter value will be stored.
3327 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
3328 * dwFlags [I] None currently defined.
3335 * Defined dwParam types are:
3336 * - KP_MODE: Values MODE_CBC, MODE_ECB, MODE_CFB.
3337 * - KP_MODE_BITS: Shift width for cipher feedback mode.
3338 * (Currently ignored by MS CSP's - always eight)
3339 * - KP_PERMISSIONS: Or'ed combination of CRYPT_ENCRYPT, CRYPT_DECRYPT,
3340 * CRYPT_EXPORT, CRYPT_READ, CRYPT_WRITE, CRYPT_MAC
3341 * - KP_IV: Initialization vector.
3342 * - KP_KEYLEN: Bitwidth of the key.
3343 * - KP_BLOCKLEN: Size of a block cipher block.
3344 * - KP_SALT: Salt value.
3346 BOOL WINAPI
RSAENH_CPGetKeyParam(HCRYPTPROV hProv
, HCRYPTKEY hKey
, DWORD dwParam
, BYTE
*pbData
,
3347 DWORD
*pdwDataLen
, DWORD dwFlags
)
3349 CRYPTKEY
*pCryptKey
;
3352 TRACE("(hProv=%08lx, hKey=%08lx, dwParam=%08x, pbData=%p, pdwDataLen=%p dwFlags=%08x)\n",
3353 hProv
, hKey
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
3355 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
3357 SetLastError(NTE_BAD_UID
);
3362 SetLastError(NTE_BAD_FLAGS
);
3366 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
3368 SetLastError(NTE_BAD_KEY
);
3375 return copy_param(pbData
, pdwDataLen
, pCryptKey
->abInitVector
,
3376 pCryptKey
->dwBlockLen
);
3379 return copy_param(pbData
, pdwDataLen
,
3380 &pCryptKey
->abKeyValue
[pCryptKey
->dwKeyLen
], pCryptKey
->dwSaltLen
);
3383 dwValue
= PKCS5_PADDING
;
3384 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwValue
, sizeof(DWORD
));
3387 dwValue
= pCryptKey
->dwKeyLen
<< 3;
3388 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwValue
, sizeof(DWORD
));
3390 case KP_EFFECTIVE_KEYLEN
:
3391 if (pCryptKey
->dwEffectiveKeyLen
)
3392 dwValue
= pCryptKey
->dwEffectiveKeyLen
;
3394 dwValue
= pCryptKey
->dwKeyLen
<< 3;
3395 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwValue
, sizeof(DWORD
));
3398 dwValue
= pCryptKey
->dwBlockLen
<< 3;
3399 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwValue
, sizeof(DWORD
));
3402 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&pCryptKey
->dwMode
, sizeof(DWORD
));
3405 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&pCryptKey
->dwModeBits
,
3408 case KP_PERMISSIONS
:
3409 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&pCryptKey
->dwPermissions
,
3413 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&pCryptKey
->aiAlgid
, sizeof(DWORD
));
3416 SetLastError(NTE_BAD_TYPE
);
3421 /******************************************************************************
3422 * CPGetProvParam (RSAENH.@)
3424 * Query a CSP parameter.
3427 * hProv [I] The key container that is to be queried.
3428 * dwParam [I] Specifies the parameter that is to be queried.
3429 * pbData [I] Pointer to the buffer where the parameter value will be stored.
3430 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
3431 * dwFlags [I] CRYPT_FIRST: Start enumeration (for PP_ENUMALGS{_EX}).
3437 * Defined dwParam types:
3438 * - PP_CONTAINER: Name of the key container.
3439 * - PP_NAME: Name of the cryptographic service provider.
3440 * - PP_SIG_KEYSIZE_INC: RSA signature keywidth granularity in bits.
3441 * - PP_KEYX_KEYSIZE_INC: RSA key-exchange keywidth granularity in bits.
3442 * - PP_ENUMALGS{_EX}: Query provider capabilities.
3444 BOOL WINAPI
RSAENH_CPGetProvParam(HCRYPTPROV hProv
, DWORD dwParam
, BYTE
*pbData
,
3445 DWORD
*pdwDataLen
, DWORD dwFlags
)
3447 KEYCONTAINER
*pKeyContainer
;
3448 PROV_ENUMALGS provEnumalgs
;
3452 /* This is for dwParam PP_CRYPT_COUNT_KEY_USE.
3453 * IE6 SP1 asks for it in the 'About' dialog.
3454 * Returning this BLOB seems to satisfy IE. The marked 0x00 seem
3455 * to be 'don't care's. If you know anything more specific about
3456 * this provider parameter, please report to wine-devel@winehq.org */
3457 static CONST BYTE abWTF
[96] = {
3458 0xb0, 0x25, 0x63, 0x86, 0x9c, 0xab, 0xb6, 0x37,
3459 0xe8, 0x82, /**/0x00,/**/ 0x72, 0x06, 0xb2, /**/0x00,/**/ 0x3b,
3460 0x60, 0x35, /**/0x00,/**/ 0x3b, 0x88, 0xce, /**/0x00,/**/ 0x82,
3461 0xbc, 0x7a, /**/0x00,/**/ 0xb7, 0x4f, 0x7e, /**/0x00,/**/ 0xde,
3462 0x92, 0xf1, /**/0x00,/**/ 0x83, 0xea, 0x5e, /**/0x00,/**/ 0xc8,
3463 0x12, 0x1e, 0xd4, 0x06, 0xf7, 0x66, /**/0x00,/**/ 0x01,
3464 0x29, 0xa4, /**/0x00,/**/ 0xf8, 0x24, 0x0c, /**/0x00,/**/ 0x33,
3465 0x06, 0x80, /**/0x00,/**/ 0x02, 0x46, 0x0b, /**/0x00,/**/ 0x6d,
3466 0x5b, 0xca, /**/0x00,/**/ 0x9a, 0x10, 0xf0, /**/0x00,/**/ 0x05,
3467 0x19, 0xd0, /**/0x00,/**/ 0x2c, 0xf6, 0x27, /**/0x00,/**/ 0xaa,
3468 0x7c, 0x6f, /**/0x00,/**/ 0xb9, 0xd8, 0x72, /**/0x00,/**/ 0x03,
3469 0xf3, 0x81, /**/0x00,/**/ 0xfa, 0xe8, 0x26, /**/0x00,/**/ 0xca
3472 TRACE("(hProv=%08lx, dwParam=%08x, pbData=%p, pdwDataLen=%p, dwFlags=%08x)\n",
3473 hProv
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
3476 SetLastError(ERROR_INVALID_PARAMETER
);
3480 if (!lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
,
3481 (OBJECTHDR
**)&pKeyContainer
))
3483 /* MSDN: hProv not containing valid context handle */
3484 SetLastError(NTE_BAD_UID
);
3491 case PP_UNIQUE_CONTAINER
:/* MSDN says we can return the same value as PP_CONTAINER */
3492 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)pKeyContainer
->szName
,
3493 strlen(pKeyContainer
->szName
)+1);
3496 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)pKeyContainer
->szProvName
,
3497 strlen(pKeyContainer
->szProvName
)+1);
3500 dwTemp
= PROV_RSA_FULL
;
3501 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwTemp
, sizeof(dwTemp
));
3504 dwTemp
= AT_SIGNATURE
| AT_KEYEXCHANGE
;
3505 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwTemp
, sizeof(dwTemp
));
3507 case PP_KEYSET_TYPE
:
3508 dwTemp
= pKeyContainer
->dwFlags
& CRYPT_MACHINE_KEYSET
;
3509 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwTemp
, sizeof(dwTemp
));
3512 dwTemp
= CRYPT_SEC_DESCR
;
3513 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwTemp
, sizeof(dwTemp
));
3515 case PP_SIG_KEYSIZE_INC
:
3516 case PP_KEYX_KEYSIZE_INC
:
3518 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwTemp
, sizeof(dwTemp
));
3521 dwTemp
= CRYPT_IMPL_SOFTWARE
;
3522 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwTemp
, sizeof(dwTemp
));
3525 dwTemp
= 0x00000200;
3526 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwTemp
, sizeof(dwTemp
));
3528 case PP_ENUMCONTAINERS
:
3529 if ((dwFlags
& CRYPT_FIRST
) == CRYPT_FIRST
) pKeyContainer
->dwEnumContainersCtr
= 0;
3532 *pdwDataLen
= (DWORD
)MAX_PATH
+ 1;
3536 if (!open_container_key("", dwFlags
, &hKey
))
3538 SetLastError(ERROR_NO_MORE_ITEMS
);
3542 dwTemp
= *pdwDataLen
;
3543 switch (RegEnumKeyExA(hKey
, pKeyContainer
->dwEnumContainersCtr
, (LPSTR
)pbData
, &dwTemp
,
3544 NULL
, NULL
, NULL
, NULL
))
3546 case ERROR_MORE_DATA
:
3547 *pdwDataLen
= (DWORD
)MAX_PATH
+ 1;
3550 pKeyContainer
->dwEnumContainersCtr
++;
3554 case ERROR_NO_MORE_ITEMS
:
3556 SetLastError(ERROR_NO_MORE_ITEMS
);
3562 case PP_ENUMALGS_EX
:
3563 if (((pKeyContainer
->dwEnumAlgsCtr
>= RSAENH_MAX_ENUMALGS
-1) ||
3564 (!aProvEnumAlgsEx
[pKeyContainer
->dwPersonality
]
3565 [pKeyContainer
->dwEnumAlgsCtr
+1].aiAlgid
)) &&
3566 ((dwFlags
& CRYPT_FIRST
) != CRYPT_FIRST
))
3568 SetLastError(ERROR_NO_MORE_ITEMS
);
3572 if (dwParam
== PP_ENUMALGS
) {
3573 if (pbData
&& (*pdwDataLen
>= sizeof(PROV_ENUMALGS
)))
3574 pKeyContainer
->dwEnumAlgsCtr
= ((dwFlags
& CRYPT_FIRST
) == CRYPT_FIRST
) ?
3575 0 : pKeyContainer
->dwEnumAlgsCtr
+1;
3577 provEnumalgs
.aiAlgid
= aProvEnumAlgsEx
3578 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
].aiAlgid
;
3579 provEnumalgs
.dwBitLen
= aProvEnumAlgsEx
3580 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
].dwDefaultLen
;
3581 provEnumalgs
.dwNameLen
= aProvEnumAlgsEx
3582 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
].dwNameLen
;
3583 memcpy(provEnumalgs
.szName
, aProvEnumAlgsEx
3584 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
].szName
,
3587 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&provEnumalgs
,
3588 sizeof(PROV_ENUMALGS
));
3590 if (pbData
&& (*pdwDataLen
>= sizeof(PROV_ENUMALGS_EX
)))
3591 pKeyContainer
->dwEnumAlgsCtr
= ((dwFlags
& CRYPT_FIRST
) == CRYPT_FIRST
) ?
3592 0 : pKeyContainer
->dwEnumAlgsCtr
+1;
3594 return copy_param(pbData
, pdwDataLen
,
3595 (CONST BYTE
*)&aProvEnumAlgsEx
3596 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
],
3597 sizeof(PROV_ENUMALGS_EX
));
3600 case PP_CRYPT_COUNT_KEY_USE
: /* Asked for by IE About dialog */
3601 return copy_param(pbData
, pdwDataLen
, abWTF
, sizeof(abWTF
));
3604 /* MSDN: Unknown parameter number in dwParam */
3605 SetLastError(NTE_BAD_TYPE
);
3610 /******************************************************************************
3611 * CPDeriveKey (RSAENH.@)
3613 * Derives a key from a hash value.
3616 * hProv [I] Key container for which a key is to be generated.
3617 * Algid [I] Crypto algorithm identifier for the key to be generated.
3618 * hBaseData [I] Hash from whose value the key will be derived.
3619 * dwFlags [I] See Notes.
3620 * phKey [O] The generated key.
3628 * - CRYPT_EXPORTABLE: Key can be exported.
3629 * - CRYPT_NO_SALT: No salt is used for 40 bit keys.
3630 * - CRYPT_CREATE_SALT: Use remaining bits as salt value.
3632 BOOL WINAPI
RSAENH_CPDeriveKey(HCRYPTPROV hProv
, ALG_ID Algid
, HCRYPTHASH hBaseData
,
3633 DWORD dwFlags
, HCRYPTKEY
*phKey
)
3635 CRYPTKEY
*pCryptKey
, *pMasterKey
;
3636 CRYPTHASH
*pCryptHash
;
3637 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
*2];
3640 TRACE("(hProv=%08lx, Algid=%d, hBaseData=%08lx, dwFlags=%08x phKey=%p)\n", hProv
, Algid
,
3641 hBaseData
, dwFlags
, phKey
);
3643 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
3645 SetLastError(NTE_BAD_UID
);
3649 if (!lookup_handle(&handle_table
, hBaseData
, RSAENH_MAGIC_HASH
,
3650 (OBJECTHDR
**)&pCryptHash
))
3652 SetLastError(NTE_BAD_HASH
);
3658 SetLastError(ERROR_INVALID_PARAMETER
);
3662 switch (GET_ALG_CLASS(Algid
))
3664 case ALG_CLASS_DATA_ENCRYPT
:
3665 *phKey
= new_key(hProv
, Algid
, dwFlags
, &pCryptKey
);
3666 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
3669 * We derive the key material from the hash.
3670 * If the hash value is not large enough for the claimed key, we have to construct
3671 * a larger binary value based on the hash. This is documented in MSDN: CryptDeriveKey.
3673 dwLen
= RSAENH_MAX_HASH_SIZE
;
3674 RSAENH_CPGetHashParam(pCryptHash
->hProv
, hBaseData
, HP_HASHVAL
, abHashValue
, &dwLen
, 0);
3676 if (dwLen
< pCryptKey
->dwKeyLen
) {
3677 BYTE pad1
[RSAENH_HMAC_DEF_PAD_LEN
], pad2
[RSAENH_HMAC_DEF_PAD_LEN
];
3678 BYTE old_hashval
[RSAENH_MAX_HASH_SIZE
];
3681 memcpy(old_hashval
, pCryptHash
->abHashValue
, RSAENH_MAX_HASH_SIZE
);
3683 for (i
=0; i
<RSAENH_HMAC_DEF_PAD_LEN
; i
++) {
3684 pad1
[i
] = RSAENH_HMAC_DEF_IPAD_CHAR
^ (i
<dwLen
? abHashValue
[i
] : 0);
3685 pad2
[i
] = RSAENH_HMAC_DEF_OPAD_CHAR
^ (i
<dwLen
? abHashValue
[i
] : 0);
3688 init_hash(pCryptHash
);
3689 update_hash(pCryptHash
, pad1
, RSAENH_HMAC_DEF_PAD_LEN
);
3690 finalize_hash(pCryptHash
);
3691 memcpy(abHashValue
, pCryptHash
->abHashValue
, pCryptHash
->dwHashSize
);
3693 init_hash(pCryptHash
);
3694 update_hash(pCryptHash
, pad2
, RSAENH_HMAC_DEF_PAD_LEN
);
3695 finalize_hash(pCryptHash
);
3696 memcpy(abHashValue
+pCryptHash
->dwHashSize
, pCryptHash
->abHashValue
,
3697 pCryptHash
->dwHashSize
);
3699 memcpy(pCryptHash
->abHashValue
, old_hashval
, RSAENH_MAX_HASH_SIZE
);
3702 memcpy(pCryptKey
->abKeyValue
, abHashValue
,
3703 RSAENH_MIN(pCryptKey
->dwKeyLen
, sizeof(pCryptKey
->abKeyValue
)));
3706 case ALG_CLASS_MSG_ENCRYPT
:
3707 if (!lookup_handle(&handle_table
, pCryptHash
->hKey
, RSAENH_MAGIC_KEY
,
3708 (OBJECTHDR
**)&pMasterKey
))
3710 SetLastError(NTE_FAIL
); /* FIXME error code */
3716 /* See RFC 2246, chapter 6.3 Key calculation */
3717 case CALG_SCHANNEL_ENC_KEY
:
3718 *phKey
= new_key(hProv
, pMasterKey
->siSChannelInfo
.saEncAlg
.Algid
,
3719 MAKELONG(LOWORD(dwFlags
),pMasterKey
->siSChannelInfo
.saEncAlg
.cBits
),
3721 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
3722 memcpy(pCryptKey
->abKeyValue
,
3723 pCryptHash
->abHashValue
+ (
3724 2 * (pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
/ 8) +
3725 ((dwFlags
& CRYPT_SERVER
) ?
3726 (pMasterKey
->siSChannelInfo
.saEncAlg
.cBits
/ 8) : 0)),
3727 pMasterKey
->siSChannelInfo
.saEncAlg
.cBits
/ 8);
3728 memcpy(pCryptKey
->abInitVector
,
3729 pCryptHash
->abHashValue
+ (
3730 2 * (pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
/ 8) +
3731 2 * (pMasterKey
->siSChannelInfo
.saEncAlg
.cBits
/ 8) +
3732 ((dwFlags
& CRYPT_SERVER
) ? pCryptKey
->dwBlockLen
: 0)),
3733 pCryptKey
->dwBlockLen
);
3736 case CALG_SCHANNEL_MAC_KEY
:
3737 *phKey
= new_key(hProv
, Algid
,
3738 MAKELONG(LOWORD(dwFlags
),pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
),
3740 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
3741 memcpy(pCryptKey
->abKeyValue
,
3742 pCryptHash
->abHashValue
+ ((dwFlags
& CRYPT_SERVER
) ?
3743 pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
/ 8 : 0),
3744 pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
/ 8);
3748 SetLastError(NTE_BAD_ALGID
);
3754 SetLastError(NTE_BAD_ALGID
);
3758 setup_key(pCryptKey
);
3762 /******************************************************************************
3763 * CPGetUserKey (RSAENH.@)
3765 * Returns a handle to the user's private key-exchange- or signature-key.
3768 * hProv [I] The key container from which a user key is requested.
3769 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
3770 * phUserKey [O] Handle to the requested key or INVALID_HANDLE_VALUE in case of failure.
3777 * A newly created key container does not contain private user key. Create them with CPGenKey.
3779 BOOL WINAPI
RSAENH_CPGetUserKey(HCRYPTPROV hProv
, DWORD dwKeySpec
, HCRYPTKEY
*phUserKey
)
3781 KEYCONTAINER
*pKeyContainer
;
3783 TRACE("(hProv=%08lx, dwKeySpec=%08x, phUserKey=%p)\n", hProv
, dwKeySpec
, phUserKey
);
3785 if (!lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
,
3786 (OBJECTHDR
**)&pKeyContainer
))
3788 /* MSDN: hProv not containing valid context handle */
3789 SetLastError(NTE_BAD_UID
);
3795 case AT_KEYEXCHANGE
:
3796 copy_handle(&handle_table
, pKeyContainer
->hKeyExchangeKeyPair
, RSAENH_MAGIC_KEY
,
3801 copy_handle(&handle_table
, pKeyContainer
->hSignatureKeyPair
, RSAENH_MAGIC_KEY
,
3806 *phUserKey
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
3809 if (*phUserKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
3811 /* MSDN: dwKeySpec parameter specifies nonexistent key */
3812 SetLastError(NTE_NO_KEY
);
3819 /******************************************************************************
3820 * CPHashData (RSAENH.@)
3822 * Updates a hash object with the given data.
3825 * hProv [I] Key container to which the hash object belongs.
3826 * hHash [I] Hash object which is to be updated.
3827 * pbData [I] Pointer to data with which the hash object is to be updated.
3828 * dwDataLen [I] Length of the data.
3829 * dwFlags [I] Currently none defined.
3836 * The actual hash value is queried with CPGetHashParam, which will finalize
3837 * the hash. Updating a finalized hash will fail with a last error NTE_BAD_HASH_STATE.
3839 BOOL WINAPI
RSAENH_CPHashData(HCRYPTPROV hProv
, HCRYPTHASH hHash
, CONST BYTE
*pbData
,
3840 DWORD dwDataLen
, DWORD dwFlags
)
3842 CRYPTHASH
*pCryptHash
;
3844 TRACE("(hProv=%08lx, hHash=%08lx, pbData=%p, dwDataLen=%d, dwFlags=%08x)\n",
3845 hProv
, hHash
, pbData
, dwDataLen
, dwFlags
);
3849 SetLastError(NTE_BAD_FLAGS
);
3853 if (!lookup_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
,
3854 (OBJECTHDR
**)&pCryptHash
))
3856 SetLastError(NTE_BAD_HASH
);
3860 if (!get_algid_info(hProv
, pCryptHash
->aiAlgid
) || pCryptHash
->aiAlgid
== CALG_SSL3_SHAMD5
)
3862 SetLastError(NTE_BAD_ALGID
);
3866 if (pCryptHash
->dwState
!= RSAENH_HASHSTATE_HASHING
)
3868 SetLastError(NTE_BAD_HASH_STATE
);
3872 update_hash(pCryptHash
, pbData
, dwDataLen
);
3876 /******************************************************************************
3877 * CPHashSessionKey (RSAENH.@)
3879 * Updates a hash object with the binary representation of a symmetric key.
3882 * hProv [I] Key container to which the hash object belongs.
3883 * hHash [I] Hash object which is to be updated.
3884 * hKey [I] The symmetric key, whose binary value will be added to the hash.
3885 * dwFlags [I] CRYPT_LITTLE_ENDIAN, if the binary key value shall be interpreted as little endian.
3891 BOOL WINAPI
RSAENH_CPHashSessionKey(HCRYPTPROV hProv
, HCRYPTHASH hHash
, HCRYPTKEY hKey
,
3894 BYTE abKeyValue
[RSAENH_MAX_KEY_SIZE
], bTemp
;
3898 TRACE("(hProv=%08lx, hHash=%08lx, hKey=%08lx, dwFlags=%08x)\n", hProv
, hHash
, hKey
, dwFlags
);
3900 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pKey
) ||
3901 (GET_ALG_CLASS(pKey
->aiAlgid
) != ALG_CLASS_DATA_ENCRYPT
))
3903 SetLastError(NTE_BAD_KEY
);
3907 if (dwFlags
& ~CRYPT_LITTLE_ENDIAN
) {
3908 SetLastError(NTE_BAD_FLAGS
);
3912 memcpy(abKeyValue
, pKey
->abKeyValue
, pKey
->dwKeyLen
);
3913 if (!(dwFlags
& CRYPT_LITTLE_ENDIAN
)) {
3914 for (i
=0; i
<pKey
->dwKeyLen
/2; i
++) {
3915 bTemp
= abKeyValue
[i
];
3916 abKeyValue
[i
] = abKeyValue
[pKey
->dwKeyLen
-i
-1];
3917 abKeyValue
[pKey
->dwKeyLen
-i
-1] = bTemp
;
3921 return RSAENH_CPHashData(hProv
, hHash
, abKeyValue
, pKey
->dwKeyLen
, 0);
3924 /******************************************************************************
3925 * CPReleaseContext (RSAENH.@)
3927 * Release a key container.
3930 * hProv [I] Key container to be released.
3931 * dwFlags [I] Currently none defined.
3937 BOOL WINAPI
RSAENH_CPReleaseContext(HCRYPTPROV hProv
, DWORD dwFlags
)
3939 TRACE("(hProv=%08lx, dwFlags=%08x)\n", hProv
, dwFlags
);
3941 if (!release_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
3943 /* MSDN: hProv not containing valid context handle */
3944 SetLastError(NTE_BAD_UID
);
3949 SetLastError(NTE_BAD_FLAGS
);
3956 /******************************************************************************
3957 * CPSetHashParam (RSAENH.@)
3959 * Set a parameter of a hash object
3962 * hProv [I] The key container to which the key belongs.
3963 * hHash [I] The hash object for which a parameter is to be set.
3964 * dwParam [I] Parameter type. See Notes.
3965 * pbData [I] Pointer to the parameter value.
3966 * dwFlags [I] Currently none defined.
3973 * Currently only the HP_HMAC_INFO dwParam type is defined.
3974 * The HMAC_INFO struct will be deep copied into the hash object.
3975 * See Internet RFC 2104 for details on the HMAC algorithm.
3977 BOOL WINAPI
RSAENH_CPSetHashParam(HCRYPTPROV hProv
, HCRYPTHASH hHash
, DWORD dwParam
,
3978 BYTE
*pbData
, DWORD dwFlags
)
3980 CRYPTHASH
*pCryptHash
;
3981 CRYPTKEY
*pCryptKey
;
3984 TRACE("(hProv=%08lx, hHash=%08lx, dwParam=%08x, pbData=%p, dwFlags=%08x)\n",
3985 hProv
, hHash
, dwParam
, pbData
, dwFlags
);
3987 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
3989 SetLastError(NTE_BAD_UID
);
3994 SetLastError(NTE_BAD_FLAGS
);
3998 if (!lookup_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
,
3999 (OBJECTHDR
**)&pCryptHash
))
4001 SetLastError(NTE_BAD_HASH
);
4007 free_hmac_info(pCryptHash
->pHMACInfo
);
4008 if (!copy_hmac_info(&pCryptHash
->pHMACInfo
, (PHMAC_INFO
)pbData
)) return FALSE
;
4010 if (!lookup_handle(&handle_table
, pCryptHash
->hKey
, RSAENH_MAGIC_KEY
,
4011 (OBJECTHDR
**)&pCryptKey
))
4013 SetLastError(NTE_FAIL
); /* FIXME: correct error code? */
4017 for (i
=0; i
<RSAENH_MIN(pCryptKey
->dwKeyLen
,pCryptHash
->pHMACInfo
->cbInnerString
); i
++) {
4018 pCryptHash
->pHMACInfo
->pbInnerString
[i
] ^= pCryptKey
->abKeyValue
[i
];
4020 for (i
=0; i
<RSAENH_MIN(pCryptKey
->dwKeyLen
,pCryptHash
->pHMACInfo
->cbOuterString
); i
++) {
4021 pCryptHash
->pHMACInfo
->pbOuterString
[i
] ^= pCryptKey
->abKeyValue
[i
];
4024 init_hash(pCryptHash
);
4028 memcpy(pCryptHash
->abHashValue
, pbData
, pCryptHash
->dwHashSize
);
4029 pCryptHash
->dwState
= RSAENH_HASHSTATE_FINISHED
;
4032 case HP_TLS1PRF_SEED
:
4033 return copy_data_blob(&pCryptHash
->tpPRFParams
.blobSeed
, (PCRYPT_DATA_BLOB
)pbData
);
4035 case HP_TLS1PRF_LABEL
:
4036 return copy_data_blob(&pCryptHash
->tpPRFParams
.blobLabel
, (PCRYPT_DATA_BLOB
)pbData
);
4039 SetLastError(NTE_BAD_TYPE
);
4044 /******************************************************************************
4045 * CPSetProvParam (RSAENH.@)
4047 BOOL WINAPI
RSAENH_CPSetProvParam(HCRYPTPROV hProv
, DWORD dwParam
, BYTE
*pbData
, DWORD dwFlags
)
4053 /******************************************************************************
4054 * CPSignHash (RSAENH.@)
4056 * Sign a hash object
4059 * hProv [I] The key container, to which the hash object belongs.
4060 * hHash [I] The hash object to be signed.
4061 * dwKeySpec [I] AT_SIGNATURE or AT_KEYEXCHANGE: Key used to generate the signature.
4062 * sDescription [I] Should be NULL for security reasons.
4063 * dwFlags [I] 0, CRYPT_NOHASHOID or CRYPT_X931_FORMAT: Format of the signature.
4064 * pbSignature [O] Buffer, to which the signature will be stored. May be NULL to query SigLen.
4065 * pdwSigLen [I/O] Size of the buffer (in), Length of the signature (out)
4071 BOOL WINAPI
RSAENH_CPSignHash(HCRYPTPROV hProv
, HCRYPTHASH hHash
, DWORD dwKeySpec
,
4072 LPCWSTR sDescription
, DWORD dwFlags
, BYTE
*pbSignature
,
4075 HCRYPTKEY hCryptKey
;
4076 CRYPTKEY
*pCryptKey
;
4078 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
];
4081 TRACE("(hProv=%08lx, hHash=%08lx, dwKeySpec=%08x, sDescription=%s, dwFlags=%08x, "
4082 "pbSignature=%p, pdwSigLen=%p)\n", hProv
, hHash
, dwKeySpec
, debugstr_w(sDescription
),
4083 dwFlags
, pbSignature
, pdwSigLen
);
4085 if (dwFlags
& ~(CRYPT_NOHASHOID
|CRYPT_X931_FORMAT
)) {
4086 SetLastError(NTE_BAD_FLAGS
);
4090 if (!RSAENH_CPGetUserKey(hProv
, dwKeySpec
, &hCryptKey
)) return FALSE
;
4092 if (!lookup_handle(&handle_table
, hCryptKey
, RSAENH_MAGIC_KEY
,
4093 (OBJECTHDR
**)&pCryptKey
))
4095 SetLastError(NTE_NO_KEY
);
4100 *pdwSigLen
= pCryptKey
->dwKeyLen
;
4103 if (pCryptKey
->dwKeyLen
> *pdwSigLen
)
4105 SetLastError(ERROR_MORE_DATA
);
4106 *pdwSigLen
= pCryptKey
->dwKeyLen
;
4109 *pdwSigLen
= pCryptKey
->dwKeyLen
;
4112 if (!RSAENH_CPHashData(hProv
, hHash
, (CONST BYTE
*)sDescription
,
4113 (DWORD
)lstrlenW(sDescription
)*sizeof(WCHAR
), 0))
4119 dwHashLen
= sizeof(DWORD
);
4120 if (!RSAENH_CPGetHashParam(hProv
, hHash
, HP_ALGID
, (BYTE
*)&aiAlgid
, &dwHashLen
, 0)) return FALSE
;
4122 dwHashLen
= RSAENH_MAX_HASH_SIZE
;
4123 if (!RSAENH_CPGetHashParam(hProv
, hHash
, HP_HASHVAL
, abHashValue
, &dwHashLen
, 0)) return FALSE
;
4126 if (!build_hash_signature(pbSignature
, *pdwSigLen
, aiAlgid
, abHashValue
, dwHashLen
, dwFlags
)) {
4130 return encrypt_block_impl(pCryptKey
->aiAlgid
, PK_PRIVATE
, &pCryptKey
->context
, pbSignature
, pbSignature
, RSAENH_ENCRYPT
);
4133 /******************************************************************************
4134 * CPVerifySignature (RSAENH.@)
4136 * Verify the signature of a hash object.
4139 * hProv [I] The key container, to which the hash belongs.
4140 * hHash [I] The hash for which the signature is verified.
4141 * pbSignature [I] The binary signature.
4142 * dwSigLen [I] Length of the signature BLOB.
4143 * hPubKey [I] Public key used to verify the signature.
4144 * sDescription [I] Should be NULL for security reasons.
4145 * dwFlags [I] 0, CRYPT_NOHASHOID or CRYPT_X931_FORMAT: Format of the signature.
4148 * Success: TRUE (Signature is valid)
4149 * Failure: FALSE (GetLastError() == NTE_BAD_SIGNATURE, if signature is invalid)
4151 BOOL WINAPI
RSAENH_CPVerifySignature(HCRYPTPROV hProv
, HCRYPTHASH hHash
, CONST BYTE
*pbSignature
,
4152 DWORD dwSigLen
, HCRYPTKEY hPubKey
, LPCWSTR sDescription
,
4155 BYTE
*pbConstructed
= NULL
, *pbDecrypted
= NULL
;
4156 CRYPTKEY
*pCryptKey
;
4159 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
];
4162 TRACE("(hProv=%08lx, hHash=%08lx, pbSignature=%p, dwSigLen=%d, hPubKey=%08lx, sDescription=%s, "
4163 "dwFlags=%08x)\n", hProv
, hHash
, pbSignature
, dwSigLen
, hPubKey
, debugstr_w(sDescription
),
4166 if (dwFlags
& ~(CRYPT_NOHASHOID
|CRYPT_X931_FORMAT
)) {
4167 SetLastError(NTE_BAD_FLAGS
);
4171 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
4173 SetLastError(NTE_BAD_UID
);
4177 if (!lookup_handle(&handle_table
, hPubKey
, RSAENH_MAGIC_KEY
,
4178 (OBJECTHDR
**)&pCryptKey
))
4180 SetLastError(NTE_BAD_KEY
);
4184 /* in Microsoft implementation, the signature length is checked before
4185 * the signature pointer.
4187 if (dwSigLen
!= pCryptKey
->dwKeyLen
)
4189 SetLastError(NTE_BAD_SIGNATURE
);
4193 if (!hHash
|| !pbSignature
)
4195 SetLastError(ERROR_INVALID_PARAMETER
);
4200 if (!RSAENH_CPHashData(hProv
, hHash
, (CONST BYTE
*)sDescription
,
4201 (DWORD
)lstrlenW(sDescription
)*sizeof(WCHAR
), 0))
4207 dwHashLen
= sizeof(DWORD
);
4208 if (!RSAENH_CPGetHashParam(hProv
, hHash
, HP_ALGID
, (BYTE
*)&aiAlgid
, &dwHashLen
, 0)) return FALSE
;
4210 dwHashLen
= RSAENH_MAX_HASH_SIZE
;
4211 if (!RSAENH_CPGetHashParam(hProv
, hHash
, HP_HASHVAL
, abHashValue
, &dwHashLen
, 0)) return FALSE
;
4213 pbConstructed
= HeapAlloc(GetProcessHeap(), 0, dwSigLen
);
4214 if (!pbConstructed
) {
4215 SetLastError(NTE_NO_MEMORY
);
4219 pbDecrypted
= HeapAlloc(GetProcessHeap(), 0, dwSigLen
);
4221 SetLastError(NTE_NO_MEMORY
);
4225 if (!encrypt_block_impl(pCryptKey
->aiAlgid
, PK_PUBLIC
, &pCryptKey
->context
, pbSignature
, pbDecrypted
,
4231 if (!build_hash_signature(pbConstructed
, dwSigLen
, aiAlgid
, abHashValue
, dwHashLen
, dwFlags
)) {
4235 if (memcmp(pbDecrypted
, pbConstructed
, dwSigLen
)) {
4236 SetLastError(NTE_BAD_SIGNATURE
);
4242 HeapFree(GetProcessHeap(), 0, pbConstructed
);
4243 HeapFree(GetProcessHeap(), 0, pbDecrypted
);
4247 static const WCHAR szProviderKeys
[6][116] = {
4248 { 'S','o','f','t','w','a','r','e','\\',
4249 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
4250 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
4251 'i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ','B','a','s',
4252 'e',' ','C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r',
4253 'o','v','i','d','e','r',' ','v','1','.','0',0 },
4254 { 'S','o','f','t','w','a','r','e','\\',
4255 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
4256 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
4257 'i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ',
4258 'E','n','h','a','n','c','e','d',
4259 ' ','C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r',
4260 'o','v','i','d','e','r',' ','v','1','.','0',0 },
4261 { 'S','o','f','t','w','a','r','e','\\',
4262 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
4263 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
4264 'i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ','S','t','r','o','n','g',
4265 ' ','C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r',
4266 'o','v','i','d','e','r',0 },
4267 { 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
4268 'C','r','y','p','t','o','g','r','a','p','h','y','\\','D','e','f','a','u','l','t','s','\\',
4269 'P','r','o','v','i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ',
4270 'R','S','A',' ','S','C','h','a','n','n','e','l',' ',
4271 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r','o','v','i','d','e','r',0 },
4272 { 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
4273 'C','r','y','p','t','o','g','r','a','p','h','y','\\','D','e','f','a','u','l','t','s','\\',
4274 'P','r','o','v','i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ',
4275 'E','n','h','a','n','c','e','d',' ','R','S','A',' ','a','n','d',' ','A','E','S',' ',
4276 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r','o','v','i','d','e','r',0 },
4277 { 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
4278 'C','r','y','p','t','o','g','r','a','p','h','y','\\','D','e','f','a','u','l','t','s','\\',
4279 'P','r','o','v','i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ',
4280 'E','n','h','a','n','c','e','d',' ','R','S','A',' ','a','n','d',' ','A','E','S',' ',
4281 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r','o','v','i','d','e','r',
4282 ' ','(','P','r','o','t','o','t','y','p','e',')',0 }
4284 static const WCHAR szDefaultKeys
[3][65] = {
4285 { 'S','o','f','t','w','a','r','e','\\',
4286 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
4287 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
4288 'i','d','e','r',' ','T','y','p','e','s','\\','T','y','p','e',' ','0','0','1',0 },
4289 { 'S','o','f','t','w','a','r','e','\\',
4290 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
4291 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
4292 'i','d','e','r',' ','T','y','p','e','s','\\','T','y','p','e',' ','0','1','2',0 },
4293 { 'S','o','f','t','w','a','r','e','\\',
4294 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
4295 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
4296 'i','d','e','r',' ','T','y','p','e','s','\\','T','y','p','e',' ','0','2','4',0 }
4300 /******************************************************************************
4301 * DllRegisterServer (RSAENH.@)
4303 * Dll self registration.
4312 * Registers the following keys:
4313 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider\
4314 * Microsoft Base Cryptographic Provider v1.0
4315 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider\
4316 * Microsoft Enhanced Cryptographic Provider
4317 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider\
4318 * Microsoft Strong Cryptographpic Provider
4319 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider Types\Type 001
4321 HRESULT WINAPI
DllRegisterServer(void)
4328 for (i
=0; i
<6; i
++) {
4329 apiRet
= RegCreateKeyExW(HKEY_LOCAL_MACHINE
, szProviderKeys
[i
], 0, NULL
,
4330 REG_OPTION_NON_VOLATILE
, KEY_ALL_ACCESS
, NULL
, &key
, &dp
);
4332 if (apiRet
== ERROR_SUCCESS
)
4334 if (dp
== REG_CREATED_NEW_KEY
)
4336 static const WCHAR szImagePath
[] = { 'I','m','a','g','e',' ','P','a','t','h',0 };
4337 static const WCHAR szRSABase
[] = { 'r','s','a','e','n','h','.','d','l','l',0 };
4338 static const WCHAR szType
[] = { 'T','y','p','e',0 };
4339 static const WCHAR szSignature
[] = { 'S','i','g','n','a','t','u','r','e',0 };
4345 type
=PROV_RSA_SCHANNEL
;
4356 RegSetValueExW(key
, szImagePath
, 0, REG_SZ
, (const BYTE
*)szRSABase
,
4357 (lstrlenW(szRSABase
) + 1) * sizeof(WCHAR
));
4358 RegSetValueExW(key
, szType
, 0, REG_DWORD
, (LPBYTE
)&type
, sizeof(type
));
4359 RegSetValueExW(key
, szSignature
, 0, REG_BINARY
, (LPBYTE
)&sign
, sizeof(sign
));
4365 for (i
=0; i
<3; i
++) {
4366 apiRet
= RegCreateKeyExW(HKEY_LOCAL_MACHINE
, szDefaultKeys
[i
], 0, NULL
,
4367 REG_OPTION_NON_VOLATILE
, KEY_ALL_ACCESS
, NULL
, &key
, &dp
);
4368 if (apiRet
== ERROR_SUCCESS
)
4370 if (dp
== REG_CREATED_NEW_KEY
)
4372 static const WCHAR szName
[] = { 'N','a','m','e',0 };
4373 static const WCHAR szRSAName
[3][54] = {
4374 { 'M','i','c','r','o','s','o','f','t',' ', 'B','a','s','e',' ',
4375 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ',
4376 'P','r','o','v','i','d','e','r',' ','v','1','.','0',0 },
4377 { 'M','i','c','r','o','s','o','f','t',' ','R','S','A',' ',
4378 'S','C','h','a','n','n','e','l',' ',
4379 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ',
4380 'P','r','o','v','i','d','e','r',0 },
4381 { 'M','i','c','r','o','s','o','f','t',' ','E','n','h','a','n','c','e','d',' ',
4382 'R','S','A',' ','a','n','d',' ','A','E','S',' ',
4383 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ',
4384 'P','r','o','v','i','d','e','r',0 } };
4385 static const WCHAR szTypeName
[] = { 'T','y','p','e','N','a','m','e',0 };
4386 static const WCHAR szRSATypeName
[3][38] = {
4387 { 'R','S','A',' ','F','u','l','l',' ',
4388 '(','S','i','g','n','a','t','u','r','e',' ','a','n','d',' ',
4389 'K','e','y',' ','E','x','c','h','a','n','g','e',')',0 },
4390 { 'R','S','A',' ','S','C','h','a','n','n','e','l',0 },
4391 { 'R','S','A',' ','F','u','l','l',' ','a','n','d',' ','A','E','S',0 } };
4393 RegSetValueExW(key
, szName
, 0, REG_SZ
,
4394 (const BYTE
*)szRSAName
[i
], lstrlenW(szRSAName
[i
])*sizeof(WCHAR
)+sizeof(WCHAR
));
4395 RegSetValueExW(key
, szTypeName
, 0, REG_SZ
,
4396 (const BYTE
*)szRSATypeName
[i
], lstrlenW(szRSATypeName
[i
])*sizeof(WCHAR
)+sizeof(WCHAR
));
4402 return HRESULT_FROM_WIN32(apiRet
);
4405 /******************************************************************************
4406 * DllUnregisterServer (RSAENH.@)
4408 * Dll self unregistration.
4416 * For the relevant keys see DllRegisterServer.
4418 HRESULT WINAPI
DllUnregisterServer(void)
4420 RegDeleteKeyW(HKEY_LOCAL_MACHINE
, szProviderKeys
[0]);
4421 RegDeleteKeyW(HKEY_LOCAL_MACHINE
, szProviderKeys
[1]);
4422 RegDeleteKeyW(HKEY_LOCAL_MACHINE
, szProviderKeys
[2]);
4423 RegDeleteKeyW(HKEY_LOCAL_MACHINE
, szProviderKeys
[3]);
4424 RegDeleteKeyW(HKEY_LOCAL_MACHINE
, szProviderKeys
[4]);
4425 RegDeleteKeyW(HKEY_LOCAL_MACHINE
, szProviderKeys
[5]);
4426 RegDeleteKeyW(HKEY_LOCAL_MACHINE
, szDefaultKeys
[0]);
4427 RegDeleteKeyW(HKEY_LOCAL_MACHINE
, szDefaultKeys
[1]);
4428 RegDeleteKeyW(HKEY_LOCAL_MACHINE
, szDefaultKeys
[2]);