blob: 1c8444075da10598d67aa1e87715926bb9f7f10b [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 {
578 name: "RSAServerKeyExchange",
579 config: Config{
580 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
581 Bugs: ProtocolBugs{
582 RSAServerKeyExchange: true,
583 },
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 {
694 protocol: dtls,
695 name: "ReorderHandshakeFragments-Small-DTLS",
696 config: Config{
697 Bugs: ProtocolBugs{
698 ReorderHandshakeFragments: true,
699 // Small enough that every handshake message is
700 // fragmented.
701 MaxHandshakeRecordLength: 2,
702 },
703 },
704 },
705 {
706 protocol: dtls,
707 name: "ReorderHandshakeFragments-Large-DTLS",
708 config: Config{
709 Bugs: ProtocolBugs{
710 ReorderHandshakeFragments: true,
711 // Large enough that no handshake message is
712 // fragmented.
713 //
714 // TODO(davidben): Also test interaction of
715 // complete handshake messages with
716 // fragments. The current logic is full of bugs
717 // here, so the reassembly logic needs a rewrite
718 // before those tests will pass.
719 MaxHandshakeRecordLength: 2048,
720 },
721 },
722 },
David Benjaminddb9f152015-02-03 15:44:39 -0500723 {
724 name: "SendInvalidRecordType",
725 config: Config{
726 Bugs: ProtocolBugs{
727 SendInvalidRecordType: true,
728 },
729 },
730 shouldFail: true,
731 expectedError: ":UNEXPECTED_RECORD:",
732 },
733 {
734 protocol: dtls,
735 name: "SendInvalidRecordType-DTLS",
736 config: Config{
737 Bugs: ProtocolBugs{
738 SendInvalidRecordType: true,
739 },
740 },
741 shouldFail: true,
742 expectedError: ":UNEXPECTED_RECORD:",
743 },
David Benjaminb80168e2015-02-08 18:30:14 -0500744 {
745 name: "FalseStart-SkipServerSecondLeg",
746 config: Config{
747 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
748 NextProtos: []string{"foo"},
749 Bugs: ProtocolBugs{
750 SkipNewSessionTicket: true,
751 SkipChangeCipherSpec: true,
752 SkipFinished: true,
753 ExpectFalseStart: true,
754 },
755 },
756 flags: []string{
757 "-false-start",
758 "-advertise-alpn", "\x03foo",
759 },
760 shimWritesFirst: true,
761 shouldFail: true,
762 expectedError: ":UNEXPECTED_RECORD:",
763 },
David Benjamin931ab342015-02-08 19:46:57 -0500764 {
765 name: "FalseStart-SkipServerSecondLeg-Implicit",
766 config: Config{
767 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
768 NextProtos: []string{"foo"},
769 Bugs: ProtocolBugs{
770 SkipNewSessionTicket: true,
771 SkipChangeCipherSpec: true,
772 SkipFinished: true,
773 },
774 },
775 flags: []string{
776 "-implicit-handshake",
777 "-false-start",
778 "-advertise-alpn", "\x03foo",
779 },
780 shouldFail: true,
781 expectedError: ":UNEXPECTED_RECORD:",
782 },
Adam Langley95c29f32014-06-20 12:00:00 -0700783}
784
David Benjamin01fe8202014-09-24 15:21:44 -0400785func doExchange(test *testCase, config *Config, conn net.Conn, messageLen int, isResume bool) error {
David Benjamin65ea8ff2014-11-23 03:01:00 -0500786 var connDebug *recordingConn
David Benjamin5fa3eba2015-01-22 16:35:40 -0500787 var connDamage *damageAdaptor
David Benjamin65ea8ff2014-11-23 03:01:00 -0500788 if *flagDebug {
789 connDebug = &recordingConn{Conn: conn}
790 conn = connDebug
791 defer func() {
792 connDebug.WriteTo(os.Stdout)
793 }()
794 }
795
David Benjamin6fd297b2014-08-11 18:43:38 -0400796 if test.protocol == dtls {
David Benjamin83f90402015-01-27 01:09:43 -0500797 config.Bugs.PacketAdaptor = newPacketAdaptor(conn)
798 conn = config.Bugs.PacketAdaptor
David Benjamin5e961c12014-11-07 01:48:35 -0500799 if test.replayWrites {
800 conn = newReplayAdaptor(conn)
801 }
David Benjamin6fd297b2014-08-11 18:43:38 -0400802 }
803
David Benjamin5fa3eba2015-01-22 16:35:40 -0500804 if test.damageFirstWrite {
805 connDamage = newDamageAdaptor(conn)
806 conn = connDamage
807 }
808
David Benjamin6fd297b2014-08-11 18:43:38 -0400809 if test.sendPrefix != "" {
810 if _, err := conn.Write([]byte(test.sendPrefix)); err != nil {
811 return err
812 }
David Benjamin98e882e2014-08-08 13:24:34 -0400813 }
814
David Benjamin1d5c83e2014-07-22 19:20:02 -0400815 var tlsConn *Conn
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400816 if test.testType == clientTest {
David Benjamin6fd297b2014-08-11 18:43:38 -0400817 if test.protocol == dtls {
818 tlsConn = DTLSServer(conn, config)
819 } else {
820 tlsConn = Server(conn, config)
821 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400822 } else {
823 config.InsecureSkipVerify = true
David Benjamin6fd297b2014-08-11 18:43:38 -0400824 if test.protocol == dtls {
825 tlsConn = DTLSClient(conn, config)
826 } else {
827 tlsConn = Client(conn, config)
828 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400829 }
830
Adam Langley95c29f32014-06-20 12:00:00 -0700831 if err := tlsConn.Handshake(); err != nil {
832 return err
833 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700834
David Benjamin01fe8202014-09-24 15:21:44 -0400835 // TODO(davidben): move all per-connection expectations into a dedicated
836 // expectations struct that can be specified separately for the two
837 // legs.
838 expectedVersion := test.expectedVersion
839 if isResume && test.expectedResumeVersion != 0 {
840 expectedVersion = test.expectedResumeVersion
841 }
842 if vers := tlsConn.ConnectionState().Version; expectedVersion != 0 && vers != expectedVersion {
843 return fmt.Errorf("got version %x, expected %x", vers, expectedVersion)
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400844 }
845
David Benjamina08e49d2014-08-24 01:46:07 -0400846 if test.expectChannelID {
847 channelID := tlsConn.ConnectionState().ChannelID
848 if channelID == nil {
849 return fmt.Errorf("no channel ID negotiated")
850 }
851 if channelID.Curve != channelIDKey.Curve ||
852 channelIDKey.X.Cmp(channelIDKey.X) != 0 ||
853 channelIDKey.Y.Cmp(channelIDKey.Y) != 0 {
854 return fmt.Errorf("incorrect channel ID")
855 }
856 }
857
David Benjaminae2888f2014-09-06 12:58:58 -0400858 if expected := test.expectedNextProto; expected != "" {
859 if actual := tlsConn.ConnectionState().NegotiatedProtocol; actual != expected {
860 return fmt.Errorf("next proto mismatch: got %s, wanted %s", actual, expected)
861 }
862 }
863
David Benjaminfc7b0862014-09-06 13:21:53 -0400864 if test.expectedNextProtoType != 0 {
865 if (test.expectedNextProtoType == alpn) != tlsConn.ConnectionState().NegotiatedProtocolFromALPN {
866 return fmt.Errorf("next proto type mismatch")
867 }
868 }
869
David Benjaminca6c8262014-11-15 19:06:08 -0500870 if p := tlsConn.ConnectionState().SRTPProtectionProfile; p != test.expectedSRTPProtectionProfile {
871 return fmt.Errorf("SRTP profile mismatch: got %d, wanted %d", p, test.expectedSRTPProtectionProfile)
872 }
873
David Benjamine58c4f52014-08-24 03:47:07 -0400874 if test.shimWritesFirst {
875 var buf [5]byte
876 _, err := io.ReadFull(tlsConn, buf[:])
877 if err != nil {
878 return err
879 }
880 if string(buf[:]) != "hello" {
881 return fmt.Errorf("bad initial message")
882 }
883 }
884
Adam Langleycf2d4f42014-10-28 19:06:14 -0700885 if test.renegotiate {
886 if test.renegotiateCiphers != nil {
887 config.CipherSuites = test.renegotiateCiphers
888 }
889 if err := tlsConn.Renegotiate(); err != nil {
890 return err
891 }
892 } else if test.renegotiateCiphers != nil {
893 panic("renegotiateCiphers without renegotiate")
894 }
895
David Benjamin5fa3eba2015-01-22 16:35:40 -0500896 if test.damageFirstWrite {
897 connDamage.setDamage(true)
898 tlsConn.Write([]byte("DAMAGED WRITE"))
899 connDamage.setDamage(false)
900 }
901
Kenny Root7fdeaf12014-08-05 15:23:37 -0700902 if messageLen < 0 {
David Benjamin6fd297b2014-08-11 18:43:38 -0400903 if test.protocol == dtls {
904 return fmt.Errorf("messageLen < 0 not supported for DTLS tests")
905 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700906 // Read until EOF.
907 _, err := io.Copy(ioutil.Discard, tlsConn)
908 return err
909 }
910
David Benjamin4189bd92015-01-25 23:52:39 -0500911 var testMessage []byte
912 if config.Bugs.AppDataAfterChangeCipherSpec != nil {
913 // We've already sent a message. Expect the shim to echo it
914 // back.
915 testMessage = config.Bugs.AppDataAfterChangeCipherSpec
916 } else {
917 if messageLen == 0 {
918 messageLen = 32
919 }
920 testMessage = make([]byte, messageLen)
921 for i := range testMessage {
922 testMessage[i] = 0x42
923 }
924 tlsConn.Write(testMessage)
Adam Langley80842bd2014-06-20 12:00:00 -0700925 }
Adam Langley95c29f32014-06-20 12:00:00 -0700926
927 buf := make([]byte, len(testMessage))
David Benjamin6fd297b2014-08-11 18:43:38 -0400928 if test.protocol == dtls {
929 bufTmp := make([]byte, len(buf)+1)
930 n, err := tlsConn.Read(bufTmp)
931 if err != nil {
932 return err
933 }
934 if n != len(buf) {
935 return fmt.Errorf("bad reply; length mismatch (%d vs %d)", n, len(buf))
936 }
937 copy(buf, bufTmp)
938 } else {
939 _, err := io.ReadFull(tlsConn, buf)
940 if err != nil {
941 return err
942 }
Adam Langley95c29f32014-06-20 12:00:00 -0700943 }
944
945 for i, v := range buf {
946 if v != testMessage[i]^0xff {
947 return fmt.Errorf("bad reply contents at byte %d", i)
948 }
949 }
950
951 return nil
952}
953
David Benjamin325b5c32014-07-01 19:40:31 -0400954func valgrindOf(dbAttach bool, path string, args ...string) *exec.Cmd {
955 valgrindArgs := []string{"--error-exitcode=99", "--track-origins=yes", "--leak-check=full"}
Adam Langley95c29f32014-06-20 12:00:00 -0700956 if dbAttach {
David Benjamin325b5c32014-07-01 19:40:31 -0400957 valgrindArgs = append(valgrindArgs, "--db-attach=yes", "--db-command=xterm -e gdb -nw %f %p")
Adam Langley95c29f32014-06-20 12:00:00 -0700958 }
David Benjamin325b5c32014-07-01 19:40:31 -0400959 valgrindArgs = append(valgrindArgs, path)
960 valgrindArgs = append(valgrindArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -0700961
David Benjamin325b5c32014-07-01 19:40:31 -0400962 return exec.Command("valgrind", valgrindArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -0700963}
964
David Benjamin325b5c32014-07-01 19:40:31 -0400965func gdbOf(path string, args ...string) *exec.Cmd {
966 xtermArgs := []string{"-e", "gdb", "--args"}
967 xtermArgs = append(xtermArgs, path)
968 xtermArgs = append(xtermArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -0700969
David Benjamin325b5c32014-07-01 19:40:31 -0400970 return exec.Command("xterm", xtermArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -0700971}
972
Adam Langley69a01602014-11-17 17:26:55 -0800973type moreMallocsError struct{}
974
975func (moreMallocsError) Error() string {
976 return "child process did not exhaust all allocation calls"
977}
978
979var errMoreMallocs = moreMallocsError{}
980
David Benjamin87c8a642015-02-21 01:54:29 -0500981// accept accepts a connection from listener, unless waitChan signals a process
982// exit first.
983func acceptOrWait(listener net.Listener, waitChan chan error) (net.Conn, error) {
984 type connOrError struct {
985 conn net.Conn
986 err error
987 }
988 connChan := make(chan connOrError, 1)
989 go func() {
990 conn, err := listener.Accept()
991 connChan <- connOrError{conn, err}
992 close(connChan)
993 }()
994 select {
995 case result := <-connChan:
996 return result.conn, result.err
997 case childErr := <-waitChan:
998 waitChan <- childErr
999 return nil, fmt.Errorf("child exited early: %s", childErr)
1000 }
1001}
1002
Adam Langley69a01602014-11-17 17:26:55 -08001003func runTest(test *testCase, buildDir string, mallocNumToFail int64) error {
Adam Langley38311732014-10-16 19:04:35 -07001004 if !test.shouldFail && (len(test.expectedError) > 0 || len(test.expectedLocalError) > 0) {
1005 panic("Error expected without shouldFail in " + test.name)
1006 }
1007
David Benjamin87c8a642015-02-21 01:54:29 -05001008 listener, err := net.ListenTCP("tcp4", &net.TCPAddr{IP: net.IP{127, 0, 0, 1}})
1009 if err != nil {
1010 panic(err)
1011 }
1012 defer func() {
1013 if listener != nil {
1014 listener.Close()
1015 }
1016 }()
Adam Langley95c29f32014-06-20 12:00:00 -07001017
David Benjamin884fdf12014-08-02 15:28:23 -04001018 shim_path := path.Join(buildDir, "ssl/test/bssl_shim")
David Benjamin87c8a642015-02-21 01:54:29 -05001019 flags := []string{"-port", strconv.Itoa(listener.Addr().(*net.TCPAddr).Port)}
David Benjamin1d5c83e2014-07-22 19:20:02 -04001020 if test.testType == serverTest {
David Benjamin5a593af2014-08-11 19:51:50 -04001021 flags = append(flags, "-server")
1022
David Benjamin025b3d32014-07-01 19:53:04 -04001023 flags = append(flags, "-key-file")
1024 if test.keyFile == "" {
1025 flags = append(flags, rsaKeyFile)
1026 } else {
1027 flags = append(flags, test.keyFile)
1028 }
1029
1030 flags = append(flags, "-cert-file")
1031 if test.certFile == "" {
1032 flags = append(flags, rsaCertificateFile)
1033 } else {
1034 flags = append(flags, test.certFile)
1035 }
1036 }
David Benjamin5a593af2014-08-11 19:51:50 -04001037
David Benjamin6fd297b2014-08-11 18:43:38 -04001038 if test.protocol == dtls {
1039 flags = append(flags, "-dtls")
1040 }
1041
David Benjamin5a593af2014-08-11 19:51:50 -04001042 if test.resumeSession {
1043 flags = append(flags, "-resume")
1044 }
1045
David Benjamine58c4f52014-08-24 03:47:07 -04001046 if test.shimWritesFirst {
1047 flags = append(flags, "-shim-writes-first")
1048 }
1049
David Benjamin025b3d32014-07-01 19:53:04 -04001050 flags = append(flags, test.flags...)
1051
1052 var shim *exec.Cmd
1053 if *useValgrind {
1054 shim = valgrindOf(false, shim_path, flags...)
Adam Langley75712922014-10-10 16:23:43 -07001055 } else if *useGDB {
1056 shim = gdbOf(shim_path, flags...)
David Benjamin025b3d32014-07-01 19:53:04 -04001057 } else {
1058 shim = exec.Command(shim_path, flags...)
1059 }
David Benjamin025b3d32014-07-01 19:53:04 -04001060 shim.Stdin = os.Stdin
1061 var stdoutBuf, stderrBuf bytes.Buffer
1062 shim.Stdout = &stdoutBuf
1063 shim.Stderr = &stderrBuf
Adam Langley69a01602014-11-17 17:26:55 -08001064 if mallocNumToFail >= 0 {
David Benjamin9e128b02015-02-09 13:13:09 -05001065 shim.Env = os.Environ()
1066 shim.Env = append(shim.Env, "MALLOC_NUMBER_TO_FAIL="+strconv.FormatInt(mallocNumToFail, 10))
Adam Langley69a01602014-11-17 17:26:55 -08001067 if *mallocTestDebug {
1068 shim.Env = append(shim.Env, "MALLOC_ABORT_ON_FAIL=1")
1069 }
1070 shim.Env = append(shim.Env, "_MALLOC_CHECK=1")
1071 }
David Benjamin025b3d32014-07-01 19:53:04 -04001072
1073 if err := shim.Start(); err != nil {
Adam Langley95c29f32014-06-20 12:00:00 -07001074 panic(err)
1075 }
David Benjamin87c8a642015-02-21 01:54:29 -05001076 waitChan := make(chan error, 1)
1077 go func() { waitChan <- shim.Wait() }()
Adam Langley95c29f32014-06-20 12:00:00 -07001078
1079 config := test.config
David Benjamin1d5c83e2014-07-22 19:20:02 -04001080 config.ClientSessionCache = NewLRUClientSessionCache(1)
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001081 config.ServerSessionCache = NewLRUServerSessionCache(1)
David Benjamin025b3d32014-07-01 19:53:04 -04001082 if test.testType == clientTest {
1083 if len(config.Certificates) == 0 {
1084 config.Certificates = []Certificate{getRSACertificate()}
1085 }
David Benjamin87c8a642015-02-21 01:54:29 -05001086 } else {
1087 // Supply a ServerName to ensure a constant session cache key,
1088 // rather than falling back to net.Conn.RemoteAddr.
1089 if len(config.ServerName) == 0 {
1090 config.ServerName = "test"
1091 }
David Benjamin025b3d32014-07-01 19:53:04 -04001092 }
Adam Langley95c29f32014-06-20 12:00:00 -07001093
David Benjamin87c8a642015-02-21 01:54:29 -05001094 conn, err := acceptOrWait(listener, waitChan)
1095 if err == nil {
1096 err = doExchange(test, &config, conn, test.messageLen, false /* not a resumption */)
1097 conn.Close()
1098 }
David Benjamin65ea8ff2014-11-23 03:01:00 -05001099
David Benjamin1d5c83e2014-07-22 19:20:02 -04001100 if err == nil && test.resumeSession {
David Benjamin01fe8202014-09-24 15:21:44 -04001101 var resumeConfig Config
1102 if test.resumeConfig != nil {
1103 resumeConfig = *test.resumeConfig
David Benjamin87c8a642015-02-21 01:54:29 -05001104 if len(resumeConfig.ServerName) == 0 {
1105 resumeConfig.ServerName = config.ServerName
1106 }
David Benjamin01fe8202014-09-24 15:21:44 -04001107 if len(resumeConfig.Certificates) == 0 {
1108 resumeConfig.Certificates = []Certificate{getRSACertificate()}
1109 }
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001110 if !test.newSessionsOnResume {
1111 resumeConfig.SessionTicketKey = config.SessionTicketKey
1112 resumeConfig.ClientSessionCache = config.ClientSessionCache
1113 resumeConfig.ServerSessionCache = config.ServerSessionCache
1114 }
David Benjamin01fe8202014-09-24 15:21:44 -04001115 } else {
1116 resumeConfig = config
1117 }
David Benjamin87c8a642015-02-21 01:54:29 -05001118 var connResume net.Conn
1119 connResume, err = acceptOrWait(listener, waitChan)
1120 if err == nil {
1121 err = doExchange(test, &resumeConfig, connResume, test.messageLen, true /* resumption */)
1122 connResume.Close()
1123 }
David Benjamin1d5c83e2014-07-22 19:20:02 -04001124 }
1125
David Benjamin87c8a642015-02-21 01:54:29 -05001126 // Close the listener now. This is to avoid hangs should the shim try to
1127 // open more connections than expected.
1128 listener.Close()
1129 listener = nil
1130
1131 childErr := <-waitChan
Adam Langley69a01602014-11-17 17:26:55 -08001132 if exitError, ok := childErr.(*exec.ExitError); ok {
1133 if exitError.Sys().(syscall.WaitStatus).ExitStatus() == 88 {
1134 return errMoreMallocs
1135 }
1136 }
Adam Langley95c29f32014-06-20 12:00:00 -07001137
1138 stdout := string(stdoutBuf.Bytes())
1139 stderr := string(stderrBuf.Bytes())
1140 failed := err != nil || childErr != nil
1141 correctFailure := len(test.expectedError) == 0 || strings.Contains(stdout, test.expectedError)
Adam Langleyac61fa32014-06-23 12:03:11 -07001142 localError := "none"
1143 if err != nil {
1144 localError = err.Error()
1145 }
1146 if len(test.expectedLocalError) != 0 {
1147 correctFailure = correctFailure && strings.Contains(localError, test.expectedLocalError)
1148 }
Adam Langley95c29f32014-06-20 12:00:00 -07001149
1150 if failed != test.shouldFail || failed && !correctFailure {
Adam Langley95c29f32014-06-20 12:00:00 -07001151 childError := "none"
Adam Langley95c29f32014-06-20 12:00:00 -07001152 if childErr != nil {
1153 childError = childErr.Error()
1154 }
1155
1156 var msg string
1157 switch {
1158 case failed && !test.shouldFail:
1159 msg = "unexpected failure"
1160 case !failed && test.shouldFail:
1161 msg = "unexpected success"
1162 case failed && !correctFailure:
Adam Langleyac61fa32014-06-23 12:03:11 -07001163 msg = "bad error (wanted '" + test.expectedError + "' / '" + test.expectedLocalError + "')"
Adam Langley95c29f32014-06-20 12:00:00 -07001164 default:
1165 panic("internal error")
1166 }
1167
1168 return fmt.Errorf("%s: local error '%s', child error '%s', stdout:\n%s\nstderr:\n%s", msg, localError, childError, string(stdoutBuf.Bytes()), stderr)
1169 }
1170
1171 if !*useValgrind && len(stderr) > 0 {
1172 println(stderr)
1173 }
1174
1175 return nil
1176}
1177
1178var tlsVersions = []struct {
1179 name string
1180 version uint16
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001181 flag string
David Benjamin8b8c0062014-11-23 02:47:52 -05001182 hasDTLS bool
Adam Langley95c29f32014-06-20 12:00:00 -07001183}{
David Benjamin8b8c0062014-11-23 02:47:52 -05001184 {"SSL3", VersionSSL30, "-no-ssl3", false},
1185 {"TLS1", VersionTLS10, "-no-tls1", true},
1186 {"TLS11", VersionTLS11, "-no-tls11", false},
1187 {"TLS12", VersionTLS12, "-no-tls12", true},
Adam Langley95c29f32014-06-20 12:00:00 -07001188}
1189
1190var testCipherSuites = []struct {
1191 name string
1192 id uint16
1193}{
1194 {"3DES-SHA", TLS_RSA_WITH_3DES_EDE_CBC_SHA},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001195 {"AES128-GCM", TLS_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001196 {"AES128-SHA", TLS_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001197 {"AES128-SHA256", TLS_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001198 {"AES256-GCM", TLS_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001199 {"AES256-SHA", TLS_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001200 {"AES256-SHA256", TLS_RSA_WITH_AES_256_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001201 {"DHE-RSA-AES128-GCM", TLS_DHE_RSA_WITH_AES_128_GCM_SHA256},
1202 {"DHE-RSA-AES128-SHA", TLS_DHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001203 {"DHE-RSA-AES128-SHA256", TLS_DHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001204 {"DHE-RSA-AES256-GCM", TLS_DHE_RSA_WITH_AES_256_GCM_SHA384},
1205 {"DHE-RSA-AES256-SHA", TLS_DHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001206 {"DHE-RSA-AES256-SHA256", TLS_DHE_RSA_WITH_AES_256_CBC_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001207 {"ECDHE-ECDSA-AES128-GCM", TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
1208 {"ECDHE-ECDSA-AES128-SHA", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001209 {"ECDHE-ECDSA-AES128-SHA256", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256},
1210 {"ECDHE-ECDSA-AES256-GCM", TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001211 {"ECDHE-ECDSA-AES256-SHA", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001212 {"ECDHE-ECDSA-AES256-SHA384", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001213 {"ECDHE-ECDSA-RC4-SHA", TLS_ECDHE_ECDSA_WITH_RC4_128_SHA},
David Benjamin2af684f2014-10-27 02:23:15 -04001214 {"ECDHE-PSK-WITH-AES-128-GCM-SHA256", TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001215 {"ECDHE-RSA-AES128-GCM", TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001216 {"ECDHE-RSA-AES128-SHA", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001217 {"ECDHE-RSA-AES128-SHA256", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001218 {"ECDHE-RSA-AES256-GCM", TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001219 {"ECDHE-RSA-AES256-SHA", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001220 {"ECDHE-RSA-AES256-SHA384", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001221 {"ECDHE-RSA-RC4-SHA", TLS_ECDHE_RSA_WITH_RC4_128_SHA},
David Benjamin48cae082014-10-27 01:06:24 -04001222 {"PSK-AES128-CBC-SHA", TLS_PSK_WITH_AES_128_CBC_SHA},
1223 {"PSK-AES256-CBC-SHA", TLS_PSK_WITH_AES_256_CBC_SHA},
1224 {"PSK-RC4-SHA", TLS_PSK_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -07001225 {"RC4-MD5", TLS_RSA_WITH_RC4_128_MD5},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001226 {"RC4-SHA", TLS_RSA_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -07001227}
1228
David Benjamin8b8c0062014-11-23 02:47:52 -05001229func hasComponent(suiteName, component string) bool {
1230 return strings.Contains("-"+suiteName+"-", "-"+component+"-")
1231}
1232
David Benjaminf7768e42014-08-31 02:06:47 -04001233func isTLS12Only(suiteName string) bool {
David Benjamin8b8c0062014-11-23 02:47:52 -05001234 return hasComponent(suiteName, "GCM") ||
1235 hasComponent(suiteName, "SHA256") ||
1236 hasComponent(suiteName, "SHA384")
1237}
1238
1239func isDTLSCipher(suiteName string) bool {
David Benjamine95d20d2014-12-23 11:16:01 -05001240 return !hasComponent(suiteName, "RC4")
David Benjaminf7768e42014-08-31 02:06:47 -04001241}
1242
Adam Langley95c29f32014-06-20 12:00:00 -07001243func addCipherSuiteTests() {
1244 for _, suite := range testCipherSuites {
David Benjamin48cae082014-10-27 01:06:24 -04001245 const psk = "12345"
1246 const pskIdentity = "luggage combo"
1247
Adam Langley95c29f32014-06-20 12:00:00 -07001248 var cert Certificate
David Benjamin025b3d32014-07-01 19:53:04 -04001249 var certFile string
1250 var keyFile string
David Benjamin8b8c0062014-11-23 02:47:52 -05001251 if hasComponent(suite.name, "ECDSA") {
Adam Langley95c29f32014-06-20 12:00:00 -07001252 cert = getECDSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -04001253 certFile = ecdsaCertificateFile
1254 keyFile = ecdsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -07001255 } else {
1256 cert = getRSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -04001257 certFile = rsaCertificateFile
1258 keyFile = rsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -07001259 }
1260
David Benjamin48cae082014-10-27 01:06:24 -04001261 var flags []string
David Benjamin8b8c0062014-11-23 02:47:52 -05001262 if hasComponent(suite.name, "PSK") {
David Benjamin48cae082014-10-27 01:06:24 -04001263 flags = append(flags,
1264 "-psk", psk,
1265 "-psk-identity", pskIdentity)
1266 }
1267
Adam Langley95c29f32014-06-20 12:00:00 -07001268 for _, ver := range tlsVersions {
David Benjaminf7768e42014-08-31 02:06:47 -04001269 if ver.version < VersionTLS12 && isTLS12Only(suite.name) {
Adam Langley95c29f32014-06-20 12:00:00 -07001270 continue
1271 }
1272
David Benjamin025b3d32014-07-01 19:53:04 -04001273 testCases = append(testCases, testCase{
1274 testType: clientTest,
1275 name: ver.name + "-" + suite.name + "-client",
Adam Langley95c29f32014-06-20 12:00:00 -07001276 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001277 MinVersion: ver.version,
1278 MaxVersion: ver.version,
1279 CipherSuites: []uint16{suite.id},
1280 Certificates: []Certificate{cert},
1281 PreSharedKey: []byte(psk),
1282 PreSharedKeyIdentity: pskIdentity,
Adam Langley95c29f32014-06-20 12:00:00 -07001283 },
David Benjamin48cae082014-10-27 01:06:24 -04001284 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001285 resumeSession: true,
Adam Langley95c29f32014-06-20 12:00:00 -07001286 })
David Benjamin025b3d32014-07-01 19:53:04 -04001287
David Benjamin76d8abe2014-08-14 16:25:34 -04001288 testCases = append(testCases, testCase{
1289 testType: serverTest,
1290 name: ver.name + "-" + suite.name + "-server",
1291 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001292 MinVersion: ver.version,
1293 MaxVersion: ver.version,
1294 CipherSuites: []uint16{suite.id},
1295 Certificates: []Certificate{cert},
1296 PreSharedKey: []byte(psk),
1297 PreSharedKeyIdentity: pskIdentity,
David Benjamin76d8abe2014-08-14 16:25:34 -04001298 },
1299 certFile: certFile,
1300 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001301 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001302 resumeSession: true,
David Benjamin76d8abe2014-08-14 16:25:34 -04001303 })
David Benjamin6fd297b2014-08-11 18:43:38 -04001304
David Benjamin8b8c0062014-11-23 02:47:52 -05001305 if ver.hasDTLS && isDTLSCipher(suite.name) {
David Benjamin6fd297b2014-08-11 18:43:38 -04001306 testCases = append(testCases, testCase{
1307 testType: clientTest,
1308 protocol: dtls,
1309 name: "D" + ver.name + "-" + suite.name + "-client",
1310 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001311 MinVersion: ver.version,
1312 MaxVersion: ver.version,
1313 CipherSuites: []uint16{suite.id},
1314 Certificates: []Certificate{cert},
1315 PreSharedKey: []byte(psk),
1316 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001317 },
David Benjamin48cae082014-10-27 01:06:24 -04001318 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001319 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001320 })
1321 testCases = append(testCases, testCase{
1322 testType: serverTest,
1323 protocol: dtls,
1324 name: "D" + ver.name + "-" + suite.name + "-server",
1325 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001326 MinVersion: ver.version,
1327 MaxVersion: ver.version,
1328 CipherSuites: []uint16{suite.id},
1329 Certificates: []Certificate{cert},
1330 PreSharedKey: []byte(psk),
1331 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001332 },
1333 certFile: certFile,
1334 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001335 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001336 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001337 })
1338 }
Adam Langley95c29f32014-06-20 12:00:00 -07001339 }
1340 }
1341}
1342
1343func addBadECDSASignatureTests() {
1344 for badR := BadValue(1); badR < NumBadValues; badR++ {
1345 for badS := BadValue(1); badS < NumBadValues; badS++ {
David Benjamin025b3d32014-07-01 19:53:04 -04001346 testCases = append(testCases, testCase{
Adam Langley95c29f32014-06-20 12:00:00 -07001347 name: fmt.Sprintf("BadECDSA-%d-%d", badR, badS),
1348 config: Config{
1349 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
1350 Certificates: []Certificate{getECDSACertificate()},
1351 Bugs: ProtocolBugs{
1352 BadECDSAR: badR,
1353 BadECDSAS: badS,
1354 },
1355 },
1356 shouldFail: true,
1357 expectedError: "SIGNATURE",
1358 })
1359 }
1360 }
1361}
1362
Adam Langley80842bd2014-06-20 12:00:00 -07001363func addCBCPaddingTests() {
David Benjamin025b3d32014-07-01 19:53:04 -04001364 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001365 name: "MaxCBCPadding",
1366 config: Config{
1367 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1368 Bugs: ProtocolBugs{
1369 MaxPadding: true,
1370 },
1371 },
1372 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1373 })
David Benjamin025b3d32014-07-01 19:53:04 -04001374 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001375 name: "BadCBCPadding",
1376 config: Config{
1377 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1378 Bugs: ProtocolBugs{
1379 PaddingFirstByteBad: true,
1380 },
1381 },
1382 shouldFail: true,
1383 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1384 })
1385 // OpenSSL previously had an issue where the first byte of padding in
1386 // 255 bytes of padding wasn't checked.
David Benjamin025b3d32014-07-01 19:53:04 -04001387 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001388 name: "BadCBCPadding255",
1389 config: Config{
1390 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1391 Bugs: ProtocolBugs{
1392 MaxPadding: true,
1393 PaddingFirstByteBadIf255: true,
1394 },
1395 },
1396 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1397 shouldFail: true,
1398 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1399 })
1400}
1401
Kenny Root7fdeaf12014-08-05 15:23:37 -07001402func addCBCSplittingTests() {
1403 testCases = append(testCases, testCase{
1404 name: "CBCRecordSplitting",
1405 config: Config{
1406 MaxVersion: VersionTLS10,
1407 MinVersion: VersionTLS10,
1408 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1409 },
1410 messageLen: -1, // read until EOF
1411 flags: []string{
1412 "-async",
1413 "-write-different-record-sizes",
1414 "-cbc-record-splitting",
1415 },
David Benjamina8e3e0e2014-08-06 22:11:10 -04001416 })
1417 testCases = append(testCases, testCase{
Kenny Root7fdeaf12014-08-05 15:23:37 -07001418 name: "CBCRecordSplittingPartialWrite",
1419 config: Config{
1420 MaxVersion: VersionTLS10,
1421 MinVersion: VersionTLS10,
1422 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1423 },
1424 messageLen: -1, // read until EOF
1425 flags: []string{
1426 "-async",
1427 "-write-different-record-sizes",
1428 "-cbc-record-splitting",
1429 "-partial-write",
1430 },
1431 })
1432}
1433
David Benjamin636293b2014-07-08 17:59:18 -04001434func addClientAuthTests() {
David Benjamin407a10c2014-07-16 12:58:59 -04001435 // Add a dummy cert pool to stress certificate authority parsing.
1436 // TODO(davidben): Add tests that those values parse out correctly.
1437 certPool := x509.NewCertPool()
1438 cert, err := x509.ParseCertificate(rsaCertificate.Certificate[0])
1439 if err != nil {
1440 panic(err)
1441 }
1442 certPool.AddCert(cert)
1443
David Benjamin636293b2014-07-08 17:59:18 -04001444 for _, ver := range tlsVersions {
David Benjamin636293b2014-07-08 17:59:18 -04001445 testCases = append(testCases, testCase{
1446 testType: clientTest,
David Benjamin67666e72014-07-12 15:47:52 -04001447 name: ver.name + "-Client-ClientAuth-RSA",
David Benjamin636293b2014-07-08 17:59:18 -04001448 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001449 MinVersion: ver.version,
1450 MaxVersion: ver.version,
1451 ClientAuth: RequireAnyClientCert,
1452 ClientCAs: certPool,
David Benjamin636293b2014-07-08 17:59:18 -04001453 },
1454 flags: []string{
1455 "-cert-file", rsaCertificateFile,
1456 "-key-file", rsaKeyFile,
1457 },
1458 })
1459 testCases = append(testCases, testCase{
David Benjamin67666e72014-07-12 15:47:52 -04001460 testType: serverTest,
1461 name: ver.name + "-Server-ClientAuth-RSA",
1462 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001463 MinVersion: ver.version,
1464 MaxVersion: ver.version,
David Benjamin67666e72014-07-12 15:47:52 -04001465 Certificates: []Certificate{rsaCertificate},
1466 },
1467 flags: []string{"-require-any-client-certificate"},
1468 })
David Benjamine098ec22014-08-27 23:13:20 -04001469 if ver.version != VersionSSL30 {
1470 testCases = append(testCases, testCase{
1471 testType: serverTest,
1472 name: ver.name + "-Server-ClientAuth-ECDSA",
1473 config: Config{
1474 MinVersion: ver.version,
1475 MaxVersion: ver.version,
1476 Certificates: []Certificate{ecdsaCertificate},
1477 },
1478 flags: []string{"-require-any-client-certificate"},
1479 })
1480 testCases = append(testCases, testCase{
1481 testType: clientTest,
1482 name: ver.name + "-Client-ClientAuth-ECDSA",
1483 config: Config{
1484 MinVersion: ver.version,
1485 MaxVersion: ver.version,
1486 ClientAuth: RequireAnyClientCert,
1487 ClientCAs: certPool,
1488 },
1489 flags: []string{
1490 "-cert-file", ecdsaCertificateFile,
1491 "-key-file", ecdsaKeyFile,
1492 },
1493 })
1494 }
David Benjamin636293b2014-07-08 17:59:18 -04001495 }
1496}
1497
Adam Langley75712922014-10-10 16:23:43 -07001498func addExtendedMasterSecretTests() {
1499 const expectEMSFlag = "-expect-extended-master-secret"
1500
1501 for _, with := range []bool{false, true} {
1502 prefix := "No"
1503 var flags []string
1504 if with {
1505 prefix = ""
1506 flags = []string{expectEMSFlag}
1507 }
1508
1509 for _, isClient := range []bool{false, true} {
1510 suffix := "-Server"
1511 testType := serverTest
1512 if isClient {
1513 suffix = "-Client"
1514 testType = clientTest
1515 }
1516
1517 for _, ver := range tlsVersions {
1518 test := testCase{
1519 testType: testType,
1520 name: prefix + "ExtendedMasterSecret-" + ver.name + suffix,
1521 config: Config{
1522 MinVersion: ver.version,
1523 MaxVersion: ver.version,
1524 Bugs: ProtocolBugs{
1525 NoExtendedMasterSecret: !with,
1526 RequireExtendedMasterSecret: with,
1527 },
1528 },
David Benjamin48cae082014-10-27 01:06:24 -04001529 flags: flags,
1530 shouldFail: ver.version == VersionSSL30 && with,
Adam Langley75712922014-10-10 16:23:43 -07001531 }
1532 if test.shouldFail {
1533 test.expectedLocalError = "extended master secret required but not supported by peer"
1534 }
1535 testCases = append(testCases, test)
1536 }
1537 }
1538 }
1539
1540 // When a session is resumed, it should still be aware that its master
1541 // secret was generated via EMS and thus it's safe to use tls-unique.
1542 testCases = append(testCases, testCase{
1543 name: "ExtendedMasterSecret-Resume",
1544 config: Config{
1545 Bugs: ProtocolBugs{
1546 RequireExtendedMasterSecret: true,
1547 },
1548 },
1549 flags: []string{expectEMSFlag},
1550 resumeSession: true,
1551 })
1552}
1553
David Benjamin43ec06f2014-08-05 02:28:57 -04001554// Adds tests that try to cover the range of the handshake state machine, under
1555// various conditions. Some of these are redundant with other tests, but they
1556// only cover the synchronous case.
David Benjamin6fd297b2014-08-11 18:43:38 -04001557func addStateMachineCoverageTests(async, splitHandshake bool, protocol protocol) {
David Benjamin43ec06f2014-08-05 02:28:57 -04001558 var suffix string
1559 var flags []string
1560 var maxHandshakeRecordLength int
David Benjamin6fd297b2014-08-11 18:43:38 -04001561 if protocol == dtls {
1562 suffix = "-DTLS"
1563 }
David Benjamin43ec06f2014-08-05 02:28:57 -04001564 if async {
David Benjamin6fd297b2014-08-11 18:43:38 -04001565 suffix += "-Async"
David Benjamin43ec06f2014-08-05 02:28:57 -04001566 flags = append(flags, "-async")
1567 } else {
David Benjamin6fd297b2014-08-11 18:43:38 -04001568 suffix += "-Sync"
David Benjamin43ec06f2014-08-05 02:28:57 -04001569 }
1570 if splitHandshake {
1571 suffix += "-SplitHandshakeRecords"
David Benjamin98214542014-08-07 18:02:39 -04001572 maxHandshakeRecordLength = 1
David Benjamin43ec06f2014-08-05 02:28:57 -04001573 }
1574
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001575 // Basic handshake, with resumption. Client and server,
1576 // session ID and session ticket.
David Benjamin43ec06f2014-08-05 02:28:57 -04001577 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001578 protocol: protocol,
1579 name: "Basic-Client" + suffix,
David Benjamin43ec06f2014-08-05 02:28:57 -04001580 config: Config{
1581 Bugs: ProtocolBugs{
1582 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1583 },
1584 },
David Benjaminbed9aae2014-08-07 19:13:38 -04001585 flags: flags,
1586 resumeSession: true,
1587 })
1588 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001589 protocol: protocol,
1590 name: "Basic-Client-RenewTicket" + suffix,
David Benjaminbed9aae2014-08-07 19:13:38 -04001591 config: Config{
1592 Bugs: ProtocolBugs{
1593 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1594 RenewTicketOnResume: true,
1595 },
1596 },
1597 flags: flags,
1598 resumeSession: true,
David Benjamin43ec06f2014-08-05 02:28:57 -04001599 })
1600 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001601 protocol: protocol,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001602 name: "Basic-Client-NoTicket" + suffix,
1603 config: Config{
1604 SessionTicketsDisabled: true,
1605 Bugs: ProtocolBugs{
1606 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1607 },
1608 },
1609 flags: flags,
1610 resumeSession: true,
1611 })
1612 testCases = append(testCases, testCase{
1613 protocol: protocol,
David Benjamine0e7d0d2015-02-08 19:33:25 -05001614 name: "Basic-Client-Implicit" + suffix,
1615 config: Config{
1616 Bugs: ProtocolBugs{
1617 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1618 },
1619 },
1620 flags: append(flags, "-implicit-handshake"),
1621 resumeSession: true,
1622 })
1623 testCases = append(testCases, testCase{
1624 protocol: protocol,
David Benjamin43ec06f2014-08-05 02:28:57 -04001625 testType: serverTest,
1626 name: "Basic-Server" + suffix,
1627 config: Config{
1628 Bugs: ProtocolBugs{
1629 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1630 },
1631 },
David Benjaminbed9aae2014-08-07 19:13:38 -04001632 flags: flags,
1633 resumeSession: true,
David Benjamin43ec06f2014-08-05 02:28:57 -04001634 })
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001635 testCases = append(testCases, testCase{
1636 protocol: protocol,
1637 testType: serverTest,
1638 name: "Basic-Server-NoTickets" + suffix,
1639 config: Config{
1640 SessionTicketsDisabled: true,
1641 Bugs: ProtocolBugs{
1642 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1643 },
1644 },
1645 flags: flags,
1646 resumeSession: true,
1647 })
David Benjamine0e7d0d2015-02-08 19:33:25 -05001648 testCases = append(testCases, testCase{
1649 protocol: protocol,
1650 testType: serverTest,
1651 name: "Basic-Server-Implicit" + suffix,
1652 config: Config{
1653 Bugs: ProtocolBugs{
1654 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1655 },
1656 },
1657 flags: append(flags, "-implicit-handshake"),
1658 resumeSession: true,
1659 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001660
David Benjamin6fd297b2014-08-11 18:43:38 -04001661 // TLS client auth.
1662 testCases = append(testCases, testCase{
1663 protocol: protocol,
1664 testType: clientTest,
1665 name: "ClientAuth-Client" + suffix,
1666 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001667 ClientAuth: RequireAnyClientCert,
David Benjamin6fd297b2014-08-11 18:43:38 -04001668 Bugs: ProtocolBugs{
1669 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1670 },
1671 },
1672 flags: append(flags,
1673 "-cert-file", rsaCertificateFile,
1674 "-key-file", rsaKeyFile),
1675 })
1676 testCases = append(testCases, testCase{
1677 protocol: protocol,
1678 testType: serverTest,
1679 name: "ClientAuth-Server" + suffix,
1680 config: Config{
1681 Certificates: []Certificate{rsaCertificate},
1682 },
1683 flags: append(flags, "-require-any-client-certificate"),
1684 })
1685
David Benjamin43ec06f2014-08-05 02:28:57 -04001686 // No session ticket support; server doesn't send NewSessionTicket.
1687 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001688 protocol: protocol,
1689 name: "SessionTicketsDisabled-Client" + suffix,
David Benjamin43ec06f2014-08-05 02:28:57 -04001690 config: Config{
1691 SessionTicketsDisabled: true,
1692 Bugs: ProtocolBugs{
1693 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1694 },
1695 },
1696 flags: flags,
1697 })
1698 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001699 protocol: protocol,
David Benjamin43ec06f2014-08-05 02:28:57 -04001700 testType: serverTest,
1701 name: "SessionTicketsDisabled-Server" + suffix,
1702 config: Config{
1703 SessionTicketsDisabled: true,
1704 Bugs: ProtocolBugs{
1705 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1706 },
1707 },
1708 flags: flags,
1709 })
1710
David Benjamin48cae082014-10-27 01:06:24 -04001711 // Skip ServerKeyExchange in PSK key exchange if there's no
1712 // identity hint.
1713 testCases = append(testCases, testCase{
1714 protocol: protocol,
1715 name: "EmptyPSKHint-Client" + suffix,
1716 config: Config{
1717 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
1718 PreSharedKey: []byte("secret"),
1719 Bugs: ProtocolBugs{
1720 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1721 },
1722 },
1723 flags: append(flags, "-psk", "secret"),
1724 })
1725 testCases = append(testCases, testCase{
1726 protocol: protocol,
1727 testType: serverTest,
1728 name: "EmptyPSKHint-Server" + suffix,
1729 config: Config{
1730 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
1731 PreSharedKey: []byte("secret"),
1732 Bugs: ProtocolBugs{
1733 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1734 },
1735 },
1736 flags: append(flags, "-psk", "secret"),
1737 })
1738
David Benjamin6fd297b2014-08-11 18:43:38 -04001739 if protocol == tls {
1740 // NPN on client and server; results in post-handshake message.
1741 testCases = append(testCases, testCase{
1742 protocol: protocol,
1743 name: "NPN-Client" + suffix,
1744 config: Config{
David Benjaminae2888f2014-09-06 12:58:58 -04001745 NextProtos: []string{"foo"},
David Benjamin6fd297b2014-08-11 18:43:38 -04001746 Bugs: ProtocolBugs{
1747 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1748 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001749 },
David Benjaminfc7b0862014-09-06 13:21:53 -04001750 flags: append(flags, "-select-next-proto", "foo"),
1751 expectedNextProto: "foo",
1752 expectedNextProtoType: npn,
David Benjamin6fd297b2014-08-11 18:43:38 -04001753 })
1754 testCases = append(testCases, testCase{
1755 protocol: protocol,
1756 testType: serverTest,
1757 name: "NPN-Server" + suffix,
1758 config: Config{
1759 NextProtos: []string{"bar"},
1760 Bugs: ProtocolBugs{
1761 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1762 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001763 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001764 flags: append(flags,
1765 "-advertise-npn", "\x03foo\x03bar\x03baz",
1766 "-expect-next-proto", "bar"),
David Benjaminfc7b0862014-09-06 13:21:53 -04001767 expectedNextProto: "bar",
1768 expectedNextProtoType: npn,
David Benjamin6fd297b2014-08-11 18:43:38 -04001769 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001770
David Benjamin195dc782015-02-19 13:27:05 -05001771 // TODO(davidben): Add tests for when False Start doesn't trigger.
1772
David Benjamin6fd297b2014-08-11 18:43:38 -04001773 // Client does False Start and negotiates NPN.
1774 testCases = append(testCases, testCase{
1775 protocol: protocol,
1776 name: "FalseStart" + suffix,
1777 config: Config{
1778 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1779 NextProtos: []string{"foo"},
1780 Bugs: ProtocolBugs{
David Benjamine58c4f52014-08-24 03:47:07 -04001781 ExpectFalseStart: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001782 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1783 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001784 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001785 flags: append(flags,
1786 "-false-start",
1787 "-select-next-proto", "foo"),
David Benjamine58c4f52014-08-24 03:47:07 -04001788 shimWritesFirst: true,
1789 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001790 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001791
David Benjaminae2888f2014-09-06 12:58:58 -04001792 // Client does False Start and negotiates ALPN.
1793 testCases = append(testCases, testCase{
1794 protocol: protocol,
1795 name: "FalseStart-ALPN" + suffix,
1796 config: Config{
1797 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1798 NextProtos: []string{"foo"},
1799 Bugs: ProtocolBugs{
1800 ExpectFalseStart: true,
1801 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1802 },
1803 },
1804 flags: append(flags,
1805 "-false-start",
1806 "-advertise-alpn", "\x03foo"),
1807 shimWritesFirst: true,
1808 resumeSession: true,
1809 })
1810
David Benjamin931ab342015-02-08 19:46:57 -05001811 // Client does False Start but doesn't explicitly call
1812 // SSL_connect.
1813 testCases = append(testCases, testCase{
1814 protocol: protocol,
1815 name: "FalseStart-Implicit" + suffix,
1816 config: Config{
1817 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1818 NextProtos: []string{"foo"},
1819 Bugs: ProtocolBugs{
1820 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1821 },
1822 },
1823 flags: append(flags,
1824 "-implicit-handshake",
1825 "-false-start",
1826 "-advertise-alpn", "\x03foo"),
1827 })
1828
David Benjamin6fd297b2014-08-11 18:43:38 -04001829 // False Start without session tickets.
1830 testCases = append(testCases, testCase{
David Benjamin5f237bc2015-02-11 17:14:15 -05001831 name: "FalseStart-SessionTicketsDisabled" + suffix,
David Benjamin6fd297b2014-08-11 18:43:38 -04001832 config: Config{
1833 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1834 NextProtos: []string{"foo"},
1835 SessionTicketsDisabled: true,
David Benjamin4e99c522014-08-24 01:45:30 -04001836 Bugs: ProtocolBugs{
David Benjamine58c4f52014-08-24 03:47:07 -04001837 ExpectFalseStart: true,
David Benjamin4e99c522014-08-24 01:45:30 -04001838 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1839 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001840 },
David Benjamin4e99c522014-08-24 01:45:30 -04001841 flags: append(flags,
David Benjamin6fd297b2014-08-11 18:43:38 -04001842 "-false-start",
1843 "-select-next-proto", "foo",
David Benjamin4e99c522014-08-24 01:45:30 -04001844 ),
David Benjamine58c4f52014-08-24 03:47:07 -04001845 shimWritesFirst: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001846 })
David Benjamin1e7f8d72014-08-08 12:27:04 -04001847
David Benjamina08e49d2014-08-24 01:46:07 -04001848 // Server parses a V2ClientHello.
David Benjamin6fd297b2014-08-11 18:43:38 -04001849 testCases = append(testCases, testCase{
1850 protocol: protocol,
1851 testType: serverTest,
1852 name: "SendV2ClientHello" + suffix,
1853 config: Config{
1854 // Choose a cipher suite that does not involve
1855 // elliptic curves, so no extensions are
1856 // involved.
1857 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
1858 Bugs: ProtocolBugs{
1859 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1860 SendV2ClientHello: true,
1861 },
David Benjamin1e7f8d72014-08-08 12:27:04 -04001862 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001863 flags: flags,
1864 })
David Benjamina08e49d2014-08-24 01:46:07 -04001865
1866 // Client sends a Channel ID.
1867 testCases = append(testCases, testCase{
1868 protocol: protocol,
1869 name: "ChannelID-Client" + suffix,
1870 config: Config{
1871 RequestChannelID: true,
1872 Bugs: ProtocolBugs{
1873 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1874 },
1875 },
1876 flags: append(flags,
1877 "-send-channel-id", channelIDKeyFile,
1878 ),
1879 resumeSession: true,
1880 expectChannelID: true,
1881 })
1882
1883 // Server accepts a Channel ID.
1884 testCases = append(testCases, testCase{
1885 protocol: protocol,
1886 testType: serverTest,
1887 name: "ChannelID-Server" + suffix,
1888 config: Config{
1889 ChannelID: channelIDKey,
1890 Bugs: ProtocolBugs{
1891 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1892 },
1893 },
1894 flags: append(flags,
1895 "-expect-channel-id",
1896 base64.StdEncoding.EncodeToString(channelIDBytes),
1897 ),
1898 resumeSession: true,
1899 expectChannelID: true,
1900 })
David Benjamin6fd297b2014-08-11 18:43:38 -04001901 } else {
1902 testCases = append(testCases, testCase{
1903 protocol: protocol,
1904 name: "SkipHelloVerifyRequest" + suffix,
1905 config: Config{
1906 Bugs: ProtocolBugs{
1907 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1908 SkipHelloVerifyRequest: true,
1909 },
1910 },
1911 flags: flags,
1912 })
David Benjamin6fd297b2014-08-11 18:43:38 -04001913 }
David Benjamin43ec06f2014-08-05 02:28:57 -04001914}
1915
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001916func addVersionNegotiationTests() {
1917 for i, shimVers := range tlsVersions {
1918 // Assemble flags to disable all newer versions on the shim.
1919 var flags []string
1920 for _, vers := range tlsVersions[i+1:] {
1921 flags = append(flags, vers.flag)
1922 }
1923
1924 for _, runnerVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05001925 protocols := []protocol{tls}
1926 if runnerVers.hasDTLS && shimVers.hasDTLS {
1927 protocols = append(protocols, dtls)
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001928 }
David Benjamin8b8c0062014-11-23 02:47:52 -05001929 for _, protocol := range protocols {
1930 expectedVersion := shimVers.version
1931 if runnerVers.version < shimVers.version {
1932 expectedVersion = runnerVers.version
1933 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001934
David Benjamin8b8c0062014-11-23 02:47:52 -05001935 suffix := shimVers.name + "-" + runnerVers.name
1936 if protocol == dtls {
1937 suffix += "-DTLS"
1938 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001939
David Benjamin1eb367c2014-12-12 18:17:51 -05001940 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
1941
David Benjamin1e29a6b2014-12-10 02:27:24 -05001942 clientVers := shimVers.version
1943 if clientVers > VersionTLS10 {
1944 clientVers = VersionTLS10
1945 }
David Benjamin8b8c0062014-11-23 02:47:52 -05001946 testCases = append(testCases, testCase{
1947 protocol: protocol,
1948 testType: clientTest,
1949 name: "VersionNegotiation-Client-" + suffix,
1950 config: Config{
1951 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05001952 Bugs: ProtocolBugs{
1953 ExpectInitialRecordVersion: clientVers,
1954 },
David Benjamin8b8c0062014-11-23 02:47:52 -05001955 },
1956 flags: flags,
1957 expectedVersion: expectedVersion,
1958 })
David Benjamin1eb367c2014-12-12 18:17:51 -05001959 testCases = append(testCases, testCase{
1960 protocol: protocol,
1961 testType: clientTest,
1962 name: "VersionNegotiation-Client2-" + suffix,
1963 config: Config{
1964 MaxVersion: runnerVers.version,
1965 Bugs: ProtocolBugs{
1966 ExpectInitialRecordVersion: clientVers,
1967 },
1968 },
1969 flags: []string{"-max-version", shimVersFlag},
1970 expectedVersion: expectedVersion,
1971 })
David Benjamin8b8c0062014-11-23 02:47:52 -05001972
1973 testCases = append(testCases, testCase{
1974 protocol: protocol,
1975 testType: serverTest,
1976 name: "VersionNegotiation-Server-" + suffix,
1977 config: Config{
1978 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05001979 Bugs: ProtocolBugs{
1980 ExpectInitialRecordVersion: expectedVersion,
1981 },
David Benjamin8b8c0062014-11-23 02:47:52 -05001982 },
1983 flags: flags,
1984 expectedVersion: expectedVersion,
1985 })
David Benjamin1eb367c2014-12-12 18:17:51 -05001986 testCases = append(testCases, testCase{
1987 protocol: protocol,
1988 testType: serverTest,
1989 name: "VersionNegotiation-Server2-" + suffix,
1990 config: Config{
1991 MaxVersion: runnerVers.version,
1992 Bugs: ProtocolBugs{
1993 ExpectInitialRecordVersion: expectedVersion,
1994 },
1995 },
1996 flags: []string{"-max-version", shimVersFlag},
1997 expectedVersion: expectedVersion,
1998 })
David Benjamin8b8c0062014-11-23 02:47:52 -05001999 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002000 }
2001 }
2002}
2003
David Benjaminaccb4542014-12-12 23:44:33 -05002004func addMinimumVersionTests() {
2005 for i, shimVers := range tlsVersions {
2006 // Assemble flags to disable all older versions on the shim.
2007 var flags []string
2008 for _, vers := range tlsVersions[:i] {
2009 flags = append(flags, vers.flag)
2010 }
2011
2012 for _, runnerVers := range tlsVersions {
2013 protocols := []protocol{tls}
2014 if runnerVers.hasDTLS && shimVers.hasDTLS {
2015 protocols = append(protocols, dtls)
2016 }
2017 for _, protocol := range protocols {
2018 suffix := shimVers.name + "-" + runnerVers.name
2019 if protocol == dtls {
2020 suffix += "-DTLS"
2021 }
2022 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
2023
David Benjaminaccb4542014-12-12 23:44:33 -05002024 var expectedVersion uint16
2025 var shouldFail bool
2026 var expectedError string
David Benjamin87909c02014-12-13 01:55:01 -05002027 var expectedLocalError string
David Benjaminaccb4542014-12-12 23:44:33 -05002028 if runnerVers.version >= shimVers.version {
2029 expectedVersion = runnerVers.version
2030 } else {
2031 shouldFail = true
2032 expectedError = ":UNSUPPORTED_PROTOCOL:"
David Benjamin87909c02014-12-13 01:55:01 -05002033 if runnerVers.version > VersionSSL30 {
2034 expectedLocalError = "remote error: protocol version not supported"
2035 } else {
2036 expectedLocalError = "remote error: handshake failure"
2037 }
David Benjaminaccb4542014-12-12 23:44:33 -05002038 }
2039
2040 testCases = append(testCases, testCase{
2041 protocol: protocol,
2042 testType: clientTest,
2043 name: "MinimumVersion-Client-" + suffix,
2044 config: Config{
2045 MaxVersion: runnerVers.version,
2046 },
David Benjamin87909c02014-12-13 01:55:01 -05002047 flags: flags,
2048 expectedVersion: expectedVersion,
2049 shouldFail: shouldFail,
2050 expectedError: expectedError,
2051 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002052 })
2053 testCases = append(testCases, testCase{
2054 protocol: protocol,
2055 testType: clientTest,
2056 name: "MinimumVersion-Client2-" + suffix,
2057 config: Config{
2058 MaxVersion: runnerVers.version,
2059 },
David Benjamin87909c02014-12-13 01:55:01 -05002060 flags: []string{"-min-version", shimVersFlag},
2061 expectedVersion: expectedVersion,
2062 shouldFail: shouldFail,
2063 expectedError: expectedError,
2064 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002065 })
2066
2067 testCases = append(testCases, testCase{
2068 protocol: protocol,
2069 testType: serverTest,
2070 name: "MinimumVersion-Server-" + suffix,
2071 config: Config{
2072 MaxVersion: runnerVers.version,
2073 },
David Benjamin87909c02014-12-13 01:55:01 -05002074 flags: flags,
2075 expectedVersion: expectedVersion,
2076 shouldFail: shouldFail,
2077 expectedError: expectedError,
2078 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002079 })
2080 testCases = append(testCases, testCase{
2081 protocol: protocol,
2082 testType: serverTest,
2083 name: "MinimumVersion-Server2-" + suffix,
2084 config: Config{
2085 MaxVersion: runnerVers.version,
2086 },
David Benjamin87909c02014-12-13 01:55:01 -05002087 flags: []string{"-min-version", shimVersFlag},
2088 expectedVersion: expectedVersion,
2089 shouldFail: shouldFail,
2090 expectedError: expectedError,
2091 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002092 })
2093 }
2094 }
2095 }
2096}
2097
David Benjamin5c24a1d2014-08-31 00:59:27 -04002098func addD5BugTests() {
2099 testCases = append(testCases, testCase{
2100 testType: serverTest,
2101 name: "D5Bug-NoQuirk-Reject",
2102 config: Config{
2103 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2104 Bugs: ProtocolBugs{
2105 SSL3RSAKeyExchange: true,
2106 },
2107 },
2108 shouldFail: true,
2109 expectedError: ":TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG:",
2110 })
2111 testCases = append(testCases, testCase{
2112 testType: serverTest,
2113 name: "D5Bug-Quirk-Normal",
2114 config: Config{
2115 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2116 },
2117 flags: []string{"-tls-d5-bug"},
2118 })
2119 testCases = append(testCases, testCase{
2120 testType: serverTest,
2121 name: "D5Bug-Quirk-Bug",
2122 config: Config{
2123 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2124 Bugs: ProtocolBugs{
2125 SSL3RSAKeyExchange: true,
2126 },
2127 },
2128 flags: []string{"-tls-d5-bug"},
2129 })
2130}
2131
David Benjamine78bfde2014-09-06 12:45:15 -04002132func addExtensionTests() {
2133 testCases = append(testCases, testCase{
2134 testType: clientTest,
2135 name: "DuplicateExtensionClient",
2136 config: Config{
2137 Bugs: ProtocolBugs{
2138 DuplicateExtension: true,
2139 },
2140 },
2141 shouldFail: true,
2142 expectedLocalError: "remote error: error decoding message",
2143 })
2144 testCases = append(testCases, testCase{
2145 testType: serverTest,
2146 name: "DuplicateExtensionServer",
2147 config: Config{
2148 Bugs: ProtocolBugs{
2149 DuplicateExtension: true,
2150 },
2151 },
2152 shouldFail: true,
2153 expectedLocalError: "remote error: error decoding message",
2154 })
2155 testCases = append(testCases, testCase{
2156 testType: clientTest,
2157 name: "ServerNameExtensionClient",
2158 config: Config{
2159 Bugs: ProtocolBugs{
2160 ExpectServerName: "example.com",
2161 },
2162 },
2163 flags: []string{"-host-name", "example.com"},
2164 })
2165 testCases = append(testCases, testCase{
2166 testType: clientTest,
David Benjamin5f237bc2015-02-11 17:14:15 -05002167 name: "ServerNameExtensionClientMismatch",
David Benjamine78bfde2014-09-06 12:45:15 -04002168 config: Config{
2169 Bugs: ProtocolBugs{
2170 ExpectServerName: "mismatch.com",
2171 },
2172 },
2173 flags: []string{"-host-name", "example.com"},
2174 shouldFail: true,
2175 expectedLocalError: "tls: unexpected server name",
2176 })
2177 testCases = append(testCases, testCase{
2178 testType: clientTest,
David Benjamin5f237bc2015-02-11 17:14:15 -05002179 name: "ServerNameExtensionClientMissing",
David Benjamine78bfde2014-09-06 12:45:15 -04002180 config: Config{
2181 Bugs: ProtocolBugs{
2182 ExpectServerName: "missing.com",
2183 },
2184 },
2185 shouldFail: true,
2186 expectedLocalError: "tls: unexpected server name",
2187 })
2188 testCases = append(testCases, testCase{
2189 testType: serverTest,
2190 name: "ServerNameExtensionServer",
2191 config: Config{
2192 ServerName: "example.com",
2193 },
2194 flags: []string{"-expect-server-name", "example.com"},
2195 resumeSession: true,
2196 })
David Benjaminae2888f2014-09-06 12:58:58 -04002197 testCases = append(testCases, testCase{
2198 testType: clientTest,
2199 name: "ALPNClient",
2200 config: Config{
2201 NextProtos: []string{"foo"},
2202 },
2203 flags: []string{
2204 "-advertise-alpn", "\x03foo\x03bar\x03baz",
2205 "-expect-alpn", "foo",
2206 },
David Benjaminfc7b0862014-09-06 13:21:53 -04002207 expectedNextProto: "foo",
2208 expectedNextProtoType: alpn,
2209 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04002210 })
2211 testCases = append(testCases, testCase{
2212 testType: serverTest,
2213 name: "ALPNServer",
2214 config: Config{
2215 NextProtos: []string{"foo", "bar", "baz"},
2216 },
2217 flags: []string{
2218 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2219 "-select-alpn", "foo",
2220 },
David Benjaminfc7b0862014-09-06 13:21:53 -04002221 expectedNextProto: "foo",
2222 expectedNextProtoType: alpn,
2223 resumeSession: true,
2224 })
2225 // Test that the server prefers ALPN over NPN.
2226 testCases = append(testCases, testCase{
2227 testType: serverTest,
2228 name: "ALPNServer-Preferred",
2229 config: Config{
2230 NextProtos: []string{"foo", "bar", "baz"},
2231 },
2232 flags: []string{
2233 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2234 "-select-alpn", "foo",
2235 "-advertise-npn", "\x03foo\x03bar\x03baz",
2236 },
2237 expectedNextProto: "foo",
2238 expectedNextProtoType: alpn,
2239 resumeSession: true,
2240 })
2241 testCases = append(testCases, testCase{
2242 testType: serverTest,
2243 name: "ALPNServer-Preferred-Swapped",
2244 config: Config{
2245 NextProtos: []string{"foo", "bar", "baz"},
2246 Bugs: ProtocolBugs{
2247 SwapNPNAndALPN: true,
2248 },
2249 },
2250 flags: []string{
2251 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2252 "-select-alpn", "foo",
2253 "-advertise-npn", "\x03foo\x03bar\x03baz",
2254 },
2255 expectedNextProto: "foo",
2256 expectedNextProtoType: alpn,
2257 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04002258 })
Adam Langley38311732014-10-16 19:04:35 -07002259 // Resume with a corrupt ticket.
2260 testCases = append(testCases, testCase{
2261 testType: serverTest,
2262 name: "CorruptTicket",
2263 config: Config{
2264 Bugs: ProtocolBugs{
2265 CorruptTicket: true,
2266 },
2267 },
2268 resumeSession: true,
2269 flags: []string{"-expect-session-miss"},
2270 })
2271 // Resume with an oversized session id.
2272 testCases = append(testCases, testCase{
2273 testType: serverTest,
2274 name: "OversizedSessionId",
2275 config: Config{
2276 Bugs: ProtocolBugs{
2277 OversizedSessionId: true,
2278 },
2279 },
2280 resumeSession: true,
Adam Langley75712922014-10-10 16:23:43 -07002281 shouldFail: true,
Adam Langley38311732014-10-16 19:04:35 -07002282 expectedError: ":DECODE_ERROR:",
2283 })
David Benjaminca6c8262014-11-15 19:06:08 -05002284 // Basic DTLS-SRTP tests. Include fake profiles to ensure they
2285 // are ignored.
2286 testCases = append(testCases, testCase{
2287 protocol: dtls,
2288 name: "SRTP-Client",
2289 config: Config{
2290 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
2291 },
2292 flags: []string{
2293 "-srtp-profiles",
2294 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2295 },
2296 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2297 })
2298 testCases = append(testCases, testCase{
2299 protocol: dtls,
2300 testType: serverTest,
2301 name: "SRTP-Server",
2302 config: Config{
2303 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
2304 },
2305 flags: []string{
2306 "-srtp-profiles",
2307 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2308 },
2309 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2310 })
2311 // Test that the MKI is ignored.
2312 testCases = append(testCases, testCase{
2313 protocol: dtls,
2314 testType: serverTest,
2315 name: "SRTP-Server-IgnoreMKI",
2316 config: Config{
2317 SRTPProtectionProfiles: []uint16{SRTP_AES128_CM_HMAC_SHA1_80},
2318 Bugs: ProtocolBugs{
2319 SRTPMasterKeyIdentifer: "bogus",
2320 },
2321 },
2322 flags: []string{
2323 "-srtp-profiles",
2324 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2325 },
2326 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2327 })
2328 // Test that SRTP isn't negotiated on the server if there were
2329 // no matching profiles.
2330 testCases = append(testCases, testCase{
2331 protocol: dtls,
2332 testType: serverTest,
2333 name: "SRTP-Server-NoMatch",
2334 config: Config{
2335 SRTPProtectionProfiles: []uint16{100, 101, 102},
2336 },
2337 flags: []string{
2338 "-srtp-profiles",
2339 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2340 },
2341 expectedSRTPProtectionProfile: 0,
2342 })
2343 // Test that the server returning an invalid SRTP profile is
2344 // flagged as an error by the client.
2345 testCases = append(testCases, testCase{
2346 protocol: dtls,
2347 name: "SRTP-Client-NoMatch",
2348 config: Config{
2349 Bugs: ProtocolBugs{
2350 SendSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_32,
2351 },
2352 },
2353 flags: []string{
2354 "-srtp-profiles",
2355 "SRTP_AES128_CM_SHA1_80",
2356 },
2357 shouldFail: true,
2358 expectedError: ":BAD_SRTP_PROTECTION_PROFILE_LIST:",
2359 })
David Benjamin61f95272014-11-25 01:55:35 -05002360 // Test OCSP stapling and SCT list.
2361 testCases = append(testCases, testCase{
2362 name: "OCSPStapling",
2363 flags: []string{
2364 "-enable-ocsp-stapling",
2365 "-expect-ocsp-response",
2366 base64.StdEncoding.EncodeToString(testOCSPResponse),
2367 },
2368 })
2369 testCases = append(testCases, testCase{
2370 name: "SignedCertificateTimestampList",
2371 flags: []string{
2372 "-enable-signed-cert-timestamps",
2373 "-expect-signed-cert-timestamps",
2374 base64.StdEncoding.EncodeToString(testSCTList),
2375 },
2376 })
David Benjamine78bfde2014-09-06 12:45:15 -04002377}
2378
David Benjamin01fe8202014-09-24 15:21:44 -04002379func addResumptionVersionTests() {
David Benjamin01fe8202014-09-24 15:21:44 -04002380 for _, sessionVers := range tlsVersions {
David Benjamin01fe8202014-09-24 15:21:44 -04002381 for _, resumeVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05002382 protocols := []protocol{tls}
2383 if sessionVers.hasDTLS && resumeVers.hasDTLS {
2384 protocols = append(protocols, dtls)
David Benjaminbdf5e722014-11-11 00:52:15 -05002385 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002386 for _, protocol := range protocols {
2387 suffix := "-" + sessionVers.name + "-" + resumeVers.name
2388 if protocol == dtls {
2389 suffix += "-DTLS"
2390 }
2391
2392 testCases = append(testCases, testCase{
2393 protocol: protocol,
2394 name: "Resume-Client" + suffix,
2395 resumeSession: true,
2396 config: Config{
2397 MaxVersion: sessionVers.version,
2398 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2399 Bugs: ProtocolBugs{
2400 AllowSessionVersionMismatch: true,
2401 },
2402 },
2403 expectedVersion: sessionVers.version,
2404 resumeConfig: &Config{
2405 MaxVersion: resumeVers.version,
2406 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2407 Bugs: ProtocolBugs{
2408 AllowSessionVersionMismatch: true,
2409 },
2410 },
2411 expectedResumeVersion: resumeVers.version,
2412 })
2413
2414 testCases = append(testCases, testCase{
2415 protocol: protocol,
2416 name: "Resume-Client-NoResume" + suffix,
2417 flags: []string{"-expect-session-miss"},
2418 resumeSession: true,
2419 config: Config{
2420 MaxVersion: sessionVers.version,
2421 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2422 },
2423 expectedVersion: sessionVers.version,
2424 resumeConfig: &Config{
2425 MaxVersion: resumeVers.version,
2426 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2427 },
2428 newSessionsOnResume: true,
2429 expectedResumeVersion: resumeVers.version,
2430 })
2431
2432 var flags []string
2433 if sessionVers.version != resumeVers.version {
2434 flags = append(flags, "-expect-session-miss")
2435 }
2436 testCases = append(testCases, testCase{
2437 protocol: protocol,
2438 testType: serverTest,
2439 name: "Resume-Server" + suffix,
2440 flags: flags,
2441 resumeSession: true,
2442 config: Config{
2443 MaxVersion: sessionVers.version,
2444 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2445 },
2446 expectedVersion: sessionVers.version,
2447 resumeConfig: &Config{
2448 MaxVersion: resumeVers.version,
2449 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2450 },
2451 expectedResumeVersion: resumeVers.version,
2452 })
2453 }
David Benjamin01fe8202014-09-24 15:21:44 -04002454 }
2455 }
2456}
2457
Adam Langley2ae77d22014-10-28 17:29:33 -07002458func addRenegotiationTests() {
2459 testCases = append(testCases, testCase{
2460 testType: serverTest,
2461 name: "Renegotiate-Server",
2462 flags: []string{"-renegotiate"},
2463 shimWritesFirst: true,
2464 })
2465 testCases = append(testCases, testCase{
2466 testType: serverTest,
2467 name: "Renegotiate-Server-EmptyExt",
2468 config: Config{
2469 Bugs: ProtocolBugs{
2470 EmptyRenegotiationInfo: true,
2471 },
2472 },
2473 flags: []string{"-renegotiate"},
2474 shimWritesFirst: true,
2475 shouldFail: true,
2476 expectedError: ":RENEGOTIATION_MISMATCH:",
2477 })
2478 testCases = append(testCases, testCase{
2479 testType: serverTest,
2480 name: "Renegotiate-Server-BadExt",
2481 config: Config{
2482 Bugs: ProtocolBugs{
2483 BadRenegotiationInfo: true,
2484 },
2485 },
2486 flags: []string{"-renegotiate"},
2487 shimWritesFirst: true,
2488 shouldFail: true,
2489 expectedError: ":RENEGOTIATION_MISMATCH:",
2490 })
David Benjaminca6554b2014-11-08 12:31:52 -05002491 testCases = append(testCases, testCase{
2492 testType: serverTest,
2493 name: "Renegotiate-Server-ClientInitiated",
2494 renegotiate: true,
2495 })
2496 testCases = append(testCases, testCase{
2497 testType: serverTest,
2498 name: "Renegotiate-Server-ClientInitiated-NoExt",
2499 renegotiate: true,
2500 config: Config{
2501 Bugs: ProtocolBugs{
2502 NoRenegotiationInfo: true,
2503 },
2504 },
2505 shouldFail: true,
2506 expectedError: ":UNSAFE_LEGACY_RENEGOTIATION_DISABLED:",
2507 })
2508 testCases = append(testCases, testCase{
2509 testType: serverTest,
2510 name: "Renegotiate-Server-ClientInitiated-NoExt-Allowed",
2511 renegotiate: true,
2512 config: Config{
2513 Bugs: ProtocolBugs{
2514 NoRenegotiationInfo: true,
2515 },
2516 },
2517 flags: []string{"-allow-unsafe-legacy-renegotiation"},
2518 })
Adam Langley2ae77d22014-10-28 17:29:33 -07002519 // TODO(agl): test the renegotiation info SCSV.
Adam Langleycf2d4f42014-10-28 19:06:14 -07002520 testCases = append(testCases, testCase{
2521 name: "Renegotiate-Client",
2522 renegotiate: true,
2523 })
2524 testCases = append(testCases, testCase{
2525 name: "Renegotiate-Client-EmptyExt",
2526 renegotiate: true,
2527 config: Config{
2528 Bugs: ProtocolBugs{
2529 EmptyRenegotiationInfo: true,
2530 },
2531 },
2532 shouldFail: true,
2533 expectedError: ":RENEGOTIATION_MISMATCH:",
2534 })
2535 testCases = append(testCases, testCase{
2536 name: "Renegotiate-Client-BadExt",
2537 renegotiate: true,
2538 config: Config{
2539 Bugs: ProtocolBugs{
2540 BadRenegotiationInfo: true,
2541 },
2542 },
2543 shouldFail: true,
2544 expectedError: ":RENEGOTIATION_MISMATCH:",
2545 })
2546 testCases = append(testCases, testCase{
2547 name: "Renegotiate-Client-SwitchCiphers",
2548 renegotiate: true,
2549 config: Config{
2550 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2551 },
2552 renegotiateCiphers: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2553 })
2554 testCases = append(testCases, testCase{
2555 name: "Renegotiate-Client-SwitchCiphers2",
2556 renegotiate: true,
2557 config: Config{
2558 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2559 },
2560 renegotiateCiphers: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2561 })
David Benjaminc44b1df2014-11-23 12:11:01 -05002562 testCases = append(testCases, testCase{
2563 name: "Renegotiate-SameClientVersion",
2564 renegotiate: true,
2565 config: Config{
2566 MaxVersion: VersionTLS10,
2567 Bugs: ProtocolBugs{
2568 RequireSameRenegoClientVersion: true,
2569 },
2570 },
2571 })
Adam Langley2ae77d22014-10-28 17:29:33 -07002572}
2573
David Benjamin5e961c12014-11-07 01:48:35 -05002574func addDTLSReplayTests() {
2575 // Test that sequence number replays are detected.
2576 testCases = append(testCases, testCase{
2577 protocol: dtls,
2578 name: "DTLS-Replay",
2579 replayWrites: true,
2580 })
2581
2582 // Test the outgoing sequence number skipping by values larger
2583 // than the retransmit window.
2584 testCases = append(testCases, testCase{
2585 protocol: dtls,
2586 name: "DTLS-Replay-LargeGaps",
2587 config: Config{
2588 Bugs: ProtocolBugs{
2589 SequenceNumberIncrement: 127,
2590 },
2591 },
2592 replayWrites: true,
2593 })
2594}
2595
Feng Lu41aa3252014-11-21 22:47:56 -08002596func addFastRadioPaddingTests() {
David Benjamin1e29a6b2014-12-10 02:27:24 -05002597 testCases = append(testCases, testCase{
2598 protocol: tls,
2599 name: "FastRadio-Padding",
Feng Lu41aa3252014-11-21 22:47:56 -08002600 config: Config{
2601 Bugs: ProtocolBugs{
2602 RequireFastradioPadding: true,
2603 },
2604 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05002605 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08002606 })
David Benjamin1e29a6b2014-12-10 02:27:24 -05002607 testCases = append(testCases, testCase{
2608 protocol: dtls,
David Benjamin5f237bc2015-02-11 17:14:15 -05002609 name: "FastRadio-Padding-DTLS",
Feng Lu41aa3252014-11-21 22:47:56 -08002610 config: Config{
2611 Bugs: ProtocolBugs{
2612 RequireFastradioPadding: true,
2613 },
2614 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05002615 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08002616 })
2617}
2618
David Benjamin000800a2014-11-14 01:43:59 -05002619var testHashes = []struct {
2620 name string
2621 id uint8
2622}{
2623 {"SHA1", hashSHA1},
2624 {"SHA224", hashSHA224},
2625 {"SHA256", hashSHA256},
2626 {"SHA384", hashSHA384},
2627 {"SHA512", hashSHA512},
2628}
2629
2630func addSigningHashTests() {
2631 // Make sure each hash works. Include some fake hashes in the list and
2632 // ensure they're ignored.
2633 for _, hash := range testHashes {
2634 testCases = append(testCases, testCase{
2635 name: "SigningHash-ClientAuth-" + hash.name,
2636 config: Config{
2637 ClientAuth: RequireAnyClientCert,
2638 SignatureAndHashes: []signatureAndHash{
2639 {signatureRSA, 42},
2640 {signatureRSA, hash.id},
2641 {signatureRSA, 255},
2642 },
2643 },
2644 flags: []string{
2645 "-cert-file", rsaCertificateFile,
2646 "-key-file", rsaKeyFile,
2647 },
2648 })
2649
2650 testCases = append(testCases, testCase{
2651 testType: serverTest,
2652 name: "SigningHash-ServerKeyExchange-Sign-" + hash.name,
2653 config: Config{
2654 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2655 SignatureAndHashes: []signatureAndHash{
2656 {signatureRSA, 42},
2657 {signatureRSA, hash.id},
2658 {signatureRSA, 255},
2659 },
2660 },
2661 })
2662 }
2663
2664 // Test that hash resolution takes the signature type into account.
2665 testCases = append(testCases, testCase{
2666 name: "SigningHash-ClientAuth-SignatureType",
2667 config: Config{
2668 ClientAuth: RequireAnyClientCert,
2669 SignatureAndHashes: []signatureAndHash{
2670 {signatureECDSA, hashSHA512},
2671 {signatureRSA, hashSHA384},
2672 {signatureECDSA, hashSHA1},
2673 },
2674 },
2675 flags: []string{
2676 "-cert-file", rsaCertificateFile,
2677 "-key-file", rsaKeyFile,
2678 },
2679 })
2680
2681 testCases = append(testCases, testCase{
2682 testType: serverTest,
2683 name: "SigningHash-ServerKeyExchange-SignatureType",
2684 config: Config{
2685 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2686 SignatureAndHashes: []signatureAndHash{
2687 {signatureECDSA, hashSHA512},
2688 {signatureRSA, hashSHA384},
2689 {signatureECDSA, hashSHA1},
2690 },
2691 },
2692 })
2693
2694 // Test that, if the list is missing, the peer falls back to SHA-1.
2695 testCases = append(testCases, testCase{
2696 name: "SigningHash-ClientAuth-Fallback",
2697 config: Config{
2698 ClientAuth: RequireAnyClientCert,
2699 SignatureAndHashes: []signatureAndHash{
2700 {signatureRSA, hashSHA1},
2701 },
2702 Bugs: ProtocolBugs{
2703 NoSignatureAndHashes: true,
2704 },
2705 },
2706 flags: []string{
2707 "-cert-file", rsaCertificateFile,
2708 "-key-file", rsaKeyFile,
2709 },
2710 })
2711
2712 testCases = append(testCases, testCase{
2713 testType: serverTest,
2714 name: "SigningHash-ServerKeyExchange-Fallback",
2715 config: Config{
2716 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2717 SignatureAndHashes: []signatureAndHash{
2718 {signatureRSA, hashSHA1},
2719 },
2720 Bugs: ProtocolBugs{
2721 NoSignatureAndHashes: true,
2722 },
2723 },
2724 })
2725}
2726
David Benjamin83f90402015-01-27 01:09:43 -05002727// timeouts is the retransmit schedule for BoringSSL. It doubles and
2728// caps at 60 seconds. On the 13th timeout, it gives up.
2729var timeouts = []time.Duration{
2730 1 * time.Second,
2731 2 * time.Second,
2732 4 * time.Second,
2733 8 * time.Second,
2734 16 * time.Second,
2735 32 * time.Second,
2736 60 * time.Second,
2737 60 * time.Second,
2738 60 * time.Second,
2739 60 * time.Second,
2740 60 * time.Second,
2741 60 * time.Second,
2742 60 * time.Second,
2743}
2744
2745func addDTLSRetransmitTests() {
2746 // Test that this is indeed the timeout schedule. Stress all
2747 // four patterns of handshake.
2748 for i := 1; i < len(timeouts); i++ {
2749 number := strconv.Itoa(i)
2750 testCases = append(testCases, testCase{
2751 protocol: dtls,
2752 name: "DTLS-Retransmit-Client-" + number,
2753 config: Config{
2754 Bugs: ProtocolBugs{
2755 TimeoutSchedule: timeouts[:i],
2756 },
2757 },
2758 resumeSession: true,
2759 flags: []string{"-async"},
2760 })
2761 testCases = append(testCases, testCase{
2762 protocol: dtls,
2763 testType: serverTest,
2764 name: "DTLS-Retransmit-Server-" + number,
2765 config: Config{
2766 Bugs: ProtocolBugs{
2767 TimeoutSchedule: timeouts[:i],
2768 },
2769 },
2770 resumeSession: true,
2771 flags: []string{"-async"},
2772 })
2773 }
2774
2775 // Test that exceeding the timeout schedule hits a read
2776 // timeout.
2777 testCases = append(testCases, testCase{
2778 protocol: dtls,
2779 name: "DTLS-Retransmit-Timeout",
2780 config: Config{
2781 Bugs: ProtocolBugs{
2782 TimeoutSchedule: timeouts,
2783 },
2784 },
2785 resumeSession: true,
2786 flags: []string{"-async"},
2787 shouldFail: true,
2788 expectedError: ":READ_TIMEOUT_EXPIRED:",
2789 })
2790
2791 // Test that timeout handling has a fudge factor, due to API
2792 // problems.
2793 testCases = append(testCases, testCase{
2794 protocol: dtls,
2795 name: "DTLS-Retransmit-Fudge",
2796 config: Config{
2797 Bugs: ProtocolBugs{
2798 TimeoutSchedule: []time.Duration{
2799 timeouts[0] - 10*time.Millisecond,
2800 },
2801 },
2802 },
2803 resumeSession: true,
2804 flags: []string{"-async"},
2805 })
2806}
2807
David Benjamin884fdf12014-08-02 15:28:23 -04002808func worker(statusChan chan statusMsg, c chan *testCase, buildDir string, wg *sync.WaitGroup) {
Adam Langley95c29f32014-06-20 12:00:00 -07002809 defer wg.Done()
2810
2811 for test := range c {
Adam Langley69a01602014-11-17 17:26:55 -08002812 var err error
2813
2814 if *mallocTest < 0 {
2815 statusChan <- statusMsg{test: test, started: true}
2816 err = runTest(test, buildDir, -1)
2817 } else {
2818 for mallocNumToFail := int64(*mallocTest); ; mallocNumToFail++ {
2819 statusChan <- statusMsg{test: test, started: true}
2820 if err = runTest(test, buildDir, mallocNumToFail); err != errMoreMallocs {
2821 if err != nil {
2822 fmt.Printf("\n\nmalloc test failed at %d: %s\n", mallocNumToFail, err)
2823 }
2824 break
2825 }
2826 }
2827 }
Adam Langley95c29f32014-06-20 12:00:00 -07002828 statusChan <- statusMsg{test: test, err: err}
2829 }
2830}
2831
2832type statusMsg struct {
2833 test *testCase
2834 started bool
2835 err error
2836}
2837
David Benjamin5f237bc2015-02-11 17:14:15 -05002838func statusPrinter(doneChan chan *testOutput, statusChan chan statusMsg, total int) {
Adam Langley95c29f32014-06-20 12:00:00 -07002839 var started, done, failed, lineLen int
Adam Langley95c29f32014-06-20 12:00:00 -07002840
David Benjamin5f237bc2015-02-11 17:14:15 -05002841 testOutput := newTestOutput()
Adam Langley95c29f32014-06-20 12:00:00 -07002842 for msg := range statusChan {
David Benjamin5f237bc2015-02-11 17:14:15 -05002843 if !*pipe {
2844 // Erase the previous status line.
David Benjamin87c8a642015-02-21 01:54:29 -05002845 var erase string
2846 for i := 0; i < lineLen; i++ {
2847 erase += "\b \b"
2848 }
2849 fmt.Print(erase)
David Benjamin5f237bc2015-02-11 17:14:15 -05002850 }
2851
Adam Langley95c29f32014-06-20 12:00:00 -07002852 if msg.started {
2853 started++
2854 } else {
2855 done++
David Benjamin5f237bc2015-02-11 17:14:15 -05002856
2857 if msg.err != nil {
2858 fmt.Printf("FAILED (%s)\n%s\n", msg.test.name, msg.err)
2859 failed++
2860 testOutput.addResult(msg.test.name, "FAIL")
2861 } else {
2862 if *pipe {
2863 // Print each test instead of a status line.
2864 fmt.Printf("PASSED (%s)\n", msg.test.name)
2865 }
2866 testOutput.addResult(msg.test.name, "PASS")
2867 }
Adam Langley95c29f32014-06-20 12:00:00 -07002868 }
2869
David Benjamin5f237bc2015-02-11 17:14:15 -05002870 if !*pipe {
2871 // Print a new status line.
2872 line := fmt.Sprintf("%d/%d/%d/%d", failed, done, started, total)
2873 lineLen = len(line)
2874 os.Stdout.WriteString(line)
Adam Langley95c29f32014-06-20 12:00:00 -07002875 }
Adam Langley95c29f32014-06-20 12:00:00 -07002876 }
David Benjamin5f237bc2015-02-11 17:14:15 -05002877
2878 doneChan <- testOutput
Adam Langley95c29f32014-06-20 12:00:00 -07002879}
2880
2881func main() {
2882 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 -04002883 var flagNumWorkers *int = flag.Int("num-workers", runtime.NumCPU(), "The number of workers to run in parallel.")
David Benjamin884fdf12014-08-02 15:28:23 -04002884 var flagBuildDir *string = flag.String("build-dir", "../../../build", "The build directory to run the shim from.")
Adam Langley95c29f32014-06-20 12:00:00 -07002885
2886 flag.Parse()
2887
2888 addCipherSuiteTests()
2889 addBadECDSASignatureTests()
Adam Langley80842bd2014-06-20 12:00:00 -07002890 addCBCPaddingTests()
Kenny Root7fdeaf12014-08-05 15:23:37 -07002891 addCBCSplittingTests()
David Benjamin636293b2014-07-08 17:59:18 -04002892 addClientAuthTests()
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002893 addVersionNegotiationTests()
David Benjaminaccb4542014-12-12 23:44:33 -05002894 addMinimumVersionTests()
David Benjamin5c24a1d2014-08-31 00:59:27 -04002895 addD5BugTests()
David Benjamine78bfde2014-09-06 12:45:15 -04002896 addExtensionTests()
David Benjamin01fe8202014-09-24 15:21:44 -04002897 addResumptionVersionTests()
Adam Langley75712922014-10-10 16:23:43 -07002898 addExtendedMasterSecretTests()
Adam Langley2ae77d22014-10-28 17:29:33 -07002899 addRenegotiationTests()
David Benjamin5e961c12014-11-07 01:48:35 -05002900 addDTLSReplayTests()
David Benjamin000800a2014-11-14 01:43:59 -05002901 addSigningHashTests()
Feng Lu41aa3252014-11-21 22:47:56 -08002902 addFastRadioPaddingTests()
David Benjamin83f90402015-01-27 01:09:43 -05002903 addDTLSRetransmitTests()
David Benjamin43ec06f2014-08-05 02:28:57 -04002904 for _, async := range []bool{false, true} {
2905 for _, splitHandshake := range []bool{false, true} {
David Benjamin6fd297b2014-08-11 18:43:38 -04002906 for _, protocol := range []protocol{tls, dtls} {
2907 addStateMachineCoverageTests(async, splitHandshake, protocol)
2908 }
David Benjamin43ec06f2014-08-05 02:28:57 -04002909 }
2910 }
Adam Langley95c29f32014-06-20 12:00:00 -07002911
2912 var wg sync.WaitGroup
2913
David Benjamin2bc8e6f2014-08-02 15:22:37 -04002914 numWorkers := *flagNumWorkers
Adam Langley95c29f32014-06-20 12:00:00 -07002915
2916 statusChan := make(chan statusMsg, numWorkers)
2917 testChan := make(chan *testCase, numWorkers)
David Benjamin5f237bc2015-02-11 17:14:15 -05002918 doneChan := make(chan *testOutput)
Adam Langley95c29f32014-06-20 12:00:00 -07002919
David Benjamin025b3d32014-07-01 19:53:04 -04002920 go statusPrinter(doneChan, statusChan, len(testCases))
Adam Langley95c29f32014-06-20 12:00:00 -07002921
2922 for i := 0; i < numWorkers; i++ {
2923 wg.Add(1)
David Benjamin884fdf12014-08-02 15:28:23 -04002924 go worker(statusChan, testChan, *flagBuildDir, &wg)
Adam Langley95c29f32014-06-20 12:00:00 -07002925 }
2926
David Benjamin025b3d32014-07-01 19:53:04 -04002927 for i := range testCases {
2928 if len(*flagTest) == 0 || *flagTest == testCases[i].name {
2929 testChan <- &testCases[i]
Adam Langley95c29f32014-06-20 12:00:00 -07002930 }
2931 }
2932
2933 close(testChan)
2934 wg.Wait()
2935 close(statusChan)
David Benjamin5f237bc2015-02-11 17:14:15 -05002936 testOutput := <-doneChan
Adam Langley95c29f32014-06-20 12:00:00 -07002937
2938 fmt.Printf("\n")
David Benjamin5f237bc2015-02-11 17:14:15 -05002939
2940 if *jsonOutput != "" {
2941 if err := testOutput.writeTo(*jsonOutput); err != nil {
2942 fmt.Fprintf(os.Stderr, "Error: %s\n", err)
2943 }
2944 }
Adam Langley95c29f32014-06-20 12:00:00 -07002945}