blob: d72ac436d631e60dca42b7406f3b456c7214b623 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001package main
2
3import (
4 "bytes"
David Benjamina08e49d2014-08-24 01:46:07 -04005 "crypto/ecdsa"
6 "crypto/elliptic"
David Benjamin407a10c2014-07-16 12:58:59 -04007 "crypto/x509"
David Benjamin2561dc32014-08-24 01:25:27 -04008 "encoding/base64"
David Benjamina08e49d2014-08-24 01:46:07 -04009 "encoding/pem"
Adam Langley95c29f32014-06-20 12:00:00 -070010 "flag"
11 "fmt"
12 "io"
Kenny Root7fdeaf12014-08-05 15:23:37 -070013 "io/ioutil"
Adam Langley95c29f32014-06-20 12:00:00 -070014 "net"
15 "os"
16 "os/exec"
David Benjamin884fdf12014-08-02 15:28:23 -040017 "path"
David Benjamin2bc8e6f2014-08-02 15:22:37 -040018 "runtime"
Adam Langley69a01602014-11-17 17:26:55 -080019 "strconv"
Adam Langley95c29f32014-06-20 12:00:00 -070020 "strings"
21 "sync"
22 "syscall"
David Benjamin83f90402015-01-27 01:09:43 -050023 "time"
Adam Langley95c29f32014-06-20 12:00:00 -070024)
25
Adam Langley69a01602014-11-17 17:26:55 -080026var (
David Benjamin5f237bc2015-02-11 17:14:15 -050027 useValgrind = flag.Bool("valgrind", false, "If true, run code under valgrind")
28 useGDB = flag.Bool("gdb", false, "If true, run BoringSSL code under gdb")
29 flagDebug = flag.Bool("debug", false, "Hexdump the contents of the connection")
30 mallocTest = flag.Int64("malloc-test", -1, "If non-negative, run each test with each malloc in turn failing from the given number onwards.")
31 mallocTestDebug = flag.Bool("malloc-test-debug", false, "If true, ask bssl_shim to abort rather than fail a malloc. This can be used with a specific value for --malloc-test to identity the malloc failing that is causing problems.")
32 jsonOutput = flag.String("json-output", "", "The file to output JSON results to.")
33 pipe = flag.Bool("pipe", false, "If true, print status output suitable for piping into another program.")
Adam Langley69a01602014-11-17 17:26:55 -080034)
Adam Langley95c29f32014-06-20 12:00:00 -070035
David Benjamin025b3d32014-07-01 19:53:04 -040036const (
37 rsaCertificateFile = "cert.pem"
38 ecdsaCertificateFile = "ecdsa_cert.pem"
39)
40
41const (
David Benjamina08e49d2014-08-24 01:46:07 -040042 rsaKeyFile = "key.pem"
43 ecdsaKeyFile = "ecdsa_key.pem"
44 channelIDKeyFile = "channel_id_key.pem"
David Benjamin025b3d32014-07-01 19:53:04 -040045)
46
Adam Langley95c29f32014-06-20 12:00:00 -070047var rsaCertificate, ecdsaCertificate Certificate
David Benjamina08e49d2014-08-24 01:46:07 -040048var channelIDKey *ecdsa.PrivateKey
49var channelIDBytes []byte
Adam Langley95c29f32014-06-20 12:00:00 -070050
David Benjamin61f95272014-11-25 01:55:35 -050051var testOCSPResponse = []byte{1, 2, 3, 4}
52var testSCTList = []byte{5, 6, 7, 8}
53
Adam Langley95c29f32014-06-20 12:00:00 -070054func initCertificates() {
55 var err error
David Benjamin025b3d32014-07-01 19:53:04 -040056 rsaCertificate, err = LoadX509KeyPair(rsaCertificateFile, rsaKeyFile)
Adam Langley95c29f32014-06-20 12:00:00 -070057 if err != nil {
58 panic(err)
59 }
David Benjamin61f95272014-11-25 01:55:35 -050060 rsaCertificate.OCSPStaple = testOCSPResponse
61 rsaCertificate.SignedCertificateTimestampList = testSCTList
Adam Langley95c29f32014-06-20 12:00:00 -070062
David Benjamin025b3d32014-07-01 19:53:04 -040063 ecdsaCertificate, err = LoadX509KeyPair(ecdsaCertificateFile, ecdsaKeyFile)
Adam Langley95c29f32014-06-20 12:00:00 -070064 if err != nil {
65 panic(err)
66 }
David Benjamin61f95272014-11-25 01:55:35 -050067 ecdsaCertificate.OCSPStaple = testOCSPResponse
68 ecdsaCertificate.SignedCertificateTimestampList = testSCTList
David Benjamina08e49d2014-08-24 01:46:07 -040069
70 channelIDPEMBlock, err := ioutil.ReadFile(channelIDKeyFile)
71 if err != nil {
72 panic(err)
73 }
74 channelIDDERBlock, _ := pem.Decode(channelIDPEMBlock)
75 if channelIDDERBlock.Type != "EC PRIVATE KEY" {
76 panic("bad key type")
77 }
78 channelIDKey, err = x509.ParseECPrivateKey(channelIDDERBlock.Bytes)
79 if err != nil {
80 panic(err)
81 }
82 if channelIDKey.Curve != elliptic.P256() {
83 panic("bad curve")
84 }
85
86 channelIDBytes = make([]byte, 64)
87 writeIntPadded(channelIDBytes[:32], channelIDKey.X)
88 writeIntPadded(channelIDBytes[32:], channelIDKey.Y)
Adam Langley95c29f32014-06-20 12:00:00 -070089}
90
91var certificateOnce sync.Once
92
93func getRSACertificate() Certificate {
94 certificateOnce.Do(initCertificates)
95 return rsaCertificate
96}
97
98func getECDSACertificate() Certificate {
99 certificateOnce.Do(initCertificates)
100 return ecdsaCertificate
101}
102
David Benjamin025b3d32014-07-01 19:53:04 -0400103type testType int
104
105const (
106 clientTest testType = iota
107 serverTest
108)
109
David Benjamin6fd297b2014-08-11 18:43:38 -0400110type protocol int
111
112const (
113 tls protocol = iota
114 dtls
115)
116
David Benjaminfc7b0862014-09-06 13:21:53 -0400117const (
118 alpn = 1
119 npn = 2
120)
121
Adam Langley95c29f32014-06-20 12:00:00 -0700122type testCase struct {
David Benjamin025b3d32014-07-01 19:53:04 -0400123 testType testType
David Benjamin6fd297b2014-08-11 18:43:38 -0400124 protocol protocol
Adam Langley95c29f32014-06-20 12:00:00 -0700125 name string
126 config Config
127 shouldFail bool
128 expectedError string
Adam Langleyac61fa32014-06-23 12:03:11 -0700129 // expectedLocalError, if not empty, contains a substring that must be
130 // found in the local error.
131 expectedLocalError string
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400132 // expectedVersion, if non-zero, specifies the TLS version that must be
133 // negotiated.
134 expectedVersion uint16
David Benjamin01fe8202014-09-24 15:21:44 -0400135 // expectedResumeVersion, if non-zero, specifies the TLS version that
136 // must be negotiated on resumption. If zero, expectedVersion is used.
137 expectedResumeVersion uint16
David Benjamina08e49d2014-08-24 01:46:07 -0400138 // expectChannelID controls whether the connection should have
139 // negotiated a Channel ID with channelIDKey.
140 expectChannelID bool
David Benjaminae2888f2014-09-06 12:58:58 -0400141 // expectedNextProto controls whether the connection should
142 // negotiate a next protocol via NPN or ALPN.
143 expectedNextProto string
David Benjaminfc7b0862014-09-06 13:21:53 -0400144 // expectedNextProtoType, if non-zero, is the expected next
145 // protocol negotiation mechanism.
146 expectedNextProtoType int
David Benjaminca6c8262014-11-15 19:06:08 -0500147 // expectedSRTPProtectionProfile is the DTLS-SRTP profile that
148 // should be negotiated. If zero, none should be negotiated.
149 expectedSRTPProtectionProfile uint16
Adam Langley80842bd2014-06-20 12:00:00 -0700150 // messageLen is the length, in bytes, of the test message that will be
151 // sent.
152 messageLen int
David Benjamin025b3d32014-07-01 19:53:04 -0400153 // certFile is the path to the certificate to use for the server.
154 certFile string
155 // keyFile is the path to the private key to use for the server.
156 keyFile string
David Benjamin1d5c83e2014-07-22 19:20:02 -0400157 // resumeSession controls whether a second connection should be tested
David Benjamin01fe8202014-09-24 15:21:44 -0400158 // which attempts to resume the first session.
David Benjamin1d5c83e2014-07-22 19:20:02 -0400159 resumeSession bool
David Benjamin01fe8202014-09-24 15:21:44 -0400160 // resumeConfig, if not nil, points to a Config to be used on
David Benjaminfe8eb9a2014-11-17 03:19:02 -0500161 // resumption. Unless newSessionsOnResume is set,
162 // SessionTicketKey, ServerSessionCache, and
163 // ClientSessionCache are copied from the initial connection's
164 // config. If nil, the initial connection's config is used.
David Benjamin01fe8202014-09-24 15:21:44 -0400165 resumeConfig *Config
David Benjaminfe8eb9a2014-11-17 03:19:02 -0500166 // newSessionsOnResume, if true, will cause resumeConfig to
167 // use a different session resumption context.
168 newSessionsOnResume bool
David Benjamin98e882e2014-08-08 13:24:34 -0400169 // sendPrefix sends a prefix on the socket before actually performing a
170 // handshake.
171 sendPrefix string
David Benjamine58c4f52014-08-24 03:47:07 -0400172 // shimWritesFirst controls whether the shim sends an initial "hello"
173 // message before doing a roundtrip with the runner.
174 shimWritesFirst bool
Adam Langleycf2d4f42014-10-28 19:06:14 -0700175 // renegotiate indicates the the connection should be renegotiated
176 // during the exchange.
177 renegotiate bool
178 // renegotiateCiphers is a list of ciphersuite ids that will be
179 // switched in just before renegotiation.
180 renegotiateCiphers []uint16
David Benjamin5e961c12014-11-07 01:48:35 -0500181 // replayWrites, if true, configures the underlying transport
182 // to replay every write it makes in DTLS tests.
183 replayWrites bool
David Benjamin5fa3eba2015-01-22 16:35:40 -0500184 // damageFirstWrite, if true, configures the underlying transport to
185 // damage the final byte of the first application data write.
186 damageFirstWrite bool
David Benjamin325b5c32014-07-01 19:40:31 -0400187 // flags, if not empty, contains a list of command-line flags that will
188 // be passed to the shim program.
189 flags []string
Adam Langley95c29f32014-06-20 12:00:00 -0700190}
191
David Benjamin025b3d32014-07-01 19:53:04 -0400192var testCases = []testCase{
Adam Langley95c29f32014-06-20 12:00:00 -0700193 {
194 name: "BadRSASignature",
195 config: Config{
196 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
197 Bugs: ProtocolBugs{
198 InvalidSKXSignature: true,
199 },
200 },
201 shouldFail: true,
202 expectedError: ":BAD_SIGNATURE:",
203 },
204 {
205 name: "BadECDSASignature",
206 config: Config{
207 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
208 Bugs: ProtocolBugs{
209 InvalidSKXSignature: true,
210 },
211 Certificates: []Certificate{getECDSACertificate()},
212 },
213 shouldFail: true,
214 expectedError: ":BAD_SIGNATURE:",
215 },
216 {
217 name: "BadECDSACurve",
218 config: Config{
219 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
220 Bugs: ProtocolBugs{
221 InvalidSKXCurve: true,
222 },
223 Certificates: []Certificate{getECDSACertificate()},
224 },
225 shouldFail: true,
226 expectedError: ":WRONG_CURVE:",
227 },
Adam Langleyac61fa32014-06-23 12:03:11 -0700228 {
David Benjamina8e3e0e2014-08-06 22:11:10 -0400229 testType: serverTest,
230 name: "BadRSAVersion",
231 config: Config{
232 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
233 Bugs: ProtocolBugs{
234 RsaClientKeyExchangeVersion: VersionTLS11,
235 },
236 },
237 shouldFail: true,
238 expectedError: ":DECRYPTION_FAILED_OR_BAD_RECORD_MAC:",
239 },
240 {
David Benjamin325b5c32014-07-01 19:40:31 -0400241 name: "NoFallbackSCSV",
Adam Langleyac61fa32014-06-23 12:03:11 -0700242 config: Config{
243 Bugs: ProtocolBugs{
244 FailIfNotFallbackSCSV: true,
245 },
246 },
247 shouldFail: true,
248 expectedLocalError: "no fallback SCSV found",
249 },
David Benjamin325b5c32014-07-01 19:40:31 -0400250 {
David Benjamin2a0c4962014-08-22 23:46:35 -0400251 name: "SendFallbackSCSV",
David Benjamin325b5c32014-07-01 19:40:31 -0400252 config: Config{
253 Bugs: ProtocolBugs{
254 FailIfNotFallbackSCSV: true,
255 },
256 },
257 flags: []string{"-fallback-scsv"},
258 },
David Benjamin197b3ab2014-07-02 18:37:33 -0400259 {
David Benjamin7b030512014-07-08 17:30:11 -0400260 name: "ClientCertificateTypes",
261 config: Config{
262 ClientAuth: RequestClientCert,
263 ClientCertificateTypes: []byte{
264 CertTypeDSSSign,
265 CertTypeRSASign,
266 CertTypeECDSASign,
267 },
268 },
David Benjamin2561dc32014-08-24 01:25:27 -0400269 flags: []string{
270 "-expect-certificate-types",
271 base64.StdEncoding.EncodeToString([]byte{
272 CertTypeDSSSign,
273 CertTypeRSASign,
274 CertTypeECDSASign,
275 }),
276 },
David Benjamin7b030512014-07-08 17:30:11 -0400277 },
David Benjamin636293b2014-07-08 17:59:18 -0400278 {
279 name: "NoClientCertificate",
280 config: Config{
281 ClientAuth: RequireAnyClientCert,
282 },
283 shouldFail: true,
284 expectedLocalError: "client didn't provide a certificate",
285 },
David Benjamin1c375dd2014-07-12 00:48:23 -0400286 {
287 name: "UnauthenticatedECDH",
288 config: Config{
289 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
290 Bugs: ProtocolBugs{
291 UnauthenticatedECDH: true,
292 },
293 },
294 shouldFail: true,
David Benjamine8f3d662014-07-12 01:10:19 -0400295 expectedError: ":UNEXPECTED_MESSAGE:",
David Benjamin1c375dd2014-07-12 00:48:23 -0400296 },
David Benjamin9c651c92014-07-12 13:27:45 -0400297 {
298 name: "SkipServerKeyExchange",
299 config: Config{
300 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
301 Bugs: ProtocolBugs{
302 SkipServerKeyExchange: true,
303 },
304 },
305 shouldFail: true,
306 expectedError: ":UNEXPECTED_MESSAGE:",
307 },
David Benjamin1f5f62b2014-07-12 16:18:02 -0400308 {
David Benjamina0e52232014-07-19 17:39:58 -0400309 name: "SkipChangeCipherSpec-Client",
310 config: Config{
311 Bugs: ProtocolBugs{
312 SkipChangeCipherSpec: true,
313 },
314 },
315 shouldFail: true,
David Benjamin86271ee2014-07-21 16:14:03 -0400316 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
David Benjamina0e52232014-07-19 17:39:58 -0400317 },
318 {
319 testType: serverTest,
320 name: "SkipChangeCipherSpec-Server",
321 config: Config{
322 Bugs: ProtocolBugs{
323 SkipChangeCipherSpec: true,
324 },
325 },
326 shouldFail: true,
David Benjamin86271ee2014-07-21 16:14:03 -0400327 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
David Benjamina0e52232014-07-19 17:39:58 -0400328 },
David Benjamin42be6452014-07-21 14:50:23 -0400329 {
330 testType: serverTest,
331 name: "SkipChangeCipherSpec-Server-NPN",
332 config: Config{
333 NextProtos: []string{"bar"},
334 Bugs: ProtocolBugs{
335 SkipChangeCipherSpec: true,
336 },
337 },
338 flags: []string{
339 "-advertise-npn", "\x03foo\x03bar\x03baz",
340 },
341 shouldFail: true,
David Benjamin86271ee2014-07-21 16:14:03 -0400342 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
343 },
344 {
345 name: "FragmentAcrossChangeCipherSpec-Client",
346 config: Config{
347 Bugs: ProtocolBugs{
348 FragmentAcrossChangeCipherSpec: true,
349 },
350 },
351 shouldFail: true,
352 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
353 },
354 {
355 testType: serverTest,
356 name: "FragmentAcrossChangeCipherSpec-Server",
357 config: Config{
358 Bugs: ProtocolBugs{
359 FragmentAcrossChangeCipherSpec: true,
360 },
361 },
362 shouldFail: true,
363 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
364 },
365 {
366 testType: serverTest,
367 name: "FragmentAcrossChangeCipherSpec-Server-NPN",
368 config: Config{
369 NextProtos: []string{"bar"},
370 Bugs: ProtocolBugs{
371 FragmentAcrossChangeCipherSpec: true,
372 },
373 },
374 flags: []string{
375 "-advertise-npn", "\x03foo\x03bar\x03baz",
376 },
377 shouldFail: true,
378 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
David Benjamin42be6452014-07-21 14:50:23 -0400379 },
David Benjaminf3ec83d2014-07-21 22:42:34 -0400380 {
381 testType: serverTest,
David Benjamin3fd1fbd2015-02-03 16:07:32 -0500382 name: "Alert",
383 config: Config{
384 Bugs: ProtocolBugs{
385 SendSpuriousAlert: alertRecordOverflow,
386 },
387 },
388 shouldFail: true,
389 expectedError: ":TLSV1_ALERT_RECORD_OVERFLOW:",
390 },
391 {
392 protocol: dtls,
393 testType: serverTest,
394 name: "Alert-DTLS",
395 config: Config{
396 Bugs: ProtocolBugs{
397 SendSpuriousAlert: alertRecordOverflow,
398 },
399 },
400 shouldFail: true,
401 expectedError: ":TLSV1_ALERT_RECORD_OVERFLOW:",
402 },
403 {
404 testType: serverTest,
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400405 name: "FragmentAlert",
406 config: Config{
407 Bugs: ProtocolBugs{
David Benjaminca6c8262014-11-15 19:06:08 -0500408 FragmentAlert: true,
David Benjamin3fd1fbd2015-02-03 16:07:32 -0500409 SendSpuriousAlert: alertRecordOverflow,
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400410 },
411 },
412 shouldFail: true,
413 expectedError: ":BAD_ALERT:",
414 },
415 {
David Benjamin0ea8dda2015-01-31 20:33:40 -0500416 protocol: dtls,
417 testType: serverTest,
418 name: "FragmentAlert-DTLS",
419 config: Config{
420 Bugs: ProtocolBugs{
421 FragmentAlert: true,
David Benjamin3fd1fbd2015-02-03 16:07:32 -0500422 SendSpuriousAlert: alertRecordOverflow,
David Benjamin0ea8dda2015-01-31 20:33:40 -0500423 },
424 },
425 shouldFail: true,
426 expectedError: ":BAD_ALERT:",
427 },
428 {
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400429 testType: serverTest,
David Benjaminf3ec83d2014-07-21 22:42:34 -0400430 name: "EarlyChangeCipherSpec-server-1",
431 config: Config{
432 Bugs: ProtocolBugs{
433 EarlyChangeCipherSpec: 1,
434 },
435 },
436 shouldFail: true,
437 expectedError: ":CCS_RECEIVED_EARLY:",
438 },
439 {
440 testType: serverTest,
441 name: "EarlyChangeCipherSpec-server-2",
442 config: Config{
443 Bugs: ProtocolBugs{
444 EarlyChangeCipherSpec: 2,
445 },
446 },
447 shouldFail: true,
448 expectedError: ":CCS_RECEIVED_EARLY:",
449 },
David Benjamind23f4122014-07-23 15:09:48 -0400450 {
David Benjamind23f4122014-07-23 15:09:48 -0400451 name: "SkipNewSessionTicket",
452 config: Config{
453 Bugs: ProtocolBugs{
454 SkipNewSessionTicket: true,
455 },
456 },
457 shouldFail: true,
458 expectedError: ":CCS_RECEIVED_EARLY:",
459 },
David Benjamin7e3305e2014-07-28 14:52:32 -0400460 {
David Benjamind86c7672014-08-02 04:07:12 -0400461 testType: serverTest,
David Benjaminbef270a2014-08-02 04:22:02 -0400462 name: "FallbackSCSV",
463 config: Config{
464 MaxVersion: VersionTLS11,
465 Bugs: ProtocolBugs{
466 SendFallbackSCSV: true,
467 },
468 },
469 shouldFail: true,
470 expectedError: ":INAPPROPRIATE_FALLBACK:",
471 },
472 {
473 testType: serverTest,
474 name: "FallbackSCSV-VersionMatch",
475 config: Config{
476 Bugs: ProtocolBugs{
477 SendFallbackSCSV: true,
478 },
479 },
480 },
David Benjamin98214542014-08-07 18:02:39 -0400481 {
482 testType: serverTest,
483 name: "FragmentedClientVersion",
484 config: Config{
485 Bugs: ProtocolBugs{
486 MaxHandshakeRecordLength: 1,
487 FragmentClientVersion: true,
488 },
489 },
David Benjamin82c9e902014-12-12 15:55:27 -0500490 expectedVersion: VersionTLS12,
David Benjamin98214542014-08-07 18:02:39 -0400491 },
David Benjamin98e882e2014-08-08 13:24:34 -0400492 {
493 testType: serverTest,
494 name: "MinorVersionTolerance",
495 config: Config{
496 Bugs: ProtocolBugs{
497 SendClientVersion: 0x03ff,
498 },
499 },
500 expectedVersion: VersionTLS12,
501 },
502 {
503 testType: serverTest,
504 name: "MajorVersionTolerance",
505 config: Config{
506 Bugs: ProtocolBugs{
507 SendClientVersion: 0x0400,
508 },
509 },
510 expectedVersion: VersionTLS12,
511 },
512 {
513 testType: serverTest,
514 name: "VersionTooLow",
515 config: Config{
516 Bugs: ProtocolBugs{
517 SendClientVersion: 0x0200,
518 },
519 },
520 shouldFail: true,
521 expectedError: ":UNSUPPORTED_PROTOCOL:",
522 },
523 {
524 testType: serverTest,
525 name: "HttpGET",
526 sendPrefix: "GET / HTTP/1.0\n",
527 shouldFail: true,
528 expectedError: ":HTTP_REQUEST:",
529 },
530 {
531 testType: serverTest,
532 name: "HttpPOST",
533 sendPrefix: "POST / HTTP/1.0\n",
534 shouldFail: true,
535 expectedError: ":HTTP_REQUEST:",
536 },
537 {
538 testType: serverTest,
539 name: "HttpHEAD",
540 sendPrefix: "HEAD / HTTP/1.0\n",
541 shouldFail: true,
542 expectedError: ":HTTP_REQUEST:",
543 },
544 {
545 testType: serverTest,
546 name: "HttpPUT",
547 sendPrefix: "PUT / HTTP/1.0\n",
548 shouldFail: true,
549 expectedError: ":HTTP_REQUEST:",
550 },
551 {
552 testType: serverTest,
553 name: "HttpCONNECT",
554 sendPrefix: "CONNECT www.google.com:443 HTTP/1.0\n",
555 shouldFail: true,
556 expectedError: ":HTTPS_PROXY_REQUEST:",
557 },
David Benjamin39ebf532014-08-31 02:23:49 -0400558 {
David Benjaminf080ecd2014-12-11 18:48:59 -0500559 testType: serverTest,
560 name: "Garbage",
561 sendPrefix: "blah",
562 shouldFail: true,
563 expectedError: ":UNKNOWN_PROTOCOL:",
564 },
565 {
David Benjamin39ebf532014-08-31 02:23:49 -0400566 name: "SkipCipherVersionCheck",
567 config: Config{
568 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
569 MaxVersion: VersionTLS11,
570 Bugs: ProtocolBugs{
571 SkipCipherVersionCheck: true,
572 },
573 },
574 shouldFail: true,
575 expectedError: ":WRONG_CIPHER_RETURNED:",
576 },
David Benjamin9114fae2014-11-08 11:41:14 -0500577 {
David Benjamina3e89492015-02-26 15:16:22 -0500578 name: "RSAEphemeralKey",
David Benjamin9114fae2014-11-08 11:41:14 -0500579 config: Config{
580 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
581 Bugs: ProtocolBugs{
David Benjamina3e89492015-02-26 15:16:22 -0500582 RSAEphemeralKey: true,
David Benjamin9114fae2014-11-08 11:41:14 -0500583 },
584 },
585 shouldFail: true,
586 expectedError: ":UNEXPECTED_MESSAGE:",
587 },
David Benjamin128dbc32014-12-01 01:27:42 -0500588 {
589 name: "DisableEverything",
590 flags: []string{"-no-tls12", "-no-tls11", "-no-tls1", "-no-ssl3"},
591 shouldFail: true,
592 expectedError: ":WRONG_SSL_VERSION:",
593 },
594 {
595 protocol: dtls,
596 name: "DisableEverything-DTLS",
597 flags: []string{"-no-tls12", "-no-tls1"},
598 shouldFail: true,
599 expectedError: ":WRONG_SSL_VERSION:",
600 },
David Benjamin780d6dd2015-01-06 12:03:19 -0500601 {
602 name: "NoSharedCipher",
603 config: Config{
604 CipherSuites: []uint16{},
605 },
606 shouldFail: true,
607 expectedError: ":HANDSHAKE_FAILURE_ON_CLIENT_HELLO:",
608 },
David Benjamin13be1de2015-01-11 16:29:36 -0500609 {
610 protocol: dtls,
611 testType: serverTest,
612 name: "MTU",
613 config: Config{
614 Bugs: ProtocolBugs{
615 MaxPacketLength: 256,
616 },
617 },
618 flags: []string{"-mtu", "256"},
619 },
620 {
621 protocol: dtls,
622 testType: serverTest,
623 name: "MTUExceeded",
624 config: Config{
625 Bugs: ProtocolBugs{
626 MaxPacketLength: 255,
627 },
628 },
629 flags: []string{"-mtu", "256"},
630 shouldFail: true,
631 expectedLocalError: "dtls: exceeded maximum packet length",
632 },
David Benjamin6095de82014-12-27 01:50:38 -0500633 {
634 name: "CertMismatchRSA",
635 config: Config{
636 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
637 Certificates: []Certificate{getECDSACertificate()},
638 Bugs: ProtocolBugs{
639 SendCipherSuite: TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
640 },
641 },
642 shouldFail: true,
643 expectedError: ":WRONG_CERTIFICATE_TYPE:",
644 },
645 {
646 name: "CertMismatchECDSA",
647 config: Config{
648 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
649 Certificates: []Certificate{getRSACertificate()},
650 Bugs: ProtocolBugs{
651 SendCipherSuite: TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
652 },
653 },
654 shouldFail: true,
655 expectedError: ":WRONG_CERTIFICATE_TYPE:",
656 },
David Benjamin5fa3eba2015-01-22 16:35:40 -0500657 {
658 name: "TLSFatalBadPackets",
659 damageFirstWrite: true,
660 shouldFail: true,
661 expectedError: ":DECRYPTION_FAILED_OR_BAD_RECORD_MAC:",
662 },
663 {
664 protocol: dtls,
665 name: "DTLSIgnoreBadPackets",
666 damageFirstWrite: true,
667 },
668 {
669 protocol: dtls,
670 name: "DTLSIgnoreBadPackets-Async",
671 damageFirstWrite: true,
672 flags: []string{"-async"},
673 },
David Benjamin4189bd92015-01-25 23:52:39 -0500674 {
675 name: "AppDataAfterChangeCipherSpec",
676 config: Config{
677 Bugs: ProtocolBugs{
678 AppDataAfterChangeCipherSpec: []byte("TEST MESSAGE"),
679 },
680 },
681 shouldFail: true,
682 expectedError: ":DATA_BETWEEN_CCS_AND_FINISHED:",
683 },
684 {
685 protocol: dtls,
686 name: "AppDataAfterChangeCipherSpec-DTLS",
687 config: Config{
688 Bugs: ProtocolBugs{
689 AppDataAfterChangeCipherSpec: []byte("TEST MESSAGE"),
690 },
691 },
692 },
David Benjaminb3774b92015-01-31 17:16:01 -0500693 {
David Benjamindc3da932015-03-12 15:09:02 -0400694 name: "AlertAfterChangeCipherSpec",
695 config: Config{
696 Bugs: ProtocolBugs{
697 AlertAfterChangeCipherSpec: alertRecordOverflow,
698 },
699 },
700 shouldFail: true,
701 expectedError: ":TLSV1_ALERT_RECORD_OVERFLOW:",
702 },
703 {
704 protocol: dtls,
705 name: "AlertAfterChangeCipherSpec-DTLS",
706 config: Config{
707 Bugs: ProtocolBugs{
708 AlertAfterChangeCipherSpec: alertRecordOverflow,
709 },
710 },
711 shouldFail: true,
712 expectedError: ":TLSV1_ALERT_RECORD_OVERFLOW:",
713 },
714 {
David Benjaminb3774b92015-01-31 17:16:01 -0500715 protocol: dtls,
716 name: "ReorderHandshakeFragments-Small-DTLS",
717 config: Config{
718 Bugs: ProtocolBugs{
719 ReorderHandshakeFragments: true,
720 // Small enough that every handshake message is
721 // fragmented.
722 MaxHandshakeRecordLength: 2,
723 },
724 },
725 },
726 {
727 protocol: dtls,
728 name: "ReorderHandshakeFragments-Large-DTLS",
729 config: Config{
730 Bugs: ProtocolBugs{
731 ReorderHandshakeFragments: true,
732 // Large enough that no handshake message is
733 // fragmented.
David Benjaminb3774b92015-01-31 17:16:01 -0500734 MaxHandshakeRecordLength: 2048,
735 },
736 },
737 },
David Benjaminddb9f152015-02-03 15:44:39 -0500738 {
David Benjamin75381222015-03-02 19:30:30 -0500739 protocol: dtls,
740 name: "MixCompleteMessageWithFragments-DTLS",
741 config: Config{
742 Bugs: ProtocolBugs{
743 ReorderHandshakeFragments: true,
744 MixCompleteMessageWithFragments: true,
745 MaxHandshakeRecordLength: 2,
746 },
747 },
748 },
749 {
David Benjaminddb9f152015-02-03 15:44:39 -0500750 name: "SendInvalidRecordType",
751 config: Config{
752 Bugs: ProtocolBugs{
753 SendInvalidRecordType: true,
754 },
755 },
756 shouldFail: true,
757 expectedError: ":UNEXPECTED_RECORD:",
758 },
759 {
760 protocol: dtls,
761 name: "SendInvalidRecordType-DTLS",
762 config: Config{
763 Bugs: ProtocolBugs{
764 SendInvalidRecordType: true,
765 },
766 },
767 shouldFail: true,
768 expectedError: ":UNEXPECTED_RECORD:",
769 },
David Benjaminb80168e2015-02-08 18:30:14 -0500770 {
771 name: "FalseStart-SkipServerSecondLeg",
772 config: Config{
773 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
774 NextProtos: []string{"foo"},
775 Bugs: ProtocolBugs{
776 SkipNewSessionTicket: true,
777 SkipChangeCipherSpec: true,
778 SkipFinished: true,
779 ExpectFalseStart: true,
780 },
781 },
782 flags: []string{
783 "-false-start",
784 "-advertise-alpn", "\x03foo",
785 },
786 shimWritesFirst: true,
787 shouldFail: true,
788 expectedError: ":UNEXPECTED_RECORD:",
789 },
David Benjamin931ab342015-02-08 19:46:57 -0500790 {
791 name: "FalseStart-SkipServerSecondLeg-Implicit",
792 config: Config{
793 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
794 NextProtos: []string{"foo"},
795 Bugs: ProtocolBugs{
796 SkipNewSessionTicket: true,
797 SkipChangeCipherSpec: true,
798 SkipFinished: true,
799 },
800 },
801 flags: []string{
802 "-implicit-handshake",
803 "-false-start",
804 "-advertise-alpn", "\x03foo",
805 },
806 shouldFail: true,
807 expectedError: ":UNEXPECTED_RECORD:",
808 },
David Benjamin6f5c0f42015-02-24 01:23:21 -0500809 {
810 testType: serverTest,
811 name: "FailEarlyCallback",
812 flags: []string{"-fail-early-callback"},
813 shouldFail: true,
814 expectedError: ":CONNECTION_REJECTED:",
815 expectedLocalError: "remote error: access denied",
816 },
David Benjaminbcb2d912015-02-24 23:45:43 -0500817 {
818 name: "WrongMessageType",
819 config: Config{
820 Bugs: ProtocolBugs{
821 WrongCertificateMessageType: true,
822 },
823 },
824 shouldFail: true,
825 expectedError: ":UNEXPECTED_MESSAGE:",
826 expectedLocalError: "remote error: unexpected message",
827 },
828 {
829 protocol: dtls,
830 name: "WrongMessageType-DTLS",
831 config: Config{
832 Bugs: ProtocolBugs{
833 WrongCertificateMessageType: true,
834 },
835 },
836 shouldFail: true,
837 expectedError: ":UNEXPECTED_MESSAGE:",
838 expectedLocalError: "remote error: unexpected message",
839 },
David Benjamin75381222015-03-02 19:30:30 -0500840 {
841 protocol: dtls,
842 name: "FragmentMessageTypeMismatch-DTLS",
843 config: Config{
844 Bugs: ProtocolBugs{
845 MaxHandshakeRecordLength: 2,
846 FragmentMessageTypeMismatch: true,
847 },
848 },
849 shouldFail: true,
850 expectedError: ":FRAGMENT_MISMATCH:",
851 },
852 {
853 protocol: dtls,
854 name: "FragmentMessageLengthMismatch-DTLS",
855 config: Config{
856 Bugs: ProtocolBugs{
857 MaxHandshakeRecordLength: 2,
858 FragmentMessageLengthMismatch: true,
859 },
860 },
861 shouldFail: true,
862 expectedError: ":FRAGMENT_MISMATCH:",
863 },
864 {
865 protocol: dtls,
866 name: "SplitFragmentHeader-DTLS",
867 config: Config{
868 Bugs: ProtocolBugs{
869 SplitFragmentHeader: true,
870 },
871 },
872 shouldFail: true,
873 expectedError: ":UNEXPECTED_MESSAGE:",
874 },
875 {
876 protocol: dtls,
877 name: "SplitFragmentBody-DTLS",
878 config: Config{
879 Bugs: ProtocolBugs{
880 SplitFragmentBody: true,
881 },
882 },
883 shouldFail: true,
884 expectedError: ":UNEXPECTED_MESSAGE:",
885 },
886 {
887 protocol: dtls,
888 name: "SendEmptyFragments-DTLS",
889 config: Config{
890 Bugs: ProtocolBugs{
891 SendEmptyFragments: true,
892 },
893 },
894 },
David Benjamin67d1fb52015-03-16 15:16:23 -0400895 {
896 name: "UnsupportedCipherSuite",
897 config: Config{
898 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
899 Bugs: ProtocolBugs{
900 IgnorePeerCipherPreferences: true,
901 },
902 },
903 flags: []string{"-cipher", "DEFAULT:!RC4"},
904 shouldFail: true,
905 expectedError: ":WRONG_CIPHER_RETURNED:",
906 },
Adam Langley95c29f32014-06-20 12:00:00 -0700907}
908
David Benjamin01fe8202014-09-24 15:21:44 -0400909func doExchange(test *testCase, config *Config, conn net.Conn, messageLen int, isResume bool) error {
David Benjamin65ea8ff2014-11-23 03:01:00 -0500910 var connDebug *recordingConn
David Benjamin5fa3eba2015-01-22 16:35:40 -0500911 var connDamage *damageAdaptor
David Benjamin65ea8ff2014-11-23 03:01:00 -0500912 if *flagDebug {
913 connDebug = &recordingConn{Conn: conn}
914 conn = connDebug
915 defer func() {
916 connDebug.WriteTo(os.Stdout)
917 }()
918 }
919
David Benjamin6fd297b2014-08-11 18:43:38 -0400920 if test.protocol == dtls {
David Benjamin83f90402015-01-27 01:09:43 -0500921 config.Bugs.PacketAdaptor = newPacketAdaptor(conn)
922 conn = config.Bugs.PacketAdaptor
David Benjamin5e961c12014-11-07 01:48:35 -0500923 if test.replayWrites {
924 conn = newReplayAdaptor(conn)
925 }
David Benjamin6fd297b2014-08-11 18:43:38 -0400926 }
927
David Benjamin5fa3eba2015-01-22 16:35:40 -0500928 if test.damageFirstWrite {
929 connDamage = newDamageAdaptor(conn)
930 conn = connDamage
931 }
932
David Benjamin6fd297b2014-08-11 18:43:38 -0400933 if test.sendPrefix != "" {
934 if _, err := conn.Write([]byte(test.sendPrefix)); err != nil {
935 return err
936 }
David Benjamin98e882e2014-08-08 13:24:34 -0400937 }
938
David Benjamin1d5c83e2014-07-22 19:20:02 -0400939 var tlsConn *Conn
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400940 if test.testType == clientTest {
David Benjamin6fd297b2014-08-11 18:43:38 -0400941 if test.protocol == dtls {
942 tlsConn = DTLSServer(conn, config)
943 } else {
944 tlsConn = Server(conn, config)
945 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400946 } else {
947 config.InsecureSkipVerify = true
David Benjamin6fd297b2014-08-11 18:43:38 -0400948 if test.protocol == dtls {
949 tlsConn = DTLSClient(conn, config)
950 } else {
951 tlsConn = Client(conn, config)
952 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400953 }
954
Adam Langley95c29f32014-06-20 12:00:00 -0700955 if err := tlsConn.Handshake(); err != nil {
956 return err
957 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700958
David Benjamin01fe8202014-09-24 15:21:44 -0400959 // TODO(davidben): move all per-connection expectations into a dedicated
960 // expectations struct that can be specified separately for the two
961 // legs.
962 expectedVersion := test.expectedVersion
963 if isResume && test.expectedResumeVersion != 0 {
964 expectedVersion = test.expectedResumeVersion
965 }
966 if vers := tlsConn.ConnectionState().Version; expectedVersion != 0 && vers != expectedVersion {
967 return fmt.Errorf("got version %x, expected %x", vers, expectedVersion)
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400968 }
969
David Benjamina08e49d2014-08-24 01:46:07 -0400970 if test.expectChannelID {
971 channelID := tlsConn.ConnectionState().ChannelID
972 if channelID == nil {
973 return fmt.Errorf("no channel ID negotiated")
974 }
975 if channelID.Curve != channelIDKey.Curve ||
976 channelIDKey.X.Cmp(channelIDKey.X) != 0 ||
977 channelIDKey.Y.Cmp(channelIDKey.Y) != 0 {
978 return fmt.Errorf("incorrect channel ID")
979 }
980 }
981
David Benjaminae2888f2014-09-06 12:58:58 -0400982 if expected := test.expectedNextProto; expected != "" {
983 if actual := tlsConn.ConnectionState().NegotiatedProtocol; actual != expected {
984 return fmt.Errorf("next proto mismatch: got %s, wanted %s", actual, expected)
985 }
986 }
987
David Benjaminfc7b0862014-09-06 13:21:53 -0400988 if test.expectedNextProtoType != 0 {
989 if (test.expectedNextProtoType == alpn) != tlsConn.ConnectionState().NegotiatedProtocolFromALPN {
990 return fmt.Errorf("next proto type mismatch")
991 }
992 }
993
David Benjaminca6c8262014-11-15 19:06:08 -0500994 if p := tlsConn.ConnectionState().SRTPProtectionProfile; p != test.expectedSRTPProtectionProfile {
995 return fmt.Errorf("SRTP profile mismatch: got %d, wanted %d", p, test.expectedSRTPProtectionProfile)
996 }
997
David Benjamine58c4f52014-08-24 03:47:07 -0400998 if test.shimWritesFirst {
999 var buf [5]byte
1000 _, err := io.ReadFull(tlsConn, buf[:])
1001 if err != nil {
1002 return err
1003 }
1004 if string(buf[:]) != "hello" {
1005 return fmt.Errorf("bad initial message")
1006 }
1007 }
1008
Adam Langleycf2d4f42014-10-28 19:06:14 -07001009 if test.renegotiate {
1010 if test.renegotiateCiphers != nil {
1011 config.CipherSuites = test.renegotiateCiphers
1012 }
1013 if err := tlsConn.Renegotiate(); err != nil {
1014 return err
1015 }
1016 } else if test.renegotiateCiphers != nil {
1017 panic("renegotiateCiphers without renegotiate")
1018 }
1019
David Benjamin5fa3eba2015-01-22 16:35:40 -05001020 if test.damageFirstWrite {
1021 connDamage.setDamage(true)
1022 tlsConn.Write([]byte("DAMAGED WRITE"))
1023 connDamage.setDamage(false)
1024 }
1025
Kenny Root7fdeaf12014-08-05 15:23:37 -07001026 if messageLen < 0 {
David Benjamin6fd297b2014-08-11 18:43:38 -04001027 if test.protocol == dtls {
1028 return fmt.Errorf("messageLen < 0 not supported for DTLS tests")
1029 }
Kenny Root7fdeaf12014-08-05 15:23:37 -07001030 // Read until EOF.
1031 _, err := io.Copy(ioutil.Discard, tlsConn)
1032 return err
1033 }
1034
David Benjamin4189bd92015-01-25 23:52:39 -05001035 var testMessage []byte
1036 if config.Bugs.AppDataAfterChangeCipherSpec != nil {
1037 // We've already sent a message. Expect the shim to echo it
1038 // back.
1039 testMessage = config.Bugs.AppDataAfterChangeCipherSpec
1040 } else {
1041 if messageLen == 0 {
1042 messageLen = 32
1043 }
1044 testMessage = make([]byte, messageLen)
1045 for i := range testMessage {
1046 testMessage[i] = 0x42
1047 }
1048 tlsConn.Write(testMessage)
Adam Langley80842bd2014-06-20 12:00:00 -07001049 }
Adam Langley95c29f32014-06-20 12:00:00 -07001050
1051 buf := make([]byte, len(testMessage))
David Benjamin6fd297b2014-08-11 18:43:38 -04001052 if test.protocol == dtls {
1053 bufTmp := make([]byte, len(buf)+1)
1054 n, err := tlsConn.Read(bufTmp)
1055 if err != nil {
1056 return err
1057 }
1058 if n != len(buf) {
1059 return fmt.Errorf("bad reply; length mismatch (%d vs %d)", n, len(buf))
1060 }
1061 copy(buf, bufTmp)
1062 } else {
1063 _, err := io.ReadFull(tlsConn, buf)
1064 if err != nil {
1065 return err
1066 }
Adam Langley95c29f32014-06-20 12:00:00 -07001067 }
1068
1069 for i, v := range buf {
1070 if v != testMessage[i]^0xff {
1071 return fmt.Errorf("bad reply contents at byte %d", i)
1072 }
1073 }
1074
1075 return nil
1076}
1077
David Benjamin325b5c32014-07-01 19:40:31 -04001078func valgrindOf(dbAttach bool, path string, args ...string) *exec.Cmd {
1079 valgrindArgs := []string{"--error-exitcode=99", "--track-origins=yes", "--leak-check=full"}
Adam Langley95c29f32014-06-20 12:00:00 -07001080 if dbAttach {
David Benjamin325b5c32014-07-01 19:40:31 -04001081 valgrindArgs = append(valgrindArgs, "--db-attach=yes", "--db-command=xterm -e gdb -nw %f %p")
Adam Langley95c29f32014-06-20 12:00:00 -07001082 }
David Benjamin325b5c32014-07-01 19:40:31 -04001083 valgrindArgs = append(valgrindArgs, path)
1084 valgrindArgs = append(valgrindArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -07001085
David Benjamin325b5c32014-07-01 19:40:31 -04001086 return exec.Command("valgrind", valgrindArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -07001087}
1088
David Benjamin325b5c32014-07-01 19:40:31 -04001089func gdbOf(path string, args ...string) *exec.Cmd {
1090 xtermArgs := []string{"-e", "gdb", "--args"}
1091 xtermArgs = append(xtermArgs, path)
1092 xtermArgs = append(xtermArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -07001093
David Benjamin325b5c32014-07-01 19:40:31 -04001094 return exec.Command("xterm", xtermArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -07001095}
1096
Adam Langley69a01602014-11-17 17:26:55 -08001097type moreMallocsError struct{}
1098
1099func (moreMallocsError) Error() string {
1100 return "child process did not exhaust all allocation calls"
1101}
1102
1103var errMoreMallocs = moreMallocsError{}
1104
David Benjamin87c8a642015-02-21 01:54:29 -05001105// accept accepts a connection from listener, unless waitChan signals a process
1106// exit first.
1107func acceptOrWait(listener net.Listener, waitChan chan error) (net.Conn, error) {
1108 type connOrError struct {
1109 conn net.Conn
1110 err error
1111 }
1112 connChan := make(chan connOrError, 1)
1113 go func() {
1114 conn, err := listener.Accept()
1115 connChan <- connOrError{conn, err}
1116 close(connChan)
1117 }()
1118 select {
1119 case result := <-connChan:
1120 return result.conn, result.err
1121 case childErr := <-waitChan:
1122 waitChan <- childErr
1123 return nil, fmt.Errorf("child exited early: %s", childErr)
1124 }
1125}
1126
Adam Langley69a01602014-11-17 17:26:55 -08001127func runTest(test *testCase, buildDir string, mallocNumToFail int64) error {
Adam Langley38311732014-10-16 19:04:35 -07001128 if !test.shouldFail && (len(test.expectedError) > 0 || len(test.expectedLocalError) > 0) {
1129 panic("Error expected without shouldFail in " + test.name)
1130 }
1131
David Benjamin87c8a642015-02-21 01:54:29 -05001132 listener, err := net.ListenTCP("tcp4", &net.TCPAddr{IP: net.IP{127, 0, 0, 1}})
1133 if err != nil {
1134 panic(err)
1135 }
1136 defer func() {
1137 if listener != nil {
1138 listener.Close()
1139 }
1140 }()
Adam Langley95c29f32014-06-20 12:00:00 -07001141
David Benjamin884fdf12014-08-02 15:28:23 -04001142 shim_path := path.Join(buildDir, "ssl/test/bssl_shim")
David Benjamin87c8a642015-02-21 01:54:29 -05001143 flags := []string{"-port", strconv.Itoa(listener.Addr().(*net.TCPAddr).Port)}
David Benjamin1d5c83e2014-07-22 19:20:02 -04001144 if test.testType == serverTest {
David Benjamin5a593af2014-08-11 19:51:50 -04001145 flags = append(flags, "-server")
1146
David Benjamin025b3d32014-07-01 19:53:04 -04001147 flags = append(flags, "-key-file")
1148 if test.keyFile == "" {
1149 flags = append(flags, rsaKeyFile)
1150 } else {
1151 flags = append(flags, test.keyFile)
1152 }
1153
1154 flags = append(flags, "-cert-file")
1155 if test.certFile == "" {
1156 flags = append(flags, rsaCertificateFile)
1157 } else {
1158 flags = append(flags, test.certFile)
1159 }
1160 }
David Benjamin5a593af2014-08-11 19:51:50 -04001161
David Benjamin6fd297b2014-08-11 18:43:38 -04001162 if test.protocol == dtls {
1163 flags = append(flags, "-dtls")
1164 }
1165
David Benjamin5a593af2014-08-11 19:51:50 -04001166 if test.resumeSession {
1167 flags = append(flags, "-resume")
1168 }
1169
David Benjamine58c4f52014-08-24 03:47:07 -04001170 if test.shimWritesFirst {
1171 flags = append(flags, "-shim-writes-first")
1172 }
1173
David Benjamin025b3d32014-07-01 19:53:04 -04001174 flags = append(flags, test.flags...)
1175
1176 var shim *exec.Cmd
1177 if *useValgrind {
1178 shim = valgrindOf(false, shim_path, flags...)
Adam Langley75712922014-10-10 16:23:43 -07001179 } else if *useGDB {
1180 shim = gdbOf(shim_path, flags...)
David Benjamin025b3d32014-07-01 19:53:04 -04001181 } else {
1182 shim = exec.Command(shim_path, flags...)
1183 }
David Benjamin025b3d32014-07-01 19:53:04 -04001184 shim.Stdin = os.Stdin
1185 var stdoutBuf, stderrBuf bytes.Buffer
1186 shim.Stdout = &stdoutBuf
1187 shim.Stderr = &stderrBuf
Adam Langley69a01602014-11-17 17:26:55 -08001188 if mallocNumToFail >= 0 {
David Benjamin9e128b02015-02-09 13:13:09 -05001189 shim.Env = os.Environ()
1190 shim.Env = append(shim.Env, "MALLOC_NUMBER_TO_FAIL="+strconv.FormatInt(mallocNumToFail, 10))
Adam Langley69a01602014-11-17 17:26:55 -08001191 if *mallocTestDebug {
1192 shim.Env = append(shim.Env, "MALLOC_ABORT_ON_FAIL=1")
1193 }
1194 shim.Env = append(shim.Env, "_MALLOC_CHECK=1")
1195 }
David Benjamin025b3d32014-07-01 19:53:04 -04001196
1197 if err := shim.Start(); err != nil {
Adam Langley95c29f32014-06-20 12:00:00 -07001198 panic(err)
1199 }
David Benjamin87c8a642015-02-21 01:54:29 -05001200 waitChan := make(chan error, 1)
1201 go func() { waitChan <- shim.Wait() }()
Adam Langley95c29f32014-06-20 12:00:00 -07001202
1203 config := test.config
David Benjamin1d5c83e2014-07-22 19:20:02 -04001204 config.ClientSessionCache = NewLRUClientSessionCache(1)
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001205 config.ServerSessionCache = NewLRUServerSessionCache(1)
David Benjamin025b3d32014-07-01 19:53:04 -04001206 if test.testType == clientTest {
1207 if len(config.Certificates) == 0 {
1208 config.Certificates = []Certificate{getRSACertificate()}
1209 }
David Benjamin87c8a642015-02-21 01:54:29 -05001210 } else {
1211 // Supply a ServerName to ensure a constant session cache key,
1212 // rather than falling back to net.Conn.RemoteAddr.
1213 if len(config.ServerName) == 0 {
1214 config.ServerName = "test"
1215 }
David Benjamin025b3d32014-07-01 19:53:04 -04001216 }
Adam Langley95c29f32014-06-20 12:00:00 -07001217
David Benjamin87c8a642015-02-21 01:54:29 -05001218 conn, err := acceptOrWait(listener, waitChan)
1219 if err == nil {
1220 err = doExchange(test, &config, conn, test.messageLen, false /* not a resumption */)
1221 conn.Close()
1222 }
David Benjamin65ea8ff2014-11-23 03:01:00 -05001223
David Benjamin1d5c83e2014-07-22 19:20:02 -04001224 if err == nil && test.resumeSession {
David Benjamin01fe8202014-09-24 15:21:44 -04001225 var resumeConfig Config
1226 if test.resumeConfig != nil {
1227 resumeConfig = *test.resumeConfig
David Benjamin87c8a642015-02-21 01:54:29 -05001228 if len(resumeConfig.ServerName) == 0 {
1229 resumeConfig.ServerName = config.ServerName
1230 }
David Benjamin01fe8202014-09-24 15:21:44 -04001231 if len(resumeConfig.Certificates) == 0 {
1232 resumeConfig.Certificates = []Certificate{getRSACertificate()}
1233 }
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001234 if !test.newSessionsOnResume {
1235 resumeConfig.SessionTicketKey = config.SessionTicketKey
1236 resumeConfig.ClientSessionCache = config.ClientSessionCache
1237 resumeConfig.ServerSessionCache = config.ServerSessionCache
1238 }
David Benjamin01fe8202014-09-24 15:21:44 -04001239 } else {
1240 resumeConfig = config
1241 }
David Benjamin87c8a642015-02-21 01:54:29 -05001242 var connResume net.Conn
1243 connResume, err = acceptOrWait(listener, waitChan)
1244 if err == nil {
1245 err = doExchange(test, &resumeConfig, connResume, test.messageLen, true /* resumption */)
1246 connResume.Close()
1247 }
David Benjamin1d5c83e2014-07-22 19:20:02 -04001248 }
1249
David Benjamin87c8a642015-02-21 01:54:29 -05001250 // Close the listener now. This is to avoid hangs should the shim try to
1251 // open more connections than expected.
1252 listener.Close()
1253 listener = nil
1254
1255 childErr := <-waitChan
Adam Langley69a01602014-11-17 17:26:55 -08001256 if exitError, ok := childErr.(*exec.ExitError); ok {
1257 if exitError.Sys().(syscall.WaitStatus).ExitStatus() == 88 {
1258 return errMoreMallocs
1259 }
1260 }
Adam Langley95c29f32014-06-20 12:00:00 -07001261
1262 stdout := string(stdoutBuf.Bytes())
1263 stderr := string(stderrBuf.Bytes())
1264 failed := err != nil || childErr != nil
1265 correctFailure := len(test.expectedError) == 0 || strings.Contains(stdout, test.expectedError)
Adam Langleyac61fa32014-06-23 12:03:11 -07001266 localError := "none"
1267 if err != nil {
1268 localError = err.Error()
1269 }
1270 if len(test.expectedLocalError) != 0 {
1271 correctFailure = correctFailure && strings.Contains(localError, test.expectedLocalError)
1272 }
Adam Langley95c29f32014-06-20 12:00:00 -07001273
1274 if failed != test.shouldFail || failed && !correctFailure {
Adam Langley95c29f32014-06-20 12:00:00 -07001275 childError := "none"
Adam Langley95c29f32014-06-20 12:00:00 -07001276 if childErr != nil {
1277 childError = childErr.Error()
1278 }
1279
1280 var msg string
1281 switch {
1282 case failed && !test.shouldFail:
1283 msg = "unexpected failure"
1284 case !failed && test.shouldFail:
1285 msg = "unexpected success"
1286 case failed && !correctFailure:
Adam Langleyac61fa32014-06-23 12:03:11 -07001287 msg = "bad error (wanted '" + test.expectedError + "' / '" + test.expectedLocalError + "')"
Adam Langley95c29f32014-06-20 12:00:00 -07001288 default:
1289 panic("internal error")
1290 }
1291
1292 return fmt.Errorf("%s: local error '%s', child error '%s', stdout:\n%s\nstderr:\n%s", msg, localError, childError, string(stdoutBuf.Bytes()), stderr)
1293 }
1294
1295 if !*useValgrind && len(stderr) > 0 {
1296 println(stderr)
1297 }
1298
1299 return nil
1300}
1301
1302var tlsVersions = []struct {
1303 name string
1304 version uint16
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001305 flag string
David Benjamin8b8c0062014-11-23 02:47:52 -05001306 hasDTLS bool
Adam Langley95c29f32014-06-20 12:00:00 -07001307}{
David Benjamin8b8c0062014-11-23 02:47:52 -05001308 {"SSL3", VersionSSL30, "-no-ssl3", false},
1309 {"TLS1", VersionTLS10, "-no-tls1", true},
1310 {"TLS11", VersionTLS11, "-no-tls11", false},
1311 {"TLS12", VersionTLS12, "-no-tls12", true},
Adam Langley95c29f32014-06-20 12:00:00 -07001312}
1313
1314var testCipherSuites = []struct {
1315 name string
1316 id uint16
1317}{
1318 {"3DES-SHA", TLS_RSA_WITH_3DES_EDE_CBC_SHA},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001319 {"AES128-GCM", TLS_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001320 {"AES128-SHA", TLS_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001321 {"AES128-SHA256", TLS_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001322 {"AES256-GCM", TLS_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001323 {"AES256-SHA", TLS_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001324 {"AES256-SHA256", TLS_RSA_WITH_AES_256_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001325 {"DHE-RSA-AES128-GCM", TLS_DHE_RSA_WITH_AES_128_GCM_SHA256},
1326 {"DHE-RSA-AES128-SHA", TLS_DHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001327 {"DHE-RSA-AES128-SHA256", TLS_DHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001328 {"DHE-RSA-AES256-GCM", TLS_DHE_RSA_WITH_AES_256_GCM_SHA384},
1329 {"DHE-RSA-AES256-SHA", TLS_DHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001330 {"DHE-RSA-AES256-SHA256", TLS_DHE_RSA_WITH_AES_256_CBC_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001331 {"ECDHE-ECDSA-AES128-GCM", TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
1332 {"ECDHE-ECDSA-AES128-SHA", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001333 {"ECDHE-ECDSA-AES128-SHA256", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256},
1334 {"ECDHE-ECDSA-AES256-GCM", TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001335 {"ECDHE-ECDSA-AES256-SHA", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001336 {"ECDHE-ECDSA-AES256-SHA384", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001337 {"ECDHE-ECDSA-RC4-SHA", TLS_ECDHE_ECDSA_WITH_RC4_128_SHA},
David Benjamin2af684f2014-10-27 02:23:15 -04001338 {"ECDHE-PSK-WITH-AES-128-GCM-SHA256", TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001339 {"ECDHE-RSA-AES128-GCM", TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001340 {"ECDHE-RSA-AES128-SHA", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001341 {"ECDHE-RSA-AES128-SHA256", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001342 {"ECDHE-RSA-AES256-GCM", TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001343 {"ECDHE-RSA-AES256-SHA", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001344 {"ECDHE-RSA-AES256-SHA384", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001345 {"ECDHE-RSA-RC4-SHA", TLS_ECDHE_RSA_WITH_RC4_128_SHA},
David Benjamin48cae082014-10-27 01:06:24 -04001346 {"PSK-AES128-CBC-SHA", TLS_PSK_WITH_AES_128_CBC_SHA},
1347 {"PSK-AES256-CBC-SHA", TLS_PSK_WITH_AES_256_CBC_SHA},
1348 {"PSK-RC4-SHA", TLS_PSK_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -07001349 {"RC4-MD5", TLS_RSA_WITH_RC4_128_MD5},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001350 {"RC4-SHA", TLS_RSA_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -07001351}
1352
David Benjamin8b8c0062014-11-23 02:47:52 -05001353func hasComponent(suiteName, component string) bool {
1354 return strings.Contains("-"+suiteName+"-", "-"+component+"-")
1355}
1356
David Benjaminf7768e42014-08-31 02:06:47 -04001357func isTLS12Only(suiteName string) bool {
David Benjamin8b8c0062014-11-23 02:47:52 -05001358 return hasComponent(suiteName, "GCM") ||
1359 hasComponent(suiteName, "SHA256") ||
1360 hasComponent(suiteName, "SHA384")
1361}
1362
1363func isDTLSCipher(suiteName string) bool {
David Benjamine95d20d2014-12-23 11:16:01 -05001364 return !hasComponent(suiteName, "RC4")
David Benjaminf7768e42014-08-31 02:06:47 -04001365}
1366
Adam Langley95c29f32014-06-20 12:00:00 -07001367func addCipherSuiteTests() {
1368 for _, suite := range testCipherSuites {
David Benjamin48cae082014-10-27 01:06:24 -04001369 const psk = "12345"
1370 const pskIdentity = "luggage combo"
1371
Adam Langley95c29f32014-06-20 12:00:00 -07001372 var cert Certificate
David Benjamin025b3d32014-07-01 19:53:04 -04001373 var certFile string
1374 var keyFile string
David Benjamin8b8c0062014-11-23 02:47:52 -05001375 if hasComponent(suite.name, "ECDSA") {
Adam Langley95c29f32014-06-20 12:00:00 -07001376 cert = getECDSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -04001377 certFile = ecdsaCertificateFile
1378 keyFile = ecdsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -07001379 } else {
1380 cert = getRSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -04001381 certFile = rsaCertificateFile
1382 keyFile = rsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -07001383 }
1384
David Benjamin48cae082014-10-27 01:06:24 -04001385 var flags []string
David Benjamin8b8c0062014-11-23 02:47:52 -05001386 if hasComponent(suite.name, "PSK") {
David Benjamin48cae082014-10-27 01:06:24 -04001387 flags = append(flags,
1388 "-psk", psk,
1389 "-psk-identity", pskIdentity)
1390 }
1391
Adam Langley95c29f32014-06-20 12:00:00 -07001392 for _, ver := range tlsVersions {
David Benjaminf7768e42014-08-31 02:06:47 -04001393 if ver.version < VersionTLS12 && isTLS12Only(suite.name) {
Adam Langley95c29f32014-06-20 12:00:00 -07001394 continue
1395 }
1396
David Benjamin025b3d32014-07-01 19:53:04 -04001397 testCases = append(testCases, testCase{
1398 testType: clientTest,
1399 name: ver.name + "-" + suite.name + "-client",
Adam Langley95c29f32014-06-20 12:00:00 -07001400 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001401 MinVersion: ver.version,
1402 MaxVersion: ver.version,
1403 CipherSuites: []uint16{suite.id},
1404 Certificates: []Certificate{cert},
1405 PreSharedKey: []byte(psk),
1406 PreSharedKeyIdentity: pskIdentity,
Adam Langley95c29f32014-06-20 12:00:00 -07001407 },
David Benjamin48cae082014-10-27 01:06:24 -04001408 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001409 resumeSession: true,
Adam Langley95c29f32014-06-20 12:00:00 -07001410 })
David Benjamin025b3d32014-07-01 19:53:04 -04001411
David Benjamin76d8abe2014-08-14 16:25:34 -04001412 testCases = append(testCases, testCase{
1413 testType: serverTest,
1414 name: ver.name + "-" + suite.name + "-server",
1415 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001416 MinVersion: ver.version,
1417 MaxVersion: ver.version,
1418 CipherSuites: []uint16{suite.id},
1419 Certificates: []Certificate{cert},
1420 PreSharedKey: []byte(psk),
1421 PreSharedKeyIdentity: pskIdentity,
David Benjamin76d8abe2014-08-14 16:25:34 -04001422 },
1423 certFile: certFile,
1424 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001425 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001426 resumeSession: true,
David Benjamin76d8abe2014-08-14 16:25:34 -04001427 })
David Benjamin6fd297b2014-08-11 18:43:38 -04001428
David Benjamin8b8c0062014-11-23 02:47:52 -05001429 if ver.hasDTLS && isDTLSCipher(suite.name) {
David Benjamin6fd297b2014-08-11 18:43:38 -04001430 testCases = append(testCases, testCase{
1431 testType: clientTest,
1432 protocol: dtls,
1433 name: "D" + ver.name + "-" + suite.name + "-client",
1434 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001435 MinVersion: ver.version,
1436 MaxVersion: ver.version,
1437 CipherSuites: []uint16{suite.id},
1438 Certificates: []Certificate{cert},
1439 PreSharedKey: []byte(psk),
1440 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001441 },
David Benjamin48cae082014-10-27 01:06:24 -04001442 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001443 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001444 })
1445 testCases = append(testCases, testCase{
1446 testType: serverTest,
1447 protocol: dtls,
1448 name: "D" + ver.name + "-" + suite.name + "-server",
1449 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001450 MinVersion: ver.version,
1451 MaxVersion: ver.version,
1452 CipherSuites: []uint16{suite.id},
1453 Certificates: []Certificate{cert},
1454 PreSharedKey: []byte(psk),
1455 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001456 },
1457 certFile: certFile,
1458 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001459 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001460 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001461 })
1462 }
Adam Langley95c29f32014-06-20 12:00:00 -07001463 }
1464 }
1465}
1466
1467func addBadECDSASignatureTests() {
1468 for badR := BadValue(1); badR < NumBadValues; badR++ {
1469 for badS := BadValue(1); badS < NumBadValues; badS++ {
David Benjamin025b3d32014-07-01 19:53:04 -04001470 testCases = append(testCases, testCase{
Adam Langley95c29f32014-06-20 12:00:00 -07001471 name: fmt.Sprintf("BadECDSA-%d-%d", badR, badS),
1472 config: Config{
1473 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
1474 Certificates: []Certificate{getECDSACertificate()},
1475 Bugs: ProtocolBugs{
1476 BadECDSAR: badR,
1477 BadECDSAS: badS,
1478 },
1479 },
1480 shouldFail: true,
1481 expectedError: "SIGNATURE",
1482 })
1483 }
1484 }
1485}
1486
Adam Langley80842bd2014-06-20 12:00:00 -07001487func addCBCPaddingTests() {
David Benjamin025b3d32014-07-01 19:53:04 -04001488 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001489 name: "MaxCBCPadding",
1490 config: Config{
1491 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1492 Bugs: ProtocolBugs{
1493 MaxPadding: true,
1494 },
1495 },
1496 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1497 })
David Benjamin025b3d32014-07-01 19:53:04 -04001498 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001499 name: "BadCBCPadding",
1500 config: Config{
1501 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1502 Bugs: ProtocolBugs{
1503 PaddingFirstByteBad: true,
1504 },
1505 },
1506 shouldFail: true,
1507 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1508 })
1509 // OpenSSL previously had an issue where the first byte of padding in
1510 // 255 bytes of padding wasn't checked.
David Benjamin025b3d32014-07-01 19:53:04 -04001511 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001512 name: "BadCBCPadding255",
1513 config: Config{
1514 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1515 Bugs: ProtocolBugs{
1516 MaxPadding: true,
1517 PaddingFirstByteBadIf255: true,
1518 },
1519 },
1520 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1521 shouldFail: true,
1522 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1523 })
1524}
1525
Kenny Root7fdeaf12014-08-05 15:23:37 -07001526func addCBCSplittingTests() {
1527 testCases = append(testCases, testCase{
1528 name: "CBCRecordSplitting",
1529 config: Config{
1530 MaxVersion: VersionTLS10,
1531 MinVersion: VersionTLS10,
1532 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1533 },
1534 messageLen: -1, // read until EOF
1535 flags: []string{
1536 "-async",
1537 "-write-different-record-sizes",
1538 "-cbc-record-splitting",
1539 },
David Benjamina8e3e0e2014-08-06 22:11:10 -04001540 })
1541 testCases = append(testCases, testCase{
Kenny Root7fdeaf12014-08-05 15:23:37 -07001542 name: "CBCRecordSplittingPartialWrite",
1543 config: Config{
1544 MaxVersion: VersionTLS10,
1545 MinVersion: VersionTLS10,
1546 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1547 },
1548 messageLen: -1, // read until EOF
1549 flags: []string{
1550 "-async",
1551 "-write-different-record-sizes",
1552 "-cbc-record-splitting",
1553 "-partial-write",
1554 },
1555 })
1556}
1557
David Benjamin636293b2014-07-08 17:59:18 -04001558func addClientAuthTests() {
David Benjamin407a10c2014-07-16 12:58:59 -04001559 // Add a dummy cert pool to stress certificate authority parsing.
1560 // TODO(davidben): Add tests that those values parse out correctly.
1561 certPool := x509.NewCertPool()
1562 cert, err := x509.ParseCertificate(rsaCertificate.Certificate[0])
1563 if err != nil {
1564 panic(err)
1565 }
1566 certPool.AddCert(cert)
1567
David Benjamin636293b2014-07-08 17:59:18 -04001568 for _, ver := range tlsVersions {
David Benjamin636293b2014-07-08 17:59:18 -04001569 testCases = append(testCases, testCase{
1570 testType: clientTest,
David Benjamin67666e72014-07-12 15:47:52 -04001571 name: ver.name + "-Client-ClientAuth-RSA",
David Benjamin636293b2014-07-08 17:59:18 -04001572 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001573 MinVersion: ver.version,
1574 MaxVersion: ver.version,
1575 ClientAuth: RequireAnyClientCert,
1576 ClientCAs: certPool,
David Benjamin636293b2014-07-08 17:59:18 -04001577 },
1578 flags: []string{
1579 "-cert-file", rsaCertificateFile,
1580 "-key-file", rsaKeyFile,
1581 },
1582 })
1583 testCases = append(testCases, testCase{
David Benjamin67666e72014-07-12 15:47:52 -04001584 testType: serverTest,
1585 name: ver.name + "-Server-ClientAuth-RSA",
1586 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001587 MinVersion: ver.version,
1588 MaxVersion: ver.version,
David Benjamin67666e72014-07-12 15:47:52 -04001589 Certificates: []Certificate{rsaCertificate},
1590 },
1591 flags: []string{"-require-any-client-certificate"},
1592 })
David Benjamine098ec22014-08-27 23:13:20 -04001593 if ver.version != VersionSSL30 {
1594 testCases = append(testCases, testCase{
1595 testType: serverTest,
1596 name: ver.name + "-Server-ClientAuth-ECDSA",
1597 config: Config{
1598 MinVersion: ver.version,
1599 MaxVersion: ver.version,
1600 Certificates: []Certificate{ecdsaCertificate},
1601 },
1602 flags: []string{"-require-any-client-certificate"},
1603 })
1604 testCases = append(testCases, testCase{
1605 testType: clientTest,
1606 name: ver.name + "-Client-ClientAuth-ECDSA",
1607 config: Config{
1608 MinVersion: ver.version,
1609 MaxVersion: ver.version,
1610 ClientAuth: RequireAnyClientCert,
1611 ClientCAs: certPool,
1612 },
1613 flags: []string{
1614 "-cert-file", ecdsaCertificateFile,
1615 "-key-file", ecdsaKeyFile,
1616 },
1617 })
1618 }
David Benjamin636293b2014-07-08 17:59:18 -04001619 }
1620}
1621
Adam Langley75712922014-10-10 16:23:43 -07001622func addExtendedMasterSecretTests() {
1623 const expectEMSFlag = "-expect-extended-master-secret"
1624
1625 for _, with := range []bool{false, true} {
1626 prefix := "No"
1627 var flags []string
1628 if with {
1629 prefix = ""
1630 flags = []string{expectEMSFlag}
1631 }
1632
1633 for _, isClient := range []bool{false, true} {
1634 suffix := "-Server"
1635 testType := serverTest
1636 if isClient {
1637 suffix = "-Client"
1638 testType = clientTest
1639 }
1640
1641 for _, ver := range tlsVersions {
1642 test := testCase{
1643 testType: testType,
1644 name: prefix + "ExtendedMasterSecret-" + ver.name + suffix,
1645 config: Config{
1646 MinVersion: ver.version,
1647 MaxVersion: ver.version,
1648 Bugs: ProtocolBugs{
1649 NoExtendedMasterSecret: !with,
1650 RequireExtendedMasterSecret: with,
1651 },
1652 },
David Benjamin48cae082014-10-27 01:06:24 -04001653 flags: flags,
1654 shouldFail: ver.version == VersionSSL30 && with,
Adam Langley75712922014-10-10 16:23:43 -07001655 }
1656 if test.shouldFail {
1657 test.expectedLocalError = "extended master secret required but not supported by peer"
1658 }
1659 testCases = append(testCases, test)
1660 }
1661 }
1662 }
1663
1664 // When a session is resumed, it should still be aware that its master
1665 // secret was generated via EMS and thus it's safe to use tls-unique.
1666 testCases = append(testCases, testCase{
1667 name: "ExtendedMasterSecret-Resume",
1668 config: Config{
1669 Bugs: ProtocolBugs{
1670 RequireExtendedMasterSecret: true,
1671 },
1672 },
1673 flags: []string{expectEMSFlag},
1674 resumeSession: true,
1675 })
1676}
1677
David Benjamin43ec06f2014-08-05 02:28:57 -04001678// Adds tests that try to cover the range of the handshake state machine, under
1679// various conditions. Some of these are redundant with other tests, but they
1680// only cover the synchronous case.
David Benjamin6fd297b2014-08-11 18:43:38 -04001681func addStateMachineCoverageTests(async, splitHandshake bool, protocol protocol) {
David Benjamin43ec06f2014-08-05 02:28:57 -04001682 var suffix string
1683 var flags []string
1684 var maxHandshakeRecordLength int
David Benjamin6fd297b2014-08-11 18:43:38 -04001685 if protocol == dtls {
1686 suffix = "-DTLS"
1687 }
David Benjamin43ec06f2014-08-05 02:28:57 -04001688 if async {
David Benjamin6fd297b2014-08-11 18:43:38 -04001689 suffix += "-Async"
David Benjamin43ec06f2014-08-05 02:28:57 -04001690 flags = append(flags, "-async")
1691 } else {
David Benjamin6fd297b2014-08-11 18:43:38 -04001692 suffix += "-Sync"
David Benjamin43ec06f2014-08-05 02:28:57 -04001693 }
1694 if splitHandshake {
1695 suffix += "-SplitHandshakeRecords"
David Benjamin98214542014-08-07 18:02:39 -04001696 maxHandshakeRecordLength = 1
David Benjamin43ec06f2014-08-05 02:28:57 -04001697 }
1698
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001699 // Basic handshake, with resumption. Client and server,
1700 // session ID and session ticket.
David Benjamin43ec06f2014-08-05 02:28:57 -04001701 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001702 protocol: protocol,
1703 name: "Basic-Client" + suffix,
David Benjamin43ec06f2014-08-05 02:28:57 -04001704 config: Config{
1705 Bugs: ProtocolBugs{
1706 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1707 },
1708 },
David Benjaminbed9aae2014-08-07 19:13:38 -04001709 flags: flags,
1710 resumeSession: true,
1711 })
1712 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001713 protocol: protocol,
1714 name: "Basic-Client-RenewTicket" + suffix,
David Benjaminbed9aae2014-08-07 19:13:38 -04001715 config: Config{
1716 Bugs: ProtocolBugs{
1717 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1718 RenewTicketOnResume: true,
1719 },
1720 },
1721 flags: flags,
1722 resumeSession: true,
David Benjamin43ec06f2014-08-05 02:28:57 -04001723 })
1724 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001725 protocol: protocol,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001726 name: "Basic-Client-NoTicket" + suffix,
1727 config: Config{
1728 SessionTicketsDisabled: true,
1729 Bugs: ProtocolBugs{
1730 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1731 },
1732 },
1733 flags: flags,
1734 resumeSession: true,
1735 })
1736 testCases = append(testCases, testCase{
1737 protocol: protocol,
David Benjamine0e7d0d2015-02-08 19:33:25 -05001738 name: "Basic-Client-Implicit" + suffix,
1739 config: Config{
1740 Bugs: ProtocolBugs{
1741 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1742 },
1743 },
1744 flags: append(flags, "-implicit-handshake"),
1745 resumeSession: true,
1746 })
1747 testCases = append(testCases, testCase{
1748 protocol: protocol,
David Benjamin43ec06f2014-08-05 02:28:57 -04001749 testType: serverTest,
1750 name: "Basic-Server" + suffix,
1751 config: Config{
1752 Bugs: ProtocolBugs{
1753 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1754 },
1755 },
David Benjaminbed9aae2014-08-07 19:13:38 -04001756 flags: flags,
1757 resumeSession: true,
David Benjamin43ec06f2014-08-05 02:28:57 -04001758 })
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001759 testCases = append(testCases, testCase{
1760 protocol: protocol,
1761 testType: serverTest,
1762 name: "Basic-Server-NoTickets" + suffix,
1763 config: Config{
1764 SessionTicketsDisabled: true,
1765 Bugs: ProtocolBugs{
1766 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1767 },
1768 },
1769 flags: flags,
1770 resumeSession: true,
1771 })
David Benjamine0e7d0d2015-02-08 19:33:25 -05001772 testCases = append(testCases, testCase{
1773 protocol: protocol,
1774 testType: serverTest,
1775 name: "Basic-Server-Implicit" + suffix,
1776 config: Config{
1777 Bugs: ProtocolBugs{
1778 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1779 },
1780 },
1781 flags: append(flags, "-implicit-handshake"),
1782 resumeSession: true,
1783 })
David Benjamin6f5c0f42015-02-24 01:23:21 -05001784 testCases = append(testCases, testCase{
1785 protocol: protocol,
1786 testType: serverTest,
1787 name: "Basic-Server-EarlyCallback" + suffix,
1788 config: Config{
1789 Bugs: ProtocolBugs{
1790 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1791 },
1792 },
1793 flags: append(flags, "-use-early-callback"),
1794 resumeSession: true,
1795 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001796
David Benjamin6fd297b2014-08-11 18:43:38 -04001797 // TLS client auth.
1798 testCases = append(testCases, testCase{
1799 protocol: protocol,
1800 testType: clientTest,
1801 name: "ClientAuth-Client" + suffix,
1802 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001803 ClientAuth: RequireAnyClientCert,
David Benjamin6fd297b2014-08-11 18:43:38 -04001804 Bugs: ProtocolBugs{
1805 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1806 },
1807 },
1808 flags: append(flags,
1809 "-cert-file", rsaCertificateFile,
1810 "-key-file", rsaKeyFile),
1811 })
1812 testCases = append(testCases, testCase{
1813 protocol: protocol,
1814 testType: serverTest,
1815 name: "ClientAuth-Server" + suffix,
1816 config: Config{
1817 Certificates: []Certificate{rsaCertificate},
1818 },
1819 flags: append(flags, "-require-any-client-certificate"),
1820 })
1821
David Benjamin43ec06f2014-08-05 02:28:57 -04001822 // No session ticket support; server doesn't send NewSessionTicket.
1823 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001824 protocol: protocol,
1825 name: "SessionTicketsDisabled-Client" + suffix,
David Benjamin43ec06f2014-08-05 02:28:57 -04001826 config: Config{
1827 SessionTicketsDisabled: true,
1828 Bugs: ProtocolBugs{
1829 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1830 },
1831 },
1832 flags: flags,
1833 })
1834 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001835 protocol: protocol,
David Benjamin43ec06f2014-08-05 02:28:57 -04001836 testType: serverTest,
1837 name: "SessionTicketsDisabled-Server" + suffix,
1838 config: Config{
1839 SessionTicketsDisabled: true,
1840 Bugs: ProtocolBugs{
1841 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1842 },
1843 },
1844 flags: flags,
1845 })
1846
David Benjamin48cae082014-10-27 01:06:24 -04001847 // Skip ServerKeyExchange in PSK key exchange if there's no
1848 // identity hint.
1849 testCases = append(testCases, testCase{
1850 protocol: protocol,
1851 name: "EmptyPSKHint-Client" + suffix,
1852 config: Config{
1853 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
1854 PreSharedKey: []byte("secret"),
1855 Bugs: ProtocolBugs{
1856 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1857 },
1858 },
1859 flags: append(flags, "-psk", "secret"),
1860 })
1861 testCases = append(testCases, testCase{
1862 protocol: protocol,
1863 testType: serverTest,
1864 name: "EmptyPSKHint-Server" + suffix,
1865 config: Config{
1866 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
1867 PreSharedKey: []byte("secret"),
1868 Bugs: ProtocolBugs{
1869 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1870 },
1871 },
1872 flags: append(flags, "-psk", "secret"),
1873 })
1874
David Benjamin6fd297b2014-08-11 18:43:38 -04001875 if protocol == tls {
1876 // NPN on client and server; results in post-handshake message.
1877 testCases = append(testCases, testCase{
1878 protocol: protocol,
1879 name: "NPN-Client" + suffix,
1880 config: Config{
David Benjaminae2888f2014-09-06 12:58:58 -04001881 NextProtos: []string{"foo"},
David Benjamin6fd297b2014-08-11 18:43:38 -04001882 Bugs: ProtocolBugs{
1883 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1884 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001885 },
David Benjaminfc7b0862014-09-06 13:21:53 -04001886 flags: append(flags, "-select-next-proto", "foo"),
1887 expectedNextProto: "foo",
1888 expectedNextProtoType: npn,
David Benjamin6fd297b2014-08-11 18:43:38 -04001889 })
1890 testCases = append(testCases, testCase{
1891 protocol: protocol,
1892 testType: serverTest,
1893 name: "NPN-Server" + suffix,
1894 config: Config{
1895 NextProtos: []string{"bar"},
1896 Bugs: ProtocolBugs{
1897 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1898 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001899 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001900 flags: append(flags,
1901 "-advertise-npn", "\x03foo\x03bar\x03baz",
1902 "-expect-next-proto", "bar"),
David Benjaminfc7b0862014-09-06 13:21:53 -04001903 expectedNextProto: "bar",
1904 expectedNextProtoType: npn,
David Benjamin6fd297b2014-08-11 18:43:38 -04001905 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001906
David Benjamin195dc782015-02-19 13:27:05 -05001907 // TODO(davidben): Add tests for when False Start doesn't trigger.
1908
David Benjamin6fd297b2014-08-11 18:43:38 -04001909 // Client does False Start and negotiates NPN.
1910 testCases = append(testCases, testCase{
1911 protocol: protocol,
1912 name: "FalseStart" + suffix,
1913 config: Config{
1914 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1915 NextProtos: []string{"foo"},
1916 Bugs: ProtocolBugs{
David Benjamine58c4f52014-08-24 03:47:07 -04001917 ExpectFalseStart: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001918 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1919 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001920 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001921 flags: append(flags,
1922 "-false-start",
1923 "-select-next-proto", "foo"),
David Benjamine58c4f52014-08-24 03:47:07 -04001924 shimWritesFirst: true,
1925 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001926 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001927
David Benjaminae2888f2014-09-06 12:58:58 -04001928 // Client does False Start and negotiates ALPN.
1929 testCases = append(testCases, testCase{
1930 protocol: protocol,
1931 name: "FalseStart-ALPN" + suffix,
1932 config: Config{
1933 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1934 NextProtos: []string{"foo"},
1935 Bugs: ProtocolBugs{
1936 ExpectFalseStart: true,
1937 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1938 },
1939 },
1940 flags: append(flags,
1941 "-false-start",
1942 "-advertise-alpn", "\x03foo"),
1943 shimWritesFirst: true,
1944 resumeSession: true,
1945 })
1946
David Benjamin931ab342015-02-08 19:46:57 -05001947 // Client does False Start but doesn't explicitly call
1948 // SSL_connect.
1949 testCases = append(testCases, testCase{
1950 protocol: protocol,
1951 name: "FalseStart-Implicit" + suffix,
1952 config: Config{
1953 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1954 NextProtos: []string{"foo"},
1955 Bugs: ProtocolBugs{
1956 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1957 },
1958 },
1959 flags: append(flags,
1960 "-implicit-handshake",
1961 "-false-start",
1962 "-advertise-alpn", "\x03foo"),
1963 })
1964
David Benjamin6fd297b2014-08-11 18:43:38 -04001965 // False Start without session tickets.
1966 testCases = append(testCases, testCase{
David Benjamin5f237bc2015-02-11 17:14:15 -05001967 name: "FalseStart-SessionTicketsDisabled" + suffix,
David Benjamin6fd297b2014-08-11 18:43:38 -04001968 config: Config{
1969 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1970 NextProtos: []string{"foo"},
1971 SessionTicketsDisabled: true,
David Benjamin4e99c522014-08-24 01:45:30 -04001972 Bugs: ProtocolBugs{
David Benjamine58c4f52014-08-24 03:47:07 -04001973 ExpectFalseStart: true,
David Benjamin4e99c522014-08-24 01:45:30 -04001974 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1975 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001976 },
David Benjamin4e99c522014-08-24 01:45:30 -04001977 flags: append(flags,
David Benjamin6fd297b2014-08-11 18:43:38 -04001978 "-false-start",
1979 "-select-next-proto", "foo",
David Benjamin4e99c522014-08-24 01:45:30 -04001980 ),
David Benjamine58c4f52014-08-24 03:47:07 -04001981 shimWritesFirst: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001982 })
David Benjamin1e7f8d72014-08-08 12:27:04 -04001983
David Benjamina08e49d2014-08-24 01:46:07 -04001984 // Server parses a V2ClientHello.
David Benjamin6fd297b2014-08-11 18:43:38 -04001985 testCases = append(testCases, testCase{
1986 protocol: protocol,
1987 testType: serverTest,
1988 name: "SendV2ClientHello" + suffix,
1989 config: Config{
1990 // Choose a cipher suite that does not involve
1991 // elliptic curves, so no extensions are
1992 // involved.
1993 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
1994 Bugs: ProtocolBugs{
1995 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1996 SendV2ClientHello: true,
1997 },
David Benjamin1e7f8d72014-08-08 12:27:04 -04001998 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001999 flags: flags,
2000 })
David Benjamina08e49d2014-08-24 01:46:07 -04002001
2002 // Client sends a Channel ID.
2003 testCases = append(testCases, testCase{
2004 protocol: protocol,
2005 name: "ChannelID-Client" + suffix,
2006 config: Config{
2007 RequestChannelID: true,
2008 Bugs: ProtocolBugs{
2009 MaxHandshakeRecordLength: maxHandshakeRecordLength,
2010 },
2011 },
2012 flags: append(flags,
2013 "-send-channel-id", channelIDKeyFile,
2014 ),
2015 resumeSession: true,
2016 expectChannelID: true,
2017 })
2018
2019 // Server accepts a Channel ID.
2020 testCases = append(testCases, testCase{
2021 protocol: protocol,
2022 testType: serverTest,
2023 name: "ChannelID-Server" + suffix,
2024 config: Config{
2025 ChannelID: channelIDKey,
2026 Bugs: ProtocolBugs{
2027 MaxHandshakeRecordLength: maxHandshakeRecordLength,
2028 },
2029 },
2030 flags: append(flags,
2031 "-expect-channel-id",
2032 base64.StdEncoding.EncodeToString(channelIDBytes),
2033 ),
2034 resumeSession: true,
2035 expectChannelID: true,
2036 })
David Benjamin6fd297b2014-08-11 18:43:38 -04002037 } else {
2038 testCases = append(testCases, testCase{
2039 protocol: protocol,
2040 name: "SkipHelloVerifyRequest" + suffix,
2041 config: Config{
2042 Bugs: ProtocolBugs{
2043 MaxHandshakeRecordLength: maxHandshakeRecordLength,
2044 SkipHelloVerifyRequest: true,
2045 },
2046 },
2047 flags: flags,
2048 })
David Benjamin6fd297b2014-08-11 18:43:38 -04002049 }
David Benjamin43ec06f2014-08-05 02:28:57 -04002050}
2051
Adam Langley524e7172015-02-20 16:04:00 -08002052func addDDoSCallbackTests() {
2053 // DDoS callback.
2054
2055 for _, resume := range []bool{false, true} {
2056 suffix := "Resume"
2057 if resume {
2058 suffix = "No" + suffix
2059 }
2060
2061 testCases = append(testCases, testCase{
2062 testType: serverTest,
2063 name: "Server-DDoS-OK-" + suffix,
2064 flags: []string{"-install-ddos-callback"},
2065 resumeSession: resume,
2066 })
2067
2068 failFlag := "-fail-ddos-callback"
2069 if resume {
2070 failFlag = "-fail-second-ddos-callback"
2071 }
2072 testCases = append(testCases, testCase{
2073 testType: serverTest,
2074 name: "Server-DDoS-Reject-" + suffix,
2075 flags: []string{"-install-ddos-callback", failFlag},
2076 resumeSession: resume,
2077 shouldFail: true,
2078 expectedError: ":CONNECTION_REJECTED:",
2079 })
2080 }
2081}
2082
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002083func addVersionNegotiationTests() {
2084 for i, shimVers := range tlsVersions {
2085 // Assemble flags to disable all newer versions on the shim.
2086 var flags []string
2087 for _, vers := range tlsVersions[i+1:] {
2088 flags = append(flags, vers.flag)
2089 }
2090
2091 for _, runnerVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05002092 protocols := []protocol{tls}
2093 if runnerVers.hasDTLS && shimVers.hasDTLS {
2094 protocols = append(protocols, dtls)
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002095 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002096 for _, protocol := range protocols {
2097 expectedVersion := shimVers.version
2098 if runnerVers.version < shimVers.version {
2099 expectedVersion = runnerVers.version
2100 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002101
David Benjamin8b8c0062014-11-23 02:47:52 -05002102 suffix := shimVers.name + "-" + runnerVers.name
2103 if protocol == dtls {
2104 suffix += "-DTLS"
2105 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002106
David Benjamin1eb367c2014-12-12 18:17:51 -05002107 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
2108
David Benjamin1e29a6b2014-12-10 02:27:24 -05002109 clientVers := shimVers.version
2110 if clientVers > VersionTLS10 {
2111 clientVers = VersionTLS10
2112 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002113 testCases = append(testCases, testCase{
2114 protocol: protocol,
2115 testType: clientTest,
2116 name: "VersionNegotiation-Client-" + suffix,
2117 config: Config{
2118 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05002119 Bugs: ProtocolBugs{
2120 ExpectInitialRecordVersion: clientVers,
2121 },
David Benjamin8b8c0062014-11-23 02:47:52 -05002122 },
2123 flags: flags,
2124 expectedVersion: expectedVersion,
2125 })
David Benjamin1eb367c2014-12-12 18:17:51 -05002126 testCases = append(testCases, testCase{
2127 protocol: protocol,
2128 testType: clientTest,
2129 name: "VersionNegotiation-Client2-" + suffix,
2130 config: Config{
2131 MaxVersion: runnerVers.version,
2132 Bugs: ProtocolBugs{
2133 ExpectInitialRecordVersion: clientVers,
2134 },
2135 },
2136 flags: []string{"-max-version", shimVersFlag},
2137 expectedVersion: expectedVersion,
2138 })
David Benjamin8b8c0062014-11-23 02:47:52 -05002139
2140 testCases = append(testCases, testCase{
2141 protocol: protocol,
2142 testType: serverTest,
2143 name: "VersionNegotiation-Server-" + suffix,
2144 config: Config{
2145 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05002146 Bugs: ProtocolBugs{
2147 ExpectInitialRecordVersion: expectedVersion,
2148 },
David Benjamin8b8c0062014-11-23 02:47:52 -05002149 },
2150 flags: flags,
2151 expectedVersion: expectedVersion,
2152 })
David Benjamin1eb367c2014-12-12 18:17:51 -05002153 testCases = append(testCases, testCase{
2154 protocol: protocol,
2155 testType: serverTest,
2156 name: "VersionNegotiation-Server2-" + suffix,
2157 config: Config{
2158 MaxVersion: runnerVers.version,
2159 Bugs: ProtocolBugs{
2160 ExpectInitialRecordVersion: expectedVersion,
2161 },
2162 },
2163 flags: []string{"-max-version", shimVersFlag},
2164 expectedVersion: expectedVersion,
2165 })
David Benjamin8b8c0062014-11-23 02:47:52 -05002166 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002167 }
2168 }
2169}
2170
David Benjaminaccb4542014-12-12 23:44:33 -05002171func addMinimumVersionTests() {
2172 for i, shimVers := range tlsVersions {
2173 // Assemble flags to disable all older versions on the shim.
2174 var flags []string
2175 for _, vers := range tlsVersions[:i] {
2176 flags = append(flags, vers.flag)
2177 }
2178
2179 for _, runnerVers := range tlsVersions {
2180 protocols := []protocol{tls}
2181 if runnerVers.hasDTLS && shimVers.hasDTLS {
2182 protocols = append(protocols, dtls)
2183 }
2184 for _, protocol := range protocols {
2185 suffix := shimVers.name + "-" + runnerVers.name
2186 if protocol == dtls {
2187 suffix += "-DTLS"
2188 }
2189 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
2190
David Benjaminaccb4542014-12-12 23:44:33 -05002191 var expectedVersion uint16
2192 var shouldFail bool
2193 var expectedError string
David Benjamin87909c02014-12-13 01:55:01 -05002194 var expectedLocalError string
David Benjaminaccb4542014-12-12 23:44:33 -05002195 if runnerVers.version >= shimVers.version {
2196 expectedVersion = runnerVers.version
2197 } else {
2198 shouldFail = true
2199 expectedError = ":UNSUPPORTED_PROTOCOL:"
David Benjamin87909c02014-12-13 01:55:01 -05002200 if runnerVers.version > VersionSSL30 {
2201 expectedLocalError = "remote error: protocol version not supported"
2202 } else {
2203 expectedLocalError = "remote error: handshake failure"
2204 }
David Benjaminaccb4542014-12-12 23:44:33 -05002205 }
2206
2207 testCases = append(testCases, testCase{
2208 protocol: protocol,
2209 testType: clientTest,
2210 name: "MinimumVersion-Client-" + suffix,
2211 config: Config{
2212 MaxVersion: runnerVers.version,
2213 },
David Benjamin87909c02014-12-13 01:55:01 -05002214 flags: flags,
2215 expectedVersion: expectedVersion,
2216 shouldFail: shouldFail,
2217 expectedError: expectedError,
2218 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002219 })
2220 testCases = append(testCases, testCase{
2221 protocol: protocol,
2222 testType: clientTest,
2223 name: "MinimumVersion-Client2-" + suffix,
2224 config: Config{
2225 MaxVersion: runnerVers.version,
2226 },
David Benjamin87909c02014-12-13 01:55:01 -05002227 flags: []string{"-min-version", shimVersFlag},
2228 expectedVersion: expectedVersion,
2229 shouldFail: shouldFail,
2230 expectedError: expectedError,
2231 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002232 })
2233
2234 testCases = append(testCases, testCase{
2235 protocol: protocol,
2236 testType: serverTest,
2237 name: "MinimumVersion-Server-" + suffix,
2238 config: Config{
2239 MaxVersion: runnerVers.version,
2240 },
David Benjamin87909c02014-12-13 01:55:01 -05002241 flags: flags,
2242 expectedVersion: expectedVersion,
2243 shouldFail: shouldFail,
2244 expectedError: expectedError,
2245 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002246 })
2247 testCases = append(testCases, testCase{
2248 protocol: protocol,
2249 testType: serverTest,
2250 name: "MinimumVersion-Server2-" + suffix,
2251 config: Config{
2252 MaxVersion: runnerVers.version,
2253 },
David Benjamin87909c02014-12-13 01:55:01 -05002254 flags: []string{"-min-version", shimVersFlag},
2255 expectedVersion: expectedVersion,
2256 shouldFail: shouldFail,
2257 expectedError: expectedError,
2258 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002259 })
2260 }
2261 }
2262 }
2263}
2264
David Benjamin5c24a1d2014-08-31 00:59:27 -04002265func addD5BugTests() {
2266 testCases = append(testCases, testCase{
2267 testType: serverTest,
2268 name: "D5Bug-NoQuirk-Reject",
2269 config: Config{
2270 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2271 Bugs: ProtocolBugs{
2272 SSL3RSAKeyExchange: true,
2273 },
2274 },
2275 shouldFail: true,
2276 expectedError: ":TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG:",
2277 })
2278 testCases = append(testCases, testCase{
2279 testType: serverTest,
2280 name: "D5Bug-Quirk-Normal",
2281 config: Config{
2282 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2283 },
2284 flags: []string{"-tls-d5-bug"},
2285 })
2286 testCases = append(testCases, testCase{
2287 testType: serverTest,
2288 name: "D5Bug-Quirk-Bug",
2289 config: Config{
2290 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2291 Bugs: ProtocolBugs{
2292 SSL3RSAKeyExchange: true,
2293 },
2294 },
2295 flags: []string{"-tls-d5-bug"},
2296 })
2297}
2298
David Benjamine78bfde2014-09-06 12:45:15 -04002299func addExtensionTests() {
2300 testCases = append(testCases, testCase{
2301 testType: clientTest,
2302 name: "DuplicateExtensionClient",
2303 config: Config{
2304 Bugs: ProtocolBugs{
2305 DuplicateExtension: true,
2306 },
2307 },
2308 shouldFail: true,
2309 expectedLocalError: "remote error: error decoding message",
2310 })
2311 testCases = append(testCases, testCase{
2312 testType: serverTest,
2313 name: "DuplicateExtensionServer",
2314 config: Config{
2315 Bugs: ProtocolBugs{
2316 DuplicateExtension: true,
2317 },
2318 },
2319 shouldFail: true,
2320 expectedLocalError: "remote error: error decoding message",
2321 })
2322 testCases = append(testCases, testCase{
2323 testType: clientTest,
2324 name: "ServerNameExtensionClient",
2325 config: Config{
2326 Bugs: ProtocolBugs{
2327 ExpectServerName: "example.com",
2328 },
2329 },
2330 flags: []string{"-host-name", "example.com"},
2331 })
2332 testCases = append(testCases, testCase{
2333 testType: clientTest,
David Benjamin5f237bc2015-02-11 17:14:15 -05002334 name: "ServerNameExtensionClientMismatch",
David Benjamine78bfde2014-09-06 12:45:15 -04002335 config: Config{
2336 Bugs: ProtocolBugs{
2337 ExpectServerName: "mismatch.com",
2338 },
2339 },
2340 flags: []string{"-host-name", "example.com"},
2341 shouldFail: true,
2342 expectedLocalError: "tls: unexpected server name",
2343 })
2344 testCases = append(testCases, testCase{
2345 testType: clientTest,
David Benjamin5f237bc2015-02-11 17:14:15 -05002346 name: "ServerNameExtensionClientMissing",
David Benjamine78bfde2014-09-06 12:45:15 -04002347 config: Config{
2348 Bugs: ProtocolBugs{
2349 ExpectServerName: "missing.com",
2350 },
2351 },
2352 shouldFail: true,
2353 expectedLocalError: "tls: unexpected server name",
2354 })
2355 testCases = append(testCases, testCase{
2356 testType: serverTest,
2357 name: "ServerNameExtensionServer",
2358 config: Config{
2359 ServerName: "example.com",
2360 },
2361 flags: []string{"-expect-server-name", "example.com"},
2362 resumeSession: true,
2363 })
David Benjaminae2888f2014-09-06 12:58:58 -04002364 testCases = append(testCases, testCase{
2365 testType: clientTest,
2366 name: "ALPNClient",
2367 config: Config{
2368 NextProtos: []string{"foo"},
2369 },
2370 flags: []string{
2371 "-advertise-alpn", "\x03foo\x03bar\x03baz",
2372 "-expect-alpn", "foo",
2373 },
David Benjaminfc7b0862014-09-06 13:21:53 -04002374 expectedNextProto: "foo",
2375 expectedNextProtoType: alpn,
2376 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04002377 })
2378 testCases = append(testCases, testCase{
2379 testType: serverTest,
2380 name: "ALPNServer",
2381 config: Config{
2382 NextProtos: []string{"foo", "bar", "baz"},
2383 },
2384 flags: []string{
2385 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2386 "-select-alpn", "foo",
2387 },
David Benjaminfc7b0862014-09-06 13:21:53 -04002388 expectedNextProto: "foo",
2389 expectedNextProtoType: alpn,
2390 resumeSession: true,
2391 })
2392 // Test that the server prefers ALPN over NPN.
2393 testCases = append(testCases, testCase{
2394 testType: serverTest,
2395 name: "ALPNServer-Preferred",
2396 config: Config{
2397 NextProtos: []string{"foo", "bar", "baz"},
2398 },
2399 flags: []string{
2400 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2401 "-select-alpn", "foo",
2402 "-advertise-npn", "\x03foo\x03bar\x03baz",
2403 },
2404 expectedNextProto: "foo",
2405 expectedNextProtoType: alpn,
2406 resumeSession: true,
2407 })
2408 testCases = append(testCases, testCase{
2409 testType: serverTest,
2410 name: "ALPNServer-Preferred-Swapped",
2411 config: Config{
2412 NextProtos: []string{"foo", "bar", "baz"},
2413 Bugs: ProtocolBugs{
2414 SwapNPNAndALPN: true,
2415 },
2416 },
2417 flags: []string{
2418 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2419 "-select-alpn", "foo",
2420 "-advertise-npn", "\x03foo\x03bar\x03baz",
2421 },
2422 expectedNextProto: "foo",
2423 expectedNextProtoType: alpn,
2424 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04002425 })
Adam Langley38311732014-10-16 19:04:35 -07002426 // Resume with a corrupt ticket.
2427 testCases = append(testCases, testCase{
2428 testType: serverTest,
2429 name: "CorruptTicket",
2430 config: Config{
2431 Bugs: ProtocolBugs{
2432 CorruptTicket: true,
2433 },
2434 },
2435 resumeSession: true,
2436 flags: []string{"-expect-session-miss"},
2437 })
2438 // Resume with an oversized session id.
2439 testCases = append(testCases, testCase{
2440 testType: serverTest,
2441 name: "OversizedSessionId",
2442 config: Config{
2443 Bugs: ProtocolBugs{
2444 OversizedSessionId: true,
2445 },
2446 },
2447 resumeSession: true,
Adam Langley75712922014-10-10 16:23:43 -07002448 shouldFail: true,
Adam Langley38311732014-10-16 19:04:35 -07002449 expectedError: ":DECODE_ERROR:",
2450 })
David Benjaminca6c8262014-11-15 19:06:08 -05002451 // Basic DTLS-SRTP tests. Include fake profiles to ensure they
2452 // are ignored.
2453 testCases = append(testCases, testCase{
2454 protocol: dtls,
2455 name: "SRTP-Client",
2456 config: Config{
2457 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
2458 },
2459 flags: []string{
2460 "-srtp-profiles",
2461 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2462 },
2463 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2464 })
2465 testCases = append(testCases, testCase{
2466 protocol: dtls,
2467 testType: serverTest,
2468 name: "SRTP-Server",
2469 config: Config{
2470 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
2471 },
2472 flags: []string{
2473 "-srtp-profiles",
2474 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2475 },
2476 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2477 })
2478 // Test that the MKI is ignored.
2479 testCases = append(testCases, testCase{
2480 protocol: dtls,
2481 testType: serverTest,
2482 name: "SRTP-Server-IgnoreMKI",
2483 config: Config{
2484 SRTPProtectionProfiles: []uint16{SRTP_AES128_CM_HMAC_SHA1_80},
2485 Bugs: ProtocolBugs{
2486 SRTPMasterKeyIdentifer: "bogus",
2487 },
2488 },
2489 flags: []string{
2490 "-srtp-profiles",
2491 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2492 },
2493 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2494 })
2495 // Test that SRTP isn't negotiated on the server if there were
2496 // no matching profiles.
2497 testCases = append(testCases, testCase{
2498 protocol: dtls,
2499 testType: serverTest,
2500 name: "SRTP-Server-NoMatch",
2501 config: Config{
2502 SRTPProtectionProfiles: []uint16{100, 101, 102},
2503 },
2504 flags: []string{
2505 "-srtp-profiles",
2506 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2507 },
2508 expectedSRTPProtectionProfile: 0,
2509 })
2510 // Test that the server returning an invalid SRTP profile is
2511 // flagged as an error by the client.
2512 testCases = append(testCases, testCase{
2513 protocol: dtls,
2514 name: "SRTP-Client-NoMatch",
2515 config: Config{
2516 Bugs: ProtocolBugs{
2517 SendSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_32,
2518 },
2519 },
2520 flags: []string{
2521 "-srtp-profiles",
2522 "SRTP_AES128_CM_SHA1_80",
2523 },
2524 shouldFail: true,
2525 expectedError: ":BAD_SRTP_PROTECTION_PROFILE_LIST:",
2526 })
David Benjamin61f95272014-11-25 01:55:35 -05002527 // Test OCSP stapling and SCT list.
2528 testCases = append(testCases, testCase{
2529 name: "OCSPStapling",
2530 flags: []string{
2531 "-enable-ocsp-stapling",
2532 "-expect-ocsp-response",
2533 base64.StdEncoding.EncodeToString(testOCSPResponse),
2534 },
2535 })
2536 testCases = append(testCases, testCase{
2537 name: "SignedCertificateTimestampList",
2538 flags: []string{
2539 "-enable-signed-cert-timestamps",
2540 "-expect-signed-cert-timestamps",
2541 base64.StdEncoding.EncodeToString(testSCTList),
2542 },
2543 })
David Benjamine78bfde2014-09-06 12:45:15 -04002544}
2545
David Benjamin01fe8202014-09-24 15:21:44 -04002546func addResumptionVersionTests() {
David Benjamin01fe8202014-09-24 15:21:44 -04002547 for _, sessionVers := range tlsVersions {
David Benjamin01fe8202014-09-24 15:21:44 -04002548 for _, resumeVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05002549 protocols := []protocol{tls}
2550 if sessionVers.hasDTLS && resumeVers.hasDTLS {
2551 protocols = append(protocols, dtls)
David Benjaminbdf5e722014-11-11 00:52:15 -05002552 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002553 for _, protocol := range protocols {
2554 suffix := "-" + sessionVers.name + "-" + resumeVers.name
2555 if protocol == dtls {
2556 suffix += "-DTLS"
2557 }
2558
2559 testCases = append(testCases, testCase{
2560 protocol: protocol,
2561 name: "Resume-Client" + suffix,
2562 resumeSession: true,
2563 config: Config{
2564 MaxVersion: sessionVers.version,
2565 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2566 Bugs: ProtocolBugs{
2567 AllowSessionVersionMismatch: true,
2568 },
2569 },
2570 expectedVersion: sessionVers.version,
2571 resumeConfig: &Config{
2572 MaxVersion: resumeVers.version,
2573 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2574 Bugs: ProtocolBugs{
2575 AllowSessionVersionMismatch: true,
2576 },
2577 },
2578 expectedResumeVersion: resumeVers.version,
2579 })
2580
2581 testCases = append(testCases, testCase{
2582 protocol: protocol,
2583 name: "Resume-Client-NoResume" + suffix,
2584 flags: []string{"-expect-session-miss"},
2585 resumeSession: true,
2586 config: Config{
2587 MaxVersion: sessionVers.version,
2588 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2589 },
2590 expectedVersion: sessionVers.version,
2591 resumeConfig: &Config{
2592 MaxVersion: resumeVers.version,
2593 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2594 },
2595 newSessionsOnResume: true,
2596 expectedResumeVersion: resumeVers.version,
2597 })
2598
2599 var flags []string
2600 if sessionVers.version != resumeVers.version {
2601 flags = append(flags, "-expect-session-miss")
2602 }
2603 testCases = append(testCases, testCase{
2604 protocol: protocol,
2605 testType: serverTest,
2606 name: "Resume-Server" + suffix,
2607 flags: flags,
2608 resumeSession: true,
2609 config: Config{
2610 MaxVersion: sessionVers.version,
2611 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2612 },
2613 expectedVersion: sessionVers.version,
2614 resumeConfig: &Config{
2615 MaxVersion: resumeVers.version,
2616 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2617 },
2618 expectedResumeVersion: resumeVers.version,
2619 })
2620 }
David Benjamin01fe8202014-09-24 15:21:44 -04002621 }
2622 }
2623}
2624
Adam Langley2ae77d22014-10-28 17:29:33 -07002625func addRenegotiationTests() {
2626 testCases = append(testCases, testCase{
2627 testType: serverTest,
2628 name: "Renegotiate-Server",
2629 flags: []string{"-renegotiate"},
2630 shimWritesFirst: true,
2631 })
2632 testCases = append(testCases, testCase{
2633 testType: serverTest,
David Benjamincdea40c2015-03-19 14:09:43 -04002634 name: "Renegotiate-Server-Full",
2635 config: Config{
2636 Bugs: ProtocolBugs{
2637 NeverResumeOnRenego: true,
2638 },
2639 },
2640 flags: []string{"-renegotiate"},
2641 shimWritesFirst: true,
2642 })
2643 testCases = append(testCases, testCase{
2644 testType: serverTest,
Adam Langley2ae77d22014-10-28 17:29:33 -07002645 name: "Renegotiate-Server-EmptyExt",
2646 config: Config{
2647 Bugs: ProtocolBugs{
2648 EmptyRenegotiationInfo: true,
2649 },
2650 },
2651 flags: []string{"-renegotiate"},
2652 shimWritesFirst: true,
2653 shouldFail: true,
2654 expectedError: ":RENEGOTIATION_MISMATCH:",
2655 })
2656 testCases = append(testCases, testCase{
2657 testType: serverTest,
2658 name: "Renegotiate-Server-BadExt",
2659 config: Config{
2660 Bugs: ProtocolBugs{
2661 BadRenegotiationInfo: true,
2662 },
2663 },
2664 flags: []string{"-renegotiate"},
2665 shimWritesFirst: true,
2666 shouldFail: true,
2667 expectedError: ":RENEGOTIATION_MISMATCH:",
2668 })
David Benjaminca6554b2014-11-08 12:31:52 -05002669 testCases = append(testCases, testCase{
2670 testType: serverTest,
2671 name: "Renegotiate-Server-ClientInitiated",
2672 renegotiate: true,
2673 })
2674 testCases = append(testCases, testCase{
2675 testType: serverTest,
2676 name: "Renegotiate-Server-ClientInitiated-NoExt",
2677 renegotiate: true,
2678 config: Config{
2679 Bugs: ProtocolBugs{
2680 NoRenegotiationInfo: true,
2681 },
2682 },
2683 shouldFail: true,
2684 expectedError: ":UNSAFE_LEGACY_RENEGOTIATION_DISABLED:",
2685 })
2686 testCases = append(testCases, testCase{
2687 testType: serverTest,
2688 name: "Renegotiate-Server-ClientInitiated-NoExt-Allowed",
2689 renegotiate: true,
2690 config: Config{
2691 Bugs: ProtocolBugs{
2692 NoRenegotiationInfo: true,
2693 },
2694 },
2695 flags: []string{"-allow-unsafe-legacy-renegotiation"},
2696 })
David Benjamin3c9746a2015-03-19 15:00:10 -04002697 // Regression test for CVE-2015-0291.
2698 testCases = append(testCases, testCase{
2699 testType: serverTest,
2700 name: "Renegotiate-Server-NoSignatureAlgorithms",
2701 config: Config{
2702 Bugs: ProtocolBugs{
2703 NeverResumeOnRenego: true,
2704 NoSignatureAlgorithmsOnRenego: true,
2705 },
2706 },
2707 flags: []string{"-renegotiate"},
2708 shimWritesFirst: true,
2709 })
Adam Langley2ae77d22014-10-28 17:29:33 -07002710 // TODO(agl): test the renegotiation info SCSV.
Adam Langleycf2d4f42014-10-28 19:06:14 -07002711 testCases = append(testCases, testCase{
2712 name: "Renegotiate-Client",
2713 renegotiate: true,
2714 })
2715 testCases = append(testCases, testCase{
David Benjamincdea40c2015-03-19 14:09:43 -04002716 name: "Renegotiate-Client-Full",
2717 config: Config{
2718 Bugs: ProtocolBugs{
2719 NeverResumeOnRenego: true,
2720 },
2721 },
2722 renegotiate: true,
2723 })
2724 testCases = append(testCases, testCase{
Adam Langleycf2d4f42014-10-28 19:06:14 -07002725 name: "Renegotiate-Client-EmptyExt",
2726 renegotiate: true,
2727 config: Config{
2728 Bugs: ProtocolBugs{
2729 EmptyRenegotiationInfo: true,
2730 },
2731 },
2732 shouldFail: true,
2733 expectedError: ":RENEGOTIATION_MISMATCH:",
2734 })
2735 testCases = append(testCases, testCase{
2736 name: "Renegotiate-Client-BadExt",
2737 renegotiate: true,
2738 config: Config{
2739 Bugs: ProtocolBugs{
2740 BadRenegotiationInfo: true,
2741 },
2742 },
2743 shouldFail: true,
2744 expectedError: ":RENEGOTIATION_MISMATCH:",
2745 })
2746 testCases = append(testCases, testCase{
2747 name: "Renegotiate-Client-SwitchCiphers",
2748 renegotiate: true,
2749 config: Config{
2750 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2751 },
2752 renegotiateCiphers: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2753 })
2754 testCases = append(testCases, testCase{
2755 name: "Renegotiate-Client-SwitchCiphers2",
2756 renegotiate: true,
2757 config: Config{
2758 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2759 },
2760 renegotiateCiphers: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2761 })
David Benjaminc44b1df2014-11-23 12:11:01 -05002762 testCases = append(testCases, testCase{
2763 name: "Renegotiate-SameClientVersion",
2764 renegotiate: true,
2765 config: Config{
2766 MaxVersion: VersionTLS10,
2767 Bugs: ProtocolBugs{
2768 RequireSameRenegoClientVersion: true,
2769 },
2770 },
2771 })
Adam Langley2ae77d22014-10-28 17:29:33 -07002772}
2773
David Benjamin5e961c12014-11-07 01:48:35 -05002774func addDTLSReplayTests() {
2775 // Test that sequence number replays are detected.
2776 testCases = append(testCases, testCase{
2777 protocol: dtls,
2778 name: "DTLS-Replay",
2779 replayWrites: true,
2780 })
2781
2782 // Test the outgoing sequence number skipping by values larger
2783 // than the retransmit window.
2784 testCases = append(testCases, testCase{
2785 protocol: dtls,
2786 name: "DTLS-Replay-LargeGaps",
2787 config: Config{
2788 Bugs: ProtocolBugs{
2789 SequenceNumberIncrement: 127,
2790 },
2791 },
2792 replayWrites: true,
2793 })
2794}
2795
Feng Lu41aa3252014-11-21 22:47:56 -08002796func addFastRadioPaddingTests() {
David Benjamin1e29a6b2014-12-10 02:27:24 -05002797 testCases = append(testCases, testCase{
2798 protocol: tls,
2799 name: "FastRadio-Padding",
Feng Lu41aa3252014-11-21 22:47:56 -08002800 config: Config{
2801 Bugs: ProtocolBugs{
2802 RequireFastradioPadding: true,
2803 },
2804 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05002805 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08002806 })
David Benjamin1e29a6b2014-12-10 02:27:24 -05002807 testCases = append(testCases, testCase{
2808 protocol: dtls,
David Benjamin5f237bc2015-02-11 17:14:15 -05002809 name: "FastRadio-Padding-DTLS",
Feng Lu41aa3252014-11-21 22:47:56 -08002810 config: Config{
2811 Bugs: ProtocolBugs{
2812 RequireFastradioPadding: true,
2813 },
2814 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05002815 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08002816 })
2817}
2818
David Benjamin000800a2014-11-14 01:43:59 -05002819var testHashes = []struct {
2820 name string
2821 id uint8
2822}{
2823 {"SHA1", hashSHA1},
2824 {"SHA224", hashSHA224},
2825 {"SHA256", hashSHA256},
2826 {"SHA384", hashSHA384},
2827 {"SHA512", hashSHA512},
2828}
2829
2830func addSigningHashTests() {
2831 // Make sure each hash works. Include some fake hashes in the list and
2832 // ensure they're ignored.
2833 for _, hash := range testHashes {
2834 testCases = append(testCases, testCase{
2835 name: "SigningHash-ClientAuth-" + hash.name,
2836 config: Config{
2837 ClientAuth: RequireAnyClientCert,
2838 SignatureAndHashes: []signatureAndHash{
2839 {signatureRSA, 42},
2840 {signatureRSA, hash.id},
2841 {signatureRSA, 255},
2842 },
2843 },
2844 flags: []string{
2845 "-cert-file", rsaCertificateFile,
2846 "-key-file", rsaKeyFile,
2847 },
2848 })
2849
2850 testCases = append(testCases, testCase{
2851 testType: serverTest,
2852 name: "SigningHash-ServerKeyExchange-Sign-" + hash.name,
2853 config: Config{
2854 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2855 SignatureAndHashes: []signatureAndHash{
2856 {signatureRSA, 42},
2857 {signatureRSA, hash.id},
2858 {signatureRSA, 255},
2859 },
2860 },
2861 })
2862 }
2863
2864 // Test that hash resolution takes the signature type into account.
2865 testCases = append(testCases, testCase{
2866 name: "SigningHash-ClientAuth-SignatureType",
2867 config: Config{
2868 ClientAuth: RequireAnyClientCert,
2869 SignatureAndHashes: []signatureAndHash{
2870 {signatureECDSA, hashSHA512},
2871 {signatureRSA, hashSHA384},
2872 {signatureECDSA, hashSHA1},
2873 },
2874 },
2875 flags: []string{
2876 "-cert-file", rsaCertificateFile,
2877 "-key-file", rsaKeyFile,
2878 },
2879 })
2880
2881 testCases = append(testCases, testCase{
2882 testType: serverTest,
2883 name: "SigningHash-ServerKeyExchange-SignatureType",
2884 config: Config{
2885 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2886 SignatureAndHashes: []signatureAndHash{
2887 {signatureECDSA, hashSHA512},
2888 {signatureRSA, hashSHA384},
2889 {signatureECDSA, hashSHA1},
2890 },
2891 },
2892 })
2893
2894 // Test that, if the list is missing, the peer falls back to SHA-1.
2895 testCases = append(testCases, testCase{
2896 name: "SigningHash-ClientAuth-Fallback",
2897 config: Config{
2898 ClientAuth: RequireAnyClientCert,
2899 SignatureAndHashes: []signatureAndHash{
2900 {signatureRSA, hashSHA1},
2901 },
2902 Bugs: ProtocolBugs{
2903 NoSignatureAndHashes: true,
2904 },
2905 },
2906 flags: []string{
2907 "-cert-file", rsaCertificateFile,
2908 "-key-file", rsaKeyFile,
2909 },
2910 })
2911
2912 testCases = append(testCases, testCase{
2913 testType: serverTest,
2914 name: "SigningHash-ServerKeyExchange-Fallback",
2915 config: Config{
2916 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2917 SignatureAndHashes: []signatureAndHash{
2918 {signatureRSA, hashSHA1},
2919 },
2920 Bugs: ProtocolBugs{
2921 NoSignatureAndHashes: true,
2922 },
2923 },
2924 })
2925}
2926
David Benjamin83f90402015-01-27 01:09:43 -05002927// timeouts is the retransmit schedule for BoringSSL. It doubles and
2928// caps at 60 seconds. On the 13th timeout, it gives up.
2929var timeouts = []time.Duration{
2930 1 * time.Second,
2931 2 * time.Second,
2932 4 * time.Second,
2933 8 * time.Second,
2934 16 * time.Second,
2935 32 * time.Second,
2936 60 * time.Second,
2937 60 * time.Second,
2938 60 * time.Second,
2939 60 * time.Second,
2940 60 * time.Second,
2941 60 * time.Second,
2942 60 * time.Second,
2943}
2944
2945func addDTLSRetransmitTests() {
2946 // Test that this is indeed the timeout schedule. Stress all
2947 // four patterns of handshake.
2948 for i := 1; i < len(timeouts); i++ {
2949 number := strconv.Itoa(i)
2950 testCases = append(testCases, testCase{
2951 protocol: dtls,
2952 name: "DTLS-Retransmit-Client-" + number,
2953 config: Config{
2954 Bugs: ProtocolBugs{
2955 TimeoutSchedule: timeouts[:i],
2956 },
2957 },
2958 resumeSession: true,
2959 flags: []string{"-async"},
2960 })
2961 testCases = append(testCases, testCase{
2962 protocol: dtls,
2963 testType: serverTest,
2964 name: "DTLS-Retransmit-Server-" + number,
2965 config: Config{
2966 Bugs: ProtocolBugs{
2967 TimeoutSchedule: timeouts[:i],
2968 },
2969 },
2970 resumeSession: true,
2971 flags: []string{"-async"},
2972 })
2973 }
2974
2975 // Test that exceeding the timeout schedule hits a read
2976 // timeout.
2977 testCases = append(testCases, testCase{
2978 protocol: dtls,
2979 name: "DTLS-Retransmit-Timeout",
2980 config: Config{
2981 Bugs: ProtocolBugs{
2982 TimeoutSchedule: timeouts,
2983 },
2984 },
2985 resumeSession: true,
2986 flags: []string{"-async"},
2987 shouldFail: true,
2988 expectedError: ":READ_TIMEOUT_EXPIRED:",
2989 })
2990
2991 // Test that timeout handling has a fudge factor, due to API
2992 // problems.
2993 testCases = append(testCases, testCase{
2994 protocol: dtls,
2995 name: "DTLS-Retransmit-Fudge",
2996 config: Config{
2997 Bugs: ProtocolBugs{
2998 TimeoutSchedule: []time.Duration{
2999 timeouts[0] - 10*time.Millisecond,
3000 },
3001 },
3002 },
3003 resumeSession: true,
3004 flags: []string{"-async"},
3005 })
David Benjamin7eaab4c2015-03-02 19:01:16 -05003006
3007 // Test that the final Finished retransmitting isn't
3008 // duplicated if the peer badly fragments everything.
3009 testCases = append(testCases, testCase{
3010 testType: serverTest,
3011 protocol: dtls,
3012 name: "DTLS-Retransmit-Fragmented",
3013 config: Config{
3014 Bugs: ProtocolBugs{
3015 TimeoutSchedule: []time.Duration{timeouts[0]},
3016 MaxHandshakeRecordLength: 2,
3017 },
3018 },
3019 flags: []string{"-async"},
3020 })
David Benjamin83f90402015-01-27 01:09:43 -05003021}
3022
David Benjamin884fdf12014-08-02 15:28:23 -04003023func worker(statusChan chan statusMsg, c chan *testCase, buildDir string, wg *sync.WaitGroup) {
Adam Langley95c29f32014-06-20 12:00:00 -07003024 defer wg.Done()
3025
3026 for test := range c {
Adam Langley69a01602014-11-17 17:26:55 -08003027 var err error
3028
3029 if *mallocTest < 0 {
3030 statusChan <- statusMsg{test: test, started: true}
3031 err = runTest(test, buildDir, -1)
3032 } else {
3033 for mallocNumToFail := int64(*mallocTest); ; mallocNumToFail++ {
3034 statusChan <- statusMsg{test: test, started: true}
3035 if err = runTest(test, buildDir, mallocNumToFail); err != errMoreMallocs {
3036 if err != nil {
3037 fmt.Printf("\n\nmalloc test failed at %d: %s\n", mallocNumToFail, err)
3038 }
3039 break
3040 }
3041 }
3042 }
Adam Langley95c29f32014-06-20 12:00:00 -07003043 statusChan <- statusMsg{test: test, err: err}
3044 }
3045}
3046
3047type statusMsg struct {
3048 test *testCase
3049 started bool
3050 err error
3051}
3052
David Benjamin5f237bc2015-02-11 17:14:15 -05003053func statusPrinter(doneChan chan *testOutput, statusChan chan statusMsg, total int) {
Adam Langley95c29f32014-06-20 12:00:00 -07003054 var started, done, failed, lineLen int
Adam Langley95c29f32014-06-20 12:00:00 -07003055
David Benjamin5f237bc2015-02-11 17:14:15 -05003056 testOutput := newTestOutput()
Adam Langley95c29f32014-06-20 12:00:00 -07003057 for msg := range statusChan {
David Benjamin5f237bc2015-02-11 17:14:15 -05003058 if !*pipe {
3059 // Erase the previous status line.
David Benjamin87c8a642015-02-21 01:54:29 -05003060 var erase string
3061 for i := 0; i < lineLen; i++ {
3062 erase += "\b \b"
3063 }
3064 fmt.Print(erase)
David Benjamin5f237bc2015-02-11 17:14:15 -05003065 }
3066
Adam Langley95c29f32014-06-20 12:00:00 -07003067 if msg.started {
3068 started++
3069 } else {
3070 done++
David Benjamin5f237bc2015-02-11 17:14:15 -05003071
3072 if msg.err != nil {
3073 fmt.Printf("FAILED (%s)\n%s\n", msg.test.name, msg.err)
3074 failed++
3075 testOutput.addResult(msg.test.name, "FAIL")
3076 } else {
3077 if *pipe {
3078 // Print each test instead of a status line.
3079 fmt.Printf("PASSED (%s)\n", msg.test.name)
3080 }
3081 testOutput.addResult(msg.test.name, "PASS")
3082 }
Adam Langley95c29f32014-06-20 12:00:00 -07003083 }
3084
David Benjamin5f237bc2015-02-11 17:14:15 -05003085 if !*pipe {
3086 // Print a new status line.
3087 line := fmt.Sprintf("%d/%d/%d/%d", failed, done, started, total)
3088 lineLen = len(line)
3089 os.Stdout.WriteString(line)
Adam Langley95c29f32014-06-20 12:00:00 -07003090 }
Adam Langley95c29f32014-06-20 12:00:00 -07003091 }
David Benjamin5f237bc2015-02-11 17:14:15 -05003092
3093 doneChan <- testOutput
Adam Langley95c29f32014-06-20 12:00:00 -07003094}
3095
3096func main() {
3097 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 -04003098 var flagNumWorkers *int = flag.Int("num-workers", runtime.NumCPU(), "The number of workers to run in parallel.")
David Benjamin884fdf12014-08-02 15:28:23 -04003099 var flagBuildDir *string = flag.String("build-dir", "../../../build", "The build directory to run the shim from.")
Adam Langley95c29f32014-06-20 12:00:00 -07003100
3101 flag.Parse()
3102
3103 addCipherSuiteTests()
3104 addBadECDSASignatureTests()
Adam Langley80842bd2014-06-20 12:00:00 -07003105 addCBCPaddingTests()
Kenny Root7fdeaf12014-08-05 15:23:37 -07003106 addCBCSplittingTests()
David Benjamin636293b2014-07-08 17:59:18 -04003107 addClientAuthTests()
Adam Langley524e7172015-02-20 16:04:00 -08003108 addDDoSCallbackTests()
David Benjamin7e2e6cf2014-08-07 17:44:24 -04003109 addVersionNegotiationTests()
David Benjaminaccb4542014-12-12 23:44:33 -05003110 addMinimumVersionTests()
David Benjamin5c24a1d2014-08-31 00:59:27 -04003111 addD5BugTests()
David Benjamine78bfde2014-09-06 12:45:15 -04003112 addExtensionTests()
David Benjamin01fe8202014-09-24 15:21:44 -04003113 addResumptionVersionTests()
Adam Langley75712922014-10-10 16:23:43 -07003114 addExtendedMasterSecretTests()
Adam Langley2ae77d22014-10-28 17:29:33 -07003115 addRenegotiationTests()
David Benjamin5e961c12014-11-07 01:48:35 -05003116 addDTLSReplayTests()
David Benjamin000800a2014-11-14 01:43:59 -05003117 addSigningHashTests()
Feng Lu41aa3252014-11-21 22:47:56 -08003118 addFastRadioPaddingTests()
David Benjamin83f90402015-01-27 01:09:43 -05003119 addDTLSRetransmitTests()
David Benjamin43ec06f2014-08-05 02:28:57 -04003120 for _, async := range []bool{false, true} {
3121 for _, splitHandshake := range []bool{false, true} {
David Benjamin6fd297b2014-08-11 18:43:38 -04003122 for _, protocol := range []protocol{tls, dtls} {
3123 addStateMachineCoverageTests(async, splitHandshake, protocol)
3124 }
David Benjamin43ec06f2014-08-05 02:28:57 -04003125 }
3126 }
Adam Langley95c29f32014-06-20 12:00:00 -07003127
3128 var wg sync.WaitGroup
3129
David Benjamin2bc8e6f2014-08-02 15:22:37 -04003130 numWorkers := *flagNumWorkers
Adam Langley95c29f32014-06-20 12:00:00 -07003131
3132 statusChan := make(chan statusMsg, numWorkers)
3133 testChan := make(chan *testCase, numWorkers)
David Benjamin5f237bc2015-02-11 17:14:15 -05003134 doneChan := make(chan *testOutput)
Adam Langley95c29f32014-06-20 12:00:00 -07003135
David Benjamin025b3d32014-07-01 19:53:04 -04003136 go statusPrinter(doneChan, statusChan, len(testCases))
Adam Langley95c29f32014-06-20 12:00:00 -07003137
3138 for i := 0; i < numWorkers; i++ {
3139 wg.Add(1)
David Benjamin884fdf12014-08-02 15:28:23 -04003140 go worker(statusChan, testChan, *flagBuildDir, &wg)
Adam Langley95c29f32014-06-20 12:00:00 -07003141 }
3142
David Benjamin025b3d32014-07-01 19:53:04 -04003143 for i := range testCases {
3144 if len(*flagTest) == 0 || *flagTest == testCases[i].name {
3145 testChan <- &testCases[i]
Adam Langley95c29f32014-06-20 12:00:00 -07003146 }
3147 }
3148
3149 close(testChan)
3150 wg.Wait()
3151 close(statusChan)
David Benjamin5f237bc2015-02-11 17:14:15 -05003152 testOutput := <-doneChan
Adam Langley95c29f32014-06-20 12:00:00 -07003153
3154 fmt.Printf("\n")
David Benjamin5f237bc2015-02-11 17:14:15 -05003155
3156 if *jsonOutput != "" {
3157 if err := testOutput.writeTo(*jsonOutput); err != nil {
3158 fmt.Fprintf(os.Stderr, "Error: %s\n", err)
3159 }
3160 }
Adam Langley95c29f32014-06-20 12:00:00 -07003161}