blob: 2b91f43a36014705418748e031672d422304db1b [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"
23)
24
Adam Langley69a01602014-11-17 17:26:55 -080025var (
26 useValgrind = flag.Bool("valgrind", false, "If true, run code under valgrind")
27 useGDB = flag.Bool("gdb", false, "If true, run BoringSSL code under gdb")
28 flagDebug *bool = flag.Bool("debug", false, "Hexdump the contents of the connection")
29 mallocTest *int64 = flag.Int64("malloc-test", -1, "If non-negative, run each test with each malloc in turn failing from the given number onwards.")
30 mallocTestDebug *bool = flag.Bool("malloc-test-debug", false, "If true, ask bssl_shim to abort rather than fail a malloc. This can be used with a specific value for --malloc-test to identity the malloc failing that is causing problems.")
31)
Adam Langley95c29f32014-06-20 12:00:00 -070032
David Benjamin025b3d32014-07-01 19:53:04 -040033const (
34 rsaCertificateFile = "cert.pem"
35 ecdsaCertificateFile = "ecdsa_cert.pem"
36)
37
38const (
David Benjamina08e49d2014-08-24 01:46:07 -040039 rsaKeyFile = "key.pem"
40 ecdsaKeyFile = "ecdsa_key.pem"
41 channelIDKeyFile = "channel_id_key.pem"
David Benjamin025b3d32014-07-01 19:53:04 -040042)
43
Adam Langley95c29f32014-06-20 12:00:00 -070044var rsaCertificate, ecdsaCertificate Certificate
David Benjamina08e49d2014-08-24 01:46:07 -040045var channelIDKey *ecdsa.PrivateKey
46var channelIDBytes []byte
Adam Langley95c29f32014-06-20 12:00:00 -070047
David Benjamin61f95272014-11-25 01:55:35 -050048var testOCSPResponse = []byte{1, 2, 3, 4}
49var testSCTList = []byte{5, 6, 7, 8}
50
Adam Langley95c29f32014-06-20 12:00:00 -070051func initCertificates() {
52 var err error
David Benjamin025b3d32014-07-01 19:53:04 -040053 rsaCertificate, err = LoadX509KeyPair(rsaCertificateFile, rsaKeyFile)
Adam Langley95c29f32014-06-20 12:00:00 -070054 if err != nil {
55 panic(err)
56 }
David Benjamin61f95272014-11-25 01:55:35 -050057 rsaCertificate.OCSPStaple = testOCSPResponse
58 rsaCertificate.SignedCertificateTimestampList = testSCTList
Adam Langley95c29f32014-06-20 12:00:00 -070059
David Benjamin025b3d32014-07-01 19:53:04 -040060 ecdsaCertificate, err = LoadX509KeyPair(ecdsaCertificateFile, ecdsaKeyFile)
Adam Langley95c29f32014-06-20 12:00:00 -070061 if err != nil {
62 panic(err)
63 }
David Benjamin61f95272014-11-25 01:55:35 -050064 ecdsaCertificate.OCSPStaple = testOCSPResponse
65 ecdsaCertificate.SignedCertificateTimestampList = testSCTList
David Benjamina08e49d2014-08-24 01:46:07 -040066
67 channelIDPEMBlock, err := ioutil.ReadFile(channelIDKeyFile)
68 if err != nil {
69 panic(err)
70 }
71 channelIDDERBlock, _ := pem.Decode(channelIDPEMBlock)
72 if channelIDDERBlock.Type != "EC PRIVATE KEY" {
73 panic("bad key type")
74 }
75 channelIDKey, err = x509.ParseECPrivateKey(channelIDDERBlock.Bytes)
76 if err != nil {
77 panic(err)
78 }
79 if channelIDKey.Curve != elliptic.P256() {
80 panic("bad curve")
81 }
82
83 channelIDBytes = make([]byte, 64)
84 writeIntPadded(channelIDBytes[:32], channelIDKey.X)
85 writeIntPadded(channelIDBytes[32:], channelIDKey.Y)
Adam Langley95c29f32014-06-20 12:00:00 -070086}
87
88var certificateOnce sync.Once
89
90func getRSACertificate() Certificate {
91 certificateOnce.Do(initCertificates)
92 return rsaCertificate
93}
94
95func getECDSACertificate() Certificate {
96 certificateOnce.Do(initCertificates)
97 return ecdsaCertificate
98}
99
David Benjamin025b3d32014-07-01 19:53:04 -0400100type testType int
101
102const (
103 clientTest testType = iota
104 serverTest
105)
106
David Benjamin6fd297b2014-08-11 18:43:38 -0400107type protocol int
108
109const (
110 tls protocol = iota
111 dtls
112)
113
David Benjaminfc7b0862014-09-06 13:21:53 -0400114const (
115 alpn = 1
116 npn = 2
117)
118
Adam Langley95c29f32014-06-20 12:00:00 -0700119type testCase struct {
David Benjamin025b3d32014-07-01 19:53:04 -0400120 testType testType
David Benjamin6fd297b2014-08-11 18:43:38 -0400121 protocol protocol
Adam Langley95c29f32014-06-20 12:00:00 -0700122 name string
123 config Config
124 shouldFail bool
125 expectedError string
Adam Langleyac61fa32014-06-23 12:03:11 -0700126 // expectedLocalError, if not empty, contains a substring that must be
127 // found in the local error.
128 expectedLocalError string
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400129 // expectedVersion, if non-zero, specifies the TLS version that must be
130 // negotiated.
131 expectedVersion uint16
David Benjamin01fe8202014-09-24 15:21:44 -0400132 // expectedResumeVersion, if non-zero, specifies the TLS version that
133 // must be negotiated on resumption. If zero, expectedVersion is used.
134 expectedResumeVersion uint16
David Benjamina08e49d2014-08-24 01:46:07 -0400135 // expectChannelID controls whether the connection should have
136 // negotiated a Channel ID with channelIDKey.
137 expectChannelID bool
David Benjaminae2888f2014-09-06 12:58:58 -0400138 // expectedNextProto controls whether the connection should
139 // negotiate a next protocol via NPN or ALPN.
140 expectedNextProto string
David Benjaminfc7b0862014-09-06 13:21:53 -0400141 // expectedNextProtoType, if non-zero, is the expected next
142 // protocol negotiation mechanism.
143 expectedNextProtoType int
David Benjaminca6c8262014-11-15 19:06:08 -0500144 // expectedSRTPProtectionProfile is the DTLS-SRTP profile that
145 // should be negotiated. If zero, none should be negotiated.
146 expectedSRTPProtectionProfile uint16
Adam Langley80842bd2014-06-20 12:00:00 -0700147 // messageLen is the length, in bytes, of the test message that will be
148 // sent.
149 messageLen int
David Benjamin025b3d32014-07-01 19:53:04 -0400150 // certFile is the path to the certificate to use for the server.
151 certFile string
152 // keyFile is the path to the private key to use for the server.
153 keyFile string
David Benjamin1d5c83e2014-07-22 19:20:02 -0400154 // resumeSession controls whether a second connection should be tested
David Benjamin01fe8202014-09-24 15:21:44 -0400155 // which attempts to resume the first session.
David Benjamin1d5c83e2014-07-22 19:20:02 -0400156 resumeSession bool
David Benjamin01fe8202014-09-24 15:21:44 -0400157 // resumeConfig, if not nil, points to a Config to be used on
David Benjaminfe8eb9a2014-11-17 03:19:02 -0500158 // resumption. Unless newSessionsOnResume is set,
159 // SessionTicketKey, ServerSessionCache, and
160 // ClientSessionCache are copied from the initial connection's
161 // config. If nil, the initial connection's config is used.
David Benjamin01fe8202014-09-24 15:21:44 -0400162 resumeConfig *Config
David Benjaminfe8eb9a2014-11-17 03:19:02 -0500163 // newSessionsOnResume, if true, will cause resumeConfig to
164 // use a different session resumption context.
165 newSessionsOnResume bool
David Benjamin98e882e2014-08-08 13:24:34 -0400166 // sendPrefix sends a prefix on the socket before actually performing a
167 // handshake.
168 sendPrefix string
David Benjamine58c4f52014-08-24 03:47:07 -0400169 // shimWritesFirst controls whether the shim sends an initial "hello"
170 // message before doing a roundtrip with the runner.
171 shimWritesFirst bool
Adam Langleycf2d4f42014-10-28 19:06:14 -0700172 // renegotiate indicates the the connection should be renegotiated
173 // during the exchange.
174 renegotiate bool
175 // renegotiateCiphers is a list of ciphersuite ids that will be
176 // switched in just before renegotiation.
177 renegotiateCiphers []uint16
David Benjamin5e961c12014-11-07 01:48:35 -0500178 // replayWrites, if true, configures the underlying transport
179 // to replay every write it makes in DTLS tests.
180 replayWrites bool
David Benjamin325b5c32014-07-01 19:40:31 -0400181 // flags, if not empty, contains a list of command-line flags that will
182 // be passed to the shim program.
183 flags []string
Adam Langley95c29f32014-06-20 12:00:00 -0700184}
185
David Benjamin025b3d32014-07-01 19:53:04 -0400186var testCases = []testCase{
Adam Langley95c29f32014-06-20 12:00:00 -0700187 {
188 name: "BadRSASignature",
189 config: Config{
190 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
191 Bugs: ProtocolBugs{
192 InvalidSKXSignature: true,
193 },
194 },
195 shouldFail: true,
196 expectedError: ":BAD_SIGNATURE:",
197 },
198 {
199 name: "BadECDSASignature",
200 config: Config{
201 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
202 Bugs: ProtocolBugs{
203 InvalidSKXSignature: true,
204 },
205 Certificates: []Certificate{getECDSACertificate()},
206 },
207 shouldFail: true,
208 expectedError: ":BAD_SIGNATURE:",
209 },
210 {
211 name: "BadECDSACurve",
212 config: Config{
213 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
214 Bugs: ProtocolBugs{
215 InvalidSKXCurve: true,
216 },
217 Certificates: []Certificate{getECDSACertificate()},
218 },
219 shouldFail: true,
220 expectedError: ":WRONG_CURVE:",
221 },
Adam Langleyac61fa32014-06-23 12:03:11 -0700222 {
David Benjamina8e3e0e2014-08-06 22:11:10 -0400223 testType: serverTest,
224 name: "BadRSAVersion",
225 config: Config{
226 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
227 Bugs: ProtocolBugs{
228 RsaClientKeyExchangeVersion: VersionTLS11,
229 },
230 },
231 shouldFail: true,
232 expectedError: ":DECRYPTION_FAILED_OR_BAD_RECORD_MAC:",
233 },
234 {
David Benjamin325b5c32014-07-01 19:40:31 -0400235 name: "NoFallbackSCSV",
Adam Langleyac61fa32014-06-23 12:03:11 -0700236 config: Config{
237 Bugs: ProtocolBugs{
238 FailIfNotFallbackSCSV: true,
239 },
240 },
241 shouldFail: true,
242 expectedLocalError: "no fallback SCSV found",
243 },
David Benjamin325b5c32014-07-01 19:40:31 -0400244 {
David Benjamin2a0c4962014-08-22 23:46:35 -0400245 name: "SendFallbackSCSV",
David Benjamin325b5c32014-07-01 19:40:31 -0400246 config: Config{
247 Bugs: ProtocolBugs{
248 FailIfNotFallbackSCSV: true,
249 },
250 },
251 flags: []string{"-fallback-scsv"},
252 },
David Benjamin197b3ab2014-07-02 18:37:33 -0400253 {
David Benjamin7b030512014-07-08 17:30:11 -0400254 name: "ClientCertificateTypes",
255 config: Config{
256 ClientAuth: RequestClientCert,
257 ClientCertificateTypes: []byte{
258 CertTypeDSSSign,
259 CertTypeRSASign,
260 CertTypeECDSASign,
261 },
262 },
David Benjamin2561dc32014-08-24 01:25:27 -0400263 flags: []string{
264 "-expect-certificate-types",
265 base64.StdEncoding.EncodeToString([]byte{
266 CertTypeDSSSign,
267 CertTypeRSASign,
268 CertTypeECDSASign,
269 }),
270 },
David Benjamin7b030512014-07-08 17:30:11 -0400271 },
David Benjamin636293b2014-07-08 17:59:18 -0400272 {
273 name: "NoClientCertificate",
274 config: Config{
275 ClientAuth: RequireAnyClientCert,
276 },
277 shouldFail: true,
278 expectedLocalError: "client didn't provide a certificate",
279 },
David Benjamin1c375dd2014-07-12 00:48:23 -0400280 {
281 name: "UnauthenticatedECDH",
282 config: Config{
283 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
284 Bugs: ProtocolBugs{
285 UnauthenticatedECDH: true,
286 },
287 },
288 shouldFail: true,
David Benjamine8f3d662014-07-12 01:10:19 -0400289 expectedError: ":UNEXPECTED_MESSAGE:",
David Benjamin1c375dd2014-07-12 00:48:23 -0400290 },
David Benjamin9c651c92014-07-12 13:27:45 -0400291 {
292 name: "SkipServerKeyExchange",
293 config: Config{
294 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
295 Bugs: ProtocolBugs{
296 SkipServerKeyExchange: true,
297 },
298 },
299 shouldFail: true,
300 expectedError: ":UNEXPECTED_MESSAGE:",
301 },
David Benjamin1f5f62b2014-07-12 16:18:02 -0400302 {
David Benjamina0e52232014-07-19 17:39:58 -0400303 name: "SkipChangeCipherSpec-Client",
304 config: Config{
305 Bugs: ProtocolBugs{
306 SkipChangeCipherSpec: true,
307 },
308 },
309 shouldFail: true,
David Benjamin86271ee2014-07-21 16:14:03 -0400310 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
David Benjamina0e52232014-07-19 17:39:58 -0400311 },
312 {
313 testType: serverTest,
314 name: "SkipChangeCipherSpec-Server",
315 config: Config{
316 Bugs: ProtocolBugs{
317 SkipChangeCipherSpec: true,
318 },
319 },
320 shouldFail: true,
David Benjamin86271ee2014-07-21 16:14:03 -0400321 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
David Benjamina0e52232014-07-19 17:39:58 -0400322 },
David Benjamin42be6452014-07-21 14:50:23 -0400323 {
324 testType: serverTest,
325 name: "SkipChangeCipherSpec-Server-NPN",
326 config: Config{
327 NextProtos: []string{"bar"},
328 Bugs: ProtocolBugs{
329 SkipChangeCipherSpec: true,
330 },
331 },
332 flags: []string{
333 "-advertise-npn", "\x03foo\x03bar\x03baz",
334 },
335 shouldFail: true,
David Benjamin86271ee2014-07-21 16:14:03 -0400336 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
337 },
338 {
339 name: "FragmentAcrossChangeCipherSpec-Client",
340 config: Config{
341 Bugs: ProtocolBugs{
342 FragmentAcrossChangeCipherSpec: true,
343 },
344 },
345 shouldFail: true,
346 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
347 },
348 {
349 testType: serverTest,
350 name: "FragmentAcrossChangeCipherSpec-Server",
351 config: Config{
352 Bugs: ProtocolBugs{
353 FragmentAcrossChangeCipherSpec: true,
354 },
355 },
356 shouldFail: true,
357 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
358 },
359 {
360 testType: serverTest,
361 name: "FragmentAcrossChangeCipherSpec-Server-NPN",
362 config: Config{
363 NextProtos: []string{"bar"},
364 Bugs: ProtocolBugs{
365 FragmentAcrossChangeCipherSpec: true,
366 },
367 },
368 flags: []string{
369 "-advertise-npn", "\x03foo\x03bar\x03baz",
370 },
371 shouldFail: true,
372 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
David Benjamin42be6452014-07-21 14:50:23 -0400373 },
David Benjaminf3ec83d2014-07-21 22:42:34 -0400374 {
375 testType: serverTest,
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400376 name: "FragmentAlert",
377 config: Config{
378 Bugs: ProtocolBugs{
David Benjaminca6c8262014-11-15 19:06:08 -0500379 FragmentAlert: true,
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400380 SendSpuriousAlert: true,
381 },
382 },
383 shouldFail: true,
384 expectedError: ":BAD_ALERT:",
385 },
386 {
387 testType: serverTest,
David Benjaminf3ec83d2014-07-21 22:42:34 -0400388 name: "EarlyChangeCipherSpec-server-1",
389 config: Config{
390 Bugs: ProtocolBugs{
391 EarlyChangeCipherSpec: 1,
392 },
393 },
394 shouldFail: true,
395 expectedError: ":CCS_RECEIVED_EARLY:",
396 },
397 {
398 testType: serverTest,
399 name: "EarlyChangeCipherSpec-server-2",
400 config: Config{
401 Bugs: ProtocolBugs{
402 EarlyChangeCipherSpec: 2,
403 },
404 },
405 shouldFail: true,
406 expectedError: ":CCS_RECEIVED_EARLY:",
407 },
David Benjamind23f4122014-07-23 15:09:48 -0400408 {
David Benjamind23f4122014-07-23 15:09:48 -0400409 name: "SkipNewSessionTicket",
410 config: Config{
411 Bugs: ProtocolBugs{
412 SkipNewSessionTicket: true,
413 },
414 },
415 shouldFail: true,
416 expectedError: ":CCS_RECEIVED_EARLY:",
417 },
David Benjamin7e3305e2014-07-28 14:52:32 -0400418 {
David Benjamind86c7672014-08-02 04:07:12 -0400419 testType: serverTest,
David Benjaminbef270a2014-08-02 04:22:02 -0400420 name: "FallbackSCSV",
421 config: Config{
422 MaxVersion: VersionTLS11,
423 Bugs: ProtocolBugs{
424 SendFallbackSCSV: true,
425 },
426 },
427 shouldFail: true,
428 expectedError: ":INAPPROPRIATE_FALLBACK:",
429 },
430 {
431 testType: serverTest,
432 name: "FallbackSCSV-VersionMatch",
433 config: Config{
434 Bugs: ProtocolBugs{
435 SendFallbackSCSV: true,
436 },
437 },
438 },
David Benjamin98214542014-08-07 18:02:39 -0400439 {
440 testType: serverTest,
441 name: "FragmentedClientVersion",
442 config: Config{
443 Bugs: ProtocolBugs{
444 MaxHandshakeRecordLength: 1,
445 FragmentClientVersion: true,
446 },
447 },
448 shouldFail: true,
449 expectedError: ":RECORD_TOO_SMALL:",
450 },
David Benjamin98e882e2014-08-08 13:24:34 -0400451 {
452 testType: serverTest,
453 name: "MinorVersionTolerance",
454 config: Config{
455 Bugs: ProtocolBugs{
456 SendClientVersion: 0x03ff,
457 },
458 },
459 expectedVersion: VersionTLS12,
460 },
461 {
462 testType: serverTest,
463 name: "MajorVersionTolerance",
464 config: Config{
465 Bugs: ProtocolBugs{
466 SendClientVersion: 0x0400,
467 },
468 },
469 expectedVersion: VersionTLS12,
470 },
471 {
472 testType: serverTest,
473 name: "VersionTooLow",
474 config: Config{
475 Bugs: ProtocolBugs{
476 SendClientVersion: 0x0200,
477 },
478 },
479 shouldFail: true,
480 expectedError: ":UNSUPPORTED_PROTOCOL:",
481 },
482 {
483 testType: serverTest,
484 name: "HttpGET",
485 sendPrefix: "GET / HTTP/1.0\n",
486 shouldFail: true,
487 expectedError: ":HTTP_REQUEST:",
488 },
489 {
490 testType: serverTest,
491 name: "HttpPOST",
492 sendPrefix: "POST / HTTP/1.0\n",
493 shouldFail: true,
494 expectedError: ":HTTP_REQUEST:",
495 },
496 {
497 testType: serverTest,
498 name: "HttpHEAD",
499 sendPrefix: "HEAD / HTTP/1.0\n",
500 shouldFail: true,
501 expectedError: ":HTTP_REQUEST:",
502 },
503 {
504 testType: serverTest,
505 name: "HttpPUT",
506 sendPrefix: "PUT / HTTP/1.0\n",
507 shouldFail: true,
508 expectedError: ":HTTP_REQUEST:",
509 },
510 {
511 testType: serverTest,
512 name: "HttpCONNECT",
513 sendPrefix: "CONNECT www.google.com:443 HTTP/1.0\n",
514 shouldFail: true,
515 expectedError: ":HTTPS_PROXY_REQUEST:",
516 },
David Benjamin39ebf532014-08-31 02:23:49 -0400517 {
David Benjaminf080ecd2014-12-11 18:48:59 -0500518 testType: serverTest,
519 name: "Garbage",
520 sendPrefix: "blah",
521 shouldFail: true,
522 expectedError: ":UNKNOWN_PROTOCOL:",
523 },
524 {
David Benjamin39ebf532014-08-31 02:23:49 -0400525 name: "SkipCipherVersionCheck",
526 config: Config{
527 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
528 MaxVersion: VersionTLS11,
529 Bugs: ProtocolBugs{
530 SkipCipherVersionCheck: true,
531 },
532 },
533 shouldFail: true,
534 expectedError: ":WRONG_CIPHER_RETURNED:",
535 },
David Benjamin9114fae2014-11-08 11:41:14 -0500536 {
537 name: "RSAServerKeyExchange",
538 config: Config{
539 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
540 Bugs: ProtocolBugs{
541 RSAServerKeyExchange: true,
542 },
543 },
544 shouldFail: true,
545 expectedError: ":UNEXPECTED_MESSAGE:",
546 },
David Benjamin128dbc32014-12-01 01:27:42 -0500547 {
548 name: "DisableEverything",
549 flags: []string{"-no-tls12", "-no-tls11", "-no-tls1", "-no-ssl3"},
550 shouldFail: true,
551 expectedError: ":WRONG_SSL_VERSION:",
552 },
553 {
554 protocol: dtls,
555 name: "DisableEverything-DTLS",
556 flags: []string{"-no-tls12", "-no-tls1"},
557 shouldFail: true,
558 expectedError: ":WRONG_SSL_VERSION:",
559 },
Adam Langley95c29f32014-06-20 12:00:00 -0700560}
561
David Benjamin01fe8202014-09-24 15:21:44 -0400562func doExchange(test *testCase, config *Config, conn net.Conn, messageLen int, isResume bool) error {
David Benjamin65ea8ff2014-11-23 03:01:00 -0500563 var connDebug *recordingConn
564 if *flagDebug {
565 connDebug = &recordingConn{Conn: conn}
566 conn = connDebug
567 defer func() {
568 connDebug.WriteTo(os.Stdout)
569 }()
570 }
571
David Benjamin6fd297b2014-08-11 18:43:38 -0400572 if test.protocol == dtls {
573 conn = newPacketAdaptor(conn)
David Benjamin5e961c12014-11-07 01:48:35 -0500574 if test.replayWrites {
575 conn = newReplayAdaptor(conn)
576 }
David Benjamin6fd297b2014-08-11 18:43:38 -0400577 }
578
579 if test.sendPrefix != "" {
580 if _, err := conn.Write([]byte(test.sendPrefix)); err != nil {
581 return err
582 }
David Benjamin98e882e2014-08-08 13:24:34 -0400583 }
584
David Benjamin1d5c83e2014-07-22 19:20:02 -0400585 var tlsConn *Conn
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400586 if test.testType == clientTest {
David Benjamin6fd297b2014-08-11 18:43:38 -0400587 if test.protocol == dtls {
588 tlsConn = DTLSServer(conn, config)
589 } else {
590 tlsConn = Server(conn, config)
591 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400592 } else {
593 config.InsecureSkipVerify = true
David Benjamin6fd297b2014-08-11 18:43:38 -0400594 if test.protocol == dtls {
595 tlsConn = DTLSClient(conn, config)
596 } else {
597 tlsConn = Client(conn, config)
598 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400599 }
600
Adam Langley95c29f32014-06-20 12:00:00 -0700601 if err := tlsConn.Handshake(); err != nil {
602 return err
603 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700604
David Benjamin01fe8202014-09-24 15:21:44 -0400605 // TODO(davidben): move all per-connection expectations into a dedicated
606 // expectations struct that can be specified separately for the two
607 // legs.
608 expectedVersion := test.expectedVersion
609 if isResume && test.expectedResumeVersion != 0 {
610 expectedVersion = test.expectedResumeVersion
611 }
612 if vers := tlsConn.ConnectionState().Version; expectedVersion != 0 && vers != expectedVersion {
613 return fmt.Errorf("got version %x, expected %x", vers, expectedVersion)
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400614 }
615
David Benjamina08e49d2014-08-24 01:46:07 -0400616 if test.expectChannelID {
617 channelID := tlsConn.ConnectionState().ChannelID
618 if channelID == nil {
619 return fmt.Errorf("no channel ID negotiated")
620 }
621 if channelID.Curve != channelIDKey.Curve ||
622 channelIDKey.X.Cmp(channelIDKey.X) != 0 ||
623 channelIDKey.Y.Cmp(channelIDKey.Y) != 0 {
624 return fmt.Errorf("incorrect channel ID")
625 }
626 }
627
David Benjaminae2888f2014-09-06 12:58:58 -0400628 if expected := test.expectedNextProto; expected != "" {
629 if actual := tlsConn.ConnectionState().NegotiatedProtocol; actual != expected {
630 return fmt.Errorf("next proto mismatch: got %s, wanted %s", actual, expected)
631 }
632 }
633
David Benjaminfc7b0862014-09-06 13:21:53 -0400634 if test.expectedNextProtoType != 0 {
635 if (test.expectedNextProtoType == alpn) != tlsConn.ConnectionState().NegotiatedProtocolFromALPN {
636 return fmt.Errorf("next proto type mismatch")
637 }
638 }
639
David Benjaminca6c8262014-11-15 19:06:08 -0500640 if p := tlsConn.ConnectionState().SRTPProtectionProfile; p != test.expectedSRTPProtectionProfile {
641 return fmt.Errorf("SRTP profile mismatch: got %d, wanted %d", p, test.expectedSRTPProtectionProfile)
642 }
643
David Benjamine58c4f52014-08-24 03:47:07 -0400644 if test.shimWritesFirst {
645 var buf [5]byte
646 _, err := io.ReadFull(tlsConn, buf[:])
647 if err != nil {
648 return err
649 }
650 if string(buf[:]) != "hello" {
651 return fmt.Errorf("bad initial message")
652 }
653 }
654
Adam Langleycf2d4f42014-10-28 19:06:14 -0700655 if test.renegotiate {
656 if test.renegotiateCiphers != nil {
657 config.CipherSuites = test.renegotiateCiphers
658 }
659 if err := tlsConn.Renegotiate(); err != nil {
660 return err
661 }
662 } else if test.renegotiateCiphers != nil {
663 panic("renegotiateCiphers without renegotiate")
664 }
665
Kenny Root7fdeaf12014-08-05 15:23:37 -0700666 if messageLen < 0 {
David Benjamin6fd297b2014-08-11 18:43:38 -0400667 if test.protocol == dtls {
668 return fmt.Errorf("messageLen < 0 not supported for DTLS tests")
669 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700670 // Read until EOF.
671 _, err := io.Copy(ioutil.Discard, tlsConn)
672 return err
673 }
674
Adam Langley80842bd2014-06-20 12:00:00 -0700675 if messageLen == 0 {
676 messageLen = 32
677 }
678 testMessage := make([]byte, messageLen)
679 for i := range testMessage {
680 testMessage[i] = 0x42
681 }
Adam Langley95c29f32014-06-20 12:00:00 -0700682 tlsConn.Write(testMessage)
683
684 buf := make([]byte, len(testMessage))
David Benjamin6fd297b2014-08-11 18:43:38 -0400685 if test.protocol == dtls {
686 bufTmp := make([]byte, len(buf)+1)
687 n, err := tlsConn.Read(bufTmp)
688 if err != nil {
689 return err
690 }
691 if n != len(buf) {
692 return fmt.Errorf("bad reply; length mismatch (%d vs %d)", n, len(buf))
693 }
694 copy(buf, bufTmp)
695 } else {
696 _, err := io.ReadFull(tlsConn, buf)
697 if err != nil {
698 return err
699 }
Adam Langley95c29f32014-06-20 12:00:00 -0700700 }
701
702 for i, v := range buf {
703 if v != testMessage[i]^0xff {
704 return fmt.Errorf("bad reply contents at byte %d", i)
705 }
706 }
707
708 return nil
709}
710
David Benjamin325b5c32014-07-01 19:40:31 -0400711func valgrindOf(dbAttach bool, path string, args ...string) *exec.Cmd {
712 valgrindArgs := []string{"--error-exitcode=99", "--track-origins=yes", "--leak-check=full"}
Adam Langley95c29f32014-06-20 12:00:00 -0700713 if dbAttach {
David Benjamin325b5c32014-07-01 19:40:31 -0400714 valgrindArgs = append(valgrindArgs, "--db-attach=yes", "--db-command=xterm -e gdb -nw %f %p")
Adam Langley95c29f32014-06-20 12:00:00 -0700715 }
David Benjamin325b5c32014-07-01 19:40:31 -0400716 valgrindArgs = append(valgrindArgs, path)
717 valgrindArgs = append(valgrindArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -0700718
David Benjamin325b5c32014-07-01 19:40:31 -0400719 return exec.Command("valgrind", valgrindArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -0700720}
721
David Benjamin325b5c32014-07-01 19:40:31 -0400722func gdbOf(path string, args ...string) *exec.Cmd {
723 xtermArgs := []string{"-e", "gdb", "--args"}
724 xtermArgs = append(xtermArgs, path)
725 xtermArgs = append(xtermArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -0700726
David Benjamin325b5c32014-07-01 19:40:31 -0400727 return exec.Command("xterm", xtermArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -0700728}
729
David Benjamin1d5c83e2014-07-22 19:20:02 -0400730func openSocketPair() (shimEnd *os.File, conn net.Conn) {
Adam Langley95c29f32014-06-20 12:00:00 -0700731 socks, err := syscall.Socketpair(syscall.AF_UNIX, syscall.SOCK_STREAM, 0)
732 if err != nil {
733 panic(err)
734 }
735
736 syscall.CloseOnExec(socks[0])
737 syscall.CloseOnExec(socks[1])
David Benjamin1d5c83e2014-07-22 19:20:02 -0400738 shimEnd = os.NewFile(uintptr(socks[0]), "shim end")
Adam Langley95c29f32014-06-20 12:00:00 -0700739 connFile := os.NewFile(uintptr(socks[1]), "our end")
David Benjamin1d5c83e2014-07-22 19:20:02 -0400740 conn, err = net.FileConn(connFile)
741 if err != nil {
742 panic(err)
743 }
Adam Langley95c29f32014-06-20 12:00:00 -0700744 connFile.Close()
745 if err != nil {
746 panic(err)
747 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400748 return shimEnd, conn
749}
750
Adam Langley69a01602014-11-17 17:26:55 -0800751type moreMallocsError struct{}
752
753func (moreMallocsError) Error() string {
754 return "child process did not exhaust all allocation calls"
755}
756
757var errMoreMallocs = moreMallocsError{}
758
759func runTest(test *testCase, buildDir string, mallocNumToFail int64) error {
Adam Langley38311732014-10-16 19:04:35 -0700760 if !test.shouldFail && (len(test.expectedError) > 0 || len(test.expectedLocalError) > 0) {
761 panic("Error expected without shouldFail in " + test.name)
762 }
763
David Benjamin1d5c83e2014-07-22 19:20:02 -0400764 shimEnd, conn := openSocketPair()
765 shimEndResume, connResume := openSocketPair()
Adam Langley95c29f32014-06-20 12:00:00 -0700766
David Benjamin884fdf12014-08-02 15:28:23 -0400767 shim_path := path.Join(buildDir, "ssl/test/bssl_shim")
David Benjamin5a593af2014-08-11 19:51:50 -0400768 var flags []string
David Benjamin1d5c83e2014-07-22 19:20:02 -0400769 if test.testType == serverTest {
David Benjamin5a593af2014-08-11 19:51:50 -0400770 flags = append(flags, "-server")
771
David Benjamin025b3d32014-07-01 19:53:04 -0400772 flags = append(flags, "-key-file")
773 if test.keyFile == "" {
774 flags = append(flags, rsaKeyFile)
775 } else {
776 flags = append(flags, test.keyFile)
777 }
778
779 flags = append(flags, "-cert-file")
780 if test.certFile == "" {
781 flags = append(flags, rsaCertificateFile)
782 } else {
783 flags = append(flags, test.certFile)
784 }
785 }
David Benjamin5a593af2014-08-11 19:51:50 -0400786
David Benjamin6fd297b2014-08-11 18:43:38 -0400787 if test.protocol == dtls {
788 flags = append(flags, "-dtls")
789 }
790
David Benjamin5a593af2014-08-11 19:51:50 -0400791 if test.resumeSession {
792 flags = append(flags, "-resume")
793 }
794
David Benjamine58c4f52014-08-24 03:47:07 -0400795 if test.shimWritesFirst {
796 flags = append(flags, "-shim-writes-first")
797 }
798
David Benjamin025b3d32014-07-01 19:53:04 -0400799 flags = append(flags, test.flags...)
800
801 var shim *exec.Cmd
802 if *useValgrind {
803 shim = valgrindOf(false, shim_path, flags...)
Adam Langley75712922014-10-10 16:23:43 -0700804 } else if *useGDB {
805 shim = gdbOf(shim_path, flags...)
David Benjamin025b3d32014-07-01 19:53:04 -0400806 } else {
807 shim = exec.Command(shim_path, flags...)
808 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400809 shim.ExtraFiles = []*os.File{shimEnd, shimEndResume}
David Benjamin025b3d32014-07-01 19:53:04 -0400810 shim.Stdin = os.Stdin
811 var stdoutBuf, stderrBuf bytes.Buffer
812 shim.Stdout = &stdoutBuf
813 shim.Stderr = &stderrBuf
Adam Langley69a01602014-11-17 17:26:55 -0800814 if mallocNumToFail >= 0 {
815 shim.Env = []string{"MALLOC_NUMBER_TO_FAIL=" + strconv.FormatInt(mallocNumToFail, 10)}
816 if *mallocTestDebug {
817 shim.Env = append(shim.Env, "MALLOC_ABORT_ON_FAIL=1")
818 }
819 shim.Env = append(shim.Env, "_MALLOC_CHECK=1")
820 }
David Benjamin025b3d32014-07-01 19:53:04 -0400821
822 if err := shim.Start(); err != nil {
Adam Langley95c29f32014-06-20 12:00:00 -0700823 panic(err)
824 }
David Benjamin025b3d32014-07-01 19:53:04 -0400825 shimEnd.Close()
David Benjamin1d5c83e2014-07-22 19:20:02 -0400826 shimEndResume.Close()
Adam Langley95c29f32014-06-20 12:00:00 -0700827
828 config := test.config
David Benjamin1d5c83e2014-07-22 19:20:02 -0400829 config.ClientSessionCache = NewLRUClientSessionCache(1)
David Benjaminfe8eb9a2014-11-17 03:19:02 -0500830 config.ServerSessionCache = NewLRUServerSessionCache(1)
David Benjamin025b3d32014-07-01 19:53:04 -0400831 if test.testType == clientTest {
832 if len(config.Certificates) == 0 {
833 config.Certificates = []Certificate{getRSACertificate()}
834 }
David Benjamin025b3d32014-07-01 19:53:04 -0400835 }
Adam Langley95c29f32014-06-20 12:00:00 -0700836
David Benjamin01fe8202014-09-24 15:21:44 -0400837 err := doExchange(test, &config, conn, test.messageLen,
838 false /* not a resumption */)
Adam Langley95c29f32014-06-20 12:00:00 -0700839 conn.Close()
David Benjamin65ea8ff2014-11-23 03:01:00 -0500840
David Benjamin1d5c83e2014-07-22 19:20:02 -0400841 if err == nil && test.resumeSession {
David Benjamin01fe8202014-09-24 15:21:44 -0400842 var resumeConfig Config
843 if test.resumeConfig != nil {
844 resumeConfig = *test.resumeConfig
845 if len(resumeConfig.Certificates) == 0 {
846 resumeConfig.Certificates = []Certificate{getRSACertificate()}
847 }
David Benjaminfe8eb9a2014-11-17 03:19:02 -0500848 if !test.newSessionsOnResume {
849 resumeConfig.SessionTicketKey = config.SessionTicketKey
850 resumeConfig.ClientSessionCache = config.ClientSessionCache
851 resumeConfig.ServerSessionCache = config.ServerSessionCache
852 }
David Benjamin01fe8202014-09-24 15:21:44 -0400853 } else {
854 resumeConfig = config
855 }
856 err = doExchange(test, &resumeConfig, connResume, test.messageLen,
857 true /* resumption */)
David Benjamin1d5c83e2014-07-22 19:20:02 -0400858 }
David Benjamin812152a2014-09-06 12:49:07 -0400859 connResume.Close()
David Benjamin1d5c83e2014-07-22 19:20:02 -0400860
David Benjamin025b3d32014-07-01 19:53:04 -0400861 childErr := shim.Wait()
Adam Langley69a01602014-11-17 17:26:55 -0800862 if exitError, ok := childErr.(*exec.ExitError); ok {
863 if exitError.Sys().(syscall.WaitStatus).ExitStatus() == 88 {
864 return errMoreMallocs
865 }
866 }
Adam Langley95c29f32014-06-20 12:00:00 -0700867
868 stdout := string(stdoutBuf.Bytes())
869 stderr := string(stderrBuf.Bytes())
870 failed := err != nil || childErr != nil
871 correctFailure := len(test.expectedError) == 0 || strings.Contains(stdout, test.expectedError)
Adam Langleyac61fa32014-06-23 12:03:11 -0700872 localError := "none"
873 if err != nil {
874 localError = err.Error()
875 }
876 if len(test.expectedLocalError) != 0 {
877 correctFailure = correctFailure && strings.Contains(localError, test.expectedLocalError)
878 }
Adam Langley95c29f32014-06-20 12:00:00 -0700879
880 if failed != test.shouldFail || failed && !correctFailure {
Adam Langley95c29f32014-06-20 12:00:00 -0700881 childError := "none"
Adam Langley95c29f32014-06-20 12:00:00 -0700882 if childErr != nil {
883 childError = childErr.Error()
884 }
885
886 var msg string
887 switch {
888 case failed && !test.shouldFail:
889 msg = "unexpected failure"
890 case !failed && test.shouldFail:
891 msg = "unexpected success"
892 case failed && !correctFailure:
Adam Langleyac61fa32014-06-23 12:03:11 -0700893 msg = "bad error (wanted '" + test.expectedError + "' / '" + test.expectedLocalError + "')"
Adam Langley95c29f32014-06-20 12:00:00 -0700894 default:
895 panic("internal error")
896 }
897
898 return fmt.Errorf("%s: local error '%s', child error '%s', stdout:\n%s\nstderr:\n%s", msg, localError, childError, string(stdoutBuf.Bytes()), stderr)
899 }
900
901 if !*useValgrind && len(stderr) > 0 {
902 println(stderr)
903 }
904
905 return nil
906}
907
908var tlsVersions = []struct {
909 name string
910 version uint16
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400911 flag string
David Benjamin8b8c0062014-11-23 02:47:52 -0500912 hasDTLS bool
Adam Langley95c29f32014-06-20 12:00:00 -0700913}{
David Benjamin8b8c0062014-11-23 02:47:52 -0500914 {"SSL3", VersionSSL30, "-no-ssl3", false},
915 {"TLS1", VersionTLS10, "-no-tls1", true},
916 {"TLS11", VersionTLS11, "-no-tls11", false},
917 {"TLS12", VersionTLS12, "-no-tls12", true},
Adam Langley95c29f32014-06-20 12:00:00 -0700918}
919
920var testCipherSuites = []struct {
921 name string
922 id uint16
923}{
924 {"3DES-SHA", TLS_RSA_WITH_3DES_EDE_CBC_SHA},
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400925 {"AES128-GCM", TLS_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -0700926 {"AES128-SHA", TLS_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -0400927 {"AES128-SHA256", TLS_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400928 {"AES256-GCM", TLS_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -0700929 {"AES256-SHA", TLS_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -0400930 {"AES256-SHA256", TLS_RSA_WITH_AES_256_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400931 {"DHE-RSA-AES128-GCM", TLS_DHE_RSA_WITH_AES_128_GCM_SHA256},
932 {"DHE-RSA-AES128-SHA", TLS_DHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -0400933 {"DHE-RSA-AES128-SHA256", TLS_DHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400934 {"DHE-RSA-AES256-GCM", TLS_DHE_RSA_WITH_AES_256_GCM_SHA384},
935 {"DHE-RSA-AES256-SHA", TLS_DHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -0400936 {"DHE-RSA-AES256-SHA256", TLS_DHE_RSA_WITH_AES_256_CBC_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -0700937 {"ECDHE-ECDSA-AES128-GCM", TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
938 {"ECDHE-ECDSA-AES128-SHA", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -0400939 {"ECDHE-ECDSA-AES128-SHA256", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256},
940 {"ECDHE-ECDSA-AES256-GCM", TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -0700941 {"ECDHE-ECDSA-AES256-SHA", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -0400942 {"ECDHE-ECDSA-AES256-SHA384", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -0700943 {"ECDHE-ECDSA-RC4-SHA", TLS_ECDHE_ECDSA_WITH_RC4_128_SHA},
David Benjamin2af684f2014-10-27 02:23:15 -0400944 {"ECDHE-PSK-WITH-AES-128-GCM-SHA256", TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -0700945 {"ECDHE-RSA-AES128-GCM", TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -0700946 {"ECDHE-RSA-AES128-SHA", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -0400947 {"ECDHE-RSA-AES128-SHA256", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400948 {"ECDHE-RSA-AES256-GCM", TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -0700949 {"ECDHE-RSA-AES256-SHA", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -0400950 {"ECDHE-RSA-AES256-SHA384", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -0700951 {"ECDHE-RSA-RC4-SHA", TLS_ECDHE_RSA_WITH_RC4_128_SHA},
David Benjamin48cae082014-10-27 01:06:24 -0400952 {"PSK-AES128-CBC-SHA", TLS_PSK_WITH_AES_128_CBC_SHA},
953 {"PSK-AES256-CBC-SHA", TLS_PSK_WITH_AES_256_CBC_SHA},
954 {"PSK-RC4-SHA", TLS_PSK_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -0700955 {"RC4-MD5", TLS_RSA_WITH_RC4_128_MD5},
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400956 {"RC4-SHA", TLS_RSA_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -0700957}
958
David Benjamin8b8c0062014-11-23 02:47:52 -0500959func hasComponent(suiteName, component string) bool {
960 return strings.Contains("-"+suiteName+"-", "-"+component+"-")
961}
962
David Benjaminf7768e42014-08-31 02:06:47 -0400963func isTLS12Only(suiteName string) bool {
David Benjamin8b8c0062014-11-23 02:47:52 -0500964 return hasComponent(suiteName, "GCM") ||
965 hasComponent(suiteName, "SHA256") ||
966 hasComponent(suiteName, "SHA384")
967}
968
969func isDTLSCipher(suiteName string) bool {
970 // TODO(davidben): AES-GCM exists in DTLS 1.2 but is currently
971 // broken because DTLS is not EVP_AEAD-aware.
972 return !hasComponent(suiteName, "RC4") &&
973 !hasComponent(suiteName, "GCM")
David Benjaminf7768e42014-08-31 02:06:47 -0400974}
975
Adam Langley95c29f32014-06-20 12:00:00 -0700976func addCipherSuiteTests() {
977 for _, suite := range testCipherSuites {
David Benjamin48cae082014-10-27 01:06:24 -0400978 const psk = "12345"
979 const pskIdentity = "luggage combo"
980
Adam Langley95c29f32014-06-20 12:00:00 -0700981 var cert Certificate
David Benjamin025b3d32014-07-01 19:53:04 -0400982 var certFile string
983 var keyFile string
David Benjamin8b8c0062014-11-23 02:47:52 -0500984 if hasComponent(suite.name, "ECDSA") {
Adam Langley95c29f32014-06-20 12:00:00 -0700985 cert = getECDSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -0400986 certFile = ecdsaCertificateFile
987 keyFile = ecdsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -0700988 } else {
989 cert = getRSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -0400990 certFile = rsaCertificateFile
991 keyFile = rsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -0700992 }
993
David Benjamin48cae082014-10-27 01:06:24 -0400994 var flags []string
David Benjamin8b8c0062014-11-23 02:47:52 -0500995 if hasComponent(suite.name, "PSK") {
David Benjamin48cae082014-10-27 01:06:24 -0400996 flags = append(flags,
997 "-psk", psk,
998 "-psk-identity", pskIdentity)
999 }
1000
Adam Langley95c29f32014-06-20 12:00:00 -07001001 for _, ver := range tlsVersions {
David Benjaminf7768e42014-08-31 02:06:47 -04001002 if ver.version < VersionTLS12 && isTLS12Only(suite.name) {
Adam Langley95c29f32014-06-20 12:00:00 -07001003 continue
1004 }
1005
David Benjamin025b3d32014-07-01 19:53:04 -04001006 testCases = append(testCases, testCase{
1007 testType: clientTest,
1008 name: ver.name + "-" + suite.name + "-client",
Adam Langley95c29f32014-06-20 12:00:00 -07001009 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001010 MinVersion: ver.version,
1011 MaxVersion: ver.version,
1012 CipherSuites: []uint16{suite.id},
1013 Certificates: []Certificate{cert},
1014 PreSharedKey: []byte(psk),
1015 PreSharedKeyIdentity: pskIdentity,
Adam Langley95c29f32014-06-20 12:00:00 -07001016 },
David Benjamin48cae082014-10-27 01:06:24 -04001017 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001018 resumeSession: true,
Adam Langley95c29f32014-06-20 12:00:00 -07001019 })
David Benjamin025b3d32014-07-01 19:53:04 -04001020
David Benjamin76d8abe2014-08-14 16:25:34 -04001021 testCases = append(testCases, testCase{
1022 testType: serverTest,
1023 name: ver.name + "-" + suite.name + "-server",
1024 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001025 MinVersion: ver.version,
1026 MaxVersion: ver.version,
1027 CipherSuites: []uint16{suite.id},
1028 Certificates: []Certificate{cert},
1029 PreSharedKey: []byte(psk),
1030 PreSharedKeyIdentity: pskIdentity,
David Benjamin76d8abe2014-08-14 16:25:34 -04001031 },
1032 certFile: certFile,
1033 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001034 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001035 resumeSession: true,
David Benjamin76d8abe2014-08-14 16:25:34 -04001036 })
David Benjamin6fd297b2014-08-11 18:43:38 -04001037
David Benjamin8b8c0062014-11-23 02:47:52 -05001038 if ver.hasDTLS && isDTLSCipher(suite.name) {
David Benjamin6fd297b2014-08-11 18:43:38 -04001039 testCases = append(testCases, testCase{
1040 testType: clientTest,
1041 protocol: dtls,
1042 name: "D" + ver.name + "-" + suite.name + "-client",
1043 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001044 MinVersion: ver.version,
1045 MaxVersion: ver.version,
1046 CipherSuites: []uint16{suite.id},
1047 Certificates: []Certificate{cert},
1048 PreSharedKey: []byte(psk),
1049 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001050 },
David Benjamin48cae082014-10-27 01:06:24 -04001051 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001052 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001053 })
1054 testCases = append(testCases, testCase{
1055 testType: serverTest,
1056 protocol: dtls,
1057 name: "D" + ver.name + "-" + suite.name + "-server",
1058 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001059 MinVersion: ver.version,
1060 MaxVersion: ver.version,
1061 CipherSuites: []uint16{suite.id},
1062 Certificates: []Certificate{cert},
1063 PreSharedKey: []byte(psk),
1064 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001065 },
1066 certFile: certFile,
1067 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001068 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001069 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001070 })
1071 }
Adam Langley95c29f32014-06-20 12:00:00 -07001072 }
1073 }
1074}
1075
1076func addBadECDSASignatureTests() {
1077 for badR := BadValue(1); badR < NumBadValues; badR++ {
1078 for badS := BadValue(1); badS < NumBadValues; badS++ {
David Benjamin025b3d32014-07-01 19:53:04 -04001079 testCases = append(testCases, testCase{
Adam Langley95c29f32014-06-20 12:00:00 -07001080 name: fmt.Sprintf("BadECDSA-%d-%d", badR, badS),
1081 config: Config{
1082 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
1083 Certificates: []Certificate{getECDSACertificate()},
1084 Bugs: ProtocolBugs{
1085 BadECDSAR: badR,
1086 BadECDSAS: badS,
1087 },
1088 },
1089 shouldFail: true,
1090 expectedError: "SIGNATURE",
1091 })
1092 }
1093 }
1094}
1095
Adam Langley80842bd2014-06-20 12:00:00 -07001096func addCBCPaddingTests() {
David Benjamin025b3d32014-07-01 19:53:04 -04001097 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001098 name: "MaxCBCPadding",
1099 config: Config{
1100 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1101 Bugs: ProtocolBugs{
1102 MaxPadding: true,
1103 },
1104 },
1105 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1106 })
David Benjamin025b3d32014-07-01 19:53:04 -04001107 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001108 name: "BadCBCPadding",
1109 config: Config{
1110 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1111 Bugs: ProtocolBugs{
1112 PaddingFirstByteBad: true,
1113 },
1114 },
1115 shouldFail: true,
1116 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1117 })
1118 // OpenSSL previously had an issue where the first byte of padding in
1119 // 255 bytes of padding wasn't checked.
David Benjamin025b3d32014-07-01 19:53:04 -04001120 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001121 name: "BadCBCPadding255",
1122 config: Config{
1123 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1124 Bugs: ProtocolBugs{
1125 MaxPadding: true,
1126 PaddingFirstByteBadIf255: true,
1127 },
1128 },
1129 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1130 shouldFail: true,
1131 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1132 })
1133}
1134
Kenny Root7fdeaf12014-08-05 15:23:37 -07001135func addCBCSplittingTests() {
1136 testCases = append(testCases, testCase{
1137 name: "CBCRecordSplitting",
1138 config: Config{
1139 MaxVersion: VersionTLS10,
1140 MinVersion: VersionTLS10,
1141 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1142 },
1143 messageLen: -1, // read until EOF
1144 flags: []string{
1145 "-async",
1146 "-write-different-record-sizes",
1147 "-cbc-record-splitting",
1148 },
David Benjamina8e3e0e2014-08-06 22:11:10 -04001149 })
1150 testCases = append(testCases, testCase{
Kenny Root7fdeaf12014-08-05 15:23:37 -07001151 name: "CBCRecordSplittingPartialWrite",
1152 config: Config{
1153 MaxVersion: VersionTLS10,
1154 MinVersion: VersionTLS10,
1155 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1156 },
1157 messageLen: -1, // read until EOF
1158 flags: []string{
1159 "-async",
1160 "-write-different-record-sizes",
1161 "-cbc-record-splitting",
1162 "-partial-write",
1163 },
1164 })
1165}
1166
David Benjamin636293b2014-07-08 17:59:18 -04001167func addClientAuthTests() {
David Benjamin407a10c2014-07-16 12:58:59 -04001168 // Add a dummy cert pool to stress certificate authority parsing.
1169 // TODO(davidben): Add tests that those values parse out correctly.
1170 certPool := x509.NewCertPool()
1171 cert, err := x509.ParseCertificate(rsaCertificate.Certificate[0])
1172 if err != nil {
1173 panic(err)
1174 }
1175 certPool.AddCert(cert)
1176
David Benjamin636293b2014-07-08 17:59:18 -04001177 for _, ver := range tlsVersions {
David Benjamin636293b2014-07-08 17:59:18 -04001178 testCases = append(testCases, testCase{
1179 testType: clientTest,
David Benjamin67666e72014-07-12 15:47:52 -04001180 name: ver.name + "-Client-ClientAuth-RSA",
David Benjamin636293b2014-07-08 17:59:18 -04001181 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001182 MinVersion: ver.version,
1183 MaxVersion: ver.version,
1184 ClientAuth: RequireAnyClientCert,
1185 ClientCAs: certPool,
David Benjamin636293b2014-07-08 17:59:18 -04001186 },
1187 flags: []string{
1188 "-cert-file", rsaCertificateFile,
1189 "-key-file", rsaKeyFile,
1190 },
1191 })
1192 testCases = append(testCases, testCase{
David Benjamin67666e72014-07-12 15:47:52 -04001193 testType: serverTest,
1194 name: ver.name + "-Server-ClientAuth-RSA",
1195 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001196 MinVersion: ver.version,
1197 MaxVersion: ver.version,
David Benjamin67666e72014-07-12 15:47:52 -04001198 Certificates: []Certificate{rsaCertificate},
1199 },
1200 flags: []string{"-require-any-client-certificate"},
1201 })
David Benjamine098ec22014-08-27 23:13:20 -04001202 if ver.version != VersionSSL30 {
1203 testCases = append(testCases, testCase{
1204 testType: serverTest,
1205 name: ver.name + "-Server-ClientAuth-ECDSA",
1206 config: Config{
1207 MinVersion: ver.version,
1208 MaxVersion: ver.version,
1209 Certificates: []Certificate{ecdsaCertificate},
1210 },
1211 flags: []string{"-require-any-client-certificate"},
1212 })
1213 testCases = append(testCases, testCase{
1214 testType: clientTest,
1215 name: ver.name + "-Client-ClientAuth-ECDSA",
1216 config: Config{
1217 MinVersion: ver.version,
1218 MaxVersion: ver.version,
1219 ClientAuth: RequireAnyClientCert,
1220 ClientCAs: certPool,
1221 },
1222 flags: []string{
1223 "-cert-file", ecdsaCertificateFile,
1224 "-key-file", ecdsaKeyFile,
1225 },
1226 })
1227 }
David Benjamin636293b2014-07-08 17:59:18 -04001228 }
1229}
1230
Adam Langley75712922014-10-10 16:23:43 -07001231func addExtendedMasterSecretTests() {
1232 const expectEMSFlag = "-expect-extended-master-secret"
1233
1234 for _, with := range []bool{false, true} {
1235 prefix := "No"
1236 var flags []string
1237 if with {
1238 prefix = ""
1239 flags = []string{expectEMSFlag}
1240 }
1241
1242 for _, isClient := range []bool{false, true} {
1243 suffix := "-Server"
1244 testType := serverTest
1245 if isClient {
1246 suffix = "-Client"
1247 testType = clientTest
1248 }
1249
1250 for _, ver := range tlsVersions {
1251 test := testCase{
1252 testType: testType,
1253 name: prefix + "ExtendedMasterSecret-" + ver.name + suffix,
1254 config: Config{
1255 MinVersion: ver.version,
1256 MaxVersion: ver.version,
1257 Bugs: ProtocolBugs{
1258 NoExtendedMasterSecret: !with,
1259 RequireExtendedMasterSecret: with,
1260 },
1261 },
David Benjamin48cae082014-10-27 01:06:24 -04001262 flags: flags,
1263 shouldFail: ver.version == VersionSSL30 && with,
Adam Langley75712922014-10-10 16:23:43 -07001264 }
1265 if test.shouldFail {
1266 test.expectedLocalError = "extended master secret required but not supported by peer"
1267 }
1268 testCases = append(testCases, test)
1269 }
1270 }
1271 }
1272
1273 // When a session is resumed, it should still be aware that its master
1274 // secret was generated via EMS and thus it's safe to use tls-unique.
1275 testCases = append(testCases, testCase{
1276 name: "ExtendedMasterSecret-Resume",
1277 config: Config{
1278 Bugs: ProtocolBugs{
1279 RequireExtendedMasterSecret: true,
1280 },
1281 },
1282 flags: []string{expectEMSFlag},
1283 resumeSession: true,
1284 })
1285}
1286
David Benjamin43ec06f2014-08-05 02:28:57 -04001287// Adds tests that try to cover the range of the handshake state machine, under
1288// various conditions. Some of these are redundant with other tests, but they
1289// only cover the synchronous case.
David Benjamin6fd297b2014-08-11 18:43:38 -04001290func addStateMachineCoverageTests(async, splitHandshake bool, protocol protocol) {
David Benjamin43ec06f2014-08-05 02:28:57 -04001291 var suffix string
1292 var flags []string
1293 var maxHandshakeRecordLength int
David Benjamin6fd297b2014-08-11 18:43:38 -04001294 if protocol == dtls {
1295 suffix = "-DTLS"
1296 }
David Benjamin43ec06f2014-08-05 02:28:57 -04001297 if async {
David Benjamin6fd297b2014-08-11 18:43:38 -04001298 suffix += "-Async"
David Benjamin43ec06f2014-08-05 02:28:57 -04001299 flags = append(flags, "-async")
1300 } else {
David Benjamin6fd297b2014-08-11 18:43:38 -04001301 suffix += "-Sync"
David Benjamin43ec06f2014-08-05 02:28:57 -04001302 }
1303 if splitHandshake {
1304 suffix += "-SplitHandshakeRecords"
David Benjamin98214542014-08-07 18:02:39 -04001305 maxHandshakeRecordLength = 1
David Benjamin43ec06f2014-08-05 02:28:57 -04001306 }
1307
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001308 // Basic handshake, with resumption. Client and server,
1309 // session ID and session ticket.
David Benjamin43ec06f2014-08-05 02:28:57 -04001310 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001311 protocol: protocol,
1312 name: "Basic-Client" + suffix,
David Benjamin43ec06f2014-08-05 02:28:57 -04001313 config: Config{
1314 Bugs: ProtocolBugs{
1315 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1316 },
1317 },
David Benjaminbed9aae2014-08-07 19:13:38 -04001318 flags: flags,
1319 resumeSession: true,
1320 })
1321 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001322 protocol: protocol,
1323 name: "Basic-Client-RenewTicket" + suffix,
David Benjaminbed9aae2014-08-07 19:13:38 -04001324 config: Config{
1325 Bugs: ProtocolBugs{
1326 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1327 RenewTicketOnResume: true,
1328 },
1329 },
1330 flags: flags,
1331 resumeSession: true,
David Benjamin43ec06f2014-08-05 02:28:57 -04001332 })
1333 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001334 protocol: protocol,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001335 name: "Basic-Client-NoTicket" + suffix,
1336 config: Config{
1337 SessionTicketsDisabled: true,
1338 Bugs: ProtocolBugs{
1339 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1340 },
1341 },
1342 flags: flags,
1343 resumeSession: true,
1344 })
1345 testCases = append(testCases, testCase{
1346 protocol: protocol,
David Benjamin43ec06f2014-08-05 02:28:57 -04001347 testType: serverTest,
1348 name: "Basic-Server" + suffix,
1349 config: Config{
1350 Bugs: ProtocolBugs{
1351 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1352 },
1353 },
David Benjaminbed9aae2014-08-07 19:13:38 -04001354 flags: flags,
1355 resumeSession: true,
David Benjamin43ec06f2014-08-05 02:28:57 -04001356 })
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001357 testCases = append(testCases, testCase{
1358 protocol: protocol,
1359 testType: serverTest,
1360 name: "Basic-Server-NoTickets" + suffix,
1361 config: Config{
1362 SessionTicketsDisabled: true,
1363 Bugs: ProtocolBugs{
1364 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1365 },
1366 },
1367 flags: flags,
1368 resumeSession: true,
1369 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001370
David Benjamin6fd297b2014-08-11 18:43:38 -04001371 // TLS client auth.
1372 testCases = append(testCases, testCase{
1373 protocol: protocol,
1374 testType: clientTest,
1375 name: "ClientAuth-Client" + suffix,
1376 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001377 ClientAuth: RequireAnyClientCert,
David Benjamin6fd297b2014-08-11 18:43:38 -04001378 Bugs: ProtocolBugs{
1379 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1380 },
1381 },
1382 flags: append(flags,
1383 "-cert-file", rsaCertificateFile,
1384 "-key-file", rsaKeyFile),
1385 })
1386 testCases = append(testCases, testCase{
1387 protocol: protocol,
1388 testType: serverTest,
1389 name: "ClientAuth-Server" + suffix,
1390 config: Config{
1391 Certificates: []Certificate{rsaCertificate},
1392 },
1393 flags: append(flags, "-require-any-client-certificate"),
1394 })
1395
David Benjamin43ec06f2014-08-05 02:28:57 -04001396 // No session ticket support; server doesn't send NewSessionTicket.
1397 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001398 protocol: protocol,
1399 name: "SessionTicketsDisabled-Client" + suffix,
David Benjamin43ec06f2014-08-05 02:28:57 -04001400 config: Config{
1401 SessionTicketsDisabled: true,
1402 Bugs: ProtocolBugs{
1403 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1404 },
1405 },
1406 flags: flags,
1407 })
1408 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001409 protocol: protocol,
David Benjamin43ec06f2014-08-05 02:28:57 -04001410 testType: serverTest,
1411 name: "SessionTicketsDisabled-Server" + suffix,
1412 config: Config{
1413 SessionTicketsDisabled: true,
1414 Bugs: ProtocolBugs{
1415 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1416 },
1417 },
1418 flags: flags,
1419 })
1420
David Benjamin48cae082014-10-27 01:06:24 -04001421 // Skip ServerKeyExchange in PSK key exchange if there's no
1422 // identity hint.
1423 testCases = append(testCases, testCase{
1424 protocol: protocol,
1425 name: "EmptyPSKHint-Client" + suffix,
1426 config: Config{
1427 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
1428 PreSharedKey: []byte("secret"),
1429 Bugs: ProtocolBugs{
1430 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1431 },
1432 },
1433 flags: append(flags, "-psk", "secret"),
1434 })
1435 testCases = append(testCases, testCase{
1436 protocol: protocol,
1437 testType: serverTest,
1438 name: "EmptyPSKHint-Server" + suffix,
1439 config: Config{
1440 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
1441 PreSharedKey: []byte("secret"),
1442 Bugs: ProtocolBugs{
1443 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1444 },
1445 },
1446 flags: append(flags, "-psk", "secret"),
1447 })
1448
David Benjamin6fd297b2014-08-11 18:43:38 -04001449 if protocol == tls {
1450 // NPN on client and server; results in post-handshake message.
1451 testCases = append(testCases, testCase{
1452 protocol: protocol,
1453 name: "NPN-Client" + suffix,
1454 config: Config{
David Benjaminae2888f2014-09-06 12:58:58 -04001455 NextProtos: []string{"foo"},
David Benjamin6fd297b2014-08-11 18:43:38 -04001456 Bugs: ProtocolBugs{
1457 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1458 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001459 },
David Benjaminfc7b0862014-09-06 13:21:53 -04001460 flags: append(flags, "-select-next-proto", "foo"),
1461 expectedNextProto: "foo",
1462 expectedNextProtoType: npn,
David Benjamin6fd297b2014-08-11 18:43:38 -04001463 })
1464 testCases = append(testCases, testCase{
1465 protocol: protocol,
1466 testType: serverTest,
1467 name: "NPN-Server" + suffix,
1468 config: Config{
1469 NextProtos: []string{"bar"},
1470 Bugs: ProtocolBugs{
1471 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1472 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001473 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001474 flags: append(flags,
1475 "-advertise-npn", "\x03foo\x03bar\x03baz",
1476 "-expect-next-proto", "bar"),
David Benjaminfc7b0862014-09-06 13:21:53 -04001477 expectedNextProto: "bar",
1478 expectedNextProtoType: npn,
David Benjamin6fd297b2014-08-11 18:43:38 -04001479 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001480
David Benjamin6fd297b2014-08-11 18:43:38 -04001481 // Client does False Start and negotiates NPN.
1482 testCases = append(testCases, testCase{
1483 protocol: protocol,
1484 name: "FalseStart" + suffix,
1485 config: Config{
1486 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1487 NextProtos: []string{"foo"},
1488 Bugs: ProtocolBugs{
David Benjamine58c4f52014-08-24 03:47:07 -04001489 ExpectFalseStart: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001490 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1491 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001492 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001493 flags: append(flags,
1494 "-false-start",
1495 "-select-next-proto", "foo"),
David Benjamine58c4f52014-08-24 03:47:07 -04001496 shimWritesFirst: true,
1497 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001498 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001499
David Benjaminae2888f2014-09-06 12:58:58 -04001500 // Client does False Start and negotiates ALPN.
1501 testCases = append(testCases, testCase{
1502 protocol: protocol,
1503 name: "FalseStart-ALPN" + suffix,
1504 config: Config{
1505 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1506 NextProtos: []string{"foo"},
1507 Bugs: ProtocolBugs{
1508 ExpectFalseStart: true,
1509 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1510 },
1511 },
1512 flags: append(flags,
1513 "-false-start",
1514 "-advertise-alpn", "\x03foo"),
1515 shimWritesFirst: true,
1516 resumeSession: true,
1517 })
1518
David Benjamin6fd297b2014-08-11 18:43:38 -04001519 // False Start without session tickets.
1520 testCases = append(testCases, testCase{
1521 name: "FalseStart-SessionTicketsDisabled",
1522 config: Config{
1523 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1524 NextProtos: []string{"foo"},
1525 SessionTicketsDisabled: true,
David Benjamin4e99c522014-08-24 01:45:30 -04001526 Bugs: ProtocolBugs{
David Benjamine58c4f52014-08-24 03:47:07 -04001527 ExpectFalseStart: true,
David Benjamin4e99c522014-08-24 01:45:30 -04001528 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1529 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001530 },
David Benjamin4e99c522014-08-24 01:45:30 -04001531 flags: append(flags,
David Benjamin6fd297b2014-08-11 18:43:38 -04001532 "-false-start",
1533 "-select-next-proto", "foo",
David Benjamin4e99c522014-08-24 01:45:30 -04001534 ),
David Benjamine58c4f52014-08-24 03:47:07 -04001535 shimWritesFirst: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001536 })
David Benjamin1e7f8d72014-08-08 12:27:04 -04001537
David Benjamina08e49d2014-08-24 01:46:07 -04001538 // Server parses a V2ClientHello.
David Benjamin6fd297b2014-08-11 18:43:38 -04001539 testCases = append(testCases, testCase{
1540 protocol: protocol,
1541 testType: serverTest,
1542 name: "SendV2ClientHello" + suffix,
1543 config: Config{
1544 // Choose a cipher suite that does not involve
1545 // elliptic curves, so no extensions are
1546 // involved.
1547 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
1548 Bugs: ProtocolBugs{
1549 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1550 SendV2ClientHello: true,
1551 },
David Benjamin1e7f8d72014-08-08 12:27:04 -04001552 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001553 flags: flags,
1554 })
David Benjamina08e49d2014-08-24 01:46:07 -04001555
1556 // Client sends a Channel ID.
1557 testCases = append(testCases, testCase{
1558 protocol: protocol,
1559 name: "ChannelID-Client" + suffix,
1560 config: Config{
1561 RequestChannelID: true,
1562 Bugs: ProtocolBugs{
1563 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1564 },
1565 },
1566 flags: append(flags,
1567 "-send-channel-id", channelIDKeyFile,
1568 ),
1569 resumeSession: true,
1570 expectChannelID: true,
1571 })
1572
1573 // Server accepts a Channel ID.
1574 testCases = append(testCases, testCase{
1575 protocol: protocol,
1576 testType: serverTest,
1577 name: "ChannelID-Server" + suffix,
1578 config: Config{
1579 ChannelID: channelIDKey,
1580 Bugs: ProtocolBugs{
1581 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1582 },
1583 },
1584 flags: append(flags,
1585 "-expect-channel-id",
1586 base64.StdEncoding.EncodeToString(channelIDBytes),
1587 ),
1588 resumeSession: true,
1589 expectChannelID: true,
1590 })
David Benjamin6fd297b2014-08-11 18:43:38 -04001591 } else {
1592 testCases = append(testCases, testCase{
1593 protocol: protocol,
1594 name: "SkipHelloVerifyRequest" + suffix,
1595 config: Config{
1596 Bugs: ProtocolBugs{
1597 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1598 SkipHelloVerifyRequest: true,
1599 },
1600 },
1601 flags: flags,
1602 })
1603
1604 testCases = append(testCases, testCase{
1605 testType: serverTest,
1606 protocol: protocol,
1607 name: "CookieExchange" + suffix,
1608 config: Config{
1609 Bugs: ProtocolBugs{
1610 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1611 },
1612 },
1613 flags: append(flags, "-cookie-exchange"),
1614 })
1615 }
David Benjamin43ec06f2014-08-05 02:28:57 -04001616}
1617
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001618func addVersionNegotiationTests() {
1619 for i, shimVers := range tlsVersions {
1620 // Assemble flags to disable all newer versions on the shim.
1621 var flags []string
1622 for _, vers := range tlsVersions[i+1:] {
1623 flags = append(flags, vers.flag)
1624 }
1625
1626 for _, runnerVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05001627 protocols := []protocol{tls}
1628 if runnerVers.hasDTLS && shimVers.hasDTLS {
1629 protocols = append(protocols, dtls)
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001630 }
David Benjamin8b8c0062014-11-23 02:47:52 -05001631 for _, protocol := range protocols {
1632 expectedVersion := shimVers.version
1633 if runnerVers.version < shimVers.version {
1634 expectedVersion = runnerVers.version
1635 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001636
David Benjamin8b8c0062014-11-23 02:47:52 -05001637 suffix := shimVers.name + "-" + runnerVers.name
1638 if protocol == dtls {
1639 suffix += "-DTLS"
1640 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001641
David Benjamin1eb367c2014-12-12 18:17:51 -05001642 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
1643
David Benjamin1e29a6b2014-12-10 02:27:24 -05001644 clientVers := shimVers.version
1645 if clientVers > VersionTLS10 {
1646 clientVers = VersionTLS10
1647 }
David Benjamin8b8c0062014-11-23 02:47:52 -05001648 testCases = append(testCases, testCase{
1649 protocol: protocol,
1650 testType: clientTest,
1651 name: "VersionNegotiation-Client-" + suffix,
1652 config: Config{
1653 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05001654 Bugs: ProtocolBugs{
1655 ExpectInitialRecordVersion: clientVers,
1656 },
David Benjamin8b8c0062014-11-23 02:47:52 -05001657 },
1658 flags: flags,
1659 expectedVersion: expectedVersion,
1660 })
David Benjamin1eb367c2014-12-12 18:17:51 -05001661 testCases = append(testCases, testCase{
1662 protocol: protocol,
1663 testType: clientTest,
1664 name: "VersionNegotiation-Client2-" + suffix,
1665 config: Config{
1666 MaxVersion: runnerVers.version,
1667 Bugs: ProtocolBugs{
1668 ExpectInitialRecordVersion: clientVers,
1669 },
1670 },
1671 flags: []string{"-max-version", shimVersFlag},
1672 expectedVersion: expectedVersion,
1673 })
David Benjamin8b8c0062014-11-23 02:47:52 -05001674
1675 testCases = append(testCases, testCase{
1676 protocol: protocol,
1677 testType: serverTest,
1678 name: "VersionNegotiation-Server-" + suffix,
1679 config: Config{
1680 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05001681 Bugs: ProtocolBugs{
1682 ExpectInitialRecordVersion: expectedVersion,
1683 },
David Benjamin8b8c0062014-11-23 02:47:52 -05001684 },
1685 flags: flags,
1686 expectedVersion: expectedVersion,
1687 })
David Benjamin1eb367c2014-12-12 18:17:51 -05001688 testCases = append(testCases, testCase{
1689 protocol: protocol,
1690 testType: serverTest,
1691 name: "VersionNegotiation-Server2-" + suffix,
1692 config: Config{
1693 MaxVersion: runnerVers.version,
1694 Bugs: ProtocolBugs{
1695 ExpectInitialRecordVersion: expectedVersion,
1696 },
1697 },
1698 flags: []string{"-max-version", shimVersFlag},
1699 expectedVersion: expectedVersion,
1700 })
David Benjamin8b8c0062014-11-23 02:47:52 -05001701 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001702 }
1703 }
1704}
1705
David Benjamin5c24a1d2014-08-31 00:59:27 -04001706func addD5BugTests() {
1707 testCases = append(testCases, testCase{
1708 testType: serverTest,
1709 name: "D5Bug-NoQuirk-Reject",
1710 config: Config{
1711 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
1712 Bugs: ProtocolBugs{
1713 SSL3RSAKeyExchange: true,
1714 },
1715 },
1716 shouldFail: true,
1717 expectedError: ":TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG:",
1718 })
1719 testCases = append(testCases, testCase{
1720 testType: serverTest,
1721 name: "D5Bug-Quirk-Normal",
1722 config: Config{
1723 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
1724 },
1725 flags: []string{"-tls-d5-bug"},
1726 })
1727 testCases = append(testCases, testCase{
1728 testType: serverTest,
1729 name: "D5Bug-Quirk-Bug",
1730 config: Config{
1731 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
1732 Bugs: ProtocolBugs{
1733 SSL3RSAKeyExchange: true,
1734 },
1735 },
1736 flags: []string{"-tls-d5-bug"},
1737 })
1738}
1739
David Benjamine78bfde2014-09-06 12:45:15 -04001740func addExtensionTests() {
1741 testCases = append(testCases, testCase{
1742 testType: clientTest,
1743 name: "DuplicateExtensionClient",
1744 config: Config{
1745 Bugs: ProtocolBugs{
1746 DuplicateExtension: true,
1747 },
1748 },
1749 shouldFail: true,
1750 expectedLocalError: "remote error: error decoding message",
1751 })
1752 testCases = append(testCases, testCase{
1753 testType: serverTest,
1754 name: "DuplicateExtensionServer",
1755 config: Config{
1756 Bugs: ProtocolBugs{
1757 DuplicateExtension: true,
1758 },
1759 },
1760 shouldFail: true,
1761 expectedLocalError: "remote error: error decoding message",
1762 })
1763 testCases = append(testCases, testCase{
1764 testType: clientTest,
1765 name: "ServerNameExtensionClient",
1766 config: Config{
1767 Bugs: ProtocolBugs{
1768 ExpectServerName: "example.com",
1769 },
1770 },
1771 flags: []string{"-host-name", "example.com"},
1772 })
1773 testCases = append(testCases, testCase{
1774 testType: clientTest,
1775 name: "ServerNameExtensionClient",
1776 config: Config{
1777 Bugs: ProtocolBugs{
1778 ExpectServerName: "mismatch.com",
1779 },
1780 },
1781 flags: []string{"-host-name", "example.com"},
1782 shouldFail: true,
1783 expectedLocalError: "tls: unexpected server name",
1784 })
1785 testCases = append(testCases, testCase{
1786 testType: clientTest,
1787 name: "ServerNameExtensionClient",
1788 config: Config{
1789 Bugs: ProtocolBugs{
1790 ExpectServerName: "missing.com",
1791 },
1792 },
1793 shouldFail: true,
1794 expectedLocalError: "tls: unexpected server name",
1795 })
1796 testCases = append(testCases, testCase{
1797 testType: serverTest,
1798 name: "ServerNameExtensionServer",
1799 config: Config{
1800 ServerName: "example.com",
1801 },
1802 flags: []string{"-expect-server-name", "example.com"},
1803 resumeSession: true,
1804 })
David Benjaminae2888f2014-09-06 12:58:58 -04001805 testCases = append(testCases, testCase{
1806 testType: clientTest,
1807 name: "ALPNClient",
1808 config: Config{
1809 NextProtos: []string{"foo"},
1810 },
1811 flags: []string{
1812 "-advertise-alpn", "\x03foo\x03bar\x03baz",
1813 "-expect-alpn", "foo",
1814 },
David Benjaminfc7b0862014-09-06 13:21:53 -04001815 expectedNextProto: "foo",
1816 expectedNextProtoType: alpn,
1817 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04001818 })
1819 testCases = append(testCases, testCase{
1820 testType: serverTest,
1821 name: "ALPNServer",
1822 config: Config{
1823 NextProtos: []string{"foo", "bar", "baz"},
1824 },
1825 flags: []string{
1826 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
1827 "-select-alpn", "foo",
1828 },
David Benjaminfc7b0862014-09-06 13:21:53 -04001829 expectedNextProto: "foo",
1830 expectedNextProtoType: alpn,
1831 resumeSession: true,
1832 })
1833 // Test that the server prefers ALPN over NPN.
1834 testCases = append(testCases, testCase{
1835 testType: serverTest,
1836 name: "ALPNServer-Preferred",
1837 config: Config{
1838 NextProtos: []string{"foo", "bar", "baz"},
1839 },
1840 flags: []string{
1841 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
1842 "-select-alpn", "foo",
1843 "-advertise-npn", "\x03foo\x03bar\x03baz",
1844 },
1845 expectedNextProto: "foo",
1846 expectedNextProtoType: alpn,
1847 resumeSession: true,
1848 })
1849 testCases = append(testCases, testCase{
1850 testType: serverTest,
1851 name: "ALPNServer-Preferred-Swapped",
1852 config: Config{
1853 NextProtos: []string{"foo", "bar", "baz"},
1854 Bugs: ProtocolBugs{
1855 SwapNPNAndALPN: true,
1856 },
1857 },
1858 flags: []string{
1859 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
1860 "-select-alpn", "foo",
1861 "-advertise-npn", "\x03foo\x03bar\x03baz",
1862 },
1863 expectedNextProto: "foo",
1864 expectedNextProtoType: alpn,
1865 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04001866 })
Adam Langley38311732014-10-16 19:04:35 -07001867 // Resume with a corrupt ticket.
1868 testCases = append(testCases, testCase{
1869 testType: serverTest,
1870 name: "CorruptTicket",
1871 config: Config{
1872 Bugs: ProtocolBugs{
1873 CorruptTicket: true,
1874 },
1875 },
1876 resumeSession: true,
1877 flags: []string{"-expect-session-miss"},
1878 })
1879 // Resume with an oversized session id.
1880 testCases = append(testCases, testCase{
1881 testType: serverTest,
1882 name: "OversizedSessionId",
1883 config: Config{
1884 Bugs: ProtocolBugs{
1885 OversizedSessionId: true,
1886 },
1887 },
1888 resumeSession: true,
Adam Langley75712922014-10-10 16:23:43 -07001889 shouldFail: true,
Adam Langley38311732014-10-16 19:04:35 -07001890 expectedError: ":DECODE_ERROR:",
1891 })
David Benjaminca6c8262014-11-15 19:06:08 -05001892 // Basic DTLS-SRTP tests. Include fake profiles to ensure they
1893 // are ignored.
1894 testCases = append(testCases, testCase{
1895 protocol: dtls,
1896 name: "SRTP-Client",
1897 config: Config{
1898 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
1899 },
1900 flags: []string{
1901 "-srtp-profiles",
1902 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
1903 },
1904 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
1905 })
1906 testCases = append(testCases, testCase{
1907 protocol: dtls,
1908 testType: serverTest,
1909 name: "SRTP-Server",
1910 config: Config{
1911 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
1912 },
1913 flags: []string{
1914 "-srtp-profiles",
1915 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
1916 },
1917 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
1918 })
1919 // Test that the MKI is ignored.
1920 testCases = append(testCases, testCase{
1921 protocol: dtls,
1922 testType: serverTest,
1923 name: "SRTP-Server-IgnoreMKI",
1924 config: Config{
1925 SRTPProtectionProfiles: []uint16{SRTP_AES128_CM_HMAC_SHA1_80},
1926 Bugs: ProtocolBugs{
1927 SRTPMasterKeyIdentifer: "bogus",
1928 },
1929 },
1930 flags: []string{
1931 "-srtp-profiles",
1932 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
1933 },
1934 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
1935 })
1936 // Test that SRTP isn't negotiated on the server if there were
1937 // no matching profiles.
1938 testCases = append(testCases, testCase{
1939 protocol: dtls,
1940 testType: serverTest,
1941 name: "SRTP-Server-NoMatch",
1942 config: Config{
1943 SRTPProtectionProfiles: []uint16{100, 101, 102},
1944 },
1945 flags: []string{
1946 "-srtp-profiles",
1947 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
1948 },
1949 expectedSRTPProtectionProfile: 0,
1950 })
1951 // Test that the server returning an invalid SRTP profile is
1952 // flagged as an error by the client.
1953 testCases = append(testCases, testCase{
1954 protocol: dtls,
1955 name: "SRTP-Client-NoMatch",
1956 config: Config{
1957 Bugs: ProtocolBugs{
1958 SendSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_32,
1959 },
1960 },
1961 flags: []string{
1962 "-srtp-profiles",
1963 "SRTP_AES128_CM_SHA1_80",
1964 },
1965 shouldFail: true,
1966 expectedError: ":BAD_SRTP_PROTECTION_PROFILE_LIST:",
1967 })
David Benjamin61f95272014-11-25 01:55:35 -05001968 // Test OCSP stapling and SCT list.
1969 testCases = append(testCases, testCase{
1970 name: "OCSPStapling",
1971 flags: []string{
1972 "-enable-ocsp-stapling",
1973 "-expect-ocsp-response",
1974 base64.StdEncoding.EncodeToString(testOCSPResponse),
1975 },
1976 })
1977 testCases = append(testCases, testCase{
1978 name: "SignedCertificateTimestampList",
1979 flags: []string{
1980 "-enable-signed-cert-timestamps",
1981 "-expect-signed-cert-timestamps",
1982 base64.StdEncoding.EncodeToString(testSCTList),
1983 },
1984 })
David Benjamine78bfde2014-09-06 12:45:15 -04001985}
1986
David Benjamin01fe8202014-09-24 15:21:44 -04001987func addResumptionVersionTests() {
David Benjamin01fe8202014-09-24 15:21:44 -04001988 for _, sessionVers := range tlsVersions {
David Benjamin01fe8202014-09-24 15:21:44 -04001989 for _, resumeVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05001990 protocols := []protocol{tls}
1991 if sessionVers.hasDTLS && resumeVers.hasDTLS {
1992 protocols = append(protocols, dtls)
David Benjaminbdf5e722014-11-11 00:52:15 -05001993 }
David Benjamin8b8c0062014-11-23 02:47:52 -05001994 for _, protocol := range protocols {
1995 suffix := "-" + sessionVers.name + "-" + resumeVers.name
1996 if protocol == dtls {
1997 suffix += "-DTLS"
1998 }
1999
2000 testCases = append(testCases, testCase{
2001 protocol: protocol,
2002 name: "Resume-Client" + suffix,
2003 resumeSession: true,
2004 config: Config{
2005 MaxVersion: sessionVers.version,
2006 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2007 Bugs: ProtocolBugs{
2008 AllowSessionVersionMismatch: true,
2009 },
2010 },
2011 expectedVersion: sessionVers.version,
2012 resumeConfig: &Config{
2013 MaxVersion: resumeVers.version,
2014 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2015 Bugs: ProtocolBugs{
2016 AllowSessionVersionMismatch: true,
2017 },
2018 },
2019 expectedResumeVersion: resumeVers.version,
2020 })
2021
2022 testCases = append(testCases, testCase{
2023 protocol: protocol,
2024 name: "Resume-Client-NoResume" + suffix,
2025 flags: []string{"-expect-session-miss"},
2026 resumeSession: true,
2027 config: Config{
2028 MaxVersion: sessionVers.version,
2029 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2030 },
2031 expectedVersion: sessionVers.version,
2032 resumeConfig: &Config{
2033 MaxVersion: resumeVers.version,
2034 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2035 },
2036 newSessionsOnResume: true,
2037 expectedResumeVersion: resumeVers.version,
2038 })
2039
2040 var flags []string
2041 if sessionVers.version != resumeVers.version {
2042 flags = append(flags, "-expect-session-miss")
2043 }
2044 testCases = append(testCases, testCase{
2045 protocol: protocol,
2046 testType: serverTest,
2047 name: "Resume-Server" + suffix,
2048 flags: flags,
2049 resumeSession: true,
2050 config: Config{
2051 MaxVersion: sessionVers.version,
2052 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2053 },
2054 expectedVersion: sessionVers.version,
2055 resumeConfig: &Config{
2056 MaxVersion: resumeVers.version,
2057 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2058 },
2059 expectedResumeVersion: resumeVers.version,
2060 })
2061 }
David Benjamin01fe8202014-09-24 15:21:44 -04002062 }
2063 }
2064}
2065
Adam Langley2ae77d22014-10-28 17:29:33 -07002066func addRenegotiationTests() {
2067 testCases = append(testCases, testCase{
2068 testType: serverTest,
2069 name: "Renegotiate-Server",
2070 flags: []string{"-renegotiate"},
2071 shimWritesFirst: true,
2072 })
2073 testCases = append(testCases, testCase{
2074 testType: serverTest,
2075 name: "Renegotiate-Server-EmptyExt",
2076 config: Config{
2077 Bugs: ProtocolBugs{
2078 EmptyRenegotiationInfo: true,
2079 },
2080 },
2081 flags: []string{"-renegotiate"},
2082 shimWritesFirst: true,
2083 shouldFail: true,
2084 expectedError: ":RENEGOTIATION_MISMATCH:",
2085 })
2086 testCases = append(testCases, testCase{
2087 testType: serverTest,
2088 name: "Renegotiate-Server-BadExt",
2089 config: Config{
2090 Bugs: ProtocolBugs{
2091 BadRenegotiationInfo: true,
2092 },
2093 },
2094 flags: []string{"-renegotiate"},
2095 shimWritesFirst: true,
2096 shouldFail: true,
2097 expectedError: ":RENEGOTIATION_MISMATCH:",
2098 })
David Benjaminca6554b2014-11-08 12:31:52 -05002099 testCases = append(testCases, testCase{
2100 testType: serverTest,
2101 name: "Renegotiate-Server-ClientInitiated",
2102 renegotiate: true,
2103 })
2104 testCases = append(testCases, testCase{
2105 testType: serverTest,
2106 name: "Renegotiate-Server-ClientInitiated-NoExt",
2107 renegotiate: true,
2108 config: Config{
2109 Bugs: ProtocolBugs{
2110 NoRenegotiationInfo: true,
2111 },
2112 },
2113 shouldFail: true,
2114 expectedError: ":UNSAFE_LEGACY_RENEGOTIATION_DISABLED:",
2115 })
2116 testCases = append(testCases, testCase{
2117 testType: serverTest,
2118 name: "Renegotiate-Server-ClientInitiated-NoExt-Allowed",
2119 renegotiate: true,
2120 config: Config{
2121 Bugs: ProtocolBugs{
2122 NoRenegotiationInfo: true,
2123 },
2124 },
2125 flags: []string{"-allow-unsafe-legacy-renegotiation"},
2126 })
Adam Langley2ae77d22014-10-28 17:29:33 -07002127 // TODO(agl): test the renegotiation info SCSV.
Adam Langleycf2d4f42014-10-28 19:06:14 -07002128 testCases = append(testCases, testCase{
2129 name: "Renegotiate-Client",
2130 renegotiate: true,
2131 })
2132 testCases = append(testCases, testCase{
2133 name: "Renegotiate-Client-EmptyExt",
2134 renegotiate: true,
2135 config: Config{
2136 Bugs: ProtocolBugs{
2137 EmptyRenegotiationInfo: true,
2138 },
2139 },
2140 shouldFail: true,
2141 expectedError: ":RENEGOTIATION_MISMATCH:",
2142 })
2143 testCases = append(testCases, testCase{
2144 name: "Renegotiate-Client-BadExt",
2145 renegotiate: true,
2146 config: Config{
2147 Bugs: ProtocolBugs{
2148 BadRenegotiationInfo: true,
2149 },
2150 },
2151 shouldFail: true,
2152 expectedError: ":RENEGOTIATION_MISMATCH:",
2153 })
2154 testCases = append(testCases, testCase{
2155 name: "Renegotiate-Client-SwitchCiphers",
2156 renegotiate: true,
2157 config: Config{
2158 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2159 },
2160 renegotiateCiphers: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2161 })
2162 testCases = append(testCases, testCase{
2163 name: "Renegotiate-Client-SwitchCiphers2",
2164 renegotiate: true,
2165 config: Config{
2166 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2167 },
2168 renegotiateCiphers: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2169 })
David Benjaminc44b1df2014-11-23 12:11:01 -05002170 testCases = append(testCases, testCase{
2171 name: "Renegotiate-SameClientVersion",
2172 renegotiate: true,
2173 config: Config{
2174 MaxVersion: VersionTLS10,
2175 Bugs: ProtocolBugs{
2176 RequireSameRenegoClientVersion: true,
2177 },
2178 },
2179 })
Adam Langley2ae77d22014-10-28 17:29:33 -07002180}
2181
David Benjamin5e961c12014-11-07 01:48:35 -05002182func addDTLSReplayTests() {
2183 // Test that sequence number replays are detected.
2184 testCases = append(testCases, testCase{
2185 protocol: dtls,
2186 name: "DTLS-Replay",
2187 replayWrites: true,
2188 })
2189
2190 // Test the outgoing sequence number skipping by values larger
2191 // than the retransmit window.
2192 testCases = append(testCases, testCase{
2193 protocol: dtls,
2194 name: "DTLS-Replay-LargeGaps",
2195 config: Config{
2196 Bugs: ProtocolBugs{
2197 SequenceNumberIncrement: 127,
2198 },
2199 },
2200 replayWrites: true,
2201 })
2202}
2203
Feng Lu41aa3252014-11-21 22:47:56 -08002204func addFastRadioPaddingTests() {
David Benjamin1e29a6b2014-12-10 02:27:24 -05002205 testCases = append(testCases, testCase{
2206 protocol: tls,
2207 name: "FastRadio-Padding",
Feng Lu41aa3252014-11-21 22:47:56 -08002208 config: Config{
2209 Bugs: ProtocolBugs{
2210 RequireFastradioPadding: true,
2211 },
2212 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05002213 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08002214 })
David Benjamin1e29a6b2014-12-10 02:27:24 -05002215 testCases = append(testCases, testCase{
2216 protocol: dtls,
2217 name: "FastRadio-Padding",
Feng Lu41aa3252014-11-21 22:47:56 -08002218 config: Config{
2219 Bugs: ProtocolBugs{
2220 RequireFastradioPadding: true,
2221 },
2222 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05002223 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08002224 })
2225}
2226
David Benjamin000800a2014-11-14 01:43:59 -05002227var testHashes = []struct {
2228 name string
2229 id uint8
2230}{
2231 {"SHA1", hashSHA1},
2232 {"SHA224", hashSHA224},
2233 {"SHA256", hashSHA256},
2234 {"SHA384", hashSHA384},
2235 {"SHA512", hashSHA512},
2236}
2237
2238func addSigningHashTests() {
2239 // Make sure each hash works. Include some fake hashes in the list and
2240 // ensure they're ignored.
2241 for _, hash := range testHashes {
2242 testCases = append(testCases, testCase{
2243 name: "SigningHash-ClientAuth-" + hash.name,
2244 config: Config{
2245 ClientAuth: RequireAnyClientCert,
2246 SignatureAndHashes: []signatureAndHash{
2247 {signatureRSA, 42},
2248 {signatureRSA, hash.id},
2249 {signatureRSA, 255},
2250 },
2251 },
2252 flags: []string{
2253 "-cert-file", rsaCertificateFile,
2254 "-key-file", rsaKeyFile,
2255 },
2256 })
2257
2258 testCases = append(testCases, testCase{
2259 testType: serverTest,
2260 name: "SigningHash-ServerKeyExchange-Sign-" + hash.name,
2261 config: Config{
2262 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2263 SignatureAndHashes: []signatureAndHash{
2264 {signatureRSA, 42},
2265 {signatureRSA, hash.id},
2266 {signatureRSA, 255},
2267 },
2268 },
2269 })
2270 }
2271
2272 // Test that hash resolution takes the signature type into account.
2273 testCases = append(testCases, testCase{
2274 name: "SigningHash-ClientAuth-SignatureType",
2275 config: Config{
2276 ClientAuth: RequireAnyClientCert,
2277 SignatureAndHashes: []signatureAndHash{
2278 {signatureECDSA, hashSHA512},
2279 {signatureRSA, hashSHA384},
2280 {signatureECDSA, hashSHA1},
2281 },
2282 },
2283 flags: []string{
2284 "-cert-file", rsaCertificateFile,
2285 "-key-file", rsaKeyFile,
2286 },
2287 })
2288
2289 testCases = append(testCases, testCase{
2290 testType: serverTest,
2291 name: "SigningHash-ServerKeyExchange-SignatureType",
2292 config: Config{
2293 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2294 SignatureAndHashes: []signatureAndHash{
2295 {signatureECDSA, hashSHA512},
2296 {signatureRSA, hashSHA384},
2297 {signatureECDSA, hashSHA1},
2298 },
2299 },
2300 })
2301
2302 // Test that, if the list is missing, the peer falls back to SHA-1.
2303 testCases = append(testCases, testCase{
2304 name: "SigningHash-ClientAuth-Fallback",
2305 config: Config{
2306 ClientAuth: RequireAnyClientCert,
2307 SignatureAndHashes: []signatureAndHash{
2308 {signatureRSA, hashSHA1},
2309 },
2310 Bugs: ProtocolBugs{
2311 NoSignatureAndHashes: true,
2312 },
2313 },
2314 flags: []string{
2315 "-cert-file", rsaCertificateFile,
2316 "-key-file", rsaKeyFile,
2317 },
2318 })
2319
2320 testCases = append(testCases, testCase{
2321 testType: serverTest,
2322 name: "SigningHash-ServerKeyExchange-Fallback",
2323 config: Config{
2324 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2325 SignatureAndHashes: []signatureAndHash{
2326 {signatureRSA, hashSHA1},
2327 },
2328 Bugs: ProtocolBugs{
2329 NoSignatureAndHashes: true,
2330 },
2331 },
2332 })
2333}
2334
David Benjamin884fdf12014-08-02 15:28:23 -04002335func worker(statusChan chan statusMsg, c chan *testCase, buildDir string, wg *sync.WaitGroup) {
Adam Langley95c29f32014-06-20 12:00:00 -07002336 defer wg.Done()
2337
2338 for test := range c {
Adam Langley69a01602014-11-17 17:26:55 -08002339 var err error
2340
2341 if *mallocTest < 0 {
2342 statusChan <- statusMsg{test: test, started: true}
2343 err = runTest(test, buildDir, -1)
2344 } else {
2345 for mallocNumToFail := int64(*mallocTest); ; mallocNumToFail++ {
2346 statusChan <- statusMsg{test: test, started: true}
2347 if err = runTest(test, buildDir, mallocNumToFail); err != errMoreMallocs {
2348 if err != nil {
2349 fmt.Printf("\n\nmalloc test failed at %d: %s\n", mallocNumToFail, err)
2350 }
2351 break
2352 }
2353 }
2354 }
Adam Langley95c29f32014-06-20 12:00:00 -07002355 statusChan <- statusMsg{test: test, err: err}
2356 }
2357}
2358
2359type statusMsg struct {
2360 test *testCase
2361 started bool
2362 err error
2363}
2364
2365func statusPrinter(doneChan chan struct{}, statusChan chan statusMsg, total int) {
2366 var started, done, failed, lineLen int
2367 defer close(doneChan)
2368
2369 for msg := range statusChan {
2370 if msg.started {
2371 started++
2372 } else {
2373 done++
2374 }
2375
2376 fmt.Printf("\x1b[%dD\x1b[K", lineLen)
2377
2378 if msg.err != nil {
2379 fmt.Printf("FAILED (%s)\n%s\n", msg.test.name, msg.err)
2380 failed++
2381 }
2382 line := fmt.Sprintf("%d/%d/%d/%d", failed, done, started, total)
2383 lineLen = len(line)
2384 os.Stdout.WriteString(line)
2385 }
2386}
2387
2388func main() {
2389 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 -04002390 var flagNumWorkers *int = flag.Int("num-workers", runtime.NumCPU(), "The number of workers to run in parallel.")
David Benjamin884fdf12014-08-02 15:28:23 -04002391 var flagBuildDir *string = flag.String("build-dir", "../../../build", "The build directory to run the shim from.")
Adam Langley95c29f32014-06-20 12:00:00 -07002392
2393 flag.Parse()
2394
2395 addCipherSuiteTests()
2396 addBadECDSASignatureTests()
Adam Langley80842bd2014-06-20 12:00:00 -07002397 addCBCPaddingTests()
Kenny Root7fdeaf12014-08-05 15:23:37 -07002398 addCBCSplittingTests()
David Benjamin636293b2014-07-08 17:59:18 -04002399 addClientAuthTests()
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002400 addVersionNegotiationTests()
David Benjamin5c24a1d2014-08-31 00:59:27 -04002401 addD5BugTests()
David Benjamine78bfde2014-09-06 12:45:15 -04002402 addExtensionTests()
David Benjamin01fe8202014-09-24 15:21:44 -04002403 addResumptionVersionTests()
Adam Langley75712922014-10-10 16:23:43 -07002404 addExtendedMasterSecretTests()
Adam Langley2ae77d22014-10-28 17:29:33 -07002405 addRenegotiationTests()
David Benjamin5e961c12014-11-07 01:48:35 -05002406 addDTLSReplayTests()
David Benjamin000800a2014-11-14 01:43:59 -05002407 addSigningHashTests()
Feng Lu41aa3252014-11-21 22:47:56 -08002408 addFastRadioPaddingTests()
David Benjamin43ec06f2014-08-05 02:28:57 -04002409 for _, async := range []bool{false, true} {
2410 for _, splitHandshake := range []bool{false, true} {
David Benjamin6fd297b2014-08-11 18:43:38 -04002411 for _, protocol := range []protocol{tls, dtls} {
2412 addStateMachineCoverageTests(async, splitHandshake, protocol)
2413 }
David Benjamin43ec06f2014-08-05 02:28:57 -04002414 }
2415 }
Adam Langley95c29f32014-06-20 12:00:00 -07002416
2417 var wg sync.WaitGroup
2418
David Benjamin2bc8e6f2014-08-02 15:22:37 -04002419 numWorkers := *flagNumWorkers
Adam Langley95c29f32014-06-20 12:00:00 -07002420
2421 statusChan := make(chan statusMsg, numWorkers)
2422 testChan := make(chan *testCase, numWorkers)
2423 doneChan := make(chan struct{})
2424
David Benjamin025b3d32014-07-01 19:53:04 -04002425 go statusPrinter(doneChan, statusChan, len(testCases))
Adam Langley95c29f32014-06-20 12:00:00 -07002426
2427 for i := 0; i < numWorkers; i++ {
2428 wg.Add(1)
David Benjamin884fdf12014-08-02 15:28:23 -04002429 go worker(statusChan, testChan, *flagBuildDir, &wg)
Adam Langley95c29f32014-06-20 12:00:00 -07002430 }
2431
David Benjamin025b3d32014-07-01 19:53:04 -04002432 for i := range testCases {
2433 if len(*flagTest) == 0 || *flagTest == testCases[i].name {
2434 testChan <- &testCases[i]
Adam Langley95c29f32014-06-20 12:00:00 -07002435 }
2436 }
2437
2438 close(testChan)
2439 wg.Wait()
2440 close(statusChan)
2441 <-doneChan
2442
2443 fmt.Printf("\n")
2444}