blob: 57aee7400338c5736018c42178258cac2d0e3f3e [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,
David Benjamin25f08462015-04-15 16:13:49 -0400208 expectedError: ":BAD_SIGNATURE:",
Adam Langley95c29f32014-06-20 12:00:00 -0700209 },
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 {
David Benjamindcd979f2015-04-20 18:26:52 -0400304 name: "SkipCertificateStatus",
305 config: Config{
306 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
307 Bugs: ProtocolBugs{
308 SkipCertificateStatus: true,
309 },
310 },
311 flags: []string{
312 "-enable-ocsp-stapling",
313 },
314 },
315 {
David Benjamin9c651c92014-07-12 13:27:45 -0400316 name: "SkipServerKeyExchange",
317 config: Config{
318 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
319 Bugs: ProtocolBugs{
320 SkipServerKeyExchange: true,
321 },
322 },
323 shouldFail: true,
324 expectedError: ":UNEXPECTED_MESSAGE:",
325 },
David Benjamin1f5f62b2014-07-12 16:18:02 -0400326 {
David Benjamina0e52232014-07-19 17:39:58 -0400327 name: "SkipChangeCipherSpec-Client",
328 config: Config{
329 Bugs: ProtocolBugs{
330 SkipChangeCipherSpec: true,
331 },
332 },
333 shouldFail: true,
David Benjamin86271ee2014-07-21 16:14:03 -0400334 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
David Benjamina0e52232014-07-19 17:39:58 -0400335 },
336 {
337 testType: serverTest,
338 name: "SkipChangeCipherSpec-Server",
339 config: Config{
340 Bugs: ProtocolBugs{
341 SkipChangeCipherSpec: true,
342 },
343 },
344 shouldFail: true,
David Benjamin86271ee2014-07-21 16:14:03 -0400345 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
David Benjamina0e52232014-07-19 17:39:58 -0400346 },
David Benjamin42be6452014-07-21 14:50:23 -0400347 {
348 testType: serverTest,
349 name: "SkipChangeCipherSpec-Server-NPN",
350 config: Config{
351 NextProtos: []string{"bar"},
352 Bugs: ProtocolBugs{
353 SkipChangeCipherSpec: true,
354 },
355 },
356 flags: []string{
357 "-advertise-npn", "\x03foo\x03bar\x03baz",
358 },
359 shouldFail: true,
David Benjamin86271ee2014-07-21 16:14:03 -0400360 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
361 },
362 {
363 name: "FragmentAcrossChangeCipherSpec-Client",
364 config: Config{
365 Bugs: ProtocolBugs{
366 FragmentAcrossChangeCipherSpec: true,
367 },
368 },
369 shouldFail: true,
370 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
371 },
372 {
373 testType: serverTest,
374 name: "FragmentAcrossChangeCipherSpec-Server",
375 config: Config{
376 Bugs: ProtocolBugs{
377 FragmentAcrossChangeCipherSpec: true,
378 },
379 },
380 shouldFail: true,
381 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
382 },
383 {
384 testType: serverTest,
385 name: "FragmentAcrossChangeCipherSpec-Server-NPN",
386 config: Config{
387 NextProtos: []string{"bar"},
388 Bugs: ProtocolBugs{
389 FragmentAcrossChangeCipherSpec: true,
390 },
391 },
392 flags: []string{
393 "-advertise-npn", "\x03foo\x03bar\x03baz",
394 },
395 shouldFail: true,
396 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
David Benjamin42be6452014-07-21 14:50:23 -0400397 },
David Benjaminf3ec83d2014-07-21 22:42:34 -0400398 {
399 testType: serverTest,
David Benjamin3fd1fbd2015-02-03 16:07:32 -0500400 name: "Alert",
401 config: Config{
402 Bugs: ProtocolBugs{
403 SendSpuriousAlert: alertRecordOverflow,
404 },
405 },
406 shouldFail: true,
407 expectedError: ":TLSV1_ALERT_RECORD_OVERFLOW:",
408 },
409 {
410 protocol: dtls,
411 testType: serverTest,
412 name: "Alert-DTLS",
413 config: Config{
414 Bugs: ProtocolBugs{
415 SendSpuriousAlert: alertRecordOverflow,
416 },
417 },
418 shouldFail: true,
419 expectedError: ":TLSV1_ALERT_RECORD_OVERFLOW:",
420 },
421 {
422 testType: serverTest,
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400423 name: "FragmentAlert",
424 config: Config{
425 Bugs: ProtocolBugs{
David Benjaminca6c8262014-11-15 19:06:08 -0500426 FragmentAlert: true,
David Benjamin3fd1fbd2015-02-03 16:07:32 -0500427 SendSpuriousAlert: alertRecordOverflow,
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400428 },
429 },
430 shouldFail: true,
431 expectedError: ":BAD_ALERT:",
432 },
433 {
David Benjamin0ea8dda2015-01-31 20:33:40 -0500434 protocol: dtls,
435 testType: serverTest,
436 name: "FragmentAlert-DTLS",
437 config: Config{
438 Bugs: ProtocolBugs{
439 FragmentAlert: true,
David Benjamin3fd1fbd2015-02-03 16:07:32 -0500440 SendSpuriousAlert: alertRecordOverflow,
David Benjamin0ea8dda2015-01-31 20:33:40 -0500441 },
442 },
443 shouldFail: true,
444 expectedError: ":BAD_ALERT:",
445 },
446 {
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400447 testType: serverTest,
David Benjaminf3ec83d2014-07-21 22:42:34 -0400448 name: "EarlyChangeCipherSpec-server-1",
449 config: Config{
450 Bugs: ProtocolBugs{
451 EarlyChangeCipherSpec: 1,
452 },
453 },
454 shouldFail: true,
455 expectedError: ":CCS_RECEIVED_EARLY:",
456 },
457 {
458 testType: serverTest,
459 name: "EarlyChangeCipherSpec-server-2",
460 config: Config{
461 Bugs: ProtocolBugs{
462 EarlyChangeCipherSpec: 2,
463 },
464 },
465 shouldFail: true,
466 expectedError: ":CCS_RECEIVED_EARLY:",
467 },
David Benjamind23f4122014-07-23 15:09:48 -0400468 {
David Benjamind23f4122014-07-23 15:09:48 -0400469 name: "SkipNewSessionTicket",
470 config: Config{
471 Bugs: ProtocolBugs{
472 SkipNewSessionTicket: true,
473 },
474 },
475 shouldFail: true,
476 expectedError: ":CCS_RECEIVED_EARLY:",
477 },
David Benjamin7e3305e2014-07-28 14:52:32 -0400478 {
David Benjamind86c7672014-08-02 04:07:12 -0400479 testType: serverTest,
David Benjaminbef270a2014-08-02 04:22:02 -0400480 name: "FallbackSCSV",
481 config: Config{
482 MaxVersion: VersionTLS11,
483 Bugs: ProtocolBugs{
484 SendFallbackSCSV: true,
485 },
486 },
487 shouldFail: true,
488 expectedError: ":INAPPROPRIATE_FALLBACK:",
489 },
490 {
491 testType: serverTest,
492 name: "FallbackSCSV-VersionMatch",
493 config: Config{
494 Bugs: ProtocolBugs{
495 SendFallbackSCSV: true,
496 },
497 },
498 },
David Benjamin98214542014-08-07 18:02:39 -0400499 {
500 testType: serverTest,
501 name: "FragmentedClientVersion",
502 config: Config{
503 Bugs: ProtocolBugs{
504 MaxHandshakeRecordLength: 1,
505 FragmentClientVersion: true,
506 },
507 },
David Benjamin82c9e902014-12-12 15:55:27 -0500508 expectedVersion: VersionTLS12,
David Benjamin98214542014-08-07 18:02:39 -0400509 },
David Benjamin98e882e2014-08-08 13:24:34 -0400510 {
511 testType: serverTest,
512 name: "MinorVersionTolerance",
513 config: Config{
514 Bugs: ProtocolBugs{
515 SendClientVersion: 0x03ff,
516 },
517 },
518 expectedVersion: VersionTLS12,
519 },
520 {
521 testType: serverTest,
522 name: "MajorVersionTolerance",
523 config: Config{
524 Bugs: ProtocolBugs{
525 SendClientVersion: 0x0400,
526 },
527 },
528 expectedVersion: VersionTLS12,
529 },
530 {
531 testType: serverTest,
532 name: "VersionTooLow",
533 config: Config{
534 Bugs: ProtocolBugs{
535 SendClientVersion: 0x0200,
536 },
537 },
538 shouldFail: true,
539 expectedError: ":UNSUPPORTED_PROTOCOL:",
540 },
541 {
542 testType: serverTest,
543 name: "HttpGET",
544 sendPrefix: "GET / HTTP/1.0\n",
545 shouldFail: true,
546 expectedError: ":HTTP_REQUEST:",
547 },
548 {
549 testType: serverTest,
550 name: "HttpPOST",
551 sendPrefix: "POST / HTTP/1.0\n",
552 shouldFail: true,
553 expectedError: ":HTTP_REQUEST:",
554 },
555 {
556 testType: serverTest,
557 name: "HttpHEAD",
558 sendPrefix: "HEAD / HTTP/1.0\n",
559 shouldFail: true,
560 expectedError: ":HTTP_REQUEST:",
561 },
562 {
563 testType: serverTest,
564 name: "HttpPUT",
565 sendPrefix: "PUT / HTTP/1.0\n",
566 shouldFail: true,
567 expectedError: ":HTTP_REQUEST:",
568 },
569 {
570 testType: serverTest,
571 name: "HttpCONNECT",
572 sendPrefix: "CONNECT www.google.com:443 HTTP/1.0\n",
573 shouldFail: true,
574 expectedError: ":HTTPS_PROXY_REQUEST:",
575 },
David Benjamin39ebf532014-08-31 02:23:49 -0400576 {
David Benjaminf080ecd2014-12-11 18:48:59 -0500577 testType: serverTest,
578 name: "Garbage",
579 sendPrefix: "blah",
580 shouldFail: true,
581 expectedError: ":UNKNOWN_PROTOCOL:",
582 },
583 {
David Benjamin39ebf532014-08-31 02:23:49 -0400584 name: "SkipCipherVersionCheck",
585 config: Config{
586 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
587 MaxVersion: VersionTLS11,
588 Bugs: ProtocolBugs{
589 SkipCipherVersionCheck: true,
590 },
591 },
592 shouldFail: true,
593 expectedError: ":WRONG_CIPHER_RETURNED:",
594 },
David Benjamin9114fae2014-11-08 11:41:14 -0500595 {
David Benjamina3e89492015-02-26 15:16:22 -0500596 name: "RSAEphemeralKey",
David Benjamin9114fae2014-11-08 11:41:14 -0500597 config: Config{
598 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
599 Bugs: ProtocolBugs{
David Benjamina3e89492015-02-26 15:16:22 -0500600 RSAEphemeralKey: true,
David Benjamin9114fae2014-11-08 11:41:14 -0500601 },
602 },
603 shouldFail: true,
604 expectedError: ":UNEXPECTED_MESSAGE:",
605 },
David Benjamin128dbc32014-12-01 01:27:42 -0500606 {
607 name: "DisableEverything",
608 flags: []string{"-no-tls12", "-no-tls11", "-no-tls1", "-no-ssl3"},
609 shouldFail: true,
610 expectedError: ":WRONG_SSL_VERSION:",
611 },
612 {
613 protocol: dtls,
614 name: "DisableEverything-DTLS",
615 flags: []string{"-no-tls12", "-no-tls1"},
616 shouldFail: true,
617 expectedError: ":WRONG_SSL_VERSION:",
618 },
David Benjamin780d6dd2015-01-06 12:03:19 -0500619 {
620 name: "NoSharedCipher",
621 config: Config{
622 CipherSuites: []uint16{},
623 },
624 shouldFail: true,
625 expectedError: ":HANDSHAKE_FAILURE_ON_CLIENT_HELLO:",
626 },
David Benjamin13be1de2015-01-11 16:29:36 -0500627 {
628 protocol: dtls,
629 testType: serverTest,
630 name: "MTU",
631 config: Config{
632 Bugs: ProtocolBugs{
633 MaxPacketLength: 256,
634 },
635 },
636 flags: []string{"-mtu", "256"},
637 },
638 {
639 protocol: dtls,
640 testType: serverTest,
641 name: "MTUExceeded",
642 config: Config{
643 Bugs: ProtocolBugs{
644 MaxPacketLength: 255,
645 },
646 },
647 flags: []string{"-mtu", "256"},
648 shouldFail: true,
649 expectedLocalError: "dtls: exceeded maximum packet length",
650 },
David Benjamin6095de82014-12-27 01:50:38 -0500651 {
652 name: "CertMismatchRSA",
653 config: Config{
654 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
655 Certificates: []Certificate{getECDSACertificate()},
656 Bugs: ProtocolBugs{
657 SendCipherSuite: TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
658 },
659 },
660 shouldFail: true,
661 expectedError: ":WRONG_CERTIFICATE_TYPE:",
662 },
663 {
664 name: "CertMismatchECDSA",
665 config: Config{
666 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
667 Certificates: []Certificate{getRSACertificate()},
668 Bugs: ProtocolBugs{
669 SendCipherSuite: TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
670 },
671 },
672 shouldFail: true,
673 expectedError: ":WRONG_CERTIFICATE_TYPE:",
674 },
David Benjamin5fa3eba2015-01-22 16:35:40 -0500675 {
676 name: "TLSFatalBadPackets",
677 damageFirstWrite: true,
678 shouldFail: true,
679 expectedError: ":DECRYPTION_FAILED_OR_BAD_RECORD_MAC:",
680 },
681 {
682 protocol: dtls,
683 name: "DTLSIgnoreBadPackets",
684 damageFirstWrite: true,
685 },
686 {
687 protocol: dtls,
688 name: "DTLSIgnoreBadPackets-Async",
689 damageFirstWrite: true,
690 flags: []string{"-async"},
691 },
David Benjamin4189bd92015-01-25 23:52:39 -0500692 {
693 name: "AppDataAfterChangeCipherSpec",
694 config: Config{
695 Bugs: ProtocolBugs{
696 AppDataAfterChangeCipherSpec: []byte("TEST MESSAGE"),
697 },
698 },
699 shouldFail: true,
700 expectedError: ":DATA_BETWEEN_CCS_AND_FINISHED:",
701 },
702 {
703 protocol: dtls,
704 name: "AppDataAfterChangeCipherSpec-DTLS",
705 config: Config{
706 Bugs: ProtocolBugs{
707 AppDataAfterChangeCipherSpec: []byte("TEST MESSAGE"),
708 },
709 },
David Benjamin4417d052015-04-05 04:17:25 -0400710 // BoringSSL's DTLS implementation will drop the out-of-order
711 // application data.
David Benjamin4189bd92015-01-25 23:52:39 -0500712 },
David Benjaminb3774b92015-01-31 17:16:01 -0500713 {
David Benjamindc3da932015-03-12 15:09:02 -0400714 name: "AlertAfterChangeCipherSpec",
715 config: Config{
716 Bugs: ProtocolBugs{
717 AlertAfterChangeCipherSpec: alertRecordOverflow,
718 },
719 },
720 shouldFail: true,
721 expectedError: ":TLSV1_ALERT_RECORD_OVERFLOW:",
722 },
723 {
724 protocol: dtls,
725 name: "AlertAfterChangeCipherSpec-DTLS",
726 config: Config{
727 Bugs: ProtocolBugs{
728 AlertAfterChangeCipherSpec: alertRecordOverflow,
729 },
730 },
731 shouldFail: true,
732 expectedError: ":TLSV1_ALERT_RECORD_OVERFLOW:",
733 },
734 {
David Benjaminb3774b92015-01-31 17:16:01 -0500735 protocol: dtls,
736 name: "ReorderHandshakeFragments-Small-DTLS",
737 config: Config{
738 Bugs: ProtocolBugs{
739 ReorderHandshakeFragments: true,
740 // Small enough that every handshake message is
741 // fragmented.
742 MaxHandshakeRecordLength: 2,
743 },
744 },
745 },
746 {
747 protocol: dtls,
748 name: "ReorderHandshakeFragments-Large-DTLS",
749 config: Config{
750 Bugs: ProtocolBugs{
751 ReorderHandshakeFragments: true,
752 // Large enough that no handshake message is
753 // fragmented.
David Benjaminb3774b92015-01-31 17:16:01 -0500754 MaxHandshakeRecordLength: 2048,
755 },
756 },
757 },
David Benjaminddb9f152015-02-03 15:44:39 -0500758 {
David Benjamin75381222015-03-02 19:30:30 -0500759 protocol: dtls,
760 name: "MixCompleteMessageWithFragments-DTLS",
761 config: Config{
762 Bugs: ProtocolBugs{
763 ReorderHandshakeFragments: true,
764 MixCompleteMessageWithFragments: true,
765 MaxHandshakeRecordLength: 2,
766 },
767 },
768 },
769 {
David Benjaminddb9f152015-02-03 15:44:39 -0500770 name: "SendInvalidRecordType",
771 config: Config{
772 Bugs: ProtocolBugs{
773 SendInvalidRecordType: true,
774 },
775 },
776 shouldFail: true,
777 expectedError: ":UNEXPECTED_RECORD:",
778 },
779 {
780 protocol: dtls,
781 name: "SendInvalidRecordType-DTLS",
782 config: Config{
783 Bugs: ProtocolBugs{
784 SendInvalidRecordType: true,
785 },
786 },
787 shouldFail: true,
788 expectedError: ":UNEXPECTED_RECORD:",
789 },
David Benjaminb80168e2015-02-08 18:30:14 -0500790 {
791 name: "FalseStart-SkipServerSecondLeg",
792 config: Config{
793 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
794 NextProtos: []string{"foo"},
795 Bugs: ProtocolBugs{
796 SkipNewSessionTicket: true,
797 SkipChangeCipherSpec: true,
798 SkipFinished: true,
799 ExpectFalseStart: true,
800 },
801 },
802 flags: []string{
803 "-false-start",
David Benjamin87e4acd2015-04-02 19:57:35 -0400804 "-handshake-never-done",
David Benjaminb80168e2015-02-08 18:30:14 -0500805 "-advertise-alpn", "\x03foo",
806 },
807 shimWritesFirst: true,
808 shouldFail: true,
809 expectedError: ":UNEXPECTED_RECORD:",
810 },
David Benjamin931ab342015-02-08 19:46:57 -0500811 {
812 name: "FalseStart-SkipServerSecondLeg-Implicit",
813 config: Config{
814 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
815 NextProtos: []string{"foo"},
816 Bugs: ProtocolBugs{
817 SkipNewSessionTicket: true,
818 SkipChangeCipherSpec: true,
819 SkipFinished: true,
820 },
821 },
822 flags: []string{
823 "-implicit-handshake",
824 "-false-start",
David Benjamin87e4acd2015-04-02 19:57:35 -0400825 "-handshake-never-done",
David Benjamin931ab342015-02-08 19:46:57 -0500826 "-advertise-alpn", "\x03foo",
827 },
828 shouldFail: true,
829 expectedError: ":UNEXPECTED_RECORD:",
830 },
David Benjamin6f5c0f42015-02-24 01:23:21 -0500831 {
832 testType: serverTest,
833 name: "FailEarlyCallback",
834 flags: []string{"-fail-early-callback"},
835 shouldFail: true,
836 expectedError: ":CONNECTION_REJECTED:",
837 expectedLocalError: "remote error: access denied",
838 },
David Benjaminbcb2d912015-02-24 23:45:43 -0500839 {
840 name: "WrongMessageType",
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 },
850 {
851 protocol: dtls,
852 name: "WrongMessageType-DTLS",
853 config: Config{
854 Bugs: ProtocolBugs{
855 WrongCertificateMessageType: true,
856 },
857 },
858 shouldFail: true,
859 expectedError: ":UNEXPECTED_MESSAGE:",
860 expectedLocalError: "remote error: unexpected message",
861 },
David Benjamin75381222015-03-02 19:30:30 -0500862 {
863 protocol: dtls,
864 name: "FragmentMessageTypeMismatch-DTLS",
865 config: Config{
866 Bugs: ProtocolBugs{
867 MaxHandshakeRecordLength: 2,
868 FragmentMessageTypeMismatch: true,
869 },
870 },
871 shouldFail: true,
872 expectedError: ":FRAGMENT_MISMATCH:",
873 },
874 {
875 protocol: dtls,
876 name: "FragmentMessageLengthMismatch-DTLS",
877 config: Config{
878 Bugs: ProtocolBugs{
879 MaxHandshakeRecordLength: 2,
880 FragmentMessageLengthMismatch: true,
881 },
882 },
883 shouldFail: true,
884 expectedError: ":FRAGMENT_MISMATCH:",
885 },
886 {
887 protocol: dtls,
888 name: "SplitFragmentHeader-DTLS",
889 config: Config{
890 Bugs: ProtocolBugs{
891 SplitFragmentHeader: true,
892 },
893 },
894 shouldFail: true,
895 expectedError: ":UNEXPECTED_MESSAGE:",
896 },
897 {
898 protocol: dtls,
899 name: "SplitFragmentBody-DTLS",
900 config: Config{
901 Bugs: ProtocolBugs{
902 SplitFragmentBody: true,
903 },
904 },
905 shouldFail: true,
906 expectedError: ":UNEXPECTED_MESSAGE:",
907 },
908 {
909 protocol: dtls,
910 name: "SendEmptyFragments-DTLS",
911 config: Config{
912 Bugs: ProtocolBugs{
913 SendEmptyFragments: true,
914 },
915 },
916 },
David Benjamin67d1fb52015-03-16 15:16:23 -0400917 {
918 name: "UnsupportedCipherSuite",
919 config: Config{
920 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
921 Bugs: ProtocolBugs{
922 IgnorePeerCipherPreferences: true,
923 },
924 },
925 flags: []string{"-cipher", "DEFAULT:!RC4"},
926 shouldFail: true,
927 expectedError: ":WRONG_CIPHER_RETURNED:",
928 },
David Benjamin340d5ed2015-03-21 02:21:37 -0400929 {
David Benjaminc574f412015-04-20 11:13:01 -0400930 name: "UnsupportedCurve",
931 config: Config{
932 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
933 // BoringSSL implements P-224 but doesn't enable it by
934 // default.
935 CurvePreferences: []CurveID{CurveP224},
936 Bugs: ProtocolBugs{
937 IgnorePeerCurvePreferences: true,
938 },
939 },
940 shouldFail: true,
941 expectedError: ":WRONG_CURVE:",
942 },
943 {
David Benjamin340d5ed2015-03-21 02:21:37 -0400944 name: "SendWarningAlerts",
945 config: Config{
946 Bugs: ProtocolBugs{
947 SendWarningAlerts: alertAccessDenied,
948 },
949 },
950 },
951 {
952 protocol: dtls,
953 name: "SendWarningAlerts-DTLS",
954 config: Config{
955 Bugs: ProtocolBugs{
956 SendWarningAlerts: alertAccessDenied,
957 },
958 },
959 },
David Benjamin513f0ea2015-04-02 19:33:31 -0400960 {
961 name: "BadFinished",
962 config: Config{
963 Bugs: ProtocolBugs{
964 BadFinished: true,
965 },
966 },
967 shouldFail: true,
968 expectedError: ":DIGEST_CHECK_FAILED:",
969 },
970 {
971 name: "FalseStart-BadFinished",
972 config: Config{
973 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
974 NextProtos: []string{"foo"},
975 Bugs: ProtocolBugs{
976 BadFinished: true,
977 ExpectFalseStart: true,
978 },
979 },
980 flags: []string{
981 "-false-start",
David Benjamin87e4acd2015-04-02 19:57:35 -0400982 "-handshake-never-done",
David Benjamin513f0ea2015-04-02 19:33:31 -0400983 "-advertise-alpn", "\x03foo",
984 },
985 shimWritesFirst: true,
986 shouldFail: true,
987 expectedError: ":DIGEST_CHECK_FAILED:",
988 },
David Benjamin1c633152015-04-02 20:19:11 -0400989 {
990 name: "NoFalseStart-NoALPN",
991 config: Config{
992 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
993 Bugs: ProtocolBugs{
994 ExpectFalseStart: true,
995 AlertBeforeFalseStartTest: alertAccessDenied,
996 },
997 },
998 flags: []string{
999 "-false-start",
1000 },
1001 shimWritesFirst: true,
1002 shouldFail: true,
1003 expectedError: ":TLSV1_ALERT_ACCESS_DENIED:",
1004 expectedLocalError: "tls: peer did not false start: EOF",
1005 },
1006 {
1007 name: "NoFalseStart-NoAEAD",
1008 config: Config{
1009 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1010 NextProtos: []string{"foo"},
1011 Bugs: ProtocolBugs{
1012 ExpectFalseStart: true,
1013 AlertBeforeFalseStartTest: alertAccessDenied,
1014 },
1015 },
1016 flags: []string{
1017 "-false-start",
1018 "-advertise-alpn", "\x03foo",
1019 },
1020 shimWritesFirst: true,
1021 shouldFail: true,
1022 expectedError: ":TLSV1_ALERT_ACCESS_DENIED:",
1023 expectedLocalError: "tls: peer did not false start: EOF",
1024 },
1025 {
1026 name: "NoFalseStart-RSA",
1027 config: Config{
1028 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
1029 NextProtos: []string{"foo"},
1030 Bugs: ProtocolBugs{
1031 ExpectFalseStart: true,
1032 AlertBeforeFalseStartTest: alertAccessDenied,
1033 },
1034 },
1035 flags: []string{
1036 "-false-start",
1037 "-advertise-alpn", "\x03foo",
1038 },
1039 shimWritesFirst: true,
1040 shouldFail: true,
1041 expectedError: ":TLSV1_ALERT_ACCESS_DENIED:",
1042 expectedLocalError: "tls: peer did not false start: EOF",
1043 },
1044 {
1045 name: "NoFalseStart-DHE_RSA",
1046 config: Config{
1047 CipherSuites: []uint16{TLS_DHE_RSA_WITH_AES_128_GCM_SHA256},
1048 NextProtos: []string{"foo"},
1049 Bugs: ProtocolBugs{
1050 ExpectFalseStart: true,
1051 AlertBeforeFalseStartTest: alertAccessDenied,
1052 },
1053 },
1054 flags: []string{
1055 "-false-start",
1056 "-advertise-alpn", "\x03foo",
1057 },
1058 shimWritesFirst: true,
1059 shouldFail: true,
1060 expectedError: ":TLSV1_ALERT_ACCESS_DENIED:",
1061 expectedLocalError: "tls: peer did not false start: EOF",
1062 },
Adam Langley95c29f32014-06-20 12:00:00 -07001063}
1064
David Benjamin01fe8202014-09-24 15:21:44 -04001065func doExchange(test *testCase, config *Config, conn net.Conn, messageLen int, isResume bool) error {
David Benjamin65ea8ff2014-11-23 03:01:00 -05001066 var connDebug *recordingConn
David Benjamin5fa3eba2015-01-22 16:35:40 -05001067 var connDamage *damageAdaptor
David Benjamin65ea8ff2014-11-23 03:01:00 -05001068 if *flagDebug {
1069 connDebug = &recordingConn{Conn: conn}
1070 conn = connDebug
1071 defer func() {
1072 connDebug.WriteTo(os.Stdout)
1073 }()
1074 }
1075
David Benjamin6fd297b2014-08-11 18:43:38 -04001076 if test.protocol == dtls {
David Benjamin83f90402015-01-27 01:09:43 -05001077 config.Bugs.PacketAdaptor = newPacketAdaptor(conn)
1078 conn = config.Bugs.PacketAdaptor
David Benjamin5e961c12014-11-07 01:48:35 -05001079 if test.replayWrites {
1080 conn = newReplayAdaptor(conn)
1081 }
David Benjamin6fd297b2014-08-11 18:43:38 -04001082 }
1083
David Benjamin5fa3eba2015-01-22 16:35:40 -05001084 if test.damageFirstWrite {
1085 connDamage = newDamageAdaptor(conn)
1086 conn = connDamage
1087 }
1088
David Benjamin6fd297b2014-08-11 18:43:38 -04001089 if test.sendPrefix != "" {
1090 if _, err := conn.Write([]byte(test.sendPrefix)); err != nil {
1091 return err
1092 }
David Benjamin98e882e2014-08-08 13:24:34 -04001093 }
1094
David Benjamin1d5c83e2014-07-22 19:20:02 -04001095 var tlsConn *Conn
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001096 if test.testType == clientTest {
David Benjamin6fd297b2014-08-11 18:43:38 -04001097 if test.protocol == dtls {
1098 tlsConn = DTLSServer(conn, config)
1099 } else {
1100 tlsConn = Server(conn, config)
1101 }
David Benjamin1d5c83e2014-07-22 19:20:02 -04001102 } else {
1103 config.InsecureSkipVerify = true
David Benjamin6fd297b2014-08-11 18:43:38 -04001104 if test.protocol == dtls {
1105 tlsConn = DTLSClient(conn, config)
1106 } else {
1107 tlsConn = Client(conn, config)
1108 }
David Benjamin1d5c83e2014-07-22 19:20:02 -04001109 }
1110
Adam Langley95c29f32014-06-20 12:00:00 -07001111 if err := tlsConn.Handshake(); err != nil {
1112 return err
1113 }
Kenny Root7fdeaf12014-08-05 15:23:37 -07001114
David Benjamin01fe8202014-09-24 15:21:44 -04001115 // TODO(davidben): move all per-connection expectations into a dedicated
1116 // expectations struct that can be specified separately for the two
1117 // legs.
1118 expectedVersion := test.expectedVersion
1119 if isResume && test.expectedResumeVersion != 0 {
1120 expectedVersion = test.expectedResumeVersion
1121 }
1122 if vers := tlsConn.ConnectionState().Version; expectedVersion != 0 && vers != expectedVersion {
1123 return fmt.Errorf("got version %x, expected %x", vers, expectedVersion)
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001124 }
1125
David Benjamina08e49d2014-08-24 01:46:07 -04001126 if test.expectChannelID {
1127 channelID := tlsConn.ConnectionState().ChannelID
1128 if channelID == nil {
1129 return fmt.Errorf("no channel ID negotiated")
1130 }
1131 if channelID.Curve != channelIDKey.Curve ||
1132 channelIDKey.X.Cmp(channelIDKey.X) != 0 ||
1133 channelIDKey.Y.Cmp(channelIDKey.Y) != 0 {
1134 return fmt.Errorf("incorrect channel ID")
1135 }
1136 }
1137
David Benjaminae2888f2014-09-06 12:58:58 -04001138 if expected := test.expectedNextProto; expected != "" {
1139 if actual := tlsConn.ConnectionState().NegotiatedProtocol; actual != expected {
1140 return fmt.Errorf("next proto mismatch: got %s, wanted %s", actual, expected)
1141 }
1142 }
1143
David Benjaminfc7b0862014-09-06 13:21:53 -04001144 if test.expectedNextProtoType != 0 {
1145 if (test.expectedNextProtoType == alpn) != tlsConn.ConnectionState().NegotiatedProtocolFromALPN {
1146 return fmt.Errorf("next proto type mismatch")
1147 }
1148 }
1149
David Benjaminca6c8262014-11-15 19:06:08 -05001150 if p := tlsConn.ConnectionState().SRTPProtectionProfile; p != test.expectedSRTPProtectionProfile {
1151 return fmt.Errorf("SRTP profile mismatch: got %d, wanted %d", p, test.expectedSRTPProtectionProfile)
1152 }
1153
David Benjaminc565ebb2015-04-03 04:06:36 -04001154 if test.exportKeyingMaterial > 0 {
1155 actual := make([]byte, test.exportKeyingMaterial)
1156 if _, err := io.ReadFull(tlsConn, actual); err != nil {
1157 return err
1158 }
1159 expected, err := tlsConn.ExportKeyingMaterial(test.exportKeyingMaterial, []byte(test.exportLabel), []byte(test.exportContext), test.useExportContext)
1160 if err != nil {
1161 return err
1162 }
1163 if !bytes.Equal(actual, expected) {
1164 return fmt.Errorf("keying material mismatch")
1165 }
1166 }
1167
David Benjamine58c4f52014-08-24 03:47:07 -04001168 if test.shimWritesFirst {
1169 var buf [5]byte
1170 _, err := io.ReadFull(tlsConn, buf[:])
1171 if err != nil {
1172 return err
1173 }
1174 if string(buf[:]) != "hello" {
1175 return fmt.Errorf("bad initial message")
1176 }
1177 }
1178
Adam Langleycf2d4f42014-10-28 19:06:14 -07001179 if test.renegotiate {
1180 if test.renegotiateCiphers != nil {
1181 config.CipherSuites = test.renegotiateCiphers
1182 }
1183 if err := tlsConn.Renegotiate(); err != nil {
1184 return err
1185 }
1186 } else if test.renegotiateCiphers != nil {
1187 panic("renegotiateCiphers without renegotiate")
1188 }
1189
David Benjamin5fa3eba2015-01-22 16:35:40 -05001190 if test.damageFirstWrite {
1191 connDamage.setDamage(true)
1192 tlsConn.Write([]byte("DAMAGED WRITE"))
1193 connDamage.setDamage(false)
1194 }
1195
Kenny Root7fdeaf12014-08-05 15:23:37 -07001196 if messageLen < 0 {
David Benjamin6fd297b2014-08-11 18:43:38 -04001197 if test.protocol == dtls {
1198 return fmt.Errorf("messageLen < 0 not supported for DTLS tests")
1199 }
Kenny Root7fdeaf12014-08-05 15:23:37 -07001200 // Read until EOF.
1201 _, err := io.Copy(ioutil.Discard, tlsConn)
1202 return err
1203 }
1204
David Benjamin4417d052015-04-05 04:17:25 -04001205 if messageLen == 0 {
1206 messageLen = 32
Adam Langley80842bd2014-06-20 12:00:00 -07001207 }
David Benjamin4417d052015-04-05 04:17:25 -04001208 testMessage := make([]byte, messageLen)
1209 for i := range testMessage {
1210 testMessage[i] = 0x42
1211 }
1212 tlsConn.Write(testMessage)
Adam Langley95c29f32014-06-20 12:00:00 -07001213
1214 buf := make([]byte, len(testMessage))
David Benjamin6fd297b2014-08-11 18:43:38 -04001215 if test.protocol == dtls {
1216 bufTmp := make([]byte, len(buf)+1)
1217 n, err := tlsConn.Read(bufTmp)
1218 if err != nil {
1219 return err
1220 }
1221 if n != len(buf) {
1222 return fmt.Errorf("bad reply; length mismatch (%d vs %d)", n, len(buf))
1223 }
1224 copy(buf, bufTmp)
1225 } else {
1226 _, err := io.ReadFull(tlsConn, buf)
1227 if err != nil {
1228 return err
1229 }
Adam Langley95c29f32014-06-20 12:00:00 -07001230 }
1231
1232 for i, v := range buf {
1233 if v != testMessage[i]^0xff {
1234 return fmt.Errorf("bad reply contents at byte %d", i)
1235 }
1236 }
1237
1238 return nil
1239}
1240
David Benjamin325b5c32014-07-01 19:40:31 -04001241func valgrindOf(dbAttach bool, path string, args ...string) *exec.Cmd {
1242 valgrindArgs := []string{"--error-exitcode=99", "--track-origins=yes", "--leak-check=full"}
Adam Langley95c29f32014-06-20 12:00:00 -07001243 if dbAttach {
David Benjamin325b5c32014-07-01 19:40:31 -04001244 valgrindArgs = append(valgrindArgs, "--db-attach=yes", "--db-command=xterm -e gdb -nw %f %p")
Adam Langley95c29f32014-06-20 12:00:00 -07001245 }
David Benjamin325b5c32014-07-01 19:40:31 -04001246 valgrindArgs = append(valgrindArgs, path)
1247 valgrindArgs = append(valgrindArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -07001248
David Benjamin325b5c32014-07-01 19:40:31 -04001249 return exec.Command("valgrind", valgrindArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -07001250}
1251
David Benjamin325b5c32014-07-01 19:40:31 -04001252func gdbOf(path string, args ...string) *exec.Cmd {
1253 xtermArgs := []string{"-e", "gdb", "--args"}
1254 xtermArgs = append(xtermArgs, path)
1255 xtermArgs = append(xtermArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -07001256
David Benjamin325b5c32014-07-01 19:40:31 -04001257 return exec.Command("xterm", xtermArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -07001258}
1259
Adam Langley69a01602014-11-17 17:26:55 -08001260type moreMallocsError struct{}
1261
1262func (moreMallocsError) Error() string {
1263 return "child process did not exhaust all allocation calls"
1264}
1265
1266var errMoreMallocs = moreMallocsError{}
1267
David Benjamin87c8a642015-02-21 01:54:29 -05001268// accept accepts a connection from listener, unless waitChan signals a process
1269// exit first.
1270func acceptOrWait(listener net.Listener, waitChan chan error) (net.Conn, error) {
1271 type connOrError struct {
1272 conn net.Conn
1273 err error
1274 }
1275 connChan := make(chan connOrError, 1)
1276 go func() {
1277 conn, err := listener.Accept()
1278 connChan <- connOrError{conn, err}
1279 close(connChan)
1280 }()
1281 select {
1282 case result := <-connChan:
1283 return result.conn, result.err
1284 case childErr := <-waitChan:
1285 waitChan <- childErr
1286 return nil, fmt.Errorf("child exited early: %s", childErr)
1287 }
1288}
1289
Adam Langley69a01602014-11-17 17:26:55 -08001290func runTest(test *testCase, buildDir string, mallocNumToFail int64) error {
Adam Langley38311732014-10-16 19:04:35 -07001291 if !test.shouldFail && (len(test.expectedError) > 0 || len(test.expectedLocalError) > 0) {
1292 panic("Error expected without shouldFail in " + test.name)
1293 }
1294
David Benjamin87c8a642015-02-21 01:54:29 -05001295 listener, err := net.ListenTCP("tcp4", &net.TCPAddr{IP: net.IP{127, 0, 0, 1}})
1296 if err != nil {
1297 panic(err)
1298 }
1299 defer func() {
1300 if listener != nil {
1301 listener.Close()
1302 }
1303 }()
Adam Langley95c29f32014-06-20 12:00:00 -07001304
David Benjamin884fdf12014-08-02 15:28:23 -04001305 shim_path := path.Join(buildDir, "ssl/test/bssl_shim")
David Benjamin87c8a642015-02-21 01:54:29 -05001306 flags := []string{"-port", strconv.Itoa(listener.Addr().(*net.TCPAddr).Port)}
David Benjamin1d5c83e2014-07-22 19:20:02 -04001307 if test.testType == serverTest {
David Benjamin5a593af2014-08-11 19:51:50 -04001308 flags = append(flags, "-server")
1309
David Benjamin025b3d32014-07-01 19:53:04 -04001310 flags = append(flags, "-key-file")
1311 if test.keyFile == "" {
1312 flags = append(flags, rsaKeyFile)
1313 } else {
1314 flags = append(flags, test.keyFile)
1315 }
1316
1317 flags = append(flags, "-cert-file")
1318 if test.certFile == "" {
1319 flags = append(flags, rsaCertificateFile)
1320 } else {
1321 flags = append(flags, test.certFile)
1322 }
1323 }
David Benjamin5a593af2014-08-11 19:51:50 -04001324
David Benjamin6fd297b2014-08-11 18:43:38 -04001325 if test.protocol == dtls {
1326 flags = append(flags, "-dtls")
1327 }
1328
David Benjamin5a593af2014-08-11 19:51:50 -04001329 if test.resumeSession {
1330 flags = append(flags, "-resume")
1331 }
1332
David Benjamine58c4f52014-08-24 03:47:07 -04001333 if test.shimWritesFirst {
1334 flags = append(flags, "-shim-writes-first")
1335 }
1336
David Benjaminc565ebb2015-04-03 04:06:36 -04001337 if test.exportKeyingMaterial > 0 {
1338 flags = append(flags, "-export-keying-material", strconv.Itoa(test.exportKeyingMaterial))
1339 flags = append(flags, "-export-label", test.exportLabel)
1340 flags = append(flags, "-export-context", test.exportContext)
1341 if test.useExportContext {
1342 flags = append(flags, "-use-export-context")
1343 }
1344 }
1345
David Benjamin025b3d32014-07-01 19:53:04 -04001346 flags = append(flags, test.flags...)
1347
1348 var shim *exec.Cmd
1349 if *useValgrind {
1350 shim = valgrindOf(false, shim_path, flags...)
Adam Langley75712922014-10-10 16:23:43 -07001351 } else if *useGDB {
1352 shim = gdbOf(shim_path, flags...)
David Benjamin025b3d32014-07-01 19:53:04 -04001353 } else {
1354 shim = exec.Command(shim_path, flags...)
1355 }
David Benjamin025b3d32014-07-01 19:53:04 -04001356 shim.Stdin = os.Stdin
1357 var stdoutBuf, stderrBuf bytes.Buffer
1358 shim.Stdout = &stdoutBuf
1359 shim.Stderr = &stderrBuf
Adam Langley69a01602014-11-17 17:26:55 -08001360 if mallocNumToFail >= 0 {
David Benjamin9e128b02015-02-09 13:13:09 -05001361 shim.Env = os.Environ()
1362 shim.Env = append(shim.Env, "MALLOC_NUMBER_TO_FAIL="+strconv.FormatInt(mallocNumToFail, 10))
Adam Langley69a01602014-11-17 17:26:55 -08001363 if *mallocTestDebug {
1364 shim.Env = append(shim.Env, "MALLOC_ABORT_ON_FAIL=1")
1365 }
1366 shim.Env = append(shim.Env, "_MALLOC_CHECK=1")
1367 }
David Benjamin025b3d32014-07-01 19:53:04 -04001368
1369 if err := shim.Start(); err != nil {
Adam Langley95c29f32014-06-20 12:00:00 -07001370 panic(err)
1371 }
David Benjamin87c8a642015-02-21 01:54:29 -05001372 waitChan := make(chan error, 1)
1373 go func() { waitChan <- shim.Wait() }()
Adam Langley95c29f32014-06-20 12:00:00 -07001374
1375 config := test.config
David Benjamin1d5c83e2014-07-22 19:20:02 -04001376 config.ClientSessionCache = NewLRUClientSessionCache(1)
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001377 config.ServerSessionCache = NewLRUServerSessionCache(1)
David Benjamin025b3d32014-07-01 19:53:04 -04001378 if test.testType == clientTest {
1379 if len(config.Certificates) == 0 {
1380 config.Certificates = []Certificate{getRSACertificate()}
1381 }
David Benjamin87c8a642015-02-21 01:54:29 -05001382 } else {
1383 // Supply a ServerName to ensure a constant session cache key,
1384 // rather than falling back to net.Conn.RemoteAddr.
1385 if len(config.ServerName) == 0 {
1386 config.ServerName = "test"
1387 }
David Benjamin025b3d32014-07-01 19:53:04 -04001388 }
Adam Langley95c29f32014-06-20 12:00:00 -07001389
David Benjamin87c8a642015-02-21 01:54:29 -05001390 conn, err := acceptOrWait(listener, waitChan)
1391 if err == nil {
1392 err = doExchange(test, &config, conn, test.messageLen, false /* not a resumption */)
1393 conn.Close()
1394 }
David Benjamin65ea8ff2014-11-23 03:01:00 -05001395
David Benjamin1d5c83e2014-07-22 19:20:02 -04001396 if err == nil && test.resumeSession {
David Benjamin01fe8202014-09-24 15:21:44 -04001397 var resumeConfig Config
1398 if test.resumeConfig != nil {
1399 resumeConfig = *test.resumeConfig
David Benjamin87c8a642015-02-21 01:54:29 -05001400 if len(resumeConfig.ServerName) == 0 {
1401 resumeConfig.ServerName = config.ServerName
1402 }
David Benjamin01fe8202014-09-24 15:21:44 -04001403 if len(resumeConfig.Certificates) == 0 {
1404 resumeConfig.Certificates = []Certificate{getRSACertificate()}
1405 }
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001406 if !test.newSessionsOnResume {
1407 resumeConfig.SessionTicketKey = config.SessionTicketKey
1408 resumeConfig.ClientSessionCache = config.ClientSessionCache
1409 resumeConfig.ServerSessionCache = config.ServerSessionCache
1410 }
David Benjamin01fe8202014-09-24 15:21:44 -04001411 } else {
1412 resumeConfig = config
1413 }
David Benjamin87c8a642015-02-21 01:54:29 -05001414 var connResume net.Conn
1415 connResume, err = acceptOrWait(listener, waitChan)
1416 if err == nil {
1417 err = doExchange(test, &resumeConfig, connResume, test.messageLen, true /* resumption */)
1418 connResume.Close()
1419 }
David Benjamin1d5c83e2014-07-22 19:20:02 -04001420 }
1421
David Benjamin87c8a642015-02-21 01:54:29 -05001422 // Close the listener now. This is to avoid hangs should the shim try to
1423 // open more connections than expected.
1424 listener.Close()
1425 listener = nil
1426
1427 childErr := <-waitChan
Adam Langley69a01602014-11-17 17:26:55 -08001428 if exitError, ok := childErr.(*exec.ExitError); ok {
1429 if exitError.Sys().(syscall.WaitStatus).ExitStatus() == 88 {
1430 return errMoreMallocs
1431 }
1432 }
Adam Langley95c29f32014-06-20 12:00:00 -07001433
1434 stdout := string(stdoutBuf.Bytes())
1435 stderr := string(stderrBuf.Bytes())
1436 failed := err != nil || childErr != nil
David Benjaminc565ebb2015-04-03 04:06:36 -04001437 correctFailure := len(test.expectedError) == 0 || strings.Contains(stderr, test.expectedError)
Adam Langleyac61fa32014-06-23 12:03:11 -07001438 localError := "none"
1439 if err != nil {
1440 localError = err.Error()
1441 }
1442 if len(test.expectedLocalError) != 0 {
1443 correctFailure = correctFailure && strings.Contains(localError, test.expectedLocalError)
1444 }
Adam Langley95c29f32014-06-20 12:00:00 -07001445
1446 if failed != test.shouldFail || failed && !correctFailure {
Adam Langley95c29f32014-06-20 12:00:00 -07001447 childError := "none"
Adam Langley95c29f32014-06-20 12:00:00 -07001448 if childErr != nil {
1449 childError = childErr.Error()
1450 }
1451
1452 var msg string
1453 switch {
1454 case failed && !test.shouldFail:
1455 msg = "unexpected failure"
1456 case !failed && test.shouldFail:
1457 msg = "unexpected success"
1458 case failed && !correctFailure:
Adam Langleyac61fa32014-06-23 12:03:11 -07001459 msg = "bad error (wanted '" + test.expectedError + "' / '" + test.expectedLocalError + "')"
Adam Langley95c29f32014-06-20 12:00:00 -07001460 default:
1461 panic("internal error")
1462 }
1463
David Benjaminc565ebb2015-04-03 04:06:36 -04001464 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 -07001465 }
1466
David Benjaminc565ebb2015-04-03 04:06:36 -04001467 if !*useValgrind && !failed && len(stderr) > 0 {
Adam Langley95c29f32014-06-20 12:00:00 -07001468 println(stderr)
1469 }
1470
1471 return nil
1472}
1473
1474var tlsVersions = []struct {
1475 name string
1476 version uint16
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001477 flag string
David Benjamin8b8c0062014-11-23 02:47:52 -05001478 hasDTLS bool
Adam Langley95c29f32014-06-20 12:00:00 -07001479}{
David Benjamin8b8c0062014-11-23 02:47:52 -05001480 {"SSL3", VersionSSL30, "-no-ssl3", false},
1481 {"TLS1", VersionTLS10, "-no-tls1", true},
1482 {"TLS11", VersionTLS11, "-no-tls11", false},
1483 {"TLS12", VersionTLS12, "-no-tls12", true},
Adam Langley95c29f32014-06-20 12:00:00 -07001484}
1485
1486var testCipherSuites = []struct {
1487 name string
1488 id uint16
1489}{
1490 {"3DES-SHA", TLS_RSA_WITH_3DES_EDE_CBC_SHA},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001491 {"AES128-GCM", TLS_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001492 {"AES128-SHA", TLS_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001493 {"AES128-SHA256", TLS_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001494 {"AES256-GCM", TLS_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001495 {"AES256-SHA", TLS_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001496 {"AES256-SHA256", TLS_RSA_WITH_AES_256_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001497 {"DHE-RSA-AES128-GCM", TLS_DHE_RSA_WITH_AES_128_GCM_SHA256},
1498 {"DHE-RSA-AES128-SHA", TLS_DHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001499 {"DHE-RSA-AES128-SHA256", TLS_DHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001500 {"DHE-RSA-AES256-GCM", TLS_DHE_RSA_WITH_AES_256_GCM_SHA384},
1501 {"DHE-RSA-AES256-SHA", TLS_DHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001502 {"DHE-RSA-AES256-SHA256", TLS_DHE_RSA_WITH_AES_256_CBC_SHA256},
David Benjamine9a80ff2015-04-07 00:46:46 -04001503 {"DHE-RSA-CHACHA20-POLY1305", TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001504 {"ECDHE-ECDSA-AES128-GCM", TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
1505 {"ECDHE-ECDSA-AES128-SHA", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001506 {"ECDHE-ECDSA-AES128-SHA256", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256},
1507 {"ECDHE-ECDSA-AES256-GCM", TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001508 {"ECDHE-ECDSA-AES256-SHA", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001509 {"ECDHE-ECDSA-AES256-SHA384", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384},
David Benjamine9a80ff2015-04-07 00:46:46 -04001510 {"ECDHE-ECDSA-CHACHA20-POLY1305", TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001511 {"ECDHE-ECDSA-RC4-SHA", TLS_ECDHE_ECDSA_WITH_RC4_128_SHA},
David Benjamin2af684f2014-10-27 02:23:15 -04001512 {"ECDHE-PSK-WITH-AES-128-GCM-SHA256", TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001513 {"ECDHE-RSA-AES128-GCM", TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001514 {"ECDHE-RSA-AES128-SHA", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001515 {"ECDHE-RSA-AES128-SHA256", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001516 {"ECDHE-RSA-AES256-GCM", TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001517 {"ECDHE-RSA-AES256-SHA", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001518 {"ECDHE-RSA-AES256-SHA384", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384},
David Benjamine9a80ff2015-04-07 00:46:46 -04001519 {"ECDHE-RSA-CHACHA20-POLY1305", TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001520 {"ECDHE-RSA-RC4-SHA", TLS_ECDHE_RSA_WITH_RC4_128_SHA},
David Benjamin48cae082014-10-27 01:06:24 -04001521 {"PSK-AES128-CBC-SHA", TLS_PSK_WITH_AES_128_CBC_SHA},
1522 {"PSK-AES256-CBC-SHA", TLS_PSK_WITH_AES_256_CBC_SHA},
1523 {"PSK-RC4-SHA", TLS_PSK_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -07001524 {"RC4-MD5", TLS_RSA_WITH_RC4_128_MD5},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001525 {"RC4-SHA", TLS_RSA_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -07001526}
1527
David Benjamin8b8c0062014-11-23 02:47:52 -05001528func hasComponent(suiteName, component string) bool {
1529 return strings.Contains("-"+suiteName+"-", "-"+component+"-")
1530}
1531
David Benjaminf7768e42014-08-31 02:06:47 -04001532func isTLS12Only(suiteName string) bool {
David Benjamin8b8c0062014-11-23 02:47:52 -05001533 return hasComponent(suiteName, "GCM") ||
1534 hasComponent(suiteName, "SHA256") ||
David Benjamine9a80ff2015-04-07 00:46:46 -04001535 hasComponent(suiteName, "SHA384") ||
1536 hasComponent(suiteName, "POLY1305")
David Benjamin8b8c0062014-11-23 02:47:52 -05001537}
1538
1539func isDTLSCipher(suiteName string) bool {
David Benjamine95d20d2014-12-23 11:16:01 -05001540 return !hasComponent(suiteName, "RC4")
David Benjaminf7768e42014-08-31 02:06:47 -04001541}
1542
Adam Langley95c29f32014-06-20 12:00:00 -07001543func addCipherSuiteTests() {
1544 for _, suite := range testCipherSuites {
David Benjamin48cae082014-10-27 01:06:24 -04001545 const psk = "12345"
1546 const pskIdentity = "luggage combo"
1547
Adam Langley95c29f32014-06-20 12:00:00 -07001548 var cert Certificate
David Benjamin025b3d32014-07-01 19:53:04 -04001549 var certFile string
1550 var keyFile string
David Benjamin8b8c0062014-11-23 02:47:52 -05001551 if hasComponent(suite.name, "ECDSA") {
Adam Langley95c29f32014-06-20 12:00:00 -07001552 cert = getECDSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -04001553 certFile = ecdsaCertificateFile
1554 keyFile = ecdsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -07001555 } else {
1556 cert = getRSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -04001557 certFile = rsaCertificateFile
1558 keyFile = rsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -07001559 }
1560
David Benjamin48cae082014-10-27 01:06:24 -04001561 var flags []string
David Benjamin8b8c0062014-11-23 02:47:52 -05001562 if hasComponent(suite.name, "PSK") {
David Benjamin48cae082014-10-27 01:06:24 -04001563 flags = append(flags,
1564 "-psk", psk,
1565 "-psk-identity", pskIdentity)
1566 }
1567
Adam Langley95c29f32014-06-20 12:00:00 -07001568 for _, ver := range tlsVersions {
David Benjaminf7768e42014-08-31 02:06:47 -04001569 if ver.version < VersionTLS12 && isTLS12Only(suite.name) {
Adam Langley95c29f32014-06-20 12:00:00 -07001570 continue
1571 }
1572
David Benjamin025b3d32014-07-01 19:53:04 -04001573 testCases = append(testCases, testCase{
1574 testType: clientTest,
1575 name: ver.name + "-" + suite.name + "-client",
Adam Langley95c29f32014-06-20 12:00:00 -07001576 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001577 MinVersion: ver.version,
1578 MaxVersion: ver.version,
1579 CipherSuites: []uint16{suite.id},
1580 Certificates: []Certificate{cert},
1581 PreSharedKey: []byte(psk),
1582 PreSharedKeyIdentity: pskIdentity,
Adam Langley95c29f32014-06-20 12:00:00 -07001583 },
David Benjamin48cae082014-10-27 01:06:24 -04001584 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001585 resumeSession: true,
Adam Langley95c29f32014-06-20 12:00:00 -07001586 })
David Benjamin025b3d32014-07-01 19:53:04 -04001587
David Benjamin76d8abe2014-08-14 16:25:34 -04001588 testCases = append(testCases, testCase{
1589 testType: serverTest,
1590 name: ver.name + "-" + suite.name + "-server",
1591 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001592 MinVersion: ver.version,
1593 MaxVersion: ver.version,
1594 CipherSuites: []uint16{suite.id},
1595 Certificates: []Certificate{cert},
1596 PreSharedKey: []byte(psk),
1597 PreSharedKeyIdentity: pskIdentity,
David Benjamin76d8abe2014-08-14 16:25:34 -04001598 },
1599 certFile: certFile,
1600 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001601 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001602 resumeSession: true,
David Benjamin76d8abe2014-08-14 16:25:34 -04001603 })
David Benjamin6fd297b2014-08-11 18:43:38 -04001604
David Benjamin8b8c0062014-11-23 02:47:52 -05001605 if ver.hasDTLS && isDTLSCipher(suite.name) {
David Benjamin6fd297b2014-08-11 18:43:38 -04001606 testCases = append(testCases, testCase{
1607 testType: clientTest,
1608 protocol: dtls,
1609 name: "D" + ver.name + "-" + suite.name + "-client",
1610 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001611 MinVersion: ver.version,
1612 MaxVersion: ver.version,
1613 CipherSuites: []uint16{suite.id},
1614 Certificates: []Certificate{cert},
1615 PreSharedKey: []byte(psk),
1616 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001617 },
David Benjamin48cae082014-10-27 01:06:24 -04001618 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001619 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001620 })
1621 testCases = append(testCases, testCase{
1622 testType: serverTest,
1623 protocol: dtls,
1624 name: "D" + ver.name + "-" + suite.name + "-server",
1625 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001626 MinVersion: ver.version,
1627 MaxVersion: ver.version,
1628 CipherSuites: []uint16{suite.id},
1629 Certificates: []Certificate{cert},
1630 PreSharedKey: []byte(psk),
1631 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001632 },
1633 certFile: certFile,
1634 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001635 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001636 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001637 })
1638 }
Adam Langley95c29f32014-06-20 12:00:00 -07001639 }
1640 }
1641}
1642
1643func addBadECDSASignatureTests() {
1644 for badR := BadValue(1); badR < NumBadValues; badR++ {
1645 for badS := BadValue(1); badS < NumBadValues; badS++ {
David Benjamin025b3d32014-07-01 19:53:04 -04001646 testCases = append(testCases, testCase{
Adam Langley95c29f32014-06-20 12:00:00 -07001647 name: fmt.Sprintf("BadECDSA-%d-%d", badR, badS),
1648 config: Config{
1649 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
1650 Certificates: []Certificate{getECDSACertificate()},
1651 Bugs: ProtocolBugs{
1652 BadECDSAR: badR,
1653 BadECDSAS: badS,
1654 },
1655 },
1656 shouldFail: true,
1657 expectedError: "SIGNATURE",
1658 })
1659 }
1660 }
1661}
1662
Adam Langley80842bd2014-06-20 12:00:00 -07001663func addCBCPaddingTests() {
David Benjamin025b3d32014-07-01 19:53:04 -04001664 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001665 name: "MaxCBCPadding",
1666 config: Config{
1667 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1668 Bugs: ProtocolBugs{
1669 MaxPadding: true,
1670 },
1671 },
1672 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1673 })
David Benjamin025b3d32014-07-01 19:53:04 -04001674 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001675 name: "BadCBCPadding",
1676 config: Config{
1677 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1678 Bugs: ProtocolBugs{
1679 PaddingFirstByteBad: true,
1680 },
1681 },
1682 shouldFail: true,
1683 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1684 })
1685 // OpenSSL previously had an issue where the first byte of padding in
1686 // 255 bytes of padding wasn't checked.
David Benjamin025b3d32014-07-01 19:53:04 -04001687 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001688 name: "BadCBCPadding255",
1689 config: Config{
1690 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1691 Bugs: ProtocolBugs{
1692 MaxPadding: true,
1693 PaddingFirstByteBadIf255: true,
1694 },
1695 },
1696 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1697 shouldFail: true,
1698 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1699 })
1700}
1701
Kenny Root7fdeaf12014-08-05 15:23:37 -07001702func addCBCSplittingTests() {
1703 testCases = append(testCases, testCase{
1704 name: "CBCRecordSplitting",
1705 config: Config{
1706 MaxVersion: VersionTLS10,
1707 MinVersion: VersionTLS10,
1708 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1709 },
1710 messageLen: -1, // read until EOF
1711 flags: []string{
1712 "-async",
1713 "-write-different-record-sizes",
1714 "-cbc-record-splitting",
1715 },
David Benjamina8e3e0e2014-08-06 22:11:10 -04001716 })
1717 testCases = append(testCases, testCase{
Kenny Root7fdeaf12014-08-05 15:23:37 -07001718 name: "CBCRecordSplittingPartialWrite",
1719 config: Config{
1720 MaxVersion: VersionTLS10,
1721 MinVersion: VersionTLS10,
1722 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1723 },
1724 messageLen: -1, // read until EOF
1725 flags: []string{
1726 "-async",
1727 "-write-different-record-sizes",
1728 "-cbc-record-splitting",
1729 "-partial-write",
1730 },
1731 })
1732}
1733
David Benjamin636293b2014-07-08 17:59:18 -04001734func addClientAuthTests() {
David Benjamin407a10c2014-07-16 12:58:59 -04001735 // Add a dummy cert pool to stress certificate authority parsing.
1736 // TODO(davidben): Add tests that those values parse out correctly.
1737 certPool := x509.NewCertPool()
1738 cert, err := x509.ParseCertificate(rsaCertificate.Certificate[0])
1739 if err != nil {
1740 panic(err)
1741 }
1742 certPool.AddCert(cert)
1743
David Benjamin636293b2014-07-08 17:59:18 -04001744 for _, ver := range tlsVersions {
David Benjamin636293b2014-07-08 17:59:18 -04001745 testCases = append(testCases, testCase{
1746 testType: clientTest,
David Benjamin67666e72014-07-12 15:47:52 -04001747 name: ver.name + "-Client-ClientAuth-RSA",
David Benjamin636293b2014-07-08 17:59:18 -04001748 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001749 MinVersion: ver.version,
1750 MaxVersion: ver.version,
1751 ClientAuth: RequireAnyClientCert,
1752 ClientCAs: certPool,
David Benjamin636293b2014-07-08 17:59:18 -04001753 },
1754 flags: []string{
1755 "-cert-file", rsaCertificateFile,
1756 "-key-file", rsaKeyFile,
1757 },
1758 })
1759 testCases = append(testCases, testCase{
David Benjamin67666e72014-07-12 15:47:52 -04001760 testType: serverTest,
1761 name: ver.name + "-Server-ClientAuth-RSA",
1762 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001763 MinVersion: ver.version,
1764 MaxVersion: ver.version,
David Benjamin67666e72014-07-12 15:47:52 -04001765 Certificates: []Certificate{rsaCertificate},
1766 },
1767 flags: []string{"-require-any-client-certificate"},
1768 })
David Benjamine098ec22014-08-27 23:13:20 -04001769 if ver.version != VersionSSL30 {
1770 testCases = append(testCases, testCase{
1771 testType: serverTest,
1772 name: ver.name + "-Server-ClientAuth-ECDSA",
1773 config: Config{
1774 MinVersion: ver.version,
1775 MaxVersion: ver.version,
1776 Certificates: []Certificate{ecdsaCertificate},
1777 },
1778 flags: []string{"-require-any-client-certificate"},
1779 })
1780 testCases = append(testCases, testCase{
1781 testType: clientTest,
1782 name: ver.name + "-Client-ClientAuth-ECDSA",
1783 config: Config{
1784 MinVersion: ver.version,
1785 MaxVersion: ver.version,
1786 ClientAuth: RequireAnyClientCert,
1787 ClientCAs: certPool,
1788 },
1789 flags: []string{
1790 "-cert-file", ecdsaCertificateFile,
1791 "-key-file", ecdsaKeyFile,
1792 },
1793 })
1794 }
David Benjamin636293b2014-07-08 17:59:18 -04001795 }
1796}
1797
Adam Langley75712922014-10-10 16:23:43 -07001798func addExtendedMasterSecretTests() {
1799 const expectEMSFlag = "-expect-extended-master-secret"
1800
1801 for _, with := range []bool{false, true} {
1802 prefix := "No"
1803 var flags []string
1804 if with {
1805 prefix = ""
1806 flags = []string{expectEMSFlag}
1807 }
1808
1809 for _, isClient := range []bool{false, true} {
1810 suffix := "-Server"
1811 testType := serverTest
1812 if isClient {
1813 suffix = "-Client"
1814 testType = clientTest
1815 }
1816
1817 for _, ver := range tlsVersions {
1818 test := testCase{
1819 testType: testType,
1820 name: prefix + "ExtendedMasterSecret-" + ver.name + suffix,
1821 config: Config{
1822 MinVersion: ver.version,
1823 MaxVersion: ver.version,
1824 Bugs: ProtocolBugs{
1825 NoExtendedMasterSecret: !with,
1826 RequireExtendedMasterSecret: with,
1827 },
1828 },
David Benjamin48cae082014-10-27 01:06:24 -04001829 flags: flags,
1830 shouldFail: ver.version == VersionSSL30 && with,
Adam Langley75712922014-10-10 16:23:43 -07001831 }
1832 if test.shouldFail {
1833 test.expectedLocalError = "extended master secret required but not supported by peer"
1834 }
1835 testCases = append(testCases, test)
1836 }
1837 }
1838 }
1839
1840 // When a session is resumed, it should still be aware that its master
1841 // secret was generated via EMS and thus it's safe to use tls-unique.
1842 testCases = append(testCases, testCase{
1843 name: "ExtendedMasterSecret-Resume",
1844 config: Config{
1845 Bugs: ProtocolBugs{
1846 RequireExtendedMasterSecret: true,
1847 },
1848 },
1849 flags: []string{expectEMSFlag},
1850 resumeSession: true,
1851 })
1852}
1853
David Benjamin43ec06f2014-08-05 02:28:57 -04001854// Adds tests that try to cover the range of the handshake state machine, under
1855// various conditions. Some of these are redundant with other tests, but they
1856// only cover the synchronous case.
David Benjamin6fd297b2014-08-11 18:43:38 -04001857func addStateMachineCoverageTests(async, splitHandshake bool, protocol protocol) {
David Benjamin43ec06f2014-08-05 02:28:57 -04001858 var suffix string
1859 var flags []string
1860 var maxHandshakeRecordLength int
David Benjamin6fd297b2014-08-11 18:43:38 -04001861 if protocol == dtls {
1862 suffix = "-DTLS"
1863 }
David Benjamin43ec06f2014-08-05 02:28:57 -04001864 if async {
David Benjamin6fd297b2014-08-11 18:43:38 -04001865 suffix += "-Async"
David Benjamin43ec06f2014-08-05 02:28:57 -04001866 flags = append(flags, "-async")
1867 } else {
David Benjamin6fd297b2014-08-11 18:43:38 -04001868 suffix += "-Sync"
David Benjamin43ec06f2014-08-05 02:28:57 -04001869 }
1870 if splitHandshake {
1871 suffix += "-SplitHandshakeRecords"
David Benjamin98214542014-08-07 18:02:39 -04001872 maxHandshakeRecordLength = 1
David Benjamin43ec06f2014-08-05 02:28:57 -04001873 }
1874
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001875 // Basic handshake, with resumption. Client and server,
1876 // session ID and session ticket.
David Benjamin43ec06f2014-08-05 02:28:57 -04001877 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001878 protocol: protocol,
1879 name: "Basic-Client" + suffix,
David Benjamin43ec06f2014-08-05 02:28:57 -04001880 config: Config{
1881 Bugs: ProtocolBugs{
1882 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1883 },
1884 },
David Benjaminbed9aae2014-08-07 19:13:38 -04001885 flags: flags,
1886 resumeSession: true,
1887 })
1888 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001889 protocol: protocol,
1890 name: "Basic-Client-RenewTicket" + suffix,
David Benjaminbed9aae2014-08-07 19:13:38 -04001891 config: Config{
1892 Bugs: ProtocolBugs{
1893 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1894 RenewTicketOnResume: true,
1895 },
1896 },
1897 flags: flags,
1898 resumeSession: true,
David Benjamin43ec06f2014-08-05 02:28:57 -04001899 })
1900 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001901 protocol: protocol,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001902 name: "Basic-Client-NoTicket" + suffix,
1903 config: Config{
1904 SessionTicketsDisabled: true,
1905 Bugs: ProtocolBugs{
1906 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1907 },
1908 },
1909 flags: flags,
1910 resumeSession: true,
1911 })
1912 testCases = append(testCases, testCase{
1913 protocol: protocol,
David Benjamine0e7d0d2015-02-08 19:33:25 -05001914 name: "Basic-Client-Implicit" + suffix,
1915 config: Config{
1916 Bugs: ProtocolBugs{
1917 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1918 },
1919 },
1920 flags: append(flags, "-implicit-handshake"),
1921 resumeSession: true,
1922 })
1923 testCases = append(testCases, testCase{
1924 protocol: protocol,
David Benjamin43ec06f2014-08-05 02:28:57 -04001925 testType: serverTest,
1926 name: "Basic-Server" + suffix,
1927 config: Config{
1928 Bugs: ProtocolBugs{
1929 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1930 },
1931 },
David Benjaminbed9aae2014-08-07 19:13:38 -04001932 flags: flags,
1933 resumeSession: true,
David Benjamin43ec06f2014-08-05 02:28:57 -04001934 })
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001935 testCases = append(testCases, testCase{
1936 protocol: protocol,
1937 testType: serverTest,
1938 name: "Basic-Server-NoTickets" + suffix,
1939 config: Config{
1940 SessionTicketsDisabled: true,
1941 Bugs: ProtocolBugs{
1942 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1943 },
1944 },
1945 flags: flags,
1946 resumeSession: true,
1947 })
David Benjamine0e7d0d2015-02-08 19:33:25 -05001948 testCases = append(testCases, testCase{
1949 protocol: protocol,
1950 testType: serverTest,
1951 name: "Basic-Server-Implicit" + suffix,
1952 config: Config{
1953 Bugs: ProtocolBugs{
1954 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1955 },
1956 },
1957 flags: append(flags, "-implicit-handshake"),
1958 resumeSession: true,
1959 })
David Benjamin6f5c0f42015-02-24 01:23:21 -05001960 testCases = append(testCases, testCase{
1961 protocol: protocol,
1962 testType: serverTest,
1963 name: "Basic-Server-EarlyCallback" + suffix,
1964 config: Config{
1965 Bugs: ProtocolBugs{
1966 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1967 },
1968 },
1969 flags: append(flags, "-use-early-callback"),
1970 resumeSession: true,
1971 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001972
David Benjamin6fd297b2014-08-11 18:43:38 -04001973 // TLS client auth.
1974 testCases = append(testCases, testCase{
1975 protocol: protocol,
1976 testType: clientTest,
1977 name: "ClientAuth-Client" + suffix,
1978 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001979 ClientAuth: RequireAnyClientCert,
David Benjamin6fd297b2014-08-11 18:43:38 -04001980 Bugs: ProtocolBugs{
1981 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1982 },
1983 },
1984 flags: append(flags,
1985 "-cert-file", rsaCertificateFile,
1986 "-key-file", rsaKeyFile),
1987 })
1988 testCases = append(testCases, testCase{
1989 protocol: protocol,
1990 testType: serverTest,
1991 name: "ClientAuth-Server" + suffix,
1992 config: Config{
1993 Certificates: []Certificate{rsaCertificate},
1994 },
1995 flags: append(flags, "-require-any-client-certificate"),
1996 })
1997
David Benjamin43ec06f2014-08-05 02:28:57 -04001998 // No session ticket support; server doesn't send NewSessionTicket.
1999 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04002000 protocol: protocol,
2001 name: "SessionTicketsDisabled-Client" + suffix,
David Benjamin43ec06f2014-08-05 02:28:57 -04002002 config: Config{
2003 SessionTicketsDisabled: true,
2004 Bugs: ProtocolBugs{
2005 MaxHandshakeRecordLength: maxHandshakeRecordLength,
2006 },
2007 },
2008 flags: flags,
2009 })
2010 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04002011 protocol: protocol,
David Benjamin43ec06f2014-08-05 02:28:57 -04002012 testType: serverTest,
2013 name: "SessionTicketsDisabled-Server" + suffix,
2014 config: Config{
2015 SessionTicketsDisabled: true,
2016 Bugs: ProtocolBugs{
2017 MaxHandshakeRecordLength: maxHandshakeRecordLength,
2018 },
2019 },
2020 flags: flags,
2021 })
2022
David Benjamin48cae082014-10-27 01:06:24 -04002023 // Skip ServerKeyExchange in PSK key exchange if there's no
2024 // identity hint.
2025 testCases = append(testCases, testCase{
2026 protocol: protocol,
2027 name: "EmptyPSKHint-Client" + suffix,
2028 config: Config{
2029 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
2030 PreSharedKey: []byte("secret"),
2031 Bugs: ProtocolBugs{
2032 MaxHandshakeRecordLength: maxHandshakeRecordLength,
2033 },
2034 },
2035 flags: append(flags, "-psk", "secret"),
2036 })
2037 testCases = append(testCases, testCase{
2038 protocol: protocol,
2039 testType: serverTest,
2040 name: "EmptyPSKHint-Server" + suffix,
2041 config: Config{
2042 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
2043 PreSharedKey: []byte("secret"),
2044 Bugs: ProtocolBugs{
2045 MaxHandshakeRecordLength: maxHandshakeRecordLength,
2046 },
2047 },
2048 flags: append(flags, "-psk", "secret"),
2049 })
2050
David Benjamin6fd297b2014-08-11 18:43:38 -04002051 if protocol == tls {
2052 // NPN on client and server; results in post-handshake message.
2053 testCases = append(testCases, testCase{
2054 protocol: protocol,
2055 name: "NPN-Client" + suffix,
2056 config: Config{
David Benjaminae2888f2014-09-06 12:58:58 -04002057 NextProtos: []string{"foo"},
David Benjamin6fd297b2014-08-11 18:43:38 -04002058 Bugs: ProtocolBugs{
2059 MaxHandshakeRecordLength: maxHandshakeRecordLength,
2060 },
David Benjamin43ec06f2014-08-05 02:28:57 -04002061 },
David Benjaminfc7b0862014-09-06 13:21:53 -04002062 flags: append(flags, "-select-next-proto", "foo"),
2063 expectedNextProto: "foo",
2064 expectedNextProtoType: npn,
David Benjamin6fd297b2014-08-11 18:43:38 -04002065 })
2066 testCases = append(testCases, testCase{
2067 protocol: protocol,
2068 testType: serverTest,
2069 name: "NPN-Server" + suffix,
2070 config: Config{
2071 NextProtos: []string{"bar"},
2072 Bugs: ProtocolBugs{
2073 MaxHandshakeRecordLength: maxHandshakeRecordLength,
2074 },
David Benjamin43ec06f2014-08-05 02:28:57 -04002075 },
David Benjamin6fd297b2014-08-11 18:43:38 -04002076 flags: append(flags,
2077 "-advertise-npn", "\x03foo\x03bar\x03baz",
2078 "-expect-next-proto", "bar"),
David Benjaminfc7b0862014-09-06 13:21:53 -04002079 expectedNextProto: "bar",
2080 expectedNextProtoType: npn,
David Benjamin6fd297b2014-08-11 18:43:38 -04002081 })
David Benjamin43ec06f2014-08-05 02:28:57 -04002082
David Benjamin195dc782015-02-19 13:27:05 -05002083 // TODO(davidben): Add tests for when False Start doesn't trigger.
2084
David Benjamin6fd297b2014-08-11 18:43:38 -04002085 // Client does False Start and negotiates NPN.
2086 testCases = append(testCases, testCase{
2087 protocol: protocol,
2088 name: "FalseStart" + suffix,
2089 config: Config{
2090 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2091 NextProtos: []string{"foo"},
2092 Bugs: ProtocolBugs{
David Benjamine58c4f52014-08-24 03:47:07 -04002093 ExpectFalseStart: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04002094 MaxHandshakeRecordLength: maxHandshakeRecordLength,
2095 },
David Benjamin43ec06f2014-08-05 02:28:57 -04002096 },
David Benjamin6fd297b2014-08-11 18:43:38 -04002097 flags: append(flags,
2098 "-false-start",
2099 "-select-next-proto", "foo"),
David Benjamine58c4f52014-08-24 03:47:07 -04002100 shimWritesFirst: true,
2101 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04002102 })
David Benjamin43ec06f2014-08-05 02:28:57 -04002103
David Benjaminae2888f2014-09-06 12:58:58 -04002104 // Client does False Start and negotiates ALPN.
2105 testCases = append(testCases, testCase{
2106 protocol: protocol,
2107 name: "FalseStart-ALPN" + suffix,
2108 config: Config{
2109 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2110 NextProtos: []string{"foo"},
2111 Bugs: ProtocolBugs{
2112 ExpectFalseStart: true,
2113 MaxHandshakeRecordLength: maxHandshakeRecordLength,
2114 },
2115 },
2116 flags: append(flags,
2117 "-false-start",
2118 "-advertise-alpn", "\x03foo"),
2119 shimWritesFirst: true,
2120 resumeSession: true,
2121 })
2122
David Benjamin931ab342015-02-08 19:46:57 -05002123 // Client does False Start but doesn't explicitly call
2124 // SSL_connect.
2125 testCases = append(testCases, testCase{
2126 protocol: protocol,
2127 name: "FalseStart-Implicit" + suffix,
2128 config: Config{
2129 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2130 NextProtos: []string{"foo"},
2131 Bugs: ProtocolBugs{
2132 MaxHandshakeRecordLength: maxHandshakeRecordLength,
2133 },
2134 },
2135 flags: append(flags,
2136 "-implicit-handshake",
2137 "-false-start",
2138 "-advertise-alpn", "\x03foo"),
2139 })
2140
David Benjamin6fd297b2014-08-11 18:43:38 -04002141 // False Start without session tickets.
2142 testCases = append(testCases, testCase{
David Benjamin5f237bc2015-02-11 17:14:15 -05002143 name: "FalseStart-SessionTicketsDisabled" + suffix,
David Benjamin6fd297b2014-08-11 18:43:38 -04002144 config: Config{
2145 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2146 NextProtos: []string{"foo"},
2147 SessionTicketsDisabled: true,
David Benjamin4e99c522014-08-24 01:45:30 -04002148 Bugs: ProtocolBugs{
David Benjamine58c4f52014-08-24 03:47:07 -04002149 ExpectFalseStart: true,
David Benjamin4e99c522014-08-24 01:45:30 -04002150 MaxHandshakeRecordLength: maxHandshakeRecordLength,
2151 },
David Benjamin43ec06f2014-08-05 02:28:57 -04002152 },
David Benjamin4e99c522014-08-24 01:45:30 -04002153 flags: append(flags,
David Benjamin6fd297b2014-08-11 18:43:38 -04002154 "-false-start",
2155 "-select-next-proto", "foo",
David Benjamin4e99c522014-08-24 01:45:30 -04002156 ),
David Benjamine58c4f52014-08-24 03:47:07 -04002157 shimWritesFirst: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04002158 })
David Benjamin1e7f8d72014-08-08 12:27:04 -04002159
David Benjamina08e49d2014-08-24 01:46:07 -04002160 // Server parses a V2ClientHello.
David Benjamin6fd297b2014-08-11 18:43:38 -04002161 testCases = append(testCases, testCase{
2162 protocol: protocol,
2163 testType: serverTest,
2164 name: "SendV2ClientHello" + suffix,
2165 config: Config{
2166 // Choose a cipher suite that does not involve
2167 // elliptic curves, so no extensions are
2168 // involved.
2169 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2170 Bugs: ProtocolBugs{
2171 MaxHandshakeRecordLength: maxHandshakeRecordLength,
2172 SendV2ClientHello: true,
2173 },
David Benjamin1e7f8d72014-08-08 12:27:04 -04002174 },
David Benjamin6fd297b2014-08-11 18:43:38 -04002175 flags: flags,
2176 })
David Benjamina08e49d2014-08-24 01:46:07 -04002177
2178 // Client sends a Channel ID.
2179 testCases = append(testCases, testCase{
2180 protocol: protocol,
2181 name: "ChannelID-Client" + suffix,
2182 config: Config{
2183 RequestChannelID: true,
2184 Bugs: ProtocolBugs{
2185 MaxHandshakeRecordLength: maxHandshakeRecordLength,
2186 },
2187 },
2188 flags: append(flags,
2189 "-send-channel-id", channelIDKeyFile,
2190 ),
2191 resumeSession: true,
2192 expectChannelID: true,
2193 })
2194
2195 // Server accepts a Channel ID.
2196 testCases = append(testCases, testCase{
2197 protocol: protocol,
2198 testType: serverTest,
2199 name: "ChannelID-Server" + suffix,
2200 config: Config{
2201 ChannelID: channelIDKey,
2202 Bugs: ProtocolBugs{
2203 MaxHandshakeRecordLength: maxHandshakeRecordLength,
2204 },
2205 },
2206 flags: append(flags,
2207 "-expect-channel-id",
2208 base64.StdEncoding.EncodeToString(channelIDBytes),
2209 ),
2210 resumeSession: true,
2211 expectChannelID: true,
2212 })
David Benjamin6fd297b2014-08-11 18:43:38 -04002213 } else {
2214 testCases = append(testCases, testCase{
2215 protocol: protocol,
2216 name: "SkipHelloVerifyRequest" + suffix,
2217 config: Config{
2218 Bugs: ProtocolBugs{
2219 MaxHandshakeRecordLength: maxHandshakeRecordLength,
2220 SkipHelloVerifyRequest: true,
2221 },
2222 },
2223 flags: flags,
2224 })
David Benjamin6fd297b2014-08-11 18:43:38 -04002225 }
David Benjamin43ec06f2014-08-05 02:28:57 -04002226}
2227
Adam Langley524e7172015-02-20 16:04:00 -08002228func addDDoSCallbackTests() {
2229 // DDoS callback.
2230
2231 for _, resume := range []bool{false, true} {
2232 suffix := "Resume"
2233 if resume {
2234 suffix = "No" + suffix
2235 }
2236
2237 testCases = append(testCases, testCase{
2238 testType: serverTest,
2239 name: "Server-DDoS-OK-" + suffix,
2240 flags: []string{"-install-ddos-callback"},
2241 resumeSession: resume,
2242 })
2243
2244 failFlag := "-fail-ddos-callback"
2245 if resume {
2246 failFlag = "-fail-second-ddos-callback"
2247 }
2248 testCases = append(testCases, testCase{
2249 testType: serverTest,
2250 name: "Server-DDoS-Reject-" + suffix,
2251 flags: []string{"-install-ddos-callback", failFlag},
2252 resumeSession: resume,
2253 shouldFail: true,
2254 expectedError: ":CONNECTION_REJECTED:",
2255 })
2256 }
2257}
2258
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002259func addVersionNegotiationTests() {
2260 for i, shimVers := range tlsVersions {
2261 // Assemble flags to disable all newer versions on the shim.
2262 var flags []string
2263 for _, vers := range tlsVersions[i+1:] {
2264 flags = append(flags, vers.flag)
2265 }
2266
2267 for _, runnerVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05002268 protocols := []protocol{tls}
2269 if runnerVers.hasDTLS && shimVers.hasDTLS {
2270 protocols = append(protocols, dtls)
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002271 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002272 for _, protocol := range protocols {
2273 expectedVersion := shimVers.version
2274 if runnerVers.version < shimVers.version {
2275 expectedVersion = runnerVers.version
2276 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002277
David Benjamin8b8c0062014-11-23 02:47:52 -05002278 suffix := shimVers.name + "-" + runnerVers.name
2279 if protocol == dtls {
2280 suffix += "-DTLS"
2281 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002282
David Benjamin1eb367c2014-12-12 18:17:51 -05002283 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
2284
David Benjamin1e29a6b2014-12-10 02:27:24 -05002285 clientVers := shimVers.version
2286 if clientVers > VersionTLS10 {
2287 clientVers = VersionTLS10
2288 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002289 testCases = append(testCases, testCase{
2290 protocol: protocol,
2291 testType: clientTest,
2292 name: "VersionNegotiation-Client-" + suffix,
2293 config: Config{
2294 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05002295 Bugs: ProtocolBugs{
2296 ExpectInitialRecordVersion: clientVers,
2297 },
David Benjamin8b8c0062014-11-23 02:47:52 -05002298 },
2299 flags: flags,
2300 expectedVersion: expectedVersion,
2301 })
David Benjamin1eb367c2014-12-12 18:17:51 -05002302 testCases = append(testCases, testCase{
2303 protocol: protocol,
2304 testType: clientTest,
2305 name: "VersionNegotiation-Client2-" + suffix,
2306 config: Config{
2307 MaxVersion: runnerVers.version,
2308 Bugs: ProtocolBugs{
2309 ExpectInitialRecordVersion: clientVers,
2310 },
2311 },
2312 flags: []string{"-max-version", shimVersFlag},
2313 expectedVersion: expectedVersion,
2314 })
David Benjamin8b8c0062014-11-23 02:47:52 -05002315
2316 testCases = append(testCases, testCase{
2317 protocol: protocol,
2318 testType: serverTest,
2319 name: "VersionNegotiation-Server-" + suffix,
2320 config: Config{
2321 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05002322 Bugs: ProtocolBugs{
2323 ExpectInitialRecordVersion: expectedVersion,
2324 },
David Benjamin8b8c0062014-11-23 02:47:52 -05002325 },
2326 flags: flags,
2327 expectedVersion: expectedVersion,
2328 })
David Benjamin1eb367c2014-12-12 18:17:51 -05002329 testCases = append(testCases, testCase{
2330 protocol: protocol,
2331 testType: serverTest,
2332 name: "VersionNegotiation-Server2-" + suffix,
2333 config: Config{
2334 MaxVersion: runnerVers.version,
2335 Bugs: ProtocolBugs{
2336 ExpectInitialRecordVersion: expectedVersion,
2337 },
2338 },
2339 flags: []string{"-max-version", shimVersFlag},
2340 expectedVersion: expectedVersion,
2341 })
David Benjamin8b8c0062014-11-23 02:47:52 -05002342 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002343 }
2344 }
2345}
2346
David Benjaminaccb4542014-12-12 23:44:33 -05002347func addMinimumVersionTests() {
2348 for i, shimVers := range tlsVersions {
2349 // Assemble flags to disable all older versions on the shim.
2350 var flags []string
2351 for _, vers := range tlsVersions[:i] {
2352 flags = append(flags, vers.flag)
2353 }
2354
2355 for _, runnerVers := range tlsVersions {
2356 protocols := []protocol{tls}
2357 if runnerVers.hasDTLS && shimVers.hasDTLS {
2358 protocols = append(protocols, dtls)
2359 }
2360 for _, protocol := range protocols {
2361 suffix := shimVers.name + "-" + runnerVers.name
2362 if protocol == dtls {
2363 suffix += "-DTLS"
2364 }
2365 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
2366
David Benjaminaccb4542014-12-12 23:44:33 -05002367 var expectedVersion uint16
2368 var shouldFail bool
2369 var expectedError string
David Benjamin87909c02014-12-13 01:55:01 -05002370 var expectedLocalError string
David Benjaminaccb4542014-12-12 23:44:33 -05002371 if runnerVers.version >= shimVers.version {
2372 expectedVersion = runnerVers.version
2373 } else {
2374 shouldFail = true
2375 expectedError = ":UNSUPPORTED_PROTOCOL:"
David Benjamin87909c02014-12-13 01:55:01 -05002376 if runnerVers.version > VersionSSL30 {
2377 expectedLocalError = "remote error: protocol version not supported"
2378 } else {
2379 expectedLocalError = "remote error: handshake failure"
2380 }
David Benjaminaccb4542014-12-12 23:44:33 -05002381 }
2382
2383 testCases = append(testCases, testCase{
2384 protocol: protocol,
2385 testType: clientTest,
2386 name: "MinimumVersion-Client-" + suffix,
2387 config: Config{
2388 MaxVersion: runnerVers.version,
2389 },
David Benjamin87909c02014-12-13 01:55:01 -05002390 flags: flags,
2391 expectedVersion: expectedVersion,
2392 shouldFail: shouldFail,
2393 expectedError: expectedError,
2394 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002395 })
2396 testCases = append(testCases, testCase{
2397 protocol: protocol,
2398 testType: clientTest,
2399 name: "MinimumVersion-Client2-" + suffix,
2400 config: Config{
2401 MaxVersion: runnerVers.version,
2402 },
David Benjamin87909c02014-12-13 01:55:01 -05002403 flags: []string{"-min-version", shimVersFlag},
2404 expectedVersion: expectedVersion,
2405 shouldFail: shouldFail,
2406 expectedError: expectedError,
2407 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002408 })
2409
2410 testCases = append(testCases, testCase{
2411 protocol: protocol,
2412 testType: serverTest,
2413 name: "MinimumVersion-Server-" + suffix,
2414 config: Config{
2415 MaxVersion: runnerVers.version,
2416 },
David Benjamin87909c02014-12-13 01:55:01 -05002417 flags: flags,
2418 expectedVersion: expectedVersion,
2419 shouldFail: shouldFail,
2420 expectedError: expectedError,
2421 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002422 })
2423 testCases = append(testCases, testCase{
2424 protocol: protocol,
2425 testType: serverTest,
2426 name: "MinimumVersion-Server2-" + suffix,
2427 config: Config{
2428 MaxVersion: runnerVers.version,
2429 },
David Benjamin87909c02014-12-13 01:55:01 -05002430 flags: []string{"-min-version", shimVersFlag},
2431 expectedVersion: expectedVersion,
2432 shouldFail: shouldFail,
2433 expectedError: expectedError,
2434 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002435 })
2436 }
2437 }
2438 }
2439}
2440
David Benjamin5c24a1d2014-08-31 00:59:27 -04002441func addD5BugTests() {
2442 testCases = append(testCases, testCase{
2443 testType: serverTest,
2444 name: "D5Bug-NoQuirk-Reject",
2445 config: Config{
2446 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2447 Bugs: ProtocolBugs{
2448 SSL3RSAKeyExchange: true,
2449 },
2450 },
2451 shouldFail: true,
2452 expectedError: ":TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG:",
2453 })
2454 testCases = append(testCases, testCase{
2455 testType: serverTest,
2456 name: "D5Bug-Quirk-Normal",
2457 config: Config{
2458 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2459 },
2460 flags: []string{"-tls-d5-bug"},
2461 })
2462 testCases = append(testCases, testCase{
2463 testType: serverTest,
2464 name: "D5Bug-Quirk-Bug",
2465 config: Config{
2466 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2467 Bugs: ProtocolBugs{
2468 SSL3RSAKeyExchange: true,
2469 },
2470 },
2471 flags: []string{"-tls-d5-bug"},
2472 })
2473}
2474
David Benjamine78bfde2014-09-06 12:45:15 -04002475func addExtensionTests() {
2476 testCases = append(testCases, testCase{
2477 testType: clientTest,
2478 name: "DuplicateExtensionClient",
2479 config: Config{
2480 Bugs: ProtocolBugs{
2481 DuplicateExtension: true,
2482 },
2483 },
2484 shouldFail: true,
2485 expectedLocalError: "remote error: error decoding message",
2486 })
2487 testCases = append(testCases, testCase{
2488 testType: serverTest,
2489 name: "DuplicateExtensionServer",
2490 config: Config{
2491 Bugs: ProtocolBugs{
2492 DuplicateExtension: true,
2493 },
2494 },
2495 shouldFail: true,
2496 expectedLocalError: "remote error: error decoding message",
2497 })
2498 testCases = append(testCases, testCase{
2499 testType: clientTest,
2500 name: "ServerNameExtensionClient",
2501 config: Config{
2502 Bugs: ProtocolBugs{
2503 ExpectServerName: "example.com",
2504 },
2505 },
2506 flags: []string{"-host-name", "example.com"},
2507 })
2508 testCases = append(testCases, testCase{
2509 testType: clientTest,
David Benjamin5f237bc2015-02-11 17:14:15 -05002510 name: "ServerNameExtensionClientMismatch",
David Benjamine78bfde2014-09-06 12:45:15 -04002511 config: Config{
2512 Bugs: ProtocolBugs{
2513 ExpectServerName: "mismatch.com",
2514 },
2515 },
2516 flags: []string{"-host-name", "example.com"},
2517 shouldFail: true,
2518 expectedLocalError: "tls: unexpected server name",
2519 })
2520 testCases = append(testCases, testCase{
2521 testType: clientTest,
David Benjamin5f237bc2015-02-11 17:14:15 -05002522 name: "ServerNameExtensionClientMissing",
David Benjamine78bfde2014-09-06 12:45:15 -04002523 config: Config{
2524 Bugs: ProtocolBugs{
2525 ExpectServerName: "missing.com",
2526 },
2527 },
2528 shouldFail: true,
2529 expectedLocalError: "tls: unexpected server name",
2530 })
2531 testCases = append(testCases, testCase{
2532 testType: serverTest,
2533 name: "ServerNameExtensionServer",
2534 config: Config{
2535 ServerName: "example.com",
2536 },
2537 flags: []string{"-expect-server-name", "example.com"},
2538 resumeSession: true,
2539 })
David Benjaminae2888f2014-09-06 12:58:58 -04002540 testCases = append(testCases, testCase{
2541 testType: clientTest,
2542 name: "ALPNClient",
2543 config: Config{
2544 NextProtos: []string{"foo"},
2545 },
2546 flags: []string{
2547 "-advertise-alpn", "\x03foo\x03bar\x03baz",
2548 "-expect-alpn", "foo",
2549 },
David Benjaminfc7b0862014-09-06 13:21:53 -04002550 expectedNextProto: "foo",
2551 expectedNextProtoType: alpn,
2552 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04002553 })
2554 testCases = append(testCases, testCase{
2555 testType: serverTest,
2556 name: "ALPNServer",
2557 config: Config{
2558 NextProtos: []string{"foo", "bar", "baz"},
2559 },
2560 flags: []string{
2561 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2562 "-select-alpn", "foo",
2563 },
David Benjaminfc7b0862014-09-06 13:21:53 -04002564 expectedNextProto: "foo",
2565 expectedNextProtoType: alpn,
2566 resumeSession: true,
2567 })
2568 // Test that the server prefers ALPN over NPN.
2569 testCases = append(testCases, testCase{
2570 testType: serverTest,
2571 name: "ALPNServer-Preferred",
2572 config: Config{
2573 NextProtos: []string{"foo", "bar", "baz"},
2574 },
2575 flags: []string{
2576 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2577 "-select-alpn", "foo",
2578 "-advertise-npn", "\x03foo\x03bar\x03baz",
2579 },
2580 expectedNextProto: "foo",
2581 expectedNextProtoType: alpn,
2582 resumeSession: true,
2583 })
2584 testCases = append(testCases, testCase{
2585 testType: serverTest,
2586 name: "ALPNServer-Preferred-Swapped",
2587 config: Config{
2588 NextProtos: []string{"foo", "bar", "baz"},
2589 Bugs: ProtocolBugs{
2590 SwapNPNAndALPN: true,
2591 },
2592 },
2593 flags: []string{
2594 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2595 "-select-alpn", "foo",
2596 "-advertise-npn", "\x03foo\x03bar\x03baz",
2597 },
2598 expectedNextProto: "foo",
2599 expectedNextProtoType: alpn,
2600 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04002601 })
Adam Langley38311732014-10-16 19:04:35 -07002602 // Resume with a corrupt ticket.
2603 testCases = append(testCases, testCase{
2604 testType: serverTest,
2605 name: "CorruptTicket",
2606 config: Config{
2607 Bugs: ProtocolBugs{
2608 CorruptTicket: true,
2609 },
2610 },
2611 resumeSession: true,
2612 flags: []string{"-expect-session-miss"},
2613 })
2614 // Resume with an oversized session id.
2615 testCases = append(testCases, testCase{
2616 testType: serverTest,
2617 name: "OversizedSessionId",
2618 config: Config{
2619 Bugs: ProtocolBugs{
2620 OversizedSessionId: true,
2621 },
2622 },
2623 resumeSession: true,
Adam Langley75712922014-10-10 16:23:43 -07002624 shouldFail: true,
Adam Langley38311732014-10-16 19:04:35 -07002625 expectedError: ":DECODE_ERROR:",
2626 })
David Benjaminca6c8262014-11-15 19:06:08 -05002627 // Basic DTLS-SRTP tests. Include fake profiles to ensure they
2628 // are ignored.
2629 testCases = append(testCases, testCase{
2630 protocol: dtls,
2631 name: "SRTP-Client",
2632 config: Config{
2633 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
2634 },
2635 flags: []string{
2636 "-srtp-profiles",
2637 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2638 },
2639 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2640 })
2641 testCases = append(testCases, testCase{
2642 protocol: dtls,
2643 testType: serverTest,
2644 name: "SRTP-Server",
2645 config: Config{
2646 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
2647 },
2648 flags: []string{
2649 "-srtp-profiles",
2650 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2651 },
2652 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2653 })
2654 // Test that the MKI is ignored.
2655 testCases = append(testCases, testCase{
2656 protocol: dtls,
2657 testType: serverTest,
2658 name: "SRTP-Server-IgnoreMKI",
2659 config: Config{
2660 SRTPProtectionProfiles: []uint16{SRTP_AES128_CM_HMAC_SHA1_80},
2661 Bugs: ProtocolBugs{
2662 SRTPMasterKeyIdentifer: "bogus",
2663 },
2664 },
2665 flags: []string{
2666 "-srtp-profiles",
2667 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2668 },
2669 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2670 })
2671 // Test that SRTP isn't negotiated on the server if there were
2672 // no matching profiles.
2673 testCases = append(testCases, testCase{
2674 protocol: dtls,
2675 testType: serverTest,
2676 name: "SRTP-Server-NoMatch",
2677 config: Config{
2678 SRTPProtectionProfiles: []uint16{100, 101, 102},
2679 },
2680 flags: []string{
2681 "-srtp-profiles",
2682 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2683 },
2684 expectedSRTPProtectionProfile: 0,
2685 })
2686 // Test that the server returning an invalid SRTP profile is
2687 // flagged as an error by the client.
2688 testCases = append(testCases, testCase{
2689 protocol: dtls,
2690 name: "SRTP-Client-NoMatch",
2691 config: Config{
2692 Bugs: ProtocolBugs{
2693 SendSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_32,
2694 },
2695 },
2696 flags: []string{
2697 "-srtp-profiles",
2698 "SRTP_AES128_CM_SHA1_80",
2699 },
2700 shouldFail: true,
2701 expectedError: ":BAD_SRTP_PROTECTION_PROFILE_LIST:",
2702 })
David Benjamin61f95272014-11-25 01:55:35 -05002703 // Test OCSP stapling and SCT list.
2704 testCases = append(testCases, testCase{
2705 name: "OCSPStapling",
2706 flags: []string{
2707 "-enable-ocsp-stapling",
2708 "-expect-ocsp-response",
2709 base64.StdEncoding.EncodeToString(testOCSPResponse),
2710 },
2711 })
2712 testCases = append(testCases, testCase{
2713 name: "SignedCertificateTimestampList",
2714 flags: []string{
2715 "-enable-signed-cert-timestamps",
2716 "-expect-signed-cert-timestamps",
2717 base64.StdEncoding.EncodeToString(testSCTList),
2718 },
2719 })
David Benjamine78bfde2014-09-06 12:45:15 -04002720}
2721
David Benjamin01fe8202014-09-24 15:21:44 -04002722func addResumptionVersionTests() {
David Benjamin01fe8202014-09-24 15:21:44 -04002723 for _, sessionVers := range tlsVersions {
David Benjamin01fe8202014-09-24 15:21:44 -04002724 for _, resumeVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05002725 protocols := []protocol{tls}
2726 if sessionVers.hasDTLS && resumeVers.hasDTLS {
2727 protocols = append(protocols, dtls)
David Benjaminbdf5e722014-11-11 00:52:15 -05002728 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002729 for _, protocol := range protocols {
2730 suffix := "-" + sessionVers.name + "-" + resumeVers.name
2731 if protocol == dtls {
2732 suffix += "-DTLS"
2733 }
2734
David Benjaminece3de92015-03-16 18:02:20 -04002735 if sessionVers.version == resumeVers.version {
2736 testCases = append(testCases, testCase{
2737 protocol: protocol,
2738 name: "Resume-Client" + suffix,
2739 resumeSession: true,
2740 config: Config{
2741 MaxVersion: sessionVers.version,
2742 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
David Benjamin8b8c0062014-11-23 02:47:52 -05002743 },
David Benjaminece3de92015-03-16 18:02:20 -04002744 expectedVersion: sessionVers.version,
2745 expectedResumeVersion: resumeVers.version,
2746 })
2747 } else {
2748 testCases = append(testCases, testCase{
2749 protocol: protocol,
2750 name: "Resume-Client-Mismatch" + suffix,
2751 resumeSession: true,
2752 config: Config{
2753 MaxVersion: sessionVers.version,
2754 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
David Benjamin8b8c0062014-11-23 02:47:52 -05002755 },
David Benjaminece3de92015-03-16 18:02:20 -04002756 expectedVersion: sessionVers.version,
2757 resumeConfig: &Config{
2758 MaxVersion: resumeVers.version,
2759 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2760 Bugs: ProtocolBugs{
2761 AllowSessionVersionMismatch: true,
2762 },
2763 },
2764 expectedResumeVersion: resumeVers.version,
2765 shouldFail: true,
2766 expectedError: ":OLD_SESSION_VERSION_NOT_RETURNED:",
2767 })
2768 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002769
2770 testCases = append(testCases, testCase{
2771 protocol: protocol,
2772 name: "Resume-Client-NoResume" + suffix,
2773 flags: []string{"-expect-session-miss"},
2774 resumeSession: true,
2775 config: Config{
2776 MaxVersion: sessionVers.version,
2777 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2778 },
2779 expectedVersion: sessionVers.version,
2780 resumeConfig: &Config{
2781 MaxVersion: resumeVers.version,
2782 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2783 },
2784 newSessionsOnResume: true,
2785 expectedResumeVersion: resumeVers.version,
2786 })
2787
2788 var flags []string
2789 if sessionVers.version != resumeVers.version {
2790 flags = append(flags, "-expect-session-miss")
2791 }
2792 testCases = append(testCases, testCase{
2793 protocol: protocol,
2794 testType: serverTest,
2795 name: "Resume-Server" + suffix,
2796 flags: flags,
2797 resumeSession: true,
2798 config: Config{
2799 MaxVersion: sessionVers.version,
2800 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2801 },
2802 expectedVersion: sessionVers.version,
2803 resumeConfig: &Config{
2804 MaxVersion: resumeVers.version,
2805 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2806 },
2807 expectedResumeVersion: resumeVers.version,
2808 })
2809 }
David Benjamin01fe8202014-09-24 15:21:44 -04002810 }
2811 }
David Benjaminece3de92015-03-16 18:02:20 -04002812
2813 testCases = append(testCases, testCase{
2814 name: "Resume-Client-CipherMismatch",
2815 resumeSession: true,
2816 config: Config{
2817 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2818 },
2819 resumeConfig: &Config{
2820 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2821 Bugs: ProtocolBugs{
2822 SendCipherSuite: TLS_RSA_WITH_AES_128_CBC_SHA,
2823 },
2824 },
2825 shouldFail: true,
2826 expectedError: ":OLD_SESSION_CIPHER_NOT_RETURNED:",
2827 })
David Benjamin01fe8202014-09-24 15:21:44 -04002828}
2829
Adam Langley2ae77d22014-10-28 17:29:33 -07002830func addRenegotiationTests() {
2831 testCases = append(testCases, testCase{
2832 testType: serverTest,
2833 name: "Renegotiate-Server",
2834 flags: []string{"-renegotiate"},
2835 shimWritesFirst: true,
2836 })
2837 testCases = append(testCases, testCase{
2838 testType: serverTest,
David Benjamincdea40c2015-03-19 14:09:43 -04002839 name: "Renegotiate-Server-Full",
2840 config: Config{
2841 Bugs: ProtocolBugs{
2842 NeverResumeOnRenego: true,
2843 },
2844 },
2845 flags: []string{"-renegotiate"},
2846 shimWritesFirst: true,
2847 })
2848 testCases = append(testCases, testCase{
2849 testType: serverTest,
Adam Langley2ae77d22014-10-28 17:29:33 -07002850 name: "Renegotiate-Server-EmptyExt",
2851 config: Config{
2852 Bugs: ProtocolBugs{
2853 EmptyRenegotiationInfo: true,
2854 },
2855 },
2856 flags: []string{"-renegotiate"},
2857 shimWritesFirst: true,
2858 shouldFail: true,
2859 expectedError: ":RENEGOTIATION_MISMATCH:",
2860 })
2861 testCases = append(testCases, testCase{
2862 testType: serverTest,
2863 name: "Renegotiate-Server-BadExt",
2864 config: Config{
2865 Bugs: ProtocolBugs{
2866 BadRenegotiationInfo: true,
2867 },
2868 },
2869 flags: []string{"-renegotiate"},
2870 shimWritesFirst: true,
2871 shouldFail: true,
2872 expectedError: ":RENEGOTIATION_MISMATCH:",
2873 })
David Benjaminca6554b2014-11-08 12:31:52 -05002874 testCases = append(testCases, testCase{
2875 testType: serverTest,
2876 name: "Renegotiate-Server-ClientInitiated",
2877 renegotiate: true,
2878 })
2879 testCases = append(testCases, testCase{
2880 testType: serverTest,
2881 name: "Renegotiate-Server-ClientInitiated-NoExt",
2882 renegotiate: true,
2883 config: Config{
2884 Bugs: ProtocolBugs{
2885 NoRenegotiationInfo: true,
2886 },
2887 },
2888 shouldFail: true,
2889 expectedError: ":UNSAFE_LEGACY_RENEGOTIATION_DISABLED:",
2890 })
2891 testCases = append(testCases, testCase{
2892 testType: serverTest,
2893 name: "Renegotiate-Server-ClientInitiated-NoExt-Allowed",
2894 renegotiate: true,
2895 config: Config{
2896 Bugs: ProtocolBugs{
2897 NoRenegotiationInfo: true,
2898 },
2899 },
2900 flags: []string{"-allow-unsafe-legacy-renegotiation"},
2901 })
David Benjaminb16346b2015-04-08 19:16:58 -04002902 testCases = append(testCases, testCase{
2903 testType: serverTest,
2904 name: "Renegotiate-Server-ClientInitiated-Forbidden",
2905 renegotiate: true,
2906 flags: []string{"-reject-peer-renegotiations"},
2907 shouldFail: true,
2908 expectedError: ":NO_RENEGOTIATION:",
2909 expectedLocalError: "remote error: no renegotiation",
2910 })
David Benjamin3c9746a2015-03-19 15:00:10 -04002911 // Regression test for CVE-2015-0291.
2912 testCases = append(testCases, testCase{
2913 testType: serverTest,
2914 name: "Renegotiate-Server-NoSignatureAlgorithms",
2915 config: Config{
2916 Bugs: ProtocolBugs{
2917 NeverResumeOnRenego: true,
2918 NoSignatureAlgorithmsOnRenego: true,
2919 },
2920 },
2921 flags: []string{"-renegotiate"},
2922 shimWritesFirst: true,
2923 })
Adam Langley2ae77d22014-10-28 17:29:33 -07002924 // TODO(agl): test the renegotiation info SCSV.
Adam Langleycf2d4f42014-10-28 19:06:14 -07002925 testCases = append(testCases, testCase{
2926 name: "Renegotiate-Client",
2927 renegotiate: true,
2928 })
2929 testCases = append(testCases, testCase{
David Benjamincdea40c2015-03-19 14:09:43 -04002930 name: "Renegotiate-Client-Full",
2931 config: Config{
2932 Bugs: ProtocolBugs{
2933 NeverResumeOnRenego: true,
2934 },
2935 },
2936 renegotiate: true,
2937 })
2938 testCases = append(testCases, testCase{
Adam Langleycf2d4f42014-10-28 19:06:14 -07002939 name: "Renegotiate-Client-EmptyExt",
2940 renegotiate: true,
2941 config: Config{
2942 Bugs: ProtocolBugs{
2943 EmptyRenegotiationInfo: true,
2944 },
2945 },
2946 shouldFail: true,
2947 expectedError: ":RENEGOTIATION_MISMATCH:",
2948 })
2949 testCases = append(testCases, testCase{
2950 name: "Renegotiate-Client-BadExt",
2951 renegotiate: true,
2952 config: Config{
2953 Bugs: ProtocolBugs{
2954 BadRenegotiationInfo: true,
2955 },
2956 },
2957 shouldFail: true,
2958 expectedError: ":RENEGOTIATION_MISMATCH:",
2959 })
2960 testCases = append(testCases, testCase{
2961 name: "Renegotiate-Client-SwitchCiphers",
2962 renegotiate: true,
2963 config: Config{
2964 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2965 },
2966 renegotiateCiphers: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2967 })
2968 testCases = append(testCases, testCase{
2969 name: "Renegotiate-Client-SwitchCiphers2",
2970 renegotiate: true,
2971 config: Config{
2972 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2973 },
2974 renegotiateCiphers: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2975 })
David Benjaminc44b1df2014-11-23 12:11:01 -05002976 testCases = append(testCases, testCase{
David Benjaminb16346b2015-04-08 19:16:58 -04002977 name: "Renegotiate-Client-Forbidden",
2978 renegotiate: true,
2979 flags: []string{"-reject-peer-renegotiations"},
2980 shouldFail: true,
2981 expectedError: ":NO_RENEGOTIATION:",
2982 expectedLocalError: "remote error: no renegotiation",
2983 })
2984 testCases = append(testCases, testCase{
David Benjaminc44b1df2014-11-23 12:11:01 -05002985 name: "Renegotiate-SameClientVersion",
2986 renegotiate: true,
2987 config: Config{
2988 MaxVersion: VersionTLS10,
2989 Bugs: ProtocolBugs{
2990 RequireSameRenegoClientVersion: true,
2991 },
2992 },
2993 })
Adam Langley2ae77d22014-10-28 17:29:33 -07002994}
2995
David Benjamin5e961c12014-11-07 01:48:35 -05002996func addDTLSReplayTests() {
2997 // Test that sequence number replays are detected.
2998 testCases = append(testCases, testCase{
2999 protocol: dtls,
3000 name: "DTLS-Replay",
3001 replayWrites: true,
3002 })
3003
3004 // Test the outgoing sequence number skipping by values larger
3005 // than the retransmit window.
3006 testCases = append(testCases, testCase{
3007 protocol: dtls,
3008 name: "DTLS-Replay-LargeGaps",
3009 config: Config{
3010 Bugs: ProtocolBugs{
3011 SequenceNumberIncrement: 127,
3012 },
3013 },
3014 replayWrites: true,
3015 })
3016}
3017
Feng Lu41aa3252014-11-21 22:47:56 -08003018func addFastRadioPaddingTests() {
David Benjamin1e29a6b2014-12-10 02:27:24 -05003019 testCases = append(testCases, testCase{
3020 protocol: tls,
3021 name: "FastRadio-Padding",
Feng Lu41aa3252014-11-21 22:47:56 -08003022 config: Config{
3023 Bugs: ProtocolBugs{
3024 RequireFastradioPadding: true,
3025 },
3026 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05003027 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08003028 })
David Benjamin1e29a6b2014-12-10 02:27:24 -05003029 testCases = append(testCases, testCase{
3030 protocol: dtls,
David Benjamin5f237bc2015-02-11 17:14:15 -05003031 name: "FastRadio-Padding-DTLS",
Feng Lu41aa3252014-11-21 22:47:56 -08003032 config: Config{
3033 Bugs: ProtocolBugs{
3034 RequireFastradioPadding: true,
3035 },
3036 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05003037 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08003038 })
3039}
3040
David Benjamin000800a2014-11-14 01:43:59 -05003041var testHashes = []struct {
3042 name string
3043 id uint8
3044}{
3045 {"SHA1", hashSHA1},
3046 {"SHA224", hashSHA224},
3047 {"SHA256", hashSHA256},
3048 {"SHA384", hashSHA384},
3049 {"SHA512", hashSHA512},
3050}
3051
3052func addSigningHashTests() {
3053 // Make sure each hash works. Include some fake hashes in the list and
3054 // ensure they're ignored.
3055 for _, hash := range testHashes {
3056 testCases = append(testCases, testCase{
3057 name: "SigningHash-ClientAuth-" + hash.name,
3058 config: Config{
3059 ClientAuth: RequireAnyClientCert,
3060 SignatureAndHashes: []signatureAndHash{
3061 {signatureRSA, 42},
3062 {signatureRSA, hash.id},
3063 {signatureRSA, 255},
3064 },
3065 },
3066 flags: []string{
3067 "-cert-file", rsaCertificateFile,
3068 "-key-file", rsaKeyFile,
3069 },
3070 })
3071
3072 testCases = append(testCases, testCase{
3073 testType: serverTest,
3074 name: "SigningHash-ServerKeyExchange-Sign-" + hash.name,
3075 config: Config{
3076 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
3077 SignatureAndHashes: []signatureAndHash{
3078 {signatureRSA, 42},
3079 {signatureRSA, hash.id},
3080 {signatureRSA, 255},
3081 },
3082 },
3083 })
3084 }
3085
3086 // Test that hash resolution takes the signature type into account.
3087 testCases = append(testCases, testCase{
3088 name: "SigningHash-ClientAuth-SignatureType",
3089 config: Config{
3090 ClientAuth: RequireAnyClientCert,
3091 SignatureAndHashes: []signatureAndHash{
3092 {signatureECDSA, hashSHA512},
3093 {signatureRSA, hashSHA384},
3094 {signatureECDSA, hashSHA1},
3095 },
3096 },
3097 flags: []string{
3098 "-cert-file", rsaCertificateFile,
3099 "-key-file", rsaKeyFile,
3100 },
3101 })
3102
3103 testCases = append(testCases, testCase{
3104 testType: serverTest,
3105 name: "SigningHash-ServerKeyExchange-SignatureType",
3106 config: Config{
3107 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
3108 SignatureAndHashes: []signatureAndHash{
3109 {signatureECDSA, hashSHA512},
3110 {signatureRSA, hashSHA384},
3111 {signatureECDSA, hashSHA1},
3112 },
3113 },
3114 })
3115
3116 // Test that, if the list is missing, the peer falls back to SHA-1.
3117 testCases = append(testCases, testCase{
3118 name: "SigningHash-ClientAuth-Fallback",
3119 config: Config{
3120 ClientAuth: RequireAnyClientCert,
3121 SignatureAndHashes: []signatureAndHash{
3122 {signatureRSA, hashSHA1},
3123 },
3124 Bugs: ProtocolBugs{
3125 NoSignatureAndHashes: true,
3126 },
3127 },
3128 flags: []string{
3129 "-cert-file", rsaCertificateFile,
3130 "-key-file", rsaKeyFile,
3131 },
3132 })
3133
3134 testCases = append(testCases, testCase{
3135 testType: serverTest,
3136 name: "SigningHash-ServerKeyExchange-Fallback",
3137 config: Config{
3138 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
3139 SignatureAndHashes: []signatureAndHash{
3140 {signatureRSA, hashSHA1},
3141 },
3142 Bugs: ProtocolBugs{
3143 NoSignatureAndHashes: true,
3144 },
3145 },
3146 })
David Benjamin72dc7832015-03-16 17:49:43 -04003147
3148 // Test that hash preferences are enforced. BoringSSL defaults to
3149 // rejecting MD5 signatures.
3150 testCases = append(testCases, testCase{
3151 testType: serverTest,
3152 name: "SigningHash-ClientAuth-Enforced",
3153 config: Config{
3154 Certificates: []Certificate{rsaCertificate},
3155 SignatureAndHashes: []signatureAndHash{
3156 {signatureRSA, hashMD5},
3157 // Advertise SHA-1 so the handshake will
3158 // proceed, but the shim's preferences will be
3159 // ignored in CertificateVerify generation, so
3160 // MD5 will be chosen.
3161 {signatureRSA, hashSHA1},
3162 },
3163 Bugs: ProtocolBugs{
3164 IgnorePeerSignatureAlgorithmPreferences: true,
3165 },
3166 },
3167 flags: []string{"-require-any-client-certificate"},
3168 shouldFail: true,
3169 expectedError: ":WRONG_SIGNATURE_TYPE:",
3170 })
3171
3172 testCases = append(testCases, testCase{
3173 name: "SigningHash-ServerKeyExchange-Enforced",
3174 config: Config{
3175 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
3176 SignatureAndHashes: []signatureAndHash{
3177 {signatureRSA, hashMD5},
3178 },
3179 Bugs: ProtocolBugs{
3180 IgnorePeerSignatureAlgorithmPreferences: true,
3181 },
3182 },
3183 shouldFail: true,
3184 expectedError: ":WRONG_SIGNATURE_TYPE:",
3185 })
David Benjamin000800a2014-11-14 01:43:59 -05003186}
3187
David Benjamin83f90402015-01-27 01:09:43 -05003188// timeouts is the retransmit schedule for BoringSSL. It doubles and
3189// caps at 60 seconds. On the 13th timeout, it gives up.
3190var timeouts = []time.Duration{
3191 1 * time.Second,
3192 2 * time.Second,
3193 4 * time.Second,
3194 8 * time.Second,
3195 16 * time.Second,
3196 32 * time.Second,
3197 60 * time.Second,
3198 60 * time.Second,
3199 60 * time.Second,
3200 60 * time.Second,
3201 60 * time.Second,
3202 60 * time.Second,
3203 60 * time.Second,
3204}
3205
3206func addDTLSRetransmitTests() {
3207 // Test that this is indeed the timeout schedule. Stress all
3208 // four patterns of handshake.
3209 for i := 1; i < len(timeouts); i++ {
3210 number := strconv.Itoa(i)
3211 testCases = append(testCases, testCase{
3212 protocol: dtls,
3213 name: "DTLS-Retransmit-Client-" + number,
3214 config: Config{
3215 Bugs: ProtocolBugs{
3216 TimeoutSchedule: timeouts[:i],
3217 },
3218 },
3219 resumeSession: true,
3220 flags: []string{"-async"},
3221 })
3222 testCases = append(testCases, testCase{
3223 protocol: dtls,
3224 testType: serverTest,
3225 name: "DTLS-Retransmit-Server-" + number,
3226 config: Config{
3227 Bugs: ProtocolBugs{
3228 TimeoutSchedule: timeouts[:i],
3229 },
3230 },
3231 resumeSession: true,
3232 flags: []string{"-async"},
3233 })
3234 }
3235
3236 // Test that exceeding the timeout schedule hits a read
3237 // timeout.
3238 testCases = append(testCases, testCase{
3239 protocol: dtls,
3240 name: "DTLS-Retransmit-Timeout",
3241 config: Config{
3242 Bugs: ProtocolBugs{
3243 TimeoutSchedule: timeouts,
3244 },
3245 },
3246 resumeSession: true,
3247 flags: []string{"-async"},
3248 shouldFail: true,
3249 expectedError: ":READ_TIMEOUT_EXPIRED:",
3250 })
3251
3252 // Test that timeout handling has a fudge factor, due to API
3253 // problems.
3254 testCases = append(testCases, testCase{
3255 protocol: dtls,
3256 name: "DTLS-Retransmit-Fudge",
3257 config: Config{
3258 Bugs: ProtocolBugs{
3259 TimeoutSchedule: []time.Duration{
3260 timeouts[0] - 10*time.Millisecond,
3261 },
3262 },
3263 },
3264 resumeSession: true,
3265 flags: []string{"-async"},
3266 })
David Benjamin7eaab4c2015-03-02 19:01:16 -05003267
3268 // Test that the final Finished retransmitting isn't
3269 // duplicated if the peer badly fragments everything.
3270 testCases = append(testCases, testCase{
3271 testType: serverTest,
3272 protocol: dtls,
3273 name: "DTLS-Retransmit-Fragmented",
3274 config: Config{
3275 Bugs: ProtocolBugs{
3276 TimeoutSchedule: []time.Duration{timeouts[0]},
3277 MaxHandshakeRecordLength: 2,
3278 },
3279 },
3280 flags: []string{"-async"},
3281 })
David Benjamin83f90402015-01-27 01:09:43 -05003282}
3283
David Benjaminc565ebb2015-04-03 04:06:36 -04003284func addExportKeyingMaterialTests() {
3285 for _, vers := range tlsVersions {
3286 if vers.version == VersionSSL30 {
3287 continue
3288 }
3289 testCases = append(testCases, testCase{
3290 name: "ExportKeyingMaterial-" + vers.name,
3291 config: Config{
3292 MaxVersion: vers.version,
3293 },
3294 exportKeyingMaterial: 1024,
3295 exportLabel: "label",
3296 exportContext: "context",
3297 useExportContext: true,
3298 })
3299 testCases = append(testCases, testCase{
3300 name: "ExportKeyingMaterial-NoContext-" + vers.name,
3301 config: Config{
3302 MaxVersion: vers.version,
3303 },
3304 exportKeyingMaterial: 1024,
3305 })
3306 testCases = append(testCases, testCase{
3307 name: "ExportKeyingMaterial-EmptyContext-" + vers.name,
3308 config: Config{
3309 MaxVersion: vers.version,
3310 },
3311 exportKeyingMaterial: 1024,
3312 useExportContext: true,
3313 })
3314 testCases = append(testCases, testCase{
3315 name: "ExportKeyingMaterial-Small-" + vers.name,
3316 config: Config{
3317 MaxVersion: vers.version,
3318 },
3319 exportKeyingMaterial: 1,
3320 exportLabel: "label",
3321 exportContext: "context",
3322 useExportContext: true,
3323 })
3324 }
3325 testCases = append(testCases, testCase{
3326 name: "ExportKeyingMaterial-SSL3",
3327 config: Config{
3328 MaxVersion: VersionSSL30,
3329 },
3330 exportKeyingMaterial: 1024,
3331 exportLabel: "label",
3332 exportContext: "context",
3333 useExportContext: true,
3334 shouldFail: true,
3335 expectedError: "failed to export keying material",
3336 })
3337}
3338
David Benjamin884fdf12014-08-02 15:28:23 -04003339func worker(statusChan chan statusMsg, c chan *testCase, buildDir string, wg *sync.WaitGroup) {
Adam Langley95c29f32014-06-20 12:00:00 -07003340 defer wg.Done()
3341
3342 for test := range c {
Adam Langley69a01602014-11-17 17:26:55 -08003343 var err error
3344
3345 if *mallocTest < 0 {
3346 statusChan <- statusMsg{test: test, started: true}
3347 err = runTest(test, buildDir, -1)
3348 } else {
3349 for mallocNumToFail := int64(*mallocTest); ; mallocNumToFail++ {
3350 statusChan <- statusMsg{test: test, started: true}
3351 if err = runTest(test, buildDir, mallocNumToFail); err != errMoreMallocs {
3352 if err != nil {
3353 fmt.Printf("\n\nmalloc test failed at %d: %s\n", mallocNumToFail, err)
3354 }
3355 break
3356 }
3357 }
3358 }
Adam Langley95c29f32014-06-20 12:00:00 -07003359 statusChan <- statusMsg{test: test, err: err}
3360 }
3361}
3362
3363type statusMsg struct {
3364 test *testCase
3365 started bool
3366 err error
3367}
3368
David Benjamin5f237bc2015-02-11 17:14:15 -05003369func statusPrinter(doneChan chan *testOutput, statusChan chan statusMsg, total int) {
Adam Langley95c29f32014-06-20 12:00:00 -07003370 var started, done, failed, lineLen int
Adam Langley95c29f32014-06-20 12:00:00 -07003371
David Benjamin5f237bc2015-02-11 17:14:15 -05003372 testOutput := newTestOutput()
Adam Langley95c29f32014-06-20 12:00:00 -07003373 for msg := range statusChan {
David Benjamin5f237bc2015-02-11 17:14:15 -05003374 if !*pipe {
3375 // Erase the previous status line.
David Benjamin87c8a642015-02-21 01:54:29 -05003376 var erase string
3377 for i := 0; i < lineLen; i++ {
3378 erase += "\b \b"
3379 }
3380 fmt.Print(erase)
David Benjamin5f237bc2015-02-11 17:14:15 -05003381 }
3382
Adam Langley95c29f32014-06-20 12:00:00 -07003383 if msg.started {
3384 started++
3385 } else {
3386 done++
David Benjamin5f237bc2015-02-11 17:14:15 -05003387
3388 if msg.err != nil {
3389 fmt.Printf("FAILED (%s)\n%s\n", msg.test.name, msg.err)
3390 failed++
3391 testOutput.addResult(msg.test.name, "FAIL")
3392 } else {
3393 if *pipe {
3394 // Print each test instead of a status line.
3395 fmt.Printf("PASSED (%s)\n", msg.test.name)
3396 }
3397 testOutput.addResult(msg.test.name, "PASS")
3398 }
Adam Langley95c29f32014-06-20 12:00:00 -07003399 }
3400
David Benjamin5f237bc2015-02-11 17:14:15 -05003401 if !*pipe {
3402 // Print a new status line.
3403 line := fmt.Sprintf("%d/%d/%d/%d", failed, done, started, total)
3404 lineLen = len(line)
3405 os.Stdout.WriteString(line)
Adam Langley95c29f32014-06-20 12:00:00 -07003406 }
Adam Langley95c29f32014-06-20 12:00:00 -07003407 }
David Benjamin5f237bc2015-02-11 17:14:15 -05003408
3409 doneChan <- testOutput
Adam Langley95c29f32014-06-20 12:00:00 -07003410}
3411
3412func main() {
3413 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 -04003414 var flagNumWorkers *int = flag.Int("num-workers", runtime.NumCPU(), "The number of workers to run in parallel.")
David Benjamin884fdf12014-08-02 15:28:23 -04003415 var flagBuildDir *string = flag.String("build-dir", "../../../build", "The build directory to run the shim from.")
Adam Langley95c29f32014-06-20 12:00:00 -07003416
3417 flag.Parse()
3418
3419 addCipherSuiteTests()
3420 addBadECDSASignatureTests()
Adam Langley80842bd2014-06-20 12:00:00 -07003421 addCBCPaddingTests()
Kenny Root7fdeaf12014-08-05 15:23:37 -07003422 addCBCSplittingTests()
David Benjamin636293b2014-07-08 17:59:18 -04003423 addClientAuthTests()
Adam Langley524e7172015-02-20 16:04:00 -08003424 addDDoSCallbackTests()
David Benjamin7e2e6cf2014-08-07 17:44:24 -04003425 addVersionNegotiationTests()
David Benjaminaccb4542014-12-12 23:44:33 -05003426 addMinimumVersionTests()
David Benjamin5c24a1d2014-08-31 00:59:27 -04003427 addD5BugTests()
David Benjamine78bfde2014-09-06 12:45:15 -04003428 addExtensionTests()
David Benjamin01fe8202014-09-24 15:21:44 -04003429 addResumptionVersionTests()
Adam Langley75712922014-10-10 16:23:43 -07003430 addExtendedMasterSecretTests()
Adam Langley2ae77d22014-10-28 17:29:33 -07003431 addRenegotiationTests()
David Benjamin5e961c12014-11-07 01:48:35 -05003432 addDTLSReplayTests()
David Benjamin000800a2014-11-14 01:43:59 -05003433 addSigningHashTests()
Feng Lu41aa3252014-11-21 22:47:56 -08003434 addFastRadioPaddingTests()
David Benjamin83f90402015-01-27 01:09:43 -05003435 addDTLSRetransmitTests()
David Benjaminc565ebb2015-04-03 04:06:36 -04003436 addExportKeyingMaterialTests()
David Benjamin43ec06f2014-08-05 02:28:57 -04003437 for _, async := range []bool{false, true} {
3438 for _, splitHandshake := range []bool{false, true} {
David Benjamin6fd297b2014-08-11 18:43:38 -04003439 for _, protocol := range []protocol{tls, dtls} {
3440 addStateMachineCoverageTests(async, splitHandshake, protocol)
3441 }
David Benjamin43ec06f2014-08-05 02:28:57 -04003442 }
3443 }
Adam Langley95c29f32014-06-20 12:00:00 -07003444
3445 var wg sync.WaitGroup
3446
David Benjamin2bc8e6f2014-08-02 15:22:37 -04003447 numWorkers := *flagNumWorkers
Adam Langley95c29f32014-06-20 12:00:00 -07003448
3449 statusChan := make(chan statusMsg, numWorkers)
3450 testChan := make(chan *testCase, numWorkers)
David Benjamin5f237bc2015-02-11 17:14:15 -05003451 doneChan := make(chan *testOutput)
Adam Langley95c29f32014-06-20 12:00:00 -07003452
David Benjamin025b3d32014-07-01 19:53:04 -04003453 go statusPrinter(doneChan, statusChan, len(testCases))
Adam Langley95c29f32014-06-20 12:00:00 -07003454
3455 for i := 0; i < numWorkers; i++ {
3456 wg.Add(1)
David Benjamin884fdf12014-08-02 15:28:23 -04003457 go worker(statusChan, testChan, *flagBuildDir, &wg)
Adam Langley95c29f32014-06-20 12:00:00 -07003458 }
3459
David Benjamin025b3d32014-07-01 19:53:04 -04003460 for i := range testCases {
3461 if len(*flagTest) == 0 || *flagTest == testCases[i].name {
3462 testChan <- &testCases[i]
Adam Langley95c29f32014-06-20 12:00:00 -07003463 }
3464 }
3465
3466 close(testChan)
3467 wg.Wait()
3468 close(statusChan)
David Benjamin5f237bc2015-02-11 17:14:15 -05003469 testOutput := <-doneChan
Adam Langley95c29f32014-06-20 12:00:00 -07003470
3471 fmt.Printf("\n")
David Benjamin5f237bc2015-02-11 17:14:15 -05003472
3473 if *jsonOutput != "" {
3474 if err := testOutput.writeTo(*jsonOutput); err != nil {
3475 fmt.Fprintf(os.Stderr, "Error: %s\n", err)
3476 }
3477 }
David Benjamin2ab7a862015-04-04 17:02:18 -04003478
3479 if !testOutput.allPassed {
3480 os.Exit(1)
3481 }
Adam Langley95c29f32014-06-20 12:00:00 -07003482}