-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathechconfig.go
334 lines (283 loc) · 7.93 KB
/
echconfig.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
package goech
import (
"bytes"
"encoding"
"encoding/base64"
"encoding/binary"
"fmt"
"github.com/cloudflare/circl/hpke"
"github.com/cloudflare/circl/kem"
"golang.org/x/crypto/cryptobyte"
)
type HpkeSymmetricCipherSuite struct {
KDF hpke.KDF
AEAD hpke.AEAD
}
func (cipherSuite HpkeSymmetricCipherSuite) String() string {
return fmt.Sprintf("{(cipher) kdf: %s, aead: %s}", KDFMapping[cipherSuite.KDF], AEADMapping[cipherSuite.AEAD])
}
type ECHConfig struct {
PublicKey kem.PublicKey
Version uint16
ConfigID uint8
RawPublicName []byte
KEM hpke.KEM
CipherSuites []HpkeSymmetricCipherSuite
MaxNameLength uint8
RawExtensions []byte
}
func (ech ECHConfig) String() string {
return fmt.Sprintf("{(conf) version: %d, config_id: %d, domain: %s, max_len: %d, kem: %v, cipher_suites: %v}", ech.Version, ech.ConfigID, string(ech.RawPublicName), ech.MaxNameLength, KemMapping[ech.KEM], ech.CipherSuites)
}
func (ech *ECHConfig) Equal(other *ECHConfig) bool {
if ech.KEM != other.KEM {
return false
}
echPublicKey, err := ech.PublicKey.MarshalBinary()
if err != nil {
return false
}
otherPublicKey, err := other.PublicKey.MarshalBinary()
if err != nil {
return false
}
if len(ech.CipherSuites) != len(other.CipherSuites) {
return false
}
for i := range ech.CipherSuites {
if ech.CipherSuites[i] != other.CipherSuites[i] {
return false
}
}
return ech.Version == other.Version && ech.ConfigID == other.ConfigID && ech.MaxNameLength == other.MaxNameLength && bytes.Equal(ech.RawPublicName, other.RawPublicName) && bytes.Equal(echPublicKey, otherPublicKey) && bytes.Equal(ech.RawExtensions, other.RawExtensions)
}
var (
_ encoding.BinaryMarshaler = ECHConfig{}
_ encoding.BinaryUnmarshaler = (*ECHConfig)(nil)
)
func (ech ECHConfig) marshalBinaryOnlyConfig(b *cryptobyte.Builder) error {
pk, err := ech.PublicKey.MarshalBinary()
if err != nil {
return err
}
if l := len(ech.RawPublicName); l == 0 || l > 255 {
return InvalidPublicNameLenError(l)
}
b.AddUint16(ech.Version)
b.AddUint16LengthPrefixed(func(child *cryptobyte.Builder) {
child.AddUint8(ech.ConfigID)
child.AddUint16(uint16(ech.KEM))
child.AddUint16(uint16(len(pk)))
child.AddBytes(pk)
child.AddUint16LengthPrefixed(func(child *cryptobyte.Builder) {
for _, cipherSuite := range ech.CipherSuites {
child.AddUint16(uint16(cipherSuite.KDF))
child.AddUint16(uint16(cipherSuite.AEAD))
}
})
child.AddUint8(ech.MaxNameLength)
child.AddUint8(uint8(len(ech.RawPublicName)))
child.AddBytes(ech.RawPublicName)
child.AddUint16(uint16(len(ech.RawExtensions)))
child.AddBytes(ech.RawExtensions)
})
return nil
}
func (ech ECHConfig) MarshalBinary() ([]byte, error) {
var (
b cryptobyte.Builder
err error
)
b.AddUint16LengthPrefixed(func(child *cryptobyte.Builder) {
err = ech.marshalBinaryOnlyConfig(child)
})
if err != nil {
return nil, err
}
return b.Bytes()
}
func (ech *ECHConfig) unmarshalBinaryConfigOnly(data []byte) error {
var content cryptobyte.String
b := cryptobyte.String(data)
if !b.ReadUint16(&ech.Version) {
return ErrInvalidLen
}
if ech.Version != DraftTLSESNI16 {
return ErrNotSupportedVersion
}
if !b.ReadUint16LengthPrefixed(&content) || !b.Empty() {
return ErrInvalidLen
}
var t cryptobyte.String
var pk []byte
if !content.ReadUint8(&ech.ConfigID) ||
!content.ReadUint16((*uint16)(&ech.KEM)) ||
!content.ReadUint16LengthPrefixed(&t) ||
!t.ReadBytes(&pk, len(t)) ||
!content.ReadUint16LengthPrefixed(&t) ||
len(t)%4 != 0 { // the length of (KDFs and AEADs) must be divisible by 4
return ErrInvalidLen
}
if !ech.KEM.IsValid() {
return InvalidKEMError(ech.KEM)
}
var err error
if ech.PublicKey, err = ech.KEM.Scheme().UnmarshalBinaryPublicKey(pk); err != nil {
return fmt.Errorf("parsing public_key: %w", err)
}
ech.CipherSuites = nil // each time you unmarshal you allocate a new CipherSuites
for !t.Empty() {
var hpkeKDF, hpkeAEAD uint16
if !t.ReadUint16(&hpkeKDF) || !t.ReadUint16(&hpkeAEAD) {
// we have already checked that the length is divisible by 4
panic("this must not happen")
}
if !hpke.KDF(hpkeKDF).IsValid() {
return InvalidKDFError(hpkeKDF)
}
if !hpke.AEAD(hpkeAEAD).IsValid() {
return InvalidAEADError(hpkeAEAD)
}
ech.CipherSuites = append(ech.CipherSuites, HpkeSymmetricCipherSuite{KDF: hpke.KDF(hpkeKDF), AEAD: hpke.AEAD(hpkeAEAD)})
}
if !content.ReadUint8(&ech.MaxNameLength) ||
!content.ReadUint8LengthPrefixed(&t) ||
!t.ReadBytes(&ech.RawPublicName, len(t)) ||
!content.ReadUint16LengthPrefixed(&t) ||
!t.ReadBytes(&ech.RawExtensions, len(t)) ||
!content.Empty() {
return ErrInvalidLen
}
return nil
}
func (ech *ECHConfig) UnmarshalBinary(data []byte) error {
b := cryptobyte.String(data)
var t cryptobyte.String
if !b.ReadUint16LengthPrefixed(&t) || !b.Empty() {
return ErrInvalidLen
}
return ech.unmarshalBinaryConfigOnly(t)
}
type ECHConfigList []ECHConfig
func (configs ECHConfigList) Equal(other ECHConfigList) bool {
if len(configs) != len(other) {
return false
}
for i := range configs {
if !configs[i].Equal(&other[i]) {
return false
}
}
return true
}
var (
_ encoding.BinaryMarshaler = (ECHConfigList)(nil)
_ encoding.BinaryUnmarshaler = (*ECHConfigList)(nil)
)
func (configs ECHConfigList) MarshalBinary() ([]byte, error) {
var (
b cryptobyte.Builder
err error
)
b.AddUint16LengthPrefixed(func(child *cryptobyte.Builder) {
for _, echConfig := range configs {
err = echConfig.marshalBinaryOnlyConfig(child)
if err != nil {
break
}
}
})
if err != nil {
return nil, err
}
return b.Bytes()
}
func (configs *ECHConfigList) UnmarshalBinary(data []byte) error {
*configs = (*configs)[:0] // here we are using the cap only, if there was a previous list, Unmarshal is gonna write over it
var (
err error
config ECHConfig
t cryptobyte.String
)
s := cryptobyte.String(data)
if !s.ReadUint16LengthPrefixed(&t) || !s.Empty() {
return ErrInvalidLen
}
for !t.Empty() {
if len(t) < 4 {
return ErrInvalidLen
}
length := int(binary.BigEndian.Uint16(t[2:4]))
if len(t) < length+4 {
return ErrInvalidLen
}
err = config.unmarshalBinaryConfigOnly(t[:length+4])
if err != nil {
return err
}
if !t.Skip(length + 4) {
return ErrInvalidLen
}
*configs = append(*configs, config)
}
return nil
}
func (configs ECHConfigList) ToBase64() (string, error) {
data, err := configs.MarshalBinary()
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(data), nil
}
func (configs ECHConfigList) ToBase64OrPanic() string {
echConfigListBase64, err := configs.ToBase64()
if err != nil {
panic(err)
}
return echConfigListBase64
}
func (configs *ECHConfigList) FromBase64(echConfigListBase64 string) error {
data, err := base64.StdEncoding.DecodeString(echConfigListBase64)
if err != nil {
return err
}
return configs.UnmarshalBinary(data)
}
func (ech ECHConfig) ToBase64() (string, error) {
data, err := ech.MarshalBinary()
if err != nil {
return "", nil
}
return base64.StdEncoding.EncodeToString(data), nil
}
func (ech ECHConfig) ToBase64OrPanic() string {
echConfigBase64, err := ech.ToBase64()
if err != nil {
panic(err)
}
return echConfigBase64
}
func (ech *ECHConfig) FromBase64(echConfigBase64 string) error {
data, err := base64.StdEncoding.DecodeString(echConfigBase64)
if err != nil {
return err
}
return ech.UnmarshalBinary(data)
}
func MarshalECHConfigList(configs []ECHConfig) ([]byte, error) {
return ECHConfigList(configs).MarshalBinary()
}
func MarshalECHConfigArgs(configs ...ECHConfig) ([]byte, error) {
return MarshalECHConfigList(configs)
}
func UnmarshalECHConfigList(data []byte) (configList ECHConfigList, err error) {
err = configList.UnmarshalBinary(data)
return configList, err
}
func ECHConfigListFromBase64(echConfigListBase64 string) (ECHConfigList, error) {
data, err := base64.StdEncoding.DecodeString(echConfigListBase64)
if err != nil {
return nil, err
}
return UnmarshalECHConfigList(data)
}