-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcipher_suite_test.go
More file actions
264 lines (232 loc) · 7.16 KB
/
Copy pathcipher_suite_test.go
File metadata and controls
264 lines (232 loc) · 7.16 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
// SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
package dtls
import (
"context"
"testing"
"time"
"github.com/pion/dtls/v3/internal/ciphersuite"
dtlserrors "github.com/pion/dtls/v3/internal/errors"
dtlsnet "github.com/pion/dtls/v3/pkg/net"
"github.com/pion/dtls/v3/pkg/protocol"
"github.com/pion/transport/v4/dpipe"
"github.com/pion/transport/v4/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestCipherSuiteName(t *testing.T) {
testCases := []struct {
suite CipherSuiteID
expected string
}{
{TLS_AES_128_GCM_SHA256, "TLS_AES_128_GCM_SHA256"},
{TLS_AES_256_GCM_SHA384, "TLS_AES_256_GCM_SHA384"},
{TLS_CHACHA20_POLY1305_SHA256, "TLS_CHACHA20_POLY1305_SHA256"},
{TLS_ECDHE_ECDSA_WITH_AES_128_CCM, "TLS_ECDHE_ECDSA_WITH_AES_128_CCM"},
{TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256"},
{TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256"},
{TLS_PSK_WITH_CHACHA20_POLY1305_SHA256, "TLS_PSK_WITH_CHACHA20_POLY1305_SHA256"},
{CipherSuiteID(0x0000), "0x0000"},
}
for _, testCase := range testCases {
assert.Equal(t, testCase.expected, CipherSuiteName(testCase.suite))
}
}
func TestAllCipherSuites(t *testing.T) {
assert.NotEmpty(t, allCipherSuites())
}
func TestInsecureCipherSuites(t *testing.T) {
assert.Empty(t, InsecureCipherSuites(), "Expected no insecure ciphersuites")
}
func TestCipherSuites(t *testing.T) {
ours := allCipherSuites()
theirs := CipherSuites()
assert.Equal(t, len(ours), len(theirs))
for i, s := range ours {
t.Run(s.String(), func(t *testing.T) {
cipher := theirs[i]
assert.Equal(t, cipher.ID, uint16(s.ID()))
assert.Equal(t, cipher.Name, s.String())
assert.Equal(t, cipherSuiteSupportedVersionIDs(s.ID()), cipher.SupportedVersions)
assert.False(t, cipher.Insecure, "Expected Insecure")
})
}
}
func TestCipherSuiteSupportedVersions(t *testing.T) {
testCases := []struct {
name string
suite CipherSuiteID
expected []protocol.Version
}{
{
name: "TLS 1.3",
suite: TLS_AES_128_GCM_SHA256,
expected: []protocol.Version{protocol.Version1_3},
},
{
name: "DTLS 1.2",
suite: TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
expected: []protocol.Version{protocol.Version1_2},
},
{
name: "custom suites default to DTLS 1.2",
suite: 0xffff,
expected: []protocol.Version{protocol.Version1_2},
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
assert.Equal(t, testCase.expected, cipherSuiteSupportedVersions(testCase.suite))
})
}
}
func TestParseCipherSuitesForVersions(t *testing.T) {
t.Run("default DTLS 1.2", func(t *testing.T) {
suites, err := parseCipherSuitesForVersions(
nil,
nil,
true,
false,
protocol.Version1_2,
protocol.Version1_2,
)
require.NoError(t, err)
require.NotEmpty(t, suites)
for _, suite := range suites {
assert.True(t, cipherSuiteIDSupportsVersion(suite.ID(), protocol.Version1_2))
assert.False(t, cipherSuiteIDSupportsVersion(suite.ID(), protocol.Version1_3))
}
})
t.Run("default DTLS 1.3", func(t *testing.T) {
suites, err := parseCipherSuitesForVersions(
nil,
nil,
true,
false,
protocol.Version1_3,
protocol.Version1_3,
)
require.NoError(t, err)
require.Equal(t, []uint16{
uint16(TLS_AES_128_GCM_SHA256),
uint16(TLS_AES_256_GCM_SHA384),
uint16(TLS_CHACHA20_POLY1305_SHA256),
}, cipherSuiteIDs(suites))
})
t.Run("default dual stack", func(t *testing.T) {
suites, err := parseCipherSuitesForVersions(
nil,
nil,
true,
false,
protocol.Version1_2,
protocol.Version1_3,
)
require.NoError(t, err)
require.Greater(t, len(suites), len(defaultCipherSuites13()))
assert.Equal(t, uint16(TLS_AES_128_GCM_SHA256), cipherSuiteIDs(suites)[0])
assert.Contains(t, cipherSuiteIDs(suites), uint16(TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256))
})
t.Run("selected suites are filtered by version", func(t *testing.T) {
suites, err := parseCipherSuitesForVersions(
[]CipherSuiteID{
TLS_AES_128_GCM_SHA256,
TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
},
nil,
true,
false,
protocol.Version1_2,
protocol.Version1_2,
)
require.NoError(t, err)
require.Equal(t, []uint16{uint16(TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)}, cipherSuiteIDs(suites))
})
t.Run("selected suite must match version", func(t *testing.T) {
_, err := parseCipherSuitesForVersions(
[]CipherSuiteID{TLS_AES_128_GCM_SHA256},
nil,
true,
false,
protocol.Version1_2,
protocol.Version1_2,
)
require.ErrorIs(t, err, dtlserrors.ErrNoAvailableCertificateCipherSuite)
})
t.Run("TLS 1.3 suites are authentication neutral", func(t *testing.T) {
suites, err := parseCipherSuitesForVersions(
[]CipherSuiteID{TLS_AES_128_GCM_SHA256},
nil,
false,
true,
protocol.Version1_3,
protocol.Version1_3,
)
require.NoError(t, err)
require.Equal(t, []uint16{uint16(TLS_AES_128_GCM_SHA256)}, cipherSuiteIDs(suites))
})
t.Run("custom anonymous suites do not satisfy PSK configs", func(t *testing.T) {
_, err := parseCipherSuitesForVersions(
[]CipherSuiteID{},
func() []CipherSuite {
return []CipherSuite{&testCustomCipherSuite{authenticationType: CipherSuiteAuthenticationTypeAnonymous}}
},
false,
true,
protocol.Version1_2,
protocol.Version1_2,
)
require.ErrorIs(t, err, dtlserrors.ErrNoAvailablePSKCipherSuite)
})
}
// CustomCipher that is just used to assert Custom IDs work.
type testCustomCipherSuite struct {
ciphersuite.TLSEcdheEcdsaWithAes128GcmSha256
authenticationType CipherSuiteAuthenticationType
}
func (t *testCustomCipherSuite) ID() CipherSuiteID {
return 0xFFFF
}
func (t *testCustomCipherSuite) AuthenticationType() CipherSuiteAuthenticationType {
return t.authenticationType
}
// Assert that two connections that pass in a CipherSuite with a CustomID works.
func TestCustomCipherSuite(t *testing.T) {
type result struct {
c *Conn
err error
}
// Check for leaking routines
report := test.CheckRoutines(t)
defer report()
runTest := func(cipherFactory func() []CipherSuite) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
ca, cb := dpipe.Pipe()
resultCh := make(chan result)
go func() {
client, err := testClient(ctx, dtlsnet.PacketConnFromConn(ca), ca.RemoteAddr(), []ClientOption{
WithCustomCipherSuites(cipherFactory),
}, true)
resultCh <- result{client, err}
}()
server, err := testServer(ctx, dtlsnet.PacketConnFromConn(cb), cb.RemoteAddr(), []ServerOption{
WithCustomCipherSuites(cipherFactory),
}, true)
clientResult := <-resultCh
assert.NoError(t, err)
assert.NoError(t, server.Close())
assert.Nil(t, clientResult.err)
assert.NoError(t, clientResult.c.Close())
}
t.Run("Custom ID", func(*testing.T) {
runTest(func() []CipherSuite {
return []CipherSuite{&testCustomCipherSuite{authenticationType: CipherSuiteAuthenticationTypeCertificate}}
})
})
t.Run("Anonymous Cipher", func(*testing.T) {
runTest(func() []CipherSuite {
return []CipherSuite{&testCustomCipherSuite{authenticationType: CipherSuiteAuthenticationTypeAnonymous}}
})
})
}