| 1 | TLS Syntax |
| 2 | ========== |
| 3 | |
| 4 | TLS defines [its own syntax](https://tlswg.github.io/tls13-spec/#rfc.section.3) |
| 5 | for describing structures used in that protocol. To facilitate experimentation |
| 6 | with TLS in Go, this module maps that syntax to the Go structure syntax, taking |
| 7 | advantage of Go's type annotations to encode non-type information carried in the |
| 8 | TLS presentation format. |
| 9 | |
| 10 | For example, in the TLS specification, a ClientHello message has the following |
| 11 | structure: |
| 12 | |
| 13 | ~~~~~ |
| 14 | uint16 ProtocolVersion; |
| 15 | opaque Random[32]; |
| 16 | uint8 CipherSuite[2]; |
| 17 | enum { ... (65535)} ExtensionType; |
| 18 | |
| 19 | struct { |
| 20 | ExtensionType extension_type; |
| 21 | opaque extension_data<0..2^16-1>; |
| 22 | } Extension; |
| 23 | |
| 24 | struct { |
| 25 | ProtocolVersion legacy_version = 0x0303; /* TLS v1.2 */ |
| 26 | Random random; |
| 27 | opaque legacy_session_id<0..32>; |
| 28 | CipherSuite cipher_suites<2..2^16-2>; |
| 29 | opaque legacy_compression_methods<1..2^8-1>; |
| 30 | Extension extensions<0..2^16-1>; |
| 31 | } ClientHello; |
| 32 | ~~~~~ |
| 33 | |
| 34 | This maps to the following Go type definitions: |
| 35 | |
| 36 | ~~~~~ |
| 37 | type protocolVersion uint16 |
| 38 | type random [32]byte |
| 39 | type cipherSuite uint16 // or [2]byte |
| 40 | type extensionType uint16 |
| 41 | |
| 42 | type extension struct { |
| 43 | ExtensionType extensionType |
| 44 | ExtensionData []byte `tls:"head=2"` |
| 45 | } |
| 46 | |
| 47 | type clientHello struct { |
| 48 | LegacyVersion protocolVersion |
| 49 | Random random |
| 50 | LegacySessionID []byte `tls:"head=1,max=32"` |
| 51 | CipherSuites []cipherSuite `tls:"head=2,min=2"` |
| 52 | LegacyCompressionMethods []byte `tls:"head=1,min=1"` |
| 53 | Extensions []extension `tls:"head=2"` |
| 54 | } |
| 55 | ~~~~~ |
| 56 | |
| 57 | Then you can just declare, marshal, and unmarshal structs just like you would |
| 58 | with, say JSON. |
| 59 | |
| 60 | The available annotations right now are all related to vectors: |
| 61 | |
| 62 | * `head`: The number of bytes of length to use as a "header" |
| 63 | * `min`: The minimum length of the vector, in bytes |
| 64 | * `max`: The maximum length of the vector, in bytes |
| 65 | |
| 66 | ## Not supported |
| 67 | |
| 68 | * The `select()` syntax for creating alternate version of the same struct (see, |
| 69 | e.g., the KeyShare extension) |
| 70 | |
| 71 | * The backreference syntax for array lengths or select parameters, as in `opaque |
| 72 | fragment[TLSPlaintext.length]`. Note, however, that in cases where the length |
| 73 | immediately preceds the array, these can be reframed as vectors with |
| 74 | appropriate sizes. |
| 75 | |
| 76 | |
| 77 | QUIC Extensions Syntax |
| 78 | ====================== |
| 79 | syntax also supports some minor extensions to allow implementing QUIC. |
| 80 | |
| 81 | * The `varint` annotation describes a QUIC-style varint |
| 82 | * `head=none` means no header, i.e., the bytes are encoded directly on the wire. |
| 83 | On reading, the decoder will consume all available data. |
| 84 | * `head=varint` means to encode the header as a varint |