-
Notifications
You must be signed in to change notification settings - Fork 325
Expand file tree
/
Copy pathu_parrots_test.go
More file actions
154 lines (133 loc) · 4.12 KB
/
u_parrots_test.go
File metadata and controls
154 lines (133 loc) · 4.12 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
package tls
import (
"bytes"
"net"
"testing"
)
type incrementingSource struct {
next byte
}
func (s *incrementingSource) Read(b []byte) (int, error) {
for i := range b {
b[i] = s.next
s.next++
}
return len(b), nil
}
func findKeyShareExtension(t *testing.T, exts []TLSExtension) *KeyShareExtension {
t.Helper()
for _, ext := range exts {
if keyShareExt, ok := ext.(*KeyShareExtension); ok {
return keyShareExt
}
}
t.Fatal("key_share extension not found")
return nil
}
func findKeyShareData(t *testing.T, keyShareExt *KeyShareExtension, group CurveID) []byte {
t.Helper()
for _, keyShare := range keyShareExt.KeyShares {
if keyShare.Group == group {
return keyShare.Data
}
}
t.Fatalf("key_share for group %v not found", group)
return nil
}
func newTestUConnWithIncrementingRand() *UConn {
return UClient(&net.TCPConn{}, &Config{
ServerName: "example.com",
Rand: &incrementingSource{},
}, HelloCustom)
}
func fingerprintsWithHybridClassicalKeyShareReuse() []ClientHelloID {
return []ClientHelloID{
HelloFirefox_148,
}
}
func TestParrotFingerprintsReuseHybridClassicalKeyShare(t *testing.T) {
for _, helloID := range fingerprintsWithHybridClassicalKeyShareReuse() {
t.Run(helloID.Str(), func(t *testing.T) {
spec, err := UTLSIdToSpec(helloID)
if err != nil {
t.Fatalf("unexpected error creating %s spec: %v", helloID.Str(), err)
}
uconn := newTestUConnWithIncrementingRand()
if err := uconn.ApplyPreset(&spec); err != nil {
t.Fatalf("unexpected error applying %s spec: %v", helloID.Str(), err)
}
keyShareExt := findKeyShareExtension(t, uconn.Extensions)
hybridData := findKeyShareData(t, keyShareExt, X25519MLKEM768)
classicalData := findKeyShareData(t, keyShareExt, X25519)
if len(hybridData) < x25519PublicKeySize {
t.Fatalf("hybrid keyshare is too short: got %d bytes", len(hybridData))
}
hybridClassicalPart := hybridData[len(hybridData)-x25519PublicKeySize:]
if !bytes.Equal(hybridClassicalPart, classicalData) {
t.Fatalf("expected %s to reuse classical keyshare: hybrid classical part != X25519 keyshare", helloID.Str())
}
keys := uconn.HandshakeState.State13.KeyShareKeys
if keys == nil || keys.MlkemEcdhe == nil || keys.Ecdhe == nil {
t.Fatal("expected both hybrid and classical ECDHE private keys to be set")
}
if keys.MlkemEcdhe != keys.Ecdhe {
t.Fatalf("expected %s hybrid/classical keyshares to reuse the same ECDHE private key", helloID.Str())
}
})
}
}
func TestHybridClassicalKeySharesAreIndependentByDefault(t *testing.T) {
spec := ClientHelloSpec{
TLSVersMin: VersionTLS12,
TLSVersMax: VersionTLS13,
CipherSuites: []uint16{
TLS_AES_128_GCM_SHA256,
},
CompressionMethods: []uint8{compressionNone},
Extensions: []TLSExtension{
&SupportedCurvesExtension{
Curves: []CurveID{
X25519MLKEM768,
X25519,
},
},
&KeyShareExtension{
KeyShares: []KeyShare{
{
Group: X25519MLKEM768,
},
{
Group: X25519,
},
},
},
&SupportedVersionsExtension{
Versions: []uint16{
VersionTLS13,
VersionTLS12,
},
},
},
}
uconn := newTestUConnWithIncrementingRand()
if err := uconn.ApplyPreset(&spec); err != nil {
t.Fatalf("unexpected error applying independent keyshare spec: %v", err)
}
keyShareExt := findKeyShareExtension(t, uconn.Extensions)
hybridData := findKeyShareData(t, keyShareExt, X25519MLKEM768)
classicalData := findKeyShareData(t, keyShareExt, X25519)
if len(hybridData) < x25519PublicKeySize {
t.Fatalf("hybrid keyshare is too short: got %d bytes", len(hybridData))
}
hybridClassicalPart := hybridData[len(hybridData)-x25519PublicKeySize:]
if bytes.Equal(hybridClassicalPart, classicalData) {
t.Fatalf("expected independent keyshares by default: hybrid classical part == X25519 keyshare")
}
keys := uconn.HandshakeState.State13.KeyShareKeys
if keys == nil || keys.MlkemEcdhe == nil || keys.Ecdhe == nil {
t.Fatal("expected both hybrid and classical ECDHE private keys to be set")
}
if keys.MlkemEcdhe == keys.Ecdhe {
t.Fatal("expected independent keyshares by default: hybrid/classical ECDHE private keys should differ")
}
}