blob: 8e9a94800f9ea33d57132e21f1916cead7349830 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001package main
2
3import (
4 "bytes"
David Benjamina08e49d2014-08-24 01:46:07 -04005 "crypto/ecdsa"
6 "crypto/elliptic"
David Benjamin407a10c2014-07-16 12:58:59 -04007 "crypto/x509"
David Benjamin2561dc32014-08-24 01:25:27 -04008 "encoding/base64"
David Benjamina08e49d2014-08-24 01:46:07 -04009 "encoding/pem"
Adam Langley95c29f32014-06-20 12:00:00 -070010 "flag"
11 "fmt"
12 "io"
Kenny Root7fdeaf12014-08-05 15:23:37 -070013 "io/ioutil"
Adam Langley95c29f32014-06-20 12:00:00 -070014 "net"
15 "os"
16 "os/exec"
David Benjamin884fdf12014-08-02 15:28:23 -040017 "path"
David Benjamin2bc8e6f2014-08-02 15:22:37 -040018 "runtime"
Adam Langley69a01602014-11-17 17:26:55 -080019 "strconv"
Adam Langley95c29f32014-06-20 12:00:00 -070020 "strings"
21 "sync"
22 "syscall"
David Benjamin83f90402015-01-27 01:09:43 -050023 "time"
Adam Langley95c29f32014-06-20 12:00:00 -070024)
25
Adam Langley69a01602014-11-17 17:26:55 -080026var (
David Benjamin5f237bc2015-02-11 17:14:15 -050027 useValgrind = flag.Bool("valgrind", false, "If true, run code under valgrind")
28 useGDB = flag.Bool("gdb", false, "If true, run BoringSSL code under gdb")
29 flagDebug = flag.Bool("debug", false, "Hexdump the contents of the connection")
30 mallocTest = flag.Int64("malloc-test", -1, "If non-negative, run each test with each malloc in turn failing from the given number onwards.")
31 mallocTestDebug = flag.Bool("malloc-test-debug", false, "If true, ask bssl_shim to abort rather than fail a malloc. This can be used with a specific value for --malloc-test to identity the malloc failing that is causing problems.")
32 jsonOutput = flag.String("json-output", "", "The file to output JSON results to.")
33 pipe = flag.Bool("pipe", false, "If true, print status output suitable for piping into another program.")
Adam Langley69a01602014-11-17 17:26:55 -080034)
Adam Langley95c29f32014-06-20 12:00:00 -070035
David Benjamin025b3d32014-07-01 19:53:04 -040036const (
37 rsaCertificateFile = "cert.pem"
38 ecdsaCertificateFile = "ecdsa_cert.pem"
39)
40
41const (
David Benjamina08e49d2014-08-24 01:46:07 -040042 rsaKeyFile = "key.pem"
43 ecdsaKeyFile = "ecdsa_key.pem"
44 channelIDKeyFile = "channel_id_key.pem"
David Benjamin025b3d32014-07-01 19:53:04 -040045)
46
Adam Langley95c29f32014-06-20 12:00:00 -070047var rsaCertificate, ecdsaCertificate Certificate
David Benjamina08e49d2014-08-24 01:46:07 -040048var channelIDKey *ecdsa.PrivateKey
49var channelIDBytes []byte
Adam Langley95c29f32014-06-20 12:00:00 -070050
David Benjamin61f95272014-11-25 01:55:35 -050051var testOCSPResponse = []byte{1, 2, 3, 4}
52var testSCTList = []byte{5, 6, 7, 8}
53
Adam Langley95c29f32014-06-20 12:00:00 -070054func initCertificates() {
55 var err error
David Benjamin025b3d32014-07-01 19:53:04 -040056 rsaCertificate, err = LoadX509KeyPair(rsaCertificateFile, rsaKeyFile)
Adam Langley95c29f32014-06-20 12:00:00 -070057 if err != nil {
58 panic(err)
59 }
David Benjamin61f95272014-11-25 01:55:35 -050060 rsaCertificate.OCSPStaple = testOCSPResponse
61 rsaCertificate.SignedCertificateTimestampList = testSCTList
Adam Langley95c29f32014-06-20 12:00:00 -070062
David Benjamin025b3d32014-07-01 19:53:04 -040063 ecdsaCertificate, err = LoadX509KeyPair(ecdsaCertificateFile, ecdsaKeyFile)
Adam Langley95c29f32014-06-20 12:00:00 -070064 if err != nil {
65 panic(err)
66 }
David Benjamin61f95272014-11-25 01:55:35 -050067 ecdsaCertificate.OCSPStaple = testOCSPResponse
68 ecdsaCertificate.SignedCertificateTimestampList = testSCTList
David Benjamina08e49d2014-08-24 01:46:07 -040069
70 channelIDPEMBlock, err := ioutil.ReadFile(channelIDKeyFile)
71 if err != nil {
72 panic(err)
73 }
74 channelIDDERBlock, _ := pem.Decode(channelIDPEMBlock)
75 if channelIDDERBlock.Type != "EC PRIVATE KEY" {
76 panic("bad key type")
77 }
78 channelIDKey, err = x509.ParseECPrivateKey(channelIDDERBlock.Bytes)
79 if err != nil {
80 panic(err)
81 }
82 if channelIDKey.Curve != elliptic.P256() {
83 panic("bad curve")
84 }
85
86 channelIDBytes = make([]byte, 64)
87 writeIntPadded(channelIDBytes[:32], channelIDKey.X)
88 writeIntPadded(channelIDBytes[32:], channelIDKey.Y)
Adam Langley95c29f32014-06-20 12:00:00 -070089}
90
91var certificateOnce sync.Once
92
93func getRSACertificate() Certificate {
94 certificateOnce.Do(initCertificates)
95 return rsaCertificate
96}
97
98func getECDSACertificate() Certificate {
99 certificateOnce.Do(initCertificates)
100 return ecdsaCertificate
101}
102
David Benjamin025b3d32014-07-01 19:53:04 -0400103type testType int
104
105const (
106 clientTest testType = iota
107 serverTest
108)
109
David Benjamin6fd297b2014-08-11 18:43:38 -0400110type protocol int
111
112const (
113 tls protocol = iota
114 dtls
115)
116
David Benjaminfc7b0862014-09-06 13:21:53 -0400117const (
118 alpn = 1
119 npn = 2
120)
121
Adam Langley95c29f32014-06-20 12:00:00 -0700122type testCase struct {
David Benjamin025b3d32014-07-01 19:53:04 -0400123 testType testType
David Benjamin6fd297b2014-08-11 18:43:38 -0400124 protocol protocol
Adam Langley95c29f32014-06-20 12:00:00 -0700125 name string
126 config Config
127 shouldFail bool
128 expectedError string
Adam Langleyac61fa32014-06-23 12:03:11 -0700129 // expectedLocalError, if not empty, contains a substring that must be
130 // found in the local error.
131 expectedLocalError string
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400132 // expectedVersion, if non-zero, specifies the TLS version that must be
133 // negotiated.
134 expectedVersion uint16
David Benjamin01fe8202014-09-24 15:21:44 -0400135 // expectedResumeVersion, if non-zero, specifies the TLS version that
136 // must be negotiated on resumption. If zero, expectedVersion is used.
137 expectedResumeVersion uint16
David Benjamina08e49d2014-08-24 01:46:07 -0400138 // expectChannelID controls whether the connection should have
139 // negotiated a Channel ID with channelIDKey.
140 expectChannelID bool
David Benjaminae2888f2014-09-06 12:58:58 -0400141 // expectedNextProto controls whether the connection should
142 // negotiate a next protocol via NPN or ALPN.
143 expectedNextProto string
David Benjaminfc7b0862014-09-06 13:21:53 -0400144 // expectedNextProtoType, if non-zero, is the expected next
145 // protocol negotiation mechanism.
146 expectedNextProtoType int
David Benjaminca6c8262014-11-15 19:06:08 -0500147 // expectedSRTPProtectionProfile is the DTLS-SRTP profile that
148 // should be negotiated. If zero, none should be negotiated.
149 expectedSRTPProtectionProfile uint16
Adam Langley80842bd2014-06-20 12:00:00 -0700150 // messageLen is the length, in bytes, of the test message that will be
151 // sent.
152 messageLen int
David Benjamin025b3d32014-07-01 19:53:04 -0400153 // certFile is the path to the certificate to use for the server.
154 certFile string
155 // keyFile is the path to the private key to use for the server.
156 keyFile string
David Benjamin1d5c83e2014-07-22 19:20:02 -0400157 // resumeSession controls whether a second connection should be tested
David Benjamin01fe8202014-09-24 15:21:44 -0400158 // which attempts to resume the first session.
David Benjamin1d5c83e2014-07-22 19:20:02 -0400159 resumeSession bool
David Benjamin01fe8202014-09-24 15:21:44 -0400160 // resumeConfig, if not nil, points to a Config to be used on
David Benjaminfe8eb9a2014-11-17 03:19:02 -0500161 // resumption. Unless newSessionsOnResume is set,
162 // SessionTicketKey, ServerSessionCache, and
163 // ClientSessionCache are copied from the initial connection's
164 // config. If nil, the initial connection's config is used.
David Benjamin01fe8202014-09-24 15:21:44 -0400165 resumeConfig *Config
David Benjaminfe8eb9a2014-11-17 03:19:02 -0500166 // newSessionsOnResume, if true, will cause resumeConfig to
167 // use a different session resumption context.
168 newSessionsOnResume bool
David Benjamin98e882e2014-08-08 13:24:34 -0400169 // sendPrefix sends a prefix on the socket before actually performing a
170 // handshake.
171 sendPrefix string
David Benjamine58c4f52014-08-24 03:47:07 -0400172 // shimWritesFirst controls whether the shim sends an initial "hello"
173 // message before doing a roundtrip with the runner.
174 shimWritesFirst bool
Adam Langleycf2d4f42014-10-28 19:06:14 -0700175 // renegotiate indicates the the connection should be renegotiated
176 // during the exchange.
177 renegotiate bool
178 // renegotiateCiphers is a list of ciphersuite ids that will be
179 // switched in just before renegotiation.
180 renegotiateCiphers []uint16
David Benjamin5e961c12014-11-07 01:48:35 -0500181 // replayWrites, if true, configures the underlying transport
182 // to replay every write it makes in DTLS tests.
183 replayWrites bool
David Benjamin5fa3eba2015-01-22 16:35:40 -0500184 // damageFirstWrite, if true, configures the underlying transport to
185 // damage the final byte of the first application data write.
186 damageFirstWrite bool
David Benjamin325b5c32014-07-01 19:40:31 -0400187 // flags, if not empty, contains a list of command-line flags that will
188 // be passed to the shim program.
189 flags []string
Adam Langley95c29f32014-06-20 12:00:00 -0700190}
191
David Benjamin025b3d32014-07-01 19:53:04 -0400192var testCases = []testCase{
Adam Langley95c29f32014-06-20 12:00:00 -0700193 {
194 name: "BadRSASignature",
195 config: Config{
196 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
197 Bugs: ProtocolBugs{
198 InvalidSKXSignature: true,
199 },
200 },
201 shouldFail: true,
202 expectedError: ":BAD_SIGNATURE:",
203 },
204 {
205 name: "BadECDSASignature",
206 config: Config{
207 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
208 Bugs: ProtocolBugs{
209 InvalidSKXSignature: true,
210 },
211 Certificates: []Certificate{getECDSACertificate()},
212 },
213 shouldFail: true,
214 expectedError: ":BAD_SIGNATURE:",
215 },
216 {
217 name: "BadECDSACurve",
218 config: Config{
219 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
220 Bugs: ProtocolBugs{
221 InvalidSKXCurve: true,
222 },
223 Certificates: []Certificate{getECDSACertificate()},
224 },
225 shouldFail: true,
226 expectedError: ":WRONG_CURVE:",
227 },
Adam Langleyac61fa32014-06-23 12:03:11 -0700228 {
David Benjamina8e3e0e2014-08-06 22:11:10 -0400229 testType: serverTest,
230 name: "BadRSAVersion",
231 config: Config{
232 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
233 Bugs: ProtocolBugs{
234 RsaClientKeyExchangeVersion: VersionTLS11,
235 },
236 },
237 shouldFail: true,
238 expectedError: ":DECRYPTION_FAILED_OR_BAD_RECORD_MAC:",
239 },
240 {
David Benjamin325b5c32014-07-01 19:40:31 -0400241 name: "NoFallbackSCSV",
Adam Langleyac61fa32014-06-23 12:03:11 -0700242 config: Config{
243 Bugs: ProtocolBugs{
244 FailIfNotFallbackSCSV: true,
245 },
246 },
247 shouldFail: true,
248 expectedLocalError: "no fallback SCSV found",
249 },
David Benjamin325b5c32014-07-01 19:40:31 -0400250 {
David Benjamin2a0c4962014-08-22 23:46:35 -0400251 name: "SendFallbackSCSV",
David Benjamin325b5c32014-07-01 19:40:31 -0400252 config: Config{
253 Bugs: ProtocolBugs{
254 FailIfNotFallbackSCSV: true,
255 },
256 },
257 flags: []string{"-fallback-scsv"},
258 },
David Benjamin197b3ab2014-07-02 18:37:33 -0400259 {
David Benjamin7b030512014-07-08 17:30:11 -0400260 name: "ClientCertificateTypes",
261 config: Config{
262 ClientAuth: RequestClientCert,
263 ClientCertificateTypes: []byte{
264 CertTypeDSSSign,
265 CertTypeRSASign,
266 CertTypeECDSASign,
267 },
268 },
David Benjamin2561dc32014-08-24 01:25:27 -0400269 flags: []string{
270 "-expect-certificate-types",
271 base64.StdEncoding.EncodeToString([]byte{
272 CertTypeDSSSign,
273 CertTypeRSASign,
274 CertTypeECDSASign,
275 }),
276 },
David Benjamin7b030512014-07-08 17:30:11 -0400277 },
David Benjamin636293b2014-07-08 17:59:18 -0400278 {
279 name: "NoClientCertificate",
280 config: Config{
281 ClientAuth: RequireAnyClientCert,
282 },
283 shouldFail: true,
284 expectedLocalError: "client didn't provide a certificate",
285 },
David Benjamin1c375dd2014-07-12 00:48:23 -0400286 {
287 name: "UnauthenticatedECDH",
288 config: Config{
289 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
290 Bugs: ProtocolBugs{
291 UnauthenticatedECDH: true,
292 },
293 },
294 shouldFail: true,
David Benjamine8f3d662014-07-12 01:10:19 -0400295 expectedError: ":UNEXPECTED_MESSAGE:",
David Benjamin1c375dd2014-07-12 00:48:23 -0400296 },
David Benjamin9c651c92014-07-12 13:27:45 -0400297 {
298 name: "SkipServerKeyExchange",
299 config: Config{
300 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
301 Bugs: ProtocolBugs{
302 SkipServerKeyExchange: true,
303 },
304 },
305 shouldFail: true,
306 expectedError: ":UNEXPECTED_MESSAGE:",
307 },
David Benjamin1f5f62b2014-07-12 16:18:02 -0400308 {
David Benjamina0e52232014-07-19 17:39:58 -0400309 name: "SkipChangeCipherSpec-Client",
310 config: Config{
311 Bugs: ProtocolBugs{
312 SkipChangeCipherSpec: true,
313 },
314 },
315 shouldFail: true,
David Benjamin86271ee2014-07-21 16:14:03 -0400316 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
David Benjamina0e52232014-07-19 17:39:58 -0400317 },
318 {
319 testType: serverTest,
320 name: "SkipChangeCipherSpec-Server",
321 config: Config{
322 Bugs: ProtocolBugs{
323 SkipChangeCipherSpec: true,
324 },
325 },
326 shouldFail: true,
David Benjamin86271ee2014-07-21 16:14:03 -0400327 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
David Benjamina0e52232014-07-19 17:39:58 -0400328 },
David Benjamin42be6452014-07-21 14:50:23 -0400329 {
330 testType: serverTest,
331 name: "SkipChangeCipherSpec-Server-NPN",
332 config: Config{
333 NextProtos: []string{"bar"},
334 Bugs: ProtocolBugs{
335 SkipChangeCipherSpec: true,
336 },
337 },
338 flags: []string{
339 "-advertise-npn", "\x03foo\x03bar\x03baz",
340 },
341 shouldFail: true,
David Benjamin86271ee2014-07-21 16:14:03 -0400342 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
343 },
344 {
345 name: "FragmentAcrossChangeCipherSpec-Client",
346 config: Config{
347 Bugs: ProtocolBugs{
348 FragmentAcrossChangeCipherSpec: true,
349 },
350 },
351 shouldFail: true,
352 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
353 },
354 {
355 testType: serverTest,
356 name: "FragmentAcrossChangeCipherSpec-Server",
357 config: Config{
358 Bugs: ProtocolBugs{
359 FragmentAcrossChangeCipherSpec: true,
360 },
361 },
362 shouldFail: true,
363 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
364 },
365 {
366 testType: serverTest,
367 name: "FragmentAcrossChangeCipherSpec-Server-NPN",
368 config: Config{
369 NextProtos: []string{"bar"},
370 Bugs: ProtocolBugs{
371 FragmentAcrossChangeCipherSpec: true,
372 },
373 },
374 flags: []string{
375 "-advertise-npn", "\x03foo\x03bar\x03baz",
376 },
377 shouldFail: true,
378 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
David Benjamin42be6452014-07-21 14:50:23 -0400379 },
David Benjaminf3ec83d2014-07-21 22:42:34 -0400380 {
381 testType: serverTest,
David Benjamin3fd1fbd2015-02-03 16:07:32 -0500382 name: "Alert",
383 config: Config{
384 Bugs: ProtocolBugs{
385 SendSpuriousAlert: alertRecordOverflow,
386 },
387 },
388 shouldFail: true,
389 expectedError: ":TLSV1_ALERT_RECORD_OVERFLOW:",
390 },
391 {
392 protocol: dtls,
393 testType: serverTest,
394 name: "Alert-DTLS",
395 config: Config{
396 Bugs: ProtocolBugs{
397 SendSpuriousAlert: alertRecordOverflow,
398 },
399 },
400 shouldFail: true,
401 expectedError: ":TLSV1_ALERT_RECORD_OVERFLOW:",
402 },
403 {
404 testType: serverTest,
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400405 name: "FragmentAlert",
406 config: Config{
407 Bugs: ProtocolBugs{
David Benjaminca6c8262014-11-15 19:06:08 -0500408 FragmentAlert: true,
David Benjamin3fd1fbd2015-02-03 16:07:32 -0500409 SendSpuriousAlert: alertRecordOverflow,
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400410 },
411 },
412 shouldFail: true,
413 expectedError: ":BAD_ALERT:",
414 },
415 {
David Benjamin0ea8dda2015-01-31 20:33:40 -0500416 protocol: dtls,
417 testType: serverTest,
418 name: "FragmentAlert-DTLS",
419 config: Config{
420 Bugs: ProtocolBugs{
421 FragmentAlert: true,
David Benjamin3fd1fbd2015-02-03 16:07:32 -0500422 SendSpuriousAlert: alertRecordOverflow,
David Benjamin0ea8dda2015-01-31 20:33:40 -0500423 },
424 },
425 shouldFail: true,
426 expectedError: ":BAD_ALERT:",
427 },
428 {
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400429 testType: serverTest,
David Benjaminf3ec83d2014-07-21 22:42:34 -0400430 name: "EarlyChangeCipherSpec-server-1",
431 config: Config{
432 Bugs: ProtocolBugs{
433 EarlyChangeCipherSpec: 1,
434 },
435 },
436 shouldFail: true,
437 expectedError: ":CCS_RECEIVED_EARLY:",
438 },
439 {
440 testType: serverTest,
441 name: "EarlyChangeCipherSpec-server-2",
442 config: Config{
443 Bugs: ProtocolBugs{
444 EarlyChangeCipherSpec: 2,
445 },
446 },
447 shouldFail: true,
448 expectedError: ":CCS_RECEIVED_EARLY:",
449 },
David Benjamind23f4122014-07-23 15:09:48 -0400450 {
David Benjamind23f4122014-07-23 15:09:48 -0400451 name: "SkipNewSessionTicket",
452 config: Config{
453 Bugs: ProtocolBugs{
454 SkipNewSessionTicket: true,
455 },
456 },
457 shouldFail: true,
458 expectedError: ":CCS_RECEIVED_EARLY:",
459 },
David Benjamin7e3305e2014-07-28 14:52:32 -0400460 {
David Benjamind86c7672014-08-02 04:07:12 -0400461 testType: serverTest,
David Benjaminbef270a2014-08-02 04:22:02 -0400462 name: "FallbackSCSV",
463 config: Config{
464 MaxVersion: VersionTLS11,
465 Bugs: ProtocolBugs{
466 SendFallbackSCSV: true,
467 },
468 },
469 shouldFail: true,
470 expectedError: ":INAPPROPRIATE_FALLBACK:",
471 },
472 {
473 testType: serverTest,
474 name: "FallbackSCSV-VersionMatch",
475 config: Config{
476 Bugs: ProtocolBugs{
477 SendFallbackSCSV: true,
478 },
479 },
480 },
David Benjamin98214542014-08-07 18:02:39 -0400481 {
482 testType: serverTest,
483 name: "FragmentedClientVersion",
484 config: Config{
485 Bugs: ProtocolBugs{
486 MaxHandshakeRecordLength: 1,
487 FragmentClientVersion: true,
488 },
489 },
David Benjamin82c9e902014-12-12 15:55:27 -0500490 expectedVersion: VersionTLS12,
David Benjamin98214542014-08-07 18:02:39 -0400491 },
David Benjamin98e882e2014-08-08 13:24:34 -0400492 {
493 testType: serverTest,
494 name: "MinorVersionTolerance",
495 config: Config{
496 Bugs: ProtocolBugs{
497 SendClientVersion: 0x03ff,
498 },
499 },
500 expectedVersion: VersionTLS12,
501 },
502 {
503 testType: serverTest,
504 name: "MajorVersionTolerance",
505 config: Config{
506 Bugs: ProtocolBugs{
507 SendClientVersion: 0x0400,
508 },
509 },
510 expectedVersion: VersionTLS12,
511 },
512 {
513 testType: serverTest,
514 name: "VersionTooLow",
515 config: Config{
516 Bugs: ProtocolBugs{
517 SendClientVersion: 0x0200,
518 },
519 },
520 shouldFail: true,
521 expectedError: ":UNSUPPORTED_PROTOCOL:",
522 },
523 {
524 testType: serverTest,
525 name: "HttpGET",
526 sendPrefix: "GET / HTTP/1.0\n",
527 shouldFail: true,
528 expectedError: ":HTTP_REQUEST:",
529 },
530 {
531 testType: serverTest,
532 name: "HttpPOST",
533 sendPrefix: "POST / HTTP/1.0\n",
534 shouldFail: true,
535 expectedError: ":HTTP_REQUEST:",
536 },
537 {
538 testType: serverTest,
539 name: "HttpHEAD",
540 sendPrefix: "HEAD / HTTP/1.0\n",
541 shouldFail: true,
542 expectedError: ":HTTP_REQUEST:",
543 },
544 {
545 testType: serverTest,
546 name: "HttpPUT",
547 sendPrefix: "PUT / HTTP/1.0\n",
548 shouldFail: true,
549 expectedError: ":HTTP_REQUEST:",
550 },
551 {
552 testType: serverTest,
553 name: "HttpCONNECT",
554 sendPrefix: "CONNECT www.google.com:443 HTTP/1.0\n",
555 shouldFail: true,
556 expectedError: ":HTTPS_PROXY_REQUEST:",
557 },
David Benjamin39ebf532014-08-31 02:23:49 -0400558 {
David Benjaminf080ecd2014-12-11 18:48:59 -0500559 testType: serverTest,
560 name: "Garbage",
561 sendPrefix: "blah",
562 shouldFail: true,
563 expectedError: ":UNKNOWN_PROTOCOL:",
564 },
565 {
David Benjamin39ebf532014-08-31 02:23:49 -0400566 name: "SkipCipherVersionCheck",
567 config: Config{
568 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
569 MaxVersion: VersionTLS11,
570 Bugs: ProtocolBugs{
571 SkipCipherVersionCheck: true,
572 },
573 },
574 shouldFail: true,
575 expectedError: ":WRONG_CIPHER_RETURNED:",
576 },
David Benjamin9114fae2014-11-08 11:41:14 -0500577 {
578 name: "RSAServerKeyExchange",
579 config: Config{
580 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
581 Bugs: ProtocolBugs{
582 RSAServerKeyExchange: true,
583 },
584 },
585 shouldFail: true,
586 expectedError: ":UNEXPECTED_MESSAGE:",
587 },
David Benjamin128dbc32014-12-01 01:27:42 -0500588 {
589 name: "DisableEverything",
590 flags: []string{"-no-tls12", "-no-tls11", "-no-tls1", "-no-ssl3"},
591 shouldFail: true,
592 expectedError: ":WRONG_SSL_VERSION:",
593 },
594 {
595 protocol: dtls,
596 name: "DisableEverything-DTLS",
597 flags: []string{"-no-tls12", "-no-tls1"},
598 shouldFail: true,
599 expectedError: ":WRONG_SSL_VERSION:",
600 },
David Benjamin780d6dd2015-01-06 12:03:19 -0500601 {
602 name: "NoSharedCipher",
603 config: Config{
604 CipherSuites: []uint16{},
605 },
606 shouldFail: true,
607 expectedError: ":HANDSHAKE_FAILURE_ON_CLIENT_HELLO:",
608 },
David Benjamin13be1de2015-01-11 16:29:36 -0500609 {
610 protocol: dtls,
611 testType: serverTest,
612 name: "MTU",
613 config: Config{
614 Bugs: ProtocolBugs{
615 MaxPacketLength: 256,
616 },
617 },
618 flags: []string{"-mtu", "256"},
619 },
620 {
621 protocol: dtls,
622 testType: serverTest,
623 name: "MTUExceeded",
624 config: Config{
625 Bugs: ProtocolBugs{
626 MaxPacketLength: 255,
627 },
628 },
629 flags: []string{"-mtu", "256"},
630 shouldFail: true,
631 expectedLocalError: "dtls: exceeded maximum packet length",
632 },
David Benjamin6095de82014-12-27 01:50:38 -0500633 {
634 name: "CertMismatchRSA",
635 config: Config{
636 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
637 Certificates: []Certificate{getECDSACertificate()},
638 Bugs: ProtocolBugs{
639 SendCipherSuite: TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
640 },
641 },
642 shouldFail: true,
643 expectedError: ":WRONG_CERTIFICATE_TYPE:",
644 },
645 {
646 name: "CertMismatchECDSA",
647 config: Config{
648 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
649 Certificates: []Certificate{getRSACertificate()},
650 Bugs: ProtocolBugs{
651 SendCipherSuite: TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
652 },
653 },
654 shouldFail: true,
655 expectedError: ":WRONG_CERTIFICATE_TYPE:",
656 },
David Benjamin5fa3eba2015-01-22 16:35:40 -0500657 {
658 name: "TLSFatalBadPackets",
659 damageFirstWrite: true,
660 shouldFail: true,
661 expectedError: ":DECRYPTION_FAILED_OR_BAD_RECORD_MAC:",
662 },
663 {
664 protocol: dtls,
665 name: "DTLSIgnoreBadPackets",
666 damageFirstWrite: true,
667 },
668 {
669 protocol: dtls,
670 name: "DTLSIgnoreBadPackets-Async",
671 damageFirstWrite: true,
672 flags: []string{"-async"},
673 },
David Benjamin4189bd92015-01-25 23:52:39 -0500674 {
675 name: "AppDataAfterChangeCipherSpec",
676 config: Config{
677 Bugs: ProtocolBugs{
678 AppDataAfterChangeCipherSpec: []byte("TEST MESSAGE"),
679 },
680 },
681 shouldFail: true,
682 expectedError: ":DATA_BETWEEN_CCS_AND_FINISHED:",
683 },
684 {
685 protocol: dtls,
686 name: "AppDataAfterChangeCipherSpec-DTLS",
687 config: Config{
688 Bugs: ProtocolBugs{
689 AppDataAfterChangeCipherSpec: []byte("TEST MESSAGE"),
690 },
691 },
692 },
David Benjaminb3774b92015-01-31 17:16:01 -0500693 {
694 protocol: dtls,
695 name: "ReorderHandshakeFragments-Small-DTLS",
696 config: Config{
697 Bugs: ProtocolBugs{
698 ReorderHandshakeFragments: true,
699 // Small enough that every handshake message is
700 // fragmented.
701 MaxHandshakeRecordLength: 2,
702 },
703 },
704 },
705 {
706 protocol: dtls,
707 name: "ReorderHandshakeFragments-Large-DTLS",
708 config: Config{
709 Bugs: ProtocolBugs{
710 ReorderHandshakeFragments: true,
711 // Large enough that no handshake message is
712 // fragmented.
713 //
714 // TODO(davidben): Also test interaction of
715 // complete handshake messages with
716 // fragments. The current logic is full of bugs
717 // here, so the reassembly logic needs a rewrite
718 // before those tests will pass.
719 MaxHandshakeRecordLength: 2048,
720 },
721 },
722 },
David Benjaminddb9f152015-02-03 15:44:39 -0500723 {
724 name: "SendInvalidRecordType",
725 config: Config{
726 Bugs: ProtocolBugs{
727 SendInvalidRecordType: true,
728 },
729 },
730 shouldFail: true,
731 expectedError: ":UNEXPECTED_RECORD:",
732 },
733 {
734 protocol: dtls,
735 name: "SendInvalidRecordType-DTLS",
736 config: Config{
737 Bugs: ProtocolBugs{
738 SendInvalidRecordType: true,
739 },
740 },
741 shouldFail: true,
742 expectedError: ":UNEXPECTED_RECORD:",
743 },
David Benjaminb80168e2015-02-08 18:30:14 -0500744 {
745 name: "FalseStart-SkipServerSecondLeg",
746 config: Config{
747 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
748 NextProtos: []string{"foo"},
749 Bugs: ProtocolBugs{
750 SkipNewSessionTicket: true,
751 SkipChangeCipherSpec: true,
752 SkipFinished: true,
753 ExpectFalseStart: true,
754 },
755 },
756 flags: []string{
757 "-false-start",
758 "-advertise-alpn", "\x03foo",
759 },
760 shimWritesFirst: true,
761 shouldFail: true,
762 expectedError: ":UNEXPECTED_RECORD:",
763 },
David Benjamin931ab342015-02-08 19:46:57 -0500764 {
765 name: "FalseStart-SkipServerSecondLeg-Implicit",
766 config: Config{
767 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
768 NextProtos: []string{"foo"},
769 Bugs: ProtocolBugs{
770 SkipNewSessionTicket: true,
771 SkipChangeCipherSpec: true,
772 SkipFinished: true,
773 },
774 },
775 flags: []string{
776 "-implicit-handshake",
777 "-false-start",
778 "-advertise-alpn", "\x03foo",
779 },
780 shouldFail: true,
781 expectedError: ":UNEXPECTED_RECORD:",
782 },
Adam Langley95c29f32014-06-20 12:00:00 -0700783}
784
David Benjamin01fe8202014-09-24 15:21:44 -0400785func doExchange(test *testCase, config *Config, conn net.Conn, messageLen int, isResume bool) error {
David Benjamin65ea8ff2014-11-23 03:01:00 -0500786 var connDebug *recordingConn
David Benjamin5fa3eba2015-01-22 16:35:40 -0500787 var connDamage *damageAdaptor
David Benjamin65ea8ff2014-11-23 03:01:00 -0500788 if *flagDebug {
789 connDebug = &recordingConn{Conn: conn}
790 conn = connDebug
791 defer func() {
792 connDebug.WriteTo(os.Stdout)
793 }()
794 }
795
David Benjamin6fd297b2014-08-11 18:43:38 -0400796 if test.protocol == dtls {
David Benjamin83f90402015-01-27 01:09:43 -0500797 config.Bugs.PacketAdaptor = newPacketAdaptor(conn)
798 conn = config.Bugs.PacketAdaptor
David Benjamin5e961c12014-11-07 01:48:35 -0500799 if test.replayWrites {
800 conn = newReplayAdaptor(conn)
801 }
David Benjamin6fd297b2014-08-11 18:43:38 -0400802 }
803
David Benjamin5fa3eba2015-01-22 16:35:40 -0500804 if test.damageFirstWrite {
805 connDamage = newDamageAdaptor(conn)
806 conn = connDamage
807 }
808
David Benjamin6fd297b2014-08-11 18:43:38 -0400809 if test.sendPrefix != "" {
810 if _, err := conn.Write([]byte(test.sendPrefix)); err != nil {
811 return err
812 }
David Benjamin98e882e2014-08-08 13:24:34 -0400813 }
814
David Benjamin1d5c83e2014-07-22 19:20:02 -0400815 var tlsConn *Conn
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400816 if test.testType == clientTest {
David Benjamin6fd297b2014-08-11 18:43:38 -0400817 if test.protocol == dtls {
818 tlsConn = DTLSServer(conn, config)
819 } else {
820 tlsConn = Server(conn, config)
821 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400822 } else {
823 config.InsecureSkipVerify = true
David Benjamin6fd297b2014-08-11 18:43:38 -0400824 if test.protocol == dtls {
825 tlsConn = DTLSClient(conn, config)
826 } else {
827 tlsConn = Client(conn, config)
828 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400829 }
830
Adam Langley95c29f32014-06-20 12:00:00 -0700831 if err := tlsConn.Handshake(); err != nil {
832 return err
833 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700834
David Benjamin01fe8202014-09-24 15:21:44 -0400835 // TODO(davidben): move all per-connection expectations into a dedicated
836 // expectations struct that can be specified separately for the two
837 // legs.
838 expectedVersion := test.expectedVersion
839 if isResume && test.expectedResumeVersion != 0 {
840 expectedVersion = test.expectedResumeVersion
841 }
842 if vers := tlsConn.ConnectionState().Version; expectedVersion != 0 && vers != expectedVersion {
843 return fmt.Errorf("got version %x, expected %x", vers, expectedVersion)
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400844 }
845
David Benjamina08e49d2014-08-24 01:46:07 -0400846 if test.expectChannelID {
847 channelID := tlsConn.ConnectionState().ChannelID
848 if channelID == nil {
849 return fmt.Errorf("no channel ID negotiated")
850 }
851 if channelID.Curve != channelIDKey.Curve ||
852 channelIDKey.X.Cmp(channelIDKey.X) != 0 ||
853 channelIDKey.Y.Cmp(channelIDKey.Y) != 0 {
854 return fmt.Errorf("incorrect channel ID")
855 }
856 }
857
David Benjaminae2888f2014-09-06 12:58:58 -0400858 if expected := test.expectedNextProto; expected != "" {
859 if actual := tlsConn.ConnectionState().NegotiatedProtocol; actual != expected {
860 return fmt.Errorf("next proto mismatch: got %s, wanted %s", actual, expected)
861 }
862 }
863
David Benjaminfc7b0862014-09-06 13:21:53 -0400864 if test.expectedNextProtoType != 0 {
865 if (test.expectedNextProtoType == alpn) != tlsConn.ConnectionState().NegotiatedProtocolFromALPN {
866 return fmt.Errorf("next proto type mismatch")
867 }
868 }
869
David Benjaminca6c8262014-11-15 19:06:08 -0500870 if p := tlsConn.ConnectionState().SRTPProtectionProfile; p != test.expectedSRTPProtectionProfile {
871 return fmt.Errorf("SRTP profile mismatch: got %d, wanted %d", p, test.expectedSRTPProtectionProfile)
872 }
873
David Benjamine58c4f52014-08-24 03:47:07 -0400874 if test.shimWritesFirst {
875 var buf [5]byte
876 _, err := io.ReadFull(tlsConn, buf[:])
877 if err != nil {
878 return err
879 }
880 if string(buf[:]) != "hello" {
881 return fmt.Errorf("bad initial message")
882 }
883 }
884
Adam Langleycf2d4f42014-10-28 19:06:14 -0700885 if test.renegotiate {
886 if test.renegotiateCiphers != nil {
887 config.CipherSuites = test.renegotiateCiphers
888 }
889 if err := tlsConn.Renegotiate(); err != nil {
890 return err
891 }
892 } else if test.renegotiateCiphers != nil {
893 panic("renegotiateCiphers without renegotiate")
894 }
895
David Benjamin5fa3eba2015-01-22 16:35:40 -0500896 if test.damageFirstWrite {
897 connDamage.setDamage(true)
898 tlsConn.Write([]byte("DAMAGED WRITE"))
899 connDamage.setDamage(false)
900 }
901
Kenny Root7fdeaf12014-08-05 15:23:37 -0700902 if messageLen < 0 {
David Benjamin6fd297b2014-08-11 18:43:38 -0400903 if test.protocol == dtls {
904 return fmt.Errorf("messageLen < 0 not supported for DTLS tests")
905 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700906 // Read until EOF.
907 _, err := io.Copy(ioutil.Discard, tlsConn)
908 return err
909 }
910
David Benjamin4189bd92015-01-25 23:52:39 -0500911 var testMessage []byte
912 if config.Bugs.AppDataAfterChangeCipherSpec != nil {
913 // We've already sent a message. Expect the shim to echo it
914 // back.
915 testMessage = config.Bugs.AppDataAfterChangeCipherSpec
916 } else {
917 if messageLen == 0 {
918 messageLen = 32
919 }
920 testMessage = make([]byte, messageLen)
921 for i := range testMessage {
922 testMessage[i] = 0x42
923 }
924 tlsConn.Write(testMessage)
Adam Langley80842bd2014-06-20 12:00:00 -0700925 }
Adam Langley95c29f32014-06-20 12:00:00 -0700926
927 buf := make([]byte, len(testMessage))
David Benjamin6fd297b2014-08-11 18:43:38 -0400928 if test.protocol == dtls {
929 bufTmp := make([]byte, len(buf)+1)
930 n, err := tlsConn.Read(bufTmp)
931 if err != nil {
932 return err
933 }
934 if n != len(buf) {
935 return fmt.Errorf("bad reply; length mismatch (%d vs %d)", n, len(buf))
936 }
937 copy(buf, bufTmp)
938 } else {
939 _, err := io.ReadFull(tlsConn, buf)
940 if err != nil {
941 return err
942 }
Adam Langley95c29f32014-06-20 12:00:00 -0700943 }
944
945 for i, v := range buf {
946 if v != testMessage[i]^0xff {
947 return fmt.Errorf("bad reply contents at byte %d", i)
948 }
949 }
950
951 return nil
952}
953
David Benjamin325b5c32014-07-01 19:40:31 -0400954func valgrindOf(dbAttach bool, path string, args ...string) *exec.Cmd {
955 valgrindArgs := []string{"--error-exitcode=99", "--track-origins=yes", "--leak-check=full"}
Adam Langley95c29f32014-06-20 12:00:00 -0700956 if dbAttach {
David Benjamin325b5c32014-07-01 19:40:31 -0400957 valgrindArgs = append(valgrindArgs, "--db-attach=yes", "--db-command=xterm -e gdb -nw %f %p")
Adam Langley95c29f32014-06-20 12:00:00 -0700958 }
David Benjamin325b5c32014-07-01 19:40:31 -0400959 valgrindArgs = append(valgrindArgs, path)
960 valgrindArgs = append(valgrindArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -0700961
David Benjamin325b5c32014-07-01 19:40:31 -0400962 return exec.Command("valgrind", valgrindArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -0700963}
964
David Benjamin325b5c32014-07-01 19:40:31 -0400965func gdbOf(path string, args ...string) *exec.Cmd {
966 xtermArgs := []string{"-e", "gdb", "--args"}
967 xtermArgs = append(xtermArgs, path)
968 xtermArgs = append(xtermArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -0700969
David Benjamin325b5c32014-07-01 19:40:31 -0400970 return exec.Command("xterm", xtermArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -0700971}
972
David Benjamin1d5c83e2014-07-22 19:20:02 -0400973func openSocketPair() (shimEnd *os.File, conn net.Conn) {
Adam Langley95c29f32014-06-20 12:00:00 -0700974 socks, err := syscall.Socketpair(syscall.AF_UNIX, syscall.SOCK_STREAM, 0)
975 if err != nil {
976 panic(err)
977 }
978
979 syscall.CloseOnExec(socks[0])
980 syscall.CloseOnExec(socks[1])
David Benjamin1d5c83e2014-07-22 19:20:02 -0400981 shimEnd = os.NewFile(uintptr(socks[0]), "shim end")
Adam Langley95c29f32014-06-20 12:00:00 -0700982 connFile := os.NewFile(uintptr(socks[1]), "our end")
David Benjamin1d5c83e2014-07-22 19:20:02 -0400983 conn, err = net.FileConn(connFile)
984 if err != nil {
985 panic(err)
986 }
Adam Langley95c29f32014-06-20 12:00:00 -0700987 connFile.Close()
988 if err != nil {
989 panic(err)
990 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400991 return shimEnd, conn
992}
993
Adam Langley69a01602014-11-17 17:26:55 -0800994type moreMallocsError struct{}
995
996func (moreMallocsError) Error() string {
997 return "child process did not exhaust all allocation calls"
998}
999
1000var errMoreMallocs = moreMallocsError{}
1001
1002func runTest(test *testCase, buildDir string, mallocNumToFail int64) error {
Adam Langley38311732014-10-16 19:04:35 -07001003 if !test.shouldFail && (len(test.expectedError) > 0 || len(test.expectedLocalError) > 0) {
1004 panic("Error expected without shouldFail in " + test.name)
1005 }
1006
David Benjamin1d5c83e2014-07-22 19:20:02 -04001007 shimEnd, conn := openSocketPair()
1008 shimEndResume, connResume := openSocketPair()
Adam Langley95c29f32014-06-20 12:00:00 -07001009
David Benjamin884fdf12014-08-02 15:28:23 -04001010 shim_path := path.Join(buildDir, "ssl/test/bssl_shim")
David Benjamin5a593af2014-08-11 19:51:50 -04001011 var flags []string
David Benjamin1d5c83e2014-07-22 19:20:02 -04001012 if test.testType == serverTest {
David Benjamin5a593af2014-08-11 19:51:50 -04001013 flags = append(flags, "-server")
1014
David Benjamin025b3d32014-07-01 19:53:04 -04001015 flags = append(flags, "-key-file")
1016 if test.keyFile == "" {
1017 flags = append(flags, rsaKeyFile)
1018 } else {
1019 flags = append(flags, test.keyFile)
1020 }
1021
1022 flags = append(flags, "-cert-file")
1023 if test.certFile == "" {
1024 flags = append(flags, rsaCertificateFile)
1025 } else {
1026 flags = append(flags, test.certFile)
1027 }
1028 }
David Benjamin5a593af2014-08-11 19:51:50 -04001029
David Benjamin6fd297b2014-08-11 18:43:38 -04001030 if test.protocol == dtls {
1031 flags = append(flags, "-dtls")
1032 }
1033
David Benjamin5a593af2014-08-11 19:51:50 -04001034 if test.resumeSession {
1035 flags = append(flags, "-resume")
1036 }
1037
David Benjamine58c4f52014-08-24 03:47:07 -04001038 if test.shimWritesFirst {
1039 flags = append(flags, "-shim-writes-first")
1040 }
1041
David Benjamin025b3d32014-07-01 19:53:04 -04001042 flags = append(flags, test.flags...)
1043
1044 var shim *exec.Cmd
1045 if *useValgrind {
1046 shim = valgrindOf(false, shim_path, flags...)
Adam Langley75712922014-10-10 16:23:43 -07001047 } else if *useGDB {
1048 shim = gdbOf(shim_path, flags...)
David Benjamin025b3d32014-07-01 19:53:04 -04001049 } else {
1050 shim = exec.Command(shim_path, flags...)
1051 }
David Benjamin1d5c83e2014-07-22 19:20:02 -04001052 shim.ExtraFiles = []*os.File{shimEnd, shimEndResume}
David Benjamin025b3d32014-07-01 19:53:04 -04001053 shim.Stdin = os.Stdin
1054 var stdoutBuf, stderrBuf bytes.Buffer
1055 shim.Stdout = &stdoutBuf
1056 shim.Stderr = &stderrBuf
Adam Langley69a01602014-11-17 17:26:55 -08001057 if mallocNumToFail >= 0 {
David Benjamin9e128b02015-02-09 13:13:09 -05001058 shim.Env = os.Environ()
1059 shim.Env = append(shim.Env, "MALLOC_NUMBER_TO_FAIL="+strconv.FormatInt(mallocNumToFail, 10))
Adam Langley69a01602014-11-17 17:26:55 -08001060 if *mallocTestDebug {
1061 shim.Env = append(shim.Env, "MALLOC_ABORT_ON_FAIL=1")
1062 }
1063 shim.Env = append(shim.Env, "_MALLOC_CHECK=1")
1064 }
David Benjamin025b3d32014-07-01 19:53:04 -04001065
1066 if err := shim.Start(); err != nil {
Adam Langley95c29f32014-06-20 12:00:00 -07001067 panic(err)
1068 }
David Benjamin025b3d32014-07-01 19:53:04 -04001069 shimEnd.Close()
David Benjamin1d5c83e2014-07-22 19:20:02 -04001070 shimEndResume.Close()
Adam Langley95c29f32014-06-20 12:00:00 -07001071
1072 config := test.config
David Benjamin1d5c83e2014-07-22 19:20:02 -04001073 config.ClientSessionCache = NewLRUClientSessionCache(1)
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001074 config.ServerSessionCache = NewLRUServerSessionCache(1)
David Benjamin025b3d32014-07-01 19:53:04 -04001075 if test.testType == clientTest {
1076 if len(config.Certificates) == 0 {
1077 config.Certificates = []Certificate{getRSACertificate()}
1078 }
David Benjamin025b3d32014-07-01 19:53:04 -04001079 }
Adam Langley95c29f32014-06-20 12:00:00 -07001080
David Benjamin01fe8202014-09-24 15:21:44 -04001081 err := doExchange(test, &config, conn, test.messageLen,
1082 false /* not a resumption */)
Adam Langley95c29f32014-06-20 12:00:00 -07001083 conn.Close()
David Benjamin65ea8ff2014-11-23 03:01:00 -05001084
David Benjamin1d5c83e2014-07-22 19:20:02 -04001085 if err == nil && test.resumeSession {
David Benjamin01fe8202014-09-24 15:21:44 -04001086 var resumeConfig Config
1087 if test.resumeConfig != nil {
1088 resumeConfig = *test.resumeConfig
1089 if len(resumeConfig.Certificates) == 0 {
1090 resumeConfig.Certificates = []Certificate{getRSACertificate()}
1091 }
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001092 if !test.newSessionsOnResume {
1093 resumeConfig.SessionTicketKey = config.SessionTicketKey
1094 resumeConfig.ClientSessionCache = config.ClientSessionCache
1095 resumeConfig.ServerSessionCache = config.ServerSessionCache
1096 }
David Benjamin01fe8202014-09-24 15:21:44 -04001097 } else {
1098 resumeConfig = config
1099 }
1100 err = doExchange(test, &resumeConfig, connResume, test.messageLen,
1101 true /* resumption */)
David Benjamin1d5c83e2014-07-22 19:20:02 -04001102 }
David Benjamin812152a2014-09-06 12:49:07 -04001103 connResume.Close()
David Benjamin1d5c83e2014-07-22 19:20:02 -04001104
David Benjamin025b3d32014-07-01 19:53:04 -04001105 childErr := shim.Wait()
Adam Langley69a01602014-11-17 17:26:55 -08001106 if exitError, ok := childErr.(*exec.ExitError); ok {
1107 if exitError.Sys().(syscall.WaitStatus).ExitStatus() == 88 {
1108 return errMoreMallocs
1109 }
1110 }
Adam Langley95c29f32014-06-20 12:00:00 -07001111
1112 stdout := string(stdoutBuf.Bytes())
1113 stderr := string(stderrBuf.Bytes())
1114 failed := err != nil || childErr != nil
1115 correctFailure := len(test.expectedError) == 0 || strings.Contains(stdout, test.expectedError)
Adam Langleyac61fa32014-06-23 12:03:11 -07001116 localError := "none"
1117 if err != nil {
1118 localError = err.Error()
1119 }
1120 if len(test.expectedLocalError) != 0 {
1121 correctFailure = correctFailure && strings.Contains(localError, test.expectedLocalError)
1122 }
Adam Langley95c29f32014-06-20 12:00:00 -07001123
1124 if failed != test.shouldFail || failed && !correctFailure {
Adam Langley95c29f32014-06-20 12:00:00 -07001125 childError := "none"
Adam Langley95c29f32014-06-20 12:00:00 -07001126 if childErr != nil {
1127 childError = childErr.Error()
1128 }
1129
1130 var msg string
1131 switch {
1132 case failed && !test.shouldFail:
1133 msg = "unexpected failure"
1134 case !failed && test.shouldFail:
1135 msg = "unexpected success"
1136 case failed && !correctFailure:
Adam Langleyac61fa32014-06-23 12:03:11 -07001137 msg = "bad error (wanted '" + test.expectedError + "' / '" + test.expectedLocalError + "')"
Adam Langley95c29f32014-06-20 12:00:00 -07001138 default:
1139 panic("internal error")
1140 }
1141
1142 return fmt.Errorf("%s: local error '%s', child error '%s', stdout:\n%s\nstderr:\n%s", msg, localError, childError, string(stdoutBuf.Bytes()), stderr)
1143 }
1144
1145 if !*useValgrind && len(stderr) > 0 {
1146 println(stderr)
1147 }
1148
1149 return nil
1150}
1151
1152var tlsVersions = []struct {
1153 name string
1154 version uint16
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001155 flag string
David Benjamin8b8c0062014-11-23 02:47:52 -05001156 hasDTLS bool
Adam Langley95c29f32014-06-20 12:00:00 -07001157}{
David Benjamin8b8c0062014-11-23 02:47:52 -05001158 {"SSL3", VersionSSL30, "-no-ssl3", false},
1159 {"TLS1", VersionTLS10, "-no-tls1", true},
1160 {"TLS11", VersionTLS11, "-no-tls11", false},
1161 {"TLS12", VersionTLS12, "-no-tls12", true},
Adam Langley95c29f32014-06-20 12:00:00 -07001162}
1163
1164var testCipherSuites = []struct {
1165 name string
1166 id uint16
1167}{
1168 {"3DES-SHA", TLS_RSA_WITH_3DES_EDE_CBC_SHA},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001169 {"AES128-GCM", TLS_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001170 {"AES128-SHA", TLS_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001171 {"AES128-SHA256", TLS_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001172 {"AES256-GCM", TLS_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001173 {"AES256-SHA", TLS_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001174 {"AES256-SHA256", TLS_RSA_WITH_AES_256_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001175 {"DHE-RSA-AES128-GCM", TLS_DHE_RSA_WITH_AES_128_GCM_SHA256},
1176 {"DHE-RSA-AES128-SHA", TLS_DHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001177 {"DHE-RSA-AES128-SHA256", TLS_DHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001178 {"DHE-RSA-AES256-GCM", TLS_DHE_RSA_WITH_AES_256_GCM_SHA384},
1179 {"DHE-RSA-AES256-SHA", TLS_DHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001180 {"DHE-RSA-AES256-SHA256", TLS_DHE_RSA_WITH_AES_256_CBC_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001181 {"ECDHE-ECDSA-AES128-GCM", TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
1182 {"ECDHE-ECDSA-AES128-SHA", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001183 {"ECDHE-ECDSA-AES128-SHA256", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256},
1184 {"ECDHE-ECDSA-AES256-GCM", TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001185 {"ECDHE-ECDSA-AES256-SHA", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001186 {"ECDHE-ECDSA-AES256-SHA384", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001187 {"ECDHE-ECDSA-RC4-SHA", TLS_ECDHE_ECDSA_WITH_RC4_128_SHA},
David Benjamin2af684f2014-10-27 02:23:15 -04001188 {"ECDHE-PSK-WITH-AES-128-GCM-SHA256", TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001189 {"ECDHE-RSA-AES128-GCM", TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001190 {"ECDHE-RSA-AES128-SHA", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001191 {"ECDHE-RSA-AES128-SHA256", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001192 {"ECDHE-RSA-AES256-GCM", TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001193 {"ECDHE-RSA-AES256-SHA", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001194 {"ECDHE-RSA-AES256-SHA384", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001195 {"ECDHE-RSA-RC4-SHA", TLS_ECDHE_RSA_WITH_RC4_128_SHA},
David Benjamin48cae082014-10-27 01:06:24 -04001196 {"PSK-AES128-CBC-SHA", TLS_PSK_WITH_AES_128_CBC_SHA},
1197 {"PSK-AES256-CBC-SHA", TLS_PSK_WITH_AES_256_CBC_SHA},
1198 {"PSK-RC4-SHA", TLS_PSK_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -07001199 {"RC4-MD5", TLS_RSA_WITH_RC4_128_MD5},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001200 {"RC4-SHA", TLS_RSA_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -07001201}
1202
David Benjamin8b8c0062014-11-23 02:47:52 -05001203func hasComponent(suiteName, component string) bool {
1204 return strings.Contains("-"+suiteName+"-", "-"+component+"-")
1205}
1206
David Benjaminf7768e42014-08-31 02:06:47 -04001207func isTLS12Only(suiteName string) bool {
David Benjamin8b8c0062014-11-23 02:47:52 -05001208 return hasComponent(suiteName, "GCM") ||
1209 hasComponent(suiteName, "SHA256") ||
1210 hasComponent(suiteName, "SHA384")
1211}
1212
1213func isDTLSCipher(suiteName string) bool {
David Benjamine95d20d2014-12-23 11:16:01 -05001214 return !hasComponent(suiteName, "RC4")
David Benjaminf7768e42014-08-31 02:06:47 -04001215}
1216
Adam Langley95c29f32014-06-20 12:00:00 -07001217func addCipherSuiteTests() {
1218 for _, suite := range testCipherSuites {
David Benjamin48cae082014-10-27 01:06:24 -04001219 const psk = "12345"
1220 const pskIdentity = "luggage combo"
1221
Adam Langley95c29f32014-06-20 12:00:00 -07001222 var cert Certificate
David Benjamin025b3d32014-07-01 19:53:04 -04001223 var certFile string
1224 var keyFile string
David Benjamin8b8c0062014-11-23 02:47:52 -05001225 if hasComponent(suite.name, "ECDSA") {
Adam Langley95c29f32014-06-20 12:00:00 -07001226 cert = getECDSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -04001227 certFile = ecdsaCertificateFile
1228 keyFile = ecdsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -07001229 } else {
1230 cert = getRSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -04001231 certFile = rsaCertificateFile
1232 keyFile = rsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -07001233 }
1234
David Benjamin48cae082014-10-27 01:06:24 -04001235 var flags []string
David Benjamin8b8c0062014-11-23 02:47:52 -05001236 if hasComponent(suite.name, "PSK") {
David Benjamin48cae082014-10-27 01:06:24 -04001237 flags = append(flags,
1238 "-psk", psk,
1239 "-psk-identity", pskIdentity)
1240 }
1241
Adam Langley95c29f32014-06-20 12:00:00 -07001242 for _, ver := range tlsVersions {
David Benjaminf7768e42014-08-31 02:06:47 -04001243 if ver.version < VersionTLS12 && isTLS12Only(suite.name) {
Adam Langley95c29f32014-06-20 12:00:00 -07001244 continue
1245 }
1246
David Benjamin025b3d32014-07-01 19:53:04 -04001247 testCases = append(testCases, testCase{
1248 testType: clientTest,
1249 name: ver.name + "-" + suite.name + "-client",
Adam Langley95c29f32014-06-20 12:00:00 -07001250 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001251 MinVersion: ver.version,
1252 MaxVersion: ver.version,
1253 CipherSuites: []uint16{suite.id},
1254 Certificates: []Certificate{cert},
1255 PreSharedKey: []byte(psk),
1256 PreSharedKeyIdentity: pskIdentity,
Adam Langley95c29f32014-06-20 12:00:00 -07001257 },
David Benjamin48cae082014-10-27 01:06:24 -04001258 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001259 resumeSession: true,
Adam Langley95c29f32014-06-20 12:00:00 -07001260 })
David Benjamin025b3d32014-07-01 19:53:04 -04001261
David Benjamin76d8abe2014-08-14 16:25:34 -04001262 testCases = append(testCases, testCase{
1263 testType: serverTest,
1264 name: ver.name + "-" + suite.name + "-server",
1265 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001266 MinVersion: ver.version,
1267 MaxVersion: ver.version,
1268 CipherSuites: []uint16{suite.id},
1269 Certificates: []Certificate{cert},
1270 PreSharedKey: []byte(psk),
1271 PreSharedKeyIdentity: pskIdentity,
David Benjamin76d8abe2014-08-14 16:25:34 -04001272 },
1273 certFile: certFile,
1274 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001275 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001276 resumeSession: true,
David Benjamin76d8abe2014-08-14 16:25:34 -04001277 })
David Benjamin6fd297b2014-08-11 18:43:38 -04001278
David Benjamin8b8c0062014-11-23 02:47:52 -05001279 if ver.hasDTLS && isDTLSCipher(suite.name) {
David Benjamin6fd297b2014-08-11 18:43:38 -04001280 testCases = append(testCases, testCase{
1281 testType: clientTest,
1282 protocol: dtls,
1283 name: "D" + ver.name + "-" + suite.name + "-client",
1284 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001285 MinVersion: ver.version,
1286 MaxVersion: ver.version,
1287 CipherSuites: []uint16{suite.id},
1288 Certificates: []Certificate{cert},
1289 PreSharedKey: []byte(psk),
1290 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001291 },
David Benjamin48cae082014-10-27 01:06:24 -04001292 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001293 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001294 })
1295 testCases = append(testCases, testCase{
1296 testType: serverTest,
1297 protocol: dtls,
1298 name: "D" + ver.name + "-" + suite.name + "-server",
1299 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001300 MinVersion: ver.version,
1301 MaxVersion: ver.version,
1302 CipherSuites: []uint16{suite.id},
1303 Certificates: []Certificate{cert},
1304 PreSharedKey: []byte(psk),
1305 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001306 },
1307 certFile: certFile,
1308 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001309 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001310 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001311 })
1312 }
Adam Langley95c29f32014-06-20 12:00:00 -07001313 }
1314 }
1315}
1316
1317func addBadECDSASignatureTests() {
1318 for badR := BadValue(1); badR < NumBadValues; badR++ {
1319 for badS := BadValue(1); badS < NumBadValues; badS++ {
David Benjamin025b3d32014-07-01 19:53:04 -04001320 testCases = append(testCases, testCase{
Adam Langley95c29f32014-06-20 12:00:00 -07001321 name: fmt.Sprintf("BadECDSA-%d-%d", badR, badS),
1322 config: Config{
1323 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
1324 Certificates: []Certificate{getECDSACertificate()},
1325 Bugs: ProtocolBugs{
1326 BadECDSAR: badR,
1327 BadECDSAS: badS,
1328 },
1329 },
1330 shouldFail: true,
1331 expectedError: "SIGNATURE",
1332 })
1333 }
1334 }
1335}
1336
Adam Langley80842bd2014-06-20 12:00:00 -07001337func addCBCPaddingTests() {
David Benjamin025b3d32014-07-01 19:53:04 -04001338 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001339 name: "MaxCBCPadding",
1340 config: Config{
1341 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1342 Bugs: ProtocolBugs{
1343 MaxPadding: true,
1344 },
1345 },
1346 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1347 })
David Benjamin025b3d32014-07-01 19:53:04 -04001348 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001349 name: "BadCBCPadding",
1350 config: Config{
1351 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1352 Bugs: ProtocolBugs{
1353 PaddingFirstByteBad: true,
1354 },
1355 },
1356 shouldFail: true,
1357 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1358 })
1359 // OpenSSL previously had an issue where the first byte of padding in
1360 // 255 bytes of padding wasn't checked.
David Benjamin025b3d32014-07-01 19:53:04 -04001361 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001362 name: "BadCBCPadding255",
1363 config: Config{
1364 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1365 Bugs: ProtocolBugs{
1366 MaxPadding: true,
1367 PaddingFirstByteBadIf255: true,
1368 },
1369 },
1370 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1371 shouldFail: true,
1372 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1373 })
1374}
1375
Kenny Root7fdeaf12014-08-05 15:23:37 -07001376func addCBCSplittingTests() {
1377 testCases = append(testCases, testCase{
1378 name: "CBCRecordSplitting",
1379 config: Config{
1380 MaxVersion: VersionTLS10,
1381 MinVersion: VersionTLS10,
1382 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1383 },
1384 messageLen: -1, // read until EOF
1385 flags: []string{
1386 "-async",
1387 "-write-different-record-sizes",
1388 "-cbc-record-splitting",
1389 },
David Benjamina8e3e0e2014-08-06 22:11:10 -04001390 })
1391 testCases = append(testCases, testCase{
Kenny Root7fdeaf12014-08-05 15:23:37 -07001392 name: "CBCRecordSplittingPartialWrite",
1393 config: Config{
1394 MaxVersion: VersionTLS10,
1395 MinVersion: VersionTLS10,
1396 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1397 },
1398 messageLen: -1, // read until EOF
1399 flags: []string{
1400 "-async",
1401 "-write-different-record-sizes",
1402 "-cbc-record-splitting",
1403 "-partial-write",
1404 },
1405 })
1406}
1407
David Benjamin636293b2014-07-08 17:59:18 -04001408func addClientAuthTests() {
David Benjamin407a10c2014-07-16 12:58:59 -04001409 // Add a dummy cert pool to stress certificate authority parsing.
1410 // TODO(davidben): Add tests that those values parse out correctly.
1411 certPool := x509.NewCertPool()
1412 cert, err := x509.ParseCertificate(rsaCertificate.Certificate[0])
1413 if err != nil {
1414 panic(err)
1415 }
1416 certPool.AddCert(cert)
1417
David Benjamin636293b2014-07-08 17:59:18 -04001418 for _, ver := range tlsVersions {
David Benjamin636293b2014-07-08 17:59:18 -04001419 testCases = append(testCases, testCase{
1420 testType: clientTest,
David Benjamin67666e72014-07-12 15:47:52 -04001421 name: ver.name + "-Client-ClientAuth-RSA",
David Benjamin636293b2014-07-08 17:59:18 -04001422 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001423 MinVersion: ver.version,
1424 MaxVersion: ver.version,
1425 ClientAuth: RequireAnyClientCert,
1426 ClientCAs: certPool,
David Benjamin636293b2014-07-08 17:59:18 -04001427 },
1428 flags: []string{
1429 "-cert-file", rsaCertificateFile,
1430 "-key-file", rsaKeyFile,
1431 },
1432 })
1433 testCases = append(testCases, testCase{
David Benjamin67666e72014-07-12 15:47:52 -04001434 testType: serverTest,
1435 name: ver.name + "-Server-ClientAuth-RSA",
1436 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001437 MinVersion: ver.version,
1438 MaxVersion: ver.version,
David Benjamin67666e72014-07-12 15:47:52 -04001439 Certificates: []Certificate{rsaCertificate},
1440 },
1441 flags: []string{"-require-any-client-certificate"},
1442 })
David Benjamine098ec22014-08-27 23:13:20 -04001443 if ver.version != VersionSSL30 {
1444 testCases = append(testCases, testCase{
1445 testType: serverTest,
1446 name: ver.name + "-Server-ClientAuth-ECDSA",
1447 config: Config{
1448 MinVersion: ver.version,
1449 MaxVersion: ver.version,
1450 Certificates: []Certificate{ecdsaCertificate},
1451 },
1452 flags: []string{"-require-any-client-certificate"},
1453 })
1454 testCases = append(testCases, testCase{
1455 testType: clientTest,
1456 name: ver.name + "-Client-ClientAuth-ECDSA",
1457 config: Config{
1458 MinVersion: ver.version,
1459 MaxVersion: ver.version,
1460 ClientAuth: RequireAnyClientCert,
1461 ClientCAs: certPool,
1462 },
1463 flags: []string{
1464 "-cert-file", ecdsaCertificateFile,
1465 "-key-file", ecdsaKeyFile,
1466 },
1467 })
1468 }
David Benjamin636293b2014-07-08 17:59:18 -04001469 }
1470}
1471
Adam Langley75712922014-10-10 16:23:43 -07001472func addExtendedMasterSecretTests() {
1473 const expectEMSFlag = "-expect-extended-master-secret"
1474
1475 for _, with := range []bool{false, true} {
1476 prefix := "No"
1477 var flags []string
1478 if with {
1479 prefix = ""
1480 flags = []string{expectEMSFlag}
1481 }
1482
1483 for _, isClient := range []bool{false, true} {
1484 suffix := "-Server"
1485 testType := serverTest
1486 if isClient {
1487 suffix = "-Client"
1488 testType = clientTest
1489 }
1490
1491 for _, ver := range tlsVersions {
1492 test := testCase{
1493 testType: testType,
1494 name: prefix + "ExtendedMasterSecret-" + ver.name + suffix,
1495 config: Config{
1496 MinVersion: ver.version,
1497 MaxVersion: ver.version,
1498 Bugs: ProtocolBugs{
1499 NoExtendedMasterSecret: !with,
1500 RequireExtendedMasterSecret: with,
1501 },
1502 },
David Benjamin48cae082014-10-27 01:06:24 -04001503 flags: flags,
1504 shouldFail: ver.version == VersionSSL30 && with,
Adam Langley75712922014-10-10 16:23:43 -07001505 }
1506 if test.shouldFail {
1507 test.expectedLocalError = "extended master secret required but not supported by peer"
1508 }
1509 testCases = append(testCases, test)
1510 }
1511 }
1512 }
1513
1514 // When a session is resumed, it should still be aware that its master
1515 // secret was generated via EMS and thus it's safe to use tls-unique.
1516 testCases = append(testCases, testCase{
1517 name: "ExtendedMasterSecret-Resume",
1518 config: Config{
1519 Bugs: ProtocolBugs{
1520 RequireExtendedMasterSecret: true,
1521 },
1522 },
1523 flags: []string{expectEMSFlag},
1524 resumeSession: true,
1525 })
1526}
1527
David Benjamin43ec06f2014-08-05 02:28:57 -04001528// Adds tests that try to cover the range of the handshake state machine, under
1529// various conditions. Some of these are redundant with other tests, but they
1530// only cover the synchronous case.
David Benjamin6fd297b2014-08-11 18:43:38 -04001531func addStateMachineCoverageTests(async, splitHandshake bool, protocol protocol) {
David Benjamin43ec06f2014-08-05 02:28:57 -04001532 var suffix string
1533 var flags []string
1534 var maxHandshakeRecordLength int
David Benjamin6fd297b2014-08-11 18:43:38 -04001535 if protocol == dtls {
1536 suffix = "-DTLS"
1537 }
David Benjamin43ec06f2014-08-05 02:28:57 -04001538 if async {
David Benjamin6fd297b2014-08-11 18:43:38 -04001539 suffix += "-Async"
David Benjamin43ec06f2014-08-05 02:28:57 -04001540 flags = append(flags, "-async")
1541 } else {
David Benjamin6fd297b2014-08-11 18:43:38 -04001542 suffix += "-Sync"
David Benjamin43ec06f2014-08-05 02:28:57 -04001543 }
1544 if splitHandshake {
1545 suffix += "-SplitHandshakeRecords"
David Benjamin98214542014-08-07 18:02:39 -04001546 maxHandshakeRecordLength = 1
David Benjamin43ec06f2014-08-05 02:28:57 -04001547 }
1548
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001549 // Basic handshake, with resumption. Client and server,
1550 // session ID and session ticket.
David Benjamin43ec06f2014-08-05 02:28:57 -04001551 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001552 protocol: protocol,
1553 name: "Basic-Client" + suffix,
David Benjamin43ec06f2014-08-05 02:28:57 -04001554 config: Config{
1555 Bugs: ProtocolBugs{
1556 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1557 },
1558 },
David Benjaminbed9aae2014-08-07 19:13:38 -04001559 flags: flags,
1560 resumeSession: true,
1561 })
1562 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001563 protocol: protocol,
1564 name: "Basic-Client-RenewTicket" + suffix,
David Benjaminbed9aae2014-08-07 19:13:38 -04001565 config: Config{
1566 Bugs: ProtocolBugs{
1567 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1568 RenewTicketOnResume: true,
1569 },
1570 },
1571 flags: flags,
1572 resumeSession: true,
David Benjamin43ec06f2014-08-05 02:28:57 -04001573 })
1574 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001575 protocol: protocol,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001576 name: "Basic-Client-NoTicket" + suffix,
1577 config: Config{
1578 SessionTicketsDisabled: true,
1579 Bugs: ProtocolBugs{
1580 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1581 },
1582 },
1583 flags: flags,
1584 resumeSession: true,
1585 })
1586 testCases = append(testCases, testCase{
1587 protocol: protocol,
David Benjamine0e7d0d2015-02-08 19:33:25 -05001588 name: "Basic-Client-Implicit" + suffix,
1589 config: Config{
1590 Bugs: ProtocolBugs{
1591 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1592 },
1593 },
1594 flags: append(flags, "-implicit-handshake"),
1595 resumeSession: true,
1596 })
1597 testCases = append(testCases, testCase{
1598 protocol: protocol,
David Benjamin43ec06f2014-08-05 02:28:57 -04001599 testType: serverTest,
1600 name: "Basic-Server" + suffix,
1601 config: Config{
1602 Bugs: ProtocolBugs{
1603 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1604 },
1605 },
David Benjaminbed9aae2014-08-07 19:13:38 -04001606 flags: flags,
1607 resumeSession: true,
David Benjamin43ec06f2014-08-05 02:28:57 -04001608 })
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001609 testCases = append(testCases, testCase{
1610 protocol: protocol,
1611 testType: serverTest,
1612 name: "Basic-Server-NoTickets" + suffix,
1613 config: Config{
1614 SessionTicketsDisabled: true,
1615 Bugs: ProtocolBugs{
1616 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1617 },
1618 },
1619 flags: flags,
1620 resumeSession: true,
1621 })
David Benjamine0e7d0d2015-02-08 19:33:25 -05001622 testCases = append(testCases, testCase{
1623 protocol: protocol,
1624 testType: serverTest,
1625 name: "Basic-Server-Implicit" + suffix,
1626 config: Config{
1627 Bugs: ProtocolBugs{
1628 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1629 },
1630 },
1631 flags: append(flags, "-implicit-handshake"),
1632 resumeSession: true,
1633 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001634
David Benjamin6fd297b2014-08-11 18:43:38 -04001635 // TLS client auth.
1636 testCases = append(testCases, testCase{
1637 protocol: protocol,
1638 testType: clientTest,
1639 name: "ClientAuth-Client" + suffix,
1640 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001641 ClientAuth: RequireAnyClientCert,
David Benjamin6fd297b2014-08-11 18:43:38 -04001642 Bugs: ProtocolBugs{
1643 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1644 },
1645 },
1646 flags: append(flags,
1647 "-cert-file", rsaCertificateFile,
1648 "-key-file", rsaKeyFile),
1649 })
1650 testCases = append(testCases, testCase{
1651 protocol: protocol,
1652 testType: serverTest,
1653 name: "ClientAuth-Server" + suffix,
1654 config: Config{
1655 Certificates: []Certificate{rsaCertificate},
1656 },
1657 flags: append(flags, "-require-any-client-certificate"),
1658 })
1659
David Benjamin43ec06f2014-08-05 02:28:57 -04001660 // No session ticket support; server doesn't send NewSessionTicket.
1661 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001662 protocol: protocol,
1663 name: "SessionTicketsDisabled-Client" + suffix,
David Benjamin43ec06f2014-08-05 02:28:57 -04001664 config: Config{
1665 SessionTicketsDisabled: true,
1666 Bugs: ProtocolBugs{
1667 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1668 },
1669 },
1670 flags: flags,
1671 })
1672 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001673 protocol: protocol,
David Benjamin43ec06f2014-08-05 02:28:57 -04001674 testType: serverTest,
1675 name: "SessionTicketsDisabled-Server" + suffix,
1676 config: Config{
1677 SessionTicketsDisabled: true,
1678 Bugs: ProtocolBugs{
1679 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1680 },
1681 },
1682 flags: flags,
1683 })
1684
David Benjamin48cae082014-10-27 01:06:24 -04001685 // Skip ServerKeyExchange in PSK key exchange if there's no
1686 // identity hint.
1687 testCases = append(testCases, testCase{
1688 protocol: protocol,
1689 name: "EmptyPSKHint-Client" + suffix,
1690 config: Config{
1691 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
1692 PreSharedKey: []byte("secret"),
1693 Bugs: ProtocolBugs{
1694 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1695 },
1696 },
1697 flags: append(flags, "-psk", "secret"),
1698 })
1699 testCases = append(testCases, testCase{
1700 protocol: protocol,
1701 testType: serverTest,
1702 name: "EmptyPSKHint-Server" + suffix,
1703 config: Config{
1704 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
1705 PreSharedKey: []byte("secret"),
1706 Bugs: ProtocolBugs{
1707 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1708 },
1709 },
1710 flags: append(flags, "-psk", "secret"),
1711 })
1712
David Benjamin6fd297b2014-08-11 18:43:38 -04001713 if protocol == tls {
1714 // NPN on client and server; results in post-handshake message.
1715 testCases = append(testCases, testCase{
1716 protocol: protocol,
1717 name: "NPN-Client" + suffix,
1718 config: Config{
David Benjaminae2888f2014-09-06 12:58:58 -04001719 NextProtos: []string{"foo"},
David Benjamin6fd297b2014-08-11 18:43:38 -04001720 Bugs: ProtocolBugs{
1721 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1722 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001723 },
David Benjaminfc7b0862014-09-06 13:21:53 -04001724 flags: append(flags, "-select-next-proto", "foo"),
1725 expectedNextProto: "foo",
1726 expectedNextProtoType: npn,
David Benjamin6fd297b2014-08-11 18:43:38 -04001727 })
1728 testCases = append(testCases, testCase{
1729 protocol: protocol,
1730 testType: serverTest,
1731 name: "NPN-Server" + suffix,
1732 config: Config{
1733 NextProtos: []string{"bar"},
1734 Bugs: ProtocolBugs{
1735 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1736 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001737 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001738 flags: append(flags,
1739 "-advertise-npn", "\x03foo\x03bar\x03baz",
1740 "-expect-next-proto", "bar"),
David Benjaminfc7b0862014-09-06 13:21:53 -04001741 expectedNextProto: "bar",
1742 expectedNextProtoType: npn,
David Benjamin6fd297b2014-08-11 18:43:38 -04001743 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001744
David Benjamin195dc782015-02-19 13:27:05 -05001745 // TODO(davidben): Add tests for when False Start doesn't trigger.
1746
David Benjamin6fd297b2014-08-11 18:43:38 -04001747 // Client does False Start and negotiates NPN.
1748 testCases = append(testCases, testCase{
1749 protocol: protocol,
1750 name: "FalseStart" + suffix,
1751 config: Config{
1752 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1753 NextProtos: []string{"foo"},
1754 Bugs: ProtocolBugs{
David Benjamine58c4f52014-08-24 03:47:07 -04001755 ExpectFalseStart: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001756 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1757 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001758 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001759 flags: append(flags,
1760 "-false-start",
1761 "-select-next-proto", "foo"),
David Benjamine58c4f52014-08-24 03:47:07 -04001762 shimWritesFirst: true,
1763 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001764 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001765
David Benjaminae2888f2014-09-06 12:58:58 -04001766 // Client does False Start and negotiates ALPN.
1767 testCases = append(testCases, testCase{
1768 protocol: protocol,
1769 name: "FalseStart-ALPN" + suffix,
1770 config: Config{
1771 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1772 NextProtos: []string{"foo"},
1773 Bugs: ProtocolBugs{
1774 ExpectFalseStart: true,
1775 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1776 },
1777 },
1778 flags: append(flags,
1779 "-false-start",
1780 "-advertise-alpn", "\x03foo"),
1781 shimWritesFirst: true,
1782 resumeSession: true,
1783 })
1784
David Benjamin931ab342015-02-08 19:46:57 -05001785 // Client does False Start but doesn't explicitly call
1786 // SSL_connect.
1787 testCases = append(testCases, testCase{
1788 protocol: protocol,
1789 name: "FalseStart-Implicit" + suffix,
1790 config: Config{
1791 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1792 NextProtos: []string{"foo"},
1793 Bugs: ProtocolBugs{
1794 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1795 },
1796 },
1797 flags: append(flags,
1798 "-implicit-handshake",
1799 "-false-start",
1800 "-advertise-alpn", "\x03foo"),
1801 })
1802
David Benjamin6fd297b2014-08-11 18:43:38 -04001803 // False Start without session tickets.
1804 testCases = append(testCases, testCase{
David Benjamin5f237bc2015-02-11 17:14:15 -05001805 name: "FalseStart-SessionTicketsDisabled" + suffix,
David Benjamin6fd297b2014-08-11 18:43:38 -04001806 config: Config{
1807 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1808 NextProtos: []string{"foo"},
1809 SessionTicketsDisabled: true,
David Benjamin4e99c522014-08-24 01:45:30 -04001810 Bugs: ProtocolBugs{
David Benjamine58c4f52014-08-24 03:47:07 -04001811 ExpectFalseStart: true,
David Benjamin4e99c522014-08-24 01:45:30 -04001812 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1813 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001814 },
David Benjamin4e99c522014-08-24 01:45:30 -04001815 flags: append(flags,
David Benjamin6fd297b2014-08-11 18:43:38 -04001816 "-false-start",
1817 "-select-next-proto", "foo",
David Benjamin4e99c522014-08-24 01:45:30 -04001818 ),
David Benjamine58c4f52014-08-24 03:47:07 -04001819 shimWritesFirst: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001820 })
David Benjamin1e7f8d72014-08-08 12:27:04 -04001821
David Benjamina08e49d2014-08-24 01:46:07 -04001822 // Server parses a V2ClientHello.
David Benjamin6fd297b2014-08-11 18:43:38 -04001823 testCases = append(testCases, testCase{
1824 protocol: protocol,
1825 testType: serverTest,
1826 name: "SendV2ClientHello" + suffix,
1827 config: Config{
1828 // Choose a cipher suite that does not involve
1829 // elliptic curves, so no extensions are
1830 // involved.
1831 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
1832 Bugs: ProtocolBugs{
1833 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1834 SendV2ClientHello: true,
1835 },
David Benjamin1e7f8d72014-08-08 12:27:04 -04001836 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001837 flags: flags,
1838 })
David Benjamina08e49d2014-08-24 01:46:07 -04001839
1840 // Client sends a Channel ID.
1841 testCases = append(testCases, testCase{
1842 protocol: protocol,
1843 name: "ChannelID-Client" + suffix,
1844 config: Config{
1845 RequestChannelID: true,
1846 Bugs: ProtocolBugs{
1847 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1848 },
1849 },
1850 flags: append(flags,
1851 "-send-channel-id", channelIDKeyFile,
1852 ),
1853 resumeSession: true,
1854 expectChannelID: true,
1855 })
1856
1857 // Server accepts a Channel ID.
1858 testCases = append(testCases, testCase{
1859 protocol: protocol,
1860 testType: serverTest,
1861 name: "ChannelID-Server" + suffix,
1862 config: Config{
1863 ChannelID: channelIDKey,
1864 Bugs: ProtocolBugs{
1865 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1866 },
1867 },
1868 flags: append(flags,
1869 "-expect-channel-id",
1870 base64.StdEncoding.EncodeToString(channelIDBytes),
1871 ),
1872 resumeSession: true,
1873 expectChannelID: true,
1874 })
David Benjamin6fd297b2014-08-11 18:43:38 -04001875 } else {
1876 testCases = append(testCases, testCase{
1877 protocol: protocol,
1878 name: "SkipHelloVerifyRequest" + suffix,
1879 config: Config{
1880 Bugs: ProtocolBugs{
1881 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1882 SkipHelloVerifyRequest: true,
1883 },
1884 },
1885 flags: flags,
1886 })
David Benjamin6fd297b2014-08-11 18:43:38 -04001887 }
David Benjamin43ec06f2014-08-05 02:28:57 -04001888}
1889
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001890func addVersionNegotiationTests() {
1891 for i, shimVers := range tlsVersions {
1892 // Assemble flags to disable all newer versions on the shim.
1893 var flags []string
1894 for _, vers := range tlsVersions[i+1:] {
1895 flags = append(flags, vers.flag)
1896 }
1897
1898 for _, runnerVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05001899 protocols := []protocol{tls}
1900 if runnerVers.hasDTLS && shimVers.hasDTLS {
1901 protocols = append(protocols, dtls)
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001902 }
David Benjamin8b8c0062014-11-23 02:47:52 -05001903 for _, protocol := range protocols {
1904 expectedVersion := shimVers.version
1905 if runnerVers.version < shimVers.version {
1906 expectedVersion = runnerVers.version
1907 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001908
David Benjamin8b8c0062014-11-23 02:47:52 -05001909 suffix := shimVers.name + "-" + runnerVers.name
1910 if protocol == dtls {
1911 suffix += "-DTLS"
1912 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001913
David Benjamin1eb367c2014-12-12 18:17:51 -05001914 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
1915
David Benjamin1e29a6b2014-12-10 02:27:24 -05001916 clientVers := shimVers.version
1917 if clientVers > VersionTLS10 {
1918 clientVers = VersionTLS10
1919 }
David Benjamin8b8c0062014-11-23 02:47:52 -05001920 testCases = append(testCases, testCase{
1921 protocol: protocol,
1922 testType: clientTest,
1923 name: "VersionNegotiation-Client-" + suffix,
1924 config: Config{
1925 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05001926 Bugs: ProtocolBugs{
1927 ExpectInitialRecordVersion: clientVers,
1928 },
David Benjamin8b8c0062014-11-23 02:47:52 -05001929 },
1930 flags: flags,
1931 expectedVersion: expectedVersion,
1932 })
David Benjamin1eb367c2014-12-12 18:17:51 -05001933 testCases = append(testCases, testCase{
1934 protocol: protocol,
1935 testType: clientTest,
1936 name: "VersionNegotiation-Client2-" + suffix,
1937 config: Config{
1938 MaxVersion: runnerVers.version,
1939 Bugs: ProtocolBugs{
1940 ExpectInitialRecordVersion: clientVers,
1941 },
1942 },
1943 flags: []string{"-max-version", shimVersFlag},
1944 expectedVersion: expectedVersion,
1945 })
David Benjamin8b8c0062014-11-23 02:47:52 -05001946
1947 testCases = append(testCases, testCase{
1948 protocol: protocol,
1949 testType: serverTest,
1950 name: "VersionNegotiation-Server-" + suffix,
1951 config: Config{
1952 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05001953 Bugs: ProtocolBugs{
1954 ExpectInitialRecordVersion: expectedVersion,
1955 },
David Benjamin8b8c0062014-11-23 02:47:52 -05001956 },
1957 flags: flags,
1958 expectedVersion: expectedVersion,
1959 })
David Benjamin1eb367c2014-12-12 18:17:51 -05001960 testCases = append(testCases, testCase{
1961 protocol: protocol,
1962 testType: serverTest,
1963 name: "VersionNegotiation-Server2-" + suffix,
1964 config: Config{
1965 MaxVersion: runnerVers.version,
1966 Bugs: ProtocolBugs{
1967 ExpectInitialRecordVersion: expectedVersion,
1968 },
1969 },
1970 flags: []string{"-max-version", shimVersFlag},
1971 expectedVersion: expectedVersion,
1972 })
David Benjamin8b8c0062014-11-23 02:47:52 -05001973 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001974 }
1975 }
1976}
1977
David Benjaminaccb4542014-12-12 23:44:33 -05001978func addMinimumVersionTests() {
1979 for i, shimVers := range tlsVersions {
1980 // Assemble flags to disable all older versions on the shim.
1981 var flags []string
1982 for _, vers := range tlsVersions[:i] {
1983 flags = append(flags, vers.flag)
1984 }
1985
1986 for _, runnerVers := range tlsVersions {
1987 protocols := []protocol{tls}
1988 if runnerVers.hasDTLS && shimVers.hasDTLS {
1989 protocols = append(protocols, dtls)
1990 }
1991 for _, protocol := range protocols {
1992 suffix := shimVers.name + "-" + runnerVers.name
1993 if protocol == dtls {
1994 suffix += "-DTLS"
1995 }
1996 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
1997
David Benjaminaccb4542014-12-12 23:44:33 -05001998 var expectedVersion uint16
1999 var shouldFail bool
2000 var expectedError string
David Benjamin87909c02014-12-13 01:55:01 -05002001 var expectedLocalError string
David Benjaminaccb4542014-12-12 23:44:33 -05002002 if runnerVers.version >= shimVers.version {
2003 expectedVersion = runnerVers.version
2004 } else {
2005 shouldFail = true
2006 expectedError = ":UNSUPPORTED_PROTOCOL:"
David Benjamin87909c02014-12-13 01:55:01 -05002007 if runnerVers.version > VersionSSL30 {
2008 expectedLocalError = "remote error: protocol version not supported"
2009 } else {
2010 expectedLocalError = "remote error: handshake failure"
2011 }
David Benjaminaccb4542014-12-12 23:44:33 -05002012 }
2013
2014 testCases = append(testCases, testCase{
2015 protocol: protocol,
2016 testType: clientTest,
2017 name: "MinimumVersion-Client-" + suffix,
2018 config: Config{
2019 MaxVersion: runnerVers.version,
2020 },
David Benjamin87909c02014-12-13 01:55:01 -05002021 flags: flags,
2022 expectedVersion: expectedVersion,
2023 shouldFail: shouldFail,
2024 expectedError: expectedError,
2025 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002026 })
2027 testCases = append(testCases, testCase{
2028 protocol: protocol,
2029 testType: clientTest,
2030 name: "MinimumVersion-Client2-" + suffix,
2031 config: Config{
2032 MaxVersion: runnerVers.version,
2033 },
David Benjamin87909c02014-12-13 01:55:01 -05002034 flags: []string{"-min-version", shimVersFlag},
2035 expectedVersion: expectedVersion,
2036 shouldFail: shouldFail,
2037 expectedError: expectedError,
2038 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002039 })
2040
2041 testCases = append(testCases, testCase{
2042 protocol: protocol,
2043 testType: serverTest,
2044 name: "MinimumVersion-Server-" + suffix,
2045 config: Config{
2046 MaxVersion: runnerVers.version,
2047 },
David Benjamin87909c02014-12-13 01:55:01 -05002048 flags: flags,
2049 expectedVersion: expectedVersion,
2050 shouldFail: shouldFail,
2051 expectedError: expectedError,
2052 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002053 })
2054 testCases = append(testCases, testCase{
2055 protocol: protocol,
2056 testType: serverTest,
2057 name: "MinimumVersion-Server2-" + suffix,
2058 config: Config{
2059 MaxVersion: runnerVers.version,
2060 },
David Benjamin87909c02014-12-13 01:55:01 -05002061 flags: []string{"-min-version", shimVersFlag},
2062 expectedVersion: expectedVersion,
2063 shouldFail: shouldFail,
2064 expectedError: expectedError,
2065 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002066 })
2067 }
2068 }
2069 }
2070}
2071
David Benjamin5c24a1d2014-08-31 00:59:27 -04002072func addD5BugTests() {
2073 testCases = append(testCases, testCase{
2074 testType: serverTest,
2075 name: "D5Bug-NoQuirk-Reject",
2076 config: Config{
2077 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2078 Bugs: ProtocolBugs{
2079 SSL3RSAKeyExchange: true,
2080 },
2081 },
2082 shouldFail: true,
2083 expectedError: ":TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG:",
2084 })
2085 testCases = append(testCases, testCase{
2086 testType: serverTest,
2087 name: "D5Bug-Quirk-Normal",
2088 config: Config{
2089 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2090 },
2091 flags: []string{"-tls-d5-bug"},
2092 })
2093 testCases = append(testCases, testCase{
2094 testType: serverTest,
2095 name: "D5Bug-Quirk-Bug",
2096 config: Config{
2097 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2098 Bugs: ProtocolBugs{
2099 SSL3RSAKeyExchange: true,
2100 },
2101 },
2102 flags: []string{"-tls-d5-bug"},
2103 })
2104}
2105
David Benjamine78bfde2014-09-06 12:45:15 -04002106func addExtensionTests() {
2107 testCases = append(testCases, testCase{
2108 testType: clientTest,
2109 name: "DuplicateExtensionClient",
2110 config: Config{
2111 Bugs: ProtocolBugs{
2112 DuplicateExtension: true,
2113 },
2114 },
2115 shouldFail: true,
2116 expectedLocalError: "remote error: error decoding message",
2117 })
2118 testCases = append(testCases, testCase{
2119 testType: serverTest,
2120 name: "DuplicateExtensionServer",
2121 config: Config{
2122 Bugs: ProtocolBugs{
2123 DuplicateExtension: true,
2124 },
2125 },
2126 shouldFail: true,
2127 expectedLocalError: "remote error: error decoding message",
2128 })
2129 testCases = append(testCases, testCase{
2130 testType: clientTest,
2131 name: "ServerNameExtensionClient",
2132 config: Config{
2133 Bugs: ProtocolBugs{
2134 ExpectServerName: "example.com",
2135 },
2136 },
2137 flags: []string{"-host-name", "example.com"},
2138 })
2139 testCases = append(testCases, testCase{
2140 testType: clientTest,
David Benjamin5f237bc2015-02-11 17:14:15 -05002141 name: "ServerNameExtensionClientMismatch",
David Benjamine78bfde2014-09-06 12:45:15 -04002142 config: Config{
2143 Bugs: ProtocolBugs{
2144 ExpectServerName: "mismatch.com",
2145 },
2146 },
2147 flags: []string{"-host-name", "example.com"},
2148 shouldFail: true,
2149 expectedLocalError: "tls: unexpected server name",
2150 })
2151 testCases = append(testCases, testCase{
2152 testType: clientTest,
David Benjamin5f237bc2015-02-11 17:14:15 -05002153 name: "ServerNameExtensionClientMissing",
David Benjamine78bfde2014-09-06 12:45:15 -04002154 config: Config{
2155 Bugs: ProtocolBugs{
2156 ExpectServerName: "missing.com",
2157 },
2158 },
2159 shouldFail: true,
2160 expectedLocalError: "tls: unexpected server name",
2161 })
2162 testCases = append(testCases, testCase{
2163 testType: serverTest,
2164 name: "ServerNameExtensionServer",
2165 config: Config{
2166 ServerName: "example.com",
2167 },
2168 flags: []string{"-expect-server-name", "example.com"},
2169 resumeSession: true,
2170 })
David Benjaminae2888f2014-09-06 12:58:58 -04002171 testCases = append(testCases, testCase{
2172 testType: clientTest,
2173 name: "ALPNClient",
2174 config: Config{
2175 NextProtos: []string{"foo"},
2176 },
2177 flags: []string{
2178 "-advertise-alpn", "\x03foo\x03bar\x03baz",
2179 "-expect-alpn", "foo",
2180 },
David Benjaminfc7b0862014-09-06 13:21:53 -04002181 expectedNextProto: "foo",
2182 expectedNextProtoType: alpn,
2183 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04002184 })
2185 testCases = append(testCases, testCase{
2186 testType: serverTest,
2187 name: "ALPNServer",
2188 config: Config{
2189 NextProtos: []string{"foo", "bar", "baz"},
2190 },
2191 flags: []string{
2192 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2193 "-select-alpn", "foo",
2194 },
David Benjaminfc7b0862014-09-06 13:21:53 -04002195 expectedNextProto: "foo",
2196 expectedNextProtoType: alpn,
2197 resumeSession: true,
2198 })
2199 // Test that the server prefers ALPN over NPN.
2200 testCases = append(testCases, testCase{
2201 testType: serverTest,
2202 name: "ALPNServer-Preferred",
2203 config: Config{
2204 NextProtos: []string{"foo", "bar", "baz"},
2205 },
2206 flags: []string{
2207 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2208 "-select-alpn", "foo",
2209 "-advertise-npn", "\x03foo\x03bar\x03baz",
2210 },
2211 expectedNextProto: "foo",
2212 expectedNextProtoType: alpn,
2213 resumeSession: true,
2214 })
2215 testCases = append(testCases, testCase{
2216 testType: serverTest,
2217 name: "ALPNServer-Preferred-Swapped",
2218 config: Config{
2219 NextProtos: []string{"foo", "bar", "baz"},
2220 Bugs: ProtocolBugs{
2221 SwapNPNAndALPN: true,
2222 },
2223 },
2224 flags: []string{
2225 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2226 "-select-alpn", "foo",
2227 "-advertise-npn", "\x03foo\x03bar\x03baz",
2228 },
2229 expectedNextProto: "foo",
2230 expectedNextProtoType: alpn,
2231 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04002232 })
Adam Langley38311732014-10-16 19:04:35 -07002233 // Resume with a corrupt ticket.
2234 testCases = append(testCases, testCase{
2235 testType: serverTest,
2236 name: "CorruptTicket",
2237 config: Config{
2238 Bugs: ProtocolBugs{
2239 CorruptTicket: true,
2240 },
2241 },
2242 resumeSession: true,
2243 flags: []string{"-expect-session-miss"},
2244 })
2245 // Resume with an oversized session id.
2246 testCases = append(testCases, testCase{
2247 testType: serverTest,
2248 name: "OversizedSessionId",
2249 config: Config{
2250 Bugs: ProtocolBugs{
2251 OversizedSessionId: true,
2252 },
2253 },
2254 resumeSession: true,
Adam Langley75712922014-10-10 16:23:43 -07002255 shouldFail: true,
Adam Langley38311732014-10-16 19:04:35 -07002256 expectedError: ":DECODE_ERROR:",
2257 })
David Benjaminca6c8262014-11-15 19:06:08 -05002258 // Basic DTLS-SRTP tests. Include fake profiles to ensure they
2259 // are ignored.
2260 testCases = append(testCases, testCase{
2261 protocol: dtls,
2262 name: "SRTP-Client",
2263 config: Config{
2264 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
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 testCases = append(testCases, testCase{
2273 protocol: dtls,
2274 testType: serverTest,
2275 name: "SRTP-Server",
2276 config: Config{
2277 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
2278 },
2279 flags: []string{
2280 "-srtp-profiles",
2281 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2282 },
2283 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2284 })
2285 // Test that the MKI is ignored.
2286 testCases = append(testCases, testCase{
2287 protocol: dtls,
2288 testType: serverTest,
2289 name: "SRTP-Server-IgnoreMKI",
2290 config: Config{
2291 SRTPProtectionProfiles: []uint16{SRTP_AES128_CM_HMAC_SHA1_80},
2292 Bugs: ProtocolBugs{
2293 SRTPMasterKeyIdentifer: "bogus",
2294 },
2295 },
2296 flags: []string{
2297 "-srtp-profiles",
2298 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2299 },
2300 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2301 })
2302 // Test that SRTP isn't negotiated on the server if there were
2303 // no matching profiles.
2304 testCases = append(testCases, testCase{
2305 protocol: dtls,
2306 testType: serverTest,
2307 name: "SRTP-Server-NoMatch",
2308 config: Config{
2309 SRTPProtectionProfiles: []uint16{100, 101, 102},
2310 },
2311 flags: []string{
2312 "-srtp-profiles",
2313 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2314 },
2315 expectedSRTPProtectionProfile: 0,
2316 })
2317 // Test that the server returning an invalid SRTP profile is
2318 // flagged as an error by the client.
2319 testCases = append(testCases, testCase{
2320 protocol: dtls,
2321 name: "SRTP-Client-NoMatch",
2322 config: Config{
2323 Bugs: ProtocolBugs{
2324 SendSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_32,
2325 },
2326 },
2327 flags: []string{
2328 "-srtp-profiles",
2329 "SRTP_AES128_CM_SHA1_80",
2330 },
2331 shouldFail: true,
2332 expectedError: ":BAD_SRTP_PROTECTION_PROFILE_LIST:",
2333 })
David Benjamin61f95272014-11-25 01:55:35 -05002334 // Test OCSP stapling and SCT list.
2335 testCases = append(testCases, testCase{
2336 name: "OCSPStapling",
2337 flags: []string{
2338 "-enable-ocsp-stapling",
2339 "-expect-ocsp-response",
2340 base64.StdEncoding.EncodeToString(testOCSPResponse),
2341 },
2342 })
2343 testCases = append(testCases, testCase{
2344 name: "SignedCertificateTimestampList",
2345 flags: []string{
2346 "-enable-signed-cert-timestamps",
2347 "-expect-signed-cert-timestamps",
2348 base64.StdEncoding.EncodeToString(testSCTList),
2349 },
2350 })
David Benjamine78bfde2014-09-06 12:45:15 -04002351}
2352
David Benjamin01fe8202014-09-24 15:21:44 -04002353func addResumptionVersionTests() {
David Benjamin01fe8202014-09-24 15:21:44 -04002354 for _, sessionVers := range tlsVersions {
David Benjamin01fe8202014-09-24 15:21:44 -04002355 for _, resumeVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05002356 protocols := []protocol{tls}
2357 if sessionVers.hasDTLS && resumeVers.hasDTLS {
2358 protocols = append(protocols, dtls)
David Benjaminbdf5e722014-11-11 00:52:15 -05002359 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002360 for _, protocol := range protocols {
2361 suffix := "-" + sessionVers.name + "-" + resumeVers.name
2362 if protocol == dtls {
2363 suffix += "-DTLS"
2364 }
2365
2366 testCases = append(testCases, testCase{
2367 protocol: protocol,
2368 name: "Resume-Client" + suffix,
2369 resumeSession: true,
2370 config: Config{
2371 MaxVersion: sessionVers.version,
2372 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2373 Bugs: ProtocolBugs{
2374 AllowSessionVersionMismatch: true,
2375 },
2376 },
2377 expectedVersion: sessionVers.version,
2378 resumeConfig: &Config{
2379 MaxVersion: resumeVers.version,
2380 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2381 Bugs: ProtocolBugs{
2382 AllowSessionVersionMismatch: true,
2383 },
2384 },
2385 expectedResumeVersion: resumeVers.version,
2386 })
2387
2388 testCases = append(testCases, testCase{
2389 protocol: protocol,
2390 name: "Resume-Client-NoResume" + suffix,
2391 flags: []string{"-expect-session-miss"},
2392 resumeSession: true,
2393 config: Config{
2394 MaxVersion: sessionVers.version,
2395 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2396 },
2397 expectedVersion: sessionVers.version,
2398 resumeConfig: &Config{
2399 MaxVersion: resumeVers.version,
2400 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2401 },
2402 newSessionsOnResume: true,
2403 expectedResumeVersion: resumeVers.version,
2404 })
2405
2406 var flags []string
2407 if sessionVers.version != resumeVers.version {
2408 flags = append(flags, "-expect-session-miss")
2409 }
2410 testCases = append(testCases, testCase{
2411 protocol: protocol,
2412 testType: serverTest,
2413 name: "Resume-Server" + suffix,
2414 flags: flags,
2415 resumeSession: true,
2416 config: Config{
2417 MaxVersion: sessionVers.version,
2418 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2419 },
2420 expectedVersion: sessionVers.version,
2421 resumeConfig: &Config{
2422 MaxVersion: resumeVers.version,
2423 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2424 },
2425 expectedResumeVersion: resumeVers.version,
2426 })
2427 }
David Benjamin01fe8202014-09-24 15:21:44 -04002428 }
2429 }
2430}
2431
Adam Langley2ae77d22014-10-28 17:29:33 -07002432func addRenegotiationTests() {
2433 testCases = append(testCases, testCase{
2434 testType: serverTest,
2435 name: "Renegotiate-Server",
2436 flags: []string{"-renegotiate"},
2437 shimWritesFirst: true,
2438 })
2439 testCases = append(testCases, testCase{
2440 testType: serverTest,
2441 name: "Renegotiate-Server-EmptyExt",
2442 config: Config{
2443 Bugs: ProtocolBugs{
2444 EmptyRenegotiationInfo: true,
2445 },
2446 },
2447 flags: []string{"-renegotiate"},
2448 shimWritesFirst: true,
2449 shouldFail: true,
2450 expectedError: ":RENEGOTIATION_MISMATCH:",
2451 })
2452 testCases = append(testCases, testCase{
2453 testType: serverTest,
2454 name: "Renegotiate-Server-BadExt",
2455 config: Config{
2456 Bugs: ProtocolBugs{
2457 BadRenegotiationInfo: true,
2458 },
2459 },
2460 flags: []string{"-renegotiate"},
2461 shimWritesFirst: true,
2462 shouldFail: true,
2463 expectedError: ":RENEGOTIATION_MISMATCH:",
2464 })
David Benjaminca6554b2014-11-08 12:31:52 -05002465 testCases = append(testCases, testCase{
2466 testType: serverTest,
2467 name: "Renegotiate-Server-ClientInitiated",
2468 renegotiate: true,
2469 })
2470 testCases = append(testCases, testCase{
2471 testType: serverTest,
2472 name: "Renegotiate-Server-ClientInitiated-NoExt",
2473 renegotiate: true,
2474 config: Config{
2475 Bugs: ProtocolBugs{
2476 NoRenegotiationInfo: true,
2477 },
2478 },
2479 shouldFail: true,
2480 expectedError: ":UNSAFE_LEGACY_RENEGOTIATION_DISABLED:",
2481 })
2482 testCases = append(testCases, testCase{
2483 testType: serverTest,
2484 name: "Renegotiate-Server-ClientInitiated-NoExt-Allowed",
2485 renegotiate: true,
2486 config: Config{
2487 Bugs: ProtocolBugs{
2488 NoRenegotiationInfo: true,
2489 },
2490 },
2491 flags: []string{"-allow-unsafe-legacy-renegotiation"},
2492 })
Adam Langley2ae77d22014-10-28 17:29:33 -07002493 // TODO(agl): test the renegotiation info SCSV.
Adam Langleycf2d4f42014-10-28 19:06:14 -07002494 testCases = append(testCases, testCase{
2495 name: "Renegotiate-Client",
2496 renegotiate: true,
2497 })
2498 testCases = append(testCases, testCase{
2499 name: "Renegotiate-Client-EmptyExt",
2500 renegotiate: true,
2501 config: Config{
2502 Bugs: ProtocolBugs{
2503 EmptyRenegotiationInfo: true,
2504 },
2505 },
2506 shouldFail: true,
2507 expectedError: ":RENEGOTIATION_MISMATCH:",
2508 })
2509 testCases = append(testCases, testCase{
2510 name: "Renegotiate-Client-BadExt",
2511 renegotiate: true,
2512 config: Config{
2513 Bugs: ProtocolBugs{
2514 BadRenegotiationInfo: true,
2515 },
2516 },
2517 shouldFail: true,
2518 expectedError: ":RENEGOTIATION_MISMATCH:",
2519 })
2520 testCases = append(testCases, testCase{
2521 name: "Renegotiate-Client-SwitchCiphers",
2522 renegotiate: true,
2523 config: Config{
2524 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2525 },
2526 renegotiateCiphers: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2527 })
2528 testCases = append(testCases, testCase{
2529 name: "Renegotiate-Client-SwitchCiphers2",
2530 renegotiate: true,
2531 config: Config{
2532 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2533 },
2534 renegotiateCiphers: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2535 })
David Benjaminc44b1df2014-11-23 12:11:01 -05002536 testCases = append(testCases, testCase{
2537 name: "Renegotiate-SameClientVersion",
2538 renegotiate: true,
2539 config: Config{
2540 MaxVersion: VersionTLS10,
2541 Bugs: ProtocolBugs{
2542 RequireSameRenegoClientVersion: true,
2543 },
2544 },
2545 })
Adam Langley2ae77d22014-10-28 17:29:33 -07002546}
2547
David Benjamin5e961c12014-11-07 01:48:35 -05002548func addDTLSReplayTests() {
2549 // Test that sequence number replays are detected.
2550 testCases = append(testCases, testCase{
2551 protocol: dtls,
2552 name: "DTLS-Replay",
2553 replayWrites: true,
2554 })
2555
2556 // Test the outgoing sequence number skipping by values larger
2557 // than the retransmit window.
2558 testCases = append(testCases, testCase{
2559 protocol: dtls,
2560 name: "DTLS-Replay-LargeGaps",
2561 config: Config{
2562 Bugs: ProtocolBugs{
2563 SequenceNumberIncrement: 127,
2564 },
2565 },
2566 replayWrites: true,
2567 })
2568}
2569
Feng Lu41aa3252014-11-21 22:47:56 -08002570func addFastRadioPaddingTests() {
David Benjamin1e29a6b2014-12-10 02:27:24 -05002571 testCases = append(testCases, testCase{
2572 protocol: tls,
2573 name: "FastRadio-Padding",
Feng Lu41aa3252014-11-21 22:47:56 -08002574 config: Config{
2575 Bugs: ProtocolBugs{
2576 RequireFastradioPadding: true,
2577 },
2578 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05002579 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08002580 })
David Benjamin1e29a6b2014-12-10 02:27:24 -05002581 testCases = append(testCases, testCase{
2582 protocol: dtls,
David Benjamin5f237bc2015-02-11 17:14:15 -05002583 name: "FastRadio-Padding-DTLS",
Feng Lu41aa3252014-11-21 22:47:56 -08002584 config: Config{
2585 Bugs: ProtocolBugs{
2586 RequireFastradioPadding: true,
2587 },
2588 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05002589 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08002590 })
2591}
2592
David Benjamin000800a2014-11-14 01:43:59 -05002593var testHashes = []struct {
2594 name string
2595 id uint8
2596}{
2597 {"SHA1", hashSHA1},
2598 {"SHA224", hashSHA224},
2599 {"SHA256", hashSHA256},
2600 {"SHA384", hashSHA384},
2601 {"SHA512", hashSHA512},
2602}
2603
2604func addSigningHashTests() {
2605 // Make sure each hash works. Include some fake hashes in the list and
2606 // ensure they're ignored.
2607 for _, hash := range testHashes {
2608 testCases = append(testCases, testCase{
2609 name: "SigningHash-ClientAuth-" + hash.name,
2610 config: Config{
2611 ClientAuth: RequireAnyClientCert,
2612 SignatureAndHashes: []signatureAndHash{
2613 {signatureRSA, 42},
2614 {signatureRSA, hash.id},
2615 {signatureRSA, 255},
2616 },
2617 },
2618 flags: []string{
2619 "-cert-file", rsaCertificateFile,
2620 "-key-file", rsaKeyFile,
2621 },
2622 })
2623
2624 testCases = append(testCases, testCase{
2625 testType: serverTest,
2626 name: "SigningHash-ServerKeyExchange-Sign-" + hash.name,
2627 config: Config{
2628 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2629 SignatureAndHashes: []signatureAndHash{
2630 {signatureRSA, 42},
2631 {signatureRSA, hash.id},
2632 {signatureRSA, 255},
2633 },
2634 },
2635 })
2636 }
2637
2638 // Test that hash resolution takes the signature type into account.
2639 testCases = append(testCases, testCase{
2640 name: "SigningHash-ClientAuth-SignatureType",
2641 config: Config{
2642 ClientAuth: RequireAnyClientCert,
2643 SignatureAndHashes: []signatureAndHash{
2644 {signatureECDSA, hashSHA512},
2645 {signatureRSA, hashSHA384},
2646 {signatureECDSA, hashSHA1},
2647 },
2648 },
2649 flags: []string{
2650 "-cert-file", rsaCertificateFile,
2651 "-key-file", rsaKeyFile,
2652 },
2653 })
2654
2655 testCases = append(testCases, testCase{
2656 testType: serverTest,
2657 name: "SigningHash-ServerKeyExchange-SignatureType",
2658 config: Config{
2659 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2660 SignatureAndHashes: []signatureAndHash{
2661 {signatureECDSA, hashSHA512},
2662 {signatureRSA, hashSHA384},
2663 {signatureECDSA, hashSHA1},
2664 },
2665 },
2666 })
2667
2668 // Test that, if the list is missing, the peer falls back to SHA-1.
2669 testCases = append(testCases, testCase{
2670 name: "SigningHash-ClientAuth-Fallback",
2671 config: Config{
2672 ClientAuth: RequireAnyClientCert,
2673 SignatureAndHashes: []signatureAndHash{
2674 {signatureRSA, hashSHA1},
2675 },
2676 Bugs: ProtocolBugs{
2677 NoSignatureAndHashes: true,
2678 },
2679 },
2680 flags: []string{
2681 "-cert-file", rsaCertificateFile,
2682 "-key-file", rsaKeyFile,
2683 },
2684 })
2685
2686 testCases = append(testCases, testCase{
2687 testType: serverTest,
2688 name: "SigningHash-ServerKeyExchange-Fallback",
2689 config: Config{
2690 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2691 SignatureAndHashes: []signatureAndHash{
2692 {signatureRSA, hashSHA1},
2693 },
2694 Bugs: ProtocolBugs{
2695 NoSignatureAndHashes: true,
2696 },
2697 },
2698 })
2699}
2700
David Benjamin83f90402015-01-27 01:09:43 -05002701// timeouts is the retransmit schedule for BoringSSL. It doubles and
2702// caps at 60 seconds. On the 13th timeout, it gives up.
2703var timeouts = []time.Duration{
2704 1 * time.Second,
2705 2 * time.Second,
2706 4 * time.Second,
2707 8 * time.Second,
2708 16 * time.Second,
2709 32 * time.Second,
2710 60 * time.Second,
2711 60 * time.Second,
2712 60 * time.Second,
2713 60 * time.Second,
2714 60 * time.Second,
2715 60 * time.Second,
2716 60 * time.Second,
2717}
2718
2719func addDTLSRetransmitTests() {
2720 // Test that this is indeed the timeout schedule. Stress all
2721 // four patterns of handshake.
2722 for i := 1; i < len(timeouts); i++ {
2723 number := strconv.Itoa(i)
2724 testCases = append(testCases, testCase{
2725 protocol: dtls,
2726 name: "DTLS-Retransmit-Client-" + number,
2727 config: Config{
2728 Bugs: ProtocolBugs{
2729 TimeoutSchedule: timeouts[:i],
2730 },
2731 },
2732 resumeSession: true,
2733 flags: []string{"-async"},
2734 })
2735 testCases = append(testCases, testCase{
2736 protocol: dtls,
2737 testType: serverTest,
2738 name: "DTLS-Retransmit-Server-" + number,
2739 config: Config{
2740 Bugs: ProtocolBugs{
2741 TimeoutSchedule: timeouts[:i],
2742 },
2743 },
2744 resumeSession: true,
2745 flags: []string{"-async"},
2746 })
2747 }
2748
2749 // Test that exceeding the timeout schedule hits a read
2750 // timeout.
2751 testCases = append(testCases, testCase{
2752 protocol: dtls,
2753 name: "DTLS-Retransmit-Timeout",
2754 config: Config{
2755 Bugs: ProtocolBugs{
2756 TimeoutSchedule: timeouts,
2757 },
2758 },
2759 resumeSession: true,
2760 flags: []string{"-async"},
2761 shouldFail: true,
2762 expectedError: ":READ_TIMEOUT_EXPIRED:",
2763 })
2764
2765 // Test that timeout handling has a fudge factor, due to API
2766 // problems.
2767 testCases = append(testCases, testCase{
2768 protocol: dtls,
2769 name: "DTLS-Retransmit-Fudge",
2770 config: Config{
2771 Bugs: ProtocolBugs{
2772 TimeoutSchedule: []time.Duration{
2773 timeouts[0] - 10*time.Millisecond,
2774 },
2775 },
2776 },
2777 resumeSession: true,
2778 flags: []string{"-async"},
2779 })
2780}
2781
David Benjamin884fdf12014-08-02 15:28:23 -04002782func worker(statusChan chan statusMsg, c chan *testCase, buildDir string, wg *sync.WaitGroup) {
Adam Langley95c29f32014-06-20 12:00:00 -07002783 defer wg.Done()
2784
2785 for test := range c {
Adam Langley69a01602014-11-17 17:26:55 -08002786 var err error
2787
2788 if *mallocTest < 0 {
2789 statusChan <- statusMsg{test: test, started: true}
2790 err = runTest(test, buildDir, -1)
2791 } else {
2792 for mallocNumToFail := int64(*mallocTest); ; mallocNumToFail++ {
2793 statusChan <- statusMsg{test: test, started: true}
2794 if err = runTest(test, buildDir, mallocNumToFail); err != errMoreMallocs {
2795 if err != nil {
2796 fmt.Printf("\n\nmalloc test failed at %d: %s\n", mallocNumToFail, err)
2797 }
2798 break
2799 }
2800 }
2801 }
Adam Langley95c29f32014-06-20 12:00:00 -07002802 statusChan <- statusMsg{test: test, err: err}
2803 }
2804}
2805
2806type statusMsg struct {
2807 test *testCase
2808 started bool
2809 err error
2810}
2811
David Benjamin5f237bc2015-02-11 17:14:15 -05002812func statusPrinter(doneChan chan *testOutput, statusChan chan statusMsg, total int) {
Adam Langley95c29f32014-06-20 12:00:00 -07002813 var started, done, failed, lineLen int
Adam Langley95c29f32014-06-20 12:00:00 -07002814
David Benjamin5f237bc2015-02-11 17:14:15 -05002815 testOutput := newTestOutput()
Adam Langley95c29f32014-06-20 12:00:00 -07002816 for msg := range statusChan {
David Benjamin5f237bc2015-02-11 17:14:15 -05002817 if !*pipe {
2818 // Erase the previous status line.
2819 fmt.Printf("\x1b[%dD\x1b[K", lineLen)
2820 }
2821
Adam Langley95c29f32014-06-20 12:00:00 -07002822 if msg.started {
2823 started++
2824 } else {
2825 done++
David Benjamin5f237bc2015-02-11 17:14:15 -05002826
2827 if msg.err != nil {
2828 fmt.Printf("FAILED (%s)\n%s\n", msg.test.name, msg.err)
2829 failed++
2830 testOutput.addResult(msg.test.name, "FAIL")
2831 } else {
2832 if *pipe {
2833 // Print each test instead of a status line.
2834 fmt.Printf("PASSED (%s)\n", msg.test.name)
2835 }
2836 testOutput.addResult(msg.test.name, "PASS")
2837 }
Adam Langley95c29f32014-06-20 12:00:00 -07002838 }
2839
David Benjamin5f237bc2015-02-11 17:14:15 -05002840 if !*pipe {
2841 // Print a new status line.
2842 line := fmt.Sprintf("%d/%d/%d/%d", failed, done, started, total)
2843 lineLen = len(line)
2844 os.Stdout.WriteString(line)
Adam Langley95c29f32014-06-20 12:00:00 -07002845 }
Adam Langley95c29f32014-06-20 12:00:00 -07002846 }
David Benjamin5f237bc2015-02-11 17:14:15 -05002847
2848 doneChan <- testOutput
Adam Langley95c29f32014-06-20 12:00:00 -07002849}
2850
2851func main() {
2852 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 -04002853 var flagNumWorkers *int = flag.Int("num-workers", runtime.NumCPU(), "The number of workers to run in parallel.")
David Benjamin884fdf12014-08-02 15:28:23 -04002854 var flagBuildDir *string = flag.String("build-dir", "../../../build", "The build directory to run the shim from.")
Adam Langley95c29f32014-06-20 12:00:00 -07002855
2856 flag.Parse()
2857
2858 addCipherSuiteTests()
2859 addBadECDSASignatureTests()
Adam Langley80842bd2014-06-20 12:00:00 -07002860 addCBCPaddingTests()
Kenny Root7fdeaf12014-08-05 15:23:37 -07002861 addCBCSplittingTests()
David Benjamin636293b2014-07-08 17:59:18 -04002862 addClientAuthTests()
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002863 addVersionNegotiationTests()
David Benjaminaccb4542014-12-12 23:44:33 -05002864 addMinimumVersionTests()
David Benjamin5c24a1d2014-08-31 00:59:27 -04002865 addD5BugTests()
David Benjamine78bfde2014-09-06 12:45:15 -04002866 addExtensionTests()
David Benjamin01fe8202014-09-24 15:21:44 -04002867 addResumptionVersionTests()
Adam Langley75712922014-10-10 16:23:43 -07002868 addExtendedMasterSecretTests()
Adam Langley2ae77d22014-10-28 17:29:33 -07002869 addRenegotiationTests()
David Benjamin5e961c12014-11-07 01:48:35 -05002870 addDTLSReplayTests()
David Benjamin000800a2014-11-14 01:43:59 -05002871 addSigningHashTests()
Feng Lu41aa3252014-11-21 22:47:56 -08002872 addFastRadioPaddingTests()
David Benjamin83f90402015-01-27 01:09:43 -05002873 addDTLSRetransmitTests()
David Benjamin43ec06f2014-08-05 02:28:57 -04002874 for _, async := range []bool{false, true} {
2875 for _, splitHandshake := range []bool{false, true} {
David Benjamin6fd297b2014-08-11 18:43:38 -04002876 for _, protocol := range []protocol{tls, dtls} {
2877 addStateMachineCoverageTests(async, splitHandshake, protocol)
2878 }
David Benjamin43ec06f2014-08-05 02:28:57 -04002879 }
2880 }
Adam Langley95c29f32014-06-20 12:00:00 -07002881
2882 var wg sync.WaitGroup
2883
David Benjamin2bc8e6f2014-08-02 15:22:37 -04002884 numWorkers := *flagNumWorkers
Adam Langley95c29f32014-06-20 12:00:00 -07002885
2886 statusChan := make(chan statusMsg, numWorkers)
2887 testChan := make(chan *testCase, numWorkers)
David Benjamin5f237bc2015-02-11 17:14:15 -05002888 doneChan := make(chan *testOutput)
Adam Langley95c29f32014-06-20 12:00:00 -07002889
David Benjamin025b3d32014-07-01 19:53:04 -04002890 go statusPrinter(doneChan, statusChan, len(testCases))
Adam Langley95c29f32014-06-20 12:00:00 -07002891
2892 for i := 0; i < numWorkers; i++ {
2893 wg.Add(1)
David Benjamin884fdf12014-08-02 15:28:23 -04002894 go worker(statusChan, testChan, *flagBuildDir, &wg)
Adam Langley95c29f32014-06-20 12:00:00 -07002895 }
2896
David Benjamin025b3d32014-07-01 19:53:04 -04002897 for i := range testCases {
2898 if len(*flagTest) == 0 || *flagTest == testCases[i].name {
2899 testChan <- &testCases[i]
Adam Langley95c29f32014-06-20 12:00:00 -07002900 }
2901 }
2902
2903 close(testChan)
2904 wg.Wait()
2905 close(statusChan)
David Benjamin5f237bc2015-02-11 17:14:15 -05002906 testOutput := <-doneChan
Adam Langley95c29f32014-06-20 12:00:00 -07002907
2908 fmt.Printf("\n")
David Benjamin5f237bc2015-02-11 17:14:15 -05002909
2910 if *jsonOutput != "" {
2911 if err := testOutput.writeTo(*jsonOutput); err != nil {
2912 fmt.Fprintf(os.Stderr, "Error: %s\n", err)
2913 }
2914 }
Adam Langley95c29f32014-06-20 12:00:00 -07002915}