blob: d76bdf723f05cbe42ba07e48cf44eb269791166d [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 },
David Benjamin780d6dd2015-01-06 12:03:19 -0500559 {
560 name: "NoSharedCipher",
561 config: Config{
562 CipherSuites: []uint16{},
563 },
564 shouldFail: true,
565 expectedError: ":HANDSHAKE_FAILURE_ON_CLIENT_HELLO:",
566 },
David Benjamin13be1de2015-01-11 16:29:36 -0500567 {
568 protocol: dtls,
569 testType: serverTest,
570 name: "MTU",
571 config: Config{
572 Bugs: ProtocolBugs{
573 MaxPacketLength: 256,
574 },
575 },
576 flags: []string{"-mtu", "256"},
577 },
578 {
579 protocol: dtls,
580 testType: serverTest,
581 name: "MTUExceeded",
582 config: Config{
583 Bugs: ProtocolBugs{
584 MaxPacketLength: 255,
585 },
586 },
587 flags: []string{"-mtu", "256"},
588 shouldFail: true,
589 expectedLocalError: "dtls: exceeded maximum packet length",
590 },
Adam Langley95c29f32014-06-20 12:00:00 -0700591}
592
David Benjamin01fe8202014-09-24 15:21:44 -0400593func doExchange(test *testCase, config *Config, conn net.Conn, messageLen int, isResume bool) error {
David Benjamin65ea8ff2014-11-23 03:01:00 -0500594 var connDebug *recordingConn
595 if *flagDebug {
596 connDebug = &recordingConn{Conn: conn}
597 conn = connDebug
598 defer func() {
599 connDebug.WriteTo(os.Stdout)
600 }()
601 }
602
David Benjamin6fd297b2014-08-11 18:43:38 -0400603 if test.protocol == dtls {
604 conn = newPacketAdaptor(conn)
David Benjamin5e961c12014-11-07 01:48:35 -0500605 if test.replayWrites {
606 conn = newReplayAdaptor(conn)
607 }
David Benjamin6fd297b2014-08-11 18:43:38 -0400608 }
609
610 if test.sendPrefix != "" {
611 if _, err := conn.Write([]byte(test.sendPrefix)); err != nil {
612 return err
613 }
David Benjamin98e882e2014-08-08 13:24:34 -0400614 }
615
David Benjamin1d5c83e2014-07-22 19:20:02 -0400616 var tlsConn *Conn
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400617 if test.testType == clientTest {
David Benjamin6fd297b2014-08-11 18:43:38 -0400618 if test.protocol == dtls {
619 tlsConn = DTLSServer(conn, config)
620 } else {
621 tlsConn = Server(conn, config)
622 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400623 } else {
624 config.InsecureSkipVerify = true
David Benjamin6fd297b2014-08-11 18:43:38 -0400625 if test.protocol == dtls {
626 tlsConn = DTLSClient(conn, config)
627 } else {
628 tlsConn = Client(conn, config)
629 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400630 }
631
Adam Langley95c29f32014-06-20 12:00:00 -0700632 if err := tlsConn.Handshake(); err != nil {
633 return err
634 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700635
David Benjamin01fe8202014-09-24 15:21:44 -0400636 // TODO(davidben): move all per-connection expectations into a dedicated
637 // expectations struct that can be specified separately for the two
638 // legs.
639 expectedVersion := test.expectedVersion
640 if isResume && test.expectedResumeVersion != 0 {
641 expectedVersion = test.expectedResumeVersion
642 }
643 if vers := tlsConn.ConnectionState().Version; expectedVersion != 0 && vers != expectedVersion {
644 return fmt.Errorf("got version %x, expected %x", vers, expectedVersion)
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400645 }
646
David Benjamina08e49d2014-08-24 01:46:07 -0400647 if test.expectChannelID {
648 channelID := tlsConn.ConnectionState().ChannelID
649 if channelID == nil {
650 return fmt.Errorf("no channel ID negotiated")
651 }
652 if channelID.Curve != channelIDKey.Curve ||
653 channelIDKey.X.Cmp(channelIDKey.X) != 0 ||
654 channelIDKey.Y.Cmp(channelIDKey.Y) != 0 {
655 return fmt.Errorf("incorrect channel ID")
656 }
657 }
658
David Benjaminae2888f2014-09-06 12:58:58 -0400659 if expected := test.expectedNextProto; expected != "" {
660 if actual := tlsConn.ConnectionState().NegotiatedProtocol; actual != expected {
661 return fmt.Errorf("next proto mismatch: got %s, wanted %s", actual, expected)
662 }
663 }
664
David Benjaminfc7b0862014-09-06 13:21:53 -0400665 if test.expectedNextProtoType != 0 {
666 if (test.expectedNextProtoType == alpn) != tlsConn.ConnectionState().NegotiatedProtocolFromALPN {
667 return fmt.Errorf("next proto type mismatch")
668 }
669 }
670
David Benjaminca6c8262014-11-15 19:06:08 -0500671 if p := tlsConn.ConnectionState().SRTPProtectionProfile; p != test.expectedSRTPProtectionProfile {
672 return fmt.Errorf("SRTP profile mismatch: got %d, wanted %d", p, test.expectedSRTPProtectionProfile)
673 }
674
David Benjamine58c4f52014-08-24 03:47:07 -0400675 if test.shimWritesFirst {
676 var buf [5]byte
677 _, err := io.ReadFull(tlsConn, buf[:])
678 if err != nil {
679 return err
680 }
681 if string(buf[:]) != "hello" {
682 return fmt.Errorf("bad initial message")
683 }
684 }
685
Adam Langleycf2d4f42014-10-28 19:06:14 -0700686 if test.renegotiate {
687 if test.renegotiateCiphers != nil {
688 config.CipherSuites = test.renegotiateCiphers
689 }
690 if err := tlsConn.Renegotiate(); err != nil {
691 return err
692 }
693 } else if test.renegotiateCiphers != nil {
694 panic("renegotiateCiphers without renegotiate")
695 }
696
Kenny Root7fdeaf12014-08-05 15:23:37 -0700697 if messageLen < 0 {
David Benjamin6fd297b2014-08-11 18:43:38 -0400698 if test.protocol == dtls {
699 return fmt.Errorf("messageLen < 0 not supported for DTLS tests")
700 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700701 // Read until EOF.
702 _, err := io.Copy(ioutil.Discard, tlsConn)
703 return err
704 }
705
Adam Langley80842bd2014-06-20 12:00:00 -0700706 if messageLen == 0 {
707 messageLen = 32
708 }
709 testMessage := make([]byte, messageLen)
710 for i := range testMessage {
711 testMessage[i] = 0x42
712 }
Adam Langley95c29f32014-06-20 12:00:00 -0700713 tlsConn.Write(testMessage)
714
715 buf := make([]byte, len(testMessage))
David Benjamin6fd297b2014-08-11 18:43:38 -0400716 if test.protocol == dtls {
717 bufTmp := make([]byte, len(buf)+1)
718 n, err := tlsConn.Read(bufTmp)
719 if err != nil {
720 return err
721 }
722 if n != len(buf) {
723 return fmt.Errorf("bad reply; length mismatch (%d vs %d)", n, len(buf))
724 }
725 copy(buf, bufTmp)
726 } else {
727 _, err := io.ReadFull(tlsConn, buf)
728 if err != nil {
729 return err
730 }
Adam Langley95c29f32014-06-20 12:00:00 -0700731 }
732
733 for i, v := range buf {
734 if v != testMessage[i]^0xff {
735 return fmt.Errorf("bad reply contents at byte %d", i)
736 }
737 }
738
739 return nil
740}
741
David Benjamin325b5c32014-07-01 19:40:31 -0400742func valgrindOf(dbAttach bool, path string, args ...string) *exec.Cmd {
743 valgrindArgs := []string{"--error-exitcode=99", "--track-origins=yes", "--leak-check=full"}
Adam Langley95c29f32014-06-20 12:00:00 -0700744 if dbAttach {
David Benjamin325b5c32014-07-01 19:40:31 -0400745 valgrindArgs = append(valgrindArgs, "--db-attach=yes", "--db-command=xterm -e gdb -nw %f %p")
Adam Langley95c29f32014-06-20 12:00:00 -0700746 }
David Benjamin325b5c32014-07-01 19:40:31 -0400747 valgrindArgs = append(valgrindArgs, path)
748 valgrindArgs = append(valgrindArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -0700749
David Benjamin325b5c32014-07-01 19:40:31 -0400750 return exec.Command("valgrind", valgrindArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -0700751}
752
David Benjamin325b5c32014-07-01 19:40:31 -0400753func gdbOf(path string, args ...string) *exec.Cmd {
754 xtermArgs := []string{"-e", "gdb", "--args"}
755 xtermArgs = append(xtermArgs, path)
756 xtermArgs = append(xtermArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -0700757
David Benjamin325b5c32014-07-01 19:40:31 -0400758 return exec.Command("xterm", xtermArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -0700759}
760
David Benjamin1d5c83e2014-07-22 19:20:02 -0400761func openSocketPair() (shimEnd *os.File, conn net.Conn) {
Adam Langley95c29f32014-06-20 12:00:00 -0700762 socks, err := syscall.Socketpair(syscall.AF_UNIX, syscall.SOCK_STREAM, 0)
763 if err != nil {
764 panic(err)
765 }
766
767 syscall.CloseOnExec(socks[0])
768 syscall.CloseOnExec(socks[1])
David Benjamin1d5c83e2014-07-22 19:20:02 -0400769 shimEnd = os.NewFile(uintptr(socks[0]), "shim end")
Adam Langley95c29f32014-06-20 12:00:00 -0700770 connFile := os.NewFile(uintptr(socks[1]), "our end")
David Benjamin1d5c83e2014-07-22 19:20:02 -0400771 conn, err = net.FileConn(connFile)
772 if err != nil {
773 panic(err)
774 }
Adam Langley95c29f32014-06-20 12:00:00 -0700775 connFile.Close()
776 if err != nil {
777 panic(err)
778 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400779 return shimEnd, conn
780}
781
Adam Langley69a01602014-11-17 17:26:55 -0800782type moreMallocsError struct{}
783
784func (moreMallocsError) Error() string {
785 return "child process did not exhaust all allocation calls"
786}
787
788var errMoreMallocs = moreMallocsError{}
789
790func runTest(test *testCase, buildDir string, mallocNumToFail int64) error {
Adam Langley38311732014-10-16 19:04:35 -0700791 if !test.shouldFail && (len(test.expectedError) > 0 || len(test.expectedLocalError) > 0) {
792 panic("Error expected without shouldFail in " + test.name)
793 }
794
David Benjamin1d5c83e2014-07-22 19:20:02 -0400795 shimEnd, conn := openSocketPair()
796 shimEndResume, connResume := openSocketPair()
Adam Langley95c29f32014-06-20 12:00:00 -0700797
David Benjamin884fdf12014-08-02 15:28:23 -0400798 shim_path := path.Join(buildDir, "ssl/test/bssl_shim")
David Benjamin5a593af2014-08-11 19:51:50 -0400799 var flags []string
David Benjamin1d5c83e2014-07-22 19:20:02 -0400800 if test.testType == serverTest {
David Benjamin5a593af2014-08-11 19:51:50 -0400801 flags = append(flags, "-server")
802
David Benjamin025b3d32014-07-01 19:53:04 -0400803 flags = append(flags, "-key-file")
804 if test.keyFile == "" {
805 flags = append(flags, rsaKeyFile)
806 } else {
807 flags = append(flags, test.keyFile)
808 }
809
810 flags = append(flags, "-cert-file")
811 if test.certFile == "" {
812 flags = append(flags, rsaCertificateFile)
813 } else {
814 flags = append(flags, test.certFile)
815 }
816 }
David Benjamin5a593af2014-08-11 19:51:50 -0400817
David Benjamin6fd297b2014-08-11 18:43:38 -0400818 if test.protocol == dtls {
819 flags = append(flags, "-dtls")
820 }
821
David Benjamin5a593af2014-08-11 19:51:50 -0400822 if test.resumeSession {
823 flags = append(flags, "-resume")
824 }
825
David Benjamine58c4f52014-08-24 03:47:07 -0400826 if test.shimWritesFirst {
827 flags = append(flags, "-shim-writes-first")
828 }
829
David Benjamin025b3d32014-07-01 19:53:04 -0400830 flags = append(flags, test.flags...)
831
832 var shim *exec.Cmd
833 if *useValgrind {
834 shim = valgrindOf(false, shim_path, flags...)
Adam Langley75712922014-10-10 16:23:43 -0700835 } else if *useGDB {
836 shim = gdbOf(shim_path, flags...)
David Benjamin025b3d32014-07-01 19:53:04 -0400837 } else {
838 shim = exec.Command(shim_path, flags...)
839 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400840 shim.ExtraFiles = []*os.File{shimEnd, shimEndResume}
David Benjamin025b3d32014-07-01 19:53:04 -0400841 shim.Stdin = os.Stdin
842 var stdoutBuf, stderrBuf bytes.Buffer
843 shim.Stdout = &stdoutBuf
844 shim.Stderr = &stderrBuf
Adam Langley69a01602014-11-17 17:26:55 -0800845 if mallocNumToFail >= 0 {
846 shim.Env = []string{"MALLOC_NUMBER_TO_FAIL=" + strconv.FormatInt(mallocNumToFail, 10)}
847 if *mallocTestDebug {
848 shim.Env = append(shim.Env, "MALLOC_ABORT_ON_FAIL=1")
849 }
850 shim.Env = append(shim.Env, "_MALLOC_CHECK=1")
851 }
David Benjamin025b3d32014-07-01 19:53:04 -0400852
853 if err := shim.Start(); err != nil {
Adam Langley95c29f32014-06-20 12:00:00 -0700854 panic(err)
855 }
David Benjamin025b3d32014-07-01 19:53:04 -0400856 shimEnd.Close()
David Benjamin1d5c83e2014-07-22 19:20:02 -0400857 shimEndResume.Close()
Adam Langley95c29f32014-06-20 12:00:00 -0700858
859 config := test.config
David Benjamin1d5c83e2014-07-22 19:20:02 -0400860 config.ClientSessionCache = NewLRUClientSessionCache(1)
David Benjaminfe8eb9a2014-11-17 03:19:02 -0500861 config.ServerSessionCache = NewLRUServerSessionCache(1)
David Benjamin025b3d32014-07-01 19:53:04 -0400862 if test.testType == clientTest {
863 if len(config.Certificates) == 0 {
864 config.Certificates = []Certificate{getRSACertificate()}
865 }
David Benjamin025b3d32014-07-01 19:53:04 -0400866 }
Adam Langley95c29f32014-06-20 12:00:00 -0700867
David Benjamin01fe8202014-09-24 15:21:44 -0400868 err := doExchange(test, &config, conn, test.messageLen,
869 false /* not a resumption */)
Adam Langley95c29f32014-06-20 12:00:00 -0700870 conn.Close()
David Benjamin65ea8ff2014-11-23 03:01:00 -0500871
David Benjamin1d5c83e2014-07-22 19:20:02 -0400872 if err == nil && test.resumeSession {
David Benjamin01fe8202014-09-24 15:21:44 -0400873 var resumeConfig Config
874 if test.resumeConfig != nil {
875 resumeConfig = *test.resumeConfig
876 if len(resumeConfig.Certificates) == 0 {
877 resumeConfig.Certificates = []Certificate{getRSACertificate()}
878 }
David Benjaminfe8eb9a2014-11-17 03:19:02 -0500879 if !test.newSessionsOnResume {
880 resumeConfig.SessionTicketKey = config.SessionTicketKey
881 resumeConfig.ClientSessionCache = config.ClientSessionCache
882 resumeConfig.ServerSessionCache = config.ServerSessionCache
883 }
David Benjamin01fe8202014-09-24 15:21:44 -0400884 } else {
885 resumeConfig = config
886 }
887 err = doExchange(test, &resumeConfig, connResume, test.messageLen,
888 true /* resumption */)
David Benjamin1d5c83e2014-07-22 19:20:02 -0400889 }
David Benjamin812152a2014-09-06 12:49:07 -0400890 connResume.Close()
David Benjamin1d5c83e2014-07-22 19:20:02 -0400891
David Benjamin025b3d32014-07-01 19:53:04 -0400892 childErr := shim.Wait()
Adam Langley69a01602014-11-17 17:26:55 -0800893 if exitError, ok := childErr.(*exec.ExitError); ok {
894 if exitError.Sys().(syscall.WaitStatus).ExitStatus() == 88 {
895 return errMoreMallocs
896 }
897 }
Adam Langley95c29f32014-06-20 12:00:00 -0700898
899 stdout := string(stdoutBuf.Bytes())
900 stderr := string(stderrBuf.Bytes())
901 failed := err != nil || childErr != nil
902 correctFailure := len(test.expectedError) == 0 || strings.Contains(stdout, test.expectedError)
Adam Langleyac61fa32014-06-23 12:03:11 -0700903 localError := "none"
904 if err != nil {
905 localError = err.Error()
906 }
907 if len(test.expectedLocalError) != 0 {
908 correctFailure = correctFailure && strings.Contains(localError, test.expectedLocalError)
909 }
Adam Langley95c29f32014-06-20 12:00:00 -0700910
911 if failed != test.shouldFail || failed && !correctFailure {
Adam Langley95c29f32014-06-20 12:00:00 -0700912 childError := "none"
Adam Langley95c29f32014-06-20 12:00:00 -0700913 if childErr != nil {
914 childError = childErr.Error()
915 }
916
917 var msg string
918 switch {
919 case failed && !test.shouldFail:
920 msg = "unexpected failure"
921 case !failed && test.shouldFail:
922 msg = "unexpected success"
923 case failed && !correctFailure:
Adam Langleyac61fa32014-06-23 12:03:11 -0700924 msg = "bad error (wanted '" + test.expectedError + "' / '" + test.expectedLocalError + "')"
Adam Langley95c29f32014-06-20 12:00:00 -0700925 default:
926 panic("internal error")
927 }
928
929 return fmt.Errorf("%s: local error '%s', child error '%s', stdout:\n%s\nstderr:\n%s", msg, localError, childError, string(stdoutBuf.Bytes()), stderr)
930 }
931
932 if !*useValgrind && len(stderr) > 0 {
933 println(stderr)
934 }
935
936 return nil
937}
938
939var tlsVersions = []struct {
940 name string
941 version uint16
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400942 flag string
David Benjamin8b8c0062014-11-23 02:47:52 -0500943 hasDTLS bool
Adam Langley95c29f32014-06-20 12:00:00 -0700944}{
David Benjamin8b8c0062014-11-23 02:47:52 -0500945 {"SSL3", VersionSSL30, "-no-ssl3", false},
946 {"TLS1", VersionTLS10, "-no-tls1", true},
947 {"TLS11", VersionTLS11, "-no-tls11", false},
948 {"TLS12", VersionTLS12, "-no-tls12", true},
Adam Langley95c29f32014-06-20 12:00:00 -0700949}
950
951var testCipherSuites = []struct {
952 name string
953 id uint16
954}{
955 {"3DES-SHA", TLS_RSA_WITH_3DES_EDE_CBC_SHA},
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400956 {"AES128-GCM", TLS_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -0700957 {"AES128-SHA", TLS_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -0400958 {"AES128-SHA256", TLS_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400959 {"AES256-GCM", TLS_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -0700960 {"AES256-SHA", TLS_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -0400961 {"AES256-SHA256", TLS_RSA_WITH_AES_256_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400962 {"DHE-RSA-AES128-GCM", TLS_DHE_RSA_WITH_AES_128_GCM_SHA256},
963 {"DHE-RSA-AES128-SHA", TLS_DHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -0400964 {"DHE-RSA-AES128-SHA256", TLS_DHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400965 {"DHE-RSA-AES256-GCM", TLS_DHE_RSA_WITH_AES_256_GCM_SHA384},
966 {"DHE-RSA-AES256-SHA", TLS_DHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -0400967 {"DHE-RSA-AES256-SHA256", TLS_DHE_RSA_WITH_AES_256_CBC_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -0700968 {"ECDHE-ECDSA-AES128-GCM", TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
969 {"ECDHE-ECDSA-AES128-SHA", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -0400970 {"ECDHE-ECDSA-AES128-SHA256", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256},
971 {"ECDHE-ECDSA-AES256-GCM", TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -0700972 {"ECDHE-ECDSA-AES256-SHA", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -0400973 {"ECDHE-ECDSA-AES256-SHA384", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -0700974 {"ECDHE-ECDSA-RC4-SHA", TLS_ECDHE_ECDSA_WITH_RC4_128_SHA},
David Benjamin2af684f2014-10-27 02:23:15 -0400975 {"ECDHE-PSK-WITH-AES-128-GCM-SHA256", TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -0700976 {"ECDHE-RSA-AES128-GCM", TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -0700977 {"ECDHE-RSA-AES128-SHA", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -0400978 {"ECDHE-RSA-AES128-SHA256", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400979 {"ECDHE-RSA-AES256-GCM", TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -0700980 {"ECDHE-RSA-AES256-SHA", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -0400981 {"ECDHE-RSA-AES256-SHA384", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -0700982 {"ECDHE-RSA-RC4-SHA", TLS_ECDHE_RSA_WITH_RC4_128_SHA},
David Benjamin48cae082014-10-27 01:06:24 -0400983 {"PSK-AES128-CBC-SHA", TLS_PSK_WITH_AES_128_CBC_SHA},
984 {"PSK-AES256-CBC-SHA", TLS_PSK_WITH_AES_256_CBC_SHA},
985 {"PSK-RC4-SHA", TLS_PSK_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -0700986 {"RC4-MD5", TLS_RSA_WITH_RC4_128_MD5},
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400987 {"RC4-SHA", TLS_RSA_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -0700988}
989
David Benjamin8b8c0062014-11-23 02:47:52 -0500990func hasComponent(suiteName, component string) bool {
991 return strings.Contains("-"+suiteName+"-", "-"+component+"-")
992}
993
David Benjaminf7768e42014-08-31 02:06:47 -0400994func isTLS12Only(suiteName string) bool {
David Benjamin8b8c0062014-11-23 02:47:52 -0500995 return hasComponent(suiteName, "GCM") ||
996 hasComponent(suiteName, "SHA256") ||
997 hasComponent(suiteName, "SHA384")
998}
999
1000func isDTLSCipher(suiteName string) bool {
1001 // TODO(davidben): AES-GCM exists in DTLS 1.2 but is currently
1002 // broken because DTLS is not EVP_AEAD-aware.
1003 return !hasComponent(suiteName, "RC4") &&
1004 !hasComponent(suiteName, "GCM")
David Benjaminf7768e42014-08-31 02:06:47 -04001005}
1006
Adam Langley95c29f32014-06-20 12:00:00 -07001007func addCipherSuiteTests() {
1008 for _, suite := range testCipherSuites {
David Benjamin48cae082014-10-27 01:06:24 -04001009 const psk = "12345"
1010 const pskIdentity = "luggage combo"
1011
Adam Langley95c29f32014-06-20 12:00:00 -07001012 var cert Certificate
David Benjamin025b3d32014-07-01 19:53:04 -04001013 var certFile string
1014 var keyFile string
David Benjamin8b8c0062014-11-23 02:47:52 -05001015 if hasComponent(suite.name, "ECDSA") {
Adam Langley95c29f32014-06-20 12:00:00 -07001016 cert = getECDSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -04001017 certFile = ecdsaCertificateFile
1018 keyFile = ecdsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -07001019 } else {
1020 cert = getRSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -04001021 certFile = rsaCertificateFile
1022 keyFile = rsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -07001023 }
1024
David Benjamin48cae082014-10-27 01:06:24 -04001025 var flags []string
David Benjamin8b8c0062014-11-23 02:47:52 -05001026 if hasComponent(suite.name, "PSK") {
David Benjamin48cae082014-10-27 01:06:24 -04001027 flags = append(flags,
1028 "-psk", psk,
1029 "-psk-identity", pskIdentity)
1030 }
1031
Adam Langley95c29f32014-06-20 12:00:00 -07001032 for _, ver := range tlsVersions {
David Benjaminf7768e42014-08-31 02:06:47 -04001033 if ver.version < VersionTLS12 && isTLS12Only(suite.name) {
Adam Langley95c29f32014-06-20 12:00:00 -07001034 continue
1035 }
1036
David Benjamin025b3d32014-07-01 19:53:04 -04001037 testCases = append(testCases, testCase{
1038 testType: clientTest,
1039 name: ver.name + "-" + suite.name + "-client",
Adam Langley95c29f32014-06-20 12:00:00 -07001040 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001041 MinVersion: ver.version,
1042 MaxVersion: ver.version,
1043 CipherSuites: []uint16{suite.id},
1044 Certificates: []Certificate{cert},
1045 PreSharedKey: []byte(psk),
1046 PreSharedKeyIdentity: pskIdentity,
Adam Langley95c29f32014-06-20 12:00:00 -07001047 },
David Benjamin48cae082014-10-27 01:06:24 -04001048 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001049 resumeSession: true,
Adam Langley95c29f32014-06-20 12:00:00 -07001050 })
David Benjamin025b3d32014-07-01 19:53:04 -04001051
David Benjamin76d8abe2014-08-14 16:25:34 -04001052 testCases = append(testCases, testCase{
1053 testType: serverTest,
1054 name: ver.name + "-" + suite.name + "-server",
1055 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001056 MinVersion: ver.version,
1057 MaxVersion: ver.version,
1058 CipherSuites: []uint16{suite.id},
1059 Certificates: []Certificate{cert},
1060 PreSharedKey: []byte(psk),
1061 PreSharedKeyIdentity: pskIdentity,
David Benjamin76d8abe2014-08-14 16:25:34 -04001062 },
1063 certFile: certFile,
1064 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001065 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001066 resumeSession: true,
David Benjamin76d8abe2014-08-14 16:25:34 -04001067 })
David Benjamin6fd297b2014-08-11 18:43:38 -04001068
David Benjamin8b8c0062014-11-23 02:47:52 -05001069 if ver.hasDTLS && isDTLSCipher(suite.name) {
David Benjamin6fd297b2014-08-11 18:43:38 -04001070 testCases = append(testCases, testCase{
1071 testType: clientTest,
1072 protocol: dtls,
1073 name: "D" + ver.name + "-" + suite.name + "-client",
1074 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001075 MinVersion: ver.version,
1076 MaxVersion: ver.version,
1077 CipherSuites: []uint16{suite.id},
1078 Certificates: []Certificate{cert},
1079 PreSharedKey: []byte(psk),
1080 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001081 },
David Benjamin48cae082014-10-27 01:06:24 -04001082 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001083 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001084 })
1085 testCases = append(testCases, testCase{
1086 testType: serverTest,
1087 protocol: dtls,
1088 name: "D" + ver.name + "-" + suite.name + "-server",
1089 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001090 MinVersion: ver.version,
1091 MaxVersion: ver.version,
1092 CipherSuites: []uint16{suite.id},
1093 Certificates: []Certificate{cert},
1094 PreSharedKey: []byte(psk),
1095 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001096 },
1097 certFile: certFile,
1098 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001099 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001100 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001101 })
1102 }
Adam Langley95c29f32014-06-20 12:00:00 -07001103 }
1104 }
1105}
1106
1107func addBadECDSASignatureTests() {
1108 for badR := BadValue(1); badR < NumBadValues; badR++ {
1109 for badS := BadValue(1); badS < NumBadValues; badS++ {
David Benjamin025b3d32014-07-01 19:53:04 -04001110 testCases = append(testCases, testCase{
Adam Langley95c29f32014-06-20 12:00:00 -07001111 name: fmt.Sprintf("BadECDSA-%d-%d", badR, badS),
1112 config: Config{
1113 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
1114 Certificates: []Certificate{getECDSACertificate()},
1115 Bugs: ProtocolBugs{
1116 BadECDSAR: badR,
1117 BadECDSAS: badS,
1118 },
1119 },
1120 shouldFail: true,
1121 expectedError: "SIGNATURE",
1122 })
1123 }
1124 }
1125}
1126
Adam Langley80842bd2014-06-20 12:00:00 -07001127func addCBCPaddingTests() {
David Benjamin025b3d32014-07-01 19:53:04 -04001128 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001129 name: "MaxCBCPadding",
1130 config: Config{
1131 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1132 Bugs: ProtocolBugs{
1133 MaxPadding: true,
1134 },
1135 },
1136 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1137 })
David Benjamin025b3d32014-07-01 19:53:04 -04001138 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001139 name: "BadCBCPadding",
1140 config: Config{
1141 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1142 Bugs: ProtocolBugs{
1143 PaddingFirstByteBad: true,
1144 },
1145 },
1146 shouldFail: true,
1147 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1148 })
1149 // OpenSSL previously had an issue where the first byte of padding in
1150 // 255 bytes of padding wasn't checked.
David Benjamin025b3d32014-07-01 19:53:04 -04001151 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001152 name: "BadCBCPadding255",
1153 config: Config{
1154 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1155 Bugs: ProtocolBugs{
1156 MaxPadding: true,
1157 PaddingFirstByteBadIf255: true,
1158 },
1159 },
1160 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1161 shouldFail: true,
1162 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1163 })
1164}
1165
Kenny Root7fdeaf12014-08-05 15:23:37 -07001166func addCBCSplittingTests() {
1167 testCases = append(testCases, testCase{
1168 name: "CBCRecordSplitting",
1169 config: Config{
1170 MaxVersion: VersionTLS10,
1171 MinVersion: VersionTLS10,
1172 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1173 },
1174 messageLen: -1, // read until EOF
1175 flags: []string{
1176 "-async",
1177 "-write-different-record-sizes",
1178 "-cbc-record-splitting",
1179 },
David Benjamina8e3e0e2014-08-06 22:11:10 -04001180 })
1181 testCases = append(testCases, testCase{
Kenny Root7fdeaf12014-08-05 15:23:37 -07001182 name: "CBCRecordSplittingPartialWrite",
1183 config: Config{
1184 MaxVersion: VersionTLS10,
1185 MinVersion: VersionTLS10,
1186 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1187 },
1188 messageLen: -1, // read until EOF
1189 flags: []string{
1190 "-async",
1191 "-write-different-record-sizes",
1192 "-cbc-record-splitting",
1193 "-partial-write",
1194 },
1195 })
1196}
1197
David Benjamin636293b2014-07-08 17:59:18 -04001198func addClientAuthTests() {
David Benjamin407a10c2014-07-16 12:58:59 -04001199 // Add a dummy cert pool to stress certificate authority parsing.
1200 // TODO(davidben): Add tests that those values parse out correctly.
1201 certPool := x509.NewCertPool()
1202 cert, err := x509.ParseCertificate(rsaCertificate.Certificate[0])
1203 if err != nil {
1204 panic(err)
1205 }
1206 certPool.AddCert(cert)
1207
David Benjamin636293b2014-07-08 17:59:18 -04001208 for _, ver := range tlsVersions {
David Benjamin636293b2014-07-08 17:59:18 -04001209 testCases = append(testCases, testCase{
1210 testType: clientTest,
David Benjamin67666e72014-07-12 15:47:52 -04001211 name: ver.name + "-Client-ClientAuth-RSA",
David Benjamin636293b2014-07-08 17:59:18 -04001212 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001213 MinVersion: ver.version,
1214 MaxVersion: ver.version,
1215 ClientAuth: RequireAnyClientCert,
1216 ClientCAs: certPool,
David Benjamin636293b2014-07-08 17:59:18 -04001217 },
1218 flags: []string{
1219 "-cert-file", rsaCertificateFile,
1220 "-key-file", rsaKeyFile,
1221 },
1222 })
1223 testCases = append(testCases, testCase{
David Benjamin67666e72014-07-12 15:47:52 -04001224 testType: serverTest,
1225 name: ver.name + "-Server-ClientAuth-RSA",
1226 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001227 MinVersion: ver.version,
1228 MaxVersion: ver.version,
David Benjamin67666e72014-07-12 15:47:52 -04001229 Certificates: []Certificate{rsaCertificate},
1230 },
1231 flags: []string{"-require-any-client-certificate"},
1232 })
David Benjamine098ec22014-08-27 23:13:20 -04001233 if ver.version != VersionSSL30 {
1234 testCases = append(testCases, testCase{
1235 testType: serverTest,
1236 name: ver.name + "-Server-ClientAuth-ECDSA",
1237 config: Config{
1238 MinVersion: ver.version,
1239 MaxVersion: ver.version,
1240 Certificates: []Certificate{ecdsaCertificate},
1241 },
1242 flags: []string{"-require-any-client-certificate"},
1243 })
1244 testCases = append(testCases, testCase{
1245 testType: clientTest,
1246 name: ver.name + "-Client-ClientAuth-ECDSA",
1247 config: Config{
1248 MinVersion: ver.version,
1249 MaxVersion: ver.version,
1250 ClientAuth: RequireAnyClientCert,
1251 ClientCAs: certPool,
1252 },
1253 flags: []string{
1254 "-cert-file", ecdsaCertificateFile,
1255 "-key-file", ecdsaKeyFile,
1256 },
1257 })
1258 }
David Benjamin636293b2014-07-08 17:59:18 -04001259 }
1260}
1261
Adam Langley75712922014-10-10 16:23:43 -07001262func addExtendedMasterSecretTests() {
1263 const expectEMSFlag = "-expect-extended-master-secret"
1264
1265 for _, with := range []bool{false, true} {
1266 prefix := "No"
1267 var flags []string
1268 if with {
1269 prefix = ""
1270 flags = []string{expectEMSFlag}
1271 }
1272
1273 for _, isClient := range []bool{false, true} {
1274 suffix := "-Server"
1275 testType := serverTest
1276 if isClient {
1277 suffix = "-Client"
1278 testType = clientTest
1279 }
1280
1281 for _, ver := range tlsVersions {
1282 test := testCase{
1283 testType: testType,
1284 name: prefix + "ExtendedMasterSecret-" + ver.name + suffix,
1285 config: Config{
1286 MinVersion: ver.version,
1287 MaxVersion: ver.version,
1288 Bugs: ProtocolBugs{
1289 NoExtendedMasterSecret: !with,
1290 RequireExtendedMasterSecret: with,
1291 },
1292 },
David Benjamin48cae082014-10-27 01:06:24 -04001293 flags: flags,
1294 shouldFail: ver.version == VersionSSL30 && with,
Adam Langley75712922014-10-10 16:23:43 -07001295 }
1296 if test.shouldFail {
1297 test.expectedLocalError = "extended master secret required but not supported by peer"
1298 }
1299 testCases = append(testCases, test)
1300 }
1301 }
1302 }
1303
1304 // When a session is resumed, it should still be aware that its master
1305 // secret was generated via EMS and thus it's safe to use tls-unique.
1306 testCases = append(testCases, testCase{
1307 name: "ExtendedMasterSecret-Resume",
1308 config: Config{
1309 Bugs: ProtocolBugs{
1310 RequireExtendedMasterSecret: true,
1311 },
1312 },
1313 flags: []string{expectEMSFlag},
1314 resumeSession: true,
1315 })
1316}
1317
David Benjamin43ec06f2014-08-05 02:28:57 -04001318// Adds tests that try to cover the range of the handshake state machine, under
1319// various conditions. Some of these are redundant with other tests, but they
1320// only cover the synchronous case.
David Benjamin6fd297b2014-08-11 18:43:38 -04001321func addStateMachineCoverageTests(async, splitHandshake bool, protocol protocol) {
David Benjamin43ec06f2014-08-05 02:28:57 -04001322 var suffix string
1323 var flags []string
1324 var maxHandshakeRecordLength int
David Benjamin6fd297b2014-08-11 18:43:38 -04001325 if protocol == dtls {
1326 suffix = "-DTLS"
1327 }
David Benjamin43ec06f2014-08-05 02:28:57 -04001328 if async {
David Benjamin6fd297b2014-08-11 18:43:38 -04001329 suffix += "-Async"
David Benjamin43ec06f2014-08-05 02:28:57 -04001330 flags = append(flags, "-async")
1331 } else {
David Benjamin6fd297b2014-08-11 18:43:38 -04001332 suffix += "-Sync"
David Benjamin43ec06f2014-08-05 02:28:57 -04001333 }
1334 if splitHandshake {
1335 suffix += "-SplitHandshakeRecords"
David Benjamin98214542014-08-07 18:02:39 -04001336 maxHandshakeRecordLength = 1
David Benjamin43ec06f2014-08-05 02:28:57 -04001337 }
1338
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001339 // Basic handshake, with resumption. Client and server,
1340 // session ID and session ticket.
David Benjamin43ec06f2014-08-05 02:28:57 -04001341 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001342 protocol: protocol,
1343 name: "Basic-Client" + suffix,
David Benjamin43ec06f2014-08-05 02:28:57 -04001344 config: Config{
1345 Bugs: ProtocolBugs{
1346 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1347 },
1348 },
David Benjaminbed9aae2014-08-07 19:13:38 -04001349 flags: flags,
1350 resumeSession: true,
1351 })
1352 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001353 protocol: protocol,
1354 name: "Basic-Client-RenewTicket" + suffix,
David Benjaminbed9aae2014-08-07 19:13:38 -04001355 config: Config{
1356 Bugs: ProtocolBugs{
1357 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1358 RenewTicketOnResume: true,
1359 },
1360 },
1361 flags: flags,
1362 resumeSession: true,
David Benjamin43ec06f2014-08-05 02:28:57 -04001363 })
1364 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001365 protocol: protocol,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001366 name: "Basic-Client-NoTicket" + suffix,
1367 config: Config{
1368 SessionTicketsDisabled: true,
1369 Bugs: ProtocolBugs{
1370 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1371 },
1372 },
1373 flags: flags,
1374 resumeSession: true,
1375 })
1376 testCases = append(testCases, testCase{
1377 protocol: protocol,
David Benjamin43ec06f2014-08-05 02:28:57 -04001378 testType: serverTest,
1379 name: "Basic-Server" + suffix,
1380 config: Config{
1381 Bugs: ProtocolBugs{
1382 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1383 },
1384 },
David Benjaminbed9aae2014-08-07 19:13:38 -04001385 flags: flags,
1386 resumeSession: true,
David Benjamin43ec06f2014-08-05 02:28:57 -04001387 })
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001388 testCases = append(testCases, testCase{
1389 protocol: protocol,
1390 testType: serverTest,
1391 name: "Basic-Server-NoTickets" + suffix,
1392 config: Config{
1393 SessionTicketsDisabled: true,
1394 Bugs: ProtocolBugs{
1395 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1396 },
1397 },
1398 flags: flags,
1399 resumeSession: true,
1400 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001401
David Benjamin6fd297b2014-08-11 18:43:38 -04001402 // TLS client auth.
1403 testCases = append(testCases, testCase{
1404 protocol: protocol,
1405 testType: clientTest,
1406 name: "ClientAuth-Client" + suffix,
1407 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001408 ClientAuth: RequireAnyClientCert,
David Benjamin6fd297b2014-08-11 18:43:38 -04001409 Bugs: ProtocolBugs{
1410 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1411 },
1412 },
1413 flags: append(flags,
1414 "-cert-file", rsaCertificateFile,
1415 "-key-file", rsaKeyFile),
1416 })
1417 testCases = append(testCases, testCase{
1418 protocol: protocol,
1419 testType: serverTest,
1420 name: "ClientAuth-Server" + suffix,
1421 config: Config{
1422 Certificates: []Certificate{rsaCertificate},
1423 },
1424 flags: append(flags, "-require-any-client-certificate"),
1425 })
1426
David Benjamin43ec06f2014-08-05 02:28:57 -04001427 // No session ticket support; server doesn't send NewSessionTicket.
1428 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001429 protocol: protocol,
1430 name: "SessionTicketsDisabled-Client" + suffix,
David Benjamin43ec06f2014-08-05 02:28:57 -04001431 config: Config{
1432 SessionTicketsDisabled: true,
1433 Bugs: ProtocolBugs{
1434 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1435 },
1436 },
1437 flags: flags,
1438 })
1439 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001440 protocol: protocol,
David Benjamin43ec06f2014-08-05 02:28:57 -04001441 testType: serverTest,
1442 name: "SessionTicketsDisabled-Server" + suffix,
1443 config: Config{
1444 SessionTicketsDisabled: true,
1445 Bugs: ProtocolBugs{
1446 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1447 },
1448 },
1449 flags: flags,
1450 })
1451
David Benjamin48cae082014-10-27 01:06:24 -04001452 // Skip ServerKeyExchange in PSK key exchange if there's no
1453 // identity hint.
1454 testCases = append(testCases, testCase{
1455 protocol: protocol,
1456 name: "EmptyPSKHint-Client" + suffix,
1457 config: Config{
1458 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
1459 PreSharedKey: []byte("secret"),
1460 Bugs: ProtocolBugs{
1461 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1462 },
1463 },
1464 flags: append(flags, "-psk", "secret"),
1465 })
1466 testCases = append(testCases, testCase{
1467 protocol: protocol,
1468 testType: serverTest,
1469 name: "EmptyPSKHint-Server" + suffix,
1470 config: Config{
1471 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
1472 PreSharedKey: []byte("secret"),
1473 Bugs: ProtocolBugs{
1474 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1475 },
1476 },
1477 flags: append(flags, "-psk", "secret"),
1478 })
1479
David Benjamin6fd297b2014-08-11 18:43:38 -04001480 if protocol == tls {
1481 // NPN on client and server; results in post-handshake message.
1482 testCases = append(testCases, testCase{
1483 protocol: protocol,
1484 name: "NPN-Client" + suffix,
1485 config: Config{
David Benjaminae2888f2014-09-06 12:58:58 -04001486 NextProtos: []string{"foo"},
David Benjamin6fd297b2014-08-11 18:43:38 -04001487 Bugs: ProtocolBugs{
1488 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1489 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001490 },
David Benjaminfc7b0862014-09-06 13:21:53 -04001491 flags: append(flags, "-select-next-proto", "foo"),
1492 expectedNextProto: "foo",
1493 expectedNextProtoType: npn,
David Benjamin6fd297b2014-08-11 18:43:38 -04001494 })
1495 testCases = append(testCases, testCase{
1496 protocol: protocol,
1497 testType: serverTest,
1498 name: "NPN-Server" + suffix,
1499 config: Config{
1500 NextProtos: []string{"bar"},
1501 Bugs: ProtocolBugs{
1502 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1503 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001504 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001505 flags: append(flags,
1506 "-advertise-npn", "\x03foo\x03bar\x03baz",
1507 "-expect-next-proto", "bar"),
David Benjaminfc7b0862014-09-06 13:21:53 -04001508 expectedNextProto: "bar",
1509 expectedNextProtoType: npn,
David Benjamin6fd297b2014-08-11 18:43:38 -04001510 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001511
David Benjamin6fd297b2014-08-11 18:43:38 -04001512 // Client does False Start and negotiates NPN.
1513 testCases = append(testCases, testCase{
1514 protocol: protocol,
1515 name: "FalseStart" + suffix,
1516 config: Config{
1517 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1518 NextProtos: []string{"foo"},
1519 Bugs: ProtocolBugs{
David Benjamine58c4f52014-08-24 03:47:07 -04001520 ExpectFalseStart: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001521 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1522 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001523 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001524 flags: append(flags,
1525 "-false-start",
1526 "-select-next-proto", "foo"),
David Benjamine58c4f52014-08-24 03:47:07 -04001527 shimWritesFirst: true,
1528 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001529 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001530
David Benjaminae2888f2014-09-06 12:58:58 -04001531 // Client does False Start and negotiates ALPN.
1532 testCases = append(testCases, testCase{
1533 protocol: protocol,
1534 name: "FalseStart-ALPN" + suffix,
1535 config: Config{
1536 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1537 NextProtos: []string{"foo"},
1538 Bugs: ProtocolBugs{
1539 ExpectFalseStart: true,
1540 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1541 },
1542 },
1543 flags: append(flags,
1544 "-false-start",
1545 "-advertise-alpn", "\x03foo"),
1546 shimWritesFirst: true,
1547 resumeSession: true,
1548 })
1549
David Benjamin6fd297b2014-08-11 18:43:38 -04001550 // False Start without session tickets.
1551 testCases = append(testCases, testCase{
1552 name: "FalseStart-SessionTicketsDisabled",
1553 config: Config{
1554 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1555 NextProtos: []string{"foo"},
1556 SessionTicketsDisabled: true,
David Benjamin4e99c522014-08-24 01:45:30 -04001557 Bugs: ProtocolBugs{
David Benjamine58c4f52014-08-24 03:47:07 -04001558 ExpectFalseStart: true,
David Benjamin4e99c522014-08-24 01:45:30 -04001559 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1560 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001561 },
David Benjamin4e99c522014-08-24 01:45:30 -04001562 flags: append(flags,
David Benjamin6fd297b2014-08-11 18:43:38 -04001563 "-false-start",
1564 "-select-next-proto", "foo",
David Benjamin4e99c522014-08-24 01:45:30 -04001565 ),
David Benjamine58c4f52014-08-24 03:47:07 -04001566 shimWritesFirst: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001567 })
David Benjamin1e7f8d72014-08-08 12:27:04 -04001568
David Benjamina08e49d2014-08-24 01:46:07 -04001569 // Server parses a V2ClientHello.
David Benjamin6fd297b2014-08-11 18:43:38 -04001570 testCases = append(testCases, testCase{
1571 protocol: protocol,
1572 testType: serverTest,
1573 name: "SendV2ClientHello" + suffix,
1574 config: Config{
1575 // Choose a cipher suite that does not involve
1576 // elliptic curves, so no extensions are
1577 // involved.
1578 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
1579 Bugs: ProtocolBugs{
1580 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1581 SendV2ClientHello: true,
1582 },
David Benjamin1e7f8d72014-08-08 12:27:04 -04001583 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001584 flags: flags,
1585 })
David Benjamina08e49d2014-08-24 01:46:07 -04001586
1587 // Client sends a Channel ID.
1588 testCases = append(testCases, testCase{
1589 protocol: protocol,
1590 name: "ChannelID-Client" + suffix,
1591 config: Config{
1592 RequestChannelID: true,
1593 Bugs: ProtocolBugs{
1594 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1595 },
1596 },
1597 flags: append(flags,
1598 "-send-channel-id", channelIDKeyFile,
1599 ),
1600 resumeSession: true,
1601 expectChannelID: true,
1602 })
1603
1604 // Server accepts a Channel ID.
1605 testCases = append(testCases, testCase{
1606 protocol: protocol,
1607 testType: serverTest,
1608 name: "ChannelID-Server" + suffix,
1609 config: Config{
1610 ChannelID: channelIDKey,
1611 Bugs: ProtocolBugs{
1612 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1613 },
1614 },
1615 flags: append(flags,
1616 "-expect-channel-id",
1617 base64.StdEncoding.EncodeToString(channelIDBytes),
1618 ),
1619 resumeSession: true,
1620 expectChannelID: true,
1621 })
David Benjamin6fd297b2014-08-11 18:43:38 -04001622 } else {
1623 testCases = append(testCases, testCase{
1624 protocol: protocol,
1625 name: "SkipHelloVerifyRequest" + suffix,
1626 config: Config{
1627 Bugs: ProtocolBugs{
1628 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1629 SkipHelloVerifyRequest: true,
1630 },
1631 },
1632 flags: flags,
1633 })
1634
1635 testCases = append(testCases, testCase{
1636 testType: serverTest,
1637 protocol: protocol,
1638 name: "CookieExchange" + suffix,
1639 config: Config{
1640 Bugs: ProtocolBugs{
1641 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1642 },
1643 },
1644 flags: append(flags, "-cookie-exchange"),
1645 })
1646 }
David Benjamin43ec06f2014-08-05 02:28:57 -04001647}
1648
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001649func addVersionNegotiationTests() {
1650 for i, shimVers := range tlsVersions {
1651 // Assemble flags to disable all newer versions on the shim.
1652 var flags []string
1653 for _, vers := range tlsVersions[i+1:] {
1654 flags = append(flags, vers.flag)
1655 }
1656
1657 for _, runnerVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05001658 protocols := []protocol{tls}
1659 if runnerVers.hasDTLS && shimVers.hasDTLS {
1660 protocols = append(protocols, dtls)
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001661 }
David Benjamin8b8c0062014-11-23 02:47:52 -05001662 for _, protocol := range protocols {
1663 expectedVersion := shimVers.version
1664 if runnerVers.version < shimVers.version {
1665 expectedVersion = runnerVers.version
1666 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001667
David Benjamin8b8c0062014-11-23 02:47:52 -05001668 suffix := shimVers.name + "-" + runnerVers.name
1669 if protocol == dtls {
1670 suffix += "-DTLS"
1671 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001672
David Benjamin1eb367c2014-12-12 18:17:51 -05001673 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
1674
David Benjamin1e29a6b2014-12-10 02:27:24 -05001675 clientVers := shimVers.version
1676 if clientVers > VersionTLS10 {
1677 clientVers = VersionTLS10
1678 }
David Benjamin8b8c0062014-11-23 02:47:52 -05001679 testCases = append(testCases, testCase{
1680 protocol: protocol,
1681 testType: clientTest,
1682 name: "VersionNegotiation-Client-" + suffix,
1683 config: Config{
1684 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05001685 Bugs: ProtocolBugs{
1686 ExpectInitialRecordVersion: clientVers,
1687 },
David Benjamin8b8c0062014-11-23 02:47:52 -05001688 },
1689 flags: flags,
1690 expectedVersion: expectedVersion,
1691 })
David Benjamin1eb367c2014-12-12 18:17:51 -05001692 testCases = append(testCases, testCase{
1693 protocol: protocol,
1694 testType: clientTest,
1695 name: "VersionNegotiation-Client2-" + suffix,
1696 config: Config{
1697 MaxVersion: runnerVers.version,
1698 Bugs: ProtocolBugs{
1699 ExpectInitialRecordVersion: clientVers,
1700 },
1701 },
1702 flags: []string{"-max-version", shimVersFlag},
1703 expectedVersion: expectedVersion,
1704 })
David Benjamin8b8c0062014-11-23 02:47:52 -05001705
1706 testCases = append(testCases, testCase{
1707 protocol: protocol,
1708 testType: serverTest,
1709 name: "VersionNegotiation-Server-" + suffix,
1710 config: Config{
1711 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05001712 Bugs: ProtocolBugs{
1713 ExpectInitialRecordVersion: expectedVersion,
1714 },
David Benjamin8b8c0062014-11-23 02:47:52 -05001715 },
1716 flags: flags,
1717 expectedVersion: expectedVersion,
1718 })
David Benjamin1eb367c2014-12-12 18:17:51 -05001719 testCases = append(testCases, testCase{
1720 protocol: protocol,
1721 testType: serverTest,
1722 name: "VersionNegotiation-Server2-" + suffix,
1723 config: Config{
1724 MaxVersion: runnerVers.version,
1725 Bugs: ProtocolBugs{
1726 ExpectInitialRecordVersion: expectedVersion,
1727 },
1728 },
1729 flags: []string{"-max-version", shimVersFlag},
1730 expectedVersion: expectedVersion,
1731 })
David Benjamin8b8c0062014-11-23 02:47:52 -05001732 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001733 }
1734 }
1735}
1736
David Benjaminaccb4542014-12-12 23:44:33 -05001737func addMinimumVersionTests() {
1738 for i, shimVers := range tlsVersions {
1739 // Assemble flags to disable all older versions on the shim.
1740 var flags []string
1741 for _, vers := range tlsVersions[:i] {
1742 flags = append(flags, vers.flag)
1743 }
1744
1745 for _, runnerVers := range tlsVersions {
1746 protocols := []protocol{tls}
1747 if runnerVers.hasDTLS && shimVers.hasDTLS {
1748 protocols = append(protocols, dtls)
1749 }
1750 for _, protocol := range protocols {
1751 suffix := shimVers.name + "-" + runnerVers.name
1752 if protocol == dtls {
1753 suffix += "-DTLS"
1754 }
1755 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
1756
David Benjaminaccb4542014-12-12 23:44:33 -05001757 var expectedVersion uint16
1758 var shouldFail bool
1759 var expectedError string
David Benjamin87909c02014-12-13 01:55:01 -05001760 var expectedLocalError string
David Benjaminaccb4542014-12-12 23:44:33 -05001761 if runnerVers.version >= shimVers.version {
1762 expectedVersion = runnerVers.version
1763 } else {
1764 shouldFail = true
1765 expectedError = ":UNSUPPORTED_PROTOCOL:"
David Benjamin87909c02014-12-13 01:55:01 -05001766 if runnerVers.version > VersionSSL30 {
1767 expectedLocalError = "remote error: protocol version not supported"
1768 } else {
1769 expectedLocalError = "remote error: handshake failure"
1770 }
David Benjaminaccb4542014-12-12 23:44:33 -05001771 }
1772
1773 testCases = append(testCases, testCase{
1774 protocol: protocol,
1775 testType: clientTest,
1776 name: "MinimumVersion-Client-" + suffix,
1777 config: Config{
1778 MaxVersion: runnerVers.version,
1779 },
David Benjamin87909c02014-12-13 01:55:01 -05001780 flags: flags,
1781 expectedVersion: expectedVersion,
1782 shouldFail: shouldFail,
1783 expectedError: expectedError,
1784 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05001785 })
1786 testCases = append(testCases, testCase{
1787 protocol: protocol,
1788 testType: clientTest,
1789 name: "MinimumVersion-Client2-" + suffix,
1790 config: Config{
1791 MaxVersion: runnerVers.version,
1792 },
David Benjamin87909c02014-12-13 01:55:01 -05001793 flags: []string{"-min-version", shimVersFlag},
1794 expectedVersion: expectedVersion,
1795 shouldFail: shouldFail,
1796 expectedError: expectedError,
1797 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05001798 })
1799
1800 testCases = append(testCases, testCase{
1801 protocol: protocol,
1802 testType: serverTest,
1803 name: "MinimumVersion-Server-" + suffix,
1804 config: Config{
1805 MaxVersion: runnerVers.version,
1806 },
David Benjamin87909c02014-12-13 01:55:01 -05001807 flags: flags,
1808 expectedVersion: expectedVersion,
1809 shouldFail: shouldFail,
1810 expectedError: expectedError,
1811 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05001812 })
1813 testCases = append(testCases, testCase{
1814 protocol: protocol,
1815 testType: serverTest,
1816 name: "MinimumVersion-Server2-" + suffix,
1817 config: Config{
1818 MaxVersion: runnerVers.version,
1819 },
David Benjamin87909c02014-12-13 01:55:01 -05001820 flags: []string{"-min-version", shimVersFlag},
1821 expectedVersion: expectedVersion,
1822 shouldFail: shouldFail,
1823 expectedError: expectedError,
1824 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05001825 })
1826 }
1827 }
1828 }
1829}
1830
David Benjamin5c24a1d2014-08-31 00:59:27 -04001831func addD5BugTests() {
1832 testCases = append(testCases, testCase{
1833 testType: serverTest,
1834 name: "D5Bug-NoQuirk-Reject",
1835 config: Config{
1836 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
1837 Bugs: ProtocolBugs{
1838 SSL3RSAKeyExchange: true,
1839 },
1840 },
1841 shouldFail: true,
1842 expectedError: ":TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG:",
1843 })
1844 testCases = append(testCases, testCase{
1845 testType: serverTest,
1846 name: "D5Bug-Quirk-Normal",
1847 config: Config{
1848 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
1849 },
1850 flags: []string{"-tls-d5-bug"},
1851 })
1852 testCases = append(testCases, testCase{
1853 testType: serverTest,
1854 name: "D5Bug-Quirk-Bug",
1855 config: Config{
1856 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
1857 Bugs: ProtocolBugs{
1858 SSL3RSAKeyExchange: true,
1859 },
1860 },
1861 flags: []string{"-tls-d5-bug"},
1862 })
1863}
1864
David Benjamine78bfde2014-09-06 12:45:15 -04001865func addExtensionTests() {
1866 testCases = append(testCases, testCase{
1867 testType: clientTest,
1868 name: "DuplicateExtensionClient",
1869 config: Config{
1870 Bugs: ProtocolBugs{
1871 DuplicateExtension: true,
1872 },
1873 },
1874 shouldFail: true,
1875 expectedLocalError: "remote error: error decoding message",
1876 })
1877 testCases = append(testCases, testCase{
1878 testType: serverTest,
1879 name: "DuplicateExtensionServer",
1880 config: Config{
1881 Bugs: ProtocolBugs{
1882 DuplicateExtension: true,
1883 },
1884 },
1885 shouldFail: true,
1886 expectedLocalError: "remote error: error decoding message",
1887 })
1888 testCases = append(testCases, testCase{
1889 testType: clientTest,
1890 name: "ServerNameExtensionClient",
1891 config: Config{
1892 Bugs: ProtocolBugs{
1893 ExpectServerName: "example.com",
1894 },
1895 },
1896 flags: []string{"-host-name", "example.com"},
1897 })
1898 testCases = append(testCases, testCase{
1899 testType: clientTest,
1900 name: "ServerNameExtensionClient",
1901 config: Config{
1902 Bugs: ProtocolBugs{
1903 ExpectServerName: "mismatch.com",
1904 },
1905 },
1906 flags: []string{"-host-name", "example.com"},
1907 shouldFail: true,
1908 expectedLocalError: "tls: unexpected server name",
1909 })
1910 testCases = append(testCases, testCase{
1911 testType: clientTest,
1912 name: "ServerNameExtensionClient",
1913 config: Config{
1914 Bugs: ProtocolBugs{
1915 ExpectServerName: "missing.com",
1916 },
1917 },
1918 shouldFail: true,
1919 expectedLocalError: "tls: unexpected server name",
1920 })
1921 testCases = append(testCases, testCase{
1922 testType: serverTest,
1923 name: "ServerNameExtensionServer",
1924 config: Config{
1925 ServerName: "example.com",
1926 },
1927 flags: []string{"-expect-server-name", "example.com"},
1928 resumeSession: true,
1929 })
David Benjaminae2888f2014-09-06 12:58:58 -04001930 testCases = append(testCases, testCase{
1931 testType: clientTest,
1932 name: "ALPNClient",
1933 config: Config{
1934 NextProtos: []string{"foo"},
1935 },
1936 flags: []string{
1937 "-advertise-alpn", "\x03foo\x03bar\x03baz",
1938 "-expect-alpn", "foo",
1939 },
David Benjaminfc7b0862014-09-06 13:21:53 -04001940 expectedNextProto: "foo",
1941 expectedNextProtoType: alpn,
1942 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04001943 })
1944 testCases = append(testCases, testCase{
1945 testType: serverTest,
1946 name: "ALPNServer",
1947 config: Config{
1948 NextProtos: []string{"foo", "bar", "baz"},
1949 },
1950 flags: []string{
1951 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
1952 "-select-alpn", "foo",
1953 },
David Benjaminfc7b0862014-09-06 13:21:53 -04001954 expectedNextProto: "foo",
1955 expectedNextProtoType: alpn,
1956 resumeSession: true,
1957 })
1958 // Test that the server prefers ALPN over NPN.
1959 testCases = append(testCases, testCase{
1960 testType: serverTest,
1961 name: "ALPNServer-Preferred",
1962 config: Config{
1963 NextProtos: []string{"foo", "bar", "baz"},
1964 },
1965 flags: []string{
1966 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
1967 "-select-alpn", "foo",
1968 "-advertise-npn", "\x03foo\x03bar\x03baz",
1969 },
1970 expectedNextProto: "foo",
1971 expectedNextProtoType: alpn,
1972 resumeSession: true,
1973 })
1974 testCases = append(testCases, testCase{
1975 testType: serverTest,
1976 name: "ALPNServer-Preferred-Swapped",
1977 config: Config{
1978 NextProtos: []string{"foo", "bar", "baz"},
1979 Bugs: ProtocolBugs{
1980 SwapNPNAndALPN: true,
1981 },
1982 },
1983 flags: []string{
1984 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
1985 "-select-alpn", "foo",
1986 "-advertise-npn", "\x03foo\x03bar\x03baz",
1987 },
1988 expectedNextProto: "foo",
1989 expectedNextProtoType: alpn,
1990 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04001991 })
Adam Langley38311732014-10-16 19:04:35 -07001992 // Resume with a corrupt ticket.
1993 testCases = append(testCases, testCase{
1994 testType: serverTest,
1995 name: "CorruptTicket",
1996 config: Config{
1997 Bugs: ProtocolBugs{
1998 CorruptTicket: true,
1999 },
2000 },
2001 resumeSession: true,
2002 flags: []string{"-expect-session-miss"},
2003 })
2004 // Resume with an oversized session id.
2005 testCases = append(testCases, testCase{
2006 testType: serverTest,
2007 name: "OversizedSessionId",
2008 config: Config{
2009 Bugs: ProtocolBugs{
2010 OversizedSessionId: true,
2011 },
2012 },
2013 resumeSession: true,
Adam Langley75712922014-10-10 16:23:43 -07002014 shouldFail: true,
Adam Langley38311732014-10-16 19:04:35 -07002015 expectedError: ":DECODE_ERROR:",
2016 })
David Benjaminca6c8262014-11-15 19:06:08 -05002017 // Basic DTLS-SRTP tests. Include fake profiles to ensure they
2018 // are ignored.
2019 testCases = append(testCases, testCase{
2020 protocol: dtls,
2021 name: "SRTP-Client",
2022 config: Config{
2023 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
2024 },
2025 flags: []string{
2026 "-srtp-profiles",
2027 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2028 },
2029 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2030 })
2031 testCases = append(testCases, testCase{
2032 protocol: dtls,
2033 testType: serverTest,
2034 name: "SRTP-Server",
2035 config: Config{
2036 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
2037 },
2038 flags: []string{
2039 "-srtp-profiles",
2040 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2041 },
2042 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2043 })
2044 // Test that the MKI is ignored.
2045 testCases = append(testCases, testCase{
2046 protocol: dtls,
2047 testType: serverTest,
2048 name: "SRTP-Server-IgnoreMKI",
2049 config: Config{
2050 SRTPProtectionProfiles: []uint16{SRTP_AES128_CM_HMAC_SHA1_80},
2051 Bugs: ProtocolBugs{
2052 SRTPMasterKeyIdentifer: "bogus",
2053 },
2054 },
2055 flags: []string{
2056 "-srtp-profiles",
2057 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2058 },
2059 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2060 })
2061 // Test that SRTP isn't negotiated on the server if there were
2062 // no matching profiles.
2063 testCases = append(testCases, testCase{
2064 protocol: dtls,
2065 testType: serverTest,
2066 name: "SRTP-Server-NoMatch",
2067 config: Config{
2068 SRTPProtectionProfiles: []uint16{100, 101, 102},
2069 },
2070 flags: []string{
2071 "-srtp-profiles",
2072 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2073 },
2074 expectedSRTPProtectionProfile: 0,
2075 })
2076 // Test that the server returning an invalid SRTP profile is
2077 // flagged as an error by the client.
2078 testCases = append(testCases, testCase{
2079 protocol: dtls,
2080 name: "SRTP-Client-NoMatch",
2081 config: Config{
2082 Bugs: ProtocolBugs{
2083 SendSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_32,
2084 },
2085 },
2086 flags: []string{
2087 "-srtp-profiles",
2088 "SRTP_AES128_CM_SHA1_80",
2089 },
2090 shouldFail: true,
2091 expectedError: ":BAD_SRTP_PROTECTION_PROFILE_LIST:",
2092 })
David Benjamin61f95272014-11-25 01:55:35 -05002093 // Test OCSP stapling and SCT list.
2094 testCases = append(testCases, testCase{
2095 name: "OCSPStapling",
2096 flags: []string{
2097 "-enable-ocsp-stapling",
2098 "-expect-ocsp-response",
2099 base64.StdEncoding.EncodeToString(testOCSPResponse),
2100 },
2101 })
2102 testCases = append(testCases, testCase{
2103 name: "SignedCertificateTimestampList",
2104 flags: []string{
2105 "-enable-signed-cert-timestamps",
2106 "-expect-signed-cert-timestamps",
2107 base64.StdEncoding.EncodeToString(testSCTList),
2108 },
2109 })
David Benjamine78bfde2014-09-06 12:45:15 -04002110}
2111
David Benjamin01fe8202014-09-24 15:21:44 -04002112func addResumptionVersionTests() {
David Benjamin01fe8202014-09-24 15:21:44 -04002113 for _, sessionVers := range tlsVersions {
David Benjamin01fe8202014-09-24 15:21:44 -04002114 for _, resumeVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05002115 protocols := []protocol{tls}
2116 if sessionVers.hasDTLS && resumeVers.hasDTLS {
2117 protocols = append(protocols, dtls)
David Benjaminbdf5e722014-11-11 00:52:15 -05002118 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002119 for _, protocol := range protocols {
2120 suffix := "-" + sessionVers.name + "-" + resumeVers.name
2121 if protocol == dtls {
2122 suffix += "-DTLS"
2123 }
2124
2125 testCases = append(testCases, testCase{
2126 protocol: protocol,
2127 name: "Resume-Client" + suffix,
2128 resumeSession: true,
2129 config: Config{
2130 MaxVersion: sessionVers.version,
2131 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2132 Bugs: ProtocolBugs{
2133 AllowSessionVersionMismatch: true,
2134 },
2135 },
2136 expectedVersion: sessionVers.version,
2137 resumeConfig: &Config{
2138 MaxVersion: resumeVers.version,
2139 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2140 Bugs: ProtocolBugs{
2141 AllowSessionVersionMismatch: true,
2142 },
2143 },
2144 expectedResumeVersion: resumeVers.version,
2145 })
2146
2147 testCases = append(testCases, testCase{
2148 protocol: protocol,
2149 name: "Resume-Client-NoResume" + suffix,
2150 flags: []string{"-expect-session-miss"},
2151 resumeSession: true,
2152 config: Config{
2153 MaxVersion: sessionVers.version,
2154 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2155 },
2156 expectedVersion: sessionVers.version,
2157 resumeConfig: &Config{
2158 MaxVersion: resumeVers.version,
2159 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2160 },
2161 newSessionsOnResume: true,
2162 expectedResumeVersion: resumeVers.version,
2163 })
2164
2165 var flags []string
2166 if sessionVers.version != resumeVers.version {
2167 flags = append(flags, "-expect-session-miss")
2168 }
2169 testCases = append(testCases, testCase{
2170 protocol: protocol,
2171 testType: serverTest,
2172 name: "Resume-Server" + suffix,
2173 flags: flags,
2174 resumeSession: true,
2175 config: Config{
2176 MaxVersion: sessionVers.version,
2177 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2178 },
2179 expectedVersion: sessionVers.version,
2180 resumeConfig: &Config{
2181 MaxVersion: resumeVers.version,
2182 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2183 },
2184 expectedResumeVersion: resumeVers.version,
2185 })
2186 }
David Benjamin01fe8202014-09-24 15:21:44 -04002187 }
2188 }
2189}
2190
Adam Langley2ae77d22014-10-28 17:29:33 -07002191func addRenegotiationTests() {
2192 testCases = append(testCases, testCase{
2193 testType: serverTest,
2194 name: "Renegotiate-Server",
2195 flags: []string{"-renegotiate"},
2196 shimWritesFirst: true,
2197 })
2198 testCases = append(testCases, testCase{
2199 testType: serverTest,
2200 name: "Renegotiate-Server-EmptyExt",
2201 config: Config{
2202 Bugs: ProtocolBugs{
2203 EmptyRenegotiationInfo: true,
2204 },
2205 },
2206 flags: []string{"-renegotiate"},
2207 shimWritesFirst: true,
2208 shouldFail: true,
2209 expectedError: ":RENEGOTIATION_MISMATCH:",
2210 })
2211 testCases = append(testCases, testCase{
2212 testType: serverTest,
2213 name: "Renegotiate-Server-BadExt",
2214 config: Config{
2215 Bugs: ProtocolBugs{
2216 BadRenegotiationInfo: true,
2217 },
2218 },
2219 flags: []string{"-renegotiate"},
2220 shimWritesFirst: true,
2221 shouldFail: true,
2222 expectedError: ":RENEGOTIATION_MISMATCH:",
2223 })
David Benjaminca6554b2014-11-08 12:31:52 -05002224 testCases = append(testCases, testCase{
2225 testType: serverTest,
2226 name: "Renegotiate-Server-ClientInitiated",
2227 renegotiate: true,
2228 })
2229 testCases = append(testCases, testCase{
2230 testType: serverTest,
2231 name: "Renegotiate-Server-ClientInitiated-NoExt",
2232 renegotiate: true,
2233 config: Config{
2234 Bugs: ProtocolBugs{
2235 NoRenegotiationInfo: true,
2236 },
2237 },
2238 shouldFail: true,
2239 expectedError: ":UNSAFE_LEGACY_RENEGOTIATION_DISABLED:",
2240 })
2241 testCases = append(testCases, testCase{
2242 testType: serverTest,
2243 name: "Renegotiate-Server-ClientInitiated-NoExt-Allowed",
2244 renegotiate: true,
2245 config: Config{
2246 Bugs: ProtocolBugs{
2247 NoRenegotiationInfo: true,
2248 },
2249 },
2250 flags: []string{"-allow-unsafe-legacy-renegotiation"},
2251 })
Adam Langley2ae77d22014-10-28 17:29:33 -07002252 // TODO(agl): test the renegotiation info SCSV.
Adam Langleycf2d4f42014-10-28 19:06:14 -07002253 testCases = append(testCases, testCase{
2254 name: "Renegotiate-Client",
2255 renegotiate: true,
2256 })
2257 testCases = append(testCases, testCase{
2258 name: "Renegotiate-Client-EmptyExt",
2259 renegotiate: true,
2260 config: Config{
2261 Bugs: ProtocolBugs{
2262 EmptyRenegotiationInfo: true,
2263 },
2264 },
2265 shouldFail: true,
2266 expectedError: ":RENEGOTIATION_MISMATCH:",
2267 })
2268 testCases = append(testCases, testCase{
2269 name: "Renegotiate-Client-BadExt",
2270 renegotiate: true,
2271 config: Config{
2272 Bugs: ProtocolBugs{
2273 BadRenegotiationInfo: true,
2274 },
2275 },
2276 shouldFail: true,
2277 expectedError: ":RENEGOTIATION_MISMATCH:",
2278 })
2279 testCases = append(testCases, testCase{
2280 name: "Renegotiate-Client-SwitchCiphers",
2281 renegotiate: true,
2282 config: Config{
2283 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2284 },
2285 renegotiateCiphers: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2286 })
2287 testCases = append(testCases, testCase{
2288 name: "Renegotiate-Client-SwitchCiphers2",
2289 renegotiate: true,
2290 config: Config{
2291 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2292 },
2293 renegotiateCiphers: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2294 })
David Benjaminc44b1df2014-11-23 12:11:01 -05002295 testCases = append(testCases, testCase{
2296 name: "Renegotiate-SameClientVersion",
2297 renegotiate: true,
2298 config: Config{
2299 MaxVersion: VersionTLS10,
2300 Bugs: ProtocolBugs{
2301 RequireSameRenegoClientVersion: true,
2302 },
2303 },
2304 })
Adam Langley2ae77d22014-10-28 17:29:33 -07002305}
2306
David Benjamin5e961c12014-11-07 01:48:35 -05002307func addDTLSReplayTests() {
2308 // Test that sequence number replays are detected.
2309 testCases = append(testCases, testCase{
2310 protocol: dtls,
2311 name: "DTLS-Replay",
2312 replayWrites: true,
2313 })
2314
2315 // Test the outgoing sequence number skipping by values larger
2316 // than the retransmit window.
2317 testCases = append(testCases, testCase{
2318 protocol: dtls,
2319 name: "DTLS-Replay-LargeGaps",
2320 config: Config{
2321 Bugs: ProtocolBugs{
2322 SequenceNumberIncrement: 127,
2323 },
2324 },
2325 replayWrites: true,
2326 })
2327}
2328
Feng Lu41aa3252014-11-21 22:47:56 -08002329func addFastRadioPaddingTests() {
David Benjamin1e29a6b2014-12-10 02:27:24 -05002330 testCases = append(testCases, testCase{
2331 protocol: tls,
2332 name: "FastRadio-Padding",
Feng Lu41aa3252014-11-21 22:47:56 -08002333 config: Config{
2334 Bugs: ProtocolBugs{
2335 RequireFastradioPadding: true,
2336 },
2337 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05002338 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08002339 })
David Benjamin1e29a6b2014-12-10 02:27:24 -05002340 testCases = append(testCases, testCase{
2341 protocol: dtls,
2342 name: "FastRadio-Padding",
Feng Lu41aa3252014-11-21 22:47:56 -08002343 config: Config{
2344 Bugs: ProtocolBugs{
2345 RequireFastradioPadding: true,
2346 },
2347 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05002348 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08002349 })
2350}
2351
David Benjamin000800a2014-11-14 01:43:59 -05002352var testHashes = []struct {
2353 name string
2354 id uint8
2355}{
2356 {"SHA1", hashSHA1},
2357 {"SHA224", hashSHA224},
2358 {"SHA256", hashSHA256},
2359 {"SHA384", hashSHA384},
2360 {"SHA512", hashSHA512},
2361}
2362
2363func addSigningHashTests() {
2364 // Make sure each hash works. Include some fake hashes in the list and
2365 // ensure they're ignored.
2366 for _, hash := range testHashes {
2367 testCases = append(testCases, testCase{
2368 name: "SigningHash-ClientAuth-" + hash.name,
2369 config: Config{
2370 ClientAuth: RequireAnyClientCert,
2371 SignatureAndHashes: []signatureAndHash{
2372 {signatureRSA, 42},
2373 {signatureRSA, hash.id},
2374 {signatureRSA, 255},
2375 },
2376 },
2377 flags: []string{
2378 "-cert-file", rsaCertificateFile,
2379 "-key-file", rsaKeyFile,
2380 },
2381 })
2382
2383 testCases = append(testCases, testCase{
2384 testType: serverTest,
2385 name: "SigningHash-ServerKeyExchange-Sign-" + hash.name,
2386 config: Config{
2387 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2388 SignatureAndHashes: []signatureAndHash{
2389 {signatureRSA, 42},
2390 {signatureRSA, hash.id},
2391 {signatureRSA, 255},
2392 },
2393 },
2394 })
2395 }
2396
2397 // Test that hash resolution takes the signature type into account.
2398 testCases = append(testCases, testCase{
2399 name: "SigningHash-ClientAuth-SignatureType",
2400 config: Config{
2401 ClientAuth: RequireAnyClientCert,
2402 SignatureAndHashes: []signatureAndHash{
2403 {signatureECDSA, hashSHA512},
2404 {signatureRSA, hashSHA384},
2405 {signatureECDSA, hashSHA1},
2406 },
2407 },
2408 flags: []string{
2409 "-cert-file", rsaCertificateFile,
2410 "-key-file", rsaKeyFile,
2411 },
2412 })
2413
2414 testCases = append(testCases, testCase{
2415 testType: serverTest,
2416 name: "SigningHash-ServerKeyExchange-SignatureType",
2417 config: Config{
2418 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2419 SignatureAndHashes: []signatureAndHash{
2420 {signatureECDSA, hashSHA512},
2421 {signatureRSA, hashSHA384},
2422 {signatureECDSA, hashSHA1},
2423 },
2424 },
2425 })
2426
2427 // Test that, if the list is missing, the peer falls back to SHA-1.
2428 testCases = append(testCases, testCase{
2429 name: "SigningHash-ClientAuth-Fallback",
2430 config: Config{
2431 ClientAuth: RequireAnyClientCert,
2432 SignatureAndHashes: []signatureAndHash{
2433 {signatureRSA, hashSHA1},
2434 },
2435 Bugs: ProtocolBugs{
2436 NoSignatureAndHashes: true,
2437 },
2438 },
2439 flags: []string{
2440 "-cert-file", rsaCertificateFile,
2441 "-key-file", rsaKeyFile,
2442 },
2443 })
2444
2445 testCases = append(testCases, testCase{
2446 testType: serverTest,
2447 name: "SigningHash-ServerKeyExchange-Fallback",
2448 config: Config{
2449 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2450 SignatureAndHashes: []signatureAndHash{
2451 {signatureRSA, hashSHA1},
2452 },
2453 Bugs: ProtocolBugs{
2454 NoSignatureAndHashes: true,
2455 },
2456 },
2457 })
2458}
2459
David Benjamin884fdf12014-08-02 15:28:23 -04002460func worker(statusChan chan statusMsg, c chan *testCase, buildDir string, wg *sync.WaitGroup) {
Adam Langley95c29f32014-06-20 12:00:00 -07002461 defer wg.Done()
2462
2463 for test := range c {
Adam Langley69a01602014-11-17 17:26:55 -08002464 var err error
2465
2466 if *mallocTest < 0 {
2467 statusChan <- statusMsg{test: test, started: true}
2468 err = runTest(test, buildDir, -1)
2469 } else {
2470 for mallocNumToFail := int64(*mallocTest); ; mallocNumToFail++ {
2471 statusChan <- statusMsg{test: test, started: true}
2472 if err = runTest(test, buildDir, mallocNumToFail); err != errMoreMallocs {
2473 if err != nil {
2474 fmt.Printf("\n\nmalloc test failed at %d: %s\n", mallocNumToFail, err)
2475 }
2476 break
2477 }
2478 }
2479 }
Adam Langley95c29f32014-06-20 12:00:00 -07002480 statusChan <- statusMsg{test: test, err: err}
2481 }
2482}
2483
2484type statusMsg struct {
2485 test *testCase
2486 started bool
2487 err error
2488}
2489
2490func statusPrinter(doneChan chan struct{}, statusChan chan statusMsg, total int) {
2491 var started, done, failed, lineLen int
2492 defer close(doneChan)
2493
2494 for msg := range statusChan {
2495 if msg.started {
2496 started++
2497 } else {
2498 done++
2499 }
2500
2501 fmt.Printf("\x1b[%dD\x1b[K", lineLen)
2502
2503 if msg.err != nil {
2504 fmt.Printf("FAILED (%s)\n%s\n", msg.test.name, msg.err)
2505 failed++
2506 }
2507 line := fmt.Sprintf("%d/%d/%d/%d", failed, done, started, total)
2508 lineLen = len(line)
2509 os.Stdout.WriteString(line)
2510 }
2511}
2512
2513func main() {
2514 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 -04002515 var flagNumWorkers *int = flag.Int("num-workers", runtime.NumCPU(), "The number of workers to run in parallel.")
David Benjamin884fdf12014-08-02 15:28:23 -04002516 var flagBuildDir *string = flag.String("build-dir", "../../../build", "The build directory to run the shim from.")
Adam Langley95c29f32014-06-20 12:00:00 -07002517
2518 flag.Parse()
2519
2520 addCipherSuiteTests()
2521 addBadECDSASignatureTests()
Adam Langley80842bd2014-06-20 12:00:00 -07002522 addCBCPaddingTests()
Kenny Root7fdeaf12014-08-05 15:23:37 -07002523 addCBCSplittingTests()
David Benjamin636293b2014-07-08 17:59:18 -04002524 addClientAuthTests()
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002525 addVersionNegotiationTests()
David Benjaminaccb4542014-12-12 23:44:33 -05002526 addMinimumVersionTests()
David Benjamin5c24a1d2014-08-31 00:59:27 -04002527 addD5BugTests()
David Benjamine78bfde2014-09-06 12:45:15 -04002528 addExtensionTests()
David Benjamin01fe8202014-09-24 15:21:44 -04002529 addResumptionVersionTests()
Adam Langley75712922014-10-10 16:23:43 -07002530 addExtendedMasterSecretTests()
Adam Langley2ae77d22014-10-28 17:29:33 -07002531 addRenegotiationTests()
David Benjamin5e961c12014-11-07 01:48:35 -05002532 addDTLSReplayTests()
David Benjamin000800a2014-11-14 01:43:59 -05002533 addSigningHashTests()
Feng Lu41aa3252014-11-21 22:47:56 -08002534 addFastRadioPaddingTests()
David Benjamin43ec06f2014-08-05 02:28:57 -04002535 for _, async := range []bool{false, true} {
2536 for _, splitHandshake := range []bool{false, true} {
David Benjamin6fd297b2014-08-11 18:43:38 -04002537 for _, protocol := range []protocol{tls, dtls} {
2538 addStateMachineCoverageTests(async, splitHandshake, protocol)
2539 }
David Benjamin43ec06f2014-08-05 02:28:57 -04002540 }
2541 }
Adam Langley95c29f32014-06-20 12:00:00 -07002542
2543 var wg sync.WaitGroup
2544
David Benjamin2bc8e6f2014-08-02 15:22:37 -04002545 numWorkers := *flagNumWorkers
Adam Langley95c29f32014-06-20 12:00:00 -07002546
2547 statusChan := make(chan statusMsg, numWorkers)
2548 testChan := make(chan *testCase, numWorkers)
2549 doneChan := make(chan struct{})
2550
David Benjamin025b3d32014-07-01 19:53:04 -04002551 go statusPrinter(doneChan, statusChan, len(testCases))
Adam Langley95c29f32014-06-20 12:00:00 -07002552
2553 for i := 0; i < numWorkers; i++ {
2554 wg.Add(1)
David Benjamin884fdf12014-08-02 15:28:23 -04002555 go worker(statusChan, testChan, *flagBuildDir, &wg)
Adam Langley95c29f32014-06-20 12:00:00 -07002556 }
2557
David Benjamin025b3d32014-07-01 19:53:04 -04002558 for i := range testCases {
2559 if len(*flagTest) == 0 || *flagTest == testCases[i].name {
2560 testChan <- &testCases[i]
Adam Langley95c29f32014-06-20 12:00:00 -07002561 }
2562 }
2563
2564 close(testChan)
2565 wg.Wait()
2566 close(statusChan)
2567 <-doneChan
2568
2569 fmt.Printf("\n")
2570}