blob: 70ed3146afae186bf9a80033172fe866765f95dc [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 },
Adam Langley95c29f32014-06-20 12:00:00 -0700742}
743
David Benjamin01fe8202014-09-24 15:21:44 -0400744func doExchange(test *testCase, config *Config, conn net.Conn, messageLen int, isResume bool) error {
David Benjamin65ea8ff2014-11-23 03:01:00 -0500745 var connDebug *recordingConn
David Benjamin5fa3eba2015-01-22 16:35:40 -0500746 var connDamage *damageAdaptor
David Benjamin65ea8ff2014-11-23 03:01:00 -0500747 if *flagDebug {
748 connDebug = &recordingConn{Conn: conn}
749 conn = connDebug
750 defer func() {
751 connDebug.WriteTo(os.Stdout)
752 }()
753 }
754
David Benjamin6fd297b2014-08-11 18:43:38 -0400755 if test.protocol == dtls {
David Benjamin83f90402015-01-27 01:09:43 -0500756 config.Bugs.PacketAdaptor = newPacketAdaptor(conn)
757 conn = config.Bugs.PacketAdaptor
David Benjamin5e961c12014-11-07 01:48:35 -0500758 if test.replayWrites {
759 conn = newReplayAdaptor(conn)
760 }
David Benjamin6fd297b2014-08-11 18:43:38 -0400761 }
762
David Benjamin5fa3eba2015-01-22 16:35:40 -0500763 if test.damageFirstWrite {
764 connDamage = newDamageAdaptor(conn)
765 conn = connDamage
766 }
767
David Benjamin6fd297b2014-08-11 18:43:38 -0400768 if test.sendPrefix != "" {
769 if _, err := conn.Write([]byte(test.sendPrefix)); err != nil {
770 return err
771 }
David Benjamin98e882e2014-08-08 13:24:34 -0400772 }
773
David Benjamin1d5c83e2014-07-22 19:20:02 -0400774 var tlsConn *Conn
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400775 if test.testType == clientTest {
David Benjamin6fd297b2014-08-11 18:43:38 -0400776 if test.protocol == dtls {
777 tlsConn = DTLSServer(conn, config)
778 } else {
779 tlsConn = Server(conn, config)
780 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400781 } else {
782 config.InsecureSkipVerify = true
David Benjamin6fd297b2014-08-11 18:43:38 -0400783 if test.protocol == dtls {
784 tlsConn = DTLSClient(conn, config)
785 } else {
786 tlsConn = Client(conn, config)
787 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400788 }
789
Adam Langley95c29f32014-06-20 12:00:00 -0700790 if err := tlsConn.Handshake(); err != nil {
791 return err
792 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700793
David Benjamin01fe8202014-09-24 15:21:44 -0400794 // TODO(davidben): move all per-connection expectations into a dedicated
795 // expectations struct that can be specified separately for the two
796 // legs.
797 expectedVersion := test.expectedVersion
798 if isResume && test.expectedResumeVersion != 0 {
799 expectedVersion = test.expectedResumeVersion
800 }
801 if vers := tlsConn.ConnectionState().Version; expectedVersion != 0 && vers != expectedVersion {
802 return fmt.Errorf("got version %x, expected %x", vers, expectedVersion)
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400803 }
804
David Benjamina08e49d2014-08-24 01:46:07 -0400805 if test.expectChannelID {
806 channelID := tlsConn.ConnectionState().ChannelID
807 if channelID == nil {
808 return fmt.Errorf("no channel ID negotiated")
809 }
810 if channelID.Curve != channelIDKey.Curve ||
811 channelIDKey.X.Cmp(channelIDKey.X) != 0 ||
812 channelIDKey.Y.Cmp(channelIDKey.Y) != 0 {
813 return fmt.Errorf("incorrect channel ID")
814 }
815 }
816
David Benjaminae2888f2014-09-06 12:58:58 -0400817 if expected := test.expectedNextProto; expected != "" {
818 if actual := tlsConn.ConnectionState().NegotiatedProtocol; actual != expected {
819 return fmt.Errorf("next proto mismatch: got %s, wanted %s", actual, expected)
820 }
821 }
822
David Benjaminfc7b0862014-09-06 13:21:53 -0400823 if test.expectedNextProtoType != 0 {
824 if (test.expectedNextProtoType == alpn) != tlsConn.ConnectionState().NegotiatedProtocolFromALPN {
825 return fmt.Errorf("next proto type mismatch")
826 }
827 }
828
David Benjaminca6c8262014-11-15 19:06:08 -0500829 if p := tlsConn.ConnectionState().SRTPProtectionProfile; p != test.expectedSRTPProtectionProfile {
830 return fmt.Errorf("SRTP profile mismatch: got %d, wanted %d", p, test.expectedSRTPProtectionProfile)
831 }
832
David Benjamine58c4f52014-08-24 03:47:07 -0400833 if test.shimWritesFirst {
834 var buf [5]byte
835 _, err := io.ReadFull(tlsConn, buf[:])
836 if err != nil {
837 return err
838 }
839 if string(buf[:]) != "hello" {
840 return fmt.Errorf("bad initial message")
841 }
842 }
843
Adam Langleycf2d4f42014-10-28 19:06:14 -0700844 if test.renegotiate {
845 if test.renegotiateCiphers != nil {
846 config.CipherSuites = test.renegotiateCiphers
847 }
848 if err := tlsConn.Renegotiate(); err != nil {
849 return err
850 }
851 } else if test.renegotiateCiphers != nil {
852 panic("renegotiateCiphers without renegotiate")
853 }
854
David Benjamin5fa3eba2015-01-22 16:35:40 -0500855 if test.damageFirstWrite {
856 connDamage.setDamage(true)
857 tlsConn.Write([]byte("DAMAGED WRITE"))
858 connDamage.setDamage(false)
859 }
860
Kenny Root7fdeaf12014-08-05 15:23:37 -0700861 if messageLen < 0 {
David Benjamin6fd297b2014-08-11 18:43:38 -0400862 if test.protocol == dtls {
863 return fmt.Errorf("messageLen < 0 not supported for DTLS tests")
864 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700865 // Read until EOF.
866 _, err := io.Copy(ioutil.Discard, tlsConn)
867 return err
868 }
869
David Benjamin4189bd92015-01-25 23:52:39 -0500870 var testMessage []byte
871 if config.Bugs.AppDataAfterChangeCipherSpec != nil {
872 // We've already sent a message. Expect the shim to echo it
873 // back.
874 testMessage = config.Bugs.AppDataAfterChangeCipherSpec
875 } else {
876 if messageLen == 0 {
877 messageLen = 32
878 }
879 testMessage = make([]byte, messageLen)
880 for i := range testMessage {
881 testMessage[i] = 0x42
882 }
883 tlsConn.Write(testMessage)
Adam Langley80842bd2014-06-20 12:00:00 -0700884 }
Adam Langley95c29f32014-06-20 12:00:00 -0700885
886 buf := make([]byte, len(testMessage))
David Benjamin6fd297b2014-08-11 18:43:38 -0400887 if test.protocol == dtls {
888 bufTmp := make([]byte, len(buf)+1)
889 n, err := tlsConn.Read(bufTmp)
890 if err != nil {
891 return err
892 }
893 if n != len(buf) {
894 return fmt.Errorf("bad reply; length mismatch (%d vs %d)", n, len(buf))
895 }
896 copy(buf, bufTmp)
897 } else {
898 _, err := io.ReadFull(tlsConn, buf)
899 if err != nil {
900 return err
901 }
Adam Langley95c29f32014-06-20 12:00:00 -0700902 }
903
904 for i, v := range buf {
905 if v != testMessage[i]^0xff {
906 return fmt.Errorf("bad reply contents at byte %d", i)
907 }
908 }
909
910 return nil
911}
912
David Benjamin325b5c32014-07-01 19:40:31 -0400913func valgrindOf(dbAttach bool, path string, args ...string) *exec.Cmd {
914 valgrindArgs := []string{"--error-exitcode=99", "--track-origins=yes", "--leak-check=full"}
Adam Langley95c29f32014-06-20 12:00:00 -0700915 if dbAttach {
David Benjamin325b5c32014-07-01 19:40:31 -0400916 valgrindArgs = append(valgrindArgs, "--db-attach=yes", "--db-command=xterm -e gdb -nw %f %p")
Adam Langley95c29f32014-06-20 12:00:00 -0700917 }
David Benjamin325b5c32014-07-01 19:40:31 -0400918 valgrindArgs = append(valgrindArgs, path)
919 valgrindArgs = append(valgrindArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -0700920
David Benjamin325b5c32014-07-01 19:40:31 -0400921 return exec.Command("valgrind", valgrindArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -0700922}
923
David Benjamin325b5c32014-07-01 19:40:31 -0400924func gdbOf(path string, args ...string) *exec.Cmd {
925 xtermArgs := []string{"-e", "gdb", "--args"}
926 xtermArgs = append(xtermArgs, path)
927 xtermArgs = append(xtermArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -0700928
David Benjamin325b5c32014-07-01 19:40:31 -0400929 return exec.Command("xterm", xtermArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -0700930}
931
David Benjamin1d5c83e2014-07-22 19:20:02 -0400932func openSocketPair() (shimEnd *os.File, conn net.Conn) {
Adam Langley95c29f32014-06-20 12:00:00 -0700933 socks, err := syscall.Socketpair(syscall.AF_UNIX, syscall.SOCK_STREAM, 0)
934 if err != nil {
935 panic(err)
936 }
937
938 syscall.CloseOnExec(socks[0])
939 syscall.CloseOnExec(socks[1])
David Benjamin1d5c83e2014-07-22 19:20:02 -0400940 shimEnd = os.NewFile(uintptr(socks[0]), "shim end")
Adam Langley95c29f32014-06-20 12:00:00 -0700941 connFile := os.NewFile(uintptr(socks[1]), "our end")
David Benjamin1d5c83e2014-07-22 19:20:02 -0400942 conn, err = net.FileConn(connFile)
943 if err != nil {
944 panic(err)
945 }
Adam Langley95c29f32014-06-20 12:00:00 -0700946 connFile.Close()
947 if err != nil {
948 panic(err)
949 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400950 return shimEnd, conn
951}
952
Adam Langley69a01602014-11-17 17:26:55 -0800953type moreMallocsError struct{}
954
955func (moreMallocsError) Error() string {
956 return "child process did not exhaust all allocation calls"
957}
958
959var errMoreMallocs = moreMallocsError{}
960
961func runTest(test *testCase, buildDir string, mallocNumToFail int64) error {
Adam Langley38311732014-10-16 19:04:35 -0700962 if !test.shouldFail && (len(test.expectedError) > 0 || len(test.expectedLocalError) > 0) {
963 panic("Error expected without shouldFail in " + test.name)
964 }
965
David Benjamin1d5c83e2014-07-22 19:20:02 -0400966 shimEnd, conn := openSocketPair()
967 shimEndResume, connResume := openSocketPair()
Adam Langley95c29f32014-06-20 12:00:00 -0700968
David Benjamin884fdf12014-08-02 15:28:23 -0400969 shim_path := path.Join(buildDir, "ssl/test/bssl_shim")
David Benjamin5a593af2014-08-11 19:51:50 -0400970 var flags []string
David Benjamin1d5c83e2014-07-22 19:20:02 -0400971 if test.testType == serverTest {
David Benjamin5a593af2014-08-11 19:51:50 -0400972 flags = append(flags, "-server")
973
David Benjamin025b3d32014-07-01 19:53:04 -0400974 flags = append(flags, "-key-file")
975 if test.keyFile == "" {
976 flags = append(flags, rsaKeyFile)
977 } else {
978 flags = append(flags, test.keyFile)
979 }
980
981 flags = append(flags, "-cert-file")
982 if test.certFile == "" {
983 flags = append(flags, rsaCertificateFile)
984 } else {
985 flags = append(flags, test.certFile)
986 }
987 }
David Benjamin5a593af2014-08-11 19:51:50 -0400988
David Benjamin6fd297b2014-08-11 18:43:38 -0400989 if test.protocol == dtls {
990 flags = append(flags, "-dtls")
991 }
992
David Benjamin5a593af2014-08-11 19:51:50 -0400993 if test.resumeSession {
994 flags = append(flags, "-resume")
995 }
996
David Benjamine58c4f52014-08-24 03:47:07 -0400997 if test.shimWritesFirst {
998 flags = append(flags, "-shim-writes-first")
999 }
1000
David Benjamin025b3d32014-07-01 19:53:04 -04001001 flags = append(flags, test.flags...)
1002
1003 var shim *exec.Cmd
1004 if *useValgrind {
1005 shim = valgrindOf(false, shim_path, flags...)
Adam Langley75712922014-10-10 16:23:43 -07001006 } else if *useGDB {
1007 shim = gdbOf(shim_path, flags...)
David Benjamin025b3d32014-07-01 19:53:04 -04001008 } else {
1009 shim = exec.Command(shim_path, flags...)
1010 }
David Benjamin1d5c83e2014-07-22 19:20:02 -04001011 shim.ExtraFiles = []*os.File{shimEnd, shimEndResume}
David Benjamin025b3d32014-07-01 19:53:04 -04001012 shim.Stdin = os.Stdin
1013 var stdoutBuf, stderrBuf bytes.Buffer
1014 shim.Stdout = &stdoutBuf
1015 shim.Stderr = &stderrBuf
Adam Langley69a01602014-11-17 17:26:55 -08001016 if mallocNumToFail >= 0 {
1017 shim.Env = []string{"MALLOC_NUMBER_TO_FAIL=" + strconv.FormatInt(mallocNumToFail, 10)}
1018 if *mallocTestDebug {
1019 shim.Env = append(shim.Env, "MALLOC_ABORT_ON_FAIL=1")
1020 }
1021 shim.Env = append(shim.Env, "_MALLOC_CHECK=1")
1022 }
David Benjamin025b3d32014-07-01 19:53:04 -04001023
1024 if err := shim.Start(); err != nil {
Adam Langley95c29f32014-06-20 12:00:00 -07001025 panic(err)
1026 }
David Benjamin025b3d32014-07-01 19:53:04 -04001027 shimEnd.Close()
David Benjamin1d5c83e2014-07-22 19:20:02 -04001028 shimEndResume.Close()
Adam Langley95c29f32014-06-20 12:00:00 -07001029
1030 config := test.config
David Benjamin1d5c83e2014-07-22 19:20:02 -04001031 config.ClientSessionCache = NewLRUClientSessionCache(1)
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001032 config.ServerSessionCache = NewLRUServerSessionCache(1)
David Benjamin025b3d32014-07-01 19:53:04 -04001033 if test.testType == clientTest {
1034 if len(config.Certificates) == 0 {
1035 config.Certificates = []Certificate{getRSACertificate()}
1036 }
David Benjamin025b3d32014-07-01 19:53:04 -04001037 }
Adam Langley95c29f32014-06-20 12:00:00 -07001038
David Benjamin01fe8202014-09-24 15:21:44 -04001039 err := doExchange(test, &config, conn, test.messageLen,
1040 false /* not a resumption */)
Adam Langley95c29f32014-06-20 12:00:00 -07001041 conn.Close()
David Benjamin65ea8ff2014-11-23 03:01:00 -05001042
David Benjamin1d5c83e2014-07-22 19:20:02 -04001043 if err == nil && test.resumeSession {
David Benjamin01fe8202014-09-24 15:21:44 -04001044 var resumeConfig Config
1045 if test.resumeConfig != nil {
1046 resumeConfig = *test.resumeConfig
1047 if len(resumeConfig.Certificates) == 0 {
1048 resumeConfig.Certificates = []Certificate{getRSACertificate()}
1049 }
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001050 if !test.newSessionsOnResume {
1051 resumeConfig.SessionTicketKey = config.SessionTicketKey
1052 resumeConfig.ClientSessionCache = config.ClientSessionCache
1053 resumeConfig.ServerSessionCache = config.ServerSessionCache
1054 }
David Benjamin01fe8202014-09-24 15:21:44 -04001055 } else {
1056 resumeConfig = config
1057 }
1058 err = doExchange(test, &resumeConfig, connResume, test.messageLen,
1059 true /* resumption */)
David Benjamin1d5c83e2014-07-22 19:20:02 -04001060 }
David Benjamin812152a2014-09-06 12:49:07 -04001061 connResume.Close()
David Benjamin1d5c83e2014-07-22 19:20:02 -04001062
David Benjamin025b3d32014-07-01 19:53:04 -04001063 childErr := shim.Wait()
Adam Langley69a01602014-11-17 17:26:55 -08001064 if exitError, ok := childErr.(*exec.ExitError); ok {
1065 if exitError.Sys().(syscall.WaitStatus).ExitStatus() == 88 {
1066 return errMoreMallocs
1067 }
1068 }
Adam Langley95c29f32014-06-20 12:00:00 -07001069
1070 stdout := string(stdoutBuf.Bytes())
1071 stderr := string(stderrBuf.Bytes())
1072 failed := err != nil || childErr != nil
1073 correctFailure := len(test.expectedError) == 0 || strings.Contains(stdout, test.expectedError)
Adam Langleyac61fa32014-06-23 12:03:11 -07001074 localError := "none"
1075 if err != nil {
1076 localError = err.Error()
1077 }
1078 if len(test.expectedLocalError) != 0 {
1079 correctFailure = correctFailure && strings.Contains(localError, test.expectedLocalError)
1080 }
Adam Langley95c29f32014-06-20 12:00:00 -07001081
1082 if failed != test.shouldFail || failed && !correctFailure {
Adam Langley95c29f32014-06-20 12:00:00 -07001083 childError := "none"
Adam Langley95c29f32014-06-20 12:00:00 -07001084 if childErr != nil {
1085 childError = childErr.Error()
1086 }
1087
1088 var msg string
1089 switch {
1090 case failed && !test.shouldFail:
1091 msg = "unexpected failure"
1092 case !failed && test.shouldFail:
1093 msg = "unexpected success"
1094 case failed && !correctFailure:
Adam Langleyac61fa32014-06-23 12:03:11 -07001095 msg = "bad error (wanted '" + test.expectedError + "' / '" + test.expectedLocalError + "')"
Adam Langley95c29f32014-06-20 12:00:00 -07001096 default:
1097 panic("internal error")
1098 }
1099
1100 return fmt.Errorf("%s: local error '%s', child error '%s', stdout:\n%s\nstderr:\n%s", msg, localError, childError, string(stdoutBuf.Bytes()), stderr)
1101 }
1102
1103 if !*useValgrind && len(stderr) > 0 {
1104 println(stderr)
1105 }
1106
1107 return nil
1108}
1109
1110var tlsVersions = []struct {
1111 name string
1112 version uint16
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001113 flag string
David Benjamin8b8c0062014-11-23 02:47:52 -05001114 hasDTLS bool
Adam Langley95c29f32014-06-20 12:00:00 -07001115}{
David Benjamin8b8c0062014-11-23 02:47:52 -05001116 {"SSL3", VersionSSL30, "-no-ssl3", false},
1117 {"TLS1", VersionTLS10, "-no-tls1", true},
1118 {"TLS11", VersionTLS11, "-no-tls11", false},
1119 {"TLS12", VersionTLS12, "-no-tls12", true},
Adam Langley95c29f32014-06-20 12:00:00 -07001120}
1121
1122var testCipherSuites = []struct {
1123 name string
1124 id uint16
1125}{
1126 {"3DES-SHA", TLS_RSA_WITH_3DES_EDE_CBC_SHA},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001127 {"AES128-GCM", TLS_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001128 {"AES128-SHA", TLS_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001129 {"AES128-SHA256", TLS_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001130 {"AES256-GCM", TLS_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001131 {"AES256-SHA", TLS_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001132 {"AES256-SHA256", TLS_RSA_WITH_AES_256_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001133 {"DHE-RSA-AES128-GCM", TLS_DHE_RSA_WITH_AES_128_GCM_SHA256},
1134 {"DHE-RSA-AES128-SHA", TLS_DHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001135 {"DHE-RSA-AES128-SHA256", TLS_DHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001136 {"DHE-RSA-AES256-GCM", TLS_DHE_RSA_WITH_AES_256_GCM_SHA384},
1137 {"DHE-RSA-AES256-SHA", TLS_DHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001138 {"DHE-RSA-AES256-SHA256", TLS_DHE_RSA_WITH_AES_256_CBC_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001139 {"ECDHE-ECDSA-AES128-GCM", TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
1140 {"ECDHE-ECDSA-AES128-SHA", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001141 {"ECDHE-ECDSA-AES128-SHA256", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256},
1142 {"ECDHE-ECDSA-AES256-GCM", TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001143 {"ECDHE-ECDSA-AES256-SHA", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001144 {"ECDHE-ECDSA-AES256-SHA384", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001145 {"ECDHE-ECDSA-RC4-SHA", TLS_ECDHE_ECDSA_WITH_RC4_128_SHA},
David Benjamin2af684f2014-10-27 02:23:15 -04001146 {"ECDHE-PSK-WITH-AES-128-GCM-SHA256", TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001147 {"ECDHE-RSA-AES128-GCM", TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001148 {"ECDHE-RSA-AES128-SHA", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001149 {"ECDHE-RSA-AES128-SHA256", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001150 {"ECDHE-RSA-AES256-GCM", TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001151 {"ECDHE-RSA-AES256-SHA", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001152 {"ECDHE-RSA-AES256-SHA384", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001153 {"ECDHE-RSA-RC4-SHA", TLS_ECDHE_RSA_WITH_RC4_128_SHA},
David Benjamin48cae082014-10-27 01:06:24 -04001154 {"PSK-AES128-CBC-SHA", TLS_PSK_WITH_AES_128_CBC_SHA},
1155 {"PSK-AES256-CBC-SHA", TLS_PSK_WITH_AES_256_CBC_SHA},
1156 {"PSK-RC4-SHA", TLS_PSK_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -07001157 {"RC4-MD5", TLS_RSA_WITH_RC4_128_MD5},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001158 {"RC4-SHA", TLS_RSA_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -07001159}
1160
David Benjamin8b8c0062014-11-23 02:47:52 -05001161func hasComponent(suiteName, component string) bool {
1162 return strings.Contains("-"+suiteName+"-", "-"+component+"-")
1163}
1164
David Benjaminf7768e42014-08-31 02:06:47 -04001165func isTLS12Only(suiteName string) bool {
David Benjamin8b8c0062014-11-23 02:47:52 -05001166 return hasComponent(suiteName, "GCM") ||
1167 hasComponent(suiteName, "SHA256") ||
1168 hasComponent(suiteName, "SHA384")
1169}
1170
1171func isDTLSCipher(suiteName string) bool {
David Benjamine95d20d2014-12-23 11:16:01 -05001172 return !hasComponent(suiteName, "RC4")
David Benjaminf7768e42014-08-31 02:06:47 -04001173}
1174
Adam Langley95c29f32014-06-20 12:00:00 -07001175func addCipherSuiteTests() {
1176 for _, suite := range testCipherSuites {
David Benjamin48cae082014-10-27 01:06:24 -04001177 const psk = "12345"
1178 const pskIdentity = "luggage combo"
1179
Adam Langley95c29f32014-06-20 12:00:00 -07001180 var cert Certificate
David Benjamin025b3d32014-07-01 19:53:04 -04001181 var certFile string
1182 var keyFile string
David Benjamin8b8c0062014-11-23 02:47:52 -05001183 if hasComponent(suite.name, "ECDSA") {
Adam Langley95c29f32014-06-20 12:00:00 -07001184 cert = getECDSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -04001185 certFile = ecdsaCertificateFile
1186 keyFile = ecdsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -07001187 } else {
1188 cert = getRSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -04001189 certFile = rsaCertificateFile
1190 keyFile = rsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -07001191 }
1192
David Benjamin48cae082014-10-27 01:06:24 -04001193 var flags []string
David Benjamin8b8c0062014-11-23 02:47:52 -05001194 if hasComponent(suite.name, "PSK") {
David Benjamin48cae082014-10-27 01:06:24 -04001195 flags = append(flags,
1196 "-psk", psk,
1197 "-psk-identity", pskIdentity)
1198 }
1199
Adam Langley95c29f32014-06-20 12:00:00 -07001200 for _, ver := range tlsVersions {
David Benjaminf7768e42014-08-31 02:06:47 -04001201 if ver.version < VersionTLS12 && isTLS12Only(suite.name) {
Adam Langley95c29f32014-06-20 12:00:00 -07001202 continue
1203 }
1204
David Benjamin025b3d32014-07-01 19:53:04 -04001205 testCases = append(testCases, testCase{
1206 testType: clientTest,
1207 name: ver.name + "-" + suite.name + "-client",
Adam Langley95c29f32014-06-20 12:00:00 -07001208 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001209 MinVersion: ver.version,
1210 MaxVersion: ver.version,
1211 CipherSuites: []uint16{suite.id},
1212 Certificates: []Certificate{cert},
1213 PreSharedKey: []byte(psk),
1214 PreSharedKeyIdentity: pskIdentity,
Adam Langley95c29f32014-06-20 12:00:00 -07001215 },
David Benjamin48cae082014-10-27 01:06:24 -04001216 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001217 resumeSession: true,
Adam Langley95c29f32014-06-20 12:00:00 -07001218 })
David Benjamin025b3d32014-07-01 19:53:04 -04001219
David Benjamin76d8abe2014-08-14 16:25:34 -04001220 testCases = append(testCases, testCase{
1221 testType: serverTest,
1222 name: ver.name + "-" + suite.name + "-server",
1223 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001224 MinVersion: ver.version,
1225 MaxVersion: ver.version,
1226 CipherSuites: []uint16{suite.id},
1227 Certificates: []Certificate{cert},
1228 PreSharedKey: []byte(psk),
1229 PreSharedKeyIdentity: pskIdentity,
David Benjamin76d8abe2014-08-14 16:25:34 -04001230 },
1231 certFile: certFile,
1232 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001233 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001234 resumeSession: true,
David Benjamin76d8abe2014-08-14 16:25:34 -04001235 })
David Benjamin6fd297b2014-08-11 18:43:38 -04001236
David Benjamin8b8c0062014-11-23 02:47:52 -05001237 if ver.hasDTLS && isDTLSCipher(suite.name) {
David Benjamin6fd297b2014-08-11 18:43:38 -04001238 testCases = append(testCases, testCase{
1239 testType: clientTest,
1240 protocol: dtls,
1241 name: "D" + ver.name + "-" + suite.name + "-client",
1242 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001243 MinVersion: ver.version,
1244 MaxVersion: ver.version,
1245 CipherSuites: []uint16{suite.id},
1246 Certificates: []Certificate{cert},
1247 PreSharedKey: []byte(psk),
1248 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001249 },
David Benjamin48cae082014-10-27 01:06:24 -04001250 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001251 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001252 })
1253 testCases = append(testCases, testCase{
1254 testType: serverTest,
1255 protocol: dtls,
1256 name: "D" + ver.name + "-" + suite.name + "-server",
1257 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001258 MinVersion: ver.version,
1259 MaxVersion: ver.version,
1260 CipherSuites: []uint16{suite.id},
1261 Certificates: []Certificate{cert},
1262 PreSharedKey: []byte(psk),
1263 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001264 },
1265 certFile: certFile,
1266 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001267 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001268 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001269 })
1270 }
Adam Langley95c29f32014-06-20 12:00:00 -07001271 }
1272 }
1273}
1274
1275func addBadECDSASignatureTests() {
1276 for badR := BadValue(1); badR < NumBadValues; badR++ {
1277 for badS := BadValue(1); badS < NumBadValues; badS++ {
David Benjamin025b3d32014-07-01 19:53:04 -04001278 testCases = append(testCases, testCase{
Adam Langley95c29f32014-06-20 12:00:00 -07001279 name: fmt.Sprintf("BadECDSA-%d-%d", badR, badS),
1280 config: Config{
1281 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
1282 Certificates: []Certificate{getECDSACertificate()},
1283 Bugs: ProtocolBugs{
1284 BadECDSAR: badR,
1285 BadECDSAS: badS,
1286 },
1287 },
1288 shouldFail: true,
1289 expectedError: "SIGNATURE",
1290 })
1291 }
1292 }
1293}
1294
Adam Langley80842bd2014-06-20 12:00:00 -07001295func addCBCPaddingTests() {
David Benjamin025b3d32014-07-01 19:53:04 -04001296 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001297 name: "MaxCBCPadding",
1298 config: Config{
1299 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1300 Bugs: ProtocolBugs{
1301 MaxPadding: true,
1302 },
1303 },
1304 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1305 })
David Benjamin025b3d32014-07-01 19:53:04 -04001306 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001307 name: "BadCBCPadding",
1308 config: Config{
1309 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1310 Bugs: ProtocolBugs{
1311 PaddingFirstByteBad: true,
1312 },
1313 },
1314 shouldFail: true,
1315 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1316 })
1317 // OpenSSL previously had an issue where the first byte of padding in
1318 // 255 bytes of padding wasn't checked.
David Benjamin025b3d32014-07-01 19:53:04 -04001319 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001320 name: "BadCBCPadding255",
1321 config: Config{
1322 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1323 Bugs: ProtocolBugs{
1324 MaxPadding: true,
1325 PaddingFirstByteBadIf255: true,
1326 },
1327 },
1328 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1329 shouldFail: true,
1330 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1331 })
1332}
1333
Kenny Root7fdeaf12014-08-05 15:23:37 -07001334func addCBCSplittingTests() {
1335 testCases = append(testCases, testCase{
1336 name: "CBCRecordSplitting",
1337 config: Config{
1338 MaxVersion: VersionTLS10,
1339 MinVersion: VersionTLS10,
1340 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1341 },
1342 messageLen: -1, // read until EOF
1343 flags: []string{
1344 "-async",
1345 "-write-different-record-sizes",
1346 "-cbc-record-splitting",
1347 },
David Benjamina8e3e0e2014-08-06 22:11:10 -04001348 })
1349 testCases = append(testCases, testCase{
Kenny Root7fdeaf12014-08-05 15:23:37 -07001350 name: "CBCRecordSplittingPartialWrite",
1351 config: Config{
1352 MaxVersion: VersionTLS10,
1353 MinVersion: VersionTLS10,
1354 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1355 },
1356 messageLen: -1, // read until EOF
1357 flags: []string{
1358 "-async",
1359 "-write-different-record-sizes",
1360 "-cbc-record-splitting",
1361 "-partial-write",
1362 },
1363 })
1364}
1365
David Benjamin636293b2014-07-08 17:59:18 -04001366func addClientAuthTests() {
David Benjamin407a10c2014-07-16 12:58:59 -04001367 // Add a dummy cert pool to stress certificate authority parsing.
1368 // TODO(davidben): Add tests that those values parse out correctly.
1369 certPool := x509.NewCertPool()
1370 cert, err := x509.ParseCertificate(rsaCertificate.Certificate[0])
1371 if err != nil {
1372 panic(err)
1373 }
1374 certPool.AddCert(cert)
1375
David Benjamin636293b2014-07-08 17:59:18 -04001376 for _, ver := range tlsVersions {
David Benjamin636293b2014-07-08 17:59:18 -04001377 testCases = append(testCases, testCase{
1378 testType: clientTest,
David Benjamin67666e72014-07-12 15:47:52 -04001379 name: ver.name + "-Client-ClientAuth-RSA",
David Benjamin636293b2014-07-08 17:59:18 -04001380 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001381 MinVersion: ver.version,
1382 MaxVersion: ver.version,
1383 ClientAuth: RequireAnyClientCert,
1384 ClientCAs: certPool,
David Benjamin636293b2014-07-08 17:59:18 -04001385 },
1386 flags: []string{
1387 "-cert-file", rsaCertificateFile,
1388 "-key-file", rsaKeyFile,
1389 },
1390 })
1391 testCases = append(testCases, testCase{
David Benjamin67666e72014-07-12 15:47:52 -04001392 testType: serverTest,
1393 name: ver.name + "-Server-ClientAuth-RSA",
1394 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001395 MinVersion: ver.version,
1396 MaxVersion: ver.version,
David Benjamin67666e72014-07-12 15:47:52 -04001397 Certificates: []Certificate{rsaCertificate},
1398 },
1399 flags: []string{"-require-any-client-certificate"},
1400 })
David Benjamine098ec22014-08-27 23:13:20 -04001401 if ver.version != VersionSSL30 {
1402 testCases = append(testCases, testCase{
1403 testType: serverTest,
1404 name: ver.name + "-Server-ClientAuth-ECDSA",
1405 config: Config{
1406 MinVersion: ver.version,
1407 MaxVersion: ver.version,
1408 Certificates: []Certificate{ecdsaCertificate},
1409 },
1410 flags: []string{"-require-any-client-certificate"},
1411 })
1412 testCases = append(testCases, testCase{
1413 testType: clientTest,
1414 name: ver.name + "-Client-ClientAuth-ECDSA",
1415 config: Config{
1416 MinVersion: ver.version,
1417 MaxVersion: ver.version,
1418 ClientAuth: RequireAnyClientCert,
1419 ClientCAs: certPool,
1420 },
1421 flags: []string{
1422 "-cert-file", ecdsaCertificateFile,
1423 "-key-file", ecdsaKeyFile,
1424 },
1425 })
1426 }
David Benjamin636293b2014-07-08 17:59:18 -04001427 }
1428}
1429
Adam Langley75712922014-10-10 16:23:43 -07001430func addExtendedMasterSecretTests() {
1431 const expectEMSFlag = "-expect-extended-master-secret"
1432
1433 for _, with := range []bool{false, true} {
1434 prefix := "No"
1435 var flags []string
1436 if with {
1437 prefix = ""
1438 flags = []string{expectEMSFlag}
1439 }
1440
1441 for _, isClient := range []bool{false, true} {
1442 suffix := "-Server"
1443 testType := serverTest
1444 if isClient {
1445 suffix = "-Client"
1446 testType = clientTest
1447 }
1448
1449 for _, ver := range tlsVersions {
1450 test := testCase{
1451 testType: testType,
1452 name: prefix + "ExtendedMasterSecret-" + ver.name + suffix,
1453 config: Config{
1454 MinVersion: ver.version,
1455 MaxVersion: ver.version,
1456 Bugs: ProtocolBugs{
1457 NoExtendedMasterSecret: !with,
1458 RequireExtendedMasterSecret: with,
1459 },
1460 },
David Benjamin48cae082014-10-27 01:06:24 -04001461 flags: flags,
1462 shouldFail: ver.version == VersionSSL30 && with,
Adam Langley75712922014-10-10 16:23:43 -07001463 }
1464 if test.shouldFail {
1465 test.expectedLocalError = "extended master secret required but not supported by peer"
1466 }
1467 testCases = append(testCases, test)
1468 }
1469 }
1470 }
1471
1472 // When a session is resumed, it should still be aware that its master
1473 // secret was generated via EMS and thus it's safe to use tls-unique.
1474 testCases = append(testCases, testCase{
1475 name: "ExtendedMasterSecret-Resume",
1476 config: Config{
1477 Bugs: ProtocolBugs{
1478 RequireExtendedMasterSecret: true,
1479 },
1480 },
1481 flags: []string{expectEMSFlag},
1482 resumeSession: true,
1483 })
1484}
1485
David Benjamin43ec06f2014-08-05 02:28:57 -04001486// Adds tests that try to cover the range of the handshake state machine, under
1487// various conditions. Some of these are redundant with other tests, but they
1488// only cover the synchronous case.
David Benjamin6fd297b2014-08-11 18:43:38 -04001489func addStateMachineCoverageTests(async, splitHandshake bool, protocol protocol) {
David Benjamin43ec06f2014-08-05 02:28:57 -04001490 var suffix string
1491 var flags []string
1492 var maxHandshakeRecordLength int
David Benjamin6fd297b2014-08-11 18:43:38 -04001493 if protocol == dtls {
1494 suffix = "-DTLS"
1495 }
David Benjamin43ec06f2014-08-05 02:28:57 -04001496 if async {
David Benjamin6fd297b2014-08-11 18:43:38 -04001497 suffix += "-Async"
David Benjamin43ec06f2014-08-05 02:28:57 -04001498 flags = append(flags, "-async")
1499 } else {
David Benjamin6fd297b2014-08-11 18:43:38 -04001500 suffix += "-Sync"
David Benjamin43ec06f2014-08-05 02:28:57 -04001501 }
1502 if splitHandshake {
1503 suffix += "-SplitHandshakeRecords"
David Benjamin98214542014-08-07 18:02:39 -04001504 maxHandshakeRecordLength = 1
David Benjamin43ec06f2014-08-05 02:28:57 -04001505 }
1506
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001507 // Basic handshake, with resumption. Client and server,
1508 // session ID and session ticket.
David Benjamin43ec06f2014-08-05 02:28:57 -04001509 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001510 protocol: protocol,
1511 name: "Basic-Client" + suffix,
David Benjamin43ec06f2014-08-05 02:28:57 -04001512 config: Config{
1513 Bugs: ProtocolBugs{
1514 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1515 },
1516 },
David Benjaminbed9aae2014-08-07 19:13:38 -04001517 flags: flags,
1518 resumeSession: true,
1519 })
1520 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001521 protocol: protocol,
1522 name: "Basic-Client-RenewTicket" + suffix,
David Benjaminbed9aae2014-08-07 19:13:38 -04001523 config: Config{
1524 Bugs: ProtocolBugs{
1525 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1526 RenewTicketOnResume: true,
1527 },
1528 },
1529 flags: flags,
1530 resumeSession: true,
David Benjamin43ec06f2014-08-05 02:28:57 -04001531 })
1532 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001533 protocol: protocol,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001534 name: "Basic-Client-NoTicket" + suffix,
1535 config: Config{
1536 SessionTicketsDisabled: true,
1537 Bugs: ProtocolBugs{
1538 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1539 },
1540 },
1541 flags: flags,
1542 resumeSession: true,
1543 })
1544 testCases = append(testCases, testCase{
1545 protocol: protocol,
David Benjamin43ec06f2014-08-05 02:28:57 -04001546 testType: serverTest,
1547 name: "Basic-Server" + suffix,
1548 config: Config{
1549 Bugs: ProtocolBugs{
1550 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1551 },
1552 },
David Benjaminbed9aae2014-08-07 19:13:38 -04001553 flags: flags,
1554 resumeSession: true,
David Benjamin43ec06f2014-08-05 02:28:57 -04001555 })
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001556 testCases = append(testCases, testCase{
1557 protocol: protocol,
1558 testType: serverTest,
1559 name: "Basic-Server-NoTickets" + suffix,
1560 config: Config{
1561 SessionTicketsDisabled: true,
1562 Bugs: ProtocolBugs{
1563 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1564 },
1565 },
1566 flags: flags,
1567 resumeSession: true,
1568 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001569
David Benjamin6fd297b2014-08-11 18:43:38 -04001570 // TLS client auth.
1571 testCases = append(testCases, testCase{
1572 protocol: protocol,
1573 testType: clientTest,
1574 name: "ClientAuth-Client" + suffix,
1575 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001576 ClientAuth: RequireAnyClientCert,
David Benjamin6fd297b2014-08-11 18:43:38 -04001577 Bugs: ProtocolBugs{
1578 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1579 },
1580 },
1581 flags: append(flags,
1582 "-cert-file", rsaCertificateFile,
1583 "-key-file", rsaKeyFile),
1584 })
1585 testCases = append(testCases, testCase{
1586 protocol: protocol,
1587 testType: serverTest,
1588 name: "ClientAuth-Server" + suffix,
1589 config: Config{
1590 Certificates: []Certificate{rsaCertificate},
1591 },
1592 flags: append(flags, "-require-any-client-certificate"),
1593 })
1594
David Benjamin43ec06f2014-08-05 02:28:57 -04001595 // No session ticket support; server doesn't send NewSessionTicket.
1596 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001597 protocol: protocol,
1598 name: "SessionTicketsDisabled-Client" + suffix,
David Benjamin43ec06f2014-08-05 02:28:57 -04001599 config: Config{
1600 SessionTicketsDisabled: true,
1601 Bugs: ProtocolBugs{
1602 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1603 },
1604 },
1605 flags: flags,
1606 })
1607 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001608 protocol: protocol,
David Benjamin43ec06f2014-08-05 02:28:57 -04001609 testType: serverTest,
1610 name: "SessionTicketsDisabled-Server" + suffix,
1611 config: Config{
1612 SessionTicketsDisabled: true,
1613 Bugs: ProtocolBugs{
1614 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1615 },
1616 },
1617 flags: flags,
1618 })
1619
David Benjamin48cae082014-10-27 01:06:24 -04001620 // Skip ServerKeyExchange in PSK key exchange if there's no
1621 // identity hint.
1622 testCases = append(testCases, testCase{
1623 protocol: protocol,
1624 name: "EmptyPSKHint-Client" + suffix,
1625 config: Config{
1626 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
1627 PreSharedKey: []byte("secret"),
1628 Bugs: ProtocolBugs{
1629 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1630 },
1631 },
1632 flags: append(flags, "-psk", "secret"),
1633 })
1634 testCases = append(testCases, testCase{
1635 protocol: protocol,
1636 testType: serverTest,
1637 name: "EmptyPSKHint-Server" + suffix,
1638 config: Config{
1639 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
1640 PreSharedKey: []byte("secret"),
1641 Bugs: ProtocolBugs{
1642 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1643 },
1644 },
1645 flags: append(flags, "-psk", "secret"),
1646 })
1647
David Benjamin6fd297b2014-08-11 18:43:38 -04001648 if protocol == tls {
1649 // NPN on client and server; results in post-handshake message.
1650 testCases = append(testCases, testCase{
1651 protocol: protocol,
1652 name: "NPN-Client" + suffix,
1653 config: Config{
David Benjaminae2888f2014-09-06 12:58:58 -04001654 NextProtos: []string{"foo"},
David Benjamin6fd297b2014-08-11 18:43:38 -04001655 Bugs: ProtocolBugs{
1656 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1657 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001658 },
David Benjaminfc7b0862014-09-06 13:21:53 -04001659 flags: append(flags, "-select-next-proto", "foo"),
1660 expectedNextProto: "foo",
1661 expectedNextProtoType: npn,
David Benjamin6fd297b2014-08-11 18:43:38 -04001662 })
1663 testCases = append(testCases, testCase{
1664 protocol: protocol,
1665 testType: serverTest,
1666 name: "NPN-Server" + suffix,
1667 config: Config{
1668 NextProtos: []string{"bar"},
1669 Bugs: ProtocolBugs{
1670 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1671 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001672 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001673 flags: append(flags,
1674 "-advertise-npn", "\x03foo\x03bar\x03baz",
1675 "-expect-next-proto", "bar"),
David Benjaminfc7b0862014-09-06 13:21:53 -04001676 expectedNextProto: "bar",
1677 expectedNextProtoType: npn,
David Benjamin6fd297b2014-08-11 18:43:38 -04001678 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001679
David Benjamin6fd297b2014-08-11 18:43:38 -04001680 // Client does False Start and negotiates NPN.
1681 testCases = append(testCases, testCase{
1682 protocol: protocol,
1683 name: "FalseStart" + suffix,
1684 config: Config{
1685 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1686 NextProtos: []string{"foo"},
1687 Bugs: ProtocolBugs{
David Benjamine58c4f52014-08-24 03:47:07 -04001688 ExpectFalseStart: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001689 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1690 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001691 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001692 flags: append(flags,
1693 "-false-start",
1694 "-select-next-proto", "foo"),
David Benjamine58c4f52014-08-24 03:47:07 -04001695 shimWritesFirst: true,
1696 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001697 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001698
David Benjaminae2888f2014-09-06 12:58:58 -04001699 // Client does False Start and negotiates ALPN.
1700 testCases = append(testCases, testCase{
1701 protocol: protocol,
1702 name: "FalseStart-ALPN" + suffix,
1703 config: Config{
1704 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1705 NextProtos: []string{"foo"},
1706 Bugs: ProtocolBugs{
1707 ExpectFalseStart: true,
1708 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1709 },
1710 },
1711 flags: append(flags,
1712 "-false-start",
1713 "-advertise-alpn", "\x03foo"),
1714 shimWritesFirst: true,
1715 resumeSession: true,
1716 })
1717
David Benjamin6fd297b2014-08-11 18:43:38 -04001718 // False Start without session tickets.
1719 testCases = append(testCases, testCase{
1720 name: "FalseStart-SessionTicketsDisabled",
1721 config: Config{
1722 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1723 NextProtos: []string{"foo"},
1724 SessionTicketsDisabled: true,
David Benjamin4e99c522014-08-24 01:45:30 -04001725 Bugs: ProtocolBugs{
David Benjamine58c4f52014-08-24 03:47:07 -04001726 ExpectFalseStart: true,
David Benjamin4e99c522014-08-24 01:45:30 -04001727 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1728 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001729 },
David Benjamin4e99c522014-08-24 01:45:30 -04001730 flags: append(flags,
David Benjamin6fd297b2014-08-11 18:43:38 -04001731 "-false-start",
1732 "-select-next-proto", "foo",
David Benjamin4e99c522014-08-24 01:45:30 -04001733 ),
David Benjamine58c4f52014-08-24 03:47:07 -04001734 shimWritesFirst: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001735 })
David Benjamin1e7f8d72014-08-08 12:27:04 -04001736
David Benjamina08e49d2014-08-24 01:46:07 -04001737 // Server parses a V2ClientHello.
David Benjamin6fd297b2014-08-11 18:43:38 -04001738 testCases = append(testCases, testCase{
1739 protocol: protocol,
1740 testType: serverTest,
1741 name: "SendV2ClientHello" + suffix,
1742 config: Config{
1743 // Choose a cipher suite that does not involve
1744 // elliptic curves, so no extensions are
1745 // involved.
1746 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
1747 Bugs: ProtocolBugs{
1748 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1749 SendV2ClientHello: true,
1750 },
David Benjamin1e7f8d72014-08-08 12:27:04 -04001751 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001752 flags: flags,
1753 })
David Benjamina08e49d2014-08-24 01:46:07 -04001754
1755 // Client sends a Channel ID.
1756 testCases = append(testCases, testCase{
1757 protocol: protocol,
1758 name: "ChannelID-Client" + suffix,
1759 config: Config{
1760 RequestChannelID: true,
1761 Bugs: ProtocolBugs{
1762 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1763 },
1764 },
1765 flags: append(flags,
1766 "-send-channel-id", channelIDKeyFile,
1767 ),
1768 resumeSession: true,
1769 expectChannelID: true,
1770 })
1771
1772 // Server accepts a Channel ID.
1773 testCases = append(testCases, testCase{
1774 protocol: protocol,
1775 testType: serverTest,
1776 name: "ChannelID-Server" + suffix,
1777 config: Config{
1778 ChannelID: channelIDKey,
1779 Bugs: ProtocolBugs{
1780 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1781 },
1782 },
1783 flags: append(flags,
1784 "-expect-channel-id",
1785 base64.StdEncoding.EncodeToString(channelIDBytes),
1786 ),
1787 resumeSession: true,
1788 expectChannelID: true,
1789 })
David Benjamin6fd297b2014-08-11 18:43:38 -04001790 } else {
1791 testCases = append(testCases, testCase{
1792 protocol: protocol,
1793 name: "SkipHelloVerifyRequest" + suffix,
1794 config: Config{
1795 Bugs: ProtocolBugs{
1796 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1797 SkipHelloVerifyRequest: true,
1798 },
1799 },
1800 flags: flags,
1801 })
1802
1803 testCases = append(testCases, testCase{
1804 testType: serverTest,
1805 protocol: protocol,
1806 name: "CookieExchange" + suffix,
1807 config: Config{
1808 Bugs: ProtocolBugs{
1809 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1810 },
1811 },
1812 flags: append(flags, "-cookie-exchange"),
1813 })
1814 }
David Benjamin43ec06f2014-08-05 02:28:57 -04001815}
1816
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001817func addVersionNegotiationTests() {
1818 for i, shimVers := range tlsVersions {
1819 // Assemble flags to disable all newer versions on the shim.
1820 var flags []string
1821 for _, vers := range tlsVersions[i+1:] {
1822 flags = append(flags, vers.flag)
1823 }
1824
1825 for _, runnerVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05001826 protocols := []protocol{tls}
1827 if runnerVers.hasDTLS && shimVers.hasDTLS {
1828 protocols = append(protocols, dtls)
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001829 }
David Benjamin8b8c0062014-11-23 02:47:52 -05001830 for _, protocol := range protocols {
1831 expectedVersion := shimVers.version
1832 if runnerVers.version < shimVers.version {
1833 expectedVersion = runnerVers.version
1834 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001835
David Benjamin8b8c0062014-11-23 02:47:52 -05001836 suffix := shimVers.name + "-" + runnerVers.name
1837 if protocol == dtls {
1838 suffix += "-DTLS"
1839 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001840
David Benjamin1eb367c2014-12-12 18:17:51 -05001841 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
1842
David Benjamin1e29a6b2014-12-10 02:27:24 -05001843 clientVers := shimVers.version
1844 if clientVers > VersionTLS10 {
1845 clientVers = VersionTLS10
1846 }
David Benjamin8b8c0062014-11-23 02:47:52 -05001847 testCases = append(testCases, testCase{
1848 protocol: protocol,
1849 testType: clientTest,
1850 name: "VersionNegotiation-Client-" + suffix,
1851 config: Config{
1852 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05001853 Bugs: ProtocolBugs{
1854 ExpectInitialRecordVersion: clientVers,
1855 },
David Benjamin8b8c0062014-11-23 02:47:52 -05001856 },
1857 flags: flags,
1858 expectedVersion: expectedVersion,
1859 })
David Benjamin1eb367c2014-12-12 18:17:51 -05001860 testCases = append(testCases, testCase{
1861 protocol: protocol,
1862 testType: clientTest,
1863 name: "VersionNegotiation-Client2-" + suffix,
1864 config: Config{
1865 MaxVersion: runnerVers.version,
1866 Bugs: ProtocolBugs{
1867 ExpectInitialRecordVersion: clientVers,
1868 },
1869 },
1870 flags: []string{"-max-version", shimVersFlag},
1871 expectedVersion: expectedVersion,
1872 })
David Benjamin8b8c0062014-11-23 02:47:52 -05001873
1874 testCases = append(testCases, testCase{
1875 protocol: protocol,
1876 testType: serverTest,
1877 name: "VersionNegotiation-Server-" + suffix,
1878 config: Config{
1879 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05001880 Bugs: ProtocolBugs{
1881 ExpectInitialRecordVersion: expectedVersion,
1882 },
David Benjamin8b8c0062014-11-23 02:47:52 -05001883 },
1884 flags: flags,
1885 expectedVersion: expectedVersion,
1886 })
David Benjamin1eb367c2014-12-12 18:17:51 -05001887 testCases = append(testCases, testCase{
1888 protocol: protocol,
1889 testType: serverTest,
1890 name: "VersionNegotiation-Server2-" + suffix,
1891 config: Config{
1892 MaxVersion: runnerVers.version,
1893 Bugs: ProtocolBugs{
1894 ExpectInitialRecordVersion: expectedVersion,
1895 },
1896 },
1897 flags: []string{"-max-version", shimVersFlag},
1898 expectedVersion: expectedVersion,
1899 })
David Benjamin8b8c0062014-11-23 02:47:52 -05001900 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001901 }
1902 }
1903}
1904
David Benjaminaccb4542014-12-12 23:44:33 -05001905func addMinimumVersionTests() {
1906 for i, shimVers := range tlsVersions {
1907 // Assemble flags to disable all older versions on the shim.
1908 var flags []string
1909 for _, vers := range tlsVersions[:i] {
1910 flags = append(flags, vers.flag)
1911 }
1912
1913 for _, runnerVers := range tlsVersions {
1914 protocols := []protocol{tls}
1915 if runnerVers.hasDTLS && shimVers.hasDTLS {
1916 protocols = append(protocols, dtls)
1917 }
1918 for _, protocol := range protocols {
1919 suffix := shimVers.name + "-" + runnerVers.name
1920 if protocol == dtls {
1921 suffix += "-DTLS"
1922 }
1923 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
1924
David Benjaminaccb4542014-12-12 23:44:33 -05001925 var expectedVersion uint16
1926 var shouldFail bool
1927 var expectedError string
David Benjamin87909c02014-12-13 01:55:01 -05001928 var expectedLocalError string
David Benjaminaccb4542014-12-12 23:44:33 -05001929 if runnerVers.version >= shimVers.version {
1930 expectedVersion = runnerVers.version
1931 } else {
1932 shouldFail = true
1933 expectedError = ":UNSUPPORTED_PROTOCOL:"
David Benjamin87909c02014-12-13 01:55:01 -05001934 if runnerVers.version > VersionSSL30 {
1935 expectedLocalError = "remote error: protocol version not supported"
1936 } else {
1937 expectedLocalError = "remote error: handshake failure"
1938 }
David Benjaminaccb4542014-12-12 23:44:33 -05001939 }
1940
1941 testCases = append(testCases, testCase{
1942 protocol: protocol,
1943 testType: clientTest,
1944 name: "MinimumVersion-Client-" + suffix,
1945 config: Config{
1946 MaxVersion: runnerVers.version,
1947 },
David Benjamin87909c02014-12-13 01:55:01 -05001948 flags: flags,
1949 expectedVersion: expectedVersion,
1950 shouldFail: shouldFail,
1951 expectedError: expectedError,
1952 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05001953 })
1954 testCases = append(testCases, testCase{
1955 protocol: protocol,
1956 testType: clientTest,
1957 name: "MinimumVersion-Client2-" + suffix,
1958 config: Config{
1959 MaxVersion: runnerVers.version,
1960 },
David Benjamin87909c02014-12-13 01:55:01 -05001961 flags: []string{"-min-version", shimVersFlag},
1962 expectedVersion: expectedVersion,
1963 shouldFail: shouldFail,
1964 expectedError: expectedError,
1965 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05001966 })
1967
1968 testCases = append(testCases, testCase{
1969 protocol: protocol,
1970 testType: serverTest,
1971 name: "MinimumVersion-Server-" + suffix,
1972 config: Config{
1973 MaxVersion: runnerVers.version,
1974 },
David Benjamin87909c02014-12-13 01:55:01 -05001975 flags: flags,
1976 expectedVersion: expectedVersion,
1977 shouldFail: shouldFail,
1978 expectedError: expectedError,
1979 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05001980 })
1981 testCases = append(testCases, testCase{
1982 protocol: protocol,
1983 testType: serverTest,
1984 name: "MinimumVersion-Server2-" + suffix,
1985 config: Config{
1986 MaxVersion: runnerVers.version,
1987 },
David Benjamin87909c02014-12-13 01:55:01 -05001988 flags: []string{"-min-version", shimVersFlag},
1989 expectedVersion: expectedVersion,
1990 shouldFail: shouldFail,
1991 expectedError: expectedError,
1992 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05001993 })
1994 }
1995 }
1996 }
1997}
1998
David Benjamin5c24a1d2014-08-31 00:59:27 -04001999func addD5BugTests() {
2000 testCases = append(testCases, testCase{
2001 testType: serverTest,
2002 name: "D5Bug-NoQuirk-Reject",
2003 config: Config{
2004 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2005 Bugs: ProtocolBugs{
2006 SSL3RSAKeyExchange: true,
2007 },
2008 },
2009 shouldFail: true,
2010 expectedError: ":TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG:",
2011 })
2012 testCases = append(testCases, testCase{
2013 testType: serverTest,
2014 name: "D5Bug-Quirk-Normal",
2015 config: Config{
2016 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2017 },
2018 flags: []string{"-tls-d5-bug"},
2019 })
2020 testCases = append(testCases, testCase{
2021 testType: serverTest,
2022 name: "D5Bug-Quirk-Bug",
2023 config: Config{
2024 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2025 Bugs: ProtocolBugs{
2026 SSL3RSAKeyExchange: true,
2027 },
2028 },
2029 flags: []string{"-tls-d5-bug"},
2030 })
2031}
2032
David Benjamine78bfde2014-09-06 12:45:15 -04002033func addExtensionTests() {
2034 testCases = append(testCases, testCase{
2035 testType: clientTest,
2036 name: "DuplicateExtensionClient",
2037 config: Config{
2038 Bugs: ProtocolBugs{
2039 DuplicateExtension: true,
2040 },
2041 },
2042 shouldFail: true,
2043 expectedLocalError: "remote error: error decoding message",
2044 })
2045 testCases = append(testCases, testCase{
2046 testType: serverTest,
2047 name: "DuplicateExtensionServer",
2048 config: Config{
2049 Bugs: ProtocolBugs{
2050 DuplicateExtension: true,
2051 },
2052 },
2053 shouldFail: true,
2054 expectedLocalError: "remote error: error decoding message",
2055 })
2056 testCases = append(testCases, testCase{
2057 testType: clientTest,
2058 name: "ServerNameExtensionClient",
2059 config: Config{
2060 Bugs: ProtocolBugs{
2061 ExpectServerName: "example.com",
2062 },
2063 },
2064 flags: []string{"-host-name", "example.com"},
2065 })
2066 testCases = append(testCases, testCase{
2067 testType: clientTest,
2068 name: "ServerNameExtensionClient",
2069 config: Config{
2070 Bugs: ProtocolBugs{
2071 ExpectServerName: "mismatch.com",
2072 },
2073 },
2074 flags: []string{"-host-name", "example.com"},
2075 shouldFail: true,
2076 expectedLocalError: "tls: unexpected server name",
2077 })
2078 testCases = append(testCases, testCase{
2079 testType: clientTest,
2080 name: "ServerNameExtensionClient",
2081 config: Config{
2082 Bugs: ProtocolBugs{
2083 ExpectServerName: "missing.com",
2084 },
2085 },
2086 shouldFail: true,
2087 expectedLocalError: "tls: unexpected server name",
2088 })
2089 testCases = append(testCases, testCase{
2090 testType: serverTest,
2091 name: "ServerNameExtensionServer",
2092 config: Config{
2093 ServerName: "example.com",
2094 },
2095 flags: []string{"-expect-server-name", "example.com"},
2096 resumeSession: true,
2097 })
David Benjaminae2888f2014-09-06 12:58:58 -04002098 testCases = append(testCases, testCase{
2099 testType: clientTest,
2100 name: "ALPNClient",
2101 config: Config{
2102 NextProtos: []string{"foo"},
2103 },
2104 flags: []string{
2105 "-advertise-alpn", "\x03foo\x03bar\x03baz",
2106 "-expect-alpn", "foo",
2107 },
David Benjaminfc7b0862014-09-06 13:21:53 -04002108 expectedNextProto: "foo",
2109 expectedNextProtoType: alpn,
2110 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04002111 })
2112 testCases = append(testCases, testCase{
2113 testType: serverTest,
2114 name: "ALPNServer",
2115 config: Config{
2116 NextProtos: []string{"foo", "bar", "baz"},
2117 },
2118 flags: []string{
2119 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2120 "-select-alpn", "foo",
2121 },
David Benjaminfc7b0862014-09-06 13:21:53 -04002122 expectedNextProto: "foo",
2123 expectedNextProtoType: alpn,
2124 resumeSession: true,
2125 })
2126 // Test that the server prefers ALPN over NPN.
2127 testCases = append(testCases, testCase{
2128 testType: serverTest,
2129 name: "ALPNServer-Preferred",
2130 config: Config{
2131 NextProtos: []string{"foo", "bar", "baz"},
2132 },
2133 flags: []string{
2134 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2135 "-select-alpn", "foo",
2136 "-advertise-npn", "\x03foo\x03bar\x03baz",
2137 },
2138 expectedNextProto: "foo",
2139 expectedNextProtoType: alpn,
2140 resumeSession: true,
2141 })
2142 testCases = append(testCases, testCase{
2143 testType: serverTest,
2144 name: "ALPNServer-Preferred-Swapped",
2145 config: Config{
2146 NextProtos: []string{"foo", "bar", "baz"},
2147 Bugs: ProtocolBugs{
2148 SwapNPNAndALPN: true,
2149 },
2150 },
2151 flags: []string{
2152 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2153 "-select-alpn", "foo",
2154 "-advertise-npn", "\x03foo\x03bar\x03baz",
2155 },
2156 expectedNextProto: "foo",
2157 expectedNextProtoType: alpn,
2158 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04002159 })
Adam Langley38311732014-10-16 19:04:35 -07002160 // Resume with a corrupt ticket.
2161 testCases = append(testCases, testCase{
2162 testType: serverTest,
2163 name: "CorruptTicket",
2164 config: Config{
2165 Bugs: ProtocolBugs{
2166 CorruptTicket: true,
2167 },
2168 },
2169 resumeSession: true,
2170 flags: []string{"-expect-session-miss"},
2171 })
2172 // Resume with an oversized session id.
2173 testCases = append(testCases, testCase{
2174 testType: serverTest,
2175 name: "OversizedSessionId",
2176 config: Config{
2177 Bugs: ProtocolBugs{
2178 OversizedSessionId: true,
2179 },
2180 },
2181 resumeSession: true,
Adam Langley75712922014-10-10 16:23:43 -07002182 shouldFail: true,
Adam Langley38311732014-10-16 19:04:35 -07002183 expectedError: ":DECODE_ERROR:",
2184 })
David Benjaminca6c8262014-11-15 19:06:08 -05002185 // Basic DTLS-SRTP tests. Include fake profiles to ensure they
2186 // are ignored.
2187 testCases = append(testCases, testCase{
2188 protocol: dtls,
2189 name: "SRTP-Client",
2190 config: Config{
2191 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
2192 },
2193 flags: []string{
2194 "-srtp-profiles",
2195 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2196 },
2197 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2198 })
2199 testCases = append(testCases, testCase{
2200 protocol: dtls,
2201 testType: serverTest,
2202 name: "SRTP-Server",
2203 config: Config{
2204 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
2205 },
2206 flags: []string{
2207 "-srtp-profiles",
2208 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2209 },
2210 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2211 })
2212 // Test that the MKI is ignored.
2213 testCases = append(testCases, testCase{
2214 protocol: dtls,
2215 testType: serverTest,
2216 name: "SRTP-Server-IgnoreMKI",
2217 config: Config{
2218 SRTPProtectionProfiles: []uint16{SRTP_AES128_CM_HMAC_SHA1_80},
2219 Bugs: ProtocolBugs{
2220 SRTPMasterKeyIdentifer: "bogus",
2221 },
2222 },
2223 flags: []string{
2224 "-srtp-profiles",
2225 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2226 },
2227 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2228 })
2229 // Test that SRTP isn't negotiated on the server if there were
2230 // no matching profiles.
2231 testCases = append(testCases, testCase{
2232 protocol: dtls,
2233 testType: serverTest,
2234 name: "SRTP-Server-NoMatch",
2235 config: Config{
2236 SRTPProtectionProfiles: []uint16{100, 101, 102},
2237 },
2238 flags: []string{
2239 "-srtp-profiles",
2240 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2241 },
2242 expectedSRTPProtectionProfile: 0,
2243 })
2244 // Test that the server returning an invalid SRTP profile is
2245 // flagged as an error by the client.
2246 testCases = append(testCases, testCase{
2247 protocol: dtls,
2248 name: "SRTP-Client-NoMatch",
2249 config: Config{
2250 Bugs: ProtocolBugs{
2251 SendSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_32,
2252 },
2253 },
2254 flags: []string{
2255 "-srtp-profiles",
2256 "SRTP_AES128_CM_SHA1_80",
2257 },
2258 shouldFail: true,
2259 expectedError: ":BAD_SRTP_PROTECTION_PROFILE_LIST:",
2260 })
David Benjamin61f95272014-11-25 01:55:35 -05002261 // Test OCSP stapling and SCT list.
2262 testCases = append(testCases, testCase{
2263 name: "OCSPStapling",
2264 flags: []string{
2265 "-enable-ocsp-stapling",
2266 "-expect-ocsp-response",
2267 base64.StdEncoding.EncodeToString(testOCSPResponse),
2268 },
2269 })
2270 testCases = append(testCases, testCase{
2271 name: "SignedCertificateTimestampList",
2272 flags: []string{
2273 "-enable-signed-cert-timestamps",
2274 "-expect-signed-cert-timestamps",
2275 base64.StdEncoding.EncodeToString(testSCTList),
2276 },
2277 })
David Benjamine78bfde2014-09-06 12:45:15 -04002278}
2279
David Benjamin01fe8202014-09-24 15:21:44 -04002280func addResumptionVersionTests() {
David Benjamin01fe8202014-09-24 15:21:44 -04002281 for _, sessionVers := range tlsVersions {
David Benjamin01fe8202014-09-24 15:21:44 -04002282 for _, resumeVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05002283 protocols := []protocol{tls}
2284 if sessionVers.hasDTLS && resumeVers.hasDTLS {
2285 protocols = append(protocols, dtls)
David Benjaminbdf5e722014-11-11 00:52:15 -05002286 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002287 for _, protocol := range protocols {
2288 suffix := "-" + sessionVers.name + "-" + resumeVers.name
2289 if protocol == dtls {
2290 suffix += "-DTLS"
2291 }
2292
2293 testCases = append(testCases, testCase{
2294 protocol: protocol,
2295 name: "Resume-Client" + suffix,
2296 resumeSession: true,
2297 config: Config{
2298 MaxVersion: sessionVers.version,
2299 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2300 Bugs: ProtocolBugs{
2301 AllowSessionVersionMismatch: true,
2302 },
2303 },
2304 expectedVersion: sessionVers.version,
2305 resumeConfig: &Config{
2306 MaxVersion: resumeVers.version,
2307 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2308 Bugs: ProtocolBugs{
2309 AllowSessionVersionMismatch: true,
2310 },
2311 },
2312 expectedResumeVersion: resumeVers.version,
2313 })
2314
2315 testCases = append(testCases, testCase{
2316 protocol: protocol,
2317 name: "Resume-Client-NoResume" + suffix,
2318 flags: []string{"-expect-session-miss"},
2319 resumeSession: true,
2320 config: Config{
2321 MaxVersion: sessionVers.version,
2322 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2323 },
2324 expectedVersion: sessionVers.version,
2325 resumeConfig: &Config{
2326 MaxVersion: resumeVers.version,
2327 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2328 },
2329 newSessionsOnResume: true,
2330 expectedResumeVersion: resumeVers.version,
2331 })
2332
2333 var flags []string
2334 if sessionVers.version != resumeVers.version {
2335 flags = append(flags, "-expect-session-miss")
2336 }
2337 testCases = append(testCases, testCase{
2338 protocol: protocol,
2339 testType: serverTest,
2340 name: "Resume-Server" + suffix,
2341 flags: flags,
2342 resumeSession: true,
2343 config: Config{
2344 MaxVersion: sessionVers.version,
2345 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2346 },
2347 expectedVersion: sessionVers.version,
2348 resumeConfig: &Config{
2349 MaxVersion: resumeVers.version,
2350 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2351 },
2352 expectedResumeVersion: resumeVers.version,
2353 })
2354 }
David Benjamin01fe8202014-09-24 15:21:44 -04002355 }
2356 }
2357}
2358
Adam Langley2ae77d22014-10-28 17:29:33 -07002359func addRenegotiationTests() {
2360 testCases = append(testCases, testCase{
2361 testType: serverTest,
2362 name: "Renegotiate-Server",
2363 flags: []string{"-renegotiate"},
2364 shimWritesFirst: true,
2365 })
2366 testCases = append(testCases, testCase{
2367 testType: serverTest,
2368 name: "Renegotiate-Server-EmptyExt",
2369 config: Config{
2370 Bugs: ProtocolBugs{
2371 EmptyRenegotiationInfo: true,
2372 },
2373 },
2374 flags: []string{"-renegotiate"},
2375 shimWritesFirst: true,
2376 shouldFail: true,
2377 expectedError: ":RENEGOTIATION_MISMATCH:",
2378 })
2379 testCases = append(testCases, testCase{
2380 testType: serverTest,
2381 name: "Renegotiate-Server-BadExt",
2382 config: Config{
2383 Bugs: ProtocolBugs{
2384 BadRenegotiationInfo: true,
2385 },
2386 },
2387 flags: []string{"-renegotiate"},
2388 shimWritesFirst: true,
2389 shouldFail: true,
2390 expectedError: ":RENEGOTIATION_MISMATCH:",
2391 })
David Benjaminca6554b2014-11-08 12:31:52 -05002392 testCases = append(testCases, testCase{
2393 testType: serverTest,
2394 name: "Renegotiate-Server-ClientInitiated",
2395 renegotiate: true,
2396 })
2397 testCases = append(testCases, testCase{
2398 testType: serverTest,
2399 name: "Renegotiate-Server-ClientInitiated-NoExt",
2400 renegotiate: true,
2401 config: Config{
2402 Bugs: ProtocolBugs{
2403 NoRenegotiationInfo: true,
2404 },
2405 },
2406 shouldFail: true,
2407 expectedError: ":UNSAFE_LEGACY_RENEGOTIATION_DISABLED:",
2408 })
2409 testCases = append(testCases, testCase{
2410 testType: serverTest,
2411 name: "Renegotiate-Server-ClientInitiated-NoExt-Allowed",
2412 renegotiate: true,
2413 config: Config{
2414 Bugs: ProtocolBugs{
2415 NoRenegotiationInfo: true,
2416 },
2417 },
2418 flags: []string{"-allow-unsafe-legacy-renegotiation"},
2419 })
Adam Langley2ae77d22014-10-28 17:29:33 -07002420 // TODO(agl): test the renegotiation info SCSV.
Adam Langleycf2d4f42014-10-28 19:06:14 -07002421 testCases = append(testCases, testCase{
2422 name: "Renegotiate-Client",
2423 renegotiate: true,
2424 })
2425 testCases = append(testCases, testCase{
2426 name: "Renegotiate-Client-EmptyExt",
2427 renegotiate: true,
2428 config: Config{
2429 Bugs: ProtocolBugs{
2430 EmptyRenegotiationInfo: true,
2431 },
2432 },
2433 shouldFail: true,
2434 expectedError: ":RENEGOTIATION_MISMATCH:",
2435 })
2436 testCases = append(testCases, testCase{
2437 name: "Renegotiate-Client-BadExt",
2438 renegotiate: true,
2439 config: Config{
2440 Bugs: ProtocolBugs{
2441 BadRenegotiationInfo: true,
2442 },
2443 },
2444 shouldFail: true,
2445 expectedError: ":RENEGOTIATION_MISMATCH:",
2446 })
2447 testCases = append(testCases, testCase{
2448 name: "Renegotiate-Client-SwitchCiphers",
2449 renegotiate: true,
2450 config: Config{
2451 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2452 },
2453 renegotiateCiphers: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2454 })
2455 testCases = append(testCases, testCase{
2456 name: "Renegotiate-Client-SwitchCiphers2",
2457 renegotiate: true,
2458 config: Config{
2459 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2460 },
2461 renegotiateCiphers: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2462 })
David Benjaminc44b1df2014-11-23 12:11:01 -05002463 testCases = append(testCases, testCase{
2464 name: "Renegotiate-SameClientVersion",
2465 renegotiate: true,
2466 config: Config{
2467 MaxVersion: VersionTLS10,
2468 Bugs: ProtocolBugs{
2469 RequireSameRenegoClientVersion: true,
2470 },
2471 },
2472 })
Adam Langley2ae77d22014-10-28 17:29:33 -07002473}
2474
David Benjamin5e961c12014-11-07 01:48:35 -05002475func addDTLSReplayTests() {
2476 // Test that sequence number replays are detected.
2477 testCases = append(testCases, testCase{
2478 protocol: dtls,
2479 name: "DTLS-Replay",
2480 replayWrites: true,
2481 })
2482
2483 // Test the outgoing sequence number skipping by values larger
2484 // than the retransmit window.
2485 testCases = append(testCases, testCase{
2486 protocol: dtls,
2487 name: "DTLS-Replay-LargeGaps",
2488 config: Config{
2489 Bugs: ProtocolBugs{
2490 SequenceNumberIncrement: 127,
2491 },
2492 },
2493 replayWrites: true,
2494 })
2495}
2496
Feng Lu41aa3252014-11-21 22:47:56 -08002497func addFastRadioPaddingTests() {
David Benjamin1e29a6b2014-12-10 02:27:24 -05002498 testCases = append(testCases, testCase{
2499 protocol: tls,
2500 name: "FastRadio-Padding",
Feng Lu41aa3252014-11-21 22:47:56 -08002501 config: Config{
2502 Bugs: ProtocolBugs{
2503 RequireFastradioPadding: true,
2504 },
2505 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05002506 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08002507 })
David Benjamin1e29a6b2014-12-10 02:27:24 -05002508 testCases = append(testCases, testCase{
2509 protocol: dtls,
2510 name: "FastRadio-Padding",
Feng Lu41aa3252014-11-21 22:47:56 -08002511 config: Config{
2512 Bugs: ProtocolBugs{
2513 RequireFastradioPadding: true,
2514 },
2515 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05002516 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08002517 })
2518}
2519
David Benjamin000800a2014-11-14 01:43:59 -05002520var testHashes = []struct {
2521 name string
2522 id uint8
2523}{
2524 {"SHA1", hashSHA1},
2525 {"SHA224", hashSHA224},
2526 {"SHA256", hashSHA256},
2527 {"SHA384", hashSHA384},
2528 {"SHA512", hashSHA512},
2529}
2530
2531func addSigningHashTests() {
2532 // Make sure each hash works. Include some fake hashes in the list and
2533 // ensure they're ignored.
2534 for _, hash := range testHashes {
2535 testCases = append(testCases, testCase{
2536 name: "SigningHash-ClientAuth-" + hash.name,
2537 config: Config{
2538 ClientAuth: RequireAnyClientCert,
2539 SignatureAndHashes: []signatureAndHash{
2540 {signatureRSA, 42},
2541 {signatureRSA, hash.id},
2542 {signatureRSA, 255},
2543 },
2544 },
2545 flags: []string{
2546 "-cert-file", rsaCertificateFile,
2547 "-key-file", rsaKeyFile,
2548 },
2549 })
2550
2551 testCases = append(testCases, testCase{
2552 testType: serverTest,
2553 name: "SigningHash-ServerKeyExchange-Sign-" + hash.name,
2554 config: Config{
2555 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2556 SignatureAndHashes: []signatureAndHash{
2557 {signatureRSA, 42},
2558 {signatureRSA, hash.id},
2559 {signatureRSA, 255},
2560 },
2561 },
2562 })
2563 }
2564
2565 // Test that hash resolution takes the signature type into account.
2566 testCases = append(testCases, testCase{
2567 name: "SigningHash-ClientAuth-SignatureType",
2568 config: Config{
2569 ClientAuth: RequireAnyClientCert,
2570 SignatureAndHashes: []signatureAndHash{
2571 {signatureECDSA, hashSHA512},
2572 {signatureRSA, hashSHA384},
2573 {signatureECDSA, hashSHA1},
2574 },
2575 },
2576 flags: []string{
2577 "-cert-file", rsaCertificateFile,
2578 "-key-file", rsaKeyFile,
2579 },
2580 })
2581
2582 testCases = append(testCases, testCase{
2583 testType: serverTest,
2584 name: "SigningHash-ServerKeyExchange-SignatureType",
2585 config: Config{
2586 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2587 SignatureAndHashes: []signatureAndHash{
2588 {signatureECDSA, hashSHA512},
2589 {signatureRSA, hashSHA384},
2590 {signatureECDSA, hashSHA1},
2591 },
2592 },
2593 })
2594
2595 // Test that, if the list is missing, the peer falls back to SHA-1.
2596 testCases = append(testCases, testCase{
2597 name: "SigningHash-ClientAuth-Fallback",
2598 config: Config{
2599 ClientAuth: RequireAnyClientCert,
2600 SignatureAndHashes: []signatureAndHash{
2601 {signatureRSA, hashSHA1},
2602 },
2603 Bugs: ProtocolBugs{
2604 NoSignatureAndHashes: true,
2605 },
2606 },
2607 flags: []string{
2608 "-cert-file", rsaCertificateFile,
2609 "-key-file", rsaKeyFile,
2610 },
2611 })
2612
2613 testCases = append(testCases, testCase{
2614 testType: serverTest,
2615 name: "SigningHash-ServerKeyExchange-Fallback",
2616 config: Config{
2617 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2618 SignatureAndHashes: []signatureAndHash{
2619 {signatureRSA, hashSHA1},
2620 },
2621 Bugs: ProtocolBugs{
2622 NoSignatureAndHashes: true,
2623 },
2624 },
2625 })
2626}
2627
David Benjamin83f90402015-01-27 01:09:43 -05002628// timeouts is the retransmit schedule for BoringSSL. It doubles and
2629// caps at 60 seconds. On the 13th timeout, it gives up.
2630var timeouts = []time.Duration{
2631 1 * time.Second,
2632 2 * time.Second,
2633 4 * time.Second,
2634 8 * time.Second,
2635 16 * time.Second,
2636 32 * time.Second,
2637 60 * time.Second,
2638 60 * time.Second,
2639 60 * time.Second,
2640 60 * time.Second,
2641 60 * time.Second,
2642 60 * time.Second,
2643 60 * time.Second,
2644}
2645
2646func addDTLSRetransmitTests() {
2647 // Test that this is indeed the timeout schedule. Stress all
2648 // four patterns of handshake.
2649 for i := 1; i < len(timeouts); i++ {
2650 number := strconv.Itoa(i)
2651 testCases = append(testCases, testCase{
2652 protocol: dtls,
2653 name: "DTLS-Retransmit-Client-" + number,
2654 config: Config{
2655 Bugs: ProtocolBugs{
2656 TimeoutSchedule: timeouts[:i],
2657 },
2658 },
2659 resumeSession: true,
2660 flags: []string{"-async"},
2661 })
2662 testCases = append(testCases, testCase{
2663 protocol: dtls,
2664 testType: serverTest,
2665 name: "DTLS-Retransmit-Server-" + number,
2666 config: Config{
2667 Bugs: ProtocolBugs{
2668 TimeoutSchedule: timeouts[:i],
2669 },
2670 },
2671 resumeSession: true,
2672 flags: []string{"-async"},
2673 })
2674 }
2675
2676 // Test that exceeding the timeout schedule hits a read
2677 // timeout.
2678 testCases = append(testCases, testCase{
2679 protocol: dtls,
2680 name: "DTLS-Retransmit-Timeout",
2681 config: Config{
2682 Bugs: ProtocolBugs{
2683 TimeoutSchedule: timeouts,
2684 },
2685 },
2686 resumeSession: true,
2687 flags: []string{"-async"},
2688 shouldFail: true,
2689 expectedError: ":READ_TIMEOUT_EXPIRED:",
2690 })
2691
2692 // Test that timeout handling has a fudge factor, due to API
2693 // problems.
2694 testCases = append(testCases, testCase{
2695 protocol: dtls,
2696 name: "DTLS-Retransmit-Fudge",
2697 config: Config{
2698 Bugs: ProtocolBugs{
2699 TimeoutSchedule: []time.Duration{
2700 timeouts[0] - 10*time.Millisecond,
2701 },
2702 },
2703 },
2704 resumeSession: true,
2705 flags: []string{"-async"},
2706 })
2707}
2708
David Benjamin884fdf12014-08-02 15:28:23 -04002709func worker(statusChan chan statusMsg, c chan *testCase, buildDir string, wg *sync.WaitGroup) {
Adam Langley95c29f32014-06-20 12:00:00 -07002710 defer wg.Done()
2711
2712 for test := range c {
Adam Langley69a01602014-11-17 17:26:55 -08002713 var err error
2714
2715 if *mallocTest < 0 {
2716 statusChan <- statusMsg{test: test, started: true}
2717 err = runTest(test, buildDir, -1)
2718 } else {
2719 for mallocNumToFail := int64(*mallocTest); ; mallocNumToFail++ {
2720 statusChan <- statusMsg{test: test, started: true}
2721 if err = runTest(test, buildDir, mallocNumToFail); err != errMoreMallocs {
2722 if err != nil {
2723 fmt.Printf("\n\nmalloc test failed at %d: %s\n", mallocNumToFail, err)
2724 }
2725 break
2726 }
2727 }
2728 }
Adam Langley95c29f32014-06-20 12:00:00 -07002729 statusChan <- statusMsg{test: test, err: err}
2730 }
2731}
2732
2733type statusMsg struct {
2734 test *testCase
2735 started bool
2736 err error
2737}
2738
2739func statusPrinter(doneChan chan struct{}, statusChan chan statusMsg, total int) {
2740 var started, done, failed, lineLen int
2741 defer close(doneChan)
2742
2743 for msg := range statusChan {
2744 if msg.started {
2745 started++
2746 } else {
2747 done++
2748 }
2749
2750 fmt.Printf("\x1b[%dD\x1b[K", lineLen)
2751
2752 if msg.err != nil {
2753 fmt.Printf("FAILED (%s)\n%s\n", msg.test.name, msg.err)
2754 failed++
2755 }
2756 line := fmt.Sprintf("%d/%d/%d/%d", failed, done, started, total)
2757 lineLen = len(line)
2758 os.Stdout.WriteString(line)
2759 }
2760}
2761
2762func main() {
2763 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 -04002764 var flagNumWorkers *int = flag.Int("num-workers", runtime.NumCPU(), "The number of workers to run in parallel.")
David Benjamin884fdf12014-08-02 15:28:23 -04002765 var flagBuildDir *string = flag.String("build-dir", "../../../build", "The build directory to run the shim from.")
Adam Langley95c29f32014-06-20 12:00:00 -07002766
2767 flag.Parse()
2768
2769 addCipherSuiteTests()
2770 addBadECDSASignatureTests()
Adam Langley80842bd2014-06-20 12:00:00 -07002771 addCBCPaddingTests()
Kenny Root7fdeaf12014-08-05 15:23:37 -07002772 addCBCSplittingTests()
David Benjamin636293b2014-07-08 17:59:18 -04002773 addClientAuthTests()
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002774 addVersionNegotiationTests()
David Benjaminaccb4542014-12-12 23:44:33 -05002775 addMinimumVersionTests()
David Benjamin5c24a1d2014-08-31 00:59:27 -04002776 addD5BugTests()
David Benjamine78bfde2014-09-06 12:45:15 -04002777 addExtensionTests()
David Benjamin01fe8202014-09-24 15:21:44 -04002778 addResumptionVersionTests()
Adam Langley75712922014-10-10 16:23:43 -07002779 addExtendedMasterSecretTests()
Adam Langley2ae77d22014-10-28 17:29:33 -07002780 addRenegotiationTests()
David Benjamin5e961c12014-11-07 01:48:35 -05002781 addDTLSReplayTests()
David Benjamin000800a2014-11-14 01:43:59 -05002782 addSigningHashTests()
Feng Lu41aa3252014-11-21 22:47:56 -08002783 addFastRadioPaddingTests()
David Benjamin83f90402015-01-27 01:09:43 -05002784 addDTLSRetransmitTests()
David Benjamin43ec06f2014-08-05 02:28:57 -04002785 for _, async := range []bool{false, true} {
2786 for _, splitHandshake := range []bool{false, true} {
David Benjamin6fd297b2014-08-11 18:43:38 -04002787 for _, protocol := range []protocol{tls, dtls} {
2788 addStateMachineCoverageTests(async, splitHandshake, protocol)
2789 }
David Benjamin43ec06f2014-08-05 02:28:57 -04002790 }
2791 }
Adam Langley95c29f32014-06-20 12:00:00 -07002792
2793 var wg sync.WaitGroup
2794
David Benjamin2bc8e6f2014-08-02 15:22:37 -04002795 numWorkers := *flagNumWorkers
Adam Langley95c29f32014-06-20 12:00:00 -07002796
2797 statusChan := make(chan statusMsg, numWorkers)
2798 testChan := make(chan *testCase, numWorkers)
2799 doneChan := make(chan struct{})
2800
David Benjamin025b3d32014-07-01 19:53:04 -04002801 go statusPrinter(doneChan, statusChan, len(testCases))
Adam Langley95c29f32014-06-20 12:00:00 -07002802
2803 for i := 0; i < numWorkers; i++ {
2804 wg.Add(1)
David Benjamin884fdf12014-08-02 15:28:23 -04002805 go worker(statusChan, testChan, *flagBuildDir, &wg)
Adam Langley95c29f32014-06-20 12:00:00 -07002806 }
2807
David Benjamin025b3d32014-07-01 19:53:04 -04002808 for i := range testCases {
2809 if len(*flagTest) == 0 || *flagTest == testCases[i].name {
2810 testChan <- &testCases[i]
Adam Langley95c29f32014-06-20 12:00:00 -07002811 }
2812 }
2813
2814 close(testChan)
2815 wg.Wait()
2816 close(statusChan)
2817 <-doneChan
2818
2819 fmt.Printf("\n")
2820}