blob: 40d8e7db3dd0a3870dd9274705a097ef81a39cab [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 Langleya7997f12015-05-14 17:38:50 -070014 "math/big"
Adam Langley95c29f32014-06-20 12:00:00 -070015 "net"
16 "os"
17 "os/exec"
David Benjamin884fdf12014-08-02 15:28:23 -040018 "path"
David Benjamin2bc8e6f2014-08-02 15:22:37 -040019 "runtime"
Adam Langley69a01602014-11-17 17:26:55 -080020 "strconv"
Adam Langley95c29f32014-06-20 12:00:00 -070021 "strings"
22 "sync"
23 "syscall"
David Benjamin83f90402015-01-27 01:09:43 -050024 "time"
Adam Langley95c29f32014-06-20 12:00:00 -070025)
26
Adam Langley69a01602014-11-17 17:26:55 -080027var (
David Benjamin5f237bc2015-02-11 17:14:15 -050028 useValgrind = flag.Bool("valgrind", false, "If true, run code under valgrind")
29 useGDB = flag.Bool("gdb", false, "If true, run BoringSSL code under gdb")
30 flagDebug = flag.Bool("debug", false, "Hexdump the contents of the connection")
31 mallocTest = flag.Int64("malloc-test", -1, "If non-negative, run each test with each malloc in turn failing from the given number onwards.")
32 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.")
33 jsonOutput = flag.String("json-output", "", "The file to output JSON results to.")
34 pipe = flag.Bool("pipe", false, "If true, print status output suitable for piping into another program.")
Adam Langley69a01602014-11-17 17:26:55 -080035)
Adam Langley95c29f32014-06-20 12:00:00 -070036
David Benjamin025b3d32014-07-01 19:53:04 -040037const (
38 rsaCertificateFile = "cert.pem"
39 ecdsaCertificateFile = "ecdsa_cert.pem"
40)
41
42const (
David Benjamina08e49d2014-08-24 01:46:07 -040043 rsaKeyFile = "key.pem"
44 ecdsaKeyFile = "ecdsa_key.pem"
45 channelIDKeyFile = "channel_id_key.pem"
David Benjamin025b3d32014-07-01 19:53:04 -040046)
47
Adam Langley95c29f32014-06-20 12:00:00 -070048var rsaCertificate, ecdsaCertificate Certificate
David Benjamina08e49d2014-08-24 01:46:07 -040049var channelIDKey *ecdsa.PrivateKey
50var channelIDBytes []byte
Adam Langley95c29f32014-06-20 12:00:00 -070051
David Benjamin61f95272014-11-25 01:55:35 -050052var testOCSPResponse = []byte{1, 2, 3, 4}
53var testSCTList = []byte{5, 6, 7, 8}
54
Adam Langley95c29f32014-06-20 12:00:00 -070055func initCertificates() {
56 var err error
David Benjamin025b3d32014-07-01 19:53:04 -040057 rsaCertificate, err = LoadX509KeyPair(rsaCertificateFile, rsaKeyFile)
Adam Langley95c29f32014-06-20 12:00:00 -070058 if err != nil {
59 panic(err)
60 }
David Benjamin61f95272014-11-25 01:55:35 -050061 rsaCertificate.OCSPStaple = testOCSPResponse
62 rsaCertificate.SignedCertificateTimestampList = testSCTList
Adam Langley95c29f32014-06-20 12:00:00 -070063
David Benjamin025b3d32014-07-01 19:53:04 -040064 ecdsaCertificate, err = LoadX509KeyPair(ecdsaCertificateFile, ecdsaKeyFile)
Adam Langley95c29f32014-06-20 12:00:00 -070065 if err != nil {
66 panic(err)
67 }
David Benjamin61f95272014-11-25 01:55:35 -050068 ecdsaCertificate.OCSPStaple = testOCSPResponse
69 ecdsaCertificate.SignedCertificateTimestampList = testSCTList
David Benjamina08e49d2014-08-24 01:46:07 -040070
71 channelIDPEMBlock, err := ioutil.ReadFile(channelIDKeyFile)
72 if err != nil {
73 panic(err)
74 }
75 channelIDDERBlock, _ := pem.Decode(channelIDPEMBlock)
76 if channelIDDERBlock.Type != "EC PRIVATE KEY" {
77 panic("bad key type")
78 }
79 channelIDKey, err = x509.ParseECPrivateKey(channelIDDERBlock.Bytes)
80 if err != nil {
81 panic(err)
82 }
83 if channelIDKey.Curve != elliptic.P256() {
84 panic("bad curve")
85 }
86
87 channelIDBytes = make([]byte, 64)
88 writeIntPadded(channelIDBytes[:32], channelIDKey.X)
89 writeIntPadded(channelIDBytes[32:], channelIDKey.Y)
Adam Langley95c29f32014-06-20 12:00:00 -070090}
91
92var certificateOnce sync.Once
93
94func getRSACertificate() Certificate {
95 certificateOnce.Do(initCertificates)
96 return rsaCertificate
97}
98
99func getECDSACertificate() Certificate {
100 certificateOnce.Do(initCertificates)
101 return ecdsaCertificate
102}
103
David Benjamin025b3d32014-07-01 19:53:04 -0400104type testType int
105
106const (
107 clientTest testType = iota
108 serverTest
109)
110
David Benjamin6fd297b2014-08-11 18:43:38 -0400111type protocol int
112
113const (
114 tls protocol = iota
115 dtls
116)
117
David Benjaminfc7b0862014-09-06 13:21:53 -0400118const (
119 alpn = 1
120 npn = 2
121)
122
Adam Langley95c29f32014-06-20 12:00:00 -0700123type testCase struct {
David Benjamin025b3d32014-07-01 19:53:04 -0400124 testType testType
David Benjamin6fd297b2014-08-11 18:43:38 -0400125 protocol protocol
Adam Langley95c29f32014-06-20 12:00:00 -0700126 name string
127 config Config
128 shouldFail bool
129 expectedError string
Adam Langleyac61fa32014-06-23 12:03:11 -0700130 // expectedLocalError, if not empty, contains a substring that must be
131 // found in the local error.
132 expectedLocalError string
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400133 // expectedVersion, if non-zero, specifies the TLS version that must be
134 // negotiated.
135 expectedVersion uint16
David Benjamin01fe8202014-09-24 15:21:44 -0400136 // expectedResumeVersion, if non-zero, specifies the TLS version that
137 // must be negotiated on resumption. If zero, expectedVersion is used.
138 expectedResumeVersion uint16
David Benjamin90da8c82015-04-20 14:57:57 -0400139 // expectedCipher, if non-zero, specifies the TLS cipher suite that
140 // should be negotiated.
141 expectedCipher uint16
David Benjamina08e49d2014-08-24 01:46:07 -0400142 // expectChannelID controls whether the connection should have
143 // negotiated a Channel ID with channelIDKey.
144 expectChannelID bool
David Benjaminae2888f2014-09-06 12:58:58 -0400145 // expectedNextProto controls whether the connection should
146 // negotiate a next protocol via NPN or ALPN.
147 expectedNextProto string
David Benjaminfc7b0862014-09-06 13:21:53 -0400148 // expectedNextProtoType, if non-zero, is the expected next
149 // protocol negotiation mechanism.
150 expectedNextProtoType int
David Benjaminca6c8262014-11-15 19:06:08 -0500151 // expectedSRTPProtectionProfile is the DTLS-SRTP profile that
152 // should be negotiated. If zero, none should be negotiated.
153 expectedSRTPProtectionProfile uint16
Adam Langley80842bd2014-06-20 12:00:00 -0700154 // messageLen is the length, in bytes, of the test message that will be
155 // sent.
156 messageLen int
David Benjamin025b3d32014-07-01 19:53:04 -0400157 // certFile is the path to the certificate to use for the server.
158 certFile string
159 // keyFile is the path to the private key to use for the server.
160 keyFile string
David Benjamin1d5c83e2014-07-22 19:20:02 -0400161 // resumeSession controls whether a second connection should be tested
David Benjamin01fe8202014-09-24 15:21:44 -0400162 // which attempts to resume the first session.
David Benjamin1d5c83e2014-07-22 19:20:02 -0400163 resumeSession bool
David Benjamin01fe8202014-09-24 15:21:44 -0400164 // resumeConfig, if not nil, points to a Config to be used on
David Benjaminfe8eb9a2014-11-17 03:19:02 -0500165 // resumption. Unless newSessionsOnResume is set,
166 // SessionTicketKey, ServerSessionCache, and
167 // ClientSessionCache are copied from the initial connection's
168 // config. If nil, the initial connection's config is used.
David Benjamin01fe8202014-09-24 15:21:44 -0400169 resumeConfig *Config
David Benjaminfe8eb9a2014-11-17 03:19:02 -0500170 // newSessionsOnResume, if true, will cause resumeConfig to
171 // use a different session resumption context.
172 newSessionsOnResume bool
David Benjamin98e882e2014-08-08 13:24:34 -0400173 // sendPrefix sends a prefix on the socket before actually performing a
174 // handshake.
175 sendPrefix string
David Benjamine58c4f52014-08-24 03:47:07 -0400176 // shimWritesFirst controls whether the shim sends an initial "hello"
177 // message before doing a roundtrip with the runner.
178 shimWritesFirst bool
Adam Langleycf2d4f42014-10-28 19:06:14 -0700179 // renegotiate indicates the the connection should be renegotiated
180 // during the exchange.
181 renegotiate bool
182 // renegotiateCiphers is a list of ciphersuite ids that will be
183 // switched in just before renegotiation.
184 renegotiateCiphers []uint16
David Benjamin5e961c12014-11-07 01:48:35 -0500185 // replayWrites, if true, configures the underlying transport
186 // to replay every write it makes in DTLS tests.
187 replayWrites bool
David Benjamin5fa3eba2015-01-22 16:35:40 -0500188 // damageFirstWrite, if true, configures the underlying transport to
189 // damage the final byte of the first application data write.
190 damageFirstWrite bool
David Benjaminc565ebb2015-04-03 04:06:36 -0400191 // exportKeyingMaterial, if non-zero, configures the test to exchange
192 // keying material and verify they match.
193 exportKeyingMaterial int
194 exportLabel string
195 exportContext string
196 useExportContext bool
David Benjamin325b5c32014-07-01 19:40:31 -0400197 // flags, if not empty, contains a list of command-line flags that will
198 // be passed to the shim program.
199 flags []string
Adam Langley95c29f32014-06-20 12:00:00 -0700200}
201
David Benjamin025b3d32014-07-01 19:53:04 -0400202var testCases = []testCase{
Adam Langley95c29f32014-06-20 12:00:00 -0700203 {
204 name: "BadRSASignature",
205 config: Config{
206 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
207 Bugs: ProtocolBugs{
208 InvalidSKXSignature: true,
209 },
210 },
211 shouldFail: true,
David Benjamin25f08462015-04-15 16:13:49 -0400212 expectedError: ":BAD_SIGNATURE:",
Adam Langley95c29f32014-06-20 12:00:00 -0700213 },
214 {
215 name: "BadECDSASignature",
216 config: Config{
217 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
218 Bugs: ProtocolBugs{
219 InvalidSKXSignature: true,
220 },
221 Certificates: []Certificate{getECDSACertificate()},
222 },
223 shouldFail: true,
224 expectedError: ":BAD_SIGNATURE:",
225 },
226 {
227 name: "BadECDSACurve",
228 config: Config{
229 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
230 Bugs: ProtocolBugs{
231 InvalidSKXCurve: true,
232 },
233 Certificates: []Certificate{getECDSACertificate()},
234 },
235 shouldFail: true,
236 expectedError: ":WRONG_CURVE:",
237 },
Adam Langleyac61fa32014-06-23 12:03:11 -0700238 {
David Benjamina8e3e0e2014-08-06 22:11:10 -0400239 testType: serverTest,
240 name: "BadRSAVersion",
241 config: Config{
242 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
243 Bugs: ProtocolBugs{
244 RsaClientKeyExchangeVersion: VersionTLS11,
245 },
246 },
247 shouldFail: true,
248 expectedError: ":DECRYPTION_FAILED_OR_BAD_RECORD_MAC:",
249 },
250 {
David Benjamin325b5c32014-07-01 19:40:31 -0400251 name: "NoFallbackSCSV",
Adam Langleyac61fa32014-06-23 12:03:11 -0700252 config: Config{
253 Bugs: ProtocolBugs{
254 FailIfNotFallbackSCSV: true,
255 },
256 },
257 shouldFail: true,
258 expectedLocalError: "no fallback SCSV found",
259 },
David Benjamin325b5c32014-07-01 19:40:31 -0400260 {
David Benjamin2a0c4962014-08-22 23:46:35 -0400261 name: "SendFallbackSCSV",
David Benjamin325b5c32014-07-01 19:40:31 -0400262 config: Config{
263 Bugs: ProtocolBugs{
264 FailIfNotFallbackSCSV: true,
265 },
266 },
267 flags: []string{"-fallback-scsv"},
268 },
David Benjamin197b3ab2014-07-02 18:37:33 -0400269 {
David Benjamin7b030512014-07-08 17:30:11 -0400270 name: "ClientCertificateTypes",
271 config: Config{
272 ClientAuth: RequestClientCert,
273 ClientCertificateTypes: []byte{
274 CertTypeDSSSign,
275 CertTypeRSASign,
276 CertTypeECDSASign,
277 },
278 },
David Benjamin2561dc32014-08-24 01:25:27 -0400279 flags: []string{
280 "-expect-certificate-types",
281 base64.StdEncoding.EncodeToString([]byte{
282 CertTypeDSSSign,
283 CertTypeRSASign,
284 CertTypeECDSASign,
285 }),
286 },
David Benjamin7b030512014-07-08 17:30:11 -0400287 },
David Benjamin636293b2014-07-08 17:59:18 -0400288 {
289 name: "NoClientCertificate",
290 config: Config{
291 ClientAuth: RequireAnyClientCert,
292 },
293 shouldFail: true,
294 expectedLocalError: "client didn't provide a certificate",
295 },
David Benjamin1c375dd2014-07-12 00:48:23 -0400296 {
297 name: "UnauthenticatedECDH",
298 config: Config{
299 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
300 Bugs: ProtocolBugs{
301 UnauthenticatedECDH: true,
302 },
303 },
304 shouldFail: true,
David Benjamine8f3d662014-07-12 01:10:19 -0400305 expectedError: ":UNEXPECTED_MESSAGE:",
David Benjamin1c375dd2014-07-12 00:48:23 -0400306 },
David Benjamin9c651c92014-07-12 13:27:45 -0400307 {
David Benjamindcd979f2015-04-20 18:26:52 -0400308 name: "SkipCertificateStatus",
309 config: Config{
310 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
311 Bugs: ProtocolBugs{
312 SkipCertificateStatus: true,
313 },
314 },
315 flags: []string{
316 "-enable-ocsp-stapling",
317 },
318 },
319 {
David Benjamin9c651c92014-07-12 13:27:45 -0400320 name: "SkipServerKeyExchange",
321 config: Config{
322 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
323 Bugs: ProtocolBugs{
324 SkipServerKeyExchange: true,
325 },
326 },
327 shouldFail: true,
328 expectedError: ":UNEXPECTED_MESSAGE:",
329 },
David Benjamin1f5f62b2014-07-12 16:18:02 -0400330 {
David Benjamina0e52232014-07-19 17:39:58 -0400331 name: "SkipChangeCipherSpec-Client",
332 config: Config{
333 Bugs: ProtocolBugs{
334 SkipChangeCipherSpec: true,
335 },
336 },
337 shouldFail: true,
David Benjamin86271ee2014-07-21 16:14:03 -0400338 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
David Benjamina0e52232014-07-19 17:39:58 -0400339 },
340 {
341 testType: serverTest,
342 name: "SkipChangeCipherSpec-Server",
343 config: Config{
344 Bugs: ProtocolBugs{
345 SkipChangeCipherSpec: true,
346 },
347 },
348 shouldFail: true,
David Benjamin86271ee2014-07-21 16:14:03 -0400349 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
David Benjamina0e52232014-07-19 17:39:58 -0400350 },
David Benjamin42be6452014-07-21 14:50:23 -0400351 {
352 testType: serverTest,
353 name: "SkipChangeCipherSpec-Server-NPN",
354 config: Config{
355 NextProtos: []string{"bar"},
356 Bugs: ProtocolBugs{
357 SkipChangeCipherSpec: true,
358 },
359 },
360 flags: []string{
361 "-advertise-npn", "\x03foo\x03bar\x03baz",
362 },
363 shouldFail: true,
David Benjamin86271ee2014-07-21 16:14:03 -0400364 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
365 },
366 {
367 name: "FragmentAcrossChangeCipherSpec-Client",
368 config: Config{
369 Bugs: ProtocolBugs{
370 FragmentAcrossChangeCipherSpec: true,
371 },
372 },
373 shouldFail: true,
374 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
375 },
376 {
377 testType: serverTest,
378 name: "FragmentAcrossChangeCipherSpec-Server",
379 config: Config{
380 Bugs: ProtocolBugs{
381 FragmentAcrossChangeCipherSpec: true,
382 },
383 },
384 shouldFail: true,
385 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
386 },
387 {
388 testType: serverTest,
389 name: "FragmentAcrossChangeCipherSpec-Server-NPN",
390 config: Config{
391 NextProtos: []string{"bar"},
392 Bugs: ProtocolBugs{
393 FragmentAcrossChangeCipherSpec: true,
394 },
395 },
396 flags: []string{
397 "-advertise-npn", "\x03foo\x03bar\x03baz",
398 },
399 shouldFail: true,
400 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
David Benjamin42be6452014-07-21 14:50:23 -0400401 },
David Benjaminf3ec83d2014-07-21 22:42:34 -0400402 {
403 testType: serverTest,
David Benjamin3fd1fbd2015-02-03 16:07:32 -0500404 name: "Alert",
405 config: Config{
406 Bugs: ProtocolBugs{
407 SendSpuriousAlert: alertRecordOverflow,
408 },
409 },
410 shouldFail: true,
411 expectedError: ":TLSV1_ALERT_RECORD_OVERFLOW:",
412 },
413 {
414 protocol: dtls,
415 testType: serverTest,
416 name: "Alert-DTLS",
417 config: Config{
418 Bugs: ProtocolBugs{
419 SendSpuriousAlert: alertRecordOverflow,
420 },
421 },
422 shouldFail: true,
423 expectedError: ":TLSV1_ALERT_RECORD_OVERFLOW:",
424 },
425 {
426 testType: serverTest,
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400427 name: "FragmentAlert",
428 config: Config{
429 Bugs: ProtocolBugs{
David Benjaminca6c8262014-11-15 19:06:08 -0500430 FragmentAlert: true,
David Benjamin3fd1fbd2015-02-03 16:07:32 -0500431 SendSpuriousAlert: alertRecordOverflow,
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400432 },
433 },
434 shouldFail: true,
435 expectedError: ":BAD_ALERT:",
436 },
437 {
David Benjamin0ea8dda2015-01-31 20:33:40 -0500438 protocol: dtls,
439 testType: serverTest,
440 name: "FragmentAlert-DTLS",
441 config: Config{
442 Bugs: ProtocolBugs{
443 FragmentAlert: true,
David Benjamin3fd1fbd2015-02-03 16:07:32 -0500444 SendSpuriousAlert: alertRecordOverflow,
David Benjamin0ea8dda2015-01-31 20:33:40 -0500445 },
446 },
447 shouldFail: true,
448 expectedError: ":BAD_ALERT:",
449 },
450 {
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400451 testType: serverTest,
David Benjaminf3ec83d2014-07-21 22:42:34 -0400452 name: "EarlyChangeCipherSpec-server-1",
453 config: Config{
454 Bugs: ProtocolBugs{
455 EarlyChangeCipherSpec: 1,
456 },
457 },
458 shouldFail: true,
459 expectedError: ":CCS_RECEIVED_EARLY:",
460 },
461 {
462 testType: serverTest,
463 name: "EarlyChangeCipherSpec-server-2",
464 config: Config{
465 Bugs: ProtocolBugs{
466 EarlyChangeCipherSpec: 2,
467 },
468 },
469 shouldFail: true,
470 expectedError: ":CCS_RECEIVED_EARLY:",
471 },
David Benjamind23f4122014-07-23 15:09:48 -0400472 {
David Benjamind23f4122014-07-23 15:09:48 -0400473 name: "SkipNewSessionTicket",
474 config: Config{
475 Bugs: ProtocolBugs{
476 SkipNewSessionTicket: true,
477 },
478 },
479 shouldFail: true,
480 expectedError: ":CCS_RECEIVED_EARLY:",
481 },
David Benjamin7e3305e2014-07-28 14:52:32 -0400482 {
David Benjamind86c7672014-08-02 04:07:12 -0400483 testType: serverTest,
David Benjaminbef270a2014-08-02 04:22:02 -0400484 name: "FallbackSCSV",
485 config: Config{
486 MaxVersion: VersionTLS11,
487 Bugs: ProtocolBugs{
488 SendFallbackSCSV: true,
489 },
490 },
491 shouldFail: true,
492 expectedError: ":INAPPROPRIATE_FALLBACK:",
493 },
494 {
495 testType: serverTest,
496 name: "FallbackSCSV-VersionMatch",
497 config: Config{
498 Bugs: ProtocolBugs{
499 SendFallbackSCSV: true,
500 },
501 },
502 },
David Benjamin98214542014-08-07 18:02:39 -0400503 {
504 testType: serverTest,
505 name: "FragmentedClientVersion",
506 config: Config{
507 Bugs: ProtocolBugs{
508 MaxHandshakeRecordLength: 1,
509 FragmentClientVersion: true,
510 },
511 },
David Benjamin82c9e902014-12-12 15:55:27 -0500512 expectedVersion: VersionTLS12,
David Benjamin98214542014-08-07 18:02:39 -0400513 },
David Benjamin98e882e2014-08-08 13:24:34 -0400514 {
515 testType: serverTest,
516 name: "MinorVersionTolerance",
517 config: Config{
518 Bugs: ProtocolBugs{
519 SendClientVersion: 0x03ff,
520 },
521 },
522 expectedVersion: VersionTLS12,
523 },
524 {
525 testType: serverTest,
526 name: "MajorVersionTolerance",
527 config: Config{
528 Bugs: ProtocolBugs{
529 SendClientVersion: 0x0400,
530 },
531 },
532 expectedVersion: VersionTLS12,
533 },
534 {
535 testType: serverTest,
536 name: "VersionTooLow",
537 config: Config{
538 Bugs: ProtocolBugs{
539 SendClientVersion: 0x0200,
540 },
541 },
542 shouldFail: true,
543 expectedError: ":UNSUPPORTED_PROTOCOL:",
544 },
545 {
546 testType: serverTest,
547 name: "HttpGET",
548 sendPrefix: "GET / HTTP/1.0\n",
549 shouldFail: true,
550 expectedError: ":HTTP_REQUEST:",
551 },
552 {
553 testType: serverTest,
554 name: "HttpPOST",
555 sendPrefix: "POST / HTTP/1.0\n",
556 shouldFail: true,
557 expectedError: ":HTTP_REQUEST:",
558 },
559 {
560 testType: serverTest,
561 name: "HttpHEAD",
562 sendPrefix: "HEAD / HTTP/1.0\n",
563 shouldFail: true,
564 expectedError: ":HTTP_REQUEST:",
565 },
566 {
567 testType: serverTest,
568 name: "HttpPUT",
569 sendPrefix: "PUT / HTTP/1.0\n",
570 shouldFail: true,
571 expectedError: ":HTTP_REQUEST:",
572 },
573 {
574 testType: serverTest,
575 name: "HttpCONNECT",
576 sendPrefix: "CONNECT www.google.com:443 HTTP/1.0\n",
577 shouldFail: true,
578 expectedError: ":HTTPS_PROXY_REQUEST:",
579 },
David Benjamin39ebf532014-08-31 02:23:49 -0400580 {
David Benjaminf080ecd2014-12-11 18:48:59 -0500581 testType: serverTest,
582 name: "Garbage",
583 sendPrefix: "blah",
584 shouldFail: true,
585 expectedError: ":UNKNOWN_PROTOCOL:",
586 },
587 {
David Benjamin39ebf532014-08-31 02:23:49 -0400588 name: "SkipCipherVersionCheck",
589 config: Config{
590 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
591 MaxVersion: VersionTLS11,
592 Bugs: ProtocolBugs{
593 SkipCipherVersionCheck: true,
594 },
595 },
596 shouldFail: true,
597 expectedError: ":WRONG_CIPHER_RETURNED:",
598 },
David Benjamin9114fae2014-11-08 11:41:14 -0500599 {
David Benjamina3e89492015-02-26 15:16:22 -0500600 name: "RSAEphemeralKey",
David Benjamin9114fae2014-11-08 11:41:14 -0500601 config: Config{
602 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
603 Bugs: ProtocolBugs{
David Benjamina3e89492015-02-26 15:16:22 -0500604 RSAEphemeralKey: true,
David Benjamin9114fae2014-11-08 11:41:14 -0500605 },
606 },
607 shouldFail: true,
608 expectedError: ":UNEXPECTED_MESSAGE:",
609 },
David Benjamin128dbc32014-12-01 01:27:42 -0500610 {
611 name: "DisableEverything",
612 flags: []string{"-no-tls12", "-no-tls11", "-no-tls1", "-no-ssl3"},
613 shouldFail: true,
614 expectedError: ":WRONG_SSL_VERSION:",
615 },
616 {
617 protocol: dtls,
618 name: "DisableEverything-DTLS",
619 flags: []string{"-no-tls12", "-no-tls1"},
620 shouldFail: true,
621 expectedError: ":WRONG_SSL_VERSION:",
622 },
David Benjamin780d6dd2015-01-06 12:03:19 -0500623 {
624 name: "NoSharedCipher",
625 config: Config{
626 CipherSuites: []uint16{},
627 },
628 shouldFail: true,
629 expectedError: ":HANDSHAKE_FAILURE_ON_CLIENT_HELLO:",
630 },
David Benjamin13be1de2015-01-11 16:29:36 -0500631 {
632 protocol: dtls,
633 testType: serverTest,
634 name: "MTU",
635 config: Config{
636 Bugs: ProtocolBugs{
637 MaxPacketLength: 256,
638 },
639 },
640 flags: []string{"-mtu", "256"},
641 },
642 {
643 protocol: dtls,
644 testType: serverTest,
645 name: "MTUExceeded",
646 config: Config{
647 Bugs: ProtocolBugs{
648 MaxPacketLength: 255,
649 },
650 },
651 flags: []string{"-mtu", "256"},
652 shouldFail: true,
653 expectedLocalError: "dtls: exceeded maximum packet length",
654 },
David Benjamin6095de82014-12-27 01:50:38 -0500655 {
656 name: "CertMismatchRSA",
657 config: Config{
658 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
659 Certificates: []Certificate{getECDSACertificate()},
660 Bugs: ProtocolBugs{
661 SendCipherSuite: TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
662 },
663 },
664 shouldFail: true,
665 expectedError: ":WRONG_CERTIFICATE_TYPE:",
666 },
667 {
668 name: "CertMismatchECDSA",
669 config: Config{
670 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
671 Certificates: []Certificate{getRSACertificate()},
672 Bugs: ProtocolBugs{
673 SendCipherSuite: TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
674 },
675 },
676 shouldFail: true,
677 expectedError: ":WRONG_CERTIFICATE_TYPE:",
678 },
David Benjamin5fa3eba2015-01-22 16:35:40 -0500679 {
680 name: "TLSFatalBadPackets",
681 damageFirstWrite: true,
682 shouldFail: true,
683 expectedError: ":DECRYPTION_FAILED_OR_BAD_RECORD_MAC:",
684 },
685 {
686 protocol: dtls,
687 name: "DTLSIgnoreBadPackets",
688 damageFirstWrite: true,
689 },
690 {
691 protocol: dtls,
692 name: "DTLSIgnoreBadPackets-Async",
693 damageFirstWrite: true,
694 flags: []string{"-async"},
695 },
David Benjamin4189bd92015-01-25 23:52:39 -0500696 {
697 name: "AppDataAfterChangeCipherSpec",
698 config: Config{
699 Bugs: ProtocolBugs{
700 AppDataAfterChangeCipherSpec: []byte("TEST MESSAGE"),
701 },
702 },
703 shouldFail: true,
704 expectedError: ":DATA_BETWEEN_CCS_AND_FINISHED:",
705 },
706 {
707 protocol: dtls,
708 name: "AppDataAfterChangeCipherSpec-DTLS",
709 config: Config{
710 Bugs: ProtocolBugs{
711 AppDataAfterChangeCipherSpec: []byte("TEST MESSAGE"),
712 },
713 },
David Benjamin4417d052015-04-05 04:17:25 -0400714 // BoringSSL's DTLS implementation will drop the out-of-order
715 // application data.
David Benjamin4189bd92015-01-25 23:52:39 -0500716 },
David Benjaminb3774b92015-01-31 17:16:01 -0500717 {
David Benjamindc3da932015-03-12 15:09:02 -0400718 name: "AlertAfterChangeCipherSpec",
719 config: Config{
720 Bugs: ProtocolBugs{
721 AlertAfterChangeCipherSpec: alertRecordOverflow,
722 },
723 },
724 shouldFail: true,
725 expectedError: ":TLSV1_ALERT_RECORD_OVERFLOW:",
726 },
727 {
728 protocol: dtls,
729 name: "AlertAfterChangeCipherSpec-DTLS",
730 config: Config{
731 Bugs: ProtocolBugs{
732 AlertAfterChangeCipherSpec: alertRecordOverflow,
733 },
734 },
735 shouldFail: true,
736 expectedError: ":TLSV1_ALERT_RECORD_OVERFLOW:",
737 },
738 {
David Benjaminb3774b92015-01-31 17:16:01 -0500739 protocol: dtls,
740 name: "ReorderHandshakeFragments-Small-DTLS",
741 config: Config{
742 Bugs: ProtocolBugs{
743 ReorderHandshakeFragments: true,
744 // Small enough that every handshake message is
745 // fragmented.
746 MaxHandshakeRecordLength: 2,
747 },
748 },
749 },
750 {
751 protocol: dtls,
752 name: "ReorderHandshakeFragments-Large-DTLS",
753 config: Config{
754 Bugs: ProtocolBugs{
755 ReorderHandshakeFragments: true,
756 // Large enough that no handshake message is
757 // fragmented.
David Benjaminb3774b92015-01-31 17:16:01 -0500758 MaxHandshakeRecordLength: 2048,
759 },
760 },
761 },
David Benjaminddb9f152015-02-03 15:44:39 -0500762 {
David Benjamin75381222015-03-02 19:30:30 -0500763 protocol: dtls,
764 name: "MixCompleteMessageWithFragments-DTLS",
765 config: Config{
766 Bugs: ProtocolBugs{
767 ReorderHandshakeFragments: true,
768 MixCompleteMessageWithFragments: true,
769 MaxHandshakeRecordLength: 2,
770 },
771 },
772 },
773 {
David Benjaminddb9f152015-02-03 15:44:39 -0500774 name: "SendInvalidRecordType",
775 config: Config{
776 Bugs: ProtocolBugs{
777 SendInvalidRecordType: true,
778 },
779 },
780 shouldFail: true,
781 expectedError: ":UNEXPECTED_RECORD:",
782 },
783 {
784 protocol: dtls,
785 name: "SendInvalidRecordType-DTLS",
786 config: Config{
787 Bugs: ProtocolBugs{
788 SendInvalidRecordType: true,
789 },
790 },
791 shouldFail: true,
792 expectedError: ":UNEXPECTED_RECORD:",
793 },
David Benjaminb80168e2015-02-08 18:30:14 -0500794 {
795 name: "FalseStart-SkipServerSecondLeg",
796 config: Config{
797 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
798 NextProtos: []string{"foo"},
799 Bugs: ProtocolBugs{
800 SkipNewSessionTicket: true,
801 SkipChangeCipherSpec: true,
802 SkipFinished: true,
803 ExpectFalseStart: true,
804 },
805 },
806 flags: []string{
807 "-false-start",
David Benjamin87e4acd2015-04-02 19:57:35 -0400808 "-handshake-never-done",
David Benjaminb80168e2015-02-08 18:30:14 -0500809 "-advertise-alpn", "\x03foo",
810 },
811 shimWritesFirst: true,
812 shouldFail: true,
813 expectedError: ":UNEXPECTED_RECORD:",
814 },
David Benjamin931ab342015-02-08 19:46:57 -0500815 {
816 name: "FalseStart-SkipServerSecondLeg-Implicit",
817 config: Config{
818 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
819 NextProtos: []string{"foo"},
820 Bugs: ProtocolBugs{
821 SkipNewSessionTicket: true,
822 SkipChangeCipherSpec: true,
823 SkipFinished: true,
824 },
825 },
826 flags: []string{
827 "-implicit-handshake",
828 "-false-start",
David Benjamin87e4acd2015-04-02 19:57:35 -0400829 "-handshake-never-done",
David Benjamin931ab342015-02-08 19:46:57 -0500830 "-advertise-alpn", "\x03foo",
831 },
832 shouldFail: true,
833 expectedError: ":UNEXPECTED_RECORD:",
834 },
David Benjamin6f5c0f42015-02-24 01:23:21 -0500835 {
836 testType: serverTest,
837 name: "FailEarlyCallback",
838 flags: []string{"-fail-early-callback"},
839 shouldFail: true,
840 expectedError: ":CONNECTION_REJECTED:",
841 expectedLocalError: "remote error: access denied",
842 },
David Benjaminbcb2d912015-02-24 23:45:43 -0500843 {
844 name: "WrongMessageType",
845 config: Config{
846 Bugs: ProtocolBugs{
847 WrongCertificateMessageType: true,
848 },
849 },
850 shouldFail: true,
851 expectedError: ":UNEXPECTED_MESSAGE:",
852 expectedLocalError: "remote error: unexpected message",
853 },
854 {
855 protocol: dtls,
856 name: "WrongMessageType-DTLS",
857 config: Config{
858 Bugs: ProtocolBugs{
859 WrongCertificateMessageType: true,
860 },
861 },
862 shouldFail: true,
863 expectedError: ":UNEXPECTED_MESSAGE:",
864 expectedLocalError: "remote error: unexpected message",
865 },
David Benjamin75381222015-03-02 19:30:30 -0500866 {
867 protocol: dtls,
868 name: "FragmentMessageTypeMismatch-DTLS",
869 config: Config{
870 Bugs: ProtocolBugs{
871 MaxHandshakeRecordLength: 2,
872 FragmentMessageTypeMismatch: true,
873 },
874 },
875 shouldFail: true,
876 expectedError: ":FRAGMENT_MISMATCH:",
877 },
878 {
879 protocol: dtls,
880 name: "FragmentMessageLengthMismatch-DTLS",
881 config: Config{
882 Bugs: ProtocolBugs{
883 MaxHandshakeRecordLength: 2,
884 FragmentMessageLengthMismatch: true,
885 },
886 },
887 shouldFail: true,
888 expectedError: ":FRAGMENT_MISMATCH:",
889 },
890 {
891 protocol: dtls,
892 name: "SplitFragmentHeader-DTLS",
893 config: Config{
894 Bugs: ProtocolBugs{
895 SplitFragmentHeader: true,
896 },
897 },
898 shouldFail: true,
899 expectedError: ":UNEXPECTED_MESSAGE:",
900 },
901 {
902 protocol: dtls,
903 name: "SplitFragmentBody-DTLS",
904 config: Config{
905 Bugs: ProtocolBugs{
906 SplitFragmentBody: true,
907 },
908 },
909 shouldFail: true,
910 expectedError: ":UNEXPECTED_MESSAGE:",
911 },
912 {
913 protocol: dtls,
914 name: "SendEmptyFragments-DTLS",
915 config: Config{
916 Bugs: ProtocolBugs{
917 SendEmptyFragments: true,
918 },
919 },
920 },
David Benjamin67d1fb52015-03-16 15:16:23 -0400921 {
922 name: "UnsupportedCipherSuite",
923 config: Config{
924 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
925 Bugs: ProtocolBugs{
926 IgnorePeerCipherPreferences: true,
927 },
928 },
929 flags: []string{"-cipher", "DEFAULT:!RC4"},
930 shouldFail: true,
931 expectedError: ":WRONG_CIPHER_RETURNED:",
932 },
David Benjamin340d5ed2015-03-21 02:21:37 -0400933 {
David Benjaminc574f412015-04-20 11:13:01 -0400934 name: "UnsupportedCurve",
935 config: Config{
936 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
937 // BoringSSL implements P-224 but doesn't enable it by
938 // default.
939 CurvePreferences: []CurveID{CurveP224},
940 Bugs: ProtocolBugs{
941 IgnorePeerCurvePreferences: true,
942 },
943 },
944 shouldFail: true,
945 expectedError: ":WRONG_CURVE:",
946 },
947 {
David Benjamin340d5ed2015-03-21 02:21:37 -0400948 name: "SendWarningAlerts",
949 config: Config{
950 Bugs: ProtocolBugs{
951 SendWarningAlerts: alertAccessDenied,
952 },
953 },
954 },
955 {
956 protocol: dtls,
957 name: "SendWarningAlerts-DTLS",
958 config: Config{
959 Bugs: ProtocolBugs{
960 SendWarningAlerts: alertAccessDenied,
961 },
962 },
963 },
David Benjamin513f0ea2015-04-02 19:33:31 -0400964 {
965 name: "BadFinished",
966 config: Config{
967 Bugs: ProtocolBugs{
968 BadFinished: true,
969 },
970 },
971 shouldFail: true,
972 expectedError: ":DIGEST_CHECK_FAILED:",
973 },
974 {
975 name: "FalseStart-BadFinished",
976 config: Config{
977 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
978 NextProtos: []string{"foo"},
979 Bugs: ProtocolBugs{
980 BadFinished: true,
981 ExpectFalseStart: true,
982 },
983 },
984 flags: []string{
985 "-false-start",
David Benjamin87e4acd2015-04-02 19:57:35 -0400986 "-handshake-never-done",
David Benjamin513f0ea2015-04-02 19:33:31 -0400987 "-advertise-alpn", "\x03foo",
988 },
989 shimWritesFirst: true,
990 shouldFail: true,
991 expectedError: ":DIGEST_CHECK_FAILED:",
992 },
David Benjamin1c633152015-04-02 20:19:11 -0400993 {
994 name: "NoFalseStart-NoALPN",
995 config: Config{
996 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
997 Bugs: ProtocolBugs{
998 ExpectFalseStart: true,
999 AlertBeforeFalseStartTest: alertAccessDenied,
1000 },
1001 },
1002 flags: []string{
1003 "-false-start",
1004 },
1005 shimWritesFirst: true,
1006 shouldFail: true,
1007 expectedError: ":TLSV1_ALERT_ACCESS_DENIED:",
1008 expectedLocalError: "tls: peer did not false start: EOF",
1009 },
1010 {
1011 name: "NoFalseStart-NoAEAD",
1012 config: Config{
1013 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1014 NextProtos: []string{"foo"},
1015 Bugs: ProtocolBugs{
1016 ExpectFalseStart: true,
1017 AlertBeforeFalseStartTest: alertAccessDenied,
1018 },
1019 },
1020 flags: []string{
1021 "-false-start",
1022 "-advertise-alpn", "\x03foo",
1023 },
1024 shimWritesFirst: true,
1025 shouldFail: true,
1026 expectedError: ":TLSV1_ALERT_ACCESS_DENIED:",
1027 expectedLocalError: "tls: peer did not false start: EOF",
1028 },
1029 {
1030 name: "NoFalseStart-RSA",
1031 config: Config{
1032 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
1033 NextProtos: []string{"foo"},
1034 Bugs: ProtocolBugs{
1035 ExpectFalseStart: true,
1036 AlertBeforeFalseStartTest: alertAccessDenied,
1037 },
1038 },
1039 flags: []string{
1040 "-false-start",
1041 "-advertise-alpn", "\x03foo",
1042 },
1043 shimWritesFirst: true,
1044 shouldFail: true,
1045 expectedError: ":TLSV1_ALERT_ACCESS_DENIED:",
1046 expectedLocalError: "tls: peer did not false start: EOF",
1047 },
1048 {
1049 name: "NoFalseStart-DHE_RSA",
1050 config: Config{
1051 CipherSuites: []uint16{TLS_DHE_RSA_WITH_AES_128_GCM_SHA256},
1052 NextProtos: []string{"foo"},
1053 Bugs: ProtocolBugs{
1054 ExpectFalseStart: true,
1055 AlertBeforeFalseStartTest: alertAccessDenied,
1056 },
1057 },
1058 flags: []string{
1059 "-false-start",
1060 "-advertise-alpn", "\x03foo",
1061 },
1062 shimWritesFirst: true,
1063 shouldFail: true,
1064 expectedError: ":TLSV1_ALERT_ACCESS_DENIED:",
1065 expectedLocalError: "tls: peer did not false start: EOF",
1066 },
David Benjamin55a43642015-04-20 14:45:55 -04001067 {
1068 testType: serverTest,
1069 name: "NoSupportedCurves",
1070 config: Config{
1071 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1072 Bugs: ProtocolBugs{
1073 NoSupportedCurves: true,
1074 },
1075 },
1076 },
David Benjamin90da8c82015-04-20 14:57:57 -04001077 {
1078 testType: serverTest,
1079 name: "NoCommonCurves",
1080 config: Config{
1081 CipherSuites: []uint16{
1082 TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
1083 TLS_DHE_RSA_WITH_AES_128_GCM_SHA256,
1084 },
1085 CurvePreferences: []CurveID{CurveP224},
1086 },
1087 expectedCipher: TLS_DHE_RSA_WITH_AES_128_GCM_SHA256,
1088 },
David Benjamin9a41d1b2015-05-16 01:30:09 -04001089 {
1090 protocol: dtls,
1091 name: "SendSplitAlert-Sync",
1092 config: Config{
1093 Bugs: ProtocolBugs{
1094 SendSplitAlert: true,
1095 },
1096 },
1097 },
1098 {
1099 protocol: dtls,
1100 name: "SendSplitAlert-Async",
1101 config: Config{
1102 Bugs: ProtocolBugs{
1103 SendSplitAlert: true,
1104 },
1105 },
1106 flags: []string{"-async"},
1107 },
David Benjaminbd15a8e2015-05-29 18:48:16 -04001108 {
1109 protocol: dtls,
1110 name: "PackDTLSHandshake",
1111 config: Config{
1112 Bugs: ProtocolBugs{
1113 MaxHandshakeRecordLength: 2,
1114 PackHandshakeFragments: 20,
1115 PackHandshakeRecords: 200,
1116 },
1117 },
1118 },
David Benjamin0fa40122015-05-30 17:13:12 -04001119 {
1120 testType: serverTest,
1121 protocol: dtls,
1122 name: "NoRC4-DTLS",
1123 config: Config{
1124 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_RC4_128_SHA},
1125 Bugs: ProtocolBugs{
1126 EnableAllCiphersInDTLS: true,
1127 },
1128 },
1129 shouldFail: true,
1130 expectedError: ":NO_SHARED_CIPHER:",
1131 },
Adam Langley95c29f32014-06-20 12:00:00 -07001132}
1133
David Benjamin01fe8202014-09-24 15:21:44 -04001134func doExchange(test *testCase, config *Config, conn net.Conn, messageLen int, isResume bool) error {
David Benjamin65ea8ff2014-11-23 03:01:00 -05001135 var connDebug *recordingConn
David Benjamin5fa3eba2015-01-22 16:35:40 -05001136 var connDamage *damageAdaptor
David Benjamin65ea8ff2014-11-23 03:01:00 -05001137 if *flagDebug {
1138 connDebug = &recordingConn{Conn: conn}
1139 conn = connDebug
1140 defer func() {
1141 connDebug.WriteTo(os.Stdout)
1142 }()
1143 }
1144
David Benjamin6fd297b2014-08-11 18:43:38 -04001145 if test.protocol == dtls {
David Benjamin83f90402015-01-27 01:09:43 -05001146 config.Bugs.PacketAdaptor = newPacketAdaptor(conn)
1147 conn = config.Bugs.PacketAdaptor
David Benjamin5e961c12014-11-07 01:48:35 -05001148 if test.replayWrites {
1149 conn = newReplayAdaptor(conn)
1150 }
David Benjamin6fd297b2014-08-11 18:43:38 -04001151 }
1152
David Benjamin5fa3eba2015-01-22 16:35:40 -05001153 if test.damageFirstWrite {
1154 connDamage = newDamageAdaptor(conn)
1155 conn = connDamage
1156 }
1157
David Benjamin6fd297b2014-08-11 18:43:38 -04001158 if test.sendPrefix != "" {
1159 if _, err := conn.Write([]byte(test.sendPrefix)); err != nil {
1160 return err
1161 }
David Benjamin98e882e2014-08-08 13:24:34 -04001162 }
1163
David Benjamin1d5c83e2014-07-22 19:20:02 -04001164 var tlsConn *Conn
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001165 if test.testType == clientTest {
David Benjamin6fd297b2014-08-11 18:43:38 -04001166 if test.protocol == dtls {
1167 tlsConn = DTLSServer(conn, config)
1168 } else {
1169 tlsConn = Server(conn, config)
1170 }
David Benjamin1d5c83e2014-07-22 19:20:02 -04001171 } else {
1172 config.InsecureSkipVerify = true
David Benjamin6fd297b2014-08-11 18:43:38 -04001173 if test.protocol == dtls {
1174 tlsConn = DTLSClient(conn, config)
1175 } else {
1176 tlsConn = Client(conn, config)
1177 }
David Benjamin1d5c83e2014-07-22 19:20:02 -04001178 }
1179
Adam Langley95c29f32014-06-20 12:00:00 -07001180 if err := tlsConn.Handshake(); err != nil {
1181 return err
1182 }
Kenny Root7fdeaf12014-08-05 15:23:37 -07001183
David Benjamin01fe8202014-09-24 15:21:44 -04001184 // TODO(davidben): move all per-connection expectations into a dedicated
1185 // expectations struct that can be specified separately for the two
1186 // legs.
1187 expectedVersion := test.expectedVersion
1188 if isResume && test.expectedResumeVersion != 0 {
1189 expectedVersion = test.expectedResumeVersion
1190 }
1191 if vers := tlsConn.ConnectionState().Version; expectedVersion != 0 && vers != expectedVersion {
1192 return fmt.Errorf("got version %x, expected %x", vers, expectedVersion)
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001193 }
1194
David Benjamin90da8c82015-04-20 14:57:57 -04001195 if cipher := tlsConn.ConnectionState().CipherSuite; test.expectedCipher != 0 && cipher != test.expectedCipher {
1196 return fmt.Errorf("got cipher %x, expected %x", cipher, test.expectedCipher)
1197 }
1198
David Benjamina08e49d2014-08-24 01:46:07 -04001199 if test.expectChannelID {
1200 channelID := tlsConn.ConnectionState().ChannelID
1201 if channelID == nil {
1202 return fmt.Errorf("no channel ID negotiated")
1203 }
1204 if channelID.Curve != channelIDKey.Curve ||
1205 channelIDKey.X.Cmp(channelIDKey.X) != 0 ||
1206 channelIDKey.Y.Cmp(channelIDKey.Y) != 0 {
1207 return fmt.Errorf("incorrect channel ID")
1208 }
1209 }
1210
David Benjaminae2888f2014-09-06 12:58:58 -04001211 if expected := test.expectedNextProto; expected != "" {
1212 if actual := tlsConn.ConnectionState().NegotiatedProtocol; actual != expected {
1213 return fmt.Errorf("next proto mismatch: got %s, wanted %s", actual, expected)
1214 }
1215 }
1216
David Benjaminfc7b0862014-09-06 13:21:53 -04001217 if test.expectedNextProtoType != 0 {
1218 if (test.expectedNextProtoType == alpn) != tlsConn.ConnectionState().NegotiatedProtocolFromALPN {
1219 return fmt.Errorf("next proto type mismatch")
1220 }
1221 }
1222
David Benjaminca6c8262014-11-15 19:06:08 -05001223 if p := tlsConn.ConnectionState().SRTPProtectionProfile; p != test.expectedSRTPProtectionProfile {
1224 return fmt.Errorf("SRTP profile mismatch: got %d, wanted %d", p, test.expectedSRTPProtectionProfile)
1225 }
1226
David Benjaminc565ebb2015-04-03 04:06:36 -04001227 if test.exportKeyingMaterial > 0 {
1228 actual := make([]byte, test.exportKeyingMaterial)
1229 if _, err := io.ReadFull(tlsConn, actual); err != nil {
1230 return err
1231 }
1232 expected, err := tlsConn.ExportKeyingMaterial(test.exportKeyingMaterial, []byte(test.exportLabel), []byte(test.exportContext), test.useExportContext)
1233 if err != nil {
1234 return err
1235 }
1236 if !bytes.Equal(actual, expected) {
1237 return fmt.Errorf("keying material mismatch")
1238 }
1239 }
1240
David Benjamine58c4f52014-08-24 03:47:07 -04001241 if test.shimWritesFirst {
1242 var buf [5]byte
1243 _, err := io.ReadFull(tlsConn, buf[:])
1244 if err != nil {
1245 return err
1246 }
1247 if string(buf[:]) != "hello" {
1248 return fmt.Errorf("bad initial message")
1249 }
1250 }
1251
Adam Langleycf2d4f42014-10-28 19:06:14 -07001252 if test.renegotiate {
1253 if test.renegotiateCiphers != nil {
1254 config.CipherSuites = test.renegotiateCiphers
1255 }
1256 if err := tlsConn.Renegotiate(); err != nil {
1257 return err
1258 }
1259 } else if test.renegotiateCiphers != nil {
1260 panic("renegotiateCiphers without renegotiate")
1261 }
1262
David Benjamin5fa3eba2015-01-22 16:35:40 -05001263 if test.damageFirstWrite {
1264 connDamage.setDamage(true)
1265 tlsConn.Write([]byte("DAMAGED WRITE"))
1266 connDamage.setDamage(false)
1267 }
1268
Kenny Root7fdeaf12014-08-05 15:23:37 -07001269 if messageLen < 0 {
David Benjamin6fd297b2014-08-11 18:43:38 -04001270 if test.protocol == dtls {
1271 return fmt.Errorf("messageLen < 0 not supported for DTLS tests")
1272 }
Kenny Root7fdeaf12014-08-05 15:23:37 -07001273 // Read until EOF.
1274 _, err := io.Copy(ioutil.Discard, tlsConn)
1275 return err
1276 }
1277
David Benjamin4417d052015-04-05 04:17:25 -04001278 if messageLen == 0 {
1279 messageLen = 32
Adam Langley80842bd2014-06-20 12:00:00 -07001280 }
David Benjamin4417d052015-04-05 04:17:25 -04001281 testMessage := make([]byte, messageLen)
1282 for i := range testMessage {
1283 testMessage[i] = 0x42
1284 }
1285 tlsConn.Write(testMessage)
Adam Langley95c29f32014-06-20 12:00:00 -07001286
1287 buf := make([]byte, len(testMessage))
David Benjamin6fd297b2014-08-11 18:43:38 -04001288 if test.protocol == dtls {
1289 bufTmp := make([]byte, len(buf)+1)
1290 n, err := tlsConn.Read(bufTmp)
1291 if err != nil {
1292 return err
1293 }
1294 if n != len(buf) {
1295 return fmt.Errorf("bad reply; length mismatch (%d vs %d)", n, len(buf))
1296 }
1297 copy(buf, bufTmp)
1298 } else {
1299 _, err := io.ReadFull(tlsConn, buf)
1300 if err != nil {
1301 return err
1302 }
Adam Langley95c29f32014-06-20 12:00:00 -07001303 }
1304
1305 for i, v := range buf {
1306 if v != testMessage[i]^0xff {
1307 return fmt.Errorf("bad reply contents at byte %d", i)
1308 }
1309 }
1310
1311 return nil
1312}
1313
David Benjamin325b5c32014-07-01 19:40:31 -04001314func valgrindOf(dbAttach bool, path string, args ...string) *exec.Cmd {
1315 valgrindArgs := []string{"--error-exitcode=99", "--track-origins=yes", "--leak-check=full"}
Adam Langley95c29f32014-06-20 12:00:00 -07001316 if dbAttach {
David Benjamin325b5c32014-07-01 19:40:31 -04001317 valgrindArgs = append(valgrindArgs, "--db-attach=yes", "--db-command=xterm -e gdb -nw %f %p")
Adam Langley95c29f32014-06-20 12:00:00 -07001318 }
David Benjamin325b5c32014-07-01 19:40:31 -04001319 valgrindArgs = append(valgrindArgs, path)
1320 valgrindArgs = append(valgrindArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -07001321
David Benjamin325b5c32014-07-01 19:40:31 -04001322 return exec.Command("valgrind", valgrindArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -07001323}
1324
David Benjamin325b5c32014-07-01 19:40:31 -04001325func gdbOf(path string, args ...string) *exec.Cmd {
1326 xtermArgs := []string{"-e", "gdb", "--args"}
1327 xtermArgs = append(xtermArgs, path)
1328 xtermArgs = append(xtermArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -07001329
David Benjamin325b5c32014-07-01 19:40:31 -04001330 return exec.Command("xterm", xtermArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -07001331}
1332
Adam Langley69a01602014-11-17 17:26:55 -08001333type moreMallocsError struct{}
1334
1335func (moreMallocsError) Error() string {
1336 return "child process did not exhaust all allocation calls"
1337}
1338
1339var errMoreMallocs = moreMallocsError{}
1340
David Benjamin87c8a642015-02-21 01:54:29 -05001341// accept accepts a connection from listener, unless waitChan signals a process
1342// exit first.
1343func acceptOrWait(listener net.Listener, waitChan chan error) (net.Conn, error) {
1344 type connOrError struct {
1345 conn net.Conn
1346 err error
1347 }
1348 connChan := make(chan connOrError, 1)
1349 go func() {
1350 conn, err := listener.Accept()
1351 connChan <- connOrError{conn, err}
1352 close(connChan)
1353 }()
1354 select {
1355 case result := <-connChan:
1356 return result.conn, result.err
1357 case childErr := <-waitChan:
1358 waitChan <- childErr
1359 return nil, fmt.Errorf("child exited early: %s", childErr)
1360 }
1361}
1362
Adam Langley69a01602014-11-17 17:26:55 -08001363func runTest(test *testCase, buildDir string, mallocNumToFail int64) error {
Adam Langley38311732014-10-16 19:04:35 -07001364 if !test.shouldFail && (len(test.expectedError) > 0 || len(test.expectedLocalError) > 0) {
1365 panic("Error expected without shouldFail in " + test.name)
1366 }
1367
David Benjamin87c8a642015-02-21 01:54:29 -05001368 listener, err := net.ListenTCP("tcp4", &net.TCPAddr{IP: net.IP{127, 0, 0, 1}})
1369 if err != nil {
1370 panic(err)
1371 }
1372 defer func() {
1373 if listener != nil {
1374 listener.Close()
1375 }
1376 }()
Adam Langley95c29f32014-06-20 12:00:00 -07001377
David Benjamin884fdf12014-08-02 15:28:23 -04001378 shim_path := path.Join(buildDir, "ssl/test/bssl_shim")
David Benjamin87c8a642015-02-21 01:54:29 -05001379 flags := []string{"-port", strconv.Itoa(listener.Addr().(*net.TCPAddr).Port)}
David Benjamin1d5c83e2014-07-22 19:20:02 -04001380 if test.testType == serverTest {
David Benjamin5a593af2014-08-11 19:51:50 -04001381 flags = append(flags, "-server")
1382
David Benjamin025b3d32014-07-01 19:53:04 -04001383 flags = append(flags, "-key-file")
1384 if test.keyFile == "" {
1385 flags = append(flags, rsaKeyFile)
1386 } else {
1387 flags = append(flags, test.keyFile)
1388 }
1389
1390 flags = append(flags, "-cert-file")
1391 if test.certFile == "" {
1392 flags = append(flags, rsaCertificateFile)
1393 } else {
1394 flags = append(flags, test.certFile)
1395 }
1396 }
David Benjamin5a593af2014-08-11 19:51:50 -04001397
David Benjamin6fd297b2014-08-11 18:43:38 -04001398 if test.protocol == dtls {
1399 flags = append(flags, "-dtls")
1400 }
1401
David Benjamin5a593af2014-08-11 19:51:50 -04001402 if test.resumeSession {
1403 flags = append(flags, "-resume")
1404 }
1405
David Benjamine58c4f52014-08-24 03:47:07 -04001406 if test.shimWritesFirst {
1407 flags = append(flags, "-shim-writes-first")
1408 }
1409
David Benjaminc565ebb2015-04-03 04:06:36 -04001410 if test.exportKeyingMaterial > 0 {
1411 flags = append(flags, "-export-keying-material", strconv.Itoa(test.exportKeyingMaterial))
1412 flags = append(flags, "-export-label", test.exportLabel)
1413 flags = append(flags, "-export-context", test.exportContext)
1414 if test.useExportContext {
1415 flags = append(flags, "-use-export-context")
1416 }
1417 }
1418
David Benjamin025b3d32014-07-01 19:53:04 -04001419 flags = append(flags, test.flags...)
1420
1421 var shim *exec.Cmd
1422 if *useValgrind {
1423 shim = valgrindOf(false, shim_path, flags...)
Adam Langley75712922014-10-10 16:23:43 -07001424 } else if *useGDB {
1425 shim = gdbOf(shim_path, flags...)
David Benjamin025b3d32014-07-01 19:53:04 -04001426 } else {
1427 shim = exec.Command(shim_path, flags...)
1428 }
David Benjamin025b3d32014-07-01 19:53:04 -04001429 shim.Stdin = os.Stdin
1430 var stdoutBuf, stderrBuf bytes.Buffer
1431 shim.Stdout = &stdoutBuf
1432 shim.Stderr = &stderrBuf
Adam Langley69a01602014-11-17 17:26:55 -08001433 if mallocNumToFail >= 0 {
David Benjamin9e128b02015-02-09 13:13:09 -05001434 shim.Env = os.Environ()
1435 shim.Env = append(shim.Env, "MALLOC_NUMBER_TO_FAIL="+strconv.FormatInt(mallocNumToFail, 10))
Adam Langley69a01602014-11-17 17:26:55 -08001436 if *mallocTestDebug {
1437 shim.Env = append(shim.Env, "MALLOC_ABORT_ON_FAIL=1")
1438 }
1439 shim.Env = append(shim.Env, "_MALLOC_CHECK=1")
1440 }
David Benjamin025b3d32014-07-01 19:53:04 -04001441
1442 if err := shim.Start(); err != nil {
Adam Langley95c29f32014-06-20 12:00:00 -07001443 panic(err)
1444 }
David Benjamin87c8a642015-02-21 01:54:29 -05001445 waitChan := make(chan error, 1)
1446 go func() { waitChan <- shim.Wait() }()
Adam Langley95c29f32014-06-20 12:00:00 -07001447
1448 config := test.config
David Benjamin1d5c83e2014-07-22 19:20:02 -04001449 config.ClientSessionCache = NewLRUClientSessionCache(1)
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001450 config.ServerSessionCache = NewLRUServerSessionCache(1)
David Benjamin025b3d32014-07-01 19:53:04 -04001451 if test.testType == clientTest {
1452 if len(config.Certificates) == 0 {
1453 config.Certificates = []Certificate{getRSACertificate()}
1454 }
David Benjamin87c8a642015-02-21 01:54:29 -05001455 } else {
1456 // Supply a ServerName to ensure a constant session cache key,
1457 // rather than falling back to net.Conn.RemoteAddr.
1458 if len(config.ServerName) == 0 {
1459 config.ServerName = "test"
1460 }
David Benjamin025b3d32014-07-01 19:53:04 -04001461 }
Adam Langley95c29f32014-06-20 12:00:00 -07001462
David Benjamin87c8a642015-02-21 01:54:29 -05001463 conn, err := acceptOrWait(listener, waitChan)
1464 if err == nil {
1465 err = doExchange(test, &config, conn, test.messageLen, false /* not a resumption */)
1466 conn.Close()
1467 }
David Benjamin65ea8ff2014-11-23 03:01:00 -05001468
David Benjamin1d5c83e2014-07-22 19:20:02 -04001469 if err == nil && test.resumeSession {
David Benjamin01fe8202014-09-24 15:21:44 -04001470 var resumeConfig Config
1471 if test.resumeConfig != nil {
1472 resumeConfig = *test.resumeConfig
David Benjamin87c8a642015-02-21 01:54:29 -05001473 if len(resumeConfig.ServerName) == 0 {
1474 resumeConfig.ServerName = config.ServerName
1475 }
David Benjamin01fe8202014-09-24 15:21:44 -04001476 if len(resumeConfig.Certificates) == 0 {
1477 resumeConfig.Certificates = []Certificate{getRSACertificate()}
1478 }
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001479 if !test.newSessionsOnResume {
1480 resumeConfig.SessionTicketKey = config.SessionTicketKey
1481 resumeConfig.ClientSessionCache = config.ClientSessionCache
1482 resumeConfig.ServerSessionCache = config.ServerSessionCache
1483 }
David Benjamin01fe8202014-09-24 15:21:44 -04001484 } else {
1485 resumeConfig = config
1486 }
David Benjamin87c8a642015-02-21 01:54:29 -05001487 var connResume net.Conn
1488 connResume, err = acceptOrWait(listener, waitChan)
1489 if err == nil {
1490 err = doExchange(test, &resumeConfig, connResume, test.messageLen, true /* resumption */)
1491 connResume.Close()
1492 }
David Benjamin1d5c83e2014-07-22 19:20:02 -04001493 }
1494
David Benjamin87c8a642015-02-21 01:54:29 -05001495 // Close the listener now. This is to avoid hangs should the shim try to
1496 // open more connections than expected.
1497 listener.Close()
1498 listener = nil
1499
1500 childErr := <-waitChan
Adam Langley69a01602014-11-17 17:26:55 -08001501 if exitError, ok := childErr.(*exec.ExitError); ok {
1502 if exitError.Sys().(syscall.WaitStatus).ExitStatus() == 88 {
1503 return errMoreMallocs
1504 }
1505 }
Adam Langley95c29f32014-06-20 12:00:00 -07001506
1507 stdout := string(stdoutBuf.Bytes())
1508 stderr := string(stderrBuf.Bytes())
1509 failed := err != nil || childErr != nil
David Benjaminc565ebb2015-04-03 04:06:36 -04001510 correctFailure := len(test.expectedError) == 0 || strings.Contains(stderr, test.expectedError)
Adam Langleyac61fa32014-06-23 12:03:11 -07001511 localError := "none"
1512 if err != nil {
1513 localError = err.Error()
1514 }
1515 if len(test.expectedLocalError) != 0 {
1516 correctFailure = correctFailure && strings.Contains(localError, test.expectedLocalError)
1517 }
Adam Langley95c29f32014-06-20 12:00:00 -07001518
1519 if failed != test.shouldFail || failed && !correctFailure {
Adam Langley95c29f32014-06-20 12:00:00 -07001520 childError := "none"
Adam Langley95c29f32014-06-20 12:00:00 -07001521 if childErr != nil {
1522 childError = childErr.Error()
1523 }
1524
1525 var msg string
1526 switch {
1527 case failed && !test.shouldFail:
1528 msg = "unexpected failure"
1529 case !failed && test.shouldFail:
1530 msg = "unexpected success"
1531 case failed && !correctFailure:
Adam Langleyac61fa32014-06-23 12:03:11 -07001532 msg = "bad error (wanted '" + test.expectedError + "' / '" + test.expectedLocalError + "')"
Adam Langley95c29f32014-06-20 12:00:00 -07001533 default:
1534 panic("internal error")
1535 }
1536
David Benjaminc565ebb2015-04-03 04:06:36 -04001537 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 -07001538 }
1539
David Benjaminc565ebb2015-04-03 04:06:36 -04001540 if !*useValgrind && !failed && len(stderr) > 0 {
Adam Langley95c29f32014-06-20 12:00:00 -07001541 println(stderr)
1542 }
1543
1544 return nil
1545}
1546
1547var tlsVersions = []struct {
1548 name string
1549 version uint16
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001550 flag string
David Benjamin8b8c0062014-11-23 02:47:52 -05001551 hasDTLS bool
Adam Langley95c29f32014-06-20 12:00:00 -07001552}{
David Benjamin8b8c0062014-11-23 02:47:52 -05001553 {"SSL3", VersionSSL30, "-no-ssl3", false},
1554 {"TLS1", VersionTLS10, "-no-tls1", true},
1555 {"TLS11", VersionTLS11, "-no-tls11", false},
1556 {"TLS12", VersionTLS12, "-no-tls12", true},
Adam Langley95c29f32014-06-20 12:00:00 -07001557}
1558
1559var testCipherSuites = []struct {
1560 name string
1561 id uint16
1562}{
1563 {"3DES-SHA", TLS_RSA_WITH_3DES_EDE_CBC_SHA},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001564 {"AES128-GCM", TLS_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001565 {"AES128-SHA", TLS_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001566 {"AES128-SHA256", TLS_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001567 {"AES256-GCM", TLS_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001568 {"AES256-SHA", TLS_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001569 {"AES256-SHA256", TLS_RSA_WITH_AES_256_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001570 {"DHE-RSA-AES128-GCM", TLS_DHE_RSA_WITH_AES_128_GCM_SHA256},
1571 {"DHE-RSA-AES128-SHA", TLS_DHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001572 {"DHE-RSA-AES128-SHA256", TLS_DHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001573 {"DHE-RSA-AES256-GCM", TLS_DHE_RSA_WITH_AES_256_GCM_SHA384},
1574 {"DHE-RSA-AES256-SHA", TLS_DHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001575 {"DHE-RSA-AES256-SHA256", TLS_DHE_RSA_WITH_AES_256_CBC_SHA256},
David Benjamine9a80ff2015-04-07 00:46:46 -04001576 {"DHE-RSA-CHACHA20-POLY1305", TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001577 {"ECDHE-ECDSA-AES128-GCM", TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
1578 {"ECDHE-ECDSA-AES128-SHA", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001579 {"ECDHE-ECDSA-AES128-SHA256", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256},
1580 {"ECDHE-ECDSA-AES256-GCM", TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001581 {"ECDHE-ECDSA-AES256-SHA", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001582 {"ECDHE-ECDSA-AES256-SHA384", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384},
David Benjamine9a80ff2015-04-07 00:46:46 -04001583 {"ECDHE-ECDSA-CHACHA20-POLY1305", TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001584 {"ECDHE-ECDSA-RC4-SHA", TLS_ECDHE_ECDSA_WITH_RC4_128_SHA},
Adam Langley97e8ba82015-04-29 15:32:10 -07001585 {"ECDHE-PSK-AES128-GCM-SHA256", TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001586 {"ECDHE-RSA-AES128-GCM", TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001587 {"ECDHE-RSA-AES128-SHA", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001588 {"ECDHE-RSA-AES128-SHA256", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001589 {"ECDHE-RSA-AES256-GCM", TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001590 {"ECDHE-RSA-AES256-SHA", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001591 {"ECDHE-RSA-AES256-SHA384", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384},
David Benjamine9a80ff2015-04-07 00:46:46 -04001592 {"ECDHE-RSA-CHACHA20-POLY1305", TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001593 {"ECDHE-RSA-RC4-SHA", TLS_ECDHE_RSA_WITH_RC4_128_SHA},
David Benjamin48cae082014-10-27 01:06:24 -04001594 {"PSK-AES128-CBC-SHA", TLS_PSK_WITH_AES_128_CBC_SHA},
1595 {"PSK-AES256-CBC-SHA", TLS_PSK_WITH_AES_256_CBC_SHA},
1596 {"PSK-RC4-SHA", TLS_PSK_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -07001597 {"RC4-MD5", TLS_RSA_WITH_RC4_128_MD5},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001598 {"RC4-SHA", TLS_RSA_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -07001599}
1600
David Benjamin8b8c0062014-11-23 02:47:52 -05001601func hasComponent(suiteName, component string) bool {
1602 return strings.Contains("-"+suiteName+"-", "-"+component+"-")
1603}
1604
David Benjaminf7768e42014-08-31 02:06:47 -04001605func isTLS12Only(suiteName string) bool {
David Benjamin8b8c0062014-11-23 02:47:52 -05001606 return hasComponent(suiteName, "GCM") ||
1607 hasComponent(suiteName, "SHA256") ||
David Benjamine9a80ff2015-04-07 00:46:46 -04001608 hasComponent(suiteName, "SHA384") ||
1609 hasComponent(suiteName, "POLY1305")
David Benjamin8b8c0062014-11-23 02:47:52 -05001610}
1611
1612func isDTLSCipher(suiteName string) bool {
David Benjamine95d20d2014-12-23 11:16:01 -05001613 return !hasComponent(suiteName, "RC4")
David Benjaminf7768e42014-08-31 02:06:47 -04001614}
1615
Adam Langleya7997f12015-05-14 17:38:50 -07001616func bigFromHex(hex string) *big.Int {
1617 ret, ok := new(big.Int).SetString(hex, 16)
1618 if !ok {
1619 panic("failed to parse hex number 0x" + hex)
1620 }
1621 return ret
1622}
1623
Adam Langley95c29f32014-06-20 12:00:00 -07001624func addCipherSuiteTests() {
1625 for _, suite := range testCipherSuites {
David Benjamin48cae082014-10-27 01:06:24 -04001626 const psk = "12345"
1627 const pskIdentity = "luggage combo"
1628
Adam Langley95c29f32014-06-20 12:00:00 -07001629 var cert Certificate
David Benjamin025b3d32014-07-01 19:53:04 -04001630 var certFile string
1631 var keyFile string
David Benjamin8b8c0062014-11-23 02:47:52 -05001632 if hasComponent(suite.name, "ECDSA") {
Adam Langley95c29f32014-06-20 12:00:00 -07001633 cert = getECDSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -04001634 certFile = ecdsaCertificateFile
1635 keyFile = ecdsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -07001636 } else {
1637 cert = getRSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -04001638 certFile = rsaCertificateFile
1639 keyFile = rsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -07001640 }
1641
David Benjamin48cae082014-10-27 01:06:24 -04001642 var flags []string
David Benjamin8b8c0062014-11-23 02:47:52 -05001643 if hasComponent(suite.name, "PSK") {
David Benjamin48cae082014-10-27 01:06:24 -04001644 flags = append(flags,
1645 "-psk", psk,
1646 "-psk-identity", pskIdentity)
1647 }
1648
Adam Langley95c29f32014-06-20 12:00:00 -07001649 for _, ver := range tlsVersions {
David Benjaminf7768e42014-08-31 02:06:47 -04001650 if ver.version < VersionTLS12 && isTLS12Only(suite.name) {
Adam Langley95c29f32014-06-20 12:00:00 -07001651 continue
1652 }
1653
David Benjamin025b3d32014-07-01 19:53:04 -04001654 testCases = append(testCases, testCase{
1655 testType: clientTest,
1656 name: ver.name + "-" + suite.name + "-client",
Adam Langley95c29f32014-06-20 12:00:00 -07001657 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001658 MinVersion: ver.version,
1659 MaxVersion: ver.version,
1660 CipherSuites: []uint16{suite.id},
1661 Certificates: []Certificate{cert},
David Benjamin68793732015-05-04 20:20:48 -04001662 PreSharedKey: []byte(psk),
David Benjamin48cae082014-10-27 01:06:24 -04001663 PreSharedKeyIdentity: pskIdentity,
Adam Langley95c29f32014-06-20 12:00:00 -07001664 },
David Benjamin48cae082014-10-27 01:06:24 -04001665 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001666 resumeSession: true,
Adam Langley95c29f32014-06-20 12:00:00 -07001667 })
David Benjamin025b3d32014-07-01 19:53:04 -04001668
David Benjamin76d8abe2014-08-14 16:25:34 -04001669 testCases = append(testCases, testCase{
1670 testType: serverTest,
1671 name: ver.name + "-" + suite.name + "-server",
1672 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001673 MinVersion: ver.version,
1674 MaxVersion: ver.version,
1675 CipherSuites: []uint16{suite.id},
1676 Certificates: []Certificate{cert},
1677 PreSharedKey: []byte(psk),
1678 PreSharedKeyIdentity: pskIdentity,
David Benjamin76d8abe2014-08-14 16:25:34 -04001679 },
1680 certFile: certFile,
1681 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001682 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001683 resumeSession: true,
David Benjamin76d8abe2014-08-14 16:25:34 -04001684 })
David Benjamin6fd297b2014-08-11 18:43:38 -04001685
David Benjamin8b8c0062014-11-23 02:47:52 -05001686 if ver.hasDTLS && isDTLSCipher(suite.name) {
David Benjamin6fd297b2014-08-11 18:43:38 -04001687 testCases = append(testCases, testCase{
1688 testType: clientTest,
1689 protocol: dtls,
1690 name: "D" + ver.name + "-" + suite.name + "-client",
1691 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001692 MinVersion: ver.version,
1693 MaxVersion: ver.version,
1694 CipherSuites: []uint16{suite.id},
1695 Certificates: []Certificate{cert},
1696 PreSharedKey: []byte(psk),
1697 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001698 },
David Benjamin48cae082014-10-27 01:06:24 -04001699 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001700 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001701 })
1702 testCases = append(testCases, testCase{
1703 testType: serverTest,
1704 protocol: dtls,
1705 name: "D" + ver.name + "-" + suite.name + "-server",
1706 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001707 MinVersion: ver.version,
1708 MaxVersion: ver.version,
1709 CipherSuites: []uint16{suite.id},
1710 Certificates: []Certificate{cert},
1711 PreSharedKey: []byte(psk),
1712 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001713 },
1714 certFile: certFile,
1715 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001716 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001717 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001718 })
1719 }
Adam Langley95c29f32014-06-20 12:00:00 -07001720 }
1721 }
Adam Langleya7997f12015-05-14 17:38:50 -07001722
1723 testCases = append(testCases, testCase{
1724 name: "WeakDH",
1725 config: Config{
1726 CipherSuites: []uint16{TLS_DHE_RSA_WITH_AES_128_GCM_SHA256},
1727 Bugs: ProtocolBugs{
1728 // This is a 1023-bit prime number, generated
1729 // with:
1730 // openssl gendh 1023 | openssl asn1parse -i
1731 DHGroupPrime: bigFromHex("518E9B7930CE61C6E445C8360584E5FC78D9137C0FFDC880B495D5338ADF7689951A6821C17A76B3ACB8E0156AEA607B7EC406EBEDBB84D8376EB8FE8F8BA1433488BEE0C3EDDFD3A32DBB9481980A7AF6C96BFCF490A094CFFB2B8192C1BB5510B77B658436E27C2D4D023FE3718222AB0CA1273995B51F6D625A4944D0DD4B"),
1732 },
1733 },
1734 shouldFail: true,
1735 expectedError: "BAD_DH_P_LENGTH",
1736 })
Adam Langley95c29f32014-06-20 12:00:00 -07001737}
1738
1739func addBadECDSASignatureTests() {
1740 for badR := BadValue(1); badR < NumBadValues; badR++ {
1741 for badS := BadValue(1); badS < NumBadValues; badS++ {
David Benjamin025b3d32014-07-01 19:53:04 -04001742 testCases = append(testCases, testCase{
Adam Langley95c29f32014-06-20 12:00:00 -07001743 name: fmt.Sprintf("BadECDSA-%d-%d", badR, badS),
1744 config: Config{
1745 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
1746 Certificates: []Certificate{getECDSACertificate()},
1747 Bugs: ProtocolBugs{
1748 BadECDSAR: badR,
1749 BadECDSAS: badS,
1750 },
1751 },
1752 shouldFail: true,
1753 expectedError: "SIGNATURE",
1754 })
1755 }
1756 }
1757}
1758
Adam Langley80842bd2014-06-20 12:00:00 -07001759func addCBCPaddingTests() {
David Benjamin025b3d32014-07-01 19:53:04 -04001760 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001761 name: "MaxCBCPadding",
1762 config: Config{
1763 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1764 Bugs: ProtocolBugs{
1765 MaxPadding: true,
1766 },
1767 },
1768 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1769 })
David Benjamin025b3d32014-07-01 19:53:04 -04001770 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001771 name: "BadCBCPadding",
1772 config: Config{
1773 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1774 Bugs: ProtocolBugs{
1775 PaddingFirstByteBad: true,
1776 },
1777 },
1778 shouldFail: true,
1779 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1780 })
1781 // OpenSSL previously had an issue where the first byte of padding in
1782 // 255 bytes of padding wasn't checked.
David Benjamin025b3d32014-07-01 19:53:04 -04001783 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001784 name: "BadCBCPadding255",
1785 config: Config{
1786 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1787 Bugs: ProtocolBugs{
1788 MaxPadding: true,
1789 PaddingFirstByteBadIf255: true,
1790 },
1791 },
1792 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1793 shouldFail: true,
1794 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1795 })
1796}
1797
Kenny Root7fdeaf12014-08-05 15:23:37 -07001798func addCBCSplittingTests() {
1799 testCases = append(testCases, testCase{
1800 name: "CBCRecordSplitting",
1801 config: Config{
1802 MaxVersion: VersionTLS10,
1803 MinVersion: VersionTLS10,
1804 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1805 },
1806 messageLen: -1, // read until EOF
1807 flags: []string{
1808 "-async",
1809 "-write-different-record-sizes",
1810 "-cbc-record-splitting",
1811 },
David Benjamina8e3e0e2014-08-06 22:11:10 -04001812 })
1813 testCases = append(testCases, testCase{
Kenny Root7fdeaf12014-08-05 15:23:37 -07001814 name: "CBCRecordSplittingPartialWrite",
1815 config: Config{
1816 MaxVersion: VersionTLS10,
1817 MinVersion: VersionTLS10,
1818 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1819 },
1820 messageLen: -1, // read until EOF
1821 flags: []string{
1822 "-async",
1823 "-write-different-record-sizes",
1824 "-cbc-record-splitting",
1825 "-partial-write",
1826 },
1827 })
1828}
1829
David Benjamin636293b2014-07-08 17:59:18 -04001830func addClientAuthTests() {
David Benjamin407a10c2014-07-16 12:58:59 -04001831 // Add a dummy cert pool to stress certificate authority parsing.
1832 // TODO(davidben): Add tests that those values parse out correctly.
1833 certPool := x509.NewCertPool()
1834 cert, err := x509.ParseCertificate(rsaCertificate.Certificate[0])
1835 if err != nil {
1836 panic(err)
1837 }
1838 certPool.AddCert(cert)
1839
David Benjamin636293b2014-07-08 17:59:18 -04001840 for _, ver := range tlsVersions {
David Benjamin636293b2014-07-08 17:59:18 -04001841 testCases = append(testCases, testCase{
1842 testType: clientTest,
David Benjamin67666e72014-07-12 15:47:52 -04001843 name: ver.name + "-Client-ClientAuth-RSA",
David Benjamin636293b2014-07-08 17:59:18 -04001844 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001845 MinVersion: ver.version,
1846 MaxVersion: ver.version,
1847 ClientAuth: RequireAnyClientCert,
1848 ClientCAs: certPool,
David Benjamin636293b2014-07-08 17:59:18 -04001849 },
1850 flags: []string{
1851 "-cert-file", rsaCertificateFile,
1852 "-key-file", rsaKeyFile,
1853 },
1854 })
1855 testCases = append(testCases, testCase{
David Benjamin67666e72014-07-12 15:47:52 -04001856 testType: serverTest,
1857 name: ver.name + "-Server-ClientAuth-RSA",
1858 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001859 MinVersion: ver.version,
1860 MaxVersion: ver.version,
David Benjamin67666e72014-07-12 15:47:52 -04001861 Certificates: []Certificate{rsaCertificate},
1862 },
1863 flags: []string{"-require-any-client-certificate"},
1864 })
David Benjamine098ec22014-08-27 23:13:20 -04001865 if ver.version != VersionSSL30 {
1866 testCases = append(testCases, testCase{
1867 testType: serverTest,
1868 name: ver.name + "-Server-ClientAuth-ECDSA",
1869 config: Config{
1870 MinVersion: ver.version,
1871 MaxVersion: ver.version,
1872 Certificates: []Certificate{ecdsaCertificate},
1873 },
1874 flags: []string{"-require-any-client-certificate"},
1875 })
1876 testCases = append(testCases, testCase{
1877 testType: clientTest,
1878 name: ver.name + "-Client-ClientAuth-ECDSA",
1879 config: Config{
1880 MinVersion: ver.version,
1881 MaxVersion: ver.version,
1882 ClientAuth: RequireAnyClientCert,
1883 ClientCAs: certPool,
1884 },
1885 flags: []string{
1886 "-cert-file", ecdsaCertificateFile,
1887 "-key-file", ecdsaKeyFile,
1888 },
1889 })
1890 }
David Benjamin636293b2014-07-08 17:59:18 -04001891 }
1892}
1893
Adam Langley75712922014-10-10 16:23:43 -07001894func addExtendedMasterSecretTests() {
1895 const expectEMSFlag = "-expect-extended-master-secret"
1896
1897 for _, with := range []bool{false, true} {
1898 prefix := "No"
1899 var flags []string
1900 if with {
1901 prefix = ""
1902 flags = []string{expectEMSFlag}
1903 }
1904
1905 for _, isClient := range []bool{false, true} {
1906 suffix := "-Server"
1907 testType := serverTest
1908 if isClient {
1909 suffix = "-Client"
1910 testType = clientTest
1911 }
1912
1913 for _, ver := range tlsVersions {
1914 test := testCase{
1915 testType: testType,
1916 name: prefix + "ExtendedMasterSecret-" + ver.name + suffix,
1917 config: Config{
1918 MinVersion: ver.version,
1919 MaxVersion: ver.version,
1920 Bugs: ProtocolBugs{
1921 NoExtendedMasterSecret: !with,
1922 RequireExtendedMasterSecret: with,
1923 },
1924 },
David Benjamin48cae082014-10-27 01:06:24 -04001925 flags: flags,
1926 shouldFail: ver.version == VersionSSL30 && with,
Adam Langley75712922014-10-10 16:23:43 -07001927 }
1928 if test.shouldFail {
1929 test.expectedLocalError = "extended master secret required but not supported by peer"
1930 }
1931 testCases = append(testCases, test)
1932 }
1933 }
1934 }
1935
1936 // When a session is resumed, it should still be aware that its master
1937 // secret was generated via EMS and thus it's safe to use tls-unique.
1938 testCases = append(testCases, testCase{
1939 name: "ExtendedMasterSecret-Resume",
1940 config: Config{
1941 Bugs: ProtocolBugs{
1942 RequireExtendedMasterSecret: true,
1943 },
1944 },
1945 flags: []string{expectEMSFlag},
1946 resumeSession: true,
1947 })
1948}
1949
David Benjamin43ec06f2014-08-05 02:28:57 -04001950// Adds tests that try to cover the range of the handshake state machine, under
1951// various conditions. Some of these are redundant with other tests, but they
1952// only cover the synchronous case.
David Benjamin6fd297b2014-08-11 18:43:38 -04001953func addStateMachineCoverageTests(async, splitHandshake bool, protocol protocol) {
David Benjamin760b1dd2015-05-15 23:33:48 -04001954 var tests []testCase
1955
1956 // Basic handshake, with resumption. Client and server,
1957 // session ID and session ticket.
1958 tests = append(tests, testCase{
1959 name: "Basic-Client",
1960 resumeSession: true,
1961 })
1962 tests = append(tests, testCase{
1963 name: "Basic-Client-RenewTicket",
1964 config: Config{
1965 Bugs: ProtocolBugs{
1966 RenewTicketOnResume: true,
1967 },
1968 },
1969 resumeSession: true,
1970 })
1971 tests = append(tests, testCase{
1972 name: "Basic-Client-NoTicket",
1973 config: Config{
1974 SessionTicketsDisabled: true,
1975 },
1976 resumeSession: true,
1977 })
1978 tests = append(tests, testCase{
1979 name: "Basic-Client-Implicit",
1980 flags: []string{"-implicit-handshake"},
1981 resumeSession: true,
1982 })
1983 tests = append(tests, testCase{
1984 testType: serverTest,
1985 name: "Basic-Server",
1986 resumeSession: true,
1987 })
1988 tests = append(tests, testCase{
1989 testType: serverTest,
1990 name: "Basic-Server-NoTickets",
1991 config: Config{
1992 SessionTicketsDisabled: true,
1993 },
1994 resumeSession: true,
1995 })
1996 tests = append(tests, testCase{
1997 testType: serverTest,
1998 name: "Basic-Server-Implicit",
1999 flags: []string{"-implicit-handshake"},
2000 resumeSession: true,
2001 })
2002 tests = append(tests, testCase{
2003 testType: serverTest,
2004 name: "Basic-Server-EarlyCallback",
2005 flags: []string{"-use-early-callback"},
2006 resumeSession: true,
2007 })
2008
2009 // TLS client auth.
2010 tests = append(tests, testCase{
2011 testType: clientTest,
2012 name: "ClientAuth-Client",
2013 config: Config{
2014 ClientAuth: RequireAnyClientCert,
2015 },
2016 flags: []string{
2017 "-cert-file", rsaCertificateFile,
2018 "-key-file", rsaKeyFile,
2019 },
2020 })
2021 tests = append(tests, testCase{
2022 testType: serverTest,
2023 name: "ClientAuth-Server",
2024 config: Config{
2025 Certificates: []Certificate{rsaCertificate},
2026 },
2027 flags: []string{"-require-any-client-certificate"},
2028 })
2029
2030 // No session ticket support; server doesn't send NewSessionTicket.
2031 tests = append(tests, testCase{
2032 name: "SessionTicketsDisabled-Client",
2033 config: Config{
2034 SessionTicketsDisabled: true,
2035 },
2036 })
2037 tests = append(tests, testCase{
2038 testType: serverTest,
2039 name: "SessionTicketsDisabled-Server",
2040 config: Config{
2041 SessionTicketsDisabled: true,
2042 },
2043 })
2044
2045 // Skip ServerKeyExchange in PSK key exchange if there's no
2046 // identity hint.
2047 tests = append(tests, testCase{
2048 name: "EmptyPSKHint-Client",
2049 config: Config{
2050 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
2051 PreSharedKey: []byte("secret"),
2052 },
2053 flags: []string{"-psk", "secret"},
2054 })
2055 tests = append(tests, testCase{
2056 testType: serverTest,
2057 name: "EmptyPSKHint-Server",
2058 config: Config{
2059 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
2060 PreSharedKey: []byte("secret"),
2061 },
2062 flags: []string{"-psk", "secret"},
2063 })
2064
2065 if protocol == tls {
2066 tests = append(tests, testCase{
2067 name: "Renegotiate-Client",
2068 renegotiate: true,
2069 })
2070 // NPN on client and server; results in post-handshake message.
2071 tests = append(tests, testCase{
2072 name: "NPN-Client",
2073 config: Config{
2074 NextProtos: []string{"foo"},
2075 },
2076 flags: []string{"-select-next-proto", "foo"},
2077 expectedNextProto: "foo",
2078 expectedNextProtoType: npn,
2079 })
2080 tests = append(tests, testCase{
2081 testType: serverTest,
2082 name: "NPN-Server",
2083 config: Config{
2084 NextProtos: []string{"bar"},
2085 },
2086 flags: []string{
2087 "-advertise-npn", "\x03foo\x03bar\x03baz",
2088 "-expect-next-proto", "bar",
2089 },
2090 expectedNextProto: "bar",
2091 expectedNextProtoType: npn,
2092 })
2093
2094 // TODO(davidben): Add tests for when False Start doesn't trigger.
2095
2096 // Client does False Start and negotiates NPN.
2097 tests = append(tests, testCase{
2098 name: "FalseStart",
2099 config: Config{
2100 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2101 NextProtos: []string{"foo"},
2102 Bugs: ProtocolBugs{
2103 ExpectFalseStart: true,
2104 },
2105 },
2106 flags: []string{
2107 "-false-start",
2108 "-select-next-proto", "foo",
2109 },
2110 shimWritesFirst: true,
2111 resumeSession: true,
2112 })
2113
2114 // Client does False Start and negotiates ALPN.
2115 tests = append(tests, testCase{
2116 name: "FalseStart-ALPN",
2117 config: Config{
2118 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2119 NextProtos: []string{"foo"},
2120 Bugs: ProtocolBugs{
2121 ExpectFalseStart: true,
2122 },
2123 },
2124 flags: []string{
2125 "-false-start",
2126 "-advertise-alpn", "\x03foo",
2127 },
2128 shimWritesFirst: true,
2129 resumeSession: true,
2130 })
2131
2132 // Client does False Start but doesn't explicitly call
2133 // SSL_connect.
2134 tests = append(tests, testCase{
2135 name: "FalseStart-Implicit",
2136 config: Config{
2137 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2138 NextProtos: []string{"foo"},
2139 },
2140 flags: []string{
2141 "-implicit-handshake",
2142 "-false-start",
2143 "-advertise-alpn", "\x03foo",
2144 },
2145 })
2146
2147 // False Start without session tickets.
2148 tests = append(tests, testCase{
2149 name: "FalseStart-SessionTicketsDisabled",
2150 config: Config{
2151 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2152 NextProtos: []string{"foo"},
2153 SessionTicketsDisabled: true,
2154 Bugs: ProtocolBugs{
2155 ExpectFalseStart: true,
2156 },
2157 },
2158 flags: []string{
2159 "-false-start",
2160 "-select-next-proto", "foo",
2161 },
2162 shimWritesFirst: true,
2163 })
2164
2165 // Server parses a V2ClientHello.
2166 tests = append(tests, testCase{
2167 testType: serverTest,
2168 name: "SendV2ClientHello",
2169 config: Config{
2170 // Choose a cipher suite that does not involve
2171 // elliptic curves, so no extensions are
2172 // involved.
2173 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2174 Bugs: ProtocolBugs{
2175 SendV2ClientHello: true,
2176 },
2177 },
2178 })
2179
2180 // Client sends a Channel ID.
2181 tests = append(tests, testCase{
2182 name: "ChannelID-Client",
2183 config: Config{
2184 RequestChannelID: true,
2185 },
2186 flags: []string{"-send-channel-id", channelIDKeyFile},
2187 resumeSession: true,
2188 expectChannelID: true,
2189 })
2190
2191 // Server accepts a Channel ID.
2192 tests = append(tests, testCase{
2193 testType: serverTest,
2194 name: "ChannelID-Server",
2195 config: Config{
2196 ChannelID: channelIDKey,
2197 },
2198 flags: []string{
2199 "-expect-channel-id",
2200 base64.StdEncoding.EncodeToString(channelIDBytes),
2201 },
2202 resumeSession: true,
2203 expectChannelID: true,
2204 })
2205 } else {
2206 tests = append(tests, testCase{
2207 name: "SkipHelloVerifyRequest",
2208 config: Config{
2209 Bugs: ProtocolBugs{
2210 SkipHelloVerifyRequest: true,
2211 },
2212 },
2213 })
2214 }
2215
David Benjamin43ec06f2014-08-05 02:28:57 -04002216 var suffix string
2217 var flags []string
2218 var maxHandshakeRecordLength int
David Benjamin6fd297b2014-08-11 18:43:38 -04002219 if protocol == dtls {
2220 suffix = "-DTLS"
2221 }
David Benjamin43ec06f2014-08-05 02:28:57 -04002222 if async {
David Benjamin6fd297b2014-08-11 18:43:38 -04002223 suffix += "-Async"
David Benjamin43ec06f2014-08-05 02:28:57 -04002224 flags = append(flags, "-async")
2225 } else {
David Benjamin6fd297b2014-08-11 18:43:38 -04002226 suffix += "-Sync"
David Benjamin43ec06f2014-08-05 02:28:57 -04002227 }
2228 if splitHandshake {
2229 suffix += "-SplitHandshakeRecords"
David Benjamin98214542014-08-07 18:02:39 -04002230 maxHandshakeRecordLength = 1
David Benjamin43ec06f2014-08-05 02:28:57 -04002231 }
David Benjamin760b1dd2015-05-15 23:33:48 -04002232 for _, test := range tests {
2233 test.protocol = protocol
2234 test.name += suffix
2235 test.config.Bugs.MaxHandshakeRecordLength = maxHandshakeRecordLength
2236 test.flags = append(test.flags, flags...)
2237 testCases = append(testCases, test)
David Benjamin6fd297b2014-08-11 18:43:38 -04002238 }
David Benjamin43ec06f2014-08-05 02:28:57 -04002239}
2240
Adam Langley524e7172015-02-20 16:04:00 -08002241func addDDoSCallbackTests() {
2242 // DDoS callback.
2243
2244 for _, resume := range []bool{false, true} {
2245 suffix := "Resume"
2246 if resume {
2247 suffix = "No" + suffix
2248 }
2249
2250 testCases = append(testCases, testCase{
2251 testType: serverTest,
2252 name: "Server-DDoS-OK-" + suffix,
2253 flags: []string{"-install-ddos-callback"},
2254 resumeSession: resume,
2255 })
2256
2257 failFlag := "-fail-ddos-callback"
2258 if resume {
2259 failFlag = "-fail-second-ddos-callback"
2260 }
2261 testCases = append(testCases, testCase{
2262 testType: serverTest,
2263 name: "Server-DDoS-Reject-" + suffix,
2264 flags: []string{"-install-ddos-callback", failFlag},
2265 resumeSession: resume,
2266 shouldFail: true,
2267 expectedError: ":CONNECTION_REJECTED:",
2268 })
2269 }
2270}
2271
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002272func addVersionNegotiationTests() {
2273 for i, shimVers := range tlsVersions {
2274 // Assemble flags to disable all newer versions on the shim.
2275 var flags []string
2276 for _, vers := range tlsVersions[i+1:] {
2277 flags = append(flags, vers.flag)
2278 }
2279
2280 for _, runnerVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05002281 protocols := []protocol{tls}
2282 if runnerVers.hasDTLS && shimVers.hasDTLS {
2283 protocols = append(protocols, dtls)
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002284 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002285 for _, protocol := range protocols {
2286 expectedVersion := shimVers.version
2287 if runnerVers.version < shimVers.version {
2288 expectedVersion = runnerVers.version
2289 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002290
David Benjamin8b8c0062014-11-23 02:47:52 -05002291 suffix := shimVers.name + "-" + runnerVers.name
2292 if protocol == dtls {
2293 suffix += "-DTLS"
2294 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002295
David Benjamin1eb367c2014-12-12 18:17:51 -05002296 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
2297
David Benjamin1e29a6b2014-12-10 02:27:24 -05002298 clientVers := shimVers.version
2299 if clientVers > VersionTLS10 {
2300 clientVers = VersionTLS10
2301 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002302 testCases = append(testCases, testCase{
2303 protocol: protocol,
2304 testType: clientTest,
2305 name: "VersionNegotiation-Client-" + suffix,
2306 config: Config{
2307 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05002308 Bugs: ProtocolBugs{
2309 ExpectInitialRecordVersion: clientVers,
2310 },
David Benjamin8b8c0062014-11-23 02:47:52 -05002311 },
2312 flags: flags,
2313 expectedVersion: expectedVersion,
2314 })
David Benjamin1eb367c2014-12-12 18:17:51 -05002315 testCases = append(testCases, testCase{
2316 protocol: protocol,
2317 testType: clientTest,
2318 name: "VersionNegotiation-Client2-" + suffix,
2319 config: Config{
2320 MaxVersion: runnerVers.version,
2321 Bugs: ProtocolBugs{
2322 ExpectInitialRecordVersion: clientVers,
2323 },
2324 },
2325 flags: []string{"-max-version", shimVersFlag},
2326 expectedVersion: expectedVersion,
2327 })
David Benjamin8b8c0062014-11-23 02:47:52 -05002328
2329 testCases = append(testCases, testCase{
2330 protocol: protocol,
2331 testType: serverTest,
2332 name: "VersionNegotiation-Server-" + suffix,
2333 config: Config{
2334 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05002335 Bugs: ProtocolBugs{
2336 ExpectInitialRecordVersion: expectedVersion,
2337 },
David Benjamin8b8c0062014-11-23 02:47:52 -05002338 },
2339 flags: flags,
2340 expectedVersion: expectedVersion,
2341 })
David Benjamin1eb367c2014-12-12 18:17:51 -05002342 testCases = append(testCases, testCase{
2343 protocol: protocol,
2344 testType: serverTest,
2345 name: "VersionNegotiation-Server2-" + suffix,
2346 config: Config{
2347 MaxVersion: runnerVers.version,
2348 Bugs: ProtocolBugs{
2349 ExpectInitialRecordVersion: expectedVersion,
2350 },
2351 },
2352 flags: []string{"-max-version", shimVersFlag},
2353 expectedVersion: expectedVersion,
2354 })
David Benjamin8b8c0062014-11-23 02:47:52 -05002355 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002356 }
2357 }
2358}
2359
David Benjaminaccb4542014-12-12 23:44:33 -05002360func addMinimumVersionTests() {
2361 for i, shimVers := range tlsVersions {
2362 // Assemble flags to disable all older versions on the shim.
2363 var flags []string
2364 for _, vers := range tlsVersions[:i] {
2365 flags = append(flags, vers.flag)
2366 }
2367
2368 for _, runnerVers := range tlsVersions {
2369 protocols := []protocol{tls}
2370 if runnerVers.hasDTLS && shimVers.hasDTLS {
2371 protocols = append(protocols, dtls)
2372 }
2373 for _, protocol := range protocols {
2374 suffix := shimVers.name + "-" + runnerVers.name
2375 if protocol == dtls {
2376 suffix += "-DTLS"
2377 }
2378 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
2379
David Benjaminaccb4542014-12-12 23:44:33 -05002380 var expectedVersion uint16
2381 var shouldFail bool
2382 var expectedError string
David Benjamin87909c02014-12-13 01:55:01 -05002383 var expectedLocalError string
David Benjaminaccb4542014-12-12 23:44:33 -05002384 if runnerVers.version >= shimVers.version {
2385 expectedVersion = runnerVers.version
2386 } else {
2387 shouldFail = true
2388 expectedError = ":UNSUPPORTED_PROTOCOL:"
David Benjamin87909c02014-12-13 01:55:01 -05002389 if runnerVers.version > VersionSSL30 {
2390 expectedLocalError = "remote error: protocol version not supported"
2391 } else {
2392 expectedLocalError = "remote error: handshake failure"
2393 }
David Benjaminaccb4542014-12-12 23:44:33 -05002394 }
2395
2396 testCases = append(testCases, testCase{
2397 protocol: protocol,
2398 testType: clientTest,
2399 name: "MinimumVersion-Client-" + suffix,
2400 config: Config{
2401 MaxVersion: runnerVers.version,
2402 },
David Benjamin87909c02014-12-13 01:55:01 -05002403 flags: flags,
2404 expectedVersion: expectedVersion,
2405 shouldFail: shouldFail,
2406 expectedError: expectedError,
2407 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002408 })
2409 testCases = append(testCases, testCase{
2410 protocol: protocol,
2411 testType: clientTest,
2412 name: "MinimumVersion-Client2-" + suffix,
2413 config: Config{
2414 MaxVersion: runnerVers.version,
2415 },
David Benjamin87909c02014-12-13 01:55:01 -05002416 flags: []string{"-min-version", shimVersFlag},
2417 expectedVersion: expectedVersion,
2418 shouldFail: shouldFail,
2419 expectedError: expectedError,
2420 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002421 })
2422
2423 testCases = append(testCases, testCase{
2424 protocol: protocol,
2425 testType: serverTest,
2426 name: "MinimumVersion-Server-" + suffix,
2427 config: Config{
2428 MaxVersion: runnerVers.version,
2429 },
David Benjamin87909c02014-12-13 01:55:01 -05002430 flags: flags,
2431 expectedVersion: expectedVersion,
2432 shouldFail: shouldFail,
2433 expectedError: expectedError,
2434 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002435 })
2436 testCases = append(testCases, testCase{
2437 protocol: protocol,
2438 testType: serverTest,
2439 name: "MinimumVersion-Server2-" + suffix,
2440 config: Config{
2441 MaxVersion: runnerVers.version,
2442 },
David Benjamin87909c02014-12-13 01:55:01 -05002443 flags: []string{"-min-version", shimVersFlag},
2444 expectedVersion: expectedVersion,
2445 shouldFail: shouldFail,
2446 expectedError: expectedError,
2447 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002448 })
2449 }
2450 }
2451 }
2452}
2453
David Benjamin5c24a1d2014-08-31 00:59:27 -04002454func addD5BugTests() {
2455 testCases = append(testCases, testCase{
2456 testType: serverTest,
2457 name: "D5Bug-NoQuirk-Reject",
2458 config: Config{
2459 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2460 Bugs: ProtocolBugs{
2461 SSL3RSAKeyExchange: true,
2462 },
2463 },
2464 shouldFail: true,
2465 expectedError: ":TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG:",
2466 })
2467 testCases = append(testCases, testCase{
2468 testType: serverTest,
2469 name: "D5Bug-Quirk-Normal",
2470 config: Config{
2471 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2472 },
2473 flags: []string{"-tls-d5-bug"},
2474 })
2475 testCases = append(testCases, testCase{
2476 testType: serverTest,
2477 name: "D5Bug-Quirk-Bug",
2478 config: Config{
2479 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2480 Bugs: ProtocolBugs{
2481 SSL3RSAKeyExchange: true,
2482 },
2483 },
2484 flags: []string{"-tls-d5-bug"},
2485 })
2486}
2487
David Benjamine78bfde2014-09-06 12:45:15 -04002488func addExtensionTests() {
2489 testCases = append(testCases, testCase{
2490 testType: clientTest,
2491 name: "DuplicateExtensionClient",
2492 config: Config{
2493 Bugs: ProtocolBugs{
2494 DuplicateExtension: true,
2495 },
2496 },
2497 shouldFail: true,
2498 expectedLocalError: "remote error: error decoding message",
2499 })
2500 testCases = append(testCases, testCase{
2501 testType: serverTest,
2502 name: "DuplicateExtensionServer",
2503 config: Config{
2504 Bugs: ProtocolBugs{
2505 DuplicateExtension: true,
2506 },
2507 },
2508 shouldFail: true,
2509 expectedLocalError: "remote error: error decoding message",
2510 })
2511 testCases = append(testCases, testCase{
2512 testType: clientTest,
2513 name: "ServerNameExtensionClient",
2514 config: Config{
2515 Bugs: ProtocolBugs{
2516 ExpectServerName: "example.com",
2517 },
2518 },
2519 flags: []string{"-host-name", "example.com"},
2520 })
2521 testCases = append(testCases, testCase{
2522 testType: clientTest,
David Benjamin5f237bc2015-02-11 17:14:15 -05002523 name: "ServerNameExtensionClientMismatch",
David Benjamine78bfde2014-09-06 12:45:15 -04002524 config: Config{
2525 Bugs: ProtocolBugs{
2526 ExpectServerName: "mismatch.com",
2527 },
2528 },
2529 flags: []string{"-host-name", "example.com"},
2530 shouldFail: true,
2531 expectedLocalError: "tls: unexpected server name",
2532 })
2533 testCases = append(testCases, testCase{
2534 testType: clientTest,
David Benjamin5f237bc2015-02-11 17:14:15 -05002535 name: "ServerNameExtensionClientMissing",
David Benjamine78bfde2014-09-06 12:45:15 -04002536 config: Config{
2537 Bugs: ProtocolBugs{
2538 ExpectServerName: "missing.com",
2539 },
2540 },
2541 shouldFail: true,
2542 expectedLocalError: "tls: unexpected server name",
2543 })
2544 testCases = append(testCases, testCase{
2545 testType: serverTest,
2546 name: "ServerNameExtensionServer",
2547 config: Config{
2548 ServerName: "example.com",
2549 },
2550 flags: []string{"-expect-server-name", "example.com"},
2551 resumeSession: true,
2552 })
David Benjaminae2888f2014-09-06 12:58:58 -04002553 testCases = append(testCases, testCase{
2554 testType: clientTest,
2555 name: "ALPNClient",
2556 config: Config{
2557 NextProtos: []string{"foo"},
2558 },
2559 flags: []string{
2560 "-advertise-alpn", "\x03foo\x03bar\x03baz",
2561 "-expect-alpn", "foo",
2562 },
David Benjaminfc7b0862014-09-06 13:21:53 -04002563 expectedNextProto: "foo",
2564 expectedNextProtoType: alpn,
2565 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04002566 })
2567 testCases = append(testCases, testCase{
2568 testType: serverTest,
2569 name: "ALPNServer",
2570 config: Config{
2571 NextProtos: []string{"foo", "bar", "baz"},
2572 },
2573 flags: []string{
2574 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2575 "-select-alpn", "foo",
2576 },
David Benjaminfc7b0862014-09-06 13:21:53 -04002577 expectedNextProto: "foo",
2578 expectedNextProtoType: alpn,
2579 resumeSession: true,
2580 })
2581 // Test that the server prefers ALPN over NPN.
2582 testCases = append(testCases, testCase{
2583 testType: serverTest,
2584 name: "ALPNServer-Preferred",
2585 config: Config{
2586 NextProtos: []string{"foo", "bar", "baz"},
2587 },
2588 flags: []string{
2589 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2590 "-select-alpn", "foo",
2591 "-advertise-npn", "\x03foo\x03bar\x03baz",
2592 },
2593 expectedNextProto: "foo",
2594 expectedNextProtoType: alpn,
2595 resumeSession: true,
2596 })
2597 testCases = append(testCases, testCase{
2598 testType: serverTest,
2599 name: "ALPNServer-Preferred-Swapped",
2600 config: Config{
2601 NextProtos: []string{"foo", "bar", "baz"},
2602 Bugs: ProtocolBugs{
2603 SwapNPNAndALPN: true,
2604 },
2605 },
2606 flags: []string{
2607 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2608 "-select-alpn", "foo",
2609 "-advertise-npn", "\x03foo\x03bar\x03baz",
2610 },
2611 expectedNextProto: "foo",
2612 expectedNextProtoType: alpn,
2613 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04002614 })
Adam Langley38311732014-10-16 19:04:35 -07002615 // Resume with a corrupt ticket.
2616 testCases = append(testCases, testCase{
2617 testType: serverTest,
2618 name: "CorruptTicket",
2619 config: Config{
2620 Bugs: ProtocolBugs{
2621 CorruptTicket: true,
2622 },
2623 },
2624 resumeSession: true,
2625 flags: []string{"-expect-session-miss"},
2626 })
2627 // Resume with an oversized session id.
2628 testCases = append(testCases, testCase{
2629 testType: serverTest,
2630 name: "OversizedSessionId",
2631 config: Config{
2632 Bugs: ProtocolBugs{
2633 OversizedSessionId: true,
2634 },
2635 },
2636 resumeSession: true,
Adam Langley75712922014-10-10 16:23:43 -07002637 shouldFail: true,
Adam Langley38311732014-10-16 19:04:35 -07002638 expectedError: ":DECODE_ERROR:",
2639 })
David Benjaminca6c8262014-11-15 19:06:08 -05002640 // Basic DTLS-SRTP tests. Include fake profiles to ensure they
2641 // are ignored.
2642 testCases = append(testCases, testCase{
2643 protocol: dtls,
2644 name: "SRTP-Client",
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 testCases = append(testCases, testCase{
2655 protocol: dtls,
2656 testType: serverTest,
2657 name: "SRTP-Server",
2658 config: Config{
2659 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
2660 },
2661 flags: []string{
2662 "-srtp-profiles",
2663 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2664 },
2665 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2666 })
2667 // Test that the MKI is ignored.
2668 testCases = append(testCases, testCase{
2669 protocol: dtls,
2670 testType: serverTest,
2671 name: "SRTP-Server-IgnoreMKI",
2672 config: Config{
2673 SRTPProtectionProfiles: []uint16{SRTP_AES128_CM_HMAC_SHA1_80},
2674 Bugs: ProtocolBugs{
2675 SRTPMasterKeyIdentifer: "bogus",
2676 },
2677 },
2678 flags: []string{
2679 "-srtp-profiles",
2680 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2681 },
2682 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2683 })
2684 // Test that SRTP isn't negotiated on the server if there were
2685 // no matching profiles.
2686 testCases = append(testCases, testCase{
2687 protocol: dtls,
2688 testType: serverTest,
2689 name: "SRTP-Server-NoMatch",
2690 config: Config{
2691 SRTPProtectionProfiles: []uint16{100, 101, 102},
2692 },
2693 flags: []string{
2694 "-srtp-profiles",
2695 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2696 },
2697 expectedSRTPProtectionProfile: 0,
2698 })
2699 // Test that the server returning an invalid SRTP profile is
2700 // flagged as an error by the client.
2701 testCases = append(testCases, testCase{
2702 protocol: dtls,
2703 name: "SRTP-Client-NoMatch",
2704 config: Config{
2705 Bugs: ProtocolBugs{
2706 SendSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_32,
2707 },
2708 },
2709 flags: []string{
2710 "-srtp-profiles",
2711 "SRTP_AES128_CM_SHA1_80",
2712 },
2713 shouldFail: true,
2714 expectedError: ":BAD_SRTP_PROTECTION_PROFILE_LIST:",
2715 })
David Benjamin61f95272014-11-25 01:55:35 -05002716 // Test OCSP stapling and SCT list.
2717 testCases = append(testCases, testCase{
2718 name: "OCSPStapling",
2719 flags: []string{
2720 "-enable-ocsp-stapling",
2721 "-expect-ocsp-response",
2722 base64.StdEncoding.EncodeToString(testOCSPResponse),
2723 },
2724 })
2725 testCases = append(testCases, testCase{
2726 name: "SignedCertificateTimestampList",
2727 flags: []string{
2728 "-enable-signed-cert-timestamps",
2729 "-expect-signed-cert-timestamps",
2730 base64.StdEncoding.EncodeToString(testSCTList),
2731 },
2732 })
David Benjamine78bfde2014-09-06 12:45:15 -04002733}
2734
David Benjamin01fe8202014-09-24 15:21:44 -04002735func addResumptionVersionTests() {
David Benjamin01fe8202014-09-24 15:21:44 -04002736 for _, sessionVers := range tlsVersions {
David Benjamin01fe8202014-09-24 15:21:44 -04002737 for _, resumeVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05002738 protocols := []protocol{tls}
2739 if sessionVers.hasDTLS && resumeVers.hasDTLS {
2740 protocols = append(protocols, dtls)
David Benjaminbdf5e722014-11-11 00:52:15 -05002741 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002742 for _, protocol := range protocols {
2743 suffix := "-" + sessionVers.name + "-" + resumeVers.name
2744 if protocol == dtls {
2745 suffix += "-DTLS"
2746 }
2747
David Benjaminece3de92015-03-16 18:02:20 -04002748 if sessionVers.version == resumeVers.version {
2749 testCases = append(testCases, testCase{
2750 protocol: protocol,
2751 name: "Resume-Client" + suffix,
2752 resumeSession: true,
2753 config: Config{
2754 MaxVersion: sessionVers.version,
2755 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
David Benjamin8b8c0062014-11-23 02:47:52 -05002756 },
David Benjaminece3de92015-03-16 18:02:20 -04002757 expectedVersion: sessionVers.version,
2758 expectedResumeVersion: resumeVers.version,
2759 })
2760 } else {
2761 testCases = append(testCases, testCase{
2762 protocol: protocol,
2763 name: "Resume-Client-Mismatch" + suffix,
2764 resumeSession: true,
2765 config: Config{
2766 MaxVersion: sessionVers.version,
2767 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
David Benjamin8b8c0062014-11-23 02:47:52 -05002768 },
David Benjaminece3de92015-03-16 18:02:20 -04002769 expectedVersion: sessionVers.version,
2770 resumeConfig: &Config{
2771 MaxVersion: resumeVers.version,
2772 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2773 Bugs: ProtocolBugs{
2774 AllowSessionVersionMismatch: true,
2775 },
2776 },
2777 expectedResumeVersion: resumeVers.version,
2778 shouldFail: true,
2779 expectedError: ":OLD_SESSION_VERSION_NOT_RETURNED:",
2780 })
2781 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002782
2783 testCases = append(testCases, testCase{
2784 protocol: protocol,
2785 name: "Resume-Client-NoResume" + suffix,
2786 flags: []string{"-expect-session-miss"},
2787 resumeSession: true,
2788 config: Config{
2789 MaxVersion: sessionVers.version,
2790 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2791 },
2792 expectedVersion: sessionVers.version,
2793 resumeConfig: &Config{
2794 MaxVersion: resumeVers.version,
2795 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2796 },
2797 newSessionsOnResume: true,
2798 expectedResumeVersion: resumeVers.version,
2799 })
2800
2801 var flags []string
2802 if sessionVers.version != resumeVers.version {
2803 flags = append(flags, "-expect-session-miss")
2804 }
2805 testCases = append(testCases, testCase{
2806 protocol: protocol,
2807 testType: serverTest,
2808 name: "Resume-Server" + suffix,
2809 flags: flags,
2810 resumeSession: true,
2811 config: Config{
2812 MaxVersion: sessionVers.version,
2813 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2814 },
2815 expectedVersion: sessionVers.version,
2816 resumeConfig: &Config{
2817 MaxVersion: resumeVers.version,
2818 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2819 },
2820 expectedResumeVersion: resumeVers.version,
2821 })
2822 }
David Benjamin01fe8202014-09-24 15:21:44 -04002823 }
2824 }
David Benjaminece3de92015-03-16 18:02:20 -04002825
2826 testCases = append(testCases, testCase{
2827 name: "Resume-Client-CipherMismatch",
2828 resumeSession: true,
2829 config: Config{
2830 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2831 },
2832 resumeConfig: &Config{
2833 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2834 Bugs: ProtocolBugs{
2835 SendCipherSuite: TLS_RSA_WITH_AES_128_CBC_SHA,
2836 },
2837 },
2838 shouldFail: true,
2839 expectedError: ":OLD_SESSION_CIPHER_NOT_RETURNED:",
2840 })
David Benjamin01fe8202014-09-24 15:21:44 -04002841}
2842
Adam Langley2ae77d22014-10-28 17:29:33 -07002843func addRenegotiationTests() {
David Benjamin44d3eed2015-05-21 01:29:55 -04002844 // Servers cannot renegotiate.
David Benjaminb16346b2015-04-08 19:16:58 -04002845 testCases = append(testCases, testCase{
2846 testType: serverTest,
David Benjamin44d3eed2015-05-21 01:29:55 -04002847 name: "Renegotiate-Server-Forbidden",
David Benjaminb16346b2015-04-08 19:16:58 -04002848 renegotiate: true,
2849 flags: []string{"-reject-peer-renegotiations"},
2850 shouldFail: true,
2851 expectedError: ":NO_RENEGOTIATION:",
2852 expectedLocalError: "remote error: no renegotiation",
2853 })
Adam Langley2ae77d22014-10-28 17:29:33 -07002854 // TODO(agl): test the renegotiation info SCSV.
Adam Langleycf2d4f42014-10-28 19:06:14 -07002855 testCases = append(testCases, testCase{
David Benjamin4b27d9f2015-05-12 22:42:52 -04002856 name: "Renegotiate-Client",
David Benjamincdea40c2015-03-19 14:09:43 -04002857 config: Config{
2858 Bugs: ProtocolBugs{
David Benjamin4b27d9f2015-05-12 22:42:52 -04002859 FailIfResumeOnRenego: true,
David Benjamincdea40c2015-03-19 14:09:43 -04002860 },
2861 },
2862 renegotiate: true,
2863 })
2864 testCases = append(testCases, testCase{
Adam Langleycf2d4f42014-10-28 19:06:14 -07002865 name: "Renegotiate-Client-EmptyExt",
2866 renegotiate: true,
2867 config: Config{
2868 Bugs: ProtocolBugs{
2869 EmptyRenegotiationInfo: true,
2870 },
2871 },
2872 shouldFail: true,
2873 expectedError: ":RENEGOTIATION_MISMATCH:",
2874 })
2875 testCases = append(testCases, testCase{
2876 name: "Renegotiate-Client-BadExt",
2877 renegotiate: true,
2878 config: Config{
2879 Bugs: ProtocolBugs{
2880 BadRenegotiationInfo: true,
2881 },
2882 },
2883 shouldFail: true,
2884 expectedError: ":RENEGOTIATION_MISMATCH:",
2885 })
2886 testCases = append(testCases, testCase{
David Benjamincff0b902015-05-15 23:09:47 -04002887 name: "Renegotiate-Client-NoExt",
2888 renegotiate: true,
2889 config: Config{
2890 Bugs: ProtocolBugs{
2891 NoRenegotiationInfo: true,
2892 },
2893 },
2894 shouldFail: true,
2895 expectedError: ":UNSAFE_LEGACY_RENEGOTIATION_DISABLED:",
2896 flags: []string{"-no-legacy-server-connect"},
2897 })
2898 testCases = append(testCases, testCase{
2899 name: "Renegotiate-Client-NoExt-Allowed",
2900 renegotiate: true,
2901 config: Config{
2902 Bugs: ProtocolBugs{
2903 NoRenegotiationInfo: true,
2904 },
2905 },
2906 })
2907 testCases = append(testCases, testCase{
Adam Langleycf2d4f42014-10-28 19:06:14 -07002908 name: "Renegotiate-Client-SwitchCiphers",
2909 renegotiate: true,
2910 config: Config{
2911 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2912 },
2913 renegotiateCiphers: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2914 })
2915 testCases = append(testCases, testCase{
2916 name: "Renegotiate-Client-SwitchCiphers2",
2917 renegotiate: true,
2918 config: Config{
2919 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2920 },
2921 renegotiateCiphers: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2922 })
David Benjaminc44b1df2014-11-23 12:11:01 -05002923 testCases = append(testCases, testCase{
David Benjaminb16346b2015-04-08 19:16:58 -04002924 name: "Renegotiate-Client-Forbidden",
2925 renegotiate: true,
2926 flags: []string{"-reject-peer-renegotiations"},
2927 shouldFail: true,
2928 expectedError: ":NO_RENEGOTIATION:",
2929 expectedLocalError: "remote error: no renegotiation",
2930 })
2931 testCases = append(testCases, testCase{
David Benjaminc44b1df2014-11-23 12:11:01 -05002932 name: "Renegotiate-SameClientVersion",
2933 renegotiate: true,
2934 config: Config{
2935 MaxVersion: VersionTLS10,
2936 Bugs: ProtocolBugs{
2937 RequireSameRenegoClientVersion: true,
2938 },
2939 },
2940 })
Adam Langley2ae77d22014-10-28 17:29:33 -07002941}
2942
David Benjamin5e961c12014-11-07 01:48:35 -05002943func addDTLSReplayTests() {
2944 // Test that sequence number replays are detected.
2945 testCases = append(testCases, testCase{
2946 protocol: dtls,
2947 name: "DTLS-Replay",
2948 replayWrites: true,
2949 })
2950
2951 // Test the outgoing sequence number skipping by values larger
2952 // than the retransmit window.
2953 testCases = append(testCases, testCase{
2954 protocol: dtls,
2955 name: "DTLS-Replay-LargeGaps",
2956 config: Config{
2957 Bugs: ProtocolBugs{
2958 SequenceNumberIncrement: 127,
2959 },
2960 },
2961 replayWrites: true,
2962 })
2963}
2964
Feng Lu41aa3252014-11-21 22:47:56 -08002965func addFastRadioPaddingTests() {
David Benjamin1e29a6b2014-12-10 02:27:24 -05002966 testCases = append(testCases, testCase{
2967 protocol: tls,
2968 name: "FastRadio-Padding",
Feng Lu41aa3252014-11-21 22:47:56 -08002969 config: Config{
2970 Bugs: ProtocolBugs{
2971 RequireFastradioPadding: true,
2972 },
2973 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05002974 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08002975 })
David Benjamin1e29a6b2014-12-10 02:27:24 -05002976 testCases = append(testCases, testCase{
2977 protocol: dtls,
David Benjamin5f237bc2015-02-11 17:14:15 -05002978 name: "FastRadio-Padding-DTLS",
Feng Lu41aa3252014-11-21 22:47:56 -08002979 config: Config{
2980 Bugs: ProtocolBugs{
2981 RequireFastradioPadding: true,
2982 },
2983 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05002984 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08002985 })
2986}
2987
David Benjamin000800a2014-11-14 01:43:59 -05002988var testHashes = []struct {
2989 name string
2990 id uint8
2991}{
2992 {"SHA1", hashSHA1},
2993 {"SHA224", hashSHA224},
2994 {"SHA256", hashSHA256},
2995 {"SHA384", hashSHA384},
2996 {"SHA512", hashSHA512},
2997}
2998
2999func addSigningHashTests() {
3000 // Make sure each hash works. Include some fake hashes in the list and
3001 // ensure they're ignored.
3002 for _, hash := range testHashes {
3003 testCases = append(testCases, testCase{
3004 name: "SigningHash-ClientAuth-" + hash.name,
3005 config: Config{
3006 ClientAuth: RequireAnyClientCert,
3007 SignatureAndHashes: []signatureAndHash{
3008 {signatureRSA, 42},
3009 {signatureRSA, hash.id},
3010 {signatureRSA, 255},
3011 },
3012 },
3013 flags: []string{
3014 "-cert-file", rsaCertificateFile,
3015 "-key-file", rsaKeyFile,
3016 },
3017 })
3018
3019 testCases = append(testCases, testCase{
3020 testType: serverTest,
3021 name: "SigningHash-ServerKeyExchange-Sign-" + hash.name,
3022 config: Config{
3023 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
3024 SignatureAndHashes: []signatureAndHash{
3025 {signatureRSA, 42},
3026 {signatureRSA, hash.id},
3027 {signatureRSA, 255},
3028 },
3029 },
3030 })
3031 }
3032
3033 // Test that hash resolution takes the signature type into account.
3034 testCases = append(testCases, testCase{
3035 name: "SigningHash-ClientAuth-SignatureType",
3036 config: Config{
3037 ClientAuth: RequireAnyClientCert,
3038 SignatureAndHashes: []signatureAndHash{
3039 {signatureECDSA, hashSHA512},
3040 {signatureRSA, hashSHA384},
3041 {signatureECDSA, hashSHA1},
3042 },
3043 },
3044 flags: []string{
3045 "-cert-file", rsaCertificateFile,
3046 "-key-file", rsaKeyFile,
3047 },
3048 })
3049
3050 testCases = append(testCases, testCase{
3051 testType: serverTest,
3052 name: "SigningHash-ServerKeyExchange-SignatureType",
3053 config: Config{
3054 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
3055 SignatureAndHashes: []signatureAndHash{
3056 {signatureECDSA, hashSHA512},
3057 {signatureRSA, hashSHA384},
3058 {signatureECDSA, hashSHA1},
3059 },
3060 },
3061 })
3062
3063 // Test that, if the list is missing, the peer falls back to SHA-1.
3064 testCases = append(testCases, testCase{
3065 name: "SigningHash-ClientAuth-Fallback",
3066 config: Config{
3067 ClientAuth: RequireAnyClientCert,
3068 SignatureAndHashes: []signatureAndHash{
3069 {signatureRSA, hashSHA1},
3070 },
3071 Bugs: ProtocolBugs{
3072 NoSignatureAndHashes: true,
3073 },
3074 },
3075 flags: []string{
3076 "-cert-file", rsaCertificateFile,
3077 "-key-file", rsaKeyFile,
3078 },
3079 })
3080
3081 testCases = append(testCases, testCase{
3082 testType: serverTest,
3083 name: "SigningHash-ServerKeyExchange-Fallback",
3084 config: Config{
3085 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
3086 SignatureAndHashes: []signatureAndHash{
3087 {signatureRSA, hashSHA1},
3088 },
3089 Bugs: ProtocolBugs{
3090 NoSignatureAndHashes: true,
3091 },
3092 },
3093 })
David Benjamin72dc7832015-03-16 17:49:43 -04003094
3095 // Test that hash preferences are enforced. BoringSSL defaults to
3096 // rejecting MD5 signatures.
3097 testCases = append(testCases, testCase{
3098 testType: serverTest,
3099 name: "SigningHash-ClientAuth-Enforced",
3100 config: Config{
3101 Certificates: []Certificate{rsaCertificate},
3102 SignatureAndHashes: []signatureAndHash{
3103 {signatureRSA, hashMD5},
3104 // Advertise SHA-1 so the handshake will
3105 // proceed, but the shim's preferences will be
3106 // ignored in CertificateVerify generation, so
3107 // MD5 will be chosen.
3108 {signatureRSA, hashSHA1},
3109 },
3110 Bugs: ProtocolBugs{
3111 IgnorePeerSignatureAlgorithmPreferences: true,
3112 },
3113 },
3114 flags: []string{"-require-any-client-certificate"},
3115 shouldFail: true,
3116 expectedError: ":WRONG_SIGNATURE_TYPE:",
3117 })
3118
3119 testCases = append(testCases, testCase{
3120 name: "SigningHash-ServerKeyExchange-Enforced",
3121 config: Config{
3122 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
3123 SignatureAndHashes: []signatureAndHash{
3124 {signatureRSA, hashMD5},
3125 },
3126 Bugs: ProtocolBugs{
3127 IgnorePeerSignatureAlgorithmPreferences: true,
3128 },
3129 },
3130 shouldFail: true,
3131 expectedError: ":WRONG_SIGNATURE_TYPE:",
3132 })
David Benjamin000800a2014-11-14 01:43:59 -05003133}
3134
David Benjamin83f90402015-01-27 01:09:43 -05003135// timeouts is the retransmit schedule for BoringSSL. It doubles and
3136// caps at 60 seconds. On the 13th timeout, it gives up.
3137var timeouts = []time.Duration{
3138 1 * time.Second,
3139 2 * time.Second,
3140 4 * time.Second,
3141 8 * time.Second,
3142 16 * time.Second,
3143 32 * time.Second,
3144 60 * time.Second,
3145 60 * time.Second,
3146 60 * time.Second,
3147 60 * time.Second,
3148 60 * time.Second,
3149 60 * time.Second,
3150 60 * time.Second,
3151}
3152
3153func addDTLSRetransmitTests() {
3154 // Test that this is indeed the timeout schedule. Stress all
3155 // four patterns of handshake.
3156 for i := 1; i < len(timeouts); i++ {
3157 number := strconv.Itoa(i)
3158 testCases = append(testCases, testCase{
3159 protocol: dtls,
3160 name: "DTLS-Retransmit-Client-" + number,
3161 config: Config{
3162 Bugs: ProtocolBugs{
3163 TimeoutSchedule: timeouts[:i],
3164 },
3165 },
3166 resumeSession: true,
3167 flags: []string{"-async"},
3168 })
3169 testCases = append(testCases, testCase{
3170 protocol: dtls,
3171 testType: serverTest,
3172 name: "DTLS-Retransmit-Server-" + number,
3173 config: Config{
3174 Bugs: ProtocolBugs{
3175 TimeoutSchedule: timeouts[:i],
3176 },
3177 },
3178 resumeSession: true,
3179 flags: []string{"-async"},
3180 })
3181 }
3182
3183 // Test that exceeding the timeout schedule hits a read
3184 // timeout.
3185 testCases = append(testCases, testCase{
3186 protocol: dtls,
3187 name: "DTLS-Retransmit-Timeout",
3188 config: Config{
3189 Bugs: ProtocolBugs{
3190 TimeoutSchedule: timeouts,
3191 },
3192 },
3193 resumeSession: true,
3194 flags: []string{"-async"},
3195 shouldFail: true,
3196 expectedError: ":READ_TIMEOUT_EXPIRED:",
3197 })
3198
3199 // Test that timeout handling has a fudge factor, due to API
3200 // problems.
3201 testCases = append(testCases, testCase{
3202 protocol: dtls,
3203 name: "DTLS-Retransmit-Fudge",
3204 config: Config{
3205 Bugs: ProtocolBugs{
3206 TimeoutSchedule: []time.Duration{
3207 timeouts[0] - 10*time.Millisecond,
3208 },
3209 },
3210 },
3211 resumeSession: true,
3212 flags: []string{"-async"},
3213 })
David Benjamin7eaab4c2015-03-02 19:01:16 -05003214
3215 // Test that the final Finished retransmitting isn't
3216 // duplicated if the peer badly fragments everything.
3217 testCases = append(testCases, testCase{
3218 testType: serverTest,
3219 protocol: dtls,
3220 name: "DTLS-Retransmit-Fragmented",
3221 config: Config{
3222 Bugs: ProtocolBugs{
3223 TimeoutSchedule: []time.Duration{timeouts[0]},
3224 MaxHandshakeRecordLength: 2,
3225 },
3226 },
3227 flags: []string{"-async"},
3228 })
David Benjamin83f90402015-01-27 01:09:43 -05003229}
3230
David Benjaminc565ebb2015-04-03 04:06:36 -04003231func addExportKeyingMaterialTests() {
3232 for _, vers := range tlsVersions {
3233 if vers.version == VersionSSL30 {
3234 continue
3235 }
3236 testCases = append(testCases, testCase{
3237 name: "ExportKeyingMaterial-" + vers.name,
3238 config: Config{
3239 MaxVersion: vers.version,
3240 },
3241 exportKeyingMaterial: 1024,
3242 exportLabel: "label",
3243 exportContext: "context",
3244 useExportContext: true,
3245 })
3246 testCases = append(testCases, testCase{
3247 name: "ExportKeyingMaterial-NoContext-" + vers.name,
3248 config: Config{
3249 MaxVersion: vers.version,
3250 },
3251 exportKeyingMaterial: 1024,
3252 })
3253 testCases = append(testCases, testCase{
3254 name: "ExportKeyingMaterial-EmptyContext-" + vers.name,
3255 config: Config{
3256 MaxVersion: vers.version,
3257 },
3258 exportKeyingMaterial: 1024,
3259 useExportContext: true,
3260 })
3261 testCases = append(testCases, testCase{
3262 name: "ExportKeyingMaterial-Small-" + vers.name,
3263 config: Config{
3264 MaxVersion: vers.version,
3265 },
3266 exportKeyingMaterial: 1,
3267 exportLabel: "label",
3268 exportContext: "context",
3269 useExportContext: true,
3270 })
3271 }
3272 testCases = append(testCases, testCase{
3273 name: "ExportKeyingMaterial-SSL3",
3274 config: Config{
3275 MaxVersion: VersionSSL30,
3276 },
3277 exportKeyingMaterial: 1024,
3278 exportLabel: "label",
3279 exportContext: "context",
3280 useExportContext: true,
3281 shouldFail: true,
3282 expectedError: "failed to export keying material",
3283 })
3284}
3285
David Benjamin884fdf12014-08-02 15:28:23 -04003286func worker(statusChan chan statusMsg, c chan *testCase, buildDir string, wg *sync.WaitGroup) {
Adam Langley95c29f32014-06-20 12:00:00 -07003287 defer wg.Done()
3288
3289 for test := range c {
Adam Langley69a01602014-11-17 17:26:55 -08003290 var err error
3291
3292 if *mallocTest < 0 {
3293 statusChan <- statusMsg{test: test, started: true}
3294 err = runTest(test, buildDir, -1)
3295 } else {
3296 for mallocNumToFail := int64(*mallocTest); ; mallocNumToFail++ {
3297 statusChan <- statusMsg{test: test, started: true}
3298 if err = runTest(test, buildDir, mallocNumToFail); err != errMoreMallocs {
3299 if err != nil {
3300 fmt.Printf("\n\nmalloc test failed at %d: %s\n", mallocNumToFail, err)
3301 }
3302 break
3303 }
3304 }
3305 }
Adam Langley95c29f32014-06-20 12:00:00 -07003306 statusChan <- statusMsg{test: test, err: err}
3307 }
3308}
3309
3310type statusMsg struct {
3311 test *testCase
3312 started bool
3313 err error
3314}
3315
David Benjamin5f237bc2015-02-11 17:14:15 -05003316func statusPrinter(doneChan chan *testOutput, statusChan chan statusMsg, total int) {
Adam Langley95c29f32014-06-20 12:00:00 -07003317 var started, done, failed, lineLen int
Adam Langley95c29f32014-06-20 12:00:00 -07003318
David Benjamin5f237bc2015-02-11 17:14:15 -05003319 testOutput := newTestOutput()
Adam Langley95c29f32014-06-20 12:00:00 -07003320 for msg := range statusChan {
David Benjamin5f237bc2015-02-11 17:14:15 -05003321 if !*pipe {
3322 // Erase the previous status line.
David Benjamin87c8a642015-02-21 01:54:29 -05003323 var erase string
3324 for i := 0; i < lineLen; i++ {
3325 erase += "\b \b"
3326 }
3327 fmt.Print(erase)
David Benjamin5f237bc2015-02-11 17:14:15 -05003328 }
3329
Adam Langley95c29f32014-06-20 12:00:00 -07003330 if msg.started {
3331 started++
3332 } else {
3333 done++
David Benjamin5f237bc2015-02-11 17:14:15 -05003334
3335 if msg.err != nil {
3336 fmt.Printf("FAILED (%s)\n%s\n", msg.test.name, msg.err)
3337 failed++
3338 testOutput.addResult(msg.test.name, "FAIL")
3339 } else {
3340 if *pipe {
3341 // Print each test instead of a status line.
3342 fmt.Printf("PASSED (%s)\n", msg.test.name)
3343 }
3344 testOutput.addResult(msg.test.name, "PASS")
3345 }
Adam Langley95c29f32014-06-20 12:00:00 -07003346 }
3347
David Benjamin5f237bc2015-02-11 17:14:15 -05003348 if !*pipe {
3349 // Print a new status line.
3350 line := fmt.Sprintf("%d/%d/%d/%d", failed, done, started, total)
3351 lineLen = len(line)
3352 os.Stdout.WriteString(line)
Adam Langley95c29f32014-06-20 12:00:00 -07003353 }
Adam Langley95c29f32014-06-20 12:00:00 -07003354 }
David Benjamin5f237bc2015-02-11 17:14:15 -05003355
3356 doneChan <- testOutput
Adam Langley95c29f32014-06-20 12:00:00 -07003357}
3358
3359func main() {
3360 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 -04003361 var flagNumWorkers *int = flag.Int("num-workers", runtime.NumCPU(), "The number of workers to run in parallel.")
David Benjamin884fdf12014-08-02 15:28:23 -04003362 var flagBuildDir *string = flag.String("build-dir", "../../../build", "The build directory to run the shim from.")
Adam Langley95c29f32014-06-20 12:00:00 -07003363
3364 flag.Parse()
3365
3366 addCipherSuiteTests()
3367 addBadECDSASignatureTests()
Adam Langley80842bd2014-06-20 12:00:00 -07003368 addCBCPaddingTests()
Kenny Root7fdeaf12014-08-05 15:23:37 -07003369 addCBCSplittingTests()
David Benjamin636293b2014-07-08 17:59:18 -04003370 addClientAuthTests()
Adam Langley524e7172015-02-20 16:04:00 -08003371 addDDoSCallbackTests()
David Benjamin7e2e6cf2014-08-07 17:44:24 -04003372 addVersionNegotiationTests()
David Benjaminaccb4542014-12-12 23:44:33 -05003373 addMinimumVersionTests()
David Benjamin5c24a1d2014-08-31 00:59:27 -04003374 addD5BugTests()
David Benjamine78bfde2014-09-06 12:45:15 -04003375 addExtensionTests()
David Benjamin01fe8202014-09-24 15:21:44 -04003376 addResumptionVersionTests()
Adam Langley75712922014-10-10 16:23:43 -07003377 addExtendedMasterSecretTests()
Adam Langley2ae77d22014-10-28 17:29:33 -07003378 addRenegotiationTests()
David Benjamin5e961c12014-11-07 01:48:35 -05003379 addDTLSReplayTests()
David Benjamin000800a2014-11-14 01:43:59 -05003380 addSigningHashTests()
Feng Lu41aa3252014-11-21 22:47:56 -08003381 addFastRadioPaddingTests()
David Benjamin83f90402015-01-27 01:09:43 -05003382 addDTLSRetransmitTests()
David Benjaminc565ebb2015-04-03 04:06:36 -04003383 addExportKeyingMaterialTests()
David Benjamin43ec06f2014-08-05 02:28:57 -04003384 for _, async := range []bool{false, true} {
3385 for _, splitHandshake := range []bool{false, true} {
David Benjamin6fd297b2014-08-11 18:43:38 -04003386 for _, protocol := range []protocol{tls, dtls} {
3387 addStateMachineCoverageTests(async, splitHandshake, protocol)
3388 }
David Benjamin43ec06f2014-08-05 02:28:57 -04003389 }
3390 }
Adam Langley95c29f32014-06-20 12:00:00 -07003391
3392 var wg sync.WaitGroup
3393
David Benjamin2bc8e6f2014-08-02 15:22:37 -04003394 numWorkers := *flagNumWorkers
Adam Langley95c29f32014-06-20 12:00:00 -07003395
3396 statusChan := make(chan statusMsg, numWorkers)
3397 testChan := make(chan *testCase, numWorkers)
David Benjamin5f237bc2015-02-11 17:14:15 -05003398 doneChan := make(chan *testOutput)
Adam Langley95c29f32014-06-20 12:00:00 -07003399
David Benjamin025b3d32014-07-01 19:53:04 -04003400 go statusPrinter(doneChan, statusChan, len(testCases))
Adam Langley95c29f32014-06-20 12:00:00 -07003401
3402 for i := 0; i < numWorkers; i++ {
3403 wg.Add(1)
David Benjamin884fdf12014-08-02 15:28:23 -04003404 go worker(statusChan, testChan, *flagBuildDir, &wg)
Adam Langley95c29f32014-06-20 12:00:00 -07003405 }
3406
David Benjamin025b3d32014-07-01 19:53:04 -04003407 for i := range testCases {
3408 if len(*flagTest) == 0 || *flagTest == testCases[i].name {
3409 testChan <- &testCases[i]
Adam Langley95c29f32014-06-20 12:00:00 -07003410 }
3411 }
3412
3413 close(testChan)
3414 wg.Wait()
3415 close(statusChan)
David Benjamin5f237bc2015-02-11 17:14:15 -05003416 testOutput := <-doneChan
Adam Langley95c29f32014-06-20 12:00:00 -07003417
3418 fmt.Printf("\n")
David Benjamin5f237bc2015-02-11 17:14:15 -05003419
3420 if *jsonOutput != "" {
3421 if err := testOutput.writeTo(*jsonOutput); err != nil {
3422 fmt.Fprintf(os.Stderr, "Error: %s\n", err)
3423 }
3424 }
David Benjamin2ab7a862015-04-04 17:02:18 -04003425
3426 if !testOutput.allPassed {
3427 os.Exit(1)
3428 }
Adam Langley95c29f32014-06-20 12:00:00 -07003429}