blob: 8d6dabc5e97252409078618faeb99209ce20170b [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001package main
2
3import (
4 "bytes"
David Benjamina08e49d2014-08-24 01:46:07 -04005 "crypto/ecdsa"
6 "crypto/elliptic"
David Benjamin407a10c2014-07-16 12:58:59 -04007 "crypto/x509"
David Benjamin2561dc32014-08-24 01:25:27 -04008 "encoding/base64"
David Benjamina08e49d2014-08-24 01:46:07 -04009 "encoding/pem"
Adam Langley95c29f32014-06-20 12:00:00 -070010 "flag"
11 "fmt"
12 "io"
Kenny Root7fdeaf12014-08-05 15:23:37 -070013 "io/ioutil"
Adam Langley95c29f32014-06-20 12:00:00 -070014 "net"
15 "os"
16 "os/exec"
David Benjamin884fdf12014-08-02 15:28:23 -040017 "path"
David Benjamin2bc8e6f2014-08-02 15:22:37 -040018 "runtime"
Adam Langley69a01602014-11-17 17:26:55 -080019 "strconv"
Adam Langley95c29f32014-06-20 12:00:00 -070020 "strings"
21 "sync"
22 "syscall"
23)
24
Adam Langley69a01602014-11-17 17:26:55 -080025var (
26 useValgrind = flag.Bool("valgrind", false, "If true, run code under valgrind")
27 useGDB = flag.Bool("gdb", false, "If true, run BoringSSL code under gdb")
28 flagDebug *bool = flag.Bool("debug", false, "Hexdump the contents of the connection")
29 mallocTest *int64 = flag.Int64("malloc-test", -1, "If non-negative, run each test with each malloc in turn failing from the given number onwards.")
30 mallocTestDebug *bool = flag.Bool("malloc-test-debug", false, "If true, ask bssl_shim to abort rather than fail a malloc. This can be used with a specific value for --malloc-test to identity the malloc failing that is causing problems.")
31)
Adam Langley95c29f32014-06-20 12:00:00 -070032
David Benjamin025b3d32014-07-01 19:53:04 -040033const (
34 rsaCertificateFile = "cert.pem"
35 ecdsaCertificateFile = "ecdsa_cert.pem"
36)
37
38const (
David Benjamina08e49d2014-08-24 01:46:07 -040039 rsaKeyFile = "key.pem"
40 ecdsaKeyFile = "ecdsa_key.pem"
41 channelIDKeyFile = "channel_id_key.pem"
David Benjamin025b3d32014-07-01 19:53:04 -040042)
43
Adam Langley95c29f32014-06-20 12:00:00 -070044var rsaCertificate, ecdsaCertificate Certificate
David Benjamina08e49d2014-08-24 01:46:07 -040045var channelIDKey *ecdsa.PrivateKey
46var channelIDBytes []byte
Adam Langley95c29f32014-06-20 12:00:00 -070047
David Benjamin61f95272014-11-25 01:55:35 -050048var testOCSPResponse = []byte{1, 2, 3, 4}
49var testSCTList = []byte{5, 6, 7, 8}
50
Adam Langley95c29f32014-06-20 12:00:00 -070051func initCertificates() {
52 var err error
David Benjamin025b3d32014-07-01 19:53:04 -040053 rsaCertificate, err = LoadX509KeyPair(rsaCertificateFile, rsaKeyFile)
Adam Langley95c29f32014-06-20 12:00:00 -070054 if err != nil {
55 panic(err)
56 }
David Benjamin61f95272014-11-25 01:55:35 -050057 rsaCertificate.OCSPStaple = testOCSPResponse
58 rsaCertificate.SignedCertificateTimestampList = testSCTList
Adam Langley95c29f32014-06-20 12:00:00 -070059
David Benjamin025b3d32014-07-01 19:53:04 -040060 ecdsaCertificate, err = LoadX509KeyPair(ecdsaCertificateFile, ecdsaKeyFile)
Adam Langley95c29f32014-06-20 12:00:00 -070061 if err != nil {
62 panic(err)
63 }
David Benjamin61f95272014-11-25 01:55:35 -050064 ecdsaCertificate.OCSPStaple = testOCSPResponse
65 ecdsaCertificate.SignedCertificateTimestampList = testSCTList
David Benjamina08e49d2014-08-24 01:46:07 -040066
67 channelIDPEMBlock, err := ioutil.ReadFile(channelIDKeyFile)
68 if err != nil {
69 panic(err)
70 }
71 channelIDDERBlock, _ := pem.Decode(channelIDPEMBlock)
72 if channelIDDERBlock.Type != "EC PRIVATE KEY" {
73 panic("bad key type")
74 }
75 channelIDKey, err = x509.ParseECPrivateKey(channelIDDERBlock.Bytes)
76 if err != nil {
77 panic(err)
78 }
79 if channelIDKey.Curve != elliptic.P256() {
80 panic("bad curve")
81 }
82
83 channelIDBytes = make([]byte, 64)
84 writeIntPadded(channelIDBytes[:32], channelIDKey.X)
85 writeIntPadded(channelIDBytes[32:], channelIDKey.Y)
Adam Langley95c29f32014-06-20 12:00:00 -070086}
87
88var certificateOnce sync.Once
89
90func getRSACertificate() Certificate {
91 certificateOnce.Do(initCertificates)
92 return rsaCertificate
93}
94
95func getECDSACertificate() Certificate {
96 certificateOnce.Do(initCertificates)
97 return ecdsaCertificate
98}
99
David Benjamin025b3d32014-07-01 19:53:04 -0400100type testType int
101
102const (
103 clientTest testType = iota
104 serverTest
105)
106
David Benjamin6fd297b2014-08-11 18:43:38 -0400107type protocol int
108
109const (
110 tls protocol = iota
111 dtls
112)
113
David Benjaminfc7b0862014-09-06 13:21:53 -0400114const (
115 alpn = 1
116 npn = 2
117)
118
Adam Langley95c29f32014-06-20 12:00:00 -0700119type testCase struct {
David Benjamin025b3d32014-07-01 19:53:04 -0400120 testType testType
David Benjamin6fd297b2014-08-11 18:43:38 -0400121 protocol protocol
Adam Langley95c29f32014-06-20 12:00:00 -0700122 name string
123 config Config
124 shouldFail bool
125 expectedError string
Adam Langleyac61fa32014-06-23 12:03:11 -0700126 // expectedLocalError, if not empty, contains a substring that must be
127 // found in the local error.
128 expectedLocalError string
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400129 // expectedVersion, if non-zero, specifies the TLS version that must be
130 // negotiated.
131 expectedVersion uint16
David Benjamin01fe8202014-09-24 15:21:44 -0400132 // expectedResumeVersion, if non-zero, specifies the TLS version that
133 // must be negotiated on resumption. If zero, expectedVersion is used.
134 expectedResumeVersion uint16
David Benjamina08e49d2014-08-24 01:46:07 -0400135 // expectChannelID controls whether the connection should have
136 // negotiated a Channel ID with channelIDKey.
137 expectChannelID bool
David Benjaminae2888f2014-09-06 12:58:58 -0400138 // expectedNextProto controls whether the connection should
139 // negotiate a next protocol via NPN or ALPN.
140 expectedNextProto string
David Benjaminfc7b0862014-09-06 13:21:53 -0400141 // expectedNextProtoType, if non-zero, is the expected next
142 // protocol negotiation mechanism.
143 expectedNextProtoType int
David Benjaminca6c8262014-11-15 19:06:08 -0500144 // expectedSRTPProtectionProfile is the DTLS-SRTP profile that
145 // should be negotiated. If zero, none should be negotiated.
146 expectedSRTPProtectionProfile uint16
Adam Langley80842bd2014-06-20 12:00:00 -0700147 // messageLen is the length, in bytes, of the test message that will be
148 // sent.
149 messageLen int
David Benjamin025b3d32014-07-01 19:53:04 -0400150 // certFile is the path to the certificate to use for the server.
151 certFile string
152 // keyFile is the path to the private key to use for the server.
153 keyFile string
David Benjamin1d5c83e2014-07-22 19:20:02 -0400154 // resumeSession controls whether a second connection should be tested
David Benjamin01fe8202014-09-24 15:21:44 -0400155 // which attempts to resume the first session.
David Benjamin1d5c83e2014-07-22 19:20:02 -0400156 resumeSession bool
David Benjamin01fe8202014-09-24 15:21:44 -0400157 // resumeConfig, if not nil, points to a Config to be used on
David Benjaminfe8eb9a2014-11-17 03:19:02 -0500158 // resumption. Unless newSessionsOnResume is set,
159 // SessionTicketKey, ServerSessionCache, and
160 // ClientSessionCache are copied from the initial connection's
161 // config. If nil, the initial connection's config is used.
David Benjamin01fe8202014-09-24 15:21:44 -0400162 resumeConfig *Config
David Benjaminfe8eb9a2014-11-17 03:19:02 -0500163 // newSessionsOnResume, if true, will cause resumeConfig to
164 // use a different session resumption context.
165 newSessionsOnResume bool
David Benjamin98e882e2014-08-08 13:24:34 -0400166 // sendPrefix sends a prefix on the socket before actually performing a
167 // handshake.
168 sendPrefix string
David Benjamine58c4f52014-08-24 03:47:07 -0400169 // shimWritesFirst controls whether the shim sends an initial "hello"
170 // message before doing a roundtrip with the runner.
171 shimWritesFirst bool
Adam Langleycf2d4f42014-10-28 19:06:14 -0700172 // renegotiate indicates the the connection should be renegotiated
173 // during the exchange.
174 renegotiate bool
175 // renegotiateCiphers is a list of ciphersuite ids that will be
176 // switched in just before renegotiation.
177 renegotiateCiphers []uint16
David Benjamin5e961c12014-11-07 01:48:35 -0500178 // replayWrites, if true, configures the underlying transport
179 // to replay every write it makes in DTLS tests.
180 replayWrites bool
David Benjamin325b5c32014-07-01 19:40:31 -0400181 // flags, if not empty, contains a list of command-line flags that will
182 // be passed to the shim program.
183 flags []string
Adam Langley95c29f32014-06-20 12:00:00 -0700184}
185
David Benjamin025b3d32014-07-01 19:53:04 -0400186var testCases = []testCase{
Adam Langley95c29f32014-06-20 12:00:00 -0700187 {
188 name: "BadRSASignature",
189 config: Config{
190 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
191 Bugs: ProtocolBugs{
192 InvalidSKXSignature: true,
193 },
194 },
195 shouldFail: true,
196 expectedError: ":BAD_SIGNATURE:",
197 },
198 {
199 name: "BadECDSASignature",
200 config: Config{
201 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
202 Bugs: ProtocolBugs{
203 InvalidSKXSignature: true,
204 },
205 Certificates: []Certificate{getECDSACertificate()},
206 },
207 shouldFail: true,
208 expectedError: ":BAD_SIGNATURE:",
209 },
210 {
211 name: "BadECDSACurve",
212 config: Config{
213 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
214 Bugs: ProtocolBugs{
215 InvalidSKXCurve: true,
216 },
217 Certificates: []Certificate{getECDSACertificate()},
218 },
219 shouldFail: true,
220 expectedError: ":WRONG_CURVE:",
221 },
Adam Langleyac61fa32014-06-23 12:03:11 -0700222 {
David Benjamina8e3e0e2014-08-06 22:11:10 -0400223 testType: serverTest,
224 name: "BadRSAVersion",
225 config: Config{
226 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
227 Bugs: ProtocolBugs{
228 RsaClientKeyExchangeVersion: VersionTLS11,
229 },
230 },
231 shouldFail: true,
232 expectedError: ":DECRYPTION_FAILED_OR_BAD_RECORD_MAC:",
233 },
234 {
David Benjamin325b5c32014-07-01 19:40:31 -0400235 name: "NoFallbackSCSV",
Adam Langleyac61fa32014-06-23 12:03:11 -0700236 config: Config{
237 Bugs: ProtocolBugs{
238 FailIfNotFallbackSCSV: true,
239 },
240 },
241 shouldFail: true,
242 expectedLocalError: "no fallback SCSV found",
243 },
David Benjamin325b5c32014-07-01 19:40:31 -0400244 {
David Benjamin2a0c4962014-08-22 23:46:35 -0400245 name: "SendFallbackSCSV",
David Benjamin325b5c32014-07-01 19:40:31 -0400246 config: Config{
247 Bugs: ProtocolBugs{
248 FailIfNotFallbackSCSV: true,
249 },
250 },
251 flags: []string{"-fallback-scsv"},
252 },
David Benjamin197b3ab2014-07-02 18:37:33 -0400253 {
David Benjamin7b030512014-07-08 17:30:11 -0400254 name: "ClientCertificateTypes",
255 config: Config{
256 ClientAuth: RequestClientCert,
257 ClientCertificateTypes: []byte{
258 CertTypeDSSSign,
259 CertTypeRSASign,
260 CertTypeECDSASign,
261 },
262 },
David Benjamin2561dc32014-08-24 01:25:27 -0400263 flags: []string{
264 "-expect-certificate-types",
265 base64.StdEncoding.EncodeToString([]byte{
266 CertTypeDSSSign,
267 CertTypeRSASign,
268 CertTypeECDSASign,
269 }),
270 },
David Benjamin7b030512014-07-08 17:30:11 -0400271 },
David Benjamin636293b2014-07-08 17:59:18 -0400272 {
273 name: "NoClientCertificate",
274 config: Config{
275 ClientAuth: RequireAnyClientCert,
276 },
277 shouldFail: true,
278 expectedLocalError: "client didn't provide a certificate",
279 },
David Benjamin1c375dd2014-07-12 00:48:23 -0400280 {
281 name: "UnauthenticatedECDH",
282 config: Config{
283 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
284 Bugs: ProtocolBugs{
285 UnauthenticatedECDH: true,
286 },
287 },
288 shouldFail: true,
David Benjamine8f3d662014-07-12 01:10:19 -0400289 expectedError: ":UNEXPECTED_MESSAGE:",
David Benjamin1c375dd2014-07-12 00:48:23 -0400290 },
David Benjamin9c651c92014-07-12 13:27:45 -0400291 {
292 name: "SkipServerKeyExchange",
293 config: Config{
294 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
295 Bugs: ProtocolBugs{
296 SkipServerKeyExchange: true,
297 },
298 },
299 shouldFail: true,
300 expectedError: ":UNEXPECTED_MESSAGE:",
301 },
David Benjamin1f5f62b2014-07-12 16:18:02 -0400302 {
David Benjamina0e52232014-07-19 17:39:58 -0400303 name: "SkipChangeCipherSpec-Client",
304 config: Config{
305 Bugs: ProtocolBugs{
306 SkipChangeCipherSpec: true,
307 },
308 },
309 shouldFail: true,
David Benjamin86271ee2014-07-21 16:14:03 -0400310 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
David Benjamina0e52232014-07-19 17:39:58 -0400311 },
312 {
313 testType: serverTest,
314 name: "SkipChangeCipherSpec-Server",
315 config: Config{
316 Bugs: ProtocolBugs{
317 SkipChangeCipherSpec: true,
318 },
319 },
320 shouldFail: true,
David Benjamin86271ee2014-07-21 16:14:03 -0400321 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
David Benjamina0e52232014-07-19 17:39:58 -0400322 },
David Benjamin42be6452014-07-21 14:50:23 -0400323 {
324 testType: serverTest,
325 name: "SkipChangeCipherSpec-Server-NPN",
326 config: Config{
327 NextProtos: []string{"bar"},
328 Bugs: ProtocolBugs{
329 SkipChangeCipherSpec: true,
330 },
331 },
332 flags: []string{
333 "-advertise-npn", "\x03foo\x03bar\x03baz",
334 },
335 shouldFail: true,
David Benjamin86271ee2014-07-21 16:14:03 -0400336 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
337 },
338 {
339 name: "FragmentAcrossChangeCipherSpec-Client",
340 config: Config{
341 Bugs: ProtocolBugs{
342 FragmentAcrossChangeCipherSpec: true,
343 },
344 },
345 shouldFail: true,
346 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
347 },
348 {
349 testType: serverTest,
350 name: "FragmentAcrossChangeCipherSpec-Server",
351 config: Config{
352 Bugs: ProtocolBugs{
353 FragmentAcrossChangeCipherSpec: true,
354 },
355 },
356 shouldFail: true,
357 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
358 },
359 {
360 testType: serverTest,
361 name: "FragmentAcrossChangeCipherSpec-Server-NPN",
362 config: Config{
363 NextProtos: []string{"bar"},
364 Bugs: ProtocolBugs{
365 FragmentAcrossChangeCipherSpec: true,
366 },
367 },
368 flags: []string{
369 "-advertise-npn", "\x03foo\x03bar\x03baz",
370 },
371 shouldFail: true,
372 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
David Benjamin42be6452014-07-21 14:50:23 -0400373 },
David Benjaminf3ec83d2014-07-21 22:42:34 -0400374 {
375 testType: serverTest,
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400376 name: "FragmentAlert",
377 config: Config{
378 Bugs: ProtocolBugs{
David Benjaminca6c8262014-11-15 19:06:08 -0500379 FragmentAlert: true,
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400380 SendSpuriousAlert: true,
381 },
382 },
383 shouldFail: true,
384 expectedError: ":BAD_ALERT:",
385 },
386 {
387 testType: serverTest,
David Benjaminf3ec83d2014-07-21 22:42:34 -0400388 name: "EarlyChangeCipherSpec-server-1",
389 config: Config{
390 Bugs: ProtocolBugs{
391 EarlyChangeCipherSpec: 1,
392 },
393 },
394 shouldFail: true,
395 expectedError: ":CCS_RECEIVED_EARLY:",
396 },
397 {
398 testType: serverTest,
399 name: "EarlyChangeCipherSpec-server-2",
400 config: Config{
401 Bugs: ProtocolBugs{
402 EarlyChangeCipherSpec: 2,
403 },
404 },
405 shouldFail: true,
406 expectedError: ":CCS_RECEIVED_EARLY:",
407 },
David Benjamind23f4122014-07-23 15:09:48 -0400408 {
David Benjamind23f4122014-07-23 15:09:48 -0400409 name: "SkipNewSessionTicket",
410 config: Config{
411 Bugs: ProtocolBugs{
412 SkipNewSessionTicket: true,
413 },
414 },
415 shouldFail: true,
416 expectedError: ":CCS_RECEIVED_EARLY:",
417 },
David Benjamin7e3305e2014-07-28 14:52:32 -0400418 {
David Benjamind86c7672014-08-02 04:07:12 -0400419 testType: serverTest,
David Benjaminbef270a2014-08-02 04:22:02 -0400420 name: "FallbackSCSV",
421 config: Config{
422 MaxVersion: VersionTLS11,
423 Bugs: ProtocolBugs{
424 SendFallbackSCSV: true,
425 },
426 },
427 shouldFail: true,
428 expectedError: ":INAPPROPRIATE_FALLBACK:",
429 },
430 {
431 testType: serverTest,
432 name: "FallbackSCSV-VersionMatch",
433 config: Config{
434 Bugs: ProtocolBugs{
435 SendFallbackSCSV: true,
436 },
437 },
438 },
David Benjamin98214542014-08-07 18:02:39 -0400439 {
440 testType: serverTest,
441 name: "FragmentedClientVersion",
442 config: Config{
443 Bugs: ProtocolBugs{
444 MaxHandshakeRecordLength: 1,
445 FragmentClientVersion: true,
446 },
447 },
David Benjamin82c9e902014-12-12 15:55:27 -0500448 expectedVersion: VersionTLS12,
David Benjamin98214542014-08-07 18:02:39 -0400449 },
David Benjamin98e882e2014-08-08 13:24:34 -0400450 {
451 testType: serverTest,
452 name: "MinorVersionTolerance",
453 config: Config{
454 Bugs: ProtocolBugs{
455 SendClientVersion: 0x03ff,
456 },
457 },
458 expectedVersion: VersionTLS12,
459 },
460 {
461 testType: serverTest,
462 name: "MajorVersionTolerance",
463 config: Config{
464 Bugs: ProtocolBugs{
465 SendClientVersion: 0x0400,
466 },
467 },
468 expectedVersion: VersionTLS12,
469 },
470 {
471 testType: serverTest,
472 name: "VersionTooLow",
473 config: Config{
474 Bugs: ProtocolBugs{
475 SendClientVersion: 0x0200,
476 },
477 },
478 shouldFail: true,
479 expectedError: ":UNSUPPORTED_PROTOCOL:",
480 },
481 {
482 testType: serverTest,
483 name: "HttpGET",
484 sendPrefix: "GET / HTTP/1.0\n",
485 shouldFail: true,
486 expectedError: ":HTTP_REQUEST:",
487 },
488 {
489 testType: serverTest,
490 name: "HttpPOST",
491 sendPrefix: "POST / HTTP/1.0\n",
492 shouldFail: true,
493 expectedError: ":HTTP_REQUEST:",
494 },
495 {
496 testType: serverTest,
497 name: "HttpHEAD",
498 sendPrefix: "HEAD / HTTP/1.0\n",
499 shouldFail: true,
500 expectedError: ":HTTP_REQUEST:",
501 },
502 {
503 testType: serverTest,
504 name: "HttpPUT",
505 sendPrefix: "PUT / HTTP/1.0\n",
506 shouldFail: true,
507 expectedError: ":HTTP_REQUEST:",
508 },
509 {
510 testType: serverTest,
511 name: "HttpCONNECT",
512 sendPrefix: "CONNECT www.google.com:443 HTTP/1.0\n",
513 shouldFail: true,
514 expectedError: ":HTTPS_PROXY_REQUEST:",
515 },
David Benjamin39ebf532014-08-31 02:23:49 -0400516 {
David Benjaminf080ecd2014-12-11 18:48:59 -0500517 testType: serverTest,
518 name: "Garbage",
519 sendPrefix: "blah",
520 shouldFail: true,
521 expectedError: ":UNKNOWN_PROTOCOL:",
522 },
523 {
David Benjamin39ebf532014-08-31 02:23:49 -0400524 name: "SkipCipherVersionCheck",
525 config: Config{
526 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
527 MaxVersion: VersionTLS11,
528 Bugs: ProtocolBugs{
529 SkipCipherVersionCheck: true,
530 },
531 },
532 shouldFail: true,
533 expectedError: ":WRONG_CIPHER_RETURNED:",
534 },
David Benjamin9114fae2014-11-08 11:41:14 -0500535 {
536 name: "RSAServerKeyExchange",
537 config: Config{
538 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
539 Bugs: ProtocolBugs{
540 RSAServerKeyExchange: true,
541 },
542 },
543 shouldFail: true,
544 expectedError: ":UNEXPECTED_MESSAGE:",
545 },
David Benjamin128dbc32014-12-01 01:27:42 -0500546 {
547 name: "DisableEverything",
548 flags: []string{"-no-tls12", "-no-tls11", "-no-tls1", "-no-ssl3"},
549 shouldFail: true,
550 expectedError: ":WRONG_SSL_VERSION:",
551 },
552 {
553 protocol: dtls,
554 name: "DisableEverything-DTLS",
555 flags: []string{"-no-tls12", "-no-tls1"},
556 shouldFail: true,
557 expectedError: ":WRONG_SSL_VERSION:",
558 },
Adam Langley95c29f32014-06-20 12:00:00 -0700559}
560
David Benjamin01fe8202014-09-24 15:21:44 -0400561func doExchange(test *testCase, config *Config, conn net.Conn, messageLen int, isResume bool) error {
David Benjamin65ea8ff2014-11-23 03:01:00 -0500562 var connDebug *recordingConn
563 if *flagDebug {
564 connDebug = &recordingConn{Conn: conn}
565 conn = connDebug
566 defer func() {
567 connDebug.WriteTo(os.Stdout)
568 }()
569 }
570
David Benjamin6fd297b2014-08-11 18:43:38 -0400571 if test.protocol == dtls {
572 conn = newPacketAdaptor(conn)
David Benjamin5e961c12014-11-07 01:48:35 -0500573 if test.replayWrites {
574 conn = newReplayAdaptor(conn)
575 }
David Benjamin6fd297b2014-08-11 18:43:38 -0400576 }
577
578 if test.sendPrefix != "" {
579 if _, err := conn.Write([]byte(test.sendPrefix)); err != nil {
580 return err
581 }
David Benjamin98e882e2014-08-08 13:24:34 -0400582 }
583
David Benjamin1d5c83e2014-07-22 19:20:02 -0400584 var tlsConn *Conn
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400585 if test.testType == clientTest {
David Benjamin6fd297b2014-08-11 18:43:38 -0400586 if test.protocol == dtls {
587 tlsConn = DTLSServer(conn, config)
588 } else {
589 tlsConn = Server(conn, config)
590 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400591 } else {
592 config.InsecureSkipVerify = true
David Benjamin6fd297b2014-08-11 18:43:38 -0400593 if test.protocol == dtls {
594 tlsConn = DTLSClient(conn, config)
595 } else {
596 tlsConn = Client(conn, config)
597 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400598 }
599
Adam Langley95c29f32014-06-20 12:00:00 -0700600 if err := tlsConn.Handshake(); err != nil {
601 return err
602 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700603
David Benjamin01fe8202014-09-24 15:21:44 -0400604 // TODO(davidben): move all per-connection expectations into a dedicated
605 // expectations struct that can be specified separately for the two
606 // legs.
607 expectedVersion := test.expectedVersion
608 if isResume && test.expectedResumeVersion != 0 {
609 expectedVersion = test.expectedResumeVersion
610 }
611 if vers := tlsConn.ConnectionState().Version; expectedVersion != 0 && vers != expectedVersion {
612 return fmt.Errorf("got version %x, expected %x", vers, expectedVersion)
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400613 }
614
David Benjamina08e49d2014-08-24 01:46:07 -0400615 if test.expectChannelID {
616 channelID := tlsConn.ConnectionState().ChannelID
617 if channelID == nil {
618 return fmt.Errorf("no channel ID negotiated")
619 }
620 if channelID.Curve != channelIDKey.Curve ||
621 channelIDKey.X.Cmp(channelIDKey.X) != 0 ||
622 channelIDKey.Y.Cmp(channelIDKey.Y) != 0 {
623 return fmt.Errorf("incorrect channel ID")
624 }
625 }
626
David Benjaminae2888f2014-09-06 12:58:58 -0400627 if expected := test.expectedNextProto; expected != "" {
628 if actual := tlsConn.ConnectionState().NegotiatedProtocol; actual != expected {
629 return fmt.Errorf("next proto mismatch: got %s, wanted %s", actual, expected)
630 }
631 }
632
David Benjaminfc7b0862014-09-06 13:21:53 -0400633 if test.expectedNextProtoType != 0 {
634 if (test.expectedNextProtoType == alpn) != tlsConn.ConnectionState().NegotiatedProtocolFromALPN {
635 return fmt.Errorf("next proto type mismatch")
636 }
637 }
638
David Benjaminca6c8262014-11-15 19:06:08 -0500639 if p := tlsConn.ConnectionState().SRTPProtectionProfile; p != test.expectedSRTPProtectionProfile {
640 return fmt.Errorf("SRTP profile mismatch: got %d, wanted %d", p, test.expectedSRTPProtectionProfile)
641 }
642
David Benjamine58c4f52014-08-24 03:47:07 -0400643 if test.shimWritesFirst {
644 var buf [5]byte
645 _, err := io.ReadFull(tlsConn, buf[:])
646 if err != nil {
647 return err
648 }
649 if string(buf[:]) != "hello" {
650 return fmt.Errorf("bad initial message")
651 }
652 }
653
Adam Langleycf2d4f42014-10-28 19:06:14 -0700654 if test.renegotiate {
655 if test.renegotiateCiphers != nil {
656 config.CipherSuites = test.renegotiateCiphers
657 }
658 if err := tlsConn.Renegotiate(); err != nil {
659 return err
660 }
661 } else if test.renegotiateCiphers != nil {
662 panic("renegotiateCiphers without renegotiate")
663 }
664
Kenny Root7fdeaf12014-08-05 15:23:37 -0700665 if messageLen < 0 {
David Benjamin6fd297b2014-08-11 18:43:38 -0400666 if test.protocol == dtls {
667 return fmt.Errorf("messageLen < 0 not supported for DTLS tests")
668 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700669 // Read until EOF.
670 _, err := io.Copy(ioutil.Discard, tlsConn)
671 return err
672 }
673
Adam Langley80842bd2014-06-20 12:00:00 -0700674 if messageLen == 0 {
675 messageLen = 32
676 }
677 testMessage := make([]byte, messageLen)
678 for i := range testMessage {
679 testMessage[i] = 0x42
680 }
Adam Langley95c29f32014-06-20 12:00:00 -0700681 tlsConn.Write(testMessage)
682
683 buf := make([]byte, len(testMessage))
David Benjamin6fd297b2014-08-11 18:43:38 -0400684 if test.protocol == dtls {
685 bufTmp := make([]byte, len(buf)+1)
686 n, err := tlsConn.Read(bufTmp)
687 if err != nil {
688 return err
689 }
690 if n != len(buf) {
691 return fmt.Errorf("bad reply; length mismatch (%d vs %d)", n, len(buf))
692 }
693 copy(buf, bufTmp)
694 } else {
695 _, err := io.ReadFull(tlsConn, buf)
696 if err != nil {
697 return err
698 }
Adam Langley95c29f32014-06-20 12:00:00 -0700699 }
700
701 for i, v := range buf {
702 if v != testMessage[i]^0xff {
703 return fmt.Errorf("bad reply contents at byte %d", i)
704 }
705 }
706
707 return nil
708}
709
David Benjamin325b5c32014-07-01 19:40:31 -0400710func valgrindOf(dbAttach bool, path string, args ...string) *exec.Cmd {
711 valgrindArgs := []string{"--error-exitcode=99", "--track-origins=yes", "--leak-check=full"}
Adam Langley95c29f32014-06-20 12:00:00 -0700712 if dbAttach {
David Benjamin325b5c32014-07-01 19:40:31 -0400713 valgrindArgs = append(valgrindArgs, "--db-attach=yes", "--db-command=xterm -e gdb -nw %f %p")
Adam Langley95c29f32014-06-20 12:00:00 -0700714 }
David Benjamin325b5c32014-07-01 19:40:31 -0400715 valgrindArgs = append(valgrindArgs, path)
716 valgrindArgs = append(valgrindArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -0700717
David Benjamin325b5c32014-07-01 19:40:31 -0400718 return exec.Command("valgrind", valgrindArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -0700719}
720
David Benjamin325b5c32014-07-01 19:40:31 -0400721func gdbOf(path string, args ...string) *exec.Cmd {
722 xtermArgs := []string{"-e", "gdb", "--args"}
723 xtermArgs = append(xtermArgs, path)
724 xtermArgs = append(xtermArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -0700725
David Benjamin325b5c32014-07-01 19:40:31 -0400726 return exec.Command("xterm", xtermArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -0700727}
728
David Benjamin1d5c83e2014-07-22 19:20:02 -0400729func openSocketPair() (shimEnd *os.File, conn net.Conn) {
Adam Langley95c29f32014-06-20 12:00:00 -0700730 socks, err := syscall.Socketpair(syscall.AF_UNIX, syscall.SOCK_STREAM, 0)
731 if err != nil {
732 panic(err)
733 }
734
735 syscall.CloseOnExec(socks[0])
736 syscall.CloseOnExec(socks[1])
David Benjamin1d5c83e2014-07-22 19:20:02 -0400737 shimEnd = os.NewFile(uintptr(socks[0]), "shim end")
Adam Langley95c29f32014-06-20 12:00:00 -0700738 connFile := os.NewFile(uintptr(socks[1]), "our end")
David Benjamin1d5c83e2014-07-22 19:20:02 -0400739 conn, err = net.FileConn(connFile)
740 if err != nil {
741 panic(err)
742 }
Adam Langley95c29f32014-06-20 12:00:00 -0700743 connFile.Close()
744 if err != nil {
745 panic(err)
746 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400747 return shimEnd, conn
748}
749
Adam Langley69a01602014-11-17 17:26:55 -0800750type moreMallocsError struct{}
751
752func (moreMallocsError) Error() string {
753 return "child process did not exhaust all allocation calls"
754}
755
756var errMoreMallocs = moreMallocsError{}
757
758func runTest(test *testCase, buildDir string, mallocNumToFail int64) error {
Adam Langley38311732014-10-16 19:04:35 -0700759 if !test.shouldFail && (len(test.expectedError) > 0 || len(test.expectedLocalError) > 0) {
760 panic("Error expected without shouldFail in " + test.name)
761 }
762
David Benjamin1d5c83e2014-07-22 19:20:02 -0400763 shimEnd, conn := openSocketPair()
764 shimEndResume, connResume := openSocketPair()
Adam Langley95c29f32014-06-20 12:00:00 -0700765
David Benjamin884fdf12014-08-02 15:28:23 -0400766 shim_path := path.Join(buildDir, "ssl/test/bssl_shim")
David Benjamin5a593af2014-08-11 19:51:50 -0400767 var flags []string
David Benjamin1d5c83e2014-07-22 19:20:02 -0400768 if test.testType == serverTest {
David Benjamin5a593af2014-08-11 19:51:50 -0400769 flags = append(flags, "-server")
770
David Benjamin025b3d32014-07-01 19:53:04 -0400771 flags = append(flags, "-key-file")
772 if test.keyFile == "" {
773 flags = append(flags, rsaKeyFile)
774 } else {
775 flags = append(flags, test.keyFile)
776 }
777
778 flags = append(flags, "-cert-file")
779 if test.certFile == "" {
780 flags = append(flags, rsaCertificateFile)
781 } else {
782 flags = append(flags, test.certFile)
783 }
784 }
David Benjamin5a593af2014-08-11 19:51:50 -0400785
David Benjamin6fd297b2014-08-11 18:43:38 -0400786 if test.protocol == dtls {
787 flags = append(flags, "-dtls")
788 }
789
David Benjamin5a593af2014-08-11 19:51:50 -0400790 if test.resumeSession {
791 flags = append(flags, "-resume")
792 }
793
David Benjamine58c4f52014-08-24 03:47:07 -0400794 if test.shimWritesFirst {
795 flags = append(flags, "-shim-writes-first")
796 }
797
David Benjamin025b3d32014-07-01 19:53:04 -0400798 flags = append(flags, test.flags...)
799
800 var shim *exec.Cmd
801 if *useValgrind {
802 shim = valgrindOf(false, shim_path, flags...)
Adam Langley75712922014-10-10 16:23:43 -0700803 } else if *useGDB {
804 shim = gdbOf(shim_path, flags...)
David Benjamin025b3d32014-07-01 19:53:04 -0400805 } else {
806 shim = exec.Command(shim_path, flags...)
807 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400808 shim.ExtraFiles = []*os.File{shimEnd, shimEndResume}
David Benjamin025b3d32014-07-01 19:53:04 -0400809 shim.Stdin = os.Stdin
810 var stdoutBuf, stderrBuf bytes.Buffer
811 shim.Stdout = &stdoutBuf
812 shim.Stderr = &stderrBuf
Adam Langley69a01602014-11-17 17:26:55 -0800813 if mallocNumToFail >= 0 {
814 shim.Env = []string{"MALLOC_NUMBER_TO_FAIL=" + strconv.FormatInt(mallocNumToFail, 10)}
815 if *mallocTestDebug {
816 shim.Env = append(shim.Env, "MALLOC_ABORT_ON_FAIL=1")
817 }
818 shim.Env = append(shim.Env, "_MALLOC_CHECK=1")
819 }
David Benjamin025b3d32014-07-01 19:53:04 -0400820
821 if err := shim.Start(); err != nil {
Adam Langley95c29f32014-06-20 12:00:00 -0700822 panic(err)
823 }
David Benjamin025b3d32014-07-01 19:53:04 -0400824 shimEnd.Close()
David Benjamin1d5c83e2014-07-22 19:20:02 -0400825 shimEndResume.Close()
Adam Langley95c29f32014-06-20 12:00:00 -0700826
827 config := test.config
David Benjamin1d5c83e2014-07-22 19:20:02 -0400828 config.ClientSessionCache = NewLRUClientSessionCache(1)
David Benjaminfe8eb9a2014-11-17 03:19:02 -0500829 config.ServerSessionCache = NewLRUServerSessionCache(1)
David Benjamin025b3d32014-07-01 19:53:04 -0400830 if test.testType == clientTest {
831 if len(config.Certificates) == 0 {
832 config.Certificates = []Certificate{getRSACertificate()}
833 }
David Benjamin025b3d32014-07-01 19:53:04 -0400834 }
Adam Langley95c29f32014-06-20 12:00:00 -0700835
David Benjamin01fe8202014-09-24 15:21:44 -0400836 err := doExchange(test, &config, conn, test.messageLen,
837 false /* not a resumption */)
Adam Langley95c29f32014-06-20 12:00:00 -0700838 conn.Close()
David Benjamin65ea8ff2014-11-23 03:01:00 -0500839
David Benjamin1d5c83e2014-07-22 19:20:02 -0400840 if err == nil && test.resumeSession {
David Benjamin01fe8202014-09-24 15:21:44 -0400841 var resumeConfig Config
842 if test.resumeConfig != nil {
843 resumeConfig = *test.resumeConfig
844 if len(resumeConfig.Certificates) == 0 {
845 resumeConfig.Certificates = []Certificate{getRSACertificate()}
846 }
David Benjaminfe8eb9a2014-11-17 03:19:02 -0500847 if !test.newSessionsOnResume {
848 resumeConfig.SessionTicketKey = config.SessionTicketKey
849 resumeConfig.ClientSessionCache = config.ClientSessionCache
850 resumeConfig.ServerSessionCache = config.ServerSessionCache
851 }
David Benjamin01fe8202014-09-24 15:21:44 -0400852 } else {
853 resumeConfig = config
854 }
855 err = doExchange(test, &resumeConfig, connResume, test.messageLen,
856 true /* resumption */)
David Benjamin1d5c83e2014-07-22 19:20:02 -0400857 }
David Benjamin812152a2014-09-06 12:49:07 -0400858 connResume.Close()
David Benjamin1d5c83e2014-07-22 19:20:02 -0400859
David Benjamin025b3d32014-07-01 19:53:04 -0400860 childErr := shim.Wait()
Adam Langley69a01602014-11-17 17:26:55 -0800861 if exitError, ok := childErr.(*exec.ExitError); ok {
862 if exitError.Sys().(syscall.WaitStatus).ExitStatus() == 88 {
863 return errMoreMallocs
864 }
865 }
Adam Langley95c29f32014-06-20 12:00:00 -0700866
867 stdout := string(stdoutBuf.Bytes())
868 stderr := string(stderrBuf.Bytes())
869 failed := err != nil || childErr != nil
870 correctFailure := len(test.expectedError) == 0 || strings.Contains(stdout, test.expectedError)
Adam Langleyac61fa32014-06-23 12:03:11 -0700871 localError := "none"
872 if err != nil {
873 localError = err.Error()
874 }
875 if len(test.expectedLocalError) != 0 {
876 correctFailure = correctFailure && strings.Contains(localError, test.expectedLocalError)
877 }
Adam Langley95c29f32014-06-20 12:00:00 -0700878
879 if failed != test.shouldFail || failed && !correctFailure {
Adam Langley95c29f32014-06-20 12:00:00 -0700880 childError := "none"
Adam Langley95c29f32014-06-20 12:00:00 -0700881 if childErr != nil {
882 childError = childErr.Error()
883 }
884
885 var msg string
886 switch {
887 case failed && !test.shouldFail:
888 msg = "unexpected failure"
889 case !failed && test.shouldFail:
890 msg = "unexpected success"
891 case failed && !correctFailure:
Adam Langleyac61fa32014-06-23 12:03:11 -0700892 msg = "bad error (wanted '" + test.expectedError + "' / '" + test.expectedLocalError + "')"
Adam Langley95c29f32014-06-20 12:00:00 -0700893 default:
894 panic("internal error")
895 }
896
897 return fmt.Errorf("%s: local error '%s', child error '%s', stdout:\n%s\nstderr:\n%s", msg, localError, childError, string(stdoutBuf.Bytes()), stderr)
898 }
899
900 if !*useValgrind && len(stderr) > 0 {
901 println(stderr)
902 }
903
904 return nil
905}
906
907var tlsVersions = []struct {
908 name string
909 version uint16
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400910 flag string
David Benjamin8b8c0062014-11-23 02:47:52 -0500911 hasDTLS bool
Adam Langley95c29f32014-06-20 12:00:00 -0700912}{
David Benjamin8b8c0062014-11-23 02:47:52 -0500913 {"SSL3", VersionSSL30, "-no-ssl3", false},
914 {"TLS1", VersionTLS10, "-no-tls1", true},
915 {"TLS11", VersionTLS11, "-no-tls11", false},
916 {"TLS12", VersionTLS12, "-no-tls12", true},
Adam Langley95c29f32014-06-20 12:00:00 -0700917}
918
919var testCipherSuites = []struct {
920 name string
921 id uint16
922}{
923 {"3DES-SHA", TLS_RSA_WITH_3DES_EDE_CBC_SHA},
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400924 {"AES128-GCM", TLS_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -0700925 {"AES128-SHA", TLS_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -0400926 {"AES128-SHA256", TLS_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400927 {"AES256-GCM", TLS_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -0700928 {"AES256-SHA", TLS_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -0400929 {"AES256-SHA256", TLS_RSA_WITH_AES_256_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400930 {"DHE-RSA-AES128-GCM", TLS_DHE_RSA_WITH_AES_128_GCM_SHA256},
931 {"DHE-RSA-AES128-SHA", TLS_DHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -0400932 {"DHE-RSA-AES128-SHA256", TLS_DHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400933 {"DHE-RSA-AES256-GCM", TLS_DHE_RSA_WITH_AES_256_GCM_SHA384},
934 {"DHE-RSA-AES256-SHA", TLS_DHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -0400935 {"DHE-RSA-AES256-SHA256", TLS_DHE_RSA_WITH_AES_256_CBC_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -0700936 {"ECDHE-ECDSA-AES128-GCM", TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
937 {"ECDHE-ECDSA-AES128-SHA", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -0400938 {"ECDHE-ECDSA-AES128-SHA256", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256},
939 {"ECDHE-ECDSA-AES256-GCM", TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -0700940 {"ECDHE-ECDSA-AES256-SHA", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -0400941 {"ECDHE-ECDSA-AES256-SHA384", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -0700942 {"ECDHE-ECDSA-RC4-SHA", TLS_ECDHE_ECDSA_WITH_RC4_128_SHA},
David Benjamin2af684f2014-10-27 02:23:15 -0400943 {"ECDHE-PSK-WITH-AES-128-GCM-SHA256", TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -0700944 {"ECDHE-RSA-AES128-GCM", TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -0700945 {"ECDHE-RSA-AES128-SHA", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -0400946 {"ECDHE-RSA-AES128-SHA256", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400947 {"ECDHE-RSA-AES256-GCM", TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -0700948 {"ECDHE-RSA-AES256-SHA", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -0400949 {"ECDHE-RSA-AES256-SHA384", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -0700950 {"ECDHE-RSA-RC4-SHA", TLS_ECDHE_RSA_WITH_RC4_128_SHA},
David Benjamin48cae082014-10-27 01:06:24 -0400951 {"PSK-AES128-CBC-SHA", TLS_PSK_WITH_AES_128_CBC_SHA},
952 {"PSK-AES256-CBC-SHA", TLS_PSK_WITH_AES_256_CBC_SHA},
953 {"PSK-RC4-SHA", TLS_PSK_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -0700954 {"RC4-MD5", TLS_RSA_WITH_RC4_128_MD5},
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400955 {"RC4-SHA", TLS_RSA_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -0700956}
957
David Benjamin8b8c0062014-11-23 02:47:52 -0500958func hasComponent(suiteName, component string) bool {
959 return strings.Contains("-"+suiteName+"-", "-"+component+"-")
960}
961
David Benjaminf7768e42014-08-31 02:06:47 -0400962func isTLS12Only(suiteName string) bool {
David Benjamin8b8c0062014-11-23 02:47:52 -0500963 return hasComponent(suiteName, "GCM") ||
964 hasComponent(suiteName, "SHA256") ||
965 hasComponent(suiteName, "SHA384")
966}
967
968func isDTLSCipher(suiteName string) bool {
969 // TODO(davidben): AES-GCM exists in DTLS 1.2 but is currently
970 // broken because DTLS is not EVP_AEAD-aware.
971 return !hasComponent(suiteName, "RC4") &&
972 !hasComponent(suiteName, "GCM")
David Benjaminf7768e42014-08-31 02:06:47 -0400973}
974
Adam Langley95c29f32014-06-20 12:00:00 -0700975func addCipherSuiteTests() {
976 for _, suite := range testCipherSuites {
David Benjamin48cae082014-10-27 01:06:24 -0400977 const psk = "12345"
978 const pskIdentity = "luggage combo"
979
Adam Langley95c29f32014-06-20 12:00:00 -0700980 var cert Certificate
David Benjamin025b3d32014-07-01 19:53:04 -0400981 var certFile string
982 var keyFile string
David Benjamin8b8c0062014-11-23 02:47:52 -0500983 if hasComponent(suite.name, "ECDSA") {
Adam Langley95c29f32014-06-20 12:00:00 -0700984 cert = getECDSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -0400985 certFile = ecdsaCertificateFile
986 keyFile = ecdsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -0700987 } else {
988 cert = getRSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -0400989 certFile = rsaCertificateFile
990 keyFile = rsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -0700991 }
992
David Benjamin48cae082014-10-27 01:06:24 -0400993 var flags []string
David Benjamin8b8c0062014-11-23 02:47:52 -0500994 if hasComponent(suite.name, "PSK") {
David Benjamin48cae082014-10-27 01:06:24 -0400995 flags = append(flags,
996 "-psk", psk,
997 "-psk-identity", pskIdentity)
998 }
999
Adam Langley95c29f32014-06-20 12:00:00 -07001000 for _, ver := range tlsVersions {
David Benjaminf7768e42014-08-31 02:06:47 -04001001 if ver.version < VersionTLS12 && isTLS12Only(suite.name) {
Adam Langley95c29f32014-06-20 12:00:00 -07001002 continue
1003 }
1004
David Benjamin025b3d32014-07-01 19:53:04 -04001005 testCases = append(testCases, testCase{
1006 testType: clientTest,
1007 name: ver.name + "-" + suite.name + "-client",
Adam Langley95c29f32014-06-20 12:00:00 -07001008 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001009 MinVersion: ver.version,
1010 MaxVersion: ver.version,
1011 CipherSuites: []uint16{suite.id},
1012 Certificates: []Certificate{cert},
1013 PreSharedKey: []byte(psk),
1014 PreSharedKeyIdentity: pskIdentity,
Adam Langley95c29f32014-06-20 12:00:00 -07001015 },
David Benjamin48cae082014-10-27 01:06:24 -04001016 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001017 resumeSession: true,
Adam Langley95c29f32014-06-20 12:00:00 -07001018 })
David Benjamin025b3d32014-07-01 19:53:04 -04001019
David Benjamin76d8abe2014-08-14 16:25:34 -04001020 testCases = append(testCases, testCase{
1021 testType: serverTest,
1022 name: ver.name + "-" + suite.name + "-server",
1023 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001024 MinVersion: ver.version,
1025 MaxVersion: ver.version,
1026 CipherSuites: []uint16{suite.id},
1027 Certificates: []Certificate{cert},
1028 PreSharedKey: []byte(psk),
1029 PreSharedKeyIdentity: pskIdentity,
David Benjamin76d8abe2014-08-14 16:25:34 -04001030 },
1031 certFile: certFile,
1032 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001033 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001034 resumeSession: true,
David Benjamin76d8abe2014-08-14 16:25:34 -04001035 })
David Benjamin6fd297b2014-08-11 18:43:38 -04001036
David Benjamin8b8c0062014-11-23 02:47:52 -05001037 if ver.hasDTLS && isDTLSCipher(suite.name) {
David Benjamin6fd297b2014-08-11 18:43:38 -04001038 testCases = append(testCases, testCase{
1039 testType: clientTest,
1040 protocol: dtls,
1041 name: "D" + ver.name + "-" + suite.name + "-client",
1042 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001043 MinVersion: ver.version,
1044 MaxVersion: ver.version,
1045 CipherSuites: []uint16{suite.id},
1046 Certificates: []Certificate{cert},
1047 PreSharedKey: []byte(psk),
1048 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001049 },
David Benjamin48cae082014-10-27 01:06:24 -04001050 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001051 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001052 })
1053 testCases = append(testCases, testCase{
1054 testType: serverTest,
1055 protocol: dtls,
1056 name: "D" + ver.name + "-" + suite.name + "-server",
1057 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001058 MinVersion: ver.version,
1059 MaxVersion: ver.version,
1060 CipherSuites: []uint16{suite.id},
1061 Certificates: []Certificate{cert},
1062 PreSharedKey: []byte(psk),
1063 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001064 },
1065 certFile: certFile,
1066 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001067 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001068 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001069 })
1070 }
Adam Langley95c29f32014-06-20 12:00:00 -07001071 }
1072 }
1073}
1074
1075func addBadECDSASignatureTests() {
1076 for badR := BadValue(1); badR < NumBadValues; badR++ {
1077 for badS := BadValue(1); badS < NumBadValues; badS++ {
David Benjamin025b3d32014-07-01 19:53:04 -04001078 testCases = append(testCases, testCase{
Adam Langley95c29f32014-06-20 12:00:00 -07001079 name: fmt.Sprintf("BadECDSA-%d-%d", badR, badS),
1080 config: Config{
1081 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
1082 Certificates: []Certificate{getECDSACertificate()},
1083 Bugs: ProtocolBugs{
1084 BadECDSAR: badR,
1085 BadECDSAS: badS,
1086 },
1087 },
1088 shouldFail: true,
1089 expectedError: "SIGNATURE",
1090 })
1091 }
1092 }
1093}
1094
Adam Langley80842bd2014-06-20 12:00:00 -07001095func addCBCPaddingTests() {
David Benjamin025b3d32014-07-01 19:53:04 -04001096 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001097 name: "MaxCBCPadding",
1098 config: Config{
1099 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1100 Bugs: ProtocolBugs{
1101 MaxPadding: true,
1102 },
1103 },
1104 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1105 })
David Benjamin025b3d32014-07-01 19:53:04 -04001106 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001107 name: "BadCBCPadding",
1108 config: Config{
1109 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1110 Bugs: ProtocolBugs{
1111 PaddingFirstByteBad: true,
1112 },
1113 },
1114 shouldFail: true,
1115 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1116 })
1117 // OpenSSL previously had an issue where the first byte of padding in
1118 // 255 bytes of padding wasn't checked.
David Benjamin025b3d32014-07-01 19:53:04 -04001119 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001120 name: "BadCBCPadding255",
1121 config: Config{
1122 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1123 Bugs: ProtocolBugs{
1124 MaxPadding: true,
1125 PaddingFirstByteBadIf255: true,
1126 },
1127 },
1128 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1129 shouldFail: true,
1130 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1131 })
1132}
1133
Kenny Root7fdeaf12014-08-05 15:23:37 -07001134func addCBCSplittingTests() {
1135 testCases = append(testCases, testCase{
1136 name: "CBCRecordSplitting",
1137 config: Config{
1138 MaxVersion: VersionTLS10,
1139 MinVersion: VersionTLS10,
1140 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1141 },
1142 messageLen: -1, // read until EOF
1143 flags: []string{
1144 "-async",
1145 "-write-different-record-sizes",
1146 "-cbc-record-splitting",
1147 },
David Benjamina8e3e0e2014-08-06 22:11:10 -04001148 })
1149 testCases = append(testCases, testCase{
Kenny Root7fdeaf12014-08-05 15:23:37 -07001150 name: "CBCRecordSplittingPartialWrite",
1151 config: Config{
1152 MaxVersion: VersionTLS10,
1153 MinVersion: VersionTLS10,
1154 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1155 },
1156 messageLen: -1, // read until EOF
1157 flags: []string{
1158 "-async",
1159 "-write-different-record-sizes",
1160 "-cbc-record-splitting",
1161 "-partial-write",
1162 },
1163 })
1164}
1165
David Benjamin636293b2014-07-08 17:59:18 -04001166func addClientAuthTests() {
David Benjamin407a10c2014-07-16 12:58:59 -04001167 // Add a dummy cert pool to stress certificate authority parsing.
1168 // TODO(davidben): Add tests that those values parse out correctly.
1169 certPool := x509.NewCertPool()
1170 cert, err := x509.ParseCertificate(rsaCertificate.Certificate[0])
1171 if err != nil {
1172 panic(err)
1173 }
1174 certPool.AddCert(cert)
1175
David Benjamin636293b2014-07-08 17:59:18 -04001176 for _, ver := range tlsVersions {
David Benjamin636293b2014-07-08 17:59:18 -04001177 testCases = append(testCases, testCase{
1178 testType: clientTest,
David Benjamin67666e72014-07-12 15:47:52 -04001179 name: ver.name + "-Client-ClientAuth-RSA",
David Benjamin636293b2014-07-08 17:59:18 -04001180 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001181 MinVersion: ver.version,
1182 MaxVersion: ver.version,
1183 ClientAuth: RequireAnyClientCert,
1184 ClientCAs: certPool,
David Benjamin636293b2014-07-08 17:59:18 -04001185 },
1186 flags: []string{
1187 "-cert-file", rsaCertificateFile,
1188 "-key-file", rsaKeyFile,
1189 },
1190 })
1191 testCases = append(testCases, testCase{
David Benjamin67666e72014-07-12 15:47:52 -04001192 testType: serverTest,
1193 name: ver.name + "-Server-ClientAuth-RSA",
1194 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001195 MinVersion: ver.version,
1196 MaxVersion: ver.version,
David Benjamin67666e72014-07-12 15:47:52 -04001197 Certificates: []Certificate{rsaCertificate},
1198 },
1199 flags: []string{"-require-any-client-certificate"},
1200 })
David Benjamine098ec22014-08-27 23:13:20 -04001201 if ver.version != VersionSSL30 {
1202 testCases = append(testCases, testCase{
1203 testType: serverTest,
1204 name: ver.name + "-Server-ClientAuth-ECDSA",
1205 config: Config{
1206 MinVersion: ver.version,
1207 MaxVersion: ver.version,
1208 Certificates: []Certificate{ecdsaCertificate},
1209 },
1210 flags: []string{"-require-any-client-certificate"},
1211 })
1212 testCases = append(testCases, testCase{
1213 testType: clientTest,
1214 name: ver.name + "-Client-ClientAuth-ECDSA",
1215 config: Config{
1216 MinVersion: ver.version,
1217 MaxVersion: ver.version,
1218 ClientAuth: RequireAnyClientCert,
1219 ClientCAs: certPool,
1220 },
1221 flags: []string{
1222 "-cert-file", ecdsaCertificateFile,
1223 "-key-file", ecdsaKeyFile,
1224 },
1225 })
1226 }
David Benjamin636293b2014-07-08 17:59:18 -04001227 }
1228}
1229
Adam Langley75712922014-10-10 16:23:43 -07001230func addExtendedMasterSecretTests() {
1231 const expectEMSFlag = "-expect-extended-master-secret"
1232
1233 for _, with := range []bool{false, true} {
1234 prefix := "No"
1235 var flags []string
1236 if with {
1237 prefix = ""
1238 flags = []string{expectEMSFlag}
1239 }
1240
1241 for _, isClient := range []bool{false, true} {
1242 suffix := "-Server"
1243 testType := serverTest
1244 if isClient {
1245 suffix = "-Client"
1246 testType = clientTest
1247 }
1248
1249 for _, ver := range tlsVersions {
1250 test := testCase{
1251 testType: testType,
1252 name: prefix + "ExtendedMasterSecret-" + ver.name + suffix,
1253 config: Config{
1254 MinVersion: ver.version,
1255 MaxVersion: ver.version,
1256 Bugs: ProtocolBugs{
1257 NoExtendedMasterSecret: !with,
1258 RequireExtendedMasterSecret: with,
1259 },
1260 },
David Benjamin48cae082014-10-27 01:06:24 -04001261 flags: flags,
1262 shouldFail: ver.version == VersionSSL30 && with,
Adam Langley75712922014-10-10 16:23:43 -07001263 }
1264 if test.shouldFail {
1265 test.expectedLocalError = "extended master secret required but not supported by peer"
1266 }
1267 testCases = append(testCases, test)
1268 }
1269 }
1270 }
1271
1272 // When a session is resumed, it should still be aware that its master
1273 // secret was generated via EMS and thus it's safe to use tls-unique.
1274 testCases = append(testCases, testCase{
1275 name: "ExtendedMasterSecret-Resume",
1276 config: Config{
1277 Bugs: ProtocolBugs{
1278 RequireExtendedMasterSecret: true,
1279 },
1280 },
1281 flags: []string{expectEMSFlag},
1282 resumeSession: true,
1283 })
1284}
1285
David Benjamin43ec06f2014-08-05 02:28:57 -04001286// Adds tests that try to cover the range of the handshake state machine, under
1287// various conditions. Some of these are redundant with other tests, but they
1288// only cover the synchronous case.
David Benjamin6fd297b2014-08-11 18:43:38 -04001289func addStateMachineCoverageTests(async, splitHandshake bool, protocol protocol) {
David Benjamin43ec06f2014-08-05 02:28:57 -04001290 var suffix string
1291 var flags []string
1292 var maxHandshakeRecordLength int
David Benjamin6fd297b2014-08-11 18:43:38 -04001293 if protocol == dtls {
1294 suffix = "-DTLS"
1295 }
David Benjamin43ec06f2014-08-05 02:28:57 -04001296 if async {
David Benjamin6fd297b2014-08-11 18:43:38 -04001297 suffix += "-Async"
David Benjamin43ec06f2014-08-05 02:28:57 -04001298 flags = append(flags, "-async")
1299 } else {
David Benjamin6fd297b2014-08-11 18:43:38 -04001300 suffix += "-Sync"
David Benjamin43ec06f2014-08-05 02:28:57 -04001301 }
1302 if splitHandshake {
1303 suffix += "-SplitHandshakeRecords"
David Benjamin98214542014-08-07 18:02:39 -04001304 maxHandshakeRecordLength = 1
David Benjamin43ec06f2014-08-05 02:28:57 -04001305 }
1306
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001307 // Basic handshake, with resumption. Client and server,
1308 // session ID and session ticket.
David Benjamin43ec06f2014-08-05 02:28:57 -04001309 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001310 protocol: protocol,
1311 name: "Basic-Client" + suffix,
David Benjamin43ec06f2014-08-05 02:28:57 -04001312 config: Config{
1313 Bugs: ProtocolBugs{
1314 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1315 },
1316 },
David Benjaminbed9aae2014-08-07 19:13:38 -04001317 flags: flags,
1318 resumeSession: true,
1319 })
1320 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001321 protocol: protocol,
1322 name: "Basic-Client-RenewTicket" + suffix,
David Benjaminbed9aae2014-08-07 19:13:38 -04001323 config: Config{
1324 Bugs: ProtocolBugs{
1325 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1326 RenewTicketOnResume: true,
1327 },
1328 },
1329 flags: flags,
1330 resumeSession: true,
David Benjamin43ec06f2014-08-05 02:28:57 -04001331 })
1332 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001333 protocol: protocol,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001334 name: "Basic-Client-NoTicket" + suffix,
1335 config: Config{
1336 SessionTicketsDisabled: true,
1337 Bugs: ProtocolBugs{
1338 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1339 },
1340 },
1341 flags: flags,
1342 resumeSession: true,
1343 })
1344 testCases = append(testCases, testCase{
1345 protocol: protocol,
David Benjamin43ec06f2014-08-05 02:28:57 -04001346 testType: serverTest,
1347 name: "Basic-Server" + suffix,
1348 config: Config{
1349 Bugs: ProtocolBugs{
1350 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1351 },
1352 },
David Benjaminbed9aae2014-08-07 19:13:38 -04001353 flags: flags,
1354 resumeSession: true,
David Benjamin43ec06f2014-08-05 02:28:57 -04001355 })
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001356 testCases = append(testCases, testCase{
1357 protocol: protocol,
1358 testType: serverTest,
1359 name: "Basic-Server-NoTickets" + suffix,
1360 config: Config{
1361 SessionTicketsDisabled: true,
1362 Bugs: ProtocolBugs{
1363 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1364 },
1365 },
1366 flags: flags,
1367 resumeSession: true,
1368 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001369
David Benjamin6fd297b2014-08-11 18:43:38 -04001370 // TLS client auth.
1371 testCases = append(testCases, testCase{
1372 protocol: protocol,
1373 testType: clientTest,
1374 name: "ClientAuth-Client" + suffix,
1375 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001376 ClientAuth: RequireAnyClientCert,
David Benjamin6fd297b2014-08-11 18:43:38 -04001377 Bugs: ProtocolBugs{
1378 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1379 },
1380 },
1381 flags: append(flags,
1382 "-cert-file", rsaCertificateFile,
1383 "-key-file", rsaKeyFile),
1384 })
1385 testCases = append(testCases, testCase{
1386 protocol: protocol,
1387 testType: serverTest,
1388 name: "ClientAuth-Server" + suffix,
1389 config: Config{
1390 Certificates: []Certificate{rsaCertificate},
1391 },
1392 flags: append(flags, "-require-any-client-certificate"),
1393 })
1394
David Benjamin43ec06f2014-08-05 02:28:57 -04001395 // No session ticket support; server doesn't send NewSessionTicket.
1396 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001397 protocol: protocol,
1398 name: "SessionTicketsDisabled-Client" + suffix,
David Benjamin43ec06f2014-08-05 02:28:57 -04001399 config: Config{
1400 SessionTicketsDisabled: true,
1401 Bugs: ProtocolBugs{
1402 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1403 },
1404 },
1405 flags: flags,
1406 })
1407 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001408 protocol: protocol,
David Benjamin43ec06f2014-08-05 02:28:57 -04001409 testType: serverTest,
1410 name: "SessionTicketsDisabled-Server" + suffix,
1411 config: Config{
1412 SessionTicketsDisabled: true,
1413 Bugs: ProtocolBugs{
1414 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1415 },
1416 },
1417 flags: flags,
1418 })
1419
David Benjamin48cae082014-10-27 01:06:24 -04001420 // Skip ServerKeyExchange in PSK key exchange if there's no
1421 // identity hint.
1422 testCases = append(testCases, testCase{
1423 protocol: protocol,
1424 name: "EmptyPSKHint-Client" + suffix,
1425 config: Config{
1426 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
1427 PreSharedKey: []byte("secret"),
1428 Bugs: ProtocolBugs{
1429 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1430 },
1431 },
1432 flags: append(flags, "-psk", "secret"),
1433 })
1434 testCases = append(testCases, testCase{
1435 protocol: protocol,
1436 testType: serverTest,
1437 name: "EmptyPSKHint-Server" + suffix,
1438 config: Config{
1439 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
1440 PreSharedKey: []byte("secret"),
1441 Bugs: ProtocolBugs{
1442 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1443 },
1444 },
1445 flags: append(flags, "-psk", "secret"),
1446 })
1447
David Benjamin6fd297b2014-08-11 18:43:38 -04001448 if protocol == tls {
1449 // NPN on client and server; results in post-handshake message.
1450 testCases = append(testCases, testCase{
1451 protocol: protocol,
1452 name: "NPN-Client" + suffix,
1453 config: Config{
David Benjaminae2888f2014-09-06 12:58:58 -04001454 NextProtos: []string{"foo"},
David Benjamin6fd297b2014-08-11 18:43:38 -04001455 Bugs: ProtocolBugs{
1456 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1457 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001458 },
David Benjaminfc7b0862014-09-06 13:21:53 -04001459 flags: append(flags, "-select-next-proto", "foo"),
1460 expectedNextProto: "foo",
1461 expectedNextProtoType: npn,
David Benjamin6fd297b2014-08-11 18:43:38 -04001462 })
1463 testCases = append(testCases, testCase{
1464 protocol: protocol,
1465 testType: serverTest,
1466 name: "NPN-Server" + suffix,
1467 config: Config{
1468 NextProtos: []string{"bar"},
1469 Bugs: ProtocolBugs{
1470 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1471 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001472 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001473 flags: append(flags,
1474 "-advertise-npn", "\x03foo\x03bar\x03baz",
1475 "-expect-next-proto", "bar"),
David Benjaminfc7b0862014-09-06 13:21:53 -04001476 expectedNextProto: "bar",
1477 expectedNextProtoType: npn,
David Benjamin6fd297b2014-08-11 18:43:38 -04001478 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001479
David Benjamin6fd297b2014-08-11 18:43:38 -04001480 // Client does False Start and negotiates NPN.
1481 testCases = append(testCases, testCase{
1482 protocol: protocol,
1483 name: "FalseStart" + suffix,
1484 config: Config{
1485 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1486 NextProtos: []string{"foo"},
1487 Bugs: ProtocolBugs{
David Benjamine58c4f52014-08-24 03:47:07 -04001488 ExpectFalseStart: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001489 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1490 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001491 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001492 flags: append(flags,
1493 "-false-start",
1494 "-select-next-proto", "foo"),
David Benjamine58c4f52014-08-24 03:47:07 -04001495 shimWritesFirst: true,
1496 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001497 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001498
David Benjaminae2888f2014-09-06 12:58:58 -04001499 // Client does False Start and negotiates ALPN.
1500 testCases = append(testCases, testCase{
1501 protocol: protocol,
1502 name: "FalseStart-ALPN" + suffix,
1503 config: Config{
1504 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1505 NextProtos: []string{"foo"},
1506 Bugs: ProtocolBugs{
1507 ExpectFalseStart: true,
1508 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1509 },
1510 },
1511 flags: append(flags,
1512 "-false-start",
1513 "-advertise-alpn", "\x03foo"),
1514 shimWritesFirst: true,
1515 resumeSession: true,
1516 })
1517
David Benjamin6fd297b2014-08-11 18:43:38 -04001518 // False Start without session tickets.
1519 testCases = append(testCases, testCase{
1520 name: "FalseStart-SessionTicketsDisabled",
1521 config: Config{
1522 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1523 NextProtos: []string{"foo"},
1524 SessionTicketsDisabled: true,
David Benjamin4e99c522014-08-24 01:45:30 -04001525 Bugs: ProtocolBugs{
David Benjamine58c4f52014-08-24 03:47:07 -04001526 ExpectFalseStart: true,
David Benjamin4e99c522014-08-24 01:45:30 -04001527 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1528 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001529 },
David Benjamin4e99c522014-08-24 01:45:30 -04001530 flags: append(flags,
David Benjamin6fd297b2014-08-11 18:43:38 -04001531 "-false-start",
1532 "-select-next-proto", "foo",
David Benjamin4e99c522014-08-24 01:45:30 -04001533 ),
David Benjamine58c4f52014-08-24 03:47:07 -04001534 shimWritesFirst: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001535 })
David Benjamin1e7f8d72014-08-08 12:27:04 -04001536
David Benjamina08e49d2014-08-24 01:46:07 -04001537 // Server parses a V2ClientHello.
David Benjamin6fd297b2014-08-11 18:43:38 -04001538 testCases = append(testCases, testCase{
1539 protocol: protocol,
1540 testType: serverTest,
1541 name: "SendV2ClientHello" + suffix,
1542 config: Config{
1543 // Choose a cipher suite that does not involve
1544 // elliptic curves, so no extensions are
1545 // involved.
1546 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
1547 Bugs: ProtocolBugs{
1548 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1549 SendV2ClientHello: true,
1550 },
David Benjamin1e7f8d72014-08-08 12:27:04 -04001551 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001552 flags: flags,
1553 })
David Benjamina08e49d2014-08-24 01:46:07 -04001554
1555 // Client sends a Channel ID.
1556 testCases = append(testCases, testCase{
1557 protocol: protocol,
1558 name: "ChannelID-Client" + suffix,
1559 config: Config{
1560 RequestChannelID: true,
1561 Bugs: ProtocolBugs{
1562 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1563 },
1564 },
1565 flags: append(flags,
1566 "-send-channel-id", channelIDKeyFile,
1567 ),
1568 resumeSession: true,
1569 expectChannelID: true,
1570 })
1571
1572 // Server accepts a Channel ID.
1573 testCases = append(testCases, testCase{
1574 protocol: protocol,
1575 testType: serverTest,
1576 name: "ChannelID-Server" + suffix,
1577 config: Config{
1578 ChannelID: channelIDKey,
1579 Bugs: ProtocolBugs{
1580 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1581 },
1582 },
1583 flags: append(flags,
1584 "-expect-channel-id",
1585 base64.StdEncoding.EncodeToString(channelIDBytes),
1586 ),
1587 resumeSession: true,
1588 expectChannelID: true,
1589 })
David Benjamin6fd297b2014-08-11 18:43:38 -04001590 } else {
1591 testCases = append(testCases, testCase{
1592 protocol: protocol,
1593 name: "SkipHelloVerifyRequest" + suffix,
1594 config: Config{
1595 Bugs: ProtocolBugs{
1596 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1597 SkipHelloVerifyRequest: true,
1598 },
1599 },
1600 flags: flags,
1601 })
1602
1603 testCases = append(testCases, testCase{
1604 testType: serverTest,
1605 protocol: protocol,
1606 name: "CookieExchange" + suffix,
1607 config: Config{
1608 Bugs: ProtocolBugs{
1609 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1610 },
1611 },
1612 flags: append(flags, "-cookie-exchange"),
1613 })
1614 }
David Benjamin43ec06f2014-08-05 02:28:57 -04001615}
1616
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001617func addVersionNegotiationTests() {
1618 for i, shimVers := range tlsVersions {
1619 // Assemble flags to disable all newer versions on the shim.
1620 var flags []string
1621 for _, vers := range tlsVersions[i+1:] {
1622 flags = append(flags, vers.flag)
1623 }
1624
1625 for _, runnerVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05001626 protocols := []protocol{tls}
1627 if runnerVers.hasDTLS && shimVers.hasDTLS {
1628 protocols = append(protocols, dtls)
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001629 }
David Benjamin8b8c0062014-11-23 02:47:52 -05001630 for _, protocol := range protocols {
1631 expectedVersion := shimVers.version
1632 if runnerVers.version < shimVers.version {
1633 expectedVersion = runnerVers.version
1634 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001635
David Benjamin8b8c0062014-11-23 02:47:52 -05001636 suffix := shimVers.name + "-" + runnerVers.name
1637 if protocol == dtls {
1638 suffix += "-DTLS"
1639 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001640
David Benjamin1eb367c2014-12-12 18:17:51 -05001641 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
1642
David Benjamin1e29a6b2014-12-10 02:27:24 -05001643 clientVers := shimVers.version
1644 if clientVers > VersionTLS10 {
1645 clientVers = VersionTLS10
1646 }
David Benjamin8b8c0062014-11-23 02:47:52 -05001647 testCases = append(testCases, testCase{
1648 protocol: protocol,
1649 testType: clientTest,
1650 name: "VersionNegotiation-Client-" + suffix,
1651 config: Config{
1652 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05001653 Bugs: ProtocolBugs{
1654 ExpectInitialRecordVersion: clientVers,
1655 },
David Benjamin8b8c0062014-11-23 02:47:52 -05001656 },
1657 flags: flags,
1658 expectedVersion: expectedVersion,
1659 })
David Benjamin1eb367c2014-12-12 18:17:51 -05001660 testCases = append(testCases, testCase{
1661 protocol: protocol,
1662 testType: clientTest,
1663 name: "VersionNegotiation-Client2-" + suffix,
1664 config: Config{
1665 MaxVersion: runnerVers.version,
1666 Bugs: ProtocolBugs{
1667 ExpectInitialRecordVersion: clientVers,
1668 },
1669 },
1670 flags: []string{"-max-version", shimVersFlag},
1671 expectedVersion: expectedVersion,
1672 })
David Benjamin8b8c0062014-11-23 02:47:52 -05001673
1674 testCases = append(testCases, testCase{
1675 protocol: protocol,
1676 testType: serverTest,
1677 name: "VersionNegotiation-Server-" + suffix,
1678 config: Config{
1679 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05001680 Bugs: ProtocolBugs{
1681 ExpectInitialRecordVersion: expectedVersion,
1682 },
David Benjamin8b8c0062014-11-23 02:47:52 -05001683 },
1684 flags: flags,
1685 expectedVersion: expectedVersion,
1686 })
David Benjamin1eb367c2014-12-12 18:17:51 -05001687 testCases = append(testCases, testCase{
1688 protocol: protocol,
1689 testType: serverTest,
1690 name: "VersionNegotiation-Server2-" + suffix,
1691 config: Config{
1692 MaxVersion: runnerVers.version,
1693 Bugs: ProtocolBugs{
1694 ExpectInitialRecordVersion: expectedVersion,
1695 },
1696 },
1697 flags: []string{"-max-version", shimVersFlag},
1698 expectedVersion: expectedVersion,
1699 })
David Benjamin8b8c0062014-11-23 02:47:52 -05001700 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001701 }
1702 }
1703}
1704
David Benjaminaccb4542014-12-12 23:44:33 -05001705func addMinimumVersionTests() {
1706 for i, shimVers := range tlsVersions {
1707 // Assemble flags to disable all older versions on the shim.
1708 var flags []string
1709 for _, vers := range tlsVersions[:i] {
1710 flags = append(flags, vers.flag)
1711 }
1712
1713 for _, runnerVers := range tlsVersions {
1714 protocols := []protocol{tls}
1715 if runnerVers.hasDTLS && shimVers.hasDTLS {
1716 protocols = append(protocols, dtls)
1717 }
1718 for _, protocol := range protocols {
1719 suffix := shimVers.name + "-" + runnerVers.name
1720 if protocol == dtls {
1721 suffix += "-DTLS"
1722 }
1723 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
1724
David Benjaminaccb4542014-12-12 23:44:33 -05001725 var expectedVersion uint16
1726 var shouldFail bool
1727 var expectedError string
David Benjamin87909c02014-12-13 01:55:01 -05001728 var expectedLocalError string
David Benjaminaccb4542014-12-12 23:44:33 -05001729 if runnerVers.version >= shimVers.version {
1730 expectedVersion = runnerVers.version
1731 } else {
1732 shouldFail = true
1733 expectedError = ":UNSUPPORTED_PROTOCOL:"
David Benjamin87909c02014-12-13 01:55:01 -05001734 if runnerVers.version > VersionSSL30 {
1735 expectedLocalError = "remote error: protocol version not supported"
1736 } else {
1737 expectedLocalError = "remote error: handshake failure"
1738 }
David Benjaminaccb4542014-12-12 23:44:33 -05001739 }
1740
1741 testCases = append(testCases, testCase{
1742 protocol: protocol,
1743 testType: clientTest,
1744 name: "MinimumVersion-Client-" + suffix,
1745 config: Config{
1746 MaxVersion: runnerVers.version,
1747 },
David Benjamin87909c02014-12-13 01:55:01 -05001748 flags: flags,
1749 expectedVersion: expectedVersion,
1750 shouldFail: shouldFail,
1751 expectedError: expectedError,
1752 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05001753 })
1754 testCases = append(testCases, testCase{
1755 protocol: protocol,
1756 testType: clientTest,
1757 name: "MinimumVersion-Client2-" + suffix,
1758 config: Config{
1759 MaxVersion: runnerVers.version,
1760 },
David Benjamin87909c02014-12-13 01:55:01 -05001761 flags: []string{"-min-version", shimVersFlag},
1762 expectedVersion: expectedVersion,
1763 shouldFail: shouldFail,
1764 expectedError: expectedError,
1765 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05001766 })
1767
1768 testCases = append(testCases, testCase{
1769 protocol: protocol,
1770 testType: serverTest,
1771 name: "MinimumVersion-Server-" + suffix,
1772 config: Config{
1773 MaxVersion: runnerVers.version,
1774 },
David Benjamin87909c02014-12-13 01:55:01 -05001775 flags: flags,
1776 expectedVersion: expectedVersion,
1777 shouldFail: shouldFail,
1778 expectedError: expectedError,
1779 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05001780 })
1781 testCases = append(testCases, testCase{
1782 protocol: protocol,
1783 testType: serverTest,
1784 name: "MinimumVersion-Server2-" + suffix,
1785 config: Config{
1786 MaxVersion: runnerVers.version,
1787 },
David Benjamin87909c02014-12-13 01:55:01 -05001788 flags: []string{"-min-version", shimVersFlag},
1789 expectedVersion: expectedVersion,
1790 shouldFail: shouldFail,
1791 expectedError: expectedError,
1792 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05001793 })
1794 }
1795 }
1796 }
1797}
1798
David Benjamin5c24a1d2014-08-31 00:59:27 -04001799func addD5BugTests() {
1800 testCases = append(testCases, testCase{
1801 testType: serverTest,
1802 name: "D5Bug-NoQuirk-Reject",
1803 config: Config{
1804 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
1805 Bugs: ProtocolBugs{
1806 SSL3RSAKeyExchange: true,
1807 },
1808 },
1809 shouldFail: true,
1810 expectedError: ":TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG:",
1811 })
1812 testCases = append(testCases, testCase{
1813 testType: serverTest,
1814 name: "D5Bug-Quirk-Normal",
1815 config: Config{
1816 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
1817 },
1818 flags: []string{"-tls-d5-bug"},
1819 })
1820 testCases = append(testCases, testCase{
1821 testType: serverTest,
1822 name: "D5Bug-Quirk-Bug",
1823 config: Config{
1824 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
1825 Bugs: ProtocolBugs{
1826 SSL3RSAKeyExchange: true,
1827 },
1828 },
1829 flags: []string{"-tls-d5-bug"},
1830 })
1831}
1832
David Benjamine78bfde2014-09-06 12:45:15 -04001833func addExtensionTests() {
1834 testCases = append(testCases, testCase{
1835 testType: clientTest,
1836 name: "DuplicateExtensionClient",
1837 config: Config{
1838 Bugs: ProtocolBugs{
1839 DuplicateExtension: true,
1840 },
1841 },
1842 shouldFail: true,
1843 expectedLocalError: "remote error: error decoding message",
1844 })
1845 testCases = append(testCases, testCase{
1846 testType: serverTest,
1847 name: "DuplicateExtensionServer",
1848 config: Config{
1849 Bugs: ProtocolBugs{
1850 DuplicateExtension: true,
1851 },
1852 },
1853 shouldFail: true,
1854 expectedLocalError: "remote error: error decoding message",
1855 })
1856 testCases = append(testCases, testCase{
1857 testType: clientTest,
1858 name: "ServerNameExtensionClient",
1859 config: Config{
1860 Bugs: ProtocolBugs{
1861 ExpectServerName: "example.com",
1862 },
1863 },
1864 flags: []string{"-host-name", "example.com"},
1865 })
1866 testCases = append(testCases, testCase{
1867 testType: clientTest,
1868 name: "ServerNameExtensionClient",
1869 config: Config{
1870 Bugs: ProtocolBugs{
1871 ExpectServerName: "mismatch.com",
1872 },
1873 },
1874 flags: []string{"-host-name", "example.com"},
1875 shouldFail: true,
1876 expectedLocalError: "tls: unexpected server name",
1877 })
1878 testCases = append(testCases, testCase{
1879 testType: clientTest,
1880 name: "ServerNameExtensionClient",
1881 config: Config{
1882 Bugs: ProtocolBugs{
1883 ExpectServerName: "missing.com",
1884 },
1885 },
1886 shouldFail: true,
1887 expectedLocalError: "tls: unexpected server name",
1888 })
1889 testCases = append(testCases, testCase{
1890 testType: serverTest,
1891 name: "ServerNameExtensionServer",
1892 config: Config{
1893 ServerName: "example.com",
1894 },
1895 flags: []string{"-expect-server-name", "example.com"},
1896 resumeSession: true,
1897 })
David Benjaminae2888f2014-09-06 12:58:58 -04001898 testCases = append(testCases, testCase{
1899 testType: clientTest,
1900 name: "ALPNClient",
1901 config: Config{
1902 NextProtos: []string{"foo"},
1903 },
1904 flags: []string{
1905 "-advertise-alpn", "\x03foo\x03bar\x03baz",
1906 "-expect-alpn", "foo",
1907 },
David Benjaminfc7b0862014-09-06 13:21:53 -04001908 expectedNextProto: "foo",
1909 expectedNextProtoType: alpn,
1910 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04001911 })
1912 testCases = append(testCases, testCase{
1913 testType: serverTest,
1914 name: "ALPNServer",
1915 config: Config{
1916 NextProtos: []string{"foo", "bar", "baz"},
1917 },
1918 flags: []string{
1919 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
1920 "-select-alpn", "foo",
1921 },
David Benjaminfc7b0862014-09-06 13:21:53 -04001922 expectedNextProto: "foo",
1923 expectedNextProtoType: alpn,
1924 resumeSession: true,
1925 })
1926 // Test that the server prefers ALPN over NPN.
1927 testCases = append(testCases, testCase{
1928 testType: serverTest,
1929 name: "ALPNServer-Preferred",
1930 config: Config{
1931 NextProtos: []string{"foo", "bar", "baz"},
1932 },
1933 flags: []string{
1934 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
1935 "-select-alpn", "foo",
1936 "-advertise-npn", "\x03foo\x03bar\x03baz",
1937 },
1938 expectedNextProto: "foo",
1939 expectedNextProtoType: alpn,
1940 resumeSession: true,
1941 })
1942 testCases = append(testCases, testCase{
1943 testType: serverTest,
1944 name: "ALPNServer-Preferred-Swapped",
1945 config: Config{
1946 NextProtos: []string{"foo", "bar", "baz"},
1947 Bugs: ProtocolBugs{
1948 SwapNPNAndALPN: true,
1949 },
1950 },
1951 flags: []string{
1952 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
1953 "-select-alpn", "foo",
1954 "-advertise-npn", "\x03foo\x03bar\x03baz",
1955 },
1956 expectedNextProto: "foo",
1957 expectedNextProtoType: alpn,
1958 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04001959 })
Adam Langley38311732014-10-16 19:04:35 -07001960 // Resume with a corrupt ticket.
1961 testCases = append(testCases, testCase{
1962 testType: serverTest,
1963 name: "CorruptTicket",
1964 config: Config{
1965 Bugs: ProtocolBugs{
1966 CorruptTicket: true,
1967 },
1968 },
1969 resumeSession: true,
1970 flags: []string{"-expect-session-miss"},
1971 })
1972 // Resume with an oversized session id.
1973 testCases = append(testCases, testCase{
1974 testType: serverTest,
1975 name: "OversizedSessionId",
1976 config: Config{
1977 Bugs: ProtocolBugs{
1978 OversizedSessionId: true,
1979 },
1980 },
1981 resumeSession: true,
Adam Langley75712922014-10-10 16:23:43 -07001982 shouldFail: true,
Adam Langley38311732014-10-16 19:04:35 -07001983 expectedError: ":DECODE_ERROR:",
1984 })
David Benjaminca6c8262014-11-15 19:06:08 -05001985 // Basic DTLS-SRTP tests. Include fake profiles to ensure they
1986 // are ignored.
1987 testCases = append(testCases, testCase{
1988 protocol: dtls,
1989 name: "SRTP-Client",
1990 config: Config{
1991 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
1992 },
1993 flags: []string{
1994 "-srtp-profiles",
1995 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
1996 },
1997 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
1998 })
1999 testCases = append(testCases, testCase{
2000 protocol: dtls,
2001 testType: serverTest,
2002 name: "SRTP-Server",
2003 config: Config{
2004 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
2005 },
2006 flags: []string{
2007 "-srtp-profiles",
2008 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2009 },
2010 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2011 })
2012 // Test that the MKI is ignored.
2013 testCases = append(testCases, testCase{
2014 protocol: dtls,
2015 testType: serverTest,
2016 name: "SRTP-Server-IgnoreMKI",
2017 config: Config{
2018 SRTPProtectionProfiles: []uint16{SRTP_AES128_CM_HMAC_SHA1_80},
2019 Bugs: ProtocolBugs{
2020 SRTPMasterKeyIdentifer: "bogus",
2021 },
2022 },
2023 flags: []string{
2024 "-srtp-profiles",
2025 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2026 },
2027 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2028 })
2029 // Test that SRTP isn't negotiated on the server if there were
2030 // no matching profiles.
2031 testCases = append(testCases, testCase{
2032 protocol: dtls,
2033 testType: serverTest,
2034 name: "SRTP-Server-NoMatch",
2035 config: Config{
2036 SRTPProtectionProfiles: []uint16{100, 101, 102},
2037 },
2038 flags: []string{
2039 "-srtp-profiles",
2040 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2041 },
2042 expectedSRTPProtectionProfile: 0,
2043 })
2044 // Test that the server returning an invalid SRTP profile is
2045 // flagged as an error by the client.
2046 testCases = append(testCases, testCase{
2047 protocol: dtls,
2048 name: "SRTP-Client-NoMatch",
2049 config: Config{
2050 Bugs: ProtocolBugs{
2051 SendSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_32,
2052 },
2053 },
2054 flags: []string{
2055 "-srtp-profiles",
2056 "SRTP_AES128_CM_SHA1_80",
2057 },
2058 shouldFail: true,
2059 expectedError: ":BAD_SRTP_PROTECTION_PROFILE_LIST:",
2060 })
David Benjamin61f95272014-11-25 01:55:35 -05002061 // Test OCSP stapling and SCT list.
2062 testCases = append(testCases, testCase{
2063 name: "OCSPStapling",
2064 flags: []string{
2065 "-enable-ocsp-stapling",
2066 "-expect-ocsp-response",
2067 base64.StdEncoding.EncodeToString(testOCSPResponse),
2068 },
2069 })
2070 testCases = append(testCases, testCase{
2071 name: "SignedCertificateTimestampList",
2072 flags: []string{
2073 "-enable-signed-cert-timestamps",
2074 "-expect-signed-cert-timestamps",
2075 base64.StdEncoding.EncodeToString(testSCTList),
2076 },
2077 })
David Benjamine78bfde2014-09-06 12:45:15 -04002078}
2079
David Benjamin01fe8202014-09-24 15:21:44 -04002080func addResumptionVersionTests() {
David Benjamin01fe8202014-09-24 15:21:44 -04002081 for _, sessionVers := range tlsVersions {
David Benjamin01fe8202014-09-24 15:21:44 -04002082 for _, resumeVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05002083 protocols := []protocol{tls}
2084 if sessionVers.hasDTLS && resumeVers.hasDTLS {
2085 protocols = append(protocols, dtls)
David Benjaminbdf5e722014-11-11 00:52:15 -05002086 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002087 for _, protocol := range protocols {
2088 suffix := "-" + sessionVers.name + "-" + resumeVers.name
2089 if protocol == dtls {
2090 suffix += "-DTLS"
2091 }
2092
2093 testCases = append(testCases, testCase{
2094 protocol: protocol,
2095 name: "Resume-Client" + suffix,
2096 resumeSession: true,
2097 config: Config{
2098 MaxVersion: sessionVers.version,
2099 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2100 Bugs: ProtocolBugs{
2101 AllowSessionVersionMismatch: true,
2102 },
2103 },
2104 expectedVersion: sessionVers.version,
2105 resumeConfig: &Config{
2106 MaxVersion: resumeVers.version,
2107 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2108 Bugs: ProtocolBugs{
2109 AllowSessionVersionMismatch: true,
2110 },
2111 },
2112 expectedResumeVersion: resumeVers.version,
2113 })
2114
2115 testCases = append(testCases, testCase{
2116 protocol: protocol,
2117 name: "Resume-Client-NoResume" + suffix,
2118 flags: []string{"-expect-session-miss"},
2119 resumeSession: true,
2120 config: Config{
2121 MaxVersion: sessionVers.version,
2122 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2123 },
2124 expectedVersion: sessionVers.version,
2125 resumeConfig: &Config{
2126 MaxVersion: resumeVers.version,
2127 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2128 },
2129 newSessionsOnResume: true,
2130 expectedResumeVersion: resumeVers.version,
2131 })
2132
2133 var flags []string
2134 if sessionVers.version != resumeVers.version {
2135 flags = append(flags, "-expect-session-miss")
2136 }
2137 testCases = append(testCases, testCase{
2138 protocol: protocol,
2139 testType: serverTest,
2140 name: "Resume-Server" + suffix,
2141 flags: flags,
2142 resumeSession: true,
2143 config: Config{
2144 MaxVersion: sessionVers.version,
2145 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2146 },
2147 expectedVersion: sessionVers.version,
2148 resumeConfig: &Config{
2149 MaxVersion: resumeVers.version,
2150 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2151 },
2152 expectedResumeVersion: resumeVers.version,
2153 })
2154 }
David Benjamin01fe8202014-09-24 15:21:44 -04002155 }
2156 }
2157}
2158
Adam Langley2ae77d22014-10-28 17:29:33 -07002159func addRenegotiationTests() {
2160 testCases = append(testCases, testCase{
2161 testType: serverTest,
2162 name: "Renegotiate-Server",
2163 flags: []string{"-renegotiate"},
2164 shimWritesFirst: true,
2165 })
2166 testCases = append(testCases, testCase{
2167 testType: serverTest,
2168 name: "Renegotiate-Server-EmptyExt",
2169 config: Config{
2170 Bugs: ProtocolBugs{
2171 EmptyRenegotiationInfo: true,
2172 },
2173 },
2174 flags: []string{"-renegotiate"},
2175 shimWritesFirst: true,
2176 shouldFail: true,
2177 expectedError: ":RENEGOTIATION_MISMATCH:",
2178 })
2179 testCases = append(testCases, testCase{
2180 testType: serverTest,
2181 name: "Renegotiate-Server-BadExt",
2182 config: Config{
2183 Bugs: ProtocolBugs{
2184 BadRenegotiationInfo: true,
2185 },
2186 },
2187 flags: []string{"-renegotiate"},
2188 shimWritesFirst: true,
2189 shouldFail: true,
2190 expectedError: ":RENEGOTIATION_MISMATCH:",
2191 })
David Benjaminca6554b2014-11-08 12:31:52 -05002192 testCases = append(testCases, testCase{
2193 testType: serverTest,
2194 name: "Renegotiate-Server-ClientInitiated",
2195 renegotiate: true,
2196 })
2197 testCases = append(testCases, testCase{
2198 testType: serverTest,
2199 name: "Renegotiate-Server-ClientInitiated-NoExt",
2200 renegotiate: true,
2201 config: Config{
2202 Bugs: ProtocolBugs{
2203 NoRenegotiationInfo: true,
2204 },
2205 },
2206 shouldFail: true,
2207 expectedError: ":UNSAFE_LEGACY_RENEGOTIATION_DISABLED:",
2208 })
2209 testCases = append(testCases, testCase{
2210 testType: serverTest,
2211 name: "Renegotiate-Server-ClientInitiated-NoExt-Allowed",
2212 renegotiate: true,
2213 config: Config{
2214 Bugs: ProtocolBugs{
2215 NoRenegotiationInfo: true,
2216 },
2217 },
2218 flags: []string{"-allow-unsafe-legacy-renegotiation"},
2219 })
Adam Langley2ae77d22014-10-28 17:29:33 -07002220 // TODO(agl): test the renegotiation info SCSV.
Adam Langleycf2d4f42014-10-28 19:06:14 -07002221 testCases = append(testCases, testCase{
2222 name: "Renegotiate-Client",
2223 renegotiate: true,
2224 })
2225 testCases = append(testCases, testCase{
2226 name: "Renegotiate-Client-EmptyExt",
2227 renegotiate: true,
2228 config: Config{
2229 Bugs: ProtocolBugs{
2230 EmptyRenegotiationInfo: true,
2231 },
2232 },
2233 shouldFail: true,
2234 expectedError: ":RENEGOTIATION_MISMATCH:",
2235 })
2236 testCases = append(testCases, testCase{
2237 name: "Renegotiate-Client-BadExt",
2238 renegotiate: true,
2239 config: Config{
2240 Bugs: ProtocolBugs{
2241 BadRenegotiationInfo: true,
2242 },
2243 },
2244 shouldFail: true,
2245 expectedError: ":RENEGOTIATION_MISMATCH:",
2246 })
2247 testCases = append(testCases, testCase{
2248 name: "Renegotiate-Client-SwitchCiphers",
2249 renegotiate: true,
2250 config: Config{
2251 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2252 },
2253 renegotiateCiphers: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2254 })
2255 testCases = append(testCases, testCase{
2256 name: "Renegotiate-Client-SwitchCiphers2",
2257 renegotiate: true,
2258 config: Config{
2259 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2260 },
2261 renegotiateCiphers: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2262 })
David Benjaminc44b1df2014-11-23 12:11:01 -05002263 testCases = append(testCases, testCase{
2264 name: "Renegotiate-SameClientVersion",
2265 renegotiate: true,
2266 config: Config{
2267 MaxVersion: VersionTLS10,
2268 Bugs: ProtocolBugs{
2269 RequireSameRenegoClientVersion: true,
2270 },
2271 },
2272 })
Adam Langley2ae77d22014-10-28 17:29:33 -07002273}
2274
David Benjamin5e961c12014-11-07 01:48:35 -05002275func addDTLSReplayTests() {
2276 // Test that sequence number replays are detected.
2277 testCases = append(testCases, testCase{
2278 protocol: dtls,
2279 name: "DTLS-Replay",
2280 replayWrites: true,
2281 })
2282
2283 // Test the outgoing sequence number skipping by values larger
2284 // than the retransmit window.
2285 testCases = append(testCases, testCase{
2286 protocol: dtls,
2287 name: "DTLS-Replay-LargeGaps",
2288 config: Config{
2289 Bugs: ProtocolBugs{
2290 SequenceNumberIncrement: 127,
2291 },
2292 },
2293 replayWrites: true,
2294 })
2295}
2296
Feng Lu41aa3252014-11-21 22:47:56 -08002297func addFastRadioPaddingTests() {
David Benjamin1e29a6b2014-12-10 02:27:24 -05002298 testCases = append(testCases, testCase{
2299 protocol: tls,
2300 name: "FastRadio-Padding",
Feng Lu41aa3252014-11-21 22:47:56 -08002301 config: Config{
2302 Bugs: ProtocolBugs{
2303 RequireFastradioPadding: true,
2304 },
2305 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05002306 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08002307 })
David Benjamin1e29a6b2014-12-10 02:27:24 -05002308 testCases = append(testCases, testCase{
2309 protocol: dtls,
2310 name: "FastRadio-Padding",
Feng Lu41aa3252014-11-21 22:47:56 -08002311 config: Config{
2312 Bugs: ProtocolBugs{
2313 RequireFastradioPadding: true,
2314 },
2315 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05002316 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08002317 })
2318}
2319
David Benjamin000800a2014-11-14 01:43:59 -05002320var testHashes = []struct {
2321 name string
2322 id uint8
2323}{
2324 {"SHA1", hashSHA1},
2325 {"SHA224", hashSHA224},
2326 {"SHA256", hashSHA256},
2327 {"SHA384", hashSHA384},
2328 {"SHA512", hashSHA512},
2329}
2330
2331func addSigningHashTests() {
2332 // Make sure each hash works. Include some fake hashes in the list and
2333 // ensure they're ignored.
2334 for _, hash := range testHashes {
2335 testCases = append(testCases, testCase{
2336 name: "SigningHash-ClientAuth-" + hash.name,
2337 config: Config{
2338 ClientAuth: RequireAnyClientCert,
2339 SignatureAndHashes: []signatureAndHash{
2340 {signatureRSA, 42},
2341 {signatureRSA, hash.id},
2342 {signatureRSA, 255},
2343 },
2344 },
2345 flags: []string{
2346 "-cert-file", rsaCertificateFile,
2347 "-key-file", rsaKeyFile,
2348 },
2349 })
2350
2351 testCases = append(testCases, testCase{
2352 testType: serverTest,
2353 name: "SigningHash-ServerKeyExchange-Sign-" + hash.name,
2354 config: Config{
2355 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2356 SignatureAndHashes: []signatureAndHash{
2357 {signatureRSA, 42},
2358 {signatureRSA, hash.id},
2359 {signatureRSA, 255},
2360 },
2361 },
2362 })
2363 }
2364
2365 // Test that hash resolution takes the signature type into account.
2366 testCases = append(testCases, testCase{
2367 name: "SigningHash-ClientAuth-SignatureType",
2368 config: Config{
2369 ClientAuth: RequireAnyClientCert,
2370 SignatureAndHashes: []signatureAndHash{
2371 {signatureECDSA, hashSHA512},
2372 {signatureRSA, hashSHA384},
2373 {signatureECDSA, hashSHA1},
2374 },
2375 },
2376 flags: []string{
2377 "-cert-file", rsaCertificateFile,
2378 "-key-file", rsaKeyFile,
2379 },
2380 })
2381
2382 testCases = append(testCases, testCase{
2383 testType: serverTest,
2384 name: "SigningHash-ServerKeyExchange-SignatureType",
2385 config: Config{
2386 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2387 SignatureAndHashes: []signatureAndHash{
2388 {signatureECDSA, hashSHA512},
2389 {signatureRSA, hashSHA384},
2390 {signatureECDSA, hashSHA1},
2391 },
2392 },
2393 })
2394
2395 // Test that, if the list is missing, the peer falls back to SHA-1.
2396 testCases = append(testCases, testCase{
2397 name: "SigningHash-ClientAuth-Fallback",
2398 config: Config{
2399 ClientAuth: RequireAnyClientCert,
2400 SignatureAndHashes: []signatureAndHash{
2401 {signatureRSA, hashSHA1},
2402 },
2403 Bugs: ProtocolBugs{
2404 NoSignatureAndHashes: true,
2405 },
2406 },
2407 flags: []string{
2408 "-cert-file", rsaCertificateFile,
2409 "-key-file", rsaKeyFile,
2410 },
2411 })
2412
2413 testCases = append(testCases, testCase{
2414 testType: serverTest,
2415 name: "SigningHash-ServerKeyExchange-Fallback",
2416 config: Config{
2417 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2418 SignatureAndHashes: []signatureAndHash{
2419 {signatureRSA, hashSHA1},
2420 },
2421 Bugs: ProtocolBugs{
2422 NoSignatureAndHashes: true,
2423 },
2424 },
2425 })
2426}
2427
David Benjamin884fdf12014-08-02 15:28:23 -04002428func worker(statusChan chan statusMsg, c chan *testCase, buildDir string, wg *sync.WaitGroup) {
Adam Langley95c29f32014-06-20 12:00:00 -07002429 defer wg.Done()
2430
2431 for test := range c {
Adam Langley69a01602014-11-17 17:26:55 -08002432 var err error
2433
2434 if *mallocTest < 0 {
2435 statusChan <- statusMsg{test: test, started: true}
2436 err = runTest(test, buildDir, -1)
2437 } else {
2438 for mallocNumToFail := int64(*mallocTest); ; mallocNumToFail++ {
2439 statusChan <- statusMsg{test: test, started: true}
2440 if err = runTest(test, buildDir, mallocNumToFail); err != errMoreMallocs {
2441 if err != nil {
2442 fmt.Printf("\n\nmalloc test failed at %d: %s\n", mallocNumToFail, err)
2443 }
2444 break
2445 }
2446 }
2447 }
Adam Langley95c29f32014-06-20 12:00:00 -07002448 statusChan <- statusMsg{test: test, err: err}
2449 }
2450}
2451
2452type statusMsg struct {
2453 test *testCase
2454 started bool
2455 err error
2456}
2457
2458func statusPrinter(doneChan chan struct{}, statusChan chan statusMsg, total int) {
2459 var started, done, failed, lineLen int
2460 defer close(doneChan)
2461
2462 for msg := range statusChan {
2463 if msg.started {
2464 started++
2465 } else {
2466 done++
2467 }
2468
2469 fmt.Printf("\x1b[%dD\x1b[K", lineLen)
2470
2471 if msg.err != nil {
2472 fmt.Printf("FAILED (%s)\n%s\n", msg.test.name, msg.err)
2473 failed++
2474 }
2475 line := fmt.Sprintf("%d/%d/%d/%d", failed, done, started, total)
2476 lineLen = len(line)
2477 os.Stdout.WriteString(line)
2478 }
2479}
2480
2481func main() {
2482 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 -04002483 var flagNumWorkers *int = flag.Int("num-workers", runtime.NumCPU(), "The number of workers to run in parallel.")
David Benjamin884fdf12014-08-02 15:28:23 -04002484 var flagBuildDir *string = flag.String("build-dir", "../../../build", "The build directory to run the shim from.")
Adam Langley95c29f32014-06-20 12:00:00 -07002485
2486 flag.Parse()
2487
2488 addCipherSuiteTests()
2489 addBadECDSASignatureTests()
Adam Langley80842bd2014-06-20 12:00:00 -07002490 addCBCPaddingTests()
Kenny Root7fdeaf12014-08-05 15:23:37 -07002491 addCBCSplittingTests()
David Benjamin636293b2014-07-08 17:59:18 -04002492 addClientAuthTests()
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002493 addVersionNegotiationTests()
David Benjaminaccb4542014-12-12 23:44:33 -05002494 addMinimumVersionTests()
David Benjamin5c24a1d2014-08-31 00:59:27 -04002495 addD5BugTests()
David Benjamine78bfde2014-09-06 12:45:15 -04002496 addExtensionTests()
David Benjamin01fe8202014-09-24 15:21:44 -04002497 addResumptionVersionTests()
Adam Langley75712922014-10-10 16:23:43 -07002498 addExtendedMasterSecretTests()
Adam Langley2ae77d22014-10-28 17:29:33 -07002499 addRenegotiationTests()
David Benjamin5e961c12014-11-07 01:48:35 -05002500 addDTLSReplayTests()
David Benjamin000800a2014-11-14 01:43:59 -05002501 addSigningHashTests()
Feng Lu41aa3252014-11-21 22:47:56 -08002502 addFastRadioPaddingTests()
David Benjamin43ec06f2014-08-05 02:28:57 -04002503 for _, async := range []bool{false, true} {
2504 for _, splitHandshake := range []bool{false, true} {
David Benjamin6fd297b2014-08-11 18:43:38 -04002505 for _, protocol := range []protocol{tls, dtls} {
2506 addStateMachineCoverageTests(async, splitHandshake, protocol)
2507 }
David Benjamin43ec06f2014-08-05 02:28:57 -04002508 }
2509 }
Adam Langley95c29f32014-06-20 12:00:00 -07002510
2511 var wg sync.WaitGroup
2512
David Benjamin2bc8e6f2014-08-02 15:22:37 -04002513 numWorkers := *flagNumWorkers
Adam Langley95c29f32014-06-20 12:00:00 -07002514
2515 statusChan := make(chan statusMsg, numWorkers)
2516 testChan := make(chan *testCase, numWorkers)
2517 doneChan := make(chan struct{})
2518
David Benjamin025b3d32014-07-01 19:53:04 -04002519 go statusPrinter(doneChan, statusChan, len(testCases))
Adam Langley95c29f32014-06-20 12:00:00 -07002520
2521 for i := 0; i < numWorkers; i++ {
2522 wg.Add(1)
David Benjamin884fdf12014-08-02 15:28:23 -04002523 go worker(statusChan, testChan, *flagBuildDir, &wg)
Adam Langley95c29f32014-06-20 12:00:00 -07002524 }
2525
David Benjamin025b3d32014-07-01 19:53:04 -04002526 for i := range testCases {
2527 if len(*flagTest) == 0 || *flagTest == testCases[i].name {
2528 testChan <- &testCases[i]
Adam Langley95c29f32014-06-20 12:00:00 -07002529 }
2530 }
2531
2532 close(testChan)
2533 wg.Wait()
2534 close(statusChan)
2535 <-doneChan
2536
2537 fmt.Printf("\n")
2538}