lib/quic/src/tls/engine/model.zig
daab053ee43316e1809a84551d573ddd1e5bf3d2
1 const std = @import("std");
2 const tls = @import("../root.zig");
3
4 pub const Role = enum { client, server };
5 pub const Level = enum { initial, handshake, one_rtt };
6 pub const Direction = enum { read, write };
7 pub const Secret = tls.schedule.Secret;
8
9 pub const FixedRandom = struct {
10 hello: [32]u8,
11 key_exchange: [32]u8,
12 };
13
14 pub const Random = union(enum) {
15 secure: std.Io,
16 fixed_for_testing: FixedRandom,
17
18 /// Builds a random source whose bytes the caller fixed in advance. The engine takes the hello
19 /// random and the key exchange secret from it, so a handshake built this way repeats exactly. A
20 /// test uses this form, and the other form of the source takes its bytes from the caller's I/O
21 /// interface through a secure-random call.
22 pub fn testing(value: FixedRandom) Random {
23 return .{ .fixed_for_testing = value };
24 }
25
26 pub fn values(self: Random) error{RandomFailed}!FixedRandom {
27 return switch (self) {
28 .fixed_for_testing => |value| value,
29 .secure => |io| blk: {
30 var value: FixedRandom = undefined;
31 io.randomSecure(&value.hello) catch return error.RandomFailed;
32 io.randomSecure(&value.key_exchange) catch return error.RandomFailed;
33 break :blk value;
34 },
35 };
36 }
37 };
38
39 pub const Config = struct {
40 role: Role,
41 identity: tls.Identity,
42 expected_peer: ?tls.PublicKey,
43 alpn: []const u8,
44 transport_parameters: []const u8,
45 server_name: ?[]const u8,
46 random: Random,
47 cipher_suite: std.crypto.tls.CipherSuite = .AES_128_GCM_SHA256,
48 };
49
50 /// How far the handshake has reached: still running, complete, or confirmed. A client passes
51 /// through completion first and reaches confirmation when the caller confirms it, which also erases
52 /// the handshake traffic secrets. A server reports confirmation directly, once the client's
53 /// Finished message verifies. A caller therefore treats a server's confirmation as both completion
54 /// and confirmation.
55 pub const Status = enum {
56 handshaking,
57 handshake_complete,
58 handshake_confirmed,
59 };
60
61 pub const Peer = struct {
62 public_key: tls.PublicKey,
63 alpn: []const u8,
64 transport_parameters: []const u8,
65 };