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