blob: 2b25d35eef23feca3d8907000d60111615bd2c94 [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 Langley95c29f32014-06-20 12:00:00 -0700204}
205
David Benjamin025b3d32014-07-01 19:53:04 -0400206var testCases = []testCase{
Adam Langley95c29f32014-06-20 12:00:00 -0700207 {
208 name: "BadRSASignature",
209 config: Config{
210 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
211 Bugs: ProtocolBugs{
212 InvalidSKXSignature: true,
213 },
214 },
215 shouldFail: true,
David Benjamin25f08462015-04-15 16:13:49 -0400216 expectedError: ":BAD_SIGNATURE:",
Adam Langley95c29f32014-06-20 12:00:00 -0700217 },
218 {
219 name: "BadECDSASignature",
220 config: Config{
221 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
222 Bugs: ProtocolBugs{
223 InvalidSKXSignature: true,
224 },
225 Certificates: []Certificate{getECDSACertificate()},
226 },
227 shouldFail: true,
228 expectedError: ":BAD_SIGNATURE:",
229 },
230 {
231 name: "BadECDSACurve",
232 config: Config{
233 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
234 Bugs: ProtocolBugs{
235 InvalidSKXCurve: true,
236 },
237 Certificates: []Certificate{getECDSACertificate()},
238 },
239 shouldFail: true,
240 expectedError: ":WRONG_CURVE:",
241 },
Adam Langleyac61fa32014-06-23 12:03:11 -0700242 {
David Benjamina8e3e0e2014-08-06 22:11:10 -0400243 testType: serverTest,
244 name: "BadRSAVersion",
245 config: Config{
246 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
247 Bugs: ProtocolBugs{
248 RsaClientKeyExchangeVersion: VersionTLS11,
249 },
250 },
251 shouldFail: true,
252 expectedError: ":DECRYPTION_FAILED_OR_BAD_RECORD_MAC:",
253 },
254 {
David Benjamin325b5c32014-07-01 19:40:31 -0400255 name: "NoFallbackSCSV",
Adam Langleyac61fa32014-06-23 12:03:11 -0700256 config: Config{
257 Bugs: ProtocolBugs{
258 FailIfNotFallbackSCSV: true,
259 },
260 },
261 shouldFail: true,
262 expectedLocalError: "no fallback SCSV found",
263 },
David Benjamin325b5c32014-07-01 19:40:31 -0400264 {
David Benjamin2a0c4962014-08-22 23:46:35 -0400265 name: "SendFallbackSCSV",
David Benjamin325b5c32014-07-01 19:40:31 -0400266 config: Config{
267 Bugs: ProtocolBugs{
268 FailIfNotFallbackSCSV: true,
269 },
270 },
271 flags: []string{"-fallback-scsv"},
272 },
David Benjamin197b3ab2014-07-02 18:37:33 -0400273 {
David Benjamin7b030512014-07-08 17:30:11 -0400274 name: "ClientCertificateTypes",
275 config: Config{
276 ClientAuth: RequestClientCert,
277 ClientCertificateTypes: []byte{
278 CertTypeDSSSign,
279 CertTypeRSASign,
280 CertTypeECDSASign,
281 },
282 },
David Benjamin2561dc32014-08-24 01:25:27 -0400283 flags: []string{
284 "-expect-certificate-types",
285 base64.StdEncoding.EncodeToString([]byte{
286 CertTypeDSSSign,
287 CertTypeRSASign,
288 CertTypeECDSASign,
289 }),
290 },
David Benjamin7b030512014-07-08 17:30:11 -0400291 },
David Benjamin636293b2014-07-08 17:59:18 -0400292 {
293 name: "NoClientCertificate",
294 config: Config{
295 ClientAuth: RequireAnyClientCert,
296 },
297 shouldFail: true,
298 expectedLocalError: "client didn't provide a certificate",
299 },
David Benjamin1c375dd2014-07-12 00:48:23 -0400300 {
301 name: "UnauthenticatedECDH",
302 config: Config{
303 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
304 Bugs: ProtocolBugs{
305 UnauthenticatedECDH: true,
306 },
307 },
308 shouldFail: true,
David Benjamine8f3d662014-07-12 01:10:19 -0400309 expectedError: ":UNEXPECTED_MESSAGE:",
David Benjamin1c375dd2014-07-12 00:48:23 -0400310 },
David Benjamin9c651c92014-07-12 13:27:45 -0400311 {
David Benjamindcd979f2015-04-20 18:26:52 -0400312 name: "SkipCertificateStatus",
313 config: Config{
314 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
315 Bugs: ProtocolBugs{
316 SkipCertificateStatus: true,
317 },
318 },
319 flags: []string{
320 "-enable-ocsp-stapling",
321 },
322 },
323 {
David Benjamin9c651c92014-07-12 13:27:45 -0400324 name: "SkipServerKeyExchange",
325 config: Config{
326 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
327 Bugs: ProtocolBugs{
328 SkipServerKeyExchange: true,
329 },
330 },
331 shouldFail: true,
332 expectedError: ":UNEXPECTED_MESSAGE:",
333 },
David Benjamin1f5f62b2014-07-12 16:18:02 -0400334 {
David Benjamina0e52232014-07-19 17:39:58 -0400335 name: "SkipChangeCipherSpec-Client",
336 config: Config{
337 Bugs: ProtocolBugs{
338 SkipChangeCipherSpec: true,
339 },
340 },
341 shouldFail: true,
David Benjamin86271ee2014-07-21 16:14:03 -0400342 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
David Benjamina0e52232014-07-19 17:39:58 -0400343 },
344 {
345 testType: serverTest,
346 name: "SkipChangeCipherSpec-Server",
347 config: Config{
348 Bugs: ProtocolBugs{
349 SkipChangeCipherSpec: true,
350 },
351 },
352 shouldFail: true,
David Benjamin86271ee2014-07-21 16:14:03 -0400353 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
David Benjamina0e52232014-07-19 17:39:58 -0400354 },
David Benjamin42be6452014-07-21 14:50:23 -0400355 {
356 testType: serverTest,
357 name: "SkipChangeCipherSpec-Server-NPN",
358 config: Config{
359 NextProtos: []string{"bar"},
360 Bugs: ProtocolBugs{
361 SkipChangeCipherSpec: true,
362 },
363 },
364 flags: []string{
365 "-advertise-npn", "\x03foo\x03bar\x03baz",
366 },
367 shouldFail: true,
David Benjamin86271ee2014-07-21 16:14:03 -0400368 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
369 },
370 {
371 name: "FragmentAcrossChangeCipherSpec-Client",
372 config: Config{
373 Bugs: ProtocolBugs{
374 FragmentAcrossChangeCipherSpec: true,
375 },
376 },
377 shouldFail: true,
378 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
379 },
380 {
381 testType: serverTest,
382 name: "FragmentAcrossChangeCipherSpec-Server",
383 config: Config{
384 Bugs: ProtocolBugs{
385 FragmentAcrossChangeCipherSpec: true,
386 },
387 },
388 shouldFail: true,
389 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
390 },
391 {
392 testType: serverTest,
393 name: "FragmentAcrossChangeCipherSpec-Server-NPN",
394 config: Config{
395 NextProtos: []string{"bar"},
396 Bugs: ProtocolBugs{
397 FragmentAcrossChangeCipherSpec: true,
398 },
399 },
400 flags: []string{
401 "-advertise-npn", "\x03foo\x03bar\x03baz",
402 },
403 shouldFail: true,
404 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
David Benjamin42be6452014-07-21 14:50:23 -0400405 },
David Benjaminf3ec83d2014-07-21 22:42:34 -0400406 {
407 testType: serverTest,
David Benjamin3fd1fbd2015-02-03 16:07:32 -0500408 name: "Alert",
409 config: Config{
410 Bugs: ProtocolBugs{
411 SendSpuriousAlert: alertRecordOverflow,
412 },
413 },
414 shouldFail: true,
415 expectedError: ":TLSV1_ALERT_RECORD_OVERFLOW:",
416 },
417 {
418 protocol: dtls,
419 testType: serverTest,
420 name: "Alert-DTLS",
421 config: Config{
422 Bugs: ProtocolBugs{
423 SendSpuriousAlert: alertRecordOverflow,
424 },
425 },
426 shouldFail: true,
427 expectedError: ":TLSV1_ALERT_RECORD_OVERFLOW:",
428 },
429 {
430 testType: serverTest,
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400431 name: "FragmentAlert",
432 config: Config{
433 Bugs: ProtocolBugs{
David Benjaminca6c8262014-11-15 19:06:08 -0500434 FragmentAlert: true,
David Benjamin3fd1fbd2015-02-03 16:07:32 -0500435 SendSpuriousAlert: alertRecordOverflow,
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400436 },
437 },
438 shouldFail: true,
439 expectedError: ":BAD_ALERT:",
440 },
441 {
David Benjamin0ea8dda2015-01-31 20:33:40 -0500442 protocol: dtls,
443 testType: serverTest,
444 name: "FragmentAlert-DTLS",
445 config: Config{
446 Bugs: ProtocolBugs{
447 FragmentAlert: true,
David Benjamin3fd1fbd2015-02-03 16:07:32 -0500448 SendSpuriousAlert: alertRecordOverflow,
David Benjamin0ea8dda2015-01-31 20:33:40 -0500449 },
450 },
451 shouldFail: true,
452 expectedError: ":BAD_ALERT:",
453 },
454 {
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400455 testType: serverTest,
David Benjaminf3ec83d2014-07-21 22:42:34 -0400456 name: "EarlyChangeCipherSpec-server-1",
457 config: Config{
458 Bugs: ProtocolBugs{
459 EarlyChangeCipherSpec: 1,
460 },
461 },
462 shouldFail: true,
463 expectedError: ":CCS_RECEIVED_EARLY:",
464 },
465 {
466 testType: serverTest,
467 name: "EarlyChangeCipherSpec-server-2",
468 config: Config{
469 Bugs: ProtocolBugs{
470 EarlyChangeCipherSpec: 2,
471 },
472 },
473 shouldFail: true,
474 expectedError: ":CCS_RECEIVED_EARLY:",
475 },
David Benjamind23f4122014-07-23 15:09:48 -0400476 {
David Benjamind23f4122014-07-23 15:09:48 -0400477 name: "SkipNewSessionTicket",
478 config: Config{
479 Bugs: ProtocolBugs{
480 SkipNewSessionTicket: true,
481 },
482 },
483 shouldFail: true,
484 expectedError: ":CCS_RECEIVED_EARLY:",
485 },
David Benjamin7e3305e2014-07-28 14:52:32 -0400486 {
David Benjamind86c7672014-08-02 04:07:12 -0400487 testType: serverTest,
David Benjaminbef270a2014-08-02 04:22:02 -0400488 name: "FallbackSCSV",
489 config: Config{
490 MaxVersion: VersionTLS11,
491 Bugs: ProtocolBugs{
492 SendFallbackSCSV: true,
493 },
494 },
495 shouldFail: true,
496 expectedError: ":INAPPROPRIATE_FALLBACK:",
497 },
498 {
499 testType: serverTest,
500 name: "FallbackSCSV-VersionMatch",
501 config: Config{
502 Bugs: ProtocolBugs{
503 SendFallbackSCSV: true,
504 },
505 },
506 },
David Benjamin98214542014-08-07 18:02:39 -0400507 {
508 testType: serverTest,
509 name: "FragmentedClientVersion",
510 config: Config{
511 Bugs: ProtocolBugs{
512 MaxHandshakeRecordLength: 1,
513 FragmentClientVersion: true,
514 },
515 },
David Benjamin82c9e902014-12-12 15:55:27 -0500516 expectedVersion: VersionTLS12,
David Benjamin98214542014-08-07 18:02:39 -0400517 },
David Benjamin98e882e2014-08-08 13:24:34 -0400518 {
519 testType: serverTest,
520 name: "MinorVersionTolerance",
521 config: Config{
522 Bugs: ProtocolBugs{
523 SendClientVersion: 0x03ff,
524 },
525 },
526 expectedVersion: VersionTLS12,
527 },
528 {
529 testType: serverTest,
530 name: "MajorVersionTolerance",
531 config: Config{
532 Bugs: ProtocolBugs{
533 SendClientVersion: 0x0400,
534 },
535 },
536 expectedVersion: VersionTLS12,
537 },
538 {
539 testType: serverTest,
540 name: "VersionTooLow",
541 config: Config{
542 Bugs: ProtocolBugs{
543 SendClientVersion: 0x0200,
544 },
545 },
546 shouldFail: true,
547 expectedError: ":UNSUPPORTED_PROTOCOL:",
548 },
549 {
550 testType: serverTest,
551 name: "HttpGET",
552 sendPrefix: "GET / HTTP/1.0\n",
553 shouldFail: true,
554 expectedError: ":HTTP_REQUEST:",
555 },
556 {
557 testType: serverTest,
558 name: "HttpPOST",
559 sendPrefix: "POST / HTTP/1.0\n",
560 shouldFail: true,
561 expectedError: ":HTTP_REQUEST:",
562 },
563 {
564 testType: serverTest,
565 name: "HttpHEAD",
566 sendPrefix: "HEAD / HTTP/1.0\n",
567 shouldFail: true,
568 expectedError: ":HTTP_REQUEST:",
569 },
570 {
571 testType: serverTest,
572 name: "HttpPUT",
573 sendPrefix: "PUT / HTTP/1.0\n",
574 shouldFail: true,
575 expectedError: ":HTTP_REQUEST:",
576 },
577 {
578 testType: serverTest,
579 name: "HttpCONNECT",
580 sendPrefix: "CONNECT www.google.com:443 HTTP/1.0\n",
581 shouldFail: true,
582 expectedError: ":HTTPS_PROXY_REQUEST:",
583 },
David Benjamin39ebf532014-08-31 02:23:49 -0400584 {
David Benjaminf080ecd2014-12-11 18:48:59 -0500585 testType: serverTest,
586 name: "Garbage",
587 sendPrefix: "blah",
588 shouldFail: true,
589 expectedError: ":UNKNOWN_PROTOCOL:",
590 },
591 {
David Benjamin39ebf532014-08-31 02:23:49 -0400592 name: "SkipCipherVersionCheck",
593 config: Config{
594 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
595 MaxVersion: VersionTLS11,
596 Bugs: ProtocolBugs{
597 SkipCipherVersionCheck: true,
598 },
599 },
600 shouldFail: true,
601 expectedError: ":WRONG_CIPHER_RETURNED:",
602 },
David Benjamin9114fae2014-11-08 11:41:14 -0500603 {
David Benjamina3e89492015-02-26 15:16:22 -0500604 name: "RSAEphemeralKey",
David Benjamin9114fae2014-11-08 11:41:14 -0500605 config: Config{
606 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
607 Bugs: ProtocolBugs{
David Benjamina3e89492015-02-26 15:16:22 -0500608 RSAEphemeralKey: true,
David Benjamin9114fae2014-11-08 11:41:14 -0500609 },
610 },
611 shouldFail: true,
612 expectedError: ":UNEXPECTED_MESSAGE:",
613 },
David Benjamin128dbc32014-12-01 01:27:42 -0500614 {
615 name: "DisableEverything",
616 flags: []string{"-no-tls12", "-no-tls11", "-no-tls1", "-no-ssl3"},
617 shouldFail: true,
618 expectedError: ":WRONG_SSL_VERSION:",
619 },
620 {
621 protocol: dtls,
622 name: "DisableEverything-DTLS",
623 flags: []string{"-no-tls12", "-no-tls1"},
624 shouldFail: true,
625 expectedError: ":WRONG_SSL_VERSION:",
626 },
David Benjamin780d6dd2015-01-06 12:03:19 -0500627 {
628 name: "NoSharedCipher",
629 config: Config{
630 CipherSuites: []uint16{},
631 },
632 shouldFail: true,
633 expectedError: ":HANDSHAKE_FAILURE_ON_CLIENT_HELLO:",
634 },
David Benjamin13be1de2015-01-11 16:29:36 -0500635 {
636 protocol: dtls,
637 testType: serverTest,
638 name: "MTU",
639 config: Config{
640 Bugs: ProtocolBugs{
641 MaxPacketLength: 256,
642 },
643 },
644 flags: []string{"-mtu", "256"},
645 },
646 {
647 protocol: dtls,
648 testType: serverTest,
649 name: "MTUExceeded",
650 config: Config{
651 Bugs: ProtocolBugs{
652 MaxPacketLength: 255,
653 },
654 },
655 flags: []string{"-mtu", "256"},
656 shouldFail: true,
657 expectedLocalError: "dtls: exceeded maximum packet length",
658 },
David Benjamin6095de82014-12-27 01:50:38 -0500659 {
660 name: "CertMismatchRSA",
661 config: Config{
662 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
663 Certificates: []Certificate{getECDSACertificate()},
664 Bugs: ProtocolBugs{
665 SendCipherSuite: TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
666 },
667 },
668 shouldFail: true,
669 expectedError: ":WRONG_CERTIFICATE_TYPE:",
670 },
671 {
672 name: "CertMismatchECDSA",
673 config: Config{
674 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
675 Certificates: []Certificate{getRSACertificate()},
676 Bugs: ProtocolBugs{
677 SendCipherSuite: TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
678 },
679 },
680 shouldFail: true,
681 expectedError: ":WRONG_CERTIFICATE_TYPE:",
682 },
David Benjamin5fa3eba2015-01-22 16:35:40 -0500683 {
684 name: "TLSFatalBadPackets",
685 damageFirstWrite: true,
686 shouldFail: true,
687 expectedError: ":DECRYPTION_FAILED_OR_BAD_RECORD_MAC:",
688 },
689 {
690 protocol: dtls,
691 name: "DTLSIgnoreBadPackets",
692 damageFirstWrite: true,
693 },
694 {
695 protocol: dtls,
696 name: "DTLSIgnoreBadPackets-Async",
697 damageFirstWrite: true,
698 flags: []string{"-async"},
699 },
David Benjamin4189bd92015-01-25 23:52:39 -0500700 {
701 name: "AppDataAfterChangeCipherSpec",
702 config: Config{
703 Bugs: ProtocolBugs{
704 AppDataAfterChangeCipherSpec: []byte("TEST MESSAGE"),
705 },
706 },
707 shouldFail: true,
708 expectedError: ":DATA_BETWEEN_CCS_AND_FINISHED:",
709 },
710 {
711 protocol: dtls,
712 name: "AppDataAfterChangeCipherSpec-DTLS",
713 config: Config{
714 Bugs: ProtocolBugs{
715 AppDataAfterChangeCipherSpec: []byte("TEST MESSAGE"),
716 },
717 },
David Benjamin4417d052015-04-05 04:17:25 -0400718 // BoringSSL's DTLS implementation will drop the out-of-order
719 // application data.
David Benjamin4189bd92015-01-25 23:52:39 -0500720 },
David Benjaminb3774b92015-01-31 17:16:01 -0500721 {
David Benjamindc3da932015-03-12 15:09:02 -0400722 name: "AlertAfterChangeCipherSpec",
723 config: Config{
724 Bugs: ProtocolBugs{
725 AlertAfterChangeCipherSpec: alertRecordOverflow,
726 },
727 },
728 shouldFail: true,
729 expectedError: ":TLSV1_ALERT_RECORD_OVERFLOW:",
730 },
731 {
732 protocol: dtls,
733 name: "AlertAfterChangeCipherSpec-DTLS",
734 config: Config{
735 Bugs: ProtocolBugs{
736 AlertAfterChangeCipherSpec: alertRecordOverflow,
737 },
738 },
739 shouldFail: true,
740 expectedError: ":TLSV1_ALERT_RECORD_OVERFLOW:",
741 },
742 {
David Benjaminb3774b92015-01-31 17:16:01 -0500743 protocol: dtls,
744 name: "ReorderHandshakeFragments-Small-DTLS",
745 config: Config{
746 Bugs: ProtocolBugs{
747 ReorderHandshakeFragments: true,
748 // Small enough that every handshake message is
749 // fragmented.
750 MaxHandshakeRecordLength: 2,
751 },
752 },
753 },
754 {
755 protocol: dtls,
756 name: "ReorderHandshakeFragments-Large-DTLS",
757 config: Config{
758 Bugs: ProtocolBugs{
759 ReorderHandshakeFragments: true,
760 // Large enough that no handshake message is
761 // fragmented.
David Benjaminb3774b92015-01-31 17:16:01 -0500762 MaxHandshakeRecordLength: 2048,
763 },
764 },
765 },
David Benjaminddb9f152015-02-03 15:44:39 -0500766 {
David Benjamin75381222015-03-02 19:30:30 -0500767 protocol: dtls,
768 name: "MixCompleteMessageWithFragments-DTLS",
769 config: Config{
770 Bugs: ProtocolBugs{
771 ReorderHandshakeFragments: true,
772 MixCompleteMessageWithFragments: true,
773 MaxHandshakeRecordLength: 2,
774 },
775 },
776 },
777 {
David Benjaminddb9f152015-02-03 15:44:39 -0500778 name: "SendInvalidRecordType",
779 config: Config{
780 Bugs: ProtocolBugs{
781 SendInvalidRecordType: true,
782 },
783 },
784 shouldFail: true,
785 expectedError: ":UNEXPECTED_RECORD:",
786 },
787 {
788 protocol: dtls,
789 name: "SendInvalidRecordType-DTLS",
790 config: Config{
791 Bugs: ProtocolBugs{
792 SendInvalidRecordType: true,
793 },
794 },
795 shouldFail: true,
796 expectedError: ":UNEXPECTED_RECORD:",
797 },
David Benjaminb80168e2015-02-08 18:30:14 -0500798 {
799 name: "FalseStart-SkipServerSecondLeg",
800 config: Config{
801 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
802 NextProtos: []string{"foo"},
803 Bugs: ProtocolBugs{
804 SkipNewSessionTicket: true,
805 SkipChangeCipherSpec: true,
806 SkipFinished: true,
807 ExpectFalseStart: true,
808 },
809 },
810 flags: []string{
811 "-false-start",
David Benjamin87e4acd2015-04-02 19:57:35 -0400812 "-handshake-never-done",
David Benjaminb80168e2015-02-08 18:30:14 -0500813 "-advertise-alpn", "\x03foo",
814 },
815 shimWritesFirst: true,
816 shouldFail: true,
817 expectedError: ":UNEXPECTED_RECORD:",
818 },
David Benjamin931ab342015-02-08 19:46:57 -0500819 {
820 name: "FalseStart-SkipServerSecondLeg-Implicit",
821 config: Config{
822 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
823 NextProtos: []string{"foo"},
824 Bugs: ProtocolBugs{
825 SkipNewSessionTicket: true,
826 SkipChangeCipherSpec: true,
827 SkipFinished: true,
828 },
829 },
830 flags: []string{
831 "-implicit-handshake",
832 "-false-start",
David Benjamin87e4acd2015-04-02 19:57:35 -0400833 "-handshake-never-done",
David Benjamin931ab342015-02-08 19:46:57 -0500834 "-advertise-alpn", "\x03foo",
835 },
836 shouldFail: true,
837 expectedError: ":UNEXPECTED_RECORD:",
838 },
David Benjamin6f5c0f42015-02-24 01:23:21 -0500839 {
840 testType: serverTest,
841 name: "FailEarlyCallback",
842 flags: []string{"-fail-early-callback"},
843 shouldFail: true,
844 expectedError: ":CONNECTION_REJECTED:",
845 expectedLocalError: "remote error: access denied",
846 },
David Benjaminbcb2d912015-02-24 23:45:43 -0500847 {
848 name: "WrongMessageType",
849 config: Config{
850 Bugs: ProtocolBugs{
851 WrongCertificateMessageType: true,
852 },
853 },
854 shouldFail: true,
855 expectedError: ":UNEXPECTED_MESSAGE:",
856 expectedLocalError: "remote error: unexpected message",
857 },
858 {
859 protocol: dtls,
860 name: "WrongMessageType-DTLS",
861 config: Config{
862 Bugs: ProtocolBugs{
863 WrongCertificateMessageType: true,
864 },
865 },
866 shouldFail: true,
867 expectedError: ":UNEXPECTED_MESSAGE:",
868 expectedLocalError: "remote error: unexpected message",
869 },
David Benjamin75381222015-03-02 19:30:30 -0500870 {
871 protocol: dtls,
872 name: "FragmentMessageTypeMismatch-DTLS",
873 config: Config{
874 Bugs: ProtocolBugs{
875 MaxHandshakeRecordLength: 2,
876 FragmentMessageTypeMismatch: true,
877 },
878 },
879 shouldFail: true,
880 expectedError: ":FRAGMENT_MISMATCH:",
881 },
882 {
883 protocol: dtls,
884 name: "FragmentMessageLengthMismatch-DTLS",
885 config: Config{
886 Bugs: ProtocolBugs{
887 MaxHandshakeRecordLength: 2,
888 FragmentMessageLengthMismatch: true,
889 },
890 },
891 shouldFail: true,
892 expectedError: ":FRAGMENT_MISMATCH:",
893 },
894 {
895 protocol: dtls,
896 name: "SplitFragmentHeader-DTLS",
897 config: Config{
898 Bugs: ProtocolBugs{
899 SplitFragmentHeader: true,
900 },
901 },
902 shouldFail: true,
903 expectedError: ":UNEXPECTED_MESSAGE:",
904 },
905 {
906 protocol: dtls,
907 name: "SplitFragmentBody-DTLS",
908 config: Config{
909 Bugs: ProtocolBugs{
910 SplitFragmentBody: true,
911 },
912 },
913 shouldFail: true,
914 expectedError: ":UNEXPECTED_MESSAGE:",
915 },
916 {
917 protocol: dtls,
918 name: "SendEmptyFragments-DTLS",
919 config: Config{
920 Bugs: ProtocolBugs{
921 SendEmptyFragments: true,
922 },
923 },
924 },
David Benjamin67d1fb52015-03-16 15:16:23 -0400925 {
926 name: "UnsupportedCipherSuite",
927 config: Config{
928 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
929 Bugs: ProtocolBugs{
930 IgnorePeerCipherPreferences: true,
931 },
932 },
933 flags: []string{"-cipher", "DEFAULT:!RC4"},
934 shouldFail: true,
935 expectedError: ":WRONG_CIPHER_RETURNED:",
936 },
David Benjamin340d5ed2015-03-21 02:21:37 -0400937 {
David Benjaminc574f412015-04-20 11:13:01 -0400938 name: "UnsupportedCurve",
939 config: Config{
940 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
941 // BoringSSL implements P-224 but doesn't enable it by
942 // default.
943 CurvePreferences: []CurveID{CurveP224},
944 Bugs: ProtocolBugs{
945 IgnorePeerCurvePreferences: true,
946 },
947 },
948 shouldFail: true,
949 expectedError: ":WRONG_CURVE:",
950 },
951 {
David Benjamin340d5ed2015-03-21 02:21:37 -0400952 name: "SendWarningAlerts",
953 config: Config{
954 Bugs: ProtocolBugs{
955 SendWarningAlerts: alertAccessDenied,
956 },
957 },
958 },
959 {
960 protocol: dtls,
961 name: "SendWarningAlerts-DTLS",
962 config: Config{
963 Bugs: ProtocolBugs{
964 SendWarningAlerts: alertAccessDenied,
965 },
966 },
967 },
David Benjamin513f0ea2015-04-02 19:33:31 -0400968 {
969 name: "BadFinished",
970 config: Config{
971 Bugs: ProtocolBugs{
972 BadFinished: true,
973 },
974 },
975 shouldFail: true,
976 expectedError: ":DIGEST_CHECK_FAILED:",
977 },
978 {
979 name: "FalseStart-BadFinished",
980 config: Config{
981 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
982 NextProtos: []string{"foo"},
983 Bugs: ProtocolBugs{
984 BadFinished: true,
985 ExpectFalseStart: true,
986 },
987 },
988 flags: []string{
989 "-false-start",
David Benjamin87e4acd2015-04-02 19:57:35 -0400990 "-handshake-never-done",
David Benjamin513f0ea2015-04-02 19:33:31 -0400991 "-advertise-alpn", "\x03foo",
992 },
993 shimWritesFirst: true,
994 shouldFail: true,
995 expectedError: ":DIGEST_CHECK_FAILED:",
996 },
David Benjamin1c633152015-04-02 20:19:11 -0400997 {
998 name: "NoFalseStart-NoALPN",
999 config: Config{
1000 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1001 Bugs: ProtocolBugs{
1002 ExpectFalseStart: true,
1003 AlertBeforeFalseStartTest: alertAccessDenied,
1004 },
1005 },
1006 flags: []string{
1007 "-false-start",
1008 },
1009 shimWritesFirst: true,
1010 shouldFail: true,
1011 expectedError: ":TLSV1_ALERT_ACCESS_DENIED:",
1012 expectedLocalError: "tls: peer did not false start: EOF",
1013 },
1014 {
1015 name: "NoFalseStart-NoAEAD",
1016 config: Config{
1017 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1018 NextProtos: []string{"foo"},
1019 Bugs: ProtocolBugs{
1020 ExpectFalseStart: true,
1021 AlertBeforeFalseStartTest: alertAccessDenied,
1022 },
1023 },
1024 flags: []string{
1025 "-false-start",
1026 "-advertise-alpn", "\x03foo",
1027 },
1028 shimWritesFirst: true,
1029 shouldFail: true,
1030 expectedError: ":TLSV1_ALERT_ACCESS_DENIED:",
1031 expectedLocalError: "tls: peer did not false start: EOF",
1032 },
1033 {
1034 name: "NoFalseStart-RSA",
1035 config: Config{
1036 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
1037 NextProtos: []string{"foo"},
1038 Bugs: ProtocolBugs{
1039 ExpectFalseStart: true,
1040 AlertBeforeFalseStartTest: alertAccessDenied,
1041 },
1042 },
1043 flags: []string{
1044 "-false-start",
1045 "-advertise-alpn", "\x03foo",
1046 },
1047 shimWritesFirst: true,
1048 shouldFail: true,
1049 expectedError: ":TLSV1_ALERT_ACCESS_DENIED:",
1050 expectedLocalError: "tls: peer did not false start: EOF",
1051 },
1052 {
1053 name: "NoFalseStart-DHE_RSA",
1054 config: Config{
1055 CipherSuites: []uint16{TLS_DHE_RSA_WITH_AES_128_GCM_SHA256},
1056 NextProtos: []string{"foo"},
1057 Bugs: ProtocolBugs{
1058 ExpectFalseStart: true,
1059 AlertBeforeFalseStartTest: alertAccessDenied,
1060 },
1061 },
1062 flags: []string{
1063 "-false-start",
1064 "-advertise-alpn", "\x03foo",
1065 },
1066 shimWritesFirst: true,
1067 shouldFail: true,
1068 expectedError: ":TLSV1_ALERT_ACCESS_DENIED:",
1069 expectedLocalError: "tls: peer did not false start: EOF",
1070 },
David Benjamin55a43642015-04-20 14:45:55 -04001071 {
1072 testType: serverTest,
1073 name: "NoSupportedCurves",
1074 config: Config{
1075 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1076 Bugs: ProtocolBugs{
1077 NoSupportedCurves: true,
1078 },
1079 },
1080 },
David Benjamin90da8c82015-04-20 14:57:57 -04001081 {
1082 testType: serverTest,
1083 name: "NoCommonCurves",
1084 config: Config{
1085 CipherSuites: []uint16{
1086 TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
1087 TLS_DHE_RSA_WITH_AES_128_GCM_SHA256,
1088 },
1089 CurvePreferences: []CurveID{CurveP224},
1090 },
1091 expectedCipher: TLS_DHE_RSA_WITH_AES_128_GCM_SHA256,
1092 },
David Benjamin9a41d1b2015-05-16 01:30:09 -04001093 {
1094 protocol: dtls,
1095 name: "SendSplitAlert-Sync",
1096 config: Config{
1097 Bugs: ProtocolBugs{
1098 SendSplitAlert: true,
1099 },
1100 },
1101 },
1102 {
1103 protocol: dtls,
1104 name: "SendSplitAlert-Async",
1105 config: Config{
1106 Bugs: ProtocolBugs{
1107 SendSplitAlert: true,
1108 },
1109 },
1110 flags: []string{"-async"},
1111 },
David Benjaminbd15a8e2015-05-29 18:48:16 -04001112 {
1113 protocol: dtls,
1114 name: "PackDTLSHandshake",
1115 config: Config{
1116 Bugs: ProtocolBugs{
1117 MaxHandshakeRecordLength: 2,
1118 PackHandshakeFragments: 20,
1119 PackHandshakeRecords: 200,
1120 },
1121 },
1122 },
David Benjamin0fa40122015-05-30 17:13:12 -04001123 {
1124 testType: serverTest,
1125 protocol: dtls,
1126 name: "NoRC4-DTLS",
1127 config: Config{
1128 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_RC4_128_SHA},
1129 Bugs: ProtocolBugs{
1130 EnableAllCiphersInDTLS: true,
1131 },
1132 },
1133 shouldFail: true,
1134 expectedError: ":NO_SHARED_CIPHER:",
1135 },
Adam Langley95c29f32014-06-20 12:00:00 -07001136}
1137
David Benjamin01fe8202014-09-24 15:21:44 -04001138func doExchange(test *testCase, config *Config, conn net.Conn, messageLen int, isResume bool) error {
David Benjamin65ea8ff2014-11-23 03:01:00 -05001139 var connDebug *recordingConn
David Benjamin5fa3eba2015-01-22 16:35:40 -05001140 var connDamage *damageAdaptor
David Benjamin65ea8ff2014-11-23 03:01:00 -05001141 if *flagDebug {
1142 connDebug = &recordingConn{Conn: conn}
1143 conn = connDebug
1144 defer func() {
1145 connDebug.WriteTo(os.Stdout)
1146 }()
1147 }
1148
David Benjamin6fd297b2014-08-11 18:43:38 -04001149 if test.protocol == dtls {
David Benjamin83f90402015-01-27 01:09:43 -05001150 config.Bugs.PacketAdaptor = newPacketAdaptor(conn)
1151 conn = config.Bugs.PacketAdaptor
David Benjamin5e961c12014-11-07 01:48:35 -05001152 if test.replayWrites {
1153 conn = newReplayAdaptor(conn)
1154 }
David Benjamin6fd297b2014-08-11 18:43:38 -04001155 }
1156
David Benjamin5fa3eba2015-01-22 16:35:40 -05001157 if test.damageFirstWrite {
1158 connDamage = newDamageAdaptor(conn)
1159 conn = connDamage
1160 }
1161
David Benjamin6fd297b2014-08-11 18:43:38 -04001162 if test.sendPrefix != "" {
1163 if _, err := conn.Write([]byte(test.sendPrefix)); err != nil {
1164 return err
1165 }
David Benjamin98e882e2014-08-08 13:24:34 -04001166 }
1167
David Benjamin1d5c83e2014-07-22 19:20:02 -04001168 var tlsConn *Conn
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001169 if test.testType == clientTest {
David Benjamin6fd297b2014-08-11 18:43:38 -04001170 if test.protocol == dtls {
1171 tlsConn = DTLSServer(conn, config)
1172 } else {
1173 tlsConn = Server(conn, config)
1174 }
David Benjamin1d5c83e2014-07-22 19:20:02 -04001175 } else {
1176 config.InsecureSkipVerify = true
David Benjamin6fd297b2014-08-11 18:43:38 -04001177 if test.protocol == dtls {
1178 tlsConn = DTLSClient(conn, config)
1179 } else {
1180 tlsConn = Client(conn, config)
1181 }
David Benjamin1d5c83e2014-07-22 19:20:02 -04001182 }
1183
Adam Langley95c29f32014-06-20 12:00:00 -07001184 if err := tlsConn.Handshake(); err != nil {
1185 return err
1186 }
Kenny Root7fdeaf12014-08-05 15:23:37 -07001187
David Benjamin01fe8202014-09-24 15:21:44 -04001188 // TODO(davidben): move all per-connection expectations into a dedicated
1189 // expectations struct that can be specified separately for the two
1190 // legs.
1191 expectedVersion := test.expectedVersion
1192 if isResume && test.expectedResumeVersion != 0 {
1193 expectedVersion = test.expectedResumeVersion
1194 }
Adam Langleyb0eef0a2015-06-02 10:47:39 -07001195 connState := tlsConn.ConnectionState()
1196 if vers := connState.Version; expectedVersion != 0 && vers != expectedVersion {
David Benjamin01fe8202014-09-24 15:21:44 -04001197 return fmt.Errorf("got version %x, expected %x", vers, expectedVersion)
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001198 }
1199
Adam Langleyb0eef0a2015-06-02 10:47:39 -07001200 if cipher := connState.CipherSuite; test.expectedCipher != 0 && cipher != test.expectedCipher {
David Benjamin90da8c82015-04-20 14:57:57 -04001201 return fmt.Errorf("got cipher %x, expected %x", cipher, test.expectedCipher)
1202 }
Adam Langleyb0eef0a2015-06-02 10:47:39 -07001203 if didResume := connState.DidResume; isResume && didResume == test.expectResumeRejected {
1204 return fmt.Errorf("didResume is %t, but we expected the opposite", didResume)
1205 }
David Benjamin90da8c82015-04-20 14:57:57 -04001206
David Benjamina08e49d2014-08-24 01:46:07 -04001207 if test.expectChannelID {
Adam Langleyb0eef0a2015-06-02 10:47:39 -07001208 channelID := connState.ChannelID
David Benjamina08e49d2014-08-24 01:46:07 -04001209 if channelID == nil {
1210 return fmt.Errorf("no channel ID negotiated")
1211 }
1212 if channelID.Curve != channelIDKey.Curve ||
1213 channelIDKey.X.Cmp(channelIDKey.X) != 0 ||
1214 channelIDKey.Y.Cmp(channelIDKey.Y) != 0 {
1215 return fmt.Errorf("incorrect channel ID")
1216 }
1217 }
1218
David Benjaminae2888f2014-09-06 12:58:58 -04001219 if expected := test.expectedNextProto; expected != "" {
Adam Langleyb0eef0a2015-06-02 10:47:39 -07001220 if actual := connState.NegotiatedProtocol; actual != expected {
David Benjaminae2888f2014-09-06 12:58:58 -04001221 return fmt.Errorf("next proto mismatch: got %s, wanted %s", actual, expected)
1222 }
1223 }
1224
David Benjaminfc7b0862014-09-06 13:21:53 -04001225 if test.expectedNextProtoType != 0 {
Adam Langleyb0eef0a2015-06-02 10:47:39 -07001226 if (test.expectedNextProtoType == alpn) != connState.NegotiatedProtocolFromALPN {
David Benjaminfc7b0862014-09-06 13:21:53 -04001227 return fmt.Errorf("next proto type mismatch")
1228 }
1229 }
1230
Adam Langleyb0eef0a2015-06-02 10:47:39 -07001231 if p := connState.SRTPProtectionProfile; p != test.expectedSRTPProtectionProfile {
David Benjaminca6c8262014-11-15 19:06:08 -05001232 return fmt.Errorf("SRTP profile mismatch: got %d, wanted %d", p, test.expectedSRTPProtectionProfile)
1233 }
1234
David Benjaminc565ebb2015-04-03 04:06:36 -04001235 if test.exportKeyingMaterial > 0 {
1236 actual := make([]byte, test.exportKeyingMaterial)
1237 if _, err := io.ReadFull(tlsConn, actual); err != nil {
1238 return err
1239 }
1240 expected, err := tlsConn.ExportKeyingMaterial(test.exportKeyingMaterial, []byte(test.exportLabel), []byte(test.exportContext), test.useExportContext)
1241 if err != nil {
1242 return err
1243 }
1244 if !bytes.Equal(actual, expected) {
1245 return fmt.Errorf("keying material mismatch")
1246 }
1247 }
1248
David Benjamine58c4f52014-08-24 03:47:07 -04001249 if test.shimWritesFirst {
1250 var buf [5]byte
1251 _, err := io.ReadFull(tlsConn, buf[:])
1252 if err != nil {
1253 return err
1254 }
1255 if string(buf[:]) != "hello" {
1256 return fmt.Errorf("bad initial message")
1257 }
1258 }
1259
Adam Langleycf2d4f42014-10-28 19:06:14 -07001260 if test.renegotiate {
1261 if test.renegotiateCiphers != nil {
1262 config.CipherSuites = test.renegotiateCiphers
1263 }
1264 if err := tlsConn.Renegotiate(); err != nil {
1265 return err
1266 }
1267 } else if test.renegotiateCiphers != nil {
1268 panic("renegotiateCiphers without renegotiate")
1269 }
1270
David Benjamin5fa3eba2015-01-22 16:35:40 -05001271 if test.damageFirstWrite {
1272 connDamage.setDamage(true)
1273 tlsConn.Write([]byte("DAMAGED WRITE"))
1274 connDamage.setDamage(false)
1275 }
1276
Kenny Root7fdeaf12014-08-05 15:23:37 -07001277 if messageLen < 0 {
David Benjamin6fd297b2014-08-11 18:43:38 -04001278 if test.protocol == dtls {
1279 return fmt.Errorf("messageLen < 0 not supported for DTLS tests")
1280 }
Kenny Root7fdeaf12014-08-05 15:23:37 -07001281 // Read until EOF.
1282 _, err := io.Copy(ioutil.Discard, tlsConn)
1283 return err
1284 }
1285
David Benjamin4417d052015-04-05 04:17:25 -04001286 if messageLen == 0 {
1287 messageLen = 32
Adam Langley80842bd2014-06-20 12:00:00 -07001288 }
David Benjamin4417d052015-04-05 04:17:25 -04001289 testMessage := make([]byte, messageLen)
1290 for i := range testMessage {
1291 testMessage[i] = 0x42
1292 }
1293 tlsConn.Write(testMessage)
Adam Langley95c29f32014-06-20 12:00:00 -07001294
1295 buf := make([]byte, len(testMessage))
David Benjamin6fd297b2014-08-11 18:43:38 -04001296 if test.protocol == dtls {
1297 bufTmp := make([]byte, len(buf)+1)
1298 n, err := tlsConn.Read(bufTmp)
1299 if err != nil {
1300 return err
1301 }
1302 if n != len(buf) {
1303 return fmt.Errorf("bad reply; length mismatch (%d vs %d)", n, len(buf))
1304 }
1305 copy(buf, bufTmp)
1306 } else {
1307 _, err := io.ReadFull(tlsConn, buf)
1308 if err != nil {
1309 return err
1310 }
Adam Langley95c29f32014-06-20 12:00:00 -07001311 }
1312
1313 for i, v := range buf {
1314 if v != testMessage[i]^0xff {
1315 return fmt.Errorf("bad reply contents at byte %d", i)
1316 }
1317 }
1318
1319 return nil
1320}
1321
David Benjamin325b5c32014-07-01 19:40:31 -04001322func valgrindOf(dbAttach bool, path string, args ...string) *exec.Cmd {
1323 valgrindArgs := []string{"--error-exitcode=99", "--track-origins=yes", "--leak-check=full"}
Adam Langley95c29f32014-06-20 12:00:00 -07001324 if dbAttach {
David Benjamin325b5c32014-07-01 19:40:31 -04001325 valgrindArgs = append(valgrindArgs, "--db-attach=yes", "--db-command=xterm -e gdb -nw %f %p")
Adam Langley95c29f32014-06-20 12:00:00 -07001326 }
David Benjamin325b5c32014-07-01 19:40:31 -04001327 valgrindArgs = append(valgrindArgs, path)
1328 valgrindArgs = append(valgrindArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -07001329
David Benjamin325b5c32014-07-01 19:40:31 -04001330 return exec.Command("valgrind", valgrindArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -07001331}
1332
David Benjamin325b5c32014-07-01 19:40:31 -04001333func gdbOf(path string, args ...string) *exec.Cmd {
1334 xtermArgs := []string{"-e", "gdb", "--args"}
1335 xtermArgs = append(xtermArgs, path)
1336 xtermArgs = append(xtermArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -07001337
David Benjamin325b5c32014-07-01 19:40:31 -04001338 return exec.Command("xterm", xtermArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -07001339}
1340
Adam Langley69a01602014-11-17 17:26:55 -08001341type moreMallocsError struct{}
1342
1343func (moreMallocsError) Error() string {
1344 return "child process did not exhaust all allocation calls"
1345}
1346
1347var errMoreMallocs = moreMallocsError{}
1348
David Benjamin87c8a642015-02-21 01:54:29 -05001349// accept accepts a connection from listener, unless waitChan signals a process
1350// exit first.
1351func acceptOrWait(listener net.Listener, waitChan chan error) (net.Conn, error) {
1352 type connOrError struct {
1353 conn net.Conn
1354 err error
1355 }
1356 connChan := make(chan connOrError, 1)
1357 go func() {
1358 conn, err := listener.Accept()
1359 connChan <- connOrError{conn, err}
1360 close(connChan)
1361 }()
1362 select {
1363 case result := <-connChan:
1364 return result.conn, result.err
1365 case childErr := <-waitChan:
1366 waitChan <- childErr
1367 return nil, fmt.Errorf("child exited early: %s", childErr)
1368 }
1369}
1370
Adam Langley69a01602014-11-17 17:26:55 -08001371func runTest(test *testCase, buildDir string, mallocNumToFail int64) error {
Adam Langley38311732014-10-16 19:04:35 -07001372 if !test.shouldFail && (len(test.expectedError) > 0 || len(test.expectedLocalError) > 0) {
1373 panic("Error expected without shouldFail in " + test.name)
1374 }
1375
Adam Langleyb0eef0a2015-06-02 10:47:39 -07001376 if test.expectResumeRejected && !test.resumeSession {
1377 panic("expectResumeRejected without resumeSession in " + test.name)
1378 }
1379
David Benjamin87c8a642015-02-21 01:54:29 -05001380 listener, err := net.ListenTCP("tcp4", &net.TCPAddr{IP: net.IP{127, 0, 0, 1}})
1381 if err != nil {
1382 panic(err)
1383 }
1384 defer func() {
1385 if listener != nil {
1386 listener.Close()
1387 }
1388 }()
Adam Langley95c29f32014-06-20 12:00:00 -07001389
David Benjamin884fdf12014-08-02 15:28:23 -04001390 shim_path := path.Join(buildDir, "ssl/test/bssl_shim")
David Benjamin87c8a642015-02-21 01:54:29 -05001391 flags := []string{"-port", strconv.Itoa(listener.Addr().(*net.TCPAddr).Port)}
David Benjamin1d5c83e2014-07-22 19:20:02 -04001392 if test.testType == serverTest {
David Benjamin5a593af2014-08-11 19:51:50 -04001393 flags = append(flags, "-server")
1394
David Benjamin025b3d32014-07-01 19:53:04 -04001395 flags = append(flags, "-key-file")
1396 if test.keyFile == "" {
1397 flags = append(flags, rsaKeyFile)
1398 } else {
1399 flags = append(flags, test.keyFile)
1400 }
1401
1402 flags = append(flags, "-cert-file")
1403 if test.certFile == "" {
1404 flags = append(flags, rsaCertificateFile)
1405 } else {
1406 flags = append(flags, test.certFile)
1407 }
1408 }
David Benjamin5a593af2014-08-11 19:51:50 -04001409
David Benjamin6fd297b2014-08-11 18:43:38 -04001410 if test.protocol == dtls {
1411 flags = append(flags, "-dtls")
1412 }
1413
David Benjamin5a593af2014-08-11 19:51:50 -04001414 if test.resumeSession {
1415 flags = append(flags, "-resume")
1416 }
1417
David Benjamine58c4f52014-08-24 03:47:07 -04001418 if test.shimWritesFirst {
1419 flags = append(flags, "-shim-writes-first")
1420 }
1421
David Benjaminc565ebb2015-04-03 04:06:36 -04001422 if test.exportKeyingMaterial > 0 {
1423 flags = append(flags, "-export-keying-material", strconv.Itoa(test.exportKeyingMaterial))
1424 flags = append(flags, "-export-label", test.exportLabel)
1425 flags = append(flags, "-export-context", test.exportContext)
1426 if test.useExportContext {
1427 flags = append(flags, "-use-export-context")
1428 }
1429 }
Adam Langleyb0eef0a2015-06-02 10:47:39 -07001430 if test.expectResumeRejected {
1431 flags = append(flags, "-expect-session-miss")
1432 }
David Benjaminc565ebb2015-04-03 04:06:36 -04001433
David Benjamin025b3d32014-07-01 19:53:04 -04001434 flags = append(flags, test.flags...)
1435
1436 var shim *exec.Cmd
1437 if *useValgrind {
1438 shim = valgrindOf(false, shim_path, flags...)
Adam Langley75712922014-10-10 16:23:43 -07001439 } else if *useGDB {
1440 shim = gdbOf(shim_path, flags...)
David Benjamin025b3d32014-07-01 19:53:04 -04001441 } else {
1442 shim = exec.Command(shim_path, flags...)
1443 }
David Benjamin025b3d32014-07-01 19:53:04 -04001444 shim.Stdin = os.Stdin
1445 var stdoutBuf, stderrBuf bytes.Buffer
1446 shim.Stdout = &stdoutBuf
1447 shim.Stderr = &stderrBuf
Adam Langley69a01602014-11-17 17:26:55 -08001448 if mallocNumToFail >= 0 {
David Benjamin9e128b02015-02-09 13:13:09 -05001449 shim.Env = os.Environ()
1450 shim.Env = append(shim.Env, "MALLOC_NUMBER_TO_FAIL="+strconv.FormatInt(mallocNumToFail, 10))
Adam Langley69a01602014-11-17 17:26:55 -08001451 if *mallocTestDebug {
1452 shim.Env = append(shim.Env, "MALLOC_ABORT_ON_FAIL=1")
1453 }
1454 shim.Env = append(shim.Env, "_MALLOC_CHECK=1")
1455 }
David Benjamin025b3d32014-07-01 19:53:04 -04001456
1457 if err := shim.Start(); err != nil {
Adam Langley95c29f32014-06-20 12:00:00 -07001458 panic(err)
1459 }
David Benjamin87c8a642015-02-21 01:54:29 -05001460 waitChan := make(chan error, 1)
1461 go func() { waitChan <- shim.Wait() }()
Adam Langley95c29f32014-06-20 12:00:00 -07001462
1463 config := test.config
David Benjamin1d5c83e2014-07-22 19:20:02 -04001464 config.ClientSessionCache = NewLRUClientSessionCache(1)
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001465 config.ServerSessionCache = NewLRUServerSessionCache(1)
David Benjamin025b3d32014-07-01 19:53:04 -04001466 if test.testType == clientTest {
1467 if len(config.Certificates) == 0 {
1468 config.Certificates = []Certificate{getRSACertificate()}
1469 }
David Benjamin87c8a642015-02-21 01:54:29 -05001470 } else {
1471 // Supply a ServerName to ensure a constant session cache key,
1472 // rather than falling back to net.Conn.RemoteAddr.
1473 if len(config.ServerName) == 0 {
1474 config.ServerName = "test"
1475 }
David Benjamin025b3d32014-07-01 19:53:04 -04001476 }
Adam Langley95c29f32014-06-20 12:00:00 -07001477
David Benjamin87c8a642015-02-21 01:54:29 -05001478 conn, err := acceptOrWait(listener, waitChan)
1479 if err == nil {
1480 err = doExchange(test, &config, conn, test.messageLen, false /* not a resumption */)
1481 conn.Close()
1482 }
David Benjamin65ea8ff2014-11-23 03:01:00 -05001483
David Benjamin1d5c83e2014-07-22 19:20:02 -04001484 if err == nil && test.resumeSession {
David Benjamin01fe8202014-09-24 15:21:44 -04001485 var resumeConfig Config
1486 if test.resumeConfig != nil {
1487 resumeConfig = *test.resumeConfig
David Benjamin87c8a642015-02-21 01:54:29 -05001488 if len(resumeConfig.ServerName) == 0 {
1489 resumeConfig.ServerName = config.ServerName
1490 }
David Benjamin01fe8202014-09-24 15:21:44 -04001491 if len(resumeConfig.Certificates) == 0 {
1492 resumeConfig.Certificates = []Certificate{getRSACertificate()}
1493 }
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001494 if !test.newSessionsOnResume {
1495 resumeConfig.SessionTicketKey = config.SessionTicketKey
1496 resumeConfig.ClientSessionCache = config.ClientSessionCache
1497 resumeConfig.ServerSessionCache = config.ServerSessionCache
1498 }
David Benjamin01fe8202014-09-24 15:21:44 -04001499 } else {
1500 resumeConfig = config
1501 }
David Benjamin87c8a642015-02-21 01:54:29 -05001502 var connResume net.Conn
1503 connResume, err = acceptOrWait(listener, waitChan)
1504 if err == nil {
1505 err = doExchange(test, &resumeConfig, connResume, test.messageLen, true /* resumption */)
1506 connResume.Close()
1507 }
David Benjamin1d5c83e2014-07-22 19:20:02 -04001508 }
1509
David Benjamin87c8a642015-02-21 01:54:29 -05001510 // Close the listener now. This is to avoid hangs should the shim try to
1511 // open more connections than expected.
1512 listener.Close()
1513 listener = nil
1514
1515 childErr := <-waitChan
Adam Langley69a01602014-11-17 17:26:55 -08001516 if exitError, ok := childErr.(*exec.ExitError); ok {
1517 if exitError.Sys().(syscall.WaitStatus).ExitStatus() == 88 {
1518 return errMoreMallocs
1519 }
1520 }
Adam Langley95c29f32014-06-20 12:00:00 -07001521
1522 stdout := string(stdoutBuf.Bytes())
1523 stderr := string(stderrBuf.Bytes())
1524 failed := err != nil || childErr != nil
David Benjaminc565ebb2015-04-03 04:06:36 -04001525 correctFailure := len(test.expectedError) == 0 || strings.Contains(stderr, test.expectedError)
Adam Langleyac61fa32014-06-23 12:03:11 -07001526 localError := "none"
1527 if err != nil {
1528 localError = err.Error()
1529 }
1530 if len(test.expectedLocalError) != 0 {
1531 correctFailure = correctFailure && strings.Contains(localError, test.expectedLocalError)
1532 }
Adam Langley95c29f32014-06-20 12:00:00 -07001533
1534 if failed != test.shouldFail || failed && !correctFailure {
Adam Langley95c29f32014-06-20 12:00:00 -07001535 childError := "none"
Adam Langley95c29f32014-06-20 12:00:00 -07001536 if childErr != nil {
1537 childError = childErr.Error()
1538 }
1539
1540 var msg string
1541 switch {
1542 case failed && !test.shouldFail:
1543 msg = "unexpected failure"
1544 case !failed && test.shouldFail:
1545 msg = "unexpected success"
1546 case failed && !correctFailure:
Adam Langleyac61fa32014-06-23 12:03:11 -07001547 msg = "bad error (wanted '" + test.expectedError + "' / '" + test.expectedLocalError + "')"
Adam Langley95c29f32014-06-20 12:00:00 -07001548 default:
1549 panic("internal error")
1550 }
1551
David Benjaminc565ebb2015-04-03 04:06:36 -04001552 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 -07001553 }
1554
David Benjaminc565ebb2015-04-03 04:06:36 -04001555 if !*useValgrind && !failed && len(stderr) > 0 {
Adam Langley95c29f32014-06-20 12:00:00 -07001556 println(stderr)
1557 }
1558
1559 return nil
1560}
1561
1562var tlsVersions = []struct {
1563 name string
1564 version uint16
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001565 flag string
David Benjamin8b8c0062014-11-23 02:47:52 -05001566 hasDTLS bool
Adam Langley95c29f32014-06-20 12:00:00 -07001567}{
David Benjamin8b8c0062014-11-23 02:47:52 -05001568 {"SSL3", VersionSSL30, "-no-ssl3", false},
1569 {"TLS1", VersionTLS10, "-no-tls1", true},
1570 {"TLS11", VersionTLS11, "-no-tls11", false},
1571 {"TLS12", VersionTLS12, "-no-tls12", true},
Adam Langley95c29f32014-06-20 12:00:00 -07001572}
1573
1574var testCipherSuites = []struct {
1575 name string
1576 id uint16
1577}{
1578 {"3DES-SHA", TLS_RSA_WITH_3DES_EDE_CBC_SHA},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001579 {"AES128-GCM", TLS_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001580 {"AES128-SHA", TLS_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001581 {"AES128-SHA256", TLS_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001582 {"AES256-GCM", TLS_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001583 {"AES256-SHA", TLS_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001584 {"AES256-SHA256", TLS_RSA_WITH_AES_256_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001585 {"DHE-RSA-AES128-GCM", TLS_DHE_RSA_WITH_AES_128_GCM_SHA256},
1586 {"DHE-RSA-AES128-SHA", TLS_DHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001587 {"DHE-RSA-AES128-SHA256", TLS_DHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001588 {"DHE-RSA-AES256-GCM", TLS_DHE_RSA_WITH_AES_256_GCM_SHA384},
1589 {"DHE-RSA-AES256-SHA", TLS_DHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001590 {"DHE-RSA-AES256-SHA256", TLS_DHE_RSA_WITH_AES_256_CBC_SHA256},
David Benjamine9a80ff2015-04-07 00:46:46 -04001591 {"DHE-RSA-CHACHA20-POLY1305", TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001592 {"ECDHE-ECDSA-AES128-GCM", TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
1593 {"ECDHE-ECDSA-AES128-SHA", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001594 {"ECDHE-ECDSA-AES128-SHA256", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256},
1595 {"ECDHE-ECDSA-AES256-GCM", TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001596 {"ECDHE-ECDSA-AES256-SHA", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001597 {"ECDHE-ECDSA-AES256-SHA384", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384},
David Benjamine9a80ff2015-04-07 00:46:46 -04001598 {"ECDHE-ECDSA-CHACHA20-POLY1305", TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001599 {"ECDHE-ECDSA-RC4-SHA", TLS_ECDHE_ECDSA_WITH_RC4_128_SHA},
Adam Langley97e8ba82015-04-29 15:32:10 -07001600 {"ECDHE-PSK-AES128-GCM-SHA256", TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001601 {"ECDHE-RSA-AES128-GCM", TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001602 {"ECDHE-RSA-AES128-SHA", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001603 {"ECDHE-RSA-AES128-SHA256", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001604 {"ECDHE-RSA-AES256-GCM", TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001605 {"ECDHE-RSA-AES256-SHA", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001606 {"ECDHE-RSA-AES256-SHA384", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384},
David Benjamine9a80ff2015-04-07 00:46:46 -04001607 {"ECDHE-RSA-CHACHA20-POLY1305", TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001608 {"ECDHE-RSA-RC4-SHA", TLS_ECDHE_RSA_WITH_RC4_128_SHA},
David Benjamin48cae082014-10-27 01:06:24 -04001609 {"PSK-AES128-CBC-SHA", TLS_PSK_WITH_AES_128_CBC_SHA},
1610 {"PSK-AES256-CBC-SHA", TLS_PSK_WITH_AES_256_CBC_SHA},
1611 {"PSK-RC4-SHA", TLS_PSK_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -07001612 {"RC4-MD5", TLS_RSA_WITH_RC4_128_MD5},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001613 {"RC4-SHA", TLS_RSA_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -07001614}
1615
David Benjamin8b8c0062014-11-23 02:47:52 -05001616func hasComponent(suiteName, component string) bool {
1617 return strings.Contains("-"+suiteName+"-", "-"+component+"-")
1618}
1619
David Benjaminf7768e42014-08-31 02:06:47 -04001620func isTLS12Only(suiteName string) bool {
David Benjamin8b8c0062014-11-23 02:47:52 -05001621 return hasComponent(suiteName, "GCM") ||
1622 hasComponent(suiteName, "SHA256") ||
David Benjamine9a80ff2015-04-07 00:46:46 -04001623 hasComponent(suiteName, "SHA384") ||
1624 hasComponent(suiteName, "POLY1305")
David Benjamin8b8c0062014-11-23 02:47:52 -05001625}
1626
1627func isDTLSCipher(suiteName string) bool {
David Benjamine95d20d2014-12-23 11:16:01 -05001628 return !hasComponent(suiteName, "RC4")
David Benjaminf7768e42014-08-31 02:06:47 -04001629}
1630
Adam Langleya7997f12015-05-14 17:38:50 -07001631func bigFromHex(hex string) *big.Int {
1632 ret, ok := new(big.Int).SetString(hex, 16)
1633 if !ok {
1634 panic("failed to parse hex number 0x" + hex)
1635 }
1636 return ret
1637}
1638
Adam Langley95c29f32014-06-20 12:00:00 -07001639func addCipherSuiteTests() {
1640 for _, suite := range testCipherSuites {
David Benjamin48cae082014-10-27 01:06:24 -04001641 const psk = "12345"
1642 const pskIdentity = "luggage combo"
1643
Adam Langley95c29f32014-06-20 12:00:00 -07001644 var cert Certificate
David Benjamin025b3d32014-07-01 19:53:04 -04001645 var certFile string
1646 var keyFile string
David Benjamin8b8c0062014-11-23 02:47:52 -05001647 if hasComponent(suite.name, "ECDSA") {
Adam Langley95c29f32014-06-20 12:00:00 -07001648 cert = getECDSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -04001649 certFile = ecdsaCertificateFile
1650 keyFile = ecdsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -07001651 } else {
1652 cert = getRSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -04001653 certFile = rsaCertificateFile
1654 keyFile = rsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -07001655 }
1656
David Benjamin48cae082014-10-27 01:06:24 -04001657 var flags []string
David Benjamin8b8c0062014-11-23 02:47:52 -05001658 if hasComponent(suite.name, "PSK") {
David Benjamin48cae082014-10-27 01:06:24 -04001659 flags = append(flags,
1660 "-psk", psk,
1661 "-psk-identity", pskIdentity)
1662 }
1663
Adam Langley95c29f32014-06-20 12:00:00 -07001664 for _, ver := range tlsVersions {
David Benjaminf7768e42014-08-31 02:06:47 -04001665 if ver.version < VersionTLS12 && isTLS12Only(suite.name) {
Adam Langley95c29f32014-06-20 12:00:00 -07001666 continue
1667 }
1668
David Benjamin025b3d32014-07-01 19:53:04 -04001669 testCases = append(testCases, testCase{
1670 testType: clientTest,
1671 name: ver.name + "-" + suite.name + "-client",
Adam Langley95c29f32014-06-20 12:00:00 -07001672 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001673 MinVersion: ver.version,
1674 MaxVersion: ver.version,
1675 CipherSuites: []uint16{suite.id},
1676 Certificates: []Certificate{cert},
David Benjamin68793732015-05-04 20:20:48 -04001677 PreSharedKey: []byte(psk),
David Benjamin48cae082014-10-27 01:06:24 -04001678 PreSharedKeyIdentity: pskIdentity,
Adam Langley95c29f32014-06-20 12:00:00 -07001679 },
David Benjamin48cae082014-10-27 01:06:24 -04001680 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001681 resumeSession: true,
Adam Langley95c29f32014-06-20 12:00:00 -07001682 })
David Benjamin025b3d32014-07-01 19:53:04 -04001683
David Benjamin76d8abe2014-08-14 16:25:34 -04001684 testCases = append(testCases, testCase{
1685 testType: serverTest,
1686 name: ver.name + "-" + suite.name + "-server",
1687 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001688 MinVersion: ver.version,
1689 MaxVersion: ver.version,
1690 CipherSuites: []uint16{suite.id},
1691 Certificates: []Certificate{cert},
1692 PreSharedKey: []byte(psk),
1693 PreSharedKeyIdentity: pskIdentity,
David Benjamin76d8abe2014-08-14 16:25:34 -04001694 },
1695 certFile: certFile,
1696 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001697 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001698 resumeSession: true,
David Benjamin76d8abe2014-08-14 16:25:34 -04001699 })
David Benjamin6fd297b2014-08-11 18:43:38 -04001700
David Benjamin8b8c0062014-11-23 02:47:52 -05001701 if ver.hasDTLS && isDTLSCipher(suite.name) {
David Benjamin6fd297b2014-08-11 18:43:38 -04001702 testCases = append(testCases, testCase{
1703 testType: clientTest,
1704 protocol: dtls,
1705 name: "D" + ver.name + "-" + suite.name + "-client",
1706 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001707 MinVersion: ver.version,
1708 MaxVersion: ver.version,
1709 CipherSuites: []uint16{suite.id},
1710 Certificates: []Certificate{cert},
1711 PreSharedKey: []byte(psk),
1712 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001713 },
David Benjamin48cae082014-10-27 01:06:24 -04001714 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001715 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001716 })
1717 testCases = append(testCases, testCase{
1718 testType: serverTest,
1719 protocol: dtls,
1720 name: "D" + ver.name + "-" + suite.name + "-server",
1721 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001722 MinVersion: ver.version,
1723 MaxVersion: ver.version,
1724 CipherSuites: []uint16{suite.id},
1725 Certificates: []Certificate{cert},
1726 PreSharedKey: []byte(psk),
1727 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001728 },
1729 certFile: certFile,
1730 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001731 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001732 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001733 })
1734 }
Adam Langley95c29f32014-06-20 12:00:00 -07001735 }
1736 }
Adam Langleya7997f12015-05-14 17:38:50 -07001737
1738 testCases = append(testCases, testCase{
1739 name: "WeakDH",
1740 config: Config{
1741 CipherSuites: []uint16{TLS_DHE_RSA_WITH_AES_128_GCM_SHA256},
1742 Bugs: ProtocolBugs{
1743 // This is a 1023-bit prime number, generated
1744 // with:
1745 // openssl gendh 1023 | openssl asn1parse -i
1746 DHGroupPrime: bigFromHex("518E9B7930CE61C6E445C8360584E5FC78D9137C0FFDC880B495D5338ADF7689951A6821C17A76B3ACB8E0156AEA607B7EC406EBEDBB84D8376EB8FE8F8BA1433488BEE0C3EDDFD3A32DBB9481980A7AF6C96BFCF490A094CFFB2B8192C1BB5510B77B658436E27C2D4D023FE3718222AB0CA1273995B51F6D625A4944D0DD4B"),
1747 },
1748 },
1749 shouldFail: true,
1750 expectedError: "BAD_DH_P_LENGTH",
1751 })
Adam Langley95c29f32014-06-20 12:00:00 -07001752}
1753
1754func addBadECDSASignatureTests() {
1755 for badR := BadValue(1); badR < NumBadValues; badR++ {
1756 for badS := BadValue(1); badS < NumBadValues; badS++ {
David Benjamin025b3d32014-07-01 19:53:04 -04001757 testCases = append(testCases, testCase{
Adam Langley95c29f32014-06-20 12:00:00 -07001758 name: fmt.Sprintf("BadECDSA-%d-%d", badR, badS),
1759 config: Config{
1760 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
1761 Certificates: []Certificate{getECDSACertificate()},
1762 Bugs: ProtocolBugs{
1763 BadECDSAR: badR,
1764 BadECDSAS: badS,
1765 },
1766 },
1767 shouldFail: true,
1768 expectedError: "SIGNATURE",
1769 })
1770 }
1771 }
1772}
1773
Adam Langley80842bd2014-06-20 12:00:00 -07001774func addCBCPaddingTests() {
David Benjamin025b3d32014-07-01 19:53:04 -04001775 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001776 name: "MaxCBCPadding",
1777 config: Config{
1778 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1779 Bugs: ProtocolBugs{
1780 MaxPadding: true,
1781 },
1782 },
1783 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1784 })
David Benjamin025b3d32014-07-01 19:53:04 -04001785 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001786 name: "BadCBCPadding",
1787 config: Config{
1788 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1789 Bugs: ProtocolBugs{
1790 PaddingFirstByteBad: true,
1791 },
1792 },
1793 shouldFail: true,
1794 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1795 })
1796 // OpenSSL previously had an issue where the first byte of padding in
1797 // 255 bytes of padding wasn't checked.
David Benjamin025b3d32014-07-01 19:53:04 -04001798 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001799 name: "BadCBCPadding255",
1800 config: Config{
1801 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1802 Bugs: ProtocolBugs{
1803 MaxPadding: true,
1804 PaddingFirstByteBadIf255: true,
1805 },
1806 },
1807 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1808 shouldFail: true,
1809 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1810 })
1811}
1812
Kenny Root7fdeaf12014-08-05 15:23:37 -07001813func addCBCSplittingTests() {
1814 testCases = append(testCases, testCase{
1815 name: "CBCRecordSplitting",
1816 config: Config{
1817 MaxVersion: VersionTLS10,
1818 MinVersion: VersionTLS10,
1819 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1820 },
1821 messageLen: -1, // read until EOF
1822 flags: []string{
1823 "-async",
1824 "-write-different-record-sizes",
1825 "-cbc-record-splitting",
1826 },
David Benjamina8e3e0e2014-08-06 22:11:10 -04001827 })
1828 testCases = append(testCases, testCase{
Kenny Root7fdeaf12014-08-05 15:23:37 -07001829 name: "CBCRecordSplittingPartialWrite",
1830 config: Config{
1831 MaxVersion: VersionTLS10,
1832 MinVersion: VersionTLS10,
1833 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1834 },
1835 messageLen: -1, // read until EOF
1836 flags: []string{
1837 "-async",
1838 "-write-different-record-sizes",
1839 "-cbc-record-splitting",
1840 "-partial-write",
1841 },
1842 })
1843}
1844
David Benjamin636293b2014-07-08 17:59:18 -04001845func addClientAuthTests() {
David Benjamin407a10c2014-07-16 12:58:59 -04001846 // Add a dummy cert pool to stress certificate authority parsing.
1847 // TODO(davidben): Add tests that those values parse out correctly.
1848 certPool := x509.NewCertPool()
1849 cert, err := x509.ParseCertificate(rsaCertificate.Certificate[0])
1850 if err != nil {
1851 panic(err)
1852 }
1853 certPool.AddCert(cert)
1854
David Benjamin636293b2014-07-08 17:59:18 -04001855 for _, ver := range tlsVersions {
David Benjamin636293b2014-07-08 17:59:18 -04001856 testCases = append(testCases, testCase{
1857 testType: clientTest,
David Benjamin67666e72014-07-12 15:47:52 -04001858 name: ver.name + "-Client-ClientAuth-RSA",
David Benjamin636293b2014-07-08 17:59:18 -04001859 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001860 MinVersion: ver.version,
1861 MaxVersion: ver.version,
1862 ClientAuth: RequireAnyClientCert,
1863 ClientCAs: certPool,
David Benjamin636293b2014-07-08 17:59:18 -04001864 },
1865 flags: []string{
1866 "-cert-file", rsaCertificateFile,
1867 "-key-file", rsaKeyFile,
1868 },
1869 })
1870 testCases = append(testCases, testCase{
David Benjamin67666e72014-07-12 15:47:52 -04001871 testType: serverTest,
1872 name: ver.name + "-Server-ClientAuth-RSA",
1873 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001874 MinVersion: ver.version,
1875 MaxVersion: ver.version,
David Benjamin67666e72014-07-12 15:47:52 -04001876 Certificates: []Certificate{rsaCertificate},
1877 },
1878 flags: []string{"-require-any-client-certificate"},
1879 })
David Benjamine098ec22014-08-27 23:13:20 -04001880 if ver.version != VersionSSL30 {
1881 testCases = append(testCases, testCase{
1882 testType: serverTest,
1883 name: ver.name + "-Server-ClientAuth-ECDSA",
1884 config: Config{
1885 MinVersion: ver.version,
1886 MaxVersion: ver.version,
1887 Certificates: []Certificate{ecdsaCertificate},
1888 },
1889 flags: []string{"-require-any-client-certificate"},
1890 })
1891 testCases = append(testCases, testCase{
1892 testType: clientTest,
1893 name: ver.name + "-Client-ClientAuth-ECDSA",
1894 config: Config{
1895 MinVersion: ver.version,
1896 MaxVersion: ver.version,
1897 ClientAuth: RequireAnyClientCert,
1898 ClientCAs: certPool,
1899 },
1900 flags: []string{
1901 "-cert-file", ecdsaCertificateFile,
1902 "-key-file", ecdsaKeyFile,
1903 },
1904 })
1905 }
David Benjamin636293b2014-07-08 17:59:18 -04001906 }
1907}
1908
Adam Langley75712922014-10-10 16:23:43 -07001909func addExtendedMasterSecretTests() {
1910 const expectEMSFlag = "-expect-extended-master-secret"
1911
1912 for _, with := range []bool{false, true} {
1913 prefix := "No"
1914 var flags []string
1915 if with {
1916 prefix = ""
1917 flags = []string{expectEMSFlag}
1918 }
1919
1920 for _, isClient := range []bool{false, true} {
1921 suffix := "-Server"
1922 testType := serverTest
1923 if isClient {
1924 suffix = "-Client"
1925 testType = clientTest
1926 }
1927
1928 for _, ver := range tlsVersions {
1929 test := testCase{
1930 testType: testType,
1931 name: prefix + "ExtendedMasterSecret-" + ver.name + suffix,
1932 config: Config{
1933 MinVersion: ver.version,
1934 MaxVersion: ver.version,
1935 Bugs: ProtocolBugs{
1936 NoExtendedMasterSecret: !with,
1937 RequireExtendedMasterSecret: with,
1938 },
1939 },
David Benjamin48cae082014-10-27 01:06:24 -04001940 flags: flags,
1941 shouldFail: ver.version == VersionSSL30 && with,
Adam Langley75712922014-10-10 16:23:43 -07001942 }
1943 if test.shouldFail {
1944 test.expectedLocalError = "extended master secret required but not supported by peer"
1945 }
1946 testCases = append(testCases, test)
1947 }
1948 }
1949 }
1950
Adam Langleyba5934b2015-06-02 10:50:35 -07001951 for _, isClient := range []bool{false, true} {
1952 for _, supportedInFirstConnection := range []bool{false, true} {
1953 for _, supportedInResumeConnection := range []bool{false, true} {
1954 boolToWord := func(b bool) string {
1955 if b {
1956 return "Yes"
1957 }
1958 return "No"
1959 }
1960 suffix := boolToWord(supportedInFirstConnection) + "To" + boolToWord(supportedInResumeConnection) + "-"
1961 if isClient {
1962 suffix += "Client"
1963 } else {
1964 suffix += "Server"
1965 }
1966
1967 supportedConfig := Config{
1968 Bugs: ProtocolBugs{
1969 RequireExtendedMasterSecret: true,
1970 },
1971 }
1972
1973 noSupportConfig := Config{
1974 Bugs: ProtocolBugs{
1975 NoExtendedMasterSecret: true,
1976 },
1977 }
1978
1979 test := testCase{
1980 name: "ExtendedMasterSecret-" + suffix,
1981 resumeSession: true,
1982 }
1983
1984 if !isClient {
1985 test.testType = serverTest
1986 }
1987
1988 if supportedInFirstConnection {
1989 test.config = supportedConfig
1990 } else {
1991 test.config = noSupportConfig
1992 }
1993
1994 if supportedInResumeConnection {
1995 test.resumeConfig = &supportedConfig
1996 } else {
1997 test.resumeConfig = &noSupportConfig
1998 }
1999
2000 switch suffix {
2001 case "YesToYes-Client", "YesToYes-Server":
2002 // When a session is resumed, it should
2003 // still be aware that its master
2004 // secret was generated via EMS and
2005 // thus it's safe to use tls-unique.
2006 test.flags = []string{expectEMSFlag}
2007 case "NoToYes-Server":
2008 // If an original connection did not
2009 // contain EMS, but a resumption
2010 // handshake does, then a server should
2011 // not resume the session.
2012 test.expectResumeRejected = true
2013 case "YesToNo-Server":
2014 // Resuming an EMS session without the
2015 // EMS extension should cause the
2016 // server to abort the connection.
2017 test.shouldFail = true
2018 test.expectedError = ":RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION:"
2019 case "NoToYes-Client":
2020 // A client should abort a connection
2021 // where the server resumed a non-EMS
2022 // session but echoed the EMS
2023 // extension.
2024 test.shouldFail = true
2025 test.expectedError = ":RESUMED_NON_EMS_SESSION_WITH_EMS_EXTENSION:"
2026 case "YesToNo-Client":
2027 // A client should abort a connection
2028 // where the server didn't echo EMS
2029 // when the session used it.
2030 test.shouldFail = true
2031 test.expectedError = ":RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION:"
2032 }
2033
2034 testCases = append(testCases, test)
2035 }
2036 }
2037 }
Adam Langley75712922014-10-10 16:23:43 -07002038}
2039
David Benjamin43ec06f2014-08-05 02:28:57 -04002040// Adds tests that try to cover the range of the handshake state machine, under
2041// various conditions. Some of these are redundant with other tests, but they
2042// only cover the synchronous case.
David Benjamin6fd297b2014-08-11 18:43:38 -04002043func addStateMachineCoverageTests(async, splitHandshake bool, protocol protocol) {
David Benjamin760b1dd2015-05-15 23:33:48 -04002044 var tests []testCase
2045
2046 // Basic handshake, with resumption. Client and server,
2047 // session ID and session ticket.
2048 tests = append(tests, testCase{
2049 name: "Basic-Client",
2050 resumeSession: true,
2051 })
2052 tests = append(tests, testCase{
2053 name: "Basic-Client-RenewTicket",
2054 config: Config{
2055 Bugs: ProtocolBugs{
2056 RenewTicketOnResume: true,
2057 },
2058 },
2059 resumeSession: true,
2060 })
2061 tests = append(tests, testCase{
2062 name: "Basic-Client-NoTicket",
2063 config: Config{
2064 SessionTicketsDisabled: true,
2065 },
2066 resumeSession: true,
2067 })
2068 tests = append(tests, testCase{
2069 name: "Basic-Client-Implicit",
2070 flags: []string{"-implicit-handshake"},
2071 resumeSession: true,
2072 })
2073 tests = append(tests, testCase{
2074 testType: serverTest,
2075 name: "Basic-Server",
2076 resumeSession: true,
2077 })
2078 tests = append(tests, testCase{
2079 testType: serverTest,
2080 name: "Basic-Server-NoTickets",
2081 config: Config{
2082 SessionTicketsDisabled: true,
2083 },
2084 resumeSession: true,
2085 })
2086 tests = append(tests, testCase{
2087 testType: serverTest,
2088 name: "Basic-Server-Implicit",
2089 flags: []string{"-implicit-handshake"},
2090 resumeSession: true,
2091 })
2092 tests = append(tests, testCase{
2093 testType: serverTest,
2094 name: "Basic-Server-EarlyCallback",
2095 flags: []string{"-use-early-callback"},
2096 resumeSession: true,
2097 })
2098
2099 // TLS client auth.
2100 tests = append(tests, testCase{
2101 testType: clientTest,
2102 name: "ClientAuth-Client",
2103 config: Config{
2104 ClientAuth: RequireAnyClientCert,
2105 },
2106 flags: []string{
2107 "-cert-file", rsaCertificateFile,
2108 "-key-file", rsaKeyFile,
2109 },
2110 })
2111 tests = append(tests, testCase{
2112 testType: serverTest,
2113 name: "ClientAuth-Server",
2114 config: Config{
2115 Certificates: []Certificate{rsaCertificate},
2116 },
2117 flags: []string{"-require-any-client-certificate"},
2118 })
2119
2120 // No session ticket support; server doesn't send NewSessionTicket.
2121 tests = append(tests, testCase{
2122 name: "SessionTicketsDisabled-Client",
2123 config: Config{
2124 SessionTicketsDisabled: true,
2125 },
2126 })
2127 tests = append(tests, testCase{
2128 testType: serverTest,
2129 name: "SessionTicketsDisabled-Server",
2130 config: Config{
2131 SessionTicketsDisabled: true,
2132 },
2133 })
2134
2135 // Skip ServerKeyExchange in PSK key exchange if there's no
2136 // identity hint.
2137 tests = append(tests, testCase{
2138 name: "EmptyPSKHint-Client",
2139 config: Config{
2140 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
2141 PreSharedKey: []byte("secret"),
2142 },
2143 flags: []string{"-psk", "secret"},
2144 })
2145 tests = append(tests, testCase{
2146 testType: serverTest,
2147 name: "EmptyPSKHint-Server",
2148 config: Config{
2149 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
2150 PreSharedKey: []byte("secret"),
2151 },
2152 flags: []string{"-psk", "secret"},
2153 })
2154
2155 if protocol == tls {
2156 tests = append(tests, testCase{
2157 name: "Renegotiate-Client",
2158 renegotiate: true,
2159 })
2160 // NPN on client and server; results in post-handshake message.
2161 tests = append(tests, testCase{
2162 name: "NPN-Client",
2163 config: Config{
2164 NextProtos: []string{"foo"},
2165 },
2166 flags: []string{"-select-next-proto", "foo"},
2167 expectedNextProto: "foo",
2168 expectedNextProtoType: npn,
2169 })
2170 tests = append(tests, testCase{
2171 testType: serverTest,
2172 name: "NPN-Server",
2173 config: Config{
2174 NextProtos: []string{"bar"},
2175 },
2176 flags: []string{
2177 "-advertise-npn", "\x03foo\x03bar\x03baz",
2178 "-expect-next-proto", "bar",
2179 },
2180 expectedNextProto: "bar",
2181 expectedNextProtoType: npn,
2182 })
2183
2184 // TODO(davidben): Add tests for when False Start doesn't trigger.
2185
2186 // Client does False Start and negotiates NPN.
2187 tests = append(tests, testCase{
2188 name: "FalseStart",
2189 config: Config{
2190 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2191 NextProtos: []string{"foo"},
2192 Bugs: ProtocolBugs{
2193 ExpectFalseStart: true,
2194 },
2195 },
2196 flags: []string{
2197 "-false-start",
2198 "-select-next-proto", "foo",
2199 },
2200 shimWritesFirst: true,
2201 resumeSession: true,
2202 })
2203
2204 // Client does False Start and negotiates ALPN.
2205 tests = append(tests, testCase{
2206 name: "FalseStart-ALPN",
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 "-advertise-alpn", "\x03foo",
2217 },
2218 shimWritesFirst: true,
2219 resumeSession: true,
2220 })
2221
2222 // Client does False Start but doesn't explicitly call
2223 // SSL_connect.
2224 tests = append(tests, testCase{
2225 name: "FalseStart-Implicit",
2226 config: Config{
2227 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2228 NextProtos: []string{"foo"},
2229 },
2230 flags: []string{
2231 "-implicit-handshake",
2232 "-false-start",
2233 "-advertise-alpn", "\x03foo",
2234 },
2235 })
2236
2237 // False Start without session tickets.
2238 tests = append(tests, testCase{
2239 name: "FalseStart-SessionTicketsDisabled",
2240 config: Config{
2241 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2242 NextProtos: []string{"foo"},
2243 SessionTicketsDisabled: true,
2244 Bugs: ProtocolBugs{
2245 ExpectFalseStart: true,
2246 },
2247 },
2248 flags: []string{
2249 "-false-start",
2250 "-select-next-proto", "foo",
2251 },
2252 shimWritesFirst: true,
2253 })
2254
2255 // Server parses a V2ClientHello.
2256 tests = append(tests, testCase{
2257 testType: serverTest,
2258 name: "SendV2ClientHello",
2259 config: Config{
2260 // Choose a cipher suite that does not involve
2261 // elliptic curves, so no extensions are
2262 // involved.
2263 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2264 Bugs: ProtocolBugs{
2265 SendV2ClientHello: true,
2266 },
2267 },
2268 })
2269
2270 // Client sends a Channel ID.
2271 tests = append(tests, testCase{
2272 name: "ChannelID-Client",
2273 config: Config{
2274 RequestChannelID: true,
2275 },
2276 flags: []string{"-send-channel-id", channelIDKeyFile},
2277 resumeSession: true,
2278 expectChannelID: true,
2279 })
2280
2281 // Server accepts a Channel ID.
2282 tests = append(tests, testCase{
2283 testType: serverTest,
2284 name: "ChannelID-Server",
2285 config: Config{
2286 ChannelID: channelIDKey,
2287 },
2288 flags: []string{
2289 "-expect-channel-id",
2290 base64.StdEncoding.EncodeToString(channelIDBytes),
2291 },
2292 resumeSession: true,
2293 expectChannelID: true,
2294 })
2295 } else {
2296 tests = append(tests, testCase{
2297 name: "SkipHelloVerifyRequest",
2298 config: Config{
2299 Bugs: ProtocolBugs{
2300 SkipHelloVerifyRequest: true,
2301 },
2302 },
2303 })
2304 }
2305
David Benjamin43ec06f2014-08-05 02:28:57 -04002306 var suffix string
2307 var flags []string
2308 var maxHandshakeRecordLength int
David Benjamin6fd297b2014-08-11 18:43:38 -04002309 if protocol == dtls {
2310 suffix = "-DTLS"
2311 }
David Benjamin43ec06f2014-08-05 02:28:57 -04002312 if async {
David Benjamin6fd297b2014-08-11 18:43:38 -04002313 suffix += "-Async"
David Benjamin43ec06f2014-08-05 02:28:57 -04002314 flags = append(flags, "-async")
2315 } else {
David Benjamin6fd297b2014-08-11 18:43:38 -04002316 suffix += "-Sync"
David Benjamin43ec06f2014-08-05 02:28:57 -04002317 }
2318 if splitHandshake {
2319 suffix += "-SplitHandshakeRecords"
David Benjamin98214542014-08-07 18:02:39 -04002320 maxHandshakeRecordLength = 1
David Benjamin43ec06f2014-08-05 02:28:57 -04002321 }
David Benjamin760b1dd2015-05-15 23:33:48 -04002322 for _, test := range tests {
2323 test.protocol = protocol
2324 test.name += suffix
2325 test.config.Bugs.MaxHandshakeRecordLength = maxHandshakeRecordLength
2326 test.flags = append(test.flags, flags...)
2327 testCases = append(testCases, test)
David Benjamin6fd297b2014-08-11 18:43:38 -04002328 }
David Benjamin43ec06f2014-08-05 02:28:57 -04002329}
2330
Adam Langley524e7172015-02-20 16:04:00 -08002331func addDDoSCallbackTests() {
2332 // DDoS callback.
2333
2334 for _, resume := range []bool{false, true} {
2335 suffix := "Resume"
2336 if resume {
2337 suffix = "No" + suffix
2338 }
2339
2340 testCases = append(testCases, testCase{
2341 testType: serverTest,
2342 name: "Server-DDoS-OK-" + suffix,
2343 flags: []string{"-install-ddos-callback"},
2344 resumeSession: resume,
2345 })
2346
2347 failFlag := "-fail-ddos-callback"
2348 if resume {
2349 failFlag = "-fail-second-ddos-callback"
2350 }
2351 testCases = append(testCases, testCase{
2352 testType: serverTest,
2353 name: "Server-DDoS-Reject-" + suffix,
2354 flags: []string{"-install-ddos-callback", failFlag},
2355 resumeSession: resume,
2356 shouldFail: true,
2357 expectedError: ":CONNECTION_REJECTED:",
2358 })
2359 }
2360}
2361
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002362func addVersionNegotiationTests() {
2363 for i, shimVers := range tlsVersions {
2364 // Assemble flags to disable all newer versions on the shim.
2365 var flags []string
2366 for _, vers := range tlsVersions[i+1:] {
2367 flags = append(flags, vers.flag)
2368 }
2369
2370 for _, runnerVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05002371 protocols := []protocol{tls}
2372 if runnerVers.hasDTLS && shimVers.hasDTLS {
2373 protocols = append(protocols, dtls)
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002374 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002375 for _, protocol := range protocols {
2376 expectedVersion := shimVers.version
2377 if runnerVers.version < shimVers.version {
2378 expectedVersion = runnerVers.version
2379 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002380
David Benjamin8b8c0062014-11-23 02:47:52 -05002381 suffix := shimVers.name + "-" + runnerVers.name
2382 if protocol == dtls {
2383 suffix += "-DTLS"
2384 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002385
David Benjamin1eb367c2014-12-12 18:17:51 -05002386 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
2387
David Benjamin1e29a6b2014-12-10 02:27:24 -05002388 clientVers := shimVers.version
2389 if clientVers > VersionTLS10 {
2390 clientVers = VersionTLS10
2391 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002392 testCases = append(testCases, testCase{
2393 protocol: protocol,
2394 testType: clientTest,
2395 name: "VersionNegotiation-Client-" + suffix,
2396 config: Config{
2397 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05002398 Bugs: ProtocolBugs{
2399 ExpectInitialRecordVersion: clientVers,
2400 },
David Benjamin8b8c0062014-11-23 02:47:52 -05002401 },
2402 flags: flags,
2403 expectedVersion: expectedVersion,
2404 })
David Benjamin1eb367c2014-12-12 18:17:51 -05002405 testCases = append(testCases, testCase{
2406 protocol: protocol,
2407 testType: clientTest,
2408 name: "VersionNegotiation-Client2-" + suffix,
2409 config: Config{
2410 MaxVersion: runnerVers.version,
2411 Bugs: ProtocolBugs{
2412 ExpectInitialRecordVersion: clientVers,
2413 },
2414 },
2415 flags: []string{"-max-version", shimVersFlag},
2416 expectedVersion: expectedVersion,
2417 })
David Benjamin8b8c0062014-11-23 02:47:52 -05002418
2419 testCases = append(testCases, testCase{
2420 protocol: protocol,
2421 testType: serverTest,
2422 name: "VersionNegotiation-Server-" + suffix,
2423 config: Config{
2424 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05002425 Bugs: ProtocolBugs{
2426 ExpectInitialRecordVersion: expectedVersion,
2427 },
David Benjamin8b8c0062014-11-23 02:47:52 -05002428 },
2429 flags: flags,
2430 expectedVersion: expectedVersion,
2431 })
David Benjamin1eb367c2014-12-12 18:17:51 -05002432 testCases = append(testCases, testCase{
2433 protocol: protocol,
2434 testType: serverTest,
2435 name: "VersionNegotiation-Server2-" + suffix,
2436 config: Config{
2437 MaxVersion: runnerVers.version,
2438 Bugs: ProtocolBugs{
2439 ExpectInitialRecordVersion: expectedVersion,
2440 },
2441 },
2442 flags: []string{"-max-version", shimVersFlag},
2443 expectedVersion: expectedVersion,
2444 })
David Benjamin8b8c0062014-11-23 02:47:52 -05002445 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002446 }
2447 }
2448}
2449
David Benjaminaccb4542014-12-12 23:44:33 -05002450func addMinimumVersionTests() {
2451 for i, shimVers := range tlsVersions {
2452 // Assemble flags to disable all older versions on the shim.
2453 var flags []string
2454 for _, vers := range tlsVersions[:i] {
2455 flags = append(flags, vers.flag)
2456 }
2457
2458 for _, runnerVers := range tlsVersions {
2459 protocols := []protocol{tls}
2460 if runnerVers.hasDTLS && shimVers.hasDTLS {
2461 protocols = append(protocols, dtls)
2462 }
2463 for _, protocol := range protocols {
2464 suffix := shimVers.name + "-" + runnerVers.name
2465 if protocol == dtls {
2466 suffix += "-DTLS"
2467 }
2468 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
2469
David Benjaminaccb4542014-12-12 23:44:33 -05002470 var expectedVersion uint16
2471 var shouldFail bool
2472 var expectedError string
David Benjamin87909c02014-12-13 01:55:01 -05002473 var expectedLocalError string
David Benjaminaccb4542014-12-12 23:44:33 -05002474 if runnerVers.version >= shimVers.version {
2475 expectedVersion = runnerVers.version
2476 } else {
2477 shouldFail = true
2478 expectedError = ":UNSUPPORTED_PROTOCOL:"
David Benjamin87909c02014-12-13 01:55:01 -05002479 if runnerVers.version > VersionSSL30 {
2480 expectedLocalError = "remote error: protocol version not supported"
2481 } else {
2482 expectedLocalError = "remote error: handshake failure"
2483 }
David Benjaminaccb4542014-12-12 23:44:33 -05002484 }
2485
2486 testCases = append(testCases, testCase{
2487 protocol: protocol,
2488 testType: clientTest,
2489 name: "MinimumVersion-Client-" + suffix,
2490 config: Config{
2491 MaxVersion: runnerVers.version,
2492 },
David Benjamin87909c02014-12-13 01:55:01 -05002493 flags: flags,
2494 expectedVersion: expectedVersion,
2495 shouldFail: shouldFail,
2496 expectedError: expectedError,
2497 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002498 })
2499 testCases = append(testCases, testCase{
2500 protocol: protocol,
2501 testType: clientTest,
2502 name: "MinimumVersion-Client2-" + suffix,
2503 config: Config{
2504 MaxVersion: runnerVers.version,
2505 },
David Benjamin87909c02014-12-13 01:55:01 -05002506 flags: []string{"-min-version", shimVersFlag},
2507 expectedVersion: expectedVersion,
2508 shouldFail: shouldFail,
2509 expectedError: expectedError,
2510 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002511 })
2512
2513 testCases = append(testCases, testCase{
2514 protocol: protocol,
2515 testType: serverTest,
2516 name: "MinimumVersion-Server-" + suffix,
2517 config: Config{
2518 MaxVersion: runnerVers.version,
2519 },
David Benjamin87909c02014-12-13 01:55:01 -05002520 flags: flags,
2521 expectedVersion: expectedVersion,
2522 shouldFail: shouldFail,
2523 expectedError: expectedError,
2524 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002525 })
2526 testCases = append(testCases, testCase{
2527 protocol: protocol,
2528 testType: serverTest,
2529 name: "MinimumVersion-Server2-" + suffix,
2530 config: Config{
2531 MaxVersion: runnerVers.version,
2532 },
David Benjamin87909c02014-12-13 01:55:01 -05002533 flags: []string{"-min-version", shimVersFlag},
2534 expectedVersion: expectedVersion,
2535 shouldFail: shouldFail,
2536 expectedError: expectedError,
2537 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002538 })
2539 }
2540 }
2541 }
2542}
2543
David Benjamin5c24a1d2014-08-31 00:59:27 -04002544func addD5BugTests() {
2545 testCases = append(testCases, testCase{
2546 testType: serverTest,
2547 name: "D5Bug-NoQuirk-Reject",
2548 config: Config{
2549 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2550 Bugs: ProtocolBugs{
2551 SSL3RSAKeyExchange: true,
2552 },
2553 },
2554 shouldFail: true,
2555 expectedError: ":TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG:",
2556 })
2557 testCases = append(testCases, testCase{
2558 testType: serverTest,
2559 name: "D5Bug-Quirk-Normal",
2560 config: Config{
2561 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2562 },
2563 flags: []string{"-tls-d5-bug"},
2564 })
2565 testCases = append(testCases, testCase{
2566 testType: serverTest,
2567 name: "D5Bug-Quirk-Bug",
2568 config: Config{
2569 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2570 Bugs: ProtocolBugs{
2571 SSL3RSAKeyExchange: true,
2572 },
2573 },
2574 flags: []string{"-tls-d5-bug"},
2575 })
2576}
2577
David Benjamine78bfde2014-09-06 12:45:15 -04002578func addExtensionTests() {
2579 testCases = append(testCases, testCase{
2580 testType: clientTest,
2581 name: "DuplicateExtensionClient",
2582 config: Config{
2583 Bugs: ProtocolBugs{
2584 DuplicateExtension: true,
2585 },
2586 },
2587 shouldFail: true,
2588 expectedLocalError: "remote error: error decoding message",
2589 })
2590 testCases = append(testCases, testCase{
2591 testType: serverTest,
2592 name: "DuplicateExtensionServer",
2593 config: Config{
2594 Bugs: ProtocolBugs{
2595 DuplicateExtension: true,
2596 },
2597 },
2598 shouldFail: true,
2599 expectedLocalError: "remote error: error decoding message",
2600 })
2601 testCases = append(testCases, testCase{
2602 testType: clientTest,
2603 name: "ServerNameExtensionClient",
2604 config: Config{
2605 Bugs: ProtocolBugs{
2606 ExpectServerName: "example.com",
2607 },
2608 },
2609 flags: []string{"-host-name", "example.com"},
2610 })
2611 testCases = append(testCases, testCase{
2612 testType: clientTest,
David Benjamin5f237bc2015-02-11 17:14:15 -05002613 name: "ServerNameExtensionClientMismatch",
David Benjamine78bfde2014-09-06 12:45:15 -04002614 config: Config{
2615 Bugs: ProtocolBugs{
2616 ExpectServerName: "mismatch.com",
2617 },
2618 },
2619 flags: []string{"-host-name", "example.com"},
2620 shouldFail: true,
2621 expectedLocalError: "tls: unexpected server name",
2622 })
2623 testCases = append(testCases, testCase{
2624 testType: clientTest,
David Benjamin5f237bc2015-02-11 17:14:15 -05002625 name: "ServerNameExtensionClientMissing",
David Benjamine78bfde2014-09-06 12:45:15 -04002626 config: Config{
2627 Bugs: ProtocolBugs{
2628 ExpectServerName: "missing.com",
2629 },
2630 },
2631 shouldFail: true,
2632 expectedLocalError: "tls: unexpected server name",
2633 })
2634 testCases = append(testCases, testCase{
2635 testType: serverTest,
2636 name: "ServerNameExtensionServer",
2637 config: Config{
2638 ServerName: "example.com",
2639 },
2640 flags: []string{"-expect-server-name", "example.com"},
2641 resumeSession: true,
2642 })
David Benjaminae2888f2014-09-06 12:58:58 -04002643 testCases = append(testCases, testCase{
2644 testType: clientTest,
2645 name: "ALPNClient",
2646 config: Config{
2647 NextProtos: []string{"foo"},
2648 },
2649 flags: []string{
2650 "-advertise-alpn", "\x03foo\x03bar\x03baz",
2651 "-expect-alpn", "foo",
2652 },
David Benjaminfc7b0862014-09-06 13:21:53 -04002653 expectedNextProto: "foo",
2654 expectedNextProtoType: alpn,
2655 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04002656 })
2657 testCases = append(testCases, testCase{
2658 testType: serverTest,
2659 name: "ALPNServer",
2660 config: Config{
2661 NextProtos: []string{"foo", "bar", "baz"},
2662 },
2663 flags: []string{
2664 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2665 "-select-alpn", "foo",
2666 },
David Benjaminfc7b0862014-09-06 13:21:53 -04002667 expectedNextProto: "foo",
2668 expectedNextProtoType: alpn,
2669 resumeSession: true,
2670 })
2671 // Test that the server prefers ALPN over NPN.
2672 testCases = append(testCases, testCase{
2673 testType: serverTest,
2674 name: "ALPNServer-Preferred",
2675 config: Config{
2676 NextProtos: []string{"foo", "bar", "baz"},
2677 },
2678 flags: []string{
2679 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2680 "-select-alpn", "foo",
2681 "-advertise-npn", "\x03foo\x03bar\x03baz",
2682 },
2683 expectedNextProto: "foo",
2684 expectedNextProtoType: alpn,
2685 resumeSession: true,
2686 })
2687 testCases = append(testCases, testCase{
2688 testType: serverTest,
2689 name: "ALPNServer-Preferred-Swapped",
2690 config: Config{
2691 NextProtos: []string{"foo", "bar", "baz"},
2692 Bugs: ProtocolBugs{
2693 SwapNPNAndALPN: true,
2694 },
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,
David Benjaminae2888f2014-09-06 12:58:58 -04002704 })
Adam Langley38311732014-10-16 19:04:35 -07002705 // Resume with a corrupt ticket.
2706 testCases = append(testCases, testCase{
2707 testType: serverTest,
2708 name: "CorruptTicket",
2709 config: Config{
2710 Bugs: ProtocolBugs{
2711 CorruptTicket: true,
2712 },
2713 },
Adam Langleyb0eef0a2015-06-02 10:47:39 -07002714 resumeSession: true,
2715 expectResumeRejected: true,
Adam Langley38311732014-10-16 19:04:35 -07002716 })
2717 // Resume with an oversized session id.
2718 testCases = append(testCases, testCase{
2719 testType: serverTest,
2720 name: "OversizedSessionId",
2721 config: Config{
2722 Bugs: ProtocolBugs{
2723 OversizedSessionId: true,
2724 },
2725 },
2726 resumeSession: true,
Adam Langley75712922014-10-10 16:23:43 -07002727 shouldFail: true,
Adam Langley38311732014-10-16 19:04:35 -07002728 expectedError: ":DECODE_ERROR:",
2729 })
David Benjaminca6c8262014-11-15 19:06:08 -05002730 // Basic DTLS-SRTP tests. Include fake profiles to ensure they
2731 // are ignored.
2732 testCases = append(testCases, testCase{
2733 protocol: dtls,
2734 name: "SRTP-Client",
2735 config: Config{
2736 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
2737 },
2738 flags: []string{
2739 "-srtp-profiles",
2740 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2741 },
2742 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2743 })
2744 testCases = append(testCases, testCase{
2745 protocol: dtls,
2746 testType: serverTest,
2747 name: "SRTP-Server",
2748 config: Config{
2749 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
2750 },
2751 flags: []string{
2752 "-srtp-profiles",
2753 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2754 },
2755 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2756 })
2757 // Test that the MKI is ignored.
2758 testCases = append(testCases, testCase{
2759 protocol: dtls,
2760 testType: serverTest,
2761 name: "SRTP-Server-IgnoreMKI",
2762 config: Config{
2763 SRTPProtectionProfiles: []uint16{SRTP_AES128_CM_HMAC_SHA1_80},
2764 Bugs: ProtocolBugs{
2765 SRTPMasterKeyIdentifer: "bogus",
2766 },
2767 },
2768 flags: []string{
2769 "-srtp-profiles",
2770 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2771 },
2772 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2773 })
2774 // Test that SRTP isn't negotiated on the server if there were
2775 // no matching profiles.
2776 testCases = append(testCases, testCase{
2777 protocol: dtls,
2778 testType: serverTest,
2779 name: "SRTP-Server-NoMatch",
2780 config: Config{
2781 SRTPProtectionProfiles: []uint16{100, 101, 102},
2782 },
2783 flags: []string{
2784 "-srtp-profiles",
2785 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2786 },
2787 expectedSRTPProtectionProfile: 0,
2788 })
2789 // Test that the server returning an invalid SRTP profile is
2790 // flagged as an error by the client.
2791 testCases = append(testCases, testCase{
2792 protocol: dtls,
2793 name: "SRTP-Client-NoMatch",
2794 config: Config{
2795 Bugs: ProtocolBugs{
2796 SendSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_32,
2797 },
2798 },
2799 flags: []string{
2800 "-srtp-profiles",
2801 "SRTP_AES128_CM_SHA1_80",
2802 },
2803 shouldFail: true,
2804 expectedError: ":BAD_SRTP_PROTECTION_PROFILE_LIST:",
2805 })
David Benjamin61f95272014-11-25 01:55:35 -05002806 // Test OCSP stapling and SCT list.
2807 testCases = append(testCases, testCase{
2808 name: "OCSPStapling",
2809 flags: []string{
2810 "-enable-ocsp-stapling",
2811 "-expect-ocsp-response",
2812 base64.StdEncoding.EncodeToString(testOCSPResponse),
2813 },
2814 })
2815 testCases = append(testCases, testCase{
2816 name: "SignedCertificateTimestampList",
2817 flags: []string{
2818 "-enable-signed-cert-timestamps",
2819 "-expect-signed-cert-timestamps",
2820 base64.StdEncoding.EncodeToString(testSCTList),
2821 },
2822 })
David Benjamine78bfde2014-09-06 12:45:15 -04002823}
2824
David Benjamin01fe8202014-09-24 15:21:44 -04002825func addResumptionVersionTests() {
David Benjamin01fe8202014-09-24 15:21:44 -04002826 for _, sessionVers := range tlsVersions {
David Benjamin01fe8202014-09-24 15:21:44 -04002827 for _, resumeVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05002828 protocols := []protocol{tls}
2829 if sessionVers.hasDTLS && resumeVers.hasDTLS {
2830 protocols = append(protocols, dtls)
David Benjaminbdf5e722014-11-11 00:52:15 -05002831 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002832 for _, protocol := range protocols {
2833 suffix := "-" + sessionVers.name + "-" + resumeVers.name
2834 if protocol == dtls {
2835 suffix += "-DTLS"
2836 }
2837
David Benjaminece3de92015-03-16 18:02:20 -04002838 if sessionVers.version == resumeVers.version {
2839 testCases = append(testCases, testCase{
2840 protocol: protocol,
2841 name: "Resume-Client" + suffix,
2842 resumeSession: true,
2843 config: Config{
2844 MaxVersion: sessionVers.version,
2845 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
David Benjamin8b8c0062014-11-23 02:47:52 -05002846 },
David Benjaminece3de92015-03-16 18:02:20 -04002847 expectedVersion: sessionVers.version,
2848 expectedResumeVersion: resumeVers.version,
2849 })
2850 } else {
2851 testCases = append(testCases, testCase{
2852 protocol: protocol,
2853 name: "Resume-Client-Mismatch" + suffix,
2854 resumeSession: true,
2855 config: Config{
2856 MaxVersion: sessionVers.version,
2857 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
David Benjamin8b8c0062014-11-23 02:47:52 -05002858 },
David Benjaminece3de92015-03-16 18:02:20 -04002859 expectedVersion: sessionVers.version,
2860 resumeConfig: &Config{
2861 MaxVersion: resumeVers.version,
2862 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2863 Bugs: ProtocolBugs{
2864 AllowSessionVersionMismatch: true,
2865 },
2866 },
2867 expectedResumeVersion: resumeVers.version,
2868 shouldFail: true,
2869 expectedError: ":OLD_SESSION_VERSION_NOT_RETURNED:",
2870 })
2871 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002872
2873 testCases = append(testCases, testCase{
2874 protocol: protocol,
2875 name: "Resume-Client-NoResume" + suffix,
David Benjamin8b8c0062014-11-23 02:47:52 -05002876 resumeSession: true,
2877 config: Config{
2878 MaxVersion: sessionVers.version,
2879 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2880 },
2881 expectedVersion: sessionVers.version,
2882 resumeConfig: &Config{
2883 MaxVersion: resumeVers.version,
2884 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2885 },
2886 newSessionsOnResume: true,
Adam Langleyb0eef0a2015-06-02 10:47:39 -07002887 expectResumeRejected: true,
David Benjamin8b8c0062014-11-23 02:47:52 -05002888 expectedResumeVersion: resumeVers.version,
2889 })
2890
David Benjamin8b8c0062014-11-23 02:47:52 -05002891 testCases = append(testCases, testCase{
2892 protocol: protocol,
2893 testType: serverTest,
2894 name: "Resume-Server" + suffix,
David Benjamin8b8c0062014-11-23 02:47:52 -05002895 resumeSession: true,
2896 config: Config{
2897 MaxVersion: sessionVers.version,
2898 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2899 },
Adam Langleyb0eef0a2015-06-02 10:47:39 -07002900 expectedVersion: sessionVers.version,
2901 expectResumeRejected: sessionVers.version != resumeVers.version,
David Benjamin8b8c0062014-11-23 02:47:52 -05002902 resumeConfig: &Config{
2903 MaxVersion: resumeVers.version,
2904 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2905 },
2906 expectedResumeVersion: resumeVers.version,
2907 })
2908 }
David Benjamin01fe8202014-09-24 15:21:44 -04002909 }
2910 }
David Benjaminece3de92015-03-16 18:02:20 -04002911
2912 testCases = append(testCases, testCase{
2913 name: "Resume-Client-CipherMismatch",
2914 resumeSession: true,
2915 config: Config{
2916 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2917 },
2918 resumeConfig: &Config{
2919 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2920 Bugs: ProtocolBugs{
2921 SendCipherSuite: TLS_RSA_WITH_AES_128_CBC_SHA,
2922 },
2923 },
2924 shouldFail: true,
2925 expectedError: ":OLD_SESSION_CIPHER_NOT_RETURNED:",
2926 })
David Benjamin01fe8202014-09-24 15:21:44 -04002927}
2928
Adam Langley2ae77d22014-10-28 17:29:33 -07002929func addRenegotiationTests() {
David Benjamin44d3eed2015-05-21 01:29:55 -04002930 // Servers cannot renegotiate.
David Benjaminb16346b2015-04-08 19:16:58 -04002931 testCases = append(testCases, testCase{
2932 testType: serverTest,
David Benjamin44d3eed2015-05-21 01:29:55 -04002933 name: "Renegotiate-Server-Forbidden",
David Benjaminb16346b2015-04-08 19:16:58 -04002934 renegotiate: true,
2935 flags: []string{"-reject-peer-renegotiations"},
2936 shouldFail: true,
2937 expectedError: ":NO_RENEGOTIATION:",
2938 expectedLocalError: "remote error: no renegotiation",
2939 })
Adam Langley2ae77d22014-10-28 17:29:33 -07002940 // TODO(agl): test the renegotiation info SCSV.
Adam Langleycf2d4f42014-10-28 19:06:14 -07002941 testCases = append(testCases, testCase{
David Benjamin4b27d9f2015-05-12 22:42:52 -04002942 name: "Renegotiate-Client",
David Benjamincdea40c2015-03-19 14:09:43 -04002943 config: Config{
2944 Bugs: ProtocolBugs{
David Benjamin4b27d9f2015-05-12 22:42:52 -04002945 FailIfResumeOnRenego: true,
David Benjamincdea40c2015-03-19 14:09:43 -04002946 },
2947 },
2948 renegotiate: true,
2949 })
2950 testCases = append(testCases, testCase{
Adam Langleycf2d4f42014-10-28 19:06:14 -07002951 name: "Renegotiate-Client-EmptyExt",
2952 renegotiate: true,
2953 config: Config{
2954 Bugs: ProtocolBugs{
2955 EmptyRenegotiationInfo: true,
2956 },
2957 },
2958 shouldFail: true,
2959 expectedError: ":RENEGOTIATION_MISMATCH:",
2960 })
2961 testCases = append(testCases, testCase{
2962 name: "Renegotiate-Client-BadExt",
2963 renegotiate: true,
2964 config: Config{
2965 Bugs: ProtocolBugs{
2966 BadRenegotiationInfo: true,
2967 },
2968 },
2969 shouldFail: true,
2970 expectedError: ":RENEGOTIATION_MISMATCH:",
2971 })
2972 testCases = append(testCases, testCase{
David Benjamincff0b902015-05-15 23:09:47 -04002973 name: "Renegotiate-Client-NoExt",
2974 renegotiate: true,
2975 config: Config{
2976 Bugs: ProtocolBugs{
2977 NoRenegotiationInfo: true,
2978 },
2979 },
2980 shouldFail: true,
2981 expectedError: ":UNSAFE_LEGACY_RENEGOTIATION_DISABLED:",
2982 flags: []string{"-no-legacy-server-connect"},
2983 })
2984 testCases = append(testCases, testCase{
2985 name: "Renegotiate-Client-NoExt-Allowed",
2986 renegotiate: true,
2987 config: Config{
2988 Bugs: ProtocolBugs{
2989 NoRenegotiationInfo: true,
2990 },
2991 },
2992 })
2993 testCases = append(testCases, testCase{
Adam Langleycf2d4f42014-10-28 19:06:14 -07002994 name: "Renegotiate-Client-SwitchCiphers",
2995 renegotiate: true,
2996 config: Config{
2997 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2998 },
2999 renegotiateCiphers: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
3000 })
3001 testCases = append(testCases, testCase{
3002 name: "Renegotiate-Client-SwitchCiphers2",
3003 renegotiate: true,
3004 config: Config{
3005 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
3006 },
3007 renegotiateCiphers: []uint16{TLS_RSA_WITH_RC4_128_SHA},
3008 })
David Benjaminc44b1df2014-11-23 12:11:01 -05003009 testCases = append(testCases, testCase{
David Benjaminb16346b2015-04-08 19:16:58 -04003010 name: "Renegotiate-Client-Forbidden",
3011 renegotiate: true,
3012 flags: []string{"-reject-peer-renegotiations"},
3013 shouldFail: true,
3014 expectedError: ":NO_RENEGOTIATION:",
3015 expectedLocalError: "remote error: no renegotiation",
3016 })
3017 testCases = append(testCases, testCase{
David Benjaminc44b1df2014-11-23 12:11:01 -05003018 name: "Renegotiate-SameClientVersion",
3019 renegotiate: true,
3020 config: Config{
3021 MaxVersion: VersionTLS10,
3022 Bugs: ProtocolBugs{
3023 RequireSameRenegoClientVersion: true,
3024 },
3025 },
3026 })
Adam Langley2ae77d22014-10-28 17:29:33 -07003027}
3028
David Benjamin5e961c12014-11-07 01:48:35 -05003029func addDTLSReplayTests() {
3030 // Test that sequence number replays are detected.
3031 testCases = append(testCases, testCase{
3032 protocol: dtls,
3033 name: "DTLS-Replay",
3034 replayWrites: true,
3035 })
3036
3037 // Test the outgoing sequence number skipping by values larger
3038 // than the retransmit window.
3039 testCases = append(testCases, testCase{
3040 protocol: dtls,
3041 name: "DTLS-Replay-LargeGaps",
3042 config: Config{
3043 Bugs: ProtocolBugs{
3044 SequenceNumberIncrement: 127,
3045 },
3046 },
3047 replayWrites: true,
3048 })
3049}
3050
Feng Lu41aa3252014-11-21 22:47:56 -08003051func addFastRadioPaddingTests() {
David Benjamin1e29a6b2014-12-10 02:27:24 -05003052 testCases = append(testCases, testCase{
3053 protocol: tls,
3054 name: "FastRadio-Padding",
Feng Lu41aa3252014-11-21 22:47:56 -08003055 config: Config{
3056 Bugs: ProtocolBugs{
3057 RequireFastradioPadding: true,
3058 },
3059 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05003060 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08003061 })
David Benjamin1e29a6b2014-12-10 02:27:24 -05003062 testCases = append(testCases, testCase{
3063 protocol: dtls,
David Benjamin5f237bc2015-02-11 17:14:15 -05003064 name: "FastRadio-Padding-DTLS",
Feng Lu41aa3252014-11-21 22:47:56 -08003065 config: Config{
3066 Bugs: ProtocolBugs{
3067 RequireFastradioPadding: true,
3068 },
3069 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05003070 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08003071 })
3072}
3073
David Benjamin000800a2014-11-14 01:43:59 -05003074var testHashes = []struct {
3075 name string
3076 id uint8
3077}{
3078 {"SHA1", hashSHA1},
3079 {"SHA224", hashSHA224},
3080 {"SHA256", hashSHA256},
3081 {"SHA384", hashSHA384},
3082 {"SHA512", hashSHA512},
3083}
3084
3085func addSigningHashTests() {
3086 // Make sure each hash works. Include some fake hashes in the list and
3087 // ensure they're ignored.
3088 for _, hash := range testHashes {
3089 testCases = append(testCases, testCase{
3090 name: "SigningHash-ClientAuth-" + hash.name,
3091 config: Config{
3092 ClientAuth: RequireAnyClientCert,
3093 SignatureAndHashes: []signatureAndHash{
3094 {signatureRSA, 42},
3095 {signatureRSA, hash.id},
3096 {signatureRSA, 255},
3097 },
3098 },
3099 flags: []string{
3100 "-cert-file", rsaCertificateFile,
3101 "-key-file", rsaKeyFile,
3102 },
3103 })
3104
3105 testCases = append(testCases, testCase{
3106 testType: serverTest,
3107 name: "SigningHash-ServerKeyExchange-Sign-" + hash.name,
3108 config: Config{
3109 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
3110 SignatureAndHashes: []signatureAndHash{
3111 {signatureRSA, 42},
3112 {signatureRSA, hash.id},
3113 {signatureRSA, 255},
3114 },
3115 },
3116 })
3117 }
3118
3119 // Test that hash resolution takes the signature type into account.
3120 testCases = append(testCases, testCase{
3121 name: "SigningHash-ClientAuth-SignatureType",
3122 config: Config{
3123 ClientAuth: RequireAnyClientCert,
3124 SignatureAndHashes: []signatureAndHash{
3125 {signatureECDSA, hashSHA512},
3126 {signatureRSA, hashSHA384},
3127 {signatureECDSA, hashSHA1},
3128 },
3129 },
3130 flags: []string{
3131 "-cert-file", rsaCertificateFile,
3132 "-key-file", rsaKeyFile,
3133 },
3134 })
3135
3136 testCases = append(testCases, testCase{
3137 testType: serverTest,
3138 name: "SigningHash-ServerKeyExchange-SignatureType",
3139 config: Config{
3140 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
3141 SignatureAndHashes: []signatureAndHash{
3142 {signatureECDSA, hashSHA512},
3143 {signatureRSA, hashSHA384},
3144 {signatureECDSA, hashSHA1},
3145 },
3146 },
3147 })
3148
3149 // Test that, if the list is missing, the peer falls back to SHA-1.
3150 testCases = append(testCases, testCase{
3151 name: "SigningHash-ClientAuth-Fallback",
3152 config: Config{
3153 ClientAuth: RequireAnyClientCert,
3154 SignatureAndHashes: []signatureAndHash{
3155 {signatureRSA, hashSHA1},
3156 },
3157 Bugs: ProtocolBugs{
3158 NoSignatureAndHashes: true,
3159 },
3160 },
3161 flags: []string{
3162 "-cert-file", rsaCertificateFile,
3163 "-key-file", rsaKeyFile,
3164 },
3165 })
3166
3167 testCases = append(testCases, testCase{
3168 testType: serverTest,
3169 name: "SigningHash-ServerKeyExchange-Fallback",
3170 config: Config{
3171 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
3172 SignatureAndHashes: []signatureAndHash{
3173 {signatureRSA, hashSHA1},
3174 },
3175 Bugs: ProtocolBugs{
3176 NoSignatureAndHashes: true,
3177 },
3178 },
3179 })
David Benjamin72dc7832015-03-16 17:49:43 -04003180
3181 // Test that hash preferences are enforced. BoringSSL defaults to
3182 // rejecting MD5 signatures.
3183 testCases = append(testCases, testCase{
3184 testType: serverTest,
3185 name: "SigningHash-ClientAuth-Enforced",
3186 config: Config{
3187 Certificates: []Certificate{rsaCertificate},
3188 SignatureAndHashes: []signatureAndHash{
3189 {signatureRSA, hashMD5},
3190 // Advertise SHA-1 so the handshake will
3191 // proceed, but the shim's preferences will be
3192 // ignored in CertificateVerify generation, so
3193 // MD5 will be chosen.
3194 {signatureRSA, hashSHA1},
3195 },
3196 Bugs: ProtocolBugs{
3197 IgnorePeerSignatureAlgorithmPreferences: true,
3198 },
3199 },
3200 flags: []string{"-require-any-client-certificate"},
3201 shouldFail: true,
3202 expectedError: ":WRONG_SIGNATURE_TYPE:",
3203 })
3204
3205 testCases = append(testCases, testCase{
3206 name: "SigningHash-ServerKeyExchange-Enforced",
3207 config: Config{
3208 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
3209 SignatureAndHashes: []signatureAndHash{
3210 {signatureRSA, hashMD5},
3211 },
3212 Bugs: ProtocolBugs{
3213 IgnorePeerSignatureAlgorithmPreferences: true,
3214 },
3215 },
3216 shouldFail: true,
3217 expectedError: ":WRONG_SIGNATURE_TYPE:",
3218 })
David Benjamin000800a2014-11-14 01:43:59 -05003219}
3220
David Benjamin83f90402015-01-27 01:09:43 -05003221// timeouts is the retransmit schedule for BoringSSL. It doubles and
3222// caps at 60 seconds. On the 13th timeout, it gives up.
3223var timeouts = []time.Duration{
3224 1 * time.Second,
3225 2 * time.Second,
3226 4 * time.Second,
3227 8 * time.Second,
3228 16 * time.Second,
3229 32 * time.Second,
3230 60 * time.Second,
3231 60 * time.Second,
3232 60 * time.Second,
3233 60 * time.Second,
3234 60 * time.Second,
3235 60 * time.Second,
3236 60 * time.Second,
3237}
3238
3239func addDTLSRetransmitTests() {
3240 // Test that this is indeed the timeout schedule. Stress all
3241 // four patterns of handshake.
3242 for i := 1; i < len(timeouts); i++ {
3243 number := strconv.Itoa(i)
3244 testCases = append(testCases, testCase{
3245 protocol: dtls,
3246 name: "DTLS-Retransmit-Client-" + number,
3247 config: Config{
3248 Bugs: ProtocolBugs{
3249 TimeoutSchedule: timeouts[:i],
3250 },
3251 },
3252 resumeSession: true,
3253 flags: []string{"-async"},
3254 })
3255 testCases = append(testCases, testCase{
3256 protocol: dtls,
3257 testType: serverTest,
3258 name: "DTLS-Retransmit-Server-" + number,
3259 config: Config{
3260 Bugs: ProtocolBugs{
3261 TimeoutSchedule: timeouts[:i],
3262 },
3263 },
3264 resumeSession: true,
3265 flags: []string{"-async"},
3266 })
3267 }
3268
3269 // Test that exceeding the timeout schedule hits a read
3270 // timeout.
3271 testCases = append(testCases, testCase{
3272 protocol: dtls,
3273 name: "DTLS-Retransmit-Timeout",
3274 config: Config{
3275 Bugs: ProtocolBugs{
3276 TimeoutSchedule: timeouts,
3277 },
3278 },
3279 resumeSession: true,
3280 flags: []string{"-async"},
3281 shouldFail: true,
3282 expectedError: ":READ_TIMEOUT_EXPIRED:",
3283 })
3284
3285 // Test that timeout handling has a fudge factor, due to API
3286 // problems.
3287 testCases = append(testCases, testCase{
3288 protocol: dtls,
3289 name: "DTLS-Retransmit-Fudge",
3290 config: Config{
3291 Bugs: ProtocolBugs{
3292 TimeoutSchedule: []time.Duration{
3293 timeouts[0] - 10*time.Millisecond,
3294 },
3295 },
3296 },
3297 resumeSession: true,
3298 flags: []string{"-async"},
3299 })
David Benjamin7eaab4c2015-03-02 19:01:16 -05003300
3301 // Test that the final Finished retransmitting isn't
3302 // duplicated if the peer badly fragments everything.
3303 testCases = append(testCases, testCase{
3304 testType: serverTest,
3305 protocol: dtls,
3306 name: "DTLS-Retransmit-Fragmented",
3307 config: Config{
3308 Bugs: ProtocolBugs{
3309 TimeoutSchedule: []time.Duration{timeouts[0]},
3310 MaxHandshakeRecordLength: 2,
3311 },
3312 },
3313 flags: []string{"-async"},
3314 })
David Benjamin83f90402015-01-27 01:09:43 -05003315}
3316
David Benjaminc565ebb2015-04-03 04:06:36 -04003317func addExportKeyingMaterialTests() {
3318 for _, vers := range tlsVersions {
3319 if vers.version == VersionSSL30 {
3320 continue
3321 }
3322 testCases = append(testCases, testCase{
3323 name: "ExportKeyingMaterial-" + vers.name,
3324 config: Config{
3325 MaxVersion: vers.version,
3326 },
3327 exportKeyingMaterial: 1024,
3328 exportLabel: "label",
3329 exportContext: "context",
3330 useExportContext: true,
3331 })
3332 testCases = append(testCases, testCase{
3333 name: "ExportKeyingMaterial-NoContext-" + vers.name,
3334 config: Config{
3335 MaxVersion: vers.version,
3336 },
3337 exportKeyingMaterial: 1024,
3338 })
3339 testCases = append(testCases, testCase{
3340 name: "ExportKeyingMaterial-EmptyContext-" + vers.name,
3341 config: Config{
3342 MaxVersion: vers.version,
3343 },
3344 exportKeyingMaterial: 1024,
3345 useExportContext: true,
3346 })
3347 testCases = append(testCases, testCase{
3348 name: "ExportKeyingMaterial-Small-" + vers.name,
3349 config: Config{
3350 MaxVersion: vers.version,
3351 },
3352 exportKeyingMaterial: 1,
3353 exportLabel: "label",
3354 exportContext: "context",
3355 useExportContext: true,
3356 })
3357 }
3358 testCases = append(testCases, testCase{
3359 name: "ExportKeyingMaterial-SSL3",
3360 config: Config{
3361 MaxVersion: VersionSSL30,
3362 },
3363 exportKeyingMaterial: 1024,
3364 exportLabel: "label",
3365 exportContext: "context",
3366 useExportContext: true,
3367 shouldFail: true,
3368 expectedError: "failed to export keying material",
3369 })
3370}
3371
David Benjamin884fdf12014-08-02 15:28:23 -04003372func worker(statusChan chan statusMsg, c chan *testCase, buildDir string, wg *sync.WaitGroup) {
Adam Langley95c29f32014-06-20 12:00:00 -07003373 defer wg.Done()
3374
3375 for test := range c {
Adam Langley69a01602014-11-17 17:26:55 -08003376 var err error
3377
3378 if *mallocTest < 0 {
3379 statusChan <- statusMsg{test: test, started: true}
3380 err = runTest(test, buildDir, -1)
3381 } else {
3382 for mallocNumToFail := int64(*mallocTest); ; mallocNumToFail++ {
3383 statusChan <- statusMsg{test: test, started: true}
3384 if err = runTest(test, buildDir, mallocNumToFail); err != errMoreMallocs {
3385 if err != nil {
3386 fmt.Printf("\n\nmalloc test failed at %d: %s\n", mallocNumToFail, err)
3387 }
3388 break
3389 }
3390 }
3391 }
Adam Langley95c29f32014-06-20 12:00:00 -07003392 statusChan <- statusMsg{test: test, err: err}
3393 }
3394}
3395
3396type statusMsg struct {
3397 test *testCase
3398 started bool
3399 err error
3400}
3401
David Benjamin5f237bc2015-02-11 17:14:15 -05003402func statusPrinter(doneChan chan *testOutput, statusChan chan statusMsg, total int) {
Adam Langley95c29f32014-06-20 12:00:00 -07003403 var started, done, failed, lineLen int
Adam Langley95c29f32014-06-20 12:00:00 -07003404
David Benjamin5f237bc2015-02-11 17:14:15 -05003405 testOutput := newTestOutput()
Adam Langley95c29f32014-06-20 12:00:00 -07003406 for msg := range statusChan {
David Benjamin5f237bc2015-02-11 17:14:15 -05003407 if !*pipe {
3408 // Erase the previous status line.
David Benjamin87c8a642015-02-21 01:54:29 -05003409 var erase string
3410 for i := 0; i < lineLen; i++ {
3411 erase += "\b \b"
3412 }
3413 fmt.Print(erase)
David Benjamin5f237bc2015-02-11 17:14:15 -05003414 }
3415
Adam Langley95c29f32014-06-20 12:00:00 -07003416 if msg.started {
3417 started++
3418 } else {
3419 done++
David Benjamin5f237bc2015-02-11 17:14:15 -05003420
3421 if msg.err != nil {
3422 fmt.Printf("FAILED (%s)\n%s\n", msg.test.name, msg.err)
3423 failed++
3424 testOutput.addResult(msg.test.name, "FAIL")
3425 } else {
3426 if *pipe {
3427 // Print each test instead of a status line.
3428 fmt.Printf("PASSED (%s)\n", msg.test.name)
3429 }
3430 testOutput.addResult(msg.test.name, "PASS")
3431 }
Adam Langley95c29f32014-06-20 12:00:00 -07003432 }
3433
David Benjamin5f237bc2015-02-11 17:14:15 -05003434 if !*pipe {
3435 // Print a new status line.
3436 line := fmt.Sprintf("%d/%d/%d/%d", failed, done, started, total)
3437 lineLen = len(line)
3438 os.Stdout.WriteString(line)
Adam Langley95c29f32014-06-20 12:00:00 -07003439 }
Adam Langley95c29f32014-06-20 12:00:00 -07003440 }
David Benjamin5f237bc2015-02-11 17:14:15 -05003441
3442 doneChan <- testOutput
Adam Langley95c29f32014-06-20 12:00:00 -07003443}
3444
3445func main() {
3446 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 -04003447 var flagNumWorkers *int = flag.Int("num-workers", runtime.NumCPU(), "The number of workers to run in parallel.")
David Benjamin884fdf12014-08-02 15:28:23 -04003448 var flagBuildDir *string = flag.String("build-dir", "../../../build", "The build directory to run the shim from.")
Adam Langley95c29f32014-06-20 12:00:00 -07003449
3450 flag.Parse()
3451
3452 addCipherSuiteTests()
3453 addBadECDSASignatureTests()
Adam Langley80842bd2014-06-20 12:00:00 -07003454 addCBCPaddingTests()
Kenny Root7fdeaf12014-08-05 15:23:37 -07003455 addCBCSplittingTests()
David Benjamin636293b2014-07-08 17:59:18 -04003456 addClientAuthTests()
Adam Langley524e7172015-02-20 16:04:00 -08003457 addDDoSCallbackTests()
David Benjamin7e2e6cf2014-08-07 17:44:24 -04003458 addVersionNegotiationTests()
David Benjaminaccb4542014-12-12 23:44:33 -05003459 addMinimumVersionTests()
David Benjamin5c24a1d2014-08-31 00:59:27 -04003460 addD5BugTests()
David Benjamine78bfde2014-09-06 12:45:15 -04003461 addExtensionTests()
David Benjamin01fe8202014-09-24 15:21:44 -04003462 addResumptionVersionTests()
Adam Langley75712922014-10-10 16:23:43 -07003463 addExtendedMasterSecretTests()
Adam Langley2ae77d22014-10-28 17:29:33 -07003464 addRenegotiationTests()
David Benjamin5e961c12014-11-07 01:48:35 -05003465 addDTLSReplayTests()
David Benjamin000800a2014-11-14 01:43:59 -05003466 addSigningHashTests()
Feng Lu41aa3252014-11-21 22:47:56 -08003467 addFastRadioPaddingTests()
David Benjamin83f90402015-01-27 01:09:43 -05003468 addDTLSRetransmitTests()
David Benjaminc565ebb2015-04-03 04:06:36 -04003469 addExportKeyingMaterialTests()
David Benjamin43ec06f2014-08-05 02:28:57 -04003470 for _, async := range []bool{false, true} {
3471 for _, splitHandshake := range []bool{false, true} {
David Benjamin6fd297b2014-08-11 18:43:38 -04003472 for _, protocol := range []protocol{tls, dtls} {
3473 addStateMachineCoverageTests(async, splitHandshake, protocol)
3474 }
David Benjamin43ec06f2014-08-05 02:28:57 -04003475 }
3476 }
Adam Langley95c29f32014-06-20 12:00:00 -07003477
3478 var wg sync.WaitGroup
3479
David Benjamin2bc8e6f2014-08-02 15:22:37 -04003480 numWorkers := *flagNumWorkers
Adam Langley95c29f32014-06-20 12:00:00 -07003481
3482 statusChan := make(chan statusMsg, numWorkers)
3483 testChan := make(chan *testCase, numWorkers)
David Benjamin5f237bc2015-02-11 17:14:15 -05003484 doneChan := make(chan *testOutput)
Adam Langley95c29f32014-06-20 12:00:00 -07003485
David Benjamin025b3d32014-07-01 19:53:04 -04003486 go statusPrinter(doneChan, statusChan, len(testCases))
Adam Langley95c29f32014-06-20 12:00:00 -07003487
3488 for i := 0; i < numWorkers; i++ {
3489 wg.Add(1)
David Benjamin884fdf12014-08-02 15:28:23 -04003490 go worker(statusChan, testChan, *flagBuildDir, &wg)
Adam Langley95c29f32014-06-20 12:00:00 -07003491 }
3492
David Benjamin025b3d32014-07-01 19:53:04 -04003493 for i := range testCases {
3494 if len(*flagTest) == 0 || *flagTest == testCases[i].name {
3495 testChan <- &testCases[i]
Adam Langley95c29f32014-06-20 12:00:00 -07003496 }
3497 }
3498
3499 close(testChan)
3500 wg.Wait()
3501 close(statusChan)
David Benjamin5f237bc2015-02-11 17:14:15 -05003502 testOutput := <-doneChan
Adam Langley95c29f32014-06-20 12:00:00 -07003503
3504 fmt.Printf("\n")
David Benjamin5f237bc2015-02-11 17:14:15 -05003505
3506 if *jsonOutput != "" {
3507 if err := testOutput.writeTo(*jsonOutput); err != nil {
3508 fmt.Fprintf(os.Stderr, "Error: %s\n", err)
3509 }
3510 }
David Benjamin2ab7a862015-04-04 17:02:18 -04003511
3512 if !testOutput.allPassed {
3513 os.Exit(1)
3514 }
Adam Langley95c29f32014-06-20 12:00:00 -07003515}