mirror of
https://github.com/ziglang/zig.git
synced 2025-12-07 06:43:07 +00:00
clean up windows cert scanning
* keep helper functions out of the DLL bindings APIs * unify the logic for linux and windows certificate scanning with regards to error handling
This commit is contained in:
parent
1f9fa82235
commit
09560bc69a
@ -111,25 +111,29 @@ pub fn rescanWindows(cb: *Bundle, gpa: Allocator) !void {
|
|||||||
cb.bytes.clearRetainingCapacity();
|
cb.bytes.clearRetainingCapacity();
|
||||||
cb.map.clearRetainingCapacity();
|
cb.map.clearRetainingCapacity();
|
||||||
|
|
||||||
const store = try os.windows.crypt32.certOpenSystemStoreW(null, &[4:0]u16{ 'R', 'O', 'O', 'T' });
|
const w = std.os.windows;
|
||||||
defer os.windows.crypt32.certCloseStore(store, 0) catch unreachable;
|
const GetLastError = w.kernel32.GetLastError;
|
||||||
|
const root = [4:0]u16{ 'R', 'O', 'O', 'T' };
|
||||||
var ctx = os.windows.crypt32.CertEnumCertificatesInStore(store, null);
|
const store = w.crypt32.CertOpenSystemStoreW(null, &root) orelse switch (GetLastError()) {
|
||||||
while (ctx) |context| : (ctx = os.windows.crypt32.CertEnumCertificatesInStore(store, ctx)) {
|
.FILE_NOT_FOUND => return error.FileNotFound,
|
||||||
var start = @intCast(u32, cb.bytes.items.len);
|
else => |err| return w.unexpectedError(err),
|
||||||
try cb.bytes.appendSlice(gpa, context.pbCertEncoded[0..context.cbCertEncoded]);
|
|
||||||
var parsed = Certificate.parse(.{
|
|
||||||
.buffer = cb.bytes.items,
|
|
||||||
.index = start,
|
|
||||||
}) catch {
|
|
||||||
cb.bytes.items.len = start;
|
|
||||||
continue;
|
|
||||||
};
|
};
|
||||||
const gop = try cb.map.getOrPutContext(gpa, parsed.subject_slice, .{ .cb = cb });
|
defer _ = w.crypt32.CertCloseStore(store, 0);
|
||||||
|
|
||||||
|
var ctx = w.crypt32.CertEnumCertificatesInStore(store, null);
|
||||||
|
while (ctx) |context| : (ctx = w.crypt32.CertEnumCertificatesInStore(store, ctx)) {
|
||||||
|
const decoded_start = @intCast(u32, cb.bytes.items.len);
|
||||||
|
const encoded_cert = context.pbCertEncoded[0..context.cbCertEncoded];
|
||||||
|
try cb.bytes.appendSlice(gpa, encoded_cert);
|
||||||
|
const parsed_cert = try Certificate.parse(.{
|
||||||
|
.buffer = cb.bytes.items,
|
||||||
|
.index = decoded_start,
|
||||||
|
});
|
||||||
|
const gop = try cb.map.getOrPutContext(gpa, parsed_cert.subject_slice, .{ .cb = cb });
|
||||||
if (gop.found_existing) {
|
if (gop.found_existing) {
|
||||||
cb.bytes.items.len = start;
|
cb.bytes.items.len = decoded_start;
|
||||||
} else {
|
} else {
|
||||||
gop.value_ptr.* = start;
|
gop.value_ptr.* = decoded_start;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cb.bytes.shrinkAndFree(gpa, cb.bytes.items.len);
|
cb.bytes.shrinkAndFree(gpa, cb.bytes.items.len);
|
||||||
@ -250,7 +254,6 @@ pub fn parseCert(cb: *Bundle, gpa: Allocator, decoded_start: u32, now_sec: i64)
|
|||||||
const builtin = @import("builtin");
|
const builtin = @import("builtin");
|
||||||
const std = @import("../../std.zig");
|
const std = @import("../../std.zig");
|
||||||
const assert = std.debug.assert;
|
const assert = std.debug.assert;
|
||||||
const os = std.os;
|
|
||||||
const fs = std.fs;
|
const fs = std.fs;
|
||||||
const mem = std.mem;
|
const mem = std.mem;
|
||||||
const crypto = std.crypto;
|
const crypto = std.crypto;
|
||||||
|
|||||||
@ -5,7 +5,6 @@ const DWORD = windows.DWORD;
|
|||||||
const BYTE = windows.BYTE;
|
const BYTE = windows.BYTE;
|
||||||
const LPCWSTR = windows.LPCWSTR;
|
const LPCWSTR = windows.LPCWSTR;
|
||||||
const WINAPI = windows.WINAPI;
|
const WINAPI = windows.WINAPI;
|
||||||
const GetLastError = windows.kernel32.GetLastError;
|
|
||||||
|
|
||||||
pub const CERT_INFO = *opaque {};
|
pub const CERT_INFO = *opaque {};
|
||||||
pub const HCERTSTORE = *opaque {};
|
pub const HCERTSTORE = *opaque {};
|
||||||
@ -21,32 +20,11 @@ pub extern "crypt32" fn CertOpenSystemStoreW(
|
|||||||
_: ?*const anyopaque,
|
_: ?*const anyopaque,
|
||||||
szSubsystemProtocol: LPCWSTR,
|
szSubsystemProtocol: LPCWSTR,
|
||||||
) callconv(WINAPI) ?HCERTSTORE;
|
) callconv(WINAPI) ?HCERTSTORE;
|
||||||
pub fn certOpenSystemStoreW(
|
|
||||||
hProv: ?*const anyopaque,
|
|
||||||
szSubsystemProtocol: LPCWSTR,
|
|
||||||
) !HCERTSTORE {
|
|
||||||
const value = CertOpenSystemStoreW(hProv, szSubsystemProtocol);
|
|
||||||
return if (value) |store|
|
|
||||||
store
|
|
||||||
else switch (GetLastError()) {
|
|
||||||
.FILE_NOT_FOUND => error.FileNotFound,
|
|
||||||
else => |err| windows.unexpectedError(err),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
pub extern "crypt32" fn CertCloseStore(
|
pub extern "crypt32" fn CertCloseStore(
|
||||||
hCertStore: HCERTSTORE,
|
hCertStore: HCERTSTORE,
|
||||||
dwFlags: DWORD,
|
dwFlags: DWORD,
|
||||||
) callconv(WINAPI) BOOL;
|
) callconv(WINAPI) BOOL;
|
||||||
pub fn certCloseStore(
|
|
||||||
hCertStore: HCERTSTORE,
|
|
||||||
dwFlags: DWORD,
|
|
||||||
) !void {
|
|
||||||
const value = CertCloseStore(hCertStore, dwFlags);
|
|
||||||
if (value == 0) {
|
|
||||||
return windows.unexpectedError(GetLastError());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub extern "crypt32" fn CertEnumCertificatesInStore(
|
pub extern "crypt32" fn CertEnumCertificatesInStore(
|
||||||
hCertStore: HCERTSTORE,
|
hCertStore: HCERTSTORE,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user