blob: ec2fede9ee6a0b637270419d8322bcf2bfce7c34 [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 Langley95c29f32014-06-20 12:00:00 -070014 "net"
15 "os"
16 "os/exec"
David Benjamin884fdf12014-08-02 15:28:23 -040017 "path"
David Benjamin2bc8e6f2014-08-02 15:22:37 -040018 "runtime"
Adam Langley69a01602014-11-17 17:26:55 -080019 "strconv"
Adam Langley95c29f32014-06-20 12:00:00 -070020 "strings"
21 "sync"
22 "syscall"
David Benjamin83f90402015-01-27 01:09:43 -050023 "time"
Adam Langley95c29f32014-06-20 12:00:00 -070024)
25
Adam Langley69a01602014-11-17 17:26:55 -080026var (
David Benjamin5f237bc2015-02-11 17:14:15 -050027 useValgrind = flag.Bool("valgrind", false, "If true, run code under valgrind")
28 useGDB = flag.Bool("gdb", false, "If true, run BoringSSL code under gdb")
29 flagDebug = flag.Bool("debug", false, "Hexdump the contents of the connection")
30 mallocTest = flag.Int64("malloc-test", -1, "If non-negative, run each test with each malloc in turn failing from the given number onwards.")
31 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.")
32 jsonOutput = flag.String("json-output", "", "The file to output JSON results to.")
33 pipe = flag.Bool("pipe", false, "If true, print status output suitable for piping into another program.")
Adam Langley69a01602014-11-17 17:26:55 -080034)
Adam Langley95c29f32014-06-20 12:00:00 -070035
David Benjamin025b3d32014-07-01 19:53:04 -040036const (
37 rsaCertificateFile = "cert.pem"
38 ecdsaCertificateFile = "ecdsa_cert.pem"
39)
40
41const (
David Benjamina08e49d2014-08-24 01:46:07 -040042 rsaKeyFile = "key.pem"
43 ecdsaKeyFile = "ecdsa_key.pem"
44 channelIDKeyFile = "channel_id_key.pem"
David Benjamin025b3d32014-07-01 19:53:04 -040045)
46
Adam Langley95c29f32014-06-20 12:00:00 -070047var rsaCertificate, ecdsaCertificate Certificate
David Benjamina08e49d2014-08-24 01:46:07 -040048var channelIDKey *ecdsa.PrivateKey
49var channelIDBytes []byte
Adam Langley95c29f32014-06-20 12:00:00 -070050
David Benjamin61f95272014-11-25 01:55:35 -050051var testOCSPResponse = []byte{1, 2, 3, 4}
52var testSCTList = []byte{5, 6, 7, 8}
53
Adam Langley95c29f32014-06-20 12:00:00 -070054func initCertificates() {
55 var err error
David Benjamin025b3d32014-07-01 19:53:04 -040056 rsaCertificate, err = LoadX509KeyPair(rsaCertificateFile, rsaKeyFile)
Adam Langley95c29f32014-06-20 12:00:00 -070057 if err != nil {
58 panic(err)
59 }
David Benjamin61f95272014-11-25 01:55:35 -050060 rsaCertificate.OCSPStaple = testOCSPResponse
61 rsaCertificate.SignedCertificateTimestampList = testSCTList
Adam Langley95c29f32014-06-20 12:00:00 -070062
David Benjamin025b3d32014-07-01 19:53:04 -040063 ecdsaCertificate, err = LoadX509KeyPair(ecdsaCertificateFile, ecdsaKeyFile)
Adam Langley95c29f32014-06-20 12:00:00 -070064 if err != nil {
65 panic(err)
66 }
David Benjamin61f95272014-11-25 01:55:35 -050067 ecdsaCertificate.OCSPStaple = testOCSPResponse
68 ecdsaCertificate.SignedCertificateTimestampList = testSCTList
David Benjamina08e49d2014-08-24 01:46:07 -040069
70 channelIDPEMBlock, err := ioutil.ReadFile(channelIDKeyFile)
71 if err != nil {
72 panic(err)
73 }
74 channelIDDERBlock, _ := pem.Decode(channelIDPEMBlock)
75 if channelIDDERBlock.Type != "EC PRIVATE KEY" {
76 panic("bad key type")
77 }
78 channelIDKey, err = x509.ParseECPrivateKey(channelIDDERBlock.Bytes)
79 if err != nil {
80 panic(err)
81 }
82 if channelIDKey.Curve != elliptic.P256() {
83 panic("bad curve")
84 }
85
86 channelIDBytes = make([]byte, 64)
87 writeIntPadded(channelIDBytes[:32], channelIDKey.X)
88 writeIntPadded(channelIDBytes[32:], channelIDKey.Y)
Adam Langley95c29f32014-06-20 12:00:00 -070089}
90
91var certificateOnce sync.Once
92
93func getRSACertificate() Certificate {
94 certificateOnce.Do(initCertificates)
95 return rsaCertificate
96}
97
98func getECDSACertificate() Certificate {
99 certificateOnce.Do(initCertificates)
100 return ecdsaCertificate
101}
102
David Benjamin025b3d32014-07-01 19:53:04 -0400103type testType int
104
105const (
106 clientTest testType = iota
107 serverTest
108)
109
David Benjamin6fd297b2014-08-11 18:43:38 -0400110type protocol int
111
112const (
113 tls protocol = iota
114 dtls
115)
116
David Benjaminfc7b0862014-09-06 13:21:53 -0400117const (
118 alpn = 1
119 npn = 2
120)
121
Adam Langley95c29f32014-06-20 12:00:00 -0700122type testCase struct {
David Benjamin025b3d32014-07-01 19:53:04 -0400123 testType testType
David Benjamin6fd297b2014-08-11 18:43:38 -0400124 protocol protocol
Adam Langley95c29f32014-06-20 12:00:00 -0700125 name string
126 config Config
127 shouldFail bool
128 expectedError string
Adam Langleyac61fa32014-06-23 12:03:11 -0700129 // expectedLocalError, if not empty, contains a substring that must be
130 // found in the local error.
131 expectedLocalError string
David Benjamin7e2e6cf2014-08-07 17:44:24 -0400132 // expectedVersion, if non-zero, specifies the TLS version that must be
133 // negotiated.
134 expectedVersion uint16
David Benjamin01fe8202014-09-24 15:21:44 -0400135 // expectedResumeVersion, if non-zero, specifies the TLS version that
136 // must be negotiated on resumption. If zero, expectedVersion is used.
137 expectedResumeVersion uint16
David Benjamin90da8c82015-04-20 14:57:57 -0400138 // expectedCipher, if non-zero, specifies the TLS cipher suite that
139 // should be negotiated.
140 expectedCipher uint16
David Benjamina08e49d2014-08-24 01:46:07 -0400141 // expectChannelID controls whether the connection should have
142 // negotiated a Channel ID with channelIDKey.
143 expectChannelID bool
David Benjaminae2888f2014-09-06 12:58:58 -0400144 // expectedNextProto controls whether the connection should
145 // negotiate a next protocol via NPN or ALPN.
146 expectedNextProto string
David Benjaminfc7b0862014-09-06 13:21:53 -0400147 // expectedNextProtoType, if non-zero, is the expected next
148 // protocol negotiation mechanism.
149 expectedNextProtoType int
David Benjaminca6c8262014-11-15 19:06:08 -0500150 // expectedSRTPProtectionProfile is the DTLS-SRTP profile that
151 // should be negotiated. If zero, none should be negotiated.
152 expectedSRTPProtectionProfile uint16
Adam Langley80842bd2014-06-20 12:00:00 -0700153 // messageLen is the length, in bytes, of the test message that will be
154 // sent.
155 messageLen int
David Benjamin025b3d32014-07-01 19:53:04 -0400156 // certFile is the path to the certificate to use for the server.
157 certFile string
158 // keyFile is the path to the private key to use for the server.
159 keyFile string
David Benjamin1d5c83e2014-07-22 19:20:02 -0400160 // resumeSession controls whether a second connection should be tested
David Benjamin01fe8202014-09-24 15:21:44 -0400161 // which attempts to resume the first session.
David Benjamin1d5c83e2014-07-22 19:20:02 -0400162 resumeSession bool
David Benjamin01fe8202014-09-24 15:21:44 -0400163 // resumeConfig, if not nil, points to a Config to be used on
David Benjaminfe8eb9a2014-11-17 03:19:02 -0500164 // resumption. Unless newSessionsOnResume is set,
165 // SessionTicketKey, ServerSessionCache, and
166 // ClientSessionCache are copied from the initial connection's
167 // config. If nil, the initial connection's config is used.
David Benjamin01fe8202014-09-24 15:21:44 -0400168 resumeConfig *Config
David Benjaminfe8eb9a2014-11-17 03:19:02 -0500169 // newSessionsOnResume, if true, will cause resumeConfig to
170 // use a different session resumption context.
171 newSessionsOnResume bool
David Benjamin98e882e2014-08-08 13:24:34 -0400172 // sendPrefix sends a prefix on the socket before actually performing a
173 // handshake.
174 sendPrefix string
David Benjamine58c4f52014-08-24 03:47:07 -0400175 // shimWritesFirst controls whether the shim sends an initial "hello"
176 // message before doing a roundtrip with the runner.
177 shimWritesFirst bool
Adam Langleycf2d4f42014-10-28 19:06:14 -0700178 // renegotiate indicates the the connection should be renegotiated
179 // during the exchange.
180 renegotiate bool
181 // renegotiateCiphers is a list of ciphersuite ids that will be
182 // switched in just before renegotiation.
183 renegotiateCiphers []uint16
David Benjamin5e961c12014-11-07 01:48:35 -0500184 // replayWrites, if true, configures the underlying transport
185 // to replay every write it makes in DTLS tests.
186 replayWrites bool
David Benjamin5fa3eba2015-01-22 16:35:40 -0500187 // damageFirstWrite, if true, configures the underlying transport to
188 // damage the final byte of the first application data write.
189 damageFirstWrite bool
David Benjaminc565ebb2015-04-03 04:06:36 -0400190 // exportKeyingMaterial, if non-zero, configures the test to exchange
191 // keying material and verify they match.
192 exportKeyingMaterial int
193 exportLabel string
194 exportContext string
195 useExportContext bool
David Benjamin325b5c32014-07-01 19:40:31 -0400196 // flags, if not empty, contains a list of command-line flags that will
197 // be passed to the shim program.
198 flags []string
Adam Langley95c29f32014-06-20 12:00:00 -0700199}
200
David Benjamin025b3d32014-07-01 19:53:04 -0400201var testCases = []testCase{
Adam Langley95c29f32014-06-20 12:00:00 -0700202 {
203 name: "BadRSASignature",
204 config: Config{
205 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
206 Bugs: ProtocolBugs{
207 InvalidSKXSignature: true,
208 },
209 },
210 shouldFail: true,
David Benjamin25f08462015-04-15 16:13:49 -0400211 expectedError: ":BAD_SIGNATURE:",
Adam Langley95c29f32014-06-20 12:00:00 -0700212 },
213 {
214 name: "BadECDSASignature",
215 config: Config{
216 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
217 Bugs: ProtocolBugs{
218 InvalidSKXSignature: true,
219 },
220 Certificates: []Certificate{getECDSACertificate()},
221 },
222 shouldFail: true,
223 expectedError: ":BAD_SIGNATURE:",
224 },
225 {
226 name: "BadECDSACurve",
227 config: Config{
228 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
229 Bugs: ProtocolBugs{
230 InvalidSKXCurve: true,
231 },
232 Certificates: []Certificate{getECDSACertificate()},
233 },
234 shouldFail: true,
235 expectedError: ":WRONG_CURVE:",
236 },
Adam Langleyac61fa32014-06-23 12:03:11 -0700237 {
David Benjamina8e3e0e2014-08-06 22:11:10 -0400238 testType: serverTest,
239 name: "BadRSAVersion",
240 config: Config{
241 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
242 Bugs: ProtocolBugs{
243 RsaClientKeyExchangeVersion: VersionTLS11,
244 },
245 },
246 shouldFail: true,
247 expectedError: ":DECRYPTION_FAILED_OR_BAD_RECORD_MAC:",
248 },
249 {
David Benjamin325b5c32014-07-01 19:40:31 -0400250 name: "NoFallbackSCSV",
Adam Langleyac61fa32014-06-23 12:03:11 -0700251 config: Config{
252 Bugs: ProtocolBugs{
253 FailIfNotFallbackSCSV: true,
254 },
255 },
256 shouldFail: true,
257 expectedLocalError: "no fallback SCSV found",
258 },
David Benjamin325b5c32014-07-01 19:40:31 -0400259 {
David Benjamin2a0c4962014-08-22 23:46:35 -0400260 name: "SendFallbackSCSV",
David Benjamin325b5c32014-07-01 19:40:31 -0400261 config: Config{
262 Bugs: ProtocolBugs{
263 FailIfNotFallbackSCSV: true,
264 },
265 },
266 flags: []string{"-fallback-scsv"},
267 },
David Benjamin197b3ab2014-07-02 18:37:33 -0400268 {
David Benjamin7b030512014-07-08 17:30:11 -0400269 name: "ClientCertificateTypes",
270 config: Config{
271 ClientAuth: RequestClientCert,
272 ClientCertificateTypes: []byte{
273 CertTypeDSSSign,
274 CertTypeRSASign,
275 CertTypeECDSASign,
276 },
277 },
David Benjamin2561dc32014-08-24 01:25:27 -0400278 flags: []string{
279 "-expect-certificate-types",
280 base64.StdEncoding.EncodeToString([]byte{
281 CertTypeDSSSign,
282 CertTypeRSASign,
283 CertTypeECDSASign,
284 }),
285 },
David Benjamin7b030512014-07-08 17:30:11 -0400286 },
David Benjamin636293b2014-07-08 17:59:18 -0400287 {
288 name: "NoClientCertificate",
289 config: Config{
290 ClientAuth: RequireAnyClientCert,
291 },
292 shouldFail: true,
293 expectedLocalError: "client didn't provide a certificate",
294 },
David Benjamin1c375dd2014-07-12 00:48:23 -0400295 {
296 name: "UnauthenticatedECDH",
297 config: Config{
298 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
299 Bugs: ProtocolBugs{
300 UnauthenticatedECDH: true,
301 },
302 },
303 shouldFail: true,
David Benjamine8f3d662014-07-12 01:10:19 -0400304 expectedError: ":UNEXPECTED_MESSAGE:",
David Benjamin1c375dd2014-07-12 00:48:23 -0400305 },
David Benjamin9c651c92014-07-12 13:27:45 -0400306 {
David Benjamindcd979f2015-04-20 18:26:52 -0400307 name: "SkipCertificateStatus",
308 config: Config{
309 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
310 Bugs: ProtocolBugs{
311 SkipCertificateStatus: true,
312 },
313 },
314 flags: []string{
315 "-enable-ocsp-stapling",
316 },
317 },
318 {
David Benjamin9c651c92014-07-12 13:27:45 -0400319 name: "SkipServerKeyExchange",
320 config: Config{
321 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
322 Bugs: ProtocolBugs{
323 SkipServerKeyExchange: true,
324 },
325 },
326 shouldFail: true,
327 expectedError: ":UNEXPECTED_MESSAGE:",
328 },
David Benjamin1f5f62b2014-07-12 16:18:02 -0400329 {
David Benjamina0e52232014-07-19 17:39:58 -0400330 name: "SkipChangeCipherSpec-Client",
331 config: Config{
332 Bugs: ProtocolBugs{
333 SkipChangeCipherSpec: true,
334 },
335 },
336 shouldFail: true,
David Benjamin86271ee2014-07-21 16:14:03 -0400337 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
David Benjamina0e52232014-07-19 17:39:58 -0400338 },
339 {
340 testType: serverTest,
341 name: "SkipChangeCipherSpec-Server",
342 config: Config{
343 Bugs: ProtocolBugs{
344 SkipChangeCipherSpec: true,
345 },
346 },
347 shouldFail: true,
David Benjamin86271ee2014-07-21 16:14:03 -0400348 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
David Benjamina0e52232014-07-19 17:39:58 -0400349 },
David Benjamin42be6452014-07-21 14:50:23 -0400350 {
351 testType: serverTest,
352 name: "SkipChangeCipherSpec-Server-NPN",
353 config: Config{
354 NextProtos: []string{"bar"},
355 Bugs: ProtocolBugs{
356 SkipChangeCipherSpec: true,
357 },
358 },
359 flags: []string{
360 "-advertise-npn", "\x03foo\x03bar\x03baz",
361 },
362 shouldFail: true,
David Benjamin86271ee2014-07-21 16:14:03 -0400363 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
364 },
365 {
366 name: "FragmentAcrossChangeCipherSpec-Client",
367 config: Config{
368 Bugs: ProtocolBugs{
369 FragmentAcrossChangeCipherSpec: true,
370 },
371 },
372 shouldFail: true,
373 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
374 },
375 {
376 testType: serverTest,
377 name: "FragmentAcrossChangeCipherSpec-Server",
378 config: Config{
379 Bugs: ProtocolBugs{
380 FragmentAcrossChangeCipherSpec: true,
381 },
382 },
383 shouldFail: true,
384 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
385 },
386 {
387 testType: serverTest,
388 name: "FragmentAcrossChangeCipherSpec-Server-NPN",
389 config: Config{
390 NextProtos: []string{"bar"},
391 Bugs: ProtocolBugs{
392 FragmentAcrossChangeCipherSpec: true,
393 },
394 },
395 flags: []string{
396 "-advertise-npn", "\x03foo\x03bar\x03baz",
397 },
398 shouldFail: true,
399 expectedError: ":HANDSHAKE_RECORD_BEFORE_CCS:",
David Benjamin42be6452014-07-21 14:50:23 -0400400 },
David Benjaminf3ec83d2014-07-21 22:42:34 -0400401 {
402 testType: serverTest,
David Benjamin3fd1fbd2015-02-03 16:07:32 -0500403 name: "Alert",
404 config: Config{
405 Bugs: ProtocolBugs{
406 SendSpuriousAlert: alertRecordOverflow,
407 },
408 },
409 shouldFail: true,
410 expectedError: ":TLSV1_ALERT_RECORD_OVERFLOW:",
411 },
412 {
413 protocol: dtls,
414 testType: serverTest,
415 name: "Alert-DTLS",
416 config: Config{
417 Bugs: ProtocolBugs{
418 SendSpuriousAlert: alertRecordOverflow,
419 },
420 },
421 shouldFail: true,
422 expectedError: ":TLSV1_ALERT_RECORD_OVERFLOW:",
423 },
424 {
425 testType: serverTest,
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400426 name: "FragmentAlert",
427 config: Config{
428 Bugs: ProtocolBugs{
David Benjaminca6c8262014-11-15 19:06:08 -0500429 FragmentAlert: true,
David Benjamin3fd1fbd2015-02-03 16:07:32 -0500430 SendSpuriousAlert: alertRecordOverflow,
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400431 },
432 },
433 shouldFail: true,
434 expectedError: ":BAD_ALERT:",
435 },
436 {
David Benjamin0ea8dda2015-01-31 20:33:40 -0500437 protocol: dtls,
438 testType: serverTest,
439 name: "FragmentAlert-DTLS",
440 config: Config{
441 Bugs: ProtocolBugs{
442 FragmentAlert: true,
David Benjamin3fd1fbd2015-02-03 16:07:32 -0500443 SendSpuriousAlert: alertRecordOverflow,
David Benjamin0ea8dda2015-01-31 20:33:40 -0500444 },
445 },
446 shouldFail: true,
447 expectedError: ":BAD_ALERT:",
448 },
449 {
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400450 testType: serverTest,
David Benjaminf3ec83d2014-07-21 22:42:34 -0400451 name: "EarlyChangeCipherSpec-server-1",
452 config: Config{
453 Bugs: ProtocolBugs{
454 EarlyChangeCipherSpec: 1,
455 },
456 },
457 shouldFail: true,
458 expectedError: ":CCS_RECEIVED_EARLY:",
459 },
460 {
461 testType: serverTest,
462 name: "EarlyChangeCipherSpec-server-2",
463 config: Config{
464 Bugs: ProtocolBugs{
465 EarlyChangeCipherSpec: 2,
466 },
467 },
468 shouldFail: true,
469 expectedError: ":CCS_RECEIVED_EARLY:",
470 },
David Benjamind23f4122014-07-23 15:09:48 -0400471 {
David Benjamind23f4122014-07-23 15:09:48 -0400472 name: "SkipNewSessionTicket",
473 config: Config{
474 Bugs: ProtocolBugs{
475 SkipNewSessionTicket: true,
476 },
477 },
478 shouldFail: true,
479 expectedError: ":CCS_RECEIVED_EARLY:",
480 },
David Benjamin7e3305e2014-07-28 14:52:32 -0400481 {
David Benjamind86c7672014-08-02 04:07:12 -0400482 testType: serverTest,
David Benjaminbef270a2014-08-02 04:22:02 -0400483 name: "FallbackSCSV",
484 config: Config{
485 MaxVersion: VersionTLS11,
486 Bugs: ProtocolBugs{
487 SendFallbackSCSV: true,
488 },
489 },
490 shouldFail: true,
491 expectedError: ":INAPPROPRIATE_FALLBACK:",
492 },
493 {
494 testType: serverTest,
495 name: "FallbackSCSV-VersionMatch",
496 config: Config{
497 Bugs: ProtocolBugs{
498 SendFallbackSCSV: true,
499 },
500 },
501 },
David Benjamin98214542014-08-07 18:02:39 -0400502 {
503 testType: serverTest,
504 name: "FragmentedClientVersion",
505 config: Config{
506 Bugs: ProtocolBugs{
507 MaxHandshakeRecordLength: 1,
508 FragmentClientVersion: true,
509 },
510 },
David Benjamin82c9e902014-12-12 15:55:27 -0500511 expectedVersion: VersionTLS12,
David Benjamin98214542014-08-07 18:02:39 -0400512 },
David Benjamin98e882e2014-08-08 13:24:34 -0400513 {
514 testType: serverTest,
515 name: "MinorVersionTolerance",
516 config: Config{
517 Bugs: ProtocolBugs{
518 SendClientVersion: 0x03ff,
519 },
520 },
521 expectedVersion: VersionTLS12,
522 },
523 {
524 testType: serverTest,
525 name: "MajorVersionTolerance",
526 config: Config{
527 Bugs: ProtocolBugs{
528 SendClientVersion: 0x0400,
529 },
530 },
531 expectedVersion: VersionTLS12,
532 },
533 {
534 testType: serverTest,
535 name: "VersionTooLow",
536 config: Config{
537 Bugs: ProtocolBugs{
538 SendClientVersion: 0x0200,
539 },
540 },
541 shouldFail: true,
542 expectedError: ":UNSUPPORTED_PROTOCOL:",
543 },
544 {
545 testType: serverTest,
546 name: "HttpGET",
547 sendPrefix: "GET / HTTP/1.0\n",
548 shouldFail: true,
549 expectedError: ":HTTP_REQUEST:",
550 },
551 {
552 testType: serverTest,
553 name: "HttpPOST",
554 sendPrefix: "POST / HTTP/1.0\n",
555 shouldFail: true,
556 expectedError: ":HTTP_REQUEST:",
557 },
558 {
559 testType: serverTest,
560 name: "HttpHEAD",
561 sendPrefix: "HEAD / HTTP/1.0\n",
562 shouldFail: true,
563 expectedError: ":HTTP_REQUEST:",
564 },
565 {
566 testType: serverTest,
567 name: "HttpPUT",
568 sendPrefix: "PUT / HTTP/1.0\n",
569 shouldFail: true,
570 expectedError: ":HTTP_REQUEST:",
571 },
572 {
573 testType: serverTest,
574 name: "HttpCONNECT",
575 sendPrefix: "CONNECT www.google.com:443 HTTP/1.0\n",
576 shouldFail: true,
577 expectedError: ":HTTPS_PROXY_REQUEST:",
578 },
David Benjamin39ebf532014-08-31 02:23:49 -0400579 {
David Benjaminf080ecd2014-12-11 18:48:59 -0500580 testType: serverTest,
581 name: "Garbage",
582 sendPrefix: "blah",
583 shouldFail: true,
584 expectedError: ":UNKNOWN_PROTOCOL:",
585 },
586 {
David Benjamin39ebf532014-08-31 02:23:49 -0400587 name: "SkipCipherVersionCheck",
588 config: Config{
589 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
590 MaxVersion: VersionTLS11,
591 Bugs: ProtocolBugs{
592 SkipCipherVersionCheck: true,
593 },
594 },
595 shouldFail: true,
596 expectedError: ":WRONG_CIPHER_RETURNED:",
597 },
David Benjamin9114fae2014-11-08 11:41:14 -0500598 {
David Benjamina3e89492015-02-26 15:16:22 -0500599 name: "RSAEphemeralKey",
David Benjamin9114fae2014-11-08 11:41:14 -0500600 config: Config{
601 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
602 Bugs: ProtocolBugs{
David Benjamina3e89492015-02-26 15:16:22 -0500603 RSAEphemeralKey: true,
David Benjamin9114fae2014-11-08 11:41:14 -0500604 },
605 },
606 shouldFail: true,
607 expectedError: ":UNEXPECTED_MESSAGE:",
608 },
David Benjamin128dbc32014-12-01 01:27:42 -0500609 {
610 name: "DisableEverything",
611 flags: []string{"-no-tls12", "-no-tls11", "-no-tls1", "-no-ssl3"},
612 shouldFail: true,
613 expectedError: ":WRONG_SSL_VERSION:",
614 },
615 {
616 protocol: dtls,
617 name: "DisableEverything-DTLS",
618 flags: []string{"-no-tls12", "-no-tls1"},
619 shouldFail: true,
620 expectedError: ":WRONG_SSL_VERSION:",
621 },
David Benjamin780d6dd2015-01-06 12:03:19 -0500622 {
623 name: "NoSharedCipher",
624 config: Config{
625 CipherSuites: []uint16{},
626 },
627 shouldFail: true,
628 expectedError: ":HANDSHAKE_FAILURE_ON_CLIENT_HELLO:",
629 },
David Benjamin13be1de2015-01-11 16:29:36 -0500630 {
631 protocol: dtls,
632 testType: serverTest,
633 name: "MTU",
634 config: Config{
635 Bugs: ProtocolBugs{
636 MaxPacketLength: 256,
637 },
638 },
639 flags: []string{"-mtu", "256"},
640 },
641 {
642 protocol: dtls,
643 testType: serverTest,
644 name: "MTUExceeded",
645 config: Config{
646 Bugs: ProtocolBugs{
647 MaxPacketLength: 255,
648 },
649 },
650 flags: []string{"-mtu", "256"},
651 shouldFail: true,
652 expectedLocalError: "dtls: exceeded maximum packet length",
653 },
David Benjamin6095de82014-12-27 01:50:38 -0500654 {
655 name: "CertMismatchRSA",
656 config: Config{
657 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
658 Certificates: []Certificate{getECDSACertificate()},
659 Bugs: ProtocolBugs{
660 SendCipherSuite: TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
661 },
662 },
663 shouldFail: true,
664 expectedError: ":WRONG_CERTIFICATE_TYPE:",
665 },
666 {
667 name: "CertMismatchECDSA",
668 config: Config{
669 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
670 Certificates: []Certificate{getRSACertificate()},
671 Bugs: ProtocolBugs{
672 SendCipherSuite: TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
673 },
674 },
675 shouldFail: true,
676 expectedError: ":WRONG_CERTIFICATE_TYPE:",
677 },
David Benjamin5fa3eba2015-01-22 16:35:40 -0500678 {
679 name: "TLSFatalBadPackets",
680 damageFirstWrite: true,
681 shouldFail: true,
682 expectedError: ":DECRYPTION_FAILED_OR_BAD_RECORD_MAC:",
683 },
684 {
685 protocol: dtls,
686 name: "DTLSIgnoreBadPackets",
687 damageFirstWrite: true,
688 },
689 {
690 protocol: dtls,
691 name: "DTLSIgnoreBadPackets-Async",
692 damageFirstWrite: true,
693 flags: []string{"-async"},
694 },
David Benjamin4189bd92015-01-25 23:52:39 -0500695 {
696 name: "AppDataAfterChangeCipherSpec",
697 config: Config{
698 Bugs: ProtocolBugs{
699 AppDataAfterChangeCipherSpec: []byte("TEST MESSAGE"),
700 },
701 },
702 shouldFail: true,
703 expectedError: ":DATA_BETWEEN_CCS_AND_FINISHED:",
704 },
705 {
706 protocol: dtls,
707 name: "AppDataAfterChangeCipherSpec-DTLS",
708 config: Config{
709 Bugs: ProtocolBugs{
710 AppDataAfterChangeCipherSpec: []byte("TEST MESSAGE"),
711 },
712 },
David Benjamin4417d052015-04-05 04:17:25 -0400713 // BoringSSL's DTLS implementation will drop the out-of-order
714 // application data.
David Benjamin4189bd92015-01-25 23:52:39 -0500715 },
David Benjaminb3774b92015-01-31 17:16:01 -0500716 {
David Benjamindc3da932015-03-12 15:09:02 -0400717 name: "AlertAfterChangeCipherSpec",
718 config: Config{
719 Bugs: ProtocolBugs{
720 AlertAfterChangeCipherSpec: alertRecordOverflow,
721 },
722 },
723 shouldFail: true,
724 expectedError: ":TLSV1_ALERT_RECORD_OVERFLOW:",
725 },
726 {
727 protocol: dtls,
728 name: "AlertAfterChangeCipherSpec-DTLS",
729 config: Config{
730 Bugs: ProtocolBugs{
731 AlertAfterChangeCipherSpec: alertRecordOverflow,
732 },
733 },
734 shouldFail: true,
735 expectedError: ":TLSV1_ALERT_RECORD_OVERFLOW:",
736 },
737 {
David Benjaminb3774b92015-01-31 17:16:01 -0500738 protocol: dtls,
739 name: "ReorderHandshakeFragments-Small-DTLS",
740 config: Config{
741 Bugs: ProtocolBugs{
742 ReorderHandshakeFragments: true,
743 // Small enough that every handshake message is
744 // fragmented.
745 MaxHandshakeRecordLength: 2,
746 },
747 },
748 },
749 {
750 protocol: dtls,
751 name: "ReorderHandshakeFragments-Large-DTLS",
752 config: Config{
753 Bugs: ProtocolBugs{
754 ReorderHandshakeFragments: true,
755 // Large enough that no handshake message is
756 // fragmented.
David Benjaminb3774b92015-01-31 17:16:01 -0500757 MaxHandshakeRecordLength: 2048,
758 },
759 },
760 },
David Benjaminddb9f152015-02-03 15:44:39 -0500761 {
David Benjamin75381222015-03-02 19:30:30 -0500762 protocol: dtls,
763 name: "MixCompleteMessageWithFragments-DTLS",
764 config: Config{
765 Bugs: ProtocolBugs{
766 ReorderHandshakeFragments: true,
767 MixCompleteMessageWithFragments: true,
768 MaxHandshakeRecordLength: 2,
769 },
770 },
771 },
772 {
David Benjaminddb9f152015-02-03 15:44:39 -0500773 name: "SendInvalidRecordType",
774 config: Config{
775 Bugs: ProtocolBugs{
776 SendInvalidRecordType: true,
777 },
778 },
779 shouldFail: true,
780 expectedError: ":UNEXPECTED_RECORD:",
781 },
782 {
783 protocol: dtls,
784 name: "SendInvalidRecordType-DTLS",
785 config: Config{
786 Bugs: ProtocolBugs{
787 SendInvalidRecordType: true,
788 },
789 },
790 shouldFail: true,
791 expectedError: ":UNEXPECTED_RECORD:",
792 },
David Benjaminb80168e2015-02-08 18:30:14 -0500793 {
794 name: "FalseStart-SkipServerSecondLeg",
795 config: Config{
796 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
797 NextProtos: []string{"foo"},
798 Bugs: ProtocolBugs{
799 SkipNewSessionTicket: true,
800 SkipChangeCipherSpec: true,
801 SkipFinished: true,
802 ExpectFalseStart: true,
803 },
804 },
805 flags: []string{
806 "-false-start",
David Benjamin87e4acd2015-04-02 19:57:35 -0400807 "-handshake-never-done",
David Benjaminb80168e2015-02-08 18:30:14 -0500808 "-advertise-alpn", "\x03foo",
809 },
810 shimWritesFirst: true,
811 shouldFail: true,
812 expectedError: ":UNEXPECTED_RECORD:",
813 },
David Benjamin931ab342015-02-08 19:46:57 -0500814 {
815 name: "FalseStart-SkipServerSecondLeg-Implicit",
816 config: Config{
817 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
818 NextProtos: []string{"foo"},
819 Bugs: ProtocolBugs{
820 SkipNewSessionTicket: true,
821 SkipChangeCipherSpec: true,
822 SkipFinished: true,
823 },
824 },
825 flags: []string{
826 "-implicit-handshake",
827 "-false-start",
David Benjamin87e4acd2015-04-02 19:57:35 -0400828 "-handshake-never-done",
David Benjamin931ab342015-02-08 19:46:57 -0500829 "-advertise-alpn", "\x03foo",
830 },
831 shouldFail: true,
832 expectedError: ":UNEXPECTED_RECORD:",
833 },
David Benjamin6f5c0f42015-02-24 01:23:21 -0500834 {
835 testType: serverTest,
836 name: "FailEarlyCallback",
837 flags: []string{"-fail-early-callback"},
838 shouldFail: true,
839 expectedError: ":CONNECTION_REJECTED:",
840 expectedLocalError: "remote error: access denied",
841 },
David Benjaminbcb2d912015-02-24 23:45:43 -0500842 {
843 name: "WrongMessageType",
844 config: Config{
845 Bugs: ProtocolBugs{
846 WrongCertificateMessageType: true,
847 },
848 },
849 shouldFail: true,
850 expectedError: ":UNEXPECTED_MESSAGE:",
851 expectedLocalError: "remote error: unexpected message",
852 },
853 {
854 protocol: dtls,
855 name: "WrongMessageType-DTLS",
856 config: Config{
857 Bugs: ProtocolBugs{
858 WrongCertificateMessageType: true,
859 },
860 },
861 shouldFail: true,
862 expectedError: ":UNEXPECTED_MESSAGE:",
863 expectedLocalError: "remote error: unexpected message",
864 },
David Benjamin75381222015-03-02 19:30:30 -0500865 {
866 protocol: dtls,
867 name: "FragmentMessageTypeMismatch-DTLS",
868 config: Config{
869 Bugs: ProtocolBugs{
870 MaxHandshakeRecordLength: 2,
871 FragmentMessageTypeMismatch: true,
872 },
873 },
874 shouldFail: true,
875 expectedError: ":FRAGMENT_MISMATCH:",
876 },
877 {
878 protocol: dtls,
879 name: "FragmentMessageLengthMismatch-DTLS",
880 config: Config{
881 Bugs: ProtocolBugs{
882 MaxHandshakeRecordLength: 2,
883 FragmentMessageLengthMismatch: true,
884 },
885 },
886 shouldFail: true,
887 expectedError: ":FRAGMENT_MISMATCH:",
888 },
889 {
890 protocol: dtls,
891 name: "SplitFragmentHeader-DTLS",
892 config: Config{
893 Bugs: ProtocolBugs{
894 SplitFragmentHeader: true,
895 },
896 },
897 shouldFail: true,
898 expectedError: ":UNEXPECTED_MESSAGE:",
899 },
900 {
901 protocol: dtls,
902 name: "SplitFragmentBody-DTLS",
903 config: Config{
904 Bugs: ProtocolBugs{
905 SplitFragmentBody: true,
906 },
907 },
908 shouldFail: true,
909 expectedError: ":UNEXPECTED_MESSAGE:",
910 },
911 {
912 protocol: dtls,
913 name: "SendEmptyFragments-DTLS",
914 config: Config{
915 Bugs: ProtocolBugs{
916 SendEmptyFragments: true,
917 },
918 },
919 },
David Benjamin67d1fb52015-03-16 15:16:23 -0400920 {
921 name: "UnsupportedCipherSuite",
922 config: Config{
923 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
924 Bugs: ProtocolBugs{
925 IgnorePeerCipherPreferences: true,
926 },
927 },
928 flags: []string{"-cipher", "DEFAULT:!RC4"},
929 shouldFail: true,
930 expectedError: ":WRONG_CIPHER_RETURNED:",
931 },
David Benjamin340d5ed2015-03-21 02:21:37 -0400932 {
David Benjaminc574f412015-04-20 11:13:01 -0400933 name: "UnsupportedCurve",
934 config: Config{
935 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
936 // BoringSSL implements P-224 but doesn't enable it by
937 // default.
938 CurvePreferences: []CurveID{CurveP224},
939 Bugs: ProtocolBugs{
940 IgnorePeerCurvePreferences: true,
941 },
942 },
943 shouldFail: true,
944 expectedError: ":WRONG_CURVE:",
945 },
946 {
David Benjamin340d5ed2015-03-21 02:21:37 -0400947 name: "SendWarningAlerts",
948 config: Config{
949 Bugs: ProtocolBugs{
950 SendWarningAlerts: alertAccessDenied,
951 },
952 },
953 },
954 {
955 protocol: dtls,
956 name: "SendWarningAlerts-DTLS",
957 config: Config{
958 Bugs: ProtocolBugs{
959 SendWarningAlerts: alertAccessDenied,
960 },
961 },
962 },
David Benjamin513f0ea2015-04-02 19:33:31 -0400963 {
964 name: "BadFinished",
965 config: Config{
966 Bugs: ProtocolBugs{
967 BadFinished: true,
968 },
969 },
970 shouldFail: true,
971 expectedError: ":DIGEST_CHECK_FAILED:",
972 },
973 {
974 name: "FalseStart-BadFinished",
975 config: Config{
976 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
977 NextProtos: []string{"foo"},
978 Bugs: ProtocolBugs{
979 BadFinished: true,
980 ExpectFalseStart: true,
981 },
982 },
983 flags: []string{
984 "-false-start",
David Benjamin87e4acd2015-04-02 19:57:35 -0400985 "-handshake-never-done",
David Benjamin513f0ea2015-04-02 19:33:31 -0400986 "-advertise-alpn", "\x03foo",
987 },
988 shimWritesFirst: true,
989 shouldFail: true,
990 expectedError: ":DIGEST_CHECK_FAILED:",
991 },
David Benjamin1c633152015-04-02 20:19:11 -0400992 {
993 name: "NoFalseStart-NoALPN",
994 config: Config{
995 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
996 Bugs: ProtocolBugs{
997 ExpectFalseStart: true,
998 AlertBeforeFalseStartTest: alertAccessDenied,
999 },
1000 },
1001 flags: []string{
1002 "-false-start",
1003 },
1004 shimWritesFirst: true,
1005 shouldFail: true,
1006 expectedError: ":TLSV1_ALERT_ACCESS_DENIED:",
1007 expectedLocalError: "tls: peer did not false start: EOF",
1008 },
1009 {
1010 name: "NoFalseStart-NoAEAD",
1011 config: Config{
1012 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1013 NextProtos: []string{"foo"},
1014 Bugs: ProtocolBugs{
1015 ExpectFalseStart: true,
1016 AlertBeforeFalseStartTest: alertAccessDenied,
1017 },
1018 },
1019 flags: []string{
1020 "-false-start",
1021 "-advertise-alpn", "\x03foo",
1022 },
1023 shimWritesFirst: true,
1024 shouldFail: true,
1025 expectedError: ":TLSV1_ALERT_ACCESS_DENIED:",
1026 expectedLocalError: "tls: peer did not false start: EOF",
1027 },
1028 {
1029 name: "NoFalseStart-RSA",
1030 config: Config{
1031 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
1032 NextProtos: []string{"foo"},
1033 Bugs: ProtocolBugs{
1034 ExpectFalseStart: true,
1035 AlertBeforeFalseStartTest: alertAccessDenied,
1036 },
1037 },
1038 flags: []string{
1039 "-false-start",
1040 "-advertise-alpn", "\x03foo",
1041 },
1042 shimWritesFirst: true,
1043 shouldFail: true,
1044 expectedError: ":TLSV1_ALERT_ACCESS_DENIED:",
1045 expectedLocalError: "tls: peer did not false start: EOF",
1046 },
1047 {
1048 name: "NoFalseStart-DHE_RSA",
1049 config: Config{
1050 CipherSuites: []uint16{TLS_DHE_RSA_WITH_AES_128_GCM_SHA256},
1051 NextProtos: []string{"foo"},
1052 Bugs: ProtocolBugs{
1053 ExpectFalseStart: true,
1054 AlertBeforeFalseStartTest: alertAccessDenied,
1055 },
1056 },
1057 flags: []string{
1058 "-false-start",
1059 "-advertise-alpn", "\x03foo",
1060 },
1061 shimWritesFirst: true,
1062 shouldFail: true,
1063 expectedError: ":TLSV1_ALERT_ACCESS_DENIED:",
1064 expectedLocalError: "tls: peer did not false start: EOF",
1065 },
David Benjamin55a43642015-04-20 14:45:55 -04001066 {
1067 testType: serverTest,
1068 name: "NoSupportedCurves",
1069 config: Config{
1070 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
1071 Bugs: ProtocolBugs{
1072 NoSupportedCurves: true,
1073 },
1074 },
1075 },
David Benjamin90da8c82015-04-20 14:57:57 -04001076 {
1077 testType: serverTest,
1078 name: "NoCommonCurves",
1079 config: Config{
1080 CipherSuites: []uint16{
1081 TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
1082 TLS_DHE_RSA_WITH_AES_128_GCM_SHA256,
1083 },
1084 CurvePreferences: []CurveID{CurveP224},
1085 },
1086 expectedCipher: TLS_DHE_RSA_WITH_AES_128_GCM_SHA256,
1087 },
Adam Langley95c29f32014-06-20 12:00:00 -07001088}
1089
David Benjamin01fe8202014-09-24 15:21:44 -04001090func doExchange(test *testCase, config *Config, conn net.Conn, messageLen int, isResume bool) error {
David Benjamin65ea8ff2014-11-23 03:01:00 -05001091 var connDebug *recordingConn
David Benjamin5fa3eba2015-01-22 16:35:40 -05001092 var connDamage *damageAdaptor
David Benjamin65ea8ff2014-11-23 03:01:00 -05001093 if *flagDebug {
1094 connDebug = &recordingConn{Conn: conn}
1095 conn = connDebug
1096 defer func() {
1097 connDebug.WriteTo(os.Stdout)
1098 }()
1099 }
1100
David Benjamin6fd297b2014-08-11 18:43:38 -04001101 if test.protocol == dtls {
David Benjamin83f90402015-01-27 01:09:43 -05001102 config.Bugs.PacketAdaptor = newPacketAdaptor(conn)
1103 conn = config.Bugs.PacketAdaptor
David Benjamin5e961c12014-11-07 01:48:35 -05001104 if test.replayWrites {
1105 conn = newReplayAdaptor(conn)
1106 }
David Benjamin6fd297b2014-08-11 18:43:38 -04001107 }
1108
David Benjamin5fa3eba2015-01-22 16:35:40 -05001109 if test.damageFirstWrite {
1110 connDamage = newDamageAdaptor(conn)
1111 conn = connDamage
1112 }
1113
David Benjamin6fd297b2014-08-11 18:43:38 -04001114 if test.sendPrefix != "" {
1115 if _, err := conn.Write([]byte(test.sendPrefix)); err != nil {
1116 return err
1117 }
David Benjamin98e882e2014-08-08 13:24:34 -04001118 }
1119
David Benjamin1d5c83e2014-07-22 19:20:02 -04001120 var tlsConn *Conn
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001121 if test.testType == clientTest {
David Benjamin6fd297b2014-08-11 18:43:38 -04001122 if test.protocol == dtls {
1123 tlsConn = DTLSServer(conn, config)
1124 } else {
1125 tlsConn = Server(conn, config)
1126 }
David Benjamin1d5c83e2014-07-22 19:20:02 -04001127 } else {
1128 config.InsecureSkipVerify = true
David Benjamin6fd297b2014-08-11 18:43:38 -04001129 if test.protocol == dtls {
1130 tlsConn = DTLSClient(conn, config)
1131 } else {
1132 tlsConn = Client(conn, config)
1133 }
David Benjamin1d5c83e2014-07-22 19:20:02 -04001134 }
1135
Adam Langley95c29f32014-06-20 12:00:00 -07001136 if err := tlsConn.Handshake(); err != nil {
1137 return err
1138 }
Kenny Root7fdeaf12014-08-05 15:23:37 -07001139
David Benjamin01fe8202014-09-24 15:21:44 -04001140 // TODO(davidben): move all per-connection expectations into a dedicated
1141 // expectations struct that can be specified separately for the two
1142 // legs.
1143 expectedVersion := test.expectedVersion
1144 if isResume && test.expectedResumeVersion != 0 {
1145 expectedVersion = test.expectedResumeVersion
1146 }
1147 if vers := tlsConn.ConnectionState().Version; expectedVersion != 0 && vers != expectedVersion {
1148 return fmt.Errorf("got version %x, expected %x", vers, expectedVersion)
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001149 }
1150
David Benjamin90da8c82015-04-20 14:57:57 -04001151 if cipher := tlsConn.ConnectionState().CipherSuite; test.expectedCipher != 0 && cipher != test.expectedCipher {
1152 return fmt.Errorf("got cipher %x, expected %x", cipher, test.expectedCipher)
1153 }
1154
David Benjamina08e49d2014-08-24 01:46:07 -04001155 if test.expectChannelID {
1156 channelID := tlsConn.ConnectionState().ChannelID
1157 if channelID == nil {
1158 return fmt.Errorf("no channel ID negotiated")
1159 }
1160 if channelID.Curve != channelIDKey.Curve ||
1161 channelIDKey.X.Cmp(channelIDKey.X) != 0 ||
1162 channelIDKey.Y.Cmp(channelIDKey.Y) != 0 {
1163 return fmt.Errorf("incorrect channel ID")
1164 }
1165 }
1166
David Benjaminae2888f2014-09-06 12:58:58 -04001167 if expected := test.expectedNextProto; expected != "" {
1168 if actual := tlsConn.ConnectionState().NegotiatedProtocol; actual != expected {
1169 return fmt.Errorf("next proto mismatch: got %s, wanted %s", actual, expected)
1170 }
1171 }
1172
David Benjaminfc7b0862014-09-06 13:21:53 -04001173 if test.expectedNextProtoType != 0 {
1174 if (test.expectedNextProtoType == alpn) != tlsConn.ConnectionState().NegotiatedProtocolFromALPN {
1175 return fmt.Errorf("next proto type mismatch")
1176 }
1177 }
1178
David Benjaminca6c8262014-11-15 19:06:08 -05001179 if p := tlsConn.ConnectionState().SRTPProtectionProfile; p != test.expectedSRTPProtectionProfile {
1180 return fmt.Errorf("SRTP profile mismatch: got %d, wanted %d", p, test.expectedSRTPProtectionProfile)
1181 }
1182
David Benjaminc565ebb2015-04-03 04:06:36 -04001183 if test.exportKeyingMaterial > 0 {
1184 actual := make([]byte, test.exportKeyingMaterial)
1185 if _, err := io.ReadFull(tlsConn, actual); err != nil {
1186 return err
1187 }
1188 expected, err := tlsConn.ExportKeyingMaterial(test.exportKeyingMaterial, []byte(test.exportLabel), []byte(test.exportContext), test.useExportContext)
1189 if err != nil {
1190 return err
1191 }
1192 if !bytes.Equal(actual, expected) {
1193 return fmt.Errorf("keying material mismatch")
1194 }
1195 }
1196
David Benjamine58c4f52014-08-24 03:47:07 -04001197 if test.shimWritesFirst {
1198 var buf [5]byte
1199 _, err := io.ReadFull(tlsConn, buf[:])
1200 if err != nil {
1201 return err
1202 }
1203 if string(buf[:]) != "hello" {
1204 return fmt.Errorf("bad initial message")
1205 }
1206 }
1207
Adam Langleycf2d4f42014-10-28 19:06:14 -07001208 if test.renegotiate {
1209 if test.renegotiateCiphers != nil {
1210 config.CipherSuites = test.renegotiateCiphers
1211 }
1212 if err := tlsConn.Renegotiate(); err != nil {
1213 return err
1214 }
1215 } else if test.renegotiateCiphers != nil {
1216 panic("renegotiateCiphers without renegotiate")
1217 }
1218
David Benjamin5fa3eba2015-01-22 16:35:40 -05001219 if test.damageFirstWrite {
1220 connDamage.setDamage(true)
1221 tlsConn.Write([]byte("DAMAGED WRITE"))
1222 connDamage.setDamage(false)
1223 }
1224
Kenny Root7fdeaf12014-08-05 15:23:37 -07001225 if messageLen < 0 {
David Benjamin6fd297b2014-08-11 18:43:38 -04001226 if test.protocol == dtls {
1227 return fmt.Errorf("messageLen < 0 not supported for DTLS tests")
1228 }
Kenny Root7fdeaf12014-08-05 15:23:37 -07001229 // Read until EOF.
1230 _, err := io.Copy(ioutil.Discard, tlsConn)
1231 return err
1232 }
1233
David Benjamin4417d052015-04-05 04:17:25 -04001234 if messageLen == 0 {
1235 messageLen = 32
Adam Langley80842bd2014-06-20 12:00:00 -07001236 }
David Benjamin4417d052015-04-05 04:17:25 -04001237 testMessage := make([]byte, messageLen)
1238 for i := range testMessage {
1239 testMessage[i] = 0x42
1240 }
1241 tlsConn.Write(testMessage)
Adam Langley95c29f32014-06-20 12:00:00 -07001242
1243 buf := make([]byte, len(testMessage))
David Benjamin6fd297b2014-08-11 18:43:38 -04001244 if test.protocol == dtls {
1245 bufTmp := make([]byte, len(buf)+1)
1246 n, err := tlsConn.Read(bufTmp)
1247 if err != nil {
1248 return err
1249 }
1250 if n != len(buf) {
1251 return fmt.Errorf("bad reply; length mismatch (%d vs %d)", n, len(buf))
1252 }
1253 copy(buf, bufTmp)
1254 } else {
1255 _, err := io.ReadFull(tlsConn, buf)
1256 if err != nil {
1257 return err
1258 }
Adam Langley95c29f32014-06-20 12:00:00 -07001259 }
1260
1261 for i, v := range buf {
1262 if v != testMessage[i]^0xff {
1263 return fmt.Errorf("bad reply contents at byte %d", i)
1264 }
1265 }
1266
1267 return nil
1268}
1269
David Benjamin325b5c32014-07-01 19:40:31 -04001270func valgrindOf(dbAttach bool, path string, args ...string) *exec.Cmd {
1271 valgrindArgs := []string{"--error-exitcode=99", "--track-origins=yes", "--leak-check=full"}
Adam Langley95c29f32014-06-20 12:00:00 -07001272 if dbAttach {
David Benjamin325b5c32014-07-01 19:40:31 -04001273 valgrindArgs = append(valgrindArgs, "--db-attach=yes", "--db-command=xterm -e gdb -nw %f %p")
Adam Langley95c29f32014-06-20 12:00:00 -07001274 }
David Benjamin325b5c32014-07-01 19:40:31 -04001275 valgrindArgs = append(valgrindArgs, path)
1276 valgrindArgs = append(valgrindArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -07001277
David Benjamin325b5c32014-07-01 19:40:31 -04001278 return exec.Command("valgrind", valgrindArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -07001279}
1280
David Benjamin325b5c32014-07-01 19:40:31 -04001281func gdbOf(path string, args ...string) *exec.Cmd {
1282 xtermArgs := []string{"-e", "gdb", "--args"}
1283 xtermArgs = append(xtermArgs, path)
1284 xtermArgs = append(xtermArgs, args...)
Adam Langley95c29f32014-06-20 12:00:00 -07001285
David Benjamin325b5c32014-07-01 19:40:31 -04001286 return exec.Command("xterm", xtermArgs...)
Adam Langley95c29f32014-06-20 12:00:00 -07001287}
1288
Adam Langley69a01602014-11-17 17:26:55 -08001289type moreMallocsError struct{}
1290
1291func (moreMallocsError) Error() string {
1292 return "child process did not exhaust all allocation calls"
1293}
1294
1295var errMoreMallocs = moreMallocsError{}
1296
David Benjamin87c8a642015-02-21 01:54:29 -05001297// accept accepts a connection from listener, unless waitChan signals a process
1298// exit first.
1299func acceptOrWait(listener net.Listener, waitChan chan error) (net.Conn, error) {
1300 type connOrError struct {
1301 conn net.Conn
1302 err error
1303 }
1304 connChan := make(chan connOrError, 1)
1305 go func() {
1306 conn, err := listener.Accept()
1307 connChan <- connOrError{conn, err}
1308 close(connChan)
1309 }()
1310 select {
1311 case result := <-connChan:
1312 return result.conn, result.err
1313 case childErr := <-waitChan:
1314 waitChan <- childErr
1315 return nil, fmt.Errorf("child exited early: %s", childErr)
1316 }
1317}
1318
Adam Langley69a01602014-11-17 17:26:55 -08001319func runTest(test *testCase, buildDir string, mallocNumToFail int64) error {
Adam Langley38311732014-10-16 19:04:35 -07001320 if !test.shouldFail && (len(test.expectedError) > 0 || len(test.expectedLocalError) > 0) {
1321 panic("Error expected without shouldFail in " + test.name)
1322 }
1323
David Benjamin87c8a642015-02-21 01:54:29 -05001324 listener, err := net.ListenTCP("tcp4", &net.TCPAddr{IP: net.IP{127, 0, 0, 1}})
1325 if err != nil {
1326 panic(err)
1327 }
1328 defer func() {
1329 if listener != nil {
1330 listener.Close()
1331 }
1332 }()
Adam Langley95c29f32014-06-20 12:00:00 -07001333
David Benjamin884fdf12014-08-02 15:28:23 -04001334 shim_path := path.Join(buildDir, "ssl/test/bssl_shim")
David Benjamin87c8a642015-02-21 01:54:29 -05001335 flags := []string{"-port", strconv.Itoa(listener.Addr().(*net.TCPAddr).Port)}
David Benjamin1d5c83e2014-07-22 19:20:02 -04001336 if test.testType == serverTest {
David Benjamin5a593af2014-08-11 19:51:50 -04001337 flags = append(flags, "-server")
1338
David Benjamin025b3d32014-07-01 19:53:04 -04001339 flags = append(flags, "-key-file")
1340 if test.keyFile == "" {
1341 flags = append(flags, rsaKeyFile)
1342 } else {
1343 flags = append(flags, test.keyFile)
1344 }
1345
1346 flags = append(flags, "-cert-file")
1347 if test.certFile == "" {
1348 flags = append(flags, rsaCertificateFile)
1349 } else {
1350 flags = append(flags, test.certFile)
1351 }
1352 }
David Benjamin5a593af2014-08-11 19:51:50 -04001353
David Benjamin6fd297b2014-08-11 18:43:38 -04001354 if test.protocol == dtls {
1355 flags = append(flags, "-dtls")
1356 }
1357
David Benjamin5a593af2014-08-11 19:51:50 -04001358 if test.resumeSession {
1359 flags = append(flags, "-resume")
1360 }
1361
David Benjamine58c4f52014-08-24 03:47:07 -04001362 if test.shimWritesFirst {
1363 flags = append(flags, "-shim-writes-first")
1364 }
1365
David Benjaminc565ebb2015-04-03 04:06:36 -04001366 if test.exportKeyingMaterial > 0 {
1367 flags = append(flags, "-export-keying-material", strconv.Itoa(test.exportKeyingMaterial))
1368 flags = append(flags, "-export-label", test.exportLabel)
1369 flags = append(flags, "-export-context", test.exportContext)
1370 if test.useExportContext {
1371 flags = append(flags, "-use-export-context")
1372 }
1373 }
1374
David Benjamin025b3d32014-07-01 19:53:04 -04001375 flags = append(flags, test.flags...)
1376
1377 var shim *exec.Cmd
1378 if *useValgrind {
1379 shim = valgrindOf(false, shim_path, flags...)
Adam Langley75712922014-10-10 16:23:43 -07001380 } else if *useGDB {
1381 shim = gdbOf(shim_path, flags...)
David Benjamin025b3d32014-07-01 19:53:04 -04001382 } else {
1383 shim = exec.Command(shim_path, flags...)
1384 }
David Benjamin025b3d32014-07-01 19:53:04 -04001385 shim.Stdin = os.Stdin
1386 var stdoutBuf, stderrBuf bytes.Buffer
1387 shim.Stdout = &stdoutBuf
1388 shim.Stderr = &stderrBuf
Adam Langley69a01602014-11-17 17:26:55 -08001389 if mallocNumToFail >= 0 {
David Benjamin9e128b02015-02-09 13:13:09 -05001390 shim.Env = os.Environ()
1391 shim.Env = append(shim.Env, "MALLOC_NUMBER_TO_FAIL="+strconv.FormatInt(mallocNumToFail, 10))
Adam Langley69a01602014-11-17 17:26:55 -08001392 if *mallocTestDebug {
1393 shim.Env = append(shim.Env, "MALLOC_ABORT_ON_FAIL=1")
1394 }
1395 shim.Env = append(shim.Env, "_MALLOC_CHECK=1")
1396 }
David Benjamin025b3d32014-07-01 19:53:04 -04001397
1398 if err := shim.Start(); err != nil {
Adam Langley95c29f32014-06-20 12:00:00 -07001399 panic(err)
1400 }
David Benjamin87c8a642015-02-21 01:54:29 -05001401 waitChan := make(chan error, 1)
1402 go func() { waitChan <- shim.Wait() }()
Adam Langley95c29f32014-06-20 12:00:00 -07001403
1404 config := test.config
David Benjamin1d5c83e2014-07-22 19:20:02 -04001405 config.ClientSessionCache = NewLRUClientSessionCache(1)
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001406 config.ServerSessionCache = NewLRUServerSessionCache(1)
David Benjamin025b3d32014-07-01 19:53:04 -04001407 if test.testType == clientTest {
1408 if len(config.Certificates) == 0 {
1409 config.Certificates = []Certificate{getRSACertificate()}
1410 }
David Benjamin87c8a642015-02-21 01:54:29 -05001411 } else {
1412 // Supply a ServerName to ensure a constant session cache key,
1413 // rather than falling back to net.Conn.RemoteAddr.
1414 if len(config.ServerName) == 0 {
1415 config.ServerName = "test"
1416 }
David Benjamin025b3d32014-07-01 19:53:04 -04001417 }
Adam Langley95c29f32014-06-20 12:00:00 -07001418
David Benjamin87c8a642015-02-21 01:54:29 -05001419 conn, err := acceptOrWait(listener, waitChan)
1420 if err == nil {
1421 err = doExchange(test, &config, conn, test.messageLen, false /* not a resumption */)
1422 conn.Close()
1423 }
David Benjamin65ea8ff2014-11-23 03:01:00 -05001424
David Benjamin1d5c83e2014-07-22 19:20:02 -04001425 if err == nil && test.resumeSession {
David Benjamin01fe8202014-09-24 15:21:44 -04001426 var resumeConfig Config
1427 if test.resumeConfig != nil {
1428 resumeConfig = *test.resumeConfig
David Benjamin87c8a642015-02-21 01:54:29 -05001429 if len(resumeConfig.ServerName) == 0 {
1430 resumeConfig.ServerName = config.ServerName
1431 }
David Benjamin01fe8202014-09-24 15:21:44 -04001432 if len(resumeConfig.Certificates) == 0 {
1433 resumeConfig.Certificates = []Certificate{getRSACertificate()}
1434 }
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001435 if !test.newSessionsOnResume {
1436 resumeConfig.SessionTicketKey = config.SessionTicketKey
1437 resumeConfig.ClientSessionCache = config.ClientSessionCache
1438 resumeConfig.ServerSessionCache = config.ServerSessionCache
1439 }
David Benjamin01fe8202014-09-24 15:21:44 -04001440 } else {
1441 resumeConfig = config
1442 }
David Benjamin87c8a642015-02-21 01:54:29 -05001443 var connResume net.Conn
1444 connResume, err = acceptOrWait(listener, waitChan)
1445 if err == nil {
1446 err = doExchange(test, &resumeConfig, connResume, test.messageLen, true /* resumption */)
1447 connResume.Close()
1448 }
David Benjamin1d5c83e2014-07-22 19:20:02 -04001449 }
1450
David Benjamin87c8a642015-02-21 01:54:29 -05001451 // Close the listener now. This is to avoid hangs should the shim try to
1452 // open more connections than expected.
1453 listener.Close()
1454 listener = nil
1455
1456 childErr := <-waitChan
Adam Langley69a01602014-11-17 17:26:55 -08001457 if exitError, ok := childErr.(*exec.ExitError); ok {
1458 if exitError.Sys().(syscall.WaitStatus).ExitStatus() == 88 {
1459 return errMoreMallocs
1460 }
1461 }
Adam Langley95c29f32014-06-20 12:00:00 -07001462
1463 stdout := string(stdoutBuf.Bytes())
1464 stderr := string(stderrBuf.Bytes())
1465 failed := err != nil || childErr != nil
David Benjaminc565ebb2015-04-03 04:06:36 -04001466 correctFailure := len(test.expectedError) == 0 || strings.Contains(stderr, test.expectedError)
Adam Langleyac61fa32014-06-23 12:03:11 -07001467 localError := "none"
1468 if err != nil {
1469 localError = err.Error()
1470 }
1471 if len(test.expectedLocalError) != 0 {
1472 correctFailure = correctFailure && strings.Contains(localError, test.expectedLocalError)
1473 }
Adam Langley95c29f32014-06-20 12:00:00 -07001474
1475 if failed != test.shouldFail || failed && !correctFailure {
Adam Langley95c29f32014-06-20 12:00:00 -07001476 childError := "none"
Adam Langley95c29f32014-06-20 12:00:00 -07001477 if childErr != nil {
1478 childError = childErr.Error()
1479 }
1480
1481 var msg string
1482 switch {
1483 case failed && !test.shouldFail:
1484 msg = "unexpected failure"
1485 case !failed && test.shouldFail:
1486 msg = "unexpected success"
1487 case failed && !correctFailure:
Adam Langleyac61fa32014-06-23 12:03:11 -07001488 msg = "bad error (wanted '" + test.expectedError + "' / '" + test.expectedLocalError + "')"
Adam Langley95c29f32014-06-20 12:00:00 -07001489 default:
1490 panic("internal error")
1491 }
1492
David Benjaminc565ebb2015-04-03 04:06:36 -04001493 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 -07001494 }
1495
David Benjaminc565ebb2015-04-03 04:06:36 -04001496 if !*useValgrind && !failed && len(stderr) > 0 {
Adam Langley95c29f32014-06-20 12:00:00 -07001497 println(stderr)
1498 }
1499
1500 return nil
1501}
1502
1503var tlsVersions = []struct {
1504 name string
1505 version uint16
David Benjamin7e2e6cf2014-08-07 17:44:24 -04001506 flag string
David Benjamin8b8c0062014-11-23 02:47:52 -05001507 hasDTLS bool
Adam Langley95c29f32014-06-20 12:00:00 -07001508}{
David Benjamin8b8c0062014-11-23 02:47:52 -05001509 {"SSL3", VersionSSL30, "-no-ssl3", false},
1510 {"TLS1", VersionTLS10, "-no-tls1", true},
1511 {"TLS11", VersionTLS11, "-no-tls11", false},
1512 {"TLS12", VersionTLS12, "-no-tls12", true},
Adam Langley95c29f32014-06-20 12:00:00 -07001513}
1514
1515var testCipherSuites = []struct {
1516 name string
1517 id uint16
1518}{
1519 {"3DES-SHA", TLS_RSA_WITH_3DES_EDE_CBC_SHA},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001520 {"AES128-GCM", TLS_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001521 {"AES128-SHA", TLS_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001522 {"AES128-SHA256", TLS_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001523 {"AES256-GCM", TLS_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001524 {"AES256-SHA", TLS_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001525 {"AES256-SHA256", TLS_RSA_WITH_AES_256_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001526 {"DHE-RSA-AES128-GCM", TLS_DHE_RSA_WITH_AES_128_GCM_SHA256},
1527 {"DHE-RSA-AES128-SHA", TLS_DHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001528 {"DHE-RSA-AES128-SHA256", TLS_DHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001529 {"DHE-RSA-AES256-GCM", TLS_DHE_RSA_WITH_AES_256_GCM_SHA384},
1530 {"DHE-RSA-AES256-SHA", TLS_DHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001531 {"DHE-RSA-AES256-SHA256", TLS_DHE_RSA_WITH_AES_256_CBC_SHA256},
David Benjamine9a80ff2015-04-07 00:46:46 -04001532 {"DHE-RSA-CHACHA20-POLY1305", TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001533 {"ECDHE-ECDSA-AES128-GCM", TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
1534 {"ECDHE-ECDSA-AES128-SHA", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001535 {"ECDHE-ECDSA-AES128-SHA256", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256},
1536 {"ECDHE-ECDSA-AES256-GCM", TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001537 {"ECDHE-ECDSA-AES256-SHA", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001538 {"ECDHE-ECDSA-AES256-SHA384", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384},
David Benjamine9a80ff2015-04-07 00:46:46 -04001539 {"ECDHE-ECDSA-CHACHA20-POLY1305", TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001540 {"ECDHE-ECDSA-RC4-SHA", TLS_ECDHE_ECDSA_WITH_RC4_128_SHA},
Adam Langley97e8ba82015-04-29 15:32:10 -07001541 {"ECDHE-PSK-AES128-GCM-SHA256", TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001542 {"ECDHE-RSA-AES128-GCM", TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001543 {"ECDHE-RSA-AES128-SHA", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001544 {"ECDHE-RSA-AES128-SHA256", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001545 {"ECDHE-RSA-AES256-GCM", TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384},
Adam Langley95c29f32014-06-20 12:00:00 -07001546 {"ECDHE-RSA-AES256-SHA", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA},
David Benjaminf7768e42014-08-31 02:06:47 -04001547 {"ECDHE-RSA-AES256-SHA384", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384},
David Benjamine9a80ff2015-04-07 00:46:46 -04001548 {"ECDHE-RSA-CHACHA20-POLY1305", TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256},
Adam Langley95c29f32014-06-20 12:00:00 -07001549 {"ECDHE-RSA-RC4-SHA", TLS_ECDHE_RSA_WITH_RC4_128_SHA},
David Benjamin48cae082014-10-27 01:06:24 -04001550 {"PSK-AES128-CBC-SHA", TLS_PSK_WITH_AES_128_CBC_SHA},
1551 {"PSK-AES256-CBC-SHA", TLS_PSK_WITH_AES_256_CBC_SHA},
1552 {"PSK-RC4-SHA", TLS_PSK_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -07001553 {"RC4-MD5", TLS_RSA_WITH_RC4_128_MD5},
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001554 {"RC4-SHA", TLS_RSA_WITH_RC4_128_SHA},
Adam Langley95c29f32014-06-20 12:00:00 -07001555}
1556
David Benjamin8b8c0062014-11-23 02:47:52 -05001557func hasComponent(suiteName, component string) bool {
1558 return strings.Contains("-"+suiteName+"-", "-"+component+"-")
1559}
1560
David Benjaminf7768e42014-08-31 02:06:47 -04001561func isTLS12Only(suiteName string) bool {
David Benjamin8b8c0062014-11-23 02:47:52 -05001562 return hasComponent(suiteName, "GCM") ||
1563 hasComponent(suiteName, "SHA256") ||
David Benjamine9a80ff2015-04-07 00:46:46 -04001564 hasComponent(suiteName, "SHA384") ||
1565 hasComponent(suiteName, "POLY1305")
David Benjamin8b8c0062014-11-23 02:47:52 -05001566}
1567
1568func isDTLSCipher(suiteName string) bool {
David Benjamine95d20d2014-12-23 11:16:01 -05001569 return !hasComponent(suiteName, "RC4")
David Benjaminf7768e42014-08-31 02:06:47 -04001570}
1571
Adam Langley95c29f32014-06-20 12:00:00 -07001572func addCipherSuiteTests() {
1573 for _, suite := range testCipherSuites {
David Benjamin48cae082014-10-27 01:06:24 -04001574 const psk = "12345"
1575 const pskIdentity = "luggage combo"
1576
Adam Langley95c29f32014-06-20 12:00:00 -07001577 var cert Certificate
David Benjamin025b3d32014-07-01 19:53:04 -04001578 var certFile string
1579 var keyFile string
David Benjamin8b8c0062014-11-23 02:47:52 -05001580 if hasComponent(suite.name, "ECDSA") {
Adam Langley95c29f32014-06-20 12:00:00 -07001581 cert = getECDSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -04001582 certFile = ecdsaCertificateFile
1583 keyFile = ecdsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -07001584 } else {
1585 cert = getRSACertificate()
David Benjamin025b3d32014-07-01 19:53:04 -04001586 certFile = rsaCertificateFile
1587 keyFile = rsaKeyFile
Adam Langley95c29f32014-06-20 12:00:00 -07001588 }
1589
David Benjamin48cae082014-10-27 01:06:24 -04001590 var flags []string
David Benjamin8b8c0062014-11-23 02:47:52 -05001591 if hasComponent(suite.name, "PSK") {
David Benjamin48cae082014-10-27 01:06:24 -04001592 flags = append(flags,
1593 "-psk", psk,
1594 "-psk-identity", pskIdentity)
1595 }
1596
Adam Langley95c29f32014-06-20 12:00:00 -07001597 for _, ver := range tlsVersions {
David Benjaminf7768e42014-08-31 02:06:47 -04001598 if ver.version < VersionTLS12 && isTLS12Only(suite.name) {
Adam Langley95c29f32014-06-20 12:00:00 -07001599 continue
1600 }
1601
David Benjamin025b3d32014-07-01 19:53:04 -04001602 testCases = append(testCases, testCase{
1603 testType: clientTest,
1604 name: ver.name + "-" + suite.name + "-client",
Adam Langley95c29f32014-06-20 12:00:00 -07001605 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001606 MinVersion: ver.version,
1607 MaxVersion: ver.version,
1608 CipherSuites: []uint16{suite.id},
1609 Certificates: []Certificate{cert},
David Benjamin68793732015-05-04 20:20:48 -04001610 PreSharedKey: []byte(psk),
David Benjamin48cae082014-10-27 01:06:24 -04001611 PreSharedKeyIdentity: pskIdentity,
Adam Langley95c29f32014-06-20 12:00:00 -07001612 },
David Benjamin48cae082014-10-27 01:06:24 -04001613 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001614 resumeSession: true,
Adam Langley95c29f32014-06-20 12:00:00 -07001615 })
David Benjamin025b3d32014-07-01 19:53:04 -04001616
David Benjamin76d8abe2014-08-14 16:25:34 -04001617 testCases = append(testCases, testCase{
1618 testType: serverTest,
1619 name: ver.name + "-" + suite.name + "-server",
1620 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001621 MinVersion: ver.version,
1622 MaxVersion: ver.version,
1623 CipherSuites: []uint16{suite.id},
1624 Certificates: []Certificate{cert},
1625 PreSharedKey: []byte(psk),
1626 PreSharedKeyIdentity: pskIdentity,
David Benjamin76d8abe2014-08-14 16:25:34 -04001627 },
1628 certFile: certFile,
1629 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001630 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001631 resumeSession: true,
David Benjamin76d8abe2014-08-14 16:25:34 -04001632 })
David Benjamin6fd297b2014-08-11 18:43:38 -04001633
David Benjamin8b8c0062014-11-23 02:47:52 -05001634 if ver.hasDTLS && isDTLSCipher(suite.name) {
David Benjamin6fd297b2014-08-11 18:43:38 -04001635 testCases = append(testCases, testCase{
1636 testType: clientTest,
1637 protocol: dtls,
1638 name: "D" + ver.name + "-" + suite.name + "-client",
1639 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001640 MinVersion: ver.version,
1641 MaxVersion: ver.version,
1642 CipherSuites: []uint16{suite.id},
1643 Certificates: []Certificate{cert},
1644 PreSharedKey: []byte(psk),
1645 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001646 },
David Benjamin48cae082014-10-27 01:06:24 -04001647 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001648 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001649 })
1650 testCases = append(testCases, testCase{
1651 testType: serverTest,
1652 protocol: dtls,
1653 name: "D" + ver.name + "-" + suite.name + "-server",
1654 config: Config{
David Benjamin48cae082014-10-27 01:06:24 -04001655 MinVersion: ver.version,
1656 MaxVersion: ver.version,
1657 CipherSuites: []uint16{suite.id},
1658 Certificates: []Certificate{cert},
1659 PreSharedKey: []byte(psk),
1660 PreSharedKeyIdentity: pskIdentity,
David Benjamin6fd297b2014-08-11 18:43:38 -04001661 },
1662 certFile: certFile,
1663 keyFile: keyFile,
David Benjamin48cae082014-10-27 01:06:24 -04001664 flags: flags,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001665 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04001666 })
1667 }
Adam Langley95c29f32014-06-20 12:00:00 -07001668 }
1669 }
1670}
1671
1672func addBadECDSASignatureTests() {
1673 for badR := BadValue(1); badR < NumBadValues; badR++ {
1674 for badS := BadValue(1); badS < NumBadValues; badS++ {
David Benjamin025b3d32014-07-01 19:53:04 -04001675 testCases = append(testCases, testCase{
Adam Langley95c29f32014-06-20 12:00:00 -07001676 name: fmt.Sprintf("BadECDSA-%d-%d", badR, badS),
1677 config: Config{
1678 CipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
1679 Certificates: []Certificate{getECDSACertificate()},
1680 Bugs: ProtocolBugs{
1681 BadECDSAR: badR,
1682 BadECDSAS: badS,
1683 },
1684 },
1685 shouldFail: true,
1686 expectedError: "SIGNATURE",
1687 })
1688 }
1689 }
1690}
1691
Adam Langley80842bd2014-06-20 12:00:00 -07001692func addCBCPaddingTests() {
David Benjamin025b3d32014-07-01 19:53:04 -04001693 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001694 name: "MaxCBCPadding",
1695 config: Config{
1696 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1697 Bugs: ProtocolBugs{
1698 MaxPadding: true,
1699 },
1700 },
1701 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1702 })
David Benjamin025b3d32014-07-01 19:53:04 -04001703 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001704 name: "BadCBCPadding",
1705 config: Config{
1706 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1707 Bugs: ProtocolBugs{
1708 PaddingFirstByteBad: true,
1709 },
1710 },
1711 shouldFail: true,
1712 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1713 })
1714 // OpenSSL previously had an issue where the first byte of padding in
1715 // 255 bytes of padding wasn't checked.
David Benjamin025b3d32014-07-01 19:53:04 -04001716 testCases = append(testCases, testCase{
Adam Langley80842bd2014-06-20 12:00:00 -07001717 name: "BadCBCPadding255",
1718 config: Config{
1719 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1720 Bugs: ProtocolBugs{
1721 MaxPadding: true,
1722 PaddingFirstByteBadIf255: true,
1723 },
1724 },
1725 messageLen: 12, // 20 bytes of SHA-1 + 12 == 0 % block size
1726 shouldFail: true,
1727 expectedError: "DECRYPTION_FAILED_OR_BAD_RECORD_MAC",
1728 })
1729}
1730
Kenny Root7fdeaf12014-08-05 15:23:37 -07001731func addCBCSplittingTests() {
1732 testCases = append(testCases, testCase{
1733 name: "CBCRecordSplitting",
1734 config: Config{
1735 MaxVersion: VersionTLS10,
1736 MinVersion: VersionTLS10,
1737 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1738 },
1739 messageLen: -1, // read until EOF
1740 flags: []string{
1741 "-async",
1742 "-write-different-record-sizes",
1743 "-cbc-record-splitting",
1744 },
David Benjamina8e3e0e2014-08-06 22:11:10 -04001745 })
1746 testCases = append(testCases, testCase{
Kenny Root7fdeaf12014-08-05 15:23:37 -07001747 name: "CBCRecordSplittingPartialWrite",
1748 config: Config{
1749 MaxVersion: VersionTLS10,
1750 MinVersion: VersionTLS10,
1751 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
1752 },
1753 messageLen: -1, // read until EOF
1754 flags: []string{
1755 "-async",
1756 "-write-different-record-sizes",
1757 "-cbc-record-splitting",
1758 "-partial-write",
1759 },
1760 })
1761}
1762
David Benjamin636293b2014-07-08 17:59:18 -04001763func addClientAuthTests() {
David Benjamin407a10c2014-07-16 12:58:59 -04001764 // Add a dummy cert pool to stress certificate authority parsing.
1765 // TODO(davidben): Add tests that those values parse out correctly.
1766 certPool := x509.NewCertPool()
1767 cert, err := x509.ParseCertificate(rsaCertificate.Certificate[0])
1768 if err != nil {
1769 panic(err)
1770 }
1771 certPool.AddCert(cert)
1772
David Benjamin636293b2014-07-08 17:59:18 -04001773 for _, ver := range tlsVersions {
David Benjamin636293b2014-07-08 17:59:18 -04001774 testCases = append(testCases, testCase{
1775 testType: clientTest,
David Benjamin67666e72014-07-12 15:47:52 -04001776 name: ver.name + "-Client-ClientAuth-RSA",
David Benjamin636293b2014-07-08 17:59:18 -04001777 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001778 MinVersion: ver.version,
1779 MaxVersion: ver.version,
1780 ClientAuth: RequireAnyClientCert,
1781 ClientCAs: certPool,
David Benjamin636293b2014-07-08 17:59:18 -04001782 },
1783 flags: []string{
1784 "-cert-file", rsaCertificateFile,
1785 "-key-file", rsaKeyFile,
1786 },
1787 })
1788 testCases = append(testCases, testCase{
David Benjamin67666e72014-07-12 15:47:52 -04001789 testType: serverTest,
1790 name: ver.name + "-Server-ClientAuth-RSA",
1791 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04001792 MinVersion: ver.version,
1793 MaxVersion: ver.version,
David Benjamin67666e72014-07-12 15:47:52 -04001794 Certificates: []Certificate{rsaCertificate},
1795 },
1796 flags: []string{"-require-any-client-certificate"},
1797 })
David Benjamine098ec22014-08-27 23:13:20 -04001798 if ver.version != VersionSSL30 {
1799 testCases = append(testCases, testCase{
1800 testType: serverTest,
1801 name: ver.name + "-Server-ClientAuth-ECDSA",
1802 config: Config{
1803 MinVersion: ver.version,
1804 MaxVersion: ver.version,
1805 Certificates: []Certificate{ecdsaCertificate},
1806 },
1807 flags: []string{"-require-any-client-certificate"},
1808 })
1809 testCases = append(testCases, testCase{
1810 testType: clientTest,
1811 name: ver.name + "-Client-ClientAuth-ECDSA",
1812 config: Config{
1813 MinVersion: ver.version,
1814 MaxVersion: ver.version,
1815 ClientAuth: RequireAnyClientCert,
1816 ClientCAs: certPool,
1817 },
1818 flags: []string{
1819 "-cert-file", ecdsaCertificateFile,
1820 "-key-file", ecdsaKeyFile,
1821 },
1822 })
1823 }
David Benjamin636293b2014-07-08 17:59:18 -04001824 }
1825}
1826
Adam Langley75712922014-10-10 16:23:43 -07001827func addExtendedMasterSecretTests() {
1828 const expectEMSFlag = "-expect-extended-master-secret"
1829
1830 for _, with := range []bool{false, true} {
1831 prefix := "No"
1832 var flags []string
1833 if with {
1834 prefix = ""
1835 flags = []string{expectEMSFlag}
1836 }
1837
1838 for _, isClient := range []bool{false, true} {
1839 suffix := "-Server"
1840 testType := serverTest
1841 if isClient {
1842 suffix = "-Client"
1843 testType = clientTest
1844 }
1845
1846 for _, ver := range tlsVersions {
1847 test := testCase{
1848 testType: testType,
1849 name: prefix + "ExtendedMasterSecret-" + ver.name + suffix,
1850 config: Config{
1851 MinVersion: ver.version,
1852 MaxVersion: ver.version,
1853 Bugs: ProtocolBugs{
1854 NoExtendedMasterSecret: !with,
1855 RequireExtendedMasterSecret: with,
1856 },
1857 },
David Benjamin48cae082014-10-27 01:06:24 -04001858 flags: flags,
1859 shouldFail: ver.version == VersionSSL30 && with,
Adam Langley75712922014-10-10 16:23:43 -07001860 }
1861 if test.shouldFail {
1862 test.expectedLocalError = "extended master secret required but not supported by peer"
1863 }
1864 testCases = append(testCases, test)
1865 }
1866 }
1867 }
1868
1869 // When a session is resumed, it should still be aware that its master
1870 // secret was generated via EMS and thus it's safe to use tls-unique.
1871 testCases = append(testCases, testCase{
1872 name: "ExtendedMasterSecret-Resume",
1873 config: Config{
1874 Bugs: ProtocolBugs{
1875 RequireExtendedMasterSecret: true,
1876 },
1877 },
1878 flags: []string{expectEMSFlag},
1879 resumeSession: true,
1880 })
1881}
1882
David Benjamin43ec06f2014-08-05 02:28:57 -04001883// Adds tests that try to cover the range of the handshake state machine, under
1884// various conditions. Some of these are redundant with other tests, but they
1885// only cover the synchronous case.
David Benjamin6fd297b2014-08-11 18:43:38 -04001886func addStateMachineCoverageTests(async, splitHandshake bool, protocol protocol) {
David Benjamin43ec06f2014-08-05 02:28:57 -04001887 var suffix string
1888 var flags []string
1889 var maxHandshakeRecordLength int
David Benjamin6fd297b2014-08-11 18:43:38 -04001890 if protocol == dtls {
1891 suffix = "-DTLS"
1892 }
David Benjamin43ec06f2014-08-05 02:28:57 -04001893 if async {
David Benjamin6fd297b2014-08-11 18:43:38 -04001894 suffix += "-Async"
David Benjamin43ec06f2014-08-05 02:28:57 -04001895 flags = append(flags, "-async")
1896 } else {
David Benjamin6fd297b2014-08-11 18:43:38 -04001897 suffix += "-Sync"
David Benjamin43ec06f2014-08-05 02:28:57 -04001898 }
1899 if splitHandshake {
1900 suffix += "-SplitHandshakeRecords"
David Benjamin98214542014-08-07 18:02:39 -04001901 maxHandshakeRecordLength = 1
David Benjamin43ec06f2014-08-05 02:28:57 -04001902 }
1903
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001904 // Basic handshake, with resumption. Client and server,
1905 // session ID and session ticket.
David Benjamin43ec06f2014-08-05 02:28:57 -04001906 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001907 protocol: protocol,
1908 name: "Basic-Client" + suffix,
David Benjamin43ec06f2014-08-05 02:28:57 -04001909 config: Config{
1910 Bugs: ProtocolBugs{
1911 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1912 },
1913 },
David Benjaminbed9aae2014-08-07 19:13:38 -04001914 flags: flags,
1915 resumeSession: true,
1916 })
1917 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001918 protocol: protocol,
1919 name: "Basic-Client-RenewTicket" + suffix,
David Benjaminbed9aae2014-08-07 19:13:38 -04001920 config: Config{
1921 Bugs: ProtocolBugs{
1922 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1923 RenewTicketOnResume: true,
1924 },
1925 },
1926 flags: flags,
1927 resumeSession: true,
David Benjamin43ec06f2014-08-05 02:28:57 -04001928 })
1929 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04001930 protocol: protocol,
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001931 name: "Basic-Client-NoTicket" + suffix,
1932 config: Config{
1933 SessionTicketsDisabled: true,
1934 Bugs: ProtocolBugs{
1935 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1936 },
1937 },
1938 flags: flags,
1939 resumeSession: true,
1940 })
1941 testCases = append(testCases, testCase{
1942 protocol: protocol,
David Benjamine0e7d0d2015-02-08 19:33:25 -05001943 name: "Basic-Client-Implicit" + suffix,
1944 config: Config{
1945 Bugs: ProtocolBugs{
1946 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1947 },
1948 },
1949 flags: append(flags, "-implicit-handshake"),
1950 resumeSession: true,
1951 })
1952 testCases = append(testCases, testCase{
1953 protocol: protocol,
David Benjamin43ec06f2014-08-05 02:28:57 -04001954 testType: serverTest,
1955 name: "Basic-Server" + suffix,
1956 config: Config{
1957 Bugs: ProtocolBugs{
1958 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1959 },
1960 },
David Benjaminbed9aae2014-08-07 19:13:38 -04001961 flags: flags,
1962 resumeSession: true,
David Benjamin43ec06f2014-08-05 02:28:57 -04001963 })
David Benjaminfe8eb9a2014-11-17 03:19:02 -05001964 testCases = append(testCases, testCase{
1965 protocol: protocol,
1966 testType: serverTest,
1967 name: "Basic-Server-NoTickets" + suffix,
1968 config: Config{
1969 SessionTicketsDisabled: true,
1970 Bugs: ProtocolBugs{
1971 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1972 },
1973 },
1974 flags: flags,
1975 resumeSession: true,
1976 })
David Benjamine0e7d0d2015-02-08 19:33:25 -05001977 testCases = append(testCases, testCase{
1978 protocol: protocol,
1979 testType: serverTest,
1980 name: "Basic-Server-Implicit" + suffix,
1981 config: Config{
1982 Bugs: ProtocolBugs{
1983 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1984 },
1985 },
1986 flags: append(flags, "-implicit-handshake"),
1987 resumeSession: true,
1988 })
David Benjamin6f5c0f42015-02-24 01:23:21 -05001989 testCases = append(testCases, testCase{
1990 protocol: protocol,
1991 testType: serverTest,
1992 name: "Basic-Server-EarlyCallback" + suffix,
1993 config: Config{
1994 Bugs: ProtocolBugs{
1995 MaxHandshakeRecordLength: maxHandshakeRecordLength,
1996 },
1997 },
1998 flags: append(flags, "-use-early-callback"),
1999 resumeSession: true,
2000 })
David Benjamin43ec06f2014-08-05 02:28:57 -04002001
David Benjamin6fd297b2014-08-11 18:43:38 -04002002 // TLS client auth.
2003 testCases = append(testCases, testCase{
2004 protocol: protocol,
2005 testType: clientTest,
2006 name: "ClientAuth-Client" + suffix,
2007 config: Config{
David Benjamine098ec22014-08-27 23:13:20 -04002008 ClientAuth: RequireAnyClientCert,
David Benjamin6fd297b2014-08-11 18:43:38 -04002009 Bugs: ProtocolBugs{
2010 MaxHandshakeRecordLength: maxHandshakeRecordLength,
2011 },
2012 },
2013 flags: append(flags,
2014 "-cert-file", rsaCertificateFile,
2015 "-key-file", rsaKeyFile),
2016 })
2017 testCases = append(testCases, testCase{
2018 protocol: protocol,
2019 testType: serverTest,
2020 name: "ClientAuth-Server" + suffix,
2021 config: Config{
2022 Certificates: []Certificate{rsaCertificate},
2023 },
2024 flags: append(flags, "-require-any-client-certificate"),
2025 })
2026
David Benjamin43ec06f2014-08-05 02:28:57 -04002027 // No session ticket support; server doesn't send NewSessionTicket.
2028 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04002029 protocol: protocol,
2030 name: "SessionTicketsDisabled-Client" + suffix,
David Benjamin43ec06f2014-08-05 02:28:57 -04002031 config: Config{
2032 SessionTicketsDisabled: true,
2033 Bugs: ProtocolBugs{
2034 MaxHandshakeRecordLength: maxHandshakeRecordLength,
2035 },
2036 },
2037 flags: flags,
2038 })
2039 testCases = append(testCases, testCase{
David Benjamin6fd297b2014-08-11 18:43:38 -04002040 protocol: protocol,
David Benjamin43ec06f2014-08-05 02:28:57 -04002041 testType: serverTest,
2042 name: "SessionTicketsDisabled-Server" + suffix,
2043 config: Config{
2044 SessionTicketsDisabled: true,
2045 Bugs: ProtocolBugs{
2046 MaxHandshakeRecordLength: maxHandshakeRecordLength,
2047 },
2048 },
2049 flags: flags,
2050 })
2051
David Benjamin48cae082014-10-27 01:06:24 -04002052 // Skip ServerKeyExchange in PSK key exchange if there's no
2053 // identity hint.
2054 testCases = append(testCases, testCase{
2055 protocol: protocol,
2056 name: "EmptyPSKHint-Client" + suffix,
2057 config: Config{
2058 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
2059 PreSharedKey: []byte("secret"),
2060 Bugs: ProtocolBugs{
2061 MaxHandshakeRecordLength: maxHandshakeRecordLength,
2062 },
2063 },
2064 flags: append(flags, "-psk", "secret"),
2065 })
2066 testCases = append(testCases, testCase{
2067 protocol: protocol,
2068 testType: serverTest,
2069 name: "EmptyPSKHint-Server" + suffix,
2070 config: Config{
2071 CipherSuites: []uint16{TLS_PSK_WITH_AES_128_CBC_SHA},
2072 PreSharedKey: []byte("secret"),
2073 Bugs: ProtocolBugs{
2074 MaxHandshakeRecordLength: maxHandshakeRecordLength,
2075 },
2076 },
2077 flags: append(flags, "-psk", "secret"),
2078 })
2079
David Benjamin6fd297b2014-08-11 18:43:38 -04002080 if protocol == tls {
2081 // NPN on client and server; results in post-handshake message.
2082 testCases = append(testCases, testCase{
2083 protocol: protocol,
2084 name: "NPN-Client" + suffix,
2085 config: Config{
David Benjaminae2888f2014-09-06 12:58:58 -04002086 NextProtos: []string{"foo"},
David Benjamin6fd297b2014-08-11 18:43:38 -04002087 Bugs: ProtocolBugs{
2088 MaxHandshakeRecordLength: maxHandshakeRecordLength,
2089 },
David Benjamin43ec06f2014-08-05 02:28:57 -04002090 },
David Benjaminfc7b0862014-09-06 13:21:53 -04002091 flags: append(flags, "-select-next-proto", "foo"),
2092 expectedNextProto: "foo",
2093 expectedNextProtoType: npn,
David Benjamin6fd297b2014-08-11 18:43:38 -04002094 })
2095 testCases = append(testCases, testCase{
2096 protocol: protocol,
2097 testType: serverTest,
2098 name: "NPN-Server" + suffix,
2099 config: Config{
2100 NextProtos: []string{"bar"},
2101 Bugs: ProtocolBugs{
2102 MaxHandshakeRecordLength: maxHandshakeRecordLength,
2103 },
David Benjamin43ec06f2014-08-05 02:28:57 -04002104 },
David Benjamin6fd297b2014-08-11 18:43:38 -04002105 flags: append(flags,
2106 "-advertise-npn", "\x03foo\x03bar\x03baz",
2107 "-expect-next-proto", "bar"),
David Benjaminfc7b0862014-09-06 13:21:53 -04002108 expectedNextProto: "bar",
2109 expectedNextProtoType: npn,
David Benjamin6fd297b2014-08-11 18:43:38 -04002110 })
David Benjamin43ec06f2014-08-05 02:28:57 -04002111
David Benjamin195dc782015-02-19 13:27:05 -05002112 // TODO(davidben): Add tests for when False Start doesn't trigger.
2113
David Benjamin6fd297b2014-08-11 18:43:38 -04002114 // Client does False Start and negotiates NPN.
2115 testCases = append(testCases, testCase{
2116 protocol: protocol,
2117 name: "FalseStart" + suffix,
2118 config: Config{
2119 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2120 NextProtos: []string{"foo"},
2121 Bugs: ProtocolBugs{
David Benjamine58c4f52014-08-24 03:47:07 -04002122 ExpectFalseStart: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04002123 MaxHandshakeRecordLength: maxHandshakeRecordLength,
2124 },
David Benjamin43ec06f2014-08-05 02:28:57 -04002125 },
David Benjamin6fd297b2014-08-11 18:43:38 -04002126 flags: append(flags,
2127 "-false-start",
2128 "-select-next-proto", "foo"),
David Benjamine58c4f52014-08-24 03:47:07 -04002129 shimWritesFirst: true,
2130 resumeSession: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04002131 })
David Benjamin43ec06f2014-08-05 02:28:57 -04002132
David Benjaminae2888f2014-09-06 12:58:58 -04002133 // Client does False Start and negotiates ALPN.
2134 testCases = append(testCases, testCase{
2135 protocol: protocol,
2136 name: "FalseStart-ALPN" + suffix,
2137 config: Config{
2138 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2139 NextProtos: []string{"foo"},
2140 Bugs: ProtocolBugs{
2141 ExpectFalseStart: true,
2142 MaxHandshakeRecordLength: maxHandshakeRecordLength,
2143 },
2144 },
2145 flags: append(flags,
2146 "-false-start",
2147 "-advertise-alpn", "\x03foo"),
2148 shimWritesFirst: true,
2149 resumeSession: true,
2150 })
2151
David Benjamin931ab342015-02-08 19:46:57 -05002152 // Client does False Start but doesn't explicitly call
2153 // SSL_connect.
2154 testCases = append(testCases, testCase{
2155 protocol: protocol,
2156 name: "FalseStart-Implicit" + suffix,
2157 config: Config{
2158 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2159 NextProtos: []string{"foo"},
2160 Bugs: ProtocolBugs{
2161 MaxHandshakeRecordLength: maxHandshakeRecordLength,
2162 },
2163 },
2164 flags: append(flags,
2165 "-implicit-handshake",
2166 "-false-start",
2167 "-advertise-alpn", "\x03foo"),
2168 })
2169
David Benjamin6fd297b2014-08-11 18:43:38 -04002170 // False Start without session tickets.
2171 testCases = append(testCases, testCase{
David Benjamin5f237bc2015-02-11 17:14:15 -05002172 name: "FalseStart-SessionTicketsDisabled" + suffix,
David Benjamin6fd297b2014-08-11 18:43:38 -04002173 config: Config{
2174 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2175 NextProtos: []string{"foo"},
2176 SessionTicketsDisabled: true,
David Benjamin4e99c522014-08-24 01:45:30 -04002177 Bugs: ProtocolBugs{
David Benjamine58c4f52014-08-24 03:47:07 -04002178 ExpectFalseStart: true,
David Benjamin4e99c522014-08-24 01:45:30 -04002179 MaxHandshakeRecordLength: maxHandshakeRecordLength,
2180 },
David Benjamin43ec06f2014-08-05 02:28:57 -04002181 },
David Benjamin4e99c522014-08-24 01:45:30 -04002182 flags: append(flags,
David Benjamin6fd297b2014-08-11 18:43:38 -04002183 "-false-start",
2184 "-select-next-proto", "foo",
David Benjamin4e99c522014-08-24 01:45:30 -04002185 ),
David Benjamine58c4f52014-08-24 03:47:07 -04002186 shimWritesFirst: true,
David Benjamin6fd297b2014-08-11 18:43:38 -04002187 })
David Benjamin1e7f8d72014-08-08 12:27:04 -04002188
David Benjamina08e49d2014-08-24 01:46:07 -04002189 // Server parses a V2ClientHello.
David Benjamin6fd297b2014-08-11 18:43:38 -04002190 testCases = append(testCases, testCase{
2191 protocol: protocol,
2192 testType: serverTest,
2193 name: "SendV2ClientHello" + suffix,
2194 config: Config{
2195 // Choose a cipher suite that does not involve
2196 // elliptic curves, so no extensions are
2197 // involved.
2198 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2199 Bugs: ProtocolBugs{
2200 MaxHandshakeRecordLength: maxHandshakeRecordLength,
2201 SendV2ClientHello: true,
2202 },
David Benjamin1e7f8d72014-08-08 12:27:04 -04002203 },
David Benjamin6fd297b2014-08-11 18:43:38 -04002204 flags: flags,
2205 })
David Benjamina08e49d2014-08-24 01:46:07 -04002206
2207 // Client sends a Channel ID.
2208 testCases = append(testCases, testCase{
2209 protocol: protocol,
2210 name: "ChannelID-Client" + suffix,
2211 config: Config{
2212 RequestChannelID: true,
2213 Bugs: ProtocolBugs{
2214 MaxHandshakeRecordLength: maxHandshakeRecordLength,
2215 },
2216 },
2217 flags: append(flags,
2218 "-send-channel-id", channelIDKeyFile,
2219 ),
2220 resumeSession: true,
2221 expectChannelID: true,
2222 })
2223
2224 // Server accepts a Channel ID.
2225 testCases = append(testCases, testCase{
2226 protocol: protocol,
2227 testType: serverTest,
2228 name: "ChannelID-Server" + suffix,
2229 config: Config{
2230 ChannelID: channelIDKey,
2231 Bugs: ProtocolBugs{
2232 MaxHandshakeRecordLength: maxHandshakeRecordLength,
2233 },
2234 },
2235 flags: append(flags,
2236 "-expect-channel-id",
2237 base64.StdEncoding.EncodeToString(channelIDBytes),
2238 ),
2239 resumeSession: true,
2240 expectChannelID: true,
2241 })
David Benjamin6fd297b2014-08-11 18:43:38 -04002242 } else {
2243 testCases = append(testCases, testCase{
2244 protocol: protocol,
2245 name: "SkipHelloVerifyRequest" + suffix,
2246 config: Config{
2247 Bugs: ProtocolBugs{
2248 MaxHandshakeRecordLength: maxHandshakeRecordLength,
2249 SkipHelloVerifyRequest: true,
2250 },
2251 },
2252 flags: flags,
2253 })
David Benjamin6fd297b2014-08-11 18:43:38 -04002254 }
David Benjamin43ec06f2014-08-05 02:28:57 -04002255}
2256
Adam Langley524e7172015-02-20 16:04:00 -08002257func addDDoSCallbackTests() {
2258 // DDoS callback.
2259
2260 for _, resume := range []bool{false, true} {
2261 suffix := "Resume"
2262 if resume {
2263 suffix = "No" + suffix
2264 }
2265
2266 testCases = append(testCases, testCase{
2267 testType: serverTest,
2268 name: "Server-DDoS-OK-" + suffix,
2269 flags: []string{"-install-ddos-callback"},
2270 resumeSession: resume,
2271 })
2272
2273 failFlag := "-fail-ddos-callback"
2274 if resume {
2275 failFlag = "-fail-second-ddos-callback"
2276 }
2277 testCases = append(testCases, testCase{
2278 testType: serverTest,
2279 name: "Server-DDoS-Reject-" + suffix,
2280 flags: []string{"-install-ddos-callback", failFlag},
2281 resumeSession: resume,
2282 shouldFail: true,
2283 expectedError: ":CONNECTION_REJECTED:",
2284 })
2285 }
2286}
2287
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002288func addVersionNegotiationTests() {
2289 for i, shimVers := range tlsVersions {
2290 // Assemble flags to disable all newer versions on the shim.
2291 var flags []string
2292 for _, vers := range tlsVersions[i+1:] {
2293 flags = append(flags, vers.flag)
2294 }
2295
2296 for _, runnerVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05002297 protocols := []protocol{tls}
2298 if runnerVers.hasDTLS && shimVers.hasDTLS {
2299 protocols = append(protocols, dtls)
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002300 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002301 for _, protocol := range protocols {
2302 expectedVersion := shimVers.version
2303 if runnerVers.version < shimVers.version {
2304 expectedVersion = runnerVers.version
2305 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002306
David Benjamin8b8c0062014-11-23 02:47:52 -05002307 suffix := shimVers.name + "-" + runnerVers.name
2308 if protocol == dtls {
2309 suffix += "-DTLS"
2310 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002311
David Benjamin1eb367c2014-12-12 18:17:51 -05002312 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
2313
David Benjamin1e29a6b2014-12-10 02:27:24 -05002314 clientVers := shimVers.version
2315 if clientVers > VersionTLS10 {
2316 clientVers = VersionTLS10
2317 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002318 testCases = append(testCases, testCase{
2319 protocol: protocol,
2320 testType: clientTest,
2321 name: "VersionNegotiation-Client-" + suffix,
2322 config: Config{
2323 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05002324 Bugs: ProtocolBugs{
2325 ExpectInitialRecordVersion: clientVers,
2326 },
David Benjamin8b8c0062014-11-23 02:47:52 -05002327 },
2328 flags: flags,
2329 expectedVersion: expectedVersion,
2330 })
David Benjamin1eb367c2014-12-12 18:17:51 -05002331 testCases = append(testCases, testCase{
2332 protocol: protocol,
2333 testType: clientTest,
2334 name: "VersionNegotiation-Client2-" + suffix,
2335 config: Config{
2336 MaxVersion: runnerVers.version,
2337 Bugs: ProtocolBugs{
2338 ExpectInitialRecordVersion: clientVers,
2339 },
2340 },
2341 flags: []string{"-max-version", shimVersFlag},
2342 expectedVersion: expectedVersion,
2343 })
David Benjamin8b8c0062014-11-23 02:47:52 -05002344
2345 testCases = append(testCases, testCase{
2346 protocol: protocol,
2347 testType: serverTest,
2348 name: "VersionNegotiation-Server-" + suffix,
2349 config: Config{
2350 MaxVersion: runnerVers.version,
David Benjamin1e29a6b2014-12-10 02:27:24 -05002351 Bugs: ProtocolBugs{
2352 ExpectInitialRecordVersion: expectedVersion,
2353 },
David Benjamin8b8c0062014-11-23 02:47:52 -05002354 },
2355 flags: flags,
2356 expectedVersion: expectedVersion,
2357 })
David Benjamin1eb367c2014-12-12 18:17:51 -05002358 testCases = append(testCases, testCase{
2359 protocol: protocol,
2360 testType: serverTest,
2361 name: "VersionNegotiation-Server2-" + suffix,
2362 config: Config{
2363 MaxVersion: runnerVers.version,
2364 Bugs: ProtocolBugs{
2365 ExpectInitialRecordVersion: expectedVersion,
2366 },
2367 },
2368 flags: []string{"-max-version", shimVersFlag},
2369 expectedVersion: expectedVersion,
2370 })
David Benjamin8b8c0062014-11-23 02:47:52 -05002371 }
David Benjamin7e2e6cf2014-08-07 17:44:24 -04002372 }
2373 }
2374}
2375
David Benjaminaccb4542014-12-12 23:44:33 -05002376func addMinimumVersionTests() {
2377 for i, shimVers := range tlsVersions {
2378 // Assemble flags to disable all older versions on the shim.
2379 var flags []string
2380 for _, vers := range tlsVersions[:i] {
2381 flags = append(flags, vers.flag)
2382 }
2383
2384 for _, runnerVers := range tlsVersions {
2385 protocols := []protocol{tls}
2386 if runnerVers.hasDTLS && shimVers.hasDTLS {
2387 protocols = append(protocols, dtls)
2388 }
2389 for _, protocol := range protocols {
2390 suffix := shimVers.name + "-" + runnerVers.name
2391 if protocol == dtls {
2392 suffix += "-DTLS"
2393 }
2394 shimVersFlag := strconv.Itoa(int(versionToWire(shimVers.version, protocol == dtls)))
2395
David Benjaminaccb4542014-12-12 23:44:33 -05002396 var expectedVersion uint16
2397 var shouldFail bool
2398 var expectedError string
David Benjamin87909c02014-12-13 01:55:01 -05002399 var expectedLocalError string
David Benjaminaccb4542014-12-12 23:44:33 -05002400 if runnerVers.version >= shimVers.version {
2401 expectedVersion = runnerVers.version
2402 } else {
2403 shouldFail = true
2404 expectedError = ":UNSUPPORTED_PROTOCOL:"
David Benjamin87909c02014-12-13 01:55:01 -05002405 if runnerVers.version > VersionSSL30 {
2406 expectedLocalError = "remote error: protocol version not supported"
2407 } else {
2408 expectedLocalError = "remote error: handshake failure"
2409 }
David Benjaminaccb4542014-12-12 23:44:33 -05002410 }
2411
2412 testCases = append(testCases, testCase{
2413 protocol: protocol,
2414 testType: clientTest,
2415 name: "MinimumVersion-Client-" + suffix,
2416 config: Config{
2417 MaxVersion: runnerVers.version,
2418 },
David Benjamin87909c02014-12-13 01:55:01 -05002419 flags: flags,
2420 expectedVersion: expectedVersion,
2421 shouldFail: shouldFail,
2422 expectedError: expectedError,
2423 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002424 })
2425 testCases = append(testCases, testCase{
2426 protocol: protocol,
2427 testType: clientTest,
2428 name: "MinimumVersion-Client2-" + suffix,
2429 config: Config{
2430 MaxVersion: runnerVers.version,
2431 },
David Benjamin87909c02014-12-13 01:55:01 -05002432 flags: []string{"-min-version", shimVersFlag},
2433 expectedVersion: expectedVersion,
2434 shouldFail: shouldFail,
2435 expectedError: expectedError,
2436 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002437 })
2438
2439 testCases = append(testCases, testCase{
2440 protocol: protocol,
2441 testType: serverTest,
2442 name: "MinimumVersion-Server-" + suffix,
2443 config: Config{
2444 MaxVersion: runnerVers.version,
2445 },
David Benjamin87909c02014-12-13 01:55:01 -05002446 flags: flags,
2447 expectedVersion: expectedVersion,
2448 shouldFail: shouldFail,
2449 expectedError: expectedError,
2450 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002451 })
2452 testCases = append(testCases, testCase{
2453 protocol: protocol,
2454 testType: serverTest,
2455 name: "MinimumVersion-Server2-" + suffix,
2456 config: Config{
2457 MaxVersion: runnerVers.version,
2458 },
David Benjamin87909c02014-12-13 01:55:01 -05002459 flags: []string{"-min-version", shimVersFlag},
2460 expectedVersion: expectedVersion,
2461 shouldFail: shouldFail,
2462 expectedError: expectedError,
2463 expectedLocalError: expectedLocalError,
David Benjaminaccb4542014-12-12 23:44:33 -05002464 })
2465 }
2466 }
2467 }
2468}
2469
David Benjamin5c24a1d2014-08-31 00:59:27 -04002470func addD5BugTests() {
2471 testCases = append(testCases, testCase{
2472 testType: serverTest,
2473 name: "D5Bug-NoQuirk-Reject",
2474 config: Config{
2475 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2476 Bugs: ProtocolBugs{
2477 SSL3RSAKeyExchange: true,
2478 },
2479 },
2480 shouldFail: true,
2481 expectedError: ":TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG:",
2482 })
2483 testCases = append(testCases, testCase{
2484 testType: serverTest,
2485 name: "D5Bug-Quirk-Normal",
2486 config: Config{
2487 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2488 },
2489 flags: []string{"-tls-d5-bug"},
2490 })
2491 testCases = append(testCases, testCase{
2492 testType: serverTest,
2493 name: "D5Bug-Quirk-Bug",
2494 config: Config{
2495 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2496 Bugs: ProtocolBugs{
2497 SSL3RSAKeyExchange: true,
2498 },
2499 },
2500 flags: []string{"-tls-d5-bug"},
2501 })
2502}
2503
David Benjamine78bfde2014-09-06 12:45:15 -04002504func addExtensionTests() {
2505 testCases = append(testCases, testCase{
2506 testType: clientTest,
2507 name: "DuplicateExtensionClient",
2508 config: Config{
2509 Bugs: ProtocolBugs{
2510 DuplicateExtension: true,
2511 },
2512 },
2513 shouldFail: true,
2514 expectedLocalError: "remote error: error decoding message",
2515 })
2516 testCases = append(testCases, testCase{
2517 testType: serverTest,
2518 name: "DuplicateExtensionServer",
2519 config: Config{
2520 Bugs: ProtocolBugs{
2521 DuplicateExtension: true,
2522 },
2523 },
2524 shouldFail: true,
2525 expectedLocalError: "remote error: error decoding message",
2526 })
2527 testCases = append(testCases, testCase{
2528 testType: clientTest,
2529 name: "ServerNameExtensionClient",
2530 config: Config{
2531 Bugs: ProtocolBugs{
2532 ExpectServerName: "example.com",
2533 },
2534 },
2535 flags: []string{"-host-name", "example.com"},
2536 })
2537 testCases = append(testCases, testCase{
2538 testType: clientTest,
David Benjamin5f237bc2015-02-11 17:14:15 -05002539 name: "ServerNameExtensionClientMismatch",
David Benjamine78bfde2014-09-06 12:45:15 -04002540 config: Config{
2541 Bugs: ProtocolBugs{
2542 ExpectServerName: "mismatch.com",
2543 },
2544 },
2545 flags: []string{"-host-name", "example.com"},
2546 shouldFail: true,
2547 expectedLocalError: "tls: unexpected server name",
2548 })
2549 testCases = append(testCases, testCase{
2550 testType: clientTest,
David Benjamin5f237bc2015-02-11 17:14:15 -05002551 name: "ServerNameExtensionClientMissing",
David Benjamine78bfde2014-09-06 12:45:15 -04002552 config: Config{
2553 Bugs: ProtocolBugs{
2554 ExpectServerName: "missing.com",
2555 },
2556 },
2557 shouldFail: true,
2558 expectedLocalError: "tls: unexpected server name",
2559 })
2560 testCases = append(testCases, testCase{
2561 testType: serverTest,
2562 name: "ServerNameExtensionServer",
2563 config: Config{
2564 ServerName: "example.com",
2565 },
2566 flags: []string{"-expect-server-name", "example.com"},
2567 resumeSession: true,
2568 })
David Benjaminae2888f2014-09-06 12:58:58 -04002569 testCases = append(testCases, testCase{
2570 testType: clientTest,
2571 name: "ALPNClient",
2572 config: Config{
2573 NextProtos: []string{"foo"},
2574 },
2575 flags: []string{
2576 "-advertise-alpn", "\x03foo\x03bar\x03baz",
2577 "-expect-alpn", "foo",
2578 },
David Benjaminfc7b0862014-09-06 13:21:53 -04002579 expectedNextProto: "foo",
2580 expectedNextProtoType: alpn,
2581 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04002582 })
2583 testCases = append(testCases, testCase{
2584 testType: serverTest,
2585 name: "ALPNServer",
2586 config: Config{
2587 NextProtos: []string{"foo", "bar", "baz"},
2588 },
2589 flags: []string{
2590 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2591 "-select-alpn", "foo",
2592 },
David Benjaminfc7b0862014-09-06 13:21:53 -04002593 expectedNextProto: "foo",
2594 expectedNextProtoType: alpn,
2595 resumeSession: true,
2596 })
2597 // Test that the server prefers ALPN over NPN.
2598 testCases = append(testCases, testCase{
2599 testType: serverTest,
2600 name: "ALPNServer-Preferred",
2601 config: Config{
2602 NextProtos: []string{"foo", "bar", "baz"},
2603 },
2604 flags: []string{
2605 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2606 "-select-alpn", "foo",
2607 "-advertise-npn", "\x03foo\x03bar\x03baz",
2608 },
2609 expectedNextProto: "foo",
2610 expectedNextProtoType: alpn,
2611 resumeSession: true,
2612 })
2613 testCases = append(testCases, testCase{
2614 testType: serverTest,
2615 name: "ALPNServer-Preferred-Swapped",
2616 config: Config{
2617 NextProtos: []string{"foo", "bar", "baz"},
2618 Bugs: ProtocolBugs{
2619 SwapNPNAndALPN: true,
2620 },
2621 },
2622 flags: []string{
2623 "-expect-advertised-alpn", "\x03foo\x03bar\x03baz",
2624 "-select-alpn", "foo",
2625 "-advertise-npn", "\x03foo\x03bar\x03baz",
2626 },
2627 expectedNextProto: "foo",
2628 expectedNextProtoType: alpn,
2629 resumeSession: true,
David Benjaminae2888f2014-09-06 12:58:58 -04002630 })
Adam Langley38311732014-10-16 19:04:35 -07002631 // Resume with a corrupt ticket.
2632 testCases = append(testCases, testCase{
2633 testType: serverTest,
2634 name: "CorruptTicket",
2635 config: Config{
2636 Bugs: ProtocolBugs{
2637 CorruptTicket: true,
2638 },
2639 },
2640 resumeSession: true,
2641 flags: []string{"-expect-session-miss"},
2642 })
2643 // Resume with an oversized session id.
2644 testCases = append(testCases, testCase{
2645 testType: serverTest,
2646 name: "OversizedSessionId",
2647 config: Config{
2648 Bugs: ProtocolBugs{
2649 OversizedSessionId: true,
2650 },
2651 },
2652 resumeSession: true,
Adam Langley75712922014-10-10 16:23:43 -07002653 shouldFail: true,
Adam Langley38311732014-10-16 19:04:35 -07002654 expectedError: ":DECODE_ERROR:",
2655 })
David Benjaminca6c8262014-11-15 19:06:08 -05002656 // Basic DTLS-SRTP tests. Include fake profiles to ensure they
2657 // are ignored.
2658 testCases = append(testCases, testCase{
2659 protocol: dtls,
2660 name: "SRTP-Client",
2661 config: Config{
2662 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
2663 },
2664 flags: []string{
2665 "-srtp-profiles",
2666 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2667 },
2668 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2669 })
2670 testCases = append(testCases, testCase{
2671 protocol: dtls,
2672 testType: serverTest,
2673 name: "SRTP-Server",
2674 config: Config{
2675 SRTPProtectionProfiles: []uint16{40, SRTP_AES128_CM_HMAC_SHA1_80, 42},
2676 },
2677 flags: []string{
2678 "-srtp-profiles",
2679 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2680 },
2681 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2682 })
2683 // Test that the MKI is ignored.
2684 testCases = append(testCases, testCase{
2685 protocol: dtls,
2686 testType: serverTest,
2687 name: "SRTP-Server-IgnoreMKI",
2688 config: Config{
2689 SRTPProtectionProfiles: []uint16{SRTP_AES128_CM_HMAC_SHA1_80},
2690 Bugs: ProtocolBugs{
2691 SRTPMasterKeyIdentifer: "bogus",
2692 },
2693 },
2694 flags: []string{
2695 "-srtp-profiles",
2696 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2697 },
2698 expectedSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_80,
2699 })
2700 // Test that SRTP isn't negotiated on the server if there were
2701 // no matching profiles.
2702 testCases = append(testCases, testCase{
2703 protocol: dtls,
2704 testType: serverTest,
2705 name: "SRTP-Server-NoMatch",
2706 config: Config{
2707 SRTPProtectionProfiles: []uint16{100, 101, 102},
2708 },
2709 flags: []string{
2710 "-srtp-profiles",
2711 "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32",
2712 },
2713 expectedSRTPProtectionProfile: 0,
2714 })
2715 // Test that the server returning an invalid SRTP profile is
2716 // flagged as an error by the client.
2717 testCases = append(testCases, testCase{
2718 protocol: dtls,
2719 name: "SRTP-Client-NoMatch",
2720 config: Config{
2721 Bugs: ProtocolBugs{
2722 SendSRTPProtectionProfile: SRTP_AES128_CM_HMAC_SHA1_32,
2723 },
2724 },
2725 flags: []string{
2726 "-srtp-profiles",
2727 "SRTP_AES128_CM_SHA1_80",
2728 },
2729 shouldFail: true,
2730 expectedError: ":BAD_SRTP_PROTECTION_PROFILE_LIST:",
2731 })
David Benjamin61f95272014-11-25 01:55:35 -05002732 // Test OCSP stapling and SCT list.
2733 testCases = append(testCases, testCase{
2734 name: "OCSPStapling",
2735 flags: []string{
2736 "-enable-ocsp-stapling",
2737 "-expect-ocsp-response",
2738 base64.StdEncoding.EncodeToString(testOCSPResponse),
2739 },
2740 })
2741 testCases = append(testCases, testCase{
2742 name: "SignedCertificateTimestampList",
2743 flags: []string{
2744 "-enable-signed-cert-timestamps",
2745 "-expect-signed-cert-timestamps",
2746 base64.StdEncoding.EncodeToString(testSCTList),
2747 },
2748 })
David Benjamine78bfde2014-09-06 12:45:15 -04002749}
2750
David Benjamin01fe8202014-09-24 15:21:44 -04002751func addResumptionVersionTests() {
David Benjamin01fe8202014-09-24 15:21:44 -04002752 for _, sessionVers := range tlsVersions {
David Benjamin01fe8202014-09-24 15:21:44 -04002753 for _, resumeVers := range tlsVersions {
David Benjamin8b8c0062014-11-23 02:47:52 -05002754 protocols := []protocol{tls}
2755 if sessionVers.hasDTLS && resumeVers.hasDTLS {
2756 protocols = append(protocols, dtls)
David Benjaminbdf5e722014-11-11 00:52:15 -05002757 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002758 for _, protocol := range protocols {
2759 suffix := "-" + sessionVers.name + "-" + resumeVers.name
2760 if protocol == dtls {
2761 suffix += "-DTLS"
2762 }
2763
David Benjaminece3de92015-03-16 18:02:20 -04002764 if sessionVers.version == resumeVers.version {
2765 testCases = append(testCases, testCase{
2766 protocol: protocol,
2767 name: "Resume-Client" + suffix,
2768 resumeSession: true,
2769 config: Config{
2770 MaxVersion: sessionVers.version,
2771 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
David Benjamin8b8c0062014-11-23 02:47:52 -05002772 },
David Benjaminece3de92015-03-16 18:02:20 -04002773 expectedVersion: sessionVers.version,
2774 expectedResumeVersion: resumeVers.version,
2775 })
2776 } else {
2777 testCases = append(testCases, testCase{
2778 protocol: protocol,
2779 name: "Resume-Client-Mismatch" + suffix,
2780 resumeSession: true,
2781 config: Config{
2782 MaxVersion: sessionVers.version,
2783 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
David Benjamin8b8c0062014-11-23 02:47:52 -05002784 },
David Benjaminece3de92015-03-16 18:02:20 -04002785 expectedVersion: sessionVers.version,
2786 resumeConfig: &Config{
2787 MaxVersion: resumeVers.version,
2788 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2789 Bugs: ProtocolBugs{
2790 AllowSessionVersionMismatch: true,
2791 },
2792 },
2793 expectedResumeVersion: resumeVers.version,
2794 shouldFail: true,
2795 expectedError: ":OLD_SESSION_VERSION_NOT_RETURNED:",
2796 })
2797 }
David Benjamin8b8c0062014-11-23 02:47:52 -05002798
2799 testCases = append(testCases, testCase{
2800 protocol: protocol,
2801 name: "Resume-Client-NoResume" + suffix,
2802 flags: []string{"-expect-session-miss"},
2803 resumeSession: true,
2804 config: Config{
2805 MaxVersion: sessionVers.version,
2806 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2807 },
2808 expectedVersion: sessionVers.version,
2809 resumeConfig: &Config{
2810 MaxVersion: resumeVers.version,
2811 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2812 },
2813 newSessionsOnResume: true,
2814 expectedResumeVersion: resumeVers.version,
2815 })
2816
2817 var flags []string
2818 if sessionVers.version != resumeVers.version {
2819 flags = append(flags, "-expect-session-miss")
2820 }
2821 testCases = append(testCases, testCase{
2822 protocol: protocol,
2823 testType: serverTest,
2824 name: "Resume-Server" + suffix,
2825 flags: flags,
2826 resumeSession: true,
2827 config: Config{
2828 MaxVersion: sessionVers.version,
2829 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2830 },
2831 expectedVersion: sessionVers.version,
2832 resumeConfig: &Config{
2833 MaxVersion: resumeVers.version,
2834 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
2835 },
2836 expectedResumeVersion: resumeVers.version,
2837 })
2838 }
David Benjamin01fe8202014-09-24 15:21:44 -04002839 }
2840 }
David Benjaminece3de92015-03-16 18:02:20 -04002841
2842 testCases = append(testCases, testCase{
2843 name: "Resume-Client-CipherMismatch",
2844 resumeSession: true,
2845 config: Config{
2846 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2847 },
2848 resumeConfig: &Config{
2849 CipherSuites: []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256},
2850 Bugs: ProtocolBugs{
2851 SendCipherSuite: TLS_RSA_WITH_AES_128_CBC_SHA,
2852 },
2853 },
2854 shouldFail: true,
2855 expectedError: ":OLD_SESSION_CIPHER_NOT_RETURNED:",
2856 })
David Benjamin01fe8202014-09-24 15:21:44 -04002857}
2858
Adam Langley2ae77d22014-10-28 17:29:33 -07002859func addRenegotiationTests() {
2860 testCases = append(testCases, testCase{
2861 testType: serverTest,
2862 name: "Renegotiate-Server",
2863 flags: []string{"-renegotiate"},
2864 shimWritesFirst: true,
2865 })
2866 testCases = append(testCases, testCase{
2867 testType: serverTest,
David Benjamincdea40c2015-03-19 14:09:43 -04002868 name: "Renegotiate-Server-Full",
2869 config: Config{
2870 Bugs: ProtocolBugs{
2871 NeverResumeOnRenego: true,
2872 },
2873 },
2874 flags: []string{"-renegotiate"},
2875 shimWritesFirst: true,
2876 })
2877 testCases = append(testCases, testCase{
2878 testType: serverTest,
Adam Langley2ae77d22014-10-28 17:29:33 -07002879 name: "Renegotiate-Server-EmptyExt",
2880 config: Config{
2881 Bugs: ProtocolBugs{
2882 EmptyRenegotiationInfo: true,
2883 },
2884 },
2885 flags: []string{"-renegotiate"},
2886 shimWritesFirst: true,
2887 shouldFail: true,
2888 expectedError: ":RENEGOTIATION_MISMATCH:",
2889 })
2890 testCases = append(testCases, testCase{
2891 testType: serverTest,
2892 name: "Renegotiate-Server-BadExt",
2893 config: Config{
2894 Bugs: ProtocolBugs{
2895 BadRenegotiationInfo: true,
2896 },
2897 },
2898 flags: []string{"-renegotiate"},
2899 shimWritesFirst: true,
2900 shouldFail: true,
2901 expectedError: ":RENEGOTIATION_MISMATCH:",
2902 })
David Benjaminca6554b2014-11-08 12:31:52 -05002903 testCases = append(testCases, testCase{
2904 testType: serverTest,
2905 name: "Renegotiate-Server-ClientInitiated",
2906 renegotiate: true,
2907 })
2908 testCases = append(testCases, testCase{
2909 testType: serverTest,
2910 name: "Renegotiate-Server-ClientInitiated-NoExt",
2911 renegotiate: true,
2912 config: Config{
2913 Bugs: ProtocolBugs{
2914 NoRenegotiationInfo: true,
2915 },
2916 },
2917 shouldFail: true,
2918 expectedError: ":UNSAFE_LEGACY_RENEGOTIATION_DISABLED:",
2919 })
2920 testCases = append(testCases, testCase{
2921 testType: serverTest,
2922 name: "Renegotiate-Server-ClientInitiated-NoExt-Allowed",
2923 renegotiate: true,
2924 config: Config{
2925 Bugs: ProtocolBugs{
2926 NoRenegotiationInfo: true,
2927 },
2928 },
2929 flags: []string{"-allow-unsafe-legacy-renegotiation"},
2930 })
David Benjaminb16346b2015-04-08 19:16:58 -04002931 testCases = append(testCases, testCase{
2932 testType: serverTest,
2933 name: "Renegotiate-Server-ClientInitiated-Forbidden",
2934 renegotiate: true,
2935 flags: []string{"-reject-peer-renegotiations"},
2936 shouldFail: true,
2937 expectedError: ":NO_RENEGOTIATION:",
2938 expectedLocalError: "remote error: no renegotiation",
2939 })
David Benjamin3c9746a2015-03-19 15:00:10 -04002940 // Regression test for CVE-2015-0291.
2941 testCases = append(testCases, testCase{
2942 testType: serverTest,
2943 name: "Renegotiate-Server-NoSignatureAlgorithms",
2944 config: Config{
2945 Bugs: ProtocolBugs{
2946 NeverResumeOnRenego: true,
2947 NoSignatureAlgorithmsOnRenego: true,
2948 },
2949 },
2950 flags: []string{"-renegotiate"},
2951 shimWritesFirst: true,
2952 })
Adam Langley2ae77d22014-10-28 17:29:33 -07002953 // TODO(agl): test the renegotiation info SCSV.
Adam Langleycf2d4f42014-10-28 19:06:14 -07002954 testCases = append(testCases, testCase{
2955 name: "Renegotiate-Client",
2956 renegotiate: true,
2957 })
2958 testCases = append(testCases, testCase{
David Benjamincdea40c2015-03-19 14:09:43 -04002959 name: "Renegotiate-Client-Full",
2960 config: Config{
2961 Bugs: ProtocolBugs{
2962 NeverResumeOnRenego: true,
2963 },
2964 },
2965 renegotiate: true,
2966 })
2967 testCases = append(testCases, testCase{
Adam Langleycf2d4f42014-10-28 19:06:14 -07002968 name: "Renegotiate-Client-EmptyExt",
2969 renegotiate: true,
2970 config: Config{
2971 Bugs: ProtocolBugs{
2972 EmptyRenegotiationInfo: true,
2973 },
2974 },
2975 shouldFail: true,
2976 expectedError: ":RENEGOTIATION_MISMATCH:",
2977 })
2978 testCases = append(testCases, testCase{
2979 name: "Renegotiate-Client-BadExt",
2980 renegotiate: true,
2981 config: Config{
2982 Bugs: ProtocolBugs{
2983 BadRenegotiationInfo: true,
2984 },
2985 },
2986 shouldFail: true,
2987 expectedError: ":RENEGOTIATION_MISMATCH:",
2988 })
2989 testCases = append(testCases, testCase{
2990 name: "Renegotiate-Client-SwitchCiphers",
2991 renegotiate: true,
2992 config: Config{
2993 CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
2994 },
2995 renegotiateCiphers: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
2996 })
2997 testCases = append(testCases, testCase{
2998 name: "Renegotiate-Client-SwitchCiphers2",
2999 renegotiate: true,
3000 config: Config{
3001 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
3002 },
3003 renegotiateCiphers: []uint16{TLS_RSA_WITH_RC4_128_SHA},
3004 })
David Benjaminc44b1df2014-11-23 12:11:01 -05003005 testCases = append(testCases, testCase{
David Benjaminb16346b2015-04-08 19:16:58 -04003006 name: "Renegotiate-Client-Forbidden",
3007 renegotiate: true,
3008 flags: []string{"-reject-peer-renegotiations"},
3009 shouldFail: true,
3010 expectedError: ":NO_RENEGOTIATION:",
3011 expectedLocalError: "remote error: no renegotiation",
3012 })
3013 testCases = append(testCases, testCase{
David Benjaminc44b1df2014-11-23 12:11:01 -05003014 name: "Renegotiate-SameClientVersion",
3015 renegotiate: true,
3016 config: Config{
3017 MaxVersion: VersionTLS10,
3018 Bugs: ProtocolBugs{
3019 RequireSameRenegoClientVersion: true,
3020 },
3021 },
3022 })
Adam Langley2ae77d22014-10-28 17:29:33 -07003023}
3024
David Benjamin5e961c12014-11-07 01:48:35 -05003025func addDTLSReplayTests() {
3026 // Test that sequence number replays are detected.
3027 testCases = append(testCases, testCase{
3028 protocol: dtls,
3029 name: "DTLS-Replay",
3030 replayWrites: true,
3031 })
3032
3033 // Test the outgoing sequence number skipping by values larger
3034 // than the retransmit window.
3035 testCases = append(testCases, testCase{
3036 protocol: dtls,
3037 name: "DTLS-Replay-LargeGaps",
3038 config: Config{
3039 Bugs: ProtocolBugs{
3040 SequenceNumberIncrement: 127,
3041 },
3042 },
3043 replayWrites: true,
3044 })
3045}
3046
Feng Lu41aa3252014-11-21 22:47:56 -08003047func addFastRadioPaddingTests() {
David Benjamin1e29a6b2014-12-10 02:27:24 -05003048 testCases = append(testCases, testCase{
3049 protocol: tls,
3050 name: "FastRadio-Padding",
Feng Lu41aa3252014-11-21 22:47:56 -08003051 config: Config{
3052 Bugs: ProtocolBugs{
3053 RequireFastradioPadding: true,
3054 },
3055 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05003056 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08003057 })
David Benjamin1e29a6b2014-12-10 02:27:24 -05003058 testCases = append(testCases, testCase{
3059 protocol: dtls,
David Benjamin5f237bc2015-02-11 17:14:15 -05003060 name: "FastRadio-Padding-DTLS",
Feng Lu41aa3252014-11-21 22:47:56 -08003061 config: Config{
3062 Bugs: ProtocolBugs{
3063 RequireFastradioPadding: true,
3064 },
3065 },
David Benjamin1e29a6b2014-12-10 02:27:24 -05003066 flags: []string{"-fastradio-padding"},
Feng Lu41aa3252014-11-21 22:47:56 -08003067 })
3068}
3069
David Benjamin000800a2014-11-14 01:43:59 -05003070var testHashes = []struct {
3071 name string
3072 id uint8
3073}{
3074 {"SHA1", hashSHA1},
3075 {"SHA224", hashSHA224},
3076 {"SHA256", hashSHA256},
3077 {"SHA384", hashSHA384},
3078 {"SHA512", hashSHA512},
3079}
3080
3081func addSigningHashTests() {
3082 // Make sure each hash works. Include some fake hashes in the list and
3083 // ensure they're ignored.
3084 for _, hash := range testHashes {
3085 testCases = append(testCases, testCase{
3086 name: "SigningHash-ClientAuth-" + hash.name,
3087 config: Config{
3088 ClientAuth: RequireAnyClientCert,
3089 SignatureAndHashes: []signatureAndHash{
3090 {signatureRSA, 42},
3091 {signatureRSA, hash.id},
3092 {signatureRSA, 255},
3093 },
3094 },
3095 flags: []string{
3096 "-cert-file", rsaCertificateFile,
3097 "-key-file", rsaKeyFile,
3098 },
3099 })
3100
3101 testCases = append(testCases, testCase{
3102 testType: serverTest,
3103 name: "SigningHash-ServerKeyExchange-Sign-" + hash.name,
3104 config: Config{
3105 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
3106 SignatureAndHashes: []signatureAndHash{
3107 {signatureRSA, 42},
3108 {signatureRSA, hash.id},
3109 {signatureRSA, 255},
3110 },
3111 },
3112 })
3113 }
3114
3115 // Test that hash resolution takes the signature type into account.
3116 testCases = append(testCases, testCase{
3117 name: "SigningHash-ClientAuth-SignatureType",
3118 config: Config{
3119 ClientAuth: RequireAnyClientCert,
3120 SignatureAndHashes: []signatureAndHash{
3121 {signatureECDSA, hashSHA512},
3122 {signatureRSA, hashSHA384},
3123 {signatureECDSA, hashSHA1},
3124 },
3125 },
3126 flags: []string{
3127 "-cert-file", rsaCertificateFile,
3128 "-key-file", rsaKeyFile,
3129 },
3130 })
3131
3132 testCases = append(testCases, testCase{
3133 testType: serverTest,
3134 name: "SigningHash-ServerKeyExchange-SignatureType",
3135 config: Config{
3136 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
3137 SignatureAndHashes: []signatureAndHash{
3138 {signatureECDSA, hashSHA512},
3139 {signatureRSA, hashSHA384},
3140 {signatureECDSA, hashSHA1},
3141 },
3142 },
3143 })
3144
3145 // Test that, if the list is missing, the peer falls back to SHA-1.
3146 testCases = append(testCases, testCase{
3147 name: "SigningHash-ClientAuth-Fallback",
3148 config: Config{
3149 ClientAuth: RequireAnyClientCert,
3150 SignatureAndHashes: []signatureAndHash{
3151 {signatureRSA, hashSHA1},
3152 },
3153 Bugs: ProtocolBugs{
3154 NoSignatureAndHashes: true,
3155 },
3156 },
3157 flags: []string{
3158 "-cert-file", rsaCertificateFile,
3159 "-key-file", rsaKeyFile,
3160 },
3161 })
3162
3163 testCases = append(testCases, testCase{
3164 testType: serverTest,
3165 name: "SigningHash-ServerKeyExchange-Fallback",
3166 config: Config{
3167 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
3168 SignatureAndHashes: []signatureAndHash{
3169 {signatureRSA, hashSHA1},
3170 },
3171 Bugs: ProtocolBugs{
3172 NoSignatureAndHashes: true,
3173 },
3174 },
3175 })
David Benjamin72dc7832015-03-16 17:49:43 -04003176
3177 // Test that hash preferences are enforced. BoringSSL defaults to
3178 // rejecting MD5 signatures.
3179 testCases = append(testCases, testCase{
3180 testType: serverTest,
3181 name: "SigningHash-ClientAuth-Enforced",
3182 config: Config{
3183 Certificates: []Certificate{rsaCertificate},
3184 SignatureAndHashes: []signatureAndHash{
3185 {signatureRSA, hashMD5},
3186 // Advertise SHA-1 so the handshake will
3187 // proceed, but the shim's preferences will be
3188 // ignored in CertificateVerify generation, so
3189 // MD5 will be chosen.
3190 {signatureRSA, hashSHA1},
3191 },
3192 Bugs: ProtocolBugs{
3193 IgnorePeerSignatureAlgorithmPreferences: true,
3194 },
3195 },
3196 flags: []string{"-require-any-client-certificate"},
3197 shouldFail: true,
3198 expectedError: ":WRONG_SIGNATURE_TYPE:",
3199 })
3200
3201 testCases = append(testCases, testCase{
3202 name: "SigningHash-ServerKeyExchange-Enforced",
3203 config: Config{
3204 CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
3205 SignatureAndHashes: []signatureAndHash{
3206 {signatureRSA, hashMD5},
3207 },
3208 Bugs: ProtocolBugs{
3209 IgnorePeerSignatureAlgorithmPreferences: true,
3210 },
3211 },
3212 shouldFail: true,
3213 expectedError: ":WRONG_SIGNATURE_TYPE:",
3214 })
David Benjamin000800a2014-11-14 01:43:59 -05003215}
3216
David Benjamin83f90402015-01-27 01:09:43 -05003217// timeouts is the retransmit schedule for BoringSSL. It doubles and
3218// caps at 60 seconds. On the 13th timeout, it gives up.
3219var timeouts = []time.Duration{
3220 1 * time.Second,
3221 2 * time.Second,
3222 4 * time.Second,
3223 8 * time.Second,
3224 16 * time.Second,
3225 32 * time.Second,
3226 60 * time.Second,
3227 60 * time.Second,
3228 60 * time.Second,
3229 60 * time.Second,
3230 60 * time.Second,
3231 60 * time.Second,
3232 60 * time.Second,
3233}
3234
3235func addDTLSRetransmitTests() {
3236 // Test that this is indeed the timeout schedule. Stress all
3237 // four patterns of handshake.
3238 for i := 1; i < len(timeouts); i++ {
3239 number := strconv.Itoa(i)
3240 testCases = append(testCases, testCase{
3241 protocol: dtls,
3242 name: "DTLS-Retransmit-Client-" + number,
3243 config: Config{
3244 Bugs: ProtocolBugs{
3245 TimeoutSchedule: timeouts[:i],
3246 },
3247 },
3248 resumeSession: true,
3249 flags: []string{"-async"},
3250 })
3251 testCases = append(testCases, testCase{
3252 protocol: dtls,
3253 testType: serverTest,
3254 name: "DTLS-Retransmit-Server-" + number,
3255 config: Config{
3256 Bugs: ProtocolBugs{
3257 TimeoutSchedule: timeouts[:i],
3258 },
3259 },
3260 resumeSession: true,
3261 flags: []string{"-async"},
3262 })
3263 }
3264
3265 // Test that exceeding the timeout schedule hits a read
3266 // timeout.
3267 testCases = append(testCases, testCase{
3268 protocol: dtls,
3269 name: "DTLS-Retransmit-Timeout",
3270 config: Config{
3271 Bugs: ProtocolBugs{
3272 TimeoutSchedule: timeouts,
3273 },
3274 },
3275 resumeSession: true,
3276 flags: []string{"-async"},
3277 shouldFail: true,
3278 expectedError: ":READ_TIMEOUT_EXPIRED:",
3279 })
3280
3281 // Test that timeout handling has a fudge factor, due to API
3282 // problems.
3283 testCases = append(testCases, testCase{
3284 protocol: dtls,
3285 name: "DTLS-Retransmit-Fudge",
3286 config: Config{
3287 Bugs: ProtocolBugs{
3288 TimeoutSchedule: []time.Duration{
3289 timeouts[0] - 10*time.Millisecond,
3290 },
3291 },
3292 },
3293 resumeSession: true,
3294 flags: []string{"-async"},
3295 })
David Benjamin7eaab4c2015-03-02 19:01:16 -05003296
3297 // Test that the final Finished retransmitting isn't
3298 // duplicated if the peer badly fragments everything.
3299 testCases = append(testCases, testCase{
3300 testType: serverTest,
3301 protocol: dtls,
3302 name: "DTLS-Retransmit-Fragmented",
3303 config: Config{
3304 Bugs: ProtocolBugs{
3305 TimeoutSchedule: []time.Duration{timeouts[0]},
3306 MaxHandshakeRecordLength: 2,
3307 },
3308 },
3309 flags: []string{"-async"},
3310 })
David Benjamin83f90402015-01-27 01:09:43 -05003311}
3312
David Benjaminc565ebb2015-04-03 04:06:36 -04003313func addExportKeyingMaterialTests() {
3314 for _, vers := range tlsVersions {
3315 if vers.version == VersionSSL30 {
3316 continue
3317 }
3318 testCases = append(testCases, testCase{
3319 name: "ExportKeyingMaterial-" + vers.name,
3320 config: Config{
3321 MaxVersion: vers.version,
3322 },
3323 exportKeyingMaterial: 1024,
3324 exportLabel: "label",
3325 exportContext: "context",
3326 useExportContext: true,
3327 })
3328 testCases = append(testCases, testCase{
3329 name: "ExportKeyingMaterial-NoContext-" + vers.name,
3330 config: Config{
3331 MaxVersion: vers.version,
3332 },
3333 exportKeyingMaterial: 1024,
3334 })
3335 testCases = append(testCases, testCase{
3336 name: "ExportKeyingMaterial-EmptyContext-" + vers.name,
3337 config: Config{
3338 MaxVersion: vers.version,
3339 },
3340 exportKeyingMaterial: 1024,
3341 useExportContext: true,
3342 })
3343 testCases = append(testCases, testCase{
3344 name: "ExportKeyingMaterial-Small-" + vers.name,
3345 config: Config{
3346 MaxVersion: vers.version,
3347 },
3348 exportKeyingMaterial: 1,
3349 exportLabel: "label",
3350 exportContext: "context",
3351 useExportContext: true,
3352 })
3353 }
3354 testCases = append(testCases, testCase{
3355 name: "ExportKeyingMaterial-SSL3",
3356 config: Config{
3357 MaxVersion: VersionSSL30,
3358 },
3359 exportKeyingMaterial: 1024,
3360 exportLabel: "label",
3361 exportContext: "context",
3362 useExportContext: true,
3363 shouldFail: true,
3364 expectedError: "failed to export keying material",
3365 })
3366}
3367
David Benjamin884fdf12014-08-02 15:28:23 -04003368func worker(statusChan chan statusMsg, c chan *testCase, buildDir string, wg *sync.WaitGroup) {
Adam Langley95c29f32014-06-20 12:00:00 -07003369 defer wg.Done()
3370
3371 for test := range c {
Adam Langley69a01602014-11-17 17:26:55 -08003372 var err error
3373
3374 if *mallocTest < 0 {
3375 statusChan <- statusMsg{test: test, started: true}
3376 err = runTest(test, buildDir, -1)
3377 } else {
3378 for mallocNumToFail := int64(*mallocTest); ; mallocNumToFail++ {
3379 statusChan <- statusMsg{test: test, started: true}
3380 if err = runTest(test, buildDir, mallocNumToFail); err != errMoreMallocs {
3381 if err != nil {
3382 fmt.Printf("\n\nmalloc test failed at %d: %s\n", mallocNumToFail, err)
3383 }
3384 break
3385 }
3386 }
3387 }
Adam Langley95c29f32014-06-20 12:00:00 -07003388 statusChan <- statusMsg{test: test, err: err}
3389 }
3390}
3391
3392type statusMsg struct {
3393 test *testCase
3394 started bool
3395 err error
3396}
3397
David Benjamin5f237bc2015-02-11 17:14:15 -05003398func statusPrinter(doneChan chan *testOutput, statusChan chan statusMsg, total int) {
Adam Langley95c29f32014-06-20 12:00:00 -07003399 var started, done, failed, lineLen int
Adam Langley95c29f32014-06-20 12:00:00 -07003400
David Benjamin5f237bc2015-02-11 17:14:15 -05003401 testOutput := newTestOutput()
Adam Langley95c29f32014-06-20 12:00:00 -07003402 for msg := range statusChan {
David Benjamin5f237bc2015-02-11 17:14:15 -05003403 if !*pipe {
3404 // Erase the previous status line.
David Benjamin87c8a642015-02-21 01:54:29 -05003405 var erase string
3406 for i := 0; i < lineLen; i++ {
3407 erase += "\b \b"
3408 }
3409 fmt.Print(erase)
David Benjamin5f237bc2015-02-11 17:14:15 -05003410 }
3411
Adam Langley95c29f32014-06-20 12:00:00 -07003412 if msg.started {
3413 started++
3414 } else {
3415 done++
David Benjamin5f237bc2015-02-11 17:14:15 -05003416
3417 if msg.err != nil {
3418 fmt.Printf("FAILED (%s)\n%s\n", msg.test.name, msg.err)
3419 failed++
3420 testOutput.addResult(msg.test.name, "FAIL")
3421 } else {
3422 if *pipe {
3423 // Print each test instead of a status line.
3424 fmt.Printf("PASSED (%s)\n", msg.test.name)
3425 }
3426 testOutput.addResult(msg.test.name, "PASS")
3427 }
Adam Langley95c29f32014-06-20 12:00:00 -07003428 }
3429
David Benjamin5f237bc2015-02-11 17:14:15 -05003430 if !*pipe {
3431 // Print a new status line.
3432 line := fmt.Sprintf("%d/%d/%d/%d", failed, done, started, total)
3433 lineLen = len(line)
3434 os.Stdout.WriteString(line)
Adam Langley95c29f32014-06-20 12:00:00 -07003435 }
Adam Langley95c29f32014-06-20 12:00:00 -07003436 }
David Benjamin5f237bc2015-02-11 17:14:15 -05003437
3438 doneChan <- testOutput
Adam Langley95c29f32014-06-20 12:00:00 -07003439}
3440
3441func main() {
3442 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 -04003443 var flagNumWorkers *int = flag.Int("num-workers", runtime.NumCPU(), "The number of workers to run in parallel.")
David Benjamin884fdf12014-08-02 15:28:23 -04003444 var flagBuildDir *string = flag.String("build-dir", "../../../build", "The build directory to run the shim from.")
Adam Langley95c29f32014-06-20 12:00:00 -07003445
3446 flag.Parse()
3447
3448 addCipherSuiteTests()
3449 addBadECDSASignatureTests()
Adam Langley80842bd2014-06-20 12:00:00 -07003450 addCBCPaddingTests()
Kenny Root7fdeaf12014-08-05 15:23:37 -07003451 addCBCSplittingTests()
David Benjamin636293b2014-07-08 17:59:18 -04003452 addClientAuthTests()
Adam Langley524e7172015-02-20 16:04:00 -08003453 addDDoSCallbackTests()
David Benjamin7e2e6cf2014-08-07 17:44:24 -04003454 addVersionNegotiationTests()
David Benjaminaccb4542014-12-12 23:44:33 -05003455 addMinimumVersionTests()
David Benjamin5c24a1d2014-08-31 00:59:27 -04003456 addD5BugTests()
David Benjamine78bfde2014-09-06 12:45:15 -04003457 addExtensionTests()
David Benjamin01fe8202014-09-24 15:21:44 -04003458 addResumptionVersionTests()
Adam Langley75712922014-10-10 16:23:43 -07003459 addExtendedMasterSecretTests()
Adam Langley2ae77d22014-10-28 17:29:33 -07003460 addRenegotiationTests()
David Benjamin5e961c12014-11-07 01:48:35 -05003461 addDTLSReplayTests()
David Benjamin000800a2014-11-14 01:43:59 -05003462 addSigningHashTests()
Feng Lu41aa3252014-11-21 22:47:56 -08003463 addFastRadioPaddingTests()
David Benjamin83f90402015-01-27 01:09:43 -05003464 addDTLSRetransmitTests()
David Benjaminc565ebb2015-04-03 04:06:36 -04003465 addExportKeyingMaterialTests()
David Benjamin43ec06f2014-08-05 02:28:57 -04003466 for _, async := range []bool{false, true} {
3467 for _, splitHandshake := range []bool{false, true} {
David Benjamin6fd297b2014-08-11 18:43:38 -04003468 for _, protocol := range []protocol{tls, dtls} {
3469 addStateMachineCoverageTests(async, splitHandshake, protocol)
3470 }
David Benjamin43ec06f2014-08-05 02:28:57 -04003471 }
3472 }
Adam Langley95c29f32014-06-20 12:00:00 -07003473
3474 var wg sync.WaitGroup
3475
David Benjamin2bc8e6f2014-08-02 15:22:37 -04003476 numWorkers := *flagNumWorkers
Adam Langley95c29f32014-06-20 12:00:00 -07003477
3478 statusChan := make(chan statusMsg, numWorkers)
3479 testChan := make(chan *testCase, numWorkers)
David Benjamin5f237bc2015-02-11 17:14:15 -05003480 doneChan := make(chan *testOutput)
Adam Langley95c29f32014-06-20 12:00:00 -07003481
David Benjamin025b3d32014-07-01 19:53:04 -04003482 go statusPrinter(doneChan, statusChan, len(testCases))
Adam Langley95c29f32014-06-20 12:00:00 -07003483
3484 for i := 0; i < numWorkers; i++ {
3485 wg.Add(1)
David Benjamin884fdf12014-08-02 15:28:23 -04003486 go worker(statusChan, testChan, *flagBuildDir, &wg)
Adam Langley95c29f32014-06-20 12:00:00 -07003487 }
3488
David Benjamin025b3d32014-07-01 19:53:04 -04003489 for i := range testCases {
3490 if len(*flagTest) == 0 || *flagTest == testCases[i].name {
3491 testChan <- &testCases[i]
Adam Langley95c29f32014-06-20 12:00:00 -07003492 }
3493 }
3494
3495 close(testChan)
3496 wg.Wait()
3497 close(statusChan)
David Benjamin5f237bc2015-02-11 17:14:15 -05003498 testOutput := <-doneChan
Adam Langley95c29f32014-06-20 12:00:00 -07003499
3500 fmt.Printf("\n")
David Benjamin5f237bc2015-02-11 17:14:15 -05003501
3502 if *jsonOutput != "" {
3503 if err := testOutput.writeTo(*jsonOutput); err != nil {
3504 fmt.Fprintf(os.Stderr, "Error: %s\n", err)
3505 }
3506 }
David Benjamin2ab7a862015-04-04 17:02:18 -04003507
3508 if !testOutput.allPassed {
3509 os.Exit(1)
3510 }
Adam Langley95c29f32014-06-20 12:00:00 -07003511}