blob: bd03cb12b4f9a9a740ec637177026356a604b69c [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 Langleya7997f12015-05-14 17:38:50 -070014 "math/big"
Adam Langley95c29f32014-06-20 12:00:00 -070015 "net"
16 "os"
17 "os/exec"
David Benjamin884fdf12014-08-02 15:28:23 -040018 "path"
David Benjamin2bc8e6f2014-08-02 15:22:37 -040019 "runtime"
Adam Langley69a01602014-11-17 17:26:55 -080020 "strconv"
Adam Langley95c29f32014-06-20 12:00:00 -070021 "strings"
22 "sync"
23 "syscall"
David Benjamin83f90402015-01-27 01:09:43 -050024 "time"
Adam Langley95c29f32014-06-20 12:00:00 -070025)
26
Adam Langley69a01602014-11-17 17:26:55 -080027var (
David Benjamin5f237bc2015-02-11 17:14:15 -050028 useValgrind = flag.Bool("valgrind", false, "If true, run code under valgrind")
29 useGDB = flag.Bool("gdb", false, "If true, run BoringSSL code under gdb")
30 flagDebug = flag.Bool("debug", false, "Hexdump the contents of the connection")
31 mallocTest = flag.Int64("malloc-test", -1, "If non-negative, run each test with each malloc in turn failing from the given number onwards.")
32 mallocTestDebug = 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.")
33 jsonOutput = flag.String("json-output", "", "The file to output JSON results to.")
34 pipe = flag.Bool("pipe", false, "If true, print status output suitable for piping into another program.")
Adam Langley69a01602014-11-17 17:26:55 -080035)
Adam Langley95c29f32014-06-20 12:00:00 -070036
David Benjamin025b3d32014-07-01 19:53:04 -040037const (
38 rsaCertificateFile = "cert.pem"
39 ecdsaCertificateFile = "ecdsa_cert.pem"
40)
41
42const (
David Benjamina08e49d2014-08-24 01:46:07 -040043 rsaKeyFile = "key.pem"
44 ecdsaKeyFile = "ecdsa_key.pem"
45 channelIDKeyFile = "channel_id_key.pem"
David Benjamin025b3d32014-07-01 19:53:04 -040046)
47
Adam Langley95c29f32014-06-20 12:00:00 -070048var rsaCertificate, ecdsaCertificate Certificate
David Benjamina08e49d2014-08-24 01:46:07 -040049var channelIDKey *ecdsa.PrivateKey
50var channelIDBytes []byte
Adam Langley95c29f32014-06-20 12:00:00 -070051
David Benjamin61f95272014-11-25 01:55:35 -050052var testOCSPResponse = []byte{1, 2, 3, 4}
53var testSCTList = []byte{5, 6, 7, 8}
54
Adam Langley95c29f32014-06-20 12:00:00 -070055func initCertificates() {
56 var err error
David Benjamin025b3d32014-07-01 19:53:04 -040057 rsaCertificate, err = LoadX509KeyPair(rsaCertificateFile, rsaKeyFile)
Adam Langley95c29f32014-06-20 12:00:00 -070058 if err != nil {
59 panic(err)
60 }
David Benjamin61f95272014-11-25 01:55:35 -050061 rsaCertificate.OCSPStaple = testOCSPResponse
62 rsaCertificate.SignedCertificateTimestampList = testSCTList
Adam Langley95c29f32014-06-20 12:00:00 -070063
David Benjamin025b3d32014-07-01 19:53:04 -040064 ecdsaCertificate, err = LoadX509KeyPair(ecdsaCertificateFile, ecdsaKeyFile)
Adam Langley95c29f32014-06-20 12:00:00 -070065 if err != nil {
66 panic(err)
67 }
David Benjamin61f95272014-11-25 01:55:35 -050068 ecdsaCertificate.OCSPStaple = testOCSPResponse
69 ecdsaCertificate.SignedCertificateTimestampList = testSCTList
David Benjamina08e49d2014-08-24 01:46:07 -040070
71 channelIDPEMBlock, err := ioutil.ReadFile(channelIDKeyFile)
72 if err != nil {
73 panic(err)
74 }
75 channelIDDERBlock, _ := pem.Decode(channelIDPEMBlock)
76 if channelIDDERBlock.Type != "EC PRIVATE KEY" {
77 panic("bad key type")
78 }
79 channelIDKey, err = x509.ParseECPrivateKey(channelIDDERBlock.Bytes)
80 if err != nil {
81 panic(err)
82 }
83 if channelIDKey.Curve != elliptic.P256() {
84 panic("bad curve")
85 }
86
87 channelIDBytes = make([]byte, 64)
88 writeIntPadded(channelIDBytes[:32], channelIDKey.X)
89 writeIntPadded(channelIDBytes[32:], channelIDKey.Y)
Adam Langley95c29f32014-06-20 12:00:00 -070090}
91
92var certificateOnce sync.Once
93
94func getRSACertificate() Certificate {
95 certificateOnce.Do(initCertificates)
96 return rsaCertificate
97}
98
99func getECDSACertificate() Certificate {
100 certificateOnce.Do(initCertificates)
101 return ecdsaCertificate
102}
103
David Benjamin025b3d32014-07-01 19:53:04 -0400104type testType int
105
106const (
107 clientTest testType = iota
108 serverTest
109)
110
David Benjamin6fd297b2014-08-11 18:43:38 -0400111type protocol int
112
113const (
114 tls protocol = iota
115 dtls
116)
117
David Benjaminfc7b0862014-09-06 13:21:53 -0400118const (
119 alpn = 1
120 npn = 2
121)
122
Adam Langley95c29f32014-06-20 12:00:00 -0700123type testCase struct {
David Benjamin025b3d32014-07-01 19:53:04 -0400124 testType testType
David Benjamin6fd297b2014-08-11 18:43:38 -0400125 protocol protocol
Adam Langley95c29f32014-06-20 12:00:00 -0700126 name string
127 config Config
128 shouldFail bool
129 expectedError string
Adam Langleyac61fa32014-06-23 12:03:11 -0700130 // expectedLocalError, if not empty, contains a substring that must be
131 // found in the local error.
132 expectedLocalError string
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400133 // expectedVersion, if non-zero, specifies the TLS version that must be
134 // negotiated.
135 expectedVersion uint16
David Benjamin01fe8202014-09-24 15:21:44 -0400136 // expectedResumeVersion, if non-zero, specifies the TLS version that
137 // must be negotiated on resumption. If zero, expectedVersion is used.
138 expectedResumeVersion uint16
David Benjamin90da8c82015-04-20 14:57:57 -0400139 // expectedCipher, if non-zero, specifies the TLS cipher suite that
140 // should be negotiated.
141 expectedCipher uint16
David Benjamina08e49d2014-08-24 01:46:07 -0400142 // expectChannelID controls whether the connection should have
143 // negotiated a Channel ID with channelIDKey.
144 expectChannelID bool
David Benjaminae2888f2014-09-06 12:58:58 -0400145 // expectedNextProto controls whether the connection should
146 // negotiate a next protocol via NPN or ALPN.
147 expectedNextProto string
David Benjaminfc7b0862014-09-06 13:21:53 -0400148 // expectedNextProtoType, if non-zero, is the expected next
149 // protocol negotiation mechanism.
150 expectedNextProtoType int
David Benjaminca6c8262014-11-15 19:06:08 -0500151 // expectedSRTPProtectionProfile is the DTLS-SRTP profile that
152 // should be negotiated. If zero, none should be negotiated.
153 expectedSRTPProtectionProfile uint16
Adam Langley80842bd2014-06-20 12:00:00 -0700154 // messageLen is the length, in bytes, of the test message that will be
155 // sent.
156 messageLen int
David Benjamin025b3d32014-07-01 19:53:04 -0400157 // certFile is the path to the certificate to use for the server.
158 certFile string
159 // keyFile is the path to the private key to use for the server.
160 keyFile string
David Benjamin1d5c83e2014-07-22 19:20:02 -0400161 // resumeSession controls whether a second connection should be tested
David Benjamin01fe8202014-09-24 15:21:44 -0400162 // which attempts to resume the first session.
David Benjamin1d5c83e2014-07-22 19:20:02 -0400163 resumeSession bool
Adam Langleyb0eef0a2015-06-02 10:47:39 -0700164 // expectResumeRejected, if true, specifies that the attempted
165 // resumption must be rejected by the client. This is only valid for a
166 // serverTest.
167 expectResumeRejected bool
David Benjamin01fe8202014-09-24 15:21:44 -0400168 // resumeConfig, if not nil, points to a Config to be used on
David Benjaminfe8eb9a2014-11-17 03:19:02 -0500169 // resumption. Unless newSessionsOnResume is set,
170 // SessionTicketKey, ServerSessionCache, and
171 // ClientSessionCache are copied from the initial connection's
172 // config. If nil, the initial connection's config is used.
David Benjamin01fe8202014-09-24 15:21:44 -0400173 resumeConfig *Config
David Benjaminfe8eb9a2014-11-17 03:19:02 -0500174 // newSessionsOnResume, if true, will cause resumeConfig to
175 // use a different session resumption context.
176 newSessionsOnResume bool
David Benjamin98e882e2014-08-08 13:24:34 -0400177 // sendPrefix sends a prefix on the socket before actually performing a
178 // handshake.
179 sendPrefix string
David Benjamine58c4f52014-08-24 03:47:07 -0400180 // shimWritesFirst controls whether the shim sends an initial "hello"
181 // message before doing a roundtrip with the runner.
182 shimWritesFirst bool
Adam Langleycf2d4f42014-10-28 19:06:14 -0700183 // renegotiate indicates the the connection should be renegotiated
184 // during the exchange.
185 renegotiate bool
186 // renegotiateCiphers is a list of ciphersuite ids that will be
187 // switched in just before renegotiation.
188 renegotiateCiphers []uint16
David Benjamin5e961c12014-11-07 01:48:35 -0500189 // replayWrites, if true, configures the underlying transport
190 // to replay every write it makes in DTLS tests.
191 replayWrites bool
David Benjamin5fa3eba2015-01-22 16:35:40 -0500192 // damageFirstWrite, if true, configures the underlying transport to
193 // damage the final byte of the first application data write.
194 damageFirstWrite bool
David Benjaminc565ebb2015-04-03 04:06:36 -0400195 // exportKeyingMaterial, if non-zero, configures the test to exchange
196 // keying material and verify they match.
197 exportKeyingMaterial int
198 exportLabel string
199 exportContext string
200 useExportContext bool
David Benjamin325b5c32014-07-01 19:40:31 -0400201 // flags, if not empty, contains a list of command-line flags that will
202 // be passed to the shim program.
203 flags []string
Adam Langleyaf0e32c2015-06-03 09:57:23 -0700204 // testTLSUnique, if true, causes the shim to send the tls-unique value
205 // which will be compared against the expected value.
206 testTLSUnique bool
Adam Langley95c29f32014-06-20 12:00:00 -0700207}
208
David Benjamin025b3d32014-07-01 19:53:04 -0400209var testCases = []testCase{
Adam Langley95c29f32014-06-20 12:00:00 -0700210 {
211 name: "BadRSASignature",
212 config: Config{
213 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
214 Bugs: ProtocolBugs{
215 InvalidSKXSignature: true,
216 },
217 },
218 shouldFail: true,
David Benjamin25f08462015-04-15 16:13:49 -0400219 expectedError: ":BAD_SIGNATURE:",
Adam Langley95c29f32014-06-20 12:00:00 -0700220 },
221 {
222 name: "BadECDSASignature",
223 config: Config{
224 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
225 Bugs: ProtocolBugs{
226 InvalidSKXSignature: true,
227 },
228 Certificates: []Certificate{getECDSACertificate()},
229 },
230 shouldFail: true,
231 expectedError: ":BAD_SIGNATURE:",
232 },
233 {
234 name: "BadECDSACurve",
235 config: Config{
236 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
237 Bugs: ProtocolBugs{
238 InvalidSKXCurve: true,
239 },
240 Certificates: []Certificate{getECDSACertificate()},
241 },
242 shouldFail: true,
243 expectedError: ":WRONG_CURVE:",
244 },
Adam Langleyac61fa32014-06-23 12:03:11 -0700245 {
David Benjamina8e3e0e2014-08-06 22:11:10 -0400246 testType: serverTest,
247 name: "BadRSAVersion",
248 config: Config{
249 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
250 Bugs: ProtocolBugs{
251 RsaClientKeyExchangeVersion: VersionTLS11,
252 },
253 },
254 shouldFail: true,
255 expectedError: ":DECRYPTION_FAILED_OR_BAD_RECORD_MAC:",
256 },
257 {
David Benjamin325b5c32014-07-01 19:40:31 -0400258 name: "NoFallbackSCSV",
Adam Langleyac61fa32014-06-23 12:03:11 -0700259 config: Config{
260 Bugs: ProtocolBugs{
261 FailIfNotFallbackSCSV: true,
262 },
263 },
264 shouldFail: true,
265 expectedLocalError: "no fallback SCSV found",
266 },
David Benjamin325b5c32014-07-01 19:40:31 -0400267 {
David Benjamin2a0c4962014-08-22 23:46:35 -0400268 name: "SendFallbackSCSV",
David Benjamin325b5c32014-07-01 19:40:31 -0400269 config: Config{
270 Bugs: ProtocolBugs{
271 FailIfNotFallbackSCSV: true,
272 },
273 },
274 flags: []string{"-fallback-scsv"},
275 },
David Benjamin197b3ab2014-07-02 18:37:33 -0400276 {
David Benjamin7b030512014-07-08 17:30:11 -0400277 name: "ClientCertificateTypes",
278 config: Config{
279 ClientAuth: RequestClientCert,
280 ClientCertificateTypes: []byte{
281 CertTypeDSSSign,
282 CertTypeRSASign,
283 CertTypeECDSASign,
284 },
285 },
David Benjamin2561dc32014-08-24 01:25:27 -0400286 flags: []string{
287 "-expect-certificate-types",
288 base64.StdEncoding.EncodeToString([]byte{
289 CertTypeDSSSign,
290 CertTypeRSASign,
291 CertTypeECDSASign,
292 }),
293 },
David Benjamin7b030512014-07-08 17:30:11 -0400294 },
David Benjamin636293b2014-07-08 17:59:18 -0400295 {
296 name: "NoClientCertificate",
297 config: Config{
298 ClientAuth: RequireAnyClientCert,
299 },
300 shouldFail: true,
301 expectedLocalError: "client didn't provide a certificate",
302 },
David Benjamin1c375dd2014-07-12 00:48:23 -0400303 {
304 name: "UnauthenticatedECDH",
305 config: Config{
306 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
307 Bugs: ProtocolBugs{
308 UnauthenticatedECDH: true,
309 },
310 },
311 shouldFail: true,
David Benjamine8f3d662014-07-12 01:10:19 -0400312 expectedError: ":UNEXPECTED_MESSAGE:",
David Benjamin1c375dd2014-07-12 00:48:23 -0400313 },
David Benjamin9c651c92014-07-12 13:27:45 -0400314 {
David Benjamindcd979f2015-04-20 18:26:52 -0400315 name: "SkipCertificateStatus",
316 config: Config{
317 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
318 Bugs: ProtocolBugs{
319 SkipCertificateStatus: true,
320 },
321 },
322 flags: []string{
323 "-enable-ocsp-stapling",
324 },
325 },
326 {
David Benjamin9c651c92014-07-12 13:27:45 -0400327 name: "SkipServerKeyExchange",
328 config: Config{
329 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
330 Bugs: ProtocolBugs{
331 SkipServerKeyExchange: true,
332 },
333 },
334 shouldFail: true,
335 expectedError: ":UNEXPECTED_MESSAGE:",
336 },
David Benjamin1f5f62b2014-07-12 16:18:02 -0400337 {
David Benjamina0e52232014-07-19 17:39:58 -0400338 name: "SkipChangeCipherSpec-Client",
339 config: Config{
340 Bugs: ProtocolBugs{
341 SkipChangeCipherSpec: true,
342 },
343 },
344 shouldFail: true,
David Benjamin86271ee2014-07-21 16:14:03 -0400345 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
David Benjamina0e52232014-07-19 17:39:58 -0400346 },
347 {
348 testType: serverTest,
349 name: "SkipChangeCipherSpec-Server",
350 config: Config{
351 Bugs: ProtocolBugs{
352 SkipChangeCipherSpec: true,
353 },
354 },
355 shouldFail: true,
David Benjamin86271ee2014-07-21 16:14:03 -0400356 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
David Benjamina0e52232014-07-19 17:39:58 -0400357 },
David Benjamin42be6452014-07-21 14:50:23 -0400358 {
359 testType: serverTest,
360 name: "SkipChangeCipherSpec-Server-NPN",
361 config: Config{
362 NextProtos: []string{"bar"},
363 Bugs: ProtocolBugs{
364 SkipChangeCipherSpec: true,
365 },
366 },
367 flags: []string{
368 "-advertise-npn", "\x03foo\x03bar\x03baz",
369 },
370 shouldFail: true,
David Benjamin86271ee2014-07-21 16:14:03 -0400371 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
372 },
373 {
374 name: "FragmentAcrossChangeCipherSpec-Client",
375 config: Config{
376 Bugs: ProtocolBugs{
377 FragmentAcrossChangeCipherSpec: true,
378 },
379 },
380 shouldFail: true,
381 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
382 },
383 {
384 testType: serverTest,
385 name: "FragmentAcrossChangeCipherSpec-Server",
386 config: Config{
387 Bugs: ProtocolBugs{
388 FragmentAcrossChangeCipherSpec: true,
389 },
390 },
391 shouldFail: true,
392 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
393 },
394 {
395 testType: serverTest,
396 name: "FragmentAcrossChangeCipherSpec-Server-NPN",
397 config: Config{
398 NextProtos: []string{"bar"},
399 Bugs: ProtocolBugs{
400 FragmentAcrossChangeCipherSpec: true,
401 },
402 },
403 flags: []string{
404 "-advertise-npn", "\x03foo\x03bar\x03baz",
405 },
406 shouldFail: true,
407 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
David Benjamin42be6452014-07-21 14:50:23 -0400408 },
David Benjaminf3ec83d2014-07-21 22:42:34 -0400409 {
410 testType: serverTest,
David Benjamin3fd1fbd2015-02-03 16:07:32 -0500411 name: "Alert",
412 config: Config{
413 Bugs: ProtocolBugs{
414 SendSpuriousAlert: alertRecordOverflow,
415 },
416 },
417 shouldFail: true,
418 expectedError: ":TLSV1_ALERT_RECORD_OVERFLOW:",
419 },
420 {
421 protocol: dtls,
422 testType: serverTest,
423 name: "Alert-DTLS",
424 config: Config{
425 Bugs: ProtocolBugs{
426 SendSpuriousAlert: alertRecordOverflow,
427 },
428 },
429 shouldFail: true,
430 expectedError: ":TLSV1_ALERT_RECORD_OVERFLOW:",
431 },
432 {
433 testType: serverTest,
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400434 name: "FragmentAlert",
435 config: Config{
436 Bugs: ProtocolBugs{
David Benjaminca6c8262014-11-15 19:06:08 -0500437 FragmentAlert: true,
David Benjamin3fd1fbd2015-02-03 16:07:32 -0500438 SendSpuriousAlert: alertRecordOverflow,
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400439 },
440 },
441 shouldFail: true,
442 expectedError: ":BAD_ALERT:",
443 },
444 {
David Benjamin0ea8dda2015-01-31 20:33:40 -0500445 protocol: dtls,
446 testType: serverTest,
447 name: "FragmentAlert-DTLS",
448 config: Config{
449 Bugs: ProtocolBugs{
450 FragmentAlert: true,
David Benjamin3fd1fbd2015-02-03 16:07:32 -0500451 SendSpuriousAlert: alertRecordOverflow,
David Benjamin0ea8dda2015-01-31 20:33:40 -0500452 },
453 },
454 shouldFail: true,
455 expectedError: ":BAD_ALERT:",
456 },
457 {
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400458 testType: serverTest,
David Benjaminf3ec83d2014-07-21 22:42:34 -0400459 name: "EarlyChangeCipherSpec-server-1",
460 config: Config{
461 Bugs: ProtocolBugs{
462 EarlyChangeCipherSpec: 1,
463 },
464 },
465 shouldFail: true,
466 expectedError: ":CCS_RECEIVED_EARLY:",
467 },
468 {
469 testType: serverTest,
470 name: "EarlyChangeCipherSpec-server-2",
471 config: Config{
472 Bugs: ProtocolBugs{
473 EarlyChangeCipherSpec: 2,
474 },
475 },
476 shouldFail: true,
477 expectedError: ":CCS_RECEIVED_EARLY:",
478 },
David Benjamind23f4122014-07-23 15:09:48 -0400479 {
David Benjamind23f4122014-07-23 15:09:48 -0400480 name: "SkipNewSessionTicket",
481 config: Config{
482 Bugs: ProtocolBugs{
483 SkipNewSessionTicket: true,
484 },
485 },
486 shouldFail: true,
487 expectedError: ":CCS_RECEIVED_EARLY:",
488 },
David Benjamin7e3305e2014-07-28 14:52:32 -0400489 {
David Benjamind86c7672014-08-02 04:07:12 -0400490 testType: serverTest,
David Benjaminbef270a2014-08-02 04:22:02 -0400491 name: "FallbackSCSV",
492 config: Config{
493 MaxVersion: VersionTLS11,
494 Bugs: ProtocolBugs{
495 SendFallbackSCSV: true,
496 },
497 },
498 shouldFail: true,
499 expectedError: ":INAPPROPRIATE_FALLBACK:",
500 },
501 {
502 testType: serverTest,
503 name: "FallbackSCSV-VersionMatch",
504 config: Config{
505 Bugs: ProtocolBugs{
506 SendFallbackSCSV: true,
507 },
508 },
509 },
David Benjamin98214542014-08-07 18:02:39 -0400510 {
511 testType: serverTest,
512 name: "FragmentedClientVersion",
513 config: Config{
514 Bugs: ProtocolBugs{
515 MaxHandshakeRecordLength: 1,
516 FragmentClientVersion: true,
517 },
518 },
David Benjamin82c9e902014-12-12 15:55:27 -0500519 expectedVersion: VersionTLS12,
David Benjamin98214542014-08-07 18:02:39 -0400520 },
David Benjamin98e882e2014-08-08 13:24:34 -0400521 {
522 testType: serverTest,
523 name: "MinorVersionTolerance",
524 config: Config{
525 Bugs: ProtocolBugs{
526 SendClientVersion: 0x03ff,
527 },
528 },
529 expectedVersion: VersionTLS12,
530 },
531 {
532 testType: serverTest,
533 name: "MajorVersionTolerance",
534 config: Config{
535 Bugs: ProtocolBugs{
536 SendClientVersion: 0x0400,
537 },
538 },
539 expectedVersion: VersionTLS12,
540 },
541 {
542 testType: serverTest,
543 name: "VersionTooLow",
544 config: Config{
545 Bugs: ProtocolBugs{
546 SendClientVersion: 0x0200,
547 },
548 },
549 shouldFail: true,
550 expectedError: ":UNSUPPORTED_PROTOCOL:",
551 },
552 {
553 testType: serverTest,
554 name: "HttpGET",
555 sendPrefix: "GET / HTTP/1.0\n",
556 shouldFail: true,
557 expectedError: ":HTTP_REQUEST:",
558 },
559 {
560 testType: serverTest,
561 name: "HttpPOST",
562 sendPrefix: "POST / HTTP/1.0\n",
563 shouldFail: true,
564 expectedError: ":HTTP_REQUEST:",
565 },
566 {
567 testType: serverTest,
568 name: "HttpHEAD",
569 sendPrefix: "HEAD / HTTP/1.0\n",
570 shouldFail: true,
571 expectedError: ":HTTP_REQUEST:",
572 },
573 {
574 testType: serverTest,
575 name: "HttpPUT",
576 sendPrefix: "PUT / HTTP/1.0\n",
577 shouldFail: true,
578 expectedError: ":HTTP_REQUEST:",
579 },
580 {
581 testType: serverTest,
582 name: "HttpCONNECT",
583 sendPrefix: "CONNECT www.google.com:443 HTTP/1.0\n",
584 shouldFail: true,
585 expectedError: ":HTTPS_PROXY_REQUEST:",
586 },
David Benjamin39ebf532014-08-31 02:23:49 -0400587 {
David Benjaminf080ecd2014-12-11 18:48:59 -0500588 testType: serverTest,
589 name: "Garbage",
590 sendPrefix: "blah",
591 shouldFail: true,
592 expectedError: ":UNKNOWN_PROTOCOL:",
593 },
594 {
David Benjamin39ebf532014-08-31 02:23:49 -0400595 name: "SkipCipherVersionCheck",
596 config: Config{
597 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
598 MaxVersion: VersionTLS11,
599 Bugs: ProtocolBugs{
600 SkipCipherVersionCheck: true,
601 },
602 },
603 shouldFail: true,
604 expectedError: ":WRONG_CIPHER_RETURNED:",
605 },
David Benjamin9114fae2014-11-08 11:41:14 -0500606 {
David Benjamina3e89492015-02-26 15:16:22 -0500607 name: "RSAEphemeralKey",
David Benjamin9114fae2014-11-08 11:41:14 -0500608 config: Config{
609 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
610 Bugs: ProtocolBugs{
David Benjamina3e89492015-02-26 15:16:22 -0500611 RSAEphemeralKey: true,
David Benjamin9114fae2014-11-08 11:41:14 -0500612 },
613 },
614 shouldFail: true,
615 expectedError: ":UNEXPECTED_MESSAGE:",
616 },
David Benjamin128dbc32014-12-01 01:27:42 -0500617 {
618 name: "DisableEverything",
619 flags: []string{"-no-tls12", "-no-tls11", "-no-tls1", "-no-ssl3"},
620 shouldFail: true,
621 expectedError: ":WRONG_SSL_VERSION:",
622 },
623 {
624 protocol: dtls,
625 name: "DisableEverything-DTLS",
626 flags: []string{"-no-tls12", "-no-tls1"},
627 shouldFail: true,
628 expectedError: ":WRONG_SSL_VERSION:",
629 },
David Benjamin780d6dd2015-01-06 12:03:19 -0500630 {
631 name: "NoSharedCipher",
632 config: Config{
633 CipherSuites: []uint16{},
634 },
635 shouldFail: true,
636 expectedError: ":HANDSHAKE_FAILURE_ON_CLIENT_HELLO:",
637 },
David Benjamin13be1de2015-01-11 16:29:36 -0500638 {
639 protocol: dtls,
640 testType: serverTest,
641 name: "MTU",
642 config: Config{
643 Bugs: ProtocolBugs{
644 MaxPacketLength: 256,
645 },
646 },
647 flags: []string{"-mtu", "256"},
648 },
649 {
650 protocol: dtls,
651 testType: serverTest,
652 name: "MTUExceeded",
653 config: Config{
654 Bugs: ProtocolBugs{
655 MaxPacketLength: 255,
656 },
657 },
658 flags: []string{"-mtu", "256"},
659 shouldFail: true,
660 expectedLocalError: "dtls: exceeded maximum packet length",
661 },
David Benjamin6095de82014-12-27 01:50:38 -0500662 {
663 name: "CertMismatchRSA",
664 config: Config{
665 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
666 Certificates: []Certificate{getECDSACertificate()},
667 Bugs: ProtocolBugs{
668 SendCipherSuite: TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
669 },
670 },
671 shouldFail: true,
672 expectedError: ":WRONG_CERTIFICATE_TYPE:",
673 },
674 {
675 name: "CertMismatchECDSA",
676 config: Config{
677 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
678 Certificates: []Certificate{getRSACertificate()},
679 Bugs: ProtocolBugs{
680 SendCipherSuite: TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
681 },
682 },
683 shouldFail: true,
684 expectedError: ":WRONG_CERTIFICATE_TYPE:",
685 },
David Benjamin5fa3eba2015-01-22 16:35:40 -0500686 {
687 name: "TLSFatalBadPackets",
688 damageFirstWrite: true,
689 shouldFail: true,
690 expectedError: ":DECRYPTION_FAILED_OR_BAD_RECORD_MAC:",
691 },
692 {
693 protocol: dtls,
694 name: "DTLSIgnoreBadPackets",
695 damageFirstWrite: true,
696 },
697 {
698 protocol: dtls,
699 name: "DTLSIgnoreBadPackets-Async",
700 damageFirstWrite: true,
701 flags: []string{"-async"},
702 },
David Benjamin4189bd92015-01-25 23:52:39 -0500703 {
704 name: "AppDataAfterChangeCipherSpec",
705 config: Config{
706 Bugs: ProtocolBugs{
707 AppDataAfterChangeCipherSpec: []byte("TEST MESSAGE"),
708 },
709 },
710 shouldFail: true,
711 expectedError: ":DATA_BETWEEN_CCS_AND_FINISHED:",
712 },
713 {
714 protocol: dtls,
715 name: "AppDataAfterChangeCipherSpec-DTLS",
716 config: Config{
717 Bugs: ProtocolBugs{
718 AppDataAfterChangeCipherSpec: []byte("TEST MESSAGE"),
719 },
720 },
David Benjamin4417d052015-04-05 04:17:25 -0400721 // BoringSSL's DTLS implementation will drop the out-of-order
722 // application data.
David Benjamin4189bd92015-01-25 23:52:39 -0500723 },
David Benjaminb3774b92015-01-31 17:16:01 -0500724 {
David Benjamindc3da932015-03-12 15:09:02 -0400725 name: "AlertAfterChangeCipherSpec",
726 config: Config{
727 Bugs: ProtocolBugs{
728 AlertAfterChangeCipherSpec: alertRecordOverflow,
729 },
730 },
731 shouldFail: true,
732 expectedError: ":TLSV1_ALERT_RECORD_OVERFLOW:",
733 },
734 {
735 protocol: dtls,
736 name: "AlertAfterChangeCipherSpec-DTLS",
737 config: Config{
738 Bugs: ProtocolBugs{
739 AlertAfterChangeCipherSpec: alertRecordOverflow,
740 },
741 },
742 shouldFail: true,
743 expectedError: ":TLSV1_ALERT_RECORD_OVERFLOW:",
744 },
745 {
David Benjaminb3774b92015-01-31 17:16:01 -0500746 protocol: dtls,
747 name: "ReorderHandshakeFragments-Small-DTLS",
748 config: Config{
749 Bugs: ProtocolBugs{
750 ReorderHandshakeFragments: true,
751 // Small enough that every handshake message is
752 // fragmented.
753 MaxHandshakeRecordLength: 2,
754 },
755 },
756 },
757 {
758 protocol: dtls,
759 name: "ReorderHandshakeFragments-Large-DTLS",
760 config: Config{
761 Bugs: ProtocolBugs{
762 ReorderHandshakeFragments: true,
763 // Large enough that no handshake message is
764 // fragmented.
David Benjaminb3774b92015-01-31 17:16:01 -0500765 MaxHandshakeRecordLength: 2048,
766 },
767 },
768 },
David Benjaminddb9f152015-02-03 15:44:39 -0500769 {
David Benjamin75381222015-03-02 19:30:30 -0500770 protocol: dtls,
771 name: "MixCompleteMessageWithFragments-DTLS",
772 config: Config{
773 Bugs: ProtocolBugs{
774 ReorderHandshakeFragments: true,
775 MixCompleteMessageWithFragments: true,
776 MaxHandshakeRecordLength: 2,
777 },
778 },
779 },
780 {
David Benjaminddb9f152015-02-03 15:44:39 -0500781 name: "SendInvalidRecordType",
782 config: Config{
783 Bugs: ProtocolBugs{
784 SendInvalidRecordType: true,
785 },
786 },
787 shouldFail: true,
788 expectedError: ":UNEXPECTED_RECORD:",
789 },
790 {
791 protocol: dtls,
792 name: "SendInvalidRecordType-DTLS",
793 config: Config{
794 Bugs: ProtocolBugs{
795 SendInvalidRecordType: true,
796 },
797 },
798 shouldFail: true,
799 expectedError: ":UNEXPECTED_RECORD:",
800 },
David Benjaminb80168e2015-02-08 18:30:14 -0500801 {
802 name: "FalseStart-SkipServerSecondLeg",
803 config: Config{
804 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
805 NextProtos: []string{"foo"},
806 Bugs: ProtocolBugs{
807 SkipNewSessionTicket: true,
808 SkipChangeCipherSpec: true,
809 SkipFinished: true,
810 ExpectFalseStart: true,
811 },
812 },
813 flags: []string{
814 "-false-start",
David Benjamin87e4acd2015-04-02 19:57:35 -0400815 "-handshake-never-done",
David Benjaminb80168e2015-02-08 18:30:14 -0500816 "-advertise-alpn", "\x03foo",
817 },
818 shimWritesFirst: true,
819 shouldFail: true,
820 expectedError: ":UNEXPECTED_RECORD:",
821 },
David Benjamin931ab342015-02-08 19:46:57 -0500822 {
823 name: "FalseStart-SkipServerSecondLeg-Implicit",
824 config: Config{
825 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
826 NextProtos: []string{"foo"},
827 Bugs: ProtocolBugs{
828 SkipNewSessionTicket: true,
829 SkipChangeCipherSpec: true,
830 SkipFinished: true,
831 },
832 },
833 flags: []string{
834 "-implicit-handshake",
835 "-false-start",
David Benjamin87e4acd2015-04-02 19:57:35 -0400836 "-handshake-never-done",
David Benjamin931ab342015-02-08 19:46:57 -0500837 "-advertise-alpn", "\x03foo",
838 },
839 shouldFail: true,
840 expectedError: ":UNEXPECTED_RECORD:",
841 },
David Benjamin6f5c0f42015-02-24 01:23:21 -0500842 {
843 testType: serverTest,
844 name: "FailEarlyCallback",
845 flags: []string{"-fail-early-callback"},
846 shouldFail: true,
847 expectedError: ":CONNECTION_REJECTED:",
848 expectedLocalError: "remote error: access denied",
849 },
David Benjaminbcb2d912015-02-24 23:45:43 -0500850 {
851 name: "WrongMessageType",
852 config: Config{
853 Bugs: ProtocolBugs{
854 WrongCertificateMessageType: true,
855 },
856 },
857 shouldFail: true,
858 expectedError: ":UNEXPECTED_MESSAGE:",
859 expectedLocalError: "remote error: unexpected message",
860 },
861 {
862 protocol: dtls,
863 name: "WrongMessageType-DTLS",
864 config: Config{
865 Bugs: ProtocolBugs{
866 WrongCertificateMessageType: true,
867 },
868 },
869 shouldFail: true,
870 expectedError: ":UNEXPECTED_MESSAGE:",
871 expectedLocalError: "remote error: unexpected message",
872 },
David Benjamin75381222015-03-02 19:30:30 -0500873 {
874 protocol: dtls,
875 name: "FragmentMessageTypeMismatch-DTLS",
876 config: Config{
877 Bugs: ProtocolBugs{
878 MaxHandshakeRecordLength: 2,
879 FragmentMessageTypeMismatch: true,
880 },
881 },
882 shouldFail: true,
883 expectedError: ":FRAGMENT_MISMATCH:",
884 },
885 {
886 protocol: dtls,
887 name: "FragmentMessageLengthMismatch-DTLS",
888 config: Config{
889 Bugs: ProtocolBugs{
890 MaxHandshakeRecordLength: 2,
891 FragmentMessageLengthMismatch: true,
892 },
893 },
894 shouldFail: true,
895 expectedError: ":FRAGMENT_MISMATCH:",
896 },
897 {
898 protocol: dtls,
899 name: "SplitFragmentHeader-DTLS",
900 config: Config{
901 Bugs: ProtocolBugs{
902 SplitFragmentHeader: true,
903 },
904 },
905 shouldFail: true,
906 expectedError: ":UNEXPECTED_MESSAGE:",
907 },
908 {
909 protocol: dtls,
910 name: "SplitFragmentBody-DTLS",
911 config: Config{
912 Bugs: ProtocolBugs{
913 SplitFragmentBody: true,
914 },
915 },
916 shouldFail: true,
917 expectedError: ":UNEXPECTED_MESSAGE:",
918 },
919 {
920 protocol: dtls,
921 name: "SendEmptyFragments-DTLS",
922 config: Config{
923 Bugs: ProtocolBugs{
924 SendEmptyFragments: true,
925 },
926 },
927 },
David Benjamin67d1fb52015-03-16 15:16:23 -0400928 {
929 name: "UnsupportedCipherSuite",
930 config: Config{
931 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
932 Bugs: ProtocolBugs{
933 IgnorePeerCipherPreferences: true,
934 },
935 },
936 flags: []string{"-cipher", "DEFAULT:!RC4"},
937 shouldFail: true,
938 expectedError: ":WRONG_CIPHER_RETURNED:",
939 },
David Benjamin340d5ed2015-03-21 02:21:37 -0400940 {
David Benjaminc574f412015-04-20 11:13:01 -0400941 name: "UnsupportedCurve",
942 config: Config{
943 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
944 // BoringSSL implements P-224 but doesn't enable it by
945 // default.
946 CurvePreferences: []CurveID{CurveP224},
947 Bugs: ProtocolBugs{
948 IgnorePeerCurvePreferences: true,
949 },
950 },
951 shouldFail: true,
952 expectedError: ":WRONG_CURVE:",
953 },
954 {
David Benjamin340d5ed2015-03-21 02:21:37 -0400955 name: "SendWarningAlerts",
956 config: Config{
957 Bugs: ProtocolBugs{
958 SendWarningAlerts: alertAccessDenied,
959 },
960 },
961 },
962 {
963 protocol: dtls,
964 name: "SendWarningAlerts-DTLS",
965 config: Config{
966 Bugs: ProtocolBugs{
967 SendWarningAlerts: alertAccessDenied,
968 },
969 },
970 },
David Benjamin513f0ea2015-04-02 19:33:31 -0400971 {
972 name: "BadFinished",
973 config: Config{
974 Bugs: ProtocolBugs{
975 BadFinished: true,
976 },
977 },
978 shouldFail: true,
979 expectedError: ":DIGEST_CHECK_FAILED:",
980 },
981 {
982 name: "FalseStart-BadFinished",
983 config: Config{
984 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
985 NextProtos: []string{"foo"},
986 Bugs: ProtocolBugs{
987 BadFinished: true,
988 ExpectFalseStart: true,
989 },
990 },
991 flags: []string{
992 "-false-start",
David Benjamin87e4acd2015-04-02 19:57:35 -0400993 "-handshake-never-done",
David Benjamin513f0ea2015-04-02 19:33:31 -0400994 "-advertise-alpn", "\x03foo",
995 },
996 shimWritesFirst: true,
997 shouldFail: true,
998 expectedError: ":DIGEST_CHECK_FAILED:",
999 },
David Benjamin1c633152015-04-02 20:19:11 -04001000 {
1001 name: "NoFalseStart-NoALPN",
1002 config: Config{
1003 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1004 Bugs: ProtocolBugs{
1005 ExpectFalseStart: true,
1006 AlertBeforeFalseStartTest: alertAccessDenied,
1007 },
1008 },
1009 flags: []string{
1010 "-false-start",
1011 },
1012 shimWritesFirst: true,
1013 shouldFail: true,
1014 expectedError: ":TLSV1_ALERT_ACCESS_DENIED:",
1015 expectedLocalError: "tls: peer did not false start: EOF",
1016 },
1017 {
1018 name: "NoFalseStart-NoAEAD",
1019 config: Config{
1020 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1021 NextProtos: []string{"foo"},
1022 Bugs: ProtocolBugs{
1023 ExpectFalseStart: true,
1024 AlertBeforeFalseStartTest: alertAccessDenied,
1025 },
1026 },
1027 flags: []string{
1028 "-false-start",
1029 "-advertise-alpn", "\x03foo",
1030 },
1031 shimWritesFirst: true,
1032 shouldFail: true,
1033 expectedError: ":TLSV1_ALERT_ACCESS_DENIED:",
1034 expectedLocalError: "tls: peer did not false start: EOF",
1035 },
1036 {
1037 name: "NoFalseStart-RSA",
1038 config: Config{
1039 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
1040 NextProtos: []string{"foo"},
1041 Bugs: ProtocolBugs{
1042 ExpectFalseStart: true,
1043 AlertBeforeFalseStartTest: alertAccessDenied,
1044 },
1045 },
1046 flags: []string{
1047 "-false-start",
1048 "-advertise-alpn", "\x03foo",
1049 },
1050 shimWritesFirst: true,
1051 shouldFail: true,
1052 expectedError: ":TLSV1_ALERT_ACCESS_DENIED:",
1053 expectedLocalError: "tls: peer did not false start: EOF",
1054 },
1055 {
1056 name: "NoFalseStart-DHE_RSA",
1057 config: Config{
1058 CipherSuites: []uint16{TLS_DHE_RSA_WITH_AES_128_GCM_SHA256},
1059 NextProtos: []string{"foo"},
1060 Bugs: ProtocolBugs{
1061 ExpectFalseStart: true,
1062 AlertBeforeFalseStartTest: alertAccessDenied,
1063 },
1064 },
1065 flags: []string{
1066 "-false-start",
1067 "-advertise-alpn", "\x03foo",
1068 },
1069 shimWritesFirst: true,
1070 shouldFail: true,
1071 expectedError: ":TLSV1_ALERT_ACCESS_DENIED:",
1072 expectedLocalError: "tls: peer did not false start: EOF",
1073 },
David Benjamin55a43642015-04-20 14:45:55 -04001074 {
1075 testType: serverTest,
1076 name: "NoSupportedCurves",
1077 config: Config{
1078 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1079 Bugs: ProtocolBugs{
1080 NoSupportedCurves: true,
1081 },
1082 },
1083 },
David Benjamin90da8c82015-04-20 14:57:57 -04001084 {
1085 testType: serverTest,
1086 name: "NoCommonCurves",
1087 config: Config{
1088 CipherSuites: []uint16{
1089 TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
1090 TLS_DHE_RSA_WITH_AES_128_GCM_SHA256,
1091 },
1092 CurvePreferences: []CurveID{CurveP224},
1093 },
1094 expectedCipher: TLS_DHE_RSA_WITH_AES_128_GCM_SHA256,
1095 },
David Benjamin9a41d1b2015-05-16 01:30:09 -04001096 {
1097 protocol: dtls,
1098 name: "SendSplitAlert-Sync",
1099 config: Config{
1100 Bugs: ProtocolBugs{
1101 SendSplitAlert: true,
1102 },
1103 },
1104 },
1105 {
1106 protocol: dtls,
1107 name: "SendSplitAlert-Async",
1108 config: Config{
1109 Bugs: ProtocolBugs{
1110 SendSplitAlert: true,
1111 },
1112 },
1113 flags: []string{"-async"},
1114 },
David Benjaminbd15a8e2015-05-29 18:48:16 -04001115 {
1116 protocol: dtls,
1117 name: "PackDTLSHandshake",
1118 config: Config{
1119 Bugs: ProtocolBugs{
1120 MaxHandshakeRecordLength: 2,
1121 PackHandshakeFragments: 20,
1122 PackHandshakeRecords: 200,
1123 },
1124 },
1125 },
David Benjamin0fa40122015-05-30 17:13:12 -04001126 {
1127 testType: serverTest,
1128 protocol: dtls,
1129 name: "NoRC4-DTLS",
1130 config: Config{
1131 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_RC4_128_SHA},
1132 Bugs: ProtocolBugs{
1133 EnableAllCiphersInDTLS: true,
1134 },
1135 },
1136 shouldFail: true,
1137 expectedError: ":NO_SHARED_CIPHER:",
1138 },
Adam Langley95c29f32014-06-20 12:00:00 -07001139}
1140
David Benjamin01fe8202014-09-24 15:21:44 -04001141func doExchange(test *testCase, config *Config, conn net.Conn, messageLen int, isResume bool) error {
David Benjamin65ea8ff2014-11-23 03:01:00 -05001142 var connDebug *recordingConn
David Benjamin5fa3eba2015-01-22 16:35:40 -05001143 var connDamage *damageAdaptor
David Benjamin65ea8ff2014-11-23 03:01:00 -05001144 if *flagDebug {
1145 connDebug = &recordingConn{Conn: conn}
1146 conn = connDebug
1147 defer func() {
1148 connDebug.WriteTo(os.Stdout)
1149 }()
1150 }
1151
David Benjamin6fd297b2014-08-11 18:43:38 -04001152 if test.protocol == dtls {
David Benjamin83f90402015-01-27 01:09:43 -05001153 config.Bugs.PacketAdaptor = newPacketAdaptor(conn)
1154 conn = config.Bugs.PacketAdaptor
David Benjamin5e961c12014-11-07 01:48:35 -05001155 if test.replayWrites {
1156 conn = newReplayAdaptor(conn)
1157 }
David Benjamin6fd297b2014-08-11 18:43:38 -04001158 }
1159
David Benjamin5fa3eba2015-01-22 16:35:40 -05001160 if test.damageFirstWrite {
1161 connDamage = newDamageAdaptor(conn)
1162 conn = connDamage
1163 }
1164
David Benjamin6fd297b2014-08-11 18:43:38 -04001165 if test.sendPrefix != "" {
1166 if _, err := conn.Write([]byte(test.sendPrefix)); err != nil {
1167 return err
1168 }
David Benjamin98e882e2014-08-08 13:24:34 -04001169 }
1170
David Benjamin1d5c83e2014-07-22 19:20:02 -04001171 var tlsConn *Conn
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001172 if test.testType == clientTest {
David Benjamin6fd297b2014-08-11 18:43:38 -04001173 if test.protocol == dtls {
1174 tlsConn = DTLSServer(conn, config)
1175 } else {
1176 tlsConn = Server(conn, config)
1177 }
David Benjamin1d5c83e2014-07-22 19:20:02 -04001178 } else {
1179 config.InsecureSkipVerify = true
David Benjamin6fd297b2014-08-11 18:43:38 -04001180 if test.protocol == dtls {
1181 tlsConn = DTLSClient(conn, config)
1182 } else {
1183 tlsConn = Client(conn, config)
1184 }
David Benjamin1d5c83e2014-07-22 19:20:02 -04001185 }
1186
Adam Langley95c29f32014-06-20 12:00:00 -07001187 if err := tlsConn.Handshake(); err != nil {
1188 return err
1189 }
Kenny Root7fdeaf12014-08-05 15:23:37 -07001190
David Benjamin01fe8202014-09-24 15:21:44 -04001191 // TODO(davidben): move all per-connection expectations into a dedicated
1192 // expectations struct that can be specified separately for the two
1193 // legs.
1194 expectedVersion := test.expectedVersion
1195 if isResume && test.expectedResumeVersion != 0 {
1196 expectedVersion = test.expectedResumeVersion
1197 }
Adam Langleyb0eef0a2015-06-02 10:47:39 -07001198 connState := tlsConn.ConnectionState()
1199 if vers := connState.Version; expectedVersion != 0 && vers != expectedVersion {
David Benjamin01fe8202014-09-24 15:21:44 -04001200 return fmt.Errorf("got version %x, expected %x", vers, expectedVersion)
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001201 }
1202
Adam Langleyb0eef0a2015-06-02 10:47:39 -07001203 if cipher := connState.CipherSuite; test.expectedCipher != 0 && cipher != test.expectedCipher {
David Benjamin90da8c82015-04-20 14:57:57 -04001204 return fmt.Errorf("got cipher %x, expected %x", cipher, test.expectedCipher)
1205 }
Adam Langleyb0eef0a2015-06-02 10:47:39 -07001206 if didResume := connState.DidResume; isResume && didResume == test.expectResumeRejected {
1207 return fmt.Errorf("didResume is %t, but we expected the opposite", didResume)
1208 }
David Benjamin90da8c82015-04-20 14:57:57 -04001209
David Benjamina08e49d2014-08-24 01:46:07 -04001210 if test.expectChannelID {
Adam Langleyb0eef0a2015-06-02 10:47:39 -07001211 channelID := connState.ChannelID
David Benjamina08e49d2014-08-24 01:46:07 -04001212 if channelID == nil {
1213 return fmt.Errorf("no channel ID negotiated")
1214 }
1215 if channelID.Curve != channelIDKey.Curve ||
1216 channelIDKey.X.Cmp(channelIDKey.X) != 0 ||
1217 channelIDKey.Y.Cmp(channelIDKey.Y) != 0 {
1218 return fmt.Errorf("incorrect channel ID")
1219 }
1220 }
1221
David Benjaminae2888f2014-09-06 12:58:58 -04001222 if expected := test.expectedNextProto; expected != "" {
Adam Langleyb0eef0a2015-06-02 10:47:39 -07001223 if actual := connState.NegotiatedProtocol; actual != expected {
David Benjaminae2888f2014-09-06 12:58:58 -04001224 return fmt.Errorf("next proto mismatch: got %s, wanted %s", actual, expected)
1225 }
1226 }
1227
David Benjaminfc7b0862014-09-06 13:21:53 -04001228 if test.expectedNextProtoType != 0 {
Adam Langleyb0eef0a2015-06-02 10:47:39 -07001229 if (test.expectedNextProtoType == alpn) != connState.NegotiatedProtocolFromALPN {
David Benjaminfc7b0862014-09-06 13:21:53 -04001230 return fmt.Errorf("next proto type mismatch")
1231 }
1232 }
1233
Adam Langleyb0eef0a2015-06-02 10:47:39 -07001234 if p := connState.SRTPProtectionProfile; p != test.expectedSRTPProtectionProfile {
David Benjaminca6c8262014-11-15 19:06:08 -05001235 return fmt.Errorf("SRTP profile mismatch: got %d, wanted %d", p, test.expectedSRTPProtectionProfile)
1236 }
1237
David Benjaminc565ebb2015-04-03 04:06:36 -04001238 if test.exportKeyingMaterial > 0 {
1239 actual := make([]byte, test.exportKeyingMaterial)
1240 if _, err := io.ReadFull(tlsConn, actual); err != nil {
1241 return err
1242 }
1243 expected, err := tlsConn.ExportKeyingMaterial(test.exportKeyingMaterial, []byte(test.exportLabel), []byte(test.exportContext), test.useExportContext)
1244 if err != nil {
1245 return err
1246 }
1247 if !bytes.Equal(actual, expected) {
1248 return fmt.Errorf("keying material mismatch")
1249 }
1250 }
1251
Adam Langleyaf0e32c2015-06-03 09:57:23 -07001252 if test.testTLSUnique {
1253 var peersValue [12]byte
1254 if _, err := io.ReadFull(tlsConn, peersValue[:]); err != nil {
1255 return err
1256 }
1257 expected := tlsConn.ConnectionState().TLSUnique
1258 if !bytes.Equal(peersValue[:], expected) {
1259 return fmt.Errorf("tls-unique mismatch: peer sent %x, but %x was expected", peersValue[:], expected)
1260 }
1261 }
1262
David Benjamine58c4f52014-08-24 03:47:07 -04001263 if test.shimWritesFirst {
1264 var buf [5]byte
1265 _, err := io.ReadFull(tlsConn, buf[:])
1266 if err != nil {
1267 return err
1268 }
1269 if string(buf[:]) != "hello" {
1270 return fmt.Errorf("bad initial message")
1271 }
1272 }
1273
Adam Langleycf2d4f42014-10-28 19:06:14 -07001274 if test.renegotiate {
1275 if test.renegotiateCiphers != nil {
1276 config.CipherSuites = test.renegotiateCiphers
1277 }
1278 if err := tlsConn.Renegotiate(); err != nil {
1279 return err
1280 }
1281 } else if test.renegotiateCiphers != nil {
1282 panic("renegotiateCiphers without renegotiate")
1283 }
1284
David Benjamin5fa3eba2015-01-22 16:35:40 -05001285 if test.damageFirstWrite {
1286 connDamage.setDamage(true)
1287 tlsConn.Write([]byte("DAMAGED WRITE"))
1288 connDamage.setDamage(false)
1289 }
1290
Kenny Root7fdeaf12014-08-05 15:23:37 -07001291 if messageLen < 0 {
David Benjamin6fd297b2014-08-11 18:43:38 -04001292 if test.protocol == dtls {
1293 return fmt.Errorf("messageLen < 0 not supported for DTLS tests")
1294 }
Kenny Root7fdeaf12014-08-05 15:23:37 -07001295 // Read until EOF.
1296 _, err := io.Copy(ioutil.Discard, tlsConn)
1297 return err
1298 }
1299
David Benjamin4417d052015-04-05 04:17:25 -04001300 if messageLen == 0 {
1301 messageLen = 32
Adam Langley80842bd2014-06-20 12:00:00 -07001302 }
David Benjamin4417d052015-04-05 04:17:25 -04001303 testMessage := make([]byte, messageLen)
1304 for i := range testMessage {
1305 testMessage[i] = 0x42
1306 }
1307 tlsConn.Write(testMessage)
Adam Langley95c29f32014-06-20 12:00:00 -07001308
1309 buf := make([]byte, len(testMessage))
David Benjamin6fd297b2014-08-11 18:43:38 -04001310 if test.protocol == dtls {
1311 bufTmp := make([]byte, len(buf)+1)
1312 n, err := tlsConn.Read(bufTmp)
1313 if err != nil {
1314 return err
1315 }
1316 if n != len(buf) {
1317 return fmt.Errorf("bad reply; length mismatch (%d vs %d)", n, len(buf))
1318 }
1319 copy(buf, bufTmp)
1320 } else {
1321 _, err := io.ReadFull(tlsConn, buf)
1322 if err != nil {
1323 return err
1324 }
Adam Langley95c29f32014-06-20 12:00:00 -07001325 }
1326
1327 for i, v := range buf {
1328 if v != testMessage[i]^0xff {
1329 return fmt.Errorf("bad reply contents at byte %d", i)
1330 }
1331 }
1332
1333 return nil
1334}
1335
David Benjamin325b5c32014-07-01 19:40:31 -04001336func valgrindOf(dbAttach bool, path string, args ...string) *exec.Cmd {
1337 valgrindArgs := []string{"--error-exitcode=99", "--track-origins=yes", "--leak-check=full"}
Adam Langley95c29f32014-06-20 12:00:00 -07001338 if dbAttach {
David Benjamin325b5c32014-07-01 19:40:31 -04001339 valgrindArgs = append(valgrindArgs, "--db-attach=yes", "--db-command=xterm -e gdb -nw %f %p")
Adam Langley95c29f32014-06-20 12:00:00 -07001340 }
David Benjamin325b5c32014-07-01 19:40:31 -04001341 valgrindArgs = append(valgrindArgs, path)
1342 valgrindArgs = append(valgrindArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -07001343
David Benjamin325b5c32014-07-01 19:40:31 -04001344 return exec.Command("valgrind", valgrindArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -07001345}
1346
David Benjamin325b5c32014-07-01 19:40:31 -04001347func gdbOf(path string, args ...string) *exec.Cmd {
1348 xtermArgs := []string{"-e", "gdb", "--args"}
1349 xtermArgs = append(xtermArgs, path)
1350 xtermArgs = append(xtermArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -07001351
David Benjamin325b5c32014-07-01 19:40:31 -04001352 return exec.Command("xterm", xtermArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -07001353}
1354
Adam Langley69a01602014-11-17 17:26:55 -08001355type moreMallocsError struct{}
1356
1357func (moreMallocsError) Error() string {
1358 return "child process did not exhaust all allocation calls"
1359}
1360
1361var errMoreMallocs = moreMallocsError{}
1362
David Benjamin87c8a642015-02-21 01:54:29 -05001363// accept accepts a connection from listener, unless waitChan signals a process
1364// exit first.
1365func acceptOrWait(listener net.Listener, waitChan chan error) (net.Conn, error) {
1366 type connOrError struct {
1367 conn net.Conn
1368 err error
1369 }
1370 connChan := make(chan connOrError, 1)
1371 go func() {
1372 conn, err := listener.Accept()
1373 connChan <- connOrError{conn, err}
1374 close(connChan)
1375 }()
1376 select {
1377 case result := <-connChan:
1378 return result.conn, result.err
1379 case childErr := <-waitChan:
1380 waitChan <- childErr
1381 return nil, fmt.Errorf("child exited early: %s", childErr)
1382 }
1383}
1384
Adam Langley69a01602014-11-17 17:26:55 -08001385func runTest(test *testCase, buildDir string, mallocNumToFail int64) error {
Adam Langley38311732014-10-16 19:04:35 -07001386 if !test.shouldFail && (len(test.expectedError) > 0 || len(test.expectedLocalError) > 0) {
1387 panic("Error expected without shouldFail in " + test.name)
1388 }
1389
Adam Langleyb0eef0a2015-06-02 10:47:39 -07001390 if test.expectResumeRejected && !test.resumeSession {
1391 panic("expectResumeRejected without resumeSession in " + test.name)
1392 }
1393
David Benjamin87c8a642015-02-21 01:54:29 -05001394 listener, err := net.ListenTCP("tcp4", &net.TCPAddr{IP: net.IP{127, 0, 0, 1}})
1395 if err != nil {
1396 panic(err)
1397 }
1398 defer func() {
1399 if listener != nil {
1400 listener.Close()
1401 }
1402 }()
Adam Langley95c29f32014-06-20 12:00:00 -07001403
David Benjamin884fdf12014-08-02 15:28:23 -04001404 shim_path := path.Join(buildDir, "ssl/test/bssl_shim")
David Benjamin87c8a642015-02-21 01:54:29 -05001405 flags := []string{"-port", strconv.Itoa(listener.Addr().(*net.TCPAddr).Port)}
David Benjamin1d5c83e2014-07-22 19:20:02 -04001406 if test.testType == serverTest {
David Benjamin5a593af2014-08-11 19:51:50 -04001407 flags = append(flags, "-server")
1408
David Benjamin025b3d32014-07-01 19:53:04 -04001409 flags = append(flags, "-key-file")
1410 if test.keyFile == "" {
1411 flags = append(flags, rsaKeyFile)
1412 } else {
1413 flags = append(flags, test.keyFile)
1414 }
1415
1416 flags = append(flags, "-cert-file")
1417 if test.certFile == "" {
1418 flags = append(flags, rsaCertificateFile)
1419 } else {
1420 flags = append(flags, test.certFile)
1421 }
1422 }
David Benjamin5a593af2014-08-11 19:51:50 -04001423
David Benjamin6fd297b2014-08-11 18:43:38 -04001424 if test.protocol == dtls {
1425 flags = append(flags, "-dtls")
1426 }
1427
David Benjamin5a593af2014-08-11 19:51:50 -04001428 if test.resumeSession {
1429 flags = append(flags, "-resume")
1430 }
1431
David Benjamine58c4f52014-08-24 03:47:07 -04001432 if test.shimWritesFirst {
1433 flags = append(flags, "-shim-writes-first")
1434 }
1435
David Benjaminc565ebb2015-04-03 04:06:36 -04001436 if test.exportKeyingMaterial > 0 {
1437 flags = append(flags, "-export-keying-material", strconv.Itoa(test.exportKeyingMaterial))
1438 flags = append(flags, "-export-label", test.exportLabel)
1439 flags = append(flags, "-export-context", test.exportContext)
1440 if test.useExportContext {
1441 flags = append(flags, "-use-export-context")
1442 }
1443 }
Adam Langleyb0eef0a2015-06-02 10:47:39 -07001444 if test.expectResumeRejected {
1445 flags = append(flags, "-expect-session-miss")
1446 }
David Benjaminc565ebb2015-04-03 04:06:36 -04001447
Adam Langleyaf0e32c2015-06-03 09:57:23 -07001448 if test.testTLSUnique {
1449 flags = append(flags, "-tls-unique")
1450 }
1451
David Benjamin025b3d32014-07-01 19:53:04 -04001452 flags = append(flags, test.flags...)
1453
1454 var shim *exec.Cmd
1455 if *useValgrind {
1456 shim = valgrindOf(false, shim_path, flags...)
Adam Langley75712922014-10-10 16:23:43 -07001457 } else if *useGDB {
1458 shim = gdbOf(shim_path, flags...)
David Benjamin025b3d32014-07-01 19:53:04 -04001459 } else {
1460 shim = exec.Command(shim_path, flags...)
1461 }
David Benjamin025b3d32014-07-01 19:53:04 -04001462 shim.Stdin = os.Stdin
1463 var stdoutBuf, stderrBuf bytes.Buffer
1464 shim.Stdout = &stdoutBuf
1465 shim.Stderr = &stderrBuf
Adam Langley69a01602014-11-17 17:26:55 -08001466 if mallocNumToFail >= 0 {
David Benjamin9e128b02015-02-09 13:13:09 -05001467 shim.Env = os.Environ()
1468 shim.Env = append(shim.Env, "MALLOC_NUMBER_TO_FAIL="+strconv.FormatInt(mallocNumToFail, 10))
Adam Langley69a01602014-11-17 17:26:55 -08001469 if *mallocTestDebug {
1470 shim.Env = append(shim.Env, "MALLOC_ABORT_ON_FAIL=1")
1471 }
1472 shim.Env = append(shim.Env, "_MALLOC_CHECK=1")
1473 }
David Benjamin025b3d32014-07-01 19:53:04 -04001474
1475 if err := shim.Start(); err != nil {
Adam Langley95c29f32014-06-20 12:00:00 -07001476 panic(err)
1477 }
David Benjamin87c8a642015-02-21 01:54:29 -05001478 waitChan := make(chan error, 1)
1479 go func() { waitChan <- shim.Wait() }()
Adam Langley95c29f32014-06-20 12:00:00 -07001480
1481 config := test.config
David Benjamin1d5c83e2014-07-22 19:20:02 -04001482 config.ClientSessionCache = NewLRUClientSessionCache(1)
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001483 config.ServerSessionCache = NewLRUServerSessionCache(1)
David Benjamin025b3d32014-07-01 19:53:04 -04001484 if test.testType == clientTest {
1485 if len(config.Certificates) == 0 {
1486 config.Certificates = []Certificate{getRSACertificate()}
1487 }
David Benjamin87c8a642015-02-21 01:54:29 -05001488 } else {
1489 // Supply a ServerName to ensure a constant session cache key,
1490 // rather than falling back to net.Conn.RemoteAddr.
1491 if len(config.ServerName) == 0 {
1492 config.ServerName = "test"
1493 }
David Benjamin025b3d32014-07-01 19:53:04 -04001494 }
Adam Langley95c29f32014-06-20 12:00:00 -07001495
David Benjamin87c8a642015-02-21 01:54:29 -05001496 conn, err := acceptOrWait(listener, waitChan)
1497 if err == nil {
1498 err = doExchange(test, &config, conn, test.messageLen, false /* not a resumption */)
1499 conn.Close()
1500 }
David Benjamin65ea8ff2014-11-23 03:01:00 -05001501
David Benjamin1d5c83e2014-07-22 19:20:02 -04001502 if err == nil && test.resumeSession {
David Benjamin01fe8202014-09-24 15:21:44 -04001503 var resumeConfig Config
1504 if test.resumeConfig != nil {
1505 resumeConfig = *test.resumeConfig
David Benjamin87c8a642015-02-21 01:54:29 -05001506 if len(resumeConfig.ServerName) == 0 {
1507 resumeConfig.ServerName = config.ServerName
1508 }
David Benjamin01fe8202014-09-24 15:21:44 -04001509 if len(resumeConfig.Certificates) == 0 {
1510 resumeConfig.Certificates = []Certificate{getRSACertificate()}
1511 }
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001512 if !test.newSessionsOnResume {
1513 resumeConfig.SessionTicketKey = config.SessionTicketKey
1514 resumeConfig.ClientSessionCache = config.ClientSessionCache
1515 resumeConfig.ServerSessionCache = config.ServerSessionCache
1516 }
David Benjamin01fe8202014-09-24 15:21:44 -04001517 } else {
1518 resumeConfig = config
1519 }
David Benjamin87c8a642015-02-21 01:54:29 -05001520 var connResume net.Conn
1521 connResume, err = acceptOrWait(listener, waitChan)
1522 if err == nil {
1523 err = doExchange(test, &resumeConfig, connResume, test.messageLen, true /* resumption */)
1524 connResume.Close()
1525 }
David Benjamin1d5c83e2014-07-22 19:20:02 -04001526 }
1527
David Benjamin87c8a642015-02-21 01:54:29 -05001528 // Close the listener now. This is to avoid hangs should the shim try to
1529 // open more connections than expected.
1530 listener.Close()
1531 listener = nil
1532
1533 childErr := <-waitChan
Adam Langley69a01602014-11-17 17:26:55 -08001534 if exitError, ok := childErr.(*exec.ExitError); ok {
1535 if exitError.Sys().(syscall.WaitStatus).ExitStatus() == 88 {
1536 return errMoreMallocs
1537 }
1538 }
Adam Langley95c29f32014-06-20 12:00:00 -07001539
1540 stdout := string(stdoutBuf.Bytes())
1541 stderr := string(stderrBuf.Bytes())
1542 failed := err != nil || childErr != nil
David Benjaminc565ebb2015-04-03 04:06:36 -04001543 correctFailure := len(test.expectedError) == 0 || strings.Contains(stderr, test.expectedError)
Adam Langleyac61fa32014-06-23 12:03:11 -07001544 localError := "none"
1545 if err != nil {
1546 localError = err.Error()
1547 }
1548 if len(test.expectedLocalError) != 0 {
1549 correctFailure = correctFailure && strings.Contains(localError, test.expectedLocalError)
1550 }
Adam Langley95c29f32014-06-20 12:00:00 -07001551
1552 if failed != test.shouldFail || failed && !correctFailure {
Adam Langley95c29f32014-06-20 12:00:00 -07001553 childError := "none"
Adam Langley95c29f32014-06-20 12:00:00 -07001554 if childErr != nil {
1555 childError = childErr.Error()
1556 }
1557
1558 var msg string
1559 switch {
1560 case failed && !test.shouldFail:
1561 msg = "unexpected failure"
1562 case !failed && test.shouldFail:
1563 msg = "unexpected success"
1564 case failed && !correctFailure:
Adam Langleyac61fa32014-06-23 12:03:11 -07001565 msg = "bad error (wanted '" + test.expectedError + "' / '" + test.expectedLocalError + "')"
Adam Langley95c29f32014-06-20 12:00:00 -07001566 default:
1567 panic("internal error")
1568 }
1569
David Benjaminc565ebb2015-04-03 04:06:36 -04001570 return fmt.Errorf("%s: local error '%s', child error '%s', stdout:\n%s\nstderr:\n%s", msg, localError, childError, stdout, stderr)
Adam Langley95c29f32014-06-20 12:00:00 -07001571 }
1572
David Benjaminc565ebb2015-04-03 04:06:36 -04001573 if !*useValgrind && !failed && len(stderr) > 0 {
Adam Langley95c29f32014-06-20 12:00:00 -07001574 println(stderr)
1575 }
1576
1577 return nil
1578}
1579
1580var tlsVersions = []struct {
1581 name string
1582 version uint16
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001583 flag string
David Benjamin8b8c0062014-11-23 02:47:52 -05001584 hasDTLS bool
Adam Langley95c29f32014-06-20 12:00:00 -07001585}{
David Benjamin8b8c0062014-11-23 02:47:52 -05001586 {"SSL3", VersionSSL30, "-no-ssl3", false},
1587 {"TLS1", VersionTLS10, "-no-tls1", true},
1588 {"TLS11", VersionTLS11, "-no-tls11", false},
1589 {"TLS12", VersionTLS12, "-no-tls12", true},
Adam Langley95c29f32014-06-20 12:00:00 -07001590}
1591
1592var testCipherSuites = []struct {
1593 name string
1594 id uint16
1595}{
1596 {"3DES-SHA", TLS_RSA_WITH_3DES_EDE_CBC_SHA},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001597 {"AES128-GCM", TLS_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001598 {"AES128-SHA", TLS_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001599 {"AES128-SHA256", TLS_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001600 {"AES256-GCM", TLS_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001601 {"AES256-SHA", TLS_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001602 {"AES256-SHA256", TLS_RSA_WITH_AES_256_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001603 {"DHE-RSA-AES128-GCM", TLS_DHE_RSA_WITH_AES_128_GCM_SHA256},
1604 {"DHE-RSA-AES128-SHA", TLS_DHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001605 {"DHE-RSA-AES128-SHA256", TLS_DHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001606 {"DHE-RSA-AES256-GCM", TLS_DHE_RSA_WITH_AES_256_GCM_SHA384},
1607 {"DHE-RSA-AES256-SHA", TLS_DHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001608 {"DHE-RSA-AES256-SHA256", TLS_DHE_RSA_WITH_AES_256_CBC_SHA256},
David Benjamine9a80ff2015-04-07 00:46:46 -04001609 {"DHE-RSA-CHACHA20-POLY1305", TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001610 {"ECDHE-ECDSA-AES128-GCM", TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
1611 {"ECDHE-ECDSA-AES128-SHA", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001612 {"ECDHE-ECDSA-AES128-SHA256", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256},
1613 {"ECDHE-ECDSA-AES256-GCM", TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001614 {"ECDHE-ECDSA-AES256-SHA", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001615 {"ECDHE-ECDSA-AES256-SHA384", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384},
David Benjamine9a80ff2015-04-07 00:46:46 -04001616 {"ECDHE-ECDSA-CHACHA20-POLY1305", TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001617 {"ECDHE-ECDSA-RC4-SHA", TLS_ECDHE_ECDSA_WITH_RC4_128_SHA},
Adam Langley97e8ba82015-04-29 15:32:10 -07001618 {"ECDHE-PSK-AES128-GCM-SHA256", TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001619 {"ECDHE-RSA-AES128-GCM", TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001620 {"ECDHE-RSA-AES128-SHA", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001621 {"ECDHE-RSA-AES128-SHA256", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001622 {"ECDHE-RSA-AES256-GCM", TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001623 {"ECDHE-RSA-AES256-SHA", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001624 {"ECDHE-RSA-AES256-SHA384", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384},
David Benjamine9a80ff2015-04-07 00:46:46 -04001625 {"ECDHE-RSA-CHACHA20-POLY1305", TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001626 {"ECDHE-RSA-RC4-SHA", TLS_ECDHE_RSA_WITH_RC4_128_SHA},
David Benjamin48cae082014-10-27 01:06:24 -04001627 {"PSK-AES128-CBC-SHA", TLS_PSK_WITH_AES_128_CBC_SHA},
1628 {"PSK-AES256-CBC-SHA", TLS_PSK_WITH_AES_256_CBC_SHA},
1629 {"PSK-RC4-SHA", TLS_PSK_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -07001630 {"RC4-MD5", TLS_RSA_WITH_RC4_128_MD5},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001631 {"RC4-SHA", TLS_RSA_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -07001632}
1633
David Benjamin8b8c0062014-11-23 02:47:52 -05001634func hasComponent(suiteName, component string) bool {
1635 return strings.Contains("-"+suiteName+"-", "-"+component+"-")
1636}
1637
David Benjaminf7768e42014-08-31 02:06:47 -04001638func isTLS12Only(suiteName string) bool {
David Benjamin8b8c0062014-11-23 02:47:52 -05001639 return hasComponent(suiteName, "GCM") ||
1640 hasComponent(suiteName, "SHA256") ||
David Benjamine9a80ff2015-04-07 00:46:46 -04001641 hasComponent(suiteName, "SHA384") ||
1642 hasComponent(suiteName, "POLY1305")
David Benjamin8b8c0062014-11-23 02:47:52 -05001643}
1644
1645func isDTLSCipher(suiteName string) bool {
David Benjamine95d20d2014-12-23 11:16:01 -05001646 return !hasComponent(suiteName, "RC4")
David Benjaminf7768e42014-08-31 02:06:47 -04001647}
1648
Adam Langleya7997f12015-05-14 17:38:50 -07001649func bigFromHex(hex string) *big.Int {
1650 ret, ok := new(big.Int).SetString(hex, 16)
1651 if !ok {
1652 panic("failed to parse hex number 0x" + hex)
1653 }
1654 return ret
1655}
1656
Adam Langley95c29f32014-06-20 12:00:00 -07001657func addCipherSuiteTests() {
1658 for _, suite := range testCipherSuites {
David Benjamin48cae082014-10-27 01:06:24 -04001659 const psk = "12345"
1660 const pskIdentity = "luggage combo"
1661
Adam Langley95c29f32014-06-20 12:00:00 -07001662 var cert Certificate
David Benjamin025b3d32014-07-01 19:53:04 -04001663 var certFile string
1664 var keyFile string
David Benjamin8b8c0062014-11-23 02:47:52 -05001665 if hasComponent(suite.name, "ECDSA") {
Adam Langley95c29f32014-06-20 12:00:00 -07001666 cert = getECDSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -04001667 certFile = ecdsaCertificateFile
1668 keyFile = ecdsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -07001669 } else {
1670 cert = getRSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -04001671 certFile = rsaCertificateFile
1672 keyFile = rsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -07001673 }
1674
David Benjamin48cae082014-10-27 01:06:24 -04001675 var flags []string
David Benjamin8b8c0062014-11-23 02:47:52 -05001676 if hasComponent(suite.name, "PSK") {
David Benjamin48cae082014-10-27 01:06:24 -04001677 flags = append(flags,
1678 "-psk", psk,
1679 "-psk-identity", pskIdentity)
1680 }
1681
Adam Langley95c29f32014-06-20 12:00:00 -07001682 for _, ver := range tlsVersions {
David Benjaminf7768e42014-08-31 02:06:47 -04001683 if ver.version < VersionTLS12 && isTLS12Only(suite.name) {
Adam Langley95c29f32014-06-20 12:00:00 -07001684 continue
1685 }
1686
David Benjamin025b3d32014-07-01 19:53:04 -04001687 testCases = append(testCases, testCase{
1688 testType: clientTest,
1689 name: ver.name + "-" + suite.name + "-client",
Adam Langley95c29f32014-06-20 12:00:00 -07001690 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001691 MinVersion: ver.version,
1692 MaxVersion: ver.version,
1693 CipherSuites: []uint16{suite.id},
1694 Certificates: []Certificate{cert},
David Benjamin68793732015-05-04 20:20:48 -04001695 PreSharedKey: []byte(psk),
David Benjamin48cae082014-10-27 01:06:24 -04001696 PreSharedKeyIdentity: pskIdentity,
Adam Langley95c29f32014-06-20 12:00:00 -07001697 },
David Benjamin48cae082014-10-27 01:06:24 -04001698 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001699 resumeSession: true,
Adam Langley95c29f32014-06-20 12:00:00 -07001700 })
David Benjamin025b3d32014-07-01 19:53:04 -04001701
David Benjamin76d8abe2014-08-14 16:25:34 -04001702 testCases = append(testCases, testCase{
1703 testType: serverTest,
1704 name: ver.name + "-" + suite.name + "-server",
1705 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001706 MinVersion: ver.version,
1707 MaxVersion: ver.version,
1708 CipherSuites: []uint16{suite.id},
1709 Certificates: []Certificate{cert},
1710 PreSharedKey: []byte(psk),
1711 PreSharedKeyIdentity: pskIdentity,
David Benjamin76d8abe2014-08-14 16:25:34 -04001712 },
1713 certFile: certFile,
1714 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001715 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001716 resumeSession: true,
David Benjamin76d8abe2014-08-14 16:25:34 -04001717 })
David Benjamin6fd297b2014-08-11 18:43:38 -04001718
David Benjamin8b8c0062014-11-23 02:47:52 -05001719 if ver.hasDTLS && isDTLSCipher(suite.name) {
David Benjamin6fd297b2014-08-11 18:43:38 -04001720 testCases = append(testCases, testCase{
1721 testType: clientTest,
1722 protocol: dtls,
1723 name: "D" + ver.name + "-" + suite.name + "-client",
1724 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001725 MinVersion: ver.version,
1726 MaxVersion: ver.version,
1727 CipherSuites: []uint16{suite.id},
1728 Certificates: []Certificate{cert},
1729 PreSharedKey: []byte(psk),
1730 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001731 },
David Benjamin48cae082014-10-27 01:06:24 -04001732 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001733 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001734 })
1735 testCases = append(testCases, testCase{
1736 testType: serverTest,
1737 protocol: dtls,
1738 name: "D" + ver.name + "-" + suite.name + "-server",
1739 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001740 MinVersion: ver.version,
1741 MaxVersion: ver.version,
1742 CipherSuites: []uint16{suite.id},
1743 Certificates: []Certificate{cert},
1744 PreSharedKey: []byte(psk),
1745 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001746 },
1747 certFile: certFile,
1748 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001749 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001750 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001751 })
1752 }
Adam Langley95c29f32014-06-20 12:00:00 -07001753 }
1754 }
Adam Langleya7997f12015-05-14 17:38:50 -07001755
1756 testCases = append(testCases, testCase{
1757 name: "WeakDH",
1758 config: Config{
1759 CipherSuites: []uint16{TLS_DHE_RSA_WITH_AES_128_GCM_SHA256},
1760 Bugs: ProtocolBugs{
1761 // This is a 1023-bit prime number, generated
1762 // with:
1763 // openssl gendh 1023 | openssl asn1parse -i
1764 DHGroupPrime: bigFromHex("518E9B7930CE61C6E445C8360584E5FC78D9137C0FFDC880B495D5338ADF7689951A6821C17A76B3ACB8E0156AEA607B7EC406EBEDBB84D8376EB8FE8F8BA1433488BEE0C3EDDFD3A32DBB9481980A7AF6C96BFCF490A094CFFB2B8192C1BB5510B77B658436E27C2D4D023FE3718222AB0CA1273995B51F6D625A4944D0DD4B"),
1765 },
1766 },
1767 shouldFail: true,
1768 expectedError: "BAD_DH_P_LENGTH",
1769 })
Adam Langley95c29f32014-06-20 12:00:00 -07001770}
1771
1772func addBadECDSASignatureTests() {
1773 for badR := BadValue(1); badR < NumBadValues; badR++ {
1774 for badS := BadValue(1); badS < NumBadValues; badS++ {
David Benjamin025b3d32014-07-01 19:53:04 -04001775 testCases = append(testCases, testCase{
Adam Langley95c29f32014-06-20 12:00:00 -07001776 name: fmt.Sprintf("BadECDSA-%d-%d", badR, badS),
1777 config: Config{
1778 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
1779 Certificates: []Certificate{getECDSACertificate()},
1780 Bugs: ProtocolBugs{
1781 BadECDSAR: badR,
1782 BadECDSAS: badS,
1783 },
1784 },
1785 shouldFail: true,
1786 expectedError: "SIGNATURE",
1787 })
1788 }
1789 }
1790}
1791
Adam Langley80842bd2014-06-20 12:00:00 -07001792func addCBCPaddingTests() {
David Benjamin025b3d32014-07-01 19:53:04 -04001793 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001794 name: "MaxCBCPadding",
1795 config: Config{
1796 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1797 Bugs: ProtocolBugs{
1798 MaxPadding: true,
1799 },
1800 },
1801 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1802 })
David Benjamin025b3d32014-07-01 19:53:04 -04001803 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001804 name: "BadCBCPadding",
1805 config: Config{
1806 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1807 Bugs: ProtocolBugs{
1808 PaddingFirstByteBad: true,
1809 },
1810 },
1811 shouldFail: true,
1812 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1813 })
1814 // OpenSSL previously had an issue where the first byte of padding in
1815 // 255 bytes of padding wasn't checked.
David Benjamin025b3d32014-07-01 19:53:04 -04001816 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001817 name: "BadCBCPadding255",
1818 config: Config{
1819 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1820 Bugs: ProtocolBugs{
1821 MaxPadding: true,
1822 PaddingFirstByteBadIf255: true,
1823 },
1824 },
1825 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1826 shouldFail: true,
1827 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1828 })
1829}
1830
Kenny Root7fdeaf12014-08-05 15:23:37 -07001831func addCBCSplittingTests() {
1832 testCases = append(testCases, testCase{
1833 name: "CBCRecordSplitting",
1834 config: Config{
1835 MaxVersion: VersionTLS10,
1836 MinVersion: VersionTLS10,
1837 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1838 },
1839 messageLen: -1, // read until EOF
1840 flags: []string{
1841 "-async",
1842 "-write-different-record-sizes",
1843 "-cbc-record-splitting",
1844 },
David Benjamina8e3e0e2014-08-06 22:11:10 -04001845 })
1846 testCases = append(testCases, testCase{
Kenny Root7fdeaf12014-08-05 15:23:37 -07001847 name: "CBCRecordSplittingPartialWrite",
1848 config: Config{
1849 MaxVersion: VersionTLS10,
1850 MinVersion: VersionTLS10,
1851 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1852 },
1853 messageLen: -1, // read until EOF
1854 flags: []string{
1855 "-async",
1856 "-write-different-record-sizes",
1857 "-cbc-record-splitting",
1858 "-partial-write",
1859 },
1860 })
1861}
1862
David Benjamin636293b2014-07-08 17:59:18 -04001863func addClientAuthTests() {
David Benjamin407a10c2014-07-16 12:58:59 -04001864 // Add a dummy cert pool to stress certificate authority parsing.
1865 // TODO(davidben): Add tests that those values parse out correctly.
1866 certPool := x509.NewCertPool()
1867 cert, err := x509.ParseCertificate(rsaCertificate.Certificate[0])
1868 if err != nil {
1869 panic(err)
1870 }
1871 certPool.AddCert(cert)
1872
David Benjamin636293b2014-07-08 17:59:18 -04001873 for _, ver := range tlsVersions {
David Benjamin636293b2014-07-08 17:59:18 -04001874 testCases = append(testCases, testCase{
1875 testType: clientTest,
David Benjamin67666e72014-07-12 15:47:52 -04001876 name: ver.name + "-Client-ClientAuth-RSA",
David Benjamin636293b2014-07-08 17:59:18 -04001877 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001878 MinVersion: ver.version,
1879 MaxVersion: ver.version,
1880 ClientAuth: RequireAnyClientCert,
1881 ClientCAs: certPool,
David Benjamin636293b2014-07-08 17:59:18 -04001882 },
1883 flags: []string{
1884 "-cert-file", rsaCertificateFile,
1885 "-key-file", rsaKeyFile,
1886 },
1887 })
1888 testCases = append(testCases, testCase{
David Benjamin67666e72014-07-12 15:47:52 -04001889 testType: serverTest,
1890 name: ver.name + "-Server-ClientAuth-RSA",
1891 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001892 MinVersion: ver.version,
1893 MaxVersion: ver.version,
David Benjamin67666e72014-07-12 15:47:52 -04001894 Certificates: []Certificate{rsaCertificate},
1895 },
1896 flags: []string{"-require-any-client-certificate"},
1897 })
David Benjamine098ec22014-08-27 23:13:20 -04001898 if ver.version != VersionSSL30 {
1899 testCases = append(testCases, testCase{
1900 testType: serverTest,
1901 name: ver.name + "-Server-ClientAuth-ECDSA",
1902 config: Config{
1903 MinVersion: ver.version,
1904 MaxVersion: ver.version,
1905 Certificates: []Certificate{ecdsaCertificate},
1906 },
1907 flags: []string{"-require-any-client-certificate"},
1908 })
1909 testCases = append(testCases, testCase{
1910 testType: clientTest,
1911 name: ver.name + "-Client-ClientAuth-ECDSA",
1912 config: Config{
1913 MinVersion: ver.version,
1914 MaxVersion: ver.version,
1915 ClientAuth: RequireAnyClientCert,
1916 ClientCAs: certPool,
1917 },
1918 flags: []string{
1919 "-cert-file", ecdsaCertificateFile,
1920 "-key-file", ecdsaKeyFile,
1921 },
1922 })
1923 }
David Benjamin636293b2014-07-08 17:59:18 -04001924 }
1925}
1926
Adam Langley75712922014-10-10 16:23:43 -07001927func addExtendedMasterSecretTests() {
1928 const expectEMSFlag = "-expect-extended-master-secret"
1929
1930 for _, with := range []bool{false, true} {
1931 prefix := "No"
1932 var flags []string
1933 if with {
1934 prefix = ""
1935 flags = []string{expectEMSFlag}
1936 }
1937
1938 for _, isClient := range []bool{false, true} {
1939 suffix := "-Server"
1940 testType := serverTest
1941 if isClient {
1942 suffix = "-Client"
1943 testType = clientTest
1944 }
1945
1946 for _, ver := range tlsVersions {
1947 test := testCase{
1948 testType: testType,
1949 name: prefix + "ExtendedMasterSecret-" + ver.name + suffix,
1950 config: Config{
1951 MinVersion: ver.version,
1952 MaxVersion: ver.version,
1953 Bugs: ProtocolBugs{
1954 NoExtendedMasterSecret: !with,
1955 RequireExtendedMasterSecret: with,
1956 },
1957 },
David Benjamin48cae082014-10-27 01:06:24 -04001958 flags: flags,
1959 shouldFail: ver.version == VersionSSL30 && with,
Adam Langley75712922014-10-10 16:23:43 -07001960 }
1961 if test.shouldFail {
1962 test.expectedLocalError = "extended master secret required but not supported by peer"
1963 }
1964 testCases = append(testCases, test)
1965 }
1966 }
1967 }
1968
Adam Langleyba5934b2015-06-02 10:50:35 -07001969 for _, isClient := range []bool{false, true} {
1970 for _, supportedInFirstConnection := range []bool{false, true} {
1971 for _, supportedInResumeConnection := range []bool{false, true} {
1972 boolToWord := func(b bool) string {
1973 if b {
1974 return "Yes"
1975 }
1976 return "No"
1977 }
1978 suffix := boolToWord(supportedInFirstConnection) + "To" + boolToWord(supportedInResumeConnection) + "-"
1979 if isClient {
1980 suffix += "Client"
1981 } else {
1982 suffix += "Server"
1983 }
1984
1985 supportedConfig := Config{
1986 Bugs: ProtocolBugs{
1987 RequireExtendedMasterSecret: true,
1988 },
1989 }
1990
1991 noSupportConfig := Config{
1992 Bugs: ProtocolBugs{
1993 NoExtendedMasterSecret: true,
1994 },
1995 }
1996
1997 test := testCase{
1998 name: "ExtendedMasterSecret-" + suffix,
1999 resumeSession: true,
2000 }
2001
2002 if !isClient {
2003 test.testType = serverTest
2004 }
2005
2006 if supportedInFirstConnection {
2007 test.config = supportedConfig
2008 } else {
2009 test.config = noSupportConfig
2010 }
2011
2012 if supportedInResumeConnection {
2013 test.resumeConfig = &supportedConfig
2014 } else {
2015 test.resumeConfig = &noSupportConfig
2016 }
2017
2018 switch suffix {
2019 case "YesToYes-Client", "YesToYes-Server":
2020 // When a session is resumed, it should
2021 // still be aware that its master
2022 // secret was generated via EMS and
2023 // thus it's safe to use tls-unique.
2024 test.flags = []string{expectEMSFlag}
2025 case "NoToYes-Server":
2026 // If an original connection did not
2027 // contain EMS, but a resumption
2028 // handshake does, then a server should
2029 // not resume the session.
2030 test.expectResumeRejected = true
2031 case "YesToNo-Server":
2032 // Resuming an EMS session without the
2033 // EMS extension should cause the
2034 // server to abort the connection.
2035 test.shouldFail = true
2036 test.expectedError = ":RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION:"
2037 case "NoToYes-Client":
2038 // A client should abort a connection
2039 // where the server resumed a non-EMS
2040 // session but echoed the EMS
2041 // extension.
2042 test.shouldFail = true
2043 test.expectedError = ":RESUMED_NON_EMS_SESSION_WITH_EMS_EXTENSION:"
2044 case "YesToNo-Client":
2045 // A client should abort a connection
2046 // where the server didn't echo EMS
2047 // when the session used it.
2048 test.shouldFail = true
2049 test.expectedError = ":RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION:"
2050 }
2051
2052 testCases = append(testCases, test)
2053 }
2054 }
2055 }
Adam Langley75712922014-10-10 16:23:43 -07002056}
2057
David Benjamin43ec06f2014-08-05 02:28:57 -04002058// Adds tests that try to cover the range of the handshake state machine, under
2059// various conditions. Some of these are redundant with other tests, but they
2060// only cover the synchronous case.
David Benjamin6fd297b2014-08-11 18:43:38 -04002061func addStateMachineCoverageTests(async, splitHandshake bool, protocol protocol) {
David Benjamin760b1dd2015-05-15 23:33:48 -04002062 var tests []testCase
2063
2064 // Basic handshake, with resumption. Client and server,
2065 // session ID and session ticket.
2066 tests = append(tests, testCase{
2067 name: "Basic-Client",
2068 resumeSession: true,
2069 })
2070 tests = append(tests, testCase{
2071 name: "Basic-Client-RenewTicket",
2072 config: Config{
2073 Bugs: ProtocolBugs{
2074 RenewTicketOnResume: true,
2075 },
2076 },
2077 resumeSession: true,
2078 })
2079 tests = append(tests, testCase{
2080 name: "Basic-Client-NoTicket",
2081 config: Config{
2082 SessionTicketsDisabled: true,
2083 },
2084 resumeSession: true,
2085 })
2086 tests = append(tests, testCase{
2087 name: "Basic-Client-Implicit",
2088 flags: []string{"-implicit-handshake"},
2089 resumeSession: true,
2090 })
2091 tests = append(tests, testCase{
2092 testType: serverTest,
2093 name: "Basic-Server",
2094 resumeSession: true,
2095 })
2096 tests = append(tests, testCase{
2097 testType: serverTest,
2098 name: "Basic-Server-NoTickets",
2099 config: Config{
2100 SessionTicketsDisabled: true,
2101 },
2102 resumeSession: true,
2103 })
2104 tests = append(tests, testCase{
2105 testType: serverTest,
2106 name: "Basic-Server-Implicit",
2107 flags: []string{"-implicit-handshake"},
2108 resumeSession: true,
2109 })
2110 tests = append(tests, testCase{
2111 testType: serverTest,
2112 name: "Basic-Server-EarlyCallback",
2113 flags: []string{"-use-early-callback"},
2114 resumeSession: true,
2115 })
2116
2117 // TLS client auth.
2118 tests = append(tests, testCase{
2119 testType: clientTest,
2120 name: "ClientAuth-Client",
2121 config: Config{
2122 ClientAuth: RequireAnyClientCert,
2123 },
2124 flags: []string{
2125 "-cert-file", rsaCertificateFile,
2126 "-key-file", rsaKeyFile,
2127 },
2128 })
2129 tests = append(tests, testCase{
2130 testType: serverTest,
2131 name: "ClientAuth-Server",
2132 config: Config{
2133 Certificates: []Certificate{rsaCertificate},
2134 },
2135 flags: []string{"-require-any-client-certificate"},
2136 })
2137
2138 // No session ticket support; server doesn't send NewSessionTicket.
2139 tests = append(tests, testCase{
2140 name: "SessionTicketsDisabled-Client",
2141 config: Config{
2142 SessionTicketsDisabled: true,
2143 },
2144 })
2145 tests = append(tests, testCase{
2146 testType: serverTest,
2147 name: "SessionTicketsDisabled-Server",
2148 config: Config{
2149 SessionTicketsDisabled: true,
2150 },
2151 })
2152
2153 // Skip ServerKeyExchange in PSK key exchange if there's no
2154 // identity hint.
2155 tests = append(tests, testCase{
2156 name: "EmptyPSKHint-Client",
2157 config: Config{
2158 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
2159 PreSharedKey: []byte("secret"),
2160 },
2161 flags: []string{"-psk", "secret"},
2162 })
2163 tests = append(tests, testCase{
2164 testType: serverTest,
2165 name: "EmptyPSKHint-Server",
2166 config: Config{
2167 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
2168 PreSharedKey: []byte("secret"),
2169 },
2170 flags: []string{"-psk", "secret"},
2171 })
2172
2173 if protocol == tls {
2174 tests = append(tests, testCase{
2175 name: "Renegotiate-Client",
2176 renegotiate: true,
2177 })
2178 // NPN on client and server; results in post-handshake message.
2179 tests = append(tests, testCase{
2180 name: "NPN-Client",
2181 config: Config{
2182 NextProtos: []string{"foo"},
2183 },
2184 flags: []string{"-select-next-proto", "foo"},
2185 expectedNextProto: "foo",
2186 expectedNextProtoType: npn,
2187 })
2188 tests = append(tests, testCase{
2189 testType: serverTest,
2190 name: "NPN-Server",
2191 config: Config{
2192 NextProtos: []string{"bar"},
2193 },
2194 flags: []string{
2195 "-advertise-npn", "\x03foo\x03bar\x03baz",
2196 "-expect-next-proto", "bar",
2197 },
2198 expectedNextProto: "bar",
2199 expectedNextProtoType: npn,
2200 })
2201
2202 // TODO(davidben): Add tests for when False Start doesn't trigger.
2203
2204 // Client does False Start and negotiates NPN.
2205 tests = append(tests, testCase{
2206 name: "FalseStart",
2207 config: Config{
2208 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2209 NextProtos: []string{"foo"},
2210 Bugs: ProtocolBugs{
2211 ExpectFalseStart: true,
2212 },
2213 },
2214 flags: []string{
2215 "-false-start",
2216 "-select-next-proto", "foo",
2217 },
2218 shimWritesFirst: true,
2219 resumeSession: true,
2220 })
2221
2222 // Client does False Start and negotiates ALPN.
2223 tests = append(tests, testCase{
2224 name: "FalseStart-ALPN",
2225 config: Config{
2226 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2227 NextProtos: []string{"foo"},
2228 Bugs: ProtocolBugs{
2229 ExpectFalseStart: true,
2230 },
2231 },
2232 flags: []string{
2233 "-false-start",
2234 "-advertise-alpn", "\x03foo",
2235 },
2236 shimWritesFirst: true,
2237 resumeSession: true,
2238 })
2239
2240 // Client does False Start but doesn't explicitly call
2241 // SSL_connect.
2242 tests = append(tests, testCase{
2243 name: "FalseStart-Implicit",
2244 config: Config{
2245 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2246 NextProtos: []string{"foo"},
2247 },
2248 flags: []string{
2249 "-implicit-handshake",
2250 "-false-start",
2251 "-advertise-alpn", "\x03foo",
2252 },
2253 })
2254
2255 // False Start without session tickets.
2256 tests = append(tests, testCase{
2257 name: "FalseStart-SessionTicketsDisabled",
2258 config: Config{
2259 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2260 NextProtos: []string{"foo"},
2261 SessionTicketsDisabled: true,
2262 Bugs: ProtocolBugs{
2263 ExpectFalseStart: true,
2264 },
2265 },
2266 flags: []string{
2267 "-false-start",
2268 "-select-next-proto", "foo",
2269 },
2270 shimWritesFirst: true,
2271 })
2272
2273 // Server parses a V2ClientHello.
2274 tests = append(tests, testCase{
2275 testType: serverTest,
2276 name: "SendV2ClientHello",
2277 config: Config{
2278 // Choose a cipher suite that does not involve
2279 // elliptic curves, so no extensions are
2280 // involved.
2281 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2282 Bugs: ProtocolBugs{
2283 SendV2ClientHello: true,
2284 },
2285 },
2286 })
2287
2288 // Client sends a Channel ID.
2289 tests = append(tests, testCase{
2290 name: "ChannelID-Client",
2291 config: Config{
2292 RequestChannelID: true,
2293 },
2294 flags: []string{"-send-channel-id", channelIDKeyFile},
2295 resumeSession: true,
2296 expectChannelID: true,
2297 })
2298
2299 // Server accepts a Channel ID.
2300 tests = append(tests, testCase{
2301 testType: serverTest,
2302 name: "ChannelID-Server",
2303 config: Config{
2304 ChannelID: channelIDKey,
2305 },
2306 flags: []string{
2307 "-expect-channel-id",
2308 base64.StdEncoding.EncodeToString(channelIDBytes),
2309 },
2310 resumeSession: true,
2311 expectChannelID: true,
2312 })
2313 } else {
2314 tests = append(tests, testCase{
2315 name: "SkipHelloVerifyRequest",
2316 config: Config{
2317 Bugs: ProtocolBugs{
2318 SkipHelloVerifyRequest: true,
2319 },
2320 },
2321 })
2322 }
2323
David Benjamin43ec06f2014-08-05 02:28:57 -04002324 var suffix string
2325 var flags []string
2326 var maxHandshakeRecordLength int
David Benjamin6fd297b2014-08-11 18:43:38 -04002327 if protocol == dtls {
2328 suffix = "-DTLS"
2329 }
David Benjamin43ec06f2014-08-05 02:28:57 -04002330 if async {
David Benjamin6fd297b2014-08-11 18:43:38 -04002331 suffix += "-Async"
David Benjamin43ec06f2014-08-05 02:28:57 -04002332 flags = append(flags, "-async")
2333 } else {
David Benjamin6fd297b2014-08-11 18:43:38 -04002334 suffix += "-Sync"
David Benjamin43ec06f2014-08-05 02:28:57 -04002335 }
2336 if splitHandshake {
2337 suffix += "-SplitHandshakeRecords"
David Benjamin98214542014-08-07 18:02:39 -04002338 maxHandshakeRecordLength = 1
David Benjamin43ec06f2014-08-05 02:28:57 -04002339 }
David Benjamin760b1dd2015-05-15 23:33:48 -04002340 for _, test := range tests {
2341 test.protocol = protocol
2342 test.name += suffix
2343 test.config.Bugs.MaxHandshakeRecordLength = maxHandshakeRecordLength
2344 test.flags = append(test.flags, flags...)
2345 testCases = append(testCases, test)
David Benjamin6fd297b2014-08-11 18:43:38 -04002346 }
David Benjamin43ec06f2014-08-05 02:28:57 -04002347}
2348
Adam Langley524e7172015-02-20 16:04:00 -08002349func addDDoSCallbackTests() {
2350 // DDoS callback.
2351
2352 for _, resume := range []bool{false, true} {
2353 suffix := "Resume"
2354 if resume {
2355 suffix = "No" + suffix
2356 }
2357
2358 testCases = append(testCases, testCase{
2359 testType: serverTest,
2360 name: "Server-DDoS-OK-" + suffix,
2361 flags: []string{"-install-ddos-callback"},
2362 resumeSession: resume,
2363 })
2364
2365 failFlag := "-fail-ddos-callback"
2366 if resume {
2367 failFlag = "-fail-second-ddos-callback"
2368 }
2369 testCases = append(testCases, testCase{
2370 testType: serverTest,
2371 name: "Server-DDoS-Reject-" + suffix,
2372 flags: []string{"-install-ddos-callback", failFlag},
2373 resumeSession: resume,
2374 shouldFail: true,
2375 expectedError: ":CONNECTION_REJECTED:",
2376 })
2377 }
2378}
2379
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002380func addVersionNegotiationTests() {
2381 for i, shimVers := range tlsVersions {
2382 // Assemble flags to disable all newer versions on the shim.
2383 var flags []string
2384 for _, vers := range tlsVersions[i+1:] {
2385 flags = append(flags, vers.flag)
2386 }
2387
2388 for _, runnerVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05002389 protocols := []protocol{tls}
2390 if runnerVers.hasDTLS && shimVers.hasDTLS {
2391 protocols = append(protocols, dtls)
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002392 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002393 for _, protocol := range protocols {
2394 expectedVersion := shimVers.version
2395 if runnerVers.version < shimVers.version {
2396 expectedVersion = runnerVers.version
2397 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002398
David Benjamin8b8c0062014-11-23 02:47:52 -05002399 suffix := shimVers.name + "-" + runnerVers.name
2400 if protocol == dtls {
2401 suffix += "-DTLS"
2402 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002403
David Benjamin1eb367c2014-12-12 18:17:51 -05002404 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
2405
David Benjamin1e29a6b2014-12-10 02:27:24 -05002406 clientVers := shimVers.version
2407 if clientVers > VersionTLS10 {
2408 clientVers = VersionTLS10
2409 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002410 testCases = append(testCases, testCase{
2411 protocol: protocol,
2412 testType: clientTest,
2413 name: "VersionNegotiation-Client-" + suffix,
2414 config: Config{
2415 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05002416 Bugs: ProtocolBugs{
2417 ExpectInitialRecordVersion: clientVers,
2418 },
David Benjamin8b8c0062014-11-23 02:47:52 -05002419 },
2420 flags: flags,
2421 expectedVersion: expectedVersion,
2422 })
David Benjamin1eb367c2014-12-12 18:17:51 -05002423 testCases = append(testCases, testCase{
2424 protocol: protocol,
2425 testType: clientTest,
2426 name: "VersionNegotiation-Client2-" + suffix,
2427 config: Config{
2428 MaxVersion: runnerVers.version,
2429 Bugs: ProtocolBugs{
2430 ExpectInitialRecordVersion: clientVers,
2431 },
2432 },
2433 flags: []string{"-max-version", shimVersFlag},
2434 expectedVersion: expectedVersion,
2435 })
David Benjamin8b8c0062014-11-23 02:47:52 -05002436
2437 testCases = append(testCases, testCase{
2438 protocol: protocol,
2439 testType: serverTest,
2440 name: "VersionNegotiation-Server-" + suffix,
2441 config: Config{
2442 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05002443 Bugs: ProtocolBugs{
2444 ExpectInitialRecordVersion: expectedVersion,
2445 },
David Benjamin8b8c0062014-11-23 02:47:52 -05002446 },
2447 flags: flags,
2448 expectedVersion: expectedVersion,
2449 })
David Benjamin1eb367c2014-12-12 18:17:51 -05002450 testCases = append(testCases, testCase{
2451 protocol: protocol,
2452 testType: serverTest,
2453 name: "VersionNegotiation-Server2-" + suffix,
2454 config: Config{
2455 MaxVersion: runnerVers.version,
2456 Bugs: ProtocolBugs{
2457 ExpectInitialRecordVersion: expectedVersion,
2458 },
2459 },
2460 flags: []string{"-max-version", shimVersFlag},
2461 expectedVersion: expectedVersion,
2462 })
David Benjamin8b8c0062014-11-23 02:47:52 -05002463 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002464 }
2465 }
2466}
2467
David Benjaminaccb4542014-12-12 23:44:33 -05002468func addMinimumVersionTests() {
2469 for i, shimVers := range tlsVersions {
2470 // Assemble flags to disable all older versions on the shim.
2471 var flags []string
2472 for _, vers := range tlsVersions[:i] {
2473 flags = append(flags, vers.flag)
2474 }
2475
2476 for _, runnerVers := range tlsVersions {
2477 protocols := []protocol{tls}
2478 if runnerVers.hasDTLS && shimVers.hasDTLS {
2479 protocols = append(protocols, dtls)
2480 }
2481 for _, protocol := range protocols {
2482 suffix := shimVers.name + "-" + runnerVers.name
2483 if protocol == dtls {
2484 suffix += "-DTLS"
2485 }
2486 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
2487
David Benjaminaccb4542014-12-12 23:44:33 -05002488 var expectedVersion uint16
2489 var shouldFail bool
2490 var expectedError string
David Benjamin87909c02014-12-13 01:55:01 -05002491 var expectedLocalError string
David Benjaminaccb4542014-12-12 23:44:33 -05002492 if runnerVers.version >= shimVers.version {
2493 expectedVersion = runnerVers.version
2494 } else {
2495 shouldFail = true
2496 expectedError = ":UNSUPPORTED_PROTOCOL:"
David Benjamin87909c02014-12-13 01:55:01 -05002497 if runnerVers.version > VersionSSL30 {
2498 expectedLocalError = "remote error: protocol version not supported"
2499 } else {
2500 expectedLocalError = "remote error: handshake failure"
2501 }
David Benjaminaccb4542014-12-12 23:44:33 -05002502 }
2503
2504 testCases = append(testCases, testCase{
2505 protocol: protocol,
2506 testType: clientTest,
2507 name: "MinimumVersion-Client-" + suffix,
2508 config: Config{
2509 MaxVersion: runnerVers.version,
2510 },
David Benjamin87909c02014-12-13 01:55:01 -05002511 flags: flags,
2512 expectedVersion: expectedVersion,
2513 shouldFail: shouldFail,
2514 expectedError: expectedError,
2515 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002516 })
2517 testCases = append(testCases, testCase{
2518 protocol: protocol,
2519 testType: clientTest,
2520 name: "MinimumVersion-Client2-" + suffix,
2521 config: Config{
2522 MaxVersion: runnerVers.version,
2523 },
David Benjamin87909c02014-12-13 01:55:01 -05002524 flags: []string{"-min-version", shimVersFlag},
2525 expectedVersion: expectedVersion,
2526 shouldFail: shouldFail,
2527 expectedError: expectedError,
2528 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002529 })
2530
2531 testCases = append(testCases, testCase{
2532 protocol: protocol,
2533 testType: serverTest,
2534 name: "MinimumVersion-Server-" + suffix,
2535 config: Config{
2536 MaxVersion: runnerVers.version,
2537 },
David Benjamin87909c02014-12-13 01:55:01 -05002538 flags: flags,
2539 expectedVersion: expectedVersion,
2540 shouldFail: shouldFail,
2541 expectedError: expectedError,
2542 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002543 })
2544 testCases = append(testCases, testCase{
2545 protocol: protocol,
2546 testType: serverTest,
2547 name: "MinimumVersion-Server2-" + suffix,
2548 config: Config{
2549 MaxVersion: runnerVers.version,
2550 },
David Benjamin87909c02014-12-13 01:55:01 -05002551 flags: []string{"-min-version", shimVersFlag},
2552 expectedVersion: expectedVersion,
2553 shouldFail: shouldFail,
2554 expectedError: expectedError,
2555 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002556 })
2557 }
2558 }
2559 }
2560}
2561
David Benjamin5c24a1d2014-08-31 00:59:27 -04002562func addD5BugTests() {
2563 testCases = append(testCases, testCase{
2564 testType: serverTest,
2565 name: "D5Bug-NoQuirk-Reject",
2566 config: Config{
2567 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2568 Bugs: ProtocolBugs{
2569 SSL3RSAKeyExchange: true,
2570 },
2571 },
2572 shouldFail: true,
2573 expectedError: ":TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG:",
2574 })
2575 testCases = append(testCases, testCase{
2576 testType: serverTest,
2577 name: "D5Bug-Quirk-Normal",
2578 config: Config{
2579 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2580 },
2581 flags: []string{"-tls-d5-bug"},
2582 })
2583 testCases = append(testCases, testCase{
2584 testType: serverTest,
2585 name: "D5Bug-Quirk-Bug",
2586 config: Config{
2587 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2588 Bugs: ProtocolBugs{
2589 SSL3RSAKeyExchange: true,
2590 },
2591 },
2592 flags: []string{"-tls-d5-bug"},
2593 })
2594}
2595
David Benjamine78bfde2014-09-06 12:45:15 -04002596func addExtensionTests() {
2597 testCases = append(testCases, testCase{
2598 testType: clientTest,
2599 name: "DuplicateExtensionClient",
2600 config: Config{
2601 Bugs: ProtocolBugs{
2602 DuplicateExtension: true,
2603 },
2604 },
2605 shouldFail: true,
2606 expectedLocalError: "remote error: error decoding message",
2607 })
2608 testCases = append(testCases, testCase{
2609 testType: serverTest,
2610 name: "DuplicateExtensionServer",
2611 config: Config{
2612 Bugs: ProtocolBugs{
2613 DuplicateExtension: true,
2614 },
2615 },
2616 shouldFail: true,
2617 expectedLocalError: "remote error: error decoding message",
2618 })
2619 testCases = append(testCases, testCase{
2620 testType: clientTest,
2621 name: "ServerNameExtensionClient",
2622 config: Config{
2623 Bugs: ProtocolBugs{
2624 ExpectServerName: "example.com",
2625 },
2626 },
2627 flags: []string{"-host-name", "example.com"},
2628 })
2629 testCases = append(testCases, testCase{
2630 testType: clientTest,
David Benjamin5f237bc2015-02-11 17:14:15 -05002631 name: "ServerNameExtensionClientMismatch",
David Benjamine78bfde2014-09-06 12:45:15 -04002632 config: Config{
2633 Bugs: ProtocolBugs{
2634 ExpectServerName: "mismatch.com",
2635 },
2636 },
2637 flags: []string{"-host-name", "example.com"},
2638 shouldFail: true,
2639 expectedLocalError: "tls: unexpected server name",
2640 })
2641 testCases = append(testCases, testCase{
2642 testType: clientTest,
David Benjamin5f237bc2015-02-11 17:14:15 -05002643 name: "ServerNameExtensionClientMissing",
David Benjamine78bfde2014-09-06 12:45:15 -04002644 config: Config{
2645 Bugs: ProtocolBugs{
2646 ExpectServerName: "missing.com",
2647 },
2648 },
2649 shouldFail: true,
2650 expectedLocalError: "tls: unexpected server name",
2651 })
2652 testCases = append(testCases, testCase{
2653 testType: serverTest,
2654 name: "ServerNameExtensionServer",
2655 config: Config{
2656 ServerName: "example.com",
2657 },
2658 flags: []string{"-expect-server-name", "example.com"},
2659 resumeSession: true,
2660 })
David Benjaminae2888f2014-09-06 12:58:58 -04002661 testCases = append(testCases, testCase{
2662 testType: clientTest,
2663 name: "ALPNClient",
2664 config: Config{
2665 NextProtos: []string{"foo"},
2666 },
2667 flags: []string{
2668 "-advertise-alpn", "\x03foo\x03bar\x03baz",
2669 "-expect-alpn", "foo",
2670 },
David Benjaminfc7b0862014-09-06 13:21:53 -04002671 expectedNextProto: "foo",
2672 expectedNextProtoType: alpn,
2673 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04002674 })
2675 testCases = append(testCases, testCase{
2676 testType: serverTest,
2677 name: "ALPNServer",
2678 config: Config{
2679 NextProtos: []string{"foo", "bar", "baz"},
2680 },
2681 flags: []string{
2682 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2683 "-select-alpn", "foo",
2684 },
David Benjaminfc7b0862014-09-06 13:21:53 -04002685 expectedNextProto: "foo",
2686 expectedNextProtoType: alpn,
2687 resumeSession: true,
2688 })
2689 // Test that the server prefers ALPN over NPN.
2690 testCases = append(testCases, testCase{
2691 testType: serverTest,
2692 name: "ALPNServer-Preferred",
2693 config: Config{
2694 NextProtos: []string{"foo", "bar", "baz"},
2695 },
2696 flags: []string{
2697 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2698 "-select-alpn", "foo",
2699 "-advertise-npn", "\x03foo\x03bar\x03baz",
2700 },
2701 expectedNextProto: "foo",
2702 expectedNextProtoType: alpn,
2703 resumeSession: true,
2704 })
2705 testCases = append(testCases, testCase{
2706 testType: serverTest,
2707 name: "ALPNServer-Preferred-Swapped",
2708 config: Config{
2709 NextProtos: []string{"foo", "bar", "baz"},
2710 Bugs: ProtocolBugs{
2711 SwapNPNAndALPN: true,
2712 },
2713 },
2714 flags: []string{
2715 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2716 "-select-alpn", "foo",
2717 "-advertise-npn", "\x03foo\x03bar\x03baz",
2718 },
2719 expectedNextProto: "foo",
2720 expectedNextProtoType: alpn,
2721 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04002722 })
Adam Langley38311732014-10-16 19:04:35 -07002723 // Resume with a corrupt ticket.
2724 testCases = append(testCases, testCase{
2725 testType: serverTest,
2726 name: "CorruptTicket",
2727 config: Config{
2728 Bugs: ProtocolBugs{
2729 CorruptTicket: true,
2730 },
2731 },
Adam Langleyb0eef0a2015-06-02 10:47:39 -07002732 resumeSession: true,
2733 expectResumeRejected: true,
Adam Langley38311732014-10-16 19:04:35 -07002734 })
2735 // Resume with an oversized session id.
2736 testCases = append(testCases, testCase{
2737 testType: serverTest,
2738 name: "OversizedSessionId",
2739 config: Config{
2740 Bugs: ProtocolBugs{
2741 OversizedSessionId: true,
2742 },
2743 },
2744 resumeSession: true,
Adam Langley75712922014-10-10 16:23:43 -07002745 shouldFail: true,
Adam Langley38311732014-10-16 19:04:35 -07002746 expectedError: ":DECODE_ERROR:",
2747 })
David Benjaminca6c8262014-11-15 19:06:08 -05002748 // Basic DTLS-SRTP tests. Include fake profiles to ensure they
2749 // are ignored.
2750 testCases = append(testCases, testCase{
2751 protocol: dtls,
2752 name: "SRTP-Client",
2753 config: Config{
2754 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
2755 },
2756 flags: []string{
2757 "-srtp-profiles",
2758 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2759 },
2760 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2761 })
2762 testCases = append(testCases, testCase{
2763 protocol: dtls,
2764 testType: serverTest,
2765 name: "SRTP-Server",
2766 config: Config{
2767 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
2768 },
2769 flags: []string{
2770 "-srtp-profiles",
2771 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2772 },
2773 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2774 })
2775 // Test that the MKI is ignored.
2776 testCases = append(testCases, testCase{
2777 protocol: dtls,
2778 testType: serverTest,
2779 name: "SRTP-Server-IgnoreMKI",
2780 config: Config{
2781 SRTPProtectionProfiles: []uint16{SRTP_AES128_CM_HMAC_SHA1_80},
2782 Bugs: ProtocolBugs{
2783 SRTPMasterKeyIdentifer: "bogus",
2784 },
2785 },
2786 flags: []string{
2787 "-srtp-profiles",
2788 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2789 },
2790 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2791 })
2792 // Test that SRTP isn't negotiated on the server if there were
2793 // no matching profiles.
2794 testCases = append(testCases, testCase{
2795 protocol: dtls,
2796 testType: serverTest,
2797 name: "SRTP-Server-NoMatch",
2798 config: Config{
2799 SRTPProtectionProfiles: []uint16{100, 101, 102},
2800 },
2801 flags: []string{
2802 "-srtp-profiles",
2803 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2804 },
2805 expectedSRTPProtectionProfile: 0,
2806 })
2807 // Test that the server returning an invalid SRTP profile is
2808 // flagged as an error by the client.
2809 testCases = append(testCases, testCase{
2810 protocol: dtls,
2811 name: "SRTP-Client-NoMatch",
2812 config: Config{
2813 Bugs: ProtocolBugs{
2814 SendSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_32,
2815 },
2816 },
2817 flags: []string{
2818 "-srtp-profiles",
2819 "SRTP_AES128_CM_SHA1_80",
2820 },
2821 shouldFail: true,
2822 expectedError: ":BAD_SRTP_PROTECTION_PROFILE_LIST:",
2823 })
David Benjamin61f95272014-11-25 01:55:35 -05002824 // Test OCSP stapling and SCT list.
2825 testCases = append(testCases, testCase{
2826 name: "OCSPStapling",
2827 flags: []string{
2828 "-enable-ocsp-stapling",
2829 "-expect-ocsp-response",
2830 base64.StdEncoding.EncodeToString(testOCSPResponse),
2831 },
2832 })
2833 testCases = append(testCases, testCase{
2834 name: "SignedCertificateTimestampList",
2835 flags: []string{
2836 "-enable-signed-cert-timestamps",
2837 "-expect-signed-cert-timestamps",
2838 base64.StdEncoding.EncodeToString(testSCTList),
2839 },
2840 })
David Benjamine78bfde2014-09-06 12:45:15 -04002841}
2842
David Benjamin01fe8202014-09-24 15:21:44 -04002843func addResumptionVersionTests() {
David Benjamin01fe8202014-09-24 15:21:44 -04002844 for _, sessionVers := range tlsVersions {
David Benjamin01fe8202014-09-24 15:21:44 -04002845 for _, resumeVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05002846 protocols := []protocol{tls}
2847 if sessionVers.hasDTLS && resumeVers.hasDTLS {
2848 protocols = append(protocols, dtls)
David Benjaminbdf5e722014-11-11 00:52:15 -05002849 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002850 for _, protocol := range protocols {
2851 suffix := "-" + sessionVers.name + "-" + resumeVers.name
2852 if protocol == dtls {
2853 suffix += "-DTLS"
2854 }
2855
David Benjaminece3de92015-03-16 18:02:20 -04002856 if sessionVers.version == resumeVers.version {
2857 testCases = append(testCases, testCase{
2858 protocol: protocol,
2859 name: "Resume-Client" + suffix,
2860 resumeSession: true,
2861 config: Config{
2862 MaxVersion: sessionVers.version,
2863 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
David Benjamin8b8c0062014-11-23 02:47:52 -05002864 },
David Benjaminece3de92015-03-16 18:02:20 -04002865 expectedVersion: sessionVers.version,
2866 expectedResumeVersion: resumeVers.version,
2867 })
2868 } else {
2869 testCases = append(testCases, testCase{
2870 protocol: protocol,
2871 name: "Resume-Client-Mismatch" + suffix,
2872 resumeSession: true,
2873 config: Config{
2874 MaxVersion: sessionVers.version,
2875 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
David Benjamin8b8c0062014-11-23 02:47:52 -05002876 },
David Benjaminece3de92015-03-16 18:02:20 -04002877 expectedVersion: sessionVers.version,
2878 resumeConfig: &Config{
2879 MaxVersion: resumeVers.version,
2880 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2881 Bugs: ProtocolBugs{
2882 AllowSessionVersionMismatch: true,
2883 },
2884 },
2885 expectedResumeVersion: resumeVers.version,
2886 shouldFail: true,
2887 expectedError: ":OLD_SESSION_VERSION_NOT_RETURNED:",
2888 })
2889 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002890
2891 testCases = append(testCases, testCase{
2892 protocol: protocol,
2893 name: "Resume-Client-NoResume" + suffix,
David Benjamin8b8c0062014-11-23 02:47:52 -05002894 resumeSession: true,
2895 config: Config{
2896 MaxVersion: sessionVers.version,
2897 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2898 },
2899 expectedVersion: sessionVers.version,
2900 resumeConfig: &Config{
2901 MaxVersion: resumeVers.version,
2902 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2903 },
2904 newSessionsOnResume: true,
Adam Langleyb0eef0a2015-06-02 10:47:39 -07002905 expectResumeRejected: true,
David Benjamin8b8c0062014-11-23 02:47:52 -05002906 expectedResumeVersion: resumeVers.version,
2907 })
2908
David Benjamin8b8c0062014-11-23 02:47:52 -05002909 testCases = append(testCases, testCase{
2910 protocol: protocol,
2911 testType: serverTest,
2912 name: "Resume-Server" + suffix,
David Benjamin8b8c0062014-11-23 02:47:52 -05002913 resumeSession: true,
2914 config: Config{
2915 MaxVersion: sessionVers.version,
2916 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2917 },
Adam Langleyb0eef0a2015-06-02 10:47:39 -07002918 expectedVersion: sessionVers.version,
2919 expectResumeRejected: sessionVers.version != resumeVers.version,
David Benjamin8b8c0062014-11-23 02:47:52 -05002920 resumeConfig: &Config{
2921 MaxVersion: resumeVers.version,
2922 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2923 },
2924 expectedResumeVersion: resumeVers.version,
2925 })
2926 }
David Benjamin01fe8202014-09-24 15:21:44 -04002927 }
2928 }
David Benjaminece3de92015-03-16 18:02:20 -04002929
2930 testCases = append(testCases, testCase{
2931 name: "Resume-Client-CipherMismatch",
2932 resumeSession: true,
2933 config: Config{
2934 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2935 },
2936 resumeConfig: &Config{
2937 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2938 Bugs: ProtocolBugs{
2939 SendCipherSuite: TLS_RSA_WITH_AES_128_CBC_SHA,
2940 },
2941 },
2942 shouldFail: true,
2943 expectedError: ":OLD_SESSION_CIPHER_NOT_RETURNED:",
2944 })
David Benjamin01fe8202014-09-24 15:21:44 -04002945}
2946
Adam Langley2ae77d22014-10-28 17:29:33 -07002947func addRenegotiationTests() {
David Benjamin44d3eed2015-05-21 01:29:55 -04002948 // Servers cannot renegotiate.
David Benjaminb16346b2015-04-08 19:16:58 -04002949 testCases = append(testCases, testCase{
2950 testType: serverTest,
David Benjamin44d3eed2015-05-21 01:29:55 -04002951 name: "Renegotiate-Server-Forbidden",
David Benjaminb16346b2015-04-08 19:16:58 -04002952 renegotiate: true,
2953 flags: []string{"-reject-peer-renegotiations"},
2954 shouldFail: true,
2955 expectedError: ":NO_RENEGOTIATION:",
2956 expectedLocalError: "remote error: no renegotiation",
2957 })
Adam Langley2ae77d22014-10-28 17:29:33 -07002958 // TODO(agl): test the renegotiation info SCSV.
Adam Langleycf2d4f42014-10-28 19:06:14 -07002959 testCases = append(testCases, testCase{
David Benjamin4b27d9f2015-05-12 22:42:52 -04002960 name: "Renegotiate-Client",
David Benjamincdea40c2015-03-19 14:09:43 -04002961 config: Config{
2962 Bugs: ProtocolBugs{
David Benjamin4b27d9f2015-05-12 22:42:52 -04002963 FailIfResumeOnRenego: true,
David Benjamincdea40c2015-03-19 14:09:43 -04002964 },
2965 },
2966 renegotiate: true,
2967 })
2968 testCases = append(testCases, testCase{
Adam Langleycf2d4f42014-10-28 19:06:14 -07002969 name: "Renegotiate-Client-EmptyExt",
2970 renegotiate: true,
2971 config: Config{
2972 Bugs: ProtocolBugs{
2973 EmptyRenegotiationInfo: true,
2974 },
2975 },
2976 shouldFail: true,
2977 expectedError: ":RENEGOTIATION_MISMATCH:",
2978 })
2979 testCases = append(testCases, testCase{
2980 name: "Renegotiate-Client-BadExt",
2981 renegotiate: true,
2982 config: Config{
2983 Bugs: ProtocolBugs{
2984 BadRenegotiationInfo: true,
2985 },
2986 },
2987 shouldFail: true,
2988 expectedError: ":RENEGOTIATION_MISMATCH:",
2989 })
2990 testCases = append(testCases, testCase{
David Benjamincff0b902015-05-15 23:09:47 -04002991 name: "Renegotiate-Client-NoExt",
2992 renegotiate: true,
2993 config: Config{
2994 Bugs: ProtocolBugs{
2995 NoRenegotiationInfo: true,
2996 },
2997 },
2998 shouldFail: true,
2999 expectedError: ":UNSAFE_LEGACY_RENEGOTIATION_DISABLED:",
3000 flags: []string{"-no-legacy-server-connect"},
3001 })
3002 testCases = append(testCases, testCase{
3003 name: "Renegotiate-Client-NoExt-Allowed",
3004 renegotiate: true,
3005 config: Config{
3006 Bugs: ProtocolBugs{
3007 NoRenegotiationInfo: true,
3008 },
3009 },
3010 })
3011 testCases = append(testCases, testCase{
Adam Langleycf2d4f42014-10-28 19:06:14 -07003012 name: "Renegotiate-Client-SwitchCiphers",
3013 renegotiate: true,
3014 config: Config{
3015 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
3016 },
3017 renegotiateCiphers: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
3018 })
3019 testCases = append(testCases, testCase{
3020 name: "Renegotiate-Client-SwitchCiphers2",
3021 renegotiate: true,
3022 config: Config{
3023 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
3024 },
3025 renegotiateCiphers: []uint16{TLS_RSA_WITH_RC4_128_SHA},
3026 })
David Benjaminc44b1df2014-11-23 12:11:01 -05003027 testCases = append(testCases, testCase{
David Benjaminb16346b2015-04-08 19:16:58 -04003028 name: "Renegotiate-Client-Forbidden",
3029 renegotiate: true,
3030 flags: []string{"-reject-peer-renegotiations"},
3031 shouldFail: true,
3032 expectedError: ":NO_RENEGOTIATION:",
3033 expectedLocalError: "remote error: no renegotiation",
3034 })
3035 testCases = append(testCases, testCase{
David Benjaminc44b1df2014-11-23 12:11:01 -05003036 name: "Renegotiate-SameClientVersion",
3037 renegotiate: true,
3038 config: Config{
3039 MaxVersion: VersionTLS10,
3040 Bugs: ProtocolBugs{
3041 RequireSameRenegoClientVersion: true,
3042 },
3043 },
3044 })
Adam Langley2ae77d22014-10-28 17:29:33 -07003045}
3046
David Benjamin5e961c12014-11-07 01:48:35 -05003047func addDTLSReplayTests() {
3048 // Test that sequence number replays are detected.
3049 testCases = append(testCases, testCase{
3050 protocol: dtls,
3051 name: "DTLS-Replay",
3052 replayWrites: true,
3053 })
3054
3055 // Test the outgoing sequence number skipping by values larger
3056 // than the retransmit window.
3057 testCases = append(testCases, testCase{
3058 protocol: dtls,
3059 name: "DTLS-Replay-LargeGaps",
3060 config: Config{
3061 Bugs: ProtocolBugs{
3062 SequenceNumberIncrement: 127,
3063 },
3064 },
3065 replayWrites: true,
3066 })
3067}
3068
Feng Lu41aa3252014-11-21 22:47:56 -08003069func addFastRadioPaddingTests() {
David Benjamin1e29a6b2014-12-10 02:27:24 -05003070 testCases = append(testCases, testCase{
3071 protocol: tls,
3072 name: "FastRadio-Padding",
Feng Lu41aa3252014-11-21 22:47:56 -08003073 config: Config{
3074 Bugs: ProtocolBugs{
3075 RequireFastradioPadding: true,
3076 },
3077 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05003078 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08003079 })
David Benjamin1e29a6b2014-12-10 02:27:24 -05003080 testCases = append(testCases, testCase{
3081 protocol: dtls,
David Benjamin5f237bc2015-02-11 17:14:15 -05003082 name: "FastRadio-Padding-DTLS",
Feng Lu41aa3252014-11-21 22:47:56 -08003083 config: Config{
3084 Bugs: ProtocolBugs{
3085 RequireFastradioPadding: true,
3086 },
3087 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05003088 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08003089 })
3090}
3091
David Benjamin000800a2014-11-14 01:43:59 -05003092var testHashes = []struct {
3093 name string
3094 id uint8
3095}{
3096 {"SHA1", hashSHA1},
3097 {"SHA224", hashSHA224},
3098 {"SHA256", hashSHA256},
3099 {"SHA384", hashSHA384},
3100 {"SHA512", hashSHA512},
3101}
3102
3103func addSigningHashTests() {
3104 // Make sure each hash works. Include some fake hashes in the list and
3105 // ensure they're ignored.
3106 for _, hash := range testHashes {
3107 testCases = append(testCases, testCase{
3108 name: "SigningHash-ClientAuth-" + hash.name,
3109 config: Config{
3110 ClientAuth: RequireAnyClientCert,
3111 SignatureAndHashes: []signatureAndHash{
3112 {signatureRSA, 42},
3113 {signatureRSA, hash.id},
3114 {signatureRSA, 255},
3115 },
3116 },
3117 flags: []string{
3118 "-cert-file", rsaCertificateFile,
3119 "-key-file", rsaKeyFile,
3120 },
3121 })
3122
3123 testCases = append(testCases, testCase{
3124 testType: serverTest,
3125 name: "SigningHash-ServerKeyExchange-Sign-" + hash.name,
3126 config: Config{
3127 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
3128 SignatureAndHashes: []signatureAndHash{
3129 {signatureRSA, 42},
3130 {signatureRSA, hash.id},
3131 {signatureRSA, 255},
3132 },
3133 },
3134 })
3135 }
3136
3137 // Test that hash resolution takes the signature type into account.
3138 testCases = append(testCases, testCase{
3139 name: "SigningHash-ClientAuth-SignatureType",
3140 config: Config{
3141 ClientAuth: RequireAnyClientCert,
3142 SignatureAndHashes: []signatureAndHash{
3143 {signatureECDSA, hashSHA512},
3144 {signatureRSA, hashSHA384},
3145 {signatureECDSA, hashSHA1},
3146 },
3147 },
3148 flags: []string{
3149 "-cert-file", rsaCertificateFile,
3150 "-key-file", rsaKeyFile,
3151 },
3152 })
3153
3154 testCases = append(testCases, testCase{
3155 testType: serverTest,
3156 name: "SigningHash-ServerKeyExchange-SignatureType",
3157 config: Config{
3158 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
3159 SignatureAndHashes: []signatureAndHash{
3160 {signatureECDSA, hashSHA512},
3161 {signatureRSA, hashSHA384},
3162 {signatureECDSA, hashSHA1},
3163 },
3164 },
3165 })
3166
3167 // Test that, if the list is missing, the peer falls back to SHA-1.
3168 testCases = append(testCases, testCase{
3169 name: "SigningHash-ClientAuth-Fallback",
3170 config: Config{
3171 ClientAuth: RequireAnyClientCert,
3172 SignatureAndHashes: []signatureAndHash{
3173 {signatureRSA, hashSHA1},
3174 },
3175 Bugs: ProtocolBugs{
3176 NoSignatureAndHashes: true,
3177 },
3178 },
3179 flags: []string{
3180 "-cert-file", rsaCertificateFile,
3181 "-key-file", rsaKeyFile,
3182 },
3183 })
3184
3185 testCases = append(testCases, testCase{
3186 testType: serverTest,
3187 name: "SigningHash-ServerKeyExchange-Fallback",
3188 config: Config{
3189 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
3190 SignatureAndHashes: []signatureAndHash{
3191 {signatureRSA, hashSHA1},
3192 },
3193 Bugs: ProtocolBugs{
3194 NoSignatureAndHashes: true,
3195 },
3196 },
3197 })
David Benjamin72dc7832015-03-16 17:49:43 -04003198
3199 // Test that hash preferences are enforced. BoringSSL defaults to
3200 // rejecting MD5 signatures.
3201 testCases = append(testCases, testCase{
3202 testType: serverTest,
3203 name: "SigningHash-ClientAuth-Enforced",
3204 config: Config{
3205 Certificates: []Certificate{rsaCertificate},
3206 SignatureAndHashes: []signatureAndHash{
3207 {signatureRSA, hashMD5},
3208 // Advertise SHA-1 so the handshake will
3209 // proceed, but the shim's preferences will be
3210 // ignored in CertificateVerify generation, so
3211 // MD5 will be chosen.
3212 {signatureRSA, hashSHA1},
3213 },
3214 Bugs: ProtocolBugs{
3215 IgnorePeerSignatureAlgorithmPreferences: true,
3216 },
3217 },
3218 flags: []string{"-require-any-client-certificate"},
3219 shouldFail: true,
3220 expectedError: ":WRONG_SIGNATURE_TYPE:",
3221 })
3222
3223 testCases = append(testCases, testCase{
3224 name: "SigningHash-ServerKeyExchange-Enforced",
3225 config: Config{
3226 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
3227 SignatureAndHashes: []signatureAndHash{
3228 {signatureRSA, hashMD5},
3229 },
3230 Bugs: ProtocolBugs{
3231 IgnorePeerSignatureAlgorithmPreferences: true,
3232 },
3233 },
3234 shouldFail: true,
3235 expectedError: ":WRONG_SIGNATURE_TYPE:",
3236 })
David Benjamin000800a2014-11-14 01:43:59 -05003237}
3238
David Benjamin83f90402015-01-27 01:09:43 -05003239// timeouts is the retransmit schedule for BoringSSL. It doubles and
3240// caps at 60 seconds. On the 13th timeout, it gives up.
3241var timeouts = []time.Duration{
3242 1 * time.Second,
3243 2 * time.Second,
3244 4 * time.Second,
3245 8 * time.Second,
3246 16 * time.Second,
3247 32 * time.Second,
3248 60 * time.Second,
3249 60 * time.Second,
3250 60 * time.Second,
3251 60 * time.Second,
3252 60 * time.Second,
3253 60 * time.Second,
3254 60 * time.Second,
3255}
3256
3257func addDTLSRetransmitTests() {
3258 // Test that this is indeed the timeout schedule. Stress all
3259 // four patterns of handshake.
3260 for i := 1; i < len(timeouts); i++ {
3261 number := strconv.Itoa(i)
3262 testCases = append(testCases, testCase{
3263 protocol: dtls,
3264 name: "DTLS-Retransmit-Client-" + number,
3265 config: Config{
3266 Bugs: ProtocolBugs{
3267 TimeoutSchedule: timeouts[:i],
3268 },
3269 },
3270 resumeSession: true,
3271 flags: []string{"-async"},
3272 })
3273 testCases = append(testCases, testCase{
3274 protocol: dtls,
3275 testType: serverTest,
3276 name: "DTLS-Retransmit-Server-" + number,
3277 config: Config{
3278 Bugs: ProtocolBugs{
3279 TimeoutSchedule: timeouts[:i],
3280 },
3281 },
3282 resumeSession: true,
3283 flags: []string{"-async"},
3284 })
3285 }
3286
3287 // Test that exceeding the timeout schedule hits a read
3288 // timeout.
3289 testCases = append(testCases, testCase{
3290 protocol: dtls,
3291 name: "DTLS-Retransmit-Timeout",
3292 config: Config{
3293 Bugs: ProtocolBugs{
3294 TimeoutSchedule: timeouts,
3295 },
3296 },
3297 resumeSession: true,
3298 flags: []string{"-async"},
3299 shouldFail: true,
3300 expectedError: ":READ_TIMEOUT_EXPIRED:",
3301 })
3302
3303 // Test that timeout handling has a fudge factor, due to API
3304 // problems.
3305 testCases = append(testCases, testCase{
3306 protocol: dtls,
3307 name: "DTLS-Retransmit-Fudge",
3308 config: Config{
3309 Bugs: ProtocolBugs{
3310 TimeoutSchedule: []time.Duration{
3311 timeouts[0] - 10*time.Millisecond,
3312 },
3313 },
3314 },
3315 resumeSession: true,
3316 flags: []string{"-async"},
3317 })
David Benjamin7eaab4c2015-03-02 19:01:16 -05003318
3319 // Test that the final Finished retransmitting isn't
3320 // duplicated if the peer badly fragments everything.
3321 testCases = append(testCases, testCase{
3322 testType: serverTest,
3323 protocol: dtls,
3324 name: "DTLS-Retransmit-Fragmented",
3325 config: Config{
3326 Bugs: ProtocolBugs{
3327 TimeoutSchedule: []time.Duration{timeouts[0]},
3328 MaxHandshakeRecordLength: 2,
3329 },
3330 },
3331 flags: []string{"-async"},
3332 })
David Benjamin83f90402015-01-27 01:09:43 -05003333}
3334
David Benjaminc565ebb2015-04-03 04:06:36 -04003335func addExportKeyingMaterialTests() {
3336 for _, vers := range tlsVersions {
3337 if vers.version == VersionSSL30 {
3338 continue
3339 }
3340 testCases = append(testCases, testCase{
3341 name: "ExportKeyingMaterial-" + vers.name,
3342 config: Config{
3343 MaxVersion: vers.version,
3344 },
3345 exportKeyingMaterial: 1024,
3346 exportLabel: "label",
3347 exportContext: "context",
3348 useExportContext: true,
3349 })
3350 testCases = append(testCases, testCase{
3351 name: "ExportKeyingMaterial-NoContext-" + vers.name,
3352 config: Config{
3353 MaxVersion: vers.version,
3354 },
3355 exportKeyingMaterial: 1024,
3356 })
3357 testCases = append(testCases, testCase{
3358 name: "ExportKeyingMaterial-EmptyContext-" + vers.name,
3359 config: Config{
3360 MaxVersion: vers.version,
3361 },
3362 exportKeyingMaterial: 1024,
3363 useExportContext: true,
3364 })
3365 testCases = append(testCases, testCase{
3366 name: "ExportKeyingMaterial-Small-" + vers.name,
3367 config: Config{
3368 MaxVersion: vers.version,
3369 },
3370 exportKeyingMaterial: 1,
3371 exportLabel: "label",
3372 exportContext: "context",
3373 useExportContext: true,
3374 })
3375 }
3376 testCases = append(testCases, testCase{
3377 name: "ExportKeyingMaterial-SSL3",
3378 config: Config{
3379 MaxVersion: VersionSSL30,
3380 },
3381 exportKeyingMaterial: 1024,
3382 exportLabel: "label",
3383 exportContext: "context",
3384 useExportContext: true,
3385 shouldFail: true,
3386 expectedError: "failed to export keying material",
3387 })
3388}
3389
Adam Langleyaf0e32c2015-06-03 09:57:23 -07003390func addTLSUniqueTests() {
3391 for _, isClient := range []bool{false, true} {
3392 for _, isResumption := range []bool{false, true} {
3393 for _, hasEMS := range []bool{false, true} {
3394 var suffix string
3395 if isResumption {
3396 suffix = "Resume-"
3397 } else {
3398 suffix = "Full-"
3399 }
3400
3401 if hasEMS {
3402 suffix += "EMS-"
3403 } else {
3404 suffix += "NoEMS-"
3405 }
3406
3407 if isClient {
3408 suffix += "Client"
3409 } else {
3410 suffix += "Server"
3411 }
3412
3413 test := testCase{
3414 name: "TLSUnique-" + suffix,
3415 testTLSUnique: true,
3416 config: Config{
3417 Bugs: ProtocolBugs{
3418 NoExtendedMasterSecret: !hasEMS,
3419 },
3420 },
3421 }
3422
3423 if isResumption {
3424 test.resumeSession = true
3425 test.resumeConfig = &Config{
3426 Bugs: ProtocolBugs{
3427 NoExtendedMasterSecret: !hasEMS,
3428 },
3429 }
3430 }
3431
3432 if isResumption && !hasEMS {
3433 test.shouldFail = true
3434 test.expectedError = "failed to get tls-unique"
3435 }
3436
3437 testCases = append(testCases, test)
3438 }
3439 }
3440 }
3441}
3442
David Benjamin884fdf12014-08-02 15:28:23 -04003443func worker(statusChan chan statusMsg, c chan *testCase, buildDir string, wg *sync.WaitGroup) {
Adam Langley95c29f32014-06-20 12:00:00 -07003444 defer wg.Done()
3445
3446 for test := range c {
Adam Langley69a01602014-11-17 17:26:55 -08003447 var err error
3448
3449 if *mallocTest < 0 {
3450 statusChan <- statusMsg{test: test, started: true}
3451 err = runTest(test, buildDir, -1)
3452 } else {
3453 for mallocNumToFail := int64(*mallocTest); ; mallocNumToFail++ {
3454 statusChan <- statusMsg{test: test, started: true}
3455 if err = runTest(test, buildDir, mallocNumToFail); err != errMoreMallocs {
3456 if err != nil {
3457 fmt.Printf("\n\nmalloc test failed at %d: %s\n", mallocNumToFail, err)
3458 }
3459 break
3460 }
3461 }
3462 }
Adam Langley95c29f32014-06-20 12:00:00 -07003463 statusChan <- statusMsg{test: test, err: err}
3464 }
3465}
3466
3467type statusMsg struct {
3468 test *testCase
3469 started bool
3470 err error
3471}
3472
David Benjamin5f237bc2015-02-11 17:14:15 -05003473func statusPrinter(doneChan chan *testOutput, statusChan chan statusMsg, total int) {
Adam Langley95c29f32014-06-20 12:00:00 -07003474 var started, done, failed, lineLen int
Adam Langley95c29f32014-06-20 12:00:00 -07003475
David Benjamin5f237bc2015-02-11 17:14:15 -05003476 testOutput := newTestOutput()
Adam Langley95c29f32014-06-20 12:00:00 -07003477 for msg := range statusChan {
David Benjamin5f237bc2015-02-11 17:14:15 -05003478 if !*pipe {
3479 // Erase the previous status line.
David Benjamin87c8a642015-02-21 01:54:29 -05003480 var erase string
3481 for i := 0; i < lineLen; i++ {
3482 erase += "\b \b"
3483 }
3484 fmt.Print(erase)
David Benjamin5f237bc2015-02-11 17:14:15 -05003485 }
3486
Adam Langley95c29f32014-06-20 12:00:00 -07003487 if msg.started {
3488 started++
3489 } else {
3490 done++
David Benjamin5f237bc2015-02-11 17:14:15 -05003491
3492 if msg.err != nil {
3493 fmt.Printf("FAILED (%s)\n%s\n", msg.test.name, msg.err)
3494 failed++
3495 testOutput.addResult(msg.test.name, "FAIL")
3496 } else {
3497 if *pipe {
3498 // Print each test instead of a status line.
3499 fmt.Printf("PASSED (%s)\n", msg.test.name)
3500 }
3501 testOutput.addResult(msg.test.name, "PASS")
3502 }
Adam Langley95c29f32014-06-20 12:00:00 -07003503 }
3504
David Benjamin5f237bc2015-02-11 17:14:15 -05003505 if !*pipe {
3506 // Print a new status line.
3507 line := fmt.Sprintf("%d/%d/%d/%d", failed, done, started, total)
3508 lineLen = len(line)
3509 os.Stdout.WriteString(line)
Adam Langley95c29f32014-06-20 12:00:00 -07003510 }
Adam Langley95c29f32014-06-20 12:00:00 -07003511 }
David Benjamin5f237bc2015-02-11 17:14:15 -05003512
3513 doneChan <- testOutput
Adam Langley95c29f32014-06-20 12:00:00 -07003514}
3515
3516func main() {
3517 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 -04003518 var flagNumWorkers *int = flag.Int("num-workers", runtime.NumCPU(), "The number of workers to run in parallel.")
David Benjamin884fdf12014-08-02 15:28:23 -04003519 var flagBuildDir *string = flag.String("build-dir", "../../../build", "The build directory to run the shim from.")
Adam Langley95c29f32014-06-20 12:00:00 -07003520
3521 flag.Parse()
3522
3523 addCipherSuiteTests()
3524 addBadECDSASignatureTests()
Adam Langley80842bd2014-06-20 12:00:00 -07003525 addCBCPaddingTests()
Kenny Root7fdeaf12014-08-05 15:23:37 -07003526 addCBCSplittingTests()
David Benjamin636293b2014-07-08 17:59:18 -04003527 addClientAuthTests()
Adam Langley524e7172015-02-20 16:04:00 -08003528 addDDoSCallbackTests()
David Benjamin7e2e6cf2014-08-07 17:44:24 -04003529 addVersionNegotiationTests()
David Benjaminaccb4542014-12-12 23:44:33 -05003530 addMinimumVersionTests()
David Benjamin5c24a1d2014-08-31 00:59:27 -04003531 addD5BugTests()
David Benjamine78bfde2014-09-06 12:45:15 -04003532 addExtensionTests()
David Benjamin01fe8202014-09-24 15:21:44 -04003533 addResumptionVersionTests()
Adam Langley75712922014-10-10 16:23:43 -07003534 addExtendedMasterSecretTests()
Adam Langley2ae77d22014-10-28 17:29:33 -07003535 addRenegotiationTests()
David Benjamin5e961c12014-11-07 01:48:35 -05003536 addDTLSReplayTests()
David Benjamin000800a2014-11-14 01:43:59 -05003537 addSigningHashTests()
Feng Lu41aa3252014-11-21 22:47:56 -08003538 addFastRadioPaddingTests()
David Benjamin83f90402015-01-27 01:09:43 -05003539 addDTLSRetransmitTests()
David Benjaminc565ebb2015-04-03 04:06:36 -04003540 addExportKeyingMaterialTests()
Adam Langleyaf0e32c2015-06-03 09:57:23 -07003541 addTLSUniqueTests()
David Benjamin43ec06f2014-08-05 02:28:57 -04003542 for _, async := range []bool{false, true} {
3543 for _, splitHandshake := range []bool{false, true} {
David Benjamin6fd297b2014-08-11 18:43:38 -04003544 for _, protocol := range []protocol{tls, dtls} {
3545 addStateMachineCoverageTests(async, splitHandshake, protocol)
3546 }
David Benjamin43ec06f2014-08-05 02:28:57 -04003547 }
3548 }
Adam Langley95c29f32014-06-20 12:00:00 -07003549
3550 var wg sync.WaitGroup
3551
David Benjamin2bc8e6f2014-08-02 15:22:37 -04003552 numWorkers := *flagNumWorkers
Adam Langley95c29f32014-06-20 12:00:00 -07003553
3554 statusChan := make(chan statusMsg, numWorkers)
3555 testChan := make(chan *testCase, numWorkers)
David Benjamin5f237bc2015-02-11 17:14:15 -05003556 doneChan := make(chan *testOutput)
Adam Langley95c29f32014-06-20 12:00:00 -07003557
David Benjamin025b3d32014-07-01 19:53:04 -04003558 go statusPrinter(doneChan, statusChan, len(testCases))
Adam Langley95c29f32014-06-20 12:00:00 -07003559
3560 for i := 0; i < numWorkers; i++ {
3561 wg.Add(1)
David Benjamin884fdf12014-08-02 15:28:23 -04003562 go worker(statusChan, testChan, *flagBuildDir, &wg)
Adam Langley95c29f32014-06-20 12:00:00 -07003563 }
3564
David Benjamin025b3d32014-07-01 19:53:04 -04003565 for i := range testCases {
3566 if len(*flagTest) == 0 || *flagTest == testCases[i].name {
3567 testChan <- &testCases[i]
Adam Langley95c29f32014-06-20 12:00:00 -07003568 }
3569 }
3570
3571 close(testChan)
3572 wg.Wait()
3573 close(statusChan)
David Benjamin5f237bc2015-02-11 17:14:15 -05003574 testOutput := <-doneChan
Adam Langley95c29f32014-06-20 12:00:00 -07003575
3576 fmt.Printf("\n")
David Benjamin5f237bc2015-02-11 17:14:15 -05003577
3578 if *jsonOutput != "" {
3579 if err := testOutput.writeTo(*jsonOutput); err != nil {
3580 fmt.Fprintf(os.Stderr, "Error: %s\n", err)
3581 }
3582 }
David Benjamin2ab7a862015-04-04 17:02:18 -04003583
3584 if !testOutput.allPassed {
3585 os.Exit(1)
3586 }
Adam Langley95c29f32014-06-20 12:00:00 -07003587}