blob: 99c66a45fcd386baaeca4e2ebbfe492cd0045015 [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 (
27 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 *bool = flag.Bool("debug", false, "Hexdump the contents of the connection")
30 mallocTest *int64 = flag.Int64("malloc-test", -1, "If non-negative, run each test with each malloc in turn failing from the given number onwards.")
31 mallocTestDebug *bool = 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)
Adam Langley95c29f32014-06-20 12:00:00 -070033
David Benjamin025b3d32014-07-01 19:53:04 -040034const (
35 rsaCertificateFile = "cert.pem"
36 ecdsaCertificateFile = "ecdsa_cert.pem"
37)
38
39const (
David Benjamina08e49d2014-08-24 01:46:07 -040040 rsaKeyFile = "key.pem"
41 ecdsaKeyFile = "ecdsa_key.pem"
42 channelIDKeyFile = "channel_id_key.pem"
David Benjamin025b3d32014-07-01 19:53:04 -040043)
44
Adam Langley95c29f32014-06-20 12:00:00 -070045var rsaCertificate, ecdsaCertificate Certificate
David Benjamina08e49d2014-08-24 01:46:07 -040046var channelIDKey *ecdsa.PrivateKey
47var channelIDBytes []byte
Adam Langley95c29f32014-06-20 12:00:00 -070048
David Benjamin61f95272014-11-25 01:55:35 -050049var testOCSPResponse = []byte{1, 2, 3, 4}
50var testSCTList = []byte{5, 6, 7, 8}
51
Adam Langley95c29f32014-06-20 12:00:00 -070052func initCertificates() {
53 var err error
David Benjamin025b3d32014-07-01 19:53:04 -040054 rsaCertificate, err = LoadX509KeyPair(rsaCertificateFile, rsaKeyFile)
Adam Langley95c29f32014-06-20 12:00:00 -070055 if err != nil {
56 panic(err)
57 }
David Benjamin61f95272014-11-25 01:55:35 -050058 rsaCertificate.OCSPStaple = testOCSPResponse
59 rsaCertificate.SignedCertificateTimestampList = testSCTList
Adam Langley95c29f32014-06-20 12:00:00 -070060
David Benjamin025b3d32014-07-01 19:53:04 -040061 ecdsaCertificate, err = LoadX509KeyPair(ecdsaCertificateFile, ecdsaKeyFile)
Adam Langley95c29f32014-06-20 12:00:00 -070062 if err != nil {
63 panic(err)
64 }
David Benjamin61f95272014-11-25 01:55:35 -050065 ecdsaCertificate.OCSPStaple = testOCSPResponse
66 ecdsaCertificate.SignedCertificateTimestampList = testSCTList
David Benjamina08e49d2014-08-24 01:46:07 -040067
68 channelIDPEMBlock, err := ioutil.ReadFile(channelIDKeyFile)
69 if err != nil {
70 panic(err)
71 }
72 channelIDDERBlock, _ := pem.Decode(channelIDPEMBlock)
73 if channelIDDERBlock.Type != "EC PRIVATE KEY" {
74 panic("bad key type")
75 }
76 channelIDKey, err = x509.ParseECPrivateKey(channelIDDERBlock.Bytes)
77 if err != nil {
78 panic(err)
79 }
80 if channelIDKey.Curve != elliptic.P256() {
81 panic("bad curve")
82 }
83
84 channelIDBytes = make([]byte, 64)
85 writeIntPadded(channelIDBytes[:32], channelIDKey.X)
86 writeIntPadded(channelIDBytes[32:], channelIDKey.Y)
Adam Langley95c29f32014-06-20 12:00:00 -070087}
88
89var certificateOnce sync.Once
90
91func getRSACertificate() Certificate {
92 certificateOnce.Do(initCertificates)
93 return rsaCertificate
94}
95
96func getECDSACertificate() Certificate {
97 certificateOnce.Do(initCertificates)
98 return ecdsaCertificate
99}
100
David Benjamin025b3d32014-07-01 19:53:04 -0400101type testType int
102
103const (
104 clientTest testType = iota
105 serverTest
106)
107
David Benjamin6fd297b2014-08-11 18:43:38 -0400108type protocol int
109
110const (
111 tls protocol = iota
112 dtls
113)
114
David Benjaminfc7b0862014-09-06 13:21:53 -0400115const (
116 alpn = 1
117 npn = 2
118)
119
Adam Langley95c29f32014-06-20 12:00:00 -0700120type testCase struct {
David Benjamin025b3d32014-07-01 19:53:04 -0400121 testType testType
David Benjamin6fd297b2014-08-11 18:43:38 -0400122 protocol protocol
Adam Langley95c29f32014-06-20 12:00:00 -0700123 name string
124 config Config
125 shouldFail bool
126 expectedError string
Adam Langleyac61fa32014-06-23 12:03:11 -0700127 // expectedLocalError, if not empty, contains a substring that must be
128 // found in the local error.
129 expectedLocalError string
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400130 // expectedVersion, if non-zero, specifies the TLS version that must be
131 // negotiated.
132 expectedVersion uint16
David Benjamin01fe8202014-09-24 15:21:44 -0400133 // expectedResumeVersion, if non-zero, specifies the TLS version that
134 // must be negotiated on resumption. If zero, expectedVersion is used.
135 expectedResumeVersion uint16
David Benjamina08e49d2014-08-24 01:46:07 -0400136 // expectChannelID controls whether the connection should have
137 // negotiated a Channel ID with channelIDKey.
138 expectChannelID bool
David Benjaminae2888f2014-09-06 12:58:58 -0400139 // expectedNextProto controls whether the connection should
140 // negotiate a next protocol via NPN or ALPN.
141 expectedNextProto string
David Benjaminfc7b0862014-09-06 13:21:53 -0400142 // expectedNextProtoType, if non-zero, is the expected next
143 // protocol negotiation mechanism.
144 expectedNextProtoType int
David Benjaminca6c8262014-11-15 19:06:08 -0500145 // expectedSRTPProtectionProfile is the DTLS-SRTP profile that
146 // should be negotiated. If zero, none should be negotiated.
147 expectedSRTPProtectionProfile uint16
Adam Langley80842bd2014-06-20 12:00:00 -0700148 // messageLen is the length, in bytes, of the test message that will be
149 // sent.
150 messageLen int
David Benjamin025b3d32014-07-01 19:53:04 -0400151 // certFile is the path to the certificate to use for the server.
152 certFile string
153 // keyFile is the path to the private key to use for the server.
154 keyFile string
David Benjamin1d5c83e2014-07-22 19:20:02 -0400155 // resumeSession controls whether a second connection should be tested
David Benjamin01fe8202014-09-24 15:21:44 -0400156 // which attempts to resume the first session.
David Benjamin1d5c83e2014-07-22 19:20:02 -0400157 resumeSession bool
David Benjamin01fe8202014-09-24 15:21:44 -0400158 // resumeConfig, if not nil, points to a Config to be used on
David Benjaminfe8eb9a2014-11-17 03:19:02 -0500159 // resumption. Unless newSessionsOnResume is set,
160 // SessionTicketKey, ServerSessionCache, and
161 // ClientSessionCache are copied from the initial connection's
162 // config. If nil, the initial connection's config is used.
David Benjamin01fe8202014-09-24 15:21:44 -0400163 resumeConfig *Config
David Benjaminfe8eb9a2014-11-17 03:19:02 -0500164 // newSessionsOnResume, if true, will cause resumeConfig to
165 // use a different session resumption context.
166 newSessionsOnResume bool
David Benjamin98e882e2014-08-08 13:24:34 -0400167 // sendPrefix sends a prefix on the socket before actually performing a
168 // handshake.
169 sendPrefix string
David Benjamine58c4f52014-08-24 03:47:07 -0400170 // shimWritesFirst controls whether the shim sends an initial "hello"
171 // message before doing a roundtrip with the runner.
172 shimWritesFirst bool
Adam Langleycf2d4f42014-10-28 19:06:14 -0700173 // renegotiate indicates the the connection should be renegotiated
174 // during the exchange.
175 renegotiate bool
176 // renegotiateCiphers is a list of ciphersuite ids that will be
177 // switched in just before renegotiation.
178 renegotiateCiphers []uint16
David Benjamin5e961c12014-11-07 01:48:35 -0500179 // replayWrites, if true, configures the underlying transport
180 // to replay every write it makes in DTLS tests.
181 replayWrites bool
David Benjamin5fa3eba2015-01-22 16:35:40 -0500182 // damageFirstWrite, if true, configures the underlying transport to
183 // damage the final byte of the first application data write.
184 damageFirstWrite bool
David Benjamin325b5c32014-07-01 19:40:31 -0400185 // flags, if not empty, contains a list of command-line flags that will
186 // be passed to the shim program.
187 flags []string
Adam Langley95c29f32014-06-20 12:00:00 -0700188}
189
David Benjamin025b3d32014-07-01 19:53:04 -0400190var testCases = []testCase{
Adam Langley95c29f32014-06-20 12:00:00 -0700191 {
192 name: "BadRSASignature",
193 config: Config{
194 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
195 Bugs: ProtocolBugs{
196 InvalidSKXSignature: true,
197 },
198 },
199 shouldFail: true,
200 expectedError: ":BAD_SIGNATURE:",
201 },
202 {
203 name: "BadECDSASignature",
204 config: Config{
205 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
206 Bugs: ProtocolBugs{
207 InvalidSKXSignature: true,
208 },
209 Certificates: []Certificate{getECDSACertificate()},
210 },
211 shouldFail: true,
212 expectedError: ":BAD_SIGNATURE:",
213 },
214 {
215 name: "BadECDSACurve",
216 config: Config{
217 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
218 Bugs: ProtocolBugs{
219 InvalidSKXCurve: true,
220 },
221 Certificates: []Certificate{getECDSACertificate()},
222 },
223 shouldFail: true,
224 expectedError: ":WRONG_CURVE:",
225 },
Adam Langleyac61fa32014-06-23 12:03:11 -0700226 {
David Benjamina8e3e0e2014-08-06 22:11:10 -0400227 testType: serverTest,
228 name: "BadRSAVersion",
229 config: Config{
230 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
231 Bugs: ProtocolBugs{
232 RsaClientKeyExchangeVersion: VersionTLS11,
233 },
234 },
235 shouldFail: true,
236 expectedError: ":DECRYPTION_FAILED_OR_BAD_RECORD_MAC:",
237 },
238 {
David Benjamin325b5c32014-07-01 19:40:31 -0400239 name: "NoFallbackSCSV",
Adam Langleyac61fa32014-06-23 12:03:11 -0700240 config: Config{
241 Bugs: ProtocolBugs{
242 FailIfNotFallbackSCSV: true,
243 },
244 },
245 shouldFail: true,
246 expectedLocalError: "no fallback SCSV found",
247 },
David Benjamin325b5c32014-07-01 19:40:31 -0400248 {
David Benjamin2a0c4962014-08-22 23:46:35 -0400249 name: "SendFallbackSCSV",
David Benjamin325b5c32014-07-01 19:40:31 -0400250 config: Config{
251 Bugs: ProtocolBugs{
252 FailIfNotFallbackSCSV: true,
253 },
254 },
255 flags: []string{"-fallback-scsv"},
256 },
David Benjamin197b3ab2014-07-02 18:37:33 -0400257 {
David Benjamin7b030512014-07-08 17:30:11 -0400258 name: "ClientCertificateTypes",
259 config: Config{
260 ClientAuth: RequestClientCert,
261 ClientCertificateTypes: []byte{
262 CertTypeDSSSign,
263 CertTypeRSASign,
264 CertTypeECDSASign,
265 },
266 },
David Benjamin2561dc32014-08-24 01:25:27 -0400267 flags: []string{
268 "-expect-certificate-types",
269 base64.StdEncoding.EncodeToString([]byte{
270 CertTypeDSSSign,
271 CertTypeRSASign,
272 CertTypeECDSASign,
273 }),
274 },
David Benjamin7b030512014-07-08 17:30:11 -0400275 },
David Benjamin636293b2014-07-08 17:59:18 -0400276 {
277 name: "NoClientCertificate",
278 config: Config{
279 ClientAuth: RequireAnyClientCert,
280 },
281 shouldFail: true,
282 expectedLocalError: "client didn't provide a certificate",
283 },
David Benjamin1c375dd2014-07-12 00:48:23 -0400284 {
285 name: "UnauthenticatedECDH",
286 config: Config{
287 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
288 Bugs: ProtocolBugs{
289 UnauthenticatedECDH: true,
290 },
291 },
292 shouldFail: true,
David Benjamine8f3d662014-07-12 01:10:19 -0400293 expectedError: ":UNEXPECTED_MESSAGE:",
David Benjamin1c375dd2014-07-12 00:48:23 -0400294 },
David Benjamin9c651c92014-07-12 13:27:45 -0400295 {
296 name: "SkipServerKeyExchange",
297 config: Config{
298 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
299 Bugs: ProtocolBugs{
300 SkipServerKeyExchange: true,
301 },
302 },
303 shouldFail: true,
304 expectedError: ":UNEXPECTED_MESSAGE:",
305 },
David Benjamin1f5f62b2014-07-12 16:18:02 -0400306 {
David Benjamina0e52232014-07-19 17:39:58 -0400307 name: "SkipChangeCipherSpec-Client",
308 config: Config{
309 Bugs: ProtocolBugs{
310 SkipChangeCipherSpec: true,
311 },
312 },
313 shouldFail: true,
David Benjamin86271ee2014-07-21 16:14:03 -0400314 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
David Benjamina0e52232014-07-19 17:39:58 -0400315 },
316 {
317 testType: serverTest,
318 name: "SkipChangeCipherSpec-Server",
319 config: Config{
320 Bugs: ProtocolBugs{
321 SkipChangeCipherSpec: true,
322 },
323 },
324 shouldFail: true,
David Benjamin86271ee2014-07-21 16:14:03 -0400325 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
David Benjamina0e52232014-07-19 17:39:58 -0400326 },
David Benjamin42be6452014-07-21 14:50:23 -0400327 {
328 testType: serverTest,
329 name: "SkipChangeCipherSpec-Server-NPN",
330 config: Config{
331 NextProtos: []string{"bar"},
332 Bugs: ProtocolBugs{
333 SkipChangeCipherSpec: true,
334 },
335 },
336 flags: []string{
337 "-advertise-npn", "\x03foo\x03bar\x03baz",
338 },
339 shouldFail: true,
David Benjamin86271ee2014-07-21 16:14:03 -0400340 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
341 },
342 {
343 name: "FragmentAcrossChangeCipherSpec-Client",
344 config: Config{
345 Bugs: ProtocolBugs{
346 FragmentAcrossChangeCipherSpec: true,
347 },
348 },
349 shouldFail: true,
350 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
351 },
352 {
353 testType: serverTest,
354 name: "FragmentAcrossChangeCipherSpec-Server",
355 config: Config{
356 Bugs: ProtocolBugs{
357 FragmentAcrossChangeCipherSpec: true,
358 },
359 },
360 shouldFail: true,
361 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
362 },
363 {
364 testType: serverTest,
365 name: "FragmentAcrossChangeCipherSpec-Server-NPN",
366 config: Config{
367 NextProtos: []string{"bar"},
368 Bugs: ProtocolBugs{
369 FragmentAcrossChangeCipherSpec: true,
370 },
371 },
372 flags: []string{
373 "-advertise-npn", "\x03foo\x03bar\x03baz",
374 },
375 shouldFail: true,
376 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
David Benjamin42be6452014-07-21 14:50:23 -0400377 },
David Benjaminf3ec83d2014-07-21 22:42:34 -0400378 {
379 testType: serverTest,
David Benjamin3fd1fbd2015-02-03 16:07:32 -0500380 name: "Alert",
381 config: Config{
382 Bugs: ProtocolBugs{
383 SendSpuriousAlert: alertRecordOverflow,
384 },
385 },
386 shouldFail: true,
387 expectedError: ":TLSV1_ALERT_RECORD_OVERFLOW:",
388 },
389 {
390 protocol: dtls,
391 testType: serverTest,
392 name: "Alert-DTLS",
393 config: Config{
394 Bugs: ProtocolBugs{
395 SendSpuriousAlert: alertRecordOverflow,
396 },
397 },
398 shouldFail: true,
399 expectedError: ":TLSV1_ALERT_RECORD_OVERFLOW:",
400 },
401 {
402 testType: serverTest,
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400403 name: "FragmentAlert",
404 config: Config{
405 Bugs: ProtocolBugs{
David Benjaminca6c8262014-11-15 19:06:08 -0500406 FragmentAlert: true,
David Benjamin3fd1fbd2015-02-03 16:07:32 -0500407 SendSpuriousAlert: alertRecordOverflow,
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400408 },
409 },
410 shouldFail: true,
411 expectedError: ":BAD_ALERT:",
412 },
413 {
David Benjamin0ea8dda2015-01-31 20:33:40 -0500414 protocol: dtls,
415 testType: serverTest,
416 name: "FragmentAlert-DTLS",
417 config: Config{
418 Bugs: ProtocolBugs{
419 FragmentAlert: true,
David Benjamin3fd1fbd2015-02-03 16:07:32 -0500420 SendSpuriousAlert: alertRecordOverflow,
David Benjamin0ea8dda2015-01-31 20:33:40 -0500421 },
422 },
423 shouldFail: true,
424 expectedError: ":BAD_ALERT:",
425 },
426 {
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400427 testType: serverTest,
David Benjaminf3ec83d2014-07-21 22:42:34 -0400428 name: "EarlyChangeCipherSpec-server-1",
429 config: Config{
430 Bugs: ProtocolBugs{
431 EarlyChangeCipherSpec: 1,
432 },
433 },
434 shouldFail: true,
435 expectedError: ":CCS_RECEIVED_EARLY:",
436 },
437 {
438 testType: serverTest,
439 name: "EarlyChangeCipherSpec-server-2",
440 config: Config{
441 Bugs: ProtocolBugs{
442 EarlyChangeCipherSpec: 2,
443 },
444 },
445 shouldFail: true,
446 expectedError: ":CCS_RECEIVED_EARLY:",
447 },
David Benjamind23f4122014-07-23 15:09:48 -0400448 {
David Benjamind23f4122014-07-23 15:09:48 -0400449 name: "SkipNewSessionTicket",
450 config: Config{
451 Bugs: ProtocolBugs{
452 SkipNewSessionTicket: true,
453 },
454 },
455 shouldFail: true,
456 expectedError: ":CCS_RECEIVED_EARLY:",
457 },
David Benjamin7e3305e2014-07-28 14:52:32 -0400458 {
David Benjamind86c7672014-08-02 04:07:12 -0400459 testType: serverTest,
David Benjaminbef270a2014-08-02 04:22:02 -0400460 name: "FallbackSCSV",
461 config: Config{
462 MaxVersion: VersionTLS11,
463 Bugs: ProtocolBugs{
464 SendFallbackSCSV: true,
465 },
466 },
467 shouldFail: true,
468 expectedError: ":INAPPROPRIATE_FALLBACK:",
469 },
470 {
471 testType: serverTest,
472 name: "FallbackSCSV-VersionMatch",
473 config: Config{
474 Bugs: ProtocolBugs{
475 SendFallbackSCSV: true,
476 },
477 },
478 },
David Benjamin98214542014-08-07 18:02:39 -0400479 {
480 testType: serverTest,
481 name: "FragmentedClientVersion",
482 config: Config{
483 Bugs: ProtocolBugs{
484 MaxHandshakeRecordLength: 1,
485 FragmentClientVersion: true,
486 },
487 },
David Benjamin82c9e902014-12-12 15:55:27 -0500488 expectedVersion: VersionTLS12,
David Benjamin98214542014-08-07 18:02:39 -0400489 },
David Benjamin98e882e2014-08-08 13:24:34 -0400490 {
491 testType: serverTest,
492 name: "MinorVersionTolerance",
493 config: Config{
494 Bugs: ProtocolBugs{
495 SendClientVersion: 0x03ff,
496 },
497 },
498 expectedVersion: VersionTLS12,
499 },
500 {
501 testType: serverTest,
502 name: "MajorVersionTolerance",
503 config: Config{
504 Bugs: ProtocolBugs{
505 SendClientVersion: 0x0400,
506 },
507 },
508 expectedVersion: VersionTLS12,
509 },
510 {
511 testType: serverTest,
512 name: "VersionTooLow",
513 config: Config{
514 Bugs: ProtocolBugs{
515 SendClientVersion: 0x0200,
516 },
517 },
518 shouldFail: true,
519 expectedError: ":UNSUPPORTED_PROTOCOL:",
520 },
521 {
522 testType: serverTest,
523 name: "HttpGET",
524 sendPrefix: "GET / HTTP/1.0\n",
525 shouldFail: true,
526 expectedError: ":HTTP_REQUEST:",
527 },
528 {
529 testType: serverTest,
530 name: "HttpPOST",
531 sendPrefix: "POST / HTTP/1.0\n",
532 shouldFail: true,
533 expectedError: ":HTTP_REQUEST:",
534 },
535 {
536 testType: serverTest,
537 name: "HttpHEAD",
538 sendPrefix: "HEAD / HTTP/1.0\n",
539 shouldFail: true,
540 expectedError: ":HTTP_REQUEST:",
541 },
542 {
543 testType: serverTest,
544 name: "HttpPUT",
545 sendPrefix: "PUT / HTTP/1.0\n",
546 shouldFail: true,
547 expectedError: ":HTTP_REQUEST:",
548 },
549 {
550 testType: serverTest,
551 name: "HttpCONNECT",
552 sendPrefix: "CONNECT www.google.com:443 HTTP/1.0\n",
553 shouldFail: true,
554 expectedError: ":HTTPS_PROXY_REQUEST:",
555 },
David Benjamin39ebf532014-08-31 02:23:49 -0400556 {
David Benjaminf080ecd2014-12-11 18:48:59 -0500557 testType: serverTest,
558 name: "Garbage",
559 sendPrefix: "blah",
560 shouldFail: true,
561 expectedError: ":UNKNOWN_PROTOCOL:",
562 },
563 {
David Benjamin39ebf532014-08-31 02:23:49 -0400564 name: "SkipCipherVersionCheck",
565 config: Config{
566 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
567 MaxVersion: VersionTLS11,
568 Bugs: ProtocolBugs{
569 SkipCipherVersionCheck: true,
570 },
571 },
572 shouldFail: true,
573 expectedError: ":WRONG_CIPHER_RETURNED:",
574 },
David Benjamin9114fae2014-11-08 11:41:14 -0500575 {
576 name: "RSAServerKeyExchange",
577 config: Config{
578 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
579 Bugs: ProtocolBugs{
580 RSAServerKeyExchange: true,
581 },
582 },
583 shouldFail: true,
584 expectedError: ":UNEXPECTED_MESSAGE:",
585 },
David Benjamin128dbc32014-12-01 01:27:42 -0500586 {
587 name: "DisableEverything",
588 flags: []string{"-no-tls12", "-no-tls11", "-no-tls1", "-no-ssl3"},
589 shouldFail: true,
590 expectedError: ":WRONG_SSL_VERSION:",
591 },
592 {
593 protocol: dtls,
594 name: "DisableEverything-DTLS",
595 flags: []string{"-no-tls12", "-no-tls1"},
596 shouldFail: true,
597 expectedError: ":WRONG_SSL_VERSION:",
598 },
David Benjamin780d6dd2015-01-06 12:03:19 -0500599 {
600 name: "NoSharedCipher",
601 config: Config{
602 CipherSuites: []uint16{},
603 },
604 shouldFail: true,
605 expectedError: ":HANDSHAKE_FAILURE_ON_CLIENT_HELLO:",
606 },
David Benjamin13be1de2015-01-11 16:29:36 -0500607 {
608 protocol: dtls,
609 testType: serverTest,
610 name: "MTU",
611 config: Config{
612 Bugs: ProtocolBugs{
613 MaxPacketLength: 256,
614 },
615 },
616 flags: []string{"-mtu", "256"},
617 },
618 {
619 protocol: dtls,
620 testType: serverTest,
621 name: "MTUExceeded",
622 config: Config{
623 Bugs: ProtocolBugs{
624 MaxPacketLength: 255,
625 },
626 },
627 flags: []string{"-mtu", "256"},
628 shouldFail: true,
629 expectedLocalError: "dtls: exceeded maximum packet length",
630 },
David Benjamin6095de82014-12-27 01:50:38 -0500631 {
632 name: "CertMismatchRSA",
633 config: Config{
634 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
635 Certificates: []Certificate{getECDSACertificate()},
636 Bugs: ProtocolBugs{
637 SendCipherSuite: TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
638 },
639 },
640 shouldFail: true,
641 expectedError: ":WRONG_CERTIFICATE_TYPE:",
642 },
643 {
644 name: "CertMismatchECDSA",
645 config: Config{
646 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
647 Certificates: []Certificate{getRSACertificate()},
648 Bugs: ProtocolBugs{
649 SendCipherSuite: TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
650 },
651 },
652 shouldFail: true,
653 expectedError: ":WRONG_CERTIFICATE_TYPE:",
654 },
David Benjamin5fa3eba2015-01-22 16:35:40 -0500655 {
656 name: "TLSFatalBadPackets",
657 damageFirstWrite: true,
658 shouldFail: true,
659 expectedError: ":DECRYPTION_FAILED_OR_BAD_RECORD_MAC:",
660 },
661 {
662 protocol: dtls,
663 name: "DTLSIgnoreBadPackets",
664 damageFirstWrite: true,
665 },
666 {
667 protocol: dtls,
668 name: "DTLSIgnoreBadPackets-Async",
669 damageFirstWrite: true,
670 flags: []string{"-async"},
671 },
David Benjamin4189bd92015-01-25 23:52:39 -0500672 {
673 name: "AppDataAfterChangeCipherSpec",
674 config: Config{
675 Bugs: ProtocolBugs{
676 AppDataAfterChangeCipherSpec: []byte("TEST MESSAGE"),
677 },
678 },
679 shouldFail: true,
680 expectedError: ":DATA_BETWEEN_CCS_AND_FINISHED:",
681 },
682 {
683 protocol: dtls,
684 name: "AppDataAfterChangeCipherSpec-DTLS",
685 config: Config{
686 Bugs: ProtocolBugs{
687 AppDataAfterChangeCipherSpec: []byte("TEST MESSAGE"),
688 },
689 },
690 },
David Benjaminb3774b92015-01-31 17:16:01 -0500691 {
692 protocol: dtls,
693 name: "ReorderHandshakeFragments-Small-DTLS",
694 config: Config{
695 Bugs: ProtocolBugs{
696 ReorderHandshakeFragments: true,
697 // Small enough that every handshake message is
698 // fragmented.
699 MaxHandshakeRecordLength: 2,
700 },
701 },
702 },
703 {
704 protocol: dtls,
705 name: "ReorderHandshakeFragments-Large-DTLS",
706 config: Config{
707 Bugs: ProtocolBugs{
708 ReorderHandshakeFragments: true,
709 // Large enough that no handshake message is
710 // fragmented.
711 //
712 // TODO(davidben): Also test interaction of
713 // complete handshake messages with
714 // fragments. The current logic is full of bugs
715 // here, so the reassembly logic needs a rewrite
716 // before those tests will pass.
717 MaxHandshakeRecordLength: 2048,
718 },
719 },
720 },
David Benjaminddb9f152015-02-03 15:44:39 -0500721 {
722 name: "SendInvalidRecordType",
723 config: Config{
724 Bugs: ProtocolBugs{
725 SendInvalidRecordType: true,
726 },
727 },
728 shouldFail: true,
729 expectedError: ":UNEXPECTED_RECORD:",
730 },
731 {
732 protocol: dtls,
733 name: "SendInvalidRecordType-DTLS",
734 config: Config{
735 Bugs: ProtocolBugs{
736 SendInvalidRecordType: true,
737 },
738 },
739 shouldFail: true,
740 expectedError: ":UNEXPECTED_RECORD:",
741 },
David Benjaminb80168e2015-02-08 18:30:14 -0500742 {
743 name: "FalseStart-SkipServerSecondLeg",
744 config: Config{
745 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
746 NextProtos: []string{"foo"},
747 Bugs: ProtocolBugs{
748 SkipNewSessionTicket: true,
749 SkipChangeCipherSpec: true,
750 SkipFinished: true,
751 ExpectFalseStart: true,
752 },
753 },
754 flags: []string{
755 "-false-start",
756 "-advertise-alpn", "\x03foo",
757 },
758 shimWritesFirst: true,
759 shouldFail: true,
760 expectedError: ":UNEXPECTED_RECORD:",
761 },
David Benjamin931ab342015-02-08 19:46:57 -0500762 {
763 name: "FalseStart-SkipServerSecondLeg-Implicit",
764 config: Config{
765 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
766 NextProtos: []string{"foo"},
767 Bugs: ProtocolBugs{
768 SkipNewSessionTicket: true,
769 SkipChangeCipherSpec: true,
770 SkipFinished: true,
771 },
772 },
773 flags: []string{
774 "-implicit-handshake",
775 "-false-start",
776 "-advertise-alpn", "\x03foo",
777 },
778 shouldFail: true,
779 expectedError: ":UNEXPECTED_RECORD:",
780 },
Adam Langley95c29f32014-06-20 12:00:00 -0700781}
782
David Benjamin01fe8202014-09-24 15:21:44 -0400783func doExchange(test *testCase, config *Config, conn net.Conn, messageLen int, isResume bool) error {
David Benjamin65ea8ff2014-11-23 03:01:00 -0500784 var connDebug *recordingConn
David Benjamin5fa3eba2015-01-22 16:35:40 -0500785 var connDamage *damageAdaptor
David Benjamin65ea8ff2014-11-23 03:01:00 -0500786 if *flagDebug {
787 connDebug = &recordingConn{Conn: conn}
788 conn = connDebug
789 defer func() {
790 connDebug.WriteTo(os.Stdout)
791 }()
792 }
793
David Benjamin6fd297b2014-08-11 18:43:38 -0400794 if test.protocol == dtls {
David Benjamin83f90402015-01-27 01:09:43 -0500795 config.Bugs.PacketAdaptor = newPacketAdaptor(conn)
796 conn = config.Bugs.PacketAdaptor
David Benjamin5e961c12014-11-07 01:48:35 -0500797 if test.replayWrites {
798 conn = newReplayAdaptor(conn)
799 }
David Benjamin6fd297b2014-08-11 18:43:38 -0400800 }
801
David Benjamin5fa3eba2015-01-22 16:35:40 -0500802 if test.damageFirstWrite {
803 connDamage = newDamageAdaptor(conn)
804 conn = connDamage
805 }
806
David Benjamin6fd297b2014-08-11 18:43:38 -0400807 if test.sendPrefix != "" {
808 if _, err := conn.Write([]byte(test.sendPrefix)); err != nil {
809 return err
810 }
David Benjamin98e882e2014-08-08 13:24:34 -0400811 }
812
David Benjamin1d5c83e2014-07-22 19:20:02 -0400813 var tlsConn *Conn
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400814 if test.testType == clientTest {
David Benjamin6fd297b2014-08-11 18:43:38 -0400815 if test.protocol == dtls {
816 tlsConn = DTLSServer(conn, config)
817 } else {
818 tlsConn = Server(conn, config)
819 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400820 } else {
821 config.InsecureSkipVerify = true
David Benjamin6fd297b2014-08-11 18:43:38 -0400822 if test.protocol == dtls {
823 tlsConn = DTLSClient(conn, config)
824 } else {
825 tlsConn = Client(conn, config)
826 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400827 }
828
Adam Langley95c29f32014-06-20 12:00:00 -0700829 if err := tlsConn.Handshake(); err != nil {
830 return err
831 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700832
David Benjamin01fe8202014-09-24 15:21:44 -0400833 // TODO(davidben): move all per-connection expectations into a dedicated
834 // expectations struct that can be specified separately for the two
835 // legs.
836 expectedVersion := test.expectedVersion
837 if isResume && test.expectedResumeVersion != 0 {
838 expectedVersion = test.expectedResumeVersion
839 }
840 if vers := tlsConn.ConnectionState().Version; expectedVersion != 0 && vers != expectedVersion {
841 return fmt.Errorf("got version %x, expected %x", vers, expectedVersion)
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400842 }
843
David Benjamina08e49d2014-08-24 01:46:07 -0400844 if test.expectChannelID {
845 channelID := tlsConn.ConnectionState().ChannelID
846 if channelID == nil {
847 return fmt.Errorf("no channel ID negotiated")
848 }
849 if channelID.Curve != channelIDKey.Curve ||
850 channelIDKey.X.Cmp(channelIDKey.X) != 0 ||
851 channelIDKey.Y.Cmp(channelIDKey.Y) != 0 {
852 return fmt.Errorf("incorrect channel ID")
853 }
854 }
855
David Benjaminae2888f2014-09-06 12:58:58 -0400856 if expected := test.expectedNextProto; expected != "" {
857 if actual := tlsConn.ConnectionState().NegotiatedProtocol; actual != expected {
858 return fmt.Errorf("next proto mismatch: got %s, wanted %s", actual, expected)
859 }
860 }
861
David Benjaminfc7b0862014-09-06 13:21:53 -0400862 if test.expectedNextProtoType != 0 {
863 if (test.expectedNextProtoType == alpn) != tlsConn.ConnectionState().NegotiatedProtocolFromALPN {
864 return fmt.Errorf("next proto type mismatch")
865 }
866 }
867
David Benjaminca6c8262014-11-15 19:06:08 -0500868 if p := tlsConn.ConnectionState().SRTPProtectionProfile; p != test.expectedSRTPProtectionProfile {
869 return fmt.Errorf("SRTP profile mismatch: got %d, wanted %d", p, test.expectedSRTPProtectionProfile)
870 }
871
David Benjamine58c4f52014-08-24 03:47:07 -0400872 if test.shimWritesFirst {
873 var buf [5]byte
874 _, err := io.ReadFull(tlsConn, buf[:])
875 if err != nil {
876 return err
877 }
878 if string(buf[:]) != "hello" {
879 return fmt.Errorf("bad initial message")
880 }
881 }
882
Adam Langleycf2d4f42014-10-28 19:06:14 -0700883 if test.renegotiate {
884 if test.renegotiateCiphers != nil {
885 config.CipherSuites = test.renegotiateCiphers
886 }
887 if err := tlsConn.Renegotiate(); err != nil {
888 return err
889 }
890 } else if test.renegotiateCiphers != nil {
891 panic("renegotiateCiphers without renegotiate")
892 }
893
David Benjamin5fa3eba2015-01-22 16:35:40 -0500894 if test.damageFirstWrite {
895 connDamage.setDamage(true)
896 tlsConn.Write([]byte("DAMAGED WRITE"))
897 connDamage.setDamage(false)
898 }
899
Kenny Root7fdeaf12014-08-05 15:23:37 -0700900 if messageLen < 0 {
David Benjamin6fd297b2014-08-11 18:43:38 -0400901 if test.protocol == dtls {
902 return fmt.Errorf("messageLen < 0 not supported for DTLS tests")
903 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700904 // Read until EOF.
905 _, err := io.Copy(ioutil.Discard, tlsConn)
906 return err
907 }
908
David Benjamin4189bd92015-01-25 23:52:39 -0500909 var testMessage []byte
910 if config.Bugs.AppDataAfterChangeCipherSpec != nil {
911 // We've already sent a message. Expect the shim to echo it
912 // back.
913 testMessage = config.Bugs.AppDataAfterChangeCipherSpec
914 } else {
915 if messageLen == 0 {
916 messageLen = 32
917 }
918 testMessage = make([]byte, messageLen)
919 for i := range testMessage {
920 testMessage[i] = 0x42
921 }
922 tlsConn.Write(testMessage)
Adam Langley80842bd2014-06-20 12:00:00 -0700923 }
Adam Langley95c29f32014-06-20 12:00:00 -0700924
925 buf := make([]byte, len(testMessage))
David Benjamin6fd297b2014-08-11 18:43:38 -0400926 if test.protocol == dtls {
927 bufTmp := make([]byte, len(buf)+1)
928 n, err := tlsConn.Read(bufTmp)
929 if err != nil {
930 return err
931 }
932 if n != len(buf) {
933 return fmt.Errorf("bad reply; length mismatch (%d vs %d)", n, len(buf))
934 }
935 copy(buf, bufTmp)
936 } else {
937 _, err := io.ReadFull(tlsConn, buf)
938 if err != nil {
939 return err
940 }
Adam Langley95c29f32014-06-20 12:00:00 -0700941 }
942
943 for i, v := range buf {
944 if v != testMessage[i]^0xff {
945 return fmt.Errorf("bad reply contents at byte %d", i)
946 }
947 }
948
949 return nil
950}
951
David Benjamin325b5c32014-07-01 19:40:31 -0400952func valgrindOf(dbAttach bool, path string, args ...string) *exec.Cmd {
953 valgrindArgs := []string{"--error-exitcode=99", "--track-origins=yes", "--leak-check=full"}
Adam Langley95c29f32014-06-20 12:00:00 -0700954 if dbAttach {
David Benjamin325b5c32014-07-01 19:40:31 -0400955 valgrindArgs = append(valgrindArgs, "--db-attach=yes", "--db-command=xterm -e gdb -nw %f %p")
Adam Langley95c29f32014-06-20 12:00:00 -0700956 }
David Benjamin325b5c32014-07-01 19:40:31 -0400957 valgrindArgs = append(valgrindArgs, path)
958 valgrindArgs = append(valgrindArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -0700959
David Benjamin325b5c32014-07-01 19:40:31 -0400960 return exec.Command("valgrind", valgrindArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -0700961}
962
David Benjamin325b5c32014-07-01 19:40:31 -0400963func gdbOf(path string, args ...string) *exec.Cmd {
964 xtermArgs := []string{"-e", "gdb", "--args"}
965 xtermArgs = append(xtermArgs, path)
966 xtermArgs = append(xtermArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -0700967
David Benjamin325b5c32014-07-01 19:40:31 -0400968 return exec.Command("xterm", xtermArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -0700969}
970
David Benjamin1d5c83e2014-07-22 19:20:02 -0400971func openSocketPair() (shimEnd *os.File, conn net.Conn) {
Adam Langley95c29f32014-06-20 12:00:00 -0700972 socks, err := syscall.Socketpair(syscall.AF_UNIX, syscall.SOCK_STREAM, 0)
973 if err != nil {
974 panic(err)
975 }
976
977 syscall.CloseOnExec(socks[0])
978 syscall.CloseOnExec(socks[1])
David Benjamin1d5c83e2014-07-22 19:20:02 -0400979 shimEnd = os.NewFile(uintptr(socks[0]), "shim end")
Adam Langley95c29f32014-06-20 12:00:00 -0700980 connFile := os.NewFile(uintptr(socks[1]), "our end")
David Benjamin1d5c83e2014-07-22 19:20:02 -0400981 conn, err = net.FileConn(connFile)
982 if err != nil {
983 panic(err)
984 }
Adam Langley95c29f32014-06-20 12:00:00 -0700985 connFile.Close()
986 if err != nil {
987 panic(err)
988 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400989 return shimEnd, conn
990}
991
Adam Langley69a01602014-11-17 17:26:55 -0800992type moreMallocsError struct{}
993
994func (moreMallocsError) Error() string {
995 return "child process did not exhaust all allocation calls"
996}
997
998var errMoreMallocs = moreMallocsError{}
999
1000func runTest(test *testCase, buildDir string, mallocNumToFail int64) error {
Adam Langley38311732014-10-16 19:04:35 -07001001 if !test.shouldFail && (len(test.expectedError) > 0 || len(test.expectedLocalError) > 0) {
1002 panic("Error expected without shouldFail in " + test.name)
1003 }
1004
David Benjamin1d5c83e2014-07-22 19:20:02 -04001005 shimEnd, conn := openSocketPair()
1006 shimEndResume, connResume := openSocketPair()
Adam Langley95c29f32014-06-20 12:00:00 -07001007
David Benjamin884fdf12014-08-02 15:28:23 -04001008 shim_path := path.Join(buildDir, "ssl/test/bssl_shim")
David Benjamin5a593af2014-08-11 19:51:50 -04001009 var flags []string
David Benjamin1d5c83e2014-07-22 19:20:02 -04001010 if test.testType == serverTest {
David Benjamin5a593af2014-08-11 19:51:50 -04001011 flags = append(flags, "-server")
1012
David Benjamin025b3d32014-07-01 19:53:04 -04001013 flags = append(flags, "-key-file")
1014 if test.keyFile == "" {
1015 flags = append(flags, rsaKeyFile)
1016 } else {
1017 flags = append(flags, test.keyFile)
1018 }
1019
1020 flags = append(flags, "-cert-file")
1021 if test.certFile == "" {
1022 flags = append(flags, rsaCertificateFile)
1023 } else {
1024 flags = append(flags, test.certFile)
1025 }
1026 }
David Benjamin5a593af2014-08-11 19:51:50 -04001027
David Benjamin6fd297b2014-08-11 18:43:38 -04001028 if test.protocol == dtls {
1029 flags = append(flags, "-dtls")
1030 }
1031
David Benjamin5a593af2014-08-11 19:51:50 -04001032 if test.resumeSession {
1033 flags = append(flags, "-resume")
1034 }
1035
David Benjamine58c4f52014-08-24 03:47:07 -04001036 if test.shimWritesFirst {
1037 flags = append(flags, "-shim-writes-first")
1038 }
1039
David Benjamin025b3d32014-07-01 19:53:04 -04001040 flags = append(flags, test.flags...)
1041
1042 var shim *exec.Cmd
1043 if *useValgrind {
1044 shim = valgrindOf(false, shim_path, flags...)
Adam Langley75712922014-10-10 16:23:43 -07001045 } else if *useGDB {
1046 shim = gdbOf(shim_path, flags...)
David Benjamin025b3d32014-07-01 19:53:04 -04001047 } else {
1048 shim = exec.Command(shim_path, flags...)
1049 }
David Benjamin1d5c83e2014-07-22 19:20:02 -04001050 shim.ExtraFiles = []*os.File{shimEnd, shimEndResume}
David Benjamin025b3d32014-07-01 19:53:04 -04001051 shim.Stdin = os.Stdin
1052 var stdoutBuf, stderrBuf bytes.Buffer
1053 shim.Stdout = &stdoutBuf
1054 shim.Stderr = &stderrBuf
Adam Langley69a01602014-11-17 17:26:55 -08001055 if mallocNumToFail >= 0 {
David Benjamin9e128b02015-02-09 13:13:09 -05001056 shim.Env = os.Environ()
1057 shim.Env = append(shim.Env, "MALLOC_NUMBER_TO_FAIL="+strconv.FormatInt(mallocNumToFail, 10))
Adam Langley69a01602014-11-17 17:26:55 -08001058 if *mallocTestDebug {
1059 shim.Env = append(shim.Env, "MALLOC_ABORT_ON_FAIL=1")
1060 }
1061 shim.Env = append(shim.Env, "_MALLOC_CHECK=1")
1062 }
David Benjamin025b3d32014-07-01 19:53:04 -04001063
1064 if err := shim.Start(); err != nil {
Adam Langley95c29f32014-06-20 12:00:00 -07001065 panic(err)
1066 }
David Benjamin025b3d32014-07-01 19:53:04 -04001067 shimEnd.Close()
David Benjamin1d5c83e2014-07-22 19:20:02 -04001068 shimEndResume.Close()
Adam Langley95c29f32014-06-20 12:00:00 -07001069
1070 config := test.config
David Benjamin1d5c83e2014-07-22 19:20:02 -04001071 config.ClientSessionCache = NewLRUClientSessionCache(1)
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001072 config.ServerSessionCache = NewLRUServerSessionCache(1)
David Benjamin025b3d32014-07-01 19:53:04 -04001073 if test.testType == clientTest {
1074 if len(config.Certificates) == 0 {
1075 config.Certificates = []Certificate{getRSACertificate()}
1076 }
David Benjamin025b3d32014-07-01 19:53:04 -04001077 }
Adam Langley95c29f32014-06-20 12:00:00 -07001078
David Benjamin01fe8202014-09-24 15:21:44 -04001079 err := doExchange(test, &config, conn, test.messageLen,
1080 false /* not a resumption */)
Adam Langley95c29f32014-06-20 12:00:00 -07001081 conn.Close()
David Benjamin65ea8ff2014-11-23 03:01:00 -05001082
David Benjamin1d5c83e2014-07-22 19:20:02 -04001083 if err == nil && test.resumeSession {
David Benjamin01fe8202014-09-24 15:21:44 -04001084 var resumeConfig Config
1085 if test.resumeConfig != nil {
1086 resumeConfig = *test.resumeConfig
1087 if len(resumeConfig.Certificates) == 0 {
1088 resumeConfig.Certificates = []Certificate{getRSACertificate()}
1089 }
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001090 if !test.newSessionsOnResume {
1091 resumeConfig.SessionTicketKey = config.SessionTicketKey
1092 resumeConfig.ClientSessionCache = config.ClientSessionCache
1093 resumeConfig.ServerSessionCache = config.ServerSessionCache
1094 }
David Benjamin01fe8202014-09-24 15:21:44 -04001095 } else {
1096 resumeConfig = config
1097 }
1098 err = doExchange(test, &resumeConfig, connResume, test.messageLen,
1099 true /* resumption */)
David Benjamin1d5c83e2014-07-22 19:20:02 -04001100 }
David Benjamin812152a2014-09-06 12:49:07 -04001101 connResume.Close()
David Benjamin1d5c83e2014-07-22 19:20:02 -04001102
David Benjamin025b3d32014-07-01 19:53:04 -04001103 childErr := shim.Wait()
Adam Langley69a01602014-11-17 17:26:55 -08001104 if exitError, ok := childErr.(*exec.ExitError); ok {
1105 if exitError.Sys().(syscall.WaitStatus).ExitStatus() == 88 {
1106 return errMoreMallocs
1107 }
1108 }
Adam Langley95c29f32014-06-20 12:00:00 -07001109
1110 stdout := string(stdoutBuf.Bytes())
1111 stderr := string(stderrBuf.Bytes())
1112 failed := err != nil || childErr != nil
1113 correctFailure := len(test.expectedError) == 0 || strings.Contains(stdout, test.expectedError)
Adam Langleyac61fa32014-06-23 12:03:11 -07001114 localError := "none"
1115 if err != nil {
1116 localError = err.Error()
1117 }
1118 if len(test.expectedLocalError) != 0 {
1119 correctFailure = correctFailure && strings.Contains(localError, test.expectedLocalError)
1120 }
Adam Langley95c29f32014-06-20 12:00:00 -07001121
1122 if failed != test.shouldFail || failed && !correctFailure {
Adam Langley95c29f32014-06-20 12:00:00 -07001123 childError := "none"
Adam Langley95c29f32014-06-20 12:00:00 -07001124 if childErr != nil {
1125 childError = childErr.Error()
1126 }
1127
1128 var msg string
1129 switch {
1130 case failed && !test.shouldFail:
1131 msg = "unexpected failure"
1132 case !failed && test.shouldFail:
1133 msg = "unexpected success"
1134 case failed && !correctFailure:
Adam Langleyac61fa32014-06-23 12:03:11 -07001135 msg = "bad error (wanted '" + test.expectedError + "' / '" + test.expectedLocalError + "')"
Adam Langley95c29f32014-06-20 12:00:00 -07001136 default:
1137 panic("internal error")
1138 }
1139
1140 return fmt.Errorf("%s: local error '%s', child error '%s', stdout:\n%s\nstderr:\n%s", msg, localError, childError, string(stdoutBuf.Bytes()), stderr)
1141 }
1142
1143 if !*useValgrind && len(stderr) > 0 {
1144 println(stderr)
1145 }
1146
1147 return nil
1148}
1149
1150var tlsVersions = []struct {
1151 name string
1152 version uint16
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001153 flag string
David Benjamin8b8c0062014-11-23 02:47:52 -05001154 hasDTLS bool
Adam Langley95c29f32014-06-20 12:00:00 -07001155}{
David Benjamin8b8c0062014-11-23 02:47:52 -05001156 {"SSL3", VersionSSL30, "-no-ssl3", false},
1157 {"TLS1", VersionTLS10, "-no-tls1", true},
1158 {"TLS11", VersionTLS11, "-no-tls11", false},
1159 {"TLS12", VersionTLS12, "-no-tls12", true},
Adam Langley95c29f32014-06-20 12:00:00 -07001160}
1161
1162var testCipherSuites = []struct {
1163 name string
1164 id uint16
1165}{
1166 {"3DES-SHA", TLS_RSA_WITH_3DES_EDE_CBC_SHA},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001167 {"AES128-GCM", TLS_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001168 {"AES128-SHA", TLS_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001169 {"AES128-SHA256", TLS_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001170 {"AES256-GCM", TLS_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001171 {"AES256-SHA", TLS_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001172 {"AES256-SHA256", TLS_RSA_WITH_AES_256_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001173 {"DHE-RSA-AES128-GCM", TLS_DHE_RSA_WITH_AES_128_GCM_SHA256},
1174 {"DHE-RSA-AES128-SHA", TLS_DHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001175 {"DHE-RSA-AES128-SHA256", TLS_DHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001176 {"DHE-RSA-AES256-GCM", TLS_DHE_RSA_WITH_AES_256_GCM_SHA384},
1177 {"DHE-RSA-AES256-SHA", TLS_DHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001178 {"DHE-RSA-AES256-SHA256", TLS_DHE_RSA_WITH_AES_256_CBC_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001179 {"ECDHE-ECDSA-AES128-GCM", TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
1180 {"ECDHE-ECDSA-AES128-SHA", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001181 {"ECDHE-ECDSA-AES128-SHA256", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256},
1182 {"ECDHE-ECDSA-AES256-GCM", TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001183 {"ECDHE-ECDSA-AES256-SHA", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001184 {"ECDHE-ECDSA-AES256-SHA384", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001185 {"ECDHE-ECDSA-RC4-SHA", TLS_ECDHE_ECDSA_WITH_RC4_128_SHA},
David Benjamin2af684f2014-10-27 02:23:15 -04001186 {"ECDHE-PSK-WITH-AES-128-GCM-SHA256", TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001187 {"ECDHE-RSA-AES128-GCM", TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001188 {"ECDHE-RSA-AES128-SHA", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001189 {"ECDHE-RSA-AES128-SHA256", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001190 {"ECDHE-RSA-AES256-GCM", TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001191 {"ECDHE-RSA-AES256-SHA", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001192 {"ECDHE-RSA-AES256-SHA384", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001193 {"ECDHE-RSA-RC4-SHA", TLS_ECDHE_RSA_WITH_RC4_128_SHA},
David Benjamin48cae082014-10-27 01:06:24 -04001194 {"PSK-AES128-CBC-SHA", TLS_PSK_WITH_AES_128_CBC_SHA},
1195 {"PSK-AES256-CBC-SHA", TLS_PSK_WITH_AES_256_CBC_SHA},
1196 {"PSK-RC4-SHA", TLS_PSK_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -07001197 {"RC4-MD5", TLS_RSA_WITH_RC4_128_MD5},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001198 {"RC4-SHA", TLS_RSA_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -07001199}
1200
David Benjamin8b8c0062014-11-23 02:47:52 -05001201func hasComponent(suiteName, component string) bool {
1202 return strings.Contains("-"+suiteName+"-", "-"+component+"-")
1203}
1204
David Benjaminf7768e42014-08-31 02:06:47 -04001205func isTLS12Only(suiteName string) bool {
David Benjamin8b8c0062014-11-23 02:47:52 -05001206 return hasComponent(suiteName, "GCM") ||
1207 hasComponent(suiteName, "SHA256") ||
1208 hasComponent(suiteName, "SHA384")
1209}
1210
1211func isDTLSCipher(suiteName string) bool {
David Benjamine95d20d2014-12-23 11:16:01 -05001212 return !hasComponent(suiteName, "RC4")
David Benjaminf7768e42014-08-31 02:06:47 -04001213}
1214
Adam Langley95c29f32014-06-20 12:00:00 -07001215func addCipherSuiteTests() {
1216 for _, suite := range testCipherSuites {
David Benjamin48cae082014-10-27 01:06:24 -04001217 const psk = "12345"
1218 const pskIdentity = "luggage combo"
1219
Adam Langley95c29f32014-06-20 12:00:00 -07001220 var cert Certificate
David Benjamin025b3d32014-07-01 19:53:04 -04001221 var certFile string
1222 var keyFile string
David Benjamin8b8c0062014-11-23 02:47:52 -05001223 if hasComponent(suite.name, "ECDSA") {
Adam Langley95c29f32014-06-20 12:00:00 -07001224 cert = getECDSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -04001225 certFile = ecdsaCertificateFile
1226 keyFile = ecdsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -07001227 } else {
1228 cert = getRSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -04001229 certFile = rsaCertificateFile
1230 keyFile = rsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -07001231 }
1232
David Benjamin48cae082014-10-27 01:06:24 -04001233 var flags []string
David Benjamin8b8c0062014-11-23 02:47:52 -05001234 if hasComponent(suite.name, "PSK") {
David Benjamin48cae082014-10-27 01:06:24 -04001235 flags = append(flags,
1236 "-psk", psk,
1237 "-psk-identity", pskIdentity)
1238 }
1239
Adam Langley95c29f32014-06-20 12:00:00 -07001240 for _, ver := range tlsVersions {
David Benjaminf7768e42014-08-31 02:06:47 -04001241 if ver.version < VersionTLS12 && isTLS12Only(suite.name) {
Adam Langley95c29f32014-06-20 12:00:00 -07001242 continue
1243 }
1244
David Benjamin025b3d32014-07-01 19:53:04 -04001245 testCases = append(testCases, testCase{
1246 testType: clientTest,
1247 name: ver.name + "-" + suite.name + "-client",
Adam Langley95c29f32014-06-20 12:00:00 -07001248 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001249 MinVersion: ver.version,
1250 MaxVersion: ver.version,
1251 CipherSuites: []uint16{suite.id},
1252 Certificates: []Certificate{cert},
1253 PreSharedKey: []byte(psk),
1254 PreSharedKeyIdentity: pskIdentity,
Adam Langley95c29f32014-06-20 12:00:00 -07001255 },
David Benjamin48cae082014-10-27 01:06:24 -04001256 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001257 resumeSession: true,
Adam Langley95c29f32014-06-20 12:00:00 -07001258 })
David Benjamin025b3d32014-07-01 19:53:04 -04001259
David Benjamin76d8abe2014-08-14 16:25:34 -04001260 testCases = append(testCases, testCase{
1261 testType: serverTest,
1262 name: ver.name + "-" + suite.name + "-server",
1263 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001264 MinVersion: ver.version,
1265 MaxVersion: ver.version,
1266 CipherSuites: []uint16{suite.id},
1267 Certificates: []Certificate{cert},
1268 PreSharedKey: []byte(psk),
1269 PreSharedKeyIdentity: pskIdentity,
David Benjamin76d8abe2014-08-14 16:25:34 -04001270 },
1271 certFile: certFile,
1272 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001273 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001274 resumeSession: true,
David Benjamin76d8abe2014-08-14 16:25:34 -04001275 })
David Benjamin6fd297b2014-08-11 18:43:38 -04001276
David Benjamin8b8c0062014-11-23 02:47:52 -05001277 if ver.hasDTLS && isDTLSCipher(suite.name) {
David Benjamin6fd297b2014-08-11 18:43:38 -04001278 testCases = append(testCases, testCase{
1279 testType: clientTest,
1280 protocol: dtls,
1281 name: "D" + ver.name + "-" + suite.name + "-client",
1282 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001283 MinVersion: ver.version,
1284 MaxVersion: ver.version,
1285 CipherSuites: []uint16{suite.id},
1286 Certificates: []Certificate{cert},
1287 PreSharedKey: []byte(psk),
1288 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001289 },
David Benjamin48cae082014-10-27 01:06:24 -04001290 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001291 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001292 })
1293 testCases = append(testCases, testCase{
1294 testType: serverTest,
1295 protocol: dtls,
1296 name: "D" + ver.name + "-" + suite.name + "-server",
1297 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001298 MinVersion: ver.version,
1299 MaxVersion: ver.version,
1300 CipherSuites: []uint16{suite.id},
1301 Certificates: []Certificate{cert},
1302 PreSharedKey: []byte(psk),
1303 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001304 },
1305 certFile: certFile,
1306 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001307 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001308 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001309 })
1310 }
Adam Langley95c29f32014-06-20 12:00:00 -07001311 }
1312 }
1313}
1314
1315func addBadECDSASignatureTests() {
1316 for badR := BadValue(1); badR < NumBadValues; badR++ {
1317 for badS := BadValue(1); badS < NumBadValues; badS++ {
David Benjamin025b3d32014-07-01 19:53:04 -04001318 testCases = append(testCases, testCase{
Adam Langley95c29f32014-06-20 12:00:00 -07001319 name: fmt.Sprintf("BadECDSA-%d-%d", badR, badS),
1320 config: Config{
1321 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
1322 Certificates: []Certificate{getECDSACertificate()},
1323 Bugs: ProtocolBugs{
1324 BadECDSAR: badR,
1325 BadECDSAS: badS,
1326 },
1327 },
1328 shouldFail: true,
1329 expectedError: "SIGNATURE",
1330 })
1331 }
1332 }
1333}
1334
Adam Langley80842bd2014-06-20 12:00:00 -07001335func addCBCPaddingTests() {
David Benjamin025b3d32014-07-01 19:53:04 -04001336 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001337 name: "MaxCBCPadding",
1338 config: Config{
1339 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1340 Bugs: ProtocolBugs{
1341 MaxPadding: true,
1342 },
1343 },
1344 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1345 })
David Benjamin025b3d32014-07-01 19:53:04 -04001346 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001347 name: "BadCBCPadding",
1348 config: Config{
1349 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1350 Bugs: ProtocolBugs{
1351 PaddingFirstByteBad: true,
1352 },
1353 },
1354 shouldFail: true,
1355 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1356 })
1357 // OpenSSL previously had an issue where the first byte of padding in
1358 // 255 bytes of padding wasn't checked.
David Benjamin025b3d32014-07-01 19:53:04 -04001359 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001360 name: "BadCBCPadding255",
1361 config: Config{
1362 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1363 Bugs: ProtocolBugs{
1364 MaxPadding: true,
1365 PaddingFirstByteBadIf255: true,
1366 },
1367 },
1368 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1369 shouldFail: true,
1370 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1371 })
1372}
1373
Kenny Root7fdeaf12014-08-05 15:23:37 -07001374func addCBCSplittingTests() {
1375 testCases = append(testCases, testCase{
1376 name: "CBCRecordSplitting",
1377 config: Config{
1378 MaxVersion: VersionTLS10,
1379 MinVersion: VersionTLS10,
1380 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1381 },
1382 messageLen: -1, // read until EOF
1383 flags: []string{
1384 "-async",
1385 "-write-different-record-sizes",
1386 "-cbc-record-splitting",
1387 },
David Benjamina8e3e0e2014-08-06 22:11:10 -04001388 })
1389 testCases = append(testCases, testCase{
Kenny Root7fdeaf12014-08-05 15:23:37 -07001390 name: "CBCRecordSplittingPartialWrite",
1391 config: Config{
1392 MaxVersion: VersionTLS10,
1393 MinVersion: VersionTLS10,
1394 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1395 },
1396 messageLen: -1, // read until EOF
1397 flags: []string{
1398 "-async",
1399 "-write-different-record-sizes",
1400 "-cbc-record-splitting",
1401 "-partial-write",
1402 },
1403 })
1404}
1405
David Benjamin636293b2014-07-08 17:59:18 -04001406func addClientAuthTests() {
David Benjamin407a10c2014-07-16 12:58:59 -04001407 // Add a dummy cert pool to stress certificate authority parsing.
1408 // TODO(davidben): Add tests that those values parse out correctly.
1409 certPool := x509.NewCertPool()
1410 cert, err := x509.ParseCertificate(rsaCertificate.Certificate[0])
1411 if err != nil {
1412 panic(err)
1413 }
1414 certPool.AddCert(cert)
1415
David Benjamin636293b2014-07-08 17:59:18 -04001416 for _, ver := range tlsVersions {
David Benjamin636293b2014-07-08 17:59:18 -04001417 testCases = append(testCases, testCase{
1418 testType: clientTest,
David Benjamin67666e72014-07-12 15:47:52 -04001419 name: ver.name + "-Client-ClientAuth-RSA",
David Benjamin636293b2014-07-08 17:59:18 -04001420 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001421 MinVersion: ver.version,
1422 MaxVersion: ver.version,
1423 ClientAuth: RequireAnyClientCert,
1424 ClientCAs: certPool,
David Benjamin636293b2014-07-08 17:59:18 -04001425 },
1426 flags: []string{
1427 "-cert-file", rsaCertificateFile,
1428 "-key-file", rsaKeyFile,
1429 },
1430 })
1431 testCases = append(testCases, testCase{
David Benjamin67666e72014-07-12 15:47:52 -04001432 testType: serverTest,
1433 name: ver.name + "-Server-ClientAuth-RSA",
1434 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001435 MinVersion: ver.version,
1436 MaxVersion: ver.version,
David Benjamin67666e72014-07-12 15:47:52 -04001437 Certificates: []Certificate{rsaCertificate},
1438 },
1439 flags: []string{"-require-any-client-certificate"},
1440 })
David Benjamine098ec22014-08-27 23:13:20 -04001441 if ver.version != VersionSSL30 {
1442 testCases = append(testCases, testCase{
1443 testType: serverTest,
1444 name: ver.name + "-Server-ClientAuth-ECDSA",
1445 config: Config{
1446 MinVersion: ver.version,
1447 MaxVersion: ver.version,
1448 Certificates: []Certificate{ecdsaCertificate},
1449 },
1450 flags: []string{"-require-any-client-certificate"},
1451 })
1452 testCases = append(testCases, testCase{
1453 testType: clientTest,
1454 name: ver.name + "-Client-ClientAuth-ECDSA",
1455 config: Config{
1456 MinVersion: ver.version,
1457 MaxVersion: ver.version,
1458 ClientAuth: RequireAnyClientCert,
1459 ClientCAs: certPool,
1460 },
1461 flags: []string{
1462 "-cert-file", ecdsaCertificateFile,
1463 "-key-file", ecdsaKeyFile,
1464 },
1465 })
1466 }
David Benjamin636293b2014-07-08 17:59:18 -04001467 }
1468}
1469
Adam Langley75712922014-10-10 16:23:43 -07001470func addExtendedMasterSecretTests() {
1471 const expectEMSFlag = "-expect-extended-master-secret"
1472
1473 for _, with := range []bool{false, true} {
1474 prefix := "No"
1475 var flags []string
1476 if with {
1477 prefix = ""
1478 flags = []string{expectEMSFlag}
1479 }
1480
1481 for _, isClient := range []bool{false, true} {
1482 suffix := "-Server"
1483 testType := serverTest
1484 if isClient {
1485 suffix = "-Client"
1486 testType = clientTest
1487 }
1488
1489 for _, ver := range tlsVersions {
1490 test := testCase{
1491 testType: testType,
1492 name: prefix + "ExtendedMasterSecret-" + ver.name + suffix,
1493 config: Config{
1494 MinVersion: ver.version,
1495 MaxVersion: ver.version,
1496 Bugs: ProtocolBugs{
1497 NoExtendedMasterSecret: !with,
1498 RequireExtendedMasterSecret: with,
1499 },
1500 },
David Benjamin48cae082014-10-27 01:06:24 -04001501 flags: flags,
1502 shouldFail: ver.version == VersionSSL30 && with,
Adam Langley75712922014-10-10 16:23:43 -07001503 }
1504 if test.shouldFail {
1505 test.expectedLocalError = "extended master secret required but not supported by peer"
1506 }
1507 testCases = append(testCases, test)
1508 }
1509 }
1510 }
1511
1512 // When a session is resumed, it should still be aware that its master
1513 // secret was generated via EMS and thus it's safe to use tls-unique.
1514 testCases = append(testCases, testCase{
1515 name: "ExtendedMasterSecret-Resume",
1516 config: Config{
1517 Bugs: ProtocolBugs{
1518 RequireExtendedMasterSecret: true,
1519 },
1520 },
1521 flags: []string{expectEMSFlag},
1522 resumeSession: true,
1523 })
1524}
1525
David Benjamin43ec06f2014-08-05 02:28:57 -04001526// Adds tests that try to cover the range of the handshake state machine, under
1527// various conditions. Some of these are redundant with other tests, but they
1528// only cover the synchronous case.
David Benjamin6fd297b2014-08-11 18:43:38 -04001529func addStateMachineCoverageTests(async, splitHandshake bool, protocol protocol) {
David Benjamin43ec06f2014-08-05 02:28:57 -04001530 var suffix string
1531 var flags []string
1532 var maxHandshakeRecordLength int
David Benjamin6fd297b2014-08-11 18:43:38 -04001533 if protocol == dtls {
1534 suffix = "-DTLS"
1535 }
David Benjamin43ec06f2014-08-05 02:28:57 -04001536 if async {
David Benjamin6fd297b2014-08-11 18:43:38 -04001537 suffix += "-Async"
David Benjamin43ec06f2014-08-05 02:28:57 -04001538 flags = append(flags, "-async")
1539 } else {
David Benjamin6fd297b2014-08-11 18:43:38 -04001540 suffix += "-Sync"
David Benjamin43ec06f2014-08-05 02:28:57 -04001541 }
1542 if splitHandshake {
1543 suffix += "-SplitHandshakeRecords"
David Benjamin98214542014-08-07 18:02:39 -04001544 maxHandshakeRecordLength = 1
David Benjamin43ec06f2014-08-05 02:28:57 -04001545 }
1546
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001547 // Basic handshake, with resumption. Client and server,
1548 // session ID and session ticket.
David Benjamin43ec06f2014-08-05 02:28:57 -04001549 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001550 protocol: protocol,
1551 name: "Basic-Client" + suffix,
David Benjamin43ec06f2014-08-05 02:28:57 -04001552 config: Config{
1553 Bugs: ProtocolBugs{
1554 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1555 },
1556 },
David Benjaminbed9aae2014-08-07 19:13:38 -04001557 flags: flags,
1558 resumeSession: true,
1559 })
1560 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001561 protocol: protocol,
1562 name: "Basic-Client-RenewTicket" + suffix,
David Benjaminbed9aae2014-08-07 19:13:38 -04001563 config: Config{
1564 Bugs: ProtocolBugs{
1565 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1566 RenewTicketOnResume: true,
1567 },
1568 },
1569 flags: flags,
1570 resumeSession: true,
David Benjamin43ec06f2014-08-05 02:28:57 -04001571 })
1572 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001573 protocol: protocol,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001574 name: "Basic-Client-NoTicket" + suffix,
1575 config: Config{
1576 SessionTicketsDisabled: true,
1577 Bugs: ProtocolBugs{
1578 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1579 },
1580 },
1581 flags: flags,
1582 resumeSession: true,
1583 })
1584 testCases = append(testCases, testCase{
1585 protocol: protocol,
David Benjamine0e7d0d2015-02-08 19:33:25 -05001586 name: "Basic-Client-Implicit" + suffix,
1587 config: Config{
1588 Bugs: ProtocolBugs{
1589 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1590 },
1591 },
1592 flags: append(flags, "-implicit-handshake"),
1593 resumeSession: true,
1594 })
1595 testCases = append(testCases, testCase{
1596 protocol: protocol,
David Benjamin43ec06f2014-08-05 02:28:57 -04001597 testType: serverTest,
1598 name: "Basic-Server" + suffix,
1599 config: Config{
1600 Bugs: ProtocolBugs{
1601 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1602 },
1603 },
David Benjaminbed9aae2014-08-07 19:13:38 -04001604 flags: flags,
1605 resumeSession: true,
David Benjamin43ec06f2014-08-05 02:28:57 -04001606 })
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001607 testCases = append(testCases, testCase{
1608 protocol: protocol,
1609 testType: serverTest,
1610 name: "Basic-Server-NoTickets" + suffix,
1611 config: Config{
1612 SessionTicketsDisabled: true,
1613 Bugs: ProtocolBugs{
1614 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1615 },
1616 },
1617 flags: flags,
1618 resumeSession: true,
1619 })
David Benjamine0e7d0d2015-02-08 19:33:25 -05001620 testCases = append(testCases, testCase{
1621 protocol: protocol,
1622 testType: serverTest,
1623 name: "Basic-Server-Implicit" + suffix,
1624 config: Config{
1625 Bugs: ProtocolBugs{
1626 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1627 },
1628 },
1629 flags: append(flags, "-implicit-handshake"),
1630 resumeSession: true,
1631 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001632
David Benjamin6fd297b2014-08-11 18:43:38 -04001633 // TLS client auth.
1634 testCases = append(testCases, testCase{
1635 protocol: protocol,
1636 testType: clientTest,
1637 name: "ClientAuth-Client" + suffix,
1638 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001639 ClientAuth: RequireAnyClientCert,
David Benjamin6fd297b2014-08-11 18:43:38 -04001640 Bugs: ProtocolBugs{
1641 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1642 },
1643 },
1644 flags: append(flags,
1645 "-cert-file", rsaCertificateFile,
1646 "-key-file", rsaKeyFile),
1647 })
1648 testCases = append(testCases, testCase{
1649 protocol: protocol,
1650 testType: serverTest,
1651 name: "ClientAuth-Server" + suffix,
1652 config: Config{
1653 Certificates: []Certificate{rsaCertificate},
1654 },
1655 flags: append(flags, "-require-any-client-certificate"),
1656 })
1657
David Benjamin43ec06f2014-08-05 02:28:57 -04001658 // No session ticket support; server doesn't send NewSessionTicket.
1659 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001660 protocol: protocol,
1661 name: "SessionTicketsDisabled-Client" + suffix,
David Benjamin43ec06f2014-08-05 02:28:57 -04001662 config: Config{
1663 SessionTicketsDisabled: true,
1664 Bugs: ProtocolBugs{
1665 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1666 },
1667 },
1668 flags: flags,
1669 })
1670 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001671 protocol: protocol,
David Benjamin43ec06f2014-08-05 02:28:57 -04001672 testType: serverTest,
1673 name: "SessionTicketsDisabled-Server" + suffix,
1674 config: Config{
1675 SessionTicketsDisabled: true,
1676 Bugs: ProtocolBugs{
1677 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1678 },
1679 },
1680 flags: flags,
1681 })
1682
David Benjamin48cae082014-10-27 01:06:24 -04001683 // Skip ServerKeyExchange in PSK key exchange if there's no
1684 // identity hint.
1685 testCases = append(testCases, testCase{
1686 protocol: protocol,
1687 name: "EmptyPSKHint-Client" + suffix,
1688 config: Config{
1689 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
1690 PreSharedKey: []byte("secret"),
1691 Bugs: ProtocolBugs{
1692 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1693 },
1694 },
1695 flags: append(flags, "-psk", "secret"),
1696 })
1697 testCases = append(testCases, testCase{
1698 protocol: protocol,
1699 testType: serverTest,
1700 name: "EmptyPSKHint-Server" + suffix,
1701 config: Config{
1702 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
1703 PreSharedKey: []byte("secret"),
1704 Bugs: ProtocolBugs{
1705 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1706 },
1707 },
1708 flags: append(flags, "-psk", "secret"),
1709 })
1710
David Benjamin6fd297b2014-08-11 18:43:38 -04001711 if protocol == tls {
1712 // NPN on client and server; results in post-handshake message.
1713 testCases = append(testCases, testCase{
1714 protocol: protocol,
1715 name: "NPN-Client" + suffix,
1716 config: Config{
David Benjaminae2888f2014-09-06 12:58:58 -04001717 NextProtos: []string{"foo"},
David Benjamin6fd297b2014-08-11 18:43:38 -04001718 Bugs: ProtocolBugs{
1719 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1720 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001721 },
David Benjaminfc7b0862014-09-06 13:21:53 -04001722 flags: append(flags, "-select-next-proto", "foo"),
1723 expectedNextProto: "foo",
1724 expectedNextProtoType: npn,
David Benjamin6fd297b2014-08-11 18:43:38 -04001725 })
1726 testCases = append(testCases, testCase{
1727 protocol: protocol,
1728 testType: serverTest,
1729 name: "NPN-Server" + suffix,
1730 config: Config{
1731 NextProtos: []string{"bar"},
1732 Bugs: ProtocolBugs{
1733 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1734 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001735 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001736 flags: append(flags,
1737 "-advertise-npn", "\x03foo\x03bar\x03baz",
1738 "-expect-next-proto", "bar"),
David Benjaminfc7b0862014-09-06 13:21:53 -04001739 expectedNextProto: "bar",
1740 expectedNextProtoType: npn,
David Benjamin6fd297b2014-08-11 18:43:38 -04001741 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001742
David Benjamin6fd297b2014-08-11 18:43:38 -04001743 // Client does False Start and negotiates NPN.
1744 testCases = append(testCases, testCase{
1745 protocol: protocol,
1746 name: "FalseStart" + suffix,
1747 config: Config{
1748 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1749 NextProtos: []string{"foo"},
1750 Bugs: ProtocolBugs{
David Benjamine58c4f52014-08-24 03:47:07 -04001751 ExpectFalseStart: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001752 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1753 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001754 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001755 flags: append(flags,
1756 "-false-start",
1757 "-select-next-proto", "foo"),
David Benjamine58c4f52014-08-24 03:47:07 -04001758 shimWritesFirst: true,
1759 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001760 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001761
David Benjaminae2888f2014-09-06 12:58:58 -04001762 // Client does False Start and negotiates ALPN.
1763 testCases = append(testCases, testCase{
1764 protocol: protocol,
1765 name: "FalseStart-ALPN" + suffix,
1766 config: Config{
1767 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1768 NextProtos: []string{"foo"},
1769 Bugs: ProtocolBugs{
1770 ExpectFalseStart: true,
1771 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1772 },
1773 },
1774 flags: append(flags,
1775 "-false-start",
1776 "-advertise-alpn", "\x03foo"),
1777 shimWritesFirst: true,
1778 resumeSession: true,
1779 })
1780
David Benjamin931ab342015-02-08 19:46:57 -05001781 // Client does False Start but doesn't explicitly call
1782 // SSL_connect.
1783 testCases = append(testCases, testCase{
1784 protocol: protocol,
1785 name: "FalseStart-Implicit" + suffix,
1786 config: Config{
1787 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1788 NextProtos: []string{"foo"},
1789 Bugs: ProtocolBugs{
1790 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1791 },
1792 },
1793 flags: append(flags,
1794 "-implicit-handshake",
1795 "-false-start",
1796 "-advertise-alpn", "\x03foo"),
1797 })
1798
David Benjamin6fd297b2014-08-11 18:43:38 -04001799 // False Start without session tickets.
1800 testCases = append(testCases, testCase{
1801 name: "FalseStart-SessionTicketsDisabled",
1802 config: Config{
1803 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1804 NextProtos: []string{"foo"},
1805 SessionTicketsDisabled: true,
David Benjamin4e99c522014-08-24 01:45:30 -04001806 Bugs: ProtocolBugs{
David Benjamine58c4f52014-08-24 03:47:07 -04001807 ExpectFalseStart: true,
David Benjamin4e99c522014-08-24 01:45:30 -04001808 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1809 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001810 },
David Benjamin4e99c522014-08-24 01:45:30 -04001811 flags: append(flags,
David Benjamin6fd297b2014-08-11 18:43:38 -04001812 "-false-start",
1813 "-select-next-proto", "foo",
David Benjamin4e99c522014-08-24 01:45:30 -04001814 ),
David Benjamine58c4f52014-08-24 03:47:07 -04001815 shimWritesFirst: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001816 })
David Benjamin1e7f8d72014-08-08 12:27:04 -04001817
David Benjamina08e49d2014-08-24 01:46:07 -04001818 // Server parses a V2ClientHello.
David Benjamin6fd297b2014-08-11 18:43:38 -04001819 testCases = append(testCases, testCase{
1820 protocol: protocol,
1821 testType: serverTest,
1822 name: "SendV2ClientHello" + suffix,
1823 config: Config{
1824 // Choose a cipher suite that does not involve
1825 // elliptic curves, so no extensions are
1826 // involved.
1827 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
1828 Bugs: ProtocolBugs{
1829 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1830 SendV2ClientHello: true,
1831 },
David Benjamin1e7f8d72014-08-08 12:27:04 -04001832 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001833 flags: flags,
1834 })
David Benjamina08e49d2014-08-24 01:46:07 -04001835
1836 // Client sends a Channel ID.
1837 testCases = append(testCases, testCase{
1838 protocol: protocol,
1839 name: "ChannelID-Client" + suffix,
1840 config: Config{
1841 RequestChannelID: true,
1842 Bugs: ProtocolBugs{
1843 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1844 },
1845 },
1846 flags: append(flags,
1847 "-send-channel-id", channelIDKeyFile,
1848 ),
1849 resumeSession: true,
1850 expectChannelID: true,
1851 })
1852
1853 // Server accepts a Channel ID.
1854 testCases = append(testCases, testCase{
1855 protocol: protocol,
1856 testType: serverTest,
1857 name: "ChannelID-Server" + suffix,
1858 config: Config{
1859 ChannelID: channelIDKey,
1860 Bugs: ProtocolBugs{
1861 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1862 },
1863 },
1864 flags: append(flags,
1865 "-expect-channel-id",
1866 base64.StdEncoding.EncodeToString(channelIDBytes),
1867 ),
1868 resumeSession: true,
1869 expectChannelID: true,
1870 })
David Benjamin6fd297b2014-08-11 18:43:38 -04001871 } else {
1872 testCases = append(testCases, testCase{
1873 protocol: protocol,
1874 name: "SkipHelloVerifyRequest" + suffix,
1875 config: Config{
1876 Bugs: ProtocolBugs{
1877 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1878 SkipHelloVerifyRequest: true,
1879 },
1880 },
1881 flags: flags,
1882 })
David Benjamin6fd297b2014-08-11 18:43:38 -04001883 }
David Benjamin43ec06f2014-08-05 02:28:57 -04001884}
1885
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001886func addVersionNegotiationTests() {
1887 for i, shimVers := range tlsVersions {
1888 // Assemble flags to disable all newer versions on the shim.
1889 var flags []string
1890 for _, vers := range tlsVersions[i+1:] {
1891 flags = append(flags, vers.flag)
1892 }
1893
1894 for _, runnerVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05001895 protocols := []protocol{tls}
1896 if runnerVers.hasDTLS && shimVers.hasDTLS {
1897 protocols = append(protocols, dtls)
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001898 }
David Benjamin8b8c0062014-11-23 02:47:52 -05001899 for _, protocol := range protocols {
1900 expectedVersion := shimVers.version
1901 if runnerVers.version < shimVers.version {
1902 expectedVersion = runnerVers.version
1903 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001904
David Benjamin8b8c0062014-11-23 02:47:52 -05001905 suffix := shimVers.name + "-" + runnerVers.name
1906 if protocol == dtls {
1907 suffix += "-DTLS"
1908 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001909
David Benjamin1eb367c2014-12-12 18:17:51 -05001910 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
1911
David Benjamin1e29a6b2014-12-10 02:27:24 -05001912 clientVers := shimVers.version
1913 if clientVers > VersionTLS10 {
1914 clientVers = VersionTLS10
1915 }
David Benjamin8b8c0062014-11-23 02:47:52 -05001916 testCases = append(testCases, testCase{
1917 protocol: protocol,
1918 testType: clientTest,
1919 name: "VersionNegotiation-Client-" + suffix,
1920 config: Config{
1921 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05001922 Bugs: ProtocolBugs{
1923 ExpectInitialRecordVersion: clientVers,
1924 },
David Benjamin8b8c0062014-11-23 02:47:52 -05001925 },
1926 flags: flags,
1927 expectedVersion: expectedVersion,
1928 })
David Benjamin1eb367c2014-12-12 18:17:51 -05001929 testCases = append(testCases, testCase{
1930 protocol: protocol,
1931 testType: clientTest,
1932 name: "VersionNegotiation-Client2-" + suffix,
1933 config: Config{
1934 MaxVersion: runnerVers.version,
1935 Bugs: ProtocolBugs{
1936 ExpectInitialRecordVersion: clientVers,
1937 },
1938 },
1939 flags: []string{"-max-version", shimVersFlag},
1940 expectedVersion: expectedVersion,
1941 })
David Benjamin8b8c0062014-11-23 02:47:52 -05001942
1943 testCases = append(testCases, testCase{
1944 protocol: protocol,
1945 testType: serverTest,
1946 name: "VersionNegotiation-Server-" + suffix,
1947 config: Config{
1948 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05001949 Bugs: ProtocolBugs{
1950 ExpectInitialRecordVersion: expectedVersion,
1951 },
David Benjamin8b8c0062014-11-23 02:47:52 -05001952 },
1953 flags: flags,
1954 expectedVersion: expectedVersion,
1955 })
David Benjamin1eb367c2014-12-12 18:17:51 -05001956 testCases = append(testCases, testCase{
1957 protocol: protocol,
1958 testType: serverTest,
1959 name: "VersionNegotiation-Server2-" + suffix,
1960 config: Config{
1961 MaxVersion: runnerVers.version,
1962 Bugs: ProtocolBugs{
1963 ExpectInitialRecordVersion: expectedVersion,
1964 },
1965 },
1966 flags: []string{"-max-version", shimVersFlag},
1967 expectedVersion: expectedVersion,
1968 })
David Benjamin8b8c0062014-11-23 02:47:52 -05001969 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001970 }
1971 }
1972}
1973
David Benjaminaccb4542014-12-12 23:44:33 -05001974func addMinimumVersionTests() {
1975 for i, shimVers := range tlsVersions {
1976 // Assemble flags to disable all older versions on the shim.
1977 var flags []string
1978 for _, vers := range tlsVersions[:i] {
1979 flags = append(flags, vers.flag)
1980 }
1981
1982 for _, runnerVers := range tlsVersions {
1983 protocols := []protocol{tls}
1984 if runnerVers.hasDTLS && shimVers.hasDTLS {
1985 protocols = append(protocols, dtls)
1986 }
1987 for _, protocol := range protocols {
1988 suffix := shimVers.name + "-" + runnerVers.name
1989 if protocol == dtls {
1990 suffix += "-DTLS"
1991 }
1992 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
1993
David Benjaminaccb4542014-12-12 23:44:33 -05001994 var expectedVersion uint16
1995 var shouldFail bool
1996 var expectedError string
David Benjamin87909c02014-12-13 01:55:01 -05001997 var expectedLocalError string
David Benjaminaccb4542014-12-12 23:44:33 -05001998 if runnerVers.version >= shimVers.version {
1999 expectedVersion = runnerVers.version
2000 } else {
2001 shouldFail = true
2002 expectedError = ":UNSUPPORTED_PROTOCOL:"
David Benjamin87909c02014-12-13 01:55:01 -05002003 if runnerVers.version > VersionSSL30 {
2004 expectedLocalError = "remote error: protocol version not supported"
2005 } else {
2006 expectedLocalError = "remote error: handshake failure"
2007 }
David Benjaminaccb4542014-12-12 23:44:33 -05002008 }
2009
2010 testCases = append(testCases, testCase{
2011 protocol: protocol,
2012 testType: clientTest,
2013 name: "MinimumVersion-Client-" + suffix,
2014 config: Config{
2015 MaxVersion: runnerVers.version,
2016 },
David Benjamin87909c02014-12-13 01:55:01 -05002017 flags: flags,
2018 expectedVersion: expectedVersion,
2019 shouldFail: shouldFail,
2020 expectedError: expectedError,
2021 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002022 })
2023 testCases = append(testCases, testCase{
2024 protocol: protocol,
2025 testType: clientTest,
2026 name: "MinimumVersion-Client2-" + suffix,
2027 config: Config{
2028 MaxVersion: runnerVers.version,
2029 },
David Benjamin87909c02014-12-13 01:55:01 -05002030 flags: []string{"-min-version", shimVersFlag},
2031 expectedVersion: expectedVersion,
2032 shouldFail: shouldFail,
2033 expectedError: expectedError,
2034 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002035 })
2036
2037 testCases = append(testCases, testCase{
2038 protocol: protocol,
2039 testType: serverTest,
2040 name: "MinimumVersion-Server-" + suffix,
2041 config: Config{
2042 MaxVersion: runnerVers.version,
2043 },
David Benjamin87909c02014-12-13 01:55:01 -05002044 flags: flags,
2045 expectedVersion: expectedVersion,
2046 shouldFail: shouldFail,
2047 expectedError: expectedError,
2048 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002049 })
2050 testCases = append(testCases, testCase{
2051 protocol: protocol,
2052 testType: serverTest,
2053 name: "MinimumVersion-Server2-" + suffix,
2054 config: Config{
2055 MaxVersion: runnerVers.version,
2056 },
David Benjamin87909c02014-12-13 01:55:01 -05002057 flags: []string{"-min-version", shimVersFlag},
2058 expectedVersion: expectedVersion,
2059 shouldFail: shouldFail,
2060 expectedError: expectedError,
2061 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002062 })
2063 }
2064 }
2065 }
2066}
2067
David Benjamin5c24a1d2014-08-31 00:59:27 -04002068func addD5BugTests() {
2069 testCases = append(testCases, testCase{
2070 testType: serverTest,
2071 name: "D5Bug-NoQuirk-Reject",
2072 config: Config{
2073 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2074 Bugs: ProtocolBugs{
2075 SSL3RSAKeyExchange: true,
2076 },
2077 },
2078 shouldFail: true,
2079 expectedError: ":TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG:",
2080 })
2081 testCases = append(testCases, testCase{
2082 testType: serverTest,
2083 name: "D5Bug-Quirk-Normal",
2084 config: Config{
2085 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2086 },
2087 flags: []string{"-tls-d5-bug"},
2088 })
2089 testCases = append(testCases, testCase{
2090 testType: serverTest,
2091 name: "D5Bug-Quirk-Bug",
2092 config: Config{
2093 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2094 Bugs: ProtocolBugs{
2095 SSL3RSAKeyExchange: true,
2096 },
2097 },
2098 flags: []string{"-tls-d5-bug"},
2099 })
2100}
2101
David Benjamine78bfde2014-09-06 12:45:15 -04002102func addExtensionTests() {
2103 testCases = append(testCases, testCase{
2104 testType: clientTest,
2105 name: "DuplicateExtensionClient",
2106 config: Config{
2107 Bugs: ProtocolBugs{
2108 DuplicateExtension: true,
2109 },
2110 },
2111 shouldFail: true,
2112 expectedLocalError: "remote error: error decoding message",
2113 })
2114 testCases = append(testCases, testCase{
2115 testType: serverTest,
2116 name: "DuplicateExtensionServer",
2117 config: Config{
2118 Bugs: ProtocolBugs{
2119 DuplicateExtension: true,
2120 },
2121 },
2122 shouldFail: true,
2123 expectedLocalError: "remote error: error decoding message",
2124 })
2125 testCases = append(testCases, testCase{
2126 testType: clientTest,
2127 name: "ServerNameExtensionClient",
2128 config: Config{
2129 Bugs: ProtocolBugs{
2130 ExpectServerName: "example.com",
2131 },
2132 },
2133 flags: []string{"-host-name", "example.com"},
2134 })
2135 testCases = append(testCases, testCase{
2136 testType: clientTest,
2137 name: "ServerNameExtensionClient",
2138 config: Config{
2139 Bugs: ProtocolBugs{
2140 ExpectServerName: "mismatch.com",
2141 },
2142 },
2143 flags: []string{"-host-name", "example.com"},
2144 shouldFail: true,
2145 expectedLocalError: "tls: unexpected server name",
2146 })
2147 testCases = append(testCases, testCase{
2148 testType: clientTest,
2149 name: "ServerNameExtensionClient",
2150 config: Config{
2151 Bugs: ProtocolBugs{
2152 ExpectServerName: "missing.com",
2153 },
2154 },
2155 shouldFail: true,
2156 expectedLocalError: "tls: unexpected server name",
2157 })
2158 testCases = append(testCases, testCase{
2159 testType: serverTest,
2160 name: "ServerNameExtensionServer",
2161 config: Config{
2162 ServerName: "example.com",
2163 },
2164 flags: []string{"-expect-server-name", "example.com"},
2165 resumeSession: true,
2166 })
David Benjaminae2888f2014-09-06 12:58:58 -04002167 testCases = append(testCases, testCase{
2168 testType: clientTest,
2169 name: "ALPNClient",
2170 config: Config{
2171 NextProtos: []string{"foo"},
2172 },
2173 flags: []string{
2174 "-advertise-alpn", "\x03foo\x03bar\x03baz",
2175 "-expect-alpn", "foo",
2176 },
David Benjaminfc7b0862014-09-06 13:21:53 -04002177 expectedNextProto: "foo",
2178 expectedNextProtoType: alpn,
2179 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04002180 })
2181 testCases = append(testCases, testCase{
2182 testType: serverTest,
2183 name: "ALPNServer",
2184 config: Config{
2185 NextProtos: []string{"foo", "bar", "baz"},
2186 },
2187 flags: []string{
2188 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2189 "-select-alpn", "foo",
2190 },
David Benjaminfc7b0862014-09-06 13:21:53 -04002191 expectedNextProto: "foo",
2192 expectedNextProtoType: alpn,
2193 resumeSession: true,
2194 })
2195 // Test that the server prefers ALPN over NPN.
2196 testCases = append(testCases, testCase{
2197 testType: serverTest,
2198 name: "ALPNServer-Preferred",
2199 config: Config{
2200 NextProtos: []string{"foo", "bar", "baz"},
2201 },
2202 flags: []string{
2203 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2204 "-select-alpn", "foo",
2205 "-advertise-npn", "\x03foo\x03bar\x03baz",
2206 },
2207 expectedNextProto: "foo",
2208 expectedNextProtoType: alpn,
2209 resumeSession: true,
2210 })
2211 testCases = append(testCases, testCase{
2212 testType: serverTest,
2213 name: "ALPNServer-Preferred-Swapped",
2214 config: Config{
2215 NextProtos: []string{"foo", "bar", "baz"},
2216 Bugs: ProtocolBugs{
2217 SwapNPNAndALPN: true,
2218 },
2219 },
2220 flags: []string{
2221 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2222 "-select-alpn", "foo",
2223 "-advertise-npn", "\x03foo\x03bar\x03baz",
2224 },
2225 expectedNextProto: "foo",
2226 expectedNextProtoType: alpn,
2227 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04002228 })
Adam Langley38311732014-10-16 19:04:35 -07002229 // Resume with a corrupt ticket.
2230 testCases = append(testCases, testCase{
2231 testType: serverTest,
2232 name: "CorruptTicket",
2233 config: Config{
2234 Bugs: ProtocolBugs{
2235 CorruptTicket: true,
2236 },
2237 },
2238 resumeSession: true,
2239 flags: []string{"-expect-session-miss"},
2240 })
2241 // Resume with an oversized session id.
2242 testCases = append(testCases, testCase{
2243 testType: serverTest,
2244 name: "OversizedSessionId",
2245 config: Config{
2246 Bugs: ProtocolBugs{
2247 OversizedSessionId: true,
2248 },
2249 },
2250 resumeSession: true,
Adam Langley75712922014-10-10 16:23:43 -07002251 shouldFail: true,
Adam Langley38311732014-10-16 19:04:35 -07002252 expectedError: ":DECODE_ERROR:",
2253 })
David Benjaminca6c8262014-11-15 19:06:08 -05002254 // Basic DTLS-SRTP tests. Include fake profiles to ensure they
2255 // are ignored.
2256 testCases = append(testCases, testCase{
2257 protocol: dtls,
2258 name: "SRTP-Client",
2259 config: Config{
2260 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
2261 },
2262 flags: []string{
2263 "-srtp-profiles",
2264 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2265 },
2266 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2267 })
2268 testCases = append(testCases, testCase{
2269 protocol: dtls,
2270 testType: serverTest,
2271 name: "SRTP-Server",
2272 config: Config{
2273 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
2274 },
2275 flags: []string{
2276 "-srtp-profiles",
2277 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2278 },
2279 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2280 })
2281 // Test that the MKI is ignored.
2282 testCases = append(testCases, testCase{
2283 protocol: dtls,
2284 testType: serverTest,
2285 name: "SRTP-Server-IgnoreMKI",
2286 config: Config{
2287 SRTPProtectionProfiles: []uint16{SRTP_AES128_CM_HMAC_SHA1_80},
2288 Bugs: ProtocolBugs{
2289 SRTPMasterKeyIdentifer: "bogus",
2290 },
2291 },
2292 flags: []string{
2293 "-srtp-profiles",
2294 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2295 },
2296 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2297 })
2298 // Test that SRTP isn't negotiated on the server if there were
2299 // no matching profiles.
2300 testCases = append(testCases, testCase{
2301 protocol: dtls,
2302 testType: serverTest,
2303 name: "SRTP-Server-NoMatch",
2304 config: Config{
2305 SRTPProtectionProfiles: []uint16{100, 101, 102},
2306 },
2307 flags: []string{
2308 "-srtp-profiles",
2309 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2310 },
2311 expectedSRTPProtectionProfile: 0,
2312 })
2313 // Test that the server returning an invalid SRTP profile is
2314 // flagged as an error by the client.
2315 testCases = append(testCases, testCase{
2316 protocol: dtls,
2317 name: "SRTP-Client-NoMatch",
2318 config: Config{
2319 Bugs: ProtocolBugs{
2320 SendSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_32,
2321 },
2322 },
2323 flags: []string{
2324 "-srtp-profiles",
2325 "SRTP_AES128_CM_SHA1_80",
2326 },
2327 shouldFail: true,
2328 expectedError: ":BAD_SRTP_PROTECTION_PROFILE_LIST:",
2329 })
David Benjamin61f95272014-11-25 01:55:35 -05002330 // Test OCSP stapling and SCT list.
2331 testCases = append(testCases, testCase{
2332 name: "OCSPStapling",
2333 flags: []string{
2334 "-enable-ocsp-stapling",
2335 "-expect-ocsp-response",
2336 base64.StdEncoding.EncodeToString(testOCSPResponse),
2337 },
2338 })
2339 testCases = append(testCases, testCase{
2340 name: "SignedCertificateTimestampList",
2341 flags: []string{
2342 "-enable-signed-cert-timestamps",
2343 "-expect-signed-cert-timestamps",
2344 base64.StdEncoding.EncodeToString(testSCTList),
2345 },
2346 })
David Benjamine78bfde2014-09-06 12:45:15 -04002347}
2348
David Benjamin01fe8202014-09-24 15:21:44 -04002349func addResumptionVersionTests() {
David Benjamin01fe8202014-09-24 15:21:44 -04002350 for _, sessionVers := range tlsVersions {
David Benjamin01fe8202014-09-24 15:21:44 -04002351 for _, resumeVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05002352 protocols := []protocol{tls}
2353 if sessionVers.hasDTLS && resumeVers.hasDTLS {
2354 protocols = append(protocols, dtls)
David Benjaminbdf5e722014-11-11 00:52:15 -05002355 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002356 for _, protocol := range protocols {
2357 suffix := "-" + sessionVers.name + "-" + resumeVers.name
2358 if protocol == dtls {
2359 suffix += "-DTLS"
2360 }
2361
2362 testCases = append(testCases, testCase{
2363 protocol: protocol,
2364 name: "Resume-Client" + suffix,
2365 resumeSession: true,
2366 config: Config{
2367 MaxVersion: sessionVers.version,
2368 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2369 Bugs: ProtocolBugs{
2370 AllowSessionVersionMismatch: true,
2371 },
2372 },
2373 expectedVersion: sessionVers.version,
2374 resumeConfig: &Config{
2375 MaxVersion: resumeVers.version,
2376 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2377 Bugs: ProtocolBugs{
2378 AllowSessionVersionMismatch: true,
2379 },
2380 },
2381 expectedResumeVersion: resumeVers.version,
2382 })
2383
2384 testCases = append(testCases, testCase{
2385 protocol: protocol,
2386 name: "Resume-Client-NoResume" + suffix,
2387 flags: []string{"-expect-session-miss"},
2388 resumeSession: true,
2389 config: Config{
2390 MaxVersion: sessionVers.version,
2391 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2392 },
2393 expectedVersion: sessionVers.version,
2394 resumeConfig: &Config{
2395 MaxVersion: resumeVers.version,
2396 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2397 },
2398 newSessionsOnResume: true,
2399 expectedResumeVersion: resumeVers.version,
2400 })
2401
2402 var flags []string
2403 if sessionVers.version != resumeVers.version {
2404 flags = append(flags, "-expect-session-miss")
2405 }
2406 testCases = append(testCases, testCase{
2407 protocol: protocol,
2408 testType: serverTest,
2409 name: "Resume-Server" + suffix,
2410 flags: flags,
2411 resumeSession: true,
2412 config: Config{
2413 MaxVersion: sessionVers.version,
2414 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2415 },
2416 expectedVersion: sessionVers.version,
2417 resumeConfig: &Config{
2418 MaxVersion: resumeVers.version,
2419 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2420 },
2421 expectedResumeVersion: resumeVers.version,
2422 })
2423 }
David Benjamin01fe8202014-09-24 15:21:44 -04002424 }
2425 }
2426}
2427
Adam Langley2ae77d22014-10-28 17:29:33 -07002428func addRenegotiationTests() {
2429 testCases = append(testCases, testCase{
2430 testType: serverTest,
2431 name: "Renegotiate-Server",
2432 flags: []string{"-renegotiate"},
2433 shimWritesFirst: true,
2434 })
2435 testCases = append(testCases, testCase{
2436 testType: serverTest,
2437 name: "Renegotiate-Server-EmptyExt",
2438 config: Config{
2439 Bugs: ProtocolBugs{
2440 EmptyRenegotiationInfo: true,
2441 },
2442 },
2443 flags: []string{"-renegotiate"},
2444 shimWritesFirst: true,
2445 shouldFail: true,
2446 expectedError: ":RENEGOTIATION_MISMATCH:",
2447 })
2448 testCases = append(testCases, testCase{
2449 testType: serverTest,
2450 name: "Renegotiate-Server-BadExt",
2451 config: Config{
2452 Bugs: ProtocolBugs{
2453 BadRenegotiationInfo: true,
2454 },
2455 },
2456 flags: []string{"-renegotiate"},
2457 shimWritesFirst: true,
2458 shouldFail: true,
2459 expectedError: ":RENEGOTIATION_MISMATCH:",
2460 })
David Benjaminca6554b2014-11-08 12:31:52 -05002461 testCases = append(testCases, testCase{
2462 testType: serverTest,
2463 name: "Renegotiate-Server-ClientInitiated",
2464 renegotiate: true,
2465 })
2466 testCases = append(testCases, testCase{
2467 testType: serverTest,
2468 name: "Renegotiate-Server-ClientInitiated-NoExt",
2469 renegotiate: true,
2470 config: Config{
2471 Bugs: ProtocolBugs{
2472 NoRenegotiationInfo: true,
2473 },
2474 },
2475 shouldFail: true,
2476 expectedError: ":UNSAFE_LEGACY_RENEGOTIATION_DISABLED:",
2477 })
2478 testCases = append(testCases, testCase{
2479 testType: serverTest,
2480 name: "Renegotiate-Server-ClientInitiated-NoExt-Allowed",
2481 renegotiate: true,
2482 config: Config{
2483 Bugs: ProtocolBugs{
2484 NoRenegotiationInfo: true,
2485 },
2486 },
2487 flags: []string{"-allow-unsafe-legacy-renegotiation"},
2488 })
Adam Langley2ae77d22014-10-28 17:29:33 -07002489 // TODO(agl): test the renegotiation info SCSV.
Adam Langleycf2d4f42014-10-28 19:06:14 -07002490 testCases = append(testCases, testCase{
2491 name: "Renegotiate-Client",
2492 renegotiate: true,
2493 })
2494 testCases = append(testCases, testCase{
2495 name: "Renegotiate-Client-EmptyExt",
2496 renegotiate: true,
2497 config: Config{
2498 Bugs: ProtocolBugs{
2499 EmptyRenegotiationInfo: true,
2500 },
2501 },
2502 shouldFail: true,
2503 expectedError: ":RENEGOTIATION_MISMATCH:",
2504 })
2505 testCases = append(testCases, testCase{
2506 name: "Renegotiate-Client-BadExt",
2507 renegotiate: true,
2508 config: Config{
2509 Bugs: ProtocolBugs{
2510 BadRenegotiationInfo: true,
2511 },
2512 },
2513 shouldFail: true,
2514 expectedError: ":RENEGOTIATION_MISMATCH:",
2515 })
2516 testCases = append(testCases, testCase{
2517 name: "Renegotiate-Client-SwitchCiphers",
2518 renegotiate: true,
2519 config: Config{
2520 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2521 },
2522 renegotiateCiphers: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2523 })
2524 testCases = append(testCases, testCase{
2525 name: "Renegotiate-Client-SwitchCiphers2",
2526 renegotiate: true,
2527 config: Config{
2528 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2529 },
2530 renegotiateCiphers: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2531 })
David Benjaminc44b1df2014-11-23 12:11:01 -05002532 testCases = append(testCases, testCase{
2533 name: "Renegotiate-SameClientVersion",
2534 renegotiate: true,
2535 config: Config{
2536 MaxVersion: VersionTLS10,
2537 Bugs: ProtocolBugs{
2538 RequireSameRenegoClientVersion: true,
2539 },
2540 },
2541 })
Adam Langley2ae77d22014-10-28 17:29:33 -07002542}
2543
David Benjamin5e961c12014-11-07 01:48:35 -05002544func addDTLSReplayTests() {
2545 // Test that sequence number replays are detected.
2546 testCases = append(testCases, testCase{
2547 protocol: dtls,
2548 name: "DTLS-Replay",
2549 replayWrites: true,
2550 })
2551
2552 // Test the outgoing sequence number skipping by values larger
2553 // than the retransmit window.
2554 testCases = append(testCases, testCase{
2555 protocol: dtls,
2556 name: "DTLS-Replay-LargeGaps",
2557 config: Config{
2558 Bugs: ProtocolBugs{
2559 SequenceNumberIncrement: 127,
2560 },
2561 },
2562 replayWrites: true,
2563 })
2564}
2565
Feng Lu41aa3252014-11-21 22:47:56 -08002566func addFastRadioPaddingTests() {
David Benjamin1e29a6b2014-12-10 02:27:24 -05002567 testCases = append(testCases, testCase{
2568 protocol: tls,
2569 name: "FastRadio-Padding",
Feng Lu41aa3252014-11-21 22:47:56 -08002570 config: Config{
2571 Bugs: ProtocolBugs{
2572 RequireFastradioPadding: true,
2573 },
2574 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05002575 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08002576 })
David Benjamin1e29a6b2014-12-10 02:27:24 -05002577 testCases = append(testCases, testCase{
2578 protocol: dtls,
2579 name: "FastRadio-Padding",
Feng Lu41aa3252014-11-21 22:47:56 -08002580 config: Config{
2581 Bugs: ProtocolBugs{
2582 RequireFastradioPadding: true,
2583 },
2584 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05002585 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08002586 })
2587}
2588
David Benjamin000800a2014-11-14 01:43:59 -05002589var testHashes = []struct {
2590 name string
2591 id uint8
2592}{
2593 {"SHA1", hashSHA1},
2594 {"SHA224", hashSHA224},
2595 {"SHA256", hashSHA256},
2596 {"SHA384", hashSHA384},
2597 {"SHA512", hashSHA512},
2598}
2599
2600func addSigningHashTests() {
2601 // Make sure each hash works. Include some fake hashes in the list and
2602 // ensure they're ignored.
2603 for _, hash := range testHashes {
2604 testCases = append(testCases, testCase{
2605 name: "SigningHash-ClientAuth-" + hash.name,
2606 config: Config{
2607 ClientAuth: RequireAnyClientCert,
2608 SignatureAndHashes: []signatureAndHash{
2609 {signatureRSA, 42},
2610 {signatureRSA, hash.id},
2611 {signatureRSA, 255},
2612 },
2613 },
2614 flags: []string{
2615 "-cert-file", rsaCertificateFile,
2616 "-key-file", rsaKeyFile,
2617 },
2618 })
2619
2620 testCases = append(testCases, testCase{
2621 testType: serverTest,
2622 name: "SigningHash-ServerKeyExchange-Sign-" + hash.name,
2623 config: Config{
2624 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2625 SignatureAndHashes: []signatureAndHash{
2626 {signatureRSA, 42},
2627 {signatureRSA, hash.id},
2628 {signatureRSA, 255},
2629 },
2630 },
2631 })
2632 }
2633
2634 // Test that hash resolution takes the signature type into account.
2635 testCases = append(testCases, testCase{
2636 name: "SigningHash-ClientAuth-SignatureType",
2637 config: Config{
2638 ClientAuth: RequireAnyClientCert,
2639 SignatureAndHashes: []signatureAndHash{
2640 {signatureECDSA, hashSHA512},
2641 {signatureRSA, hashSHA384},
2642 {signatureECDSA, hashSHA1},
2643 },
2644 },
2645 flags: []string{
2646 "-cert-file", rsaCertificateFile,
2647 "-key-file", rsaKeyFile,
2648 },
2649 })
2650
2651 testCases = append(testCases, testCase{
2652 testType: serverTest,
2653 name: "SigningHash-ServerKeyExchange-SignatureType",
2654 config: Config{
2655 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2656 SignatureAndHashes: []signatureAndHash{
2657 {signatureECDSA, hashSHA512},
2658 {signatureRSA, hashSHA384},
2659 {signatureECDSA, hashSHA1},
2660 },
2661 },
2662 })
2663
2664 // Test that, if the list is missing, the peer falls back to SHA-1.
2665 testCases = append(testCases, testCase{
2666 name: "SigningHash-ClientAuth-Fallback",
2667 config: Config{
2668 ClientAuth: RequireAnyClientCert,
2669 SignatureAndHashes: []signatureAndHash{
2670 {signatureRSA, hashSHA1},
2671 },
2672 Bugs: ProtocolBugs{
2673 NoSignatureAndHashes: true,
2674 },
2675 },
2676 flags: []string{
2677 "-cert-file", rsaCertificateFile,
2678 "-key-file", rsaKeyFile,
2679 },
2680 })
2681
2682 testCases = append(testCases, testCase{
2683 testType: serverTest,
2684 name: "SigningHash-ServerKeyExchange-Fallback",
2685 config: Config{
2686 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2687 SignatureAndHashes: []signatureAndHash{
2688 {signatureRSA, hashSHA1},
2689 },
2690 Bugs: ProtocolBugs{
2691 NoSignatureAndHashes: true,
2692 },
2693 },
2694 })
2695}
2696
David Benjamin83f90402015-01-27 01:09:43 -05002697// timeouts is the retransmit schedule for BoringSSL. It doubles and
2698// caps at 60 seconds. On the 13th timeout, it gives up.
2699var timeouts = []time.Duration{
2700 1 * time.Second,
2701 2 * time.Second,
2702 4 * time.Second,
2703 8 * time.Second,
2704 16 * time.Second,
2705 32 * time.Second,
2706 60 * time.Second,
2707 60 * time.Second,
2708 60 * time.Second,
2709 60 * time.Second,
2710 60 * time.Second,
2711 60 * time.Second,
2712 60 * time.Second,
2713}
2714
2715func addDTLSRetransmitTests() {
2716 // Test that this is indeed the timeout schedule. Stress all
2717 // four patterns of handshake.
2718 for i := 1; i < len(timeouts); i++ {
2719 number := strconv.Itoa(i)
2720 testCases = append(testCases, testCase{
2721 protocol: dtls,
2722 name: "DTLS-Retransmit-Client-" + number,
2723 config: Config{
2724 Bugs: ProtocolBugs{
2725 TimeoutSchedule: timeouts[:i],
2726 },
2727 },
2728 resumeSession: true,
2729 flags: []string{"-async"},
2730 })
2731 testCases = append(testCases, testCase{
2732 protocol: dtls,
2733 testType: serverTest,
2734 name: "DTLS-Retransmit-Server-" + number,
2735 config: Config{
2736 Bugs: ProtocolBugs{
2737 TimeoutSchedule: timeouts[:i],
2738 },
2739 },
2740 resumeSession: true,
2741 flags: []string{"-async"},
2742 })
2743 }
2744
2745 // Test that exceeding the timeout schedule hits a read
2746 // timeout.
2747 testCases = append(testCases, testCase{
2748 protocol: dtls,
2749 name: "DTLS-Retransmit-Timeout",
2750 config: Config{
2751 Bugs: ProtocolBugs{
2752 TimeoutSchedule: timeouts,
2753 },
2754 },
2755 resumeSession: true,
2756 flags: []string{"-async"},
2757 shouldFail: true,
2758 expectedError: ":READ_TIMEOUT_EXPIRED:",
2759 })
2760
2761 // Test that timeout handling has a fudge factor, due to API
2762 // problems.
2763 testCases = append(testCases, testCase{
2764 protocol: dtls,
2765 name: "DTLS-Retransmit-Fudge",
2766 config: Config{
2767 Bugs: ProtocolBugs{
2768 TimeoutSchedule: []time.Duration{
2769 timeouts[0] - 10*time.Millisecond,
2770 },
2771 },
2772 },
2773 resumeSession: true,
2774 flags: []string{"-async"},
2775 })
2776}
2777
David Benjamin884fdf12014-08-02 15:28:23 -04002778func worker(statusChan chan statusMsg, c chan *testCase, buildDir string, wg *sync.WaitGroup) {
Adam Langley95c29f32014-06-20 12:00:00 -07002779 defer wg.Done()
2780
2781 for test := range c {
Adam Langley69a01602014-11-17 17:26:55 -08002782 var err error
2783
2784 if *mallocTest < 0 {
2785 statusChan <- statusMsg{test: test, started: true}
2786 err = runTest(test, buildDir, -1)
2787 } else {
2788 for mallocNumToFail := int64(*mallocTest); ; mallocNumToFail++ {
2789 statusChan <- statusMsg{test: test, started: true}
2790 if err = runTest(test, buildDir, mallocNumToFail); err != errMoreMallocs {
2791 if err != nil {
2792 fmt.Printf("\n\nmalloc test failed at %d: %s\n", mallocNumToFail, err)
2793 }
2794 break
2795 }
2796 }
2797 }
Adam Langley95c29f32014-06-20 12:00:00 -07002798 statusChan <- statusMsg{test: test, err: err}
2799 }
2800}
2801
2802type statusMsg struct {
2803 test *testCase
2804 started bool
2805 err error
2806}
2807
2808func statusPrinter(doneChan chan struct{}, statusChan chan statusMsg, total int) {
2809 var started, done, failed, lineLen int
2810 defer close(doneChan)
2811
2812 for msg := range statusChan {
2813 if msg.started {
2814 started++
2815 } else {
2816 done++
2817 }
2818
2819 fmt.Printf("\x1b[%dD\x1b[K", lineLen)
2820
2821 if msg.err != nil {
2822 fmt.Printf("FAILED (%s)\n%s\n", msg.test.name, msg.err)
2823 failed++
2824 }
2825 line := fmt.Sprintf("%d/%d/%d/%d", failed, done, started, total)
2826 lineLen = len(line)
2827 os.Stdout.WriteString(line)
2828 }
2829}
2830
2831func main() {
2832 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 -04002833 var flagNumWorkers *int = flag.Int("num-workers", runtime.NumCPU(), "The number of workers to run in parallel.")
David Benjamin884fdf12014-08-02 15:28:23 -04002834 var flagBuildDir *string = flag.String("build-dir", "../../../build", "The build directory to run the shim from.")
Adam Langley95c29f32014-06-20 12:00:00 -07002835
2836 flag.Parse()
2837
2838 addCipherSuiteTests()
2839 addBadECDSASignatureTests()
Adam Langley80842bd2014-06-20 12:00:00 -07002840 addCBCPaddingTests()
Kenny Root7fdeaf12014-08-05 15:23:37 -07002841 addCBCSplittingTests()
David Benjamin636293b2014-07-08 17:59:18 -04002842 addClientAuthTests()
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002843 addVersionNegotiationTests()
David Benjaminaccb4542014-12-12 23:44:33 -05002844 addMinimumVersionTests()
David Benjamin5c24a1d2014-08-31 00:59:27 -04002845 addD5BugTests()
David Benjamine78bfde2014-09-06 12:45:15 -04002846 addExtensionTests()
David Benjamin01fe8202014-09-24 15:21:44 -04002847 addResumptionVersionTests()
Adam Langley75712922014-10-10 16:23:43 -07002848 addExtendedMasterSecretTests()
Adam Langley2ae77d22014-10-28 17:29:33 -07002849 addRenegotiationTests()
David Benjamin5e961c12014-11-07 01:48:35 -05002850 addDTLSReplayTests()
David Benjamin000800a2014-11-14 01:43:59 -05002851 addSigningHashTests()
Feng Lu41aa3252014-11-21 22:47:56 -08002852 addFastRadioPaddingTests()
David Benjamin83f90402015-01-27 01:09:43 -05002853 addDTLSRetransmitTests()
David Benjamin43ec06f2014-08-05 02:28:57 -04002854 for _, async := range []bool{false, true} {
2855 for _, splitHandshake := range []bool{false, true} {
David Benjamin6fd297b2014-08-11 18:43:38 -04002856 for _, protocol := range []protocol{tls, dtls} {
2857 addStateMachineCoverageTests(async, splitHandshake, protocol)
2858 }
David Benjamin43ec06f2014-08-05 02:28:57 -04002859 }
2860 }
Adam Langley95c29f32014-06-20 12:00:00 -07002861
2862 var wg sync.WaitGroup
2863
David Benjamin2bc8e6f2014-08-02 15:22:37 -04002864 numWorkers := *flagNumWorkers
Adam Langley95c29f32014-06-20 12:00:00 -07002865
2866 statusChan := make(chan statusMsg, numWorkers)
2867 testChan := make(chan *testCase, numWorkers)
2868 doneChan := make(chan struct{})
2869
David Benjamin025b3d32014-07-01 19:53:04 -04002870 go statusPrinter(doneChan, statusChan, len(testCases))
Adam Langley95c29f32014-06-20 12:00:00 -07002871
2872 for i := 0; i < numWorkers; i++ {
2873 wg.Add(1)
David Benjamin884fdf12014-08-02 15:28:23 -04002874 go worker(statusChan, testChan, *flagBuildDir, &wg)
Adam Langley95c29f32014-06-20 12:00:00 -07002875 }
2876
David Benjamin025b3d32014-07-01 19:53:04 -04002877 for i := range testCases {
2878 if len(*flagTest) == 0 || *flagTest == testCases[i].name {
2879 testChan <- &testCases[i]
Adam Langley95c29f32014-06-20 12:00:00 -07002880 }
2881 }
2882
2883 close(testChan)
2884 wg.Wait()
2885 close(statusChan)
2886 <-doneChan
2887
2888 fmt.Printf("\n")
2889}