blob: d17b048a3c42db2c423ad88ca5da6a65d4783714 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001package main
2
3import (
4 "bytes"
David Benjamina08e49d2014-08-24 01:46:07 -04005 "crypto/ecdsa"
6 "crypto/elliptic"
David Benjamin407a10c2014-07-16 12:58:59 -04007 "crypto/x509"
David Benjamin2561dc32014-08-24 01:25:27 -04008 "encoding/base64"
David Benjamina08e49d2014-08-24 01:46:07 -04009 "encoding/pem"
Adam Langley95c29f32014-06-20 12:00:00 -070010 "flag"
11 "fmt"
12 "io"
Kenny Root7fdeaf12014-08-05 15:23:37 -070013 "io/ioutil"
Adam Langleya7997f12015-05-14 17:38:50 -070014 "math/big"
Adam Langley95c29f32014-06-20 12:00:00 -070015 "net"
16 "os"
17 "os/exec"
David Benjamin884fdf12014-08-02 15:28:23 -040018 "path"
David Benjamin2bc8e6f2014-08-02 15:22:37 -040019 "runtime"
Adam Langley69a01602014-11-17 17:26:55 -080020 "strconv"
Adam Langley95c29f32014-06-20 12:00:00 -070021 "strings"
22 "sync"
23 "syscall"
David Benjamin83f90402015-01-27 01:09:43 -050024 "time"
Adam Langley95c29f32014-06-20 12:00:00 -070025)
26
Adam Langley69a01602014-11-17 17:26:55 -080027var (
David Benjamin5f237bc2015-02-11 17:14:15 -050028 useValgrind = flag.Bool("valgrind", false, "If true, run code under valgrind")
29 useGDB = flag.Bool("gdb", false, "If true, run BoringSSL code under gdb")
30 flagDebug = flag.Bool("debug", false, "Hexdump the contents of the connection")
31 mallocTest = flag.Int64("malloc-test", -1, "If non-negative, run each test with each malloc in turn failing from the given number onwards.")
32 mallocTestDebug = flag.Bool("malloc-test-debug", false, "If true, ask bssl_shim to abort rather than fail a malloc. This can be used with a specific value for --malloc-test to identity the malloc failing that is causing problems.")
33 jsonOutput = flag.String("json-output", "", "The file to output JSON results to.")
34 pipe = flag.Bool("pipe", false, "If true, print status output suitable for piping into another program.")
Adam Langley69a01602014-11-17 17:26:55 -080035)
Adam Langley95c29f32014-06-20 12:00:00 -070036
David Benjamin025b3d32014-07-01 19:53:04 -040037const (
38 rsaCertificateFile = "cert.pem"
39 ecdsaCertificateFile = "ecdsa_cert.pem"
40)
41
42const (
David Benjamina08e49d2014-08-24 01:46:07 -040043 rsaKeyFile = "key.pem"
44 ecdsaKeyFile = "ecdsa_key.pem"
45 channelIDKeyFile = "channel_id_key.pem"
David Benjamin025b3d32014-07-01 19:53:04 -040046)
47
Adam Langley95c29f32014-06-20 12:00:00 -070048var rsaCertificate, ecdsaCertificate Certificate
David Benjamina08e49d2014-08-24 01:46:07 -040049var channelIDKey *ecdsa.PrivateKey
50var channelIDBytes []byte
Adam Langley95c29f32014-06-20 12:00:00 -070051
David Benjamin61f95272014-11-25 01:55:35 -050052var testOCSPResponse = []byte{1, 2, 3, 4}
53var testSCTList = []byte{5, 6, 7, 8}
54
Adam Langley95c29f32014-06-20 12:00:00 -070055func initCertificates() {
56 var err error
David Benjamin025b3d32014-07-01 19:53:04 -040057 rsaCertificate, err = LoadX509KeyPair(rsaCertificateFile, rsaKeyFile)
Adam Langley95c29f32014-06-20 12:00:00 -070058 if err != nil {
59 panic(err)
60 }
David Benjamin61f95272014-11-25 01:55:35 -050061 rsaCertificate.OCSPStaple = testOCSPResponse
62 rsaCertificate.SignedCertificateTimestampList = testSCTList
Adam Langley95c29f32014-06-20 12:00:00 -070063
David Benjamin025b3d32014-07-01 19:53:04 -040064 ecdsaCertificate, err = LoadX509KeyPair(ecdsaCertificateFile, ecdsaKeyFile)
Adam Langley95c29f32014-06-20 12:00:00 -070065 if err != nil {
66 panic(err)
67 }
David Benjamin61f95272014-11-25 01:55:35 -050068 ecdsaCertificate.OCSPStaple = testOCSPResponse
69 ecdsaCertificate.SignedCertificateTimestampList = testSCTList
David Benjamina08e49d2014-08-24 01:46:07 -040070
71 channelIDPEMBlock, err := ioutil.ReadFile(channelIDKeyFile)
72 if err != nil {
73 panic(err)
74 }
75 channelIDDERBlock, _ := pem.Decode(channelIDPEMBlock)
76 if channelIDDERBlock.Type != "EC PRIVATE KEY" {
77 panic("bad key type")
78 }
79 channelIDKey, err = x509.ParseECPrivateKey(channelIDDERBlock.Bytes)
80 if err != nil {
81 panic(err)
82 }
83 if channelIDKey.Curve != elliptic.P256() {
84 panic("bad curve")
85 }
86
87 channelIDBytes = make([]byte, 64)
88 writeIntPadded(channelIDBytes[:32], channelIDKey.X)
89 writeIntPadded(channelIDBytes[32:], channelIDKey.Y)
Adam Langley95c29f32014-06-20 12:00:00 -070090}
91
92var certificateOnce sync.Once
93
94func getRSACertificate() Certificate {
95 certificateOnce.Do(initCertificates)
96 return rsaCertificate
97}
98
99func getECDSACertificate() Certificate {
100 certificateOnce.Do(initCertificates)
101 return ecdsaCertificate
102}
103
David Benjamin025b3d32014-07-01 19:53:04 -0400104type testType int
105
106const (
107 clientTest testType = iota
108 serverTest
109)
110
David Benjamin6fd297b2014-08-11 18:43:38 -0400111type protocol int
112
113const (
114 tls protocol = iota
115 dtls
116)
117
David Benjaminfc7b0862014-09-06 13:21:53 -0400118const (
119 alpn = 1
120 npn = 2
121)
122
Adam Langley95c29f32014-06-20 12:00:00 -0700123type testCase struct {
David Benjamin025b3d32014-07-01 19:53:04 -0400124 testType testType
David Benjamin6fd297b2014-08-11 18:43:38 -0400125 protocol protocol
Adam Langley95c29f32014-06-20 12:00:00 -0700126 name string
127 config Config
128 shouldFail bool
129 expectedError string
Adam Langleyac61fa32014-06-23 12:03:11 -0700130 // expectedLocalError, if not empty, contains a substring that must be
131 // found in the local error.
132 expectedLocalError string
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400133 // expectedVersion, if non-zero, specifies the TLS version that must be
134 // negotiated.
135 expectedVersion uint16
David Benjamin01fe8202014-09-24 15:21:44 -0400136 // expectedResumeVersion, if non-zero, specifies the TLS version that
137 // must be negotiated on resumption. If zero, expectedVersion is used.
138 expectedResumeVersion uint16
David Benjamin90da8c82015-04-20 14:57:57 -0400139 // expectedCipher, if non-zero, specifies the TLS cipher suite that
140 // should be negotiated.
141 expectedCipher uint16
David Benjamina08e49d2014-08-24 01:46:07 -0400142 // expectChannelID controls whether the connection should have
143 // negotiated a Channel ID with channelIDKey.
144 expectChannelID bool
David Benjaminae2888f2014-09-06 12:58:58 -0400145 // expectedNextProto controls whether the connection should
146 // negotiate a next protocol via NPN or ALPN.
147 expectedNextProto string
David Benjaminfc7b0862014-09-06 13:21:53 -0400148 // expectedNextProtoType, if non-zero, is the expected next
149 // protocol negotiation mechanism.
150 expectedNextProtoType int
David Benjaminca6c8262014-11-15 19:06:08 -0500151 // expectedSRTPProtectionProfile is the DTLS-SRTP profile that
152 // should be negotiated. If zero, none should be negotiated.
153 expectedSRTPProtectionProfile uint16
Adam Langley80842bd2014-06-20 12:00:00 -0700154 // messageLen is the length, in bytes, of the test message that will be
155 // sent.
156 messageLen int
David Benjamin025b3d32014-07-01 19:53:04 -0400157 // certFile is the path to the certificate to use for the server.
158 certFile string
159 // keyFile is the path to the private key to use for the server.
160 keyFile string
David Benjamin1d5c83e2014-07-22 19:20:02 -0400161 // resumeSession controls whether a second connection should be tested
David Benjamin01fe8202014-09-24 15:21:44 -0400162 // which attempts to resume the first session.
David Benjamin1d5c83e2014-07-22 19:20:02 -0400163 resumeSession bool
Adam Langleyb0eef0a2015-06-02 10:47:39 -0700164 // expectResumeRejected, if true, specifies that the attempted
165 // resumption must be rejected by the client. This is only valid for a
166 // serverTest.
167 expectResumeRejected bool
David Benjamin01fe8202014-09-24 15:21:44 -0400168 // resumeConfig, if not nil, points to a Config to be used on
David Benjaminfe8eb9a2014-11-17 03:19:02 -0500169 // resumption. Unless newSessionsOnResume is set,
170 // SessionTicketKey, ServerSessionCache, and
171 // ClientSessionCache are copied from the initial connection's
172 // config. If nil, the initial connection's config is used.
David Benjamin01fe8202014-09-24 15:21:44 -0400173 resumeConfig *Config
David Benjaminfe8eb9a2014-11-17 03:19:02 -0500174 // newSessionsOnResume, if true, will cause resumeConfig to
175 // use a different session resumption context.
176 newSessionsOnResume bool
David Benjamin98e882e2014-08-08 13:24:34 -0400177 // sendPrefix sends a prefix on the socket before actually performing a
178 // handshake.
179 sendPrefix string
David Benjamine58c4f52014-08-24 03:47:07 -0400180 // shimWritesFirst controls whether the shim sends an initial "hello"
181 // message before doing a roundtrip with the runner.
182 shimWritesFirst bool
Adam Langleycf2d4f42014-10-28 19:06:14 -0700183 // renegotiate indicates the the connection should be renegotiated
184 // during the exchange.
185 renegotiate bool
186 // renegotiateCiphers is a list of ciphersuite ids that will be
187 // switched in just before renegotiation.
188 renegotiateCiphers []uint16
David Benjamin5e961c12014-11-07 01:48:35 -0500189 // replayWrites, if true, configures the underlying transport
190 // to replay every write it makes in DTLS tests.
191 replayWrites bool
David Benjamin5fa3eba2015-01-22 16:35:40 -0500192 // damageFirstWrite, if true, configures the underlying transport to
193 // damage the final byte of the first application data write.
194 damageFirstWrite bool
David Benjaminc565ebb2015-04-03 04:06:36 -0400195 // exportKeyingMaterial, if non-zero, configures the test to exchange
196 // keying material and verify they match.
197 exportKeyingMaterial int
198 exportLabel string
199 exportContext string
200 useExportContext bool
David Benjamin325b5c32014-07-01 19:40:31 -0400201 // flags, if not empty, contains a list of command-line flags that will
202 // be passed to the shim program.
203 flags []string
Adam Langleyaf0e32c2015-06-03 09:57:23 -0700204 // testTLSUnique, if true, causes the shim to send the tls-unique value
205 // which will be compared against the expected value.
206 testTLSUnique bool
David Benjamina8ebe222015-06-06 03:04:39 -0400207 // sendEmptyRecords is the number of consecutive empty records to send
208 // before and after the test message.
209 sendEmptyRecords int
David Benjamin24f346d2015-06-06 03:28:08 -0400210 // sendWarningAlerts is the number of consecutive warning alerts to send
211 // before and after the test message.
212 sendWarningAlerts int
Adam Langley95c29f32014-06-20 12:00:00 -0700213}
214
David Benjamin025b3d32014-07-01 19:53:04 -0400215var testCases = []testCase{
Adam Langley95c29f32014-06-20 12:00:00 -0700216 {
217 name: "BadRSASignature",
218 config: Config{
219 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
220 Bugs: ProtocolBugs{
221 InvalidSKXSignature: true,
222 },
223 },
224 shouldFail: true,
David Benjamin25f08462015-04-15 16:13:49 -0400225 expectedError: ":BAD_SIGNATURE:",
Adam Langley95c29f32014-06-20 12:00:00 -0700226 },
227 {
228 name: "BadECDSASignature",
229 config: Config{
230 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
231 Bugs: ProtocolBugs{
232 InvalidSKXSignature: true,
233 },
234 Certificates: []Certificate{getECDSACertificate()},
235 },
236 shouldFail: true,
237 expectedError: ":BAD_SIGNATURE:",
238 },
239 {
240 name: "BadECDSACurve",
241 config: Config{
242 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
243 Bugs: ProtocolBugs{
244 InvalidSKXCurve: true,
245 },
246 Certificates: []Certificate{getECDSACertificate()},
247 },
248 shouldFail: true,
249 expectedError: ":WRONG_CURVE:",
250 },
Adam Langleyac61fa32014-06-23 12:03:11 -0700251 {
David Benjamina8e3e0e2014-08-06 22:11:10 -0400252 testType: serverTest,
253 name: "BadRSAVersion",
254 config: Config{
255 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
256 Bugs: ProtocolBugs{
257 RsaClientKeyExchangeVersion: VersionTLS11,
258 },
259 },
260 shouldFail: true,
261 expectedError: ":DECRYPTION_FAILED_OR_BAD_RECORD_MAC:",
262 },
263 {
David Benjamin325b5c32014-07-01 19:40:31 -0400264 name: "NoFallbackSCSV",
Adam Langleyac61fa32014-06-23 12:03:11 -0700265 config: Config{
266 Bugs: ProtocolBugs{
267 FailIfNotFallbackSCSV: true,
268 },
269 },
270 shouldFail: true,
271 expectedLocalError: "no fallback SCSV found",
272 },
David Benjamin325b5c32014-07-01 19:40:31 -0400273 {
David Benjamin2a0c4962014-08-22 23:46:35 -0400274 name: "SendFallbackSCSV",
David Benjamin325b5c32014-07-01 19:40:31 -0400275 config: Config{
276 Bugs: ProtocolBugs{
277 FailIfNotFallbackSCSV: true,
278 },
279 },
280 flags: []string{"-fallback-scsv"},
281 },
David Benjamin197b3ab2014-07-02 18:37:33 -0400282 {
David Benjamin7b030512014-07-08 17:30:11 -0400283 name: "ClientCertificateTypes",
284 config: Config{
285 ClientAuth: RequestClientCert,
286 ClientCertificateTypes: []byte{
287 CertTypeDSSSign,
288 CertTypeRSASign,
289 CertTypeECDSASign,
290 },
291 },
David Benjamin2561dc32014-08-24 01:25:27 -0400292 flags: []string{
293 "-expect-certificate-types",
294 base64.StdEncoding.EncodeToString([]byte{
295 CertTypeDSSSign,
296 CertTypeRSASign,
297 CertTypeECDSASign,
298 }),
299 },
David Benjamin7b030512014-07-08 17:30:11 -0400300 },
David Benjamin636293b2014-07-08 17:59:18 -0400301 {
302 name: "NoClientCertificate",
303 config: Config{
304 ClientAuth: RequireAnyClientCert,
305 },
306 shouldFail: true,
307 expectedLocalError: "client didn't provide a certificate",
308 },
David Benjamin1c375dd2014-07-12 00:48:23 -0400309 {
310 name: "UnauthenticatedECDH",
311 config: Config{
312 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
313 Bugs: ProtocolBugs{
314 UnauthenticatedECDH: true,
315 },
316 },
317 shouldFail: true,
David Benjamine8f3d662014-07-12 01:10:19 -0400318 expectedError: ":UNEXPECTED_MESSAGE:",
David Benjamin1c375dd2014-07-12 00:48:23 -0400319 },
David Benjamin9c651c92014-07-12 13:27:45 -0400320 {
David Benjamindcd979f2015-04-20 18:26:52 -0400321 name: "SkipCertificateStatus",
322 config: Config{
323 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
324 Bugs: ProtocolBugs{
325 SkipCertificateStatus: true,
326 },
327 },
328 flags: []string{
329 "-enable-ocsp-stapling",
330 },
331 },
332 {
David Benjamin9c651c92014-07-12 13:27:45 -0400333 name: "SkipServerKeyExchange",
334 config: Config{
335 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
336 Bugs: ProtocolBugs{
337 SkipServerKeyExchange: true,
338 },
339 },
340 shouldFail: true,
341 expectedError: ":UNEXPECTED_MESSAGE:",
342 },
David Benjamin1f5f62b2014-07-12 16:18:02 -0400343 {
David Benjamina0e52232014-07-19 17:39:58 -0400344 name: "SkipChangeCipherSpec-Client",
345 config: Config{
346 Bugs: ProtocolBugs{
347 SkipChangeCipherSpec: true,
348 },
349 },
350 shouldFail: true,
David Benjamin86271ee2014-07-21 16:14:03 -0400351 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
David Benjamina0e52232014-07-19 17:39:58 -0400352 },
353 {
354 testType: serverTest,
355 name: "SkipChangeCipherSpec-Server",
356 config: Config{
357 Bugs: ProtocolBugs{
358 SkipChangeCipherSpec: true,
359 },
360 },
361 shouldFail: true,
David Benjamin86271ee2014-07-21 16:14:03 -0400362 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
David Benjamina0e52232014-07-19 17:39:58 -0400363 },
David Benjamin42be6452014-07-21 14:50:23 -0400364 {
365 testType: serverTest,
366 name: "SkipChangeCipherSpec-Server-NPN",
367 config: Config{
368 NextProtos: []string{"bar"},
369 Bugs: ProtocolBugs{
370 SkipChangeCipherSpec: true,
371 },
372 },
373 flags: []string{
374 "-advertise-npn", "\x03foo\x03bar\x03baz",
375 },
376 shouldFail: true,
David Benjamin86271ee2014-07-21 16:14:03 -0400377 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
378 },
379 {
380 name: "FragmentAcrossChangeCipherSpec-Client",
381 config: Config{
382 Bugs: ProtocolBugs{
383 FragmentAcrossChangeCipherSpec: true,
384 },
385 },
386 shouldFail: true,
387 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
388 },
389 {
390 testType: serverTest,
391 name: "FragmentAcrossChangeCipherSpec-Server",
392 config: Config{
393 Bugs: ProtocolBugs{
394 FragmentAcrossChangeCipherSpec: true,
395 },
396 },
397 shouldFail: true,
398 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
399 },
400 {
401 testType: serverTest,
402 name: "FragmentAcrossChangeCipherSpec-Server-NPN",
403 config: Config{
404 NextProtos: []string{"bar"},
405 Bugs: ProtocolBugs{
406 FragmentAcrossChangeCipherSpec: true,
407 },
408 },
409 flags: []string{
410 "-advertise-npn", "\x03foo\x03bar\x03baz",
411 },
412 shouldFail: true,
413 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
David Benjamin42be6452014-07-21 14:50:23 -0400414 },
David Benjaminf3ec83d2014-07-21 22:42:34 -0400415 {
416 testType: serverTest,
David Benjamin3fd1fbd2015-02-03 16:07:32 -0500417 name: "Alert",
418 config: Config{
419 Bugs: ProtocolBugs{
420 SendSpuriousAlert: alertRecordOverflow,
421 },
422 },
423 shouldFail: true,
424 expectedError: ":TLSV1_ALERT_RECORD_OVERFLOW:",
425 },
426 {
427 protocol: dtls,
428 testType: serverTest,
429 name: "Alert-DTLS",
430 config: Config{
431 Bugs: ProtocolBugs{
432 SendSpuriousAlert: alertRecordOverflow,
433 },
434 },
435 shouldFail: true,
436 expectedError: ":TLSV1_ALERT_RECORD_OVERFLOW:",
437 },
438 {
439 testType: serverTest,
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400440 name: "FragmentAlert",
441 config: Config{
442 Bugs: ProtocolBugs{
David Benjaminca6c8262014-11-15 19:06:08 -0500443 FragmentAlert: true,
David Benjamin3fd1fbd2015-02-03 16:07:32 -0500444 SendSpuriousAlert: alertRecordOverflow,
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400445 },
446 },
447 shouldFail: true,
448 expectedError: ":BAD_ALERT:",
449 },
450 {
David Benjamin0ea8dda2015-01-31 20:33:40 -0500451 protocol: dtls,
452 testType: serverTest,
453 name: "FragmentAlert-DTLS",
454 config: Config{
455 Bugs: ProtocolBugs{
456 FragmentAlert: true,
David Benjamin3fd1fbd2015-02-03 16:07:32 -0500457 SendSpuriousAlert: alertRecordOverflow,
David Benjamin0ea8dda2015-01-31 20:33:40 -0500458 },
459 },
460 shouldFail: true,
461 expectedError: ":BAD_ALERT:",
462 },
463 {
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400464 testType: serverTest,
David Benjaminf3ec83d2014-07-21 22:42:34 -0400465 name: "EarlyChangeCipherSpec-server-1",
466 config: Config{
467 Bugs: ProtocolBugs{
468 EarlyChangeCipherSpec: 1,
469 },
470 },
471 shouldFail: true,
472 expectedError: ":CCS_RECEIVED_EARLY:",
473 },
474 {
475 testType: serverTest,
476 name: "EarlyChangeCipherSpec-server-2",
477 config: Config{
478 Bugs: ProtocolBugs{
479 EarlyChangeCipherSpec: 2,
480 },
481 },
482 shouldFail: true,
483 expectedError: ":CCS_RECEIVED_EARLY:",
484 },
David Benjamind23f4122014-07-23 15:09:48 -0400485 {
David Benjamind23f4122014-07-23 15:09:48 -0400486 name: "SkipNewSessionTicket",
487 config: Config{
488 Bugs: ProtocolBugs{
489 SkipNewSessionTicket: true,
490 },
491 },
492 shouldFail: true,
493 expectedError: ":CCS_RECEIVED_EARLY:",
494 },
David Benjamin7e3305e2014-07-28 14:52:32 -0400495 {
David Benjamind86c7672014-08-02 04:07:12 -0400496 testType: serverTest,
David Benjaminbef270a2014-08-02 04:22:02 -0400497 name: "FallbackSCSV",
498 config: Config{
499 MaxVersion: VersionTLS11,
500 Bugs: ProtocolBugs{
501 SendFallbackSCSV: true,
502 },
503 },
504 shouldFail: true,
505 expectedError: ":INAPPROPRIATE_FALLBACK:",
506 },
507 {
508 testType: serverTest,
509 name: "FallbackSCSV-VersionMatch",
510 config: Config{
511 Bugs: ProtocolBugs{
512 SendFallbackSCSV: true,
513 },
514 },
515 },
David Benjamin98214542014-08-07 18:02:39 -0400516 {
517 testType: serverTest,
518 name: "FragmentedClientVersion",
519 config: Config{
520 Bugs: ProtocolBugs{
521 MaxHandshakeRecordLength: 1,
522 FragmentClientVersion: true,
523 },
524 },
David Benjamin82c9e902014-12-12 15:55:27 -0500525 expectedVersion: VersionTLS12,
David Benjamin98214542014-08-07 18:02:39 -0400526 },
David Benjamin98e882e2014-08-08 13:24:34 -0400527 {
528 testType: serverTest,
529 name: "MinorVersionTolerance",
530 config: Config{
531 Bugs: ProtocolBugs{
532 SendClientVersion: 0x03ff,
533 },
534 },
535 expectedVersion: VersionTLS12,
536 },
537 {
538 testType: serverTest,
539 name: "MajorVersionTolerance",
540 config: Config{
541 Bugs: ProtocolBugs{
542 SendClientVersion: 0x0400,
543 },
544 },
545 expectedVersion: VersionTLS12,
546 },
547 {
548 testType: serverTest,
549 name: "VersionTooLow",
550 config: Config{
551 Bugs: ProtocolBugs{
552 SendClientVersion: 0x0200,
553 },
554 },
555 shouldFail: true,
556 expectedError: ":UNSUPPORTED_PROTOCOL:",
557 },
558 {
559 testType: serverTest,
560 name: "HttpGET",
561 sendPrefix: "GET / HTTP/1.0\n",
562 shouldFail: true,
563 expectedError: ":HTTP_REQUEST:",
564 },
565 {
566 testType: serverTest,
567 name: "HttpPOST",
568 sendPrefix: "POST / HTTP/1.0\n",
569 shouldFail: true,
570 expectedError: ":HTTP_REQUEST:",
571 },
572 {
573 testType: serverTest,
574 name: "HttpHEAD",
575 sendPrefix: "HEAD / HTTP/1.0\n",
576 shouldFail: true,
577 expectedError: ":HTTP_REQUEST:",
578 },
579 {
580 testType: serverTest,
581 name: "HttpPUT",
582 sendPrefix: "PUT / HTTP/1.0\n",
583 shouldFail: true,
584 expectedError: ":HTTP_REQUEST:",
585 },
586 {
587 testType: serverTest,
588 name: "HttpCONNECT",
589 sendPrefix: "CONNECT www.google.com:443 HTTP/1.0\n",
590 shouldFail: true,
591 expectedError: ":HTTPS_PROXY_REQUEST:",
592 },
David Benjamin39ebf532014-08-31 02:23:49 -0400593 {
David Benjaminf080ecd2014-12-11 18:48:59 -0500594 testType: serverTest,
595 name: "Garbage",
596 sendPrefix: "blah",
597 shouldFail: true,
598 expectedError: ":UNKNOWN_PROTOCOL:",
599 },
600 {
David Benjamin39ebf532014-08-31 02:23:49 -0400601 name: "SkipCipherVersionCheck",
602 config: Config{
603 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
604 MaxVersion: VersionTLS11,
605 Bugs: ProtocolBugs{
606 SkipCipherVersionCheck: true,
607 },
608 },
609 shouldFail: true,
610 expectedError: ":WRONG_CIPHER_RETURNED:",
611 },
David Benjamin9114fae2014-11-08 11:41:14 -0500612 {
David Benjamina3e89492015-02-26 15:16:22 -0500613 name: "RSAEphemeralKey",
David Benjamin9114fae2014-11-08 11:41:14 -0500614 config: Config{
615 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
616 Bugs: ProtocolBugs{
David Benjamina3e89492015-02-26 15:16:22 -0500617 RSAEphemeralKey: true,
David Benjamin9114fae2014-11-08 11:41:14 -0500618 },
619 },
620 shouldFail: true,
621 expectedError: ":UNEXPECTED_MESSAGE:",
622 },
David Benjamin128dbc32014-12-01 01:27:42 -0500623 {
624 name: "DisableEverything",
625 flags: []string{"-no-tls12", "-no-tls11", "-no-tls1", "-no-ssl3"},
626 shouldFail: true,
627 expectedError: ":WRONG_SSL_VERSION:",
628 },
629 {
630 protocol: dtls,
631 name: "DisableEverything-DTLS",
632 flags: []string{"-no-tls12", "-no-tls1"},
633 shouldFail: true,
634 expectedError: ":WRONG_SSL_VERSION:",
635 },
David Benjamin780d6dd2015-01-06 12:03:19 -0500636 {
637 name: "NoSharedCipher",
638 config: Config{
639 CipherSuites: []uint16{},
640 },
641 shouldFail: true,
642 expectedError: ":HANDSHAKE_FAILURE_ON_CLIENT_HELLO:",
643 },
David Benjamin13be1de2015-01-11 16:29:36 -0500644 {
645 protocol: dtls,
646 testType: serverTest,
647 name: "MTU",
648 config: Config{
649 Bugs: ProtocolBugs{
650 MaxPacketLength: 256,
651 },
652 },
653 flags: []string{"-mtu", "256"},
654 },
655 {
656 protocol: dtls,
657 testType: serverTest,
658 name: "MTUExceeded",
659 config: Config{
660 Bugs: ProtocolBugs{
661 MaxPacketLength: 255,
662 },
663 },
664 flags: []string{"-mtu", "256"},
665 shouldFail: true,
666 expectedLocalError: "dtls: exceeded maximum packet length",
667 },
David Benjamin6095de82014-12-27 01:50:38 -0500668 {
669 name: "CertMismatchRSA",
670 config: Config{
671 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
672 Certificates: []Certificate{getECDSACertificate()},
673 Bugs: ProtocolBugs{
674 SendCipherSuite: TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
675 },
676 },
677 shouldFail: true,
678 expectedError: ":WRONG_CERTIFICATE_TYPE:",
679 },
680 {
681 name: "CertMismatchECDSA",
682 config: Config{
683 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
684 Certificates: []Certificate{getRSACertificate()},
685 Bugs: ProtocolBugs{
686 SendCipherSuite: TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
687 },
688 },
689 shouldFail: true,
690 expectedError: ":WRONG_CERTIFICATE_TYPE:",
691 },
David Benjamin5fa3eba2015-01-22 16:35:40 -0500692 {
693 name: "TLSFatalBadPackets",
694 damageFirstWrite: true,
695 shouldFail: true,
696 expectedError: ":DECRYPTION_FAILED_OR_BAD_RECORD_MAC:",
697 },
698 {
699 protocol: dtls,
700 name: "DTLSIgnoreBadPackets",
701 damageFirstWrite: true,
702 },
703 {
704 protocol: dtls,
705 name: "DTLSIgnoreBadPackets-Async",
706 damageFirstWrite: true,
707 flags: []string{"-async"},
708 },
David Benjamin4189bd92015-01-25 23:52:39 -0500709 {
710 name: "AppDataAfterChangeCipherSpec",
711 config: Config{
712 Bugs: ProtocolBugs{
713 AppDataAfterChangeCipherSpec: []byte("TEST MESSAGE"),
714 },
715 },
716 shouldFail: true,
717 expectedError: ":DATA_BETWEEN_CCS_AND_FINISHED:",
718 },
719 {
720 protocol: dtls,
721 name: "AppDataAfterChangeCipherSpec-DTLS",
722 config: Config{
723 Bugs: ProtocolBugs{
724 AppDataAfterChangeCipherSpec: []byte("TEST MESSAGE"),
725 },
726 },
David Benjamin4417d052015-04-05 04:17:25 -0400727 // BoringSSL's DTLS implementation will drop the out-of-order
728 // application data.
David Benjamin4189bd92015-01-25 23:52:39 -0500729 },
David Benjaminb3774b92015-01-31 17:16:01 -0500730 {
David Benjamindc3da932015-03-12 15:09:02 -0400731 name: "AlertAfterChangeCipherSpec",
732 config: Config{
733 Bugs: ProtocolBugs{
734 AlertAfterChangeCipherSpec: alertRecordOverflow,
735 },
736 },
737 shouldFail: true,
738 expectedError: ":TLSV1_ALERT_RECORD_OVERFLOW:",
739 },
740 {
741 protocol: dtls,
742 name: "AlertAfterChangeCipherSpec-DTLS",
743 config: Config{
744 Bugs: ProtocolBugs{
745 AlertAfterChangeCipherSpec: alertRecordOverflow,
746 },
747 },
748 shouldFail: true,
749 expectedError: ":TLSV1_ALERT_RECORD_OVERFLOW:",
750 },
751 {
David Benjaminb3774b92015-01-31 17:16:01 -0500752 protocol: dtls,
753 name: "ReorderHandshakeFragments-Small-DTLS",
754 config: Config{
755 Bugs: ProtocolBugs{
756 ReorderHandshakeFragments: true,
757 // Small enough that every handshake message is
758 // fragmented.
759 MaxHandshakeRecordLength: 2,
760 },
761 },
762 },
763 {
764 protocol: dtls,
765 name: "ReorderHandshakeFragments-Large-DTLS",
766 config: Config{
767 Bugs: ProtocolBugs{
768 ReorderHandshakeFragments: true,
769 // Large enough that no handshake message is
770 // fragmented.
David Benjaminb3774b92015-01-31 17:16:01 -0500771 MaxHandshakeRecordLength: 2048,
772 },
773 },
774 },
David Benjaminddb9f152015-02-03 15:44:39 -0500775 {
David Benjamin75381222015-03-02 19:30:30 -0500776 protocol: dtls,
777 name: "MixCompleteMessageWithFragments-DTLS",
778 config: Config{
779 Bugs: ProtocolBugs{
780 ReorderHandshakeFragments: true,
781 MixCompleteMessageWithFragments: true,
782 MaxHandshakeRecordLength: 2,
783 },
784 },
785 },
786 {
David Benjaminddb9f152015-02-03 15:44:39 -0500787 name: "SendInvalidRecordType",
788 config: Config{
789 Bugs: ProtocolBugs{
790 SendInvalidRecordType: true,
791 },
792 },
793 shouldFail: true,
794 expectedError: ":UNEXPECTED_RECORD:",
795 },
796 {
797 protocol: dtls,
798 name: "SendInvalidRecordType-DTLS",
799 config: Config{
800 Bugs: ProtocolBugs{
801 SendInvalidRecordType: true,
802 },
803 },
804 shouldFail: true,
805 expectedError: ":UNEXPECTED_RECORD:",
806 },
David Benjaminb80168e2015-02-08 18:30:14 -0500807 {
808 name: "FalseStart-SkipServerSecondLeg",
809 config: Config{
810 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
811 NextProtos: []string{"foo"},
812 Bugs: ProtocolBugs{
813 SkipNewSessionTicket: true,
814 SkipChangeCipherSpec: true,
815 SkipFinished: true,
816 ExpectFalseStart: true,
817 },
818 },
819 flags: []string{
820 "-false-start",
David Benjamin87e4acd2015-04-02 19:57:35 -0400821 "-handshake-never-done",
David Benjaminb80168e2015-02-08 18:30:14 -0500822 "-advertise-alpn", "\x03foo",
823 },
824 shimWritesFirst: true,
825 shouldFail: true,
826 expectedError: ":UNEXPECTED_RECORD:",
827 },
David Benjamin931ab342015-02-08 19:46:57 -0500828 {
829 name: "FalseStart-SkipServerSecondLeg-Implicit",
830 config: Config{
831 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
832 NextProtos: []string{"foo"},
833 Bugs: ProtocolBugs{
834 SkipNewSessionTicket: true,
835 SkipChangeCipherSpec: true,
836 SkipFinished: true,
837 },
838 },
839 flags: []string{
840 "-implicit-handshake",
841 "-false-start",
David Benjamin87e4acd2015-04-02 19:57:35 -0400842 "-handshake-never-done",
David Benjamin931ab342015-02-08 19:46:57 -0500843 "-advertise-alpn", "\x03foo",
844 },
845 shouldFail: true,
846 expectedError: ":UNEXPECTED_RECORD:",
847 },
David Benjamin6f5c0f42015-02-24 01:23:21 -0500848 {
849 testType: serverTest,
850 name: "FailEarlyCallback",
851 flags: []string{"-fail-early-callback"},
852 shouldFail: true,
853 expectedError: ":CONNECTION_REJECTED:",
854 expectedLocalError: "remote error: access denied",
855 },
David Benjaminbcb2d912015-02-24 23:45:43 -0500856 {
857 name: "WrongMessageType",
858 config: Config{
859 Bugs: ProtocolBugs{
860 WrongCertificateMessageType: true,
861 },
862 },
863 shouldFail: true,
864 expectedError: ":UNEXPECTED_MESSAGE:",
865 expectedLocalError: "remote error: unexpected message",
866 },
867 {
868 protocol: dtls,
869 name: "WrongMessageType-DTLS",
870 config: Config{
871 Bugs: ProtocolBugs{
872 WrongCertificateMessageType: true,
873 },
874 },
875 shouldFail: true,
876 expectedError: ":UNEXPECTED_MESSAGE:",
877 expectedLocalError: "remote error: unexpected message",
878 },
David Benjamin75381222015-03-02 19:30:30 -0500879 {
880 protocol: dtls,
881 name: "FragmentMessageTypeMismatch-DTLS",
882 config: Config{
883 Bugs: ProtocolBugs{
884 MaxHandshakeRecordLength: 2,
885 FragmentMessageTypeMismatch: true,
886 },
887 },
888 shouldFail: true,
889 expectedError: ":FRAGMENT_MISMATCH:",
890 },
891 {
892 protocol: dtls,
893 name: "FragmentMessageLengthMismatch-DTLS",
894 config: Config{
895 Bugs: ProtocolBugs{
896 MaxHandshakeRecordLength: 2,
897 FragmentMessageLengthMismatch: true,
898 },
899 },
900 shouldFail: true,
901 expectedError: ":FRAGMENT_MISMATCH:",
902 },
903 {
904 protocol: dtls,
905 name: "SplitFragmentHeader-DTLS",
906 config: Config{
907 Bugs: ProtocolBugs{
908 SplitFragmentHeader: true,
909 },
910 },
911 shouldFail: true,
912 expectedError: ":UNEXPECTED_MESSAGE:",
913 },
914 {
915 protocol: dtls,
916 name: "SplitFragmentBody-DTLS",
917 config: Config{
918 Bugs: ProtocolBugs{
919 SplitFragmentBody: true,
920 },
921 },
922 shouldFail: true,
923 expectedError: ":UNEXPECTED_MESSAGE:",
924 },
925 {
926 protocol: dtls,
927 name: "SendEmptyFragments-DTLS",
928 config: Config{
929 Bugs: ProtocolBugs{
930 SendEmptyFragments: true,
931 },
932 },
933 },
David Benjamin67d1fb52015-03-16 15:16:23 -0400934 {
935 name: "UnsupportedCipherSuite",
936 config: Config{
937 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
938 Bugs: ProtocolBugs{
939 IgnorePeerCipherPreferences: true,
940 },
941 },
942 flags: []string{"-cipher", "DEFAULT:!RC4"},
943 shouldFail: true,
944 expectedError: ":WRONG_CIPHER_RETURNED:",
945 },
David Benjamin340d5ed2015-03-21 02:21:37 -0400946 {
David Benjaminc574f412015-04-20 11:13:01 -0400947 name: "UnsupportedCurve",
948 config: Config{
949 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
950 // BoringSSL implements P-224 but doesn't enable it by
951 // default.
952 CurvePreferences: []CurveID{CurveP224},
953 Bugs: ProtocolBugs{
954 IgnorePeerCurvePreferences: true,
955 },
956 },
957 shouldFail: true,
958 expectedError: ":WRONG_CURVE:",
959 },
960 {
David Benjamin513f0ea2015-04-02 19:33:31 -0400961 name: "BadFinished",
962 config: Config{
963 Bugs: ProtocolBugs{
964 BadFinished: true,
965 },
966 },
967 shouldFail: true,
968 expectedError: ":DIGEST_CHECK_FAILED:",
969 },
970 {
971 name: "FalseStart-BadFinished",
972 config: Config{
973 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
974 NextProtos: []string{"foo"},
975 Bugs: ProtocolBugs{
976 BadFinished: true,
977 ExpectFalseStart: true,
978 },
979 },
980 flags: []string{
981 "-false-start",
David Benjamin87e4acd2015-04-02 19:57:35 -0400982 "-handshake-never-done",
David Benjamin513f0ea2015-04-02 19:33:31 -0400983 "-advertise-alpn", "\x03foo",
984 },
985 shimWritesFirst: true,
986 shouldFail: true,
987 expectedError: ":DIGEST_CHECK_FAILED:",
988 },
David Benjamin1c633152015-04-02 20:19:11 -0400989 {
990 name: "NoFalseStart-NoALPN",
991 config: Config{
992 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
993 Bugs: ProtocolBugs{
994 ExpectFalseStart: true,
995 AlertBeforeFalseStartTest: alertAccessDenied,
996 },
997 },
998 flags: []string{
999 "-false-start",
1000 },
1001 shimWritesFirst: true,
1002 shouldFail: true,
1003 expectedError: ":TLSV1_ALERT_ACCESS_DENIED:",
1004 expectedLocalError: "tls: peer did not false start: EOF",
1005 },
1006 {
1007 name: "NoFalseStart-NoAEAD",
1008 config: Config{
1009 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1010 NextProtos: []string{"foo"},
1011 Bugs: ProtocolBugs{
1012 ExpectFalseStart: true,
1013 AlertBeforeFalseStartTest: alertAccessDenied,
1014 },
1015 },
1016 flags: []string{
1017 "-false-start",
1018 "-advertise-alpn", "\x03foo",
1019 },
1020 shimWritesFirst: true,
1021 shouldFail: true,
1022 expectedError: ":TLSV1_ALERT_ACCESS_DENIED:",
1023 expectedLocalError: "tls: peer did not false start: EOF",
1024 },
1025 {
1026 name: "NoFalseStart-RSA",
1027 config: Config{
1028 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
1029 NextProtos: []string{"foo"},
1030 Bugs: ProtocolBugs{
1031 ExpectFalseStart: true,
1032 AlertBeforeFalseStartTest: alertAccessDenied,
1033 },
1034 },
1035 flags: []string{
1036 "-false-start",
1037 "-advertise-alpn", "\x03foo",
1038 },
1039 shimWritesFirst: true,
1040 shouldFail: true,
1041 expectedError: ":TLSV1_ALERT_ACCESS_DENIED:",
1042 expectedLocalError: "tls: peer did not false start: EOF",
1043 },
1044 {
1045 name: "NoFalseStart-DHE_RSA",
1046 config: Config{
1047 CipherSuites: []uint16{TLS_DHE_RSA_WITH_AES_128_GCM_SHA256},
1048 NextProtos: []string{"foo"},
1049 Bugs: ProtocolBugs{
1050 ExpectFalseStart: true,
1051 AlertBeforeFalseStartTest: alertAccessDenied,
1052 },
1053 },
1054 flags: []string{
1055 "-false-start",
1056 "-advertise-alpn", "\x03foo",
1057 },
1058 shimWritesFirst: true,
1059 shouldFail: true,
1060 expectedError: ":TLSV1_ALERT_ACCESS_DENIED:",
1061 expectedLocalError: "tls: peer did not false start: EOF",
1062 },
David Benjamin55a43642015-04-20 14:45:55 -04001063 {
1064 testType: serverTest,
1065 name: "NoSupportedCurves",
1066 config: Config{
1067 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1068 Bugs: ProtocolBugs{
1069 NoSupportedCurves: true,
1070 },
1071 },
1072 },
David Benjamin90da8c82015-04-20 14:57:57 -04001073 {
1074 testType: serverTest,
1075 name: "NoCommonCurves",
1076 config: Config{
1077 CipherSuites: []uint16{
1078 TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
1079 TLS_DHE_RSA_WITH_AES_128_GCM_SHA256,
1080 },
1081 CurvePreferences: []CurveID{CurveP224},
1082 },
1083 expectedCipher: TLS_DHE_RSA_WITH_AES_128_GCM_SHA256,
1084 },
David Benjamin9a41d1b2015-05-16 01:30:09 -04001085 {
1086 protocol: dtls,
1087 name: "SendSplitAlert-Sync",
1088 config: Config{
1089 Bugs: ProtocolBugs{
1090 SendSplitAlert: true,
1091 },
1092 },
1093 },
1094 {
1095 protocol: dtls,
1096 name: "SendSplitAlert-Async",
1097 config: Config{
1098 Bugs: ProtocolBugs{
1099 SendSplitAlert: true,
1100 },
1101 },
1102 flags: []string{"-async"},
1103 },
David Benjaminbd15a8e2015-05-29 18:48:16 -04001104 {
1105 protocol: dtls,
1106 name: "PackDTLSHandshake",
1107 config: Config{
1108 Bugs: ProtocolBugs{
1109 MaxHandshakeRecordLength: 2,
1110 PackHandshakeFragments: 20,
1111 PackHandshakeRecords: 200,
1112 },
1113 },
1114 },
David Benjamin0fa40122015-05-30 17:13:12 -04001115 {
1116 testType: serverTest,
1117 protocol: dtls,
1118 name: "NoRC4-DTLS",
1119 config: Config{
1120 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_RC4_128_SHA},
1121 Bugs: ProtocolBugs{
1122 EnableAllCiphersInDTLS: true,
1123 },
1124 },
1125 shouldFail: true,
1126 expectedError: ":NO_SHARED_CIPHER:",
1127 },
David Benjamina8ebe222015-06-06 03:04:39 -04001128 {
1129 name: "SendEmptyRecords-Pass",
1130 sendEmptyRecords: 32,
1131 },
1132 {
1133 name: "SendEmptyRecords",
1134 sendEmptyRecords: 33,
1135 shouldFail: true,
1136 expectedError: ":TOO_MANY_EMPTY_FRAGMENTS:",
1137 },
1138 {
1139 name: "SendEmptyRecords-Async",
1140 sendEmptyRecords: 33,
1141 flags: []string{"-async"},
1142 shouldFail: true,
1143 expectedError: ":TOO_MANY_EMPTY_FRAGMENTS:",
1144 },
David Benjamin24f346d2015-06-06 03:28:08 -04001145 {
1146 name: "SendWarningAlerts-Pass",
1147 sendWarningAlerts: 4,
1148 },
1149 {
1150 protocol: dtls,
1151 name: "SendWarningAlerts-DTLS-Pass",
1152 sendWarningAlerts: 4,
1153 },
1154 {
1155 name: "SendWarningAlerts",
1156 sendWarningAlerts: 5,
1157 shouldFail: true,
1158 expectedError: ":TOO_MANY_WARNING_ALERTS:",
1159 },
1160 {
1161 name: "SendWarningAlerts-Async",
1162 sendWarningAlerts: 5,
1163 flags: []string{"-async"},
1164 shouldFail: true,
1165 expectedError: ":TOO_MANY_WARNING_ALERTS:",
1166 },
Adam Langley95c29f32014-06-20 12:00:00 -07001167}
1168
David Benjamin01fe8202014-09-24 15:21:44 -04001169func doExchange(test *testCase, config *Config, conn net.Conn, messageLen int, isResume bool) error {
David Benjamin65ea8ff2014-11-23 03:01:00 -05001170 var connDebug *recordingConn
David Benjamin5fa3eba2015-01-22 16:35:40 -05001171 var connDamage *damageAdaptor
David Benjamin65ea8ff2014-11-23 03:01:00 -05001172 if *flagDebug {
1173 connDebug = &recordingConn{Conn: conn}
1174 conn = connDebug
1175 defer func() {
1176 connDebug.WriteTo(os.Stdout)
1177 }()
1178 }
1179
David Benjamin6fd297b2014-08-11 18:43:38 -04001180 if test.protocol == dtls {
David Benjamin83f90402015-01-27 01:09:43 -05001181 config.Bugs.PacketAdaptor = newPacketAdaptor(conn)
1182 conn = config.Bugs.PacketAdaptor
David Benjamin5e961c12014-11-07 01:48:35 -05001183 if test.replayWrites {
1184 conn = newReplayAdaptor(conn)
1185 }
David Benjamin6fd297b2014-08-11 18:43:38 -04001186 }
1187
David Benjamin5fa3eba2015-01-22 16:35:40 -05001188 if test.damageFirstWrite {
1189 connDamage = newDamageAdaptor(conn)
1190 conn = connDamage
1191 }
1192
David Benjamin6fd297b2014-08-11 18:43:38 -04001193 if test.sendPrefix != "" {
1194 if _, err := conn.Write([]byte(test.sendPrefix)); err != nil {
1195 return err
1196 }
David Benjamin98e882e2014-08-08 13:24:34 -04001197 }
1198
David Benjamin1d5c83e2014-07-22 19:20:02 -04001199 var tlsConn *Conn
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001200 if test.testType == clientTest {
David Benjamin6fd297b2014-08-11 18:43:38 -04001201 if test.protocol == dtls {
1202 tlsConn = DTLSServer(conn, config)
1203 } else {
1204 tlsConn = Server(conn, config)
1205 }
David Benjamin1d5c83e2014-07-22 19:20:02 -04001206 } else {
1207 config.InsecureSkipVerify = true
David Benjamin6fd297b2014-08-11 18:43:38 -04001208 if test.protocol == dtls {
1209 tlsConn = DTLSClient(conn, config)
1210 } else {
1211 tlsConn = Client(conn, config)
1212 }
David Benjamin1d5c83e2014-07-22 19:20:02 -04001213 }
1214
Adam Langley95c29f32014-06-20 12:00:00 -07001215 if err := tlsConn.Handshake(); err != nil {
1216 return err
1217 }
Kenny Root7fdeaf12014-08-05 15:23:37 -07001218
David Benjamin01fe8202014-09-24 15:21:44 -04001219 // TODO(davidben): move all per-connection expectations into a dedicated
1220 // expectations struct that can be specified separately for the two
1221 // legs.
1222 expectedVersion := test.expectedVersion
1223 if isResume && test.expectedResumeVersion != 0 {
1224 expectedVersion = test.expectedResumeVersion
1225 }
Adam Langleyb0eef0a2015-06-02 10:47:39 -07001226 connState := tlsConn.ConnectionState()
1227 if vers := connState.Version; expectedVersion != 0 && vers != expectedVersion {
David Benjamin01fe8202014-09-24 15:21:44 -04001228 return fmt.Errorf("got version %x, expected %x", vers, expectedVersion)
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001229 }
1230
Adam Langleyb0eef0a2015-06-02 10:47:39 -07001231 if cipher := connState.CipherSuite; test.expectedCipher != 0 && cipher != test.expectedCipher {
David Benjamin90da8c82015-04-20 14:57:57 -04001232 return fmt.Errorf("got cipher %x, expected %x", cipher, test.expectedCipher)
1233 }
Adam Langleyb0eef0a2015-06-02 10:47:39 -07001234 if didResume := connState.DidResume; isResume && didResume == test.expectResumeRejected {
1235 return fmt.Errorf("didResume is %t, but we expected the opposite", didResume)
1236 }
David Benjamin90da8c82015-04-20 14:57:57 -04001237
David Benjamina08e49d2014-08-24 01:46:07 -04001238 if test.expectChannelID {
Adam Langleyb0eef0a2015-06-02 10:47:39 -07001239 channelID := connState.ChannelID
David Benjamina08e49d2014-08-24 01:46:07 -04001240 if channelID == nil {
1241 return fmt.Errorf("no channel ID negotiated")
1242 }
1243 if channelID.Curve != channelIDKey.Curve ||
1244 channelIDKey.X.Cmp(channelIDKey.X) != 0 ||
1245 channelIDKey.Y.Cmp(channelIDKey.Y) != 0 {
1246 return fmt.Errorf("incorrect channel ID")
1247 }
1248 }
1249
David Benjaminae2888f2014-09-06 12:58:58 -04001250 if expected := test.expectedNextProto; expected != "" {
Adam Langleyb0eef0a2015-06-02 10:47:39 -07001251 if actual := connState.NegotiatedProtocol; actual != expected {
David Benjaminae2888f2014-09-06 12:58:58 -04001252 return fmt.Errorf("next proto mismatch: got %s, wanted %s", actual, expected)
1253 }
1254 }
1255
David Benjaminfc7b0862014-09-06 13:21:53 -04001256 if test.expectedNextProtoType != 0 {
Adam Langleyb0eef0a2015-06-02 10:47:39 -07001257 if (test.expectedNextProtoType == alpn) != connState.NegotiatedProtocolFromALPN {
David Benjaminfc7b0862014-09-06 13:21:53 -04001258 return fmt.Errorf("next proto type mismatch")
1259 }
1260 }
1261
Adam Langleyb0eef0a2015-06-02 10:47:39 -07001262 if p := connState.SRTPProtectionProfile; p != test.expectedSRTPProtectionProfile {
David Benjaminca6c8262014-11-15 19:06:08 -05001263 return fmt.Errorf("SRTP profile mismatch: got %d, wanted %d", p, test.expectedSRTPProtectionProfile)
1264 }
1265
David Benjaminc565ebb2015-04-03 04:06:36 -04001266 if test.exportKeyingMaterial > 0 {
1267 actual := make([]byte, test.exportKeyingMaterial)
1268 if _, err := io.ReadFull(tlsConn, actual); err != nil {
1269 return err
1270 }
1271 expected, err := tlsConn.ExportKeyingMaterial(test.exportKeyingMaterial, []byte(test.exportLabel), []byte(test.exportContext), test.useExportContext)
1272 if err != nil {
1273 return err
1274 }
1275 if !bytes.Equal(actual, expected) {
1276 return fmt.Errorf("keying material mismatch")
1277 }
1278 }
1279
Adam Langleyaf0e32c2015-06-03 09:57:23 -07001280 if test.testTLSUnique {
1281 var peersValue [12]byte
1282 if _, err := io.ReadFull(tlsConn, peersValue[:]); err != nil {
1283 return err
1284 }
1285 expected := tlsConn.ConnectionState().TLSUnique
1286 if !bytes.Equal(peersValue[:], expected) {
1287 return fmt.Errorf("tls-unique mismatch: peer sent %x, but %x was expected", peersValue[:], expected)
1288 }
1289 }
1290
David Benjamine58c4f52014-08-24 03:47:07 -04001291 if test.shimWritesFirst {
1292 var buf [5]byte
1293 _, err := io.ReadFull(tlsConn, buf[:])
1294 if err != nil {
1295 return err
1296 }
1297 if string(buf[:]) != "hello" {
1298 return fmt.Errorf("bad initial message")
1299 }
1300 }
1301
David Benjamina8ebe222015-06-06 03:04:39 -04001302 for i := 0; i < test.sendEmptyRecords; i++ {
1303 tlsConn.Write(nil)
1304 }
1305
David Benjamin24f346d2015-06-06 03:28:08 -04001306 for i := 0; i < test.sendWarningAlerts; i++ {
1307 tlsConn.SendAlert(alertLevelWarning, alertUnexpectedMessage)
1308 }
1309
Adam Langleycf2d4f42014-10-28 19:06:14 -07001310 if test.renegotiate {
1311 if test.renegotiateCiphers != nil {
1312 config.CipherSuites = test.renegotiateCiphers
1313 }
1314 if err := tlsConn.Renegotiate(); err != nil {
1315 return err
1316 }
1317 } else if test.renegotiateCiphers != nil {
1318 panic("renegotiateCiphers without renegotiate")
1319 }
1320
David Benjamin5fa3eba2015-01-22 16:35:40 -05001321 if test.damageFirstWrite {
1322 connDamage.setDamage(true)
1323 tlsConn.Write([]byte("DAMAGED WRITE"))
1324 connDamage.setDamage(false)
1325 }
1326
Kenny Root7fdeaf12014-08-05 15:23:37 -07001327 if messageLen < 0 {
David Benjamin6fd297b2014-08-11 18:43:38 -04001328 if test.protocol == dtls {
1329 return fmt.Errorf("messageLen < 0 not supported for DTLS tests")
1330 }
Kenny Root7fdeaf12014-08-05 15:23:37 -07001331 // Read until EOF.
1332 _, err := io.Copy(ioutil.Discard, tlsConn)
1333 return err
1334 }
1335
David Benjamin4417d052015-04-05 04:17:25 -04001336 if messageLen == 0 {
1337 messageLen = 32
Adam Langley80842bd2014-06-20 12:00:00 -07001338 }
David Benjamin4417d052015-04-05 04:17:25 -04001339 testMessage := make([]byte, messageLen)
1340 for i := range testMessage {
1341 testMessage[i] = 0x42
1342 }
1343 tlsConn.Write(testMessage)
Adam Langley95c29f32014-06-20 12:00:00 -07001344
David Benjamina8ebe222015-06-06 03:04:39 -04001345 for i := 0; i < test.sendEmptyRecords; i++ {
1346 tlsConn.Write(nil)
1347 }
1348
David Benjamin24f346d2015-06-06 03:28:08 -04001349 for i := 0; i < test.sendWarningAlerts; i++ {
1350 tlsConn.SendAlert(alertLevelWarning, alertUnexpectedMessage)
1351 }
1352
Adam Langley95c29f32014-06-20 12:00:00 -07001353 buf := make([]byte, len(testMessage))
David Benjamin6fd297b2014-08-11 18:43:38 -04001354 if test.protocol == dtls {
1355 bufTmp := make([]byte, len(buf)+1)
1356 n, err := tlsConn.Read(bufTmp)
1357 if err != nil {
1358 return err
1359 }
1360 if n != len(buf) {
1361 return fmt.Errorf("bad reply; length mismatch (%d vs %d)", n, len(buf))
1362 }
1363 copy(buf, bufTmp)
1364 } else {
1365 _, err := io.ReadFull(tlsConn, buf)
1366 if err != nil {
1367 return err
1368 }
Adam Langley95c29f32014-06-20 12:00:00 -07001369 }
1370
1371 for i, v := range buf {
1372 if v != testMessage[i]^0xff {
1373 return fmt.Errorf("bad reply contents at byte %d", i)
1374 }
1375 }
1376
1377 return nil
1378}
1379
David Benjamin325b5c32014-07-01 19:40:31 -04001380func valgrindOf(dbAttach bool, path string, args ...string) *exec.Cmd {
1381 valgrindArgs := []string{"--error-exitcode=99", "--track-origins=yes", "--leak-check=full"}
Adam Langley95c29f32014-06-20 12:00:00 -07001382 if dbAttach {
David Benjamin325b5c32014-07-01 19:40:31 -04001383 valgrindArgs = append(valgrindArgs, "--db-attach=yes", "--db-command=xterm -e gdb -nw %f %p")
Adam Langley95c29f32014-06-20 12:00:00 -07001384 }
David Benjamin325b5c32014-07-01 19:40:31 -04001385 valgrindArgs = append(valgrindArgs, path)
1386 valgrindArgs = append(valgrindArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -07001387
David Benjamin325b5c32014-07-01 19:40:31 -04001388 return exec.Command("valgrind", valgrindArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -07001389}
1390
David Benjamin325b5c32014-07-01 19:40:31 -04001391func gdbOf(path string, args ...string) *exec.Cmd {
1392 xtermArgs := []string{"-e", "gdb", "--args"}
1393 xtermArgs = append(xtermArgs, path)
1394 xtermArgs = append(xtermArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -07001395
David Benjamin325b5c32014-07-01 19:40:31 -04001396 return exec.Command("xterm", xtermArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -07001397}
1398
Adam Langley69a01602014-11-17 17:26:55 -08001399type moreMallocsError struct{}
1400
1401func (moreMallocsError) Error() string {
1402 return "child process did not exhaust all allocation calls"
1403}
1404
1405var errMoreMallocs = moreMallocsError{}
1406
David Benjamin87c8a642015-02-21 01:54:29 -05001407// accept accepts a connection from listener, unless waitChan signals a process
1408// exit first.
1409func acceptOrWait(listener net.Listener, waitChan chan error) (net.Conn, error) {
1410 type connOrError struct {
1411 conn net.Conn
1412 err error
1413 }
1414 connChan := make(chan connOrError, 1)
1415 go func() {
1416 conn, err := listener.Accept()
1417 connChan <- connOrError{conn, err}
1418 close(connChan)
1419 }()
1420 select {
1421 case result := <-connChan:
1422 return result.conn, result.err
1423 case childErr := <-waitChan:
1424 waitChan <- childErr
1425 return nil, fmt.Errorf("child exited early: %s", childErr)
1426 }
1427}
1428
Adam Langley69a01602014-11-17 17:26:55 -08001429func runTest(test *testCase, buildDir string, mallocNumToFail int64) error {
Adam Langley38311732014-10-16 19:04:35 -07001430 if !test.shouldFail && (len(test.expectedError) > 0 || len(test.expectedLocalError) > 0) {
1431 panic("Error expected without shouldFail in " + test.name)
1432 }
1433
Adam Langleyb0eef0a2015-06-02 10:47:39 -07001434 if test.expectResumeRejected && !test.resumeSession {
1435 panic("expectResumeRejected without resumeSession in " + test.name)
1436 }
1437
David Benjamin87c8a642015-02-21 01:54:29 -05001438 listener, err := net.ListenTCP("tcp4", &net.TCPAddr{IP: net.IP{127, 0, 0, 1}})
1439 if err != nil {
1440 panic(err)
1441 }
1442 defer func() {
1443 if listener != nil {
1444 listener.Close()
1445 }
1446 }()
Adam Langley95c29f32014-06-20 12:00:00 -07001447
David Benjamin884fdf12014-08-02 15:28:23 -04001448 shim_path := path.Join(buildDir, "ssl/test/bssl_shim")
David Benjamin87c8a642015-02-21 01:54:29 -05001449 flags := []string{"-port", strconv.Itoa(listener.Addr().(*net.TCPAddr).Port)}
David Benjamin1d5c83e2014-07-22 19:20:02 -04001450 if test.testType == serverTest {
David Benjamin5a593af2014-08-11 19:51:50 -04001451 flags = append(flags, "-server")
1452
David Benjamin025b3d32014-07-01 19:53:04 -04001453 flags = append(flags, "-key-file")
1454 if test.keyFile == "" {
1455 flags = append(flags, rsaKeyFile)
1456 } else {
1457 flags = append(flags, test.keyFile)
1458 }
1459
1460 flags = append(flags, "-cert-file")
1461 if test.certFile == "" {
1462 flags = append(flags, rsaCertificateFile)
1463 } else {
1464 flags = append(flags, test.certFile)
1465 }
1466 }
David Benjamin5a593af2014-08-11 19:51:50 -04001467
David Benjamin6fd297b2014-08-11 18:43:38 -04001468 if test.protocol == dtls {
1469 flags = append(flags, "-dtls")
1470 }
1471
David Benjamin5a593af2014-08-11 19:51:50 -04001472 if test.resumeSession {
1473 flags = append(flags, "-resume")
1474 }
1475
David Benjamine58c4f52014-08-24 03:47:07 -04001476 if test.shimWritesFirst {
1477 flags = append(flags, "-shim-writes-first")
1478 }
1479
David Benjaminc565ebb2015-04-03 04:06:36 -04001480 if test.exportKeyingMaterial > 0 {
1481 flags = append(flags, "-export-keying-material", strconv.Itoa(test.exportKeyingMaterial))
1482 flags = append(flags, "-export-label", test.exportLabel)
1483 flags = append(flags, "-export-context", test.exportContext)
1484 if test.useExportContext {
1485 flags = append(flags, "-use-export-context")
1486 }
1487 }
Adam Langleyb0eef0a2015-06-02 10:47:39 -07001488 if test.expectResumeRejected {
1489 flags = append(flags, "-expect-session-miss")
1490 }
David Benjaminc565ebb2015-04-03 04:06:36 -04001491
Adam Langleyaf0e32c2015-06-03 09:57:23 -07001492 if test.testTLSUnique {
1493 flags = append(flags, "-tls-unique")
1494 }
1495
David Benjamin025b3d32014-07-01 19:53:04 -04001496 flags = append(flags, test.flags...)
1497
1498 var shim *exec.Cmd
1499 if *useValgrind {
1500 shim = valgrindOf(false, shim_path, flags...)
Adam Langley75712922014-10-10 16:23:43 -07001501 } else if *useGDB {
1502 shim = gdbOf(shim_path, flags...)
David Benjamin025b3d32014-07-01 19:53:04 -04001503 } else {
1504 shim = exec.Command(shim_path, flags...)
1505 }
David Benjamin025b3d32014-07-01 19:53:04 -04001506 shim.Stdin = os.Stdin
1507 var stdoutBuf, stderrBuf bytes.Buffer
1508 shim.Stdout = &stdoutBuf
1509 shim.Stderr = &stderrBuf
Adam Langley69a01602014-11-17 17:26:55 -08001510 if mallocNumToFail >= 0 {
David Benjamin9e128b02015-02-09 13:13:09 -05001511 shim.Env = os.Environ()
1512 shim.Env = append(shim.Env, "MALLOC_NUMBER_TO_FAIL="+strconv.FormatInt(mallocNumToFail, 10))
Adam Langley69a01602014-11-17 17:26:55 -08001513 if *mallocTestDebug {
1514 shim.Env = append(shim.Env, "MALLOC_ABORT_ON_FAIL=1")
1515 }
1516 shim.Env = append(shim.Env, "_MALLOC_CHECK=1")
1517 }
David Benjamin025b3d32014-07-01 19:53:04 -04001518
1519 if err := shim.Start(); err != nil {
Adam Langley95c29f32014-06-20 12:00:00 -07001520 panic(err)
1521 }
David Benjamin87c8a642015-02-21 01:54:29 -05001522 waitChan := make(chan error, 1)
1523 go func() { waitChan <- shim.Wait() }()
Adam Langley95c29f32014-06-20 12:00:00 -07001524
1525 config := test.config
David Benjamin1d5c83e2014-07-22 19:20:02 -04001526 config.ClientSessionCache = NewLRUClientSessionCache(1)
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001527 config.ServerSessionCache = NewLRUServerSessionCache(1)
David Benjamin025b3d32014-07-01 19:53:04 -04001528 if test.testType == clientTest {
1529 if len(config.Certificates) == 0 {
1530 config.Certificates = []Certificate{getRSACertificate()}
1531 }
David Benjamin87c8a642015-02-21 01:54:29 -05001532 } else {
1533 // Supply a ServerName to ensure a constant session cache key,
1534 // rather than falling back to net.Conn.RemoteAddr.
1535 if len(config.ServerName) == 0 {
1536 config.ServerName = "test"
1537 }
David Benjamin025b3d32014-07-01 19:53:04 -04001538 }
Adam Langley95c29f32014-06-20 12:00:00 -07001539
David Benjamin87c8a642015-02-21 01:54:29 -05001540 conn, err := acceptOrWait(listener, waitChan)
1541 if err == nil {
1542 err = doExchange(test, &config, conn, test.messageLen, false /* not a resumption */)
1543 conn.Close()
1544 }
David Benjamin65ea8ff2014-11-23 03:01:00 -05001545
David Benjamin1d5c83e2014-07-22 19:20:02 -04001546 if err == nil && test.resumeSession {
David Benjamin01fe8202014-09-24 15:21:44 -04001547 var resumeConfig Config
1548 if test.resumeConfig != nil {
1549 resumeConfig = *test.resumeConfig
David Benjamin87c8a642015-02-21 01:54:29 -05001550 if len(resumeConfig.ServerName) == 0 {
1551 resumeConfig.ServerName = config.ServerName
1552 }
David Benjamin01fe8202014-09-24 15:21:44 -04001553 if len(resumeConfig.Certificates) == 0 {
1554 resumeConfig.Certificates = []Certificate{getRSACertificate()}
1555 }
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001556 if !test.newSessionsOnResume {
1557 resumeConfig.SessionTicketKey = config.SessionTicketKey
1558 resumeConfig.ClientSessionCache = config.ClientSessionCache
1559 resumeConfig.ServerSessionCache = config.ServerSessionCache
1560 }
David Benjamin01fe8202014-09-24 15:21:44 -04001561 } else {
1562 resumeConfig = config
1563 }
David Benjamin87c8a642015-02-21 01:54:29 -05001564 var connResume net.Conn
1565 connResume, err = acceptOrWait(listener, waitChan)
1566 if err == nil {
1567 err = doExchange(test, &resumeConfig, connResume, test.messageLen, true /* resumption */)
1568 connResume.Close()
1569 }
David Benjamin1d5c83e2014-07-22 19:20:02 -04001570 }
1571
David Benjamin87c8a642015-02-21 01:54:29 -05001572 // Close the listener now. This is to avoid hangs should the shim try to
1573 // open more connections than expected.
1574 listener.Close()
1575 listener = nil
1576
1577 childErr := <-waitChan
Adam Langley69a01602014-11-17 17:26:55 -08001578 if exitError, ok := childErr.(*exec.ExitError); ok {
1579 if exitError.Sys().(syscall.WaitStatus).ExitStatus() == 88 {
1580 return errMoreMallocs
1581 }
1582 }
Adam Langley95c29f32014-06-20 12:00:00 -07001583
1584 stdout := string(stdoutBuf.Bytes())
1585 stderr := string(stderrBuf.Bytes())
1586 failed := err != nil || childErr != nil
David Benjaminc565ebb2015-04-03 04:06:36 -04001587 correctFailure := len(test.expectedError) == 0 || strings.Contains(stderr, test.expectedError)
Adam Langleyac61fa32014-06-23 12:03:11 -07001588 localError := "none"
1589 if err != nil {
1590 localError = err.Error()
1591 }
1592 if len(test.expectedLocalError) != 0 {
1593 correctFailure = correctFailure && strings.Contains(localError, test.expectedLocalError)
1594 }
Adam Langley95c29f32014-06-20 12:00:00 -07001595
1596 if failed != test.shouldFail || failed && !correctFailure {
Adam Langley95c29f32014-06-20 12:00:00 -07001597 childError := "none"
Adam Langley95c29f32014-06-20 12:00:00 -07001598 if childErr != nil {
1599 childError = childErr.Error()
1600 }
1601
1602 var msg string
1603 switch {
1604 case failed && !test.shouldFail:
1605 msg = "unexpected failure"
1606 case !failed && test.shouldFail:
1607 msg = "unexpected success"
1608 case failed && !correctFailure:
Adam Langleyac61fa32014-06-23 12:03:11 -07001609 msg = "bad error (wanted '" + test.expectedError + "' / '" + test.expectedLocalError + "')"
Adam Langley95c29f32014-06-20 12:00:00 -07001610 default:
1611 panic("internal error")
1612 }
1613
David Benjaminc565ebb2015-04-03 04:06:36 -04001614 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 -07001615 }
1616
David Benjaminc565ebb2015-04-03 04:06:36 -04001617 if !*useValgrind && !failed && len(stderr) > 0 {
Adam Langley95c29f32014-06-20 12:00:00 -07001618 println(stderr)
1619 }
1620
1621 return nil
1622}
1623
1624var tlsVersions = []struct {
1625 name string
1626 version uint16
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001627 flag string
David Benjamin8b8c0062014-11-23 02:47:52 -05001628 hasDTLS bool
Adam Langley95c29f32014-06-20 12:00:00 -07001629}{
David Benjamin8b8c0062014-11-23 02:47:52 -05001630 {"SSL3", VersionSSL30, "-no-ssl3", false},
1631 {"TLS1", VersionTLS10, "-no-tls1", true},
1632 {"TLS11", VersionTLS11, "-no-tls11", false},
1633 {"TLS12", VersionTLS12, "-no-tls12", true},
Adam Langley95c29f32014-06-20 12:00:00 -07001634}
1635
1636var testCipherSuites = []struct {
1637 name string
1638 id uint16
1639}{
1640 {"3DES-SHA", TLS_RSA_WITH_3DES_EDE_CBC_SHA},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001641 {"AES128-GCM", TLS_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001642 {"AES128-SHA", TLS_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001643 {"AES128-SHA256", TLS_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001644 {"AES256-GCM", TLS_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001645 {"AES256-SHA", TLS_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001646 {"AES256-SHA256", TLS_RSA_WITH_AES_256_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001647 {"DHE-RSA-AES128-GCM", TLS_DHE_RSA_WITH_AES_128_GCM_SHA256},
1648 {"DHE-RSA-AES128-SHA", TLS_DHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001649 {"DHE-RSA-AES128-SHA256", TLS_DHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001650 {"DHE-RSA-AES256-GCM", TLS_DHE_RSA_WITH_AES_256_GCM_SHA384},
1651 {"DHE-RSA-AES256-SHA", TLS_DHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001652 {"DHE-RSA-AES256-SHA256", TLS_DHE_RSA_WITH_AES_256_CBC_SHA256},
David Benjamine9a80ff2015-04-07 00:46:46 -04001653 {"DHE-RSA-CHACHA20-POLY1305", TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001654 {"ECDHE-ECDSA-AES128-GCM", TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
1655 {"ECDHE-ECDSA-AES128-SHA", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001656 {"ECDHE-ECDSA-AES128-SHA256", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256},
1657 {"ECDHE-ECDSA-AES256-GCM", TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001658 {"ECDHE-ECDSA-AES256-SHA", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001659 {"ECDHE-ECDSA-AES256-SHA384", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384},
David Benjamine9a80ff2015-04-07 00:46:46 -04001660 {"ECDHE-ECDSA-CHACHA20-POLY1305", TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001661 {"ECDHE-ECDSA-RC4-SHA", TLS_ECDHE_ECDSA_WITH_RC4_128_SHA},
Adam Langley97e8ba82015-04-29 15:32:10 -07001662 {"ECDHE-PSK-AES128-GCM-SHA256", TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001663 {"ECDHE-RSA-AES128-GCM", TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001664 {"ECDHE-RSA-AES128-SHA", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001665 {"ECDHE-RSA-AES128-SHA256", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001666 {"ECDHE-RSA-AES256-GCM", TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001667 {"ECDHE-RSA-AES256-SHA", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001668 {"ECDHE-RSA-AES256-SHA384", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384},
David Benjamine9a80ff2015-04-07 00:46:46 -04001669 {"ECDHE-RSA-CHACHA20-POLY1305", TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001670 {"ECDHE-RSA-RC4-SHA", TLS_ECDHE_RSA_WITH_RC4_128_SHA},
David Benjamin48cae082014-10-27 01:06:24 -04001671 {"PSK-AES128-CBC-SHA", TLS_PSK_WITH_AES_128_CBC_SHA},
1672 {"PSK-AES256-CBC-SHA", TLS_PSK_WITH_AES_256_CBC_SHA},
1673 {"PSK-RC4-SHA", TLS_PSK_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -07001674 {"RC4-MD5", TLS_RSA_WITH_RC4_128_MD5},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001675 {"RC4-SHA", TLS_RSA_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -07001676}
1677
David Benjamin8b8c0062014-11-23 02:47:52 -05001678func hasComponent(suiteName, component string) bool {
1679 return strings.Contains("-"+suiteName+"-", "-"+component+"-")
1680}
1681
David Benjaminf7768e42014-08-31 02:06:47 -04001682func isTLS12Only(suiteName string) bool {
David Benjamin8b8c0062014-11-23 02:47:52 -05001683 return hasComponent(suiteName, "GCM") ||
1684 hasComponent(suiteName, "SHA256") ||
David Benjamine9a80ff2015-04-07 00:46:46 -04001685 hasComponent(suiteName, "SHA384") ||
1686 hasComponent(suiteName, "POLY1305")
David Benjamin8b8c0062014-11-23 02:47:52 -05001687}
1688
1689func isDTLSCipher(suiteName string) bool {
David Benjamine95d20d2014-12-23 11:16:01 -05001690 return !hasComponent(suiteName, "RC4")
David Benjaminf7768e42014-08-31 02:06:47 -04001691}
1692
Adam Langleya7997f12015-05-14 17:38:50 -07001693func bigFromHex(hex string) *big.Int {
1694 ret, ok := new(big.Int).SetString(hex, 16)
1695 if !ok {
1696 panic("failed to parse hex number 0x" + hex)
1697 }
1698 return ret
1699}
1700
Adam Langley95c29f32014-06-20 12:00:00 -07001701func addCipherSuiteTests() {
1702 for _, suite := range testCipherSuites {
David Benjamin48cae082014-10-27 01:06:24 -04001703 const psk = "12345"
1704 const pskIdentity = "luggage combo"
1705
Adam Langley95c29f32014-06-20 12:00:00 -07001706 var cert Certificate
David Benjamin025b3d32014-07-01 19:53:04 -04001707 var certFile string
1708 var keyFile string
David Benjamin8b8c0062014-11-23 02:47:52 -05001709 if hasComponent(suite.name, "ECDSA") {
Adam Langley95c29f32014-06-20 12:00:00 -07001710 cert = getECDSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -04001711 certFile = ecdsaCertificateFile
1712 keyFile = ecdsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -07001713 } else {
1714 cert = getRSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -04001715 certFile = rsaCertificateFile
1716 keyFile = rsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -07001717 }
1718
David Benjamin48cae082014-10-27 01:06:24 -04001719 var flags []string
David Benjamin8b8c0062014-11-23 02:47:52 -05001720 if hasComponent(suite.name, "PSK") {
David Benjamin48cae082014-10-27 01:06:24 -04001721 flags = append(flags,
1722 "-psk", psk,
1723 "-psk-identity", pskIdentity)
1724 }
1725
Adam Langley95c29f32014-06-20 12:00:00 -07001726 for _, ver := range tlsVersions {
David Benjaminf7768e42014-08-31 02:06:47 -04001727 if ver.version < VersionTLS12 && isTLS12Only(suite.name) {
Adam Langley95c29f32014-06-20 12:00:00 -07001728 continue
1729 }
1730
David Benjamin025b3d32014-07-01 19:53:04 -04001731 testCases = append(testCases, testCase{
1732 testType: clientTest,
1733 name: ver.name + "-" + suite.name + "-client",
Adam Langley95c29f32014-06-20 12:00:00 -07001734 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001735 MinVersion: ver.version,
1736 MaxVersion: ver.version,
1737 CipherSuites: []uint16{suite.id},
1738 Certificates: []Certificate{cert},
David Benjamin68793732015-05-04 20:20:48 -04001739 PreSharedKey: []byte(psk),
David Benjamin48cae082014-10-27 01:06:24 -04001740 PreSharedKeyIdentity: pskIdentity,
Adam Langley95c29f32014-06-20 12:00:00 -07001741 },
David Benjamin48cae082014-10-27 01:06:24 -04001742 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001743 resumeSession: true,
Adam Langley95c29f32014-06-20 12:00:00 -07001744 })
David Benjamin025b3d32014-07-01 19:53:04 -04001745
David Benjamin76d8abe2014-08-14 16:25:34 -04001746 testCases = append(testCases, testCase{
1747 testType: serverTest,
1748 name: ver.name + "-" + suite.name + "-server",
1749 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001750 MinVersion: ver.version,
1751 MaxVersion: ver.version,
1752 CipherSuites: []uint16{suite.id},
1753 Certificates: []Certificate{cert},
1754 PreSharedKey: []byte(psk),
1755 PreSharedKeyIdentity: pskIdentity,
David Benjamin76d8abe2014-08-14 16:25:34 -04001756 },
1757 certFile: certFile,
1758 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001759 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001760 resumeSession: true,
David Benjamin76d8abe2014-08-14 16:25:34 -04001761 })
David Benjamin6fd297b2014-08-11 18:43:38 -04001762
David Benjamin8b8c0062014-11-23 02:47:52 -05001763 if ver.hasDTLS && isDTLSCipher(suite.name) {
David Benjamin6fd297b2014-08-11 18:43:38 -04001764 testCases = append(testCases, testCase{
1765 testType: clientTest,
1766 protocol: dtls,
1767 name: "D" + ver.name + "-" + suite.name + "-client",
1768 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001769 MinVersion: ver.version,
1770 MaxVersion: ver.version,
1771 CipherSuites: []uint16{suite.id},
1772 Certificates: []Certificate{cert},
1773 PreSharedKey: []byte(psk),
1774 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001775 },
David Benjamin48cae082014-10-27 01:06:24 -04001776 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001777 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001778 })
1779 testCases = append(testCases, testCase{
1780 testType: serverTest,
1781 protocol: dtls,
1782 name: "D" + ver.name + "-" + suite.name + "-server",
1783 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001784 MinVersion: ver.version,
1785 MaxVersion: ver.version,
1786 CipherSuites: []uint16{suite.id},
1787 Certificates: []Certificate{cert},
1788 PreSharedKey: []byte(psk),
1789 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001790 },
1791 certFile: certFile,
1792 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001793 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001794 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001795 })
1796 }
Adam Langley95c29f32014-06-20 12:00:00 -07001797 }
1798 }
Adam Langleya7997f12015-05-14 17:38:50 -07001799
1800 testCases = append(testCases, testCase{
1801 name: "WeakDH",
1802 config: Config{
1803 CipherSuites: []uint16{TLS_DHE_RSA_WITH_AES_128_GCM_SHA256},
1804 Bugs: ProtocolBugs{
1805 // This is a 1023-bit prime number, generated
1806 // with:
1807 // openssl gendh 1023 | openssl asn1parse -i
1808 DHGroupPrime: bigFromHex("518E9B7930CE61C6E445C8360584E5FC78D9137C0FFDC880B495D5338ADF7689951A6821C17A76B3ACB8E0156AEA607B7EC406EBEDBB84D8376EB8FE8F8BA1433488BEE0C3EDDFD3A32DBB9481980A7AF6C96BFCF490A094CFFB2B8192C1BB5510B77B658436E27C2D4D023FE3718222AB0CA1273995B51F6D625A4944D0DD4B"),
1809 },
1810 },
1811 shouldFail: true,
1812 expectedError: "BAD_DH_P_LENGTH",
1813 })
Adam Langley95c29f32014-06-20 12:00:00 -07001814}
1815
1816func addBadECDSASignatureTests() {
1817 for badR := BadValue(1); badR < NumBadValues; badR++ {
1818 for badS := BadValue(1); badS < NumBadValues; badS++ {
David Benjamin025b3d32014-07-01 19:53:04 -04001819 testCases = append(testCases, testCase{
Adam Langley95c29f32014-06-20 12:00:00 -07001820 name: fmt.Sprintf("BadECDSA-%d-%d", badR, badS),
1821 config: Config{
1822 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
1823 Certificates: []Certificate{getECDSACertificate()},
1824 Bugs: ProtocolBugs{
1825 BadECDSAR: badR,
1826 BadECDSAS: badS,
1827 },
1828 },
1829 shouldFail: true,
1830 expectedError: "SIGNATURE",
1831 })
1832 }
1833 }
1834}
1835
Adam Langley80842bd2014-06-20 12:00:00 -07001836func addCBCPaddingTests() {
David Benjamin025b3d32014-07-01 19:53:04 -04001837 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001838 name: "MaxCBCPadding",
1839 config: Config{
1840 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1841 Bugs: ProtocolBugs{
1842 MaxPadding: true,
1843 },
1844 },
1845 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1846 })
David Benjamin025b3d32014-07-01 19:53:04 -04001847 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001848 name: "BadCBCPadding",
1849 config: Config{
1850 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1851 Bugs: ProtocolBugs{
1852 PaddingFirstByteBad: true,
1853 },
1854 },
1855 shouldFail: true,
1856 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1857 })
1858 // OpenSSL previously had an issue where the first byte of padding in
1859 // 255 bytes of padding wasn't checked.
David Benjamin025b3d32014-07-01 19:53:04 -04001860 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001861 name: "BadCBCPadding255",
1862 config: Config{
1863 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1864 Bugs: ProtocolBugs{
1865 MaxPadding: true,
1866 PaddingFirstByteBadIf255: true,
1867 },
1868 },
1869 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1870 shouldFail: true,
1871 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1872 })
1873}
1874
Kenny Root7fdeaf12014-08-05 15:23:37 -07001875func addCBCSplittingTests() {
1876 testCases = append(testCases, testCase{
1877 name: "CBCRecordSplitting",
1878 config: Config{
1879 MaxVersion: VersionTLS10,
1880 MinVersion: VersionTLS10,
1881 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1882 },
1883 messageLen: -1, // read until EOF
1884 flags: []string{
1885 "-async",
1886 "-write-different-record-sizes",
1887 "-cbc-record-splitting",
1888 },
David Benjamina8e3e0e2014-08-06 22:11:10 -04001889 })
1890 testCases = append(testCases, testCase{
Kenny Root7fdeaf12014-08-05 15:23:37 -07001891 name: "CBCRecordSplittingPartialWrite",
1892 config: Config{
1893 MaxVersion: VersionTLS10,
1894 MinVersion: VersionTLS10,
1895 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1896 },
1897 messageLen: -1, // read until EOF
1898 flags: []string{
1899 "-async",
1900 "-write-different-record-sizes",
1901 "-cbc-record-splitting",
1902 "-partial-write",
1903 },
1904 })
1905}
1906
David Benjamin636293b2014-07-08 17:59:18 -04001907func addClientAuthTests() {
David Benjamin407a10c2014-07-16 12:58:59 -04001908 // Add a dummy cert pool to stress certificate authority parsing.
1909 // TODO(davidben): Add tests that those values parse out correctly.
1910 certPool := x509.NewCertPool()
1911 cert, err := x509.ParseCertificate(rsaCertificate.Certificate[0])
1912 if err != nil {
1913 panic(err)
1914 }
1915 certPool.AddCert(cert)
1916
David Benjamin636293b2014-07-08 17:59:18 -04001917 for _, ver := range tlsVersions {
David Benjamin636293b2014-07-08 17:59:18 -04001918 testCases = append(testCases, testCase{
1919 testType: clientTest,
David Benjamin67666e72014-07-12 15:47:52 -04001920 name: ver.name + "-Client-ClientAuth-RSA",
David Benjamin636293b2014-07-08 17:59:18 -04001921 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001922 MinVersion: ver.version,
1923 MaxVersion: ver.version,
1924 ClientAuth: RequireAnyClientCert,
1925 ClientCAs: certPool,
David Benjamin636293b2014-07-08 17:59:18 -04001926 },
1927 flags: []string{
1928 "-cert-file", rsaCertificateFile,
1929 "-key-file", rsaKeyFile,
1930 },
1931 })
1932 testCases = append(testCases, testCase{
David Benjamin67666e72014-07-12 15:47:52 -04001933 testType: serverTest,
1934 name: ver.name + "-Server-ClientAuth-RSA",
1935 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001936 MinVersion: ver.version,
1937 MaxVersion: ver.version,
David Benjamin67666e72014-07-12 15:47:52 -04001938 Certificates: []Certificate{rsaCertificate},
1939 },
1940 flags: []string{"-require-any-client-certificate"},
1941 })
David Benjamine098ec22014-08-27 23:13:20 -04001942 if ver.version != VersionSSL30 {
1943 testCases = append(testCases, testCase{
1944 testType: serverTest,
1945 name: ver.name + "-Server-ClientAuth-ECDSA",
1946 config: Config{
1947 MinVersion: ver.version,
1948 MaxVersion: ver.version,
1949 Certificates: []Certificate{ecdsaCertificate},
1950 },
1951 flags: []string{"-require-any-client-certificate"},
1952 })
1953 testCases = append(testCases, testCase{
1954 testType: clientTest,
1955 name: ver.name + "-Client-ClientAuth-ECDSA",
1956 config: Config{
1957 MinVersion: ver.version,
1958 MaxVersion: ver.version,
1959 ClientAuth: RequireAnyClientCert,
1960 ClientCAs: certPool,
1961 },
1962 flags: []string{
1963 "-cert-file", ecdsaCertificateFile,
1964 "-key-file", ecdsaKeyFile,
1965 },
1966 })
1967 }
David Benjamin636293b2014-07-08 17:59:18 -04001968 }
1969}
1970
Adam Langley75712922014-10-10 16:23:43 -07001971func addExtendedMasterSecretTests() {
1972 const expectEMSFlag = "-expect-extended-master-secret"
1973
1974 for _, with := range []bool{false, true} {
1975 prefix := "No"
1976 var flags []string
1977 if with {
1978 prefix = ""
1979 flags = []string{expectEMSFlag}
1980 }
1981
1982 for _, isClient := range []bool{false, true} {
1983 suffix := "-Server"
1984 testType := serverTest
1985 if isClient {
1986 suffix = "-Client"
1987 testType = clientTest
1988 }
1989
1990 for _, ver := range tlsVersions {
1991 test := testCase{
1992 testType: testType,
1993 name: prefix + "ExtendedMasterSecret-" + ver.name + suffix,
1994 config: Config{
1995 MinVersion: ver.version,
1996 MaxVersion: ver.version,
1997 Bugs: ProtocolBugs{
1998 NoExtendedMasterSecret: !with,
1999 RequireExtendedMasterSecret: with,
2000 },
2001 },
David Benjamin48cae082014-10-27 01:06:24 -04002002 flags: flags,
2003 shouldFail: ver.version == VersionSSL30 && with,
Adam Langley75712922014-10-10 16:23:43 -07002004 }
2005 if test.shouldFail {
2006 test.expectedLocalError = "extended master secret required but not supported by peer"
2007 }
2008 testCases = append(testCases, test)
2009 }
2010 }
2011 }
2012
Adam Langleyba5934b2015-06-02 10:50:35 -07002013 for _, isClient := range []bool{false, true} {
2014 for _, supportedInFirstConnection := range []bool{false, true} {
2015 for _, supportedInResumeConnection := range []bool{false, true} {
2016 boolToWord := func(b bool) string {
2017 if b {
2018 return "Yes"
2019 }
2020 return "No"
2021 }
2022 suffix := boolToWord(supportedInFirstConnection) + "To" + boolToWord(supportedInResumeConnection) + "-"
2023 if isClient {
2024 suffix += "Client"
2025 } else {
2026 suffix += "Server"
2027 }
2028
2029 supportedConfig := Config{
2030 Bugs: ProtocolBugs{
2031 RequireExtendedMasterSecret: true,
2032 },
2033 }
2034
2035 noSupportConfig := Config{
2036 Bugs: ProtocolBugs{
2037 NoExtendedMasterSecret: true,
2038 },
2039 }
2040
2041 test := testCase{
2042 name: "ExtendedMasterSecret-" + suffix,
2043 resumeSession: true,
2044 }
2045
2046 if !isClient {
2047 test.testType = serverTest
2048 }
2049
2050 if supportedInFirstConnection {
2051 test.config = supportedConfig
2052 } else {
2053 test.config = noSupportConfig
2054 }
2055
2056 if supportedInResumeConnection {
2057 test.resumeConfig = &supportedConfig
2058 } else {
2059 test.resumeConfig = &noSupportConfig
2060 }
2061
2062 switch suffix {
2063 case "YesToYes-Client", "YesToYes-Server":
2064 // When a session is resumed, it should
2065 // still be aware that its master
2066 // secret was generated via EMS and
2067 // thus it's safe to use tls-unique.
2068 test.flags = []string{expectEMSFlag}
2069 case "NoToYes-Server":
2070 // If an original connection did not
2071 // contain EMS, but a resumption
2072 // handshake does, then a server should
2073 // not resume the session.
2074 test.expectResumeRejected = true
2075 case "YesToNo-Server":
2076 // Resuming an EMS session without the
2077 // EMS extension should cause the
2078 // server to abort the connection.
2079 test.shouldFail = true
2080 test.expectedError = ":RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION:"
2081 case "NoToYes-Client":
2082 // A client should abort a connection
2083 // where the server resumed a non-EMS
2084 // session but echoed the EMS
2085 // extension.
2086 test.shouldFail = true
2087 test.expectedError = ":RESUMED_NON_EMS_SESSION_WITH_EMS_EXTENSION:"
2088 case "YesToNo-Client":
2089 // A client should abort a connection
2090 // where the server didn't echo EMS
2091 // when the session used it.
2092 test.shouldFail = true
2093 test.expectedError = ":RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION:"
2094 }
2095
2096 testCases = append(testCases, test)
2097 }
2098 }
2099 }
Adam Langley75712922014-10-10 16:23:43 -07002100}
2101
David Benjamin43ec06f2014-08-05 02:28:57 -04002102// Adds tests that try to cover the range of the handshake state machine, under
2103// various conditions. Some of these are redundant with other tests, but they
2104// only cover the synchronous case.
David Benjamin6fd297b2014-08-11 18:43:38 -04002105func addStateMachineCoverageTests(async, splitHandshake bool, protocol protocol) {
David Benjamin760b1dd2015-05-15 23:33:48 -04002106 var tests []testCase
2107
2108 // Basic handshake, with resumption. Client and server,
2109 // session ID and session ticket.
2110 tests = append(tests, testCase{
2111 name: "Basic-Client",
2112 resumeSession: true,
2113 })
2114 tests = append(tests, testCase{
2115 name: "Basic-Client-RenewTicket",
2116 config: Config{
2117 Bugs: ProtocolBugs{
2118 RenewTicketOnResume: true,
2119 },
2120 },
2121 resumeSession: true,
2122 })
2123 tests = append(tests, testCase{
2124 name: "Basic-Client-NoTicket",
2125 config: Config{
2126 SessionTicketsDisabled: true,
2127 },
2128 resumeSession: true,
2129 })
2130 tests = append(tests, testCase{
2131 name: "Basic-Client-Implicit",
2132 flags: []string{"-implicit-handshake"},
2133 resumeSession: true,
2134 })
2135 tests = append(tests, testCase{
2136 testType: serverTest,
2137 name: "Basic-Server",
2138 resumeSession: true,
2139 })
2140 tests = append(tests, testCase{
2141 testType: serverTest,
2142 name: "Basic-Server-NoTickets",
2143 config: Config{
2144 SessionTicketsDisabled: true,
2145 },
2146 resumeSession: true,
2147 })
2148 tests = append(tests, testCase{
2149 testType: serverTest,
2150 name: "Basic-Server-Implicit",
2151 flags: []string{"-implicit-handshake"},
2152 resumeSession: true,
2153 })
2154 tests = append(tests, testCase{
2155 testType: serverTest,
2156 name: "Basic-Server-EarlyCallback",
2157 flags: []string{"-use-early-callback"},
2158 resumeSession: true,
2159 })
2160
2161 // TLS client auth.
2162 tests = append(tests, testCase{
2163 testType: clientTest,
2164 name: "ClientAuth-Client",
2165 config: Config{
2166 ClientAuth: RequireAnyClientCert,
2167 },
2168 flags: []string{
2169 "-cert-file", rsaCertificateFile,
2170 "-key-file", rsaKeyFile,
2171 },
2172 })
2173 tests = append(tests, testCase{
2174 testType: serverTest,
2175 name: "ClientAuth-Server",
2176 config: Config{
2177 Certificates: []Certificate{rsaCertificate},
2178 },
2179 flags: []string{"-require-any-client-certificate"},
2180 })
2181
2182 // No session ticket support; server doesn't send NewSessionTicket.
2183 tests = append(tests, testCase{
2184 name: "SessionTicketsDisabled-Client",
2185 config: Config{
2186 SessionTicketsDisabled: true,
2187 },
2188 })
2189 tests = append(tests, testCase{
2190 testType: serverTest,
2191 name: "SessionTicketsDisabled-Server",
2192 config: Config{
2193 SessionTicketsDisabled: true,
2194 },
2195 })
2196
2197 // Skip ServerKeyExchange in PSK key exchange if there's no
2198 // identity hint.
2199 tests = append(tests, testCase{
2200 name: "EmptyPSKHint-Client",
2201 config: Config{
2202 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
2203 PreSharedKey: []byte("secret"),
2204 },
2205 flags: []string{"-psk", "secret"},
2206 })
2207 tests = append(tests, testCase{
2208 testType: serverTest,
2209 name: "EmptyPSKHint-Server",
2210 config: Config{
2211 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
2212 PreSharedKey: []byte("secret"),
2213 },
2214 flags: []string{"-psk", "secret"},
2215 })
2216
2217 if protocol == tls {
2218 tests = append(tests, testCase{
2219 name: "Renegotiate-Client",
2220 renegotiate: true,
2221 })
2222 // NPN on client and server; results in post-handshake message.
2223 tests = append(tests, testCase{
2224 name: "NPN-Client",
2225 config: Config{
2226 NextProtos: []string{"foo"},
2227 },
2228 flags: []string{"-select-next-proto", "foo"},
2229 expectedNextProto: "foo",
2230 expectedNextProtoType: npn,
2231 })
2232 tests = append(tests, testCase{
2233 testType: serverTest,
2234 name: "NPN-Server",
2235 config: Config{
2236 NextProtos: []string{"bar"},
2237 },
2238 flags: []string{
2239 "-advertise-npn", "\x03foo\x03bar\x03baz",
2240 "-expect-next-proto", "bar",
2241 },
2242 expectedNextProto: "bar",
2243 expectedNextProtoType: npn,
2244 })
2245
2246 // TODO(davidben): Add tests for when False Start doesn't trigger.
2247
2248 // Client does False Start and negotiates NPN.
2249 tests = append(tests, testCase{
2250 name: "FalseStart",
2251 config: Config{
2252 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2253 NextProtos: []string{"foo"},
2254 Bugs: ProtocolBugs{
2255 ExpectFalseStart: true,
2256 },
2257 },
2258 flags: []string{
2259 "-false-start",
2260 "-select-next-proto", "foo",
2261 },
2262 shimWritesFirst: true,
2263 resumeSession: true,
2264 })
2265
2266 // Client does False Start and negotiates ALPN.
2267 tests = append(tests, testCase{
2268 name: "FalseStart-ALPN",
2269 config: Config{
2270 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2271 NextProtos: []string{"foo"},
2272 Bugs: ProtocolBugs{
2273 ExpectFalseStart: true,
2274 },
2275 },
2276 flags: []string{
2277 "-false-start",
2278 "-advertise-alpn", "\x03foo",
2279 },
2280 shimWritesFirst: true,
2281 resumeSession: true,
2282 })
2283
2284 // Client does False Start but doesn't explicitly call
2285 // SSL_connect.
2286 tests = append(tests, testCase{
2287 name: "FalseStart-Implicit",
2288 config: Config{
2289 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2290 NextProtos: []string{"foo"},
2291 },
2292 flags: []string{
2293 "-implicit-handshake",
2294 "-false-start",
2295 "-advertise-alpn", "\x03foo",
2296 },
2297 })
2298
2299 // False Start without session tickets.
2300 tests = append(tests, testCase{
2301 name: "FalseStart-SessionTicketsDisabled",
2302 config: Config{
2303 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2304 NextProtos: []string{"foo"},
2305 SessionTicketsDisabled: true,
2306 Bugs: ProtocolBugs{
2307 ExpectFalseStart: true,
2308 },
2309 },
2310 flags: []string{
2311 "-false-start",
2312 "-select-next-proto", "foo",
2313 },
2314 shimWritesFirst: true,
2315 })
2316
2317 // Server parses a V2ClientHello.
2318 tests = append(tests, testCase{
2319 testType: serverTest,
2320 name: "SendV2ClientHello",
2321 config: Config{
2322 // Choose a cipher suite that does not involve
2323 // elliptic curves, so no extensions are
2324 // involved.
2325 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2326 Bugs: ProtocolBugs{
2327 SendV2ClientHello: true,
2328 },
2329 },
2330 })
2331
2332 // Client sends a Channel ID.
2333 tests = append(tests, testCase{
2334 name: "ChannelID-Client",
2335 config: Config{
2336 RequestChannelID: true,
2337 },
2338 flags: []string{"-send-channel-id", channelIDKeyFile},
2339 resumeSession: true,
2340 expectChannelID: true,
2341 })
2342
2343 // Server accepts a Channel ID.
2344 tests = append(tests, testCase{
2345 testType: serverTest,
2346 name: "ChannelID-Server",
2347 config: Config{
2348 ChannelID: channelIDKey,
2349 },
2350 flags: []string{
2351 "-expect-channel-id",
2352 base64.StdEncoding.EncodeToString(channelIDBytes),
2353 },
2354 resumeSession: true,
2355 expectChannelID: true,
2356 })
2357 } else {
2358 tests = append(tests, testCase{
2359 name: "SkipHelloVerifyRequest",
2360 config: Config{
2361 Bugs: ProtocolBugs{
2362 SkipHelloVerifyRequest: true,
2363 },
2364 },
2365 })
2366 }
2367
David Benjamin43ec06f2014-08-05 02:28:57 -04002368 var suffix string
2369 var flags []string
2370 var maxHandshakeRecordLength int
David Benjamin6fd297b2014-08-11 18:43:38 -04002371 if protocol == dtls {
2372 suffix = "-DTLS"
2373 }
David Benjamin43ec06f2014-08-05 02:28:57 -04002374 if async {
David Benjamin6fd297b2014-08-11 18:43:38 -04002375 suffix += "-Async"
David Benjamin43ec06f2014-08-05 02:28:57 -04002376 flags = append(flags, "-async")
2377 } else {
David Benjamin6fd297b2014-08-11 18:43:38 -04002378 suffix += "-Sync"
David Benjamin43ec06f2014-08-05 02:28:57 -04002379 }
2380 if splitHandshake {
2381 suffix += "-SplitHandshakeRecords"
David Benjamin98214542014-08-07 18:02:39 -04002382 maxHandshakeRecordLength = 1
David Benjamin43ec06f2014-08-05 02:28:57 -04002383 }
David Benjamin760b1dd2015-05-15 23:33:48 -04002384 for _, test := range tests {
2385 test.protocol = protocol
2386 test.name += suffix
2387 test.config.Bugs.MaxHandshakeRecordLength = maxHandshakeRecordLength
2388 test.flags = append(test.flags, flags...)
2389 testCases = append(testCases, test)
David Benjamin6fd297b2014-08-11 18:43:38 -04002390 }
David Benjamin43ec06f2014-08-05 02:28:57 -04002391}
2392
Adam Langley524e7172015-02-20 16:04:00 -08002393func addDDoSCallbackTests() {
2394 // DDoS callback.
2395
2396 for _, resume := range []bool{false, true} {
2397 suffix := "Resume"
2398 if resume {
2399 suffix = "No" + suffix
2400 }
2401
2402 testCases = append(testCases, testCase{
2403 testType: serverTest,
2404 name: "Server-DDoS-OK-" + suffix,
2405 flags: []string{"-install-ddos-callback"},
2406 resumeSession: resume,
2407 })
2408
2409 failFlag := "-fail-ddos-callback"
2410 if resume {
2411 failFlag = "-fail-second-ddos-callback"
2412 }
2413 testCases = append(testCases, testCase{
2414 testType: serverTest,
2415 name: "Server-DDoS-Reject-" + suffix,
2416 flags: []string{"-install-ddos-callback", failFlag},
2417 resumeSession: resume,
2418 shouldFail: true,
2419 expectedError: ":CONNECTION_REJECTED:",
2420 })
2421 }
2422}
2423
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002424func addVersionNegotiationTests() {
2425 for i, shimVers := range tlsVersions {
2426 // Assemble flags to disable all newer versions on the shim.
2427 var flags []string
2428 for _, vers := range tlsVersions[i+1:] {
2429 flags = append(flags, vers.flag)
2430 }
2431
2432 for _, runnerVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05002433 protocols := []protocol{tls}
2434 if runnerVers.hasDTLS && shimVers.hasDTLS {
2435 protocols = append(protocols, dtls)
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002436 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002437 for _, protocol := range protocols {
2438 expectedVersion := shimVers.version
2439 if runnerVers.version < shimVers.version {
2440 expectedVersion = runnerVers.version
2441 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002442
David Benjamin8b8c0062014-11-23 02:47:52 -05002443 suffix := shimVers.name + "-" + runnerVers.name
2444 if protocol == dtls {
2445 suffix += "-DTLS"
2446 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002447
David Benjamin1eb367c2014-12-12 18:17:51 -05002448 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
2449
David Benjamin1e29a6b2014-12-10 02:27:24 -05002450 clientVers := shimVers.version
2451 if clientVers > VersionTLS10 {
2452 clientVers = VersionTLS10
2453 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002454 testCases = append(testCases, testCase{
2455 protocol: protocol,
2456 testType: clientTest,
2457 name: "VersionNegotiation-Client-" + suffix,
2458 config: Config{
2459 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05002460 Bugs: ProtocolBugs{
2461 ExpectInitialRecordVersion: clientVers,
2462 },
David Benjamin8b8c0062014-11-23 02:47:52 -05002463 },
2464 flags: flags,
2465 expectedVersion: expectedVersion,
2466 })
David Benjamin1eb367c2014-12-12 18:17:51 -05002467 testCases = append(testCases, testCase{
2468 protocol: protocol,
2469 testType: clientTest,
2470 name: "VersionNegotiation-Client2-" + suffix,
2471 config: Config{
2472 MaxVersion: runnerVers.version,
2473 Bugs: ProtocolBugs{
2474 ExpectInitialRecordVersion: clientVers,
2475 },
2476 },
2477 flags: []string{"-max-version", shimVersFlag},
2478 expectedVersion: expectedVersion,
2479 })
David Benjamin8b8c0062014-11-23 02:47:52 -05002480
2481 testCases = append(testCases, testCase{
2482 protocol: protocol,
2483 testType: serverTest,
2484 name: "VersionNegotiation-Server-" + suffix,
2485 config: Config{
2486 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05002487 Bugs: ProtocolBugs{
2488 ExpectInitialRecordVersion: expectedVersion,
2489 },
David Benjamin8b8c0062014-11-23 02:47:52 -05002490 },
2491 flags: flags,
2492 expectedVersion: expectedVersion,
2493 })
David Benjamin1eb367c2014-12-12 18:17:51 -05002494 testCases = append(testCases, testCase{
2495 protocol: protocol,
2496 testType: serverTest,
2497 name: "VersionNegotiation-Server2-" + suffix,
2498 config: Config{
2499 MaxVersion: runnerVers.version,
2500 Bugs: ProtocolBugs{
2501 ExpectInitialRecordVersion: expectedVersion,
2502 },
2503 },
2504 flags: []string{"-max-version", shimVersFlag},
2505 expectedVersion: expectedVersion,
2506 })
David Benjamin8b8c0062014-11-23 02:47:52 -05002507 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002508 }
2509 }
2510}
2511
David Benjaminaccb4542014-12-12 23:44:33 -05002512func addMinimumVersionTests() {
2513 for i, shimVers := range tlsVersions {
2514 // Assemble flags to disable all older versions on the shim.
2515 var flags []string
2516 for _, vers := range tlsVersions[:i] {
2517 flags = append(flags, vers.flag)
2518 }
2519
2520 for _, runnerVers := range tlsVersions {
2521 protocols := []protocol{tls}
2522 if runnerVers.hasDTLS && shimVers.hasDTLS {
2523 protocols = append(protocols, dtls)
2524 }
2525 for _, protocol := range protocols {
2526 suffix := shimVers.name + "-" + runnerVers.name
2527 if protocol == dtls {
2528 suffix += "-DTLS"
2529 }
2530 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
2531
David Benjaminaccb4542014-12-12 23:44:33 -05002532 var expectedVersion uint16
2533 var shouldFail bool
2534 var expectedError string
David Benjamin87909c02014-12-13 01:55:01 -05002535 var expectedLocalError string
David Benjaminaccb4542014-12-12 23:44:33 -05002536 if runnerVers.version >= shimVers.version {
2537 expectedVersion = runnerVers.version
2538 } else {
2539 shouldFail = true
2540 expectedError = ":UNSUPPORTED_PROTOCOL:"
David Benjamin87909c02014-12-13 01:55:01 -05002541 if runnerVers.version > VersionSSL30 {
2542 expectedLocalError = "remote error: protocol version not supported"
2543 } else {
2544 expectedLocalError = "remote error: handshake failure"
2545 }
David Benjaminaccb4542014-12-12 23:44:33 -05002546 }
2547
2548 testCases = append(testCases, testCase{
2549 protocol: protocol,
2550 testType: clientTest,
2551 name: "MinimumVersion-Client-" + suffix,
2552 config: Config{
2553 MaxVersion: runnerVers.version,
2554 },
David Benjamin87909c02014-12-13 01:55:01 -05002555 flags: flags,
2556 expectedVersion: expectedVersion,
2557 shouldFail: shouldFail,
2558 expectedError: expectedError,
2559 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002560 })
2561 testCases = append(testCases, testCase{
2562 protocol: protocol,
2563 testType: clientTest,
2564 name: "MinimumVersion-Client2-" + suffix,
2565 config: Config{
2566 MaxVersion: runnerVers.version,
2567 },
David Benjamin87909c02014-12-13 01:55:01 -05002568 flags: []string{"-min-version", shimVersFlag},
2569 expectedVersion: expectedVersion,
2570 shouldFail: shouldFail,
2571 expectedError: expectedError,
2572 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002573 })
2574
2575 testCases = append(testCases, testCase{
2576 protocol: protocol,
2577 testType: serverTest,
2578 name: "MinimumVersion-Server-" + suffix,
2579 config: Config{
2580 MaxVersion: runnerVers.version,
2581 },
David Benjamin87909c02014-12-13 01:55:01 -05002582 flags: flags,
2583 expectedVersion: expectedVersion,
2584 shouldFail: shouldFail,
2585 expectedError: expectedError,
2586 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002587 })
2588 testCases = append(testCases, testCase{
2589 protocol: protocol,
2590 testType: serverTest,
2591 name: "MinimumVersion-Server2-" + suffix,
2592 config: Config{
2593 MaxVersion: runnerVers.version,
2594 },
David Benjamin87909c02014-12-13 01:55:01 -05002595 flags: []string{"-min-version", shimVersFlag},
2596 expectedVersion: expectedVersion,
2597 shouldFail: shouldFail,
2598 expectedError: expectedError,
2599 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002600 })
2601 }
2602 }
2603 }
2604}
2605
David Benjamin5c24a1d2014-08-31 00:59:27 -04002606func addD5BugTests() {
2607 testCases = append(testCases, testCase{
2608 testType: serverTest,
2609 name: "D5Bug-NoQuirk-Reject",
2610 config: Config{
2611 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2612 Bugs: ProtocolBugs{
2613 SSL3RSAKeyExchange: true,
2614 },
2615 },
2616 shouldFail: true,
2617 expectedError: ":TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG:",
2618 })
2619 testCases = append(testCases, testCase{
2620 testType: serverTest,
2621 name: "D5Bug-Quirk-Normal",
2622 config: Config{
2623 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2624 },
2625 flags: []string{"-tls-d5-bug"},
2626 })
2627 testCases = append(testCases, testCase{
2628 testType: serverTest,
2629 name: "D5Bug-Quirk-Bug",
2630 config: Config{
2631 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2632 Bugs: ProtocolBugs{
2633 SSL3RSAKeyExchange: true,
2634 },
2635 },
2636 flags: []string{"-tls-d5-bug"},
2637 })
2638}
2639
David Benjamine78bfde2014-09-06 12:45:15 -04002640func addExtensionTests() {
2641 testCases = append(testCases, testCase{
2642 testType: clientTest,
2643 name: "DuplicateExtensionClient",
2644 config: Config{
2645 Bugs: ProtocolBugs{
2646 DuplicateExtension: true,
2647 },
2648 },
2649 shouldFail: true,
2650 expectedLocalError: "remote error: error decoding message",
2651 })
2652 testCases = append(testCases, testCase{
2653 testType: serverTest,
2654 name: "DuplicateExtensionServer",
2655 config: Config{
2656 Bugs: ProtocolBugs{
2657 DuplicateExtension: true,
2658 },
2659 },
2660 shouldFail: true,
2661 expectedLocalError: "remote error: error decoding message",
2662 })
2663 testCases = append(testCases, testCase{
2664 testType: clientTest,
2665 name: "ServerNameExtensionClient",
2666 config: Config{
2667 Bugs: ProtocolBugs{
2668 ExpectServerName: "example.com",
2669 },
2670 },
2671 flags: []string{"-host-name", "example.com"},
2672 })
2673 testCases = append(testCases, testCase{
2674 testType: clientTest,
David Benjamin5f237bc2015-02-11 17:14:15 -05002675 name: "ServerNameExtensionClientMismatch",
David Benjamine78bfde2014-09-06 12:45:15 -04002676 config: Config{
2677 Bugs: ProtocolBugs{
2678 ExpectServerName: "mismatch.com",
2679 },
2680 },
2681 flags: []string{"-host-name", "example.com"},
2682 shouldFail: true,
2683 expectedLocalError: "tls: unexpected server name",
2684 })
2685 testCases = append(testCases, testCase{
2686 testType: clientTest,
David Benjamin5f237bc2015-02-11 17:14:15 -05002687 name: "ServerNameExtensionClientMissing",
David Benjamine78bfde2014-09-06 12:45:15 -04002688 config: Config{
2689 Bugs: ProtocolBugs{
2690 ExpectServerName: "missing.com",
2691 },
2692 },
2693 shouldFail: true,
2694 expectedLocalError: "tls: unexpected server name",
2695 })
2696 testCases = append(testCases, testCase{
2697 testType: serverTest,
2698 name: "ServerNameExtensionServer",
2699 config: Config{
2700 ServerName: "example.com",
2701 },
2702 flags: []string{"-expect-server-name", "example.com"},
2703 resumeSession: true,
2704 })
David Benjaminae2888f2014-09-06 12:58:58 -04002705 testCases = append(testCases, testCase{
2706 testType: clientTest,
2707 name: "ALPNClient",
2708 config: Config{
2709 NextProtos: []string{"foo"},
2710 },
2711 flags: []string{
2712 "-advertise-alpn", "\x03foo\x03bar\x03baz",
2713 "-expect-alpn", "foo",
2714 },
David Benjaminfc7b0862014-09-06 13:21:53 -04002715 expectedNextProto: "foo",
2716 expectedNextProtoType: alpn,
2717 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04002718 })
2719 testCases = append(testCases, testCase{
2720 testType: serverTest,
2721 name: "ALPNServer",
2722 config: Config{
2723 NextProtos: []string{"foo", "bar", "baz"},
2724 },
2725 flags: []string{
2726 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2727 "-select-alpn", "foo",
2728 },
David Benjaminfc7b0862014-09-06 13:21:53 -04002729 expectedNextProto: "foo",
2730 expectedNextProtoType: alpn,
2731 resumeSession: true,
2732 })
2733 // Test that the server prefers ALPN over NPN.
2734 testCases = append(testCases, testCase{
2735 testType: serverTest,
2736 name: "ALPNServer-Preferred",
2737 config: Config{
2738 NextProtos: []string{"foo", "bar", "baz"},
2739 },
2740 flags: []string{
2741 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2742 "-select-alpn", "foo",
2743 "-advertise-npn", "\x03foo\x03bar\x03baz",
2744 },
2745 expectedNextProto: "foo",
2746 expectedNextProtoType: alpn,
2747 resumeSession: true,
2748 })
2749 testCases = append(testCases, testCase{
2750 testType: serverTest,
2751 name: "ALPNServer-Preferred-Swapped",
2752 config: Config{
2753 NextProtos: []string{"foo", "bar", "baz"},
2754 Bugs: ProtocolBugs{
2755 SwapNPNAndALPN: true,
2756 },
2757 },
2758 flags: []string{
2759 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2760 "-select-alpn", "foo",
2761 "-advertise-npn", "\x03foo\x03bar\x03baz",
2762 },
2763 expectedNextProto: "foo",
2764 expectedNextProtoType: alpn,
2765 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04002766 })
Adam Langley38311732014-10-16 19:04:35 -07002767 // Resume with a corrupt ticket.
2768 testCases = append(testCases, testCase{
2769 testType: serverTest,
2770 name: "CorruptTicket",
2771 config: Config{
2772 Bugs: ProtocolBugs{
2773 CorruptTicket: true,
2774 },
2775 },
Adam Langleyb0eef0a2015-06-02 10:47:39 -07002776 resumeSession: true,
2777 expectResumeRejected: true,
Adam Langley38311732014-10-16 19:04:35 -07002778 })
2779 // Resume with an oversized session id.
2780 testCases = append(testCases, testCase{
2781 testType: serverTest,
2782 name: "OversizedSessionId",
2783 config: Config{
2784 Bugs: ProtocolBugs{
2785 OversizedSessionId: true,
2786 },
2787 },
2788 resumeSession: true,
Adam Langley75712922014-10-10 16:23:43 -07002789 shouldFail: true,
Adam Langley38311732014-10-16 19:04:35 -07002790 expectedError: ":DECODE_ERROR:",
2791 })
David Benjaminca6c8262014-11-15 19:06:08 -05002792 // Basic DTLS-SRTP tests. Include fake profiles to ensure they
2793 // are ignored.
2794 testCases = append(testCases, testCase{
2795 protocol: dtls,
2796 name: "SRTP-Client",
2797 config: Config{
2798 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
2799 },
2800 flags: []string{
2801 "-srtp-profiles",
2802 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2803 },
2804 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2805 })
2806 testCases = append(testCases, testCase{
2807 protocol: dtls,
2808 testType: serverTest,
2809 name: "SRTP-Server",
2810 config: Config{
2811 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
2812 },
2813 flags: []string{
2814 "-srtp-profiles",
2815 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2816 },
2817 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2818 })
2819 // Test that the MKI is ignored.
2820 testCases = append(testCases, testCase{
2821 protocol: dtls,
2822 testType: serverTest,
2823 name: "SRTP-Server-IgnoreMKI",
2824 config: Config{
2825 SRTPProtectionProfiles: []uint16{SRTP_AES128_CM_HMAC_SHA1_80},
2826 Bugs: ProtocolBugs{
2827 SRTPMasterKeyIdentifer: "bogus",
2828 },
2829 },
2830 flags: []string{
2831 "-srtp-profiles",
2832 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2833 },
2834 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2835 })
2836 // Test that SRTP isn't negotiated on the server if there were
2837 // no matching profiles.
2838 testCases = append(testCases, testCase{
2839 protocol: dtls,
2840 testType: serverTest,
2841 name: "SRTP-Server-NoMatch",
2842 config: Config{
2843 SRTPProtectionProfiles: []uint16{100, 101, 102},
2844 },
2845 flags: []string{
2846 "-srtp-profiles",
2847 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2848 },
2849 expectedSRTPProtectionProfile: 0,
2850 })
2851 // Test that the server returning an invalid SRTP profile is
2852 // flagged as an error by the client.
2853 testCases = append(testCases, testCase{
2854 protocol: dtls,
2855 name: "SRTP-Client-NoMatch",
2856 config: Config{
2857 Bugs: ProtocolBugs{
2858 SendSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_32,
2859 },
2860 },
2861 flags: []string{
2862 "-srtp-profiles",
2863 "SRTP_AES128_CM_SHA1_80",
2864 },
2865 shouldFail: true,
2866 expectedError: ":BAD_SRTP_PROTECTION_PROFILE_LIST:",
2867 })
David Benjamin61f95272014-11-25 01:55:35 -05002868 // Test OCSP stapling and SCT list.
2869 testCases = append(testCases, testCase{
2870 name: "OCSPStapling",
2871 flags: []string{
2872 "-enable-ocsp-stapling",
2873 "-expect-ocsp-response",
2874 base64.StdEncoding.EncodeToString(testOCSPResponse),
2875 },
2876 })
2877 testCases = append(testCases, testCase{
2878 name: "SignedCertificateTimestampList",
2879 flags: []string{
2880 "-enable-signed-cert-timestamps",
2881 "-expect-signed-cert-timestamps",
2882 base64.StdEncoding.EncodeToString(testSCTList),
2883 },
2884 })
David Benjamine78bfde2014-09-06 12:45:15 -04002885}
2886
David Benjamin01fe8202014-09-24 15:21:44 -04002887func addResumptionVersionTests() {
David Benjamin01fe8202014-09-24 15:21:44 -04002888 for _, sessionVers := range tlsVersions {
David Benjamin01fe8202014-09-24 15:21:44 -04002889 for _, resumeVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05002890 protocols := []protocol{tls}
2891 if sessionVers.hasDTLS && resumeVers.hasDTLS {
2892 protocols = append(protocols, dtls)
David Benjaminbdf5e722014-11-11 00:52:15 -05002893 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002894 for _, protocol := range protocols {
2895 suffix := "-" + sessionVers.name + "-" + resumeVers.name
2896 if protocol == dtls {
2897 suffix += "-DTLS"
2898 }
2899
David Benjaminece3de92015-03-16 18:02:20 -04002900 if sessionVers.version == resumeVers.version {
2901 testCases = append(testCases, testCase{
2902 protocol: protocol,
2903 name: "Resume-Client" + suffix,
2904 resumeSession: true,
2905 config: Config{
2906 MaxVersion: sessionVers.version,
2907 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
David Benjamin8b8c0062014-11-23 02:47:52 -05002908 },
David Benjaminece3de92015-03-16 18:02:20 -04002909 expectedVersion: sessionVers.version,
2910 expectedResumeVersion: resumeVers.version,
2911 })
2912 } else {
2913 testCases = append(testCases, testCase{
2914 protocol: protocol,
2915 name: "Resume-Client-Mismatch" + suffix,
2916 resumeSession: true,
2917 config: Config{
2918 MaxVersion: sessionVers.version,
2919 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
David Benjamin8b8c0062014-11-23 02:47:52 -05002920 },
David Benjaminece3de92015-03-16 18:02:20 -04002921 expectedVersion: sessionVers.version,
2922 resumeConfig: &Config{
2923 MaxVersion: resumeVers.version,
2924 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2925 Bugs: ProtocolBugs{
2926 AllowSessionVersionMismatch: true,
2927 },
2928 },
2929 expectedResumeVersion: resumeVers.version,
2930 shouldFail: true,
2931 expectedError: ":OLD_SESSION_VERSION_NOT_RETURNED:",
2932 })
2933 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002934
2935 testCases = append(testCases, testCase{
2936 protocol: protocol,
2937 name: "Resume-Client-NoResume" + suffix,
David Benjamin8b8c0062014-11-23 02:47:52 -05002938 resumeSession: true,
2939 config: Config{
2940 MaxVersion: sessionVers.version,
2941 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2942 },
2943 expectedVersion: sessionVers.version,
2944 resumeConfig: &Config{
2945 MaxVersion: resumeVers.version,
2946 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2947 },
2948 newSessionsOnResume: true,
Adam Langleyb0eef0a2015-06-02 10:47:39 -07002949 expectResumeRejected: true,
David Benjamin8b8c0062014-11-23 02:47:52 -05002950 expectedResumeVersion: resumeVers.version,
2951 })
2952
David Benjamin8b8c0062014-11-23 02:47:52 -05002953 testCases = append(testCases, testCase{
2954 protocol: protocol,
2955 testType: serverTest,
2956 name: "Resume-Server" + suffix,
David Benjamin8b8c0062014-11-23 02:47:52 -05002957 resumeSession: true,
2958 config: Config{
2959 MaxVersion: sessionVers.version,
2960 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2961 },
Adam Langleyb0eef0a2015-06-02 10:47:39 -07002962 expectedVersion: sessionVers.version,
2963 expectResumeRejected: sessionVers.version != resumeVers.version,
David Benjamin8b8c0062014-11-23 02:47:52 -05002964 resumeConfig: &Config{
2965 MaxVersion: resumeVers.version,
2966 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2967 },
2968 expectedResumeVersion: resumeVers.version,
2969 })
2970 }
David Benjamin01fe8202014-09-24 15:21:44 -04002971 }
2972 }
David Benjaminece3de92015-03-16 18:02:20 -04002973
2974 testCases = append(testCases, testCase{
2975 name: "Resume-Client-CipherMismatch",
2976 resumeSession: true,
2977 config: Config{
2978 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2979 },
2980 resumeConfig: &Config{
2981 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2982 Bugs: ProtocolBugs{
2983 SendCipherSuite: TLS_RSA_WITH_AES_128_CBC_SHA,
2984 },
2985 },
2986 shouldFail: true,
2987 expectedError: ":OLD_SESSION_CIPHER_NOT_RETURNED:",
2988 })
David Benjamin01fe8202014-09-24 15:21:44 -04002989}
2990
Adam Langley2ae77d22014-10-28 17:29:33 -07002991func addRenegotiationTests() {
David Benjamin44d3eed2015-05-21 01:29:55 -04002992 // Servers cannot renegotiate.
David Benjaminb16346b2015-04-08 19:16:58 -04002993 testCases = append(testCases, testCase{
2994 testType: serverTest,
David Benjamin44d3eed2015-05-21 01:29:55 -04002995 name: "Renegotiate-Server-Forbidden",
David Benjaminb16346b2015-04-08 19:16:58 -04002996 renegotiate: true,
2997 flags: []string{"-reject-peer-renegotiations"},
2998 shouldFail: true,
2999 expectedError: ":NO_RENEGOTIATION:",
3000 expectedLocalError: "remote error: no renegotiation",
3001 })
Adam Langley2ae77d22014-10-28 17:29:33 -07003002 // TODO(agl): test the renegotiation info SCSV.
Adam Langleycf2d4f42014-10-28 19:06:14 -07003003 testCases = append(testCases, testCase{
David Benjamin4b27d9f2015-05-12 22:42:52 -04003004 name: "Renegotiate-Client",
David Benjamincdea40c2015-03-19 14:09:43 -04003005 config: Config{
3006 Bugs: ProtocolBugs{
David Benjamin4b27d9f2015-05-12 22:42:52 -04003007 FailIfResumeOnRenego: true,
David Benjamincdea40c2015-03-19 14:09:43 -04003008 },
3009 },
3010 renegotiate: true,
3011 })
3012 testCases = append(testCases, testCase{
Adam Langleycf2d4f42014-10-28 19:06:14 -07003013 name: "Renegotiate-Client-EmptyExt",
3014 renegotiate: true,
3015 config: Config{
3016 Bugs: ProtocolBugs{
3017 EmptyRenegotiationInfo: true,
3018 },
3019 },
3020 shouldFail: true,
3021 expectedError: ":RENEGOTIATION_MISMATCH:",
3022 })
3023 testCases = append(testCases, testCase{
3024 name: "Renegotiate-Client-BadExt",
3025 renegotiate: true,
3026 config: Config{
3027 Bugs: ProtocolBugs{
3028 BadRenegotiationInfo: true,
3029 },
3030 },
3031 shouldFail: true,
3032 expectedError: ":RENEGOTIATION_MISMATCH:",
3033 })
3034 testCases = append(testCases, testCase{
David Benjamincff0b902015-05-15 23:09:47 -04003035 name: "Renegotiate-Client-NoExt",
3036 renegotiate: true,
3037 config: Config{
3038 Bugs: ProtocolBugs{
3039 NoRenegotiationInfo: true,
3040 },
3041 },
3042 shouldFail: true,
3043 expectedError: ":UNSAFE_LEGACY_RENEGOTIATION_DISABLED:",
3044 flags: []string{"-no-legacy-server-connect"},
3045 })
3046 testCases = append(testCases, testCase{
3047 name: "Renegotiate-Client-NoExt-Allowed",
3048 renegotiate: true,
3049 config: Config{
3050 Bugs: ProtocolBugs{
3051 NoRenegotiationInfo: true,
3052 },
3053 },
3054 })
3055 testCases = append(testCases, testCase{
Adam Langleycf2d4f42014-10-28 19:06:14 -07003056 name: "Renegotiate-Client-SwitchCiphers",
3057 renegotiate: true,
3058 config: Config{
3059 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
3060 },
3061 renegotiateCiphers: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
3062 })
3063 testCases = append(testCases, testCase{
3064 name: "Renegotiate-Client-SwitchCiphers2",
3065 renegotiate: true,
3066 config: Config{
3067 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
3068 },
3069 renegotiateCiphers: []uint16{TLS_RSA_WITH_RC4_128_SHA},
3070 })
David Benjaminc44b1df2014-11-23 12:11:01 -05003071 testCases = append(testCases, testCase{
David Benjaminb16346b2015-04-08 19:16:58 -04003072 name: "Renegotiate-Client-Forbidden",
3073 renegotiate: true,
3074 flags: []string{"-reject-peer-renegotiations"},
3075 shouldFail: true,
3076 expectedError: ":NO_RENEGOTIATION:",
3077 expectedLocalError: "remote error: no renegotiation",
3078 })
3079 testCases = append(testCases, testCase{
David Benjaminc44b1df2014-11-23 12:11:01 -05003080 name: "Renegotiate-SameClientVersion",
3081 renegotiate: true,
3082 config: Config{
3083 MaxVersion: VersionTLS10,
3084 Bugs: ProtocolBugs{
3085 RequireSameRenegoClientVersion: true,
3086 },
3087 },
3088 })
Adam Langley2ae77d22014-10-28 17:29:33 -07003089}
3090
David Benjamin5e961c12014-11-07 01:48:35 -05003091func addDTLSReplayTests() {
3092 // Test that sequence number replays are detected.
3093 testCases = append(testCases, testCase{
3094 protocol: dtls,
3095 name: "DTLS-Replay",
3096 replayWrites: true,
3097 })
3098
3099 // Test the outgoing sequence number skipping by values larger
3100 // than the retransmit window.
3101 testCases = append(testCases, testCase{
3102 protocol: dtls,
3103 name: "DTLS-Replay-LargeGaps",
3104 config: Config{
3105 Bugs: ProtocolBugs{
3106 SequenceNumberIncrement: 127,
3107 },
3108 },
3109 replayWrites: true,
3110 })
3111}
3112
Feng Lu41aa3252014-11-21 22:47:56 -08003113func addFastRadioPaddingTests() {
David Benjamin1e29a6b2014-12-10 02:27:24 -05003114 testCases = append(testCases, testCase{
3115 protocol: tls,
3116 name: "FastRadio-Padding",
Feng Lu41aa3252014-11-21 22:47:56 -08003117 config: Config{
3118 Bugs: ProtocolBugs{
3119 RequireFastradioPadding: true,
3120 },
3121 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05003122 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08003123 })
David Benjamin1e29a6b2014-12-10 02:27:24 -05003124 testCases = append(testCases, testCase{
3125 protocol: dtls,
David Benjamin5f237bc2015-02-11 17:14:15 -05003126 name: "FastRadio-Padding-DTLS",
Feng Lu41aa3252014-11-21 22:47:56 -08003127 config: Config{
3128 Bugs: ProtocolBugs{
3129 RequireFastradioPadding: true,
3130 },
3131 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05003132 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08003133 })
3134}
3135
David Benjamin000800a2014-11-14 01:43:59 -05003136var testHashes = []struct {
3137 name string
3138 id uint8
3139}{
3140 {"SHA1", hashSHA1},
3141 {"SHA224", hashSHA224},
3142 {"SHA256", hashSHA256},
3143 {"SHA384", hashSHA384},
3144 {"SHA512", hashSHA512},
3145}
3146
3147func addSigningHashTests() {
3148 // Make sure each hash works. Include some fake hashes in the list and
3149 // ensure they're ignored.
3150 for _, hash := range testHashes {
3151 testCases = append(testCases, testCase{
3152 name: "SigningHash-ClientAuth-" + hash.name,
3153 config: Config{
3154 ClientAuth: RequireAnyClientCert,
3155 SignatureAndHashes: []signatureAndHash{
3156 {signatureRSA, 42},
3157 {signatureRSA, hash.id},
3158 {signatureRSA, 255},
3159 },
3160 },
3161 flags: []string{
3162 "-cert-file", rsaCertificateFile,
3163 "-key-file", rsaKeyFile,
3164 },
3165 })
3166
3167 testCases = append(testCases, testCase{
3168 testType: serverTest,
3169 name: "SigningHash-ServerKeyExchange-Sign-" + hash.name,
3170 config: Config{
3171 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
3172 SignatureAndHashes: []signatureAndHash{
3173 {signatureRSA, 42},
3174 {signatureRSA, hash.id},
3175 {signatureRSA, 255},
3176 },
3177 },
3178 })
3179 }
3180
3181 // Test that hash resolution takes the signature type into account.
3182 testCases = append(testCases, testCase{
3183 name: "SigningHash-ClientAuth-SignatureType",
3184 config: Config{
3185 ClientAuth: RequireAnyClientCert,
3186 SignatureAndHashes: []signatureAndHash{
3187 {signatureECDSA, hashSHA512},
3188 {signatureRSA, hashSHA384},
3189 {signatureECDSA, hashSHA1},
3190 },
3191 },
3192 flags: []string{
3193 "-cert-file", rsaCertificateFile,
3194 "-key-file", rsaKeyFile,
3195 },
3196 })
3197
3198 testCases = append(testCases, testCase{
3199 testType: serverTest,
3200 name: "SigningHash-ServerKeyExchange-SignatureType",
3201 config: Config{
3202 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
3203 SignatureAndHashes: []signatureAndHash{
3204 {signatureECDSA, hashSHA512},
3205 {signatureRSA, hashSHA384},
3206 {signatureECDSA, hashSHA1},
3207 },
3208 },
3209 })
3210
3211 // Test that, if the list is missing, the peer falls back to SHA-1.
3212 testCases = append(testCases, testCase{
3213 name: "SigningHash-ClientAuth-Fallback",
3214 config: Config{
3215 ClientAuth: RequireAnyClientCert,
3216 SignatureAndHashes: []signatureAndHash{
3217 {signatureRSA, hashSHA1},
3218 },
3219 Bugs: ProtocolBugs{
3220 NoSignatureAndHashes: true,
3221 },
3222 },
3223 flags: []string{
3224 "-cert-file", rsaCertificateFile,
3225 "-key-file", rsaKeyFile,
3226 },
3227 })
3228
3229 testCases = append(testCases, testCase{
3230 testType: serverTest,
3231 name: "SigningHash-ServerKeyExchange-Fallback",
3232 config: Config{
3233 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
3234 SignatureAndHashes: []signatureAndHash{
3235 {signatureRSA, hashSHA1},
3236 },
3237 Bugs: ProtocolBugs{
3238 NoSignatureAndHashes: true,
3239 },
3240 },
3241 })
David Benjamin72dc7832015-03-16 17:49:43 -04003242
3243 // Test that hash preferences are enforced. BoringSSL defaults to
3244 // rejecting MD5 signatures.
3245 testCases = append(testCases, testCase{
3246 testType: serverTest,
3247 name: "SigningHash-ClientAuth-Enforced",
3248 config: Config{
3249 Certificates: []Certificate{rsaCertificate},
3250 SignatureAndHashes: []signatureAndHash{
3251 {signatureRSA, hashMD5},
3252 // Advertise SHA-1 so the handshake will
3253 // proceed, but the shim's preferences will be
3254 // ignored in CertificateVerify generation, so
3255 // MD5 will be chosen.
3256 {signatureRSA, hashSHA1},
3257 },
3258 Bugs: ProtocolBugs{
3259 IgnorePeerSignatureAlgorithmPreferences: true,
3260 },
3261 },
3262 flags: []string{"-require-any-client-certificate"},
3263 shouldFail: true,
3264 expectedError: ":WRONG_SIGNATURE_TYPE:",
3265 })
3266
3267 testCases = append(testCases, testCase{
3268 name: "SigningHash-ServerKeyExchange-Enforced",
3269 config: Config{
3270 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
3271 SignatureAndHashes: []signatureAndHash{
3272 {signatureRSA, hashMD5},
3273 },
3274 Bugs: ProtocolBugs{
3275 IgnorePeerSignatureAlgorithmPreferences: true,
3276 },
3277 },
3278 shouldFail: true,
3279 expectedError: ":WRONG_SIGNATURE_TYPE:",
3280 })
David Benjamin000800a2014-11-14 01:43:59 -05003281}
3282
David Benjamin83f90402015-01-27 01:09:43 -05003283// timeouts is the retransmit schedule for BoringSSL. It doubles and
3284// caps at 60 seconds. On the 13th timeout, it gives up.
3285var timeouts = []time.Duration{
3286 1 * time.Second,
3287 2 * time.Second,
3288 4 * time.Second,
3289 8 * time.Second,
3290 16 * time.Second,
3291 32 * time.Second,
3292 60 * time.Second,
3293 60 * time.Second,
3294 60 * time.Second,
3295 60 * time.Second,
3296 60 * time.Second,
3297 60 * time.Second,
3298 60 * time.Second,
3299}
3300
3301func addDTLSRetransmitTests() {
3302 // Test that this is indeed the timeout schedule. Stress all
3303 // four patterns of handshake.
3304 for i := 1; i < len(timeouts); i++ {
3305 number := strconv.Itoa(i)
3306 testCases = append(testCases, testCase{
3307 protocol: dtls,
3308 name: "DTLS-Retransmit-Client-" + number,
3309 config: Config{
3310 Bugs: ProtocolBugs{
3311 TimeoutSchedule: timeouts[:i],
3312 },
3313 },
3314 resumeSession: true,
3315 flags: []string{"-async"},
3316 })
3317 testCases = append(testCases, testCase{
3318 protocol: dtls,
3319 testType: serverTest,
3320 name: "DTLS-Retransmit-Server-" + number,
3321 config: Config{
3322 Bugs: ProtocolBugs{
3323 TimeoutSchedule: timeouts[:i],
3324 },
3325 },
3326 resumeSession: true,
3327 flags: []string{"-async"},
3328 })
3329 }
3330
3331 // Test that exceeding the timeout schedule hits a read
3332 // timeout.
3333 testCases = append(testCases, testCase{
3334 protocol: dtls,
3335 name: "DTLS-Retransmit-Timeout",
3336 config: Config{
3337 Bugs: ProtocolBugs{
3338 TimeoutSchedule: timeouts,
3339 },
3340 },
3341 resumeSession: true,
3342 flags: []string{"-async"},
3343 shouldFail: true,
3344 expectedError: ":READ_TIMEOUT_EXPIRED:",
3345 })
3346
3347 // Test that timeout handling has a fudge factor, due to API
3348 // problems.
3349 testCases = append(testCases, testCase{
3350 protocol: dtls,
3351 name: "DTLS-Retransmit-Fudge",
3352 config: Config{
3353 Bugs: ProtocolBugs{
3354 TimeoutSchedule: []time.Duration{
3355 timeouts[0] - 10*time.Millisecond,
3356 },
3357 },
3358 },
3359 resumeSession: true,
3360 flags: []string{"-async"},
3361 })
David Benjamin7eaab4c2015-03-02 19:01:16 -05003362
3363 // Test that the final Finished retransmitting isn't
3364 // duplicated if the peer badly fragments everything.
3365 testCases = append(testCases, testCase{
3366 testType: serverTest,
3367 protocol: dtls,
3368 name: "DTLS-Retransmit-Fragmented",
3369 config: Config{
3370 Bugs: ProtocolBugs{
3371 TimeoutSchedule: []time.Duration{timeouts[0]},
3372 MaxHandshakeRecordLength: 2,
3373 },
3374 },
3375 flags: []string{"-async"},
3376 })
David Benjamin83f90402015-01-27 01:09:43 -05003377}
3378
David Benjaminc565ebb2015-04-03 04:06:36 -04003379func addExportKeyingMaterialTests() {
3380 for _, vers := range tlsVersions {
3381 if vers.version == VersionSSL30 {
3382 continue
3383 }
3384 testCases = append(testCases, testCase{
3385 name: "ExportKeyingMaterial-" + vers.name,
3386 config: Config{
3387 MaxVersion: vers.version,
3388 },
3389 exportKeyingMaterial: 1024,
3390 exportLabel: "label",
3391 exportContext: "context",
3392 useExportContext: true,
3393 })
3394 testCases = append(testCases, testCase{
3395 name: "ExportKeyingMaterial-NoContext-" + vers.name,
3396 config: Config{
3397 MaxVersion: vers.version,
3398 },
3399 exportKeyingMaterial: 1024,
3400 })
3401 testCases = append(testCases, testCase{
3402 name: "ExportKeyingMaterial-EmptyContext-" + vers.name,
3403 config: Config{
3404 MaxVersion: vers.version,
3405 },
3406 exportKeyingMaterial: 1024,
3407 useExportContext: true,
3408 })
3409 testCases = append(testCases, testCase{
3410 name: "ExportKeyingMaterial-Small-" + vers.name,
3411 config: Config{
3412 MaxVersion: vers.version,
3413 },
3414 exportKeyingMaterial: 1,
3415 exportLabel: "label",
3416 exportContext: "context",
3417 useExportContext: true,
3418 })
3419 }
3420 testCases = append(testCases, testCase{
3421 name: "ExportKeyingMaterial-SSL3",
3422 config: Config{
3423 MaxVersion: VersionSSL30,
3424 },
3425 exportKeyingMaterial: 1024,
3426 exportLabel: "label",
3427 exportContext: "context",
3428 useExportContext: true,
3429 shouldFail: true,
3430 expectedError: "failed to export keying material",
3431 })
3432}
3433
Adam Langleyaf0e32c2015-06-03 09:57:23 -07003434func addTLSUniqueTests() {
3435 for _, isClient := range []bool{false, true} {
3436 for _, isResumption := range []bool{false, true} {
3437 for _, hasEMS := range []bool{false, true} {
3438 var suffix string
3439 if isResumption {
3440 suffix = "Resume-"
3441 } else {
3442 suffix = "Full-"
3443 }
3444
3445 if hasEMS {
3446 suffix += "EMS-"
3447 } else {
3448 suffix += "NoEMS-"
3449 }
3450
3451 if isClient {
3452 suffix += "Client"
3453 } else {
3454 suffix += "Server"
3455 }
3456
3457 test := testCase{
3458 name: "TLSUnique-" + suffix,
3459 testTLSUnique: true,
3460 config: Config{
3461 Bugs: ProtocolBugs{
3462 NoExtendedMasterSecret: !hasEMS,
3463 },
3464 },
3465 }
3466
3467 if isResumption {
3468 test.resumeSession = true
3469 test.resumeConfig = &Config{
3470 Bugs: ProtocolBugs{
3471 NoExtendedMasterSecret: !hasEMS,
3472 },
3473 }
3474 }
3475
3476 if isResumption && !hasEMS {
3477 test.shouldFail = true
3478 test.expectedError = "failed to get tls-unique"
3479 }
3480
3481 testCases = append(testCases, test)
3482 }
3483 }
3484 }
3485}
3486
David Benjamin884fdf12014-08-02 15:28:23 -04003487func worker(statusChan chan statusMsg, c chan *testCase, buildDir string, wg *sync.WaitGroup) {
Adam Langley95c29f32014-06-20 12:00:00 -07003488 defer wg.Done()
3489
3490 for test := range c {
Adam Langley69a01602014-11-17 17:26:55 -08003491 var err error
3492
3493 if *mallocTest < 0 {
3494 statusChan <- statusMsg{test: test, started: true}
3495 err = runTest(test, buildDir, -1)
3496 } else {
3497 for mallocNumToFail := int64(*mallocTest); ; mallocNumToFail++ {
3498 statusChan <- statusMsg{test: test, started: true}
3499 if err = runTest(test, buildDir, mallocNumToFail); err != errMoreMallocs {
3500 if err != nil {
3501 fmt.Printf("\n\nmalloc test failed at %d: %s\n", mallocNumToFail, err)
3502 }
3503 break
3504 }
3505 }
3506 }
Adam Langley95c29f32014-06-20 12:00:00 -07003507 statusChan <- statusMsg{test: test, err: err}
3508 }
3509}
3510
3511type statusMsg struct {
3512 test *testCase
3513 started bool
3514 err error
3515}
3516
David Benjamin5f237bc2015-02-11 17:14:15 -05003517func statusPrinter(doneChan chan *testOutput, statusChan chan statusMsg, total int) {
Adam Langley95c29f32014-06-20 12:00:00 -07003518 var started, done, failed, lineLen int
Adam Langley95c29f32014-06-20 12:00:00 -07003519
David Benjamin5f237bc2015-02-11 17:14:15 -05003520 testOutput := newTestOutput()
Adam Langley95c29f32014-06-20 12:00:00 -07003521 for msg := range statusChan {
David Benjamin5f237bc2015-02-11 17:14:15 -05003522 if !*pipe {
3523 // Erase the previous status line.
David Benjamin87c8a642015-02-21 01:54:29 -05003524 var erase string
3525 for i := 0; i < lineLen; i++ {
3526 erase += "\b \b"
3527 }
3528 fmt.Print(erase)
David Benjamin5f237bc2015-02-11 17:14:15 -05003529 }
3530
Adam Langley95c29f32014-06-20 12:00:00 -07003531 if msg.started {
3532 started++
3533 } else {
3534 done++
David Benjamin5f237bc2015-02-11 17:14:15 -05003535
3536 if msg.err != nil {
3537 fmt.Printf("FAILED (%s)\n%s\n", msg.test.name, msg.err)
3538 failed++
3539 testOutput.addResult(msg.test.name, "FAIL")
3540 } else {
3541 if *pipe {
3542 // Print each test instead of a status line.
3543 fmt.Printf("PASSED (%s)\n", msg.test.name)
3544 }
3545 testOutput.addResult(msg.test.name, "PASS")
3546 }
Adam Langley95c29f32014-06-20 12:00:00 -07003547 }
3548
David Benjamin5f237bc2015-02-11 17:14:15 -05003549 if !*pipe {
3550 // Print a new status line.
3551 line := fmt.Sprintf("%d/%d/%d/%d", failed, done, started, total)
3552 lineLen = len(line)
3553 os.Stdout.WriteString(line)
Adam Langley95c29f32014-06-20 12:00:00 -07003554 }
Adam Langley95c29f32014-06-20 12:00:00 -07003555 }
David Benjamin5f237bc2015-02-11 17:14:15 -05003556
3557 doneChan <- testOutput
Adam Langley95c29f32014-06-20 12:00:00 -07003558}
3559
3560func main() {
3561 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 -04003562 var flagNumWorkers *int = flag.Int("num-workers", runtime.NumCPU(), "The number of workers to run in parallel.")
David Benjamin884fdf12014-08-02 15:28:23 -04003563 var flagBuildDir *string = flag.String("build-dir", "../../../build", "The build directory to run the shim from.")
Adam Langley95c29f32014-06-20 12:00:00 -07003564
3565 flag.Parse()
3566
3567 addCipherSuiteTests()
3568 addBadECDSASignatureTests()
Adam Langley80842bd2014-06-20 12:00:00 -07003569 addCBCPaddingTests()
Kenny Root7fdeaf12014-08-05 15:23:37 -07003570 addCBCSplittingTests()
David Benjamin636293b2014-07-08 17:59:18 -04003571 addClientAuthTests()
Adam Langley524e7172015-02-20 16:04:00 -08003572 addDDoSCallbackTests()
David Benjamin7e2e6cf2014-08-07 17:44:24 -04003573 addVersionNegotiationTests()
David Benjaminaccb4542014-12-12 23:44:33 -05003574 addMinimumVersionTests()
David Benjamin5c24a1d2014-08-31 00:59:27 -04003575 addD5BugTests()
David Benjamine78bfde2014-09-06 12:45:15 -04003576 addExtensionTests()
David Benjamin01fe8202014-09-24 15:21:44 -04003577 addResumptionVersionTests()
Adam Langley75712922014-10-10 16:23:43 -07003578 addExtendedMasterSecretTests()
Adam Langley2ae77d22014-10-28 17:29:33 -07003579 addRenegotiationTests()
David Benjamin5e961c12014-11-07 01:48:35 -05003580 addDTLSReplayTests()
David Benjamin000800a2014-11-14 01:43:59 -05003581 addSigningHashTests()
Feng Lu41aa3252014-11-21 22:47:56 -08003582 addFastRadioPaddingTests()
David Benjamin83f90402015-01-27 01:09:43 -05003583 addDTLSRetransmitTests()
David Benjaminc565ebb2015-04-03 04:06:36 -04003584 addExportKeyingMaterialTests()
Adam Langleyaf0e32c2015-06-03 09:57:23 -07003585 addTLSUniqueTests()
David Benjamin43ec06f2014-08-05 02:28:57 -04003586 for _, async := range []bool{false, true} {
3587 for _, splitHandshake := range []bool{false, true} {
David Benjamin6fd297b2014-08-11 18:43:38 -04003588 for _, protocol := range []protocol{tls, dtls} {
3589 addStateMachineCoverageTests(async, splitHandshake, protocol)
3590 }
David Benjamin43ec06f2014-08-05 02:28:57 -04003591 }
3592 }
Adam Langley95c29f32014-06-20 12:00:00 -07003593
3594 var wg sync.WaitGroup
3595
David Benjamin2bc8e6f2014-08-02 15:22:37 -04003596 numWorkers := *flagNumWorkers
Adam Langley95c29f32014-06-20 12:00:00 -07003597
3598 statusChan := make(chan statusMsg, numWorkers)
3599 testChan := make(chan *testCase, numWorkers)
David Benjamin5f237bc2015-02-11 17:14:15 -05003600 doneChan := make(chan *testOutput)
Adam Langley95c29f32014-06-20 12:00:00 -07003601
David Benjamin025b3d32014-07-01 19:53:04 -04003602 go statusPrinter(doneChan, statusChan, len(testCases))
Adam Langley95c29f32014-06-20 12:00:00 -07003603
3604 for i := 0; i < numWorkers; i++ {
3605 wg.Add(1)
David Benjamin884fdf12014-08-02 15:28:23 -04003606 go worker(statusChan, testChan, *flagBuildDir, &wg)
Adam Langley95c29f32014-06-20 12:00:00 -07003607 }
3608
David Benjamin025b3d32014-07-01 19:53:04 -04003609 for i := range testCases {
3610 if len(*flagTest) == 0 || *flagTest == testCases[i].name {
3611 testChan <- &testCases[i]
Adam Langley95c29f32014-06-20 12:00:00 -07003612 }
3613 }
3614
3615 close(testChan)
3616 wg.Wait()
3617 close(statusChan)
David Benjamin5f237bc2015-02-11 17:14:15 -05003618 testOutput := <-doneChan
Adam Langley95c29f32014-06-20 12:00:00 -07003619
3620 fmt.Printf("\n")
David Benjamin5f237bc2015-02-11 17:14:15 -05003621
3622 if *jsonOutput != "" {
3623 if err := testOutput.writeTo(*jsonOutput); err != nil {
3624 fmt.Fprintf(os.Stderr, "Error: %s\n", err)
3625 }
3626 }
David Benjamin2ab7a862015-04-04 17:02:18 -04003627
3628 if !testOutput.allPassed {
3629 os.Exit(1)
3630 }
Adam Langley95c29f32014-06-20 12:00:00 -07003631}