1 /***********************************************************************\
2 *                               wincrypt.d                              *
3 *                                                                       *
4 *                       Windows API header module                       *
5 *                                                                       *
6 *                 Translated from MinGW Windows headers                 *
7 *                           by Stewart Gordon                           *
8 *                                                                       *
9 *                       Placed into public domain                       *
10 \***********************************************************************/
11 module win32.wincrypt;
12 
13 private import win32.w32api, win32.winbase, win32.windef;
14 
15 /* FIXME:
16  *	Types of some constants
17  *	Types of macros
18  *	Inits of various "size" and "version" members
19  *	Why are some #ifdefs commented out?
20  */
21 
22 const TCHAR[]
23 	MS_DEF_PROV = "Microsoft Base Cryptographic Provider v1.0",
24 	MS_ENHANCED_PROV = "Microsoft Enhanced Cryptographic Provider v1.0",
25 	MS_STRONG_PROV = "Microsoft Strong Cryptographic Provider",
26 	MS_DEF_RSA_SIG_PROV = "Microsoft RSA Signature Cryptographic Provider",
27 	MS_DEF_RSA_SCHANNEL_PROV = "Microsoft RSA SChannel Cryptographic Provider",
28 	MS_DEF_DSS_PROV = "Microsoft Base DSS Cryptographic Provider",
29 	MS_DEF_DSS_DH_PROV
30 	  = "Microsoft Base DSS and Diffie-Hellman Cryptographic Provider",
31 	MS_ENH_DSS_DH_PROV
32 	  = "Microsoft Enhanced DSS and Diffie-Hellman Cryptographic Provider",
33 	MS_DEF_DH_SCHANNEL_PROV = "Microsoft DH SChannel Cryptographic Provider",
34 	MS_SCARD_PROV = "Microsoft Base Smart Card Crypto Provider";
35 
36 static if (WINVER > 0x501) {
37 	const TCHAR[] MS_ENH_RSA_AES_PROV
38 	  = "Microsoft Enhanced RSA and AES Cryptographic Provider";
39 } else static if (WINVER == 0x501) {
40 	const TCHAR[] MS_ENH_RSA_AES_PROV
41 	  = "Microsoft Enhanced RSA and AES Cryptographic Provider (Prototype)";
42 }
43 
44 ALG_ID GET_ALG_CLASS(ALG_ID x) { return x & 0xE000; }
45 ALG_ID GET_ALG_TYPE (ALG_ID x) { return x & 0x1E00; }
46 ALG_ID GET_ALG_SID  (ALG_ID x) { return x & 0x01FF; }
47 
48 enum : ALG_ID {
49 	ALG_CLASS_ANY           = 0,
50 	ALG_CLASS_SIGNATURE     = 0x2000,
51 	ALG_CLASS_MSG_ENCRYPT   = 0x4000,
52 	ALG_CLASS_DATA_ENCRYPT  = 0x6000,
53 	ALG_CLASS_HASH          = 0x8000,
54 	ALG_CLASS_KEY_EXCHANGE  = 0xA000,
55 	ALG_CLASS_ALL           = 0xE000
56 }
57 
58 enum : ALG_ID {
59 	ALG_TYPE_ANY           = 0,
60 	ALG_TYPE_DSS           = 0x0200,
61 	ALG_TYPE_RSA           = 0x0400,
62 	ALG_TYPE_BLOCK         = 0x0600,
63 	ALG_TYPE_STREAM        = 0x0800,
64 	ALG_TYPE_DH            = 0x0A00,
65 	ALG_TYPE_SECURECHANNEL = 0x0C00
66 }
67 
68 enum : ALG_ID {
69 	ALG_SID_ANY          =  0,
70 	ALG_SID_RSA_ANY      =  0,
71 	ALG_SID_RSA_PKCS,
72 	ALG_SID_RSA_MSATWORK,
73 	ALG_SID_RSA_ENTRUST,
74 	ALG_SID_RSA_PGP,  // =  4
75 	ALG_SID_DSS_ANY      =  0,
76 	ALG_SID_DSS_PKCS,
77 	ALG_SID_DSS_DMS,  // =  2
78 	ALG_SID_DES          =  1,
79 	ALG_SID_3DES         =  3,
80 	ALG_SID_DESX,
81 	ALG_SID_IDEA,
82 	ALG_SID_CAST,
83 	ALG_SID_SAFERSK64,
84 	ALG_SID_SAFERSK128,
85 	ALG_SID_3DES_112,
86 	ALG_SID_SKIPJACK,
87 	ALG_SID_TEK,
88 	ALG_SID_CYLINK_MEK,
89 	ALG_SID_RC5,      // = 13
90 	ALG_SID_RC2          =  2,
91 	ALG_SID_RC4          =  1,
92 	ALG_SID_SEAL         =  2,
93 	ALG_SID_MD2          =  1,
94 	ALG_SID_MD4,
95 	ALG_SID_MD5,
96 	ALG_SID_SHA,
97 	ALG_SID_MAC,
98 	ALG_SID_RIPEMD,
99 	ALG_SID_RIPEMD160,
100 	ALG_SID_SSL3SHAMD5,
101 	ALG_SID_HMAC,
102 	ALG_SID_TLS1PRF,  // = 10
103 	ALG_SID_AES_128      = 14,
104 	ALG_SID_AES_192,
105 	ALG_SID_AES_256,
106 	ALG_SID_AES,      // = 17
107 	ALG_SID_EXAMPLE      = 80
108 }
109 
110 enum : ALG_ID {
111 	CALG_MD2        = ALG_CLASS_HASH | ALG_TYPE_ANY | ALG_SID_MD2,
112 	CALG_MD4        = ALG_CLASS_HASH | ALG_TYPE_ANY | ALG_SID_MD4,
113 	CALG_MD5        = ALG_CLASS_HASH | ALG_TYPE_ANY | ALG_SID_MD5,
114 	CALG_SHA        = ALG_CLASS_HASH | ALG_TYPE_ANY | ALG_SID_SHA,
115 	CALG_SHA1       = CALG_SHA,
116 	CALG_MAC        = ALG_CLASS_HASH | ALG_TYPE_ANY | ALG_SID_MAC,
117 	CALG_3DES       = ALG_CLASS_DATA_ENCRYPT | ALG_TYPE_BLOCK | 3,
118 	CALG_CYLINK_MEK = ALG_CLASS_DATA_ENCRYPT | ALG_TYPE_BLOCK | 12,
119 	CALG_SKIPJACK   = ALG_CLASS_DATA_ENCRYPT | ALG_TYPE_BLOCK | 10,
120 	CALG_KEA_KEYX   = ALG_CLASS_KEY_EXCHANGE | ALG_TYPE_STREAM | ALG_TYPE_DSS | 4,
121 	CALG_RSA_SIGN   = ALG_CLASS_SIGNATURE | ALG_TYPE_RSA | ALG_SID_RSA_ANY,
122 	CALG_DSS_SIGN   = ALG_CLASS_SIGNATURE | ALG_TYPE_DSS | ALG_SID_DSS_ANY,
123 	CALG_RSA_KEYX   = ALG_CLASS_KEY_EXCHANGE | ALG_TYPE_RSA | ALG_SID_RSA_ANY,
124 	CALG_DES        = ALG_CLASS_DATA_ENCRYPT | ALG_TYPE_BLOCK | ALG_SID_DES,
125 	CALG_RC2        = ALG_CLASS_DATA_ENCRYPT | ALG_TYPE_BLOCK | ALG_SID_RC2,
126 	CALG_RC4        = ALG_CLASS_DATA_ENCRYPT | ALG_TYPE_STREAM | ALG_SID_RC4,
127 	CALG_SEAL       = ALG_CLASS_DATA_ENCRYPT | ALG_TYPE_STREAM | ALG_SID_SEAL,
128 	CALG_DH_EPHEM   = ALG_CLASS_KEY_EXCHANGE | ALG_TYPE_STREAM | ALG_TYPE_DSS
129 	                  | ALG_SID_DSS_DMS,
130 	CALG_DESX       = ALG_CLASS_DATA_ENCRYPT | ALG_TYPE_BLOCK | ALG_SID_DESX,
131 // is undefined ALG_CLASS_DHASH in MinGW - presuming typo
132 	CALG_TLS1PRF    = ALG_CLASS_HASH | ALG_TYPE_ANY | ALG_SID_TLS1PRF,
133 	CALG_AES_128    = ALG_CLASS_DATA_ENCRYPT | ALG_TYPE_BLOCK | ALG_SID_AES_128,
134 	CALG_AES_192    = ALG_CLASS_DATA_ENCRYPT | ALG_TYPE_BLOCK | ALG_SID_AES_192,
135 	CALG_AES_256    = ALG_CLASS_DATA_ENCRYPT | ALG_TYPE_BLOCK | ALG_SID_AES_256,
136 	CALG_AES        = ALG_CLASS_DATA_ENCRYPT | ALG_TYPE_BLOCK | ALG_SID_AES,
137 }
138 
139 /+
140 #define CRYPT_VERIFYCONTEXT 0xF0000000
141 #define CRYPT_NEWKEYSET 8
142 #define CRYPT_DELETEKEYSET 16
143 #define CRYPT_MACHINE_KEYSET 32
144 #define CRYPT_SILENT 64
145 #define CRYPT_EXPORTABLE 1
146 #define CRYPT_USER_PROTECTED 2
147 #define CRYPT_CREATE_SALT 4
148 #define CRYPT_UPDATE_KEY 8
149 #define SIMPLEBLOB 1
150 #define PUBLICKEYBLOB 6
151 #define PRIVATEKEYBLOB 7
152 #define PLAINTEXTKEYBLOB 8
153 #define OPAQUEKEYBLOB 9
154 #define PUBLICKEYBLOBEX 10
155 #define SYMMETRICWRAPKEYBLOB 11
156 #define AT_KEYEXCHANGE 1
157 #define AT_SIGNATURE 2
158 #define CRYPT_USERDATA 1
159 #define PKCS5_PADDING 1
160 #define CRYPT_MODE_CBC 1
161 #define CRYPT_MODE_ECB 2
162 #define CRYPT_MODE_OFB 3
163 #define CRYPT_MODE_CFB 4
164 #define CRYPT_MODE_CTS 5
165 #define CRYPT_MODE_CBCI 6
166 #define CRYPT_MODE_CFBP 7
167 #define CRYPT_MODE_OFBP 8
168 #define CRYPT_MODE_CBCOFM 9
169 #define CRYPT_MODE_CBCOFMI 10
170 #define CRYPT_ENCRYPT 1
171 #define CRYPT_DECRYPT 2
172 #define CRYPT_EXPORT 4
173 #define CRYPT_READ 8
174 #define CRYPT_WRITE 16
175 #define CRYPT_MAC 32
176 #define HP_ALGID 1
177 #define HP_HASHVAL 2
178 #define HP_HASHSIZE 4
179 #define HP_HMAC_INFO 5
180 #define CRYPT_FAILED FALSE
181 #define CRYPT_SUCCEED TRUE
182 #define RCRYPT_SUCCEEDED(r) ((r)==CRYPT_SUCCEED)
183 #define RCRYPT_FAILED(r) ((r)==CRYPT_FAILED)
184 #define PP_ENUMALGS 1
185 #define PP_ENUMCONTAINERS 2
186 #define PP_IMPTYPE 3
187 #define PP_NAME 4
188 #define PP_VERSION 5
189 #define PP_CONTAINER 6
190 #define PP_CHANGE_PASSWORD	7
191 #define PP_KEYSET_SEC_DESCR	8
192 #define PP_CERTCHAIN	9
193 #define PP_KEY_TYPE_SUBTYPE	10
194 #define PP_PROVTYPE	16
195 #define PP_KEYSTORAGE	17
196 #define PP_APPLI_CERT	18
197 #define PP_SYM_KEYSIZE	19
198 #define PP_SESSION_KEYSIZE	20
199 #define PP_UI_PROMPT	21
200 #define PP_ENUMALGS_EX	22
201 #define PP_ENUMMANDROOTS 25
202 #define PP_ENUMELECTROOTS 26
203 #define PP_KEYSET_TYPE 27
204 #define PP_ADMIN_PIN 31
205 #define PP_KEYEXCHANGE_PIN 32
206 #define PP_SIGNATURE_PIN 33
207 #define PP_SIG_KEYSIZE_INC 34
208 #define PP_KEYX_KEYSIZE_INC 35
209 #define PP_UNIQUE_CONTAINER 36
210 #define PP_SGC_INFO 37
211 #define PP_USE_HARDWARE_RNG 38
212 #define PP_KEYSPEC 39
213 #define PP_ENUMEX_SIGNING_PROT 40
214 #define CRYPT_FIRST 1
215 #define CRYPT_NEXT 2
216 #define CRYPT_IMPL_HARDWARE 1
217 #define CRYPT_IMPL_SOFTWARE 2
218 #define CRYPT_IMPL_MIXED 3
219 #define CRYPT_IMPL_UNKNOWN 4
220 #define PROV_RSA_FULL 1
221 #define PROV_RSA_SIG 2
222 #define PROV_DSS 3
223 #define PROV_FORTEZZA 4
224 #define PROV_MS_MAIL 5
225 #define PROV_SSL 6
226 #define PROV_STT_MER 7
227 #define PROV_STT_ACQ 8
228 #define PROV_STT_BRND 9
229 #define PROV_STT_ROOT 10
230 #define PROV_STT_ISS 11
231 #define PROV_RSA_SCHANNEL 12
232 #define PROV_DSS_DH 13
233 #define PROV_EC_ECDSA_SIG 14
234 #define PROV_EC_ECNRA_SIG 15
235 #define PROV_EC_ECDSA_FULL 16
236 #define PROV_EC_ECNRA_FULL 17
237 #define PROV_DH_SCHANNEL 18
238 #define PROV_SPYRUS_LYNKS 20
239 #define PROV_RNG 21
240 #define PROV_INTEL_SEC 22
241 #define PROV_RSA_AES 24
242 #define MAXUIDLEN 64
243 #define CUR_BLOB_VERSION 2
244 #define X509_ASN_ENCODING 1
245 #define PKCS_7_ASN_ENCODING  65536
246 #define CERT_V1 0
247 #define CERT_V2 1
248 #define CERT_V3 2
249 #define CERT_E_CHAINING (-2146762486)
250 #define CERT_E_CN_NO_MATCH (-2146762481)
251 #define CERT_E_EXPIRED (-2146762495)
252 #define CERT_E_PURPOSE (-2146762490)
253 #define CERT_E_REVOCATION_FAILURE (-2146762482)
254 #define CERT_E_REVOKED (-2146762484)
255 #define CERT_E_ROLE (-2146762493)
256 #define CERT_E_UNTRUSTEDROOT (-2146762487)
257 #define CERT_E_UNTRUSTEDTESTROOT (-2146762483)
258 #define CERT_E_VALIDITYPERIODNESTING (-2146762494)
259 #define CERT_E_WRONG_USAGE (-2146762480)
260 #define CERT_E_PATHLENCONST (-2146762492)
261 #define CERT_E_CRITICAL (-2146762491)
262 #define CERT_E_ISSUERCHAINING (-2146762489)
263 #define CERT_E_MALFORMED (-2146762488)
264 #define CRYPT_E_REVOCATION_OFFLINE (-2146885613)
265 #define CRYPT_E_REVOKED (-2146885616)
266 #define TRUST_E_BASIC_CONSTRAINTS (-2146869223)
267 #define TRUST_E_CERT_SIGNATURE (-2146869244)
268 #define TRUST_E_FAIL (-2146762485)
269 #define CERT_TRUST_NO_ERROR 0
270 #define CERT_TRUST_IS_NOT_TIME_VALID 1
271 #define CERT_TRUST_IS_NOT_TIME_NESTED 2
272 #define CERT_TRUST_IS_REVOKED 4
273 #define CERT_TRUST_IS_NOT_SIGNATURE_VALID 8
274 #define CERT_TRUST_IS_NOT_VALID_FOR_USAGE 16
275 #define CERT_TRUST_IS_UNTRUSTED_ROOT 32
276 #define CERT_TRUST_REVOCATION_STATUS_UNKNOWN 64
277 #define CERT_TRUST_IS_CYCLIC 128
278 #define CERT_TRUST_IS_PARTIAL_CHAIN 65536
279 #define CERT_TRUST_CTL_IS_NOT_TIME_VALID 131072
280 #define CERT_TRUST_CTL_IS_NOT_SIGNATURE_VALID 262144
281 #define CERT_TRUST_CTL_IS_NOT_VALID_FOR_USAGE 524288
282 #define CERT_TRUST_HAS_EXACT_MATCH_ISSUER 1
283 #define CERT_TRUST_HAS_KEY_MATCH_ISSUER 2
284 #define CERT_TRUST_HAS_NAME_MATCH_ISSUER 4
285 #define CERT_TRUST_IS_SELF_SIGNED 8
286 #define CERT_TRUST_IS_COMPLEX_CHAIN 65536
287 #define CERT_CHAIN_POLICY_BASE ((LPCSTR) 1)
288 #define CERT_CHAIN_POLICY_AUTHENTICODE  ((LPCSTR) 2)
289 #define CERT_CHAIN_POLICY_AUTHENTICODE_TS  ((LPCSTR) 3)
290 #define CERT_CHAIN_POLICY_SSL  ((LPCSTR) 4)
291 #define CERT_CHAIN_POLICY_BASIC_CONSTRAINTS ((LPCSTR) 5)
292 #define CERT_CHAIN_POLICY_NT_AUTH ((LPCSTR) 6)
293 #define USAGE_MATCH_TYPE_AND 0
294 #define USAGE_MATCH_TYPE_OR 1
295 #define CERT_SIMPLE_NAME_STR 1
296 #define CERT_OID_NAME_STR 2
297 #define CERT_X500_NAME_STR 3
298 #define CERT_NAME_STR_SEMICOLON_FLAG 1073741824
299 #define CERT_NAME_STR_CRLF_FLAG 134217728
300 #define CERT_NAME_STR_NO_PLUS_FLAG 536870912
301 #define CERT_NAME_STR_NO_QUOTING_FLAG 268435456
302 #define CERT_NAME_STR_REVERSE_FLAG 33554432
303 #define CERT_NAME_STR_ENABLE_T61_UNICODE_FLAG 131072
304 #define CERT_FIND_ANY 0
305 #define CERT_FIND_CERT_ID 1048576
306 #define CERT_FIND_CTL_USAGE 655360
307 #define CERT_FIND_ENHKEY_USAGE 655360
308 #define CERT_FIND_EXISTING 851968
309 #define CERT_FIND_HASH 65536
310 #define CERT_FIND_ISSUER_ATTR 196612
311 #define CERT_FIND_ISSUER_NAME 131076
312 #define CERT_FIND_ISSUER_OF 786432
313 #define CERT_FIND_KEY_IDENTIFIER 983040
314 #define CERT_FIND_KEY_SPEC 589824
315 #define CERT_FIND_MD5_HASH 262144
316 #define CERT_FIND_PROPERTY 327680
317 #define CERT_FIND_PUBLIC_KEY 393216
318 #define CERT_FIND_SHA1_HASH 65536
319 #define CERT_FIND_SIGNATURE_HASH 917504
320 #define CERT_FIND_SUBJECT_ATTR 196615
321 #define CERT_FIND_SUBJECT_CERT 720896
322 #define CERT_FIND_SUBJECT_NAME 131079
323 #define CERT_FIND_SUBJECT_STR_A 458759
324 #define CERT_FIND_SUBJECT_STR_W 524295
325 #define CERT_FIND_ISSUER_STR_A 458756
326 #define CERT_FIND_ISSUER_STR_W 524292
327 #define CERT_FIND_OR_ENHKEY_USAGE_FLAG 16
328 #define CERT_FIND_OPTIONAL_ENHKEY_USAGE_FLAG  1
329 #define CERT_FIND_NO_ENHKEY_USAGE_FLAG  8
330 #define CERT_FIND_VALID_ENHKEY_USAGE_FLAG  32
331 #define CERT_FIND_EXT_ONLY_ENHKEY_USAGE_FLAG  2
332 #define CERT_CASE_INSENSITIVE_IS_RDN_ATTRS_FLAG  2
333 #define CERT_UNICODE_IS_RDN_ATTRS_FLAG 1
334 #define CERT_CHAIN_FIND_BY_ISSUER 1
335 #define CERT_CHAIN_FIND_BY_ISSUER_COMPARE_KEY_FLAG 1
336 #define CERT_CHAIN_FIND_BY_ISSUER_COMPLEX_CHAIN_FLAG 2
337 #define CERT_CHAIN_FIND_BY_ISSUER_CACHE_ONLY_FLAG 32768
338 #define CERT_CHAIN_FIND_BY_ISSUER_CACHE_ONLY_URL_FLAG 4
339 #define CERT_CHAIN_FIND_BY_ISSUER_LOCAL_MACHINE_FLAG 8
340 #define CERT_CHAIN_FIND_BY_ISSUER_NO_KEY_FLAG 16384
341 #define CERT_STORE_PROV_SYSTEM 10
342 #define CERT_SYSTEM_STORE_LOCAL_MACHINE 131072
343 #define szOID_PKIX_KP_SERVER_AUTH "4235600"
344 #define szOID_SERVER_GATED_CRYPTO "4235658"
345 #define szOID_SGC_NETSCAPE "2.16.840.1.113730.4.1"
346 #define szOID_PKIX_KP_CLIENT_AUTH "1.3.6.1.5.5.7.3.2"
347 #define CRYPT_NOHASHOID 0x00000001
348 #define CRYPT_NO_SALT 0x10
349 #define CRYPT_PREGEN 0x40
350 #define CRYPT_RECIPIENT 0x10
351 #define CRYPT_INITIATOR 0x40
352 #define CRYPT_ONLINE 0x80
353 #define CRYPT_SF 0x100
354 #define CRYPT_CREATE_IV 0x200
355 #define CRYPT_KEK 0x400
356 #define CRYPT_DATA_KEY 0x800
357 #define CRYPT_VOLATILE 0x1000
358 #define CRYPT_SGCKEY 0x2000
359 #define KP_IV               0x00000001
360 #define KP_SALT             0x00000002
361 #define KP_PADDING          0x00000003
362 #define KP_MODE             0x00000004
363 #define KP_MODE_BITS        0x00000005
364 #define KP_PERMISSIONS      0x00000006
365 #define KP_ALGID            0x00000007
366 #define KP_BLOCKLEN         0x00000008
367 #define KP_KEYLEN           0x00000009
368 #define KP_SALT_EX          0x0000000a
369 #define KP_P                0x0000000b
370 #define KP_G                0x0000000c
371 #define KP_Q                0x0000000d
372 #define KP_X                0x0000000e
373 #define KP_Y                0x0000000f
374 #define KP_RA               0x00000010
375 #define KP_RB               0x00000011
376 #define KP_INFO             0x00000012
377 #define KP_EFFECTIVE_KEYLEN 0x00000013
378 #define KP_SCHANNEL_ALG     0x00000014
379 #define KP_PUB_PARAMS       0x00000027
380 #define CRYPT_FLAG_PCT1    0x0001
381 #define CRYPT_FLAG_SSL2    0x0002
382 #define CRYPT_FLAG_SSL3    0x0004
383 #define CRYPT_FLAG_TLS1    0x0008
384 #define CRYPT_FLAG_IPSEC   0x0010
385 #define CRYPT_FLAG_SIGNING 0x0020
386 #define SCHANNEL_MAC_KEY    0x00000000
387 #define SCHANNEL_ENC_KEY    0x00000001
388 #define INTERNATIONAL_USAGE 0x00000001
389 +/
390 
391 alias UINT ALG_ID;
392 alias ULONG HCRYPTPROV, HCRYPTKEY, HCRYPTHASH;
393 alias PVOID HCERTSTORE, HCRYPTMSG, HCERTCHAINENGINE;
394 
395 struct VTableProvStruc {
396 	FARPROC FuncVerifyImage;
397 }
398 alias VTableProvStruc* PVTableProvStruc;
399 
400 struct _CRYPTOAPI_BLOB {
401 	DWORD cbData;
402 	BYTE* pbData;
403 }
404 alias _CRYPTOAPI_BLOB CRYPT_INTEGER_BLOB, CRYPT_UINT_BLOB,
405   CRYPT_OBJID_BLOB, CERT_NAME_BLOB, CERT_RDN_VALUE_BLOB, CERT_BLOB,
406   CRL_BLOB, DATA_BLOB, CRYPT_DATA_BLOB, CRYPT_HASH_BLOB,
407   CRYPT_DIGEST_BLOB, CRYPT_DER_BLOB, CRYPT_ATTR_BLOB;
408 alias _CRYPTOAPI_BLOB* PCRYPT_INTEGER_BLOB, PCRYPT_UINT_BLOB,
409   PCRYPT_OBJID_BLOB, PCERT_NAME_BLOB, PCERT_RDN_VALUE_BLOB, PCERT_BLOB,
410   PCRL_BLOB, PDATA_BLOB, PCRYPT_DATA_BLOB, PCRYPT_HASH_BLOB,
411   PCRYPT_DIGEST_BLOB, PCRYPT_DER_BLOB, PCRYPT_ATTR_BLOB;
412 
413 // not described in SDK; has the same layout as HTTPSPolicyCallbackData
414 struct SSL_EXTRA_CERT_CHAIN_POLICY_PARA {
415 	DWORD  cbStruct;
416 	DWORD  dwAuthType;
417 	DWORD  fdwChecks;
418 	LPWSTR pwszServerName;
419 }
420 alias SSL_EXTRA_CERT_CHAIN_POLICY_PARA HTTPSPolicyCallbackData;
421 alias SSL_EXTRA_CERT_CHAIN_POLICY_PARA* PSSL_EXTRA_CERT_CHAIN_POLICY_PARA,
422   PHTTPSPolicyCallbackData;
423 
424 /* #if (_WIN32_WINNT>=0x500) */
425 struct CERT_CHAIN_POLICY_PARA {
426 	DWORD cbSize = CERT_CHAIN_POLICY_PARA.sizeof;
427 	DWORD dwFlags;
428 	void* pvExtraPolicyPara;
429 }
430 alias CERT_CHAIN_POLICY_PARA* PCERT_CHAIN_POLICY_PARA;
431 
432 struct CERT_CHAIN_POLICY_STATUS {
433 	DWORD cbSize = CERT_CHAIN_POLICY_STATUS.sizeof;
434 	DWORD dwError;
435 	LONG  lChainIndex;
436 	LONG  lElementIndex;
437 	void* pvExtraPolicyStatus;
438 }
439 alias CERT_CHAIN_POLICY_STATUS* PCERT_CHAIN_POLICY_STATUS;
440 /* #endif */
441 
442 struct CRYPT_ALGORITHM_IDENTIFIER {
443 	LPSTR pszObjId;
444 	CRYPT_OBJID_BLOB Parameters;
445 }
446 alias CRYPT_ALGORITHM_IDENTIFIER* PCRYPT_ALGORITHM_IDENTIFIER;
447 
448 struct CRYPT_BIT_BLOB {
449 	DWORD cbData;
450 	BYTE* pbData;
451 	DWORD cUnusedBits;
452 }
453 alias CRYPT_BIT_BLOB* PCRYPT_BIT_BLOB;
454 
455 struct CERT_PUBLIC_KEY_INFO {
456 	CRYPT_ALGORITHM_IDENTIFIER Algorithm;
457 	CRYPT_BIT_BLOB             PublicKey;
458 }
459 alias CERT_PUBLIC_KEY_INFO* PCERT_PUBLIC_KEY_INFO;
460 
461 struct CERT_EXTENSION {
462 	LPSTR            pszObjId;
463 	BOOL             fCritical;
464 	CRYPT_OBJID_BLOB Value;
465 }
466 alias CERT_EXTENSION* PCERT_EXTENSION;
467 
468 struct CERT_INFO {
469 	DWORD dwVersion;
470 	CRYPT_INTEGER_BLOB SerialNumber;
471 	CRYPT_ALGORITHM_IDENTIFIER SignatureAlgorithm;
472 	CERT_NAME_BLOB Issuer;
473 	FILETIME NotBefore;
474 	FILETIME NotAfter;
475 	CERT_NAME_BLOB Subject;
476 	CERT_PUBLIC_KEY_INFO SubjectPublicKeyInfo;
477 	CRYPT_BIT_BLOB IssuerUniqueId;
478 	CRYPT_BIT_BLOB SubjectUniqueId;
479 	DWORD cExtension;
480 	PCERT_EXTENSION rgExtension;
481 }
482 alias CERT_INFO* PCERT_INFO;
483 
484 struct CERT_CONTEXT {
485 	DWORD      dwCertEncodingType;
486 	BYTE*      pbCertEncoded;
487 	DWORD      cbCertEncoded;
488 	PCERT_INFO pCertInfo;
489 	HCERTSTORE hCertStore;
490 }
491 alias CERT_CONTEXT*       PCERT_CONTEXT;
492 alias CPtr!(CERT_CONTEXT) PCCERT_CONTEXT;
493 
494 struct CTL_USAGE {
495 	DWORD  cUsageIdentifier;
496 	LPSTR* rgpszUsageIdentifier;
497 }
498 alias CTL_USAGE CERT_ENHKEY_USAGE;
499 alias CTL_USAGE* PCTRL_USAGE, PCERT_ENHKEY_USAGE;
500 
501 struct CERT_USAGE_MATCH {
502 	DWORD             dwType;
503 	CERT_ENHKEY_USAGE Usage;
504 }
505 alias CERT_USAGE_MATCH* PCERT_USAGE_MATCH;
506 /* #if (_WIN32_WINNT>=0x500) */
507 
508 struct CERT_CHAIN_PARA {
509 	DWORD            cbSize = CERT_CHAIN_PARA.sizeof;
510 	CERT_USAGE_MATCH RequestedUsage;
511 //#if CERT_CHAIN_PARA_HAS_EXTRA_FIELDS
512 	CERT_USAGE_MATCH RequestedIssuancePolicy;
513 	DWORD            dwUrlRetrievalTimeout;
514 	BOOL             fCheckRevocationFreshnessTime;
515 	DWORD            dwRevocationFreshnessTime;
516 //#endif
517 }
518 alias CERT_CHAIN_PARA* PCERT_CHAIN_PARA;
519 
520 extern (Windows) alias BOOL function(PCCERT_CONTEXT, void*)
521   PFN_CERT_CHAIN_FIND_BY_ISSUER_CALLBACK;
522 
523 struct CERT_CHAIN_FIND_BY_ISSUER_PARA {
524 	DWORD  cbSize = CERT_CHAIN_FIND_BY_ISSUER_PARA.sizeof;
525 	LPCSTR pszUsageIdentifier;
526 	DWORD  dwKeySpec;
527 	DWORD  dwAcquirePrivateKeyFlags;
528 	DWORD  cIssuer;
529 	CERT_NAME_BLOB* rgIssuer;
530 	PFN_CERT_CHAIN_FIND_BY_ISSUER_CALLBACK pfnFIndCallback;
531 	void*  pvFindArg;
532 	DWORD* pdwIssuerChainIndex;
533 	DWORD* pdwIssuerElementIndex;
534 }
535 alias CERT_CHAIN_FIND_BY_ISSUER_PARA* PCERT_CHAIN_FIND_BY_ISSUER_PARA;
536 /* #endif */
537 
538 struct CERT_TRUST_STATUS {
539 	DWORD dwErrorStatus;
540 	DWORD dwInfoStatus;
541 }
542 alias CERT_TRUST_STATUS* PCERT_TRUST_STATUS;
543 
544 struct CRL_ENTRY {
545 	CRYPT_INTEGER_BLOB SerialNumber;
546 	FILETIME           RevocationDate;
547 	DWORD              cExtension;
548 	PCERT_EXTENSION    rgExtension;
549 }
550 alias CRL_ENTRY* PCRL_ENTRY;
551 
552 struct CRL_INFO {
553 	DWORD           dwVersion;
554 	CRYPT_ALGORITHM_IDENTIFIER SignatureAlgorithm;
555 	CERT_NAME_BLOB  Issuer;
556 	FILETIME        ThisUpdate;
557 	FILETIME        NextUpdate;
558 	DWORD           cCRLEntry;
559 	PCRL_ENTRY      rgCRLEntry;
560 	DWORD           cExtension;
561 	PCERT_EXTENSION rgExtension;
562 }
563 alias CRL_INFO* PCRL_INFO;
564 
565 struct CRL_CONTEXT {
566 	DWORD      dwCertEncodingType;
567 	BYTE*      pbCrlEncoded;
568 	DWORD      cbCrlEncoded;
569 	PCRL_INFO  pCrlInfo;
570 	HCERTSTORE hCertStore;
571 }
572 alias CRL_CONTEXT*       PCRL_CONTEXT;
573 alias CPtr!(CRL_CONTEXT) PCCRL_CONTEXT;
574 
575 struct CERT_REVOCATION_CRL_INFO {
576 	DWORD         cbSize = CERT_REVOCATION_CRL_INFO.sizeof;
577 	PCCRL_CONTEXT pBaseCRLContext;
578 	PCCRL_CONTEXT pDeltaCRLContext;
579 	PCRL_ENTRY    pCrlEntry;
580 	BOOL          fDeltaCrlEntry;
581 }
582 alias CERT_REVOCATION_CRL_INFO* PCERT_REVOCATION_CRL_INFO;
583 
584 struct CERT_REVOCATION_INFO {
585 	DWORD  cbSize = CERT_REVOCATION_INFO.sizeof;
586 	DWORD  dwRevocationResult;
587 	LPCSTR pszRevocationOid;
588 	LPVOID pvOidSpecificInfo;
589 	BOOL   fHasFreshnessTime;
590 	DWORD  dwFreshnessTime;
591 	PCERT_REVOCATION_CRL_INFO pCrlInfo;
592 }
593 alias CERT_REVOCATION_INFO* PCERT_REVOCATION_INFO;
594 
595 /* #if (_WIN32_WINNT>=0x500) */
596 struct CERT_CHAIN_ELEMENT {
597 	DWORD                 cbSize = CERT_CHAIN_ELEMENT.sizeof;
598 	PCCERT_CONTEXT        pCertContext;
599 	CERT_TRUST_STATUS     TrustStatus;
600 	PCERT_REVOCATION_INFO pRevocationInfo;
601 	PCERT_ENHKEY_USAGE    pIssuanceUsage;
602 	PCERT_ENHKEY_USAGE    pApplicationUsage;
603 }
604 alias CERT_CHAIN_ELEMENT* PCERT_CHAIN_ELEMENT;
605 /* #endif */
606 
607 struct CRYPT_ATTRIBUTE {
608 	LPSTR            pszObjId;
609 	DWORD            cValue;
610 	PCRYPT_ATTR_BLOB rgValue;
611 }
612 alias CRYPT_ATTRIBUTE* PCRYPT_ATTRIBUTE;
613 
614 struct CTL_ENTRY {
615 	CRYPT_DATA_BLOB  SubjectIdentifier;
616 	DWORD            cAttribute;
617 	PCRYPT_ATTRIBUTE rgAttribute;
618 }
619 alias CTL_ENTRY* PCTL_ENTRY;
620 
621 struct CTL_INFO {
622 	DWORD              dwVersion;
623 	CTL_USAGE          SubjectUsage;
624 	CRYPT_DATA_BLOB    ListIdentifier;
625 	CRYPT_INTEGER_BLOB SequenceNumber;
626 	FILETIME           ThisUpdate;
627 	FILETIME           NextUpdate;
628 	CRYPT_ALGORITHM_IDENTIFIER SubjectAlgorithm;
629 	DWORD              cCTLEntry;
630 	PCTL_ENTRY         rgCTLEntry;
631 	DWORD              cExtension;
632 	PCERT_EXTENSION    rgExtension;
633 }
634 alias CTL_INFO* PCTL_INFO;
635 
636 struct CTL_CONTEXT {
637 	DWORD      dwMsgAndCertEncodingType;
638 	BYTE*      pbCtlEncoded;
639 	DWORD      cbCtlEncoded;
640 	PCTL_INFO  pCtlInfo;
641 	HCERTSTORE hCertStore;
642 	HCRYPTMSG  hCryptMsg;
643 	BYTE*      pbCtlContent;
644 	DWORD      cbCtlContent;
645 }
646 alias CTL_CONTEXT*       PCTL_CONTEXT;
647 alias CPtr!(CTL_CONTEXT) PCCTL_CONTEXT;
648 
649 struct CERT_TRUST_LIST_INFO {
650 	DWORD         cbSize = CERT_TRUST_LIST_INFO.sizeof;
651 	PCTL_ENTRY    pCtlEntry;
652 	PCCTL_CONTEXT pCtlContext;
653 }
654 alias CERT_TRUST_LIST_INFO* PCERT_TRUST_LIST_INFO;
655 
656 struct CERT_SIMPLE_CHAIN {
657 	DWORD                 cbSize = CERT_SIMPLE_CHAIN.sizeof;
658 	CERT_TRUST_STATUS     TrustStatus;
659 	DWORD                 cElement;
660 	PCERT_CHAIN_ELEMENT*  rgpElement;
661 	PCERT_TRUST_LIST_INFO pTrustListInfo;
662 	BOOL                  fHasRevocationFreshnessTime;
663 	DWORD                 dwRevocationFreshnessTime;
664 }
665 alias CERT_SIMPLE_CHAIN* PCERT_SIMPLE_CHAIN;
666 
667 /* #if (_WIN32_WINNT>=0x500) */
668 alias CPtr!(CERT_CHAIN_CONTEXT) PCCERT_CHAIN_CONTEXT;
669 struct CERT_CHAIN_CONTEXT {
670 	DWORD                 cbSize = CERT_CHAIN_CONTEXT.sizeof;
671 	CERT_TRUST_STATUS     TrustStatus;
672 	DWORD                 cChain;
673 	PCERT_SIMPLE_CHAIN*   rgpChain;
674 	DWORD                 cLowerQualityChainContext;
675 	PCCERT_CHAIN_CONTEXT* rgpLowerQualityChainContext;
676 	BOOL                  fHasRevocationFreshnessTime;
677 	DWORD                 dwRevocationFreshnessTime;
678 }
679 alias CERT_CHAIN_CONTEXT* PCERT_CHAIN_CONTEXT;
680 /* #endif */
681 
682 struct PROV_ENUMALGS {
683 	ALG_ID   aiAlgid;
684 	DWORD    dwBitLen;
685 	DWORD    dwNameLen;
686 	CHAR[20] szName;
687 }
688 
689 struct PUBLICKEYSTRUC {
690 	BYTE   bType;
691 	BYTE   bVersion;
692 	WORD   reserved;
693 	ALG_ID aiKeyAlg;
694 }
695 alias PUBLICKEYSTRUC BLOBHEADER;
696 
697 struct RSAPUBKEY {
698 	DWORD magic;
699 	DWORD bitlen;
700 	DWORD pubexp;
701 }
702 
703 struct HMAC_INFO {
704 	ALG_ID HashAlgid;
705 	BYTE*  pbInnerString;
706 	DWORD  cbInnerString;
707 	BYTE*  pbOuterString;
708 	DWORD  cbOuterString;
709 }
710 alias HMAC_INFO* PHMAC_INFO;
711 
712 extern (Windows) {
713 	BOOL CertCloseStore(HCERTSTORE, DWORD);
714 	BOOL CertGetCertificateChain(HCERTCHAINENGINE, PCCERT_CONTEXT, LPFILETIME,
715 	  HCERTSTORE, PCERT_CHAIN_PARA, DWORD, LPVOID, PCCERT_CHAIN_CONTEXT*);
716 	BOOL CertVerifyCertificateChainPolicy(LPCSTR, PCCERT_CHAIN_CONTEXT,
717 	  PCERT_CHAIN_POLICY_PARA, PCERT_CHAIN_POLICY_STATUS);
718 	void CertFreeCertificateChain(PCCERT_CHAIN_CONTEXT);
719 	DWORD CertNameToStrA(DWORD, PCERT_NAME_BLOB, DWORD, LPSTR, DWORD);
720 	DWORD CertNameToStrW(DWORD, PCERT_NAME_BLOB, DWORD, LPWSTR, DWORD);
721 	HCERTSTORE CertOpenSystemStoreA(HCRYPTPROV, LPCSTR);
722 	HCERTSTORE CertOpenSystemStoreW(HCRYPTPROV, LPCWSTR);
723 	HCERTSTORE CertOpenStore(LPCSTR, DWORD, HCRYPTPROV, DWORD, CPtr!(void));
724 	PCCERT_CONTEXT CertFindCertificateInStore(HCERTSTORE, DWORD, DWORD, DWORD,
725 	  CPtr!(void), PCCERT_CONTEXT);
726 	BOOL CertFreeCertificateContext(PCCERT_CONTEXT);
727 	PCCERT_CONTEXT CertGetIssuerCertificateFromStore(HCERTSTORE,
728 	  PCCERT_CONTEXT, PCCERT_CONTEXT, DWORD*);
729 	PCCERT_CHAIN_CONTEXT CertFindChainInStore(HCERTSTORE, DWORD, DWORD, DWORD,
730 	  CPtr!(void), PCCERT_CHAIN_CONTEXT);
731 
732 	BOOL CryptAcquireContextA(HCRYPTPROV*, LPCSTR, LPCSTR, DWORD, DWORD);
733 	BOOL CryptAcquireContextW(HCRYPTPROV*, LPCWSTR, LPCWSTR, DWORD, DWORD);
734 	 BOOL CryptContextAddRef(HCRYPTPROV, DWORD*, DWORD);
735 	BOOL CryptReleaseContext(HCRYPTPROV, DWORD);
736 	BOOL CryptGenKey(HCRYPTPROV, ALG_ID, DWORD, HCRYPTKEY*);
737 	BOOL CryptDeriveKey(HCRYPTPROV, ALG_ID, HCRYPTHASH, DWORD, HCRYPTKEY*);
738 	BOOL CryptDestroyKey(HCRYPTKEY);
739 	static if (WINVER >= 0x0500) {
740 		BOOL CryptDuplicateHash(HCRYPTHASH, DWORD*, DWORD, HCRYPTHASH*);
741 		BOOL CryptDuplicateKey(HCRYPTKEY, DWORD*, DWORD, HCRYPTKEY*);
742 	}
743 	BOOL CryptSetKeyParam(HCRYPTKEY, DWORD, PBYTE, DWORD);
744 	BOOL CryptGetKeyParam(HCRYPTKEY, DWORD, PBYTE, PDWORD, DWORD);
745 	BOOL CryptSetHashParam(HCRYPTHASH, DWORD, PBYTE, DWORD);
746 	BOOL CryptGetHashParam(HCRYPTHASH, DWORD, PBYTE, PDWORD, DWORD);
747 	BOOL CryptSetProvParam(HCRYPTPROV, DWORD, PBYTE, DWORD);
748 	BOOL CryptGetProvParam(HCRYPTPROV, DWORD, PBYTE, PDWORD, DWORD);
749 	BOOL CryptGenRandom(HCRYPTPROV, DWORD, PBYTE);
750 	BOOL CryptGetUserKey(HCRYPTPROV, DWORD, HCRYPTKEY*);
751 	BOOL CryptExportKey(HCRYPTKEY, HCRYPTKEY, DWORD, DWORD, PBYTE, PDWORD);
752 	BOOL CryptImportKey(HCRYPTPROV, PBYTE, DWORD, HCRYPTKEY, DWORD,
753 	  HCRYPTKEY*);
754 	BOOL CryptEncrypt(HCRYPTKEY, HCRYPTHASH, BOOL, DWORD, PBYTE, PDWORD,
755 	  DWORD);
756 	BOOL CryptDecrypt(HCRYPTKEY, HCRYPTHASH, BOOL, DWORD, PBYTE, PDWORD);
757 	BOOL CryptCreateHash(HCRYPTPROV, ALG_ID, HCRYPTKEY, DWORD, HCRYPTHASH*);
758 	BOOL CryptHashData(HCRYPTHASH, PBYTE, DWORD, DWORD);
759 	BOOL CryptHashSessionKey(HCRYPTHASH, HCRYPTKEY, DWORD);
760 	BOOL CryptGetHashValue(HCRYPTHASH, DWORD, PBYTE, PDWORD);
761 	BOOL CryptDestroyHash(HCRYPTHASH);
762 	BOOL CryptSignHashA(HCRYPTHASH, DWORD, LPCSTR, DWORD, PBYTE, PDWORD);
763 	BOOL CryptSignHashW(HCRYPTHASH, DWORD, LPCWSTR, DWORD, PBYTE, PDWORD);
764 	BOOL CryptVerifySignatureA(HCRYPTHASH, PBYTE, DWORD, HCRYPTKEY, LPCSTR,
765 	  DWORD);
766 	BOOL CryptVerifySignatureW(HCRYPTHASH, PBYTE, DWORD, HCRYPTKEY, LPCWSTR,
767 	  DWORD);
768 	BOOL CryptSetProviderA(LPCSTR, DWORD);
769 	BOOL CryptSetProviderW(LPCWSTR, DWORD);
770 }
771 
772 version (Unicode) {
773 	alias CertNameToStrW CertNameToStr;
774 	alias CryptAcquireContextW CryptAcquireContext;
775 	alias CryptSignHashW CryptSignHash;
776 	alias CryptVerifySignatureW CryptVerifySignature;
777 	alias CryptSetProviderW CryptSetProvider;
778 	alias CertOpenSystemStoreW CertOpenSystemStore;
779 	/+alias CERT_FIND_SUBJECT_STR_W CERT_FIND_SUBJECT_STR;
780 	alias CERT_FIND_ISSUER_STR_W CERT_FIND_ISSUER_STR;+/
781 } else {
782 	alias CertNameToStrA CertNameToStr;
783 	alias CryptAcquireContextA CryptAcquireContext;
784 	alias CryptSignHashA CryptSignHash;
785 	alias CryptVerifySignatureA CryptVerifySignature;
786 	alias CryptSetProviderA CryptSetProvider;
787 	alias CertOpenSystemStoreA CertOpenSystemStore;
788 	/+alias CERT_FIND_SUBJECT_STR_A CERT_FIND_SUBJECT_STR;
789 	alias CERT_FIND_ISSUER_STR_A CERT_FIND_ISSUER_STR;+/
790 }