-
Notifications
You must be signed in to change notification settings - Fork 1
/
resumetls_test.go
362 lines (326 loc) · 9.31 KB
/
resumetls_test.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
package resumetls
import (
"bytes"
"crypto/tls"
"net"
"testing"
)
var cert = `-----BEGIN CERTIFICATE-----
MIIDADCCAeigAwIBAgIRAMlZFfrjDjpriu1r+XIr1kwwDQYJKoZIhvcNAQELBQAw
EjEQMA4GA1UEChMHQWNtZSBDbzAeFw0xODA4MDIxMTI0MTlaFw0xOTA4MDIxMTI0
MTlaMBIxEDAOBgNVBAoTB0FjbWUgQ28wggEiMA0GCSqGSIb3DQEB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
3HZOGPOxSTPQZe4cNqwA97kCgYBfVvYk+rPwDsW3LtZOIQKg1NpLymeV2swtmeZ6
TKp5AZvbWHgarmJqIoCuQD7UPuV9KRPUqNey4rRChuV2Cb0xxQZVPsDgPBcmWQL2
KzYGY/rEJ0CUvgeJaOMzHPXzUOisKX9wiBYYEcXBEEk4Hx4cRcM9O/VyMcuVLFaG
dFARiQKBgQCHKrb0SzYVnaEWFR+GP+sJMfxrhq/N8m+WcCpoQ/UIvguMWmFKtVtC
WVTd3XNizIpuNpDgGI4qvIwmEs7UhAzemxasYoP3y3FO2dT0QGC+T1SX/BsW6AiO
fi06KUiLh/4rJtf2wph2wN8SPAY4yQkopFlDYTJNmhhYsKTGIhrpww==
-----END RSA PRIVATE KEY-----`
var ciphers = []struct {
name string
ciphers []uint16
}{
{
name: "DEFAULT",
ciphers: nil,
},
{
name: "TLS_RSA_WITH_RC4_128_SHA",
ciphers: []uint16{tls.TLS_RSA_WITH_RC4_128_SHA},
},
{
name: "TLS_RSA_WITH_3DES_EDE_CBC_SHA",
ciphers: []uint16{tls.TLS_RSA_WITH_3DES_EDE_CBC_SHA},
},
{
name: "TLS_RSA_WITH_AES_128_CBC_SHA",
ciphers: []uint16{tls.TLS_RSA_WITH_AES_128_CBC_SHA},
},
{
name: "TLS_RSA_WITH_AES_256_CBC_SHA",
ciphers: []uint16{tls.TLS_RSA_WITH_AES_256_CBC_SHA},
},
{
name: "TLS_RSA_WITH_AES_128_CBC_SHA256",
ciphers: []uint16{tls.TLS_RSA_WITH_AES_128_CBC_SHA256},
},
{
name: "TLS_RSA_WITH_AES_128_GCM_SHA256",
ciphers: []uint16{tls.TLS_RSA_WITH_AES_128_GCM_SHA256},
},
{
name: "TLS_RSA_WITH_AES_256_GCM_SHA384",
ciphers: []uint16{tls.TLS_RSA_WITH_AES_256_GCM_SHA384},
},
{
name: "TLS_ECDHE_ECDSA_WITH_RC4_128_SHA",
ciphers: []uint16{tls.TLS_ECDHE_ECDSA_WITH_RC4_128_SHA},
},
{
name: "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA",
ciphers: []uint16{tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA},
},
{
name: "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA",
ciphers: []uint16{tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA},
},
{
name: "TLS_ECDHE_RSA_WITH_RC4_128_SHA",
ciphers: []uint16{tls.TLS_ECDHE_RSA_WITH_RC4_128_SHA},
},
{
name: "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA",
ciphers: []uint16{tls.TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA},
},
{
name: "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA",
ciphers: []uint16{tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
},
{
name: "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA",
ciphers: []uint16{tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA},
},
{
name: "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256",
ciphers: []uint16{tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256},
},
{
name: "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256",
ciphers: []uint16{tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256},
},
{
name: "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",
ciphers: []uint16{tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
},
{
name: "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256",
ciphers: []uint16{tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
},
{
name: "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384",
ciphers: []uint16{tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384},
},
{
name: "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384",
ciphers: []uint16{tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384},
},
{
name: "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256",
ciphers: []uint16{tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256},
},
{
name: "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256",
ciphers: []uint16{tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256},
},
{
name: "TLS_AES_128_GCM_SHA256",
ciphers: []uint16{tls.TLS_AES_128_GCM_SHA256},
},
{
name: "TLS_AES_256_GCM_SHA384",
ciphers: []uint16{tls.TLS_AES_256_GCM_SHA384},
},
{
name: "TLS_CHACHA20_POLY1305_SHA256",
ciphers: []uint16{tls.TLS_CHACHA20_POLY1305_SHA256},
},
}
func TestClient(t *testing.T) {
for _, tt := range ciphers {
t.Run(tt.name, func(t *testing.T) {
for i := 0; i < 100; i++ {
testClient(t, tt.ciphers)
}
})
}
}
func testClient(t *testing.T, ciphers []uint16) {
sConn, cConn := net.Pipe()
pair, err := tls.X509KeyPair([]byte(cert), []byte(key))
if err != nil {
t.Fatal(err)
}
srv := tls.Server(sConn, &tls.Config{
Certificates: []tls.Certificate{pair},
CipherSuites: ciphers,
})
cli, err := Client(cConn, &tls.Config{
InsecureSkipVerify: true,
}, nil)
if err != nil {
t.Fatal(err)
}
// Launch server in another goroutine
go func() {
if err := srv.Handshake(); err != nil {
panic(err)
}
// Loop of read write
for i := 0; i < 2; i++ {
recv := make([]byte, 1024)
n, err := srv.Read(recv)
if err != nil {
panic(err)
}
if _, err := srv.Write(recv[:n]); err != nil {
panic(err)
}
}
}()
// Initial handshake
if err := cli.Handshake(); err != nil {
t.Fatal(err)
}
// Test write and read
message := []byte("Hello")
if _, err := cli.Write(message); err != nil {
t.Fatal(err)
}
recv := make([]byte, 1024)
n, err := cli.Read(recv)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(message, recv[:n]) {
t.Errorf("messages missmatch: %s != %s", message, recv[:n])
}
// Extract TLS state
state := cli.State()
// Resume client
cli2, err := Client(cConn, &tls.Config{
InsecureSkipVerify: true,
}, state)
if err != nil {
t.Fatal(err)
}
// Test write and read on resumed client
if _, err := cli2.Write(message); err != nil {
t.Fatal(err)
}
recv = make([]byte, 1024)
n, err = cli2.Read(recv)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(message, recv[:n]) {
t.Errorf("messages missmatch: %s != %s", message, recv[:n])
}
}
func TestServer(t *testing.T) {
for _, tt := range ciphers {
t.Run(tt.name, func(t *testing.T) {
for i := 0; i < 100; i++ {
testServer(t, tt.ciphers)
}
})
}
}
func testServer(t *testing.T, ciphers []uint16) {
sConn, cConn := net.Pipe()
pair, err := tls.X509KeyPair([]byte(cert), []byte(key))
if err != nil {
t.Fatal(err)
}
cli := tls.Client(sConn, &tls.Config{
InsecureSkipVerify: true,
CipherSuites: ciphers,
})
srv, err := Server(cConn, &tls.Config{
InsecureSkipVerify: true,
Certificates: []tls.Certificate{pair},
}, nil)
if err != nil {
t.Fatal(err)
}
// Launch client in another goroutine
go func() {
if err := cli.Handshake(); err != nil {
panic(err)
}
// Loop of read write
for i := 0; i < 2; i++ {
recv := make([]byte, 1024)
n, err := cli.Read(recv)
if err != nil {
panic(err)
}
if _, err := cli.Write(recv[:n]); err != nil {
panic(err)
}
}
}()
// Initial handshake
if err := srv.Handshake(); err != nil {
t.Fatal(err)
}
// Test write and read
message := []byte("Hello")
if _, err := srv.Write(message); err != nil {
t.Fatal(err)
}
recv := make([]byte, 1024)
n, err := srv.Read(recv)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(message, recv[:n]) {
t.Errorf("messages missmatch: %s != %s", message, recv[:n])
}
// Extract TLS state
state := srv.State()
// Resume server
srv2, err := Server(cConn, &tls.Config{
InsecureSkipVerify: true,
Certificates: []tls.Certificate{pair},
}, state)
if err != nil {
t.Fatal(err)
}
// Test write and read on resumed client
if _, err := srv2.Write(message); err != nil {
t.Fatal(err)
}
recv = make([]byte, 1024)
n, err = srv.Read(recv)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(message, recv[:n]) {
t.Errorf("messages missmatch: %s != %s", message, recv[:n])
}
}