| author | |
| committer | |
| log | 1f9fa822350ddf73fb7b3b9eb8f493ab64c935c8 |
| tree | 6f125079acb184599bc57f68538493366ff86a6e |
| parent | d56a65a8c4609a740eee43fd7073c2485c87c2c6 |
3 files changed, 85 insertions(+), 3 deletions(-)
lib/std/crypto/Certificate/Bundle.zig+30-3| ... | @@ -57,10 +57,8 @@ pub fn deinit(cb: *Bundle, gpa: Allocator) void { | ... | @@ -57,10 +57,8 @@ pub fn deinit(cb: *Bundle, gpa: Allocator) void { |
| 57 | pub fn rescan(cb: *Bundle, gpa: Allocator) !void { | 57 | pub fn rescan(cb: *Bundle, gpa: Allocator) !void { |
| 58 | switch (builtin.os.tag) { | 58 | switch (builtin.os.tag) { |
| 59 | .linux => return rescanLinux(cb, gpa), | 59 | .linux => return rescanLinux(cb, gpa), |
| 60 | .windows => { | ||
| 61 | // TODO | ||
| 62 | }, | ||
| 63 | .macos => return rescanMac(cb, gpa), | 60 | .macos => return rescanMac(cb, gpa), |
| 61 | .windows => return rescanWindows(cb, gpa), | ||
| 64 | else => {}, | 62 | else => {}, |
| 65 | } | 63 | } |
| 66 | } | 64 | } |
| ... | @@ -109,6 +107,34 @@ pub fn rescanLinux(cb: *Bundle, gpa: Allocator) !void { | ... | @@ -109,6 +107,34 @@ pub fn rescanLinux(cb: *Bundle, gpa: Allocator) !void { |
| 109 | cb.bytes.shrinkAndFree(gpa, cb.bytes.items.len); | 107 | cb.bytes.shrinkAndFree(gpa, cb.bytes.items.len); |
| 110 | } | 108 | } |
| 111 | 109 | ||
| 110 | pub fn rescanWindows(cb: *Bundle, gpa: Allocator) !void { | ||
| 111 | cb.bytes.clearRetainingCapacity(); | ||
| 112 | cb.map.clearRetainingCapacity(); | ||
| 113 | |||
| 114 | const store = try os.windows.crypt32.certOpenSystemStoreW(null, &[4:0]u16{ 'R', 'O', 'O', 'T' }); | ||
| 115 | defer os.windows.crypt32.certCloseStore(store, 0) catch unreachable; | ||
| 116 | |||
| 117 | var ctx = os.windows.crypt32.CertEnumCertificatesInStore(store, null); | ||
| 118 | while (ctx) |context| : (ctx = os.windows.crypt32.CertEnumCertificatesInStore(store, ctx)) { | ||
| 119 | var start = @intCast(u32, cb.bytes.items.len); | ||
| 120 | try cb.bytes.appendSlice(gpa, context.pbCertEncoded[0..context.cbCertEncoded]); | ||
| 121 | var parsed = Certificate.parse(.{ | ||
| 122 | .buffer = cb.bytes.items, | ||
| 123 | .index = start, | ||
| 124 | }) catch { | ||
| 125 | cb.bytes.items.len = start; | ||
| 126 | continue; | ||
| 127 | }; | ||
| 128 | const gop = try cb.map.getOrPutContext(gpa, parsed.subject_slice, .{ .cb = cb }); | ||
| 129 | if (gop.found_existing) { | ||
| 130 | cb.bytes.items.len = start; | ||
| 131 | } else { | ||
| 132 | gop.value_ptr.* = start; | ||
| 133 | } | ||
| 134 | } | ||
| 135 | cb.bytes.shrinkAndFree(gpa, cb.bytes.items.len); | ||
| 136 | } | ||
| 137 | |||
| 112 | pub fn addCertsFromDirPath( | 138 | pub fn addCertsFromDirPath( |
| 113 | cb: *Bundle, | 139 | cb: *Bundle, |
| 114 | gpa: Allocator, | 140 | gpa: Allocator, |
| ... | @@ -224,6 +250,7 @@ pub fn parseCert(cb: *Bundle, gpa: Allocator, decoded_start: u32, now_sec: i64) | ... | @@ -224,6 +250,7 @@ pub fn parseCert(cb: *Bundle, gpa: Allocator, decoded_start: u32, now_sec: i64) |
| 224 | const builtin = @import("builtin"); | 250 | const builtin = @import("builtin"); |
| 225 | const std = @import("../../std.zig"); | 251 | const std = @import("../../std.zig"); |
| 226 | const assert = std.debug.assert; | 252 | const assert = std.debug.assert; |
| 253 | const os = std.os; | ||
| 227 | const fs = std.fs; | 254 | const fs = std.fs; |
| 228 | const mem = std.mem; | 255 | const mem = std.mem; |
| 229 | const crypto = std.crypto; | 256 | const crypto = std.crypto; |
lib/std/os/windows.zig+1| ... | @@ -28,6 +28,7 @@ pub const user32 = @import("windows/user32.zig"); | ... | @@ -28,6 +28,7 @@ pub const user32 = @import("windows/user32.zig"); |
| 28 | pub const ws2_32 = @import("windows/ws2_32.zig"); | 28 | pub const ws2_32 = @import("windows/ws2_32.zig"); |
| 29 | pub const gdi32 = @import("windows/gdi32.zig"); | 29 | pub const gdi32 = @import("windows/gdi32.zig"); |
| 30 | pub const winmm = @import("windows/winmm.zig"); | 30 | pub const winmm = @import("windows/winmm.zig"); |
| 31 | pub const crypt32 = @import("windows/crypt32.zig"); | ||
| 31 | 32 | ||
| 32 | pub const self_process_handle = @intToPtr(HANDLE, maxInt(usize)); | 33 | pub const self_process_handle = @intToPtr(HANDLE, maxInt(usize)); |
| 33 | 34 |
lib/std/os/windows/crypt32.zig created+54| ... | @@ -0,0 +1,54 @@ | ||
| 1 | const std = @import("../../std.zig"); | ||
| 2 | const windows = std.os.windows; | ||
| 3 | const BOOL = windows.BOOL; | ||
| 4 | const DWORD = windows.DWORD; | ||
| 5 | const BYTE = windows.BYTE; | ||
| 6 | const LPCWSTR = windows.LPCWSTR; | ||
| 7 | const WINAPI = windows.WINAPI; | ||
| 8 | const GetLastError = windows.kernel32.GetLastError; | ||
| 9 | |||
| 10 | pub const CERT_INFO = *opaque {}; | ||
| 11 | pub const HCERTSTORE = *opaque {}; | ||
| 12 | pub const CERT_CONTEXT = extern struct { | ||
| 13 | dwCertEncodingType: DWORD, | ||
| 14 | pbCertEncoded: [*]BYTE, | ||
| 15 | cbCertEncoded: DWORD, | ||
| 16 | pCertInfo: CERT_INFO, | ||
| 17 | hCertStore: HCERTSTORE, | ||
| 18 | }; | ||
| 19 | |||
| 20 | pub extern "crypt32" fn CertOpenSystemStoreW( | ||
| 21 | _: ?*const anyopaque, | ||
| 22 | szSubsystemProtocol: LPCWSTR, | ||
| 23 | ) callconv(WINAPI) ?HCERTSTORE; | ||
| 24 | pub fn certOpenSystemStoreW( | ||
| 25 | hProv: ?*const anyopaque, | ||
| 26 | szSubsystemProtocol: LPCWSTR, | ||
| 27 | ) !HCERTSTORE { | ||
| 28 | const value = CertOpenSystemStoreW(hProv, szSubsystemProtocol); | ||
| 29 | return if (value) |store| | ||
| 30 | store | ||
| 31 | else switch (GetLastError()) { | ||
| 32 | .FILE_NOT_FOUND => error.FileNotFound, | ||
| 33 | else => |err| windows.unexpectedError(err), | ||
| 34 | }; | ||
| 35 | } | ||
| 36 | |||
| 37 | pub extern "crypt32" fn CertCloseStore( | ||
| 38 | hCertStore: HCERTSTORE, | ||
| 39 | dwFlags: DWORD, | ||
| 40 | ) callconv(WINAPI) BOOL; | ||
| 41 | pub fn certCloseStore( | ||
| 42 | hCertStore: HCERTSTORE, | ||
| 43 | dwFlags: DWORD, | ||
| 44 | ) !void { | ||
| 45 | const value = CertCloseStore(hCertStore, dwFlags); | ||
| 46 | if (value == 0) { | ||
| 47 | return windows.unexpectedError(GetLastError()); | ||
| 48 | } | ||
| 49 | } | ||
| 50 | |||
| 51 | pub extern "crypt32" fn CertEnumCertificatesInStore( | ||
| 52 | hCertStore: HCERTSTORE, | ||
| 53 | pPrevCertContext: ?*CERT_CONTEXT, | ||
| 54 | ) callconv(WINAPI) ?*CERT_CONTEXT; | ||