blob: f66993fbf863cfce2240b8df92f9a21ef5338689 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001package main
2
3import (
4 "bytes"
David Benjamina08e49d2014-08-24 01:46:07 -04005 "crypto/ecdsa"
6 "crypto/elliptic"
David Benjamin407a10c2014-07-16 12:58:59 -04007 "crypto/x509"
David Benjamin2561dc32014-08-24 01:25:27 -04008 "encoding/base64"
David Benjamina08e49d2014-08-24 01:46:07 -04009 "encoding/pem"
Adam Langley95c29f32014-06-20 12:00:00 -070010 "flag"
11 "fmt"
12 "io"
Kenny Root7fdeaf12014-08-05 15:23:37 -070013 "io/ioutil"
Adam Langley95c29f32014-06-20 12:00:00 -070014 "net"
15 "os"
16 "os/exec"
David Benjamin884fdf12014-08-02 15:28:23 -040017 "path"
David Benjamin2bc8e6f2014-08-02 15:22:37 -040018 "runtime"
Adam Langley69a01602014-11-17 17:26:55 -080019 "strconv"
Adam Langley95c29f32014-06-20 12:00:00 -070020 "strings"
21 "sync"
22 "syscall"
David Benjamin83f90402015-01-27 01:09:43 -050023 "time"
Adam Langley95c29f32014-06-20 12:00:00 -070024)
25
Adam Langley69a01602014-11-17 17:26:55 -080026var (
David Benjamin5f237bc2015-02-11 17:14:15 -050027 useValgrind = flag.Bool("valgrind", false, "If true, run code under valgrind")
28 useGDB = flag.Bool("gdb", false, "If true, run BoringSSL code under gdb")
29 flagDebug = flag.Bool("debug", false, "Hexdump the contents of the connection")
30 mallocTest = flag.Int64("malloc-test", -1, "If non-negative, run each test with each malloc in turn failing from the given number onwards.")
31 mallocTestDebug = flag.Bool("malloc-test-debug", false, "If true, ask bssl_shim to abort rather than fail a malloc. This can be used with a specific value for --malloc-test to identity the malloc failing that is causing problems.")
32 jsonOutput = flag.String("json-output", "", "The file to output JSON results to.")
33 pipe = flag.Bool("pipe", false, "If true, print status output suitable for piping into another program.")
Adam Langley69a01602014-11-17 17:26:55 -080034)
Adam Langley95c29f32014-06-20 12:00:00 -070035
David Benjamin025b3d32014-07-01 19:53:04 -040036const (
37 rsaCertificateFile = "cert.pem"
38 ecdsaCertificateFile = "ecdsa_cert.pem"
39)
40
41const (
David Benjamina08e49d2014-08-24 01:46:07 -040042 rsaKeyFile = "key.pem"
43 ecdsaKeyFile = "ecdsa_key.pem"
44 channelIDKeyFile = "channel_id_key.pem"
David Benjamin025b3d32014-07-01 19:53:04 -040045)
46
Adam Langley95c29f32014-06-20 12:00:00 -070047var rsaCertificate, ecdsaCertificate Certificate
David Benjamina08e49d2014-08-24 01:46:07 -040048var channelIDKey *ecdsa.PrivateKey
49var channelIDBytes []byte
Adam Langley95c29f32014-06-20 12:00:00 -070050
David Benjamin61f95272014-11-25 01:55:35 -050051var testOCSPResponse = []byte{1, 2, 3, 4}
52var testSCTList = []byte{5, 6, 7, 8}
53
Adam Langley95c29f32014-06-20 12:00:00 -070054func initCertificates() {
55 var err error
David Benjamin025b3d32014-07-01 19:53:04 -040056 rsaCertificate, err = LoadX509KeyPair(rsaCertificateFile, rsaKeyFile)
Adam Langley95c29f32014-06-20 12:00:00 -070057 if err != nil {
58 panic(err)
59 }
David Benjamin61f95272014-11-25 01:55:35 -050060 rsaCertificate.OCSPStaple = testOCSPResponse
61 rsaCertificate.SignedCertificateTimestampList = testSCTList
Adam Langley95c29f32014-06-20 12:00:00 -070062
David Benjamin025b3d32014-07-01 19:53:04 -040063 ecdsaCertificate, err = LoadX509KeyPair(ecdsaCertificateFile, ecdsaKeyFile)
Adam Langley95c29f32014-06-20 12:00:00 -070064 if err != nil {
65 panic(err)
66 }
David Benjamin61f95272014-11-25 01:55:35 -050067 ecdsaCertificate.OCSPStaple = testOCSPResponse
68 ecdsaCertificate.SignedCertificateTimestampList = testSCTList
David Benjamina08e49d2014-08-24 01:46:07 -040069
70 channelIDPEMBlock, err := ioutil.ReadFile(channelIDKeyFile)
71 if err != nil {
72 panic(err)
73 }
74 channelIDDERBlock, _ := pem.Decode(channelIDPEMBlock)
75 if channelIDDERBlock.Type != "EC PRIVATE KEY" {
76 panic("bad key type")
77 }
78 channelIDKey, err = x509.ParseECPrivateKey(channelIDDERBlock.Bytes)
79 if err != nil {
80 panic(err)
81 }
82 if channelIDKey.Curve != elliptic.P256() {
83 panic("bad curve")
84 }
85
86 channelIDBytes = make([]byte, 64)
87 writeIntPadded(channelIDBytes[:32], channelIDKey.X)
88 writeIntPadded(channelIDBytes[32:], channelIDKey.Y)
Adam Langley95c29f32014-06-20 12:00:00 -070089}
90
91var certificateOnce sync.Once
92
93func getRSACertificate() Certificate {
94 certificateOnce.Do(initCertificates)
95 return rsaCertificate
96}
97
98func getECDSACertificate() Certificate {
99 certificateOnce.Do(initCertificates)
100 return ecdsaCertificate
101}
102
David Benjamin025b3d32014-07-01 19:53:04 -0400103type testType int
104
105const (
106 clientTest testType = iota
107 serverTest
108)
109
David Benjamin6fd297b2014-08-11 18:43:38 -0400110type protocol int
111
112const (
113 tls protocol = iota
114 dtls
115)
116
David Benjaminfc7b0862014-09-06 13:21:53 -0400117const (
118 alpn = 1
119 npn = 2
120)
121
Adam Langley95c29f32014-06-20 12:00:00 -0700122type testCase struct {
David Benjamin025b3d32014-07-01 19:53:04 -0400123 testType testType
David Benjamin6fd297b2014-08-11 18:43:38 -0400124 protocol protocol
Adam Langley95c29f32014-06-20 12:00:00 -0700125 name string
126 config Config
127 shouldFail bool
128 expectedError string
Adam Langleyac61fa32014-06-23 12:03:11 -0700129 // expectedLocalError, if not empty, contains a substring that must be
130 // found in the local error.
131 expectedLocalError string
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400132 // expectedVersion, if non-zero, specifies the TLS version that must be
133 // negotiated.
134 expectedVersion uint16
David Benjamin01fe8202014-09-24 15:21:44 -0400135 // expectedResumeVersion, if non-zero, specifies the TLS version that
136 // must be negotiated on resumption. If zero, expectedVersion is used.
137 expectedResumeVersion uint16
David Benjamina08e49d2014-08-24 01:46:07 -0400138 // expectChannelID controls whether the connection should have
139 // negotiated a Channel ID with channelIDKey.
140 expectChannelID bool
David Benjaminae2888f2014-09-06 12:58:58 -0400141 // expectedNextProto controls whether the connection should
142 // negotiate a next protocol via NPN or ALPN.
143 expectedNextProto string
David Benjaminfc7b0862014-09-06 13:21:53 -0400144 // expectedNextProtoType, if non-zero, is the expected next
145 // protocol negotiation mechanism.
146 expectedNextProtoType int
David Benjaminca6c8262014-11-15 19:06:08 -0500147 // expectedSRTPProtectionProfile is the DTLS-SRTP profile that
148 // should be negotiated. If zero, none should be negotiated.
149 expectedSRTPProtectionProfile uint16
Adam Langley80842bd2014-06-20 12:00:00 -0700150 // messageLen is the length, in bytes, of the test message that will be
151 // sent.
152 messageLen int
David Benjamin025b3d32014-07-01 19:53:04 -0400153 // certFile is the path to the certificate to use for the server.
154 certFile string
155 // keyFile is the path to the private key to use for the server.
156 keyFile string
David Benjamin1d5c83e2014-07-22 19:20:02 -0400157 // resumeSession controls whether a second connection should be tested
David Benjamin01fe8202014-09-24 15:21:44 -0400158 // which attempts to resume the first session.
David Benjamin1d5c83e2014-07-22 19:20:02 -0400159 resumeSession bool
David Benjamin01fe8202014-09-24 15:21:44 -0400160 // resumeConfig, if not nil, points to a Config to be used on
David Benjaminfe8eb9a2014-11-17 03:19:02 -0500161 // resumption. Unless newSessionsOnResume is set,
162 // SessionTicketKey, ServerSessionCache, and
163 // ClientSessionCache are copied from the initial connection's
164 // config. If nil, the initial connection's config is used.
David Benjamin01fe8202014-09-24 15:21:44 -0400165 resumeConfig *Config
David Benjaminfe8eb9a2014-11-17 03:19:02 -0500166 // newSessionsOnResume, if true, will cause resumeConfig to
167 // use a different session resumption context.
168 newSessionsOnResume bool
David Benjamin98e882e2014-08-08 13:24:34 -0400169 // sendPrefix sends a prefix on the socket before actually performing a
170 // handshake.
171 sendPrefix string
David Benjamine58c4f52014-08-24 03:47:07 -0400172 // shimWritesFirst controls whether the shim sends an initial "hello"
173 // message before doing a roundtrip with the runner.
174 shimWritesFirst bool
Adam Langleycf2d4f42014-10-28 19:06:14 -0700175 // renegotiate indicates the the connection should be renegotiated
176 // during the exchange.
177 renegotiate bool
178 // renegotiateCiphers is a list of ciphersuite ids that will be
179 // switched in just before renegotiation.
180 renegotiateCiphers []uint16
David Benjamin5e961c12014-11-07 01:48:35 -0500181 // replayWrites, if true, configures the underlying transport
182 // to replay every write it makes in DTLS tests.
183 replayWrites bool
David Benjamin5fa3eba2015-01-22 16:35:40 -0500184 // damageFirstWrite, if true, configures the underlying transport to
185 // damage the final byte of the first application data write.
186 damageFirstWrite bool
David Benjamin325b5c32014-07-01 19:40:31 -0400187 // flags, if not empty, contains a list of command-line flags that will
188 // be passed to the shim program.
189 flags []string
Adam Langley95c29f32014-06-20 12:00:00 -0700190}
191
David Benjamin025b3d32014-07-01 19:53:04 -0400192var testCases = []testCase{
Adam Langley95c29f32014-06-20 12:00:00 -0700193 {
194 name: "BadRSASignature",
195 config: Config{
196 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
197 Bugs: ProtocolBugs{
198 InvalidSKXSignature: true,
199 },
200 },
201 shouldFail: true,
202 expectedError: ":BAD_SIGNATURE:",
203 },
204 {
205 name: "BadECDSASignature",
206 config: Config{
207 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
208 Bugs: ProtocolBugs{
209 InvalidSKXSignature: true,
210 },
211 Certificates: []Certificate{getECDSACertificate()},
212 },
213 shouldFail: true,
214 expectedError: ":BAD_SIGNATURE:",
215 },
216 {
217 name: "BadECDSACurve",
218 config: Config{
219 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
220 Bugs: ProtocolBugs{
221 InvalidSKXCurve: true,
222 },
223 Certificates: []Certificate{getECDSACertificate()},
224 },
225 shouldFail: true,
226 expectedError: ":WRONG_CURVE:",
227 },
Adam Langleyac61fa32014-06-23 12:03:11 -0700228 {
David Benjamina8e3e0e2014-08-06 22:11:10 -0400229 testType: serverTest,
230 name: "BadRSAVersion",
231 config: Config{
232 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
233 Bugs: ProtocolBugs{
234 RsaClientKeyExchangeVersion: VersionTLS11,
235 },
236 },
237 shouldFail: true,
238 expectedError: ":DECRYPTION_FAILED_OR_BAD_RECORD_MAC:",
239 },
240 {
David Benjamin325b5c32014-07-01 19:40:31 -0400241 name: "NoFallbackSCSV",
Adam Langleyac61fa32014-06-23 12:03:11 -0700242 config: Config{
243 Bugs: ProtocolBugs{
244 FailIfNotFallbackSCSV: true,
245 },
246 },
247 shouldFail: true,
248 expectedLocalError: "no fallback SCSV found",
249 },
David Benjamin325b5c32014-07-01 19:40:31 -0400250 {
David Benjamin2a0c4962014-08-22 23:46:35 -0400251 name: "SendFallbackSCSV",
David Benjamin325b5c32014-07-01 19:40:31 -0400252 config: Config{
253 Bugs: ProtocolBugs{
254 FailIfNotFallbackSCSV: true,
255 },
256 },
257 flags: []string{"-fallback-scsv"},
258 },
David Benjamin197b3ab2014-07-02 18:37:33 -0400259 {
David Benjamin7b030512014-07-08 17:30:11 -0400260 name: "ClientCertificateTypes",
261 config: Config{
262 ClientAuth: RequestClientCert,
263 ClientCertificateTypes: []byte{
264 CertTypeDSSSign,
265 CertTypeRSASign,
266 CertTypeECDSASign,
267 },
268 },
David Benjamin2561dc32014-08-24 01:25:27 -0400269 flags: []string{
270 "-expect-certificate-types",
271 base64.StdEncoding.EncodeToString([]byte{
272 CertTypeDSSSign,
273 CertTypeRSASign,
274 CertTypeECDSASign,
275 }),
276 },
David Benjamin7b030512014-07-08 17:30:11 -0400277 },
David Benjamin636293b2014-07-08 17:59:18 -0400278 {
279 name: "NoClientCertificate",
280 config: Config{
281 ClientAuth: RequireAnyClientCert,
282 },
283 shouldFail: true,
284 expectedLocalError: "client didn't provide a certificate",
285 },
David Benjamin1c375dd2014-07-12 00:48:23 -0400286 {
287 name: "UnauthenticatedECDH",
288 config: Config{
289 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
290 Bugs: ProtocolBugs{
291 UnauthenticatedECDH: true,
292 },
293 },
294 shouldFail: true,
David Benjamine8f3d662014-07-12 01:10:19 -0400295 expectedError: ":UNEXPECTED_MESSAGE:",
David Benjamin1c375dd2014-07-12 00:48:23 -0400296 },
David Benjamin9c651c92014-07-12 13:27:45 -0400297 {
298 name: "SkipServerKeyExchange",
299 config: Config{
300 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
301 Bugs: ProtocolBugs{
302 SkipServerKeyExchange: true,
303 },
304 },
305 shouldFail: true,
306 expectedError: ":UNEXPECTED_MESSAGE:",
307 },
David Benjamin1f5f62b2014-07-12 16:18:02 -0400308 {
David Benjamina0e52232014-07-19 17:39:58 -0400309 name: "SkipChangeCipherSpec-Client",
310 config: Config{
311 Bugs: ProtocolBugs{
312 SkipChangeCipherSpec: true,
313 },
314 },
315 shouldFail: true,
David Benjamin86271ee2014-07-21 16:14:03 -0400316 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
David Benjamina0e52232014-07-19 17:39:58 -0400317 },
318 {
319 testType: serverTest,
320 name: "SkipChangeCipherSpec-Server",
321 config: Config{
322 Bugs: ProtocolBugs{
323 SkipChangeCipherSpec: true,
324 },
325 },
326 shouldFail: true,
David Benjamin86271ee2014-07-21 16:14:03 -0400327 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
David Benjamina0e52232014-07-19 17:39:58 -0400328 },
David Benjamin42be6452014-07-21 14:50:23 -0400329 {
330 testType: serverTest,
331 name: "SkipChangeCipherSpec-Server-NPN",
332 config: Config{
333 NextProtos: []string{"bar"},
334 Bugs: ProtocolBugs{
335 SkipChangeCipherSpec: true,
336 },
337 },
338 flags: []string{
339 "-advertise-npn", "\x03foo\x03bar\x03baz",
340 },
341 shouldFail: true,
David Benjamin86271ee2014-07-21 16:14:03 -0400342 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
343 },
344 {
345 name: "FragmentAcrossChangeCipherSpec-Client",
346 config: Config{
347 Bugs: ProtocolBugs{
348 FragmentAcrossChangeCipherSpec: true,
349 },
350 },
351 shouldFail: true,
352 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
353 },
354 {
355 testType: serverTest,
356 name: "FragmentAcrossChangeCipherSpec-Server",
357 config: Config{
358 Bugs: ProtocolBugs{
359 FragmentAcrossChangeCipherSpec: true,
360 },
361 },
362 shouldFail: true,
363 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
364 },
365 {
366 testType: serverTest,
367 name: "FragmentAcrossChangeCipherSpec-Server-NPN",
368 config: Config{
369 NextProtos: []string{"bar"},
370 Bugs: ProtocolBugs{
371 FragmentAcrossChangeCipherSpec: true,
372 },
373 },
374 flags: []string{
375 "-advertise-npn", "\x03foo\x03bar\x03baz",
376 },
377 shouldFail: true,
378 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
David Benjamin42be6452014-07-21 14:50:23 -0400379 },
David Benjaminf3ec83d2014-07-21 22:42:34 -0400380 {
381 testType: serverTest,
David Benjamin3fd1fbd2015-02-03 16:07:32 -0500382 name: "Alert",
383 config: Config{
384 Bugs: ProtocolBugs{
385 SendSpuriousAlert: alertRecordOverflow,
386 },
387 },
388 shouldFail: true,
389 expectedError: ":TLSV1_ALERT_RECORD_OVERFLOW:",
390 },
391 {
392 protocol: dtls,
393 testType: serverTest,
394 name: "Alert-DTLS",
395 config: Config{
396 Bugs: ProtocolBugs{
397 SendSpuriousAlert: alertRecordOverflow,
398 },
399 },
400 shouldFail: true,
401 expectedError: ":TLSV1_ALERT_RECORD_OVERFLOW:",
402 },
403 {
404 testType: serverTest,
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400405 name: "FragmentAlert",
406 config: Config{
407 Bugs: ProtocolBugs{
David Benjaminca6c8262014-11-15 19:06:08 -0500408 FragmentAlert: true,
David Benjamin3fd1fbd2015-02-03 16:07:32 -0500409 SendSpuriousAlert: alertRecordOverflow,
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400410 },
411 },
412 shouldFail: true,
413 expectedError: ":BAD_ALERT:",
414 },
415 {
David Benjamin0ea8dda2015-01-31 20:33:40 -0500416 protocol: dtls,
417 testType: serverTest,
418 name: "FragmentAlert-DTLS",
419 config: Config{
420 Bugs: ProtocolBugs{
421 FragmentAlert: true,
David Benjamin3fd1fbd2015-02-03 16:07:32 -0500422 SendSpuriousAlert: alertRecordOverflow,
David Benjamin0ea8dda2015-01-31 20:33:40 -0500423 },
424 },
425 shouldFail: true,
426 expectedError: ":BAD_ALERT:",
427 },
428 {
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400429 testType: serverTest,
David Benjaminf3ec83d2014-07-21 22:42:34 -0400430 name: "EarlyChangeCipherSpec-server-1",
431 config: Config{
432 Bugs: ProtocolBugs{
433 EarlyChangeCipherSpec: 1,
434 },
435 },
436 shouldFail: true,
437 expectedError: ":CCS_RECEIVED_EARLY:",
438 },
439 {
440 testType: serverTest,
441 name: "EarlyChangeCipherSpec-server-2",
442 config: Config{
443 Bugs: ProtocolBugs{
444 EarlyChangeCipherSpec: 2,
445 },
446 },
447 shouldFail: true,
448 expectedError: ":CCS_RECEIVED_EARLY:",
449 },
David Benjamind23f4122014-07-23 15:09:48 -0400450 {
David Benjamind23f4122014-07-23 15:09:48 -0400451 name: "SkipNewSessionTicket",
452 config: Config{
453 Bugs: ProtocolBugs{
454 SkipNewSessionTicket: true,
455 },
456 },
457 shouldFail: true,
458 expectedError: ":CCS_RECEIVED_EARLY:",
459 },
David Benjamin7e3305e2014-07-28 14:52:32 -0400460 {
David Benjamind86c7672014-08-02 04:07:12 -0400461 testType: serverTest,
David Benjaminbef270a2014-08-02 04:22:02 -0400462 name: "FallbackSCSV",
463 config: Config{
464 MaxVersion: VersionTLS11,
465 Bugs: ProtocolBugs{
466 SendFallbackSCSV: true,
467 },
468 },
469 shouldFail: true,
470 expectedError: ":INAPPROPRIATE_FALLBACK:",
471 },
472 {
473 testType: serverTest,
474 name: "FallbackSCSV-VersionMatch",
475 config: Config{
476 Bugs: ProtocolBugs{
477 SendFallbackSCSV: true,
478 },
479 },
480 },
David Benjamin98214542014-08-07 18:02:39 -0400481 {
482 testType: serverTest,
483 name: "FragmentedClientVersion",
484 config: Config{
485 Bugs: ProtocolBugs{
486 MaxHandshakeRecordLength: 1,
487 FragmentClientVersion: true,
488 },
489 },
David Benjamin82c9e902014-12-12 15:55:27 -0500490 expectedVersion: VersionTLS12,
David Benjamin98214542014-08-07 18:02:39 -0400491 },
David Benjamin98e882e2014-08-08 13:24:34 -0400492 {
493 testType: serverTest,
494 name: "MinorVersionTolerance",
495 config: Config{
496 Bugs: ProtocolBugs{
497 SendClientVersion: 0x03ff,
498 },
499 },
500 expectedVersion: VersionTLS12,
501 },
502 {
503 testType: serverTest,
504 name: "MajorVersionTolerance",
505 config: Config{
506 Bugs: ProtocolBugs{
507 SendClientVersion: 0x0400,
508 },
509 },
510 expectedVersion: VersionTLS12,
511 },
512 {
513 testType: serverTest,
514 name: "VersionTooLow",
515 config: Config{
516 Bugs: ProtocolBugs{
517 SendClientVersion: 0x0200,
518 },
519 },
520 shouldFail: true,
521 expectedError: ":UNSUPPORTED_PROTOCOL:",
522 },
523 {
524 testType: serverTest,
525 name: "HttpGET",
526 sendPrefix: "GET / HTTP/1.0\n",
527 shouldFail: true,
528 expectedError: ":HTTP_REQUEST:",
529 },
530 {
531 testType: serverTest,
532 name: "HttpPOST",
533 sendPrefix: "POST / HTTP/1.0\n",
534 shouldFail: true,
535 expectedError: ":HTTP_REQUEST:",
536 },
537 {
538 testType: serverTest,
539 name: "HttpHEAD",
540 sendPrefix: "HEAD / HTTP/1.0\n",
541 shouldFail: true,
542 expectedError: ":HTTP_REQUEST:",
543 },
544 {
545 testType: serverTest,
546 name: "HttpPUT",
547 sendPrefix: "PUT / HTTP/1.0\n",
548 shouldFail: true,
549 expectedError: ":HTTP_REQUEST:",
550 },
551 {
552 testType: serverTest,
553 name: "HttpCONNECT",
554 sendPrefix: "CONNECT www.google.com:443 HTTP/1.0\n",
555 shouldFail: true,
556 expectedError: ":HTTPS_PROXY_REQUEST:",
557 },
David Benjamin39ebf532014-08-31 02:23:49 -0400558 {
David Benjaminf080ecd2014-12-11 18:48:59 -0500559 testType: serverTest,
560 name: "Garbage",
561 sendPrefix: "blah",
562 shouldFail: true,
563 expectedError: ":UNKNOWN_PROTOCOL:",
564 },
565 {
David Benjamin39ebf532014-08-31 02:23:49 -0400566 name: "SkipCipherVersionCheck",
567 config: Config{
568 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
569 MaxVersion: VersionTLS11,
570 Bugs: ProtocolBugs{
571 SkipCipherVersionCheck: true,
572 },
573 },
574 shouldFail: true,
575 expectedError: ":WRONG_CIPHER_RETURNED:",
576 },
David Benjamin9114fae2014-11-08 11:41:14 -0500577 {
David Benjamina3e89492015-02-26 15:16:22 -0500578 name: "RSAEphemeralKey",
David Benjamin9114fae2014-11-08 11:41:14 -0500579 config: Config{
580 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
581 Bugs: ProtocolBugs{
David Benjamina3e89492015-02-26 15:16:22 -0500582 RSAEphemeralKey: true,
David Benjamin9114fae2014-11-08 11:41:14 -0500583 },
584 },
585 shouldFail: true,
586 expectedError: ":UNEXPECTED_MESSAGE:",
587 },
David Benjamin128dbc32014-12-01 01:27:42 -0500588 {
589 name: "DisableEverything",
590 flags: []string{"-no-tls12", "-no-tls11", "-no-tls1", "-no-ssl3"},
591 shouldFail: true,
592 expectedError: ":WRONG_SSL_VERSION:",
593 },
594 {
595 protocol: dtls,
596 name: "DisableEverything-DTLS",
597 flags: []string{"-no-tls12", "-no-tls1"},
598 shouldFail: true,
599 expectedError: ":WRONG_SSL_VERSION:",
600 },
David Benjamin780d6dd2015-01-06 12:03:19 -0500601 {
602 name: "NoSharedCipher",
603 config: Config{
604 CipherSuites: []uint16{},
605 },
606 shouldFail: true,
607 expectedError: ":HANDSHAKE_FAILURE_ON_CLIENT_HELLO:",
608 },
David Benjamin13be1de2015-01-11 16:29:36 -0500609 {
610 protocol: dtls,
611 testType: serverTest,
612 name: "MTU",
613 config: Config{
614 Bugs: ProtocolBugs{
615 MaxPacketLength: 256,
616 },
617 },
618 flags: []string{"-mtu", "256"},
619 },
620 {
621 protocol: dtls,
622 testType: serverTest,
623 name: "MTUExceeded",
624 config: Config{
625 Bugs: ProtocolBugs{
626 MaxPacketLength: 255,
627 },
628 },
629 flags: []string{"-mtu", "256"},
630 shouldFail: true,
631 expectedLocalError: "dtls: exceeded maximum packet length",
632 },
David Benjamin6095de82014-12-27 01:50:38 -0500633 {
634 name: "CertMismatchRSA",
635 config: Config{
636 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
637 Certificates: []Certificate{getECDSACertificate()},
638 Bugs: ProtocolBugs{
639 SendCipherSuite: TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
640 },
641 },
642 shouldFail: true,
643 expectedError: ":WRONG_CERTIFICATE_TYPE:",
644 },
645 {
646 name: "CertMismatchECDSA",
647 config: Config{
648 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
649 Certificates: []Certificate{getRSACertificate()},
650 Bugs: ProtocolBugs{
651 SendCipherSuite: TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
652 },
653 },
654 shouldFail: true,
655 expectedError: ":WRONG_CERTIFICATE_TYPE:",
656 },
David Benjamin5fa3eba2015-01-22 16:35:40 -0500657 {
658 name: "TLSFatalBadPackets",
659 damageFirstWrite: true,
660 shouldFail: true,
661 expectedError: ":DECRYPTION_FAILED_OR_BAD_RECORD_MAC:",
662 },
663 {
664 protocol: dtls,
665 name: "DTLSIgnoreBadPackets",
666 damageFirstWrite: true,
667 },
668 {
669 protocol: dtls,
670 name: "DTLSIgnoreBadPackets-Async",
671 damageFirstWrite: true,
672 flags: []string{"-async"},
673 },
David Benjamin4189bd92015-01-25 23:52:39 -0500674 {
675 name: "AppDataAfterChangeCipherSpec",
676 config: Config{
677 Bugs: ProtocolBugs{
678 AppDataAfterChangeCipherSpec: []byte("TEST MESSAGE"),
679 },
680 },
681 shouldFail: true,
682 expectedError: ":DATA_BETWEEN_CCS_AND_FINISHED:",
683 },
684 {
685 protocol: dtls,
686 name: "AppDataAfterChangeCipherSpec-DTLS",
687 config: Config{
688 Bugs: ProtocolBugs{
689 AppDataAfterChangeCipherSpec: []byte("TEST MESSAGE"),
690 },
691 },
692 },
David Benjaminb3774b92015-01-31 17:16:01 -0500693 {
David Benjamindc3da932015-03-12 15:09:02 -0400694 name: "AlertAfterChangeCipherSpec",
695 config: Config{
696 Bugs: ProtocolBugs{
697 AlertAfterChangeCipherSpec: alertRecordOverflow,
698 },
699 },
700 shouldFail: true,
701 expectedError: ":TLSV1_ALERT_RECORD_OVERFLOW:",
702 },
703 {
704 protocol: dtls,
705 name: "AlertAfterChangeCipherSpec-DTLS",
706 config: Config{
707 Bugs: ProtocolBugs{
708 AlertAfterChangeCipherSpec: alertRecordOverflow,
709 },
710 },
711 shouldFail: true,
712 expectedError: ":TLSV1_ALERT_RECORD_OVERFLOW:",
713 },
714 {
David Benjaminb3774b92015-01-31 17:16:01 -0500715 protocol: dtls,
716 name: "ReorderHandshakeFragments-Small-DTLS",
717 config: Config{
718 Bugs: ProtocolBugs{
719 ReorderHandshakeFragments: true,
720 // Small enough that every handshake message is
721 // fragmented.
722 MaxHandshakeRecordLength: 2,
723 },
724 },
725 },
726 {
727 protocol: dtls,
728 name: "ReorderHandshakeFragments-Large-DTLS",
729 config: Config{
730 Bugs: ProtocolBugs{
731 ReorderHandshakeFragments: true,
732 // Large enough that no handshake message is
733 // fragmented.
David Benjaminb3774b92015-01-31 17:16:01 -0500734 MaxHandshakeRecordLength: 2048,
735 },
736 },
737 },
David Benjaminddb9f152015-02-03 15:44:39 -0500738 {
David Benjamin75381222015-03-02 19:30:30 -0500739 protocol: dtls,
740 name: "MixCompleteMessageWithFragments-DTLS",
741 config: Config{
742 Bugs: ProtocolBugs{
743 ReorderHandshakeFragments: true,
744 MixCompleteMessageWithFragments: true,
745 MaxHandshakeRecordLength: 2,
746 },
747 },
748 },
749 {
David Benjaminddb9f152015-02-03 15:44:39 -0500750 name: "SendInvalidRecordType",
751 config: Config{
752 Bugs: ProtocolBugs{
753 SendInvalidRecordType: true,
754 },
755 },
756 shouldFail: true,
757 expectedError: ":UNEXPECTED_RECORD:",
758 },
759 {
760 protocol: dtls,
761 name: "SendInvalidRecordType-DTLS",
762 config: Config{
763 Bugs: ProtocolBugs{
764 SendInvalidRecordType: true,
765 },
766 },
767 shouldFail: true,
768 expectedError: ":UNEXPECTED_RECORD:",
769 },
David Benjaminb80168e2015-02-08 18:30:14 -0500770 {
771 name: "FalseStart-SkipServerSecondLeg",
772 config: Config{
773 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
774 NextProtos: []string{"foo"},
775 Bugs: ProtocolBugs{
776 SkipNewSessionTicket: true,
777 SkipChangeCipherSpec: true,
778 SkipFinished: true,
779 ExpectFalseStart: true,
780 },
781 },
782 flags: []string{
783 "-false-start",
784 "-advertise-alpn", "\x03foo",
785 },
786 shimWritesFirst: true,
787 shouldFail: true,
788 expectedError: ":UNEXPECTED_RECORD:",
789 },
David Benjamin931ab342015-02-08 19:46:57 -0500790 {
791 name: "FalseStart-SkipServerSecondLeg-Implicit",
792 config: Config{
793 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
794 NextProtos: []string{"foo"},
795 Bugs: ProtocolBugs{
796 SkipNewSessionTicket: true,
797 SkipChangeCipherSpec: true,
798 SkipFinished: true,
799 },
800 },
801 flags: []string{
802 "-implicit-handshake",
803 "-false-start",
804 "-advertise-alpn", "\x03foo",
805 },
806 shouldFail: true,
807 expectedError: ":UNEXPECTED_RECORD:",
808 },
David Benjamin6f5c0f42015-02-24 01:23:21 -0500809 {
810 testType: serverTest,
811 name: "FailEarlyCallback",
812 flags: []string{"-fail-early-callback"},
813 shouldFail: true,
814 expectedError: ":CONNECTION_REJECTED:",
815 expectedLocalError: "remote error: access denied",
816 },
David Benjaminbcb2d912015-02-24 23:45:43 -0500817 {
818 name: "WrongMessageType",
819 config: Config{
820 Bugs: ProtocolBugs{
821 WrongCertificateMessageType: true,
822 },
823 },
824 shouldFail: true,
825 expectedError: ":UNEXPECTED_MESSAGE:",
826 expectedLocalError: "remote error: unexpected message",
827 },
828 {
829 protocol: dtls,
830 name: "WrongMessageType-DTLS",
831 config: Config{
832 Bugs: ProtocolBugs{
833 WrongCertificateMessageType: true,
834 },
835 },
836 shouldFail: true,
837 expectedError: ":UNEXPECTED_MESSAGE:",
838 expectedLocalError: "remote error: unexpected message",
839 },
David Benjamin75381222015-03-02 19:30:30 -0500840 {
841 protocol: dtls,
842 name: "FragmentMessageTypeMismatch-DTLS",
843 config: Config{
844 Bugs: ProtocolBugs{
845 MaxHandshakeRecordLength: 2,
846 FragmentMessageTypeMismatch: true,
847 },
848 },
849 shouldFail: true,
850 expectedError: ":FRAGMENT_MISMATCH:",
851 },
852 {
853 protocol: dtls,
854 name: "FragmentMessageLengthMismatch-DTLS",
855 config: Config{
856 Bugs: ProtocolBugs{
857 MaxHandshakeRecordLength: 2,
858 FragmentMessageLengthMismatch: true,
859 },
860 },
861 shouldFail: true,
862 expectedError: ":FRAGMENT_MISMATCH:",
863 },
864 {
865 protocol: dtls,
866 name: "SplitFragmentHeader-DTLS",
867 config: Config{
868 Bugs: ProtocolBugs{
869 SplitFragmentHeader: true,
870 },
871 },
872 shouldFail: true,
873 expectedError: ":UNEXPECTED_MESSAGE:",
874 },
875 {
876 protocol: dtls,
877 name: "SplitFragmentBody-DTLS",
878 config: Config{
879 Bugs: ProtocolBugs{
880 SplitFragmentBody: true,
881 },
882 },
883 shouldFail: true,
884 expectedError: ":UNEXPECTED_MESSAGE:",
885 },
886 {
887 protocol: dtls,
888 name: "SendEmptyFragments-DTLS",
889 config: Config{
890 Bugs: ProtocolBugs{
891 SendEmptyFragments: true,
892 },
893 },
894 },
David Benjamin67d1fb52015-03-16 15:16:23 -0400895 {
896 name: "UnsupportedCipherSuite",
897 config: Config{
898 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
899 Bugs: ProtocolBugs{
900 IgnorePeerCipherPreferences: true,
901 },
902 },
903 flags: []string{"-cipher", "DEFAULT:!RC4"},
904 shouldFail: true,
905 expectedError: ":WRONG_CIPHER_RETURNED:",
906 },
David Benjamin340d5ed2015-03-21 02:21:37 -0400907 {
908 name: "SendWarningAlerts",
909 config: Config{
910 Bugs: ProtocolBugs{
911 SendWarningAlerts: alertAccessDenied,
912 },
913 },
914 },
915 {
916 protocol: dtls,
917 name: "SendWarningAlerts-DTLS",
918 config: Config{
919 Bugs: ProtocolBugs{
920 SendWarningAlerts: alertAccessDenied,
921 },
922 },
923 },
Adam Langley95c29f32014-06-20 12:00:00 -0700924}
925
David Benjamin01fe8202014-09-24 15:21:44 -0400926func doExchange(test *testCase, config *Config, conn net.Conn, messageLen int, isResume bool) error {
David Benjamin65ea8ff2014-11-23 03:01:00 -0500927 var connDebug *recordingConn
David Benjamin5fa3eba2015-01-22 16:35:40 -0500928 var connDamage *damageAdaptor
David Benjamin65ea8ff2014-11-23 03:01:00 -0500929 if *flagDebug {
930 connDebug = &recordingConn{Conn: conn}
931 conn = connDebug
932 defer func() {
933 connDebug.WriteTo(os.Stdout)
934 }()
935 }
936
David Benjamin6fd297b2014-08-11 18:43:38 -0400937 if test.protocol == dtls {
David Benjamin83f90402015-01-27 01:09:43 -0500938 config.Bugs.PacketAdaptor = newPacketAdaptor(conn)
939 conn = config.Bugs.PacketAdaptor
David Benjamin5e961c12014-11-07 01:48:35 -0500940 if test.replayWrites {
941 conn = newReplayAdaptor(conn)
942 }
David Benjamin6fd297b2014-08-11 18:43:38 -0400943 }
944
David Benjamin5fa3eba2015-01-22 16:35:40 -0500945 if test.damageFirstWrite {
946 connDamage = newDamageAdaptor(conn)
947 conn = connDamage
948 }
949
David Benjamin6fd297b2014-08-11 18:43:38 -0400950 if test.sendPrefix != "" {
951 if _, err := conn.Write([]byte(test.sendPrefix)); err != nil {
952 return err
953 }
David Benjamin98e882e2014-08-08 13:24:34 -0400954 }
955
David Benjamin1d5c83e2014-07-22 19:20:02 -0400956 var tlsConn *Conn
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400957 if test.testType == clientTest {
David Benjamin6fd297b2014-08-11 18:43:38 -0400958 if test.protocol == dtls {
959 tlsConn = DTLSServer(conn, config)
960 } else {
961 tlsConn = Server(conn, config)
962 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400963 } else {
964 config.InsecureSkipVerify = true
David Benjamin6fd297b2014-08-11 18:43:38 -0400965 if test.protocol == dtls {
966 tlsConn = DTLSClient(conn, config)
967 } else {
968 tlsConn = Client(conn, config)
969 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400970 }
971
Adam Langley95c29f32014-06-20 12:00:00 -0700972 if err := tlsConn.Handshake(); err != nil {
973 return err
974 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700975
David Benjamin01fe8202014-09-24 15:21:44 -0400976 // TODO(davidben): move all per-connection expectations into a dedicated
977 // expectations struct that can be specified separately for the two
978 // legs.
979 expectedVersion := test.expectedVersion
980 if isResume && test.expectedResumeVersion != 0 {
981 expectedVersion = test.expectedResumeVersion
982 }
983 if vers := tlsConn.ConnectionState().Version; expectedVersion != 0 && vers != expectedVersion {
984 return fmt.Errorf("got version %x, expected %x", vers, expectedVersion)
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400985 }
986
David Benjamina08e49d2014-08-24 01:46:07 -0400987 if test.expectChannelID {
988 channelID := tlsConn.ConnectionState().ChannelID
989 if channelID == nil {
990 return fmt.Errorf("no channel ID negotiated")
991 }
992 if channelID.Curve != channelIDKey.Curve ||
993 channelIDKey.X.Cmp(channelIDKey.X) != 0 ||
994 channelIDKey.Y.Cmp(channelIDKey.Y) != 0 {
995 return fmt.Errorf("incorrect channel ID")
996 }
997 }
998
David Benjaminae2888f2014-09-06 12:58:58 -0400999 if expected := test.expectedNextProto; expected != "" {
1000 if actual := tlsConn.ConnectionState().NegotiatedProtocol; actual != expected {
1001 return fmt.Errorf("next proto mismatch: got %s, wanted %s", actual, expected)
1002 }
1003 }
1004
David Benjaminfc7b0862014-09-06 13:21:53 -04001005 if test.expectedNextProtoType != 0 {
1006 if (test.expectedNextProtoType == alpn) != tlsConn.ConnectionState().NegotiatedProtocolFromALPN {
1007 return fmt.Errorf("next proto type mismatch")
1008 }
1009 }
1010
David Benjaminca6c8262014-11-15 19:06:08 -05001011 if p := tlsConn.ConnectionState().SRTPProtectionProfile; p != test.expectedSRTPProtectionProfile {
1012 return fmt.Errorf("SRTP profile mismatch: got %d, wanted %d", p, test.expectedSRTPProtectionProfile)
1013 }
1014
David Benjamine58c4f52014-08-24 03:47:07 -04001015 if test.shimWritesFirst {
1016 var buf [5]byte
1017 _, err := io.ReadFull(tlsConn, buf[:])
1018 if err != nil {
1019 return err
1020 }
1021 if string(buf[:]) != "hello" {
1022 return fmt.Errorf("bad initial message")
1023 }
1024 }
1025
Adam Langleycf2d4f42014-10-28 19:06:14 -07001026 if test.renegotiate {
1027 if test.renegotiateCiphers != nil {
1028 config.CipherSuites = test.renegotiateCiphers
1029 }
1030 if err := tlsConn.Renegotiate(); err != nil {
1031 return err
1032 }
1033 } else if test.renegotiateCiphers != nil {
1034 panic("renegotiateCiphers without renegotiate")
1035 }
1036
David Benjamin5fa3eba2015-01-22 16:35:40 -05001037 if test.damageFirstWrite {
1038 connDamage.setDamage(true)
1039 tlsConn.Write([]byte("DAMAGED WRITE"))
1040 connDamage.setDamage(false)
1041 }
1042
Kenny Root7fdeaf12014-08-05 15:23:37 -07001043 if messageLen < 0 {
David Benjamin6fd297b2014-08-11 18:43:38 -04001044 if test.protocol == dtls {
1045 return fmt.Errorf("messageLen < 0 not supported for DTLS tests")
1046 }
Kenny Root7fdeaf12014-08-05 15:23:37 -07001047 // Read until EOF.
1048 _, err := io.Copy(ioutil.Discard, tlsConn)
1049 return err
1050 }
1051
David Benjamin4189bd92015-01-25 23:52:39 -05001052 var testMessage []byte
1053 if config.Bugs.AppDataAfterChangeCipherSpec != nil {
1054 // We've already sent a message. Expect the shim to echo it
1055 // back.
1056 testMessage = config.Bugs.AppDataAfterChangeCipherSpec
1057 } else {
1058 if messageLen == 0 {
1059 messageLen = 32
1060 }
1061 testMessage = make([]byte, messageLen)
1062 for i := range testMessage {
1063 testMessage[i] = 0x42
1064 }
1065 tlsConn.Write(testMessage)
Adam Langley80842bd2014-06-20 12:00:00 -07001066 }
Adam Langley95c29f32014-06-20 12:00:00 -07001067
1068 buf := make([]byte, len(testMessage))
David Benjamin6fd297b2014-08-11 18:43:38 -04001069 if test.protocol == dtls {
1070 bufTmp := make([]byte, len(buf)+1)
1071 n, err := tlsConn.Read(bufTmp)
1072 if err != nil {
1073 return err
1074 }
1075 if n != len(buf) {
1076 return fmt.Errorf("bad reply; length mismatch (%d vs %d)", n, len(buf))
1077 }
1078 copy(buf, bufTmp)
1079 } else {
1080 _, err := io.ReadFull(tlsConn, buf)
1081 if err != nil {
1082 return err
1083 }
Adam Langley95c29f32014-06-20 12:00:00 -07001084 }
1085
1086 for i, v := range buf {
1087 if v != testMessage[i]^0xff {
1088 return fmt.Errorf("bad reply contents at byte %d", i)
1089 }
1090 }
1091
1092 return nil
1093}
1094
David Benjamin325b5c32014-07-01 19:40:31 -04001095func valgrindOf(dbAttach bool, path string, args ...string) *exec.Cmd {
1096 valgrindArgs := []string{"--error-exitcode=99", "--track-origins=yes", "--leak-check=full"}
Adam Langley95c29f32014-06-20 12:00:00 -07001097 if dbAttach {
David Benjamin325b5c32014-07-01 19:40:31 -04001098 valgrindArgs = append(valgrindArgs, "--db-attach=yes", "--db-command=xterm -e gdb -nw %f %p")
Adam Langley95c29f32014-06-20 12:00:00 -07001099 }
David Benjamin325b5c32014-07-01 19:40:31 -04001100 valgrindArgs = append(valgrindArgs, path)
1101 valgrindArgs = append(valgrindArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -07001102
David Benjamin325b5c32014-07-01 19:40:31 -04001103 return exec.Command("valgrind", valgrindArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -07001104}
1105
David Benjamin325b5c32014-07-01 19:40:31 -04001106func gdbOf(path string, args ...string) *exec.Cmd {
1107 xtermArgs := []string{"-e", "gdb", "--args"}
1108 xtermArgs = append(xtermArgs, path)
1109 xtermArgs = append(xtermArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -07001110
David Benjamin325b5c32014-07-01 19:40:31 -04001111 return exec.Command("xterm", xtermArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -07001112}
1113
Adam Langley69a01602014-11-17 17:26:55 -08001114type moreMallocsError struct{}
1115
1116func (moreMallocsError) Error() string {
1117 return "child process did not exhaust all allocation calls"
1118}
1119
1120var errMoreMallocs = moreMallocsError{}
1121
David Benjamin87c8a642015-02-21 01:54:29 -05001122// accept accepts a connection from listener, unless waitChan signals a process
1123// exit first.
1124func acceptOrWait(listener net.Listener, waitChan chan error) (net.Conn, error) {
1125 type connOrError struct {
1126 conn net.Conn
1127 err error
1128 }
1129 connChan := make(chan connOrError, 1)
1130 go func() {
1131 conn, err := listener.Accept()
1132 connChan <- connOrError{conn, err}
1133 close(connChan)
1134 }()
1135 select {
1136 case result := <-connChan:
1137 return result.conn, result.err
1138 case childErr := <-waitChan:
1139 waitChan <- childErr
1140 return nil, fmt.Errorf("child exited early: %s", childErr)
1141 }
1142}
1143
Adam Langley69a01602014-11-17 17:26:55 -08001144func runTest(test *testCase, buildDir string, mallocNumToFail int64) error {
Adam Langley38311732014-10-16 19:04:35 -07001145 if !test.shouldFail && (len(test.expectedError) > 0 || len(test.expectedLocalError) > 0) {
1146 panic("Error expected without shouldFail in " + test.name)
1147 }
1148
David Benjamin87c8a642015-02-21 01:54:29 -05001149 listener, err := net.ListenTCP("tcp4", &net.TCPAddr{IP: net.IP{127, 0, 0, 1}})
1150 if err != nil {
1151 panic(err)
1152 }
1153 defer func() {
1154 if listener != nil {
1155 listener.Close()
1156 }
1157 }()
Adam Langley95c29f32014-06-20 12:00:00 -07001158
David Benjamin884fdf12014-08-02 15:28:23 -04001159 shim_path := path.Join(buildDir, "ssl/test/bssl_shim")
David Benjamin87c8a642015-02-21 01:54:29 -05001160 flags := []string{"-port", strconv.Itoa(listener.Addr().(*net.TCPAddr).Port)}
David Benjamin1d5c83e2014-07-22 19:20:02 -04001161 if test.testType == serverTest {
David Benjamin5a593af2014-08-11 19:51:50 -04001162 flags = append(flags, "-server")
1163
David Benjamin025b3d32014-07-01 19:53:04 -04001164 flags = append(flags, "-key-file")
1165 if test.keyFile == "" {
1166 flags = append(flags, rsaKeyFile)
1167 } else {
1168 flags = append(flags, test.keyFile)
1169 }
1170
1171 flags = append(flags, "-cert-file")
1172 if test.certFile == "" {
1173 flags = append(flags, rsaCertificateFile)
1174 } else {
1175 flags = append(flags, test.certFile)
1176 }
1177 }
David Benjamin5a593af2014-08-11 19:51:50 -04001178
David Benjamin6fd297b2014-08-11 18:43:38 -04001179 if test.protocol == dtls {
1180 flags = append(flags, "-dtls")
1181 }
1182
David Benjamin5a593af2014-08-11 19:51:50 -04001183 if test.resumeSession {
1184 flags = append(flags, "-resume")
1185 }
1186
David Benjamine58c4f52014-08-24 03:47:07 -04001187 if test.shimWritesFirst {
1188 flags = append(flags, "-shim-writes-first")
1189 }
1190
David Benjamin025b3d32014-07-01 19:53:04 -04001191 flags = append(flags, test.flags...)
1192
1193 var shim *exec.Cmd
1194 if *useValgrind {
1195 shim = valgrindOf(false, shim_path, flags...)
Adam Langley75712922014-10-10 16:23:43 -07001196 } else if *useGDB {
1197 shim = gdbOf(shim_path, flags...)
David Benjamin025b3d32014-07-01 19:53:04 -04001198 } else {
1199 shim = exec.Command(shim_path, flags...)
1200 }
David Benjamin025b3d32014-07-01 19:53:04 -04001201 shim.Stdin = os.Stdin
1202 var stdoutBuf, stderrBuf bytes.Buffer
1203 shim.Stdout = &stdoutBuf
1204 shim.Stderr = &stderrBuf
Adam Langley69a01602014-11-17 17:26:55 -08001205 if mallocNumToFail >= 0 {
David Benjamin9e128b02015-02-09 13:13:09 -05001206 shim.Env = os.Environ()
1207 shim.Env = append(shim.Env, "MALLOC_NUMBER_TO_FAIL="+strconv.FormatInt(mallocNumToFail, 10))
Adam Langley69a01602014-11-17 17:26:55 -08001208 if *mallocTestDebug {
1209 shim.Env = append(shim.Env, "MALLOC_ABORT_ON_FAIL=1")
1210 }
1211 shim.Env = append(shim.Env, "_MALLOC_CHECK=1")
1212 }
David Benjamin025b3d32014-07-01 19:53:04 -04001213
1214 if err := shim.Start(); err != nil {
Adam Langley95c29f32014-06-20 12:00:00 -07001215 panic(err)
1216 }
David Benjamin87c8a642015-02-21 01:54:29 -05001217 waitChan := make(chan error, 1)
1218 go func() { waitChan <- shim.Wait() }()
Adam Langley95c29f32014-06-20 12:00:00 -07001219
1220 config := test.config
David Benjamin1d5c83e2014-07-22 19:20:02 -04001221 config.ClientSessionCache = NewLRUClientSessionCache(1)
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001222 config.ServerSessionCache = NewLRUServerSessionCache(1)
David Benjamin025b3d32014-07-01 19:53:04 -04001223 if test.testType == clientTest {
1224 if len(config.Certificates) == 0 {
1225 config.Certificates = []Certificate{getRSACertificate()}
1226 }
David Benjamin87c8a642015-02-21 01:54:29 -05001227 } else {
1228 // Supply a ServerName to ensure a constant session cache key,
1229 // rather than falling back to net.Conn.RemoteAddr.
1230 if len(config.ServerName) == 0 {
1231 config.ServerName = "test"
1232 }
David Benjamin025b3d32014-07-01 19:53:04 -04001233 }
Adam Langley95c29f32014-06-20 12:00:00 -07001234
David Benjamin87c8a642015-02-21 01:54:29 -05001235 conn, err := acceptOrWait(listener, waitChan)
1236 if err == nil {
1237 err = doExchange(test, &config, conn, test.messageLen, false /* not a resumption */)
1238 conn.Close()
1239 }
David Benjamin65ea8ff2014-11-23 03:01:00 -05001240
David Benjamin1d5c83e2014-07-22 19:20:02 -04001241 if err == nil && test.resumeSession {
David Benjamin01fe8202014-09-24 15:21:44 -04001242 var resumeConfig Config
1243 if test.resumeConfig != nil {
1244 resumeConfig = *test.resumeConfig
David Benjamin87c8a642015-02-21 01:54:29 -05001245 if len(resumeConfig.ServerName) == 0 {
1246 resumeConfig.ServerName = config.ServerName
1247 }
David Benjamin01fe8202014-09-24 15:21:44 -04001248 if len(resumeConfig.Certificates) == 0 {
1249 resumeConfig.Certificates = []Certificate{getRSACertificate()}
1250 }
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001251 if !test.newSessionsOnResume {
1252 resumeConfig.SessionTicketKey = config.SessionTicketKey
1253 resumeConfig.ClientSessionCache = config.ClientSessionCache
1254 resumeConfig.ServerSessionCache = config.ServerSessionCache
1255 }
David Benjamin01fe8202014-09-24 15:21:44 -04001256 } else {
1257 resumeConfig = config
1258 }
David Benjamin87c8a642015-02-21 01:54:29 -05001259 var connResume net.Conn
1260 connResume, err = acceptOrWait(listener, waitChan)
1261 if err == nil {
1262 err = doExchange(test, &resumeConfig, connResume, test.messageLen, true /* resumption */)
1263 connResume.Close()
1264 }
David Benjamin1d5c83e2014-07-22 19:20:02 -04001265 }
1266
David Benjamin87c8a642015-02-21 01:54:29 -05001267 // Close the listener now. This is to avoid hangs should the shim try to
1268 // open more connections than expected.
1269 listener.Close()
1270 listener = nil
1271
1272 childErr := <-waitChan
Adam Langley69a01602014-11-17 17:26:55 -08001273 if exitError, ok := childErr.(*exec.ExitError); ok {
1274 if exitError.Sys().(syscall.WaitStatus).ExitStatus() == 88 {
1275 return errMoreMallocs
1276 }
1277 }
Adam Langley95c29f32014-06-20 12:00:00 -07001278
1279 stdout := string(stdoutBuf.Bytes())
1280 stderr := string(stderrBuf.Bytes())
1281 failed := err != nil || childErr != nil
1282 correctFailure := len(test.expectedError) == 0 || strings.Contains(stdout, test.expectedError)
Adam Langleyac61fa32014-06-23 12:03:11 -07001283 localError := "none"
1284 if err != nil {
1285 localError = err.Error()
1286 }
1287 if len(test.expectedLocalError) != 0 {
1288 correctFailure = correctFailure && strings.Contains(localError, test.expectedLocalError)
1289 }
Adam Langley95c29f32014-06-20 12:00:00 -07001290
1291 if failed != test.shouldFail || failed && !correctFailure {
Adam Langley95c29f32014-06-20 12:00:00 -07001292 childError := "none"
Adam Langley95c29f32014-06-20 12:00:00 -07001293 if childErr != nil {
1294 childError = childErr.Error()
1295 }
1296
1297 var msg string
1298 switch {
1299 case failed && !test.shouldFail:
1300 msg = "unexpected failure"
1301 case !failed && test.shouldFail:
1302 msg = "unexpected success"
1303 case failed && !correctFailure:
Adam Langleyac61fa32014-06-23 12:03:11 -07001304 msg = "bad error (wanted '" + test.expectedError + "' / '" + test.expectedLocalError + "')"
Adam Langley95c29f32014-06-20 12:00:00 -07001305 default:
1306 panic("internal error")
1307 }
1308
1309 return fmt.Errorf("%s: local error '%s', child error '%s', stdout:\n%s\nstderr:\n%s", msg, localError, childError, string(stdoutBuf.Bytes()), stderr)
1310 }
1311
1312 if !*useValgrind && len(stderr) > 0 {
1313 println(stderr)
1314 }
1315
1316 return nil
1317}
1318
1319var tlsVersions = []struct {
1320 name string
1321 version uint16
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001322 flag string
David Benjamin8b8c0062014-11-23 02:47:52 -05001323 hasDTLS bool
Adam Langley95c29f32014-06-20 12:00:00 -07001324}{
David Benjamin8b8c0062014-11-23 02:47:52 -05001325 {"SSL3", VersionSSL30, "-no-ssl3", false},
1326 {"TLS1", VersionTLS10, "-no-tls1", true},
1327 {"TLS11", VersionTLS11, "-no-tls11", false},
1328 {"TLS12", VersionTLS12, "-no-tls12", true},
Adam Langley95c29f32014-06-20 12:00:00 -07001329}
1330
1331var testCipherSuites = []struct {
1332 name string
1333 id uint16
1334}{
1335 {"3DES-SHA", TLS_RSA_WITH_3DES_EDE_CBC_SHA},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001336 {"AES128-GCM", TLS_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001337 {"AES128-SHA", TLS_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001338 {"AES128-SHA256", TLS_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001339 {"AES256-GCM", TLS_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001340 {"AES256-SHA", TLS_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001341 {"AES256-SHA256", TLS_RSA_WITH_AES_256_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001342 {"DHE-RSA-AES128-GCM", TLS_DHE_RSA_WITH_AES_128_GCM_SHA256},
1343 {"DHE-RSA-AES128-SHA", TLS_DHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001344 {"DHE-RSA-AES128-SHA256", TLS_DHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001345 {"DHE-RSA-AES256-GCM", TLS_DHE_RSA_WITH_AES_256_GCM_SHA384},
1346 {"DHE-RSA-AES256-SHA", TLS_DHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001347 {"DHE-RSA-AES256-SHA256", TLS_DHE_RSA_WITH_AES_256_CBC_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001348 {"ECDHE-ECDSA-AES128-GCM", TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
1349 {"ECDHE-ECDSA-AES128-SHA", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001350 {"ECDHE-ECDSA-AES128-SHA256", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256},
1351 {"ECDHE-ECDSA-AES256-GCM", TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001352 {"ECDHE-ECDSA-AES256-SHA", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001353 {"ECDHE-ECDSA-AES256-SHA384", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001354 {"ECDHE-ECDSA-RC4-SHA", TLS_ECDHE_ECDSA_WITH_RC4_128_SHA},
David Benjamin2af684f2014-10-27 02:23:15 -04001355 {"ECDHE-PSK-WITH-AES-128-GCM-SHA256", TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001356 {"ECDHE-RSA-AES128-GCM", TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001357 {"ECDHE-RSA-AES128-SHA", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001358 {"ECDHE-RSA-AES128-SHA256", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001359 {"ECDHE-RSA-AES256-GCM", TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001360 {"ECDHE-RSA-AES256-SHA", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001361 {"ECDHE-RSA-AES256-SHA384", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001362 {"ECDHE-RSA-RC4-SHA", TLS_ECDHE_RSA_WITH_RC4_128_SHA},
David Benjamin48cae082014-10-27 01:06:24 -04001363 {"PSK-AES128-CBC-SHA", TLS_PSK_WITH_AES_128_CBC_SHA},
1364 {"PSK-AES256-CBC-SHA", TLS_PSK_WITH_AES_256_CBC_SHA},
1365 {"PSK-RC4-SHA", TLS_PSK_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -07001366 {"RC4-MD5", TLS_RSA_WITH_RC4_128_MD5},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001367 {"RC4-SHA", TLS_RSA_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -07001368}
1369
David Benjamin8b8c0062014-11-23 02:47:52 -05001370func hasComponent(suiteName, component string) bool {
1371 return strings.Contains("-"+suiteName+"-", "-"+component+"-")
1372}
1373
David Benjaminf7768e42014-08-31 02:06:47 -04001374func isTLS12Only(suiteName string) bool {
David Benjamin8b8c0062014-11-23 02:47:52 -05001375 return hasComponent(suiteName, "GCM") ||
1376 hasComponent(suiteName, "SHA256") ||
1377 hasComponent(suiteName, "SHA384")
1378}
1379
1380func isDTLSCipher(suiteName string) bool {
David Benjamine95d20d2014-12-23 11:16:01 -05001381 return !hasComponent(suiteName, "RC4")
David Benjaminf7768e42014-08-31 02:06:47 -04001382}
1383
Adam Langley95c29f32014-06-20 12:00:00 -07001384func addCipherSuiteTests() {
1385 for _, suite := range testCipherSuites {
David Benjamin48cae082014-10-27 01:06:24 -04001386 const psk = "12345"
1387 const pskIdentity = "luggage combo"
1388
Adam Langley95c29f32014-06-20 12:00:00 -07001389 var cert Certificate
David Benjamin025b3d32014-07-01 19:53:04 -04001390 var certFile string
1391 var keyFile string
David Benjamin8b8c0062014-11-23 02:47:52 -05001392 if hasComponent(suite.name, "ECDSA") {
Adam Langley95c29f32014-06-20 12:00:00 -07001393 cert = getECDSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -04001394 certFile = ecdsaCertificateFile
1395 keyFile = ecdsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -07001396 } else {
1397 cert = getRSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -04001398 certFile = rsaCertificateFile
1399 keyFile = rsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -07001400 }
1401
David Benjamin48cae082014-10-27 01:06:24 -04001402 var flags []string
David Benjamin8b8c0062014-11-23 02:47:52 -05001403 if hasComponent(suite.name, "PSK") {
David Benjamin48cae082014-10-27 01:06:24 -04001404 flags = append(flags,
1405 "-psk", psk,
1406 "-psk-identity", pskIdentity)
1407 }
1408
Adam Langley95c29f32014-06-20 12:00:00 -07001409 for _, ver := range tlsVersions {
David Benjaminf7768e42014-08-31 02:06:47 -04001410 if ver.version < VersionTLS12 && isTLS12Only(suite.name) {
Adam Langley95c29f32014-06-20 12:00:00 -07001411 continue
1412 }
1413
David Benjamin025b3d32014-07-01 19:53:04 -04001414 testCases = append(testCases, testCase{
1415 testType: clientTest,
1416 name: ver.name + "-" + suite.name + "-client",
Adam Langley95c29f32014-06-20 12:00:00 -07001417 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001418 MinVersion: ver.version,
1419 MaxVersion: ver.version,
1420 CipherSuites: []uint16{suite.id},
1421 Certificates: []Certificate{cert},
1422 PreSharedKey: []byte(psk),
1423 PreSharedKeyIdentity: pskIdentity,
Adam Langley95c29f32014-06-20 12:00:00 -07001424 },
David Benjamin48cae082014-10-27 01:06:24 -04001425 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001426 resumeSession: true,
Adam Langley95c29f32014-06-20 12:00:00 -07001427 })
David Benjamin025b3d32014-07-01 19:53:04 -04001428
David Benjamin76d8abe2014-08-14 16:25:34 -04001429 testCases = append(testCases, testCase{
1430 testType: serverTest,
1431 name: ver.name + "-" + suite.name + "-server",
1432 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001433 MinVersion: ver.version,
1434 MaxVersion: ver.version,
1435 CipherSuites: []uint16{suite.id},
1436 Certificates: []Certificate{cert},
1437 PreSharedKey: []byte(psk),
1438 PreSharedKeyIdentity: pskIdentity,
David Benjamin76d8abe2014-08-14 16:25:34 -04001439 },
1440 certFile: certFile,
1441 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001442 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001443 resumeSession: true,
David Benjamin76d8abe2014-08-14 16:25:34 -04001444 })
David Benjamin6fd297b2014-08-11 18:43:38 -04001445
David Benjamin8b8c0062014-11-23 02:47:52 -05001446 if ver.hasDTLS && isDTLSCipher(suite.name) {
David Benjamin6fd297b2014-08-11 18:43:38 -04001447 testCases = append(testCases, testCase{
1448 testType: clientTest,
1449 protocol: dtls,
1450 name: "D" + ver.name + "-" + suite.name + "-client",
1451 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001452 MinVersion: ver.version,
1453 MaxVersion: ver.version,
1454 CipherSuites: []uint16{suite.id},
1455 Certificates: []Certificate{cert},
1456 PreSharedKey: []byte(psk),
1457 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001458 },
David Benjamin48cae082014-10-27 01:06:24 -04001459 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001460 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001461 })
1462 testCases = append(testCases, testCase{
1463 testType: serverTest,
1464 protocol: dtls,
1465 name: "D" + ver.name + "-" + suite.name + "-server",
1466 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001467 MinVersion: ver.version,
1468 MaxVersion: ver.version,
1469 CipherSuites: []uint16{suite.id},
1470 Certificates: []Certificate{cert},
1471 PreSharedKey: []byte(psk),
1472 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001473 },
1474 certFile: certFile,
1475 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001476 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001477 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001478 })
1479 }
Adam Langley95c29f32014-06-20 12:00:00 -07001480 }
1481 }
1482}
1483
1484func addBadECDSASignatureTests() {
1485 for badR := BadValue(1); badR < NumBadValues; badR++ {
1486 for badS := BadValue(1); badS < NumBadValues; badS++ {
David Benjamin025b3d32014-07-01 19:53:04 -04001487 testCases = append(testCases, testCase{
Adam Langley95c29f32014-06-20 12:00:00 -07001488 name: fmt.Sprintf("BadECDSA-%d-%d", badR, badS),
1489 config: Config{
1490 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
1491 Certificates: []Certificate{getECDSACertificate()},
1492 Bugs: ProtocolBugs{
1493 BadECDSAR: badR,
1494 BadECDSAS: badS,
1495 },
1496 },
1497 shouldFail: true,
1498 expectedError: "SIGNATURE",
1499 })
1500 }
1501 }
1502}
1503
Adam Langley80842bd2014-06-20 12:00:00 -07001504func addCBCPaddingTests() {
David Benjamin025b3d32014-07-01 19:53:04 -04001505 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001506 name: "MaxCBCPadding",
1507 config: Config{
1508 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1509 Bugs: ProtocolBugs{
1510 MaxPadding: true,
1511 },
1512 },
1513 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1514 })
David Benjamin025b3d32014-07-01 19:53:04 -04001515 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001516 name: "BadCBCPadding",
1517 config: Config{
1518 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1519 Bugs: ProtocolBugs{
1520 PaddingFirstByteBad: true,
1521 },
1522 },
1523 shouldFail: true,
1524 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1525 })
1526 // OpenSSL previously had an issue where the first byte of padding in
1527 // 255 bytes of padding wasn't checked.
David Benjamin025b3d32014-07-01 19:53:04 -04001528 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001529 name: "BadCBCPadding255",
1530 config: Config{
1531 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1532 Bugs: ProtocolBugs{
1533 MaxPadding: true,
1534 PaddingFirstByteBadIf255: true,
1535 },
1536 },
1537 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1538 shouldFail: true,
1539 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1540 })
1541}
1542
Kenny Root7fdeaf12014-08-05 15:23:37 -07001543func addCBCSplittingTests() {
1544 testCases = append(testCases, testCase{
1545 name: "CBCRecordSplitting",
1546 config: Config{
1547 MaxVersion: VersionTLS10,
1548 MinVersion: VersionTLS10,
1549 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1550 },
1551 messageLen: -1, // read until EOF
1552 flags: []string{
1553 "-async",
1554 "-write-different-record-sizes",
1555 "-cbc-record-splitting",
1556 },
David Benjamina8e3e0e2014-08-06 22:11:10 -04001557 })
1558 testCases = append(testCases, testCase{
Kenny Root7fdeaf12014-08-05 15:23:37 -07001559 name: "CBCRecordSplittingPartialWrite",
1560 config: Config{
1561 MaxVersion: VersionTLS10,
1562 MinVersion: VersionTLS10,
1563 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1564 },
1565 messageLen: -1, // read until EOF
1566 flags: []string{
1567 "-async",
1568 "-write-different-record-sizes",
1569 "-cbc-record-splitting",
1570 "-partial-write",
1571 },
1572 })
1573}
1574
David Benjamin636293b2014-07-08 17:59:18 -04001575func addClientAuthTests() {
David Benjamin407a10c2014-07-16 12:58:59 -04001576 // Add a dummy cert pool to stress certificate authority parsing.
1577 // TODO(davidben): Add tests that those values parse out correctly.
1578 certPool := x509.NewCertPool()
1579 cert, err := x509.ParseCertificate(rsaCertificate.Certificate[0])
1580 if err != nil {
1581 panic(err)
1582 }
1583 certPool.AddCert(cert)
1584
David Benjamin636293b2014-07-08 17:59:18 -04001585 for _, ver := range tlsVersions {
David Benjamin636293b2014-07-08 17:59:18 -04001586 testCases = append(testCases, testCase{
1587 testType: clientTest,
David Benjamin67666e72014-07-12 15:47:52 -04001588 name: ver.name + "-Client-ClientAuth-RSA",
David Benjamin636293b2014-07-08 17:59:18 -04001589 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001590 MinVersion: ver.version,
1591 MaxVersion: ver.version,
1592 ClientAuth: RequireAnyClientCert,
1593 ClientCAs: certPool,
David Benjamin636293b2014-07-08 17:59:18 -04001594 },
1595 flags: []string{
1596 "-cert-file", rsaCertificateFile,
1597 "-key-file", rsaKeyFile,
1598 },
1599 })
1600 testCases = append(testCases, testCase{
David Benjamin67666e72014-07-12 15:47:52 -04001601 testType: serverTest,
1602 name: ver.name + "-Server-ClientAuth-RSA",
1603 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001604 MinVersion: ver.version,
1605 MaxVersion: ver.version,
David Benjamin67666e72014-07-12 15:47:52 -04001606 Certificates: []Certificate{rsaCertificate},
1607 },
1608 flags: []string{"-require-any-client-certificate"},
1609 })
David Benjamine098ec22014-08-27 23:13:20 -04001610 if ver.version != VersionSSL30 {
1611 testCases = append(testCases, testCase{
1612 testType: serverTest,
1613 name: ver.name + "-Server-ClientAuth-ECDSA",
1614 config: Config{
1615 MinVersion: ver.version,
1616 MaxVersion: ver.version,
1617 Certificates: []Certificate{ecdsaCertificate},
1618 },
1619 flags: []string{"-require-any-client-certificate"},
1620 })
1621 testCases = append(testCases, testCase{
1622 testType: clientTest,
1623 name: ver.name + "-Client-ClientAuth-ECDSA",
1624 config: Config{
1625 MinVersion: ver.version,
1626 MaxVersion: ver.version,
1627 ClientAuth: RequireAnyClientCert,
1628 ClientCAs: certPool,
1629 },
1630 flags: []string{
1631 "-cert-file", ecdsaCertificateFile,
1632 "-key-file", ecdsaKeyFile,
1633 },
1634 })
1635 }
David Benjamin636293b2014-07-08 17:59:18 -04001636 }
1637}
1638
Adam Langley75712922014-10-10 16:23:43 -07001639func addExtendedMasterSecretTests() {
1640 const expectEMSFlag = "-expect-extended-master-secret"
1641
1642 for _, with := range []bool{false, true} {
1643 prefix := "No"
1644 var flags []string
1645 if with {
1646 prefix = ""
1647 flags = []string{expectEMSFlag}
1648 }
1649
1650 for _, isClient := range []bool{false, true} {
1651 suffix := "-Server"
1652 testType := serverTest
1653 if isClient {
1654 suffix = "-Client"
1655 testType = clientTest
1656 }
1657
1658 for _, ver := range tlsVersions {
1659 test := testCase{
1660 testType: testType,
1661 name: prefix + "ExtendedMasterSecret-" + ver.name + suffix,
1662 config: Config{
1663 MinVersion: ver.version,
1664 MaxVersion: ver.version,
1665 Bugs: ProtocolBugs{
1666 NoExtendedMasterSecret: !with,
1667 RequireExtendedMasterSecret: with,
1668 },
1669 },
David Benjamin48cae082014-10-27 01:06:24 -04001670 flags: flags,
1671 shouldFail: ver.version == VersionSSL30 && with,
Adam Langley75712922014-10-10 16:23:43 -07001672 }
1673 if test.shouldFail {
1674 test.expectedLocalError = "extended master secret required but not supported by peer"
1675 }
1676 testCases = append(testCases, test)
1677 }
1678 }
1679 }
1680
1681 // When a session is resumed, it should still be aware that its master
1682 // secret was generated via EMS and thus it's safe to use tls-unique.
1683 testCases = append(testCases, testCase{
1684 name: "ExtendedMasterSecret-Resume",
1685 config: Config{
1686 Bugs: ProtocolBugs{
1687 RequireExtendedMasterSecret: true,
1688 },
1689 },
1690 flags: []string{expectEMSFlag},
1691 resumeSession: true,
1692 })
1693}
1694
David Benjamin43ec06f2014-08-05 02:28:57 -04001695// Adds tests that try to cover the range of the handshake state machine, under
1696// various conditions. Some of these are redundant with other tests, but they
1697// only cover the synchronous case.
David Benjamin6fd297b2014-08-11 18:43:38 -04001698func addStateMachineCoverageTests(async, splitHandshake bool, protocol protocol) {
David Benjamin43ec06f2014-08-05 02:28:57 -04001699 var suffix string
1700 var flags []string
1701 var maxHandshakeRecordLength int
David Benjamin6fd297b2014-08-11 18:43:38 -04001702 if protocol == dtls {
1703 suffix = "-DTLS"
1704 }
David Benjamin43ec06f2014-08-05 02:28:57 -04001705 if async {
David Benjamin6fd297b2014-08-11 18:43:38 -04001706 suffix += "-Async"
David Benjamin43ec06f2014-08-05 02:28:57 -04001707 flags = append(flags, "-async")
1708 } else {
David Benjamin6fd297b2014-08-11 18:43:38 -04001709 suffix += "-Sync"
David Benjamin43ec06f2014-08-05 02:28:57 -04001710 }
1711 if splitHandshake {
1712 suffix += "-SplitHandshakeRecords"
David Benjamin98214542014-08-07 18:02:39 -04001713 maxHandshakeRecordLength = 1
David Benjamin43ec06f2014-08-05 02:28:57 -04001714 }
1715
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001716 // Basic handshake, with resumption. Client and server,
1717 // session ID and session ticket.
David Benjamin43ec06f2014-08-05 02:28:57 -04001718 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001719 protocol: protocol,
1720 name: "Basic-Client" + suffix,
David Benjamin43ec06f2014-08-05 02:28:57 -04001721 config: Config{
1722 Bugs: ProtocolBugs{
1723 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1724 },
1725 },
David Benjaminbed9aae2014-08-07 19:13:38 -04001726 flags: flags,
1727 resumeSession: true,
1728 })
1729 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001730 protocol: protocol,
1731 name: "Basic-Client-RenewTicket" + suffix,
David Benjaminbed9aae2014-08-07 19:13:38 -04001732 config: Config{
1733 Bugs: ProtocolBugs{
1734 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1735 RenewTicketOnResume: true,
1736 },
1737 },
1738 flags: flags,
1739 resumeSession: true,
David Benjamin43ec06f2014-08-05 02:28:57 -04001740 })
1741 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001742 protocol: protocol,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001743 name: "Basic-Client-NoTicket" + suffix,
1744 config: Config{
1745 SessionTicketsDisabled: true,
1746 Bugs: ProtocolBugs{
1747 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1748 },
1749 },
1750 flags: flags,
1751 resumeSession: true,
1752 })
1753 testCases = append(testCases, testCase{
1754 protocol: protocol,
David Benjamine0e7d0d2015-02-08 19:33:25 -05001755 name: "Basic-Client-Implicit" + suffix,
1756 config: Config{
1757 Bugs: ProtocolBugs{
1758 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1759 },
1760 },
1761 flags: append(flags, "-implicit-handshake"),
1762 resumeSession: true,
1763 })
1764 testCases = append(testCases, testCase{
1765 protocol: protocol,
David Benjamin43ec06f2014-08-05 02:28:57 -04001766 testType: serverTest,
1767 name: "Basic-Server" + suffix,
1768 config: Config{
1769 Bugs: ProtocolBugs{
1770 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1771 },
1772 },
David Benjaminbed9aae2014-08-07 19:13:38 -04001773 flags: flags,
1774 resumeSession: true,
David Benjamin43ec06f2014-08-05 02:28:57 -04001775 })
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001776 testCases = append(testCases, testCase{
1777 protocol: protocol,
1778 testType: serverTest,
1779 name: "Basic-Server-NoTickets" + suffix,
1780 config: Config{
1781 SessionTicketsDisabled: true,
1782 Bugs: ProtocolBugs{
1783 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1784 },
1785 },
1786 flags: flags,
1787 resumeSession: true,
1788 })
David Benjamine0e7d0d2015-02-08 19:33:25 -05001789 testCases = append(testCases, testCase{
1790 protocol: protocol,
1791 testType: serverTest,
1792 name: "Basic-Server-Implicit" + suffix,
1793 config: Config{
1794 Bugs: ProtocolBugs{
1795 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1796 },
1797 },
1798 flags: append(flags, "-implicit-handshake"),
1799 resumeSession: true,
1800 })
David Benjamin6f5c0f42015-02-24 01:23:21 -05001801 testCases = append(testCases, testCase{
1802 protocol: protocol,
1803 testType: serverTest,
1804 name: "Basic-Server-EarlyCallback" + suffix,
1805 config: Config{
1806 Bugs: ProtocolBugs{
1807 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1808 },
1809 },
1810 flags: append(flags, "-use-early-callback"),
1811 resumeSession: true,
1812 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001813
David Benjamin6fd297b2014-08-11 18:43:38 -04001814 // TLS client auth.
1815 testCases = append(testCases, testCase{
1816 protocol: protocol,
1817 testType: clientTest,
1818 name: "ClientAuth-Client" + suffix,
1819 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001820 ClientAuth: RequireAnyClientCert,
David Benjamin6fd297b2014-08-11 18:43:38 -04001821 Bugs: ProtocolBugs{
1822 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1823 },
1824 },
1825 flags: append(flags,
1826 "-cert-file", rsaCertificateFile,
1827 "-key-file", rsaKeyFile),
1828 })
1829 testCases = append(testCases, testCase{
1830 protocol: protocol,
1831 testType: serverTest,
1832 name: "ClientAuth-Server" + suffix,
1833 config: Config{
1834 Certificates: []Certificate{rsaCertificate},
1835 },
1836 flags: append(flags, "-require-any-client-certificate"),
1837 })
1838
David Benjamin43ec06f2014-08-05 02:28:57 -04001839 // No session ticket support; server doesn't send NewSessionTicket.
1840 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001841 protocol: protocol,
1842 name: "SessionTicketsDisabled-Client" + suffix,
David Benjamin43ec06f2014-08-05 02:28:57 -04001843 config: Config{
1844 SessionTicketsDisabled: true,
1845 Bugs: ProtocolBugs{
1846 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1847 },
1848 },
1849 flags: flags,
1850 })
1851 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001852 protocol: protocol,
David Benjamin43ec06f2014-08-05 02:28:57 -04001853 testType: serverTest,
1854 name: "SessionTicketsDisabled-Server" + suffix,
1855 config: Config{
1856 SessionTicketsDisabled: true,
1857 Bugs: ProtocolBugs{
1858 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1859 },
1860 },
1861 flags: flags,
1862 })
1863
David Benjamin48cae082014-10-27 01:06:24 -04001864 // Skip ServerKeyExchange in PSK key exchange if there's no
1865 // identity hint.
1866 testCases = append(testCases, testCase{
1867 protocol: protocol,
1868 name: "EmptyPSKHint-Client" + suffix,
1869 config: Config{
1870 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
1871 PreSharedKey: []byte("secret"),
1872 Bugs: ProtocolBugs{
1873 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1874 },
1875 },
1876 flags: append(flags, "-psk", "secret"),
1877 })
1878 testCases = append(testCases, testCase{
1879 protocol: protocol,
1880 testType: serverTest,
1881 name: "EmptyPSKHint-Server" + suffix,
1882 config: Config{
1883 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
1884 PreSharedKey: []byte("secret"),
1885 Bugs: ProtocolBugs{
1886 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1887 },
1888 },
1889 flags: append(flags, "-psk", "secret"),
1890 })
1891
David Benjamin6fd297b2014-08-11 18:43:38 -04001892 if protocol == tls {
1893 // NPN on client and server; results in post-handshake message.
1894 testCases = append(testCases, testCase{
1895 protocol: protocol,
1896 name: "NPN-Client" + suffix,
1897 config: Config{
David Benjaminae2888f2014-09-06 12:58:58 -04001898 NextProtos: []string{"foo"},
David Benjamin6fd297b2014-08-11 18:43:38 -04001899 Bugs: ProtocolBugs{
1900 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1901 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001902 },
David Benjaminfc7b0862014-09-06 13:21:53 -04001903 flags: append(flags, "-select-next-proto", "foo"),
1904 expectedNextProto: "foo",
1905 expectedNextProtoType: npn,
David Benjamin6fd297b2014-08-11 18:43:38 -04001906 })
1907 testCases = append(testCases, testCase{
1908 protocol: protocol,
1909 testType: serverTest,
1910 name: "NPN-Server" + suffix,
1911 config: Config{
1912 NextProtos: []string{"bar"},
1913 Bugs: ProtocolBugs{
1914 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1915 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001916 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001917 flags: append(flags,
1918 "-advertise-npn", "\x03foo\x03bar\x03baz",
1919 "-expect-next-proto", "bar"),
David Benjaminfc7b0862014-09-06 13:21:53 -04001920 expectedNextProto: "bar",
1921 expectedNextProtoType: npn,
David Benjamin6fd297b2014-08-11 18:43:38 -04001922 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001923
David Benjamin195dc782015-02-19 13:27:05 -05001924 // TODO(davidben): Add tests for when False Start doesn't trigger.
1925
David Benjamin6fd297b2014-08-11 18:43:38 -04001926 // Client does False Start and negotiates NPN.
1927 testCases = append(testCases, testCase{
1928 protocol: protocol,
1929 name: "FalseStart" + suffix,
1930 config: Config{
1931 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1932 NextProtos: []string{"foo"},
1933 Bugs: ProtocolBugs{
David Benjamine58c4f52014-08-24 03:47:07 -04001934 ExpectFalseStart: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001935 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1936 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001937 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001938 flags: append(flags,
1939 "-false-start",
1940 "-select-next-proto", "foo"),
David Benjamine58c4f52014-08-24 03:47:07 -04001941 shimWritesFirst: true,
1942 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001943 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001944
David Benjaminae2888f2014-09-06 12:58:58 -04001945 // Client does False Start and negotiates ALPN.
1946 testCases = append(testCases, testCase{
1947 protocol: protocol,
1948 name: "FalseStart-ALPN" + suffix,
1949 config: Config{
1950 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1951 NextProtos: []string{"foo"},
1952 Bugs: ProtocolBugs{
1953 ExpectFalseStart: true,
1954 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1955 },
1956 },
1957 flags: append(flags,
1958 "-false-start",
1959 "-advertise-alpn", "\x03foo"),
1960 shimWritesFirst: true,
1961 resumeSession: true,
1962 })
1963
David Benjamin931ab342015-02-08 19:46:57 -05001964 // Client does False Start but doesn't explicitly call
1965 // SSL_connect.
1966 testCases = append(testCases, testCase{
1967 protocol: protocol,
1968 name: "FalseStart-Implicit" + suffix,
1969 config: Config{
1970 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1971 NextProtos: []string{"foo"},
1972 Bugs: ProtocolBugs{
1973 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1974 },
1975 },
1976 flags: append(flags,
1977 "-implicit-handshake",
1978 "-false-start",
1979 "-advertise-alpn", "\x03foo"),
1980 })
1981
David Benjamin6fd297b2014-08-11 18:43:38 -04001982 // False Start without session tickets.
1983 testCases = append(testCases, testCase{
David Benjamin5f237bc2015-02-11 17:14:15 -05001984 name: "FalseStart-SessionTicketsDisabled" + suffix,
David Benjamin6fd297b2014-08-11 18:43:38 -04001985 config: Config{
1986 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1987 NextProtos: []string{"foo"},
1988 SessionTicketsDisabled: true,
David Benjamin4e99c522014-08-24 01:45:30 -04001989 Bugs: ProtocolBugs{
David Benjamine58c4f52014-08-24 03:47:07 -04001990 ExpectFalseStart: true,
David Benjamin4e99c522014-08-24 01:45:30 -04001991 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1992 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001993 },
David Benjamin4e99c522014-08-24 01:45:30 -04001994 flags: append(flags,
David Benjamin6fd297b2014-08-11 18:43:38 -04001995 "-false-start",
1996 "-select-next-proto", "foo",
David Benjamin4e99c522014-08-24 01:45:30 -04001997 ),
David Benjamine58c4f52014-08-24 03:47:07 -04001998 shimWritesFirst: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001999 })
David Benjamin1e7f8d72014-08-08 12:27:04 -04002000
David Benjamina08e49d2014-08-24 01:46:07 -04002001 // Server parses a V2ClientHello.
David Benjamin6fd297b2014-08-11 18:43:38 -04002002 testCases = append(testCases, testCase{
2003 protocol: protocol,
2004 testType: serverTest,
2005 name: "SendV2ClientHello" + suffix,
2006 config: Config{
2007 // Choose a cipher suite that does not involve
2008 // elliptic curves, so no extensions are
2009 // involved.
2010 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2011 Bugs: ProtocolBugs{
2012 MaxHandshakeRecordLength: maxHandshakeRecordLength,
2013 SendV2ClientHello: true,
2014 },
David Benjamin1e7f8d72014-08-08 12:27:04 -04002015 },
David Benjamin6fd297b2014-08-11 18:43:38 -04002016 flags: flags,
2017 })
David Benjamina08e49d2014-08-24 01:46:07 -04002018
2019 // Client sends a Channel ID.
2020 testCases = append(testCases, testCase{
2021 protocol: protocol,
2022 name: "ChannelID-Client" + suffix,
2023 config: Config{
2024 RequestChannelID: true,
2025 Bugs: ProtocolBugs{
2026 MaxHandshakeRecordLength: maxHandshakeRecordLength,
2027 },
2028 },
2029 flags: append(flags,
2030 "-send-channel-id", channelIDKeyFile,
2031 ),
2032 resumeSession: true,
2033 expectChannelID: true,
2034 })
2035
2036 // Server accepts a Channel ID.
2037 testCases = append(testCases, testCase{
2038 protocol: protocol,
2039 testType: serverTest,
2040 name: "ChannelID-Server" + suffix,
2041 config: Config{
2042 ChannelID: channelIDKey,
2043 Bugs: ProtocolBugs{
2044 MaxHandshakeRecordLength: maxHandshakeRecordLength,
2045 },
2046 },
2047 flags: append(flags,
2048 "-expect-channel-id",
2049 base64.StdEncoding.EncodeToString(channelIDBytes),
2050 ),
2051 resumeSession: true,
2052 expectChannelID: true,
2053 })
David Benjamin6fd297b2014-08-11 18:43:38 -04002054 } else {
2055 testCases = append(testCases, testCase{
2056 protocol: protocol,
2057 name: "SkipHelloVerifyRequest" + suffix,
2058 config: Config{
2059 Bugs: ProtocolBugs{
2060 MaxHandshakeRecordLength: maxHandshakeRecordLength,
2061 SkipHelloVerifyRequest: true,
2062 },
2063 },
2064 flags: flags,
2065 })
David Benjamin6fd297b2014-08-11 18:43:38 -04002066 }
David Benjamin43ec06f2014-08-05 02:28:57 -04002067}
2068
Adam Langley524e7172015-02-20 16:04:00 -08002069func addDDoSCallbackTests() {
2070 // DDoS callback.
2071
2072 for _, resume := range []bool{false, true} {
2073 suffix := "Resume"
2074 if resume {
2075 suffix = "No" + suffix
2076 }
2077
2078 testCases = append(testCases, testCase{
2079 testType: serverTest,
2080 name: "Server-DDoS-OK-" + suffix,
2081 flags: []string{"-install-ddos-callback"},
2082 resumeSession: resume,
2083 })
2084
2085 failFlag := "-fail-ddos-callback"
2086 if resume {
2087 failFlag = "-fail-second-ddos-callback"
2088 }
2089 testCases = append(testCases, testCase{
2090 testType: serverTest,
2091 name: "Server-DDoS-Reject-" + suffix,
2092 flags: []string{"-install-ddos-callback", failFlag},
2093 resumeSession: resume,
2094 shouldFail: true,
2095 expectedError: ":CONNECTION_REJECTED:",
2096 })
2097 }
2098}
2099
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002100func addVersionNegotiationTests() {
2101 for i, shimVers := range tlsVersions {
2102 // Assemble flags to disable all newer versions on the shim.
2103 var flags []string
2104 for _, vers := range tlsVersions[i+1:] {
2105 flags = append(flags, vers.flag)
2106 }
2107
2108 for _, runnerVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05002109 protocols := []protocol{tls}
2110 if runnerVers.hasDTLS && shimVers.hasDTLS {
2111 protocols = append(protocols, dtls)
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002112 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002113 for _, protocol := range protocols {
2114 expectedVersion := shimVers.version
2115 if runnerVers.version < shimVers.version {
2116 expectedVersion = runnerVers.version
2117 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002118
David Benjamin8b8c0062014-11-23 02:47:52 -05002119 suffix := shimVers.name + "-" + runnerVers.name
2120 if protocol == dtls {
2121 suffix += "-DTLS"
2122 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002123
David Benjamin1eb367c2014-12-12 18:17:51 -05002124 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
2125
David Benjamin1e29a6b2014-12-10 02:27:24 -05002126 clientVers := shimVers.version
2127 if clientVers > VersionTLS10 {
2128 clientVers = VersionTLS10
2129 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002130 testCases = append(testCases, testCase{
2131 protocol: protocol,
2132 testType: clientTest,
2133 name: "VersionNegotiation-Client-" + suffix,
2134 config: Config{
2135 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05002136 Bugs: ProtocolBugs{
2137 ExpectInitialRecordVersion: clientVers,
2138 },
David Benjamin8b8c0062014-11-23 02:47:52 -05002139 },
2140 flags: flags,
2141 expectedVersion: expectedVersion,
2142 })
David Benjamin1eb367c2014-12-12 18:17:51 -05002143 testCases = append(testCases, testCase{
2144 protocol: protocol,
2145 testType: clientTest,
2146 name: "VersionNegotiation-Client2-" + suffix,
2147 config: Config{
2148 MaxVersion: runnerVers.version,
2149 Bugs: ProtocolBugs{
2150 ExpectInitialRecordVersion: clientVers,
2151 },
2152 },
2153 flags: []string{"-max-version", shimVersFlag},
2154 expectedVersion: expectedVersion,
2155 })
David Benjamin8b8c0062014-11-23 02:47:52 -05002156
2157 testCases = append(testCases, testCase{
2158 protocol: protocol,
2159 testType: serverTest,
2160 name: "VersionNegotiation-Server-" + suffix,
2161 config: Config{
2162 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05002163 Bugs: ProtocolBugs{
2164 ExpectInitialRecordVersion: expectedVersion,
2165 },
David Benjamin8b8c0062014-11-23 02:47:52 -05002166 },
2167 flags: flags,
2168 expectedVersion: expectedVersion,
2169 })
David Benjamin1eb367c2014-12-12 18:17:51 -05002170 testCases = append(testCases, testCase{
2171 protocol: protocol,
2172 testType: serverTest,
2173 name: "VersionNegotiation-Server2-" + suffix,
2174 config: Config{
2175 MaxVersion: runnerVers.version,
2176 Bugs: ProtocolBugs{
2177 ExpectInitialRecordVersion: expectedVersion,
2178 },
2179 },
2180 flags: []string{"-max-version", shimVersFlag},
2181 expectedVersion: expectedVersion,
2182 })
David Benjamin8b8c0062014-11-23 02:47:52 -05002183 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002184 }
2185 }
2186}
2187
David Benjaminaccb4542014-12-12 23:44:33 -05002188func addMinimumVersionTests() {
2189 for i, shimVers := range tlsVersions {
2190 // Assemble flags to disable all older versions on the shim.
2191 var flags []string
2192 for _, vers := range tlsVersions[:i] {
2193 flags = append(flags, vers.flag)
2194 }
2195
2196 for _, runnerVers := range tlsVersions {
2197 protocols := []protocol{tls}
2198 if runnerVers.hasDTLS && shimVers.hasDTLS {
2199 protocols = append(protocols, dtls)
2200 }
2201 for _, protocol := range protocols {
2202 suffix := shimVers.name + "-" + runnerVers.name
2203 if protocol == dtls {
2204 suffix += "-DTLS"
2205 }
2206 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
2207
David Benjaminaccb4542014-12-12 23:44:33 -05002208 var expectedVersion uint16
2209 var shouldFail bool
2210 var expectedError string
David Benjamin87909c02014-12-13 01:55:01 -05002211 var expectedLocalError string
David Benjaminaccb4542014-12-12 23:44:33 -05002212 if runnerVers.version >= shimVers.version {
2213 expectedVersion = runnerVers.version
2214 } else {
2215 shouldFail = true
2216 expectedError = ":UNSUPPORTED_PROTOCOL:"
David Benjamin87909c02014-12-13 01:55:01 -05002217 if runnerVers.version > VersionSSL30 {
2218 expectedLocalError = "remote error: protocol version not supported"
2219 } else {
2220 expectedLocalError = "remote error: handshake failure"
2221 }
David Benjaminaccb4542014-12-12 23:44:33 -05002222 }
2223
2224 testCases = append(testCases, testCase{
2225 protocol: protocol,
2226 testType: clientTest,
2227 name: "MinimumVersion-Client-" + suffix,
2228 config: Config{
2229 MaxVersion: runnerVers.version,
2230 },
David Benjamin87909c02014-12-13 01:55:01 -05002231 flags: flags,
2232 expectedVersion: expectedVersion,
2233 shouldFail: shouldFail,
2234 expectedError: expectedError,
2235 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002236 })
2237 testCases = append(testCases, testCase{
2238 protocol: protocol,
2239 testType: clientTest,
2240 name: "MinimumVersion-Client2-" + suffix,
2241 config: Config{
2242 MaxVersion: runnerVers.version,
2243 },
David Benjamin87909c02014-12-13 01:55:01 -05002244 flags: []string{"-min-version", shimVersFlag},
2245 expectedVersion: expectedVersion,
2246 shouldFail: shouldFail,
2247 expectedError: expectedError,
2248 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002249 })
2250
2251 testCases = append(testCases, testCase{
2252 protocol: protocol,
2253 testType: serverTest,
2254 name: "MinimumVersion-Server-" + suffix,
2255 config: Config{
2256 MaxVersion: runnerVers.version,
2257 },
David Benjamin87909c02014-12-13 01:55:01 -05002258 flags: flags,
2259 expectedVersion: expectedVersion,
2260 shouldFail: shouldFail,
2261 expectedError: expectedError,
2262 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002263 })
2264 testCases = append(testCases, testCase{
2265 protocol: protocol,
2266 testType: serverTest,
2267 name: "MinimumVersion-Server2-" + suffix,
2268 config: Config{
2269 MaxVersion: runnerVers.version,
2270 },
David Benjamin87909c02014-12-13 01:55:01 -05002271 flags: []string{"-min-version", shimVersFlag},
2272 expectedVersion: expectedVersion,
2273 shouldFail: shouldFail,
2274 expectedError: expectedError,
2275 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002276 })
2277 }
2278 }
2279 }
2280}
2281
David Benjamin5c24a1d2014-08-31 00:59:27 -04002282func addD5BugTests() {
2283 testCases = append(testCases, testCase{
2284 testType: serverTest,
2285 name: "D5Bug-NoQuirk-Reject",
2286 config: Config{
2287 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2288 Bugs: ProtocolBugs{
2289 SSL3RSAKeyExchange: true,
2290 },
2291 },
2292 shouldFail: true,
2293 expectedError: ":TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG:",
2294 })
2295 testCases = append(testCases, testCase{
2296 testType: serverTest,
2297 name: "D5Bug-Quirk-Normal",
2298 config: Config{
2299 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2300 },
2301 flags: []string{"-tls-d5-bug"},
2302 })
2303 testCases = append(testCases, testCase{
2304 testType: serverTest,
2305 name: "D5Bug-Quirk-Bug",
2306 config: Config{
2307 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2308 Bugs: ProtocolBugs{
2309 SSL3RSAKeyExchange: true,
2310 },
2311 },
2312 flags: []string{"-tls-d5-bug"},
2313 })
2314}
2315
David Benjamine78bfde2014-09-06 12:45:15 -04002316func addExtensionTests() {
2317 testCases = append(testCases, testCase{
2318 testType: clientTest,
2319 name: "DuplicateExtensionClient",
2320 config: Config{
2321 Bugs: ProtocolBugs{
2322 DuplicateExtension: true,
2323 },
2324 },
2325 shouldFail: true,
2326 expectedLocalError: "remote error: error decoding message",
2327 })
2328 testCases = append(testCases, testCase{
2329 testType: serverTest,
2330 name: "DuplicateExtensionServer",
2331 config: Config{
2332 Bugs: ProtocolBugs{
2333 DuplicateExtension: true,
2334 },
2335 },
2336 shouldFail: true,
2337 expectedLocalError: "remote error: error decoding message",
2338 })
2339 testCases = append(testCases, testCase{
2340 testType: clientTest,
2341 name: "ServerNameExtensionClient",
2342 config: Config{
2343 Bugs: ProtocolBugs{
2344 ExpectServerName: "example.com",
2345 },
2346 },
2347 flags: []string{"-host-name", "example.com"},
2348 })
2349 testCases = append(testCases, testCase{
2350 testType: clientTest,
David Benjamin5f237bc2015-02-11 17:14:15 -05002351 name: "ServerNameExtensionClientMismatch",
David Benjamine78bfde2014-09-06 12:45:15 -04002352 config: Config{
2353 Bugs: ProtocolBugs{
2354 ExpectServerName: "mismatch.com",
2355 },
2356 },
2357 flags: []string{"-host-name", "example.com"},
2358 shouldFail: true,
2359 expectedLocalError: "tls: unexpected server name",
2360 })
2361 testCases = append(testCases, testCase{
2362 testType: clientTest,
David Benjamin5f237bc2015-02-11 17:14:15 -05002363 name: "ServerNameExtensionClientMissing",
David Benjamine78bfde2014-09-06 12:45:15 -04002364 config: Config{
2365 Bugs: ProtocolBugs{
2366 ExpectServerName: "missing.com",
2367 },
2368 },
2369 shouldFail: true,
2370 expectedLocalError: "tls: unexpected server name",
2371 })
2372 testCases = append(testCases, testCase{
2373 testType: serverTest,
2374 name: "ServerNameExtensionServer",
2375 config: Config{
2376 ServerName: "example.com",
2377 },
2378 flags: []string{"-expect-server-name", "example.com"},
2379 resumeSession: true,
2380 })
David Benjaminae2888f2014-09-06 12:58:58 -04002381 testCases = append(testCases, testCase{
2382 testType: clientTest,
2383 name: "ALPNClient",
2384 config: Config{
2385 NextProtos: []string{"foo"},
2386 },
2387 flags: []string{
2388 "-advertise-alpn", "\x03foo\x03bar\x03baz",
2389 "-expect-alpn", "foo",
2390 },
David Benjaminfc7b0862014-09-06 13:21:53 -04002391 expectedNextProto: "foo",
2392 expectedNextProtoType: alpn,
2393 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04002394 })
2395 testCases = append(testCases, testCase{
2396 testType: serverTest,
2397 name: "ALPNServer",
2398 config: Config{
2399 NextProtos: []string{"foo", "bar", "baz"},
2400 },
2401 flags: []string{
2402 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2403 "-select-alpn", "foo",
2404 },
David Benjaminfc7b0862014-09-06 13:21:53 -04002405 expectedNextProto: "foo",
2406 expectedNextProtoType: alpn,
2407 resumeSession: true,
2408 })
2409 // Test that the server prefers ALPN over NPN.
2410 testCases = append(testCases, testCase{
2411 testType: serverTest,
2412 name: "ALPNServer-Preferred",
2413 config: Config{
2414 NextProtos: []string{"foo", "bar", "baz"},
2415 },
2416 flags: []string{
2417 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2418 "-select-alpn", "foo",
2419 "-advertise-npn", "\x03foo\x03bar\x03baz",
2420 },
2421 expectedNextProto: "foo",
2422 expectedNextProtoType: alpn,
2423 resumeSession: true,
2424 })
2425 testCases = append(testCases, testCase{
2426 testType: serverTest,
2427 name: "ALPNServer-Preferred-Swapped",
2428 config: Config{
2429 NextProtos: []string{"foo", "bar", "baz"},
2430 Bugs: ProtocolBugs{
2431 SwapNPNAndALPN: true,
2432 },
2433 },
2434 flags: []string{
2435 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2436 "-select-alpn", "foo",
2437 "-advertise-npn", "\x03foo\x03bar\x03baz",
2438 },
2439 expectedNextProto: "foo",
2440 expectedNextProtoType: alpn,
2441 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04002442 })
Adam Langley38311732014-10-16 19:04:35 -07002443 // Resume with a corrupt ticket.
2444 testCases = append(testCases, testCase{
2445 testType: serverTest,
2446 name: "CorruptTicket",
2447 config: Config{
2448 Bugs: ProtocolBugs{
2449 CorruptTicket: true,
2450 },
2451 },
2452 resumeSession: true,
2453 flags: []string{"-expect-session-miss"},
2454 })
2455 // Resume with an oversized session id.
2456 testCases = append(testCases, testCase{
2457 testType: serverTest,
2458 name: "OversizedSessionId",
2459 config: Config{
2460 Bugs: ProtocolBugs{
2461 OversizedSessionId: true,
2462 },
2463 },
2464 resumeSession: true,
Adam Langley75712922014-10-10 16:23:43 -07002465 shouldFail: true,
Adam Langley38311732014-10-16 19:04:35 -07002466 expectedError: ":DECODE_ERROR:",
2467 })
David Benjaminca6c8262014-11-15 19:06:08 -05002468 // Basic DTLS-SRTP tests. Include fake profiles to ensure they
2469 // are ignored.
2470 testCases = append(testCases, testCase{
2471 protocol: dtls,
2472 name: "SRTP-Client",
2473 config: Config{
2474 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
2475 },
2476 flags: []string{
2477 "-srtp-profiles",
2478 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2479 },
2480 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2481 })
2482 testCases = append(testCases, testCase{
2483 protocol: dtls,
2484 testType: serverTest,
2485 name: "SRTP-Server",
2486 config: Config{
2487 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
2488 },
2489 flags: []string{
2490 "-srtp-profiles",
2491 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2492 },
2493 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2494 })
2495 // Test that the MKI is ignored.
2496 testCases = append(testCases, testCase{
2497 protocol: dtls,
2498 testType: serverTest,
2499 name: "SRTP-Server-IgnoreMKI",
2500 config: Config{
2501 SRTPProtectionProfiles: []uint16{SRTP_AES128_CM_HMAC_SHA1_80},
2502 Bugs: ProtocolBugs{
2503 SRTPMasterKeyIdentifer: "bogus",
2504 },
2505 },
2506 flags: []string{
2507 "-srtp-profiles",
2508 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2509 },
2510 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2511 })
2512 // Test that SRTP isn't negotiated on the server if there were
2513 // no matching profiles.
2514 testCases = append(testCases, testCase{
2515 protocol: dtls,
2516 testType: serverTest,
2517 name: "SRTP-Server-NoMatch",
2518 config: Config{
2519 SRTPProtectionProfiles: []uint16{100, 101, 102},
2520 },
2521 flags: []string{
2522 "-srtp-profiles",
2523 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2524 },
2525 expectedSRTPProtectionProfile: 0,
2526 })
2527 // Test that the server returning an invalid SRTP profile is
2528 // flagged as an error by the client.
2529 testCases = append(testCases, testCase{
2530 protocol: dtls,
2531 name: "SRTP-Client-NoMatch",
2532 config: Config{
2533 Bugs: ProtocolBugs{
2534 SendSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_32,
2535 },
2536 },
2537 flags: []string{
2538 "-srtp-profiles",
2539 "SRTP_AES128_CM_SHA1_80",
2540 },
2541 shouldFail: true,
2542 expectedError: ":BAD_SRTP_PROTECTION_PROFILE_LIST:",
2543 })
David Benjamin61f95272014-11-25 01:55:35 -05002544 // Test OCSP stapling and SCT list.
2545 testCases = append(testCases, testCase{
2546 name: "OCSPStapling",
2547 flags: []string{
2548 "-enable-ocsp-stapling",
2549 "-expect-ocsp-response",
2550 base64.StdEncoding.EncodeToString(testOCSPResponse),
2551 },
2552 })
2553 testCases = append(testCases, testCase{
2554 name: "SignedCertificateTimestampList",
2555 flags: []string{
2556 "-enable-signed-cert-timestamps",
2557 "-expect-signed-cert-timestamps",
2558 base64.StdEncoding.EncodeToString(testSCTList),
2559 },
2560 })
David Benjamine78bfde2014-09-06 12:45:15 -04002561}
2562
David Benjamin01fe8202014-09-24 15:21:44 -04002563func addResumptionVersionTests() {
David Benjamin01fe8202014-09-24 15:21:44 -04002564 for _, sessionVers := range tlsVersions {
David Benjamin01fe8202014-09-24 15:21:44 -04002565 for _, resumeVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05002566 protocols := []protocol{tls}
2567 if sessionVers.hasDTLS && resumeVers.hasDTLS {
2568 protocols = append(protocols, dtls)
David Benjaminbdf5e722014-11-11 00:52:15 -05002569 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002570 for _, protocol := range protocols {
2571 suffix := "-" + sessionVers.name + "-" + resumeVers.name
2572 if protocol == dtls {
2573 suffix += "-DTLS"
2574 }
2575
2576 testCases = append(testCases, testCase{
2577 protocol: protocol,
2578 name: "Resume-Client" + suffix,
2579 resumeSession: true,
2580 config: Config{
2581 MaxVersion: sessionVers.version,
2582 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2583 Bugs: ProtocolBugs{
2584 AllowSessionVersionMismatch: true,
2585 },
2586 },
2587 expectedVersion: sessionVers.version,
2588 resumeConfig: &Config{
2589 MaxVersion: resumeVers.version,
2590 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2591 Bugs: ProtocolBugs{
2592 AllowSessionVersionMismatch: true,
2593 },
2594 },
2595 expectedResumeVersion: resumeVers.version,
2596 })
2597
2598 testCases = append(testCases, testCase{
2599 protocol: protocol,
2600 name: "Resume-Client-NoResume" + suffix,
2601 flags: []string{"-expect-session-miss"},
2602 resumeSession: true,
2603 config: Config{
2604 MaxVersion: sessionVers.version,
2605 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2606 },
2607 expectedVersion: sessionVers.version,
2608 resumeConfig: &Config{
2609 MaxVersion: resumeVers.version,
2610 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2611 },
2612 newSessionsOnResume: true,
2613 expectedResumeVersion: resumeVers.version,
2614 })
2615
2616 var flags []string
2617 if sessionVers.version != resumeVers.version {
2618 flags = append(flags, "-expect-session-miss")
2619 }
2620 testCases = append(testCases, testCase{
2621 protocol: protocol,
2622 testType: serverTest,
2623 name: "Resume-Server" + suffix,
2624 flags: flags,
2625 resumeSession: true,
2626 config: Config{
2627 MaxVersion: sessionVers.version,
2628 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2629 },
2630 expectedVersion: sessionVers.version,
2631 resumeConfig: &Config{
2632 MaxVersion: resumeVers.version,
2633 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2634 },
2635 expectedResumeVersion: resumeVers.version,
2636 })
2637 }
David Benjamin01fe8202014-09-24 15:21:44 -04002638 }
2639 }
2640}
2641
Adam Langley2ae77d22014-10-28 17:29:33 -07002642func addRenegotiationTests() {
2643 testCases = append(testCases, testCase{
2644 testType: serverTest,
2645 name: "Renegotiate-Server",
2646 flags: []string{"-renegotiate"},
2647 shimWritesFirst: true,
2648 })
2649 testCases = append(testCases, testCase{
2650 testType: serverTest,
David Benjamincdea40c2015-03-19 14:09:43 -04002651 name: "Renegotiate-Server-Full",
2652 config: Config{
2653 Bugs: ProtocolBugs{
2654 NeverResumeOnRenego: true,
2655 },
2656 },
2657 flags: []string{"-renegotiate"},
2658 shimWritesFirst: true,
2659 })
2660 testCases = append(testCases, testCase{
2661 testType: serverTest,
Adam Langley2ae77d22014-10-28 17:29:33 -07002662 name: "Renegotiate-Server-EmptyExt",
2663 config: Config{
2664 Bugs: ProtocolBugs{
2665 EmptyRenegotiationInfo: true,
2666 },
2667 },
2668 flags: []string{"-renegotiate"},
2669 shimWritesFirst: true,
2670 shouldFail: true,
2671 expectedError: ":RENEGOTIATION_MISMATCH:",
2672 })
2673 testCases = append(testCases, testCase{
2674 testType: serverTest,
2675 name: "Renegotiate-Server-BadExt",
2676 config: Config{
2677 Bugs: ProtocolBugs{
2678 BadRenegotiationInfo: true,
2679 },
2680 },
2681 flags: []string{"-renegotiate"},
2682 shimWritesFirst: true,
2683 shouldFail: true,
2684 expectedError: ":RENEGOTIATION_MISMATCH:",
2685 })
David Benjaminca6554b2014-11-08 12:31:52 -05002686 testCases = append(testCases, testCase{
2687 testType: serverTest,
2688 name: "Renegotiate-Server-ClientInitiated",
2689 renegotiate: true,
2690 })
2691 testCases = append(testCases, testCase{
2692 testType: serverTest,
2693 name: "Renegotiate-Server-ClientInitiated-NoExt",
2694 renegotiate: true,
2695 config: Config{
2696 Bugs: ProtocolBugs{
2697 NoRenegotiationInfo: true,
2698 },
2699 },
2700 shouldFail: true,
2701 expectedError: ":UNSAFE_LEGACY_RENEGOTIATION_DISABLED:",
2702 })
2703 testCases = append(testCases, testCase{
2704 testType: serverTest,
2705 name: "Renegotiate-Server-ClientInitiated-NoExt-Allowed",
2706 renegotiate: true,
2707 config: Config{
2708 Bugs: ProtocolBugs{
2709 NoRenegotiationInfo: true,
2710 },
2711 },
2712 flags: []string{"-allow-unsafe-legacy-renegotiation"},
2713 })
David Benjamin3c9746a2015-03-19 15:00:10 -04002714 // Regression test for CVE-2015-0291.
2715 testCases = append(testCases, testCase{
2716 testType: serverTest,
2717 name: "Renegotiate-Server-NoSignatureAlgorithms",
2718 config: Config{
2719 Bugs: ProtocolBugs{
2720 NeverResumeOnRenego: true,
2721 NoSignatureAlgorithmsOnRenego: true,
2722 },
2723 },
2724 flags: []string{"-renegotiate"},
2725 shimWritesFirst: true,
2726 })
Adam Langley2ae77d22014-10-28 17:29:33 -07002727 // TODO(agl): test the renegotiation info SCSV.
Adam Langleycf2d4f42014-10-28 19:06:14 -07002728 testCases = append(testCases, testCase{
2729 name: "Renegotiate-Client",
2730 renegotiate: true,
2731 })
2732 testCases = append(testCases, testCase{
David Benjamincdea40c2015-03-19 14:09:43 -04002733 name: "Renegotiate-Client-Full",
2734 config: Config{
2735 Bugs: ProtocolBugs{
2736 NeverResumeOnRenego: true,
2737 },
2738 },
2739 renegotiate: true,
2740 })
2741 testCases = append(testCases, testCase{
Adam Langleycf2d4f42014-10-28 19:06:14 -07002742 name: "Renegotiate-Client-EmptyExt",
2743 renegotiate: true,
2744 config: Config{
2745 Bugs: ProtocolBugs{
2746 EmptyRenegotiationInfo: true,
2747 },
2748 },
2749 shouldFail: true,
2750 expectedError: ":RENEGOTIATION_MISMATCH:",
2751 })
2752 testCases = append(testCases, testCase{
2753 name: "Renegotiate-Client-BadExt",
2754 renegotiate: true,
2755 config: Config{
2756 Bugs: ProtocolBugs{
2757 BadRenegotiationInfo: true,
2758 },
2759 },
2760 shouldFail: true,
2761 expectedError: ":RENEGOTIATION_MISMATCH:",
2762 })
2763 testCases = append(testCases, testCase{
2764 name: "Renegotiate-Client-SwitchCiphers",
2765 renegotiate: true,
2766 config: Config{
2767 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2768 },
2769 renegotiateCiphers: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2770 })
2771 testCases = append(testCases, testCase{
2772 name: "Renegotiate-Client-SwitchCiphers2",
2773 renegotiate: true,
2774 config: Config{
2775 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2776 },
2777 renegotiateCiphers: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2778 })
David Benjaminc44b1df2014-11-23 12:11:01 -05002779 testCases = append(testCases, testCase{
2780 name: "Renegotiate-SameClientVersion",
2781 renegotiate: true,
2782 config: Config{
2783 MaxVersion: VersionTLS10,
2784 Bugs: ProtocolBugs{
2785 RequireSameRenegoClientVersion: true,
2786 },
2787 },
2788 })
Adam Langley2ae77d22014-10-28 17:29:33 -07002789}
2790
David Benjamin5e961c12014-11-07 01:48:35 -05002791func addDTLSReplayTests() {
2792 // Test that sequence number replays are detected.
2793 testCases = append(testCases, testCase{
2794 protocol: dtls,
2795 name: "DTLS-Replay",
2796 replayWrites: true,
2797 })
2798
2799 // Test the outgoing sequence number skipping by values larger
2800 // than the retransmit window.
2801 testCases = append(testCases, testCase{
2802 protocol: dtls,
2803 name: "DTLS-Replay-LargeGaps",
2804 config: Config{
2805 Bugs: ProtocolBugs{
2806 SequenceNumberIncrement: 127,
2807 },
2808 },
2809 replayWrites: true,
2810 })
2811}
2812
Feng Lu41aa3252014-11-21 22:47:56 -08002813func addFastRadioPaddingTests() {
David Benjamin1e29a6b2014-12-10 02:27:24 -05002814 testCases = append(testCases, testCase{
2815 protocol: tls,
2816 name: "FastRadio-Padding",
Feng Lu41aa3252014-11-21 22:47:56 -08002817 config: Config{
2818 Bugs: ProtocolBugs{
2819 RequireFastradioPadding: true,
2820 },
2821 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05002822 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08002823 })
David Benjamin1e29a6b2014-12-10 02:27:24 -05002824 testCases = append(testCases, testCase{
2825 protocol: dtls,
David Benjamin5f237bc2015-02-11 17:14:15 -05002826 name: "FastRadio-Padding-DTLS",
Feng Lu41aa3252014-11-21 22:47:56 -08002827 config: Config{
2828 Bugs: ProtocolBugs{
2829 RequireFastradioPadding: true,
2830 },
2831 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05002832 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08002833 })
2834}
2835
David Benjamin000800a2014-11-14 01:43:59 -05002836var testHashes = []struct {
2837 name string
2838 id uint8
2839}{
2840 {"SHA1", hashSHA1},
2841 {"SHA224", hashSHA224},
2842 {"SHA256", hashSHA256},
2843 {"SHA384", hashSHA384},
2844 {"SHA512", hashSHA512},
2845}
2846
2847func addSigningHashTests() {
2848 // Make sure each hash works. Include some fake hashes in the list and
2849 // ensure they're ignored.
2850 for _, hash := range testHashes {
2851 testCases = append(testCases, testCase{
2852 name: "SigningHash-ClientAuth-" + hash.name,
2853 config: Config{
2854 ClientAuth: RequireAnyClientCert,
2855 SignatureAndHashes: []signatureAndHash{
2856 {signatureRSA, 42},
2857 {signatureRSA, hash.id},
2858 {signatureRSA, 255},
2859 },
2860 },
2861 flags: []string{
2862 "-cert-file", rsaCertificateFile,
2863 "-key-file", rsaKeyFile,
2864 },
2865 })
2866
2867 testCases = append(testCases, testCase{
2868 testType: serverTest,
2869 name: "SigningHash-ServerKeyExchange-Sign-" + hash.name,
2870 config: Config{
2871 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2872 SignatureAndHashes: []signatureAndHash{
2873 {signatureRSA, 42},
2874 {signatureRSA, hash.id},
2875 {signatureRSA, 255},
2876 },
2877 },
2878 })
2879 }
2880
2881 // Test that hash resolution takes the signature type into account.
2882 testCases = append(testCases, testCase{
2883 name: "SigningHash-ClientAuth-SignatureType",
2884 config: Config{
2885 ClientAuth: RequireAnyClientCert,
2886 SignatureAndHashes: []signatureAndHash{
2887 {signatureECDSA, hashSHA512},
2888 {signatureRSA, hashSHA384},
2889 {signatureECDSA, hashSHA1},
2890 },
2891 },
2892 flags: []string{
2893 "-cert-file", rsaCertificateFile,
2894 "-key-file", rsaKeyFile,
2895 },
2896 })
2897
2898 testCases = append(testCases, testCase{
2899 testType: serverTest,
2900 name: "SigningHash-ServerKeyExchange-SignatureType",
2901 config: Config{
2902 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2903 SignatureAndHashes: []signatureAndHash{
2904 {signatureECDSA, hashSHA512},
2905 {signatureRSA, hashSHA384},
2906 {signatureECDSA, hashSHA1},
2907 },
2908 },
2909 })
2910
2911 // Test that, if the list is missing, the peer falls back to SHA-1.
2912 testCases = append(testCases, testCase{
2913 name: "SigningHash-ClientAuth-Fallback",
2914 config: Config{
2915 ClientAuth: RequireAnyClientCert,
2916 SignatureAndHashes: []signatureAndHash{
2917 {signatureRSA, hashSHA1},
2918 },
2919 Bugs: ProtocolBugs{
2920 NoSignatureAndHashes: true,
2921 },
2922 },
2923 flags: []string{
2924 "-cert-file", rsaCertificateFile,
2925 "-key-file", rsaKeyFile,
2926 },
2927 })
2928
2929 testCases = append(testCases, testCase{
2930 testType: serverTest,
2931 name: "SigningHash-ServerKeyExchange-Fallback",
2932 config: Config{
2933 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2934 SignatureAndHashes: []signatureAndHash{
2935 {signatureRSA, hashSHA1},
2936 },
2937 Bugs: ProtocolBugs{
2938 NoSignatureAndHashes: true,
2939 },
2940 },
2941 })
David Benjamin72dc7832015-03-16 17:49:43 -04002942
2943 // Test that hash preferences are enforced. BoringSSL defaults to
2944 // rejecting MD5 signatures.
2945 testCases = append(testCases, testCase{
2946 testType: serverTest,
2947 name: "SigningHash-ClientAuth-Enforced",
2948 config: Config{
2949 Certificates: []Certificate{rsaCertificate},
2950 SignatureAndHashes: []signatureAndHash{
2951 {signatureRSA, hashMD5},
2952 // Advertise SHA-1 so the handshake will
2953 // proceed, but the shim's preferences will be
2954 // ignored in CertificateVerify generation, so
2955 // MD5 will be chosen.
2956 {signatureRSA, hashSHA1},
2957 },
2958 Bugs: ProtocolBugs{
2959 IgnorePeerSignatureAlgorithmPreferences: true,
2960 },
2961 },
2962 flags: []string{"-require-any-client-certificate"},
2963 shouldFail: true,
2964 expectedError: ":WRONG_SIGNATURE_TYPE:",
2965 })
2966
2967 testCases = append(testCases, testCase{
2968 name: "SigningHash-ServerKeyExchange-Enforced",
2969 config: Config{
2970 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2971 SignatureAndHashes: []signatureAndHash{
2972 {signatureRSA, hashMD5},
2973 },
2974 Bugs: ProtocolBugs{
2975 IgnorePeerSignatureAlgorithmPreferences: true,
2976 },
2977 },
2978 shouldFail: true,
2979 expectedError: ":WRONG_SIGNATURE_TYPE:",
2980 })
David Benjamin000800a2014-11-14 01:43:59 -05002981}
2982
David Benjamin83f90402015-01-27 01:09:43 -05002983// timeouts is the retransmit schedule for BoringSSL. It doubles and
2984// caps at 60 seconds. On the 13th timeout, it gives up.
2985var timeouts = []time.Duration{
2986 1 * time.Second,
2987 2 * time.Second,
2988 4 * time.Second,
2989 8 * time.Second,
2990 16 * time.Second,
2991 32 * time.Second,
2992 60 * time.Second,
2993 60 * time.Second,
2994 60 * time.Second,
2995 60 * time.Second,
2996 60 * time.Second,
2997 60 * time.Second,
2998 60 * time.Second,
2999}
3000
3001func addDTLSRetransmitTests() {
3002 // Test that this is indeed the timeout schedule. Stress all
3003 // four patterns of handshake.
3004 for i := 1; i < len(timeouts); i++ {
3005 number := strconv.Itoa(i)
3006 testCases = append(testCases, testCase{
3007 protocol: dtls,
3008 name: "DTLS-Retransmit-Client-" + number,
3009 config: Config{
3010 Bugs: ProtocolBugs{
3011 TimeoutSchedule: timeouts[:i],
3012 },
3013 },
3014 resumeSession: true,
3015 flags: []string{"-async"},
3016 })
3017 testCases = append(testCases, testCase{
3018 protocol: dtls,
3019 testType: serverTest,
3020 name: "DTLS-Retransmit-Server-" + number,
3021 config: Config{
3022 Bugs: ProtocolBugs{
3023 TimeoutSchedule: timeouts[:i],
3024 },
3025 },
3026 resumeSession: true,
3027 flags: []string{"-async"},
3028 })
3029 }
3030
3031 // Test that exceeding the timeout schedule hits a read
3032 // timeout.
3033 testCases = append(testCases, testCase{
3034 protocol: dtls,
3035 name: "DTLS-Retransmit-Timeout",
3036 config: Config{
3037 Bugs: ProtocolBugs{
3038 TimeoutSchedule: timeouts,
3039 },
3040 },
3041 resumeSession: true,
3042 flags: []string{"-async"},
3043 shouldFail: true,
3044 expectedError: ":READ_TIMEOUT_EXPIRED:",
3045 })
3046
3047 // Test that timeout handling has a fudge factor, due to API
3048 // problems.
3049 testCases = append(testCases, testCase{
3050 protocol: dtls,
3051 name: "DTLS-Retransmit-Fudge",
3052 config: Config{
3053 Bugs: ProtocolBugs{
3054 TimeoutSchedule: []time.Duration{
3055 timeouts[0] - 10*time.Millisecond,
3056 },
3057 },
3058 },
3059 resumeSession: true,
3060 flags: []string{"-async"},
3061 })
David Benjamin7eaab4c2015-03-02 19:01:16 -05003062
3063 // Test that the final Finished retransmitting isn't
3064 // duplicated if the peer badly fragments everything.
3065 testCases = append(testCases, testCase{
3066 testType: serverTest,
3067 protocol: dtls,
3068 name: "DTLS-Retransmit-Fragmented",
3069 config: Config{
3070 Bugs: ProtocolBugs{
3071 TimeoutSchedule: []time.Duration{timeouts[0]},
3072 MaxHandshakeRecordLength: 2,
3073 },
3074 },
3075 flags: []string{"-async"},
3076 })
David Benjamin83f90402015-01-27 01:09:43 -05003077}
3078
David Benjamin884fdf12014-08-02 15:28:23 -04003079func worker(statusChan chan statusMsg, c chan *testCase, buildDir string, wg *sync.WaitGroup) {
Adam Langley95c29f32014-06-20 12:00:00 -07003080 defer wg.Done()
3081
3082 for test := range c {
Adam Langley69a01602014-11-17 17:26:55 -08003083 var err error
3084
3085 if *mallocTest < 0 {
3086 statusChan <- statusMsg{test: test, started: true}
3087 err = runTest(test, buildDir, -1)
3088 } else {
3089 for mallocNumToFail := int64(*mallocTest); ; mallocNumToFail++ {
3090 statusChan <- statusMsg{test: test, started: true}
3091 if err = runTest(test, buildDir, mallocNumToFail); err != errMoreMallocs {
3092 if err != nil {
3093 fmt.Printf("\n\nmalloc test failed at %d: %s\n", mallocNumToFail, err)
3094 }
3095 break
3096 }
3097 }
3098 }
Adam Langley95c29f32014-06-20 12:00:00 -07003099 statusChan <- statusMsg{test: test, err: err}
3100 }
3101}
3102
3103type statusMsg struct {
3104 test *testCase
3105 started bool
3106 err error
3107}
3108
David Benjamin5f237bc2015-02-11 17:14:15 -05003109func statusPrinter(doneChan chan *testOutput, statusChan chan statusMsg, total int) {
Adam Langley95c29f32014-06-20 12:00:00 -07003110 var started, done, failed, lineLen int
Adam Langley95c29f32014-06-20 12:00:00 -07003111
David Benjamin5f237bc2015-02-11 17:14:15 -05003112 testOutput := newTestOutput()
Adam Langley95c29f32014-06-20 12:00:00 -07003113 for msg := range statusChan {
David Benjamin5f237bc2015-02-11 17:14:15 -05003114 if !*pipe {
3115 // Erase the previous status line.
David Benjamin87c8a642015-02-21 01:54:29 -05003116 var erase string
3117 for i := 0; i < lineLen; i++ {
3118 erase += "\b \b"
3119 }
3120 fmt.Print(erase)
David Benjamin5f237bc2015-02-11 17:14:15 -05003121 }
3122
Adam Langley95c29f32014-06-20 12:00:00 -07003123 if msg.started {
3124 started++
3125 } else {
3126 done++
David Benjamin5f237bc2015-02-11 17:14:15 -05003127
3128 if msg.err != nil {
3129 fmt.Printf("FAILED (%s)\n%s\n", msg.test.name, msg.err)
3130 failed++
3131 testOutput.addResult(msg.test.name, "FAIL")
3132 } else {
3133 if *pipe {
3134 // Print each test instead of a status line.
3135 fmt.Printf("PASSED (%s)\n", msg.test.name)
3136 }
3137 testOutput.addResult(msg.test.name, "PASS")
3138 }
Adam Langley95c29f32014-06-20 12:00:00 -07003139 }
3140
David Benjamin5f237bc2015-02-11 17:14:15 -05003141 if !*pipe {
3142 // Print a new status line.
3143 line := fmt.Sprintf("%d/%d/%d/%d", failed, done, started, total)
3144 lineLen = len(line)
3145 os.Stdout.WriteString(line)
Adam Langley95c29f32014-06-20 12:00:00 -07003146 }
Adam Langley95c29f32014-06-20 12:00:00 -07003147 }
David Benjamin5f237bc2015-02-11 17:14:15 -05003148
3149 doneChan <- testOutput
Adam Langley95c29f32014-06-20 12:00:00 -07003150}
3151
3152func main() {
3153 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 -04003154 var flagNumWorkers *int = flag.Int("num-workers", runtime.NumCPU(), "The number of workers to run in parallel.")
David Benjamin884fdf12014-08-02 15:28:23 -04003155 var flagBuildDir *string = flag.String("build-dir", "../../../build", "The build directory to run the shim from.")
Adam Langley95c29f32014-06-20 12:00:00 -07003156
3157 flag.Parse()
3158
3159 addCipherSuiteTests()
3160 addBadECDSASignatureTests()
Adam Langley80842bd2014-06-20 12:00:00 -07003161 addCBCPaddingTests()
Kenny Root7fdeaf12014-08-05 15:23:37 -07003162 addCBCSplittingTests()
David Benjamin636293b2014-07-08 17:59:18 -04003163 addClientAuthTests()
Adam Langley524e7172015-02-20 16:04:00 -08003164 addDDoSCallbackTests()
David Benjamin7e2e6cf2014-08-07 17:44:24 -04003165 addVersionNegotiationTests()
David Benjaminaccb4542014-12-12 23:44:33 -05003166 addMinimumVersionTests()
David Benjamin5c24a1d2014-08-31 00:59:27 -04003167 addD5BugTests()
David Benjamine78bfde2014-09-06 12:45:15 -04003168 addExtensionTests()
David Benjamin01fe8202014-09-24 15:21:44 -04003169 addResumptionVersionTests()
Adam Langley75712922014-10-10 16:23:43 -07003170 addExtendedMasterSecretTests()
Adam Langley2ae77d22014-10-28 17:29:33 -07003171 addRenegotiationTests()
David Benjamin5e961c12014-11-07 01:48:35 -05003172 addDTLSReplayTests()
David Benjamin000800a2014-11-14 01:43:59 -05003173 addSigningHashTests()
Feng Lu41aa3252014-11-21 22:47:56 -08003174 addFastRadioPaddingTests()
David Benjamin83f90402015-01-27 01:09:43 -05003175 addDTLSRetransmitTests()
David Benjamin43ec06f2014-08-05 02:28:57 -04003176 for _, async := range []bool{false, true} {
3177 for _, splitHandshake := range []bool{false, true} {
David Benjamin6fd297b2014-08-11 18:43:38 -04003178 for _, protocol := range []protocol{tls, dtls} {
3179 addStateMachineCoverageTests(async, splitHandshake, protocol)
3180 }
David Benjamin43ec06f2014-08-05 02:28:57 -04003181 }
3182 }
Adam Langley95c29f32014-06-20 12:00:00 -07003183
3184 var wg sync.WaitGroup
3185
David Benjamin2bc8e6f2014-08-02 15:22:37 -04003186 numWorkers := *flagNumWorkers
Adam Langley95c29f32014-06-20 12:00:00 -07003187
3188 statusChan := make(chan statusMsg, numWorkers)
3189 testChan := make(chan *testCase, numWorkers)
David Benjamin5f237bc2015-02-11 17:14:15 -05003190 doneChan := make(chan *testOutput)
Adam Langley95c29f32014-06-20 12:00:00 -07003191
David Benjamin025b3d32014-07-01 19:53:04 -04003192 go statusPrinter(doneChan, statusChan, len(testCases))
Adam Langley95c29f32014-06-20 12:00:00 -07003193
3194 for i := 0; i < numWorkers; i++ {
3195 wg.Add(1)
David Benjamin884fdf12014-08-02 15:28:23 -04003196 go worker(statusChan, testChan, *flagBuildDir, &wg)
Adam Langley95c29f32014-06-20 12:00:00 -07003197 }
3198
David Benjamin025b3d32014-07-01 19:53:04 -04003199 for i := range testCases {
3200 if len(*flagTest) == 0 || *flagTest == testCases[i].name {
3201 testChan <- &testCases[i]
Adam Langley95c29f32014-06-20 12:00:00 -07003202 }
3203 }
3204
3205 close(testChan)
3206 wg.Wait()
3207 close(statusChan)
David Benjamin5f237bc2015-02-11 17:14:15 -05003208 testOutput := <-doneChan
Adam Langley95c29f32014-06-20 12:00:00 -07003209
3210 fmt.Printf("\n")
David Benjamin5f237bc2015-02-11 17:14:15 -05003211
3212 if *jsonOutput != "" {
3213 if err := testOutput.writeTo(*jsonOutput); err != nil {
3214 fmt.Fprintf(os.Stderr, "Error: %s\n", err)
3215 }
3216 }
Adam Langley95c29f32014-06-20 12:00:00 -07003217}