also looking at this
Harden encrypted mail storage, recovery, and forwarding
#8
50 files
+1701
-9001
| @@ -1,462 +0,0 @@ | ||
| 1 | const std = @import("std"); | |
| 2 | const builtin = @import("builtin"); | |
| 3 | const assert = std.debug.assert; | |
| 4 | const mem = std.mem; | |
| 5 | const Io = std.Io; | |
| 6 | const proto = @import("protocol.zig"); | |
| 7 | const max_ciphertext_len = @import("cipher.zig").max_ciphertext_len; | |
| 8 | ||
| 9 | const log = std.log.scoped(.tls); | |
| 10 | ||
| 11 | pub const header_len = 5; | |
| 12 | ||
| 13 | pub const Record = struct { | |
| 14 | content_type: proto.ContentType, | |
| 15 | protocol_version: proto.Version = .tls_1_2, | |
| 16 | buffer: []const u8, | |
| 17 | header: []const u8, | |
| 18 | payload: []const u8, | |
| 19 | ||
| 20 | pub fn init(buffer: []const u8) Record { | |
| 21 | return .{ | |
| 22 | .content_type = @fromBackingInt(@intCast(buffer[0])), | |
| 23 | .protocol_version = @fromBackingInt(@intCast(mem.readInt(u16, buffer[1..3], .big))), | |
| 24 | .buffer = buffer, | |
| 25 | .header = buffer[0..header_len], | |
| 26 | .payload = buffer[header_len..], | |
| 27 | }; | |
| 28 | } | |
| 29 | ||
| 30 | const Error = error{ | |
| 31 | EndOfStream, // clean close of the stream | |
| 32 | ReadFailed, // all other stream close | |
| 33 | InputBufferUndersize, // input can't fit tls record | |
| 34 | TlsRecordOverflow, // incorrect tls record | |
| 35 | }; | |
| 36 | ||
| 37 | pub fn read(rdr: *Io.Reader) Error!Record { | |
| 38 | if (header_len > rdr.buffer.len) { | |
| 39 | @branchHint(.cold); | |
| 40 | return error.InputBufferUndersize; | |
| 41 | } | |
| 42 | const hdr = try rdr.peek(header_len); | |
| 43 | const payload_len = mem.readInt(u16, hdr[3..5], .big); | |
| 44 | if (payload_len > max_ciphertext_len) { | |
| 45 | @branchHint(.cold); | |
| 46 | return error.TlsRecordOverflow; | |
| 47 | } | |
| 48 | const record_len = header_len + payload_len; | |
| 49 | if (record_len > rdr.buffer.len) { | |
| 50 | @branchHint(.cold); | |
| 51 | return error.InputBufferUndersize; | |
| 52 | } | |
| 53 | return .init(try rdr.take(record_len)); | |
| 54 | } | |
| 55 | ||
| 56 | pub fn decoder(rdr: *Io.Reader) !Decoder { | |
| 57 | const rec = try Record.read(rdr); | |
| 58 | if (@backingInt(rec.protocol_version) != 0x0300 and | |
| 59 | @backingInt(rec.protocol_version) != 0x0301 and | |
| 60 | rec.protocol_version != .tls_1_2) | |
| 61 | return error.TlsBadVersion; | |
| 62 | return .{ | |
| 63 | .content_type = rec.content_type, | |
| 64 | .payload = rec.payload, | |
| 65 | }; | |
| 66 | } | |
| 67 | }; | |
| 68 | ||
| 69 | pub const Decoder = struct { | |
| 70 | content_type: proto.ContentType, | |
| 71 | payload: []const u8, | |
| 72 | idx: usize = 0, | |
| 73 | ||
| 74 | pub fn init(content_type: proto.ContentType, payload: []const u8) Decoder { | |
| 75 | return .{ | |
| 76 | .content_type = content_type, | |
| 77 | .payload = payload, | |
| 78 | }; | |
| 79 | } | |
| 80 | ||
| 81 | pub fn decode(d: *Decoder, comptime T: type) !T { | |
| 82 | switch (@typeInfo(T)) { | |
| 83 | .int => |info| switch (info.bits) { | |
| 84 | 8 => { | |
| 85 | try skip(d, 1); | |
| 86 | return d.payload[d.idx - 1]; | |
| 87 | }, | |
| 88 | 16 => { | |
| 89 | try skip(d, 2); | |
| 90 | const b0: u16 = d.payload[d.idx - 2]; | |
| 91 | const b1: u16 = d.payload[d.idx - 1]; | |
| 92 | return (b0 << 8) | b1; | |
| 93 | }, | |
| 94 | 24 => { | |
| 95 | try skip(d, 3); | |
| 96 | const b0: u24 = d.payload[d.idx - 3]; | |
| 97 | const b1: u24 = d.payload[d.idx - 2]; | |
| 98 | const b2: u24 = d.payload[d.idx - 1]; | |
| 99 | return (b0 << 16) | (b1 << 8) | b2; | |
| 100 | }, | |
| 101 | 32 => { | |
| 102 | try skip(d, 4); | |
| 103 | const b0: u32 = d.payload[d.idx - 4]; | |
| 104 | const b1: u32 = d.payload[d.idx - 3]; | |
| 105 | const b2: u32 = d.payload[d.idx - 2]; | |
| 106 | const b3: u32 = d.payload[d.idx - 1]; | |
| 107 | return (b0 << 24) | (b1 << 16) | (b2 << 8) | b3; | |
| 108 | }, | |
| 109 | else => @compileError("unsupported int type: " ++ @typeName(T)), | |
| 110 | }, | |
| 111 | .@"enum" => |info| { | |
| 112 | const int = try d.decode(info.tag_type); | |
| 113 | if (info.mode == .exhaustive) @compileError("exhaustive enum cannot be used"); | |
| 114 | return @as(T, @fromBackingInt(@intCast(int))); | |
| 115 | }, | |
| 116 | else => @compileError("unsupported type: " ++ @typeName(T)), | |
| 117 | } | |
| 118 | } | |
| 119 | ||
| 120 | pub fn array(d: *Decoder, comptime len: usize) ![len]u8 { | |
| 121 | try d.skip(len); | |
| 122 | return d.payload[d.idx - len ..][0..len].*; | |
| 123 | } | |
| 124 | ||
| 125 | pub fn slice(d: *Decoder, len: usize) ![]const u8 { | |
| 126 | try d.skip(len); | |
| 127 | return d.payload[d.idx - len ..][0..len]; | |
| 128 | } | |
| 129 | ||
| 130 | pub fn skip(d: *Decoder, amt: usize) !void { | |
| 131 | if (d.idx + amt > d.payload.len) return error.TlsDecodeError; | |
| 132 | d.idx += amt; | |
| 133 | } | |
| 134 | ||
| 135 | pub fn rest(d: Decoder) []const u8 { | |
| 136 | return d.payload[d.idx..]; | |
| 137 | } | |
| 138 | ||
| 139 | pub fn eof(d: Decoder) bool { | |
| 140 | return d.idx == d.payload.len; | |
| 141 | } | |
| 142 | ||
| 143 | pub fn expectContentType(d: *Decoder, content_type: proto.ContentType) !void { | |
| 144 | if (d.content_type == content_type) return; | |
| 145 | ||
| 146 | switch (d.content_type) { | |
| 147 | .alert => try d.raiseAlert(), | |
| 148 | else => return error.TlsUnexpectedMessage, | |
| 149 | } | |
| 150 | } | |
| 151 | ||
| 152 | pub fn raiseAlert(d: *Decoder) !void { | |
| 153 | if (d.payload.len < 2) return error.TlsUnexpectedMessage; | |
| 154 | try proto.Alert.parse(try d.array(2)).toError(); | |
| 155 | return error.TlsAlertCloseNotify; | |
| 156 | } | |
| 157 | }; | |
| 158 | ||
| 159 | const testing = std.testing; | |
| 160 | const data12 = @import("testdata/tls12.zig"); | |
| 161 | const testu = @import("testu.zig"); | |
| 162 | const CipherSuite = @import("cipher.zig").CipherSuite; | |
| 163 | ||
| 164 | test "record.read" { | |
| 165 | // expected records in the data12.server_responses | |
| 166 | const expected = [_]struct { | |
| 167 | content_type: proto.ContentType, | |
| 168 | payload_len: usize, | |
| 169 | }{ | |
| 170 | .{ .content_type = .handshake, .payload_len = 49 }, | |
| 171 | .{ .content_type = .handshake, .payload_len = 815 }, | |
| 172 | .{ .content_type = .handshake, .payload_len = 300 }, | |
| 173 | .{ .content_type = .handshake, .payload_len = 4 }, | |
| 174 | .{ .content_type = .change_cipher_spec, .payload_len = 1 }, | |
| 175 | .{ .content_type = .handshake, .payload_len = 64 }, | |
| 176 | }; | |
| 177 | var reader: Io.Reader = .fixed(&data12.server_responses); | |
| 178 | ||
| 179 | { | |
| 180 | var n: usize = 0; | |
| 181 | for (expected) |e| { | |
| 182 | const rec = try Record.read(&reader); | |
| 183 | try testing.expectEqual(e.content_type, rec.content_type); | |
| 184 | try testing.expectEqual(e.payload_len, rec.payload.len); | |
| 185 | try testing.expectEqual(.tls_1_2, rec.protocol_version); | |
| 186 | try testing.expectEqual(e.payload_len + header_len, rec.buffer.len); | |
| 187 | ||
| 188 | n += rec.payload.len + header_len; | |
| 189 | try testing.expectEqual(n, reader.seek); | |
| 190 | } | |
| 191 | try testing.expectEqual(data12.server_responses.len, reader.seek); | |
| 192 | try testing.expectError(error.EndOfStream, Record.read(&reader)); | |
| 193 | } | |
| 194 | ||
| 195 | reader.seek = 0; // rewind | |
| 196 | { // use smaller buffer to force fill, buffer must fit largest record 815 + 5 (header) | |
| 197 | var buffer: [820]u8 = undefined; | |
| 198 | var limited = reader.limited(.unlimited, &buffer); | |
| 199 | for (expected) |e| { | |
| 200 | const rec = try Record.read(&limited.interface); | |
| 201 | try testing.expectEqual(e.content_type, rec.content_type); | |
| 202 | try testing.expectEqual(e.payload_len, rec.payload.len); | |
| 203 | try testing.expectEqual(.tls_1_2, rec.protocol_version); | |
| 204 | } | |
| 205 | try testing.expectError(error.EndOfStream, Record.read(&limited.interface)); | |
| 206 | } | |
| 207 | } | |
| 208 | ||
| 209 | test Decoder { | |
| 210 | var reader: Io.Reader = .fixed(&data12.server_responses); | |
| 211 | var d = (try Record.decoder(&reader)); | |
| 212 | try testing.expectEqual(.handshake, d.content_type); | |
| 213 | ||
| 214 | try testing.expectEqual(.server_hello, try d.decode(proto.Handshake)); | |
| 215 | try testing.expectEqual(45, try d.decode(u24)); // length | |
| 216 | try testing.expectEqual(.tls_1_2, try d.decode(proto.Version)); | |
| 217 | try testing.expectEqualStrings( | |
| 218 | &testu.hexToBytes("707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f"), | |
| 219 | try d.slice(32), | |
| 220 | ); // server random | |
| 221 | try testing.expectEqual(0, try d.decode(u8)); // session id len | |
| 222 | try testing.expectEqual(.ECDHE_RSA_WITH_AES_128_CBC_SHA, try d.decode(CipherSuite)); | |
| 223 | try testing.expectEqual(0, try d.decode(u8)); // compression method | |
| 224 | try testing.expectEqual(5, try d.decode(u16)); // extension length | |
| 225 | try testing.expectEqual(5, d.rest().len); | |
| 226 | try d.skip(5); | |
| 227 | try testing.expect(d.eof()); | |
| 228 | } | |
| 229 | ||
| 230 | pub const Writer = struct { | |
| 231 | inner: Io.Writer, | |
| 232 | ||
| 233 | pub fn initFromIo(io_w: *Io.Writer) Writer { | |
| 234 | return .{ .inner = Io.Writer.fixed(io_w.unusedCapacitySlice()) }; | |
| 235 | } | |
| 236 | ||
| 237 | pub fn init(buffer: []u8) Writer { | |
| 238 | return .{ .inner = Io.Writer.fixed(buffer) }; | |
| 239 | } | |
| 240 | ||
| 241 | fn ensureCapacity(w: Writer, n: usize) !void { | |
| 242 | if (w.inner.unusedCapacityLen() < n) | |
| 243 | return error.OutputBufferUndersize; | |
| 244 | } | |
| 245 | ||
| 246 | pub fn buffered(w: Writer) []const u8 { | |
| 247 | return w.inner.buffered(); | |
| 248 | } | |
| 249 | ||
| 250 | pub fn bytesWritten(w: Writer) usize { | |
| 251 | return w.inner.end; | |
| 252 | } | |
| 253 | ||
| 254 | pub fn pos(w: Writer) usize { | |
| 255 | return w.inner.end; | |
| 256 | } | |
| 257 | ||
| 258 | pub fn byte(w: *Writer, b: u8) !void { | |
| 259 | try w.ensureCapacity(1); | |
| 260 | try w.inner.writeByte(b); | |
| 261 | } | |
| 262 | ||
| 263 | pub fn slice(w: *Writer, bytes: []const u8) !void { | |
| 264 | try w.ensureCapacity(bytes.len); | |
| 265 | try w.inner.writeAll(bytes); | |
| 266 | } | |
| 267 | ||
| 268 | pub fn int(w: *Writer, comptime T: type, value: anytype) !void { | |
| 269 | try w.ensureCapacity(@divExact(@typeInfo(T).int.bits, 8)); | |
| 270 | try w.inner.writeInt(T, @intCast(value), .big); | |
| 271 | } | |
| 272 | ||
| 273 | pub fn writableArray(w: *Writer, comptime len: usize) !*[len]u8 { | |
| 274 | try w.ensureCapacity(len); | |
| 275 | return try w.inner.writableArray(len); | |
| 276 | } | |
| 277 | ||
| 278 | pub fn enumValue(w: *Writer, value: anytype) !void { | |
| 279 | const i = @backingInt(value); | |
| 280 | try w.int(@TypeOf(i), i); | |
| 281 | } | |
| 282 | ||
| 283 | pub fn enumList(w: *Writer, comptime E: type, tags: []const E) !void { | |
| 284 | assert(@sizeOf(E) == 2); | |
| 285 | try w.int(u16, tags.len * 2); | |
| 286 | for (tags) |t| { | |
| 287 | try w.enumValue(t); | |
| 288 | } | |
| 289 | } | |
| 290 | ||
| 291 | /// Default extension writer, writes extension type and list of tags | |
| 292 | pub fn extension(w: *Writer, ex: proto.Extension, tags: anytype) !void { | |
| 293 | try w.enumValue(ex); | |
| 294 | if (ex == .supported_versions) { | |
| 295 | try w.int(u16, tags.len * 2 + 1); | |
| 296 | try w.int(u8, tags.len * 2); | |
| 297 | } else if (ex == .psk_key_exchange_modes) { | |
| 298 | try w.int(u16, tags.len + 1); | |
| 299 | try w.int(u8, tags.len); | |
| 300 | } else { | |
| 301 | try w.int(u16, tags.len * 2 + 2); | |
| 302 | try w.int(u16, tags.len * 2); | |
| 303 | } | |
| 304 | for (tags) |t| { | |
| 305 | try w.enumValue(t); | |
| 306 | } | |
| 307 | } | |
| 308 | ||
| 309 | /// Key share extension | |
| 310 | pub fn keyShare(w: *Writer, named_groups: []const proto.NamedGroup, keys: []const []const u8) !void { | |
| 311 | if (keys.len == 0) return; | |
| 312 | assert(named_groups.len == keys.len); | |
| 313 | try w.enumValue(proto.Extension.key_share); | |
| 314 | var l: usize = 0; | |
| 315 | for (keys) |key| { | |
| 316 | l += key.len + 4; | |
| 317 | } | |
| 318 | try w.int(u16, l + 2); | |
| 319 | try w.int(u16, l); | |
| 320 | for (named_groups, 0..) |ng, i| { | |
| 321 | const key = keys[i]; | |
| 322 | try w.enumValue(ng); | |
| 323 | try w.int(u16, key.len); | |
| 324 | try w.inner.writeAll(key); | |
| 325 | } | |
| 326 | } | |
| 327 | ||
| 328 | /// Server name extension | |
| 329 | pub fn serverName(w: *Writer, host: []const u8) !void { | |
| 330 | const host_len: u16 = @intCast(host.len); | |
| 331 | try w.enumValue(proto.Extension.server_name); | |
| 332 | try w.int(u16, host_len + 5); // byte length of extension payload | |
| 333 | try w.int(u16, host_len + 3); // server_name_list byte count | |
| 334 | try w.inner.writeByte(0); // name type | |
| 335 | try w.int(u16, host_len); | |
| 336 | try w.inner.writeAll(host); | |
| 337 | } | |
| 338 | ||
| 339 | /// Writes header of the pre shared key extension, without binders | |
| 340 | pub fn preSharedKey( | |
| 341 | w: *Writer, | |
| 342 | identity: []const u8, | |
| 343 | obfuscated_age: u32, | |
| 344 | binder_len: u8, | |
| 345 | ) !void { | |
| 346 | try w.enumValue(proto.Extension.pre_shared_key); | |
| 347 | try w.int(u16, identity.len + binder_len + 4 + 2 + 2); | |
| 348 | try w.int(u16, identity.len + 4 + 2); | |
| 349 | try w.int(u16, identity.len); | |
| 350 | try w.inner.writeAll(identity); | |
| 351 | try w.int(u32, obfuscated_age); | |
| 352 | } | |
| 353 | ||
| 354 | /// Writes the rest of the pre shared keys extension | |
| 355 | pub fn preSharedKeyBinder(w: *Writer, binder: []const u8) !void { | |
| 356 | try w.int(u16, binder.len + 1); | |
| 357 | try w.int(u8, binder.len); | |
| 358 | try w.inner.writeAll(binder); | |
| 359 | } | |
| 360 | ||
| 361 | /// tls record | |
| 362 | pub fn record(w: *Writer, content_type: proto.ContentType, payload: []const u8) !void { | |
| 363 | try w.enumValue(content_type); | |
| 364 | try w.enumValue(proto.Version.tls_1_2); | |
| 365 | try w.int(u16, payload.len); | |
| 366 | if (w.unused().ptr == payload.ptr) { | |
| 367 | w.inner.advance(payload.len); | |
| 368 | } else { | |
| 369 | try w.slice(payload); | |
| 370 | } | |
| 371 | } | |
| 372 | ||
| 373 | pub fn recordHeader(w: *Writer, content_type: proto.ContentType, payload_len: usize) !void { | |
| 374 | try w.enumValue(content_type); | |
| 375 | try w.enumValue(proto.Version.tls_1_2); | |
| 376 | try w.int(u16, payload_len); | |
| 377 | } | |
| 378 | ||
| 379 | /// tls handshake record | |
| 380 | pub fn handshakeRecord(w: *Writer, handshake_type: proto.Handshake, payload: []const u8) !void { | |
| 381 | try w.enumValue(handshake_type); | |
| 382 | try w.int(u24, payload.len); | |
| 383 | if (w.unused().ptr == payload.ptr) { | |
| 384 | w.inner.advance(payload.len); | |
| 385 | } else { | |
| 386 | try w.slice(payload); | |
| 387 | } | |
| 388 | } | |
| 389 | ||
| 390 | pub fn handshakeRecordHeader(w: *Writer, handshake_type: proto.Handshake, payload_len: usize) !void { | |
| 391 | try w.enumValue(handshake_type); | |
| 392 | try w.int(u24, payload_len); | |
| 393 | } | |
| 394 | ||
| 395 | pub fn unused(w: *Writer) []u8 { | |
| 396 | return w.inner.unusedCapacitySlice(); | |
| 397 | } | |
| 398 | ||
| 399 | pub fn advance(w: *Writer, n: usize) void { | |
| 400 | w.inner.advance(n); | |
| 401 | } | |
| 402 | ||
| 403 | /// Skip `n` bytes from current position and return position before skip. | |
| 404 | /// Used with writerAt to later return to that point and write skipped | |
| 405 | /// bytes. | |
| 406 | pub fn skip(w: *Writer, n: usize) !usize { | |
| 407 | try w.ensureCapacity(n); | |
| 408 | const p = w.pos(); | |
| 409 | w.inner.advance(n); | |
| 410 | return p; | |
| 411 | } | |
| 412 | ||
| 413 | /// Returns writer at some previous position | |
| 414 | /// Parent buffer position is not changed. | |
| 415 | pub fn writerAt(w: *Writer, p: usize) Writer { | |
| 416 | assert(p < w.inner.end); | |
| 417 | return Writer{ .inner = Io.Writer.fixed(w.inner.buffered()[p..]) }; | |
| 418 | } | |
| 419 | ||
| 420 | /// Returns new writer in unused buffer part advancing `n` bytes. | |
| 421 | /// Parent buffer position is not changed. | |
| 422 | pub fn writerAdvance(w: *Writer, n: usize) !Writer { | |
| 423 | try w.ensureCapacity(n); | |
| 424 | return Writer{ .inner = Io.Writer.fixed(w.inner.unusedCapacitySlice()[n..]) }; | |
| 425 | } | |
| 426 | }; | |
| 427 | ||
| 428 | test "Writer" { | |
| 429 | var buf: [16]u8 = undefined; | |
| 430 | var w = Writer{ .inner = Io.Writer.fixed(&buf) }; | |
| 431 | ||
| 432 | try w.slice("ab"); | |
| 433 | try w.enumValue(proto.Curve.named_curve); | |
| 434 | try w.enumValue(proto.NamedGroup.x25519); | |
| 435 | try w.int(u16, 0x1234); | |
| 436 | try testing.expectEqualSlices(u8, &[_]u8{ 'a', 'b', 0x03, 0x00, 0x1d, 0x12, 0x34 }, w.buffered()); | |
| 437 | } | |
| 438 | ||
| 439 | fn int2(int: u16) [2]u8 { | |
| 440 | var arr: [2]u8 = undefined; | |
| 441 | std.mem.writeInt(u16, &arr, int, .big); | |
| 442 | return arr; | |
| 443 | } | |
| 444 | ||
| 445 | fn int3(int: u24) [3]u8 { | |
| 446 | var arr: [3]u8 = undefined; | |
| 447 | std.mem.writeInt(u24, &arr, int, .big); | |
| 448 | return arr; | |
| 449 | } | |
| 450 | ||
| 451 | pub fn header(content_type: proto.ContentType, payload_len: usize) [header_len]u8 { | |
| 452 | return [1]u8{@backingInt(content_type)} ++ | |
| 453 | int2(@backingInt(proto.Version.tls_1_2)) ++ | |
| 454 | int2(@intCast(payload_len)); | |
| 455 | } | |
| 456 | ||
| 457 | pub fn handshakeHeader(handshake_type: proto.Handshake, payload_len: usize) [4]u8 { | |
| 458 | var ret: [4]u8 = undefined; | |
| 459 | ret[0] = @backingInt(handshake_type); | |
| 460 | std.mem.writeInt(u24, ret[1..4], @intCast(payload_len), .big); | |
| 461 | return ret; | |
| 462 | } | |