blob: b97bc421a9d8832246d0b016fe2113279abca72d [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001package main
2
3import (
4 "bytes"
David Benjamina08e49d2014-08-24 01:46:07 -04005 "crypto/ecdsa"
6 "crypto/elliptic"
David Benjamin407a10c2014-07-16 12:58:59 -04007 "crypto/x509"
David Benjamin2561dc32014-08-24 01:25:27 -04008 "encoding/base64"
David Benjamina08e49d2014-08-24 01:46:07 -04009 "encoding/pem"
Adam Langley95c29f32014-06-20 12:00:00 -070010 "flag"
11 "fmt"
12 "io"
Kenny Root7fdeaf12014-08-05 15:23:37 -070013 "io/ioutil"
Adam Langley95c29f32014-06-20 12:00:00 -070014 "net"
15 "os"
16 "os/exec"
David Benjamin884fdf12014-08-02 15:28:23 -040017 "path"
David Benjamin2bc8e6f2014-08-02 15:22:37 -040018 "runtime"
Adam Langley69a01602014-11-17 17:26:55 -080019 "strconv"
Adam Langley95c29f32014-06-20 12:00:00 -070020 "strings"
21 "sync"
22 "syscall"
David Benjamin83f90402015-01-27 01:09:43 -050023 "time"
Adam Langley95c29f32014-06-20 12:00:00 -070024)
25
Adam Langley69a01602014-11-17 17:26:55 -080026var (
27 useValgrind = flag.Bool("valgrind", false, "If true, run code under valgrind")
28 useGDB = flag.Bool("gdb", false, "If true, run BoringSSL code under gdb")
29 flagDebug *bool = flag.Bool("debug", false, "Hexdump the contents of the connection")
30 mallocTest *int64 = flag.Int64("malloc-test", -1, "If non-negative, run each test with each malloc in turn failing from the given number onwards.")
31 mallocTestDebug *bool = flag.Bool("malloc-test-debug", false, "If true, ask bssl_shim to abort rather than fail a malloc. This can be used with a specific value for --malloc-test to identity the malloc failing that is causing problems.")
32)
Adam Langley95c29f32014-06-20 12:00:00 -070033
David Benjamin025b3d32014-07-01 19:53:04 -040034const (
35 rsaCertificateFile = "cert.pem"
36 ecdsaCertificateFile = "ecdsa_cert.pem"
37)
38
39const (
David Benjamina08e49d2014-08-24 01:46:07 -040040 rsaKeyFile = "key.pem"
41 ecdsaKeyFile = "ecdsa_key.pem"
42 channelIDKeyFile = "channel_id_key.pem"
David Benjamin025b3d32014-07-01 19:53:04 -040043)
44
Adam Langley95c29f32014-06-20 12:00:00 -070045var rsaCertificate, ecdsaCertificate Certificate
David Benjamina08e49d2014-08-24 01:46:07 -040046var channelIDKey *ecdsa.PrivateKey
47var channelIDBytes []byte
Adam Langley95c29f32014-06-20 12:00:00 -070048
David Benjamin61f95272014-11-25 01:55:35 -050049var testOCSPResponse = []byte{1, 2, 3, 4}
50var testSCTList = []byte{5, 6, 7, 8}
51
Adam Langley95c29f32014-06-20 12:00:00 -070052func initCertificates() {
53 var err error
David Benjamin025b3d32014-07-01 19:53:04 -040054 rsaCertificate, err = LoadX509KeyPair(rsaCertificateFile, rsaKeyFile)
Adam Langley95c29f32014-06-20 12:00:00 -070055 if err != nil {
56 panic(err)
57 }
David Benjamin61f95272014-11-25 01:55:35 -050058 rsaCertificate.OCSPStaple = testOCSPResponse
59 rsaCertificate.SignedCertificateTimestampList = testSCTList
Adam Langley95c29f32014-06-20 12:00:00 -070060
David Benjamin025b3d32014-07-01 19:53:04 -040061 ecdsaCertificate, err = LoadX509KeyPair(ecdsaCertificateFile, ecdsaKeyFile)
Adam Langley95c29f32014-06-20 12:00:00 -070062 if err != nil {
63 panic(err)
64 }
David Benjamin61f95272014-11-25 01:55:35 -050065 ecdsaCertificate.OCSPStaple = testOCSPResponse
66 ecdsaCertificate.SignedCertificateTimestampList = testSCTList
David Benjamina08e49d2014-08-24 01:46:07 -040067
68 channelIDPEMBlock, err := ioutil.ReadFile(channelIDKeyFile)
69 if err != nil {
70 panic(err)
71 }
72 channelIDDERBlock, _ := pem.Decode(channelIDPEMBlock)
73 if channelIDDERBlock.Type != "EC PRIVATE KEY" {
74 panic("bad key type")
75 }
76 channelIDKey, err = x509.ParseECPrivateKey(channelIDDERBlock.Bytes)
77 if err != nil {
78 panic(err)
79 }
80 if channelIDKey.Curve != elliptic.P256() {
81 panic("bad curve")
82 }
83
84 channelIDBytes = make([]byte, 64)
85 writeIntPadded(channelIDBytes[:32], channelIDKey.X)
86 writeIntPadded(channelIDBytes[32:], channelIDKey.Y)
Adam Langley95c29f32014-06-20 12:00:00 -070087}
88
89var certificateOnce sync.Once
90
91func getRSACertificate() Certificate {
92 certificateOnce.Do(initCertificates)
93 return rsaCertificate
94}
95
96func getECDSACertificate() Certificate {
97 certificateOnce.Do(initCertificates)
98 return ecdsaCertificate
99}
100
David Benjamin025b3d32014-07-01 19:53:04 -0400101type testType int
102
103const (
104 clientTest testType = iota
105 serverTest
106)
107
David Benjamin6fd297b2014-08-11 18:43:38 -0400108type protocol int
109
110const (
111 tls protocol = iota
112 dtls
113)
114
David Benjaminfc7b0862014-09-06 13:21:53 -0400115const (
116 alpn = 1
117 npn = 2
118)
119
Adam Langley95c29f32014-06-20 12:00:00 -0700120type testCase struct {
David Benjamin025b3d32014-07-01 19:53:04 -0400121 testType testType
David Benjamin6fd297b2014-08-11 18:43:38 -0400122 protocol protocol
Adam Langley95c29f32014-06-20 12:00:00 -0700123 name string
124 config Config
125 shouldFail bool
126 expectedError string
Adam Langleyac61fa32014-06-23 12:03:11 -0700127 // expectedLocalError, if not empty, contains a substring that must be
128 // found in the local error.
129 expectedLocalError string
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400130 // expectedVersion, if non-zero, specifies the TLS version that must be
131 // negotiated.
132 expectedVersion uint16
David Benjamin01fe8202014-09-24 15:21:44 -0400133 // expectedResumeVersion, if non-zero, specifies the TLS version that
134 // must be negotiated on resumption. If zero, expectedVersion is used.
135 expectedResumeVersion uint16
David Benjamina08e49d2014-08-24 01:46:07 -0400136 // expectChannelID controls whether the connection should have
137 // negotiated a Channel ID with channelIDKey.
138 expectChannelID bool
David Benjaminae2888f2014-09-06 12:58:58 -0400139 // expectedNextProto controls whether the connection should
140 // negotiate a next protocol via NPN or ALPN.
141 expectedNextProto string
David Benjaminfc7b0862014-09-06 13:21:53 -0400142 // expectedNextProtoType, if non-zero, is the expected next
143 // protocol negotiation mechanism.
144 expectedNextProtoType int
David Benjaminca6c8262014-11-15 19:06:08 -0500145 // expectedSRTPProtectionProfile is the DTLS-SRTP profile that
146 // should be negotiated. If zero, none should be negotiated.
147 expectedSRTPProtectionProfile uint16
Adam Langley80842bd2014-06-20 12:00:00 -0700148 // messageLen is the length, in bytes, of the test message that will be
149 // sent.
150 messageLen int
David Benjamin025b3d32014-07-01 19:53:04 -0400151 // certFile is the path to the certificate to use for the server.
152 certFile string
153 // keyFile is the path to the private key to use for the server.
154 keyFile string
David Benjamin1d5c83e2014-07-22 19:20:02 -0400155 // resumeSession controls whether a second connection should be tested
David Benjamin01fe8202014-09-24 15:21:44 -0400156 // which attempts to resume the first session.
David Benjamin1d5c83e2014-07-22 19:20:02 -0400157 resumeSession bool
David Benjamin01fe8202014-09-24 15:21:44 -0400158 // resumeConfig, if not nil, points to a Config to be used on
David Benjaminfe8eb9a2014-11-17 03:19:02 -0500159 // resumption. Unless newSessionsOnResume is set,
160 // SessionTicketKey, ServerSessionCache, and
161 // ClientSessionCache are copied from the initial connection's
162 // config. If nil, the initial connection's config is used.
David Benjamin01fe8202014-09-24 15:21:44 -0400163 resumeConfig *Config
David Benjaminfe8eb9a2014-11-17 03:19:02 -0500164 // newSessionsOnResume, if true, will cause resumeConfig to
165 // use a different session resumption context.
166 newSessionsOnResume bool
David Benjamin98e882e2014-08-08 13:24:34 -0400167 // sendPrefix sends a prefix on the socket before actually performing a
168 // handshake.
169 sendPrefix string
David Benjamine58c4f52014-08-24 03:47:07 -0400170 // shimWritesFirst controls whether the shim sends an initial "hello"
171 // message before doing a roundtrip with the runner.
172 shimWritesFirst bool
Adam Langleycf2d4f42014-10-28 19:06:14 -0700173 // renegotiate indicates the the connection should be renegotiated
174 // during the exchange.
175 renegotiate bool
176 // renegotiateCiphers is a list of ciphersuite ids that will be
177 // switched in just before renegotiation.
178 renegotiateCiphers []uint16
David Benjamin5e961c12014-11-07 01:48:35 -0500179 // replayWrites, if true, configures the underlying transport
180 // to replay every write it makes in DTLS tests.
181 replayWrites bool
David Benjamin5fa3eba2015-01-22 16:35:40 -0500182 // damageFirstWrite, if true, configures the underlying transport to
183 // damage the final byte of the first application data write.
184 damageFirstWrite bool
David Benjamin325b5c32014-07-01 19:40:31 -0400185 // flags, if not empty, contains a list of command-line flags that will
186 // be passed to the shim program.
187 flags []string
Adam Langley95c29f32014-06-20 12:00:00 -0700188}
189
David Benjamin025b3d32014-07-01 19:53:04 -0400190var testCases = []testCase{
Adam Langley95c29f32014-06-20 12:00:00 -0700191 {
192 name: "BadRSASignature",
193 config: Config{
194 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
195 Bugs: ProtocolBugs{
196 InvalidSKXSignature: true,
197 },
198 },
199 shouldFail: true,
200 expectedError: ":BAD_SIGNATURE:",
201 },
202 {
203 name: "BadECDSASignature",
204 config: Config{
205 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
206 Bugs: ProtocolBugs{
207 InvalidSKXSignature: true,
208 },
209 Certificates: []Certificate{getECDSACertificate()},
210 },
211 shouldFail: true,
212 expectedError: ":BAD_SIGNATURE:",
213 },
214 {
215 name: "BadECDSACurve",
216 config: Config{
217 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
218 Bugs: ProtocolBugs{
219 InvalidSKXCurve: true,
220 },
221 Certificates: []Certificate{getECDSACertificate()},
222 },
223 shouldFail: true,
224 expectedError: ":WRONG_CURVE:",
225 },
Adam Langleyac61fa32014-06-23 12:03:11 -0700226 {
David Benjamina8e3e0e2014-08-06 22:11:10 -0400227 testType: serverTest,
228 name: "BadRSAVersion",
229 config: Config{
230 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
231 Bugs: ProtocolBugs{
232 RsaClientKeyExchangeVersion: VersionTLS11,
233 },
234 },
235 shouldFail: true,
236 expectedError: ":DECRYPTION_FAILED_OR_BAD_RECORD_MAC:",
237 },
238 {
David Benjamin325b5c32014-07-01 19:40:31 -0400239 name: "NoFallbackSCSV",
Adam Langleyac61fa32014-06-23 12:03:11 -0700240 config: Config{
241 Bugs: ProtocolBugs{
242 FailIfNotFallbackSCSV: true,
243 },
244 },
245 shouldFail: true,
246 expectedLocalError: "no fallback SCSV found",
247 },
David Benjamin325b5c32014-07-01 19:40:31 -0400248 {
David Benjamin2a0c4962014-08-22 23:46:35 -0400249 name: "SendFallbackSCSV",
David Benjamin325b5c32014-07-01 19:40:31 -0400250 config: Config{
251 Bugs: ProtocolBugs{
252 FailIfNotFallbackSCSV: true,
253 },
254 },
255 flags: []string{"-fallback-scsv"},
256 },
David Benjamin197b3ab2014-07-02 18:37:33 -0400257 {
David Benjamin7b030512014-07-08 17:30:11 -0400258 name: "ClientCertificateTypes",
259 config: Config{
260 ClientAuth: RequestClientCert,
261 ClientCertificateTypes: []byte{
262 CertTypeDSSSign,
263 CertTypeRSASign,
264 CertTypeECDSASign,
265 },
266 },
David Benjamin2561dc32014-08-24 01:25:27 -0400267 flags: []string{
268 "-expect-certificate-types",
269 base64.StdEncoding.EncodeToString([]byte{
270 CertTypeDSSSign,
271 CertTypeRSASign,
272 CertTypeECDSASign,
273 }),
274 },
David Benjamin7b030512014-07-08 17:30:11 -0400275 },
David Benjamin636293b2014-07-08 17:59:18 -0400276 {
277 name: "NoClientCertificate",
278 config: Config{
279 ClientAuth: RequireAnyClientCert,
280 },
281 shouldFail: true,
282 expectedLocalError: "client didn't provide a certificate",
283 },
David Benjamin1c375dd2014-07-12 00:48:23 -0400284 {
285 name: "UnauthenticatedECDH",
286 config: Config{
287 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
288 Bugs: ProtocolBugs{
289 UnauthenticatedECDH: true,
290 },
291 },
292 shouldFail: true,
David Benjamine8f3d662014-07-12 01:10:19 -0400293 expectedError: ":UNEXPECTED_MESSAGE:",
David Benjamin1c375dd2014-07-12 00:48:23 -0400294 },
David Benjamin9c651c92014-07-12 13:27:45 -0400295 {
296 name: "SkipServerKeyExchange",
297 config: Config{
298 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
299 Bugs: ProtocolBugs{
300 SkipServerKeyExchange: true,
301 },
302 },
303 shouldFail: true,
304 expectedError: ":UNEXPECTED_MESSAGE:",
305 },
David Benjamin1f5f62b2014-07-12 16:18:02 -0400306 {
David Benjamina0e52232014-07-19 17:39:58 -0400307 name: "SkipChangeCipherSpec-Client",
308 config: Config{
309 Bugs: ProtocolBugs{
310 SkipChangeCipherSpec: true,
311 },
312 },
313 shouldFail: true,
David Benjamin86271ee2014-07-21 16:14:03 -0400314 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
David Benjamina0e52232014-07-19 17:39:58 -0400315 },
316 {
317 testType: serverTest,
318 name: "SkipChangeCipherSpec-Server",
319 config: Config{
320 Bugs: ProtocolBugs{
321 SkipChangeCipherSpec: true,
322 },
323 },
324 shouldFail: true,
David Benjamin86271ee2014-07-21 16:14:03 -0400325 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
David Benjamina0e52232014-07-19 17:39:58 -0400326 },
David Benjamin42be6452014-07-21 14:50:23 -0400327 {
328 testType: serverTest,
329 name: "SkipChangeCipherSpec-Server-NPN",
330 config: Config{
331 NextProtos: []string{"bar"},
332 Bugs: ProtocolBugs{
333 SkipChangeCipherSpec: true,
334 },
335 },
336 flags: []string{
337 "-advertise-npn", "\x03foo\x03bar\x03baz",
338 },
339 shouldFail: true,
David Benjamin86271ee2014-07-21 16:14:03 -0400340 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
341 },
342 {
343 name: "FragmentAcrossChangeCipherSpec-Client",
344 config: Config{
345 Bugs: ProtocolBugs{
346 FragmentAcrossChangeCipherSpec: true,
347 },
348 },
349 shouldFail: true,
350 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
351 },
352 {
353 testType: serverTest,
354 name: "FragmentAcrossChangeCipherSpec-Server",
355 config: Config{
356 Bugs: ProtocolBugs{
357 FragmentAcrossChangeCipherSpec: true,
358 },
359 },
360 shouldFail: true,
361 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
362 },
363 {
364 testType: serverTest,
365 name: "FragmentAcrossChangeCipherSpec-Server-NPN",
366 config: Config{
367 NextProtos: []string{"bar"},
368 Bugs: ProtocolBugs{
369 FragmentAcrossChangeCipherSpec: true,
370 },
371 },
372 flags: []string{
373 "-advertise-npn", "\x03foo\x03bar\x03baz",
374 },
375 shouldFail: true,
376 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
David Benjamin42be6452014-07-21 14:50:23 -0400377 },
David Benjaminf3ec83d2014-07-21 22:42:34 -0400378 {
379 testType: serverTest,
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400380 name: "FragmentAlert",
381 config: Config{
382 Bugs: ProtocolBugs{
David Benjaminca6c8262014-11-15 19:06:08 -0500383 FragmentAlert: true,
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400384 SendSpuriousAlert: true,
385 },
386 },
387 shouldFail: true,
388 expectedError: ":BAD_ALERT:",
389 },
390 {
391 testType: serverTest,
David Benjaminf3ec83d2014-07-21 22:42:34 -0400392 name: "EarlyChangeCipherSpec-server-1",
393 config: Config{
394 Bugs: ProtocolBugs{
395 EarlyChangeCipherSpec: 1,
396 },
397 },
398 shouldFail: true,
399 expectedError: ":CCS_RECEIVED_EARLY:",
400 },
401 {
402 testType: serverTest,
403 name: "EarlyChangeCipherSpec-server-2",
404 config: Config{
405 Bugs: ProtocolBugs{
406 EarlyChangeCipherSpec: 2,
407 },
408 },
409 shouldFail: true,
410 expectedError: ":CCS_RECEIVED_EARLY:",
411 },
David Benjamind23f4122014-07-23 15:09:48 -0400412 {
David Benjamind23f4122014-07-23 15:09:48 -0400413 name: "SkipNewSessionTicket",
414 config: Config{
415 Bugs: ProtocolBugs{
416 SkipNewSessionTicket: true,
417 },
418 },
419 shouldFail: true,
420 expectedError: ":CCS_RECEIVED_EARLY:",
421 },
David Benjamin7e3305e2014-07-28 14:52:32 -0400422 {
David Benjamind86c7672014-08-02 04:07:12 -0400423 testType: serverTest,
David Benjaminbef270a2014-08-02 04:22:02 -0400424 name: "FallbackSCSV",
425 config: Config{
426 MaxVersion: VersionTLS11,
427 Bugs: ProtocolBugs{
428 SendFallbackSCSV: true,
429 },
430 },
431 shouldFail: true,
432 expectedError: ":INAPPROPRIATE_FALLBACK:",
433 },
434 {
435 testType: serverTest,
436 name: "FallbackSCSV-VersionMatch",
437 config: Config{
438 Bugs: ProtocolBugs{
439 SendFallbackSCSV: true,
440 },
441 },
442 },
David Benjamin98214542014-08-07 18:02:39 -0400443 {
444 testType: serverTest,
445 name: "FragmentedClientVersion",
446 config: Config{
447 Bugs: ProtocolBugs{
448 MaxHandshakeRecordLength: 1,
449 FragmentClientVersion: true,
450 },
451 },
David Benjamin82c9e902014-12-12 15:55:27 -0500452 expectedVersion: VersionTLS12,
David Benjamin98214542014-08-07 18:02:39 -0400453 },
David Benjamin98e882e2014-08-08 13:24:34 -0400454 {
455 testType: serverTest,
456 name: "MinorVersionTolerance",
457 config: Config{
458 Bugs: ProtocolBugs{
459 SendClientVersion: 0x03ff,
460 },
461 },
462 expectedVersion: VersionTLS12,
463 },
464 {
465 testType: serverTest,
466 name: "MajorVersionTolerance",
467 config: Config{
468 Bugs: ProtocolBugs{
469 SendClientVersion: 0x0400,
470 },
471 },
472 expectedVersion: VersionTLS12,
473 },
474 {
475 testType: serverTest,
476 name: "VersionTooLow",
477 config: Config{
478 Bugs: ProtocolBugs{
479 SendClientVersion: 0x0200,
480 },
481 },
482 shouldFail: true,
483 expectedError: ":UNSUPPORTED_PROTOCOL:",
484 },
485 {
486 testType: serverTest,
487 name: "HttpGET",
488 sendPrefix: "GET / HTTP/1.0\n",
489 shouldFail: true,
490 expectedError: ":HTTP_REQUEST:",
491 },
492 {
493 testType: serverTest,
494 name: "HttpPOST",
495 sendPrefix: "POST / HTTP/1.0\n",
496 shouldFail: true,
497 expectedError: ":HTTP_REQUEST:",
498 },
499 {
500 testType: serverTest,
501 name: "HttpHEAD",
502 sendPrefix: "HEAD / HTTP/1.0\n",
503 shouldFail: true,
504 expectedError: ":HTTP_REQUEST:",
505 },
506 {
507 testType: serverTest,
508 name: "HttpPUT",
509 sendPrefix: "PUT / HTTP/1.0\n",
510 shouldFail: true,
511 expectedError: ":HTTP_REQUEST:",
512 },
513 {
514 testType: serverTest,
515 name: "HttpCONNECT",
516 sendPrefix: "CONNECT www.google.com:443 HTTP/1.0\n",
517 shouldFail: true,
518 expectedError: ":HTTPS_PROXY_REQUEST:",
519 },
David Benjamin39ebf532014-08-31 02:23:49 -0400520 {
David Benjaminf080ecd2014-12-11 18:48:59 -0500521 testType: serverTest,
522 name: "Garbage",
523 sendPrefix: "blah",
524 shouldFail: true,
525 expectedError: ":UNKNOWN_PROTOCOL:",
526 },
527 {
David Benjamin39ebf532014-08-31 02:23:49 -0400528 name: "SkipCipherVersionCheck",
529 config: Config{
530 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
531 MaxVersion: VersionTLS11,
532 Bugs: ProtocolBugs{
533 SkipCipherVersionCheck: true,
534 },
535 },
536 shouldFail: true,
537 expectedError: ":WRONG_CIPHER_RETURNED:",
538 },
David Benjamin9114fae2014-11-08 11:41:14 -0500539 {
540 name: "RSAServerKeyExchange",
541 config: Config{
542 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
543 Bugs: ProtocolBugs{
544 RSAServerKeyExchange: true,
545 },
546 },
547 shouldFail: true,
548 expectedError: ":UNEXPECTED_MESSAGE:",
549 },
David Benjamin128dbc32014-12-01 01:27:42 -0500550 {
551 name: "DisableEverything",
552 flags: []string{"-no-tls12", "-no-tls11", "-no-tls1", "-no-ssl3"},
553 shouldFail: true,
554 expectedError: ":WRONG_SSL_VERSION:",
555 },
556 {
557 protocol: dtls,
558 name: "DisableEverything-DTLS",
559 flags: []string{"-no-tls12", "-no-tls1"},
560 shouldFail: true,
561 expectedError: ":WRONG_SSL_VERSION:",
562 },
David Benjamin780d6dd2015-01-06 12:03:19 -0500563 {
564 name: "NoSharedCipher",
565 config: Config{
566 CipherSuites: []uint16{},
567 },
568 shouldFail: true,
569 expectedError: ":HANDSHAKE_FAILURE_ON_CLIENT_HELLO:",
570 },
David Benjamin13be1de2015-01-11 16:29:36 -0500571 {
572 protocol: dtls,
573 testType: serverTest,
574 name: "MTU",
575 config: Config{
576 Bugs: ProtocolBugs{
577 MaxPacketLength: 256,
578 },
579 },
580 flags: []string{"-mtu", "256"},
581 },
582 {
583 protocol: dtls,
584 testType: serverTest,
585 name: "MTUExceeded",
586 config: Config{
587 Bugs: ProtocolBugs{
588 MaxPacketLength: 255,
589 },
590 },
591 flags: []string{"-mtu", "256"},
592 shouldFail: true,
593 expectedLocalError: "dtls: exceeded maximum packet length",
594 },
David Benjamin6095de82014-12-27 01:50:38 -0500595 {
596 name: "CertMismatchRSA",
597 config: Config{
598 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
599 Certificates: []Certificate{getECDSACertificate()},
600 Bugs: ProtocolBugs{
601 SendCipherSuite: TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
602 },
603 },
604 shouldFail: true,
605 expectedError: ":WRONG_CERTIFICATE_TYPE:",
606 },
607 {
608 name: "CertMismatchECDSA",
609 config: Config{
610 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
611 Certificates: []Certificate{getRSACertificate()},
612 Bugs: ProtocolBugs{
613 SendCipherSuite: TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
614 },
615 },
616 shouldFail: true,
617 expectedError: ":WRONG_CERTIFICATE_TYPE:",
618 },
David Benjamin5fa3eba2015-01-22 16:35:40 -0500619 {
620 name: "TLSFatalBadPackets",
621 damageFirstWrite: true,
622 shouldFail: true,
623 expectedError: ":DECRYPTION_FAILED_OR_BAD_RECORD_MAC:",
624 },
625 {
626 protocol: dtls,
627 name: "DTLSIgnoreBadPackets",
628 damageFirstWrite: true,
629 },
630 {
631 protocol: dtls,
632 name: "DTLSIgnoreBadPackets-Async",
633 damageFirstWrite: true,
634 flags: []string{"-async"},
635 },
David Benjamin4189bd92015-01-25 23:52:39 -0500636 {
637 name: "AppDataAfterChangeCipherSpec",
638 config: Config{
639 Bugs: ProtocolBugs{
640 AppDataAfterChangeCipherSpec: []byte("TEST MESSAGE"),
641 },
642 },
643 shouldFail: true,
644 expectedError: ":DATA_BETWEEN_CCS_AND_FINISHED:",
645 },
646 {
647 protocol: dtls,
648 name: "AppDataAfterChangeCipherSpec-DTLS",
649 config: Config{
650 Bugs: ProtocolBugs{
651 AppDataAfterChangeCipherSpec: []byte("TEST MESSAGE"),
652 },
653 },
654 },
David Benjaminb3774b92015-01-31 17:16:01 -0500655 {
656 protocol: dtls,
657 name: "ReorderHandshakeFragments-Small-DTLS",
658 config: Config{
659 Bugs: ProtocolBugs{
660 ReorderHandshakeFragments: true,
661 // Small enough that every handshake message is
662 // fragmented.
663 MaxHandshakeRecordLength: 2,
664 },
665 },
666 },
667 {
668 protocol: dtls,
669 name: "ReorderHandshakeFragments-Large-DTLS",
670 config: Config{
671 Bugs: ProtocolBugs{
672 ReorderHandshakeFragments: true,
673 // Large enough that no handshake message is
674 // fragmented.
675 //
676 // TODO(davidben): Also test interaction of
677 // complete handshake messages with
678 // fragments. The current logic is full of bugs
679 // here, so the reassembly logic needs a rewrite
680 // before those tests will pass.
681 MaxHandshakeRecordLength: 2048,
682 },
683 },
684 },
Adam Langley95c29f32014-06-20 12:00:00 -0700685}
686
David Benjamin01fe8202014-09-24 15:21:44 -0400687func doExchange(test *testCase, config *Config, conn net.Conn, messageLen int, isResume bool) error {
David Benjamin65ea8ff2014-11-23 03:01:00 -0500688 var connDebug *recordingConn
David Benjamin5fa3eba2015-01-22 16:35:40 -0500689 var connDamage *damageAdaptor
David Benjamin65ea8ff2014-11-23 03:01:00 -0500690 if *flagDebug {
691 connDebug = &recordingConn{Conn: conn}
692 conn = connDebug
693 defer func() {
694 connDebug.WriteTo(os.Stdout)
695 }()
696 }
697
David Benjamin6fd297b2014-08-11 18:43:38 -0400698 if test.protocol == dtls {
David Benjamin83f90402015-01-27 01:09:43 -0500699 config.Bugs.PacketAdaptor = newPacketAdaptor(conn)
700 conn = config.Bugs.PacketAdaptor
David Benjamin5e961c12014-11-07 01:48:35 -0500701 if test.replayWrites {
702 conn = newReplayAdaptor(conn)
703 }
David Benjamin6fd297b2014-08-11 18:43:38 -0400704 }
705
David Benjamin5fa3eba2015-01-22 16:35:40 -0500706 if test.damageFirstWrite {
707 connDamage = newDamageAdaptor(conn)
708 conn = connDamage
709 }
710
David Benjamin6fd297b2014-08-11 18:43:38 -0400711 if test.sendPrefix != "" {
712 if _, err := conn.Write([]byte(test.sendPrefix)); err != nil {
713 return err
714 }
David Benjamin98e882e2014-08-08 13:24:34 -0400715 }
716
David Benjamin1d5c83e2014-07-22 19:20:02 -0400717 var tlsConn *Conn
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400718 if test.testType == clientTest {
David Benjamin6fd297b2014-08-11 18:43:38 -0400719 if test.protocol == dtls {
720 tlsConn = DTLSServer(conn, config)
721 } else {
722 tlsConn = Server(conn, config)
723 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400724 } else {
725 config.InsecureSkipVerify = true
David Benjamin6fd297b2014-08-11 18:43:38 -0400726 if test.protocol == dtls {
727 tlsConn = DTLSClient(conn, config)
728 } else {
729 tlsConn = Client(conn, config)
730 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400731 }
732
Adam Langley95c29f32014-06-20 12:00:00 -0700733 if err := tlsConn.Handshake(); err != nil {
734 return err
735 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700736
David Benjamin01fe8202014-09-24 15:21:44 -0400737 // TODO(davidben): move all per-connection expectations into a dedicated
738 // expectations struct that can be specified separately for the two
739 // legs.
740 expectedVersion := test.expectedVersion
741 if isResume && test.expectedResumeVersion != 0 {
742 expectedVersion = test.expectedResumeVersion
743 }
744 if vers := tlsConn.ConnectionState().Version; expectedVersion != 0 && vers != expectedVersion {
745 return fmt.Errorf("got version %x, expected %x", vers, expectedVersion)
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400746 }
747
David Benjamina08e49d2014-08-24 01:46:07 -0400748 if test.expectChannelID {
749 channelID := tlsConn.ConnectionState().ChannelID
750 if channelID == nil {
751 return fmt.Errorf("no channel ID negotiated")
752 }
753 if channelID.Curve != channelIDKey.Curve ||
754 channelIDKey.X.Cmp(channelIDKey.X) != 0 ||
755 channelIDKey.Y.Cmp(channelIDKey.Y) != 0 {
756 return fmt.Errorf("incorrect channel ID")
757 }
758 }
759
David Benjaminae2888f2014-09-06 12:58:58 -0400760 if expected := test.expectedNextProto; expected != "" {
761 if actual := tlsConn.ConnectionState().NegotiatedProtocol; actual != expected {
762 return fmt.Errorf("next proto mismatch: got %s, wanted %s", actual, expected)
763 }
764 }
765
David Benjaminfc7b0862014-09-06 13:21:53 -0400766 if test.expectedNextProtoType != 0 {
767 if (test.expectedNextProtoType == alpn) != tlsConn.ConnectionState().NegotiatedProtocolFromALPN {
768 return fmt.Errorf("next proto type mismatch")
769 }
770 }
771
David Benjaminca6c8262014-11-15 19:06:08 -0500772 if p := tlsConn.ConnectionState().SRTPProtectionProfile; p != test.expectedSRTPProtectionProfile {
773 return fmt.Errorf("SRTP profile mismatch: got %d, wanted %d", p, test.expectedSRTPProtectionProfile)
774 }
775
David Benjamine58c4f52014-08-24 03:47:07 -0400776 if test.shimWritesFirst {
777 var buf [5]byte
778 _, err := io.ReadFull(tlsConn, buf[:])
779 if err != nil {
780 return err
781 }
782 if string(buf[:]) != "hello" {
783 return fmt.Errorf("bad initial message")
784 }
785 }
786
Adam Langleycf2d4f42014-10-28 19:06:14 -0700787 if test.renegotiate {
788 if test.renegotiateCiphers != nil {
789 config.CipherSuites = test.renegotiateCiphers
790 }
791 if err := tlsConn.Renegotiate(); err != nil {
792 return err
793 }
794 } else if test.renegotiateCiphers != nil {
795 panic("renegotiateCiphers without renegotiate")
796 }
797
David Benjamin5fa3eba2015-01-22 16:35:40 -0500798 if test.damageFirstWrite {
799 connDamage.setDamage(true)
800 tlsConn.Write([]byte("DAMAGED WRITE"))
801 connDamage.setDamage(false)
802 }
803
Kenny Root7fdeaf12014-08-05 15:23:37 -0700804 if messageLen < 0 {
David Benjamin6fd297b2014-08-11 18:43:38 -0400805 if test.protocol == dtls {
806 return fmt.Errorf("messageLen < 0 not supported for DTLS tests")
807 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700808 // Read until EOF.
809 _, err := io.Copy(ioutil.Discard, tlsConn)
810 return err
811 }
812
David Benjamin4189bd92015-01-25 23:52:39 -0500813 var testMessage []byte
814 if config.Bugs.AppDataAfterChangeCipherSpec != nil {
815 // We've already sent a message. Expect the shim to echo it
816 // back.
817 testMessage = config.Bugs.AppDataAfterChangeCipherSpec
818 } else {
819 if messageLen == 0 {
820 messageLen = 32
821 }
822 testMessage = make([]byte, messageLen)
823 for i := range testMessage {
824 testMessage[i] = 0x42
825 }
826 tlsConn.Write(testMessage)
Adam Langley80842bd2014-06-20 12:00:00 -0700827 }
Adam Langley95c29f32014-06-20 12:00:00 -0700828
829 buf := make([]byte, len(testMessage))
David Benjamin6fd297b2014-08-11 18:43:38 -0400830 if test.protocol == dtls {
831 bufTmp := make([]byte, len(buf)+1)
832 n, err := tlsConn.Read(bufTmp)
833 if err != nil {
834 return err
835 }
836 if n != len(buf) {
837 return fmt.Errorf("bad reply; length mismatch (%d vs %d)", n, len(buf))
838 }
839 copy(buf, bufTmp)
840 } else {
841 _, err := io.ReadFull(tlsConn, buf)
842 if err != nil {
843 return err
844 }
Adam Langley95c29f32014-06-20 12:00:00 -0700845 }
846
847 for i, v := range buf {
848 if v != testMessage[i]^0xff {
849 return fmt.Errorf("bad reply contents at byte %d", i)
850 }
851 }
852
853 return nil
854}
855
David Benjamin325b5c32014-07-01 19:40:31 -0400856func valgrindOf(dbAttach bool, path string, args ...string) *exec.Cmd {
857 valgrindArgs := []string{"--error-exitcode=99", "--track-origins=yes", "--leak-check=full"}
Adam Langley95c29f32014-06-20 12:00:00 -0700858 if dbAttach {
David Benjamin325b5c32014-07-01 19:40:31 -0400859 valgrindArgs = append(valgrindArgs, "--db-attach=yes", "--db-command=xterm -e gdb -nw %f %p")
Adam Langley95c29f32014-06-20 12:00:00 -0700860 }
David Benjamin325b5c32014-07-01 19:40:31 -0400861 valgrindArgs = append(valgrindArgs, path)
862 valgrindArgs = append(valgrindArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -0700863
David Benjamin325b5c32014-07-01 19:40:31 -0400864 return exec.Command("valgrind", valgrindArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -0700865}
866
David Benjamin325b5c32014-07-01 19:40:31 -0400867func gdbOf(path string, args ...string) *exec.Cmd {
868 xtermArgs := []string{"-e", "gdb", "--args"}
869 xtermArgs = append(xtermArgs, path)
870 xtermArgs = append(xtermArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -0700871
David Benjamin325b5c32014-07-01 19:40:31 -0400872 return exec.Command("xterm", xtermArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -0700873}
874
David Benjamin1d5c83e2014-07-22 19:20:02 -0400875func openSocketPair() (shimEnd *os.File, conn net.Conn) {
Adam Langley95c29f32014-06-20 12:00:00 -0700876 socks, err := syscall.Socketpair(syscall.AF_UNIX, syscall.SOCK_STREAM, 0)
877 if err != nil {
878 panic(err)
879 }
880
881 syscall.CloseOnExec(socks[0])
882 syscall.CloseOnExec(socks[1])
David Benjamin1d5c83e2014-07-22 19:20:02 -0400883 shimEnd = os.NewFile(uintptr(socks[0]), "shim end")
Adam Langley95c29f32014-06-20 12:00:00 -0700884 connFile := os.NewFile(uintptr(socks[1]), "our end")
David Benjamin1d5c83e2014-07-22 19:20:02 -0400885 conn, err = net.FileConn(connFile)
886 if err != nil {
887 panic(err)
888 }
Adam Langley95c29f32014-06-20 12:00:00 -0700889 connFile.Close()
890 if err != nil {
891 panic(err)
892 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400893 return shimEnd, conn
894}
895
Adam Langley69a01602014-11-17 17:26:55 -0800896type moreMallocsError struct{}
897
898func (moreMallocsError) Error() string {
899 return "child process did not exhaust all allocation calls"
900}
901
902var errMoreMallocs = moreMallocsError{}
903
904func runTest(test *testCase, buildDir string, mallocNumToFail int64) error {
Adam Langley38311732014-10-16 19:04:35 -0700905 if !test.shouldFail && (len(test.expectedError) > 0 || len(test.expectedLocalError) > 0) {
906 panic("Error expected without shouldFail in " + test.name)
907 }
908
David Benjamin1d5c83e2014-07-22 19:20:02 -0400909 shimEnd, conn := openSocketPair()
910 shimEndResume, connResume := openSocketPair()
Adam Langley95c29f32014-06-20 12:00:00 -0700911
David Benjamin884fdf12014-08-02 15:28:23 -0400912 shim_path := path.Join(buildDir, "ssl/test/bssl_shim")
David Benjamin5a593af2014-08-11 19:51:50 -0400913 var flags []string
David Benjamin1d5c83e2014-07-22 19:20:02 -0400914 if test.testType == serverTest {
David Benjamin5a593af2014-08-11 19:51:50 -0400915 flags = append(flags, "-server")
916
David Benjamin025b3d32014-07-01 19:53:04 -0400917 flags = append(flags, "-key-file")
918 if test.keyFile == "" {
919 flags = append(flags, rsaKeyFile)
920 } else {
921 flags = append(flags, test.keyFile)
922 }
923
924 flags = append(flags, "-cert-file")
925 if test.certFile == "" {
926 flags = append(flags, rsaCertificateFile)
927 } else {
928 flags = append(flags, test.certFile)
929 }
930 }
David Benjamin5a593af2014-08-11 19:51:50 -0400931
David Benjamin6fd297b2014-08-11 18:43:38 -0400932 if test.protocol == dtls {
933 flags = append(flags, "-dtls")
934 }
935
David Benjamin5a593af2014-08-11 19:51:50 -0400936 if test.resumeSession {
937 flags = append(flags, "-resume")
938 }
939
David Benjamine58c4f52014-08-24 03:47:07 -0400940 if test.shimWritesFirst {
941 flags = append(flags, "-shim-writes-first")
942 }
943
David Benjamin025b3d32014-07-01 19:53:04 -0400944 flags = append(flags, test.flags...)
945
946 var shim *exec.Cmd
947 if *useValgrind {
948 shim = valgrindOf(false, shim_path, flags...)
Adam Langley75712922014-10-10 16:23:43 -0700949 } else if *useGDB {
950 shim = gdbOf(shim_path, flags...)
David Benjamin025b3d32014-07-01 19:53:04 -0400951 } else {
952 shim = exec.Command(shim_path, flags...)
953 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400954 shim.ExtraFiles = []*os.File{shimEnd, shimEndResume}
David Benjamin025b3d32014-07-01 19:53:04 -0400955 shim.Stdin = os.Stdin
956 var stdoutBuf, stderrBuf bytes.Buffer
957 shim.Stdout = &stdoutBuf
958 shim.Stderr = &stderrBuf
Adam Langley69a01602014-11-17 17:26:55 -0800959 if mallocNumToFail >= 0 {
960 shim.Env = []string{"MALLOC_NUMBER_TO_FAIL=" + strconv.FormatInt(mallocNumToFail, 10)}
961 if *mallocTestDebug {
962 shim.Env = append(shim.Env, "MALLOC_ABORT_ON_FAIL=1")
963 }
964 shim.Env = append(shim.Env, "_MALLOC_CHECK=1")
965 }
David Benjamin025b3d32014-07-01 19:53:04 -0400966
967 if err := shim.Start(); err != nil {
Adam Langley95c29f32014-06-20 12:00:00 -0700968 panic(err)
969 }
David Benjamin025b3d32014-07-01 19:53:04 -0400970 shimEnd.Close()
David Benjamin1d5c83e2014-07-22 19:20:02 -0400971 shimEndResume.Close()
Adam Langley95c29f32014-06-20 12:00:00 -0700972
973 config := test.config
David Benjamin1d5c83e2014-07-22 19:20:02 -0400974 config.ClientSessionCache = NewLRUClientSessionCache(1)
David Benjaminfe8eb9a2014-11-17 03:19:02 -0500975 config.ServerSessionCache = NewLRUServerSessionCache(1)
David Benjamin025b3d32014-07-01 19:53:04 -0400976 if test.testType == clientTest {
977 if len(config.Certificates) == 0 {
978 config.Certificates = []Certificate{getRSACertificate()}
979 }
David Benjamin025b3d32014-07-01 19:53:04 -0400980 }
Adam Langley95c29f32014-06-20 12:00:00 -0700981
David Benjamin01fe8202014-09-24 15:21:44 -0400982 err := doExchange(test, &config, conn, test.messageLen,
983 false /* not a resumption */)
Adam Langley95c29f32014-06-20 12:00:00 -0700984 conn.Close()
David Benjamin65ea8ff2014-11-23 03:01:00 -0500985
David Benjamin1d5c83e2014-07-22 19:20:02 -0400986 if err == nil && test.resumeSession {
David Benjamin01fe8202014-09-24 15:21:44 -0400987 var resumeConfig Config
988 if test.resumeConfig != nil {
989 resumeConfig = *test.resumeConfig
990 if len(resumeConfig.Certificates) == 0 {
991 resumeConfig.Certificates = []Certificate{getRSACertificate()}
992 }
David Benjaminfe8eb9a2014-11-17 03:19:02 -0500993 if !test.newSessionsOnResume {
994 resumeConfig.SessionTicketKey = config.SessionTicketKey
995 resumeConfig.ClientSessionCache = config.ClientSessionCache
996 resumeConfig.ServerSessionCache = config.ServerSessionCache
997 }
David Benjamin01fe8202014-09-24 15:21:44 -0400998 } else {
999 resumeConfig = config
1000 }
1001 err = doExchange(test, &resumeConfig, connResume, test.messageLen,
1002 true /* resumption */)
David Benjamin1d5c83e2014-07-22 19:20:02 -04001003 }
David Benjamin812152a2014-09-06 12:49:07 -04001004 connResume.Close()
David Benjamin1d5c83e2014-07-22 19:20:02 -04001005
David Benjamin025b3d32014-07-01 19:53:04 -04001006 childErr := shim.Wait()
Adam Langley69a01602014-11-17 17:26:55 -08001007 if exitError, ok := childErr.(*exec.ExitError); ok {
1008 if exitError.Sys().(syscall.WaitStatus).ExitStatus() == 88 {
1009 return errMoreMallocs
1010 }
1011 }
Adam Langley95c29f32014-06-20 12:00:00 -07001012
1013 stdout := string(stdoutBuf.Bytes())
1014 stderr := string(stderrBuf.Bytes())
1015 failed := err != nil || childErr != nil
1016 correctFailure := len(test.expectedError) == 0 || strings.Contains(stdout, test.expectedError)
Adam Langleyac61fa32014-06-23 12:03:11 -07001017 localError := "none"
1018 if err != nil {
1019 localError = err.Error()
1020 }
1021 if len(test.expectedLocalError) != 0 {
1022 correctFailure = correctFailure && strings.Contains(localError, test.expectedLocalError)
1023 }
Adam Langley95c29f32014-06-20 12:00:00 -07001024
1025 if failed != test.shouldFail || failed && !correctFailure {
Adam Langley95c29f32014-06-20 12:00:00 -07001026 childError := "none"
Adam Langley95c29f32014-06-20 12:00:00 -07001027 if childErr != nil {
1028 childError = childErr.Error()
1029 }
1030
1031 var msg string
1032 switch {
1033 case failed && !test.shouldFail:
1034 msg = "unexpected failure"
1035 case !failed && test.shouldFail:
1036 msg = "unexpected success"
1037 case failed && !correctFailure:
Adam Langleyac61fa32014-06-23 12:03:11 -07001038 msg = "bad error (wanted '" + test.expectedError + "' / '" + test.expectedLocalError + "')"
Adam Langley95c29f32014-06-20 12:00:00 -07001039 default:
1040 panic("internal error")
1041 }
1042
1043 return fmt.Errorf("%s: local error '%s', child error '%s', stdout:\n%s\nstderr:\n%s", msg, localError, childError, string(stdoutBuf.Bytes()), stderr)
1044 }
1045
1046 if !*useValgrind && len(stderr) > 0 {
1047 println(stderr)
1048 }
1049
1050 return nil
1051}
1052
1053var tlsVersions = []struct {
1054 name string
1055 version uint16
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001056 flag string
David Benjamin8b8c0062014-11-23 02:47:52 -05001057 hasDTLS bool
Adam Langley95c29f32014-06-20 12:00:00 -07001058}{
David Benjamin8b8c0062014-11-23 02:47:52 -05001059 {"SSL3", VersionSSL30, "-no-ssl3", false},
1060 {"TLS1", VersionTLS10, "-no-tls1", true},
1061 {"TLS11", VersionTLS11, "-no-tls11", false},
1062 {"TLS12", VersionTLS12, "-no-tls12", true},
Adam Langley95c29f32014-06-20 12:00:00 -07001063}
1064
1065var testCipherSuites = []struct {
1066 name string
1067 id uint16
1068}{
1069 {"3DES-SHA", TLS_RSA_WITH_3DES_EDE_CBC_SHA},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001070 {"AES128-GCM", TLS_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001071 {"AES128-SHA", TLS_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001072 {"AES128-SHA256", TLS_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001073 {"AES256-GCM", TLS_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001074 {"AES256-SHA", TLS_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001075 {"AES256-SHA256", TLS_RSA_WITH_AES_256_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001076 {"DHE-RSA-AES128-GCM", TLS_DHE_RSA_WITH_AES_128_GCM_SHA256},
1077 {"DHE-RSA-AES128-SHA", TLS_DHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001078 {"DHE-RSA-AES128-SHA256", TLS_DHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001079 {"DHE-RSA-AES256-GCM", TLS_DHE_RSA_WITH_AES_256_GCM_SHA384},
1080 {"DHE-RSA-AES256-SHA", TLS_DHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001081 {"DHE-RSA-AES256-SHA256", TLS_DHE_RSA_WITH_AES_256_CBC_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001082 {"ECDHE-ECDSA-AES128-GCM", TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
1083 {"ECDHE-ECDSA-AES128-SHA", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001084 {"ECDHE-ECDSA-AES128-SHA256", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256},
1085 {"ECDHE-ECDSA-AES256-GCM", TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001086 {"ECDHE-ECDSA-AES256-SHA", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001087 {"ECDHE-ECDSA-AES256-SHA384", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001088 {"ECDHE-ECDSA-RC4-SHA", TLS_ECDHE_ECDSA_WITH_RC4_128_SHA},
David Benjamin2af684f2014-10-27 02:23:15 -04001089 {"ECDHE-PSK-WITH-AES-128-GCM-SHA256", TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001090 {"ECDHE-RSA-AES128-GCM", TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001091 {"ECDHE-RSA-AES128-SHA", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001092 {"ECDHE-RSA-AES128-SHA256", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001093 {"ECDHE-RSA-AES256-GCM", TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001094 {"ECDHE-RSA-AES256-SHA", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001095 {"ECDHE-RSA-AES256-SHA384", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001096 {"ECDHE-RSA-RC4-SHA", TLS_ECDHE_RSA_WITH_RC4_128_SHA},
David Benjamin48cae082014-10-27 01:06:24 -04001097 {"PSK-AES128-CBC-SHA", TLS_PSK_WITH_AES_128_CBC_SHA},
1098 {"PSK-AES256-CBC-SHA", TLS_PSK_WITH_AES_256_CBC_SHA},
1099 {"PSK-RC4-SHA", TLS_PSK_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -07001100 {"RC4-MD5", TLS_RSA_WITH_RC4_128_MD5},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001101 {"RC4-SHA", TLS_RSA_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -07001102}
1103
David Benjamin8b8c0062014-11-23 02:47:52 -05001104func hasComponent(suiteName, component string) bool {
1105 return strings.Contains("-"+suiteName+"-", "-"+component+"-")
1106}
1107
David Benjaminf7768e42014-08-31 02:06:47 -04001108func isTLS12Only(suiteName string) bool {
David Benjamin8b8c0062014-11-23 02:47:52 -05001109 return hasComponent(suiteName, "GCM") ||
1110 hasComponent(suiteName, "SHA256") ||
1111 hasComponent(suiteName, "SHA384")
1112}
1113
1114func isDTLSCipher(suiteName string) bool {
David Benjamine95d20d2014-12-23 11:16:01 -05001115 return !hasComponent(suiteName, "RC4")
David Benjaminf7768e42014-08-31 02:06:47 -04001116}
1117
Adam Langley95c29f32014-06-20 12:00:00 -07001118func addCipherSuiteTests() {
1119 for _, suite := range testCipherSuites {
David Benjamin48cae082014-10-27 01:06:24 -04001120 const psk = "12345"
1121 const pskIdentity = "luggage combo"
1122
Adam Langley95c29f32014-06-20 12:00:00 -07001123 var cert Certificate
David Benjamin025b3d32014-07-01 19:53:04 -04001124 var certFile string
1125 var keyFile string
David Benjamin8b8c0062014-11-23 02:47:52 -05001126 if hasComponent(suite.name, "ECDSA") {
Adam Langley95c29f32014-06-20 12:00:00 -07001127 cert = getECDSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -04001128 certFile = ecdsaCertificateFile
1129 keyFile = ecdsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -07001130 } else {
1131 cert = getRSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -04001132 certFile = rsaCertificateFile
1133 keyFile = rsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -07001134 }
1135
David Benjamin48cae082014-10-27 01:06:24 -04001136 var flags []string
David Benjamin8b8c0062014-11-23 02:47:52 -05001137 if hasComponent(suite.name, "PSK") {
David Benjamin48cae082014-10-27 01:06:24 -04001138 flags = append(flags,
1139 "-psk", psk,
1140 "-psk-identity", pskIdentity)
1141 }
1142
Adam Langley95c29f32014-06-20 12:00:00 -07001143 for _, ver := range tlsVersions {
David Benjaminf7768e42014-08-31 02:06:47 -04001144 if ver.version < VersionTLS12 && isTLS12Only(suite.name) {
Adam Langley95c29f32014-06-20 12:00:00 -07001145 continue
1146 }
1147
David Benjamin025b3d32014-07-01 19:53:04 -04001148 testCases = append(testCases, testCase{
1149 testType: clientTest,
1150 name: ver.name + "-" + suite.name + "-client",
Adam Langley95c29f32014-06-20 12:00:00 -07001151 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001152 MinVersion: ver.version,
1153 MaxVersion: ver.version,
1154 CipherSuites: []uint16{suite.id},
1155 Certificates: []Certificate{cert},
1156 PreSharedKey: []byte(psk),
1157 PreSharedKeyIdentity: pskIdentity,
Adam Langley95c29f32014-06-20 12:00:00 -07001158 },
David Benjamin48cae082014-10-27 01:06:24 -04001159 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001160 resumeSession: true,
Adam Langley95c29f32014-06-20 12:00:00 -07001161 })
David Benjamin025b3d32014-07-01 19:53:04 -04001162
David Benjamin76d8abe2014-08-14 16:25:34 -04001163 testCases = append(testCases, testCase{
1164 testType: serverTest,
1165 name: ver.name + "-" + suite.name + "-server",
1166 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001167 MinVersion: ver.version,
1168 MaxVersion: ver.version,
1169 CipherSuites: []uint16{suite.id},
1170 Certificates: []Certificate{cert},
1171 PreSharedKey: []byte(psk),
1172 PreSharedKeyIdentity: pskIdentity,
David Benjamin76d8abe2014-08-14 16:25:34 -04001173 },
1174 certFile: certFile,
1175 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001176 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001177 resumeSession: true,
David Benjamin76d8abe2014-08-14 16:25:34 -04001178 })
David Benjamin6fd297b2014-08-11 18:43:38 -04001179
David Benjamin8b8c0062014-11-23 02:47:52 -05001180 if ver.hasDTLS && isDTLSCipher(suite.name) {
David Benjamin6fd297b2014-08-11 18:43:38 -04001181 testCases = append(testCases, testCase{
1182 testType: clientTest,
1183 protocol: dtls,
1184 name: "D" + ver.name + "-" + suite.name + "-client",
1185 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001186 MinVersion: ver.version,
1187 MaxVersion: ver.version,
1188 CipherSuites: []uint16{suite.id},
1189 Certificates: []Certificate{cert},
1190 PreSharedKey: []byte(psk),
1191 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001192 },
David Benjamin48cae082014-10-27 01:06:24 -04001193 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001194 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001195 })
1196 testCases = append(testCases, testCase{
1197 testType: serverTest,
1198 protocol: dtls,
1199 name: "D" + ver.name + "-" + suite.name + "-server",
1200 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001201 MinVersion: ver.version,
1202 MaxVersion: ver.version,
1203 CipherSuites: []uint16{suite.id},
1204 Certificates: []Certificate{cert},
1205 PreSharedKey: []byte(psk),
1206 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001207 },
1208 certFile: certFile,
1209 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001210 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001211 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001212 })
1213 }
Adam Langley95c29f32014-06-20 12:00:00 -07001214 }
1215 }
1216}
1217
1218func addBadECDSASignatureTests() {
1219 for badR := BadValue(1); badR < NumBadValues; badR++ {
1220 for badS := BadValue(1); badS < NumBadValues; badS++ {
David Benjamin025b3d32014-07-01 19:53:04 -04001221 testCases = append(testCases, testCase{
Adam Langley95c29f32014-06-20 12:00:00 -07001222 name: fmt.Sprintf("BadECDSA-%d-%d", badR, badS),
1223 config: Config{
1224 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
1225 Certificates: []Certificate{getECDSACertificate()},
1226 Bugs: ProtocolBugs{
1227 BadECDSAR: badR,
1228 BadECDSAS: badS,
1229 },
1230 },
1231 shouldFail: true,
1232 expectedError: "SIGNATURE",
1233 })
1234 }
1235 }
1236}
1237
Adam Langley80842bd2014-06-20 12:00:00 -07001238func addCBCPaddingTests() {
David Benjamin025b3d32014-07-01 19:53:04 -04001239 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001240 name: "MaxCBCPadding",
1241 config: Config{
1242 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1243 Bugs: ProtocolBugs{
1244 MaxPadding: true,
1245 },
1246 },
1247 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1248 })
David Benjamin025b3d32014-07-01 19:53:04 -04001249 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001250 name: "BadCBCPadding",
1251 config: Config{
1252 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1253 Bugs: ProtocolBugs{
1254 PaddingFirstByteBad: true,
1255 },
1256 },
1257 shouldFail: true,
1258 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1259 })
1260 // OpenSSL previously had an issue where the first byte of padding in
1261 // 255 bytes of padding wasn't checked.
David Benjamin025b3d32014-07-01 19:53:04 -04001262 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001263 name: "BadCBCPadding255",
1264 config: Config{
1265 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1266 Bugs: ProtocolBugs{
1267 MaxPadding: true,
1268 PaddingFirstByteBadIf255: true,
1269 },
1270 },
1271 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1272 shouldFail: true,
1273 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1274 })
1275}
1276
Kenny Root7fdeaf12014-08-05 15:23:37 -07001277func addCBCSplittingTests() {
1278 testCases = append(testCases, testCase{
1279 name: "CBCRecordSplitting",
1280 config: Config{
1281 MaxVersion: VersionTLS10,
1282 MinVersion: VersionTLS10,
1283 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1284 },
1285 messageLen: -1, // read until EOF
1286 flags: []string{
1287 "-async",
1288 "-write-different-record-sizes",
1289 "-cbc-record-splitting",
1290 },
David Benjamina8e3e0e2014-08-06 22:11:10 -04001291 })
1292 testCases = append(testCases, testCase{
Kenny Root7fdeaf12014-08-05 15:23:37 -07001293 name: "CBCRecordSplittingPartialWrite",
1294 config: Config{
1295 MaxVersion: VersionTLS10,
1296 MinVersion: VersionTLS10,
1297 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1298 },
1299 messageLen: -1, // read until EOF
1300 flags: []string{
1301 "-async",
1302 "-write-different-record-sizes",
1303 "-cbc-record-splitting",
1304 "-partial-write",
1305 },
1306 })
1307}
1308
David Benjamin636293b2014-07-08 17:59:18 -04001309func addClientAuthTests() {
David Benjamin407a10c2014-07-16 12:58:59 -04001310 // Add a dummy cert pool to stress certificate authority parsing.
1311 // TODO(davidben): Add tests that those values parse out correctly.
1312 certPool := x509.NewCertPool()
1313 cert, err := x509.ParseCertificate(rsaCertificate.Certificate[0])
1314 if err != nil {
1315 panic(err)
1316 }
1317 certPool.AddCert(cert)
1318
David Benjamin636293b2014-07-08 17:59:18 -04001319 for _, ver := range tlsVersions {
David Benjamin636293b2014-07-08 17:59:18 -04001320 testCases = append(testCases, testCase{
1321 testType: clientTest,
David Benjamin67666e72014-07-12 15:47:52 -04001322 name: ver.name + "-Client-ClientAuth-RSA",
David Benjamin636293b2014-07-08 17:59:18 -04001323 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001324 MinVersion: ver.version,
1325 MaxVersion: ver.version,
1326 ClientAuth: RequireAnyClientCert,
1327 ClientCAs: certPool,
David Benjamin636293b2014-07-08 17:59:18 -04001328 },
1329 flags: []string{
1330 "-cert-file", rsaCertificateFile,
1331 "-key-file", rsaKeyFile,
1332 },
1333 })
1334 testCases = append(testCases, testCase{
David Benjamin67666e72014-07-12 15:47:52 -04001335 testType: serverTest,
1336 name: ver.name + "-Server-ClientAuth-RSA",
1337 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001338 MinVersion: ver.version,
1339 MaxVersion: ver.version,
David Benjamin67666e72014-07-12 15:47:52 -04001340 Certificates: []Certificate{rsaCertificate},
1341 },
1342 flags: []string{"-require-any-client-certificate"},
1343 })
David Benjamine098ec22014-08-27 23:13:20 -04001344 if ver.version != VersionSSL30 {
1345 testCases = append(testCases, testCase{
1346 testType: serverTest,
1347 name: ver.name + "-Server-ClientAuth-ECDSA",
1348 config: Config{
1349 MinVersion: ver.version,
1350 MaxVersion: ver.version,
1351 Certificates: []Certificate{ecdsaCertificate},
1352 },
1353 flags: []string{"-require-any-client-certificate"},
1354 })
1355 testCases = append(testCases, testCase{
1356 testType: clientTest,
1357 name: ver.name + "-Client-ClientAuth-ECDSA",
1358 config: Config{
1359 MinVersion: ver.version,
1360 MaxVersion: ver.version,
1361 ClientAuth: RequireAnyClientCert,
1362 ClientCAs: certPool,
1363 },
1364 flags: []string{
1365 "-cert-file", ecdsaCertificateFile,
1366 "-key-file", ecdsaKeyFile,
1367 },
1368 })
1369 }
David Benjamin636293b2014-07-08 17:59:18 -04001370 }
1371}
1372
Adam Langley75712922014-10-10 16:23:43 -07001373func addExtendedMasterSecretTests() {
1374 const expectEMSFlag = "-expect-extended-master-secret"
1375
1376 for _, with := range []bool{false, true} {
1377 prefix := "No"
1378 var flags []string
1379 if with {
1380 prefix = ""
1381 flags = []string{expectEMSFlag}
1382 }
1383
1384 for _, isClient := range []bool{false, true} {
1385 suffix := "-Server"
1386 testType := serverTest
1387 if isClient {
1388 suffix = "-Client"
1389 testType = clientTest
1390 }
1391
1392 for _, ver := range tlsVersions {
1393 test := testCase{
1394 testType: testType,
1395 name: prefix + "ExtendedMasterSecret-" + ver.name + suffix,
1396 config: Config{
1397 MinVersion: ver.version,
1398 MaxVersion: ver.version,
1399 Bugs: ProtocolBugs{
1400 NoExtendedMasterSecret: !with,
1401 RequireExtendedMasterSecret: with,
1402 },
1403 },
David Benjamin48cae082014-10-27 01:06:24 -04001404 flags: flags,
1405 shouldFail: ver.version == VersionSSL30 && with,
Adam Langley75712922014-10-10 16:23:43 -07001406 }
1407 if test.shouldFail {
1408 test.expectedLocalError = "extended master secret required but not supported by peer"
1409 }
1410 testCases = append(testCases, test)
1411 }
1412 }
1413 }
1414
1415 // When a session is resumed, it should still be aware that its master
1416 // secret was generated via EMS and thus it's safe to use tls-unique.
1417 testCases = append(testCases, testCase{
1418 name: "ExtendedMasterSecret-Resume",
1419 config: Config{
1420 Bugs: ProtocolBugs{
1421 RequireExtendedMasterSecret: true,
1422 },
1423 },
1424 flags: []string{expectEMSFlag},
1425 resumeSession: true,
1426 })
1427}
1428
David Benjamin43ec06f2014-08-05 02:28:57 -04001429// Adds tests that try to cover the range of the handshake state machine, under
1430// various conditions. Some of these are redundant with other tests, but they
1431// only cover the synchronous case.
David Benjamin6fd297b2014-08-11 18:43:38 -04001432func addStateMachineCoverageTests(async, splitHandshake bool, protocol protocol) {
David Benjamin43ec06f2014-08-05 02:28:57 -04001433 var suffix string
1434 var flags []string
1435 var maxHandshakeRecordLength int
David Benjamin6fd297b2014-08-11 18:43:38 -04001436 if protocol == dtls {
1437 suffix = "-DTLS"
1438 }
David Benjamin43ec06f2014-08-05 02:28:57 -04001439 if async {
David Benjamin6fd297b2014-08-11 18:43:38 -04001440 suffix += "-Async"
David Benjamin43ec06f2014-08-05 02:28:57 -04001441 flags = append(flags, "-async")
1442 } else {
David Benjamin6fd297b2014-08-11 18:43:38 -04001443 suffix += "-Sync"
David Benjamin43ec06f2014-08-05 02:28:57 -04001444 }
1445 if splitHandshake {
1446 suffix += "-SplitHandshakeRecords"
David Benjamin98214542014-08-07 18:02:39 -04001447 maxHandshakeRecordLength = 1
David Benjamin43ec06f2014-08-05 02:28:57 -04001448 }
1449
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001450 // Basic handshake, with resumption. Client and server,
1451 // session ID and session ticket.
David Benjamin43ec06f2014-08-05 02:28:57 -04001452 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001453 protocol: protocol,
1454 name: "Basic-Client" + suffix,
David Benjamin43ec06f2014-08-05 02:28:57 -04001455 config: Config{
1456 Bugs: ProtocolBugs{
1457 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1458 },
1459 },
David Benjaminbed9aae2014-08-07 19:13:38 -04001460 flags: flags,
1461 resumeSession: true,
1462 })
1463 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001464 protocol: protocol,
1465 name: "Basic-Client-RenewTicket" + suffix,
David Benjaminbed9aae2014-08-07 19:13:38 -04001466 config: Config{
1467 Bugs: ProtocolBugs{
1468 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1469 RenewTicketOnResume: true,
1470 },
1471 },
1472 flags: flags,
1473 resumeSession: true,
David Benjamin43ec06f2014-08-05 02:28:57 -04001474 })
1475 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001476 protocol: protocol,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001477 name: "Basic-Client-NoTicket" + suffix,
1478 config: Config{
1479 SessionTicketsDisabled: true,
1480 Bugs: ProtocolBugs{
1481 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1482 },
1483 },
1484 flags: flags,
1485 resumeSession: true,
1486 })
1487 testCases = append(testCases, testCase{
1488 protocol: protocol,
David Benjamin43ec06f2014-08-05 02:28:57 -04001489 testType: serverTest,
1490 name: "Basic-Server" + suffix,
1491 config: Config{
1492 Bugs: ProtocolBugs{
1493 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1494 },
1495 },
David Benjaminbed9aae2014-08-07 19:13:38 -04001496 flags: flags,
1497 resumeSession: true,
David Benjamin43ec06f2014-08-05 02:28:57 -04001498 })
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001499 testCases = append(testCases, testCase{
1500 protocol: protocol,
1501 testType: serverTest,
1502 name: "Basic-Server-NoTickets" + suffix,
1503 config: Config{
1504 SessionTicketsDisabled: true,
1505 Bugs: ProtocolBugs{
1506 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1507 },
1508 },
1509 flags: flags,
1510 resumeSession: true,
1511 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001512
David Benjamin6fd297b2014-08-11 18:43:38 -04001513 // TLS client auth.
1514 testCases = append(testCases, testCase{
1515 protocol: protocol,
1516 testType: clientTest,
1517 name: "ClientAuth-Client" + suffix,
1518 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001519 ClientAuth: RequireAnyClientCert,
David Benjamin6fd297b2014-08-11 18:43:38 -04001520 Bugs: ProtocolBugs{
1521 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1522 },
1523 },
1524 flags: append(flags,
1525 "-cert-file", rsaCertificateFile,
1526 "-key-file", rsaKeyFile),
1527 })
1528 testCases = append(testCases, testCase{
1529 protocol: protocol,
1530 testType: serverTest,
1531 name: "ClientAuth-Server" + suffix,
1532 config: Config{
1533 Certificates: []Certificate{rsaCertificate},
1534 },
1535 flags: append(flags, "-require-any-client-certificate"),
1536 })
1537
David Benjamin43ec06f2014-08-05 02:28:57 -04001538 // No session ticket support; server doesn't send NewSessionTicket.
1539 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001540 protocol: protocol,
1541 name: "SessionTicketsDisabled-Client" + suffix,
David Benjamin43ec06f2014-08-05 02:28:57 -04001542 config: Config{
1543 SessionTicketsDisabled: true,
1544 Bugs: ProtocolBugs{
1545 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1546 },
1547 },
1548 flags: flags,
1549 })
1550 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001551 protocol: protocol,
David Benjamin43ec06f2014-08-05 02:28:57 -04001552 testType: serverTest,
1553 name: "SessionTicketsDisabled-Server" + suffix,
1554 config: Config{
1555 SessionTicketsDisabled: true,
1556 Bugs: ProtocolBugs{
1557 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1558 },
1559 },
1560 flags: flags,
1561 })
1562
David Benjamin48cae082014-10-27 01:06:24 -04001563 // Skip ServerKeyExchange in PSK key exchange if there's no
1564 // identity hint.
1565 testCases = append(testCases, testCase{
1566 protocol: protocol,
1567 name: "EmptyPSKHint-Client" + suffix,
1568 config: Config{
1569 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
1570 PreSharedKey: []byte("secret"),
1571 Bugs: ProtocolBugs{
1572 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1573 },
1574 },
1575 flags: append(flags, "-psk", "secret"),
1576 })
1577 testCases = append(testCases, testCase{
1578 protocol: protocol,
1579 testType: serverTest,
1580 name: "EmptyPSKHint-Server" + suffix,
1581 config: Config{
1582 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
1583 PreSharedKey: []byte("secret"),
1584 Bugs: ProtocolBugs{
1585 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1586 },
1587 },
1588 flags: append(flags, "-psk", "secret"),
1589 })
1590
David Benjamin6fd297b2014-08-11 18:43:38 -04001591 if protocol == tls {
1592 // NPN on client and server; results in post-handshake message.
1593 testCases = append(testCases, testCase{
1594 protocol: protocol,
1595 name: "NPN-Client" + suffix,
1596 config: Config{
David Benjaminae2888f2014-09-06 12:58:58 -04001597 NextProtos: []string{"foo"},
David Benjamin6fd297b2014-08-11 18:43:38 -04001598 Bugs: ProtocolBugs{
1599 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1600 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001601 },
David Benjaminfc7b0862014-09-06 13:21:53 -04001602 flags: append(flags, "-select-next-proto", "foo"),
1603 expectedNextProto: "foo",
1604 expectedNextProtoType: npn,
David Benjamin6fd297b2014-08-11 18:43:38 -04001605 })
1606 testCases = append(testCases, testCase{
1607 protocol: protocol,
1608 testType: serverTest,
1609 name: "NPN-Server" + suffix,
1610 config: Config{
1611 NextProtos: []string{"bar"},
1612 Bugs: ProtocolBugs{
1613 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1614 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001615 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001616 flags: append(flags,
1617 "-advertise-npn", "\x03foo\x03bar\x03baz",
1618 "-expect-next-proto", "bar"),
David Benjaminfc7b0862014-09-06 13:21:53 -04001619 expectedNextProto: "bar",
1620 expectedNextProtoType: npn,
David Benjamin6fd297b2014-08-11 18:43:38 -04001621 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001622
David Benjamin6fd297b2014-08-11 18:43:38 -04001623 // Client does False Start and negotiates NPN.
1624 testCases = append(testCases, testCase{
1625 protocol: protocol,
1626 name: "FalseStart" + suffix,
1627 config: Config{
1628 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1629 NextProtos: []string{"foo"},
1630 Bugs: ProtocolBugs{
David Benjamine58c4f52014-08-24 03:47:07 -04001631 ExpectFalseStart: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001632 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1633 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001634 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001635 flags: append(flags,
1636 "-false-start",
1637 "-select-next-proto", "foo"),
David Benjamine58c4f52014-08-24 03:47:07 -04001638 shimWritesFirst: true,
1639 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001640 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001641
David Benjaminae2888f2014-09-06 12:58:58 -04001642 // Client does False Start and negotiates ALPN.
1643 testCases = append(testCases, testCase{
1644 protocol: protocol,
1645 name: "FalseStart-ALPN" + suffix,
1646 config: Config{
1647 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1648 NextProtos: []string{"foo"},
1649 Bugs: ProtocolBugs{
1650 ExpectFalseStart: true,
1651 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1652 },
1653 },
1654 flags: append(flags,
1655 "-false-start",
1656 "-advertise-alpn", "\x03foo"),
1657 shimWritesFirst: true,
1658 resumeSession: true,
1659 })
1660
David Benjamin6fd297b2014-08-11 18:43:38 -04001661 // False Start without session tickets.
1662 testCases = append(testCases, testCase{
1663 name: "FalseStart-SessionTicketsDisabled",
1664 config: Config{
1665 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1666 NextProtos: []string{"foo"},
1667 SessionTicketsDisabled: true,
David Benjamin4e99c522014-08-24 01:45:30 -04001668 Bugs: ProtocolBugs{
David Benjamine58c4f52014-08-24 03:47:07 -04001669 ExpectFalseStart: true,
David Benjamin4e99c522014-08-24 01:45:30 -04001670 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1671 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001672 },
David Benjamin4e99c522014-08-24 01:45:30 -04001673 flags: append(flags,
David Benjamin6fd297b2014-08-11 18:43:38 -04001674 "-false-start",
1675 "-select-next-proto", "foo",
David Benjamin4e99c522014-08-24 01:45:30 -04001676 ),
David Benjamine58c4f52014-08-24 03:47:07 -04001677 shimWritesFirst: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001678 })
David Benjamin1e7f8d72014-08-08 12:27:04 -04001679
David Benjamina08e49d2014-08-24 01:46:07 -04001680 // Server parses a V2ClientHello.
David Benjamin6fd297b2014-08-11 18:43:38 -04001681 testCases = append(testCases, testCase{
1682 protocol: protocol,
1683 testType: serverTest,
1684 name: "SendV2ClientHello" + suffix,
1685 config: Config{
1686 // Choose a cipher suite that does not involve
1687 // elliptic curves, so no extensions are
1688 // involved.
1689 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
1690 Bugs: ProtocolBugs{
1691 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1692 SendV2ClientHello: true,
1693 },
David Benjamin1e7f8d72014-08-08 12:27:04 -04001694 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001695 flags: flags,
1696 })
David Benjamina08e49d2014-08-24 01:46:07 -04001697
1698 // Client sends a Channel ID.
1699 testCases = append(testCases, testCase{
1700 protocol: protocol,
1701 name: "ChannelID-Client" + suffix,
1702 config: Config{
1703 RequestChannelID: true,
1704 Bugs: ProtocolBugs{
1705 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1706 },
1707 },
1708 flags: append(flags,
1709 "-send-channel-id", channelIDKeyFile,
1710 ),
1711 resumeSession: true,
1712 expectChannelID: true,
1713 })
1714
1715 // Server accepts a Channel ID.
1716 testCases = append(testCases, testCase{
1717 protocol: protocol,
1718 testType: serverTest,
1719 name: "ChannelID-Server" + suffix,
1720 config: Config{
1721 ChannelID: channelIDKey,
1722 Bugs: ProtocolBugs{
1723 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1724 },
1725 },
1726 flags: append(flags,
1727 "-expect-channel-id",
1728 base64.StdEncoding.EncodeToString(channelIDBytes),
1729 ),
1730 resumeSession: true,
1731 expectChannelID: true,
1732 })
David Benjamin6fd297b2014-08-11 18:43:38 -04001733 } else {
1734 testCases = append(testCases, testCase{
1735 protocol: protocol,
1736 name: "SkipHelloVerifyRequest" + suffix,
1737 config: Config{
1738 Bugs: ProtocolBugs{
1739 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1740 SkipHelloVerifyRequest: true,
1741 },
1742 },
1743 flags: flags,
1744 })
1745
1746 testCases = append(testCases, testCase{
1747 testType: serverTest,
1748 protocol: protocol,
1749 name: "CookieExchange" + suffix,
1750 config: Config{
1751 Bugs: ProtocolBugs{
1752 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1753 },
1754 },
1755 flags: append(flags, "-cookie-exchange"),
1756 })
1757 }
David Benjamin43ec06f2014-08-05 02:28:57 -04001758}
1759
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001760func addVersionNegotiationTests() {
1761 for i, shimVers := range tlsVersions {
1762 // Assemble flags to disable all newer versions on the shim.
1763 var flags []string
1764 for _, vers := range tlsVersions[i+1:] {
1765 flags = append(flags, vers.flag)
1766 }
1767
1768 for _, runnerVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05001769 protocols := []protocol{tls}
1770 if runnerVers.hasDTLS && shimVers.hasDTLS {
1771 protocols = append(protocols, dtls)
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001772 }
David Benjamin8b8c0062014-11-23 02:47:52 -05001773 for _, protocol := range protocols {
1774 expectedVersion := shimVers.version
1775 if runnerVers.version < shimVers.version {
1776 expectedVersion = runnerVers.version
1777 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001778
David Benjamin8b8c0062014-11-23 02:47:52 -05001779 suffix := shimVers.name + "-" + runnerVers.name
1780 if protocol == dtls {
1781 suffix += "-DTLS"
1782 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001783
David Benjamin1eb367c2014-12-12 18:17:51 -05001784 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
1785
David Benjamin1e29a6b2014-12-10 02:27:24 -05001786 clientVers := shimVers.version
1787 if clientVers > VersionTLS10 {
1788 clientVers = VersionTLS10
1789 }
David Benjamin8b8c0062014-11-23 02:47:52 -05001790 testCases = append(testCases, testCase{
1791 protocol: protocol,
1792 testType: clientTest,
1793 name: "VersionNegotiation-Client-" + suffix,
1794 config: Config{
1795 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05001796 Bugs: ProtocolBugs{
1797 ExpectInitialRecordVersion: clientVers,
1798 },
David Benjamin8b8c0062014-11-23 02:47:52 -05001799 },
1800 flags: flags,
1801 expectedVersion: expectedVersion,
1802 })
David Benjamin1eb367c2014-12-12 18:17:51 -05001803 testCases = append(testCases, testCase{
1804 protocol: protocol,
1805 testType: clientTest,
1806 name: "VersionNegotiation-Client2-" + suffix,
1807 config: Config{
1808 MaxVersion: runnerVers.version,
1809 Bugs: ProtocolBugs{
1810 ExpectInitialRecordVersion: clientVers,
1811 },
1812 },
1813 flags: []string{"-max-version", shimVersFlag},
1814 expectedVersion: expectedVersion,
1815 })
David Benjamin8b8c0062014-11-23 02:47:52 -05001816
1817 testCases = append(testCases, testCase{
1818 protocol: protocol,
1819 testType: serverTest,
1820 name: "VersionNegotiation-Server-" + suffix,
1821 config: Config{
1822 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05001823 Bugs: ProtocolBugs{
1824 ExpectInitialRecordVersion: expectedVersion,
1825 },
David Benjamin8b8c0062014-11-23 02:47:52 -05001826 },
1827 flags: flags,
1828 expectedVersion: expectedVersion,
1829 })
David Benjamin1eb367c2014-12-12 18:17:51 -05001830 testCases = append(testCases, testCase{
1831 protocol: protocol,
1832 testType: serverTest,
1833 name: "VersionNegotiation-Server2-" + suffix,
1834 config: Config{
1835 MaxVersion: runnerVers.version,
1836 Bugs: ProtocolBugs{
1837 ExpectInitialRecordVersion: expectedVersion,
1838 },
1839 },
1840 flags: []string{"-max-version", shimVersFlag},
1841 expectedVersion: expectedVersion,
1842 })
David Benjamin8b8c0062014-11-23 02:47:52 -05001843 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001844 }
1845 }
1846}
1847
David Benjaminaccb4542014-12-12 23:44:33 -05001848func addMinimumVersionTests() {
1849 for i, shimVers := range tlsVersions {
1850 // Assemble flags to disable all older versions on the shim.
1851 var flags []string
1852 for _, vers := range tlsVersions[:i] {
1853 flags = append(flags, vers.flag)
1854 }
1855
1856 for _, runnerVers := range tlsVersions {
1857 protocols := []protocol{tls}
1858 if runnerVers.hasDTLS && shimVers.hasDTLS {
1859 protocols = append(protocols, dtls)
1860 }
1861 for _, protocol := range protocols {
1862 suffix := shimVers.name + "-" + runnerVers.name
1863 if protocol == dtls {
1864 suffix += "-DTLS"
1865 }
1866 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
1867
David Benjaminaccb4542014-12-12 23:44:33 -05001868 var expectedVersion uint16
1869 var shouldFail bool
1870 var expectedError string
David Benjamin87909c02014-12-13 01:55:01 -05001871 var expectedLocalError string
David Benjaminaccb4542014-12-12 23:44:33 -05001872 if runnerVers.version >= shimVers.version {
1873 expectedVersion = runnerVers.version
1874 } else {
1875 shouldFail = true
1876 expectedError = ":UNSUPPORTED_PROTOCOL:"
David Benjamin87909c02014-12-13 01:55:01 -05001877 if runnerVers.version > VersionSSL30 {
1878 expectedLocalError = "remote error: protocol version not supported"
1879 } else {
1880 expectedLocalError = "remote error: handshake failure"
1881 }
David Benjaminaccb4542014-12-12 23:44:33 -05001882 }
1883
1884 testCases = append(testCases, testCase{
1885 protocol: protocol,
1886 testType: clientTest,
1887 name: "MinimumVersion-Client-" + suffix,
1888 config: Config{
1889 MaxVersion: runnerVers.version,
1890 },
David Benjamin87909c02014-12-13 01:55:01 -05001891 flags: flags,
1892 expectedVersion: expectedVersion,
1893 shouldFail: shouldFail,
1894 expectedError: expectedError,
1895 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05001896 })
1897 testCases = append(testCases, testCase{
1898 protocol: protocol,
1899 testType: clientTest,
1900 name: "MinimumVersion-Client2-" + suffix,
1901 config: Config{
1902 MaxVersion: runnerVers.version,
1903 },
David Benjamin87909c02014-12-13 01:55:01 -05001904 flags: []string{"-min-version", shimVersFlag},
1905 expectedVersion: expectedVersion,
1906 shouldFail: shouldFail,
1907 expectedError: expectedError,
1908 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05001909 })
1910
1911 testCases = append(testCases, testCase{
1912 protocol: protocol,
1913 testType: serverTest,
1914 name: "MinimumVersion-Server-" + suffix,
1915 config: Config{
1916 MaxVersion: runnerVers.version,
1917 },
David Benjamin87909c02014-12-13 01:55:01 -05001918 flags: flags,
1919 expectedVersion: expectedVersion,
1920 shouldFail: shouldFail,
1921 expectedError: expectedError,
1922 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05001923 })
1924 testCases = append(testCases, testCase{
1925 protocol: protocol,
1926 testType: serverTest,
1927 name: "MinimumVersion-Server2-" + suffix,
1928 config: Config{
1929 MaxVersion: runnerVers.version,
1930 },
David Benjamin87909c02014-12-13 01:55:01 -05001931 flags: []string{"-min-version", shimVersFlag},
1932 expectedVersion: expectedVersion,
1933 shouldFail: shouldFail,
1934 expectedError: expectedError,
1935 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05001936 })
1937 }
1938 }
1939 }
1940}
1941
David Benjamin5c24a1d2014-08-31 00:59:27 -04001942func addD5BugTests() {
1943 testCases = append(testCases, testCase{
1944 testType: serverTest,
1945 name: "D5Bug-NoQuirk-Reject",
1946 config: Config{
1947 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
1948 Bugs: ProtocolBugs{
1949 SSL3RSAKeyExchange: true,
1950 },
1951 },
1952 shouldFail: true,
1953 expectedError: ":TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG:",
1954 })
1955 testCases = append(testCases, testCase{
1956 testType: serverTest,
1957 name: "D5Bug-Quirk-Normal",
1958 config: Config{
1959 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
1960 },
1961 flags: []string{"-tls-d5-bug"},
1962 })
1963 testCases = append(testCases, testCase{
1964 testType: serverTest,
1965 name: "D5Bug-Quirk-Bug",
1966 config: Config{
1967 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
1968 Bugs: ProtocolBugs{
1969 SSL3RSAKeyExchange: true,
1970 },
1971 },
1972 flags: []string{"-tls-d5-bug"},
1973 })
1974}
1975
David Benjamine78bfde2014-09-06 12:45:15 -04001976func addExtensionTests() {
1977 testCases = append(testCases, testCase{
1978 testType: clientTest,
1979 name: "DuplicateExtensionClient",
1980 config: Config{
1981 Bugs: ProtocolBugs{
1982 DuplicateExtension: true,
1983 },
1984 },
1985 shouldFail: true,
1986 expectedLocalError: "remote error: error decoding message",
1987 })
1988 testCases = append(testCases, testCase{
1989 testType: serverTest,
1990 name: "DuplicateExtensionServer",
1991 config: Config{
1992 Bugs: ProtocolBugs{
1993 DuplicateExtension: true,
1994 },
1995 },
1996 shouldFail: true,
1997 expectedLocalError: "remote error: error decoding message",
1998 })
1999 testCases = append(testCases, testCase{
2000 testType: clientTest,
2001 name: "ServerNameExtensionClient",
2002 config: Config{
2003 Bugs: ProtocolBugs{
2004 ExpectServerName: "example.com",
2005 },
2006 },
2007 flags: []string{"-host-name", "example.com"},
2008 })
2009 testCases = append(testCases, testCase{
2010 testType: clientTest,
2011 name: "ServerNameExtensionClient",
2012 config: Config{
2013 Bugs: ProtocolBugs{
2014 ExpectServerName: "mismatch.com",
2015 },
2016 },
2017 flags: []string{"-host-name", "example.com"},
2018 shouldFail: true,
2019 expectedLocalError: "tls: unexpected server name",
2020 })
2021 testCases = append(testCases, testCase{
2022 testType: clientTest,
2023 name: "ServerNameExtensionClient",
2024 config: Config{
2025 Bugs: ProtocolBugs{
2026 ExpectServerName: "missing.com",
2027 },
2028 },
2029 shouldFail: true,
2030 expectedLocalError: "tls: unexpected server name",
2031 })
2032 testCases = append(testCases, testCase{
2033 testType: serverTest,
2034 name: "ServerNameExtensionServer",
2035 config: Config{
2036 ServerName: "example.com",
2037 },
2038 flags: []string{"-expect-server-name", "example.com"},
2039 resumeSession: true,
2040 })
David Benjaminae2888f2014-09-06 12:58:58 -04002041 testCases = append(testCases, testCase{
2042 testType: clientTest,
2043 name: "ALPNClient",
2044 config: Config{
2045 NextProtos: []string{"foo"},
2046 },
2047 flags: []string{
2048 "-advertise-alpn", "\x03foo\x03bar\x03baz",
2049 "-expect-alpn", "foo",
2050 },
David Benjaminfc7b0862014-09-06 13:21:53 -04002051 expectedNextProto: "foo",
2052 expectedNextProtoType: alpn,
2053 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04002054 })
2055 testCases = append(testCases, testCase{
2056 testType: serverTest,
2057 name: "ALPNServer",
2058 config: Config{
2059 NextProtos: []string{"foo", "bar", "baz"},
2060 },
2061 flags: []string{
2062 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2063 "-select-alpn", "foo",
2064 },
David Benjaminfc7b0862014-09-06 13:21:53 -04002065 expectedNextProto: "foo",
2066 expectedNextProtoType: alpn,
2067 resumeSession: true,
2068 })
2069 // Test that the server prefers ALPN over NPN.
2070 testCases = append(testCases, testCase{
2071 testType: serverTest,
2072 name: "ALPNServer-Preferred",
2073 config: Config{
2074 NextProtos: []string{"foo", "bar", "baz"},
2075 },
2076 flags: []string{
2077 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2078 "-select-alpn", "foo",
2079 "-advertise-npn", "\x03foo\x03bar\x03baz",
2080 },
2081 expectedNextProto: "foo",
2082 expectedNextProtoType: alpn,
2083 resumeSession: true,
2084 })
2085 testCases = append(testCases, testCase{
2086 testType: serverTest,
2087 name: "ALPNServer-Preferred-Swapped",
2088 config: Config{
2089 NextProtos: []string{"foo", "bar", "baz"},
2090 Bugs: ProtocolBugs{
2091 SwapNPNAndALPN: true,
2092 },
2093 },
2094 flags: []string{
2095 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2096 "-select-alpn", "foo",
2097 "-advertise-npn", "\x03foo\x03bar\x03baz",
2098 },
2099 expectedNextProto: "foo",
2100 expectedNextProtoType: alpn,
2101 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04002102 })
Adam Langley38311732014-10-16 19:04:35 -07002103 // Resume with a corrupt ticket.
2104 testCases = append(testCases, testCase{
2105 testType: serverTest,
2106 name: "CorruptTicket",
2107 config: Config{
2108 Bugs: ProtocolBugs{
2109 CorruptTicket: true,
2110 },
2111 },
2112 resumeSession: true,
2113 flags: []string{"-expect-session-miss"},
2114 })
2115 // Resume with an oversized session id.
2116 testCases = append(testCases, testCase{
2117 testType: serverTest,
2118 name: "OversizedSessionId",
2119 config: Config{
2120 Bugs: ProtocolBugs{
2121 OversizedSessionId: true,
2122 },
2123 },
2124 resumeSession: true,
Adam Langley75712922014-10-10 16:23:43 -07002125 shouldFail: true,
Adam Langley38311732014-10-16 19:04:35 -07002126 expectedError: ":DECODE_ERROR:",
2127 })
David Benjaminca6c8262014-11-15 19:06:08 -05002128 // Basic DTLS-SRTP tests. Include fake profiles to ensure they
2129 // are ignored.
2130 testCases = append(testCases, testCase{
2131 protocol: dtls,
2132 name: "SRTP-Client",
2133 config: Config{
2134 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
2135 },
2136 flags: []string{
2137 "-srtp-profiles",
2138 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2139 },
2140 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2141 })
2142 testCases = append(testCases, testCase{
2143 protocol: dtls,
2144 testType: serverTest,
2145 name: "SRTP-Server",
2146 config: Config{
2147 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
2148 },
2149 flags: []string{
2150 "-srtp-profiles",
2151 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2152 },
2153 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2154 })
2155 // Test that the MKI is ignored.
2156 testCases = append(testCases, testCase{
2157 protocol: dtls,
2158 testType: serverTest,
2159 name: "SRTP-Server-IgnoreMKI",
2160 config: Config{
2161 SRTPProtectionProfiles: []uint16{SRTP_AES128_CM_HMAC_SHA1_80},
2162 Bugs: ProtocolBugs{
2163 SRTPMasterKeyIdentifer: "bogus",
2164 },
2165 },
2166 flags: []string{
2167 "-srtp-profiles",
2168 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2169 },
2170 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2171 })
2172 // Test that SRTP isn't negotiated on the server if there were
2173 // no matching profiles.
2174 testCases = append(testCases, testCase{
2175 protocol: dtls,
2176 testType: serverTest,
2177 name: "SRTP-Server-NoMatch",
2178 config: Config{
2179 SRTPProtectionProfiles: []uint16{100, 101, 102},
2180 },
2181 flags: []string{
2182 "-srtp-profiles",
2183 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2184 },
2185 expectedSRTPProtectionProfile: 0,
2186 })
2187 // Test that the server returning an invalid SRTP profile is
2188 // flagged as an error by the client.
2189 testCases = append(testCases, testCase{
2190 protocol: dtls,
2191 name: "SRTP-Client-NoMatch",
2192 config: Config{
2193 Bugs: ProtocolBugs{
2194 SendSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_32,
2195 },
2196 },
2197 flags: []string{
2198 "-srtp-profiles",
2199 "SRTP_AES128_CM_SHA1_80",
2200 },
2201 shouldFail: true,
2202 expectedError: ":BAD_SRTP_PROTECTION_PROFILE_LIST:",
2203 })
David Benjamin61f95272014-11-25 01:55:35 -05002204 // Test OCSP stapling and SCT list.
2205 testCases = append(testCases, testCase{
2206 name: "OCSPStapling",
2207 flags: []string{
2208 "-enable-ocsp-stapling",
2209 "-expect-ocsp-response",
2210 base64.StdEncoding.EncodeToString(testOCSPResponse),
2211 },
2212 })
2213 testCases = append(testCases, testCase{
2214 name: "SignedCertificateTimestampList",
2215 flags: []string{
2216 "-enable-signed-cert-timestamps",
2217 "-expect-signed-cert-timestamps",
2218 base64.StdEncoding.EncodeToString(testSCTList),
2219 },
2220 })
David Benjamine78bfde2014-09-06 12:45:15 -04002221}
2222
David Benjamin01fe8202014-09-24 15:21:44 -04002223func addResumptionVersionTests() {
David Benjamin01fe8202014-09-24 15:21:44 -04002224 for _, sessionVers := range tlsVersions {
David Benjamin01fe8202014-09-24 15:21:44 -04002225 for _, resumeVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05002226 protocols := []protocol{tls}
2227 if sessionVers.hasDTLS && resumeVers.hasDTLS {
2228 protocols = append(protocols, dtls)
David Benjaminbdf5e722014-11-11 00:52:15 -05002229 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002230 for _, protocol := range protocols {
2231 suffix := "-" + sessionVers.name + "-" + resumeVers.name
2232 if protocol == dtls {
2233 suffix += "-DTLS"
2234 }
2235
2236 testCases = append(testCases, testCase{
2237 protocol: protocol,
2238 name: "Resume-Client" + suffix,
2239 resumeSession: true,
2240 config: Config{
2241 MaxVersion: sessionVers.version,
2242 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2243 Bugs: ProtocolBugs{
2244 AllowSessionVersionMismatch: true,
2245 },
2246 },
2247 expectedVersion: sessionVers.version,
2248 resumeConfig: &Config{
2249 MaxVersion: resumeVers.version,
2250 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2251 Bugs: ProtocolBugs{
2252 AllowSessionVersionMismatch: true,
2253 },
2254 },
2255 expectedResumeVersion: resumeVers.version,
2256 })
2257
2258 testCases = append(testCases, testCase{
2259 protocol: protocol,
2260 name: "Resume-Client-NoResume" + suffix,
2261 flags: []string{"-expect-session-miss"},
2262 resumeSession: true,
2263 config: Config{
2264 MaxVersion: sessionVers.version,
2265 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2266 },
2267 expectedVersion: sessionVers.version,
2268 resumeConfig: &Config{
2269 MaxVersion: resumeVers.version,
2270 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2271 },
2272 newSessionsOnResume: true,
2273 expectedResumeVersion: resumeVers.version,
2274 })
2275
2276 var flags []string
2277 if sessionVers.version != resumeVers.version {
2278 flags = append(flags, "-expect-session-miss")
2279 }
2280 testCases = append(testCases, testCase{
2281 protocol: protocol,
2282 testType: serverTest,
2283 name: "Resume-Server" + suffix,
2284 flags: flags,
2285 resumeSession: true,
2286 config: Config{
2287 MaxVersion: sessionVers.version,
2288 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2289 },
2290 expectedVersion: sessionVers.version,
2291 resumeConfig: &Config{
2292 MaxVersion: resumeVers.version,
2293 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2294 },
2295 expectedResumeVersion: resumeVers.version,
2296 })
2297 }
David Benjamin01fe8202014-09-24 15:21:44 -04002298 }
2299 }
2300}
2301
Adam Langley2ae77d22014-10-28 17:29:33 -07002302func addRenegotiationTests() {
2303 testCases = append(testCases, testCase{
2304 testType: serverTest,
2305 name: "Renegotiate-Server",
2306 flags: []string{"-renegotiate"},
2307 shimWritesFirst: true,
2308 })
2309 testCases = append(testCases, testCase{
2310 testType: serverTest,
2311 name: "Renegotiate-Server-EmptyExt",
2312 config: Config{
2313 Bugs: ProtocolBugs{
2314 EmptyRenegotiationInfo: true,
2315 },
2316 },
2317 flags: []string{"-renegotiate"},
2318 shimWritesFirst: true,
2319 shouldFail: true,
2320 expectedError: ":RENEGOTIATION_MISMATCH:",
2321 })
2322 testCases = append(testCases, testCase{
2323 testType: serverTest,
2324 name: "Renegotiate-Server-BadExt",
2325 config: Config{
2326 Bugs: ProtocolBugs{
2327 BadRenegotiationInfo: true,
2328 },
2329 },
2330 flags: []string{"-renegotiate"},
2331 shimWritesFirst: true,
2332 shouldFail: true,
2333 expectedError: ":RENEGOTIATION_MISMATCH:",
2334 })
David Benjaminca6554b2014-11-08 12:31:52 -05002335 testCases = append(testCases, testCase{
2336 testType: serverTest,
2337 name: "Renegotiate-Server-ClientInitiated",
2338 renegotiate: true,
2339 })
2340 testCases = append(testCases, testCase{
2341 testType: serverTest,
2342 name: "Renegotiate-Server-ClientInitiated-NoExt",
2343 renegotiate: true,
2344 config: Config{
2345 Bugs: ProtocolBugs{
2346 NoRenegotiationInfo: true,
2347 },
2348 },
2349 shouldFail: true,
2350 expectedError: ":UNSAFE_LEGACY_RENEGOTIATION_DISABLED:",
2351 })
2352 testCases = append(testCases, testCase{
2353 testType: serverTest,
2354 name: "Renegotiate-Server-ClientInitiated-NoExt-Allowed",
2355 renegotiate: true,
2356 config: Config{
2357 Bugs: ProtocolBugs{
2358 NoRenegotiationInfo: true,
2359 },
2360 },
2361 flags: []string{"-allow-unsafe-legacy-renegotiation"},
2362 })
Adam Langley2ae77d22014-10-28 17:29:33 -07002363 // TODO(agl): test the renegotiation info SCSV.
Adam Langleycf2d4f42014-10-28 19:06:14 -07002364 testCases = append(testCases, testCase{
2365 name: "Renegotiate-Client",
2366 renegotiate: true,
2367 })
2368 testCases = append(testCases, testCase{
2369 name: "Renegotiate-Client-EmptyExt",
2370 renegotiate: true,
2371 config: Config{
2372 Bugs: ProtocolBugs{
2373 EmptyRenegotiationInfo: true,
2374 },
2375 },
2376 shouldFail: true,
2377 expectedError: ":RENEGOTIATION_MISMATCH:",
2378 })
2379 testCases = append(testCases, testCase{
2380 name: "Renegotiate-Client-BadExt",
2381 renegotiate: true,
2382 config: Config{
2383 Bugs: ProtocolBugs{
2384 BadRenegotiationInfo: true,
2385 },
2386 },
2387 shouldFail: true,
2388 expectedError: ":RENEGOTIATION_MISMATCH:",
2389 })
2390 testCases = append(testCases, testCase{
2391 name: "Renegotiate-Client-SwitchCiphers",
2392 renegotiate: true,
2393 config: Config{
2394 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2395 },
2396 renegotiateCiphers: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2397 })
2398 testCases = append(testCases, testCase{
2399 name: "Renegotiate-Client-SwitchCiphers2",
2400 renegotiate: true,
2401 config: Config{
2402 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2403 },
2404 renegotiateCiphers: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2405 })
David Benjaminc44b1df2014-11-23 12:11:01 -05002406 testCases = append(testCases, testCase{
2407 name: "Renegotiate-SameClientVersion",
2408 renegotiate: true,
2409 config: Config{
2410 MaxVersion: VersionTLS10,
2411 Bugs: ProtocolBugs{
2412 RequireSameRenegoClientVersion: true,
2413 },
2414 },
2415 })
Adam Langley2ae77d22014-10-28 17:29:33 -07002416}
2417
David Benjamin5e961c12014-11-07 01:48:35 -05002418func addDTLSReplayTests() {
2419 // Test that sequence number replays are detected.
2420 testCases = append(testCases, testCase{
2421 protocol: dtls,
2422 name: "DTLS-Replay",
2423 replayWrites: true,
2424 })
2425
2426 // Test the outgoing sequence number skipping by values larger
2427 // than the retransmit window.
2428 testCases = append(testCases, testCase{
2429 protocol: dtls,
2430 name: "DTLS-Replay-LargeGaps",
2431 config: Config{
2432 Bugs: ProtocolBugs{
2433 SequenceNumberIncrement: 127,
2434 },
2435 },
2436 replayWrites: true,
2437 })
2438}
2439
Feng Lu41aa3252014-11-21 22:47:56 -08002440func addFastRadioPaddingTests() {
David Benjamin1e29a6b2014-12-10 02:27:24 -05002441 testCases = append(testCases, testCase{
2442 protocol: tls,
2443 name: "FastRadio-Padding",
Feng Lu41aa3252014-11-21 22:47:56 -08002444 config: Config{
2445 Bugs: ProtocolBugs{
2446 RequireFastradioPadding: true,
2447 },
2448 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05002449 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08002450 })
David Benjamin1e29a6b2014-12-10 02:27:24 -05002451 testCases = append(testCases, testCase{
2452 protocol: dtls,
2453 name: "FastRadio-Padding",
Feng Lu41aa3252014-11-21 22:47:56 -08002454 config: Config{
2455 Bugs: ProtocolBugs{
2456 RequireFastradioPadding: true,
2457 },
2458 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05002459 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08002460 })
2461}
2462
David Benjamin000800a2014-11-14 01:43:59 -05002463var testHashes = []struct {
2464 name string
2465 id uint8
2466}{
2467 {"SHA1", hashSHA1},
2468 {"SHA224", hashSHA224},
2469 {"SHA256", hashSHA256},
2470 {"SHA384", hashSHA384},
2471 {"SHA512", hashSHA512},
2472}
2473
2474func addSigningHashTests() {
2475 // Make sure each hash works. Include some fake hashes in the list and
2476 // ensure they're ignored.
2477 for _, hash := range testHashes {
2478 testCases = append(testCases, testCase{
2479 name: "SigningHash-ClientAuth-" + hash.name,
2480 config: Config{
2481 ClientAuth: RequireAnyClientCert,
2482 SignatureAndHashes: []signatureAndHash{
2483 {signatureRSA, 42},
2484 {signatureRSA, hash.id},
2485 {signatureRSA, 255},
2486 },
2487 },
2488 flags: []string{
2489 "-cert-file", rsaCertificateFile,
2490 "-key-file", rsaKeyFile,
2491 },
2492 })
2493
2494 testCases = append(testCases, testCase{
2495 testType: serverTest,
2496 name: "SigningHash-ServerKeyExchange-Sign-" + hash.name,
2497 config: Config{
2498 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2499 SignatureAndHashes: []signatureAndHash{
2500 {signatureRSA, 42},
2501 {signatureRSA, hash.id},
2502 {signatureRSA, 255},
2503 },
2504 },
2505 })
2506 }
2507
2508 // Test that hash resolution takes the signature type into account.
2509 testCases = append(testCases, testCase{
2510 name: "SigningHash-ClientAuth-SignatureType",
2511 config: Config{
2512 ClientAuth: RequireAnyClientCert,
2513 SignatureAndHashes: []signatureAndHash{
2514 {signatureECDSA, hashSHA512},
2515 {signatureRSA, hashSHA384},
2516 {signatureECDSA, hashSHA1},
2517 },
2518 },
2519 flags: []string{
2520 "-cert-file", rsaCertificateFile,
2521 "-key-file", rsaKeyFile,
2522 },
2523 })
2524
2525 testCases = append(testCases, testCase{
2526 testType: serverTest,
2527 name: "SigningHash-ServerKeyExchange-SignatureType",
2528 config: Config{
2529 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2530 SignatureAndHashes: []signatureAndHash{
2531 {signatureECDSA, hashSHA512},
2532 {signatureRSA, hashSHA384},
2533 {signatureECDSA, hashSHA1},
2534 },
2535 },
2536 })
2537
2538 // Test that, if the list is missing, the peer falls back to SHA-1.
2539 testCases = append(testCases, testCase{
2540 name: "SigningHash-ClientAuth-Fallback",
2541 config: Config{
2542 ClientAuth: RequireAnyClientCert,
2543 SignatureAndHashes: []signatureAndHash{
2544 {signatureRSA, hashSHA1},
2545 },
2546 Bugs: ProtocolBugs{
2547 NoSignatureAndHashes: true,
2548 },
2549 },
2550 flags: []string{
2551 "-cert-file", rsaCertificateFile,
2552 "-key-file", rsaKeyFile,
2553 },
2554 })
2555
2556 testCases = append(testCases, testCase{
2557 testType: serverTest,
2558 name: "SigningHash-ServerKeyExchange-Fallback",
2559 config: Config{
2560 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2561 SignatureAndHashes: []signatureAndHash{
2562 {signatureRSA, hashSHA1},
2563 },
2564 Bugs: ProtocolBugs{
2565 NoSignatureAndHashes: true,
2566 },
2567 },
2568 })
2569}
2570
David Benjamin83f90402015-01-27 01:09:43 -05002571// timeouts is the retransmit schedule for BoringSSL. It doubles and
2572// caps at 60 seconds. On the 13th timeout, it gives up.
2573var timeouts = []time.Duration{
2574 1 * time.Second,
2575 2 * time.Second,
2576 4 * time.Second,
2577 8 * time.Second,
2578 16 * time.Second,
2579 32 * time.Second,
2580 60 * time.Second,
2581 60 * time.Second,
2582 60 * time.Second,
2583 60 * time.Second,
2584 60 * time.Second,
2585 60 * time.Second,
2586 60 * time.Second,
2587}
2588
2589func addDTLSRetransmitTests() {
2590 // Test that this is indeed the timeout schedule. Stress all
2591 // four patterns of handshake.
2592 for i := 1; i < len(timeouts); i++ {
2593 number := strconv.Itoa(i)
2594 testCases = append(testCases, testCase{
2595 protocol: dtls,
2596 name: "DTLS-Retransmit-Client-" + number,
2597 config: Config{
2598 Bugs: ProtocolBugs{
2599 TimeoutSchedule: timeouts[:i],
2600 },
2601 },
2602 resumeSession: true,
2603 flags: []string{"-async"},
2604 })
2605 testCases = append(testCases, testCase{
2606 protocol: dtls,
2607 testType: serverTest,
2608 name: "DTLS-Retransmit-Server-" + number,
2609 config: Config{
2610 Bugs: ProtocolBugs{
2611 TimeoutSchedule: timeouts[:i],
2612 },
2613 },
2614 resumeSession: true,
2615 flags: []string{"-async"},
2616 })
2617 }
2618
2619 // Test that exceeding the timeout schedule hits a read
2620 // timeout.
2621 testCases = append(testCases, testCase{
2622 protocol: dtls,
2623 name: "DTLS-Retransmit-Timeout",
2624 config: Config{
2625 Bugs: ProtocolBugs{
2626 TimeoutSchedule: timeouts,
2627 },
2628 },
2629 resumeSession: true,
2630 flags: []string{"-async"},
2631 shouldFail: true,
2632 expectedError: ":READ_TIMEOUT_EXPIRED:",
2633 })
2634
2635 // Test that timeout handling has a fudge factor, due to API
2636 // problems.
2637 testCases = append(testCases, testCase{
2638 protocol: dtls,
2639 name: "DTLS-Retransmit-Fudge",
2640 config: Config{
2641 Bugs: ProtocolBugs{
2642 TimeoutSchedule: []time.Duration{
2643 timeouts[0] - 10*time.Millisecond,
2644 },
2645 },
2646 },
2647 resumeSession: true,
2648 flags: []string{"-async"},
2649 })
2650}
2651
David Benjamin884fdf12014-08-02 15:28:23 -04002652func worker(statusChan chan statusMsg, c chan *testCase, buildDir string, wg *sync.WaitGroup) {
Adam Langley95c29f32014-06-20 12:00:00 -07002653 defer wg.Done()
2654
2655 for test := range c {
Adam Langley69a01602014-11-17 17:26:55 -08002656 var err error
2657
2658 if *mallocTest < 0 {
2659 statusChan <- statusMsg{test: test, started: true}
2660 err = runTest(test, buildDir, -1)
2661 } else {
2662 for mallocNumToFail := int64(*mallocTest); ; mallocNumToFail++ {
2663 statusChan <- statusMsg{test: test, started: true}
2664 if err = runTest(test, buildDir, mallocNumToFail); err != errMoreMallocs {
2665 if err != nil {
2666 fmt.Printf("\n\nmalloc test failed at %d: %s\n", mallocNumToFail, err)
2667 }
2668 break
2669 }
2670 }
2671 }
Adam Langley95c29f32014-06-20 12:00:00 -07002672 statusChan <- statusMsg{test: test, err: err}
2673 }
2674}
2675
2676type statusMsg struct {
2677 test *testCase
2678 started bool
2679 err error
2680}
2681
2682func statusPrinter(doneChan chan struct{}, statusChan chan statusMsg, total int) {
2683 var started, done, failed, lineLen int
2684 defer close(doneChan)
2685
2686 for msg := range statusChan {
2687 if msg.started {
2688 started++
2689 } else {
2690 done++
2691 }
2692
2693 fmt.Printf("\x1b[%dD\x1b[K", lineLen)
2694
2695 if msg.err != nil {
2696 fmt.Printf("FAILED (%s)\n%s\n", msg.test.name, msg.err)
2697 failed++
2698 }
2699 line := fmt.Sprintf("%d/%d/%d/%d", failed, done, started, total)
2700 lineLen = len(line)
2701 os.Stdout.WriteString(line)
2702 }
2703}
2704
2705func main() {
2706 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 -04002707 var flagNumWorkers *int = flag.Int("num-workers", runtime.NumCPU(), "The number of workers to run in parallel.")
David Benjamin884fdf12014-08-02 15:28:23 -04002708 var flagBuildDir *string = flag.String("build-dir", "../../../build", "The build directory to run the shim from.")
Adam Langley95c29f32014-06-20 12:00:00 -07002709
2710 flag.Parse()
2711
2712 addCipherSuiteTests()
2713 addBadECDSASignatureTests()
Adam Langley80842bd2014-06-20 12:00:00 -07002714 addCBCPaddingTests()
Kenny Root7fdeaf12014-08-05 15:23:37 -07002715 addCBCSplittingTests()
David Benjamin636293b2014-07-08 17:59:18 -04002716 addClientAuthTests()
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002717 addVersionNegotiationTests()
David Benjaminaccb4542014-12-12 23:44:33 -05002718 addMinimumVersionTests()
David Benjamin5c24a1d2014-08-31 00:59:27 -04002719 addD5BugTests()
David Benjamine78bfde2014-09-06 12:45:15 -04002720 addExtensionTests()
David Benjamin01fe8202014-09-24 15:21:44 -04002721 addResumptionVersionTests()
Adam Langley75712922014-10-10 16:23:43 -07002722 addExtendedMasterSecretTests()
Adam Langley2ae77d22014-10-28 17:29:33 -07002723 addRenegotiationTests()
David Benjamin5e961c12014-11-07 01:48:35 -05002724 addDTLSReplayTests()
David Benjamin000800a2014-11-14 01:43:59 -05002725 addSigningHashTests()
Feng Lu41aa3252014-11-21 22:47:56 -08002726 addFastRadioPaddingTests()
David Benjamin83f90402015-01-27 01:09:43 -05002727 addDTLSRetransmitTests()
David Benjamin43ec06f2014-08-05 02:28:57 -04002728 for _, async := range []bool{false, true} {
2729 for _, splitHandshake := range []bool{false, true} {
David Benjamin6fd297b2014-08-11 18:43:38 -04002730 for _, protocol := range []protocol{tls, dtls} {
2731 addStateMachineCoverageTests(async, splitHandshake, protocol)
2732 }
David Benjamin43ec06f2014-08-05 02:28:57 -04002733 }
2734 }
Adam Langley95c29f32014-06-20 12:00:00 -07002735
2736 var wg sync.WaitGroup
2737
David Benjamin2bc8e6f2014-08-02 15:22:37 -04002738 numWorkers := *flagNumWorkers
Adam Langley95c29f32014-06-20 12:00:00 -07002739
2740 statusChan := make(chan statusMsg, numWorkers)
2741 testChan := make(chan *testCase, numWorkers)
2742 doneChan := make(chan struct{})
2743
David Benjamin025b3d32014-07-01 19:53:04 -04002744 go statusPrinter(doneChan, statusChan, len(testCases))
Adam Langley95c29f32014-06-20 12:00:00 -07002745
2746 for i := 0; i < numWorkers; i++ {
2747 wg.Add(1)
David Benjamin884fdf12014-08-02 15:28:23 -04002748 go worker(statusChan, testChan, *flagBuildDir, &wg)
Adam Langley95c29f32014-06-20 12:00:00 -07002749 }
2750
David Benjamin025b3d32014-07-01 19:53:04 -04002751 for i := range testCases {
2752 if len(*flagTest) == 0 || *flagTest == testCases[i].name {
2753 testChan <- &testCases[i]
Adam Langley95c29f32014-06-20 12:00:00 -07002754 }
2755 }
2756
2757 close(testChan)
2758 wg.Wait()
2759 close(statusChan)
2760 <-doneChan
2761
2762 fmt.Printf("\n")
2763}