1const std = @import("../../std.zig");
2const windows = std.os.windows;
3
4const BOOL = windows.BOOL;
5const DWORD = windows.DWORD;
6const BYTE = windows.BYTE;
7const LONG = windows.LONG;
8const LPCSTR = windows.LPCSTR;
9const LPCWSTR = windows.LPCWSTR;
10const FILETIME = windows.FILETIME;
11const HANDLE = windows.HANDLE;
12
13// ref: um/wincrypt.h
14
15pub const HCRYPTPROV_LEGACY = enum(usize) { NULL = 0 };
16
17pub const CERT_INFO = *opaque {};
18
19pub const CTL_USAGE = extern struct {
20 cUsageIdentifier: DWORD,
21 rgpszUsageIdentifier: [*]const LPCSTR,
22};
23
24pub const CERT_ENHKEY_USAGE = CTL_USAGE;
25
26pub const ENCODING = enum(u16) {
27 UNSPECIFIED = 0x0000,
28 ASN = 0x0001,
29 NDR = 0x0002,
30 _,
31
32 pub const TYPE = packed struct(DWORD) {
33 CERT: ENCODING = .UNSPECIFIED,
34 CMSG: ENCODING = .UNSPECIFIED,
35 };
36};
37
38pub const HCERTSTORE = *opaque {};
39
40pub const CERT_CONTEXT = extern struct {
41 dwCertEncodingType: ENCODING.TYPE,
42 pbCertEncoded: [*]BYTE,
43 cbCertEncoded: DWORD,
44 pCertInfo: CERT_INFO,
45 hCertStore: HCERTSTORE,
46};
47
48pub const CERT_STORE = struct {
49 pub const PROV = enum(usize) {
50 MSG = 1,
51 MEMORY = 2,
52 FILE = 3,
53 REG = 4,
54
55 PKCS7 = 5,
56 SERIALIZED = 6,
57 FILENAME_A = 7,
58 FILENAME_W = 8,
59 SYSTEM_A = 9,
60 SYSTEM_W = 10,
61
62 COLLECTION = 11,
63 SYSTEM_REGISTRY_A = 12,
64 SYSTEM_REGISTRY_W = 13,
65 PHYSICAL_W = 14,
66
67 SMART_CARD_W = 15,
68
69 LDAP_W = 16,
70 PKCS12 = 17,
71
72 /// LPCSTR
73 _,
74
75 pub fn fromString(str: LPCSTR) PROV {
76 return @fromBackingInt(@intCast(@intFromPtr(str)));
77 }
78 };
79
80 pub const FLAG = packed struct(DWORD) {
81 NO_CRYPT_RELEASE: bool = false,
82 SET_LOCALIZED_NAME: bool = false,
83 DEFER_CLOSE_UNTIL_LAST_FREE: bool = false,
84 Reserved3: u1 = 0,
85 DELETE: bool = false,
86 UNSAFE_PHYSICAL: bool = false,
87 SHARE_STORE: bool = false,
88 SHARE_CONTEXT: bool = false,
89 MANIFOLD: bool = false,
90 ENUM_ARCHIVED: bool = false,
91 UPDATE_KEYID: bool = false,
92 BACKUP_RESTORE: bool = false,
93 MAXIMUM_ALLOWED: bool = false,
94 CREATE_NEW: bool = false,
95 OPEN_EXISTING: bool = false,
96 READONLY: bool = false,
97 Reserved16: u16 = 0,
98 };
99
100 pub const ADD = enum(DWORD) {
101 NEW = 1,
102 USE_EXISTING = 2,
103 REPLACE_EXISTING = 3,
104 ALWAYS = 4,
105 REPLACE_EXISTING_INHERIT_PROPERTIES = 5,
106 REWER = 6,
107 NEWER_INHERIT_PROPERTIES = 7,
108 _,
109 };
110};
111
112pub extern "crypt32" fn CertOpenStore(
113 lpszStoreProvider: CERT_STORE.PROV,
114 dwEncodingType: ENCODING.TYPE,
115 hCryptProv: HCRYPTPROV_LEGACY,
116 dwFlags: CERT_STORE.FLAG,
117 pvPara: ?*const anyopaque,
118) callconv(.winapi) ?HCERTSTORE;
119
120pub const CERT_CLOSE_STORE_FLAG = packed struct(DWORD) {
121 FORCE: bool = false,
122 CHECK: bool = false,
123 Reserved2: u30 = 0,
124};
125
126pub extern "crypt32" fn CertCloseStore(
127 hCertStore: HCERTSTORE,
128 dwFlags: CERT_CLOSE_STORE_FLAG,
129) callconv(.winapi) BOOL;
130
131pub extern "crypt32" fn CertEnumCertificatesInStore(
132 hCertStore: HCERTSTORE,
133 pPrevCertContext: ?*CERT_CONTEXT,
134) callconv(.winapi) ?*CERT_CONTEXT;
135
136pub extern "crypt32" fn CertFreeCertificateContext(
137 pCertContext: ?*const CERT_CONTEXT,
138) callconv(.winapi) BOOL;
139
140pub extern "crypt32" fn CertAddEncodedCertificateToStore(
141 hCertStore: ?HCERTSTORE,
142 dwCertEncodingType: ENCODING.TYPE,
143 pbCertEncoded: [*]const BYTE,
144 cbCertEncoded: DWORD,
145 dwAddDisposition: CERT_STORE.ADD,
146 ppCertContext: ?*?*const CERT_CONTEXT,
147) callconv(.winapi) BOOL;
148
149pub extern "crypt32" fn CertOpenSystemStoreW(
150 hProv: HCRYPTPROV_LEGACY,
151 szSubsystemProtocol: LPCWSTR,
152) callconv(.winapi) ?HCERTSTORE;
153
154pub const HCERTCHAINENGINE = enum(usize) {
155 CURRENT_USER = 0x0,
156 LOCAL_MACHINE = 0x1,
157 SERIAL_LOCAL_MACHINE = 0x2,
158 /// HANDLE
159 _,
160
161 pub fn fromHandle(handle: HANDLE) HCERTCHAINENGINE {
162 return @fromBackingInt(@intCast(@intFromPtr(handle)));
163 }
164};
165
166pub const CERT_CHAIN = packed struct(DWORD) {
167 CACHE_END_CERT: bool = false,
168 THREAD_STORE_SYNC: bool = false,
169 CACHE_ONLY_URL_RETRIEVAL: bool = false,
170 USE_LOCAL_MACHINE_STORE: bool = false,
171 ENABLE_CACHE_AUTO_UPDATE: bool = false,
172 ENABLE_SHARE_STORE: bool = false,
173 Reserved6: u20 = 0,
174 REVOCATION_CHECK_OCSP_CERT: bool = false,
175 REVOCATION_ACCUMULATIVE_TIMEOUT: bool = false,
176 REVOCATION_CHECK_END_CERT: bool = false,
177 REVOCATION_CHECK_CHAIN: bool = false,
178 REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT: bool = false,
179 REVOCATION_CHECK_CACHE_ONLY: bool = false,
180
181 pub const CONTEXT = opaque {};
182
183 pub const USAGE_MATCH = extern struct {
184 dwType: TYPE,
185 Usage: CERT_ENHKEY_USAGE,
186
187 pub const TYPE = enum(DWORD) { AND = 0x00000000, OR = 0x00000001, _ };
188 };
189
190 pub const PARA = extern struct {
191 cbSize: DWORD = @sizeOf(PARA),
192 RequestedUsage: USAGE_MATCH,
193 };
194
195 pub const POLICY = enum(usize) {
196 BASE = 1,
197 AUTHENTICODE = 2,
198 AUTHENTICODE_TS = 3,
199 SSL = 4,
200 BASIC_CONSTRAINTS = 5,
201 NT_AUTH = 6,
202 MICROSOFT_ROOT = 7,
203 EV = 8,
204 SSL_F12 = 9,
205 SSL_HPKP_HEADER = 10,
206 THIRD_PARTY_ROOT = 11,
207 SSL_KEY_PIN = 12,
208 CT = 13,
209 /// LPCSTR
210 _,
211
212 pub fn fromString(str: LPCSTR) POLICY {
213 return @fromBackingInt(@intCast(@intFromPtr(str)));
214 }
215
216 pub const PARA = extern struct {
217 cbSize: DWORD = @sizeOf(POLICY.PARA),
218 dwFlags: FLAG,
219 pvExtraPolicyPara: ?*anyopaque,
220 };
221
222 pub const STATUS = extern struct {
223 cbSize: DWORD = @sizeOf(STATUS),
224 dwError: windows.Win32Error,
225 lChainIndex: LONG,
226 lElementIndex: LONG,
227 pvExtraPolicyStatus: ?*anyopaque,
228 };
229
230 pub const FLAG = packed struct(DWORD) {
231 IGNORE_NOT_TIME_VALID: bool = false,
232 IGNORE_CTL_NOT_TIME_VALID: bool = false,
233 IGNORE_NOT_TIME_NESTED: bool = false,
234 IGNORE_INVALID_BASIC_CONSTRAINTS: bool = false,
235 ALLOW_UNKNOWN_CA: bool = false,
236 IGNORE_WRONG_USAGE: bool = false,
237 IGNORE_INVALID_NAME: bool = false,
238 IGNORE_INVALID_POLICY: bool = false,
239 IGNORE_END_REV_UNKNOWN: bool = false,
240 IGNORE_CTL_SIGNER_REV_UNKNOWN: bool = false,
241 IGNORE_CA_REV_UNKNOWN: bool = false,
242 IGNORE_ROOT_REV_UNKNOWN: bool = false,
243 IGNORE_PEER_TRUST: bool = false,
244 IGNORE_NOT_SUPPORTED_CRITICAL_EXT: bool = false,
245 TRUST_TESTROOT: bool = false,
246 ALLOW_TESTROOT: bool = false,
247 Reserved16: u11 = 0,
248 IGNORE_WEAK_SIGNATURE: bool = false,
249 Reserved28: u4 = 0,
250 };
251 };
252};
253
254pub const HTTPSPolicyCallbackData = extern struct {
255 cbSize: DWORD = @sizeOf(HTTPSPolicyCallbackData),
256 dwAuthType: AUTHTYPE,
257 fdwChecks: DWORD = 0,
258 pwszServerName: ?LPCWSTR = null,
259
260 pub const AUTHTYPE = enum(DWORD) { CLIENT = 1, SERVER = 2, _ };
261};
262
263pub extern "crypt32" fn CertGetCertificateChain(
264 hChainEngine: HCERTCHAINENGINE,
265 pCertContext: *const CERT_CONTEXT,
266 pTime: ?*const FILETIME,
267 hAdditionalStore: ?HCERTSTORE,
268 pChainPara: *const CERT_CHAIN.PARA,
269 dwFlags: CERT_CHAIN,
270 pvReserved: ?*const anyopaque,
271 ppChainContext: **const CERT_CHAIN.CONTEXT,
272) callconv(.winapi) BOOL;
273
274pub extern "crypt32" fn CertFreeCertificateChain(
275 pChainContext: *const CERT_CHAIN.CONTEXT,
276) callconv(.winapi) void;
277
278pub extern "crypt32" fn CertVerifyCertificateChainPolicy(
279 pszPolicyOID: CERT_CHAIN.POLICY,
280 pChainContext: *const CERT_CHAIN.CONTEXT,
281 pPolicyPara: *const CERT_CHAIN.POLICY.PARA,
282 pPolicyStatus: *CERT_CHAIN.POLICY.STATUS,
283) callconv(.winapi) BOOL;