blob: 0d207705f525fd8c8a03ec3e361f2e9fefe91773 [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 (
David Benjamin5f237bc2015-02-11 17:14:15 -050027 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 = flag.Bool("debug", false, "Hexdump the contents of the connection")
30 mallocTest = flag.Int64("malloc-test", -1, "If non-negative, run each test with each malloc in turn failing from the given number onwards.")
31 mallocTestDebug = 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 jsonOutput = flag.String("json-output", "", "The file to output JSON results to.")
33 pipe = flag.Bool("pipe", false, "If true, print status output suitable for piping into another program.")
Adam Langley69a01602014-11-17 17:26:55 -080034)
Adam Langley95c29f32014-06-20 12:00:00 -070035
David Benjamin025b3d32014-07-01 19:53:04 -040036const (
37 rsaCertificateFile = "cert.pem"
38 ecdsaCertificateFile = "ecdsa_cert.pem"
39)
40
41const (
David Benjamina08e49d2014-08-24 01:46:07 -040042 rsaKeyFile = "key.pem"
43 ecdsaKeyFile = "ecdsa_key.pem"
44 channelIDKeyFile = "channel_id_key.pem"
David Benjamin025b3d32014-07-01 19:53:04 -040045)
46
Adam Langley95c29f32014-06-20 12:00:00 -070047var rsaCertificate, ecdsaCertificate Certificate
David Benjamina08e49d2014-08-24 01:46:07 -040048var channelIDKey *ecdsa.PrivateKey
49var channelIDBytes []byte
Adam Langley95c29f32014-06-20 12:00:00 -070050
David Benjamin61f95272014-11-25 01:55:35 -050051var testOCSPResponse = []byte{1, 2, 3, 4}
52var testSCTList = []byte{5, 6, 7, 8}
53
Adam Langley95c29f32014-06-20 12:00:00 -070054func initCertificates() {
55 var err error
David Benjamin025b3d32014-07-01 19:53:04 -040056 rsaCertificate, err = LoadX509KeyPair(rsaCertificateFile, rsaKeyFile)
Adam Langley95c29f32014-06-20 12:00:00 -070057 if err != nil {
58 panic(err)
59 }
David Benjamin61f95272014-11-25 01:55:35 -050060 rsaCertificate.OCSPStaple = testOCSPResponse
61 rsaCertificate.SignedCertificateTimestampList = testSCTList
Adam Langley95c29f32014-06-20 12:00:00 -070062
David Benjamin025b3d32014-07-01 19:53:04 -040063 ecdsaCertificate, err = LoadX509KeyPair(ecdsaCertificateFile, ecdsaKeyFile)
Adam Langley95c29f32014-06-20 12:00:00 -070064 if err != nil {
65 panic(err)
66 }
David Benjamin61f95272014-11-25 01:55:35 -050067 ecdsaCertificate.OCSPStaple = testOCSPResponse
68 ecdsaCertificate.SignedCertificateTimestampList = testSCTList
David Benjamina08e49d2014-08-24 01:46:07 -040069
70 channelIDPEMBlock, err := ioutil.ReadFile(channelIDKeyFile)
71 if err != nil {
72 panic(err)
73 }
74 channelIDDERBlock, _ := pem.Decode(channelIDPEMBlock)
75 if channelIDDERBlock.Type != "EC PRIVATE KEY" {
76 panic("bad key type")
77 }
78 channelIDKey, err = x509.ParseECPrivateKey(channelIDDERBlock.Bytes)
79 if err != nil {
80 panic(err)
81 }
82 if channelIDKey.Curve != elliptic.P256() {
83 panic("bad curve")
84 }
85
86 channelIDBytes = make([]byte, 64)
87 writeIntPadded(channelIDBytes[:32], channelIDKey.X)
88 writeIntPadded(channelIDBytes[32:], channelIDKey.Y)
Adam Langley95c29f32014-06-20 12:00:00 -070089}
90
91var certificateOnce sync.Once
92
93func getRSACertificate() Certificate {
94 certificateOnce.Do(initCertificates)
95 return rsaCertificate
96}
97
98func getECDSACertificate() Certificate {
99 certificateOnce.Do(initCertificates)
100 return ecdsaCertificate
101}
102
David Benjamin025b3d32014-07-01 19:53:04 -0400103type testType int
104
105const (
106 clientTest testType = iota
107 serverTest
108)
109
David Benjamin6fd297b2014-08-11 18:43:38 -0400110type protocol int
111
112const (
113 tls protocol = iota
114 dtls
115)
116
David Benjaminfc7b0862014-09-06 13:21:53 -0400117const (
118 alpn = 1
119 npn = 2
120)
121
Adam Langley95c29f32014-06-20 12:00:00 -0700122type testCase struct {
David Benjamin025b3d32014-07-01 19:53:04 -0400123 testType testType
David Benjamin6fd297b2014-08-11 18:43:38 -0400124 protocol protocol
Adam Langley95c29f32014-06-20 12:00:00 -0700125 name string
126 config Config
127 shouldFail bool
128 expectedError string
Adam Langleyac61fa32014-06-23 12:03:11 -0700129 // expectedLocalError, if not empty, contains a substring that must be
130 // found in the local error.
131 expectedLocalError string
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400132 // expectedVersion, if non-zero, specifies the TLS version that must be
133 // negotiated.
134 expectedVersion uint16
David Benjamin01fe8202014-09-24 15:21:44 -0400135 // expectedResumeVersion, if non-zero, specifies the TLS version that
136 // must be negotiated on resumption. If zero, expectedVersion is used.
137 expectedResumeVersion uint16
David Benjamina08e49d2014-08-24 01:46:07 -0400138 // expectChannelID controls whether the connection should have
139 // negotiated a Channel ID with channelIDKey.
140 expectChannelID bool
David Benjaminae2888f2014-09-06 12:58:58 -0400141 // expectedNextProto controls whether the connection should
142 // negotiate a next protocol via NPN or ALPN.
143 expectedNextProto string
David Benjaminfc7b0862014-09-06 13:21:53 -0400144 // expectedNextProtoType, if non-zero, is the expected next
145 // protocol negotiation mechanism.
146 expectedNextProtoType int
David Benjaminca6c8262014-11-15 19:06:08 -0500147 // expectedSRTPProtectionProfile is the DTLS-SRTP profile that
148 // should be negotiated. If zero, none should be negotiated.
149 expectedSRTPProtectionProfile uint16
Adam Langley80842bd2014-06-20 12:00:00 -0700150 // messageLen is the length, in bytes, of the test message that will be
151 // sent.
152 messageLen int
David Benjamin025b3d32014-07-01 19:53:04 -0400153 // certFile is the path to the certificate to use for the server.
154 certFile string
155 // keyFile is the path to the private key to use for the server.
156 keyFile string
David Benjamin1d5c83e2014-07-22 19:20:02 -0400157 // resumeSession controls whether a second connection should be tested
David Benjamin01fe8202014-09-24 15:21:44 -0400158 // which attempts to resume the first session.
David Benjamin1d5c83e2014-07-22 19:20:02 -0400159 resumeSession bool
David Benjamin01fe8202014-09-24 15:21:44 -0400160 // resumeConfig, if not nil, points to a Config to be used on
David Benjaminfe8eb9a2014-11-17 03:19:02 -0500161 // resumption. Unless newSessionsOnResume is set,
162 // SessionTicketKey, ServerSessionCache, and
163 // ClientSessionCache are copied from the initial connection's
164 // config. If nil, the initial connection's config is used.
David Benjamin01fe8202014-09-24 15:21:44 -0400165 resumeConfig *Config
David Benjaminfe8eb9a2014-11-17 03:19:02 -0500166 // newSessionsOnResume, if true, will cause resumeConfig to
167 // use a different session resumption context.
168 newSessionsOnResume bool
David Benjamin98e882e2014-08-08 13:24:34 -0400169 // sendPrefix sends a prefix on the socket before actually performing a
170 // handshake.
171 sendPrefix string
David Benjamine58c4f52014-08-24 03:47:07 -0400172 // shimWritesFirst controls whether the shim sends an initial "hello"
173 // message before doing a roundtrip with the runner.
174 shimWritesFirst bool
Adam Langleycf2d4f42014-10-28 19:06:14 -0700175 // renegotiate indicates the the connection should be renegotiated
176 // during the exchange.
177 renegotiate bool
178 // renegotiateCiphers is a list of ciphersuite ids that will be
179 // switched in just before renegotiation.
180 renegotiateCiphers []uint16
David Benjamin5e961c12014-11-07 01:48:35 -0500181 // replayWrites, if true, configures the underlying transport
182 // to replay every write it makes in DTLS tests.
183 replayWrites bool
David Benjamin5fa3eba2015-01-22 16:35:40 -0500184 // damageFirstWrite, if true, configures the underlying transport to
185 // damage the final byte of the first application data write.
186 damageFirstWrite bool
David Benjamin325b5c32014-07-01 19:40:31 -0400187 // flags, if not empty, contains a list of command-line flags that will
188 // be passed to the shim program.
189 flags []string
Adam Langley95c29f32014-06-20 12:00:00 -0700190}
191
David Benjamin025b3d32014-07-01 19:53:04 -0400192var testCases = []testCase{
Adam Langley95c29f32014-06-20 12:00:00 -0700193 {
194 name: "BadRSASignature",
195 config: Config{
196 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
197 Bugs: ProtocolBugs{
198 InvalidSKXSignature: true,
199 },
200 },
201 shouldFail: true,
202 expectedError: ":BAD_SIGNATURE:",
203 },
204 {
205 name: "BadECDSASignature",
206 config: Config{
207 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
208 Bugs: ProtocolBugs{
209 InvalidSKXSignature: true,
210 },
211 Certificates: []Certificate{getECDSACertificate()},
212 },
213 shouldFail: true,
214 expectedError: ":BAD_SIGNATURE:",
215 },
216 {
217 name: "BadECDSACurve",
218 config: Config{
219 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
220 Bugs: ProtocolBugs{
221 InvalidSKXCurve: true,
222 },
223 Certificates: []Certificate{getECDSACertificate()},
224 },
225 shouldFail: true,
226 expectedError: ":WRONG_CURVE:",
227 },
Adam Langleyac61fa32014-06-23 12:03:11 -0700228 {
David Benjamina8e3e0e2014-08-06 22:11:10 -0400229 testType: serverTest,
230 name: "BadRSAVersion",
231 config: Config{
232 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
233 Bugs: ProtocolBugs{
234 RsaClientKeyExchangeVersion: VersionTLS11,
235 },
236 },
237 shouldFail: true,
238 expectedError: ":DECRYPTION_FAILED_OR_BAD_RECORD_MAC:",
239 },
240 {
David Benjamin325b5c32014-07-01 19:40:31 -0400241 name: "NoFallbackSCSV",
Adam Langleyac61fa32014-06-23 12:03:11 -0700242 config: Config{
243 Bugs: ProtocolBugs{
244 FailIfNotFallbackSCSV: true,
245 },
246 },
247 shouldFail: true,
248 expectedLocalError: "no fallback SCSV found",
249 },
David Benjamin325b5c32014-07-01 19:40:31 -0400250 {
David Benjamin2a0c4962014-08-22 23:46:35 -0400251 name: "SendFallbackSCSV",
David Benjamin325b5c32014-07-01 19:40:31 -0400252 config: Config{
253 Bugs: ProtocolBugs{
254 FailIfNotFallbackSCSV: true,
255 },
256 },
257 flags: []string{"-fallback-scsv"},
258 },
David Benjamin197b3ab2014-07-02 18:37:33 -0400259 {
David Benjamin7b030512014-07-08 17:30:11 -0400260 name: "ClientCertificateTypes",
261 config: Config{
262 ClientAuth: RequestClientCert,
263 ClientCertificateTypes: []byte{
264 CertTypeDSSSign,
265 CertTypeRSASign,
266 CertTypeECDSASign,
267 },
268 },
David Benjamin2561dc32014-08-24 01:25:27 -0400269 flags: []string{
270 "-expect-certificate-types",
271 base64.StdEncoding.EncodeToString([]byte{
272 CertTypeDSSSign,
273 CertTypeRSASign,
274 CertTypeECDSASign,
275 }),
276 },
David Benjamin7b030512014-07-08 17:30:11 -0400277 },
David Benjamin636293b2014-07-08 17:59:18 -0400278 {
279 name: "NoClientCertificate",
280 config: Config{
281 ClientAuth: RequireAnyClientCert,
282 },
283 shouldFail: true,
284 expectedLocalError: "client didn't provide a certificate",
285 },
David Benjamin1c375dd2014-07-12 00:48:23 -0400286 {
287 name: "UnauthenticatedECDH",
288 config: Config{
289 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
290 Bugs: ProtocolBugs{
291 UnauthenticatedECDH: true,
292 },
293 },
294 shouldFail: true,
David Benjamine8f3d662014-07-12 01:10:19 -0400295 expectedError: ":UNEXPECTED_MESSAGE:",
David Benjamin1c375dd2014-07-12 00:48:23 -0400296 },
David Benjamin9c651c92014-07-12 13:27:45 -0400297 {
298 name: "SkipServerKeyExchange",
299 config: Config{
300 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
301 Bugs: ProtocolBugs{
302 SkipServerKeyExchange: true,
303 },
304 },
305 shouldFail: true,
306 expectedError: ":UNEXPECTED_MESSAGE:",
307 },
David Benjamin1f5f62b2014-07-12 16:18:02 -0400308 {
David Benjamina0e52232014-07-19 17:39:58 -0400309 name: "SkipChangeCipherSpec-Client",
310 config: Config{
311 Bugs: ProtocolBugs{
312 SkipChangeCipherSpec: true,
313 },
314 },
315 shouldFail: true,
David Benjamin86271ee2014-07-21 16:14:03 -0400316 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
David Benjamina0e52232014-07-19 17:39:58 -0400317 },
318 {
319 testType: serverTest,
320 name: "SkipChangeCipherSpec-Server",
321 config: Config{
322 Bugs: ProtocolBugs{
323 SkipChangeCipherSpec: true,
324 },
325 },
326 shouldFail: true,
David Benjamin86271ee2014-07-21 16:14:03 -0400327 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
David Benjamina0e52232014-07-19 17:39:58 -0400328 },
David Benjamin42be6452014-07-21 14:50:23 -0400329 {
330 testType: serverTest,
331 name: "SkipChangeCipherSpec-Server-NPN",
332 config: Config{
333 NextProtos: []string{"bar"},
334 Bugs: ProtocolBugs{
335 SkipChangeCipherSpec: true,
336 },
337 },
338 flags: []string{
339 "-advertise-npn", "\x03foo\x03bar\x03baz",
340 },
341 shouldFail: true,
David Benjamin86271ee2014-07-21 16:14:03 -0400342 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
343 },
344 {
345 name: "FragmentAcrossChangeCipherSpec-Client",
346 config: Config{
347 Bugs: ProtocolBugs{
348 FragmentAcrossChangeCipherSpec: true,
349 },
350 },
351 shouldFail: true,
352 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
353 },
354 {
355 testType: serverTest,
356 name: "FragmentAcrossChangeCipherSpec-Server",
357 config: Config{
358 Bugs: ProtocolBugs{
359 FragmentAcrossChangeCipherSpec: true,
360 },
361 },
362 shouldFail: true,
363 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
364 },
365 {
366 testType: serverTest,
367 name: "FragmentAcrossChangeCipherSpec-Server-NPN",
368 config: Config{
369 NextProtos: []string{"bar"},
370 Bugs: ProtocolBugs{
371 FragmentAcrossChangeCipherSpec: true,
372 },
373 },
374 flags: []string{
375 "-advertise-npn", "\x03foo\x03bar\x03baz",
376 },
377 shouldFail: true,
378 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
David Benjamin42be6452014-07-21 14:50:23 -0400379 },
David Benjaminf3ec83d2014-07-21 22:42:34 -0400380 {
381 testType: serverTest,
David Benjamin3fd1fbd2015-02-03 16:07:32 -0500382 name: "Alert",
383 config: Config{
384 Bugs: ProtocolBugs{
385 SendSpuriousAlert: alertRecordOverflow,
386 },
387 },
388 shouldFail: true,
389 expectedError: ":TLSV1_ALERT_RECORD_OVERFLOW:",
390 },
391 {
392 protocol: dtls,
393 testType: serverTest,
394 name: "Alert-DTLS",
395 config: Config{
396 Bugs: ProtocolBugs{
397 SendSpuriousAlert: alertRecordOverflow,
398 },
399 },
400 shouldFail: true,
401 expectedError: ":TLSV1_ALERT_RECORD_OVERFLOW:",
402 },
403 {
404 testType: serverTest,
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400405 name: "FragmentAlert",
406 config: Config{
407 Bugs: ProtocolBugs{
David Benjaminca6c8262014-11-15 19:06:08 -0500408 FragmentAlert: true,
David Benjamin3fd1fbd2015-02-03 16:07:32 -0500409 SendSpuriousAlert: alertRecordOverflow,
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400410 },
411 },
412 shouldFail: true,
413 expectedError: ":BAD_ALERT:",
414 },
415 {
David Benjamin0ea8dda2015-01-31 20:33:40 -0500416 protocol: dtls,
417 testType: serverTest,
418 name: "FragmentAlert-DTLS",
419 config: Config{
420 Bugs: ProtocolBugs{
421 FragmentAlert: true,
David Benjamin3fd1fbd2015-02-03 16:07:32 -0500422 SendSpuriousAlert: alertRecordOverflow,
David Benjamin0ea8dda2015-01-31 20:33:40 -0500423 },
424 },
425 shouldFail: true,
426 expectedError: ":BAD_ALERT:",
427 },
428 {
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400429 testType: serverTest,
David Benjaminf3ec83d2014-07-21 22:42:34 -0400430 name: "EarlyChangeCipherSpec-server-1",
431 config: Config{
432 Bugs: ProtocolBugs{
433 EarlyChangeCipherSpec: 1,
434 },
435 },
436 shouldFail: true,
437 expectedError: ":CCS_RECEIVED_EARLY:",
438 },
439 {
440 testType: serverTest,
441 name: "EarlyChangeCipherSpec-server-2",
442 config: Config{
443 Bugs: ProtocolBugs{
444 EarlyChangeCipherSpec: 2,
445 },
446 },
447 shouldFail: true,
448 expectedError: ":CCS_RECEIVED_EARLY:",
449 },
David Benjamind23f4122014-07-23 15:09:48 -0400450 {
David Benjamind23f4122014-07-23 15:09:48 -0400451 name: "SkipNewSessionTicket",
452 config: Config{
453 Bugs: ProtocolBugs{
454 SkipNewSessionTicket: true,
455 },
456 },
457 shouldFail: true,
458 expectedError: ":CCS_RECEIVED_EARLY:",
459 },
David Benjamin7e3305e2014-07-28 14:52:32 -0400460 {
David Benjamind86c7672014-08-02 04:07:12 -0400461 testType: serverTest,
David Benjaminbef270a2014-08-02 04:22:02 -0400462 name: "FallbackSCSV",
463 config: Config{
464 MaxVersion: VersionTLS11,
465 Bugs: ProtocolBugs{
466 SendFallbackSCSV: true,
467 },
468 },
469 shouldFail: true,
470 expectedError: ":INAPPROPRIATE_FALLBACK:",
471 },
472 {
473 testType: serverTest,
474 name: "FallbackSCSV-VersionMatch",
475 config: Config{
476 Bugs: ProtocolBugs{
477 SendFallbackSCSV: true,
478 },
479 },
480 },
David Benjamin98214542014-08-07 18:02:39 -0400481 {
482 testType: serverTest,
483 name: "FragmentedClientVersion",
484 config: Config{
485 Bugs: ProtocolBugs{
486 MaxHandshakeRecordLength: 1,
487 FragmentClientVersion: true,
488 },
489 },
David Benjamin82c9e902014-12-12 15:55:27 -0500490 expectedVersion: VersionTLS12,
David Benjamin98214542014-08-07 18:02:39 -0400491 },
David Benjamin98e882e2014-08-08 13:24:34 -0400492 {
493 testType: serverTest,
494 name: "MinorVersionTolerance",
495 config: Config{
496 Bugs: ProtocolBugs{
497 SendClientVersion: 0x03ff,
498 },
499 },
500 expectedVersion: VersionTLS12,
501 },
502 {
503 testType: serverTest,
504 name: "MajorVersionTolerance",
505 config: Config{
506 Bugs: ProtocolBugs{
507 SendClientVersion: 0x0400,
508 },
509 },
510 expectedVersion: VersionTLS12,
511 },
512 {
513 testType: serverTest,
514 name: "VersionTooLow",
515 config: Config{
516 Bugs: ProtocolBugs{
517 SendClientVersion: 0x0200,
518 },
519 },
520 shouldFail: true,
521 expectedError: ":UNSUPPORTED_PROTOCOL:",
522 },
523 {
524 testType: serverTest,
525 name: "HttpGET",
526 sendPrefix: "GET / HTTP/1.0\n",
527 shouldFail: true,
528 expectedError: ":HTTP_REQUEST:",
529 },
530 {
531 testType: serverTest,
532 name: "HttpPOST",
533 sendPrefix: "POST / HTTP/1.0\n",
534 shouldFail: true,
535 expectedError: ":HTTP_REQUEST:",
536 },
537 {
538 testType: serverTest,
539 name: "HttpHEAD",
540 sendPrefix: "HEAD / HTTP/1.0\n",
541 shouldFail: true,
542 expectedError: ":HTTP_REQUEST:",
543 },
544 {
545 testType: serverTest,
546 name: "HttpPUT",
547 sendPrefix: "PUT / HTTP/1.0\n",
548 shouldFail: true,
549 expectedError: ":HTTP_REQUEST:",
550 },
551 {
552 testType: serverTest,
553 name: "HttpCONNECT",
554 sendPrefix: "CONNECT www.google.com:443 HTTP/1.0\n",
555 shouldFail: true,
556 expectedError: ":HTTPS_PROXY_REQUEST:",
557 },
David Benjamin39ebf532014-08-31 02:23:49 -0400558 {
David Benjaminf080ecd2014-12-11 18:48:59 -0500559 testType: serverTest,
560 name: "Garbage",
561 sendPrefix: "blah",
562 shouldFail: true,
563 expectedError: ":UNKNOWN_PROTOCOL:",
564 },
565 {
David Benjamin39ebf532014-08-31 02:23:49 -0400566 name: "SkipCipherVersionCheck",
567 config: Config{
568 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
569 MaxVersion: VersionTLS11,
570 Bugs: ProtocolBugs{
571 SkipCipherVersionCheck: true,
572 },
573 },
574 shouldFail: true,
575 expectedError: ":WRONG_CIPHER_RETURNED:",
576 },
David Benjamin9114fae2014-11-08 11:41:14 -0500577 {
David Benjamina3e89492015-02-26 15:16:22 -0500578 name: "RSAEphemeralKey",
David Benjamin9114fae2014-11-08 11:41:14 -0500579 config: Config{
580 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
581 Bugs: ProtocolBugs{
David Benjamina3e89492015-02-26 15:16:22 -0500582 RSAEphemeralKey: true,
David Benjamin9114fae2014-11-08 11:41:14 -0500583 },
584 },
585 shouldFail: true,
586 expectedError: ":UNEXPECTED_MESSAGE:",
587 },
David Benjamin128dbc32014-12-01 01:27:42 -0500588 {
589 name: "DisableEverything",
590 flags: []string{"-no-tls12", "-no-tls11", "-no-tls1", "-no-ssl3"},
591 shouldFail: true,
592 expectedError: ":WRONG_SSL_VERSION:",
593 },
594 {
595 protocol: dtls,
596 name: "DisableEverything-DTLS",
597 flags: []string{"-no-tls12", "-no-tls1"},
598 shouldFail: true,
599 expectedError: ":WRONG_SSL_VERSION:",
600 },
David Benjamin780d6dd2015-01-06 12:03:19 -0500601 {
602 name: "NoSharedCipher",
603 config: Config{
604 CipherSuites: []uint16{},
605 },
606 shouldFail: true,
607 expectedError: ":HANDSHAKE_FAILURE_ON_CLIENT_HELLO:",
608 },
David Benjamin13be1de2015-01-11 16:29:36 -0500609 {
610 protocol: dtls,
611 testType: serverTest,
612 name: "MTU",
613 config: Config{
614 Bugs: ProtocolBugs{
615 MaxPacketLength: 256,
616 },
617 },
618 flags: []string{"-mtu", "256"},
619 },
620 {
621 protocol: dtls,
622 testType: serverTest,
623 name: "MTUExceeded",
624 config: Config{
625 Bugs: ProtocolBugs{
626 MaxPacketLength: 255,
627 },
628 },
629 flags: []string{"-mtu", "256"},
630 shouldFail: true,
631 expectedLocalError: "dtls: exceeded maximum packet length",
632 },
David Benjamin6095de82014-12-27 01:50:38 -0500633 {
634 name: "CertMismatchRSA",
635 config: Config{
636 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
637 Certificates: []Certificate{getECDSACertificate()},
638 Bugs: ProtocolBugs{
639 SendCipherSuite: TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
640 },
641 },
642 shouldFail: true,
643 expectedError: ":WRONG_CERTIFICATE_TYPE:",
644 },
645 {
646 name: "CertMismatchECDSA",
647 config: Config{
648 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
649 Certificates: []Certificate{getRSACertificate()},
650 Bugs: ProtocolBugs{
651 SendCipherSuite: TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
652 },
653 },
654 shouldFail: true,
655 expectedError: ":WRONG_CERTIFICATE_TYPE:",
656 },
David Benjamin5fa3eba2015-01-22 16:35:40 -0500657 {
658 name: "TLSFatalBadPackets",
659 damageFirstWrite: true,
660 shouldFail: true,
661 expectedError: ":DECRYPTION_FAILED_OR_BAD_RECORD_MAC:",
662 },
663 {
664 protocol: dtls,
665 name: "DTLSIgnoreBadPackets",
666 damageFirstWrite: true,
667 },
668 {
669 protocol: dtls,
670 name: "DTLSIgnoreBadPackets-Async",
671 damageFirstWrite: true,
672 flags: []string{"-async"},
673 },
David Benjamin4189bd92015-01-25 23:52:39 -0500674 {
675 name: "AppDataAfterChangeCipherSpec",
676 config: Config{
677 Bugs: ProtocolBugs{
678 AppDataAfterChangeCipherSpec: []byte("TEST MESSAGE"),
679 },
680 },
681 shouldFail: true,
682 expectedError: ":DATA_BETWEEN_CCS_AND_FINISHED:",
683 },
684 {
685 protocol: dtls,
686 name: "AppDataAfterChangeCipherSpec-DTLS",
687 config: Config{
688 Bugs: ProtocolBugs{
689 AppDataAfterChangeCipherSpec: []byte("TEST MESSAGE"),
690 },
691 },
692 },
David Benjaminb3774b92015-01-31 17:16:01 -0500693 {
David Benjamindc3da932015-03-12 15:09:02 -0400694 name: "AlertAfterChangeCipherSpec",
695 config: Config{
696 Bugs: ProtocolBugs{
697 AlertAfterChangeCipherSpec: alertRecordOverflow,
698 },
699 },
700 shouldFail: true,
701 expectedError: ":TLSV1_ALERT_RECORD_OVERFLOW:",
702 },
703 {
704 protocol: dtls,
705 name: "AlertAfterChangeCipherSpec-DTLS",
706 config: Config{
707 Bugs: ProtocolBugs{
708 AlertAfterChangeCipherSpec: alertRecordOverflow,
709 },
710 },
711 shouldFail: true,
712 expectedError: ":TLSV1_ALERT_RECORD_OVERFLOW:",
713 },
714 {
David Benjaminb3774b92015-01-31 17:16:01 -0500715 protocol: dtls,
716 name: "ReorderHandshakeFragments-Small-DTLS",
717 config: Config{
718 Bugs: ProtocolBugs{
719 ReorderHandshakeFragments: true,
720 // Small enough that every handshake message is
721 // fragmented.
722 MaxHandshakeRecordLength: 2,
723 },
724 },
725 },
726 {
727 protocol: dtls,
728 name: "ReorderHandshakeFragments-Large-DTLS",
729 config: Config{
730 Bugs: ProtocolBugs{
731 ReorderHandshakeFragments: true,
732 // Large enough that no handshake message is
733 // fragmented.
David Benjaminb3774b92015-01-31 17:16:01 -0500734 MaxHandshakeRecordLength: 2048,
735 },
736 },
737 },
David Benjaminddb9f152015-02-03 15:44:39 -0500738 {
David Benjamin75381222015-03-02 19:30:30 -0500739 protocol: dtls,
740 name: "MixCompleteMessageWithFragments-DTLS",
741 config: Config{
742 Bugs: ProtocolBugs{
743 ReorderHandshakeFragments: true,
744 MixCompleteMessageWithFragments: true,
745 MaxHandshakeRecordLength: 2,
746 },
747 },
748 },
749 {
David Benjaminddb9f152015-02-03 15:44:39 -0500750 name: "SendInvalidRecordType",
751 config: Config{
752 Bugs: ProtocolBugs{
753 SendInvalidRecordType: true,
754 },
755 },
756 shouldFail: true,
757 expectedError: ":UNEXPECTED_RECORD:",
758 },
759 {
760 protocol: dtls,
761 name: "SendInvalidRecordType-DTLS",
762 config: Config{
763 Bugs: ProtocolBugs{
764 SendInvalidRecordType: true,
765 },
766 },
767 shouldFail: true,
768 expectedError: ":UNEXPECTED_RECORD:",
769 },
David Benjaminb80168e2015-02-08 18:30:14 -0500770 {
771 name: "FalseStart-SkipServerSecondLeg",
772 config: Config{
773 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
774 NextProtos: []string{"foo"},
775 Bugs: ProtocolBugs{
776 SkipNewSessionTicket: true,
777 SkipChangeCipherSpec: true,
778 SkipFinished: true,
779 ExpectFalseStart: true,
780 },
781 },
782 flags: []string{
783 "-false-start",
David Benjamin87e4acd2015-04-02 19:57:35 -0400784 "-handshake-never-done",
David Benjaminb80168e2015-02-08 18:30:14 -0500785 "-advertise-alpn", "\x03foo",
786 },
787 shimWritesFirst: true,
788 shouldFail: true,
789 expectedError: ":UNEXPECTED_RECORD:",
790 },
David Benjamin931ab342015-02-08 19:46:57 -0500791 {
792 name: "FalseStart-SkipServerSecondLeg-Implicit",
793 config: Config{
794 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
795 NextProtos: []string{"foo"},
796 Bugs: ProtocolBugs{
797 SkipNewSessionTicket: true,
798 SkipChangeCipherSpec: true,
799 SkipFinished: true,
800 },
801 },
802 flags: []string{
803 "-implicit-handshake",
804 "-false-start",
David Benjamin87e4acd2015-04-02 19:57:35 -0400805 "-handshake-never-done",
David Benjamin931ab342015-02-08 19:46:57 -0500806 "-advertise-alpn", "\x03foo",
807 },
808 shouldFail: true,
809 expectedError: ":UNEXPECTED_RECORD:",
810 },
David Benjamin6f5c0f42015-02-24 01:23:21 -0500811 {
812 testType: serverTest,
813 name: "FailEarlyCallback",
814 flags: []string{"-fail-early-callback"},
815 shouldFail: true,
816 expectedError: ":CONNECTION_REJECTED:",
817 expectedLocalError: "remote error: access denied",
818 },
David Benjaminbcb2d912015-02-24 23:45:43 -0500819 {
820 name: "WrongMessageType",
821 config: Config{
822 Bugs: ProtocolBugs{
823 WrongCertificateMessageType: true,
824 },
825 },
826 shouldFail: true,
827 expectedError: ":UNEXPECTED_MESSAGE:",
828 expectedLocalError: "remote error: unexpected message",
829 },
830 {
831 protocol: dtls,
832 name: "WrongMessageType-DTLS",
833 config: Config{
834 Bugs: ProtocolBugs{
835 WrongCertificateMessageType: true,
836 },
837 },
838 shouldFail: true,
839 expectedError: ":UNEXPECTED_MESSAGE:",
840 expectedLocalError: "remote error: unexpected message",
841 },
David Benjamin75381222015-03-02 19:30:30 -0500842 {
843 protocol: dtls,
844 name: "FragmentMessageTypeMismatch-DTLS",
845 config: Config{
846 Bugs: ProtocolBugs{
847 MaxHandshakeRecordLength: 2,
848 FragmentMessageTypeMismatch: true,
849 },
850 },
851 shouldFail: true,
852 expectedError: ":FRAGMENT_MISMATCH:",
853 },
854 {
855 protocol: dtls,
856 name: "FragmentMessageLengthMismatch-DTLS",
857 config: Config{
858 Bugs: ProtocolBugs{
859 MaxHandshakeRecordLength: 2,
860 FragmentMessageLengthMismatch: true,
861 },
862 },
863 shouldFail: true,
864 expectedError: ":FRAGMENT_MISMATCH:",
865 },
866 {
867 protocol: dtls,
868 name: "SplitFragmentHeader-DTLS",
869 config: Config{
870 Bugs: ProtocolBugs{
871 SplitFragmentHeader: true,
872 },
873 },
874 shouldFail: true,
875 expectedError: ":UNEXPECTED_MESSAGE:",
876 },
877 {
878 protocol: dtls,
879 name: "SplitFragmentBody-DTLS",
880 config: Config{
881 Bugs: ProtocolBugs{
882 SplitFragmentBody: true,
883 },
884 },
885 shouldFail: true,
886 expectedError: ":UNEXPECTED_MESSAGE:",
887 },
888 {
889 protocol: dtls,
890 name: "SendEmptyFragments-DTLS",
891 config: Config{
892 Bugs: ProtocolBugs{
893 SendEmptyFragments: true,
894 },
895 },
896 },
David Benjamin67d1fb52015-03-16 15:16:23 -0400897 {
898 name: "UnsupportedCipherSuite",
899 config: Config{
900 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
901 Bugs: ProtocolBugs{
902 IgnorePeerCipherPreferences: true,
903 },
904 },
905 flags: []string{"-cipher", "DEFAULT:!RC4"},
906 shouldFail: true,
907 expectedError: ":WRONG_CIPHER_RETURNED:",
908 },
David Benjamin340d5ed2015-03-21 02:21:37 -0400909 {
910 name: "SendWarningAlerts",
911 config: Config{
912 Bugs: ProtocolBugs{
913 SendWarningAlerts: alertAccessDenied,
914 },
915 },
916 },
917 {
918 protocol: dtls,
919 name: "SendWarningAlerts-DTLS",
920 config: Config{
921 Bugs: ProtocolBugs{
922 SendWarningAlerts: alertAccessDenied,
923 },
924 },
925 },
David Benjamin513f0ea2015-04-02 19:33:31 -0400926 {
927 name: "BadFinished",
928 config: Config{
929 Bugs: ProtocolBugs{
930 BadFinished: true,
931 },
932 },
933 shouldFail: true,
934 expectedError: ":DIGEST_CHECK_FAILED:",
935 },
936 {
937 name: "FalseStart-BadFinished",
938 config: Config{
939 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
940 NextProtos: []string{"foo"},
941 Bugs: ProtocolBugs{
942 BadFinished: true,
943 ExpectFalseStart: true,
944 },
945 },
946 flags: []string{
947 "-false-start",
David Benjamin87e4acd2015-04-02 19:57:35 -0400948 "-handshake-never-done",
David Benjamin513f0ea2015-04-02 19:33:31 -0400949 "-advertise-alpn", "\x03foo",
950 },
951 shimWritesFirst: true,
952 shouldFail: true,
953 expectedError: ":DIGEST_CHECK_FAILED:",
954 },
Adam Langley95c29f32014-06-20 12:00:00 -0700955}
956
David Benjamin01fe8202014-09-24 15:21:44 -0400957func doExchange(test *testCase, config *Config, conn net.Conn, messageLen int, isResume bool) error {
David Benjamin65ea8ff2014-11-23 03:01:00 -0500958 var connDebug *recordingConn
David Benjamin5fa3eba2015-01-22 16:35:40 -0500959 var connDamage *damageAdaptor
David Benjamin65ea8ff2014-11-23 03:01:00 -0500960 if *flagDebug {
961 connDebug = &recordingConn{Conn: conn}
962 conn = connDebug
963 defer func() {
964 connDebug.WriteTo(os.Stdout)
965 }()
966 }
967
David Benjamin6fd297b2014-08-11 18:43:38 -0400968 if test.protocol == dtls {
David Benjamin83f90402015-01-27 01:09:43 -0500969 config.Bugs.PacketAdaptor = newPacketAdaptor(conn)
970 conn = config.Bugs.PacketAdaptor
David Benjamin5e961c12014-11-07 01:48:35 -0500971 if test.replayWrites {
972 conn = newReplayAdaptor(conn)
973 }
David Benjamin6fd297b2014-08-11 18:43:38 -0400974 }
975
David Benjamin5fa3eba2015-01-22 16:35:40 -0500976 if test.damageFirstWrite {
977 connDamage = newDamageAdaptor(conn)
978 conn = connDamage
979 }
980
David Benjamin6fd297b2014-08-11 18:43:38 -0400981 if test.sendPrefix != "" {
982 if _, err := conn.Write([]byte(test.sendPrefix)); err != nil {
983 return err
984 }
David Benjamin98e882e2014-08-08 13:24:34 -0400985 }
986
David Benjamin1d5c83e2014-07-22 19:20:02 -0400987 var tlsConn *Conn
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400988 if test.testType == clientTest {
David Benjamin6fd297b2014-08-11 18:43:38 -0400989 if test.protocol == dtls {
990 tlsConn = DTLSServer(conn, config)
991 } else {
992 tlsConn = Server(conn, config)
993 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400994 } else {
995 config.InsecureSkipVerify = true
David Benjamin6fd297b2014-08-11 18:43:38 -0400996 if test.protocol == dtls {
997 tlsConn = DTLSClient(conn, config)
998 } else {
999 tlsConn = Client(conn, config)
1000 }
David Benjamin1d5c83e2014-07-22 19:20:02 -04001001 }
1002
Adam Langley95c29f32014-06-20 12:00:00 -07001003 if err := tlsConn.Handshake(); err != nil {
1004 return err
1005 }
Kenny Root7fdeaf12014-08-05 15:23:37 -07001006
David Benjamin01fe8202014-09-24 15:21:44 -04001007 // TODO(davidben): move all per-connection expectations into a dedicated
1008 // expectations struct that can be specified separately for the two
1009 // legs.
1010 expectedVersion := test.expectedVersion
1011 if isResume && test.expectedResumeVersion != 0 {
1012 expectedVersion = test.expectedResumeVersion
1013 }
1014 if vers := tlsConn.ConnectionState().Version; expectedVersion != 0 && vers != expectedVersion {
1015 return fmt.Errorf("got version %x, expected %x", vers, expectedVersion)
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001016 }
1017
David Benjamina08e49d2014-08-24 01:46:07 -04001018 if test.expectChannelID {
1019 channelID := tlsConn.ConnectionState().ChannelID
1020 if channelID == nil {
1021 return fmt.Errorf("no channel ID negotiated")
1022 }
1023 if channelID.Curve != channelIDKey.Curve ||
1024 channelIDKey.X.Cmp(channelIDKey.X) != 0 ||
1025 channelIDKey.Y.Cmp(channelIDKey.Y) != 0 {
1026 return fmt.Errorf("incorrect channel ID")
1027 }
1028 }
1029
David Benjaminae2888f2014-09-06 12:58:58 -04001030 if expected := test.expectedNextProto; expected != "" {
1031 if actual := tlsConn.ConnectionState().NegotiatedProtocol; actual != expected {
1032 return fmt.Errorf("next proto mismatch: got %s, wanted %s", actual, expected)
1033 }
1034 }
1035
David Benjaminfc7b0862014-09-06 13:21:53 -04001036 if test.expectedNextProtoType != 0 {
1037 if (test.expectedNextProtoType == alpn) != tlsConn.ConnectionState().NegotiatedProtocolFromALPN {
1038 return fmt.Errorf("next proto type mismatch")
1039 }
1040 }
1041
David Benjaminca6c8262014-11-15 19:06:08 -05001042 if p := tlsConn.ConnectionState().SRTPProtectionProfile; p != test.expectedSRTPProtectionProfile {
1043 return fmt.Errorf("SRTP profile mismatch: got %d, wanted %d", p, test.expectedSRTPProtectionProfile)
1044 }
1045
David Benjamine58c4f52014-08-24 03:47:07 -04001046 if test.shimWritesFirst {
1047 var buf [5]byte
1048 _, err := io.ReadFull(tlsConn, buf[:])
1049 if err != nil {
1050 return err
1051 }
1052 if string(buf[:]) != "hello" {
1053 return fmt.Errorf("bad initial message")
1054 }
1055 }
1056
Adam Langleycf2d4f42014-10-28 19:06:14 -07001057 if test.renegotiate {
1058 if test.renegotiateCiphers != nil {
1059 config.CipherSuites = test.renegotiateCiphers
1060 }
1061 if err := tlsConn.Renegotiate(); err != nil {
1062 return err
1063 }
1064 } else if test.renegotiateCiphers != nil {
1065 panic("renegotiateCiphers without renegotiate")
1066 }
1067
David Benjamin5fa3eba2015-01-22 16:35:40 -05001068 if test.damageFirstWrite {
1069 connDamage.setDamage(true)
1070 tlsConn.Write([]byte("DAMAGED WRITE"))
1071 connDamage.setDamage(false)
1072 }
1073
Kenny Root7fdeaf12014-08-05 15:23:37 -07001074 if messageLen < 0 {
David Benjamin6fd297b2014-08-11 18:43:38 -04001075 if test.protocol == dtls {
1076 return fmt.Errorf("messageLen < 0 not supported for DTLS tests")
1077 }
Kenny Root7fdeaf12014-08-05 15:23:37 -07001078 // Read until EOF.
1079 _, err := io.Copy(ioutil.Discard, tlsConn)
1080 return err
1081 }
1082
David Benjamin4189bd92015-01-25 23:52:39 -05001083 var testMessage []byte
1084 if config.Bugs.AppDataAfterChangeCipherSpec != nil {
1085 // We've already sent a message. Expect the shim to echo it
1086 // back.
1087 testMessage = config.Bugs.AppDataAfterChangeCipherSpec
1088 } else {
1089 if messageLen == 0 {
1090 messageLen = 32
1091 }
1092 testMessage = make([]byte, messageLen)
1093 for i := range testMessage {
1094 testMessage[i] = 0x42
1095 }
1096 tlsConn.Write(testMessage)
Adam Langley80842bd2014-06-20 12:00:00 -07001097 }
Adam Langley95c29f32014-06-20 12:00:00 -07001098
1099 buf := make([]byte, len(testMessage))
David Benjamin6fd297b2014-08-11 18:43:38 -04001100 if test.protocol == dtls {
1101 bufTmp := make([]byte, len(buf)+1)
1102 n, err := tlsConn.Read(bufTmp)
1103 if err != nil {
1104 return err
1105 }
1106 if n != len(buf) {
1107 return fmt.Errorf("bad reply; length mismatch (%d vs %d)", n, len(buf))
1108 }
1109 copy(buf, bufTmp)
1110 } else {
1111 _, err := io.ReadFull(tlsConn, buf)
1112 if err != nil {
1113 return err
1114 }
Adam Langley95c29f32014-06-20 12:00:00 -07001115 }
1116
1117 for i, v := range buf {
1118 if v != testMessage[i]^0xff {
1119 return fmt.Errorf("bad reply contents at byte %d", i)
1120 }
1121 }
1122
1123 return nil
1124}
1125
David Benjamin325b5c32014-07-01 19:40:31 -04001126func valgrindOf(dbAttach bool, path string, args ...string) *exec.Cmd {
1127 valgrindArgs := []string{"--error-exitcode=99", "--track-origins=yes", "--leak-check=full"}
Adam Langley95c29f32014-06-20 12:00:00 -07001128 if dbAttach {
David Benjamin325b5c32014-07-01 19:40:31 -04001129 valgrindArgs = append(valgrindArgs, "--db-attach=yes", "--db-command=xterm -e gdb -nw %f %p")
Adam Langley95c29f32014-06-20 12:00:00 -07001130 }
David Benjamin325b5c32014-07-01 19:40:31 -04001131 valgrindArgs = append(valgrindArgs, path)
1132 valgrindArgs = append(valgrindArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -07001133
David Benjamin325b5c32014-07-01 19:40:31 -04001134 return exec.Command("valgrind", valgrindArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -07001135}
1136
David Benjamin325b5c32014-07-01 19:40:31 -04001137func gdbOf(path string, args ...string) *exec.Cmd {
1138 xtermArgs := []string{"-e", "gdb", "--args"}
1139 xtermArgs = append(xtermArgs, path)
1140 xtermArgs = append(xtermArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -07001141
David Benjamin325b5c32014-07-01 19:40:31 -04001142 return exec.Command("xterm", xtermArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -07001143}
1144
Adam Langley69a01602014-11-17 17:26:55 -08001145type moreMallocsError struct{}
1146
1147func (moreMallocsError) Error() string {
1148 return "child process did not exhaust all allocation calls"
1149}
1150
1151var errMoreMallocs = moreMallocsError{}
1152
David Benjamin87c8a642015-02-21 01:54:29 -05001153// accept accepts a connection from listener, unless waitChan signals a process
1154// exit first.
1155func acceptOrWait(listener net.Listener, waitChan chan error) (net.Conn, error) {
1156 type connOrError struct {
1157 conn net.Conn
1158 err error
1159 }
1160 connChan := make(chan connOrError, 1)
1161 go func() {
1162 conn, err := listener.Accept()
1163 connChan <- connOrError{conn, err}
1164 close(connChan)
1165 }()
1166 select {
1167 case result := <-connChan:
1168 return result.conn, result.err
1169 case childErr := <-waitChan:
1170 waitChan <- childErr
1171 return nil, fmt.Errorf("child exited early: %s", childErr)
1172 }
1173}
1174
Adam Langley69a01602014-11-17 17:26:55 -08001175func runTest(test *testCase, buildDir string, mallocNumToFail int64) error {
Adam Langley38311732014-10-16 19:04:35 -07001176 if !test.shouldFail && (len(test.expectedError) > 0 || len(test.expectedLocalError) > 0) {
1177 panic("Error expected without shouldFail in " + test.name)
1178 }
1179
David Benjamin87c8a642015-02-21 01:54:29 -05001180 listener, err := net.ListenTCP("tcp4", &net.TCPAddr{IP: net.IP{127, 0, 0, 1}})
1181 if err != nil {
1182 panic(err)
1183 }
1184 defer func() {
1185 if listener != nil {
1186 listener.Close()
1187 }
1188 }()
Adam Langley95c29f32014-06-20 12:00:00 -07001189
David Benjamin884fdf12014-08-02 15:28:23 -04001190 shim_path := path.Join(buildDir, "ssl/test/bssl_shim")
David Benjamin87c8a642015-02-21 01:54:29 -05001191 flags := []string{"-port", strconv.Itoa(listener.Addr().(*net.TCPAddr).Port)}
David Benjamin1d5c83e2014-07-22 19:20:02 -04001192 if test.testType == serverTest {
David Benjamin5a593af2014-08-11 19:51:50 -04001193 flags = append(flags, "-server")
1194
David Benjamin025b3d32014-07-01 19:53:04 -04001195 flags = append(flags, "-key-file")
1196 if test.keyFile == "" {
1197 flags = append(flags, rsaKeyFile)
1198 } else {
1199 flags = append(flags, test.keyFile)
1200 }
1201
1202 flags = append(flags, "-cert-file")
1203 if test.certFile == "" {
1204 flags = append(flags, rsaCertificateFile)
1205 } else {
1206 flags = append(flags, test.certFile)
1207 }
1208 }
David Benjamin5a593af2014-08-11 19:51:50 -04001209
David Benjamin6fd297b2014-08-11 18:43:38 -04001210 if test.protocol == dtls {
1211 flags = append(flags, "-dtls")
1212 }
1213
David Benjamin5a593af2014-08-11 19:51:50 -04001214 if test.resumeSession {
1215 flags = append(flags, "-resume")
1216 }
1217
David Benjamine58c4f52014-08-24 03:47:07 -04001218 if test.shimWritesFirst {
1219 flags = append(flags, "-shim-writes-first")
1220 }
1221
David Benjamin025b3d32014-07-01 19:53:04 -04001222 flags = append(flags, test.flags...)
1223
1224 var shim *exec.Cmd
1225 if *useValgrind {
1226 shim = valgrindOf(false, shim_path, flags...)
Adam Langley75712922014-10-10 16:23:43 -07001227 } else if *useGDB {
1228 shim = gdbOf(shim_path, flags...)
David Benjamin025b3d32014-07-01 19:53:04 -04001229 } else {
1230 shim = exec.Command(shim_path, flags...)
1231 }
David Benjamin025b3d32014-07-01 19:53:04 -04001232 shim.Stdin = os.Stdin
1233 var stdoutBuf, stderrBuf bytes.Buffer
1234 shim.Stdout = &stdoutBuf
1235 shim.Stderr = &stderrBuf
Adam Langley69a01602014-11-17 17:26:55 -08001236 if mallocNumToFail >= 0 {
David Benjamin9e128b02015-02-09 13:13:09 -05001237 shim.Env = os.Environ()
1238 shim.Env = append(shim.Env, "MALLOC_NUMBER_TO_FAIL="+strconv.FormatInt(mallocNumToFail, 10))
Adam Langley69a01602014-11-17 17:26:55 -08001239 if *mallocTestDebug {
1240 shim.Env = append(shim.Env, "MALLOC_ABORT_ON_FAIL=1")
1241 }
1242 shim.Env = append(shim.Env, "_MALLOC_CHECK=1")
1243 }
David Benjamin025b3d32014-07-01 19:53:04 -04001244
1245 if err := shim.Start(); err != nil {
Adam Langley95c29f32014-06-20 12:00:00 -07001246 panic(err)
1247 }
David Benjamin87c8a642015-02-21 01:54:29 -05001248 waitChan := make(chan error, 1)
1249 go func() { waitChan <- shim.Wait() }()
Adam Langley95c29f32014-06-20 12:00:00 -07001250
1251 config := test.config
David Benjamin1d5c83e2014-07-22 19:20:02 -04001252 config.ClientSessionCache = NewLRUClientSessionCache(1)
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001253 config.ServerSessionCache = NewLRUServerSessionCache(1)
David Benjamin025b3d32014-07-01 19:53:04 -04001254 if test.testType == clientTest {
1255 if len(config.Certificates) == 0 {
1256 config.Certificates = []Certificate{getRSACertificate()}
1257 }
David Benjamin87c8a642015-02-21 01:54:29 -05001258 } else {
1259 // Supply a ServerName to ensure a constant session cache key,
1260 // rather than falling back to net.Conn.RemoteAddr.
1261 if len(config.ServerName) == 0 {
1262 config.ServerName = "test"
1263 }
David Benjamin025b3d32014-07-01 19:53:04 -04001264 }
Adam Langley95c29f32014-06-20 12:00:00 -07001265
David Benjamin87c8a642015-02-21 01:54:29 -05001266 conn, err := acceptOrWait(listener, waitChan)
1267 if err == nil {
1268 err = doExchange(test, &config, conn, test.messageLen, false /* not a resumption */)
1269 conn.Close()
1270 }
David Benjamin65ea8ff2014-11-23 03:01:00 -05001271
David Benjamin1d5c83e2014-07-22 19:20:02 -04001272 if err == nil && test.resumeSession {
David Benjamin01fe8202014-09-24 15:21:44 -04001273 var resumeConfig Config
1274 if test.resumeConfig != nil {
1275 resumeConfig = *test.resumeConfig
David Benjamin87c8a642015-02-21 01:54:29 -05001276 if len(resumeConfig.ServerName) == 0 {
1277 resumeConfig.ServerName = config.ServerName
1278 }
David Benjamin01fe8202014-09-24 15:21:44 -04001279 if len(resumeConfig.Certificates) == 0 {
1280 resumeConfig.Certificates = []Certificate{getRSACertificate()}
1281 }
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001282 if !test.newSessionsOnResume {
1283 resumeConfig.SessionTicketKey = config.SessionTicketKey
1284 resumeConfig.ClientSessionCache = config.ClientSessionCache
1285 resumeConfig.ServerSessionCache = config.ServerSessionCache
1286 }
David Benjamin01fe8202014-09-24 15:21:44 -04001287 } else {
1288 resumeConfig = config
1289 }
David Benjamin87c8a642015-02-21 01:54:29 -05001290 var connResume net.Conn
1291 connResume, err = acceptOrWait(listener, waitChan)
1292 if err == nil {
1293 err = doExchange(test, &resumeConfig, connResume, test.messageLen, true /* resumption */)
1294 connResume.Close()
1295 }
David Benjamin1d5c83e2014-07-22 19:20:02 -04001296 }
1297
David Benjamin87c8a642015-02-21 01:54:29 -05001298 // Close the listener now. This is to avoid hangs should the shim try to
1299 // open more connections than expected.
1300 listener.Close()
1301 listener = nil
1302
1303 childErr := <-waitChan
Adam Langley69a01602014-11-17 17:26:55 -08001304 if exitError, ok := childErr.(*exec.ExitError); ok {
1305 if exitError.Sys().(syscall.WaitStatus).ExitStatus() == 88 {
1306 return errMoreMallocs
1307 }
1308 }
Adam Langley95c29f32014-06-20 12:00:00 -07001309
1310 stdout := string(stdoutBuf.Bytes())
1311 stderr := string(stderrBuf.Bytes())
1312 failed := err != nil || childErr != nil
1313 correctFailure := len(test.expectedError) == 0 || strings.Contains(stdout, test.expectedError)
Adam Langleyac61fa32014-06-23 12:03:11 -07001314 localError := "none"
1315 if err != nil {
1316 localError = err.Error()
1317 }
1318 if len(test.expectedLocalError) != 0 {
1319 correctFailure = correctFailure && strings.Contains(localError, test.expectedLocalError)
1320 }
Adam Langley95c29f32014-06-20 12:00:00 -07001321
1322 if failed != test.shouldFail || failed && !correctFailure {
Adam Langley95c29f32014-06-20 12:00:00 -07001323 childError := "none"
Adam Langley95c29f32014-06-20 12:00:00 -07001324 if childErr != nil {
1325 childError = childErr.Error()
1326 }
1327
1328 var msg string
1329 switch {
1330 case failed && !test.shouldFail:
1331 msg = "unexpected failure"
1332 case !failed && test.shouldFail:
1333 msg = "unexpected success"
1334 case failed && !correctFailure:
Adam Langleyac61fa32014-06-23 12:03:11 -07001335 msg = "bad error (wanted '" + test.expectedError + "' / '" + test.expectedLocalError + "')"
Adam Langley95c29f32014-06-20 12:00:00 -07001336 default:
1337 panic("internal error")
1338 }
1339
1340 return fmt.Errorf("%s: local error '%s', child error '%s', stdout:\n%s\nstderr:\n%s", msg, localError, childError, string(stdoutBuf.Bytes()), stderr)
1341 }
1342
1343 if !*useValgrind && len(stderr) > 0 {
1344 println(stderr)
1345 }
1346
1347 return nil
1348}
1349
1350var tlsVersions = []struct {
1351 name string
1352 version uint16
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001353 flag string
David Benjamin8b8c0062014-11-23 02:47:52 -05001354 hasDTLS bool
Adam Langley95c29f32014-06-20 12:00:00 -07001355}{
David Benjamin8b8c0062014-11-23 02:47:52 -05001356 {"SSL3", VersionSSL30, "-no-ssl3", false},
1357 {"TLS1", VersionTLS10, "-no-tls1", true},
1358 {"TLS11", VersionTLS11, "-no-tls11", false},
1359 {"TLS12", VersionTLS12, "-no-tls12", true},
Adam Langley95c29f32014-06-20 12:00:00 -07001360}
1361
1362var testCipherSuites = []struct {
1363 name string
1364 id uint16
1365}{
1366 {"3DES-SHA", TLS_RSA_WITH_3DES_EDE_CBC_SHA},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001367 {"AES128-GCM", TLS_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001368 {"AES128-SHA", TLS_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001369 {"AES128-SHA256", TLS_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001370 {"AES256-GCM", TLS_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001371 {"AES256-SHA", TLS_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001372 {"AES256-SHA256", TLS_RSA_WITH_AES_256_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001373 {"DHE-RSA-AES128-GCM", TLS_DHE_RSA_WITH_AES_128_GCM_SHA256},
1374 {"DHE-RSA-AES128-SHA", TLS_DHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001375 {"DHE-RSA-AES128-SHA256", TLS_DHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001376 {"DHE-RSA-AES256-GCM", TLS_DHE_RSA_WITH_AES_256_GCM_SHA384},
1377 {"DHE-RSA-AES256-SHA", TLS_DHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001378 {"DHE-RSA-AES256-SHA256", TLS_DHE_RSA_WITH_AES_256_CBC_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001379 {"ECDHE-ECDSA-AES128-GCM", TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
1380 {"ECDHE-ECDSA-AES128-SHA", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001381 {"ECDHE-ECDSA-AES128-SHA256", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256},
1382 {"ECDHE-ECDSA-AES256-GCM", TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001383 {"ECDHE-ECDSA-AES256-SHA", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001384 {"ECDHE-ECDSA-AES256-SHA384", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001385 {"ECDHE-ECDSA-RC4-SHA", TLS_ECDHE_ECDSA_WITH_RC4_128_SHA},
David Benjamin2af684f2014-10-27 02:23:15 -04001386 {"ECDHE-PSK-WITH-AES-128-GCM-SHA256", TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001387 {"ECDHE-RSA-AES128-GCM", TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001388 {"ECDHE-RSA-AES128-SHA", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001389 {"ECDHE-RSA-AES128-SHA256", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001390 {"ECDHE-RSA-AES256-GCM", TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001391 {"ECDHE-RSA-AES256-SHA", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001392 {"ECDHE-RSA-AES256-SHA384", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001393 {"ECDHE-RSA-RC4-SHA", TLS_ECDHE_RSA_WITH_RC4_128_SHA},
David Benjamin48cae082014-10-27 01:06:24 -04001394 {"PSK-AES128-CBC-SHA", TLS_PSK_WITH_AES_128_CBC_SHA},
1395 {"PSK-AES256-CBC-SHA", TLS_PSK_WITH_AES_256_CBC_SHA},
1396 {"PSK-RC4-SHA", TLS_PSK_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -07001397 {"RC4-MD5", TLS_RSA_WITH_RC4_128_MD5},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001398 {"RC4-SHA", TLS_RSA_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -07001399}
1400
David Benjamin8b8c0062014-11-23 02:47:52 -05001401func hasComponent(suiteName, component string) bool {
1402 return strings.Contains("-"+suiteName+"-", "-"+component+"-")
1403}
1404
David Benjaminf7768e42014-08-31 02:06:47 -04001405func isTLS12Only(suiteName string) bool {
David Benjamin8b8c0062014-11-23 02:47:52 -05001406 return hasComponent(suiteName, "GCM") ||
1407 hasComponent(suiteName, "SHA256") ||
1408 hasComponent(suiteName, "SHA384")
1409}
1410
1411func isDTLSCipher(suiteName string) bool {
David Benjamine95d20d2014-12-23 11:16:01 -05001412 return !hasComponent(suiteName, "RC4")
David Benjaminf7768e42014-08-31 02:06:47 -04001413}
1414
Adam Langley95c29f32014-06-20 12:00:00 -07001415func addCipherSuiteTests() {
1416 for _, suite := range testCipherSuites {
David Benjamin48cae082014-10-27 01:06:24 -04001417 const psk = "12345"
1418 const pskIdentity = "luggage combo"
1419
Adam Langley95c29f32014-06-20 12:00:00 -07001420 var cert Certificate
David Benjamin025b3d32014-07-01 19:53:04 -04001421 var certFile string
1422 var keyFile string
David Benjamin8b8c0062014-11-23 02:47:52 -05001423 if hasComponent(suite.name, "ECDSA") {
Adam Langley95c29f32014-06-20 12:00:00 -07001424 cert = getECDSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -04001425 certFile = ecdsaCertificateFile
1426 keyFile = ecdsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -07001427 } else {
1428 cert = getRSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -04001429 certFile = rsaCertificateFile
1430 keyFile = rsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -07001431 }
1432
David Benjamin48cae082014-10-27 01:06:24 -04001433 var flags []string
David Benjamin8b8c0062014-11-23 02:47:52 -05001434 if hasComponent(suite.name, "PSK") {
David Benjamin48cae082014-10-27 01:06:24 -04001435 flags = append(flags,
1436 "-psk", psk,
1437 "-psk-identity", pskIdentity)
1438 }
1439
Adam Langley95c29f32014-06-20 12:00:00 -07001440 for _, ver := range tlsVersions {
David Benjaminf7768e42014-08-31 02:06:47 -04001441 if ver.version < VersionTLS12 && isTLS12Only(suite.name) {
Adam Langley95c29f32014-06-20 12:00:00 -07001442 continue
1443 }
1444
David Benjamin025b3d32014-07-01 19:53:04 -04001445 testCases = append(testCases, testCase{
1446 testType: clientTest,
1447 name: ver.name + "-" + suite.name + "-client",
Adam Langley95c29f32014-06-20 12:00:00 -07001448 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001449 MinVersion: ver.version,
1450 MaxVersion: ver.version,
1451 CipherSuites: []uint16{suite.id},
1452 Certificates: []Certificate{cert},
1453 PreSharedKey: []byte(psk),
1454 PreSharedKeyIdentity: pskIdentity,
Adam Langley95c29f32014-06-20 12:00:00 -07001455 },
David Benjamin48cae082014-10-27 01:06:24 -04001456 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001457 resumeSession: true,
Adam Langley95c29f32014-06-20 12:00:00 -07001458 })
David Benjamin025b3d32014-07-01 19:53:04 -04001459
David Benjamin76d8abe2014-08-14 16:25:34 -04001460 testCases = append(testCases, testCase{
1461 testType: serverTest,
1462 name: ver.name + "-" + suite.name + "-server",
1463 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001464 MinVersion: ver.version,
1465 MaxVersion: ver.version,
1466 CipherSuites: []uint16{suite.id},
1467 Certificates: []Certificate{cert},
1468 PreSharedKey: []byte(psk),
1469 PreSharedKeyIdentity: pskIdentity,
David Benjamin76d8abe2014-08-14 16:25:34 -04001470 },
1471 certFile: certFile,
1472 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001473 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001474 resumeSession: true,
David Benjamin76d8abe2014-08-14 16:25:34 -04001475 })
David Benjamin6fd297b2014-08-11 18:43:38 -04001476
David Benjamin8b8c0062014-11-23 02:47:52 -05001477 if ver.hasDTLS && isDTLSCipher(suite.name) {
David Benjamin6fd297b2014-08-11 18:43:38 -04001478 testCases = append(testCases, testCase{
1479 testType: clientTest,
1480 protocol: dtls,
1481 name: "D" + ver.name + "-" + suite.name + "-client",
1482 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001483 MinVersion: ver.version,
1484 MaxVersion: ver.version,
1485 CipherSuites: []uint16{suite.id},
1486 Certificates: []Certificate{cert},
1487 PreSharedKey: []byte(psk),
1488 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001489 },
David Benjamin48cae082014-10-27 01:06:24 -04001490 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001491 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001492 })
1493 testCases = append(testCases, testCase{
1494 testType: serverTest,
1495 protocol: dtls,
1496 name: "D" + ver.name + "-" + suite.name + "-server",
1497 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001498 MinVersion: ver.version,
1499 MaxVersion: ver.version,
1500 CipherSuites: []uint16{suite.id},
1501 Certificates: []Certificate{cert},
1502 PreSharedKey: []byte(psk),
1503 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001504 },
1505 certFile: certFile,
1506 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001507 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001508 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001509 })
1510 }
Adam Langley95c29f32014-06-20 12:00:00 -07001511 }
1512 }
1513}
1514
1515func addBadECDSASignatureTests() {
1516 for badR := BadValue(1); badR < NumBadValues; badR++ {
1517 for badS := BadValue(1); badS < NumBadValues; badS++ {
David Benjamin025b3d32014-07-01 19:53:04 -04001518 testCases = append(testCases, testCase{
Adam Langley95c29f32014-06-20 12:00:00 -07001519 name: fmt.Sprintf("BadECDSA-%d-%d", badR, badS),
1520 config: Config{
1521 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
1522 Certificates: []Certificate{getECDSACertificate()},
1523 Bugs: ProtocolBugs{
1524 BadECDSAR: badR,
1525 BadECDSAS: badS,
1526 },
1527 },
1528 shouldFail: true,
1529 expectedError: "SIGNATURE",
1530 })
1531 }
1532 }
1533}
1534
Adam Langley80842bd2014-06-20 12:00:00 -07001535func addCBCPaddingTests() {
David Benjamin025b3d32014-07-01 19:53:04 -04001536 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001537 name: "MaxCBCPadding",
1538 config: Config{
1539 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1540 Bugs: ProtocolBugs{
1541 MaxPadding: true,
1542 },
1543 },
1544 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1545 })
David Benjamin025b3d32014-07-01 19:53:04 -04001546 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001547 name: "BadCBCPadding",
1548 config: Config{
1549 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1550 Bugs: ProtocolBugs{
1551 PaddingFirstByteBad: true,
1552 },
1553 },
1554 shouldFail: true,
1555 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1556 })
1557 // OpenSSL previously had an issue where the first byte of padding in
1558 // 255 bytes of padding wasn't checked.
David Benjamin025b3d32014-07-01 19:53:04 -04001559 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001560 name: "BadCBCPadding255",
1561 config: Config{
1562 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1563 Bugs: ProtocolBugs{
1564 MaxPadding: true,
1565 PaddingFirstByteBadIf255: true,
1566 },
1567 },
1568 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1569 shouldFail: true,
1570 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1571 })
1572}
1573
Kenny Root7fdeaf12014-08-05 15:23:37 -07001574func addCBCSplittingTests() {
1575 testCases = append(testCases, testCase{
1576 name: "CBCRecordSplitting",
1577 config: Config{
1578 MaxVersion: VersionTLS10,
1579 MinVersion: VersionTLS10,
1580 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1581 },
1582 messageLen: -1, // read until EOF
1583 flags: []string{
1584 "-async",
1585 "-write-different-record-sizes",
1586 "-cbc-record-splitting",
1587 },
David Benjamina8e3e0e2014-08-06 22:11:10 -04001588 })
1589 testCases = append(testCases, testCase{
Kenny Root7fdeaf12014-08-05 15:23:37 -07001590 name: "CBCRecordSplittingPartialWrite",
1591 config: Config{
1592 MaxVersion: VersionTLS10,
1593 MinVersion: VersionTLS10,
1594 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1595 },
1596 messageLen: -1, // read until EOF
1597 flags: []string{
1598 "-async",
1599 "-write-different-record-sizes",
1600 "-cbc-record-splitting",
1601 "-partial-write",
1602 },
1603 })
1604}
1605
David Benjamin636293b2014-07-08 17:59:18 -04001606func addClientAuthTests() {
David Benjamin407a10c2014-07-16 12:58:59 -04001607 // Add a dummy cert pool to stress certificate authority parsing.
1608 // TODO(davidben): Add tests that those values parse out correctly.
1609 certPool := x509.NewCertPool()
1610 cert, err := x509.ParseCertificate(rsaCertificate.Certificate[0])
1611 if err != nil {
1612 panic(err)
1613 }
1614 certPool.AddCert(cert)
1615
David Benjamin636293b2014-07-08 17:59:18 -04001616 for _, ver := range tlsVersions {
David Benjamin636293b2014-07-08 17:59:18 -04001617 testCases = append(testCases, testCase{
1618 testType: clientTest,
David Benjamin67666e72014-07-12 15:47:52 -04001619 name: ver.name + "-Client-ClientAuth-RSA",
David Benjamin636293b2014-07-08 17:59:18 -04001620 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001621 MinVersion: ver.version,
1622 MaxVersion: ver.version,
1623 ClientAuth: RequireAnyClientCert,
1624 ClientCAs: certPool,
David Benjamin636293b2014-07-08 17:59:18 -04001625 },
1626 flags: []string{
1627 "-cert-file", rsaCertificateFile,
1628 "-key-file", rsaKeyFile,
1629 },
1630 })
1631 testCases = append(testCases, testCase{
David Benjamin67666e72014-07-12 15:47:52 -04001632 testType: serverTest,
1633 name: ver.name + "-Server-ClientAuth-RSA",
1634 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001635 MinVersion: ver.version,
1636 MaxVersion: ver.version,
David Benjamin67666e72014-07-12 15:47:52 -04001637 Certificates: []Certificate{rsaCertificate},
1638 },
1639 flags: []string{"-require-any-client-certificate"},
1640 })
David Benjamine098ec22014-08-27 23:13:20 -04001641 if ver.version != VersionSSL30 {
1642 testCases = append(testCases, testCase{
1643 testType: serverTest,
1644 name: ver.name + "-Server-ClientAuth-ECDSA",
1645 config: Config{
1646 MinVersion: ver.version,
1647 MaxVersion: ver.version,
1648 Certificates: []Certificate{ecdsaCertificate},
1649 },
1650 flags: []string{"-require-any-client-certificate"},
1651 })
1652 testCases = append(testCases, testCase{
1653 testType: clientTest,
1654 name: ver.name + "-Client-ClientAuth-ECDSA",
1655 config: Config{
1656 MinVersion: ver.version,
1657 MaxVersion: ver.version,
1658 ClientAuth: RequireAnyClientCert,
1659 ClientCAs: certPool,
1660 },
1661 flags: []string{
1662 "-cert-file", ecdsaCertificateFile,
1663 "-key-file", ecdsaKeyFile,
1664 },
1665 })
1666 }
David Benjamin636293b2014-07-08 17:59:18 -04001667 }
1668}
1669
Adam Langley75712922014-10-10 16:23:43 -07001670func addExtendedMasterSecretTests() {
1671 const expectEMSFlag = "-expect-extended-master-secret"
1672
1673 for _, with := range []bool{false, true} {
1674 prefix := "No"
1675 var flags []string
1676 if with {
1677 prefix = ""
1678 flags = []string{expectEMSFlag}
1679 }
1680
1681 for _, isClient := range []bool{false, true} {
1682 suffix := "-Server"
1683 testType := serverTest
1684 if isClient {
1685 suffix = "-Client"
1686 testType = clientTest
1687 }
1688
1689 for _, ver := range tlsVersions {
1690 test := testCase{
1691 testType: testType,
1692 name: prefix + "ExtendedMasterSecret-" + ver.name + suffix,
1693 config: Config{
1694 MinVersion: ver.version,
1695 MaxVersion: ver.version,
1696 Bugs: ProtocolBugs{
1697 NoExtendedMasterSecret: !with,
1698 RequireExtendedMasterSecret: with,
1699 },
1700 },
David Benjamin48cae082014-10-27 01:06:24 -04001701 flags: flags,
1702 shouldFail: ver.version == VersionSSL30 && with,
Adam Langley75712922014-10-10 16:23:43 -07001703 }
1704 if test.shouldFail {
1705 test.expectedLocalError = "extended master secret required but not supported by peer"
1706 }
1707 testCases = append(testCases, test)
1708 }
1709 }
1710 }
1711
1712 // When a session is resumed, it should still be aware that its master
1713 // secret was generated via EMS and thus it's safe to use tls-unique.
1714 testCases = append(testCases, testCase{
1715 name: "ExtendedMasterSecret-Resume",
1716 config: Config{
1717 Bugs: ProtocolBugs{
1718 RequireExtendedMasterSecret: true,
1719 },
1720 },
1721 flags: []string{expectEMSFlag},
1722 resumeSession: true,
1723 })
1724}
1725
David Benjamin43ec06f2014-08-05 02:28:57 -04001726// Adds tests that try to cover the range of the handshake state machine, under
1727// various conditions. Some of these are redundant with other tests, but they
1728// only cover the synchronous case.
David Benjamin6fd297b2014-08-11 18:43:38 -04001729func addStateMachineCoverageTests(async, splitHandshake bool, protocol protocol) {
David Benjamin43ec06f2014-08-05 02:28:57 -04001730 var suffix string
1731 var flags []string
1732 var maxHandshakeRecordLength int
David Benjamin6fd297b2014-08-11 18:43:38 -04001733 if protocol == dtls {
1734 suffix = "-DTLS"
1735 }
David Benjamin43ec06f2014-08-05 02:28:57 -04001736 if async {
David Benjamin6fd297b2014-08-11 18:43:38 -04001737 suffix += "-Async"
David Benjamin43ec06f2014-08-05 02:28:57 -04001738 flags = append(flags, "-async")
1739 } else {
David Benjamin6fd297b2014-08-11 18:43:38 -04001740 suffix += "-Sync"
David Benjamin43ec06f2014-08-05 02:28:57 -04001741 }
1742 if splitHandshake {
1743 suffix += "-SplitHandshakeRecords"
David Benjamin98214542014-08-07 18:02:39 -04001744 maxHandshakeRecordLength = 1
David Benjamin43ec06f2014-08-05 02:28:57 -04001745 }
1746
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001747 // Basic handshake, with resumption. Client and server,
1748 // session ID and session ticket.
David Benjamin43ec06f2014-08-05 02:28:57 -04001749 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001750 protocol: protocol,
1751 name: "Basic-Client" + suffix,
David Benjamin43ec06f2014-08-05 02:28:57 -04001752 config: Config{
1753 Bugs: ProtocolBugs{
1754 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1755 },
1756 },
David Benjaminbed9aae2014-08-07 19:13:38 -04001757 flags: flags,
1758 resumeSession: true,
1759 })
1760 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001761 protocol: protocol,
1762 name: "Basic-Client-RenewTicket" + suffix,
David Benjaminbed9aae2014-08-07 19:13:38 -04001763 config: Config{
1764 Bugs: ProtocolBugs{
1765 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1766 RenewTicketOnResume: true,
1767 },
1768 },
1769 flags: flags,
1770 resumeSession: true,
David Benjamin43ec06f2014-08-05 02:28:57 -04001771 })
1772 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001773 protocol: protocol,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001774 name: "Basic-Client-NoTicket" + suffix,
1775 config: Config{
1776 SessionTicketsDisabled: true,
1777 Bugs: ProtocolBugs{
1778 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1779 },
1780 },
1781 flags: flags,
1782 resumeSession: true,
1783 })
1784 testCases = append(testCases, testCase{
1785 protocol: protocol,
David Benjamine0e7d0d2015-02-08 19:33:25 -05001786 name: "Basic-Client-Implicit" + suffix,
1787 config: Config{
1788 Bugs: ProtocolBugs{
1789 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1790 },
1791 },
1792 flags: append(flags, "-implicit-handshake"),
1793 resumeSession: true,
1794 })
1795 testCases = append(testCases, testCase{
1796 protocol: protocol,
David Benjamin43ec06f2014-08-05 02:28:57 -04001797 testType: serverTest,
1798 name: "Basic-Server" + suffix,
1799 config: Config{
1800 Bugs: ProtocolBugs{
1801 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1802 },
1803 },
David Benjaminbed9aae2014-08-07 19:13:38 -04001804 flags: flags,
1805 resumeSession: true,
David Benjamin43ec06f2014-08-05 02:28:57 -04001806 })
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001807 testCases = append(testCases, testCase{
1808 protocol: protocol,
1809 testType: serverTest,
1810 name: "Basic-Server-NoTickets" + suffix,
1811 config: Config{
1812 SessionTicketsDisabled: true,
1813 Bugs: ProtocolBugs{
1814 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1815 },
1816 },
1817 flags: flags,
1818 resumeSession: true,
1819 })
David Benjamine0e7d0d2015-02-08 19:33:25 -05001820 testCases = append(testCases, testCase{
1821 protocol: protocol,
1822 testType: serverTest,
1823 name: "Basic-Server-Implicit" + suffix,
1824 config: Config{
1825 Bugs: ProtocolBugs{
1826 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1827 },
1828 },
1829 flags: append(flags, "-implicit-handshake"),
1830 resumeSession: true,
1831 })
David Benjamin6f5c0f42015-02-24 01:23:21 -05001832 testCases = append(testCases, testCase{
1833 protocol: protocol,
1834 testType: serverTest,
1835 name: "Basic-Server-EarlyCallback" + suffix,
1836 config: Config{
1837 Bugs: ProtocolBugs{
1838 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1839 },
1840 },
1841 flags: append(flags, "-use-early-callback"),
1842 resumeSession: true,
1843 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001844
David Benjamin6fd297b2014-08-11 18:43:38 -04001845 // TLS client auth.
1846 testCases = append(testCases, testCase{
1847 protocol: protocol,
1848 testType: clientTest,
1849 name: "ClientAuth-Client" + suffix,
1850 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001851 ClientAuth: RequireAnyClientCert,
David Benjamin6fd297b2014-08-11 18:43:38 -04001852 Bugs: ProtocolBugs{
1853 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1854 },
1855 },
1856 flags: append(flags,
1857 "-cert-file", rsaCertificateFile,
1858 "-key-file", rsaKeyFile),
1859 })
1860 testCases = append(testCases, testCase{
1861 protocol: protocol,
1862 testType: serverTest,
1863 name: "ClientAuth-Server" + suffix,
1864 config: Config{
1865 Certificates: []Certificate{rsaCertificate},
1866 },
1867 flags: append(flags, "-require-any-client-certificate"),
1868 })
1869
David Benjamin43ec06f2014-08-05 02:28:57 -04001870 // No session ticket support; server doesn't send NewSessionTicket.
1871 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001872 protocol: protocol,
1873 name: "SessionTicketsDisabled-Client" + suffix,
David Benjamin43ec06f2014-08-05 02:28:57 -04001874 config: Config{
1875 SessionTicketsDisabled: true,
1876 Bugs: ProtocolBugs{
1877 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1878 },
1879 },
1880 flags: flags,
1881 })
1882 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001883 protocol: protocol,
David Benjamin43ec06f2014-08-05 02:28:57 -04001884 testType: serverTest,
1885 name: "SessionTicketsDisabled-Server" + suffix,
1886 config: Config{
1887 SessionTicketsDisabled: true,
1888 Bugs: ProtocolBugs{
1889 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1890 },
1891 },
1892 flags: flags,
1893 })
1894
David Benjamin48cae082014-10-27 01:06:24 -04001895 // Skip ServerKeyExchange in PSK key exchange if there's no
1896 // identity hint.
1897 testCases = append(testCases, testCase{
1898 protocol: protocol,
1899 name: "EmptyPSKHint-Client" + suffix,
1900 config: Config{
1901 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
1902 PreSharedKey: []byte("secret"),
1903 Bugs: ProtocolBugs{
1904 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1905 },
1906 },
1907 flags: append(flags, "-psk", "secret"),
1908 })
1909 testCases = append(testCases, testCase{
1910 protocol: protocol,
1911 testType: serverTest,
1912 name: "EmptyPSKHint-Server" + suffix,
1913 config: Config{
1914 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
1915 PreSharedKey: []byte("secret"),
1916 Bugs: ProtocolBugs{
1917 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1918 },
1919 },
1920 flags: append(flags, "-psk", "secret"),
1921 })
1922
David Benjamin6fd297b2014-08-11 18:43:38 -04001923 if protocol == tls {
1924 // NPN on client and server; results in post-handshake message.
1925 testCases = append(testCases, testCase{
1926 protocol: protocol,
1927 name: "NPN-Client" + suffix,
1928 config: Config{
David Benjaminae2888f2014-09-06 12:58:58 -04001929 NextProtos: []string{"foo"},
David Benjamin6fd297b2014-08-11 18:43:38 -04001930 Bugs: ProtocolBugs{
1931 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1932 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001933 },
David Benjaminfc7b0862014-09-06 13:21:53 -04001934 flags: append(flags, "-select-next-proto", "foo"),
1935 expectedNextProto: "foo",
1936 expectedNextProtoType: npn,
David Benjamin6fd297b2014-08-11 18:43:38 -04001937 })
1938 testCases = append(testCases, testCase{
1939 protocol: protocol,
1940 testType: serverTest,
1941 name: "NPN-Server" + suffix,
1942 config: Config{
1943 NextProtos: []string{"bar"},
1944 Bugs: ProtocolBugs{
1945 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1946 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001947 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001948 flags: append(flags,
1949 "-advertise-npn", "\x03foo\x03bar\x03baz",
1950 "-expect-next-proto", "bar"),
David Benjaminfc7b0862014-09-06 13:21:53 -04001951 expectedNextProto: "bar",
1952 expectedNextProtoType: npn,
David Benjamin6fd297b2014-08-11 18:43:38 -04001953 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001954
David Benjamin195dc782015-02-19 13:27:05 -05001955 // TODO(davidben): Add tests for when False Start doesn't trigger.
1956
David Benjamin6fd297b2014-08-11 18:43:38 -04001957 // Client does False Start and negotiates NPN.
1958 testCases = append(testCases, testCase{
1959 protocol: protocol,
1960 name: "FalseStart" + suffix,
1961 config: Config{
1962 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1963 NextProtos: []string{"foo"},
1964 Bugs: ProtocolBugs{
David Benjamine58c4f52014-08-24 03:47:07 -04001965 ExpectFalseStart: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001966 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1967 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001968 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001969 flags: append(flags,
1970 "-false-start",
1971 "-select-next-proto", "foo"),
David Benjamine58c4f52014-08-24 03:47:07 -04001972 shimWritesFirst: true,
1973 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001974 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001975
David Benjaminae2888f2014-09-06 12:58:58 -04001976 // Client does False Start and negotiates ALPN.
1977 testCases = append(testCases, testCase{
1978 protocol: protocol,
1979 name: "FalseStart-ALPN" + suffix,
1980 config: Config{
1981 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1982 NextProtos: []string{"foo"},
1983 Bugs: ProtocolBugs{
1984 ExpectFalseStart: true,
1985 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1986 },
1987 },
1988 flags: append(flags,
1989 "-false-start",
1990 "-advertise-alpn", "\x03foo"),
1991 shimWritesFirst: true,
1992 resumeSession: true,
1993 })
1994
David Benjamin931ab342015-02-08 19:46:57 -05001995 // Client does False Start but doesn't explicitly call
1996 // SSL_connect.
1997 testCases = append(testCases, testCase{
1998 protocol: protocol,
1999 name: "FalseStart-Implicit" + suffix,
2000 config: Config{
2001 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2002 NextProtos: []string{"foo"},
2003 Bugs: ProtocolBugs{
2004 MaxHandshakeRecordLength: maxHandshakeRecordLength,
2005 },
2006 },
2007 flags: append(flags,
2008 "-implicit-handshake",
2009 "-false-start",
2010 "-advertise-alpn", "\x03foo"),
2011 })
2012
David Benjamin6fd297b2014-08-11 18:43:38 -04002013 // False Start without session tickets.
2014 testCases = append(testCases, testCase{
David Benjamin5f237bc2015-02-11 17:14:15 -05002015 name: "FalseStart-SessionTicketsDisabled" + suffix,
David Benjamin6fd297b2014-08-11 18:43:38 -04002016 config: Config{
2017 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2018 NextProtos: []string{"foo"},
2019 SessionTicketsDisabled: true,
David Benjamin4e99c522014-08-24 01:45:30 -04002020 Bugs: ProtocolBugs{
David Benjamine58c4f52014-08-24 03:47:07 -04002021 ExpectFalseStart: true,
David Benjamin4e99c522014-08-24 01:45:30 -04002022 MaxHandshakeRecordLength: maxHandshakeRecordLength,
2023 },
David Benjamin43ec06f2014-08-05 02:28:57 -04002024 },
David Benjamin4e99c522014-08-24 01:45:30 -04002025 flags: append(flags,
David Benjamin6fd297b2014-08-11 18:43:38 -04002026 "-false-start",
2027 "-select-next-proto", "foo",
David Benjamin4e99c522014-08-24 01:45:30 -04002028 ),
David Benjamine58c4f52014-08-24 03:47:07 -04002029 shimWritesFirst: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04002030 })
David Benjamin1e7f8d72014-08-08 12:27:04 -04002031
David Benjamina08e49d2014-08-24 01:46:07 -04002032 // Server parses a V2ClientHello.
David Benjamin6fd297b2014-08-11 18:43:38 -04002033 testCases = append(testCases, testCase{
2034 protocol: protocol,
2035 testType: serverTest,
2036 name: "SendV2ClientHello" + suffix,
2037 config: Config{
2038 // Choose a cipher suite that does not involve
2039 // elliptic curves, so no extensions are
2040 // involved.
2041 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2042 Bugs: ProtocolBugs{
2043 MaxHandshakeRecordLength: maxHandshakeRecordLength,
2044 SendV2ClientHello: true,
2045 },
David Benjamin1e7f8d72014-08-08 12:27:04 -04002046 },
David Benjamin6fd297b2014-08-11 18:43:38 -04002047 flags: flags,
2048 })
David Benjamina08e49d2014-08-24 01:46:07 -04002049
2050 // Client sends a Channel ID.
2051 testCases = append(testCases, testCase{
2052 protocol: protocol,
2053 name: "ChannelID-Client" + suffix,
2054 config: Config{
2055 RequestChannelID: true,
2056 Bugs: ProtocolBugs{
2057 MaxHandshakeRecordLength: maxHandshakeRecordLength,
2058 },
2059 },
2060 flags: append(flags,
2061 "-send-channel-id", channelIDKeyFile,
2062 ),
2063 resumeSession: true,
2064 expectChannelID: true,
2065 })
2066
2067 // Server accepts a Channel ID.
2068 testCases = append(testCases, testCase{
2069 protocol: protocol,
2070 testType: serverTest,
2071 name: "ChannelID-Server" + suffix,
2072 config: Config{
2073 ChannelID: channelIDKey,
2074 Bugs: ProtocolBugs{
2075 MaxHandshakeRecordLength: maxHandshakeRecordLength,
2076 },
2077 },
2078 flags: append(flags,
2079 "-expect-channel-id",
2080 base64.StdEncoding.EncodeToString(channelIDBytes),
2081 ),
2082 resumeSession: true,
2083 expectChannelID: true,
2084 })
David Benjamin6fd297b2014-08-11 18:43:38 -04002085 } else {
2086 testCases = append(testCases, testCase{
2087 protocol: protocol,
2088 name: "SkipHelloVerifyRequest" + suffix,
2089 config: Config{
2090 Bugs: ProtocolBugs{
2091 MaxHandshakeRecordLength: maxHandshakeRecordLength,
2092 SkipHelloVerifyRequest: true,
2093 },
2094 },
2095 flags: flags,
2096 })
David Benjamin6fd297b2014-08-11 18:43:38 -04002097 }
David Benjamin43ec06f2014-08-05 02:28:57 -04002098}
2099
Adam Langley524e7172015-02-20 16:04:00 -08002100func addDDoSCallbackTests() {
2101 // DDoS callback.
2102
2103 for _, resume := range []bool{false, true} {
2104 suffix := "Resume"
2105 if resume {
2106 suffix = "No" + suffix
2107 }
2108
2109 testCases = append(testCases, testCase{
2110 testType: serverTest,
2111 name: "Server-DDoS-OK-" + suffix,
2112 flags: []string{"-install-ddos-callback"},
2113 resumeSession: resume,
2114 })
2115
2116 failFlag := "-fail-ddos-callback"
2117 if resume {
2118 failFlag = "-fail-second-ddos-callback"
2119 }
2120 testCases = append(testCases, testCase{
2121 testType: serverTest,
2122 name: "Server-DDoS-Reject-" + suffix,
2123 flags: []string{"-install-ddos-callback", failFlag},
2124 resumeSession: resume,
2125 shouldFail: true,
2126 expectedError: ":CONNECTION_REJECTED:",
2127 })
2128 }
2129}
2130
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002131func addVersionNegotiationTests() {
2132 for i, shimVers := range tlsVersions {
2133 // Assemble flags to disable all newer versions on the shim.
2134 var flags []string
2135 for _, vers := range tlsVersions[i+1:] {
2136 flags = append(flags, vers.flag)
2137 }
2138
2139 for _, runnerVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05002140 protocols := []protocol{tls}
2141 if runnerVers.hasDTLS && shimVers.hasDTLS {
2142 protocols = append(protocols, dtls)
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002143 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002144 for _, protocol := range protocols {
2145 expectedVersion := shimVers.version
2146 if runnerVers.version < shimVers.version {
2147 expectedVersion = runnerVers.version
2148 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002149
David Benjamin8b8c0062014-11-23 02:47:52 -05002150 suffix := shimVers.name + "-" + runnerVers.name
2151 if protocol == dtls {
2152 suffix += "-DTLS"
2153 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002154
David Benjamin1eb367c2014-12-12 18:17:51 -05002155 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
2156
David Benjamin1e29a6b2014-12-10 02:27:24 -05002157 clientVers := shimVers.version
2158 if clientVers > VersionTLS10 {
2159 clientVers = VersionTLS10
2160 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002161 testCases = append(testCases, testCase{
2162 protocol: protocol,
2163 testType: clientTest,
2164 name: "VersionNegotiation-Client-" + suffix,
2165 config: Config{
2166 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05002167 Bugs: ProtocolBugs{
2168 ExpectInitialRecordVersion: clientVers,
2169 },
David Benjamin8b8c0062014-11-23 02:47:52 -05002170 },
2171 flags: flags,
2172 expectedVersion: expectedVersion,
2173 })
David Benjamin1eb367c2014-12-12 18:17:51 -05002174 testCases = append(testCases, testCase{
2175 protocol: protocol,
2176 testType: clientTest,
2177 name: "VersionNegotiation-Client2-" + suffix,
2178 config: Config{
2179 MaxVersion: runnerVers.version,
2180 Bugs: ProtocolBugs{
2181 ExpectInitialRecordVersion: clientVers,
2182 },
2183 },
2184 flags: []string{"-max-version", shimVersFlag},
2185 expectedVersion: expectedVersion,
2186 })
David Benjamin8b8c0062014-11-23 02:47:52 -05002187
2188 testCases = append(testCases, testCase{
2189 protocol: protocol,
2190 testType: serverTest,
2191 name: "VersionNegotiation-Server-" + suffix,
2192 config: Config{
2193 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05002194 Bugs: ProtocolBugs{
2195 ExpectInitialRecordVersion: expectedVersion,
2196 },
David Benjamin8b8c0062014-11-23 02:47:52 -05002197 },
2198 flags: flags,
2199 expectedVersion: expectedVersion,
2200 })
David Benjamin1eb367c2014-12-12 18:17:51 -05002201 testCases = append(testCases, testCase{
2202 protocol: protocol,
2203 testType: serverTest,
2204 name: "VersionNegotiation-Server2-" + suffix,
2205 config: Config{
2206 MaxVersion: runnerVers.version,
2207 Bugs: ProtocolBugs{
2208 ExpectInitialRecordVersion: expectedVersion,
2209 },
2210 },
2211 flags: []string{"-max-version", shimVersFlag},
2212 expectedVersion: expectedVersion,
2213 })
David Benjamin8b8c0062014-11-23 02:47:52 -05002214 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002215 }
2216 }
2217}
2218
David Benjaminaccb4542014-12-12 23:44:33 -05002219func addMinimumVersionTests() {
2220 for i, shimVers := range tlsVersions {
2221 // Assemble flags to disable all older versions on the shim.
2222 var flags []string
2223 for _, vers := range tlsVersions[:i] {
2224 flags = append(flags, vers.flag)
2225 }
2226
2227 for _, runnerVers := range tlsVersions {
2228 protocols := []protocol{tls}
2229 if runnerVers.hasDTLS && shimVers.hasDTLS {
2230 protocols = append(protocols, dtls)
2231 }
2232 for _, protocol := range protocols {
2233 suffix := shimVers.name + "-" + runnerVers.name
2234 if protocol == dtls {
2235 suffix += "-DTLS"
2236 }
2237 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
2238
David Benjaminaccb4542014-12-12 23:44:33 -05002239 var expectedVersion uint16
2240 var shouldFail bool
2241 var expectedError string
David Benjamin87909c02014-12-13 01:55:01 -05002242 var expectedLocalError string
David Benjaminaccb4542014-12-12 23:44:33 -05002243 if runnerVers.version >= shimVers.version {
2244 expectedVersion = runnerVers.version
2245 } else {
2246 shouldFail = true
2247 expectedError = ":UNSUPPORTED_PROTOCOL:"
David Benjamin87909c02014-12-13 01:55:01 -05002248 if runnerVers.version > VersionSSL30 {
2249 expectedLocalError = "remote error: protocol version not supported"
2250 } else {
2251 expectedLocalError = "remote error: handshake failure"
2252 }
David Benjaminaccb4542014-12-12 23:44:33 -05002253 }
2254
2255 testCases = append(testCases, testCase{
2256 protocol: protocol,
2257 testType: clientTest,
2258 name: "MinimumVersion-Client-" + suffix,
2259 config: Config{
2260 MaxVersion: runnerVers.version,
2261 },
David Benjamin87909c02014-12-13 01:55:01 -05002262 flags: flags,
2263 expectedVersion: expectedVersion,
2264 shouldFail: shouldFail,
2265 expectedError: expectedError,
2266 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002267 })
2268 testCases = append(testCases, testCase{
2269 protocol: protocol,
2270 testType: clientTest,
2271 name: "MinimumVersion-Client2-" + suffix,
2272 config: Config{
2273 MaxVersion: runnerVers.version,
2274 },
David Benjamin87909c02014-12-13 01:55:01 -05002275 flags: []string{"-min-version", shimVersFlag},
2276 expectedVersion: expectedVersion,
2277 shouldFail: shouldFail,
2278 expectedError: expectedError,
2279 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002280 })
2281
2282 testCases = append(testCases, testCase{
2283 protocol: protocol,
2284 testType: serverTest,
2285 name: "MinimumVersion-Server-" + suffix,
2286 config: Config{
2287 MaxVersion: runnerVers.version,
2288 },
David Benjamin87909c02014-12-13 01:55:01 -05002289 flags: flags,
2290 expectedVersion: expectedVersion,
2291 shouldFail: shouldFail,
2292 expectedError: expectedError,
2293 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002294 })
2295 testCases = append(testCases, testCase{
2296 protocol: protocol,
2297 testType: serverTest,
2298 name: "MinimumVersion-Server2-" + suffix,
2299 config: Config{
2300 MaxVersion: runnerVers.version,
2301 },
David Benjamin87909c02014-12-13 01:55:01 -05002302 flags: []string{"-min-version", shimVersFlag},
2303 expectedVersion: expectedVersion,
2304 shouldFail: shouldFail,
2305 expectedError: expectedError,
2306 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002307 })
2308 }
2309 }
2310 }
2311}
2312
David Benjamin5c24a1d2014-08-31 00:59:27 -04002313func addD5BugTests() {
2314 testCases = append(testCases, testCase{
2315 testType: serverTest,
2316 name: "D5Bug-NoQuirk-Reject",
2317 config: Config{
2318 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2319 Bugs: ProtocolBugs{
2320 SSL3RSAKeyExchange: true,
2321 },
2322 },
2323 shouldFail: true,
2324 expectedError: ":TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG:",
2325 })
2326 testCases = append(testCases, testCase{
2327 testType: serverTest,
2328 name: "D5Bug-Quirk-Normal",
2329 config: Config{
2330 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2331 },
2332 flags: []string{"-tls-d5-bug"},
2333 })
2334 testCases = append(testCases, testCase{
2335 testType: serverTest,
2336 name: "D5Bug-Quirk-Bug",
2337 config: Config{
2338 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2339 Bugs: ProtocolBugs{
2340 SSL3RSAKeyExchange: true,
2341 },
2342 },
2343 flags: []string{"-tls-d5-bug"},
2344 })
2345}
2346
David Benjamine78bfde2014-09-06 12:45:15 -04002347func addExtensionTests() {
2348 testCases = append(testCases, testCase{
2349 testType: clientTest,
2350 name: "DuplicateExtensionClient",
2351 config: Config{
2352 Bugs: ProtocolBugs{
2353 DuplicateExtension: true,
2354 },
2355 },
2356 shouldFail: true,
2357 expectedLocalError: "remote error: error decoding message",
2358 })
2359 testCases = append(testCases, testCase{
2360 testType: serverTest,
2361 name: "DuplicateExtensionServer",
2362 config: Config{
2363 Bugs: ProtocolBugs{
2364 DuplicateExtension: true,
2365 },
2366 },
2367 shouldFail: true,
2368 expectedLocalError: "remote error: error decoding message",
2369 })
2370 testCases = append(testCases, testCase{
2371 testType: clientTest,
2372 name: "ServerNameExtensionClient",
2373 config: Config{
2374 Bugs: ProtocolBugs{
2375 ExpectServerName: "example.com",
2376 },
2377 },
2378 flags: []string{"-host-name", "example.com"},
2379 })
2380 testCases = append(testCases, testCase{
2381 testType: clientTest,
David Benjamin5f237bc2015-02-11 17:14:15 -05002382 name: "ServerNameExtensionClientMismatch",
David Benjamine78bfde2014-09-06 12:45:15 -04002383 config: Config{
2384 Bugs: ProtocolBugs{
2385 ExpectServerName: "mismatch.com",
2386 },
2387 },
2388 flags: []string{"-host-name", "example.com"},
2389 shouldFail: true,
2390 expectedLocalError: "tls: unexpected server name",
2391 })
2392 testCases = append(testCases, testCase{
2393 testType: clientTest,
David Benjamin5f237bc2015-02-11 17:14:15 -05002394 name: "ServerNameExtensionClientMissing",
David Benjamine78bfde2014-09-06 12:45:15 -04002395 config: Config{
2396 Bugs: ProtocolBugs{
2397 ExpectServerName: "missing.com",
2398 },
2399 },
2400 shouldFail: true,
2401 expectedLocalError: "tls: unexpected server name",
2402 })
2403 testCases = append(testCases, testCase{
2404 testType: serverTest,
2405 name: "ServerNameExtensionServer",
2406 config: Config{
2407 ServerName: "example.com",
2408 },
2409 flags: []string{"-expect-server-name", "example.com"},
2410 resumeSession: true,
2411 })
David Benjaminae2888f2014-09-06 12:58:58 -04002412 testCases = append(testCases, testCase{
2413 testType: clientTest,
2414 name: "ALPNClient",
2415 config: Config{
2416 NextProtos: []string{"foo"},
2417 },
2418 flags: []string{
2419 "-advertise-alpn", "\x03foo\x03bar\x03baz",
2420 "-expect-alpn", "foo",
2421 },
David Benjaminfc7b0862014-09-06 13:21:53 -04002422 expectedNextProto: "foo",
2423 expectedNextProtoType: alpn,
2424 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04002425 })
2426 testCases = append(testCases, testCase{
2427 testType: serverTest,
2428 name: "ALPNServer",
2429 config: Config{
2430 NextProtos: []string{"foo", "bar", "baz"},
2431 },
2432 flags: []string{
2433 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2434 "-select-alpn", "foo",
2435 },
David Benjaminfc7b0862014-09-06 13:21:53 -04002436 expectedNextProto: "foo",
2437 expectedNextProtoType: alpn,
2438 resumeSession: true,
2439 })
2440 // Test that the server prefers ALPN over NPN.
2441 testCases = append(testCases, testCase{
2442 testType: serverTest,
2443 name: "ALPNServer-Preferred",
2444 config: Config{
2445 NextProtos: []string{"foo", "bar", "baz"},
2446 },
2447 flags: []string{
2448 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2449 "-select-alpn", "foo",
2450 "-advertise-npn", "\x03foo\x03bar\x03baz",
2451 },
2452 expectedNextProto: "foo",
2453 expectedNextProtoType: alpn,
2454 resumeSession: true,
2455 })
2456 testCases = append(testCases, testCase{
2457 testType: serverTest,
2458 name: "ALPNServer-Preferred-Swapped",
2459 config: Config{
2460 NextProtos: []string{"foo", "bar", "baz"},
2461 Bugs: ProtocolBugs{
2462 SwapNPNAndALPN: true,
2463 },
2464 },
2465 flags: []string{
2466 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2467 "-select-alpn", "foo",
2468 "-advertise-npn", "\x03foo\x03bar\x03baz",
2469 },
2470 expectedNextProto: "foo",
2471 expectedNextProtoType: alpn,
2472 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04002473 })
Adam Langley38311732014-10-16 19:04:35 -07002474 // Resume with a corrupt ticket.
2475 testCases = append(testCases, testCase{
2476 testType: serverTest,
2477 name: "CorruptTicket",
2478 config: Config{
2479 Bugs: ProtocolBugs{
2480 CorruptTicket: true,
2481 },
2482 },
2483 resumeSession: true,
2484 flags: []string{"-expect-session-miss"},
2485 })
2486 // Resume with an oversized session id.
2487 testCases = append(testCases, testCase{
2488 testType: serverTest,
2489 name: "OversizedSessionId",
2490 config: Config{
2491 Bugs: ProtocolBugs{
2492 OversizedSessionId: true,
2493 },
2494 },
2495 resumeSession: true,
Adam Langley75712922014-10-10 16:23:43 -07002496 shouldFail: true,
Adam Langley38311732014-10-16 19:04:35 -07002497 expectedError: ":DECODE_ERROR:",
2498 })
David Benjaminca6c8262014-11-15 19:06:08 -05002499 // Basic DTLS-SRTP tests. Include fake profiles to ensure they
2500 // are ignored.
2501 testCases = append(testCases, testCase{
2502 protocol: dtls,
2503 name: "SRTP-Client",
2504 config: Config{
2505 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
2506 },
2507 flags: []string{
2508 "-srtp-profiles",
2509 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2510 },
2511 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2512 })
2513 testCases = append(testCases, testCase{
2514 protocol: dtls,
2515 testType: serverTest,
2516 name: "SRTP-Server",
2517 config: Config{
2518 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
2519 },
2520 flags: []string{
2521 "-srtp-profiles",
2522 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2523 },
2524 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2525 })
2526 // Test that the MKI is ignored.
2527 testCases = append(testCases, testCase{
2528 protocol: dtls,
2529 testType: serverTest,
2530 name: "SRTP-Server-IgnoreMKI",
2531 config: Config{
2532 SRTPProtectionProfiles: []uint16{SRTP_AES128_CM_HMAC_SHA1_80},
2533 Bugs: ProtocolBugs{
2534 SRTPMasterKeyIdentifer: "bogus",
2535 },
2536 },
2537 flags: []string{
2538 "-srtp-profiles",
2539 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2540 },
2541 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2542 })
2543 // Test that SRTP isn't negotiated on the server if there were
2544 // no matching profiles.
2545 testCases = append(testCases, testCase{
2546 protocol: dtls,
2547 testType: serverTest,
2548 name: "SRTP-Server-NoMatch",
2549 config: Config{
2550 SRTPProtectionProfiles: []uint16{100, 101, 102},
2551 },
2552 flags: []string{
2553 "-srtp-profiles",
2554 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2555 },
2556 expectedSRTPProtectionProfile: 0,
2557 })
2558 // Test that the server returning an invalid SRTP profile is
2559 // flagged as an error by the client.
2560 testCases = append(testCases, testCase{
2561 protocol: dtls,
2562 name: "SRTP-Client-NoMatch",
2563 config: Config{
2564 Bugs: ProtocolBugs{
2565 SendSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_32,
2566 },
2567 },
2568 flags: []string{
2569 "-srtp-profiles",
2570 "SRTP_AES128_CM_SHA1_80",
2571 },
2572 shouldFail: true,
2573 expectedError: ":BAD_SRTP_PROTECTION_PROFILE_LIST:",
2574 })
David Benjamin61f95272014-11-25 01:55:35 -05002575 // Test OCSP stapling and SCT list.
2576 testCases = append(testCases, testCase{
2577 name: "OCSPStapling",
2578 flags: []string{
2579 "-enable-ocsp-stapling",
2580 "-expect-ocsp-response",
2581 base64.StdEncoding.EncodeToString(testOCSPResponse),
2582 },
2583 })
2584 testCases = append(testCases, testCase{
2585 name: "SignedCertificateTimestampList",
2586 flags: []string{
2587 "-enable-signed-cert-timestamps",
2588 "-expect-signed-cert-timestamps",
2589 base64.StdEncoding.EncodeToString(testSCTList),
2590 },
2591 })
David Benjamine78bfde2014-09-06 12:45:15 -04002592}
2593
David Benjamin01fe8202014-09-24 15:21:44 -04002594func addResumptionVersionTests() {
David Benjamin01fe8202014-09-24 15:21:44 -04002595 for _, sessionVers := range tlsVersions {
David Benjamin01fe8202014-09-24 15:21:44 -04002596 for _, resumeVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05002597 protocols := []protocol{tls}
2598 if sessionVers.hasDTLS && resumeVers.hasDTLS {
2599 protocols = append(protocols, dtls)
David Benjaminbdf5e722014-11-11 00:52:15 -05002600 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002601 for _, protocol := range protocols {
2602 suffix := "-" + sessionVers.name + "-" + resumeVers.name
2603 if protocol == dtls {
2604 suffix += "-DTLS"
2605 }
2606
2607 testCases = append(testCases, testCase{
2608 protocol: protocol,
2609 name: "Resume-Client" + suffix,
2610 resumeSession: true,
2611 config: Config{
2612 MaxVersion: sessionVers.version,
2613 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2614 Bugs: ProtocolBugs{
2615 AllowSessionVersionMismatch: true,
2616 },
2617 },
2618 expectedVersion: sessionVers.version,
2619 resumeConfig: &Config{
2620 MaxVersion: resumeVers.version,
2621 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2622 Bugs: ProtocolBugs{
2623 AllowSessionVersionMismatch: true,
2624 },
2625 },
2626 expectedResumeVersion: resumeVers.version,
2627 })
2628
2629 testCases = append(testCases, testCase{
2630 protocol: protocol,
2631 name: "Resume-Client-NoResume" + suffix,
2632 flags: []string{"-expect-session-miss"},
2633 resumeSession: true,
2634 config: Config{
2635 MaxVersion: sessionVers.version,
2636 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2637 },
2638 expectedVersion: sessionVers.version,
2639 resumeConfig: &Config{
2640 MaxVersion: resumeVers.version,
2641 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2642 },
2643 newSessionsOnResume: true,
2644 expectedResumeVersion: resumeVers.version,
2645 })
2646
2647 var flags []string
2648 if sessionVers.version != resumeVers.version {
2649 flags = append(flags, "-expect-session-miss")
2650 }
2651 testCases = append(testCases, testCase{
2652 protocol: protocol,
2653 testType: serverTest,
2654 name: "Resume-Server" + suffix,
2655 flags: flags,
2656 resumeSession: true,
2657 config: Config{
2658 MaxVersion: sessionVers.version,
2659 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2660 },
2661 expectedVersion: sessionVers.version,
2662 resumeConfig: &Config{
2663 MaxVersion: resumeVers.version,
2664 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2665 },
2666 expectedResumeVersion: resumeVers.version,
2667 })
2668 }
David Benjamin01fe8202014-09-24 15:21:44 -04002669 }
2670 }
2671}
2672
Adam Langley2ae77d22014-10-28 17:29:33 -07002673func addRenegotiationTests() {
2674 testCases = append(testCases, testCase{
2675 testType: serverTest,
2676 name: "Renegotiate-Server",
2677 flags: []string{"-renegotiate"},
2678 shimWritesFirst: true,
2679 })
2680 testCases = append(testCases, testCase{
2681 testType: serverTest,
David Benjamincdea40c2015-03-19 14:09:43 -04002682 name: "Renegotiate-Server-Full",
2683 config: Config{
2684 Bugs: ProtocolBugs{
2685 NeverResumeOnRenego: true,
2686 },
2687 },
2688 flags: []string{"-renegotiate"},
2689 shimWritesFirst: true,
2690 })
2691 testCases = append(testCases, testCase{
2692 testType: serverTest,
Adam Langley2ae77d22014-10-28 17:29:33 -07002693 name: "Renegotiate-Server-EmptyExt",
2694 config: Config{
2695 Bugs: ProtocolBugs{
2696 EmptyRenegotiationInfo: true,
2697 },
2698 },
2699 flags: []string{"-renegotiate"},
2700 shimWritesFirst: true,
2701 shouldFail: true,
2702 expectedError: ":RENEGOTIATION_MISMATCH:",
2703 })
2704 testCases = append(testCases, testCase{
2705 testType: serverTest,
2706 name: "Renegotiate-Server-BadExt",
2707 config: Config{
2708 Bugs: ProtocolBugs{
2709 BadRenegotiationInfo: true,
2710 },
2711 },
2712 flags: []string{"-renegotiate"},
2713 shimWritesFirst: true,
2714 shouldFail: true,
2715 expectedError: ":RENEGOTIATION_MISMATCH:",
2716 })
David Benjaminca6554b2014-11-08 12:31:52 -05002717 testCases = append(testCases, testCase{
2718 testType: serverTest,
2719 name: "Renegotiate-Server-ClientInitiated",
2720 renegotiate: true,
2721 })
2722 testCases = append(testCases, testCase{
2723 testType: serverTest,
2724 name: "Renegotiate-Server-ClientInitiated-NoExt",
2725 renegotiate: true,
2726 config: Config{
2727 Bugs: ProtocolBugs{
2728 NoRenegotiationInfo: true,
2729 },
2730 },
2731 shouldFail: true,
2732 expectedError: ":UNSAFE_LEGACY_RENEGOTIATION_DISABLED:",
2733 })
2734 testCases = append(testCases, testCase{
2735 testType: serverTest,
2736 name: "Renegotiate-Server-ClientInitiated-NoExt-Allowed",
2737 renegotiate: true,
2738 config: Config{
2739 Bugs: ProtocolBugs{
2740 NoRenegotiationInfo: true,
2741 },
2742 },
2743 flags: []string{"-allow-unsafe-legacy-renegotiation"},
2744 })
David Benjamin3c9746a2015-03-19 15:00:10 -04002745 // Regression test for CVE-2015-0291.
2746 testCases = append(testCases, testCase{
2747 testType: serverTest,
2748 name: "Renegotiate-Server-NoSignatureAlgorithms",
2749 config: Config{
2750 Bugs: ProtocolBugs{
2751 NeverResumeOnRenego: true,
2752 NoSignatureAlgorithmsOnRenego: true,
2753 },
2754 },
2755 flags: []string{"-renegotiate"},
2756 shimWritesFirst: true,
2757 })
Adam Langley2ae77d22014-10-28 17:29:33 -07002758 // TODO(agl): test the renegotiation info SCSV.
Adam Langleycf2d4f42014-10-28 19:06:14 -07002759 testCases = append(testCases, testCase{
2760 name: "Renegotiate-Client",
2761 renegotiate: true,
2762 })
2763 testCases = append(testCases, testCase{
David Benjamincdea40c2015-03-19 14:09:43 -04002764 name: "Renegotiate-Client-Full",
2765 config: Config{
2766 Bugs: ProtocolBugs{
2767 NeverResumeOnRenego: true,
2768 },
2769 },
2770 renegotiate: true,
2771 })
2772 testCases = append(testCases, testCase{
Adam Langleycf2d4f42014-10-28 19:06:14 -07002773 name: "Renegotiate-Client-EmptyExt",
2774 renegotiate: true,
2775 config: Config{
2776 Bugs: ProtocolBugs{
2777 EmptyRenegotiationInfo: true,
2778 },
2779 },
2780 shouldFail: true,
2781 expectedError: ":RENEGOTIATION_MISMATCH:",
2782 })
2783 testCases = append(testCases, testCase{
2784 name: "Renegotiate-Client-BadExt",
2785 renegotiate: true,
2786 config: Config{
2787 Bugs: ProtocolBugs{
2788 BadRenegotiationInfo: true,
2789 },
2790 },
2791 shouldFail: true,
2792 expectedError: ":RENEGOTIATION_MISMATCH:",
2793 })
2794 testCases = append(testCases, testCase{
2795 name: "Renegotiate-Client-SwitchCiphers",
2796 renegotiate: true,
2797 config: Config{
2798 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2799 },
2800 renegotiateCiphers: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2801 })
2802 testCases = append(testCases, testCase{
2803 name: "Renegotiate-Client-SwitchCiphers2",
2804 renegotiate: true,
2805 config: Config{
2806 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2807 },
2808 renegotiateCiphers: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2809 })
David Benjaminc44b1df2014-11-23 12:11:01 -05002810 testCases = append(testCases, testCase{
2811 name: "Renegotiate-SameClientVersion",
2812 renegotiate: true,
2813 config: Config{
2814 MaxVersion: VersionTLS10,
2815 Bugs: ProtocolBugs{
2816 RequireSameRenegoClientVersion: true,
2817 },
2818 },
2819 })
Adam Langley2ae77d22014-10-28 17:29:33 -07002820}
2821
David Benjamin5e961c12014-11-07 01:48:35 -05002822func addDTLSReplayTests() {
2823 // Test that sequence number replays are detected.
2824 testCases = append(testCases, testCase{
2825 protocol: dtls,
2826 name: "DTLS-Replay",
2827 replayWrites: true,
2828 })
2829
2830 // Test the outgoing sequence number skipping by values larger
2831 // than the retransmit window.
2832 testCases = append(testCases, testCase{
2833 protocol: dtls,
2834 name: "DTLS-Replay-LargeGaps",
2835 config: Config{
2836 Bugs: ProtocolBugs{
2837 SequenceNumberIncrement: 127,
2838 },
2839 },
2840 replayWrites: true,
2841 })
2842}
2843
Feng Lu41aa3252014-11-21 22:47:56 -08002844func addFastRadioPaddingTests() {
David Benjamin1e29a6b2014-12-10 02:27:24 -05002845 testCases = append(testCases, testCase{
2846 protocol: tls,
2847 name: "FastRadio-Padding",
Feng Lu41aa3252014-11-21 22:47:56 -08002848 config: Config{
2849 Bugs: ProtocolBugs{
2850 RequireFastradioPadding: true,
2851 },
2852 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05002853 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08002854 })
David Benjamin1e29a6b2014-12-10 02:27:24 -05002855 testCases = append(testCases, testCase{
2856 protocol: dtls,
David Benjamin5f237bc2015-02-11 17:14:15 -05002857 name: "FastRadio-Padding-DTLS",
Feng Lu41aa3252014-11-21 22:47:56 -08002858 config: Config{
2859 Bugs: ProtocolBugs{
2860 RequireFastradioPadding: true,
2861 },
2862 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05002863 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08002864 })
2865}
2866
David Benjamin000800a2014-11-14 01:43:59 -05002867var testHashes = []struct {
2868 name string
2869 id uint8
2870}{
2871 {"SHA1", hashSHA1},
2872 {"SHA224", hashSHA224},
2873 {"SHA256", hashSHA256},
2874 {"SHA384", hashSHA384},
2875 {"SHA512", hashSHA512},
2876}
2877
2878func addSigningHashTests() {
2879 // Make sure each hash works. Include some fake hashes in the list and
2880 // ensure they're ignored.
2881 for _, hash := range testHashes {
2882 testCases = append(testCases, testCase{
2883 name: "SigningHash-ClientAuth-" + hash.name,
2884 config: Config{
2885 ClientAuth: RequireAnyClientCert,
2886 SignatureAndHashes: []signatureAndHash{
2887 {signatureRSA, 42},
2888 {signatureRSA, hash.id},
2889 {signatureRSA, 255},
2890 },
2891 },
2892 flags: []string{
2893 "-cert-file", rsaCertificateFile,
2894 "-key-file", rsaKeyFile,
2895 },
2896 })
2897
2898 testCases = append(testCases, testCase{
2899 testType: serverTest,
2900 name: "SigningHash-ServerKeyExchange-Sign-" + hash.name,
2901 config: Config{
2902 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2903 SignatureAndHashes: []signatureAndHash{
2904 {signatureRSA, 42},
2905 {signatureRSA, hash.id},
2906 {signatureRSA, 255},
2907 },
2908 },
2909 })
2910 }
2911
2912 // Test that hash resolution takes the signature type into account.
2913 testCases = append(testCases, testCase{
2914 name: "SigningHash-ClientAuth-SignatureType",
2915 config: Config{
2916 ClientAuth: RequireAnyClientCert,
2917 SignatureAndHashes: []signatureAndHash{
2918 {signatureECDSA, hashSHA512},
2919 {signatureRSA, hashSHA384},
2920 {signatureECDSA, hashSHA1},
2921 },
2922 },
2923 flags: []string{
2924 "-cert-file", rsaCertificateFile,
2925 "-key-file", rsaKeyFile,
2926 },
2927 })
2928
2929 testCases = append(testCases, testCase{
2930 testType: serverTest,
2931 name: "SigningHash-ServerKeyExchange-SignatureType",
2932 config: Config{
2933 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2934 SignatureAndHashes: []signatureAndHash{
2935 {signatureECDSA, hashSHA512},
2936 {signatureRSA, hashSHA384},
2937 {signatureECDSA, hashSHA1},
2938 },
2939 },
2940 })
2941
2942 // Test that, if the list is missing, the peer falls back to SHA-1.
2943 testCases = append(testCases, testCase{
2944 name: "SigningHash-ClientAuth-Fallback",
2945 config: Config{
2946 ClientAuth: RequireAnyClientCert,
2947 SignatureAndHashes: []signatureAndHash{
2948 {signatureRSA, hashSHA1},
2949 },
2950 Bugs: ProtocolBugs{
2951 NoSignatureAndHashes: true,
2952 },
2953 },
2954 flags: []string{
2955 "-cert-file", rsaCertificateFile,
2956 "-key-file", rsaKeyFile,
2957 },
2958 })
2959
2960 testCases = append(testCases, testCase{
2961 testType: serverTest,
2962 name: "SigningHash-ServerKeyExchange-Fallback",
2963 config: Config{
2964 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2965 SignatureAndHashes: []signatureAndHash{
2966 {signatureRSA, hashSHA1},
2967 },
2968 Bugs: ProtocolBugs{
2969 NoSignatureAndHashes: true,
2970 },
2971 },
2972 })
David Benjamin72dc7832015-03-16 17:49:43 -04002973
2974 // Test that hash preferences are enforced. BoringSSL defaults to
2975 // rejecting MD5 signatures.
2976 testCases = append(testCases, testCase{
2977 testType: serverTest,
2978 name: "SigningHash-ClientAuth-Enforced",
2979 config: Config{
2980 Certificates: []Certificate{rsaCertificate},
2981 SignatureAndHashes: []signatureAndHash{
2982 {signatureRSA, hashMD5},
2983 // Advertise SHA-1 so the handshake will
2984 // proceed, but the shim's preferences will be
2985 // ignored in CertificateVerify generation, so
2986 // MD5 will be chosen.
2987 {signatureRSA, hashSHA1},
2988 },
2989 Bugs: ProtocolBugs{
2990 IgnorePeerSignatureAlgorithmPreferences: true,
2991 },
2992 },
2993 flags: []string{"-require-any-client-certificate"},
2994 shouldFail: true,
2995 expectedError: ":WRONG_SIGNATURE_TYPE:",
2996 })
2997
2998 testCases = append(testCases, testCase{
2999 name: "SigningHash-ServerKeyExchange-Enforced",
3000 config: Config{
3001 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
3002 SignatureAndHashes: []signatureAndHash{
3003 {signatureRSA, hashMD5},
3004 },
3005 Bugs: ProtocolBugs{
3006 IgnorePeerSignatureAlgorithmPreferences: true,
3007 },
3008 },
3009 shouldFail: true,
3010 expectedError: ":WRONG_SIGNATURE_TYPE:",
3011 })
David Benjamin000800a2014-11-14 01:43:59 -05003012}
3013
David Benjamin83f90402015-01-27 01:09:43 -05003014// timeouts is the retransmit schedule for BoringSSL. It doubles and
3015// caps at 60 seconds. On the 13th timeout, it gives up.
3016var timeouts = []time.Duration{
3017 1 * time.Second,
3018 2 * time.Second,
3019 4 * time.Second,
3020 8 * time.Second,
3021 16 * time.Second,
3022 32 * time.Second,
3023 60 * time.Second,
3024 60 * time.Second,
3025 60 * time.Second,
3026 60 * time.Second,
3027 60 * time.Second,
3028 60 * time.Second,
3029 60 * time.Second,
3030}
3031
3032func addDTLSRetransmitTests() {
3033 // Test that this is indeed the timeout schedule. Stress all
3034 // four patterns of handshake.
3035 for i := 1; i < len(timeouts); i++ {
3036 number := strconv.Itoa(i)
3037 testCases = append(testCases, testCase{
3038 protocol: dtls,
3039 name: "DTLS-Retransmit-Client-" + number,
3040 config: Config{
3041 Bugs: ProtocolBugs{
3042 TimeoutSchedule: timeouts[:i],
3043 },
3044 },
3045 resumeSession: true,
3046 flags: []string{"-async"},
3047 })
3048 testCases = append(testCases, testCase{
3049 protocol: dtls,
3050 testType: serverTest,
3051 name: "DTLS-Retransmit-Server-" + number,
3052 config: Config{
3053 Bugs: ProtocolBugs{
3054 TimeoutSchedule: timeouts[:i],
3055 },
3056 },
3057 resumeSession: true,
3058 flags: []string{"-async"},
3059 })
3060 }
3061
3062 // Test that exceeding the timeout schedule hits a read
3063 // timeout.
3064 testCases = append(testCases, testCase{
3065 protocol: dtls,
3066 name: "DTLS-Retransmit-Timeout",
3067 config: Config{
3068 Bugs: ProtocolBugs{
3069 TimeoutSchedule: timeouts,
3070 },
3071 },
3072 resumeSession: true,
3073 flags: []string{"-async"},
3074 shouldFail: true,
3075 expectedError: ":READ_TIMEOUT_EXPIRED:",
3076 })
3077
3078 // Test that timeout handling has a fudge factor, due to API
3079 // problems.
3080 testCases = append(testCases, testCase{
3081 protocol: dtls,
3082 name: "DTLS-Retransmit-Fudge",
3083 config: Config{
3084 Bugs: ProtocolBugs{
3085 TimeoutSchedule: []time.Duration{
3086 timeouts[0] - 10*time.Millisecond,
3087 },
3088 },
3089 },
3090 resumeSession: true,
3091 flags: []string{"-async"},
3092 })
David Benjamin7eaab4c2015-03-02 19:01:16 -05003093
3094 // Test that the final Finished retransmitting isn't
3095 // duplicated if the peer badly fragments everything.
3096 testCases = append(testCases, testCase{
3097 testType: serverTest,
3098 protocol: dtls,
3099 name: "DTLS-Retransmit-Fragmented",
3100 config: Config{
3101 Bugs: ProtocolBugs{
3102 TimeoutSchedule: []time.Duration{timeouts[0]},
3103 MaxHandshakeRecordLength: 2,
3104 },
3105 },
3106 flags: []string{"-async"},
3107 })
David Benjamin83f90402015-01-27 01:09:43 -05003108}
3109
David Benjamin884fdf12014-08-02 15:28:23 -04003110func worker(statusChan chan statusMsg, c chan *testCase, buildDir string, wg *sync.WaitGroup) {
Adam Langley95c29f32014-06-20 12:00:00 -07003111 defer wg.Done()
3112
3113 for test := range c {
Adam Langley69a01602014-11-17 17:26:55 -08003114 var err error
3115
3116 if *mallocTest < 0 {
3117 statusChan <- statusMsg{test: test, started: true}
3118 err = runTest(test, buildDir, -1)
3119 } else {
3120 for mallocNumToFail := int64(*mallocTest); ; mallocNumToFail++ {
3121 statusChan <- statusMsg{test: test, started: true}
3122 if err = runTest(test, buildDir, mallocNumToFail); err != errMoreMallocs {
3123 if err != nil {
3124 fmt.Printf("\n\nmalloc test failed at %d: %s\n", mallocNumToFail, err)
3125 }
3126 break
3127 }
3128 }
3129 }
Adam Langley95c29f32014-06-20 12:00:00 -07003130 statusChan <- statusMsg{test: test, err: err}
3131 }
3132}
3133
3134type statusMsg struct {
3135 test *testCase
3136 started bool
3137 err error
3138}
3139
David Benjamin5f237bc2015-02-11 17:14:15 -05003140func statusPrinter(doneChan chan *testOutput, statusChan chan statusMsg, total int) {
Adam Langley95c29f32014-06-20 12:00:00 -07003141 var started, done, failed, lineLen int
Adam Langley95c29f32014-06-20 12:00:00 -07003142
David Benjamin5f237bc2015-02-11 17:14:15 -05003143 testOutput := newTestOutput()
Adam Langley95c29f32014-06-20 12:00:00 -07003144 for msg := range statusChan {
David Benjamin5f237bc2015-02-11 17:14:15 -05003145 if !*pipe {
3146 // Erase the previous status line.
David Benjamin87c8a642015-02-21 01:54:29 -05003147 var erase string
3148 for i := 0; i < lineLen; i++ {
3149 erase += "\b \b"
3150 }
3151 fmt.Print(erase)
David Benjamin5f237bc2015-02-11 17:14:15 -05003152 }
3153
Adam Langley95c29f32014-06-20 12:00:00 -07003154 if msg.started {
3155 started++
3156 } else {
3157 done++
David Benjamin5f237bc2015-02-11 17:14:15 -05003158
3159 if msg.err != nil {
3160 fmt.Printf("FAILED (%s)\n%s\n", msg.test.name, msg.err)
3161 failed++
3162 testOutput.addResult(msg.test.name, "FAIL")
3163 } else {
3164 if *pipe {
3165 // Print each test instead of a status line.
3166 fmt.Printf("PASSED (%s)\n", msg.test.name)
3167 }
3168 testOutput.addResult(msg.test.name, "PASS")
3169 }
Adam Langley95c29f32014-06-20 12:00:00 -07003170 }
3171
David Benjamin5f237bc2015-02-11 17:14:15 -05003172 if !*pipe {
3173 // Print a new status line.
3174 line := fmt.Sprintf("%d/%d/%d/%d", failed, done, started, total)
3175 lineLen = len(line)
3176 os.Stdout.WriteString(line)
Adam Langley95c29f32014-06-20 12:00:00 -07003177 }
Adam Langley95c29f32014-06-20 12:00:00 -07003178 }
David Benjamin5f237bc2015-02-11 17:14:15 -05003179
3180 doneChan <- testOutput
Adam Langley95c29f32014-06-20 12:00:00 -07003181}
3182
3183func main() {
3184 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 -04003185 var flagNumWorkers *int = flag.Int("num-workers", runtime.NumCPU(), "The number of workers to run in parallel.")
David Benjamin884fdf12014-08-02 15:28:23 -04003186 var flagBuildDir *string = flag.String("build-dir", "../../../build", "The build directory to run the shim from.")
Adam Langley95c29f32014-06-20 12:00:00 -07003187
3188 flag.Parse()
3189
3190 addCipherSuiteTests()
3191 addBadECDSASignatureTests()
Adam Langley80842bd2014-06-20 12:00:00 -07003192 addCBCPaddingTests()
Kenny Root7fdeaf12014-08-05 15:23:37 -07003193 addCBCSplittingTests()
David Benjamin636293b2014-07-08 17:59:18 -04003194 addClientAuthTests()
Adam Langley524e7172015-02-20 16:04:00 -08003195 addDDoSCallbackTests()
David Benjamin7e2e6cf2014-08-07 17:44:24 -04003196 addVersionNegotiationTests()
David Benjaminaccb4542014-12-12 23:44:33 -05003197 addMinimumVersionTests()
David Benjamin5c24a1d2014-08-31 00:59:27 -04003198 addD5BugTests()
David Benjamine78bfde2014-09-06 12:45:15 -04003199 addExtensionTests()
David Benjamin01fe8202014-09-24 15:21:44 -04003200 addResumptionVersionTests()
Adam Langley75712922014-10-10 16:23:43 -07003201 addExtendedMasterSecretTests()
Adam Langley2ae77d22014-10-28 17:29:33 -07003202 addRenegotiationTests()
David Benjamin5e961c12014-11-07 01:48:35 -05003203 addDTLSReplayTests()
David Benjamin000800a2014-11-14 01:43:59 -05003204 addSigningHashTests()
Feng Lu41aa3252014-11-21 22:47:56 -08003205 addFastRadioPaddingTests()
David Benjamin83f90402015-01-27 01:09:43 -05003206 addDTLSRetransmitTests()
David Benjamin43ec06f2014-08-05 02:28:57 -04003207 for _, async := range []bool{false, true} {
3208 for _, splitHandshake := range []bool{false, true} {
David Benjamin6fd297b2014-08-11 18:43:38 -04003209 for _, protocol := range []protocol{tls, dtls} {
3210 addStateMachineCoverageTests(async, splitHandshake, protocol)
3211 }
David Benjamin43ec06f2014-08-05 02:28:57 -04003212 }
3213 }
Adam Langley95c29f32014-06-20 12:00:00 -07003214
3215 var wg sync.WaitGroup
3216
David Benjamin2bc8e6f2014-08-02 15:22:37 -04003217 numWorkers := *flagNumWorkers
Adam Langley95c29f32014-06-20 12:00:00 -07003218
3219 statusChan := make(chan statusMsg, numWorkers)
3220 testChan := make(chan *testCase, numWorkers)
David Benjamin5f237bc2015-02-11 17:14:15 -05003221 doneChan := make(chan *testOutput)
Adam Langley95c29f32014-06-20 12:00:00 -07003222
David Benjamin025b3d32014-07-01 19:53:04 -04003223 go statusPrinter(doneChan, statusChan, len(testCases))
Adam Langley95c29f32014-06-20 12:00:00 -07003224
3225 for i := 0; i < numWorkers; i++ {
3226 wg.Add(1)
David Benjamin884fdf12014-08-02 15:28:23 -04003227 go worker(statusChan, testChan, *flagBuildDir, &wg)
Adam Langley95c29f32014-06-20 12:00:00 -07003228 }
3229
David Benjamin025b3d32014-07-01 19:53:04 -04003230 for i := range testCases {
3231 if len(*flagTest) == 0 || *flagTest == testCases[i].name {
3232 testChan <- &testCases[i]
Adam Langley95c29f32014-06-20 12:00:00 -07003233 }
3234 }
3235
3236 close(testChan)
3237 wg.Wait()
3238 close(statusChan)
David Benjamin5f237bc2015-02-11 17:14:15 -05003239 testOutput := <-doneChan
Adam Langley95c29f32014-06-20 12:00:00 -07003240
3241 fmt.Printf("\n")
David Benjamin5f237bc2015-02-11 17:14:15 -05003242
3243 if *jsonOutput != "" {
3244 if err := testOutput.writeTo(*jsonOutput); err != nil {
3245 fmt.Fprintf(os.Stderr, "Error: %s\n", err)
3246 }
3247 }
Adam Langley95c29f32014-06-20 12:00:00 -07003248}