authorgravatar for michael.dusan@gmail.comMichael Dusan <michael.dusan@gmail.com> 2023-07-02 15:10:06-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-07-02 19:24:37-07:00
logfc9ab5f0e838196e99d1706e4d4e91f3d761d509
treeae8dc64b1a526ad44f1b06f3540eefc117701993
parent689f3163af48fd6e0c08bb76fadfb711db30c97c

tls certificates: support more BSDs

- add support for freebsd, netbsd, dragonfly - refactor rescanOpenBSD -> rescanBSD - make os-specific rescan*() non-public closes #16279

1 files changed, 13 insertions(+), 15 deletions(-)

lib/std/crypto/Certificate/Bundle.zig+13-15
...@@ -50,7 +50,7 @@ pub fn deinit(cb: *Bundle, gpa: Allocator) void {...@@ -50,7 +50,7 @@ pub fn deinit(cb: *Bundle, gpa: Allocator) void {
50 cb.* = undefined;50 cb.* = undefined;
51}51}
5252
53pub const RescanError = RescanLinuxError || RescanMacError || RescanWindowsError;53pub const RescanError = RescanLinuxError || RescanMacError || RescanBSDError || RescanWindowsError;
5454
55/// Clears the set of certificates and then scans the host operating system55/// Clears the set of certificates and then scans the host operating system
56/// file system standard locations for certificates.56/// file system standard locations for certificates.
...@@ -60,18 +60,20 @@ pub fn rescan(cb: *Bundle, gpa: Allocator) RescanError!void {...@@ -60,18 +60,20 @@ pub fn rescan(cb: *Bundle, gpa: Allocator) RescanError!void {
60 switch (builtin.os.tag) {60 switch (builtin.os.tag) {
61 .linux => return rescanLinux(cb, gpa),61 .linux => return rescanLinux(cb, gpa),
62 .macos => return rescanMac(cb, gpa),62 .macos => return rescanMac(cb, gpa),
63 .openbsd => return rescanOpenBSD(cb, gpa),63 .freebsd, .openbsd => return rescanBSD(cb, gpa, "/etc/ssl/cert.pem"),
64 .netbsd => return rescanBSD(cb, gpa, "/etc/openssl/certs/ca-certificates.crt"),
65 .dragonfly => return rescanBSD(cb, gpa, "/usr/local/etc/ssl/cert.pem"),
64 .windows => return rescanWindows(cb, gpa),66 .windows => return rescanWindows(cb, gpa),
65 else => {},67 else => {},
66 }68 }
67}69}
6870
69pub const rescanMac = @import("Bundle/macos.zig").rescanMac;71const rescanMac = @import("Bundle/macos.zig").rescanMac;
70pub const RescanMacError = @import("Bundle/macos.zig").RescanMacError;72const RescanMacError = @import("Bundle/macos.zig").RescanMacError;
7173
72pub const RescanLinuxError = AddCertsFromFilePathError || AddCertsFromDirPathError;74const RescanLinuxError = AddCertsFromFilePathError || AddCertsFromDirPathError;
7375
74pub fn rescanLinux(cb: *Bundle, gpa: Allocator) RescanLinuxError!void {76fn rescanLinux(cb: *Bundle, gpa: Allocator) RescanLinuxError!void {
75 // Possible certificate files; stop after finding one.77 // Possible certificate files; stop after finding one.
76 const cert_file_paths = [_][]const u8{78 const cert_file_paths = [_][]const u8{
77 "/etc/ssl/certs/ca-certificates.crt", // Debian/Ubuntu/Gentoo etc.79 "/etc/ssl/certs/ca-certificates.crt", // Debian/Ubuntu/Gentoo etc.
...@@ -113,22 +115,18 @@ pub fn rescanLinux(cb: *Bundle, gpa: Allocator) RescanLinuxError!void {...@@ -113,22 +115,18 @@ pub fn rescanLinux(cb: *Bundle, gpa: Allocator) RescanLinuxError!void {
113 cb.bytes.shrinkAndFree(gpa, cb.bytes.items.len);115 cb.bytes.shrinkAndFree(gpa, cb.bytes.items.len);
114}116}
115117
116pub const RescanOpenBSDError = AddCertsFromFilePathError;118const RescanBSDError = AddCertsFromFilePathError;
117
118pub fn rescanOpenBSD(cb: *Bundle, gpa: Allocator) RescanOpenBSDError!void {
119 const cert_file_path = "/etc/ssl/cert.pem";
120119
120fn rescanBSD(cb: *Bundle, gpa: Allocator, cert_file_path: []const u8) RescanBSDError!void {
121 cb.bytes.clearRetainingCapacity();121 cb.bytes.clearRetainingCapacity();
122 cb.map.clearRetainingCapacity();122 cb.map.clearRetainingCapacity();
123123 try addCertsFromFilePathAbsolute(cb, gpa, cert_file_path);
124 addCertsFromFilePathAbsolute(cb, gpa, cert_file_path) catch |err| return err;
125
126 cb.bytes.shrinkAndFree(gpa, cb.bytes.items.len);124 cb.bytes.shrinkAndFree(gpa, cb.bytes.items.len);
127}125}
128126
129pub const RescanWindowsError = Allocator.Error || ParseCertError || std.os.UnexpectedError || error{FileNotFound};127const RescanWindowsError = Allocator.Error || ParseCertError || std.os.UnexpectedError || error{FileNotFound};
130128
131pub fn rescanWindows(cb: *Bundle, gpa: Allocator) RescanWindowsError!void {129fn rescanWindows(cb: *Bundle, gpa: Allocator) RescanWindowsError!void {
132 cb.bytes.clearRetainingCapacity();130 cb.bytes.clearRetainingCapacity();
133 cb.map.clearRetainingCapacity();131 cb.map.clearRetainingCapacity();
134132