blob: 3e633166d55d74834d1d44fbfd927a62108c7a82 [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
1951 // When a session is resumed, it should still be aware that its master
1952 // secret was generated via EMS and thus it's safe to use tls-unique.
1953 testCases = append(testCases, testCase{
1954 name: "ExtendedMasterSecret-Resume",
1955 config: Config{
1956 Bugs: ProtocolBugs{
1957 RequireExtendedMasterSecret: true,
1958 },
1959 },
1960 flags: []string{expectEMSFlag},
1961 resumeSession: true,
1962 })
1963}
1964
David Benjamin43ec06f2014-08-05 02:28:57 -04001965// Adds tests that try to cover the range of the handshake state machine, under
1966// various conditions. Some of these are redundant with other tests, but they
1967// only cover the synchronous case.
David Benjamin6fd297b2014-08-11 18:43:38 -04001968func addStateMachineCoverageTests(async, splitHandshake bool, protocol protocol) {
David Benjamin760b1dd2015-05-15 23:33:48 -04001969 var tests []testCase
1970
1971 // Basic handshake, with resumption. Client and server,
1972 // session ID and session ticket.
1973 tests = append(tests, testCase{
1974 name: "Basic-Client",
1975 resumeSession: true,
1976 })
1977 tests = append(tests, testCase{
1978 name: "Basic-Client-RenewTicket",
1979 config: Config{
1980 Bugs: ProtocolBugs{
1981 RenewTicketOnResume: true,
1982 },
1983 },
1984 resumeSession: true,
1985 })
1986 tests = append(tests, testCase{
1987 name: "Basic-Client-NoTicket",
1988 config: Config{
1989 SessionTicketsDisabled: true,
1990 },
1991 resumeSession: true,
1992 })
1993 tests = append(tests, testCase{
1994 name: "Basic-Client-Implicit",
1995 flags: []string{"-implicit-handshake"},
1996 resumeSession: true,
1997 })
1998 tests = append(tests, testCase{
1999 testType: serverTest,
2000 name: "Basic-Server",
2001 resumeSession: true,
2002 })
2003 tests = append(tests, testCase{
2004 testType: serverTest,
2005 name: "Basic-Server-NoTickets",
2006 config: Config{
2007 SessionTicketsDisabled: true,
2008 },
2009 resumeSession: true,
2010 })
2011 tests = append(tests, testCase{
2012 testType: serverTest,
2013 name: "Basic-Server-Implicit",
2014 flags: []string{"-implicit-handshake"},
2015 resumeSession: true,
2016 })
2017 tests = append(tests, testCase{
2018 testType: serverTest,
2019 name: "Basic-Server-EarlyCallback",
2020 flags: []string{"-use-early-callback"},
2021 resumeSession: true,
2022 })
2023
2024 // TLS client auth.
2025 tests = append(tests, testCase{
2026 testType: clientTest,
2027 name: "ClientAuth-Client",
2028 config: Config{
2029 ClientAuth: RequireAnyClientCert,
2030 },
2031 flags: []string{
2032 "-cert-file", rsaCertificateFile,
2033 "-key-file", rsaKeyFile,
2034 },
2035 })
2036 tests = append(tests, testCase{
2037 testType: serverTest,
2038 name: "ClientAuth-Server",
2039 config: Config{
2040 Certificates: []Certificate{rsaCertificate},
2041 },
2042 flags: []string{"-require-any-client-certificate"},
2043 })
2044
2045 // No session ticket support; server doesn't send NewSessionTicket.
2046 tests = append(tests, testCase{
2047 name: "SessionTicketsDisabled-Client",
2048 config: Config{
2049 SessionTicketsDisabled: true,
2050 },
2051 })
2052 tests = append(tests, testCase{
2053 testType: serverTest,
2054 name: "SessionTicketsDisabled-Server",
2055 config: Config{
2056 SessionTicketsDisabled: true,
2057 },
2058 })
2059
2060 // Skip ServerKeyExchange in PSK key exchange if there's no
2061 // identity hint.
2062 tests = append(tests, testCase{
2063 name: "EmptyPSKHint-Client",
2064 config: Config{
2065 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
2066 PreSharedKey: []byte("secret"),
2067 },
2068 flags: []string{"-psk", "secret"},
2069 })
2070 tests = append(tests, testCase{
2071 testType: serverTest,
2072 name: "EmptyPSKHint-Server",
2073 config: Config{
2074 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
2075 PreSharedKey: []byte("secret"),
2076 },
2077 flags: []string{"-psk", "secret"},
2078 })
2079
2080 if protocol == tls {
2081 tests = append(tests, testCase{
2082 name: "Renegotiate-Client",
2083 renegotiate: true,
2084 })
2085 // NPN on client and server; results in post-handshake message.
2086 tests = append(tests, testCase{
2087 name: "NPN-Client",
2088 config: Config{
2089 NextProtos: []string{"foo"},
2090 },
2091 flags: []string{"-select-next-proto", "foo"},
2092 expectedNextProto: "foo",
2093 expectedNextProtoType: npn,
2094 })
2095 tests = append(tests, testCase{
2096 testType: serverTest,
2097 name: "NPN-Server",
2098 config: Config{
2099 NextProtos: []string{"bar"},
2100 },
2101 flags: []string{
2102 "-advertise-npn", "\x03foo\x03bar\x03baz",
2103 "-expect-next-proto", "bar",
2104 },
2105 expectedNextProto: "bar",
2106 expectedNextProtoType: npn,
2107 })
2108
2109 // TODO(davidben): Add tests for when False Start doesn't trigger.
2110
2111 // Client does False Start and negotiates NPN.
2112 tests = append(tests, testCase{
2113 name: "FalseStart",
2114 config: Config{
2115 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2116 NextProtos: []string{"foo"},
2117 Bugs: ProtocolBugs{
2118 ExpectFalseStart: true,
2119 },
2120 },
2121 flags: []string{
2122 "-false-start",
2123 "-select-next-proto", "foo",
2124 },
2125 shimWritesFirst: true,
2126 resumeSession: true,
2127 })
2128
2129 // Client does False Start and negotiates ALPN.
2130 tests = append(tests, testCase{
2131 name: "FalseStart-ALPN",
2132 config: Config{
2133 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2134 NextProtos: []string{"foo"},
2135 Bugs: ProtocolBugs{
2136 ExpectFalseStart: true,
2137 },
2138 },
2139 flags: []string{
2140 "-false-start",
2141 "-advertise-alpn", "\x03foo",
2142 },
2143 shimWritesFirst: true,
2144 resumeSession: true,
2145 })
2146
2147 // Client does False Start but doesn't explicitly call
2148 // SSL_connect.
2149 tests = append(tests, testCase{
2150 name: "FalseStart-Implicit",
2151 config: Config{
2152 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2153 NextProtos: []string{"foo"},
2154 },
2155 flags: []string{
2156 "-implicit-handshake",
2157 "-false-start",
2158 "-advertise-alpn", "\x03foo",
2159 },
2160 })
2161
2162 // False Start without session tickets.
2163 tests = append(tests, testCase{
2164 name: "FalseStart-SessionTicketsDisabled",
2165 config: Config{
2166 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2167 NextProtos: []string{"foo"},
2168 SessionTicketsDisabled: true,
2169 Bugs: ProtocolBugs{
2170 ExpectFalseStart: true,
2171 },
2172 },
2173 flags: []string{
2174 "-false-start",
2175 "-select-next-proto", "foo",
2176 },
2177 shimWritesFirst: true,
2178 })
2179
2180 // Server parses a V2ClientHello.
2181 tests = append(tests, testCase{
2182 testType: serverTest,
2183 name: "SendV2ClientHello",
2184 config: Config{
2185 // Choose a cipher suite that does not involve
2186 // elliptic curves, so no extensions are
2187 // involved.
2188 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2189 Bugs: ProtocolBugs{
2190 SendV2ClientHello: true,
2191 },
2192 },
2193 })
2194
2195 // Client sends a Channel ID.
2196 tests = append(tests, testCase{
2197 name: "ChannelID-Client",
2198 config: Config{
2199 RequestChannelID: true,
2200 },
2201 flags: []string{"-send-channel-id", channelIDKeyFile},
2202 resumeSession: true,
2203 expectChannelID: true,
2204 })
2205
2206 // Server accepts a Channel ID.
2207 tests = append(tests, testCase{
2208 testType: serverTest,
2209 name: "ChannelID-Server",
2210 config: Config{
2211 ChannelID: channelIDKey,
2212 },
2213 flags: []string{
2214 "-expect-channel-id",
2215 base64.StdEncoding.EncodeToString(channelIDBytes),
2216 },
2217 resumeSession: true,
2218 expectChannelID: true,
2219 })
2220 } else {
2221 tests = append(tests, testCase{
2222 name: "SkipHelloVerifyRequest",
2223 config: Config{
2224 Bugs: ProtocolBugs{
2225 SkipHelloVerifyRequest: true,
2226 },
2227 },
2228 })
2229 }
2230
David Benjamin43ec06f2014-08-05 02:28:57 -04002231 var suffix string
2232 var flags []string
2233 var maxHandshakeRecordLength int
David Benjamin6fd297b2014-08-11 18:43:38 -04002234 if protocol == dtls {
2235 suffix = "-DTLS"
2236 }
David Benjamin43ec06f2014-08-05 02:28:57 -04002237 if async {
David Benjamin6fd297b2014-08-11 18:43:38 -04002238 suffix += "-Async"
David Benjamin43ec06f2014-08-05 02:28:57 -04002239 flags = append(flags, "-async")
2240 } else {
David Benjamin6fd297b2014-08-11 18:43:38 -04002241 suffix += "-Sync"
David Benjamin43ec06f2014-08-05 02:28:57 -04002242 }
2243 if splitHandshake {
2244 suffix += "-SplitHandshakeRecords"
David Benjamin98214542014-08-07 18:02:39 -04002245 maxHandshakeRecordLength = 1
David Benjamin43ec06f2014-08-05 02:28:57 -04002246 }
David Benjamin760b1dd2015-05-15 23:33:48 -04002247 for _, test := range tests {
2248 test.protocol = protocol
2249 test.name += suffix
2250 test.config.Bugs.MaxHandshakeRecordLength = maxHandshakeRecordLength
2251 test.flags = append(test.flags, flags...)
2252 testCases = append(testCases, test)
David Benjamin6fd297b2014-08-11 18:43:38 -04002253 }
David Benjamin43ec06f2014-08-05 02:28:57 -04002254}
2255
Adam Langley524e7172015-02-20 16:04:00 -08002256func addDDoSCallbackTests() {
2257 // DDoS callback.
2258
2259 for _, resume := range []bool{false, true} {
2260 suffix := "Resume"
2261 if resume {
2262 suffix = "No" + suffix
2263 }
2264
2265 testCases = append(testCases, testCase{
2266 testType: serverTest,
2267 name: "Server-DDoS-OK-" + suffix,
2268 flags: []string{"-install-ddos-callback"},
2269 resumeSession: resume,
2270 })
2271
2272 failFlag := "-fail-ddos-callback"
2273 if resume {
2274 failFlag = "-fail-second-ddos-callback"
2275 }
2276 testCases = append(testCases, testCase{
2277 testType: serverTest,
2278 name: "Server-DDoS-Reject-" + suffix,
2279 flags: []string{"-install-ddos-callback", failFlag},
2280 resumeSession: resume,
2281 shouldFail: true,
2282 expectedError: ":CONNECTION_REJECTED:",
2283 })
2284 }
2285}
2286
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002287func addVersionNegotiationTests() {
2288 for i, shimVers := range tlsVersions {
2289 // Assemble flags to disable all newer versions on the shim.
2290 var flags []string
2291 for _, vers := range tlsVersions[i+1:] {
2292 flags = append(flags, vers.flag)
2293 }
2294
2295 for _, runnerVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05002296 protocols := []protocol{tls}
2297 if runnerVers.hasDTLS && shimVers.hasDTLS {
2298 protocols = append(protocols, dtls)
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002299 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002300 for _, protocol := range protocols {
2301 expectedVersion := shimVers.version
2302 if runnerVers.version < shimVers.version {
2303 expectedVersion = runnerVers.version
2304 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002305
David Benjamin8b8c0062014-11-23 02:47:52 -05002306 suffix := shimVers.name + "-" + runnerVers.name
2307 if protocol == dtls {
2308 suffix += "-DTLS"
2309 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002310
David Benjamin1eb367c2014-12-12 18:17:51 -05002311 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
2312
David Benjamin1e29a6b2014-12-10 02:27:24 -05002313 clientVers := shimVers.version
2314 if clientVers > VersionTLS10 {
2315 clientVers = VersionTLS10
2316 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002317 testCases = append(testCases, testCase{
2318 protocol: protocol,
2319 testType: clientTest,
2320 name: "VersionNegotiation-Client-" + suffix,
2321 config: Config{
2322 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05002323 Bugs: ProtocolBugs{
2324 ExpectInitialRecordVersion: clientVers,
2325 },
David Benjamin8b8c0062014-11-23 02:47:52 -05002326 },
2327 flags: flags,
2328 expectedVersion: expectedVersion,
2329 })
David Benjamin1eb367c2014-12-12 18:17:51 -05002330 testCases = append(testCases, testCase{
2331 protocol: protocol,
2332 testType: clientTest,
2333 name: "VersionNegotiation-Client2-" + suffix,
2334 config: Config{
2335 MaxVersion: runnerVers.version,
2336 Bugs: ProtocolBugs{
2337 ExpectInitialRecordVersion: clientVers,
2338 },
2339 },
2340 flags: []string{"-max-version", shimVersFlag},
2341 expectedVersion: expectedVersion,
2342 })
David Benjamin8b8c0062014-11-23 02:47:52 -05002343
2344 testCases = append(testCases, testCase{
2345 protocol: protocol,
2346 testType: serverTest,
2347 name: "VersionNegotiation-Server-" + suffix,
2348 config: Config{
2349 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05002350 Bugs: ProtocolBugs{
2351 ExpectInitialRecordVersion: expectedVersion,
2352 },
David Benjamin8b8c0062014-11-23 02:47:52 -05002353 },
2354 flags: flags,
2355 expectedVersion: expectedVersion,
2356 })
David Benjamin1eb367c2014-12-12 18:17:51 -05002357 testCases = append(testCases, testCase{
2358 protocol: protocol,
2359 testType: serverTest,
2360 name: "VersionNegotiation-Server2-" + suffix,
2361 config: Config{
2362 MaxVersion: runnerVers.version,
2363 Bugs: ProtocolBugs{
2364 ExpectInitialRecordVersion: expectedVersion,
2365 },
2366 },
2367 flags: []string{"-max-version", shimVersFlag},
2368 expectedVersion: expectedVersion,
2369 })
David Benjamin8b8c0062014-11-23 02:47:52 -05002370 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002371 }
2372 }
2373}
2374
David Benjaminaccb4542014-12-12 23:44:33 -05002375func addMinimumVersionTests() {
2376 for i, shimVers := range tlsVersions {
2377 // Assemble flags to disable all older versions on the shim.
2378 var flags []string
2379 for _, vers := range tlsVersions[:i] {
2380 flags = append(flags, vers.flag)
2381 }
2382
2383 for _, runnerVers := range tlsVersions {
2384 protocols := []protocol{tls}
2385 if runnerVers.hasDTLS && shimVers.hasDTLS {
2386 protocols = append(protocols, dtls)
2387 }
2388 for _, protocol := range protocols {
2389 suffix := shimVers.name + "-" + runnerVers.name
2390 if protocol == dtls {
2391 suffix += "-DTLS"
2392 }
2393 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
2394
David Benjaminaccb4542014-12-12 23:44:33 -05002395 var expectedVersion uint16
2396 var shouldFail bool
2397 var expectedError string
David Benjamin87909c02014-12-13 01:55:01 -05002398 var expectedLocalError string
David Benjaminaccb4542014-12-12 23:44:33 -05002399 if runnerVers.version >= shimVers.version {
2400 expectedVersion = runnerVers.version
2401 } else {
2402 shouldFail = true
2403 expectedError = ":UNSUPPORTED_PROTOCOL:"
David Benjamin87909c02014-12-13 01:55:01 -05002404 if runnerVers.version > VersionSSL30 {
2405 expectedLocalError = "remote error: protocol version not supported"
2406 } else {
2407 expectedLocalError = "remote error: handshake failure"
2408 }
David Benjaminaccb4542014-12-12 23:44:33 -05002409 }
2410
2411 testCases = append(testCases, testCase{
2412 protocol: protocol,
2413 testType: clientTest,
2414 name: "MinimumVersion-Client-" + suffix,
2415 config: Config{
2416 MaxVersion: runnerVers.version,
2417 },
David Benjamin87909c02014-12-13 01:55:01 -05002418 flags: flags,
2419 expectedVersion: expectedVersion,
2420 shouldFail: shouldFail,
2421 expectedError: expectedError,
2422 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002423 })
2424 testCases = append(testCases, testCase{
2425 protocol: protocol,
2426 testType: clientTest,
2427 name: "MinimumVersion-Client2-" + suffix,
2428 config: Config{
2429 MaxVersion: runnerVers.version,
2430 },
David Benjamin87909c02014-12-13 01:55:01 -05002431 flags: []string{"-min-version", shimVersFlag},
2432 expectedVersion: expectedVersion,
2433 shouldFail: shouldFail,
2434 expectedError: expectedError,
2435 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002436 })
2437
2438 testCases = append(testCases, testCase{
2439 protocol: protocol,
2440 testType: serverTest,
2441 name: "MinimumVersion-Server-" + suffix,
2442 config: Config{
2443 MaxVersion: runnerVers.version,
2444 },
David Benjamin87909c02014-12-13 01:55:01 -05002445 flags: flags,
2446 expectedVersion: expectedVersion,
2447 shouldFail: shouldFail,
2448 expectedError: expectedError,
2449 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002450 })
2451 testCases = append(testCases, testCase{
2452 protocol: protocol,
2453 testType: serverTest,
2454 name: "MinimumVersion-Server2-" + suffix,
2455 config: Config{
2456 MaxVersion: runnerVers.version,
2457 },
David Benjamin87909c02014-12-13 01:55:01 -05002458 flags: []string{"-min-version", shimVersFlag},
2459 expectedVersion: expectedVersion,
2460 shouldFail: shouldFail,
2461 expectedError: expectedError,
2462 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002463 })
2464 }
2465 }
2466 }
2467}
2468
David Benjamin5c24a1d2014-08-31 00:59:27 -04002469func addD5BugTests() {
2470 testCases = append(testCases, testCase{
2471 testType: serverTest,
2472 name: "D5Bug-NoQuirk-Reject",
2473 config: Config{
2474 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2475 Bugs: ProtocolBugs{
2476 SSL3RSAKeyExchange: true,
2477 },
2478 },
2479 shouldFail: true,
2480 expectedError: ":TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG:",
2481 })
2482 testCases = append(testCases, testCase{
2483 testType: serverTest,
2484 name: "D5Bug-Quirk-Normal",
2485 config: Config{
2486 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2487 },
2488 flags: []string{"-tls-d5-bug"},
2489 })
2490 testCases = append(testCases, testCase{
2491 testType: serverTest,
2492 name: "D5Bug-Quirk-Bug",
2493 config: Config{
2494 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2495 Bugs: ProtocolBugs{
2496 SSL3RSAKeyExchange: true,
2497 },
2498 },
2499 flags: []string{"-tls-d5-bug"},
2500 })
2501}
2502
David Benjamine78bfde2014-09-06 12:45:15 -04002503func addExtensionTests() {
2504 testCases = append(testCases, testCase{
2505 testType: clientTest,
2506 name: "DuplicateExtensionClient",
2507 config: Config{
2508 Bugs: ProtocolBugs{
2509 DuplicateExtension: true,
2510 },
2511 },
2512 shouldFail: true,
2513 expectedLocalError: "remote error: error decoding message",
2514 })
2515 testCases = append(testCases, testCase{
2516 testType: serverTest,
2517 name: "DuplicateExtensionServer",
2518 config: Config{
2519 Bugs: ProtocolBugs{
2520 DuplicateExtension: true,
2521 },
2522 },
2523 shouldFail: true,
2524 expectedLocalError: "remote error: error decoding message",
2525 })
2526 testCases = append(testCases, testCase{
2527 testType: clientTest,
2528 name: "ServerNameExtensionClient",
2529 config: Config{
2530 Bugs: ProtocolBugs{
2531 ExpectServerName: "example.com",
2532 },
2533 },
2534 flags: []string{"-host-name", "example.com"},
2535 })
2536 testCases = append(testCases, testCase{
2537 testType: clientTest,
David Benjamin5f237bc2015-02-11 17:14:15 -05002538 name: "ServerNameExtensionClientMismatch",
David Benjamine78bfde2014-09-06 12:45:15 -04002539 config: Config{
2540 Bugs: ProtocolBugs{
2541 ExpectServerName: "mismatch.com",
2542 },
2543 },
2544 flags: []string{"-host-name", "example.com"},
2545 shouldFail: true,
2546 expectedLocalError: "tls: unexpected server name",
2547 })
2548 testCases = append(testCases, testCase{
2549 testType: clientTest,
David Benjamin5f237bc2015-02-11 17:14:15 -05002550 name: "ServerNameExtensionClientMissing",
David Benjamine78bfde2014-09-06 12:45:15 -04002551 config: Config{
2552 Bugs: ProtocolBugs{
2553 ExpectServerName: "missing.com",
2554 },
2555 },
2556 shouldFail: true,
2557 expectedLocalError: "tls: unexpected server name",
2558 })
2559 testCases = append(testCases, testCase{
2560 testType: serverTest,
2561 name: "ServerNameExtensionServer",
2562 config: Config{
2563 ServerName: "example.com",
2564 },
2565 flags: []string{"-expect-server-name", "example.com"},
2566 resumeSession: true,
2567 })
David Benjaminae2888f2014-09-06 12:58:58 -04002568 testCases = append(testCases, testCase{
2569 testType: clientTest,
2570 name: "ALPNClient",
2571 config: Config{
2572 NextProtos: []string{"foo"},
2573 },
2574 flags: []string{
2575 "-advertise-alpn", "\x03foo\x03bar\x03baz",
2576 "-expect-alpn", "foo",
2577 },
David Benjaminfc7b0862014-09-06 13:21:53 -04002578 expectedNextProto: "foo",
2579 expectedNextProtoType: alpn,
2580 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04002581 })
2582 testCases = append(testCases, testCase{
2583 testType: serverTest,
2584 name: "ALPNServer",
2585 config: Config{
2586 NextProtos: []string{"foo", "bar", "baz"},
2587 },
2588 flags: []string{
2589 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2590 "-select-alpn", "foo",
2591 },
David Benjaminfc7b0862014-09-06 13:21:53 -04002592 expectedNextProto: "foo",
2593 expectedNextProtoType: alpn,
2594 resumeSession: true,
2595 })
2596 // Test that the server prefers ALPN over NPN.
2597 testCases = append(testCases, testCase{
2598 testType: serverTest,
2599 name: "ALPNServer-Preferred",
2600 config: Config{
2601 NextProtos: []string{"foo", "bar", "baz"},
2602 },
2603 flags: []string{
2604 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2605 "-select-alpn", "foo",
2606 "-advertise-npn", "\x03foo\x03bar\x03baz",
2607 },
2608 expectedNextProto: "foo",
2609 expectedNextProtoType: alpn,
2610 resumeSession: true,
2611 })
2612 testCases = append(testCases, testCase{
2613 testType: serverTest,
2614 name: "ALPNServer-Preferred-Swapped",
2615 config: Config{
2616 NextProtos: []string{"foo", "bar", "baz"},
2617 Bugs: ProtocolBugs{
2618 SwapNPNAndALPN: true,
2619 },
2620 },
2621 flags: []string{
2622 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2623 "-select-alpn", "foo",
2624 "-advertise-npn", "\x03foo\x03bar\x03baz",
2625 },
2626 expectedNextProto: "foo",
2627 expectedNextProtoType: alpn,
2628 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04002629 })
Adam Langley38311732014-10-16 19:04:35 -07002630 // Resume with a corrupt ticket.
2631 testCases = append(testCases, testCase{
2632 testType: serverTest,
2633 name: "CorruptTicket",
2634 config: Config{
2635 Bugs: ProtocolBugs{
2636 CorruptTicket: true,
2637 },
2638 },
Adam Langleyb0eef0a2015-06-02 10:47:39 -07002639 resumeSession: true,
2640 expectResumeRejected: true,
Adam Langley38311732014-10-16 19:04:35 -07002641 })
2642 // Resume with an oversized session id.
2643 testCases = append(testCases, testCase{
2644 testType: serverTest,
2645 name: "OversizedSessionId",
2646 config: Config{
2647 Bugs: ProtocolBugs{
2648 OversizedSessionId: true,
2649 },
2650 },
2651 resumeSession: true,
Adam Langley75712922014-10-10 16:23:43 -07002652 shouldFail: true,
Adam Langley38311732014-10-16 19:04:35 -07002653 expectedError: ":DECODE_ERROR:",
2654 })
David Benjaminca6c8262014-11-15 19:06:08 -05002655 // Basic DTLS-SRTP tests. Include fake profiles to ensure they
2656 // are ignored.
2657 testCases = append(testCases, testCase{
2658 protocol: dtls,
2659 name: "SRTP-Client",
2660 config: Config{
2661 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
2662 },
2663 flags: []string{
2664 "-srtp-profiles",
2665 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2666 },
2667 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2668 })
2669 testCases = append(testCases, testCase{
2670 protocol: dtls,
2671 testType: serverTest,
2672 name: "SRTP-Server",
2673 config: Config{
2674 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
2675 },
2676 flags: []string{
2677 "-srtp-profiles",
2678 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2679 },
2680 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2681 })
2682 // Test that the MKI is ignored.
2683 testCases = append(testCases, testCase{
2684 protocol: dtls,
2685 testType: serverTest,
2686 name: "SRTP-Server-IgnoreMKI",
2687 config: Config{
2688 SRTPProtectionProfiles: []uint16{SRTP_AES128_CM_HMAC_SHA1_80},
2689 Bugs: ProtocolBugs{
2690 SRTPMasterKeyIdentifer: "bogus",
2691 },
2692 },
2693 flags: []string{
2694 "-srtp-profiles",
2695 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2696 },
2697 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2698 })
2699 // Test that SRTP isn't negotiated on the server if there were
2700 // no matching profiles.
2701 testCases = append(testCases, testCase{
2702 protocol: dtls,
2703 testType: serverTest,
2704 name: "SRTP-Server-NoMatch",
2705 config: Config{
2706 SRTPProtectionProfiles: []uint16{100, 101, 102},
2707 },
2708 flags: []string{
2709 "-srtp-profiles",
2710 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2711 },
2712 expectedSRTPProtectionProfile: 0,
2713 })
2714 // Test that the server returning an invalid SRTP profile is
2715 // flagged as an error by the client.
2716 testCases = append(testCases, testCase{
2717 protocol: dtls,
2718 name: "SRTP-Client-NoMatch",
2719 config: Config{
2720 Bugs: ProtocolBugs{
2721 SendSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_32,
2722 },
2723 },
2724 flags: []string{
2725 "-srtp-profiles",
2726 "SRTP_AES128_CM_SHA1_80",
2727 },
2728 shouldFail: true,
2729 expectedError: ":BAD_SRTP_PROTECTION_PROFILE_LIST:",
2730 })
David Benjamin61f95272014-11-25 01:55:35 -05002731 // Test OCSP stapling and SCT list.
2732 testCases = append(testCases, testCase{
2733 name: "OCSPStapling",
2734 flags: []string{
2735 "-enable-ocsp-stapling",
2736 "-expect-ocsp-response",
2737 base64.StdEncoding.EncodeToString(testOCSPResponse),
2738 },
2739 })
2740 testCases = append(testCases, testCase{
2741 name: "SignedCertificateTimestampList",
2742 flags: []string{
2743 "-enable-signed-cert-timestamps",
2744 "-expect-signed-cert-timestamps",
2745 base64.StdEncoding.EncodeToString(testSCTList),
2746 },
2747 })
David Benjamine78bfde2014-09-06 12:45:15 -04002748}
2749
David Benjamin01fe8202014-09-24 15:21:44 -04002750func addResumptionVersionTests() {
David Benjamin01fe8202014-09-24 15:21:44 -04002751 for _, sessionVers := range tlsVersions {
David Benjamin01fe8202014-09-24 15:21:44 -04002752 for _, resumeVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05002753 protocols := []protocol{tls}
2754 if sessionVers.hasDTLS && resumeVers.hasDTLS {
2755 protocols = append(protocols, dtls)
David Benjaminbdf5e722014-11-11 00:52:15 -05002756 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002757 for _, protocol := range protocols {
2758 suffix := "-" + sessionVers.name + "-" + resumeVers.name
2759 if protocol == dtls {
2760 suffix += "-DTLS"
2761 }
2762
David Benjaminece3de92015-03-16 18:02:20 -04002763 if sessionVers.version == resumeVers.version {
2764 testCases = append(testCases, testCase{
2765 protocol: protocol,
2766 name: "Resume-Client" + suffix,
2767 resumeSession: true,
2768 config: Config{
2769 MaxVersion: sessionVers.version,
2770 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
David Benjamin8b8c0062014-11-23 02:47:52 -05002771 },
David Benjaminece3de92015-03-16 18:02:20 -04002772 expectedVersion: sessionVers.version,
2773 expectedResumeVersion: resumeVers.version,
2774 })
2775 } else {
2776 testCases = append(testCases, testCase{
2777 protocol: protocol,
2778 name: "Resume-Client-Mismatch" + suffix,
2779 resumeSession: true,
2780 config: Config{
2781 MaxVersion: sessionVers.version,
2782 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
David Benjamin8b8c0062014-11-23 02:47:52 -05002783 },
David Benjaminece3de92015-03-16 18:02:20 -04002784 expectedVersion: sessionVers.version,
2785 resumeConfig: &Config{
2786 MaxVersion: resumeVers.version,
2787 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2788 Bugs: ProtocolBugs{
2789 AllowSessionVersionMismatch: true,
2790 },
2791 },
2792 expectedResumeVersion: resumeVers.version,
2793 shouldFail: true,
2794 expectedError: ":OLD_SESSION_VERSION_NOT_RETURNED:",
2795 })
2796 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002797
2798 testCases = append(testCases, testCase{
2799 protocol: protocol,
2800 name: "Resume-Client-NoResume" + suffix,
David Benjamin8b8c0062014-11-23 02:47:52 -05002801 resumeSession: true,
2802 config: Config{
2803 MaxVersion: sessionVers.version,
2804 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2805 },
2806 expectedVersion: sessionVers.version,
2807 resumeConfig: &Config{
2808 MaxVersion: resumeVers.version,
2809 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2810 },
2811 newSessionsOnResume: true,
Adam Langleyb0eef0a2015-06-02 10:47:39 -07002812 expectResumeRejected: true,
David Benjamin8b8c0062014-11-23 02:47:52 -05002813 expectedResumeVersion: resumeVers.version,
2814 })
2815
David Benjamin8b8c0062014-11-23 02:47:52 -05002816 testCases = append(testCases, testCase{
2817 protocol: protocol,
2818 testType: serverTest,
2819 name: "Resume-Server" + suffix,
David Benjamin8b8c0062014-11-23 02:47:52 -05002820 resumeSession: true,
2821 config: Config{
2822 MaxVersion: sessionVers.version,
2823 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2824 },
Adam Langleyb0eef0a2015-06-02 10:47:39 -07002825 expectedVersion: sessionVers.version,
2826 expectResumeRejected: sessionVers.version != resumeVers.version,
David Benjamin8b8c0062014-11-23 02:47:52 -05002827 resumeConfig: &Config{
2828 MaxVersion: resumeVers.version,
2829 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2830 },
2831 expectedResumeVersion: resumeVers.version,
2832 })
2833 }
David Benjamin01fe8202014-09-24 15:21:44 -04002834 }
2835 }
David Benjaminece3de92015-03-16 18:02:20 -04002836
2837 testCases = append(testCases, testCase{
2838 name: "Resume-Client-CipherMismatch",
2839 resumeSession: true,
2840 config: Config{
2841 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2842 },
2843 resumeConfig: &Config{
2844 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2845 Bugs: ProtocolBugs{
2846 SendCipherSuite: TLS_RSA_WITH_AES_128_CBC_SHA,
2847 },
2848 },
2849 shouldFail: true,
2850 expectedError: ":OLD_SESSION_CIPHER_NOT_RETURNED:",
2851 })
David Benjamin01fe8202014-09-24 15:21:44 -04002852}
2853
Adam Langley2ae77d22014-10-28 17:29:33 -07002854func addRenegotiationTests() {
David Benjamin44d3eed2015-05-21 01:29:55 -04002855 // Servers cannot renegotiate.
David Benjaminb16346b2015-04-08 19:16:58 -04002856 testCases = append(testCases, testCase{
2857 testType: serverTest,
David Benjamin44d3eed2015-05-21 01:29:55 -04002858 name: "Renegotiate-Server-Forbidden",
David Benjaminb16346b2015-04-08 19:16:58 -04002859 renegotiate: true,
2860 flags: []string{"-reject-peer-renegotiations"},
2861 shouldFail: true,
2862 expectedError: ":NO_RENEGOTIATION:",
2863 expectedLocalError: "remote error: no renegotiation",
2864 })
Adam Langley2ae77d22014-10-28 17:29:33 -07002865 // TODO(agl): test the renegotiation info SCSV.
Adam Langleycf2d4f42014-10-28 19:06:14 -07002866 testCases = append(testCases, testCase{
David Benjamin4b27d9f2015-05-12 22:42:52 -04002867 name: "Renegotiate-Client",
David Benjamincdea40c2015-03-19 14:09:43 -04002868 config: Config{
2869 Bugs: ProtocolBugs{
David Benjamin4b27d9f2015-05-12 22:42:52 -04002870 FailIfResumeOnRenego: true,
David Benjamincdea40c2015-03-19 14:09:43 -04002871 },
2872 },
2873 renegotiate: true,
2874 })
2875 testCases = append(testCases, testCase{
Adam Langleycf2d4f42014-10-28 19:06:14 -07002876 name: "Renegotiate-Client-EmptyExt",
2877 renegotiate: true,
2878 config: Config{
2879 Bugs: ProtocolBugs{
2880 EmptyRenegotiationInfo: true,
2881 },
2882 },
2883 shouldFail: true,
2884 expectedError: ":RENEGOTIATION_MISMATCH:",
2885 })
2886 testCases = append(testCases, testCase{
2887 name: "Renegotiate-Client-BadExt",
2888 renegotiate: true,
2889 config: Config{
2890 Bugs: ProtocolBugs{
2891 BadRenegotiationInfo: true,
2892 },
2893 },
2894 shouldFail: true,
2895 expectedError: ":RENEGOTIATION_MISMATCH:",
2896 })
2897 testCases = append(testCases, testCase{
David Benjamincff0b902015-05-15 23:09:47 -04002898 name: "Renegotiate-Client-NoExt",
2899 renegotiate: true,
2900 config: Config{
2901 Bugs: ProtocolBugs{
2902 NoRenegotiationInfo: true,
2903 },
2904 },
2905 shouldFail: true,
2906 expectedError: ":UNSAFE_LEGACY_RENEGOTIATION_DISABLED:",
2907 flags: []string{"-no-legacy-server-connect"},
2908 })
2909 testCases = append(testCases, testCase{
2910 name: "Renegotiate-Client-NoExt-Allowed",
2911 renegotiate: true,
2912 config: Config{
2913 Bugs: ProtocolBugs{
2914 NoRenegotiationInfo: true,
2915 },
2916 },
2917 })
2918 testCases = append(testCases, testCase{
Adam Langleycf2d4f42014-10-28 19:06:14 -07002919 name: "Renegotiate-Client-SwitchCiphers",
2920 renegotiate: true,
2921 config: Config{
2922 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2923 },
2924 renegotiateCiphers: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2925 })
2926 testCases = append(testCases, testCase{
2927 name: "Renegotiate-Client-SwitchCiphers2",
2928 renegotiate: true,
2929 config: Config{
2930 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2931 },
2932 renegotiateCiphers: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2933 })
David Benjaminc44b1df2014-11-23 12:11:01 -05002934 testCases = append(testCases, testCase{
David Benjaminb16346b2015-04-08 19:16:58 -04002935 name: "Renegotiate-Client-Forbidden",
2936 renegotiate: true,
2937 flags: []string{"-reject-peer-renegotiations"},
2938 shouldFail: true,
2939 expectedError: ":NO_RENEGOTIATION:",
2940 expectedLocalError: "remote error: no renegotiation",
2941 })
2942 testCases = append(testCases, testCase{
David Benjaminc44b1df2014-11-23 12:11:01 -05002943 name: "Renegotiate-SameClientVersion",
2944 renegotiate: true,
2945 config: Config{
2946 MaxVersion: VersionTLS10,
2947 Bugs: ProtocolBugs{
2948 RequireSameRenegoClientVersion: true,
2949 },
2950 },
2951 })
Adam Langley2ae77d22014-10-28 17:29:33 -07002952}
2953
David Benjamin5e961c12014-11-07 01:48:35 -05002954func addDTLSReplayTests() {
2955 // Test that sequence number replays are detected.
2956 testCases = append(testCases, testCase{
2957 protocol: dtls,
2958 name: "DTLS-Replay",
2959 replayWrites: true,
2960 })
2961
2962 // Test the outgoing sequence number skipping by values larger
2963 // than the retransmit window.
2964 testCases = append(testCases, testCase{
2965 protocol: dtls,
2966 name: "DTLS-Replay-LargeGaps",
2967 config: Config{
2968 Bugs: ProtocolBugs{
2969 SequenceNumberIncrement: 127,
2970 },
2971 },
2972 replayWrites: true,
2973 })
2974}
2975
Feng Lu41aa3252014-11-21 22:47:56 -08002976func addFastRadioPaddingTests() {
David Benjamin1e29a6b2014-12-10 02:27:24 -05002977 testCases = append(testCases, testCase{
2978 protocol: tls,
2979 name: "FastRadio-Padding",
Feng Lu41aa3252014-11-21 22:47:56 -08002980 config: Config{
2981 Bugs: ProtocolBugs{
2982 RequireFastradioPadding: true,
2983 },
2984 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05002985 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08002986 })
David Benjamin1e29a6b2014-12-10 02:27:24 -05002987 testCases = append(testCases, testCase{
2988 protocol: dtls,
David Benjamin5f237bc2015-02-11 17:14:15 -05002989 name: "FastRadio-Padding-DTLS",
Feng Lu41aa3252014-11-21 22:47:56 -08002990 config: Config{
2991 Bugs: ProtocolBugs{
2992 RequireFastradioPadding: true,
2993 },
2994 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05002995 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08002996 })
2997}
2998
David Benjamin000800a2014-11-14 01:43:59 -05002999var testHashes = []struct {
3000 name string
3001 id uint8
3002}{
3003 {"SHA1", hashSHA1},
3004 {"SHA224", hashSHA224},
3005 {"SHA256", hashSHA256},
3006 {"SHA384", hashSHA384},
3007 {"SHA512", hashSHA512},
3008}
3009
3010func addSigningHashTests() {
3011 // Make sure each hash works. Include some fake hashes in the list and
3012 // ensure they're ignored.
3013 for _, hash := range testHashes {
3014 testCases = append(testCases, testCase{
3015 name: "SigningHash-ClientAuth-" + hash.name,
3016 config: Config{
3017 ClientAuth: RequireAnyClientCert,
3018 SignatureAndHashes: []signatureAndHash{
3019 {signatureRSA, 42},
3020 {signatureRSA, hash.id},
3021 {signatureRSA, 255},
3022 },
3023 },
3024 flags: []string{
3025 "-cert-file", rsaCertificateFile,
3026 "-key-file", rsaKeyFile,
3027 },
3028 })
3029
3030 testCases = append(testCases, testCase{
3031 testType: serverTest,
3032 name: "SigningHash-ServerKeyExchange-Sign-" + hash.name,
3033 config: Config{
3034 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
3035 SignatureAndHashes: []signatureAndHash{
3036 {signatureRSA, 42},
3037 {signatureRSA, hash.id},
3038 {signatureRSA, 255},
3039 },
3040 },
3041 })
3042 }
3043
3044 // Test that hash resolution takes the signature type into account.
3045 testCases = append(testCases, testCase{
3046 name: "SigningHash-ClientAuth-SignatureType",
3047 config: Config{
3048 ClientAuth: RequireAnyClientCert,
3049 SignatureAndHashes: []signatureAndHash{
3050 {signatureECDSA, hashSHA512},
3051 {signatureRSA, hashSHA384},
3052 {signatureECDSA, hashSHA1},
3053 },
3054 },
3055 flags: []string{
3056 "-cert-file", rsaCertificateFile,
3057 "-key-file", rsaKeyFile,
3058 },
3059 })
3060
3061 testCases = append(testCases, testCase{
3062 testType: serverTest,
3063 name: "SigningHash-ServerKeyExchange-SignatureType",
3064 config: Config{
3065 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
3066 SignatureAndHashes: []signatureAndHash{
3067 {signatureECDSA, hashSHA512},
3068 {signatureRSA, hashSHA384},
3069 {signatureECDSA, hashSHA1},
3070 },
3071 },
3072 })
3073
3074 // Test that, if the list is missing, the peer falls back to SHA-1.
3075 testCases = append(testCases, testCase{
3076 name: "SigningHash-ClientAuth-Fallback",
3077 config: Config{
3078 ClientAuth: RequireAnyClientCert,
3079 SignatureAndHashes: []signatureAndHash{
3080 {signatureRSA, hashSHA1},
3081 },
3082 Bugs: ProtocolBugs{
3083 NoSignatureAndHashes: true,
3084 },
3085 },
3086 flags: []string{
3087 "-cert-file", rsaCertificateFile,
3088 "-key-file", rsaKeyFile,
3089 },
3090 })
3091
3092 testCases = append(testCases, testCase{
3093 testType: serverTest,
3094 name: "SigningHash-ServerKeyExchange-Fallback",
3095 config: Config{
3096 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
3097 SignatureAndHashes: []signatureAndHash{
3098 {signatureRSA, hashSHA1},
3099 },
3100 Bugs: ProtocolBugs{
3101 NoSignatureAndHashes: true,
3102 },
3103 },
3104 })
David Benjamin72dc7832015-03-16 17:49:43 -04003105
3106 // Test that hash preferences are enforced. BoringSSL defaults to
3107 // rejecting MD5 signatures.
3108 testCases = append(testCases, testCase{
3109 testType: serverTest,
3110 name: "SigningHash-ClientAuth-Enforced",
3111 config: Config{
3112 Certificates: []Certificate{rsaCertificate},
3113 SignatureAndHashes: []signatureAndHash{
3114 {signatureRSA, hashMD5},
3115 // Advertise SHA-1 so the handshake will
3116 // proceed, but the shim's preferences will be
3117 // ignored in CertificateVerify generation, so
3118 // MD5 will be chosen.
3119 {signatureRSA, hashSHA1},
3120 },
3121 Bugs: ProtocolBugs{
3122 IgnorePeerSignatureAlgorithmPreferences: true,
3123 },
3124 },
3125 flags: []string{"-require-any-client-certificate"},
3126 shouldFail: true,
3127 expectedError: ":WRONG_SIGNATURE_TYPE:",
3128 })
3129
3130 testCases = append(testCases, testCase{
3131 name: "SigningHash-ServerKeyExchange-Enforced",
3132 config: Config{
3133 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
3134 SignatureAndHashes: []signatureAndHash{
3135 {signatureRSA, hashMD5},
3136 },
3137 Bugs: ProtocolBugs{
3138 IgnorePeerSignatureAlgorithmPreferences: true,
3139 },
3140 },
3141 shouldFail: true,
3142 expectedError: ":WRONG_SIGNATURE_TYPE:",
3143 })
David Benjamin000800a2014-11-14 01:43:59 -05003144}
3145
David Benjamin83f90402015-01-27 01:09:43 -05003146// timeouts is the retransmit schedule for BoringSSL. It doubles and
3147// caps at 60 seconds. On the 13th timeout, it gives up.
3148var timeouts = []time.Duration{
3149 1 * time.Second,
3150 2 * time.Second,
3151 4 * time.Second,
3152 8 * time.Second,
3153 16 * time.Second,
3154 32 * time.Second,
3155 60 * time.Second,
3156 60 * time.Second,
3157 60 * time.Second,
3158 60 * time.Second,
3159 60 * time.Second,
3160 60 * time.Second,
3161 60 * time.Second,
3162}
3163
3164func addDTLSRetransmitTests() {
3165 // Test that this is indeed the timeout schedule. Stress all
3166 // four patterns of handshake.
3167 for i := 1; i < len(timeouts); i++ {
3168 number := strconv.Itoa(i)
3169 testCases = append(testCases, testCase{
3170 protocol: dtls,
3171 name: "DTLS-Retransmit-Client-" + number,
3172 config: Config{
3173 Bugs: ProtocolBugs{
3174 TimeoutSchedule: timeouts[:i],
3175 },
3176 },
3177 resumeSession: true,
3178 flags: []string{"-async"},
3179 })
3180 testCases = append(testCases, testCase{
3181 protocol: dtls,
3182 testType: serverTest,
3183 name: "DTLS-Retransmit-Server-" + number,
3184 config: Config{
3185 Bugs: ProtocolBugs{
3186 TimeoutSchedule: timeouts[:i],
3187 },
3188 },
3189 resumeSession: true,
3190 flags: []string{"-async"},
3191 })
3192 }
3193
3194 // Test that exceeding the timeout schedule hits a read
3195 // timeout.
3196 testCases = append(testCases, testCase{
3197 protocol: dtls,
3198 name: "DTLS-Retransmit-Timeout",
3199 config: Config{
3200 Bugs: ProtocolBugs{
3201 TimeoutSchedule: timeouts,
3202 },
3203 },
3204 resumeSession: true,
3205 flags: []string{"-async"},
3206 shouldFail: true,
3207 expectedError: ":READ_TIMEOUT_EXPIRED:",
3208 })
3209
3210 // Test that timeout handling has a fudge factor, due to API
3211 // problems.
3212 testCases = append(testCases, testCase{
3213 protocol: dtls,
3214 name: "DTLS-Retransmit-Fudge",
3215 config: Config{
3216 Bugs: ProtocolBugs{
3217 TimeoutSchedule: []time.Duration{
3218 timeouts[0] - 10*time.Millisecond,
3219 },
3220 },
3221 },
3222 resumeSession: true,
3223 flags: []string{"-async"},
3224 })
David Benjamin7eaab4c2015-03-02 19:01:16 -05003225
3226 // Test that the final Finished retransmitting isn't
3227 // duplicated if the peer badly fragments everything.
3228 testCases = append(testCases, testCase{
3229 testType: serverTest,
3230 protocol: dtls,
3231 name: "DTLS-Retransmit-Fragmented",
3232 config: Config{
3233 Bugs: ProtocolBugs{
3234 TimeoutSchedule: []time.Duration{timeouts[0]},
3235 MaxHandshakeRecordLength: 2,
3236 },
3237 },
3238 flags: []string{"-async"},
3239 })
David Benjamin83f90402015-01-27 01:09:43 -05003240}
3241
David Benjaminc565ebb2015-04-03 04:06:36 -04003242func addExportKeyingMaterialTests() {
3243 for _, vers := range tlsVersions {
3244 if vers.version == VersionSSL30 {
3245 continue
3246 }
3247 testCases = append(testCases, testCase{
3248 name: "ExportKeyingMaterial-" + vers.name,
3249 config: Config{
3250 MaxVersion: vers.version,
3251 },
3252 exportKeyingMaterial: 1024,
3253 exportLabel: "label",
3254 exportContext: "context",
3255 useExportContext: true,
3256 })
3257 testCases = append(testCases, testCase{
3258 name: "ExportKeyingMaterial-NoContext-" + vers.name,
3259 config: Config{
3260 MaxVersion: vers.version,
3261 },
3262 exportKeyingMaterial: 1024,
3263 })
3264 testCases = append(testCases, testCase{
3265 name: "ExportKeyingMaterial-EmptyContext-" + vers.name,
3266 config: Config{
3267 MaxVersion: vers.version,
3268 },
3269 exportKeyingMaterial: 1024,
3270 useExportContext: true,
3271 })
3272 testCases = append(testCases, testCase{
3273 name: "ExportKeyingMaterial-Small-" + vers.name,
3274 config: Config{
3275 MaxVersion: vers.version,
3276 },
3277 exportKeyingMaterial: 1,
3278 exportLabel: "label",
3279 exportContext: "context",
3280 useExportContext: true,
3281 })
3282 }
3283 testCases = append(testCases, testCase{
3284 name: "ExportKeyingMaterial-SSL3",
3285 config: Config{
3286 MaxVersion: VersionSSL30,
3287 },
3288 exportKeyingMaterial: 1024,
3289 exportLabel: "label",
3290 exportContext: "context",
3291 useExportContext: true,
3292 shouldFail: true,
3293 expectedError: "failed to export keying material",
3294 })
3295}
3296
David Benjamin884fdf12014-08-02 15:28:23 -04003297func worker(statusChan chan statusMsg, c chan *testCase, buildDir string, wg *sync.WaitGroup) {
Adam Langley95c29f32014-06-20 12:00:00 -07003298 defer wg.Done()
3299
3300 for test := range c {
Adam Langley69a01602014-11-17 17:26:55 -08003301 var err error
3302
3303 if *mallocTest < 0 {
3304 statusChan <- statusMsg{test: test, started: true}
3305 err = runTest(test, buildDir, -1)
3306 } else {
3307 for mallocNumToFail := int64(*mallocTest); ; mallocNumToFail++ {
3308 statusChan <- statusMsg{test: test, started: true}
3309 if err = runTest(test, buildDir, mallocNumToFail); err != errMoreMallocs {
3310 if err != nil {
3311 fmt.Printf("\n\nmalloc test failed at %d: %s\n", mallocNumToFail, err)
3312 }
3313 break
3314 }
3315 }
3316 }
Adam Langley95c29f32014-06-20 12:00:00 -07003317 statusChan <- statusMsg{test: test, err: err}
3318 }
3319}
3320
3321type statusMsg struct {
3322 test *testCase
3323 started bool
3324 err error
3325}
3326
David Benjamin5f237bc2015-02-11 17:14:15 -05003327func statusPrinter(doneChan chan *testOutput, statusChan chan statusMsg, total int) {
Adam Langley95c29f32014-06-20 12:00:00 -07003328 var started, done, failed, lineLen int
Adam Langley95c29f32014-06-20 12:00:00 -07003329
David Benjamin5f237bc2015-02-11 17:14:15 -05003330 testOutput := newTestOutput()
Adam Langley95c29f32014-06-20 12:00:00 -07003331 for msg := range statusChan {
David Benjamin5f237bc2015-02-11 17:14:15 -05003332 if !*pipe {
3333 // Erase the previous status line.
David Benjamin87c8a642015-02-21 01:54:29 -05003334 var erase string
3335 for i := 0; i < lineLen; i++ {
3336 erase += "\b \b"
3337 }
3338 fmt.Print(erase)
David Benjamin5f237bc2015-02-11 17:14:15 -05003339 }
3340
Adam Langley95c29f32014-06-20 12:00:00 -07003341 if msg.started {
3342 started++
3343 } else {
3344 done++
David Benjamin5f237bc2015-02-11 17:14:15 -05003345
3346 if msg.err != nil {
3347 fmt.Printf("FAILED (%s)\n%s\n", msg.test.name, msg.err)
3348 failed++
3349 testOutput.addResult(msg.test.name, "FAIL")
3350 } else {
3351 if *pipe {
3352 // Print each test instead of a status line.
3353 fmt.Printf("PASSED (%s)\n", msg.test.name)
3354 }
3355 testOutput.addResult(msg.test.name, "PASS")
3356 }
Adam Langley95c29f32014-06-20 12:00:00 -07003357 }
3358
David Benjamin5f237bc2015-02-11 17:14:15 -05003359 if !*pipe {
3360 // Print a new status line.
3361 line := fmt.Sprintf("%d/%d/%d/%d", failed, done, started, total)
3362 lineLen = len(line)
3363 os.Stdout.WriteString(line)
Adam Langley95c29f32014-06-20 12:00:00 -07003364 }
Adam Langley95c29f32014-06-20 12:00:00 -07003365 }
David Benjamin5f237bc2015-02-11 17:14:15 -05003366
3367 doneChan <- testOutput
Adam Langley95c29f32014-06-20 12:00:00 -07003368}
3369
3370func main() {
3371 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 -04003372 var flagNumWorkers *int = flag.Int("num-workers", runtime.NumCPU(), "The number of workers to run in parallel.")
David Benjamin884fdf12014-08-02 15:28:23 -04003373 var flagBuildDir *string = flag.String("build-dir", "../../../build", "The build directory to run the shim from.")
Adam Langley95c29f32014-06-20 12:00:00 -07003374
3375 flag.Parse()
3376
3377 addCipherSuiteTests()
3378 addBadECDSASignatureTests()
Adam Langley80842bd2014-06-20 12:00:00 -07003379 addCBCPaddingTests()
Kenny Root7fdeaf12014-08-05 15:23:37 -07003380 addCBCSplittingTests()
David Benjamin636293b2014-07-08 17:59:18 -04003381 addClientAuthTests()
Adam Langley524e7172015-02-20 16:04:00 -08003382 addDDoSCallbackTests()
David Benjamin7e2e6cf2014-08-07 17:44:24 -04003383 addVersionNegotiationTests()
David Benjaminaccb4542014-12-12 23:44:33 -05003384 addMinimumVersionTests()
David Benjamin5c24a1d2014-08-31 00:59:27 -04003385 addD5BugTests()
David Benjamine78bfde2014-09-06 12:45:15 -04003386 addExtensionTests()
David Benjamin01fe8202014-09-24 15:21:44 -04003387 addResumptionVersionTests()
Adam Langley75712922014-10-10 16:23:43 -07003388 addExtendedMasterSecretTests()
Adam Langley2ae77d22014-10-28 17:29:33 -07003389 addRenegotiationTests()
David Benjamin5e961c12014-11-07 01:48:35 -05003390 addDTLSReplayTests()
David Benjamin000800a2014-11-14 01:43:59 -05003391 addSigningHashTests()
Feng Lu41aa3252014-11-21 22:47:56 -08003392 addFastRadioPaddingTests()
David Benjamin83f90402015-01-27 01:09:43 -05003393 addDTLSRetransmitTests()
David Benjaminc565ebb2015-04-03 04:06:36 -04003394 addExportKeyingMaterialTests()
David Benjamin43ec06f2014-08-05 02:28:57 -04003395 for _, async := range []bool{false, true} {
3396 for _, splitHandshake := range []bool{false, true} {
David Benjamin6fd297b2014-08-11 18:43:38 -04003397 for _, protocol := range []protocol{tls, dtls} {
3398 addStateMachineCoverageTests(async, splitHandshake, protocol)
3399 }
David Benjamin43ec06f2014-08-05 02:28:57 -04003400 }
3401 }
Adam Langley95c29f32014-06-20 12:00:00 -07003402
3403 var wg sync.WaitGroup
3404
David Benjamin2bc8e6f2014-08-02 15:22:37 -04003405 numWorkers := *flagNumWorkers
Adam Langley95c29f32014-06-20 12:00:00 -07003406
3407 statusChan := make(chan statusMsg, numWorkers)
3408 testChan := make(chan *testCase, numWorkers)
David Benjamin5f237bc2015-02-11 17:14:15 -05003409 doneChan := make(chan *testOutput)
Adam Langley95c29f32014-06-20 12:00:00 -07003410
David Benjamin025b3d32014-07-01 19:53:04 -04003411 go statusPrinter(doneChan, statusChan, len(testCases))
Adam Langley95c29f32014-06-20 12:00:00 -07003412
3413 for i := 0; i < numWorkers; i++ {
3414 wg.Add(1)
David Benjamin884fdf12014-08-02 15:28:23 -04003415 go worker(statusChan, testChan, *flagBuildDir, &wg)
Adam Langley95c29f32014-06-20 12:00:00 -07003416 }
3417
David Benjamin025b3d32014-07-01 19:53:04 -04003418 for i := range testCases {
3419 if len(*flagTest) == 0 || *flagTest == testCases[i].name {
3420 testChan <- &testCases[i]
Adam Langley95c29f32014-06-20 12:00:00 -07003421 }
3422 }
3423
3424 close(testChan)
3425 wg.Wait()
3426 close(statusChan)
David Benjamin5f237bc2015-02-11 17:14:15 -05003427 testOutput := <-doneChan
Adam Langley95c29f32014-06-20 12:00:00 -07003428
3429 fmt.Printf("\n")
David Benjamin5f237bc2015-02-11 17:14:15 -05003430
3431 if *jsonOutput != "" {
3432 if err := testOutput.writeTo(*jsonOutput); err != nil {
3433 fmt.Fprintf(os.Stderr, "Error: %s\n", err)
3434 }
3435 }
David Benjamin2ab7a862015-04-04 17:02:18 -04003436
3437 if !testOutput.allPassed {
3438 os.Exit(1)
3439 }
Adam Langley95c29f32014-06-20 12:00:00 -07003440}