also looking at this
Harden encrypted mail storage, recovery, and forwarding
#8
50 files
+1701
-9001
| @@ -1,67 +0,0 @@ | ||
| 1 | //! Exporting tls key so we can share them with Wireshark and analyze decrypted | |
| 2 | //! traffic in Wireshark. | |
| 3 | //! To configure Wireshark to use exprted keys see curl reference. | |
| 4 | //! | |
| 5 | //! References: | |
| 6 | //! curl: https://everything.curl.dev/usingcurl/tls/sslkeylogfile.html | |
| 7 | //! openssl: https://www.openssl.org/docs/manmaster/man3/SSL_CTX_set_keylog_callback.html | |
| 8 | //! https://udn.realityripple.com/docs/Mozilla/Projects/NSS/Key_Log_Format | |
| 9 | ||
| 10 | const std = @import("std"); | |
| 11 | ||
| 12 | const key_log_file_env = "SSLKEYLOGFILE"; | |
| 13 | ||
| 14 | pub const label = struct { | |
| 15 | // tls 1.3 | |
| 16 | pub const client_handshake_traffic_secret: []const u8 = "CLIENT_HANDSHAKE_TRAFFIC_SECRET"; | |
| 17 | pub const server_handshake_traffic_secret: []const u8 = "SERVER_HANDSHAKE_TRAFFIC_SECRET"; | |
| 18 | pub const client_traffic_secret_0: []const u8 = "CLIENT_TRAFFIC_SECRET_0"; | |
| 19 | pub const server_traffic_secret_0: []const u8 = "SERVER_TRAFFIC_SECRET_0"; | |
| 20 | // tls 1.2 | |
| 21 | pub const client_random: []const u8 = "CLIENT_RANDOM"; | |
| 22 | }; | |
| 23 | ||
| 24 | pub const Callback = *const fn (label: []const u8, client_random: []const u8, secret: []const u8) void; | |
| 25 | ||
| 26 | /// Writes tls keys to the file pointed by SSLKEYLOGFILE environment variable. | |
| 27 | pub fn callback(label_: []const u8, client_random: []const u8, secret: []const u8) void { | |
| 28 | const builtin = @import("builtin"); | |
| 29 | const native_os = builtin.os.tag; | |
| 30 | const file_name: []const u8 = if (native_os == .windows) | |
| 31 | return | |
| 32 | else if (native_os == .wasi and !builtin.link_libc) | |
| 33 | return | |
| 34 | else | |
| 35 | std.posix.getenv(key_log_file_env) orelse return; | |
| 36 | fileAppend(file_name, label_, client_random, secret) catch return; | |
| 37 | } | |
| 38 | ||
| 39 | pub fn fileAppend(file_name: []const u8, label_: []const u8, client_random: []const u8, secret: []const u8) !void { | |
| 40 | var buf: [1024]u8 = undefined; | |
| 41 | const line = try formatLine(&buf, label_, client_random, secret); | |
| 42 | try fileWrite(file_name, line); | |
| 43 | } | |
| 44 | ||
| 45 | fn fileWrite(file_name: []const u8, line: []const u8) !void { | |
| 46 | var path_buf: [std.posix.PATH_MAX]u8 = undefined; | |
| 47 | if (file_name.len >= path_buf.len) return error.NameTooLong; | |
| 48 | @memcpy(path_buf[0..file_name.len], file_name); | |
| 49 | path_buf[file_name.len] = 0; | |
| 50 | const fd = try std.posix.open(path_buf[0..file_name.len :0], .{ .ACCMODE = .WRONLY, .CREAT = true, .APPEND = true }, 0o600); | |
| 51 | defer _ = std.c.close(fd); | |
| 52 | _ = try std.posix.write(fd, line); | |
| 53 | } | |
| 54 | ||
| 55 | pub fn formatLine(buf: []u8, label_: []const u8, client_random: []const u8, secret: []const u8) ![]const u8 { | |
| 56 | var w = std.Io.Writer.fixed(buf); | |
| 57 | try w.print("{s} ", .{label_}); | |
| 58 | for (client_random) |b| { | |
| 59 | try w.print("{x:0>2}", .{b}); | |
| 60 | } | |
| 61 | try w.writeByte(' '); | |
| 62 | for (secret) |b| { | |
| 63 | try w.print("{x:0>2}", .{b}); | |
| 64 | } | |
| 65 | try w.writeByte('\n'); | |
| 66 | return w.buffered(); | |
| 67 | } | |