blob: 19369af0752819e4d6052164652bf6b6b23d4526 [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 },
Adam Langley95c29f32014-06-20 12:00:00 -0700762}
763
David Benjamin01fe8202014-09-24 15:21:44 -0400764func doExchange(test *testCase, config *Config, conn net.Conn, messageLen int, isResume bool) error {
David Benjamin65ea8ff2014-11-23 03:01:00 -0500765 var connDebug *recordingConn
David Benjamin5fa3eba2015-01-22 16:35:40 -0500766 var connDamage *damageAdaptor
David Benjamin65ea8ff2014-11-23 03:01:00 -0500767 if *flagDebug {
768 connDebug = &recordingConn{Conn: conn}
769 conn = connDebug
770 defer func() {
771 connDebug.WriteTo(os.Stdout)
772 }()
773 }
774
David Benjamin6fd297b2014-08-11 18:43:38 -0400775 if test.protocol == dtls {
David Benjamin83f90402015-01-27 01:09:43 -0500776 config.Bugs.PacketAdaptor = newPacketAdaptor(conn)
777 conn = config.Bugs.PacketAdaptor
David Benjamin5e961c12014-11-07 01:48:35 -0500778 if test.replayWrites {
779 conn = newReplayAdaptor(conn)
780 }
David Benjamin6fd297b2014-08-11 18:43:38 -0400781 }
782
David Benjamin5fa3eba2015-01-22 16:35:40 -0500783 if test.damageFirstWrite {
784 connDamage = newDamageAdaptor(conn)
785 conn = connDamage
786 }
787
David Benjamin6fd297b2014-08-11 18:43:38 -0400788 if test.sendPrefix != "" {
789 if _, err := conn.Write([]byte(test.sendPrefix)); err != nil {
790 return err
791 }
David Benjamin98e882e2014-08-08 13:24:34 -0400792 }
793
David Benjamin1d5c83e2014-07-22 19:20:02 -0400794 var tlsConn *Conn
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400795 if test.testType == clientTest {
David Benjamin6fd297b2014-08-11 18:43:38 -0400796 if test.protocol == dtls {
797 tlsConn = DTLSServer(conn, config)
798 } else {
799 tlsConn = Server(conn, config)
800 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400801 } else {
802 config.InsecureSkipVerify = true
David Benjamin6fd297b2014-08-11 18:43:38 -0400803 if test.protocol == dtls {
804 tlsConn = DTLSClient(conn, config)
805 } else {
806 tlsConn = Client(conn, config)
807 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400808 }
809
Adam Langley95c29f32014-06-20 12:00:00 -0700810 if err := tlsConn.Handshake(); err != nil {
811 return err
812 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700813
David Benjamin01fe8202014-09-24 15:21:44 -0400814 // TODO(davidben): move all per-connection expectations into a dedicated
815 // expectations struct that can be specified separately for the two
816 // legs.
817 expectedVersion := test.expectedVersion
818 if isResume && test.expectedResumeVersion != 0 {
819 expectedVersion = test.expectedResumeVersion
820 }
821 if vers := tlsConn.ConnectionState().Version; expectedVersion != 0 && vers != expectedVersion {
822 return fmt.Errorf("got version %x, expected %x", vers, expectedVersion)
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400823 }
824
David Benjamina08e49d2014-08-24 01:46:07 -0400825 if test.expectChannelID {
826 channelID := tlsConn.ConnectionState().ChannelID
827 if channelID == nil {
828 return fmt.Errorf("no channel ID negotiated")
829 }
830 if channelID.Curve != channelIDKey.Curve ||
831 channelIDKey.X.Cmp(channelIDKey.X) != 0 ||
832 channelIDKey.Y.Cmp(channelIDKey.Y) != 0 {
833 return fmt.Errorf("incorrect channel ID")
834 }
835 }
836
David Benjaminae2888f2014-09-06 12:58:58 -0400837 if expected := test.expectedNextProto; expected != "" {
838 if actual := tlsConn.ConnectionState().NegotiatedProtocol; actual != expected {
839 return fmt.Errorf("next proto mismatch: got %s, wanted %s", actual, expected)
840 }
841 }
842
David Benjaminfc7b0862014-09-06 13:21:53 -0400843 if test.expectedNextProtoType != 0 {
844 if (test.expectedNextProtoType == alpn) != tlsConn.ConnectionState().NegotiatedProtocolFromALPN {
845 return fmt.Errorf("next proto type mismatch")
846 }
847 }
848
David Benjaminca6c8262014-11-15 19:06:08 -0500849 if p := tlsConn.ConnectionState().SRTPProtectionProfile; p != test.expectedSRTPProtectionProfile {
850 return fmt.Errorf("SRTP profile mismatch: got %d, wanted %d", p, test.expectedSRTPProtectionProfile)
851 }
852
David Benjamine58c4f52014-08-24 03:47:07 -0400853 if test.shimWritesFirst {
854 var buf [5]byte
855 _, err := io.ReadFull(tlsConn, buf[:])
856 if err != nil {
857 return err
858 }
859 if string(buf[:]) != "hello" {
860 return fmt.Errorf("bad initial message")
861 }
862 }
863
Adam Langleycf2d4f42014-10-28 19:06:14 -0700864 if test.renegotiate {
865 if test.renegotiateCiphers != nil {
866 config.CipherSuites = test.renegotiateCiphers
867 }
868 if err := tlsConn.Renegotiate(); err != nil {
869 return err
870 }
871 } else if test.renegotiateCiphers != nil {
872 panic("renegotiateCiphers without renegotiate")
873 }
874
David Benjamin5fa3eba2015-01-22 16:35:40 -0500875 if test.damageFirstWrite {
876 connDamage.setDamage(true)
877 tlsConn.Write([]byte("DAMAGED WRITE"))
878 connDamage.setDamage(false)
879 }
880
Kenny Root7fdeaf12014-08-05 15:23:37 -0700881 if messageLen < 0 {
David Benjamin6fd297b2014-08-11 18:43:38 -0400882 if test.protocol == dtls {
883 return fmt.Errorf("messageLen < 0 not supported for DTLS tests")
884 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700885 // Read until EOF.
886 _, err := io.Copy(ioutil.Discard, tlsConn)
887 return err
888 }
889
David Benjamin4189bd92015-01-25 23:52:39 -0500890 var testMessage []byte
891 if config.Bugs.AppDataAfterChangeCipherSpec != nil {
892 // We've already sent a message. Expect the shim to echo it
893 // back.
894 testMessage = config.Bugs.AppDataAfterChangeCipherSpec
895 } else {
896 if messageLen == 0 {
897 messageLen = 32
898 }
899 testMessage = make([]byte, messageLen)
900 for i := range testMessage {
901 testMessage[i] = 0x42
902 }
903 tlsConn.Write(testMessage)
Adam Langley80842bd2014-06-20 12:00:00 -0700904 }
Adam Langley95c29f32014-06-20 12:00:00 -0700905
906 buf := make([]byte, len(testMessage))
David Benjamin6fd297b2014-08-11 18:43:38 -0400907 if test.protocol == dtls {
908 bufTmp := make([]byte, len(buf)+1)
909 n, err := tlsConn.Read(bufTmp)
910 if err != nil {
911 return err
912 }
913 if n != len(buf) {
914 return fmt.Errorf("bad reply; length mismatch (%d vs %d)", n, len(buf))
915 }
916 copy(buf, bufTmp)
917 } else {
918 _, err := io.ReadFull(tlsConn, buf)
919 if err != nil {
920 return err
921 }
Adam Langley95c29f32014-06-20 12:00:00 -0700922 }
923
924 for i, v := range buf {
925 if v != testMessage[i]^0xff {
926 return fmt.Errorf("bad reply contents at byte %d", i)
927 }
928 }
929
930 return nil
931}
932
David Benjamin325b5c32014-07-01 19:40:31 -0400933func valgrindOf(dbAttach bool, path string, args ...string) *exec.Cmd {
934 valgrindArgs := []string{"--error-exitcode=99", "--track-origins=yes", "--leak-check=full"}
Adam Langley95c29f32014-06-20 12:00:00 -0700935 if dbAttach {
David Benjamin325b5c32014-07-01 19:40:31 -0400936 valgrindArgs = append(valgrindArgs, "--db-attach=yes", "--db-command=xterm -e gdb -nw %f %p")
Adam Langley95c29f32014-06-20 12:00:00 -0700937 }
David Benjamin325b5c32014-07-01 19:40:31 -0400938 valgrindArgs = append(valgrindArgs, path)
939 valgrindArgs = append(valgrindArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -0700940
David Benjamin325b5c32014-07-01 19:40:31 -0400941 return exec.Command("valgrind", valgrindArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -0700942}
943
David Benjamin325b5c32014-07-01 19:40:31 -0400944func gdbOf(path string, args ...string) *exec.Cmd {
945 xtermArgs := []string{"-e", "gdb", "--args"}
946 xtermArgs = append(xtermArgs, path)
947 xtermArgs = append(xtermArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -0700948
David Benjamin325b5c32014-07-01 19:40:31 -0400949 return exec.Command("xterm", xtermArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -0700950}
951
David Benjamin1d5c83e2014-07-22 19:20:02 -0400952func openSocketPair() (shimEnd *os.File, conn net.Conn) {
Adam Langley95c29f32014-06-20 12:00:00 -0700953 socks, err := syscall.Socketpair(syscall.AF_UNIX, syscall.SOCK_STREAM, 0)
954 if err != nil {
955 panic(err)
956 }
957
958 syscall.CloseOnExec(socks[0])
959 syscall.CloseOnExec(socks[1])
David Benjamin1d5c83e2014-07-22 19:20:02 -0400960 shimEnd = os.NewFile(uintptr(socks[0]), "shim end")
Adam Langley95c29f32014-06-20 12:00:00 -0700961 connFile := os.NewFile(uintptr(socks[1]), "our end")
David Benjamin1d5c83e2014-07-22 19:20:02 -0400962 conn, err = net.FileConn(connFile)
963 if err != nil {
964 panic(err)
965 }
Adam Langley95c29f32014-06-20 12:00:00 -0700966 connFile.Close()
967 if err != nil {
968 panic(err)
969 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400970 return shimEnd, conn
971}
972
Adam Langley69a01602014-11-17 17:26:55 -0800973type moreMallocsError struct{}
974
975func (moreMallocsError) Error() string {
976 return "child process did not exhaust all allocation calls"
977}
978
979var errMoreMallocs = moreMallocsError{}
980
981func runTest(test *testCase, buildDir string, mallocNumToFail int64) error {
Adam Langley38311732014-10-16 19:04:35 -0700982 if !test.shouldFail && (len(test.expectedError) > 0 || len(test.expectedLocalError) > 0) {
983 panic("Error expected without shouldFail in " + test.name)
984 }
985
David Benjamin1d5c83e2014-07-22 19:20:02 -0400986 shimEnd, conn := openSocketPair()
987 shimEndResume, connResume := openSocketPair()
Adam Langley95c29f32014-06-20 12:00:00 -0700988
David Benjamin884fdf12014-08-02 15:28:23 -0400989 shim_path := path.Join(buildDir, "ssl/test/bssl_shim")
David Benjamin5a593af2014-08-11 19:51:50 -0400990 var flags []string
David Benjamin1d5c83e2014-07-22 19:20:02 -0400991 if test.testType == serverTest {
David Benjamin5a593af2014-08-11 19:51:50 -0400992 flags = append(flags, "-server")
993
David Benjamin025b3d32014-07-01 19:53:04 -0400994 flags = append(flags, "-key-file")
995 if test.keyFile == "" {
996 flags = append(flags, rsaKeyFile)
997 } else {
998 flags = append(flags, test.keyFile)
999 }
1000
1001 flags = append(flags, "-cert-file")
1002 if test.certFile == "" {
1003 flags = append(flags, rsaCertificateFile)
1004 } else {
1005 flags = append(flags, test.certFile)
1006 }
1007 }
David Benjamin5a593af2014-08-11 19:51:50 -04001008
David Benjamin6fd297b2014-08-11 18:43:38 -04001009 if test.protocol == dtls {
1010 flags = append(flags, "-dtls")
1011 }
1012
David Benjamin5a593af2014-08-11 19:51:50 -04001013 if test.resumeSession {
1014 flags = append(flags, "-resume")
1015 }
1016
David Benjamine58c4f52014-08-24 03:47:07 -04001017 if test.shimWritesFirst {
1018 flags = append(flags, "-shim-writes-first")
1019 }
1020
David Benjamin025b3d32014-07-01 19:53:04 -04001021 flags = append(flags, test.flags...)
1022
1023 var shim *exec.Cmd
1024 if *useValgrind {
1025 shim = valgrindOf(false, shim_path, flags...)
Adam Langley75712922014-10-10 16:23:43 -07001026 } else if *useGDB {
1027 shim = gdbOf(shim_path, flags...)
David Benjamin025b3d32014-07-01 19:53:04 -04001028 } else {
1029 shim = exec.Command(shim_path, flags...)
1030 }
David Benjamin1d5c83e2014-07-22 19:20:02 -04001031 shim.ExtraFiles = []*os.File{shimEnd, shimEndResume}
David Benjamin025b3d32014-07-01 19:53:04 -04001032 shim.Stdin = os.Stdin
1033 var stdoutBuf, stderrBuf bytes.Buffer
1034 shim.Stdout = &stdoutBuf
1035 shim.Stderr = &stderrBuf
Adam Langley69a01602014-11-17 17:26:55 -08001036 if mallocNumToFail >= 0 {
1037 shim.Env = []string{"MALLOC_NUMBER_TO_FAIL=" + strconv.FormatInt(mallocNumToFail, 10)}
1038 if *mallocTestDebug {
1039 shim.Env = append(shim.Env, "MALLOC_ABORT_ON_FAIL=1")
1040 }
1041 shim.Env = append(shim.Env, "_MALLOC_CHECK=1")
1042 }
David Benjamin025b3d32014-07-01 19:53:04 -04001043
1044 if err := shim.Start(); err != nil {
Adam Langley95c29f32014-06-20 12:00:00 -07001045 panic(err)
1046 }
David Benjamin025b3d32014-07-01 19:53:04 -04001047 shimEnd.Close()
David Benjamin1d5c83e2014-07-22 19:20:02 -04001048 shimEndResume.Close()
Adam Langley95c29f32014-06-20 12:00:00 -07001049
1050 config := test.config
David Benjamin1d5c83e2014-07-22 19:20:02 -04001051 config.ClientSessionCache = NewLRUClientSessionCache(1)
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001052 config.ServerSessionCache = NewLRUServerSessionCache(1)
David Benjamin025b3d32014-07-01 19:53:04 -04001053 if test.testType == clientTest {
1054 if len(config.Certificates) == 0 {
1055 config.Certificates = []Certificate{getRSACertificate()}
1056 }
David Benjamin025b3d32014-07-01 19:53:04 -04001057 }
Adam Langley95c29f32014-06-20 12:00:00 -07001058
David Benjamin01fe8202014-09-24 15:21:44 -04001059 err := doExchange(test, &config, conn, test.messageLen,
1060 false /* not a resumption */)
Adam Langley95c29f32014-06-20 12:00:00 -07001061 conn.Close()
David Benjamin65ea8ff2014-11-23 03:01:00 -05001062
David Benjamin1d5c83e2014-07-22 19:20:02 -04001063 if err == nil && test.resumeSession {
David Benjamin01fe8202014-09-24 15:21:44 -04001064 var resumeConfig Config
1065 if test.resumeConfig != nil {
1066 resumeConfig = *test.resumeConfig
1067 if len(resumeConfig.Certificates) == 0 {
1068 resumeConfig.Certificates = []Certificate{getRSACertificate()}
1069 }
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001070 if !test.newSessionsOnResume {
1071 resumeConfig.SessionTicketKey = config.SessionTicketKey
1072 resumeConfig.ClientSessionCache = config.ClientSessionCache
1073 resumeConfig.ServerSessionCache = config.ServerSessionCache
1074 }
David Benjamin01fe8202014-09-24 15:21:44 -04001075 } else {
1076 resumeConfig = config
1077 }
1078 err = doExchange(test, &resumeConfig, connResume, test.messageLen,
1079 true /* resumption */)
David Benjamin1d5c83e2014-07-22 19:20:02 -04001080 }
David Benjamin812152a2014-09-06 12:49:07 -04001081 connResume.Close()
David Benjamin1d5c83e2014-07-22 19:20:02 -04001082
David Benjamin025b3d32014-07-01 19:53:04 -04001083 childErr := shim.Wait()
Adam Langley69a01602014-11-17 17:26:55 -08001084 if exitError, ok := childErr.(*exec.ExitError); ok {
1085 if exitError.Sys().(syscall.WaitStatus).ExitStatus() == 88 {
1086 return errMoreMallocs
1087 }
1088 }
Adam Langley95c29f32014-06-20 12:00:00 -07001089
1090 stdout := string(stdoutBuf.Bytes())
1091 stderr := string(stderrBuf.Bytes())
1092 failed := err != nil || childErr != nil
1093 correctFailure := len(test.expectedError) == 0 || strings.Contains(stdout, test.expectedError)
Adam Langleyac61fa32014-06-23 12:03:11 -07001094 localError := "none"
1095 if err != nil {
1096 localError = err.Error()
1097 }
1098 if len(test.expectedLocalError) != 0 {
1099 correctFailure = correctFailure && strings.Contains(localError, test.expectedLocalError)
1100 }
Adam Langley95c29f32014-06-20 12:00:00 -07001101
1102 if failed != test.shouldFail || failed && !correctFailure {
Adam Langley95c29f32014-06-20 12:00:00 -07001103 childError := "none"
Adam Langley95c29f32014-06-20 12:00:00 -07001104 if childErr != nil {
1105 childError = childErr.Error()
1106 }
1107
1108 var msg string
1109 switch {
1110 case failed && !test.shouldFail:
1111 msg = "unexpected failure"
1112 case !failed && test.shouldFail:
1113 msg = "unexpected success"
1114 case failed && !correctFailure:
Adam Langleyac61fa32014-06-23 12:03:11 -07001115 msg = "bad error (wanted '" + test.expectedError + "' / '" + test.expectedLocalError + "')"
Adam Langley95c29f32014-06-20 12:00:00 -07001116 default:
1117 panic("internal error")
1118 }
1119
1120 return fmt.Errorf("%s: local error '%s', child error '%s', stdout:\n%s\nstderr:\n%s", msg, localError, childError, string(stdoutBuf.Bytes()), stderr)
1121 }
1122
1123 if !*useValgrind && len(stderr) > 0 {
1124 println(stderr)
1125 }
1126
1127 return nil
1128}
1129
1130var tlsVersions = []struct {
1131 name string
1132 version uint16
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001133 flag string
David Benjamin8b8c0062014-11-23 02:47:52 -05001134 hasDTLS bool
Adam Langley95c29f32014-06-20 12:00:00 -07001135}{
David Benjamin8b8c0062014-11-23 02:47:52 -05001136 {"SSL3", VersionSSL30, "-no-ssl3", false},
1137 {"TLS1", VersionTLS10, "-no-tls1", true},
1138 {"TLS11", VersionTLS11, "-no-tls11", false},
1139 {"TLS12", VersionTLS12, "-no-tls12", true},
Adam Langley95c29f32014-06-20 12:00:00 -07001140}
1141
1142var testCipherSuites = []struct {
1143 name string
1144 id uint16
1145}{
1146 {"3DES-SHA", TLS_RSA_WITH_3DES_EDE_CBC_SHA},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001147 {"AES128-GCM", TLS_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001148 {"AES128-SHA", TLS_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001149 {"AES128-SHA256", TLS_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001150 {"AES256-GCM", TLS_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001151 {"AES256-SHA", TLS_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001152 {"AES256-SHA256", TLS_RSA_WITH_AES_256_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001153 {"DHE-RSA-AES128-GCM", TLS_DHE_RSA_WITH_AES_128_GCM_SHA256},
1154 {"DHE-RSA-AES128-SHA", TLS_DHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001155 {"DHE-RSA-AES128-SHA256", TLS_DHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001156 {"DHE-RSA-AES256-GCM", TLS_DHE_RSA_WITH_AES_256_GCM_SHA384},
1157 {"DHE-RSA-AES256-SHA", TLS_DHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001158 {"DHE-RSA-AES256-SHA256", TLS_DHE_RSA_WITH_AES_256_CBC_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001159 {"ECDHE-ECDSA-AES128-GCM", TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
1160 {"ECDHE-ECDSA-AES128-SHA", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001161 {"ECDHE-ECDSA-AES128-SHA256", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256},
1162 {"ECDHE-ECDSA-AES256-GCM", TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001163 {"ECDHE-ECDSA-AES256-SHA", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001164 {"ECDHE-ECDSA-AES256-SHA384", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001165 {"ECDHE-ECDSA-RC4-SHA", TLS_ECDHE_ECDSA_WITH_RC4_128_SHA},
David Benjamin2af684f2014-10-27 02:23:15 -04001166 {"ECDHE-PSK-WITH-AES-128-GCM-SHA256", TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001167 {"ECDHE-RSA-AES128-GCM", TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001168 {"ECDHE-RSA-AES128-SHA", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001169 {"ECDHE-RSA-AES128-SHA256", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001170 {"ECDHE-RSA-AES256-GCM", TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001171 {"ECDHE-RSA-AES256-SHA", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001172 {"ECDHE-RSA-AES256-SHA384", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001173 {"ECDHE-RSA-RC4-SHA", TLS_ECDHE_RSA_WITH_RC4_128_SHA},
David Benjamin48cae082014-10-27 01:06:24 -04001174 {"PSK-AES128-CBC-SHA", TLS_PSK_WITH_AES_128_CBC_SHA},
1175 {"PSK-AES256-CBC-SHA", TLS_PSK_WITH_AES_256_CBC_SHA},
1176 {"PSK-RC4-SHA", TLS_PSK_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -07001177 {"RC4-MD5", TLS_RSA_WITH_RC4_128_MD5},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001178 {"RC4-SHA", TLS_RSA_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -07001179}
1180
David Benjamin8b8c0062014-11-23 02:47:52 -05001181func hasComponent(suiteName, component string) bool {
1182 return strings.Contains("-"+suiteName+"-", "-"+component+"-")
1183}
1184
David Benjaminf7768e42014-08-31 02:06:47 -04001185func isTLS12Only(suiteName string) bool {
David Benjamin8b8c0062014-11-23 02:47:52 -05001186 return hasComponent(suiteName, "GCM") ||
1187 hasComponent(suiteName, "SHA256") ||
1188 hasComponent(suiteName, "SHA384")
1189}
1190
1191func isDTLSCipher(suiteName string) bool {
David Benjamine95d20d2014-12-23 11:16:01 -05001192 return !hasComponent(suiteName, "RC4")
David Benjaminf7768e42014-08-31 02:06:47 -04001193}
1194
Adam Langley95c29f32014-06-20 12:00:00 -07001195func addCipherSuiteTests() {
1196 for _, suite := range testCipherSuites {
David Benjamin48cae082014-10-27 01:06:24 -04001197 const psk = "12345"
1198 const pskIdentity = "luggage combo"
1199
Adam Langley95c29f32014-06-20 12:00:00 -07001200 var cert Certificate
David Benjamin025b3d32014-07-01 19:53:04 -04001201 var certFile string
1202 var keyFile string
David Benjamin8b8c0062014-11-23 02:47:52 -05001203 if hasComponent(suite.name, "ECDSA") {
Adam Langley95c29f32014-06-20 12:00:00 -07001204 cert = getECDSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -04001205 certFile = ecdsaCertificateFile
1206 keyFile = ecdsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -07001207 } else {
1208 cert = getRSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -04001209 certFile = rsaCertificateFile
1210 keyFile = rsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -07001211 }
1212
David Benjamin48cae082014-10-27 01:06:24 -04001213 var flags []string
David Benjamin8b8c0062014-11-23 02:47:52 -05001214 if hasComponent(suite.name, "PSK") {
David Benjamin48cae082014-10-27 01:06:24 -04001215 flags = append(flags,
1216 "-psk", psk,
1217 "-psk-identity", pskIdentity)
1218 }
1219
Adam Langley95c29f32014-06-20 12:00:00 -07001220 for _, ver := range tlsVersions {
David Benjaminf7768e42014-08-31 02:06:47 -04001221 if ver.version < VersionTLS12 && isTLS12Only(suite.name) {
Adam Langley95c29f32014-06-20 12:00:00 -07001222 continue
1223 }
1224
David Benjamin025b3d32014-07-01 19:53:04 -04001225 testCases = append(testCases, testCase{
1226 testType: clientTest,
1227 name: ver.name + "-" + suite.name + "-client",
Adam Langley95c29f32014-06-20 12:00:00 -07001228 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001229 MinVersion: ver.version,
1230 MaxVersion: ver.version,
1231 CipherSuites: []uint16{suite.id},
1232 Certificates: []Certificate{cert},
1233 PreSharedKey: []byte(psk),
1234 PreSharedKeyIdentity: pskIdentity,
Adam Langley95c29f32014-06-20 12:00:00 -07001235 },
David Benjamin48cae082014-10-27 01:06:24 -04001236 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001237 resumeSession: true,
Adam Langley95c29f32014-06-20 12:00:00 -07001238 })
David Benjamin025b3d32014-07-01 19:53:04 -04001239
David Benjamin76d8abe2014-08-14 16:25:34 -04001240 testCases = append(testCases, testCase{
1241 testType: serverTest,
1242 name: ver.name + "-" + suite.name + "-server",
1243 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001244 MinVersion: ver.version,
1245 MaxVersion: ver.version,
1246 CipherSuites: []uint16{suite.id},
1247 Certificates: []Certificate{cert},
1248 PreSharedKey: []byte(psk),
1249 PreSharedKeyIdentity: pskIdentity,
David Benjamin76d8abe2014-08-14 16:25:34 -04001250 },
1251 certFile: certFile,
1252 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001253 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001254 resumeSession: true,
David Benjamin76d8abe2014-08-14 16:25:34 -04001255 })
David Benjamin6fd297b2014-08-11 18:43:38 -04001256
David Benjamin8b8c0062014-11-23 02:47:52 -05001257 if ver.hasDTLS && isDTLSCipher(suite.name) {
David Benjamin6fd297b2014-08-11 18:43:38 -04001258 testCases = append(testCases, testCase{
1259 testType: clientTest,
1260 protocol: dtls,
1261 name: "D" + ver.name + "-" + suite.name + "-client",
1262 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001263 MinVersion: ver.version,
1264 MaxVersion: ver.version,
1265 CipherSuites: []uint16{suite.id},
1266 Certificates: []Certificate{cert},
1267 PreSharedKey: []byte(psk),
1268 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001269 },
David Benjamin48cae082014-10-27 01:06:24 -04001270 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001271 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001272 })
1273 testCases = append(testCases, testCase{
1274 testType: serverTest,
1275 protocol: dtls,
1276 name: "D" + ver.name + "-" + suite.name + "-server",
1277 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001278 MinVersion: ver.version,
1279 MaxVersion: ver.version,
1280 CipherSuites: []uint16{suite.id},
1281 Certificates: []Certificate{cert},
1282 PreSharedKey: []byte(psk),
1283 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001284 },
1285 certFile: certFile,
1286 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001287 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001288 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001289 })
1290 }
Adam Langley95c29f32014-06-20 12:00:00 -07001291 }
1292 }
1293}
1294
1295func addBadECDSASignatureTests() {
1296 for badR := BadValue(1); badR < NumBadValues; badR++ {
1297 for badS := BadValue(1); badS < NumBadValues; badS++ {
David Benjamin025b3d32014-07-01 19:53:04 -04001298 testCases = append(testCases, testCase{
Adam Langley95c29f32014-06-20 12:00:00 -07001299 name: fmt.Sprintf("BadECDSA-%d-%d", badR, badS),
1300 config: Config{
1301 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
1302 Certificates: []Certificate{getECDSACertificate()},
1303 Bugs: ProtocolBugs{
1304 BadECDSAR: badR,
1305 BadECDSAS: badS,
1306 },
1307 },
1308 shouldFail: true,
1309 expectedError: "SIGNATURE",
1310 })
1311 }
1312 }
1313}
1314
Adam Langley80842bd2014-06-20 12:00:00 -07001315func addCBCPaddingTests() {
David Benjamin025b3d32014-07-01 19:53:04 -04001316 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001317 name: "MaxCBCPadding",
1318 config: Config{
1319 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1320 Bugs: ProtocolBugs{
1321 MaxPadding: true,
1322 },
1323 },
1324 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1325 })
David Benjamin025b3d32014-07-01 19:53:04 -04001326 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001327 name: "BadCBCPadding",
1328 config: Config{
1329 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1330 Bugs: ProtocolBugs{
1331 PaddingFirstByteBad: true,
1332 },
1333 },
1334 shouldFail: true,
1335 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1336 })
1337 // OpenSSL previously had an issue where the first byte of padding in
1338 // 255 bytes of padding wasn't checked.
David Benjamin025b3d32014-07-01 19:53:04 -04001339 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001340 name: "BadCBCPadding255",
1341 config: Config{
1342 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1343 Bugs: ProtocolBugs{
1344 MaxPadding: true,
1345 PaddingFirstByteBadIf255: true,
1346 },
1347 },
1348 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1349 shouldFail: true,
1350 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1351 })
1352}
1353
Kenny Root7fdeaf12014-08-05 15:23:37 -07001354func addCBCSplittingTests() {
1355 testCases = append(testCases, testCase{
1356 name: "CBCRecordSplitting",
1357 config: Config{
1358 MaxVersion: VersionTLS10,
1359 MinVersion: VersionTLS10,
1360 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1361 },
1362 messageLen: -1, // read until EOF
1363 flags: []string{
1364 "-async",
1365 "-write-different-record-sizes",
1366 "-cbc-record-splitting",
1367 },
David Benjamina8e3e0e2014-08-06 22:11:10 -04001368 })
1369 testCases = append(testCases, testCase{
Kenny Root7fdeaf12014-08-05 15:23:37 -07001370 name: "CBCRecordSplittingPartialWrite",
1371 config: Config{
1372 MaxVersion: VersionTLS10,
1373 MinVersion: VersionTLS10,
1374 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1375 },
1376 messageLen: -1, // read until EOF
1377 flags: []string{
1378 "-async",
1379 "-write-different-record-sizes",
1380 "-cbc-record-splitting",
1381 "-partial-write",
1382 },
1383 })
1384}
1385
David Benjamin636293b2014-07-08 17:59:18 -04001386func addClientAuthTests() {
David Benjamin407a10c2014-07-16 12:58:59 -04001387 // Add a dummy cert pool to stress certificate authority parsing.
1388 // TODO(davidben): Add tests that those values parse out correctly.
1389 certPool := x509.NewCertPool()
1390 cert, err := x509.ParseCertificate(rsaCertificate.Certificate[0])
1391 if err != nil {
1392 panic(err)
1393 }
1394 certPool.AddCert(cert)
1395
David Benjamin636293b2014-07-08 17:59:18 -04001396 for _, ver := range tlsVersions {
David Benjamin636293b2014-07-08 17:59:18 -04001397 testCases = append(testCases, testCase{
1398 testType: clientTest,
David Benjamin67666e72014-07-12 15:47:52 -04001399 name: ver.name + "-Client-ClientAuth-RSA",
David Benjamin636293b2014-07-08 17:59:18 -04001400 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001401 MinVersion: ver.version,
1402 MaxVersion: ver.version,
1403 ClientAuth: RequireAnyClientCert,
1404 ClientCAs: certPool,
David Benjamin636293b2014-07-08 17:59:18 -04001405 },
1406 flags: []string{
1407 "-cert-file", rsaCertificateFile,
1408 "-key-file", rsaKeyFile,
1409 },
1410 })
1411 testCases = append(testCases, testCase{
David Benjamin67666e72014-07-12 15:47:52 -04001412 testType: serverTest,
1413 name: ver.name + "-Server-ClientAuth-RSA",
1414 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001415 MinVersion: ver.version,
1416 MaxVersion: ver.version,
David Benjamin67666e72014-07-12 15:47:52 -04001417 Certificates: []Certificate{rsaCertificate},
1418 },
1419 flags: []string{"-require-any-client-certificate"},
1420 })
David Benjamine098ec22014-08-27 23:13:20 -04001421 if ver.version != VersionSSL30 {
1422 testCases = append(testCases, testCase{
1423 testType: serverTest,
1424 name: ver.name + "-Server-ClientAuth-ECDSA",
1425 config: Config{
1426 MinVersion: ver.version,
1427 MaxVersion: ver.version,
1428 Certificates: []Certificate{ecdsaCertificate},
1429 },
1430 flags: []string{"-require-any-client-certificate"},
1431 })
1432 testCases = append(testCases, testCase{
1433 testType: clientTest,
1434 name: ver.name + "-Client-ClientAuth-ECDSA",
1435 config: Config{
1436 MinVersion: ver.version,
1437 MaxVersion: ver.version,
1438 ClientAuth: RequireAnyClientCert,
1439 ClientCAs: certPool,
1440 },
1441 flags: []string{
1442 "-cert-file", ecdsaCertificateFile,
1443 "-key-file", ecdsaKeyFile,
1444 },
1445 })
1446 }
David Benjamin636293b2014-07-08 17:59:18 -04001447 }
1448}
1449
Adam Langley75712922014-10-10 16:23:43 -07001450func addExtendedMasterSecretTests() {
1451 const expectEMSFlag = "-expect-extended-master-secret"
1452
1453 for _, with := range []bool{false, true} {
1454 prefix := "No"
1455 var flags []string
1456 if with {
1457 prefix = ""
1458 flags = []string{expectEMSFlag}
1459 }
1460
1461 for _, isClient := range []bool{false, true} {
1462 suffix := "-Server"
1463 testType := serverTest
1464 if isClient {
1465 suffix = "-Client"
1466 testType = clientTest
1467 }
1468
1469 for _, ver := range tlsVersions {
1470 test := testCase{
1471 testType: testType,
1472 name: prefix + "ExtendedMasterSecret-" + ver.name + suffix,
1473 config: Config{
1474 MinVersion: ver.version,
1475 MaxVersion: ver.version,
1476 Bugs: ProtocolBugs{
1477 NoExtendedMasterSecret: !with,
1478 RequireExtendedMasterSecret: with,
1479 },
1480 },
David Benjamin48cae082014-10-27 01:06:24 -04001481 flags: flags,
1482 shouldFail: ver.version == VersionSSL30 && with,
Adam Langley75712922014-10-10 16:23:43 -07001483 }
1484 if test.shouldFail {
1485 test.expectedLocalError = "extended master secret required but not supported by peer"
1486 }
1487 testCases = append(testCases, test)
1488 }
1489 }
1490 }
1491
1492 // When a session is resumed, it should still be aware that its master
1493 // secret was generated via EMS and thus it's safe to use tls-unique.
1494 testCases = append(testCases, testCase{
1495 name: "ExtendedMasterSecret-Resume",
1496 config: Config{
1497 Bugs: ProtocolBugs{
1498 RequireExtendedMasterSecret: true,
1499 },
1500 },
1501 flags: []string{expectEMSFlag},
1502 resumeSession: true,
1503 })
1504}
1505
David Benjamin43ec06f2014-08-05 02:28:57 -04001506// Adds tests that try to cover the range of the handshake state machine, under
1507// various conditions. Some of these are redundant with other tests, but they
1508// only cover the synchronous case.
David Benjamin6fd297b2014-08-11 18:43:38 -04001509func addStateMachineCoverageTests(async, splitHandshake bool, protocol protocol) {
David Benjamin43ec06f2014-08-05 02:28:57 -04001510 var suffix string
1511 var flags []string
1512 var maxHandshakeRecordLength int
David Benjamin6fd297b2014-08-11 18:43:38 -04001513 if protocol == dtls {
1514 suffix = "-DTLS"
1515 }
David Benjamin43ec06f2014-08-05 02:28:57 -04001516 if async {
David Benjamin6fd297b2014-08-11 18:43:38 -04001517 suffix += "-Async"
David Benjamin43ec06f2014-08-05 02:28:57 -04001518 flags = append(flags, "-async")
1519 } else {
David Benjamin6fd297b2014-08-11 18:43:38 -04001520 suffix += "-Sync"
David Benjamin43ec06f2014-08-05 02:28:57 -04001521 }
1522 if splitHandshake {
1523 suffix += "-SplitHandshakeRecords"
David Benjamin98214542014-08-07 18:02:39 -04001524 maxHandshakeRecordLength = 1
David Benjamin43ec06f2014-08-05 02:28:57 -04001525 }
1526
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001527 // Basic handshake, with resumption. Client and server,
1528 // session ID and session ticket.
David Benjamin43ec06f2014-08-05 02:28:57 -04001529 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001530 protocol: protocol,
1531 name: "Basic-Client" + suffix,
David Benjamin43ec06f2014-08-05 02:28:57 -04001532 config: Config{
1533 Bugs: ProtocolBugs{
1534 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1535 },
1536 },
David Benjaminbed9aae2014-08-07 19:13:38 -04001537 flags: flags,
1538 resumeSession: true,
1539 })
1540 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001541 protocol: protocol,
1542 name: "Basic-Client-RenewTicket" + suffix,
David Benjaminbed9aae2014-08-07 19:13:38 -04001543 config: Config{
1544 Bugs: ProtocolBugs{
1545 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1546 RenewTicketOnResume: true,
1547 },
1548 },
1549 flags: flags,
1550 resumeSession: true,
David Benjamin43ec06f2014-08-05 02:28:57 -04001551 })
1552 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001553 protocol: protocol,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001554 name: "Basic-Client-NoTicket" + suffix,
1555 config: Config{
1556 SessionTicketsDisabled: true,
1557 Bugs: ProtocolBugs{
1558 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1559 },
1560 },
1561 flags: flags,
1562 resumeSession: true,
1563 })
1564 testCases = append(testCases, testCase{
1565 protocol: protocol,
David Benjamine0e7d0d2015-02-08 19:33:25 -05001566 name: "Basic-Client-Implicit" + suffix,
1567 config: Config{
1568 Bugs: ProtocolBugs{
1569 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1570 },
1571 },
1572 flags: append(flags, "-implicit-handshake"),
1573 resumeSession: true,
1574 })
1575 testCases = append(testCases, testCase{
1576 protocol: protocol,
David Benjamin43ec06f2014-08-05 02:28:57 -04001577 testType: serverTest,
1578 name: "Basic-Server" + suffix,
1579 config: Config{
1580 Bugs: ProtocolBugs{
1581 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1582 },
1583 },
David Benjaminbed9aae2014-08-07 19:13:38 -04001584 flags: flags,
1585 resumeSession: true,
David Benjamin43ec06f2014-08-05 02:28:57 -04001586 })
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001587 testCases = append(testCases, testCase{
1588 protocol: protocol,
1589 testType: serverTest,
1590 name: "Basic-Server-NoTickets" + suffix,
1591 config: Config{
1592 SessionTicketsDisabled: true,
1593 Bugs: ProtocolBugs{
1594 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1595 },
1596 },
1597 flags: flags,
1598 resumeSession: true,
1599 })
David Benjamine0e7d0d2015-02-08 19:33:25 -05001600 testCases = append(testCases, testCase{
1601 protocol: protocol,
1602 testType: serverTest,
1603 name: "Basic-Server-Implicit" + suffix,
1604 config: Config{
1605 Bugs: ProtocolBugs{
1606 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1607 },
1608 },
1609 flags: append(flags, "-implicit-handshake"),
1610 resumeSession: true,
1611 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001612
David Benjamin6fd297b2014-08-11 18:43:38 -04001613 // TLS client auth.
1614 testCases = append(testCases, testCase{
1615 protocol: protocol,
1616 testType: clientTest,
1617 name: "ClientAuth-Client" + suffix,
1618 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001619 ClientAuth: RequireAnyClientCert,
David Benjamin6fd297b2014-08-11 18:43:38 -04001620 Bugs: ProtocolBugs{
1621 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1622 },
1623 },
1624 flags: append(flags,
1625 "-cert-file", rsaCertificateFile,
1626 "-key-file", rsaKeyFile),
1627 })
1628 testCases = append(testCases, testCase{
1629 protocol: protocol,
1630 testType: serverTest,
1631 name: "ClientAuth-Server" + suffix,
1632 config: Config{
1633 Certificates: []Certificate{rsaCertificate},
1634 },
1635 flags: append(flags, "-require-any-client-certificate"),
1636 })
1637
David Benjamin43ec06f2014-08-05 02:28:57 -04001638 // No session ticket support; server doesn't send NewSessionTicket.
1639 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001640 protocol: protocol,
1641 name: "SessionTicketsDisabled-Client" + suffix,
David Benjamin43ec06f2014-08-05 02:28:57 -04001642 config: Config{
1643 SessionTicketsDisabled: true,
1644 Bugs: ProtocolBugs{
1645 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1646 },
1647 },
1648 flags: flags,
1649 })
1650 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001651 protocol: protocol,
David Benjamin43ec06f2014-08-05 02:28:57 -04001652 testType: serverTest,
1653 name: "SessionTicketsDisabled-Server" + suffix,
1654 config: Config{
1655 SessionTicketsDisabled: true,
1656 Bugs: ProtocolBugs{
1657 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1658 },
1659 },
1660 flags: flags,
1661 })
1662
David Benjamin48cae082014-10-27 01:06:24 -04001663 // Skip ServerKeyExchange in PSK key exchange if there's no
1664 // identity hint.
1665 testCases = append(testCases, testCase{
1666 protocol: protocol,
1667 name: "EmptyPSKHint-Client" + suffix,
1668 config: Config{
1669 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
1670 PreSharedKey: []byte("secret"),
1671 Bugs: ProtocolBugs{
1672 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1673 },
1674 },
1675 flags: append(flags, "-psk", "secret"),
1676 })
1677 testCases = append(testCases, testCase{
1678 protocol: protocol,
1679 testType: serverTest,
1680 name: "EmptyPSKHint-Server" + suffix,
1681 config: Config{
1682 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
1683 PreSharedKey: []byte("secret"),
1684 Bugs: ProtocolBugs{
1685 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1686 },
1687 },
1688 flags: append(flags, "-psk", "secret"),
1689 })
1690
David Benjamin6fd297b2014-08-11 18:43:38 -04001691 if protocol == tls {
1692 // NPN on client and server; results in post-handshake message.
1693 testCases = append(testCases, testCase{
1694 protocol: protocol,
1695 name: "NPN-Client" + suffix,
1696 config: Config{
David Benjaminae2888f2014-09-06 12:58:58 -04001697 NextProtos: []string{"foo"},
David Benjamin6fd297b2014-08-11 18:43:38 -04001698 Bugs: ProtocolBugs{
1699 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1700 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001701 },
David Benjaminfc7b0862014-09-06 13:21:53 -04001702 flags: append(flags, "-select-next-proto", "foo"),
1703 expectedNextProto: "foo",
1704 expectedNextProtoType: npn,
David Benjamin6fd297b2014-08-11 18:43:38 -04001705 })
1706 testCases = append(testCases, testCase{
1707 protocol: protocol,
1708 testType: serverTest,
1709 name: "NPN-Server" + suffix,
1710 config: Config{
1711 NextProtos: []string{"bar"},
1712 Bugs: ProtocolBugs{
1713 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1714 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001715 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001716 flags: append(flags,
1717 "-advertise-npn", "\x03foo\x03bar\x03baz",
1718 "-expect-next-proto", "bar"),
David Benjaminfc7b0862014-09-06 13:21:53 -04001719 expectedNextProto: "bar",
1720 expectedNextProtoType: npn,
David Benjamin6fd297b2014-08-11 18:43:38 -04001721 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001722
David Benjamin6fd297b2014-08-11 18:43:38 -04001723 // Client does False Start and negotiates NPN.
1724 testCases = append(testCases, testCase{
1725 protocol: protocol,
1726 name: "FalseStart" + suffix,
1727 config: Config{
1728 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1729 NextProtos: []string{"foo"},
1730 Bugs: ProtocolBugs{
David Benjamine58c4f52014-08-24 03:47:07 -04001731 ExpectFalseStart: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001732 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1733 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001734 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001735 flags: append(flags,
1736 "-false-start",
1737 "-select-next-proto", "foo"),
David Benjamine58c4f52014-08-24 03:47:07 -04001738 shimWritesFirst: true,
1739 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001740 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001741
David Benjaminae2888f2014-09-06 12:58:58 -04001742 // Client does False Start and negotiates ALPN.
1743 testCases = append(testCases, testCase{
1744 protocol: protocol,
1745 name: "FalseStart-ALPN" + suffix,
1746 config: Config{
1747 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1748 NextProtos: []string{"foo"},
1749 Bugs: ProtocolBugs{
1750 ExpectFalseStart: true,
1751 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1752 },
1753 },
1754 flags: append(flags,
1755 "-false-start",
1756 "-advertise-alpn", "\x03foo"),
1757 shimWritesFirst: true,
1758 resumeSession: true,
1759 })
1760
David Benjamin6fd297b2014-08-11 18:43:38 -04001761 // False Start without session tickets.
1762 testCases = append(testCases, testCase{
1763 name: "FalseStart-SessionTicketsDisabled",
1764 config: Config{
1765 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1766 NextProtos: []string{"foo"},
1767 SessionTicketsDisabled: true,
David Benjamin4e99c522014-08-24 01:45:30 -04001768 Bugs: ProtocolBugs{
David Benjamine58c4f52014-08-24 03:47:07 -04001769 ExpectFalseStart: true,
David Benjamin4e99c522014-08-24 01:45:30 -04001770 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1771 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001772 },
David Benjamin4e99c522014-08-24 01:45:30 -04001773 flags: append(flags,
David Benjamin6fd297b2014-08-11 18:43:38 -04001774 "-false-start",
1775 "-select-next-proto", "foo",
David Benjamin4e99c522014-08-24 01:45:30 -04001776 ),
David Benjamine58c4f52014-08-24 03:47:07 -04001777 shimWritesFirst: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001778 })
David Benjamin1e7f8d72014-08-08 12:27:04 -04001779
David Benjamina08e49d2014-08-24 01:46:07 -04001780 // Server parses a V2ClientHello.
David Benjamin6fd297b2014-08-11 18:43:38 -04001781 testCases = append(testCases, testCase{
1782 protocol: protocol,
1783 testType: serverTest,
1784 name: "SendV2ClientHello" + suffix,
1785 config: Config{
1786 // Choose a cipher suite that does not involve
1787 // elliptic curves, so no extensions are
1788 // involved.
1789 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
1790 Bugs: ProtocolBugs{
1791 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1792 SendV2ClientHello: true,
1793 },
David Benjamin1e7f8d72014-08-08 12:27:04 -04001794 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001795 flags: flags,
1796 })
David Benjamina08e49d2014-08-24 01:46:07 -04001797
1798 // Client sends a Channel ID.
1799 testCases = append(testCases, testCase{
1800 protocol: protocol,
1801 name: "ChannelID-Client" + suffix,
1802 config: Config{
1803 RequestChannelID: true,
1804 Bugs: ProtocolBugs{
1805 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1806 },
1807 },
1808 flags: append(flags,
1809 "-send-channel-id", channelIDKeyFile,
1810 ),
1811 resumeSession: true,
1812 expectChannelID: true,
1813 })
1814
1815 // Server accepts a Channel ID.
1816 testCases = append(testCases, testCase{
1817 protocol: protocol,
1818 testType: serverTest,
1819 name: "ChannelID-Server" + suffix,
1820 config: Config{
1821 ChannelID: channelIDKey,
1822 Bugs: ProtocolBugs{
1823 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1824 },
1825 },
1826 flags: append(flags,
1827 "-expect-channel-id",
1828 base64.StdEncoding.EncodeToString(channelIDBytes),
1829 ),
1830 resumeSession: true,
1831 expectChannelID: true,
1832 })
David Benjamin6fd297b2014-08-11 18:43:38 -04001833 } else {
1834 testCases = append(testCases, testCase{
1835 protocol: protocol,
1836 name: "SkipHelloVerifyRequest" + suffix,
1837 config: Config{
1838 Bugs: ProtocolBugs{
1839 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1840 SkipHelloVerifyRequest: true,
1841 },
1842 },
1843 flags: flags,
1844 })
1845
1846 testCases = append(testCases, testCase{
1847 testType: serverTest,
1848 protocol: protocol,
1849 name: "CookieExchange" + suffix,
1850 config: Config{
1851 Bugs: ProtocolBugs{
1852 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1853 },
1854 },
1855 flags: append(flags, "-cookie-exchange"),
1856 })
1857 }
David Benjamin43ec06f2014-08-05 02:28:57 -04001858}
1859
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001860func addVersionNegotiationTests() {
1861 for i, shimVers := range tlsVersions {
1862 // Assemble flags to disable all newer versions on the shim.
1863 var flags []string
1864 for _, vers := range tlsVersions[i+1:] {
1865 flags = append(flags, vers.flag)
1866 }
1867
1868 for _, runnerVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05001869 protocols := []protocol{tls}
1870 if runnerVers.hasDTLS && shimVers.hasDTLS {
1871 protocols = append(protocols, dtls)
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001872 }
David Benjamin8b8c0062014-11-23 02:47:52 -05001873 for _, protocol := range protocols {
1874 expectedVersion := shimVers.version
1875 if runnerVers.version < shimVers.version {
1876 expectedVersion = runnerVers.version
1877 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001878
David Benjamin8b8c0062014-11-23 02:47:52 -05001879 suffix := shimVers.name + "-" + runnerVers.name
1880 if protocol == dtls {
1881 suffix += "-DTLS"
1882 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001883
David Benjamin1eb367c2014-12-12 18:17:51 -05001884 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
1885
David Benjamin1e29a6b2014-12-10 02:27:24 -05001886 clientVers := shimVers.version
1887 if clientVers > VersionTLS10 {
1888 clientVers = VersionTLS10
1889 }
David Benjamin8b8c0062014-11-23 02:47:52 -05001890 testCases = append(testCases, testCase{
1891 protocol: protocol,
1892 testType: clientTest,
1893 name: "VersionNegotiation-Client-" + suffix,
1894 config: Config{
1895 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05001896 Bugs: ProtocolBugs{
1897 ExpectInitialRecordVersion: clientVers,
1898 },
David Benjamin8b8c0062014-11-23 02:47:52 -05001899 },
1900 flags: flags,
1901 expectedVersion: expectedVersion,
1902 })
David Benjamin1eb367c2014-12-12 18:17:51 -05001903 testCases = append(testCases, testCase{
1904 protocol: protocol,
1905 testType: clientTest,
1906 name: "VersionNegotiation-Client2-" + suffix,
1907 config: Config{
1908 MaxVersion: runnerVers.version,
1909 Bugs: ProtocolBugs{
1910 ExpectInitialRecordVersion: clientVers,
1911 },
1912 },
1913 flags: []string{"-max-version", shimVersFlag},
1914 expectedVersion: expectedVersion,
1915 })
David Benjamin8b8c0062014-11-23 02:47:52 -05001916
1917 testCases = append(testCases, testCase{
1918 protocol: protocol,
1919 testType: serverTest,
1920 name: "VersionNegotiation-Server-" + suffix,
1921 config: Config{
1922 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05001923 Bugs: ProtocolBugs{
1924 ExpectInitialRecordVersion: expectedVersion,
1925 },
David Benjamin8b8c0062014-11-23 02:47:52 -05001926 },
1927 flags: flags,
1928 expectedVersion: expectedVersion,
1929 })
David Benjamin1eb367c2014-12-12 18:17:51 -05001930 testCases = append(testCases, testCase{
1931 protocol: protocol,
1932 testType: serverTest,
1933 name: "VersionNegotiation-Server2-" + suffix,
1934 config: Config{
1935 MaxVersion: runnerVers.version,
1936 Bugs: ProtocolBugs{
1937 ExpectInitialRecordVersion: expectedVersion,
1938 },
1939 },
1940 flags: []string{"-max-version", shimVersFlag},
1941 expectedVersion: expectedVersion,
1942 })
David Benjamin8b8c0062014-11-23 02:47:52 -05001943 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001944 }
1945 }
1946}
1947
David Benjaminaccb4542014-12-12 23:44:33 -05001948func addMinimumVersionTests() {
1949 for i, shimVers := range tlsVersions {
1950 // Assemble flags to disable all older versions on the shim.
1951 var flags []string
1952 for _, vers := range tlsVersions[:i] {
1953 flags = append(flags, vers.flag)
1954 }
1955
1956 for _, runnerVers := range tlsVersions {
1957 protocols := []protocol{tls}
1958 if runnerVers.hasDTLS && shimVers.hasDTLS {
1959 protocols = append(protocols, dtls)
1960 }
1961 for _, protocol := range protocols {
1962 suffix := shimVers.name + "-" + runnerVers.name
1963 if protocol == dtls {
1964 suffix += "-DTLS"
1965 }
1966 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
1967
David Benjaminaccb4542014-12-12 23:44:33 -05001968 var expectedVersion uint16
1969 var shouldFail bool
1970 var expectedError string
David Benjamin87909c02014-12-13 01:55:01 -05001971 var expectedLocalError string
David Benjaminaccb4542014-12-12 23:44:33 -05001972 if runnerVers.version >= shimVers.version {
1973 expectedVersion = runnerVers.version
1974 } else {
1975 shouldFail = true
1976 expectedError = ":UNSUPPORTED_PROTOCOL:"
David Benjamin87909c02014-12-13 01:55:01 -05001977 if runnerVers.version > VersionSSL30 {
1978 expectedLocalError = "remote error: protocol version not supported"
1979 } else {
1980 expectedLocalError = "remote error: handshake failure"
1981 }
David Benjaminaccb4542014-12-12 23:44:33 -05001982 }
1983
1984 testCases = append(testCases, testCase{
1985 protocol: protocol,
1986 testType: clientTest,
1987 name: "MinimumVersion-Client-" + suffix,
1988 config: Config{
1989 MaxVersion: runnerVers.version,
1990 },
David Benjamin87909c02014-12-13 01:55:01 -05001991 flags: flags,
1992 expectedVersion: expectedVersion,
1993 shouldFail: shouldFail,
1994 expectedError: expectedError,
1995 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05001996 })
1997 testCases = append(testCases, testCase{
1998 protocol: protocol,
1999 testType: clientTest,
2000 name: "MinimumVersion-Client2-" + suffix,
2001 config: Config{
2002 MaxVersion: runnerVers.version,
2003 },
David Benjamin87909c02014-12-13 01:55:01 -05002004 flags: []string{"-min-version", shimVersFlag},
2005 expectedVersion: expectedVersion,
2006 shouldFail: shouldFail,
2007 expectedError: expectedError,
2008 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002009 })
2010
2011 testCases = append(testCases, testCase{
2012 protocol: protocol,
2013 testType: serverTest,
2014 name: "MinimumVersion-Server-" + suffix,
2015 config: Config{
2016 MaxVersion: runnerVers.version,
2017 },
David Benjamin87909c02014-12-13 01:55:01 -05002018 flags: flags,
2019 expectedVersion: expectedVersion,
2020 shouldFail: shouldFail,
2021 expectedError: expectedError,
2022 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002023 })
2024 testCases = append(testCases, testCase{
2025 protocol: protocol,
2026 testType: serverTest,
2027 name: "MinimumVersion-Server2-" + suffix,
2028 config: Config{
2029 MaxVersion: runnerVers.version,
2030 },
David Benjamin87909c02014-12-13 01:55:01 -05002031 flags: []string{"-min-version", shimVersFlag},
2032 expectedVersion: expectedVersion,
2033 shouldFail: shouldFail,
2034 expectedError: expectedError,
2035 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002036 })
2037 }
2038 }
2039 }
2040}
2041
David Benjamin5c24a1d2014-08-31 00:59:27 -04002042func addD5BugTests() {
2043 testCases = append(testCases, testCase{
2044 testType: serverTest,
2045 name: "D5Bug-NoQuirk-Reject",
2046 config: Config{
2047 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2048 Bugs: ProtocolBugs{
2049 SSL3RSAKeyExchange: true,
2050 },
2051 },
2052 shouldFail: true,
2053 expectedError: ":TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG:",
2054 })
2055 testCases = append(testCases, testCase{
2056 testType: serverTest,
2057 name: "D5Bug-Quirk-Normal",
2058 config: Config{
2059 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2060 },
2061 flags: []string{"-tls-d5-bug"},
2062 })
2063 testCases = append(testCases, testCase{
2064 testType: serverTest,
2065 name: "D5Bug-Quirk-Bug",
2066 config: Config{
2067 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2068 Bugs: ProtocolBugs{
2069 SSL3RSAKeyExchange: true,
2070 },
2071 },
2072 flags: []string{"-tls-d5-bug"},
2073 })
2074}
2075
David Benjamine78bfde2014-09-06 12:45:15 -04002076func addExtensionTests() {
2077 testCases = append(testCases, testCase{
2078 testType: clientTest,
2079 name: "DuplicateExtensionClient",
2080 config: Config{
2081 Bugs: ProtocolBugs{
2082 DuplicateExtension: true,
2083 },
2084 },
2085 shouldFail: true,
2086 expectedLocalError: "remote error: error decoding message",
2087 })
2088 testCases = append(testCases, testCase{
2089 testType: serverTest,
2090 name: "DuplicateExtensionServer",
2091 config: Config{
2092 Bugs: ProtocolBugs{
2093 DuplicateExtension: true,
2094 },
2095 },
2096 shouldFail: true,
2097 expectedLocalError: "remote error: error decoding message",
2098 })
2099 testCases = append(testCases, testCase{
2100 testType: clientTest,
2101 name: "ServerNameExtensionClient",
2102 config: Config{
2103 Bugs: ProtocolBugs{
2104 ExpectServerName: "example.com",
2105 },
2106 },
2107 flags: []string{"-host-name", "example.com"},
2108 })
2109 testCases = append(testCases, testCase{
2110 testType: clientTest,
2111 name: "ServerNameExtensionClient",
2112 config: Config{
2113 Bugs: ProtocolBugs{
2114 ExpectServerName: "mismatch.com",
2115 },
2116 },
2117 flags: []string{"-host-name", "example.com"},
2118 shouldFail: true,
2119 expectedLocalError: "tls: unexpected server name",
2120 })
2121 testCases = append(testCases, testCase{
2122 testType: clientTest,
2123 name: "ServerNameExtensionClient",
2124 config: Config{
2125 Bugs: ProtocolBugs{
2126 ExpectServerName: "missing.com",
2127 },
2128 },
2129 shouldFail: true,
2130 expectedLocalError: "tls: unexpected server name",
2131 })
2132 testCases = append(testCases, testCase{
2133 testType: serverTest,
2134 name: "ServerNameExtensionServer",
2135 config: Config{
2136 ServerName: "example.com",
2137 },
2138 flags: []string{"-expect-server-name", "example.com"},
2139 resumeSession: true,
2140 })
David Benjaminae2888f2014-09-06 12:58:58 -04002141 testCases = append(testCases, testCase{
2142 testType: clientTest,
2143 name: "ALPNClient",
2144 config: Config{
2145 NextProtos: []string{"foo"},
2146 },
2147 flags: []string{
2148 "-advertise-alpn", "\x03foo\x03bar\x03baz",
2149 "-expect-alpn", "foo",
2150 },
David Benjaminfc7b0862014-09-06 13:21:53 -04002151 expectedNextProto: "foo",
2152 expectedNextProtoType: alpn,
2153 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04002154 })
2155 testCases = append(testCases, testCase{
2156 testType: serverTest,
2157 name: "ALPNServer",
2158 config: Config{
2159 NextProtos: []string{"foo", "bar", "baz"},
2160 },
2161 flags: []string{
2162 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2163 "-select-alpn", "foo",
2164 },
David Benjaminfc7b0862014-09-06 13:21:53 -04002165 expectedNextProto: "foo",
2166 expectedNextProtoType: alpn,
2167 resumeSession: true,
2168 })
2169 // Test that the server prefers ALPN over NPN.
2170 testCases = append(testCases, testCase{
2171 testType: serverTest,
2172 name: "ALPNServer-Preferred",
2173 config: Config{
2174 NextProtos: []string{"foo", "bar", "baz"},
2175 },
2176 flags: []string{
2177 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2178 "-select-alpn", "foo",
2179 "-advertise-npn", "\x03foo\x03bar\x03baz",
2180 },
2181 expectedNextProto: "foo",
2182 expectedNextProtoType: alpn,
2183 resumeSession: true,
2184 })
2185 testCases = append(testCases, testCase{
2186 testType: serverTest,
2187 name: "ALPNServer-Preferred-Swapped",
2188 config: Config{
2189 NextProtos: []string{"foo", "bar", "baz"},
2190 Bugs: ProtocolBugs{
2191 SwapNPNAndALPN: true,
2192 },
2193 },
2194 flags: []string{
2195 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2196 "-select-alpn", "foo",
2197 "-advertise-npn", "\x03foo\x03bar\x03baz",
2198 },
2199 expectedNextProto: "foo",
2200 expectedNextProtoType: alpn,
2201 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04002202 })
Adam Langley38311732014-10-16 19:04:35 -07002203 // Resume with a corrupt ticket.
2204 testCases = append(testCases, testCase{
2205 testType: serverTest,
2206 name: "CorruptTicket",
2207 config: Config{
2208 Bugs: ProtocolBugs{
2209 CorruptTicket: true,
2210 },
2211 },
2212 resumeSession: true,
2213 flags: []string{"-expect-session-miss"},
2214 })
2215 // Resume with an oversized session id.
2216 testCases = append(testCases, testCase{
2217 testType: serverTest,
2218 name: "OversizedSessionId",
2219 config: Config{
2220 Bugs: ProtocolBugs{
2221 OversizedSessionId: true,
2222 },
2223 },
2224 resumeSession: true,
Adam Langley75712922014-10-10 16:23:43 -07002225 shouldFail: true,
Adam Langley38311732014-10-16 19:04:35 -07002226 expectedError: ":DECODE_ERROR:",
2227 })
David Benjaminca6c8262014-11-15 19:06:08 -05002228 // Basic DTLS-SRTP tests. Include fake profiles to ensure they
2229 // are ignored.
2230 testCases = append(testCases, testCase{
2231 protocol: dtls,
2232 name: "SRTP-Client",
2233 config: Config{
2234 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
2235 },
2236 flags: []string{
2237 "-srtp-profiles",
2238 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2239 },
2240 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2241 })
2242 testCases = append(testCases, testCase{
2243 protocol: dtls,
2244 testType: serverTest,
2245 name: "SRTP-Server",
2246 config: Config{
2247 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
2248 },
2249 flags: []string{
2250 "-srtp-profiles",
2251 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2252 },
2253 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2254 })
2255 // Test that the MKI is ignored.
2256 testCases = append(testCases, testCase{
2257 protocol: dtls,
2258 testType: serverTest,
2259 name: "SRTP-Server-IgnoreMKI",
2260 config: Config{
2261 SRTPProtectionProfiles: []uint16{SRTP_AES128_CM_HMAC_SHA1_80},
2262 Bugs: ProtocolBugs{
2263 SRTPMasterKeyIdentifer: "bogus",
2264 },
2265 },
2266 flags: []string{
2267 "-srtp-profiles",
2268 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2269 },
2270 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2271 })
2272 // Test that SRTP isn't negotiated on the server if there were
2273 // no matching profiles.
2274 testCases = append(testCases, testCase{
2275 protocol: dtls,
2276 testType: serverTest,
2277 name: "SRTP-Server-NoMatch",
2278 config: Config{
2279 SRTPProtectionProfiles: []uint16{100, 101, 102},
2280 },
2281 flags: []string{
2282 "-srtp-profiles",
2283 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2284 },
2285 expectedSRTPProtectionProfile: 0,
2286 })
2287 // Test that the server returning an invalid SRTP profile is
2288 // flagged as an error by the client.
2289 testCases = append(testCases, testCase{
2290 protocol: dtls,
2291 name: "SRTP-Client-NoMatch",
2292 config: Config{
2293 Bugs: ProtocolBugs{
2294 SendSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_32,
2295 },
2296 },
2297 flags: []string{
2298 "-srtp-profiles",
2299 "SRTP_AES128_CM_SHA1_80",
2300 },
2301 shouldFail: true,
2302 expectedError: ":BAD_SRTP_PROTECTION_PROFILE_LIST:",
2303 })
David Benjamin61f95272014-11-25 01:55:35 -05002304 // Test OCSP stapling and SCT list.
2305 testCases = append(testCases, testCase{
2306 name: "OCSPStapling",
2307 flags: []string{
2308 "-enable-ocsp-stapling",
2309 "-expect-ocsp-response",
2310 base64.StdEncoding.EncodeToString(testOCSPResponse),
2311 },
2312 })
2313 testCases = append(testCases, testCase{
2314 name: "SignedCertificateTimestampList",
2315 flags: []string{
2316 "-enable-signed-cert-timestamps",
2317 "-expect-signed-cert-timestamps",
2318 base64.StdEncoding.EncodeToString(testSCTList),
2319 },
2320 })
David Benjamine78bfde2014-09-06 12:45:15 -04002321}
2322
David Benjamin01fe8202014-09-24 15:21:44 -04002323func addResumptionVersionTests() {
David Benjamin01fe8202014-09-24 15:21:44 -04002324 for _, sessionVers := range tlsVersions {
David Benjamin01fe8202014-09-24 15:21:44 -04002325 for _, resumeVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05002326 protocols := []protocol{tls}
2327 if sessionVers.hasDTLS && resumeVers.hasDTLS {
2328 protocols = append(protocols, dtls)
David Benjaminbdf5e722014-11-11 00:52:15 -05002329 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002330 for _, protocol := range protocols {
2331 suffix := "-" + sessionVers.name + "-" + resumeVers.name
2332 if protocol == dtls {
2333 suffix += "-DTLS"
2334 }
2335
2336 testCases = append(testCases, testCase{
2337 protocol: protocol,
2338 name: "Resume-Client" + suffix,
2339 resumeSession: true,
2340 config: Config{
2341 MaxVersion: sessionVers.version,
2342 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2343 Bugs: ProtocolBugs{
2344 AllowSessionVersionMismatch: true,
2345 },
2346 },
2347 expectedVersion: sessionVers.version,
2348 resumeConfig: &Config{
2349 MaxVersion: resumeVers.version,
2350 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2351 Bugs: ProtocolBugs{
2352 AllowSessionVersionMismatch: true,
2353 },
2354 },
2355 expectedResumeVersion: resumeVers.version,
2356 })
2357
2358 testCases = append(testCases, testCase{
2359 protocol: protocol,
2360 name: "Resume-Client-NoResume" + suffix,
2361 flags: []string{"-expect-session-miss"},
2362 resumeSession: true,
2363 config: Config{
2364 MaxVersion: sessionVers.version,
2365 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2366 },
2367 expectedVersion: sessionVers.version,
2368 resumeConfig: &Config{
2369 MaxVersion: resumeVers.version,
2370 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2371 },
2372 newSessionsOnResume: true,
2373 expectedResumeVersion: resumeVers.version,
2374 })
2375
2376 var flags []string
2377 if sessionVers.version != resumeVers.version {
2378 flags = append(flags, "-expect-session-miss")
2379 }
2380 testCases = append(testCases, testCase{
2381 protocol: protocol,
2382 testType: serverTest,
2383 name: "Resume-Server" + suffix,
2384 flags: flags,
2385 resumeSession: true,
2386 config: Config{
2387 MaxVersion: sessionVers.version,
2388 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2389 },
2390 expectedVersion: sessionVers.version,
2391 resumeConfig: &Config{
2392 MaxVersion: resumeVers.version,
2393 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2394 },
2395 expectedResumeVersion: resumeVers.version,
2396 })
2397 }
David Benjamin01fe8202014-09-24 15:21:44 -04002398 }
2399 }
2400}
2401
Adam Langley2ae77d22014-10-28 17:29:33 -07002402func addRenegotiationTests() {
2403 testCases = append(testCases, testCase{
2404 testType: serverTest,
2405 name: "Renegotiate-Server",
2406 flags: []string{"-renegotiate"},
2407 shimWritesFirst: true,
2408 })
2409 testCases = append(testCases, testCase{
2410 testType: serverTest,
2411 name: "Renegotiate-Server-EmptyExt",
2412 config: Config{
2413 Bugs: ProtocolBugs{
2414 EmptyRenegotiationInfo: true,
2415 },
2416 },
2417 flags: []string{"-renegotiate"},
2418 shimWritesFirst: true,
2419 shouldFail: true,
2420 expectedError: ":RENEGOTIATION_MISMATCH:",
2421 })
2422 testCases = append(testCases, testCase{
2423 testType: serverTest,
2424 name: "Renegotiate-Server-BadExt",
2425 config: Config{
2426 Bugs: ProtocolBugs{
2427 BadRenegotiationInfo: true,
2428 },
2429 },
2430 flags: []string{"-renegotiate"},
2431 shimWritesFirst: true,
2432 shouldFail: true,
2433 expectedError: ":RENEGOTIATION_MISMATCH:",
2434 })
David Benjaminca6554b2014-11-08 12:31:52 -05002435 testCases = append(testCases, testCase{
2436 testType: serverTest,
2437 name: "Renegotiate-Server-ClientInitiated",
2438 renegotiate: true,
2439 })
2440 testCases = append(testCases, testCase{
2441 testType: serverTest,
2442 name: "Renegotiate-Server-ClientInitiated-NoExt",
2443 renegotiate: true,
2444 config: Config{
2445 Bugs: ProtocolBugs{
2446 NoRenegotiationInfo: true,
2447 },
2448 },
2449 shouldFail: true,
2450 expectedError: ":UNSAFE_LEGACY_RENEGOTIATION_DISABLED:",
2451 })
2452 testCases = append(testCases, testCase{
2453 testType: serverTest,
2454 name: "Renegotiate-Server-ClientInitiated-NoExt-Allowed",
2455 renegotiate: true,
2456 config: Config{
2457 Bugs: ProtocolBugs{
2458 NoRenegotiationInfo: true,
2459 },
2460 },
2461 flags: []string{"-allow-unsafe-legacy-renegotiation"},
2462 })
Adam Langley2ae77d22014-10-28 17:29:33 -07002463 // TODO(agl): test the renegotiation info SCSV.
Adam Langleycf2d4f42014-10-28 19:06:14 -07002464 testCases = append(testCases, testCase{
2465 name: "Renegotiate-Client",
2466 renegotiate: true,
2467 })
2468 testCases = append(testCases, testCase{
2469 name: "Renegotiate-Client-EmptyExt",
2470 renegotiate: true,
2471 config: Config{
2472 Bugs: ProtocolBugs{
2473 EmptyRenegotiationInfo: true,
2474 },
2475 },
2476 shouldFail: true,
2477 expectedError: ":RENEGOTIATION_MISMATCH:",
2478 })
2479 testCases = append(testCases, testCase{
2480 name: "Renegotiate-Client-BadExt",
2481 renegotiate: true,
2482 config: Config{
2483 Bugs: ProtocolBugs{
2484 BadRenegotiationInfo: true,
2485 },
2486 },
2487 shouldFail: true,
2488 expectedError: ":RENEGOTIATION_MISMATCH:",
2489 })
2490 testCases = append(testCases, testCase{
2491 name: "Renegotiate-Client-SwitchCiphers",
2492 renegotiate: true,
2493 config: Config{
2494 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2495 },
2496 renegotiateCiphers: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2497 })
2498 testCases = append(testCases, testCase{
2499 name: "Renegotiate-Client-SwitchCiphers2",
2500 renegotiate: true,
2501 config: Config{
2502 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2503 },
2504 renegotiateCiphers: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2505 })
David Benjaminc44b1df2014-11-23 12:11:01 -05002506 testCases = append(testCases, testCase{
2507 name: "Renegotiate-SameClientVersion",
2508 renegotiate: true,
2509 config: Config{
2510 MaxVersion: VersionTLS10,
2511 Bugs: ProtocolBugs{
2512 RequireSameRenegoClientVersion: true,
2513 },
2514 },
2515 })
Adam Langley2ae77d22014-10-28 17:29:33 -07002516}
2517
David Benjamin5e961c12014-11-07 01:48:35 -05002518func addDTLSReplayTests() {
2519 // Test that sequence number replays are detected.
2520 testCases = append(testCases, testCase{
2521 protocol: dtls,
2522 name: "DTLS-Replay",
2523 replayWrites: true,
2524 })
2525
2526 // Test the outgoing sequence number skipping by values larger
2527 // than the retransmit window.
2528 testCases = append(testCases, testCase{
2529 protocol: dtls,
2530 name: "DTLS-Replay-LargeGaps",
2531 config: Config{
2532 Bugs: ProtocolBugs{
2533 SequenceNumberIncrement: 127,
2534 },
2535 },
2536 replayWrites: true,
2537 })
2538}
2539
Feng Lu41aa3252014-11-21 22:47:56 -08002540func addFastRadioPaddingTests() {
David Benjamin1e29a6b2014-12-10 02:27:24 -05002541 testCases = append(testCases, testCase{
2542 protocol: tls,
2543 name: "FastRadio-Padding",
Feng Lu41aa3252014-11-21 22:47:56 -08002544 config: Config{
2545 Bugs: ProtocolBugs{
2546 RequireFastradioPadding: true,
2547 },
2548 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05002549 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08002550 })
David Benjamin1e29a6b2014-12-10 02:27:24 -05002551 testCases = append(testCases, testCase{
2552 protocol: dtls,
2553 name: "FastRadio-Padding",
Feng Lu41aa3252014-11-21 22:47:56 -08002554 config: Config{
2555 Bugs: ProtocolBugs{
2556 RequireFastradioPadding: true,
2557 },
2558 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05002559 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08002560 })
2561}
2562
David Benjamin000800a2014-11-14 01:43:59 -05002563var testHashes = []struct {
2564 name string
2565 id uint8
2566}{
2567 {"SHA1", hashSHA1},
2568 {"SHA224", hashSHA224},
2569 {"SHA256", hashSHA256},
2570 {"SHA384", hashSHA384},
2571 {"SHA512", hashSHA512},
2572}
2573
2574func addSigningHashTests() {
2575 // Make sure each hash works. Include some fake hashes in the list and
2576 // ensure they're ignored.
2577 for _, hash := range testHashes {
2578 testCases = append(testCases, testCase{
2579 name: "SigningHash-ClientAuth-" + hash.name,
2580 config: Config{
2581 ClientAuth: RequireAnyClientCert,
2582 SignatureAndHashes: []signatureAndHash{
2583 {signatureRSA, 42},
2584 {signatureRSA, hash.id},
2585 {signatureRSA, 255},
2586 },
2587 },
2588 flags: []string{
2589 "-cert-file", rsaCertificateFile,
2590 "-key-file", rsaKeyFile,
2591 },
2592 })
2593
2594 testCases = append(testCases, testCase{
2595 testType: serverTest,
2596 name: "SigningHash-ServerKeyExchange-Sign-" + hash.name,
2597 config: Config{
2598 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2599 SignatureAndHashes: []signatureAndHash{
2600 {signatureRSA, 42},
2601 {signatureRSA, hash.id},
2602 {signatureRSA, 255},
2603 },
2604 },
2605 })
2606 }
2607
2608 // Test that hash resolution takes the signature type into account.
2609 testCases = append(testCases, testCase{
2610 name: "SigningHash-ClientAuth-SignatureType",
2611 config: Config{
2612 ClientAuth: RequireAnyClientCert,
2613 SignatureAndHashes: []signatureAndHash{
2614 {signatureECDSA, hashSHA512},
2615 {signatureRSA, hashSHA384},
2616 {signatureECDSA, hashSHA1},
2617 },
2618 },
2619 flags: []string{
2620 "-cert-file", rsaCertificateFile,
2621 "-key-file", rsaKeyFile,
2622 },
2623 })
2624
2625 testCases = append(testCases, testCase{
2626 testType: serverTest,
2627 name: "SigningHash-ServerKeyExchange-SignatureType",
2628 config: Config{
2629 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2630 SignatureAndHashes: []signatureAndHash{
2631 {signatureECDSA, hashSHA512},
2632 {signatureRSA, hashSHA384},
2633 {signatureECDSA, hashSHA1},
2634 },
2635 },
2636 })
2637
2638 // Test that, if the list is missing, the peer falls back to SHA-1.
2639 testCases = append(testCases, testCase{
2640 name: "SigningHash-ClientAuth-Fallback",
2641 config: Config{
2642 ClientAuth: RequireAnyClientCert,
2643 SignatureAndHashes: []signatureAndHash{
2644 {signatureRSA, hashSHA1},
2645 },
2646 Bugs: ProtocolBugs{
2647 NoSignatureAndHashes: true,
2648 },
2649 },
2650 flags: []string{
2651 "-cert-file", rsaCertificateFile,
2652 "-key-file", rsaKeyFile,
2653 },
2654 })
2655
2656 testCases = append(testCases, testCase{
2657 testType: serverTest,
2658 name: "SigningHash-ServerKeyExchange-Fallback",
2659 config: Config{
2660 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2661 SignatureAndHashes: []signatureAndHash{
2662 {signatureRSA, hashSHA1},
2663 },
2664 Bugs: ProtocolBugs{
2665 NoSignatureAndHashes: true,
2666 },
2667 },
2668 })
2669}
2670
David Benjamin83f90402015-01-27 01:09:43 -05002671// timeouts is the retransmit schedule for BoringSSL. It doubles and
2672// caps at 60 seconds. On the 13th timeout, it gives up.
2673var timeouts = []time.Duration{
2674 1 * time.Second,
2675 2 * time.Second,
2676 4 * time.Second,
2677 8 * time.Second,
2678 16 * time.Second,
2679 32 * time.Second,
2680 60 * time.Second,
2681 60 * time.Second,
2682 60 * time.Second,
2683 60 * time.Second,
2684 60 * time.Second,
2685 60 * time.Second,
2686 60 * time.Second,
2687}
2688
2689func addDTLSRetransmitTests() {
2690 // Test that this is indeed the timeout schedule. Stress all
2691 // four patterns of handshake.
2692 for i := 1; i < len(timeouts); i++ {
2693 number := strconv.Itoa(i)
2694 testCases = append(testCases, testCase{
2695 protocol: dtls,
2696 name: "DTLS-Retransmit-Client-" + number,
2697 config: Config{
2698 Bugs: ProtocolBugs{
2699 TimeoutSchedule: timeouts[:i],
2700 },
2701 },
2702 resumeSession: true,
2703 flags: []string{"-async"},
2704 })
2705 testCases = append(testCases, testCase{
2706 protocol: dtls,
2707 testType: serverTest,
2708 name: "DTLS-Retransmit-Server-" + number,
2709 config: Config{
2710 Bugs: ProtocolBugs{
2711 TimeoutSchedule: timeouts[:i],
2712 },
2713 },
2714 resumeSession: true,
2715 flags: []string{"-async"},
2716 })
2717 }
2718
2719 // Test that exceeding the timeout schedule hits a read
2720 // timeout.
2721 testCases = append(testCases, testCase{
2722 protocol: dtls,
2723 name: "DTLS-Retransmit-Timeout",
2724 config: Config{
2725 Bugs: ProtocolBugs{
2726 TimeoutSchedule: timeouts,
2727 },
2728 },
2729 resumeSession: true,
2730 flags: []string{"-async"},
2731 shouldFail: true,
2732 expectedError: ":READ_TIMEOUT_EXPIRED:",
2733 })
2734
2735 // Test that timeout handling has a fudge factor, due to API
2736 // problems.
2737 testCases = append(testCases, testCase{
2738 protocol: dtls,
2739 name: "DTLS-Retransmit-Fudge",
2740 config: Config{
2741 Bugs: ProtocolBugs{
2742 TimeoutSchedule: []time.Duration{
2743 timeouts[0] - 10*time.Millisecond,
2744 },
2745 },
2746 },
2747 resumeSession: true,
2748 flags: []string{"-async"},
2749 })
2750}
2751
David Benjamin884fdf12014-08-02 15:28:23 -04002752func worker(statusChan chan statusMsg, c chan *testCase, buildDir string, wg *sync.WaitGroup) {
Adam Langley95c29f32014-06-20 12:00:00 -07002753 defer wg.Done()
2754
2755 for test := range c {
Adam Langley69a01602014-11-17 17:26:55 -08002756 var err error
2757
2758 if *mallocTest < 0 {
2759 statusChan <- statusMsg{test: test, started: true}
2760 err = runTest(test, buildDir, -1)
2761 } else {
2762 for mallocNumToFail := int64(*mallocTest); ; mallocNumToFail++ {
2763 statusChan <- statusMsg{test: test, started: true}
2764 if err = runTest(test, buildDir, mallocNumToFail); err != errMoreMallocs {
2765 if err != nil {
2766 fmt.Printf("\n\nmalloc test failed at %d: %s\n", mallocNumToFail, err)
2767 }
2768 break
2769 }
2770 }
2771 }
Adam Langley95c29f32014-06-20 12:00:00 -07002772 statusChan <- statusMsg{test: test, err: err}
2773 }
2774}
2775
2776type statusMsg struct {
2777 test *testCase
2778 started bool
2779 err error
2780}
2781
2782func statusPrinter(doneChan chan struct{}, statusChan chan statusMsg, total int) {
2783 var started, done, failed, lineLen int
2784 defer close(doneChan)
2785
2786 for msg := range statusChan {
2787 if msg.started {
2788 started++
2789 } else {
2790 done++
2791 }
2792
2793 fmt.Printf("\x1b[%dD\x1b[K", lineLen)
2794
2795 if msg.err != nil {
2796 fmt.Printf("FAILED (%s)\n%s\n", msg.test.name, msg.err)
2797 failed++
2798 }
2799 line := fmt.Sprintf("%d/%d/%d/%d", failed, done, started, total)
2800 lineLen = len(line)
2801 os.Stdout.WriteString(line)
2802 }
2803}
2804
2805func main() {
2806 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 -04002807 var flagNumWorkers *int = flag.Int("num-workers", runtime.NumCPU(), "The number of workers to run in parallel.")
David Benjamin884fdf12014-08-02 15:28:23 -04002808 var flagBuildDir *string = flag.String("build-dir", "../../../build", "The build directory to run the shim from.")
Adam Langley95c29f32014-06-20 12:00:00 -07002809
2810 flag.Parse()
2811
2812 addCipherSuiteTests()
2813 addBadECDSASignatureTests()
Adam Langley80842bd2014-06-20 12:00:00 -07002814 addCBCPaddingTests()
Kenny Root7fdeaf12014-08-05 15:23:37 -07002815 addCBCSplittingTests()
David Benjamin636293b2014-07-08 17:59:18 -04002816 addClientAuthTests()
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002817 addVersionNegotiationTests()
David Benjaminaccb4542014-12-12 23:44:33 -05002818 addMinimumVersionTests()
David Benjamin5c24a1d2014-08-31 00:59:27 -04002819 addD5BugTests()
David Benjamine78bfde2014-09-06 12:45:15 -04002820 addExtensionTests()
David Benjamin01fe8202014-09-24 15:21:44 -04002821 addResumptionVersionTests()
Adam Langley75712922014-10-10 16:23:43 -07002822 addExtendedMasterSecretTests()
Adam Langley2ae77d22014-10-28 17:29:33 -07002823 addRenegotiationTests()
David Benjamin5e961c12014-11-07 01:48:35 -05002824 addDTLSReplayTests()
David Benjamin000800a2014-11-14 01:43:59 -05002825 addSigningHashTests()
Feng Lu41aa3252014-11-21 22:47:56 -08002826 addFastRadioPaddingTests()
David Benjamin83f90402015-01-27 01:09:43 -05002827 addDTLSRetransmitTests()
David Benjamin43ec06f2014-08-05 02:28:57 -04002828 for _, async := range []bool{false, true} {
2829 for _, splitHandshake := range []bool{false, true} {
David Benjamin6fd297b2014-08-11 18:43:38 -04002830 for _, protocol := range []protocol{tls, dtls} {
2831 addStateMachineCoverageTests(async, splitHandshake, protocol)
2832 }
David Benjamin43ec06f2014-08-05 02:28:57 -04002833 }
2834 }
Adam Langley95c29f32014-06-20 12:00:00 -07002835
2836 var wg sync.WaitGroup
2837
David Benjamin2bc8e6f2014-08-02 15:22:37 -04002838 numWorkers := *flagNumWorkers
Adam Langley95c29f32014-06-20 12:00:00 -07002839
2840 statusChan := make(chan statusMsg, numWorkers)
2841 testChan := make(chan *testCase, numWorkers)
2842 doneChan := make(chan struct{})
2843
David Benjamin025b3d32014-07-01 19:53:04 -04002844 go statusPrinter(doneChan, statusChan, len(testCases))
Adam Langley95c29f32014-06-20 12:00:00 -07002845
2846 for i := 0; i < numWorkers; i++ {
2847 wg.Add(1)
David Benjamin884fdf12014-08-02 15:28:23 -04002848 go worker(statusChan, testChan, *flagBuildDir, &wg)
Adam Langley95c29f32014-06-20 12:00:00 -07002849 }
2850
David Benjamin025b3d32014-07-01 19:53:04 -04002851 for i := range testCases {
2852 if len(*flagTest) == 0 || *flagTest == testCases[i].name {
2853 testChan <- &testCases[i]
Adam Langley95c29f32014-06-20 12:00:00 -07002854 }
2855 }
2856
2857 close(testChan)
2858 wg.Wait()
2859 close(statusChan)
2860 <-doneChan
2861
2862 fmt.Printf("\n")
2863}