blob: c9362148daa0be9a00975871759e6f57afb94cda [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 },
Adam Langley95c29f32014-06-20 12:00:00 -0700895}
896
David Benjamin01fe8202014-09-24 15:21:44 -0400897func doExchange(test *testCase, config *Config, conn net.Conn, messageLen int, isResume bool) error {
David Benjamin65ea8ff2014-11-23 03:01:00 -0500898 var connDebug *recordingConn
David Benjamin5fa3eba2015-01-22 16:35:40 -0500899 var connDamage *damageAdaptor
David Benjamin65ea8ff2014-11-23 03:01:00 -0500900 if *flagDebug {
901 connDebug = &recordingConn{Conn: conn}
902 conn = connDebug
903 defer func() {
904 connDebug.WriteTo(os.Stdout)
905 }()
906 }
907
David Benjamin6fd297b2014-08-11 18:43:38 -0400908 if test.protocol == dtls {
David Benjamin83f90402015-01-27 01:09:43 -0500909 config.Bugs.PacketAdaptor = newPacketAdaptor(conn)
910 conn = config.Bugs.PacketAdaptor
David Benjamin5e961c12014-11-07 01:48:35 -0500911 if test.replayWrites {
912 conn = newReplayAdaptor(conn)
913 }
David Benjamin6fd297b2014-08-11 18:43:38 -0400914 }
915
David Benjamin5fa3eba2015-01-22 16:35:40 -0500916 if test.damageFirstWrite {
917 connDamage = newDamageAdaptor(conn)
918 conn = connDamage
919 }
920
David Benjamin6fd297b2014-08-11 18:43:38 -0400921 if test.sendPrefix != "" {
922 if _, err := conn.Write([]byte(test.sendPrefix)); err != nil {
923 return err
924 }
David Benjamin98e882e2014-08-08 13:24:34 -0400925 }
926
David Benjamin1d5c83e2014-07-22 19:20:02 -0400927 var tlsConn *Conn
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400928 if test.testType == clientTest {
David Benjamin6fd297b2014-08-11 18:43:38 -0400929 if test.protocol == dtls {
930 tlsConn = DTLSServer(conn, config)
931 } else {
932 tlsConn = Server(conn, config)
933 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400934 } else {
935 config.InsecureSkipVerify = true
David Benjamin6fd297b2014-08-11 18:43:38 -0400936 if test.protocol == dtls {
937 tlsConn = DTLSClient(conn, config)
938 } else {
939 tlsConn = Client(conn, config)
940 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400941 }
942
Adam Langley95c29f32014-06-20 12:00:00 -0700943 if err := tlsConn.Handshake(); err != nil {
944 return err
945 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700946
David Benjamin01fe8202014-09-24 15:21:44 -0400947 // TODO(davidben): move all per-connection expectations into a dedicated
948 // expectations struct that can be specified separately for the two
949 // legs.
950 expectedVersion := test.expectedVersion
951 if isResume && test.expectedResumeVersion != 0 {
952 expectedVersion = test.expectedResumeVersion
953 }
954 if vers := tlsConn.ConnectionState().Version; expectedVersion != 0 && vers != expectedVersion {
955 return fmt.Errorf("got version %x, expected %x", vers, expectedVersion)
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400956 }
957
David Benjamina08e49d2014-08-24 01:46:07 -0400958 if test.expectChannelID {
959 channelID := tlsConn.ConnectionState().ChannelID
960 if channelID == nil {
961 return fmt.Errorf("no channel ID negotiated")
962 }
963 if channelID.Curve != channelIDKey.Curve ||
964 channelIDKey.X.Cmp(channelIDKey.X) != 0 ||
965 channelIDKey.Y.Cmp(channelIDKey.Y) != 0 {
966 return fmt.Errorf("incorrect channel ID")
967 }
968 }
969
David Benjaminae2888f2014-09-06 12:58:58 -0400970 if expected := test.expectedNextProto; expected != "" {
971 if actual := tlsConn.ConnectionState().NegotiatedProtocol; actual != expected {
972 return fmt.Errorf("next proto mismatch: got %s, wanted %s", actual, expected)
973 }
974 }
975
David Benjaminfc7b0862014-09-06 13:21:53 -0400976 if test.expectedNextProtoType != 0 {
977 if (test.expectedNextProtoType == alpn) != tlsConn.ConnectionState().NegotiatedProtocolFromALPN {
978 return fmt.Errorf("next proto type mismatch")
979 }
980 }
981
David Benjaminca6c8262014-11-15 19:06:08 -0500982 if p := tlsConn.ConnectionState().SRTPProtectionProfile; p != test.expectedSRTPProtectionProfile {
983 return fmt.Errorf("SRTP profile mismatch: got %d, wanted %d", p, test.expectedSRTPProtectionProfile)
984 }
985
David Benjamine58c4f52014-08-24 03:47:07 -0400986 if test.shimWritesFirst {
987 var buf [5]byte
988 _, err := io.ReadFull(tlsConn, buf[:])
989 if err != nil {
990 return err
991 }
992 if string(buf[:]) != "hello" {
993 return fmt.Errorf("bad initial message")
994 }
995 }
996
Adam Langleycf2d4f42014-10-28 19:06:14 -0700997 if test.renegotiate {
998 if test.renegotiateCiphers != nil {
999 config.CipherSuites = test.renegotiateCiphers
1000 }
1001 if err := tlsConn.Renegotiate(); err != nil {
1002 return err
1003 }
1004 } else if test.renegotiateCiphers != nil {
1005 panic("renegotiateCiphers without renegotiate")
1006 }
1007
David Benjamin5fa3eba2015-01-22 16:35:40 -05001008 if test.damageFirstWrite {
1009 connDamage.setDamage(true)
1010 tlsConn.Write([]byte("DAMAGED WRITE"))
1011 connDamage.setDamage(false)
1012 }
1013
Kenny Root7fdeaf12014-08-05 15:23:37 -07001014 if messageLen < 0 {
David Benjamin6fd297b2014-08-11 18:43:38 -04001015 if test.protocol == dtls {
1016 return fmt.Errorf("messageLen < 0 not supported for DTLS tests")
1017 }
Kenny Root7fdeaf12014-08-05 15:23:37 -07001018 // Read until EOF.
1019 _, err := io.Copy(ioutil.Discard, tlsConn)
1020 return err
1021 }
1022
David Benjamin4189bd92015-01-25 23:52:39 -05001023 var testMessage []byte
1024 if config.Bugs.AppDataAfterChangeCipherSpec != nil {
1025 // We've already sent a message. Expect the shim to echo it
1026 // back.
1027 testMessage = config.Bugs.AppDataAfterChangeCipherSpec
1028 } else {
1029 if messageLen == 0 {
1030 messageLen = 32
1031 }
1032 testMessage = make([]byte, messageLen)
1033 for i := range testMessage {
1034 testMessage[i] = 0x42
1035 }
1036 tlsConn.Write(testMessage)
Adam Langley80842bd2014-06-20 12:00:00 -07001037 }
Adam Langley95c29f32014-06-20 12:00:00 -07001038
1039 buf := make([]byte, len(testMessage))
David Benjamin6fd297b2014-08-11 18:43:38 -04001040 if test.protocol == dtls {
1041 bufTmp := make([]byte, len(buf)+1)
1042 n, err := tlsConn.Read(bufTmp)
1043 if err != nil {
1044 return err
1045 }
1046 if n != len(buf) {
1047 return fmt.Errorf("bad reply; length mismatch (%d vs %d)", n, len(buf))
1048 }
1049 copy(buf, bufTmp)
1050 } else {
1051 _, err := io.ReadFull(tlsConn, buf)
1052 if err != nil {
1053 return err
1054 }
Adam Langley95c29f32014-06-20 12:00:00 -07001055 }
1056
1057 for i, v := range buf {
1058 if v != testMessage[i]^0xff {
1059 return fmt.Errorf("bad reply contents at byte %d", i)
1060 }
1061 }
1062
1063 return nil
1064}
1065
David Benjamin325b5c32014-07-01 19:40:31 -04001066func valgrindOf(dbAttach bool, path string, args ...string) *exec.Cmd {
1067 valgrindArgs := []string{"--error-exitcode=99", "--track-origins=yes", "--leak-check=full"}
Adam Langley95c29f32014-06-20 12:00:00 -07001068 if dbAttach {
David Benjamin325b5c32014-07-01 19:40:31 -04001069 valgrindArgs = append(valgrindArgs, "--db-attach=yes", "--db-command=xterm -e gdb -nw %f %p")
Adam Langley95c29f32014-06-20 12:00:00 -07001070 }
David Benjamin325b5c32014-07-01 19:40:31 -04001071 valgrindArgs = append(valgrindArgs, path)
1072 valgrindArgs = append(valgrindArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -07001073
David Benjamin325b5c32014-07-01 19:40:31 -04001074 return exec.Command("valgrind", valgrindArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -07001075}
1076
David Benjamin325b5c32014-07-01 19:40:31 -04001077func gdbOf(path string, args ...string) *exec.Cmd {
1078 xtermArgs := []string{"-e", "gdb", "--args"}
1079 xtermArgs = append(xtermArgs, path)
1080 xtermArgs = append(xtermArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -07001081
David Benjamin325b5c32014-07-01 19:40:31 -04001082 return exec.Command("xterm", xtermArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -07001083}
1084
Adam Langley69a01602014-11-17 17:26:55 -08001085type moreMallocsError struct{}
1086
1087func (moreMallocsError) Error() string {
1088 return "child process did not exhaust all allocation calls"
1089}
1090
1091var errMoreMallocs = moreMallocsError{}
1092
David Benjamin87c8a642015-02-21 01:54:29 -05001093// accept accepts a connection from listener, unless waitChan signals a process
1094// exit first.
1095func acceptOrWait(listener net.Listener, waitChan chan error) (net.Conn, error) {
1096 type connOrError struct {
1097 conn net.Conn
1098 err error
1099 }
1100 connChan := make(chan connOrError, 1)
1101 go func() {
1102 conn, err := listener.Accept()
1103 connChan <- connOrError{conn, err}
1104 close(connChan)
1105 }()
1106 select {
1107 case result := <-connChan:
1108 return result.conn, result.err
1109 case childErr := <-waitChan:
1110 waitChan <- childErr
1111 return nil, fmt.Errorf("child exited early: %s", childErr)
1112 }
1113}
1114
Adam Langley69a01602014-11-17 17:26:55 -08001115func runTest(test *testCase, buildDir string, mallocNumToFail int64) error {
Adam Langley38311732014-10-16 19:04:35 -07001116 if !test.shouldFail && (len(test.expectedError) > 0 || len(test.expectedLocalError) > 0) {
1117 panic("Error expected without shouldFail in " + test.name)
1118 }
1119
David Benjamin87c8a642015-02-21 01:54:29 -05001120 listener, err := net.ListenTCP("tcp4", &net.TCPAddr{IP: net.IP{127, 0, 0, 1}})
1121 if err != nil {
1122 panic(err)
1123 }
1124 defer func() {
1125 if listener != nil {
1126 listener.Close()
1127 }
1128 }()
Adam Langley95c29f32014-06-20 12:00:00 -07001129
David Benjamin884fdf12014-08-02 15:28:23 -04001130 shim_path := path.Join(buildDir, "ssl/test/bssl_shim")
David Benjamin87c8a642015-02-21 01:54:29 -05001131 flags := []string{"-port", strconv.Itoa(listener.Addr().(*net.TCPAddr).Port)}
David Benjamin1d5c83e2014-07-22 19:20:02 -04001132 if test.testType == serverTest {
David Benjamin5a593af2014-08-11 19:51:50 -04001133 flags = append(flags, "-server")
1134
David Benjamin025b3d32014-07-01 19:53:04 -04001135 flags = append(flags, "-key-file")
1136 if test.keyFile == "" {
1137 flags = append(flags, rsaKeyFile)
1138 } else {
1139 flags = append(flags, test.keyFile)
1140 }
1141
1142 flags = append(flags, "-cert-file")
1143 if test.certFile == "" {
1144 flags = append(flags, rsaCertificateFile)
1145 } else {
1146 flags = append(flags, test.certFile)
1147 }
1148 }
David Benjamin5a593af2014-08-11 19:51:50 -04001149
David Benjamin6fd297b2014-08-11 18:43:38 -04001150 if test.protocol == dtls {
1151 flags = append(flags, "-dtls")
1152 }
1153
David Benjamin5a593af2014-08-11 19:51:50 -04001154 if test.resumeSession {
1155 flags = append(flags, "-resume")
1156 }
1157
David Benjamine58c4f52014-08-24 03:47:07 -04001158 if test.shimWritesFirst {
1159 flags = append(flags, "-shim-writes-first")
1160 }
1161
David Benjamin025b3d32014-07-01 19:53:04 -04001162 flags = append(flags, test.flags...)
1163
1164 var shim *exec.Cmd
1165 if *useValgrind {
1166 shim = valgrindOf(false, shim_path, flags...)
Adam Langley75712922014-10-10 16:23:43 -07001167 } else if *useGDB {
1168 shim = gdbOf(shim_path, flags...)
David Benjamin025b3d32014-07-01 19:53:04 -04001169 } else {
1170 shim = exec.Command(shim_path, flags...)
1171 }
David Benjamin025b3d32014-07-01 19:53:04 -04001172 shim.Stdin = os.Stdin
1173 var stdoutBuf, stderrBuf bytes.Buffer
1174 shim.Stdout = &stdoutBuf
1175 shim.Stderr = &stderrBuf
Adam Langley69a01602014-11-17 17:26:55 -08001176 if mallocNumToFail >= 0 {
David Benjamin9e128b02015-02-09 13:13:09 -05001177 shim.Env = os.Environ()
1178 shim.Env = append(shim.Env, "MALLOC_NUMBER_TO_FAIL="+strconv.FormatInt(mallocNumToFail, 10))
Adam Langley69a01602014-11-17 17:26:55 -08001179 if *mallocTestDebug {
1180 shim.Env = append(shim.Env, "MALLOC_ABORT_ON_FAIL=1")
1181 }
1182 shim.Env = append(shim.Env, "_MALLOC_CHECK=1")
1183 }
David Benjamin025b3d32014-07-01 19:53:04 -04001184
1185 if err := shim.Start(); err != nil {
Adam Langley95c29f32014-06-20 12:00:00 -07001186 panic(err)
1187 }
David Benjamin87c8a642015-02-21 01:54:29 -05001188 waitChan := make(chan error, 1)
1189 go func() { waitChan <- shim.Wait() }()
Adam Langley95c29f32014-06-20 12:00:00 -07001190
1191 config := test.config
David Benjamin1d5c83e2014-07-22 19:20:02 -04001192 config.ClientSessionCache = NewLRUClientSessionCache(1)
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001193 config.ServerSessionCache = NewLRUServerSessionCache(1)
David Benjamin025b3d32014-07-01 19:53:04 -04001194 if test.testType == clientTest {
1195 if len(config.Certificates) == 0 {
1196 config.Certificates = []Certificate{getRSACertificate()}
1197 }
David Benjamin87c8a642015-02-21 01:54:29 -05001198 } else {
1199 // Supply a ServerName to ensure a constant session cache key,
1200 // rather than falling back to net.Conn.RemoteAddr.
1201 if len(config.ServerName) == 0 {
1202 config.ServerName = "test"
1203 }
David Benjamin025b3d32014-07-01 19:53:04 -04001204 }
Adam Langley95c29f32014-06-20 12:00:00 -07001205
David Benjamin87c8a642015-02-21 01:54:29 -05001206 conn, err := acceptOrWait(listener, waitChan)
1207 if err == nil {
1208 err = doExchange(test, &config, conn, test.messageLen, false /* not a resumption */)
1209 conn.Close()
1210 }
David Benjamin65ea8ff2014-11-23 03:01:00 -05001211
David Benjamin1d5c83e2014-07-22 19:20:02 -04001212 if err == nil && test.resumeSession {
David Benjamin01fe8202014-09-24 15:21:44 -04001213 var resumeConfig Config
1214 if test.resumeConfig != nil {
1215 resumeConfig = *test.resumeConfig
David Benjamin87c8a642015-02-21 01:54:29 -05001216 if len(resumeConfig.ServerName) == 0 {
1217 resumeConfig.ServerName = config.ServerName
1218 }
David Benjamin01fe8202014-09-24 15:21:44 -04001219 if len(resumeConfig.Certificates) == 0 {
1220 resumeConfig.Certificates = []Certificate{getRSACertificate()}
1221 }
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001222 if !test.newSessionsOnResume {
1223 resumeConfig.SessionTicketKey = config.SessionTicketKey
1224 resumeConfig.ClientSessionCache = config.ClientSessionCache
1225 resumeConfig.ServerSessionCache = config.ServerSessionCache
1226 }
David Benjamin01fe8202014-09-24 15:21:44 -04001227 } else {
1228 resumeConfig = config
1229 }
David Benjamin87c8a642015-02-21 01:54:29 -05001230 var connResume net.Conn
1231 connResume, err = acceptOrWait(listener, waitChan)
1232 if err == nil {
1233 err = doExchange(test, &resumeConfig, connResume, test.messageLen, true /* resumption */)
1234 connResume.Close()
1235 }
David Benjamin1d5c83e2014-07-22 19:20:02 -04001236 }
1237
David Benjamin87c8a642015-02-21 01:54:29 -05001238 // Close the listener now. This is to avoid hangs should the shim try to
1239 // open more connections than expected.
1240 listener.Close()
1241 listener = nil
1242
1243 childErr := <-waitChan
Adam Langley69a01602014-11-17 17:26:55 -08001244 if exitError, ok := childErr.(*exec.ExitError); ok {
1245 if exitError.Sys().(syscall.WaitStatus).ExitStatus() == 88 {
1246 return errMoreMallocs
1247 }
1248 }
Adam Langley95c29f32014-06-20 12:00:00 -07001249
1250 stdout := string(stdoutBuf.Bytes())
1251 stderr := string(stderrBuf.Bytes())
1252 failed := err != nil || childErr != nil
1253 correctFailure := len(test.expectedError) == 0 || strings.Contains(stdout, test.expectedError)
Adam Langleyac61fa32014-06-23 12:03:11 -07001254 localError := "none"
1255 if err != nil {
1256 localError = err.Error()
1257 }
1258 if len(test.expectedLocalError) != 0 {
1259 correctFailure = correctFailure && strings.Contains(localError, test.expectedLocalError)
1260 }
Adam Langley95c29f32014-06-20 12:00:00 -07001261
1262 if failed != test.shouldFail || failed && !correctFailure {
Adam Langley95c29f32014-06-20 12:00:00 -07001263 childError := "none"
Adam Langley95c29f32014-06-20 12:00:00 -07001264 if childErr != nil {
1265 childError = childErr.Error()
1266 }
1267
1268 var msg string
1269 switch {
1270 case failed && !test.shouldFail:
1271 msg = "unexpected failure"
1272 case !failed && test.shouldFail:
1273 msg = "unexpected success"
1274 case failed && !correctFailure:
Adam Langleyac61fa32014-06-23 12:03:11 -07001275 msg = "bad error (wanted '" + test.expectedError + "' / '" + test.expectedLocalError + "')"
Adam Langley95c29f32014-06-20 12:00:00 -07001276 default:
1277 panic("internal error")
1278 }
1279
1280 return fmt.Errorf("%s: local error '%s', child error '%s', stdout:\n%s\nstderr:\n%s", msg, localError, childError, string(stdoutBuf.Bytes()), stderr)
1281 }
1282
1283 if !*useValgrind && len(stderr) > 0 {
1284 println(stderr)
1285 }
1286
1287 return nil
1288}
1289
1290var tlsVersions = []struct {
1291 name string
1292 version uint16
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001293 flag string
David Benjamin8b8c0062014-11-23 02:47:52 -05001294 hasDTLS bool
Adam Langley95c29f32014-06-20 12:00:00 -07001295}{
David Benjamin8b8c0062014-11-23 02:47:52 -05001296 {"SSL3", VersionSSL30, "-no-ssl3", false},
1297 {"TLS1", VersionTLS10, "-no-tls1", true},
1298 {"TLS11", VersionTLS11, "-no-tls11", false},
1299 {"TLS12", VersionTLS12, "-no-tls12", true},
Adam Langley95c29f32014-06-20 12:00:00 -07001300}
1301
1302var testCipherSuites = []struct {
1303 name string
1304 id uint16
1305}{
1306 {"3DES-SHA", TLS_RSA_WITH_3DES_EDE_CBC_SHA},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001307 {"AES128-GCM", TLS_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001308 {"AES128-SHA", TLS_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001309 {"AES128-SHA256", TLS_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001310 {"AES256-GCM", TLS_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001311 {"AES256-SHA", TLS_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001312 {"AES256-SHA256", TLS_RSA_WITH_AES_256_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001313 {"DHE-RSA-AES128-GCM", TLS_DHE_RSA_WITH_AES_128_GCM_SHA256},
1314 {"DHE-RSA-AES128-SHA", TLS_DHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001315 {"DHE-RSA-AES128-SHA256", TLS_DHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001316 {"DHE-RSA-AES256-GCM", TLS_DHE_RSA_WITH_AES_256_GCM_SHA384},
1317 {"DHE-RSA-AES256-SHA", TLS_DHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001318 {"DHE-RSA-AES256-SHA256", TLS_DHE_RSA_WITH_AES_256_CBC_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001319 {"ECDHE-ECDSA-AES128-GCM", TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
1320 {"ECDHE-ECDSA-AES128-SHA", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001321 {"ECDHE-ECDSA-AES128-SHA256", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256},
1322 {"ECDHE-ECDSA-AES256-GCM", TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001323 {"ECDHE-ECDSA-AES256-SHA", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001324 {"ECDHE-ECDSA-AES256-SHA384", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001325 {"ECDHE-ECDSA-RC4-SHA", TLS_ECDHE_ECDSA_WITH_RC4_128_SHA},
David Benjamin2af684f2014-10-27 02:23:15 -04001326 {"ECDHE-PSK-WITH-AES-128-GCM-SHA256", TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001327 {"ECDHE-RSA-AES128-GCM", TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001328 {"ECDHE-RSA-AES128-SHA", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001329 {"ECDHE-RSA-AES128-SHA256", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001330 {"ECDHE-RSA-AES256-GCM", TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001331 {"ECDHE-RSA-AES256-SHA", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001332 {"ECDHE-RSA-AES256-SHA384", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001333 {"ECDHE-RSA-RC4-SHA", TLS_ECDHE_RSA_WITH_RC4_128_SHA},
David Benjamin48cae082014-10-27 01:06:24 -04001334 {"PSK-AES128-CBC-SHA", TLS_PSK_WITH_AES_128_CBC_SHA},
1335 {"PSK-AES256-CBC-SHA", TLS_PSK_WITH_AES_256_CBC_SHA},
1336 {"PSK-RC4-SHA", TLS_PSK_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -07001337 {"RC4-MD5", TLS_RSA_WITH_RC4_128_MD5},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001338 {"RC4-SHA", TLS_RSA_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -07001339}
1340
David Benjamin8b8c0062014-11-23 02:47:52 -05001341func hasComponent(suiteName, component string) bool {
1342 return strings.Contains("-"+suiteName+"-", "-"+component+"-")
1343}
1344
David Benjaminf7768e42014-08-31 02:06:47 -04001345func isTLS12Only(suiteName string) bool {
David Benjamin8b8c0062014-11-23 02:47:52 -05001346 return hasComponent(suiteName, "GCM") ||
1347 hasComponent(suiteName, "SHA256") ||
1348 hasComponent(suiteName, "SHA384")
1349}
1350
1351func isDTLSCipher(suiteName string) bool {
David Benjamine95d20d2014-12-23 11:16:01 -05001352 return !hasComponent(suiteName, "RC4")
David Benjaminf7768e42014-08-31 02:06:47 -04001353}
1354
Adam Langley95c29f32014-06-20 12:00:00 -07001355func addCipherSuiteTests() {
1356 for _, suite := range testCipherSuites {
David Benjamin48cae082014-10-27 01:06:24 -04001357 const psk = "12345"
1358 const pskIdentity = "luggage combo"
1359
Adam Langley95c29f32014-06-20 12:00:00 -07001360 var cert Certificate
David Benjamin025b3d32014-07-01 19:53:04 -04001361 var certFile string
1362 var keyFile string
David Benjamin8b8c0062014-11-23 02:47:52 -05001363 if hasComponent(suite.name, "ECDSA") {
Adam Langley95c29f32014-06-20 12:00:00 -07001364 cert = getECDSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -04001365 certFile = ecdsaCertificateFile
1366 keyFile = ecdsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -07001367 } else {
1368 cert = getRSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -04001369 certFile = rsaCertificateFile
1370 keyFile = rsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -07001371 }
1372
David Benjamin48cae082014-10-27 01:06:24 -04001373 var flags []string
David Benjamin8b8c0062014-11-23 02:47:52 -05001374 if hasComponent(suite.name, "PSK") {
David Benjamin48cae082014-10-27 01:06:24 -04001375 flags = append(flags,
1376 "-psk", psk,
1377 "-psk-identity", pskIdentity)
1378 }
1379
Adam Langley95c29f32014-06-20 12:00:00 -07001380 for _, ver := range tlsVersions {
David Benjaminf7768e42014-08-31 02:06:47 -04001381 if ver.version < VersionTLS12 && isTLS12Only(suite.name) {
Adam Langley95c29f32014-06-20 12:00:00 -07001382 continue
1383 }
1384
David Benjamin025b3d32014-07-01 19:53:04 -04001385 testCases = append(testCases, testCase{
1386 testType: clientTest,
1387 name: ver.name + "-" + suite.name + "-client",
Adam Langley95c29f32014-06-20 12:00:00 -07001388 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001389 MinVersion: ver.version,
1390 MaxVersion: ver.version,
1391 CipherSuites: []uint16{suite.id},
1392 Certificates: []Certificate{cert},
1393 PreSharedKey: []byte(psk),
1394 PreSharedKeyIdentity: pskIdentity,
Adam Langley95c29f32014-06-20 12:00:00 -07001395 },
David Benjamin48cae082014-10-27 01:06:24 -04001396 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001397 resumeSession: true,
Adam Langley95c29f32014-06-20 12:00:00 -07001398 })
David Benjamin025b3d32014-07-01 19:53:04 -04001399
David Benjamin76d8abe2014-08-14 16:25:34 -04001400 testCases = append(testCases, testCase{
1401 testType: serverTest,
1402 name: ver.name + "-" + suite.name + "-server",
1403 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001404 MinVersion: ver.version,
1405 MaxVersion: ver.version,
1406 CipherSuites: []uint16{suite.id},
1407 Certificates: []Certificate{cert},
1408 PreSharedKey: []byte(psk),
1409 PreSharedKeyIdentity: pskIdentity,
David Benjamin76d8abe2014-08-14 16:25:34 -04001410 },
1411 certFile: certFile,
1412 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001413 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001414 resumeSession: true,
David Benjamin76d8abe2014-08-14 16:25:34 -04001415 })
David Benjamin6fd297b2014-08-11 18:43:38 -04001416
David Benjamin8b8c0062014-11-23 02:47:52 -05001417 if ver.hasDTLS && isDTLSCipher(suite.name) {
David Benjamin6fd297b2014-08-11 18:43:38 -04001418 testCases = append(testCases, testCase{
1419 testType: clientTest,
1420 protocol: dtls,
1421 name: "D" + ver.name + "-" + suite.name + "-client",
1422 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001423 MinVersion: ver.version,
1424 MaxVersion: ver.version,
1425 CipherSuites: []uint16{suite.id},
1426 Certificates: []Certificate{cert},
1427 PreSharedKey: []byte(psk),
1428 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001429 },
David Benjamin48cae082014-10-27 01:06:24 -04001430 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001431 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001432 })
1433 testCases = append(testCases, testCase{
1434 testType: serverTest,
1435 protocol: dtls,
1436 name: "D" + ver.name + "-" + suite.name + "-server",
1437 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001438 MinVersion: ver.version,
1439 MaxVersion: ver.version,
1440 CipherSuites: []uint16{suite.id},
1441 Certificates: []Certificate{cert},
1442 PreSharedKey: []byte(psk),
1443 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001444 },
1445 certFile: certFile,
1446 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001447 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001448 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001449 })
1450 }
Adam Langley95c29f32014-06-20 12:00:00 -07001451 }
1452 }
1453}
1454
1455func addBadECDSASignatureTests() {
1456 for badR := BadValue(1); badR < NumBadValues; badR++ {
1457 for badS := BadValue(1); badS < NumBadValues; badS++ {
David Benjamin025b3d32014-07-01 19:53:04 -04001458 testCases = append(testCases, testCase{
Adam Langley95c29f32014-06-20 12:00:00 -07001459 name: fmt.Sprintf("BadECDSA-%d-%d", badR, badS),
1460 config: Config{
1461 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
1462 Certificates: []Certificate{getECDSACertificate()},
1463 Bugs: ProtocolBugs{
1464 BadECDSAR: badR,
1465 BadECDSAS: badS,
1466 },
1467 },
1468 shouldFail: true,
1469 expectedError: "SIGNATURE",
1470 })
1471 }
1472 }
1473}
1474
Adam Langley80842bd2014-06-20 12:00:00 -07001475func addCBCPaddingTests() {
David Benjamin025b3d32014-07-01 19:53:04 -04001476 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001477 name: "MaxCBCPadding",
1478 config: Config{
1479 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1480 Bugs: ProtocolBugs{
1481 MaxPadding: true,
1482 },
1483 },
1484 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1485 })
David Benjamin025b3d32014-07-01 19:53:04 -04001486 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001487 name: "BadCBCPadding",
1488 config: Config{
1489 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1490 Bugs: ProtocolBugs{
1491 PaddingFirstByteBad: true,
1492 },
1493 },
1494 shouldFail: true,
1495 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1496 })
1497 // OpenSSL previously had an issue where the first byte of padding in
1498 // 255 bytes of padding wasn't checked.
David Benjamin025b3d32014-07-01 19:53:04 -04001499 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001500 name: "BadCBCPadding255",
1501 config: Config{
1502 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1503 Bugs: ProtocolBugs{
1504 MaxPadding: true,
1505 PaddingFirstByteBadIf255: true,
1506 },
1507 },
1508 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1509 shouldFail: true,
1510 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1511 })
1512}
1513
Kenny Root7fdeaf12014-08-05 15:23:37 -07001514func addCBCSplittingTests() {
1515 testCases = append(testCases, testCase{
1516 name: "CBCRecordSplitting",
1517 config: Config{
1518 MaxVersion: VersionTLS10,
1519 MinVersion: VersionTLS10,
1520 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1521 },
1522 messageLen: -1, // read until EOF
1523 flags: []string{
1524 "-async",
1525 "-write-different-record-sizes",
1526 "-cbc-record-splitting",
1527 },
David Benjamina8e3e0e2014-08-06 22:11:10 -04001528 })
1529 testCases = append(testCases, testCase{
Kenny Root7fdeaf12014-08-05 15:23:37 -07001530 name: "CBCRecordSplittingPartialWrite",
1531 config: Config{
1532 MaxVersion: VersionTLS10,
1533 MinVersion: VersionTLS10,
1534 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1535 },
1536 messageLen: -1, // read until EOF
1537 flags: []string{
1538 "-async",
1539 "-write-different-record-sizes",
1540 "-cbc-record-splitting",
1541 "-partial-write",
1542 },
1543 })
1544}
1545
David Benjamin636293b2014-07-08 17:59:18 -04001546func addClientAuthTests() {
David Benjamin407a10c2014-07-16 12:58:59 -04001547 // Add a dummy cert pool to stress certificate authority parsing.
1548 // TODO(davidben): Add tests that those values parse out correctly.
1549 certPool := x509.NewCertPool()
1550 cert, err := x509.ParseCertificate(rsaCertificate.Certificate[0])
1551 if err != nil {
1552 panic(err)
1553 }
1554 certPool.AddCert(cert)
1555
David Benjamin636293b2014-07-08 17:59:18 -04001556 for _, ver := range tlsVersions {
David Benjamin636293b2014-07-08 17:59:18 -04001557 testCases = append(testCases, testCase{
1558 testType: clientTest,
David Benjamin67666e72014-07-12 15:47:52 -04001559 name: ver.name + "-Client-ClientAuth-RSA",
David Benjamin636293b2014-07-08 17:59:18 -04001560 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001561 MinVersion: ver.version,
1562 MaxVersion: ver.version,
1563 ClientAuth: RequireAnyClientCert,
1564 ClientCAs: certPool,
David Benjamin636293b2014-07-08 17:59:18 -04001565 },
1566 flags: []string{
1567 "-cert-file", rsaCertificateFile,
1568 "-key-file", rsaKeyFile,
1569 },
1570 })
1571 testCases = append(testCases, testCase{
David Benjamin67666e72014-07-12 15:47:52 -04001572 testType: serverTest,
1573 name: ver.name + "-Server-ClientAuth-RSA",
1574 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001575 MinVersion: ver.version,
1576 MaxVersion: ver.version,
David Benjamin67666e72014-07-12 15:47:52 -04001577 Certificates: []Certificate{rsaCertificate},
1578 },
1579 flags: []string{"-require-any-client-certificate"},
1580 })
David Benjamine098ec22014-08-27 23:13:20 -04001581 if ver.version != VersionSSL30 {
1582 testCases = append(testCases, testCase{
1583 testType: serverTest,
1584 name: ver.name + "-Server-ClientAuth-ECDSA",
1585 config: Config{
1586 MinVersion: ver.version,
1587 MaxVersion: ver.version,
1588 Certificates: []Certificate{ecdsaCertificate},
1589 },
1590 flags: []string{"-require-any-client-certificate"},
1591 })
1592 testCases = append(testCases, testCase{
1593 testType: clientTest,
1594 name: ver.name + "-Client-ClientAuth-ECDSA",
1595 config: Config{
1596 MinVersion: ver.version,
1597 MaxVersion: ver.version,
1598 ClientAuth: RequireAnyClientCert,
1599 ClientCAs: certPool,
1600 },
1601 flags: []string{
1602 "-cert-file", ecdsaCertificateFile,
1603 "-key-file", ecdsaKeyFile,
1604 },
1605 })
1606 }
David Benjamin636293b2014-07-08 17:59:18 -04001607 }
1608}
1609
Adam Langley75712922014-10-10 16:23:43 -07001610func addExtendedMasterSecretTests() {
1611 const expectEMSFlag = "-expect-extended-master-secret"
1612
1613 for _, with := range []bool{false, true} {
1614 prefix := "No"
1615 var flags []string
1616 if with {
1617 prefix = ""
1618 flags = []string{expectEMSFlag}
1619 }
1620
1621 for _, isClient := range []bool{false, true} {
1622 suffix := "-Server"
1623 testType := serverTest
1624 if isClient {
1625 suffix = "-Client"
1626 testType = clientTest
1627 }
1628
1629 for _, ver := range tlsVersions {
1630 test := testCase{
1631 testType: testType,
1632 name: prefix + "ExtendedMasterSecret-" + ver.name + suffix,
1633 config: Config{
1634 MinVersion: ver.version,
1635 MaxVersion: ver.version,
1636 Bugs: ProtocolBugs{
1637 NoExtendedMasterSecret: !with,
1638 RequireExtendedMasterSecret: with,
1639 },
1640 },
David Benjamin48cae082014-10-27 01:06:24 -04001641 flags: flags,
1642 shouldFail: ver.version == VersionSSL30 && with,
Adam Langley75712922014-10-10 16:23:43 -07001643 }
1644 if test.shouldFail {
1645 test.expectedLocalError = "extended master secret required but not supported by peer"
1646 }
1647 testCases = append(testCases, test)
1648 }
1649 }
1650 }
1651
1652 // When a session is resumed, it should still be aware that its master
1653 // secret was generated via EMS and thus it's safe to use tls-unique.
1654 testCases = append(testCases, testCase{
1655 name: "ExtendedMasterSecret-Resume",
1656 config: Config{
1657 Bugs: ProtocolBugs{
1658 RequireExtendedMasterSecret: true,
1659 },
1660 },
1661 flags: []string{expectEMSFlag},
1662 resumeSession: true,
1663 })
1664}
1665
David Benjamin43ec06f2014-08-05 02:28:57 -04001666// Adds tests that try to cover the range of the handshake state machine, under
1667// various conditions. Some of these are redundant with other tests, but they
1668// only cover the synchronous case.
David Benjamin6fd297b2014-08-11 18:43:38 -04001669func addStateMachineCoverageTests(async, splitHandshake bool, protocol protocol) {
David Benjamin43ec06f2014-08-05 02:28:57 -04001670 var suffix string
1671 var flags []string
1672 var maxHandshakeRecordLength int
David Benjamin6fd297b2014-08-11 18:43:38 -04001673 if protocol == dtls {
1674 suffix = "-DTLS"
1675 }
David Benjamin43ec06f2014-08-05 02:28:57 -04001676 if async {
David Benjamin6fd297b2014-08-11 18:43:38 -04001677 suffix += "-Async"
David Benjamin43ec06f2014-08-05 02:28:57 -04001678 flags = append(flags, "-async")
1679 } else {
David Benjamin6fd297b2014-08-11 18:43:38 -04001680 suffix += "-Sync"
David Benjamin43ec06f2014-08-05 02:28:57 -04001681 }
1682 if splitHandshake {
1683 suffix += "-SplitHandshakeRecords"
David Benjamin98214542014-08-07 18:02:39 -04001684 maxHandshakeRecordLength = 1
David Benjamin43ec06f2014-08-05 02:28:57 -04001685 }
1686
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001687 // Basic handshake, with resumption. Client and server,
1688 // session ID and session ticket.
David Benjamin43ec06f2014-08-05 02:28:57 -04001689 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001690 protocol: protocol,
1691 name: "Basic-Client" + suffix,
David Benjamin43ec06f2014-08-05 02:28:57 -04001692 config: Config{
1693 Bugs: ProtocolBugs{
1694 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1695 },
1696 },
David Benjaminbed9aae2014-08-07 19:13:38 -04001697 flags: flags,
1698 resumeSession: true,
1699 })
1700 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001701 protocol: protocol,
1702 name: "Basic-Client-RenewTicket" + suffix,
David Benjaminbed9aae2014-08-07 19:13:38 -04001703 config: Config{
1704 Bugs: ProtocolBugs{
1705 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1706 RenewTicketOnResume: true,
1707 },
1708 },
1709 flags: flags,
1710 resumeSession: true,
David Benjamin43ec06f2014-08-05 02:28:57 -04001711 })
1712 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001713 protocol: protocol,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001714 name: "Basic-Client-NoTicket" + suffix,
1715 config: Config{
1716 SessionTicketsDisabled: true,
1717 Bugs: ProtocolBugs{
1718 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1719 },
1720 },
1721 flags: flags,
1722 resumeSession: true,
1723 })
1724 testCases = append(testCases, testCase{
1725 protocol: protocol,
David Benjamine0e7d0d2015-02-08 19:33:25 -05001726 name: "Basic-Client-Implicit" + suffix,
1727 config: Config{
1728 Bugs: ProtocolBugs{
1729 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1730 },
1731 },
1732 flags: append(flags, "-implicit-handshake"),
1733 resumeSession: true,
1734 })
1735 testCases = append(testCases, testCase{
1736 protocol: protocol,
David Benjamin43ec06f2014-08-05 02:28:57 -04001737 testType: serverTest,
1738 name: "Basic-Server" + suffix,
1739 config: Config{
1740 Bugs: ProtocolBugs{
1741 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1742 },
1743 },
David Benjaminbed9aae2014-08-07 19:13:38 -04001744 flags: flags,
1745 resumeSession: true,
David Benjamin43ec06f2014-08-05 02:28:57 -04001746 })
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001747 testCases = append(testCases, testCase{
1748 protocol: protocol,
1749 testType: serverTest,
1750 name: "Basic-Server-NoTickets" + suffix,
1751 config: Config{
1752 SessionTicketsDisabled: true,
1753 Bugs: ProtocolBugs{
1754 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1755 },
1756 },
1757 flags: flags,
1758 resumeSession: true,
1759 })
David Benjamine0e7d0d2015-02-08 19:33:25 -05001760 testCases = append(testCases, testCase{
1761 protocol: protocol,
1762 testType: serverTest,
1763 name: "Basic-Server-Implicit" + suffix,
1764 config: Config{
1765 Bugs: ProtocolBugs{
1766 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1767 },
1768 },
1769 flags: append(flags, "-implicit-handshake"),
1770 resumeSession: true,
1771 })
David Benjamin6f5c0f42015-02-24 01:23:21 -05001772 testCases = append(testCases, testCase{
1773 protocol: protocol,
1774 testType: serverTest,
1775 name: "Basic-Server-EarlyCallback" + suffix,
1776 config: Config{
1777 Bugs: ProtocolBugs{
1778 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1779 },
1780 },
1781 flags: append(flags, "-use-early-callback"),
1782 resumeSession: true,
1783 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001784
David Benjamin6fd297b2014-08-11 18:43:38 -04001785 // TLS client auth.
1786 testCases = append(testCases, testCase{
1787 protocol: protocol,
1788 testType: clientTest,
1789 name: "ClientAuth-Client" + suffix,
1790 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001791 ClientAuth: RequireAnyClientCert,
David Benjamin6fd297b2014-08-11 18:43:38 -04001792 Bugs: ProtocolBugs{
1793 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1794 },
1795 },
1796 flags: append(flags,
1797 "-cert-file", rsaCertificateFile,
1798 "-key-file", rsaKeyFile),
1799 })
1800 testCases = append(testCases, testCase{
1801 protocol: protocol,
1802 testType: serverTest,
1803 name: "ClientAuth-Server" + suffix,
1804 config: Config{
1805 Certificates: []Certificate{rsaCertificate},
1806 },
1807 flags: append(flags, "-require-any-client-certificate"),
1808 })
1809
David Benjamin43ec06f2014-08-05 02:28:57 -04001810 // No session ticket support; server doesn't send NewSessionTicket.
1811 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001812 protocol: protocol,
1813 name: "SessionTicketsDisabled-Client" + suffix,
David Benjamin43ec06f2014-08-05 02:28:57 -04001814 config: Config{
1815 SessionTicketsDisabled: true,
1816 Bugs: ProtocolBugs{
1817 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1818 },
1819 },
1820 flags: flags,
1821 })
1822 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001823 protocol: protocol,
David Benjamin43ec06f2014-08-05 02:28:57 -04001824 testType: serverTest,
1825 name: "SessionTicketsDisabled-Server" + suffix,
1826 config: Config{
1827 SessionTicketsDisabled: true,
1828 Bugs: ProtocolBugs{
1829 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1830 },
1831 },
1832 flags: flags,
1833 })
1834
David Benjamin48cae082014-10-27 01:06:24 -04001835 // Skip ServerKeyExchange in PSK key exchange if there's no
1836 // identity hint.
1837 testCases = append(testCases, testCase{
1838 protocol: protocol,
1839 name: "EmptyPSKHint-Client" + suffix,
1840 config: Config{
1841 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
1842 PreSharedKey: []byte("secret"),
1843 Bugs: ProtocolBugs{
1844 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1845 },
1846 },
1847 flags: append(flags, "-psk", "secret"),
1848 })
1849 testCases = append(testCases, testCase{
1850 protocol: protocol,
1851 testType: serverTest,
1852 name: "EmptyPSKHint-Server" + suffix,
1853 config: Config{
1854 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
1855 PreSharedKey: []byte("secret"),
1856 Bugs: ProtocolBugs{
1857 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1858 },
1859 },
1860 flags: append(flags, "-psk", "secret"),
1861 })
1862
David Benjamin6fd297b2014-08-11 18:43:38 -04001863 if protocol == tls {
1864 // NPN on client and server; results in post-handshake message.
1865 testCases = append(testCases, testCase{
1866 protocol: protocol,
1867 name: "NPN-Client" + suffix,
1868 config: Config{
David Benjaminae2888f2014-09-06 12:58:58 -04001869 NextProtos: []string{"foo"},
David Benjamin6fd297b2014-08-11 18:43:38 -04001870 Bugs: ProtocolBugs{
1871 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1872 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001873 },
David Benjaminfc7b0862014-09-06 13:21:53 -04001874 flags: append(flags, "-select-next-proto", "foo"),
1875 expectedNextProto: "foo",
1876 expectedNextProtoType: npn,
David Benjamin6fd297b2014-08-11 18:43:38 -04001877 })
1878 testCases = append(testCases, testCase{
1879 protocol: protocol,
1880 testType: serverTest,
1881 name: "NPN-Server" + suffix,
1882 config: Config{
1883 NextProtos: []string{"bar"},
1884 Bugs: ProtocolBugs{
1885 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1886 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001887 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001888 flags: append(flags,
1889 "-advertise-npn", "\x03foo\x03bar\x03baz",
1890 "-expect-next-proto", "bar"),
David Benjaminfc7b0862014-09-06 13:21:53 -04001891 expectedNextProto: "bar",
1892 expectedNextProtoType: npn,
David Benjamin6fd297b2014-08-11 18:43:38 -04001893 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001894
David Benjamin195dc782015-02-19 13:27:05 -05001895 // TODO(davidben): Add tests for when False Start doesn't trigger.
1896
David Benjamin6fd297b2014-08-11 18:43:38 -04001897 // Client does False Start and negotiates NPN.
1898 testCases = append(testCases, testCase{
1899 protocol: protocol,
1900 name: "FalseStart" + suffix,
1901 config: Config{
1902 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1903 NextProtos: []string{"foo"},
1904 Bugs: ProtocolBugs{
David Benjamine58c4f52014-08-24 03:47:07 -04001905 ExpectFalseStart: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001906 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1907 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001908 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001909 flags: append(flags,
1910 "-false-start",
1911 "-select-next-proto", "foo"),
David Benjamine58c4f52014-08-24 03:47:07 -04001912 shimWritesFirst: true,
1913 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001914 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001915
David Benjaminae2888f2014-09-06 12:58:58 -04001916 // Client does False Start and negotiates ALPN.
1917 testCases = append(testCases, testCase{
1918 protocol: protocol,
1919 name: "FalseStart-ALPN" + suffix,
1920 config: Config{
1921 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1922 NextProtos: []string{"foo"},
1923 Bugs: ProtocolBugs{
1924 ExpectFalseStart: true,
1925 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1926 },
1927 },
1928 flags: append(flags,
1929 "-false-start",
1930 "-advertise-alpn", "\x03foo"),
1931 shimWritesFirst: true,
1932 resumeSession: true,
1933 })
1934
David Benjamin931ab342015-02-08 19:46:57 -05001935 // Client does False Start but doesn't explicitly call
1936 // SSL_connect.
1937 testCases = append(testCases, testCase{
1938 protocol: protocol,
1939 name: "FalseStart-Implicit" + suffix,
1940 config: Config{
1941 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1942 NextProtos: []string{"foo"},
1943 Bugs: ProtocolBugs{
1944 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1945 },
1946 },
1947 flags: append(flags,
1948 "-implicit-handshake",
1949 "-false-start",
1950 "-advertise-alpn", "\x03foo"),
1951 })
1952
David Benjamin6fd297b2014-08-11 18:43:38 -04001953 // False Start without session tickets.
1954 testCases = append(testCases, testCase{
David Benjamin5f237bc2015-02-11 17:14:15 -05001955 name: "FalseStart-SessionTicketsDisabled" + suffix,
David Benjamin6fd297b2014-08-11 18:43:38 -04001956 config: Config{
1957 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1958 NextProtos: []string{"foo"},
1959 SessionTicketsDisabled: true,
David Benjamin4e99c522014-08-24 01:45:30 -04001960 Bugs: ProtocolBugs{
David Benjamine58c4f52014-08-24 03:47:07 -04001961 ExpectFalseStart: true,
David Benjamin4e99c522014-08-24 01:45:30 -04001962 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1963 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001964 },
David Benjamin4e99c522014-08-24 01:45:30 -04001965 flags: append(flags,
David Benjamin6fd297b2014-08-11 18:43:38 -04001966 "-false-start",
1967 "-select-next-proto", "foo",
David Benjamin4e99c522014-08-24 01:45:30 -04001968 ),
David Benjamine58c4f52014-08-24 03:47:07 -04001969 shimWritesFirst: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001970 })
David Benjamin1e7f8d72014-08-08 12:27:04 -04001971
David Benjamina08e49d2014-08-24 01:46:07 -04001972 // Server parses a V2ClientHello.
David Benjamin6fd297b2014-08-11 18:43:38 -04001973 testCases = append(testCases, testCase{
1974 protocol: protocol,
1975 testType: serverTest,
1976 name: "SendV2ClientHello" + suffix,
1977 config: Config{
1978 // Choose a cipher suite that does not involve
1979 // elliptic curves, so no extensions are
1980 // involved.
1981 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
1982 Bugs: ProtocolBugs{
1983 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1984 SendV2ClientHello: true,
1985 },
David Benjamin1e7f8d72014-08-08 12:27:04 -04001986 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001987 flags: flags,
1988 })
David Benjamina08e49d2014-08-24 01:46:07 -04001989
1990 // Client sends a Channel ID.
1991 testCases = append(testCases, testCase{
1992 protocol: protocol,
1993 name: "ChannelID-Client" + suffix,
1994 config: Config{
1995 RequestChannelID: true,
1996 Bugs: ProtocolBugs{
1997 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1998 },
1999 },
2000 flags: append(flags,
2001 "-send-channel-id", channelIDKeyFile,
2002 ),
2003 resumeSession: true,
2004 expectChannelID: true,
2005 })
2006
2007 // Server accepts a Channel ID.
2008 testCases = append(testCases, testCase{
2009 protocol: protocol,
2010 testType: serverTest,
2011 name: "ChannelID-Server" + suffix,
2012 config: Config{
2013 ChannelID: channelIDKey,
2014 Bugs: ProtocolBugs{
2015 MaxHandshakeRecordLength: maxHandshakeRecordLength,
2016 },
2017 },
2018 flags: append(flags,
2019 "-expect-channel-id",
2020 base64.StdEncoding.EncodeToString(channelIDBytes),
2021 ),
2022 resumeSession: true,
2023 expectChannelID: true,
2024 })
David Benjamin6fd297b2014-08-11 18:43:38 -04002025 } else {
2026 testCases = append(testCases, testCase{
2027 protocol: protocol,
2028 name: "SkipHelloVerifyRequest" + suffix,
2029 config: Config{
2030 Bugs: ProtocolBugs{
2031 MaxHandshakeRecordLength: maxHandshakeRecordLength,
2032 SkipHelloVerifyRequest: true,
2033 },
2034 },
2035 flags: flags,
2036 })
David Benjamin6fd297b2014-08-11 18:43:38 -04002037 }
David Benjamin43ec06f2014-08-05 02:28:57 -04002038}
2039
Adam Langley524e7172015-02-20 16:04:00 -08002040func addDDoSCallbackTests() {
2041 // DDoS callback.
2042
2043 for _, resume := range []bool{false, true} {
2044 suffix := "Resume"
2045 if resume {
2046 suffix = "No" + suffix
2047 }
2048
2049 testCases = append(testCases, testCase{
2050 testType: serverTest,
2051 name: "Server-DDoS-OK-" + suffix,
2052 flags: []string{"-install-ddos-callback"},
2053 resumeSession: resume,
2054 })
2055
2056 failFlag := "-fail-ddos-callback"
2057 if resume {
2058 failFlag = "-fail-second-ddos-callback"
2059 }
2060 testCases = append(testCases, testCase{
2061 testType: serverTest,
2062 name: "Server-DDoS-Reject-" + suffix,
2063 flags: []string{"-install-ddos-callback", failFlag},
2064 resumeSession: resume,
2065 shouldFail: true,
2066 expectedError: ":CONNECTION_REJECTED:",
2067 })
2068 }
2069}
2070
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002071func addVersionNegotiationTests() {
2072 for i, shimVers := range tlsVersions {
2073 // Assemble flags to disable all newer versions on the shim.
2074 var flags []string
2075 for _, vers := range tlsVersions[i+1:] {
2076 flags = append(flags, vers.flag)
2077 }
2078
2079 for _, runnerVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05002080 protocols := []protocol{tls}
2081 if runnerVers.hasDTLS && shimVers.hasDTLS {
2082 protocols = append(protocols, dtls)
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002083 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002084 for _, protocol := range protocols {
2085 expectedVersion := shimVers.version
2086 if runnerVers.version < shimVers.version {
2087 expectedVersion = runnerVers.version
2088 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002089
David Benjamin8b8c0062014-11-23 02:47:52 -05002090 suffix := shimVers.name + "-" + runnerVers.name
2091 if protocol == dtls {
2092 suffix += "-DTLS"
2093 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002094
David Benjamin1eb367c2014-12-12 18:17:51 -05002095 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
2096
David Benjamin1e29a6b2014-12-10 02:27:24 -05002097 clientVers := shimVers.version
2098 if clientVers > VersionTLS10 {
2099 clientVers = VersionTLS10
2100 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002101 testCases = append(testCases, testCase{
2102 protocol: protocol,
2103 testType: clientTest,
2104 name: "VersionNegotiation-Client-" + suffix,
2105 config: Config{
2106 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05002107 Bugs: ProtocolBugs{
2108 ExpectInitialRecordVersion: clientVers,
2109 },
David Benjamin8b8c0062014-11-23 02:47:52 -05002110 },
2111 flags: flags,
2112 expectedVersion: expectedVersion,
2113 })
David Benjamin1eb367c2014-12-12 18:17:51 -05002114 testCases = append(testCases, testCase{
2115 protocol: protocol,
2116 testType: clientTest,
2117 name: "VersionNegotiation-Client2-" + suffix,
2118 config: Config{
2119 MaxVersion: runnerVers.version,
2120 Bugs: ProtocolBugs{
2121 ExpectInitialRecordVersion: clientVers,
2122 },
2123 },
2124 flags: []string{"-max-version", shimVersFlag},
2125 expectedVersion: expectedVersion,
2126 })
David Benjamin8b8c0062014-11-23 02:47:52 -05002127
2128 testCases = append(testCases, testCase{
2129 protocol: protocol,
2130 testType: serverTest,
2131 name: "VersionNegotiation-Server-" + suffix,
2132 config: Config{
2133 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05002134 Bugs: ProtocolBugs{
2135 ExpectInitialRecordVersion: expectedVersion,
2136 },
David Benjamin8b8c0062014-11-23 02:47:52 -05002137 },
2138 flags: flags,
2139 expectedVersion: expectedVersion,
2140 })
David Benjamin1eb367c2014-12-12 18:17:51 -05002141 testCases = append(testCases, testCase{
2142 protocol: protocol,
2143 testType: serverTest,
2144 name: "VersionNegotiation-Server2-" + suffix,
2145 config: Config{
2146 MaxVersion: runnerVers.version,
2147 Bugs: ProtocolBugs{
2148 ExpectInitialRecordVersion: expectedVersion,
2149 },
2150 },
2151 flags: []string{"-max-version", shimVersFlag},
2152 expectedVersion: expectedVersion,
2153 })
David Benjamin8b8c0062014-11-23 02:47:52 -05002154 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002155 }
2156 }
2157}
2158
David Benjaminaccb4542014-12-12 23:44:33 -05002159func addMinimumVersionTests() {
2160 for i, shimVers := range tlsVersions {
2161 // Assemble flags to disable all older versions on the shim.
2162 var flags []string
2163 for _, vers := range tlsVersions[:i] {
2164 flags = append(flags, vers.flag)
2165 }
2166
2167 for _, runnerVers := range tlsVersions {
2168 protocols := []protocol{tls}
2169 if runnerVers.hasDTLS && shimVers.hasDTLS {
2170 protocols = append(protocols, dtls)
2171 }
2172 for _, protocol := range protocols {
2173 suffix := shimVers.name + "-" + runnerVers.name
2174 if protocol == dtls {
2175 suffix += "-DTLS"
2176 }
2177 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
2178
David Benjaminaccb4542014-12-12 23:44:33 -05002179 var expectedVersion uint16
2180 var shouldFail bool
2181 var expectedError string
David Benjamin87909c02014-12-13 01:55:01 -05002182 var expectedLocalError string
David Benjaminaccb4542014-12-12 23:44:33 -05002183 if runnerVers.version >= shimVers.version {
2184 expectedVersion = runnerVers.version
2185 } else {
2186 shouldFail = true
2187 expectedError = ":UNSUPPORTED_PROTOCOL:"
David Benjamin87909c02014-12-13 01:55:01 -05002188 if runnerVers.version > VersionSSL30 {
2189 expectedLocalError = "remote error: protocol version not supported"
2190 } else {
2191 expectedLocalError = "remote error: handshake failure"
2192 }
David Benjaminaccb4542014-12-12 23:44:33 -05002193 }
2194
2195 testCases = append(testCases, testCase{
2196 protocol: protocol,
2197 testType: clientTest,
2198 name: "MinimumVersion-Client-" + suffix,
2199 config: Config{
2200 MaxVersion: runnerVers.version,
2201 },
David Benjamin87909c02014-12-13 01:55:01 -05002202 flags: flags,
2203 expectedVersion: expectedVersion,
2204 shouldFail: shouldFail,
2205 expectedError: expectedError,
2206 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002207 })
2208 testCases = append(testCases, testCase{
2209 protocol: protocol,
2210 testType: clientTest,
2211 name: "MinimumVersion-Client2-" + suffix,
2212 config: Config{
2213 MaxVersion: runnerVers.version,
2214 },
David Benjamin87909c02014-12-13 01:55:01 -05002215 flags: []string{"-min-version", shimVersFlag},
2216 expectedVersion: expectedVersion,
2217 shouldFail: shouldFail,
2218 expectedError: expectedError,
2219 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002220 })
2221
2222 testCases = append(testCases, testCase{
2223 protocol: protocol,
2224 testType: serverTest,
2225 name: "MinimumVersion-Server-" + suffix,
2226 config: Config{
2227 MaxVersion: runnerVers.version,
2228 },
David Benjamin87909c02014-12-13 01:55:01 -05002229 flags: flags,
2230 expectedVersion: expectedVersion,
2231 shouldFail: shouldFail,
2232 expectedError: expectedError,
2233 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002234 })
2235 testCases = append(testCases, testCase{
2236 protocol: protocol,
2237 testType: serverTest,
2238 name: "MinimumVersion-Server2-" + suffix,
2239 config: Config{
2240 MaxVersion: runnerVers.version,
2241 },
David Benjamin87909c02014-12-13 01:55:01 -05002242 flags: []string{"-min-version", shimVersFlag},
2243 expectedVersion: expectedVersion,
2244 shouldFail: shouldFail,
2245 expectedError: expectedError,
2246 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002247 })
2248 }
2249 }
2250 }
2251}
2252
David Benjamin5c24a1d2014-08-31 00:59:27 -04002253func addD5BugTests() {
2254 testCases = append(testCases, testCase{
2255 testType: serverTest,
2256 name: "D5Bug-NoQuirk-Reject",
2257 config: Config{
2258 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2259 Bugs: ProtocolBugs{
2260 SSL3RSAKeyExchange: true,
2261 },
2262 },
2263 shouldFail: true,
2264 expectedError: ":TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG:",
2265 })
2266 testCases = append(testCases, testCase{
2267 testType: serverTest,
2268 name: "D5Bug-Quirk-Normal",
2269 config: Config{
2270 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2271 },
2272 flags: []string{"-tls-d5-bug"},
2273 })
2274 testCases = append(testCases, testCase{
2275 testType: serverTest,
2276 name: "D5Bug-Quirk-Bug",
2277 config: Config{
2278 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2279 Bugs: ProtocolBugs{
2280 SSL3RSAKeyExchange: true,
2281 },
2282 },
2283 flags: []string{"-tls-d5-bug"},
2284 })
2285}
2286
David Benjamine78bfde2014-09-06 12:45:15 -04002287func addExtensionTests() {
2288 testCases = append(testCases, testCase{
2289 testType: clientTest,
2290 name: "DuplicateExtensionClient",
2291 config: Config{
2292 Bugs: ProtocolBugs{
2293 DuplicateExtension: true,
2294 },
2295 },
2296 shouldFail: true,
2297 expectedLocalError: "remote error: error decoding message",
2298 })
2299 testCases = append(testCases, testCase{
2300 testType: serverTest,
2301 name: "DuplicateExtensionServer",
2302 config: Config{
2303 Bugs: ProtocolBugs{
2304 DuplicateExtension: true,
2305 },
2306 },
2307 shouldFail: true,
2308 expectedLocalError: "remote error: error decoding message",
2309 })
2310 testCases = append(testCases, testCase{
2311 testType: clientTest,
2312 name: "ServerNameExtensionClient",
2313 config: Config{
2314 Bugs: ProtocolBugs{
2315 ExpectServerName: "example.com",
2316 },
2317 },
2318 flags: []string{"-host-name", "example.com"},
2319 })
2320 testCases = append(testCases, testCase{
2321 testType: clientTest,
David Benjamin5f237bc2015-02-11 17:14:15 -05002322 name: "ServerNameExtensionClientMismatch",
David Benjamine78bfde2014-09-06 12:45:15 -04002323 config: Config{
2324 Bugs: ProtocolBugs{
2325 ExpectServerName: "mismatch.com",
2326 },
2327 },
2328 flags: []string{"-host-name", "example.com"},
2329 shouldFail: true,
2330 expectedLocalError: "tls: unexpected server name",
2331 })
2332 testCases = append(testCases, testCase{
2333 testType: clientTest,
David Benjamin5f237bc2015-02-11 17:14:15 -05002334 name: "ServerNameExtensionClientMissing",
David Benjamine78bfde2014-09-06 12:45:15 -04002335 config: Config{
2336 Bugs: ProtocolBugs{
2337 ExpectServerName: "missing.com",
2338 },
2339 },
2340 shouldFail: true,
2341 expectedLocalError: "tls: unexpected server name",
2342 })
2343 testCases = append(testCases, testCase{
2344 testType: serverTest,
2345 name: "ServerNameExtensionServer",
2346 config: Config{
2347 ServerName: "example.com",
2348 },
2349 flags: []string{"-expect-server-name", "example.com"},
2350 resumeSession: true,
2351 })
David Benjaminae2888f2014-09-06 12:58:58 -04002352 testCases = append(testCases, testCase{
2353 testType: clientTest,
2354 name: "ALPNClient",
2355 config: Config{
2356 NextProtos: []string{"foo"},
2357 },
2358 flags: []string{
2359 "-advertise-alpn", "\x03foo\x03bar\x03baz",
2360 "-expect-alpn", "foo",
2361 },
David Benjaminfc7b0862014-09-06 13:21:53 -04002362 expectedNextProto: "foo",
2363 expectedNextProtoType: alpn,
2364 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04002365 })
2366 testCases = append(testCases, testCase{
2367 testType: serverTest,
2368 name: "ALPNServer",
2369 config: Config{
2370 NextProtos: []string{"foo", "bar", "baz"},
2371 },
2372 flags: []string{
2373 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2374 "-select-alpn", "foo",
2375 },
David Benjaminfc7b0862014-09-06 13:21:53 -04002376 expectedNextProto: "foo",
2377 expectedNextProtoType: alpn,
2378 resumeSession: true,
2379 })
2380 // Test that the server prefers ALPN over NPN.
2381 testCases = append(testCases, testCase{
2382 testType: serverTest,
2383 name: "ALPNServer-Preferred",
2384 config: Config{
2385 NextProtos: []string{"foo", "bar", "baz"},
2386 },
2387 flags: []string{
2388 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2389 "-select-alpn", "foo",
2390 "-advertise-npn", "\x03foo\x03bar\x03baz",
2391 },
2392 expectedNextProto: "foo",
2393 expectedNextProtoType: alpn,
2394 resumeSession: true,
2395 })
2396 testCases = append(testCases, testCase{
2397 testType: serverTest,
2398 name: "ALPNServer-Preferred-Swapped",
2399 config: Config{
2400 NextProtos: []string{"foo", "bar", "baz"},
2401 Bugs: ProtocolBugs{
2402 SwapNPNAndALPN: true,
2403 },
2404 },
2405 flags: []string{
2406 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2407 "-select-alpn", "foo",
2408 "-advertise-npn", "\x03foo\x03bar\x03baz",
2409 },
2410 expectedNextProto: "foo",
2411 expectedNextProtoType: alpn,
2412 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04002413 })
Adam Langley38311732014-10-16 19:04:35 -07002414 // Resume with a corrupt ticket.
2415 testCases = append(testCases, testCase{
2416 testType: serverTest,
2417 name: "CorruptTicket",
2418 config: Config{
2419 Bugs: ProtocolBugs{
2420 CorruptTicket: true,
2421 },
2422 },
2423 resumeSession: true,
2424 flags: []string{"-expect-session-miss"},
2425 })
2426 // Resume with an oversized session id.
2427 testCases = append(testCases, testCase{
2428 testType: serverTest,
2429 name: "OversizedSessionId",
2430 config: Config{
2431 Bugs: ProtocolBugs{
2432 OversizedSessionId: true,
2433 },
2434 },
2435 resumeSession: true,
Adam Langley75712922014-10-10 16:23:43 -07002436 shouldFail: true,
Adam Langley38311732014-10-16 19:04:35 -07002437 expectedError: ":DECODE_ERROR:",
2438 })
David Benjaminca6c8262014-11-15 19:06:08 -05002439 // Basic DTLS-SRTP tests. Include fake profiles to ensure they
2440 // are ignored.
2441 testCases = append(testCases, testCase{
2442 protocol: dtls,
2443 name: "SRTP-Client",
2444 config: Config{
2445 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
2446 },
2447 flags: []string{
2448 "-srtp-profiles",
2449 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2450 },
2451 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2452 })
2453 testCases = append(testCases, testCase{
2454 protocol: dtls,
2455 testType: serverTest,
2456 name: "SRTP-Server",
2457 config: Config{
2458 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
2459 },
2460 flags: []string{
2461 "-srtp-profiles",
2462 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2463 },
2464 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2465 })
2466 // Test that the MKI is ignored.
2467 testCases = append(testCases, testCase{
2468 protocol: dtls,
2469 testType: serverTest,
2470 name: "SRTP-Server-IgnoreMKI",
2471 config: Config{
2472 SRTPProtectionProfiles: []uint16{SRTP_AES128_CM_HMAC_SHA1_80},
2473 Bugs: ProtocolBugs{
2474 SRTPMasterKeyIdentifer: "bogus",
2475 },
2476 },
2477 flags: []string{
2478 "-srtp-profiles",
2479 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2480 },
2481 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2482 })
2483 // Test that SRTP isn't negotiated on the server if there were
2484 // no matching profiles.
2485 testCases = append(testCases, testCase{
2486 protocol: dtls,
2487 testType: serverTest,
2488 name: "SRTP-Server-NoMatch",
2489 config: Config{
2490 SRTPProtectionProfiles: []uint16{100, 101, 102},
2491 },
2492 flags: []string{
2493 "-srtp-profiles",
2494 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2495 },
2496 expectedSRTPProtectionProfile: 0,
2497 })
2498 // Test that the server returning an invalid SRTP profile is
2499 // flagged as an error by the client.
2500 testCases = append(testCases, testCase{
2501 protocol: dtls,
2502 name: "SRTP-Client-NoMatch",
2503 config: Config{
2504 Bugs: ProtocolBugs{
2505 SendSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_32,
2506 },
2507 },
2508 flags: []string{
2509 "-srtp-profiles",
2510 "SRTP_AES128_CM_SHA1_80",
2511 },
2512 shouldFail: true,
2513 expectedError: ":BAD_SRTP_PROTECTION_PROFILE_LIST:",
2514 })
David Benjamin61f95272014-11-25 01:55:35 -05002515 // Test OCSP stapling and SCT list.
2516 testCases = append(testCases, testCase{
2517 name: "OCSPStapling",
2518 flags: []string{
2519 "-enable-ocsp-stapling",
2520 "-expect-ocsp-response",
2521 base64.StdEncoding.EncodeToString(testOCSPResponse),
2522 },
2523 })
2524 testCases = append(testCases, testCase{
2525 name: "SignedCertificateTimestampList",
2526 flags: []string{
2527 "-enable-signed-cert-timestamps",
2528 "-expect-signed-cert-timestamps",
2529 base64.StdEncoding.EncodeToString(testSCTList),
2530 },
2531 })
David Benjamine78bfde2014-09-06 12:45:15 -04002532}
2533
David Benjamin01fe8202014-09-24 15:21:44 -04002534func addResumptionVersionTests() {
David Benjamin01fe8202014-09-24 15:21:44 -04002535 for _, sessionVers := range tlsVersions {
David Benjamin01fe8202014-09-24 15:21:44 -04002536 for _, resumeVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05002537 protocols := []protocol{tls}
2538 if sessionVers.hasDTLS && resumeVers.hasDTLS {
2539 protocols = append(protocols, dtls)
David Benjaminbdf5e722014-11-11 00:52:15 -05002540 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002541 for _, protocol := range protocols {
2542 suffix := "-" + sessionVers.name + "-" + resumeVers.name
2543 if protocol == dtls {
2544 suffix += "-DTLS"
2545 }
2546
2547 testCases = append(testCases, testCase{
2548 protocol: protocol,
2549 name: "Resume-Client" + suffix,
2550 resumeSession: true,
2551 config: Config{
2552 MaxVersion: sessionVers.version,
2553 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2554 Bugs: ProtocolBugs{
2555 AllowSessionVersionMismatch: true,
2556 },
2557 },
2558 expectedVersion: sessionVers.version,
2559 resumeConfig: &Config{
2560 MaxVersion: resumeVers.version,
2561 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2562 Bugs: ProtocolBugs{
2563 AllowSessionVersionMismatch: true,
2564 },
2565 },
2566 expectedResumeVersion: resumeVers.version,
2567 })
2568
2569 testCases = append(testCases, testCase{
2570 protocol: protocol,
2571 name: "Resume-Client-NoResume" + suffix,
2572 flags: []string{"-expect-session-miss"},
2573 resumeSession: true,
2574 config: Config{
2575 MaxVersion: sessionVers.version,
2576 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2577 },
2578 expectedVersion: sessionVers.version,
2579 resumeConfig: &Config{
2580 MaxVersion: resumeVers.version,
2581 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2582 },
2583 newSessionsOnResume: true,
2584 expectedResumeVersion: resumeVers.version,
2585 })
2586
2587 var flags []string
2588 if sessionVers.version != resumeVers.version {
2589 flags = append(flags, "-expect-session-miss")
2590 }
2591 testCases = append(testCases, testCase{
2592 protocol: protocol,
2593 testType: serverTest,
2594 name: "Resume-Server" + suffix,
2595 flags: flags,
2596 resumeSession: true,
2597 config: Config{
2598 MaxVersion: sessionVers.version,
2599 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2600 },
2601 expectedVersion: sessionVers.version,
2602 resumeConfig: &Config{
2603 MaxVersion: resumeVers.version,
2604 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2605 },
2606 expectedResumeVersion: resumeVers.version,
2607 })
2608 }
David Benjamin01fe8202014-09-24 15:21:44 -04002609 }
2610 }
2611}
2612
Adam Langley2ae77d22014-10-28 17:29:33 -07002613func addRenegotiationTests() {
2614 testCases = append(testCases, testCase{
2615 testType: serverTest,
2616 name: "Renegotiate-Server",
2617 flags: []string{"-renegotiate"},
2618 shimWritesFirst: true,
2619 })
2620 testCases = append(testCases, testCase{
2621 testType: serverTest,
David Benjamincdea40c2015-03-19 14:09:43 -04002622 name: "Renegotiate-Server-Full",
2623 config: Config{
2624 Bugs: ProtocolBugs{
2625 NeverResumeOnRenego: true,
2626 },
2627 },
2628 flags: []string{"-renegotiate"},
2629 shimWritesFirst: true,
2630 })
2631 testCases = append(testCases, testCase{
2632 testType: serverTest,
Adam Langley2ae77d22014-10-28 17:29:33 -07002633 name: "Renegotiate-Server-EmptyExt",
2634 config: Config{
2635 Bugs: ProtocolBugs{
2636 EmptyRenegotiationInfo: true,
2637 },
2638 },
2639 flags: []string{"-renegotiate"},
2640 shimWritesFirst: true,
2641 shouldFail: true,
2642 expectedError: ":RENEGOTIATION_MISMATCH:",
2643 })
2644 testCases = append(testCases, testCase{
2645 testType: serverTest,
2646 name: "Renegotiate-Server-BadExt",
2647 config: Config{
2648 Bugs: ProtocolBugs{
2649 BadRenegotiationInfo: true,
2650 },
2651 },
2652 flags: []string{"-renegotiate"},
2653 shimWritesFirst: true,
2654 shouldFail: true,
2655 expectedError: ":RENEGOTIATION_MISMATCH:",
2656 })
David Benjaminca6554b2014-11-08 12:31:52 -05002657 testCases = append(testCases, testCase{
2658 testType: serverTest,
2659 name: "Renegotiate-Server-ClientInitiated",
2660 renegotiate: true,
2661 })
2662 testCases = append(testCases, testCase{
2663 testType: serverTest,
2664 name: "Renegotiate-Server-ClientInitiated-NoExt",
2665 renegotiate: true,
2666 config: Config{
2667 Bugs: ProtocolBugs{
2668 NoRenegotiationInfo: true,
2669 },
2670 },
2671 shouldFail: true,
2672 expectedError: ":UNSAFE_LEGACY_RENEGOTIATION_DISABLED:",
2673 })
2674 testCases = append(testCases, testCase{
2675 testType: serverTest,
2676 name: "Renegotiate-Server-ClientInitiated-NoExt-Allowed",
2677 renegotiate: true,
2678 config: Config{
2679 Bugs: ProtocolBugs{
2680 NoRenegotiationInfo: true,
2681 },
2682 },
2683 flags: []string{"-allow-unsafe-legacy-renegotiation"},
2684 })
David Benjamin3c9746a2015-03-19 15:00:10 -04002685 // Regression test for CVE-2015-0291.
2686 testCases = append(testCases, testCase{
2687 testType: serverTest,
2688 name: "Renegotiate-Server-NoSignatureAlgorithms",
2689 config: Config{
2690 Bugs: ProtocolBugs{
2691 NeverResumeOnRenego: true,
2692 NoSignatureAlgorithmsOnRenego: true,
2693 },
2694 },
2695 flags: []string{"-renegotiate"},
2696 shimWritesFirst: true,
2697 })
Adam Langley2ae77d22014-10-28 17:29:33 -07002698 // TODO(agl): test the renegotiation info SCSV.
Adam Langleycf2d4f42014-10-28 19:06:14 -07002699 testCases = append(testCases, testCase{
2700 name: "Renegotiate-Client",
2701 renegotiate: true,
2702 })
2703 testCases = append(testCases, testCase{
David Benjamincdea40c2015-03-19 14:09:43 -04002704 name: "Renegotiate-Client-Full",
2705 config: Config{
2706 Bugs: ProtocolBugs{
2707 NeverResumeOnRenego: true,
2708 },
2709 },
2710 renegotiate: true,
2711 })
2712 testCases = append(testCases, testCase{
Adam Langleycf2d4f42014-10-28 19:06:14 -07002713 name: "Renegotiate-Client-EmptyExt",
2714 renegotiate: true,
2715 config: Config{
2716 Bugs: ProtocolBugs{
2717 EmptyRenegotiationInfo: true,
2718 },
2719 },
2720 shouldFail: true,
2721 expectedError: ":RENEGOTIATION_MISMATCH:",
2722 })
2723 testCases = append(testCases, testCase{
2724 name: "Renegotiate-Client-BadExt",
2725 renegotiate: true,
2726 config: Config{
2727 Bugs: ProtocolBugs{
2728 BadRenegotiationInfo: true,
2729 },
2730 },
2731 shouldFail: true,
2732 expectedError: ":RENEGOTIATION_MISMATCH:",
2733 })
2734 testCases = append(testCases, testCase{
2735 name: "Renegotiate-Client-SwitchCiphers",
2736 renegotiate: true,
2737 config: Config{
2738 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2739 },
2740 renegotiateCiphers: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2741 })
2742 testCases = append(testCases, testCase{
2743 name: "Renegotiate-Client-SwitchCiphers2",
2744 renegotiate: true,
2745 config: Config{
2746 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2747 },
2748 renegotiateCiphers: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2749 })
David Benjaminc44b1df2014-11-23 12:11:01 -05002750 testCases = append(testCases, testCase{
2751 name: "Renegotiate-SameClientVersion",
2752 renegotiate: true,
2753 config: Config{
2754 MaxVersion: VersionTLS10,
2755 Bugs: ProtocolBugs{
2756 RequireSameRenegoClientVersion: true,
2757 },
2758 },
2759 })
Adam Langley2ae77d22014-10-28 17:29:33 -07002760}
2761
David Benjamin5e961c12014-11-07 01:48:35 -05002762func addDTLSReplayTests() {
2763 // Test that sequence number replays are detected.
2764 testCases = append(testCases, testCase{
2765 protocol: dtls,
2766 name: "DTLS-Replay",
2767 replayWrites: true,
2768 })
2769
2770 // Test the outgoing sequence number skipping by values larger
2771 // than the retransmit window.
2772 testCases = append(testCases, testCase{
2773 protocol: dtls,
2774 name: "DTLS-Replay-LargeGaps",
2775 config: Config{
2776 Bugs: ProtocolBugs{
2777 SequenceNumberIncrement: 127,
2778 },
2779 },
2780 replayWrites: true,
2781 })
2782}
2783
Feng Lu41aa3252014-11-21 22:47:56 -08002784func addFastRadioPaddingTests() {
David Benjamin1e29a6b2014-12-10 02:27:24 -05002785 testCases = append(testCases, testCase{
2786 protocol: tls,
2787 name: "FastRadio-Padding",
Feng Lu41aa3252014-11-21 22:47:56 -08002788 config: Config{
2789 Bugs: ProtocolBugs{
2790 RequireFastradioPadding: true,
2791 },
2792 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05002793 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08002794 })
David Benjamin1e29a6b2014-12-10 02:27:24 -05002795 testCases = append(testCases, testCase{
2796 protocol: dtls,
David Benjamin5f237bc2015-02-11 17:14:15 -05002797 name: "FastRadio-Padding-DTLS",
Feng Lu41aa3252014-11-21 22:47:56 -08002798 config: Config{
2799 Bugs: ProtocolBugs{
2800 RequireFastradioPadding: true,
2801 },
2802 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05002803 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08002804 })
2805}
2806
David Benjamin000800a2014-11-14 01:43:59 -05002807var testHashes = []struct {
2808 name string
2809 id uint8
2810}{
2811 {"SHA1", hashSHA1},
2812 {"SHA224", hashSHA224},
2813 {"SHA256", hashSHA256},
2814 {"SHA384", hashSHA384},
2815 {"SHA512", hashSHA512},
2816}
2817
2818func addSigningHashTests() {
2819 // Make sure each hash works. Include some fake hashes in the list and
2820 // ensure they're ignored.
2821 for _, hash := range testHashes {
2822 testCases = append(testCases, testCase{
2823 name: "SigningHash-ClientAuth-" + hash.name,
2824 config: Config{
2825 ClientAuth: RequireAnyClientCert,
2826 SignatureAndHashes: []signatureAndHash{
2827 {signatureRSA, 42},
2828 {signatureRSA, hash.id},
2829 {signatureRSA, 255},
2830 },
2831 },
2832 flags: []string{
2833 "-cert-file", rsaCertificateFile,
2834 "-key-file", rsaKeyFile,
2835 },
2836 })
2837
2838 testCases = append(testCases, testCase{
2839 testType: serverTest,
2840 name: "SigningHash-ServerKeyExchange-Sign-" + hash.name,
2841 config: Config{
2842 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2843 SignatureAndHashes: []signatureAndHash{
2844 {signatureRSA, 42},
2845 {signatureRSA, hash.id},
2846 {signatureRSA, 255},
2847 },
2848 },
2849 })
2850 }
2851
2852 // Test that hash resolution takes the signature type into account.
2853 testCases = append(testCases, testCase{
2854 name: "SigningHash-ClientAuth-SignatureType",
2855 config: Config{
2856 ClientAuth: RequireAnyClientCert,
2857 SignatureAndHashes: []signatureAndHash{
2858 {signatureECDSA, hashSHA512},
2859 {signatureRSA, hashSHA384},
2860 {signatureECDSA, hashSHA1},
2861 },
2862 },
2863 flags: []string{
2864 "-cert-file", rsaCertificateFile,
2865 "-key-file", rsaKeyFile,
2866 },
2867 })
2868
2869 testCases = append(testCases, testCase{
2870 testType: serverTest,
2871 name: "SigningHash-ServerKeyExchange-SignatureType",
2872 config: Config{
2873 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2874 SignatureAndHashes: []signatureAndHash{
2875 {signatureECDSA, hashSHA512},
2876 {signatureRSA, hashSHA384},
2877 {signatureECDSA, hashSHA1},
2878 },
2879 },
2880 })
2881
2882 // Test that, if the list is missing, the peer falls back to SHA-1.
2883 testCases = append(testCases, testCase{
2884 name: "SigningHash-ClientAuth-Fallback",
2885 config: Config{
2886 ClientAuth: RequireAnyClientCert,
2887 SignatureAndHashes: []signatureAndHash{
2888 {signatureRSA, hashSHA1},
2889 },
2890 Bugs: ProtocolBugs{
2891 NoSignatureAndHashes: true,
2892 },
2893 },
2894 flags: []string{
2895 "-cert-file", rsaCertificateFile,
2896 "-key-file", rsaKeyFile,
2897 },
2898 })
2899
2900 testCases = append(testCases, testCase{
2901 testType: serverTest,
2902 name: "SigningHash-ServerKeyExchange-Fallback",
2903 config: Config{
2904 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2905 SignatureAndHashes: []signatureAndHash{
2906 {signatureRSA, hashSHA1},
2907 },
2908 Bugs: ProtocolBugs{
2909 NoSignatureAndHashes: true,
2910 },
2911 },
2912 })
2913}
2914
David Benjamin83f90402015-01-27 01:09:43 -05002915// timeouts is the retransmit schedule for BoringSSL. It doubles and
2916// caps at 60 seconds. On the 13th timeout, it gives up.
2917var timeouts = []time.Duration{
2918 1 * time.Second,
2919 2 * time.Second,
2920 4 * time.Second,
2921 8 * time.Second,
2922 16 * time.Second,
2923 32 * time.Second,
2924 60 * time.Second,
2925 60 * time.Second,
2926 60 * time.Second,
2927 60 * time.Second,
2928 60 * time.Second,
2929 60 * time.Second,
2930 60 * time.Second,
2931}
2932
2933func addDTLSRetransmitTests() {
2934 // Test that this is indeed the timeout schedule. Stress all
2935 // four patterns of handshake.
2936 for i := 1; i < len(timeouts); i++ {
2937 number := strconv.Itoa(i)
2938 testCases = append(testCases, testCase{
2939 protocol: dtls,
2940 name: "DTLS-Retransmit-Client-" + number,
2941 config: Config{
2942 Bugs: ProtocolBugs{
2943 TimeoutSchedule: timeouts[:i],
2944 },
2945 },
2946 resumeSession: true,
2947 flags: []string{"-async"},
2948 })
2949 testCases = append(testCases, testCase{
2950 protocol: dtls,
2951 testType: serverTest,
2952 name: "DTLS-Retransmit-Server-" + number,
2953 config: Config{
2954 Bugs: ProtocolBugs{
2955 TimeoutSchedule: timeouts[:i],
2956 },
2957 },
2958 resumeSession: true,
2959 flags: []string{"-async"},
2960 })
2961 }
2962
2963 // Test that exceeding the timeout schedule hits a read
2964 // timeout.
2965 testCases = append(testCases, testCase{
2966 protocol: dtls,
2967 name: "DTLS-Retransmit-Timeout",
2968 config: Config{
2969 Bugs: ProtocolBugs{
2970 TimeoutSchedule: timeouts,
2971 },
2972 },
2973 resumeSession: true,
2974 flags: []string{"-async"},
2975 shouldFail: true,
2976 expectedError: ":READ_TIMEOUT_EXPIRED:",
2977 })
2978
2979 // Test that timeout handling has a fudge factor, due to API
2980 // problems.
2981 testCases = append(testCases, testCase{
2982 protocol: dtls,
2983 name: "DTLS-Retransmit-Fudge",
2984 config: Config{
2985 Bugs: ProtocolBugs{
2986 TimeoutSchedule: []time.Duration{
2987 timeouts[0] - 10*time.Millisecond,
2988 },
2989 },
2990 },
2991 resumeSession: true,
2992 flags: []string{"-async"},
2993 })
David Benjamin7eaab4c2015-03-02 19:01:16 -05002994
2995 // Test that the final Finished retransmitting isn't
2996 // duplicated if the peer badly fragments everything.
2997 testCases = append(testCases, testCase{
2998 testType: serverTest,
2999 protocol: dtls,
3000 name: "DTLS-Retransmit-Fragmented",
3001 config: Config{
3002 Bugs: ProtocolBugs{
3003 TimeoutSchedule: []time.Duration{timeouts[0]},
3004 MaxHandshakeRecordLength: 2,
3005 },
3006 },
3007 flags: []string{"-async"},
3008 })
David Benjamin83f90402015-01-27 01:09:43 -05003009}
3010
David Benjamin884fdf12014-08-02 15:28:23 -04003011func worker(statusChan chan statusMsg, c chan *testCase, buildDir string, wg *sync.WaitGroup) {
Adam Langley95c29f32014-06-20 12:00:00 -07003012 defer wg.Done()
3013
3014 for test := range c {
Adam Langley69a01602014-11-17 17:26:55 -08003015 var err error
3016
3017 if *mallocTest < 0 {
3018 statusChan <- statusMsg{test: test, started: true}
3019 err = runTest(test, buildDir, -1)
3020 } else {
3021 for mallocNumToFail := int64(*mallocTest); ; mallocNumToFail++ {
3022 statusChan <- statusMsg{test: test, started: true}
3023 if err = runTest(test, buildDir, mallocNumToFail); err != errMoreMallocs {
3024 if err != nil {
3025 fmt.Printf("\n\nmalloc test failed at %d: %s\n", mallocNumToFail, err)
3026 }
3027 break
3028 }
3029 }
3030 }
Adam Langley95c29f32014-06-20 12:00:00 -07003031 statusChan <- statusMsg{test: test, err: err}
3032 }
3033}
3034
3035type statusMsg struct {
3036 test *testCase
3037 started bool
3038 err error
3039}
3040
David Benjamin5f237bc2015-02-11 17:14:15 -05003041func statusPrinter(doneChan chan *testOutput, statusChan chan statusMsg, total int) {
Adam Langley95c29f32014-06-20 12:00:00 -07003042 var started, done, failed, lineLen int
Adam Langley95c29f32014-06-20 12:00:00 -07003043
David Benjamin5f237bc2015-02-11 17:14:15 -05003044 testOutput := newTestOutput()
Adam Langley95c29f32014-06-20 12:00:00 -07003045 for msg := range statusChan {
David Benjamin5f237bc2015-02-11 17:14:15 -05003046 if !*pipe {
3047 // Erase the previous status line.
David Benjamin87c8a642015-02-21 01:54:29 -05003048 var erase string
3049 for i := 0; i < lineLen; i++ {
3050 erase += "\b \b"
3051 }
3052 fmt.Print(erase)
David Benjamin5f237bc2015-02-11 17:14:15 -05003053 }
3054
Adam Langley95c29f32014-06-20 12:00:00 -07003055 if msg.started {
3056 started++
3057 } else {
3058 done++
David Benjamin5f237bc2015-02-11 17:14:15 -05003059
3060 if msg.err != nil {
3061 fmt.Printf("FAILED (%s)\n%s\n", msg.test.name, msg.err)
3062 failed++
3063 testOutput.addResult(msg.test.name, "FAIL")
3064 } else {
3065 if *pipe {
3066 // Print each test instead of a status line.
3067 fmt.Printf("PASSED (%s)\n", msg.test.name)
3068 }
3069 testOutput.addResult(msg.test.name, "PASS")
3070 }
Adam Langley95c29f32014-06-20 12:00:00 -07003071 }
3072
David Benjamin5f237bc2015-02-11 17:14:15 -05003073 if !*pipe {
3074 // Print a new status line.
3075 line := fmt.Sprintf("%d/%d/%d/%d", failed, done, started, total)
3076 lineLen = len(line)
3077 os.Stdout.WriteString(line)
Adam Langley95c29f32014-06-20 12:00:00 -07003078 }
Adam Langley95c29f32014-06-20 12:00:00 -07003079 }
David Benjamin5f237bc2015-02-11 17:14:15 -05003080
3081 doneChan <- testOutput
Adam Langley95c29f32014-06-20 12:00:00 -07003082}
3083
3084func main() {
3085 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 -04003086 var flagNumWorkers *int = flag.Int("num-workers", runtime.NumCPU(), "The number of workers to run in parallel.")
David Benjamin884fdf12014-08-02 15:28:23 -04003087 var flagBuildDir *string = flag.String("build-dir", "../../../build", "The build directory to run the shim from.")
Adam Langley95c29f32014-06-20 12:00:00 -07003088
3089 flag.Parse()
3090
3091 addCipherSuiteTests()
3092 addBadECDSASignatureTests()
Adam Langley80842bd2014-06-20 12:00:00 -07003093 addCBCPaddingTests()
Kenny Root7fdeaf12014-08-05 15:23:37 -07003094 addCBCSplittingTests()
David Benjamin636293b2014-07-08 17:59:18 -04003095 addClientAuthTests()
Adam Langley524e7172015-02-20 16:04:00 -08003096 addDDoSCallbackTests()
David Benjamin7e2e6cf2014-08-07 17:44:24 -04003097 addVersionNegotiationTests()
David Benjaminaccb4542014-12-12 23:44:33 -05003098 addMinimumVersionTests()
David Benjamin5c24a1d2014-08-31 00:59:27 -04003099 addD5BugTests()
David Benjamine78bfde2014-09-06 12:45:15 -04003100 addExtensionTests()
David Benjamin01fe8202014-09-24 15:21:44 -04003101 addResumptionVersionTests()
Adam Langley75712922014-10-10 16:23:43 -07003102 addExtendedMasterSecretTests()
Adam Langley2ae77d22014-10-28 17:29:33 -07003103 addRenegotiationTests()
David Benjamin5e961c12014-11-07 01:48:35 -05003104 addDTLSReplayTests()
David Benjamin000800a2014-11-14 01:43:59 -05003105 addSigningHashTests()
Feng Lu41aa3252014-11-21 22:47:56 -08003106 addFastRadioPaddingTests()
David Benjamin83f90402015-01-27 01:09:43 -05003107 addDTLSRetransmitTests()
David Benjamin43ec06f2014-08-05 02:28:57 -04003108 for _, async := range []bool{false, true} {
3109 for _, splitHandshake := range []bool{false, true} {
David Benjamin6fd297b2014-08-11 18:43:38 -04003110 for _, protocol := range []protocol{tls, dtls} {
3111 addStateMachineCoverageTests(async, splitHandshake, protocol)
3112 }
David Benjamin43ec06f2014-08-05 02:28:57 -04003113 }
3114 }
Adam Langley95c29f32014-06-20 12:00:00 -07003115
3116 var wg sync.WaitGroup
3117
David Benjamin2bc8e6f2014-08-02 15:22:37 -04003118 numWorkers := *flagNumWorkers
Adam Langley95c29f32014-06-20 12:00:00 -07003119
3120 statusChan := make(chan statusMsg, numWorkers)
3121 testChan := make(chan *testCase, numWorkers)
David Benjamin5f237bc2015-02-11 17:14:15 -05003122 doneChan := make(chan *testOutput)
Adam Langley95c29f32014-06-20 12:00:00 -07003123
David Benjamin025b3d32014-07-01 19:53:04 -04003124 go statusPrinter(doneChan, statusChan, len(testCases))
Adam Langley95c29f32014-06-20 12:00:00 -07003125
3126 for i := 0; i < numWorkers; i++ {
3127 wg.Add(1)
David Benjamin884fdf12014-08-02 15:28:23 -04003128 go worker(statusChan, testChan, *flagBuildDir, &wg)
Adam Langley95c29f32014-06-20 12:00:00 -07003129 }
3130
David Benjamin025b3d32014-07-01 19:53:04 -04003131 for i := range testCases {
3132 if len(*flagTest) == 0 || *flagTest == testCases[i].name {
3133 testChan <- &testCases[i]
Adam Langley95c29f32014-06-20 12:00:00 -07003134 }
3135 }
3136
3137 close(testChan)
3138 wg.Wait()
3139 close(statusChan)
David Benjamin5f237bc2015-02-11 17:14:15 -05003140 testOutput := <-doneChan
Adam Langley95c29f32014-06-20 12:00:00 -07003141
3142 fmt.Printf("\n")
David Benjamin5f237bc2015-02-11 17:14:15 -05003143
3144 if *jsonOutput != "" {
3145 if err := testOutput.writeTo(*jsonOutput); err != nil {
3146 fmt.Fprintf(os.Stderr, "Error: %s\n", err)
3147 }
3148 }
Adam Langley95c29f32014-06-20 12:00:00 -07003149}