-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeys.go
418 lines (334 loc) · 11.5 KB
/
keys.go
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
package esni
import (
"bytes"
"crypto/sha256"
"encoding/binary"
"encoding/hex"
"fmt"
"strings"
"time"
"github.com/pkg/errors"
)
var (
// ErrChecksumMismatch is returned during unmarshalling
// of a ESNI Keys record when the body of the record
// doesn't match the checksum included in the record
ErrChecksumMismatch = errors.New("calculated checksum did not match received checksum")
)
// Keys represents a ENSIKeys record used
// to specify information to be used to encrypt
// an SNI with a specific server
type Keys struct {
// Version specifies the ESNI specification version
// the Keys record conforms too
Version Version
// Checksum is the first 4 bytes of a SHA-256
// sum of the binary Keys record, this field
// is ignored during marshalling
Checksum [4]byte
// PublicName specifies the clear text SNI that
// should be utilized during the TLS handshake
// to allow for intermediate servers to handle
// the request before being forwarded to the backend
// server
PublicName string
// Keys defines a list of individual
// public keys that are permitted to
// be used for generating the shared
// encryption secret
Keys KeyShareEntryList
// CipherSuites defines a list of cipher
// suites that are permitted to be used
// for the encryption of the SNI
CipherSuites []CipherSuite
// PaddedLength specifies the required length
// of the encrypted SNI, if the SNI is smaller
// than the padded length it must be combined
// with extra zero data to match the padded
// length before encryption
PaddedLength uint16
// NotBefore specifies the time at which the
// keys in this record are valid for use
NotBefore time.Time
// NotAfter specifies the time at which the keys
// in this record are no longer valid of use
NotAfter time.Time
// Extensions specifies a list of extensions
// to the ESNI specification to provide extra
// information to the client
Extensions ExtensionList
}
// String returns a friendly representation
// of the information stored in this structure
func (keys *Keys) String() string {
var builder strings.Builder
builder.WriteString("{")
_, _ = fmt.Fprintf(&builder, "Version:%s, ", keys.Version)
_, _ = fmt.Fprintf(&builder, "Checksum:%s, ", hex.EncodeToString(keys.Checksum[:]))
if keys.Version >= VersionDraft03 {
_, _ = fmt.Fprintf(&builder, "PublicName:%s, ", keys.PublicName)
}
_, _ = fmt.Fprintf(&builder, "Keys:%s, ", keys.Keys)
_, _ = fmt.Fprintf(&builder, "CipherSuites:%s, ", keys.CipherSuites)
_, _ = fmt.Fprintf(&builder, "PaddedLength:%d, ", keys.PaddedLength)
_, _ = fmt.Fprintf(&builder, "NotBefore:%s, ", keys.NotBefore)
_, _ = fmt.Fprintf(&builder, "NotAfter:%s, ", keys.NotAfter)
_, _ = fmt.Fprintf(&builder, "Extensions:%s", keys.Extensions)
builder.WriteString("}")
return builder.String()
}
// MarshalBinary will attempt to marshal the contents
// of the Keys record into a binary format specified
// by the ESNI specification
func (keys Keys) MarshalBinary() ([]byte, error) {
var data bytes.Buffer
if err := binary.Write(&data, binary.BigEndian, keys.Version); err != nil {
return nil, errors.Wrap(err, "write version")
}
if _, err := data.Write([]byte{0x0, 0x0, 0x0, 0x0}); err != nil {
return nil, errors.Wrap(err, "write empty checksum")
}
if err := keys.marshalPublicName(&data); err != nil {
return nil, errors.Wrap(err, "marshal public name")
}
if err := keys.marshalKeyShareList(&data); err != nil {
return nil, errors.Wrap(err, "marshal key share list")
}
if err := keys.marshalCipherSuites(&data); err != nil {
return nil, errors.Wrap(err, "marshal cipher suite list")
}
if err := binary.Write(&data, binary.BigEndian, keys.PaddedLength); err != nil {
return nil, errors.Wrap(err, "write padded length")
}
if err := keys.marshalValidityPeriod(&data); err != nil {
return nil, errors.Wrap(err, "marshal validity period")
}
if err := keys.marshalExtensions(&data); err != nil {
return nil, errors.Wrap(err, "marshal extensions list")
}
final := data.Bytes()
sum := sha256.Sum256(final)
copy(final[2:6], sum[:4])
return final, nil
}
// UnmarshalBinary will attempt to unmarshal and parse
// information about a Keys record from the binary data
// provided
func (keys *Keys) UnmarshalBinary(b []byte) error {
keys.Version = Version(binary.BigEndian.Uint16(b[0:]))
copy(keys.Checksum[:], b[2:])
copy(b[2:], []byte{0x00, 0x00, 0x00, 0x00})
sum := sha256.Sum256(b)
if bytes.Compare(keys.Checksum[:], sum[:4]) != 0 {
return ErrChecksumMismatch
}
reader := bytes.NewReader(b[6:])
if err := keys.unmarshalPublicName(reader); err != nil {
return errors.Wrap(err, "unmarshal public name")
}
if err := keys.unmarshalKeyShareList(reader); err != nil {
return errors.Wrap(err, "unmarshal key share list")
}
if err := keys.unmarshalCipherSuites(reader); err != nil {
return errors.Wrap(err, "unmarshal cipher suite list")
}
if err := binary.Read(reader, binary.BigEndian, &keys.PaddedLength); err != nil {
return errors.Wrap(err, "read padded length")
}
if err := keys.unmarshalValidityPeriod(reader); err != nil {
return errors.Wrap(err, "unmarshal validity period")
}
if err := keys.unmarshalExtensions(reader); err != nil {
return errors.Wrap(err, "unmarshal extensions list")
}
return nil
}
// marshalPublicName will write the length of
// the public name field along with the value
// of the field
func (keys Keys) marshalPublicName(data *bytes.Buffer) error {
// TODO(lh): Once the ESNI specific leaves draft
// status this will need to be removed
// as it will most likely be mandatory
// for all versions
if keys.Version < VersionDraft03 {
return nil
}
if len(keys.PublicName) == 0 {
return errors.New("public name is empty")
} else if len(keys.PublicName) > 255 {
return errors.New("public name is too large")
}
if err := data.WriteByte(uint8(len(keys.PublicName))); err != nil {
return errors.Wrap(err, "write public name length")
}
if _, err := data.WriteString(keys.PublicName); err != nil {
return errors.Wrap(err, "write public name")
}
return nil
}
// unmarshalPublicName will read the length of
// the public name and attempt to read the public
// name
func (keys *Keys) unmarshalPublicName(reader *bytes.Reader) error {
// TODO(lh): Once the ESNI specific leaves draft
// status this will need to be removed
// as it will most likely be mandatory
// for all versions
if keys.Version < VersionDraft03 {
return nil
}
nameLength, err := reader.ReadByte()
if err != nil {
return errors.Wrap(err, "read length")
}
if nameLength == 0 {
return errors.New("public name is empty")
}
name := make([]byte, nameLength)
if _, err := reader.Read(name); err != nil {
return err
}
keys.PublicName = string(name)
return nil
}
// marshalKeyShareList will write the binary length
// of the entry list and marshal the list to binary
// format, writing it to the buffer
func (keys Keys) marshalKeyShareList(data *bytes.Buffer) error {
if len(keys.Keys) == 0 {
return errors.New("key share list is empty")
}
if err := binary.Write(data, binary.BigEndian, keys.Keys.Size()); err != nil {
return errors.Wrap(err, "write key share list size")
}
listData, err := keys.Keys.MarshalBinary()
if err != nil {
return err
}
if _, err := data.Write(listData); err != nil {
return errors.Wrap(err, "write key share list")
}
return nil
}
// unmarshalKeyShareList will read the length of the
// entry list and attempt to unmarshal a KeyShareEntryList
// from the read data
func (keys *Keys) unmarshalKeyShareList(reader *bytes.Reader) error {
var listLen uint16
if err := binary.Read(reader, binary.BigEndian, &listLen); err != nil {
return errors.Wrap(err, "read key share list size")
}
if listLen == 0 {
return errors.New("key share list is empty")
}
data := make([]byte, listLen)
if _, err := reader.Read(data); err != nil {
return errors.Wrap(err, "read key share list")
}
keys.Keys = make(KeyShareEntryList, 0)
if err := keys.Keys.UnmarshalBinary(data); err != nil {
return err
}
return nil
}
// marshalCipherSuites will write the binary size
// of the cipher suite list and unique identifier
// for each supported cipher suite
func (keys Keys) marshalCipherSuites(data *bytes.Buffer) error {
if err := binary.Write(data, binary.BigEndian, uint16(len(keys.CipherSuites)*2)); err != nil {
return errors.Wrap(err, "write cipher suite list size")
}
for i := range keys.CipherSuites {
if err := binary.Write(data, binary.BigEndian, keys.CipherSuites[i]); err != nil {
return errors.Wrap(err, "write cipher suite")
}
}
return nil
}
// unmarshalCipherSuites will read the binary length
// of the cipher suite list and will read each individual
// cipher
func (keys *Keys) unmarshalCipherSuites(reader *bytes.Reader) error {
var suitesLen uint16
if err := binary.Read(reader, binary.BigEndian, &suitesLen); err != nil {
return errors.Wrap(err, "read cipher suite list size")
}
if suitesLen%2 != 0 {
return errors.New("invalid cipher suite list size")
}
keys.CipherSuites = make([]CipherSuite, suitesLen/2)
for i := range keys.CipherSuites {
var suite uint16
if err := binary.Read(reader, binary.BigEndian, &suite); err != nil {
return errors.Wrapf(err, "read cipher suite %d", i)
}
keys.CipherSuites[i] = CipherSuite(suite)
}
return nil
}
// marshalValidityPeriod will write the not before
// and not after fields as uint64 binary variables
func (keys Keys) marshalValidityPeriod(data *bytes.Buffer) error {
if err := binary.Write(data, binary.BigEndian, uint64(keys.NotBefore.Unix())); err != nil {
return errors.Wrap(err, "write not before")
}
if err := binary.Write(data, binary.BigEndian, uint64(keys.NotAfter.Unix())); err != nil {
return errors.Wrap(err, "write not after")
}
return nil
}
// unmarshalValidityPeriod will read the not before
// and not after fields from the binary data
func (keys *Keys) unmarshalValidityPeriod(reader *bytes.Reader) error {
var notBefore, notAfter uint64
if err := binary.Read(reader, binary.BigEndian, ¬Before); err != nil {
return errors.Wrap(err, "read not before")
}
if err := binary.Read(reader, binary.BigEndian, ¬After); err != nil {
return errors.Wrap(err, "read not after")
}
keys.NotBefore = time.Unix(int64(notBefore), 0)
keys.NotAfter = time.Unix(int64(notAfter), 0)
return nil
}
// marshalExtensions will write the binary size of
// the extensions list and will marshal the list to
// binary format, writing it to the buffer
func (keys *Keys) marshalExtensions(data *bytes.Buffer) error {
if err := binary.Write(data, binary.BigEndian, keys.Extensions.Size()); err != nil {
return errors.Wrap(err, "write extensions list length")
}
if len(keys.Extensions) == 0 {
return nil
}
extsData, err := keys.Extensions.MarshalBinary()
if err != nil {
return err
}
if _, err := data.Write(extsData); err != nil {
return errors.Wrap(err, "write extensions list")
}
return nil
}
// unmarshalExtensions will read the binary length of
// the extensions list and will attempt to unmarshal
// a ExtensionList from that data
func (keys *Keys) unmarshalExtensions(reader *bytes.Reader) error {
var extsLen uint16
if err := binary.Read(reader, binary.BigEndian, &extsLen); err != nil {
return errors.Wrap(err, "read extensions list length")
}
if extsLen == 0 {
return nil
}
extsData := make([]byte, extsLen)
if _, err := reader.Read(extsData); err != nil {
return errors.Wrap(err, "read extensions list")
}
keys.Extensions = make(ExtensionList, 0)
if err := keys.Extensions.UnmarshalBinary(extsData); err != nil {
return err
}
return nil
}