blob: 6faccb5bd69ed571ba08f4a2689fbfdac9357e76 [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 },
Adam Langley95c29f32014-06-20 12:00:00 -0700567}
568
David Benjamin01fe8202014-09-24 15:21:44 -0400569func doExchange(test *testCase, config *Config, conn net.Conn, messageLen int, isResume bool) error {
David Benjamin65ea8ff2014-11-23 03:01:00 -0500570 var connDebug *recordingConn
571 if *flagDebug {
572 connDebug = &recordingConn{Conn: conn}
573 conn = connDebug
574 defer func() {
575 connDebug.WriteTo(os.Stdout)
576 }()
577 }
578
David Benjamin6fd297b2014-08-11 18:43:38 -0400579 if test.protocol == dtls {
580 conn = newPacketAdaptor(conn)
David Benjamin5e961c12014-11-07 01:48:35 -0500581 if test.replayWrites {
582 conn = newReplayAdaptor(conn)
583 }
David Benjamin6fd297b2014-08-11 18:43:38 -0400584 }
585
586 if test.sendPrefix != "" {
587 if _, err := conn.Write([]byte(test.sendPrefix)); err != nil {
588 return err
589 }
David Benjamin98e882e2014-08-08 13:24:34 -0400590 }
591
David Benjamin1d5c83e2014-07-22 19:20:02 -0400592 var tlsConn *Conn
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400593 if test.testType == clientTest {
David Benjamin6fd297b2014-08-11 18:43:38 -0400594 if test.protocol == dtls {
595 tlsConn = DTLSServer(conn, config)
596 } else {
597 tlsConn = Server(conn, config)
598 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400599 } else {
600 config.InsecureSkipVerify = true
David Benjamin6fd297b2014-08-11 18:43:38 -0400601 if test.protocol == dtls {
602 tlsConn = DTLSClient(conn, config)
603 } else {
604 tlsConn = Client(conn, config)
605 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400606 }
607
Adam Langley95c29f32014-06-20 12:00:00 -0700608 if err := tlsConn.Handshake(); err != nil {
609 return err
610 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700611
David Benjamin01fe8202014-09-24 15:21:44 -0400612 // TODO(davidben): move all per-connection expectations into a dedicated
613 // expectations struct that can be specified separately for the two
614 // legs.
615 expectedVersion := test.expectedVersion
616 if isResume && test.expectedResumeVersion != 0 {
617 expectedVersion = test.expectedResumeVersion
618 }
619 if vers := tlsConn.ConnectionState().Version; expectedVersion != 0 && vers != expectedVersion {
620 return fmt.Errorf("got version %x, expected %x", vers, expectedVersion)
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400621 }
622
David Benjamina08e49d2014-08-24 01:46:07 -0400623 if test.expectChannelID {
624 channelID := tlsConn.ConnectionState().ChannelID
625 if channelID == nil {
626 return fmt.Errorf("no channel ID negotiated")
627 }
628 if channelID.Curve != channelIDKey.Curve ||
629 channelIDKey.X.Cmp(channelIDKey.X) != 0 ||
630 channelIDKey.Y.Cmp(channelIDKey.Y) != 0 {
631 return fmt.Errorf("incorrect channel ID")
632 }
633 }
634
David Benjaminae2888f2014-09-06 12:58:58 -0400635 if expected := test.expectedNextProto; expected != "" {
636 if actual := tlsConn.ConnectionState().NegotiatedProtocol; actual != expected {
637 return fmt.Errorf("next proto mismatch: got %s, wanted %s", actual, expected)
638 }
639 }
640
David Benjaminfc7b0862014-09-06 13:21:53 -0400641 if test.expectedNextProtoType != 0 {
642 if (test.expectedNextProtoType == alpn) != tlsConn.ConnectionState().NegotiatedProtocolFromALPN {
643 return fmt.Errorf("next proto type mismatch")
644 }
645 }
646
David Benjaminca6c8262014-11-15 19:06:08 -0500647 if p := tlsConn.ConnectionState().SRTPProtectionProfile; p != test.expectedSRTPProtectionProfile {
648 return fmt.Errorf("SRTP profile mismatch: got %d, wanted %d", p, test.expectedSRTPProtectionProfile)
649 }
650
David Benjamine58c4f52014-08-24 03:47:07 -0400651 if test.shimWritesFirst {
652 var buf [5]byte
653 _, err := io.ReadFull(tlsConn, buf[:])
654 if err != nil {
655 return err
656 }
657 if string(buf[:]) != "hello" {
658 return fmt.Errorf("bad initial message")
659 }
660 }
661
Adam Langleycf2d4f42014-10-28 19:06:14 -0700662 if test.renegotiate {
663 if test.renegotiateCiphers != nil {
664 config.CipherSuites = test.renegotiateCiphers
665 }
666 if err := tlsConn.Renegotiate(); err != nil {
667 return err
668 }
669 } else if test.renegotiateCiphers != nil {
670 panic("renegotiateCiphers without renegotiate")
671 }
672
Kenny Root7fdeaf12014-08-05 15:23:37 -0700673 if messageLen < 0 {
David Benjamin6fd297b2014-08-11 18:43:38 -0400674 if test.protocol == dtls {
675 return fmt.Errorf("messageLen < 0 not supported for DTLS tests")
676 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700677 // Read until EOF.
678 _, err := io.Copy(ioutil.Discard, tlsConn)
679 return err
680 }
681
Adam Langley80842bd2014-06-20 12:00:00 -0700682 if messageLen == 0 {
683 messageLen = 32
684 }
685 testMessage := make([]byte, messageLen)
686 for i := range testMessage {
687 testMessage[i] = 0x42
688 }
Adam Langley95c29f32014-06-20 12:00:00 -0700689 tlsConn.Write(testMessage)
690
691 buf := make([]byte, len(testMessage))
David Benjamin6fd297b2014-08-11 18:43:38 -0400692 if test.protocol == dtls {
693 bufTmp := make([]byte, len(buf)+1)
694 n, err := tlsConn.Read(bufTmp)
695 if err != nil {
696 return err
697 }
698 if n != len(buf) {
699 return fmt.Errorf("bad reply; length mismatch (%d vs %d)", n, len(buf))
700 }
701 copy(buf, bufTmp)
702 } else {
703 _, err := io.ReadFull(tlsConn, buf)
704 if err != nil {
705 return err
706 }
Adam Langley95c29f32014-06-20 12:00:00 -0700707 }
708
709 for i, v := range buf {
710 if v != testMessage[i]^0xff {
711 return fmt.Errorf("bad reply contents at byte %d", i)
712 }
713 }
714
715 return nil
716}
717
David Benjamin325b5c32014-07-01 19:40:31 -0400718func valgrindOf(dbAttach bool, path string, args ...string) *exec.Cmd {
719 valgrindArgs := []string{"--error-exitcode=99", "--track-origins=yes", "--leak-check=full"}
Adam Langley95c29f32014-06-20 12:00:00 -0700720 if dbAttach {
David Benjamin325b5c32014-07-01 19:40:31 -0400721 valgrindArgs = append(valgrindArgs, "--db-attach=yes", "--db-command=xterm -e gdb -nw %f %p")
Adam Langley95c29f32014-06-20 12:00:00 -0700722 }
David Benjamin325b5c32014-07-01 19:40:31 -0400723 valgrindArgs = append(valgrindArgs, path)
724 valgrindArgs = append(valgrindArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -0700725
David Benjamin325b5c32014-07-01 19:40:31 -0400726 return exec.Command("valgrind", valgrindArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -0700727}
728
David Benjamin325b5c32014-07-01 19:40:31 -0400729func gdbOf(path string, args ...string) *exec.Cmd {
730 xtermArgs := []string{"-e", "gdb", "--args"}
731 xtermArgs = append(xtermArgs, path)
732 xtermArgs = append(xtermArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -0700733
David Benjamin325b5c32014-07-01 19:40:31 -0400734 return exec.Command("xterm", xtermArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -0700735}
736
David Benjamin1d5c83e2014-07-22 19:20:02 -0400737func openSocketPair() (shimEnd *os.File, conn net.Conn) {
Adam Langley95c29f32014-06-20 12:00:00 -0700738 socks, err := syscall.Socketpair(syscall.AF_UNIX, syscall.SOCK_STREAM, 0)
739 if err != nil {
740 panic(err)
741 }
742
743 syscall.CloseOnExec(socks[0])
744 syscall.CloseOnExec(socks[1])
David Benjamin1d5c83e2014-07-22 19:20:02 -0400745 shimEnd = os.NewFile(uintptr(socks[0]), "shim end")
Adam Langley95c29f32014-06-20 12:00:00 -0700746 connFile := os.NewFile(uintptr(socks[1]), "our end")
David Benjamin1d5c83e2014-07-22 19:20:02 -0400747 conn, err = net.FileConn(connFile)
748 if err != nil {
749 panic(err)
750 }
Adam Langley95c29f32014-06-20 12:00:00 -0700751 connFile.Close()
752 if err != nil {
753 panic(err)
754 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400755 return shimEnd, conn
756}
757
Adam Langley69a01602014-11-17 17:26:55 -0800758type moreMallocsError struct{}
759
760func (moreMallocsError) Error() string {
761 return "child process did not exhaust all allocation calls"
762}
763
764var errMoreMallocs = moreMallocsError{}
765
766func runTest(test *testCase, buildDir string, mallocNumToFail int64) error {
Adam Langley38311732014-10-16 19:04:35 -0700767 if !test.shouldFail && (len(test.expectedError) > 0 || len(test.expectedLocalError) > 0) {
768 panic("Error expected without shouldFail in " + test.name)
769 }
770
David Benjamin1d5c83e2014-07-22 19:20:02 -0400771 shimEnd, conn := openSocketPair()
772 shimEndResume, connResume := openSocketPair()
Adam Langley95c29f32014-06-20 12:00:00 -0700773
David Benjamin884fdf12014-08-02 15:28:23 -0400774 shim_path := path.Join(buildDir, "ssl/test/bssl_shim")
David Benjamin5a593af2014-08-11 19:51:50 -0400775 var flags []string
David Benjamin1d5c83e2014-07-22 19:20:02 -0400776 if test.testType == serverTest {
David Benjamin5a593af2014-08-11 19:51:50 -0400777 flags = append(flags, "-server")
778
David Benjamin025b3d32014-07-01 19:53:04 -0400779 flags = append(flags, "-key-file")
780 if test.keyFile == "" {
781 flags = append(flags, rsaKeyFile)
782 } else {
783 flags = append(flags, test.keyFile)
784 }
785
786 flags = append(flags, "-cert-file")
787 if test.certFile == "" {
788 flags = append(flags, rsaCertificateFile)
789 } else {
790 flags = append(flags, test.certFile)
791 }
792 }
David Benjamin5a593af2014-08-11 19:51:50 -0400793
David Benjamin6fd297b2014-08-11 18:43:38 -0400794 if test.protocol == dtls {
795 flags = append(flags, "-dtls")
796 }
797
David Benjamin5a593af2014-08-11 19:51:50 -0400798 if test.resumeSession {
799 flags = append(flags, "-resume")
800 }
801
David Benjamine58c4f52014-08-24 03:47:07 -0400802 if test.shimWritesFirst {
803 flags = append(flags, "-shim-writes-first")
804 }
805
David Benjamin025b3d32014-07-01 19:53:04 -0400806 flags = append(flags, test.flags...)
807
808 var shim *exec.Cmd
809 if *useValgrind {
810 shim = valgrindOf(false, shim_path, flags...)
Adam Langley75712922014-10-10 16:23:43 -0700811 } else if *useGDB {
812 shim = gdbOf(shim_path, flags...)
David Benjamin025b3d32014-07-01 19:53:04 -0400813 } else {
814 shim = exec.Command(shim_path, flags...)
815 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400816 shim.ExtraFiles = []*os.File{shimEnd, shimEndResume}
David Benjamin025b3d32014-07-01 19:53:04 -0400817 shim.Stdin = os.Stdin
818 var stdoutBuf, stderrBuf bytes.Buffer
819 shim.Stdout = &stdoutBuf
820 shim.Stderr = &stderrBuf
Adam Langley69a01602014-11-17 17:26:55 -0800821 if mallocNumToFail >= 0 {
822 shim.Env = []string{"MALLOC_NUMBER_TO_FAIL=" + strconv.FormatInt(mallocNumToFail, 10)}
823 if *mallocTestDebug {
824 shim.Env = append(shim.Env, "MALLOC_ABORT_ON_FAIL=1")
825 }
826 shim.Env = append(shim.Env, "_MALLOC_CHECK=1")
827 }
David Benjamin025b3d32014-07-01 19:53:04 -0400828
829 if err := shim.Start(); err != nil {
Adam Langley95c29f32014-06-20 12:00:00 -0700830 panic(err)
831 }
David Benjamin025b3d32014-07-01 19:53:04 -0400832 shimEnd.Close()
David Benjamin1d5c83e2014-07-22 19:20:02 -0400833 shimEndResume.Close()
Adam Langley95c29f32014-06-20 12:00:00 -0700834
835 config := test.config
David Benjamin1d5c83e2014-07-22 19:20:02 -0400836 config.ClientSessionCache = NewLRUClientSessionCache(1)
David Benjaminfe8eb9a2014-11-17 03:19:02 -0500837 config.ServerSessionCache = NewLRUServerSessionCache(1)
David Benjamin025b3d32014-07-01 19:53:04 -0400838 if test.testType == clientTest {
839 if len(config.Certificates) == 0 {
840 config.Certificates = []Certificate{getRSACertificate()}
841 }
David Benjamin025b3d32014-07-01 19:53:04 -0400842 }
Adam Langley95c29f32014-06-20 12:00:00 -0700843
David Benjamin01fe8202014-09-24 15:21:44 -0400844 err := doExchange(test, &config, conn, test.messageLen,
845 false /* not a resumption */)
Adam Langley95c29f32014-06-20 12:00:00 -0700846 conn.Close()
David Benjamin65ea8ff2014-11-23 03:01:00 -0500847
David Benjamin1d5c83e2014-07-22 19:20:02 -0400848 if err == nil && test.resumeSession {
David Benjamin01fe8202014-09-24 15:21:44 -0400849 var resumeConfig Config
850 if test.resumeConfig != nil {
851 resumeConfig = *test.resumeConfig
852 if len(resumeConfig.Certificates) == 0 {
853 resumeConfig.Certificates = []Certificate{getRSACertificate()}
854 }
David Benjaminfe8eb9a2014-11-17 03:19:02 -0500855 if !test.newSessionsOnResume {
856 resumeConfig.SessionTicketKey = config.SessionTicketKey
857 resumeConfig.ClientSessionCache = config.ClientSessionCache
858 resumeConfig.ServerSessionCache = config.ServerSessionCache
859 }
David Benjamin01fe8202014-09-24 15:21:44 -0400860 } else {
861 resumeConfig = config
862 }
863 err = doExchange(test, &resumeConfig, connResume, test.messageLen,
864 true /* resumption */)
David Benjamin1d5c83e2014-07-22 19:20:02 -0400865 }
David Benjamin812152a2014-09-06 12:49:07 -0400866 connResume.Close()
David Benjamin1d5c83e2014-07-22 19:20:02 -0400867
David Benjamin025b3d32014-07-01 19:53:04 -0400868 childErr := shim.Wait()
Adam Langley69a01602014-11-17 17:26:55 -0800869 if exitError, ok := childErr.(*exec.ExitError); ok {
870 if exitError.Sys().(syscall.WaitStatus).ExitStatus() == 88 {
871 return errMoreMallocs
872 }
873 }
Adam Langley95c29f32014-06-20 12:00:00 -0700874
875 stdout := string(stdoutBuf.Bytes())
876 stderr := string(stderrBuf.Bytes())
877 failed := err != nil || childErr != nil
878 correctFailure := len(test.expectedError) == 0 || strings.Contains(stdout, test.expectedError)
Adam Langleyac61fa32014-06-23 12:03:11 -0700879 localError := "none"
880 if err != nil {
881 localError = err.Error()
882 }
883 if len(test.expectedLocalError) != 0 {
884 correctFailure = correctFailure && strings.Contains(localError, test.expectedLocalError)
885 }
Adam Langley95c29f32014-06-20 12:00:00 -0700886
887 if failed != test.shouldFail || failed && !correctFailure {
Adam Langley95c29f32014-06-20 12:00:00 -0700888 childError := "none"
Adam Langley95c29f32014-06-20 12:00:00 -0700889 if childErr != nil {
890 childError = childErr.Error()
891 }
892
893 var msg string
894 switch {
895 case failed && !test.shouldFail:
896 msg = "unexpected failure"
897 case !failed && test.shouldFail:
898 msg = "unexpected success"
899 case failed && !correctFailure:
Adam Langleyac61fa32014-06-23 12:03:11 -0700900 msg = "bad error (wanted '" + test.expectedError + "' / '" + test.expectedLocalError + "')"
Adam Langley95c29f32014-06-20 12:00:00 -0700901 default:
902 panic("internal error")
903 }
904
905 return fmt.Errorf("%s: local error '%s', child error '%s', stdout:\n%s\nstderr:\n%s", msg, localError, childError, string(stdoutBuf.Bytes()), stderr)
906 }
907
908 if !*useValgrind && len(stderr) > 0 {
909 println(stderr)
910 }
911
912 return nil
913}
914
915var tlsVersions = []struct {
916 name string
917 version uint16
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400918 flag string
David Benjamin8b8c0062014-11-23 02:47:52 -0500919 hasDTLS bool
Adam Langley95c29f32014-06-20 12:00:00 -0700920}{
David Benjamin8b8c0062014-11-23 02:47:52 -0500921 {"SSL3", VersionSSL30, "-no-ssl3", false},
922 {"TLS1", VersionTLS10, "-no-tls1", true},
923 {"TLS11", VersionTLS11, "-no-tls11", false},
924 {"TLS12", VersionTLS12, "-no-tls12", true},
Adam Langley95c29f32014-06-20 12:00:00 -0700925}
926
927var testCipherSuites = []struct {
928 name string
929 id uint16
930}{
931 {"3DES-SHA", TLS_RSA_WITH_3DES_EDE_CBC_SHA},
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400932 {"AES128-GCM", TLS_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -0700933 {"AES128-SHA", TLS_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -0400934 {"AES128-SHA256", TLS_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400935 {"AES256-GCM", TLS_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -0700936 {"AES256-SHA", TLS_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -0400937 {"AES256-SHA256", TLS_RSA_WITH_AES_256_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400938 {"DHE-RSA-AES128-GCM", TLS_DHE_RSA_WITH_AES_128_GCM_SHA256},
939 {"DHE-RSA-AES128-SHA", TLS_DHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -0400940 {"DHE-RSA-AES128-SHA256", TLS_DHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400941 {"DHE-RSA-AES256-GCM", TLS_DHE_RSA_WITH_AES_256_GCM_SHA384},
942 {"DHE-RSA-AES256-SHA", TLS_DHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -0400943 {"DHE-RSA-AES256-SHA256", TLS_DHE_RSA_WITH_AES_256_CBC_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -0700944 {"ECDHE-ECDSA-AES128-GCM", TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
945 {"ECDHE-ECDSA-AES128-SHA", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -0400946 {"ECDHE-ECDSA-AES128-SHA256", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256},
947 {"ECDHE-ECDSA-AES256-GCM", TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -0700948 {"ECDHE-ECDSA-AES256-SHA", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -0400949 {"ECDHE-ECDSA-AES256-SHA384", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -0700950 {"ECDHE-ECDSA-RC4-SHA", TLS_ECDHE_ECDSA_WITH_RC4_128_SHA},
David Benjamin2af684f2014-10-27 02:23:15 -0400951 {"ECDHE-PSK-WITH-AES-128-GCM-SHA256", TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -0700952 {"ECDHE-RSA-AES128-GCM", TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -0700953 {"ECDHE-RSA-AES128-SHA", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -0400954 {"ECDHE-RSA-AES128-SHA256", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400955 {"ECDHE-RSA-AES256-GCM", TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -0700956 {"ECDHE-RSA-AES256-SHA", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -0400957 {"ECDHE-RSA-AES256-SHA384", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -0700958 {"ECDHE-RSA-RC4-SHA", TLS_ECDHE_RSA_WITH_RC4_128_SHA},
David Benjamin48cae082014-10-27 01:06:24 -0400959 {"PSK-AES128-CBC-SHA", TLS_PSK_WITH_AES_128_CBC_SHA},
960 {"PSK-AES256-CBC-SHA", TLS_PSK_WITH_AES_256_CBC_SHA},
961 {"PSK-RC4-SHA", TLS_PSK_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -0700962 {"RC4-MD5", TLS_RSA_WITH_RC4_128_MD5},
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400963 {"RC4-SHA", TLS_RSA_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -0700964}
965
David Benjamin8b8c0062014-11-23 02:47:52 -0500966func hasComponent(suiteName, component string) bool {
967 return strings.Contains("-"+suiteName+"-", "-"+component+"-")
968}
969
David Benjaminf7768e42014-08-31 02:06:47 -0400970func isTLS12Only(suiteName string) bool {
David Benjamin8b8c0062014-11-23 02:47:52 -0500971 return hasComponent(suiteName, "GCM") ||
972 hasComponent(suiteName, "SHA256") ||
973 hasComponent(suiteName, "SHA384")
974}
975
976func isDTLSCipher(suiteName string) bool {
977 // TODO(davidben): AES-GCM exists in DTLS 1.2 but is currently
978 // broken because DTLS is not EVP_AEAD-aware.
979 return !hasComponent(suiteName, "RC4") &&
980 !hasComponent(suiteName, "GCM")
David Benjaminf7768e42014-08-31 02:06:47 -0400981}
982
Adam Langley95c29f32014-06-20 12:00:00 -0700983func addCipherSuiteTests() {
984 for _, suite := range testCipherSuites {
David Benjamin48cae082014-10-27 01:06:24 -0400985 const psk = "12345"
986 const pskIdentity = "luggage combo"
987
Adam Langley95c29f32014-06-20 12:00:00 -0700988 var cert Certificate
David Benjamin025b3d32014-07-01 19:53:04 -0400989 var certFile string
990 var keyFile string
David Benjamin8b8c0062014-11-23 02:47:52 -0500991 if hasComponent(suite.name, "ECDSA") {
Adam Langley95c29f32014-06-20 12:00:00 -0700992 cert = getECDSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -0400993 certFile = ecdsaCertificateFile
994 keyFile = ecdsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -0700995 } else {
996 cert = getRSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -0400997 certFile = rsaCertificateFile
998 keyFile = rsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -0700999 }
1000
David Benjamin48cae082014-10-27 01:06:24 -04001001 var flags []string
David Benjamin8b8c0062014-11-23 02:47:52 -05001002 if hasComponent(suite.name, "PSK") {
David Benjamin48cae082014-10-27 01:06:24 -04001003 flags = append(flags,
1004 "-psk", psk,
1005 "-psk-identity", pskIdentity)
1006 }
1007
Adam Langley95c29f32014-06-20 12:00:00 -07001008 for _, ver := range tlsVersions {
David Benjaminf7768e42014-08-31 02:06:47 -04001009 if ver.version < VersionTLS12 && isTLS12Only(suite.name) {
Adam Langley95c29f32014-06-20 12:00:00 -07001010 continue
1011 }
1012
David Benjamin025b3d32014-07-01 19:53:04 -04001013 testCases = append(testCases, testCase{
1014 testType: clientTest,
1015 name: ver.name + "-" + suite.name + "-client",
Adam Langley95c29f32014-06-20 12:00:00 -07001016 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001017 MinVersion: ver.version,
1018 MaxVersion: ver.version,
1019 CipherSuites: []uint16{suite.id},
1020 Certificates: []Certificate{cert},
1021 PreSharedKey: []byte(psk),
1022 PreSharedKeyIdentity: pskIdentity,
Adam Langley95c29f32014-06-20 12:00:00 -07001023 },
David Benjamin48cae082014-10-27 01:06:24 -04001024 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001025 resumeSession: true,
Adam Langley95c29f32014-06-20 12:00:00 -07001026 })
David Benjamin025b3d32014-07-01 19:53:04 -04001027
David Benjamin76d8abe2014-08-14 16:25:34 -04001028 testCases = append(testCases, testCase{
1029 testType: serverTest,
1030 name: ver.name + "-" + suite.name + "-server",
1031 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001032 MinVersion: ver.version,
1033 MaxVersion: ver.version,
1034 CipherSuites: []uint16{suite.id},
1035 Certificates: []Certificate{cert},
1036 PreSharedKey: []byte(psk),
1037 PreSharedKeyIdentity: pskIdentity,
David Benjamin76d8abe2014-08-14 16:25:34 -04001038 },
1039 certFile: certFile,
1040 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001041 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001042 resumeSession: true,
David Benjamin76d8abe2014-08-14 16:25:34 -04001043 })
David Benjamin6fd297b2014-08-11 18:43:38 -04001044
David Benjamin8b8c0062014-11-23 02:47:52 -05001045 if ver.hasDTLS && isDTLSCipher(suite.name) {
David Benjamin6fd297b2014-08-11 18:43:38 -04001046 testCases = append(testCases, testCase{
1047 testType: clientTest,
1048 protocol: dtls,
1049 name: "D" + ver.name + "-" + suite.name + "-client",
1050 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001051 MinVersion: ver.version,
1052 MaxVersion: ver.version,
1053 CipherSuites: []uint16{suite.id},
1054 Certificates: []Certificate{cert},
1055 PreSharedKey: []byte(psk),
1056 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001057 },
David Benjamin48cae082014-10-27 01:06:24 -04001058 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001059 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001060 })
1061 testCases = append(testCases, testCase{
1062 testType: serverTest,
1063 protocol: dtls,
1064 name: "D" + ver.name + "-" + suite.name + "-server",
1065 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001066 MinVersion: ver.version,
1067 MaxVersion: ver.version,
1068 CipherSuites: []uint16{suite.id},
1069 Certificates: []Certificate{cert},
1070 PreSharedKey: []byte(psk),
1071 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001072 },
1073 certFile: certFile,
1074 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001075 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001076 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001077 })
1078 }
Adam Langley95c29f32014-06-20 12:00:00 -07001079 }
1080 }
1081}
1082
1083func addBadECDSASignatureTests() {
1084 for badR := BadValue(1); badR < NumBadValues; badR++ {
1085 for badS := BadValue(1); badS < NumBadValues; badS++ {
David Benjamin025b3d32014-07-01 19:53:04 -04001086 testCases = append(testCases, testCase{
Adam Langley95c29f32014-06-20 12:00:00 -07001087 name: fmt.Sprintf("BadECDSA-%d-%d", badR, badS),
1088 config: Config{
1089 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
1090 Certificates: []Certificate{getECDSACertificate()},
1091 Bugs: ProtocolBugs{
1092 BadECDSAR: badR,
1093 BadECDSAS: badS,
1094 },
1095 },
1096 shouldFail: true,
1097 expectedError: "SIGNATURE",
1098 })
1099 }
1100 }
1101}
1102
Adam Langley80842bd2014-06-20 12:00:00 -07001103func addCBCPaddingTests() {
David Benjamin025b3d32014-07-01 19:53:04 -04001104 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001105 name: "MaxCBCPadding",
1106 config: Config{
1107 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1108 Bugs: ProtocolBugs{
1109 MaxPadding: true,
1110 },
1111 },
1112 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1113 })
David Benjamin025b3d32014-07-01 19:53:04 -04001114 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001115 name: "BadCBCPadding",
1116 config: Config{
1117 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1118 Bugs: ProtocolBugs{
1119 PaddingFirstByteBad: true,
1120 },
1121 },
1122 shouldFail: true,
1123 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1124 })
1125 // OpenSSL previously had an issue where the first byte of padding in
1126 // 255 bytes of padding wasn't checked.
David Benjamin025b3d32014-07-01 19:53:04 -04001127 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001128 name: "BadCBCPadding255",
1129 config: Config{
1130 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1131 Bugs: ProtocolBugs{
1132 MaxPadding: true,
1133 PaddingFirstByteBadIf255: true,
1134 },
1135 },
1136 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1137 shouldFail: true,
1138 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1139 })
1140}
1141
Kenny Root7fdeaf12014-08-05 15:23:37 -07001142func addCBCSplittingTests() {
1143 testCases = append(testCases, testCase{
1144 name: "CBCRecordSplitting",
1145 config: Config{
1146 MaxVersion: VersionTLS10,
1147 MinVersion: VersionTLS10,
1148 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1149 },
1150 messageLen: -1, // read until EOF
1151 flags: []string{
1152 "-async",
1153 "-write-different-record-sizes",
1154 "-cbc-record-splitting",
1155 },
David Benjamina8e3e0e2014-08-06 22:11:10 -04001156 })
1157 testCases = append(testCases, testCase{
Kenny Root7fdeaf12014-08-05 15:23:37 -07001158 name: "CBCRecordSplittingPartialWrite",
1159 config: Config{
1160 MaxVersion: VersionTLS10,
1161 MinVersion: VersionTLS10,
1162 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1163 },
1164 messageLen: -1, // read until EOF
1165 flags: []string{
1166 "-async",
1167 "-write-different-record-sizes",
1168 "-cbc-record-splitting",
1169 "-partial-write",
1170 },
1171 })
1172}
1173
David Benjamin636293b2014-07-08 17:59:18 -04001174func addClientAuthTests() {
David Benjamin407a10c2014-07-16 12:58:59 -04001175 // Add a dummy cert pool to stress certificate authority parsing.
1176 // TODO(davidben): Add tests that those values parse out correctly.
1177 certPool := x509.NewCertPool()
1178 cert, err := x509.ParseCertificate(rsaCertificate.Certificate[0])
1179 if err != nil {
1180 panic(err)
1181 }
1182 certPool.AddCert(cert)
1183
David Benjamin636293b2014-07-08 17:59:18 -04001184 for _, ver := range tlsVersions {
David Benjamin636293b2014-07-08 17:59:18 -04001185 testCases = append(testCases, testCase{
1186 testType: clientTest,
David Benjamin67666e72014-07-12 15:47:52 -04001187 name: ver.name + "-Client-ClientAuth-RSA",
David Benjamin636293b2014-07-08 17:59:18 -04001188 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001189 MinVersion: ver.version,
1190 MaxVersion: ver.version,
1191 ClientAuth: RequireAnyClientCert,
1192 ClientCAs: certPool,
David Benjamin636293b2014-07-08 17:59:18 -04001193 },
1194 flags: []string{
1195 "-cert-file", rsaCertificateFile,
1196 "-key-file", rsaKeyFile,
1197 },
1198 })
1199 testCases = append(testCases, testCase{
David Benjamin67666e72014-07-12 15:47:52 -04001200 testType: serverTest,
1201 name: ver.name + "-Server-ClientAuth-RSA",
1202 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001203 MinVersion: ver.version,
1204 MaxVersion: ver.version,
David Benjamin67666e72014-07-12 15:47:52 -04001205 Certificates: []Certificate{rsaCertificate},
1206 },
1207 flags: []string{"-require-any-client-certificate"},
1208 })
David Benjamine098ec22014-08-27 23:13:20 -04001209 if ver.version != VersionSSL30 {
1210 testCases = append(testCases, testCase{
1211 testType: serverTest,
1212 name: ver.name + "-Server-ClientAuth-ECDSA",
1213 config: Config{
1214 MinVersion: ver.version,
1215 MaxVersion: ver.version,
1216 Certificates: []Certificate{ecdsaCertificate},
1217 },
1218 flags: []string{"-require-any-client-certificate"},
1219 })
1220 testCases = append(testCases, testCase{
1221 testType: clientTest,
1222 name: ver.name + "-Client-ClientAuth-ECDSA",
1223 config: Config{
1224 MinVersion: ver.version,
1225 MaxVersion: ver.version,
1226 ClientAuth: RequireAnyClientCert,
1227 ClientCAs: certPool,
1228 },
1229 flags: []string{
1230 "-cert-file", ecdsaCertificateFile,
1231 "-key-file", ecdsaKeyFile,
1232 },
1233 })
1234 }
David Benjamin636293b2014-07-08 17:59:18 -04001235 }
1236}
1237
Adam Langley75712922014-10-10 16:23:43 -07001238func addExtendedMasterSecretTests() {
1239 const expectEMSFlag = "-expect-extended-master-secret"
1240
1241 for _, with := range []bool{false, true} {
1242 prefix := "No"
1243 var flags []string
1244 if with {
1245 prefix = ""
1246 flags = []string{expectEMSFlag}
1247 }
1248
1249 for _, isClient := range []bool{false, true} {
1250 suffix := "-Server"
1251 testType := serverTest
1252 if isClient {
1253 suffix = "-Client"
1254 testType = clientTest
1255 }
1256
1257 for _, ver := range tlsVersions {
1258 test := testCase{
1259 testType: testType,
1260 name: prefix + "ExtendedMasterSecret-" + ver.name + suffix,
1261 config: Config{
1262 MinVersion: ver.version,
1263 MaxVersion: ver.version,
1264 Bugs: ProtocolBugs{
1265 NoExtendedMasterSecret: !with,
1266 RequireExtendedMasterSecret: with,
1267 },
1268 },
David Benjamin48cae082014-10-27 01:06:24 -04001269 flags: flags,
1270 shouldFail: ver.version == VersionSSL30 && with,
Adam Langley75712922014-10-10 16:23:43 -07001271 }
1272 if test.shouldFail {
1273 test.expectedLocalError = "extended master secret required but not supported by peer"
1274 }
1275 testCases = append(testCases, test)
1276 }
1277 }
1278 }
1279
1280 // When a session is resumed, it should still be aware that its master
1281 // secret was generated via EMS and thus it's safe to use tls-unique.
1282 testCases = append(testCases, testCase{
1283 name: "ExtendedMasterSecret-Resume",
1284 config: Config{
1285 Bugs: ProtocolBugs{
1286 RequireExtendedMasterSecret: true,
1287 },
1288 },
1289 flags: []string{expectEMSFlag},
1290 resumeSession: true,
1291 })
1292}
1293
David Benjamin43ec06f2014-08-05 02:28:57 -04001294// Adds tests that try to cover the range of the handshake state machine, under
1295// various conditions. Some of these are redundant with other tests, but they
1296// only cover the synchronous case.
David Benjamin6fd297b2014-08-11 18:43:38 -04001297func addStateMachineCoverageTests(async, splitHandshake bool, protocol protocol) {
David Benjamin43ec06f2014-08-05 02:28:57 -04001298 var suffix string
1299 var flags []string
1300 var maxHandshakeRecordLength int
David Benjamin6fd297b2014-08-11 18:43:38 -04001301 if protocol == dtls {
1302 suffix = "-DTLS"
1303 }
David Benjamin43ec06f2014-08-05 02:28:57 -04001304 if async {
David Benjamin6fd297b2014-08-11 18:43:38 -04001305 suffix += "-Async"
David Benjamin43ec06f2014-08-05 02:28:57 -04001306 flags = append(flags, "-async")
1307 } else {
David Benjamin6fd297b2014-08-11 18:43:38 -04001308 suffix += "-Sync"
David Benjamin43ec06f2014-08-05 02:28:57 -04001309 }
1310 if splitHandshake {
1311 suffix += "-SplitHandshakeRecords"
David Benjamin98214542014-08-07 18:02:39 -04001312 maxHandshakeRecordLength = 1
David Benjamin43ec06f2014-08-05 02:28:57 -04001313 }
1314
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001315 // Basic handshake, with resumption. Client and server,
1316 // session ID and session ticket.
David Benjamin43ec06f2014-08-05 02:28:57 -04001317 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001318 protocol: protocol,
1319 name: "Basic-Client" + suffix,
David Benjamin43ec06f2014-08-05 02:28:57 -04001320 config: Config{
1321 Bugs: ProtocolBugs{
1322 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1323 },
1324 },
David Benjaminbed9aae2014-08-07 19:13:38 -04001325 flags: flags,
1326 resumeSession: true,
1327 })
1328 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001329 protocol: protocol,
1330 name: "Basic-Client-RenewTicket" + suffix,
David Benjaminbed9aae2014-08-07 19:13:38 -04001331 config: Config{
1332 Bugs: ProtocolBugs{
1333 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1334 RenewTicketOnResume: true,
1335 },
1336 },
1337 flags: flags,
1338 resumeSession: true,
David Benjamin43ec06f2014-08-05 02:28:57 -04001339 })
1340 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001341 protocol: protocol,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001342 name: "Basic-Client-NoTicket" + suffix,
1343 config: Config{
1344 SessionTicketsDisabled: true,
1345 Bugs: ProtocolBugs{
1346 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1347 },
1348 },
1349 flags: flags,
1350 resumeSession: true,
1351 })
1352 testCases = append(testCases, testCase{
1353 protocol: protocol,
David Benjamin43ec06f2014-08-05 02:28:57 -04001354 testType: serverTest,
1355 name: "Basic-Server" + suffix,
1356 config: Config{
1357 Bugs: ProtocolBugs{
1358 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1359 },
1360 },
David Benjaminbed9aae2014-08-07 19:13:38 -04001361 flags: flags,
1362 resumeSession: true,
David Benjamin43ec06f2014-08-05 02:28:57 -04001363 })
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001364 testCases = append(testCases, testCase{
1365 protocol: protocol,
1366 testType: serverTest,
1367 name: "Basic-Server-NoTickets" + suffix,
1368 config: Config{
1369 SessionTicketsDisabled: true,
1370 Bugs: ProtocolBugs{
1371 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1372 },
1373 },
1374 flags: flags,
1375 resumeSession: true,
1376 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001377
David Benjamin6fd297b2014-08-11 18:43:38 -04001378 // TLS client auth.
1379 testCases = append(testCases, testCase{
1380 protocol: protocol,
1381 testType: clientTest,
1382 name: "ClientAuth-Client" + suffix,
1383 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001384 ClientAuth: RequireAnyClientCert,
David Benjamin6fd297b2014-08-11 18:43:38 -04001385 Bugs: ProtocolBugs{
1386 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1387 },
1388 },
1389 flags: append(flags,
1390 "-cert-file", rsaCertificateFile,
1391 "-key-file", rsaKeyFile),
1392 })
1393 testCases = append(testCases, testCase{
1394 protocol: protocol,
1395 testType: serverTest,
1396 name: "ClientAuth-Server" + suffix,
1397 config: Config{
1398 Certificates: []Certificate{rsaCertificate},
1399 },
1400 flags: append(flags, "-require-any-client-certificate"),
1401 })
1402
David Benjamin43ec06f2014-08-05 02:28:57 -04001403 // No session ticket support; server doesn't send NewSessionTicket.
1404 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001405 protocol: protocol,
1406 name: "SessionTicketsDisabled-Client" + suffix,
David Benjamin43ec06f2014-08-05 02:28:57 -04001407 config: Config{
1408 SessionTicketsDisabled: true,
1409 Bugs: ProtocolBugs{
1410 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1411 },
1412 },
1413 flags: flags,
1414 })
1415 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001416 protocol: protocol,
David Benjamin43ec06f2014-08-05 02:28:57 -04001417 testType: serverTest,
1418 name: "SessionTicketsDisabled-Server" + suffix,
1419 config: Config{
1420 SessionTicketsDisabled: true,
1421 Bugs: ProtocolBugs{
1422 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1423 },
1424 },
1425 flags: flags,
1426 })
1427
David Benjamin48cae082014-10-27 01:06:24 -04001428 // Skip ServerKeyExchange in PSK key exchange if there's no
1429 // identity hint.
1430 testCases = append(testCases, testCase{
1431 protocol: protocol,
1432 name: "EmptyPSKHint-Client" + suffix,
1433 config: Config{
1434 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
1435 PreSharedKey: []byte("secret"),
1436 Bugs: ProtocolBugs{
1437 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1438 },
1439 },
1440 flags: append(flags, "-psk", "secret"),
1441 })
1442 testCases = append(testCases, testCase{
1443 protocol: protocol,
1444 testType: serverTest,
1445 name: "EmptyPSKHint-Server" + suffix,
1446 config: Config{
1447 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
1448 PreSharedKey: []byte("secret"),
1449 Bugs: ProtocolBugs{
1450 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1451 },
1452 },
1453 flags: append(flags, "-psk", "secret"),
1454 })
1455
David Benjamin6fd297b2014-08-11 18:43:38 -04001456 if protocol == tls {
1457 // NPN on client and server; results in post-handshake message.
1458 testCases = append(testCases, testCase{
1459 protocol: protocol,
1460 name: "NPN-Client" + suffix,
1461 config: Config{
David Benjaminae2888f2014-09-06 12:58:58 -04001462 NextProtos: []string{"foo"},
David Benjamin6fd297b2014-08-11 18:43:38 -04001463 Bugs: ProtocolBugs{
1464 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1465 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001466 },
David Benjaminfc7b0862014-09-06 13:21:53 -04001467 flags: append(flags, "-select-next-proto", "foo"),
1468 expectedNextProto: "foo",
1469 expectedNextProtoType: npn,
David Benjamin6fd297b2014-08-11 18:43:38 -04001470 })
1471 testCases = append(testCases, testCase{
1472 protocol: protocol,
1473 testType: serverTest,
1474 name: "NPN-Server" + suffix,
1475 config: Config{
1476 NextProtos: []string{"bar"},
1477 Bugs: ProtocolBugs{
1478 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1479 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001480 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001481 flags: append(flags,
1482 "-advertise-npn", "\x03foo\x03bar\x03baz",
1483 "-expect-next-proto", "bar"),
David Benjaminfc7b0862014-09-06 13:21:53 -04001484 expectedNextProto: "bar",
1485 expectedNextProtoType: npn,
David Benjamin6fd297b2014-08-11 18:43:38 -04001486 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001487
David Benjamin6fd297b2014-08-11 18:43:38 -04001488 // Client does False Start and negotiates NPN.
1489 testCases = append(testCases, testCase{
1490 protocol: protocol,
1491 name: "FalseStart" + suffix,
1492 config: Config{
1493 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1494 NextProtos: []string{"foo"},
1495 Bugs: ProtocolBugs{
David Benjamine58c4f52014-08-24 03:47:07 -04001496 ExpectFalseStart: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001497 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1498 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001499 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001500 flags: append(flags,
1501 "-false-start",
1502 "-select-next-proto", "foo"),
David Benjamine58c4f52014-08-24 03:47:07 -04001503 shimWritesFirst: true,
1504 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001505 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001506
David Benjaminae2888f2014-09-06 12:58:58 -04001507 // Client does False Start and negotiates ALPN.
1508 testCases = append(testCases, testCase{
1509 protocol: protocol,
1510 name: "FalseStart-ALPN" + suffix,
1511 config: Config{
1512 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1513 NextProtos: []string{"foo"},
1514 Bugs: ProtocolBugs{
1515 ExpectFalseStart: true,
1516 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1517 },
1518 },
1519 flags: append(flags,
1520 "-false-start",
1521 "-advertise-alpn", "\x03foo"),
1522 shimWritesFirst: true,
1523 resumeSession: true,
1524 })
1525
David Benjamin6fd297b2014-08-11 18:43:38 -04001526 // False Start without session tickets.
1527 testCases = append(testCases, testCase{
1528 name: "FalseStart-SessionTicketsDisabled",
1529 config: Config{
1530 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1531 NextProtos: []string{"foo"},
1532 SessionTicketsDisabled: true,
David Benjamin4e99c522014-08-24 01:45:30 -04001533 Bugs: ProtocolBugs{
David Benjamine58c4f52014-08-24 03:47:07 -04001534 ExpectFalseStart: true,
David Benjamin4e99c522014-08-24 01:45:30 -04001535 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1536 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001537 },
David Benjamin4e99c522014-08-24 01:45:30 -04001538 flags: append(flags,
David Benjamin6fd297b2014-08-11 18:43:38 -04001539 "-false-start",
1540 "-select-next-proto", "foo",
David Benjamin4e99c522014-08-24 01:45:30 -04001541 ),
David Benjamine58c4f52014-08-24 03:47:07 -04001542 shimWritesFirst: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001543 })
David Benjamin1e7f8d72014-08-08 12:27:04 -04001544
David Benjamina08e49d2014-08-24 01:46:07 -04001545 // Server parses a V2ClientHello.
David Benjamin6fd297b2014-08-11 18:43:38 -04001546 testCases = append(testCases, testCase{
1547 protocol: protocol,
1548 testType: serverTest,
1549 name: "SendV2ClientHello" + suffix,
1550 config: Config{
1551 // Choose a cipher suite that does not involve
1552 // elliptic curves, so no extensions are
1553 // involved.
1554 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
1555 Bugs: ProtocolBugs{
1556 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1557 SendV2ClientHello: true,
1558 },
David Benjamin1e7f8d72014-08-08 12:27:04 -04001559 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001560 flags: flags,
1561 })
David Benjamina08e49d2014-08-24 01:46:07 -04001562
1563 // Client sends a Channel ID.
1564 testCases = append(testCases, testCase{
1565 protocol: protocol,
1566 name: "ChannelID-Client" + suffix,
1567 config: Config{
1568 RequestChannelID: true,
1569 Bugs: ProtocolBugs{
1570 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1571 },
1572 },
1573 flags: append(flags,
1574 "-send-channel-id", channelIDKeyFile,
1575 ),
1576 resumeSession: true,
1577 expectChannelID: true,
1578 })
1579
1580 // Server accepts a Channel ID.
1581 testCases = append(testCases, testCase{
1582 protocol: protocol,
1583 testType: serverTest,
1584 name: "ChannelID-Server" + suffix,
1585 config: Config{
1586 ChannelID: channelIDKey,
1587 Bugs: ProtocolBugs{
1588 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1589 },
1590 },
1591 flags: append(flags,
1592 "-expect-channel-id",
1593 base64.StdEncoding.EncodeToString(channelIDBytes),
1594 ),
1595 resumeSession: true,
1596 expectChannelID: true,
1597 })
David Benjamin6fd297b2014-08-11 18:43:38 -04001598 } else {
1599 testCases = append(testCases, testCase{
1600 protocol: protocol,
1601 name: "SkipHelloVerifyRequest" + suffix,
1602 config: Config{
1603 Bugs: ProtocolBugs{
1604 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1605 SkipHelloVerifyRequest: true,
1606 },
1607 },
1608 flags: flags,
1609 })
1610
1611 testCases = append(testCases, testCase{
1612 testType: serverTest,
1613 protocol: protocol,
1614 name: "CookieExchange" + suffix,
1615 config: Config{
1616 Bugs: ProtocolBugs{
1617 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1618 },
1619 },
1620 flags: append(flags, "-cookie-exchange"),
1621 })
1622 }
David Benjamin43ec06f2014-08-05 02:28:57 -04001623}
1624
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001625func addVersionNegotiationTests() {
1626 for i, shimVers := range tlsVersions {
1627 // Assemble flags to disable all newer versions on the shim.
1628 var flags []string
1629 for _, vers := range tlsVersions[i+1:] {
1630 flags = append(flags, vers.flag)
1631 }
1632
1633 for _, runnerVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05001634 protocols := []protocol{tls}
1635 if runnerVers.hasDTLS && shimVers.hasDTLS {
1636 protocols = append(protocols, dtls)
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001637 }
David Benjamin8b8c0062014-11-23 02:47:52 -05001638 for _, protocol := range protocols {
1639 expectedVersion := shimVers.version
1640 if runnerVers.version < shimVers.version {
1641 expectedVersion = runnerVers.version
1642 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001643
David Benjamin8b8c0062014-11-23 02:47:52 -05001644 suffix := shimVers.name + "-" + runnerVers.name
1645 if protocol == dtls {
1646 suffix += "-DTLS"
1647 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001648
David Benjamin1eb367c2014-12-12 18:17:51 -05001649 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
1650
David Benjamin1e29a6b2014-12-10 02:27:24 -05001651 clientVers := shimVers.version
1652 if clientVers > VersionTLS10 {
1653 clientVers = VersionTLS10
1654 }
David Benjamin8b8c0062014-11-23 02:47:52 -05001655 testCases = append(testCases, testCase{
1656 protocol: protocol,
1657 testType: clientTest,
1658 name: "VersionNegotiation-Client-" + suffix,
1659 config: Config{
1660 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05001661 Bugs: ProtocolBugs{
1662 ExpectInitialRecordVersion: clientVers,
1663 },
David Benjamin8b8c0062014-11-23 02:47:52 -05001664 },
1665 flags: flags,
1666 expectedVersion: expectedVersion,
1667 })
David Benjamin1eb367c2014-12-12 18:17:51 -05001668 testCases = append(testCases, testCase{
1669 protocol: protocol,
1670 testType: clientTest,
1671 name: "VersionNegotiation-Client2-" + suffix,
1672 config: Config{
1673 MaxVersion: runnerVers.version,
1674 Bugs: ProtocolBugs{
1675 ExpectInitialRecordVersion: clientVers,
1676 },
1677 },
1678 flags: []string{"-max-version", shimVersFlag},
1679 expectedVersion: expectedVersion,
1680 })
David Benjamin8b8c0062014-11-23 02:47:52 -05001681
1682 testCases = append(testCases, testCase{
1683 protocol: protocol,
1684 testType: serverTest,
1685 name: "VersionNegotiation-Server-" + suffix,
1686 config: Config{
1687 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05001688 Bugs: ProtocolBugs{
1689 ExpectInitialRecordVersion: expectedVersion,
1690 },
David Benjamin8b8c0062014-11-23 02:47:52 -05001691 },
1692 flags: flags,
1693 expectedVersion: expectedVersion,
1694 })
David Benjamin1eb367c2014-12-12 18:17:51 -05001695 testCases = append(testCases, testCase{
1696 protocol: protocol,
1697 testType: serverTest,
1698 name: "VersionNegotiation-Server2-" + suffix,
1699 config: Config{
1700 MaxVersion: runnerVers.version,
1701 Bugs: ProtocolBugs{
1702 ExpectInitialRecordVersion: expectedVersion,
1703 },
1704 },
1705 flags: []string{"-max-version", shimVersFlag},
1706 expectedVersion: expectedVersion,
1707 })
David Benjamin8b8c0062014-11-23 02:47:52 -05001708 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001709 }
1710 }
1711}
1712
David Benjaminaccb4542014-12-12 23:44:33 -05001713func addMinimumVersionTests() {
1714 for i, shimVers := range tlsVersions {
1715 // Assemble flags to disable all older versions on the shim.
1716 var flags []string
1717 for _, vers := range tlsVersions[:i] {
1718 flags = append(flags, vers.flag)
1719 }
1720
1721 for _, runnerVers := range tlsVersions {
1722 protocols := []protocol{tls}
1723 if runnerVers.hasDTLS && shimVers.hasDTLS {
1724 protocols = append(protocols, dtls)
1725 }
1726 for _, protocol := range protocols {
1727 suffix := shimVers.name + "-" + runnerVers.name
1728 if protocol == dtls {
1729 suffix += "-DTLS"
1730 }
1731 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
1732
David Benjaminaccb4542014-12-12 23:44:33 -05001733 var expectedVersion uint16
1734 var shouldFail bool
1735 var expectedError string
David Benjamin87909c02014-12-13 01:55:01 -05001736 var expectedLocalError string
David Benjaminaccb4542014-12-12 23:44:33 -05001737 if runnerVers.version >= shimVers.version {
1738 expectedVersion = runnerVers.version
1739 } else {
1740 shouldFail = true
1741 expectedError = ":UNSUPPORTED_PROTOCOL:"
David Benjamin87909c02014-12-13 01:55:01 -05001742 if runnerVers.version > VersionSSL30 {
1743 expectedLocalError = "remote error: protocol version not supported"
1744 } else {
1745 expectedLocalError = "remote error: handshake failure"
1746 }
David Benjaminaccb4542014-12-12 23:44:33 -05001747 }
1748
1749 testCases = append(testCases, testCase{
1750 protocol: protocol,
1751 testType: clientTest,
1752 name: "MinimumVersion-Client-" + suffix,
1753 config: Config{
1754 MaxVersion: runnerVers.version,
1755 },
David Benjamin87909c02014-12-13 01:55:01 -05001756 flags: flags,
1757 expectedVersion: expectedVersion,
1758 shouldFail: shouldFail,
1759 expectedError: expectedError,
1760 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05001761 })
1762 testCases = append(testCases, testCase{
1763 protocol: protocol,
1764 testType: clientTest,
1765 name: "MinimumVersion-Client2-" + suffix,
1766 config: Config{
1767 MaxVersion: runnerVers.version,
1768 },
David Benjamin87909c02014-12-13 01:55:01 -05001769 flags: []string{"-min-version", shimVersFlag},
1770 expectedVersion: expectedVersion,
1771 shouldFail: shouldFail,
1772 expectedError: expectedError,
1773 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05001774 })
1775
1776 testCases = append(testCases, testCase{
1777 protocol: protocol,
1778 testType: serverTest,
1779 name: "MinimumVersion-Server-" + suffix,
1780 config: Config{
1781 MaxVersion: runnerVers.version,
1782 },
David Benjamin87909c02014-12-13 01:55:01 -05001783 flags: flags,
1784 expectedVersion: expectedVersion,
1785 shouldFail: shouldFail,
1786 expectedError: expectedError,
1787 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05001788 })
1789 testCases = append(testCases, testCase{
1790 protocol: protocol,
1791 testType: serverTest,
1792 name: "MinimumVersion-Server2-" + suffix,
1793 config: Config{
1794 MaxVersion: runnerVers.version,
1795 },
David Benjamin87909c02014-12-13 01:55:01 -05001796 flags: []string{"-min-version", shimVersFlag},
1797 expectedVersion: expectedVersion,
1798 shouldFail: shouldFail,
1799 expectedError: expectedError,
1800 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05001801 })
1802 }
1803 }
1804 }
1805}
1806
David Benjamin5c24a1d2014-08-31 00:59:27 -04001807func addD5BugTests() {
1808 testCases = append(testCases, testCase{
1809 testType: serverTest,
1810 name: "D5Bug-NoQuirk-Reject",
1811 config: Config{
1812 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
1813 Bugs: ProtocolBugs{
1814 SSL3RSAKeyExchange: true,
1815 },
1816 },
1817 shouldFail: true,
1818 expectedError: ":TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG:",
1819 })
1820 testCases = append(testCases, testCase{
1821 testType: serverTest,
1822 name: "D5Bug-Quirk-Normal",
1823 config: Config{
1824 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
1825 },
1826 flags: []string{"-tls-d5-bug"},
1827 })
1828 testCases = append(testCases, testCase{
1829 testType: serverTest,
1830 name: "D5Bug-Quirk-Bug",
1831 config: Config{
1832 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
1833 Bugs: ProtocolBugs{
1834 SSL3RSAKeyExchange: true,
1835 },
1836 },
1837 flags: []string{"-tls-d5-bug"},
1838 })
1839}
1840
David Benjamine78bfde2014-09-06 12:45:15 -04001841func addExtensionTests() {
1842 testCases = append(testCases, testCase{
1843 testType: clientTest,
1844 name: "DuplicateExtensionClient",
1845 config: Config{
1846 Bugs: ProtocolBugs{
1847 DuplicateExtension: true,
1848 },
1849 },
1850 shouldFail: true,
1851 expectedLocalError: "remote error: error decoding message",
1852 })
1853 testCases = append(testCases, testCase{
1854 testType: serverTest,
1855 name: "DuplicateExtensionServer",
1856 config: Config{
1857 Bugs: ProtocolBugs{
1858 DuplicateExtension: true,
1859 },
1860 },
1861 shouldFail: true,
1862 expectedLocalError: "remote error: error decoding message",
1863 })
1864 testCases = append(testCases, testCase{
1865 testType: clientTest,
1866 name: "ServerNameExtensionClient",
1867 config: Config{
1868 Bugs: ProtocolBugs{
1869 ExpectServerName: "example.com",
1870 },
1871 },
1872 flags: []string{"-host-name", "example.com"},
1873 })
1874 testCases = append(testCases, testCase{
1875 testType: clientTest,
1876 name: "ServerNameExtensionClient",
1877 config: Config{
1878 Bugs: ProtocolBugs{
1879 ExpectServerName: "mismatch.com",
1880 },
1881 },
1882 flags: []string{"-host-name", "example.com"},
1883 shouldFail: true,
1884 expectedLocalError: "tls: unexpected server name",
1885 })
1886 testCases = append(testCases, testCase{
1887 testType: clientTest,
1888 name: "ServerNameExtensionClient",
1889 config: Config{
1890 Bugs: ProtocolBugs{
1891 ExpectServerName: "missing.com",
1892 },
1893 },
1894 shouldFail: true,
1895 expectedLocalError: "tls: unexpected server name",
1896 })
1897 testCases = append(testCases, testCase{
1898 testType: serverTest,
1899 name: "ServerNameExtensionServer",
1900 config: Config{
1901 ServerName: "example.com",
1902 },
1903 flags: []string{"-expect-server-name", "example.com"},
1904 resumeSession: true,
1905 })
David Benjaminae2888f2014-09-06 12:58:58 -04001906 testCases = append(testCases, testCase{
1907 testType: clientTest,
1908 name: "ALPNClient",
1909 config: Config{
1910 NextProtos: []string{"foo"},
1911 },
1912 flags: []string{
1913 "-advertise-alpn", "\x03foo\x03bar\x03baz",
1914 "-expect-alpn", "foo",
1915 },
David Benjaminfc7b0862014-09-06 13:21:53 -04001916 expectedNextProto: "foo",
1917 expectedNextProtoType: alpn,
1918 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04001919 })
1920 testCases = append(testCases, testCase{
1921 testType: serverTest,
1922 name: "ALPNServer",
1923 config: Config{
1924 NextProtos: []string{"foo", "bar", "baz"},
1925 },
1926 flags: []string{
1927 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
1928 "-select-alpn", "foo",
1929 },
David Benjaminfc7b0862014-09-06 13:21:53 -04001930 expectedNextProto: "foo",
1931 expectedNextProtoType: alpn,
1932 resumeSession: true,
1933 })
1934 // Test that the server prefers ALPN over NPN.
1935 testCases = append(testCases, testCase{
1936 testType: serverTest,
1937 name: "ALPNServer-Preferred",
1938 config: Config{
1939 NextProtos: []string{"foo", "bar", "baz"},
1940 },
1941 flags: []string{
1942 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
1943 "-select-alpn", "foo",
1944 "-advertise-npn", "\x03foo\x03bar\x03baz",
1945 },
1946 expectedNextProto: "foo",
1947 expectedNextProtoType: alpn,
1948 resumeSession: true,
1949 })
1950 testCases = append(testCases, testCase{
1951 testType: serverTest,
1952 name: "ALPNServer-Preferred-Swapped",
1953 config: Config{
1954 NextProtos: []string{"foo", "bar", "baz"},
1955 Bugs: ProtocolBugs{
1956 SwapNPNAndALPN: true,
1957 },
1958 },
1959 flags: []string{
1960 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
1961 "-select-alpn", "foo",
1962 "-advertise-npn", "\x03foo\x03bar\x03baz",
1963 },
1964 expectedNextProto: "foo",
1965 expectedNextProtoType: alpn,
1966 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04001967 })
Adam Langley38311732014-10-16 19:04:35 -07001968 // Resume with a corrupt ticket.
1969 testCases = append(testCases, testCase{
1970 testType: serverTest,
1971 name: "CorruptTicket",
1972 config: Config{
1973 Bugs: ProtocolBugs{
1974 CorruptTicket: true,
1975 },
1976 },
1977 resumeSession: true,
1978 flags: []string{"-expect-session-miss"},
1979 })
1980 // Resume with an oversized session id.
1981 testCases = append(testCases, testCase{
1982 testType: serverTest,
1983 name: "OversizedSessionId",
1984 config: Config{
1985 Bugs: ProtocolBugs{
1986 OversizedSessionId: true,
1987 },
1988 },
1989 resumeSession: true,
Adam Langley75712922014-10-10 16:23:43 -07001990 shouldFail: true,
Adam Langley38311732014-10-16 19:04:35 -07001991 expectedError: ":DECODE_ERROR:",
1992 })
David Benjaminca6c8262014-11-15 19:06:08 -05001993 // Basic DTLS-SRTP tests. Include fake profiles to ensure they
1994 // are ignored.
1995 testCases = append(testCases, testCase{
1996 protocol: dtls,
1997 name: "SRTP-Client",
1998 config: Config{
1999 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
2000 },
2001 flags: []string{
2002 "-srtp-profiles",
2003 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2004 },
2005 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2006 })
2007 testCases = append(testCases, testCase{
2008 protocol: dtls,
2009 testType: serverTest,
2010 name: "SRTP-Server",
2011 config: Config{
2012 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
2013 },
2014 flags: []string{
2015 "-srtp-profiles",
2016 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2017 },
2018 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2019 })
2020 // Test that the MKI is ignored.
2021 testCases = append(testCases, testCase{
2022 protocol: dtls,
2023 testType: serverTest,
2024 name: "SRTP-Server-IgnoreMKI",
2025 config: Config{
2026 SRTPProtectionProfiles: []uint16{SRTP_AES128_CM_HMAC_SHA1_80},
2027 Bugs: ProtocolBugs{
2028 SRTPMasterKeyIdentifer: "bogus",
2029 },
2030 },
2031 flags: []string{
2032 "-srtp-profiles",
2033 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2034 },
2035 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2036 })
2037 // Test that SRTP isn't negotiated on the server if there were
2038 // no matching profiles.
2039 testCases = append(testCases, testCase{
2040 protocol: dtls,
2041 testType: serverTest,
2042 name: "SRTP-Server-NoMatch",
2043 config: Config{
2044 SRTPProtectionProfiles: []uint16{100, 101, 102},
2045 },
2046 flags: []string{
2047 "-srtp-profiles",
2048 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2049 },
2050 expectedSRTPProtectionProfile: 0,
2051 })
2052 // Test that the server returning an invalid SRTP profile is
2053 // flagged as an error by the client.
2054 testCases = append(testCases, testCase{
2055 protocol: dtls,
2056 name: "SRTP-Client-NoMatch",
2057 config: Config{
2058 Bugs: ProtocolBugs{
2059 SendSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_32,
2060 },
2061 },
2062 flags: []string{
2063 "-srtp-profiles",
2064 "SRTP_AES128_CM_SHA1_80",
2065 },
2066 shouldFail: true,
2067 expectedError: ":BAD_SRTP_PROTECTION_PROFILE_LIST:",
2068 })
David Benjamin61f95272014-11-25 01:55:35 -05002069 // Test OCSP stapling and SCT list.
2070 testCases = append(testCases, testCase{
2071 name: "OCSPStapling",
2072 flags: []string{
2073 "-enable-ocsp-stapling",
2074 "-expect-ocsp-response",
2075 base64.StdEncoding.EncodeToString(testOCSPResponse),
2076 },
2077 })
2078 testCases = append(testCases, testCase{
2079 name: "SignedCertificateTimestampList",
2080 flags: []string{
2081 "-enable-signed-cert-timestamps",
2082 "-expect-signed-cert-timestamps",
2083 base64.StdEncoding.EncodeToString(testSCTList),
2084 },
2085 })
David Benjamine78bfde2014-09-06 12:45:15 -04002086}
2087
David Benjamin01fe8202014-09-24 15:21:44 -04002088func addResumptionVersionTests() {
David Benjamin01fe8202014-09-24 15:21:44 -04002089 for _, sessionVers := range tlsVersions {
David Benjamin01fe8202014-09-24 15:21:44 -04002090 for _, resumeVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05002091 protocols := []protocol{tls}
2092 if sessionVers.hasDTLS && resumeVers.hasDTLS {
2093 protocols = append(protocols, dtls)
David Benjaminbdf5e722014-11-11 00:52:15 -05002094 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002095 for _, protocol := range protocols {
2096 suffix := "-" + sessionVers.name + "-" + resumeVers.name
2097 if protocol == dtls {
2098 suffix += "-DTLS"
2099 }
2100
2101 testCases = append(testCases, testCase{
2102 protocol: protocol,
2103 name: "Resume-Client" + suffix,
2104 resumeSession: true,
2105 config: Config{
2106 MaxVersion: sessionVers.version,
2107 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2108 Bugs: ProtocolBugs{
2109 AllowSessionVersionMismatch: true,
2110 },
2111 },
2112 expectedVersion: sessionVers.version,
2113 resumeConfig: &Config{
2114 MaxVersion: resumeVers.version,
2115 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2116 Bugs: ProtocolBugs{
2117 AllowSessionVersionMismatch: true,
2118 },
2119 },
2120 expectedResumeVersion: resumeVers.version,
2121 })
2122
2123 testCases = append(testCases, testCase{
2124 protocol: protocol,
2125 name: "Resume-Client-NoResume" + suffix,
2126 flags: []string{"-expect-session-miss"},
2127 resumeSession: true,
2128 config: Config{
2129 MaxVersion: sessionVers.version,
2130 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2131 },
2132 expectedVersion: sessionVers.version,
2133 resumeConfig: &Config{
2134 MaxVersion: resumeVers.version,
2135 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2136 },
2137 newSessionsOnResume: true,
2138 expectedResumeVersion: resumeVers.version,
2139 })
2140
2141 var flags []string
2142 if sessionVers.version != resumeVers.version {
2143 flags = append(flags, "-expect-session-miss")
2144 }
2145 testCases = append(testCases, testCase{
2146 protocol: protocol,
2147 testType: serverTest,
2148 name: "Resume-Server" + suffix,
2149 flags: flags,
2150 resumeSession: true,
2151 config: Config{
2152 MaxVersion: sessionVers.version,
2153 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2154 },
2155 expectedVersion: sessionVers.version,
2156 resumeConfig: &Config{
2157 MaxVersion: resumeVers.version,
2158 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2159 },
2160 expectedResumeVersion: resumeVers.version,
2161 })
2162 }
David Benjamin01fe8202014-09-24 15:21:44 -04002163 }
2164 }
2165}
2166
Adam Langley2ae77d22014-10-28 17:29:33 -07002167func addRenegotiationTests() {
2168 testCases = append(testCases, testCase{
2169 testType: serverTest,
2170 name: "Renegotiate-Server",
2171 flags: []string{"-renegotiate"},
2172 shimWritesFirst: true,
2173 })
2174 testCases = append(testCases, testCase{
2175 testType: serverTest,
2176 name: "Renegotiate-Server-EmptyExt",
2177 config: Config{
2178 Bugs: ProtocolBugs{
2179 EmptyRenegotiationInfo: true,
2180 },
2181 },
2182 flags: []string{"-renegotiate"},
2183 shimWritesFirst: true,
2184 shouldFail: true,
2185 expectedError: ":RENEGOTIATION_MISMATCH:",
2186 })
2187 testCases = append(testCases, testCase{
2188 testType: serverTest,
2189 name: "Renegotiate-Server-BadExt",
2190 config: Config{
2191 Bugs: ProtocolBugs{
2192 BadRenegotiationInfo: true,
2193 },
2194 },
2195 flags: []string{"-renegotiate"},
2196 shimWritesFirst: true,
2197 shouldFail: true,
2198 expectedError: ":RENEGOTIATION_MISMATCH:",
2199 })
David Benjaminca6554b2014-11-08 12:31:52 -05002200 testCases = append(testCases, testCase{
2201 testType: serverTest,
2202 name: "Renegotiate-Server-ClientInitiated",
2203 renegotiate: true,
2204 })
2205 testCases = append(testCases, testCase{
2206 testType: serverTest,
2207 name: "Renegotiate-Server-ClientInitiated-NoExt",
2208 renegotiate: true,
2209 config: Config{
2210 Bugs: ProtocolBugs{
2211 NoRenegotiationInfo: true,
2212 },
2213 },
2214 shouldFail: true,
2215 expectedError: ":UNSAFE_LEGACY_RENEGOTIATION_DISABLED:",
2216 })
2217 testCases = append(testCases, testCase{
2218 testType: serverTest,
2219 name: "Renegotiate-Server-ClientInitiated-NoExt-Allowed",
2220 renegotiate: true,
2221 config: Config{
2222 Bugs: ProtocolBugs{
2223 NoRenegotiationInfo: true,
2224 },
2225 },
2226 flags: []string{"-allow-unsafe-legacy-renegotiation"},
2227 })
Adam Langley2ae77d22014-10-28 17:29:33 -07002228 // TODO(agl): test the renegotiation info SCSV.
Adam Langleycf2d4f42014-10-28 19:06:14 -07002229 testCases = append(testCases, testCase{
2230 name: "Renegotiate-Client",
2231 renegotiate: true,
2232 })
2233 testCases = append(testCases, testCase{
2234 name: "Renegotiate-Client-EmptyExt",
2235 renegotiate: true,
2236 config: Config{
2237 Bugs: ProtocolBugs{
2238 EmptyRenegotiationInfo: true,
2239 },
2240 },
2241 shouldFail: true,
2242 expectedError: ":RENEGOTIATION_MISMATCH:",
2243 })
2244 testCases = append(testCases, testCase{
2245 name: "Renegotiate-Client-BadExt",
2246 renegotiate: true,
2247 config: Config{
2248 Bugs: ProtocolBugs{
2249 BadRenegotiationInfo: true,
2250 },
2251 },
2252 shouldFail: true,
2253 expectedError: ":RENEGOTIATION_MISMATCH:",
2254 })
2255 testCases = append(testCases, testCase{
2256 name: "Renegotiate-Client-SwitchCiphers",
2257 renegotiate: true,
2258 config: Config{
2259 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2260 },
2261 renegotiateCiphers: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2262 })
2263 testCases = append(testCases, testCase{
2264 name: "Renegotiate-Client-SwitchCiphers2",
2265 renegotiate: true,
2266 config: Config{
2267 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2268 },
2269 renegotiateCiphers: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2270 })
David Benjaminc44b1df2014-11-23 12:11:01 -05002271 testCases = append(testCases, testCase{
2272 name: "Renegotiate-SameClientVersion",
2273 renegotiate: true,
2274 config: Config{
2275 MaxVersion: VersionTLS10,
2276 Bugs: ProtocolBugs{
2277 RequireSameRenegoClientVersion: true,
2278 },
2279 },
2280 })
Adam Langley2ae77d22014-10-28 17:29:33 -07002281}
2282
David Benjamin5e961c12014-11-07 01:48:35 -05002283func addDTLSReplayTests() {
2284 // Test that sequence number replays are detected.
2285 testCases = append(testCases, testCase{
2286 protocol: dtls,
2287 name: "DTLS-Replay",
2288 replayWrites: true,
2289 })
2290
2291 // Test the outgoing sequence number skipping by values larger
2292 // than the retransmit window.
2293 testCases = append(testCases, testCase{
2294 protocol: dtls,
2295 name: "DTLS-Replay-LargeGaps",
2296 config: Config{
2297 Bugs: ProtocolBugs{
2298 SequenceNumberIncrement: 127,
2299 },
2300 },
2301 replayWrites: true,
2302 })
2303}
2304
Feng Lu41aa3252014-11-21 22:47:56 -08002305func addFastRadioPaddingTests() {
David Benjamin1e29a6b2014-12-10 02:27:24 -05002306 testCases = append(testCases, testCase{
2307 protocol: tls,
2308 name: "FastRadio-Padding",
Feng Lu41aa3252014-11-21 22:47:56 -08002309 config: Config{
2310 Bugs: ProtocolBugs{
2311 RequireFastradioPadding: true,
2312 },
2313 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05002314 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08002315 })
David Benjamin1e29a6b2014-12-10 02:27:24 -05002316 testCases = append(testCases, testCase{
2317 protocol: dtls,
2318 name: "FastRadio-Padding",
Feng Lu41aa3252014-11-21 22:47:56 -08002319 config: Config{
2320 Bugs: ProtocolBugs{
2321 RequireFastradioPadding: true,
2322 },
2323 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05002324 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08002325 })
2326}
2327
David Benjamin000800a2014-11-14 01:43:59 -05002328var testHashes = []struct {
2329 name string
2330 id uint8
2331}{
2332 {"SHA1", hashSHA1},
2333 {"SHA224", hashSHA224},
2334 {"SHA256", hashSHA256},
2335 {"SHA384", hashSHA384},
2336 {"SHA512", hashSHA512},
2337}
2338
2339func addSigningHashTests() {
2340 // Make sure each hash works. Include some fake hashes in the list and
2341 // ensure they're ignored.
2342 for _, hash := range testHashes {
2343 testCases = append(testCases, testCase{
2344 name: "SigningHash-ClientAuth-" + hash.name,
2345 config: Config{
2346 ClientAuth: RequireAnyClientCert,
2347 SignatureAndHashes: []signatureAndHash{
2348 {signatureRSA, 42},
2349 {signatureRSA, hash.id},
2350 {signatureRSA, 255},
2351 },
2352 },
2353 flags: []string{
2354 "-cert-file", rsaCertificateFile,
2355 "-key-file", rsaKeyFile,
2356 },
2357 })
2358
2359 testCases = append(testCases, testCase{
2360 testType: serverTest,
2361 name: "SigningHash-ServerKeyExchange-Sign-" + hash.name,
2362 config: Config{
2363 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2364 SignatureAndHashes: []signatureAndHash{
2365 {signatureRSA, 42},
2366 {signatureRSA, hash.id},
2367 {signatureRSA, 255},
2368 },
2369 },
2370 })
2371 }
2372
2373 // Test that hash resolution takes the signature type into account.
2374 testCases = append(testCases, testCase{
2375 name: "SigningHash-ClientAuth-SignatureType",
2376 config: Config{
2377 ClientAuth: RequireAnyClientCert,
2378 SignatureAndHashes: []signatureAndHash{
2379 {signatureECDSA, hashSHA512},
2380 {signatureRSA, hashSHA384},
2381 {signatureECDSA, hashSHA1},
2382 },
2383 },
2384 flags: []string{
2385 "-cert-file", rsaCertificateFile,
2386 "-key-file", rsaKeyFile,
2387 },
2388 })
2389
2390 testCases = append(testCases, testCase{
2391 testType: serverTest,
2392 name: "SigningHash-ServerKeyExchange-SignatureType",
2393 config: Config{
2394 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2395 SignatureAndHashes: []signatureAndHash{
2396 {signatureECDSA, hashSHA512},
2397 {signatureRSA, hashSHA384},
2398 {signatureECDSA, hashSHA1},
2399 },
2400 },
2401 })
2402
2403 // Test that, if the list is missing, the peer falls back to SHA-1.
2404 testCases = append(testCases, testCase{
2405 name: "SigningHash-ClientAuth-Fallback",
2406 config: Config{
2407 ClientAuth: RequireAnyClientCert,
2408 SignatureAndHashes: []signatureAndHash{
2409 {signatureRSA, hashSHA1},
2410 },
2411 Bugs: ProtocolBugs{
2412 NoSignatureAndHashes: true,
2413 },
2414 },
2415 flags: []string{
2416 "-cert-file", rsaCertificateFile,
2417 "-key-file", rsaKeyFile,
2418 },
2419 })
2420
2421 testCases = append(testCases, testCase{
2422 testType: serverTest,
2423 name: "SigningHash-ServerKeyExchange-Fallback",
2424 config: Config{
2425 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2426 SignatureAndHashes: []signatureAndHash{
2427 {signatureRSA, hashSHA1},
2428 },
2429 Bugs: ProtocolBugs{
2430 NoSignatureAndHashes: true,
2431 },
2432 },
2433 })
2434}
2435
David Benjamin884fdf12014-08-02 15:28:23 -04002436func worker(statusChan chan statusMsg, c chan *testCase, buildDir string, wg *sync.WaitGroup) {
Adam Langley95c29f32014-06-20 12:00:00 -07002437 defer wg.Done()
2438
2439 for test := range c {
Adam Langley69a01602014-11-17 17:26:55 -08002440 var err error
2441
2442 if *mallocTest < 0 {
2443 statusChan <- statusMsg{test: test, started: true}
2444 err = runTest(test, buildDir, -1)
2445 } else {
2446 for mallocNumToFail := int64(*mallocTest); ; mallocNumToFail++ {
2447 statusChan <- statusMsg{test: test, started: true}
2448 if err = runTest(test, buildDir, mallocNumToFail); err != errMoreMallocs {
2449 if err != nil {
2450 fmt.Printf("\n\nmalloc test failed at %d: %s\n", mallocNumToFail, err)
2451 }
2452 break
2453 }
2454 }
2455 }
Adam Langley95c29f32014-06-20 12:00:00 -07002456 statusChan <- statusMsg{test: test, err: err}
2457 }
2458}
2459
2460type statusMsg struct {
2461 test *testCase
2462 started bool
2463 err error
2464}
2465
2466func statusPrinter(doneChan chan struct{}, statusChan chan statusMsg, total int) {
2467 var started, done, failed, lineLen int
2468 defer close(doneChan)
2469
2470 for msg := range statusChan {
2471 if msg.started {
2472 started++
2473 } else {
2474 done++
2475 }
2476
2477 fmt.Printf("\x1b[%dD\x1b[K", lineLen)
2478
2479 if msg.err != nil {
2480 fmt.Printf("FAILED (%s)\n%s\n", msg.test.name, msg.err)
2481 failed++
2482 }
2483 line := fmt.Sprintf("%d/%d/%d/%d", failed, done, started, total)
2484 lineLen = len(line)
2485 os.Stdout.WriteString(line)
2486 }
2487}
2488
2489func main() {
2490 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 -04002491 var flagNumWorkers *int = flag.Int("num-workers", runtime.NumCPU(), "The number of workers to run in parallel.")
David Benjamin884fdf12014-08-02 15:28:23 -04002492 var flagBuildDir *string = flag.String("build-dir", "../../../build", "The build directory to run the shim from.")
Adam Langley95c29f32014-06-20 12:00:00 -07002493
2494 flag.Parse()
2495
2496 addCipherSuiteTests()
2497 addBadECDSASignatureTests()
Adam Langley80842bd2014-06-20 12:00:00 -07002498 addCBCPaddingTests()
Kenny Root7fdeaf12014-08-05 15:23:37 -07002499 addCBCSplittingTests()
David Benjamin636293b2014-07-08 17:59:18 -04002500 addClientAuthTests()
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002501 addVersionNegotiationTests()
David Benjaminaccb4542014-12-12 23:44:33 -05002502 addMinimumVersionTests()
David Benjamin5c24a1d2014-08-31 00:59:27 -04002503 addD5BugTests()
David Benjamine78bfde2014-09-06 12:45:15 -04002504 addExtensionTests()
David Benjamin01fe8202014-09-24 15:21:44 -04002505 addResumptionVersionTests()
Adam Langley75712922014-10-10 16:23:43 -07002506 addExtendedMasterSecretTests()
Adam Langley2ae77d22014-10-28 17:29:33 -07002507 addRenegotiationTests()
David Benjamin5e961c12014-11-07 01:48:35 -05002508 addDTLSReplayTests()
David Benjamin000800a2014-11-14 01:43:59 -05002509 addSigningHashTests()
Feng Lu41aa3252014-11-21 22:47:56 -08002510 addFastRadioPaddingTests()
David Benjamin43ec06f2014-08-05 02:28:57 -04002511 for _, async := range []bool{false, true} {
2512 for _, splitHandshake := range []bool{false, true} {
David Benjamin6fd297b2014-08-11 18:43:38 -04002513 for _, protocol := range []protocol{tls, dtls} {
2514 addStateMachineCoverageTests(async, splitHandshake, protocol)
2515 }
David Benjamin43ec06f2014-08-05 02:28:57 -04002516 }
2517 }
Adam Langley95c29f32014-06-20 12:00:00 -07002518
2519 var wg sync.WaitGroup
2520
David Benjamin2bc8e6f2014-08-02 15:22:37 -04002521 numWorkers := *flagNumWorkers
Adam Langley95c29f32014-06-20 12:00:00 -07002522
2523 statusChan := make(chan statusMsg, numWorkers)
2524 testChan := make(chan *testCase, numWorkers)
2525 doneChan := make(chan struct{})
2526
David Benjamin025b3d32014-07-01 19:53:04 -04002527 go statusPrinter(doneChan, statusChan, len(testCases))
Adam Langley95c29f32014-06-20 12:00:00 -07002528
2529 for i := 0; i < numWorkers; i++ {
2530 wg.Add(1)
David Benjamin884fdf12014-08-02 15:28:23 -04002531 go worker(statusChan, testChan, *flagBuildDir, &wg)
Adam Langley95c29f32014-06-20 12:00:00 -07002532 }
2533
David Benjamin025b3d32014-07-01 19:53:04 -04002534 for i := range testCases {
2535 if len(*flagTest) == 0 || *flagTest == testCases[i].name {
2536 testChan <- &testCases[i]
Adam Langley95c29f32014-06-20 12:00:00 -07002537 }
2538 }
2539
2540 close(testChan)
2541 wg.Wait()
2542 close(statusChan)
2543 <-doneChan
2544
2545 fmt.Printf("\n")
2546}