blob: 69160b3a94caf92bb8152a23ffd392d01cf33cde [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001package main
2
3import (
4 "bytes"
David Benjamina08e49d2014-08-24 01:46:07 -04005 "crypto/ecdsa"
6 "crypto/elliptic"
David Benjamin407a10c2014-07-16 12:58:59 -04007 "crypto/x509"
David Benjamin2561dc32014-08-24 01:25:27 -04008 "encoding/base64"
David Benjamina08e49d2014-08-24 01:46:07 -04009 "encoding/pem"
Adam Langley95c29f32014-06-20 12:00:00 -070010 "flag"
11 "fmt"
12 "io"
Kenny Root7fdeaf12014-08-05 15:23:37 -070013 "io/ioutil"
Adam Langley95c29f32014-06-20 12:00:00 -070014 "net"
15 "os"
16 "os/exec"
David Benjamin884fdf12014-08-02 15:28:23 -040017 "path"
David Benjamin2bc8e6f2014-08-02 15:22:37 -040018 "runtime"
Adam Langley69a01602014-11-17 17:26:55 -080019 "strconv"
Adam Langley95c29f32014-06-20 12:00:00 -070020 "strings"
21 "sync"
22 "syscall"
David Benjamin83f90402015-01-27 01:09:43 -050023 "time"
Adam Langley95c29f32014-06-20 12:00:00 -070024)
25
Adam Langley69a01602014-11-17 17:26:55 -080026var (
David Benjamin5f237bc2015-02-11 17:14:15 -050027 useValgrind = flag.Bool("valgrind", false, "If true, run code under valgrind")
28 useGDB = flag.Bool("gdb", false, "If true, run BoringSSL code under gdb")
29 flagDebug = flag.Bool("debug", false, "Hexdump the contents of the connection")
30 mallocTest = flag.Int64("malloc-test", -1, "If non-negative, run each test with each malloc in turn failing from the given number onwards.")
31 mallocTestDebug = flag.Bool("malloc-test-debug", false, "If true, ask bssl_shim to abort rather than fail a malloc. This can be used with a specific value for --malloc-test to identity the malloc failing that is causing problems.")
32 jsonOutput = flag.String("json-output", "", "The file to output JSON results to.")
33 pipe = flag.Bool("pipe", false, "If true, print status output suitable for piping into another program.")
Adam Langley69a01602014-11-17 17:26:55 -080034)
Adam Langley95c29f32014-06-20 12:00:00 -070035
David Benjamin025b3d32014-07-01 19:53:04 -040036const (
37 rsaCertificateFile = "cert.pem"
38 ecdsaCertificateFile = "ecdsa_cert.pem"
39)
40
41const (
David Benjamina08e49d2014-08-24 01:46:07 -040042 rsaKeyFile = "key.pem"
43 ecdsaKeyFile = "ecdsa_key.pem"
44 channelIDKeyFile = "channel_id_key.pem"
David Benjamin025b3d32014-07-01 19:53:04 -040045)
46
Adam Langley95c29f32014-06-20 12:00:00 -070047var rsaCertificate, ecdsaCertificate Certificate
David Benjamina08e49d2014-08-24 01:46:07 -040048var channelIDKey *ecdsa.PrivateKey
49var channelIDBytes []byte
Adam Langley95c29f32014-06-20 12:00:00 -070050
David Benjamin61f95272014-11-25 01:55:35 -050051var testOCSPResponse = []byte{1, 2, 3, 4}
52var testSCTList = []byte{5, 6, 7, 8}
53
Adam Langley95c29f32014-06-20 12:00:00 -070054func initCertificates() {
55 var err error
David Benjamin025b3d32014-07-01 19:53:04 -040056 rsaCertificate, err = LoadX509KeyPair(rsaCertificateFile, rsaKeyFile)
Adam Langley95c29f32014-06-20 12:00:00 -070057 if err != nil {
58 panic(err)
59 }
David Benjamin61f95272014-11-25 01:55:35 -050060 rsaCertificate.OCSPStaple = testOCSPResponse
61 rsaCertificate.SignedCertificateTimestampList = testSCTList
Adam Langley95c29f32014-06-20 12:00:00 -070062
David Benjamin025b3d32014-07-01 19:53:04 -040063 ecdsaCertificate, err = LoadX509KeyPair(ecdsaCertificateFile, ecdsaKeyFile)
Adam Langley95c29f32014-06-20 12:00:00 -070064 if err != nil {
65 panic(err)
66 }
David Benjamin61f95272014-11-25 01:55:35 -050067 ecdsaCertificate.OCSPStaple = testOCSPResponse
68 ecdsaCertificate.SignedCertificateTimestampList = testSCTList
David Benjamina08e49d2014-08-24 01:46:07 -040069
70 channelIDPEMBlock, err := ioutil.ReadFile(channelIDKeyFile)
71 if err != nil {
72 panic(err)
73 }
74 channelIDDERBlock, _ := pem.Decode(channelIDPEMBlock)
75 if channelIDDERBlock.Type != "EC PRIVATE KEY" {
76 panic("bad key type")
77 }
78 channelIDKey, err = x509.ParseECPrivateKey(channelIDDERBlock.Bytes)
79 if err != nil {
80 panic(err)
81 }
82 if channelIDKey.Curve != elliptic.P256() {
83 panic("bad curve")
84 }
85
86 channelIDBytes = make([]byte, 64)
87 writeIntPadded(channelIDBytes[:32], channelIDKey.X)
88 writeIntPadded(channelIDBytes[32:], channelIDKey.Y)
Adam Langley95c29f32014-06-20 12:00:00 -070089}
90
91var certificateOnce sync.Once
92
93func getRSACertificate() Certificate {
94 certificateOnce.Do(initCertificates)
95 return rsaCertificate
96}
97
98func getECDSACertificate() Certificate {
99 certificateOnce.Do(initCertificates)
100 return ecdsaCertificate
101}
102
David Benjamin025b3d32014-07-01 19:53:04 -0400103type testType int
104
105const (
106 clientTest testType = iota
107 serverTest
108)
109
David Benjamin6fd297b2014-08-11 18:43:38 -0400110type protocol int
111
112const (
113 tls protocol = iota
114 dtls
115)
116
David Benjaminfc7b0862014-09-06 13:21:53 -0400117const (
118 alpn = 1
119 npn = 2
120)
121
Adam Langley95c29f32014-06-20 12:00:00 -0700122type testCase struct {
David Benjamin025b3d32014-07-01 19:53:04 -0400123 testType testType
David Benjamin6fd297b2014-08-11 18:43:38 -0400124 protocol protocol
Adam Langley95c29f32014-06-20 12:00:00 -0700125 name string
126 config Config
127 shouldFail bool
128 expectedError string
Adam Langleyac61fa32014-06-23 12:03:11 -0700129 // expectedLocalError, if not empty, contains a substring that must be
130 // found in the local error.
131 expectedLocalError string
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400132 // expectedVersion, if non-zero, specifies the TLS version that must be
133 // negotiated.
134 expectedVersion uint16
David Benjamin01fe8202014-09-24 15:21:44 -0400135 // expectedResumeVersion, if non-zero, specifies the TLS version that
136 // must be negotiated on resumption. If zero, expectedVersion is used.
137 expectedResumeVersion uint16
David Benjamina08e49d2014-08-24 01:46:07 -0400138 // expectChannelID controls whether the connection should have
139 // negotiated a Channel ID with channelIDKey.
140 expectChannelID bool
David Benjaminae2888f2014-09-06 12:58:58 -0400141 // expectedNextProto controls whether the connection should
142 // negotiate a next protocol via NPN or ALPN.
143 expectedNextProto string
David Benjaminfc7b0862014-09-06 13:21:53 -0400144 // expectedNextProtoType, if non-zero, is the expected next
145 // protocol negotiation mechanism.
146 expectedNextProtoType int
David Benjaminca6c8262014-11-15 19:06:08 -0500147 // expectedSRTPProtectionProfile is the DTLS-SRTP profile that
148 // should be negotiated. If zero, none should be negotiated.
149 expectedSRTPProtectionProfile uint16
Adam Langley80842bd2014-06-20 12:00:00 -0700150 // messageLen is the length, in bytes, of the test message that will be
151 // sent.
152 messageLen int
David Benjamin025b3d32014-07-01 19:53:04 -0400153 // certFile is the path to the certificate to use for the server.
154 certFile string
155 // keyFile is the path to the private key to use for the server.
156 keyFile string
David Benjamin1d5c83e2014-07-22 19:20:02 -0400157 // resumeSession controls whether a second connection should be tested
David Benjamin01fe8202014-09-24 15:21:44 -0400158 // which attempts to resume the first session.
David Benjamin1d5c83e2014-07-22 19:20:02 -0400159 resumeSession bool
David Benjamin01fe8202014-09-24 15:21:44 -0400160 // resumeConfig, if not nil, points to a Config to be used on
David Benjaminfe8eb9a2014-11-17 03:19:02 -0500161 // resumption. Unless newSessionsOnResume is set,
162 // SessionTicketKey, ServerSessionCache, and
163 // ClientSessionCache are copied from the initial connection's
164 // config. If nil, the initial connection's config is used.
David Benjamin01fe8202014-09-24 15:21:44 -0400165 resumeConfig *Config
David Benjaminfe8eb9a2014-11-17 03:19:02 -0500166 // newSessionsOnResume, if true, will cause resumeConfig to
167 // use a different session resumption context.
168 newSessionsOnResume bool
David Benjamin98e882e2014-08-08 13:24:34 -0400169 // sendPrefix sends a prefix on the socket before actually performing a
170 // handshake.
171 sendPrefix string
David Benjamine58c4f52014-08-24 03:47:07 -0400172 // shimWritesFirst controls whether the shim sends an initial "hello"
173 // message before doing a roundtrip with the runner.
174 shimWritesFirst bool
Adam Langleycf2d4f42014-10-28 19:06:14 -0700175 // renegotiate indicates the the connection should be renegotiated
176 // during the exchange.
177 renegotiate bool
178 // renegotiateCiphers is a list of ciphersuite ids that will be
179 // switched in just before renegotiation.
180 renegotiateCiphers []uint16
David Benjamin5e961c12014-11-07 01:48:35 -0500181 // replayWrites, if true, configures the underlying transport
182 // to replay every write it makes in DTLS tests.
183 replayWrites bool
David Benjamin5fa3eba2015-01-22 16:35:40 -0500184 // damageFirstWrite, if true, configures the underlying transport to
185 // damage the final byte of the first application data write.
186 damageFirstWrite bool
David Benjamin325b5c32014-07-01 19:40:31 -0400187 // flags, if not empty, contains a list of command-line flags that will
188 // be passed to the shim program.
189 flags []string
Adam Langley95c29f32014-06-20 12:00:00 -0700190}
191
David Benjamin025b3d32014-07-01 19:53:04 -0400192var testCases = []testCase{
Adam Langley95c29f32014-06-20 12:00:00 -0700193 {
194 name: "BadRSASignature",
195 config: Config{
196 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
197 Bugs: ProtocolBugs{
198 InvalidSKXSignature: true,
199 },
200 },
201 shouldFail: true,
202 expectedError: ":BAD_SIGNATURE:",
203 },
204 {
205 name: "BadECDSASignature",
206 config: Config{
207 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
208 Bugs: ProtocolBugs{
209 InvalidSKXSignature: true,
210 },
211 Certificates: []Certificate{getECDSACertificate()},
212 },
213 shouldFail: true,
214 expectedError: ":BAD_SIGNATURE:",
215 },
216 {
217 name: "BadECDSACurve",
218 config: Config{
219 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
220 Bugs: ProtocolBugs{
221 InvalidSKXCurve: true,
222 },
223 Certificates: []Certificate{getECDSACertificate()},
224 },
225 shouldFail: true,
226 expectedError: ":WRONG_CURVE:",
227 },
Adam Langleyac61fa32014-06-23 12:03:11 -0700228 {
David Benjamina8e3e0e2014-08-06 22:11:10 -0400229 testType: serverTest,
230 name: "BadRSAVersion",
231 config: Config{
232 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
233 Bugs: ProtocolBugs{
234 RsaClientKeyExchangeVersion: VersionTLS11,
235 },
236 },
237 shouldFail: true,
238 expectedError: ":DECRYPTION_FAILED_OR_BAD_RECORD_MAC:",
239 },
240 {
David Benjamin325b5c32014-07-01 19:40:31 -0400241 name: "NoFallbackSCSV",
Adam Langleyac61fa32014-06-23 12:03:11 -0700242 config: Config{
243 Bugs: ProtocolBugs{
244 FailIfNotFallbackSCSV: true,
245 },
246 },
247 shouldFail: true,
248 expectedLocalError: "no fallback SCSV found",
249 },
David Benjamin325b5c32014-07-01 19:40:31 -0400250 {
David Benjamin2a0c4962014-08-22 23:46:35 -0400251 name: "SendFallbackSCSV",
David Benjamin325b5c32014-07-01 19:40:31 -0400252 config: Config{
253 Bugs: ProtocolBugs{
254 FailIfNotFallbackSCSV: true,
255 },
256 },
257 flags: []string{"-fallback-scsv"},
258 },
David Benjamin197b3ab2014-07-02 18:37:33 -0400259 {
David Benjamin7b030512014-07-08 17:30:11 -0400260 name: "ClientCertificateTypes",
261 config: Config{
262 ClientAuth: RequestClientCert,
263 ClientCertificateTypes: []byte{
264 CertTypeDSSSign,
265 CertTypeRSASign,
266 CertTypeECDSASign,
267 },
268 },
David Benjamin2561dc32014-08-24 01:25:27 -0400269 flags: []string{
270 "-expect-certificate-types",
271 base64.StdEncoding.EncodeToString([]byte{
272 CertTypeDSSSign,
273 CertTypeRSASign,
274 CertTypeECDSASign,
275 }),
276 },
David Benjamin7b030512014-07-08 17:30:11 -0400277 },
David Benjamin636293b2014-07-08 17:59:18 -0400278 {
279 name: "NoClientCertificate",
280 config: Config{
281 ClientAuth: RequireAnyClientCert,
282 },
283 shouldFail: true,
284 expectedLocalError: "client didn't provide a certificate",
285 },
David Benjamin1c375dd2014-07-12 00:48:23 -0400286 {
287 name: "UnauthenticatedECDH",
288 config: Config{
289 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
290 Bugs: ProtocolBugs{
291 UnauthenticatedECDH: true,
292 },
293 },
294 shouldFail: true,
David Benjamine8f3d662014-07-12 01:10:19 -0400295 expectedError: ":UNEXPECTED_MESSAGE:",
David Benjamin1c375dd2014-07-12 00:48:23 -0400296 },
David Benjamin9c651c92014-07-12 13:27:45 -0400297 {
298 name: "SkipServerKeyExchange",
299 config: Config{
300 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
301 Bugs: ProtocolBugs{
302 SkipServerKeyExchange: true,
303 },
304 },
305 shouldFail: true,
306 expectedError: ":UNEXPECTED_MESSAGE:",
307 },
David Benjamin1f5f62b2014-07-12 16:18:02 -0400308 {
David Benjamina0e52232014-07-19 17:39:58 -0400309 name: "SkipChangeCipherSpec-Client",
310 config: Config{
311 Bugs: ProtocolBugs{
312 SkipChangeCipherSpec: true,
313 },
314 },
315 shouldFail: true,
David Benjamin86271ee2014-07-21 16:14:03 -0400316 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
David Benjamina0e52232014-07-19 17:39:58 -0400317 },
318 {
319 testType: serverTest,
320 name: "SkipChangeCipherSpec-Server",
321 config: Config{
322 Bugs: ProtocolBugs{
323 SkipChangeCipherSpec: true,
324 },
325 },
326 shouldFail: true,
David Benjamin86271ee2014-07-21 16:14:03 -0400327 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
David Benjamina0e52232014-07-19 17:39:58 -0400328 },
David Benjamin42be6452014-07-21 14:50:23 -0400329 {
330 testType: serverTest,
331 name: "SkipChangeCipherSpec-Server-NPN",
332 config: Config{
333 NextProtos: []string{"bar"},
334 Bugs: ProtocolBugs{
335 SkipChangeCipherSpec: true,
336 },
337 },
338 flags: []string{
339 "-advertise-npn", "\x03foo\x03bar\x03baz",
340 },
341 shouldFail: true,
David Benjamin86271ee2014-07-21 16:14:03 -0400342 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
343 },
344 {
345 name: "FragmentAcrossChangeCipherSpec-Client",
346 config: Config{
347 Bugs: ProtocolBugs{
348 FragmentAcrossChangeCipherSpec: true,
349 },
350 },
351 shouldFail: true,
352 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
353 },
354 {
355 testType: serverTest,
356 name: "FragmentAcrossChangeCipherSpec-Server",
357 config: Config{
358 Bugs: ProtocolBugs{
359 FragmentAcrossChangeCipherSpec: true,
360 },
361 },
362 shouldFail: true,
363 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
364 },
365 {
366 testType: serverTest,
367 name: "FragmentAcrossChangeCipherSpec-Server-NPN",
368 config: Config{
369 NextProtos: []string{"bar"},
370 Bugs: ProtocolBugs{
371 FragmentAcrossChangeCipherSpec: true,
372 },
373 },
374 flags: []string{
375 "-advertise-npn", "\x03foo\x03bar\x03baz",
376 },
377 shouldFail: true,
378 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
David Benjamin42be6452014-07-21 14:50:23 -0400379 },
David Benjaminf3ec83d2014-07-21 22:42:34 -0400380 {
381 testType: serverTest,
David Benjamin3fd1fbd2015-02-03 16:07:32 -0500382 name: "Alert",
383 config: Config{
384 Bugs: ProtocolBugs{
385 SendSpuriousAlert: alertRecordOverflow,
386 },
387 },
388 shouldFail: true,
389 expectedError: ":TLSV1_ALERT_RECORD_OVERFLOW:",
390 },
391 {
392 protocol: dtls,
393 testType: serverTest,
394 name: "Alert-DTLS",
395 config: Config{
396 Bugs: ProtocolBugs{
397 SendSpuriousAlert: alertRecordOverflow,
398 },
399 },
400 shouldFail: true,
401 expectedError: ":TLSV1_ALERT_RECORD_OVERFLOW:",
402 },
403 {
404 testType: serverTest,
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400405 name: "FragmentAlert",
406 config: Config{
407 Bugs: ProtocolBugs{
David Benjaminca6c8262014-11-15 19:06:08 -0500408 FragmentAlert: true,
David Benjamin3fd1fbd2015-02-03 16:07:32 -0500409 SendSpuriousAlert: alertRecordOverflow,
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400410 },
411 },
412 shouldFail: true,
413 expectedError: ":BAD_ALERT:",
414 },
415 {
David Benjamin0ea8dda2015-01-31 20:33:40 -0500416 protocol: dtls,
417 testType: serverTest,
418 name: "FragmentAlert-DTLS",
419 config: Config{
420 Bugs: ProtocolBugs{
421 FragmentAlert: true,
David Benjamin3fd1fbd2015-02-03 16:07:32 -0500422 SendSpuriousAlert: alertRecordOverflow,
David Benjamin0ea8dda2015-01-31 20:33:40 -0500423 },
424 },
425 shouldFail: true,
426 expectedError: ":BAD_ALERT:",
427 },
428 {
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400429 testType: serverTest,
David Benjaminf3ec83d2014-07-21 22:42:34 -0400430 name: "EarlyChangeCipherSpec-server-1",
431 config: Config{
432 Bugs: ProtocolBugs{
433 EarlyChangeCipherSpec: 1,
434 },
435 },
436 shouldFail: true,
437 expectedError: ":CCS_RECEIVED_EARLY:",
438 },
439 {
440 testType: serverTest,
441 name: "EarlyChangeCipherSpec-server-2",
442 config: Config{
443 Bugs: ProtocolBugs{
444 EarlyChangeCipherSpec: 2,
445 },
446 },
447 shouldFail: true,
448 expectedError: ":CCS_RECEIVED_EARLY:",
449 },
David Benjamind23f4122014-07-23 15:09:48 -0400450 {
David Benjamind23f4122014-07-23 15:09:48 -0400451 name: "SkipNewSessionTicket",
452 config: Config{
453 Bugs: ProtocolBugs{
454 SkipNewSessionTicket: true,
455 },
456 },
457 shouldFail: true,
458 expectedError: ":CCS_RECEIVED_EARLY:",
459 },
David Benjamin7e3305e2014-07-28 14:52:32 -0400460 {
David Benjamind86c7672014-08-02 04:07:12 -0400461 testType: serverTest,
David Benjaminbef270a2014-08-02 04:22:02 -0400462 name: "FallbackSCSV",
463 config: Config{
464 MaxVersion: VersionTLS11,
465 Bugs: ProtocolBugs{
466 SendFallbackSCSV: true,
467 },
468 },
469 shouldFail: true,
470 expectedError: ":INAPPROPRIATE_FALLBACK:",
471 },
472 {
473 testType: serverTest,
474 name: "FallbackSCSV-VersionMatch",
475 config: Config{
476 Bugs: ProtocolBugs{
477 SendFallbackSCSV: true,
478 },
479 },
480 },
David Benjamin98214542014-08-07 18:02:39 -0400481 {
482 testType: serverTest,
483 name: "FragmentedClientVersion",
484 config: Config{
485 Bugs: ProtocolBugs{
486 MaxHandshakeRecordLength: 1,
487 FragmentClientVersion: true,
488 },
489 },
David Benjamin82c9e902014-12-12 15:55:27 -0500490 expectedVersion: VersionTLS12,
David Benjamin98214542014-08-07 18:02:39 -0400491 },
David Benjamin98e882e2014-08-08 13:24:34 -0400492 {
493 testType: serverTest,
494 name: "MinorVersionTolerance",
495 config: Config{
496 Bugs: ProtocolBugs{
497 SendClientVersion: 0x03ff,
498 },
499 },
500 expectedVersion: VersionTLS12,
501 },
502 {
503 testType: serverTest,
504 name: "MajorVersionTolerance",
505 config: Config{
506 Bugs: ProtocolBugs{
507 SendClientVersion: 0x0400,
508 },
509 },
510 expectedVersion: VersionTLS12,
511 },
512 {
513 testType: serverTest,
514 name: "VersionTooLow",
515 config: Config{
516 Bugs: ProtocolBugs{
517 SendClientVersion: 0x0200,
518 },
519 },
520 shouldFail: true,
521 expectedError: ":UNSUPPORTED_PROTOCOL:",
522 },
523 {
524 testType: serverTest,
525 name: "HttpGET",
526 sendPrefix: "GET / HTTP/1.0\n",
527 shouldFail: true,
528 expectedError: ":HTTP_REQUEST:",
529 },
530 {
531 testType: serverTest,
532 name: "HttpPOST",
533 sendPrefix: "POST / HTTP/1.0\n",
534 shouldFail: true,
535 expectedError: ":HTTP_REQUEST:",
536 },
537 {
538 testType: serverTest,
539 name: "HttpHEAD",
540 sendPrefix: "HEAD / HTTP/1.0\n",
541 shouldFail: true,
542 expectedError: ":HTTP_REQUEST:",
543 },
544 {
545 testType: serverTest,
546 name: "HttpPUT",
547 sendPrefix: "PUT / HTTP/1.0\n",
548 shouldFail: true,
549 expectedError: ":HTTP_REQUEST:",
550 },
551 {
552 testType: serverTest,
553 name: "HttpCONNECT",
554 sendPrefix: "CONNECT www.google.com:443 HTTP/1.0\n",
555 shouldFail: true,
556 expectedError: ":HTTPS_PROXY_REQUEST:",
557 },
David Benjamin39ebf532014-08-31 02:23:49 -0400558 {
David Benjaminf080ecd2014-12-11 18:48:59 -0500559 testType: serverTest,
560 name: "Garbage",
561 sendPrefix: "blah",
562 shouldFail: true,
563 expectedError: ":UNKNOWN_PROTOCOL:",
564 },
565 {
David Benjamin39ebf532014-08-31 02:23:49 -0400566 name: "SkipCipherVersionCheck",
567 config: Config{
568 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
569 MaxVersion: VersionTLS11,
570 Bugs: ProtocolBugs{
571 SkipCipherVersionCheck: true,
572 },
573 },
574 shouldFail: true,
575 expectedError: ":WRONG_CIPHER_RETURNED:",
576 },
David Benjamin9114fae2014-11-08 11:41:14 -0500577 {
David Benjamina3e89492015-02-26 15:16:22 -0500578 name: "RSAEphemeralKey",
David Benjamin9114fae2014-11-08 11:41:14 -0500579 config: Config{
580 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
581 Bugs: ProtocolBugs{
David Benjamina3e89492015-02-26 15:16:22 -0500582 RSAEphemeralKey: true,
David Benjamin9114fae2014-11-08 11:41:14 -0500583 },
584 },
585 shouldFail: true,
586 expectedError: ":UNEXPECTED_MESSAGE:",
587 },
David Benjamin128dbc32014-12-01 01:27:42 -0500588 {
589 name: "DisableEverything",
590 flags: []string{"-no-tls12", "-no-tls11", "-no-tls1", "-no-ssl3"},
591 shouldFail: true,
592 expectedError: ":WRONG_SSL_VERSION:",
593 },
594 {
595 protocol: dtls,
596 name: "DisableEverything-DTLS",
597 flags: []string{"-no-tls12", "-no-tls1"},
598 shouldFail: true,
599 expectedError: ":WRONG_SSL_VERSION:",
600 },
David Benjamin780d6dd2015-01-06 12:03:19 -0500601 {
602 name: "NoSharedCipher",
603 config: Config{
604 CipherSuites: []uint16{},
605 },
606 shouldFail: true,
607 expectedError: ":HANDSHAKE_FAILURE_ON_CLIENT_HELLO:",
608 },
David Benjamin13be1de2015-01-11 16:29:36 -0500609 {
610 protocol: dtls,
611 testType: serverTest,
612 name: "MTU",
613 config: Config{
614 Bugs: ProtocolBugs{
615 MaxPacketLength: 256,
616 },
617 },
618 flags: []string{"-mtu", "256"},
619 },
620 {
621 protocol: dtls,
622 testType: serverTest,
623 name: "MTUExceeded",
624 config: Config{
625 Bugs: ProtocolBugs{
626 MaxPacketLength: 255,
627 },
628 },
629 flags: []string{"-mtu", "256"},
630 shouldFail: true,
631 expectedLocalError: "dtls: exceeded maximum packet length",
632 },
David Benjamin6095de82014-12-27 01:50:38 -0500633 {
634 name: "CertMismatchRSA",
635 config: Config{
636 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
637 Certificates: []Certificate{getECDSACertificate()},
638 Bugs: ProtocolBugs{
639 SendCipherSuite: TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
640 },
641 },
642 shouldFail: true,
643 expectedError: ":WRONG_CERTIFICATE_TYPE:",
644 },
645 {
646 name: "CertMismatchECDSA",
647 config: Config{
648 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
649 Certificates: []Certificate{getRSACertificate()},
650 Bugs: ProtocolBugs{
651 SendCipherSuite: TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
652 },
653 },
654 shouldFail: true,
655 expectedError: ":WRONG_CERTIFICATE_TYPE:",
656 },
David Benjamin5fa3eba2015-01-22 16:35:40 -0500657 {
658 name: "TLSFatalBadPackets",
659 damageFirstWrite: true,
660 shouldFail: true,
661 expectedError: ":DECRYPTION_FAILED_OR_BAD_RECORD_MAC:",
662 },
663 {
664 protocol: dtls,
665 name: "DTLSIgnoreBadPackets",
666 damageFirstWrite: true,
667 },
668 {
669 protocol: dtls,
670 name: "DTLSIgnoreBadPackets-Async",
671 damageFirstWrite: true,
672 flags: []string{"-async"},
673 },
David Benjamin4189bd92015-01-25 23:52:39 -0500674 {
675 name: "AppDataAfterChangeCipherSpec",
676 config: Config{
677 Bugs: ProtocolBugs{
678 AppDataAfterChangeCipherSpec: []byte("TEST MESSAGE"),
679 },
680 },
681 shouldFail: true,
682 expectedError: ":DATA_BETWEEN_CCS_AND_FINISHED:",
683 },
684 {
685 protocol: dtls,
686 name: "AppDataAfterChangeCipherSpec-DTLS",
687 config: Config{
688 Bugs: ProtocolBugs{
689 AppDataAfterChangeCipherSpec: []byte("TEST MESSAGE"),
690 },
691 },
692 },
David Benjaminb3774b92015-01-31 17:16:01 -0500693 {
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 },
David Benjamin6f5c0f42015-02-24 01:23:21 -0500783 {
784 testType: serverTest,
785 name: "FailEarlyCallback",
786 flags: []string{"-fail-early-callback"},
787 shouldFail: true,
788 expectedError: ":CONNECTION_REJECTED:",
789 expectedLocalError: "remote error: access denied",
790 },
David Benjaminbcb2d912015-02-24 23:45:43 -0500791 {
792 name: "WrongMessageType",
793 config: Config{
794 Bugs: ProtocolBugs{
795 WrongCertificateMessageType: true,
796 },
797 },
798 shouldFail: true,
799 expectedError: ":UNEXPECTED_MESSAGE:",
800 expectedLocalError: "remote error: unexpected message",
801 },
802 {
803 protocol: dtls,
804 name: "WrongMessageType-DTLS",
805 config: Config{
806 Bugs: ProtocolBugs{
807 WrongCertificateMessageType: true,
808 },
809 },
810 shouldFail: true,
811 expectedError: ":UNEXPECTED_MESSAGE:",
812 expectedLocalError: "remote error: unexpected message",
813 },
Adam Langley95c29f32014-06-20 12:00:00 -0700814}
815
David Benjamin01fe8202014-09-24 15:21:44 -0400816func doExchange(test *testCase, config *Config, conn net.Conn, messageLen int, isResume bool) error {
David Benjamin65ea8ff2014-11-23 03:01:00 -0500817 var connDebug *recordingConn
David Benjamin5fa3eba2015-01-22 16:35:40 -0500818 var connDamage *damageAdaptor
David Benjamin65ea8ff2014-11-23 03:01:00 -0500819 if *flagDebug {
820 connDebug = &recordingConn{Conn: conn}
821 conn = connDebug
822 defer func() {
823 connDebug.WriteTo(os.Stdout)
824 }()
825 }
826
David Benjamin6fd297b2014-08-11 18:43:38 -0400827 if test.protocol == dtls {
David Benjamin83f90402015-01-27 01:09:43 -0500828 config.Bugs.PacketAdaptor = newPacketAdaptor(conn)
829 conn = config.Bugs.PacketAdaptor
David Benjamin5e961c12014-11-07 01:48:35 -0500830 if test.replayWrites {
831 conn = newReplayAdaptor(conn)
832 }
David Benjamin6fd297b2014-08-11 18:43:38 -0400833 }
834
David Benjamin5fa3eba2015-01-22 16:35:40 -0500835 if test.damageFirstWrite {
836 connDamage = newDamageAdaptor(conn)
837 conn = connDamage
838 }
839
David Benjamin6fd297b2014-08-11 18:43:38 -0400840 if test.sendPrefix != "" {
841 if _, err := conn.Write([]byte(test.sendPrefix)); err != nil {
842 return err
843 }
David Benjamin98e882e2014-08-08 13:24:34 -0400844 }
845
David Benjamin1d5c83e2014-07-22 19:20:02 -0400846 var tlsConn *Conn
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400847 if test.testType == clientTest {
David Benjamin6fd297b2014-08-11 18:43:38 -0400848 if test.protocol == dtls {
849 tlsConn = DTLSServer(conn, config)
850 } else {
851 tlsConn = Server(conn, config)
852 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400853 } else {
854 config.InsecureSkipVerify = true
David Benjamin6fd297b2014-08-11 18:43:38 -0400855 if test.protocol == dtls {
856 tlsConn = DTLSClient(conn, config)
857 } else {
858 tlsConn = Client(conn, config)
859 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400860 }
861
Adam Langley95c29f32014-06-20 12:00:00 -0700862 if err := tlsConn.Handshake(); err != nil {
863 return err
864 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700865
David Benjamin01fe8202014-09-24 15:21:44 -0400866 // TODO(davidben): move all per-connection expectations into a dedicated
867 // expectations struct that can be specified separately for the two
868 // legs.
869 expectedVersion := test.expectedVersion
870 if isResume && test.expectedResumeVersion != 0 {
871 expectedVersion = test.expectedResumeVersion
872 }
873 if vers := tlsConn.ConnectionState().Version; expectedVersion != 0 && vers != expectedVersion {
874 return fmt.Errorf("got version %x, expected %x", vers, expectedVersion)
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400875 }
876
David Benjamina08e49d2014-08-24 01:46:07 -0400877 if test.expectChannelID {
878 channelID := tlsConn.ConnectionState().ChannelID
879 if channelID == nil {
880 return fmt.Errorf("no channel ID negotiated")
881 }
882 if channelID.Curve != channelIDKey.Curve ||
883 channelIDKey.X.Cmp(channelIDKey.X) != 0 ||
884 channelIDKey.Y.Cmp(channelIDKey.Y) != 0 {
885 return fmt.Errorf("incorrect channel ID")
886 }
887 }
888
David Benjaminae2888f2014-09-06 12:58:58 -0400889 if expected := test.expectedNextProto; expected != "" {
890 if actual := tlsConn.ConnectionState().NegotiatedProtocol; actual != expected {
891 return fmt.Errorf("next proto mismatch: got %s, wanted %s", actual, expected)
892 }
893 }
894
David Benjaminfc7b0862014-09-06 13:21:53 -0400895 if test.expectedNextProtoType != 0 {
896 if (test.expectedNextProtoType == alpn) != tlsConn.ConnectionState().NegotiatedProtocolFromALPN {
897 return fmt.Errorf("next proto type mismatch")
898 }
899 }
900
David Benjaminca6c8262014-11-15 19:06:08 -0500901 if p := tlsConn.ConnectionState().SRTPProtectionProfile; p != test.expectedSRTPProtectionProfile {
902 return fmt.Errorf("SRTP profile mismatch: got %d, wanted %d", p, test.expectedSRTPProtectionProfile)
903 }
904
David Benjamine58c4f52014-08-24 03:47:07 -0400905 if test.shimWritesFirst {
906 var buf [5]byte
907 _, err := io.ReadFull(tlsConn, buf[:])
908 if err != nil {
909 return err
910 }
911 if string(buf[:]) != "hello" {
912 return fmt.Errorf("bad initial message")
913 }
914 }
915
Adam Langleycf2d4f42014-10-28 19:06:14 -0700916 if test.renegotiate {
917 if test.renegotiateCiphers != nil {
918 config.CipherSuites = test.renegotiateCiphers
919 }
920 if err := tlsConn.Renegotiate(); err != nil {
921 return err
922 }
923 } else if test.renegotiateCiphers != nil {
924 panic("renegotiateCiphers without renegotiate")
925 }
926
David Benjamin5fa3eba2015-01-22 16:35:40 -0500927 if test.damageFirstWrite {
928 connDamage.setDamage(true)
929 tlsConn.Write([]byte("DAMAGED WRITE"))
930 connDamage.setDamage(false)
931 }
932
Kenny Root7fdeaf12014-08-05 15:23:37 -0700933 if messageLen < 0 {
David Benjamin6fd297b2014-08-11 18:43:38 -0400934 if test.protocol == dtls {
935 return fmt.Errorf("messageLen < 0 not supported for DTLS tests")
936 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700937 // Read until EOF.
938 _, err := io.Copy(ioutil.Discard, tlsConn)
939 return err
940 }
941
David Benjamin4189bd92015-01-25 23:52:39 -0500942 var testMessage []byte
943 if config.Bugs.AppDataAfterChangeCipherSpec != nil {
944 // We've already sent a message. Expect the shim to echo it
945 // back.
946 testMessage = config.Bugs.AppDataAfterChangeCipherSpec
947 } else {
948 if messageLen == 0 {
949 messageLen = 32
950 }
951 testMessage = make([]byte, messageLen)
952 for i := range testMessage {
953 testMessage[i] = 0x42
954 }
955 tlsConn.Write(testMessage)
Adam Langley80842bd2014-06-20 12:00:00 -0700956 }
Adam Langley95c29f32014-06-20 12:00:00 -0700957
958 buf := make([]byte, len(testMessage))
David Benjamin6fd297b2014-08-11 18:43:38 -0400959 if test.protocol == dtls {
960 bufTmp := make([]byte, len(buf)+1)
961 n, err := tlsConn.Read(bufTmp)
962 if err != nil {
963 return err
964 }
965 if n != len(buf) {
966 return fmt.Errorf("bad reply; length mismatch (%d vs %d)", n, len(buf))
967 }
968 copy(buf, bufTmp)
969 } else {
970 _, err := io.ReadFull(tlsConn, buf)
971 if err != nil {
972 return err
973 }
Adam Langley95c29f32014-06-20 12:00:00 -0700974 }
975
976 for i, v := range buf {
977 if v != testMessage[i]^0xff {
978 return fmt.Errorf("bad reply contents at byte %d", i)
979 }
980 }
981
982 return nil
983}
984
David Benjamin325b5c32014-07-01 19:40:31 -0400985func valgrindOf(dbAttach bool, path string, args ...string) *exec.Cmd {
986 valgrindArgs := []string{"--error-exitcode=99", "--track-origins=yes", "--leak-check=full"}
Adam Langley95c29f32014-06-20 12:00:00 -0700987 if dbAttach {
David Benjamin325b5c32014-07-01 19:40:31 -0400988 valgrindArgs = append(valgrindArgs, "--db-attach=yes", "--db-command=xterm -e gdb -nw %f %p")
Adam Langley95c29f32014-06-20 12:00:00 -0700989 }
David Benjamin325b5c32014-07-01 19:40:31 -0400990 valgrindArgs = append(valgrindArgs, path)
991 valgrindArgs = append(valgrindArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -0700992
David Benjamin325b5c32014-07-01 19:40:31 -0400993 return exec.Command("valgrind", valgrindArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -0700994}
995
David Benjamin325b5c32014-07-01 19:40:31 -0400996func gdbOf(path string, args ...string) *exec.Cmd {
997 xtermArgs := []string{"-e", "gdb", "--args"}
998 xtermArgs = append(xtermArgs, path)
999 xtermArgs = append(xtermArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -07001000
David Benjamin325b5c32014-07-01 19:40:31 -04001001 return exec.Command("xterm", xtermArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -07001002}
1003
Adam Langley69a01602014-11-17 17:26:55 -08001004type moreMallocsError struct{}
1005
1006func (moreMallocsError) Error() string {
1007 return "child process did not exhaust all allocation calls"
1008}
1009
1010var errMoreMallocs = moreMallocsError{}
1011
David Benjamin87c8a642015-02-21 01:54:29 -05001012// accept accepts a connection from listener, unless waitChan signals a process
1013// exit first.
1014func acceptOrWait(listener net.Listener, waitChan chan error) (net.Conn, error) {
1015 type connOrError struct {
1016 conn net.Conn
1017 err error
1018 }
1019 connChan := make(chan connOrError, 1)
1020 go func() {
1021 conn, err := listener.Accept()
1022 connChan <- connOrError{conn, err}
1023 close(connChan)
1024 }()
1025 select {
1026 case result := <-connChan:
1027 return result.conn, result.err
1028 case childErr := <-waitChan:
1029 waitChan <- childErr
1030 return nil, fmt.Errorf("child exited early: %s", childErr)
1031 }
1032}
1033
Adam Langley69a01602014-11-17 17:26:55 -08001034func runTest(test *testCase, buildDir string, mallocNumToFail int64) error {
Adam Langley38311732014-10-16 19:04:35 -07001035 if !test.shouldFail && (len(test.expectedError) > 0 || len(test.expectedLocalError) > 0) {
1036 panic("Error expected without shouldFail in " + test.name)
1037 }
1038
David Benjamin87c8a642015-02-21 01:54:29 -05001039 listener, err := net.ListenTCP("tcp4", &net.TCPAddr{IP: net.IP{127, 0, 0, 1}})
1040 if err != nil {
1041 panic(err)
1042 }
1043 defer func() {
1044 if listener != nil {
1045 listener.Close()
1046 }
1047 }()
Adam Langley95c29f32014-06-20 12:00:00 -07001048
David Benjamin884fdf12014-08-02 15:28:23 -04001049 shim_path := path.Join(buildDir, "ssl/test/bssl_shim")
David Benjamin87c8a642015-02-21 01:54:29 -05001050 flags := []string{"-port", strconv.Itoa(listener.Addr().(*net.TCPAddr).Port)}
David Benjamin1d5c83e2014-07-22 19:20:02 -04001051 if test.testType == serverTest {
David Benjamin5a593af2014-08-11 19:51:50 -04001052 flags = append(flags, "-server")
1053
David Benjamin025b3d32014-07-01 19:53:04 -04001054 flags = append(flags, "-key-file")
1055 if test.keyFile == "" {
1056 flags = append(flags, rsaKeyFile)
1057 } else {
1058 flags = append(flags, test.keyFile)
1059 }
1060
1061 flags = append(flags, "-cert-file")
1062 if test.certFile == "" {
1063 flags = append(flags, rsaCertificateFile)
1064 } else {
1065 flags = append(flags, test.certFile)
1066 }
1067 }
David Benjamin5a593af2014-08-11 19:51:50 -04001068
David Benjamin6fd297b2014-08-11 18:43:38 -04001069 if test.protocol == dtls {
1070 flags = append(flags, "-dtls")
1071 }
1072
David Benjamin5a593af2014-08-11 19:51:50 -04001073 if test.resumeSession {
1074 flags = append(flags, "-resume")
1075 }
1076
David Benjamine58c4f52014-08-24 03:47:07 -04001077 if test.shimWritesFirst {
1078 flags = append(flags, "-shim-writes-first")
1079 }
1080
David Benjamin025b3d32014-07-01 19:53:04 -04001081 flags = append(flags, test.flags...)
1082
1083 var shim *exec.Cmd
1084 if *useValgrind {
1085 shim = valgrindOf(false, shim_path, flags...)
Adam Langley75712922014-10-10 16:23:43 -07001086 } else if *useGDB {
1087 shim = gdbOf(shim_path, flags...)
David Benjamin025b3d32014-07-01 19:53:04 -04001088 } else {
1089 shim = exec.Command(shim_path, flags...)
1090 }
David Benjamin025b3d32014-07-01 19:53:04 -04001091 shim.Stdin = os.Stdin
1092 var stdoutBuf, stderrBuf bytes.Buffer
1093 shim.Stdout = &stdoutBuf
1094 shim.Stderr = &stderrBuf
Adam Langley69a01602014-11-17 17:26:55 -08001095 if mallocNumToFail >= 0 {
David Benjamin9e128b02015-02-09 13:13:09 -05001096 shim.Env = os.Environ()
1097 shim.Env = append(shim.Env, "MALLOC_NUMBER_TO_FAIL="+strconv.FormatInt(mallocNumToFail, 10))
Adam Langley69a01602014-11-17 17:26:55 -08001098 if *mallocTestDebug {
1099 shim.Env = append(shim.Env, "MALLOC_ABORT_ON_FAIL=1")
1100 }
1101 shim.Env = append(shim.Env, "_MALLOC_CHECK=1")
1102 }
David Benjamin025b3d32014-07-01 19:53:04 -04001103
1104 if err := shim.Start(); err != nil {
Adam Langley95c29f32014-06-20 12:00:00 -07001105 panic(err)
1106 }
David Benjamin87c8a642015-02-21 01:54:29 -05001107 waitChan := make(chan error, 1)
1108 go func() { waitChan <- shim.Wait() }()
Adam Langley95c29f32014-06-20 12:00:00 -07001109
1110 config := test.config
David Benjamin1d5c83e2014-07-22 19:20:02 -04001111 config.ClientSessionCache = NewLRUClientSessionCache(1)
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001112 config.ServerSessionCache = NewLRUServerSessionCache(1)
David Benjamin025b3d32014-07-01 19:53:04 -04001113 if test.testType == clientTest {
1114 if len(config.Certificates) == 0 {
1115 config.Certificates = []Certificate{getRSACertificate()}
1116 }
David Benjamin87c8a642015-02-21 01:54:29 -05001117 } else {
1118 // Supply a ServerName to ensure a constant session cache key,
1119 // rather than falling back to net.Conn.RemoteAddr.
1120 if len(config.ServerName) == 0 {
1121 config.ServerName = "test"
1122 }
David Benjamin025b3d32014-07-01 19:53:04 -04001123 }
Adam Langley95c29f32014-06-20 12:00:00 -07001124
David Benjamin87c8a642015-02-21 01:54:29 -05001125 conn, err := acceptOrWait(listener, waitChan)
1126 if err == nil {
1127 err = doExchange(test, &config, conn, test.messageLen, false /* not a resumption */)
1128 conn.Close()
1129 }
David Benjamin65ea8ff2014-11-23 03:01:00 -05001130
David Benjamin1d5c83e2014-07-22 19:20:02 -04001131 if err == nil && test.resumeSession {
David Benjamin01fe8202014-09-24 15:21:44 -04001132 var resumeConfig Config
1133 if test.resumeConfig != nil {
1134 resumeConfig = *test.resumeConfig
David Benjamin87c8a642015-02-21 01:54:29 -05001135 if len(resumeConfig.ServerName) == 0 {
1136 resumeConfig.ServerName = config.ServerName
1137 }
David Benjamin01fe8202014-09-24 15:21:44 -04001138 if len(resumeConfig.Certificates) == 0 {
1139 resumeConfig.Certificates = []Certificate{getRSACertificate()}
1140 }
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001141 if !test.newSessionsOnResume {
1142 resumeConfig.SessionTicketKey = config.SessionTicketKey
1143 resumeConfig.ClientSessionCache = config.ClientSessionCache
1144 resumeConfig.ServerSessionCache = config.ServerSessionCache
1145 }
David Benjamin01fe8202014-09-24 15:21:44 -04001146 } else {
1147 resumeConfig = config
1148 }
David Benjamin87c8a642015-02-21 01:54:29 -05001149 var connResume net.Conn
1150 connResume, err = acceptOrWait(listener, waitChan)
1151 if err == nil {
1152 err = doExchange(test, &resumeConfig, connResume, test.messageLen, true /* resumption */)
1153 connResume.Close()
1154 }
David Benjamin1d5c83e2014-07-22 19:20:02 -04001155 }
1156
David Benjamin87c8a642015-02-21 01:54:29 -05001157 // Close the listener now. This is to avoid hangs should the shim try to
1158 // open more connections than expected.
1159 listener.Close()
1160 listener = nil
1161
1162 childErr := <-waitChan
Adam Langley69a01602014-11-17 17:26:55 -08001163 if exitError, ok := childErr.(*exec.ExitError); ok {
1164 if exitError.Sys().(syscall.WaitStatus).ExitStatus() == 88 {
1165 return errMoreMallocs
1166 }
1167 }
Adam Langley95c29f32014-06-20 12:00:00 -07001168
1169 stdout := string(stdoutBuf.Bytes())
1170 stderr := string(stderrBuf.Bytes())
1171 failed := err != nil || childErr != nil
1172 correctFailure := len(test.expectedError) == 0 || strings.Contains(stdout, test.expectedError)
Adam Langleyac61fa32014-06-23 12:03:11 -07001173 localError := "none"
1174 if err != nil {
1175 localError = err.Error()
1176 }
1177 if len(test.expectedLocalError) != 0 {
1178 correctFailure = correctFailure && strings.Contains(localError, test.expectedLocalError)
1179 }
Adam Langley95c29f32014-06-20 12:00:00 -07001180
1181 if failed != test.shouldFail || failed && !correctFailure {
Adam Langley95c29f32014-06-20 12:00:00 -07001182 childError := "none"
Adam Langley95c29f32014-06-20 12:00:00 -07001183 if childErr != nil {
1184 childError = childErr.Error()
1185 }
1186
1187 var msg string
1188 switch {
1189 case failed && !test.shouldFail:
1190 msg = "unexpected failure"
1191 case !failed && test.shouldFail:
1192 msg = "unexpected success"
1193 case failed && !correctFailure:
Adam Langleyac61fa32014-06-23 12:03:11 -07001194 msg = "bad error (wanted '" + test.expectedError + "' / '" + test.expectedLocalError + "')"
Adam Langley95c29f32014-06-20 12:00:00 -07001195 default:
1196 panic("internal error")
1197 }
1198
1199 return fmt.Errorf("%s: local error '%s', child error '%s', stdout:\n%s\nstderr:\n%s", msg, localError, childError, string(stdoutBuf.Bytes()), stderr)
1200 }
1201
1202 if !*useValgrind && len(stderr) > 0 {
1203 println(stderr)
1204 }
1205
1206 return nil
1207}
1208
1209var tlsVersions = []struct {
1210 name string
1211 version uint16
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001212 flag string
David Benjamin8b8c0062014-11-23 02:47:52 -05001213 hasDTLS bool
Adam Langley95c29f32014-06-20 12:00:00 -07001214}{
David Benjamin8b8c0062014-11-23 02:47:52 -05001215 {"SSL3", VersionSSL30, "-no-ssl3", false},
1216 {"TLS1", VersionTLS10, "-no-tls1", true},
1217 {"TLS11", VersionTLS11, "-no-tls11", false},
1218 {"TLS12", VersionTLS12, "-no-tls12", true},
Adam Langley95c29f32014-06-20 12:00:00 -07001219}
1220
1221var testCipherSuites = []struct {
1222 name string
1223 id uint16
1224}{
1225 {"3DES-SHA", TLS_RSA_WITH_3DES_EDE_CBC_SHA},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001226 {"AES128-GCM", TLS_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001227 {"AES128-SHA", TLS_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001228 {"AES128-SHA256", TLS_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001229 {"AES256-GCM", TLS_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001230 {"AES256-SHA", TLS_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001231 {"AES256-SHA256", TLS_RSA_WITH_AES_256_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001232 {"DHE-RSA-AES128-GCM", TLS_DHE_RSA_WITH_AES_128_GCM_SHA256},
1233 {"DHE-RSA-AES128-SHA", TLS_DHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001234 {"DHE-RSA-AES128-SHA256", TLS_DHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001235 {"DHE-RSA-AES256-GCM", TLS_DHE_RSA_WITH_AES_256_GCM_SHA384},
1236 {"DHE-RSA-AES256-SHA", TLS_DHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001237 {"DHE-RSA-AES256-SHA256", TLS_DHE_RSA_WITH_AES_256_CBC_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001238 {"ECDHE-ECDSA-AES128-GCM", TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
1239 {"ECDHE-ECDSA-AES128-SHA", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001240 {"ECDHE-ECDSA-AES128-SHA256", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256},
1241 {"ECDHE-ECDSA-AES256-GCM", TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001242 {"ECDHE-ECDSA-AES256-SHA", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001243 {"ECDHE-ECDSA-AES256-SHA384", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001244 {"ECDHE-ECDSA-RC4-SHA", TLS_ECDHE_ECDSA_WITH_RC4_128_SHA},
David Benjamin2af684f2014-10-27 02:23:15 -04001245 {"ECDHE-PSK-WITH-AES-128-GCM-SHA256", TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001246 {"ECDHE-RSA-AES128-GCM", TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001247 {"ECDHE-RSA-AES128-SHA", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001248 {"ECDHE-RSA-AES128-SHA256", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001249 {"ECDHE-RSA-AES256-GCM", TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001250 {"ECDHE-RSA-AES256-SHA", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001251 {"ECDHE-RSA-AES256-SHA384", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001252 {"ECDHE-RSA-RC4-SHA", TLS_ECDHE_RSA_WITH_RC4_128_SHA},
David Benjamin48cae082014-10-27 01:06:24 -04001253 {"PSK-AES128-CBC-SHA", TLS_PSK_WITH_AES_128_CBC_SHA},
1254 {"PSK-AES256-CBC-SHA", TLS_PSK_WITH_AES_256_CBC_SHA},
1255 {"PSK-RC4-SHA", TLS_PSK_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -07001256 {"RC4-MD5", TLS_RSA_WITH_RC4_128_MD5},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001257 {"RC4-SHA", TLS_RSA_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -07001258}
1259
David Benjamin8b8c0062014-11-23 02:47:52 -05001260func hasComponent(suiteName, component string) bool {
1261 return strings.Contains("-"+suiteName+"-", "-"+component+"-")
1262}
1263
David Benjaminf7768e42014-08-31 02:06:47 -04001264func isTLS12Only(suiteName string) bool {
David Benjamin8b8c0062014-11-23 02:47:52 -05001265 return hasComponent(suiteName, "GCM") ||
1266 hasComponent(suiteName, "SHA256") ||
1267 hasComponent(suiteName, "SHA384")
1268}
1269
1270func isDTLSCipher(suiteName string) bool {
David Benjamine95d20d2014-12-23 11:16:01 -05001271 return !hasComponent(suiteName, "RC4")
David Benjaminf7768e42014-08-31 02:06:47 -04001272}
1273
Adam Langley95c29f32014-06-20 12:00:00 -07001274func addCipherSuiteTests() {
1275 for _, suite := range testCipherSuites {
David Benjamin48cae082014-10-27 01:06:24 -04001276 const psk = "12345"
1277 const pskIdentity = "luggage combo"
1278
Adam Langley95c29f32014-06-20 12:00:00 -07001279 var cert Certificate
David Benjamin025b3d32014-07-01 19:53:04 -04001280 var certFile string
1281 var keyFile string
David Benjamin8b8c0062014-11-23 02:47:52 -05001282 if hasComponent(suite.name, "ECDSA") {
Adam Langley95c29f32014-06-20 12:00:00 -07001283 cert = getECDSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -04001284 certFile = ecdsaCertificateFile
1285 keyFile = ecdsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -07001286 } else {
1287 cert = getRSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -04001288 certFile = rsaCertificateFile
1289 keyFile = rsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -07001290 }
1291
David Benjamin48cae082014-10-27 01:06:24 -04001292 var flags []string
David Benjamin8b8c0062014-11-23 02:47:52 -05001293 if hasComponent(suite.name, "PSK") {
David Benjamin48cae082014-10-27 01:06:24 -04001294 flags = append(flags,
1295 "-psk", psk,
1296 "-psk-identity", pskIdentity)
1297 }
1298
Adam Langley95c29f32014-06-20 12:00:00 -07001299 for _, ver := range tlsVersions {
David Benjaminf7768e42014-08-31 02:06:47 -04001300 if ver.version < VersionTLS12 && isTLS12Only(suite.name) {
Adam Langley95c29f32014-06-20 12:00:00 -07001301 continue
1302 }
1303
David Benjamin025b3d32014-07-01 19:53:04 -04001304 testCases = append(testCases, testCase{
1305 testType: clientTest,
1306 name: ver.name + "-" + suite.name + "-client",
Adam Langley95c29f32014-06-20 12:00:00 -07001307 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001308 MinVersion: ver.version,
1309 MaxVersion: ver.version,
1310 CipherSuites: []uint16{suite.id},
1311 Certificates: []Certificate{cert},
1312 PreSharedKey: []byte(psk),
1313 PreSharedKeyIdentity: pskIdentity,
Adam Langley95c29f32014-06-20 12:00:00 -07001314 },
David Benjamin48cae082014-10-27 01:06:24 -04001315 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001316 resumeSession: true,
Adam Langley95c29f32014-06-20 12:00:00 -07001317 })
David Benjamin025b3d32014-07-01 19:53:04 -04001318
David Benjamin76d8abe2014-08-14 16:25:34 -04001319 testCases = append(testCases, testCase{
1320 testType: serverTest,
1321 name: ver.name + "-" + suite.name + "-server",
1322 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001323 MinVersion: ver.version,
1324 MaxVersion: ver.version,
1325 CipherSuites: []uint16{suite.id},
1326 Certificates: []Certificate{cert},
1327 PreSharedKey: []byte(psk),
1328 PreSharedKeyIdentity: pskIdentity,
David Benjamin76d8abe2014-08-14 16:25:34 -04001329 },
1330 certFile: certFile,
1331 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001332 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001333 resumeSession: true,
David Benjamin76d8abe2014-08-14 16:25:34 -04001334 })
David Benjamin6fd297b2014-08-11 18:43:38 -04001335
David Benjamin8b8c0062014-11-23 02:47:52 -05001336 if ver.hasDTLS && isDTLSCipher(suite.name) {
David Benjamin6fd297b2014-08-11 18:43:38 -04001337 testCases = append(testCases, testCase{
1338 testType: clientTest,
1339 protocol: dtls,
1340 name: "D" + ver.name + "-" + suite.name + "-client",
1341 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001342 MinVersion: ver.version,
1343 MaxVersion: ver.version,
1344 CipherSuites: []uint16{suite.id},
1345 Certificates: []Certificate{cert},
1346 PreSharedKey: []byte(psk),
1347 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001348 },
David Benjamin48cae082014-10-27 01:06:24 -04001349 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001350 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001351 })
1352 testCases = append(testCases, testCase{
1353 testType: serverTest,
1354 protocol: dtls,
1355 name: "D" + ver.name + "-" + suite.name + "-server",
1356 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001357 MinVersion: ver.version,
1358 MaxVersion: ver.version,
1359 CipherSuites: []uint16{suite.id},
1360 Certificates: []Certificate{cert},
1361 PreSharedKey: []byte(psk),
1362 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001363 },
1364 certFile: certFile,
1365 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001366 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001367 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001368 })
1369 }
Adam Langley95c29f32014-06-20 12:00:00 -07001370 }
1371 }
1372}
1373
1374func addBadECDSASignatureTests() {
1375 for badR := BadValue(1); badR < NumBadValues; badR++ {
1376 for badS := BadValue(1); badS < NumBadValues; badS++ {
David Benjamin025b3d32014-07-01 19:53:04 -04001377 testCases = append(testCases, testCase{
Adam Langley95c29f32014-06-20 12:00:00 -07001378 name: fmt.Sprintf("BadECDSA-%d-%d", badR, badS),
1379 config: Config{
1380 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
1381 Certificates: []Certificate{getECDSACertificate()},
1382 Bugs: ProtocolBugs{
1383 BadECDSAR: badR,
1384 BadECDSAS: badS,
1385 },
1386 },
1387 shouldFail: true,
1388 expectedError: "SIGNATURE",
1389 })
1390 }
1391 }
1392}
1393
Adam Langley80842bd2014-06-20 12:00:00 -07001394func addCBCPaddingTests() {
David Benjamin025b3d32014-07-01 19:53:04 -04001395 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001396 name: "MaxCBCPadding",
1397 config: Config{
1398 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1399 Bugs: ProtocolBugs{
1400 MaxPadding: true,
1401 },
1402 },
1403 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1404 })
David Benjamin025b3d32014-07-01 19:53:04 -04001405 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001406 name: "BadCBCPadding",
1407 config: Config{
1408 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1409 Bugs: ProtocolBugs{
1410 PaddingFirstByteBad: true,
1411 },
1412 },
1413 shouldFail: true,
1414 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1415 })
1416 // OpenSSL previously had an issue where the first byte of padding in
1417 // 255 bytes of padding wasn't checked.
David Benjamin025b3d32014-07-01 19:53:04 -04001418 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001419 name: "BadCBCPadding255",
1420 config: Config{
1421 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1422 Bugs: ProtocolBugs{
1423 MaxPadding: true,
1424 PaddingFirstByteBadIf255: true,
1425 },
1426 },
1427 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1428 shouldFail: true,
1429 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1430 })
1431}
1432
Kenny Root7fdeaf12014-08-05 15:23:37 -07001433func addCBCSplittingTests() {
1434 testCases = append(testCases, testCase{
1435 name: "CBCRecordSplitting",
1436 config: Config{
1437 MaxVersion: VersionTLS10,
1438 MinVersion: VersionTLS10,
1439 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1440 },
1441 messageLen: -1, // read until EOF
1442 flags: []string{
1443 "-async",
1444 "-write-different-record-sizes",
1445 "-cbc-record-splitting",
1446 },
David Benjamina8e3e0e2014-08-06 22:11:10 -04001447 })
1448 testCases = append(testCases, testCase{
Kenny Root7fdeaf12014-08-05 15:23:37 -07001449 name: "CBCRecordSplittingPartialWrite",
1450 config: Config{
1451 MaxVersion: VersionTLS10,
1452 MinVersion: VersionTLS10,
1453 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1454 },
1455 messageLen: -1, // read until EOF
1456 flags: []string{
1457 "-async",
1458 "-write-different-record-sizes",
1459 "-cbc-record-splitting",
1460 "-partial-write",
1461 },
1462 })
1463}
1464
David Benjamin636293b2014-07-08 17:59:18 -04001465func addClientAuthTests() {
David Benjamin407a10c2014-07-16 12:58:59 -04001466 // Add a dummy cert pool to stress certificate authority parsing.
1467 // TODO(davidben): Add tests that those values parse out correctly.
1468 certPool := x509.NewCertPool()
1469 cert, err := x509.ParseCertificate(rsaCertificate.Certificate[0])
1470 if err != nil {
1471 panic(err)
1472 }
1473 certPool.AddCert(cert)
1474
David Benjamin636293b2014-07-08 17:59:18 -04001475 for _, ver := range tlsVersions {
David Benjamin636293b2014-07-08 17:59:18 -04001476 testCases = append(testCases, testCase{
1477 testType: clientTest,
David Benjamin67666e72014-07-12 15:47:52 -04001478 name: ver.name + "-Client-ClientAuth-RSA",
David Benjamin636293b2014-07-08 17:59:18 -04001479 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001480 MinVersion: ver.version,
1481 MaxVersion: ver.version,
1482 ClientAuth: RequireAnyClientCert,
1483 ClientCAs: certPool,
David Benjamin636293b2014-07-08 17:59:18 -04001484 },
1485 flags: []string{
1486 "-cert-file", rsaCertificateFile,
1487 "-key-file", rsaKeyFile,
1488 },
1489 })
1490 testCases = append(testCases, testCase{
David Benjamin67666e72014-07-12 15:47:52 -04001491 testType: serverTest,
1492 name: ver.name + "-Server-ClientAuth-RSA",
1493 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001494 MinVersion: ver.version,
1495 MaxVersion: ver.version,
David Benjamin67666e72014-07-12 15:47:52 -04001496 Certificates: []Certificate{rsaCertificate},
1497 },
1498 flags: []string{"-require-any-client-certificate"},
1499 })
David Benjamine098ec22014-08-27 23:13:20 -04001500 if ver.version != VersionSSL30 {
1501 testCases = append(testCases, testCase{
1502 testType: serverTest,
1503 name: ver.name + "-Server-ClientAuth-ECDSA",
1504 config: Config{
1505 MinVersion: ver.version,
1506 MaxVersion: ver.version,
1507 Certificates: []Certificate{ecdsaCertificate},
1508 },
1509 flags: []string{"-require-any-client-certificate"},
1510 })
1511 testCases = append(testCases, testCase{
1512 testType: clientTest,
1513 name: ver.name + "-Client-ClientAuth-ECDSA",
1514 config: Config{
1515 MinVersion: ver.version,
1516 MaxVersion: ver.version,
1517 ClientAuth: RequireAnyClientCert,
1518 ClientCAs: certPool,
1519 },
1520 flags: []string{
1521 "-cert-file", ecdsaCertificateFile,
1522 "-key-file", ecdsaKeyFile,
1523 },
1524 })
1525 }
David Benjamin636293b2014-07-08 17:59:18 -04001526 }
1527}
1528
Adam Langley75712922014-10-10 16:23:43 -07001529func addExtendedMasterSecretTests() {
1530 const expectEMSFlag = "-expect-extended-master-secret"
1531
1532 for _, with := range []bool{false, true} {
1533 prefix := "No"
1534 var flags []string
1535 if with {
1536 prefix = ""
1537 flags = []string{expectEMSFlag}
1538 }
1539
1540 for _, isClient := range []bool{false, true} {
1541 suffix := "-Server"
1542 testType := serverTest
1543 if isClient {
1544 suffix = "-Client"
1545 testType = clientTest
1546 }
1547
1548 for _, ver := range tlsVersions {
1549 test := testCase{
1550 testType: testType,
1551 name: prefix + "ExtendedMasterSecret-" + ver.name + suffix,
1552 config: Config{
1553 MinVersion: ver.version,
1554 MaxVersion: ver.version,
1555 Bugs: ProtocolBugs{
1556 NoExtendedMasterSecret: !with,
1557 RequireExtendedMasterSecret: with,
1558 },
1559 },
David Benjamin48cae082014-10-27 01:06:24 -04001560 flags: flags,
1561 shouldFail: ver.version == VersionSSL30 && with,
Adam Langley75712922014-10-10 16:23:43 -07001562 }
1563 if test.shouldFail {
1564 test.expectedLocalError = "extended master secret required but not supported by peer"
1565 }
1566 testCases = append(testCases, test)
1567 }
1568 }
1569 }
1570
1571 // When a session is resumed, it should still be aware that its master
1572 // secret was generated via EMS and thus it's safe to use tls-unique.
1573 testCases = append(testCases, testCase{
1574 name: "ExtendedMasterSecret-Resume",
1575 config: Config{
1576 Bugs: ProtocolBugs{
1577 RequireExtendedMasterSecret: true,
1578 },
1579 },
1580 flags: []string{expectEMSFlag},
1581 resumeSession: true,
1582 })
1583}
1584
David Benjamin43ec06f2014-08-05 02:28:57 -04001585// Adds tests that try to cover the range of the handshake state machine, under
1586// various conditions. Some of these are redundant with other tests, but they
1587// only cover the synchronous case.
David Benjamin6fd297b2014-08-11 18:43:38 -04001588func addStateMachineCoverageTests(async, splitHandshake bool, protocol protocol) {
David Benjamin43ec06f2014-08-05 02:28:57 -04001589 var suffix string
1590 var flags []string
1591 var maxHandshakeRecordLength int
David Benjamin6fd297b2014-08-11 18:43:38 -04001592 if protocol == dtls {
1593 suffix = "-DTLS"
1594 }
David Benjamin43ec06f2014-08-05 02:28:57 -04001595 if async {
David Benjamin6fd297b2014-08-11 18:43:38 -04001596 suffix += "-Async"
David Benjamin43ec06f2014-08-05 02:28:57 -04001597 flags = append(flags, "-async")
1598 } else {
David Benjamin6fd297b2014-08-11 18:43:38 -04001599 suffix += "-Sync"
David Benjamin43ec06f2014-08-05 02:28:57 -04001600 }
1601 if splitHandshake {
1602 suffix += "-SplitHandshakeRecords"
David Benjamin98214542014-08-07 18:02:39 -04001603 maxHandshakeRecordLength = 1
David Benjamin43ec06f2014-08-05 02:28:57 -04001604 }
1605
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001606 // Basic handshake, with resumption. Client and server,
1607 // session ID and session ticket.
David Benjamin43ec06f2014-08-05 02:28:57 -04001608 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001609 protocol: protocol,
1610 name: "Basic-Client" + suffix,
David Benjamin43ec06f2014-08-05 02:28:57 -04001611 config: Config{
1612 Bugs: ProtocolBugs{
1613 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1614 },
1615 },
David Benjaminbed9aae2014-08-07 19:13:38 -04001616 flags: flags,
1617 resumeSession: true,
1618 })
1619 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001620 protocol: protocol,
1621 name: "Basic-Client-RenewTicket" + suffix,
David Benjaminbed9aae2014-08-07 19:13:38 -04001622 config: Config{
1623 Bugs: ProtocolBugs{
1624 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1625 RenewTicketOnResume: true,
1626 },
1627 },
1628 flags: flags,
1629 resumeSession: true,
David Benjamin43ec06f2014-08-05 02:28:57 -04001630 })
1631 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001632 protocol: protocol,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001633 name: "Basic-Client-NoTicket" + suffix,
1634 config: Config{
1635 SessionTicketsDisabled: true,
1636 Bugs: ProtocolBugs{
1637 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1638 },
1639 },
1640 flags: flags,
1641 resumeSession: true,
1642 })
1643 testCases = append(testCases, testCase{
1644 protocol: protocol,
David Benjamine0e7d0d2015-02-08 19:33:25 -05001645 name: "Basic-Client-Implicit" + suffix,
1646 config: Config{
1647 Bugs: ProtocolBugs{
1648 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1649 },
1650 },
1651 flags: append(flags, "-implicit-handshake"),
1652 resumeSession: true,
1653 })
1654 testCases = append(testCases, testCase{
1655 protocol: protocol,
David Benjamin43ec06f2014-08-05 02:28:57 -04001656 testType: serverTest,
1657 name: "Basic-Server" + suffix,
1658 config: Config{
1659 Bugs: ProtocolBugs{
1660 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1661 },
1662 },
David Benjaminbed9aae2014-08-07 19:13:38 -04001663 flags: flags,
1664 resumeSession: true,
David Benjamin43ec06f2014-08-05 02:28:57 -04001665 })
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001666 testCases = append(testCases, testCase{
1667 protocol: protocol,
1668 testType: serverTest,
1669 name: "Basic-Server-NoTickets" + suffix,
1670 config: Config{
1671 SessionTicketsDisabled: true,
1672 Bugs: ProtocolBugs{
1673 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1674 },
1675 },
1676 flags: flags,
1677 resumeSession: true,
1678 })
David Benjamine0e7d0d2015-02-08 19:33:25 -05001679 testCases = append(testCases, testCase{
1680 protocol: protocol,
1681 testType: serverTest,
1682 name: "Basic-Server-Implicit" + suffix,
1683 config: Config{
1684 Bugs: ProtocolBugs{
1685 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1686 },
1687 },
1688 flags: append(flags, "-implicit-handshake"),
1689 resumeSession: true,
1690 })
David Benjamin6f5c0f42015-02-24 01:23:21 -05001691 testCases = append(testCases, testCase{
1692 protocol: protocol,
1693 testType: serverTest,
1694 name: "Basic-Server-EarlyCallback" + suffix,
1695 config: Config{
1696 Bugs: ProtocolBugs{
1697 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1698 },
1699 },
1700 flags: append(flags, "-use-early-callback"),
1701 resumeSession: true,
1702 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001703
David Benjamin6fd297b2014-08-11 18:43:38 -04001704 // TLS client auth.
1705 testCases = append(testCases, testCase{
1706 protocol: protocol,
1707 testType: clientTest,
1708 name: "ClientAuth-Client" + suffix,
1709 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001710 ClientAuth: RequireAnyClientCert,
David Benjamin6fd297b2014-08-11 18:43:38 -04001711 Bugs: ProtocolBugs{
1712 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1713 },
1714 },
1715 flags: append(flags,
1716 "-cert-file", rsaCertificateFile,
1717 "-key-file", rsaKeyFile),
1718 })
1719 testCases = append(testCases, testCase{
1720 protocol: protocol,
1721 testType: serverTest,
1722 name: "ClientAuth-Server" + suffix,
1723 config: Config{
1724 Certificates: []Certificate{rsaCertificate},
1725 },
1726 flags: append(flags, "-require-any-client-certificate"),
1727 })
1728
David Benjamin43ec06f2014-08-05 02:28:57 -04001729 // No session ticket support; server doesn't send NewSessionTicket.
1730 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001731 protocol: protocol,
1732 name: "SessionTicketsDisabled-Client" + suffix,
David Benjamin43ec06f2014-08-05 02:28:57 -04001733 config: Config{
1734 SessionTicketsDisabled: true,
1735 Bugs: ProtocolBugs{
1736 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1737 },
1738 },
1739 flags: flags,
1740 })
1741 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001742 protocol: protocol,
David Benjamin43ec06f2014-08-05 02:28:57 -04001743 testType: serverTest,
1744 name: "SessionTicketsDisabled-Server" + suffix,
1745 config: Config{
1746 SessionTicketsDisabled: true,
1747 Bugs: ProtocolBugs{
1748 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1749 },
1750 },
1751 flags: flags,
1752 })
1753
David Benjamin48cae082014-10-27 01:06:24 -04001754 // Skip ServerKeyExchange in PSK key exchange if there's no
1755 // identity hint.
1756 testCases = append(testCases, testCase{
1757 protocol: protocol,
1758 name: "EmptyPSKHint-Client" + suffix,
1759 config: Config{
1760 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
1761 PreSharedKey: []byte("secret"),
1762 Bugs: ProtocolBugs{
1763 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1764 },
1765 },
1766 flags: append(flags, "-psk", "secret"),
1767 })
1768 testCases = append(testCases, testCase{
1769 protocol: protocol,
1770 testType: serverTest,
1771 name: "EmptyPSKHint-Server" + suffix,
1772 config: Config{
1773 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
1774 PreSharedKey: []byte("secret"),
1775 Bugs: ProtocolBugs{
1776 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1777 },
1778 },
1779 flags: append(flags, "-psk", "secret"),
1780 })
1781
David Benjamin6fd297b2014-08-11 18:43:38 -04001782 if protocol == tls {
1783 // NPN on client and server; results in post-handshake message.
1784 testCases = append(testCases, testCase{
1785 protocol: protocol,
1786 name: "NPN-Client" + suffix,
1787 config: Config{
David Benjaminae2888f2014-09-06 12:58:58 -04001788 NextProtos: []string{"foo"},
David Benjamin6fd297b2014-08-11 18:43:38 -04001789 Bugs: ProtocolBugs{
1790 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1791 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001792 },
David Benjaminfc7b0862014-09-06 13:21:53 -04001793 flags: append(flags, "-select-next-proto", "foo"),
1794 expectedNextProto: "foo",
1795 expectedNextProtoType: npn,
David Benjamin6fd297b2014-08-11 18:43:38 -04001796 })
1797 testCases = append(testCases, testCase{
1798 protocol: protocol,
1799 testType: serverTest,
1800 name: "NPN-Server" + suffix,
1801 config: Config{
1802 NextProtos: []string{"bar"},
1803 Bugs: ProtocolBugs{
1804 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1805 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001806 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001807 flags: append(flags,
1808 "-advertise-npn", "\x03foo\x03bar\x03baz",
1809 "-expect-next-proto", "bar"),
David Benjaminfc7b0862014-09-06 13:21:53 -04001810 expectedNextProto: "bar",
1811 expectedNextProtoType: npn,
David Benjamin6fd297b2014-08-11 18:43:38 -04001812 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001813
David Benjamin195dc782015-02-19 13:27:05 -05001814 // TODO(davidben): Add tests for when False Start doesn't trigger.
1815
David Benjamin6fd297b2014-08-11 18:43:38 -04001816 // Client does False Start and negotiates NPN.
1817 testCases = append(testCases, testCase{
1818 protocol: protocol,
1819 name: "FalseStart" + suffix,
1820 config: Config{
1821 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1822 NextProtos: []string{"foo"},
1823 Bugs: ProtocolBugs{
David Benjamine58c4f52014-08-24 03:47:07 -04001824 ExpectFalseStart: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001825 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1826 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001827 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001828 flags: append(flags,
1829 "-false-start",
1830 "-select-next-proto", "foo"),
David Benjamine58c4f52014-08-24 03:47:07 -04001831 shimWritesFirst: true,
1832 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001833 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001834
David Benjaminae2888f2014-09-06 12:58:58 -04001835 // Client does False Start and negotiates ALPN.
1836 testCases = append(testCases, testCase{
1837 protocol: protocol,
1838 name: "FalseStart-ALPN" + suffix,
1839 config: Config{
1840 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1841 NextProtos: []string{"foo"},
1842 Bugs: ProtocolBugs{
1843 ExpectFalseStart: true,
1844 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1845 },
1846 },
1847 flags: append(flags,
1848 "-false-start",
1849 "-advertise-alpn", "\x03foo"),
1850 shimWritesFirst: true,
1851 resumeSession: true,
1852 })
1853
David Benjamin931ab342015-02-08 19:46:57 -05001854 // Client does False Start but doesn't explicitly call
1855 // SSL_connect.
1856 testCases = append(testCases, testCase{
1857 protocol: protocol,
1858 name: "FalseStart-Implicit" + suffix,
1859 config: Config{
1860 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1861 NextProtos: []string{"foo"},
1862 Bugs: ProtocolBugs{
1863 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1864 },
1865 },
1866 flags: append(flags,
1867 "-implicit-handshake",
1868 "-false-start",
1869 "-advertise-alpn", "\x03foo"),
1870 })
1871
David Benjamin6fd297b2014-08-11 18:43:38 -04001872 // False Start without session tickets.
1873 testCases = append(testCases, testCase{
David Benjamin5f237bc2015-02-11 17:14:15 -05001874 name: "FalseStart-SessionTicketsDisabled" + suffix,
David Benjamin6fd297b2014-08-11 18:43:38 -04001875 config: Config{
1876 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1877 NextProtos: []string{"foo"},
1878 SessionTicketsDisabled: true,
David Benjamin4e99c522014-08-24 01:45:30 -04001879 Bugs: ProtocolBugs{
David Benjamine58c4f52014-08-24 03:47:07 -04001880 ExpectFalseStart: true,
David Benjamin4e99c522014-08-24 01:45:30 -04001881 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1882 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001883 },
David Benjamin4e99c522014-08-24 01:45:30 -04001884 flags: append(flags,
David Benjamin6fd297b2014-08-11 18:43:38 -04001885 "-false-start",
1886 "-select-next-proto", "foo",
David Benjamin4e99c522014-08-24 01:45:30 -04001887 ),
David Benjamine58c4f52014-08-24 03:47:07 -04001888 shimWritesFirst: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001889 })
David Benjamin1e7f8d72014-08-08 12:27:04 -04001890
David Benjamina08e49d2014-08-24 01:46:07 -04001891 // Server parses a V2ClientHello.
David Benjamin6fd297b2014-08-11 18:43:38 -04001892 testCases = append(testCases, testCase{
1893 protocol: protocol,
1894 testType: serverTest,
1895 name: "SendV2ClientHello" + suffix,
1896 config: Config{
1897 // Choose a cipher suite that does not involve
1898 // elliptic curves, so no extensions are
1899 // involved.
1900 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
1901 Bugs: ProtocolBugs{
1902 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1903 SendV2ClientHello: true,
1904 },
David Benjamin1e7f8d72014-08-08 12:27:04 -04001905 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001906 flags: flags,
1907 })
David Benjamina08e49d2014-08-24 01:46:07 -04001908
1909 // Client sends a Channel ID.
1910 testCases = append(testCases, testCase{
1911 protocol: protocol,
1912 name: "ChannelID-Client" + suffix,
1913 config: Config{
1914 RequestChannelID: true,
1915 Bugs: ProtocolBugs{
1916 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1917 },
1918 },
1919 flags: append(flags,
1920 "-send-channel-id", channelIDKeyFile,
1921 ),
1922 resumeSession: true,
1923 expectChannelID: true,
1924 })
1925
1926 // Server accepts a Channel ID.
1927 testCases = append(testCases, testCase{
1928 protocol: protocol,
1929 testType: serverTest,
1930 name: "ChannelID-Server" + suffix,
1931 config: Config{
1932 ChannelID: channelIDKey,
1933 Bugs: ProtocolBugs{
1934 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1935 },
1936 },
1937 flags: append(flags,
1938 "-expect-channel-id",
1939 base64.StdEncoding.EncodeToString(channelIDBytes),
1940 ),
1941 resumeSession: true,
1942 expectChannelID: true,
1943 })
David Benjamin6fd297b2014-08-11 18:43:38 -04001944 } else {
1945 testCases = append(testCases, testCase{
1946 protocol: protocol,
1947 name: "SkipHelloVerifyRequest" + suffix,
1948 config: Config{
1949 Bugs: ProtocolBugs{
1950 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1951 SkipHelloVerifyRequest: true,
1952 },
1953 },
1954 flags: flags,
1955 })
David Benjamin6fd297b2014-08-11 18:43:38 -04001956 }
David Benjamin43ec06f2014-08-05 02:28:57 -04001957}
1958
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001959func addVersionNegotiationTests() {
1960 for i, shimVers := range tlsVersions {
1961 // Assemble flags to disable all newer versions on the shim.
1962 var flags []string
1963 for _, vers := range tlsVersions[i+1:] {
1964 flags = append(flags, vers.flag)
1965 }
1966
1967 for _, runnerVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05001968 protocols := []protocol{tls}
1969 if runnerVers.hasDTLS && shimVers.hasDTLS {
1970 protocols = append(protocols, dtls)
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001971 }
David Benjamin8b8c0062014-11-23 02:47:52 -05001972 for _, protocol := range protocols {
1973 expectedVersion := shimVers.version
1974 if runnerVers.version < shimVers.version {
1975 expectedVersion = runnerVers.version
1976 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001977
David Benjamin8b8c0062014-11-23 02:47:52 -05001978 suffix := shimVers.name + "-" + runnerVers.name
1979 if protocol == dtls {
1980 suffix += "-DTLS"
1981 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001982
David Benjamin1eb367c2014-12-12 18:17:51 -05001983 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
1984
David Benjamin1e29a6b2014-12-10 02:27:24 -05001985 clientVers := shimVers.version
1986 if clientVers > VersionTLS10 {
1987 clientVers = VersionTLS10
1988 }
David Benjamin8b8c0062014-11-23 02:47:52 -05001989 testCases = append(testCases, testCase{
1990 protocol: protocol,
1991 testType: clientTest,
1992 name: "VersionNegotiation-Client-" + suffix,
1993 config: Config{
1994 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05001995 Bugs: ProtocolBugs{
1996 ExpectInitialRecordVersion: clientVers,
1997 },
David Benjamin8b8c0062014-11-23 02:47:52 -05001998 },
1999 flags: flags,
2000 expectedVersion: expectedVersion,
2001 })
David Benjamin1eb367c2014-12-12 18:17:51 -05002002 testCases = append(testCases, testCase{
2003 protocol: protocol,
2004 testType: clientTest,
2005 name: "VersionNegotiation-Client2-" + suffix,
2006 config: Config{
2007 MaxVersion: runnerVers.version,
2008 Bugs: ProtocolBugs{
2009 ExpectInitialRecordVersion: clientVers,
2010 },
2011 },
2012 flags: []string{"-max-version", shimVersFlag},
2013 expectedVersion: expectedVersion,
2014 })
David Benjamin8b8c0062014-11-23 02:47:52 -05002015
2016 testCases = append(testCases, testCase{
2017 protocol: protocol,
2018 testType: serverTest,
2019 name: "VersionNegotiation-Server-" + suffix,
2020 config: Config{
2021 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05002022 Bugs: ProtocolBugs{
2023 ExpectInitialRecordVersion: expectedVersion,
2024 },
David Benjamin8b8c0062014-11-23 02:47:52 -05002025 },
2026 flags: flags,
2027 expectedVersion: expectedVersion,
2028 })
David Benjamin1eb367c2014-12-12 18:17:51 -05002029 testCases = append(testCases, testCase{
2030 protocol: protocol,
2031 testType: serverTest,
2032 name: "VersionNegotiation-Server2-" + suffix,
2033 config: Config{
2034 MaxVersion: runnerVers.version,
2035 Bugs: ProtocolBugs{
2036 ExpectInitialRecordVersion: expectedVersion,
2037 },
2038 },
2039 flags: []string{"-max-version", shimVersFlag},
2040 expectedVersion: expectedVersion,
2041 })
David Benjamin8b8c0062014-11-23 02:47:52 -05002042 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002043 }
2044 }
2045}
2046
David Benjaminaccb4542014-12-12 23:44:33 -05002047func addMinimumVersionTests() {
2048 for i, shimVers := range tlsVersions {
2049 // Assemble flags to disable all older versions on the shim.
2050 var flags []string
2051 for _, vers := range tlsVersions[:i] {
2052 flags = append(flags, vers.flag)
2053 }
2054
2055 for _, runnerVers := range tlsVersions {
2056 protocols := []protocol{tls}
2057 if runnerVers.hasDTLS && shimVers.hasDTLS {
2058 protocols = append(protocols, dtls)
2059 }
2060 for _, protocol := range protocols {
2061 suffix := shimVers.name + "-" + runnerVers.name
2062 if protocol == dtls {
2063 suffix += "-DTLS"
2064 }
2065 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
2066
David Benjaminaccb4542014-12-12 23:44:33 -05002067 var expectedVersion uint16
2068 var shouldFail bool
2069 var expectedError string
David Benjamin87909c02014-12-13 01:55:01 -05002070 var expectedLocalError string
David Benjaminaccb4542014-12-12 23:44:33 -05002071 if runnerVers.version >= shimVers.version {
2072 expectedVersion = runnerVers.version
2073 } else {
2074 shouldFail = true
2075 expectedError = ":UNSUPPORTED_PROTOCOL:"
David Benjamin87909c02014-12-13 01:55:01 -05002076 if runnerVers.version > VersionSSL30 {
2077 expectedLocalError = "remote error: protocol version not supported"
2078 } else {
2079 expectedLocalError = "remote error: handshake failure"
2080 }
David Benjaminaccb4542014-12-12 23:44:33 -05002081 }
2082
2083 testCases = append(testCases, testCase{
2084 protocol: protocol,
2085 testType: clientTest,
2086 name: "MinimumVersion-Client-" + suffix,
2087 config: Config{
2088 MaxVersion: runnerVers.version,
2089 },
David Benjamin87909c02014-12-13 01:55:01 -05002090 flags: flags,
2091 expectedVersion: expectedVersion,
2092 shouldFail: shouldFail,
2093 expectedError: expectedError,
2094 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002095 })
2096 testCases = append(testCases, testCase{
2097 protocol: protocol,
2098 testType: clientTest,
2099 name: "MinimumVersion-Client2-" + suffix,
2100 config: Config{
2101 MaxVersion: runnerVers.version,
2102 },
David Benjamin87909c02014-12-13 01:55:01 -05002103 flags: []string{"-min-version", shimVersFlag},
2104 expectedVersion: expectedVersion,
2105 shouldFail: shouldFail,
2106 expectedError: expectedError,
2107 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002108 })
2109
2110 testCases = append(testCases, testCase{
2111 protocol: protocol,
2112 testType: serverTest,
2113 name: "MinimumVersion-Server-" + suffix,
2114 config: Config{
2115 MaxVersion: runnerVers.version,
2116 },
David Benjamin87909c02014-12-13 01:55:01 -05002117 flags: flags,
2118 expectedVersion: expectedVersion,
2119 shouldFail: shouldFail,
2120 expectedError: expectedError,
2121 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002122 })
2123 testCases = append(testCases, testCase{
2124 protocol: protocol,
2125 testType: serverTest,
2126 name: "MinimumVersion-Server2-" + suffix,
2127 config: Config{
2128 MaxVersion: runnerVers.version,
2129 },
David Benjamin87909c02014-12-13 01:55:01 -05002130 flags: []string{"-min-version", shimVersFlag},
2131 expectedVersion: expectedVersion,
2132 shouldFail: shouldFail,
2133 expectedError: expectedError,
2134 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002135 })
2136 }
2137 }
2138 }
2139}
2140
David Benjamin5c24a1d2014-08-31 00:59:27 -04002141func addD5BugTests() {
2142 testCases = append(testCases, testCase{
2143 testType: serverTest,
2144 name: "D5Bug-NoQuirk-Reject",
2145 config: Config{
2146 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2147 Bugs: ProtocolBugs{
2148 SSL3RSAKeyExchange: true,
2149 },
2150 },
2151 shouldFail: true,
2152 expectedError: ":TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG:",
2153 })
2154 testCases = append(testCases, testCase{
2155 testType: serverTest,
2156 name: "D5Bug-Quirk-Normal",
2157 config: Config{
2158 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2159 },
2160 flags: []string{"-tls-d5-bug"},
2161 })
2162 testCases = append(testCases, testCase{
2163 testType: serverTest,
2164 name: "D5Bug-Quirk-Bug",
2165 config: Config{
2166 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2167 Bugs: ProtocolBugs{
2168 SSL3RSAKeyExchange: true,
2169 },
2170 },
2171 flags: []string{"-tls-d5-bug"},
2172 })
2173}
2174
David Benjamine78bfde2014-09-06 12:45:15 -04002175func addExtensionTests() {
2176 testCases = append(testCases, testCase{
2177 testType: clientTest,
2178 name: "DuplicateExtensionClient",
2179 config: Config{
2180 Bugs: ProtocolBugs{
2181 DuplicateExtension: true,
2182 },
2183 },
2184 shouldFail: true,
2185 expectedLocalError: "remote error: error decoding message",
2186 })
2187 testCases = append(testCases, testCase{
2188 testType: serverTest,
2189 name: "DuplicateExtensionServer",
2190 config: Config{
2191 Bugs: ProtocolBugs{
2192 DuplicateExtension: true,
2193 },
2194 },
2195 shouldFail: true,
2196 expectedLocalError: "remote error: error decoding message",
2197 })
2198 testCases = append(testCases, testCase{
2199 testType: clientTest,
2200 name: "ServerNameExtensionClient",
2201 config: Config{
2202 Bugs: ProtocolBugs{
2203 ExpectServerName: "example.com",
2204 },
2205 },
2206 flags: []string{"-host-name", "example.com"},
2207 })
2208 testCases = append(testCases, testCase{
2209 testType: clientTest,
David Benjamin5f237bc2015-02-11 17:14:15 -05002210 name: "ServerNameExtensionClientMismatch",
David Benjamine78bfde2014-09-06 12:45:15 -04002211 config: Config{
2212 Bugs: ProtocolBugs{
2213 ExpectServerName: "mismatch.com",
2214 },
2215 },
2216 flags: []string{"-host-name", "example.com"},
2217 shouldFail: true,
2218 expectedLocalError: "tls: unexpected server name",
2219 })
2220 testCases = append(testCases, testCase{
2221 testType: clientTest,
David Benjamin5f237bc2015-02-11 17:14:15 -05002222 name: "ServerNameExtensionClientMissing",
David Benjamine78bfde2014-09-06 12:45:15 -04002223 config: Config{
2224 Bugs: ProtocolBugs{
2225 ExpectServerName: "missing.com",
2226 },
2227 },
2228 shouldFail: true,
2229 expectedLocalError: "tls: unexpected server name",
2230 })
2231 testCases = append(testCases, testCase{
2232 testType: serverTest,
2233 name: "ServerNameExtensionServer",
2234 config: Config{
2235 ServerName: "example.com",
2236 },
2237 flags: []string{"-expect-server-name", "example.com"},
2238 resumeSession: true,
2239 })
David Benjaminae2888f2014-09-06 12:58:58 -04002240 testCases = append(testCases, testCase{
2241 testType: clientTest,
2242 name: "ALPNClient",
2243 config: Config{
2244 NextProtos: []string{"foo"},
2245 },
2246 flags: []string{
2247 "-advertise-alpn", "\x03foo\x03bar\x03baz",
2248 "-expect-alpn", "foo",
2249 },
David Benjaminfc7b0862014-09-06 13:21:53 -04002250 expectedNextProto: "foo",
2251 expectedNextProtoType: alpn,
2252 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04002253 })
2254 testCases = append(testCases, testCase{
2255 testType: serverTest,
2256 name: "ALPNServer",
2257 config: Config{
2258 NextProtos: []string{"foo", "bar", "baz"},
2259 },
2260 flags: []string{
2261 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2262 "-select-alpn", "foo",
2263 },
David Benjaminfc7b0862014-09-06 13:21:53 -04002264 expectedNextProto: "foo",
2265 expectedNextProtoType: alpn,
2266 resumeSession: true,
2267 })
2268 // Test that the server prefers ALPN over NPN.
2269 testCases = append(testCases, testCase{
2270 testType: serverTest,
2271 name: "ALPNServer-Preferred",
2272 config: Config{
2273 NextProtos: []string{"foo", "bar", "baz"},
2274 },
2275 flags: []string{
2276 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2277 "-select-alpn", "foo",
2278 "-advertise-npn", "\x03foo\x03bar\x03baz",
2279 },
2280 expectedNextProto: "foo",
2281 expectedNextProtoType: alpn,
2282 resumeSession: true,
2283 })
2284 testCases = append(testCases, testCase{
2285 testType: serverTest,
2286 name: "ALPNServer-Preferred-Swapped",
2287 config: Config{
2288 NextProtos: []string{"foo", "bar", "baz"},
2289 Bugs: ProtocolBugs{
2290 SwapNPNAndALPN: true,
2291 },
2292 },
2293 flags: []string{
2294 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2295 "-select-alpn", "foo",
2296 "-advertise-npn", "\x03foo\x03bar\x03baz",
2297 },
2298 expectedNextProto: "foo",
2299 expectedNextProtoType: alpn,
2300 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04002301 })
Adam Langley38311732014-10-16 19:04:35 -07002302 // Resume with a corrupt ticket.
2303 testCases = append(testCases, testCase{
2304 testType: serverTest,
2305 name: "CorruptTicket",
2306 config: Config{
2307 Bugs: ProtocolBugs{
2308 CorruptTicket: true,
2309 },
2310 },
2311 resumeSession: true,
2312 flags: []string{"-expect-session-miss"},
2313 })
2314 // Resume with an oversized session id.
2315 testCases = append(testCases, testCase{
2316 testType: serverTest,
2317 name: "OversizedSessionId",
2318 config: Config{
2319 Bugs: ProtocolBugs{
2320 OversizedSessionId: true,
2321 },
2322 },
2323 resumeSession: true,
Adam Langley75712922014-10-10 16:23:43 -07002324 shouldFail: true,
Adam Langley38311732014-10-16 19:04:35 -07002325 expectedError: ":DECODE_ERROR:",
2326 })
David Benjaminca6c8262014-11-15 19:06:08 -05002327 // Basic DTLS-SRTP tests. Include fake profiles to ensure they
2328 // are ignored.
2329 testCases = append(testCases, testCase{
2330 protocol: dtls,
2331 name: "SRTP-Client",
2332 config: Config{
2333 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
2334 },
2335 flags: []string{
2336 "-srtp-profiles",
2337 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2338 },
2339 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2340 })
2341 testCases = append(testCases, testCase{
2342 protocol: dtls,
2343 testType: serverTest,
2344 name: "SRTP-Server",
2345 config: Config{
2346 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
2347 },
2348 flags: []string{
2349 "-srtp-profiles",
2350 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2351 },
2352 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2353 })
2354 // Test that the MKI is ignored.
2355 testCases = append(testCases, testCase{
2356 protocol: dtls,
2357 testType: serverTest,
2358 name: "SRTP-Server-IgnoreMKI",
2359 config: Config{
2360 SRTPProtectionProfiles: []uint16{SRTP_AES128_CM_HMAC_SHA1_80},
2361 Bugs: ProtocolBugs{
2362 SRTPMasterKeyIdentifer: "bogus",
2363 },
2364 },
2365 flags: []string{
2366 "-srtp-profiles",
2367 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2368 },
2369 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2370 })
2371 // Test that SRTP isn't negotiated on the server if there were
2372 // no matching profiles.
2373 testCases = append(testCases, testCase{
2374 protocol: dtls,
2375 testType: serverTest,
2376 name: "SRTP-Server-NoMatch",
2377 config: Config{
2378 SRTPProtectionProfiles: []uint16{100, 101, 102},
2379 },
2380 flags: []string{
2381 "-srtp-profiles",
2382 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2383 },
2384 expectedSRTPProtectionProfile: 0,
2385 })
2386 // Test that the server returning an invalid SRTP profile is
2387 // flagged as an error by the client.
2388 testCases = append(testCases, testCase{
2389 protocol: dtls,
2390 name: "SRTP-Client-NoMatch",
2391 config: Config{
2392 Bugs: ProtocolBugs{
2393 SendSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_32,
2394 },
2395 },
2396 flags: []string{
2397 "-srtp-profiles",
2398 "SRTP_AES128_CM_SHA1_80",
2399 },
2400 shouldFail: true,
2401 expectedError: ":BAD_SRTP_PROTECTION_PROFILE_LIST:",
2402 })
David Benjamin61f95272014-11-25 01:55:35 -05002403 // Test OCSP stapling and SCT list.
2404 testCases = append(testCases, testCase{
2405 name: "OCSPStapling",
2406 flags: []string{
2407 "-enable-ocsp-stapling",
2408 "-expect-ocsp-response",
2409 base64.StdEncoding.EncodeToString(testOCSPResponse),
2410 },
2411 })
2412 testCases = append(testCases, testCase{
2413 name: "SignedCertificateTimestampList",
2414 flags: []string{
2415 "-enable-signed-cert-timestamps",
2416 "-expect-signed-cert-timestamps",
2417 base64.StdEncoding.EncodeToString(testSCTList),
2418 },
2419 })
David Benjamine78bfde2014-09-06 12:45:15 -04002420}
2421
David Benjamin01fe8202014-09-24 15:21:44 -04002422func addResumptionVersionTests() {
David Benjamin01fe8202014-09-24 15:21:44 -04002423 for _, sessionVers := range tlsVersions {
David Benjamin01fe8202014-09-24 15:21:44 -04002424 for _, resumeVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05002425 protocols := []protocol{tls}
2426 if sessionVers.hasDTLS && resumeVers.hasDTLS {
2427 protocols = append(protocols, dtls)
David Benjaminbdf5e722014-11-11 00:52:15 -05002428 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002429 for _, protocol := range protocols {
2430 suffix := "-" + sessionVers.name + "-" + resumeVers.name
2431 if protocol == dtls {
2432 suffix += "-DTLS"
2433 }
2434
2435 testCases = append(testCases, testCase{
2436 protocol: protocol,
2437 name: "Resume-Client" + suffix,
2438 resumeSession: true,
2439 config: Config{
2440 MaxVersion: sessionVers.version,
2441 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2442 Bugs: ProtocolBugs{
2443 AllowSessionVersionMismatch: true,
2444 },
2445 },
2446 expectedVersion: sessionVers.version,
2447 resumeConfig: &Config{
2448 MaxVersion: resumeVers.version,
2449 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2450 Bugs: ProtocolBugs{
2451 AllowSessionVersionMismatch: true,
2452 },
2453 },
2454 expectedResumeVersion: resumeVers.version,
2455 })
2456
2457 testCases = append(testCases, testCase{
2458 protocol: protocol,
2459 name: "Resume-Client-NoResume" + suffix,
2460 flags: []string{"-expect-session-miss"},
2461 resumeSession: true,
2462 config: Config{
2463 MaxVersion: sessionVers.version,
2464 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2465 },
2466 expectedVersion: sessionVers.version,
2467 resumeConfig: &Config{
2468 MaxVersion: resumeVers.version,
2469 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2470 },
2471 newSessionsOnResume: true,
2472 expectedResumeVersion: resumeVers.version,
2473 })
2474
2475 var flags []string
2476 if sessionVers.version != resumeVers.version {
2477 flags = append(flags, "-expect-session-miss")
2478 }
2479 testCases = append(testCases, testCase{
2480 protocol: protocol,
2481 testType: serverTest,
2482 name: "Resume-Server" + suffix,
2483 flags: flags,
2484 resumeSession: true,
2485 config: Config{
2486 MaxVersion: sessionVers.version,
2487 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2488 },
2489 expectedVersion: sessionVers.version,
2490 resumeConfig: &Config{
2491 MaxVersion: resumeVers.version,
2492 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2493 },
2494 expectedResumeVersion: resumeVers.version,
2495 })
2496 }
David Benjamin01fe8202014-09-24 15:21:44 -04002497 }
2498 }
2499}
2500
Adam Langley2ae77d22014-10-28 17:29:33 -07002501func addRenegotiationTests() {
2502 testCases = append(testCases, testCase{
2503 testType: serverTest,
2504 name: "Renegotiate-Server",
2505 flags: []string{"-renegotiate"},
2506 shimWritesFirst: true,
2507 })
2508 testCases = append(testCases, testCase{
2509 testType: serverTest,
2510 name: "Renegotiate-Server-EmptyExt",
2511 config: Config{
2512 Bugs: ProtocolBugs{
2513 EmptyRenegotiationInfo: true,
2514 },
2515 },
2516 flags: []string{"-renegotiate"},
2517 shimWritesFirst: true,
2518 shouldFail: true,
2519 expectedError: ":RENEGOTIATION_MISMATCH:",
2520 })
2521 testCases = append(testCases, testCase{
2522 testType: serverTest,
2523 name: "Renegotiate-Server-BadExt",
2524 config: Config{
2525 Bugs: ProtocolBugs{
2526 BadRenegotiationInfo: true,
2527 },
2528 },
2529 flags: []string{"-renegotiate"},
2530 shimWritesFirst: true,
2531 shouldFail: true,
2532 expectedError: ":RENEGOTIATION_MISMATCH:",
2533 })
David Benjaminca6554b2014-11-08 12:31:52 -05002534 testCases = append(testCases, testCase{
2535 testType: serverTest,
2536 name: "Renegotiate-Server-ClientInitiated",
2537 renegotiate: true,
2538 })
2539 testCases = append(testCases, testCase{
2540 testType: serverTest,
2541 name: "Renegotiate-Server-ClientInitiated-NoExt",
2542 renegotiate: true,
2543 config: Config{
2544 Bugs: ProtocolBugs{
2545 NoRenegotiationInfo: true,
2546 },
2547 },
2548 shouldFail: true,
2549 expectedError: ":UNSAFE_LEGACY_RENEGOTIATION_DISABLED:",
2550 })
2551 testCases = append(testCases, testCase{
2552 testType: serverTest,
2553 name: "Renegotiate-Server-ClientInitiated-NoExt-Allowed",
2554 renegotiate: true,
2555 config: Config{
2556 Bugs: ProtocolBugs{
2557 NoRenegotiationInfo: true,
2558 },
2559 },
2560 flags: []string{"-allow-unsafe-legacy-renegotiation"},
2561 })
Adam Langley2ae77d22014-10-28 17:29:33 -07002562 // TODO(agl): test the renegotiation info SCSV.
Adam Langleycf2d4f42014-10-28 19:06:14 -07002563 testCases = append(testCases, testCase{
2564 name: "Renegotiate-Client",
2565 renegotiate: true,
2566 })
2567 testCases = append(testCases, testCase{
2568 name: "Renegotiate-Client-EmptyExt",
2569 renegotiate: true,
2570 config: Config{
2571 Bugs: ProtocolBugs{
2572 EmptyRenegotiationInfo: true,
2573 },
2574 },
2575 shouldFail: true,
2576 expectedError: ":RENEGOTIATION_MISMATCH:",
2577 })
2578 testCases = append(testCases, testCase{
2579 name: "Renegotiate-Client-BadExt",
2580 renegotiate: true,
2581 config: Config{
2582 Bugs: ProtocolBugs{
2583 BadRenegotiationInfo: true,
2584 },
2585 },
2586 shouldFail: true,
2587 expectedError: ":RENEGOTIATION_MISMATCH:",
2588 })
2589 testCases = append(testCases, testCase{
2590 name: "Renegotiate-Client-SwitchCiphers",
2591 renegotiate: true,
2592 config: Config{
2593 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2594 },
2595 renegotiateCiphers: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2596 })
2597 testCases = append(testCases, testCase{
2598 name: "Renegotiate-Client-SwitchCiphers2",
2599 renegotiate: true,
2600 config: Config{
2601 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2602 },
2603 renegotiateCiphers: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2604 })
David Benjaminc44b1df2014-11-23 12:11:01 -05002605 testCases = append(testCases, testCase{
2606 name: "Renegotiate-SameClientVersion",
2607 renegotiate: true,
2608 config: Config{
2609 MaxVersion: VersionTLS10,
2610 Bugs: ProtocolBugs{
2611 RequireSameRenegoClientVersion: true,
2612 },
2613 },
2614 })
Adam Langley2ae77d22014-10-28 17:29:33 -07002615}
2616
David Benjamin5e961c12014-11-07 01:48:35 -05002617func addDTLSReplayTests() {
2618 // Test that sequence number replays are detected.
2619 testCases = append(testCases, testCase{
2620 protocol: dtls,
2621 name: "DTLS-Replay",
2622 replayWrites: true,
2623 })
2624
2625 // Test the outgoing sequence number skipping by values larger
2626 // than the retransmit window.
2627 testCases = append(testCases, testCase{
2628 protocol: dtls,
2629 name: "DTLS-Replay-LargeGaps",
2630 config: Config{
2631 Bugs: ProtocolBugs{
2632 SequenceNumberIncrement: 127,
2633 },
2634 },
2635 replayWrites: true,
2636 })
2637}
2638
Feng Lu41aa3252014-11-21 22:47:56 -08002639func addFastRadioPaddingTests() {
David Benjamin1e29a6b2014-12-10 02:27:24 -05002640 testCases = append(testCases, testCase{
2641 protocol: tls,
2642 name: "FastRadio-Padding",
Feng Lu41aa3252014-11-21 22:47:56 -08002643 config: Config{
2644 Bugs: ProtocolBugs{
2645 RequireFastradioPadding: true,
2646 },
2647 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05002648 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08002649 })
David Benjamin1e29a6b2014-12-10 02:27:24 -05002650 testCases = append(testCases, testCase{
2651 protocol: dtls,
David Benjamin5f237bc2015-02-11 17:14:15 -05002652 name: "FastRadio-Padding-DTLS",
Feng Lu41aa3252014-11-21 22:47:56 -08002653 config: Config{
2654 Bugs: ProtocolBugs{
2655 RequireFastradioPadding: true,
2656 },
2657 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05002658 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08002659 })
2660}
2661
David Benjamin000800a2014-11-14 01:43:59 -05002662var testHashes = []struct {
2663 name string
2664 id uint8
2665}{
2666 {"SHA1", hashSHA1},
2667 {"SHA224", hashSHA224},
2668 {"SHA256", hashSHA256},
2669 {"SHA384", hashSHA384},
2670 {"SHA512", hashSHA512},
2671}
2672
2673func addSigningHashTests() {
2674 // Make sure each hash works. Include some fake hashes in the list and
2675 // ensure they're ignored.
2676 for _, hash := range testHashes {
2677 testCases = append(testCases, testCase{
2678 name: "SigningHash-ClientAuth-" + hash.name,
2679 config: Config{
2680 ClientAuth: RequireAnyClientCert,
2681 SignatureAndHashes: []signatureAndHash{
2682 {signatureRSA, 42},
2683 {signatureRSA, hash.id},
2684 {signatureRSA, 255},
2685 },
2686 },
2687 flags: []string{
2688 "-cert-file", rsaCertificateFile,
2689 "-key-file", rsaKeyFile,
2690 },
2691 })
2692
2693 testCases = append(testCases, testCase{
2694 testType: serverTest,
2695 name: "SigningHash-ServerKeyExchange-Sign-" + hash.name,
2696 config: Config{
2697 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2698 SignatureAndHashes: []signatureAndHash{
2699 {signatureRSA, 42},
2700 {signatureRSA, hash.id},
2701 {signatureRSA, 255},
2702 },
2703 },
2704 })
2705 }
2706
2707 // Test that hash resolution takes the signature type into account.
2708 testCases = append(testCases, testCase{
2709 name: "SigningHash-ClientAuth-SignatureType",
2710 config: Config{
2711 ClientAuth: RequireAnyClientCert,
2712 SignatureAndHashes: []signatureAndHash{
2713 {signatureECDSA, hashSHA512},
2714 {signatureRSA, hashSHA384},
2715 {signatureECDSA, hashSHA1},
2716 },
2717 },
2718 flags: []string{
2719 "-cert-file", rsaCertificateFile,
2720 "-key-file", rsaKeyFile,
2721 },
2722 })
2723
2724 testCases = append(testCases, testCase{
2725 testType: serverTest,
2726 name: "SigningHash-ServerKeyExchange-SignatureType",
2727 config: Config{
2728 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2729 SignatureAndHashes: []signatureAndHash{
2730 {signatureECDSA, hashSHA512},
2731 {signatureRSA, hashSHA384},
2732 {signatureECDSA, hashSHA1},
2733 },
2734 },
2735 })
2736
2737 // Test that, if the list is missing, the peer falls back to SHA-1.
2738 testCases = append(testCases, testCase{
2739 name: "SigningHash-ClientAuth-Fallback",
2740 config: Config{
2741 ClientAuth: RequireAnyClientCert,
2742 SignatureAndHashes: []signatureAndHash{
2743 {signatureRSA, hashSHA1},
2744 },
2745 Bugs: ProtocolBugs{
2746 NoSignatureAndHashes: true,
2747 },
2748 },
2749 flags: []string{
2750 "-cert-file", rsaCertificateFile,
2751 "-key-file", rsaKeyFile,
2752 },
2753 })
2754
2755 testCases = append(testCases, testCase{
2756 testType: serverTest,
2757 name: "SigningHash-ServerKeyExchange-Fallback",
2758 config: Config{
2759 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2760 SignatureAndHashes: []signatureAndHash{
2761 {signatureRSA, hashSHA1},
2762 },
2763 Bugs: ProtocolBugs{
2764 NoSignatureAndHashes: true,
2765 },
2766 },
2767 })
2768}
2769
David Benjamin83f90402015-01-27 01:09:43 -05002770// timeouts is the retransmit schedule for BoringSSL. It doubles and
2771// caps at 60 seconds. On the 13th timeout, it gives up.
2772var timeouts = []time.Duration{
2773 1 * time.Second,
2774 2 * time.Second,
2775 4 * time.Second,
2776 8 * time.Second,
2777 16 * time.Second,
2778 32 * time.Second,
2779 60 * time.Second,
2780 60 * time.Second,
2781 60 * time.Second,
2782 60 * time.Second,
2783 60 * time.Second,
2784 60 * time.Second,
2785 60 * time.Second,
2786}
2787
2788func addDTLSRetransmitTests() {
2789 // Test that this is indeed the timeout schedule. Stress all
2790 // four patterns of handshake.
2791 for i := 1; i < len(timeouts); i++ {
2792 number := strconv.Itoa(i)
2793 testCases = append(testCases, testCase{
2794 protocol: dtls,
2795 name: "DTLS-Retransmit-Client-" + number,
2796 config: Config{
2797 Bugs: ProtocolBugs{
2798 TimeoutSchedule: timeouts[:i],
2799 },
2800 },
2801 resumeSession: true,
2802 flags: []string{"-async"},
2803 })
2804 testCases = append(testCases, testCase{
2805 protocol: dtls,
2806 testType: serverTest,
2807 name: "DTLS-Retransmit-Server-" + number,
2808 config: Config{
2809 Bugs: ProtocolBugs{
2810 TimeoutSchedule: timeouts[:i],
2811 },
2812 },
2813 resumeSession: true,
2814 flags: []string{"-async"},
2815 })
2816 }
2817
2818 // Test that exceeding the timeout schedule hits a read
2819 // timeout.
2820 testCases = append(testCases, testCase{
2821 protocol: dtls,
2822 name: "DTLS-Retransmit-Timeout",
2823 config: Config{
2824 Bugs: ProtocolBugs{
2825 TimeoutSchedule: timeouts,
2826 },
2827 },
2828 resumeSession: true,
2829 flags: []string{"-async"},
2830 shouldFail: true,
2831 expectedError: ":READ_TIMEOUT_EXPIRED:",
2832 })
2833
2834 // Test that timeout handling has a fudge factor, due to API
2835 // problems.
2836 testCases = append(testCases, testCase{
2837 protocol: dtls,
2838 name: "DTLS-Retransmit-Fudge",
2839 config: Config{
2840 Bugs: ProtocolBugs{
2841 TimeoutSchedule: []time.Duration{
2842 timeouts[0] - 10*time.Millisecond,
2843 },
2844 },
2845 },
2846 resumeSession: true,
2847 flags: []string{"-async"},
2848 })
2849}
2850
David Benjamin884fdf12014-08-02 15:28:23 -04002851func worker(statusChan chan statusMsg, c chan *testCase, buildDir string, wg *sync.WaitGroup) {
Adam Langley95c29f32014-06-20 12:00:00 -07002852 defer wg.Done()
2853
2854 for test := range c {
Adam Langley69a01602014-11-17 17:26:55 -08002855 var err error
2856
2857 if *mallocTest < 0 {
2858 statusChan <- statusMsg{test: test, started: true}
2859 err = runTest(test, buildDir, -1)
2860 } else {
2861 for mallocNumToFail := int64(*mallocTest); ; mallocNumToFail++ {
2862 statusChan <- statusMsg{test: test, started: true}
2863 if err = runTest(test, buildDir, mallocNumToFail); err != errMoreMallocs {
2864 if err != nil {
2865 fmt.Printf("\n\nmalloc test failed at %d: %s\n", mallocNumToFail, err)
2866 }
2867 break
2868 }
2869 }
2870 }
Adam Langley95c29f32014-06-20 12:00:00 -07002871 statusChan <- statusMsg{test: test, err: err}
2872 }
2873}
2874
2875type statusMsg struct {
2876 test *testCase
2877 started bool
2878 err error
2879}
2880
David Benjamin5f237bc2015-02-11 17:14:15 -05002881func statusPrinter(doneChan chan *testOutput, statusChan chan statusMsg, total int) {
Adam Langley95c29f32014-06-20 12:00:00 -07002882 var started, done, failed, lineLen int
Adam Langley95c29f32014-06-20 12:00:00 -07002883
David Benjamin5f237bc2015-02-11 17:14:15 -05002884 testOutput := newTestOutput()
Adam Langley95c29f32014-06-20 12:00:00 -07002885 for msg := range statusChan {
David Benjamin5f237bc2015-02-11 17:14:15 -05002886 if !*pipe {
2887 // Erase the previous status line.
David Benjamin87c8a642015-02-21 01:54:29 -05002888 var erase string
2889 for i := 0; i < lineLen; i++ {
2890 erase += "\b \b"
2891 }
2892 fmt.Print(erase)
David Benjamin5f237bc2015-02-11 17:14:15 -05002893 }
2894
Adam Langley95c29f32014-06-20 12:00:00 -07002895 if msg.started {
2896 started++
2897 } else {
2898 done++
David Benjamin5f237bc2015-02-11 17:14:15 -05002899
2900 if msg.err != nil {
2901 fmt.Printf("FAILED (%s)\n%s\n", msg.test.name, msg.err)
2902 failed++
2903 testOutput.addResult(msg.test.name, "FAIL")
2904 } else {
2905 if *pipe {
2906 // Print each test instead of a status line.
2907 fmt.Printf("PASSED (%s)\n", msg.test.name)
2908 }
2909 testOutput.addResult(msg.test.name, "PASS")
2910 }
Adam Langley95c29f32014-06-20 12:00:00 -07002911 }
2912
David Benjamin5f237bc2015-02-11 17:14:15 -05002913 if !*pipe {
2914 // Print a new status line.
2915 line := fmt.Sprintf("%d/%d/%d/%d", failed, done, started, total)
2916 lineLen = len(line)
2917 os.Stdout.WriteString(line)
Adam Langley95c29f32014-06-20 12:00:00 -07002918 }
Adam Langley95c29f32014-06-20 12:00:00 -07002919 }
David Benjamin5f237bc2015-02-11 17:14:15 -05002920
2921 doneChan <- testOutput
Adam Langley95c29f32014-06-20 12:00:00 -07002922}
2923
2924func main() {
2925 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 -04002926 var flagNumWorkers *int = flag.Int("num-workers", runtime.NumCPU(), "The number of workers to run in parallel.")
David Benjamin884fdf12014-08-02 15:28:23 -04002927 var flagBuildDir *string = flag.String("build-dir", "../../../build", "The build directory to run the shim from.")
Adam Langley95c29f32014-06-20 12:00:00 -07002928
2929 flag.Parse()
2930
2931 addCipherSuiteTests()
2932 addBadECDSASignatureTests()
Adam Langley80842bd2014-06-20 12:00:00 -07002933 addCBCPaddingTests()
Kenny Root7fdeaf12014-08-05 15:23:37 -07002934 addCBCSplittingTests()
David Benjamin636293b2014-07-08 17:59:18 -04002935 addClientAuthTests()
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002936 addVersionNegotiationTests()
David Benjaminaccb4542014-12-12 23:44:33 -05002937 addMinimumVersionTests()
David Benjamin5c24a1d2014-08-31 00:59:27 -04002938 addD5BugTests()
David Benjamine78bfde2014-09-06 12:45:15 -04002939 addExtensionTests()
David Benjamin01fe8202014-09-24 15:21:44 -04002940 addResumptionVersionTests()
Adam Langley75712922014-10-10 16:23:43 -07002941 addExtendedMasterSecretTests()
Adam Langley2ae77d22014-10-28 17:29:33 -07002942 addRenegotiationTests()
David Benjamin5e961c12014-11-07 01:48:35 -05002943 addDTLSReplayTests()
David Benjamin000800a2014-11-14 01:43:59 -05002944 addSigningHashTests()
Feng Lu41aa3252014-11-21 22:47:56 -08002945 addFastRadioPaddingTests()
David Benjamin83f90402015-01-27 01:09:43 -05002946 addDTLSRetransmitTests()
David Benjamin43ec06f2014-08-05 02:28:57 -04002947 for _, async := range []bool{false, true} {
2948 for _, splitHandshake := range []bool{false, true} {
David Benjamin6fd297b2014-08-11 18:43:38 -04002949 for _, protocol := range []protocol{tls, dtls} {
2950 addStateMachineCoverageTests(async, splitHandshake, protocol)
2951 }
David Benjamin43ec06f2014-08-05 02:28:57 -04002952 }
2953 }
Adam Langley95c29f32014-06-20 12:00:00 -07002954
2955 var wg sync.WaitGroup
2956
David Benjamin2bc8e6f2014-08-02 15:22:37 -04002957 numWorkers := *flagNumWorkers
Adam Langley95c29f32014-06-20 12:00:00 -07002958
2959 statusChan := make(chan statusMsg, numWorkers)
2960 testChan := make(chan *testCase, numWorkers)
David Benjamin5f237bc2015-02-11 17:14:15 -05002961 doneChan := make(chan *testOutput)
Adam Langley95c29f32014-06-20 12:00:00 -07002962
David Benjamin025b3d32014-07-01 19:53:04 -04002963 go statusPrinter(doneChan, statusChan, len(testCases))
Adam Langley95c29f32014-06-20 12:00:00 -07002964
2965 for i := 0; i < numWorkers; i++ {
2966 wg.Add(1)
David Benjamin884fdf12014-08-02 15:28:23 -04002967 go worker(statusChan, testChan, *flagBuildDir, &wg)
Adam Langley95c29f32014-06-20 12:00:00 -07002968 }
2969
David Benjamin025b3d32014-07-01 19:53:04 -04002970 for i := range testCases {
2971 if len(*flagTest) == 0 || *flagTest == testCases[i].name {
2972 testChan <- &testCases[i]
Adam Langley95c29f32014-06-20 12:00:00 -07002973 }
2974 }
2975
2976 close(testChan)
2977 wg.Wait()
2978 close(statusChan)
David Benjamin5f237bc2015-02-11 17:14:15 -05002979 testOutput := <-doneChan
Adam Langley95c29f32014-06-20 12:00:00 -07002980
2981 fmt.Printf("\n")
David Benjamin5f237bc2015-02-11 17:14:15 -05002982
2983 if *jsonOutput != "" {
2984 if err := testOutput.writeTo(*jsonOutput); err != nil {
2985 fmt.Fprintf(os.Stderr, "Error: %s\n", err)
2986 }
2987 }
Adam Langley95c29f32014-06-20 12:00:00 -07002988}