-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient_hello.go
More file actions
386 lines (359 loc) · 10.8 KB
/
client_hello.go
File metadata and controls
386 lines (359 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
package ech
import (
"fmt"
"slices"
"strings"
"golang.org/x/crypto/cryptobyte"
)
// The Client Hello message is specified in RFC 8446 Section 4.1.2
type clientHello struct {
LegacyVersion uint16
Random []uint8
LegacySessionID []byte
CipherSuite []byte
LegacyCompressionMethods []byte
Extensions []extension
ServerName string
ALPNProtos []string
hasECHOuterExtensions bool
tls13 bool
echExt *echExt
}
// The ECH Extension as specified in Section 5 of RFC 9849.
type echExt struct {
Type uint8
CipherSuite CipherSuite
ConfigID uint8
Enc []byte
Payload []byte
}
func (c clientHello) String() string {
var b strings.Builder
fmt.Fprintf(&b, "LegacyVersion: 0x%04x\n", c.LegacyVersion)
fmt.Fprintf(&b, "Random: 0x%x\n", c.Random)
fmt.Fprintf(&b, "LegacySessionID: 0x%x\n", c.LegacySessionID)
fmt.Fprintf(&b, "CipherSuite: 0x%x\n", c.CipherSuite)
fmt.Fprintf(&b, "LegacyCompressionMethods: 0x%x\n", c.LegacyCompressionMethods)
fmt.Fprintf(&b, "Extensions:\n")
for _, ext := range c.Extensions {
fmt.Fprintf(&b, " %s(%d): 0x%X (%d bytes)\n", extensionName(ext.Type), ext.Type, ext.Data, len(ext.Data))
}
if c.echExt != nil {
fmt.Fprintf(&b, "ECH Type: 0x%02x\n", c.echExt.Type)
if c.echExt.Type == 0 {
fmt.Fprintf(&b, "ECH CipherSuite: KDF 0x%04x AEAD 0x%04x\n", c.echExt.CipherSuite.KDF, c.echExt.CipherSuite.AEAD)
fmt.Fprintf(&b, "ECH ConfigID: 0x%02x\n", c.echExt.ConfigID)
fmt.Fprintf(&b, "ECH Enc: 0x%x\n", c.echExt.Enc)
fmt.Fprintf(&b, "ECH Payload: 0x%x\n", c.echExt.Payload)
}
}
return b.String()
}
type extension struct {
Type uint16
Data []byte
}
func (c *clientHello) Marshal() ([]byte, error) {
return c.marshal(false)
}
func (c *clientHello) marshalAAD() ([]byte, error) {
m, err := c.marshal(true)
if err != nil {
return nil, err
}
return m[9:], nil
}
func (c *clientHello) marshal(aad bool) ([]byte, error) {
b := cryptobyte.NewBuilder(nil)
b.AddUint8(0x16)
b.AddUint16(c.LegacyVersion)
b.AddUint16LengthPrefixed(func(b *cryptobyte.Builder) {
b.AddUint8(0x01)
b.AddUint24LengthPrefixed(func(b *cryptobyte.Builder) {
b.AddUint16(c.LegacyVersion)
b.AddBytes(c.Random)
b.AddUint8LengthPrefixed(func(b *cryptobyte.Builder) {
b.AddBytes(c.LegacySessionID)
})
b.AddUint16LengthPrefixed(func(b *cryptobyte.Builder) {
b.AddBytes(c.CipherSuite)
})
b.AddUint8LengthPrefixed(func(b *cryptobyte.Builder) {
b.AddBytes(c.LegacyCompressionMethods)
})
b.AddUint16LengthPrefixed(func(b *cryptobyte.Builder) {
for _, ext := range c.Extensions {
b.AddUint16(ext.Type)
b.AddUint16LengthPrefixed(func(b *cryptobyte.Builder) {
if aad && ext.Type == 0xfe0d {
n := len(ext.Data) - len(c.echExt.Payload)
b.AddBytes(ext.Data[:n])
b.AddBytes(make([]byte, len(ext.Data[n:])))
return
}
b.AddBytes(ext.Data)
})
}
})
})
})
return b.Bytes()
}
func parseClientHello(buf []byte) (*clientHello, error) {
hello := new(clientHello)
// https://datatracker.ietf.org/doc/html/rfc8446#section-4
//
// struct {
// HandshakeType msg_type; /* handshake type */
// uint24 length; /* remaining bytes in message */
// select (Handshake.msg_type) {
// case client_hello: ClientHello;
// ...
// };
// } Handshake;
s := cryptobyte.String(buf)
var msgType uint8
if !s.ReadUint8(&msgType) { // msg_type(1)
return nil, ErrDecodeError
}
if msgType != 0x01 { // ClientHello
return nil, fmt.Errorf("%w: msg_type 0x%x != 0x01", ErrUnexpectedMessage, msgType)
}
var ss cryptobyte.String
if !s.ReadUint24LengthPrefixed(&ss) {
return nil, ErrDecodeError
}
zeros := s
s = ss
// https://datatracker.ietf.org/doc/html/rfc8446#section-4.1.2
// ClientHello
// uint16 ProtocolVersion;
// opaque Random[32];
//
// uint8 CipherSuite[2]; /* Cryptographic suite selector */
//
// struct {
// ProtocolVersion legacy_version = 0x0303; /* TLS v1.2 */
// Random random;
// opaque legacy_session_id<0..32>;
// CipherSuite cipher_suites<2..2^16-2>;
// opaque legacy_compression_methods<1..2^8-1>;
// Extension extensions<8..2^16-1>;
// } ClientHello;
if !s.ReadUint16(&hello.LegacyVersion) { // legacy_version
return nil, ErrDecodeError
}
if !s.ReadBytes(&hello.Random, 32) { // random
return nil, ErrDecodeError
}
var v cryptobyte.String
if !s.ReadUint8LengthPrefixed(&v) { // legacy_session_id
return nil, ErrDecodeError
}
hello.LegacySessionID = slices.Clone(v)
if !s.ReadUint16LengthPrefixed(&v) { // cipher_suites
return nil, ErrDecodeError
}
hello.CipherSuite = slices.Clone(v)
if !s.ReadUint8LengthPrefixed(&v) { // legacy_compression_methods
return nil, ErrDecodeError
}
hello.LegacyCompressionMethods = slices.Clone(v)
//if len(hello.LegacyCompressionMethods) != 1 || hello.LegacyCompressionMethods[0] != 0x0 {
// return nil, ErrIllegalParameter
//}
var extensions cryptobyte.String
if !s.ReadUint16LengthPrefixed(&extensions) {
return nil, ErrDecodeError
}
// https://datatracker.ietf.org/doc/html/rfc8446#section-4.2
// Extensions
//
// struct {
// ExtensionType extension_type;
// opaque extension_data<0..2^16-1>;
// } Extension;
//
// enum {
// server_name(0), /* RFC 6066 */
// ...
// application_layer_protocol_negotiation(16), /* RFC 7301 */
// ...
// } ExtensionType;
for !extensions.Empty() {
var extType uint16
var data cryptobyte.String
if !extensions.ReadUint16(&extType) || !extensions.ReadUint16LengthPrefixed(&data) {
return nil, ErrDecodeError
}
hello.Extensions = append(hello.Extensions, extension{
Type: extType,
Data: slices.Clone(data),
})
}
if err := hello.parseExtensions(); err != nil {
return nil, err
}
if hello.echExt != nil && hello.echExt.Type == 1 {
for _, p := range zeros {
if p != 0 {
return nil, ErrIllegalParameter
}
}
}
return hello, nil
}
func (c *clientHello) parseExtensions() error {
c.ServerName = ""
c.ALPNProtos = nil
c.hasECHOuterExtensions = false
c.tls13 = false
c.echExt = nil
for _, ext := range c.Extensions {
data := cryptobyte.String(ext.Data)
switch ext.Type {
case 0:
// https://datatracker.ietf.org/doc/html/rfc6066#section-3
// Server Name Indication
//
// struct {
// NameType name_type;
// select (name_type) {
// case host_name: HostName;
// } name;
// } ServerName;
//
// enum {
// host_name(0), (255)
// } NameType;
//
// opaque HostName<1..2^16-1>;
//
// struct {
// ServerName server_name_list<1..2^16-1>
// } ServerNameList;
var serverNameList cryptobyte.String
if !data.ReadUint16LengthPrefixed(&serverNameList) {
return fmt.Errorf("%w: serverNameList", ErrDecodeError)
}
for !serverNameList.Empty() {
var nameType uint8
var hostName cryptobyte.String
if !serverNameList.ReadUint8(&nameType) {
return fmt.Errorf("%w: name type", ErrDecodeError)
}
if nameType != 0 { // host name
return fmt.Errorf("%w: invalid nametype 0x%x", ErrIllegalParameter, nameType)
}
if !serverNameList.ReadUint16LengthPrefixed(&hostName) || c.ServerName != "" {
return fmt.Errorf("%w: host name", ErrDecodeError)
}
c.ServerName = string(hostName)
}
case 16:
// https://datatracker.ietf.org/doc/html/rfc7301#section-3
// Application-Layer Protocol Negotiation
//
// enum {
// application_layer_protocol_negotiation(16), (65535)
// } ExtensionType;
//
// The "extension_data" field of the
// ("application_layer_protocol_negotiation(16)") extension SHALL
// contain a "ProtocolNameList" value.
//
// opaque ProtocolName<1..2^8-1>;
//
// struct {
// ProtocolName protocol_name_list<2..2^16-1>
// } ProtocolNameList;
var protocolNameList cryptobyte.String
if !data.ReadUint16LengthPrefixed(&protocolNameList) {
return fmt.Errorf("%w: protocol name list", ErrDecodeError)
}
for !protocolNameList.Empty() {
var protocolName cryptobyte.String
if !protocolNameList.ReadUint8LengthPrefixed(&protocolName) {
return fmt.Errorf("%w: protocol name", ErrDecodeError)
}
c.ALPNProtos = append(c.ALPNProtos, string(protocolName))
}
case 43:
// struct {
// select (Handshake.msg_type) {
// case client_hello:
// ProtocolVersion versions<2..254>;
// case server_hello: /* and HelloRetryRequest */
// ProtocolVersion selected_version;
// };
// } SupportedVersions;
var versions cryptobyte.String
if !data.ReadUint8LengthPrefixed(&versions) {
return fmt.Errorf("%w: supported versions", ErrDecodeError)
}
for !versions.Empty() {
var v uint16
if !versions.ReadUint16(&v) {
return fmt.Errorf("%w: version", ErrDecodeError)
}
if v >= 0x0304 {
c.tls13 = true
}
}
case 0xfd00:
c.hasECHOuterExtensions = true
case 0xfe0d:
// RFC 9849
// 5. The "encrypted_client_hello" Extension
//
// enum {
// encrypted_client_hello(0xfe0d), (65535)
// } ExtensionType;
// enum { outer(0), inner(1) } ECHClientHelloType;
//
// struct {
// ECHClientHelloType type;
// select (ECHClientHello.type) {
// case outer:
// HpkeSymmetricCipherSuite cipher_suite;
// uint8 config_id;
// opaque enc<0..2^16-1>;
// opaque payload<1..2^16-1>;
// case inner:
// Empty;
// };
// } ECHClientHello;
c.echExt = &echExt{}
if !data.ReadUint8(&c.echExt.Type) { // type
return fmt.Errorf("%w: ech type", ErrDecodeError)
}
// Section 7
// If ECHClientHello.type is not a valid ECHClientHelloType, then the
// server MUST abort with an "illegal_parameter" alert.
if c.echExt.Type > 1 {
return fmt.Errorf("%w: ech type %d", ErrIllegalParameter, c.echExt.Type)
}
if c.echExt.Type == 0 { // Outer
if !data.ReadUint16(&c.echExt.CipherSuite.KDF) { // cipher_suite.kdf
return fmt.Errorf("%w: ech ext kdf", ErrDecodeError)
}
if !data.ReadUint16(&c.echExt.CipherSuite.AEAD) { // cipher_suite.aead
return fmt.Errorf("%w: ech ext aead", ErrDecodeError)
}
if !data.ReadUint8(&c.echExt.ConfigID) { // config_id
return fmt.Errorf("%w: ech ext config id", ErrDecodeError)
}
var v cryptobyte.String
if !data.ReadUint16LengthPrefixed(&v) { // enc
return fmt.Errorf("%w: ech ext enc", ErrDecodeError)
}
c.echExt.Enc = slices.Clone(v)
if !data.ReadUint16LengthPrefixed(&v) { // payload
return fmt.Errorf("%w: ech ext payload", ErrDecodeError)
}
c.echExt.Payload = slices.Clone(v)
}
}
}
return nil
}