blob: 3cfc92dea816945ea1855b1e6c20dad6a5188b26 [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 },
David Benjamin82c9e902014-12-12 15:55:27 -0500448 expectedVersion: VersionTLS12,
David Benjamin98214542014-08-07 18:02:39 -0400449 },
David Benjamin98e882e2014-08-08 13:24:34 -0400450 {
451 testType: serverTest,
452 name: "MinorVersionTolerance",
453 config: Config{
454 Bugs: ProtocolBugs{
455 SendClientVersion: 0x03ff,
456 },
457 },
458 expectedVersion: VersionTLS12,
459 },
460 {
461 testType: serverTest,
462 name: "MajorVersionTolerance",
463 config: Config{
464 Bugs: ProtocolBugs{
465 SendClientVersion: 0x0400,
466 },
467 },
468 expectedVersion: VersionTLS12,
469 },
470 {
471 testType: serverTest,
472 name: "VersionTooLow",
473 config: Config{
474 Bugs: ProtocolBugs{
475 SendClientVersion: 0x0200,
476 },
477 },
478 shouldFail: true,
479 expectedError: ":UNSUPPORTED_PROTOCOL:",
480 },
481 {
482 testType: serverTest,
483 name: "HttpGET",
484 sendPrefix: "GET / HTTP/1.0\n",
485 shouldFail: true,
486 expectedError: ":HTTP_REQUEST:",
487 },
488 {
489 testType: serverTest,
490 name: "HttpPOST",
491 sendPrefix: "POST / HTTP/1.0\n",
492 shouldFail: true,
493 expectedError: ":HTTP_REQUEST:",
494 },
495 {
496 testType: serverTest,
497 name: "HttpHEAD",
498 sendPrefix: "HEAD / HTTP/1.0\n",
499 shouldFail: true,
500 expectedError: ":HTTP_REQUEST:",
501 },
502 {
503 testType: serverTest,
504 name: "HttpPUT",
505 sendPrefix: "PUT / HTTP/1.0\n",
506 shouldFail: true,
507 expectedError: ":HTTP_REQUEST:",
508 },
509 {
510 testType: serverTest,
511 name: "HttpCONNECT",
512 sendPrefix: "CONNECT www.google.com:443 HTTP/1.0\n",
513 shouldFail: true,
514 expectedError: ":HTTPS_PROXY_REQUEST:",
515 },
David Benjamin39ebf532014-08-31 02:23:49 -0400516 {
David Benjaminf080ecd2014-12-11 18:48:59 -0500517 testType: serverTest,
518 name: "Garbage",
519 sendPrefix: "blah",
520 shouldFail: true,
521 expectedError: ":UNKNOWN_PROTOCOL:",
522 },
523 {
David Benjamin39ebf532014-08-31 02:23:49 -0400524 name: "SkipCipherVersionCheck",
525 config: Config{
526 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
527 MaxVersion: VersionTLS11,
528 Bugs: ProtocolBugs{
529 SkipCipherVersionCheck: true,
530 },
531 },
532 shouldFail: true,
533 expectedError: ":WRONG_CIPHER_RETURNED:",
534 },
David Benjamin9114fae2014-11-08 11:41:14 -0500535 {
536 name: "RSAServerKeyExchange",
537 config: Config{
538 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
539 Bugs: ProtocolBugs{
540 RSAServerKeyExchange: true,
541 },
542 },
543 shouldFail: true,
544 expectedError: ":UNEXPECTED_MESSAGE:",
545 },
David Benjamin128dbc32014-12-01 01:27:42 -0500546 {
547 name: "DisableEverything",
548 flags: []string{"-no-tls12", "-no-tls11", "-no-tls1", "-no-ssl3"},
549 shouldFail: true,
550 expectedError: ":WRONG_SSL_VERSION:",
551 },
552 {
553 protocol: dtls,
554 name: "DisableEverything-DTLS",
555 flags: []string{"-no-tls12", "-no-tls1"},
556 shouldFail: true,
557 expectedError: ":WRONG_SSL_VERSION:",
558 },
Adam Langley95c29f32014-06-20 12:00:00 -0700559}
560
David Benjamin01fe8202014-09-24 15:21:44 -0400561func doExchange(test *testCase, config *Config, conn net.Conn, messageLen int, isResume bool) error {
David Benjamin65ea8ff2014-11-23 03:01:00 -0500562 var connDebug *recordingConn
563 if *flagDebug {
564 connDebug = &recordingConn{Conn: conn}
565 conn = connDebug
566 defer func() {
567 connDebug.WriteTo(os.Stdout)
568 }()
569 }
570
David Benjamin6fd297b2014-08-11 18:43:38 -0400571 if test.protocol == dtls {
572 conn = newPacketAdaptor(conn)
David Benjamin5e961c12014-11-07 01:48:35 -0500573 if test.replayWrites {
574 conn = newReplayAdaptor(conn)
575 }
David Benjamin6fd297b2014-08-11 18:43:38 -0400576 }
577
578 if test.sendPrefix != "" {
579 if _, err := conn.Write([]byte(test.sendPrefix)); err != nil {
580 return err
581 }
David Benjamin98e882e2014-08-08 13:24:34 -0400582 }
583
David Benjamin1d5c83e2014-07-22 19:20:02 -0400584 var tlsConn *Conn
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400585 if test.testType == clientTest {
David Benjamin6fd297b2014-08-11 18:43:38 -0400586 if test.protocol == dtls {
587 tlsConn = DTLSServer(conn, config)
588 } else {
589 tlsConn = Server(conn, config)
590 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400591 } else {
592 config.InsecureSkipVerify = true
David Benjamin6fd297b2014-08-11 18:43:38 -0400593 if test.protocol == dtls {
594 tlsConn = DTLSClient(conn, config)
595 } else {
596 tlsConn = Client(conn, config)
597 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400598 }
599
Adam Langley95c29f32014-06-20 12:00:00 -0700600 if err := tlsConn.Handshake(); err != nil {
601 return err
602 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700603
David Benjamin01fe8202014-09-24 15:21:44 -0400604 // TODO(davidben): move all per-connection expectations into a dedicated
605 // expectations struct that can be specified separately for the two
606 // legs.
607 expectedVersion := test.expectedVersion
608 if isResume && test.expectedResumeVersion != 0 {
609 expectedVersion = test.expectedResumeVersion
610 }
611 if vers := tlsConn.ConnectionState().Version; expectedVersion != 0 && vers != expectedVersion {
612 return fmt.Errorf("got version %x, expected %x", vers, expectedVersion)
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400613 }
614
David Benjamina08e49d2014-08-24 01:46:07 -0400615 if test.expectChannelID {
616 channelID := tlsConn.ConnectionState().ChannelID
617 if channelID == nil {
618 return fmt.Errorf("no channel ID negotiated")
619 }
620 if channelID.Curve != channelIDKey.Curve ||
621 channelIDKey.X.Cmp(channelIDKey.X) != 0 ||
622 channelIDKey.Y.Cmp(channelIDKey.Y) != 0 {
623 return fmt.Errorf("incorrect channel ID")
624 }
625 }
626
David Benjaminae2888f2014-09-06 12:58:58 -0400627 if expected := test.expectedNextProto; expected != "" {
628 if actual := tlsConn.ConnectionState().NegotiatedProtocol; actual != expected {
629 return fmt.Errorf("next proto mismatch: got %s, wanted %s", actual, expected)
630 }
631 }
632
David Benjaminfc7b0862014-09-06 13:21:53 -0400633 if test.expectedNextProtoType != 0 {
634 if (test.expectedNextProtoType == alpn) != tlsConn.ConnectionState().NegotiatedProtocolFromALPN {
635 return fmt.Errorf("next proto type mismatch")
636 }
637 }
638
David Benjaminca6c8262014-11-15 19:06:08 -0500639 if p := tlsConn.ConnectionState().SRTPProtectionProfile; p != test.expectedSRTPProtectionProfile {
640 return fmt.Errorf("SRTP profile mismatch: got %d, wanted %d", p, test.expectedSRTPProtectionProfile)
641 }
642
David Benjamine58c4f52014-08-24 03:47:07 -0400643 if test.shimWritesFirst {
644 var buf [5]byte
645 _, err := io.ReadFull(tlsConn, buf[:])
646 if err != nil {
647 return err
648 }
649 if string(buf[:]) != "hello" {
650 return fmt.Errorf("bad initial message")
651 }
652 }
653
Adam Langleycf2d4f42014-10-28 19:06:14 -0700654 if test.renegotiate {
655 if test.renegotiateCiphers != nil {
656 config.CipherSuites = test.renegotiateCiphers
657 }
658 if err := tlsConn.Renegotiate(); err != nil {
659 return err
660 }
661 } else if test.renegotiateCiphers != nil {
662 panic("renegotiateCiphers without renegotiate")
663 }
664
Kenny Root7fdeaf12014-08-05 15:23:37 -0700665 if messageLen < 0 {
David Benjamin6fd297b2014-08-11 18:43:38 -0400666 if test.protocol == dtls {
667 return fmt.Errorf("messageLen < 0 not supported for DTLS tests")
668 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700669 // Read until EOF.
670 _, err := io.Copy(ioutil.Discard, tlsConn)
671 return err
672 }
673
Adam Langley80842bd2014-06-20 12:00:00 -0700674 if messageLen == 0 {
675 messageLen = 32
676 }
677 testMessage := make([]byte, messageLen)
678 for i := range testMessage {
679 testMessage[i] = 0x42
680 }
Adam Langley95c29f32014-06-20 12:00:00 -0700681 tlsConn.Write(testMessage)
682
683 buf := make([]byte, len(testMessage))
David Benjamin6fd297b2014-08-11 18:43:38 -0400684 if test.protocol == dtls {
685 bufTmp := make([]byte, len(buf)+1)
686 n, err := tlsConn.Read(bufTmp)
687 if err != nil {
688 return err
689 }
690 if n != len(buf) {
691 return fmt.Errorf("bad reply; length mismatch (%d vs %d)", n, len(buf))
692 }
693 copy(buf, bufTmp)
694 } else {
695 _, err := io.ReadFull(tlsConn, buf)
696 if err != nil {
697 return err
698 }
Adam Langley95c29f32014-06-20 12:00:00 -0700699 }
700
701 for i, v := range buf {
702 if v != testMessage[i]^0xff {
703 return fmt.Errorf("bad reply contents at byte %d", i)
704 }
705 }
706
707 return nil
708}
709
David Benjamin325b5c32014-07-01 19:40:31 -0400710func valgrindOf(dbAttach bool, path string, args ...string) *exec.Cmd {
711 valgrindArgs := []string{"--error-exitcode=99", "--track-origins=yes", "--leak-check=full"}
Adam Langley95c29f32014-06-20 12:00:00 -0700712 if dbAttach {
David Benjamin325b5c32014-07-01 19:40:31 -0400713 valgrindArgs = append(valgrindArgs, "--db-attach=yes", "--db-command=xterm -e gdb -nw %f %p")
Adam Langley95c29f32014-06-20 12:00:00 -0700714 }
David Benjamin325b5c32014-07-01 19:40:31 -0400715 valgrindArgs = append(valgrindArgs, path)
716 valgrindArgs = append(valgrindArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -0700717
David Benjamin325b5c32014-07-01 19:40:31 -0400718 return exec.Command("valgrind", valgrindArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -0700719}
720
David Benjamin325b5c32014-07-01 19:40:31 -0400721func gdbOf(path string, args ...string) *exec.Cmd {
722 xtermArgs := []string{"-e", "gdb", "--args"}
723 xtermArgs = append(xtermArgs, path)
724 xtermArgs = append(xtermArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -0700725
David Benjamin325b5c32014-07-01 19:40:31 -0400726 return exec.Command("xterm", xtermArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -0700727}
728
David Benjamin1d5c83e2014-07-22 19:20:02 -0400729func openSocketPair() (shimEnd *os.File, conn net.Conn) {
Adam Langley95c29f32014-06-20 12:00:00 -0700730 socks, err := syscall.Socketpair(syscall.AF_UNIX, syscall.SOCK_STREAM, 0)
731 if err != nil {
732 panic(err)
733 }
734
735 syscall.CloseOnExec(socks[0])
736 syscall.CloseOnExec(socks[1])
David Benjamin1d5c83e2014-07-22 19:20:02 -0400737 shimEnd = os.NewFile(uintptr(socks[0]), "shim end")
Adam Langley95c29f32014-06-20 12:00:00 -0700738 connFile := os.NewFile(uintptr(socks[1]), "our end")
David Benjamin1d5c83e2014-07-22 19:20:02 -0400739 conn, err = net.FileConn(connFile)
740 if err != nil {
741 panic(err)
742 }
Adam Langley95c29f32014-06-20 12:00:00 -0700743 connFile.Close()
744 if err != nil {
745 panic(err)
746 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400747 return shimEnd, conn
748}
749
Adam Langley69a01602014-11-17 17:26:55 -0800750type moreMallocsError struct{}
751
752func (moreMallocsError) Error() string {
753 return "child process did not exhaust all allocation calls"
754}
755
756var errMoreMallocs = moreMallocsError{}
757
758func runTest(test *testCase, buildDir string, mallocNumToFail int64) error {
Adam Langley38311732014-10-16 19:04:35 -0700759 if !test.shouldFail && (len(test.expectedError) > 0 || len(test.expectedLocalError) > 0) {
760 panic("Error expected without shouldFail in " + test.name)
761 }
762
David Benjamin1d5c83e2014-07-22 19:20:02 -0400763 shimEnd, conn := openSocketPair()
764 shimEndResume, connResume := openSocketPair()
Adam Langley95c29f32014-06-20 12:00:00 -0700765
David Benjamin884fdf12014-08-02 15:28:23 -0400766 shim_path := path.Join(buildDir, "ssl/test/bssl_shim")
David Benjamin5a593af2014-08-11 19:51:50 -0400767 var flags []string
David Benjamin1d5c83e2014-07-22 19:20:02 -0400768 if test.testType == serverTest {
David Benjamin5a593af2014-08-11 19:51:50 -0400769 flags = append(flags, "-server")
770
David Benjamin025b3d32014-07-01 19:53:04 -0400771 flags = append(flags, "-key-file")
772 if test.keyFile == "" {
773 flags = append(flags, rsaKeyFile)
774 } else {
775 flags = append(flags, test.keyFile)
776 }
777
778 flags = append(flags, "-cert-file")
779 if test.certFile == "" {
780 flags = append(flags, rsaCertificateFile)
781 } else {
782 flags = append(flags, test.certFile)
783 }
784 }
David Benjamin5a593af2014-08-11 19:51:50 -0400785
David Benjamin6fd297b2014-08-11 18:43:38 -0400786 if test.protocol == dtls {
787 flags = append(flags, "-dtls")
788 }
789
David Benjamin5a593af2014-08-11 19:51:50 -0400790 if test.resumeSession {
791 flags = append(flags, "-resume")
792 }
793
David Benjamine58c4f52014-08-24 03:47:07 -0400794 if test.shimWritesFirst {
795 flags = append(flags, "-shim-writes-first")
796 }
797
David Benjamin025b3d32014-07-01 19:53:04 -0400798 flags = append(flags, test.flags...)
799
800 var shim *exec.Cmd
801 if *useValgrind {
802 shim = valgrindOf(false, shim_path, flags...)
Adam Langley75712922014-10-10 16:23:43 -0700803 } else if *useGDB {
804 shim = gdbOf(shim_path, flags...)
David Benjamin025b3d32014-07-01 19:53:04 -0400805 } else {
806 shim = exec.Command(shim_path, flags...)
807 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400808 shim.ExtraFiles = []*os.File{shimEnd, shimEndResume}
David Benjamin025b3d32014-07-01 19:53:04 -0400809 shim.Stdin = os.Stdin
810 var stdoutBuf, stderrBuf bytes.Buffer
811 shim.Stdout = &stdoutBuf
812 shim.Stderr = &stderrBuf
Adam Langley69a01602014-11-17 17:26:55 -0800813 if mallocNumToFail >= 0 {
814 shim.Env = []string{"MALLOC_NUMBER_TO_FAIL=" + strconv.FormatInt(mallocNumToFail, 10)}
815 if *mallocTestDebug {
816 shim.Env = append(shim.Env, "MALLOC_ABORT_ON_FAIL=1")
817 }
818 shim.Env = append(shim.Env, "_MALLOC_CHECK=1")
819 }
David Benjamin025b3d32014-07-01 19:53:04 -0400820
821 if err := shim.Start(); err != nil {
Adam Langley95c29f32014-06-20 12:00:00 -0700822 panic(err)
823 }
David Benjamin025b3d32014-07-01 19:53:04 -0400824 shimEnd.Close()
David Benjamin1d5c83e2014-07-22 19:20:02 -0400825 shimEndResume.Close()
Adam Langley95c29f32014-06-20 12:00:00 -0700826
827 config := test.config
David Benjamin1d5c83e2014-07-22 19:20:02 -0400828 config.ClientSessionCache = NewLRUClientSessionCache(1)
David Benjaminfe8eb9a2014-11-17 03:19:02 -0500829 config.ServerSessionCache = NewLRUServerSessionCache(1)
David Benjamin025b3d32014-07-01 19:53:04 -0400830 if test.testType == clientTest {
831 if len(config.Certificates) == 0 {
832 config.Certificates = []Certificate{getRSACertificate()}
833 }
David Benjamin025b3d32014-07-01 19:53:04 -0400834 }
Adam Langley95c29f32014-06-20 12:00:00 -0700835
David Benjamin01fe8202014-09-24 15:21:44 -0400836 err := doExchange(test, &config, conn, test.messageLen,
837 false /* not a resumption */)
Adam Langley95c29f32014-06-20 12:00:00 -0700838 conn.Close()
David Benjamin65ea8ff2014-11-23 03:01:00 -0500839
David Benjamin1d5c83e2014-07-22 19:20:02 -0400840 if err == nil && test.resumeSession {
David Benjamin01fe8202014-09-24 15:21:44 -0400841 var resumeConfig Config
842 if test.resumeConfig != nil {
843 resumeConfig = *test.resumeConfig
844 if len(resumeConfig.Certificates) == 0 {
845 resumeConfig.Certificates = []Certificate{getRSACertificate()}
846 }
David Benjaminfe8eb9a2014-11-17 03:19:02 -0500847 if !test.newSessionsOnResume {
848 resumeConfig.SessionTicketKey = config.SessionTicketKey
849 resumeConfig.ClientSessionCache = config.ClientSessionCache
850 resumeConfig.ServerSessionCache = config.ServerSessionCache
851 }
David Benjamin01fe8202014-09-24 15:21:44 -0400852 } else {
853 resumeConfig = config
854 }
855 err = doExchange(test, &resumeConfig, connResume, test.messageLen,
856 true /* resumption */)
David Benjamin1d5c83e2014-07-22 19:20:02 -0400857 }
David Benjamin812152a2014-09-06 12:49:07 -0400858 connResume.Close()
David Benjamin1d5c83e2014-07-22 19:20:02 -0400859
David Benjamin025b3d32014-07-01 19:53:04 -0400860 childErr := shim.Wait()
Adam Langley69a01602014-11-17 17:26:55 -0800861 if exitError, ok := childErr.(*exec.ExitError); ok {
862 if exitError.Sys().(syscall.WaitStatus).ExitStatus() == 88 {
863 return errMoreMallocs
864 }
865 }
Adam Langley95c29f32014-06-20 12:00:00 -0700866
867 stdout := string(stdoutBuf.Bytes())
868 stderr := string(stderrBuf.Bytes())
869 failed := err != nil || childErr != nil
870 correctFailure := len(test.expectedError) == 0 || strings.Contains(stdout, test.expectedError)
Adam Langleyac61fa32014-06-23 12:03:11 -0700871 localError := "none"
872 if err != nil {
873 localError = err.Error()
874 }
875 if len(test.expectedLocalError) != 0 {
876 correctFailure = correctFailure && strings.Contains(localError, test.expectedLocalError)
877 }
Adam Langley95c29f32014-06-20 12:00:00 -0700878
879 if failed != test.shouldFail || failed && !correctFailure {
Adam Langley95c29f32014-06-20 12:00:00 -0700880 childError := "none"
Adam Langley95c29f32014-06-20 12:00:00 -0700881 if childErr != nil {
882 childError = childErr.Error()
883 }
884
885 var msg string
886 switch {
887 case failed && !test.shouldFail:
888 msg = "unexpected failure"
889 case !failed && test.shouldFail:
890 msg = "unexpected success"
891 case failed && !correctFailure:
Adam Langleyac61fa32014-06-23 12:03:11 -0700892 msg = "bad error (wanted '" + test.expectedError + "' / '" + test.expectedLocalError + "')"
Adam Langley95c29f32014-06-20 12:00:00 -0700893 default:
894 panic("internal error")
895 }
896
897 return fmt.Errorf("%s: local error '%s', child error '%s', stdout:\n%s\nstderr:\n%s", msg, localError, childError, string(stdoutBuf.Bytes()), stderr)
898 }
899
900 if !*useValgrind && len(stderr) > 0 {
901 println(stderr)
902 }
903
904 return nil
905}
906
907var tlsVersions = []struct {
908 name string
909 version uint16
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400910 flag string
David Benjamin8b8c0062014-11-23 02:47:52 -0500911 hasDTLS bool
Adam Langley95c29f32014-06-20 12:00:00 -0700912}{
David Benjamin8b8c0062014-11-23 02:47:52 -0500913 {"SSL3", VersionSSL30, "-no-ssl3", false},
914 {"TLS1", VersionTLS10, "-no-tls1", true},
915 {"TLS11", VersionTLS11, "-no-tls11", false},
916 {"TLS12", VersionTLS12, "-no-tls12", true},
Adam Langley95c29f32014-06-20 12:00:00 -0700917}
918
919var testCipherSuites = []struct {
920 name string
921 id uint16
922}{
923 {"3DES-SHA", TLS_RSA_WITH_3DES_EDE_CBC_SHA},
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400924 {"AES128-GCM", TLS_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -0700925 {"AES128-SHA", TLS_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -0400926 {"AES128-SHA256", TLS_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400927 {"AES256-GCM", TLS_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -0700928 {"AES256-SHA", TLS_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -0400929 {"AES256-SHA256", TLS_RSA_WITH_AES_256_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400930 {"DHE-RSA-AES128-GCM", TLS_DHE_RSA_WITH_AES_128_GCM_SHA256},
931 {"DHE-RSA-AES128-SHA", TLS_DHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -0400932 {"DHE-RSA-AES128-SHA256", TLS_DHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400933 {"DHE-RSA-AES256-GCM", TLS_DHE_RSA_WITH_AES_256_GCM_SHA384},
934 {"DHE-RSA-AES256-SHA", TLS_DHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -0400935 {"DHE-RSA-AES256-SHA256", TLS_DHE_RSA_WITH_AES_256_CBC_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -0700936 {"ECDHE-ECDSA-AES128-GCM", TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
937 {"ECDHE-ECDSA-AES128-SHA", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -0400938 {"ECDHE-ECDSA-AES128-SHA256", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256},
939 {"ECDHE-ECDSA-AES256-GCM", TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -0700940 {"ECDHE-ECDSA-AES256-SHA", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -0400941 {"ECDHE-ECDSA-AES256-SHA384", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -0700942 {"ECDHE-ECDSA-RC4-SHA", TLS_ECDHE_ECDSA_WITH_RC4_128_SHA},
David Benjamin2af684f2014-10-27 02:23:15 -0400943 {"ECDHE-PSK-WITH-AES-128-GCM-SHA256", TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -0700944 {"ECDHE-RSA-AES128-GCM", TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -0700945 {"ECDHE-RSA-AES128-SHA", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -0400946 {"ECDHE-RSA-AES128-SHA256", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400947 {"ECDHE-RSA-AES256-GCM", TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -0700948 {"ECDHE-RSA-AES256-SHA", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -0400949 {"ECDHE-RSA-AES256-SHA384", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -0700950 {"ECDHE-RSA-RC4-SHA", TLS_ECDHE_RSA_WITH_RC4_128_SHA},
David Benjamin48cae082014-10-27 01:06:24 -0400951 {"PSK-AES128-CBC-SHA", TLS_PSK_WITH_AES_128_CBC_SHA},
952 {"PSK-AES256-CBC-SHA", TLS_PSK_WITH_AES_256_CBC_SHA},
953 {"PSK-RC4-SHA", TLS_PSK_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -0700954 {"RC4-MD5", TLS_RSA_WITH_RC4_128_MD5},
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400955 {"RC4-SHA", TLS_RSA_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -0700956}
957
David Benjamin8b8c0062014-11-23 02:47:52 -0500958func hasComponent(suiteName, component string) bool {
959 return strings.Contains("-"+suiteName+"-", "-"+component+"-")
960}
961
David Benjaminf7768e42014-08-31 02:06:47 -0400962func isTLS12Only(suiteName string) bool {
David Benjamin8b8c0062014-11-23 02:47:52 -0500963 return hasComponent(suiteName, "GCM") ||
964 hasComponent(suiteName, "SHA256") ||
965 hasComponent(suiteName, "SHA384")
966}
967
968func isDTLSCipher(suiteName string) bool {
969 // TODO(davidben): AES-GCM exists in DTLS 1.2 but is currently
970 // broken because DTLS is not EVP_AEAD-aware.
971 return !hasComponent(suiteName, "RC4") &&
972 !hasComponent(suiteName, "GCM")
David Benjaminf7768e42014-08-31 02:06:47 -0400973}
974
Adam Langley95c29f32014-06-20 12:00:00 -0700975func addCipherSuiteTests() {
976 for _, suite := range testCipherSuites {
David Benjamin48cae082014-10-27 01:06:24 -0400977 const psk = "12345"
978 const pskIdentity = "luggage combo"
979
Adam Langley95c29f32014-06-20 12:00:00 -0700980 var cert Certificate
David Benjamin025b3d32014-07-01 19:53:04 -0400981 var certFile string
982 var keyFile string
David Benjamin8b8c0062014-11-23 02:47:52 -0500983 if hasComponent(suite.name, "ECDSA") {
Adam Langley95c29f32014-06-20 12:00:00 -0700984 cert = getECDSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -0400985 certFile = ecdsaCertificateFile
986 keyFile = ecdsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -0700987 } else {
988 cert = getRSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -0400989 certFile = rsaCertificateFile
990 keyFile = rsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -0700991 }
992
David Benjamin48cae082014-10-27 01:06:24 -0400993 var flags []string
David Benjamin8b8c0062014-11-23 02:47:52 -0500994 if hasComponent(suite.name, "PSK") {
David Benjamin48cae082014-10-27 01:06:24 -0400995 flags = append(flags,
996 "-psk", psk,
997 "-psk-identity", pskIdentity)
998 }
999
Adam Langley95c29f32014-06-20 12:00:00 -07001000 for _, ver := range tlsVersions {
David Benjaminf7768e42014-08-31 02:06:47 -04001001 if ver.version < VersionTLS12 && isTLS12Only(suite.name) {
Adam Langley95c29f32014-06-20 12:00:00 -07001002 continue
1003 }
1004
David Benjamin025b3d32014-07-01 19:53:04 -04001005 testCases = append(testCases, testCase{
1006 testType: clientTest,
1007 name: ver.name + "-" + suite.name + "-client",
Adam Langley95c29f32014-06-20 12:00:00 -07001008 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001009 MinVersion: ver.version,
1010 MaxVersion: ver.version,
1011 CipherSuites: []uint16{suite.id},
1012 Certificates: []Certificate{cert},
1013 PreSharedKey: []byte(psk),
1014 PreSharedKeyIdentity: pskIdentity,
Adam Langley95c29f32014-06-20 12:00:00 -07001015 },
David Benjamin48cae082014-10-27 01:06:24 -04001016 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001017 resumeSession: true,
Adam Langley95c29f32014-06-20 12:00:00 -07001018 })
David Benjamin025b3d32014-07-01 19:53:04 -04001019
David Benjamin76d8abe2014-08-14 16:25:34 -04001020 testCases = append(testCases, testCase{
1021 testType: serverTest,
1022 name: ver.name + "-" + suite.name + "-server",
1023 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001024 MinVersion: ver.version,
1025 MaxVersion: ver.version,
1026 CipherSuites: []uint16{suite.id},
1027 Certificates: []Certificate{cert},
1028 PreSharedKey: []byte(psk),
1029 PreSharedKeyIdentity: pskIdentity,
David Benjamin76d8abe2014-08-14 16:25:34 -04001030 },
1031 certFile: certFile,
1032 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001033 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001034 resumeSession: true,
David Benjamin76d8abe2014-08-14 16:25:34 -04001035 })
David Benjamin6fd297b2014-08-11 18:43:38 -04001036
David Benjamin8b8c0062014-11-23 02:47:52 -05001037 if ver.hasDTLS && isDTLSCipher(suite.name) {
David Benjamin6fd297b2014-08-11 18:43:38 -04001038 testCases = append(testCases, testCase{
1039 testType: clientTest,
1040 protocol: dtls,
1041 name: "D" + ver.name + "-" + suite.name + "-client",
1042 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001043 MinVersion: ver.version,
1044 MaxVersion: ver.version,
1045 CipherSuites: []uint16{suite.id},
1046 Certificates: []Certificate{cert},
1047 PreSharedKey: []byte(psk),
1048 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001049 },
David Benjamin48cae082014-10-27 01:06:24 -04001050 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001051 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001052 })
1053 testCases = append(testCases, testCase{
1054 testType: serverTest,
1055 protocol: dtls,
1056 name: "D" + ver.name + "-" + suite.name + "-server",
1057 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001058 MinVersion: ver.version,
1059 MaxVersion: ver.version,
1060 CipherSuites: []uint16{suite.id},
1061 Certificates: []Certificate{cert},
1062 PreSharedKey: []byte(psk),
1063 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001064 },
1065 certFile: certFile,
1066 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001067 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001068 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001069 })
1070 }
Adam Langley95c29f32014-06-20 12:00:00 -07001071 }
1072 }
1073}
1074
1075func addBadECDSASignatureTests() {
1076 for badR := BadValue(1); badR < NumBadValues; badR++ {
1077 for badS := BadValue(1); badS < NumBadValues; badS++ {
David Benjamin025b3d32014-07-01 19:53:04 -04001078 testCases = append(testCases, testCase{
Adam Langley95c29f32014-06-20 12:00:00 -07001079 name: fmt.Sprintf("BadECDSA-%d-%d", badR, badS),
1080 config: Config{
1081 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
1082 Certificates: []Certificate{getECDSACertificate()},
1083 Bugs: ProtocolBugs{
1084 BadECDSAR: badR,
1085 BadECDSAS: badS,
1086 },
1087 },
1088 shouldFail: true,
1089 expectedError: "SIGNATURE",
1090 })
1091 }
1092 }
1093}
1094
Adam Langley80842bd2014-06-20 12:00:00 -07001095func addCBCPaddingTests() {
David Benjamin025b3d32014-07-01 19:53:04 -04001096 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001097 name: "MaxCBCPadding",
1098 config: Config{
1099 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1100 Bugs: ProtocolBugs{
1101 MaxPadding: true,
1102 },
1103 },
1104 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1105 })
David Benjamin025b3d32014-07-01 19:53:04 -04001106 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001107 name: "BadCBCPadding",
1108 config: Config{
1109 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1110 Bugs: ProtocolBugs{
1111 PaddingFirstByteBad: true,
1112 },
1113 },
1114 shouldFail: true,
1115 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1116 })
1117 // OpenSSL previously had an issue where the first byte of padding in
1118 // 255 bytes of padding wasn't checked.
David Benjamin025b3d32014-07-01 19:53:04 -04001119 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001120 name: "BadCBCPadding255",
1121 config: Config{
1122 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1123 Bugs: ProtocolBugs{
1124 MaxPadding: true,
1125 PaddingFirstByteBadIf255: true,
1126 },
1127 },
1128 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1129 shouldFail: true,
1130 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1131 })
1132}
1133
Kenny Root7fdeaf12014-08-05 15:23:37 -07001134func addCBCSplittingTests() {
1135 testCases = append(testCases, testCase{
1136 name: "CBCRecordSplitting",
1137 config: Config{
1138 MaxVersion: VersionTLS10,
1139 MinVersion: VersionTLS10,
1140 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1141 },
1142 messageLen: -1, // read until EOF
1143 flags: []string{
1144 "-async",
1145 "-write-different-record-sizes",
1146 "-cbc-record-splitting",
1147 },
David Benjamina8e3e0e2014-08-06 22:11:10 -04001148 })
1149 testCases = append(testCases, testCase{
Kenny Root7fdeaf12014-08-05 15:23:37 -07001150 name: "CBCRecordSplittingPartialWrite",
1151 config: Config{
1152 MaxVersion: VersionTLS10,
1153 MinVersion: VersionTLS10,
1154 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1155 },
1156 messageLen: -1, // read until EOF
1157 flags: []string{
1158 "-async",
1159 "-write-different-record-sizes",
1160 "-cbc-record-splitting",
1161 "-partial-write",
1162 },
1163 })
1164}
1165
David Benjamin636293b2014-07-08 17:59:18 -04001166func addClientAuthTests() {
David Benjamin407a10c2014-07-16 12:58:59 -04001167 // Add a dummy cert pool to stress certificate authority parsing.
1168 // TODO(davidben): Add tests that those values parse out correctly.
1169 certPool := x509.NewCertPool()
1170 cert, err := x509.ParseCertificate(rsaCertificate.Certificate[0])
1171 if err != nil {
1172 panic(err)
1173 }
1174 certPool.AddCert(cert)
1175
David Benjamin636293b2014-07-08 17:59:18 -04001176 for _, ver := range tlsVersions {
David Benjamin636293b2014-07-08 17:59:18 -04001177 testCases = append(testCases, testCase{
1178 testType: clientTest,
David Benjamin67666e72014-07-12 15:47:52 -04001179 name: ver.name + "-Client-ClientAuth-RSA",
David Benjamin636293b2014-07-08 17:59:18 -04001180 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001181 MinVersion: ver.version,
1182 MaxVersion: ver.version,
1183 ClientAuth: RequireAnyClientCert,
1184 ClientCAs: certPool,
David Benjamin636293b2014-07-08 17:59:18 -04001185 },
1186 flags: []string{
1187 "-cert-file", rsaCertificateFile,
1188 "-key-file", rsaKeyFile,
1189 },
1190 })
1191 testCases = append(testCases, testCase{
David Benjamin67666e72014-07-12 15:47:52 -04001192 testType: serverTest,
1193 name: ver.name + "-Server-ClientAuth-RSA",
1194 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001195 MinVersion: ver.version,
1196 MaxVersion: ver.version,
David Benjamin67666e72014-07-12 15:47:52 -04001197 Certificates: []Certificate{rsaCertificate},
1198 },
1199 flags: []string{"-require-any-client-certificate"},
1200 })
David Benjamine098ec22014-08-27 23:13:20 -04001201 if ver.version != VersionSSL30 {
1202 testCases = append(testCases, testCase{
1203 testType: serverTest,
1204 name: ver.name + "-Server-ClientAuth-ECDSA",
1205 config: Config{
1206 MinVersion: ver.version,
1207 MaxVersion: ver.version,
1208 Certificates: []Certificate{ecdsaCertificate},
1209 },
1210 flags: []string{"-require-any-client-certificate"},
1211 })
1212 testCases = append(testCases, testCase{
1213 testType: clientTest,
1214 name: ver.name + "-Client-ClientAuth-ECDSA",
1215 config: Config{
1216 MinVersion: ver.version,
1217 MaxVersion: ver.version,
1218 ClientAuth: RequireAnyClientCert,
1219 ClientCAs: certPool,
1220 },
1221 flags: []string{
1222 "-cert-file", ecdsaCertificateFile,
1223 "-key-file", ecdsaKeyFile,
1224 },
1225 })
1226 }
David Benjamin636293b2014-07-08 17:59:18 -04001227 }
1228}
1229
Adam Langley75712922014-10-10 16:23:43 -07001230func addExtendedMasterSecretTests() {
1231 const expectEMSFlag = "-expect-extended-master-secret"
1232
1233 for _, with := range []bool{false, true} {
1234 prefix := "No"
1235 var flags []string
1236 if with {
1237 prefix = ""
1238 flags = []string{expectEMSFlag}
1239 }
1240
1241 for _, isClient := range []bool{false, true} {
1242 suffix := "-Server"
1243 testType := serverTest
1244 if isClient {
1245 suffix = "-Client"
1246 testType = clientTest
1247 }
1248
1249 for _, ver := range tlsVersions {
1250 test := testCase{
1251 testType: testType,
1252 name: prefix + "ExtendedMasterSecret-" + ver.name + suffix,
1253 config: Config{
1254 MinVersion: ver.version,
1255 MaxVersion: ver.version,
1256 Bugs: ProtocolBugs{
1257 NoExtendedMasterSecret: !with,
1258 RequireExtendedMasterSecret: with,
1259 },
1260 },
David Benjamin48cae082014-10-27 01:06:24 -04001261 flags: flags,
1262 shouldFail: ver.version == VersionSSL30 && with,
Adam Langley75712922014-10-10 16:23:43 -07001263 }
1264 if test.shouldFail {
1265 test.expectedLocalError = "extended master secret required but not supported by peer"
1266 }
1267 testCases = append(testCases, test)
1268 }
1269 }
1270 }
1271
1272 // When a session is resumed, it should still be aware that its master
1273 // secret was generated via EMS and thus it's safe to use tls-unique.
1274 testCases = append(testCases, testCase{
1275 name: "ExtendedMasterSecret-Resume",
1276 config: Config{
1277 Bugs: ProtocolBugs{
1278 RequireExtendedMasterSecret: true,
1279 },
1280 },
1281 flags: []string{expectEMSFlag},
1282 resumeSession: true,
1283 })
1284}
1285
David Benjamin43ec06f2014-08-05 02:28:57 -04001286// Adds tests that try to cover the range of the handshake state machine, under
1287// various conditions. Some of these are redundant with other tests, but they
1288// only cover the synchronous case.
David Benjamin6fd297b2014-08-11 18:43:38 -04001289func addStateMachineCoverageTests(async, splitHandshake bool, protocol protocol) {
David Benjamin43ec06f2014-08-05 02:28:57 -04001290 var suffix string
1291 var flags []string
1292 var maxHandshakeRecordLength int
David Benjamin6fd297b2014-08-11 18:43:38 -04001293 if protocol == dtls {
1294 suffix = "-DTLS"
1295 }
David Benjamin43ec06f2014-08-05 02:28:57 -04001296 if async {
David Benjamin6fd297b2014-08-11 18:43:38 -04001297 suffix += "-Async"
David Benjamin43ec06f2014-08-05 02:28:57 -04001298 flags = append(flags, "-async")
1299 } else {
David Benjamin6fd297b2014-08-11 18:43:38 -04001300 suffix += "-Sync"
David Benjamin43ec06f2014-08-05 02:28:57 -04001301 }
1302 if splitHandshake {
1303 suffix += "-SplitHandshakeRecords"
David Benjamin98214542014-08-07 18:02:39 -04001304 maxHandshakeRecordLength = 1
David Benjamin43ec06f2014-08-05 02:28:57 -04001305 }
1306
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001307 // Basic handshake, with resumption. Client and server,
1308 // session ID and session ticket.
David Benjamin43ec06f2014-08-05 02:28:57 -04001309 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001310 protocol: protocol,
1311 name: "Basic-Client" + suffix,
David Benjamin43ec06f2014-08-05 02:28:57 -04001312 config: Config{
1313 Bugs: ProtocolBugs{
1314 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1315 },
1316 },
David Benjaminbed9aae2014-08-07 19:13:38 -04001317 flags: flags,
1318 resumeSession: true,
1319 })
1320 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001321 protocol: protocol,
1322 name: "Basic-Client-RenewTicket" + suffix,
David Benjaminbed9aae2014-08-07 19:13:38 -04001323 config: Config{
1324 Bugs: ProtocolBugs{
1325 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1326 RenewTicketOnResume: true,
1327 },
1328 },
1329 flags: flags,
1330 resumeSession: true,
David Benjamin43ec06f2014-08-05 02:28:57 -04001331 })
1332 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001333 protocol: protocol,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001334 name: "Basic-Client-NoTicket" + suffix,
1335 config: Config{
1336 SessionTicketsDisabled: true,
1337 Bugs: ProtocolBugs{
1338 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1339 },
1340 },
1341 flags: flags,
1342 resumeSession: true,
1343 })
1344 testCases = append(testCases, testCase{
1345 protocol: protocol,
David Benjamin43ec06f2014-08-05 02:28:57 -04001346 testType: serverTest,
1347 name: "Basic-Server" + suffix,
1348 config: Config{
1349 Bugs: ProtocolBugs{
1350 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1351 },
1352 },
David Benjaminbed9aae2014-08-07 19:13:38 -04001353 flags: flags,
1354 resumeSession: true,
David Benjamin43ec06f2014-08-05 02:28:57 -04001355 })
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001356 testCases = append(testCases, testCase{
1357 protocol: protocol,
1358 testType: serverTest,
1359 name: "Basic-Server-NoTickets" + suffix,
1360 config: Config{
1361 SessionTicketsDisabled: true,
1362 Bugs: ProtocolBugs{
1363 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1364 },
1365 },
1366 flags: flags,
1367 resumeSession: true,
1368 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001369
David Benjamin6fd297b2014-08-11 18:43:38 -04001370 // TLS client auth.
1371 testCases = append(testCases, testCase{
1372 protocol: protocol,
1373 testType: clientTest,
1374 name: "ClientAuth-Client" + suffix,
1375 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001376 ClientAuth: RequireAnyClientCert,
David Benjamin6fd297b2014-08-11 18:43:38 -04001377 Bugs: ProtocolBugs{
1378 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1379 },
1380 },
1381 flags: append(flags,
1382 "-cert-file", rsaCertificateFile,
1383 "-key-file", rsaKeyFile),
1384 })
1385 testCases = append(testCases, testCase{
1386 protocol: protocol,
1387 testType: serverTest,
1388 name: "ClientAuth-Server" + suffix,
1389 config: Config{
1390 Certificates: []Certificate{rsaCertificate},
1391 },
1392 flags: append(flags, "-require-any-client-certificate"),
1393 })
1394
David Benjamin43ec06f2014-08-05 02:28:57 -04001395 // No session ticket support; server doesn't send NewSessionTicket.
1396 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001397 protocol: protocol,
1398 name: "SessionTicketsDisabled-Client" + suffix,
David Benjamin43ec06f2014-08-05 02:28:57 -04001399 config: Config{
1400 SessionTicketsDisabled: true,
1401 Bugs: ProtocolBugs{
1402 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1403 },
1404 },
1405 flags: flags,
1406 })
1407 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001408 protocol: protocol,
David Benjamin43ec06f2014-08-05 02:28:57 -04001409 testType: serverTest,
1410 name: "SessionTicketsDisabled-Server" + suffix,
1411 config: Config{
1412 SessionTicketsDisabled: true,
1413 Bugs: ProtocolBugs{
1414 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1415 },
1416 },
1417 flags: flags,
1418 })
1419
David Benjamin48cae082014-10-27 01:06:24 -04001420 // Skip ServerKeyExchange in PSK key exchange if there's no
1421 // identity hint.
1422 testCases = append(testCases, testCase{
1423 protocol: protocol,
1424 name: "EmptyPSKHint-Client" + suffix,
1425 config: Config{
1426 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
1427 PreSharedKey: []byte("secret"),
1428 Bugs: ProtocolBugs{
1429 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1430 },
1431 },
1432 flags: append(flags, "-psk", "secret"),
1433 })
1434 testCases = append(testCases, testCase{
1435 protocol: protocol,
1436 testType: serverTest,
1437 name: "EmptyPSKHint-Server" + suffix,
1438 config: Config{
1439 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
1440 PreSharedKey: []byte("secret"),
1441 Bugs: ProtocolBugs{
1442 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1443 },
1444 },
1445 flags: append(flags, "-psk", "secret"),
1446 })
1447
David Benjamin6fd297b2014-08-11 18:43:38 -04001448 if protocol == tls {
1449 // NPN on client and server; results in post-handshake message.
1450 testCases = append(testCases, testCase{
1451 protocol: protocol,
1452 name: "NPN-Client" + suffix,
1453 config: Config{
David Benjaminae2888f2014-09-06 12:58:58 -04001454 NextProtos: []string{"foo"},
David Benjamin6fd297b2014-08-11 18:43:38 -04001455 Bugs: ProtocolBugs{
1456 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1457 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001458 },
David Benjaminfc7b0862014-09-06 13:21:53 -04001459 flags: append(flags, "-select-next-proto", "foo"),
1460 expectedNextProto: "foo",
1461 expectedNextProtoType: npn,
David Benjamin6fd297b2014-08-11 18:43:38 -04001462 })
1463 testCases = append(testCases, testCase{
1464 protocol: protocol,
1465 testType: serverTest,
1466 name: "NPN-Server" + suffix,
1467 config: Config{
1468 NextProtos: []string{"bar"},
1469 Bugs: ProtocolBugs{
1470 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1471 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001472 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001473 flags: append(flags,
1474 "-advertise-npn", "\x03foo\x03bar\x03baz",
1475 "-expect-next-proto", "bar"),
David Benjaminfc7b0862014-09-06 13:21:53 -04001476 expectedNextProto: "bar",
1477 expectedNextProtoType: npn,
David Benjamin6fd297b2014-08-11 18:43:38 -04001478 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001479
David Benjamin6fd297b2014-08-11 18:43:38 -04001480 // Client does False Start and negotiates NPN.
1481 testCases = append(testCases, testCase{
1482 protocol: protocol,
1483 name: "FalseStart" + suffix,
1484 config: Config{
1485 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1486 NextProtos: []string{"foo"},
1487 Bugs: ProtocolBugs{
David Benjamine58c4f52014-08-24 03:47:07 -04001488 ExpectFalseStart: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001489 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1490 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001491 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001492 flags: append(flags,
1493 "-false-start",
1494 "-select-next-proto", "foo"),
David Benjamine58c4f52014-08-24 03:47:07 -04001495 shimWritesFirst: true,
1496 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001497 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001498
David Benjaminae2888f2014-09-06 12:58:58 -04001499 // Client does False Start and negotiates ALPN.
1500 testCases = append(testCases, testCase{
1501 protocol: protocol,
1502 name: "FalseStart-ALPN" + suffix,
1503 config: Config{
1504 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1505 NextProtos: []string{"foo"},
1506 Bugs: ProtocolBugs{
1507 ExpectFalseStart: true,
1508 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1509 },
1510 },
1511 flags: append(flags,
1512 "-false-start",
1513 "-advertise-alpn", "\x03foo"),
1514 shimWritesFirst: true,
1515 resumeSession: true,
1516 })
1517
David Benjamin6fd297b2014-08-11 18:43:38 -04001518 // False Start without session tickets.
1519 testCases = append(testCases, testCase{
1520 name: "FalseStart-SessionTicketsDisabled",
1521 config: Config{
1522 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1523 NextProtos: []string{"foo"},
1524 SessionTicketsDisabled: true,
David Benjamin4e99c522014-08-24 01:45:30 -04001525 Bugs: ProtocolBugs{
David Benjamine58c4f52014-08-24 03:47:07 -04001526 ExpectFalseStart: true,
David Benjamin4e99c522014-08-24 01:45:30 -04001527 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1528 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001529 },
David Benjamin4e99c522014-08-24 01:45:30 -04001530 flags: append(flags,
David Benjamin6fd297b2014-08-11 18:43:38 -04001531 "-false-start",
1532 "-select-next-proto", "foo",
David Benjamin4e99c522014-08-24 01:45:30 -04001533 ),
David Benjamine58c4f52014-08-24 03:47:07 -04001534 shimWritesFirst: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001535 })
David Benjamin1e7f8d72014-08-08 12:27:04 -04001536
David Benjamina08e49d2014-08-24 01:46:07 -04001537 // Server parses a V2ClientHello.
David Benjamin6fd297b2014-08-11 18:43:38 -04001538 testCases = append(testCases, testCase{
1539 protocol: protocol,
1540 testType: serverTest,
1541 name: "SendV2ClientHello" + suffix,
1542 config: Config{
1543 // Choose a cipher suite that does not involve
1544 // elliptic curves, so no extensions are
1545 // involved.
1546 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
1547 Bugs: ProtocolBugs{
1548 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1549 SendV2ClientHello: true,
1550 },
David Benjamin1e7f8d72014-08-08 12:27:04 -04001551 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001552 flags: flags,
1553 })
David Benjamina08e49d2014-08-24 01:46:07 -04001554
1555 // Client sends a Channel ID.
1556 testCases = append(testCases, testCase{
1557 protocol: protocol,
1558 name: "ChannelID-Client" + suffix,
1559 config: Config{
1560 RequestChannelID: true,
1561 Bugs: ProtocolBugs{
1562 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1563 },
1564 },
1565 flags: append(flags,
1566 "-send-channel-id", channelIDKeyFile,
1567 ),
1568 resumeSession: true,
1569 expectChannelID: true,
1570 })
1571
1572 // Server accepts a Channel ID.
1573 testCases = append(testCases, testCase{
1574 protocol: protocol,
1575 testType: serverTest,
1576 name: "ChannelID-Server" + suffix,
1577 config: Config{
1578 ChannelID: channelIDKey,
1579 Bugs: ProtocolBugs{
1580 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1581 },
1582 },
1583 flags: append(flags,
1584 "-expect-channel-id",
1585 base64.StdEncoding.EncodeToString(channelIDBytes),
1586 ),
1587 resumeSession: true,
1588 expectChannelID: true,
1589 })
David Benjamin6fd297b2014-08-11 18:43:38 -04001590 } else {
1591 testCases = append(testCases, testCase{
1592 protocol: protocol,
1593 name: "SkipHelloVerifyRequest" + suffix,
1594 config: Config{
1595 Bugs: ProtocolBugs{
1596 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1597 SkipHelloVerifyRequest: true,
1598 },
1599 },
1600 flags: flags,
1601 })
1602
1603 testCases = append(testCases, testCase{
1604 testType: serverTest,
1605 protocol: protocol,
1606 name: "CookieExchange" + suffix,
1607 config: Config{
1608 Bugs: ProtocolBugs{
1609 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1610 },
1611 },
1612 flags: append(flags, "-cookie-exchange"),
1613 })
1614 }
David Benjamin43ec06f2014-08-05 02:28:57 -04001615}
1616
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001617func addVersionNegotiationTests() {
1618 for i, shimVers := range tlsVersions {
1619 // Assemble flags to disable all newer versions on the shim.
1620 var flags []string
1621 for _, vers := range tlsVersions[i+1:] {
1622 flags = append(flags, vers.flag)
1623 }
1624
1625 for _, runnerVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05001626 protocols := []protocol{tls}
1627 if runnerVers.hasDTLS && shimVers.hasDTLS {
1628 protocols = append(protocols, dtls)
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001629 }
David Benjamin8b8c0062014-11-23 02:47:52 -05001630 for _, protocol := range protocols {
1631 expectedVersion := shimVers.version
1632 if runnerVers.version < shimVers.version {
1633 expectedVersion = runnerVers.version
1634 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001635
David Benjamin8b8c0062014-11-23 02:47:52 -05001636 suffix := shimVers.name + "-" + runnerVers.name
1637 if protocol == dtls {
1638 suffix += "-DTLS"
1639 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001640
David Benjamin1eb367c2014-12-12 18:17:51 -05001641 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
1642
David Benjamin1e29a6b2014-12-10 02:27:24 -05001643 clientVers := shimVers.version
1644 if clientVers > VersionTLS10 {
1645 clientVers = VersionTLS10
1646 }
David Benjamin8b8c0062014-11-23 02:47:52 -05001647 testCases = append(testCases, testCase{
1648 protocol: protocol,
1649 testType: clientTest,
1650 name: "VersionNegotiation-Client-" + suffix,
1651 config: Config{
1652 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05001653 Bugs: ProtocolBugs{
1654 ExpectInitialRecordVersion: clientVers,
1655 },
David Benjamin8b8c0062014-11-23 02:47:52 -05001656 },
1657 flags: flags,
1658 expectedVersion: expectedVersion,
1659 })
David Benjamin1eb367c2014-12-12 18:17:51 -05001660 testCases = append(testCases, testCase{
1661 protocol: protocol,
1662 testType: clientTest,
1663 name: "VersionNegotiation-Client2-" + suffix,
1664 config: Config{
1665 MaxVersion: runnerVers.version,
1666 Bugs: ProtocolBugs{
1667 ExpectInitialRecordVersion: clientVers,
1668 },
1669 },
1670 flags: []string{"-max-version", shimVersFlag},
1671 expectedVersion: expectedVersion,
1672 })
David Benjamin8b8c0062014-11-23 02:47:52 -05001673
1674 testCases = append(testCases, testCase{
1675 protocol: protocol,
1676 testType: serverTest,
1677 name: "VersionNegotiation-Server-" + suffix,
1678 config: Config{
1679 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05001680 Bugs: ProtocolBugs{
1681 ExpectInitialRecordVersion: expectedVersion,
1682 },
David Benjamin8b8c0062014-11-23 02:47:52 -05001683 },
1684 flags: flags,
1685 expectedVersion: expectedVersion,
1686 })
David Benjamin1eb367c2014-12-12 18:17:51 -05001687 testCases = append(testCases, testCase{
1688 protocol: protocol,
1689 testType: serverTest,
1690 name: "VersionNegotiation-Server2-" + suffix,
1691 config: Config{
1692 MaxVersion: runnerVers.version,
1693 Bugs: ProtocolBugs{
1694 ExpectInitialRecordVersion: expectedVersion,
1695 },
1696 },
1697 flags: []string{"-max-version", shimVersFlag},
1698 expectedVersion: expectedVersion,
1699 })
David Benjamin8b8c0062014-11-23 02:47:52 -05001700 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001701 }
1702 }
1703}
1704
David Benjaminaccb4542014-12-12 23:44:33 -05001705func addMinimumVersionTests() {
1706 for i, shimVers := range tlsVersions {
1707 // Assemble flags to disable all older versions on the shim.
1708 var flags []string
1709 for _, vers := range tlsVersions[:i] {
1710 flags = append(flags, vers.flag)
1711 }
1712
1713 for _, runnerVers := range tlsVersions {
1714 protocols := []protocol{tls}
1715 if runnerVers.hasDTLS && shimVers.hasDTLS {
1716 protocols = append(protocols, dtls)
1717 }
1718 for _, protocol := range protocols {
1719 suffix := shimVers.name + "-" + runnerVers.name
1720 if protocol == dtls {
1721 suffix += "-DTLS"
1722 }
1723 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
1724
1725 // TODO(davidben): This should also assert on
1726 // expectedLocalError to check we send an alert
1727 // rather than close the connection, but the TLS
1728 // code currently fails this.
1729 var expectedVersion uint16
1730 var shouldFail bool
1731 var expectedError string
1732 if runnerVers.version >= shimVers.version {
1733 expectedVersion = runnerVers.version
1734 } else {
1735 shouldFail = true
1736 expectedError = ":UNSUPPORTED_PROTOCOL:"
1737 }
1738
1739 testCases = append(testCases, testCase{
1740 protocol: protocol,
1741 testType: clientTest,
1742 name: "MinimumVersion-Client-" + suffix,
1743 config: Config{
1744 MaxVersion: runnerVers.version,
1745 },
1746 flags: flags,
1747 expectedVersion: expectedVersion,
1748 shouldFail: shouldFail,
1749 expectedError: expectedError,
1750 })
1751 testCases = append(testCases, testCase{
1752 protocol: protocol,
1753 testType: clientTest,
1754 name: "MinimumVersion-Client2-" + suffix,
1755 config: Config{
1756 MaxVersion: runnerVers.version,
1757 },
1758 flags: []string{"-min-version", shimVersFlag},
1759 expectedVersion: expectedVersion,
1760 shouldFail: shouldFail,
1761 expectedError: expectedError,
1762 })
1763
1764 testCases = append(testCases, testCase{
1765 protocol: protocol,
1766 testType: serverTest,
1767 name: "MinimumVersion-Server-" + suffix,
1768 config: Config{
1769 MaxVersion: runnerVers.version,
1770 },
1771 flags: flags,
1772 expectedVersion: expectedVersion,
1773 shouldFail: shouldFail,
1774 expectedError: expectedError,
1775 })
1776 testCases = append(testCases, testCase{
1777 protocol: protocol,
1778 testType: serverTest,
1779 name: "MinimumVersion-Server2-" + suffix,
1780 config: Config{
1781 MaxVersion: runnerVers.version,
1782 },
1783 flags: []string{"-min-version", shimVersFlag},
1784 expectedVersion: expectedVersion,
1785 shouldFail: shouldFail,
1786 expectedError: expectedError,
1787 })
1788 }
1789 }
1790 }
1791}
1792
David Benjamin5c24a1d2014-08-31 00:59:27 -04001793func addD5BugTests() {
1794 testCases = append(testCases, testCase{
1795 testType: serverTest,
1796 name: "D5Bug-NoQuirk-Reject",
1797 config: Config{
1798 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
1799 Bugs: ProtocolBugs{
1800 SSL3RSAKeyExchange: true,
1801 },
1802 },
1803 shouldFail: true,
1804 expectedError: ":TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG:",
1805 })
1806 testCases = append(testCases, testCase{
1807 testType: serverTest,
1808 name: "D5Bug-Quirk-Normal",
1809 config: Config{
1810 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
1811 },
1812 flags: []string{"-tls-d5-bug"},
1813 })
1814 testCases = append(testCases, testCase{
1815 testType: serverTest,
1816 name: "D5Bug-Quirk-Bug",
1817 config: Config{
1818 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
1819 Bugs: ProtocolBugs{
1820 SSL3RSAKeyExchange: true,
1821 },
1822 },
1823 flags: []string{"-tls-d5-bug"},
1824 })
1825}
1826
David Benjamine78bfde2014-09-06 12:45:15 -04001827func addExtensionTests() {
1828 testCases = append(testCases, testCase{
1829 testType: clientTest,
1830 name: "DuplicateExtensionClient",
1831 config: Config{
1832 Bugs: ProtocolBugs{
1833 DuplicateExtension: true,
1834 },
1835 },
1836 shouldFail: true,
1837 expectedLocalError: "remote error: error decoding message",
1838 })
1839 testCases = append(testCases, testCase{
1840 testType: serverTest,
1841 name: "DuplicateExtensionServer",
1842 config: Config{
1843 Bugs: ProtocolBugs{
1844 DuplicateExtension: true,
1845 },
1846 },
1847 shouldFail: true,
1848 expectedLocalError: "remote error: error decoding message",
1849 })
1850 testCases = append(testCases, testCase{
1851 testType: clientTest,
1852 name: "ServerNameExtensionClient",
1853 config: Config{
1854 Bugs: ProtocolBugs{
1855 ExpectServerName: "example.com",
1856 },
1857 },
1858 flags: []string{"-host-name", "example.com"},
1859 })
1860 testCases = append(testCases, testCase{
1861 testType: clientTest,
1862 name: "ServerNameExtensionClient",
1863 config: Config{
1864 Bugs: ProtocolBugs{
1865 ExpectServerName: "mismatch.com",
1866 },
1867 },
1868 flags: []string{"-host-name", "example.com"},
1869 shouldFail: true,
1870 expectedLocalError: "tls: unexpected server name",
1871 })
1872 testCases = append(testCases, testCase{
1873 testType: clientTest,
1874 name: "ServerNameExtensionClient",
1875 config: Config{
1876 Bugs: ProtocolBugs{
1877 ExpectServerName: "missing.com",
1878 },
1879 },
1880 shouldFail: true,
1881 expectedLocalError: "tls: unexpected server name",
1882 })
1883 testCases = append(testCases, testCase{
1884 testType: serverTest,
1885 name: "ServerNameExtensionServer",
1886 config: Config{
1887 ServerName: "example.com",
1888 },
1889 flags: []string{"-expect-server-name", "example.com"},
1890 resumeSession: true,
1891 })
David Benjaminae2888f2014-09-06 12:58:58 -04001892 testCases = append(testCases, testCase{
1893 testType: clientTest,
1894 name: "ALPNClient",
1895 config: Config{
1896 NextProtos: []string{"foo"},
1897 },
1898 flags: []string{
1899 "-advertise-alpn", "\x03foo\x03bar\x03baz",
1900 "-expect-alpn", "foo",
1901 },
David Benjaminfc7b0862014-09-06 13:21:53 -04001902 expectedNextProto: "foo",
1903 expectedNextProtoType: alpn,
1904 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04001905 })
1906 testCases = append(testCases, testCase{
1907 testType: serverTest,
1908 name: "ALPNServer",
1909 config: Config{
1910 NextProtos: []string{"foo", "bar", "baz"},
1911 },
1912 flags: []string{
1913 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
1914 "-select-alpn", "foo",
1915 },
David Benjaminfc7b0862014-09-06 13:21:53 -04001916 expectedNextProto: "foo",
1917 expectedNextProtoType: alpn,
1918 resumeSession: true,
1919 })
1920 // Test that the server prefers ALPN over NPN.
1921 testCases = append(testCases, testCase{
1922 testType: serverTest,
1923 name: "ALPNServer-Preferred",
1924 config: Config{
1925 NextProtos: []string{"foo", "bar", "baz"},
1926 },
1927 flags: []string{
1928 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
1929 "-select-alpn", "foo",
1930 "-advertise-npn", "\x03foo\x03bar\x03baz",
1931 },
1932 expectedNextProto: "foo",
1933 expectedNextProtoType: alpn,
1934 resumeSession: true,
1935 })
1936 testCases = append(testCases, testCase{
1937 testType: serverTest,
1938 name: "ALPNServer-Preferred-Swapped",
1939 config: Config{
1940 NextProtos: []string{"foo", "bar", "baz"},
1941 Bugs: ProtocolBugs{
1942 SwapNPNAndALPN: true,
1943 },
1944 },
1945 flags: []string{
1946 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
1947 "-select-alpn", "foo",
1948 "-advertise-npn", "\x03foo\x03bar\x03baz",
1949 },
1950 expectedNextProto: "foo",
1951 expectedNextProtoType: alpn,
1952 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04001953 })
Adam Langley38311732014-10-16 19:04:35 -07001954 // Resume with a corrupt ticket.
1955 testCases = append(testCases, testCase{
1956 testType: serverTest,
1957 name: "CorruptTicket",
1958 config: Config{
1959 Bugs: ProtocolBugs{
1960 CorruptTicket: true,
1961 },
1962 },
1963 resumeSession: true,
1964 flags: []string{"-expect-session-miss"},
1965 })
1966 // Resume with an oversized session id.
1967 testCases = append(testCases, testCase{
1968 testType: serverTest,
1969 name: "OversizedSessionId",
1970 config: Config{
1971 Bugs: ProtocolBugs{
1972 OversizedSessionId: true,
1973 },
1974 },
1975 resumeSession: true,
Adam Langley75712922014-10-10 16:23:43 -07001976 shouldFail: true,
Adam Langley38311732014-10-16 19:04:35 -07001977 expectedError: ":DECODE_ERROR:",
1978 })
David Benjaminca6c8262014-11-15 19:06:08 -05001979 // Basic DTLS-SRTP tests. Include fake profiles to ensure they
1980 // are ignored.
1981 testCases = append(testCases, testCase{
1982 protocol: dtls,
1983 name: "SRTP-Client",
1984 config: Config{
1985 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
1986 },
1987 flags: []string{
1988 "-srtp-profiles",
1989 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
1990 },
1991 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
1992 })
1993 testCases = append(testCases, testCase{
1994 protocol: dtls,
1995 testType: serverTest,
1996 name: "SRTP-Server",
1997 config: Config{
1998 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
1999 },
2000 flags: []string{
2001 "-srtp-profiles",
2002 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2003 },
2004 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2005 })
2006 // Test that the MKI is ignored.
2007 testCases = append(testCases, testCase{
2008 protocol: dtls,
2009 testType: serverTest,
2010 name: "SRTP-Server-IgnoreMKI",
2011 config: Config{
2012 SRTPProtectionProfiles: []uint16{SRTP_AES128_CM_HMAC_SHA1_80},
2013 Bugs: ProtocolBugs{
2014 SRTPMasterKeyIdentifer: "bogus",
2015 },
2016 },
2017 flags: []string{
2018 "-srtp-profiles",
2019 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2020 },
2021 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2022 })
2023 // Test that SRTP isn't negotiated on the server if there were
2024 // no matching profiles.
2025 testCases = append(testCases, testCase{
2026 protocol: dtls,
2027 testType: serverTest,
2028 name: "SRTP-Server-NoMatch",
2029 config: Config{
2030 SRTPProtectionProfiles: []uint16{100, 101, 102},
2031 },
2032 flags: []string{
2033 "-srtp-profiles",
2034 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2035 },
2036 expectedSRTPProtectionProfile: 0,
2037 })
2038 // Test that the server returning an invalid SRTP profile is
2039 // flagged as an error by the client.
2040 testCases = append(testCases, testCase{
2041 protocol: dtls,
2042 name: "SRTP-Client-NoMatch",
2043 config: Config{
2044 Bugs: ProtocolBugs{
2045 SendSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_32,
2046 },
2047 },
2048 flags: []string{
2049 "-srtp-profiles",
2050 "SRTP_AES128_CM_SHA1_80",
2051 },
2052 shouldFail: true,
2053 expectedError: ":BAD_SRTP_PROTECTION_PROFILE_LIST:",
2054 })
David Benjamin61f95272014-11-25 01:55:35 -05002055 // Test OCSP stapling and SCT list.
2056 testCases = append(testCases, testCase{
2057 name: "OCSPStapling",
2058 flags: []string{
2059 "-enable-ocsp-stapling",
2060 "-expect-ocsp-response",
2061 base64.StdEncoding.EncodeToString(testOCSPResponse),
2062 },
2063 })
2064 testCases = append(testCases, testCase{
2065 name: "SignedCertificateTimestampList",
2066 flags: []string{
2067 "-enable-signed-cert-timestamps",
2068 "-expect-signed-cert-timestamps",
2069 base64.StdEncoding.EncodeToString(testSCTList),
2070 },
2071 })
David Benjamine78bfde2014-09-06 12:45:15 -04002072}
2073
David Benjamin01fe8202014-09-24 15:21:44 -04002074func addResumptionVersionTests() {
David Benjamin01fe8202014-09-24 15:21:44 -04002075 for _, sessionVers := range tlsVersions {
David Benjamin01fe8202014-09-24 15:21:44 -04002076 for _, resumeVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05002077 protocols := []protocol{tls}
2078 if sessionVers.hasDTLS && resumeVers.hasDTLS {
2079 protocols = append(protocols, dtls)
David Benjaminbdf5e722014-11-11 00:52:15 -05002080 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002081 for _, protocol := range protocols {
2082 suffix := "-" + sessionVers.name + "-" + resumeVers.name
2083 if protocol == dtls {
2084 suffix += "-DTLS"
2085 }
2086
2087 testCases = append(testCases, testCase{
2088 protocol: protocol,
2089 name: "Resume-Client" + suffix,
2090 resumeSession: true,
2091 config: Config{
2092 MaxVersion: sessionVers.version,
2093 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2094 Bugs: ProtocolBugs{
2095 AllowSessionVersionMismatch: true,
2096 },
2097 },
2098 expectedVersion: sessionVers.version,
2099 resumeConfig: &Config{
2100 MaxVersion: resumeVers.version,
2101 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2102 Bugs: ProtocolBugs{
2103 AllowSessionVersionMismatch: true,
2104 },
2105 },
2106 expectedResumeVersion: resumeVers.version,
2107 })
2108
2109 testCases = append(testCases, testCase{
2110 protocol: protocol,
2111 name: "Resume-Client-NoResume" + suffix,
2112 flags: []string{"-expect-session-miss"},
2113 resumeSession: true,
2114 config: Config{
2115 MaxVersion: sessionVers.version,
2116 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2117 },
2118 expectedVersion: sessionVers.version,
2119 resumeConfig: &Config{
2120 MaxVersion: resumeVers.version,
2121 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2122 },
2123 newSessionsOnResume: true,
2124 expectedResumeVersion: resumeVers.version,
2125 })
2126
2127 var flags []string
2128 if sessionVers.version != resumeVers.version {
2129 flags = append(flags, "-expect-session-miss")
2130 }
2131 testCases = append(testCases, testCase{
2132 protocol: protocol,
2133 testType: serverTest,
2134 name: "Resume-Server" + suffix,
2135 flags: flags,
2136 resumeSession: true,
2137 config: Config{
2138 MaxVersion: sessionVers.version,
2139 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2140 },
2141 expectedVersion: sessionVers.version,
2142 resumeConfig: &Config{
2143 MaxVersion: resumeVers.version,
2144 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2145 },
2146 expectedResumeVersion: resumeVers.version,
2147 })
2148 }
David Benjamin01fe8202014-09-24 15:21:44 -04002149 }
2150 }
2151}
2152
Adam Langley2ae77d22014-10-28 17:29:33 -07002153func addRenegotiationTests() {
2154 testCases = append(testCases, testCase{
2155 testType: serverTest,
2156 name: "Renegotiate-Server",
2157 flags: []string{"-renegotiate"},
2158 shimWritesFirst: true,
2159 })
2160 testCases = append(testCases, testCase{
2161 testType: serverTest,
2162 name: "Renegotiate-Server-EmptyExt",
2163 config: Config{
2164 Bugs: ProtocolBugs{
2165 EmptyRenegotiationInfo: true,
2166 },
2167 },
2168 flags: []string{"-renegotiate"},
2169 shimWritesFirst: true,
2170 shouldFail: true,
2171 expectedError: ":RENEGOTIATION_MISMATCH:",
2172 })
2173 testCases = append(testCases, testCase{
2174 testType: serverTest,
2175 name: "Renegotiate-Server-BadExt",
2176 config: Config{
2177 Bugs: ProtocolBugs{
2178 BadRenegotiationInfo: true,
2179 },
2180 },
2181 flags: []string{"-renegotiate"},
2182 shimWritesFirst: true,
2183 shouldFail: true,
2184 expectedError: ":RENEGOTIATION_MISMATCH:",
2185 })
David Benjaminca6554b2014-11-08 12:31:52 -05002186 testCases = append(testCases, testCase{
2187 testType: serverTest,
2188 name: "Renegotiate-Server-ClientInitiated",
2189 renegotiate: true,
2190 })
2191 testCases = append(testCases, testCase{
2192 testType: serverTest,
2193 name: "Renegotiate-Server-ClientInitiated-NoExt",
2194 renegotiate: true,
2195 config: Config{
2196 Bugs: ProtocolBugs{
2197 NoRenegotiationInfo: true,
2198 },
2199 },
2200 shouldFail: true,
2201 expectedError: ":UNSAFE_LEGACY_RENEGOTIATION_DISABLED:",
2202 })
2203 testCases = append(testCases, testCase{
2204 testType: serverTest,
2205 name: "Renegotiate-Server-ClientInitiated-NoExt-Allowed",
2206 renegotiate: true,
2207 config: Config{
2208 Bugs: ProtocolBugs{
2209 NoRenegotiationInfo: true,
2210 },
2211 },
2212 flags: []string{"-allow-unsafe-legacy-renegotiation"},
2213 })
Adam Langley2ae77d22014-10-28 17:29:33 -07002214 // TODO(agl): test the renegotiation info SCSV.
Adam Langleycf2d4f42014-10-28 19:06:14 -07002215 testCases = append(testCases, testCase{
2216 name: "Renegotiate-Client",
2217 renegotiate: true,
2218 })
2219 testCases = append(testCases, testCase{
2220 name: "Renegotiate-Client-EmptyExt",
2221 renegotiate: true,
2222 config: Config{
2223 Bugs: ProtocolBugs{
2224 EmptyRenegotiationInfo: true,
2225 },
2226 },
2227 shouldFail: true,
2228 expectedError: ":RENEGOTIATION_MISMATCH:",
2229 })
2230 testCases = append(testCases, testCase{
2231 name: "Renegotiate-Client-BadExt",
2232 renegotiate: true,
2233 config: Config{
2234 Bugs: ProtocolBugs{
2235 BadRenegotiationInfo: true,
2236 },
2237 },
2238 shouldFail: true,
2239 expectedError: ":RENEGOTIATION_MISMATCH:",
2240 })
2241 testCases = append(testCases, testCase{
2242 name: "Renegotiate-Client-SwitchCiphers",
2243 renegotiate: true,
2244 config: Config{
2245 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2246 },
2247 renegotiateCiphers: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2248 })
2249 testCases = append(testCases, testCase{
2250 name: "Renegotiate-Client-SwitchCiphers2",
2251 renegotiate: true,
2252 config: Config{
2253 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2254 },
2255 renegotiateCiphers: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2256 })
David Benjaminc44b1df2014-11-23 12:11:01 -05002257 testCases = append(testCases, testCase{
2258 name: "Renegotiate-SameClientVersion",
2259 renegotiate: true,
2260 config: Config{
2261 MaxVersion: VersionTLS10,
2262 Bugs: ProtocolBugs{
2263 RequireSameRenegoClientVersion: true,
2264 },
2265 },
2266 })
Adam Langley2ae77d22014-10-28 17:29:33 -07002267}
2268
David Benjamin5e961c12014-11-07 01:48:35 -05002269func addDTLSReplayTests() {
2270 // Test that sequence number replays are detected.
2271 testCases = append(testCases, testCase{
2272 protocol: dtls,
2273 name: "DTLS-Replay",
2274 replayWrites: true,
2275 })
2276
2277 // Test the outgoing sequence number skipping by values larger
2278 // than the retransmit window.
2279 testCases = append(testCases, testCase{
2280 protocol: dtls,
2281 name: "DTLS-Replay-LargeGaps",
2282 config: Config{
2283 Bugs: ProtocolBugs{
2284 SequenceNumberIncrement: 127,
2285 },
2286 },
2287 replayWrites: true,
2288 })
2289}
2290
Feng Lu41aa3252014-11-21 22:47:56 -08002291func addFastRadioPaddingTests() {
David Benjamin1e29a6b2014-12-10 02:27:24 -05002292 testCases = append(testCases, testCase{
2293 protocol: tls,
2294 name: "FastRadio-Padding",
Feng Lu41aa3252014-11-21 22:47:56 -08002295 config: Config{
2296 Bugs: ProtocolBugs{
2297 RequireFastradioPadding: true,
2298 },
2299 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05002300 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08002301 })
David Benjamin1e29a6b2014-12-10 02:27:24 -05002302 testCases = append(testCases, testCase{
2303 protocol: dtls,
2304 name: "FastRadio-Padding",
Feng Lu41aa3252014-11-21 22:47:56 -08002305 config: Config{
2306 Bugs: ProtocolBugs{
2307 RequireFastradioPadding: true,
2308 },
2309 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05002310 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08002311 })
2312}
2313
David Benjamin000800a2014-11-14 01:43:59 -05002314var testHashes = []struct {
2315 name string
2316 id uint8
2317}{
2318 {"SHA1", hashSHA1},
2319 {"SHA224", hashSHA224},
2320 {"SHA256", hashSHA256},
2321 {"SHA384", hashSHA384},
2322 {"SHA512", hashSHA512},
2323}
2324
2325func addSigningHashTests() {
2326 // Make sure each hash works. Include some fake hashes in the list and
2327 // ensure they're ignored.
2328 for _, hash := range testHashes {
2329 testCases = append(testCases, testCase{
2330 name: "SigningHash-ClientAuth-" + hash.name,
2331 config: Config{
2332 ClientAuth: RequireAnyClientCert,
2333 SignatureAndHashes: []signatureAndHash{
2334 {signatureRSA, 42},
2335 {signatureRSA, hash.id},
2336 {signatureRSA, 255},
2337 },
2338 },
2339 flags: []string{
2340 "-cert-file", rsaCertificateFile,
2341 "-key-file", rsaKeyFile,
2342 },
2343 })
2344
2345 testCases = append(testCases, testCase{
2346 testType: serverTest,
2347 name: "SigningHash-ServerKeyExchange-Sign-" + hash.name,
2348 config: Config{
2349 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2350 SignatureAndHashes: []signatureAndHash{
2351 {signatureRSA, 42},
2352 {signatureRSA, hash.id},
2353 {signatureRSA, 255},
2354 },
2355 },
2356 })
2357 }
2358
2359 // Test that hash resolution takes the signature type into account.
2360 testCases = append(testCases, testCase{
2361 name: "SigningHash-ClientAuth-SignatureType",
2362 config: Config{
2363 ClientAuth: RequireAnyClientCert,
2364 SignatureAndHashes: []signatureAndHash{
2365 {signatureECDSA, hashSHA512},
2366 {signatureRSA, hashSHA384},
2367 {signatureECDSA, hashSHA1},
2368 },
2369 },
2370 flags: []string{
2371 "-cert-file", rsaCertificateFile,
2372 "-key-file", rsaKeyFile,
2373 },
2374 })
2375
2376 testCases = append(testCases, testCase{
2377 testType: serverTest,
2378 name: "SigningHash-ServerKeyExchange-SignatureType",
2379 config: Config{
2380 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2381 SignatureAndHashes: []signatureAndHash{
2382 {signatureECDSA, hashSHA512},
2383 {signatureRSA, hashSHA384},
2384 {signatureECDSA, hashSHA1},
2385 },
2386 },
2387 })
2388
2389 // Test that, if the list is missing, the peer falls back to SHA-1.
2390 testCases = append(testCases, testCase{
2391 name: "SigningHash-ClientAuth-Fallback",
2392 config: Config{
2393 ClientAuth: RequireAnyClientCert,
2394 SignatureAndHashes: []signatureAndHash{
2395 {signatureRSA, hashSHA1},
2396 },
2397 Bugs: ProtocolBugs{
2398 NoSignatureAndHashes: true,
2399 },
2400 },
2401 flags: []string{
2402 "-cert-file", rsaCertificateFile,
2403 "-key-file", rsaKeyFile,
2404 },
2405 })
2406
2407 testCases = append(testCases, testCase{
2408 testType: serverTest,
2409 name: "SigningHash-ServerKeyExchange-Fallback",
2410 config: Config{
2411 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2412 SignatureAndHashes: []signatureAndHash{
2413 {signatureRSA, hashSHA1},
2414 },
2415 Bugs: ProtocolBugs{
2416 NoSignatureAndHashes: true,
2417 },
2418 },
2419 })
2420}
2421
David Benjamin884fdf12014-08-02 15:28:23 -04002422func worker(statusChan chan statusMsg, c chan *testCase, buildDir string, wg *sync.WaitGroup) {
Adam Langley95c29f32014-06-20 12:00:00 -07002423 defer wg.Done()
2424
2425 for test := range c {
Adam Langley69a01602014-11-17 17:26:55 -08002426 var err error
2427
2428 if *mallocTest < 0 {
2429 statusChan <- statusMsg{test: test, started: true}
2430 err = runTest(test, buildDir, -1)
2431 } else {
2432 for mallocNumToFail := int64(*mallocTest); ; mallocNumToFail++ {
2433 statusChan <- statusMsg{test: test, started: true}
2434 if err = runTest(test, buildDir, mallocNumToFail); err != errMoreMallocs {
2435 if err != nil {
2436 fmt.Printf("\n\nmalloc test failed at %d: %s\n", mallocNumToFail, err)
2437 }
2438 break
2439 }
2440 }
2441 }
Adam Langley95c29f32014-06-20 12:00:00 -07002442 statusChan <- statusMsg{test: test, err: err}
2443 }
2444}
2445
2446type statusMsg struct {
2447 test *testCase
2448 started bool
2449 err error
2450}
2451
2452func statusPrinter(doneChan chan struct{}, statusChan chan statusMsg, total int) {
2453 var started, done, failed, lineLen int
2454 defer close(doneChan)
2455
2456 for msg := range statusChan {
2457 if msg.started {
2458 started++
2459 } else {
2460 done++
2461 }
2462
2463 fmt.Printf("\x1b[%dD\x1b[K", lineLen)
2464
2465 if msg.err != nil {
2466 fmt.Printf("FAILED (%s)\n%s\n", msg.test.name, msg.err)
2467 failed++
2468 }
2469 line := fmt.Sprintf("%d/%d/%d/%d", failed, done, started, total)
2470 lineLen = len(line)
2471 os.Stdout.WriteString(line)
2472 }
2473}
2474
2475func main() {
2476 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 -04002477 var flagNumWorkers *int = flag.Int("num-workers", runtime.NumCPU(), "The number of workers to run in parallel.")
David Benjamin884fdf12014-08-02 15:28:23 -04002478 var flagBuildDir *string = flag.String("build-dir", "../../../build", "The build directory to run the shim from.")
Adam Langley95c29f32014-06-20 12:00:00 -07002479
2480 flag.Parse()
2481
2482 addCipherSuiteTests()
2483 addBadECDSASignatureTests()
Adam Langley80842bd2014-06-20 12:00:00 -07002484 addCBCPaddingTests()
Kenny Root7fdeaf12014-08-05 15:23:37 -07002485 addCBCSplittingTests()
David Benjamin636293b2014-07-08 17:59:18 -04002486 addClientAuthTests()
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002487 addVersionNegotiationTests()
David Benjaminaccb4542014-12-12 23:44:33 -05002488 addMinimumVersionTests()
David Benjamin5c24a1d2014-08-31 00:59:27 -04002489 addD5BugTests()
David Benjamine78bfde2014-09-06 12:45:15 -04002490 addExtensionTests()
David Benjamin01fe8202014-09-24 15:21:44 -04002491 addResumptionVersionTests()
Adam Langley75712922014-10-10 16:23:43 -07002492 addExtendedMasterSecretTests()
Adam Langley2ae77d22014-10-28 17:29:33 -07002493 addRenegotiationTests()
David Benjamin5e961c12014-11-07 01:48:35 -05002494 addDTLSReplayTests()
David Benjamin000800a2014-11-14 01:43:59 -05002495 addSigningHashTests()
Feng Lu41aa3252014-11-21 22:47:56 -08002496 addFastRadioPaddingTests()
David Benjamin43ec06f2014-08-05 02:28:57 -04002497 for _, async := range []bool{false, true} {
2498 for _, splitHandshake := range []bool{false, true} {
David Benjamin6fd297b2014-08-11 18:43:38 -04002499 for _, protocol := range []protocol{tls, dtls} {
2500 addStateMachineCoverageTests(async, splitHandshake, protocol)
2501 }
David Benjamin43ec06f2014-08-05 02:28:57 -04002502 }
2503 }
Adam Langley95c29f32014-06-20 12:00:00 -07002504
2505 var wg sync.WaitGroup
2506
David Benjamin2bc8e6f2014-08-02 15:22:37 -04002507 numWorkers := *flagNumWorkers
Adam Langley95c29f32014-06-20 12:00:00 -07002508
2509 statusChan := make(chan statusMsg, numWorkers)
2510 testChan := make(chan *testCase, numWorkers)
2511 doneChan := make(chan struct{})
2512
David Benjamin025b3d32014-07-01 19:53:04 -04002513 go statusPrinter(doneChan, statusChan, len(testCases))
Adam Langley95c29f32014-06-20 12:00:00 -07002514
2515 for i := 0; i < numWorkers; i++ {
2516 wg.Add(1)
David Benjamin884fdf12014-08-02 15:28:23 -04002517 go worker(statusChan, testChan, *flagBuildDir, &wg)
Adam Langley95c29f32014-06-20 12:00:00 -07002518 }
2519
David Benjamin025b3d32014-07-01 19:53:04 -04002520 for i := range testCases {
2521 if len(*flagTest) == 0 || *flagTest == testCases[i].name {
2522 testChan <- &testCases[i]
Adam Langley95c29f32014-06-20 12:00:00 -07002523 }
2524 }
2525
2526 close(testChan)
2527 wg.Wait()
2528 close(statusChan)
2529 <-doneChan
2530
2531 fmt.Printf("\n")
2532}