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