blob: 6c0d294371ee2bd4077738171ad5f5482901fded [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 },
Adam Langley95c29f32014-06-20 12:00:00 -07001119}
1120
David Benjamin01fe8202014-09-24 15:21:44 -04001121func doExchange(test *testCase, config *Config, conn net.Conn, messageLen int, isResume bool) error {
David Benjamin65ea8ff2014-11-23 03:01:00 -05001122 var connDebug *recordingConn
David Benjamin5fa3eba2015-01-22 16:35:40 -05001123 var connDamage *damageAdaptor
David Benjamin65ea8ff2014-11-23 03:01:00 -05001124 if *flagDebug {
1125 connDebug = &recordingConn{Conn: conn}
1126 conn = connDebug
1127 defer func() {
1128 connDebug.WriteTo(os.Stdout)
1129 }()
1130 }
1131
David Benjamin6fd297b2014-08-11 18:43:38 -04001132 if test.protocol == dtls {
David Benjamin83f90402015-01-27 01:09:43 -05001133 config.Bugs.PacketAdaptor = newPacketAdaptor(conn)
1134 conn = config.Bugs.PacketAdaptor
David Benjamin5e961c12014-11-07 01:48:35 -05001135 if test.replayWrites {
1136 conn = newReplayAdaptor(conn)
1137 }
David Benjamin6fd297b2014-08-11 18:43:38 -04001138 }
1139
David Benjamin5fa3eba2015-01-22 16:35:40 -05001140 if test.damageFirstWrite {
1141 connDamage = newDamageAdaptor(conn)
1142 conn = connDamage
1143 }
1144
David Benjamin6fd297b2014-08-11 18:43:38 -04001145 if test.sendPrefix != "" {
1146 if _, err := conn.Write([]byte(test.sendPrefix)); err != nil {
1147 return err
1148 }
David Benjamin98e882e2014-08-08 13:24:34 -04001149 }
1150
David Benjamin1d5c83e2014-07-22 19:20:02 -04001151 var tlsConn *Conn
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001152 if test.testType == clientTest {
David Benjamin6fd297b2014-08-11 18:43:38 -04001153 if test.protocol == dtls {
1154 tlsConn = DTLSServer(conn, config)
1155 } else {
1156 tlsConn = Server(conn, config)
1157 }
David Benjamin1d5c83e2014-07-22 19:20:02 -04001158 } else {
1159 config.InsecureSkipVerify = true
David Benjamin6fd297b2014-08-11 18:43:38 -04001160 if test.protocol == dtls {
1161 tlsConn = DTLSClient(conn, config)
1162 } else {
1163 tlsConn = Client(conn, config)
1164 }
David Benjamin1d5c83e2014-07-22 19:20:02 -04001165 }
1166
Adam Langley95c29f32014-06-20 12:00:00 -07001167 if err := tlsConn.Handshake(); err != nil {
1168 return err
1169 }
Kenny Root7fdeaf12014-08-05 15:23:37 -07001170
David Benjamin01fe8202014-09-24 15:21:44 -04001171 // TODO(davidben): move all per-connection expectations into a dedicated
1172 // expectations struct that can be specified separately for the two
1173 // legs.
1174 expectedVersion := test.expectedVersion
1175 if isResume && test.expectedResumeVersion != 0 {
1176 expectedVersion = test.expectedResumeVersion
1177 }
1178 if vers := tlsConn.ConnectionState().Version; expectedVersion != 0 && vers != expectedVersion {
1179 return fmt.Errorf("got version %x, expected %x", vers, expectedVersion)
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001180 }
1181
David Benjamin90da8c82015-04-20 14:57:57 -04001182 if cipher := tlsConn.ConnectionState().CipherSuite; test.expectedCipher != 0 && cipher != test.expectedCipher {
1183 return fmt.Errorf("got cipher %x, expected %x", cipher, test.expectedCipher)
1184 }
1185
David Benjamina08e49d2014-08-24 01:46:07 -04001186 if test.expectChannelID {
1187 channelID := tlsConn.ConnectionState().ChannelID
1188 if channelID == nil {
1189 return fmt.Errorf("no channel ID negotiated")
1190 }
1191 if channelID.Curve != channelIDKey.Curve ||
1192 channelIDKey.X.Cmp(channelIDKey.X) != 0 ||
1193 channelIDKey.Y.Cmp(channelIDKey.Y) != 0 {
1194 return fmt.Errorf("incorrect channel ID")
1195 }
1196 }
1197
David Benjaminae2888f2014-09-06 12:58:58 -04001198 if expected := test.expectedNextProto; expected != "" {
1199 if actual := tlsConn.ConnectionState().NegotiatedProtocol; actual != expected {
1200 return fmt.Errorf("next proto mismatch: got %s, wanted %s", actual, expected)
1201 }
1202 }
1203
David Benjaminfc7b0862014-09-06 13:21:53 -04001204 if test.expectedNextProtoType != 0 {
1205 if (test.expectedNextProtoType == alpn) != tlsConn.ConnectionState().NegotiatedProtocolFromALPN {
1206 return fmt.Errorf("next proto type mismatch")
1207 }
1208 }
1209
David Benjaminca6c8262014-11-15 19:06:08 -05001210 if p := tlsConn.ConnectionState().SRTPProtectionProfile; p != test.expectedSRTPProtectionProfile {
1211 return fmt.Errorf("SRTP profile mismatch: got %d, wanted %d", p, test.expectedSRTPProtectionProfile)
1212 }
1213
David Benjaminc565ebb2015-04-03 04:06:36 -04001214 if test.exportKeyingMaterial > 0 {
1215 actual := make([]byte, test.exportKeyingMaterial)
1216 if _, err := io.ReadFull(tlsConn, actual); err != nil {
1217 return err
1218 }
1219 expected, err := tlsConn.ExportKeyingMaterial(test.exportKeyingMaterial, []byte(test.exportLabel), []byte(test.exportContext), test.useExportContext)
1220 if err != nil {
1221 return err
1222 }
1223 if !bytes.Equal(actual, expected) {
1224 return fmt.Errorf("keying material mismatch")
1225 }
1226 }
1227
David Benjamine58c4f52014-08-24 03:47:07 -04001228 if test.shimWritesFirst {
1229 var buf [5]byte
1230 _, err := io.ReadFull(tlsConn, buf[:])
1231 if err != nil {
1232 return err
1233 }
1234 if string(buf[:]) != "hello" {
1235 return fmt.Errorf("bad initial message")
1236 }
1237 }
1238
Adam Langleycf2d4f42014-10-28 19:06:14 -07001239 if test.renegotiate {
1240 if test.renegotiateCiphers != nil {
1241 config.CipherSuites = test.renegotiateCiphers
1242 }
1243 if err := tlsConn.Renegotiate(); err != nil {
1244 return err
1245 }
1246 } else if test.renegotiateCiphers != nil {
1247 panic("renegotiateCiphers without renegotiate")
1248 }
1249
David Benjamin5fa3eba2015-01-22 16:35:40 -05001250 if test.damageFirstWrite {
1251 connDamage.setDamage(true)
1252 tlsConn.Write([]byte("DAMAGED WRITE"))
1253 connDamage.setDamage(false)
1254 }
1255
Kenny Root7fdeaf12014-08-05 15:23:37 -07001256 if messageLen < 0 {
David Benjamin6fd297b2014-08-11 18:43:38 -04001257 if test.protocol == dtls {
1258 return fmt.Errorf("messageLen < 0 not supported for DTLS tests")
1259 }
Kenny Root7fdeaf12014-08-05 15:23:37 -07001260 // Read until EOF.
1261 _, err := io.Copy(ioutil.Discard, tlsConn)
1262 return err
1263 }
1264
David Benjamin4417d052015-04-05 04:17:25 -04001265 if messageLen == 0 {
1266 messageLen = 32
Adam Langley80842bd2014-06-20 12:00:00 -07001267 }
David Benjamin4417d052015-04-05 04:17:25 -04001268 testMessage := make([]byte, messageLen)
1269 for i := range testMessage {
1270 testMessage[i] = 0x42
1271 }
1272 tlsConn.Write(testMessage)
Adam Langley95c29f32014-06-20 12:00:00 -07001273
1274 buf := make([]byte, len(testMessage))
David Benjamin6fd297b2014-08-11 18:43:38 -04001275 if test.protocol == dtls {
1276 bufTmp := make([]byte, len(buf)+1)
1277 n, err := tlsConn.Read(bufTmp)
1278 if err != nil {
1279 return err
1280 }
1281 if n != len(buf) {
1282 return fmt.Errorf("bad reply; length mismatch (%d vs %d)", n, len(buf))
1283 }
1284 copy(buf, bufTmp)
1285 } else {
1286 _, err := io.ReadFull(tlsConn, buf)
1287 if err != nil {
1288 return err
1289 }
Adam Langley95c29f32014-06-20 12:00:00 -07001290 }
1291
1292 for i, v := range buf {
1293 if v != testMessage[i]^0xff {
1294 return fmt.Errorf("bad reply contents at byte %d", i)
1295 }
1296 }
1297
1298 return nil
1299}
1300
David Benjamin325b5c32014-07-01 19:40:31 -04001301func valgrindOf(dbAttach bool, path string, args ...string) *exec.Cmd {
1302 valgrindArgs := []string{"--error-exitcode=99", "--track-origins=yes", "--leak-check=full"}
Adam Langley95c29f32014-06-20 12:00:00 -07001303 if dbAttach {
David Benjamin325b5c32014-07-01 19:40:31 -04001304 valgrindArgs = append(valgrindArgs, "--db-attach=yes", "--db-command=xterm -e gdb -nw %f %p")
Adam Langley95c29f32014-06-20 12:00:00 -07001305 }
David Benjamin325b5c32014-07-01 19:40:31 -04001306 valgrindArgs = append(valgrindArgs, path)
1307 valgrindArgs = append(valgrindArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -07001308
David Benjamin325b5c32014-07-01 19:40:31 -04001309 return exec.Command("valgrind", valgrindArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -07001310}
1311
David Benjamin325b5c32014-07-01 19:40:31 -04001312func gdbOf(path string, args ...string) *exec.Cmd {
1313 xtermArgs := []string{"-e", "gdb", "--args"}
1314 xtermArgs = append(xtermArgs, path)
1315 xtermArgs = append(xtermArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -07001316
David Benjamin325b5c32014-07-01 19:40:31 -04001317 return exec.Command("xterm", xtermArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -07001318}
1319
Adam Langley69a01602014-11-17 17:26:55 -08001320type moreMallocsError struct{}
1321
1322func (moreMallocsError) Error() string {
1323 return "child process did not exhaust all allocation calls"
1324}
1325
1326var errMoreMallocs = moreMallocsError{}
1327
David Benjamin87c8a642015-02-21 01:54:29 -05001328// accept accepts a connection from listener, unless waitChan signals a process
1329// exit first.
1330func acceptOrWait(listener net.Listener, waitChan chan error) (net.Conn, error) {
1331 type connOrError struct {
1332 conn net.Conn
1333 err error
1334 }
1335 connChan := make(chan connOrError, 1)
1336 go func() {
1337 conn, err := listener.Accept()
1338 connChan <- connOrError{conn, err}
1339 close(connChan)
1340 }()
1341 select {
1342 case result := <-connChan:
1343 return result.conn, result.err
1344 case childErr := <-waitChan:
1345 waitChan <- childErr
1346 return nil, fmt.Errorf("child exited early: %s", childErr)
1347 }
1348}
1349
Adam Langley69a01602014-11-17 17:26:55 -08001350func runTest(test *testCase, buildDir string, mallocNumToFail int64) error {
Adam Langley38311732014-10-16 19:04:35 -07001351 if !test.shouldFail && (len(test.expectedError) > 0 || len(test.expectedLocalError) > 0) {
1352 panic("Error expected without shouldFail in " + test.name)
1353 }
1354
David Benjamin87c8a642015-02-21 01:54:29 -05001355 listener, err := net.ListenTCP("tcp4", &net.TCPAddr{IP: net.IP{127, 0, 0, 1}})
1356 if err != nil {
1357 panic(err)
1358 }
1359 defer func() {
1360 if listener != nil {
1361 listener.Close()
1362 }
1363 }()
Adam Langley95c29f32014-06-20 12:00:00 -07001364
David Benjamin884fdf12014-08-02 15:28:23 -04001365 shim_path := path.Join(buildDir, "ssl/test/bssl_shim")
David Benjamin87c8a642015-02-21 01:54:29 -05001366 flags := []string{"-port", strconv.Itoa(listener.Addr().(*net.TCPAddr).Port)}
David Benjamin1d5c83e2014-07-22 19:20:02 -04001367 if test.testType == serverTest {
David Benjamin5a593af2014-08-11 19:51:50 -04001368 flags = append(flags, "-server")
1369
David Benjamin025b3d32014-07-01 19:53:04 -04001370 flags = append(flags, "-key-file")
1371 if test.keyFile == "" {
1372 flags = append(flags, rsaKeyFile)
1373 } else {
1374 flags = append(flags, test.keyFile)
1375 }
1376
1377 flags = append(flags, "-cert-file")
1378 if test.certFile == "" {
1379 flags = append(flags, rsaCertificateFile)
1380 } else {
1381 flags = append(flags, test.certFile)
1382 }
1383 }
David Benjamin5a593af2014-08-11 19:51:50 -04001384
David Benjamin6fd297b2014-08-11 18:43:38 -04001385 if test.protocol == dtls {
1386 flags = append(flags, "-dtls")
1387 }
1388
David Benjamin5a593af2014-08-11 19:51:50 -04001389 if test.resumeSession {
1390 flags = append(flags, "-resume")
1391 }
1392
David Benjamine58c4f52014-08-24 03:47:07 -04001393 if test.shimWritesFirst {
1394 flags = append(flags, "-shim-writes-first")
1395 }
1396
David Benjaminc565ebb2015-04-03 04:06:36 -04001397 if test.exportKeyingMaterial > 0 {
1398 flags = append(flags, "-export-keying-material", strconv.Itoa(test.exportKeyingMaterial))
1399 flags = append(flags, "-export-label", test.exportLabel)
1400 flags = append(flags, "-export-context", test.exportContext)
1401 if test.useExportContext {
1402 flags = append(flags, "-use-export-context")
1403 }
1404 }
1405
David Benjamin025b3d32014-07-01 19:53:04 -04001406 flags = append(flags, test.flags...)
1407
1408 var shim *exec.Cmd
1409 if *useValgrind {
1410 shim = valgrindOf(false, shim_path, flags...)
Adam Langley75712922014-10-10 16:23:43 -07001411 } else if *useGDB {
1412 shim = gdbOf(shim_path, flags...)
David Benjamin025b3d32014-07-01 19:53:04 -04001413 } else {
1414 shim = exec.Command(shim_path, flags...)
1415 }
David Benjamin025b3d32014-07-01 19:53:04 -04001416 shim.Stdin = os.Stdin
1417 var stdoutBuf, stderrBuf bytes.Buffer
1418 shim.Stdout = &stdoutBuf
1419 shim.Stderr = &stderrBuf
Adam Langley69a01602014-11-17 17:26:55 -08001420 if mallocNumToFail >= 0 {
David Benjamin9e128b02015-02-09 13:13:09 -05001421 shim.Env = os.Environ()
1422 shim.Env = append(shim.Env, "MALLOC_NUMBER_TO_FAIL="+strconv.FormatInt(mallocNumToFail, 10))
Adam Langley69a01602014-11-17 17:26:55 -08001423 if *mallocTestDebug {
1424 shim.Env = append(shim.Env, "MALLOC_ABORT_ON_FAIL=1")
1425 }
1426 shim.Env = append(shim.Env, "_MALLOC_CHECK=1")
1427 }
David Benjamin025b3d32014-07-01 19:53:04 -04001428
1429 if err := shim.Start(); err != nil {
Adam Langley95c29f32014-06-20 12:00:00 -07001430 panic(err)
1431 }
David Benjamin87c8a642015-02-21 01:54:29 -05001432 waitChan := make(chan error, 1)
1433 go func() { waitChan <- shim.Wait() }()
Adam Langley95c29f32014-06-20 12:00:00 -07001434
1435 config := test.config
David Benjamin1d5c83e2014-07-22 19:20:02 -04001436 config.ClientSessionCache = NewLRUClientSessionCache(1)
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001437 config.ServerSessionCache = NewLRUServerSessionCache(1)
David Benjamin025b3d32014-07-01 19:53:04 -04001438 if test.testType == clientTest {
1439 if len(config.Certificates) == 0 {
1440 config.Certificates = []Certificate{getRSACertificate()}
1441 }
David Benjamin87c8a642015-02-21 01:54:29 -05001442 } else {
1443 // Supply a ServerName to ensure a constant session cache key,
1444 // rather than falling back to net.Conn.RemoteAddr.
1445 if len(config.ServerName) == 0 {
1446 config.ServerName = "test"
1447 }
David Benjamin025b3d32014-07-01 19:53:04 -04001448 }
Adam Langley95c29f32014-06-20 12:00:00 -07001449
David Benjamin87c8a642015-02-21 01:54:29 -05001450 conn, err := acceptOrWait(listener, waitChan)
1451 if err == nil {
1452 err = doExchange(test, &config, conn, test.messageLen, false /* not a resumption */)
1453 conn.Close()
1454 }
David Benjamin65ea8ff2014-11-23 03:01:00 -05001455
David Benjamin1d5c83e2014-07-22 19:20:02 -04001456 if err == nil && test.resumeSession {
David Benjamin01fe8202014-09-24 15:21:44 -04001457 var resumeConfig Config
1458 if test.resumeConfig != nil {
1459 resumeConfig = *test.resumeConfig
David Benjamin87c8a642015-02-21 01:54:29 -05001460 if len(resumeConfig.ServerName) == 0 {
1461 resumeConfig.ServerName = config.ServerName
1462 }
David Benjamin01fe8202014-09-24 15:21:44 -04001463 if len(resumeConfig.Certificates) == 0 {
1464 resumeConfig.Certificates = []Certificate{getRSACertificate()}
1465 }
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001466 if !test.newSessionsOnResume {
1467 resumeConfig.SessionTicketKey = config.SessionTicketKey
1468 resumeConfig.ClientSessionCache = config.ClientSessionCache
1469 resumeConfig.ServerSessionCache = config.ServerSessionCache
1470 }
David Benjamin01fe8202014-09-24 15:21:44 -04001471 } else {
1472 resumeConfig = config
1473 }
David Benjamin87c8a642015-02-21 01:54:29 -05001474 var connResume net.Conn
1475 connResume, err = acceptOrWait(listener, waitChan)
1476 if err == nil {
1477 err = doExchange(test, &resumeConfig, connResume, test.messageLen, true /* resumption */)
1478 connResume.Close()
1479 }
David Benjamin1d5c83e2014-07-22 19:20:02 -04001480 }
1481
David Benjamin87c8a642015-02-21 01:54:29 -05001482 // Close the listener now. This is to avoid hangs should the shim try to
1483 // open more connections than expected.
1484 listener.Close()
1485 listener = nil
1486
1487 childErr := <-waitChan
Adam Langley69a01602014-11-17 17:26:55 -08001488 if exitError, ok := childErr.(*exec.ExitError); ok {
1489 if exitError.Sys().(syscall.WaitStatus).ExitStatus() == 88 {
1490 return errMoreMallocs
1491 }
1492 }
Adam Langley95c29f32014-06-20 12:00:00 -07001493
1494 stdout := string(stdoutBuf.Bytes())
1495 stderr := string(stderrBuf.Bytes())
1496 failed := err != nil || childErr != nil
David Benjaminc565ebb2015-04-03 04:06:36 -04001497 correctFailure := len(test.expectedError) == 0 || strings.Contains(stderr, test.expectedError)
Adam Langleyac61fa32014-06-23 12:03:11 -07001498 localError := "none"
1499 if err != nil {
1500 localError = err.Error()
1501 }
1502 if len(test.expectedLocalError) != 0 {
1503 correctFailure = correctFailure && strings.Contains(localError, test.expectedLocalError)
1504 }
Adam Langley95c29f32014-06-20 12:00:00 -07001505
1506 if failed != test.shouldFail || failed && !correctFailure {
Adam Langley95c29f32014-06-20 12:00:00 -07001507 childError := "none"
Adam Langley95c29f32014-06-20 12:00:00 -07001508 if childErr != nil {
1509 childError = childErr.Error()
1510 }
1511
1512 var msg string
1513 switch {
1514 case failed && !test.shouldFail:
1515 msg = "unexpected failure"
1516 case !failed && test.shouldFail:
1517 msg = "unexpected success"
1518 case failed && !correctFailure:
Adam Langleyac61fa32014-06-23 12:03:11 -07001519 msg = "bad error (wanted '" + test.expectedError + "' / '" + test.expectedLocalError + "')"
Adam Langley95c29f32014-06-20 12:00:00 -07001520 default:
1521 panic("internal error")
1522 }
1523
David Benjaminc565ebb2015-04-03 04:06:36 -04001524 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 -07001525 }
1526
David Benjaminc565ebb2015-04-03 04:06:36 -04001527 if !*useValgrind && !failed && len(stderr) > 0 {
Adam Langley95c29f32014-06-20 12:00:00 -07001528 println(stderr)
1529 }
1530
1531 return nil
1532}
1533
1534var tlsVersions = []struct {
1535 name string
1536 version uint16
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001537 flag string
David Benjamin8b8c0062014-11-23 02:47:52 -05001538 hasDTLS bool
Adam Langley95c29f32014-06-20 12:00:00 -07001539}{
David Benjamin8b8c0062014-11-23 02:47:52 -05001540 {"SSL3", VersionSSL30, "-no-ssl3", false},
1541 {"TLS1", VersionTLS10, "-no-tls1", true},
1542 {"TLS11", VersionTLS11, "-no-tls11", false},
1543 {"TLS12", VersionTLS12, "-no-tls12", true},
Adam Langley95c29f32014-06-20 12:00:00 -07001544}
1545
1546var testCipherSuites = []struct {
1547 name string
1548 id uint16
1549}{
1550 {"3DES-SHA", TLS_RSA_WITH_3DES_EDE_CBC_SHA},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001551 {"AES128-GCM", TLS_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001552 {"AES128-SHA", TLS_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001553 {"AES128-SHA256", TLS_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001554 {"AES256-GCM", TLS_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001555 {"AES256-SHA", TLS_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001556 {"AES256-SHA256", TLS_RSA_WITH_AES_256_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001557 {"DHE-RSA-AES128-GCM", TLS_DHE_RSA_WITH_AES_128_GCM_SHA256},
1558 {"DHE-RSA-AES128-SHA", TLS_DHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001559 {"DHE-RSA-AES128-SHA256", TLS_DHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001560 {"DHE-RSA-AES256-GCM", TLS_DHE_RSA_WITH_AES_256_GCM_SHA384},
1561 {"DHE-RSA-AES256-SHA", TLS_DHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001562 {"DHE-RSA-AES256-SHA256", TLS_DHE_RSA_WITH_AES_256_CBC_SHA256},
David Benjamine9a80ff2015-04-07 00:46:46 -04001563 {"DHE-RSA-CHACHA20-POLY1305", TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001564 {"ECDHE-ECDSA-AES128-GCM", TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
1565 {"ECDHE-ECDSA-AES128-SHA", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001566 {"ECDHE-ECDSA-AES128-SHA256", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256},
1567 {"ECDHE-ECDSA-AES256-GCM", TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001568 {"ECDHE-ECDSA-AES256-SHA", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001569 {"ECDHE-ECDSA-AES256-SHA384", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384},
David Benjamine9a80ff2015-04-07 00:46:46 -04001570 {"ECDHE-ECDSA-CHACHA20-POLY1305", TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001571 {"ECDHE-ECDSA-RC4-SHA", TLS_ECDHE_ECDSA_WITH_RC4_128_SHA},
Adam Langley97e8ba82015-04-29 15:32:10 -07001572 {"ECDHE-PSK-AES128-GCM-SHA256", TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001573 {"ECDHE-RSA-AES128-GCM", TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001574 {"ECDHE-RSA-AES128-SHA", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001575 {"ECDHE-RSA-AES128-SHA256", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001576 {"ECDHE-RSA-AES256-GCM", TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001577 {"ECDHE-RSA-AES256-SHA", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001578 {"ECDHE-RSA-AES256-SHA384", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384},
David Benjamine9a80ff2015-04-07 00:46:46 -04001579 {"ECDHE-RSA-CHACHA20-POLY1305", TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001580 {"ECDHE-RSA-RC4-SHA", TLS_ECDHE_RSA_WITH_RC4_128_SHA},
David Benjamin48cae082014-10-27 01:06:24 -04001581 {"PSK-AES128-CBC-SHA", TLS_PSK_WITH_AES_128_CBC_SHA},
1582 {"PSK-AES256-CBC-SHA", TLS_PSK_WITH_AES_256_CBC_SHA},
1583 {"PSK-RC4-SHA", TLS_PSK_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -07001584 {"RC4-MD5", TLS_RSA_WITH_RC4_128_MD5},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001585 {"RC4-SHA", TLS_RSA_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -07001586}
1587
David Benjamin8b8c0062014-11-23 02:47:52 -05001588func hasComponent(suiteName, component string) bool {
1589 return strings.Contains("-"+suiteName+"-", "-"+component+"-")
1590}
1591
David Benjaminf7768e42014-08-31 02:06:47 -04001592func isTLS12Only(suiteName string) bool {
David Benjamin8b8c0062014-11-23 02:47:52 -05001593 return hasComponent(suiteName, "GCM") ||
1594 hasComponent(suiteName, "SHA256") ||
David Benjamine9a80ff2015-04-07 00:46:46 -04001595 hasComponent(suiteName, "SHA384") ||
1596 hasComponent(suiteName, "POLY1305")
David Benjamin8b8c0062014-11-23 02:47:52 -05001597}
1598
1599func isDTLSCipher(suiteName string) bool {
David Benjamine95d20d2014-12-23 11:16:01 -05001600 return !hasComponent(suiteName, "RC4")
David Benjaminf7768e42014-08-31 02:06:47 -04001601}
1602
Adam Langleya7997f12015-05-14 17:38:50 -07001603func bigFromHex(hex string) *big.Int {
1604 ret, ok := new(big.Int).SetString(hex, 16)
1605 if !ok {
1606 panic("failed to parse hex number 0x" + hex)
1607 }
1608 return ret
1609}
1610
Adam Langley95c29f32014-06-20 12:00:00 -07001611func addCipherSuiteTests() {
1612 for _, suite := range testCipherSuites {
David Benjamin48cae082014-10-27 01:06:24 -04001613 const psk = "12345"
1614 const pskIdentity = "luggage combo"
1615
Adam Langley95c29f32014-06-20 12:00:00 -07001616 var cert Certificate
David Benjamin025b3d32014-07-01 19:53:04 -04001617 var certFile string
1618 var keyFile string
David Benjamin8b8c0062014-11-23 02:47:52 -05001619 if hasComponent(suite.name, "ECDSA") {
Adam Langley95c29f32014-06-20 12:00:00 -07001620 cert = getECDSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -04001621 certFile = ecdsaCertificateFile
1622 keyFile = ecdsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -07001623 } else {
1624 cert = getRSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -04001625 certFile = rsaCertificateFile
1626 keyFile = rsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -07001627 }
1628
David Benjamin48cae082014-10-27 01:06:24 -04001629 var flags []string
David Benjamin8b8c0062014-11-23 02:47:52 -05001630 if hasComponent(suite.name, "PSK") {
David Benjamin48cae082014-10-27 01:06:24 -04001631 flags = append(flags,
1632 "-psk", psk,
1633 "-psk-identity", pskIdentity)
1634 }
1635
Adam Langley95c29f32014-06-20 12:00:00 -07001636 for _, ver := range tlsVersions {
David Benjaminf7768e42014-08-31 02:06:47 -04001637 if ver.version < VersionTLS12 && isTLS12Only(suite.name) {
Adam Langley95c29f32014-06-20 12:00:00 -07001638 continue
1639 }
1640
David Benjamin025b3d32014-07-01 19:53:04 -04001641 testCases = append(testCases, testCase{
1642 testType: clientTest,
1643 name: ver.name + "-" + suite.name + "-client",
Adam Langley95c29f32014-06-20 12:00:00 -07001644 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001645 MinVersion: ver.version,
1646 MaxVersion: ver.version,
1647 CipherSuites: []uint16{suite.id},
1648 Certificates: []Certificate{cert},
David Benjamin68793732015-05-04 20:20:48 -04001649 PreSharedKey: []byte(psk),
David Benjamin48cae082014-10-27 01:06:24 -04001650 PreSharedKeyIdentity: pskIdentity,
Adam Langley95c29f32014-06-20 12:00:00 -07001651 },
David Benjamin48cae082014-10-27 01:06:24 -04001652 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001653 resumeSession: true,
Adam Langley95c29f32014-06-20 12:00:00 -07001654 })
David Benjamin025b3d32014-07-01 19:53:04 -04001655
David Benjamin76d8abe2014-08-14 16:25:34 -04001656 testCases = append(testCases, testCase{
1657 testType: serverTest,
1658 name: ver.name + "-" + suite.name + "-server",
1659 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001660 MinVersion: ver.version,
1661 MaxVersion: ver.version,
1662 CipherSuites: []uint16{suite.id},
1663 Certificates: []Certificate{cert},
1664 PreSharedKey: []byte(psk),
1665 PreSharedKeyIdentity: pskIdentity,
David Benjamin76d8abe2014-08-14 16:25:34 -04001666 },
1667 certFile: certFile,
1668 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001669 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001670 resumeSession: true,
David Benjamin76d8abe2014-08-14 16:25:34 -04001671 })
David Benjamin6fd297b2014-08-11 18:43:38 -04001672
David Benjamin8b8c0062014-11-23 02:47:52 -05001673 if ver.hasDTLS && isDTLSCipher(suite.name) {
David Benjamin6fd297b2014-08-11 18:43:38 -04001674 testCases = append(testCases, testCase{
1675 testType: clientTest,
1676 protocol: dtls,
1677 name: "D" + ver.name + "-" + suite.name + "-client",
1678 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001679 MinVersion: ver.version,
1680 MaxVersion: ver.version,
1681 CipherSuites: []uint16{suite.id},
1682 Certificates: []Certificate{cert},
1683 PreSharedKey: []byte(psk),
1684 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001685 },
David Benjamin48cae082014-10-27 01:06:24 -04001686 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001687 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001688 })
1689 testCases = append(testCases, testCase{
1690 testType: serverTest,
1691 protocol: dtls,
1692 name: "D" + ver.name + "-" + suite.name + "-server",
1693 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001694 MinVersion: ver.version,
1695 MaxVersion: ver.version,
1696 CipherSuites: []uint16{suite.id},
1697 Certificates: []Certificate{cert},
1698 PreSharedKey: []byte(psk),
1699 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001700 },
1701 certFile: certFile,
1702 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001703 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001704 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001705 })
1706 }
Adam Langley95c29f32014-06-20 12:00:00 -07001707 }
1708 }
Adam Langleya7997f12015-05-14 17:38:50 -07001709
1710 testCases = append(testCases, testCase{
1711 name: "WeakDH",
1712 config: Config{
1713 CipherSuites: []uint16{TLS_DHE_RSA_WITH_AES_128_GCM_SHA256},
1714 Bugs: ProtocolBugs{
1715 // This is a 1023-bit prime number, generated
1716 // with:
1717 // openssl gendh 1023 | openssl asn1parse -i
1718 DHGroupPrime: bigFromHex("518E9B7930CE61C6E445C8360584E5FC78D9137C0FFDC880B495D5338ADF7689951A6821C17A76B3ACB8E0156AEA607B7EC406EBEDBB84D8376EB8FE8F8BA1433488BEE0C3EDDFD3A32DBB9481980A7AF6C96BFCF490A094CFFB2B8192C1BB5510B77B658436E27C2D4D023FE3718222AB0CA1273995B51F6D625A4944D0DD4B"),
1719 },
1720 },
1721 shouldFail: true,
1722 expectedError: "BAD_DH_P_LENGTH",
1723 })
Adam Langley95c29f32014-06-20 12:00:00 -07001724}
1725
1726func addBadECDSASignatureTests() {
1727 for badR := BadValue(1); badR < NumBadValues; badR++ {
1728 for badS := BadValue(1); badS < NumBadValues; badS++ {
David Benjamin025b3d32014-07-01 19:53:04 -04001729 testCases = append(testCases, testCase{
Adam Langley95c29f32014-06-20 12:00:00 -07001730 name: fmt.Sprintf("BadECDSA-%d-%d", badR, badS),
1731 config: Config{
1732 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
1733 Certificates: []Certificate{getECDSACertificate()},
1734 Bugs: ProtocolBugs{
1735 BadECDSAR: badR,
1736 BadECDSAS: badS,
1737 },
1738 },
1739 shouldFail: true,
1740 expectedError: "SIGNATURE",
1741 })
1742 }
1743 }
1744}
1745
Adam Langley80842bd2014-06-20 12:00:00 -07001746func addCBCPaddingTests() {
David Benjamin025b3d32014-07-01 19:53:04 -04001747 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001748 name: "MaxCBCPadding",
1749 config: Config{
1750 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1751 Bugs: ProtocolBugs{
1752 MaxPadding: true,
1753 },
1754 },
1755 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1756 })
David Benjamin025b3d32014-07-01 19:53:04 -04001757 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001758 name: "BadCBCPadding",
1759 config: Config{
1760 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1761 Bugs: ProtocolBugs{
1762 PaddingFirstByteBad: true,
1763 },
1764 },
1765 shouldFail: true,
1766 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1767 })
1768 // OpenSSL previously had an issue where the first byte of padding in
1769 // 255 bytes of padding wasn't checked.
David Benjamin025b3d32014-07-01 19:53:04 -04001770 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001771 name: "BadCBCPadding255",
1772 config: Config{
1773 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1774 Bugs: ProtocolBugs{
1775 MaxPadding: true,
1776 PaddingFirstByteBadIf255: true,
1777 },
1778 },
1779 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1780 shouldFail: true,
1781 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1782 })
1783}
1784
Kenny Root7fdeaf12014-08-05 15:23:37 -07001785func addCBCSplittingTests() {
1786 testCases = append(testCases, testCase{
1787 name: "CBCRecordSplitting",
1788 config: Config{
1789 MaxVersion: VersionTLS10,
1790 MinVersion: VersionTLS10,
1791 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1792 },
1793 messageLen: -1, // read until EOF
1794 flags: []string{
1795 "-async",
1796 "-write-different-record-sizes",
1797 "-cbc-record-splitting",
1798 },
David Benjamina8e3e0e2014-08-06 22:11:10 -04001799 })
1800 testCases = append(testCases, testCase{
Kenny Root7fdeaf12014-08-05 15:23:37 -07001801 name: "CBCRecordSplittingPartialWrite",
1802 config: Config{
1803 MaxVersion: VersionTLS10,
1804 MinVersion: VersionTLS10,
1805 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1806 },
1807 messageLen: -1, // read until EOF
1808 flags: []string{
1809 "-async",
1810 "-write-different-record-sizes",
1811 "-cbc-record-splitting",
1812 "-partial-write",
1813 },
1814 })
1815}
1816
David Benjamin636293b2014-07-08 17:59:18 -04001817func addClientAuthTests() {
David Benjamin407a10c2014-07-16 12:58:59 -04001818 // Add a dummy cert pool to stress certificate authority parsing.
1819 // TODO(davidben): Add tests that those values parse out correctly.
1820 certPool := x509.NewCertPool()
1821 cert, err := x509.ParseCertificate(rsaCertificate.Certificate[0])
1822 if err != nil {
1823 panic(err)
1824 }
1825 certPool.AddCert(cert)
1826
David Benjamin636293b2014-07-08 17:59:18 -04001827 for _, ver := range tlsVersions {
David Benjamin636293b2014-07-08 17:59:18 -04001828 testCases = append(testCases, testCase{
1829 testType: clientTest,
David Benjamin67666e72014-07-12 15:47:52 -04001830 name: ver.name + "-Client-ClientAuth-RSA",
David Benjamin636293b2014-07-08 17:59:18 -04001831 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001832 MinVersion: ver.version,
1833 MaxVersion: ver.version,
1834 ClientAuth: RequireAnyClientCert,
1835 ClientCAs: certPool,
David Benjamin636293b2014-07-08 17:59:18 -04001836 },
1837 flags: []string{
1838 "-cert-file", rsaCertificateFile,
1839 "-key-file", rsaKeyFile,
1840 },
1841 })
1842 testCases = append(testCases, testCase{
David Benjamin67666e72014-07-12 15:47:52 -04001843 testType: serverTest,
1844 name: ver.name + "-Server-ClientAuth-RSA",
1845 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001846 MinVersion: ver.version,
1847 MaxVersion: ver.version,
David Benjamin67666e72014-07-12 15:47:52 -04001848 Certificates: []Certificate{rsaCertificate},
1849 },
1850 flags: []string{"-require-any-client-certificate"},
1851 })
David Benjamine098ec22014-08-27 23:13:20 -04001852 if ver.version != VersionSSL30 {
1853 testCases = append(testCases, testCase{
1854 testType: serverTest,
1855 name: ver.name + "-Server-ClientAuth-ECDSA",
1856 config: Config{
1857 MinVersion: ver.version,
1858 MaxVersion: ver.version,
1859 Certificates: []Certificate{ecdsaCertificate},
1860 },
1861 flags: []string{"-require-any-client-certificate"},
1862 })
1863 testCases = append(testCases, testCase{
1864 testType: clientTest,
1865 name: ver.name + "-Client-ClientAuth-ECDSA",
1866 config: Config{
1867 MinVersion: ver.version,
1868 MaxVersion: ver.version,
1869 ClientAuth: RequireAnyClientCert,
1870 ClientCAs: certPool,
1871 },
1872 flags: []string{
1873 "-cert-file", ecdsaCertificateFile,
1874 "-key-file", ecdsaKeyFile,
1875 },
1876 })
1877 }
David Benjamin636293b2014-07-08 17:59:18 -04001878 }
1879}
1880
Adam Langley75712922014-10-10 16:23:43 -07001881func addExtendedMasterSecretTests() {
1882 const expectEMSFlag = "-expect-extended-master-secret"
1883
1884 for _, with := range []bool{false, true} {
1885 prefix := "No"
1886 var flags []string
1887 if with {
1888 prefix = ""
1889 flags = []string{expectEMSFlag}
1890 }
1891
1892 for _, isClient := range []bool{false, true} {
1893 suffix := "-Server"
1894 testType := serverTest
1895 if isClient {
1896 suffix = "-Client"
1897 testType = clientTest
1898 }
1899
1900 for _, ver := range tlsVersions {
1901 test := testCase{
1902 testType: testType,
1903 name: prefix + "ExtendedMasterSecret-" + ver.name + suffix,
1904 config: Config{
1905 MinVersion: ver.version,
1906 MaxVersion: ver.version,
1907 Bugs: ProtocolBugs{
1908 NoExtendedMasterSecret: !with,
1909 RequireExtendedMasterSecret: with,
1910 },
1911 },
David Benjamin48cae082014-10-27 01:06:24 -04001912 flags: flags,
1913 shouldFail: ver.version == VersionSSL30 && with,
Adam Langley75712922014-10-10 16:23:43 -07001914 }
1915 if test.shouldFail {
1916 test.expectedLocalError = "extended master secret required but not supported by peer"
1917 }
1918 testCases = append(testCases, test)
1919 }
1920 }
1921 }
1922
1923 // When a session is resumed, it should still be aware that its master
1924 // secret was generated via EMS and thus it's safe to use tls-unique.
1925 testCases = append(testCases, testCase{
1926 name: "ExtendedMasterSecret-Resume",
1927 config: Config{
1928 Bugs: ProtocolBugs{
1929 RequireExtendedMasterSecret: true,
1930 },
1931 },
1932 flags: []string{expectEMSFlag},
1933 resumeSession: true,
1934 })
1935}
1936
David Benjamin43ec06f2014-08-05 02:28:57 -04001937// Adds tests that try to cover the range of the handshake state machine, under
1938// various conditions. Some of these are redundant with other tests, but they
1939// only cover the synchronous case.
David Benjamin6fd297b2014-08-11 18:43:38 -04001940func addStateMachineCoverageTests(async, splitHandshake bool, protocol protocol) {
David Benjamin760b1dd2015-05-15 23:33:48 -04001941 var tests []testCase
1942
1943 // Basic handshake, with resumption. Client and server,
1944 // session ID and session ticket.
1945 tests = append(tests, testCase{
1946 name: "Basic-Client",
1947 resumeSession: true,
1948 })
1949 tests = append(tests, testCase{
1950 name: "Basic-Client-RenewTicket",
1951 config: Config{
1952 Bugs: ProtocolBugs{
1953 RenewTicketOnResume: true,
1954 },
1955 },
1956 resumeSession: true,
1957 })
1958 tests = append(tests, testCase{
1959 name: "Basic-Client-NoTicket",
1960 config: Config{
1961 SessionTicketsDisabled: true,
1962 },
1963 resumeSession: true,
1964 })
1965 tests = append(tests, testCase{
1966 name: "Basic-Client-Implicit",
1967 flags: []string{"-implicit-handshake"},
1968 resumeSession: true,
1969 })
1970 tests = append(tests, testCase{
1971 testType: serverTest,
1972 name: "Basic-Server",
1973 resumeSession: true,
1974 })
1975 tests = append(tests, testCase{
1976 testType: serverTest,
1977 name: "Basic-Server-NoTickets",
1978 config: Config{
1979 SessionTicketsDisabled: true,
1980 },
1981 resumeSession: true,
1982 })
1983 tests = append(tests, testCase{
1984 testType: serverTest,
1985 name: "Basic-Server-Implicit",
1986 flags: []string{"-implicit-handshake"},
1987 resumeSession: true,
1988 })
1989 tests = append(tests, testCase{
1990 testType: serverTest,
1991 name: "Basic-Server-EarlyCallback",
1992 flags: []string{"-use-early-callback"},
1993 resumeSession: true,
1994 })
1995
1996 // TLS client auth.
1997 tests = append(tests, testCase{
1998 testType: clientTest,
1999 name: "ClientAuth-Client",
2000 config: Config{
2001 ClientAuth: RequireAnyClientCert,
2002 },
2003 flags: []string{
2004 "-cert-file", rsaCertificateFile,
2005 "-key-file", rsaKeyFile,
2006 },
2007 })
2008 tests = append(tests, testCase{
2009 testType: serverTest,
2010 name: "ClientAuth-Server",
2011 config: Config{
2012 Certificates: []Certificate{rsaCertificate},
2013 },
2014 flags: []string{"-require-any-client-certificate"},
2015 })
2016
2017 // No session ticket support; server doesn't send NewSessionTicket.
2018 tests = append(tests, testCase{
2019 name: "SessionTicketsDisabled-Client",
2020 config: Config{
2021 SessionTicketsDisabled: true,
2022 },
2023 })
2024 tests = append(tests, testCase{
2025 testType: serverTest,
2026 name: "SessionTicketsDisabled-Server",
2027 config: Config{
2028 SessionTicketsDisabled: true,
2029 },
2030 })
2031
2032 // Skip ServerKeyExchange in PSK key exchange if there's no
2033 // identity hint.
2034 tests = append(tests, testCase{
2035 name: "EmptyPSKHint-Client",
2036 config: Config{
2037 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
2038 PreSharedKey: []byte("secret"),
2039 },
2040 flags: []string{"-psk", "secret"},
2041 })
2042 tests = append(tests, testCase{
2043 testType: serverTest,
2044 name: "EmptyPSKHint-Server",
2045 config: Config{
2046 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
2047 PreSharedKey: []byte("secret"),
2048 },
2049 flags: []string{"-psk", "secret"},
2050 })
2051
2052 if protocol == tls {
2053 tests = append(tests, testCase{
2054 name: "Renegotiate-Client",
2055 renegotiate: true,
2056 })
2057 // NPN on client and server; results in post-handshake message.
2058 tests = append(tests, testCase{
2059 name: "NPN-Client",
2060 config: Config{
2061 NextProtos: []string{"foo"},
2062 },
2063 flags: []string{"-select-next-proto", "foo"},
2064 expectedNextProto: "foo",
2065 expectedNextProtoType: npn,
2066 })
2067 tests = append(tests, testCase{
2068 testType: serverTest,
2069 name: "NPN-Server",
2070 config: Config{
2071 NextProtos: []string{"bar"},
2072 },
2073 flags: []string{
2074 "-advertise-npn", "\x03foo\x03bar\x03baz",
2075 "-expect-next-proto", "bar",
2076 },
2077 expectedNextProto: "bar",
2078 expectedNextProtoType: npn,
2079 })
2080
2081 // TODO(davidben): Add tests for when False Start doesn't trigger.
2082
2083 // Client does False Start and negotiates NPN.
2084 tests = append(tests, testCase{
2085 name: "FalseStart",
2086 config: Config{
2087 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2088 NextProtos: []string{"foo"},
2089 Bugs: ProtocolBugs{
2090 ExpectFalseStart: true,
2091 },
2092 },
2093 flags: []string{
2094 "-false-start",
2095 "-select-next-proto", "foo",
2096 },
2097 shimWritesFirst: true,
2098 resumeSession: true,
2099 })
2100
2101 // Client does False Start and negotiates ALPN.
2102 tests = append(tests, testCase{
2103 name: "FalseStart-ALPN",
2104 config: Config{
2105 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2106 NextProtos: []string{"foo"},
2107 Bugs: ProtocolBugs{
2108 ExpectFalseStart: true,
2109 },
2110 },
2111 flags: []string{
2112 "-false-start",
2113 "-advertise-alpn", "\x03foo",
2114 },
2115 shimWritesFirst: true,
2116 resumeSession: true,
2117 })
2118
2119 // Client does False Start but doesn't explicitly call
2120 // SSL_connect.
2121 tests = append(tests, testCase{
2122 name: "FalseStart-Implicit",
2123 config: Config{
2124 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2125 NextProtos: []string{"foo"},
2126 },
2127 flags: []string{
2128 "-implicit-handshake",
2129 "-false-start",
2130 "-advertise-alpn", "\x03foo",
2131 },
2132 })
2133
2134 // False Start without session tickets.
2135 tests = append(tests, testCase{
2136 name: "FalseStart-SessionTicketsDisabled",
2137 config: Config{
2138 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2139 NextProtos: []string{"foo"},
2140 SessionTicketsDisabled: true,
2141 Bugs: ProtocolBugs{
2142 ExpectFalseStart: true,
2143 },
2144 },
2145 flags: []string{
2146 "-false-start",
2147 "-select-next-proto", "foo",
2148 },
2149 shimWritesFirst: true,
2150 })
2151
2152 // Server parses a V2ClientHello.
2153 tests = append(tests, testCase{
2154 testType: serverTest,
2155 name: "SendV2ClientHello",
2156 config: Config{
2157 // Choose a cipher suite that does not involve
2158 // elliptic curves, so no extensions are
2159 // involved.
2160 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2161 Bugs: ProtocolBugs{
2162 SendV2ClientHello: true,
2163 },
2164 },
2165 })
2166
2167 // Client sends a Channel ID.
2168 tests = append(tests, testCase{
2169 name: "ChannelID-Client",
2170 config: Config{
2171 RequestChannelID: true,
2172 },
2173 flags: []string{"-send-channel-id", channelIDKeyFile},
2174 resumeSession: true,
2175 expectChannelID: true,
2176 })
2177
2178 // Server accepts a Channel ID.
2179 tests = append(tests, testCase{
2180 testType: serverTest,
2181 name: "ChannelID-Server",
2182 config: Config{
2183 ChannelID: channelIDKey,
2184 },
2185 flags: []string{
2186 "-expect-channel-id",
2187 base64.StdEncoding.EncodeToString(channelIDBytes),
2188 },
2189 resumeSession: true,
2190 expectChannelID: true,
2191 })
2192 } else {
2193 tests = append(tests, testCase{
2194 name: "SkipHelloVerifyRequest",
2195 config: Config{
2196 Bugs: ProtocolBugs{
2197 SkipHelloVerifyRequest: true,
2198 },
2199 },
2200 })
2201 }
2202
David Benjamin43ec06f2014-08-05 02:28:57 -04002203 var suffix string
2204 var flags []string
2205 var maxHandshakeRecordLength int
David Benjamin6fd297b2014-08-11 18:43:38 -04002206 if protocol == dtls {
2207 suffix = "-DTLS"
2208 }
David Benjamin43ec06f2014-08-05 02:28:57 -04002209 if async {
David Benjamin6fd297b2014-08-11 18:43:38 -04002210 suffix += "-Async"
David Benjamin43ec06f2014-08-05 02:28:57 -04002211 flags = append(flags, "-async")
2212 } else {
David Benjamin6fd297b2014-08-11 18:43:38 -04002213 suffix += "-Sync"
David Benjamin43ec06f2014-08-05 02:28:57 -04002214 }
2215 if splitHandshake {
2216 suffix += "-SplitHandshakeRecords"
David Benjamin98214542014-08-07 18:02:39 -04002217 maxHandshakeRecordLength = 1
David Benjamin43ec06f2014-08-05 02:28:57 -04002218 }
David Benjamin760b1dd2015-05-15 23:33:48 -04002219 for _, test := range tests {
2220 test.protocol = protocol
2221 test.name += suffix
2222 test.config.Bugs.MaxHandshakeRecordLength = maxHandshakeRecordLength
2223 test.flags = append(test.flags, flags...)
2224 testCases = append(testCases, test)
David Benjamin6fd297b2014-08-11 18:43:38 -04002225 }
David Benjamin43ec06f2014-08-05 02:28:57 -04002226}
2227
Adam Langley524e7172015-02-20 16:04:00 -08002228func addDDoSCallbackTests() {
2229 // DDoS callback.
2230
2231 for _, resume := range []bool{false, true} {
2232 suffix := "Resume"
2233 if resume {
2234 suffix = "No" + suffix
2235 }
2236
2237 testCases = append(testCases, testCase{
2238 testType: serverTest,
2239 name: "Server-DDoS-OK-" + suffix,
2240 flags: []string{"-install-ddos-callback"},
2241 resumeSession: resume,
2242 })
2243
2244 failFlag := "-fail-ddos-callback"
2245 if resume {
2246 failFlag = "-fail-second-ddos-callback"
2247 }
2248 testCases = append(testCases, testCase{
2249 testType: serverTest,
2250 name: "Server-DDoS-Reject-" + suffix,
2251 flags: []string{"-install-ddos-callback", failFlag},
2252 resumeSession: resume,
2253 shouldFail: true,
2254 expectedError: ":CONNECTION_REJECTED:",
2255 })
2256 }
2257}
2258
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002259func addVersionNegotiationTests() {
2260 for i, shimVers := range tlsVersions {
2261 // Assemble flags to disable all newer versions on the shim.
2262 var flags []string
2263 for _, vers := range tlsVersions[i+1:] {
2264 flags = append(flags, vers.flag)
2265 }
2266
2267 for _, runnerVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05002268 protocols := []protocol{tls}
2269 if runnerVers.hasDTLS && shimVers.hasDTLS {
2270 protocols = append(protocols, dtls)
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002271 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002272 for _, protocol := range protocols {
2273 expectedVersion := shimVers.version
2274 if runnerVers.version < shimVers.version {
2275 expectedVersion = runnerVers.version
2276 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002277
David Benjamin8b8c0062014-11-23 02:47:52 -05002278 suffix := shimVers.name + "-" + runnerVers.name
2279 if protocol == dtls {
2280 suffix += "-DTLS"
2281 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002282
David Benjamin1eb367c2014-12-12 18:17:51 -05002283 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
2284
David Benjamin1e29a6b2014-12-10 02:27:24 -05002285 clientVers := shimVers.version
2286 if clientVers > VersionTLS10 {
2287 clientVers = VersionTLS10
2288 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002289 testCases = append(testCases, testCase{
2290 protocol: protocol,
2291 testType: clientTest,
2292 name: "VersionNegotiation-Client-" + suffix,
2293 config: Config{
2294 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05002295 Bugs: ProtocolBugs{
2296 ExpectInitialRecordVersion: clientVers,
2297 },
David Benjamin8b8c0062014-11-23 02:47:52 -05002298 },
2299 flags: flags,
2300 expectedVersion: expectedVersion,
2301 })
David Benjamin1eb367c2014-12-12 18:17:51 -05002302 testCases = append(testCases, testCase{
2303 protocol: protocol,
2304 testType: clientTest,
2305 name: "VersionNegotiation-Client2-" + suffix,
2306 config: Config{
2307 MaxVersion: runnerVers.version,
2308 Bugs: ProtocolBugs{
2309 ExpectInitialRecordVersion: clientVers,
2310 },
2311 },
2312 flags: []string{"-max-version", shimVersFlag},
2313 expectedVersion: expectedVersion,
2314 })
David Benjamin8b8c0062014-11-23 02:47:52 -05002315
2316 testCases = append(testCases, testCase{
2317 protocol: protocol,
2318 testType: serverTest,
2319 name: "VersionNegotiation-Server-" + suffix,
2320 config: Config{
2321 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05002322 Bugs: ProtocolBugs{
2323 ExpectInitialRecordVersion: expectedVersion,
2324 },
David Benjamin8b8c0062014-11-23 02:47:52 -05002325 },
2326 flags: flags,
2327 expectedVersion: expectedVersion,
2328 })
David Benjamin1eb367c2014-12-12 18:17:51 -05002329 testCases = append(testCases, testCase{
2330 protocol: protocol,
2331 testType: serverTest,
2332 name: "VersionNegotiation-Server2-" + suffix,
2333 config: Config{
2334 MaxVersion: runnerVers.version,
2335 Bugs: ProtocolBugs{
2336 ExpectInitialRecordVersion: expectedVersion,
2337 },
2338 },
2339 flags: []string{"-max-version", shimVersFlag},
2340 expectedVersion: expectedVersion,
2341 })
David Benjamin8b8c0062014-11-23 02:47:52 -05002342 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002343 }
2344 }
2345}
2346
David Benjaminaccb4542014-12-12 23:44:33 -05002347func addMinimumVersionTests() {
2348 for i, shimVers := range tlsVersions {
2349 // Assemble flags to disable all older versions on the shim.
2350 var flags []string
2351 for _, vers := range tlsVersions[:i] {
2352 flags = append(flags, vers.flag)
2353 }
2354
2355 for _, runnerVers := range tlsVersions {
2356 protocols := []protocol{tls}
2357 if runnerVers.hasDTLS && shimVers.hasDTLS {
2358 protocols = append(protocols, dtls)
2359 }
2360 for _, protocol := range protocols {
2361 suffix := shimVers.name + "-" + runnerVers.name
2362 if protocol == dtls {
2363 suffix += "-DTLS"
2364 }
2365 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
2366
David Benjaminaccb4542014-12-12 23:44:33 -05002367 var expectedVersion uint16
2368 var shouldFail bool
2369 var expectedError string
David Benjamin87909c02014-12-13 01:55:01 -05002370 var expectedLocalError string
David Benjaminaccb4542014-12-12 23:44:33 -05002371 if runnerVers.version >= shimVers.version {
2372 expectedVersion = runnerVers.version
2373 } else {
2374 shouldFail = true
2375 expectedError = ":UNSUPPORTED_PROTOCOL:"
David Benjamin87909c02014-12-13 01:55:01 -05002376 if runnerVers.version > VersionSSL30 {
2377 expectedLocalError = "remote error: protocol version not supported"
2378 } else {
2379 expectedLocalError = "remote error: handshake failure"
2380 }
David Benjaminaccb4542014-12-12 23:44:33 -05002381 }
2382
2383 testCases = append(testCases, testCase{
2384 protocol: protocol,
2385 testType: clientTest,
2386 name: "MinimumVersion-Client-" + suffix,
2387 config: Config{
2388 MaxVersion: runnerVers.version,
2389 },
David Benjamin87909c02014-12-13 01:55:01 -05002390 flags: flags,
2391 expectedVersion: expectedVersion,
2392 shouldFail: shouldFail,
2393 expectedError: expectedError,
2394 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002395 })
2396 testCases = append(testCases, testCase{
2397 protocol: protocol,
2398 testType: clientTest,
2399 name: "MinimumVersion-Client2-" + suffix,
2400 config: Config{
2401 MaxVersion: runnerVers.version,
2402 },
David Benjamin87909c02014-12-13 01:55:01 -05002403 flags: []string{"-min-version", shimVersFlag},
2404 expectedVersion: expectedVersion,
2405 shouldFail: shouldFail,
2406 expectedError: expectedError,
2407 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002408 })
2409
2410 testCases = append(testCases, testCase{
2411 protocol: protocol,
2412 testType: serverTest,
2413 name: "MinimumVersion-Server-" + suffix,
2414 config: Config{
2415 MaxVersion: runnerVers.version,
2416 },
David Benjamin87909c02014-12-13 01:55:01 -05002417 flags: flags,
2418 expectedVersion: expectedVersion,
2419 shouldFail: shouldFail,
2420 expectedError: expectedError,
2421 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002422 })
2423 testCases = append(testCases, testCase{
2424 protocol: protocol,
2425 testType: serverTest,
2426 name: "MinimumVersion-Server2-" + suffix,
2427 config: Config{
2428 MaxVersion: runnerVers.version,
2429 },
David Benjamin87909c02014-12-13 01:55:01 -05002430 flags: []string{"-min-version", shimVersFlag},
2431 expectedVersion: expectedVersion,
2432 shouldFail: shouldFail,
2433 expectedError: expectedError,
2434 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002435 })
2436 }
2437 }
2438 }
2439}
2440
David Benjamin5c24a1d2014-08-31 00:59:27 -04002441func addD5BugTests() {
2442 testCases = append(testCases, testCase{
2443 testType: serverTest,
2444 name: "D5Bug-NoQuirk-Reject",
2445 config: Config{
2446 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2447 Bugs: ProtocolBugs{
2448 SSL3RSAKeyExchange: true,
2449 },
2450 },
2451 shouldFail: true,
2452 expectedError: ":TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG:",
2453 })
2454 testCases = append(testCases, testCase{
2455 testType: serverTest,
2456 name: "D5Bug-Quirk-Normal",
2457 config: Config{
2458 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2459 },
2460 flags: []string{"-tls-d5-bug"},
2461 })
2462 testCases = append(testCases, testCase{
2463 testType: serverTest,
2464 name: "D5Bug-Quirk-Bug",
2465 config: Config{
2466 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2467 Bugs: ProtocolBugs{
2468 SSL3RSAKeyExchange: true,
2469 },
2470 },
2471 flags: []string{"-tls-d5-bug"},
2472 })
2473}
2474
David Benjamine78bfde2014-09-06 12:45:15 -04002475func addExtensionTests() {
2476 testCases = append(testCases, testCase{
2477 testType: clientTest,
2478 name: "DuplicateExtensionClient",
2479 config: Config{
2480 Bugs: ProtocolBugs{
2481 DuplicateExtension: true,
2482 },
2483 },
2484 shouldFail: true,
2485 expectedLocalError: "remote error: error decoding message",
2486 })
2487 testCases = append(testCases, testCase{
2488 testType: serverTest,
2489 name: "DuplicateExtensionServer",
2490 config: Config{
2491 Bugs: ProtocolBugs{
2492 DuplicateExtension: true,
2493 },
2494 },
2495 shouldFail: true,
2496 expectedLocalError: "remote error: error decoding message",
2497 })
2498 testCases = append(testCases, testCase{
2499 testType: clientTest,
2500 name: "ServerNameExtensionClient",
2501 config: Config{
2502 Bugs: ProtocolBugs{
2503 ExpectServerName: "example.com",
2504 },
2505 },
2506 flags: []string{"-host-name", "example.com"},
2507 })
2508 testCases = append(testCases, testCase{
2509 testType: clientTest,
David Benjamin5f237bc2015-02-11 17:14:15 -05002510 name: "ServerNameExtensionClientMismatch",
David Benjamine78bfde2014-09-06 12:45:15 -04002511 config: Config{
2512 Bugs: ProtocolBugs{
2513 ExpectServerName: "mismatch.com",
2514 },
2515 },
2516 flags: []string{"-host-name", "example.com"},
2517 shouldFail: true,
2518 expectedLocalError: "tls: unexpected server name",
2519 })
2520 testCases = append(testCases, testCase{
2521 testType: clientTest,
David Benjamin5f237bc2015-02-11 17:14:15 -05002522 name: "ServerNameExtensionClientMissing",
David Benjamine78bfde2014-09-06 12:45:15 -04002523 config: Config{
2524 Bugs: ProtocolBugs{
2525 ExpectServerName: "missing.com",
2526 },
2527 },
2528 shouldFail: true,
2529 expectedLocalError: "tls: unexpected server name",
2530 })
2531 testCases = append(testCases, testCase{
2532 testType: serverTest,
2533 name: "ServerNameExtensionServer",
2534 config: Config{
2535 ServerName: "example.com",
2536 },
2537 flags: []string{"-expect-server-name", "example.com"},
2538 resumeSession: true,
2539 })
David Benjaminae2888f2014-09-06 12:58:58 -04002540 testCases = append(testCases, testCase{
2541 testType: clientTest,
2542 name: "ALPNClient",
2543 config: Config{
2544 NextProtos: []string{"foo"},
2545 },
2546 flags: []string{
2547 "-advertise-alpn", "\x03foo\x03bar\x03baz",
2548 "-expect-alpn", "foo",
2549 },
David Benjaminfc7b0862014-09-06 13:21:53 -04002550 expectedNextProto: "foo",
2551 expectedNextProtoType: alpn,
2552 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04002553 })
2554 testCases = append(testCases, testCase{
2555 testType: serverTest,
2556 name: "ALPNServer",
2557 config: Config{
2558 NextProtos: []string{"foo", "bar", "baz"},
2559 },
2560 flags: []string{
2561 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2562 "-select-alpn", "foo",
2563 },
David Benjaminfc7b0862014-09-06 13:21:53 -04002564 expectedNextProto: "foo",
2565 expectedNextProtoType: alpn,
2566 resumeSession: true,
2567 })
2568 // Test that the server prefers ALPN over NPN.
2569 testCases = append(testCases, testCase{
2570 testType: serverTest,
2571 name: "ALPNServer-Preferred",
2572 config: Config{
2573 NextProtos: []string{"foo", "bar", "baz"},
2574 },
2575 flags: []string{
2576 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2577 "-select-alpn", "foo",
2578 "-advertise-npn", "\x03foo\x03bar\x03baz",
2579 },
2580 expectedNextProto: "foo",
2581 expectedNextProtoType: alpn,
2582 resumeSession: true,
2583 })
2584 testCases = append(testCases, testCase{
2585 testType: serverTest,
2586 name: "ALPNServer-Preferred-Swapped",
2587 config: Config{
2588 NextProtos: []string{"foo", "bar", "baz"},
2589 Bugs: ProtocolBugs{
2590 SwapNPNAndALPN: true,
2591 },
2592 },
2593 flags: []string{
2594 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2595 "-select-alpn", "foo",
2596 "-advertise-npn", "\x03foo\x03bar\x03baz",
2597 },
2598 expectedNextProto: "foo",
2599 expectedNextProtoType: alpn,
2600 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04002601 })
Adam Langley38311732014-10-16 19:04:35 -07002602 // Resume with a corrupt ticket.
2603 testCases = append(testCases, testCase{
2604 testType: serverTest,
2605 name: "CorruptTicket",
2606 config: Config{
2607 Bugs: ProtocolBugs{
2608 CorruptTicket: true,
2609 },
2610 },
2611 resumeSession: true,
2612 flags: []string{"-expect-session-miss"},
2613 })
2614 // Resume with an oversized session id.
2615 testCases = append(testCases, testCase{
2616 testType: serverTest,
2617 name: "OversizedSessionId",
2618 config: Config{
2619 Bugs: ProtocolBugs{
2620 OversizedSessionId: true,
2621 },
2622 },
2623 resumeSession: true,
Adam Langley75712922014-10-10 16:23:43 -07002624 shouldFail: true,
Adam Langley38311732014-10-16 19:04:35 -07002625 expectedError: ":DECODE_ERROR:",
2626 })
David Benjaminca6c8262014-11-15 19:06:08 -05002627 // Basic DTLS-SRTP tests. Include fake profiles to ensure they
2628 // are ignored.
2629 testCases = append(testCases, testCase{
2630 protocol: dtls,
2631 name: "SRTP-Client",
2632 config: Config{
2633 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
2634 },
2635 flags: []string{
2636 "-srtp-profiles",
2637 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2638 },
2639 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2640 })
2641 testCases = append(testCases, testCase{
2642 protocol: dtls,
2643 testType: serverTest,
2644 name: "SRTP-Server",
2645 config: Config{
2646 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
2647 },
2648 flags: []string{
2649 "-srtp-profiles",
2650 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2651 },
2652 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2653 })
2654 // Test that the MKI is ignored.
2655 testCases = append(testCases, testCase{
2656 protocol: dtls,
2657 testType: serverTest,
2658 name: "SRTP-Server-IgnoreMKI",
2659 config: Config{
2660 SRTPProtectionProfiles: []uint16{SRTP_AES128_CM_HMAC_SHA1_80},
2661 Bugs: ProtocolBugs{
2662 SRTPMasterKeyIdentifer: "bogus",
2663 },
2664 },
2665 flags: []string{
2666 "-srtp-profiles",
2667 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2668 },
2669 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2670 })
2671 // Test that SRTP isn't negotiated on the server if there were
2672 // no matching profiles.
2673 testCases = append(testCases, testCase{
2674 protocol: dtls,
2675 testType: serverTest,
2676 name: "SRTP-Server-NoMatch",
2677 config: Config{
2678 SRTPProtectionProfiles: []uint16{100, 101, 102},
2679 },
2680 flags: []string{
2681 "-srtp-profiles",
2682 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2683 },
2684 expectedSRTPProtectionProfile: 0,
2685 })
2686 // Test that the server returning an invalid SRTP profile is
2687 // flagged as an error by the client.
2688 testCases = append(testCases, testCase{
2689 protocol: dtls,
2690 name: "SRTP-Client-NoMatch",
2691 config: Config{
2692 Bugs: ProtocolBugs{
2693 SendSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_32,
2694 },
2695 },
2696 flags: []string{
2697 "-srtp-profiles",
2698 "SRTP_AES128_CM_SHA1_80",
2699 },
2700 shouldFail: true,
2701 expectedError: ":BAD_SRTP_PROTECTION_PROFILE_LIST:",
2702 })
David Benjamin61f95272014-11-25 01:55:35 -05002703 // Test OCSP stapling and SCT list.
2704 testCases = append(testCases, testCase{
2705 name: "OCSPStapling",
2706 flags: []string{
2707 "-enable-ocsp-stapling",
2708 "-expect-ocsp-response",
2709 base64.StdEncoding.EncodeToString(testOCSPResponse),
2710 },
2711 })
2712 testCases = append(testCases, testCase{
2713 name: "SignedCertificateTimestampList",
2714 flags: []string{
2715 "-enable-signed-cert-timestamps",
2716 "-expect-signed-cert-timestamps",
2717 base64.StdEncoding.EncodeToString(testSCTList),
2718 },
2719 })
David Benjamine78bfde2014-09-06 12:45:15 -04002720}
2721
David Benjamin01fe8202014-09-24 15:21:44 -04002722func addResumptionVersionTests() {
David Benjamin01fe8202014-09-24 15:21:44 -04002723 for _, sessionVers := range tlsVersions {
David Benjamin01fe8202014-09-24 15:21:44 -04002724 for _, resumeVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05002725 protocols := []protocol{tls}
2726 if sessionVers.hasDTLS && resumeVers.hasDTLS {
2727 protocols = append(protocols, dtls)
David Benjaminbdf5e722014-11-11 00:52:15 -05002728 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002729 for _, protocol := range protocols {
2730 suffix := "-" + sessionVers.name + "-" + resumeVers.name
2731 if protocol == dtls {
2732 suffix += "-DTLS"
2733 }
2734
David Benjaminece3de92015-03-16 18:02:20 -04002735 if sessionVers.version == resumeVers.version {
2736 testCases = append(testCases, testCase{
2737 protocol: protocol,
2738 name: "Resume-Client" + suffix,
2739 resumeSession: true,
2740 config: Config{
2741 MaxVersion: sessionVers.version,
2742 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
David Benjamin8b8c0062014-11-23 02:47:52 -05002743 },
David Benjaminece3de92015-03-16 18:02:20 -04002744 expectedVersion: sessionVers.version,
2745 expectedResumeVersion: resumeVers.version,
2746 })
2747 } else {
2748 testCases = append(testCases, testCase{
2749 protocol: protocol,
2750 name: "Resume-Client-Mismatch" + suffix,
2751 resumeSession: true,
2752 config: Config{
2753 MaxVersion: sessionVers.version,
2754 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
David Benjamin8b8c0062014-11-23 02:47:52 -05002755 },
David Benjaminece3de92015-03-16 18:02:20 -04002756 expectedVersion: sessionVers.version,
2757 resumeConfig: &Config{
2758 MaxVersion: resumeVers.version,
2759 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2760 Bugs: ProtocolBugs{
2761 AllowSessionVersionMismatch: true,
2762 },
2763 },
2764 expectedResumeVersion: resumeVers.version,
2765 shouldFail: true,
2766 expectedError: ":OLD_SESSION_VERSION_NOT_RETURNED:",
2767 })
2768 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002769
2770 testCases = append(testCases, testCase{
2771 protocol: protocol,
2772 name: "Resume-Client-NoResume" + suffix,
2773 flags: []string{"-expect-session-miss"},
2774 resumeSession: true,
2775 config: Config{
2776 MaxVersion: sessionVers.version,
2777 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2778 },
2779 expectedVersion: sessionVers.version,
2780 resumeConfig: &Config{
2781 MaxVersion: resumeVers.version,
2782 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2783 },
2784 newSessionsOnResume: true,
2785 expectedResumeVersion: resumeVers.version,
2786 })
2787
2788 var flags []string
2789 if sessionVers.version != resumeVers.version {
2790 flags = append(flags, "-expect-session-miss")
2791 }
2792 testCases = append(testCases, testCase{
2793 protocol: protocol,
2794 testType: serverTest,
2795 name: "Resume-Server" + suffix,
2796 flags: flags,
2797 resumeSession: true,
2798 config: Config{
2799 MaxVersion: sessionVers.version,
2800 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2801 },
2802 expectedVersion: sessionVers.version,
2803 resumeConfig: &Config{
2804 MaxVersion: resumeVers.version,
2805 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2806 },
2807 expectedResumeVersion: resumeVers.version,
2808 })
2809 }
David Benjamin01fe8202014-09-24 15:21:44 -04002810 }
2811 }
David Benjaminece3de92015-03-16 18:02:20 -04002812
2813 testCases = append(testCases, testCase{
2814 name: "Resume-Client-CipherMismatch",
2815 resumeSession: true,
2816 config: Config{
2817 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2818 },
2819 resumeConfig: &Config{
2820 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2821 Bugs: ProtocolBugs{
2822 SendCipherSuite: TLS_RSA_WITH_AES_128_CBC_SHA,
2823 },
2824 },
2825 shouldFail: true,
2826 expectedError: ":OLD_SESSION_CIPHER_NOT_RETURNED:",
2827 })
David Benjamin01fe8202014-09-24 15:21:44 -04002828}
2829
Adam Langley2ae77d22014-10-28 17:29:33 -07002830func addRenegotiationTests() {
David Benjamin44d3eed2015-05-21 01:29:55 -04002831 // Servers cannot renegotiate.
David Benjaminb16346b2015-04-08 19:16:58 -04002832 testCases = append(testCases, testCase{
2833 testType: serverTest,
David Benjamin44d3eed2015-05-21 01:29:55 -04002834 name: "Renegotiate-Server-Forbidden",
David Benjaminb16346b2015-04-08 19:16:58 -04002835 renegotiate: true,
2836 flags: []string{"-reject-peer-renegotiations"},
2837 shouldFail: true,
2838 expectedError: ":NO_RENEGOTIATION:",
2839 expectedLocalError: "remote error: no renegotiation",
2840 })
Adam Langley2ae77d22014-10-28 17:29:33 -07002841 // TODO(agl): test the renegotiation info SCSV.
Adam Langleycf2d4f42014-10-28 19:06:14 -07002842 testCases = append(testCases, testCase{
David Benjamin4b27d9f2015-05-12 22:42:52 -04002843 name: "Renegotiate-Client",
David Benjamincdea40c2015-03-19 14:09:43 -04002844 config: Config{
2845 Bugs: ProtocolBugs{
David Benjamin4b27d9f2015-05-12 22:42:52 -04002846 FailIfResumeOnRenego: true,
David Benjamincdea40c2015-03-19 14:09:43 -04002847 },
2848 },
2849 renegotiate: true,
2850 })
2851 testCases = append(testCases, testCase{
Adam Langleycf2d4f42014-10-28 19:06:14 -07002852 name: "Renegotiate-Client-EmptyExt",
2853 renegotiate: true,
2854 config: Config{
2855 Bugs: ProtocolBugs{
2856 EmptyRenegotiationInfo: true,
2857 },
2858 },
2859 shouldFail: true,
2860 expectedError: ":RENEGOTIATION_MISMATCH:",
2861 })
2862 testCases = append(testCases, testCase{
2863 name: "Renegotiate-Client-BadExt",
2864 renegotiate: true,
2865 config: Config{
2866 Bugs: ProtocolBugs{
2867 BadRenegotiationInfo: true,
2868 },
2869 },
2870 shouldFail: true,
2871 expectedError: ":RENEGOTIATION_MISMATCH:",
2872 })
2873 testCases = append(testCases, testCase{
David Benjamincff0b902015-05-15 23:09:47 -04002874 name: "Renegotiate-Client-NoExt",
2875 renegotiate: true,
2876 config: Config{
2877 Bugs: ProtocolBugs{
2878 NoRenegotiationInfo: true,
2879 },
2880 },
2881 shouldFail: true,
2882 expectedError: ":UNSAFE_LEGACY_RENEGOTIATION_DISABLED:",
2883 flags: []string{"-no-legacy-server-connect"},
2884 })
2885 testCases = append(testCases, testCase{
2886 name: "Renegotiate-Client-NoExt-Allowed",
2887 renegotiate: true,
2888 config: Config{
2889 Bugs: ProtocolBugs{
2890 NoRenegotiationInfo: true,
2891 },
2892 },
2893 })
2894 testCases = append(testCases, testCase{
Adam Langleycf2d4f42014-10-28 19:06:14 -07002895 name: "Renegotiate-Client-SwitchCiphers",
2896 renegotiate: true,
2897 config: Config{
2898 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2899 },
2900 renegotiateCiphers: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2901 })
2902 testCases = append(testCases, testCase{
2903 name: "Renegotiate-Client-SwitchCiphers2",
2904 renegotiate: true,
2905 config: Config{
2906 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2907 },
2908 renegotiateCiphers: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2909 })
David Benjaminc44b1df2014-11-23 12:11:01 -05002910 testCases = append(testCases, testCase{
David Benjaminb16346b2015-04-08 19:16:58 -04002911 name: "Renegotiate-Client-Forbidden",
2912 renegotiate: true,
2913 flags: []string{"-reject-peer-renegotiations"},
2914 shouldFail: true,
2915 expectedError: ":NO_RENEGOTIATION:",
2916 expectedLocalError: "remote error: no renegotiation",
2917 })
2918 testCases = append(testCases, testCase{
David Benjaminc44b1df2014-11-23 12:11:01 -05002919 name: "Renegotiate-SameClientVersion",
2920 renegotiate: true,
2921 config: Config{
2922 MaxVersion: VersionTLS10,
2923 Bugs: ProtocolBugs{
2924 RequireSameRenegoClientVersion: true,
2925 },
2926 },
2927 })
Adam Langley2ae77d22014-10-28 17:29:33 -07002928}
2929
David Benjamin5e961c12014-11-07 01:48:35 -05002930func addDTLSReplayTests() {
2931 // Test that sequence number replays are detected.
2932 testCases = append(testCases, testCase{
2933 protocol: dtls,
2934 name: "DTLS-Replay",
2935 replayWrites: true,
2936 })
2937
2938 // Test the outgoing sequence number skipping by values larger
2939 // than the retransmit window.
2940 testCases = append(testCases, testCase{
2941 protocol: dtls,
2942 name: "DTLS-Replay-LargeGaps",
2943 config: Config{
2944 Bugs: ProtocolBugs{
2945 SequenceNumberIncrement: 127,
2946 },
2947 },
2948 replayWrites: true,
2949 })
2950}
2951
Feng Lu41aa3252014-11-21 22:47:56 -08002952func addFastRadioPaddingTests() {
David Benjamin1e29a6b2014-12-10 02:27:24 -05002953 testCases = append(testCases, testCase{
2954 protocol: tls,
2955 name: "FastRadio-Padding",
Feng Lu41aa3252014-11-21 22:47:56 -08002956 config: Config{
2957 Bugs: ProtocolBugs{
2958 RequireFastradioPadding: true,
2959 },
2960 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05002961 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08002962 })
David Benjamin1e29a6b2014-12-10 02:27:24 -05002963 testCases = append(testCases, testCase{
2964 protocol: dtls,
David Benjamin5f237bc2015-02-11 17:14:15 -05002965 name: "FastRadio-Padding-DTLS",
Feng Lu41aa3252014-11-21 22:47:56 -08002966 config: Config{
2967 Bugs: ProtocolBugs{
2968 RequireFastradioPadding: true,
2969 },
2970 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05002971 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08002972 })
2973}
2974
David Benjamin000800a2014-11-14 01:43:59 -05002975var testHashes = []struct {
2976 name string
2977 id uint8
2978}{
2979 {"SHA1", hashSHA1},
2980 {"SHA224", hashSHA224},
2981 {"SHA256", hashSHA256},
2982 {"SHA384", hashSHA384},
2983 {"SHA512", hashSHA512},
2984}
2985
2986func addSigningHashTests() {
2987 // Make sure each hash works. Include some fake hashes in the list and
2988 // ensure they're ignored.
2989 for _, hash := range testHashes {
2990 testCases = append(testCases, testCase{
2991 name: "SigningHash-ClientAuth-" + hash.name,
2992 config: Config{
2993 ClientAuth: RequireAnyClientCert,
2994 SignatureAndHashes: []signatureAndHash{
2995 {signatureRSA, 42},
2996 {signatureRSA, hash.id},
2997 {signatureRSA, 255},
2998 },
2999 },
3000 flags: []string{
3001 "-cert-file", rsaCertificateFile,
3002 "-key-file", rsaKeyFile,
3003 },
3004 })
3005
3006 testCases = append(testCases, testCase{
3007 testType: serverTest,
3008 name: "SigningHash-ServerKeyExchange-Sign-" + hash.name,
3009 config: Config{
3010 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
3011 SignatureAndHashes: []signatureAndHash{
3012 {signatureRSA, 42},
3013 {signatureRSA, hash.id},
3014 {signatureRSA, 255},
3015 },
3016 },
3017 })
3018 }
3019
3020 // Test that hash resolution takes the signature type into account.
3021 testCases = append(testCases, testCase{
3022 name: "SigningHash-ClientAuth-SignatureType",
3023 config: Config{
3024 ClientAuth: RequireAnyClientCert,
3025 SignatureAndHashes: []signatureAndHash{
3026 {signatureECDSA, hashSHA512},
3027 {signatureRSA, hashSHA384},
3028 {signatureECDSA, hashSHA1},
3029 },
3030 },
3031 flags: []string{
3032 "-cert-file", rsaCertificateFile,
3033 "-key-file", rsaKeyFile,
3034 },
3035 })
3036
3037 testCases = append(testCases, testCase{
3038 testType: serverTest,
3039 name: "SigningHash-ServerKeyExchange-SignatureType",
3040 config: Config{
3041 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
3042 SignatureAndHashes: []signatureAndHash{
3043 {signatureECDSA, hashSHA512},
3044 {signatureRSA, hashSHA384},
3045 {signatureECDSA, hashSHA1},
3046 },
3047 },
3048 })
3049
3050 // Test that, if the list is missing, the peer falls back to SHA-1.
3051 testCases = append(testCases, testCase{
3052 name: "SigningHash-ClientAuth-Fallback",
3053 config: Config{
3054 ClientAuth: RequireAnyClientCert,
3055 SignatureAndHashes: []signatureAndHash{
3056 {signatureRSA, hashSHA1},
3057 },
3058 Bugs: ProtocolBugs{
3059 NoSignatureAndHashes: true,
3060 },
3061 },
3062 flags: []string{
3063 "-cert-file", rsaCertificateFile,
3064 "-key-file", rsaKeyFile,
3065 },
3066 })
3067
3068 testCases = append(testCases, testCase{
3069 testType: serverTest,
3070 name: "SigningHash-ServerKeyExchange-Fallback",
3071 config: Config{
3072 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
3073 SignatureAndHashes: []signatureAndHash{
3074 {signatureRSA, hashSHA1},
3075 },
3076 Bugs: ProtocolBugs{
3077 NoSignatureAndHashes: true,
3078 },
3079 },
3080 })
David Benjamin72dc7832015-03-16 17:49:43 -04003081
3082 // Test that hash preferences are enforced. BoringSSL defaults to
3083 // rejecting MD5 signatures.
3084 testCases = append(testCases, testCase{
3085 testType: serverTest,
3086 name: "SigningHash-ClientAuth-Enforced",
3087 config: Config{
3088 Certificates: []Certificate{rsaCertificate},
3089 SignatureAndHashes: []signatureAndHash{
3090 {signatureRSA, hashMD5},
3091 // Advertise SHA-1 so the handshake will
3092 // proceed, but the shim's preferences will be
3093 // ignored in CertificateVerify generation, so
3094 // MD5 will be chosen.
3095 {signatureRSA, hashSHA1},
3096 },
3097 Bugs: ProtocolBugs{
3098 IgnorePeerSignatureAlgorithmPreferences: true,
3099 },
3100 },
3101 flags: []string{"-require-any-client-certificate"},
3102 shouldFail: true,
3103 expectedError: ":WRONG_SIGNATURE_TYPE:",
3104 })
3105
3106 testCases = append(testCases, testCase{
3107 name: "SigningHash-ServerKeyExchange-Enforced",
3108 config: Config{
3109 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
3110 SignatureAndHashes: []signatureAndHash{
3111 {signatureRSA, hashMD5},
3112 },
3113 Bugs: ProtocolBugs{
3114 IgnorePeerSignatureAlgorithmPreferences: true,
3115 },
3116 },
3117 shouldFail: true,
3118 expectedError: ":WRONG_SIGNATURE_TYPE:",
3119 })
David Benjamin000800a2014-11-14 01:43:59 -05003120}
3121
David Benjamin83f90402015-01-27 01:09:43 -05003122// timeouts is the retransmit schedule for BoringSSL. It doubles and
3123// caps at 60 seconds. On the 13th timeout, it gives up.
3124var timeouts = []time.Duration{
3125 1 * time.Second,
3126 2 * time.Second,
3127 4 * time.Second,
3128 8 * time.Second,
3129 16 * time.Second,
3130 32 * time.Second,
3131 60 * time.Second,
3132 60 * time.Second,
3133 60 * time.Second,
3134 60 * time.Second,
3135 60 * time.Second,
3136 60 * time.Second,
3137 60 * time.Second,
3138}
3139
3140func addDTLSRetransmitTests() {
3141 // Test that this is indeed the timeout schedule. Stress all
3142 // four patterns of handshake.
3143 for i := 1; i < len(timeouts); i++ {
3144 number := strconv.Itoa(i)
3145 testCases = append(testCases, testCase{
3146 protocol: dtls,
3147 name: "DTLS-Retransmit-Client-" + number,
3148 config: Config{
3149 Bugs: ProtocolBugs{
3150 TimeoutSchedule: timeouts[:i],
3151 },
3152 },
3153 resumeSession: true,
3154 flags: []string{"-async"},
3155 })
3156 testCases = append(testCases, testCase{
3157 protocol: dtls,
3158 testType: serverTest,
3159 name: "DTLS-Retransmit-Server-" + number,
3160 config: Config{
3161 Bugs: ProtocolBugs{
3162 TimeoutSchedule: timeouts[:i],
3163 },
3164 },
3165 resumeSession: true,
3166 flags: []string{"-async"},
3167 })
3168 }
3169
3170 // Test that exceeding the timeout schedule hits a read
3171 // timeout.
3172 testCases = append(testCases, testCase{
3173 protocol: dtls,
3174 name: "DTLS-Retransmit-Timeout",
3175 config: Config{
3176 Bugs: ProtocolBugs{
3177 TimeoutSchedule: timeouts,
3178 },
3179 },
3180 resumeSession: true,
3181 flags: []string{"-async"},
3182 shouldFail: true,
3183 expectedError: ":READ_TIMEOUT_EXPIRED:",
3184 })
3185
3186 // Test that timeout handling has a fudge factor, due to API
3187 // problems.
3188 testCases = append(testCases, testCase{
3189 protocol: dtls,
3190 name: "DTLS-Retransmit-Fudge",
3191 config: Config{
3192 Bugs: ProtocolBugs{
3193 TimeoutSchedule: []time.Duration{
3194 timeouts[0] - 10*time.Millisecond,
3195 },
3196 },
3197 },
3198 resumeSession: true,
3199 flags: []string{"-async"},
3200 })
David Benjamin7eaab4c2015-03-02 19:01:16 -05003201
3202 // Test that the final Finished retransmitting isn't
3203 // duplicated if the peer badly fragments everything.
3204 testCases = append(testCases, testCase{
3205 testType: serverTest,
3206 protocol: dtls,
3207 name: "DTLS-Retransmit-Fragmented",
3208 config: Config{
3209 Bugs: ProtocolBugs{
3210 TimeoutSchedule: []time.Duration{timeouts[0]},
3211 MaxHandshakeRecordLength: 2,
3212 },
3213 },
3214 flags: []string{"-async"},
3215 })
David Benjamin83f90402015-01-27 01:09:43 -05003216}
3217
David Benjaminc565ebb2015-04-03 04:06:36 -04003218func addExportKeyingMaterialTests() {
3219 for _, vers := range tlsVersions {
3220 if vers.version == VersionSSL30 {
3221 continue
3222 }
3223 testCases = append(testCases, testCase{
3224 name: "ExportKeyingMaterial-" + vers.name,
3225 config: Config{
3226 MaxVersion: vers.version,
3227 },
3228 exportKeyingMaterial: 1024,
3229 exportLabel: "label",
3230 exportContext: "context",
3231 useExportContext: true,
3232 })
3233 testCases = append(testCases, testCase{
3234 name: "ExportKeyingMaterial-NoContext-" + vers.name,
3235 config: Config{
3236 MaxVersion: vers.version,
3237 },
3238 exportKeyingMaterial: 1024,
3239 })
3240 testCases = append(testCases, testCase{
3241 name: "ExportKeyingMaterial-EmptyContext-" + vers.name,
3242 config: Config{
3243 MaxVersion: vers.version,
3244 },
3245 exportKeyingMaterial: 1024,
3246 useExportContext: true,
3247 })
3248 testCases = append(testCases, testCase{
3249 name: "ExportKeyingMaterial-Small-" + vers.name,
3250 config: Config{
3251 MaxVersion: vers.version,
3252 },
3253 exportKeyingMaterial: 1,
3254 exportLabel: "label",
3255 exportContext: "context",
3256 useExportContext: true,
3257 })
3258 }
3259 testCases = append(testCases, testCase{
3260 name: "ExportKeyingMaterial-SSL3",
3261 config: Config{
3262 MaxVersion: VersionSSL30,
3263 },
3264 exportKeyingMaterial: 1024,
3265 exportLabel: "label",
3266 exportContext: "context",
3267 useExportContext: true,
3268 shouldFail: true,
3269 expectedError: "failed to export keying material",
3270 })
3271}
3272
David Benjamin884fdf12014-08-02 15:28:23 -04003273func worker(statusChan chan statusMsg, c chan *testCase, buildDir string, wg *sync.WaitGroup) {
Adam Langley95c29f32014-06-20 12:00:00 -07003274 defer wg.Done()
3275
3276 for test := range c {
Adam Langley69a01602014-11-17 17:26:55 -08003277 var err error
3278
3279 if *mallocTest < 0 {
3280 statusChan <- statusMsg{test: test, started: true}
3281 err = runTest(test, buildDir, -1)
3282 } else {
3283 for mallocNumToFail := int64(*mallocTest); ; mallocNumToFail++ {
3284 statusChan <- statusMsg{test: test, started: true}
3285 if err = runTest(test, buildDir, mallocNumToFail); err != errMoreMallocs {
3286 if err != nil {
3287 fmt.Printf("\n\nmalloc test failed at %d: %s\n", mallocNumToFail, err)
3288 }
3289 break
3290 }
3291 }
3292 }
Adam Langley95c29f32014-06-20 12:00:00 -07003293 statusChan <- statusMsg{test: test, err: err}
3294 }
3295}
3296
3297type statusMsg struct {
3298 test *testCase
3299 started bool
3300 err error
3301}
3302
David Benjamin5f237bc2015-02-11 17:14:15 -05003303func statusPrinter(doneChan chan *testOutput, statusChan chan statusMsg, total int) {
Adam Langley95c29f32014-06-20 12:00:00 -07003304 var started, done, failed, lineLen int
Adam Langley95c29f32014-06-20 12:00:00 -07003305
David Benjamin5f237bc2015-02-11 17:14:15 -05003306 testOutput := newTestOutput()
Adam Langley95c29f32014-06-20 12:00:00 -07003307 for msg := range statusChan {
David Benjamin5f237bc2015-02-11 17:14:15 -05003308 if !*pipe {
3309 // Erase the previous status line.
David Benjamin87c8a642015-02-21 01:54:29 -05003310 var erase string
3311 for i := 0; i < lineLen; i++ {
3312 erase += "\b \b"
3313 }
3314 fmt.Print(erase)
David Benjamin5f237bc2015-02-11 17:14:15 -05003315 }
3316
Adam Langley95c29f32014-06-20 12:00:00 -07003317 if msg.started {
3318 started++
3319 } else {
3320 done++
David Benjamin5f237bc2015-02-11 17:14:15 -05003321
3322 if msg.err != nil {
3323 fmt.Printf("FAILED (%s)\n%s\n", msg.test.name, msg.err)
3324 failed++
3325 testOutput.addResult(msg.test.name, "FAIL")
3326 } else {
3327 if *pipe {
3328 // Print each test instead of a status line.
3329 fmt.Printf("PASSED (%s)\n", msg.test.name)
3330 }
3331 testOutput.addResult(msg.test.name, "PASS")
3332 }
Adam Langley95c29f32014-06-20 12:00:00 -07003333 }
3334
David Benjamin5f237bc2015-02-11 17:14:15 -05003335 if !*pipe {
3336 // Print a new status line.
3337 line := fmt.Sprintf("%d/%d/%d/%d", failed, done, started, total)
3338 lineLen = len(line)
3339 os.Stdout.WriteString(line)
Adam Langley95c29f32014-06-20 12:00:00 -07003340 }
Adam Langley95c29f32014-06-20 12:00:00 -07003341 }
David Benjamin5f237bc2015-02-11 17:14:15 -05003342
3343 doneChan <- testOutput
Adam Langley95c29f32014-06-20 12:00:00 -07003344}
3345
3346func main() {
3347 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 -04003348 var flagNumWorkers *int = flag.Int("num-workers", runtime.NumCPU(), "The number of workers to run in parallel.")
David Benjamin884fdf12014-08-02 15:28:23 -04003349 var flagBuildDir *string = flag.String("build-dir", "../../../build", "The build directory to run the shim from.")
Adam Langley95c29f32014-06-20 12:00:00 -07003350
3351 flag.Parse()
3352
3353 addCipherSuiteTests()
3354 addBadECDSASignatureTests()
Adam Langley80842bd2014-06-20 12:00:00 -07003355 addCBCPaddingTests()
Kenny Root7fdeaf12014-08-05 15:23:37 -07003356 addCBCSplittingTests()
David Benjamin636293b2014-07-08 17:59:18 -04003357 addClientAuthTests()
Adam Langley524e7172015-02-20 16:04:00 -08003358 addDDoSCallbackTests()
David Benjamin7e2e6cf2014-08-07 17:44:24 -04003359 addVersionNegotiationTests()
David Benjaminaccb4542014-12-12 23:44:33 -05003360 addMinimumVersionTests()
David Benjamin5c24a1d2014-08-31 00:59:27 -04003361 addD5BugTests()
David Benjamine78bfde2014-09-06 12:45:15 -04003362 addExtensionTests()
David Benjamin01fe8202014-09-24 15:21:44 -04003363 addResumptionVersionTests()
Adam Langley75712922014-10-10 16:23:43 -07003364 addExtendedMasterSecretTests()
Adam Langley2ae77d22014-10-28 17:29:33 -07003365 addRenegotiationTests()
David Benjamin5e961c12014-11-07 01:48:35 -05003366 addDTLSReplayTests()
David Benjamin000800a2014-11-14 01:43:59 -05003367 addSigningHashTests()
Feng Lu41aa3252014-11-21 22:47:56 -08003368 addFastRadioPaddingTests()
David Benjamin83f90402015-01-27 01:09:43 -05003369 addDTLSRetransmitTests()
David Benjaminc565ebb2015-04-03 04:06:36 -04003370 addExportKeyingMaterialTests()
David Benjamin43ec06f2014-08-05 02:28:57 -04003371 for _, async := range []bool{false, true} {
3372 for _, splitHandshake := range []bool{false, true} {
David Benjamin6fd297b2014-08-11 18:43:38 -04003373 for _, protocol := range []protocol{tls, dtls} {
3374 addStateMachineCoverageTests(async, splitHandshake, protocol)
3375 }
David Benjamin43ec06f2014-08-05 02:28:57 -04003376 }
3377 }
Adam Langley95c29f32014-06-20 12:00:00 -07003378
3379 var wg sync.WaitGroup
3380
David Benjamin2bc8e6f2014-08-02 15:22:37 -04003381 numWorkers := *flagNumWorkers
Adam Langley95c29f32014-06-20 12:00:00 -07003382
3383 statusChan := make(chan statusMsg, numWorkers)
3384 testChan := make(chan *testCase, numWorkers)
David Benjamin5f237bc2015-02-11 17:14:15 -05003385 doneChan := make(chan *testOutput)
Adam Langley95c29f32014-06-20 12:00:00 -07003386
David Benjamin025b3d32014-07-01 19:53:04 -04003387 go statusPrinter(doneChan, statusChan, len(testCases))
Adam Langley95c29f32014-06-20 12:00:00 -07003388
3389 for i := 0; i < numWorkers; i++ {
3390 wg.Add(1)
David Benjamin884fdf12014-08-02 15:28:23 -04003391 go worker(statusChan, testChan, *flagBuildDir, &wg)
Adam Langley95c29f32014-06-20 12:00:00 -07003392 }
3393
David Benjamin025b3d32014-07-01 19:53:04 -04003394 for i := range testCases {
3395 if len(*flagTest) == 0 || *flagTest == testCases[i].name {
3396 testChan <- &testCases[i]
Adam Langley95c29f32014-06-20 12:00:00 -07003397 }
3398 }
3399
3400 close(testChan)
3401 wg.Wait()
3402 close(statusChan)
David Benjamin5f237bc2015-02-11 17:14:15 -05003403 testOutput := <-doneChan
Adam Langley95c29f32014-06-20 12:00:00 -07003404
3405 fmt.Printf("\n")
David Benjamin5f237bc2015-02-11 17:14:15 -05003406
3407 if *jsonOutput != "" {
3408 if err := testOutput.writeTo(*jsonOutput); err != nil {
3409 fmt.Fprintf(os.Stderr, "Error: %s\n", err)
3410 }
3411 }
David Benjamin2ab7a862015-04-04 17:02:18 -04003412
3413 if !testOutput.allPassed {
3414 os.Exit(1)
3415 }
Adam Langley95c29f32014-06-20 12:00:00 -07003416}