blob: e4a3f9a64482f723857c8010cf809ee336bc4617 [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 },
David Benjamin9a41d1b2015-05-16 01:30:09 -04001089 {
1090 protocol: dtls,
1091 name: "SendSplitAlert-Sync",
1092 config: Config{
1093 Bugs: ProtocolBugs{
1094 SendSplitAlert: true,
1095 },
1096 },
1097 },
1098 {
1099 protocol: dtls,
1100 name: "SendSplitAlert-Async",
1101 config: Config{
1102 Bugs: ProtocolBugs{
1103 SendSplitAlert: true,
1104 },
1105 },
1106 flags: []string{"-async"},
1107 },
Adam Langley95c29f32014-06-20 12:00:00 -07001108}
1109
David Benjamin01fe8202014-09-24 15:21:44 -04001110func doExchange(test *testCase, config *Config, conn net.Conn, messageLen int, isResume bool) error {
David Benjamin65ea8ff2014-11-23 03:01:00 -05001111 var connDebug *recordingConn
David Benjamin5fa3eba2015-01-22 16:35:40 -05001112 var connDamage *damageAdaptor
David Benjamin65ea8ff2014-11-23 03:01:00 -05001113 if *flagDebug {
1114 connDebug = &recordingConn{Conn: conn}
1115 conn = connDebug
1116 defer func() {
1117 connDebug.WriteTo(os.Stdout)
1118 }()
1119 }
1120
David Benjamin6fd297b2014-08-11 18:43:38 -04001121 if test.protocol == dtls {
David Benjamin83f90402015-01-27 01:09:43 -05001122 config.Bugs.PacketAdaptor = newPacketAdaptor(conn)
1123 conn = config.Bugs.PacketAdaptor
David Benjamin5e961c12014-11-07 01:48:35 -05001124 if test.replayWrites {
1125 conn = newReplayAdaptor(conn)
1126 }
David Benjamin6fd297b2014-08-11 18:43:38 -04001127 }
1128
David Benjamin5fa3eba2015-01-22 16:35:40 -05001129 if test.damageFirstWrite {
1130 connDamage = newDamageAdaptor(conn)
1131 conn = connDamage
1132 }
1133
David Benjamin6fd297b2014-08-11 18:43:38 -04001134 if test.sendPrefix != "" {
1135 if _, err := conn.Write([]byte(test.sendPrefix)); err != nil {
1136 return err
1137 }
David Benjamin98e882e2014-08-08 13:24:34 -04001138 }
1139
David Benjamin1d5c83e2014-07-22 19:20:02 -04001140 var tlsConn *Conn
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001141 if test.testType == clientTest {
David Benjamin6fd297b2014-08-11 18:43:38 -04001142 if test.protocol == dtls {
1143 tlsConn = DTLSServer(conn, config)
1144 } else {
1145 tlsConn = Server(conn, config)
1146 }
David Benjamin1d5c83e2014-07-22 19:20:02 -04001147 } else {
1148 config.InsecureSkipVerify = true
David Benjamin6fd297b2014-08-11 18:43:38 -04001149 if test.protocol == dtls {
1150 tlsConn = DTLSClient(conn, config)
1151 } else {
1152 tlsConn = Client(conn, config)
1153 }
David Benjamin1d5c83e2014-07-22 19:20:02 -04001154 }
1155
Adam Langley95c29f32014-06-20 12:00:00 -07001156 if err := tlsConn.Handshake(); err != nil {
1157 return err
1158 }
Kenny Root7fdeaf12014-08-05 15:23:37 -07001159
David Benjamin01fe8202014-09-24 15:21:44 -04001160 // TODO(davidben): move all per-connection expectations into a dedicated
1161 // expectations struct that can be specified separately for the two
1162 // legs.
1163 expectedVersion := test.expectedVersion
1164 if isResume && test.expectedResumeVersion != 0 {
1165 expectedVersion = test.expectedResumeVersion
1166 }
1167 if vers := tlsConn.ConnectionState().Version; expectedVersion != 0 && vers != expectedVersion {
1168 return fmt.Errorf("got version %x, expected %x", vers, expectedVersion)
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001169 }
1170
David Benjamin90da8c82015-04-20 14:57:57 -04001171 if cipher := tlsConn.ConnectionState().CipherSuite; test.expectedCipher != 0 && cipher != test.expectedCipher {
1172 return fmt.Errorf("got cipher %x, expected %x", cipher, test.expectedCipher)
1173 }
1174
David Benjamina08e49d2014-08-24 01:46:07 -04001175 if test.expectChannelID {
1176 channelID := tlsConn.ConnectionState().ChannelID
1177 if channelID == nil {
1178 return fmt.Errorf("no channel ID negotiated")
1179 }
1180 if channelID.Curve != channelIDKey.Curve ||
1181 channelIDKey.X.Cmp(channelIDKey.X) != 0 ||
1182 channelIDKey.Y.Cmp(channelIDKey.Y) != 0 {
1183 return fmt.Errorf("incorrect channel ID")
1184 }
1185 }
1186
David Benjaminae2888f2014-09-06 12:58:58 -04001187 if expected := test.expectedNextProto; expected != "" {
1188 if actual := tlsConn.ConnectionState().NegotiatedProtocol; actual != expected {
1189 return fmt.Errorf("next proto mismatch: got %s, wanted %s", actual, expected)
1190 }
1191 }
1192
David Benjaminfc7b0862014-09-06 13:21:53 -04001193 if test.expectedNextProtoType != 0 {
1194 if (test.expectedNextProtoType == alpn) != tlsConn.ConnectionState().NegotiatedProtocolFromALPN {
1195 return fmt.Errorf("next proto type mismatch")
1196 }
1197 }
1198
David Benjaminca6c8262014-11-15 19:06:08 -05001199 if p := tlsConn.ConnectionState().SRTPProtectionProfile; p != test.expectedSRTPProtectionProfile {
1200 return fmt.Errorf("SRTP profile mismatch: got %d, wanted %d", p, test.expectedSRTPProtectionProfile)
1201 }
1202
David Benjaminc565ebb2015-04-03 04:06:36 -04001203 if test.exportKeyingMaterial > 0 {
1204 actual := make([]byte, test.exportKeyingMaterial)
1205 if _, err := io.ReadFull(tlsConn, actual); err != nil {
1206 return err
1207 }
1208 expected, err := tlsConn.ExportKeyingMaterial(test.exportKeyingMaterial, []byte(test.exportLabel), []byte(test.exportContext), test.useExportContext)
1209 if err != nil {
1210 return err
1211 }
1212 if !bytes.Equal(actual, expected) {
1213 return fmt.Errorf("keying material mismatch")
1214 }
1215 }
1216
David Benjamine58c4f52014-08-24 03:47:07 -04001217 if test.shimWritesFirst {
1218 var buf [5]byte
1219 _, err := io.ReadFull(tlsConn, buf[:])
1220 if err != nil {
1221 return err
1222 }
1223 if string(buf[:]) != "hello" {
1224 return fmt.Errorf("bad initial message")
1225 }
1226 }
1227
Adam Langleycf2d4f42014-10-28 19:06:14 -07001228 if test.renegotiate {
1229 if test.renegotiateCiphers != nil {
1230 config.CipherSuites = test.renegotiateCiphers
1231 }
1232 if err := tlsConn.Renegotiate(); err != nil {
1233 return err
1234 }
1235 } else if test.renegotiateCiphers != nil {
1236 panic("renegotiateCiphers without renegotiate")
1237 }
1238
David Benjamin5fa3eba2015-01-22 16:35:40 -05001239 if test.damageFirstWrite {
1240 connDamage.setDamage(true)
1241 tlsConn.Write([]byte("DAMAGED WRITE"))
1242 connDamage.setDamage(false)
1243 }
1244
Kenny Root7fdeaf12014-08-05 15:23:37 -07001245 if messageLen < 0 {
David Benjamin6fd297b2014-08-11 18:43:38 -04001246 if test.protocol == dtls {
1247 return fmt.Errorf("messageLen < 0 not supported for DTLS tests")
1248 }
Kenny Root7fdeaf12014-08-05 15:23:37 -07001249 // Read until EOF.
1250 _, err := io.Copy(ioutil.Discard, tlsConn)
1251 return err
1252 }
1253
David Benjamin4417d052015-04-05 04:17:25 -04001254 if messageLen == 0 {
1255 messageLen = 32
Adam Langley80842bd2014-06-20 12:00:00 -07001256 }
David Benjamin4417d052015-04-05 04:17:25 -04001257 testMessage := make([]byte, messageLen)
1258 for i := range testMessage {
1259 testMessage[i] = 0x42
1260 }
1261 tlsConn.Write(testMessage)
Adam Langley95c29f32014-06-20 12:00:00 -07001262
1263 buf := make([]byte, len(testMessage))
David Benjamin6fd297b2014-08-11 18:43:38 -04001264 if test.protocol == dtls {
1265 bufTmp := make([]byte, len(buf)+1)
1266 n, err := tlsConn.Read(bufTmp)
1267 if err != nil {
1268 return err
1269 }
1270 if n != len(buf) {
1271 return fmt.Errorf("bad reply; length mismatch (%d vs %d)", n, len(buf))
1272 }
1273 copy(buf, bufTmp)
1274 } else {
1275 _, err := io.ReadFull(tlsConn, buf)
1276 if err != nil {
1277 return err
1278 }
Adam Langley95c29f32014-06-20 12:00:00 -07001279 }
1280
1281 for i, v := range buf {
1282 if v != testMessage[i]^0xff {
1283 return fmt.Errorf("bad reply contents at byte %d", i)
1284 }
1285 }
1286
1287 return nil
1288}
1289
David Benjamin325b5c32014-07-01 19:40:31 -04001290func valgrindOf(dbAttach bool, path string, args ...string) *exec.Cmd {
1291 valgrindArgs := []string{"--error-exitcode=99", "--track-origins=yes", "--leak-check=full"}
Adam Langley95c29f32014-06-20 12:00:00 -07001292 if dbAttach {
David Benjamin325b5c32014-07-01 19:40:31 -04001293 valgrindArgs = append(valgrindArgs, "--db-attach=yes", "--db-command=xterm -e gdb -nw %f %p")
Adam Langley95c29f32014-06-20 12:00:00 -07001294 }
David Benjamin325b5c32014-07-01 19:40:31 -04001295 valgrindArgs = append(valgrindArgs, path)
1296 valgrindArgs = append(valgrindArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -07001297
David Benjamin325b5c32014-07-01 19:40:31 -04001298 return exec.Command("valgrind", valgrindArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -07001299}
1300
David Benjamin325b5c32014-07-01 19:40:31 -04001301func gdbOf(path string, args ...string) *exec.Cmd {
1302 xtermArgs := []string{"-e", "gdb", "--args"}
1303 xtermArgs = append(xtermArgs, path)
1304 xtermArgs = append(xtermArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -07001305
David Benjamin325b5c32014-07-01 19:40:31 -04001306 return exec.Command("xterm", xtermArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -07001307}
1308
Adam Langley69a01602014-11-17 17:26:55 -08001309type moreMallocsError struct{}
1310
1311func (moreMallocsError) Error() string {
1312 return "child process did not exhaust all allocation calls"
1313}
1314
1315var errMoreMallocs = moreMallocsError{}
1316
David Benjamin87c8a642015-02-21 01:54:29 -05001317// accept accepts a connection from listener, unless waitChan signals a process
1318// exit first.
1319func acceptOrWait(listener net.Listener, waitChan chan error) (net.Conn, error) {
1320 type connOrError struct {
1321 conn net.Conn
1322 err error
1323 }
1324 connChan := make(chan connOrError, 1)
1325 go func() {
1326 conn, err := listener.Accept()
1327 connChan <- connOrError{conn, err}
1328 close(connChan)
1329 }()
1330 select {
1331 case result := <-connChan:
1332 return result.conn, result.err
1333 case childErr := <-waitChan:
1334 waitChan <- childErr
1335 return nil, fmt.Errorf("child exited early: %s", childErr)
1336 }
1337}
1338
Adam Langley69a01602014-11-17 17:26:55 -08001339func runTest(test *testCase, buildDir string, mallocNumToFail int64) error {
Adam Langley38311732014-10-16 19:04:35 -07001340 if !test.shouldFail && (len(test.expectedError) > 0 || len(test.expectedLocalError) > 0) {
1341 panic("Error expected without shouldFail in " + test.name)
1342 }
1343
David Benjamin87c8a642015-02-21 01:54:29 -05001344 listener, err := net.ListenTCP("tcp4", &net.TCPAddr{IP: net.IP{127, 0, 0, 1}})
1345 if err != nil {
1346 panic(err)
1347 }
1348 defer func() {
1349 if listener != nil {
1350 listener.Close()
1351 }
1352 }()
Adam Langley95c29f32014-06-20 12:00:00 -07001353
David Benjamin884fdf12014-08-02 15:28:23 -04001354 shim_path := path.Join(buildDir, "ssl/test/bssl_shim")
David Benjamin87c8a642015-02-21 01:54:29 -05001355 flags := []string{"-port", strconv.Itoa(listener.Addr().(*net.TCPAddr).Port)}
David Benjamin1d5c83e2014-07-22 19:20:02 -04001356 if test.testType == serverTest {
David Benjamin5a593af2014-08-11 19:51:50 -04001357 flags = append(flags, "-server")
1358
David Benjamin025b3d32014-07-01 19:53:04 -04001359 flags = append(flags, "-key-file")
1360 if test.keyFile == "" {
1361 flags = append(flags, rsaKeyFile)
1362 } else {
1363 flags = append(flags, test.keyFile)
1364 }
1365
1366 flags = append(flags, "-cert-file")
1367 if test.certFile == "" {
1368 flags = append(flags, rsaCertificateFile)
1369 } else {
1370 flags = append(flags, test.certFile)
1371 }
1372 }
David Benjamin5a593af2014-08-11 19:51:50 -04001373
David Benjamin6fd297b2014-08-11 18:43:38 -04001374 if test.protocol == dtls {
1375 flags = append(flags, "-dtls")
1376 }
1377
David Benjamin5a593af2014-08-11 19:51:50 -04001378 if test.resumeSession {
1379 flags = append(flags, "-resume")
1380 }
1381
David Benjamine58c4f52014-08-24 03:47:07 -04001382 if test.shimWritesFirst {
1383 flags = append(flags, "-shim-writes-first")
1384 }
1385
David Benjaminc565ebb2015-04-03 04:06:36 -04001386 if test.exportKeyingMaterial > 0 {
1387 flags = append(flags, "-export-keying-material", strconv.Itoa(test.exportKeyingMaterial))
1388 flags = append(flags, "-export-label", test.exportLabel)
1389 flags = append(flags, "-export-context", test.exportContext)
1390 if test.useExportContext {
1391 flags = append(flags, "-use-export-context")
1392 }
1393 }
1394
David Benjamin025b3d32014-07-01 19:53:04 -04001395 flags = append(flags, test.flags...)
1396
1397 var shim *exec.Cmd
1398 if *useValgrind {
1399 shim = valgrindOf(false, shim_path, flags...)
Adam Langley75712922014-10-10 16:23:43 -07001400 } else if *useGDB {
1401 shim = gdbOf(shim_path, flags...)
David Benjamin025b3d32014-07-01 19:53:04 -04001402 } else {
1403 shim = exec.Command(shim_path, flags...)
1404 }
David Benjamin025b3d32014-07-01 19:53:04 -04001405 shim.Stdin = os.Stdin
1406 var stdoutBuf, stderrBuf bytes.Buffer
1407 shim.Stdout = &stdoutBuf
1408 shim.Stderr = &stderrBuf
Adam Langley69a01602014-11-17 17:26:55 -08001409 if mallocNumToFail >= 0 {
David Benjamin9e128b02015-02-09 13:13:09 -05001410 shim.Env = os.Environ()
1411 shim.Env = append(shim.Env, "MALLOC_NUMBER_TO_FAIL="+strconv.FormatInt(mallocNumToFail, 10))
Adam Langley69a01602014-11-17 17:26:55 -08001412 if *mallocTestDebug {
1413 shim.Env = append(shim.Env, "MALLOC_ABORT_ON_FAIL=1")
1414 }
1415 shim.Env = append(shim.Env, "_MALLOC_CHECK=1")
1416 }
David Benjamin025b3d32014-07-01 19:53:04 -04001417
1418 if err := shim.Start(); err != nil {
Adam Langley95c29f32014-06-20 12:00:00 -07001419 panic(err)
1420 }
David Benjamin87c8a642015-02-21 01:54:29 -05001421 waitChan := make(chan error, 1)
1422 go func() { waitChan <- shim.Wait() }()
Adam Langley95c29f32014-06-20 12:00:00 -07001423
1424 config := test.config
David Benjamin1d5c83e2014-07-22 19:20:02 -04001425 config.ClientSessionCache = NewLRUClientSessionCache(1)
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001426 config.ServerSessionCache = NewLRUServerSessionCache(1)
David Benjamin025b3d32014-07-01 19:53:04 -04001427 if test.testType == clientTest {
1428 if len(config.Certificates) == 0 {
1429 config.Certificates = []Certificate{getRSACertificate()}
1430 }
David Benjamin87c8a642015-02-21 01:54:29 -05001431 } else {
1432 // Supply a ServerName to ensure a constant session cache key,
1433 // rather than falling back to net.Conn.RemoteAddr.
1434 if len(config.ServerName) == 0 {
1435 config.ServerName = "test"
1436 }
David Benjamin025b3d32014-07-01 19:53:04 -04001437 }
Adam Langley95c29f32014-06-20 12:00:00 -07001438
David Benjamin87c8a642015-02-21 01:54:29 -05001439 conn, err := acceptOrWait(listener, waitChan)
1440 if err == nil {
1441 err = doExchange(test, &config, conn, test.messageLen, false /* not a resumption */)
1442 conn.Close()
1443 }
David Benjamin65ea8ff2014-11-23 03:01:00 -05001444
David Benjamin1d5c83e2014-07-22 19:20:02 -04001445 if err == nil && test.resumeSession {
David Benjamin01fe8202014-09-24 15:21:44 -04001446 var resumeConfig Config
1447 if test.resumeConfig != nil {
1448 resumeConfig = *test.resumeConfig
David Benjamin87c8a642015-02-21 01:54:29 -05001449 if len(resumeConfig.ServerName) == 0 {
1450 resumeConfig.ServerName = config.ServerName
1451 }
David Benjamin01fe8202014-09-24 15:21:44 -04001452 if len(resumeConfig.Certificates) == 0 {
1453 resumeConfig.Certificates = []Certificate{getRSACertificate()}
1454 }
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001455 if !test.newSessionsOnResume {
1456 resumeConfig.SessionTicketKey = config.SessionTicketKey
1457 resumeConfig.ClientSessionCache = config.ClientSessionCache
1458 resumeConfig.ServerSessionCache = config.ServerSessionCache
1459 }
David Benjamin01fe8202014-09-24 15:21:44 -04001460 } else {
1461 resumeConfig = config
1462 }
David Benjamin87c8a642015-02-21 01:54:29 -05001463 var connResume net.Conn
1464 connResume, err = acceptOrWait(listener, waitChan)
1465 if err == nil {
1466 err = doExchange(test, &resumeConfig, connResume, test.messageLen, true /* resumption */)
1467 connResume.Close()
1468 }
David Benjamin1d5c83e2014-07-22 19:20:02 -04001469 }
1470
David Benjamin87c8a642015-02-21 01:54:29 -05001471 // Close the listener now. This is to avoid hangs should the shim try to
1472 // open more connections than expected.
1473 listener.Close()
1474 listener = nil
1475
1476 childErr := <-waitChan
Adam Langley69a01602014-11-17 17:26:55 -08001477 if exitError, ok := childErr.(*exec.ExitError); ok {
1478 if exitError.Sys().(syscall.WaitStatus).ExitStatus() == 88 {
1479 return errMoreMallocs
1480 }
1481 }
Adam Langley95c29f32014-06-20 12:00:00 -07001482
1483 stdout := string(stdoutBuf.Bytes())
1484 stderr := string(stderrBuf.Bytes())
1485 failed := err != nil || childErr != nil
David Benjaminc565ebb2015-04-03 04:06:36 -04001486 correctFailure := len(test.expectedError) == 0 || strings.Contains(stderr, test.expectedError)
Adam Langleyac61fa32014-06-23 12:03:11 -07001487 localError := "none"
1488 if err != nil {
1489 localError = err.Error()
1490 }
1491 if len(test.expectedLocalError) != 0 {
1492 correctFailure = correctFailure && strings.Contains(localError, test.expectedLocalError)
1493 }
Adam Langley95c29f32014-06-20 12:00:00 -07001494
1495 if failed != test.shouldFail || failed && !correctFailure {
Adam Langley95c29f32014-06-20 12:00:00 -07001496 childError := "none"
Adam Langley95c29f32014-06-20 12:00:00 -07001497 if childErr != nil {
1498 childError = childErr.Error()
1499 }
1500
1501 var msg string
1502 switch {
1503 case failed && !test.shouldFail:
1504 msg = "unexpected failure"
1505 case !failed && test.shouldFail:
1506 msg = "unexpected success"
1507 case failed && !correctFailure:
Adam Langleyac61fa32014-06-23 12:03:11 -07001508 msg = "bad error (wanted '" + test.expectedError + "' / '" + test.expectedLocalError + "')"
Adam Langley95c29f32014-06-20 12:00:00 -07001509 default:
1510 panic("internal error")
1511 }
1512
David Benjaminc565ebb2015-04-03 04:06:36 -04001513 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 -07001514 }
1515
David Benjaminc565ebb2015-04-03 04:06:36 -04001516 if !*useValgrind && !failed && len(stderr) > 0 {
Adam Langley95c29f32014-06-20 12:00:00 -07001517 println(stderr)
1518 }
1519
1520 return nil
1521}
1522
1523var tlsVersions = []struct {
1524 name string
1525 version uint16
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001526 flag string
David Benjamin8b8c0062014-11-23 02:47:52 -05001527 hasDTLS bool
Adam Langley95c29f32014-06-20 12:00:00 -07001528}{
David Benjamin8b8c0062014-11-23 02:47:52 -05001529 {"SSL3", VersionSSL30, "-no-ssl3", false},
1530 {"TLS1", VersionTLS10, "-no-tls1", true},
1531 {"TLS11", VersionTLS11, "-no-tls11", false},
1532 {"TLS12", VersionTLS12, "-no-tls12", true},
Adam Langley95c29f32014-06-20 12:00:00 -07001533}
1534
1535var testCipherSuites = []struct {
1536 name string
1537 id uint16
1538}{
1539 {"3DES-SHA", TLS_RSA_WITH_3DES_EDE_CBC_SHA},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001540 {"AES128-GCM", TLS_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001541 {"AES128-SHA", TLS_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001542 {"AES128-SHA256", TLS_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001543 {"AES256-GCM", TLS_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001544 {"AES256-SHA", TLS_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001545 {"AES256-SHA256", TLS_RSA_WITH_AES_256_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001546 {"DHE-RSA-AES128-GCM", TLS_DHE_RSA_WITH_AES_128_GCM_SHA256},
1547 {"DHE-RSA-AES128-SHA", TLS_DHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001548 {"DHE-RSA-AES128-SHA256", TLS_DHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001549 {"DHE-RSA-AES256-GCM", TLS_DHE_RSA_WITH_AES_256_GCM_SHA384},
1550 {"DHE-RSA-AES256-SHA", TLS_DHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001551 {"DHE-RSA-AES256-SHA256", TLS_DHE_RSA_WITH_AES_256_CBC_SHA256},
David Benjamine9a80ff2015-04-07 00:46:46 -04001552 {"DHE-RSA-CHACHA20-POLY1305", TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001553 {"ECDHE-ECDSA-AES128-GCM", TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
1554 {"ECDHE-ECDSA-AES128-SHA", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001555 {"ECDHE-ECDSA-AES128-SHA256", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256},
1556 {"ECDHE-ECDSA-AES256-GCM", TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001557 {"ECDHE-ECDSA-AES256-SHA", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001558 {"ECDHE-ECDSA-AES256-SHA384", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384},
David Benjamine9a80ff2015-04-07 00:46:46 -04001559 {"ECDHE-ECDSA-CHACHA20-POLY1305", TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001560 {"ECDHE-ECDSA-RC4-SHA", TLS_ECDHE_ECDSA_WITH_RC4_128_SHA},
Adam Langley97e8ba82015-04-29 15:32:10 -07001561 {"ECDHE-PSK-AES128-GCM-SHA256", TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001562 {"ECDHE-RSA-AES128-GCM", TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001563 {"ECDHE-RSA-AES128-SHA", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001564 {"ECDHE-RSA-AES128-SHA256", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001565 {"ECDHE-RSA-AES256-GCM", TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001566 {"ECDHE-RSA-AES256-SHA", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001567 {"ECDHE-RSA-AES256-SHA384", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384},
David Benjamine9a80ff2015-04-07 00:46:46 -04001568 {"ECDHE-RSA-CHACHA20-POLY1305", TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001569 {"ECDHE-RSA-RC4-SHA", TLS_ECDHE_RSA_WITH_RC4_128_SHA},
David Benjamin48cae082014-10-27 01:06:24 -04001570 {"PSK-AES128-CBC-SHA", TLS_PSK_WITH_AES_128_CBC_SHA},
1571 {"PSK-AES256-CBC-SHA", TLS_PSK_WITH_AES_256_CBC_SHA},
1572 {"PSK-RC4-SHA", TLS_PSK_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -07001573 {"RC4-MD5", TLS_RSA_WITH_RC4_128_MD5},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001574 {"RC4-SHA", TLS_RSA_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -07001575}
1576
David Benjamin8b8c0062014-11-23 02:47:52 -05001577func hasComponent(suiteName, component string) bool {
1578 return strings.Contains("-"+suiteName+"-", "-"+component+"-")
1579}
1580
David Benjaminf7768e42014-08-31 02:06:47 -04001581func isTLS12Only(suiteName string) bool {
David Benjamin8b8c0062014-11-23 02:47:52 -05001582 return hasComponent(suiteName, "GCM") ||
1583 hasComponent(suiteName, "SHA256") ||
David Benjamine9a80ff2015-04-07 00:46:46 -04001584 hasComponent(suiteName, "SHA384") ||
1585 hasComponent(suiteName, "POLY1305")
David Benjamin8b8c0062014-11-23 02:47:52 -05001586}
1587
1588func isDTLSCipher(suiteName string) bool {
David Benjamine95d20d2014-12-23 11:16:01 -05001589 return !hasComponent(suiteName, "RC4")
David Benjaminf7768e42014-08-31 02:06:47 -04001590}
1591
Adam Langleya7997f12015-05-14 17:38:50 -07001592func bigFromHex(hex string) *big.Int {
1593 ret, ok := new(big.Int).SetString(hex, 16)
1594 if !ok {
1595 panic("failed to parse hex number 0x" + hex)
1596 }
1597 return ret
1598}
1599
Adam Langley95c29f32014-06-20 12:00:00 -07001600func addCipherSuiteTests() {
1601 for _, suite := range testCipherSuites {
David Benjamin48cae082014-10-27 01:06:24 -04001602 const psk = "12345"
1603 const pskIdentity = "luggage combo"
1604
Adam Langley95c29f32014-06-20 12:00:00 -07001605 var cert Certificate
David Benjamin025b3d32014-07-01 19:53:04 -04001606 var certFile string
1607 var keyFile string
David Benjamin8b8c0062014-11-23 02:47:52 -05001608 if hasComponent(suite.name, "ECDSA") {
Adam Langley95c29f32014-06-20 12:00:00 -07001609 cert = getECDSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -04001610 certFile = ecdsaCertificateFile
1611 keyFile = ecdsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -07001612 } else {
1613 cert = getRSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -04001614 certFile = rsaCertificateFile
1615 keyFile = rsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -07001616 }
1617
David Benjamin48cae082014-10-27 01:06:24 -04001618 var flags []string
David Benjamin8b8c0062014-11-23 02:47:52 -05001619 if hasComponent(suite.name, "PSK") {
David Benjamin48cae082014-10-27 01:06:24 -04001620 flags = append(flags,
1621 "-psk", psk,
1622 "-psk-identity", pskIdentity)
1623 }
1624
Adam Langley95c29f32014-06-20 12:00:00 -07001625 for _, ver := range tlsVersions {
David Benjaminf7768e42014-08-31 02:06:47 -04001626 if ver.version < VersionTLS12 && isTLS12Only(suite.name) {
Adam Langley95c29f32014-06-20 12:00:00 -07001627 continue
1628 }
1629
David Benjamin025b3d32014-07-01 19:53:04 -04001630 testCases = append(testCases, testCase{
1631 testType: clientTest,
1632 name: ver.name + "-" + suite.name + "-client",
Adam Langley95c29f32014-06-20 12:00:00 -07001633 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001634 MinVersion: ver.version,
1635 MaxVersion: ver.version,
1636 CipherSuites: []uint16{suite.id},
1637 Certificates: []Certificate{cert},
David Benjamin68793732015-05-04 20:20:48 -04001638 PreSharedKey: []byte(psk),
David Benjamin48cae082014-10-27 01:06:24 -04001639 PreSharedKeyIdentity: pskIdentity,
Adam Langley95c29f32014-06-20 12:00:00 -07001640 },
David Benjamin48cae082014-10-27 01:06:24 -04001641 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001642 resumeSession: true,
Adam Langley95c29f32014-06-20 12:00:00 -07001643 })
David Benjamin025b3d32014-07-01 19:53:04 -04001644
David Benjamin76d8abe2014-08-14 16:25:34 -04001645 testCases = append(testCases, testCase{
1646 testType: serverTest,
1647 name: ver.name + "-" + suite.name + "-server",
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 Benjamin76d8abe2014-08-14 16:25:34 -04001655 },
1656 certFile: certFile,
1657 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001658 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001659 resumeSession: true,
David Benjamin76d8abe2014-08-14 16:25:34 -04001660 })
David Benjamin6fd297b2014-08-11 18:43:38 -04001661
David Benjamin8b8c0062014-11-23 02:47:52 -05001662 if ver.hasDTLS && isDTLSCipher(suite.name) {
David Benjamin6fd297b2014-08-11 18:43:38 -04001663 testCases = append(testCases, testCase{
1664 testType: clientTest,
1665 protocol: dtls,
1666 name: "D" + ver.name + "-" + suite.name + "-client",
1667 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001668 MinVersion: ver.version,
1669 MaxVersion: ver.version,
1670 CipherSuites: []uint16{suite.id},
1671 Certificates: []Certificate{cert},
1672 PreSharedKey: []byte(psk),
1673 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001674 },
David Benjamin48cae082014-10-27 01:06:24 -04001675 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001676 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001677 })
1678 testCases = append(testCases, testCase{
1679 testType: serverTest,
1680 protocol: dtls,
1681 name: "D" + ver.name + "-" + suite.name + "-server",
1682 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001683 MinVersion: ver.version,
1684 MaxVersion: ver.version,
1685 CipherSuites: []uint16{suite.id},
1686 Certificates: []Certificate{cert},
1687 PreSharedKey: []byte(psk),
1688 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001689 },
1690 certFile: certFile,
1691 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001692 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001693 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001694 })
1695 }
Adam Langley95c29f32014-06-20 12:00:00 -07001696 }
1697 }
Adam Langleya7997f12015-05-14 17:38:50 -07001698
1699 testCases = append(testCases, testCase{
1700 name: "WeakDH",
1701 config: Config{
1702 CipherSuites: []uint16{TLS_DHE_RSA_WITH_AES_128_GCM_SHA256},
1703 Bugs: ProtocolBugs{
1704 // This is a 1023-bit prime number, generated
1705 // with:
1706 // openssl gendh 1023 | openssl asn1parse -i
1707 DHGroupPrime: bigFromHex("518E9B7930CE61C6E445C8360584E5FC78D9137C0FFDC880B495D5338ADF7689951A6821C17A76B3ACB8E0156AEA607B7EC406EBEDBB84D8376EB8FE8F8BA1433488BEE0C3EDDFD3A32DBB9481980A7AF6C96BFCF490A094CFFB2B8192C1BB5510B77B658436E27C2D4D023FE3718222AB0CA1273995B51F6D625A4944D0DD4B"),
1708 },
1709 },
1710 shouldFail: true,
1711 expectedError: "BAD_DH_P_LENGTH",
1712 })
Adam Langley95c29f32014-06-20 12:00:00 -07001713}
1714
1715func addBadECDSASignatureTests() {
1716 for badR := BadValue(1); badR < NumBadValues; badR++ {
1717 for badS := BadValue(1); badS < NumBadValues; badS++ {
David Benjamin025b3d32014-07-01 19:53:04 -04001718 testCases = append(testCases, testCase{
Adam Langley95c29f32014-06-20 12:00:00 -07001719 name: fmt.Sprintf("BadECDSA-%d-%d", badR, badS),
1720 config: Config{
1721 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
1722 Certificates: []Certificate{getECDSACertificate()},
1723 Bugs: ProtocolBugs{
1724 BadECDSAR: badR,
1725 BadECDSAS: badS,
1726 },
1727 },
1728 shouldFail: true,
1729 expectedError: "SIGNATURE",
1730 })
1731 }
1732 }
1733}
1734
Adam Langley80842bd2014-06-20 12:00:00 -07001735func addCBCPaddingTests() {
David Benjamin025b3d32014-07-01 19:53:04 -04001736 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001737 name: "MaxCBCPadding",
1738 config: Config{
1739 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1740 Bugs: ProtocolBugs{
1741 MaxPadding: true,
1742 },
1743 },
1744 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1745 })
David Benjamin025b3d32014-07-01 19:53:04 -04001746 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001747 name: "BadCBCPadding",
1748 config: Config{
1749 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1750 Bugs: ProtocolBugs{
1751 PaddingFirstByteBad: true,
1752 },
1753 },
1754 shouldFail: true,
1755 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1756 })
1757 // OpenSSL previously had an issue where the first byte of padding in
1758 // 255 bytes of padding wasn't checked.
David Benjamin025b3d32014-07-01 19:53:04 -04001759 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001760 name: "BadCBCPadding255",
1761 config: Config{
1762 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1763 Bugs: ProtocolBugs{
1764 MaxPadding: true,
1765 PaddingFirstByteBadIf255: true,
1766 },
1767 },
1768 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1769 shouldFail: true,
1770 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1771 })
1772}
1773
Kenny Root7fdeaf12014-08-05 15:23:37 -07001774func addCBCSplittingTests() {
1775 testCases = append(testCases, testCase{
1776 name: "CBCRecordSplitting",
1777 config: Config{
1778 MaxVersion: VersionTLS10,
1779 MinVersion: VersionTLS10,
1780 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1781 },
1782 messageLen: -1, // read until EOF
1783 flags: []string{
1784 "-async",
1785 "-write-different-record-sizes",
1786 "-cbc-record-splitting",
1787 },
David Benjamina8e3e0e2014-08-06 22:11:10 -04001788 })
1789 testCases = append(testCases, testCase{
Kenny Root7fdeaf12014-08-05 15:23:37 -07001790 name: "CBCRecordSplittingPartialWrite",
1791 config: Config{
1792 MaxVersion: VersionTLS10,
1793 MinVersion: VersionTLS10,
1794 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1795 },
1796 messageLen: -1, // read until EOF
1797 flags: []string{
1798 "-async",
1799 "-write-different-record-sizes",
1800 "-cbc-record-splitting",
1801 "-partial-write",
1802 },
1803 })
1804}
1805
David Benjamin636293b2014-07-08 17:59:18 -04001806func addClientAuthTests() {
David Benjamin407a10c2014-07-16 12:58:59 -04001807 // Add a dummy cert pool to stress certificate authority parsing.
1808 // TODO(davidben): Add tests that those values parse out correctly.
1809 certPool := x509.NewCertPool()
1810 cert, err := x509.ParseCertificate(rsaCertificate.Certificate[0])
1811 if err != nil {
1812 panic(err)
1813 }
1814 certPool.AddCert(cert)
1815
David Benjamin636293b2014-07-08 17:59:18 -04001816 for _, ver := range tlsVersions {
David Benjamin636293b2014-07-08 17:59:18 -04001817 testCases = append(testCases, testCase{
1818 testType: clientTest,
David Benjamin67666e72014-07-12 15:47:52 -04001819 name: ver.name + "-Client-ClientAuth-RSA",
David Benjamin636293b2014-07-08 17:59:18 -04001820 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001821 MinVersion: ver.version,
1822 MaxVersion: ver.version,
1823 ClientAuth: RequireAnyClientCert,
1824 ClientCAs: certPool,
David Benjamin636293b2014-07-08 17:59:18 -04001825 },
1826 flags: []string{
1827 "-cert-file", rsaCertificateFile,
1828 "-key-file", rsaKeyFile,
1829 },
1830 })
1831 testCases = append(testCases, testCase{
David Benjamin67666e72014-07-12 15:47:52 -04001832 testType: serverTest,
1833 name: ver.name + "-Server-ClientAuth-RSA",
1834 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001835 MinVersion: ver.version,
1836 MaxVersion: ver.version,
David Benjamin67666e72014-07-12 15:47:52 -04001837 Certificates: []Certificate{rsaCertificate},
1838 },
1839 flags: []string{"-require-any-client-certificate"},
1840 })
David Benjamine098ec22014-08-27 23:13:20 -04001841 if ver.version != VersionSSL30 {
1842 testCases = append(testCases, testCase{
1843 testType: serverTest,
1844 name: ver.name + "-Server-ClientAuth-ECDSA",
1845 config: Config{
1846 MinVersion: ver.version,
1847 MaxVersion: ver.version,
1848 Certificates: []Certificate{ecdsaCertificate},
1849 },
1850 flags: []string{"-require-any-client-certificate"},
1851 })
1852 testCases = append(testCases, testCase{
1853 testType: clientTest,
1854 name: ver.name + "-Client-ClientAuth-ECDSA",
1855 config: Config{
1856 MinVersion: ver.version,
1857 MaxVersion: ver.version,
1858 ClientAuth: RequireAnyClientCert,
1859 ClientCAs: certPool,
1860 },
1861 flags: []string{
1862 "-cert-file", ecdsaCertificateFile,
1863 "-key-file", ecdsaKeyFile,
1864 },
1865 })
1866 }
David Benjamin636293b2014-07-08 17:59:18 -04001867 }
1868}
1869
Adam Langley75712922014-10-10 16:23:43 -07001870func addExtendedMasterSecretTests() {
1871 const expectEMSFlag = "-expect-extended-master-secret"
1872
1873 for _, with := range []bool{false, true} {
1874 prefix := "No"
1875 var flags []string
1876 if with {
1877 prefix = ""
1878 flags = []string{expectEMSFlag}
1879 }
1880
1881 for _, isClient := range []bool{false, true} {
1882 suffix := "-Server"
1883 testType := serverTest
1884 if isClient {
1885 suffix = "-Client"
1886 testType = clientTest
1887 }
1888
1889 for _, ver := range tlsVersions {
1890 test := testCase{
1891 testType: testType,
1892 name: prefix + "ExtendedMasterSecret-" + ver.name + suffix,
1893 config: Config{
1894 MinVersion: ver.version,
1895 MaxVersion: ver.version,
1896 Bugs: ProtocolBugs{
1897 NoExtendedMasterSecret: !with,
1898 RequireExtendedMasterSecret: with,
1899 },
1900 },
David Benjamin48cae082014-10-27 01:06:24 -04001901 flags: flags,
1902 shouldFail: ver.version == VersionSSL30 && with,
Adam Langley75712922014-10-10 16:23:43 -07001903 }
1904 if test.shouldFail {
1905 test.expectedLocalError = "extended master secret required but not supported by peer"
1906 }
1907 testCases = append(testCases, test)
1908 }
1909 }
1910 }
1911
1912 // When a session is resumed, it should still be aware that its master
1913 // secret was generated via EMS and thus it's safe to use tls-unique.
1914 testCases = append(testCases, testCase{
1915 name: "ExtendedMasterSecret-Resume",
1916 config: Config{
1917 Bugs: ProtocolBugs{
1918 RequireExtendedMasterSecret: true,
1919 },
1920 },
1921 flags: []string{expectEMSFlag},
1922 resumeSession: true,
1923 })
1924}
1925
David Benjamin43ec06f2014-08-05 02:28:57 -04001926// Adds tests that try to cover the range of the handshake state machine, under
1927// various conditions. Some of these are redundant with other tests, but they
1928// only cover the synchronous case.
David Benjamin6fd297b2014-08-11 18:43:38 -04001929func addStateMachineCoverageTests(async, splitHandshake bool, protocol protocol) {
David Benjamin760b1dd2015-05-15 23:33:48 -04001930 var tests []testCase
1931
1932 // Basic handshake, with resumption. Client and server,
1933 // session ID and session ticket.
1934 tests = append(tests, testCase{
1935 name: "Basic-Client",
1936 resumeSession: true,
1937 })
1938 tests = append(tests, testCase{
1939 name: "Basic-Client-RenewTicket",
1940 config: Config{
1941 Bugs: ProtocolBugs{
1942 RenewTicketOnResume: true,
1943 },
1944 },
1945 resumeSession: true,
1946 })
1947 tests = append(tests, testCase{
1948 name: "Basic-Client-NoTicket",
1949 config: Config{
1950 SessionTicketsDisabled: true,
1951 },
1952 resumeSession: true,
1953 })
1954 tests = append(tests, testCase{
1955 name: "Basic-Client-Implicit",
1956 flags: []string{"-implicit-handshake"},
1957 resumeSession: true,
1958 })
1959 tests = append(tests, testCase{
1960 testType: serverTest,
1961 name: "Basic-Server",
1962 resumeSession: true,
1963 })
1964 tests = append(tests, testCase{
1965 testType: serverTest,
1966 name: "Basic-Server-NoTickets",
1967 config: Config{
1968 SessionTicketsDisabled: true,
1969 },
1970 resumeSession: true,
1971 })
1972 tests = append(tests, testCase{
1973 testType: serverTest,
1974 name: "Basic-Server-Implicit",
1975 flags: []string{"-implicit-handshake"},
1976 resumeSession: true,
1977 })
1978 tests = append(tests, testCase{
1979 testType: serverTest,
1980 name: "Basic-Server-EarlyCallback",
1981 flags: []string{"-use-early-callback"},
1982 resumeSession: true,
1983 })
1984
1985 // TLS client auth.
1986 tests = append(tests, testCase{
1987 testType: clientTest,
1988 name: "ClientAuth-Client",
1989 config: Config{
1990 ClientAuth: RequireAnyClientCert,
1991 },
1992 flags: []string{
1993 "-cert-file", rsaCertificateFile,
1994 "-key-file", rsaKeyFile,
1995 },
1996 })
1997 tests = append(tests, testCase{
1998 testType: serverTest,
1999 name: "ClientAuth-Server",
2000 config: Config{
2001 Certificates: []Certificate{rsaCertificate},
2002 },
2003 flags: []string{"-require-any-client-certificate"},
2004 })
2005
2006 // No session ticket support; server doesn't send NewSessionTicket.
2007 tests = append(tests, testCase{
2008 name: "SessionTicketsDisabled-Client",
2009 config: Config{
2010 SessionTicketsDisabled: true,
2011 },
2012 })
2013 tests = append(tests, testCase{
2014 testType: serverTest,
2015 name: "SessionTicketsDisabled-Server",
2016 config: Config{
2017 SessionTicketsDisabled: true,
2018 },
2019 })
2020
2021 // Skip ServerKeyExchange in PSK key exchange if there's no
2022 // identity hint.
2023 tests = append(tests, testCase{
2024 name: "EmptyPSKHint-Client",
2025 config: Config{
2026 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
2027 PreSharedKey: []byte("secret"),
2028 },
2029 flags: []string{"-psk", "secret"},
2030 })
2031 tests = append(tests, testCase{
2032 testType: serverTest,
2033 name: "EmptyPSKHint-Server",
2034 config: Config{
2035 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
2036 PreSharedKey: []byte("secret"),
2037 },
2038 flags: []string{"-psk", "secret"},
2039 })
2040
2041 if protocol == tls {
2042 tests = append(tests, testCase{
2043 name: "Renegotiate-Client",
2044 renegotiate: true,
2045 })
2046 // NPN on client and server; results in post-handshake message.
2047 tests = append(tests, testCase{
2048 name: "NPN-Client",
2049 config: Config{
2050 NextProtos: []string{"foo"},
2051 },
2052 flags: []string{"-select-next-proto", "foo"},
2053 expectedNextProto: "foo",
2054 expectedNextProtoType: npn,
2055 })
2056 tests = append(tests, testCase{
2057 testType: serverTest,
2058 name: "NPN-Server",
2059 config: Config{
2060 NextProtos: []string{"bar"},
2061 },
2062 flags: []string{
2063 "-advertise-npn", "\x03foo\x03bar\x03baz",
2064 "-expect-next-proto", "bar",
2065 },
2066 expectedNextProto: "bar",
2067 expectedNextProtoType: npn,
2068 })
2069
2070 // TODO(davidben): Add tests for when False Start doesn't trigger.
2071
2072 // Client does False Start and negotiates NPN.
2073 tests = append(tests, testCase{
2074 name: "FalseStart",
2075 config: Config{
2076 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2077 NextProtos: []string{"foo"},
2078 Bugs: ProtocolBugs{
2079 ExpectFalseStart: true,
2080 },
2081 },
2082 flags: []string{
2083 "-false-start",
2084 "-select-next-proto", "foo",
2085 },
2086 shimWritesFirst: true,
2087 resumeSession: true,
2088 })
2089
2090 // Client does False Start and negotiates ALPN.
2091 tests = append(tests, testCase{
2092 name: "FalseStart-ALPN",
2093 config: Config{
2094 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2095 NextProtos: []string{"foo"},
2096 Bugs: ProtocolBugs{
2097 ExpectFalseStart: true,
2098 },
2099 },
2100 flags: []string{
2101 "-false-start",
2102 "-advertise-alpn", "\x03foo",
2103 },
2104 shimWritesFirst: true,
2105 resumeSession: true,
2106 })
2107
2108 // Client does False Start but doesn't explicitly call
2109 // SSL_connect.
2110 tests = append(tests, testCase{
2111 name: "FalseStart-Implicit",
2112 config: Config{
2113 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2114 NextProtos: []string{"foo"},
2115 },
2116 flags: []string{
2117 "-implicit-handshake",
2118 "-false-start",
2119 "-advertise-alpn", "\x03foo",
2120 },
2121 })
2122
2123 // False Start without session tickets.
2124 tests = append(tests, testCase{
2125 name: "FalseStart-SessionTicketsDisabled",
2126 config: Config{
2127 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2128 NextProtos: []string{"foo"},
2129 SessionTicketsDisabled: true,
2130 Bugs: ProtocolBugs{
2131 ExpectFalseStart: true,
2132 },
2133 },
2134 flags: []string{
2135 "-false-start",
2136 "-select-next-proto", "foo",
2137 },
2138 shimWritesFirst: true,
2139 })
2140
2141 // Server parses a V2ClientHello.
2142 tests = append(tests, testCase{
2143 testType: serverTest,
2144 name: "SendV2ClientHello",
2145 config: Config{
2146 // Choose a cipher suite that does not involve
2147 // elliptic curves, so no extensions are
2148 // involved.
2149 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2150 Bugs: ProtocolBugs{
2151 SendV2ClientHello: true,
2152 },
2153 },
2154 })
2155
2156 // Client sends a Channel ID.
2157 tests = append(tests, testCase{
2158 name: "ChannelID-Client",
2159 config: Config{
2160 RequestChannelID: true,
2161 },
2162 flags: []string{"-send-channel-id", channelIDKeyFile},
2163 resumeSession: true,
2164 expectChannelID: true,
2165 })
2166
2167 // Server accepts a Channel ID.
2168 tests = append(tests, testCase{
2169 testType: serverTest,
2170 name: "ChannelID-Server",
2171 config: Config{
2172 ChannelID: channelIDKey,
2173 },
2174 flags: []string{
2175 "-expect-channel-id",
2176 base64.StdEncoding.EncodeToString(channelIDBytes),
2177 },
2178 resumeSession: true,
2179 expectChannelID: true,
2180 })
2181 } else {
2182 tests = append(tests, testCase{
2183 name: "SkipHelloVerifyRequest",
2184 config: Config{
2185 Bugs: ProtocolBugs{
2186 SkipHelloVerifyRequest: true,
2187 },
2188 },
2189 })
2190 }
2191
David Benjamin43ec06f2014-08-05 02:28:57 -04002192 var suffix string
2193 var flags []string
2194 var maxHandshakeRecordLength int
David Benjamin6fd297b2014-08-11 18:43:38 -04002195 if protocol == dtls {
2196 suffix = "-DTLS"
2197 }
David Benjamin43ec06f2014-08-05 02:28:57 -04002198 if async {
David Benjamin6fd297b2014-08-11 18:43:38 -04002199 suffix += "-Async"
David Benjamin43ec06f2014-08-05 02:28:57 -04002200 flags = append(flags, "-async")
2201 } else {
David Benjamin6fd297b2014-08-11 18:43:38 -04002202 suffix += "-Sync"
David Benjamin43ec06f2014-08-05 02:28:57 -04002203 }
2204 if splitHandshake {
2205 suffix += "-SplitHandshakeRecords"
David Benjamin98214542014-08-07 18:02:39 -04002206 maxHandshakeRecordLength = 1
David Benjamin43ec06f2014-08-05 02:28:57 -04002207 }
David Benjamin760b1dd2015-05-15 23:33:48 -04002208 for _, test := range tests {
2209 test.protocol = protocol
2210 test.name += suffix
2211 test.config.Bugs.MaxHandshakeRecordLength = maxHandshakeRecordLength
2212 test.flags = append(test.flags, flags...)
2213 testCases = append(testCases, test)
David Benjamin6fd297b2014-08-11 18:43:38 -04002214 }
David Benjamin43ec06f2014-08-05 02:28:57 -04002215}
2216
Adam Langley524e7172015-02-20 16:04:00 -08002217func addDDoSCallbackTests() {
2218 // DDoS callback.
2219
2220 for _, resume := range []bool{false, true} {
2221 suffix := "Resume"
2222 if resume {
2223 suffix = "No" + suffix
2224 }
2225
2226 testCases = append(testCases, testCase{
2227 testType: serverTest,
2228 name: "Server-DDoS-OK-" + suffix,
2229 flags: []string{"-install-ddos-callback"},
2230 resumeSession: resume,
2231 })
2232
2233 failFlag := "-fail-ddos-callback"
2234 if resume {
2235 failFlag = "-fail-second-ddos-callback"
2236 }
2237 testCases = append(testCases, testCase{
2238 testType: serverTest,
2239 name: "Server-DDoS-Reject-" + suffix,
2240 flags: []string{"-install-ddos-callback", failFlag},
2241 resumeSession: resume,
2242 shouldFail: true,
2243 expectedError: ":CONNECTION_REJECTED:",
2244 })
2245 }
2246}
2247
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002248func addVersionNegotiationTests() {
2249 for i, shimVers := range tlsVersions {
2250 // Assemble flags to disable all newer versions on the shim.
2251 var flags []string
2252 for _, vers := range tlsVersions[i+1:] {
2253 flags = append(flags, vers.flag)
2254 }
2255
2256 for _, runnerVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05002257 protocols := []protocol{tls}
2258 if runnerVers.hasDTLS && shimVers.hasDTLS {
2259 protocols = append(protocols, dtls)
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002260 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002261 for _, protocol := range protocols {
2262 expectedVersion := shimVers.version
2263 if runnerVers.version < shimVers.version {
2264 expectedVersion = runnerVers.version
2265 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002266
David Benjamin8b8c0062014-11-23 02:47:52 -05002267 suffix := shimVers.name + "-" + runnerVers.name
2268 if protocol == dtls {
2269 suffix += "-DTLS"
2270 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002271
David Benjamin1eb367c2014-12-12 18:17:51 -05002272 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
2273
David Benjamin1e29a6b2014-12-10 02:27:24 -05002274 clientVers := shimVers.version
2275 if clientVers > VersionTLS10 {
2276 clientVers = VersionTLS10
2277 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002278 testCases = append(testCases, testCase{
2279 protocol: protocol,
2280 testType: clientTest,
2281 name: "VersionNegotiation-Client-" + suffix,
2282 config: Config{
2283 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05002284 Bugs: ProtocolBugs{
2285 ExpectInitialRecordVersion: clientVers,
2286 },
David Benjamin8b8c0062014-11-23 02:47:52 -05002287 },
2288 flags: flags,
2289 expectedVersion: expectedVersion,
2290 })
David Benjamin1eb367c2014-12-12 18:17:51 -05002291 testCases = append(testCases, testCase{
2292 protocol: protocol,
2293 testType: clientTest,
2294 name: "VersionNegotiation-Client2-" + suffix,
2295 config: Config{
2296 MaxVersion: runnerVers.version,
2297 Bugs: ProtocolBugs{
2298 ExpectInitialRecordVersion: clientVers,
2299 },
2300 },
2301 flags: []string{"-max-version", shimVersFlag},
2302 expectedVersion: expectedVersion,
2303 })
David Benjamin8b8c0062014-11-23 02:47:52 -05002304
2305 testCases = append(testCases, testCase{
2306 protocol: protocol,
2307 testType: serverTest,
2308 name: "VersionNegotiation-Server-" + suffix,
2309 config: Config{
2310 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05002311 Bugs: ProtocolBugs{
2312 ExpectInitialRecordVersion: expectedVersion,
2313 },
David Benjamin8b8c0062014-11-23 02:47:52 -05002314 },
2315 flags: flags,
2316 expectedVersion: expectedVersion,
2317 })
David Benjamin1eb367c2014-12-12 18:17:51 -05002318 testCases = append(testCases, testCase{
2319 protocol: protocol,
2320 testType: serverTest,
2321 name: "VersionNegotiation-Server2-" + suffix,
2322 config: Config{
2323 MaxVersion: runnerVers.version,
2324 Bugs: ProtocolBugs{
2325 ExpectInitialRecordVersion: expectedVersion,
2326 },
2327 },
2328 flags: []string{"-max-version", shimVersFlag},
2329 expectedVersion: expectedVersion,
2330 })
David Benjamin8b8c0062014-11-23 02:47:52 -05002331 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002332 }
2333 }
2334}
2335
David Benjaminaccb4542014-12-12 23:44:33 -05002336func addMinimumVersionTests() {
2337 for i, shimVers := range tlsVersions {
2338 // Assemble flags to disable all older versions on the shim.
2339 var flags []string
2340 for _, vers := range tlsVersions[:i] {
2341 flags = append(flags, vers.flag)
2342 }
2343
2344 for _, runnerVers := range tlsVersions {
2345 protocols := []protocol{tls}
2346 if runnerVers.hasDTLS && shimVers.hasDTLS {
2347 protocols = append(protocols, dtls)
2348 }
2349 for _, protocol := range protocols {
2350 suffix := shimVers.name + "-" + runnerVers.name
2351 if protocol == dtls {
2352 suffix += "-DTLS"
2353 }
2354 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
2355
David Benjaminaccb4542014-12-12 23:44:33 -05002356 var expectedVersion uint16
2357 var shouldFail bool
2358 var expectedError string
David Benjamin87909c02014-12-13 01:55:01 -05002359 var expectedLocalError string
David Benjaminaccb4542014-12-12 23:44:33 -05002360 if runnerVers.version >= shimVers.version {
2361 expectedVersion = runnerVers.version
2362 } else {
2363 shouldFail = true
2364 expectedError = ":UNSUPPORTED_PROTOCOL:"
David Benjamin87909c02014-12-13 01:55:01 -05002365 if runnerVers.version > VersionSSL30 {
2366 expectedLocalError = "remote error: protocol version not supported"
2367 } else {
2368 expectedLocalError = "remote error: handshake failure"
2369 }
David Benjaminaccb4542014-12-12 23:44:33 -05002370 }
2371
2372 testCases = append(testCases, testCase{
2373 protocol: protocol,
2374 testType: clientTest,
2375 name: "MinimumVersion-Client-" + suffix,
2376 config: Config{
2377 MaxVersion: runnerVers.version,
2378 },
David Benjamin87909c02014-12-13 01:55:01 -05002379 flags: flags,
2380 expectedVersion: expectedVersion,
2381 shouldFail: shouldFail,
2382 expectedError: expectedError,
2383 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002384 })
2385 testCases = append(testCases, testCase{
2386 protocol: protocol,
2387 testType: clientTest,
2388 name: "MinimumVersion-Client2-" + suffix,
2389 config: Config{
2390 MaxVersion: runnerVers.version,
2391 },
David Benjamin87909c02014-12-13 01:55:01 -05002392 flags: []string{"-min-version", shimVersFlag},
2393 expectedVersion: expectedVersion,
2394 shouldFail: shouldFail,
2395 expectedError: expectedError,
2396 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002397 })
2398
2399 testCases = append(testCases, testCase{
2400 protocol: protocol,
2401 testType: serverTest,
2402 name: "MinimumVersion-Server-" + suffix,
2403 config: Config{
2404 MaxVersion: runnerVers.version,
2405 },
David Benjamin87909c02014-12-13 01:55:01 -05002406 flags: flags,
2407 expectedVersion: expectedVersion,
2408 shouldFail: shouldFail,
2409 expectedError: expectedError,
2410 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002411 })
2412 testCases = append(testCases, testCase{
2413 protocol: protocol,
2414 testType: serverTest,
2415 name: "MinimumVersion-Server2-" + suffix,
2416 config: Config{
2417 MaxVersion: runnerVers.version,
2418 },
David Benjamin87909c02014-12-13 01:55:01 -05002419 flags: []string{"-min-version", shimVersFlag},
2420 expectedVersion: expectedVersion,
2421 shouldFail: shouldFail,
2422 expectedError: expectedError,
2423 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002424 })
2425 }
2426 }
2427 }
2428}
2429
David Benjamin5c24a1d2014-08-31 00:59:27 -04002430func addD5BugTests() {
2431 testCases = append(testCases, testCase{
2432 testType: serverTest,
2433 name: "D5Bug-NoQuirk-Reject",
2434 config: Config{
2435 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2436 Bugs: ProtocolBugs{
2437 SSL3RSAKeyExchange: true,
2438 },
2439 },
2440 shouldFail: true,
2441 expectedError: ":TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG:",
2442 })
2443 testCases = append(testCases, testCase{
2444 testType: serverTest,
2445 name: "D5Bug-Quirk-Normal",
2446 config: Config{
2447 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2448 },
2449 flags: []string{"-tls-d5-bug"},
2450 })
2451 testCases = append(testCases, testCase{
2452 testType: serverTest,
2453 name: "D5Bug-Quirk-Bug",
2454 config: Config{
2455 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2456 Bugs: ProtocolBugs{
2457 SSL3RSAKeyExchange: true,
2458 },
2459 },
2460 flags: []string{"-tls-d5-bug"},
2461 })
2462}
2463
David Benjamine78bfde2014-09-06 12:45:15 -04002464func addExtensionTests() {
2465 testCases = append(testCases, testCase{
2466 testType: clientTest,
2467 name: "DuplicateExtensionClient",
2468 config: Config{
2469 Bugs: ProtocolBugs{
2470 DuplicateExtension: true,
2471 },
2472 },
2473 shouldFail: true,
2474 expectedLocalError: "remote error: error decoding message",
2475 })
2476 testCases = append(testCases, testCase{
2477 testType: serverTest,
2478 name: "DuplicateExtensionServer",
2479 config: Config{
2480 Bugs: ProtocolBugs{
2481 DuplicateExtension: true,
2482 },
2483 },
2484 shouldFail: true,
2485 expectedLocalError: "remote error: error decoding message",
2486 })
2487 testCases = append(testCases, testCase{
2488 testType: clientTest,
2489 name: "ServerNameExtensionClient",
2490 config: Config{
2491 Bugs: ProtocolBugs{
2492 ExpectServerName: "example.com",
2493 },
2494 },
2495 flags: []string{"-host-name", "example.com"},
2496 })
2497 testCases = append(testCases, testCase{
2498 testType: clientTest,
David Benjamin5f237bc2015-02-11 17:14:15 -05002499 name: "ServerNameExtensionClientMismatch",
David Benjamine78bfde2014-09-06 12:45:15 -04002500 config: Config{
2501 Bugs: ProtocolBugs{
2502 ExpectServerName: "mismatch.com",
2503 },
2504 },
2505 flags: []string{"-host-name", "example.com"},
2506 shouldFail: true,
2507 expectedLocalError: "tls: unexpected server name",
2508 })
2509 testCases = append(testCases, testCase{
2510 testType: clientTest,
David Benjamin5f237bc2015-02-11 17:14:15 -05002511 name: "ServerNameExtensionClientMissing",
David Benjamine78bfde2014-09-06 12:45:15 -04002512 config: Config{
2513 Bugs: ProtocolBugs{
2514 ExpectServerName: "missing.com",
2515 },
2516 },
2517 shouldFail: true,
2518 expectedLocalError: "tls: unexpected server name",
2519 })
2520 testCases = append(testCases, testCase{
2521 testType: serverTest,
2522 name: "ServerNameExtensionServer",
2523 config: Config{
2524 ServerName: "example.com",
2525 },
2526 flags: []string{"-expect-server-name", "example.com"},
2527 resumeSession: true,
2528 })
David Benjaminae2888f2014-09-06 12:58:58 -04002529 testCases = append(testCases, testCase{
2530 testType: clientTest,
2531 name: "ALPNClient",
2532 config: Config{
2533 NextProtos: []string{"foo"},
2534 },
2535 flags: []string{
2536 "-advertise-alpn", "\x03foo\x03bar\x03baz",
2537 "-expect-alpn", "foo",
2538 },
David Benjaminfc7b0862014-09-06 13:21:53 -04002539 expectedNextProto: "foo",
2540 expectedNextProtoType: alpn,
2541 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04002542 })
2543 testCases = append(testCases, testCase{
2544 testType: serverTest,
2545 name: "ALPNServer",
2546 config: Config{
2547 NextProtos: []string{"foo", "bar", "baz"},
2548 },
2549 flags: []string{
2550 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2551 "-select-alpn", "foo",
2552 },
David Benjaminfc7b0862014-09-06 13:21:53 -04002553 expectedNextProto: "foo",
2554 expectedNextProtoType: alpn,
2555 resumeSession: true,
2556 })
2557 // Test that the server prefers ALPN over NPN.
2558 testCases = append(testCases, testCase{
2559 testType: serverTest,
2560 name: "ALPNServer-Preferred",
2561 config: Config{
2562 NextProtos: []string{"foo", "bar", "baz"},
2563 },
2564 flags: []string{
2565 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2566 "-select-alpn", "foo",
2567 "-advertise-npn", "\x03foo\x03bar\x03baz",
2568 },
2569 expectedNextProto: "foo",
2570 expectedNextProtoType: alpn,
2571 resumeSession: true,
2572 })
2573 testCases = append(testCases, testCase{
2574 testType: serverTest,
2575 name: "ALPNServer-Preferred-Swapped",
2576 config: Config{
2577 NextProtos: []string{"foo", "bar", "baz"},
2578 Bugs: ProtocolBugs{
2579 SwapNPNAndALPN: true,
2580 },
2581 },
2582 flags: []string{
2583 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2584 "-select-alpn", "foo",
2585 "-advertise-npn", "\x03foo\x03bar\x03baz",
2586 },
2587 expectedNextProto: "foo",
2588 expectedNextProtoType: alpn,
2589 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04002590 })
Adam Langley38311732014-10-16 19:04:35 -07002591 // Resume with a corrupt ticket.
2592 testCases = append(testCases, testCase{
2593 testType: serverTest,
2594 name: "CorruptTicket",
2595 config: Config{
2596 Bugs: ProtocolBugs{
2597 CorruptTicket: true,
2598 },
2599 },
2600 resumeSession: true,
2601 flags: []string{"-expect-session-miss"},
2602 })
2603 // Resume with an oversized session id.
2604 testCases = append(testCases, testCase{
2605 testType: serverTest,
2606 name: "OversizedSessionId",
2607 config: Config{
2608 Bugs: ProtocolBugs{
2609 OversizedSessionId: true,
2610 },
2611 },
2612 resumeSession: true,
Adam Langley75712922014-10-10 16:23:43 -07002613 shouldFail: true,
Adam Langley38311732014-10-16 19:04:35 -07002614 expectedError: ":DECODE_ERROR:",
2615 })
David Benjaminca6c8262014-11-15 19:06:08 -05002616 // Basic DTLS-SRTP tests. Include fake profiles to ensure they
2617 // are ignored.
2618 testCases = append(testCases, testCase{
2619 protocol: dtls,
2620 name: "SRTP-Client",
2621 config: Config{
2622 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
2623 },
2624 flags: []string{
2625 "-srtp-profiles",
2626 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2627 },
2628 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2629 })
2630 testCases = append(testCases, testCase{
2631 protocol: dtls,
2632 testType: serverTest,
2633 name: "SRTP-Server",
2634 config: Config{
2635 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
2636 },
2637 flags: []string{
2638 "-srtp-profiles",
2639 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2640 },
2641 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2642 })
2643 // Test that the MKI is ignored.
2644 testCases = append(testCases, testCase{
2645 protocol: dtls,
2646 testType: serverTest,
2647 name: "SRTP-Server-IgnoreMKI",
2648 config: Config{
2649 SRTPProtectionProfiles: []uint16{SRTP_AES128_CM_HMAC_SHA1_80},
2650 Bugs: ProtocolBugs{
2651 SRTPMasterKeyIdentifer: "bogus",
2652 },
2653 },
2654 flags: []string{
2655 "-srtp-profiles",
2656 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2657 },
2658 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2659 })
2660 // Test that SRTP isn't negotiated on the server if there were
2661 // no matching profiles.
2662 testCases = append(testCases, testCase{
2663 protocol: dtls,
2664 testType: serverTest,
2665 name: "SRTP-Server-NoMatch",
2666 config: Config{
2667 SRTPProtectionProfiles: []uint16{100, 101, 102},
2668 },
2669 flags: []string{
2670 "-srtp-profiles",
2671 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2672 },
2673 expectedSRTPProtectionProfile: 0,
2674 })
2675 // Test that the server returning an invalid SRTP profile is
2676 // flagged as an error by the client.
2677 testCases = append(testCases, testCase{
2678 protocol: dtls,
2679 name: "SRTP-Client-NoMatch",
2680 config: Config{
2681 Bugs: ProtocolBugs{
2682 SendSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_32,
2683 },
2684 },
2685 flags: []string{
2686 "-srtp-profiles",
2687 "SRTP_AES128_CM_SHA1_80",
2688 },
2689 shouldFail: true,
2690 expectedError: ":BAD_SRTP_PROTECTION_PROFILE_LIST:",
2691 })
David Benjamin61f95272014-11-25 01:55:35 -05002692 // Test OCSP stapling and SCT list.
2693 testCases = append(testCases, testCase{
2694 name: "OCSPStapling",
2695 flags: []string{
2696 "-enable-ocsp-stapling",
2697 "-expect-ocsp-response",
2698 base64.StdEncoding.EncodeToString(testOCSPResponse),
2699 },
2700 })
2701 testCases = append(testCases, testCase{
2702 name: "SignedCertificateTimestampList",
2703 flags: []string{
2704 "-enable-signed-cert-timestamps",
2705 "-expect-signed-cert-timestamps",
2706 base64.StdEncoding.EncodeToString(testSCTList),
2707 },
2708 })
David Benjamine78bfde2014-09-06 12:45:15 -04002709}
2710
David Benjamin01fe8202014-09-24 15:21:44 -04002711func addResumptionVersionTests() {
David Benjamin01fe8202014-09-24 15:21:44 -04002712 for _, sessionVers := range tlsVersions {
David Benjamin01fe8202014-09-24 15:21:44 -04002713 for _, resumeVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05002714 protocols := []protocol{tls}
2715 if sessionVers.hasDTLS && resumeVers.hasDTLS {
2716 protocols = append(protocols, dtls)
David Benjaminbdf5e722014-11-11 00:52:15 -05002717 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002718 for _, protocol := range protocols {
2719 suffix := "-" + sessionVers.name + "-" + resumeVers.name
2720 if protocol == dtls {
2721 suffix += "-DTLS"
2722 }
2723
David Benjaminece3de92015-03-16 18:02:20 -04002724 if sessionVers.version == resumeVers.version {
2725 testCases = append(testCases, testCase{
2726 protocol: protocol,
2727 name: "Resume-Client" + suffix,
2728 resumeSession: true,
2729 config: Config{
2730 MaxVersion: sessionVers.version,
2731 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
David Benjamin8b8c0062014-11-23 02:47:52 -05002732 },
David Benjaminece3de92015-03-16 18:02:20 -04002733 expectedVersion: sessionVers.version,
2734 expectedResumeVersion: resumeVers.version,
2735 })
2736 } else {
2737 testCases = append(testCases, testCase{
2738 protocol: protocol,
2739 name: "Resume-Client-Mismatch" + suffix,
2740 resumeSession: true,
2741 config: Config{
2742 MaxVersion: sessionVers.version,
2743 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
David Benjamin8b8c0062014-11-23 02:47:52 -05002744 },
David Benjaminece3de92015-03-16 18:02:20 -04002745 expectedVersion: sessionVers.version,
2746 resumeConfig: &Config{
2747 MaxVersion: resumeVers.version,
2748 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2749 Bugs: ProtocolBugs{
2750 AllowSessionVersionMismatch: true,
2751 },
2752 },
2753 expectedResumeVersion: resumeVers.version,
2754 shouldFail: true,
2755 expectedError: ":OLD_SESSION_VERSION_NOT_RETURNED:",
2756 })
2757 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002758
2759 testCases = append(testCases, testCase{
2760 protocol: protocol,
2761 name: "Resume-Client-NoResume" + suffix,
2762 flags: []string{"-expect-session-miss"},
2763 resumeSession: true,
2764 config: Config{
2765 MaxVersion: sessionVers.version,
2766 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2767 },
2768 expectedVersion: sessionVers.version,
2769 resumeConfig: &Config{
2770 MaxVersion: resumeVers.version,
2771 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2772 },
2773 newSessionsOnResume: true,
2774 expectedResumeVersion: resumeVers.version,
2775 })
2776
2777 var flags []string
2778 if sessionVers.version != resumeVers.version {
2779 flags = append(flags, "-expect-session-miss")
2780 }
2781 testCases = append(testCases, testCase{
2782 protocol: protocol,
2783 testType: serverTest,
2784 name: "Resume-Server" + suffix,
2785 flags: flags,
2786 resumeSession: true,
2787 config: Config{
2788 MaxVersion: sessionVers.version,
2789 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2790 },
2791 expectedVersion: sessionVers.version,
2792 resumeConfig: &Config{
2793 MaxVersion: resumeVers.version,
2794 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2795 },
2796 expectedResumeVersion: resumeVers.version,
2797 })
2798 }
David Benjamin01fe8202014-09-24 15:21:44 -04002799 }
2800 }
David Benjaminece3de92015-03-16 18:02:20 -04002801
2802 testCases = append(testCases, testCase{
2803 name: "Resume-Client-CipherMismatch",
2804 resumeSession: true,
2805 config: Config{
2806 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2807 },
2808 resumeConfig: &Config{
2809 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2810 Bugs: ProtocolBugs{
2811 SendCipherSuite: TLS_RSA_WITH_AES_128_CBC_SHA,
2812 },
2813 },
2814 shouldFail: true,
2815 expectedError: ":OLD_SESSION_CIPHER_NOT_RETURNED:",
2816 })
David Benjamin01fe8202014-09-24 15:21:44 -04002817}
2818
Adam Langley2ae77d22014-10-28 17:29:33 -07002819func addRenegotiationTests() {
2820 testCases = append(testCases, testCase{
Adam Langley2ae77d22014-10-28 17:29:33 -07002821 testType: serverTest,
David Benjamin4b27d9f2015-05-12 22:42:52 -04002822 name: "Renegotiate-Server",
David Benjamincdea40c2015-03-19 14:09:43 -04002823 config: Config{
2824 Bugs: ProtocolBugs{
David Benjamin4b27d9f2015-05-12 22:42:52 -04002825 FailIfResumeOnRenego: true,
David Benjamincdea40c2015-03-19 14:09:43 -04002826 },
2827 },
2828 flags: []string{"-renegotiate"},
2829 shimWritesFirst: true,
2830 })
2831 testCases = append(testCases, testCase{
2832 testType: serverTest,
Adam Langley2ae77d22014-10-28 17:29:33 -07002833 name: "Renegotiate-Server-EmptyExt",
2834 config: Config{
2835 Bugs: ProtocolBugs{
2836 EmptyRenegotiationInfo: true,
2837 },
2838 },
2839 flags: []string{"-renegotiate"},
2840 shimWritesFirst: true,
2841 shouldFail: true,
2842 expectedError: ":RENEGOTIATION_MISMATCH:",
2843 })
2844 testCases = append(testCases, testCase{
2845 testType: serverTest,
2846 name: "Renegotiate-Server-BadExt",
2847 config: Config{
2848 Bugs: ProtocolBugs{
2849 BadRenegotiationInfo: true,
2850 },
2851 },
2852 flags: []string{"-renegotiate"},
2853 shimWritesFirst: true,
2854 shouldFail: true,
2855 expectedError: ":RENEGOTIATION_MISMATCH:",
2856 })
David Benjaminca6554b2014-11-08 12:31:52 -05002857 testCases = append(testCases, testCase{
2858 testType: serverTest,
2859 name: "Renegotiate-Server-ClientInitiated",
2860 renegotiate: true,
2861 })
2862 testCases = append(testCases, testCase{
2863 testType: serverTest,
2864 name: "Renegotiate-Server-ClientInitiated-NoExt",
2865 renegotiate: true,
2866 config: Config{
2867 Bugs: ProtocolBugs{
2868 NoRenegotiationInfo: true,
2869 },
2870 },
2871 shouldFail: true,
2872 expectedError: ":UNSAFE_LEGACY_RENEGOTIATION_DISABLED:",
2873 })
2874 testCases = append(testCases, testCase{
2875 testType: serverTest,
2876 name: "Renegotiate-Server-ClientInitiated-NoExt-Allowed",
2877 renegotiate: true,
2878 config: Config{
2879 Bugs: ProtocolBugs{
2880 NoRenegotiationInfo: true,
2881 },
2882 },
2883 flags: []string{"-allow-unsafe-legacy-renegotiation"},
2884 })
David Benjaminb16346b2015-04-08 19:16:58 -04002885 testCases = append(testCases, testCase{
2886 testType: serverTest,
2887 name: "Renegotiate-Server-ClientInitiated-Forbidden",
2888 renegotiate: true,
2889 flags: []string{"-reject-peer-renegotiations"},
2890 shouldFail: true,
2891 expectedError: ":NO_RENEGOTIATION:",
2892 expectedLocalError: "remote error: no renegotiation",
2893 })
David Benjamin3c9746a2015-03-19 15:00:10 -04002894 // Regression test for CVE-2015-0291.
2895 testCases = append(testCases, testCase{
2896 testType: serverTest,
2897 name: "Renegotiate-Server-NoSignatureAlgorithms",
2898 config: Config{
2899 Bugs: ProtocolBugs{
David Benjamin3c9746a2015-03-19 15:00:10 -04002900 NoSignatureAlgorithmsOnRenego: true,
2901 },
2902 },
2903 flags: []string{"-renegotiate"},
2904 shimWritesFirst: true,
2905 })
Adam Langley2ae77d22014-10-28 17:29:33 -07002906 // TODO(agl): test the renegotiation info SCSV.
Adam Langleycf2d4f42014-10-28 19:06:14 -07002907 testCases = append(testCases, testCase{
David Benjamin4b27d9f2015-05-12 22:42:52 -04002908 name: "Renegotiate-Client",
David Benjamincdea40c2015-03-19 14:09:43 -04002909 config: Config{
2910 Bugs: ProtocolBugs{
David Benjamin4b27d9f2015-05-12 22:42:52 -04002911 FailIfResumeOnRenego: true,
David Benjamincdea40c2015-03-19 14:09:43 -04002912 },
2913 },
2914 renegotiate: true,
2915 })
2916 testCases = append(testCases, testCase{
Adam Langleycf2d4f42014-10-28 19:06:14 -07002917 name: "Renegotiate-Client-EmptyExt",
2918 renegotiate: true,
2919 config: Config{
2920 Bugs: ProtocolBugs{
2921 EmptyRenegotiationInfo: true,
2922 },
2923 },
2924 shouldFail: true,
2925 expectedError: ":RENEGOTIATION_MISMATCH:",
2926 })
2927 testCases = append(testCases, testCase{
2928 name: "Renegotiate-Client-BadExt",
2929 renegotiate: true,
2930 config: Config{
2931 Bugs: ProtocolBugs{
2932 BadRenegotiationInfo: true,
2933 },
2934 },
2935 shouldFail: true,
2936 expectedError: ":RENEGOTIATION_MISMATCH:",
2937 })
2938 testCases = append(testCases, testCase{
David Benjamincff0b902015-05-15 23:09:47 -04002939 name: "Renegotiate-Client-NoExt",
2940 renegotiate: true,
2941 config: Config{
2942 Bugs: ProtocolBugs{
2943 NoRenegotiationInfo: true,
2944 },
2945 },
2946 shouldFail: true,
2947 expectedError: ":UNSAFE_LEGACY_RENEGOTIATION_DISABLED:",
2948 flags: []string{"-no-legacy-server-connect"},
2949 })
2950 testCases = append(testCases, testCase{
2951 name: "Renegotiate-Client-NoExt-Allowed",
2952 renegotiate: true,
2953 config: Config{
2954 Bugs: ProtocolBugs{
2955 NoRenegotiationInfo: true,
2956 },
2957 },
2958 })
2959 testCases = append(testCases, testCase{
Adam Langleycf2d4f42014-10-28 19:06:14 -07002960 name: "Renegotiate-Client-SwitchCiphers",
2961 renegotiate: true,
2962 config: Config{
2963 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2964 },
2965 renegotiateCiphers: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2966 })
2967 testCases = append(testCases, testCase{
2968 name: "Renegotiate-Client-SwitchCiphers2",
2969 renegotiate: true,
2970 config: Config{
2971 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2972 },
2973 renegotiateCiphers: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2974 })
David Benjaminc44b1df2014-11-23 12:11:01 -05002975 testCases = append(testCases, testCase{
David Benjaminb16346b2015-04-08 19:16:58 -04002976 name: "Renegotiate-Client-Forbidden",
2977 renegotiate: true,
2978 flags: []string{"-reject-peer-renegotiations"},
2979 shouldFail: true,
2980 expectedError: ":NO_RENEGOTIATION:",
2981 expectedLocalError: "remote error: no renegotiation",
2982 })
2983 testCases = append(testCases, testCase{
David Benjaminc44b1df2014-11-23 12:11:01 -05002984 name: "Renegotiate-SameClientVersion",
2985 renegotiate: true,
2986 config: Config{
2987 MaxVersion: VersionTLS10,
2988 Bugs: ProtocolBugs{
2989 RequireSameRenegoClientVersion: true,
2990 },
2991 },
2992 })
Adam Langley2ae77d22014-10-28 17:29:33 -07002993}
2994
David Benjamin5e961c12014-11-07 01:48:35 -05002995func addDTLSReplayTests() {
2996 // Test that sequence number replays are detected.
2997 testCases = append(testCases, testCase{
2998 protocol: dtls,
2999 name: "DTLS-Replay",
3000 replayWrites: true,
3001 })
3002
3003 // Test the outgoing sequence number skipping by values larger
3004 // than the retransmit window.
3005 testCases = append(testCases, testCase{
3006 protocol: dtls,
3007 name: "DTLS-Replay-LargeGaps",
3008 config: Config{
3009 Bugs: ProtocolBugs{
3010 SequenceNumberIncrement: 127,
3011 },
3012 },
3013 replayWrites: true,
3014 })
3015}
3016
Feng Lu41aa3252014-11-21 22:47:56 -08003017func addFastRadioPaddingTests() {
David Benjamin1e29a6b2014-12-10 02:27:24 -05003018 testCases = append(testCases, testCase{
3019 protocol: tls,
3020 name: "FastRadio-Padding",
Feng Lu41aa3252014-11-21 22:47:56 -08003021 config: Config{
3022 Bugs: ProtocolBugs{
3023 RequireFastradioPadding: true,
3024 },
3025 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05003026 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08003027 })
David Benjamin1e29a6b2014-12-10 02:27:24 -05003028 testCases = append(testCases, testCase{
3029 protocol: dtls,
David Benjamin5f237bc2015-02-11 17:14:15 -05003030 name: "FastRadio-Padding-DTLS",
Feng Lu41aa3252014-11-21 22:47:56 -08003031 config: Config{
3032 Bugs: ProtocolBugs{
3033 RequireFastradioPadding: true,
3034 },
3035 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05003036 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08003037 })
3038}
3039
David Benjamin000800a2014-11-14 01:43:59 -05003040var testHashes = []struct {
3041 name string
3042 id uint8
3043}{
3044 {"SHA1", hashSHA1},
3045 {"SHA224", hashSHA224},
3046 {"SHA256", hashSHA256},
3047 {"SHA384", hashSHA384},
3048 {"SHA512", hashSHA512},
3049}
3050
3051func addSigningHashTests() {
3052 // Make sure each hash works. Include some fake hashes in the list and
3053 // ensure they're ignored.
3054 for _, hash := range testHashes {
3055 testCases = append(testCases, testCase{
3056 name: "SigningHash-ClientAuth-" + hash.name,
3057 config: Config{
3058 ClientAuth: RequireAnyClientCert,
3059 SignatureAndHashes: []signatureAndHash{
3060 {signatureRSA, 42},
3061 {signatureRSA, hash.id},
3062 {signatureRSA, 255},
3063 },
3064 },
3065 flags: []string{
3066 "-cert-file", rsaCertificateFile,
3067 "-key-file", rsaKeyFile,
3068 },
3069 })
3070
3071 testCases = append(testCases, testCase{
3072 testType: serverTest,
3073 name: "SigningHash-ServerKeyExchange-Sign-" + hash.name,
3074 config: Config{
3075 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
3076 SignatureAndHashes: []signatureAndHash{
3077 {signatureRSA, 42},
3078 {signatureRSA, hash.id},
3079 {signatureRSA, 255},
3080 },
3081 },
3082 })
3083 }
3084
3085 // Test that hash resolution takes the signature type into account.
3086 testCases = append(testCases, testCase{
3087 name: "SigningHash-ClientAuth-SignatureType",
3088 config: Config{
3089 ClientAuth: RequireAnyClientCert,
3090 SignatureAndHashes: []signatureAndHash{
3091 {signatureECDSA, hashSHA512},
3092 {signatureRSA, hashSHA384},
3093 {signatureECDSA, hashSHA1},
3094 },
3095 },
3096 flags: []string{
3097 "-cert-file", rsaCertificateFile,
3098 "-key-file", rsaKeyFile,
3099 },
3100 })
3101
3102 testCases = append(testCases, testCase{
3103 testType: serverTest,
3104 name: "SigningHash-ServerKeyExchange-SignatureType",
3105 config: Config{
3106 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
3107 SignatureAndHashes: []signatureAndHash{
3108 {signatureECDSA, hashSHA512},
3109 {signatureRSA, hashSHA384},
3110 {signatureECDSA, hashSHA1},
3111 },
3112 },
3113 })
3114
3115 // Test that, if the list is missing, the peer falls back to SHA-1.
3116 testCases = append(testCases, testCase{
3117 name: "SigningHash-ClientAuth-Fallback",
3118 config: Config{
3119 ClientAuth: RequireAnyClientCert,
3120 SignatureAndHashes: []signatureAndHash{
3121 {signatureRSA, hashSHA1},
3122 },
3123 Bugs: ProtocolBugs{
3124 NoSignatureAndHashes: true,
3125 },
3126 },
3127 flags: []string{
3128 "-cert-file", rsaCertificateFile,
3129 "-key-file", rsaKeyFile,
3130 },
3131 })
3132
3133 testCases = append(testCases, testCase{
3134 testType: serverTest,
3135 name: "SigningHash-ServerKeyExchange-Fallback",
3136 config: Config{
3137 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
3138 SignatureAndHashes: []signatureAndHash{
3139 {signatureRSA, hashSHA1},
3140 },
3141 Bugs: ProtocolBugs{
3142 NoSignatureAndHashes: true,
3143 },
3144 },
3145 })
David Benjamin72dc7832015-03-16 17:49:43 -04003146
3147 // Test that hash preferences are enforced. BoringSSL defaults to
3148 // rejecting MD5 signatures.
3149 testCases = append(testCases, testCase{
3150 testType: serverTest,
3151 name: "SigningHash-ClientAuth-Enforced",
3152 config: Config{
3153 Certificates: []Certificate{rsaCertificate},
3154 SignatureAndHashes: []signatureAndHash{
3155 {signatureRSA, hashMD5},
3156 // Advertise SHA-1 so the handshake will
3157 // proceed, but the shim's preferences will be
3158 // ignored in CertificateVerify generation, so
3159 // MD5 will be chosen.
3160 {signatureRSA, hashSHA1},
3161 },
3162 Bugs: ProtocolBugs{
3163 IgnorePeerSignatureAlgorithmPreferences: true,
3164 },
3165 },
3166 flags: []string{"-require-any-client-certificate"},
3167 shouldFail: true,
3168 expectedError: ":WRONG_SIGNATURE_TYPE:",
3169 })
3170
3171 testCases = append(testCases, testCase{
3172 name: "SigningHash-ServerKeyExchange-Enforced",
3173 config: Config{
3174 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
3175 SignatureAndHashes: []signatureAndHash{
3176 {signatureRSA, hashMD5},
3177 },
3178 Bugs: ProtocolBugs{
3179 IgnorePeerSignatureAlgorithmPreferences: true,
3180 },
3181 },
3182 shouldFail: true,
3183 expectedError: ":WRONG_SIGNATURE_TYPE:",
3184 })
David Benjamin000800a2014-11-14 01:43:59 -05003185}
3186
David Benjamin83f90402015-01-27 01:09:43 -05003187// timeouts is the retransmit schedule for BoringSSL. It doubles and
3188// caps at 60 seconds. On the 13th timeout, it gives up.
3189var timeouts = []time.Duration{
3190 1 * time.Second,
3191 2 * time.Second,
3192 4 * time.Second,
3193 8 * time.Second,
3194 16 * time.Second,
3195 32 * time.Second,
3196 60 * time.Second,
3197 60 * time.Second,
3198 60 * time.Second,
3199 60 * time.Second,
3200 60 * time.Second,
3201 60 * time.Second,
3202 60 * time.Second,
3203}
3204
3205func addDTLSRetransmitTests() {
3206 // Test that this is indeed the timeout schedule. Stress all
3207 // four patterns of handshake.
3208 for i := 1; i < len(timeouts); i++ {
3209 number := strconv.Itoa(i)
3210 testCases = append(testCases, testCase{
3211 protocol: dtls,
3212 name: "DTLS-Retransmit-Client-" + number,
3213 config: Config{
3214 Bugs: ProtocolBugs{
3215 TimeoutSchedule: timeouts[:i],
3216 },
3217 },
3218 resumeSession: true,
3219 flags: []string{"-async"},
3220 })
3221 testCases = append(testCases, testCase{
3222 protocol: dtls,
3223 testType: serverTest,
3224 name: "DTLS-Retransmit-Server-" + number,
3225 config: Config{
3226 Bugs: ProtocolBugs{
3227 TimeoutSchedule: timeouts[:i],
3228 },
3229 },
3230 resumeSession: true,
3231 flags: []string{"-async"},
3232 })
3233 }
3234
3235 // Test that exceeding the timeout schedule hits a read
3236 // timeout.
3237 testCases = append(testCases, testCase{
3238 protocol: dtls,
3239 name: "DTLS-Retransmit-Timeout",
3240 config: Config{
3241 Bugs: ProtocolBugs{
3242 TimeoutSchedule: timeouts,
3243 },
3244 },
3245 resumeSession: true,
3246 flags: []string{"-async"},
3247 shouldFail: true,
3248 expectedError: ":READ_TIMEOUT_EXPIRED:",
3249 })
3250
3251 // Test that timeout handling has a fudge factor, due to API
3252 // problems.
3253 testCases = append(testCases, testCase{
3254 protocol: dtls,
3255 name: "DTLS-Retransmit-Fudge",
3256 config: Config{
3257 Bugs: ProtocolBugs{
3258 TimeoutSchedule: []time.Duration{
3259 timeouts[0] - 10*time.Millisecond,
3260 },
3261 },
3262 },
3263 resumeSession: true,
3264 flags: []string{"-async"},
3265 })
David Benjamin7eaab4c2015-03-02 19:01:16 -05003266
3267 // Test that the final Finished retransmitting isn't
3268 // duplicated if the peer badly fragments everything.
3269 testCases = append(testCases, testCase{
3270 testType: serverTest,
3271 protocol: dtls,
3272 name: "DTLS-Retransmit-Fragmented",
3273 config: Config{
3274 Bugs: ProtocolBugs{
3275 TimeoutSchedule: []time.Duration{timeouts[0]},
3276 MaxHandshakeRecordLength: 2,
3277 },
3278 },
3279 flags: []string{"-async"},
3280 })
David Benjamin83f90402015-01-27 01:09:43 -05003281}
3282
David Benjaminc565ebb2015-04-03 04:06:36 -04003283func addExportKeyingMaterialTests() {
3284 for _, vers := range tlsVersions {
3285 if vers.version == VersionSSL30 {
3286 continue
3287 }
3288 testCases = append(testCases, testCase{
3289 name: "ExportKeyingMaterial-" + vers.name,
3290 config: Config{
3291 MaxVersion: vers.version,
3292 },
3293 exportKeyingMaterial: 1024,
3294 exportLabel: "label",
3295 exportContext: "context",
3296 useExportContext: true,
3297 })
3298 testCases = append(testCases, testCase{
3299 name: "ExportKeyingMaterial-NoContext-" + vers.name,
3300 config: Config{
3301 MaxVersion: vers.version,
3302 },
3303 exportKeyingMaterial: 1024,
3304 })
3305 testCases = append(testCases, testCase{
3306 name: "ExportKeyingMaterial-EmptyContext-" + vers.name,
3307 config: Config{
3308 MaxVersion: vers.version,
3309 },
3310 exportKeyingMaterial: 1024,
3311 useExportContext: true,
3312 })
3313 testCases = append(testCases, testCase{
3314 name: "ExportKeyingMaterial-Small-" + vers.name,
3315 config: Config{
3316 MaxVersion: vers.version,
3317 },
3318 exportKeyingMaterial: 1,
3319 exportLabel: "label",
3320 exportContext: "context",
3321 useExportContext: true,
3322 })
3323 }
3324 testCases = append(testCases, testCase{
3325 name: "ExportKeyingMaterial-SSL3",
3326 config: Config{
3327 MaxVersion: VersionSSL30,
3328 },
3329 exportKeyingMaterial: 1024,
3330 exportLabel: "label",
3331 exportContext: "context",
3332 useExportContext: true,
3333 shouldFail: true,
3334 expectedError: "failed to export keying material",
3335 })
3336}
3337
David Benjamin884fdf12014-08-02 15:28:23 -04003338func worker(statusChan chan statusMsg, c chan *testCase, buildDir string, wg *sync.WaitGroup) {
Adam Langley95c29f32014-06-20 12:00:00 -07003339 defer wg.Done()
3340
3341 for test := range c {
Adam Langley69a01602014-11-17 17:26:55 -08003342 var err error
3343
3344 if *mallocTest < 0 {
3345 statusChan <- statusMsg{test: test, started: true}
3346 err = runTest(test, buildDir, -1)
3347 } else {
3348 for mallocNumToFail := int64(*mallocTest); ; mallocNumToFail++ {
3349 statusChan <- statusMsg{test: test, started: true}
3350 if err = runTest(test, buildDir, mallocNumToFail); err != errMoreMallocs {
3351 if err != nil {
3352 fmt.Printf("\n\nmalloc test failed at %d: %s\n", mallocNumToFail, err)
3353 }
3354 break
3355 }
3356 }
3357 }
Adam Langley95c29f32014-06-20 12:00:00 -07003358 statusChan <- statusMsg{test: test, err: err}
3359 }
3360}
3361
3362type statusMsg struct {
3363 test *testCase
3364 started bool
3365 err error
3366}
3367
David Benjamin5f237bc2015-02-11 17:14:15 -05003368func statusPrinter(doneChan chan *testOutput, statusChan chan statusMsg, total int) {
Adam Langley95c29f32014-06-20 12:00:00 -07003369 var started, done, failed, lineLen int
Adam Langley95c29f32014-06-20 12:00:00 -07003370
David Benjamin5f237bc2015-02-11 17:14:15 -05003371 testOutput := newTestOutput()
Adam Langley95c29f32014-06-20 12:00:00 -07003372 for msg := range statusChan {
David Benjamin5f237bc2015-02-11 17:14:15 -05003373 if !*pipe {
3374 // Erase the previous status line.
David Benjamin87c8a642015-02-21 01:54:29 -05003375 var erase string
3376 for i := 0; i < lineLen; i++ {
3377 erase += "\b \b"
3378 }
3379 fmt.Print(erase)
David Benjamin5f237bc2015-02-11 17:14:15 -05003380 }
3381
Adam Langley95c29f32014-06-20 12:00:00 -07003382 if msg.started {
3383 started++
3384 } else {
3385 done++
David Benjamin5f237bc2015-02-11 17:14:15 -05003386
3387 if msg.err != nil {
3388 fmt.Printf("FAILED (%s)\n%s\n", msg.test.name, msg.err)
3389 failed++
3390 testOutput.addResult(msg.test.name, "FAIL")
3391 } else {
3392 if *pipe {
3393 // Print each test instead of a status line.
3394 fmt.Printf("PASSED (%s)\n", msg.test.name)
3395 }
3396 testOutput.addResult(msg.test.name, "PASS")
3397 }
Adam Langley95c29f32014-06-20 12:00:00 -07003398 }
3399
David Benjamin5f237bc2015-02-11 17:14:15 -05003400 if !*pipe {
3401 // Print a new status line.
3402 line := fmt.Sprintf("%d/%d/%d/%d", failed, done, started, total)
3403 lineLen = len(line)
3404 os.Stdout.WriteString(line)
Adam Langley95c29f32014-06-20 12:00:00 -07003405 }
Adam Langley95c29f32014-06-20 12:00:00 -07003406 }
David Benjamin5f237bc2015-02-11 17:14:15 -05003407
3408 doneChan <- testOutput
Adam Langley95c29f32014-06-20 12:00:00 -07003409}
3410
3411func main() {
3412 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 -04003413 var flagNumWorkers *int = flag.Int("num-workers", runtime.NumCPU(), "The number of workers to run in parallel.")
David Benjamin884fdf12014-08-02 15:28:23 -04003414 var flagBuildDir *string = flag.String("build-dir", "../../../build", "The build directory to run the shim from.")
Adam Langley95c29f32014-06-20 12:00:00 -07003415
3416 flag.Parse()
3417
3418 addCipherSuiteTests()
3419 addBadECDSASignatureTests()
Adam Langley80842bd2014-06-20 12:00:00 -07003420 addCBCPaddingTests()
Kenny Root7fdeaf12014-08-05 15:23:37 -07003421 addCBCSplittingTests()
David Benjamin636293b2014-07-08 17:59:18 -04003422 addClientAuthTests()
Adam Langley524e7172015-02-20 16:04:00 -08003423 addDDoSCallbackTests()
David Benjamin7e2e6cf2014-08-07 17:44:24 -04003424 addVersionNegotiationTests()
David Benjaminaccb4542014-12-12 23:44:33 -05003425 addMinimumVersionTests()
David Benjamin5c24a1d2014-08-31 00:59:27 -04003426 addD5BugTests()
David Benjamine78bfde2014-09-06 12:45:15 -04003427 addExtensionTests()
David Benjamin01fe8202014-09-24 15:21:44 -04003428 addResumptionVersionTests()
Adam Langley75712922014-10-10 16:23:43 -07003429 addExtendedMasterSecretTests()
Adam Langley2ae77d22014-10-28 17:29:33 -07003430 addRenegotiationTests()
David Benjamin5e961c12014-11-07 01:48:35 -05003431 addDTLSReplayTests()
David Benjamin000800a2014-11-14 01:43:59 -05003432 addSigningHashTests()
Feng Lu41aa3252014-11-21 22:47:56 -08003433 addFastRadioPaddingTests()
David Benjamin83f90402015-01-27 01:09:43 -05003434 addDTLSRetransmitTests()
David Benjaminc565ebb2015-04-03 04:06:36 -04003435 addExportKeyingMaterialTests()
David Benjamin43ec06f2014-08-05 02:28:57 -04003436 for _, async := range []bool{false, true} {
3437 for _, splitHandshake := range []bool{false, true} {
David Benjamin6fd297b2014-08-11 18:43:38 -04003438 for _, protocol := range []protocol{tls, dtls} {
3439 addStateMachineCoverageTests(async, splitHandshake, protocol)
3440 }
David Benjamin43ec06f2014-08-05 02:28:57 -04003441 }
3442 }
Adam Langley95c29f32014-06-20 12:00:00 -07003443
3444 var wg sync.WaitGroup
3445
David Benjamin2bc8e6f2014-08-02 15:22:37 -04003446 numWorkers := *flagNumWorkers
Adam Langley95c29f32014-06-20 12:00:00 -07003447
3448 statusChan := make(chan statusMsg, numWorkers)
3449 testChan := make(chan *testCase, numWorkers)
David Benjamin5f237bc2015-02-11 17:14:15 -05003450 doneChan := make(chan *testOutput)
Adam Langley95c29f32014-06-20 12:00:00 -07003451
David Benjamin025b3d32014-07-01 19:53:04 -04003452 go statusPrinter(doneChan, statusChan, len(testCases))
Adam Langley95c29f32014-06-20 12:00:00 -07003453
3454 for i := 0; i < numWorkers; i++ {
3455 wg.Add(1)
David Benjamin884fdf12014-08-02 15:28:23 -04003456 go worker(statusChan, testChan, *flagBuildDir, &wg)
Adam Langley95c29f32014-06-20 12:00:00 -07003457 }
3458
David Benjamin025b3d32014-07-01 19:53:04 -04003459 for i := range testCases {
3460 if len(*flagTest) == 0 || *flagTest == testCases[i].name {
3461 testChan <- &testCases[i]
Adam Langley95c29f32014-06-20 12:00:00 -07003462 }
3463 }
3464
3465 close(testChan)
3466 wg.Wait()
3467 close(statusChan)
David Benjamin5f237bc2015-02-11 17:14:15 -05003468 testOutput := <-doneChan
Adam Langley95c29f32014-06-20 12:00:00 -07003469
3470 fmt.Printf("\n")
David Benjamin5f237bc2015-02-11 17:14:15 -05003471
3472 if *jsonOutput != "" {
3473 if err := testOutput.writeTo(*jsonOutput); err != nil {
3474 fmt.Fprintf(os.Stderr, "Error: %s\n", err)
3475 }
3476 }
David Benjamin2ab7a862015-04-04 17:02:18 -04003477
3478 if !testOutput.allPassed {
3479 os.Exit(1)
3480 }
Adam Langley95c29f32014-06-20 12:00:00 -07003481}