blob: bb2184706bb76fd288a076c15f6a87002af2ef8e [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
David Benjamin01fe8202014-09-24 15:21:44 -0400164 // resumeConfig, if not nil, points to a Config to be used on
David Benjaminfe8eb9a2014-11-17 03:19:02 -0500165 // resumption. Unless newSessionsOnResume is set,
166 // SessionTicketKey, ServerSessionCache, and
167 // ClientSessionCache are copied from the initial connection's
168 // config. If nil, the initial connection's config is used.
David Benjamin01fe8202014-09-24 15:21:44 -0400169 resumeConfig *Config
David Benjaminfe8eb9a2014-11-17 03:19:02 -0500170 // newSessionsOnResume, if true, will cause resumeConfig to
171 // use a different session resumption context.
172 newSessionsOnResume bool
David Benjamin98e882e2014-08-08 13:24:34 -0400173 // sendPrefix sends a prefix on the socket before actually performing a
174 // handshake.
175 sendPrefix string
David Benjamine58c4f52014-08-24 03:47:07 -0400176 // shimWritesFirst controls whether the shim sends an initial "hello"
177 // message before doing a roundtrip with the runner.
178 shimWritesFirst bool
Adam Langleycf2d4f42014-10-28 19:06:14 -0700179 // renegotiate indicates the the connection should be renegotiated
180 // during the exchange.
181 renegotiate bool
182 // renegotiateCiphers is a list of ciphersuite ids that will be
183 // switched in just before renegotiation.
184 renegotiateCiphers []uint16
David Benjamin5e961c12014-11-07 01:48:35 -0500185 // replayWrites, if true, configures the underlying transport
186 // to replay every write it makes in DTLS tests.
187 replayWrites bool
David Benjamin5fa3eba2015-01-22 16:35:40 -0500188 // damageFirstWrite, if true, configures the underlying transport to
189 // damage the final byte of the first application data write.
190 damageFirstWrite bool
David Benjaminc565ebb2015-04-03 04:06:36 -0400191 // exportKeyingMaterial, if non-zero, configures the test to exchange
192 // keying material and verify they match.
193 exportKeyingMaterial int
194 exportLabel string
195 exportContext string
196 useExportContext bool
David Benjamin325b5c32014-07-01 19:40:31 -0400197 // flags, if not empty, contains a list of command-line flags that will
198 // be passed to the shim program.
199 flags []string
Adam Langley95c29f32014-06-20 12:00:00 -0700200}
201
David Benjamin025b3d32014-07-01 19:53:04 -0400202var testCases = []testCase{
Adam Langley95c29f32014-06-20 12:00:00 -0700203 {
204 name: "BadRSASignature",
205 config: Config{
206 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
207 Bugs: ProtocolBugs{
208 InvalidSKXSignature: true,
209 },
210 },
211 shouldFail: true,
David Benjamin25f08462015-04-15 16:13:49 -0400212 expectedError: ":BAD_SIGNATURE:",
Adam Langley95c29f32014-06-20 12:00:00 -0700213 },
214 {
215 name: "BadECDSASignature",
216 config: Config{
217 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
218 Bugs: ProtocolBugs{
219 InvalidSKXSignature: true,
220 },
221 Certificates: []Certificate{getECDSACertificate()},
222 },
223 shouldFail: true,
224 expectedError: ":BAD_SIGNATURE:",
225 },
226 {
227 name: "BadECDSACurve",
228 config: Config{
229 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
230 Bugs: ProtocolBugs{
231 InvalidSKXCurve: true,
232 },
233 Certificates: []Certificate{getECDSACertificate()},
234 },
235 shouldFail: true,
236 expectedError: ":WRONG_CURVE:",
237 },
Adam Langleyac61fa32014-06-23 12:03:11 -0700238 {
David Benjamina8e3e0e2014-08-06 22:11:10 -0400239 testType: serverTest,
240 name: "BadRSAVersion",
241 config: Config{
242 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
243 Bugs: ProtocolBugs{
244 RsaClientKeyExchangeVersion: VersionTLS11,
245 },
246 },
247 shouldFail: true,
248 expectedError: ":DECRYPTION_FAILED_OR_BAD_RECORD_MAC:",
249 },
250 {
David Benjamin325b5c32014-07-01 19:40:31 -0400251 name: "NoFallbackSCSV",
Adam Langleyac61fa32014-06-23 12:03:11 -0700252 config: Config{
253 Bugs: ProtocolBugs{
254 FailIfNotFallbackSCSV: true,
255 },
256 },
257 shouldFail: true,
258 expectedLocalError: "no fallback SCSV found",
259 },
David Benjamin325b5c32014-07-01 19:40:31 -0400260 {
David Benjamin2a0c4962014-08-22 23:46:35 -0400261 name: "SendFallbackSCSV",
David Benjamin325b5c32014-07-01 19:40:31 -0400262 config: Config{
263 Bugs: ProtocolBugs{
264 FailIfNotFallbackSCSV: true,
265 },
266 },
267 flags: []string{"-fallback-scsv"},
268 },
David Benjamin197b3ab2014-07-02 18:37:33 -0400269 {
David Benjamin7b030512014-07-08 17:30:11 -0400270 name: "ClientCertificateTypes",
271 config: Config{
272 ClientAuth: RequestClientCert,
273 ClientCertificateTypes: []byte{
274 CertTypeDSSSign,
275 CertTypeRSASign,
276 CertTypeECDSASign,
277 },
278 },
David Benjamin2561dc32014-08-24 01:25:27 -0400279 flags: []string{
280 "-expect-certificate-types",
281 base64.StdEncoding.EncodeToString([]byte{
282 CertTypeDSSSign,
283 CertTypeRSASign,
284 CertTypeECDSASign,
285 }),
286 },
David Benjamin7b030512014-07-08 17:30:11 -0400287 },
David Benjamin636293b2014-07-08 17:59:18 -0400288 {
289 name: "NoClientCertificate",
290 config: Config{
291 ClientAuth: RequireAnyClientCert,
292 },
293 shouldFail: true,
294 expectedLocalError: "client didn't provide a certificate",
295 },
David Benjamin1c375dd2014-07-12 00:48:23 -0400296 {
297 name: "UnauthenticatedECDH",
298 config: Config{
299 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
300 Bugs: ProtocolBugs{
301 UnauthenticatedECDH: true,
302 },
303 },
304 shouldFail: true,
David Benjamine8f3d662014-07-12 01:10:19 -0400305 expectedError: ":UNEXPECTED_MESSAGE:",
David Benjamin1c375dd2014-07-12 00:48:23 -0400306 },
David Benjamin9c651c92014-07-12 13:27:45 -0400307 {
David Benjamindcd979f2015-04-20 18:26:52 -0400308 name: "SkipCertificateStatus",
309 config: Config{
310 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
311 Bugs: ProtocolBugs{
312 SkipCertificateStatus: true,
313 },
314 },
315 flags: []string{
316 "-enable-ocsp-stapling",
317 },
318 },
319 {
David Benjamin9c651c92014-07-12 13:27:45 -0400320 name: "SkipServerKeyExchange",
321 config: Config{
322 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
323 Bugs: ProtocolBugs{
324 SkipServerKeyExchange: true,
325 },
326 },
327 shouldFail: true,
328 expectedError: ":UNEXPECTED_MESSAGE:",
329 },
David Benjamin1f5f62b2014-07-12 16:18:02 -0400330 {
David Benjamina0e52232014-07-19 17:39:58 -0400331 name: "SkipChangeCipherSpec-Client",
332 config: Config{
333 Bugs: ProtocolBugs{
334 SkipChangeCipherSpec: true,
335 },
336 },
337 shouldFail: true,
David Benjamin86271ee2014-07-21 16:14:03 -0400338 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
David Benjamina0e52232014-07-19 17:39:58 -0400339 },
340 {
341 testType: serverTest,
342 name: "SkipChangeCipherSpec-Server",
343 config: Config{
344 Bugs: ProtocolBugs{
345 SkipChangeCipherSpec: true,
346 },
347 },
348 shouldFail: true,
David Benjamin86271ee2014-07-21 16:14:03 -0400349 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
David Benjamina0e52232014-07-19 17:39:58 -0400350 },
David Benjamin42be6452014-07-21 14:50:23 -0400351 {
352 testType: serverTest,
353 name: "SkipChangeCipherSpec-Server-NPN",
354 config: Config{
355 NextProtos: []string{"bar"},
356 Bugs: ProtocolBugs{
357 SkipChangeCipherSpec: true,
358 },
359 },
360 flags: []string{
361 "-advertise-npn", "\x03foo\x03bar\x03baz",
362 },
363 shouldFail: true,
David Benjamin86271ee2014-07-21 16:14:03 -0400364 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
365 },
366 {
367 name: "FragmentAcrossChangeCipherSpec-Client",
368 config: Config{
369 Bugs: ProtocolBugs{
370 FragmentAcrossChangeCipherSpec: true,
371 },
372 },
373 shouldFail: true,
374 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
375 },
376 {
377 testType: serverTest,
378 name: "FragmentAcrossChangeCipherSpec-Server",
379 config: Config{
380 Bugs: ProtocolBugs{
381 FragmentAcrossChangeCipherSpec: true,
382 },
383 },
384 shouldFail: true,
385 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
386 },
387 {
388 testType: serverTest,
389 name: "FragmentAcrossChangeCipherSpec-Server-NPN",
390 config: Config{
391 NextProtos: []string{"bar"},
392 Bugs: ProtocolBugs{
393 FragmentAcrossChangeCipherSpec: true,
394 },
395 },
396 flags: []string{
397 "-advertise-npn", "\x03foo\x03bar\x03baz",
398 },
399 shouldFail: true,
400 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
David Benjamin42be6452014-07-21 14:50:23 -0400401 },
David Benjaminf3ec83d2014-07-21 22:42:34 -0400402 {
403 testType: serverTest,
David Benjamin3fd1fbd2015-02-03 16:07:32 -0500404 name: "Alert",
405 config: Config{
406 Bugs: ProtocolBugs{
407 SendSpuriousAlert: alertRecordOverflow,
408 },
409 },
410 shouldFail: true,
411 expectedError: ":TLSV1_ALERT_RECORD_OVERFLOW:",
412 },
413 {
414 protocol: dtls,
415 testType: serverTest,
416 name: "Alert-DTLS",
417 config: Config{
418 Bugs: ProtocolBugs{
419 SendSpuriousAlert: alertRecordOverflow,
420 },
421 },
422 shouldFail: true,
423 expectedError: ":TLSV1_ALERT_RECORD_OVERFLOW:",
424 },
425 {
426 testType: serverTest,
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400427 name: "FragmentAlert",
428 config: Config{
429 Bugs: ProtocolBugs{
David Benjaminca6c8262014-11-15 19:06:08 -0500430 FragmentAlert: true,
David Benjamin3fd1fbd2015-02-03 16:07:32 -0500431 SendSpuriousAlert: alertRecordOverflow,
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400432 },
433 },
434 shouldFail: true,
435 expectedError: ":BAD_ALERT:",
436 },
437 {
David Benjamin0ea8dda2015-01-31 20:33:40 -0500438 protocol: dtls,
439 testType: serverTest,
440 name: "FragmentAlert-DTLS",
441 config: Config{
442 Bugs: ProtocolBugs{
443 FragmentAlert: true,
David Benjamin3fd1fbd2015-02-03 16:07:32 -0500444 SendSpuriousAlert: alertRecordOverflow,
David Benjamin0ea8dda2015-01-31 20:33:40 -0500445 },
446 },
447 shouldFail: true,
448 expectedError: ":BAD_ALERT:",
449 },
450 {
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400451 testType: serverTest,
David Benjaminf3ec83d2014-07-21 22:42:34 -0400452 name: "EarlyChangeCipherSpec-server-1",
453 config: Config{
454 Bugs: ProtocolBugs{
455 EarlyChangeCipherSpec: 1,
456 },
457 },
458 shouldFail: true,
459 expectedError: ":CCS_RECEIVED_EARLY:",
460 },
461 {
462 testType: serverTest,
463 name: "EarlyChangeCipherSpec-server-2",
464 config: Config{
465 Bugs: ProtocolBugs{
466 EarlyChangeCipherSpec: 2,
467 },
468 },
469 shouldFail: true,
470 expectedError: ":CCS_RECEIVED_EARLY:",
471 },
David Benjamind23f4122014-07-23 15:09:48 -0400472 {
David Benjamind23f4122014-07-23 15:09:48 -0400473 name: "SkipNewSessionTicket",
474 config: Config{
475 Bugs: ProtocolBugs{
476 SkipNewSessionTicket: true,
477 },
478 },
479 shouldFail: true,
480 expectedError: ":CCS_RECEIVED_EARLY:",
481 },
David Benjamin7e3305e2014-07-28 14:52:32 -0400482 {
David Benjamind86c7672014-08-02 04:07:12 -0400483 testType: serverTest,
David Benjaminbef270a2014-08-02 04:22:02 -0400484 name: "FallbackSCSV",
485 config: Config{
486 MaxVersion: VersionTLS11,
487 Bugs: ProtocolBugs{
488 SendFallbackSCSV: true,
489 },
490 },
491 shouldFail: true,
492 expectedError: ":INAPPROPRIATE_FALLBACK:",
493 },
494 {
495 testType: serverTest,
496 name: "FallbackSCSV-VersionMatch",
497 config: Config{
498 Bugs: ProtocolBugs{
499 SendFallbackSCSV: true,
500 },
501 },
502 },
David Benjamin98214542014-08-07 18:02:39 -0400503 {
504 testType: serverTest,
505 name: "FragmentedClientVersion",
506 config: Config{
507 Bugs: ProtocolBugs{
508 MaxHandshakeRecordLength: 1,
509 FragmentClientVersion: true,
510 },
511 },
David Benjamin82c9e902014-12-12 15:55:27 -0500512 expectedVersion: VersionTLS12,
David Benjamin98214542014-08-07 18:02:39 -0400513 },
David Benjamin98e882e2014-08-08 13:24:34 -0400514 {
515 testType: serverTest,
516 name: "MinorVersionTolerance",
517 config: Config{
518 Bugs: ProtocolBugs{
519 SendClientVersion: 0x03ff,
520 },
521 },
522 expectedVersion: VersionTLS12,
523 },
524 {
525 testType: serverTest,
526 name: "MajorVersionTolerance",
527 config: Config{
528 Bugs: ProtocolBugs{
529 SendClientVersion: 0x0400,
530 },
531 },
532 expectedVersion: VersionTLS12,
533 },
534 {
535 testType: serverTest,
536 name: "VersionTooLow",
537 config: Config{
538 Bugs: ProtocolBugs{
539 SendClientVersion: 0x0200,
540 },
541 },
542 shouldFail: true,
543 expectedError: ":UNSUPPORTED_PROTOCOL:",
544 },
545 {
546 testType: serverTest,
547 name: "HttpGET",
548 sendPrefix: "GET / HTTP/1.0\n",
549 shouldFail: true,
550 expectedError: ":HTTP_REQUEST:",
551 },
552 {
553 testType: serverTest,
554 name: "HttpPOST",
555 sendPrefix: "POST / HTTP/1.0\n",
556 shouldFail: true,
557 expectedError: ":HTTP_REQUEST:",
558 },
559 {
560 testType: serverTest,
561 name: "HttpHEAD",
562 sendPrefix: "HEAD / HTTP/1.0\n",
563 shouldFail: true,
564 expectedError: ":HTTP_REQUEST:",
565 },
566 {
567 testType: serverTest,
568 name: "HttpPUT",
569 sendPrefix: "PUT / HTTP/1.0\n",
570 shouldFail: true,
571 expectedError: ":HTTP_REQUEST:",
572 },
573 {
574 testType: serverTest,
575 name: "HttpCONNECT",
576 sendPrefix: "CONNECT www.google.com:443 HTTP/1.0\n",
577 shouldFail: true,
578 expectedError: ":HTTPS_PROXY_REQUEST:",
579 },
David Benjamin39ebf532014-08-31 02:23:49 -0400580 {
David Benjaminf080ecd2014-12-11 18:48:59 -0500581 testType: serverTest,
582 name: "Garbage",
583 sendPrefix: "blah",
584 shouldFail: true,
585 expectedError: ":UNKNOWN_PROTOCOL:",
586 },
587 {
David Benjamin39ebf532014-08-31 02:23:49 -0400588 name: "SkipCipherVersionCheck",
589 config: Config{
590 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
591 MaxVersion: VersionTLS11,
592 Bugs: ProtocolBugs{
593 SkipCipherVersionCheck: true,
594 },
595 },
596 shouldFail: true,
597 expectedError: ":WRONG_CIPHER_RETURNED:",
598 },
David Benjamin9114fae2014-11-08 11:41:14 -0500599 {
David Benjamina3e89492015-02-26 15:16:22 -0500600 name: "RSAEphemeralKey",
David Benjamin9114fae2014-11-08 11:41:14 -0500601 config: Config{
602 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
603 Bugs: ProtocolBugs{
David Benjamina3e89492015-02-26 15:16:22 -0500604 RSAEphemeralKey: true,
David Benjamin9114fae2014-11-08 11:41:14 -0500605 },
606 },
607 shouldFail: true,
608 expectedError: ":UNEXPECTED_MESSAGE:",
609 },
David Benjamin128dbc32014-12-01 01:27:42 -0500610 {
611 name: "DisableEverything",
612 flags: []string{"-no-tls12", "-no-tls11", "-no-tls1", "-no-ssl3"},
613 shouldFail: true,
614 expectedError: ":WRONG_SSL_VERSION:",
615 },
616 {
617 protocol: dtls,
618 name: "DisableEverything-DTLS",
619 flags: []string{"-no-tls12", "-no-tls1"},
620 shouldFail: true,
621 expectedError: ":WRONG_SSL_VERSION:",
622 },
David Benjamin780d6dd2015-01-06 12:03:19 -0500623 {
624 name: "NoSharedCipher",
625 config: Config{
626 CipherSuites: []uint16{},
627 },
628 shouldFail: true,
629 expectedError: ":HANDSHAKE_FAILURE_ON_CLIENT_HELLO:",
630 },
David Benjamin13be1de2015-01-11 16:29:36 -0500631 {
632 protocol: dtls,
633 testType: serverTest,
634 name: "MTU",
635 config: Config{
636 Bugs: ProtocolBugs{
637 MaxPacketLength: 256,
638 },
639 },
640 flags: []string{"-mtu", "256"},
641 },
642 {
643 protocol: dtls,
644 testType: serverTest,
645 name: "MTUExceeded",
646 config: Config{
647 Bugs: ProtocolBugs{
648 MaxPacketLength: 255,
649 },
650 },
651 flags: []string{"-mtu", "256"},
652 shouldFail: true,
653 expectedLocalError: "dtls: exceeded maximum packet length",
654 },
David Benjamin6095de82014-12-27 01:50:38 -0500655 {
656 name: "CertMismatchRSA",
657 config: Config{
658 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
659 Certificates: []Certificate{getECDSACertificate()},
660 Bugs: ProtocolBugs{
661 SendCipherSuite: TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
662 },
663 },
664 shouldFail: true,
665 expectedError: ":WRONG_CERTIFICATE_TYPE:",
666 },
667 {
668 name: "CertMismatchECDSA",
669 config: Config{
670 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
671 Certificates: []Certificate{getRSACertificate()},
672 Bugs: ProtocolBugs{
673 SendCipherSuite: TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
674 },
675 },
676 shouldFail: true,
677 expectedError: ":WRONG_CERTIFICATE_TYPE:",
678 },
David Benjamin5fa3eba2015-01-22 16:35:40 -0500679 {
680 name: "TLSFatalBadPackets",
681 damageFirstWrite: true,
682 shouldFail: true,
683 expectedError: ":DECRYPTION_FAILED_OR_BAD_RECORD_MAC:",
684 },
685 {
686 protocol: dtls,
687 name: "DTLSIgnoreBadPackets",
688 damageFirstWrite: true,
689 },
690 {
691 protocol: dtls,
692 name: "DTLSIgnoreBadPackets-Async",
693 damageFirstWrite: true,
694 flags: []string{"-async"},
695 },
David Benjamin4189bd92015-01-25 23:52:39 -0500696 {
697 name: "AppDataAfterChangeCipherSpec",
698 config: Config{
699 Bugs: ProtocolBugs{
700 AppDataAfterChangeCipherSpec: []byte("TEST MESSAGE"),
701 },
702 },
703 shouldFail: true,
704 expectedError: ":DATA_BETWEEN_CCS_AND_FINISHED:",
705 },
706 {
707 protocol: dtls,
708 name: "AppDataAfterChangeCipherSpec-DTLS",
709 config: Config{
710 Bugs: ProtocolBugs{
711 AppDataAfterChangeCipherSpec: []byte("TEST MESSAGE"),
712 },
713 },
David Benjamin4417d052015-04-05 04:17:25 -0400714 // BoringSSL's DTLS implementation will drop the out-of-order
715 // application data.
David Benjamin4189bd92015-01-25 23:52:39 -0500716 },
David Benjaminb3774b92015-01-31 17:16:01 -0500717 {
David Benjamindc3da932015-03-12 15:09:02 -0400718 name: "AlertAfterChangeCipherSpec",
719 config: Config{
720 Bugs: ProtocolBugs{
721 AlertAfterChangeCipherSpec: alertRecordOverflow,
722 },
723 },
724 shouldFail: true,
725 expectedError: ":TLSV1_ALERT_RECORD_OVERFLOW:",
726 },
727 {
728 protocol: dtls,
729 name: "AlertAfterChangeCipherSpec-DTLS",
730 config: Config{
731 Bugs: ProtocolBugs{
732 AlertAfterChangeCipherSpec: alertRecordOverflow,
733 },
734 },
735 shouldFail: true,
736 expectedError: ":TLSV1_ALERT_RECORD_OVERFLOW:",
737 },
738 {
David Benjaminb3774b92015-01-31 17:16:01 -0500739 protocol: dtls,
740 name: "ReorderHandshakeFragments-Small-DTLS",
741 config: Config{
742 Bugs: ProtocolBugs{
743 ReorderHandshakeFragments: true,
744 // Small enough that every handshake message is
745 // fragmented.
746 MaxHandshakeRecordLength: 2,
747 },
748 },
749 },
750 {
751 protocol: dtls,
752 name: "ReorderHandshakeFragments-Large-DTLS",
753 config: Config{
754 Bugs: ProtocolBugs{
755 ReorderHandshakeFragments: true,
756 // Large enough that no handshake message is
757 // fragmented.
David Benjaminb3774b92015-01-31 17:16:01 -0500758 MaxHandshakeRecordLength: 2048,
759 },
760 },
761 },
David Benjaminddb9f152015-02-03 15:44:39 -0500762 {
David Benjamin75381222015-03-02 19:30:30 -0500763 protocol: dtls,
764 name: "MixCompleteMessageWithFragments-DTLS",
765 config: Config{
766 Bugs: ProtocolBugs{
767 ReorderHandshakeFragments: true,
768 MixCompleteMessageWithFragments: true,
769 MaxHandshakeRecordLength: 2,
770 },
771 },
772 },
773 {
David Benjaminddb9f152015-02-03 15:44:39 -0500774 name: "SendInvalidRecordType",
775 config: Config{
776 Bugs: ProtocolBugs{
777 SendInvalidRecordType: true,
778 },
779 },
780 shouldFail: true,
781 expectedError: ":UNEXPECTED_RECORD:",
782 },
783 {
784 protocol: dtls,
785 name: "SendInvalidRecordType-DTLS",
786 config: Config{
787 Bugs: ProtocolBugs{
788 SendInvalidRecordType: true,
789 },
790 },
791 shouldFail: true,
792 expectedError: ":UNEXPECTED_RECORD:",
793 },
David Benjaminb80168e2015-02-08 18:30:14 -0500794 {
795 name: "FalseStart-SkipServerSecondLeg",
796 config: Config{
797 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
798 NextProtos: []string{"foo"},
799 Bugs: ProtocolBugs{
800 SkipNewSessionTicket: true,
801 SkipChangeCipherSpec: true,
802 SkipFinished: true,
803 ExpectFalseStart: true,
804 },
805 },
806 flags: []string{
807 "-false-start",
David Benjamin87e4acd2015-04-02 19:57:35 -0400808 "-handshake-never-done",
David Benjaminb80168e2015-02-08 18:30:14 -0500809 "-advertise-alpn", "\x03foo",
810 },
811 shimWritesFirst: true,
812 shouldFail: true,
813 expectedError: ":UNEXPECTED_RECORD:",
814 },
David Benjamin931ab342015-02-08 19:46:57 -0500815 {
816 name: "FalseStart-SkipServerSecondLeg-Implicit",
817 config: Config{
818 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
819 NextProtos: []string{"foo"},
820 Bugs: ProtocolBugs{
821 SkipNewSessionTicket: true,
822 SkipChangeCipherSpec: true,
823 SkipFinished: true,
824 },
825 },
826 flags: []string{
827 "-implicit-handshake",
828 "-false-start",
David Benjamin87e4acd2015-04-02 19:57:35 -0400829 "-handshake-never-done",
David Benjamin931ab342015-02-08 19:46:57 -0500830 "-advertise-alpn", "\x03foo",
831 },
832 shouldFail: true,
833 expectedError: ":UNEXPECTED_RECORD:",
834 },
David Benjamin6f5c0f42015-02-24 01:23:21 -0500835 {
836 testType: serverTest,
837 name: "FailEarlyCallback",
838 flags: []string{"-fail-early-callback"},
839 shouldFail: true,
840 expectedError: ":CONNECTION_REJECTED:",
841 expectedLocalError: "remote error: access denied",
842 },
David Benjaminbcb2d912015-02-24 23:45:43 -0500843 {
844 name: "WrongMessageType",
845 config: Config{
846 Bugs: ProtocolBugs{
847 WrongCertificateMessageType: true,
848 },
849 },
850 shouldFail: true,
851 expectedError: ":UNEXPECTED_MESSAGE:",
852 expectedLocalError: "remote error: unexpected message",
853 },
854 {
855 protocol: dtls,
856 name: "WrongMessageType-DTLS",
857 config: Config{
858 Bugs: ProtocolBugs{
859 WrongCertificateMessageType: true,
860 },
861 },
862 shouldFail: true,
863 expectedError: ":UNEXPECTED_MESSAGE:",
864 expectedLocalError: "remote error: unexpected message",
865 },
David Benjamin75381222015-03-02 19:30:30 -0500866 {
867 protocol: dtls,
868 name: "FragmentMessageTypeMismatch-DTLS",
869 config: Config{
870 Bugs: ProtocolBugs{
871 MaxHandshakeRecordLength: 2,
872 FragmentMessageTypeMismatch: true,
873 },
874 },
875 shouldFail: true,
876 expectedError: ":FRAGMENT_MISMATCH:",
877 },
878 {
879 protocol: dtls,
880 name: "FragmentMessageLengthMismatch-DTLS",
881 config: Config{
882 Bugs: ProtocolBugs{
883 MaxHandshakeRecordLength: 2,
884 FragmentMessageLengthMismatch: true,
885 },
886 },
887 shouldFail: true,
888 expectedError: ":FRAGMENT_MISMATCH:",
889 },
890 {
891 protocol: dtls,
892 name: "SplitFragmentHeader-DTLS",
893 config: Config{
894 Bugs: ProtocolBugs{
895 SplitFragmentHeader: true,
896 },
897 },
898 shouldFail: true,
899 expectedError: ":UNEXPECTED_MESSAGE:",
900 },
901 {
902 protocol: dtls,
903 name: "SplitFragmentBody-DTLS",
904 config: Config{
905 Bugs: ProtocolBugs{
906 SplitFragmentBody: true,
907 },
908 },
909 shouldFail: true,
910 expectedError: ":UNEXPECTED_MESSAGE:",
911 },
912 {
913 protocol: dtls,
914 name: "SendEmptyFragments-DTLS",
915 config: Config{
916 Bugs: ProtocolBugs{
917 SendEmptyFragments: true,
918 },
919 },
920 },
David Benjamin67d1fb52015-03-16 15:16:23 -0400921 {
922 name: "UnsupportedCipherSuite",
923 config: Config{
924 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
925 Bugs: ProtocolBugs{
926 IgnorePeerCipherPreferences: true,
927 },
928 },
929 flags: []string{"-cipher", "DEFAULT:!RC4"},
930 shouldFail: true,
931 expectedError: ":WRONG_CIPHER_RETURNED:",
932 },
David Benjamin340d5ed2015-03-21 02:21:37 -0400933 {
David Benjaminc574f412015-04-20 11:13:01 -0400934 name: "UnsupportedCurve",
935 config: Config{
936 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
937 // BoringSSL implements P-224 but doesn't enable it by
938 // default.
939 CurvePreferences: []CurveID{CurveP224},
940 Bugs: ProtocolBugs{
941 IgnorePeerCurvePreferences: true,
942 },
943 },
944 shouldFail: true,
945 expectedError: ":WRONG_CURVE:",
946 },
947 {
David Benjamin340d5ed2015-03-21 02:21:37 -0400948 name: "SendWarningAlerts",
949 config: Config{
950 Bugs: ProtocolBugs{
951 SendWarningAlerts: alertAccessDenied,
952 },
953 },
954 },
955 {
956 protocol: dtls,
957 name: "SendWarningAlerts-DTLS",
958 config: Config{
959 Bugs: ProtocolBugs{
960 SendWarningAlerts: alertAccessDenied,
961 },
962 },
963 },
David Benjamin513f0ea2015-04-02 19:33:31 -0400964 {
965 name: "BadFinished",
966 config: Config{
967 Bugs: ProtocolBugs{
968 BadFinished: true,
969 },
970 },
971 shouldFail: true,
972 expectedError: ":DIGEST_CHECK_FAILED:",
973 },
974 {
975 name: "FalseStart-BadFinished",
976 config: Config{
977 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
978 NextProtos: []string{"foo"},
979 Bugs: ProtocolBugs{
980 BadFinished: true,
981 ExpectFalseStart: true,
982 },
983 },
984 flags: []string{
985 "-false-start",
David Benjamin87e4acd2015-04-02 19:57:35 -0400986 "-handshake-never-done",
David Benjamin513f0ea2015-04-02 19:33:31 -0400987 "-advertise-alpn", "\x03foo",
988 },
989 shimWritesFirst: true,
990 shouldFail: true,
991 expectedError: ":DIGEST_CHECK_FAILED:",
992 },
David Benjamin1c633152015-04-02 20:19:11 -0400993 {
994 name: "NoFalseStart-NoALPN",
995 config: Config{
996 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
997 Bugs: ProtocolBugs{
998 ExpectFalseStart: true,
999 AlertBeforeFalseStartTest: alertAccessDenied,
1000 },
1001 },
1002 flags: []string{
1003 "-false-start",
1004 },
1005 shimWritesFirst: true,
1006 shouldFail: true,
1007 expectedError: ":TLSV1_ALERT_ACCESS_DENIED:",
1008 expectedLocalError: "tls: peer did not false start: EOF",
1009 },
1010 {
1011 name: "NoFalseStart-NoAEAD",
1012 config: Config{
1013 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1014 NextProtos: []string{"foo"},
1015 Bugs: ProtocolBugs{
1016 ExpectFalseStart: true,
1017 AlertBeforeFalseStartTest: alertAccessDenied,
1018 },
1019 },
1020 flags: []string{
1021 "-false-start",
1022 "-advertise-alpn", "\x03foo",
1023 },
1024 shimWritesFirst: true,
1025 shouldFail: true,
1026 expectedError: ":TLSV1_ALERT_ACCESS_DENIED:",
1027 expectedLocalError: "tls: peer did not false start: EOF",
1028 },
1029 {
1030 name: "NoFalseStart-RSA",
1031 config: Config{
1032 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
1033 NextProtos: []string{"foo"},
1034 Bugs: ProtocolBugs{
1035 ExpectFalseStart: true,
1036 AlertBeforeFalseStartTest: alertAccessDenied,
1037 },
1038 },
1039 flags: []string{
1040 "-false-start",
1041 "-advertise-alpn", "\x03foo",
1042 },
1043 shimWritesFirst: true,
1044 shouldFail: true,
1045 expectedError: ":TLSV1_ALERT_ACCESS_DENIED:",
1046 expectedLocalError: "tls: peer did not false start: EOF",
1047 },
1048 {
1049 name: "NoFalseStart-DHE_RSA",
1050 config: Config{
1051 CipherSuites: []uint16{TLS_DHE_RSA_WITH_AES_128_GCM_SHA256},
1052 NextProtos: []string{"foo"},
1053 Bugs: ProtocolBugs{
1054 ExpectFalseStart: true,
1055 AlertBeforeFalseStartTest: alertAccessDenied,
1056 },
1057 },
1058 flags: []string{
1059 "-false-start",
1060 "-advertise-alpn", "\x03foo",
1061 },
1062 shimWritesFirst: true,
1063 shouldFail: true,
1064 expectedError: ":TLSV1_ALERT_ACCESS_DENIED:",
1065 expectedLocalError: "tls: peer did not false start: EOF",
1066 },
David Benjamin55a43642015-04-20 14:45:55 -04001067 {
1068 testType: serverTest,
1069 name: "NoSupportedCurves",
1070 config: Config{
1071 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1072 Bugs: ProtocolBugs{
1073 NoSupportedCurves: true,
1074 },
1075 },
1076 },
David Benjamin90da8c82015-04-20 14:57:57 -04001077 {
1078 testType: serverTest,
1079 name: "NoCommonCurves",
1080 config: Config{
1081 CipherSuites: []uint16{
1082 TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
1083 TLS_DHE_RSA_WITH_AES_128_GCM_SHA256,
1084 },
1085 CurvePreferences: []CurveID{CurveP224},
1086 },
1087 expectedCipher: TLS_DHE_RSA_WITH_AES_128_GCM_SHA256,
1088 },
Adam Langley95c29f32014-06-20 12:00:00 -07001089}
1090
David Benjamin01fe8202014-09-24 15:21:44 -04001091func doExchange(test *testCase, config *Config, conn net.Conn, messageLen int, isResume bool) error {
David Benjamin65ea8ff2014-11-23 03:01:00 -05001092 var connDebug *recordingConn
David Benjamin5fa3eba2015-01-22 16:35:40 -05001093 var connDamage *damageAdaptor
David Benjamin65ea8ff2014-11-23 03:01:00 -05001094 if *flagDebug {
1095 connDebug = &recordingConn{Conn: conn}
1096 conn = connDebug
1097 defer func() {
1098 connDebug.WriteTo(os.Stdout)
1099 }()
1100 }
1101
David Benjamin6fd297b2014-08-11 18:43:38 -04001102 if test.protocol == dtls {
David Benjamin83f90402015-01-27 01:09:43 -05001103 config.Bugs.PacketAdaptor = newPacketAdaptor(conn)
1104 conn = config.Bugs.PacketAdaptor
David Benjamin5e961c12014-11-07 01:48:35 -05001105 if test.replayWrites {
1106 conn = newReplayAdaptor(conn)
1107 }
David Benjamin6fd297b2014-08-11 18:43:38 -04001108 }
1109
David Benjamin5fa3eba2015-01-22 16:35:40 -05001110 if test.damageFirstWrite {
1111 connDamage = newDamageAdaptor(conn)
1112 conn = connDamage
1113 }
1114
David Benjamin6fd297b2014-08-11 18:43:38 -04001115 if test.sendPrefix != "" {
1116 if _, err := conn.Write([]byte(test.sendPrefix)); err != nil {
1117 return err
1118 }
David Benjamin98e882e2014-08-08 13:24:34 -04001119 }
1120
David Benjamin1d5c83e2014-07-22 19:20:02 -04001121 var tlsConn *Conn
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001122 if test.testType == clientTest {
David Benjamin6fd297b2014-08-11 18:43:38 -04001123 if test.protocol == dtls {
1124 tlsConn = DTLSServer(conn, config)
1125 } else {
1126 tlsConn = Server(conn, config)
1127 }
David Benjamin1d5c83e2014-07-22 19:20:02 -04001128 } else {
1129 config.InsecureSkipVerify = true
David Benjamin6fd297b2014-08-11 18:43:38 -04001130 if test.protocol == dtls {
1131 tlsConn = DTLSClient(conn, config)
1132 } else {
1133 tlsConn = Client(conn, config)
1134 }
David Benjamin1d5c83e2014-07-22 19:20:02 -04001135 }
1136
Adam Langley95c29f32014-06-20 12:00:00 -07001137 if err := tlsConn.Handshake(); err != nil {
1138 return err
1139 }
Kenny Root7fdeaf12014-08-05 15:23:37 -07001140
David Benjamin01fe8202014-09-24 15:21:44 -04001141 // TODO(davidben): move all per-connection expectations into a dedicated
1142 // expectations struct that can be specified separately for the two
1143 // legs.
1144 expectedVersion := test.expectedVersion
1145 if isResume && test.expectedResumeVersion != 0 {
1146 expectedVersion = test.expectedResumeVersion
1147 }
1148 if vers := tlsConn.ConnectionState().Version; expectedVersion != 0 && vers != expectedVersion {
1149 return fmt.Errorf("got version %x, expected %x", vers, expectedVersion)
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001150 }
1151
David Benjamin90da8c82015-04-20 14:57:57 -04001152 if cipher := tlsConn.ConnectionState().CipherSuite; test.expectedCipher != 0 && cipher != test.expectedCipher {
1153 return fmt.Errorf("got cipher %x, expected %x", cipher, test.expectedCipher)
1154 }
1155
David Benjamina08e49d2014-08-24 01:46:07 -04001156 if test.expectChannelID {
1157 channelID := tlsConn.ConnectionState().ChannelID
1158 if channelID == nil {
1159 return fmt.Errorf("no channel ID negotiated")
1160 }
1161 if channelID.Curve != channelIDKey.Curve ||
1162 channelIDKey.X.Cmp(channelIDKey.X) != 0 ||
1163 channelIDKey.Y.Cmp(channelIDKey.Y) != 0 {
1164 return fmt.Errorf("incorrect channel ID")
1165 }
1166 }
1167
David Benjaminae2888f2014-09-06 12:58:58 -04001168 if expected := test.expectedNextProto; expected != "" {
1169 if actual := tlsConn.ConnectionState().NegotiatedProtocol; actual != expected {
1170 return fmt.Errorf("next proto mismatch: got %s, wanted %s", actual, expected)
1171 }
1172 }
1173
David Benjaminfc7b0862014-09-06 13:21:53 -04001174 if test.expectedNextProtoType != 0 {
1175 if (test.expectedNextProtoType == alpn) != tlsConn.ConnectionState().NegotiatedProtocolFromALPN {
1176 return fmt.Errorf("next proto type mismatch")
1177 }
1178 }
1179
David Benjaminca6c8262014-11-15 19:06:08 -05001180 if p := tlsConn.ConnectionState().SRTPProtectionProfile; p != test.expectedSRTPProtectionProfile {
1181 return fmt.Errorf("SRTP profile mismatch: got %d, wanted %d", p, test.expectedSRTPProtectionProfile)
1182 }
1183
David Benjaminc565ebb2015-04-03 04:06:36 -04001184 if test.exportKeyingMaterial > 0 {
1185 actual := make([]byte, test.exportKeyingMaterial)
1186 if _, err := io.ReadFull(tlsConn, actual); err != nil {
1187 return err
1188 }
1189 expected, err := tlsConn.ExportKeyingMaterial(test.exportKeyingMaterial, []byte(test.exportLabel), []byte(test.exportContext), test.useExportContext)
1190 if err != nil {
1191 return err
1192 }
1193 if !bytes.Equal(actual, expected) {
1194 return fmt.Errorf("keying material mismatch")
1195 }
1196 }
1197
David Benjamine58c4f52014-08-24 03:47:07 -04001198 if test.shimWritesFirst {
1199 var buf [5]byte
1200 _, err := io.ReadFull(tlsConn, buf[:])
1201 if err != nil {
1202 return err
1203 }
1204 if string(buf[:]) != "hello" {
1205 return fmt.Errorf("bad initial message")
1206 }
1207 }
1208
Adam Langleycf2d4f42014-10-28 19:06:14 -07001209 if test.renegotiate {
1210 if test.renegotiateCiphers != nil {
1211 config.CipherSuites = test.renegotiateCiphers
1212 }
1213 if err := tlsConn.Renegotiate(); err != nil {
1214 return err
1215 }
1216 } else if test.renegotiateCiphers != nil {
1217 panic("renegotiateCiphers without renegotiate")
1218 }
1219
David Benjamin5fa3eba2015-01-22 16:35:40 -05001220 if test.damageFirstWrite {
1221 connDamage.setDamage(true)
1222 tlsConn.Write([]byte("DAMAGED WRITE"))
1223 connDamage.setDamage(false)
1224 }
1225
Kenny Root7fdeaf12014-08-05 15:23:37 -07001226 if messageLen < 0 {
David Benjamin6fd297b2014-08-11 18:43:38 -04001227 if test.protocol == dtls {
1228 return fmt.Errorf("messageLen < 0 not supported for DTLS tests")
1229 }
Kenny Root7fdeaf12014-08-05 15:23:37 -07001230 // Read until EOF.
1231 _, err := io.Copy(ioutil.Discard, tlsConn)
1232 return err
1233 }
1234
David Benjamin4417d052015-04-05 04:17:25 -04001235 if messageLen == 0 {
1236 messageLen = 32
Adam Langley80842bd2014-06-20 12:00:00 -07001237 }
David Benjamin4417d052015-04-05 04:17:25 -04001238 testMessage := make([]byte, messageLen)
1239 for i := range testMessage {
1240 testMessage[i] = 0x42
1241 }
1242 tlsConn.Write(testMessage)
Adam Langley95c29f32014-06-20 12:00:00 -07001243
1244 buf := make([]byte, len(testMessage))
David Benjamin6fd297b2014-08-11 18:43:38 -04001245 if test.protocol == dtls {
1246 bufTmp := make([]byte, len(buf)+1)
1247 n, err := tlsConn.Read(bufTmp)
1248 if err != nil {
1249 return err
1250 }
1251 if n != len(buf) {
1252 return fmt.Errorf("bad reply; length mismatch (%d vs %d)", n, len(buf))
1253 }
1254 copy(buf, bufTmp)
1255 } else {
1256 _, err := io.ReadFull(tlsConn, buf)
1257 if err != nil {
1258 return err
1259 }
Adam Langley95c29f32014-06-20 12:00:00 -07001260 }
1261
1262 for i, v := range buf {
1263 if v != testMessage[i]^0xff {
1264 return fmt.Errorf("bad reply contents at byte %d", i)
1265 }
1266 }
1267
1268 return nil
1269}
1270
David Benjamin325b5c32014-07-01 19:40:31 -04001271func valgrindOf(dbAttach bool, path string, args ...string) *exec.Cmd {
1272 valgrindArgs := []string{"--error-exitcode=99", "--track-origins=yes", "--leak-check=full"}
Adam Langley95c29f32014-06-20 12:00:00 -07001273 if dbAttach {
David Benjamin325b5c32014-07-01 19:40:31 -04001274 valgrindArgs = append(valgrindArgs, "--db-attach=yes", "--db-command=xterm -e gdb -nw %f %p")
Adam Langley95c29f32014-06-20 12:00:00 -07001275 }
David Benjamin325b5c32014-07-01 19:40:31 -04001276 valgrindArgs = append(valgrindArgs, path)
1277 valgrindArgs = append(valgrindArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -07001278
David Benjamin325b5c32014-07-01 19:40:31 -04001279 return exec.Command("valgrind", valgrindArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -07001280}
1281
David Benjamin325b5c32014-07-01 19:40:31 -04001282func gdbOf(path string, args ...string) *exec.Cmd {
1283 xtermArgs := []string{"-e", "gdb", "--args"}
1284 xtermArgs = append(xtermArgs, path)
1285 xtermArgs = append(xtermArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -07001286
David Benjamin325b5c32014-07-01 19:40:31 -04001287 return exec.Command("xterm", xtermArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -07001288}
1289
Adam Langley69a01602014-11-17 17:26:55 -08001290type moreMallocsError struct{}
1291
1292func (moreMallocsError) Error() string {
1293 return "child process did not exhaust all allocation calls"
1294}
1295
1296var errMoreMallocs = moreMallocsError{}
1297
David Benjamin87c8a642015-02-21 01:54:29 -05001298// accept accepts a connection from listener, unless waitChan signals a process
1299// exit first.
1300func acceptOrWait(listener net.Listener, waitChan chan error) (net.Conn, error) {
1301 type connOrError struct {
1302 conn net.Conn
1303 err error
1304 }
1305 connChan := make(chan connOrError, 1)
1306 go func() {
1307 conn, err := listener.Accept()
1308 connChan <- connOrError{conn, err}
1309 close(connChan)
1310 }()
1311 select {
1312 case result := <-connChan:
1313 return result.conn, result.err
1314 case childErr := <-waitChan:
1315 waitChan <- childErr
1316 return nil, fmt.Errorf("child exited early: %s", childErr)
1317 }
1318}
1319
Adam Langley69a01602014-11-17 17:26:55 -08001320func runTest(test *testCase, buildDir string, mallocNumToFail int64) error {
Adam Langley38311732014-10-16 19:04:35 -07001321 if !test.shouldFail && (len(test.expectedError) > 0 || len(test.expectedLocalError) > 0) {
1322 panic("Error expected without shouldFail in " + test.name)
1323 }
1324
David Benjamin87c8a642015-02-21 01:54:29 -05001325 listener, err := net.ListenTCP("tcp4", &net.TCPAddr{IP: net.IP{127, 0, 0, 1}})
1326 if err != nil {
1327 panic(err)
1328 }
1329 defer func() {
1330 if listener != nil {
1331 listener.Close()
1332 }
1333 }()
Adam Langley95c29f32014-06-20 12:00:00 -07001334
David Benjamin884fdf12014-08-02 15:28:23 -04001335 shim_path := path.Join(buildDir, "ssl/test/bssl_shim")
David Benjamin87c8a642015-02-21 01:54:29 -05001336 flags := []string{"-port", strconv.Itoa(listener.Addr().(*net.TCPAddr).Port)}
David Benjamin1d5c83e2014-07-22 19:20:02 -04001337 if test.testType == serverTest {
David Benjamin5a593af2014-08-11 19:51:50 -04001338 flags = append(flags, "-server")
1339
David Benjamin025b3d32014-07-01 19:53:04 -04001340 flags = append(flags, "-key-file")
1341 if test.keyFile == "" {
1342 flags = append(flags, rsaKeyFile)
1343 } else {
1344 flags = append(flags, test.keyFile)
1345 }
1346
1347 flags = append(flags, "-cert-file")
1348 if test.certFile == "" {
1349 flags = append(flags, rsaCertificateFile)
1350 } else {
1351 flags = append(flags, test.certFile)
1352 }
1353 }
David Benjamin5a593af2014-08-11 19:51:50 -04001354
David Benjamin6fd297b2014-08-11 18:43:38 -04001355 if test.protocol == dtls {
1356 flags = append(flags, "-dtls")
1357 }
1358
David Benjamin5a593af2014-08-11 19:51:50 -04001359 if test.resumeSession {
1360 flags = append(flags, "-resume")
1361 }
1362
David Benjamine58c4f52014-08-24 03:47:07 -04001363 if test.shimWritesFirst {
1364 flags = append(flags, "-shim-writes-first")
1365 }
1366
David Benjaminc565ebb2015-04-03 04:06:36 -04001367 if test.exportKeyingMaterial > 0 {
1368 flags = append(flags, "-export-keying-material", strconv.Itoa(test.exportKeyingMaterial))
1369 flags = append(flags, "-export-label", test.exportLabel)
1370 flags = append(flags, "-export-context", test.exportContext)
1371 if test.useExportContext {
1372 flags = append(flags, "-use-export-context")
1373 }
1374 }
1375
David Benjamin025b3d32014-07-01 19:53:04 -04001376 flags = append(flags, test.flags...)
1377
1378 var shim *exec.Cmd
1379 if *useValgrind {
1380 shim = valgrindOf(false, shim_path, flags...)
Adam Langley75712922014-10-10 16:23:43 -07001381 } else if *useGDB {
1382 shim = gdbOf(shim_path, flags...)
David Benjamin025b3d32014-07-01 19:53:04 -04001383 } else {
1384 shim = exec.Command(shim_path, flags...)
1385 }
David Benjamin025b3d32014-07-01 19:53:04 -04001386 shim.Stdin = os.Stdin
1387 var stdoutBuf, stderrBuf bytes.Buffer
1388 shim.Stdout = &stdoutBuf
1389 shim.Stderr = &stderrBuf
Adam Langley69a01602014-11-17 17:26:55 -08001390 if mallocNumToFail >= 0 {
David Benjamin9e128b02015-02-09 13:13:09 -05001391 shim.Env = os.Environ()
1392 shim.Env = append(shim.Env, "MALLOC_NUMBER_TO_FAIL="+strconv.FormatInt(mallocNumToFail, 10))
Adam Langley69a01602014-11-17 17:26:55 -08001393 if *mallocTestDebug {
1394 shim.Env = append(shim.Env, "MALLOC_ABORT_ON_FAIL=1")
1395 }
1396 shim.Env = append(shim.Env, "_MALLOC_CHECK=1")
1397 }
David Benjamin025b3d32014-07-01 19:53:04 -04001398
1399 if err := shim.Start(); err != nil {
Adam Langley95c29f32014-06-20 12:00:00 -07001400 panic(err)
1401 }
David Benjamin87c8a642015-02-21 01:54:29 -05001402 waitChan := make(chan error, 1)
1403 go func() { waitChan <- shim.Wait() }()
Adam Langley95c29f32014-06-20 12:00:00 -07001404
1405 config := test.config
David Benjamin1d5c83e2014-07-22 19:20:02 -04001406 config.ClientSessionCache = NewLRUClientSessionCache(1)
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001407 config.ServerSessionCache = NewLRUServerSessionCache(1)
David Benjamin025b3d32014-07-01 19:53:04 -04001408 if test.testType == clientTest {
1409 if len(config.Certificates) == 0 {
1410 config.Certificates = []Certificate{getRSACertificate()}
1411 }
David Benjamin87c8a642015-02-21 01:54:29 -05001412 } else {
1413 // Supply a ServerName to ensure a constant session cache key,
1414 // rather than falling back to net.Conn.RemoteAddr.
1415 if len(config.ServerName) == 0 {
1416 config.ServerName = "test"
1417 }
David Benjamin025b3d32014-07-01 19:53:04 -04001418 }
Adam Langley95c29f32014-06-20 12:00:00 -07001419
David Benjamin87c8a642015-02-21 01:54:29 -05001420 conn, err := acceptOrWait(listener, waitChan)
1421 if err == nil {
1422 err = doExchange(test, &config, conn, test.messageLen, false /* not a resumption */)
1423 conn.Close()
1424 }
David Benjamin65ea8ff2014-11-23 03:01:00 -05001425
David Benjamin1d5c83e2014-07-22 19:20:02 -04001426 if err == nil && test.resumeSession {
David Benjamin01fe8202014-09-24 15:21:44 -04001427 var resumeConfig Config
1428 if test.resumeConfig != nil {
1429 resumeConfig = *test.resumeConfig
David Benjamin87c8a642015-02-21 01:54:29 -05001430 if len(resumeConfig.ServerName) == 0 {
1431 resumeConfig.ServerName = config.ServerName
1432 }
David Benjamin01fe8202014-09-24 15:21:44 -04001433 if len(resumeConfig.Certificates) == 0 {
1434 resumeConfig.Certificates = []Certificate{getRSACertificate()}
1435 }
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001436 if !test.newSessionsOnResume {
1437 resumeConfig.SessionTicketKey = config.SessionTicketKey
1438 resumeConfig.ClientSessionCache = config.ClientSessionCache
1439 resumeConfig.ServerSessionCache = config.ServerSessionCache
1440 }
David Benjamin01fe8202014-09-24 15:21:44 -04001441 } else {
1442 resumeConfig = config
1443 }
David Benjamin87c8a642015-02-21 01:54:29 -05001444 var connResume net.Conn
1445 connResume, err = acceptOrWait(listener, waitChan)
1446 if err == nil {
1447 err = doExchange(test, &resumeConfig, connResume, test.messageLen, true /* resumption */)
1448 connResume.Close()
1449 }
David Benjamin1d5c83e2014-07-22 19:20:02 -04001450 }
1451
David Benjamin87c8a642015-02-21 01:54:29 -05001452 // Close the listener now. This is to avoid hangs should the shim try to
1453 // open more connections than expected.
1454 listener.Close()
1455 listener = nil
1456
1457 childErr := <-waitChan
Adam Langley69a01602014-11-17 17:26:55 -08001458 if exitError, ok := childErr.(*exec.ExitError); ok {
1459 if exitError.Sys().(syscall.WaitStatus).ExitStatus() == 88 {
1460 return errMoreMallocs
1461 }
1462 }
Adam Langley95c29f32014-06-20 12:00:00 -07001463
1464 stdout := string(stdoutBuf.Bytes())
1465 stderr := string(stderrBuf.Bytes())
1466 failed := err != nil || childErr != nil
David Benjaminc565ebb2015-04-03 04:06:36 -04001467 correctFailure := len(test.expectedError) == 0 || strings.Contains(stderr, test.expectedError)
Adam Langleyac61fa32014-06-23 12:03:11 -07001468 localError := "none"
1469 if err != nil {
1470 localError = err.Error()
1471 }
1472 if len(test.expectedLocalError) != 0 {
1473 correctFailure = correctFailure && strings.Contains(localError, test.expectedLocalError)
1474 }
Adam Langley95c29f32014-06-20 12:00:00 -07001475
1476 if failed != test.shouldFail || failed && !correctFailure {
Adam Langley95c29f32014-06-20 12:00:00 -07001477 childError := "none"
Adam Langley95c29f32014-06-20 12:00:00 -07001478 if childErr != nil {
1479 childError = childErr.Error()
1480 }
1481
1482 var msg string
1483 switch {
1484 case failed && !test.shouldFail:
1485 msg = "unexpected failure"
1486 case !failed && test.shouldFail:
1487 msg = "unexpected success"
1488 case failed && !correctFailure:
Adam Langleyac61fa32014-06-23 12:03:11 -07001489 msg = "bad error (wanted '" + test.expectedError + "' / '" + test.expectedLocalError + "')"
Adam Langley95c29f32014-06-20 12:00:00 -07001490 default:
1491 panic("internal error")
1492 }
1493
David Benjaminc565ebb2015-04-03 04:06:36 -04001494 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 -07001495 }
1496
David Benjaminc565ebb2015-04-03 04:06:36 -04001497 if !*useValgrind && !failed && len(stderr) > 0 {
Adam Langley95c29f32014-06-20 12:00:00 -07001498 println(stderr)
1499 }
1500
1501 return nil
1502}
1503
1504var tlsVersions = []struct {
1505 name string
1506 version uint16
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001507 flag string
David Benjamin8b8c0062014-11-23 02:47:52 -05001508 hasDTLS bool
Adam Langley95c29f32014-06-20 12:00:00 -07001509}{
David Benjamin8b8c0062014-11-23 02:47:52 -05001510 {"SSL3", VersionSSL30, "-no-ssl3", false},
1511 {"TLS1", VersionTLS10, "-no-tls1", true},
1512 {"TLS11", VersionTLS11, "-no-tls11", false},
1513 {"TLS12", VersionTLS12, "-no-tls12", true},
Adam Langley95c29f32014-06-20 12:00:00 -07001514}
1515
1516var testCipherSuites = []struct {
1517 name string
1518 id uint16
1519}{
1520 {"3DES-SHA", TLS_RSA_WITH_3DES_EDE_CBC_SHA},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001521 {"AES128-GCM", TLS_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001522 {"AES128-SHA", TLS_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001523 {"AES128-SHA256", TLS_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001524 {"AES256-GCM", TLS_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001525 {"AES256-SHA", TLS_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001526 {"AES256-SHA256", TLS_RSA_WITH_AES_256_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001527 {"DHE-RSA-AES128-GCM", TLS_DHE_RSA_WITH_AES_128_GCM_SHA256},
1528 {"DHE-RSA-AES128-SHA", TLS_DHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001529 {"DHE-RSA-AES128-SHA256", TLS_DHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001530 {"DHE-RSA-AES256-GCM", TLS_DHE_RSA_WITH_AES_256_GCM_SHA384},
1531 {"DHE-RSA-AES256-SHA", TLS_DHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001532 {"DHE-RSA-AES256-SHA256", TLS_DHE_RSA_WITH_AES_256_CBC_SHA256},
David Benjamine9a80ff2015-04-07 00:46:46 -04001533 {"DHE-RSA-CHACHA20-POLY1305", TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001534 {"ECDHE-ECDSA-AES128-GCM", TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
1535 {"ECDHE-ECDSA-AES128-SHA", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001536 {"ECDHE-ECDSA-AES128-SHA256", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256},
1537 {"ECDHE-ECDSA-AES256-GCM", TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001538 {"ECDHE-ECDSA-AES256-SHA", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001539 {"ECDHE-ECDSA-AES256-SHA384", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384},
David Benjamine9a80ff2015-04-07 00:46:46 -04001540 {"ECDHE-ECDSA-CHACHA20-POLY1305", TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001541 {"ECDHE-ECDSA-RC4-SHA", TLS_ECDHE_ECDSA_WITH_RC4_128_SHA},
Adam Langley97e8ba82015-04-29 15:32:10 -07001542 {"ECDHE-PSK-AES128-GCM-SHA256", TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001543 {"ECDHE-RSA-AES128-GCM", TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001544 {"ECDHE-RSA-AES128-SHA", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001545 {"ECDHE-RSA-AES128-SHA256", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001546 {"ECDHE-RSA-AES256-GCM", TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001547 {"ECDHE-RSA-AES256-SHA", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001548 {"ECDHE-RSA-AES256-SHA384", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384},
David Benjamine9a80ff2015-04-07 00:46:46 -04001549 {"ECDHE-RSA-CHACHA20-POLY1305", TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001550 {"ECDHE-RSA-RC4-SHA", TLS_ECDHE_RSA_WITH_RC4_128_SHA},
David Benjamin48cae082014-10-27 01:06:24 -04001551 {"PSK-AES128-CBC-SHA", TLS_PSK_WITH_AES_128_CBC_SHA},
1552 {"PSK-AES256-CBC-SHA", TLS_PSK_WITH_AES_256_CBC_SHA},
1553 {"PSK-RC4-SHA", TLS_PSK_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -07001554 {"RC4-MD5", TLS_RSA_WITH_RC4_128_MD5},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001555 {"RC4-SHA", TLS_RSA_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -07001556}
1557
David Benjamin8b8c0062014-11-23 02:47:52 -05001558func hasComponent(suiteName, component string) bool {
1559 return strings.Contains("-"+suiteName+"-", "-"+component+"-")
1560}
1561
David Benjaminf7768e42014-08-31 02:06:47 -04001562func isTLS12Only(suiteName string) bool {
David Benjamin8b8c0062014-11-23 02:47:52 -05001563 return hasComponent(suiteName, "GCM") ||
1564 hasComponent(suiteName, "SHA256") ||
David Benjamine9a80ff2015-04-07 00:46:46 -04001565 hasComponent(suiteName, "SHA384") ||
1566 hasComponent(suiteName, "POLY1305")
David Benjamin8b8c0062014-11-23 02:47:52 -05001567}
1568
1569func isDTLSCipher(suiteName string) bool {
David Benjamine95d20d2014-12-23 11:16:01 -05001570 return !hasComponent(suiteName, "RC4")
David Benjaminf7768e42014-08-31 02:06:47 -04001571}
1572
Adam Langleya7997f12015-05-14 17:38:50 -07001573func bigFromHex(hex string) *big.Int {
1574 ret, ok := new(big.Int).SetString(hex, 16)
1575 if !ok {
1576 panic("failed to parse hex number 0x" + hex)
1577 }
1578 return ret
1579}
1580
Adam Langley95c29f32014-06-20 12:00:00 -07001581func addCipherSuiteTests() {
1582 for _, suite := range testCipherSuites {
David Benjamin48cae082014-10-27 01:06:24 -04001583 const psk = "12345"
1584 const pskIdentity = "luggage combo"
1585
Adam Langley95c29f32014-06-20 12:00:00 -07001586 var cert Certificate
David Benjamin025b3d32014-07-01 19:53:04 -04001587 var certFile string
1588 var keyFile string
David Benjamin8b8c0062014-11-23 02:47:52 -05001589 if hasComponent(suite.name, "ECDSA") {
Adam Langley95c29f32014-06-20 12:00:00 -07001590 cert = getECDSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -04001591 certFile = ecdsaCertificateFile
1592 keyFile = ecdsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -07001593 } else {
1594 cert = getRSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -04001595 certFile = rsaCertificateFile
1596 keyFile = rsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -07001597 }
1598
David Benjamin48cae082014-10-27 01:06:24 -04001599 var flags []string
David Benjamin8b8c0062014-11-23 02:47:52 -05001600 if hasComponent(suite.name, "PSK") {
David Benjamin48cae082014-10-27 01:06:24 -04001601 flags = append(flags,
1602 "-psk", psk,
1603 "-psk-identity", pskIdentity)
1604 }
1605
Adam Langley95c29f32014-06-20 12:00:00 -07001606 for _, ver := range tlsVersions {
David Benjaminf7768e42014-08-31 02:06:47 -04001607 if ver.version < VersionTLS12 && isTLS12Only(suite.name) {
Adam Langley95c29f32014-06-20 12:00:00 -07001608 continue
1609 }
1610
David Benjamin025b3d32014-07-01 19:53:04 -04001611 testCases = append(testCases, testCase{
1612 testType: clientTest,
1613 name: ver.name + "-" + suite.name + "-client",
Adam Langley95c29f32014-06-20 12:00:00 -07001614 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001615 MinVersion: ver.version,
1616 MaxVersion: ver.version,
1617 CipherSuites: []uint16{suite.id},
1618 Certificates: []Certificate{cert},
David Benjamin68793732015-05-04 20:20:48 -04001619 PreSharedKey: []byte(psk),
David Benjamin48cae082014-10-27 01:06:24 -04001620 PreSharedKeyIdentity: pskIdentity,
Adam Langley95c29f32014-06-20 12:00:00 -07001621 },
David Benjamin48cae082014-10-27 01:06:24 -04001622 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001623 resumeSession: true,
Adam Langley95c29f32014-06-20 12:00:00 -07001624 })
David Benjamin025b3d32014-07-01 19:53:04 -04001625
David Benjamin76d8abe2014-08-14 16:25:34 -04001626 testCases = append(testCases, testCase{
1627 testType: serverTest,
1628 name: ver.name + "-" + suite.name + "-server",
1629 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001630 MinVersion: ver.version,
1631 MaxVersion: ver.version,
1632 CipherSuites: []uint16{suite.id},
1633 Certificates: []Certificate{cert},
1634 PreSharedKey: []byte(psk),
1635 PreSharedKeyIdentity: pskIdentity,
David Benjamin76d8abe2014-08-14 16:25:34 -04001636 },
1637 certFile: certFile,
1638 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001639 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001640 resumeSession: true,
David Benjamin76d8abe2014-08-14 16:25:34 -04001641 })
David Benjamin6fd297b2014-08-11 18:43:38 -04001642
David Benjamin8b8c0062014-11-23 02:47:52 -05001643 if ver.hasDTLS && isDTLSCipher(suite.name) {
David Benjamin6fd297b2014-08-11 18:43:38 -04001644 testCases = append(testCases, testCase{
1645 testType: clientTest,
1646 protocol: dtls,
1647 name: "D" + ver.name + "-" + suite.name + "-client",
1648 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001649 MinVersion: ver.version,
1650 MaxVersion: ver.version,
1651 CipherSuites: []uint16{suite.id},
1652 Certificates: []Certificate{cert},
1653 PreSharedKey: []byte(psk),
1654 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001655 },
David Benjamin48cae082014-10-27 01:06:24 -04001656 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001657 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001658 })
1659 testCases = append(testCases, testCase{
1660 testType: serverTest,
1661 protocol: dtls,
1662 name: "D" + ver.name + "-" + suite.name + "-server",
1663 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001664 MinVersion: ver.version,
1665 MaxVersion: ver.version,
1666 CipherSuites: []uint16{suite.id},
1667 Certificates: []Certificate{cert},
1668 PreSharedKey: []byte(psk),
1669 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001670 },
1671 certFile: certFile,
1672 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001673 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001674 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001675 })
1676 }
Adam Langley95c29f32014-06-20 12:00:00 -07001677 }
1678 }
Adam Langleya7997f12015-05-14 17:38:50 -07001679
1680 testCases = append(testCases, testCase{
1681 name: "WeakDH",
1682 config: Config{
1683 CipherSuites: []uint16{TLS_DHE_RSA_WITH_AES_128_GCM_SHA256},
1684 Bugs: ProtocolBugs{
1685 // This is a 1023-bit prime number, generated
1686 // with:
1687 // openssl gendh 1023 | openssl asn1parse -i
1688 DHGroupPrime: bigFromHex("518E9B7930CE61C6E445C8360584E5FC78D9137C0FFDC880B495D5338ADF7689951A6821C17A76B3ACB8E0156AEA607B7EC406EBEDBB84D8376EB8FE8F8BA1433488BEE0C3EDDFD3A32DBB9481980A7AF6C96BFCF490A094CFFB2B8192C1BB5510B77B658436E27C2D4D023FE3718222AB0CA1273995B51F6D625A4944D0DD4B"),
1689 },
1690 },
1691 shouldFail: true,
1692 expectedError: "BAD_DH_P_LENGTH",
1693 })
Adam Langley95c29f32014-06-20 12:00:00 -07001694}
1695
1696func addBadECDSASignatureTests() {
1697 for badR := BadValue(1); badR < NumBadValues; badR++ {
1698 for badS := BadValue(1); badS < NumBadValues; badS++ {
David Benjamin025b3d32014-07-01 19:53:04 -04001699 testCases = append(testCases, testCase{
Adam Langley95c29f32014-06-20 12:00:00 -07001700 name: fmt.Sprintf("BadECDSA-%d-%d", badR, badS),
1701 config: Config{
1702 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
1703 Certificates: []Certificate{getECDSACertificate()},
1704 Bugs: ProtocolBugs{
1705 BadECDSAR: badR,
1706 BadECDSAS: badS,
1707 },
1708 },
1709 shouldFail: true,
1710 expectedError: "SIGNATURE",
1711 })
1712 }
1713 }
1714}
1715
Adam Langley80842bd2014-06-20 12:00:00 -07001716func addCBCPaddingTests() {
David Benjamin025b3d32014-07-01 19:53:04 -04001717 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001718 name: "MaxCBCPadding",
1719 config: Config{
1720 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1721 Bugs: ProtocolBugs{
1722 MaxPadding: true,
1723 },
1724 },
1725 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1726 })
David Benjamin025b3d32014-07-01 19:53:04 -04001727 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001728 name: "BadCBCPadding",
1729 config: Config{
1730 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1731 Bugs: ProtocolBugs{
1732 PaddingFirstByteBad: true,
1733 },
1734 },
1735 shouldFail: true,
1736 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1737 })
1738 // OpenSSL previously had an issue where the first byte of padding in
1739 // 255 bytes of padding wasn't checked.
David Benjamin025b3d32014-07-01 19:53:04 -04001740 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001741 name: "BadCBCPadding255",
1742 config: Config{
1743 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1744 Bugs: ProtocolBugs{
1745 MaxPadding: true,
1746 PaddingFirstByteBadIf255: true,
1747 },
1748 },
1749 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1750 shouldFail: true,
1751 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1752 })
1753}
1754
Kenny Root7fdeaf12014-08-05 15:23:37 -07001755func addCBCSplittingTests() {
1756 testCases = append(testCases, testCase{
1757 name: "CBCRecordSplitting",
1758 config: Config{
1759 MaxVersion: VersionTLS10,
1760 MinVersion: VersionTLS10,
1761 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1762 },
1763 messageLen: -1, // read until EOF
1764 flags: []string{
1765 "-async",
1766 "-write-different-record-sizes",
1767 "-cbc-record-splitting",
1768 },
David Benjamina8e3e0e2014-08-06 22:11:10 -04001769 })
1770 testCases = append(testCases, testCase{
Kenny Root7fdeaf12014-08-05 15:23:37 -07001771 name: "CBCRecordSplittingPartialWrite",
1772 config: Config{
1773 MaxVersion: VersionTLS10,
1774 MinVersion: VersionTLS10,
1775 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1776 },
1777 messageLen: -1, // read until EOF
1778 flags: []string{
1779 "-async",
1780 "-write-different-record-sizes",
1781 "-cbc-record-splitting",
1782 "-partial-write",
1783 },
1784 })
1785}
1786
David Benjamin636293b2014-07-08 17:59:18 -04001787func addClientAuthTests() {
David Benjamin407a10c2014-07-16 12:58:59 -04001788 // Add a dummy cert pool to stress certificate authority parsing.
1789 // TODO(davidben): Add tests that those values parse out correctly.
1790 certPool := x509.NewCertPool()
1791 cert, err := x509.ParseCertificate(rsaCertificate.Certificate[0])
1792 if err != nil {
1793 panic(err)
1794 }
1795 certPool.AddCert(cert)
1796
David Benjamin636293b2014-07-08 17:59:18 -04001797 for _, ver := range tlsVersions {
David Benjamin636293b2014-07-08 17:59:18 -04001798 testCases = append(testCases, testCase{
1799 testType: clientTest,
David Benjamin67666e72014-07-12 15:47:52 -04001800 name: ver.name + "-Client-ClientAuth-RSA",
David Benjamin636293b2014-07-08 17:59:18 -04001801 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001802 MinVersion: ver.version,
1803 MaxVersion: ver.version,
1804 ClientAuth: RequireAnyClientCert,
1805 ClientCAs: certPool,
David Benjamin636293b2014-07-08 17:59:18 -04001806 },
1807 flags: []string{
1808 "-cert-file", rsaCertificateFile,
1809 "-key-file", rsaKeyFile,
1810 },
1811 })
1812 testCases = append(testCases, testCase{
David Benjamin67666e72014-07-12 15:47:52 -04001813 testType: serverTest,
1814 name: ver.name + "-Server-ClientAuth-RSA",
1815 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001816 MinVersion: ver.version,
1817 MaxVersion: ver.version,
David Benjamin67666e72014-07-12 15:47:52 -04001818 Certificates: []Certificate{rsaCertificate},
1819 },
1820 flags: []string{"-require-any-client-certificate"},
1821 })
David Benjamine098ec22014-08-27 23:13:20 -04001822 if ver.version != VersionSSL30 {
1823 testCases = append(testCases, testCase{
1824 testType: serverTest,
1825 name: ver.name + "-Server-ClientAuth-ECDSA",
1826 config: Config{
1827 MinVersion: ver.version,
1828 MaxVersion: ver.version,
1829 Certificates: []Certificate{ecdsaCertificate},
1830 },
1831 flags: []string{"-require-any-client-certificate"},
1832 })
1833 testCases = append(testCases, testCase{
1834 testType: clientTest,
1835 name: ver.name + "-Client-ClientAuth-ECDSA",
1836 config: Config{
1837 MinVersion: ver.version,
1838 MaxVersion: ver.version,
1839 ClientAuth: RequireAnyClientCert,
1840 ClientCAs: certPool,
1841 },
1842 flags: []string{
1843 "-cert-file", ecdsaCertificateFile,
1844 "-key-file", ecdsaKeyFile,
1845 },
1846 })
1847 }
David Benjamin636293b2014-07-08 17:59:18 -04001848 }
1849}
1850
Adam Langley75712922014-10-10 16:23:43 -07001851func addExtendedMasterSecretTests() {
1852 const expectEMSFlag = "-expect-extended-master-secret"
1853
1854 for _, with := range []bool{false, true} {
1855 prefix := "No"
1856 var flags []string
1857 if with {
1858 prefix = ""
1859 flags = []string{expectEMSFlag}
1860 }
1861
1862 for _, isClient := range []bool{false, true} {
1863 suffix := "-Server"
1864 testType := serverTest
1865 if isClient {
1866 suffix = "-Client"
1867 testType = clientTest
1868 }
1869
1870 for _, ver := range tlsVersions {
1871 test := testCase{
1872 testType: testType,
1873 name: prefix + "ExtendedMasterSecret-" + ver.name + suffix,
1874 config: Config{
1875 MinVersion: ver.version,
1876 MaxVersion: ver.version,
1877 Bugs: ProtocolBugs{
1878 NoExtendedMasterSecret: !with,
1879 RequireExtendedMasterSecret: with,
1880 },
1881 },
David Benjamin48cae082014-10-27 01:06:24 -04001882 flags: flags,
1883 shouldFail: ver.version == VersionSSL30 && with,
Adam Langley75712922014-10-10 16:23:43 -07001884 }
1885 if test.shouldFail {
1886 test.expectedLocalError = "extended master secret required but not supported by peer"
1887 }
1888 testCases = append(testCases, test)
1889 }
1890 }
1891 }
1892
1893 // When a session is resumed, it should still be aware that its master
1894 // secret was generated via EMS and thus it's safe to use tls-unique.
1895 testCases = append(testCases, testCase{
1896 name: "ExtendedMasterSecret-Resume",
1897 config: Config{
1898 Bugs: ProtocolBugs{
1899 RequireExtendedMasterSecret: true,
1900 },
1901 },
1902 flags: []string{expectEMSFlag},
1903 resumeSession: true,
1904 })
1905}
1906
David Benjamin43ec06f2014-08-05 02:28:57 -04001907// Adds tests that try to cover the range of the handshake state machine, under
1908// various conditions. Some of these are redundant with other tests, but they
1909// only cover the synchronous case.
David Benjamin6fd297b2014-08-11 18:43:38 -04001910func addStateMachineCoverageTests(async, splitHandshake bool, protocol protocol) {
David Benjamin760b1dd2015-05-15 23:33:48 -04001911 var tests []testCase
1912
1913 // Basic handshake, with resumption. Client and server,
1914 // session ID and session ticket.
1915 tests = append(tests, testCase{
1916 name: "Basic-Client",
1917 resumeSession: true,
1918 })
1919 tests = append(tests, testCase{
1920 name: "Basic-Client-RenewTicket",
1921 config: Config{
1922 Bugs: ProtocolBugs{
1923 RenewTicketOnResume: true,
1924 },
1925 },
1926 resumeSession: true,
1927 })
1928 tests = append(tests, testCase{
1929 name: "Basic-Client-NoTicket",
1930 config: Config{
1931 SessionTicketsDisabled: true,
1932 },
1933 resumeSession: true,
1934 })
1935 tests = append(tests, testCase{
1936 name: "Basic-Client-Implicit",
1937 flags: []string{"-implicit-handshake"},
1938 resumeSession: true,
1939 })
1940 tests = append(tests, testCase{
1941 testType: serverTest,
1942 name: "Basic-Server",
1943 resumeSession: true,
1944 })
1945 tests = append(tests, testCase{
1946 testType: serverTest,
1947 name: "Basic-Server-NoTickets",
1948 config: Config{
1949 SessionTicketsDisabled: true,
1950 },
1951 resumeSession: true,
1952 })
1953 tests = append(tests, testCase{
1954 testType: serverTest,
1955 name: "Basic-Server-Implicit",
1956 flags: []string{"-implicit-handshake"},
1957 resumeSession: true,
1958 })
1959 tests = append(tests, testCase{
1960 testType: serverTest,
1961 name: "Basic-Server-EarlyCallback",
1962 flags: []string{"-use-early-callback"},
1963 resumeSession: true,
1964 })
1965
1966 // TLS client auth.
1967 tests = append(tests, testCase{
1968 testType: clientTest,
1969 name: "ClientAuth-Client",
1970 config: Config{
1971 ClientAuth: RequireAnyClientCert,
1972 },
1973 flags: []string{
1974 "-cert-file", rsaCertificateFile,
1975 "-key-file", rsaKeyFile,
1976 },
1977 })
1978 tests = append(tests, testCase{
1979 testType: serverTest,
1980 name: "ClientAuth-Server",
1981 config: Config{
1982 Certificates: []Certificate{rsaCertificate},
1983 },
1984 flags: []string{"-require-any-client-certificate"},
1985 })
1986
1987 // No session ticket support; server doesn't send NewSessionTicket.
1988 tests = append(tests, testCase{
1989 name: "SessionTicketsDisabled-Client",
1990 config: Config{
1991 SessionTicketsDisabled: true,
1992 },
1993 })
1994 tests = append(tests, testCase{
1995 testType: serverTest,
1996 name: "SessionTicketsDisabled-Server",
1997 config: Config{
1998 SessionTicketsDisabled: true,
1999 },
2000 })
2001
2002 // Skip ServerKeyExchange in PSK key exchange if there's no
2003 // identity hint.
2004 tests = append(tests, testCase{
2005 name: "EmptyPSKHint-Client",
2006 config: Config{
2007 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
2008 PreSharedKey: []byte("secret"),
2009 },
2010 flags: []string{"-psk", "secret"},
2011 })
2012 tests = append(tests, testCase{
2013 testType: serverTest,
2014 name: "EmptyPSKHint-Server",
2015 config: Config{
2016 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
2017 PreSharedKey: []byte("secret"),
2018 },
2019 flags: []string{"-psk", "secret"},
2020 })
2021
2022 if protocol == tls {
2023 tests = append(tests, testCase{
2024 name: "Renegotiate-Client",
2025 renegotiate: true,
2026 })
2027 // NPN on client and server; results in post-handshake message.
2028 tests = append(tests, testCase{
2029 name: "NPN-Client",
2030 config: Config{
2031 NextProtos: []string{"foo"},
2032 },
2033 flags: []string{"-select-next-proto", "foo"},
2034 expectedNextProto: "foo",
2035 expectedNextProtoType: npn,
2036 })
2037 tests = append(tests, testCase{
2038 testType: serverTest,
2039 name: "NPN-Server",
2040 config: Config{
2041 NextProtos: []string{"bar"},
2042 },
2043 flags: []string{
2044 "-advertise-npn", "\x03foo\x03bar\x03baz",
2045 "-expect-next-proto", "bar",
2046 },
2047 expectedNextProto: "bar",
2048 expectedNextProtoType: npn,
2049 })
2050
2051 // TODO(davidben): Add tests for when False Start doesn't trigger.
2052
2053 // Client does False Start and negotiates NPN.
2054 tests = append(tests, testCase{
2055 name: "FalseStart",
2056 config: Config{
2057 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2058 NextProtos: []string{"foo"},
2059 Bugs: ProtocolBugs{
2060 ExpectFalseStart: true,
2061 },
2062 },
2063 flags: []string{
2064 "-false-start",
2065 "-select-next-proto", "foo",
2066 },
2067 shimWritesFirst: true,
2068 resumeSession: true,
2069 })
2070
2071 // Client does False Start and negotiates ALPN.
2072 tests = append(tests, testCase{
2073 name: "FalseStart-ALPN",
2074 config: Config{
2075 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2076 NextProtos: []string{"foo"},
2077 Bugs: ProtocolBugs{
2078 ExpectFalseStart: true,
2079 },
2080 },
2081 flags: []string{
2082 "-false-start",
2083 "-advertise-alpn", "\x03foo",
2084 },
2085 shimWritesFirst: true,
2086 resumeSession: true,
2087 })
2088
2089 // Client does False Start but doesn't explicitly call
2090 // SSL_connect.
2091 tests = append(tests, testCase{
2092 name: "FalseStart-Implicit",
2093 config: Config{
2094 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2095 NextProtos: []string{"foo"},
2096 },
2097 flags: []string{
2098 "-implicit-handshake",
2099 "-false-start",
2100 "-advertise-alpn", "\x03foo",
2101 },
2102 })
2103
2104 // False Start without session tickets.
2105 tests = append(tests, testCase{
2106 name: "FalseStart-SessionTicketsDisabled",
2107 config: Config{
2108 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2109 NextProtos: []string{"foo"},
2110 SessionTicketsDisabled: true,
2111 Bugs: ProtocolBugs{
2112 ExpectFalseStart: true,
2113 },
2114 },
2115 flags: []string{
2116 "-false-start",
2117 "-select-next-proto", "foo",
2118 },
2119 shimWritesFirst: true,
2120 })
2121
2122 // Server parses a V2ClientHello.
2123 tests = append(tests, testCase{
2124 testType: serverTest,
2125 name: "SendV2ClientHello",
2126 config: Config{
2127 // Choose a cipher suite that does not involve
2128 // elliptic curves, so no extensions are
2129 // involved.
2130 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2131 Bugs: ProtocolBugs{
2132 SendV2ClientHello: true,
2133 },
2134 },
2135 })
2136
2137 // Client sends a Channel ID.
2138 tests = append(tests, testCase{
2139 name: "ChannelID-Client",
2140 config: Config{
2141 RequestChannelID: true,
2142 },
2143 flags: []string{"-send-channel-id", channelIDKeyFile},
2144 resumeSession: true,
2145 expectChannelID: true,
2146 })
2147
2148 // Server accepts a Channel ID.
2149 tests = append(tests, testCase{
2150 testType: serverTest,
2151 name: "ChannelID-Server",
2152 config: Config{
2153 ChannelID: channelIDKey,
2154 },
2155 flags: []string{
2156 "-expect-channel-id",
2157 base64.StdEncoding.EncodeToString(channelIDBytes),
2158 },
2159 resumeSession: true,
2160 expectChannelID: true,
2161 })
2162 } else {
2163 tests = append(tests, testCase{
2164 name: "SkipHelloVerifyRequest",
2165 config: Config{
2166 Bugs: ProtocolBugs{
2167 SkipHelloVerifyRequest: true,
2168 },
2169 },
2170 })
2171 }
2172
David Benjamin43ec06f2014-08-05 02:28:57 -04002173 var suffix string
2174 var flags []string
2175 var maxHandshakeRecordLength int
David Benjamin6fd297b2014-08-11 18:43:38 -04002176 if protocol == dtls {
2177 suffix = "-DTLS"
2178 }
David Benjamin43ec06f2014-08-05 02:28:57 -04002179 if async {
David Benjamin6fd297b2014-08-11 18:43:38 -04002180 suffix += "-Async"
David Benjamin43ec06f2014-08-05 02:28:57 -04002181 flags = append(flags, "-async")
2182 } else {
David Benjamin6fd297b2014-08-11 18:43:38 -04002183 suffix += "-Sync"
David Benjamin43ec06f2014-08-05 02:28:57 -04002184 }
2185 if splitHandshake {
2186 suffix += "-SplitHandshakeRecords"
David Benjamin98214542014-08-07 18:02:39 -04002187 maxHandshakeRecordLength = 1
David Benjamin43ec06f2014-08-05 02:28:57 -04002188 }
David Benjamin760b1dd2015-05-15 23:33:48 -04002189 for _, test := range tests {
2190 test.protocol = protocol
2191 test.name += suffix
2192 test.config.Bugs.MaxHandshakeRecordLength = maxHandshakeRecordLength
2193 test.flags = append(test.flags, flags...)
2194 testCases = append(testCases, test)
David Benjamin6fd297b2014-08-11 18:43:38 -04002195 }
David Benjamin43ec06f2014-08-05 02:28:57 -04002196}
2197
Adam Langley524e7172015-02-20 16:04:00 -08002198func addDDoSCallbackTests() {
2199 // DDoS callback.
2200
2201 for _, resume := range []bool{false, true} {
2202 suffix := "Resume"
2203 if resume {
2204 suffix = "No" + suffix
2205 }
2206
2207 testCases = append(testCases, testCase{
2208 testType: serverTest,
2209 name: "Server-DDoS-OK-" + suffix,
2210 flags: []string{"-install-ddos-callback"},
2211 resumeSession: resume,
2212 })
2213
2214 failFlag := "-fail-ddos-callback"
2215 if resume {
2216 failFlag = "-fail-second-ddos-callback"
2217 }
2218 testCases = append(testCases, testCase{
2219 testType: serverTest,
2220 name: "Server-DDoS-Reject-" + suffix,
2221 flags: []string{"-install-ddos-callback", failFlag},
2222 resumeSession: resume,
2223 shouldFail: true,
2224 expectedError: ":CONNECTION_REJECTED:",
2225 })
2226 }
2227}
2228
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002229func addVersionNegotiationTests() {
2230 for i, shimVers := range tlsVersions {
2231 // Assemble flags to disable all newer versions on the shim.
2232 var flags []string
2233 for _, vers := range tlsVersions[i+1:] {
2234 flags = append(flags, vers.flag)
2235 }
2236
2237 for _, runnerVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05002238 protocols := []protocol{tls}
2239 if runnerVers.hasDTLS && shimVers.hasDTLS {
2240 protocols = append(protocols, dtls)
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002241 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002242 for _, protocol := range protocols {
2243 expectedVersion := shimVers.version
2244 if runnerVers.version < shimVers.version {
2245 expectedVersion = runnerVers.version
2246 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002247
David Benjamin8b8c0062014-11-23 02:47:52 -05002248 suffix := shimVers.name + "-" + runnerVers.name
2249 if protocol == dtls {
2250 suffix += "-DTLS"
2251 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002252
David Benjamin1eb367c2014-12-12 18:17:51 -05002253 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
2254
David Benjamin1e29a6b2014-12-10 02:27:24 -05002255 clientVers := shimVers.version
2256 if clientVers > VersionTLS10 {
2257 clientVers = VersionTLS10
2258 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002259 testCases = append(testCases, testCase{
2260 protocol: protocol,
2261 testType: clientTest,
2262 name: "VersionNegotiation-Client-" + suffix,
2263 config: Config{
2264 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05002265 Bugs: ProtocolBugs{
2266 ExpectInitialRecordVersion: clientVers,
2267 },
David Benjamin8b8c0062014-11-23 02:47:52 -05002268 },
2269 flags: flags,
2270 expectedVersion: expectedVersion,
2271 })
David Benjamin1eb367c2014-12-12 18:17:51 -05002272 testCases = append(testCases, testCase{
2273 protocol: protocol,
2274 testType: clientTest,
2275 name: "VersionNegotiation-Client2-" + suffix,
2276 config: Config{
2277 MaxVersion: runnerVers.version,
2278 Bugs: ProtocolBugs{
2279 ExpectInitialRecordVersion: clientVers,
2280 },
2281 },
2282 flags: []string{"-max-version", shimVersFlag},
2283 expectedVersion: expectedVersion,
2284 })
David Benjamin8b8c0062014-11-23 02:47:52 -05002285
2286 testCases = append(testCases, testCase{
2287 protocol: protocol,
2288 testType: serverTest,
2289 name: "VersionNegotiation-Server-" + suffix,
2290 config: Config{
2291 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05002292 Bugs: ProtocolBugs{
2293 ExpectInitialRecordVersion: expectedVersion,
2294 },
David Benjamin8b8c0062014-11-23 02:47:52 -05002295 },
2296 flags: flags,
2297 expectedVersion: expectedVersion,
2298 })
David Benjamin1eb367c2014-12-12 18:17:51 -05002299 testCases = append(testCases, testCase{
2300 protocol: protocol,
2301 testType: serverTest,
2302 name: "VersionNegotiation-Server2-" + suffix,
2303 config: Config{
2304 MaxVersion: runnerVers.version,
2305 Bugs: ProtocolBugs{
2306 ExpectInitialRecordVersion: expectedVersion,
2307 },
2308 },
2309 flags: []string{"-max-version", shimVersFlag},
2310 expectedVersion: expectedVersion,
2311 })
David Benjamin8b8c0062014-11-23 02:47:52 -05002312 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002313 }
2314 }
2315}
2316
David Benjaminaccb4542014-12-12 23:44:33 -05002317func addMinimumVersionTests() {
2318 for i, shimVers := range tlsVersions {
2319 // Assemble flags to disable all older versions on the shim.
2320 var flags []string
2321 for _, vers := range tlsVersions[:i] {
2322 flags = append(flags, vers.flag)
2323 }
2324
2325 for _, runnerVers := range tlsVersions {
2326 protocols := []protocol{tls}
2327 if runnerVers.hasDTLS && shimVers.hasDTLS {
2328 protocols = append(protocols, dtls)
2329 }
2330 for _, protocol := range protocols {
2331 suffix := shimVers.name + "-" + runnerVers.name
2332 if protocol == dtls {
2333 suffix += "-DTLS"
2334 }
2335 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
2336
David Benjaminaccb4542014-12-12 23:44:33 -05002337 var expectedVersion uint16
2338 var shouldFail bool
2339 var expectedError string
David Benjamin87909c02014-12-13 01:55:01 -05002340 var expectedLocalError string
David Benjaminaccb4542014-12-12 23:44:33 -05002341 if runnerVers.version >= shimVers.version {
2342 expectedVersion = runnerVers.version
2343 } else {
2344 shouldFail = true
2345 expectedError = ":UNSUPPORTED_PROTOCOL:"
David Benjamin87909c02014-12-13 01:55:01 -05002346 if runnerVers.version > VersionSSL30 {
2347 expectedLocalError = "remote error: protocol version not supported"
2348 } else {
2349 expectedLocalError = "remote error: handshake failure"
2350 }
David Benjaminaccb4542014-12-12 23:44:33 -05002351 }
2352
2353 testCases = append(testCases, testCase{
2354 protocol: protocol,
2355 testType: clientTest,
2356 name: "MinimumVersion-Client-" + suffix,
2357 config: Config{
2358 MaxVersion: runnerVers.version,
2359 },
David Benjamin87909c02014-12-13 01:55:01 -05002360 flags: flags,
2361 expectedVersion: expectedVersion,
2362 shouldFail: shouldFail,
2363 expectedError: expectedError,
2364 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002365 })
2366 testCases = append(testCases, testCase{
2367 protocol: protocol,
2368 testType: clientTest,
2369 name: "MinimumVersion-Client2-" + suffix,
2370 config: Config{
2371 MaxVersion: runnerVers.version,
2372 },
David Benjamin87909c02014-12-13 01:55:01 -05002373 flags: []string{"-min-version", shimVersFlag},
2374 expectedVersion: expectedVersion,
2375 shouldFail: shouldFail,
2376 expectedError: expectedError,
2377 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002378 })
2379
2380 testCases = append(testCases, testCase{
2381 protocol: protocol,
2382 testType: serverTest,
2383 name: "MinimumVersion-Server-" + suffix,
2384 config: Config{
2385 MaxVersion: runnerVers.version,
2386 },
David Benjamin87909c02014-12-13 01:55:01 -05002387 flags: flags,
2388 expectedVersion: expectedVersion,
2389 shouldFail: shouldFail,
2390 expectedError: expectedError,
2391 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002392 })
2393 testCases = append(testCases, testCase{
2394 protocol: protocol,
2395 testType: serverTest,
2396 name: "MinimumVersion-Server2-" + suffix,
2397 config: Config{
2398 MaxVersion: runnerVers.version,
2399 },
David Benjamin87909c02014-12-13 01:55:01 -05002400 flags: []string{"-min-version", shimVersFlag},
2401 expectedVersion: expectedVersion,
2402 shouldFail: shouldFail,
2403 expectedError: expectedError,
2404 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002405 })
2406 }
2407 }
2408 }
2409}
2410
David Benjamin5c24a1d2014-08-31 00:59:27 -04002411func addD5BugTests() {
2412 testCases = append(testCases, testCase{
2413 testType: serverTest,
2414 name: "D5Bug-NoQuirk-Reject",
2415 config: Config{
2416 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2417 Bugs: ProtocolBugs{
2418 SSL3RSAKeyExchange: true,
2419 },
2420 },
2421 shouldFail: true,
2422 expectedError: ":TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG:",
2423 })
2424 testCases = append(testCases, testCase{
2425 testType: serverTest,
2426 name: "D5Bug-Quirk-Normal",
2427 config: Config{
2428 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2429 },
2430 flags: []string{"-tls-d5-bug"},
2431 })
2432 testCases = append(testCases, testCase{
2433 testType: serverTest,
2434 name: "D5Bug-Quirk-Bug",
2435 config: Config{
2436 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2437 Bugs: ProtocolBugs{
2438 SSL3RSAKeyExchange: true,
2439 },
2440 },
2441 flags: []string{"-tls-d5-bug"},
2442 })
2443}
2444
David Benjamine78bfde2014-09-06 12:45:15 -04002445func addExtensionTests() {
2446 testCases = append(testCases, testCase{
2447 testType: clientTest,
2448 name: "DuplicateExtensionClient",
2449 config: Config{
2450 Bugs: ProtocolBugs{
2451 DuplicateExtension: true,
2452 },
2453 },
2454 shouldFail: true,
2455 expectedLocalError: "remote error: error decoding message",
2456 })
2457 testCases = append(testCases, testCase{
2458 testType: serverTest,
2459 name: "DuplicateExtensionServer",
2460 config: Config{
2461 Bugs: ProtocolBugs{
2462 DuplicateExtension: true,
2463 },
2464 },
2465 shouldFail: true,
2466 expectedLocalError: "remote error: error decoding message",
2467 })
2468 testCases = append(testCases, testCase{
2469 testType: clientTest,
2470 name: "ServerNameExtensionClient",
2471 config: Config{
2472 Bugs: ProtocolBugs{
2473 ExpectServerName: "example.com",
2474 },
2475 },
2476 flags: []string{"-host-name", "example.com"},
2477 })
2478 testCases = append(testCases, testCase{
2479 testType: clientTest,
David Benjamin5f237bc2015-02-11 17:14:15 -05002480 name: "ServerNameExtensionClientMismatch",
David Benjamine78bfde2014-09-06 12:45:15 -04002481 config: Config{
2482 Bugs: ProtocolBugs{
2483 ExpectServerName: "mismatch.com",
2484 },
2485 },
2486 flags: []string{"-host-name", "example.com"},
2487 shouldFail: true,
2488 expectedLocalError: "tls: unexpected server name",
2489 })
2490 testCases = append(testCases, testCase{
2491 testType: clientTest,
David Benjamin5f237bc2015-02-11 17:14:15 -05002492 name: "ServerNameExtensionClientMissing",
David Benjamine78bfde2014-09-06 12:45:15 -04002493 config: Config{
2494 Bugs: ProtocolBugs{
2495 ExpectServerName: "missing.com",
2496 },
2497 },
2498 shouldFail: true,
2499 expectedLocalError: "tls: unexpected server name",
2500 })
2501 testCases = append(testCases, testCase{
2502 testType: serverTest,
2503 name: "ServerNameExtensionServer",
2504 config: Config{
2505 ServerName: "example.com",
2506 },
2507 flags: []string{"-expect-server-name", "example.com"},
2508 resumeSession: true,
2509 })
David Benjaminae2888f2014-09-06 12:58:58 -04002510 testCases = append(testCases, testCase{
2511 testType: clientTest,
2512 name: "ALPNClient",
2513 config: Config{
2514 NextProtos: []string{"foo"},
2515 },
2516 flags: []string{
2517 "-advertise-alpn", "\x03foo\x03bar\x03baz",
2518 "-expect-alpn", "foo",
2519 },
David Benjaminfc7b0862014-09-06 13:21:53 -04002520 expectedNextProto: "foo",
2521 expectedNextProtoType: alpn,
2522 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04002523 })
2524 testCases = append(testCases, testCase{
2525 testType: serverTest,
2526 name: "ALPNServer",
2527 config: Config{
2528 NextProtos: []string{"foo", "bar", "baz"},
2529 },
2530 flags: []string{
2531 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2532 "-select-alpn", "foo",
2533 },
David Benjaminfc7b0862014-09-06 13:21:53 -04002534 expectedNextProto: "foo",
2535 expectedNextProtoType: alpn,
2536 resumeSession: true,
2537 })
2538 // Test that the server prefers ALPN over NPN.
2539 testCases = append(testCases, testCase{
2540 testType: serverTest,
2541 name: "ALPNServer-Preferred",
2542 config: Config{
2543 NextProtos: []string{"foo", "bar", "baz"},
2544 },
2545 flags: []string{
2546 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2547 "-select-alpn", "foo",
2548 "-advertise-npn", "\x03foo\x03bar\x03baz",
2549 },
2550 expectedNextProto: "foo",
2551 expectedNextProtoType: alpn,
2552 resumeSession: true,
2553 })
2554 testCases = append(testCases, testCase{
2555 testType: serverTest,
2556 name: "ALPNServer-Preferred-Swapped",
2557 config: Config{
2558 NextProtos: []string{"foo", "bar", "baz"},
2559 Bugs: ProtocolBugs{
2560 SwapNPNAndALPN: true,
2561 },
2562 },
2563 flags: []string{
2564 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2565 "-select-alpn", "foo",
2566 "-advertise-npn", "\x03foo\x03bar\x03baz",
2567 },
2568 expectedNextProto: "foo",
2569 expectedNextProtoType: alpn,
2570 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04002571 })
Adam Langley38311732014-10-16 19:04:35 -07002572 // Resume with a corrupt ticket.
2573 testCases = append(testCases, testCase{
2574 testType: serverTest,
2575 name: "CorruptTicket",
2576 config: Config{
2577 Bugs: ProtocolBugs{
2578 CorruptTicket: true,
2579 },
2580 },
2581 resumeSession: true,
2582 flags: []string{"-expect-session-miss"},
2583 })
2584 // Resume with an oversized session id.
2585 testCases = append(testCases, testCase{
2586 testType: serverTest,
2587 name: "OversizedSessionId",
2588 config: Config{
2589 Bugs: ProtocolBugs{
2590 OversizedSessionId: true,
2591 },
2592 },
2593 resumeSession: true,
Adam Langley75712922014-10-10 16:23:43 -07002594 shouldFail: true,
Adam Langley38311732014-10-16 19:04:35 -07002595 expectedError: ":DECODE_ERROR:",
2596 })
David Benjaminca6c8262014-11-15 19:06:08 -05002597 // Basic DTLS-SRTP tests. Include fake profiles to ensure they
2598 // are ignored.
2599 testCases = append(testCases, testCase{
2600 protocol: dtls,
2601 name: "SRTP-Client",
2602 config: Config{
2603 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
2604 },
2605 flags: []string{
2606 "-srtp-profiles",
2607 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2608 },
2609 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2610 })
2611 testCases = append(testCases, testCase{
2612 protocol: dtls,
2613 testType: serverTest,
2614 name: "SRTP-Server",
2615 config: Config{
2616 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
2617 },
2618 flags: []string{
2619 "-srtp-profiles",
2620 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2621 },
2622 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2623 })
2624 // Test that the MKI is ignored.
2625 testCases = append(testCases, testCase{
2626 protocol: dtls,
2627 testType: serverTest,
2628 name: "SRTP-Server-IgnoreMKI",
2629 config: Config{
2630 SRTPProtectionProfiles: []uint16{SRTP_AES128_CM_HMAC_SHA1_80},
2631 Bugs: ProtocolBugs{
2632 SRTPMasterKeyIdentifer: "bogus",
2633 },
2634 },
2635 flags: []string{
2636 "-srtp-profiles",
2637 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2638 },
2639 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2640 })
2641 // Test that SRTP isn't negotiated on the server if there were
2642 // no matching profiles.
2643 testCases = append(testCases, testCase{
2644 protocol: dtls,
2645 testType: serverTest,
2646 name: "SRTP-Server-NoMatch",
2647 config: Config{
2648 SRTPProtectionProfiles: []uint16{100, 101, 102},
2649 },
2650 flags: []string{
2651 "-srtp-profiles",
2652 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2653 },
2654 expectedSRTPProtectionProfile: 0,
2655 })
2656 // Test that the server returning an invalid SRTP profile is
2657 // flagged as an error by the client.
2658 testCases = append(testCases, testCase{
2659 protocol: dtls,
2660 name: "SRTP-Client-NoMatch",
2661 config: Config{
2662 Bugs: ProtocolBugs{
2663 SendSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_32,
2664 },
2665 },
2666 flags: []string{
2667 "-srtp-profiles",
2668 "SRTP_AES128_CM_SHA1_80",
2669 },
2670 shouldFail: true,
2671 expectedError: ":BAD_SRTP_PROTECTION_PROFILE_LIST:",
2672 })
David Benjamin61f95272014-11-25 01:55:35 -05002673 // Test OCSP stapling and SCT list.
2674 testCases = append(testCases, testCase{
2675 name: "OCSPStapling",
2676 flags: []string{
2677 "-enable-ocsp-stapling",
2678 "-expect-ocsp-response",
2679 base64.StdEncoding.EncodeToString(testOCSPResponse),
2680 },
2681 })
2682 testCases = append(testCases, testCase{
2683 name: "SignedCertificateTimestampList",
2684 flags: []string{
2685 "-enable-signed-cert-timestamps",
2686 "-expect-signed-cert-timestamps",
2687 base64.StdEncoding.EncodeToString(testSCTList),
2688 },
2689 })
David Benjamine78bfde2014-09-06 12:45:15 -04002690}
2691
David Benjamin01fe8202014-09-24 15:21:44 -04002692func addResumptionVersionTests() {
David Benjamin01fe8202014-09-24 15:21:44 -04002693 for _, sessionVers := range tlsVersions {
David Benjamin01fe8202014-09-24 15:21:44 -04002694 for _, resumeVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05002695 protocols := []protocol{tls}
2696 if sessionVers.hasDTLS && resumeVers.hasDTLS {
2697 protocols = append(protocols, dtls)
David Benjaminbdf5e722014-11-11 00:52:15 -05002698 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002699 for _, protocol := range protocols {
2700 suffix := "-" + sessionVers.name + "-" + resumeVers.name
2701 if protocol == dtls {
2702 suffix += "-DTLS"
2703 }
2704
David Benjaminece3de92015-03-16 18:02:20 -04002705 if sessionVers.version == resumeVers.version {
2706 testCases = append(testCases, testCase{
2707 protocol: protocol,
2708 name: "Resume-Client" + suffix,
2709 resumeSession: true,
2710 config: Config{
2711 MaxVersion: sessionVers.version,
2712 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
David Benjamin8b8c0062014-11-23 02:47:52 -05002713 },
David Benjaminece3de92015-03-16 18:02:20 -04002714 expectedVersion: sessionVers.version,
2715 expectedResumeVersion: resumeVers.version,
2716 })
2717 } else {
2718 testCases = append(testCases, testCase{
2719 protocol: protocol,
2720 name: "Resume-Client-Mismatch" + suffix,
2721 resumeSession: true,
2722 config: Config{
2723 MaxVersion: sessionVers.version,
2724 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
David Benjamin8b8c0062014-11-23 02:47:52 -05002725 },
David Benjaminece3de92015-03-16 18:02:20 -04002726 expectedVersion: sessionVers.version,
2727 resumeConfig: &Config{
2728 MaxVersion: resumeVers.version,
2729 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2730 Bugs: ProtocolBugs{
2731 AllowSessionVersionMismatch: true,
2732 },
2733 },
2734 expectedResumeVersion: resumeVers.version,
2735 shouldFail: true,
2736 expectedError: ":OLD_SESSION_VERSION_NOT_RETURNED:",
2737 })
2738 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002739
2740 testCases = append(testCases, testCase{
2741 protocol: protocol,
2742 name: "Resume-Client-NoResume" + suffix,
2743 flags: []string{"-expect-session-miss"},
2744 resumeSession: true,
2745 config: Config{
2746 MaxVersion: sessionVers.version,
2747 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2748 },
2749 expectedVersion: sessionVers.version,
2750 resumeConfig: &Config{
2751 MaxVersion: resumeVers.version,
2752 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2753 },
2754 newSessionsOnResume: true,
2755 expectedResumeVersion: resumeVers.version,
2756 })
2757
2758 var flags []string
2759 if sessionVers.version != resumeVers.version {
2760 flags = append(flags, "-expect-session-miss")
2761 }
2762 testCases = append(testCases, testCase{
2763 protocol: protocol,
2764 testType: serverTest,
2765 name: "Resume-Server" + suffix,
2766 flags: flags,
2767 resumeSession: true,
2768 config: Config{
2769 MaxVersion: sessionVers.version,
2770 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2771 },
2772 expectedVersion: sessionVers.version,
2773 resumeConfig: &Config{
2774 MaxVersion: resumeVers.version,
2775 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2776 },
2777 expectedResumeVersion: resumeVers.version,
2778 })
2779 }
David Benjamin01fe8202014-09-24 15:21:44 -04002780 }
2781 }
David Benjaminece3de92015-03-16 18:02:20 -04002782
2783 testCases = append(testCases, testCase{
2784 name: "Resume-Client-CipherMismatch",
2785 resumeSession: true,
2786 config: Config{
2787 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2788 },
2789 resumeConfig: &Config{
2790 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2791 Bugs: ProtocolBugs{
2792 SendCipherSuite: TLS_RSA_WITH_AES_128_CBC_SHA,
2793 },
2794 },
2795 shouldFail: true,
2796 expectedError: ":OLD_SESSION_CIPHER_NOT_RETURNED:",
2797 })
David Benjamin01fe8202014-09-24 15:21:44 -04002798}
2799
Adam Langley2ae77d22014-10-28 17:29:33 -07002800func addRenegotiationTests() {
2801 testCases = append(testCases, testCase{
Adam Langley2ae77d22014-10-28 17:29:33 -07002802 testType: serverTest,
David Benjamin4b27d9f2015-05-12 22:42:52 -04002803 name: "Renegotiate-Server",
David Benjamincdea40c2015-03-19 14:09:43 -04002804 config: Config{
2805 Bugs: ProtocolBugs{
David Benjamin4b27d9f2015-05-12 22:42:52 -04002806 FailIfResumeOnRenego: true,
David Benjamincdea40c2015-03-19 14:09:43 -04002807 },
2808 },
2809 flags: []string{"-renegotiate"},
2810 shimWritesFirst: true,
2811 })
2812 testCases = append(testCases, testCase{
2813 testType: serverTest,
Adam Langley2ae77d22014-10-28 17:29:33 -07002814 name: "Renegotiate-Server-EmptyExt",
2815 config: Config{
2816 Bugs: ProtocolBugs{
2817 EmptyRenegotiationInfo: true,
2818 },
2819 },
2820 flags: []string{"-renegotiate"},
2821 shimWritesFirst: true,
2822 shouldFail: true,
2823 expectedError: ":RENEGOTIATION_MISMATCH:",
2824 })
2825 testCases = append(testCases, testCase{
2826 testType: serverTest,
2827 name: "Renegotiate-Server-BadExt",
2828 config: Config{
2829 Bugs: ProtocolBugs{
2830 BadRenegotiationInfo: true,
2831 },
2832 },
2833 flags: []string{"-renegotiate"},
2834 shimWritesFirst: true,
2835 shouldFail: true,
2836 expectedError: ":RENEGOTIATION_MISMATCH:",
2837 })
David Benjaminca6554b2014-11-08 12:31:52 -05002838 testCases = append(testCases, testCase{
2839 testType: serverTest,
2840 name: "Renegotiate-Server-ClientInitiated",
2841 renegotiate: true,
2842 })
2843 testCases = append(testCases, testCase{
2844 testType: serverTest,
2845 name: "Renegotiate-Server-ClientInitiated-NoExt",
2846 renegotiate: true,
2847 config: Config{
2848 Bugs: ProtocolBugs{
2849 NoRenegotiationInfo: true,
2850 },
2851 },
2852 shouldFail: true,
2853 expectedError: ":UNSAFE_LEGACY_RENEGOTIATION_DISABLED:",
2854 })
2855 testCases = append(testCases, testCase{
2856 testType: serverTest,
2857 name: "Renegotiate-Server-ClientInitiated-NoExt-Allowed",
2858 renegotiate: true,
2859 config: Config{
2860 Bugs: ProtocolBugs{
2861 NoRenegotiationInfo: true,
2862 },
2863 },
2864 flags: []string{"-allow-unsafe-legacy-renegotiation"},
2865 })
David Benjaminb16346b2015-04-08 19:16:58 -04002866 testCases = append(testCases, testCase{
2867 testType: serverTest,
2868 name: "Renegotiate-Server-ClientInitiated-Forbidden",
2869 renegotiate: true,
2870 flags: []string{"-reject-peer-renegotiations"},
2871 shouldFail: true,
2872 expectedError: ":NO_RENEGOTIATION:",
2873 expectedLocalError: "remote error: no renegotiation",
2874 })
David Benjamin3c9746a2015-03-19 15:00:10 -04002875 // Regression test for CVE-2015-0291.
2876 testCases = append(testCases, testCase{
2877 testType: serverTest,
2878 name: "Renegotiate-Server-NoSignatureAlgorithms",
2879 config: Config{
2880 Bugs: ProtocolBugs{
David Benjamin3c9746a2015-03-19 15:00:10 -04002881 NoSignatureAlgorithmsOnRenego: true,
2882 },
2883 },
2884 flags: []string{"-renegotiate"},
2885 shimWritesFirst: true,
2886 })
Adam Langley2ae77d22014-10-28 17:29:33 -07002887 // TODO(agl): test the renegotiation info SCSV.
Adam Langleycf2d4f42014-10-28 19:06:14 -07002888 testCases = append(testCases, testCase{
David Benjamin4b27d9f2015-05-12 22:42:52 -04002889 name: "Renegotiate-Client",
David Benjamincdea40c2015-03-19 14:09:43 -04002890 config: Config{
2891 Bugs: ProtocolBugs{
David Benjamin4b27d9f2015-05-12 22:42:52 -04002892 FailIfResumeOnRenego: true,
David Benjamincdea40c2015-03-19 14:09:43 -04002893 },
2894 },
2895 renegotiate: true,
2896 })
2897 testCases = append(testCases, testCase{
Adam Langleycf2d4f42014-10-28 19:06:14 -07002898 name: "Renegotiate-Client-EmptyExt",
2899 renegotiate: true,
2900 config: Config{
2901 Bugs: ProtocolBugs{
2902 EmptyRenegotiationInfo: true,
2903 },
2904 },
2905 shouldFail: true,
2906 expectedError: ":RENEGOTIATION_MISMATCH:",
2907 })
2908 testCases = append(testCases, testCase{
2909 name: "Renegotiate-Client-BadExt",
2910 renegotiate: true,
2911 config: Config{
2912 Bugs: ProtocolBugs{
2913 BadRenegotiationInfo: true,
2914 },
2915 },
2916 shouldFail: true,
2917 expectedError: ":RENEGOTIATION_MISMATCH:",
2918 })
2919 testCases = append(testCases, testCase{
David Benjamincff0b902015-05-15 23:09:47 -04002920 name: "Renegotiate-Client-NoExt",
2921 renegotiate: true,
2922 config: Config{
2923 Bugs: ProtocolBugs{
2924 NoRenegotiationInfo: true,
2925 },
2926 },
2927 shouldFail: true,
2928 expectedError: ":UNSAFE_LEGACY_RENEGOTIATION_DISABLED:",
2929 flags: []string{"-no-legacy-server-connect"},
2930 })
2931 testCases = append(testCases, testCase{
2932 name: "Renegotiate-Client-NoExt-Allowed",
2933 renegotiate: true,
2934 config: Config{
2935 Bugs: ProtocolBugs{
2936 NoRenegotiationInfo: true,
2937 },
2938 },
2939 })
2940 testCases = append(testCases, testCase{
Adam Langleycf2d4f42014-10-28 19:06:14 -07002941 name: "Renegotiate-Client-SwitchCiphers",
2942 renegotiate: true,
2943 config: Config{
2944 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2945 },
2946 renegotiateCiphers: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2947 })
2948 testCases = append(testCases, testCase{
2949 name: "Renegotiate-Client-SwitchCiphers2",
2950 renegotiate: true,
2951 config: Config{
2952 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2953 },
2954 renegotiateCiphers: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2955 })
David Benjaminc44b1df2014-11-23 12:11:01 -05002956 testCases = append(testCases, testCase{
David Benjaminb16346b2015-04-08 19:16:58 -04002957 name: "Renegotiate-Client-Forbidden",
2958 renegotiate: true,
2959 flags: []string{"-reject-peer-renegotiations"},
2960 shouldFail: true,
2961 expectedError: ":NO_RENEGOTIATION:",
2962 expectedLocalError: "remote error: no renegotiation",
2963 })
2964 testCases = append(testCases, testCase{
David Benjaminc44b1df2014-11-23 12:11:01 -05002965 name: "Renegotiate-SameClientVersion",
2966 renegotiate: true,
2967 config: Config{
2968 MaxVersion: VersionTLS10,
2969 Bugs: ProtocolBugs{
2970 RequireSameRenegoClientVersion: true,
2971 },
2972 },
2973 })
Adam Langley2ae77d22014-10-28 17:29:33 -07002974}
2975
David Benjamin5e961c12014-11-07 01:48:35 -05002976func addDTLSReplayTests() {
2977 // Test that sequence number replays are detected.
2978 testCases = append(testCases, testCase{
2979 protocol: dtls,
2980 name: "DTLS-Replay",
2981 replayWrites: true,
2982 })
2983
2984 // Test the outgoing sequence number skipping by values larger
2985 // than the retransmit window.
2986 testCases = append(testCases, testCase{
2987 protocol: dtls,
2988 name: "DTLS-Replay-LargeGaps",
2989 config: Config{
2990 Bugs: ProtocolBugs{
2991 SequenceNumberIncrement: 127,
2992 },
2993 },
2994 replayWrites: true,
2995 })
2996}
2997
Feng Lu41aa3252014-11-21 22:47:56 -08002998func addFastRadioPaddingTests() {
David Benjamin1e29a6b2014-12-10 02:27:24 -05002999 testCases = append(testCases, testCase{
3000 protocol: tls,
3001 name: "FastRadio-Padding",
Feng Lu41aa3252014-11-21 22:47:56 -08003002 config: Config{
3003 Bugs: ProtocolBugs{
3004 RequireFastradioPadding: true,
3005 },
3006 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05003007 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08003008 })
David Benjamin1e29a6b2014-12-10 02:27:24 -05003009 testCases = append(testCases, testCase{
3010 protocol: dtls,
David Benjamin5f237bc2015-02-11 17:14:15 -05003011 name: "FastRadio-Padding-DTLS",
Feng Lu41aa3252014-11-21 22:47:56 -08003012 config: Config{
3013 Bugs: ProtocolBugs{
3014 RequireFastradioPadding: true,
3015 },
3016 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05003017 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08003018 })
3019}
3020
David Benjamin000800a2014-11-14 01:43:59 -05003021var testHashes = []struct {
3022 name string
3023 id uint8
3024}{
3025 {"SHA1", hashSHA1},
3026 {"SHA224", hashSHA224},
3027 {"SHA256", hashSHA256},
3028 {"SHA384", hashSHA384},
3029 {"SHA512", hashSHA512},
3030}
3031
3032func addSigningHashTests() {
3033 // Make sure each hash works. Include some fake hashes in the list and
3034 // ensure they're ignored.
3035 for _, hash := range testHashes {
3036 testCases = append(testCases, testCase{
3037 name: "SigningHash-ClientAuth-" + hash.name,
3038 config: Config{
3039 ClientAuth: RequireAnyClientCert,
3040 SignatureAndHashes: []signatureAndHash{
3041 {signatureRSA, 42},
3042 {signatureRSA, hash.id},
3043 {signatureRSA, 255},
3044 },
3045 },
3046 flags: []string{
3047 "-cert-file", rsaCertificateFile,
3048 "-key-file", rsaKeyFile,
3049 },
3050 })
3051
3052 testCases = append(testCases, testCase{
3053 testType: serverTest,
3054 name: "SigningHash-ServerKeyExchange-Sign-" + hash.name,
3055 config: Config{
3056 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
3057 SignatureAndHashes: []signatureAndHash{
3058 {signatureRSA, 42},
3059 {signatureRSA, hash.id},
3060 {signatureRSA, 255},
3061 },
3062 },
3063 })
3064 }
3065
3066 // Test that hash resolution takes the signature type into account.
3067 testCases = append(testCases, testCase{
3068 name: "SigningHash-ClientAuth-SignatureType",
3069 config: Config{
3070 ClientAuth: RequireAnyClientCert,
3071 SignatureAndHashes: []signatureAndHash{
3072 {signatureECDSA, hashSHA512},
3073 {signatureRSA, hashSHA384},
3074 {signatureECDSA, hashSHA1},
3075 },
3076 },
3077 flags: []string{
3078 "-cert-file", rsaCertificateFile,
3079 "-key-file", rsaKeyFile,
3080 },
3081 })
3082
3083 testCases = append(testCases, testCase{
3084 testType: serverTest,
3085 name: "SigningHash-ServerKeyExchange-SignatureType",
3086 config: Config{
3087 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
3088 SignatureAndHashes: []signatureAndHash{
3089 {signatureECDSA, hashSHA512},
3090 {signatureRSA, hashSHA384},
3091 {signatureECDSA, hashSHA1},
3092 },
3093 },
3094 })
3095
3096 // Test that, if the list is missing, the peer falls back to SHA-1.
3097 testCases = append(testCases, testCase{
3098 name: "SigningHash-ClientAuth-Fallback",
3099 config: Config{
3100 ClientAuth: RequireAnyClientCert,
3101 SignatureAndHashes: []signatureAndHash{
3102 {signatureRSA, hashSHA1},
3103 },
3104 Bugs: ProtocolBugs{
3105 NoSignatureAndHashes: true,
3106 },
3107 },
3108 flags: []string{
3109 "-cert-file", rsaCertificateFile,
3110 "-key-file", rsaKeyFile,
3111 },
3112 })
3113
3114 testCases = append(testCases, testCase{
3115 testType: serverTest,
3116 name: "SigningHash-ServerKeyExchange-Fallback",
3117 config: Config{
3118 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
3119 SignatureAndHashes: []signatureAndHash{
3120 {signatureRSA, hashSHA1},
3121 },
3122 Bugs: ProtocolBugs{
3123 NoSignatureAndHashes: true,
3124 },
3125 },
3126 })
David Benjamin72dc7832015-03-16 17:49:43 -04003127
3128 // Test that hash preferences are enforced. BoringSSL defaults to
3129 // rejecting MD5 signatures.
3130 testCases = append(testCases, testCase{
3131 testType: serverTest,
3132 name: "SigningHash-ClientAuth-Enforced",
3133 config: Config{
3134 Certificates: []Certificate{rsaCertificate},
3135 SignatureAndHashes: []signatureAndHash{
3136 {signatureRSA, hashMD5},
3137 // Advertise SHA-1 so the handshake will
3138 // proceed, but the shim's preferences will be
3139 // ignored in CertificateVerify generation, so
3140 // MD5 will be chosen.
3141 {signatureRSA, hashSHA1},
3142 },
3143 Bugs: ProtocolBugs{
3144 IgnorePeerSignatureAlgorithmPreferences: true,
3145 },
3146 },
3147 flags: []string{"-require-any-client-certificate"},
3148 shouldFail: true,
3149 expectedError: ":WRONG_SIGNATURE_TYPE:",
3150 })
3151
3152 testCases = append(testCases, testCase{
3153 name: "SigningHash-ServerKeyExchange-Enforced",
3154 config: Config{
3155 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
3156 SignatureAndHashes: []signatureAndHash{
3157 {signatureRSA, hashMD5},
3158 },
3159 Bugs: ProtocolBugs{
3160 IgnorePeerSignatureAlgorithmPreferences: true,
3161 },
3162 },
3163 shouldFail: true,
3164 expectedError: ":WRONG_SIGNATURE_TYPE:",
3165 })
David Benjamin000800a2014-11-14 01:43:59 -05003166}
3167
David Benjamin83f90402015-01-27 01:09:43 -05003168// timeouts is the retransmit schedule for BoringSSL. It doubles and
3169// caps at 60 seconds. On the 13th timeout, it gives up.
3170var timeouts = []time.Duration{
3171 1 * time.Second,
3172 2 * time.Second,
3173 4 * time.Second,
3174 8 * time.Second,
3175 16 * time.Second,
3176 32 * time.Second,
3177 60 * time.Second,
3178 60 * time.Second,
3179 60 * time.Second,
3180 60 * time.Second,
3181 60 * time.Second,
3182 60 * time.Second,
3183 60 * time.Second,
3184}
3185
3186func addDTLSRetransmitTests() {
3187 // Test that this is indeed the timeout schedule. Stress all
3188 // four patterns of handshake.
3189 for i := 1; i < len(timeouts); i++ {
3190 number := strconv.Itoa(i)
3191 testCases = append(testCases, testCase{
3192 protocol: dtls,
3193 name: "DTLS-Retransmit-Client-" + number,
3194 config: Config{
3195 Bugs: ProtocolBugs{
3196 TimeoutSchedule: timeouts[:i],
3197 },
3198 },
3199 resumeSession: true,
3200 flags: []string{"-async"},
3201 })
3202 testCases = append(testCases, testCase{
3203 protocol: dtls,
3204 testType: serverTest,
3205 name: "DTLS-Retransmit-Server-" + number,
3206 config: Config{
3207 Bugs: ProtocolBugs{
3208 TimeoutSchedule: timeouts[:i],
3209 },
3210 },
3211 resumeSession: true,
3212 flags: []string{"-async"},
3213 })
3214 }
3215
3216 // Test that exceeding the timeout schedule hits a read
3217 // timeout.
3218 testCases = append(testCases, testCase{
3219 protocol: dtls,
3220 name: "DTLS-Retransmit-Timeout",
3221 config: Config{
3222 Bugs: ProtocolBugs{
3223 TimeoutSchedule: timeouts,
3224 },
3225 },
3226 resumeSession: true,
3227 flags: []string{"-async"},
3228 shouldFail: true,
3229 expectedError: ":READ_TIMEOUT_EXPIRED:",
3230 })
3231
3232 // Test that timeout handling has a fudge factor, due to API
3233 // problems.
3234 testCases = append(testCases, testCase{
3235 protocol: dtls,
3236 name: "DTLS-Retransmit-Fudge",
3237 config: Config{
3238 Bugs: ProtocolBugs{
3239 TimeoutSchedule: []time.Duration{
3240 timeouts[0] - 10*time.Millisecond,
3241 },
3242 },
3243 },
3244 resumeSession: true,
3245 flags: []string{"-async"},
3246 })
David Benjamin7eaab4c2015-03-02 19:01:16 -05003247
3248 // Test that the final Finished retransmitting isn't
3249 // duplicated if the peer badly fragments everything.
3250 testCases = append(testCases, testCase{
3251 testType: serverTest,
3252 protocol: dtls,
3253 name: "DTLS-Retransmit-Fragmented",
3254 config: Config{
3255 Bugs: ProtocolBugs{
3256 TimeoutSchedule: []time.Duration{timeouts[0]},
3257 MaxHandshakeRecordLength: 2,
3258 },
3259 },
3260 flags: []string{"-async"},
3261 })
David Benjamin83f90402015-01-27 01:09:43 -05003262}
3263
David Benjaminc565ebb2015-04-03 04:06:36 -04003264func addExportKeyingMaterialTests() {
3265 for _, vers := range tlsVersions {
3266 if vers.version == VersionSSL30 {
3267 continue
3268 }
3269 testCases = append(testCases, testCase{
3270 name: "ExportKeyingMaterial-" + vers.name,
3271 config: Config{
3272 MaxVersion: vers.version,
3273 },
3274 exportKeyingMaterial: 1024,
3275 exportLabel: "label",
3276 exportContext: "context",
3277 useExportContext: true,
3278 })
3279 testCases = append(testCases, testCase{
3280 name: "ExportKeyingMaterial-NoContext-" + vers.name,
3281 config: Config{
3282 MaxVersion: vers.version,
3283 },
3284 exportKeyingMaterial: 1024,
3285 })
3286 testCases = append(testCases, testCase{
3287 name: "ExportKeyingMaterial-EmptyContext-" + vers.name,
3288 config: Config{
3289 MaxVersion: vers.version,
3290 },
3291 exportKeyingMaterial: 1024,
3292 useExportContext: true,
3293 })
3294 testCases = append(testCases, testCase{
3295 name: "ExportKeyingMaterial-Small-" + vers.name,
3296 config: Config{
3297 MaxVersion: vers.version,
3298 },
3299 exportKeyingMaterial: 1,
3300 exportLabel: "label",
3301 exportContext: "context",
3302 useExportContext: true,
3303 })
3304 }
3305 testCases = append(testCases, testCase{
3306 name: "ExportKeyingMaterial-SSL3",
3307 config: Config{
3308 MaxVersion: VersionSSL30,
3309 },
3310 exportKeyingMaterial: 1024,
3311 exportLabel: "label",
3312 exportContext: "context",
3313 useExportContext: true,
3314 shouldFail: true,
3315 expectedError: "failed to export keying material",
3316 })
3317}
3318
David Benjamin884fdf12014-08-02 15:28:23 -04003319func worker(statusChan chan statusMsg, c chan *testCase, buildDir string, wg *sync.WaitGroup) {
Adam Langley95c29f32014-06-20 12:00:00 -07003320 defer wg.Done()
3321
3322 for test := range c {
Adam Langley69a01602014-11-17 17:26:55 -08003323 var err error
3324
3325 if *mallocTest < 0 {
3326 statusChan <- statusMsg{test: test, started: true}
3327 err = runTest(test, buildDir, -1)
3328 } else {
3329 for mallocNumToFail := int64(*mallocTest); ; mallocNumToFail++ {
3330 statusChan <- statusMsg{test: test, started: true}
3331 if err = runTest(test, buildDir, mallocNumToFail); err != errMoreMallocs {
3332 if err != nil {
3333 fmt.Printf("\n\nmalloc test failed at %d: %s\n", mallocNumToFail, err)
3334 }
3335 break
3336 }
3337 }
3338 }
Adam Langley95c29f32014-06-20 12:00:00 -07003339 statusChan <- statusMsg{test: test, err: err}
3340 }
3341}
3342
3343type statusMsg struct {
3344 test *testCase
3345 started bool
3346 err error
3347}
3348
David Benjamin5f237bc2015-02-11 17:14:15 -05003349func statusPrinter(doneChan chan *testOutput, statusChan chan statusMsg, total int) {
Adam Langley95c29f32014-06-20 12:00:00 -07003350 var started, done, failed, lineLen int
Adam Langley95c29f32014-06-20 12:00:00 -07003351
David Benjamin5f237bc2015-02-11 17:14:15 -05003352 testOutput := newTestOutput()
Adam Langley95c29f32014-06-20 12:00:00 -07003353 for msg := range statusChan {
David Benjamin5f237bc2015-02-11 17:14:15 -05003354 if !*pipe {
3355 // Erase the previous status line.
David Benjamin87c8a642015-02-21 01:54:29 -05003356 var erase string
3357 for i := 0; i < lineLen; i++ {
3358 erase += "\b \b"
3359 }
3360 fmt.Print(erase)
David Benjamin5f237bc2015-02-11 17:14:15 -05003361 }
3362
Adam Langley95c29f32014-06-20 12:00:00 -07003363 if msg.started {
3364 started++
3365 } else {
3366 done++
David Benjamin5f237bc2015-02-11 17:14:15 -05003367
3368 if msg.err != nil {
3369 fmt.Printf("FAILED (%s)\n%s\n", msg.test.name, msg.err)
3370 failed++
3371 testOutput.addResult(msg.test.name, "FAIL")
3372 } else {
3373 if *pipe {
3374 // Print each test instead of a status line.
3375 fmt.Printf("PASSED (%s)\n", msg.test.name)
3376 }
3377 testOutput.addResult(msg.test.name, "PASS")
3378 }
Adam Langley95c29f32014-06-20 12:00:00 -07003379 }
3380
David Benjamin5f237bc2015-02-11 17:14:15 -05003381 if !*pipe {
3382 // Print a new status line.
3383 line := fmt.Sprintf("%d/%d/%d/%d", failed, done, started, total)
3384 lineLen = len(line)
3385 os.Stdout.WriteString(line)
Adam Langley95c29f32014-06-20 12:00:00 -07003386 }
Adam Langley95c29f32014-06-20 12:00:00 -07003387 }
David Benjamin5f237bc2015-02-11 17:14:15 -05003388
3389 doneChan <- testOutput
Adam Langley95c29f32014-06-20 12:00:00 -07003390}
3391
3392func main() {
3393 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 -04003394 var flagNumWorkers *int = flag.Int("num-workers", runtime.NumCPU(), "The number of workers to run in parallel.")
David Benjamin884fdf12014-08-02 15:28:23 -04003395 var flagBuildDir *string = flag.String("build-dir", "../../../build", "The build directory to run the shim from.")
Adam Langley95c29f32014-06-20 12:00:00 -07003396
3397 flag.Parse()
3398
3399 addCipherSuiteTests()
3400 addBadECDSASignatureTests()
Adam Langley80842bd2014-06-20 12:00:00 -07003401 addCBCPaddingTests()
Kenny Root7fdeaf12014-08-05 15:23:37 -07003402 addCBCSplittingTests()
David Benjamin636293b2014-07-08 17:59:18 -04003403 addClientAuthTests()
Adam Langley524e7172015-02-20 16:04:00 -08003404 addDDoSCallbackTests()
David Benjamin7e2e6cf2014-08-07 17:44:24 -04003405 addVersionNegotiationTests()
David Benjaminaccb4542014-12-12 23:44:33 -05003406 addMinimumVersionTests()
David Benjamin5c24a1d2014-08-31 00:59:27 -04003407 addD5BugTests()
David Benjamine78bfde2014-09-06 12:45:15 -04003408 addExtensionTests()
David Benjamin01fe8202014-09-24 15:21:44 -04003409 addResumptionVersionTests()
Adam Langley75712922014-10-10 16:23:43 -07003410 addExtendedMasterSecretTests()
Adam Langley2ae77d22014-10-28 17:29:33 -07003411 addRenegotiationTests()
David Benjamin5e961c12014-11-07 01:48:35 -05003412 addDTLSReplayTests()
David Benjamin000800a2014-11-14 01:43:59 -05003413 addSigningHashTests()
Feng Lu41aa3252014-11-21 22:47:56 -08003414 addFastRadioPaddingTests()
David Benjamin83f90402015-01-27 01:09:43 -05003415 addDTLSRetransmitTests()
David Benjaminc565ebb2015-04-03 04:06:36 -04003416 addExportKeyingMaterialTests()
David Benjamin43ec06f2014-08-05 02:28:57 -04003417 for _, async := range []bool{false, true} {
3418 for _, splitHandshake := range []bool{false, true} {
David Benjamin6fd297b2014-08-11 18:43:38 -04003419 for _, protocol := range []protocol{tls, dtls} {
3420 addStateMachineCoverageTests(async, splitHandshake, protocol)
3421 }
David Benjamin43ec06f2014-08-05 02:28:57 -04003422 }
3423 }
Adam Langley95c29f32014-06-20 12:00:00 -07003424
3425 var wg sync.WaitGroup
3426
David Benjamin2bc8e6f2014-08-02 15:22:37 -04003427 numWorkers := *flagNumWorkers
Adam Langley95c29f32014-06-20 12:00:00 -07003428
3429 statusChan := make(chan statusMsg, numWorkers)
3430 testChan := make(chan *testCase, numWorkers)
David Benjamin5f237bc2015-02-11 17:14:15 -05003431 doneChan := make(chan *testOutput)
Adam Langley95c29f32014-06-20 12:00:00 -07003432
David Benjamin025b3d32014-07-01 19:53:04 -04003433 go statusPrinter(doneChan, statusChan, len(testCases))
Adam Langley95c29f32014-06-20 12:00:00 -07003434
3435 for i := 0; i < numWorkers; i++ {
3436 wg.Add(1)
David Benjamin884fdf12014-08-02 15:28:23 -04003437 go worker(statusChan, testChan, *flagBuildDir, &wg)
Adam Langley95c29f32014-06-20 12:00:00 -07003438 }
3439
David Benjamin025b3d32014-07-01 19:53:04 -04003440 for i := range testCases {
3441 if len(*flagTest) == 0 || *flagTest == testCases[i].name {
3442 testChan <- &testCases[i]
Adam Langley95c29f32014-06-20 12:00:00 -07003443 }
3444 }
3445
3446 close(testChan)
3447 wg.Wait()
3448 close(statusChan)
David Benjamin5f237bc2015-02-11 17:14:15 -05003449 testOutput := <-doneChan
Adam Langley95c29f32014-06-20 12:00:00 -07003450
3451 fmt.Printf("\n")
David Benjamin5f237bc2015-02-11 17:14:15 -05003452
3453 if *jsonOutput != "" {
3454 if err := testOutput.writeTo(*jsonOutput); err != nil {
3455 fmt.Fprintf(os.Stderr, "Error: %s\n", err)
3456 }
3457 }
David Benjamin2ab7a862015-04-04 17:02:18 -04003458
3459 if !testOutput.allPassed {
3460 os.Exit(1)
3461 }
Adam Langley95c29f32014-06-20 12:00:00 -07003462}