blob: ce0271f712343c1bcd69f442d5eb3e6b2a49b0a1 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001package main
2
3import (
4 "bytes"
David Benjamina08e49d2014-08-24 01:46:07 -04005 "crypto/ecdsa"
6 "crypto/elliptic"
David Benjamin407a10c2014-07-16 12:58:59 -04007 "crypto/x509"
David Benjamin2561dc32014-08-24 01:25:27 -04008 "encoding/base64"
David Benjamina08e49d2014-08-24 01:46:07 -04009 "encoding/pem"
Adam Langley95c29f32014-06-20 12:00:00 -070010 "flag"
11 "fmt"
12 "io"
Kenny Root7fdeaf12014-08-05 15:23:37 -070013 "io/ioutil"
Adam Langley95c29f32014-06-20 12:00:00 -070014 "net"
15 "os"
16 "os/exec"
David Benjamin884fdf12014-08-02 15:28:23 -040017 "path"
David Benjamin2bc8e6f2014-08-02 15:22:37 -040018 "runtime"
Adam Langley69a01602014-11-17 17:26:55 -080019 "strconv"
Adam Langley95c29f32014-06-20 12:00:00 -070020 "strings"
21 "sync"
22 "syscall"
David Benjamin83f90402015-01-27 01:09:43 -050023 "time"
Adam Langley95c29f32014-06-20 12:00:00 -070024)
25
Adam Langley69a01602014-11-17 17:26:55 -080026var (
David Benjamin5f237bc2015-02-11 17:14:15 -050027 useValgrind = flag.Bool("valgrind", false, "If true, run code under valgrind")
28 useGDB = flag.Bool("gdb", false, "If true, run BoringSSL code under gdb")
29 flagDebug = flag.Bool("debug", false, "Hexdump the contents of the connection")
30 mallocTest = flag.Int64("malloc-test", -1, "If non-negative, run each test with each malloc in turn failing from the given number onwards.")
31 mallocTestDebug = flag.Bool("malloc-test-debug", false, "If true, ask bssl_shim to abort rather than fail a malloc. This can be used with a specific value for --malloc-test to identity the malloc failing that is causing problems.")
32 jsonOutput = flag.String("json-output", "", "The file to output JSON results to.")
33 pipe = flag.Bool("pipe", false, "If true, print status output suitable for piping into another program.")
Adam Langley69a01602014-11-17 17:26:55 -080034)
Adam Langley95c29f32014-06-20 12:00:00 -070035
David Benjamin025b3d32014-07-01 19:53:04 -040036const (
37 rsaCertificateFile = "cert.pem"
38 ecdsaCertificateFile = "ecdsa_cert.pem"
39)
40
41const (
David Benjamina08e49d2014-08-24 01:46:07 -040042 rsaKeyFile = "key.pem"
43 ecdsaKeyFile = "ecdsa_key.pem"
44 channelIDKeyFile = "channel_id_key.pem"
David Benjamin025b3d32014-07-01 19:53:04 -040045)
46
Adam Langley95c29f32014-06-20 12:00:00 -070047var rsaCertificate, ecdsaCertificate Certificate
David Benjamina08e49d2014-08-24 01:46:07 -040048var channelIDKey *ecdsa.PrivateKey
49var channelIDBytes []byte
Adam Langley95c29f32014-06-20 12:00:00 -070050
David Benjamin61f95272014-11-25 01:55:35 -050051var testOCSPResponse = []byte{1, 2, 3, 4}
52var testSCTList = []byte{5, 6, 7, 8}
53
Adam Langley95c29f32014-06-20 12:00:00 -070054func initCertificates() {
55 var err error
David Benjamin025b3d32014-07-01 19:53:04 -040056 rsaCertificate, err = LoadX509KeyPair(rsaCertificateFile, rsaKeyFile)
Adam Langley95c29f32014-06-20 12:00:00 -070057 if err != nil {
58 panic(err)
59 }
David Benjamin61f95272014-11-25 01:55:35 -050060 rsaCertificate.OCSPStaple = testOCSPResponse
61 rsaCertificate.SignedCertificateTimestampList = testSCTList
Adam Langley95c29f32014-06-20 12:00:00 -070062
David Benjamin025b3d32014-07-01 19:53:04 -040063 ecdsaCertificate, err = LoadX509KeyPair(ecdsaCertificateFile, ecdsaKeyFile)
Adam Langley95c29f32014-06-20 12:00:00 -070064 if err != nil {
65 panic(err)
66 }
David Benjamin61f95272014-11-25 01:55:35 -050067 ecdsaCertificate.OCSPStaple = testOCSPResponse
68 ecdsaCertificate.SignedCertificateTimestampList = testSCTList
David Benjamina08e49d2014-08-24 01:46:07 -040069
70 channelIDPEMBlock, err := ioutil.ReadFile(channelIDKeyFile)
71 if err != nil {
72 panic(err)
73 }
74 channelIDDERBlock, _ := pem.Decode(channelIDPEMBlock)
75 if channelIDDERBlock.Type != "EC PRIVATE KEY" {
76 panic("bad key type")
77 }
78 channelIDKey, err = x509.ParseECPrivateKey(channelIDDERBlock.Bytes)
79 if err != nil {
80 panic(err)
81 }
82 if channelIDKey.Curve != elliptic.P256() {
83 panic("bad curve")
84 }
85
86 channelIDBytes = make([]byte, 64)
87 writeIntPadded(channelIDBytes[:32], channelIDKey.X)
88 writeIntPadded(channelIDBytes[32:], channelIDKey.Y)
Adam Langley95c29f32014-06-20 12:00:00 -070089}
90
91var certificateOnce sync.Once
92
93func getRSACertificate() Certificate {
94 certificateOnce.Do(initCertificates)
95 return rsaCertificate
96}
97
98func getECDSACertificate() Certificate {
99 certificateOnce.Do(initCertificates)
100 return ecdsaCertificate
101}
102
David Benjamin025b3d32014-07-01 19:53:04 -0400103type testType int
104
105const (
106 clientTest testType = iota
107 serverTest
108)
109
David Benjamin6fd297b2014-08-11 18:43:38 -0400110type protocol int
111
112const (
113 tls protocol = iota
114 dtls
115)
116
David Benjaminfc7b0862014-09-06 13:21:53 -0400117const (
118 alpn = 1
119 npn = 2
120)
121
Adam Langley95c29f32014-06-20 12:00:00 -0700122type testCase struct {
David Benjamin025b3d32014-07-01 19:53:04 -0400123 testType testType
David Benjamin6fd297b2014-08-11 18:43:38 -0400124 protocol protocol
Adam Langley95c29f32014-06-20 12:00:00 -0700125 name string
126 config Config
127 shouldFail bool
128 expectedError string
Adam Langleyac61fa32014-06-23 12:03:11 -0700129 // expectedLocalError, if not empty, contains a substring that must be
130 // found in the local error.
131 expectedLocalError string
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400132 // expectedVersion, if non-zero, specifies the TLS version that must be
133 // negotiated.
134 expectedVersion uint16
David Benjamin01fe8202014-09-24 15:21:44 -0400135 // expectedResumeVersion, if non-zero, specifies the TLS version that
136 // must be negotiated on resumption. If zero, expectedVersion is used.
137 expectedResumeVersion uint16
David Benjamina08e49d2014-08-24 01:46:07 -0400138 // expectChannelID controls whether the connection should have
139 // negotiated a Channel ID with channelIDKey.
140 expectChannelID bool
David Benjaminae2888f2014-09-06 12:58:58 -0400141 // expectedNextProto controls whether the connection should
142 // negotiate a next protocol via NPN or ALPN.
143 expectedNextProto string
David Benjaminfc7b0862014-09-06 13:21:53 -0400144 // expectedNextProtoType, if non-zero, is the expected next
145 // protocol negotiation mechanism.
146 expectedNextProtoType int
David Benjaminca6c8262014-11-15 19:06:08 -0500147 // expectedSRTPProtectionProfile is the DTLS-SRTP profile that
148 // should be negotiated. If zero, none should be negotiated.
149 expectedSRTPProtectionProfile uint16
Adam Langley80842bd2014-06-20 12:00:00 -0700150 // messageLen is the length, in bytes, of the test message that will be
151 // sent.
152 messageLen int
David Benjamin025b3d32014-07-01 19:53:04 -0400153 // certFile is the path to the certificate to use for the server.
154 certFile string
155 // keyFile is the path to the private key to use for the server.
156 keyFile string
David Benjamin1d5c83e2014-07-22 19:20:02 -0400157 // resumeSession controls whether a second connection should be tested
David Benjamin01fe8202014-09-24 15:21:44 -0400158 // which attempts to resume the first session.
David Benjamin1d5c83e2014-07-22 19:20:02 -0400159 resumeSession bool
David Benjamin01fe8202014-09-24 15:21:44 -0400160 // resumeConfig, if not nil, points to a Config to be used on
David Benjaminfe8eb9a2014-11-17 03:19:02 -0500161 // resumption. Unless newSessionsOnResume is set,
162 // SessionTicketKey, ServerSessionCache, and
163 // ClientSessionCache are copied from the initial connection's
164 // config. If nil, the initial connection's config is used.
David Benjamin01fe8202014-09-24 15:21:44 -0400165 resumeConfig *Config
David Benjaminfe8eb9a2014-11-17 03:19:02 -0500166 // newSessionsOnResume, if true, will cause resumeConfig to
167 // use a different session resumption context.
168 newSessionsOnResume bool
David Benjamin98e882e2014-08-08 13:24:34 -0400169 // sendPrefix sends a prefix on the socket before actually performing a
170 // handshake.
171 sendPrefix string
David Benjamine58c4f52014-08-24 03:47:07 -0400172 // shimWritesFirst controls whether the shim sends an initial "hello"
173 // message before doing a roundtrip with the runner.
174 shimWritesFirst bool
Adam Langleycf2d4f42014-10-28 19:06:14 -0700175 // renegotiate indicates the the connection should be renegotiated
176 // during the exchange.
177 renegotiate bool
178 // renegotiateCiphers is a list of ciphersuite ids that will be
179 // switched in just before renegotiation.
180 renegotiateCiphers []uint16
David Benjamin5e961c12014-11-07 01:48:35 -0500181 // replayWrites, if true, configures the underlying transport
182 // to replay every write it makes in DTLS tests.
183 replayWrites bool
David Benjamin5fa3eba2015-01-22 16:35:40 -0500184 // damageFirstWrite, if true, configures the underlying transport to
185 // damage the final byte of the first application data write.
186 damageFirstWrite bool
David Benjaminc565ebb2015-04-03 04:06:36 -0400187 // exportKeyingMaterial, if non-zero, configures the test to exchange
188 // keying material and verify they match.
189 exportKeyingMaterial int
190 exportLabel string
191 exportContext string
192 useExportContext bool
David Benjamin325b5c32014-07-01 19:40:31 -0400193 // flags, if not empty, contains a list of command-line flags that will
194 // be passed to the shim program.
195 flags []string
Adam Langley95c29f32014-06-20 12:00:00 -0700196}
197
David Benjamin025b3d32014-07-01 19:53:04 -0400198var testCases = []testCase{
Adam Langley95c29f32014-06-20 12:00:00 -0700199 {
200 name: "BadRSASignature",
201 config: Config{
202 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
203 Bugs: ProtocolBugs{
204 InvalidSKXSignature: true,
205 },
206 },
207 shouldFail: true,
208 expectedError: ":BAD_SIGNATURE:",
209 },
210 {
211 name: "BadECDSASignature",
212 config: Config{
213 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
214 Bugs: ProtocolBugs{
215 InvalidSKXSignature: true,
216 },
217 Certificates: []Certificate{getECDSACertificate()},
218 },
219 shouldFail: true,
220 expectedError: ":BAD_SIGNATURE:",
221 },
222 {
223 name: "BadECDSACurve",
224 config: Config{
225 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
226 Bugs: ProtocolBugs{
227 InvalidSKXCurve: true,
228 },
229 Certificates: []Certificate{getECDSACertificate()},
230 },
231 shouldFail: true,
232 expectedError: ":WRONG_CURVE:",
233 },
Adam Langleyac61fa32014-06-23 12:03:11 -0700234 {
David Benjamina8e3e0e2014-08-06 22:11:10 -0400235 testType: serverTest,
236 name: "BadRSAVersion",
237 config: Config{
238 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
239 Bugs: ProtocolBugs{
240 RsaClientKeyExchangeVersion: VersionTLS11,
241 },
242 },
243 shouldFail: true,
244 expectedError: ":DECRYPTION_FAILED_OR_BAD_RECORD_MAC:",
245 },
246 {
David Benjamin325b5c32014-07-01 19:40:31 -0400247 name: "NoFallbackSCSV",
Adam Langleyac61fa32014-06-23 12:03:11 -0700248 config: Config{
249 Bugs: ProtocolBugs{
250 FailIfNotFallbackSCSV: true,
251 },
252 },
253 shouldFail: true,
254 expectedLocalError: "no fallback SCSV found",
255 },
David Benjamin325b5c32014-07-01 19:40:31 -0400256 {
David Benjamin2a0c4962014-08-22 23:46:35 -0400257 name: "SendFallbackSCSV",
David Benjamin325b5c32014-07-01 19:40:31 -0400258 config: Config{
259 Bugs: ProtocolBugs{
260 FailIfNotFallbackSCSV: true,
261 },
262 },
263 flags: []string{"-fallback-scsv"},
264 },
David Benjamin197b3ab2014-07-02 18:37:33 -0400265 {
David Benjamin7b030512014-07-08 17:30:11 -0400266 name: "ClientCertificateTypes",
267 config: Config{
268 ClientAuth: RequestClientCert,
269 ClientCertificateTypes: []byte{
270 CertTypeDSSSign,
271 CertTypeRSASign,
272 CertTypeECDSASign,
273 },
274 },
David Benjamin2561dc32014-08-24 01:25:27 -0400275 flags: []string{
276 "-expect-certificate-types",
277 base64.StdEncoding.EncodeToString([]byte{
278 CertTypeDSSSign,
279 CertTypeRSASign,
280 CertTypeECDSASign,
281 }),
282 },
David Benjamin7b030512014-07-08 17:30:11 -0400283 },
David Benjamin636293b2014-07-08 17:59:18 -0400284 {
285 name: "NoClientCertificate",
286 config: Config{
287 ClientAuth: RequireAnyClientCert,
288 },
289 shouldFail: true,
290 expectedLocalError: "client didn't provide a certificate",
291 },
David Benjamin1c375dd2014-07-12 00:48:23 -0400292 {
293 name: "UnauthenticatedECDH",
294 config: Config{
295 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
296 Bugs: ProtocolBugs{
297 UnauthenticatedECDH: true,
298 },
299 },
300 shouldFail: true,
David Benjamine8f3d662014-07-12 01:10:19 -0400301 expectedError: ":UNEXPECTED_MESSAGE:",
David Benjamin1c375dd2014-07-12 00:48:23 -0400302 },
David Benjamin9c651c92014-07-12 13:27:45 -0400303 {
304 name: "SkipServerKeyExchange",
305 config: Config{
306 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
307 Bugs: ProtocolBugs{
308 SkipServerKeyExchange: true,
309 },
310 },
311 shouldFail: true,
312 expectedError: ":UNEXPECTED_MESSAGE:",
313 },
David Benjamin1f5f62b2014-07-12 16:18:02 -0400314 {
David Benjamina0e52232014-07-19 17:39:58 -0400315 name: "SkipChangeCipherSpec-Client",
316 config: Config{
317 Bugs: ProtocolBugs{
318 SkipChangeCipherSpec: true,
319 },
320 },
321 shouldFail: true,
David Benjamin86271ee2014-07-21 16:14:03 -0400322 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
David Benjamina0e52232014-07-19 17:39:58 -0400323 },
324 {
325 testType: serverTest,
326 name: "SkipChangeCipherSpec-Server",
327 config: Config{
328 Bugs: ProtocolBugs{
329 SkipChangeCipherSpec: true,
330 },
331 },
332 shouldFail: true,
David Benjamin86271ee2014-07-21 16:14:03 -0400333 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
David Benjamina0e52232014-07-19 17:39:58 -0400334 },
David Benjamin42be6452014-07-21 14:50:23 -0400335 {
336 testType: serverTest,
337 name: "SkipChangeCipherSpec-Server-NPN",
338 config: Config{
339 NextProtos: []string{"bar"},
340 Bugs: ProtocolBugs{
341 SkipChangeCipherSpec: true,
342 },
343 },
344 flags: []string{
345 "-advertise-npn", "\x03foo\x03bar\x03baz",
346 },
347 shouldFail: true,
David Benjamin86271ee2014-07-21 16:14:03 -0400348 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
349 },
350 {
351 name: "FragmentAcrossChangeCipherSpec-Client",
352 config: Config{
353 Bugs: ProtocolBugs{
354 FragmentAcrossChangeCipherSpec: true,
355 },
356 },
357 shouldFail: true,
358 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
359 },
360 {
361 testType: serverTest,
362 name: "FragmentAcrossChangeCipherSpec-Server",
363 config: Config{
364 Bugs: ProtocolBugs{
365 FragmentAcrossChangeCipherSpec: true,
366 },
367 },
368 shouldFail: true,
369 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
370 },
371 {
372 testType: serverTest,
373 name: "FragmentAcrossChangeCipherSpec-Server-NPN",
374 config: Config{
375 NextProtos: []string{"bar"},
376 Bugs: ProtocolBugs{
377 FragmentAcrossChangeCipherSpec: true,
378 },
379 },
380 flags: []string{
381 "-advertise-npn", "\x03foo\x03bar\x03baz",
382 },
383 shouldFail: true,
384 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
David Benjamin42be6452014-07-21 14:50:23 -0400385 },
David Benjaminf3ec83d2014-07-21 22:42:34 -0400386 {
387 testType: serverTest,
David Benjamin3fd1fbd2015-02-03 16:07:32 -0500388 name: "Alert",
389 config: Config{
390 Bugs: ProtocolBugs{
391 SendSpuriousAlert: alertRecordOverflow,
392 },
393 },
394 shouldFail: true,
395 expectedError: ":TLSV1_ALERT_RECORD_OVERFLOW:",
396 },
397 {
398 protocol: dtls,
399 testType: serverTest,
400 name: "Alert-DTLS",
401 config: Config{
402 Bugs: ProtocolBugs{
403 SendSpuriousAlert: alertRecordOverflow,
404 },
405 },
406 shouldFail: true,
407 expectedError: ":TLSV1_ALERT_RECORD_OVERFLOW:",
408 },
409 {
410 testType: serverTest,
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400411 name: "FragmentAlert",
412 config: Config{
413 Bugs: ProtocolBugs{
David Benjaminca6c8262014-11-15 19:06:08 -0500414 FragmentAlert: true,
David Benjamin3fd1fbd2015-02-03 16:07:32 -0500415 SendSpuriousAlert: alertRecordOverflow,
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400416 },
417 },
418 shouldFail: true,
419 expectedError: ":BAD_ALERT:",
420 },
421 {
David Benjamin0ea8dda2015-01-31 20:33:40 -0500422 protocol: dtls,
423 testType: serverTest,
424 name: "FragmentAlert-DTLS",
425 config: Config{
426 Bugs: ProtocolBugs{
427 FragmentAlert: true,
David Benjamin3fd1fbd2015-02-03 16:07:32 -0500428 SendSpuriousAlert: alertRecordOverflow,
David Benjamin0ea8dda2015-01-31 20:33:40 -0500429 },
430 },
431 shouldFail: true,
432 expectedError: ":BAD_ALERT:",
433 },
434 {
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400435 testType: serverTest,
David Benjaminf3ec83d2014-07-21 22:42:34 -0400436 name: "EarlyChangeCipherSpec-server-1",
437 config: Config{
438 Bugs: ProtocolBugs{
439 EarlyChangeCipherSpec: 1,
440 },
441 },
442 shouldFail: true,
443 expectedError: ":CCS_RECEIVED_EARLY:",
444 },
445 {
446 testType: serverTest,
447 name: "EarlyChangeCipherSpec-server-2",
448 config: Config{
449 Bugs: ProtocolBugs{
450 EarlyChangeCipherSpec: 2,
451 },
452 },
453 shouldFail: true,
454 expectedError: ":CCS_RECEIVED_EARLY:",
455 },
David Benjamind23f4122014-07-23 15:09:48 -0400456 {
David Benjamind23f4122014-07-23 15:09:48 -0400457 name: "SkipNewSessionTicket",
458 config: Config{
459 Bugs: ProtocolBugs{
460 SkipNewSessionTicket: true,
461 },
462 },
463 shouldFail: true,
464 expectedError: ":CCS_RECEIVED_EARLY:",
465 },
David Benjamin7e3305e2014-07-28 14:52:32 -0400466 {
David Benjamind86c7672014-08-02 04:07:12 -0400467 testType: serverTest,
David Benjaminbef270a2014-08-02 04:22:02 -0400468 name: "FallbackSCSV",
469 config: Config{
470 MaxVersion: VersionTLS11,
471 Bugs: ProtocolBugs{
472 SendFallbackSCSV: true,
473 },
474 },
475 shouldFail: true,
476 expectedError: ":INAPPROPRIATE_FALLBACK:",
477 },
478 {
479 testType: serverTest,
480 name: "FallbackSCSV-VersionMatch",
481 config: Config{
482 Bugs: ProtocolBugs{
483 SendFallbackSCSV: true,
484 },
485 },
486 },
David Benjamin98214542014-08-07 18:02:39 -0400487 {
488 testType: serverTest,
489 name: "FragmentedClientVersion",
490 config: Config{
491 Bugs: ProtocolBugs{
492 MaxHandshakeRecordLength: 1,
493 FragmentClientVersion: true,
494 },
495 },
David Benjamin82c9e902014-12-12 15:55:27 -0500496 expectedVersion: VersionTLS12,
David Benjamin98214542014-08-07 18:02:39 -0400497 },
David Benjamin98e882e2014-08-08 13:24:34 -0400498 {
499 testType: serverTest,
500 name: "MinorVersionTolerance",
501 config: Config{
502 Bugs: ProtocolBugs{
503 SendClientVersion: 0x03ff,
504 },
505 },
506 expectedVersion: VersionTLS12,
507 },
508 {
509 testType: serverTest,
510 name: "MajorVersionTolerance",
511 config: Config{
512 Bugs: ProtocolBugs{
513 SendClientVersion: 0x0400,
514 },
515 },
516 expectedVersion: VersionTLS12,
517 },
518 {
519 testType: serverTest,
520 name: "VersionTooLow",
521 config: Config{
522 Bugs: ProtocolBugs{
523 SendClientVersion: 0x0200,
524 },
525 },
526 shouldFail: true,
527 expectedError: ":UNSUPPORTED_PROTOCOL:",
528 },
529 {
530 testType: serverTest,
531 name: "HttpGET",
532 sendPrefix: "GET / HTTP/1.0\n",
533 shouldFail: true,
534 expectedError: ":HTTP_REQUEST:",
535 },
536 {
537 testType: serverTest,
538 name: "HttpPOST",
539 sendPrefix: "POST / HTTP/1.0\n",
540 shouldFail: true,
541 expectedError: ":HTTP_REQUEST:",
542 },
543 {
544 testType: serverTest,
545 name: "HttpHEAD",
546 sendPrefix: "HEAD / HTTP/1.0\n",
547 shouldFail: true,
548 expectedError: ":HTTP_REQUEST:",
549 },
550 {
551 testType: serverTest,
552 name: "HttpPUT",
553 sendPrefix: "PUT / HTTP/1.0\n",
554 shouldFail: true,
555 expectedError: ":HTTP_REQUEST:",
556 },
557 {
558 testType: serverTest,
559 name: "HttpCONNECT",
560 sendPrefix: "CONNECT www.google.com:443 HTTP/1.0\n",
561 shouldFail: true,
562 expectedError: ":HTTPS_PROXY_REQUEST:",
563 },
David Benjamin39ebf532014-08-31 02:23:49 -0400564 {
David Benjaminf080ecd2014-12-11 18:48:59 -0500565 testType: serverTest,
566 name: "Garbage",
567 sendPrefix: "blah",
568 shouldFail: true,
569 expectedError: ":UNKNOWN_PROTOCOL:",
570 },
571 {
David Benjamin39ebf532014-08-31 02:23:49 -0400572 name: "SkipCipherVersionCheck",
573 config: Config{
574 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
575 MaxVersion: VersionTLS11,
576 Bugs: ProtocolBugs{
577 SkipCipherVersionCheck: true,
578 },
579 },
580 shouldFail: true,
581 expectedError: ":WRONG_CIPHER_RETURNED:",
582 },
David Benjamin9114fae2014-11-08 11:41:14 -0500583 {
David Benjamina3e89492015-02-26 15:16:22 -0500584 name: "RSAEphemeralKey",
David Benjamin9114fae2014-11-08 11:41:14 -0500585 config: Config{
586 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
587 Bugs: ProtocolBugs{
David Benjamina3e89492015-02-26 15:16:22 -0500588 RSAEphemeralKey: true,
David Benjamin9114fae2014-11-08 11:41:14 -0500589 },
590 },
591 shouldFail: true,
592 expectedError: ":UNEXPECTED_MESSAGE:",
593 },
David Benjamin128dbc32014-12-01 01:27:42 -0500594 {
595 name: "DisableEverything",
596 flags: []string{"-no-tls12", "-no-tls11", "-no-tls1", "-no-ssl3"},
597 shouldFail: true,
598 expectedError: ":WRONG_SSL_VERSION:",
599 },
600 {
601 protocol: dtls,
602 name: "DisableEverything-DTLS",
603 flags: []string{"-no-tls12", "-no-tls1"},
604 shouldFail: true,
605 expectedError: ":WRONG_SSL_VERSION:",
606 },
David Benjamin780d6dd2015-01-06 12:03:19 -0500607 {
608 name: "NoSharedCipher",
609 config: Config{
610 CipherSuites: []uint16{},
611 },
612 shouldFail: true,
613 expectedError: ":HANDSHAKE_FAILURE_ON_CLIENT_HELLO:",
614 },
David Benjamin13be1de2015-01-11 16:29:36 -0500615 {
616 protocol: dtls,
617 testType: serverTest,
618 name: "MTU",
619 config: Config{
620 Bugs: ProtocolBugs{
621 MaxPacketLength: 256,
622 },
623 },
624 flags: []string{"-mtu", "256"},
625 },
626 {
627 protocol: dtls,
628 testType: serverTest,
629 name: "MTUExceeded",
630 config: Config{
631 Bugs: ProtocolBugs{
632 MaxPacketLength: 255,
633 },
634 },
635 flags: []string{"-mtu", "256"},
636 shouldFail: true,
637 expectedLocalError: "dtls: exceeded maximum packet length",
638 },
David Benjamin6095de82014-12-27 01:50:38 -0500639 {
640 name: "CertMismatchRSA",
641 config: Config{
642 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
643 Certificates: []Certificate{getECDSACertificate()},
644 Bugs: ProtocolBugs{
645 SendCipherSuite: TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
646 },
647 },
648 shouldFail: true,
649 expectedError: ":WRONG_CERTIFICATE_TYPE:",
650 },
651 {
652 name: "CertMismatchECDSA",
653 config: Config{
654 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
655 Certificates: []Certificate{getRSACertificate()},
656 Bugs: ProtocolBugs{
657 SendCipherSuite: TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
658 },
659 },
660 shouldFail: true,
661 expectedError: ":WRONG_CERTIFICATE_TYPE:",
662 },
David Benjamin5fa3eba2015-01-22 16:35:40 -0500663 {
664 name: "TLSFatalBadPackets",
665 damageFirstWrite: true,
666 shouldFail: true,
667 expectedError: ":DECRYPTION_FAILED_OR_BAD_RECORD_MAC:",
668 },
669 {
670 protocol: dtls,
671 name: "DTLSIgnoreBadPackets",
672 damageFirstWrite: true,
673 },
674 {
675 protocol: dtls,
676 name: "DTLSIgnoreBadPackets-Async",
677 damageFirstWrite: true,
678 flags: []string{"-async"},
679 },
David Benjamin4189bd92015-01-25 23:52:39 -0500680 {
681 name: "AppDataAfterChangeCipherSpec",
682 config: Config{
683 Bugs: ProtocolBugs{
684 AppDataAfterChangeCipherSpec: []byte("TEST MESSAGE"),
685 },
686 },
687 shouldFail: true,
688 expectedError: ":DATA_BETWEEN_CCS_AND_FINISHED:",
689 },
690 {
691 protocol: dtls,
692 name: "AppDataAfterChangeCipherSpec-DTLS",
693 config: Config{
694 Bugs: ProtocolBugs{
695 AppDataAfterChangeCipherSpec: []byte("TEST MESSAGE"),
696 },
697 },
David Benjamin4417d052015-04-05 04:17:25 -0400698 // BoringSSL's DTLS implementation will drop the out-of-order
699 // application data.
David Benjamin4189bd92015-01-25 23:52:39 -0500700 },
David Benjaminb3774b92015-01-31 17:16:01 -0500701 {
David Benjamindc3da932015-03-12 15:09:02 -0400702 name: "AlertAfterChangeCipherSpec",
703 config: Config{
704 Bugs: ProtocolBugs{
705 AlertAfterChangeCipherSpec: alertRecordOverflow,
706 },
707 },
708 shouldFail: true,
709 expectedError: ":TLSV1_ALERT_RECORD_OVERFLOW:",
710 },
711 {
712 protocol: dtls,
713 name: "AlertAfterChangeCipherSpec-DTLS",
714 config: Config{
715 Bugs: ProtocolBugs{
716 AlertAfterChangeCipherSpec: alertRecordOverflow,
717 },
718 },
719 shouldFail: true,
720 expectedError: ":TLSV1_ALERT_RECORD_OVERFLOW:",
721 },
722 {
David Benjaminb3774b92015-01-31 17:16:01 -0500723 protocol: dtls,
724 name: "ReorderHandshakeFragments-Small-DTLS",
725 config: Config{
726 Bugs: ProtocolBugs{
727 ReorderHandshakeFragments: true,
728 // Small enough that every handshake message is
729 // fragmented.
730 MaxHandshakeRecordLength: 2,
731 },
732 },
733 },
734 {
735 protocol: dtls,
736 name: "ReorderHandshakeFragments-Large-DTLS",
737 config: Config{
738 Bugs: ProtocolBugs{
739 ReorderHandshakeFragments: true,
740 // Large enough that no handshake message is
741 // fragmented.
David Benjaminb3774b92015-01-31 17:16:01 -0500742 MaxHandshakeRecordLength: 2048,
743 },
744 },
745 },
David Benjaminddb9f152015-02-03 15:44:39 -0500746 {
David Benjamin75381222015-03-02 19:30:30 -0500747 protocol: dtls,
748 name: "MixCompleteMessageWithFragments-DTLS",
749 config: Config{
750 Bugs: ProtocolBugs{
751 ReorderHandshakeFragments: true,
752 MixCompleteMessageWithFragments: true,
753 MaxHandshakeRecordLength: 2,
754 },
755 },
756 },
757 {
David Benjaminddb9f152015-02-03 15:44:39 -0500758 name: "SendInvalidRecordType",
759 config: Config{
760 Bugs: ProtocolBugs{
761 SendInvalidRecordType: true,
762 },
763 },
764 shouldFail: true,
765 expectedError: ":UNEXPECTED_RECORD:",
766 },
767 {
768 protocol: dtls,
769 name: "SendInvalidRecordType-DTLS",
770 config: Config{
771 Bugs: ProtocolBugs{
772 SendInvalidRecordType: true,
773 },
774 },
775 shouldFail: true,
776 expectedError: ":UNEXPECTED_RECORD:",
777 },
David Benjaminb80168e2015-02-08 18:30:14 -0500778 {
779 name: "FalseStart-SkipServerSecondLeg",
780 config: Config{
781 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
782 NextProtos: []string{"foo"},
783 Bugs: ProtocolBugs{
784 SkipNewSessionTicket: true,
785 SkipChangeCipherSpec: true,
786 SkipFinished: true,
787 ExpectFalseStart: true,
788 },
789 },
790 flags: []string{
791 "-false-start",
David Benjamin87e4acd2015-04-02 19:57:35 -0400792 "-handshake-never-done",
David Benjaminb80168e2015-02-08 18:30:14 -0500793 "-advertise-alpn", "\x03foo",
794 },
795 shimWritesFirst: true,
796 shouldFail: true,
797 expectedError: ":UNEXPECTED_RECORD:",
798 },
David Benjamin931ab342015-02-08 19:46:57 -0500799 {
800 name: "FalseStart-SkipServerSecondLeg-Implicit",
801 config: Config{
802 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
803 NextProtos: []string{"foo"},
804 Bugs: ProtocolBugs{
805 SkipNewSessionTicket: true,
806 SkipChangeCipherSpec: true,
807 SkipFinished: true,
808 },
809 },
810 flags: []string{
811 "-implicit-handshake",
812 "-false-start",
David Benjamin87e4acd2015-04-02 19:57:35 -0400813 "-handshake-never-done",
David Benjamin931ab342015-02-08 19:46:57 -0500814 "-advertise-alpn", "\x03foo",
815 },
816 shouldFail: true,
817 expectedError: ":UNEXPECTED_RECORD:",
818 },
David Benjamin6f5c0f42015-02-24 01:23:21 -0500819 {
820 testType: serverTest,
821 name: "FailEarlyCallback",
822 flags: []string{"-fail-early-callback"},
823 shouldFail: true,
824 expectedError: ":CONNECTION_REJECTED:",
825 expectedLocalError: "remote error: access denied",
826 },
David Benjaminbcb2d912015-02-24 23:45:43 -0500827 {
828 name: "WrongMessageType",
829 config: Config{
830 Bugs: ProtocolBugs{
831 WrongCertificateMessageType: true,
832 },
833 },
834 shouldFail: true,
835 expectedError: ":UNEXPECTED_MESSAGE:",
836 expectedLocalError: "remote error: unexpected message",
837 },
838 {
839 protocol: dtls,
840 name: "WrongMessageType-DTLS",
841 config: Config{
842 Bugs: ProtocolBugs{
843 WrongCertificateMessageType: true,
844 },
845 },
846 shouldFail: true,
847 expectedError: ":UNEXPECTED_MESSAGE:",
848 expectedLocalError: "remote error: unexpected message",
849 },
David Benjamin75381222015-03-02 19:30:30 -0500850 {
851 protocol: dtls,
852 name: "FragmentMessageTypeMismatch-DTLS",
853 config: Config{
854 Bugs: ProtocolBugs{
855 MaxHandshakeRecordLength: 2,
856 FragmentMessageTypeMismatch: true,
857 },
858 },
859 shouldFail: true,
860 expectedError: ":FRAGMENT_MISMATCH:",
861 },
862 {
863 protocol: dtls,
864 name: "FragmentMessageLengthMismatch-DTLS",
865 config: Config{
866 Bugs: ProtocolBugs{
867 MaxHandshakeRecordLength: 2,
868 FragmentMessageLengthMismatch: true,
869 },
870 },
871 shouldFail: true,
872 expectedError: ":FRAGMENT_MISMATCH:",
873 },
874 {
875 protocol: dtls,
876 name: "SplitFragmentHeader-DTLS",
877 config: Config{
878 Bugs: ProtocolBugs{
879 SplitFragmentHeader: true,
880 },
881 },
882 shouldFail: true,
883 expectedError: ":UNEXPECTED_MESSAGE:",
884 },
885 {
886 protocol: dtls,
887 name: "SplitFragmentBody-DTLS",
888 config: Config{
889 Bugs: ProtocolBugs{
890 SplitFragmentBody: true,
891 },
892 },
893 shouldFail: true,
894 expectedError: ":UNEXPECTED_MESSAGE:",
895 },
896 {
897 protocol: dtls,
898 name: "SendEmptyFragments-DTLS",
899 config: Config{
900 Bugs: ProtocolBugs{
901 SendEmptyFragments: true,
902 },
903 },
904 },
David Benjamin67d1fb52015-03-16 15:16:23 -0400905 {
906 name: "UnsupportedCipherSuite",
907 config: Config{
908 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
909 Bugs: ProtocolBugs{
910 IgnorePeerCipherPreferences: true,
911 },
912 },
913 flags: []string{"-cipher", "DEFAULT:!RC4"},
914 shouldFail: true,
915 expectedError: ":WRONG_CIPHER_RETURNED:",
916 },
David Benjamin340d5ed2015-03-21 02:21:37 -0400917 {
918 name: "SendWarningAlerts",
919 config: Config{
920 Bugs: ProtocolBugs{
921 SendWarningAlerts: alertAccessDenied,
922 },
923 },
924 },
925 {
926 protocol: dtls,
927 name: "SendWarningAlerts-DTLS",
928 config: Config{
929 Bugs: ProtocolBugs{
930 SendWarningAlerts: alertAccessDenied,
931 },
932 },
933 },
David Benjamin513f0ea2015-04-02 19:33:31 -0400934 {
935 name: "BadFinished",
936 config: Config{
937 Bugs: ProtocolBugs{
938 BadFinished: true,
939 },
940 },
941 shouldFail: true,
942 expectedError: ":DIGEST_CHECK_FAILED:",
943 },
944 {
945 name: "FalseStart-BadFinished",
946 config: Config{
947 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
948 NextProtos: []string{"foo"},
949 Bugs: ProtocolBugs{
950 BadFinished: true,
951 ExpectFalseStart: true,
952 },
953 },
954 flags: []string{
955 "-false-start",
David Benjamin87e4acd2015-04-02 19:57:35 -0400956 "-handshake-never-done",
David Benjamin513f0ea2015-04-02 19:33:31 -0400957 "-advertise-alpn", "\x03foo",
958 },
959 shimWritesFirst: true,
960 shouldFail: true,
961 expectedError: ":DIGEST_CHECK_FAILED:",
962 },
David Benjamin1c633152015-04-02 20:19:11 -0400963 {
964 name: "NoFalseStart-NoALPN",
965 config: Config{
966 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
967 Bugs: ProtocolBugs{
968 ExpectFalseStart: true,
969 AlertBeforeFalseStartTest: alertAccessDenied,
970 },
971 },
972 flags: []string{
973 "-false-start",
974 },
975 shimWritesFirst: true,
976 shouldFail: true,
977 expectedError: ":TLSV1_ALERT_ACCESS_DENIED:",
978 expectedLocalError: "tls: peer did not false start: EOF",
979 },
980 {
981 name: "NoFalseStart-NoAEAD",
982 config: Config{
983 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
984 NextProtos: []string{"foo"},
985 Bugs: ProtocolBugs{
986 ExpectFalseStart: true,
987 AlertBeforeFalseStartTest: alertAccessDenied,
988 },
989 },
990 flags: []string{
991 "-false-start",
992 "-advertise-alpn", "\x03foo",
993 },
994 shimWritesFirst: true,
995 shouldFail: true,
996 expectedError: ":TLSV1_ALERT_ACCESS_DENIED:",
997 expectedLocalError: "tls: peer did not false start: EOF",
998 },
999 {
1000 name: "NoFalseStart-RSA",
1001 config: Config{
1002 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
1003 NextProtos: []string{"foo"},
1004 Bugs: ProtocolBugs{
1005 ExpectFalseStart: true,
1006 AlertBeforeFalseStartTest: alertAccessDenied,
1007 },
1008 },
1009 flags: []string{
1010 "-false-start",
1011 "-advertise-alpn", "\x03foo",
1012 },
1013 shimWritesFirst: true,
1014 shouldFail: true,
1015 expectedError: ":TLSV1_ALERT_ACCESS_DENIED:",
1016 expectedLocalError: "tls: peer did not false start: EOF",
1017 },
1018 {
1019 name: "NoFalseStart-DHE_RSA",
1020 config: Config{
1021 CipherSuites: []uint16{TLS_DHE_RSA_WITH_AES_128_GCM_SHA256},
1022 NextProtos: []string{"foo"},
1023 Bugs: ProtocolBugs{
1024 ExpectFalseStart: true,
1025 AlertBeforeFalseStartTest: alertAccessDenied,
1026 },
1027 },
1028 flags: []string{
1029 "-false-start",
1030 "-advertise-alpn", "\x03foo",
1031 },
1032 shimWritesFirst: true,
1033 shouldFail: true,
1034 expectedError: ":TLSV1_ALERT_ACCESS_DENIED:",
1035 expectedLocalError: "tls: peer did not false start: EOF",
1036 },
Adam Langley95c29f32014-06-20 12:00:00 -07001037}
1038
David Benjamin01fe8202014-09-24 15:21:44 -04001039func doExchange(test *testCase, config *Config, conn net.Conn, messageLen int, isResume bool) error {
David Benjamin65ea8ff2014-11-23 03:01:00 -05001040 var connDebug *recordingConn
David Benjamin5fa3eba2015-01-22 16:35:40 -05001041 var connDamage *damageAdaptor
David Benjamin65ea8ff2014-11-23 03:01:00 -05001042 if *flagDebug {
1043 connDebug = &recordingConn{Conn: conn}
1044 conn = connDebug
1045 defer func() {
1046 connDebug.WriteTo(os.Stdout)
1047 }()
1048 }
1049
David Benjamin6fd297b2014-08-11 18:43:38 -04001050 if test.protocol == dtls {
David Benjamin83f90402015-01-27 01:09:43 -05001051 config.Bugs.PacketAdaptor = newPacketAdaptor(conn)
1052 conn = config.Bugs.PacketAdaptor
David Benjamin5e961c12014-11-07 01:48:35 -05001053 if test.replayWrites {
1054 conn = newReplayAdaptor(conn)
1055 }
David Benjamin6fd297b2014-08-11 18:43:38 -04001056 }
1057
David Benjamin5fa3eba2015-01-22 16:35:40 -05001058 if test.damageFirstWrite {
1059 connDamage = newDamageAdaptor(conn)
1060 conn = connDamage
1061 }
1062
David Benjamin6fd297b2014-08-11 18:43:38 -04001063 if test.sendPrefix != "" {
1064 if _, err := conn.Write([]byte(test.sendPrefix)); err != nil {
1065 return err
1066 }
David Benjamin98e882e2014-08-08 13:24:34 -04001067 }
1068
David Benjamin1d5c83e2014-07-22 19:20:02 -04001069 var tlsConn *Conn
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001070 if test.testType == clientTest {
David Benjamin6fd297b2014-08-11 18:43:38 -04001071 if test.protocol == dtls {
1072 tlsConn = DTLSServer(conn, config)
1073 } else {
1074 tlsConn = Server(conn, config)
1075 }
David Benjamin1d5c83e2014-07-22 19:20:02 -04001076 } else {
1077 config.InsecureSkipVerify = true
David Benjamin6fd297b2014-08-11 18:43:38 -04001078 if test.protocol == dtls {
1079 tlsConn = DTLSClient(conn, config)
1080 } else {
1081 tlsConn = Client(conn, config)
1082 }
David Benjamin1d5c83e2014-07-22 19:20:02 -04001083 }
1084
Adam Langley95c29f32014-06-20 12:00:00 -07001085 if err := tlsConn.Handshake(); err != nil {
1086 return err
1087 }
Kenny Root7fdeaf12014-08-05 15:23:37 -07001088
David Benjamin01fe8202014-09-24 15:21:44 -04001089 // TODO(davidben): move all per-connection expectations into a dedicated
1090 // expectations struct that can be specified separately for the two
1091 // legs.
1092 expectedVersion := test.expectedVersion
1093 if isResume && test.expectedResumeVersion != 0 {
1094 expectedVersion = test.expectedResumeVersion
1095 }
1096 if vers := tlsConn.ConnectionState().Version; expectedVersion != 0 && vers != expectedVersion {
1097 return fmt.Errorf("got version %x, expected %x", vers, expectedVersion)
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001098 }
1099
David Benjamina08e49d2014-08-24 01:46:07 -04001100 if test.expectChannelID {
1101 channelID := tlsConn.ConnectionState().ChannelID
1102 if channelID == nil {
1103 return fmt.Errorf("no channel ID negotiated")
1104 }
1105 if channelID.Curve != channelIDKey.Curve ||
1106 channelIDKey.X.Cmp(channelIDKey.X) != 0 ||
1107 channelIDKey.Y.Cmp(channelIDKey.Y) != 0 {
1108 return fmt.Errorf("incorrect channel ID")
1109 }
1110 }
1111
David Benjaminae2888f2014-09-06 12:58:58 -04001112 if expected := test.expectedNextProto; expected != "" {
1113 if actual := tlsConn.ConnectionState().NegotiatedProtocol; actual != expected {
1114 return fmt.Errorf("next proto mismatch: got %s, wanted %s", actual, expected)
1115 }
1116 }
1117
David Benjaminfc7b0862014-09-06 13:21:53 -04001118 if test.expectedNextProtoType != 0 {
1119 if (test.expectedNextProtoType == alpn) != tlsConn.ConnectionState().NegotiatedProtocolFromALPN {
1120 return fmt.Errorf("next proto type mismatch")
1121 }
1122 }
1123
David Benjaminca6c8262014-11-15 19:06:08 -05001124 if p := tlsConn.ConnectionState().SRTPProtectionProfile; p != test.expectedSRTPProtectionProfile {
1125 return fmt.Errorf("SRTP profile mismatch: got %d, wanted %d", p, test.expectedSRTPProtectionProfile)
1126 }
1127
David Benjaminc565ebb2015-04-03 04:06:36 -04001128 if test.exportKeyingMaterial > 0 {
1129 actual := make([]byte, test.exportKeyingMaterial)
1130 if _, err := io.ReadFull(tlsConn, actual); err != nil {
1131 return err
1132 }
1133 expected, err := tlsConn.ExportKeyingMaterial(test.exportKeyingMaterial, []byte(test.exportLabel), []byte(test.exportContext), test.useExportContext)
1134 if err != nil {
1135 return err
1136 }
1137 if !bytes.Equal(actual, expected) {
1138 return fmt.Errorf("keying material mismatch")
1139 }
1140 }
1141
David Benjamine58c4f52014-08-24 03:47:07 -04001142 if test.shimWritesFirst {
1143 var buf [5]byte
1144 _, err := io.ReadFull(tlsConn, buf[:])
1145 if err != nil {
1146 return err
1147 }
1148 if string(buf[:]) != "hello" {
1149 return fmt.Errorf("bad initial message")
1150 }
1151 }
1152
Adam Langleycf2d4f42014-10-28 19:06:14 -07001153 if test.renegotiate {
1154 if test.renegotiateCiphers != nil {
1155 config.CipherSuites = test.renegotiateCiphers
1156 }
1157 if err := tlsConn.Renegotiate(); err != nil {
1158 return err
1159 }
1160 } else if test.renegotiateCiphers != nil {
1161 panic("renegotiateCiphers without renegotiate")
1162 }
1163
David Benjamin5fa3eba2015-01-22 16:35:40 -05001164 if test.damageFirstWrite {
1165 connDamage.setDamage(true)
1166 tlsConn.Write([]byte("DAMAGED WRITE"))
1167 connDamage.setDamage(false)
1168 }
1169
Kenny Root7fdeaf12014-08-05 15:23:37 -07001170 if messageLen < 0 {
David Benjamin6fd297b2014-08-11 18:43:38 -04001171 if test.protocol == dtls {
1172 return fmt.Errorf("messageLen < 0 not supported for DTLS tests")
1173 }
Kenny Root7fdeaf12014-08-05 15:23:37 -07001174 // Read until EOF.
1175 _, err := io.Copy(ioutil.Discard, tlsConn)
1176 return err
1177 }
1178
David Benjamin4417d052015-04-05 04:17:25 -04001179 if messageLen == 0 {
1180 messageLen = 32
Adam Langley80842bd2014-06-20 12:00:00 -07001181 }
David Benjamin4417d052015-04-05 04:17:25 -04001182 testMessage := make([]byte, messageLen)
1183 for i := range testMessage {
1184 testMessage[i] = 0x42
1185 }
1186 tlsConn.Write(testMessage)
Adam Langley95c29f32014-06-20 12:00:00 -07001187
1188 buf := make([]byte, len(testMessage))
David Benjamin6fd297b2014-08-11 18:43:38 -04001189 if test.protocol == dtls {
1190 bufTmp := make([]byte, len(buf)+1)
1191 n, err := tlsConn.Read(bufTmp)
1192 if err != nil {
1193 return err
1194 }
1195 if n != len(buf) {
1196 return fmt.Errorf("bad reply; length mismatch (%d vs %d)", n, len(buf))
1197 }
1198 copy(buf, bufTmp)
1199 } else {
1200 _, err := io.ReadFull(tlsConn, buf)
1201 if err != nil {
1202 return err
1203 }
Adam Langley95c29f32014-06-20 12:00:00 -07001204 }
1205
1206 for i, v := range buf {
1207 if v != testMessage[i]^0xff {
1208 return fmt.Errorf("bad reply contents at byte %d", i)
1209 }
1210 }
1211
1212 return nil
1213}
1214
David Benjamin325b5c32014-07-01 19:40:31 -04001215func valgrindOf(dbAttach bool, path string, args ...string) *exec.Cmd {
1216 valgrindArgs := []string{"--error-exitcode=99", "--track-origins=yes", "--leak-check=full"}
Adam Langley95c29f32014-06-20 12:00:00 -07001217 if dbAttach {
David Benjamin325b5c32014-07-01 19:40:31 -04001218 valgrindArgs = append(valgrindArgs, "--db-attach=yes", "--db-command=xterm -e gdb -nw %f %p")
Adam Langley95c29f32014-06-20 12:00:00 -07001219 }
David Benjamin325b5c32014-07-01 19:40:31 -04001220 valgrindArgs = append(valgrindArgs, path)
1221 valgrindArgs = append(valgrindArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -07001222
David Benjamin325b5c32014-07-01 19:40:31 -04001223 return exec.Command("valgrind", valgrindArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -07001224}
1225
David Benjamin325b5c32014-07-01 19:40:31 -04001226func gdbOf(path string, args ...string) *exec.Cmd {
1227 xtermArgs := []string{"-e", "gdb", "--args"}
1228 xtermArgs = append(xtermArgs, path)
1229 xtermArgs = append(xtermArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -07001230
David Benjamin325b5c32014-07-01 19:40:31 -04001231 return exec.Command("xterm", xtermArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -07001232}
1233
Adam Langley69a01602014-11-17 17:26:55 -08001234type moreMallocsError struct{}
1235
1236func (moreMallocsError) Error() string {
1237 return "child process did not exhaust all allocation calls"
1238}
1239
1240var errMoreMallocs = moreMallocsError{}
1241
David Benjamin87c8a642015-02-21 01:54:29 -05001242// accept accepts a connection from listener, unless waitChan signals a process
1243// exit first.
1244func acceptOrWait(listener net.Listener, waitChan chan error) (net.Conn, error) {
1245 type connOrError struct {
1246 conn net.Conn
1247 err error
1248 }
1249 connChan := make(chan connOrError, 1)
1250 go func() {
1251 conn, err := listener.Accept()
1252 connChan <- connOrError{conn, err}
1253 close(connChan)
1254 }()
1255 select {
1256 case result := <-connChan:
1257 return result.conn, result.err
1258 case childErr := <-waitChan:
1259 waitChan <- childErr
1260 return nil, fmt.Errorf("child exited early: %s", childErr)
1261 }
1262}
1263
Adam Langley69a01602014-11-17 17:26:55 -08001264func runTest(test *testCase, buildDir string, mallocNumToFail int64) error {
Adam Langley38311732014-10-16 19:04:35 -07001265 if !test.shouldFail && (len(test.expectedError) > 0 || len(test.expectedLocalError) > 0) {
1266 panic("Error expected without shouldFail in " + test.name)
1267 }
1268
David Benjamin87c8a642015-02-21 01:54:29 -05001269 listener, err := net.ListenTCP("tcp4", &net.TCPAddr{IP: net.IP{127, 0, 0, 1}})
1270 if err != nil {
1271 panic(err)
1272 }
1273 defer func() {
1274 if listener != nil {
1275 listener.Close()
1276 }
1277 }()
Adam Langley95c29f32014-06-20 12:00:00 -07001278
David Benjamin884fdf12014-08-02 15:28:23 -04001279 shim_path := path.Join(buildDir, "ssl/test/bssl_shim")
David Benjamin87c8a642015-02-21 01:54:29 -05001280 flags := []string{"-port", strconv.Itoa(listener.Addr().(*net.TCPAddr).Port)}
David Benjamin1d5c83e2014-07-22 19:20:02 -04001281 if test.testType == serverTest {
David Benjamin5a593af2014-08-11 19:51:50 -04001282 flags = append(flags, "-server")
1283
David Benjamin025b3d32014-07-01 19:53:04 -04001284 flags = append(flags, "-key-file")
1285 if test.keyFile == "" {
1286 flags = append(flags, rsaKeyFile)
1287 } else {
1288 flags = append(flags, test.keyFile)
1289 }
1290
1291 flags = append(flags, "-cert-file")
1292 if test.certFile == "" {
1293 flags = append(flags, rsaCertificateFile)
1294 } else {
1295 flags = append(flags, test.certFile)
1296 }
1297 }
David Benjamin5a593af2014-08-11 19:51:50 -04001298
David Benjamin6fd297b2014-08-11 18:43:38 -04001299 if test.protocol == dtls {
1300 flags = append(flags, "-dtls")
1301 }
1302
David Benjamin5a593af2014-08-11 19:51:50 -04001303 if test.resumeSession {
1304 flags = append(flags, "-resume")
1305 }
1306
David Benjamine58c4f52014-08-24 03:47:07 -04001307 if test.shimWritesFirst {
1308 flags = append(flags, "-shim-writes-first")
1309 }
1310
David Benjaminc565ebb2015-04-03 04:06:36 -04001311 if test.exportKeyingMaterial > 0 {
1312 flags = append(flags, "-export-keying-material", strconv.Itoa(test.exportKeyingMaterial))
1313 flags = append(flags, "-export-label", test.exportLabel)
1314 flags = append(flags, "-export-context", test.exportContext)
1315 if test.useExportContext {
1316 flags = append(flags, "-use-export-context")
1317 }
1318 }
1319
David Benjamin025b3d32014-07-01 19:53:04 -04001320 flags = append(flags, test.flags...)
1321
1322 var shim *exec.Cmd
1323 if *useValgrind {
1324 shim = valgrindOf(false, shim_path, flags...)
Adam Langley75712922014-10-10 16:23:43 -07001325 } else if *useGDB {
1326 shim = gdbOf(shim_path, flags...)
David Benjamin025b3d32014-07-01 19:53:04 -04001327 } else {
1328 shim = exec.Command(shim_path, flags...)
1329 }
David Benjamin025b3d32014-07-01 19:53:04 -04001330 shim.Stdin = os.Stdin
1331 var stdoutBuf, stderrBuf bytes.Buffer
1332 shim.Stdout = &stdoutBuf
1333 shim.Stderr = &stderrBuf
Adam Langley69a01602014-11-17 17:26:55 -08001334 if mallocNumToFail >= 0 {
David Benjamin9e128b02015-02-09 13:13:09 -05001335 shim.Env = os.Environ()
1336 shim.Env = append(shim.Env, "MALLOC_NUMBER_TO_FAIL="+strconv.FormatInt(mallocNumToFail, 10))
Adam Langley69a01602014-11-17 17:26:55 -08001337 if *mallocTestDebug {
1338 shim.Env = append(shim.Env, "MALLOC_ABORT_ON_FAIL=1")
1339 }
1340 shim.Env = append(shim.Env, "_MALLOC_CHECK=1")
1341 }
David Benjamin025b3d32014-07-01 19:53:04 -04001342
1343 if err := shim.Start(); err != nil {
Adam Langley95c29f32014-06-20 12:00:00 -07001344 panic(err)
1345 }
David Benjamin87c8a642015-02-21 01:54:29 -05001346 waitChan := make(chan error, 1)
1347 go func() { waitChan <- shim.Wait() }()
Adam Langley95c29f32014-06-20 12:00:00 -07001348
1349 config := test.config
David Benjamin1d5c83e2014-07-22 19:20:02 -04001350 config.ClientSessionCache = NewLRUClientSessionCache(1)
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001351 config.ServerSessionCache = NewLRUServerSessionCache(1)
David Benjamin025b3d32014-07-01 19:53:04 -04001352 if test.testType == clientTest {
1353 if len(config.Certificates) == 0 {
1354 config.Certificates = []Certificate{getRSACertificate()}
1355 }
David Benjamin87c8a642015-02-21 01:54:29 -05001356 } else {
1357 // Supply a ServerName to ensure a constant session cache key,
1358 // rather than falling back to net.Conn.RemoteAddr.
1359 if len(config.ServerName) == 0 {
1360 config.ServerName = "test"
1361 }
David Benjamin025b3d32014-07-01 19:53:04 -04001362 }
Adam Langley95c29f32014-06-20 12:00:00 -07001363
David Benjamin87c8a642015-02-21 01:54:29 -05001364 conn, err := acceptOrWait(listener, waitChan)
1365 if err == nil {
1366 err = doExchange(test, &config, conn, test.messageLen, false /* not a resumption */)
1367 conn.Close()
1368 }
David Benjamin65ea8ff2014-11-23 03:01:00 -05001369
David Benjamin1d5c83e2014-07-22 19:20:02 -04001370 if err == nil && test.resumeSession {
David Benjamin01fe8202014-09-24 15:21:44 -04001371 var resumeConfig Config
1372 if test.resumeConfig != nil {
1373 resumeConfig = *test.resumeConfig
David Benjamin87c8a642015-02-21 01:54:29 -05001374 if len(resumeConfig.ServerName) == 0 {
1375 resumeConfig.ServerName = config.ServerName
1376 }
David Benjamin01fe8202014-09-24 15:21:44 -04001377 if len(resumeConfig.Certificates) == 0 {
1378 resumeConfig.Certificates = []Certificate{getRSACertificate()}
1379 }
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001380 if !test.newSessionsOnResume {
1381 resumeConfig.SessionTicketKey = config.SessionTicketKey
1382 resumeConfig.ClientSessionCache = config.ClientSessionCache
1383 resumeConfig.ServerSessionCache = config.ServerSessionCache
1384 }
David Benjamin01fe8202014-09-24 15:21:44 -04001385 } else {
1386 resumeConfig = config
1387 }
David Benjamin87c8a642015-02-21 01:54:29 -05001388 var connResume net.Conn
1389 connResume, err = acceptOrWait(listener, waitChan)
1390 if err == nil {
1391 err = doExchange(test, &resumeConfig, connResume, test.messageLen, true /* resumption */)
1392 connResume.Close()
1393 }
David Benjamin1d5c83e2014-07-22 19:20:02 -04001394 }
1395
David Benjamin87c8a642015-02-21 01:54:29 -05001396 // Close the listener now. This is to avoid hangs should the shim try to
1397 // open more connections than expected.
1398 listener.Close()
1399 listener = nil
1400
1401 childErr := <-waitChan
Adam Langley69a01602014-11-17 17:26:55 -08001402 if exitError, ok := childErr.(*exec.ExitError); ok {
1403 if exitError.Sys().(syscall.WaitStatus).ExitStatus() == 88 {
1404 return errMoreMallocs
1405 }
1406 }
Adam Langley95c29f32014-06-20 12:00:00 -07001407
1408 stdout := string(stdoutBuf.Bytes())
1409 stderr := string(stderrBuf.Bytes())
1410 failed := err != nil || childErr != nil
David Benjaminc565ebb2015-04-03 04:06:36 -04001411 correctFailure := len(test.expectedError) == 0 || strings.Contains(stderr, test.expectedError)
Adam Langleyac61fa32014-06-23 12:03:11 -07001412 localError := "none"
1413 if err != nil {
1414 localError = err.Error()
1415 }
1416 if len(test.expectedLocalError) != 0 {
1417 correctFailure = correctFailure && strings.Contains(localError, test.expectedLocalError)
1418 }
Adam Langley95c29f32014-06-20 12:00:00 -07001419
1420 if failed != test.shouldFail || failed && !correctFailure {
Adam Langley95c29f32014-06-20 12:00:00 -07001421 childError := "none"
Adam Langley95c29f32014-06-20 12:00:00 -07001422 if childErr != nil {
1423 childError = childErr.Error()
1424 }
1425
1426 var msg string
1427 switch {
1428 case failed && !test.shouldFail:
1429 msg = "unexpected failure"
1430 case !failed && test.shouldFail:
1431 msg = "unexpected success"
1432 case failed && !correctFailure:
Adam Langleyac61fa32014-06-23 12:03:11 -07001433 msg = "bad error (wanted '" + test.expectedError + "' / '" + test.expectedLocalError + "')"
Adam Langley95c29f32014-06-20 12:00:00 -07001434 default:
1435 panic("internal error")
1436 }
1437
David Benjaminc565ebb2015-04-03 04:06:36 -04001438 return fmt.Errorf("%s: local error '%s', child error '%s', stdout:\n%s\nstderr:\n%s", msg, localError, childError, stdout, stderr)
Adam Langley95c29f32014-06-20 12:00:00 -07001439 }
1440
David Benjaminc565ebb2015-04-03 04:06:36 -04001441 if !*useValgrind && !failed && len(stderr) > 0 {
Adam Langley95c29f32014-06-20 12:00:00 -07001442 println(stderr)
1443 }
1444
1445 return nil
1446}
1447
1448var tlsVersions = []struct {
1449 name string
1450 version uint16
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001451 flag string
David Benjamin8b8c0062014-11-23 02:47:52 -05001452 hasDTLS bool
Adam Langley95c29f32014-06-20 12:00:00 -07001453}{
David Benjamin8b8c0062014-11-23 02:47:52 -05001454 {"SSL3", VersionSSL30, "-no-ssl3", false},
1455 {"TLS1", VersionTLS10, "-no-tls1", true},
1456 {"TLS11", VersionTLS11, "-no-tls11", false},
1457 {"TLS12", VersionTLS12, "-no-tls12", true},
Adam Langley95c29f32014-06-20 12:00:00 -07001458}
1459
1460var testCipherSuites = []struct {
1461 name string
1462 id uint16
1463}{
1464 {"3DES-SHA", TLS_RSA_WITH_3DES_EDE_CBC_SHA},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001465 {"AES128-GCM", TLS_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001466 {"AES128-SHA", TLS_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001467 {"AES128-SHA256", TLS_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001468 {"AES256-GCM", TLS_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001469 {"AES256-SHA", TLS_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001470 {"AES256-SHA256", TLS_RSA_WITH_AES_256_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001471 {"DHE-RSA-AES128-GCM", TLS_DHE_RSA_WITH_AES_128_GCM_SHA256},
1472 {"DHE-RSA-AES128-SHA", TLS_DHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001473 {"DHE-RSA-AES128-SHA256", TLS_DHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001474 {"DHE-RSA-AES256-GCM", TLS_DHE_RSA_WITH_AES_256_GCM_SHA384},
1475 {"DHE-RSA-AES256-SHA", TLS_DHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001476 {"DHE-RSA-AES256-SHA256", TLS_DHE_RSA_WITH_AES_256_CBC_SHA256},
David Benjamine9a80ff2015-04-07 00:46:46 -04001477 {"DHE-RSA-CHACHA20-POLY1305", TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001478 {"ECDHE-ECDSA-AES128-GCM", TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
1479 {"ECDHE-ECDSA-AES128-SHA", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001480 {"ECDHE-ECDSA-AES128-SHA256", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256},
1481 {"ECDHE-ECDSA-AES256-GCM", TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001482 {"ECDHE-ECDSA-AES256-SHA", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001483 {"ECDHE-ECDSA-AES256-SHA384", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384},
David Benjamine9a80ff2015-04-07 00:46:46 -04001484 {"ECDHE-ECDSA-CHACHA20-POLY1305", TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001485 {"ECDHE-ECDSA-RC4-SHA", TLS_ECDHE_ECDSA_WITH_RC4_128_SHA},
David Benjamin2af684f2014-10-27 02:23:15 -04001486 {"ECDHE-PSK-WITH-AES-128-GCM-SHA256", TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001487 {"ECDHE-RSA-AES128-GCM", TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001488 {"ECDHE-RSA-AES128-SHA", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001489 {"ECDHE-RSA-AES128-SHA256", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001490 {"ECDHE-RSA-AES256-GCM", TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001491 {"ECDHE-RSA-AES256-SHA", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001492 {"ECDHE-RSA-AES256-SHA384", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384},
David Benjamine9a80ff2015-04-07 00:46:46 -04001493 {"ECDHE-RSA-CHACHA20-POLY1305", TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001494 {"ECDHE-RSA-RC4-SHA", TLS_ECDHE_RSA_WITH_RC4_128_SHA},
David Benjamin48cae082014-10-27 01:06:24 -04001495 {"PSK-AES128-CBC-SHA", TLS_PSK_WITH_AES_128_CBC_SHA},
1496 {"PSK-AES256-CBC-SHA", TLS_PSK_WITH_AES_256_CBC_SHA},
1497 {"PSK-RC4-SHA", TLS_PSK_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -07001498 {"RC4-MD5", TLS_RSA_WITH_RC4_128_MD5},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001499 {"RC4-SHA", TLS_RSA_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -07001500}
1501
David Benjamin8b8c0062014-11-23 02:47:52 -05001502func hasComponent(suiteName, component string) bool {
1503 return strings.Contains("-"+suiteName+"-", "-"+component+"-")
1504}
1505
David Benjaminf7768e42014-08-31 02:06:47 -04001506func isTLS12Only(suiteName string) bool {
David Benjamin8b8c0062014-11-23 02:47:52 -05001507 return hasComponent(suiteName, "GCM") ||
1508 hasComponent(suiteName, "SHA256") ||
David Benjamine9a80ff2015-04-07 00:46:46 -04001509 hasComponent(suiteName, "SHA384") ||
1510 hasComponent(suiteName, "POLY1305")
David Benjamin8b8c0062014-11-23 02:47:52 -05001511}
1512
1513func isDTLSCipher(suiteName string) bool {
David Benjamine95d20d2014-12-23 11:16:01 -05001514 return !hasComponent(suiteName, "RC4")
David Benjaminf7768e42014-08-31 02:06:47 -04001515}
1516
Adam Langley95c29f32014-06-20 12:00:00 -07001517func addCipherSuiteTests() {
1518 for _, suite := range testCipherSuites {
David Benjamin48cae082014-10-27 01:06:24 -04001519 const psk = "12345"
1520 const pskIdentity = "luggage combo"
1521
Adam Langley95c29f32014-06-20 12:00:00 -07001522 var cert Certificate
David Benjamin025b3d32014-07-01 19:53:04 -04001523 var certFile string
1524 var keyFile string
David Benjamin8b8c0062014-11-23 02:47:52 -05001525 if hasComponent(suite.name, "ECDSA") {
Adam Langley95c29f32014-06-20 12:00:00 -07001526 cert = getECDSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -04001527 certFile = ecdsaCertificateFile
1528 keyFile = ecdsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -07001529 } else {
1530 cert = getRSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -04001531 certFile = rsaCertificateFile
1532 keyFile = rsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -07001533 }
1534
David Benjamin48cae082014-10-27 01:06:24 -04001535 var flags []string
David Benjamin8b8c0062014-11-23 02:47:52 -05001536 if hasComponent(suite.name, "PSK") {
David Benjamin48cae082014-10-27 01:06:24 -04001537 flags = append(flags,
1538 "-psk", psk,
1539 "-psk-identity", pskIdentity)
1540 }
1541
Adam Langley95c29f32014-06-20 12:00:00 -07001542 for _, ver := range tlsVersions {
David Benjaminf7768e42014-08-31 02:06:47 -04001543 if ver.version < VersionTLS12 && isTLS12Only(suite.name) {
Adam Langley95c29f32014-06-20 12:00:00 -07001544 continue
1545 }
1546
David Benjamin025b3d32014-07-01 19:53:04 -04001547 testCases = append(testCases, testCase{
1548 testType: clientTest,
1549 name: ver.name + "-" + suite.name + "-client",
Adam Langley95c29f32014-06-20 12:00:00 -07001550 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001551 MinVersion: ver.version,
1552 MaxVersion: ver.version,
1553 CipherSuites: []uint16{suite.id},
1554 Certificates: []Certificate{cert},
1555 PreSharedKey: []byte(psk),
1556 PreSharedKeyIdentity: pskIdentity,
Adam Langley95c29f32014-06-20 12:00:00 -07001557 },
David Benjamin48cae082014-10-27 01:06:24 -04001558 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001559 resumeSession: true,
Adam Langley95c29f32014-06-20 12:00:00 -07001560 })
David Benjamin025b3d32014-07-01 19:53:04 -04001561
David Benjamin76d8abe2014-08-14 16:25:34 -04001562 testCases = append(testCases, testCase{
1563 testType: serverTest,
1564 name: ver.name + "-" + suite.name + "-server",
1565 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001566 MinVersion: ver.version,
1567 MaxVersion: ver.version,
1568 CipherSuites: []uint16{suite.id},
1569 Certificates: []Certificate{cert},
1570 PreSharedKey: []byte(psk),
1571 PreSharedKeyIdentity: pskIdentity,
David Benjamin76d8abe2014-08-14 16:25:34 -04001572 },
1573 certFile: certFile,
1574 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001575 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001576 resumeSession: true,
David Benjamin76d8abe2014-08-14 16:25:34 -04001577 })
David Benjamin6fd297b2014-08-11 18:43:38 -04001578
David Benjamin8b8c0062014-11-23 02:47:52 -05001579 if ver.hasDTLS && isDTLSCipher(suite.name) {
David Benjamin6fd297b2014-08-11 18:43:38 -04001580 testCases = append(testCases, testCase{
1581 testType: clientTest,
1582 protocol: dtls,
1583 name: "D" + ver.name + "-" + suite.name + "-client",
1584 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001585 MinVersion: ver.version,
1586 MaxVersion: ver.version,
1587 CipherSuites: []uint16{suite.id},
1588 Certificates: []Certificate{cert},
1589 PreSharedKey: []byte(psk),
1590 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001591 },
David Benjamin48cae082014-10-27 01:06:24 -04001592 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001593 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001594 })
1595 testCases = append(testCases, testCase{
1596 testType: serverTest,
1597 protocol: dtls,
1598 name: "D" + ver.name + "-" + suite.name + "-server",
1599 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001600 MinVersion: ver.version,
1601 MaxVersion: ver.version,
1602 CipherSuites: []uint16{suite.id},
1603 Certificates: []Certificate{cert},
1604 PreSharedKey: []byte(psk),
1605 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001606 },
1607 certFile: certFile,
1608 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001609 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001610 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001611 })
1612 }
Adam Langley95c29f32014-06-20 12:00:00 -07001613 }
1614 }
1615}
1616
1617func addBadECDSASignatureTests() {
1618 for badR := BadValue(1); badR < NumBadValues; badR++ {
1619 for badS := BadValue(1); badS < NumBadValues; badS++ {
David Benjamin025b3d32014-07-01 19:53:04 -04001620 testCases = append(testCases, testCase{
Adam Langley95c29f32014-06-20 12:00:00 -07001621 name: fmt.Sprintf("BadECDSA-%d-%d", badR, badS),
1622 config: Config{
1623 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
1624 Certificates: []Certificate{getECDSACertificate()},
1625 Bugs: ProtocolBugs{
1626 BadECDSAR: badR,
1627 BadECDSAS: badS,
1628 },
1629 },
1630 shouldFail: true,
1631 expectedError: "SIGNATURE",
1632 })
1633 }
1634 }
1635}
1636
Adam Langley80842bd2014-06-20 12:00:00 -07001637func addCBCPaddingTests() {
David Benjamin025b3d32014-07-01 19:53:04 -04001638 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001639 name: "MaxCBCPadding",
1640 config: Config{
1641 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1642 Bugs: ProtocolBugs{
1643 MaxPadding: true,
1644 },
1645 },
1646 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1647 })
David Benjamin025b3d32014-07-01 19:53:04 -04001648 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001649 name: "BadCBCPadding",
1650 config: Config{
1651 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1652 Bugs: ProtocolBugs{
1653 PaddingFirstByteBad: true,
1654 },
1655 },
1656 shouldFail: true,
1657 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1658 })
1659 // OpenSSL previously had an issue where the first byte of padding in
1660 // 255 bytes of padding wasn't checked.
David Benjamin025b3d32014-07-01 19:53:04 -04001661 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001662 name: "BadCBCPadding255",
1663 config: Config{
1664 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1665 Bugs: ProtocolBugs{
1666 MaxPadding: true,
1667 PaddingFirstByteBadIf255: true,
1668 },
1669 },
1670 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1671 shouldFail: true,
1672 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1673 })
1674}
1675
Kenny Root7fdeaf12014-08-05 15:23:37 -07001676func addCBCSplittingTests() {
1677 testCases = append(testCases, testCase{
1678 name: "CBCRecordSplitting",
1679 config: Config{
1680 MaxVersion: VersionTLS10,
1681 MinVersion: VersionTLS10,
1682 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1683 },
1684 messageLen: -1, // read until EOF
1685 flags: []string{
1686 "-async",
1687 "-write-different-record-sizes",
1688 "-cbc-record-splitting",
1689 },
David Benjamina8e3e0e2014-08-06 22:11:10 -04001690 })
1691 testCases = append(testCases, testCase{
Kenny Root7fdeaf12014-08-05 15:23:37 -07001692 name: "CBCRecordSplittingPartialWrite",
1693 config: Config{
1694 MaxVersion: VersionTLS10,
1695 MinVersion: VersionTLS10,
1696 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1697 },
1698 messageLen: -1, // read until EOF
1699 flags: []string{
1700 "-async",
1701 "-write-different-record-sizes",
1702 "-cbc-record-splitting",
1703 "-partial-write",
1704 },
1705 })
1706}
1707
David Benjamin636293b2014-07-08 17:59:18 -04001708func addClientAuthTests() {
David Benjamin407a10c2014-07-16 12:58:59 -04001709 // Add a dummy cert pool to stress certificate authority parsing.
1710 // TODO(davidben): Add tests that those values parse out correctly.
1711 certPool := x509.NewCertPool()
1712 cert, err := x509.ParseCertificate(rsaCertificate.Certificate[0])
1713 if err != nil {
1714 panic(err)
1715 }
1716 certPool.AddCert(cert)
1717
David Benjamin636293b2014-07-08 17:59:18 -04001718 for _, ver := range tlsVersions {
David Benjamin636293b2014-07-08 17:59:18 -04001719 testCases = append(testCases, testCase{
1720 testType: clientTest,
David Benjamin67666e72014-07-12 15:47:52 -04001721 name: ver.name + "-Client-ClientAuth-RSA",
David Benjamin636293b2014-07-08 17:59:18 -04001722 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001723 MinVersion: ver.version,
1724 MaxVersion: ver.version,
1725 ClientAuth: RequireAnyClientCert,
1726 ClientCAs: certPool,
David Benjamin636293b2014-07-08 17:59:18 -04001727 },
1728 flags: []string{
1729 "-cert-file", rsaCertificateFile,
1730 "-key-file", rsaKeyFile,
1731 },
1732 })
1733 testCases = append(testCases, testCase{
David Benjamin67666e72014-07-12 15:47:52 -04001734 testType: serverTest,
1735 name: ver.name + "-Server-ClientAuth-RSA",
1736 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001737 MinVersion: ver.version,
1738 MaxVersion: ver.version,
David Benjamin67666e72014-07-12 15:47:52 -04001739 Certificates: []Certificate{rsaCertificate},
1740 },
1741 flags: []string{"-require-any-client-certificate"},
1742 })
David Benjamine098ec22014-08-27 23:13:20 -04001743 if ver.version != VersionSSL30 {
1744 testCases = append(testCases, testCase{
1745 testType: serverTest,
1746 name: ver.name + "-Server-ClientAuth-ECDSA",
1747 config: Config{
1748 MinVersion: ver.version,
1749 MaxVersion: ver.version,
1750 Certificates: []Certificate{ecdsaCertificate},
1751 },
1752 flags: []string{"-require-any-client-certificate"},
1753 })
1754 testCases = append(testCases, testCase{
1755 testType: clientTest,
1756 name: ver.name + "-Client-ClientAuth-ECDSA",
1757 config: Config{
1758 MinVersion: ver.version,
1759 MaxVersion: ver.version,
1760 ClientAuth: RequireAnyClientCert,
1761 ClientCAs: certPool,
1762 },
1763 flags: []string{
1764 "-cert-file", ecdsaCertificateFile,
1765 "-key-file", ecdsaKeyFile,
1766 },
1767 })
1768 }
David Benjamin636293b2014-07-08 17:59:18 -04001769 }
1770}
1771
Adam Langley75712922014-10-10 16:23:43 -07001772func addExtendedMasterSecretTests() {
1773 const expectEMSFlag = "-expect-extended-master-secret"
1774
1775 for _, with := range []bool{false, true} {
1776 prefix := "No"
1777 var flags []string
1778 if with {
1779 prefix = ""
1780 flags = []string{expectEMSFlag}
1781 }
1782
1783 for _, isClient := range []bool{false, true} {
1784 suffix := "-Server"
1785 testType := serverTest
1786 if isClient {
1787 suffix = "-Client"
1788 testType = clientTest
1789 }
1790
1791 for _, ver := range tlsVersions {
1792 test := testCase{
1793 testType: testType,
1794 name: prefix + "ExtendedMasterSecret-" + ver.name + suffix,
1795 config: Config{
1796 MinVersion: ver.version,
1797 MaxVersion: ver.version,
1798 Bugs: ProtocolBugs{
1799 NoExtendedMasterSecret: !with,
1800 RequireExtendedMasterSecret: with,
1801 },
1802 },
David Benjamin48cae082014-10-27 01:06:24 -04001803 flags: flags,
1804 shouldFail: ver.version == VersionSSL30 && with,
Adam Langley75712922014-10-10 16:23:43 -07001805 }
1806 if test.shouldFail {
1807 test.expectedLocalError = "extended master secret required but not supported by peer"
1808 }
1809 testCases = append(testCases, test)
1810 }
1811 }
1812 }
1813
1814 // When a session is resumed, it should still be aware that its master
1815 // secret was generated via EMS and thus it's safe to use tls-unique.
1816 testCases = append(testCases, testCase{
1817 name: "ExtendedMasterSecret-Resume",
1818 config: Config{
1819 Bugs: ProtocolBugs{
1820 RequireExtendedMasterSecret: true,
1821 },
1822 },
1823 flags: []string{expectEMSFlag},
1824 resumeSession: true,
1825 })
1826}
1827
David Benjamin43ec06f2014-08-05 02:28:57 -04001828// Adds tests that try to cover the range of the handshake state machine, under
1829// various conditions. Some of these are redundant with other tests, but they
1830// only cover the synchronous case.
David Benjamin6fd297b2014-08-11 18:43:38 -04001831func addStateMachineCoverageTests(async, splitHandshake bool, protocol protocol) {
David Benjamin43ec06f2014-08-05 02:28:57 -04001832 var suffix string
1833 var flags []string
1834 var maxHandshakeRecordLength int
David Benjamin6fd297b2014-08-11 18:43:38 -04001835 if protocol == dtls {
1836 suffix = "-DTLS"
1837 }
David Benjamin43ec06f2014-08-05 02:28:57 -04001838 if async {
David Benjamin6fd297b2014-08-11 18:43:38 -04001839 suffix += "-Async"
David Benjamin43ec06f2014-08-05 02:28:57 -04001840 flags = append(flags, "-async")
1841 } else {
David Benjamin6fd297b2014-08-11 18:43:38 -04001842 suffix += "-Sync"
David Benjamin43ec06f2014-08-05 02:28:57 -04001843 }
1844 if splitHandshake {
1845 suffix += "-SplitHandshakeRecords"
David Benjamin98214542014-08-07 18:02:39 -04001846 maxHandshakeRecordLength = 1
David Benjamin43ec06f2014-08-05 02:28:57 -04001847 }
1848
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001849 // Basic handshake, with resumption. Client and server,
1850 // session ID and session ticket.
David Benjamin43ec06f2014-08-05 02:28:57 -04001851 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001852 protocol: protocol,
1853 name: "Basic-Client" + suffix,
David Benjamin43ec06f2014-08-05 02:28:57 -04001854 config: Config{
1855 Bugs: ProtocolBugs{
1856 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1857 },
1858 },
David Benjaminbed9aae2014-08-07 19:13:38 -04001859 flags: flags,
1860 resumeSession: true,
1861 })
1862 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001863 protocol: protocol,
1864 name: "Basic-Client-RenewTicket" + suffix,
David Benjaminbed9aae2014-08-07 19:13:38 -04001865 config: Config{
1866 Bugs: ProtocolBugs{
1867 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1868 RenewTicketOnResume: true,
1869 },
1870 },
1871 flags: flags,
1872 resumeSession: true,
David Benjamin43ec06f2014-08-05 02:28:57 -04001873 })
1874 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001875 protocol: protocol,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001876 name: "Basic-Client-NoTicket" + suffix,
1877 config: Config{
1878 SessionTicketsDisabled: true,
1879 Bugs: ProtocolBugs{
1880 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1881 },
1882 },
1883 flags: flags,
1884 resumeSession: true,
1885 })
1886 testCases = append(testCases, testCase{
1887 protocol: protocol,
David Benjamine0e7d0d2015-02-08 19:33:25 -05001888 name: "Basic-Client-Implicit" + suffix,
1889 config: Config{
1890 Bugs: ProtocolBugs{
1891 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1892 },
1893 },
1894 flags: append(flags, "-implicit-handshake"),
1895 resumeSession: true,
1896 })
1897 testCases = append(testCases, testCase{
1898 protocol: protocol,
David Benjamin43ec06f2014-08-05 02:28:57 -04001899 testType: serverTest,
1900 name: "Basic-Server" + suffix,
1901 config: Config{
1902 Bugs: ProtocolBugs{
1903 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1904 },
1905 },
David Benjaminbed9aae2014-08-07 19:13:38 -04001906 flags: flags,
1907 resumeSession: true,
David Benjamin43ec06f2014-08-05 02:28:57 -04001908 })
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001909 testCases = append(testCases, testCase{
1910 protocol: protocol,
1911 testType: serverTest,
1912 name: "Basic-Server-NoTickets" + suffix,
1913 config: Config{
1914 SessionTicketsDisabled: true,
1915 Bugs: ProtocolBugs{
1916 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1917 },
1918 },
1919 flags: flags,
1920 resumeSession: true,
1921 })
David Benjamine0e7d0d2015-02-08 19:33:25 -05001922 testCases = append(testCases, testCase{
1923 protocol: protocol,
1924 testType: serverTest,
1925 name: "Basic-Server-Implicit" + suffix,
1926 config: Config{
1927 Bugs: ProtocolBugs{
1928 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1929 },
1930 },
1931 flags: append(flags, "-implicit-handshake"),
1932 resumeSession: true,
1933 })
David Benjamin6f5c0f42015-02-24 01:23:21 -05001934 testCases = append(testCases, testCase{
1935 protocol: protocol,
1936 testType: serverTest,
1937 name: "Basic-Server-EarlyCallback" + suffix,
1938 config: Config{
1939 Bugs: ProtocolBugs{
1940 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1941 },
1942 },
1943 flags: append(flags, "-use-early-callback"),
1944 resumeSession: true,
1945 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001946
David Benjamin6fd297b2014-08-11 18:43:38 -04001947 // TLS client auth.
1948 testCases = append(testCases, testCase{
1949 protocol: protocol,
1950 testType: clientTest,
1951 name: "ClientAuth-Client" + suffix,
1952 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001953 ClientAuth: RequireAnyClientCert,
David Benjamin6fd297b2014-08-11 18:43:38 -04001954 Bugs: ProtocolBugs{
1955 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1956 },
1957 },
1958 flags: append(flags,
1959 "-cert-file", rsaCertificateFile,
1960 "-key-file", rsaKeyFile),
1961 })
1962 testCases = append(testCases, testCase{
1963 protocol: protocol,
1964 testType: serverTest,
1965 name: "ClientAuth-Server" + suffix,
1966 config: Config{
1967 Certificates: []Certificate{rsaCertificate},
1968 },
1969 flags: append(flags, "-require-any-client-certificate"),
1970 })
1971
David Benjamin43ec06f2014-08-05 02:28:57 -04001972 // No session ticket support; server doesn't send NewSessionTicket.
1973 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001974 protocol: protocol,
1975 name: "SessionTicketsDisabled-Client" + suffix,
David Benjamin43ec06f2014-08-05 02:28:57 -04001976 config: Config{
1977 SessionTicketsDisabled: true,
1978 Bugs: ProtocolBugs{
1979 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1980 },
1981 },
1982 flags: flags,
1983 })
1984 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001985 protocol: protocol,
David Benjamin43ec06f2014-08-05 02:28:57 -04001986 testType: serverTest,
1987 name: "SessionTicketsDisabled-Server" + suffix,
1988 config: Config{
1989 SessionTicketsDisabled: true,
1990 Bugs: ProtocolBugs{
1991 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1992 },
1993 },
1994 flags: flags,
1995 })
1996
David Benjamin48cae082014-10-27 01:06:24 -04001997 // Skip ServerKeyExchange in PSK key exchange if there's no
1998 // identity hint.
1999 testCases = append(testCases, testCase{
2000 protocol: protocol,
2001 name: "EmptyPSKHint-Client" + suffix,
2002 config: Config{
2003 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
2004 PreSharedKey: []byte("secret"),
2005 Bugs: ProtocolBugs{
2006 MaxHandshakeRecordLength: maxHandshakeRecordLength,
2007 },
2008 },
2009 flags: append(flags, "-psk", "secret"),
2010 })
2011 testCases = append(testCases, testCase{
2012 protocol: protocol,
2013 testType: serverTest,
2014 name: "EmptyPSKHint-Server" + suffix,
2015 config: Config{
2016 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
2017 PreSharedKey: []byte("secret"),
2018 Bugs: ProtocolBugs{
2019 MaxHandshakeRecordLength: maxHandshakeRecordLength,
2020 },
2021 },
2022 flags: append(flags, "-psk", "secret"),
2023 })
2024
David Benjamin6fd297b2014-08-11 18:43:38 -04002025 if protocol == tls {
2026 // NPN on client and server; results in post-handshake message.
2027 testCases = append(testCases, testCase{
2028 protocol: protocol,
2029 name: "NPN-Client" + suffix,
2030 config: Config{
David Benjaminae2888f2014-09-06 12:58:58 -04002031 NextProtos: []string{"foo"},
David Benjamin6fd297b2014-08-11 18:43:38 -04002032 Bugs: ProtocolBugs{
2033 MaxHandshakeRecordLength: maxHandshakeRecordLength,
2034 },
David Benjamin43ec06f2014-08-05 02:28:57 -04002035 },
David Benjaminfc7b0862014-09-06 13:21:53 -04002036 flags: append(flags, "-select-next-proto", "foo"),
2037 expectedNextProto: "foo",
2038 expectedNextProtoType: npn,
David Benjamin6fd297b2014-08-11 18:43:38 -04002039 })
2040 testCases = append(testCases, testCase{
2041 protocol: protocol,
2042 testType: serverTest,
2043 name: "NPN-Server" + suffix,
2044 config: Config{
2045 NextProtos: []string{"bar"},
2046 Bugs: ProtocolBugs{
2047 MaxHandshakeRecordLength: maxHandshakeRecordLength,
2048 },
David Benjamin43ec06f2014-08-05 02:28:57 -04002049 },
David Benjamin6fd297b2014-08-11 18:43:38 -04002050 flags: append(flags,
2051 "-advertise-npn", "\x03foo\x03bar\x03baz",
2052 "-expect-next-proto", "bar"),
David Benjaminfc7b0862014-09-06 13:21:53 -04002053 expectedNextProto: "bar",
2054 expectedNextProtoType: npn,
David Benjamin6fd297b2014-08-11 18:43:38 -04002055 })
David Benjamin43ec06f2014-08-05 02:28:57 -04002056
David Benjamin195dc782015-02-19 13:27:05 -05002057 // TODO(davidben): Add tests for when False Start doesn't trigger.
2058
David Benjamin6fd297b2014-08-11 18:43:38 -04002059 // Client does False Start and negotiates NPN.
2060 testCases = append(testCases, testCase{
2061 protocol: protocol,
2062 name: "FalseStart" + suffix,
2063 config: Config{
2064 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2065 NextProtos: []string{"foo"},
2066 Bugs: ProtocolBugs{
David Benjamine58c4f52014-08-24 03:47:07 -04002067 ExpectFalseStart: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04002068 MaxHandshakeRecordLength: maxHandshakeRecordLength,
2069 },
David Benjamin43ec06f2014-08-05 02:28:57 -04002070 },
David Benjamin6fd297b2014-08-11 18:43:38 -04002071 flags: append(flags,
2072 "-false-start",
2073 "-select-next-proto", "foo"),
David Benjamine58c4f52014-08-24 03:47:07 -04002074 shimWritesFirst: true,
2075 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04002076 })
David Benjamin43ec06f2014-08-05 02:28:57 -04002077
David Benjaminae2888f2014-09-06 12:58:58 -04002078 // Client does False Start and negotiates ALPN.
2079 testCases = append(testCases, testCase{
2080 protocol: protocol,
2081 name: "FalseStart-ALPN" + suffix,
2082 config: Config{
2083 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2084 NextProtos: []string{"foo"},
2085 Bugs: ProtocolBugs{
2086 ExpectFalseStart: true,
2087 MaxHandshakeRecordLength: maxHandshakeRecordLength,
2088 },
2089 },
2090 flags: append(flags,
2091 "-false-start",
2092 "-advertise-alpn", "\x03foo"),
2093 shimWritesFirst: true,
2094 resumeSession: true,
2095 })
2096
David Benjamin931ab342015-02-08 19:46:57 -05002097 // Client does False Start but doesn't explicitly call
2098 // SSL_connect.
2099 testCases = append(testCases, testCase{
2100 protocol: protocol,
2101 name: "FalseStart-Implicit" + suffix,
2102 config: Config{
2103 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2104 NextProtos: []string{"foo"},
2105 Bugs: ProtocolBugs{
2106 MaxHandshakeRecordLength: maxHandshakeRecordLength,
2107 },
2108 },
2109 flags: append(flags,
2110 "-implicit-handshake",
2111 "-false-start",
2112 "-advertise-alpn", "\x03foo"),
2113 })
2114
David Benjamin6fd297b2014-08-11 18:43:38 -04002115 // False Start without session tickets.
2116 testCases = append(testCases, testCase{
David Benjamin5f237bc2015-02-11 17:14:15 -05002117 name: "FalseStart-SessionTicketsDisabled" + suffix,
David Benjamin6fd297b2014-08-11 18:43:38 -04002118 config: Config{
2119 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2120 NextProtos: []string{"foo"},
2121 SessionTicketsDisabled: true,
David Benjamin4e99c522014-08-24 01:45:30 -04002122 Bugs: ProtocolBugs{
David Benjamine58c4f52014-08-24 03:47:07 -04002123 ExpectFalseStart: true,
David Benjamin4e99c522014-08-24 01:45:30 -04002124 MaxHandshakeRecordLength: maxHandshakeRecordLength,
2125 },
David Benjamin43ec06f2014-08-05 02:28:57 -04002126 },
David Benjamin4e99c522014-08-24 01:45:30 -04002127 flags: append(flags,
David Benjamin6fd297b2014-08-11 18:43:38 -04002128 "-false-start",
2129 "-select-next-proto", "foo",
David Benjamin4e99c522014-08-24 01:45:30 -04002130 ),
David Benjamine58c4f52014-08-24 03:47:07 -04002131 shimWritesFirst: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04002132 })
David Benjamin1e7f8d72014-08-08 12:27:04 -04002133
David Benjamina08e49d2014-08-24 01:46:07 -04002134 // Server parses a V2ClientHello.
David Benjamin6fd297b2014-08-11 18:43:38 -04002135 testCases = append(testCases, testCase{
2136 protocol: protocol,
2137 testType: serverTest,
2138 name: "SendV2ClientHello" + suffix,
2139 config: Config{
2140 // Choose a cipher suite that does not involve
2141 // elliptic curves, so no extensions are
2142 // involved.
2143 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2144 Bugs: ProtocolBugs{
2145 MaxHandshakeRecordLength: maxHandshakeRecordLength,
2146 SendV2ClientHello: true,
2147 },
David Benjamin1e7f8d72014-08-08 12:27:04 -04002148 },
David Benjamin6fd297b2014-08-11 18:43:38 -04002149 flags: flags,
2150 })
David Benjamina08e49d2014-08-24 01:46:07 -04002151
2152 // Client sends a Channel ID.
2153 testCases = append(testCases, testCase{
2154 protocol: protocol,
2155 name: "ChannelID-Client" + suffix,
2156 config: Config{
2157 RequestChannelID: true,
2158 Bugs: ProtocolBugs{
2159 MaxHandshakeRecordLength: maxHandshakeRecordLength,
2160 },
2161 },
2162 flags: append(flags,
2163 "-send-channel-id", channelIDKeyFile,
2164 ),
2165 resumeSession: true,
2166 expectChannelID: true,
2167 })
2168
2169 // Server accepts a Channel ID.
2170 testCases = append(testCases, testCase{
2171 protocol: protocol,
2172 testType: serverTest,
2173 name: "ChannelID-Server" + suffix,
2174 config: Config{
2175 ChannelID: channelIDKey,
2176 Bugs: ProtocolBugs{
2177 MaxHandshakeRecordLength: maxHandshakeRecordLength,
2178 },
2179 },
2180 flags: append(flags,
2181 "-expect-channel-id",
2182 base64.StdEncoding.EncodeToString(channelIDBytes),
2183 ),
2184 resumeSession: true,
2185 expectChannelID: true,
2186 })
David Benjamin6fd297b2014-08-11 18:43:38 -04002187 } else {
2188 testCases = append(testCases, testCase{
2189 protocol: protocol,
2190 name: "SkipHelloVerifyRequest" + suffix,
2191 config: Config{
2192 Bugs: ProtocolBugs{
2193 MaxHandshakeRecordLength: maxHandshakeRecordLength,
2194 SkipHelloVerifyRequest: true,
2195 },
2196 },
2197 flags: flags,
2198 })
David Benjamin6fd297b2014-08-11 18:43:38 -04002199 }
David Benjamin43ec06f2014-08-05 02:28:57 -04002200}
2201
Adam Langley524e7172015-02-20 16:04:00 -08002202func addDDoSCallbackTests() {
2203 // DDoS callback.
2204
2205 for _, resume := range []bool{false, true} {
2206 suffix := "Resume"
2207 if resume {
2208 suffix = "No" + suffix
2209 }
2210
2211 testCases = append(testCases, testCase{
2212 testType: serverTest,
2213 name: "Server-DDoS-OK-" + suffix,
2214 flags: []string{"-install-ddos-callback"},
2215 resumeSession: resume,
2216 })
2217
2218 failFlag := "-fail-ddos-callback"
2219 if resume {
2220 failFlag = "-fail-second-ddos-callback"
2221 }
2222 testCases = append(testCases, testCase{
2223 testType: serverTest,
2224 name: "Server-DDoS-Reject-" + suffix,
2225 flags: []string{"-install-ddos-callback", failFlag},
2226 resumeSession: resume,
2227 shouldFail: true,
2228 expectedError: ":CONNECTION_REJECTED:",
2229 })
2230 }
2231}
2232
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002233func addVersionNegotiationTests() {
2234 for i, shimVers := range tlsVersions {
2235 // Assemble flags to disable all newer versions on the shim.
2236 var flags []string
2237 for _, vers := range tlsVersions[i+1:] {
2238 flags = append(flags, vers.flag)
2239 }
2240
2241 for _, runnerVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05002242 protocols := []protocol{tls}
2243 if runnerVers.hasDTLS && shimVers.hasDTLS {
2244 protocols = append(protocols, dtls)
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002245 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002246 for _, protocol := range protocols {
2247 expectedVersion := shimVers.version
2248 if runnerVers.version < shimVers.version {
2249 expectedVersion = runnerVers.version
2250 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002251
David Benjamin8b8c0062014-11-23 02:47:52 -05002252 suffix := shimVers.name + "-" + runnerVers.name
2253 if protocol == dtls {
2254 suffix += "-DTLS"
2255 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002256
David Benjamin1eb367c2014-12-12 18:17:51 -05002257 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
2258
David Benjamin1e29a6b2014-12-10 02:27:24 -05002259 clientVers := shimVers.version
2260 if clientVers > VersionTLS10 {
2261 clientVers = VersionTLS10
2262 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002263 testCases = append(testCases, testCase{
2264 protocol: protocol,
2265 testType: clientTest,
2266 name: "VersionNegotiation-Client-" + suffix,
2267 config: Config{
2268 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05002269 Bugs: ProtocolBugs{
2270 ExpectInitialRecordVersion: clientVers,
2271 },
David Benjamin8b8c0062014-11-23 02:47:52 -05002272 },
2273 flags: flags,
2274 expectedVersion: expectedVersion,
2275 })
David Benjamin1eb367c2014-12-12 18:17:51 -05002276 testCases = append(testCases, testCase{
2277 protocol: protocol,
2278 testType: clientTest,
2279 name: "VersionNegotiation-Client2-" + suffix,
2280 config: Config{
2281 MaxVersion: runnerVers.version,
2282 Bugs: ProtocolBugs{
2283 ExpectInitialRecordVersion: clientVers,
2284 },
2285 },
2286 flags: []string{"-max-version", shimVersFlag},
2287 expectedVersion: expectedVersion,
2288 })
David Benjamin8b8c0062014-11-23 02:47:52 -05002289
2290 testCases = append(testCases, testCase{
2291 protocol: protocol,
2292 testType: serverTest,
2293 name: "VersionNegotiation-Server-" + suffix,
2294 config: Config{
2295 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05002296 Bugs: ProtocolBugs{
2297 ExpectInitialRecordVersion: expectedVersion,
2298 },
David Benjamin8b8c0062014-11-23 02:47:52 -05002299 },
2300 flags: flags,
2301 expectedVersion: expectedVersion,
2302 })
David Benjamin1eb367c2014-12-12 18:17:51 -05002303 testCases = append(testCases, testCase{
2304 protocol: protocol,
2305 testType: serverTest,
2306 name: "VersionNegotiation-Server2-" + suffix,
2307 config: Config{
2308 MaxVersion: runnerVers.version,
2309 Bugs: ProtocolBugs{
2310 ExpectInitialRecordVersion: expectedVersion,
2311 },
2312 },
2313 flags: []string{"-max-version", shimVersFlag},
2314 expectedVersion: expectedVersion,
2315 })
David Benjamin8b8c0062014-11-23 02:47:52 -05002316 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002317 }
2318 }
2319}
2320
David Benjaminaccb4542014-12-12 23:44:33 -05002321func addMinimumVersionTests() {
2322 for i, shimVers := range tlsVersions {
2323 // Assemble flags to disable all older versions on the shim.
2324 var flags []string
2325 for _, vers := range tlsVersions[:i] {
2326 flags = append(flags, vers.flag)
2327 }
2328
2329 for _, runnerVers := range tlsVersions {
2330 protocols := []protocol{tls}
2331 if runnerVers.hasDTLS && shimVers.hasDTLS {
2332 protocols = append(protocols, dtls)
2333 }
2334 for _, protocol := range protocols {
2335 suffix := shimVers.name + "-" + runnerVers.name
2336 if protocol == dtls {
2337 suffix += "-DTLS"
2338 }
2339 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
2340
David Benjaminaccb4542014-12-12 23:44:33 -05002341 var expectedVersion uint16
2342 var shouldFail bool
2343 var expectedError string
David Benjamin87909c02014-12-13 01:55:01 -05002344 var expectedLocalError string
David Benjaminaccb4542014-12-12 23:44:33 -05002345 if runnerVers.version >= shimVers.version {
2346 expectedVersion = runnerVers.version
2347 } else {
2348 shouldFail = true
2349 expectedError = ":UNSUPPORTED_PROTOCOL:"
David Benjamin87909c02014-12-13 01:55:01 -05002350 if runnerVers.version > VersionSSL30 {
2351 expectedLocalError = "remote error: protocol version not supported"
2352 } else {
2353 expectedLocalError = "remote error: handshake failure"
2354 }
David Benjaminaccb4542014-12-12 23:44:33 -05002355 }
2356
2357 testCases = append(testCases, testCase{
2358 protocol: protocol,
2359 testType: clientTest,
2360 name: "MinimumVersion-Client-" + suffix,
2361 config: Config{
2362 MaxVersion: runnerVers.version,
2363 },
David Benjamin87909c02014-12-13 01:55:01 -05002364 flags: flags,
2365 expectedVersion: expectedVersion,
2366 shouldFail: shouldFail,
2367 expectedError: expectedError,
2368 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002369 })
2370 testCases = append(testCases, testCase{
2371 protocol: protocol,
2372 testType: clientTest,
2373 name: "MinimumVersion-Client2-" + suffix,
2374 config: Config{
2375 MaxVersion: runnerVers.version,
2376 },
David Benjamin87909c02014-12-13 01:55:01 -05002377 flags: []string{"-min-version", shimVersFlag},
2378 expectedVersion: expectedVersion,
2379 shouldFail: shouldFail,
2380 expectedError: expectedError,
2381 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002382 })
2383
2384 testCases = append(testCases, testCase{
2385 protocol: protocol,
2386 testType: serverTest,
2387 name: "MinimumVersion-Server-" + suffix,
2388 config: Config{
2389 MaxVersion: runnerVers.version,
2390 },
David Benjamin87909c02014-12-13 01:55:01 -05002391 flags: flags,
2392 expectedVersion: expectedVersion,
2393 shouldFail: shouldFail,
2394 expectedError: expectedError,
2395 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002396 })
2397 testCases = append(testCases, testCase{
2398 protocol: protocol,
2399 testType: serverTest,
2400 name: "MinimumVersion-Server2-" + suffix,
2401 config: Config{
2402 MaxVersion: runnerVers.version,
2403 },
David Benjamin87909c02014-12-13 01:55:01 -05002404 flags: []string{"-min-version", shimVersFlag},
2405 expectedVersion: expectedVersion,
2406 shouldFail: shouldFail,
2407 expectedError: expectedError,
2408 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002409 })
2410 }
2411 }
2412 }
2413}
2414
David Benjamin5c24a1d2014-08-31 00:59:27 -04002415func addD5BugTests() {
2416 testCases = append(testCases, testCase{
2417 testType: serverTest,
2418 name: "D5Bug-NoQuirk-Reject",
2419 config: Config{
2420 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2421 Bugs: ProtocolBugs{
2422 SSL3RSAKeyExchange: true,
2423 },
2424 },
2425 shouldFail: true,
2426 expectedError: ":TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG:",
2427 })
2428 testCases = append(testCases, testCase{
2429 testType: serverTest,
2430 name: "D5Bug-Quirk-Normal",
2431 config: Config{
2432 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2433 },
2434 flags: []string{"-tls-d5-bug"},
2435 })
2436 testCases = append(testCases, testCase{
2437 testType: serverTest,
2438 name: "D5Bug-Quirk-Bug",
2439 config: Config{
2440 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2441 Bugs: ProtocolBugs{
2442 SSL3RSAKeyExchange: true,
2443 },
2444 },
2445 flags: []string{"-tls-d5-bug"},
2446 })
2447}
2448
David Benjamine78bfde2014-09-06 12:45:15 -04002449func addExtensionTests() {
2450 testCases = append(testCases, testCase{
2451 testType: clientTest,
2452 name: "DuplicateExtensionClient",
2453 config: Config{
2454 Bugs: ProtocolBugs{
2455 DuplicateExtension: true,
2456 },
2457 },
2458 shouldFail: true,
2459 expectedLocalError: "remote error: error decoding message",
2460 })
2461 testCases = append(testCases, testCase{
2462 testType: serverTest,
2463 name: "DuplicateExtensionServer",
2464 config: Config{
2465 Bugs: ProtocolBugs{
2466 DuplicateExtension: true,
2467 },
2468 },
2469 shouldFail: true,
2470 expectedLocalError: "remote error: error decoding message",
2471 })
2472 testCases = append(testCases, testCase{
2473 testType: clientTest,
2474 name: "ServerNameExtensionClient",
2475 config: Config{
2476 Bugs: ProtocolBugs{
2477 ExpectServerName: "example.com",
2478 },
2479 },
2480 flags: []string{"-host-name", "example.com"},
2481 })
2482 testCases = append(testCases, testCase{
2483 testType: clientTest,
David Benjamin5f237bc2015-02-11 17:14:15 -05002484 name: "ServerNameExtensionClientMismatch",
David Benjamine78bfde2014-09-06 12:45:15 -04002485 config: Config{
2486 Bugs: ProtocolBugs{
2487 ExpectServerName: "mismatch.com",
2488 },
2489 },
2490 flags: []string{"-host-name", "example.com"},
2491 shouldFail: true,
2492 expectedLocalError: "tls: unexpected server name",
2493 })
2494 testCases = append(testCases, testCase{
2495 testType: clientTest,
David Benjamin5f237bc2015-02-11 17:14:15 -05002496 name: "ServerNameExtensionClientMissing",
David Benjamine78bfde2014-09-06 12:45:15 -04002497 config: Config{
2498 Bugs: ProtocolBugs{
2499 ExpectServerName: "missing.com",
2500 },
2501 },
2502 shouldFail: true,
2503 expectedLocalError: "tls: unexpected server name",
2504 })
2505 testCases = append(testCases, testCase{
2506 testType: serverTest,
2507 name: "ServerNameExtensionServer",
2508 config: Config{
2509 ServerName: "example.com",
2510 },
2511 flags: []string{"-expect-server-name", "example.com"},
2512 resumeSession: true,
2513 })
David Benjaminae2888f2014-09-06 12:58:58 -04002514 testCases = append(testCases, testCase{
2515 testType: clientTest,
2516 name: "ALPNClient",
2517 config: Config{
2518 NextProtos: []string{"foo"},
2519 },
2520 flags: []string{
2521 "-advertise-alpn", "\x03foo\x03bar\x03baz",
2522 "-expect-alpn", "foo",
2523 },
David Benjaminfc7b0862014-09-06 13:21:53 -04002524 expectedNextProto: "foo",
2525 expectedNextProtoType: alpn,
2526 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04002527 })
2528 testCases = append(testCases, testCase{
2529 testType: serverTest,
2530 name: "ALPNServer",
2531 config: Config{
2532 NextProtos: []string{"foo", "bar", "baz"},
2533 },
2534 flags: []string{
2535 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2536 "-select-alpn", "foo",
2537 },
David Benjaminfc7b0862014-09-06 13:21:53 -04002538 expectedNextProto: "foo",
2539 expectedNextProtoType: alpn,
2540 resumeSession: true,
2541 })
2542 // Test that the server prefers ALPN over NPN.
2543 testCases = append(testCases, testCase{
2544 testType: serverTest,
2545 name: "ALPNServer-Preferred",
2546 config: Config{
2547 NextProtos: []string{"foo", "bar", "baz"},
2548 },
2549 flags: []string{
2550 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2551 "-select-alpn", "foo",
2552 "-advertise-npn", "\x03foo\x03bar\x03baz",
2553 },
2554 expectedNextProto: "foo",
2555 expectedNextProtoType: alpn,
2556 resumeSession: true,
2557 })
2558 testCases = append(testCases, testCase{
2559 testType: serverTest,
2560 name: "ALPNServer-Preferred-Swapped",
2561 config: Config{
2562 NextProtos: []string{"foo", "bar", "baz"},
2563 Bugs: ProtocolBugs{
2564 SwapNPNAndALPN: true,
2565 },
2566 },
2567 flags: []string{
2568 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2569 "-select-alpn", "foo",
2570 "-advertise-npn", "\x03foo\x03bar\x03baz",
2571 },
2572 expectedNextProto: "foo",
2573 expectedNextProtoType: alpn,
2574 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04002575 })
Adam Langley38311732014-10-16 19:04:35 -07002576 // Resume with a corrupt ticket.
2577 testCases = append(testCases, testCase{
2578 testType: serverTest,
2579 name: "CorruptTicket",
2580 config: Config{
2581 Bugs: ProtocolBugs{
2582 CorruptTicket: true,
2583 },
2584 },
2585 resumeSession: true,
2586 flags: []string{"-expect-session-miss"},
2587 })
2588 // Resume with an oversized session id.
2589 testCases = append(testCases, testCase{
2590 testType: serverTest,
2591 name: "OversizedSessionId",
2592 config: Config{
2593 Bugs: ProtocolBugs{
2594 OversizedSessionId: true,
2595 },
2596 },
2597 resumeSession: true,
Adam Langley75712922014-10-10 16:23:43 -07002598 shouldFail: true,
Adam Langley38311732014-10-16 19:04:35 -07002599 expectedError: ":DECODE_ERROR:",
2600 })
David Benjaminca6c8262014-11-15 19:06:08 -05002601 // Basic DTLS-SRTP tests. Include fake profiles to ensure they
2602 // are ignored.
2603 testCases = append(testCases, testCase{
2604 protocol: dtls,
2605 name: "SRTP-Client",
2606 config: Config{
2607 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
2608 },
2609 flags: []string{
2610 "-srtp-profiles",
2611 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2612 },
2613 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2614 })
2615 testCases = append(testCases, testCase{
2616 protocol: dtls,
2617 testType: serverTest,
2618 name: "SRTP-Server",
2619 config: Config{
2620 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
2621 },
2622 flags: []string{
2623 "-srtp-profiles",
2624 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2625 },
2626 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2627 })
2628 // Test that the MKI is ignored.
2629 testCases = append(testCases, testCase{
2630 protocol: dtls,
2631 testType: serverTest,
2632 name: "SRTP-Server-IgnoreMKI",
2633 config: Config{
2634 SRTPProtectionProfiles: []uint16{SRTP_AES128_CM_HMAC_SHA1_80},
2635 Bugs: ProtocolBugs{
2636 SRTPMasterKeyIdentifer: "bogus",
2637 },
2638 },
2639 flags: []string{
2640 "-srtp-profiles",
2641 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2642 },
2643 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2644 })
2645 // Test that SRTP isn't negotiated on the server if there were
2646 // no matching profiles.
2647 testCases = append(testCases, testCase{
2648 protocol: dtls,
2649 testType: serverTest,
2650 name: "SRTP-Server-NoMatch",
2651 config: Config{
2652 SRTPProtectionProfiles: []uint16{100, 101, 102},
2653 },
2654 flags: []string{
2655 "-srtp-profiles",
2656 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2657 },
2658 expectedSRTPProtectionProfile: 0,
2659 })
2660 // Test that the server returning an invalid SRTP profile is
2661 // flagged as an error by the client.
2662 testCases = append(testCases, testCase{
2663 protocol: dtls,
2664 name: "SRTP-Client-NoMatch",
2665 config: Config{
2666 Bugs: ProtocolBugs{
2667 SendSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_32,
2668 },
2669 },
2670 flags: []string{
2671 "-srtp-profiles",
2672 "SRTP_AES128_CM_SHA1_80",
2673 },
2674 shouldFail: true,
2675 expectedError: ":BAD_SRTP_PROTECTION_PROFILE_LIST:",
2676 })
David Benjamin61f95272014-11-25 01:55:35 -05002677 // Test OCSP stapling and SCT list.
2678 testCases = append(testCases, testCase{
2679 name: "OCSPStapling",
2680 flags: []string{
2681 "-enable-ocsp-stapling",
2682 "-expect-ocsp-response",
2683 base64.StdEncoding.EncodeToString(testOCSPResponse),
2684 },
2685 })
2686 testCases = append(testCases, testCase{
2687 name: "SignedCertificateTimestampList",
2688 flags: []string{
2689 "-enable-signed-cert-timestamps",
2690 "-expect-signed-cert-timestamps",
2691 base64.StdEncoding.EncodeToString(testSCTList),
2692 },
2693 })
David Benjamine78bfde2014-09-06 12:45:15 -04002694}
2695
David Benjamin01fe8202014-09-24 15:21:44 -04002696func addResumptionVersionTests() {
David Benjamin01fe8202014-09-24 15:21:44 -04002697 for _, sessionVers := range tlsVersions {
David Benjamin01fe8202014-09-24 15:21:44 -04002698 for _, resumeVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05002699 protocols := []protocol{tls}
2700 if sessionVers.hasDTLS && resumeVers.hasDTLS {
2701 protocols = append(protocols, dtls)
David Benjaminbdf5e722014-11-11 00:52:15 -05002702 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002703 for _, protocol := range protocols {
2704 suffix := "-" + sessionVers.name + "-" + resumeVers.name
2705 if protocol == dtls {
2706 suffix += "-DTLS"
2707 }
2708
David Benjaminece3de92015-03-16 18:02:20 -04002709 if sessionVers.version == resumeVers.version {
2710 testCases = append(testCases, testCase{
2711 protocol: protocol,
2712 name: "Resume-Client" + suffix,
2713 resumeSession: true,
2714 config: Config{
2715 MaxVersion: sessionVers.version,
2716 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
David Benjamin8b8c0062014-11-23 02:47:52 -05002717 },
David Benjaminece3de92015-03-16 18:02:20 -04002718 expectedVersion: sessionVers.version,
2719 expectedResumeVersion: resumeVers.version,
2720 })
2721 } else {
2722 testCases = append(testCases, testCase{
2723 protocol: protocol,
2724 name: "Resume-Client-Mismatch" + suffix,
2725 resumeSession: true,
2726 config: Config{
2727 MaxVersion: sessionVers.version,
2728 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
David Benjamin8b8c0062014-11-23 02:47:52 -05002729 },
David Benjaminece3de92015-03-16 18:02:20 -04002730 expectedVersion: sessionVers.version,
2731 resumeConfig: &Config{
2732 MaxVersion: resumeVers.version,
2733 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2734 Bugs: ProtocolBugs{
2735 AllowSessionVersionMismatch: true,
2736 },
2737 },
2738 expectedResumeVersion: resumeVers.version,
2739 shouldFail: true,
2740 expectedError: ":OLD_SESSION_VERSION_NOT_RETURNED:",
2741 })
2742 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002743
2744 testCases = append(testCases, testCase{
2745 protocol: protocol,
2746 name: "Resume-Client-NoResume" + suffix,
2747 flags: []string{"-expect-session-miss"},
2748 resumeSession: true,
2749 config: Config{
2750 MaxVersion: sessionVers.version,
2751 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2752 },
2753 expectedVersion: sessionVers.version,
2754 resumeConfig: &Config{
2755 MaxVersion: resumeVers.version,
2756 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2757 },
2758 newSessionsOnResume: true,
2759 expectedResumeVersion: resumeVers.version,
2760 })
2761
2762 var flags []string
2763 if sessionVers.version != resumeVers.version {
2764 flags = append(flags, "-expect-session-miss")
2765 }
2766 testCases = append(testCases, testCase{
2767 protocol: protocol,
2768 testType: serverTest,
2769 name: "Resume-Server" + suffix,
2770 flags: flags,
2771 resumeSession: true,
2772 config: Config{
2773 MaxVersion: sessionVers.version,
2774 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2775 },
2776 expectedVersion: sessionVers.version,
2777 resumeConfig: &Config{
2778 MaxVersion: resumeVers.version,
2779 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2780 },
2781 expectedResumeVersion: resumeVers.version,
2782 })
2783 }
David Benjamin01fe8202014-09-24 15:21:44 -04002784 }
2785 }
David Benjaminece3de92015-03-16 18:02:20 -04002786
2787 testCases = append(testCases, testCase{
2788 name: "Resume-Client-CipherMismatch",
2789 resumeSession: true,
2790 config: Config{
2791 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2792 },
2793 resumeConfig: &Config{
2794 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2795 Bugs: ProtocolBugs{
2796 SendCipherSuite: TLS_RSA_WITH_AES_128_CBC_SHA,
2797 },
2798 },
2799 shouldFail: true,
2800 expectedError: ":OLD_SESSION_CIPHER_NOT_RETURNED:",
2801 })
David Benjamin01fe8202014-09-24 15:21:44 -04002802}
2803
Adam Langley2ae77d22014-10-28 17:29:33 -07002804func addRenegotiationTests() {
2805 testCases = append(testCases, testCase{
2806 testType: serverTest,
2807 name: "Renegotiate-Server",
2808 flags: []string{"-renegotiate"},
2809 shimWritesFirst: true,
2810 })
2811 testCases = append(testCases, testCase{
2812 testType: serverTest,
David Benjamincdea40c2015-03-19 14:09:43 -04002813 name: "Renegotiate-Server-Full",
2814 config: Config{
2815 Bugs: ProtocolBugs{
2816 NeverResumeOnRenego: true,
2817 },
2818 },
2819 flags: []string{"-renegotiate"},
2820 shimWritesFirst: true,
2821 })
2822 testCases = append(testCases, testCase{
2823 testType: serverTest,
Adam Langley2ae77d22014-10-28 17:29:33 -07002824 name: "Renegotiate-Server-EmptyExt",
2825 config: Config{
2826 Bugs: ProtocolBugs{
2827 EmptyRenegotiationInfo: true,
2828 },
2829 },
2830 flags: []string{"-renegotiate"},
2831 shimWritesFirst: true,
2832 shouldFail: true,
2833 expectedError: ":RENEGOTIATION_MISMATCH:",
2834 })
2835 testCases = append(testCases, testCase{
2836 testType: serverTest,
2837 name: "Renegotiate-Server-BadExt",
2838 config: Config{
2839 Bugs: ProtocolBugs{
2840 BadRenegotiationInfo: true,
2841 },
2842 },
2843 flags: []string{"-renegotiate"},
2844 shimWritesFirst: true,
2845 shouldFail: true,
2846 expectedError: ":RENEGOTIATION_MISMATCH:",
2847 })
David Benjaminca6554b2014-11-08 12:31:52 -05002848 testCases = append(testCases, testCase{
2849 testType: serverTest,
2850 name: "Renegotiate-Server-ClientInitiated",
2851 renegotiate: true,
2852 })
2853 testCases = append(testCases, testCase{
2854 testType: serverTest,
2855 name: "Renegotiate-Server-ClientInitiated-NoExt",
2856 renegotiate: true,
2857 config: Config{
2858 Bugs: ProtocolBugs{
2859 NoRenegotiationInfo: true,
2860 },
2861 },
2862 shouldFail: true,
2863 expectedError: ":UNSAFE_LEGACY_RENEGOTIATION_DISABLED:",
2864 })
2865 testCases = append(testCases, testCase{
2866 testType: serverTest,
2867 name: "Renegotiate-Server-ClientInitiated-NoExt-Allowed",
2868 renegotiate: true,
2869 config: Config{
2870 Bugs: ProtocolBugs{
2871 NoRenegotiationInfo: true,
2872 },
2873 },
2874 flags: []string{"-allow-unsafe-legacy-renegotiation"},
2875 })
David Benjaminb16346b2015-04-08 19:16:58 -04002876 testCases = append(testCases, testCase{
2877 testType: serverTest,
2878 name: "Renegotiate-Server-ClientInitiated-Forbidden",
2879 renegotiate: true,
2880 flags: []string{"-reject-peer-renegotiations"},
2881 shouldFail: true,
2882 expectedError: ":NO_RENEGOTIATION:",
2883 expectedLocalError: "remote error: no renegotiation",
2884 })
David Benjamin3c9746a2015-03-19 15:00:10 -04002885 // Regression test for CVE-2015-0291.
2886 testCases = append(testCases, testCase{
2887 testType: serverTest,
2888 name: "Renegotiate-Server-NoSignatureAlgorithms",
2889 config: Config{
2890 Bugs: ProtocolBugs{
2891 NeverResumeOnRenego: true,
2892 NoSignatureAlgorithmsOnRenego: true,
2893 },
2894 },
2895 flags: []string{"-renegotiate"},
2896 shimWritesFirst: true,
2897 })
Adam Langley2ae77d22014-10-28 17:29:33 -07002898 // TODO(agl): test the renegotiation info SCSV.
Adam Langleycf2d4f42014-10-28 19:06:14 -07002899 testCases = append(testCases, testCase{
2900 name: "Renegotiate-Client",
2901 renegotiate: true,
2902 })
2903 testCases = append(testCases, testCase{
David Benjamincdea40c2015-03-19 14:09:43 -04002904 name: "Renegotiate-Client-Full",
2905 config: Config{
2906 Bugs: ProtocolBugs{
2907 NeverResumeOnRenego: true,
2908 },
2909 },
2910 renegotiate: true,
2911 })
2912 testCases = append(testCases, testCase{
Adam Langleycf2d4f42014-10-28 19:06:14 -07002913 name: "Renegotiate-Client-EmptyExt",
2914 renegotiate: true,
2915 config: Config{
2916 Bugs: ProtocolBugs{
2917 EmptyRenegotiationInfo: true,
2918 },
2919 },
2920 shouldFail: true,
2921 expectedError: ":RENEGOTIATION_MISMATCH:",
2922 })
2923 testCases = append(testCases, testCase{
2924 name: "Renegotiate-Client-BadExt",
2925 renegotiate: true,
2926 config: Config{
2927 Bugs: ProtocolBugs{
2928 BadRenegotiationInfo: true,
2929 },
2930 },
2931 shouldFail: true,
2932 expectedError: ":RENEGOTIATION_MISMATCH:",
2933 })
2934 testCases = append(testCases, testCase{
2935 name: "Renegotiate-Client-SwitchCiphers",
2936 renegotiate: true,
2937 config: Config{
2938 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2939 },
2940 renegotiateCiphers: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2941 })
2942 testCases = append(testCases, testCase{
2943 name: "Renegotiate-Client-SwitchCiphers2",
2944 renegotiate: true,
2945 config: Config{
2946 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2947 },
2948 renegotiateCiphers: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2949 })
David Benjaminc44b1df2014-11-23 12:11:01 -05002950 testCases = append(testCases, testCase{
David Benjaminb16346b2015-04-08 19:16:58 -04002951 name: "Renegotiate-Client-Forbidden",
2952 renegotiate: true,
2953 flags: []string{"-reject-peer-renegotiations"},
2954 shouldFail: true,
2955 expectedError: ":NO_RENEGOTIATION:",
2956 expectedLocalError: "remote error: no renegotiation",
2957 })
2958 testCases = append(testCases, testCase{
David Benjaminc44b1df2014-11-23 12:11:01 -05002959 name: "Renegotiate-SameClientVersion",
2960 renegotiate: true,
2961 config: Config{
2962 MaxVersion: VersionTLS10,
2963 Bugs: ProtocolBugs{
2964 RequireSameRenegoClientVersion: true,
2965 },
2966 },
2967 })
Adam Langley2ae77d22014-10-28 17:29:33 -07002968}
2969
David Benjamin5e961c12014-11-07 01:48:35 -05002970func addDTLSReplayTests() {
2971 // Test that sequence number replays are detected.
2972 testCases = append(testCases, testCase{
2973 protocol: dtls,
2974 name: "DTLS-Replay",
2975 replayWrites: true,
2976 })
2977
2978 // Test the outgoing sequence number skipping by values larger
2979 // than the retransmit window.
2980 testCases = append(testCases, testCase{
2981 protocol: dtls,
2982 name: "DTLS-Replay-LargeGaps",
2983 config: Config{
2984 Bugs: ProtocolBugs{
2985 SequenceNumberIncrement: 127,
2986 },
2987 },
2988 replayWrites: true,
2989 })
2990}
2991
Feng Lu41aa3252014-11-21 22:47:56 -08002992func addFastRadioPaddingTests() {
David Benjamin1e29a6b2014-12-10 02:27:24 -05002993 testCases = append(testCases, testCase{
2994 protocol: tls,
2995 name: "FastRadio-Padding",
Feng Lu41aa3252014-11-21 22:47:56 -08002996 config: Config{
2997 Bugs: ProtocolBugs{
2998 RequireFastradioPadding: true,
2999 },
3000 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05003001 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08003002 })
David Benjamin1e29a6b2014-12-10 02:27:24 -05003003 testCases = append(testCases, testCase{
3004 protocol: dtls,
David Benjamin5f237bc2015-02-11 17:14:15 -05003005 name: "FastRadio-Padding-DTLS",
Feng Lu41aa3252014-11-21 22:47:56 -08003006 config: Config{
3007 Bugs: ProtocolBugs{
3008 RequireFastradioPadding: true,
3009 },
3010 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05003011 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08003012 })
3013}
3014
David Benjamin000800a2014-11-14 01:43:59 -05003015var testHashes = []struct {
3016 name string
3017 id uint8
3018}{
3019 {"SHA1", hashSHA1},
3020 {"SHA224", hashSHA224},
3021 {"SHA256", hashSHA256},
3022 {"SHA384", hashSHA384},
3023 {"SHA512", hashSHA512},
3024}
3025
3026func addSigningHashTests() {
3027 // Make sure each hash works. Include some fake hashes in the list and
3028 // ensure they're ignored.
3029 for _, hash := range testHashes {
3030 testCases = append(testCases, testCase{
3031 name: "SigningHash-ClientAuth-" + hash.name,
3032 config: Config{
3033 ClientAuth: RequireAnyClientCert,
3034 SignatureAndHashes: []signatureAndHash{
3035 {signatureRSA, 42},
3036 {signatureRSA, hash.id},
3037 {signatureRSA, 255},
3038 },
3039 },
3040 flags: []string{
3041 "-cert-file", rsaCertificateFile,
3042 "-key-file", rsaKeyFile,
3043 },
3044 })
3045
3046 testCases = append(testCases, testCase{
3047 testType: serverTest,
3048 name: "SigningHash-ServerKeyExchange-Sign-" + hash.name,
3049 config: Config{
3050 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
3051 SignatureAndHashes: []signatureAndHash{
3052 {signatureRSA, 42},
3053 {signatureRSA, hash.id},
3054 {signatureRSA, 255},
3055 },
3056 },
3057 })
3058 }
3059
3060 // Test that hash resolution takes the signature type into account.
3061 testCases = append(testCases, testCase{
3062 name: "SigningHash-ClientAuth-SignatureType",
3063 config: Config{
3064 ClientAuth: RequireAnyClientCert,
3065 SignatureAndHashes: []signatureAndHash{
3066 {signatureECDSA, hashSHA512},
3067 {signatureRSA, hashSHA384},
3068 {signatureECDSA, hashSHA1},
3069 },
3070 },
3071 flags: []string{
3072 "-cert-file", rsaCertificateFile,
3073 "-key-file", rsaKeyFile,
3074 },
3075 })
3076
3077 testCases = append(testCases, testCase{
3078 testType: serverTest,
3079 name: "SigningHash-ServerKeyExchange-SignatureType",
3080 config: Config{
3081 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
3082 SignatureAndHashes: []signatureAndHash{
3083 {signatureECDSA, hashSHA512},
3084 {signatureRSA, hashSHA384},
3085 {signatureECDSA, hashSHA1},
3086 },
3087 },
3088 })
3089
3090 // Test that, if the list is missing, the peer falls back to SHA-1.
3091 testCases = append(testCases, testCase{
3092 name: "SigningHash-ClientAuth-Fallback",
3093 config: Config{
3094 ClientAuth: RequireAnyClientCert,
3095 SignatureAndHashes: []signatureAndHash{
3096 {signatureRSA, hashSHA1},
3097 },
3098 Bugs: ProtocolBugs{
3099 NoSignatureAndHashes: true,
3100 },
3101 },
3102 flags: []string{
3103 "-cert-file", rsaCertificateFile,
3104 "-key-file", rsaKeyFile,
3105 },
3106 })
3107
3108 testCases = append(testCases, testCase{
3109 testType: serverTest,
3110 name: "SigningHash-ServerKeyExchange-Fallback",
3111 config: Config{
3112 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
3113 SignatureAndHashes: []signatureAndHash{
3114 {signatureRSA, hashSHA1},
3115 },
3116 Bugs: ProtocolBugs{
3117 NoSignatureAndHashes: true,
3118 },
3119 },
3120 })
David Benjamin72dc7832015-03-16 17:49:43 -04003121
3122 // Test that hash preferences are enforced. BoringSSL defaults to
3123 // rejecting MD5 signatures.
3124 testCases = append(testCases, testCase{
3125 testType: serverTest,
3126 name: "SigningHash-ClientAuth-Enforced",
3127 config: Config{
3128 Certificates: []Certificate{rsaCertificate},
3129 SignatureAndHashes: []signatureAndHash{
3130 {signatureRSA, hashMD5},
3131 // Advertise SHA-1 so the handshake will
3132 // proceed, but the shim's preferences will be
3133 // ignored in CertificateVerify generation, so
3134 // MD5 will be chosen.
3135 {signatureRSA, hashSHA1},
3136 },
3137 Bugs: ProtocolBugs{
3138 IgnorePeerSignatureAlgorithmPreferences: true,
3139 },
3140 },
3141 flags: []string{"-require-any-client-certificate"},
3142 shouldFail: true,
3143 expectedError: ":WRONG_SIGNATURE_TYPE:",
3144 })
3145
3146 testCases = append(testCases, testCase{
3147 name: "SigningHash-ServerKeyExchange-Enforced",
3148 config: Config{
3149 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
3150 SignatureAndHashes: []signatureAndHash{
3151 {signatureRSA, hashMD5},
3152 },
3153 Bugs: ProtocolBugs{
3154 IgnorePeerSignatureAlgorithmPreferences: true,
3155 },
3156 },
3157 shouldFail: true,
3158 expectedError: ":WRONG_SIGNATURE_TYPE:",
3159 })
David Benjamin000800a2014-11-14 01:43:59 -05003160}
3161
David Benjamin83f90402015-01-27 01:09:43 -05003162// timeouts is the retransmit schedule for BoringSSL. It doubles and
3163// caps at 60 seconds. On the 13th timeout, it gives up.
3164var timeouts = []time.Duration{
3165 1 * time.Second,
3166 2 * time.Second,
3167 4 * time.Second,
3168 8 * time.Second,
3169 16 * time.Second,
3170 32 * time.Second,
3171 60 * time.Second,
3172 60 * time.Second,
3173 60 * time.Second,
3174 60 * time.Second,
3175 60 * time.Second,
3176 60 * time.Second,
3177 60 * time.Second,
3178}
3179
3180func addDTLSRetransmitTests() {
3181 // Test that this is indeed the timeout schedule. Stress all
3182 // four patterns of handshake.
3183 for i := 1; i < len(timeouts); i++ {
3184 number := strconv.Itoa(i)
3185 testCases = append(testCases, testCase{
3186 protocol: dtls,
3187 name: "DTLS-Retransmit-Client-" + number,
3188 config: Config{
3189 Bugs: ProtocolBugs{
3190 TimeoutSchedule: timeouts[:i],
3191 },
3192 },
3193 resumeSession: true,
3194 flags: []string{"-async"},
3195 })
3196 testCases = append(testCases, testCase{
3197 protocol: dtls,
3198 testType: serverTest,
3199 name: "DTLS-Retransmit-Server-" + number,
3200 config: Config{
3201 Bugs: ProtocolBugs{
3202 TimeoutSchedule: timeouts[:i],
3203 },
3204 },
3205 resumeSession: true,
3206 flags: []string{"-async"},
3207 })
3208 }
3209
3210 // Test that exceeding the timeout schedule hits a read
3211 // timeout.
3212 testCases = append(testCases, testCase{
3213 protocol: dtls,
3214 name: "DTLS-Retransmit-Timeout",
3215 config: Config{
3216 Bugs: ProtocolBugs{
3217 TimeoutSchedule: timeouts,
3218 },
3219 },
3220 resumeSession: true,
3221 flags: []string{"-async"},
3222 shouldFail: true,
3223 expectedError: ":READ_TIMEOUT_EXPIRED:",
3224 })
3225
3226 // Test that timeout handling has a fudge factor, due to API
3227 // problems.
3228 testCases = append(testCases, testCase{
3229 protocol: dtls,
3230 name: "DTLS-Retransmit-Fudge",
3231 config: Config{
3232 Bugs: ProtocolBugs{
3233 TimeoutSchedule: []time.Duration{
3234 timeouts[0] - 10*time.Millisecond,
3235 },
3236 },
3237 },
3238 resumeSession: true,
3239 flags: []string{"-async"},
3240 })
David Benjamin7eaab4c2015-03-02 19:01:16 -05003241
3242 // Test that the final Finished retransmitting isn't
3243 // duplicated if the peer badly fragments everything.
3244 testCases = append(testCases, testCase{
3245 testType: serverTest,
3246 protocol: dtls,
3247 name: "DTLS-Retransmit-Fragmented",
3248 config: Config{
3249 Bugs: ProtocolBugs{
3250 TimeoutSchedule: []time.Duration{timeouts[0]},
3251 MaxHandshakeRecordLength: 2,
3252 },
3253 },
3254 flags: []string{"-async"},
3255 })
David Benjamin83f90402015-01-27 01:09:43 -05003256}
3257
David Benjaminc565ebb2015-04-03 04:06:36 -04003258func addExportKeyingMaterialTests() {
3259 for _, vers := range tlsVersions {
3260 if vers.version == VersionSSL30 {
3261 continue
3262 }
3263 testCases = append(testCases, testCase{
3264 name: "ExportKeyingMaterial-" + vers.name,
3265 config: Config{
3266 MaxVersion: vers.version,
3267 },
3268 exportKeyingMaterial: 1024,
3269 exportLabel: "label",
3270 exportContext: "context",
3271 useExportContext: true,
3272 })
3273 testCases = append(testCases, testCase{
3274 name: "ExportKeyingMaterial-NoContext-" + vers.name,
3275 config: Config{
3276 MaxVersion: vers.version,
3277 },
3278 exportKeyingMaterial: 1024,
3279 })
3280 testCases = append(testCases, testCase{
3281 name: "ExportKeyingMaterial-EmptyContext-" + vers.name,
3282 config: Config{
3283 MaxVersion: vers.version,
3284 },
3285 exportKeyingMaterial: 1024,
3286 useExportContext: true,
3287 })
3288 testCases = append(testCases, testCase{
3289 name: "ExportKeyingMaterial-Small-" + vers.name,
3290 config: Config{
3291 MaxVersion: vers.version,
3292 },
3293 exportKeyingMaterial: 1,
3294 exportLabel: "label",
3295 exportContext: "context",
3296 useExportContext: true,
3297 })
3298 }
3299 testCases = append(testCases, testCase{
3300 name: "ExportKeyingMaterial-SSL3",
3301 config: Config{
3302 MaxVersion: VersionSSL30,
3303 },
3304 exportKeyingMaterial: 1024,
3305 exportLabel: "label",
3306 exportContext: "context",
3307 useExportContext: true,
3308 shouldFail: true,
3309 expectedError: "failed to export keying material",
3310 })
3311}
3312
David Benjamin884fdf12014-08-02 15:28:23 -04003313func worker(statusChan chan statusMsg, c chan *testCase, buildDir string, wg *sync.WaitGroup) {
Adam Langley95c29f32014-06-20 12:00:00 -07003314 defer wg.Done()
3315
3316 for test := range c {
Adam Langley69a01602014-11-17 17:26:55 -08003317 var err error
3318
3319 if *mallocTest < 0 {
3320 statusChan <- statusMsg{test: test, started: true}
3321 err = runTest(test, buildDir, -1)
3322 } else {
3323 for mallocNumToFail := int64(*mallocTest); ; mallocNumToFail++ {
3324 statusChan <- statusMsg{test: test, started: true}
3325 if err = runTest(test, buildDir, mallocNumToFail); err != errMoreMallocs {
3326 if err != nil {
3327 fmt.Printf("\n\nmalloc test failed at %d: %s\n", mallocNumToFail, err)
3328 }
3329 break
3330 }
3331 }
3332 }
Adam Langley95c29f32014-06-20 12:00:00 -07003333 statusChan <- statusMsg{test: test, err: err}
3334 }
3335}
3336
3337type statusMsg struct {
3338 test *testCase
3339 started bool
3340 err error
3341}
3342
David Benjamin5f237bc2015-02-11 17:14:15 -05003343func statusPrinter(doneChan chan *testOutput, statusChan chan statusMsg, total int) {
Adam Langley95c29f32014-06-20 12:00:00 -07003344 var started, done, failed, lineLen int
Adam Langley95c29f32014-06-20 12:00:00 -07003345
David Benjamin5f237bc2015-02-11 17:14:15 -05003346 testOutput := newTestOutput()
Adam Langley95c29f32014-06-20 12:00:00 -07003347 for msg := range statusChan {
David Benjamin5f237bc2015-02-11 17:14:15 -05003348 if !*pipe {
3349 // Erase the previous status line.
David Benjamin87c8a642015-02-21 01:54:29 -05003350 var erase string
3351 for i := 0; i < lineLen; i++ {
3352 erase += "\b \b"
3353 }
3354 fmt.Print(erase)
David Benjamin5f237bc2015-02-11 17:14:15 -05003355 }
3356
Adam Langley95c29f32014-06-20 12:00:00 -07003357 if msg.started {
3358 started++
3359 } else {
3360 done++
David Benjamin5f237bc2015-02-11 17:14:15 -05003361
3362 if msg.err != nil {
3363 fmt.Printf("FAILED (%s)\n%s\n", msg.test.name, msg.err)
3364 failed++
3365 testOutput.addResult(msg.test.name, "FAIL")
3366 } else {
3367 if *pipe {
3368 // Print each test instead of a status line.
3369 fmt.Printf("PASSED (%s)\n", msg.test.name)
3370 }
3371 testOutput.addResult(msg.test.name, "PASS")
3372 }
Adam Langley95c29f32014-06-20 12:00:00 -07003373 }
3374
David Benjamin5f237bc2015-02-11 17:14:15 -05003375 if !*pipe {
3376 // Print a new status line.
3377 line := fmt.Sprintf("%d/%d/%d/%d", failed, done, started, total)
3378 lineLen = len(line)
3379 os.Stdout.WriteString(line)
Adam Langley95c29f32014-06-20 12:00:00 -07003380 }
Adam Langley95c29f32014-06-20 12:00:00 -07003381 }
David Benjamin5f237bc2015-02-11 17:14:15 -05003382
3383 doneChan <- testOutput
Adam Langley95c29f32014-06-20 12:00:00 -07003384}
3385
3386func main() {
3387 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 -04003388 var flagNumWorkers *int = flag.Int("num-workers", runtime.NumCPU(), "The number of workers to run in parallel.")
David Benjamin884fdf12014-08-02 15:28:23 -04003389 var flagBuildDir *string = flag.String("build-dir", "../../../build", "The build directory to run the shim from.")
Adam Langley95c29f32014-06-20 12:00:00 -07003390
3391 flag.Parse()
3392
3393 addCipherSuiteTests()
3394 addBadECDSASignatureTests()
Adam Langley80842bd2014-06-20 12:00:00 -07003395 addCBCPaddingTests()
Kenny Root7fdeaf12014-08-05 15:23:37 -07003396 addCBCSplittingTests()
David Benjamin636293b2014-07-08 17:59:18 -04003397 addClientAuthTests()
Adam Langley524e7172015-02-20 16:04:00 -08003398 addDDoSCallbackTests()
David Benjamin7e2e6cf2014-08-07 17:44:24 -04003399 addVersionNegotiationTests()
David Benjaminaccb4542014-12-12 23:44:33 -05003400 addMinimumVersionTests()
David Benjamin5c24a1d2014-08-31 00:59:27 -04003401 addD5BugTests()
David Benjamine78bfde2014-09-06 12:45:15 -04003402 addExtensionTests()
David Benjamin01fe8202014-09-24 15:21:44 -04003403 addResumptionVersionTests()
Adam Langley75712922014-10-10 16:23:43 -07003404 addExtendedMasterSecretTests()
Adam Langley2ae77d22014-10-28 17:29:33 -07003405 addRenegotiationTests()
David Benjamin5e961c12014-11-07 01:48:35 -05003406 addDTLSReplayTests()
David Benjamin000800a2014-11-14 01:43:59 -05003407 addSigningHashTests()
Feng Lu41aa3252014-11-21 22:47:56 -08003408 addFastRadioPaddingTests()
David Benjamin83f90402015-01-27 01:09:43 -05003409 addDTLSRetransmitTests()
David Benjaminc565ebb2015-04-03 04:06:36 -04003410 addExportKeyingMaterialTests()
David Benjamin43ec06f2014-08-05 02:28:57 -04003411 for _, async := range []bool{false, true} {
3412 for _, splitHandshake := range []bool{false, true} {
David Benjamin6fd297b2014-08-11 18:43:38 -04003413 for _, protocol := range []protocol{tls, dtls} {
3414 addStateMachineCoverageTests(async, splitHandshake, protocol)
3415 }
David Benjamin43ec06f2014-08-05 02:28:57 -04003416 }
3417 }
Adam Langley95c29f32014-06-20 12:00:00 -07003418
3419 var wg sync.WaitGroup
3420
David Benjamin2bc8e6f2014-08-02 15:22:37 -04003421 numWorkers := *flagNumWorkers
Adam Langley95c29f32014-06-20 12:00:00 -07003422
3423 statusChan := make(chan statusMsg, numWorkers)
3424 testChan := make(chan *testCase, numWorkers)
David Benjamin5f237bc2015-02-11 17:14:15 -05003425 doneChan := make(chan *testOutput)
Adam Langley95c29f32014-06-20 12:00:00 -07003426
David Benjamin025b3d32014-07-01 19:53:04 -04003427 go statusPrinter(doneChan, statusChan, len(testCases))
Adam Langley95c29f32014-06-20 12:00:00 -07003428
3429 for i := 0; i < numWorkers; i++ {
3430 wg.Add(1)
David Benjamin884fdf12014-08-02 15:28:23 -04003431 go worker(statusChan, testChan, *flagBuildDir, &wg)
Adam Langley95c29f32014-06-20 12:00:00 -07003432 }
3433
David Benjamin025b3d32014-07-01 19:53:04 -04003434 for i := range testCases {
3435 if len(*flagTest) == 0 || *flagTest == testCases[i].name {
3436 testChan <- &testCases[i]
Adam Langley95c29f32014-06-20 12:00:00 -07003437 }
3438 }
3439
3440 close(testChan)
3441 wg.Wait()
3442 close(statusChan)
David Benjamin5f237bc2015-02-11 17:14:15 -05003443 testOutput := <-doneChan
Adam Langley95c29f32014-06-20 12:00:00 -07003444
3445 fmt.Printf("\n")
David Benjamin5f237bc2015-02-11 17:14:15 -05003446
3447 if *jsonOutput != "" {
3448 if err := testOutput.writeTo(*jsonOutput); err != nil {
3449 fmt.Fprintf(os.Stderr, "Error: %s\n", err)
3450 }
3451 }
David Benjamin2ab7a862015-04-04 17:02:18 -04003452
3453 if !testOutput.allPassed {
3454 os.Exit(1)
3455 }
Adam Langley95c29f32014-06-20 12:00:00 -07003456}