blob: dc27513c679e8c1f5cd37ac66b6e7ac4c1e55063 [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 {
694 protocol: dtls,
695 name: "ReorderHandshakeFragments-Small-DTLS",
696 config: Config{
697 Bugs: ProtocolBugs{
698 ReorderHandshakeFragments: true,
699 // Small enough that every handshake message is
700 // fragmented.
701 MaxHandshakeRecordLength: 2,
702 },
703 },
704 },
705 {
706 protocol: dtls,
707 name: "ReorderHandshakeFragments-Large-DTLS",
708 config: Config{
709 Bugs: ProtocolBugs{
710 ReorderHandshakeFragments: true,
711 // Large enough that no handshake message is
712 // fragmented.
David Benjaminb3774b92015-01-31 17:16:01 -0500713 MaxHandshakeRecordLength: 2048,
714 },
715 },
716 },
David Benjaminddb9f152015-02-03 15:44:39 -0500717 {
David Benjamin75381222015-03-02 19:30:30 -0500718 protocol: dtls,
719 name: "MixCompleteMessageWithFragments-DTLS",
720 config: Config{
721 Bugs: ProtocolBugs{
722 ReorderHandshakeFragments: true,
723 MixCompleteMessageWithFragments: true,
724 MaxHandshakeRecordLength: 2,
725 },
726 },
727 },
728 {
David Benjaminddb9f152015-02-03 15:44:39 -0500729 name: "SendInvalidRecordType",
730 config: Config{
731 Bugs: ProtocolBugs{
732 SendInvalidRecordType: true,
733 },
734 },
735 shouldFail: true,
736 expectedError: ":UNEXPECTED_RECORD:",
737 },
738 {
739 protocol: dtls,
740 name: "SendInvalidRecordType-DTLS",
741 config: Config{
742 Bugs: ProtocolBugs{
743 SendInvalidRecordType: true,
744 },
745 },
746 shouldFail: true,
747 expectedError: ":UNEXPECTED_RECORD:",
748 },
David Benjaminb80168e2015-02-08 18:30:14 -0500749 {
750 name: "FalseStart-SkipServerSecondLeg",
751 config: Config{
752 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
753 NextProtos: []string{"foo"},
754 Bugs: ProtocolBugs{
755 SkipNewSessionTicket: true,
756 SkipChangeCipherSpec: true,
757 SkipFinished: true,
758 ExpectFalseStart: true,
759 },
760 },
761 flags: []string{
762 "-false-start",
763 "-advertise-alpn", "\x03foo",
764 },
765 shimWritesFirst: true,
766 shouldFail: true,
767 expectedError: ":UNEXPECTED_RECORD:",
768 },
David Benjamin931ab342015-02-08 19:46:57 -0500769 {
770 name: "FalseStart-SkipServerSecondLeg-Implicit",
771 config: Config{
772 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
773 NextProtos: []string{"foo"},
774 Bugs: ProtocolBugs{
775 SkipNewSessionTicket: true,
776 SkipChangeCipherSpec: true,
777 SkipFinished: true,
778 },
779 },
780 flags: []string{
781 "-implicit-handshake",
782 "-false-start",
783 "-advertise-alpn", "\x03foo",
784 },
785 shouldFail: true,
786 expectedError: ":UNEXPECTED_RECORD:",
787 },
David Benjamin6f5c0f42015-02-24 01:23:21 -0500788 {
789 testType: serverTest,
790 name: "FailEarlyCallback",
791 flags: []string{"-fail-early-callback"},
792 shouldFail: true,
793 expectedError: ":CONNECTION_REJECTED:",
794 expectedLocalError: "remote error: access denied",
795 },
David Benjaminbcb2d912015-02-24 23:45:43 -0500796 {
797 name: "WrongMessageType",
798 config: Config{
799 Bugs: ProtocolBugs{
800 WrongCertificateMessageType: true,
801 },
802 },
803 shouldFail: true,
804 expectedError: ":UNEXPECTED_MESSAGE:",
805 expectedLocalError: "remote error: unexpected message",
806 },
807 {
808 protocol: dtls,
809 name: "WrongMessageType-DTLS",
810 config: Config{
811 Bugs: ProtocolBugs{
812 WrongCertificateMessageType: true,
813 },
814 },
815 shouldFail: true,
816 expectedError: ":UNEXPECTED_MESSAGE:",
817 expectedLocalError: "remote error: unexpected message",
818 },
David Benjamin75381222015-03-02 19:30:30 -0500819 {
820 protocol: dtls,
821 name: "FragmentMessageTypeMismatch-DTLS",
822 config: Config{
823 Bugs: ProtocolBugs{
824 MaxHandshakeRecordLength: 2,
825 FragmentMessageTypeMismatch: true,
826 },
827 },
828 shouldFail: true,
829 expectedError: ":FRAGMENT_MISMATCH:",
830 },
831 {
832 protocol: dtls,
833 name: "FragmentMessageLengthMismatch-DTLS",
834 config: Config{
835 Bugs: ProtocolBugs{
836 MaxHandshakeRecordLength: 2,
837 FragmentMessageLengthMismatch: true,
838 },
839 },
840 shouldFail: true,
841 expectedError: ":FRAGMENT_MISMATCH:",
842 },
843 {
844 protocol: dtls,
845 name: "SplitFragmentHeader-DTLS",
846 config: Config{
847 Bugs: ProtocolBugs{
848 SplitFragmentHeader: true,
849 },
850 },
851 shouldFail: true,
852 expectedError: ":UNEXPECTED_MESSAGE:",
853 },
854 {
855 protocol: dtls,
856 name: "SplitFragmentBody-DTLS",
857 config: Config{
858 Bugs: ProtocolBugs{
859 SplitFragmentBody: true,
860 },
861 },
862 shouldFail: true,
863 expectedError: ":UNEXPECTED_MESSAGE:",
864 },
865 {
866 protocol: dtls,
867 name: "SendEmptyFragments-DTLS",
868 config: Config{
869 Bugs: ProtocolBugs{
870 SendEmptyFragments: true,
871 },
872 },
873 },
Adam Langley95c29f32014-06-20 12:00:00 -0700874}
875
David Benjamin01fe8202014-09-24 15:21:44 -0400876func doExchange(test *testCase, config *Config, conn net.Conn, messageLen int, isResume bool) error {
David Benjamin65ea8ff2014-11-23 03:01:00 -0500877 var connDebug *recordingConn
David Benjamin5fa3eba2015-01-22 16:35:40 -0500878 var connDamage *damageAdaptor
David Benjamin65ea8ff2014-11-23 03:01:00 -0500879 if *flagDebug {
880 connDebug = &recordingConn{Conn: conn}
881 conn = connDebug
882 defer func() {
883 connDebug.WriteTo(os.Stdout)
884 }()
885 }
886
David Benjamin6fd297b2014-08-11 18:43:38 -0400887 if test.protocol == dtls {
David Benjamin83f90402015-01-27 01:09:43 -0500888 config.Bugs.PacketAdaptor = newPacketAdaptor(conn)
889 conn = config.Bugs.PacketAdaptor
David Benjamin5e961c12014-11-07 01:48:35 -0500890 if test.replayWrites {
891 conn = newReplayAdaptor(conn)
892 }
David Benjamin6fd297b2014-08-11 18:43:38 -0400893 }
894
David Benjamin5fa3eba2015-01-22 16:35:40 -0500895 if test.damageFirstWrite {
896 connDamage = newDamageAdaptor(conn)
897 conn = connDamage
898 }
899
David Benjamin6fd297b2014-08-11 18:43:38 -0400900 if test.sendPrefix != "" {
901 if _, err := conn.Write([]byte(test.sendPrefix)); err != nil {
902 return err
903 }
David Benjamin98e882e2014-08-08 13:24:34 -0400904 }
905
David Benjamin1d5c83e2014-07-22 19:20:02 -0400906 var tlsConn *Conn
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400907 if test.testType == clientTest {
David Benjamin6fd297b2014-08-11 18:43:38 -0400908 if test.protocol == dtls {
909 tlsConn = DTLSServer(conn, config)
910 } else {
911 tlsConn = Server(conn, config)
912 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400913 } else {
914 config.InsecureSkipVerify = true
David Benjamin6fd297b2014-08-11 18:43:38 -0400915 if test.protocol == dtls {
916 tlsConn = DTLSClient(conn, config)
917 } else {
918 tlsConn = Client(conn, config)
919 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400920 }
921
Adam Langley95c29f32014-06-20 12:00:00 -0700922 if err := tlsConn.Handshake(); err != nil {
923 return err
924 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700925
David Benjamin01fe8202014-09-24 15:21:44 -0400926 // TODO(davidben): move all per-connection expectations into a dedicated
927 // expectations struct that can be specified separately for the two
928 // legs.
929 expectedVersion := test.expectedVersion
930 if isResume && test.expectedResumeVersion != 0 {
931 expectedVersion = test.expectedResumeVersion
932 }
933 if vers := tlsConn.ConnectionState().Version; expectedVersion != 0 && vers != expectedVersion {
934 return fmt.Errorf("got version %x, expected %x", vers, expectedVersion)
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400935 }
936
David Benjamina08e49d2014-08-24 01:46:07 -0400937 if test.expectChannelID {
938 channelID := tlsConn.ConnectionState().ChannelID
939 if channelID == nil {
940 return fmt.Errorf("no channel ID negotiated")
941 }
942 if channelID.Curve != channelIDKey.Curve ||
943 channelIDKey.X.Cmp(channelIDKey.X) != 0 ||
944 channelIDKey.Y.Cmp(channelIDKey.Y) != 0 {
945 return fmt.Errorf("incorrect channel ID")
946 }
947 }
948
David Benjaminae2888f2014-09-06 12:58:58 -0400949 if expected := test.expectedNextProto; expected != "" {
950 if actual := tlsConn.ConnectionState().NegotiatedProtocol; actual != expected {
951 return fmt.Errorf("next proto mismatch: got %s, wanted %s", actual, expected)
952 }
953 }
954
David Benjaminfc7b0862014-09-06 13:21:53 -0400955 if test.expectedNextProtoType != 0 {
956 if (test.expectedNextProtoType == alpn) != tlsConn.ConnectionState().NegotiatedProtocolFromALPN {
957 return fmt.Errorf("next proto type mismatch")
958 }
959 }
960
David Benjaminca6c8262014-11-15 19:06:08 -0500961 if p := tlsConn.ConnectionState().SRTPProtectionProfile; p != test.expectedSRTPProtectionProfile {
962 return fmt.Errorf("SRTP profile mismatch: got %d, wanted %d", p, test.expectedSRTPProtectionProfile)
963 }
964
David Benjamine58c4f52014-08-24 03:47:07 -0400965 if test.shimWritesFirst {
966 var buf [5]byte
967 _, err := io.ReadFull(tlsConn, buf[:])
968 if err != nil {
969 return err
970 }
971 if string(buf[:]) != "hello" {
972 return fmt.Errorf("bad initial message")
973 }
974 }
975
Adam Langleycf2d4f42014-10-28 19:06:14 -0700976 if test.renegotiate {
977 if test.renegotiateCiphers != nil {
978 config.CipherSuites = test.renegotiateCiphers
979 }
980 if err := tlsConn.Renegotiate(); err != nil {
981 return err
982 }
983 } else if test.renegotiateCiphers != nil {
984 panic("renegotiateCiphers without renegotiate")
985 }
986
David Benjamin5fa3eba2015-01-22 16:35:40 -0500987 if test.damageFirstWrite {
988 connDamage.setDamage(true)
989 tlsConn.Write([]byte("DAMAGED WRITE"))
990 connDamage.setDamage(false)
991 }
992
Kenny Root7fdeaf12014-08-05 15:23:37 -0700993 if messageLen < 0 {
David Benjamin6fd297b2014-08-11 18:43:38 -0400994 if test.protocol == dtls {
995 return fmt.Errorf("messageLen < 0 not supported for DTLS tests")
996 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700997 // Read until EOF.
998 _, err := io.Copy(ioutil.Discard, tlsConn)
999 return err
1000 }
1001
David Benjamin4189bd92015-01-25 23:52:39 -05001002 var testMessage []byte
1003 if config.Bugs.AppDataAfterChangeCipherSpec != nil {
1004 // We've already sent a message. Expect the shim to echo it
1005 // back.
1006 testMessage = config.Bugs.AppDataAfterChangeCipherSpec
1007 } else {
1008 if messageLen == 0 {
1009 messageLen = 32
1010 }
1011 testMessage = make([]byte, messageLen)
1012 for i := range testMessage {
1013 testMessage[i] = 0x42
1014 }
1015 tlsConn.Write(testMessage)
Adam Langley80842bd2014-06-20 12:00:00 -07001016 }
Adam Langley95c29f32014-06-20 12:00:00 -07001017
1018 buf := make([]byte, len(testMessage))
David Benjamin6fd297b2014-08-11 18:43:38 -04001019 if test.protocol == dtls {
1020 bufTmp := make([]byte, len(buf)+1)
1021 n, err := tlsConn.Read(bufTmp)
1022 if err != nil {
1023 return err
1024 }
1025 if n != len(buf) {
1026 return fmt.Errorf("bad reply; length mismatch (%d vs %d)", n, len(buf))
1027 }
1028 copy(buf, bufTmp)
1029 } else {
1030 _, err := io.ReadFull(tlsConn, buf)
1031 if err != nil {
1032 return err
1033 }
Adam Langley95c29f32014-06-20 12:00:00 -07001034 }
1035
1036 for i, v := range buf {
1037 if v != testMessage[i]^0xff {
1038 return fmt.Errorf("bad reply contents at byte %d", i)
1039 }
1040 }
1041
1042 return nil
1043}
1044
David Benjamin325b5c32014-07-01 19:40:31 -04001045func valgrindOf(dbAttach bool, path string, args ...string) *exec.Cmd {
1046 valgrindArgs := []string{"--error-exitcode=99", "--track-origins=yes", "--leak-check=full"}
Adam Langley95c29f32014-06-20 12:00:00 -07001047 if dbAttach {
David Benjamin325b5c32014-07-01 19:40:31 -04001048 valgrindArgs = append(valgrindArgs, "--db-attach=yes", "--db-command=xterm -e gdb -nw %f %p")
Adam Langley95c29f32014-06-20 12:00:00 -07001049 }
David Benjamin325b5c32014-07-01 19:40:31 -04001050 valgrindArgs = append(valgrindArgs, path)
1051 valgrindArgs = append(valgrindArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -07001052
David Benjamin325b5c32014-07-01 19:40:31 -04001053 return exec.Command("valgrind", valgrindArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -07001054}
1055
David Benjamin325b5c32014-07-01 19:40:31 -04001056func gdbOf(path string, args ...string) *exec.Cmd {
1057 xtermArgs := []string{"-e", "gdb", "--args"}
1058 xtermArgs = append(xtermArgs, path)
1059 xtermArgs = append(xtermArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -07001060
David Benjamin325b5c32014-07-01 19:40:31 -04001061 return exec.Command("xterm", xtermArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -07001062}
1063
Adam Langley69a01602014-11-17 17:26:55 -08001064type moreMallocsError struct{}
1065
1066func (moreMallocsError) Error() string {
1067 return "child process did not exhaust all allocation calls"
1068}
1069
1070var errMoreMallocs = moreMallocsError{}
1071
David Benjamin87c8a642015-02-21 01:54:29 -05001072// accept accepts a connection from listener, unless waitChan signals a process
1073// exit first.
1074func acceptOrWait(listener net.Listener, waitChan chan error) (net.Conn, error) {
1075 type connOrError struct {
1076 conn net.Conn
1077 err error
1078 }
1079 connChan := make(chan connOrError, 1)
1080 go func() {
1081 conn, err := listener.Accept()
1082 connChan <- connOrError{conn, err}
1083 close(connChan)
1084 }()
1085 select {
1086 case result := <-connChan:
1087 return result.conn, result.err
1088 case childErr := <-waitChan:
1089 waitChan <- childErr
1090 return nil, fmt.Errorf("child exited early: %s", childErr)
1091 }
1092}
1093
Adam Langley69a01602014-11-17 17:26:55 -08001094func runTest(test *testCase, buildDir string, mallocNumToFail int64) error {
Adam Langley38311732014-10-16 19:04:35 -07001095 if !test.shouldFail && (len(test.expectedError) > 0 || len(test.expectedLocalError) > 0) {
1096 panic("Error expected without shouldFail in " + test.name)
1097 }
1098
David Benjamin87c8a642015-02-21 01:54:29 -05001099 listener, err := net.ListenTCP("tcp4", &net.TCPAddr{IP: net.IP{127, 0, 0, 1}})
1100 if err != nil {
1101 panic(err)
1102 }
1103 defer func() {
1104 if listener != nil {
1105 listener.Close()
1106 }
1107 }()
Adam Langley95c29f32014-06-20 12:00:00 -07001108
David Benjamin884fdf12014-08-02 15:28:23 -04001109 shim_path := path.Join(buildDir, "ssl/test/bssl_shim")
David Benjamin87c8a642015-02-21 01:54:29 -05001110 flags := []string{"-port", strconv.Itoa(listener.Addr().(*net.TCPAddr).Port)}
David Benjamin1d5c83e2014-07-22 19:20:02 -04001111 if test.testType == serverTest {
David Benjamin5a593af2014-08-11 19:51:50 -04001112 flags = append(flags, "-server")
1113
David Benjamin025b3d32014-07-01 19:53:04 -04001114 flags = append(flags, "-key-file")
1115 if test.keyFile == "" {
1116 flags = append(flags, rsaKeyFile)
1117 } else {
1118 flags = append(flags, test.keyFile)
1119 }
1120
1121 flags = append(flags, "-cert-file")
1122 if test.certFile == "" {
1123 flags = append(flags, rsaCertificateFile)
1124 } else {
1125 flags = append(flags, test.certFile)
1126 }
1127 }
David Benjamin5a593af2014-08-11 19:51:50 -04001128
David Benjamin6fd297b2014-08-11 18:43:38 -04001129 if test.protocol == dtls {
1130 flags = append(flags, "-dtls")
1131 }
1132
David Benjamin5a593af2014-08-11 19:51:50 -04001133 if test.resumeSession {
1134 flags = append(flags, "-resume")
1135 }
1136
David Benjamine58c4f52014-08-24 03:47:07 -04001137 if test.shimWritesFirst {
1138 flags = append(flags, "-shim-writes-first")
1139 }
1140
David Benjamin025b3d32014-07-01 19:53:04 -04001141 flags = append(flags, test.flags...)
1142
1143 var shim *exec.Cmd
1144 if *useValgrind {
1145 shim = valgrindOf(false, shim_path, flags...)
Adam Langley75712922014-10-10 16:23:43 -07001146 } else if *useGDB {
1147 shim = gdbOf(shim_path, flags...)
David Benjamin025b3d32014-07-01 19:53:04 -04001148 } else {
1149 shim = exec.Command(shim_path, flags...)
1150 }
David Benjamin025b3d32014-07-01 19:53:04 -04001151 shim.Stdin = os.Stdin
1152 var stdoutBuf, stderrBuf bytes.Buffer
1153 shim.Stdout = &stdoutBuf
1154 shim.Stderr = &stderrBuf
Adam Langley69a01602014-11-17 17:26:55 -08001155 if mallocNumToFail >= 0 {
David Benjamin9e128b02015-02-09 13:13:09 -05001156 shim.Env = os.Environ()
1157 shim.Env = append(shim.Env, "MALLOC_NUMBER_TO_FAIL="+strconv.FormatInt(mallocNumToFail, 10))
Adam Langley69a01602014-11-17 17:26:55 -08001158 if *mallocTestDebug {
1159 shim.Env = append(shim.Env, "MALLOC_ABORT_ON_FAIL=1")
1160 }
1161 shim.Env = append(shim.Env, "_MALLOC_CHECK=1")
1162 }
David Benjamin025b3d32014-07-01 19:53:04 -04001163
1164 if err := shim.Start(); err != nil {
Adam Langley95c29f32014-06-20 12:00:00 -07001165 panic(err)
1166 }
David Benjamin87c8a642015-02-21 01:54:29 -05001167 waitChan := make(chan error, 1)
1168 go func() { waitChan <- shim.Wait() }()
Adam Langley95c29f32014-06-20 12:00:00 -07001169
1170 config := test.config
David Benjamin1d5c83e2014-07-22 19:20:02 -04001171 config.ClientSessionCache = NewLRUClientSessionCache(1)
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001172 config.ServerSessionCache = NewLRUServerSessionCache(1)
David Benjamin025b3d32014-07-01 19:53:04 -04001173 if test.testType == clientTest {
1174 if len(config.Certificates) == 0 {
1175 config.Certificates = []Certificate{getRSACertificate()}
1176 }
David Benjamin87c8a642015-02-21 01:54:29 -05001177 } else {
1178 // Supply a ServerName to ensure a constant session cache key,
1179 // rather than falling back to net.Conn.RemoteAddr.
1180 if len(config.ServerName) == 0 {
1181 config.ServerName = "test"
1182 }
David Benjamin025b3d32014-07-01 19:53:04 -04001183 }
Adam Langley95c29f32014-06-20 12:00:00 -07001184
David Benjamin87c8a642015-02-21 01:54:29 -05001185 conn, err := acceptOrWait(listener, waitChan)
1186 if err == nil {
1187 err = doExchange(test, &config, conn, test.messageLen, false /* not a resumption */)
1188 conn.Close()
1189 }
David Benjamin65ea8ff2014-11-23 03:01:00 -05001190
David Benjamin1d5c83e2014-07-22 19:20:02 -04001191 if err == nil && test.resumeSession {
David Benjamin01fe8202014-09-24 15:21:44 -04001192 var resumeConfig Config
1193 if test.resumeConfig != nil {
1194 resumeConfig = *test.resumeConfig
David Benjamin87c8a642015-02-21 01:54:29 -05001195 if len(resumeConfig.ServerName) == 0 {
1196 resumeConfig.ServerName = config.ServerName
1197 }
David Benjamin01fe8202014-09-24 15:21:44 -04001198 if len(resumeConfig.Certificates) == 0 {
1199 resumeConfig.Certificates = []Certificate{getRSACertificate()}
1200 }
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001201 if !test.newSessionsOnResume {
1202 resumeConfig.SessionTicketKey = config.SessionTicketKey
1203 resumeConfig.ClientSessionCache = config.ClientSessionCache
1204 resumeConfig.ServerSessionCache = config.ServerSessionCache
1205 }
David Benjamin01fe8202014-09-24 15:21:44 -04001206 } else {
1207 resumeConfig = config
1208 }
David Benjamin87c8a642015-02-21 01:54:29 -05001209 var connResume net.Conn
1210 connResume, err = acceptOrWait(listener, waitChan)
1211 if err == nil {
1212 err = doExchange(test, &resumeConfig, connResume, test.messageLen, true /* resumption */)
1213 connResume.Close()
1214 }
David Benjamin1d5c83e2014-07-22 19:20:02 -04001215 }
1216
David Benjamin87c8a642015-02-21 01:54:29 -05001217 // Close the listener now. This is to avoid hangs should the shim try to
1218 // open more connections than expected.
1219 listener.Close()
1220 listener = nil
1221
1222 childErr := <-waitChan
Adam Langley69a01602014-11-17 17:26:55 -08001223 if exitError, ok := childErr.(*exec.ExitError); ok {
1224 if exitError.Sys().(syscall.WaitStatus).ExitStatus() == 88 {
1225 return errMoreMallocs
1226 }
1227 }
Adam Langley95c29f32014-06-20 12:00:00 -07001228
1229 stdout := string(stdoutBuf.Bytes())
1230 stderr := string(stderrBuf.Bytes())
1231 failed := err != nil || childErr != nil
1232 correctFailure := len(test.expectedError) == 0 || strings.Contains(stdout, test.expectedError)
Adam Langleyac61fa32014-06-23 12:03:11 -07001233 localError := "none"
1234 if err != nil {
1235 localError = err.Error()
1236 }
1237 if len(test.expectedLocalError) != 0 {
1238 correctFailure = correctFailure && strings.Contains(localError, test.expectedLocalError)
1239 }
Adam Langley95c29f32014-06-20 12:00:00 -07001240
1241 if failed != test.shouldFail || failed && !correctFailure {
Adam Langley95c29f32014-06-20 12:00:00 -07001242 childError := "none"
Adam Langley95c29f32014-06-20 12:00:00 -07001243 if childErr != nil {
1244 childError = childErr.Error()
1245 }
1246
1247 var msg string
1248 switch {
1249 case failed && !test.shouldFail:
1250 msg = "unexpected failure"
1251 case !failed && test.shouldFail:
1252 msg = "unexpected success"
1253 case failed && !correctFailure:
Adam Langleyac61fa32014-06-23 12:03:11 -07001254 msg = "bad error (wanted '" + test.expectedError + "' / '" + test.expectedLocalError + "')"
Adam Langley95c29f32014-06-20 12:00:00 -07001255 default:
1256 panic("internal error")
1257 }
1258
1259 return fmt.Errorf("%s: local error '%s', child error '%s', stdout:\n%s\nstderr:\n%s", msg, localError, childError, string(stdoutBuf.Bytes()), stderr)
1260 }
1261
1262 if !*useValgrind && len(stderr) > 0 {
1263 println(stderr)
1264 }
1265
1266 return nil
1267}
1268
1269var tlsVersions = []struct {
1270 name string
1271 version uint16
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001272 flag string
David Benjamin8b8c0062014-11-23 02:47:52 -05001273 hasDTLS bool
Adam Langley95c29f32014-06-20 12:00:00 -07001274}{
David Benjamin8b8c0062014-11-23 02:47:52 -05001275 {"SSL3", VersionSSL30, "-no-ssl3", false},
1276 {"TLS1", VersionTLS10, "-no-tls1", true},
1277 {"TLS11", VersionTLS11, "-no-tls11", false},
1278 {"TLS12", VersionTLS12, "-no-tls12", true},
Adam Langley95c29f32014-06-20 12:00:00 -07001279}
1280
1281var testCipherSuites = []struct {
1282 name string
1283 id uint16
1284}{
1285 {"3DES-SHA", TLS_RSA_WITH_3DES_EDE_CBC_SHA},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001286 {"AES128-GCM", TLS_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001287 {"AES128-SHA", TLS_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001288 {"AES128-SHA256", TLS_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001289 {"AES256-GCM", TLS_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001290 {"AES256-SHA", TLS_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001291 {"AES256-SHA256", TLS_RSA_WITH_AES_256_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001292 {"DHE-RSA-AES128-GCM", TLS_DHE_RSA_WITH_AES_128_GCM_SHA256},
1293 {"DHE-RSA-AES128-SHA", TLS_DHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001294 {"DHE-RSA-AES128-SHA256", TLS_DHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001295 {"DHE-RSA-AES256-GCM", TLS_DHE_RSA_WITH_AES_256_GCM_SHA384},
1296 {"DHE-RSA-AES256-SHA", TLS_DHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001297 {"DHE-RSA-AES256-SHA256", TLS_DHE_RSA_WITH_AES_256_CBC_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001298 {"ECDHE-ECDSA-AES128-GCM", TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
1299 {"ECDHE-ECDSA-AES128-SHA", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001300 {"ECDHE-ECDSA-AES128-SHA256", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256},
1301 {"ECDHE-ECDSA-AES256-GCM", TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001302 {"ECDHE-ECDSA-AES256-SHA", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001303 {"ECDHE-ECDSA-AES256-SHA384", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001304 {"ECDHE-ECDSA-RC4-SHA", TLS_ECDHE_ECDSA_WITH_RC4_128_SHA},
David Benjamin2af684f2014-10-27 02:23:15 -04001305 {"ECDHE-PSK-WITH-AES-128-GCM-SHA256", TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001306 {"ECDHE-RSA-AES128-GCM", TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001307 {"ECDHE-RSA-AES128-SHA", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001308 {"ECDHE-RSA-AES128-SHA256", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001309 {"ECDHE-RSA-AES256-GCM", TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001310 {"ECDHE-RSA-AES256-SHA", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001311 {"ECDHE-RSA-AES256-SHA384", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001312 {"ECDHE-RSA-RC4-SHA", TLS_ECDHE_RSA_WITH_RC4_128_SHA},
David Benjamin48cae082014-10-27 01:06:24 -04001313 {"PSK-AES128-CBC-SHA", TLS_PSK_WITH_AES_128_CBC_SHA},
1314 {"PSK-AES256-CBC-SHA", TLS_PSK_WITH_AES_256_CBC_SHA},
1315 {"PSK-RC4-SHA", TLS_PSK_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -07001316 {"RC4-MD5", TLS_RSA_WITH_RC4_128_MD5},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001317 {"RC4-SHA", TLS_RSA_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -07001318}
1319
David Benjamin8b8c0062014-11-23 02:47:52 -05001320func hasComponent(suiteName, component string) bool {
1321 return strings.Contains("-"+suiteName+"-", "-"+component+"-")
1322}
1323
David Benjaminf7768e42014-08-31 02:06:47 -04001324func isTLS12Only(suiteName string) bool {
David Benjamin8b8c0062014-11-23 02:47:52 -05001325 return hasComponent(suiteName, "GCM") ||
1326 hasComponent(suiteName, "SHA256") ||
1327 hasComponent(suiteName, "SHA384")
1328}
1329
1330func isDTLSCipher(suiteName string) bool {
David Benjamine95d20d2014-12-23 11:16:01 -05001331 return !hasComponent(suiteName, "RC4")
David Benjaminf7768e42014-08-31 02:06:47 -04001332}
1333
Adam Langley95c29f32014-06-20 12:00:00 -07001334func addCipherSuiteTests() {
1335 for _, suite := range testCipherSuites {
David Benjamin48cae082014-10-27 01:06:24 -04001336 const psk = "12345"
1337 const pskIdentity = "luggage combo"
1338
Adam Langley95c29f32014-06-20 12:00:00 -07001339 var cert Certificate
David Benjamin025b3d32014-07-01 19:53:04 -04001340 var certFile string
1341 var keyFile string
David Benjamin8b8c0062014-11-23 02:47:52 -05001342 if hasComponent(suite.name, "ECDSA") {
Adam Langley95c29f32014-06-20 12:00:00 -07001343 cert = getECDSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -04001344 certFile = ecdsaCertificateFile
1345 keyFile = ecdsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -07001346 } else {
1347 cert = getRSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -04001348 certFile = rsaCertificateFile
1349 keyFile = rsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -07001350 }
1351
David Benjamin48cae082014-10-27 01:06:24 -04001352 var flags []string
David Benjamin8b8c0062014-11-23 02:47:52 -05001353 if hasComponent(suite.name, "PSK") {
David Benjamin48cae082014-10-27 01:06:24 -04001354 flags = append(flags,
1355 "-psk", psk,
1356 "-psk-identity", pskIdentity)
1357 }
1358
Adam Langley95c29f32014-06-20 12:00:00 -07001359 for _, ver := range tlsVersions {
David Benjaminf7768e42014-08-31 02:06:47 -04001360 if ver.version < VersionTLS12 && isTLS12Only(suite.name) {
Adam Langley95c29f32014-06-20 12:00:00 -07001361 continue
1362 }
1363
David Benjamin025b3d32014-07-01 19:53:04 -04001364 testCases = append(testCases, testCase{
1365 testType: clientTest,
1366 name: ver.name + "-" + suite.name + "-client",
Adam Langley95c29f32014-06-20 12:00:00 -07001367 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001368 MinVersion: ver.version,
1369 MaxVersion: ver.version,
1370 CipherSuites: []uint16{suite.id},
1371 Certificates: []Certificate{cert},
1372 PreSharedKey: []byte(psk),
1373 PreSharedKeyIdentity: pskIdentity,
Adam Langley95c29f32014-06-20 12:00:00 -07001374 },
David Benjamin48cae082014-10-27 01:06:24 -04001375 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001376 resumeSession: true,
Adam Langley95c29f32014-06-20 12:00:00 -07001377 })
David Benjamin025b3d32014-07-01 19:53:04 -04001378
David Benjamin76d8abe2014-08-14 16:25:34 -04001379 testCases = append(testCases, testCase{
1380 testType: serverTest,
1381 name: ver.name + "-" + suite.name + "-server",
1382 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001383 MinVersion: ver.version,
1384 MaxVersion: ver.version,
1385 CipherSuites: []uint16{suite.id},
1386 Certificates: []Certificate{cert},
1387 PreSharedKey: []byte(psk),
1388 PreSharedKeyIdentity: pskIdentity,
David Benjamin76d8abe2014-08-14 16:25:34 -04001389 },
1390 certFile: certFile,
1391 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001392 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001393 resumeSession: true,
David Benjamin76d8abe2014-08-14 16:25:34 -04001394 })
David Benjamin6fd297b2014-08-11 18:43:38 -04001395
David Benjamin8b8c0062014-11-23 02:47:52 -05001396 if ver.hasDTLS && isDTLSCipher(suite.name) {
David Benjamin6fd297b2014-08-11 18:43:38 -04001397 testCases = append(testCases, testCase{
1398 testType: clientTest,
1399 protocol: dtls,
1400 name: "D" + ver.name + "-" + suite.name + "-client",
1401 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001402 MinVersion: ver.version,
1403 MaxVersion: ver.version,
1404 CipherSuites: []uint16{suite.id},
1405 Certificates: []Certificate{cert},
1406 PreSharedKey: []byte(psk),
1407 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001408 },
David Benjamin48cae082014-10-27 01:06:24 -04001409 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001410 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001411 })
1412 testCases = append(testCases, testCase{
1413 testType: serverTest,
1414 protocol: dtls,
1415 name: "D" + ver.name + "-" + suite.name + "-server",
1416 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001417 MinVersion: ver.version,
1418 MaxVersion: ver.version,
1419 CipherSuites: []uint16{suite.id},
1420 Certificates: []Certificate{cert},
1421 PreSharedKey: []byte(psk),
1422 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001423 },
1424 certFile: certFile,
1425 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001426 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001427 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001428 })
1429 }
Adam Langley95c29f32014-06-20 12:00:00 -07001430 }
1431 }
1432}
1433
1434func addBadECDSASignatureTests() {
1435 for badR := BadValue(1); badR < NumBadValues; badR++ {
1436 for badS := BadValue(1); badS < NumBadValues; badS++ {
David Benjamin025b3d32014-07-01 19:53:04 -04001437 testCases = append(testCases, testCase{
Adam Langley95c29f32014-06-20 12:00:00 -07001438 name: fmt.Sprintf("BadECDSA-%d-%d", badR, badS),
1439 config: Config{
1440 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
1441 Certificates: []Certificate{getECDSACertificate()},
1442 Bugs: ProtocolBugs{
1443 BadECDSAR: badR,
1444 BadECDSAS: badS,
1445 },
1446 },
1447 shouldFail: true,
1448 expectedError: "SIGNATURE",
1449 })
1450 }
1451 }
1452}
1453
Adam Langley80842bd2014-06-20 12:00:00 -07001454func addCBCPaddingTests() {
David Benjamin025b3d32014-07-01 19:53:04 -04001455 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001456 name: "MaxCBCPadding",
1457 config: Config{
1458 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1459 Bugs: ProtocolBugs{
1460 MaxPadding: true,
1461 },
1462 },
1463 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1464 })
David Benjamin025b3d32014-07-01 19:53:04 -04001465 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001466 name: "BadCBCPadding",
1467 config: Config{
1468 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1469 Bugs: ProtocolBugs{
1470 PaddingFirstByteBad: true,
1471 },
1472 },
1473 shouldFail: true,
1474 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1475 })
1476 // OpenSSL previously had an issue where the first byte of padding in
1477 // 255 bytes of padding wasn't checked.
David Benjamin025b3d32014-07-01 19:53:04 -04001478 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001479 name: "BadCBCPadding255",
1480 config: Config{
1481 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1482 Bugs: ProtocolBugs{
1483 MaxPadding: true,
1484 PaddingFirstByteBadIf255: true,
1485 },
1486 },
1487 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1488 shouldFail: true,
1489 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1490 })
1491}
1492
Kenny Root7fdeaf12014-08-05 15:23:37 -07001493func addCBCSplittingTests() {
1494 testCases = append(testCases, testCase{
1495 name: "CBCRecordSplitting",
1496 config: Config{
1497 MaxVersion: VersionTLS10,
1498 MinVersion: VersionTLS10,
1499 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1500 },
1501 messageLen: -1, // read until EOF
1502 flags: []string{
1503 "-async",
1504 "-write-different-record-sizes",
1505 "-cbc-record-splitting",
1506 },
David Benjamina8e3e0e2014-08-06 22:11:10 -04001507 })
1508 testCases = append(testCases, testCase{
Kenny Root7fdeaf12014-08-05 15:23:37 -07001509 name: "CBCRecordSplittingPartialWrite",
1510 config: Config{
1511 MaxVersion: VersionTLS10,
1512 MinVersion: VersionTLS10,
1513 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1514 },
1515 messageLen: -1, // read until EOF
1516 flags: []string{
1517 "-async",
1518 "-write-different-record-sizes",
1519 "-cbc-record-splitting",
1520 "-partial-write",
1521 },
1522 })
1523}
1524
David Benjamin636293b2014-07-08 17:59:18 -04001525func addClientAuthTests() {
David Benjamin407a10c2014-07-16 12:58:59 -04001526 // Add a dummy cert pool to stress certificate authority parsing.
1527 // TODO(davidben): Add tests that those values parse out correctly.
1528 certPool := x509.NewCertPool()
1529 cert, err := x509.ParseCertificate(rsaCertificate.Certificate[0])
1530 if err != nil {
1531 panic(err)
1532 }
1533 certPool.AddCert(cert)
1534
David Benjamin636293b2014-07-08 17:59:18 -04001535 for _, ver := range tlsVersions {
David Benjamin636293b2014-07-08 17:59:18 -04001536 testCases = append(testCases, testCase{
1537 testType: clientTest,
David Benjamin67666e72014-07-12 15:47:52 -04001538 name: ver.name + "-Client-ClientAuth-RSA",
David Benjamin636293b2014-07-08 17:59:18 -04001539 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001540 MinVersion: ver.version,
1541 MaxVersion: ver.version,
1542 ClientAuth: RequireAnyClientCert,
1543 ClientCAs: certPool,
David Benjamin636293b2014-07-08 17:59:18 -04001544 },
1545 flags: []string{
1546 "-cert-file", rsaCertificateFile,
1547 "-key-file", rsaKeyFile,
1548 },
1549 })
1550 testCases = append(testCases, testCase{
David Benjamin67666e72014-07-12 15:47:52 -04001551 testType: serverTest,
1552 name: ver.name + "-Server-ClientAuth-RSA",
1553 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001554 MinVersion: ver.version,
1555 MaxVersion: ver.version,
David Benjamin67666e72014-07-12 15:47:52 -04001556 Certificates: []Certificate{rsaCertificate},
1557 },
1558 flags: []string{"-require-any-client-certificate"},
1559 })
David Benjamine098ec22014-08-27 23:13:20 -04001560 if ver.version != VersionSSL30 {
1561 testCases = append(testCases, testCase{
1562 testType: serverTest,
1563 name: ver.name + "-Server-ClientAuth-ECDSA",
1564 config: Config{
1565 MinVersion: ver.version,
1566 MaxVersion: ver.version,
1567 Certificates: []Certificate{ecdsaCertificate},
1568 },
1569 flags: []string{"-require-any-client-certificate"},
1570 })
1571 testCases = append(testCases, testCase{
1572 testType: clientTest,
1573 name: ver.name + "-Client-ClientAuth-ECDSA",
1574 config: Config{
1575 MinVersion: ver.version,
1576 MaxVersion: ver.version,
1577 ClientAuth: RequireAnyClientCert,
1578 ClientCAs: certPool,
1579 },
1580 flags: []string{
1581 "-cert-file", ecdsaCertificateFile,
1582 "-key-file", ecdsaKeyFile,
1583 },
1584 })
1585 }
David Benjamin636293b2014-07-08 17:59:18 -04001586 }
1587}
1588
Adam Langley75712922014-10-10 16:23:43 -07001589func addExtendedMasterSecretTests() {
1590 const expectEMSFlag = "-expect-extended-master-secret"
1591
1592 for _, with := range []bool{false, true} {
1593 prefix := "No"
1594 var flags []string
1595 if with {
1596 prefix = ""
1597 flags = []string{expectEMSFlag}
1598 }
1599
1600 for _, isClient := range []bool{false, true} {
1601 suffix := "-Server"
1602 testType := serverTest
1603 if isClient {
1604 suffix = "-Client"
1605 testType = clientTest
1606 }
1607
1608 for _, ver := range tlsVersions {
1609 test := testCase{
1610 testType: testType,
1611 name: prefix + "ExtendedMasterSecret-" + ver.name + suffix,
1612 config: Config{
1613 MinVersion: ver.version,
1614 MaxVersion: ver.version,
1615 Bugs: ProtocolBugs{
1616 NoExtendedMasterSecret: !with,
1617 RequireExtendedMasterSecret: with,
1618 },
1619 },
David Benjamin48cae082014-10-27 01:06:24 -04001620 flags: flags,
1621 shouldFail: ver.version == VersionSSL30 && with,
Adam Langley75712922014-10-10 16:23:43 -07001622 }
1623 if test.shouldFail {
1624 test.expectedLocalError = "extended master secret required but not supported by peer"
1625 }
1626 testCases = append(testCases, test)
1627 }
1628 }
1629 }
1630
1631 // When a session is resumed, it should still be aware that its master
1632 // secret was generated via EMS and thus it's safe to use tls-unique.
1633 testCases = append(testCases, testCase{
1634 name: "ExtendedMasterSecret-Resume",
1635 config: Config{
1636 Bugs: ProtocolBugs{
1637 RequireExtendedMasterSecret: true,
1638 },
1639 },
1640 flags: []string{expectEMSFlag},
1641 resumeSession: true,
1642 })
1643}
1644
David Benjamin43ec06f2014-08-05 02:28:57 -04001645// Adds tests that try to cover the range of the handshake state machine, under
1646// various conditions. Some of these are redundant with other tests, but they
1647// only cover the synchronous case.
David Benjamin6fd297b2014-08-11 18:43:38 -04001648func addStateMachineCoverageTests(async, splitHandshake bool, protocol protocol) {
David Benjamin43ec06f2014-08-05 02:28:57 -04001649 var suffix string
1650 var flags []string
1651 var maxHandshakeRecordLength int
David Benjamin6fd297b2014-08-11 18:43:38 -04001652 if protocol == dtls {
1653 suffix = "-DTLS"
1654 }
David Benjamin43ec06f2014-08-05 02:28:57 -04001655 if async {
David Benjamin6fd297b2014-08-11 18:43:38 -04001656 suffix += "-Async"
David Benjamin43ec06f2014-08-05 02:28:57 -04001657 flags = append(flags, "-async")
1658 } else {
David Benjamin6fd297b2014-08-11 18:43:38 -04001659 suffix += "-Sync"
David Benjamin43ec06f2014-08-05 02:28:57 -04001660 }
1661 if splitHandshake {
1662 suffix += "-SplitHandshakeRecords"
David Benjamin98214542014-08-07 18:02:39 -04001663 maxHandshakeRecordLength = 1
David Benjamin43ec06f2014-08-05 02:28:57 -04001664 }
1665
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001666 // Basic handshake, with resumption. Client and server,
1667 // session ID and session ticket.
David Benjamin43ec06f2014-08-05 02:28:57 -04001668 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001669 protocol: protocol,
1670 name: "Basic-Client" + suffix,
David Benjamin43ec06f2014-08-05 02:28:57 -04001671 config: Config{
1672 Bugs: ProtocolBugs{
1673 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1674 },
1675 },
David Benjaminbed9aae2014-08-07 19:13:38 -04001676 flags: flags,
1677 resumeSession: true,
1678 })
1679 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001680 protocol: protocol,
1681 name: "Basic-Client-RenewTicket" + suffix,
David Benjaminbed9aae2014-08-07 19:13:38 -04001682 config: Config{
1683 Bugs: ProtocolBugs{
1684 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1685 RenewTicketOnResume: true,
1686 },
1687 },
1688 flags: flags,
1689 resumeSession: true,
David Benjamin43ec06f2014-08-05 02:28:57 -04001690 })
1691 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001692 protocol: protocol,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001693 name: "Basic-Client-NoTicket" + suffix,
1694 config: Config{
1695 SessionTicketsDisabled: true,
1696 Bugs: ProtocolBugs{
1697 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1698 },
1699 },
1700 flags: flags,
1701 resumeSession: true,
1702 })
1703 testCases = append(testCases, testCase{
1704 protocol: protocol,
David Benjamine0e7d0d2015-02-08 19:33:25 -05001705 name: "Basic-Client-Implicit" + suffix,
1706 config: Config{
1707 Bugs: ProtocolBugs{
1708 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1709 },
1710 },
1711 flags: append(flags, "-implicit-handshake"),
1712 resumeSession: true,
1713 })
1714 testCases = append(testCases, testCase{
1715 protocol: protocol,
David Benjamin43ec06f2014-08-05 02:28:57 -04001716 testType: serverTest,
1717 name: "Basic-Server" + suffix,
1718 config: Config{
1719 Bugs: ProtocolBugs{
1720 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1721 },
1722 },
David Benjaminbed9aae2014-08-07 19:13:38 -04001723 flags: flags,
1724 resumeSession: true,
David Benjamin43ec06f2014-08-05 02:28:57 -04001725 })
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001726 testCases = append(testCases, testCase{
1727 protocol: protocol,
1728 testType: serverTest,
1729 name: "Basic-Server-NoTickets" + suffix,
1730 config: Config{
1731 SessionTicketsDisabled: true,
1732 Bugs: ProtocolBugs{
1733 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1734 },
1735 },
1736 flags: flags,
1737 resumeSession: true,
1738 })
David Benjamine0e7d0d2015-02-08 19:33:25 -05001739 testCases = append(testCases, testCase{
1740 protocol: protocol,
1741 testType: serverTest,
1742 name: "Basic-Server-Implicit" + suffix,
1743 config: Config{
1744 Bugs: ProtocolBugs{
1745 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1746 },
1747 },
1748 flags: append(flags, "-implicit-handshake"),
1749 resumeSession: true,
1750 })
David Benjamin6f5c0f42015-02-24 01:23:21 -05001751 testCases = append(testCases, testCase{
1752 protocol: protocol,
1753 testType: serverTest,
1754 name: "Basic-Server-EarlyCallback" + suffix,
1755 config: Config{
1756 Bugs: ProtocolBugs{
1757 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1758 },
1759 },
1760 flags: append(flags, "-use-early-callback"),
1761 resumeSession: true,
1762 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001763
David Benjamin6fd297b2014-08-11 18:43:38 -04001764 // TLS client auth.
1765 testCases = append(testCases, testCase{
1766 protocol: protocol,
1767 testType: clientTest,
1768 name: "ClientAuth-Client" + suffix,
1769 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001770 ClientAuth: RequireAnyClientCert,
David Benjamin6fd297b2014-08-11 18:43:38 -04001771 Bugs: ProtocolBugs{
1772 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1773 },
1774 },
1775 flags: append(flags,
1776 "-cert-file", rsaCertificateFile,
1777 "-key-file", rsaKeyFile),
1778 })
1779 testCases = append(testCases, testCase{
1780 protocol: protocol,
1781 testType: serverTest,
1782 name: "ClientAuth-Server" + suffix,
1783 config: Config{
1784 Certificates: []Certificate{rsaCertificate},
1785 },
1786 flags: append(flags, "-require-any-client-certificate"),
1787 })
1788
David Benjamin43ec06f2014-08-05 02:28:57 -04001789 // No session ticket support; server doesn't send NewSessionTicket.
1790 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001791 protocol: protocol,
1792 name: "SessionTicketsDisabled-Client" + suffix,
David Benjamin43ec06f2014-08-05 02:28:57 -04001793 config: Config{
1794 SessionTicketsDisabled: true,
1795 Bugs: ProtocolBugs{
1796 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1797 },
1798 },
1799 flags: flags,
1800 })
1801 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001802 protocol: protocol,
David Benjamin43ec06f2014-08-05 02:28:57 -04001803 testType: serverTest,
1804 name: "SessionTicketsDisabled-Server" + suffix,
1805 config: Config{
1806 SessionTicketsDisabled: true,
1807 Bugs: ProtocolBugs{
1808 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1809 },
1810 },
1811 flags: flags,
1812 })
1813
David Benjamin48cae082014-10-27 01:06:24 -04001814 // Skip ServerKeyExchange in PSK key exchange if there's no
1815 // identity hint.
1816 testCases = append(testCases, testCase{
1817 protocol: protocol,
1818 name: "EmptyPSKHint-Client" + suffix,
1819 config: Config{
1820 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
1821 PreSharedKey: []byte("secret"),
1822 Bugs: ProtocolBugs{
1823 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1824 },
1825 },
1826 flags: append(flags, "-psk", "secret"),
1827 })
1828 testCases = append(testCases, testCase{
1829 protocol: protocol,
1830 testType: serverTest,
1831 name: "EmptyPSKHint-Server" + suffix,
1832 config: Config{
1833 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
1834 PreSharedKey: []byte("secret"),
1835 Bugs: ProtocolBugs{
1836 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1837 },
1838 },
1839 flags: append(flags, "-psk", "secret"),
1840 })
1841
David Benjamin6fd297b2014-08-11 18:43:38 -04001842 if protocol == tls {
1843 // NPN on client and server; results in post-handshake message.
1844 testCases = append(testCases, testCase{
1845 protocol: protocol,
1846 name: "NPN-Client" + suffix,
1847 config: Config{
David Benjaminae2888f2014-09-06 12:58:58 -04001848 NextProtos: []string{"foo"},
David Benjamin6fd297b2014-08-11 18:43:38 -04001849 Bugs: ProtocolBugs{
1850 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1851 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001852 },
David Benjaminfc7b0862014-09-06 13:21:53 -04001853 flags: append(flags, "-select-next-proto", "foo"),
1854 expectedNextProto: "foo",
1855 expectedNextProtoType: npn,
David Benjamin6fd297b2014-08-11 18:43:38 -04001856 })
1857 testCases = append(testCases, testCase{
1858 protocol: protocol,
1859 testType: serverTest,
1860 name: "NPN-Server" + suffix,
1861 config: Config{
1862 NextProtos: []string{"bar"},
1863 Bugs: ProtocolBugs{
1864 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1865 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001866 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001867 flags: append(flags,
1868 "-advertise-npn", "\x03foo\x03bar\x03baz",
1869 "-expect-next-proto", "bar"),
David Benjaminfc7b0862014-09-06 13:21:53 -04001870 expectedNextProto: "bar",
1871 expectedNextProtoType: npn,
David Benjamin6fd297b2014-08-11 18:43:38 -04001872 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001873
David Benjamin195dc782015-02-19 13:27:05 -05001874 // TODO(davidben): Add tests for when False Start doesn't trigger.
1875
David Benjamin6fd297b2014-08-11 18:43:38 -04001876 // Client does False Start and negotiates NPN.
1877 testCases = append(testCases, testCase{
1878 protocol: protocol,
1879 name: "FalseStart" + suffix,
1880 config: Config{
1881 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1882 NextProtos: []string{"foo"},
1883 Bugs: ProtocolBugs{
David Benjamine58c4f52014-08-24 03:47:07 -04001884 ExpectFalseStart: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001885 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1886 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001887 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001888 flags: append(flags,
1889 "-false-start",
1890 "-select-next-proto", "foo"),
David Benjamine58c4f52014-08-24 03:47:07 -04001891 shimWritesFirst: true,
1892 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001893 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001894
David Benjaminae2888f2014-09-06 12:58:58 -04001895 // Client does False Start and negotiates ALPN.
1896 testCases = append(testCases, testCase{
1897 protocol: protocol,
1898 name: "FalseStart-ALPN" + suffix,
1899 config: Config{
1900 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1901 NextProtos: []string{"foo"},
1902 Bugs: ProtocolBugs{
1903 ExpectFalseStart: true,
1904 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1905 },
1906 },
1907 flags: append(flags,
1908 "-false-start",
1909 "-advertise-alpn", "\x03foo"),
1910 shimWritesFirst: true,
1911 resumeSession: true,
1912 })
1913
David Benjamin931ab342015-02-08 19:46:57 -05001914 // Client does False Start but doesn't explicitly call
1915 // SSL_connect.
1916 testCases = append(testCases, testCase{
1917 protocol: protocol,
1918 name: "FalseStart-Implicit" + suffix,
1919 config: Config{
1920 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1921 NextProtos: []string{"foo"},
1922 Bugs: ProtocolBugs{
1923 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1924 },
1925 },
1926 flags: append(flags,
1927 "-implicit-handshake",
1928 "-false-start",
1929 "-advertise-alpn", "\x03foo"),
1930 })
1931
David Benjamin6fd297b2014-08-11 18:43:38 -04001932 // False Start without session tickets.
1933 testCases = append(testCases, testCase{
David Benjamin5f237bc2015-02-11 17:14:15 -05001934 name: "FalseStart-SessionTicketsDisabled" + suffix,
David Benjamin6fd297b2014-08-11 18:43:38 -04001935 config: Config{
1936 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1937 NextProtos: []string{"foo"},
1938 SessionTicketsDisabled: true,
David Benjamin4e99c522014-08-24 01:45:30 -04001939 Bugs: ProtocolBugs{
David Benjamine58c4f52014-08-24 03:47:07 -04001940 ExpectFalseStart: true,
David Benjamin4e99c522014-08-24 01:45:30 -04001941 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1942 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001943 },
David Benjamin4e99c522014-08-24 01:45:30 -04001944 flags: append(flags,
David Benjamin6fd297b2014-08-11 18:43:38 -04001945 "-false-start",
1946 "-select-next-proto", "foo",
David Benjamin4e99c522014-08-24 01:45:30 -04001947 ),
David Benjamine58c4f52014-08-24 03:47:07 -04001948 shimWritesFirst: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001949 })
David Benjamin1e7f8d72014-08-08 12:27:04 -04001950
David Benjamina08e49d2014-08-24 01:46:07 -04001951 // Server parses a V2ClientHello.
David Benjamin6fd297b2014-08-11 18:43:38 -04001952 testCases = append(testCases, testCase{
1953 protocol: protocol,
1954 testType: serverTest,
1955 name: "SendV2ClientHello" + suffix,
1956 config: Config{
1957 // Choose a cipher suite that does not involve
1958 // elliptic curves, so no extensions are
1959 // involved.
1960 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
1961 Bugs: ProtocolBugs{
1962 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1963 SendV2ClientHello: true,
1964 },
David Benjamin1e7f8d72014-08-08 12:27:04 -04001965 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001966 flags: flags,
1967 })
David Benjamina08e49d2014-08-24 01:46:07 -04001968
1969 // Client sends a Channel ID.
1970 testCases = append(testCases, testCase{
1971 protocol: protocol,
1972 name: "ChannelID-Client" + suffix,
1973 config: Config{
1974 RequestChannelID: true,
1975 Bugs: ProtocolBugs{
1976 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1977 },
1978 },
1979 flags: append(flags,
1980 "-send-channel-id", channelIDKeyFile,
1981 ),
1982 resumeSession: true,
1983 expectChannelID: true,
1984 })
1985
1986 // Server accepts a Channel ID.
1987 testCases = append(testCases, testCase{
1988 protocol: protocol,
1989 testType: serverTest,
1990 name: "ChannelID-Server" + suffix,
1991 config: Config{
1992 ChannelID: channelIDKey,
1993 Bugs: ProtocolBugs{
1994 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1995 },
1996 },
1997 flags: append(flags,
1998 "-expect-channel-id",
1999 base64.StdEncoding.EncodeToString(channelIDBytes),
2000 ),
2001 resumeSession: true,
2002 expectChannelID: true,
2003 })
David Benjamin6fd297b2014-08-11 18:43:38 -04002004 } else {
2005 testCases = append(testCases, testCase{
2006 protocol: protocol,
2007 name: "SkipHelloVerifyRequest" + suffix,
2008 config: Config{
2009 Bugs: ProtocolBugs{
2010 MaxHandshakeRecordLength: maxHandshakeRecordLength,
2011 SkipHelloVerifyRequest: true,
2012 },
2013 },
2014 flags: flags,
2015 })
David Benjamin6fd297b2014-08-11 18:43:38 -04002016 }
David Benjamin43ec06f2014-08-05 02:28:57 -04002017}
2018
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002019func addVersionNegotiationTests() {
2020 for i, shimVers := range tlsVersions {
2021 // Assemble flags to disable all newer versions on the shim.
2022 var flags []string
2023 for _, vers := range tlsVersions[i+1:] {
2024 flags = append(flags, vers.flag)
2025 }
2026
2027 for _, runnerVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05002028 protocols := []protocol{tls}
2029 if runnerVers.hasDTLS && shimVers.hasDTLS {
2030 protocols = append(protocols, dtls)
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002031 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002032 for _, protocol := range protocols {
2033 expectedVersion := shimVers.version
2034 if runnerVers.version < shimVers.version {
2035 expectedVersion = runnerVers.version
2036 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002037
David Benjamin8b8c0062014-11-23 02:47:52 -05002038 suffix := shimVers.name + "-" + runnerVers.name
2039 if protocol == dtls {
2040 suffix += "-DTLS"
2041 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002042
David Benjamin1eb367c2014-12-12 18:17:51 -05002043 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
2044
David Benjamin1e29a6b2014-12-10 02:27:24 -05002045 clientVers := shimVers.version
2046 if clientVers > VersionTLS10 {
2047 clientVers = VersionTLS10
2048 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002049 testCases = append(testCases, testCase{
2050 protocol: protocol,
2051 testType: clientTest,
2052 name: "VersionNegotiation-Client-" + suffix,
2053 config: Config{
2054 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05002055 Bugs: ProtocolBugs{
2056 ExpectInitialRecordVersion: clientVers,
2057 },
David Benjamin8b8c0062014-11-23 02:47:52 -05002058 },
2059 flags: flags,
2060 expectedVersion: expectedVersion,
2061 })
David Benjamin1eb367c2014-12-12 18:17:51 -05002062 testCases = append(testCases, testCase{
2063 protocol: protocol,
2064 testType: clientTest,
2065 name: "VersionNegotiation-Client2-" + suffix,
2066 config: Config{
2067 MaxVersion: runnerVers.version,
2068 Bugs: ProtocolBugs{
2069 ExpectInitialRecordVersion: clientVers,
2070 },
2071 },
2072 flags: []string{"-max-version", shimVersFlag},
2073 expectedVersion: expectedVersion,
2074 })
David Benjamin8b8c0062014-11-23 02:47:52 -05002075
2076 testCases = append(testCases, testCase{
2077 protocol: protocol,
2078 testType: serverTest,
2079 name: "VersionNegotiation-Server-" + suffix,
2080 config: Config{
2081 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05002082 Bugs: ProtocolBugs{
2083 ExpectInitialRecordVersion: expectedVersion,
2084 },
David Benjamin8b8c0062014-11-23 02:47:52 -05002085 },
2086 flags: flags,
2087 expectedVersion: expectedVersion,
2088 })
David Benjamin1eb367c2014-12-12 18:17:51 -05002089 testCases = append(testCases, testCase{
2090 protocol: protocol,
2091 testType: serverTest,
2092 name: "VersionNegotiation-Server2-" + suffix,
2093 config: Config{
2094 MaxVersion: runnerVers.version,
2095 Bugs: ProtocolBugs{
2096 ExpectInitialRecordVersion: expectedVersion,
2097 },
2098 },
2099 flags: []string{"-max-version", shimVersFlag},
2100 expectedVersion: expectedVersion,
2101 })
David Benjamin8b8c0062014-11-23 02:47:52 -05002102 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002103 }
2104 }
2105}
2106
David Benjaminaccb4542014-12-12 23:44:33 -05002107func addMinimumVersionTests() {
2108 for i, shimVers := range tlsVersions {
2109 // Assemble flags to disable all older versions on the shim.
2110 var flags []string
2111 for _, vers := range tlsVersions[:i] {
2112 flags = append(flags, vers.flag)
2113 }
2114
2115 for _, runnerVers := range tlsVersions {
2116 protocols := []protocol{tls}
2117 if runnerVers.hasDTLS && shimVers.hasDTLS {
2118 protocols = append(protocols, dtls)
2119 }
2120 for _, protocol := range protocols {
2121 suffix := shimVers.name + "-" + runnerVers.name
2122 if protocol == dtls {
2123 suffix += "-DTLS"
2124 }
2125 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
2126
David Benjaminaccb4542014-12-12 23:44:33 -05002127 var expectedVersion uint16
2128 var shouldFail bool
2129 var expectedError string
David Benjamin87909c02014-12-13 01:55:01 -05002130 var expectedLocalError string
David Benjaminaccb4542014-12-12 23:44:33 -05002131 if runnerVers.version >= shimVers.version {
2132 expectedVersion = runnerVers.version
2133 } else {
2134 shouldFail = true
2135 expectedError = ":UNSUPPORTED_PROTOCOL:"
David Benjamin87909c02014-12-13 01:55:01 -05002136 if runnerVers.version > VersionSSL30 {
2137 expectedLocalError = "remote error: protocol version not supported"
2138 } else {
2139 expectedLocalError = "remote error: handshake failure"
2140 }
David Benjaminaccb4542014-12-12 23:44:33 -05002141 }
2142
2143 testCases = append(testCases, testCase{
2144 protocol: protocol,
2145 testType: clientTest,
2146 name: "MinimumVersion-Client-" + suffix,
2147 config: Config{
2148 MaxVersion: runnerVers.version,
2149 },
David Benjamin87909c02014-12-13 01:55:01 -05002150 flags: flags,
2151 expectedVersion: expectedVersion,
2152 shouldFail: shouldFail,
2153 expectedError: expectedError,
2154 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002155 })
2156 testCases = append(testCases, testCase{
2157 protocol: protocol,
2158 testType: clientTest,
2159 name: "MinimumVersion-Client2-" + suffix,
2160 config: Config{
2161 MaxVersion: runnerVers.version,
2162 },
David Benjamin87909c02014-12-13 01:55:01 -05002163 flags: []string{"-min-version", shimVersFlag},
2164 expectedVersion: expectedVersion,
2165 shouldFail: shouldFail,
2166 expectedError: expectedError,
2167 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002168 })
2169
2170 testCases = append(testCases, testCase{
2171 protocol: protocol,
2172 testType: serverTest,
2173 name: "MinimumVersion-Server-" + suffix,
2174 config: Config{
2175 MaxVersion: runnerVers.version,
2176 },
David Benjamin87909c02014-12-13 01:55:01 -05002177 flags: flags,
2178 expectedVersion: expectedVersion,
2179 shouldFail: shouldFail,
2180 expectedError: expectedError,
2181 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002182 })
2183 testCases = append(testCases, testCase{
2184 protocol: protocol,
2185 testType: serverTest,
2186 name: "MinimumVersion-Server2-" + suffix,
2187 config: Config{
2188 MaxVersion: runnerVers.version,
2189 },
David Benjamin87909c02014-12-13 01:55:01 -05002190 flags: []string{"-min-version", shimVersFlag},
2191 expectedVersion: expectedVersion,
2192 shouldFail: shouldFail,
2193 expectedError: expectedError,
2194 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002195 })
2196 }
2197 }
2198 }
2199}
2200
David Benjamin5c24a1d2014-08-31 00:59:27 -04002201func addD5BugTests() {
2202 testCases = append(testCases, testCase{
2203 testType: serverTest,
2204 name: "D5Bug-NoQuirk-Reject",
2205 config: Config{
2206 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2207 Bugs: ProtocolBugs{
2208 SSL3RSAKeyExchange: true,
2209 },
2210 },
2211 shouldFail: true,
2212 expectedError: ":TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG:",
2213 })
2214 testCases = append(testCases, testCase{
2215 testType: serverTest,
2216 name: "D5Bug-Quirk-Normal",
2217 config: Config{
2218 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2219 },
2220 flags: []string{"-tls-d5-bug"},
2221 })
2222 testCases = append(testCases, testCase{
2223 testType: serverTest,
2224 name: "D5Bug-Quirk-Bug",
2225 config: Config{
2226 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2227 Bugs: ProtocolBugs{
2228 SSL3RSAKeyExchange: true,
2229 },
2230 },
2231 flags: []string{"-tls-d5-bug"},
2232 })
2233}
2234
David Benjamine78bfde2014-09-06 12:45:15 -04002235func addExtensionTests() {
2236 testCases = append(testCases, testCase{
2237 testType: clientTest,
2238 name: "DuplicateExtensionClient",
2239 config: Config{
2240 Bugs: ProtocolBugs{
2241 DuplicateExtension: true,
2242 },
2243 },
2244 shouldFail: true,
2245 expectedLocalError: "remote error: error decoding message",
2246 })
2247 testCases = append(testCases, testCase{
2248 testType: serverTest,
2249 name: "DuplicateExtensionServer",
2250 config: Config{
2251 Bugs: ProtocolBugs{
2252 DuplicateExtension: true,
2253 },
2254 },
2255 shouldFail: true,
2256 expectedLocalError: "remote error: error decoding message",
2257 })
2258 testCases = append(testCases, testCase{
2259 testType: clientTest,
2260 name: "ServerNameExtensionClient",
2261 config: Config{
2262 Bugs: ProtocolBugs{
2263 ExpectServerName: "example.com",
2264 },
2265 },
2266 flags: []string{"-host-name", "example.com"},
2267 })
2268 testCases = append(testCases, testCase{
2269 testType: clientTest,
David Benjamin5f237bc2015-02-11 17:14:15 -05002270 name: "ServerNameExtensionClientMismatch",
David Benjamine78bfde2014-09-06 12:45:15 -04002271 config: Config{
2272 Bugs: ProtocolBugs{
2273 ExpectServerName: "mismatch.com",
2274 },
2275 },
2276 flags: []string{"-host-name", "example.com"},
2277 shouldFail: true,
2278 expectedLocalError: "tls: unexpected server name",
2279 })
2280 testCases = append(testCases, testCase{
2281 testType: clientTest,
David Benjamin5f237bc2015-02-11 17:14:15 -05002282 name: "ServerNameExtensionClientMissing",
David Benjamine78bfde2014-09-06 12:45:15 -04002283 config: Config{
2284 Bugs: ProtocolBugs{
2285 ExpectServerName: "missing.com",
2286 },
2287 },
2288 shouldFail: true,
2289 expectedLocalError: "tls: unexpected server name",
2290 })
2291 testCases = append(testCases, testCase{
2292 testType: serverTest,
2293 name: "ServerNameExtensionServer",
2294 config: Config{
2295 ServerName: "example.com",
2296 },
2297 flags: []string{"-expect-server-name", "example.com"},
2298 resumeSession: true,
2299 })
David Benjaminae2888f2014-09-06 12:58:58 -04002300 testCases = append(testCases, testCase{
2301 testType: clientTest,
2302 name: "ALPNClient",
2303 config: Config{
2304 NextProtos: []string{"foo"},
2305 },
2306 flags: []string{
2307 "-advertise-alpn", "\x03foo\x03bar\x03baz",
2308 "-expect-alpn", "foo",
2309 },
David Benjaminfc7b0862014-09-06 13:21:53 -04002310 expectedNextProto: "foo",
2311 expectedNextProtoType: alpn,
2312 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04002313 })
2314 testCases = append(testCases, testCase{
2315 testType: serverTest,
2316 name: "ALPNServer",
2317 config: Config{
2318 NextProtos: []string{"foo", "bar", "baz"},
2319 },
2320 flags: []string{
2321 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2322 "-select-alpn", "foo",
2323 },
David Benjaminfc7b0862014-09-06 13:21:53 -04002324 expectedNextProto: "foo",
2325 expectedNextProtoType: alpn,
2326 resumeSession: true,
2327 })
2328 // Test that the server prefers ALPN over NPN.
2329 testCases = append(testCases, testCase{
2330 testType: serverTest,
2331 name: "ALPNServer-Preferred",
2332 config: Config{
2333 NextProtos: []string{"foo", "bar", "baz"},
2334 },
2335 flags: []string{
2336 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2337 "-select-alpn", "foo",
2338 "-advertise-npn", "\x03foo\x03bar\x03baz",
2339 },
2340 expectedNextProto: "foo",
2341 expectedNextProtoType: alpn,
2342 resumeSession: true,
2343 })
2344 testCases = append(testCases, testCase{
2345 testType: serverTest,
2346 name: "ALPNServer-Preferred-Swapped",
2347 config: Config{
2348 NextProtos: []string{"foo", "bar", "baz"},
2349 Bugs: ProtocolBugs{
2350 SwapNPNAndALPN: true,
2351 },
2352 },
2353 flags: []string{
2354 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2355 "-select-alpn", "foo",
2356 "-advertise-npn", "\x03foo\x03bar\x03baz",
2357 },
2358 expectedNextProto: "foo",
2359 expectedNextProtoType: alpn,
2360 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04002361 })
Adam Langley38311732014-10-16 19:04:35 -07002362 // Resume with a corrupt ticket.
2363 testCases = append(testCases, testCase{
2364 testType: serverTest,
2365 name: "CorruptTicket",
2366 config: Config{
2367 Bugs: ProtocolBugs{
2368 CorruptTicket: true,
2369 },
2370 },
2371 resumeSession: true,
2372 flags: []string{"-expect-session-miss"},
2373 })
2374 // Resume with an oversized session id.
2375 testCases = append(testCases, testCase{
2376 testType: serverTest,
2377 name: "OversizedSessionId",
2378 config: Config{
2379 Bugs: ProtocolBugs{
2380 OversizedSessionId: true,
2381 },
2382 },
2383 resumeSession: true,
Adam Langley75712922014-10-10 16:23:43 -07002384 shouldFail: true,
Adam Langley38311732014-10-16 19:04:35 -07002385 expectedError: ":DECODE_ERROR:",
2386 })
David Benjaminca6c8262014-11-15 19:06:08 -05002387 // Basic DTLS-SRTP tests. Include fake profiles to ensure they
2388 // are ignored.
2389 testCases = append(testCases, testCase{
2390 protocol: dtls,
2391 name: "SRTP-Client",
2392 config: Config{
2393 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
2394 },
2395 flags: []string{
2396 "-srtp-profiles",
2397 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2398 },
2399 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2400 })
2401 testCases = append(testCases, testCase{
2402 protocol: dtls,
2403 testType: serverTest,
2404 name: "SRTP-Server",
2405 config: Config{
2406 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
2407 },
2408 flags: []string{
2409 "-srtp-profiles",
2410 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2411 },
2412 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2413 })
2414 // Test that the MKI is ignored.
2415 testCases = append(testCases, testCase{
2416 protocol: dtls,
2417 testType: serverTest,
2418 name: "SRTP-Server-IgnoreMKI",
2419 config: Config{
2420 SRTPProtectionProfiles: []uint16{SRTP_AES128_CM_HMAC_SHA1_80},
2421 Bugs: ProtocolBugs{
2422 SRTPMasterKeyIdentifer: "bogus",
2423 },
2424 },
2425 flags: []string{
2426 "-srtp-profiles",
2427 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2428 },
2429 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2430 })
2431 // Test that SRTP isn't negotiated on the server if there were
2432 // no matching profiles.
2433 testCases = append(testCases, testCase{
2434 protocol: dtls,
2435 testType: serverTest,
2436 name: "SRTP-Server-NoMatch",
2437 config: Config{
2438 SRTPProtectionProfiles: []uint16{100, 101, 102},
2439 },
2440 flags: []string{
2441 "-srtp-profiles",
2442 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2443 },
2444 expectedSRTPProtectionProfile: 0,
2445 })
2446 // Test that the server returning an invalid SRTP profile is
2447 // flagged as an error by the client.
2448 testCases = append(testCases, testCase{
2449 protocol: dtls,
2450 name: "SRTP-Client-NoMatch",
2451 config: Config{
2452 Bugs: ProtocolBugs{
2453 SendSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_32,
2454 },
2455 },
2456 flags: []string{
2457 "-srtp-profiles",
2458 "SRTP_AES128_CM_SHA1_80",
2459 },
2460 shouldFail: true,
2461 expectedError: ":BAD_SRTP_PROTECTION_PROFILE_LIST:",
2462 })
David Benjamin61f95272014-11-25 01:55:35 -05002463 // Test OCSP stapling and SCT list.
2464 testCases = append(testCases, testCase{
2465 name: "OCSPStapling",
2466 flags: []string{
2467 "-enable-ocsp-stapling",
2468 "-expect-ocsp-response",
2469 base64.StdEncoding.EncodeToString(testOCSPResponse),
2470 },
2471 })
2472 testCases = append(testCases, testCase{
2473 name: "SignedCertificateTimestampList",
2474 flags: []string{
2475 "-enable-signed-cert-timestamps",
2476 "-expect-signed-cert-timestamps",
2477 base64.StdEncoding.EncodeToString(testSCTList),
2478 },
2479 })
David Benjamine78bfde2014-09-06 12:45:15 -04002480}
2481
David Benjamin01fe8202014-09-24 15:21:44 -04002482func addResumptionVersionTests() {
David Benjamin01fe8202014-09-24 15:21:44 -04002483 for _, sessionVers := range tlsVersions {
David Benjamin01fe8202014-09-24 15:21:44 -04002484 for _, resumeVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05002485 protocols := []protocol{tls}
2486 if sessionVers.hasDTLS && resumeVers.hasDTLS {
2487 protocols = append(protocols, dtls)
David Benjaminbdf5e722014-11-11 00:52:15 -05002488 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002489 for _, protocol := range protocols {
2490 suffix := "-" + sessionVers.name + "-" + resumeVers.name
2491 if protocol == dtls {
2492 suffix += "-DTLS"
2493 }
2494
2495 testCases = append(testCases, testCase{
2496 protocol: protocol,
2497 name: "Resume-Client" + suffix,
2498 resumeSession: true,
2499 config: Config{
2500 MaxVersion: sessionVers.version,
2501 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2502 Bugs: ProtocolBugs{
2503 AllowSessionVersionMismatch: true,
2504 },
2505 },
2506 expectedVersion: sessionVers.version,
2507 resumeConfig: &Config{
2508 MaxVersion: resumeVers.version,
2509 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2510 Bugs: ProtocolBugs{
2511 AllowSessionVersionMismatch: true,
2512 },
2513 },
2514 expectedResumeVersion: resumeVers.version,
2515 })
2516
2517 testCases = append(testCases, testCase{
2518 protocol: protocol,
2519 name: "Resume-Client-NoResume" + suffix,
2520 flags: []string{"-expect-session-miss"},
2521 resumeSession: true,
2522 config: Config{
2523 MaxVersion: sessionVers.version,
2524 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2525 },
2526 expectedVersion: sessionVers.version,
2527 resumeConfig: &Config{
2528 MaxVersion: resumeVers.version,
2529 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2530 },
2531 newSessionsOnResume: true,
2532 expectedResumeVersion: resumeVers.version,
2533 })
2534
2535 var flags []string
2536 if sessionVers.version != resumeVers.version {
2537 flags = append(flags, "-expect-session-miss")
2538 }
2539 testCases = append(testCases, testCase{
2540 protocol: protocol,
2541 testType: serverTest,
2542 name: "Resume-Server" + suffix,
2543 flags: flags,
2544 resumeSession: true,
2545 config: Config{
2546 MaxVersion: sessionVers.version,
2547 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2548 },
2549 expectedVersion: sessionVers.version,
2550 resumeConfig: &Config{
2551 MaxVersion: resumeVers.version,
2552 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2553 },
2554 expectedResumeVersion: resumeVers.version,
2555 })
2556 }
David Benjamin01fe8202014-09-24 15:21:44 -04002557 }
2558 }
2559}
2560
Adam Langley2ae77d22014-10-28 17:29:33 -07002561func addRenegotiationTests() {
2562 testCases = append(testCases, testCase{
2563 testType: serverTest,
2564 name: "Renegotiate-Server",
2565 flags: []string{"-renegotiate"},
2566 shimWritesFirst: true,
2567 })
2568 testCases = append(testCases, testCase{
2569 testType: serverTest,
2570 name: "Renegotiate-Server-EmptyExt",
2571 config: Config{
2572 Bugs: ProtocolBugs{
2573 EmptyRenegotiationInfo: true,
2574 },
2575 },
2576 flags: []string{"-renegotiate"},
2577 shimWritesFirst: true,
2578 shouldFail: true,
2579 expectedError: ":RENEGOTIATION_MISMATCH:",
2580 })
2581 testCases = append(testCases, testCase{
2582 testType: serverTest,
2583 name: "Renegotiate-Server-BadExt",
2584 config: Config{
2585 Bugs: ProtocolBugs{
2586 BadRenegotiationInfo: true,
2587 },
2588 },
2589 flags: []string{"-renegotiate"},
2590 shimWritesFirst: true,
2591 shouldFail: true,
2592 expectedError: ":RENEGOTIATION_MISMATCH:",
2593 })
David Benjaminca6554b2014-11-08 12:31:52 -05002594 testCases = append(testCases, testCase{
2595 testType: serverTest,
2596 name: "Renegotiate-Server-ClientInitiated",
2597 renegotiate: true,
2598 })
2599 testCases = append(testCases, testCase{
2600 testType: serverTest,
2601 name: "Renegotiate-Server-ClientInitiated-NoExt",
2602 renegotiate: true,
2603 config: Config{
2604 Bugs: ProtocolBugs{
2605 NoRenegotiationInfo: true,
2606 },
2607 },
2608 shouldFail: true,
2609 expectedError: ":UNSAFE_LEGACY_RENEGOTIATION_DISABLED:",
2610 })
2611 testCases = append(testCases, testCase{
2612 testType: serverTest,
2613 name: "Renegotiate-Server-ClientInitiated-NoExt-Allowed",
2614 renegotiate: true,
2615 config: Config{
2616 Bugs: ProtocolBugs{
2617 NoRenegotiationInfo: true,
2618 },
2619 },
2620 flags: []string{"-allow-unsafe-legacy-renegotiation"},
2621 })
Adam Langley2ae77d22014-10-28 17:29:33 -07002622 // TODO(agl): test the renegotiation info SCSV.
Adam Langleycf2d4f42014-10-28 19:06:14 -07002623 testCases = append(testCases, testCase{
2624 name: "Renegotiate-Client",
2625 renegotiate: true,
2626 })
2627 testCases = append(testCases, testCase{
2628 name: "Renegotiate-Client-EmptyExt",
2629 renegotiate: true,
2630 config: Config{
2631 Bugs: ProtocolBugs{
2632 EmptyRenegotiationInfo: true,
2633 },
2634 },
2635 shouldFail: true,
2636 expectedError: ":RENEGOTIATION_MISMATCH:",
2637 })
2638 testCases = append(testCases, testCase{
2639 name: "Renegotiate-Client-BadExt",
2640 renegotiate: true,
2641 config: Config{
2642 Bugs: ProtocolBugs{
2643 BadRenegotiationInfo: true,
2644 },
2645 },
2646 shouldFail: true,
2647 expectedError: ":RENEGOTIATION_MISMATCH:",
2648 })
2649 testCases = append(testCases, testCase{
2650 name: "Renegotiate-Client-SwitchCiphers",
2651 renegotiate: true,
2652 config: Config{
2653 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2654 },
2655 renegotiateCiphers: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2656 })
2657 testCases = append(testCases, testCase{
2658 name: "Renegotiate-Client-SwitchCiphers2",
2659 renegotiate: true,
2660 config: Config{
2661 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2662 },
2663 renegotiateCiphers: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2664 })
David Benjaminc44b1df2014-11-23 12:11:01 -05002665 testCases = append(testCases, testCase{
2666 name: "Renegotiate-SameClientVersion",
2667 renegotiate: true,
2668 config: Config{
2669 MaxVersion: VersionTLS10,
2670 Bugs: ProtocolBugs{
2671 RequireSameRenegoClientVersion: true,
2672 },
2673 },
2674 })
Adam Langley2ae77d22014-10-28 17:29:33 -07002675}
2676
David Benjamin5e961c12014-11-07 01:48:35 -05002677func addDTLSReplayTests() {
2678 // Test that sequence number replays are detected.
2679 testCases = append(testCases, testCase{
2680 protocol: dtls,
2681 name: "DTLS-Replay",
2682 replayWrites: true,
2683 })
2684
2685 // Test the outgoing sequence number skipping by values larger
2686 // than the retransmit window.
2687 testCases = append(testCases, testCase{
2688 protocol: dtls,
2689 name: "DTLS-Replay-LargeGaps",
2690 config: Config{
2691 Bugs: ProtocolBugs{
2692 SequenceNumberIncrement: 127,
2693 },
2694 },
2695 replayWrites: true,
2696 })
2697}
2698
Feng Lu41aa3252014-11-21 22:47:56 -08002699func addFastRadioPaddingTests() {
David Benjamin1e29a6b2014-12-10 02:27:24 -05002700 testCases = append(testCases, testCase{
2701 protocol: tls,
2702 name: "FastRadio-Padding",
Feng Lu41aa3252014-11-21 22:47:56 -08002703 config: Config{
2704 Bugs: ProtocolBugs{
2705 RequireFastradioPadding: true,
2706 },
2707 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05002708 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08002709 })
David Benjamin1e29a6b2014-12-10 02:27:24 -05002710 testCases = append(testCases, testCase{
2711 protocol: dtls,
David Benjamin5f237bc2015-02-11 17:14:15 -05002712 name: "FastRadio-Padding-DTLS",
Feng Lu41aa3252014-11-21 22:47:56 -08002713 config: Config{
2714 Bugs: ProtocolBugs{
2715 RequireFastradioPadding: true,
2716 },
2717 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05002718 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08002719 })
2720}
2721
David Benjamin000800a2014-11-14 01:43:59 -05002722var testHashes = []struct {
2723 name string
2724 id uint8
2725}{
2726 {"SHA1", hashSHA1},
2727 {"SHA224", hashSHA224},
2728 {"SHA256", hashSHA256},
2729 {"SHA384", hashSHA384},
2730 {"SHA512", hashSHA512},
2731}
2732
2733func addSigningHashTests() {
2734 // Make sure each hash works. Include some fake hashes in the list and
2735 // ensure they're ignored.
2736 for _, hash := range testHashes {
2737 testCases = append(testCases, testCase{
2738 name: "SigningHash-ClientAuth-" + hash.name,
2739 config: Config{
2740 ClientAuth: RequireAnyClientCert,
2741 SignatureAndHashes: []signatureAndHash{
2742 {signatureRSA, 42},
2743 {signatureRSA, hash.id},
2744 {signatureRSA, 255},
2745 },
2746 },
2747 flags: []string{
2748 "-cert-file", rsaCertificateFile,
2749 "-key-file", rsaKeyFile,
2750 },
2751 })
2752
2753 testCases = append(testCases, testCase{
2754 testType: serverTest,
2755 name: "SigningHash-ServerKeyExchange-Sign-" + hash.name,
2756 config: Config{
2757 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2758 SignatureAndHashes: []signatureAndHash{
2759 {signatureRSA, 42},
2760 {signatureRSA, hash.id},
2761 {signatureRSA, 255},
2762 },
2763 },
2764 })
2765 }
2766
2767 // Test that hash resolution takes the signature type into account.
2768 testCases = append(testCases, testCase{
2769 name: "SigningHash-ClientAuth-SignatureType",
2770 config: Config{
2771 ClientAuth: RequireAnyClientCert,
2772 SignatureAndHashes: []signatureAndHash{
2773 {signatureECDSA, hashSHA512},
2774 {signatureRSA, hashSHA384},
2775 {signatureECDSA, hashSHA1},
2776 },
2777 },
2778 flags: []string{
2779 "-cert-file", rsaCertificateFile,
2780 "-key-file", rsaKeyFile,
2781 },
2782 })
2783
2784 testCases = append(testCases, testCase{
2785 testType: serverTest,
2786 name: "SigningHash-ServerKeyExchange-SignatureType",
2787 config: Config{
2788 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2789 SignatureAndHashes: []signatureAndHash{
2790 {signatureECDSA, hashSHA512},
2791 {signatureRSA, hashSHA384},
2792 {signatureECDSA, hashSHA1},
2793 },
2794 },
2795 })
2796
2797 // Test that, if the list is missing, the peer falls back to SHA-1.
2798 testCases = append(testCases, testCase{
2799 name: "SigningHash-ClientAuth-Fallback",
2800 config: Config{
2801 ClientAuth: RequireAnyClientCert,
2802 SignatureAndHashes: []signatureAndHash{
2803 {signatureRSA, hashSHA1},
2804 },
2805 Bugs: ProtocolBugs{
2806 NoSignatureAndHashes: true,
2807 },
2808 },
2809 flags: []string{
2810 "-cert-file", rsaCertificateFile,
2811 "-key-file", rsaKeyFile,
2812 },
2813 })
2814
2815 testCases = append(testCases, testCase{
2816 testType: serverTest,
2817 name: "SigningHash-ServerKeyExchange-Fallback",
2818 config: Config{
2819 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2820 SignatureAndHashes: []signatureAndHash{
2821 {signatureRSA, hashSHA1},
2822 },
2823 Bugs: ProtocolBugs{
2824 NoSignatureAndHashes: true,
2825 },
2826 },
2827 })
2828}
2829
David Benjamin83f90402015-01-27 01:09:43 -05002830// timeouts is the retransmit schedule for BoringSSL. It doubles and
2831// caps at 60 seconds. On the 13th timeout, it gives up.
2832var timeouts = []time.Duration{
2833 1 * time.Second,
2834 2 * time.Second,
2835 4 * time.Second,
2836 8 * time.Second,
2837 16 * time.Second,
2838 32 * time.Second,
2839 60 * time.Second,
2840 60 * time.Second,
2841 60 * time.Second,
2842 60 * time.Second,
2843 60 * time.Second,
2844 60 * time.Second,
2845 60 * time.Second,
2846}
2847
2848func addDTLSRetransmitTests() {
2849 // Test that this is indeed the timeout schedule. Stress all
2850 // four patterns of handshake.
2851 for i := 1; i < len(timeouts); i++ {
2852 number := strconv.Itoa(i)
2853 testCases = append(testCases, testCase{
2854 protocol: dtls,
2855 name: "DTLS-Retransmit-Client-" + number,
2856 config: Config{
2857 Bugs: ProtocolBugs{
2858 TimeoutSchedule: timeouts[:i],
2859 },
2860 },
2861 resumeSession: true,
2862 flags: []string{"-async"},
2863 })
2864 testCases = append(testCases, testCase{
2865 protocol: dtls,
2866 testType: serverTest,
2867 name: "DTLS-Retransmit-Server-" + number,
2868 config: Config{
2869 Bugs: ProtocolBugs{
2870 TimeoutSchedule: timeouts[:i],
2871 },
2872 },
2873 resumeSession: true,
2874 flags: []string{"-async"},
2875 })
2876 }
2877
2878 // Test that exceeding the timeout schedule hits a read
2879 // timeout.
2880 testCases = append(testCases, testCase{
2881 protocol: dtls,
2882 name: "DTLS-Retransmit-Timeout",
2883 config: Config{
2884 Bugs: ProtocolBugs{
2885 TimeoutSchedule: timeouts,
2886 },
2887 },
2888 resumeSession: true,
2889 flags: []string{"-async"},
2890 shouldFail: true,
2891 expectedError: ":READ_TIMEOUT_EXPIRED:",
2892 })
2893
2894 // Test that timeout handling has a fudge factor, due to API
2895 // problems.
2896 testCases = append(testCases, testCase{
2897 protocol: dtls,
2898 name: "DTLS-Retransmit-Fudge",
2899 config: Config{
2900 Bugs: ProtocolBugs{
2901 TimeoutSchedule: []time.Duration{
2902 timeouts[0] - 10*time.Millisecond,
2903 },
2904 },
2905 },
2906 resumeSession: true,
2907 flags: []string{"-async"},
2908 })
David Benjamin7eaab4c2015-03-02 19:01:16 -05002909
2910 // Test that the final Finished retransmitting isn't
2911 // duplicated if the peer badly fragments everything.
2912 testCases = append(testCases, testCase{
2913 testType: serverTest,
2914 protocol: dtls,
2915 name: "DTLS-Retransmit-Fragmented",
2916 config: Config{
2917 Bugs: ProtocolBugs{
2918 TimeoutSchedule: []time.Duration{timeouts[0]},
2919 MaxHandshakeRecordLength: 2,
2920 },
2921 },
2922 flags: []string{"-async"},
2923 })
David Benjamin83f90402015-01-27 01:09:43 -05002924}
2925
David Benjamin884fdf12014-08-02 15:28:23 -04002926func worker(statusChan chan statusMsg, c chan *testCase, buildDir string, wg *sync.WaitGroup) {
Adam Langley95c29f32014-06-20 12:00:00 -07002927 defer wg.Done()
2928
2929 for test := range c {
Adam Langley69a01602014-11-17 17:26:55 -08002930 var err error
2931
2932 if *mallocTest < 0 {
2933 statusChan <- statusMsg{test: test, started: true}
2934 err = runTest(test, buildDir, -1)
2935 } else {
2936 for mallocNumToFail := int64(*mallocTest); ; mallocNumToFail++ {
2937 statusChan <- statusMsg{test: test, started: true}
2938 if err = runTest(test, buildDir, mallocNumToFail); err != errMoreMallocs {
2939 if err != nil {
2940 fmt.Printf("\n\nmalloc test failed at %d: %s\n", mallocNumToFail, err)
2941 }
2942 break
2943 }
2944 }
2945 }
Adam Langley95c29f32014-06-20 12:00:00 -07002946 statusChan <- statusMsg{test: test, err: err}
2947 }
2948}
2949
2950type statusMsg struct {
2951 test *testCase
2952 started bool
2953 err error
2954}
2955
David Benjamin5f237bc2015-02-11 17:14:15 -05002956func statusPrinter(doneChan chan *testOutput, statusChan chan statusMsg, total int) {
Adam Langley95c29f32014-06-20 12:00:00 -07002957 var started, done, failed, lineLen int
Adam Langley95c29f32014-06-20 12:00:00 -07002958
David Benjamin5f237bc2015-02-11 17:14:15 -05002959 testOutput := newTestOutput()
Adam Langley95c29f32014-06-20 12:00:00 -07002960 for msg := range statusChan {
David Benjamin5f237bc2015-02-11 17:14:15 -05002961 if !*pipe {
2962 // Erase the previous status line.
David Benjamin87c8a642015-02-21 01:54:29 -05002963 var erase string
2964 for i := 0; i < lineLen; i++ {
2965 erase += "\b \b"
2966 }
2967 fmt.Print(erase)
David Benjamin5f237bc2015-02-11 17:14:15 -05002968 }
2969
Adam Langley95c29f32014-06-20 12:00:00 -07002970 if msg.started {
2971 started++
2972 } else {
2973 done++
David Benjamin5f237bc2015-02-11 17:14:15 -05002974
2975 if msg.err != nil {
2976 fmt.Printf("FAILED (%s)\n%s\n", msg.test.name, msg.err)
2977 failed++
2978 testOutput.addResult(msg.test.name, "FAIL")
2979 } else {
2980 if *pipe {
2981 // Print each test instead of a status line.
2982 fmt.Printf("PASSED (%s)\n", msg.test.name)
2983 }
2984 testOutput.addResult(msg.test.name, "PASS")
2985 }
Adam Langley95c29f32014-06-20 12:00:00 -07002986 }
2987
David Benjamin5f237bc2015-02-11 17:14:15 -05002988 if !*pipe {
2989 // Print a new status line.
2990 line := fmt.Sprintf("%d/%d/%d/%d", failed, done, started, total)
2991 lineLen = len(line)
2992 os.Stdout.WriteString(line)
Adam Langley95c29f32014-06-20 12:00:00 -07002993 }
Adam Langley95c29f32014-06-20 12:00:00 -07002994 }
David Benjamin5f237bc2015-02-11 17:14:15 -05002995
2996 doneChan <- testOutput
Adam Langley95c29f32014-06-20 12:00:00 -07002997}
2998
2999func main() {
3000 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 -04003001 var flagNumWorkers *int = flag.Int("num-workers", runtime.NumCPU(), "The number of workers to run in parallel.")
David Benjamin884fdf12014-08-02 15:28:23 -04003002 var flagBuildDir *string = flag.String("build-dir", "../../../build", "The build directory to run the shim from.")
Adam Langley95c29f32014-06-20 12:00:00 -07003003
3004 flag.Parse()
3005
3006 addCipherSuiteTests()
3007 addBadECDSASignatureTests()
Adam Langley80842bd2014-06-20 12:00:00 -07003008 addCBCPaddingTests()
Kenny Root7fdeaf12014-08-05 15:23:37 -07003009 addCBCSplittingTests()
David Benjamin636293b2014-07-08 17:59:18 -04003010 addClientAuthTests()
David Benjamin7e2e6cf2014-08-07 17:44:24 -04003011 addVersionNegotiationTests()
David Benjaminaccb4542014-12-12 23:44:33 -05003012 addMinimumVersionTests()
David Benjamin5c24a1d2014-08-31 00:59:27 -04003013 addD5BugTests()
David Benjamine78bfde2014-09-06 12:45:15 -04003014 addExtensionTests()
David Benjamin01fe8202014-09-24 15:21:44 -04003015 addResumptionVersionTests()
Adam Langley75712922014-10-10 16:23:43 -07003016 addExtendedMasterSecretTests()
Adam Langley2ae77d22014-10-28 17:29:33 -07003017 addRenegotiationTests()
David Benjamin5e961c12014-11-07 01:48:35 -05003018 addDTLSReplayTests()
David Benjamin000800a2014-11-14 01:43:59 -05003019 addSigningHashTests()
Feng Lu41aa3252014-11-21 22:47:56 -08003020 addFastRadioPaddingTests()
David Benjamin83f90402015-01-27 01:09:43 -05003021 addDTLSRetransmitTests()
David Benjamin43ec06f2014-08-05 02:28:57 -04003022 for _, async := range []bool{false, true} {
3023 for _, splitHandshake := range []bool{false, true} {
David Benjamin6fd297b2014-08-11 18:43:38 -04003024 for _, protocol := range []protocol{tls, dtls} {
3025 addStateMachineCoverageTests(async, splitHandshake, protocol)
3026 }
David Benjamin43ec06f2014-08-05 02:28:57 -04003027 }
3028 }
Adam Langley95c29f32014-06-20 12:00:00 -07003029
3030 var wg sync.WaitGroup
3031
David Benjamin2bc8e6f2014-08-02 15:22:37 -04003032 numWorkers := *flagNumWorkers
Adam Langley95c29f32014-06-20 12:00:00 -07003033
3034 statusChan := make(chan statusMsg, numWorkers)
3035 testChan := make(chan *testCase, numWorkers)
David Benjamin5f237bc2015-02-11 17:14:15 -05003036 doneChan := make(chan *testOutput)
Adam Langley95c29f32014-06-20 12:00:00 -07003037
David Benjamin025b3d32014-07-01 19:53:04 -04003038 go statusPrinter(doneChan, statusChan, len(testCases))
Adam Langley95c29f32014-06-20 12:00:00 -07003039
3040 for i := 0; i < numWorkers; i++ {
3041 wg.Add(1)
David Benjamin884fdf12014-08-02 15:28:23 -04003042 go worker(statusChan, testChan, *flagBuildDir, &wg)
Adam Langley95c29f32014-06-20 12:00:00 -07003043 }
3044
David Benjamin025b3d32014-07-01 19:53:04 -04003045 for i := range testCases {
3046 if len(*flagTest) == 0 || *flagTest == testCases[i].name {
3047 testChan <- &testCases[i]
Adam Langley95c29f32014-06-20 12:00:00 -07003048 }
3049 }
3050
3051 close(testChan)
3052 wg.Wait()
3053 close(statusChan)
David Benjamin5f237bc2015-02-11 17:14:15 -05003054 testOutput := <-doneChan
Adam Langley95c29f32014-06-20 12:00:00 -07003055
3056 fmt.Printf("\n")
David Benjamin5f237bc2015-02-11 17:14:15 -05003057
3058 if *jsonOutput != "" {
3059 if err := testOutput.writeTo(*jsonOutput); err != nil {
3060 fmt.Fprintf(os.Stderr, "Error: %s\n", err)
3061 }
3062 }
Adam Langley95c29f32014-06-20 12:00:00 -07003063}