blob: dd63c5b4340710bc01c08730aa69774327788145 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001package main
2
3import (
4 "bytes"
David Benjamina08e49d2014-08-24 01:46:07 -04005 "crypto/ecdsa"
6 "crypto/elliptic"
David Benjamin407a10c2014-07-16 12:58:59 -04007 "crypto/x509"
David Benjamin2561dc32014-08-24 01:25:27 -04008 "encoding/base64"
David Benjamina08e49d2014-08-24 01:46:07 -04009 "encoding/pem"
Adam Langley95c29f32014-06-20 12:00:00 -070010 "flag"
11 "fmt"
12 "io"
Kenny Root7fdeaf12014-08-05 15:23:37 -070013 "io/ioutil"
Adam Langley95c29f32014-06-20 12:00:00 -070014 "net"
15 "os"
16 "os/exec"
David Benjamin884fdf12014-08-02 15:28:23 -040017 "path"
David Benjamin2bc8e6f2014-08-02 15:22:37 -040018 "runtime"
Adam Langley69a01602014-11-17 17:26:55 -080019 "strconv"
Adam Langley95c29f32014-06-20 12:00:00 -070020 "strings"
21 "sync"
22 "syscall"
David Benjamin83f90402015-01-27 01:09:43 -050023 "time"
Adam Langley95c29f32014-06-20 12:00:00 -070024)
25
Adam Langley69a01602014-11-17 17:26:55 -080026var (
27 useValgrind = flag.Bool("valgrind", false, "If true, run code under valgrind")
28 useGDB = flag.Bool("gdb", false, "If true, run BoringSSL code under gdb")
29 flagDebug *bool = flag.Bool("debug", false, "Hexdump the contents of the connection")
30 mallocTest *int64 = flag.Int64("malloc-test", -1, "If non-negative, run each test with each malloc in turn failing from the given number onwards.")
31 mallocTestDebug *bool = flag.Bool("malloc-test-debug", false, "If true, ask bssl_shim to abort rather than fail a malloc. This can be used with a specific value for --malloc-test to identity the malloc failing that is causing problems.")
32)
Adam Langley95c29f32014-06-20 12:00:00 -070033
David Benjamin025b3d32014-07-01 19:53:04 -040034const (
35 rsaCertificateFile = "cert.pem"
36 ecdsaCertificateFile = "ecdsa_cert.pem"
37)
38
39const (
David Benjamina08e49d2014-08-24 01:46:07 -040040 rsaKeyFile = "key.pem"
41 ecdsaKeyFile = "ecdsa_key.pem"
42 channelIDKeyFile = "channel_id_key.pem"
David Benjamin025b3d32014-07-01 19:53:04 -040043)
44
Adam Langley95c29f32014-06-20 12:00:00 -070045var rsaCertificate, ecdsaCertificate Certificate
David Benjamina08e49d2014-08-24 01:46:07 -040046var channelIDKey *ecdsa.PrivateKey
47var channelIDBytes []byte
Adam Langley95c29f32014-06-20 12:00:00 -070048
David Benjamin61f95272014-11-25 01:55:35 -050049var testOCSPResponse = []byte{1, 2, 3, 4}
50var testSCTList = []byte{5, 6, 7, 8}
51
Adam Langley95c29f32014-06-20 12:00:00 -070052func initCertificates() {
53 var err error
David Benjamin025b3d32014-07-01 19:53:04 -040054 rsaCertificate, err = LoadX509KeyPair(rsaCertificateFile, rsaKeyFile)
Adam Langley95c29f32014-06-20 12:00:00 -070055 if err != nil {
56 panic(err)
57 }
David Benjamin61f95272014-11-25 01:55:35 -050058 rsaCertificate.OCSPStaple = testOCSPResponse
59 rsaCertificate.SignedCertificateTimestampList = testSCTList
Adam Langley95c29f32014-06-20 12:00:00 -070060
David Benjamin025b3d32014-07-01 19:53:04 -040061 ecdsaCertificate, err = LoadX509KeyPair(ecdsaCertificateFile, ecdsaKeyFile)
Adam Langley95c29f32014-06-20 12:00:00 -070062 if err != nil {
63 panic(err)
64 }
David Benjamin61f95272014-11-25 01:55:35 -050065 ecdsaCertificate.OCSPStaple = testOCSPResponse
66 ecdsaCertificate.SignedCertificateTimestampList = testSCTList
David Benjamina08e49d2014-08-24 01:46:07 -040067
68 channelIDPEMBlock, err := ioutil.ReadFile(channelIDKeyFile)
69 if err != nil {
70 panic(err)
71 }
72 channelIDDERBlock, _ := pem.Decode(channelIDPEMBlock)
73 if channelIDDERBlock.Type != "EC PRIVATE KEY" {
74 panic("bad key type")
75 }
76 channelIDKey, err = x509.ParseECPrivateKey(channelIDDERBlock.Bytes)
77 if err != nil {
78 panic(err)
79 }
80 if channelIDKey.Curve != elliptic.P256() {
81 panic("bad curve")
82 }
83
84 channelIDBytes = make([]byte, 64)
85 writeIntPadded(channelIDBytes[:32], channelIDKey.X)
86 writeIntPadded(channelIDBytes[32:], channelIDKey.Y)
Adam Langley95c29f32014-06-20 12:00:00 -070087}
88
89var certificateOnce sync.Once
90
91func getRSACertificate() Certificate {
92 certificateOnce.Do(initCertificates)
93 return rsaCertificate
94}
95
96func getECDSACertificate() Certificate {
97 certificateOnce.Do(initCertificates)
98 return ecdsaCertificate
99}
100
David Benjamin025b3d32014-07-01 19:53:04 -0400101type testType int
102
103const (
104 clientTest testType = iota
105 serverTest
106)
107
David Benjamin6fd297b2014-08-11 18:43:38 -0400108type protocol int
109
110const (
111 tls protocol = iota
112 dtls
113)
114
David Benjaminfc7b0862014-09-06 13:21:53 -0400115const (
116 alpn = 1
117 npn = 2
118)
119
Adam Langley95c29f32014-06-20 12:00:00 -0700120type testCase struct {
David Benjamin025b3d32014-07-01 19:53:04 -0400121 testType testType
David Benjamin6fd297b2014-08-11 18:43:38 -0400122 protocol protocol
Adam Langley95c29f32014-06-20 12:00:00 -0700123 name string
124 config Config
125 shouldFail bool
126 expectedError string
Adam Langleyac61fa32014-06-23 12:03:11 -0700127 // expectedLocalError, if not empty, contains a substring that must be
128 // found in the local error.
129 expectedLocalError string
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400130 // expectedVersion, if non-zero, specifies the TLS version that must be
131 // negotiated.
132 expectedVersion uint16
David Benjamin01fe8202014-09-24 15:21:44 -0400133 // expectedResumeVersion, if non-zero, specifies the TLS version that
134 // must be negotiated on resumption. If zero, expectedVersion is used.
135 expectedResumeVersion uint16
David Benjamina08e49d2014-08-24 01:46:07 -0400136 // expectChannelID controls whether the connection should have
137 // negotiated a Channel ID with channelIDKey.
138 expectChannelID bool
David Benjaminae2888f2014-09-06 12:58:58 -0400139 // expectedNextProto controls whether the connection should
140 // negotiate a next protocol via NPN or ALPN.
141 expectedNextProto string
David Benjaminfc7b0862014-09-06 13:21:53 -0400142 // expectedNextProtoType, if non-zero, is the expected next
143 // protocol negotiation mechanism.
144 expectedNextProtoType int
David Benjaminca6c8262014-11-15 19:06:08 -0500145 // expectedSRTPProtectionProfile is the DTLS-SRTP profile that
146 // should be negotiated. If zero, none should be negotiated.
147 expectedSRTPProtectionProfile uint16
Adam Langley80842bd2014-06-20 12:00:00 -0700148 // messageLen is the length, in bytes, of the test message that will be
149 // sent.
150 messageLen int
David Benjamin025b3d32014-07-01 19:53:04 -0400151 // certFile is the path to the certificate to use for the server.
152 certFile string
153 // keyFile is the path to the private key to use for the server.
154 keyFile string
David Benjamin1d5c83e2014-07-22 19:20:02 -0400155 // resumeSession controls whether a second connection should be tested
David Benjamin01fe8202014-09-24 15:21:44 -0400156 // which attempts to resume the first session.
David Benjamin1d5c83e2014-07-22 19:20:02 -0400157 resumeSession bool
David Benjamin01fe8202014-09-24 15:21:44 -0400158 // resumeConfig, if not nil, points to a Config to be used on
David Benjaminfe8eb9a2014-11-17 03:19:02 -0500159 // resumption. Unless newSessionsOnResume is set,
160 // SessionTicketKey, ServerSessionCache, and
161 // ClientSessionCache are copied from the initial connection's
162 // config. If nil, the initial connection's config is used.
David Benjamin01fe8202014-09-24 15:21:44 -0400163 resumeConfig *Config
David Benjaminfe8eb9a2014-11-17 03:19:02 -0500164 // newSessionsOnResume, if true, will cause resumeConfig to
165 // use a different session resumption context.
166 newSessionsOnResume bool
David Benjamin98e882e2014-08-08 13:24:34 -0400167 // sendPrefix sends a prefix on the socket before actually performing a
168 // handshake.
169 sendPrefix string
David Benjamine58c4f52014-08-24 03:47:07 -0400170 // shimWritesFirst controls whether the shim sends an initial "hello"
171 // message before doing a roundtrip with the runner.
172 shimWritesFirst bool
Adam Langleycf2d4f42014-10-28 19:06:14 -0700173 // renegotiate indicates the the connection should be renegotiated
174 // during the exchange.
175 renegotiate bool
176 // renegotiateCiphers is a list of ciphersuite ids that will be
177 // switched in just before renegotiation.
178 renegotiateCiphers []uint16
David Benjamin5e961c12014-11-07 01:48:35 -0500179 // replayWrites, if true, configures the underlying transport
180 // to replay every write it makes in DTLS tests.
181 replayWrites bool
David Benjamin5fa3eba2015-01-22 16:35:40 -0500182 // damageFirstWrite, if true, configures the underlying transport to
183 // damage the final byte of the first application data write.
184 damageFirstWrite bool
David Benjamin325b5c32014-07-01 19:40:31 -0400185 // flags, if not empty, contains a list of command-line flags that will
186 // be passed to the shim program.
187 flags []string
Adam Langley95c29f32014-06-20 12:00:00 -0700188}
189
David Benjamin025b3d32014-07-01 19:53:04 -0400190var testCases = []testCase{
Adam Langley95c29f32014-06-20 12:00:00 -0700191 {
192 name: "BadRSASignature",
193 config: Config{
194 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
195 Bugs: ProtocolBugs{
196 InvalidSKXSignature: true,
197 },
198 },
199 shouldFail: true,
200 expectedError: ":BAD_SIGNATURE:",
201 },
202 {
203 name: "BadECDSASignature",
204 config: Config{
205 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
206 Bugs: ProtocolBugs{
207 InvalidSKXSignature: true,
208 },
209 Certificates: []Certificate{getECDSACertificate()},
210 },
211 shouldFail: true,
212 expectedError: ":BAD_SIGNATURE:",
213 },
214 {
215 name: "BadECDSACurve",
216 config: Config{
217 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
218 Bugs: ProtocolBugs{
219 InvalidSKXCurve: true,
220 },
221 Certificates: []Certificate{getECDSACertificate()},
222 },
223 shouldFail: true,
224 expectedError: ":WRONG_CURVE:",
225 },
Adam Langleyac61fa32014-06-23 12:03:11 -0700226 {
David Benjamina8e3e0e2014-08-06 22:11:10 -0400227 testType: serverTest,
228 name: "BadRSAVersion",
229 config: Config{
230 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
231 Bugs: ProtocolBugs{
232 RsaClientKeyExchangeVersion: VersionTLS11,
233 },
234 },
235 shouldFail: true,
236 expectedError: ":DECRYPTION_FAILED_OR_BAD_RECORD_MAC:",
237 },
238 {
David Benjamin325b5c32014-07-01 19:40:31 -0400239 name: "NoFallbackSCSV",
Adam Langleyac61fa32014-06-23 12:03:11 -0700240 config: Config{
241 Bugs: ProtocolBugs{
242 FailIfNotFallbackSCSV: true,
243 },
244 },
245 shouldFail: true,
246 expectedLocalError: "no fallback SCSV found",
247 },
David Benjamin325b5c32014-07-01 19:40:31 -0400248 {
David Benjamin2a0c4962014-08-22 23:46:35 -0400249 name: "SendFallbackSCSV",
David Benjamin325b5c32014-07-01 19:40:31 -0400250 config: Config{
251 Bugs: ProtocolBugs{
252 FailIfNotFallbackSCSV: true,
253 },
254 },
255 flags: []string{"-fallback-scsv"},
256 },
David Benjamin197b3ab2014-07-02 18:37:33 -0400257 {
David Benjamin7b030512014-07-08 17:30:11 -0400258 name: "ClientCertificateTypes",
259 config: Config{
260 ClientAuth: RequestClientCert,
261 ClientCertificateTypes: []byte{
262 CertTypeDSSSign,
263 CertTypeRSASign,
264 CertTypeECDSASign,
265 },
266 },
David Benjamin2561dc32014-08-24 01:25:27 -0400267 flags: []string{
268 "-expect-certificate-types",
269 base64.StdEncoding.EncodeToString([]byte{
270 CertTypeDSSSign,
271 CertTypeRSASign,
272 CertTypeECDSASign,
273 }),
274 },
David Benjamin7b030512014-07-08 17:30:11 -0400275 },
David Benjamin636293b2014-07-08 17:59:18 -0400276 {
277 name: "NoClientCertificate",
278 config: Config{
279 ClientAuth: RequireAnyClientCert,
280 },
281 shouldFail: true,
282 expectedLocalError: "client didn't provide a certificate",
283 },
David Benjamin1c375dd2014-07-12 00:48:23 -0400284 {
285 name: "UnauthenticatedECDH",
286 config: Config{
287 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
288 Bugs: ProtocolBugs{
289 UnauthenticatedECDH: true,
290 },
291 },
292 shouldFail: true,
David Benjamine8f3d662014-07-12 01:10:19 -0400293 expectedError: ":UNEXPECTED_MESSAGE:",
David Benjamin1c375dd2014-07-12 00:48:23 -0400294 },
David Benjamin9c651c92014-07-12 13:27:45 -0400295 {
296 name: "SkipServerKeyExchange",
297 config: Config{
298 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
299 Bugs: ProtocolBugs{
300 SkipServerKeyExchange: true,
301 },
302 },
303 shouldFail: true,
304 expectedError: ":UNEXPECTED_MESSAGE:",
305 },
David Benjamin1f5f62b2014-07-12 16:18:02 -0400306 {
David Benjamina0e52232014-07-19 17:39:58 -0400307 name: "SkipChangeCipherSpec-Client",
308 config: Config{
309 Bugs: ProtocolBugs{
310 SkipChangeCipherSpec: true,
311 },
312 },
313 shouldFail: true,
David Benjamin86271ee2014-07-21 16:14:03 -0400314 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
David Benjamina0e52232014-07-19 17:39:58 -0400315 },
316 {
317 testType: serverTest,
318 name: "SkipChangeCipherSpec-Server",
319 config: Config{
320 Bugs: ProtocolBugs{
321 SkipChangeCipherSpec: true,
322 },
323 },
324 shouldFail: true,
David Benjamin86271ee2014-07-21 16:14:03 -0400325 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
David Benjamina0e52232014-07-19 17:39:58 -0400326 },
David Benjamin42be6452014-07-21 14:50:23 -0400327 {
328 testType: serverTest,
329 name: "SkipChangeCipherSpec-Server-NPN",
330 config: Config{
331 NextProtos: []string{"bar"},
332 Bugs: ProtocolBugs{
333 SkipChangeCipherSpec: true,
334 },
335 },
336 flags: []string{
337 "-advertise-npn", "\x03foo\x03bar\x03baz",
338 },
339 shouldFail: true,
David Benjamin86271ee2014-07-21 16:14:03 -0400340 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
341 },
342 {
343 name: "FragmentAcrossChangeCipherSpec-Client",
344 config: Config{
345 Bugs: ProtocolBugs{
346 FragmentAcrossChangeCipherSpec: true,
347 },
348 },
349 shouldFail: true,
350 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
351 },
352 {
353 testType: serverTest,
354 name: "FragmentAcrossChangeCipherSpec-Server",
355 config: Config{
356 Bugs: ProtocolBugs{
357 FragmentAcrossChangeCipherSpec: true,
358 },
359 },
360 shouldFail: true,
361 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
362 },
363 {
364 testType: serverTest,
365 name: "FragmentAcrossChangeCipherSpec-Server-NPN",
366 config: Config{
367 NextProtos: []string{"bar"},
368 Bugs: ProtocolBugs{
369 FragmentAcrossChangeCipherSpec: true,
370 },
371 },
372 flags: []string{
373 "-advertise-npn", "\x03foo\x03bar\x03baz",
374 },
375 shouldFail: true,
376 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
David Benjamin42be6452014-07-21 14:50:23 -0400377 },
David Benjaminf3ec83d2014-07-21 22:42:34 -0400378 {
379 testType: serverTest,
David Benjamin3fd1fbd2015-02-03 16:07:32 -0500380 name: "Alert",
381 config: Config{
382 Bugs: ProtocolBugs{
383 SendSpuriousAlert: alertRecordOverflow,
384 },
385 },
386 shouldFail: true,
387 expectedError: ":TLSV1_ALERT_RECORD_OVERFLOW:",
388 },
389 {
390 protocol: dtls,
391 testType: serverTest,
392 name: "Alert-DTLS",
393 config: Config{
394 Bugs: ProtocolBugs{
395 SendSpuriousAlert: alertRecordOverflow,
396 },
397 },
398 shouldFail: true,
399 expectedError: ":TLSV1_ALERT_RECORD_OVERFLOW:",
400 },
401 {
402 testType: serverTest,
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400403 name: "FragmentAlert",
404 config: Config{
405 Bugs: ProtocolBugs{
David Benjaminca6c8262014-11-15 19:06:08 -0500406 FragmentAlert: true,
David Benjamin3fd1fbd2015-02-03 16:07:32 -0500407 SendSpuriousAlert: alertRecordOverflow,
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400408 },
409 },
410 shouldFail: true,
411 expectedError: ":BAD_ALERT:",
412 },
413 {
David Benjamin0ea8dda2015-01-31 20:33:40 -0500414 protocol: dtls,
415 testType: serverTest,
416 name: "FragmentAlert-DTLS",
417 config: Config{
418 Bugs: ProtocolBugs{
419 FragmentAlert: true,
David Benjamin3fd1fbd2015-02-03 16:07:32 -0500420 SendSpuriousAlert: alertRecordOverflow,
David Benjamin0ea8dda2015-01-31 20:33:40 -0500421 },
422 },
423 shouldFail: true,
424 expectedError: ":BAD_ALERT:",
425 },
426 {
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400427 testType: serverTest,
David Benjaminf3ec83d2014-07-21 22:42:34 -0400428 name: "EarlyChangeCipherSpec-server-1",
429 config: Config{
430 Bugs: ProtocolBugs{
431 EarlyChangeCipherSpec: 1,
432 },
433 },
434 shouldFail: true,
435 expectedError: ":CCS_RECEIVED_EARLY:",
436 },
437 {
438 testType: serverTest,
439 name: "EarlyChangeCipherSpec-server-2",
440 config: Config{
441 Bugs: ProtocolBugs{
442 EarlyChangeCipherSpec: 2,
443 },
444 },
445 shouldFail: true,
446 expectedError: ":CCS_RECEIVED_EARLY:",
447 },
David Benjamind23f4122014-07-23 15:09:48 -0400448 {
David Benjamind23f4122014-07-23 15:09:48 -0400449 name: "SkipNewSessionTicket",
450 config: Config{
451 Bugs: ProtocolBugs{
452 SkipNewSessionTicket: true,
453 },
454 },
455 shouldFail: true,
456 expectedError: ":CCS_RECEIVED_EARLY:",
457 },
David Benjamin7e3305e2014-07-28 14:52:32 -0400458 {
David Benjamind86c7672014-08-02 04:07:12 -0400459 testType: serverTest,
David Benjaminbef270a2014-08-02 04:22:02 -0400460 name: "FallbackSCSV",
461 config: Config{
462 MaxVersion: VersionTLS11,
463 Bugs: ProtocolBugs{
464 SendFallbackSCSV: true,
465 },
466 },
467 shouldFail: true,
468 expectedError: ":INAPPROPRIATE_FALLBACK:",
469 },
470 {
471 testType: serverTest,
472 name: "FallbackSCSV-VersionMatch",
473 config: Config{
474 Bugs: ProtocolBugs{
475 SendFallbackSCSV: true,
476 },
477 },
478 },
David Benjamin98214542014-08-07 18:02:39 -0400479 {
480 testType: serverTest,
481 name: "FragmentedClientVersion",
482 config: Config{
483 Bugs: ProtocolBugs{
484 MaxHandshakeRecordLength: 1,
485 FragmentClientVersion: true,
486 },
487 },
David Benjamin82c9e902014-12-12 15:55:27 -0500488 expectedVersion: VersionTLS12,
David Benjamin98214542014-08-07 18:02:39 -0400489 },
David Benjamin98e882e2014-08-08 13:24:34 -0400490 {
491 testType: serverTest,
492 name: "MinorVersionTolerance",
493 config: Config{
494 Bugs: ProtocolBugs{
495 SendClientVersion: 0x03ff,
496 },
497 },
498 expectedVersion: VersionTLS12,
499 },
500 {
501 testType: serverTest,
502 name: "MajorVersionTolerance",
503 config: Config{
504 Bugs: ProtocolBugs{
505 SendClientVersion: 0x0400,
506 },
507 },
508 expectedVersion: VersionTLS12,
509 },
510 {
511 testType: serverTest,
512 name: "VersionTooLow",
513 config: Config{
514 Bugs: ProtocolBugs{
515 SendClientVersion: 0x0200,
516 },
517 },
518 shouldFail: true,
519 expectedError: ":UNSUPPORTED_PROTOCOL:",
520 },
521 {
522 testType: serverTest,
523 name: "HttpGET",
524 sendPrefix: "GET / HTTP/1.0\n",
525 shouldFail: true,
526 expectedError: ":HTTP_REQUEST:",
527 },
528 {
529 testType: serverTest,
530 name: "HttpPOST",
531 sendPrefix: "POST / HTTP/1.0\n",
532 shouldFail: true,
533 expectedError: ":HTTP_REQUEST:",
534 },
535 {
536 testType: serverTest,
537 name: "HttpHEAD",
538 sendPrefix: "HEAD / HTTP/1.0\n",
539 shouldFail: true,
540 expectedError: ":HTTP_REQUEST:",
541 },
542 {
543 testType: serverTest,
544 name: "HttpPUT",
545 sendPrefix: "PUT / HTTP/1.0\n",
546 shouldFail: true,
547 expectedError: ":HTTP_REQUEST:",
548 },
549 {
550 testType: serverTest,
551 name: "HttpCONNECT",
552 sendPrefix: "CONNECT www.google.com:443 HTTP/1.0\n",
553 shouldFail: true,
554 expectedError: ":HTTPS_PROXY_REQUEST:",
555 },
David Benjamin39ebf532014-08-31 02:23:49 -0400556 {
David Benjaminf080ecd2014-12-11 18:48:59 -0500557 testType: serverTest,
558 name: "Garbage",
559 sendPrefix: "blah",
560 shouldFail: true,
561 expectedError: ":UNKNOWN_PROTOCOL:",
562 },
563 {
David Benjamin39ebf532014-08-31 02:23:49 -0400564 name: "SkipCipherVersionCheck",
565 config: Config{
566 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
567 MaxVersion: VersionTLS11,
568 Bugs: ProtocolBugs{
569 SkipCipherVersionCheck: true,
570 },
571 },
572 shouldFail: true,
573 expectedError: ":WRONG_CIPHER_RETURNED:",
574 },
David Benjamin9114fae2014-11-08 11:41:14 -0500575 {
576 name: "RSAServerKeyExchange",
577 config: Config{
578 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
579 Bugs: ProtocolBugs{
580 RSAServerKeyExchange: true,
581 },
582 },
583 shouldFail: true,
584 expectedError: ":UNEXPECTED_MESSAGE:",
585 },
David Benjamin128dbc32014-12-01 01:27:42 -0500586 {
587 name: "DisableEverything",
588 flags: []string{"-no-tls12", "-no-tls11", "-no-tls1", "-no-ssl3"},
589 shouldFail: true,
590 expectedError: ":WRONG_SSL_VERSION:",
591 },
592 {
593 protocol: dtls,
594 name: "DisableEverything-DTLS",
595 flags: []string{"-no-tls12", "-no-tls1"},
596 shouldFail: true,
597 expectedError: ":WRONG_SSL_VERSION:",
598 },
David Benjamin780d6dd2015-01-06 12:03:19 -0500599 {
600 name: "NoSharedCipher",
601 config: Config{
602 CipherSuites: []uint16{},
603 },
604 shouldFail: true,
605 expectedError: ":HANDSHAKE_FAILURE_ON_CLIENT_HELLO:",
606 },
David Benjamin13be1de2015-01-11 16:29:36 -0500607 {
608 protocol: dtls,
609 testType: serverTest,
610 name: "MTU",
611 config: Config{
612 Bugs: ProtocolBugs{
613 MaxPacketLength: 256,
614 },
615 },
616 flags: []string{"-mtu", "256"},
617 },
618 {
619 protocol: dtls,
620 testType: serverTest,
621 name: "MTUExceeded",
622 config: Config{
623 Bugs: ProtocolBugs{
624 MaxPacketLength: 255,
625 },
626 },
627 flags: []string{"-mtu", "256"},
628 shouldFail: true,
629 expectedLocalError: "dtls: exceeded maximum packet length",
630 },
David Benjamin6095de82014-12-27 01:50:38 -0500631 {
632 name: "CertMismatchRSA",
633 config: Config{
634 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
635 Certificates: []Certificate{getECDSACertificate()},
636 Bugs: ProtocolBugs{
637 SendCipherSuite: TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
638 },
639 },
640 shouldFail: true,
641 expectedError: ":WRONG_CERTIFICATE_TYPE:",
642 },
643 {
644 name: "CertMismatchECDSA",
645 config: Config{
646 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
647 Certificates: []Certificate{getRSACertificate()},
648 Bugs: ProtocolBugs{
649 SendCipherSuite: TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
650 },
651 },
652 shouldFail: true,
653 expectedError: ":WRONG_CERTIFICATE_TYPE:",
654 },
David Benjamin5fa3eba2015-01-22 16:35:40 -0500655 {
656 name: "TLSFatalBadPackets",
657 damageFirstWrite: true,
658 shouldFail: true,
659 expectedError: ":DECRYPTION_FAILED_OR_BAD_RECORD_MAC:",
660 },
661 {
662 protocol: dtls,
663 name: "DTLSIgnoreBadPackets",
664 damageFirstWrite: true,
665 },
666 {
667 protocol: dtls,
668 name: "DTLSIgnoreBadPackets-Async",
669 damageFirstWrite: true,
670 flags: []string{"-async"},
671 },
David Benjamin4189bd92015-01-25 23:52:39 -0500672 {
673 name: "AppDataAfterChangeCipherSpec",
674 config: Config{
675 Bugs: ProtocolBugs{
676 AppDataAfterChangeCipherSpec: []byte("TEST MESSAGE"),
677 },
678 },
679 shouldFail: true,
680 expectedError: ":DATA_BETWEEN_CCS_AND_FINISHED:",
681 },
682 {
683 protocol: dtls,
684 name: "AppDataAfterChangeCipherSpec-DTLS",
685 config: Config{
686 Bugs: ProtocolBugs{
687 AppDataAfterChangeCipherSpec: []byte("TEST MESSAGE"),
688 },
689 },
690 },
David Benjaminb3774b92015-01-31 17:16:01 -0500691 {
692 protocol: dtls,
693 name: "ReorderHandshakeFragments-Small-DTLS",
694 config: Config{
695 Bugs: ProtocolBugs{
696 ReorderHandshakeFragments: true,
697 // Small enough that every handshake message is
698 // fragmented.
699 MaxHandshakeRecordLength: 2,
700 },
701 },
702 },
703 {
704 protocol: dtls,
705 name: "ReorderHandshakeFragments-Large-DTLS",
706 config: Config{
707 Bugs: ProtocolBugs{
708 ReorderHandshakeFragments: true,
709 // Large enough that no handshake message is
710 // fragmented.
711 //
712 // TODO(davidben): Also test interaction of
713 // complete handshake messages with
714 // fragments. The current logic is full of bugs
715 // here, so the reassembly logic needs a rewrite
716 // before those tests will pass.
717 MaxHandshakeRecordLength: 2048,
718 },
719 },
720 },
David Benjaminddb9f152015-02-03 15:44:39 -0500721 {
722 name: "SendInvalidRecordType",
723 config: Config{
724 Bugs: ProtocolBugs{
725 SendInvalidRecordType: true,
726 },
727 },
728 shouldFail: true,
729 expectedError: ":UNEXPECTED_RECORD:",
730 },
731 {
732 protocol: dtls,
733 name: "SendInvalidRecordType-DTLS",
734 config: Config{
735 Bugs: ProtocolBugs{
736 SendInvalidRecordType: true,
737 },
738 },
739 shouldFail: true,
740 expectedError: ":UNEXPECTED_RECORD:",
741 },
David Benjaminb80168e2015-02-08 18:30:14 -0500742 {
743 name: "FalseStart-SkipServerSecondLeg",
744 config: Config{
745 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
746 NextProtos: []string{"foo"},
747 Bugs: ProtocolBugs{
748 SkipNewSessionTicket: true,
749 SkipChangeCipherSpec: true,
750 SkipFinished: true,
751 ExpectFalseStart: true,
752 },
753 },
754 flags: []string{
755 "-false-start",
756 "-advertise-alpn", "\x03foo",
757 },
758 shimWritesFirst: true,
759 shouldFail: true,
760 expectedError: ":UNEXPECTED_RECORD:",
761 },
Adam Langley95c29f32014-06-20 12:00:00 -0700762}
763
David Benjamin01fe8202014-09-24 15:21:44 -0400764func doExchange(test *testCase, config *Config, conn net.Conn, messageLen int, isResume bool) error {
David Benjamin65ea8ff2014-11-23 03:01:00 -0500765 var connDebug *recordingConn
David Benjamin5fa3eba2015-01-22 16:35:40 -0500766 var connDamage *damageAdaptor
David Benjamin65ea8ff2014-11-23 03:01:00 -0500767 if *flagDebug {
768 connDebug = &recordingConn{Conn: conn}
769 conn = connDebug
770 defer func() {
771 connDebug.WriteTo(os.Stdout)
772 }()
773 }
774
David Benjamin6fd297b2014-08-11 18:43:38 -0400775 if test.protocol == dtls {
David Benjamin83f90402015-01-27 01:09:43 -0500776 config.Bugs.PacketAdaptor = newPacketAdaptor(conn)
777 conn = config.Bugs.PacketAdaptor
David Benjamin5e961c12014-11-07 01:48:35 -0500778 if test.replayWrites {
779 conn = newReplayAdaptor(conn)
780 }
David Benjamin6fd297b2014-08-11 18:43:38 -0400781 }
782
David Benjamin5fa3eba2015-01-22 16:35:40 -0500783 if test.damageFirstWrite {
784 connDamage = newDamageAdaptor(conn)
785 conn = connDamage
786 }
787
David Benjamin6fd297b2014-08-11 18:43:38 -0400788 if test.sendPrefix != "" {
789 if _, err := conn.Write([]byte(test.sendPrefix)); err != nil {
790 return err
791 }
David Benjamin98e882e2014-08-08 13:24:34 -0400792 }
793
David Benjamin1d5c83e2014-07-22 19:20:02 -0400794 var tlsConn *Conn
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400795 if test.testType == clientTest {
David Benjamin6fd297b2014-08-11 18:43:38 -0400796 if test.protocol == dtls {
797 tlsConn = DTLSServer(conn, config)
798 } else {
799 tlsConn = Server(conn, config)
800 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400801 } else {
802 config.InsecureSkipVerify = true
David Benjamin6fd297b2014-08-11 18:43:38 -0400803 if test.protocol == dtls {
804 tlsConn = DTLSClient(conn, config)
805 } else {
806 tlsConn = Client(conn, config)
807 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400808 }
809
Adam Langley95c29f32014-06-20 12:00:00 -0700810 if err := tlsConn.Handshake(); err != nil {
811 return err
812 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700813
David Benjamin01fe8202014-09-24 15:21:44 -0400814 // TODO(davidben): move all per-connection expectations into a dedicated
815 // expectations struct that can be specified separately for the two
816 // legs.
817 expectedVersion := test.expectedVersion
818 if isResume && test.expectedResumeVersion != 0 {
819 expectedVersion = test.expectedResumeVersion
820 }
821 if vers := tlsConn.ConnectionState().Version; expectedVersion != 0 && vers != expectedVersion {
822 return fmt.Errorf("got version %x, expected %x", vers, expectedVersion)
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400823 }
824
David Benjamina08e49d2014-08-24 01:46:07 -0400825 if test.expectChannelID {
826 channelID := tlsConn.ConnectionState().ChannelID
827 if channelID == nil {
828 return fmt.Errorf("no channel ID negotiated")
829 }
830 if channelID.Curve != channelIDKey.Curve ||
831 channelIDKey.X.Cmp(channelIDKey.X) != 0 ||
832 channelIDKey.Y.Cmp(channelIDKey.Y) != 0 {
833 return fmt.Errorf("incorrect channel ID")
834 }
835 }
836
David Benjaminae2888f2014-09-06 12:58:58 -0400837 if expected := test.expectedNextProto; expected != "" {
838 if actual := tlsConn.ConnectionState().NegotiatedProtocol; actual != expected {
839 return fmt.Errorf("next proto mismatch: got %s, wanted %s", actual, expected)
840 }
841 }
842
David Benjaminfc7b0862014-09-06 13:21:53 -0400843 if test.expectedNextProtoType != 0 {
844 if (test.expectedNextProtoType == alpn) != tlsConn.ConnectionState().NegotiatedProtocolFromALPN {
845 return fmt.Errorf("next proto type mismatch")
846 }
847 }
848
David Benjaminca6c8262014-11-15 19:06:08 -0500849 if p := tlsConn.ConnectionState().SRTPProtectionProfile; p != test.expectedSRTPProtectionProfile {
850 return fmt.Errorf("SRTP profile mismatch: got %d, wanted %d", p, test.expectedSRTPProtectionProfile)
851 }
852
David Benjamine58c4f52014-08-24 03:47:07 -0400853 if test.shimWritesFirst {
854 var buf [5]byte
855 _, err := io.ReadFull(tlsConn, buf[:])
856 if err != nil {
857 return err
858 }
859 if string(buf[:]) != "hello" {
860 return fmt.Errorf("bad initial message")
861 }
862 }
863
Adam Langleycf2d4f42014-10-28 19:06:14 -0700864 if test.renegotiate {
865 if test.renegotiateCiphers != nil {
866 config.CipherSuites = test.renegotiateCiphers
867 }
868 if err := tlsConn.Renegotiate(); err != nil {
869 return err
870 }
871 } else if test.renegotiateCiphers != nil {
872 panic("renegotiateCiphers without renegotiate")
873 }
874
David Benjamin5fa3eba2015-01-22 16:35:40 -0500875 if test.damageFirstWrite {
876 connDamage.setDamage(true)
877 tlsConn.Write([]byte("DAMAGED WRITE"))
878 connDamage.setDamage(false)
879 }
880
Kenny Root7fdeaf12014-08-05 15:23:37 -0700881 if messageLen < 0 {
David Benjamin6fd297b2014-08-11 18:43:38 -0400882 if test.protocol == dtls {
883 return fmt.Errorf("messageLen < 0 not supported for DTLS tests")
884 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700885 // Read until EOF.
886 _, err := io.Copy(ioutil.Discard, tlsConn)
887 return err
888 }
889
David Benjamin4189bd92015-01-25 23:52:39 -0500890 var testMessage []byte
891 if config.Bugs.AppDataAfterChangeCipherSpec != nil {
892 // We've already sent a message. Expect the shim to echo it
893 // back.
894 testMessage = config.Bugs.AppDataAfterChangeCipherSpec
895 } else {
896 if messageLen == 0 {
897 messageLen = 32
898 }
899 testMessage = make([]byte, messageLen)
900 for i := range testMessage {
901 testMessage[i] = 0x42
902 }
903 tlsConn.Write(testMessage)
Adam Langley80842bd2014-06-20 12:00:00 -0700904 }
Adam Langley95c29f32014-06-20 12:00:00 -0700905
906 buf := make([]byte, len(testMessage))
David Benjamin6fd297b2014-08-11 18:43:38 -0400907 if test.protocol == dtls {
908 bufTmp := make([]byte, len(buf)+1)
909 n, err := tlsConn.Read(bufTmp)
910 if err != nil {
911 return err
912 }
913 if n != len(buf) {
914 return fmt.Errorf("bad reply; length mismatch (%d vs %d)", n, len(buf))
915 }
916 copy(buf, bufTmp)
917 } else {
918 _, err := io.ReadFull(tlsConn, buf)
919 if err != nil {
920 return err
921 }
Adam Langley95c29f32014-06-20 12:00:00 -0700922 }
923
924 for i, v := range buf {
925 if v != testMessage[i]^0xff {
926 return fmt.Errorf("bad reply contents at byte %d", i)
927 }
928 }
929
930 return nil
931}
932
David Benjamin325b5c32014-07-01 19:40:31 -0400933func valgrindOf(dbAttach bool, path string, args ...string) *exec.Cmd {
934 valgrindArgs := []string{"--error-exitcode=99", "--track-origins=yes", "--leak-check=full"}
Adam Langley95c29f32014-06-20 12:00:00 -0700935 if dbAttach {
David Benjamin325b5c32014-07-01 19:40:31 -0400936 valgrindArgs = append(valgrindArgs, "--db-attach=yes", "--db-command=xterm -e gdb -nw %f %p")
Adam Langley95c29f32014-06-20 12:00:00 -0700937 }
David Benjamin325b5c32014-07-01 19:40:31 -0400938 valgrindArgs = append(valgrindArgs, path)
939 valgrindArgs = append(valgrindArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -0700940
David Benjamin325b5c32014-07-01 19:40:31 -0400941 return exec.Command("valgrind", valgrindArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -0700942}
943
David Benjamin325b5c32014-07-01 19:40:31 -0400944func gdbOf(path string, args ...string) *exec.Cmd {
945 xtermArgs := []string{"-e", "gdb", "--args"}
946 xtermArgs = append(xtermArgs, path)
947 xtermArgs = append(xtermArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -0700948
David Benjamin325b5c32014-07-01 19:40:31 -0400949 return exec.Command("xterm", xtermArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -0700950}
951
David Benjamin1d5c83e2014-07-22 19:20:02 -0400952func openSocketPair() (shimEnd *os.File, conn net.Conn) {
Adam Langley95c29f32014-06-20 12:00:00 -0700953 socks, err := syscall.Socketpair(syscall.AF_UNIX, syscall.SOCK_STREAM, 0)
954 if err != nil {
955 panic(err)
956 }
957
958 syscall.CloseOnExec(socks[0])
959 syscall.CloseOnExec(socks[1])
David Benjamin1d5c83e2014-07-22 19:20:02 -0400960 shimEnd = os.NewFile(uintptr(socks[0]), "shim end")
Adam Langley95c29f32014-06-20 12:00:00 -0700961 connFile := os.NewFile(uintptr(socks[1]), "our end")
David Benjamin1d5c83e2014-07-22 19:20:02 -0400962 conn, err = net.FileConn(connFile)
963 if err != nil {
964 panic(err)
965 }
Adam Langley95c29f32014-06-20 12:00:00 -0700966 connFile.Close()
967 if err != nil {
968 panic(err)
969 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400970 return shimEnd, conn
971}
972
Adam Langley69a01602014-11-17 17:26:55 -0800973type moreMallocsError struct{}
974
975func (moreMallocsError) Error() string {
976 return "child process did not exhaust all allocation calls"
977}
978
979var errMoreMallocs = moreMallocsError{}
980
981func runTest(test *testCase, buildDir string, mallocNumToFail int64) error {
Adam Langley38311732014-10-16 19:04:35 -0700982 if !test.shouldFail && (len(test.expectedError) > 0 || len(test.expectedLocalError) > 0) {
983 panic("Error expected without shouldFail in " + test.name)
984 }
985
David Benjamin1d5c83e2014-07-22 19:20:02 -0400986 shimEnd, conn := openSocketPair()
987 shimEndResume, connResume := openSocketPair()
Adam Langley95c29f32014-06-20 12:00:00 -0700988
David Benjamin884fdf12014-08-02 15:28:23 -0400989 shim_path := path.Join(buildDir, "ssl/test/bssl_shim")
David Benjamin5a593af2014-08-11 19:51:50 -0400990 var flags []string
David Benjamin1d5c83e2014-07-22 19:20:02 -0400991 if test.testType == serverTest {
David Benjamin5a593af2014-08-11 19:51:50 -0400992 flags = append(flags, "-server")
993
David Benjamin025b3d32014-07-01 19:53:04 -0400994 flags = append(flags, "-key-file")
995 if test.keyFile == "" {
996 flags = append(flags, rsaKeyFile)
997 } else {
998 flags = append(flags, test.keyFile)
999 }
1000
1001 flags = append(flags, "-cert-file")
1002 if test.certFile == "" {
1003 flags = append(flags, rsaCertificateFile)
1004 } else {
1005 flags = append(flags, test.certFile)
1006 }
1007 }
David Benjamin5a593af2014-08-11 19:51:50 -04001008
David Benjamin6fd297b2014-08-11 18:43:38 -04001009 if test.protocol == dtls {
1010 flags = append(flags, "-dtls")
1011 }
1012
David Benjamin5a593af2014-08-11 19:51:50 -04001013 if test.resumeSession {
1014 flags = append(flags, "-resume")
1015 }
1016
David Benjamine58c4f52014-08-24 03:47:07 -04001017 if test.shimWritesFirst {
1018 flags = append(flags, "-shim-writes-first")
1019 }
1020
David Benjamin025b3d32014-07-01 19:53:04 -04001021 flags = append(flags, test.flags...)
1022
1023 var shim *exec.Cmd
1024 if *useValgrind {
1025 shim = valgrindOf(false, shim_path, flags...)
Adam Langley75712922014-10-10 16:23:43 -07001026 } else if *useGDB {
1027 shim = gdbOf(shim_path, flags...)
David Benjamin025b3d32014-07-01 19:53:04 -04001028 } else {
1029 shim = exec.Command(shim_path, flags...)
1030 }
David Benjamin1d5c83e2014-07-22 19:20:02 -04001031 shim.ExtraFiles = []*os.File{shimEnd, shimEndResume}
David Benjamin025b3d32014-07-01 19:53:04 -04001032 shim.Stdin = os.Stdin
1033 var stdoutBuf, stderrBuf bytes.Buffer
1034 shim.Stdout = &stdoutBuf
1035 shim.Stderr = &stderrBuf
Adam Langley69a01602014-11-17 17:26:55 -08001036 if mallocNumToFail >= 0 {
1037 shim.Env = []string{"MALLOC_NUMBER_TO_FAIL=" + strconv.FormatInt(mallocNumToFail, 10)}
1038 if *mallocTestDebug {
1039 shim.Env = append(shim.Env, "MALLOC_ABORT_ON_FAIL=1")
1040 }
1041 shim.Env = append(shim.Env, "_MALLOC_CHECK=1")
1042 }
David Benjamin025b3d32014-07-01 19:53:04 -04001043
1044 if err := shim.Start(); err != nil {
Adam Langley95c29f32014-06-20 12:00:00 -07001045 panic(err)
1046 }
David Benjamin025b3d32014-07-01 19:53:04 -04001047 shimEnd.Close()
David Benjamin1d5c83e2014-07-22 19:20:02 -04001048 shimEndResume.Close()
Adam Langley95c29f32014-06-20 12:00:00 -07001049
1050 config := test.config
David Benjamin1d5c83e2014-07-22 19:20:02 -04001051 config.ClientSessionCache = NewLRUClientSessionCache(1)
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001052 config.ServerSessionCache = NewLRUServerSessionCache(1)
David Benjamin025b3d32014-07-01 19:53:04 -04001053 if test.testType == clientTest {
1054 if len(config.Certificates) == 0 {
1055 config.Certificates = []Certificate{getRSACertificate()}
1056 }
David Benjamin025b3d32014-07-01 19:53:04 -04001057 }
Adam Langley95c29f32014-06-20 12:00:00 -07001058
David Benjamin01fe8202014-09-24 15:21:44 -04001059 err := doExchange(test, &config, conn, test.messageLen,
1060 false /* not a resumption */)
Adam Langley95c29f32014-06-20 12:00:00 -07001061 conn.Close()
David Benjamin65ea8ff2014-11-23 03:01:00 -05001062
David Benjamin1d5c83e2014-07-22 19:20:02 -04001063 if err == nil && test.resumeSession {
David Benjamin01fe8202014-09-24 15:21:44 -04001064 var resumeConfig Config
1065 if test.resumeConfig != nil {
1066 resumeConfig = *test.resumeConfig
1067 if len(resumeConfig.Certificates) == 0 {
1068 resumeConfig.Certificates = []Certificate{getRSACertificate()}
1069 }
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001070 if !test.newSessionsOnResume {
1071 resumeConfig.SessionTicketKey = config.SessionTicketKey
1072 resumeConfig.ClientSessionCache = config.ClientSessionCache
1073 resumeConfig.ServerSessionCache = config.ServerSessionCache
1074 }
David Benjamin01fe8202014-09-24 15:21:44 -04001075 } else {
1076 resumeConfig = config
1077 }
1078 err = doExchange(test, &resumeConfig, connResume, test.messageLen,
1079 true /* resumption */)
David Benjamin1d5c83e2014-07-22 19:20:02 -04001080 }
David Benjamin812152a2014-09-06 12:49:07 -04001081 connResume.Close()
David Benjamin1d5c83e2014-07-22 19:20:02 -04001082
David Benjamin025b3d32014-07-01 19:53:04 -04001083 childErr := shim.Wait()
Adam Langley69a01602014-11-17 17:26:55 -08001084 if exitError, ok := childErr.(*exec.ExitError); ok {
1085 if exitError.Sys().(syscall.WaitStatus).ExitStatus() == 88 {
1086 return errMoreMallocs
1087 }
1088 }
Adam Langley95c29f32014-06-20 12:00:00 -07001089
1090 stdout := string(stdoutBuf.Bytes())
1091 stderr := string(stderrBuf.Bytes())
1092 failed := err != nil || childErr != nil
1093 correctFailure := len(test.expectedError) == 0 || strings.Contains(stdout, test.expectedError)
Adam Langleyac61fa32014-06-23 12:03:11 -07001094 localError := "none"
1095 if err != nil {
1096 localError = err.Error()
1097 }
1098 if len(test.expectedLocalError) != 0 {
1099 correctFailure = correctFailure && strings.Contains(localError, test.expectedLocalError)
1100 }
Adam Langley95c29f32014-06-20 12:00:00 -07001101
1102 if failed != test.shouldFail || failed && !correctFailure {
Adam Langley95c29f32014-06-20 12:00:00 -07001103 childError := "none"
Adam Langley95c29f32014-06-20 12:00:00 -07001104 if childErr != nil {
1105 childError = childErr.Error()
1106 }
1107
1108 var msg string
1109 switch {
1110 case failed && !test.shouldFail:
1111 msg = "unexpected failure"
1112 case !failed && test.shouldFail:
1113 msg = "unexpected success"
1114 case failed && !correctFailure:
Adam Langleyac61fa32014-06-23 12:03:11 -07001115 msg = "bad error (wanted '" + test.expectedError + "' / '" + test.expectedLocalError + "')"
Adam Langley95c29f32014-06-20 12:00:00 -07001116 default:
1117 panic("internal error")
1118 }
1119
1120 return fmt.Errorf("%s: local error '%s', child error '%s', stdout:\n%s\nstderr:\n%s", msg, localError, childError, string(stdoutBuf.Bytes()), stderr)
1121 }
1122
1123 if !*useValgrind && len(stderr) > 0 {
1124 println(stderr)
1125 }
1126
1127 return nil
1128}
1129
1130var tlsVersions = []struct {
1131 name string
1132 version uint16
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001133 flag string
David Benjamin8b8c0062014-11-23 02:47:52 -05001134 hasDTLS bool
Adam Langley95c29f32014-06-20 12:00:00 -07001135}{
David Benjamin8b8c0062014-11-23 02:47:52 -05001136 {"SSL3", VersionSSL30, "-no-ssl3", false},
1137 {"TLS1", VersionTLS10, "-no-tls1", true},
1138 {"TLS11", VersionTLS11, "-no-tls11", false},
1139 {"TLS12", VersionTLS12, "-no-tls12", true},
Adam Langley95c29f32014-06-20 12:00:00 -07001140}
1141
1142var testCipherSuites = []struct {
1143 name string
1144 id uint16
1145}{
1146 {"3DES-SHA", TLS_RSA_WITH_3DES_EDE_CBC_SHA},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001147 {"AES128-GCM", TLS_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001148 {"AES128-SHA", TLS_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001149 {"AES128-SHA256", TLS_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001150 {"AES256-GCM", TLS_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001151 {"AES256-SHA", TLS_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001152 {"AES256-SHA256", TLS_RSA_WITH_AES_256_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001153 {"DHE-RSA-AES128-GCM", TLS_DHE_RSA_WITH_AES_128_GCM_SHA256},
1154 {"DHE-RSA-AES128-SHA", TLS_DHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001155 {"DHE-RSA-AES128-SHA256", TLS_DHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001156 {"DHE-RSA-AES256-GCM", TLS_DHE_RSA_WITH_AES_256_GCM_SHA384},
1157 {"DHE-RSA-AES256-SHA", TLS_DHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001158 {"DHE-RSA-AES256-SHA256", TLS_DHE_RSA_WITH_AES_256_CBC_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001159 {"ECDHE-ECDSA-AES128-GCM", TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
1160 {"ECDHE-ECDSA-AES128-SHA", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001161 {"ECDHE-ECDSA-AES128-SHA256", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256},
1162 {"ECDHE-ECDSA-AES256-GCM", TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001163 {"ECDHE-ECDSA-AES256-SHA", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001164 {"ECDHE-ECDSA-AES256-SHA384", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001165 {"ECDHE-ECDSA-RC4-SHA", TLS_ECDHE_ECDSA_WITH_RC4_128_SHA},
David Benjamin2af684f2014-10-27 02:23:15 -04001166 {"ECDHE-PSK-WITH-AES-128-GCM-SHA256", TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001167 {"ECDHE-RSA-AES128-GCM", TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001168 {"ECDHE-RSA-AES128-SHA", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001169 {"ECDHE-RSA-AES128-SHA256", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001170 {"ECDHE-RSA-AES256-GCM", TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001171 {"ECDHE-RSA-AES256-SHA", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001172 {"ECDHE-RSA-AES256-SHA384", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001173 {"ECDHE-RSA-RC4-SHA", TLS_ECDHE_RSA_WITH_RC4_128_SHA},
David Benjamin48cae082014-10-27 01:06:24 -04001174 {"PSK-AES128-CBC-SHA", TLS_PSK_WITH_AES_128_CBC_SHA},
1175 {"PSK-AES256-CBC-SHA", TLS_PSK_WITH_AES_256_CBC_SHA},
1176 {"PSK-RC4-SHA", TLS_PSK_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -07001177 {"RC4-MD5", TLS_RSA_WITH_RC4_128_MD5},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001178 {"RC4-SHA", TLS_RSA_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -07001179}
1180
David Benjamin8b8c0062014-11-23 02:47:52 -05001181func hasComponent(suiteName, component string) bool {
1182 return strings.Contains("-"+suiteName+"-", "-"+component+"-")
1183}
1184
David Benjaminf7768e42014-08-31 02:06:47 -04001185func isTLS12Only(suiteName string) bool {
David Benjamin8b8c0062014-11-23 02:47:52 -05001186 return hasComponent(suiteName, "GCM") ||
1187 hasComponent(suiteName, "SHA256") ||
1188 hasComponent(suiteName, "SHA384")
1189}
1190
1191func isDTLSCipher(suiteName string) bool {
David Benjamine95d20d2014-12-23 11:16:01 -05001192 return !hasComponent(suiteName, "RC4")
David Benjaminf7768e42014-08-31 02:06:47 -04001193}
1194
Adam Langley95c29f32014-06-20 12:00:00 -07001195func addCipherSuiteTests() {
1196 for _, suite := range testCipherSuites {
David Benjamin48cae082014-10-27 01:06:24 -04001197 const psk = "12345"
1198 const pskIdentity = "luggage combo"
1199
Adam Langley95c29f32014-06-20 12:00:00 -07001200 var cert Certificate
David Benjamin025b3d32014-07-01 19:53:04 -04001201 var certFile string
1202 var keyFile string
David Benjamin8b8c0062014-11-23 02:47:52 -05001203 if hasComponent(suite.name, "ECDSA") {
Adam Langley95c29f32014-06-20 12:00:00 -07001204 cert = getECDSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -04001205 certFile = ecdsaCertificateFile
1206 keyFile = ecdsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -07001207 } else {
1208 cert = getRSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -04001209 certFile = rsaCertificateFile
1210 keyFile = rsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -07001211 }
1212
David Benjamin48cae082014-10-27 01:06:24 -04001213 var flags []string
David Benjamin8b8c0062014-11-23 02:47:52 -05001214 if hasComponent(suite.name, "PSK") {
David Benjamin48cae082014-10-27 01:06:24 -04001215 flags = append(flags,
1216 "-psk", psk,
1217 "-psk-identity", pskIdentity)
1218 }
1219
Adam Langley95c29f32014-06-20 12:00:00 -07001220 for _, ver := range tlsVersions {
David Benjaminf7768e42014-08-31 02:06:47 -04001221 if ver.version < VersionTLS12 && isTLS12Only(suite.name) {
Adam Langley95c29f32014-06-20 12:00:00 -07001222 continue
1223 }
1224
David Benjamin025b3d32014-07-01 19:53:04 -04001225 testCases = append(testCases, testCase{
1226 testType: clientTest,
1227 name: ver.name + "-" + suite.name + "-client",
Adam Langley95c29f32014-06-20 12:00:00 -07001228 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001229 MinVersion: ver.version,
1230 MaxVersion: ver.version,
1231 CipherSuites: []uint16{suite.id},
1232 Certificates: []Certificate{cert},
1233 PreSharedKey: []byte(psk),
1234 PreSharedKeyIdentity: pskIdentity,
Adam Langley95c29f32014-06-20 12:00:00 -07001235 },
David Benjamin48cae082014-10-27 01:06:24 -04001236 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001237 resumeSession: true,
Adam Langley95c29f32014-06-20 12:00:00 -07001238 })
David Benjamin025b3d32014-07-01 19:53:04 -04001239
David Benjamin76d8abe2014-08-14 16:25:34 -04001240 testCases = append(testCases, testCase{
1241 testType: serverTest,
1242 name: ver.name + "-" + suite.name + "-server",
1243 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001244 MinVersion: ver.version,
1245 MaxVersion: ver.version,
1246 CipherSuites: []uint16{suite.id},
1247 Certificates: []Certificate{cert},
1248 PreSharedKey: []byte(psk),
1249 PreSharedKeyIdentity: pskIdentity,
David Benjamin76d8abe2014-08-14 16:25:34 -04001250 },
1251 certFile: certFile,
1252 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001253 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001254 resumeSession: true,
David Benjamin76d8abe2014-08-14 16:25:34 -04001255 })
David Benjamin6fd297b2014-08-11 18:43:38 -04001256
David Benjamin8b8c0062014-11-23 02:47:52 -05001257 if ver.hasDTLS && isDTLSCipher(suite.name) {
David Benjamin6fd297b2014-08-11 18:43:38 -04001258 testCases = append(testCases, testCase{
1259 testType: clientTest,
1260 protocol: dtls,
1261 name: "D" + ver.name + "-" + suite.name + "-client",
1262 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001263 MinVersion: ver.version,
1264 MaxVersion: ver.version,
1265 CipherSuites: []uint16{suite.id},
1266 Certificates: []Certificate{cert},
1267 PreSharedKey: []byte(psk),
1268 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001269 },
David Benjamin48cae082014-10-27 01:06:24 -04001270 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001271 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001272 })
1273 testCases = append(testCases, testCase{
1274 testType: serverTest,
1275 protocol: dtls,
1276 name: "D" + ver.name + "-" + suite.name + "-server",
1277 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001278 MinVersion: ver.version,
1279 MaxVersion: ver.version,
1280 CipherSuites: []uint16{suite.id},
1281 Certificates: []Certificate{cert},
1282 PreSharedKey: []byte(psk),
1283 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001284 },
1285 certFile: certFile,
1286 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001287 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001288 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001289 })
1290 }
Adam Langley95c29f32014-06-20 12:00:00 -07001291 }
1292 }
1293}
1294
1295func addBadECDSASignatureTests() {
1296 for badR := BadValue(1); badR < NumBadValues; badR++ {
1297 for badS := BadValue(1); badS < NumBadValues; badS++ {
David Benjamin025b3d32014-07-01 19:53:04 -04001298 testCases = append(testCases, testCase{
Adam Langley95c29f32014-06-20 12:00:00 -07001299 name: fmt.Sprintf("BadECDSA-%d-%d", badR, badS),
1300 config: Config{
1301 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
1302 Certificates: []Certificate{getECDSACertificate()},
1303 Bugs: ProtocolBugs{
1304 BadECDSAR: badR,
1305 BadECDSAS: badS,
1306 },
1307 },
1308 shouldFail: true,
1309 expectedError: "SIGNATURE",
1310 })
1311 }
1312 }
1313}
1314
Adam Langley80842bd2014-06-20 12:00:00 -07001315func addCBCPaddingTests() {
David Benjamin025b3d32014-07-01 19:53:04 -04001316 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001317 name: "MaxCBCPadding",
1318 config: Config{
1319 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1320 Bugs: ProtocolBugs{
1321 MaxPadding: true,
1322 },
1323 },
1324 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1325 })
David Benjamin025b3d32014-07-01 19:53:04 -04001326 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001327 name: "BadCBCPadding",
1328 config: Config{
1329 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1330 Bugs: ProtocolBugs{
1331 PaddingFirstByteBad: true,
1332 },
1333 },
1334 shouldFail: true,
1335 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1336 })
1337 // OpenSSL previously had an issue where the first byte of padding in
1338 // 255 bytes of padding wasn't checked.
David Benjamin025b3d32014-07-01 19:53:04 -04001339 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001340 name: "BadCBCPadding255",
1341 config: Config{
1342 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1343 Bugs: ProtocolBugs{
1344 MaxPadding: true,
1345 PaddingFirstByteBadIf255: true,
1346 },
1347 },
1348 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1349 shouldFail: true,
1350 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1351 })
1352}
1353
Kenny Root7fdeaf12014-08-05 15:23:37 -07001354func addCBCSplittingTests() {
1355 testCases = append(testCases, testCase{
1356 name: "CBCRecordSplitting",
1357 config: Config{
1358 MaxVersion: VersionTLS10,
1359 MinVersion: VersionTLS10,
1360 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1361 },
1362 messageLen: -1, // read until EOF
1363 flags: []string{
1364 "-async",
1365 "-write-different-record-sizes",
1366 "-cbc-record-splitting",
1367 },
David Benjamina8e3e0e2014-08-06 22:11:10 -04001368 })
1369 testCases = append(testCases, testCase{
Kenny Root7fdeaf12014-08-05 15:23:37 -07001370 name: "CBCRecordSplittingPartialWrite",
1371 config: Config{
1372 MaxVersion: VersionTLS10,
1373 MinVersion: VersionTLS10,
1374 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1375 },
1376 messageLen: -1, // read until EOF
1377 flags: []string{
1378 "-async",
1379 "-write-different-record-sizes",
1380 "-cbc-record-splitting",
1381 "-partial-write",
1382 },
1383 })
1384}
1385
David Benjamin636293b2014-07-08 17:59:18 -04001386func addClientAuthTests() {
David Benjamin407a10c2014-07-16 12:58:59 -04001387 // Add a dummy cert pool to stress certificate authority parsing.
1388 // TODO(davidben): Add tests that those values parse out correctly.
1389 certPool := x509.NewCertPool()
1390 cert, err := x509.ParseCertificate(rsaCertificate.Certificate[0])
1391 if err != nil {
1392 panic(err)
1393 }
1394 certPool.AddCert(cert)
1395
David Benjamin636293b2014-07-08 17:59:18 -04001396 for _, ver := range tlsVersions {
David Benjamin636293b2014-07-08 17:59:18 -04001397 testCases = append(testCases, testCase{
1398 testType: clientTest,
David Benjamin67666e72014-07-12 15:47:52 -04001399 name: ver.name + "-Client-ClientAuth-RSA",
David Benjamin636293b2014-07-08 17:59:18 -04001400 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001401 MinVersion: ver.version,
1402 MaxVersion: ver.version,
1403 ClientAuth: RequireAnyClientCert,
1404 ClientCAs: certPool,
David Benjamin636293b2014-07-08 17:59:18 -04001405 },
1406 flags: []string{
1407 "-cert-file", rsaCertificateFile,
1408 "-key-file", rsaKeyFile,
1409 },
1410 })
1411 testCases = append(testCases, testCase{
David Benjamin67666e72014-07-12 15:47:52 -04001412 testType: serverTest,
1413 name: ver.name + "-Server-ClientAuth-RSA",
1414 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001415 MinVersion: ver.version,
1416 MaxVersion: ver.version,
David Benjamin67666e72014-07-12 15:47:52 -04001417 Certificates: []Certificate{rsaCertificate},
1418 },
1419 flags: []string{"-require-any-client-certificate"},
1420 })
David Benjamine098ec22014-08-27 23:13:20 -04001421 if ver.version != VersionSSL30 {
1422 testCases = append(testCases, testCase{
1423 testType: serverTest,
1424 name: ver.name + "-Server-ClientAuth-ECDSA",
1425 config: Config{
1426 MinVersion: ver.version,
1427 MaxVersion: ver.version,
1428 Certificates: []Certificate{ecdsaCertificate},
1429 },
1430 flags: []string{"-require-any-client-certificate"},
1431 })
1432 testCases = append(testCases, testCase{
1433 testType: clientTest,
1434 name: ver.name + "-Client-ClientAuth-ECDSA",
1435 config: Config{
1436 MinVersion: ver.version,
1437 MaxVersion: ver.version,
1438 ClientAuth: RequireAnyClientCert,
1439 ClientCAs: certPool,
1440 },
1441 flags: []string{
1442 "-cert-file", ecdsaCertificateFile,
1443 "-key-file", ecdsaKeyFile,
1444 },
1445 })
1446 }
David Benjamin636293b2014-07-08 17:59:18 -04001447 }
1448}
1449
Adam Langley75712922014-10-10 16:23:43 -07001450func addExtendedMasterSecretTests() {
1451 const expectEMSFlag = "-expect-extended-master-secret"
1452
1453 for _, with := range []bool{false, true} {
1454 prefix := "No"
1455 var flags []string
1456 if with {
1457 prefix = ""
1458 flags = []string{expectEMSFlag}
1459 }
1460
1461 for _, isClient := range []bool{false, true} {
1462 suffix := "-Server"
1463 testType := serverTest
1464 if isClient {
1465 suffix = "-Client"
1466 testType = clientTest
1467 }
1468
1469 for _, ver := range tlsVersions {
1470 test := testCase{
1471 testType: testType,
1472 name: prefix + "ExtendedMasterSecret-" + ver.name + suffix,
1473 config: Config{
1474 MinVersion: ver.version,
1475 MaxVersion: ver.version,
1476 Bugs: ProtocolBugs{
1477 NoExtendedMasterSecret: !with,
1478 RequireExtendedMasterSecret: with,
1479 },
1480 },
David Benjamin48cae082014-10-27 01:06:24 -04001481 flags: flags,
1482 shouldFail: ver.version == VersionSSL30 && with,
Adam Langley75712922014-10-10 16:23:43 -07001483 }
1484 if test.shouldFail {
1485 test.expectedLocalError = "extended master secret required but not supported by peer"
1486 }
1487 testCases = append(testCases, test)
1488 }
1489 }
1490 }
1491
1492 // When a session is resumed, it should still be aware that its master
1493 // secret was generated via EMS and thus it's safe to use tls-unique.
1494 testCases = append(testCases, testCase{
1495 name: "ExtendedMasterSecret-Resume",
1496 config: Config{
1497 Bugs: ProtocolBugs{
1498 RequireExtendedMasterSecret: true,
1499 },
1500 },
1501 flags: []string{expectEMSFlag},
1502 resumeSession: true,
1503 })
1504}
1505
David Benjamin43ec06f2014-08-05 02:28:57 -04001506// Adds tests that try to cover the range of the handshake state machine, under
1507// various conditions. Some of these are redundant with other tests, but they
1508// only cover the synchronous case.
David Benjamin6fd297b2014-08-11 18:43:38 -04001509func addStateMachineCoverageTests(async, splitHandshake bool, protocol protocol) {
David Benjamin43ec06f2014-08-05 02:28:57 -04001510 var suffix string
1511 var flags []string
1512 var maxHandshakeRecordLength int
David Benjamin6fd297b2014-08-11 18:43:38 -04001513 if protocol == dtls {
1514 suffix = "-DTLS"
1515 }
David Benjamin43ec06f2014-08-05 02:28:57 -04001516 if async {
David Benjamin6fd297b2014-08-11 18:43:38 -04001517 suffix += "-Async"
David Benjamin43ec06f2014-08-05 02:28:57 -04001518 flags = append(flags, "-async")
1519 } else {
David Benjamin6fd297b2014-08-11 18:43:38 -04001520 suffix += "-Sync"
David Benjamin43ec06f2014-08-05 02:28:57 -04001521 }
1522 if splitHandshake {
1523 suffix += "-SplitHandshakeRecords"
David Benjamin98214542014-08-07 18:02:39 -04001524 maxHandshakeRecordLength = 1
David Benjamin43ec06f2014-08-05 02:28:57 -04001525 }
1526
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001527 // Basic handshake, with resumption. Client and server,
1528 // session ID and session ticket.
David Benjamin43ec06f2014-08-05 02:28:57 -04001529 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001530 protocol: protocol,
1531 name: "Basic-Client" + suffix,
David Benjamin43ec06f2014-08-05 02:28:57 -04001532 config: Config{
1533 Bugs: ProtocolBugs{
1534 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1535 },
1536 },
David Benjaminbed9aae2014-08-07 19:13:38 -04001537 flags: flags,
1538 resumeSession: true,
1539 })
1540 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001541 protocol: protocol,
1542 name: "Basic-Client-RenewTicket" + suffix,
David Benjaminbed9aae2014-08-07 19:13:38 -04001543 config: Config{
1544 Bugs: ProtocolBugs{
1545 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1546 RenewTicketOnResume: true,
1547 },
1548 },
1549 flags: flags,
1550 resumeSession: true,
David Benjamin43ec06f2014-08-05 02:28:57 -04001551 })
1552 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001553 protocol: protocol,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001554 name: "Basic-Client-NoTicket" + suffix,
1555 config: Config{
1556 SessionTicketsDisabled: true,
1557 Bugs: ProtocolBugs{
1558 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1559 },
1560 },
1561 flags: flags,
1562 resumeSession: true,
1563 })
1564 testCases = append(testCases, testCase{
1565 protocol: protocol,
David Benjamin43ec06f2014-08-05 02:28:57 -04001566 testType: serverTest,
1567 name: "Basic-Server" + suffix,
1568 config: Config{
1569 Bugs: ProtocolBugs{
1570 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1571 },
1572 },
David Benjaminbed9aae2014-08-07 19:13:38 -04001573 flags: flags,
1574 resumeSession: true,
David Benjamin43ec06f2014-08-05 02:28:57 -04001575 })
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001576 testCases = append(testCases, testCase{
1577 protocol: protocol,
1578 testType: serverTest,
1579 name: "Basic-Server-NoTickets" + suffix,
1580 config: Config{
1581 SessionTicketsDisabled: true,
1582 Bugs: ProtocolBugs{
1583 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1584 },
1585 },
1586 flags: flags,
1587 resumeSession: true,
1588 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001589
David Benjamin6fd297b2014-08-11 18:43:38 -04001590 // TLS client auth.
1591 testCases = append(testCases, testCase{
1592 protocol: protocol,
1593 testType: clientTest,
1594 name: "ClientAuth-Client" + suffix,
1595 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001596 ClientAuth: RequireAnyClientCert,
David Benjamin6fd297b2014-08-11 18:43:38 -04001597 Bugs: ProtocolBugs{
1598 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1599 },
1600 },
1601 flags: append(flags,
1602 "-cert-file", rsaCertificateFile,
1603 "-key-file", rsaKeyFile),
1604 })
1605 testCases = append(testCases, testCase{
1606 protocol: protocol,
1607 testType: serverTest,
1608 name: "ClientAuth-Server" + suffix,
1609 config: Config{
1610 Certificates: []Certificate{rsaCertificate},
1611 },
1612 flags: append(flags, "-require-any-client-certificate"),
1613 })
1614
David Benjamin43ec06f2014-08-05 02:28:57 -04001615 // No session ticket support; server doesn't send NewSessionTicket.
1616 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001617 protocol: protocol,
1618 name: "SessionTicketsDisabled-Client" + suffix,
David Benjamin43ec06f2014-08-05 02:28:57 -04001619 config: Config{
1620 SessionTicketsDisabled: true,
1621 Bugs: ProtocolBugs{
1622 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1623 },
1624 },
1625 flags: flags,
1626 })
1627 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001628 protocol: protocol,
David Benjamin43ec06f2014-08-05 02:28:57 -04001629 testType: serverTest,
1630 name: "SessionTicketsDisabled-Server" + suffix,
1631 config: Config{
1632 SessionTicketsDisabled: true,
1633 Bugs: ProtocolBugs{
1634 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1635 },
1636 },
1637 flags: flags,
1638 })
1639
David Benjamin48cae082014-10-27 01:06:24 -04001640 // Skip ServerKeyExchange in PSK key exchange if there's no
1641 // identity hint.
1642 testCases = append(testCases, testCase{
1643 protocol: protocol,
1644 name: "EmptyPSKHint-Client" + suffix,
1645 config: Config{
1646 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
1647 PreSharedKey: []byte("secret"),
1648 Bugs: ProtocolBugs{
1649 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1650 },
1651 },
1652 flags: append(flags, "-psk", "secret"),
1653 })
1654 testCases = append(testCases, testCase{
1655 protocol: protocol,
1656 testType: serverTest,
1657 name: "EmptyPSKHint-Server" + suffix,
1658 config: Config{
1659 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
1660 PreSharedKey: []byte("secret"),
1661 Bugs: ProtocolBugs{
1662 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1663 },
1664 },
1665 flags: append(flags, "-psk", "secret"),
1666 })
1667
David Benjamin6fd297b2014-08-11 18:43:38 -04001668 if protocol == tls {
1669 // NPN on client and server; results in post-handshake message.
1670 testCases = append(testCases, testCase{
1671 protocol: protocol,
1672 name: "NPN-Client" + suffix,
1673 config: Config{
David Benjaminae2888f2014-09-06 12:58:58 -04001674 NextProtos: []string{"foo"},
David Benjamin6fd297b2014-08-11 18:43:38 -04001675 Bugs: ProtocolBugs{
1676 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1677 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001678 },
David Benjaminfc7b0862014-09-06 13:21:53 -04001679 flags: append(flags, "-select-next-proto", "foo"),
1680 expectedNextProto: "foo",
1681 expectedNextProtoType: npn,
David Benjamin6fd297b2014-08-11 18:43:38 -04001682 })
1683 testCases = append(testCases, testCase{
1684 protocol: protocol,
1685 testType: serverTest,
1686 name: "NPN-Server" + suffix,
1687 config: Config{
1688 NextProtos: []string{"bar"},
1689 Bugs: ProtocolBugs{
1690 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1691 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001692 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001693 flags: append(flags,
1694 "-advertise-npn", "\x03foo\x03bar\x03baz",
1695 "-expect-next-proto", "bar"),
David Benjaminfc7b0862014-09-06 13:21:53 -04001696 expectedNextProto: "bar",
1697 expectedNextProtoType: npn,
David Benjamin6fd297b2014-08-11 18:43:38 -04001698 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001699
David Benjamin6fd297b2014-08-11 18:43:38 -04001700 // Client does False Start and negotiates NPN.
1701 testCases = append(testCases, testCase{
1702 protocol: protocol,
1703 name: "FalseStart" + suffix,
1704 config: Config{
1705 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1706 NextProtos: []string{"foo"},
1707 Bugs: ProtocolBugs{
David Benjamine58c4f52014-08-24 03:47:07 -04001708 ExpectFalseStart: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001709 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1710 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001711 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001712 flags: append(flags,
1713 "-false-start",
1714 "-select-next-proto", "foo"),
David Benjamine58c4f52014-08-24 03:47:07 -04001715 shimWritesFirst: true,
1716 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001717 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001718
David Benjaminae2888f2014-09-06 12:58:58 -04001719 // Client does False Start and negotiates ALPN.
1720 testCases = append(testCases, testCase{
1721 protocol: protocol,
1722 name: "FalseStart-ALPN" + suffix,
1723 config: Config{
1724 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1725 NextProtos: []string{"foo"},
1726 Bugs: ProtocolBugs{
1727 ExpectFalseStart: true,
1728 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1729 },
1730 },
1731 flags: append(flags,
1732 "-false-start",
1733 "-advertise-alpn", "\x03foo"),
1734 shimWritesFirst: true,
1735 resumeSession: true,
1736 })
1737
David Benjamin6fd297b2014-08-11 18:43:38 -04001738 // False Start without session tickets.
1739 testCases = append(testCases, testCase{
1740 name: "FalseStart-SessionTicketsDisabled",
1741 config: Config{
1742 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1743 NextProtos: []string{"foo"},
1744 SessionTicketsDisabled: true,
David Benjamin4e99c522014-08-24 01:45:30 -04001745 Bugs: ProtocolBugs{
David Benjamine58c4f52014-08-24 03:47:07 -04001746 ExpectFalseStart: true,
David Benjamin4e99c522014-08-24 01:45:30 -04001747 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1748 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001749 },
David Benjamin4e99c522014-08-24 01:45:30 -04001750 flags: append(flags,
David Benjamin6fd297b2014-08-11 18:43:38 -04001751 "-false-start",
1752 "-select-next-proto", "foo",
David Benjamin4e99c522014-08-24 01:45:30 -04001753 ),
David Benjamine58c4f52014-08-24 03:47:07 -04001754 shimWritesFirst: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001755 })
David Benjamin1e7f8d72014-08-08 12:27:04 -04001756
David Benjamina08e49d2014-08-24 01:46:07 -04001757 // Server parses a V2ClientHello.
David Benjamin6fd297b2014-08-11 18:43:38 -04001758 testCases = append(testCases, testCase{
1759 protocol: protocol,
1760 testType: serverTest,
1761 name: "SendV2ClientHello" + suffix,
1762 config: Config{
1763 // Choose a cipher suite that does not involve
1764 // elliptic curves, so no extensions are
1765 // involved.
1766 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
1767 Bugs: ProtocolBugs{
1768 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1769 SendV2ClientHello: true,
1770 },
David Benjamin1e7f8d72014-08-08 12:27:04 -04001771 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001772 flags: flags,
1773 })
David Benjamina08e49d2014-08-24 01:46:07 -04001774
1775 // Client sends a Channel ID.
1776 testCases = append(testCases, testCase{
1777 protocol: protocol,
1778 name: "ChannelID-Client" + suffix,
1779 config: Config{
1780 RequestChannelID: true,
1781 Bugs: ProtocolBugs{
1782 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1783 },
1784 },
1785 flags: append(flags,
1786 "-send-channel-id", channelIDKeyFile,
1787 ),
1788 resumeSession: true,
1789 expectChannelID: true,
1790 })
1791
1792 // Server accepts a Channel ID.
1793 testCases = append(testCases, testCase{
1794 protocol: protocol,
1795 testType: serverTest,
1796 name: "ChannelID-Server" + suffix,
1797 config: Config{
1798 ChannelID: channelIDKey,
1799 Bugs: ProtocolBugs{
1800 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1801 },
1802 },
1803 flags: append(flags,
1804 "-expect-channel-id",
1805 base64.StdEncoding.EncodeToString(channelIDBytes),
1806 ),
1807 resumeSession: true,
1808 expectChannelID: true,
1809 })
David Benjamin6fd297b2014-08-11 18:43:38 -04001810 } else {
1811 testCases = append(testCases, testCase{
1812 protocol: protocol,
1813 name: "SkipHelloVerifyRequest" + suffix,
1814 config: Config{
1815 Bugs: ProtocolBugs{
1816 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1817 SkipHelloVerifyRequest: true,
1818 },
1819 },
1820 flags: flags,
1821 })
1822
1823 testCases = append(testCases, testCase{
1824 testType: serverTest,
1825 protocol: protocol,
1826 name: "CookieExchange" + suffix,
1827 config: Config{
1828 Bugs: ProtocolBugs{
1829 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1830 },
1831 },
1832 flags: append(flags, "-cookie-exchange"),
1833 })
1834 }
David Benjamin43ec06f2014-08-05 02:28:57 -04001835}
1836
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001837func addVersionNegotiationTests() {
1838 for i, shimVers := range tlsVersions {
1839 // Assemble flags to disable all newer versions on the shim.
1840 var flags []string
1841 for _, vers := range tlsVersions[i+1:] {
1842 flags = append(flags, vers.flag)
1843 }
1844
1845 for _, runnerVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05001846 protocols := []protocol{tls}
1847 if runnerVers.hasDTLS && shimVers.hasDTLS {
1848 protocols = append(protocols, dtls)
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001849 }
David Benjamin8b8c0062014-11-23 02:47:52 -05001850 for _, protocol := range protocols {
1851 expectedVersion := shimVers.version
1852 if runnerVers.version < shimVers.version {
1853 expectedVersion = runnerVers.version
1854 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001855
David Benjamin8b8c0062014-11-23 02:47:52 -05001856 suffix := shimVers.name + "-" + runnerVers.name
1857 if protocol == dtls {
1858 suffix += "-DTLS"
1859 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001860
David Benjamin1eb367c2014-12-12 18:17:51 -05001861 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
1862
David Benjamin1e29a6b2014-12-10 02:27:24 -05001863 clientVers := shimVers.version
1864 if clientVers > VersionTLS10 {
1865 clientVers = VersionTLS10
1866 }
David Benjamin8b8c0062014-11-23 02:47:52 -05001867 testCases = append(testCases, testCase{
1868 protocol: protocol,
1869 testType: clientTest,
1870 name: "VersionNegotiation-Client-" + suffix,
1871 config: Config{
1872 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05001873 Bugs: ProtocolBugs{
1874 ExpectInitialRecordVersion: clientVers,
1875 },
David Benjamin8b8c0062014-11-23 02:47:52 -05001876 },
1877 flags: flags,
1878 expectedVersion: expectedVersion,
1879 })
David Benjamin1eb367c2014-12-12 18:17:51 -05001880 testCases = append(testCases, testCase{
1881 protocol: protocol,
1882 testType: clientTest,
1883 name: "VersionNegotiation-Client2-" + suffix,
1884 config: Config{
1885 MaxVersion: runnerVers.version,
1886 Bugs: ProtocolBugs{
1887 ExpectInitialRecordVersion: clientVers,
1888 },
1889 },
1890 flags: []string{"-max-version", shimVersFlag},
1891 expectedVersion: expectedVersion,
1892 })
David Benjamin8b8c0062014-11-23 02:47:52 -05001893
1894 testCases = append(testCases, testCase{
1895 protocol: protocol,
1896 testType: serverTest,
1897 name: "VersionNegotiation-Server-" + suffix,
1898 config: Config{
1899 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05001900 Bugs: ProtocolBugs{
1901 ExpectInitialRecordVersion: expectedVersion,
1902 },
David Benjamin8b8c0062014-11-23 02:47:52 -05001903 },
1904 flags: flags,
1905 expectedVersion: expectedVersion,
1906 })
David Benjamin1eb367c2014-12-12 18:17:51 -05001907 testCases = append(testCases, testCase{
1908 protocol: protocol,
1909 testType: serverTest,
1910 name: "VersionNegotiation-Server2-" + suffix,
1911 config: Config{
1912 MaxVersion: runnerVers.version,
1913 Bugs: ProtocolBugs{
1914 ExpectInitialRecordVersion: expectedVersion,
1915 },
1916 },
1917 flags: []string{"-max-version", shimVersFlag},
1918 expectedVersion: expectedVersion,
1919 })
David Benjamin8b8c0062014-11-23 02:47:52 -05001920 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001921 }
1922 }
1923}
1924
David Benjaminaccb4542014-12-12 23:44:33 -05001925func addMinimumVersionTests() {
1926 for i, shimVers := range tlsVersions {
1927 // Assemble flags to disable all older versions on the shim.
1928 var flags []string
1929 for _, vers := range tlsVersions[:i] {
1930 flags = append(flags, vers.flag)
1931 }
1932
1933 for _, runnerVers := range tlsVersions {
1934 protocols := []protocol{tls}
1935 if runnerVers.hasDTLS && shimVers.hasDTLS {
1936 protocols = append(protocols, dtls)
1937 }
1938 for _, protocol := range protocols {
1939 suffix := shimVers.name + "-" + runnerVers.name
1940 if protocol == dtls {
1941 suffix += "-DTLS"
1942 }
1943 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
1944
David Benjaminaccb4542014-12-12 23:44:33 -05001945 var expectedVersion uint16
1946 var shouldFail bool
1947 var expectedError string
David Benjamin87909c02014-12-13 01:55:01 -05001948 var expectedLocalError string
David Benjaminaccb4542014-12-12 23:44:33 -05001949 if runnerVers.version >= shimVers.version {
1950 expectedVersion = runnerVers.version
1951 } else {
1952 shouldFail = true
1953 expectedError = ":UNSUPPORTED_PROTOCOL:"
David Benjamin87909c02014-12-13 01:55:01 -05001954 if runnerVers.version > VersionSSL30 {
1955 expectedLocalError = "remote error: protocol version not supported"
1956 } else {
1957 expectedLocalError = "remote error: handshake failure"
1958 }
David Benjaminaccb4542014-12-12 23:44:33 -05001959 }
1960
1961 testCases = append(testCases, testCase{
1962 protocol: protocol,
1963 testType: clientTest,
1964 name: "MinimumVersion-Client-" + suffix,
1965 config: Config{
1966 MaxVersion: runnerVers.version,
1967 },
David Benjamin87909c02014-12-13 01:55:01 -05001968 flags: flags,
1969 expectedVersion: expectedVersion,
1970 shouldFail: shouldFail,
1971 expectedError: expectedError,
1972 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05001973 })
1974 testCases = append(testCases, testCase{
1975 protocol: protocol,
1976 testType: clientTest,
1977 name: "MinimumVersion-Client2-" + suffix,
1978 config: Config{
1979 MaxVersion: runnerVers.version,
1980 },
David Benjamin87909c02014-12-13 01:55:01 -05001981 flags: []string{"-min-version", shimVersFlag},
1982 expectedVersion: expectedVersion,
1983 shouldFail: shouldFail,
1984 expectedError: expectedError,
1985 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05001986 })
1987
1988 testCases = append(testCases, testCase{
1989 protocol: protocol,
1990 testType: serverTest,
1991 name: "MinimumVersion-Server-" + suffix,
1992 config: Config{
1993 MaxVersion: runnerVers.version,
1994 },
David Benjamin87909c02014-12-13 01:55:01 -05001995 flags: flags,
1996 expectedVersion: expectedVersion,
1997 shouldFail: shouldFail,
1998 expectedError: expectedError,
1999 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002000 })
2001 testCases = append(testCases, testCase{
2002 protocol: protocol,
2003 testType: serverTest,
2004 name: "MinimumVersion-Server2-" + suffix,
2005 config: Config{
2006 MaxVersion: runnerVers.version,
2007 },
David Benjamin87909c02014-12-13 01:55:01 -05002008 flags: []string{"-min-version", shimVersFlag},
2009 expectedVersion: expectedVersion,
2010 shouldFail: shouldFail,
2011 expectedError: expectedError,
2012 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002013 })
2014 }
2015 }
2016 }
2017}
2018
David Benjamin5c24a1d2014-08-31 00:59:27 -04002019func addD5BugTests() {
2020 testCases = append(testCases, testCase{
2021 testType: serverTest,
2022 name: "D5Bug-NoQuirk-Reject",
2023 config: Config{
2024 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2025 Bugs: ProtocolBugs{
2026 SSL3RSAKeyExchange: true,
2027 },
2028 },
2029 shouldFail: true,
2030 expectedError: ":TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG:",
2031 })
2032 testCases = append(testCases, testCase{
2033 testType: serverTest,
2034 name: "D5Bug-Quirk-Normal",
2035 config: Config{
2036 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2037 },
2038 flags: []string{"-tls-d5-bug"},
2039 })
2040 testCases = append(testCases, testCase{
2041 testType: serverTest,
2042 name: "D5Bug-Quirk-Bug",
2043 config: Config{
2044 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2045 Bugs: ProtocolBugs{
2046 SSL3RSAKeyExchange: true,
2047 },
2048 },
2049 flags: []string{"-tls-d5-bug"},
2050 })
2051}
2052
David Benjamine78bfde2014-09-06 12:45:15 -04002053func addExtensionTests() {
2054 testCases = append(testCases, testCase{
2055 testType: clientTest,
2056 name: "DuplicateExtensionClient",
2057 config: Config{
2058 Bugs: ProtocolBugs{
2059 DuplicateExtension: true,
2060 },
2061 },
2062 shouldFail: true,
2063 expectedLocalError: "remote error: error decoding message",
2064 })
2065 testCases = append(testCases, testCase{
2066 testType: serverTest,
2067 name: "DuplicateExtensionServer",
2068 config: Config{
2069 Bugs: ProtocolBugs{
2070 DuplicateExtension: true,
2071 },
2072 },
2073 shouldFail: true,
2074 expectedLocalError: "remote error: error decoding message",
2075 })
2076 testCases = append(testCases, testCase{
2077 testType: clientTest,
2078 name: "ServerNameExtensionClient",
2079 config: Config{
2080 Bugs: ProtocolBugs{
2081 ExpectServerName: "example.com",
2082 },
2083 },
2084 flags: []string{"-host-name", "example.com"},
2085 })
2086 testCases = append(testCases, testCase{
2087 testType: clientTest,
2088 name: "ServerNameExtensionClient",
2089 config: Config{
2090 Bugs: ProtocolBugs{
2091 ExpectServerName: "mismatch.com",
2092 },
2093 },
2094 flags: []string{"-host-name", "example.com"},
2095 shouldFail: true,
2096 expectedLocalError: "tls: unexpected server name",
2097 })
2098 testCases = append(testCases, testCase{
2099 testType: clientTest,
2100 name: "ServerNameExtensionClient",
2101 config: Config{
2102 Bugs: ProtocolBugs{
2103 ExpectServerName: "missing.com",
2104 },
2105 },
2106 shouldFail: true,
2107 expectedLocalError: "tls: unexpected server name",
2108 })
2109 testCases = append(testCases, testCase{
2110 testType: serverTest,
2111 name: "ServerNameExtensionServer",
2112 config: Config{
2113 ServerName: "example.com",
2114 },
2115 flags: []string{"-expect-server-name", "example.com"},
2116 resumeSession: true,
2117 })
David Benjaminae2888f2014-09-06 12:58:58 -04002118 testCases = append(testCases, testCase{
2119 testType: clientTest,
2120 name: "ALPNClient",
2121 config: Config{
2122 NextProtos: []string{"foo"},
2123 },
2124 flags: []string{
2125 "-advertise-alpn", "\x03foo\x03bar\x03baz",
2126 "-expect-alpn", "foo",
2127 },
David Benjaminfc7b0862014-09-06 13:21:53 -04002128 expectedNextProto: "foo",
2129 expectedNextProtoType: alpn,
2130 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04002131 })
2132 testCases = append(testCases, testCase{
2133 testType: serverTest,
2134 name: "ALPNServer",
2135 config: Config{
2136 NextProtos: []string{"foo", "bar", "baz"},
2137 },
2138 flags: []string{
2139 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2140 "-select-alpn", "foo",
2141 },
David Benjaminfc7b0862014-09-06 13:21:53 -04002142 expectedNextProto: "foo",
2143 expectedNextProtoType: alpn,
2144 resumeSession: true,
2145 })
2146 // Test that the server prefers ALPN over NPN.
2147 testCases = append(testCases, testCase{
2148 testType: serverTest,
2149 name: "ALPNServer-Preferred",
2150 config: Config{
2151 NextProtos: []string{"foo", "bar", "baz"},
2152 },
2153 flags: []string{
2154 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2155 "-select-alpn", "foo",
2156 "-advertise-npn", "\x03foo\x03bar\x03baz",
2157 },
2158 expectedNextProto: "foo",
2159 expectedNextProtoType: alpn,
2160 resumeSession: true,
2161 })
2162 testCases = append(testCases, testCase{
2163 testType: serverTest,
2164 name: "ALPNServer-Preferred-Swapped",
2165 config: Config{
2166 NextProtos: []string{"foo", "bar", "baz"},
2167 Bugs: ProtocolBugs{
2168 SwapNPNAndALPN: true,
2169 },
2170 },
2171 flags: []string{
2172 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2173 "-select-alpn", "foo",
2174 "-advertise-npn", "\x03foo\x03bar\x03baz",
2175 },
2176 expectedNextProto: "foo",
2177 expectedNextProtoType: alpn,
2178 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04002179 })
Adam Langley38311732014-10-16 19:04:35 -07002180 // Resume with a corrupt ticket.
2181 testCases = append(testCases, testCase{
2182 testType: serverTest,
2183 name: "CorruptTicket",
2184 config: Config{
2185 Bugs: ProtocolBugs{
2186 CorruptTicket: true,
2187 },
2188 },
2189 resumeSession: true,
2190 flags: []string{"-expect-session-miss"},
2191 })
2192 // Resume with an oversized session id.
2193 testCases = append(testCases, testCase{
2194 testType: serverTest,
2195 name: "OversizedSessionId",
2196 config: Config{
2197 Bugs: ProtocolBugs{
2198 OversizedSessionId: true,
2199 },
2200 },
2201 resumeSession: true,
Adam Langley75712922014-10-10 16:23:43 -07002202 shouldFail: true,
Adam Langley38311732014-10-16 19:04:35 -07002203 expectedError: ":DECODE_ERROR:",
2204 })
David Benjaminca6c8262014-11-15 19:06:08 -05002205 // Basic DTLS-SRTP tests. Include fake profiles to ensure they
2206 // are ignored.
2207 testCases = append(testCases, testCase{
2208 protocol: dtls,
2209 name: "SRTP-Client",
2210 config: Config{
2211 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
2212 },
2213 flags: []string{
2214 "-srtp-profiles",
2215 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2216 },
2217 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2218 })
2219 testCases = append(testCases, testCase{
2220 protocol: dtls,
2221 testType: serverTest,
2222 name: "SRTP-Server",
2223 config: Config{
2224 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
2225 },
2226 flags: []string{
2227 "-srtp-profiles",
2228 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2229 },
2230 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2231 })
2232 // Test that the MKI is ignored.
2233 testCases = append(testCases, testCase{
2234 protocol: dtls,
2235 testType: serverTest,
2236 name: "SRTP-Server-IgnoreMKI",
2237 config: Config{
2238 SRTPProtectionProfiles: []uint16{SRTP_AES128_CM_HMAC_SHA1_80},
2239 Bugs: ProtocolBugs{
2240 SRTPMasterKeyIdentifer: "bogus",
2241 },
2242 },
2243 flags: []string{
2244 "-srtp-profiles",
2245 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2246 },
2247 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2248 })
2249 // Test that SRTP isn't negotiated on the server if there were
2250 // no matching profiles.
2251 testCases = append(testCases, testCase{
2252 protocol: dtls,
2253 testType: serverTest,
2254 name: "SRTP-Server-NoMatch",
2255 config: Config{
2256 SRTPProtectionProfiles: []uint16{100, 101, 102},
2257 },
2258 flags: []string{
2259 "-srtp-profiles",
2260 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2261 },
2262 expectedSRTPProtectionProfile: 0,
2263 })
2264 // Test that the server returning an invalid SRTP profile is
2265 // flagged as an error by the client.
2266 testCases = append(testCases, testCase{
2267 protocol: dtls,
2268 name: "SRTP-Client-NoMatch",
2269 config: Config{
2270 Bugs: ProtocolBugs{
2271 SendSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_32,
2272 },
2273 },
2274 flags: []string{
2275 "-srtp-profiles",
2276 "SRTP_AES128_CM_SHA1_80",
2277 },
2278 shouldFail: true,
2279 expectedError: ":BAD_SRTP_PROTECTION_PROFILE_LIST:",
2280 })
David Benjamin61f95272014-11-25 01:55:35 -05002281 // Test OCSP stapling and SCT list.
2282 testCases = append(testCases, testCase{
2283 name: "OCSPStapling",
2284 flags: []string{
2285 "-enable-ocsp-stapling",
2286 "-expect-ocsp-response",
2287 base64.StdEncoding.EncodeToString(testOCSPResponse),
2288 },
2289 })
2290 testCases = append(testCases, testCase{
2291 name: "SignedCertificateTimestampList",
2292 flags: []string{
2293 "-enable-signed-cert-timestamps",
2294 "-expect-signed-cert-timestamps",
2295 base64.StdEncoding.EncodeToString(testSCTList),
2296 },
2297 })
David Benjamine78bfde2014-09-06 12:45:15 -04002298}
2299
David Benjamin01fe8202014-09-24 15:21:44 -04002300func addResumptionVersionTests() {
David Benjamin01fe8202014-09-24 15:21:44 -04002301 for _, sessionVers := range tlsVersions {
David Benjamin01fe8202014-09-24 15:21:44 -04002302 for _, resumeVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05002303 protocols := []protocol{tls}
2304 if sessionVers.hasDTLS && resumeVers.hasDTLS {
2305 protocols = append(protocols, dtls)
David Benjaminbdf5e722014-11-11 00:52:15 -05002306 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002307 for _, protocol := range protocols {
2308 suffix := "-" + sessionVers.name + "-" + resumeVers.name
2309 if protocol == dtls {
2310 suffix += "-DTLS"
2311 }
2312
2313 testCases = append(testCases, testCase{
2314 protocol: protocol,
2315 name: "Resume-Client" + suffix,
2316 resumeSession: true,
2317 config: Config{
2318 MaxVersion: sessionVers.version,
2319 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2320 Bugs: ProtocolBugs{
2321 AllowSessionVersionMismatch: true,
2322 },
2323 },
2324 expectedVersion: sessionVers.version,
2325 resumeConfig: &Config{
2326 MaxVersion: resumeVers.version,
2327 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2328 Bugs: ProtocolBugs{
2329 AllowSessionVersionMismatch: true,
2330 },
2331 },
2332 expectedResumeVersion: resumeVers.version,
2333 })
2334
2335 testCases = append(testCases, testCase{
2336 protocol: protocol,
2337 name: "Resume-Client-NoResume" + suffix,
2338 flags: []string{"-expect-session-miss"},
2339 resumeSession: true,
2340 config: Config{
2341 MaxVersion: sessionVers.version,
2342 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2343 },
2344 expectedVersion: sessionVers.version,
2345 resumeConfig: &Config{
2346 MaxVersion: resumeVers.version,
2347 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2348 },
2349 newSessionsOnResume: true,
2350 expectedResumeVersion: resumeVers.version,
2351 })
2352
2353 var flags []string
2354 if sessionVers.version != resumeVers.version {
2355 flags = append(flags, "-expect-session-miss")
2356 }
2357 testCases = append(testCases, testCase{
2358 protocol: protocol,
2359 testType: serverTest,
2360 name: "Resume-Server" + suffix,
2361 flags: flags,
2362 resumeSession: true,
2363 config: Config{
2364 MaxVersion: sessionVers.version,
2365 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2366 },
2367 expectedVersion: sessionVers.version,
2368 resumeConfig: &Config{
2369 MaxVersion: resumeVers.version,
2370 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2371 },
2372 expectedResumeVersion: resumeVers.version,
2373 })
2374 }
David Benjamin01fe8202014-09-24 15:21:44 -04002375 }
2376 }
2377}
2378
Adam Langley2ae77d22014-10-28 17:29:33 -07002379func addRenegotiationTests() {
2380 testCases = append(testCases, testCase{
2381 testType: serverTest,
2382 name: "Renegotiate-Server",
2383 flags: []string{"-renegotiate"},
2384 shimWritesFirst: true,
2385 })
2386 testCases = append(testCases, testCase{
2387 testType: serverTest,
2388 name: "Renegotiate-Server-EmptyExt",
2389 config: Config{
2390 Bugs: ProtocolBugs{
2391 EmptyRenegotiationInfo: true,
2392 },
2393 },
2394 flags: []string{"-renegotiate"},
2395 shimWritesFirst: true,
2396 shouldFail: true,
2397 expectedError: ":RENEGOTIATION_MISMATCH:",
2398 })
2399 testCases = append(testCases, testCase{
2400 testType: serverTest,
2401 name: "Renegotiate-Server-BadExt",
2402 config: Config{
2403 Bugs: ProtocolBugs{
2404 BadRenegotiationInfo: true,
2405 },
2406 },
2407 flags: []string{"-renegotiate"},
2408 shimWritesFirst: true,
2409 shouldFail: true,
2410 expectedError: ":RENEGOTIATION_MISMATCH:",
2411 })
David Benjaminca6554b2014-11-08 12:31:52 -05002412 testCases = append(testCases, testCase{
2413 testType: serverTest,
2414 name: "Renegotiate-Server-ClientInitiated",
2415 renegotiate: true,
2416 })
2417 testCases = append(testCases, testCase{
2418 testType: serverTest,
2419 name: "Renegotiate-Server-ClientInitiated-NoExt",
2420 renegotiate: true,
2421 config: Config{
2422 Bugs: ProtocolBugs{
2423 NoRenegotiationInfo: true,
2424 },
2425 },
2426 shouldFail: true,
2427 expectedError: ":UNSAFE_LEGACY_RENEGOTIATION_DISABLED:",
2428 })
2429 testCases = append(testCases, testCase{
2430 testType: serverTest,
2431 name: "Renegotiate-Server-ClientInitiated-NoExt-Allowed",
2432 renegotiate: true,
2433 config: Config{
2434 Bugs: ProtocolBugs{
2435 NoRenegotiationInfo: true,
2436 },
2437 },
2438 flags: []string{"-allow-unsafe-legacy-renegotiation"},
2439 })
Adam Langley2ae77d22014-10-28 17:29:33 -07002440 // TODO(agl): test the renegotiation info SCSV.
Adam Langleycf2d4f42014-10-28 19:06:14 -07002441 testCases = append(testCases, testCase{
2442 name: "Renegotiate-Client",
2443 renegotiate: true,
2444 })
2445 testCases = append(testCases, testCase{
2446 name: "Renegotiate-Client-EmptyExt",
2447 renegotiate: true,
2448 config: Config{
2449 Bugs: ProtocolBugs{
2450 EmptyRenegotiationInfo: true,
2451 },
2452 },
2453 shouldFail: true,
2454 expectedError: ":RENEGOTIATION_MISMATCH:",
2455 })
2456 testCases = append(testCases, testCase{
2457 name: "Renegotiate-Client-BadExt",
2458 renegotiate: true,
2459 config: Config{
2460 Bugs: ProtocolBugs{
2461 BadRenegotiationInfo: true,
2462 },
2463 },
2464 shouldFail: true,
2465 expectedError: ":RENEGOTIATION_MISMATCH:",
2466 })
2467 testCases = append(testCases, testCase{
2468 name: "Renegotiate-Client-SwitchCiphers",
2469 renegotiate: true,
2470 config: Config{
2471 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2472 },
2473 renegotiateCiphers: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2474 })
2475 testCases = append(testCases, testCase{
2476 name: "Renegotiate-Client-SwitchCiphers2",
2477 renegotiate: true,
2478 config: Config{
2479 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2480 },
2481 renegotiateCiphers: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2482 })
David Benjaminc44b1df2014-11-23 12:11:01 -05002483 testCases = append(testCases, testCase{
2484 name: "Renegotiate-SameClientVersion",
2485 renegotiate: true,
2486 config: Config{
2487 MaxVersion: VersionTLS10,
2488 Bugs: ProtocolBugs{
2489 RequireSameRenegoClientVersion: true,
2490 },
2491 },
2492 })
Adam Langley2ae77d22014-10-28 17:29:33 -07002493}
2494
David Benjamin5e961c12014-11-07 01:48:35 -05002495func addDTLSReplayTests() {
2496 // Test that sequence number replays are detected.
2497 testCases = append(testCases, testCase{
2498 protocol: dtls,
2499 name: "DTLS-Replay",
2500 replayWrites: true,
2501 })
2502
2503 // Test the outgoing sequence number skipping by values larger
2504 // than the retransmit window.
2505 testCases = append(testCases, testCase{
2506 protocol: dtls,
2507 name: "DTLS-Replay-LargeGaps",
2508 config: Config{
2509 Bugs: ProtocolBugs{
2510 SequenceNumberIncrement: 127,
2511 },
2512 },
2513 replayWrites: true,
2514 })
2515}
2516
Feng Lu41aa3252014-11-21 22:47:56 -08002517func addFastRadioPaddingTests() {
David Benjamin1e29a6b2014-12-10 02:27:24 -05002518 testCases = append(testCases, testCase{
2519 protocol: tls,
2520 name: "FastRadio-Padding",
Feng Lu41aa3252014-11-21 22:47:56 -08002521 config: Config{
2522 Bugs: ProtocolBugs{
2523 RequireFastradioPadding: true,
2524 },
2525 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05002526 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08002527 })
David Benjamin1e29a6b2014-12-10 02:27:24 -05002528 testCases = append(testCases, testCase{
2529 protocol: dtls,
2530 name: "FastRadio-Padding",
Feng Lu41aa3252014-11-21 22:47:56 -08002531 config: Config{
2532 Bugs: ProtocolBugs{
2533 RequireFastradioPadding: true,
2534 },
2535 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05002536 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08002537 })
2538}
2539
David Benjamin000800a2014-11-14 01:43:59 -05002540var testHashes = []struct {
2541 name string
2542 id uint8
2543}{
2544 {"SHA1", hashSHA1},
2545 {"SHA224", hashSHA224},
2546 {"SHA256", hashSHA256},
2547 {"SHA384", hashSHA384},
2548 {"SHA512", hashSHA512},
2549}
2550
2551func addSigningHashTests() {
2552 // Make sure each hash works. Include some fake hashes in the list and
2553 // ensure they're ignored.
2554 for _, hash := range testHashes {
2555 testCases = append(testCases, testCase{
2556 name: "SigningHash-ClientAuth-" + hash.name,
2557 config: Config{
2558 ClientAuth: RequireAnyClientCert,
2559 SignatureAndHashes: []signatureAndHash{
2560 {signatureRSA, 42},
2561 {signatureRSA, hash.id},
2562 {signatureRSA, 255},
2563 },
2564 },
2565 flags: []string{
2566 "-cert-file", rsaCertificateFile,
2567 "-key-file", rsaKeyFile,
2568 },
2569 })
2570
2571 testCases = append(testCases, testCase{
2572 testType: serverTest,
2573 name: "SigningHash-ServerKeyExchange-Sign-" + hash.name,
2574 config: Config{
2575 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2576 SignatureAndHashes: []signatureAndHash{
2577 {signatureRSA, 42},
2578 {signatureRSA, hash.id},
2579 {signatureRSA, 255},
2580 },
2581 },
2582 })
2583 }
2584
2585 // Test that hash resolution takes the signature type into account.
2586 testCases = append(testCases, testCase{
2587 name: "SigningHash-ClientAuth-SignatureType",
2588 config: Config{
2589 ClientAuth: RequireAnyClientCert,
2590 SignatureAndHashes: []signatureAndHash{
2591 {signatureECDSA, hashSHA512},
2592 {signatureRSA, hashSHA384},
2593 {signatureECDSA, hashSHA1},
2594 },
2595 },
2596 flags: []string{
2597 "-cert-file", rsaCertificateFile,
2598 "-key-file", rsaKeyFile,
2599 },
2600 })
2601
2602 testCases = append(testCases, testCase{
2603 testType: serverTest,
2604 name: "SigningHash-ServerKeyExchange-SignatureType",
2605 config: Config{
2606 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2607 SignatureAndHashes: []signatureAndHash{
2608 {signatureECDSA, hashSHA512},
2609 {signatureRSA, hashSHA384},
2610 {signatureECDSA, hashSHA1},
2611 },
2612 },
2613 })
2614
2615 // Test that, if the list is missing, the peer falls back to SHA-1.
2616 testCases = append(testCases, testCase{
2617 name: "SigningHash-ClientAuth-Fallback",
2618 config: Config{
2619 ClientAuth: RequireAnyClientCert,
2620 SignatureAndHashes: []signatureAndHash{
2621 {signatureRSA, hashSHA1},
2622 },
2623 Bugs: ProtocolBugs{
2624 NoSignatureAndHashes: true,
2625 },
2626 },
2627 flags: []string{
2628 "-cert-file", rsaCertificateFile,
2629 "-key-file", rsaKeyFile,
2630 },
2631 })
2632
2633 testCases = append(testCases, testCase{
2634 testType: serverTest,
2635 name: "SigningHash-ServerKeyExchange-Fallback",
2636 config: Config{
2637 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2638 SignatureAndHashes: []signatureAndHash{
2639 {signatureRSA, hashSHA1},
2640 },
2641 Bugs: ProtocolBugs{
2642 NoSignatureAndHashes: true,
2643 },
2644 },
2645 })
2646}
2647
David Benjamin83f90402015-01-27 01:09:43 -05002648// timeouts is the retransmit schedule for BoringSSL. It doubles and
2649// caps at 60 seconds. On the 13th timeout, it gives up.
2650var timeouts = []time.Duration{
2651 1 * time.Second,
2652 2 * time.Second,
2653 4 * time.Second,
2654 8 * time.Second,
2655 16 * time.Second,
2656 32 * time.Second,
2657 60 * time.Second,
2658 60 * time.Second,
2659 60 * time.Second,
2660 60 * time.Second,
2661 60 * time.Second,
2662 60 * time.Second,
2663 60 * time.Second,
2664}
2665
2666func addDTLSRetransmitTests() {
2667 // Test that this is indeed the timeout schedule. Stress all
2668 // four patterns of handshake.
2669 for i := 1; i < len(timeouts); i++ {
2670 number := strconv.Itoa(i)
2671 testCases = append(testCases, testCase{
2672 protocol: dtls,
2673 name: "DTLS-Retransmit-Client-" + number,
2674 config: Config{
2675 Bugs: ProtocolBugs{
2676 TimeoutSchedule: timeouts[:i],
2677 },
2678 },
2679 resumeSession: true,
2680 flags: []string{"-async"},
2681 })
2682 testCases = append(testCases, testCase{
2683 protocol: dtls,
2684 testType: serverTest,
2685 name: "DTLS-Retransmit-Server-" + number,
2686 config: Config{
2687 Bugs: ProtocolBugs{
2688 TimeoutSchedule: timeouts[:i],
2689 },
2690 },
2691 resumeSession: true,
2692 flags: []string{"-async"},
2693 })
2694 }
2695
2696 // Test that exceeding the timeout schedule hits a read
2697 // timeout.
2698 testCases = append(testCases, testCase{
2699 protocol: dtls,
2700 name: "DTLS-Retransmit-Timeout",
2701 config: Config{
2702 Bugs: ProtocolBugs{
2703 TimeoutSchedule: timeouts,
2704 },
2705 },
2706 resumeSession: true,
2707 flags: []string{"-async"},
2708 shouldFail: true,
2709 expectedError: ":READ_TIMEOUT_EXPIRED:",
2710 })
2711
2712 // Test that timeout handling has a fudge factor, due to API
2713 // problems.
2714 testCases = append(testCases, testCase{
2715 protocol: dtls,
2716 name: "DTLS-Retransmit-Fudge",
2717 config: Config{
2718 Bugs: ProtocolBugs{
2719 TimeoutSchedule: []time.Duration{
2720 timeouts[0] - 10*time.Millisecond,
2721 },
2722 },
2723 },
2724 resumeSession: true,
2725 flags: []string{"-async"},
2726 })
2727}
2728
David Benjamin884fdf12014-08-02 15:28:23 -04002729func worker(statusChan chan statusMsg, c chan *testCase, buildDir string, wg *sync.WaitGroup) {
Adam Langley95c29f32014-06-20 12:00:00 -07002730 defer wg.Done()
2731
2732 for test := range c {
Adam Langley69a01602014-11-17 17:26:55 -08002733 var err error
2734
2735 if *mallocTest < 0 {
2736 statusChan <- statusMsg{test: test, started: true}
2737 err = runTest(test, buildDir, -1)
2738 } else {
2739 for mallocNumToFail := int64(*mallocTest); ; mallocNumToFail++ {
2740 statusChan <- statusMsg{test: test, started: true}
2741 if err = runTest(test, buildDir, mallocNumToFail); err != errMoreMallocs {
2742 if err != nil {
2743 fmt.Printf("\n\nmalloc test failed at %d: %s\n", mallocNumToFail, err)
2744 }
2745 break
2746 }
2747 }
2748 }
Adam Langley95c29f32014-06-20 12:00:00 -07002749 statusChan <- statusMsg{test: test, err: err}
2750 }
2751}
2752
2753type statusMsg struct {
2754 test *testCase
2755 started bool
2756 err error
2757}
2758
2759func statusPrinter(doneChan chan struct{}, statusChan chan statusMsg, total int) {
2760 var started, done, failed, lineLen int
2761 defer close(doneChan)
2762
2763 for msg := range statusChan {
2764 if msg.started {
2765 started++
2766 } else {
2767 done++
2768 }
2769
2770 fmt.Printf("\x1b[%dD\x1b[K", lineLen)
2771
2772 if msg.err != nil {
2773 fmt.Printf("FAILED (%s)\n%s\n", msg.test.name, msg.err)
2774 failed++
2775 }
2776 line := fmt.Sprintf("%d/%d/%d/%d", failed, done, started, total)
2777 lineLen = len(line)
2778 os.Stdout.WriteString(line)
2779 }
2780}
2781
2782func main() {
2783 var flagTest *string = flag.String("test", "", "The name of a test to run, or empty to run all tests")
David Benjamin2bc8e6f2014-08-02 15:22:37 -04002784 var flagNumWorkers *int = flag.Int("num-workers", runtime.NumCPU(), "The number of workers to run in parallel.")
David Benjamin884fdf12014-08-02 15:28:23 -04002785 var flagBuildDir *string = flag.String("build-dir", "../../../build", "The build directory to run the shim from.")
Adam Langley95c29f32014-06-20 12:00:00 -07002786
2787 flag.Parse()
2788
2789 addCipherSuiteTests()
2790 addBadECDSASignatureTests()
Adam Langley80842bd2014-06-20 12:00:00 -07002791 addCBCPaddingTests()
Kenny Root7fdeaf12014-08-05 15:23:37 -07002792 addCBCSplittingTests()
David Benjamin636293b2014-07-08 17:59:18 -04002793 addClientAuthTests()
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002794 addVersionNegotiationTests()
David Benjaminaccb4542014-12-12 23:44:33 -05002795 addMinimumVersionTests()
David Benjamin5c24a1d2014-08-31 00:59:27 -04002796 addD5BugTests()
David Benjamine78bfde2014-09-06 12:45:15 -04002797 addExtensionTests()
David Benjamin01fe8202014-09-24 15:21:44 -04002798 addResumptionVersionTests()
Adam Langley75712922014-10-10 16:23:43 -07002799 addExtendedMasterSecretTests()
Adam Langley2ae77d22014-10-28 17:29:33 -07002800 addRenegotiationTests()
David Benjamin5e961c12014-11-07 01:48:35 -05002801 addDTLSReplayTests()
David Benjamin000800a2014-11-14 01:43:59 -05002802 addSigningHashTests()
Feng Lu41aa3252014-11-21 22:47:56 -08002803 addFastRadioPaddingTests()
David Benjamin83f90402015-01-27 01:09:43 -05002804 addDTLSRetransmitTests()
David Benjamin43ec06f2014-08-05 02:28:57 -04002805 for _, async := range []bool{false, true} {
2806 for _, splitHandshake := range []bool{false, true} {
David Benjamin6fd297b2014-08-11 18:43:38 -04002807 for _, protocol := range []protocol{tls, dtls} {
2808 addStateMachineCoverageTests(async, splitHandshake, protocol)
2809 }
David Benjamin43ec06f2014-08-05 02:28:57 -04002810 }
2811 }
Adam Langley95c29f32014-06-20 12:00:00 -07002812
2813 var wg sync.WaitGroup
2814
David Benjamin2bc8e6f2014-08-02 15:22:37 -04002815 numWorkers := *flagNumWorkers
Adam Langley95c29f32014-06-20 12:00:00 -07002816
2817 statusChan := make(chan statusMsg, numWorkers)
2818 testChan := make(chan *testCase, numWorkers)
2819 doneChan := make(chan struct{})
2820
David Benjamin025b3d32014-07-01 19:53:04 -04002821 go statusPrinter(doneChan, statusChan, len(testCases))
Adam Langley95c29f32014-06-20 12:00:00 -07002822
2823 for i := 0; i < numWorkers; i++ {
2824 wg.Add(1)
David Benjamin884fdf12014-08-02 15:28:23 -04002825 go worker(statusChan, testChan, *flagBuildDir, &wg)
Adam Langley95c29f32014-06-20 12:00:00 -07002826 }
2827
David Benjamin025b3d32014-07-01 19:53:04 -04002828 for i := range testCases {
2829 if len(*flagTest) == 0 || *flagTest == testCases[i].name {
2830 testChan <- &testCases[i]
Adam Langley95c29f32014-06-20 12:00:00 -07002831 }
2832 }
2833
2834 close(testChan)
2835 wg.Wait()
2836 close(statusChan)
2837 <-doneChan
2838
2839 fmt.Printf("\n")
2840}