blob: 421940d3952affe96b3ec42be75edee58887472b [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001package main
2
3import (
4 "bytes"
David Benjamina08e49d2014-08-24 01:46:07 -04005 "crypto/ecdsa"
6 "crypto/elliptic"
David Benjamin407a10c2014-07-16 12:58:59 -04007 "crypto/x509"
David Benjamin2561dc32014-08-24 01:25:27 -04008 "encoding/base64"
David Benjamina08e49d2014-08-24 01:46:07 -04009 "encoding/pem"
Adam Langley95c29f32014-06-20 12:00:00 -070010 "flag"
11 "fmt"
12 "io"
Kenny Root7fdeaf12014-08-05 15:23:37 -070013 "io/ioutil"
Adam Langley95c29f32014-06-20 12:00:00 -070014 "net"
15 "os"
16 "os/exec"
David Benjamin884fdf12014-08-02 15:28:23 -040017 "path"
David Benjamin2bc8e6f2014-08-02 15:22:37 -040018 "runtime"
Adam Langley69a01602014-11-17 17:26:55 -080019 "strconv"
Adam Langley95c29f32014-06-20 12:00:00 -070020 "strings"
21 "sync"
22 "syscall"
23)
24
Adam Langley69a01602014-11-17 17:26:55 -080025var (
26 useValgrind = flag.Bool("valgrind", false, "If true, run code under valgrind")
27 useGDB = flag.Bool("gdb", false, "If true, run BoringSSL code under gdb")
28 flagDebug *bool = flag.Bool("debug", false, "Hexdump the contents of the connection")
29 mallocTest *int64 = flag.Int64("malloc-test", -1, "If non-negative, run each test with each malloc in turn failing from the given number onwards.")
30 mallocTestDebug *bool = flag.Bool("malloc-test-debug", false, "If true, ask bssl_shim to abort rather than fail a malloc. This can be used with a specific value for --malloc-test to identity the malloc failing that is causing problems.")
31)
Adam Langley95c29f32014-06-20 12:00:00 -070032
David Benjamin025b3d32014-07-01 19:53:04 -040033const (
34 rsaCertificateFile = "cert.pem"
35 ecdsaCertificateFile = "ecdsa_cert.pem"
36)
37
38const (
David Benjamina08e49d2014-08-24 01:46:07 -040039 rsaKeyFile = "key.pem"
40 ecdsaKeyFile = "ecdsa_key.pem"
41 channelIDKeyFile = "channel_id_key.pem"
David Benjamin025b3d32014-07-01 19:53:04 -040042)
43
Adam Langley95c29f32014-06-20 12:00:00 -070044var rsaCertificate, ecdsaCertificate Certificate
David Benjamina08e49d2014-08-24 01:46:07 -040045var channelIDKey *ecdsa.PrivateKey
46var channelIDBytes []byte
Adam Langley95c29f32014-06-20 12:00:00 -070047
David Benjamin61f95272014-11-25 01:55:35 -050048var testOCSPResponse = []byte{1, 2, 3, 4}
49var testSCTList = []byte{5, 6, 7, 8}
50
Adam Langley95c29f32014-06-20 12:00:00 -070051func initCertificates() {
52 var err error
David Benjamin025b3d32014-07-01 19:53:04 -040053 rsaCertificate, err = LoadX509KeyPair(rsaCertificateFile, rsaKeyFile)
Adam Langley95c29f32014-06-20 12:00:00 -070054 if err != nil {
55 panic(err)
56 }
David Benjamin61f95272014-11-25 01:55:35 -050057 rsaCertificate.OCSPStaple = testOCSPResponse
58 rsaCertificate.SignedCertificateTimestampList = testSCTList
Adam Langley95c29f32014-06-20 12:00:00 -070059
David Benjamin025b3d32014-07-01 19:53:04 -040060 ecdsaCertificate, err = LoadX509KeyPair(ecdsaCertificateFile, ecdsaKeyFile)
Adam Langley95c29f32014-06-20 12:00:00 -070061 if err != nil {
62 panic(err)
63 }
David Benjamin61f95272014-11-25 01:55:35 -050064 ecdsaCertificate.OCSPStaple = testOCSPResponse
65 ecdsaCertificate.SignedCertificateTimestampList = testSCTList
David Benjamina08e49d2014-08-24 01:46:07 -040066
67 channelIDPEMBlock, err := ioutil.ReadFile(channelIDKeyFile)
68 if err != nil {
69 panic(err)
70 }
71 channelIDDERBlock, _ := pem.Decode(channelIDPEMBlock)
72 if channelIDDERBlock.Type != "EC PRIVATE KEY" {
73 panic("bad key type")
74 }
75 channelIDKey, err = x509.ParseECPrivateKey(channelIDDERBlock.Bytes)
76 if err != nil {
77 panic(err)
78 }
79 if channelIDKey.Curve != elliptic.P256() {
80 panic("bad curve")
81 }
82
83 channelIDBytes = make([]byte, 64)
84 writeIntPadded(channelIDBytes[:32], channelIDKey.X)
85 writeIntPadded(channelIDBytes[32:], channelIDKey.Y)
Adam Langley95c29f32014-06-20 12:00:00 -070086}
87
88var certificateOnce sync.Once
89
90func getRSACertificate() Certificate {
91 certificateOnce.Do(initCertificates)
92 return rsaCertificate
93}
94
95func getECDSACertificate() Certificate {
96 certificateOnce.Do(initCertificates)
97 return ecdsaCertificate
98}
99
David Benjamin025b3d32014-07-01 19:53:04 -0400100type testType int
101
102const (
103 clientTest testType = iota
104 serverTest
105)
106
David Benjamin6fd297b2014-08-11 18:43:38 -0400107type protocol int
108
109const (
110 tls protocol = iota
111 dtls
112)
113
David Benjaminfc7b0862014-09-06 13:21:53 -0400114const (
115 alpn = 1
116 npn = 2
117)
118
Adam Langley95c29f32014-06-20 12:00:00 -0700119type testCase struct {
David Benjamin025b3d32014-07-01 19:53:04 -0400120 testType testType
David Benjamin6fd297b2014-08-11 18:43:38 -0400121 protocol protocol
Adam Langley95c29f32014-06-20 12:00:00 -0700122 name string
123 config Config
124 shouldFail bool
125 expectedError string
Adam Langleyac61fa32014-06-23 12:03:11 -0700126 // expectedLocalError, if not empty, contains a substring that must be
127 // found in the local error.
128 expectedLocalError string
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400129 // expectedVersion, if non-zero, specifies the TLS version that must be
130 // negotiated.
131 expectedVersion uint16
David Benjamin01fe8202014-09-24 15:21:44 -0400132 // expectedResumeVersion, if non-zero, specifies the TLS version that
133 // must be negotiated on resumption. If zero, expectedVersion is used.
134 expectedResumeVersion uint16
David Benjamina08e49d2014-08-24 01:46:07 -0400135 // expectChannelID controls whether the connection should have
136 // negotiated a Channel ID with channelIDKey.
137 expectChannelID bool
David Benjaminae2888f2014-09-06 12:58:58 -0400138 // expectedNextProto controls whether the connection should
139 // negotiate a next protocol via NPN or ALPN.
140 expectedNextProto string
David Benjaminfc7b0862014-09-06 13:21:53 -0400141 // expectedNextProtoType, if non-zero, is the expected next
142 // protocol negotiation mechanism.
143 expectedNextProtoType int
David Benjaminca6c8262014-11-15 19:06:08 -0500144 // expectedSRTPProtectionProfile is the DTLS-SRTP profile that
145 // should be negotiated. If zero, none should be negotiated.
146 expectedSRTPProtectionProfile uint16
Adam Langley80842bd2014-06-20 12:00:00 -0700147 // messageLen is the length, in bytes, of the test message that will be
148 // sent.
149 messageLen int
David Benjamin025b3d32014-07-01 19:53:04 -0400150 // certFile is the path to the certificate to use for the server.
151 certFile string
152 // keyFile is the path to the private key to use for the server.
153 keyFile string
David Benjamin1d5c83e2014-07-22 19:20:02 -0400154 // resumeSession controls whether a second connection should be tested
David Benjamin01fe8202014-09-24 15:21:44 -0400155 // which attempts to resume the first session.
David Benjamin1d5c83e2014-07-22 19:20:02 -0400156 resumeSession bool
David Benjamin01fe8202014-09-24 15:21:44 -0400157 // resumeConfig, if not nil, points to a Config to be used on
David Benjaminfe8eb9a2014-11-17 03:19:02 -0500158 // resumption. Unless newSessionsOnResume is set,
159 // SessionTicketKey, ServerSessionCache, and
160 // ClientSessionCache are copied from the initial connection's
161 // config. If nil, the initial connection's config is used.
David Benjamin01fe8202014-09-24 15:21:44 -0400162 resumeConfig *Config
David Benjaminfe8eb9a2014-11-17 03:19:02 -0500163 // newSessionsOnResume, if true, will cause resumeConfig to
164 // use a different session resumption context.
165 newSessionsOnResume bool
David Benjamin98e882e2014-08-08 13:24:34 -0400166 // sendPrefix sends a prefix on the socket before actually performing a
167 // handshake.
168 sendPrefix string
David Benjamine58c4f52014-08-24 03:47:07 -0400169 // shimWritesFirst controls whether the shim sends an initial "hello"
170 // message before doing a roundtrip with the runner.
171 shimWritesFirst bool
Adam Langleycf2d4f42014-10-28 19:06:14 -0700172 // renegotiate indicates the the connection should be renegotiated
173 // during the exchange.
174 renegotiate bool
175 // renegotiateCiphers is a list of ciphersuite ids that will be
176 // switched in just before renegotiation.
177 renegotiateCiphers []uint16
David Benjamin5e961c12014-11-07 01:48:35 -0500178 // replayWrites, if true, configures the underlying transport
179 // to replay every write it makes in DTLS tests.
180 replayWrites bool
David Benjamin325b5c32014-07-01 19:40:31 -0400181 // flags, if not empty, contains a list of command-line flags that will
182 // be passed to the shim program.
183 flags []string
Adam Langley95c29f32014-06-20 12:00:00 -0700184}
185
David Benjamin025b3d32014-07-01 19:53:04 -0400186var testCases = []testCase{
Adam Langley95c29f32014-06-20 12:00:00 -0700187 {
188 name: "BadRSASignature",
189 config: Config{
190 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
191 Bugs: ProtocolBugs{
192 InvalidSKXSignature: true,
193 },
194 },
195 shouldFail: true,
196 expectedError: ":BAD_SIGNATURE:",
197 },
198 {
199 name: "BadECDSASignature",
200 config: Config{
201 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
202 Bugs: ProtocolBugs{
203 InvalidSKXSignature: true,
204 },
205 Certificates: []Certificate{getECDSACertificate()},
206 },
207 shouldFail: true,
208 expectedError: ":BAD_SIGNATURE:",
209 },
210 {
211 name: "BadECDSACurve",
212 config: Config{
213 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
214 Bugs: ProtocolBugs{
215 InvalidSKXCurve: true,
216 },
217 Certificates: []Certificate{getECDSACertificate()},
218 },
219 shouldFail: true,
220 expectedError: ":WRONG_CURVE:",
221 },
Adam Langleyac61fa32014-06-23 12:03:11 -0700222 {
David Benjamina8e3e0e2014-08-06 22:11:10 -0400223 testType: serverTest,
224 name: "BadRSAVersion",
225 config: Config{
226 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
227 Bugs: ProtocolBugs{
228 RsaClientKeyExchangeVersion: VersionTLS11,
229 },
230 },
231 shouldFail: true,
232 expectedError: ":DECRYPTION_FAILED_OR_BAD_RECORD_MAC:",
233 },
234 {
David Benjamin325b5c32014-07-01 19:40:31 -0400235 name: "NoFallbackSCSV",
Adam Langleyac61fa32014-06-23 12:03:11 -0700236 config: Config{
237 Bugs: ProtocolBugs{
238 FailIfNotFallbackSCSV: true,
239 },
240 },
241 shouldFail: true,
242 expectedLocalError: "no fallback SCSV found",
243 },
David Benjamin325b5c32014-07-01 19:40:31 -0400244 {
David Benjamin2a0c4962014-08-22 23:46:35 -0400245 name: "SendFallbackSCSV",
David Benjamin325b5c32014-07-01 19:40:31 -0400246 config: Config{
247 Bugs: ProtocolBugs{
248 FailIfNotFallbackSCSV: true,
249 },
250 },
251 flags: []string{"-fallback-scsv"},
252 },
David Benjamin197b3ab2014-07-02 18:37:33 -0400253 {
David Benjamin7b030512014-07-08 17:30:11 -0400254 name: "ClientCertificateTypes",
255 config: Config{
256 ClientAuth: RequestClientCert,
257 ClientCertificateTypes: []byte{
258 CertTypeDSSSign,
259 CertTypeRSASign,
260 CertTypeECDSASign,
261 },
262 },
David Benjamin2561dc32014-08-24 01:25:27 -0400263 flags: []string{
264 "-expect-certificate-types",
265 base64.StdEncoding.EncodeToString([]byte{
266 CertTypeDSSSign,
267 CertTypeRSASign,
268 CertTypeECDSASign,
269 }),
270 },
David Benjamin7b030512014-07-08 17:30:11 -0400271 },
David Benjamin636293b2014-07-08 17:59:18 -0400272 {
273 name: "NoClientCertificate",
274 config: Config{
275 ClientAuth: RequireAnyClientCert,
276 },
277 shouldFail: true,
278 expectedLocalError: "client didn't provide a certificate",
279 },
David Benjamin1c375dd2014-07-12 00:48:23 -0400280 {
281 name: "UnauthenticatedECDH",
282 config: Config{
283 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
284 Bugs: ProtocolBugs{
285 UnauthenticatedECDH: true,
286 },
287 },
288 shouldFail: true,
David Benjamine8f3d662014-07-12 01:10:19 -0400289 expectedError: ":UNEXPECTED_MESSAGE:",
David Benjamin1c375dd2014-07-12 00:48:23 -0400290 },
David Benjamin9c651c92014-07-12 13:27:45 -0400291 {
292 name: "SkipServerKeyExchange",
293 config: Config{
294 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
295 Bugs: ProtocolBugs{
296 SkipServerKeyExchange: true,
297 },
298 },
299 shouldFail: true,
300 expectedError: ":UNEXPECTED_MESSAGE:",
301 },
David Benjamin1f5f62b2014-07-12 16:18:02 -0400302 {
David Benjamina0e52232014-07-19 17:39:58 -0400303 name: "SkipChangeCipherSpec-Client",
304 config: Config{
305 Bugs: ProtocolBugs{
306 SkipChangeCipherSpec: true,
307 },
308 },
309 shouldFail: true,
David Benjamin86271ee2014-07-21 16:14:03 -0400310 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
David Benjamina0e52232014-07-19 17:39:58 -0400311 },
312 {
313 testType: serverTest,
314 name: "SkipChangeCipherSpec-Server",
315 config: Config{
316 Bugs: ProtocolBugs{
317 SkipChangeCipherSpec: true,
318 },
319 },
320 shouldFail: true,
David Benjamin86271ee2014-07-21 16:14:03 -0400321 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
David Benjamina0e52232014-07-19 17:39:58 -0400322 },
David Benjamin42be6452014-07-21 14:50:23 -0400323 {
324 testType: serverTest,
325 name: "SkipChangeCipherSpec-Server-NPN",
326 config: Config{
327 NextProtos: []string{"bar"},
328 Bugs: ProtocolBugs{
329 SkipChangeCipherSpec: true,
330 },
331 },
332 flags: []string{
333 "-advertise-npn", "\x03foo\x03bar\x03baz",
334 },
335 shouldFail: true,
David Benjamin86271ee2014-07-21 16:14:03 -0400336 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
337 },
338 {
339 name: "FragmentAcrossChangeCipherSpec-Client",
340 config: Config{
341 Bugs: ProtocolBugs{
342 FragmentAcrossChangeCipherSpec: true,
343 },
344 },
345 shouldFail: true,
346 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
347 },
348 {
349 testType: serverTest,
350 name: "FragmentAcrossChangeCipherSpec-Server",
351 config: Config{
352 Bugs: ProtocolBugs{
353 FragmentAcrossChangeCipherSpec: true,
354 },
355 },
356 shouldFail: true,
357 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
358 },
359 {
360 testType: serverTest,
361 name: "FragmentAcrossChangeCipherSpec-Server-NPN",
362 config: Config{
363 NextProtos: []string{"bar"},
364 Bugs: ProtocolBugs{
365 FragmentAcrossChangeCipherSpec: true,
366 },
367 },
368 flags: []string{
369 "-advertise-npn", "\x03foo\x03bar\x03baz",
370 },
371 shouldFail: true,
372 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
David Benjamin42be6452014-07-21 14:50:23 -0400373 },
David Benjaminf3ec83d2014-07-21 22:42:34 -0400374 {
375 testType: serverTest,
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400376 name: "FragmentAlert",
377 config: Config{
378 Bugs: ProtocolBugs{
David Benjaminca6c8262014-11-15 19:06:08 -0500379 FragmentAlert: true,
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400380 SendSpuriousAlert: true,
381 },
382 },
383 shouldFail: true,
384 expectedError: ":BAD_ALERT:",
385 },
386 {
387 testType: serverTest,
David Benjaminf3ec83d2014-07-21 22:42:34 -0400388 name: "EarlyChangeCipherSpec-server-1",
389 config: Config{
390 Bugs: ProtocolBugs{
391 EarlyChangeCipherSpec: 1,
392 },
393 },
394 shouldFail: true,
395 expectedError: ":CCS_RECEIVED_EARLY:",
396 },
397 {
398 testType: serverTest,
399 name: "EarlyChangeCipherSpec-server-2",
400 config: Config{
401 Bugs: ProtocolBugs{
402 EarlyChangeCipherSpec: 2,
403 },
404 },
405 shouldFail: true,
406 expectedError: ":CCS_RECEIVED_EARLY:",
407 },
David Benjamind23f4122014-07-23 15:09:48 -0400408 {
David Benjamind23f4122014-07-23 15:09:48 -0400409 name: "SkipNewSessionTicket",
410 config: Config{
411 Bugs: ProtocolBugs{
412 SkipNewSessionTicket: true,
413 },
414 },
415 shouldFail: true,
416 expectedError: ":CCS_RECEIVED_EARLY:",
417 },
David Benjamin7e3305e2014-07-28 14:52:32 -0400418 {
David Benjamind86c7672014-08-02 04:07:12 -0400419 testType: serverTest,
David Benjaminbef270a2014-08-02 04:22:02 -0400420 name: "FallbackSCSV",
421 config: Config{
422 MaxVersion: VersionTLS11,
423 Bugs: ProtocolBugs{
424 SendFallbackSCSV: true,
425 },
426 },
427 shouldFail: true,
428 expectedError: ":INAPPROPRIATE_FALLBACK:",
429 },
430 {
431 testType: serverTest,
432 name: "FallbackSCSV-VersionMatch",
433 config: Config{
434 Bugs: ProtocolBugs{
435 SendFallbackSCSV: true,
436 },
437 },
438 },
David Benjamin98214542014-08-07 18:02:39 -0400439 {
440 testType: serverTest,
441 name: "FragmentedClientVersion",
442 config: Config{
443 Bugs: ProtocolBugs{
444 MaxHandshakeRecordLength: 1,
445 FragmentClientVersion: true,
446 },
447 },
448 shouldFail: true,
449 expectedError: ":RECORD_TOO_SMALL:",
450 },
David Benjamin98e882e2014-08-08 13:24:34 -0400451 {
452 testType: serverTest,
453 name: "MinorVersionTolerance",
454 config: Config{
455 Bugs: ProtocolBugs{
456 SendClientVersion: 0x03ff,
457 },
458 },
459 expectedVersion: VersionTLS12,
460 },
461 {
462 testType: serverTest,
463 name: "MajorVersionTolerance",
464 config: Config{
465 Bugs: ProtocolBugs{
466 SendClientVersion: 0x0400,
467 },
468 },
469 expectedVersion: VersionTLS12,
470 },
471 {
472 testType: serverTest,
473 name: "VersionTooLow",
474 config: Config{
475 Bugs: ProtocolBugs{
476 SendClientVersion: 0x0200,
477 },
478 },
479 shouldFail: true,
480 expectedError: ":UNSUPPORTED_PROTOCOL:",
481 },
482 {
483 testType: serverTest,
484 name: "HttpGET",
485 sendPrefix: "GET / HTTP/1.0\n",
486 shouldFail: true,
487 expectedError: ":HTTP_REQUEST:",
488 },
489 {
490 testType: serverTest,
491 name: "HttpPOST",
492 sendPrefix: "POST / HTTP/1.0\n",
493 shouldFail: true,
494 expectedError: ":HTTP_REQUEST:",
495 },
496 {
497 testType: serverTest,
498 name: "HttpHEAD",
499 sendPrefix: "HEAD / HTTP/1.0\n",
500 shouldFail: true,
501 expectedError: ":HTTP_REQUEST:",
502 },
503 {
504 testType: serverTest,
505 name: "HttpPUT",
506 sendPrefix: "PUT / HTTP/1.0\n",
507 shouldFail: true,
508 expectedError: ":HTTP_REQUEST:",
509 },
510 {
511 testType: serverTest,
512 name: "HttpCONNECT",
513 sendPrefix: "CONNECT www.google.com:443 HTTP/1.0\n",
514 shouldFail: true,
515 expectedError: ":HTTPS_PROXY_REQUEST:",
516 },
David Benjamin39ebf532014-08-31 02:23:49 -0400517 {
518 name: "SkipCipherVersionCheck",
519 config: Config{
520 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
521 MaxVersion: VersionTLS11,
522 Bugs: ProtocolBugs{
523 SkipCipherVersionCheck: true,
524 },
525 },
526 shouldFail: true,
527 expectedError: ":WRONG_CIPHER_RETURNED:",
528 },
David Benjamin9114fae2014-11-08 11:41:14 -0500529 {
530 name: "RSAServerKeyExchange",
531 config: Config{
532 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
533 Bugs: ProtocolBugs{
534 RSAServerKeyExchange: true,
535 },
536 },
537 shouldFail: true,
538 expectedError: ":UNEXPECTED_MESSAGE:",
539 },
David Benjamin128dbc32014-12-01 01:27:42 -0500540 {
541 name: "DisableEverything",
542 flags: []string{"-no-tls12", "-no-tls11", "-no-tls1", "-no-ssl3"},
543 shouldFail: true,
544 expectedError: ":WRONG_SSL_VERSION:",
545 },
546 {
547 protocol: dtls,
548 name: "DisableEverything-DTLS",
549 flags: []string{"-no-tls12", "-no-tls1"},
550 shouldFail: true,
551 expectedError: ":WRONG_SSL_VERSION:",
552 },
Adam Langley95c29f32014-06-20 12:00:00 -0700553}
554
David Benjamin01fe8202014-09-24 15:21:44 -0400555func doExchange(test *testCase, config *Config, conn net.Conn, messageLen int, isResume bool) error {
David Benjamin65ea8ff2014-11-23 03:01:00 -0500556 var connDebug *recordingConn
557 if *flagDebug {
558 connDebug = &recordingConn{Conn: conn}
559 conn = connDebug
560 defer func() {
561 connDebug.WriteTo(os.Stdout)
562 }()
563 }
564
David Benjamin6fd297b2014-08-11 18:43:38 -0400565 if test.protocol == dtls {
566 conn = newPacketAdaptor(conn)
David Benjamin5e961c12014-11-07 01:48:35 -0500567 if test.replayWrites {
568 conn = newReplayAdaptor(conn)
569 }
David Benjamin6fd297b2014-08-11 18:43:38 -0400570 }
571
572 if test.sendPrefix != "" {
573 if _, err := conn.Write([]byte(test.sendPrefix)); err != nil {
574 return err
575 }
David Benjamin98e882e2014-08-08 13:24:34 -0400576 }
577
David Benjamin1d5c83e2014-07-22 19:20:02 -0400578 var tlsConn *Conn
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400579 if test.testType == clientTest {
David Benjamin6fd297b2014-08-11 18:43:38 -0400580 if test.protocol == dtls {
581 tlsConn = DTLSServer(conn, config)
582 } else {
583 tlsConn = Server(conn, config)
584 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400585 } else {
586 config.InsecureSkipVerify = true
David Benjamin6fd297b2014-08-11 18:43:38 -0400587 if test.protocol == dtls {
588 tlsConn = DTLSClient(conn, config)
589 } else {
590 tlsConn = Client(conn, config)
591 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400592 }
593
Adam Langley95c29f32014-06-20 12:00:00 -0700594 if err := tlsConn.Handshake(); err != nil {
595 return err
596 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700597
David Benjamin01fe8202014-09-24 15:21:44 -0400598 // TODO(davidben): move all per-connection expectations into a dedicated
599 // expectations struct that can be specified separately for the two
600 // legs.
601 expectedVersion := test.expectedVersion
602 if isResume && test.expectedResumeVersion != 0 {
603 expectedVersion = test.expectedResumeVersion
604 }
605 if vers := tlsConn.ConnectionState().Version; expectedVersion != 0 && vers != expectedVersion {
606 return fmt.Errorf("got version %x, expected %x", vers, expectedVersion)
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400607 }
608
David Benjamina08e49d2014-08-24 01:46:07 -0400609 if test.expectChannelID {
610 channelID := tlsConn.ConnectionState().ChannelID
611 if channelID == nil {
612 return fmt.Errorf("no channel ID negotiated")
613 }
614 if channelID.Curve != channelIDKey.Curve ||
615 channelIDKey.X.Cmp(channelIDKey.X) != 0 ||
616 channelIDKey.Y.Cmp(channelIDKey.Y) != 0 {
617 return fmt.Errorf("incorrect channel ID")
618 }
619 }
620
David Benjaminae2888f2014-09-06 12:58:58 -0400621 if expected := test.expectedNextProto; expected != "" {
622 if actual := tlsConn.ConnectionState().NegotiatedProtocol; actual != expected {
623 return fmt.Errorf("next proto mismatch: got %s, wanted %s", actual, expected)
624 }
625 }
626
David Benjaminfc7b0862014-09-06 13:21:53 -0400627 if test.expectedNextProtoType != 0 {
628 if (test.expectedNextProtoType == alpn) != tlsConn.ConnectionState().NegotiatedProtocolFromALPN {
629 return fmt.Errorf("next proto type mismatch")
630 }
631 }
632
David Benjaminca6c8262014-11-15 19:06:08 -0500633 if p := tlsConn.ConnectionState().SRTPProtectionProfile; p != test.expectedSRTPProtectionProfile {
634 return fmt.Errorf("SRTP profile mismatch: got %d, wanted %d", p, test.expectedSRTPProtectionProfile)
635 }
636
David Benjamine58c4f52014-08-24 03:47:07 -0400637 if test.shimWritesFirst {
638 var buf [5]byte
639 _, err := io.ReadFull(tlsConn, buf[:])
640 if err != nil {
641 return err
642 }
643 if string(buf[:]) != "hello" {
644 return fmt.Errorf("bad initial message")
645 }
646 }
647
Adam Langleycf2d4f42014-10-28 19:06:14 -0700648 if test.renegotiate {
649 if test.renegotiateCiphers != nil {
650 config.CipherSuites = test.renegotiateCiphers
651 }
652 if err := tlsConn.Renegotiate(); err != nil {
653 return err
654 }
655 } else if test.renegotiateCiphers != nil {
656 panic("renegotiateCiphers without renegotiate")
657 }
658
Kenny Root7fdeaf12014-08-05 15:23:37 -0700659 if messageLen < 0 {
David Benjamin6fd297b2014-08-11 18:43:38 -0400660 if test.protocol == dtls {
661 return fmt.Errorf("messageLen < 0 not supported for DTLS tests")
662 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700663 // Read until EOF.
664 _, err := io.Copy(ioutil.Discard, tlsConn)
665 return err
666 }
667
Adam Langley80842bd2014-06-20 12:00:00 -0700668 if messageLen == 0 {
669 messageLen = 32
670 }
671 testMessage := make([]byte, messageLen)
672 for i := range testMessage {
673 testMessage[i] = 0x42
674 }
Adam Langley95c29f32014-06-20 12:00:00 -0700675 tlsConn.Write(testMessage)
676
677 buf := make([]byte, len(testMessage))
David Benjamin6fd297b2014-08-11 18:43:38 -0400678 if test.protocol == dtls {
679 bufTmp := make([]byte, len(buf)+1)
680 n, err := tlsConn.Read(bufTmp)
681 if err != nil {
682 return err
683 }
684 if n != len(buf) {
685 return fmt.Errorf("bad reply; length mismatch (%d vs %d)", n, len(buf))
686 }
687 copy(buf, bufTmp)
688 } else {
689 _, err := io.ReadFull(tlsConn, buf)
690 if err != nil {
691 return err
692 }
Adam Langley95c29f32014-06-20 12:00:00 -0700693 }
694
695 for i, v := range buf {
696 if v != testMessage[i]^0xff {
697 return fmt.Errorf("bad reply contents at byte %d", i)
698 }
699 }
700
701 return nil
702}
703
David Benjamin325b5c32014-07-01 19:40:31 -0400704func valgrindOf(dbAttach bool, path string, args ...string) *exec.Cmd {
705 valgrindArgs := []string{"--error-exitcode=99", "--track-origins=yes", "--leak-check=full"}
Adam Langley95c29f32014-06-20 12:00:00 -0700706 if dbAttach {
David Benjamin325b5c32014-07-01 19:40:31 -0400707 valgrindArgs = append(valgrindArgs, "--db-attach=yes", "--db-command=xterm -e gdb -nw %f %p")
Adam Langley95c29f32014-06-20 12:00:00 -0700708 }
David Benjamin325b5c32014-07-01 19:40:31 -0400709 valgrindArgs = append(valgrindArgs, path)
710 valgrindArgs = append(valgrindArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -0700711
David Benjamin325b5c32014-07-01 19:40:31 -0400712 return exec.Command("valgrind", valgrindArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -0700713}
714
David Benjamin325b5c32014-07-01 19:40:31 -0400715func gdbOf(path string, args ...string) *exec.Cmd {
716 xtermArgs := []string{"-e", "gdb", "--args"}
717 xtermArgs = append(xtermArgs, path)
718 xtermArgs = append(xtermArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -0700719
David Benjamin325b5c32014-07-01 19:40:31 -0400720 return exec.Command("xterm", xtermArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -0700721}
722
David Benjamin1d5c83e2014-07-22 19:20:02 -0400723func openSocketPair() (shimEnd *os.File, conn net.Conn) {
Adam Langley95c29f32014-06-20 12:00:00 -0700724 socks, err := syscall.Socketpair(syscall.AF_UNIX, syscall.SOCK_STREAM, 0)
725 if err != nil {
726 panic(err)
727 }
728
729 syscall.CloseOnExec(socks[0])
730 syscall.CloseOnExec(socks[1])
David Benjamin1d5c83e2014-07-22 19:20:02 -0400731 shimEnd = os.NewFile(uintptr(socks[0]), "shim end")
Adam Langley95c29f32014-06-20 12:00:00 -0700732 connFile := os.NewFile(uintptr(socks[1]), "our end")
David Benjamin1d5c83e2014-07-22 19:20:02 -0400733 conn, err = net.FileConn(connFile)
734 if err != nil {
735 panic(err)
736 }
Adam Langley95c29f32014-06-20 12:00:00 -0700737 connFile.Close()
738 if err != nil {
739 panic(err)
740 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400741 return shimEnd, conn
742}
743
Adam Langley69a01602014-11-17 17:26:55 -0800744type moreMallocsError struct{}
745
746func (moreMallocsError) Error() string {
747 return "child process did not exhaust all allocation calls"
748}
749
750var errMoreMallocs = moreMallocsError{}
751
752func runTest(test *testCase, buildDir string, mallocNumToFail int64) error {
Adam Langley38311732014-10-16 19:04:35 -0700753 if !test.shouldFail && (len(test.expectedError) > 0 || len(test.expectedLocalError) > 0) {
754 panic("Error expected without shouldFail in " + test.name)
755 }
756
David Benjamin1d5c83e2014-07-22 19:20:02 -0400757 shimEnd, conn := openSocketPair()
758 shimEndResume, connResume := openSocketPair()
Adam Langley95c29f32014-06-20 12:00:00 -0700759
David Benjamin884fdf12014-08-02 15:28:23 -0400760 shim_path := path.Join(buildDir, "ssl/test/bssl_shim")
David Benjamin5a593af2014-08-11 19:51:50 -0400761 var flags []string
David Benjamin1d5c83e2014-07-22 19:20:02 -0400762 if test.testType == serverTest {
David Benjamin5a593af2014-08-11 19:51:50 -0400763 flags = append(flags, "-server")
764
David Benjamin025b3d32014-07-01 19:53:04 -0400765 flags = append(flags, "-key-file")
766 if test.keyFile == "" {
767 flags = append(flags, rsaKeyFile)
768 } else {
769 flags = append(flags, test.keyFile)
770 }
771
772 flags = append(flags, "-cert-file")
773 if test.certFile == "" {
774 flags = append(flags, rsaCertificateFile)
775 } else {
776 flags = append(flags, test.certFile)
777 }
778 }
David Benjamin5a593af2014-08-11 19:51:50 -0400779
David Benjamin6fd297b2014-08-11 18:43:38 -0400780 if test.protocol == dtls {
781 flags = append(flags, "-dtls")
782 }
783
David Benjamin5a593af2014-08-11 19:51:50 -0400784 if test.resumeSession {
785 flags = append(flags, "-resume")
786 }
787
David Benjamine58c4f52014-08-24 03:47:07 -0400788 if test.shimWritesFirst {
789 flags = append(flags, "-shim-writes-first")
790 }
791
David Benjamin025b3d32014-07-01 19:53:04 -0400792 flags = append(flags, test.flags...)
793
794 var shim *exec.Cmd
795 if *useValgrind {
796 shim = valgrindOf(false, shim_path, flags...)
Adam Langley75712922014-10-10 16:23:43 -0700797 } else if *useGDB {
798 shim = gdbOf(shim_path, flags...)
David Benjamin025b3d32014-07-01 19:53:04 -0400799 } else {
800 shim = exec.Command(shim_path, flags...)
801 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400802 shim.ExtraFiles = []*os.File{shimEnd, shimEndResume}
David Benjamin025b3d32014-07-01 19:53:04 -0400803 shim.Stdin = os.Stdin
804 var stdoutBuf, stderrBuf bytes.Buffer
805 shim.Stdout = &stdoutBuf
806 shim.Stderr = &stderrBuf
Adam Langley69a01602014-11-17 17:26:55 -0800807 if mallocNumToFail >= 0 {
808 shim.Env = []string{"MALLOC_NUMBER_TO_FAIL=" + strconv.FormatInt(mallocNumToFail, 10)}
809 if *mallocTestDebug {
810 shim.Env = append(shim.Env, "MALLOC_ABORT_ON_FAIL=1")
811 }
812 shim.Env = append(shim.Env, "_MALLOC_CHECK=1")
813 }
David Benjamin025b3d32014-07-01 19:53:04 -0400814
815 if err := shim.Start(); err != nil {
Adam Langley95c29f32014-06-20 12:00:00 -0700816 panic(err)
817 }
David Benjamin025b3d32014-07-01 19:53:04 -0400818 shimEnd.Close()
David Benjamin1d5c83e2014-07-22 19:20:02 -0400819 shimEndResume.Close()
Adam Langley95c29f32014-06-20 12:00:00 -0700820
821 config := test.config
David Benjamin1d5c83e2014-07-22 19:20:02 -0400822 config.ClientSessionCache = NewLRUClientSessionCache(1)
David Benjaminfe8eb9a2014-11-17 03:19:02 -0500823 config.ServerSessionCache = NewLRUServerSessionCache(1)
David Benjamin025b3d32014-07-01 19:53:04 -0400824 if test.testType == clientTest {
825 if len(config.Certificates) == 0 {
826 config.Certificates = []Certificate{getRSACertificate()}
827 }
David Benjamin025b3d32014-07-01 19:53:04 -0400828 }
Adam Langley95c29f32014-06-20 12:00:00 -0700829
David Benjamin01fe8202014-09-24 15:21:44 -0400830 err := doExchange(test, &config, conn, test.messageLen,
831 false /* not a resumption */)
Adam Langley95c29f32014-06-20 12:00:00 -0700832 conn.Close()
David Benjamin65ea8ff2014-11-23 03:01:00 -0500833
David Benjamin1d5c83e2014-07-22 19:20:02 -0400834 if err == nil && test.resumeSession {
David Benjamin01fe8202014-09-24 15:21:44 -0400835 var resumeConfig Config
836 if test.resumeConfig != nil {
837 resumeConfig = *test.resumeConfig
838 if len(resumeConfig.Certificates) == 0 {
839 resumeConfig.Certificates = []Certificate{getRSACertificate()}
840 }
David Benjaminfe8eb9a2014-11-17 03:19:02 -0500841 if !test.newSessionsOnResume {
842 resumeConfig.SessionTicketKey = config.SessionTicketKey
843 resumeConfig.ClientSessionCache = config.ClientSessionCache
844 resumeConfig.ServerSessionCache = config.ServerSessionCache
845 }
David Benjamin01fe8202014-09-24 15:21:44 -0400846 } else {
847 resumeConfig = config
848 }
849 err = doExchange(test, &resumeConfig, connResume, test.messageLen,
850 true /* resumption */)
David Benjamin1d5c83e2014-07-22 19:20:02 -0400851 }
David Benjamin812152a2014-09-06 12:49:07 -0400852 connResume.Close()
David Benjamin1d5c83e2014-07-22 19:20:02 -0400853
David Benjamin025b3d32014-07-01 19:53:04 -0400854 childErr := shim.Wait()
Adam Langley69a01602014-11-17 17:26:55 -0800855 if exitError, ok := childErr.(*exec.ExitError); ok {
856 if exitError.Sys().(syscall.WaitStatus).ExitStatus() == 88 {
857 return errMoreMallocs
858 }
859 }
Adam Langley95c29f32014-06-20 12:00:00 -0700860
861 stdout := string(stdoutBuf.Bytes())
862 stderr := string(stderrBuf.Bytes())
863 failed := err != nil || childErr != nil
864 correctFailure := len(test.expectedError) == 0 || strings.Contains(stdout, test.expectedError)
Adam Langleyac61fa32014-06-23 12:03:11 -0700865 localError := "none"
866 if err != nil {
867 localError = err.Error()
868 }
869 if len(test.expectedLocalError) != 0 {
870 correctFailure = correctFailure && strings.Contains(localError, test.expectedLocalError)
871 }
Adam Langley95c29f32014-06-20 12:00:00 -0700872
873 if failed != test.shouldFail || failed && !correctFailure {
Adam Langley95c29f32014-06-20 12:00:00 -0700874 childError := "none"
Adam Langley95c29f32014-06-20 12:00:00 -0700875 if childErr != nil {
876 childError = childErr.Error()
877 }
878
879 var msg string
880 switch {
881 case failed && !test.shouldFail:
882 msg = "unexpected failure"
883 case !failed && test.shouldFail:
884 msg = "unexpected success"
885 case failed && !correctFailure:
Adam Langleyac61fa32014-06-23 12:03:11 -0700886 msg = "bad error (wanted '" + test.expectedError + "' / '" + test.expectedLocalError + "')"
Adam Langley95c29f32014-06-20 12:00:00 -0700887 default:
888 panic("internal error")
889 }
890
891 return fmt.Errorf("%s: local error '%s', child error '%s', stdout:\n%s\nstderr:\n%s", msg, localError, childError, string(stdoutBuf.Bytes()), stderr)
892 }
893
894 if !*useValgrind && len(stderr) > 0 {
895 println(stderr)
896 }
897
898 return nil
899}
900
901var tlsVersions = []struct {
902 name string
903 version uint16
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400904 flag string
David Benjamin8b8c0062014-11-23 02:47:52 -0500905 hasDTLS bool
Adam Langley95c29f32014-06-20 12:00:00 -0700906}{
David Benjamin8b8c0062014-11-23 02:47:52 -0500907 {"SSL3", VersionSSL30, "-no-ssl3", false},
908 {"TLS1", VersionTLS10, "-no-tls1", true},
909 {"TLS11", VersionTLS11, "-no-tls11", false},
910 {"TLS12", VersionTLS12, "-no-tls12", true},
Adam Langley95c29f32014-06-20 12:00:00 -0700911}
912
913var testCipherSuites = []struct {
914 name string
915 id uint16
916}{
917 {"3DES-SHA", TLS_RSA_WITH_3DES_EDE_CBC_SHA},
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400918 {"AES128-GCM", TLS_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -0700919 {"AES128-SHA", TLS_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -0400920 {"AES128-SHA256", TLS_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400921 {"AES256-GCM", TLS_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -0700922 {"AES256-SHA", TLS_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -0400923 {"AES256-SHA256", TLS_RSA_WITH_AES_256_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400924 {"DHE-RSA-AES128-GCM", TLS_DHE_RSA_WITH_AES_128_GCM_SHA256},
925 {"DHE-RSA-AES128-SHA", TLS_DHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -0400926 {"DHE-RSA-AES128-SHA256", TLS_DHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400927 {"DHE-RSA-AES256-GCM", TLS_DHE_RSA_WITH_AES_256_GCM_SHA384},
928 {"DHE-RSA-AES256-SHA", TLS_DHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -0400929 {"DHE-RSA-AES256-SHA256", TLS_DHE_RSA_WITH_AES_256_CBC_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -0700930 {"ECDHE-ECDSA-AES128-GCM", TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
931 {"ECDHE-ECDSA-AES128-SHA", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -0400932 {"ECDHE-ECDSA-AES128-SHA256", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256},
933 {"ECDHE-ECDSA-AES256-GCM", TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -0700934 {"ECDHE-ECDSA-AES256-SHA", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -0400935 {"ECDHE-ECDSA-AES256-SHA384", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -0700936 {"ECDHE-ECDSA-RC4-SHA", TLS_ECDHE_ECDSA_WITH_RC4_128_SHA},
David Benjamin2af684f2014-10-27 02:23:15 -0400937 {"ECDHE-PSK-WITH-AES-128-GCM-SHA256", TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -0700938 {"ECDHE-RSA-AES128-GCM", TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -0700939 {"ECDHE-RSA-AES128-SHA", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -0400940 {"ECDHE-RSA-AES128-SHA256", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400941 {"ECDHE-RSA-AES256-GCM", TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -0700942 {"ECDHE-RSA-AES256-SHA", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -0400943 {"ECDHE-RSA-AES256-SHA384", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -0700944 {"ECDHE-RSA-RC4-SHA", TLS_ECDHE_RSA_WITH_RC4_128_SHA},
David Benjamin48cae082014-10-27 01:06:24 -0400945 {"PSK-AES128-CBC-SHA", TLS_PSK_WITH_AES_128_CBC_SHA},
946 {"PSK-AES256-CBC-SHA", TLS_PSK_WITH_AES_256_CBC_SHA},
947 {"PSK-RC4-SHA", TLS_PSK_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -0700948 {"RC4-MD5", TLS_RSA_WITH_RC4_128_MD5},
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400949 {"RC4-SHA", TLS_RSA_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -0700950}
951
David Benjamin8b8c0062014-11-23 02:47:52 -0500952func hasComponent(suiteName, component string) bool {
953 return strings.Contains("-"+suiteName+"-", "-"+component+"-")
954}
955
David Benjaminf7768e42014-08-31 02:06:47 -0400956func isTLS12Only(suiteName string) bool {
David Benjamin8b8c0062014-11-23 02:47:52 -0500957 return hasComponent(suiteName, "GCM") ||
958 hasComponent(suiteName, "SHA256") ||
959 hasComponent(suiteName, "SHA384")
960}
961
962func isDTLSCipher(suiteName string) bool {
963 // TODO(davidben): AES-GCM exists in DTLS 1.2 but is currently
964 // broken because DTLS is not EVP_AEAD-aware.
965 return !hasComponent(suiteName, "RC4") &&
966 !hasComponent(suiteName, "GCM")
David Benjaminf7768e42014-08-31 02:06:47 -0400967}
968
Adam Langley95c29f32014-06-20 12:00:00 -0700969func addCipherSuiteTests() {
970 for _, suite := range testCipherSuites {
David Benjamin48cae082014-10-27 01:06:24 -0400971 const psk = "12345"
972 const pskIdentity = "luggage combo"
973
Adam Langley95c29f32014-06-20 12:00:00 -0700974 var cert Certificate
David Benjamin025b3d32014-07-01 19:53:04 -0400975 var certFile string
976 var keyFile string
David Benjamin8b8c0062014-11-23 02:47:52 -0500977 if hasComponent(suite.name, "ECDSA") {
Adam Langley95c29f32014-06-20 12:00:00 -0700978 cert = getECDSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -0400979 certFile = ecdsaCertificateFile
980 keyFile = ecdsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -0700981 } else {
982 cert = getRSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -0400983 certFile = rsaCertificateFile
984 keyFile = rsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -0700985 }
986
David Benjamin48cae082014-10-27 01:06:24 -0400987 var flags []string
David Benjamin8b8c0062014-11-23 02:47:52 -0500988 if hasComponent(suite.name, "PSK") {
David Benjamin48cae082014-10-27 01:06:24 -0400989 flags = append(flags,
990 "-psk", psk,
991 "-psk-identity", pskIdentity)
992 }
993
Adam Langley95c29f32014-06-20 12:00:00 -0700994 for _, ver := range tlsVersions {
David Benjaminf7768e42014-08-31 02:06:47 -0400995 if ver.version < VersionTLS12 && isTLS12Only(suite.name) {
Adam Langley95c29f32014-06-20 12:00:00 -0700996 continue
997 }
998
David Benjamin025b3d32014-07-01 19:53:04 -0400999 testCases = append(testCases, testCase{
1000 testType: clientTest,
1001 name: ver.name + "-" + suite.name + "-client",
Adam Langley95c29f32014-06-20 12:00:00 -07001002 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001003 MinVersion: ver.version,
1004 MaxVersion: ver.version,
1005 CipherSuites: []uint16{suite.id},
1006 Certificates: []Certificate{cert},
1007 PreSharedKey: []byte(psk),
1008 PreSharedKeyIdentity: pskIdentity,
Adam Langley95c29f32014-06-20 12:00:00 -07001009 },
David Benjamin48cae082014-10-27 01:06:24 -04001010 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001011 resumeSession: true,
Adam Langley95c29f32014-06-20 12:00:00 -07001012 })
David Benjamin025b3d32014-07-01 19:53:04 -04001013
David Benjamin76d8abe2014-08-14 16:25:34 -04001014 testCases = append(testCases, testCase{
1015 testType: serverTest,
1016 name: ver.name + "-" + suite.name + "-server",
1017 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001018 MinVersion: ver.version,
1019 MaxVersion: ver.version,
1020 CipherSuites: []uint16{suite.id},
1021 Certificates: []Certificate{cert},
1022 PreSharedKey: []byte(psk),
1023 PreSharedKeyIdentity: pskIdentity,
David Benjamin76d8abe2014-08-14 16:25:34 -04001024 },
1025 certFile: certFile,
1026 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001027 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001028 resumeSession: true,
David Benjamin76d8abe2014-08-14 16:25:34 -04001029 })
David Benjamin6fd297b2014-08-11 18:43:38 -04001030
David Benjamin8b8c0062014-11-23 02:47:52 -05001031 if ver.hasDTLS && isDTLSCipher(suite.name) {
David Benjamin6fd297b2014-08-11 18:43:38 -04001032 testCases = append(testCases, testCase{
1033 testType: clientTest,
1034 protocol: dtls,
1035 name: "D" + ver.name + "-" + suite.name + "-client",
1036 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001037 MinVersion: ver.version,
1038 MaxVersion: ver.version,
1039 CipherSuites: []uint16{suite.id},
1040 Certificates: []Certificate{cert},
1041 PreSharedKey: []byte(psk),
1042 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001043 },
David Benjamin48cae082014-10-27 01:06:24 -04001044 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001045 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001046 })
1047 testCases = append(testCases, testCase{
1048 testType: serverTest,
1049 protocol: dtls,
1050 name: "D" + ver.name + "-" + suite.name + "-server",
1051 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001052 MinVersion: ver.version,
1053 MaxVersion: ver.version,
1054 CipherSuites: []uint16{suite.id},
1055 Certificates: []Certificate{cert},
1056 PreSharedKey: []byte(psk),
1057 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001058 },
1059 certFile: certFile,
1060 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001061 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001062 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001063 })
1064 }
Adam Langley95c29f32014-06-20 12:00:00 -07001065 }
1066 }
1067}
1068
1069func addBadECDSASignatureTests() {
1070 for badR := BadValue(1); badR < NumBadValues; badR++ {
1071 for badS := BadValue(1); badS < NumBadValues; badS++ {
David Benjamin025b3d32014-07-01 19:53:04 -04001072 testCases = append(testCases, testCase{
Adam Langley95c29f32014-06-20 12:00:00 -07001073 name: fmt.Sprintf("BadECDSA-%d-%d", badR, badS),
1074 config: Config{
1075 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
1076 Certificates: []Certificate{getECDSACertificate()},
1077 Bugs: ProtocolBugs{
1078 BadECDSAR: badR,
1079 BadECDSAS: badS,
1080 },
1081 },
1082 shouldFail: true,
1083 expectedError: "SIGNATURE",
1084 })
1085 }
1086 }
1087}
1088
Adam Langley80842bd2014-06-20 12:00:00 -07001089func addCBCPaddingTests() {
David Benjamin025b3d32014-07-01 19:53:04 -04001090 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001091 name: "MaxCBCPadding",
1092 config: Config{
1093 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1094 Bugs: ProtocolBugs{
1095 MaxPadding: true,
1096 },
1097 },
1098 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1099 })
David Benjamin025b3d32014-07-01 19:53:04 -04001100 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001101 name: "BadCBCPadding",
1102 config: Config{
1103 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1104 Bugs: ProtocolBugs{
1105 PaddingFirstByteBad: true,
1106 },
1107 },
1108 shouldFail: true,
1109 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1110 })
1111 // OpenSSL previously had an issue where the first byte of padding in
1112 // 255 bytes of padding wasn't checked.
David Benjamin025b3d32014-07-01 19:53:04 -04001113 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001114 name: "BadCBCPadding255",
1115 config: Config{
1116 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1117 Bugs: ProtocolBugs{
1118 MaxPadding: true,
1119 PaddingFirstByteBadIf255: true,
1120 },
1121 },
1122 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1123 shouldFail: true,
1124 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1125 })
1126}
1127
Kenny Root7fdeaf12014-08-05 15:23:37 -07001128func addCBCSplittingTests() {
1129 testCases = append(testCases, testCase{
1130 name: "CBCRecordSplitting",
1131 config: Config{
1132 MaxVersion: VersionTLS10,
1133 MinVersion: VersionTLS10,
1134 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1135 },
1136 messageLen: -1, // read until EOF
1137 flags: []string{
1138 "-async",
1139 "-write-different-record-sizes",
1140 "-cbc-record-splitting",
1141 },
David Benjamina8e3e0e2014-08-06 22:11:10 -04001142 })
1143 testCases = append(testCases, testCase{
Kenny Root7fdeaf12014-08-05 15:23:37 -07001144 name: "CBCRecordSplittingPartialWrite",
1145 config: Config{
1146 MaxVersion: VersionTLS10,
1147 MinVersion: VersionTLS10,
1148 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1149 },
1150 messageLen: -1, // read until EOF
1151 flags: []string{
1152 "-async",
1153 "-write-different-record-sizes",
1154 "-cbc-record-splitting",
1155 "-partial-write",
1156 },
1157 })
1158}
1159
David Benjamin636293b2014-07-08 17:59:18 -04001160func addClientAuthTests() {
David Benjamin407a10c2014-07-16 12:58:59 -04001161 // Add a dummy cert pool to stress certificate authority parsing.
1162 // TODO(davidben): Add tests that those values parse out correctly.
1163 certPool := x509.NewCertPool()
1164 cert, err := x509.ParseCertificate(rsaCertificate.Certificate[0])
1165 if err != nil {
1166 panic(err)
1167 }
1168 certPool.AddCert(cert)
1169
David Benjamin636293b2014-07-08 17:59:18 -04001170 for _, ver := range tlsVersions {
David Benjamin636293b2014-07-08 17:59:18 -04001171 testCases = append(testCases, testCase{
1172 testType: clientTest,
David Benjamin67666e72014-07-12 15:47:52 -04001173 name: ver.name + "-Client-ClientAuth-RSA",
David Benjamin636293b2014-07-08 17:59:18 -04001174 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001175 MinVersion: ver.version,
1176 MaxVersion: ver.version,
1177 ClientAuth: RequireAnyClientCert,
1178 ClientCAs: certPool,
David Benjamin636293b2014-07-08 17:59:18 -04001179 },
1180 flags: []string{
1181 "-cert-file", rsaCertificateFile,
1182 "-key-file", rsaKeyFile,
1183 },
1184 })
1185 testCases = append(testCases, testCase{
David Benjamin67666e72014-07-12 15:47:52 -04001186 testType: serverTest,
1187 name: ver.name + "-Server-ClientAuth-RSA",
1188 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001189 MinVersion: ver.version,
1190 MaxVersion: ver.version,
David Benjamin67666e72014-07-12 15:47:52 -04001191 Certificates: []Certificate{rsaCertificate},
1192 },
1193 flags: []string{"-require-any-client-certificate"},
1194 })
David Benjamine098ec22014-08-27 23:13:20 -04001195 if ver.version != VersionSSL30 {
1196 testCases = append(testCases, testCase{
1197 testType: serverTest,
1198 name: ver.name + "-Server-ClientAuth-ECDSA",
1199 config: Config{
1200 MinVersion: ver.version,
1201 MaxVersion: ver.version,
1202 Certificates: []Certificate{ecdsaCertificate},
1203 },
1204 flags: []string{"-require-any-client-certificate"},
1205 })
1206 testCases = append(testCases, testCase{
1207 testType: clientTest,
1208 name: ver.name + "-Client-ClientAuth-ECDSA",
1209 config: Config{
1210 MinVersion: ver.version,
1211 MaxVersion: ver.version,
1212 ClientAuth: RequireAnyClientCert,
1213 ClientCAs: certPool,
1214 },
1215 flags: []string{
1216 "-cert-file", ecdsaCertificateFile,
1217 "-key-file", ecdsaKeyFile,
1218 },
1219 })
1220 }
David Benjamin636293b2014-07-08 17:59:18 -04001221 }
1222}
1223
Adam Langley75712922014-10-10 16:23:43 -07001224func addExtendedMasterSecretTests() {
1225 const expectEMSFlag = "-expect-extended-master-secret"
1226
1227 for _, with := range []bool{false, true} {
1228 prefix := "No"
1229 var flags []string
1230 if with {
1231 prefix = ""
1232 flags = []string{expectEMSFlag}
1233 }
1234
1235 for _, isClient := range []bool{false, true} {
1236 suffix := "-Server"
1237 testType := serverTest
1238 if isClient {
1239 suffix = "-Client"
1240 testType = clientTest
1241 }
1242
1243 for _, ver := range tlsVersions {
1244 test := testCase{
1245 testType: testType,
1246 name: prefix + "ExtendedMasterSecret-" + ver.name + suffix,
1247 config: Config{
1248 MinVersion: ver.version,
1249 MaxVersion: ver.version,
1250 Bugs: ProtocolBugs{
1251 NoExtendedMasterSecret: !with,
1252 RequireExtendedMasterSecret: with,
1253 },
1254 },
David Benjamin48cae082014-10-27 01:06:24 -04001255 flags: flags,
1256 shouldFail: ver.version == VersionSSL30 && with,
Adam Langley75712922014-10-10 16:23:43 -07001257 }
1258 if test.shouldFail {
1259 test.expectedLocalError = "extended master secret required but not supported by peer"
1260 }
1261 testCases = append(testCases, test)
1262 }
1263 }
1264 }
1265
1266 // When a session is resumed, it should still be aware that its master
1267 // secret was generated via EMS and thus it's safe to use tls-unique.
1268 testCases = append(testCases, testCase{
1269 name: "ExtendedMasterSecret-Resume",
1270 config: Config{
1271 Bugs: ProtocolBugs{
1272 RequireExtendedMasterSecret: true,
1273 },
1274 },
1275 flags: []string{expectEMSFlag},
1276 resumeSession: true,
1277 })
1278}
1279
David Benjamin43ec06f2014-08-05 02:28:57 -04001280// Adds tests that try to cover the range of the handshake state machine, under
1281// various conditions. Some of these are redundant with other tests, but they
1282// only cover the synchronous case.
David Benjamin6fd297b2014-08-11 18:43:38 -04001283func addStateMachineCoverageTests(async, splitHandshake bool, protocol protocol) {
David Benjamin43ec06f2014-08-05 02:28:57 -04001284 var suffix string
1285 var flags []string
1286 var maxHandshakeRecordLength int
David Benjamin6fd297b2014-08-11 18:43:38 -04001287 if protocol == dtls {
1288 suffix = "-DTLS"
1289 }
David Benjamin43ec06f2014-08-05 02:28:57 -04001290 if async {
David Benjamin6fd297b2014-08-11 18:43:38 -04001291 suffix += "-Async"
David Benjamin43ec06f2014-08-05 02:28:57 -04001292 flags = append(flags, "-async")
1293 } else {
David Benjamin6fd297b2014-08-11 18:43:38 -04001294 suffix += "-Sync"
David Benjamin43ec06f2014-08-05 02:28:57 -04001295 }
1296 if splitHandshake {
1297 suffix += "-SplitHandshakeRecords"
David Benjamin98214542014-08-07 18:02:39 -04001298 maxHandshakeRecordLength = 1
David Benjamin43ec06f2014-08-05 02:28:57 -04001299 }
1300
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001301 // Basic handshake, with resumption. Client and server,
1302 // session ID and session ticket.
David Benjamin43ec06f2014-08-05 02:28:57 -04001303 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001304 protocol: protocol,
1305 name: "Basic-Client" + suffix,
David Benjamin43ec06f2014-08-05 02:28:57 -04001306 config: Config{
1307 Bugs: ProtocolBugs{
1308 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1309 },
1310 },
David Benjaminbed9aae2014-08-07 19:13:38 -04001311 flags: flags,
1312 resumeSession: true,
1313 })
1314 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001315 protocol: protocol,
1316 name: "Basic-Client-RenewTicket" + suffix,
David Benjaminbed9aae2014-08-07 19:13:38 -04001317 config: Config{
1318 Bugs: ProtocolBugs{
1319 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1320 RenewTicketOnResume: true,
1321 },
1322 },
1323 flags: flags,
1324 resumeSession: true,
David Benjamin43ec06f2014-08-05 02:28:57 -04001325 })
1326 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001327 protocol: protocol,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001328 name: "Basic-Client-NoTicket" + suffix,
1329 config: Config{
1330 SessionTicketsDisabled: true,
1331 Bugs: ProtocolBugs{
1332 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1333 },
1334 },
1335 flags: flags,
1336 resumeSession: true,
1337 })
1338 testCases = append(testCases, testCase{
1339 protocol: protocol,
David Benjamin43ec06f2014-08-05 02:28:57 -04001340 testType: serverTest,
1341 name: "Basic-Server" + suffix,
1342 config: Config{
1343 Bugs: ProtocolBugs{
1344 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1345 },
1346 },
David Benjaminbed9aae2014-08-07 19:13:38 -04001347 flags: flags,
1348 resumeSession: true,
David Benjamin43ec06f2014-08-05 02:28:57 -04001349 })
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001350 testCases = append(testCases, testCase{
1351 protocol: protocol,
1352 testType: serverTest,
1353 name: "Basic-Server-NoTickets" + suffix,
1354 config: Config{
1355 SessionTicketsDisabled: true,
1356 Bugs: ProtocolBugs{
1357 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1358 },
1359 },
1360 flags: flags,
1361 resumeSession: true,
1362 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001363
David Benjamin6fd297b2014-08-11 18:43:38 -04001364 // TLS client auth.
1365 testCases = append(testCases, testCase{
1366 protocol: protocol,
1367 testType: clientTest,
1368 name: "ClientAuth-Client" + suffix,
1369 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001370 ClientAuth: RequireAnyClientCert,
David Benjamin6fd297b2014-08-11 18:43:38 -04001371 Bugs: ProtocolBugs{
1372 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1373 },
1374 },
1375 flags: append(flags,
1376 "-cert-file", rsaCertificateFile,
1377 "-key-file", rsaKeyFile),
1378 })
1379 testCases = append(testCases, testCase{
1380 protocol: protocol,
1381 testType: serverTest,
1382 name: "ClientAuth-Server" + suffix,
1383 config: Config{
1384 Certificates: []Certificate{rsaCertificate},
1385 },
1386 flags: append(flags, "-require-any-client-certificate"),
1387 })
1388
David Benjamin43ec06f2014-08-05 02:28:57 -04001389 // No session ticket support; server doesn't send NewSessionTicket.
1390 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001391 protocol: protocol,
1392 name: "SessionTicketsDisabled-Client" + suffix,
David Benjamin43ec06f2014-08-05 02:28:57 -04001393 config: Config{
1394 SessionTicketsDisabled: true,
1395 Bugs: ProtocolBugs{
1396 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1397 },
1398 },
1399 flags: flags,
1400 })
1401 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001402 protocol: protocol,
David Benjamin43ec06f2014-08-05 02:28:57 -04001403 testType: serverTest,
1404 name: "SessionTicketsDisabled-Server" + suffix,
1405 config: Config{
1406 SessionTicketsDisabled: true,
1407 Bugs: ProtocolBugs{
1408 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1409 },
1410 },
1411 flags: flags,
1412 })
1413
David Benjamin48cae082014-10-27 01:06:24 -04001414 // Skip ServerKeyExchange in PSK key exchange if there's no
1415 // identity hint.
1416 testCases = append(testCases, testCase{
1417 protocol: protocol,
1418 name: "EmptyPSKHint-Client" + suffix,
1419 config: Config{
1420 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
1421 PreSharedKey: []byte("secret"),
1422 Bugs: ProtocolBugs{
1423 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1424 },
1425 },
1426 flags: append(flags, "-psk", "secret"),
1427 })
1428 testCases = append(testCases, testCase{
1429 protocol: protocol,
1430 testType: serverTest,
1431 name: "EmptyPSKHint-Server" + suffix,
1432 config: Config{
1433 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
1434 PreSharedKey: []byte("secret"),
1435 Bugs: ProtocolBugs{
1436 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1437 },
1438 },
1439 flags: append(flags, "-psk", "secret"),
1440 })
1441
David Benjamin6fd297b2014-08-11 18:43:38 -04001442 if protocol == tls {
1443 // NPN on client and server; results in post-handshake message.
1444 testCases = append(testCases, testCase{
1445 protocol: protocol,
1446 name: "NPN-Client" + suffix,
1447 config: Config{
David Benjaminae2888f2014-09-06 12:58:58 -04001448 NextProtos: []string{"foo"},
David Benjamin6fd297b2014-08-11 18:43:38 -04001449 Bugs: ProtocolBugs{
1450 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1451 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001452 },
David Benjaminfc7b0862014-09-06 13:21:53 -04001453 flags: append(flags, "-select-next-proto", "foo"),
1454 expectedNextProto: "foo",
1455 expectedNextProtoType: npn,
David Benjamin6fd297b2014-08-11 18:43:38 -04001456 })
1457 testCases = append(testCases, testCase{
1458 protocol: protocol,
1459 testType: serverTest,
1460 name: "NPN-Server" + suffix,
1461 config: Config{
1462 NextProtos: []string{"bar"},
1463 Bugs: ProtocolBugs{
1464 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1465 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001466 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001467 flags: append(flags,
1468 "-advertise-npn", "\x03foo\x03bar\x03baz",
1469 "-expect-next-proto", "bar"),
David Benjaminfc7b0862014-09-06 13:21:53 -04001470 expectedNextProto: "bar",
1471 expectedNextProtoType: npn,
David Benjamin6fd297b2014-08-11 18:43:38 -04001472 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001473
David Benjamin6fd297b2014-08-11 18:43:38 -04001474 // Client does False Start and negotiates NPN.
1475 testCases = append(testCases, testCase{
1476 protocol: protocol,
1477 name: "FalseStart" + suffix,
1478 config: Config{
1479 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1480 NextProtos: []string{"foo"},
1481 Bugs: ProtocolBugs{
David Benjamine58c4f52014-08-24 03:47:07 -04001482 ExpectFalseStart: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001483 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1484 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001485 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001486 flags: append(flags,
1487 "-false-start",
1488 "-select-next-proto", "foo"),
David Benjamine58c4f52014-08-24 03:47:07 -04001489 shimWritesFirst: true,
1490 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001491 })
David Benjamin43ec06f2014-08-05 02:28:57 -04001492
David Benjaminae2888f2014-09-06 12:58:58 -04001493 // Client does False Start and negotiates ALPN.
1494 testCases = append(testCases, testCase{
1495 protocol: protocol,
1496 name: "FalseStart-ALPN" + suffix,
1497 config: Config{
1498 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1499 NextProtos: []string{"foo"},
1500 Bugs: ProtocolBugs{
1501 ExpectFalseStart: true,
1502 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1503 },
1504 },
1505 flags: append(flags,
1506 "-false-start",
1507 "-advertise-alpn", "\x03foo"),
1508 shimWritesFirst: true,
1509 resumeSession: true,
1510 })
1511
David Benjamin6fd297b2014-08-11 18:43:38 -04001512 // False Start without session tickets.
1513 testCases = append(testCases, testCase{
1514 name: "FalseStart-SessionTicketsDisabled",
1515 config: Config{
1516 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1517 NextProtos: []string{"foo"},
1518 SessionTicketsDisabled: true,
David Benjamin4e99c522014-08-24 01:45:30 -04001519 Bugs: ProtocolBugs{
David Benjamine58c4f52014-08-24 03:47:07 -04001520 ExpectFalseStart: true,
David Benjamin4e99c522014-08-24 01:45:30 -04001521 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1522 },
David Benjamin43ec06f2014-08-05 02:28:57 -04001523 },
David Benjamin4e99c522014-08-24 01:45:30 -04001524 flags: append(flags,
David Benjamin6fd297b2014-08-11 18:43:38 -04001525 "-false-start",
1526 "-select-next-proto", "foo",
David Benjamin4e99c522014-08-24 01:45:30 -04001527 ),
David Benjamine58c4f52014-08-24 03:47:07 -04001528 shimWritesFirst: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001529 })
David Benjamin1e7f8d72014-08-08 12:27:04 -04001530
David Benjamina08e49d2014-08-24 01:46:07 -04001531 // Server parses a V2ClientHello.
David Benjamin6fd297b2014-08-11 18:43:38 -04001532 testCases = append(testCases, testCase{
1533 protocol: protocol,
1534 testType: serverTest,
1535 name: "SendV2ClientHello" + suffix,
1536 config: Config{
1537 // Choose a cipher suite that does not involve
1538 // elliptic curves, so no extensions are
1539 // involved.
1540 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
1541 Bugs: ProtocolBugs{
1542 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1543 SendV2ClientHello: true,
1544 },
David Benjamin1e7f8d72014-08-08 12:27:04 -04001545 },
David Benjamin6fd297b2014-08-11 18:43:38 -04001546 flags: flags,
1547 })
David Benjamina08e49d2014-08-24 01:46:07 -04001548
1549 // Client sends a Channel ID.
1550 testCases = append(testCases, testCase{
1551 protocol: protocol,
1552 name: "ChannelID-Client" + suffix,
1553 config: Config{
1554 RequestChannelID: true,
1555 Bugs: ProtocolBugs{
1556 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1557 },
1558 },
1559 flags: append(flags,
1560 "-send-channel-id", channelIDKeyFile,
1561 ),
1562 resumeSession: true,
1563 expectChannelID: true,
1564 })
1565
1566 // Server accepts a Channel ID.
1567 testCases = append(testCases, testCase{
1568 protocol: protocol,
1569 testType: serverTest,
1570 name: "ChannelID-Server" + suffix,
1571 config: Config{
1572 ChannelID: channelIDKey,
1573 Bugs: ProtocolBugs{
1574 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1575 },
1576 },
1577 flags: append(flags,
1578 "-expect-channel-id",
1579 base64.StdEncoding.EncodeToString(channelIDBytes),
1580 ),
1581 resumeSession: true,
1582 expectChannelID: true,
1583 })
David Benjamin6fd297b2014-08-11 18:43:38 -04001584 } else {
1585 testCases = append(testCases, testCase{
1586 protocol: protocol,
1587 name: "SkipHelloVerifyRequest" + suffix,
1588 config: Config{
1589 Bugs: ProtocolBugs{
1590 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1591 SkipHelloVerifyRequest: true,
1592 },
1593 },
1594 flags: flags,
1595 })
1596
1597 testCases = append(testCases, testCase{
1598 testType: serverTest,
1599 protocol: protocol,
1600 name: "CookieExchange" + suffix,
1601 config: Config{
1602 Bugs: ProtocolBugs{
1603 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1604 },
1605 },
1606 flags: append(flags, "-cookie-exchange"),
1607 })
1608 }
David Benjamin43ec06f2014-08-05 02:28:57 -04001609}
1610
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001611func addVersionNegotiationTests() {
1612 for i, shimVers := range tlsVersions {
1613 // Assemble flags to disable all newer versions on the shim.
1614 var flags []string
1615 for _, vers := range tlsVersions[i+1:] {
1616 flags = append(flags, vers.flag)
1617 }
1618
1619 for _, runnerVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05001620 protocols := []protocol{tls}
1621 if runnerVers.hasDTLS && shimVers.hasDTLS {
1622 protocols = append(protocols, dtls)
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001623 }
David Benjamin8b8c0062014-11-23 02:47:52 -05001624 for _, protocol := range protocols {
1625 expectedVersion := shimVers.version
1626 if runnerVers.version < shimVers.version {
1627 expectedVersion = runnerVers.version
1628 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001629
David Benjamin8b8c0062014-11-23 02:47:52 -05001630 suffix := shimVers.name + "-" + runnerVers.name
1631 if protocol == dtls {
1632 suffix += "-DTLS"
1633 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001634
David Benjamin8b8c0062014-11-23 02:47:52 -05001635 testCases = append(testCases, testCase{
1636 protocol: protocol,
1637 testType: clientTest,
1638 name: "VersionNegotiation-Client-" + suffix,
1639 config: Config{
1640 MaxVersion: runnerVers.version,
1641 },
1642 flags: flags,
1643 expectedVersion: expectedVersion,
1644 })
1645
1646 testCases = append(testCases, testCase{
1647 protocol: protocol,
1648 testType: serverTest,
1649 name: "VersionNegotiation-Server-" + suffix,
1650 config: Config{
1651 MaxVersion: runnerVers.version,
1652 },
1653 flags: flags,
1654 expectedVersion: expectedVersion,
1655 })
1656 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001657 }
1658 }
1659}
1660
David Benjamin5c24a1d2014-08-31 00:59:27 -04001661func addD5BugTests() {
1662 testCases = append(testCases, testCase{
1663 testType: serverTest,
1664 name: "D5Bug-NoQuirk-Reject",
1665 config: Config{
1666 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
1667 Bugs: ProtocolBugs{
1668 SSL3RSAKeyExchange: true,
1669 },
1670 },
1671 shouldFail: true,
1672 expectedError: ":TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG:",
1673 })
1674 testCases = append(testCases, testCase{
1675 testType: serverTest,
1676 name: "D5Bug-Quirk-Normal",
1677 config: Config{
1678 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
1679 },
1680 flags: []string{"-tls-d5-bug"},
1681 })
1682 testCases = append(testCases, testCase{
1683 testType: serverTest,
1684 name: "D5Bug-Quirk-Bug",
1685 config: Config{
1686 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
1687 Bugs: ProtocolBugs{
1688 SSL3RSAKeyExchange: true,
1689 },
1690 },
1691 flags: []string{"-tls-d5-bug"},
1692 })
1693}
1694
David Benjamine78bfde2014-09-06 12:45:15 -04001695func addExtensionTests() {
1696 testCases = append(testCases, testCase{
1697 testType: clientTest,
1698 name: "DuplicateExtensionClient",
1699 config: Config{
1700 Bugs: ProtocolBugs{
1701 DuplicateExtension: true,
1702 },
1703 },
1704 shouldFail: true,
1705 expectedLocalError: "remote error: error decoding message",
1706 })
1707 testCases = append(testCases, testCase{
1708 testType: serverTest,
1709 name: "DuplicateExtensionServer",
1710 config: Config{
1711 Bugs: ProtocolBugs{
1712 DuplicateExtension: true,
1713 },
1714 },
1715 shouldFail: true,
1716 expectedLocalError: "remote error: error decoding message",
1717 })
1718 testCases = append(testCases, testCase{
1719 testType: clientTest,
1720 name: "ServerNameExtensionClient",
1721 config: Config{
1722 Bugs: ProtocolBugs{
1723 ExpectServerName: "example.com",
1724 },
1725 },
1726 flags: []string{"-host-name", "example.com"},
1727 })
1728 testCases = append(testCases, testCase{
1729 testType: clientTest,
1730 name: "ServerNameExtensionClient",
1731 config: Config{
1732 Bugs: ProtocolBugs{
1733 ExpectServerName: "mismatch.com",
1734 },
1735 },
1736 flags: []string{"-host-name", "example.com"},
1737 shouldFail: true,
1738 expectedLocalError: "tls: unexpected server name",
1739 })
1740 testCases = append(testCases, testCase{
1741 testType: clientTest,
1742 name: "ServerNameExtensionClient",
1743 config: Config{
1744 Bugs: ProtocolBugs{
1745 ExpectServerName: "missing.com",
1746 },
1747 },
1748 shouldFail: true,
1749 expectedLocalError: "tls: unexpected server name",
1750 })
1751 testCases = append(testCases, testCase{
1752 testType: serverTest,
1753 name: "ServerNameExtensionServer",
1754 config: Config{
1755 ServerName: "example.com",
1756 },
1757 flags: []string{"-expect-server-name", "example.com"},
1758 resumeSession: true,
1759 })
David Benjaminae2888f2014-09-06 12:58:58 -04001760 testCases = append(testCases, testCase{
1761 testType: clientTest,
1762 name: "ALPNClient",
1763 config: Config{
1764 NextProtos: []string{"foo"},
1765 },
1766 flags: []string{
1767 "-advertise-alpn", "\x03foo\x03bar\x03baz",
1768 "-expect-alpn", "foo",
1769 },
David Benjaminfc7b0862014-09-06 13:21:53 -04001770 expectedNextProto: "foo",
1771 expectedNextProtoType: alpn,
1772 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04001773 })
1774 testCases = append(testCases, testCase{
1775 testType: serverTest,
1776 name: "ALPNServer",
1777 config: Config{
1778 NextProtos: []string{"foo", "bar", "baz"},
1779 },
1780 flags: []string{
1781 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
1782 "-select-alpn", "foo",
1783 },
David Benjaminfc7b0862014-09-06 13:21:53 -04001784 expectedNextProto: "foo",
1785 expectedNextProtoType: alpn,
1786 resumeSession: true,
1787 })
1788 // Test that the server prefers ALPN over NPN.
1789 testCases = append(testCases, testCase{
1790 testType: serverTest,
1791 name: "ALPNServer-Preferred",
1792 config: Config{
1793 NextProtos: []string{"foo", "bar", "baz"},
1794 },
1795 flags: []string{
1796 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
1797 "-select-alpn", "foo",
1798 "-advertise-npn", "\x03foo\x03bar\x03baz",
1799 },
1800 expectedNextProto: "foo",
1801 expectedNextProtoType: alpn,
1802 resumeSession: true,
1803 })
1804 testCases = append(testCases, testCase{
1805 testType: serverTest,
1806 name: "ALPNServer-Preferred-Swapped",
1807 config: Config{
1808 NextProtos: []string{"foo", "bar", "baz"},
1809 Bugs: ProtocolBugs{
1810 SwapNPNAndALPN: true,
1811 },
1812 },
1813 flags: []string{
1814 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
1815 "-select-alpn", "foo",
1816 "-advertise-npn", "\x03foo\x03bar\x03baz",
1817 },
1818 expectedNextProto: "foo",
1819 expectedNextProtoType: alpn,
1820 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04001821 })
Adam Langley38311732014-10-16 19:04:35 -07001822 // Resume with a corrupt ticket.
1823 testCases = append(testCases, testCase{
1824 testType: serverTest,
1825 name: "CorruptTicket",
1826 config: Config{
1827 Bugs: ProtocolBugs{
1828 CorruptTicket: true,
1829 },
1830 },
1831 resumeSession: true,
1832 flags: []string{"-expect-session-miss"},
1833 })
1834 // Resume with an oversized session id.
1835 testCases = append(testCases, testCase{
1836 testType: serverTest,
1837 name: "OversizedSessionId",
1838 config: Config{
1839 Bugs: ProtocolBugs{
1840 OversizedSessionId: true,
1841 },
1842 },
1843 resumeSession: true,
Adam Langley75712922014-10-10 16:23:43 -07001844 shouldFail: true,
Adam Langley38311732014-10-16 19:04:35 -07001845 expectedError: ":DECODE_ERROR:",
1846 })
David Benjaminca6c8262014-11-15 19:06:08 -05001847 // Basic DTLS-SRTP tests. Include fake profiles to ensure they
1848 // are ignored.
1849 testCases = append(testCases, testCase{
1850 protocol: dtls,
1851 name: "SRTP-Client",
1852 config: Config{
1853 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
1854 },
1855 flags: []string{
1856 "-srtp-profiles",
1857 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
1858 },
1859 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
1860 })
1861 testCases = append(testCases, testCase{
1862 protocol: dtls,
1863 testType: serverTest,
1864 name: "SRTP-Server",
1865 config: Config{
1866 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
1867 },
1868 flags: []string{
1869 "-srtp-profiles",
1870 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
1871 },
1872 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
1873 })
1874 // Test that the MKI is ignored.
1875 testCases = append(testCases, testCase{
1876 protocol: dtls,
1877 testType: serverTest,
1878 name: "SRTP-Server-IgnoreMKI",
1879 config: Config{
1880 SRTPProtectionProfiles: []uint16{SRTP_AES128_CM_HMAC_SHA1_80},
1881 Bugs: ProtocolBugs{
1882 SRTPMasterKeyIdentifer: "bogus",
1883 },
1884 },
1885 flags: []string{
1886 "-srtp-profiles",
1887 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
1888 },
1889 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
1890 })
1891 // Test that SRTP isn't negotiated on the server if there were
1892 // no matching profiles.
1893 testCases = append(testCases, testCase{
1894 protocol: dtls,
1895 testType: serverTest,
1896 name: "SRTP-Server-NoMatch",
1897 config: Config{
1898 SRTPProtectionProfiles: []uint16{100, 101, 102},
1899 },
1900 flags: []string{
1901 "-srtp-profiles",
1902 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
1903 },
1904 expectedSRTPProtectionProfile: 0,
1905 })
1906 // Test that the server returning an invalid SRTP profile is
1907 // flagged as an error by the client.
1908 testCases = append(testCases, testCase{
1909 protocol: dtls,
1910 name: "SRTP-Client-NoMatch",
1911 config: Config{
1912 Bugs: ProtocolBugs{
1913 SendSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_32,
1914 },
1915 },
1916 flags: []string{
1917 "-srtp-profiles",
1918 "SRTP_AES128_CM_SHA1_80",
1919 },
1920 shouldFail: true,
1921 expectedError: ":BAD_SRTP_PROTECTION_PROFILE_LIST:",
1922 })
David Benjamin61f95272014-11-25 01:55:35 -05001923 // Test OCSP stapling and SCT list.
1924 testCases = append(testCases, testCase{
1925 name: "OCSPStapling",
1926 flags: []string{
1927 "-enable-ocsp-stapling",
1928 "-expect-ocsp-response",
1929 base64.StdEncoding.EncodeToString(testOCSPResponse),
1930 },
1931 })
1932 testCases = append(testCases, testCase{
1933 name: "SignedCertificateTimestampList",
1934 flags: []string{
1935 "-enable-signed-cert-timestamps",
1936 "-expect-signed-cert-timestamps",
1937 base64.StdEncoding.EncodeToString(testSCTList),
1938 },
1939 })
David Benjamine78bfde2014-09-06 12:45:15 -04001940}
1941
David Benjamin01fe8202014-09-24 15:21:44 -04001942func addResumptionVersionTests() {
David Benjamin01fe8202014-09-24 15:21:44 -04001943 for _, sessionVers := range tlsVersions {
David Benjamin01fe8202014-09-24 15:21:44 -04001944 for _, resumeVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05001945 protocols := []protocol{tls}
1946 if sessionVers.hasDTLS && resumeVers.hasDTLS {
1947 protocols = append(protocols, dtls)
David Benjaminbdf5e722014-11-11 00:52:15 -05001948 }
David Benjamin8b8c0062014-11-23 02:47:52 -05001949 for _, protocol := range protocols {
1950 suffix := "-" + sessionVers.name + "-" + resumeVers.name
1951 if protocol == dtls {
1952 suffix += "-DTLS"
1953 }
1954
1955 testCases = append(testCases, testCase{
1956 protocol: protocol,
1957 name: "Resume-Client" + suffix,
1958 resumeSession: true,
1959 config: Config{
1960 MaxVersion: sessionVers.version,
1961 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
1962 Bugs: ProtocolBugs{
1963 AllowSessionVersionMismatch: true,
1964 },
1965 },
1966 expectedVersion: sessionVers.version,
1967 resumeConfig: &Config{
1968 MaxVersion: resumeVers.version,
1969 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
1970 Bugs: ProtocolBugs{
1971 AllowSessionVersionMismatch: true,
1972 },
1973 },
1974 expectedResumeVersion: resumeVers.version,
1975 })
1976
1977 testCases = append(testCases, testCase{
1978 protocol: protocol,
1979 name: "Resume-Client-NoResume" + suffix,
1980 flags: []string{"-expect-session-miss"},
1981 resumeSession: true,
1982 config: Config{
1983 MaxVersion: sessionVers.version,
1984 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
1985 },
1986 expectedVersion: sessionVers.version,
1987 resumeConfig: &Config{
1988 MaxVersion: resumeVers.version,
1989 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
1990 },
1991 newSessionsOnResume: true,
1992 expectedResumeVersion: resumeVers.version,
1993 })
1994
1995 var flags []string
1996 if sessionVers.version != resumeVers.version {
1997 flags = append(flags, "-expect-session-miss")
1998 }
1999 testCases = append(testCases, testCase{
2000 protocol: protocol,
2001 testType: serverTest,
2002 name: "Resume-Server" + suffix,
2003 flags: flags,
2004 resumeSession: true,
2005 config: Config{
2006 MaxVersion: sessionVers.version,
2007 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2008 },
2009 expectedVersion: sessionVers.version,
2010 resumeConfig: &Config{
2011 MaxVersion: resumeVers.version,
2012 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2013 },
2014 expectedResumeVersion: resumeVers.version,
2015 })
2016 }
David Benjamin01fe8202014-09-24 15:21:44 -04002017 }
2018 }
2019}
2020
Adam Langley2ae77d22014-10-28 17:29:33 -07002021func addRenegotiationTests() {
2022 testCases = append(testCases, testCase{
2023 testType: serverTest,
2024 name: "Renegotiate-Server",
2025 flags: []string{"-renegotiate"},
2026 shimWritesFirst: true,
2027 })
2028 testCases = append(testCases, testCase{
2029 testType: serverTest,
2030 name: "Renegotiate-Server-EmptyExt",
2031 config: Config{
2032 Bugs: ProtocolBugs{
2033 EmptyRenegotiationInfo: true,
2034 },
2035 },
2036 flags: []string{"-renegotiate"},
2037 shimWritesFirst: true,
2038 shouldFail: true,
2039 expectedError: ":RENEGOTIATION_MISMATCH:",
2040 })
2041 testCases = append(testCases, testCase{
2042 testType: serverTest,
2043 name: "Renegotiate-Server-BadExt",
2044 config: Config{
2045 Bugs: ProtocolBugs{
2046 BadRenegotiationInfo: true,
2047 },
2048 },
2049 flags: []string{"-renegotiate"},
2050 shimWritesFirst: true,
2051 shouldFail: true,
2052 expectedError: ":RENEGOTIATION_MISMATCH:",
2053 })
David Benjaminca6554b2014-11-08 12:31:52 -05002054 testCases = append(testCases, testCase{
2055 testType: serverTest,
2056 name: "Renegotiate-Server-ClientInitiated",
2057 renegotiate: true,
2058 })
2059 testCases = append(testCases, testCase{
2060 testType: serverTest,
2061 name: "Renegotiate-Server-ClientInitiated-NoExt",
2062 renegotiate: true,
2063 config: Config{
2064 Bugs: ProtocolBugs{
2065 NoRenegotiationInfo: true,
2066 },
2067 },
2068 shouldFail: true,
2069 expectedError: ":UNSAFE_LEGACY_RENEGOTIATION_DISABLED:",
2070 })
2071 testCases = append(testCases, testCase{
2072 testType: serverTest,
2073 name: "Renegotiate-Server-ClientInitiated-NoExt-Allowed",
2074 renegotiate: true,
2075 config: Config{
2076 Bugs: ProtocolBugs{
2077 NoRenegotiationInfo: true,
2078 },
2079 },
2080 flags: []string{"-allow-unsafe-legacy-renegotiation"},
2081 })
Adam Langley2ae77d22014-10-28 17:29:33 -07002082 // TODO(agl): test the renegotiation info SCSV.
Adam Langleycf2d4f42014-10-28 19:06:14 -07002083 testCases = append(testCases, testCase{
2084 name: "Renegotiate-Client",
2085 renegotiate: true,
2086 })
2087 testCases = append(testCases, testCase{
2088 name: "Renegotiate-Client-EmptyExt",
2089 renegotiate: true,
2090 config: Config{
2091 Bugs: ProtocolBugs{
2092 EmptyRenegotiationInfo: true,
2093 },
2094 },
2095 shouldFail: true,
2096 expectedError: ":RENEGOTIATION_MISMATCH:",
2097 })
2098 testCases = append(testCases, testCase{
2099 name: "Renegotiate-Client-BadExt",
2100 renegotiate: true,
2101 config: Config{
2102 Bugs: ProtocolBugs{
2103 BadRenegotiationInfo: true,
2104 },
2105 },
2106 shouldFail: true,
2107 expectedError: ":RENEGOTIATION_MISMATCH:",
2108 })
2109 testCases = append(testCases, testCase{
2110 name: "Renegotiate-Client-SwitchCiphers",
2111 renegotiate: true,
2112 config: Config{
2113 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2114 },
2115 renegotiateCiphers: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2116 })
2117 testCases = append(testCases, testCase{
2118 name: "Renegotiate-Client-SwitchCiphers2",
2119 renegotiate: true,
2120 config: Config{
2121 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2122 },
2123 renegotiateCiphers: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2124 })
David Benjaminc44b1df2014-11-23 12:11:01 -05002125 testCases = append(testCases, testCase{
2126 name: "Renegotiate-SameClientVersion",
2127 renegotiate: true,
2128 config: Config{
2129 MaxVersion: VersionTLS10,
2130 Bugs: ProtocolBugs{
2131 RequireSameRenegoClientVersion: true,
2132 },
2133 },
2134 })
Adam Langley2ae77d22014-10-28 17:29:33 -07002135}
2136
David Benjamin5e961c12014-11-07 01:48:35 -05002137func addDTLSReplayTests() {
2138 // Test that sequence number replays are detected.
2139 testCases = append(testCases, testCase{
2140 protocol: dtls,
2141 name: "DTLS-Replay",
2142 replayWrites: true,
2143 })
2144
2145 // Test the outgoing sequence number skipping by values larger
2146 // than the retransmit window.
2147 testCases = append(testCases, testCase{
2148 protocol: dtls,
2149 name: "DTLS-Replay-LargeGaps",
2150 config: Config{
2151 Bugs: ProtocolBugs{
2152 SequenceNumberIncrement: 127,
2153 },
2154 },
2155 replayWrites: true,
2156 })
2157}
2158
David Benjamin000800a2014-11-14 01:43:59 -05002159var testHashes = []struct {
2160 name string
2161 id uint8
2162}{
2163 {"SHA1", hashSHA1},
2164 {"SHA224", hashSHA224},
2165 {"SHA256", hashSHA256},
2166 {"SHA384", hashSHA384},
2167 {"SHA512", hashSHA512},
2168}
2169
2170func addSigningHashTests() {
2171 // Make sure each hash works. Include some fake hashes in the list and
2172 // ensure they're ignored.
2173 for _, hash := range testHashes {
2174 testCases = append(testCases, testCase{
2175 name: "SigningHash-ClientAuth-" + hash.name,
2176 config: Config{
2177 ClientAuth: RequireAnyClientCert,
2178 SignatureAndHashes: []signatureAndHash{
2179 {signatureRSA, 42},
2180 {signatureRSA, hash.id},
2181 {signatureRSA, 255},
2182 },
2183 },
2184 flags: []string{
2185 "-cert-file", rsaCertificateFile,
2186 "-key-file", rsaKeyFile,
2187 },
2188 })
2189
2190 testCases = append(testCases, testCase{
2191 testType: serverTest,
2192 name: "SigningHash-ServerKeyExchange-Sign-" + hash.name,
2193 config: Config{
2194 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2195 SignatureAndHashes: []signatureAndHash{
2196 {signatureRSA, 42},
2197 {signatureRSA, hash.id},
2198 {signatureRSA, 255},
2199 },
2200 },
2201 })
2202 }
2203
2204 // Test that hash resolution takes the signature type into account.
2205 testCases = append(testCases, testCase{
2206 name: "SigningHash-ClientAuth-SignatureType",
2207 config: Config{
2208 ClientAuth: RequireAnyClientCert,
2209 SignatureAndHashes: []signatureAndHash{
2210 {signatureECDSA, hashSHA512},
2211 {signatureRSA, hashSHA384},
2212 {signatureECDSA, hashSHA1},
2213 },
2214 },
2215 flags: []string{
2216 "-cert-file", rsaCertificateFile,
2217 "-key-file", rsaKeyFile,
2218 },
2219 })
2220
2221 testCases = append(testCases, testCase{
2222 testType: serverTest,
2223 name: "SigningHash-ServerKeyExchange-SignatureType",
2224 config: Config{
2225 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2226 SignatureAndHashes: []signatureAndHash{
2227 {signatureECDSA, hashSHA512},
2228 {signatureRSA, hashSHA384},
2229 {signatureECDSA, hashSHA1},
2230 },
2231 },
2232 })
2233
2234 // Test that, if the list is missing, the peer falls back to SHA-1.
2235 testCases = append(testCases, testCase{
2236 name: "SigningHash-ClientAuth-Fallback",
2237 config: Config{
2238 ClientAuth: RequireAnyClientCert,
2239 SignatureAndHashes: []signatureAndHash{
2240 {signatureRSA, hashSHA1},
2241 },
2242 Bugs: ProtocolBugs{
2243 NoSignatureAndHashes: true,
2244 },
2245 },
2246 flags: []string{
2247 "-cert-file", rsaCertificateFile,
2248 "-key-file", rsaKeyFile,
2249 },
2250 })
2251
2252 testCases = append(testCases, testCase{
2253 testType: serverTest,
2254 name: "SigningHash-ServerKeyExchange-Fallback",
2255 config: Config{
2256 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2257 SignatureAndHashes: []signatureAndHash{
2258 {signatureRSA, hashSHA1},
2259 },
2260 Bugs: ProtocolBugs{
2261 NoSignatureAndHashes: true,
2262 },
2263 },
2264 })
2265}
2266
David Benjamin884fdf12014-08-02 15:28:23 -04002267func worker(statusChan chan statusMsg, c chan *testCase, buildDir string, wg *sync.WaitGroup) {
Adam Langley95c29f32014-06-20 12:00:00 -07002268 defer wg.Done()
2269
2270 for test := range c {
Adam Langley69a01602014-11-17 17:26:55 -08002271 var err error
2272
2273 if *mallocTest < 0 {
2274 statusChan <- statusMsg{test: test, started: true}
2275 err = runTest(test, buildDir, -1)
2276 } else {
2277 for mallocNumToFail := int64(*mallocTest); ; mallocNumToFail++ {
2278 statusChan <- statusMsg{test: test, started: true}
2279 if err = runTest(test, buildDir, mallocNumToFail); err != errMoreMallocs {
2280 if err != nil {
2281 fmt.Printf("\n\nmalloc test failed at %d: %s\n", mallocNumToFail, err)
2282 }
2283 break
2284 }
2285 }
2286 }
Adam Langley95c29f32014-06-20 12:00:00 -07002287 statusChan <- statusMsg{test: test, err: err}
2288 }
2289}
2290
2291type statusMsg struct {
2292 test *testCase
2293 started bool
2294 err error
2295}
2296
2297func statusPrinter(doneChan chan struct{}, statusChan chan statusMsg, total int) {
2298 var started, done, failed, lineLen int
2299 defer close(doneChan)
2300
2301 for msg := range statusChan {
2302 if msg.started {
2303 started++
2304 } else {
2305 done++
2306 }
2307
2308 fmt.Printf("\x1b[%dD\x1b[K", lineLen)
2309
2310 if msg.err != nil {
2311 fmt.Printf("FAILED (%s)\n%s\n", msg.test.name, msg.err)
2312 failed++
2313 }
2314 line := fmt.Sprintf("%d/%d/%d/%d", failed, done, started, total)
2315 lineLen = len(line)
2316 os.Stdout.WriteString(line)
2317 }
2318}
2319
2320func main() {
2321 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 -04002322 var flagNumWorkers *int = flag.Int("num-workers", runtime.NumCPU(), "The number of workers to run in parallel.")
David Benjamin884fdf12014-08-02 15:28:23 -04002323 var flagBuildDir *string = flag.String("build-dir", "../../../build", "The build directory to run the shim from.")
Adam Langley95c29f32014-06-20 12:00:00 -07002324
2325 flag.Parse()
2326
2327 addCipherSuiteTests()
2328 addBadECDSASignatureTests()
Adam Langley80842bd2014-06-20 12:00:00 -07002329 addCBCPaddingTests()
Kenny Root7fdeaf12014-08-05 15:23:37 -07002330 addCBCSplittingTests()
David Benjamin636293b2014-07-08 17:59:18 -04002331 addClientAuthTests()
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002332 addVersionNegotiationTests()
David Benjamin5c24a1d2014-08-31 00:59:27 -04002333 addD5BugTests()
David Benjamine78bfde2014-09-06 12:45:15 -04002334 addExtensionTests()
David Benjamin01fe8202014-09-24 15:21:44 -04002335 addResumptionVersionTests()
Adam Langley75712922014-10-10 16:23:43 -07002336 addExtendedMasterSecretTests()
Adam Langley2ae77d22014-10-28 17:29:33 -07002337 addRenegotiationTests()
David Benjamin5e961c12014-11-07 01:48:35 -05002338 addDTLSReplayTests()
David Benjamin000800a2014-11-14 01:43:59 -05002339 addSigningHashTests()
David Benjamin43ec06f2014-08-05 02:28:57 -04002340 for _, async := range []bool{false, true} {
2341 for _, splitHandshake := range []bool{false, true} {
David Benjamin6fd297b2014-08-11 18:43:38 -04002342 for _, protocol := range []protocol{tls, dtls} {
2343 addStateMachineCoverageTests(async, splitHandshake, protocol)
2344 }
David Benjamin43ec06f2014-08-05 02:28:57 -04002345 }
2346 }
Adam Langley95c29f32014-06-20 12:00:00 -07002347
2348 var wg sync.WaitGroup
2349
David Benjamin2bc8e6f2014-08-02 15:22:37 -04002350 numWorkers := *flagNumWorkers
Adam Langley95c29f32014-06-20 12:00:00 -07002351
2352 statusChan := make(chan statusMsg, numWorkers)
2353 testChan := make(chan *testCase, numWorkers)
2354 doneChan := make(chan struct{})
2355
David Benjamin025b3d32014-07-01 19:53:04 -04002356 go statusPrinter(doneChan, statusChan, len(testCases))
Adam Langley95c29f32014-06-20 12:00:00 -07002357
2358 for i := 0; i < numWorkers; i++ {
2359 wg.Add(1)
David Benjamin884fdf12014-08-02 15:28:23 -04002360 go worker(statusChan, testChan, *flagBuildDir, &wg)
Adam Langley95c29f32014-06-20 12:00:00 -07002361 }
2362
David Benjamin025b3d32014-07-01 19:53:04 -04002363 for i := range testCases {
2364 if len(*flagTest) == 0 || *flagTest == testCases[i].name {
2365 testChan <- &testCases[i]
Adam Langley95c29f32014-06-20 12:00:00 -07002366 }
2367 }
2368
2369 close(testChan)
2370 wg.Wait()
2371 close(statusChan)
2372 <-doneChan
2373
2374 fmt.Printf("\n")
2375}