blob: 6ee34adc56e14ac48b428b8ec9028cf5a63ec253 [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 {
David Benjamin0ea8dda2015-01-31 20:33:40 -0500391 protocol: dtls,
392 testType: serverTest,
393 name: "FragmentAlert-DTLS",
394 config: Config{
395 Bugs: ProtocolBugs{
396 FragmentAlert: true,
397 SendSpuriousAlert: true,
398 },
399 },
400 shouldFail: true,
401 expectedError: ":BAD_ALERT:",
402 },
403 {
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400404 testType: serverTest,
David Benjaminf3ec83d2014-07-21 22:42:34 -0400405 name: "EarlyChangeCipherSpec-server-1",
406 config: Config{
407 Bugs: ProtocolBugs{
408 EarlyChangeCipherSpec: 1,
409 },
410 },
411 shouldFail: true,
412 expectedError: ":CCS_RECEIVED_EARLY:",
413 },
414 {
415 testType: serverTest,
416 name: "EarlyChangeCipherSpec-server-2",
417 config: Config{
418 Bugs: ProtocolBugs{
419 EarlyChangeCipherSpec: 2,
420 },
421 },
422 shouldFail: true,
423 expectedError: ":CCS_RECEIVED_EARLY:",
424 },
David Benjamind23f4122014-07-23 15:09:48 -0400425 {
David Benjamind23f4122014-07-23 15:09:48 -0400426 name: "SkipNewSessionTicket",
427 config: Config{
428 Bugs: ProtocolBugs{
429 SkipNewSessionTicket: true,
430 },
431 },
432 shouldFail: true,
433 expectedError: ":CCS_RECEIVED_EARLY:",
434 },
David Benjamin7e3305e2014-07-28 14:52:32 -0400435 {
David Benjamind86c7672014-08-02 04:07:12 -0400436 testType: serverTest,
David Benjaminbef270a2014-08-02 04:22:02 -0400437 name: "FallbackSCSV",
438 config: Config{
439 MaxVersion: VersionTLS11,
440 Bugs: ProtocolBugs{
441 SendFallbackSCSV: true,
442 },
443 },
444 shouldFail: true,
445 expectedError: ":INAPPROPRIATE_FALLBACK:",
446 },
447 {
448 testType: serverTest,
449 name: "FallbackSCSV-VersionMatch",
450 config: Config{
451 Bugs: ProtocolBugs{
452 SendFallbackSCSV: true,
453 },
454 },
455 },
David Benjamin98214542014-08-07 18:02:39 -0400456 {
457 testType: serverTest,
458 name: "FragmentedClientVersion",
459 config: Config{
460 Bugs: ProtocolBugs{
461 MaxHandshakeRecordLength: 1,
462 FragmentClientVersion: true,
463 },
464 },
David Benjamin82c9e902014-12-12 15:55:27 -0500465 expectedVersion: VersionTLS12,
David Benjamin98214542014-08-07 18:02:39 -0400466 },
David Benjamin98e882e2014-08-08 13:24:34 -0400467 {
468 testType: serverTest,
469 name: "MinorVersionTolerance",
470 config: Config{
471 Bugs: ProtocolBugs{
472 SendClientVersion: 0x03ff,
473 },
474 },
475 expectedVersion: VersionTLS12,
476 },
477 {
478 testType: serverTest,
479 name: "MajorVersionTolerance",
480 config: Config{
481 Bugs: ProtocolBugs{
482 SendClientVersion: 0x0400,
483 },
484 },
485 expectedVersion: VersionTLS12,
486 },
487 {
488 testType: serverTest,
489 name: "VersionTooLow",
490 config: Config{
491 Bugs: ProtocolBugs{
492 SendClientVersion: 0x0200,
493 },
494 },
495 shouldFail: true,
496 expectedError: ":UNSUPPORTED_PROTOCOL:",
497 },
498 {
499 testType: serverTest,
500 name: "HttpGET",
501 sendPrefix: "GET / HTTP/1.0\n",
502 shouldFail: true,
503 expectedError: ":HTTP_REQUEST:",
504 },
505 {
506 testType: serverTest,
507 name: "HttpPOST",
508 sendPrefix: "POST / HTTP/1.0\n",
509 shouldFail: true,
510 expectedError: ":HTTP_REQUEST:",
511 },
512 {
513 testType: serverTest,
514 name: "HttpHEAD",
515 sendPrefix: "HEAD / HTTP/1.0\n",
516 shouldFail: true,
517 expectedError: ":HTTP_REQUEST:",
518 },
519 {
520 testType: serverTest,
521 name: "HttpPUT",
522 sendPrefix: "PUT / HTTP/1.0\n",
523 shouldFail: true,
524 expectedError: ":HTTP_REQUEST:",
525 },
526 {
527 testType: serverTest,
528 name: "HttpCONNECT",
529 sendPrefix: "CONNECT www.google.com:443 HTTP/1.0\n",
530 shouldFail: true,
531 expectedError: ":HTTPS_PROXY_REQUEST:",
532 },
David Benjamin39ebf532014-08-31 02:23:49 -0400533 {
David Benjaminf080ecd2014-12-11 18:48:59 -0500534 testType: serverTest,
535 name: "Garbage",
536 sendPrefix: "blah",
537 shouldFail: true,
538 expectedError: ":UNKNOWN_PROTOCOL:",
539 },
540 {
David Benjamin39ebf532014-08-31 02:23:49 -0400541 name: "SkipCipherVersionCheck",
542 config: Config{
543 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
544 MaxVersion: VersionTLS11,
545 Bugs: ProtocolBugs{
546 SkipCipherVersionCheck: true,
547 },
548 },
549 shouldFail: true,
550 expectedError: ":WRONG_CIPHER_RETURNED:",
551 },
David Benjamin9114fae2014-11-08 11:41:14 -0500552 {
553 name: "RSAServerKeyExchange",
554 config: Config{
555 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
556 Bugs: ProtocolBugs{
557 RSAServerKeyExchange: true,
558 },
559 },
560 shouldFail: true,
561 expectedError: ":UNEXPECTED_MESSAGE:",
562 },
David Benjamin128dbc32014-12-01 01:27:42 -0500563 {
564 name: "DisableEverything",
565 flags: []string{"-no-tls12", "-no-tls11", "-no-tls1", "-no-ssl3"},
566 shouldFail: true,
567 expectedError: ":WRONG_SSL_VERSION:",
568 },
569 {
570 protocol: dtls,
571 name: "DisableEverything-DTLS",
572 flags: []string{"-no-tls12", "-no-tls1"},
573 shouldFail: true,
574 expectedError: ":WRONG_SSL_VERSION:",
575 },
David Benjamin780d6dd2015-01-06 12:03:19 -0500576 {
577 name: "NoSharedCipher",
578 config: Config{
579 CipherSuites: []uint16{},
580 },
581 shouldFail: true,
582 expectedError: ":HANDSHAKE_FAILURE_ON_CLIENT_HELLO:",
583 },
David Benjamin13be1de2015-01-11 16:29:36 -0500584 {
585 protocol: dtls,
586 testType: serverTest,
587 name: "MTU",
588 config: Config{
589 Bugs: ProtocolBugs{
590 MaxPacketLength: 256,
591 },
592 },
593 flags: []string{"-mtu", "256"},
594 },
595 {
596 protocol: dtls,
597 testType: serverTest,
598 name: "MTUExceeded",
599 config: Config{
600 Bugs: ProtocolBugs{
601 MaxPacketLength: 255,
602 },
603 },
604 flags: []string{"-mtu", "256"},
605 shouldFail: true,
606 expectedLocalError: "dtls: exceeded maximum packet length",
607 },
David Benjamin6095de82014-12-27 01:50:38 -0500608 {
609 name: "CertMismatchRSA",
610 config: Config{
611 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
612 Certificates: []Certificate{getECDSACertificate()},
613 Bugs: ProtocolBugs{
614 SendCipherSuite: TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
615 },
616 },
617 shouldFail: true,
618 expectedError: ":WRONG_CERTIFICATE_TYPE:",
619 },
620 {
621 name: "CertMismatchECDSA",
622 config: Config{
623 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
624 Certificates: []Certificate{getRSACertificate()},
625 Bugs: ProtocolBugs{
626 SendCipherSuite: TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
627 },
628 },
629 shouldFail: true,
630 expectedError: ":WRONG_CERTIFICATE_TYPE:",
631 },
David Benjamin5fa3eba2015-01-22 16:35:40 -0500632 {
633 name: "TLSFatalBadPackets",
634 damageFirstWrite: true,
635 shouldFail: true,
636 expectedError: ":DECRYPTION_FAILED_OR_BAD_RECORD_MAC:",
637 },
638 {
639 protocol: dtls,
640 name: "DTLSIgnoreBadPackets",
641 damageFirstWrite: true,
642 },
643 {
644 protocol: dtls,
645 name: "DTLSIgnoreBadPackets-Async",
646 damageFirstWrite: true,
647 flags: []string{"-async"},
648 },
David Benjamin4189bd92015-01-25 23:52:39 -0500649 {
650 name: "AppDataAfterChangeCipherSpec",
651 config: Config{
652 Bugs: ProtocolBugs{
653 AppDataAfterChangeCipherSpec: []byte("TEST MESSAGE"),
654 },
655 },
656 shouldFail: true,
657 expectedError: ":DATA_BETWEEN_CCS_AND_FINISHED:",
658 },
659 {
660 protocol: dtls,
661 name: "AppDataAfterChangeCipherSpec-DTLS",
662 config: Config{
663 Bugs: ProtocolBugs{
664 AppDataAfterChangeCipherSpec: []byte("TEST MESSAGE"),
665 },
666 },
667 },
David Benjaminb3774b92015-01-31 17:16:01 -0500668 {
669 protocol: dtls,
670 name: "ReorderHandshakeFragments-Small-DTLS",
671 config: Config{
672 Bugs: ProtocolBugs{
673 ReorderHandshakeFragments: true,
674 // Small enough that every handshake message is
675 // fragmented.
676 MaxHandshakeRecordLength: 2,
677 },
678 },
679 },
680 {
681 protocol: dtls,
682 name: "ReorderHandshakeFragments-Large-DTLS",
683 config: Config{
684 Bugs: ProtocolBugs{
685 ReorderHandshakeFragments: true,
686 // Large enough that no handshake message is
687 // fragmented.
688 //
689 // TODO(davidben): Also test interaction of
690 // complete handshake messages with
691 // fragments. The current logic is full of bugs
692 // here, so the reassembly logic needs a rewrite
693 // before those tests will pass.
694 MaxHandshakeRecordLength: 2048,
695 },
696 },
697 },
David Benjaminddb9f152015-02-03 15:44:39 -0500698 {
699 name: "SendInvalidRecordType",
700 config: Config{
701 Bugs: ProtocolBugs{
702 SendInvalidRecordType: true,
703 },
704 },
705 shouldFail: true,
706 expectedError: ":UNEXPECTED_RECORD:",
707 },
708 {
709 protocol: dtls,
710 name: "SendInvalidRecordType-DTLS",
711 config: Config{
712 Bugs: ProtocolBugs{
713 SendInvalidRecordType: true,
714 },
715 },
716 shouldFail: true,
717 expectedError: ":UNEXPECTED_RECORD:",
718 },
Adam Langley95c29f32014-06-20 12:00:00 -0700719}
720
David Benjamin01fe8202014-09-24 15:21:44 -0400721func doExchange(test *testCase, config *Config, conn net.Conn, messageLen int, isResume bool) error {
David Benjamin65ea8ff2014-11-23 03:01:00 -0500722 var connDebug *recordingConn
David Benjamin5fa3eba2015-01-22 16:35:40 -0500723 var connDamage *damageAdaptor
David Benjamin65ea8ff2014-11-23 03:01:00 -0500724 if *flagDebug {
725 connDebug = &recordingConn{Conn: conn}
726 conn = connDebug
727 defer func() {
728 connDebug.WriteTo(os.Stdout)
729 }()
730 }
731
David Benjamin6fd297b2014-08-11 18:43:38 -0400732 if test.protocol == dtls {
David Benjamin83f90402015-01-27 01:09:43 -0500733 config.Bugs.PacketAdaptor = newPacketAdaptor(conn)
734 conn = config.Bugs.PacketAdaptor
David Benjamin5e961c12014-11-07 01:48:35 -0500735 if test.replayWrites {
736 conn = newReplayAdaptor(conn)
737 }
David Benjamin6fd297b2014-08-11 18:43:38 -0400738 }
739
David Benjamin5fa3eba2015-01-22 16:35:40 -0500740 if test.damageFirstWrite {
741 connDamage = newDamageAdaptor(conn)
742 conn = connDamage
743 }
744
David Benjamin6fd297b2014-08-11 18:43:38 -0400745 if test.sendPrefix != "" {
746 if _, err := conn.Write([]byte(test.sendPrefix)); err != nil {
747 return err
748 }
David Benjamin98e882e2014-08-08 13:24:34 -0400749 }
750
David Benjamin1d5c83e2014-07-22 19:20:02 -0400751 var tlsConn *Conn
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400752 if test.testType == clientTest {
David Benjamin6fd297b2014-08-11 18:43:38 -0400753 if test.protocol == dtls {
754 tlsConn = DTLSServer(conn, config)
755 } else {
756 tlsConn = Server(conn, config)
757 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400758 } else {
759 config.InsecureSkipVerify = true
David Benjamin6fd297b2014-08-11 18:43:38 -0400760 if test.protocol == dtls {
761 tlsConn = DTLSClient(conn, config)
762 } else {
763 tlsConn = Client(conn, config)
764 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400765 }
766
Adam Langley95c29f32014-06-20 12:00:00 -0700767 if err := tlsConn.Handshake(); err != nil {
768 return err
769 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700770
David Benjamin01fe8202014-09-24 15:21:44 -0400771 // TODO(davidben): move all per-connection expectations into a dedicated
772 // expectations struct that can be specified separately for the two
773 // legs.
774 expectedVersion := test.expectedVersion
775 if isResume && test.expectedResumeVersion != 0 {
776 expectedVersion = test.expectedResumeVersion
777 }
778 if vers := tlsConn.ConnectionState().Version; expectedVersion != 0 && vers != expectedVersion {
779 return fmt.Errorf("got version %x, expected %x", vers, expectedVersion)
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400780 }
781
David Benjamina08e49d2014-08-24 01:46:07 -0400782 if test.expectChannelID {
783 channelID := tlsConn.ConnectionState().ChannelID
784 if channelID == nil {
785 return fmt.Errorf("no channel ID negotiated")
786 }
787 if channelID.Curve != channelIDKey.Curve ||
788 channelIDKey.X.Cmp(channelIDKey.X) != 0 ||
789 channelIDKey.Y.Cmp(channelIDKey.Y) != 0 {
790 return fmt.Errorf("incorrect channel ID")
791 }
792 }
793
David Benjaminae2888f2014-09-06 12:58:58 -0400794 if expected := test.expectedNextProto; expected != "" {
795 if actual := tlsConn.ConnectionState().NegotiatedProtocol; actual != expected {
796 return fmt.Errorf("next proto mismatch: got %s, wanted %s", actual, expected)
797 }
798 }
799
David Benjaminfc7b0862014-09-06 13:21:53 -0400800 if test.expectedNextProtoType != 0 {
801 if (test.expectedNextProtoType == alpn) != tlsConn.ConnectionState().NegotiatedProtocolFromALPN {
802 return fmt.Errorf("next proto type mismatch")
803 }
804 }
805
David Benjaminca6c8262014-11-15 19:06:08 -0500806 if p := tlsConn.ConnectionState().SRTPProtectionProfile; p != test.expectedSRTPProtectionProfile {
807 return fmt.Errorf("SRTP profile mismatch: got %d, wanted %d", p, test.expectedSRTPProtectionProfile)
808 }
809
David Benjamine58c4f52014-08-24 03:47:07 -0400810 if test.shimWritesFirst {
811 var buf [5]byte
812 _, err := io.ReadFull(tlsConn, buf[:])
813 if err != nil {
814 return err
815 }
816 if string(buf[:]) != "hello" {
817 return fmt.Errorf("bad initial message")
818 }
819 }
820
Adam Langleycf2d4f42014-10-28 19:06:14 -0700821 if test.renegotiate {
822 if test.renegotiateCiphers != nil {
823 config.CipherSuites = test.renegotiateCiphers
824 }
825 if err := tlsConn.Renegotiate(); err != nil {
826 return err
827 }
828 } else if test.renegotiateCiphers != nil {
829 panic("renegotiateCiphers without renegotiate")
830 }
831
David Benjamin5fa3eba2015-01-22 16:35:40 -0500832 if test.damageFirstWrite {
833 connDamage.setDamage(true)
834 tlsConn.Write([]byte("DAMAGED WRITE"))
835 connDamage.setDamage(false)
836 }
837
Kenny Root7fdeaf12014-08-05 15:23:37 -0700838 if messageLen < 0 {
David Benjamin6fd297b2014-08-11 18:43:38 -0400839 if test.protocol == dtls {
840 return fmt.Errorf("messageLen < 0 not supported for DTLS tests")
841 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700842 // Read until EOF.
843 _, err := io.Copy(ioutil.Discard, tlsConn)
844 return err
845 }
846
David Benjamin4189bd92015-01-25 23:52:39 -0500847 var testMessage []byte
848 if config.Bugs.AppDataAfterChangeCipherSpec != nil {
849 // We've already sent a message. Expect the shim to echo it
850 // back.
851 testMessage = config.Bugs.AppDataAfterChangeCipherSpec
852 } else {
853 if messageLen == 0 {
854 messageLen = 32
855 }
856 testMessage = make([]byte, messageLen)
857 for i := range testMessage {
858 testMessage[i] = 0x42
859 }
860 tlsConn.Write(testMessage)
Adam Langley80842bd2014-06-20 12:00:00 -0700861 }
Adam Langley95c29f32014-06-20 12:00:00 -0700862
863 buf := make([]byte, len(testMessage))
David Benjamin6fd297b2014-08-11 18:43:38 -0400864 if test.protocol == dtls {
865 bufTmp := make([]byte, len(buf)+1)
866 n, err := tlsConn.Read(bufTmp)
867 if err != nil {
868 return err
869 }
870 if n != len(buf) {
871 return fmt.Errorf("bad reply; length mismatch (%d vs %d)", n, len(buf))
872 }
873 copy(buf, bufTmp)
874 } else {
875 _, err := io.ReadFull(tlsConn, buf)
876 if err != nil {
877 return err
878 }
Adam Langley95c29f32014-06-20 12:00:00 -0700879 }
880
881 for i, v := range buf {
882 if v != testMessage[i]^0xff {
883 return fmt.Errorf("bad reply contents at byte %d", i)
884 }
885 }
886
887 return nil
888}
889
David Benjamin325b5c32014-07-01 19:40:31 -0400890func valgrindOf(dbAttach bool, path string, args ...string) *exec.Cmd {
891 valgrindArgs := []string{"--error-exitcode=99", "--track-origins=yes", "--leak-check=full"}
Adam Langley95c29f32014-06-20 12:00:00 -0700892 if dbAttach {
David Benjamin325b5c32014-07-01 19:40:31 -0400893 valgrindArgs = append(valgrindArgs, "--db-attach=yes", "--db-command=xterm -e gdb -nw %f %p")
Adam Langley95c29f32014-06-20 12:00:00 -0700894 }
David Benjamin325b5c32014-07-01 19:40:31 -0400895 valgrindArgs = append(valgrindArgs, path)
896 valgrindArgs = append(valgrindArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -0700897
David Benjamin325b5c32014-07-01 19:40:31 -0400898 return exec.Command("valgrind", valgrindArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -0700899}
900
David Benjamin325b5c32014-07-01 19:40:31 -0400901func gdbOf(path string, args ...string) *exec.Cmd {
902 xtermArgs := []string{"-e", "gdb", "--args"}
903 xtermArgs = append(xtermArgs, path)
904 xtermArgs = append(xtermArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -0700905
David Benjamin325b5c32014-07-01 19:40:31 -0400906 return exec.Command("xterm", xtermArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -0700907}
908
David Benjamin1d5c83e2014-07-22 19:20:02 -0400909func openSocketPair() (shimEnd *os.File, conn net.Conn) {
Adam Langley95c29f32014-06-20 12:00:00 -0700910 socks, err := syscall.Socketpair(syscall.AF_UNIX, syscall.SOCK_STREAM, 0)
911 if err != nil {
912 panic(err)
913 }
914
915 syscall.CloseOnExec(socks[0])
916 syscall.CloseOnExec(socks[1])
David Benjamin1d5c83e2014-07-22 19:20:02 -0400917 shimEnd = os.NewFile(uintptr(socks[0]), "shim end")
Adam Langley95c29f32014-06-20 12:00:00 -0700918 connFile := os.NewFile(uintptr(socks[1]), "our end")
David Benjamin1d5c83e2014-07-22 19:20:02 -0400919 conn, err = net.FileConn(connFile)
920 if err != nil {
921 panic(err)
922 }
Adam Langley95c29f32014-06-20 12:00:00 -0700923 connFile.Close()
924 if err != nil {
925 panic(err)
926 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400927 return shimEnd, conn
928}
929
Adam Langley69a01602014-11-17 17:26:55 -0800930type moreMallocsError struct{}
931
932func (moreMallocsError) Error() string {
933 return "child process did not exhaust all allocation calls"
934}
935
936var errMoreMallocs = moreMallocsError{}
937
938func runTest(test *testCase, buildDir string, mallocNumToFail int64) error {
Adam Langley38311732014-10-16 19:04:35 -0700939 if !test.shouldFail && (len(test.expectedError) > 0 || len(test.expectedLocalError) > 0) {
940 panic("Error expected without shouldFail in " + test.name)
941 }
942
David Benjamin1d5c83e2014-07-22 19:20:02 -0400943 shimEnd, conn := openSocketPair()
944 shimEndResume, connResume := openSocketPair()
Adam Langley95c29f32014-06-20 12:00:00 -0700945
David Benjamin884fdf12014-08-02 15:28:23 -0400946 shim_path := path.Join(buildDir, "ssl/test/bssl_shim")
David Benjamin5a593af2014-08-11 19:51:50 -0400947 var flags []string
David Benjamin1d5c83e2014-07-22 19:20:02 -0400948 if test.testType == serverTest {
David Benjamin5a593af2014-08-11 19:51:50 -0400949 flags = append(flags, "-server")
950
David Benjamin025b3d32014-07-01 19:53:04 -0400951 flags = append(flags, "-key-file")
952 if test.keyFile == "" {
953 flags = append(flags, rsaKeyFile)
954 } else {
955 flags = append(flags, test.keyFile)
956 }
957
958 flags = append(flags, "-cert-file")
959 if test.certFile == "" {
960 flags = append(flags, rsaCertificateFile)
961 } else {
962 flags = append(flags, test.certFile)
963 }
964 }
David Benjamin5a593af2014-08-11 19:51:50 -0400965
David Benjamin6fd297b2014-08-11 18:43:38 -0400966 if test.protocol == dtls {
967 flags = append(flags, "-dtls")
968 }
969
David Benjamin5a593af2014-08-11 19:51:50 -0400970 if test.resumeSession {
971 flags = append(flags, "-resume")
972 }
973
David Benjamine58c4f52014-08-24 03:47:07 -0400974 if test.shimWritesFirst {
975 flags = append(flags, "-shim-writes-first")
976 }
977
David Benjamin025b3d32014-07-01 19:53:04 -0400978 flags = append(flags, test.flags...)
979
980 var shim *exec.Cmd
981 if *useValgrind {
982 shim = valgrindOf(false, shim_path, flags...)
Adam Langley75712922014-10-10 16:23:43 -0700983 } else if *useGDB {
984 shim = gdbOf(shim_path, flags...)
David Benjamin025b3d32014-07-01 19:53:04 -0400985 } else {
986 shim = exec.Command(shim_path, flags...)
987 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400988 shim.ExtraFiles = []*os.File{shimEnd, shimEndResume}
David Benjamin025b3d32014-07-01 19:53:04 -0400989 shim.Stdin = os.Stdin
990 var stdoutBuf, stderrBuf bytes.Buffer
991 shim.Stdout = &stdoutBuf
992 shim.Stderr = &stderrBuf
Adam Langley69a01602014-11-17 17:26:55 -0800993 if mallocNumToFail >= 0 {
994 shim.Env = []string{"MALLOC_NUMBER_TO_FAIL=" + strconv.FormatInt(mallocNumToFail, 10)}
995 if *mallocTestDebug {
996 shim.Env = append(shim.Env, "MALLOC_ABORT_ON_FAIL=1")
997 }
998 shim.Env = append(shim.Env, "_MALLOC_CHECK=1")
999 }
David Benjamin025b3d32014-07-01 19:53:04 -04001000
1001 if err := shim.Start(); err != nil {
Adam Langley95c29f32014-06-20 12:00:00 -07001002 panic(err)
1003 }
David Benjamin025b3d32014-07-01 19:53:04 -04001004 shimEnd.Close()
David Benjamin1d5c83e2014-07-22 19:20:02 -04001005 shimEndResume.Close()
Adam Langley95c29f32014-06-20 12:00:00 -07001006
1007 config := test.config
David Benjamin1d5c83e2014-07-22 19:20:02 -04001008 config.ClientSessionCache = NewLRUClientSessionCache(1)
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001009 config.ServerSessionCache = NewLRUServerSessionCache(1)
David Benjamin025b3d32014-07-01 19:53:04 -04001010 if test.testType == clientTest {
1011 if len(config.Certificates) == 0 {
1012 config.Certificates = []Certificate{getRSACertificate()}
1013 }
David Benjamin025b3d32014-07-01 19:53:04 -04001014 }
Adam Langley95c29f32014-06-20 12:00:00 -07001015
David Benjamin01fe8202014-09-24 15:21:44 -04001016 err := doExchange(test, &config, conn, test.messageLen,
1017 false /* not a resumption */)
Adam Langley95c29f32014-06-20 12:00:00 -07001018 conn.Close()
David Benjamin65ea8ff2014-11-23 03:01:00 -05001019
David Benjamin1d5c83e2014-07-22 19:20:02 -04001020 if err == nil && test.resumeSession {
David Benjamin01fe8202014-09-24 15:21:44 -04001021 var resumeConfig Config
1022 if test.resumeConfig != nil {
1023 resumeConfig = *test.resumeConfig
1024 if len(resumeConfig.Certificates) == 0 {
1025 resumeConfig.Certificates = []Certificate{getRSACertificate()}
1026 }
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001027 if !test.newSessionsOnResume {
1028 resumeConfig.SessionTicketKey = config.SessionTicketKey
1029 resumeConfig.ClientSessionCache = config.ClientSessionCache
1030 resumeConfig.ServerSessionCache = config.ServerSessionCache
1031 }
David Benjamin01fe8202014-09-24 15:21:44 -04001032 } else {
1033 resumeConfig = config
1034 }
1035 err = doExchange(test, &resumeConfig, connResume, test.messageLen,
1036 true /* resumption */)
David Benjamin1d5c83e2014-07-22 19:20:02 -04001037 }
David Benjamin812152a2014-09-06 12:49:07 -04001038 connResume.Close()
David Benjamin1d5c83e2014-07-22 19:20:02 -04001039
David Benjamin025b3d32014-07-01 19:53:04 -04001040 childErr := shim.Wait()
Adam Langley69a01602014-11-17 17:26:55 -08001041 if exitError, ok := childErr.(*exec.ExitError); ok {
1042 if exitError.Sys().(syscall.WaitStatus).ExitStatus() == 88 {
1043 return errMoreMallocs
1044 }
1045 }
Adam Langley95c29f32014-06-20 12:00:00 -07001046
1047 stdout := string(stdoutBuf.Bytes())
1048 stderr := string(stderrBuf.Bytes())
1049 failed := err != nil || childErr != nil
1050 correctFailure := len(test.expectedError) == 0 || strings.Contains(stdout, test.expectedError)
Adam Langleyac61fa32014-06-23 12:03:11 -07001051 localError := "none"
1052 if err != nil {
1053 localError = err.Error()
1054 }
1055 if len(test.expectedLocalError) != 0 {
1056 correctFailure = correctFailure && strings.Contains(localError, test.expectedLocalError)
1057 }
Adam Langley95c29f32014-06-20 12:00:00 -07001058
1059 if failed != test.shouldFail || failed && !correctFailure {
Adam Langley95c29f32014-06-20 12:00:00 -07001060 childError := "none"
Adam Langley95c29f32014-06-20 12:00:00 -07001061 if childErr != nil {
1062 childError = childErr.Error()
1063 }
1064
1065 var msg string
1066 switch {
1067 case failed && !test.shouldFail:
1068 msg = "unexpected failure"
1069 case !failed && test.shouldFail:
1070 msg = "unexpected success"
1071 case failed && !correctFailure:
Adam Langleyac61fa32014-06-23 12:03:11 -07001072 msg = "bad error (wanted '" + test.expectedError + "' / '" + test.expectedLocalError + "')"
Adam Langley95c29f32014-06-20 12:00:00 -07001073 default:
1074 panic("internal error")
1075 }
1076
1077 return fmt.Errorf("%s: local error '%s', child error '%s', stdout:\n%s\nstderr:\n%s", msg, localError, childError, string(stdoutBuf.Bytes()), stderr)
1078 }
1079
1080 if !*useValgrind && len(stderr) > 0 {
1081 println(stderr)
1082 }
1083
1084 return nil
1085}
1086
1087var tlsVersions = []struct {
1088 name string
1089 version uint16
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001090 flag string
David Benjamin8b8c0062014-11-23 02:47:52 -05001091 hasDTLS bool
Adam Langley95c29f32014-06-20 12:00:00 -07001092}{
David Benjamin8b8c0062014-11-23 02:47:52 -05001093 {"SSL3", VersionSSL30, "-no-ssl3", false},
1094 {"TLS1", VersionTLS10, "-no-tls1", true},
1095 {"TLS11", VersionTLS11, "-no-tls11", false},
1096 {"TLS12", VersionTLS12, "-no-tls12", true},
Adam Langley95c29f32014-06-20 12:00:00 -07001097}
1098
1099var testCipherSuites = []struct {
1100 name string
1101 id uint16
1102}{
1103 {"3DES-SHA", TLS_RSA_WITH_3DES_EDE_CBC_SHA},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001104 {"AES128-GCM", TLS_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001105 {"AES128-SHA", TLS_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001106 {"AES128-SHA256", TLS_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001107 {"AES256-GCM", TLS_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001108 {"AES256-SHA", TLS_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001109 {"AES256-SHA256", TLS_RSA_WITH_AES_256_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001110 {"DHE-RSA-AES128-GCM", TLS_DHE_RSA_WITH_AES_128_GCM_SHA256},
1111 {"DHE-RSA-AES128-SHA", TLS_DHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001112 {"DHE-RSA-AES128-SHA256", TLS_DHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001113 {"DHE-RSA-AES256-GCM", TLS_DHE_RSA_WITH_AES_256_GCM_SHA384},
1114 {"DHE-RSA-AES256-SHA", TLS_DHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001115 {"DHE-RSA-AES256-SHA256", TLS_DHE_RSA_WITH_AES_256_CBC_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001116 {"ECDHE-ECDSA-AES128-GCM", TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
1117 {"ECDHE-ECDSA-AES128-SHA", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001118 {"ECDHE-ECDSA-AES128-SHA256", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256},
1119 {"ECDHE-ECDSA-AES256-GCM", TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001120 {"ECDHE-ECDSA-AES256-SHA", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001121 {"ECDHE-ECDSA-AES256-SHA384", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001122 {"ECDHE-ECDSA-RC4-SHA", TLS_ECDHE_ECDSA_WITH_RC4_128_SHA},
David Benjamin2af684f2014-10-27 02:23:15 -04001123 {"ECDHE-PSK-WITH-AES-128-GCM-SHA256", TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001124 {"ECDHE-RSA-AES128-GCM", TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001125 {"ECDHE-RSA-AES128-SHA", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001126 {"ECDHE-RSA-AES128-SHA256", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001127 {"ECDHE-RSA-AES256-GCM", TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001128 {"ECDHE-RSA-AES256-SHA", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001129 {"ECDHE-RSA-AES256-SHA384", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001130 {"ECDHE-RSA-RC4-SHA", TLS_ECDHE_RSA_WITH_RC4_128_SHA},
David Benjamin48cae082014-10-27 01:06:24 -04001131 {"PSK-AES128-CBC-SHA", TLS_PSK_WITH_AES_128_CBC_SHA},
1132 {"PSK-AES256-CBC-SHA", TLS_PSK_WITH_AES_256_CBC_SHA},
1133 {"PSK-RC4-SHA", TLS_PSK_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -07001134 {"RC4-MD5", TLS_RSA_WITH_RC4_128_MD5},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001135 {"RC4-SHA", TLS_RSA_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -07001136}
1137
David Benjamin8b8c0062014-11-23 02:47:52 -05001138func hasComponent(suiteName, component string) bool {
1139 return strings.Contains("-"+suiteName+"-", "-"+component+"-")
1140}
1141
David Benjaminf7768e42014-08-31 02:06:47 -04001142func isTLS12Only(suiteName string) bool {
David Benjamin8b8c0062014-11-23 02:47:52 -05001143 return hasComponent(suiteName, "GCM") ||
1144 hasComponent(suiteName, "SHA256") ||
1145 hasComponent(suiteName, "SHA384")
1146}
1147
1148func isDTLSCipher(suiteName string) bool {
David Benjamine95d20d2014-12-23 11:16:01 -05001149 return !hasComponent(suiteName, "RC4")
David Benjaminf7768e42014-08-31 02:06:47 -04001150}
1151
Adam Langley95c29f32014-06-20 12:00:00 -07001152func addCipherSuiteTests() {
1153 for _, suite := range testCipherSuites {
David Benjamin48cae082014-10-27 01:06:24 -04001154 const psk = "12345"
1155 const pskIdentity = "luggage combo"
1156
Adam Langley95c29f32014-06-20 12:00:00 -07001157 var cert Certificate
David Benjamin025b3d32014-07-01 19:53:04 -04001158 var certFile string
1159 var keyFile string
David Benjamin8b8c0062014-11-23 02:47:52 -05001160 if hasComponent(suite.name, "ECDSA") {
Adam Langley95c29f32014-06-20 12:00:00 -07001161 cert = getECDSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -04001162 certFile = ecdsaCertificateFile
1163 keyFile = ecdsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -07001164 } else {
1165 cert = getRSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -04001166 certFile = rsaCertificateFile
1167 keyFile = rsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -07001168 }
1169
David Benjamin48cae082014-10-27 01:06:24 -04001170 var flags []string
David Benjamin8b8c0062014-11-23 02:47:52 -05001171 if hasComponent(suite.name, "PSK") {
David Benjamin48cae082014-10-27 01:06:24 -04001172 flags = append(flags,
1173 "-psk", psk,
1174 "-psk-identity", pskIdentity)
1175 }
1176
Adam Langley95c29f32014-06-20 12:00:00 -07001177 for _, ver := range tlsVersions {
David Benjaminf7768e42014-08-31 02:06:47 -04001178 if ver.version < VersionTLS12 && isTLS12Only(suite.name) {
Adam Langley95c29f32014-06-20 12:00:00 -07001179 continue
1180 }
1181
David Benjamin025b3d32014-07-01 19:53:04 -04001182 testCases = append(testCases, testCase{
1183 testType: clientTest,
1184 name: ver.name + "-" + suite.name + "-client",
Adam Langley95c29f32014-06-20 12:00:00 -07001185 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,
Adam Langley95c29f32014-06-20 12:00:00 -07001192 },
David Benjamin48cae082014-10-27 01:06:24 -04001193 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001194 resumeSession: true,
Adam Langley95c29f32014-06-20 12:00:00 -07001195 })
David Benjamin025b3d32014-07-01 19:53:04 -04001196
David Benjamin76d8abe2014-08-14 16:25:34 -04001197 testCases = append(testCases, testCase{
1198 testType: serverTest,
1199 name: 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 Benjamin76d8abe2014-08-14 16:25:34 -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 Benjamin76d8abe2014-08-14 16:25:34 -04001212 })
David Benjamin6fd297b2014-08-11 18:43:38 -04001213
David Benjamin8b8c0062014-11-23 02:47:52 -05001214 if ver.hasDTLS && isDTLSCipher(suite.name) {
David Benjamin6fd297b2014-08-11 18:43:38 -04001215 testCases = append(testCases, testCase{
1216 testType: clientTest,
1217 protocol: dtls,
1218 name: "D" + ver.name + "-" + suite.name + "-client",
1219 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001220 MinVersion: ver.version,
1221 MaxVersion: ver.version,
1222 CipherSuites: []uint16{suite.id},
1223 Certificates: []Certificate{cert},
1224 PreSharedKey: []byte(psk),
1225 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001226 },
David Benjamin48cae082014-10-27 01:06:24 -04001227 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001228 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001229 })
1230 testCases = append(testCases, testCase{
1231 testType: serverTest,
1232 protocol: dtls,
1233 name: "D" + ver.name + "-" + suite.name + "-server",
1234 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001235 MinVersion: ver.version,
1236 MaxVersion: ver.version,
1237 CipherSuites: []uint16{suite.id},
1238 Certificates: []Certificate{cert},
1239 PreSharedKey: []byte(psk),
1240 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001241 },
1242 certFile: certFile,
1243 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001244 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001245 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001246 })
1247 }
Adam Langley95c29f32014-06-20 12:00:00 -07001248 }
1249 }
1250}
1251
1252func addBadECDSASignatureTests() {
1253 for badR := BadValue(1); badR < NumBadValues; badR++ {
1254 for badS := BadValue(1); badS < NumBadValues; badS++ {
David Benjamin025b3d32014-07-01 19:53:04 -04001255 testCases = append(testCases, testCase{
Adam Langley95c29f32014-06-20 12:00:00 -07001256 name: fmt.Sprintf("BadECDSA-%d-%d", badR, badS),
1257 config: Config{
1258 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
1259 Certificates: []Certificate{getECDSACertificate()},
1260 Bugs: ProtocolBugs{
1261 BadECDSAR: badR,
1262 BadECDSAS: badS,
1263 },
1264 },
1265 shouldFail: true,
1266 expectedError: "SIGNATURE",
1267 })
1268 }
1269 }
1270}
1271
Adam Langley80842bd2014-06-20 12:00:00 -07001272func addCBCPaddingTests() {
David Benjamin025b3d32014-07-01 19:53:04 -04001273 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001274 name: "MaxCBCPadding",
1275 config: Config{
1276 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1277 Bugs: ProtocolBugs{
1278 MaxPadding: true,
1279 },
1280 },
1281 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1282 })
David Benjamin025b3d32014-07-01 19:53:04 -04001283 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001284 name: "BadCBCPadding",
1285 config: Config{
1286 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1287 Bugs: ProtocolBugs{
1288 PaddingFirstByteBad: true,
1289 },
1290 },
1291 shouldFail: true,
1292 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1293 })
1294 // OpenSSL previously had an issue where the first byte of padding in
1295 // 255 bytes of padding wasn't checked.
David Benjamin025b3d32014-07-01 19:53:04 -04001296 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001297 name: "BadCBCPadding255",
1298 config: Config{
1299 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1300 Bugs: ProtocolBugs{
1301 MaxPadding: true,
1302 PaddingFirstByteBadIf255: true,
1303 },
1304 },
1305 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1306 shouldFail: true,
1307 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1308 })
1309}
1310
Kenny Root7fdeaf12014-08-05 15:23:37 -07001311func addCBCSplittingTests() {
1312 testCases = append(testCases, testCase{
1313 name: "CBCRecordSplitting",
1314 config: Config{
1315 MaxVersion: VersionTLS10,
1316 MinVersion: VersionTLS10,
1317 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1318 },
1319 messageLen: -1, // read until EOF
1320 flags: []string{
1321 "-async",
1322 "-write-different-record-sizes",
1323 "-cbc-record-splitting",
1324 },
David Benjamina8e3e0e2014-08-06 22:11:10 -04001325 })
1326 testCases = append(testCases, testCase{
Kenny Root7fdeaf12014-08-05 15:23:37 -07001327 name: "CBCRecordSplittingPartialWrite",
1328 config: Config{
1329 MaxVersion: VersionTLS10,
1330 MinVersion: VersionTLS10,
1331 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1332 },
1333 messageLen: -1, // read until EOF
1334 flags: []string{
1335 "-async",
1336 "-write-different-record-sizes",
1337 "-cbc-record-splitting",
1338 "-partial-write",
1339 },
1340 })
1341}
1342
David Benjamin636293b2014-07-08 17:59:18 -04001343func addClientAuthTests() {
David Benjamin407a10c2014-07-16 12:58:59 -04001344 // Add a dummy cert pool to stress certificate authority parsing.
1345 // TODO(davidben): Add tests that those values parse out correctly.
1346 certPool := x509.NewCertPool()
1347 cert, err := x509.ParseCertificate(rsaCertificate.Certificate[0])
1348 if err != nil {
1349 panic(err)
1350 }
1351 certPool.AddCert(cert)
1352
David Benjamin636293b2014-07-08 17:59:18 -04001353 for _, ver := range tlsVersions {
David Benjamin636293b2014-07-08 17:59:18 -04001354 testCases = append(testCases, testCase{
1355 testType: clientTest,
David Benjamin67666e72014-07-12 15:47:52 -04001356 name: ver.name + "-Client-ClientAuth-RSA",
David Benjamin636293b2014-07-08 17:59:18 -04001357 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001358 MinVersion: ver.version,
1359 MaxVersion: ver.version,
1360 ClientAuth: RequireAnyClientCert,
1361 ClientCAs: certPool,
David Benjamin636293b2014-07-08 17:59:18 -04001362 },
1363 flags: []string{
1364 "-cert-file", rsaCertificateFile,
1365 "-key-file", rsaKeyFile,
1366 },
1367 })
1368 testCases = append(testCases, testCase{
David Benjamin67666e72014-07-12 15:47:52 -04001369 testType: serverTest,
1370 name: ver.name + "-Server-ClientAuth-RSA",
1371 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001372 MinVersion: ver.version,
1373 MaxVersion: ver.version,
David Benjamin67666e72014-07-12 15:47:52 -04001374 Certificates: []Certificate{rsaCertificate},
1375 },
1376 flags: []string{"-require-any-client-certificate"},
1377 })
David Benjamine098ec22014-08-27 23:13:20 -04001378 if ver.version != VersionSSL30 {
1379 testCases = append(testCases, testCase{
1380 testType: serverTest,
1381 name: ver.name + "-Server-ClientAuth-ECDSA",
1382 config: Config{
1383 MinVersion: ver.version,
1384 MaxVersion: ver.version,
1385 Certificates: []Certificate{ecdsaCertificate},
1386 },
1387 flags: []string{"-require-any-client-certificate"},
1388 })
1389 testCases = append(testCases, testCase{
1390 testType: clientTest,
1391 name: ver.name + "-Client-ClientAuth-ECDSA",
1392 config: Config{
1393 MinVersion: ver.version,
1394 MaxVersion: ver.version,
1395 ClientAuth: RequireAnyClientCert,
1396 ClientCAs: certPool,
1397 },
1398 flags: []string{
1399 "-cert-file", ecdsaCertificateFile,
1400 "-key-file", ecdsaKeyFile,
1401 },
1402 })
1403 }
David Benjamin636293b2014-07-08 17:59:18 -04001404 }
1405}
1406
Adam Langley75712922014-10-10 16:23:43 -07001407func addExtendedMasterSecretTests() {
1408 const expectEMSFlag = "-expect-extended-master-secret"
1409
1410 for _, with := range []bool{false, true} {
1411 prefix := "No"
1412 var flags []string
1413 if with {
1414 prefix = ""
1415 flags = []string{expectEMSFlag}
1416 }
1417
1418 for _, isClient := range []bool{false, true} {
1419 suffix := "-Server"
1420 testType := serverTest
1421 if isClient {
1422 suffix = "-Client"
1423 testType = clientTest
1424 }
1425
1426 for _, ver := range tlsVersions {
1427 test := testCase{
1428 testType: testType,
1429 name: prefix + "ExtendedMasterSecret-" + ver.name + suffix,
1430 config: Config{
1431 MinVersion: ver.version,
1432 MaxVersion: ver.version,
1433 Bugs: ProtocolBugs{
1434 NoExtendedMasterSecret: !with,
1435 RequireExtendedMasterSecret: with,
1436 },
1437 },
David Benjamin48cae082014-10-27 01:06:24 -04001438 flags: flags,
1439 shouldFail: ver.version == VersionSSL30 && with,
Adam Langley75712922014-10-10 16:23:43 -07001440 }
1441 if test.shouldFail {
1442 test.expectedLocalError = "extended master secret required but not supported by peer"
1443 }
1444 testCases = append(testCases, test)
1445 }
1446 }
1447 }
1448
1449 // When a session is resumed, it should still be aware that its master
1450 // secret was generated via EMS and thus it's safe to use tls-unique.
1451 testCases = append(testCases, testCase{
1452 name: "ExtendedMasterSecret-Resume",
1453 config: Config{
1454 Bugs: ProtocolBugs{
1455 RequireExtendedMasterSecret: true,
1456 },
1457 },
1458 flags: []string{expectEMSFlag},
1459 resumeSession: true,
1460 })
1461}
1462
David Benjamin43ec06f2014-08-05 02:28:57 -04001463// Adds tests that try to cover the range of the handshake state machine, under
1464// various conditions. Some of these are redundant with other tests, but they
1465// only cover the synchronous case.
David Benjamin6fd297b2014-08-11 18:43:38 -04001466func addStateMachineCoverageTests(async, splitHandshake bool, protocol protocol) {
David Benjamin43ec06f2014-08-05 02:28:57 -04001467 var suffix string
1468 var flags []string
1469 var maxHandshakeRecordLength int
David Benjamin6fd297b2014-08-11 18:43:38 -04001470 if protocol == dtls {
1471 suffix = "-DTLS"
1472 }
David Benjamin43ec06f2014-08-05 02:28:57 -04001473 if async {
David Benjamin6fd297b2014-08-11 18:43:38 -04001474 suffix += "-Async"
David Benjamin43ec06f2014-08-05 02:28:57 -04001475 flags = append(flags, "-async")
1476 } else {
David Benjamin6fd297b2014-08-11 18:43:38 -04001477 suffix += "-Sync"
David Benjamin43ec06f2014-08-05 02:28:57 -04001478 }
1479 if splitHandshake {
1480 suffix += "-SplitHandshakeRecords"
David Benjamin98214542014-08-07 18:02:39 -04001481 maxHandshakeRecordLength = 1
David Benjamin43ec06f2014-08-05 02:28:57 -04001482 }
1483
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001484 // Basic handshake, with resumption. Client and server,
1485 // session ID and session ticket.
David Benjamin43ec06f2014-08-05 02:28:57 -04001486 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001487 protocol: protocol,
1488 name: "Basic-Client" + suffix,
David Benjamin43ec06f2014-08-05 02:28:57 -04001489 config: Config{
1490 Bugs: ProtocolBugs{
1491 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1492 },
1493 },
David Benjaminbed9aae2014-08-07 19:13:38 -04001494 flags: flags,
1495 resumeSession: true,
1496 })
1497 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001498 protocol: protocol,
1499 name: "Basic-Client-RenewTicket" + suffix,
David Benjaminbed9aae2014-08-07 19:13:38 -04001500 config: Config{
1501 Bugs: ProtocolBugs{
1502 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1503 RenewTicketOnResume: true,
1504 },
1505 },
1506 flags: flags,
1507 resumeSession: true,
David Benjamin43ec06f2014-08-05 02:28:57 -04001508 })
1509 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001510 protocol: protocol,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001511 name: "Basic-Client-NoTicket" + suffix,
1512 config: Config{
1513 SessionTicketsDisabled: true,
1514 Bugs: ProtocolBugs{
1515 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1516 },
1517 },
1518 flags: flags,
1519 resumeSession: true,
1520 })
1521 testCases = append(testCases, testCase{
1522 protocol: protocol,
David Benjamin43ec06f2014-08-05 02:28:57 -04001523 testType: serverTest,
1524 name: "Basic-Server" + suffix,
1525 config: Config{
1526 Bugs: ProtocolBugs{
1527 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1528 },
1529 },
David Benjaminbed9aae2014-08-07 19:13:38 -04001530 flags: flags,
1531 resumeSession: true,
David Benjamin43ec06f2014-08-05 02:28:57 -04001532 })
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001533 testCases = append(testCases, testCase{
1534 protocol: protocol,
1535 testType: serverTest,
1536 name: "Basic-Server-NoTickets" + suffix,
1537 config: Config{
1538 SessionTicketsDisabled: true,
1539 Bugs: ProtocolBugs{
1540 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1541 },
1542 },
1543 flags: flags,
1544 resumeSession: true,
1545 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001546
David Benjamin6fd297b2014-08-11 18:43:38 -04001547 // TLS client auth.
1548 testCases = append(testCases, testCase{
1549 protocol: protocol,
1550 testType: clientTest,
1551 name: "ClientAuth-Client" + suffix,
1552 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001553 ClientAuth: RequireAnyClientCert,
David Benjamin6fd297b2014-08-11 18:43:38 -04001554 Bugs: ProtocolBugs{
1555 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1556 },
1557 },
1558 flags: append(flags,
1559 "-cert-file", rsaCertificateFile,
1560 "-key-file", rsaKeyFile),
1561 })
1562 testCases = append(testCases, testCase{
1563 protocol: protocol,
1564 testType: serverTest,
1565 name: "ClientAuth-Server" + suffix,
1566 config: Config{
1567 Certificates: []Certificate{rsaCertificate},
1568 },
1569 flags: append(flags, "-require-any-client-certificate"),
1570 })
1571
David Benjamin43ec06f2014-08-05 02:28:57 -04001572 // No session ticket support; server doesn't send NewSessionTicket.
1573 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001574 protocol: protocol,
1575 name: "SessionTicketsDisabled-Client" + suffix,
David Benjamin43ec06f2014-08-05 02:28:57 -04001576 config: Config{
1577 SessionTicketsDisabled: true,
1578 Bugs: ProtocolBugs{
1579 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1580 },
1581 },
1582 flags: flags,
1583 })
1584 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001585 protocol: protocol,
David Benjamin43ec06f2014-08-05 02:28:57 -04001586 testType: serverTest,
1587 name: "SessionTicketsDisabled-Server" + suffix,
1588 config: Config{
1589 SessionTicketsDisabled: true,
1590 Bugs: ProtocolBugs{
1591 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1592 },
1593 },
1594 flags: flags,
1595 })
1596
David Benjamin48cae082014-10-27 01:06:24 -04001597 // Skip ServerKeyExchange in PSK key exchange if there's no
1598 // identity hint.
1599 testCases = append(testCases, testCase{
1600 protocol: protocol,
1601 name: "EmptyPSKHint-Client" + suffix,
1602 config: Config{
1603 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
1604 PreSharedKey: []byte("secret"),
1605 Bugs: ProtocolBugs{
1606 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1607 },
1608 },
1609 flags: append(flags, "-psk", "secret"),
1610 })
1611 testCases = append(testCases, testCase{
1612 protocol: protocol,
1613 testType: serverTest,
1614 name: "EmptyPSKHint-Server" + suffix,
1615 config: Config{
1616 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
1617 PreSharedKey: []byte("secret"),
1618 Bugs: ProtocolBugs{
1619 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1620 },
1621 },
1622 flags: append(flags, "-psk", "secret"),
1623 })
1624
David Benjamin6fd297b2014-08-11 18:43:38 -04001625 if protocol == tls {
1626 // NPN on client and server; results in post-handshake message.
1627 testCases = append(testCases, testCase{
1628 protocol: protocol,
1629 name: "NPN-Client" + suffix,
1630 config: Config{
David Benjaminae2888f2014-09-06 12:58:58 -04001631 NextProtos: []string{"foo"},
David Benjamin6fd297b2014-08-11 18:43:38 -04001632 Bugs: ProtocolBugs{
1633 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1634 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001635 },
David Benjaminfc7b0862014-09-06 13:21:53 -04001636 flags: append(flags, "-select-next-proto", "foo"),
1637 expectedNextProto: "foo",
1638 expectedNextProtoType: npn,
David Benjamin6fd297b2014-08-11 18:43:38 -04001639 })
1640 testCases = append(testCases, testCase{
1641 protocol: protocol,
1642 testType: serverTest,
1643 name: "NPN-Server" + suffix,
1644 config: Config{
1645 NextProtos: []string{"bar"},
1646 Bugs: ProtocolBugs{
1647 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1648 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001649 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001650 flags: append(flags,
1651 "-advertise-npn", "\x03foo\x03bar\x03baz",
1652 "-expect-next-proto", "bar"),
David Benjaminfc7b0862014-09-06 13:21:53 -04001653 expectedNextProto: "bar",
1654 expectedNextProtoType: npn,
David Benjamin6fd297b2014-08-11 18:43:38 -04001655 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001656
David Benjamin6fd297b2014-08-11 18:43:38 -04001657 // Client does False Start and negotiates NPN.
1658 testCases = append(testCases, testCase{
1659 protocol: protocol,
1660 name: "FalseStart" + suffix,
1661 config: Config{
1662 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1663 NextProtos: []string{"foo"},
1664 Bugs: ProtocolBugs{
David Benjamine58c4f52014-08-24 03:47:07 -04001665 ExpectFalseStart: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001666 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1667 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001668 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001669 flags: append(flags,
1670 "-false-start",
1671 "-select-next-proto", "foo"),
David Benjamine58c4f52014-08-24 03:47:07 -04001672 shimWritesFirst: true,
1673 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001674 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001675
David Benjaminae2888f2014-09-06 12:58:58 -04001676 // Client does False Start and negotiates ALPN.
1677 testCases = append(testCases, testCase{
1678 protocol: protocol,
1679 name: "FalseStart-ALPN" + suffix,
1680 config: Config{
1681 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1682 NextProtos: []string{"foo"},
1683 Bugs: ProtocolBugs{
1684 ExpectFalseStart: true,
1685 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1686 },
1687 },
1688 flags: append(flags,
1689 "-false-start",
1690 "-advertise-alpn", "\x03foo"),
1691 shimWritesFirst: true,
1692 resumeSession: true,
1693 })
1694
David Benjamin6fd297b2014-08-11 18:43:38 -04001695 // False Start without session tickets.
1696 testCases = append(testCases, testCase{
1697 name: "FalseStart-SessionTicketsDisabled",
1698 config: Config{
1699 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1700 NextProtos: []string{"foo"},
1701 SessionTicketsDisabled: true,
David Benjamin4e99c522014-08-24 01:45:30 -04001702 Bugs: ProtocolBugs{
David Benjamine58c4f52014-08-24 03:47:07 -04001703 ExpectFalseStart: true,
David Benjamin4e99c522014-08-24 01:45:30 -04001704 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1705 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001706 },
David Benjamin4e99c522014-08-24 01:45:30 -04001707 flags: append(flags,
David Benjamin6fd297b2014-08-11 18:43:38 -04001708 "-false-start",
1709 "-select-next-proto", "foo",
David Benjamin4e99c522014-08-24 01:45:30 -04001710 ),
David Benjamine58c4f52014-08-24 03:47:07 -04001711 shimWritesFirst: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001712 })
David Benjamin1e7f8d72014-08-08 12:27:04 -04001713
David Benjamina08e49d2014-08-24 01:46:07 -04001714 // Server parses a V2ClientHello.
David Benjamin6fd297b2014-08-11 18:43:38 -04001715 testCases = append(testCases, testCase{
1716 protocol: protocol,
1717 testType: serverTest,
1718 name: "SendV2ClientHello" + suffix,
1719 config: Config{
1720 // Choose a cipher suite that does not involve
1721 // elliptic curves, so no extensions are
1722 // involved.
1723 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
1724 Bugs: ProtocolBugs{
1725 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1726 SendV2ClientHello: true,
1727 },
David Benjamin1e7f8d72014-08-08 12:27:04 -04001728 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001729 flags: flags,
1730 })
David Benjamina08e49d2014-08-24 01:46:07 -04001731
1732 // Client sends a Channel ID.
1733 testCases = append(testCases, testCase{
1734 protocol: protocol,
1735 name: "ChannelID-Client" + suffix,
1736 config: Config{
1737 RequestChannelID: true,
1738 Bugs: ProtocolBugs{
1739 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1740 },
1741 },
1742 flags: append(flags,
1743 "-send-channel-id", channelIDKeyFile,
1744 ),
1745 resumeSession: true,
1746 expectChannelID: true,
1747 })
1748
1749 // Server accepts a Channel ID.
1750 testCases = append(testCases, testCase{
1751 protocol: protocol,
1752 testType: serverTest,
1753 name: "ChannelID-Server" + suffix,
1754 config: Config{
1755 ChannelID: channelIDKey,
1756 Bugs: ProtocolBugs{
1757 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1758 },
1759 },
1760 flags: append(flags,
1761 "-expect-channel-id",
1762 base64.StdEncoding.EncodeToString(channelIDBytes),
1763 ),
1764 resumeSession: true,
1765 expectChannelID: true,
1766 })
David Benjamin6fd297b2014-08-11 18:43:38 -04001767 } else {
1768 testCases = append(testCases, testCase{
1769 protocol: protocol,
1770 name: "SkipHelloVerifyRequest" + suffix,
1771 config: Config{
1772 Bugs: ProtocolBugs{
1773 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1774 SkipHelloVerifyRequest: true,
1775 },
1776 },
1777 flags: flags,
1778 })
1779
1780 testCases = append(testCases, testCase{
1781 testType: serverTest,
1782 protocol: protocol,
1783 name: "CookieExchange" + suffix,
1784 config: Config{
1785 Bugs: ProtocolBugs{
1786 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1787 },
1788 },
1789 flags: append(flags, "-cookie-exchange"),
1790 })
1791 }
David Benjamin43ec06f2014-08-05 02:28:57 -04001792}
1793
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001794func addVersionNegotiationTests() {
1795 for i, shimVers := range tlsVersions {
1796 // Assemble flags to disable all newer versions on the shim.
1797 var flags []string
1798 for _, vers := range tlsVersions[i+1:] {
1799 flags = append(flags, vers.flag)
1800 }
1801
1802 for _, runnerVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05001803 protocols := []protocol{tls}
1804 if runnerVers.hasDTLS && shimVers.hasDTLS {
1805 protocols = append(protocols, dtls)
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001806 }
David Benjamin8b8c0062014-11-23 02:47:52 -05001807 for _, protocol := range protocols {
1808 expectedVersion := shimVers.version
1809 if runnerVers.version < shimVers.version {
1810 expectedVersion = runnerVers.version
1811 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001812
David Benjamin8b8c0062014-11-23 02:47:52 -05001813 suffix := shimVers.name + "-" + runnerVers.name
1814 if protocol == dtls {
1815 suffix += "-DTLS"
1816 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001817
David Benjamin1eb367c2014-12-12 18:17:51 -05001818 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
1819
David Benjamin1e29a6b2014-12-10 02:27:24 -05001820 clientVers := shimVers.version
1821 if clientVers > VersionTLS10 {
1822 clientVers = VersionTLS10
1823 }
David Benjamin8b8c0062014-11-23 02:47:52 -05001824 testCases = append(testCases, testCase{
1825 protocol: protocol,
1826 testType: clientTest,
1827 name: "VersionNegotiation-Client-" + suffix,
1828 config: Config{
1829 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05001830 Bugs: ProtocolBugs{
1831 ExpectInitialRecordVersion: clientVers,
1832 },
David Benjamin8b8c0062014-11-23 02:47:52 -05001833 },
1834 flags: flags,
1835 expectedVersion: expectedVersion,
1836 })
David Benjamin1eb367c2014-12-12 18:17:51 -05001837 testCases = append(testCases, testCase{
1838 protocol: protocol,
1839 testType: clientTest,
1840 name: "VersionNegotiation-Client2-" + suffix,
1841 config: Config{
1842 MaxVersion: runnerVers.version,
1843 Bugs: ProtocolBugs{
1844 ExpectInitialRecordVersion: clientVers,
1845 },
1846 },
1847 flags: []string{"-max-version", shimVersFlag},
1848 expectedVersion: expectedVersion,
1849 })
David Benjamin8b8c0062014-11-23 02:47:52 -05001850
1851 testCases = append(testCases, testCase{
1852 protocol: protocol,
1853 testType: serverTest,
1854 name: "VersionNegotiation-Server-" + suffix,
1855 config: Config{
1856 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05001857 Bugs: ProtocolBugs{
1858 ExpectInitialRecordVersion: expectedVersion,
1859 },
David Benjamin8b8c0062014-11-23 02:47:52 -05001860 },
1861 flags: flags,
1862 expectedVersion: expectedVersion,
1863 })
David Benjamin1eb367c2014-12-12 18:17:51 -05001864 testCases = append(testCases, testCase{
1865 protocol: protocol,
1866 testType: serverTest,
1867 name: "VersionNegotiation-Server2-" + suffix,
1868 config: Config{
1869 MaxVersion: runnerVers.version,
1870 Bugs: ProtocolBugs{
1871 ExpectInitialRecordVersion: expectedVersion,
1872 },
1873 },
1874 flags: []string{"-max-version", shimVersFlag},
1875 expectedVersion: expectedVersion,
1876 })
David Benjamin8b8c0062014-11-23 02:47:52 -05001877 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001878 }
1879 }
1880}
1881
David Benjaminaccb4542014-12-12 23:44:33 -05001882func addMinimumVersionTests() {
1883 for i, shimVers := range tlsVersions {
1884 // Assemble flags to disable all older versions on the shim.
1885 var flags []string
1886 for _, vers := range tlsVersions[:i] {
1887 flags = append(flags, vers.flag)
1888 }
1889
1890 for _, runnerVers := range tlsVersions {
1891 protocols := []protocol{tls}
1892 if runnerVers.hasDTLS && shimVers.hasDTLS {
1893 protocols = append(protocols, dtls)
1894 }
1895 for _, protocol := range protocols {
1896 suffix := shimVers.name + "-" + runnerVers.name
1897 if protocol == dtls {
1898 suffix += "-DTLS"
1899 }
1900 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
1901
David Benjaminaccb4542014-12-12 23:44:33 -05001902 var expectedVersion uint16
1903 var shouldFail bool
1904 var expectedError string
David Benjamin87909c02014-12-13 01:55:01 -05001905 var expectedLocalError string
David Benjaminaccb4542014-12-12 23:44:33 -05001906 if runnerVers.version >= shimVers.version {
1907 expectedVersion = runnerVers.version
1908 } else {
1909 shouldFail = true
1910 expectedError = ":UNSUPPORTED_PROTOCOL:"
David Benjamin87909c02014-12-13 01:55:01 -05001911 if runnerVers.version > VersionSSL30 {
1912 expectedLocalError = "remote error: protocol version not supported"
1913 } else {
1914 expectedLocalError = "remote error: handshake failure"
1915 }
David Benjaminaccb4542014-12-12 23:44:33 -05001916 }
1917
1918 testCases = append(testCases, testCase{
1919 protocol: protocol,
1920 testType: clientTest,
1921 name: "MinimumVersion-Client-" + suffix,
1922 config: Config{
1923 MaxVersion: runnerVers.version,
1924 },
David Benjamin87909c02014-12-13 01:55:01 -05001925 flags: flags,
1926 expectedVersion: expectedVersion,
1927 shouldFail: shouldFail,
1928 expectedError: expectedError,
1929 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05001930 })
1931 testCases = append(testCases, testCase{
1932 protocol: protocol,
1933 testType: clientTest,
1934 name: "MinimumVersion-Client2-" + suffix,
1935 config: Config{
1936 MaxVersion: runnerVers.version,
1937 },
David Benjamin87909c02014-12-13 01:55:01 -05001938 flags: []string{"-min-version", shimVersFlag},
1939 expectedVersion: expectedVersion,
1940 shouldFail: shouldFail,
1941 expectedError: expectedError,
1942 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05001943 })
1944
1945 testCases = append(testCases, testCase{
1946 protocol: protocol,
1947 testType: serverTest,
1948 name: "MinimumVersion-Server-" + suffix,
1949 config: Config{
1950 MaxVersion: runnerVers.version,
1951 },
David Benjamin87909c02014-12-13 01:55:01 -05001952 flags: flags,
1953 expectedVersion: expectedVersion,
1954 shouldFail: shouldFail,
1955 expectedError: expectedError,
1956 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05001957 })
1958 testCases = append(testCases, testCase{
1959 protocol: protocol,
1960 testType: serverTest,
1961 name: "MinimumVersion-Server2-" + suffix,
1962 config: Config{
1963 MaxVersion: runnerVers.version,
1964 },
David Benjamin87909c02014-12-13 01:55:01 -05001965 flags: []string{"-min-version", shimVersFlag},
1966 expectedVersion: expectedVersion,
1967 shouldFail: shouldFail,
1968 expectedError: expectedError,
1969 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05001970 })
1971 }
1972 }
1973 }
1974}
1975
David Benjamin5c24a1d2014-08-31 00:59:27 -04001976func addD5BugTests() {
1977 testCases = append(testCases, testCase{
1978 testType: serverTest,
1979 name: "D5Bug-NoQuirk-Reject",
1980 config: Config{
1981 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
1982 Bugs: ProtocolBugs{
1983 SSL3RSAKeyExchange: true,
1984 },
1985 },
1986 shouldFail: true,
1987 expectedError: ":TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG:",
1988 })
1989 testCases = append(testCases, testCase{
1990 testType: serverTest,
1991 name: "D5Bug-Quirk-Normal",
1992 config: Config{
1993 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
1994 },
1995 flags: []string{"-tls-d5-bug"},
1996 })
1997 testCases = append(testCases, testCase{
1998 testType: serverTest,
1999 name: "D5Bug-Quirk-Bug",
2000 config: Config{
2001 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2002 Bugs: ProtocolBugs{
2003 SSL3RSAKeyExchange: true,
2004 },
2005 },
2006 flags: []string{"-tls-d5-bug"},
2007 })
2008}
2009
David Benjamine78bfde2014-09-06 12:45:15 -04002010func addExtensionTests() {
2011 testCases = append(testCases, testCase{
2012 testType: clientTest,
2013 name: "DuplicateExtensionClient",
2014 config: Config{
2015 Bugs: ProtocolBugs{
2016 DuplicateExtension: true,
2017 },
2018 },
2019 shouldFail: true,
2020 expectedLocalError: "remote error: error decoding message",
2021 })
2022 testCases = append(testCases, testCase{
2023 testType: serverTest,
2024 name: "DuplicateExtensionServer",
2025 config: Config{
2026 Bugs: ProtocolBugs{
2027 DuplicateExtension: true,
2028 },
2029 },
2030 shouldFail: true,
2031 expectedLocalError: "remote error: error decoding message",
2032 })
2033 testCases = append(testCases, testCase{
2034 testType: clientTest,
2035 name: "ServerNameExtensionClient",
2036 config: Config{
2037 Bugs: ProtocolBugs{
2038 ExpectServerName: "example.com",
2039 },
2040 },
2041 flags: []string{"-host-name", "example.com"},
2042 })
2043 testCases = append(testCases, testCase{
2044 testType: clientTest,
2045 name: "ServerNameExtensionClient",
2046 config: Config{
2047 Bugs: ProtocolBugs{
2048 ExpectServerName: "mismatch.com",
2049 },
2050 },
2051 flags: []string{"-host-name", "example.com"},
2052 shouldFail: true,
2053 expectedLocalError: "tls: unexpected server name",
2054 })
2055 testCases = append(testCases, testCase{
2056 testType: clientTest,
2057 name: "ServerNameExtensionClient",
2058 config: Config{
2059 Bugs: ProtocolBugs{
2060 ExpectServerName: "missing.com",
2061 },
2062 },
2063 shouldFail: true,
2064 expectedLocalError: "tls: unexpected server name",
2065 })
2066 testCases = append(testCases, testCase{
2067 testType: serverTest,
2068 name: "ServerNameExtensionServer",
2069 config: Config{
2070 ServerName: "example.com",
2071 },
2072 flags: []string{"-expect-server-name", "example.com"},
2073 resumeSession: true,
2074 })
David Benjaminae2888f2014-09-06 12:58:58 -04002075 testCases = append(testCases, testCase{
2076 testType: clientTest,
2077 name: "ALPNClient",
2078 config: Config{
2079 NextProtos: []string{"foo"},
2080 },
2081 flags: []string{
2082 "-advertise-alpn", "\x03foo\x03bar\x03baz",
2083 "-expect-alpn", "foo",
2084 },
David Benjaminfc7b0862014-09-06 13:21:53 -04002085 expectedNextProto: "foo",
2086 expectedNextProtoType: alpn,
2087 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04002088 })
2089 testCases = append(testCases, testCase{
2090 testType: serverTest,
2091 name: "ALPNServer",
2092 config: Config{
2093 NextProtos: []string{"foo", "bar", "baz"},
2094 },
2095 flags: []string{
2096 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2097 "-select-alpn", "foo",
2098 },
David Benjaminfc7b0862014-09-06 13:21:53 -04002099 expectedNextProto: "foo",
2100 expectedNextProtoType: alpn,
2101 resumeSession: true,
2102 })
2103 // Test that the server prefers ALPN over NPN.
2104 testCases = append(testCases, testCase{
2105 testType: serverTest,
2106 name: "ALPNServer-Preferred",
2107 config: Config{
2108 NextProtos: []string{"foo", "bar", "baz"},
2109 },
2110 flags: []string{
2111 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2112 "-select-alpn", "foo",
2113 "-advertise-npn", "\x03foo\x03bar\x03baz",
2114 },
2115 expectedNextProto: "foo",
2116 expectedNextProtoType: alpn,
2117 resumeSession: true,
2118 })
2119 testCases = append(testCases, testCase{
2120 testType: serverTest,
2121 name: "ALPNServer-Preferred-Swapped",
2122 config: Config{
2123 NextProtos: []string{"foo", "bar", "baz"},
2124 Bugs: ProtocolBugs{
2125 SwapNPNAndALPN: true,
2126 },
2127 },
2128 flags: []string{
2129 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2130 "-select-alpn", "foo",
2131 "-advertise-npn", "\x03foo\x03bar\x03baz",
2132 },
2133 expectedNextProto: "foo",
2134 expectedNextProtoType: alpn,
2135 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04002136 })
Adam Langley38311732014-10-16 19:04:35 -07002137 // Resume with a corrupt ticket.
2138 testCases = append(testCases, testCase{
2139 testType: serverTest,
2140 name: "CorruptTicket",
2141 config: Config{
2142 Bugs: ProtocolBugs{
2143 CorruptTicket: true,
2144 },
2145 },
2146 resumeSession: true,
2147 flags: []string{"-expect-session-miss"},
2148 })
2149 // Resume with an oversized session id.
2150 testCases = append(testCases, testCase{
2151 testType: serverTest,
2152 name: "OversizedSessionId",
2153 config: Config{
2154 Bugs: ProtocolBugs{
2155 OversizedSessionId: true,
2156 },
2157 },
2158 resumeSession: true,
Adam Langley75712922014-10-10 16:23:43 -07002159 shouldFail: true,
Adam Langley38311732014-10-16 19:04:35 -07002160 expectedError: ":DECODE_ERROR:",
2161 })
David Benjaminca6c8262014-11-15 19:06:08 -05002162 // Basic DTLS-SRTP tests. Include fake profiles to ensure they
2163 // are ignored.
2164 testCases = append(testCases, testCase{
2165 protocol: dtls,
2166 name: "SRTP-Client",
2167 config: Config{
2168 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
2169 },
2170 flags: []string{
2171 "-srtp-profiles",
2172 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2173 },
2174 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2175 })
2176 testCases = append(testCases, testCase{
2177 protocol: dtls,
2178 testType: serverTest,
2179 name: "SRTP-Server",
2180 config: Config{
2181 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
2182 },
2183 flags: []string{
2184 "-srtp-profiles",
2185 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2186 },
2187 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2188 })
2189 // Test that the MKI is ignored.
2190 testCases = append(testCases, testCase{
2191 protocol: dtls,
2192 testType: serverTest,
2193 name: "SRTP-Server-IgnoreMKI",
2194 config: Config{
2195 SRTPProtectionProfiles: []uint16{SRTP_AES128_CM_HMAC_SHA1_80},
2196 Bugs: ProtocolBugs{
2197 SRTPMasterKeyIdentifer: "bogus",
2198 },
2199 },
2200 flags: []string{
2201 "-srtp-profiles",
2202 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2203 },
2204 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2205 })
2206 // Test that SRTP isn't negotiated on the server if there were
2207 // no matching profiles.
2208 testCases = append(testCases, testCase{
2209 protocol: dtls,
2210 testType: serverTest,
2211 name: "SRTP-Server-NoMatch",
2212 config: Config{
2213 SRTPProtectionProfiles: []uint16{100, 101, 102},
2214 },
2215 flags: []string{
2216 "-srtp-profiles",
2217 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2218 },
2219 expectedSRTPProtectionProfile: 0,
2220 })
2221 // Test that the server returning an invalid SRTP profile is
2222 // flagged as an error by the client.
2223 testCases = append(testCases, testCase{
2224 protocol: dtls,
2225 name: "SRTP-Client-NoMatch",
2226 config: Config{
2227 Bugs: ProtocolBugs{
2228 SendSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_32,
2229 },
2230 },
2231 flags: []string{
2232 "-srtp-profiles",
2233 "SRTP_AES128_CM_SHA1_80",
2234 },
2235 shouldFail: true,
2236 expectedError: ":BAD_SRTP_PROTECTION_PROFILE_LIST:",
2237 })
David Benjamin61f95272014-11-25 01:55:35 -05002238 // Test OCSP stapling and SCT list.
2239 testCases = append(testCases, testCase{
2240 name: "OCSPStapling",
2241 flags: []string{
2242 "-enable-ocsp-stapling",
2243 "-expect-ocsp-response",
2244 base64.StdEncoding.EncodeToString(testOCSPResponse),
2245 },
2246 })
2247 testCases = append(testCases, testCase{
2248 name: "SignedCertificateTimestampList",
2249 flags: []string{
2250 "-enable-signed-cert-timestamps",
2251 "-expect-signed-cert-timestamps",
2252 base64.StdEncoding.EncodeToString(testSCTList),
2253 },
2254 })
David Benjamine78bfde2014-09-06 12:45:15 -04002255}
2256
David Benjamin01fe8202014-09-24 15:21:44 -04002257func addResumptionVersionTests() {
David Benjamin01fe8202014-09-24 15:21:44 -04002258 for _, sessionVers := range tlsVersions {
David Benjamin01fe8202014-09-24 15:21:44 -04002259 for _, resumeVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05002260 protocols := []protocol{tls}
2261 if sessionVers.hasDTLS && resumeVers.hasDTLS {
2262 protocols = append(protocols, dtls)
David Benjaminbdf5e722014-11-11 00:52:15 -05002263 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002264 for _, protocol := range protocols {
2265 suffix := "-" + sessionVers.name + "-" + resumeVers.name
2266 if protocol == dtls {
2267 suffix += "-DTLS"
2268 }
2269
2270 testCases = append(testCases, testCase{
2271 protocol: protocol,
2272 name: "Resume-Client" + suffix,
2273 resumeSession: true,
2274 config: Config{
2275 MaxVersion: sessionVers.version,
2276 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2277 Bugs: ProtocolBugs{
2278 AllowSessionVersionMismatch: true,
2279 },
2280 },
2281 expectedVersion: sessionVers.version,
2282 resumeConfig: &Config{
2283 MaxVersion: resumeVers.version,
2284 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2285 Bugs: ProtocolBugs{
2286 AllowSessionVersionMismatch: true,
2287 },
2288 },
2289 expectedResumeVersion: resumeVers.version,
2290 })
2291
2292 testCases = append(testCases, testCase{
2293 protocol: protocol,
2294 name: "Resume-Client-NoResume" + suffix,
2295 flags: []string{"-expect-session-miss"},
2296 resumeSession: true,
2297 config: Config{
2298 MaxVersion: sessionVers.version,
2299 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2300 },
2301 expectedVersion: sessionVers.version,
2302 resumeConfig: &Config{
2303 MaxVersion: resumeVers.version,
2304 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2305 },
2306 newSessionsOnResume: true,
2307 expectedResumeVersion: resumeVers.version,
2308 })
2309
2310 var flags []string
2311 if sessionVers.version != resumeVers.version {
2312 flags = append(flags, "-expect-session-miss")
2313 }
2314 testCases = append(testCases, testCase{
2315 protocol: protocol,
2316 testType: serverTest,
2317 name: "Resume-Server" + suffix,
2318 flags: flags,
2319 resumeSession: true,
2320 config: Config{
2321 MaxVersion: sessionVers.version,
2322 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2323 },
2324 expectedVersion: sessionVers.version,
2325 resumeConfig: &Config{
2326 MaxVersion: resumeVers.version,
2327 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2328 },
2329 expectedResumeVersion: resumeVers.version,
2330 })
2331 }
David Benjamin01fe8202014-09-24 15:21:44 -04002332 }
2333 }
2334}
2335
Adam Langley2ae77d22014-10-28 17:29:33 -07002336func addRenegotiationTests() {
2337 testCases = append(testCases, testCase{
2338 testType: serverTest,
2339 name: "Renegotiate-Server",
2340 flags: []string{"-renegotiate"},
2341 shimWritesFirst: true,
2342 })
2343 testCases = append(testCases, testCase{
2344 testType: serverTest,
2345 name: "Renegotiate-Server-EmptyExt",
2346 config: Config{
2347 Bugs: ProtocolBugs{
2348 EmptyRenegotiationInfo: true,
2349 },
2350 },
2351 flags: []string{"-renegotiate"},
2352 shimWritesFirst: true,
2353 shouldFail: true,
2354 expectedError: ":RENEGOTIATION_MISMATCH:",
2355 })
2356 testCases = append(testCases, testCase{
2357 testType: serverTest,
2358 name: "Renegotiate-Server-BadExt",
2359 config: Config{
2360 Bugs: ProtocolBugs{
2361 BadRenegotiationInfo: true,
2362 },
2363 },
2364 flags: []string{"-renegotiate"},
2365 shimWritesFirst: true,
2366 shouldFail: true,
2367 expectedError: ":RENEGOTIATION_MISMATCH:",
2368 })
David Benjaminca6554b2014-11-08 12:31:52 -05002369 testCases = append(testCases, testCase{
2370 testType: serverTest,
2371 name: "Renegotiate-Server-ClientInitiated",
2372 renegotiate: true,
2373 })
2374 testCases = append(testCases, testCase{
2375 testType: serverTest,
2376 name: "Renegotiate-Server-ClientInitiated-NoExt",
2377 renegotiate: true,
2378 config: Config{
2379 Bugs: ProtocolBugs{
2380 NoRenegotiationInfo: true,
2381 },
2382 },
2383 shouldFail: true,
2384 expectedError: ":UNSAFE_LEGACY_RENEGOTIATION_DISABLED:",
2385 })
2386 testCases = append(testCases, testCase{
2387 testType: serverTest,
2388 name: "Renegotiate-Server-ClientInitiated-NoExt-Allowed",
2389 renegotiate: true,
2390 config: Config{
2391 Bugs: ProtocolBugs{
2392 NoRenegotiationInfo: true,
2393 },
2394 },
2395 flags: []string{"-allow-unsafe-legacy-renegotiation"},
2396 })
Adam Langley2ae77d22014-10-28 17:29:33 -07002397 // TODO(agl): test the renegotiation info SCSV.
Adam Langleycf2d4f42014-10-28 19:06:14 -07002398 testCases = append(testCases, testCase{
2399 name: "Renegotiate-Client",
2400 renegotiate: true,
2401 })
2402 testCases = append(testCases, testCase{
2403 name: "Renegotiate-Client-EmptyExt",
2404 renegotiate: true,
2405 config: Config{
2406 Bugs: ProtocolBugs{
2407 EmptyRenegotiationInfo: true,
2408 },
2409 },
2410 shouldFail: true,
2411 expectedError: ":RENEGOTIATION_MISMATCH:",
2412 })
2413 testCases = append(testCases, testCase{
2414 name: "Renegotiate-Client-BadExt",
2415 renegotiate: true,
2416 config: Config{
2417 Bugs: ProtocolBugs{
2418 BadRenegotiationInfo: true,
2419 },
2420 },
2421 shouldFail: true,
2422 expectedError: ":RENEGOTIATION_MISMATCH:",
2423 })
2424 testCases = append(testCases, testCase{
2425 name: "Renegotiate-Client-SwitchCiphers",
2426 renegotiate: true,
2427 config: Config{
2428 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2429 },
2430 renegotiateCiphers: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2431 })
2432 testCases = append(testCases, testCase{
2433 name: "Renegotiate-Client-SwitchCiphers2",
2434 renegotiate: true,
2435 config: Config{
2436 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2437 },
2438 renegotiateCiphers: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2439 })
David Benjaminc44b1df2014-11-23 12:11:01 -05002440 testCases = append(testCases, testCase{
2441 name: "Renegotiate-SameClientVersion",
2442 renegotiate: true,
2443 config: Config{
2444 MaxVersion: VersionTLS10,
2445 Bugs: ProtocolBugs{
2446 RequireSameRenegoClientVersion: true,
2447 },
2448 },
2449 })
Adam Langley2ae77d22014-10-28 17:29:33 -07002450}
2451
David Benjamin5e961c12014-11-07 01:48:35 -05002452func addDTLSReplayTests() {
2453 // Test that sequence number replays are detected.
2454 testCases = append(testCases, testCase{
2455 protocol: dtls,
2456 name: "DTLS-Replay",
2457 replayWrites: true,
2458 })
2459
2460 // Test the outgoing sequence number skipping by values larger
2461 // than the retransmit window.
2462 testCases = append(testCases, testCase{
2463 protocol: dtls,
2464 name: "DTLS-Replay-LargeGaps",
2465 config: Config{
2466 Bugs: ProtocolBugs{
2467 SequenceNumberIncrement: 127,
2468 },
2469 },
2470 replayWrites: true,
2471 })
2472}
2473
Feng Lu41aa3252014-11-21 22:47:56 -08002474func addFastRadioPaddingTests() {
David Benjamin1e29a6b2014-12-10 02:27:24 -05002475 testCases = append(testCases, testCase{
2476 protocol: tls,
2477 name: "FastRadio-Padding",
Feng Lu41aa3252014-11-21 22:47:56 -08002478 config: Config{
2479 Bugs: ProtocolBugs{
2480 RequireFastradioPadding: true,
2481 },
2482 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05002483 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08002484 })
David Benjamin1e29a6b2014-12-10 02:27:24 -05002485 testCases = append(testCases, testCase{
2486 protocol: dtls,
2487 name: "FastRadio-Padding",
Feng Lu41aa3252014-11-21 22:47:56 -08002488 config: Config{
2489 Bugs: ProtocolBugs{
2490 RequireFastradioPadding: true,
2491 },
2492 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05002493 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08002494 })
2495}
2496
David Benjamin000800a2014-11-14 01:43:59 -05002497var testHashes = []struct {
2498 name string
2499 id uint8
2500}{
2501 {"SHA1", hashSHA1},
2502 {"SHA224", hashSHA224},
2503 {"SHA256", hashSHA256},
2504 {"SHA384", hashSHA384},
2505 {"SHA512", hashSHA512},
2506}
2507
2508func addSigningHashTests() {
2509 // Make sure each hash works. Include some fake hashes in the list and
2510 // ensure they're ignored.
2511 for _, hash := range testHashes {
2512 testCases = append(testCases, testCase{
2513 name: "SigningHash-ClientAuth-" + hash.name,
2514 config: Config{
2515 ClientAuth: RequireAnyClientCert,
2516 SignatureAndHashes: []signatureAndHash{
2517 {signatureRSA, 42},
2518 {signatureRSA, hash.id},
2519 {signatureRSA, 255},
2520 },
2521 },
2522 flags: []string{
2523 "-cert-file", rsaCertificateFile,
2524 "-key-file", rsaKeyFile,
2525 },
2526 })
2527
2528 testCases = append(testCases, testCase{
2529 testType: serverTest,
2530 name: "SigningHash-ServerKeyExchange-Sign-" + hash.name,
2531 config: Config{
2532 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2533 SignatureAndHashes: []signatureAndHash{
2534 {signatureRSA, 42},
2535 {signatureRSA, hash.id},
2536 {signatureRSA, 255},
2537 },
2538 },
2539 })
2540 }
2541
2542 // Test that hash resolution takes the signature type into account.
2543 testCases = append(testCases, testCase{
2544 name: "SigningHash-ClientAuth-SignatureType",
2545 config: Config{
2546 ClientAuth: RequireAnyClientCert,
2547 SignatureAndHashes: []signatureAndHash{
2548 {signatureECDSA, hashSHA512},
2549 {signatureRSA, hashSHA384},
2550 {signatureECDSA, hashSHA1},
2551 },
2552 },
2553 flags: []string{
2554 "-cert-file", rsaCertificateFile,
2555 "-key-file", rsaKeyFile,
2556 },
2557 })
2558
2559 testCases = append(testCases, testCase{
2560 testType: serverTest,
2561 name: "SigningHash-ServerKeyExchange-SignatureType",
2562 config: Config{
2563 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2564 SignatureAndHashes: []signatureAndHash{
2565 {signatureECDSA, hashSHA512},
2566 {signatureRSA, hashSHA384},
2567 {signatureECDSA, hashSHA1},
2568 },
2569 },
2570 })
2571
2572 // Test that, if the list is missing, the peer falls back to SHA-1.
2573 testCases = append(testCases, testCase{
2574 name: "SigningHash-ClientAuth-Fallback",
2575 config: Config{
2576 ClientAuth: RequireAnyClientCert,
2577 SignatureAndHashes: []signatureAndHash{
2578 {signatureRSA, hashSHA1},
2579 },
2580 Bugs: ProtocolBugs{
2581 NoSignatureAndHashes: true,
2582 },
2583 },
2584 flags: []string{
2585 "-cert-file", rsaCertificateFile,
2586 "-key-file", rsaKeyFile,
2587 },
2588 })
2589
2590 testCases = append(testCases, testCase{
2591 testType: serverTest,
2592 name: "SigningHash-ServerKeyExchange-Fallback",
2593 config: Config{
2594 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2595 SignatureAndHashes: []signatureAndHash{
2596 {signatureRSA, hashSHA1},
2597 },
2598 Bugs: ProtocolBugs{
2599 NoSignatureAndHashes: true,
2600 },
2601 },
2602 })
2603}
2604
David Benjamin83f90402015-01-27 01:09:43 -05002605// timeouts is the retransmit schedule for BoringSSL. It doubles and
2606// caps at 60 seconds. On the 13th timeout, it gives up.
2607var timeouts = []time.Duration{
2608 1 * time.Second,
2609 2 * time.Second,
2610 4 * time.Second,
2611 8 * time.Second,
2612 16 * time.Second,
2613 32 * time.Second,
2614 60 * time.Second,
2615 60 * time.Second,
2616 60 * time.Second,
2617 60 * time.Second,
2618 60 * time.Second,
2619 60 * time.Second,
2620 60 * time.Second,
2621}
2622
2623func addDTLSRetransmitTests() {
2624 // Test that this is indeed the timeout schedule. Stress all
2625 // four patterns of handshake.
2626 for i := 1; i < len(timeouts); i++ {
2627 number := strconv.Itoa(i)
2628 testCases = append(testCases, testCase{
2629 protocol: dtls,
2630 name: "DTLS-Retransmit-Client-" + number,
2631 config: Config{
2632 Bugs: ProtocolBugs{
2633 TimeoutSchedule: timeouts[:i],
2634 },
2635 },
2636 resumeSession: true,
2637 flags: []string{"-async"},
2638 })
2639 testCases = append(testCases, testCase{
2640 protocol: dtls,
2641 testType: serverTest,
2642 name: "DTLS-Retransmit-Server-" + number,
2643 config: Config{
2644 Bugs: ProtocolBugs{
2645 TimeoutSchedule: timeouts[:i],
2646 },
2647 },
2648 resumeSession: true,
2649 flags: []string{"-async"},
2650 })
2651 }
2652
2653 // Test that exceeding the timeout schedule hits a read
2654 // timeout.
2655 testCases = append(testCases, testCase{
2656 protocol: dtls,
2657 name: "DTLS-Retransmit-Timeout",
2658 config: Config{
2659 Bugs: ProtocolBugs{
2660 TimeoutSchedule: timeouts,
2661 },
2662 },
2663 resumeSession: true,
2664 flags: []string{"-async"},
2665 shouldFail: true,
2666 expectedError: ":READ_TIMEOUT_EXPIRED:",
2667 })
2668
2669 // Test that timeout handling has a fudge factor, due to API
2670 // problems.
2671 testCases = append(testCases, testCase{
2672 protocol: dtls,
2673 name: "DTLS-Retransmit-Fudge",
2674 config: Config{
2675 Bugs: ProtocolBugs{
2676 TimeoutSchedule: []time.Duration{
2677 timeouts[0] - 10*time.Millisecond,
2678 },
2679 },
2680 },
2681 resumeSession: true,
2682 flags: []string{"-async"},
2683 })
2684}
2685
David Benjamin884fdf12014-08-02 15:28:23 -04002686func worker(statusChan chan statusMsg, c chan *testCase, buildDir string, wg *sync.WaitGroup) {
Adam Langley95c29f32014-06-20 12:00:00 -07002687 defer wg.Done()
2688
2689 for test := range c {
Adam Langley69a01602014-11-17 17:26:55 -08002690 var err error
2691
2692 if *mallocTest < 0 {
2693 statusChan <- statusMsg{test: test, started: true}
2694 err = runTest(test, buildDir, -1)
2695 } else {
2696 for mallocNumToFail := int64(*mallocTest); ; mallocNumToFail++ {
2697 statusChan <- statusMsg{test: test, started: true}
2698 if err = runTest(test, buildDir, mallocNumToFail); err != errMoreMallocs {
2699 if err != nil {
2700 fmt.Printf("\n\nmalloc test failed at %d: %s\n", mallocNumToFail, err)
2701 }
2702 break
2703 }
2704 }
2705 }
Adam Langley95c29f32014-06-20 12:00:00 -07002706 statusChan <- statusMsg{test: test, err: err}
2707 }
2708}
2709
2710type statusMsg struct {
2711 test *testCase
2712 started bool
2713 err error
2714}
2715
2716func statusPrinter(doneChan chan struct{}, statusChan chan statusMsg, total int) {
2717 var started, done, failed, lineLen int
2718 defer close(doneChan)
2719
2720 for msg := range statusChan {
2721 if msg.started {
2722 started++
2723 } else {
2724 done++
2725 }
2726
2727 fmt.Printf("\x1b[%dD\x1b[K", lineLen)
2728
2729 if msg.err != nil {
2730 fmt.Printf("FAILED (%s)\n%s\n", msg.test.name, msg.err)
2731 failed++
2732 }
2733 line := fmt.Sprintf("%d/%d/%d/%d", failed, done, started, total)
2734 lineLen = len(line)
2735 os.Stdout.WriteString(line)
2736 }
2737}
2738
2739func main() {
2740 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 -04002741 var flagNumWorkers *int = flag.Int("num-workers", runtime.NumCPU(), "The number of workers to run in parallel.")
David Benjamin884fdf12014-08-02 15:28:23 -04002742 var flagBuildDir *string = flag.String("build-dir", "../../../build", "The build directory to run the shim from.")
Adam Langley95c29f32014-06-20 12:00:00 -07002743
2744 flag.Parse()
2745
2746 addCipherSuiteTests()
2747 addBadECDSASignatureTests()
Adam Langley80842bd2014-06-20 12:00:00 -07002748 addCBCPaddingTests()
Kenny Root7fdeaf12014-08-05 15:23:37 -07002749 addCBCSplittingTests()
David Benjamin636293b2014-07-08 17:59:18 -04002750 addClientAuthTests()
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002751 addVersionNegotiationTests()
David Benjaminaccb4542014-12-12 23:44:33 -05002752 addMinimumVersionTests()
David Benjamin5c24a1d2014-08-31 00:59:27 -04002753 addD5BugTests()
David Benjamine78bfde2014-09-06 12:45:15 -04002754 addExtensionTests()
David Benjamin01fe8202014-09-24 15:21:44 -04002755 addResumptionVersionTests()
Adam Langley75712922014-10-10 16:23:43 -07002756 addExtendedMasterSecretTests()
Adam Langley2ae77d22014-10-28 17:29:33 -07002757 addRenegotiationTests()
David Benjamin5e961c12014-11-07 01:48:35 -05002758 addDTLSReplayTests()
David Benjamin000800a2014-11-14 01:43:59 -05002759 addSigningHashTests()
Feng Lu41aa3252014-11-21 22:47:56 -08002760 addFastRadioPaddingTests()
David Benjamin83f90402015-01-27 01:09:43 -05002761 addDTLSRetransmitTests()
David Benjamin43ec06f2014-08-05 02:28:57 -04002762 for _, async := range []bool{false, true} {
2763 for _, splitHandshake := range []bool{false, true} {
David Benjamin6fd297b2014-08-11 18:43:38 -04002764 for _, protocol := range []protocol{tls, dtls} {
2765 addStateMachineCoverageTests(async, splitHandshake, protocol)
2766 }
David Benjamin43ec06f2014-08-05 02:28:57 -04002767 }
2768 }
Adam Langley95c29f32014-06-20 12:00:00 -07002769
2770 var wg sync.WaitGroup
2771
David Benjamin2bc8e6f2014-08-02 15:22:37 -04002772 numWorkers := *flagNumWorkers
Adam Langley95c29f32014-06-20 12:00:00 -07002773
2774 statusChan := make(chan statusMsg, numWorkers)
2775 testChan := make(chan *testCase, numWorkers)
2776 doneChan := make(chan struct{})
2777
David Benjamin025b3d32014-07-01 19:53:04 -04002778 go statusPrinter(doneChan, statusChan, len(testCases))
Adam Langley95c29f32014-06-20 12:00:00 -07002779
2780 for i := 0; i < numWorkers; i++ {
2781 wg.Add(1)
David Benjamin884fdf12014-08-02 15:28:23 -04002782 go worker(statusChan, testChan, *flagBuildDir, &wg)
Adam Langley95c29f32014-06-20 12:00:00 -07002783 }
2784
David Benjamin025b3d32014-07-01 19:53:04 -04002785 for i := range testCases {
2786 if len(*flagTest) == 0 || *flagTest == testCases[i].name {
2787 testChan <- &testCases[i]
Adam Langley95c29f32014-06-20 12:00:00 -07002788 }
2789 }
2790
2791 close(testChan)
2792 wg.Wait()
2793 close(statusChan)
2794 <-doneChan
2795
2796 fmt.Printf("\n")
2797}