| 1 |
// Copyright 2009 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package tls |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"context" |
| 10 |
"crypto" |
| 11 |
"crypto/elliptic" |
| 12 |
"crypto/x509" |
| 13 |
"encoding/pem" |
| 14 |
"errors" |
| 15 |
"fmt" |
| 16 |
"io" |
| 17 |
"net" |
| 18 |
"os" |
| 19 |
"os/exec" |
| 20 |
"path/filepath" |
| 21 |
"runtime" |
| 22 |
"strings" |
| 23 |
"testing" |
| 24 |
"time" |
| 25 |
|
| 26 |
"golang.org/x/crypto/curve25519" |
| 27 |
) |
| 28 |
|
| 29 |
func testClientHello(t *testing.T, serverConfig *Config, m handshakeMessage) { |
| 30 |
testClientHelloFailure(t, serverConfig, m, "") |
| 31 |
} |
| 32 |
|
| 33 |
func testClientHelloFailure(t *testing.T, serverConfig *Config, m handshakeMessage, expectedSubStr string) { |
| 34 |
c, s := localPipe(t) |
| 35 |
go func() { |
| 36 |
cli := Client(c, testConfig) |
| 37 |
if ch, ok := m.(*clientHelloMsg); ok { |
| 38 |
cli.vers = ch.vers |
| 39 |
} |
| 40 |
cli.writeRecord(recordTypeHandshake, m.marshal()) |
| 41 |
c.Close() |
| 42 |
}() |
| 43 |
ctx := context.Background() |
| 44 |
conn := Server(s, serverConfig) |
| 45 |
ch, err := conn.readClientHello(ctx) |
| 46 |
hs := serverHandshakeState{ |
| 47 |
c: conn, |
| 48 |
ctx: ctx, |
| 49 |
clientHello: ch, |
| 50 |
} |
| 51 |
if err == nil { |
| 52 |
err = hs.processClientHello() |
| 53 |
} |
| 54 |
if err == nil { |
| 55 |
err = hs.pickCipherSuite() |
| 56 |
} |
| 57 |
s.Close() |
| 58 |
if len(expectedSubStr) == 0 { |
| 59 |
if err != nil && err != io.EOF { |
| 60 |
t.Errorf("Got error: %s; expected to succeed", err) |
| 61 |
} |
| 62 |
} else if err == nil || !strings.Contains(err.Error(), expectedSubStr) { |
| 63 |
t.Errorf("Got error: %v; expected to match substring '%s'", err, expectedSubStr) |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
func TestSimpleError(t *testing.T) { |
| 68 |
testClientHelloFailure(t, testConfig, &serverHelloDoneMsg{}, "unexpected handshake message") |
| 69 |
} |
| 70 |
|
| 71 |
var badProtocolVersions = []uint16{0x0000, 0x0005, 0x0100, 0x0105, 0x0200, 0x0205, VersionSSL30} |
| 72 |
|
| 73 |
func TestRejectBadProtocolVersion(t *testing.T) { |
| 74 |
config := testConfig.Clone() |
| 75 |
config.MinVersion = VersionSSL30 |
| 76 |
for _, v := range badProtocolVersions { |
| 77 |
testClientHelloFailure(t, config, &clientHelloMsg{ |
| 78 |
vers: v, |
| 79 |
random: make([]byte, 32), |
| 80 |
}, "unsupported versions") |
| 81 |
} |
| 82 |
testClientHelloFailure(t, config, &clientHelloMsg{ |
| 83 |
vers: VersionTLS12, |
| 84 |
supportedVersions: badProtocolVersions, |
| 85 |
random: make([]byte, 32), |
| 86 |
}, "unsupported versions") |
| 87 |
} |
| 88 |
|
| 89 |
func TestNoSuiteOverlap(t *testing.T) { |
| 90 |
clientHello := &clientHelloMsg{ |
| 91 |
vers: VersionTLS10, |
| 92 |
random: make([]byte, 32), |
| 93 |
cipherSuites: []uint16{0xff00}, |
| 94 |
compressionMethods: []uint8{compressionNone}, |
| 95 |
} |
| 96 |
testClientHelloFailure(t, testConfig, clientHello, "no cipher suite supported by both client and server") |
| 97 |
} |
| 98 |
|
| 99 |
func TestNoCompressionOverlap(t *testing.T) { |
| 100 |
clientHello := &clientHelloMsg{ |
| 101 |
vers: VersionTLS10, |
| 102 |
random: make([]byte, 32), |
| 103 |
cipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA}, |
| 104 |
compressionMethods: []uint8{0xff}, |
| 105 |
} |
| 106 |
testClientHelloFailure(t, testConfig, clientHello, "client does not support uncompressed connections") |
| 107 |
} |
| 108 |
|
| 109 |
func TestNoRC4ByDefault(t *testing.T) { |
| 110 |
clientHello := &clientHelloMsg{ |
| 111 |
vers: VersionTLS10, |
| 112 |
random: make([]byte, 32), |
| 113 |
cipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA}, |
| 114 |
compressionMethods: []uint8{compressionNone}, |
| 115 |
} |
| 116 |
serverConfig := testConfig.Clone() |
| 117 |
// Reset the enabled cipher suites to nil in order to test the |
| 118 |
// defaults. |
| 119 |
serverConfig.CipherSuites = nil |
| 120 |
testClientHelloFailure(t, serverConfig, clientHello, "no cipher suite supported by both client and server") |
| 121 |
} |
| 122 |
|
| 123 |
func TestRejectSNIWithTrailingDot(t *testing.T) { |
| 124 |
testClientHelloFailure(t, testConfig, &clientHelloMsg{ |
| 125 |
vers: VersionTLS12, |
| 126 |
random: make([]byte, 32), |
| 127 |
serverName: "foo.com.", |
| 128 |
}, "unexpected message") |
| 129 |
} |
| 130 |
|
| 131 |
func TestDontSelectECDSAWithRSAKey(t *testing.T) { |
| 132 |
// Test that, even when both sides support an ECDSA cipher suite, it |
| 133 |
// won't be selected if the server's private key doesn't support it. |
| 134 |
clientHello := &clientHelloMsg{ |
| 135 |
vers: VersionTLS10, |
| 136 |
random: make([]byte, 32), |
| 137 |
cipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA}, |
| 138 |
compressionMethods: []uint8{compressionNone}, |
| 139 |
supportedCurves: []CurveID{CurveP256}, |
| 140 |
supportedPoints: []uint8{pointFormatUncompressed}, |
| 141 |
} |
| 142 |
serverConfig := testConfig.Clone() |
| 143 |
serverConfig.CipherSuites = clientHello.cipherSuites |
| 144 |
serverConfig.Certificates = make([]Certificate, 1) |
| 145 |
serverConfig.Certificates[0].Certificate = [][]byte{testECDSACertificate} |
| 146 |
serverConfig.Certificates[0].PrivateKey = testECDSAPrivateKey |
| 147 |
serverConfig.BuildNameToCertificate() |
| 148 |
// First test that it *does* work when the server's key is ECDSA. |
| 149 |
testClientHello(t, serverConfig, clientHello) |
| 150 |
|
| 151 |
// Now test that switching to an RSA key causes the expected error (and |
| 152 |
// not an internal error about a signing failure). |
| 153 |
serverConfig.Certificates = testConfig.Certificates |
| 154 |
testClientHelloFailure(t, serverConfig, clientHello, "no cipher suite supported by both client and server") |
| 155 |
} |
| 156 |
|
| 157 |
func TestDontSelectRSAWithECDSAKey(t *testing.T) { |
| 158 |
// Test that, even when both sides support an RSA cipher suite, it |
| 159 |
// won't be selected if the server's private key doesn't support it. |
| 160 |
clientHello := &clientHelloMsg{ |
| 161 |
vers: VersionTLS10, |
| 162 |
random: make([]byte, 32), |
| 163 |
cipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA}, |
| 164 |
compressionMethods: []uint8{compressionNone}, |
| 165 |
supportedCurves: []CurveID{CurveP256}, |
| 166 |
supportedPoints: []uint8{pointFormatUncompressed}, |
| 167 |
} |
| 168 |
serverConfig := testConfig.Clone() |
| 169 |
serverConfig.CipherSuites = clientHello.cipherSuites |
| 170 |
// First test that it *does* work when the server's key is RSA. |
| 171 |
testClientHello(t, serverConfig, clientHello) |
| 172 |
|
| 173 |
// Now test that switching to an ECDSA key causes the expected error |
| 174 |
// (and not an internal error about a signing failure). |
| 175 |
serverConfig.Certificates = make([]Certificate, 1) |
| 176 |
serverConfig.Certificates[0].Certificate = [][]byte{testECDSACertificate} |
| 177 |
serverConfig.Certificates[0].PrivateKey = testECDSAPrivateKey |
| 178 |
serverConfig.BuildNameToCertificate() |
| 179 |
testClientHelloFailure(t, serverConfig, clientHello, "no cipher suite supported by both client and server") |
| 180 |
} |
| 181 |
|
| 182 |
func TestRenegotiationExtension(t *testing.T) { |
| 183 |
clientHello := &clientHelloMsg{ |
| 184 |
vers: VersionTLS12, |
| 185 |
compressionMethods: []uint8{compressionNone}, |
| 186 |
random: make([]byte, 32), |
| 187 |
secureRenegotiationSupported: true, |
| 188 |
cipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA}, |
| 189 |
} |
| 190 |
|
| 191 |
bufChan := make(chan []byte, 1) |
| 192 |
c, s := localPipe(t) |
| 193 |
|
| 194 |
go func() { |
| 195 |
cli := Client(c, testConfig) |
| 196 |
cli.vers = clientHello.vers |
| 197 |
cli.writeRecord(recordTypeHandshake, clientHello.marshal()) |
| 198 |
|
| 199 |
buf := make([]byte, 1024) |
| 200 |
n, err := c.Read(buf) |
| 201 |
if err != nil { |
| 202 |
t.Errorf("Server read returned error: %s", err) |
| 203 |
return |
| 204 |
} |
| 205 |
c.Close() |
| 206 |
bufChan <- buf[:n] |
| 207 |
}() |
| 208 |
|
| 209 |
Server(s, testConfig).Handshake() |
| 210 |
buf := <-bufChan |
| 211 |
|
| 212 |
if len(buf) < 5+4 { |
| 213 |
t.Fatalf("Server returned short message of length %d", len(buf)) |
| 214 |
} |
| 215 |
// buf contains a TLS record, with a 5 byte record header and a 4 byte |
| 216 |
// handshake header. The length of the ServerHello is taken from the |
| 217 |
// handshake header. |
| 218 |
serverHelloLen := int(buf[6])<<16 | int(buf[7])<<8 | int(buf[8]) |
| 219 |
|
| 220 |
var serverHello serverHelloMsg |
| 221 |
// unmarshal expects to be given the handshake header, but |
| 222 |
// serverHelloLen doesn't include it. |
| 223 |
if !serverHello.unmarshal(buf[5 : 9+serverHelloLen]) { |
| 224 |
t.Fatalf("Failed to parse ServerHello") |
| 225 |
} |
| 226 |
|
| 227 |
if !serverHello.secureRenegotiationSupported { |
| 228 |
t.Errorf("Secure renegotiation extension was not echoed.") |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
func TestTLS12OnlyCipherSuites(t *testing.T) { |
| 233 |
// Test that a Server doesn't select a TLS 1.2-only cipher suite when |
| 234 |
// the client negotiates TLS 1.1. |
| 235 |
clientHello := &clientHelloMsg{ |
| 236 |
vers: VersionTLS11, |
| 237 |
random: make([]byte, 32), |
| 238 |
cipherSuites: []uint16{ |
| 239 |
// The Server, by default, will use the client's |
| 240 |
// preference order. So the GCM cipher suite |
| 241 |
// will be selected unless it's excluded because |
| 242 |
// of the version in this ClientHello. |
| 243 |
TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, |
| 244 |
TLS_RSA_WITH_RC4_128_SHA, |
| 245 |
}, |
| 246 |
compressionMethods: []uint8{compressionNone}, |
| 247 |
supportedCurves: []CurveID{CurveP256, CurveP384, CurveP521}, |
| 248 |
supportedPoints: []uint8{pointFormatUncompressed}, |
| 249 |
} |
| 250 |
|
| 251 |
c, s := localPipe(t) |
| 252 |
replyChan := make(chan any) |
| 253 |
go func() { |
| 254 |
cli := Client(c, testConfig) |
| 255 |
cli.vers = clientHello.vers |
| 256 |
cli.writeRecord(recordTypeHandshake, clientHello.marshal()) |
| 257 |
reply, err := cli.readHandshake() |
| 258 |
c.Close() |
| 259 |
if err != nil { |
| 260 |
replyChan <- err |
| 261 |
} else { |
| 262 |
replyChan <- reply |
| 263 |
} |
| 264 |
}() |
| 265 |
config := testConfig.Clone() |
| 266 |
config.CipherSuites = clientHello.cipherSuites |
| 267 |
Server(s, config).Handshake() |
| 268 |
s.Close() |
| 269 |
reply := <-replyChan |
| 270 |
if err, ok := reply.(error); ok { |
| 271 |
t.Fatal(err) |
| 272 |
} |
| 273 |
serverHello, ok := reply.(*serverHelloMsg) |
| 274 |
if !ok { |
| 275 |
t.Fatalf("didn't get ServerHello message in reply. Got %v\n", reply) |
| 276 |
} |
| 277 |
if s := serverHello.cipherSuite; s != TLS_RSA_WITH_RC4_128_SHA { |
| 278 |
t.Fatalf("bad cipher suite from server: %x", s) |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
func TestTLSPointFormats(t *testing.T) { |
| 283 |
// Test that a Server returns the ec_point_format extension when ECC is |
| 284 |
// negotiated, and not returned on RSA handshake. |
| 285 |
tests := []struct { |
| 286 |
name string |
| 287 |
cipherSuites []uint16 |
| 288 |
supportedCurves []CurveID |
| 289 |
supportedPoints []uint8 |
| 290 |
wantSupportedPoints bool |
| 291 |
}{ |
| 292 |
{"ECC", []uint16{TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA}, []CurveID{CurveP256}, []uint8{compressionNone}, true}, |
| 293 |
{"RSA", []uint16{TLS_RSA_WITH_AES_256_GCM_SHA384}, nil, nil, false}, |
| 294 |
} |
| 295 |
for _, tt := range tests { |
| 296 |
t.Run(tt.name, func(t *testing.T) { |
| 297 |
clientHello := &clientHelloMsg{ |
| 298 |
vers: VersionTLS12, |
| 299 |
random: make([]byte, 32), |
| 300 |
cipherSuites: tt.cipherSuites, |
| 301 |
compressionMethods: []uint8{compressionNone}, |
| 302 |
supportedCurves: tt.supportedCurves, |
| 303 |
supportedPoints: tt.supportedPoints, |
| 304 |
} |
| 305 |
|
| 306 |
c, s := localPipe(t) |
| 307 |
replyChan := make(chan any) |
| 308 |
go func() { |
| 309 |
cli := Client(c, testConfig) |
| 310 |
cli.vers = clientHello.vers |
| 311 |
cli.writeRecord(recordTypeHandshake, clientHello.marshal()) |
| 312 |
reply, err := cli.readHandshake() |
| 313 |
c.Close() |
| 314 |
if err != nil { |
| 315 |
replyChan <- err |
| 316 |
} else { |
| 317 |
replyChan <- reply |
| 318 |
} |
| 319 |
}() |
| 320 |
config := testConfig.Clone() |
| 321 |
config.CipherSuites = clientHello.cipherSuites |
| 322 |
Server(s, config).Handshake() |
| 323 |
s.Close() |
| 324 |
reply := <-replyChan |
| 325 |
if err, ok := reply.(error); ok { |
| 326 |
t.Fatal(err) |
| 327 |
} |
| 328 |
serverHello, ok := reply.(*serverHelloMsg) |
| 329 |
if !ok { |
| 330 |
t.Fatalf("didn't get ServerHello message in reply. Got %v\n", reply) |
| 331 |
} |
| 332 |
if tt.wantSupportedPoints { |
| 333 |
if len(serverHello.supportedPoints) < 1 { |
| 334 |
t.Fatal("missing ec_point_format extension from server") |
| 335 |
} |
| 336 |
found := false |
| 337 |
for _, p := range serverHello.supportedPoints { |
| 338 |
if p == pointFormatUncompressed { |
| 339 |
found = true |
| 340 |
break |
| 341 |
} |
| 342 |
} |
| 343 |
if !found { |
| 344 |
t.Fatal("missing uncompressed format in ec_point_format extension from server") |
| 345 |
} |
| 346 |
} else { |
| 347 |
if len(serverHello.supportedPoints) != 0 { |
| 348 |
t.Fatalf("unexcpected ec_point_format extension from server: %v", serverHello.supportedPoints) |
| 349 |
} |
| 350 |
} |
| 351 |
}) |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
func TestAlertForwarding(t *testing.T) { |
| 356 |
c, s := localPipe(t) |
| 357 |
go func() { |
| 358 |
Client(c, testConfig).sendAlert(alertUnknownCA) |
| 359 |
c.Close() |
| 360 |
}() |
| 361 |
|
| 362 |
err := Server(s, testConfig).Handshake() |
| 363 |
s.Close() |
| 364 |
var opErr *net.OpError |
| 365 |
if !errors.As(err, &opErr) || opErr.Err != error(alertUnknownCA) { |
| 366 |
t.Errorf("Got error: %s; expected: %s", err, error(alertUnknownCA)) |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
func TestClose(t *testing.T) { |
| 371 |
c, s := localPipe(t) |
| 372 |
go c.Close() |
| 373 |
|
| 374 |
err := Server(s, testConfig).Handshake() |
| 375 |
s.Close() |
| 376 |
if err != io.EOF { |
| 377 |
t.Errorf("Got error: %s; expected: %s", err, io.EOF) |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
func TestVersion(t *testing.T) { |
| 382 |
serverConfig := &Config{ |
| 383 |
Certificates: testConfig.Certificates, |
| 384 |
MaxVersion: VersionTLS11, |
| 385 |
} |
| 386 |
clientConfig := &Config{ |
| 387 |
InsecureSkipVerify: true, |
| 388 |
MinVersion: VersionTLS10, |
| 389 |
} |
| 390 |
state, _, err := testHandshake(t, clientConfig, serverConfig) |
| 391 |
if err != nil { |
| 392 |
t.Fatalf("handshake failed: %s", err) |
| 393 |
} |
| 394 |
if state.Version != VersionTLS11 { |
| 395 |
t.Fatalf("incorrect version %x, should be %x", state.Version, VersionTLS11) |
| 396 |
} |
| 397 |
|
| 398 |
clientConfig.MinVersion = 0 |
| 399 |
_, _, err = testHandshake(t, clientConfig, serverConfig) |
| 400 |
if err == nil { |
| 401 |
t.Fatalf("expected failure to connect with TLS 1.0/1.1") |
| 402 |
} |
| 403 |
|
| 404 |
defer func(old bool) { debugEnableTLS10 = old }(debugEnableTLS10) |
| 405 |
debugEnableTLS10 = true |
| 406 |
_, _, err = testHandshake(t, clientConfig, serverConfig) |
| 407 |
if err != nil { |
| 408 |
t.Fatalf("handshake failed: %s", err) |
| 409 |
} |
| 410 |
if state.Version != VersionTLS11 { |
| 411 |
t.Fatalf("incorrect version %x, should be %x", state.Version, VersionTLS11) |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
func TestCipherSuitePreference(t *testing.T) { |
| 416 |
serverConfig := &Config{ |
| 417 |
CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA, TLS_AES_128_GCM_SHA256, |
| 418 |
TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256}, |
| 419 |
Certificates: testConfig.Certificates, |
| 420 |
MaxVersion: VersionTLS12, |
| 421 |
GetConfigForClient: func(chi *ClientHelloInfo) (*Config, error) { |
| 422 |
if chi.CipherSuites[0] != TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 { |
| 423 |
t.Error("the advertised order should not depend on Config.CipherSuites") |
| 424 |
} |
| 425 |
if len(chi.CipherSuites) != 2+len(defaultCipherSuitesTLS13) { |
| 426 |
t.Error("the advertised TLS 1.2 suites should be filtered by Config.CipherSuites") |
| 427 |
} |
| 428 |
return nil, nil |
| 429 |
}, |
| 430 |
} |
| 431 |
clientConfig := &Config{ |
| 432 |
CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA, TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256}, |
| 433 |
InsecureSkipVerify: true, |
| 434 |
} |
| 435 |
state, _, err := testHandshake(t, clientConfig, serverConfig) |
| 436 |
if err != nil { |
| 437 |
t.Fatalf("handshake failed: %s", err) |
| 438 |
} |
| 439 |
if state.CipherSuite != TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 { |
| 440 |
t.Error("the preference order should not depend on Config.CipherSuites") |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
func TestSCTHandshake(t *testing.T) { |
| 445 |
t.Run("TLSv12", func(t *testing.T) { testSCTHandshake(t, VersionTLS12) }) |
| 446 |
t.Run("TLSv13", func(t *testing.T) { testSCTHandshake(t, VersionTLS13) }) |
| 447 |
} |
| 448 |
|
| 449 |
func testSCTHandshake(t *testing.T, version uint16) { |
| 450 |
expected := [][]byte{[]byte("certificate"), []byte("transparency")} |
| 451 |
serverConfig := &Config{ |
| 452 |
Certificates: []Certificate{{ |
| 453 |
Certificate: [][]byte{testRSACertificate}, |
| 454 |
PrivateKey: testRSAPrivateKey, |
| 455 |
SignedCertificateTimestamps: expected, |
| 456 |
}}, |
| 457 |
MaxVersion: version, |
| 458 |
} |
| 459 |
clientConfig := &Config{ |
| 460 |
InsecureSkipVerify: true, |
| 461 |
} |
| 462 |
_, state, err := testHandshake(t, clientConfig, serverConfig) |
| 463 |
if err != nil { |
| 464 |
t.Fatalf("handshake failed: %s", err) |
| 465 |
} |
| 466 |
actual := state.SignedCertificateTimestamps |
| 467 |
if len(actual) != len(expected) { |
| 468 |
t.Fatalf("got %d scts, want %d", len(actual), len(expected)) |
| 469 |
} |
| 470 |
for i, sct := range expected { |
| 471 |
if !bytes.Equal(sct, actual[i]) { |
| 472 |
t.Fatalf("SCT #%d was %x, but expected %x", i, actual[i], sct) |
| 473 |
} |
| 474 |
} |
| 475 |
} |
| 476 |
|
| 477 |
func TestCrossVersionResume(t *testing.T) { |
| 478 |
t.Run("TLSv12", func(t *testing.T) { testCrossVersionResume(t, VersionTLS12) }) |
| 479 |
t.Run("TLSv13", func(t *testing.T) { testCrossVersionResume(t, VersionTLS13) }) |
| 480 |
} |
| 481 |
|
| 482 |
func testCrossVersionResume(t *testing.T, version uint16) { |
| 483 |
serverConfig := &Config{ |
| 484 |
CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA}, |
| 485 |
Certificates: testConfig.Certificates, |
| 486 |
Time: testTime, |
| 487 |
} |
| 488 |
clientConfig := &Config{ |
| 489 |
CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA}, |
| 490 |
InsecureSkipVerify: true, |
| 491 |
ClientSessionCache: NewLRUClientSessionCache(1), |
| 492 |
ServerName: "servername", |
| 493 |
MinVersion: VersionTLS10, |
| 494 |
Time: testTime, |
| 495 |
} |
| 496 |
|
| 497 |
// Establish a session at TLS 1.1. |
| 498 |
clientConfig.MaxVersion = VersionTLS11 |
| 499 |
_, _, err := testHandshake(t, clientConfig, serverConfig) |
| 500 |
if err != nil { |
| 501 |
t.Fatalf("handshake failed: %s", err) |
| 502 |
} |
| 503 |
|
| 504 |
// The client session cache now contains a TLS 1.1 session. |
| 505 |
state, _, err := testHandshake(t, clientConfig, serverConfig) |
| 506 |
if err != nil { |
| 507 |
t.Fatalf("handshake failed: %s", err) |
| 508 |
} |
| 509 |
if !state.DidResume { |
| 510 |
t.Fatalf("handshake did not resume at the same version") |
| 511 |
} |
| 512 |
|
| 513 |
// Test that the server will decline to resume at a lower version. |
| 514 |
clientConfig.MaxVersion = VersionTLS10 |
| 515 |
state, _, err = testHandshake(t, clientConfig, serverConfig) |
| 516 |
if err != nil { |
| 517 |
t.Fatalf("handshake failed: %s", err) |
| 518 |
} |
| 519 |
if state.DidResume { |
| 520 |
t.Fatalf("handshake resumed at a lower version") |
| 521 |
} |
| 522 |
|
| 523 |
// The client session cache now contains a TLS 1.0 session. |
| 524 |
state, _, err = testHandshake(t, clientConfig, serverConfig) |
| 525 |
if err != nil { |
| 526 |
t.Fatalf("handshake failed: %s", err) |
| 527 |
} |
| 528 |
if !state.DidResume { |
| 529 |
t.Fatalf("handshake did not resume at the same version") |
| 530 |
} |
| 531 |
|
| 532 |
// Test that the server will decline to resume at a higher version. |
| 533 |
clientConfig.MaxVersion = VersionTLS11 |
| 534 |
state, _, err = testHandshake(t, clientConfig, serverConfig) |
| 535 |
if err != nil { |
| 536 |
t.Fatalf("handshake failed: %s", err) |
| 537 |
} |
| 538 |
if state.DidResume { |
| 539 |
t.Fatalf("handshake resumed at a higher version") |
| 540 |
} |
| 541 |
} |
| 542 |
|
| 543 |
// Note: see comment in handshake_test.go for details of how the reference |
| 544 |
// tests work. |
| 545 |
|
| 546 |
// serverTest represents a test of the TLS server handshake against a reference |
| 547 |
// implementation. |
| 548 |
type serverTest struct { |
| 549 |
// name is a freeform string identifying the test and the file in which |
| 550 |
// the expected results will be stored. |
| 551 |
name string |
| 552 |
// command, if not empty, contains a series of arguments for the |
| 553 |
// command to run for the reference server. |
| 554 |
command []string |
| 555 |
// expectedPeerCerts contains a list of PEM blocks of expected |
| 556 |
// certificates from the client. |
| 557 |
expectedPeerCerts []string |
| 558 |
// config, if not nil, contains a custom Config to use for this test. |
| 559 |
config *Config |
| 560 |
// expectHandshakeErrorIncluding, when not empty, contains a string |
| 561 |
// that must be a substring of the error resulting from the handshake. |
| 562 |
expectHandshakeErrorIncluding string |
| 563 |
// validate, if not nil, is a function that will be called with the |
| 564 |
// ConnectionState of the resulting connection. It returns false if the |
| 565 |
// ConnectionState is unacceptable. |
| 566 |
validate func(ConnectionState) error |
| 567 |
// wait, if true, prevents this subtest from calling t.Parallel. |
| 568 |
// If false, runServerTest* returns immediately. |
| 569 |
wait bool |
| 570 |
} |
| 571 |
|
| 572 |
var defaultClientCommand = []string{"openssl", "s_client", "-no_ticket"} |
| 573 |
|
| 574 |
// connFromCommand starts opens a listening socket and starts the reference |
| 575 |
// client to connect to it. It returns a recordingConn that wraps the resulting |
| 576 |
// connection. |
| 577 |
func (test *serverTest) connFromCommand() (conn *recordingConn, child *exec.Cmd, err error) { |
| 578 |
l, err := net.ListenTCP("tcp", &net.TCPAddr{ |
| 579 |
IP: net.IPv4(127, 0, 0, 1), |
| 580 |
Port: 0, |
| 581 |
}) |
| 582 |
if err != nil { |
| 583 |
return nil, nil, err |
| 584 |
} |
| 585 |
defer l.Close() |
| 586 |
|
| 587 |
port := l.Addr().(*net.TCPAddr).Port |
| 588 |
|
| 589 |
var command []string |
| 590 |
command = append(command, test.command...) |
| 591 |
if len(command) == 0 { |
| 592 |
command = defaultClientCommand |
| 593 |
} |
| 594 |
command = append(command, "-connect") |
| 595 |
command = append(command, fmt.Sprintf("127.0.0.1:%d", port)) |
| 596 |
cmd := exec.Command(command[0], command[1:]...) |
| 597 |
cmd.Stdin = nil |
| 598 |
var output bytes.Buffer |
| 599 |
cmd.Stdout = &output |
| 600 |
cmd.Stderr = &output |
| 601 |
if err := cmd.Start(); err != nil { |
| 602 |
return nil, nil, err |
| 603 |
} |
| 604 |
|
| 605 |
connChan := make(chan any, 1) |
| 606 |
go func() { |
| 607 |
tcpConn, err := l.Accept() |
| 608 |
if err != nil { |
| 609 |
connChan <- err |
| 610 |
return |
| 611 |
} |
| 612 |
connChan <- tcpConn |
| 613 |
}() |
| 614 |
|
| 615 |
var tcpConn net.Conn |
| 616 |
select { |
| 617 |
case connOrError := <-connChan: |
| 618 |
if err, ok := connOrError.(error); ok { |
| 619 |
return nil, nil, err |
| 620 |
} |
| 621 |
tcpConn = connOrError.(net.Conn) |
| 622 |
case <-time.After(2 * time.Second): |
| 623 |
return nil, nil, errors.New("timed out waiting for connection from child process") |
| 624 |
} |
| 625 |
|
| 626 |
record := &recordingConn{ |
| 627 |
Conn: tcpConn, |
| 628 |
} |
| 629 |
|
| 630 |
return record, cmd, nil |
| 631 |
} |
| 632 |
|
| 633 |
func (test *serverTest) dataPath() string { |
| 634 |
return filepath.Join("testdata", "Server-"+test.name) |
| 635 |
} |
| 636 |
|
| 637 |
func (test *serverTest) loadData() (flows [][]byte, err error) { |
| 638 |
in, err := os.Open(test.dataPath()) |
| 639 |
if err != nil { |
| 640 |
return nil, err |
| 641 |
} |
| 642 |
defer in.Close() |
| 643 |
return parseTestData(in) |
| 644 |
} |
| 645 |
|
| 646 |
func (test *serverTest) run(t *testing.T, write bool) { |
| 647 |
var clientConn, serverConn net.Conn |
| 648 |
var recordingConn *recordingConn |
| 649 |
var childProcess *exec.Cmd |
| 650 |
|
| 651 |
if write { |
| 652 |
var err error |
| 653 |
recordingConn, childProcess, err = test.connFromCommand() |
| 654 |
if err != nil { |
| 655 |
t.Fatalf("Failed to start subcommand: %s", err) |
| 656 |
} |
| 657 |
serverConn = recordingConn |
| 658 |
defer func() { |
| 659 |
if t.Failed() { |
| 660 |
t.Logf("OpenSSL output:\n\n%s", childProcess.Stdout) |
| 661 |
} |
| 662 |
}() |
| 663 |
} else { |
| 664 |
clientConn, serverConn = localPipe(t) |
| 665 |
} |
| 666 |
config := test.config |
| 667 |
if config == nil { |
| 668 |
config = testConfig |
| 669 |
} |
| 670 |
server := Server(serverConn, config) |
| 671 |
connStateChan := make(chan ConnectionState, 1) |
| 672 |
go func() { |
| 673 |
_, err := server.Write([]byte("hello, world\n")) |
| 674 |
if len(test.expectHandshakeErrorIncluding) > 0 { |
| 675 |
if err == nil { |
| 676 |
t.Errorf("Error expected, but no error returned") |
| 677 |
} else if s := err.Error(); !strings.Contains(s, test.expectHandshakeErrorIncluding) { |
| 678 |
t.Errorf("Error expected containing '%s' but got '%s'", test.expectHandshakeErrorIncluding, s) |
| 679 |
} |
| 680 |
} else { |
| 681 |
if err != nil { |
| 682 |
t.Logf("Error from Server.Write: '%s'", err) |
| 683 |
} |
| 684 |
} |
| 685 |
server.Close() |
| 686 |
serverConn.Close() |
| 687 |
connStateChan <- server.ConnectionState() |
| 688 |
}() |
| 689 |
|
| 690 |
if !write { |
| 691 |
flows, err := test.loadData() |
| 692 |
if err != nil { |
| 693 |
t.Fatalf("%s: failed to load data from %s", test.name, test.dataPath()) |
| 694 |
} |
| 695 |
for i, b := range flows { |
| 696 |
if i%2 == 0 { |
| 697 |
if *fast { |
| 698 |
clientConn.SetWriteDeadline(time.Now().Add(1 * time.Second)) |
| 699 |
} else { |
| 700 |
clientConn.SetWriteDeadline(time.Now().Add(1 * time.Minute)) |
| 701 |
} |
| 702 |
clientConn.Write(b) |
| 703 |
continue |
| 704 |
} |
| 705 |
bb := make([]byte, len(b)) |
| 706 |
if *fast { |
| 707 |
clientConn.SetReadDeadline(time.Now().Add(1 * time.Second)) |
| 708 |
} else { |
| 709 |
clientConn.SetReadDeadline(time.Now().Add(1 * time.Minute)) |
| 710 |
} |
| 711 |
n, err := io.ReadFull(clientConn, bb) |
| 712 |
if err != nil { |
| 713 |
t.Fatalf("%s #%d: %s\nRead %d, wanted %d, got %x, wanted %x\n", test.name, i+1, err, n, len(bb), bb[:n], b) |
| 714 |
} |
| 715 |
if !bytes.Equal(b, bb) { |
| 716 |
t.Fatalf("%s #%d: mismatch on read: got:%x want:%x", test.name, i+1, bb, b) |
| 717 |
} |
| 718 |
} |
| 719 |
clientConn.Close() |
| 720 |
} |
| 721 |
|
| 722 |
connState := <-connStateChan |
| 723 |
peerCerts := connState.PeerCertificates |
| 724 |
if len(peerCerts) == len(test.expectedPeerCerts) { |
| 725 |
for i, peerCert := range peerCerts { |
| 726 |
block, _ := pem.Decode([]byte(test.expectedPeerCerts[i])) |
| 727 |
if !bytes.Equal(block.Bytes, peerCert.Raw) { |
| 728 |
t.Fatalf("%s: mismatch on peer cert %d", test.name, i+1) |
| 729 |
} |
| 730 |
} |
| 731 |
} else { |
| 732 |
t.Fatalf("%s: mismatch on peer list length: %d (wanted) != %d (got)", test.name, len(test.expectedPeerCerts), len(peerCerts)) |
| 733 |
} |
| 734 |
|
| 735 |
if test.validate != nil { |
| 736 |
if err := test.validate(connState); err != nil { |
| 737 |
t.Fatalf("validate callback returned error: %s", err) |
| 738 |
} |
| 739 |
} |
| 740 |
|
| 741 |
if write { |
| 742 |
path := test.dataPath() |
| 743 |
out, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) |
| 744 |
if err != nil { |
| 745 |
t.Fatalf("Failed to create output file: %s", err) |
| 746 |
} |
| 747 |
defer out.Close() |
| 748 |
recordingConn.Close() |
| 749 |
if len(recordingConn.flows) < 3 { |
| 750 |
if len(test.expectHandshakeErrorIncluding) == 0 { |
| 751 |
t.Fatalf("Handshake failed") |
| 752 |
} |
| 753 |
} |
| 754 |
recordingConn.WriteTo(out) |
| 755 |
t.Logf("Wrote %s\n", path) |
| 756 |
childProcess.Wait() |
| 757 |
} |
| 758 |
} |
| 759 |
|
| 760 |
func runServerTestForVersion(t *testing.T, template *serverTest, version, option string) { |
| 761 |
// Make a deep copy of the template before going parallel. |
| 762 |
test := *template |
| 763 |
if template.config != nil { |
| 764 |
test.config = template.config.Clone() |
| 765 |
} |
| 766 |
test.name = version + "-" + test.name |
| 767 |
if len(test.command) == 0 { |
| 768 |
test.command = defaultClientCommand |
| 769 |
} |
| 770 |
test.command = append([]string(nil), test.command...) |
| 771 |
test.command = append(test.command, option) |
| 772 |
|
| 773 |
runTestAndUpdateIfNeeded(t, version, test.run, test.wait) |
| 774 |
} |
| 775 |
|
| 776 |
func runServerTestTLS10(t *testing.T, template *serverTest) { |
| 777 |
runServerTestForVersion(t, template, "TLSv10", "-tls1") |
| 778 |
} |
| 779 |
|
| 780 |
func runServerTestTLS11(t *testing.T, template *serverTest) { |
| 781 |
runServerTestForVersion(t, template, "TLSv11", "-tls1_1") |
| 782 |
} |
| 783 |
|
| 784 |
func runServerTestTLS12(t *testing.T, template *serverTest) { |
| 785 |
runServerTestForVersion(t, template, "TLSv12", "-tls1_2") |
| 786 |
} |
| 787 |
|
| 788 |
func runServerTestTLS13(t *testing.T, template *serverTest) { |
| 789 |
runServerTestForVersion(t, template, "TLSv13", "-tls1_3") |
| 790 |
} |
| 791 |
|
| 792 |
func TestHandshakeServerRSARC4(t *testing.T) { |
| 793 |
test := &serverTest{ |
| 794 |
name: "RSA-RC4", |
| 795 |
command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "RC4-SHA"}, |
| 796 |
} |
| 797 |
runServerTestTLS10(t, test) |
| 798 |
runServerTestTLS11(t, test) |
| 799 |
runServerTestTLS12(t, test) |
| 800 |
} |
| 801 |
|
| 802 |
func TestHandshakeServerRSA3DES(t *testing.T) { |
| 803 |
test := &serverTest{ |
| 804 |
name: "RSA-3DES", |
| 805 |
command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "DES-CBC3-SHA"}, |
| 806 |
} |
| 807 |
runServerTestTLS10(t, test) |
| 808 |
runServerTestTLS12(t, test) |
| 809 |
} |
| 810 |
|
| 811 |
func TestHandshakeServerRSAAES(t *testing.T) { |
| 812 |
test := &serverTest{ |
| 813 |
name: "RSA-AES", |
| 814 |
command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "AES128-SHA"}, |
| 815 |
} |
| 816 |
runServerTestTLS10(t, test) |
| 817 |
runServerTestTLS12(t, test) |
| 818 |
} |
| 819 |
|
| 820 |
func TestHandshakeServerAESGCM(t *testing.T) { |
| 821 |
test := &serverTest{ |
| 822 |
name: "RSA-AES-GCM", |
| 823 |
command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "ECDHE-RSA-AES128-GCM-SHA256"}, |
| 824 |
} |
| 825 |
runServerTestTLS12(t, test) |
| 826 |
} |
| 827 |
|
| 828 |
func TestHandshakeServerAES256GCMSHA384(t *testing.T) { |
| 829 |
test := &serverTest{ |
| 830 |
name: "RSA-AES256-GCM-SHA384", |
| 831 |
command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "ECDHE-RSA-AES256-GCM-SHA384"}, |
| 832 |
} |
| 833 |
runServerTestTLS12(t, test) |
| 834 |
} |
| 835 |
|
| 836 |
func TestHandshakeServerAES128SHA256(t *testing.T) { |
| 837 |
test := &serverTest{ |
| 838 |
name: "AES128-SHA256", |
| 839 |
command: []string{"openssl", "s_client", "-no_ticket", "-ciphersuites", "TLS_AES_128_GCM_SHA256"}, |
| 840 |
} |
| 841 |
runServerTestTLS13(t, test) |
| 842 |
} |
| 843 |
func TestHandshakeServerAES256SHA384(t *testing.T) { |
| 844 |
test := &serverTest{ |
| 845 |
name: "AES256-SHA384", |
| 846 |
command: []string{"openssl", "s_client", "-no_ticket", "-ciphersuites", "TLS_AES_256_GCM_SHA384"}, |
| 847 |
} |
| 848 |
runServerTestTLS13(t, test) |
| 849 |
} |
| 850 |
func TestHandshakeServerCHACHA20SHA256(t *testing.T) { |
| 851 |
test := &serverTest{ |
| 852 |
name: "CHACHA20-SHA256", |
| 853 |
command: []string{"openssl", "s_client", "-no_ticket", "-ciphersuites", "TLS_CHACHA20_POLY1305_SHA256"}, |
| 854 |
} |
| 855 |
runServerTestTLS13(t, test) |
| 856 |
} |
| 857 |
|
| 858 |
func TestHandshakeServerECDHEECDSAAES(t *testing.T) { |
| 859 |
config := testConfig.Clone() |
| 860 |
config.Certificates = make([]Certificate, 1) |
| 861 |
config.Certificates[0].Certificate = [][]byte{testECDSACertificate} |
| 862 |
config.Certificates[0].PrivateKey = testECDSAPrivateKey |
| 863 |
config.BuildNameToCertificate() |
| 864 |
|
| 865 |
test := &serverTest{ |
| 866 |
name: "ECDHE-ECDSA-AES", |
| 867 |
command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "ECDHE-ECDSA-AES256-SHA", "-ciphersuites", "TLS_AES_128_GCM_SHA256"}, |
| 868 |
config: config, |
| 869 |
} |
| 870 |
runServerTestTLS10(t, test) |
| 871 |
runServerTestTLS12(t, test) |
| 872 |
runServerTestTLS13(t, test) |
| 873 |
} |
| 874 |
|
| 875 |
func TestHandshakeServerX25519(t *testing.T) { |
| 876 |
config := testConfig.Clone() |
| 877 |
config.CurvePreferences = []CurveID{X25519} |
| 878 |
|
| 879 |
test := &serverTest{ |
| 880 |
name: "X25519", |
| 881 |
command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "ECDHE-RSA-CHACHA20-POLY1305", "-ciphersuites", "TLS_CHACHA20_POLY1305_SHA256", "-curves", "X25519"}, |
| 882 |
config: config, |
| 883 |
} |
| 884 |
runServerTestTLS12(t, test) |
| 885 |
runServerTestTLS13(t, test) |
| 886 |
} |
| 887 |
|
| 888 |
func TestHandshakeServerP256(t *testing.T) { |
| 889 |
config := testConfig.Clone() |
| 890 |
config.CurvePreferences = []CurveID{CurveP256} |
| 891 |
|
| 892 |
test := &serverTest{ |
| 893 |
name: "P256", |
| 894 |
command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "ECDHE-RSA-CHACHA20-POLY1305", "-ciphersuites", "TLS_CHACHA20_POLY1305_SHA256", "-curves", "P-256"}, |
| 895 |
config: config, |
| 896 |
} |
| 897 |
runServerTestTLS12(t, test) |
| 898 |
runServerTestTLS13(t, test) |
| 899 |
} |
| 900 |
|
| 901 |
func TestHandshakeServerHelloRetryRequest(t *testing.T) { |
| 902 |
config := testConfig.Clone() |
| 903 |
config.CurvePreferences = []CurveID{CurveP256} |
| 904 |
|
| 905 |
test := &serverTest{ |
| 906 |
name: "HelloRetryRequest", |
| 907 |
command: []string{"openssl", "s_client", "-no_ticket", "-ciphersuites", "TLS_CHACHA20_POLY1305_SHA256", "-curves", "X25519:P-256"}, |
| 908 |
config: config, |
| 909 |
} |
| 910 |
runServerTestTLS13(t, test) |
| 911 |
} |
| 912 |
|
| 913 |
func TestHandshakeServerALPN(t *testing.T) { |
| 914 |
config := testConfig.Clone() |
| 915 |
config.NextProtos = []string{"proto1", "proto2"} |
| 916 |
|
| 917 |
test := &serverTest{ |
| 918 |
name: "ALPN", |
| 919 |
// Note that this needs OpenSSL 1.0.2 because that is the first |
| 920 |
// version that supports the -alpn flag. |
| 921 |
command: []string{"openssl", "s_client", "-alpn", "proto2,proto1", "-cipher", "ECDHE-RSA-CHACHA20-POLY1305", "-ciphersuites", "TLS_CHACHA20_POLY1305_SHA256"}, |
| 922 |
config: config, |
| 923 |
validate: func(state ConnectionState) error { |
| 924 |
// The server's preferences should override the client. |
| 925 |
if state.NegotiatedProtocol != "proto1" { |
| 926 |
return fmt.Errorf("Got protocol %q, wanted proto1", state.NegotiatedProtocol) |
| 927 |
} |
| 928 |
return nil |
| 929 |
}, |
| 930 |
} |
| 931 |
runServerTestTLS12(t, test) |
| 932 |
runServerTestTLS13(t, test) |
| 933 |
} |
| 934 |
|
| 935 |
func TestHandshakeServerALPNNoMatch(t *testing.T) { |
| 936 |
config := testConfig.Clone() |
| 937 |
config.NextProtos = []string{"proto3"} |
| 938 |
|
| 939 |
test := &serverTest{ |
| 940 |
name: "ALPN-NoMatch", |
| 941 |
// Note that this needs OpenSSL 1.0.2 because that is the first |
| 942 |
// version that supports the -alpn flag. |
| 943 |
command: []string{"openssl", "s_client", "-alpn", "proto2,proto1", "-cipher", "ECDHE-RSA-CHACHA20-POLY1305", "-ciphersuites", "TLS_CHACHA20_POLY1305_SHA256"}, |
| 944 |
config: config, |
| 945 |
expectHandshakeErrorIncluding: "client requested unsupported application protocol", |
| 946 |
} |
| 947 |
runServerTestTLS12(t, test) |
| 948 |
runServerTestTLS13(t, test) |
| 949 |
} |
| 950 |
|
| 951 |
func TestHandshakeServerALPNNotConfigured(t *testing.T) { |
| 952 |
config := testConfig.Clone() |
| 953 |
config.NextProtos = nil |
| 954 |
|
| 955 |
test := &serverTest{ |
| 956 |
name: "ALPN-NotConfigured", |
| 957 |
// Note that this needs OpenSSL 1.0.2 because that is the first |
| 958 |
// version that supports the -alpn flag. |
| 959 |
command: []string{"openssl", "s_client", "-alpn", "proto2,proto1", "-cipher", "ECDHE-RSA-CHACHA20-POLY1305", "-ciphersuites", "TLS_CHACHA20_POLY1305_SHA256"}, |
| 960 |
config: config, |
| 961 |
validate: func(state ConnectionState) error { |
| 962 |
if state.NegotiatedProtocol != "" { |
| 963 |
return fmt.Errorf("Got protocol %q, wanted nothing", state.NegotiatedProtocol) |
| 964 |
} |
| 965 |
return nil |
| 966 |
}, |
| 967 |
} |
| 968 |
runServerTestTLS12(t, test) |
| 969 |
runServerTestTLS13(t, test) |
| 970 |
} |
| 971 |
|
| 972 |
func TestHandshakeServerALPNFallback(t *testing.T) { |
| 973 |
config := testConfig.Clone() |
| 974 |
config.NextProtos = []string{"proto1", "h2", "proto2"} |
| 975 |
|
| 976 |
test := &serverTest{ |
| 977 |
name: "ALPN-Fallback", |
| 978 |
// Note that this needs OpenSSL 1.0.2 because that is the first |
| 979 |
// version that supports the -alpn flag. |
| 980 |
command: []string{"openssl", "s_client", "-alpn", "proto3,http/1.1,proto4", "-cipher", "ECDHE-RSA-CHACHA20-POLY1305", "-ciphersuites", "TLS_CHACHA20_POLY1305_SHA256"}, |
| 981 |
config: config, |
| 982 |
validate: func(state ConnectionState) error { |
| 983 |
if state.NegotiatedProtocol != "" { |
| 984 |
return fmt.Errorf("Got protocol %q, wanted nothing", state.NegotiatedProtocol) |
| 985 |
} |
| 986 |
return nil |
| 987 |
}, |
| 988 |
} |
| 989 |
runServerTestTLS12(t, test) |
| 990 |
runServerTestTLS13(t, test) |
| 991 |
} |
| 992 |
|
| 993 |
// TestHandshakeServerSNI involves a client sending an SNI extension of |
| 994 |
// "snitest.com", which happens to match the CN of testSNICertificate. The test |
| 995 |
// verifies that the server correctly selects that certificate. |
| 996 |
func TestHandshakeServerSNI(t *testing.T) { |
| 997 |
test := &serverTest{ |
| 998 |
name: "SNI", |
| 999 |
command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "AES128-SHA", "-servername", "snitest.com"}, |
| 1000 |
} |
| 1001 |
runServerTestTLS12(t, test) |
| 1002 |
} |
| 1003 |
|
| 1004 |
// TestHandshakeServerSNICertForName is similar to TestHandshakeServerSNI, but |
| 1005 |
// tests the dynamic GetCertificate method |
| 1006 |
func TestHandshakeServerSNIGetCertificate(t *testing.T) { |
| 1007 |
config := testConfig.Clone() |
| 1008 |
|
| 1009 |
// Replace the NameToCertificate map with a GetCertificate function |
| 1010 |
nameToCert := config.NameToCertificate |
| 1011 |
config.NameToCertificate = nil |
| 1012 |
config.GetCertificate = func(clientHello *ClientHelloInfo) (*Certificate, error) { |
| 1013 |
cert := nameToCert[clientHello.ServerName] |
| 1014 |
return cert, nil |
| 1015 |
} |
| 1016 |
test := &serverTest{ |
| 1017 |
name: "SNI-GetCertificate", |
| 1018 |
command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "AES128-SHA", "-servername", "snitest.com"}, |
| 1019 |
config: config, |
| 1020 |
} |
| 1021 |
runServerTestTLS12(t, test) |
| 1022 |
} |
| 1023 |
|
| 1024 |
// TestHandshakeServerSNICertForNameNotFound is similar to |
| 1025 |
// TestHandshakeServerSNICertForName, but tests to make sure that when the |
| 1026 |
// GetCertificate method doesn't return a cert, we fall back to what's in |
| 1027 |
// the NameToCertificate map. |
| 1028 |
func TestHandshakeServerSNIGetCertificateNotFound(t *testing.T) { |
| 1029 |
config := testConfig.Clone() |
| 1030 |
|
| 1031 |
config.GetCertificate = func(clientHello *ClientHelloInfo) (*Certificate, error) { |
| 1032 |
return nil, nil |
| 1033 |
} |
| 1034 |
test := &serverTest{ |
| 1035 |
name: "SNI-GetCertificateNotFound", |
| 1036 |
command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "AES128-SHA", "-servername", "snitest.com"}, |
| 1037 |
config: config, |
| 1038 |
} |
| 1039 |
runServerTestTLS12(t, test) |
| 1040 |
} |
| 1041 |
|
| 1042 |
// TestHandshakeServerSNICertForNameError tests to make sure that errors in |
| 1043 |
// GetCertificate result in a tls alert. |
| 1044 |
func TestHandshakeServerSNIGetCertificateError(t *testing.T) { |
| 1045 |
const errMsg = "TestHandshakeServerSNIGetCertificateError error" |
| 1046 |
|
| 1047 |
serverConfig := testConfig.Clone() |
| 1048 |
serverConfig.GetCertificate = func(clientHello *ClientHelloInfo) (*Certificate, error) { |
| 1049 |
return nil, errors.New(errMsg) |
| 1050 |
} |
| 1051 |
|
| 1052 |
clientHello := &clientHelloMsg{ |
| 1053 |
vers: VersionTLS10, |
| 1054 |
random: make([]byte, 32), |
| 1055 |
cipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA}, |
| 1056 |
compressionMethods: []uint8{compressionNone}, |
| 1057 |
serverName: "test", |
| 1058 |
} |
| 1059 |
testClientHelloFailure(t, serverConfig, clientHello, errMsg) |
| 1060 |
} |
| 1061 |
|
| 1062 |
// TestHandshakeServerEmptyCertificates tests that GetCertificates is called in |
| 1063 |
// the case that Certificates is empty, even without SNI. |
| 1064 |
func TestHandshakeServerEmptyCertificates(t *testing.T) { |
| 1065 |
const errMsg = "TestHandshakeServerEmptyCertificates error" |
| 1066 |
|
| 1067 |
serverConfig := testConfig.Clone() |
| 1068 |
serverConfig.GetCertificate = func(clientHello *ClientHelloInfo) (*Certificate, error) { |
| 1069 |
return nil, errors.New(errMsg) |
| 1070 |
} |
| 1071 |
serverConfig.Certificates = nil |
| 1072 |
|
| 1073 |
clientHello := &clientHelloMsg{ |
| 1074 |
vers: VersionTLS10, |
| 1075 |
random: make([]byte, 32), |
| 1076 |
cipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA}, |
| 1077 |
compressionMethods: []uint8{compressionNone}, |
| 1078 |
} |
| 1079 |
testClientHelloFailure(t, serverConfig, clientHello, errMsg) |
| 1080 |
|
| 1081 |
// With an empty Certificates and a nil GetCertificate, the server |
| 1082 |
// should always return a “no certificates” error. |
| 1083 |
serverConfig.GetCertificate = nil |
| 1084 |
|
| 1085 |
clientHello = &clientHelloMsg{ |
| 1086 |
vers: VersionTLS10, |
| 1087 |
random: make([]byte, 32), |
| 1088 |
cipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA}, |
| 1089 |
compressionMethods: []uint8{compressionNone}, |
| 1090 |
} |
| 1091 |
testClientHelloFailure(t, serverConfig, clientHello, "no certificates") |
| 1092 |
} |
| 1093 |
|
| 1094 |
func TestServerResumption(t *testing.T) { |
| 1095 |
sessionFilePath := tempFile("") |
| 1096 |
defer os.Remove(sessionFilePath) |
| 1097 |
|
| 1098 |
testIssue := &serverTest{ |
| 1099 |
name: "IssueTicket", |
| 1100 |
command: []string{"openssl", "s_client", "-cipher", "AES128-SHA", "-ciphersuites", "TLS_AES_128_GCM_SHA256", "-sess_out", sessionFilePath}, |
| 1101 |
wait: true, |
| 1102 |
} |
| 1103 |
testResume := &serverTest{ |
| 1104 |
name: "Resume", |
| 1105 |
command: []string{"openssl", "s_client", "-cipher", "AES128-SHA", "-ciphersuites", "TLS_AES_128_GCM_SHA256", "-sess_in", sessionFilePath}, |
| 1106 |
validate: func(state ConnectionState) error { |
| 1107 |
if !state.DidResume { |
| 1108 |
return errors.New("did not resume") |
| 1109 |
} |
| 1110 |
return nil |
| 1111 |
}, |
| 1112 |
} |
| 1113 |
|
| 1114 |
runServerTestTLS12(t, testIssue) |
| 1115 |
runServerTestTLS12(t, testResume) |
| 1116 |
|
| 1117 |
runServerTestTLS13(t, testIssue) |
| 1118 |
runServerTestTLS13(t, testResume) |
| 1119 |
|
| 1120 |
config := testConfig.Clone() |
| 1121 |
config.CurvePreferences = []CurveID{CurveP256} |
| 1122 |
|
| 1123 |
testResumeHRR := &serverTest{ |
| 1124 |
name: "Resume-HelloRetryRequest", |
| 1125 |
command: []string{"openssl", "s_client", "-curves", "X25519:P-256", "-cipher", "AES128-SHA", "-ciphersuites", |
| 1126 |
"TLS_AES_128_GCM_SHA256", "-sess_in", sessionFilePath}, |
| 1127 |
config: config, |
| 1128 |
validate: func(state ConnectionState) error { |
| 1129 |
if !state.DidResume { |
| 1130 |
return errors.New("did not resume") |
| 1131 |
} |
| 1132 |
return nil |
| 1133 |
}, |
| 1134 |
} |
| 1135 |
|
| 1136 |
runServerTestTLS13(t, testResumeHRR) |
| 1137 |
} |
| 1138 |
|
| 1139 |
func TestServerResumptionDisabled(t *testing.T) { |
| 1140 |
sessionFilePath := tempFile("") |
| 1141 |
defer os.Remove(sessionFilePath) |
| 1142 |
|
| 1143 |
config := testConfig.Clone() |
| 1144 |
|
| 1145 |
testIssue := &serverTest{ |
| 1146 |
name: "IssueTicketPreDisable", |
| 1147 |
command: []string{"openssl", "s_client", "-cipher", "AES128-SHA", "-ciphersuites", "TLS_AES_128_GCM_SHA256", "-sess_out", sessionFilePath}, |
| 1148 |
config: config, |
| 1149 |
wait: true, |
| 1150 |
} |
| 1151 |
testResume := &serverTest{ |
| 1152 |
name: "ResumeDisabled", |
| 1153 |
command: []string{"openssl", "s_client", "-cipher", "AES128-SHA", "-ciphersuites", "TLS_AES_128_GCM_SHA256", "-sess_in", sessionFilePath}, |
| 1154 |
config: config, |
| 1155 |
validate: func(state ConnectionState) error { |
| 1156 |
if state.DidResume { |
| 1157 |
return errors.New("resumed with SessionTicketsDisabled") |
| 1158 |
} |
| 1159 |
return nil |
| 1160 |
}, |
| 1161 |
} |
| 1162 |
|
| 1163 |
config.SessionTicketsDisabled = false |
| 1164 |
runServerTestTLS12(t, testIssue) |
| 1165 |
config.SessionTicketsDisabled = true |
| 1166 |
runServerTestTLS12(t, testResume) |
| 1167 |
|
| 1168 |
config.SessionTicketsDisabled = false |
| 1169 |
runServerTestTLS13(t, testIssue) |
| 1170 |
config.SessionTicketsDisabled = true |
| 1171 |
runServerTestTLS13(t, testResume) |
| 1172 |
} |
| 1173 |
|
| 1174 |
func TestFallbackSCSV(t *testing.T) { |
| 1175 |
serverConfig := Config{ |
| 1176 |
Certificates: testConfig.Certificates, |
| 1177 |
} |
| 1178 |
test := &serverTest{ |
| 1179 |
name: "FallbackSCSV", |
| 1180 |
config: &serverConfig, |
| 1181 |
// OpenSSL 1.0.1j is needed for the -fallback_scsv option. |
| 1182 |
command: []string{"openssl", "s_client", "-fallback_scsv"}, |
| 1183 |
expectHandshakeErrorIncluding: "inappropriate protocol fallback", |
| 1184 |
} |
| 1185 |
runServerTestTLS11(t, test) |
| 1186 |
} |
| 1187 |
|
| 1188 |
func TestHandshakeServerExportKeyingMaterial(t *testing.T) { |
| 1189 |
test := &serverTest{ |
| 1190 |
name: "ExportKeyingMaterial", |
| 1191 |
command: []string{"openssl", "s_client", "-cipher", "ECDHE-RSA-AES256-SHA", "-ciphersuites", "TLS_CHACHA20_POLY1305_SHA256"}, |
| 1192 |
config: testConfig.Clone(), |
| 1193 |
validate: func(state ConnectionState) error { |
| 1194 |
if km, err := state.ExportKeyingMaterial("test", nil, 42); err != nil { |
| 1195 |
return fmt.Errorf("ExportKeyingMaterial failed: %v", err) |
| 1196 |
} else if len(km) != 42 { |
| 1197 |
return fmt.Errorf("Got %d bytes from ExportKeyingMaterial, wanted %d", len(km), 42) |
| 1198 |
} |
| 1199 |
return nil |
| 1200 |
}, |
| 1201 |
} |
| 1202 |
runServerTestTLS10(t, test) |
| 1203 |
runServerTestTLS12(t, test) |
| 1204 |
runServerTestTLS13(t, test) |
| 1205 |
} |
| 1206 |
|
| 1207 |
func TestHandshakeServerRSAPKCS1v15(t *testing.T) { |
| 1208 |
test := &serverTest{ |
| 1209 |
name: "RSA-RSAPKCS1v15", |
| 1210 |
command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "ECDHE-RSA-CHACHA20-POLY1305", "-sigalgs", "rsa_pkcs1_sha256"}, |
| 1211 |
} |
| 1212 |
runServerTestTLS12(t, test) |
| 1213 |
} |
| 1214 |
|
| 1215 |
func TestHandshakeServerRSAPSS(t *testing.T) { |
| 1216 |
// We send rsa_pss_rsae_sha512 first, as the test key won't fit, and we |
| 1217 |
// verify the server implementation will disregard the client preference in |
| 1218 |
// that case. See Issue 29793. |
| 1219 |
test := &serverTest{ |
| 1220 |
name: "RSA-RSAPSS", |
| 1221 |
command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "ECDHE-RSA-CHACHA20-POLY1305", "-ciphersuites", "TLS_CHACHA20_POLY1305_SHA256", "-sigalgs", "rsa_pss_rsae_sha512:rsa_pss_rsae_sha256"}, |
| 1222 |
} |
| 1223 |
runServerTestTLS12(t, test) |
| 1224 |
runServerTestTLS13(t, test) |
| 1225 |
|
| 1226 |
test = &serverTest{ |
| 1227 |
name: "RSA-RSAPSS-TooSmall", |
| 1228 |
command: []string{"openssl", "s_client", "-no_ticket", "-ciphersuites", "TLS_CHACHA20_POLY1305_SHA256", "-sigalgs", "rsa_pss_rsae_sha512"}, |
| 1229 |
expectHandshakeErrorIncluding: "peer doesn't support any of the certificate's signature algorithms", |
| 1230 |
} |
| 1231 |
runServerTestTLS13(t, test) |
| 1232 |
} |
| 1233 |
|
| 1234 |
func TestHandshakeServerEd25519(t *testing.T) { |
| 1235 |
config := testConfig.Clone() |
| 1236 |
config.Certificates = make([]Certificate, 1) |
| 1237 |
config.Certificates[0].Certificate = [][]byte{testEd25519Certificate} |
| 1238 |
config.Certificates[0].PrivateKey = testEd25519PrivateKey |
| 1239 |
config.BuildNameToCertificate() |
| 1240 |
|
| 1241 |
test := &serverTest{ |
| 1242 |
name: "Ed25519", |
| 1243 |
command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "ECDHE-ECDSA-CHACHA20-POLY1305", "-ciphersuites", "TLS_CHACHA20_POLY1305_SHA256"}, |
| 1244 |
config: config, |
| 1245 |
} |
| 1246 |
runServerTestTLS12(t, test) |
| 1247 |
runServerTestTLS13(t, test) |
| 1248 |
} |
| 1249 |
|
| 1250 |
func benchmarkHandshakeServer(b *testing.B, version uint16, cipherSuite uint16, curve CurveID, cert []byte, key crypto.PrivateKey) { |
| 1251 |
config := testConfig.Clone() |
| 1252 |
config.CipherSuites = []uint16{cipherSuite} |
| 1253 |
config.CurvePreferences = []CurveID{curve} |
| 1254 |
config.Certificates = make([]Certificate, 1) |
| 1255 |
config.Certificates[0].Certificate = [][]byte{cert} |
| 1256 |
config.Certificates[0].PrivateKey = key |
| 1257 |
config.BuildNameToCertificate() |
| 1258 |
|
| 1259 |
clientConn, serverConn := localPipe(b) |
| 1260 |
serverConn = &recordingConn{Conn: serverConn} |
| 1261 |
go func() { |
| 1262 |
config := testConfig.Clone() |
| 1263 |
config.MaxVersion = version |
| 1264 |
config.CurvePreferences = []CurveID{curve} |
| 1265 |
client := Client(clientConn, config) |
| 1266 |
client.Handshake() |
| 1267 |
}() |
| 1268 |
server := Server(serverConn, config) |
| 1269 |
if err := server.Handshake(); err != nil { |
| 1270 |
b.Fatalf("handshake failed: %v", err) |
| 1271 |
} |
| 1272 |
serverConn.Close() |
| 1273 |
flows := serverConn.(*recordingConn).flows |
| 1274 |
|
| 1275 |
feeder := make(chan struct{}) |
| 1276 |
clientConn, serverConn = localPipe(b) |
| 1277 |
|
| 1278 |
go func() { |
| 1279 |
for range feeder { |
| 1280 |
for i, f := range flows { |
| 1281 |
if i%2 == 0 { |
| 1282 |
clientConn.Write(f) |
| 1283 |
continue |
| 1284 |
} |
| 1285 |
ff := make([]byte, len(f)) |
| 1286 |
n, err := io.ReadFull(clientConn, ff) |
| 1287 |
if err != nil { |
| 1288 |
b.Errorf("#%d: %s\nRead %d, wanted %d, got %x, wanted %x\n", i+1, err, n, len(ff), ff[:n], f) |
| 1289 |
} |
| 1290 |
if !bytes.Equal(f, ff) { |
| 1291 |
b.Errorf("#%d: mismatch on read: got:%x want:%x", i+1, ff, f) |
| 1292 |
} |
| 1293 |
} |
| 1294 |
} |
| 1295 |
}() |
| 1296 |
|
| 1297 |
b.ResetTimer() |
| 1298 |
for i := 0; i < b.N; i++ { |
| 1299 |
feeder <- struct{}{} |
| 1300 |
server := Server(serverConn, config) |
| 1301 |
if err := server.Handshake(); err != nil { |
| 1302 |
b.Fatalf("handshake failed: %v", err) |
| 1303 |
} |
| 1304 |
} |
| 1305 |
close(feeder) |
| 1306 |
} |
| 1307 |
|
| 1308 |
func BenchmarkHandshakeServer(b *testing.B) { |
| 1309 |
b.Run("RSA", func(b *testing.B) { |
| 1310 |
benchmarkHandshakeServer(b, VersionTLS12, TLS_RSA_WITH_AES_128_GCM_SHA256, |
| 1311 |
0, testRSACertificate, testRSAPrivateKey) |
| 1312 |
}) |
| 1313 |
b.Run("ECDHE-P256-RSA", func(b *testing.B) { |
| 1314 |
b.Run("TLSv13", func(b *testing.B) { |
| 1315 |
benchmarkHandshakeServer(b, VersionTLS13, TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305, |
| 1316 |
CurveP256, testRSACertificate, testRSAPrivateKey) |
| 1317 |
}) |
| 1318 |
b.Run("TLSv12", func(b *testing.B) { |
| 1319 |
benchmarkHandshakeServer(b, VersionTLS12, TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305, |
| 1320 |
CurveP256, testRSACertificate, testRSAPrivateKey) |
| 1321 |
}) |
| 1322 |
}) |
| 1323 |
b.Run("ECDHE-P256-ECDSA-P256", func(b *testing.B) { |
| 1324 |
b.Run("TLSv13", func(b *testing.B) { |
| 1325 |
benchmarkHandshakeServer(b, VersionTLS13, TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305, |
| 1326 |
CurveP256, testP256Certificate, testP256PrivateKey) |
| 1327 |
}) |
| 1328 |
b.Run("TLSv12", func(b *testing.B) { |
| 1329 |
benchmarkHandshakeServer(b, VersionTLS12, TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305, |
| 1330 |
CurveP256, testP256Certificate, testP256PrivateKey) |
| 1331 |
}) |
| 1332 |
}) |
| 1333 |
b.Run("ECDHE-X25519-ECDSA-P256", func(b *testing.B) { |
| 1334 |
b.Run("TLSv13", func(b *testing.B) { |
| 1335 |
benchmarkHandshakeServer(b, VersionTLS13, TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305, |
| 1336 |
X25519, testP256Certificate, testP256PrivateKey) |
| 1337 |
}) |
| 1338 |
b.Run("TLSv12", func(b *testing.B) { |
| 1339 |
benchmarkHandshakeServer(b, VersionTLS12, TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305, |
| 1340 |
X25519, testP256Certificate, testP256PrivateKey) |
| 1341 |
}) |
| 1342 |
}) |
| 1343 |
b.Run("ECDHE-P521-ECDSA-P521", func(b *testing.B) { |
| 1344 |
if testECDSAPrivateKey.PublicKey.Curve != elliptic.P521() { |
| 1345 |
b.Fatal("test ECDSA key doesn't use curve P-521") |
| 1346 |
} |
| 1347 |
b.Run("TLSv13", func(b *testing.B) { |
| 1348 |
benchmarkHandshakeServer(b, VersionTLS13, TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305, |
| 1349 |
CurveP521, testECDSACertificate, testECDSAPrivateKey) |
| 1350 |
}) |
| 1351 |
b.Run("TLSv12", func(b *testing.B) { |
| 1352 |
benchmarkHandshakeServer(b, VersionTLS12, TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305, |
| 1353 |
CurveP521, testECDSACertificate, testECDSAPrivateKey) |
| 1354 |
}) |
| 1355 |
}) |
| 1356 |
} |
| 1357 |
|
| 1358 |
func TestClientAuth(t *testing.T) { |
| 1359 |
var certPath, keyPath, ecdsaCertPath, ecdsaKeyPath, ed25519CertPath, ed25519KeyPath string |
| 1360 |
|
| 1361 |
if *update { |
| 1362 |
certPath = tempFile(clientCertificatePEM) |
| 1363 |
defer os.Remove(certPath) |
| 1364 |
keyPath = tempFile(clientKeyPEM) |
| 1365 |
defer os.Remove(keyPath) |
| 1366 |
ecdsaCertPath = tempFile(clientECDSACertificatePEM) |
| 1367 |
defer os.Remove(ecdsaCertPath) |
| 1368 |
ecdsaKeyPath = tempFile(clientECDSAKeyPEM) |
| 1369 |
defer os.Remove(ecdsaKeyPath) |
| 1370 |
ed25519CertPath = tempFile(clientEd25519CertificatePEM) |
| 1371 |
defer os.Remove(ed25519CertPath) |
| 1372 |
ed25519KeyPath = tempFile(clientEd25519KeyPEM) |
| 1373 |
defer os.Remove(ed25519KeyPath) |
| 1374 |
} else { |
| 1375 |
t.Parallel() |
| 1376 |
} |
| 1377 |
|
| 1378 |
config := testConfig.Clone() |
| 1379 |
config.ClientAuth = RequestClientCert |
| 1380 |
|
| 1381 |
test := &serverTest{ |
| 1382 |
name: "ClientAuthRequestedNotGiven", |
| 1383 |
command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "AES128-SHA", "-ciphersuites", "TLS_AES_128_GCM_SHA256"}, |
| 1384 |
config: config, |
| 1385 |
} |
| 1386 |
runServerTestTLS12(t, test) |
| 1387 |
runServerTestTLS13(t, test) |
| 1388 |
|
| 1389 |
test = &serverTest{ |
| 1390 |
name: "ClientAuthRequestedAndGiven", |
| 1391 |
command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "AES128-SHA", "-ciphersuites", "TLS_AES_128_GCM_SHA256", |
| 1392 |
"-cert", certPath, "-key", keyPath, "-client_sigalgs", "rsa_pss_rsae_sha256"}, |
| 1393 |
config: config, |
| 1394 |
expectedPeerCerts: []string{clientCertificatePEM}, |
| 1395 |
} |
| 1396 |
runServerTestTLS12(t, test) |
| 1397 |
runServerTestTLS13(t, test) |
| 1398 |
|
| 1399 |
test = &serverTest{ |
| 1400 |
name: "ClientAuthRequestedAndECDSAGiven", |
| 1401 |
command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "AES128-SHA", "-ciphersuites", "TLS_AES_128_GCM_SHA256", |
| 1402 |
"-cert", ecdsaCertPath, "-key", ecdsaKeyPath}, |
| 1403 |
config: config, |
| 1404 |
expectedPeerCerts: []string{clientECDSACertificatePEM}, |
| 1405 |
} |
| 1406 |
runServerTestTLS12(t, test) |
| 1407 |
runServerTestTLS13(t, test) |
| 1408 |
|
| 1409 |
test = &serverTest{ |
| 1410 |
name: "ClientAuthRequestedAndEd25519Given", |
| 1411 |
command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "AES128-SHA", "-ciphersuites", "TLS_AES_128_GCM_SHA256", |
| 1412 |
"-cert", ed25519CertPath, "-key", ed25519KeyPath}, |
| 1413 |
config: config, |
| 1414 |
expectedPeerCerts: []string{clientEd25519CertificatePEM}, |
| 1415 |
} |
| 1416 |
runServerTestTLS12(t, test) |
| 1417 |
runServerTestTLS13(t, test) |
| 1418 |
|
| 1419 |
test = &serverTest{ |
| 1420 |
name: "ClientAuthRequestedAndPKCS1v15Given", |
| 1421 |
command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "AES128-SHA", |
| 1422 |
"-cert", certPath, "-key", keyPath, "-client_sigalgs", "rsa_pkcs1_sha256"}, |
| 1423 |
config: config, |
| 1424 |
expectedPeerCerts: []string{clientCertificatePEM}, |
| 1425 |
} |
| 1426 |
runServerTestTLS12(t, test) |
| 1427 |
} |
| 1428 |
|
| 1429 |
func TestSNIGivenOnFailure(t *testing.T) { |
| 1430 |
const expectedServerName = "test.testing" |
| 1431 |
|
| 1432 |
clientHello := &clientHelloMsg{ |
| 1433 |
vers: VersionTLS10, |
| 1434 |
random: make([]byte, 32), |
| 1435 |
cipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA}, |
| 1436 |
compressionMethods: []uint8{compressionNone}, |
| 1437 |
serverName: expectedServerName, |
| 1438 |
} |
| 1439 |
|
| 1440 |
serverConfig := testConfig.Clone() |
| 1441 |
// Erase the server's cipher suites to ensure the handshake fails. |
| 1442 |
serverConfig.CipherSuites = nil |
| 1443 |
|
| 1444 |
c, s := localPipe(t) |
| 1445 |
go func() { |
| 1446 |
cli := Client(c, testConfig) |
| 1447 |
cli.vers = clientHello.vers |
| 1448 |
cli.writeRecord(recordTypeHandshake, clientHello.marshal()) |
| 1449 |
c.Close() |
| 1450 |
}() |
| 1451 |
conn := Server(s, serverConfig) |
| 1452 |
ctx := context.Background() |
| 1453 |
ch, err := conn.readClientHello(ctx) |
| 1454 |
hs := serverHandshakeState{ |
| 1455 |
c: conn, |
| 1456 |
ctx: ctx, |
| 1457 |
clientHello: ch, |
| 1458 |
} |
| 1459 |
if err == nil { |
| 1460 |
err = hs.processClientHello() |
| 1461 |
} |
| 1462 |
if err == nil { |
| 1463 |
err = hs.pickCipherSuite() |
| 1464 |
} |
| 1465 |
defer s.Close() |
| 1466 |
|
| 1467 |
if err == nil { |
| 1468 |
t.Error("No error reported from server") |
| 1469 |
} |
| 1470 |
|
| 1471 |
cs := hs.c.ConnectionState() |
| 1472 |
if cs.HandshakeComplete { |
| 1473 |
t.Error("Handshake registered as complete") |
| 1474 |
} |
| 1475 |
|
| 1476 |
if cs.ServerName != expectedServerName { |
| 1477 |
t.Errorf("Expected ServerName of %q, but got %q", expectedServerName, cs.ServerName) |
| 1478 |
} |
| 1479 |
} |
| 1480 |
|
| 1481 |
var getConfigForClientTests = []struct { |
| 1482 |
setup func(config *Config) |
| 1483 |
callback func(clientHello *ClientHelloInfo) (*Config, error) |
| 1484 |
errorSubstring string |
| 1485 |
verify func(config *Config) error |
| 1486 |
}{ |
| 1487 |
{ |
| 1488 |
nil, |
| 1489 |
func(clientHello *ClientHelloInfo) (*Config, error) { |
| 1490 |
return nil, nil |
| 1491 |
}, |
| 1492 |
"", |
| 1493 |
nil, |
| 1494 |
}, |
| 1495 |
{ |
| 1496 |
nil, |
| 1497 |
func(clientHello *ClientHelloInfo) (*Config, error) { |
| 1498 |
return nil, errors.New("should bubble up") |
| 1499 |
}, |
| 1500 |
"should bubble up", |
| 1501 |
nil, |
| 1502 |
}, |
| 1503 |
{ |
| 1504 |
nil, |
| 1505 |
func(clientHello *ClientHelloInfo) (*Config, error) { |
| 1506 |
config := testConfig.Clone() |
| 1507 |
// Setting a maximum version of TLS 1.1 should cause |
| 1508 |
// the handshake to fail, as the client MinVersion is TLS 1.2. |
| 1509 |
config.MaxVersion = VersionTLS11 |
| 1510 |
return config, nil |
| 1511 |
}, |
| 1512 |
"client offered only unsupported versions", |
| 1513 |
nil, |
| 1514 |
}, |
| 1515 |
{ |
| 1516 |
func(config *Config) { |
| 1517 |
for i := range config.SessionTicketKey { |
| 1518 |
config.SessionTicketKey[i] = byte(i) |
| 1519 |
} |
| 1520 |
config.sessionTicketKeys = nil |
| 1521 |
}, |
| 1522 |
func(clientHello *ClientHelloInfo) (*Config, error) { |
| 1523 |
config := testConfig.Clone() |
| 1524 |
for i := range config.SessionTicketKey { |
| 1525 |
config.SessionTicketKey[i] = 0 |
| 1526 |
} |
| 1527 |
config.sessionTicketKeys = nil |
| 1528 |
return config, nil |
| 1529 |
}, |
| 1530 |
"", |
| 1531 |
func(config *Config) error { |
| 1532 |
if config.SessionTicketKey == [32]byte{} { |
| 1533 |
return fmt.Errorf("expected SessionTicketKey to be set") |
| 1534 |
} |
| 1535 |
return nil |
| 1536 |
}, |
| 1537 |
}, |
| 1538 |
{ |
| 1539 |
func(config *Config) { |
| 1540 |
var dummyKey [32]byte |
| 1541 |
for i := range dummyKey { |
| 1542 |
dummyKey[i] = byte(i) |
| 1543 |
} |
| 1544 |
|
| 1545 |
config.SetSessionTicketKeys([][32]byte{dummyKey}) |
| 1546 |
}, |
| 1547 |
func(clientHello *ClientHelloInfo) (*Config, error) { |
| 1548 |
config := testConfig.Clone() |
| 1549 |
config.sessionTicketKeys = nil |
| 1550 |
return config, nil |
| 1551 |
}, |
| 1552 |
"", |
| 1553 |
func(config *Config) error { |
| 1554 |
if config.SessionTicketKey == [32]byte{} { |
| 1555 |
return fmt.Errorf("expected SessionTicketKey to be set") |
| 1556 |
} |
| 1557 |
return nil |
| 1558 |
}, |
| 1559 |
}, |
| 1560 |
} |
| 1561 |
|
| 1562 |
func TestGetConfigForClient(t *testing.T) { |
| 1563 |
serverConfig := testConfig.Clone() |
| 1564 |
clientConfig := testConfig.Clone() |
| 1565 |
clientConfig.MinVersion = VersionTLS12 |
| 1566 |
|
| 1567 |
for i, test := range getConfigForClientTests { |
| 1568 |
if test.setup != nil { |
| 1569 |
test.setup(serverConfig) |
| 1570 |
} |
| 1571 |
|
| 1572 |
var configReturned *Config |
| 1573 |
serverConfig.GetConfigForClient = func(clientHello *ClientHelloInfo) (*Config, error) { |
| 1574 |
config, err := test.callback(clientHello) |
| 1575 |
configReturned = config |
| 1576 |
return config, err |
| 1577 |
} |
| 1578 |
c, s := localPipe(t) |
| 1579 |
done := make(chan error) |
| 1580 |
|
| 1581 |
go func() { |
| 1582 |
defer s.Close() |
| 1583 |
done <- Server(s, serverConfig).Handshake() |
| 1584 |
}() |
| 1585 |
|
| 1586 |
clientErr := Client(c, clientConfig).Handshake() |
| 1587 |
c.Close() |
| 1588 |
|
| 1589 |
serverErr := <-done |
| 1590 |
|
| 1591 |
if len(test.errorSubstring) == 0 { |
| 1592 |
if serverErr != nil || clientErr != nil { |
| 1593 |
t.Errorf("test[%d]: expected no error but got serverErr: %q, clientErr: %q", i, serverErr, clientErr) |
| 1594 |
} |
| 1595 |
if test.verify != nil { |
| 1596 |
if err := test.verify(configReturned); err != nil { |
| 1597 |
t.Errorf("test[%d]: verify returned error: %v", i, err) |
| 1598 |
} |
| 1599 |
} |
| 1600 |
} else { |
| 1601 |
if serverErr == nil { |
| 1602 |
t.Errorf("test[%d]: expected error containing %q but got no error", i, test.errorSubstring) |
| 1603 |
} else if !strings.Contains(serverErr.Error(), test.errorSubstring) { |
| 1604 |
t.Errorf("test[%d]: expected error to contain %q but it was %q", i, test.errorSubstring, serverErr) |
| 1605 |
} |
| 1606 |
} |
| 1607 |
} |
| 1608 |
} |
| 1609 |
|
| 1610 |
func TestCloseServerConnectionOnIdleClient(t *testing.T) { |
| 1611 |
clientConn, serverConn := localPipe(t) |
| 1612 |
server := Server(serverConn, testConfig.Clone()) |
| 1613 |
go func() { |
| 1614 |
clientConn.Write([]byte{'0'}) |
| 1615 |
server.Close() |
| 1616 |
}() |
| 1617 |
server.SetReadDeadline(time.Now().Add(time.Minute)) |
| 1618 |
err := server.Handshake() |
| 1619 |
if err != nil { |
| 1620 |
if err, ok := err.(net.Error); ok && err.Timeout() { |
| 1621 |
t.Errorf("Expected a closed network connection error but got '%s'", err.Error()) |
| 1622 |
} |
| 1623 |
} else { |
| 1624 |
t.Errorf("Error expected, but no error returned") |
| 1625 |
} |
| 1626 |
} |
| 1627 |
|
| 1628 |
func TestCloneHash(t *testing.T) { |
| 1629 |
h1 := crypto.SHA256.New() |
| 1630 |
h1.Write([]byte("test")) |
| 1631 |
s1 := h1.Sum(nil) |
| 1632 |
h2 := cloneHash(h1, crypto.SHA256) |
| 1633 |
s2 := h2.Sum(nil) |
| 1634 |
if !bytes.Equal(s1, s2) { |
| 1635 |
t.Error("cloned hash generated a different sum") |
| 1636 |
} |
| 1637 |
} |
| 1638 |
|
| 1639 |
func expectError(t *testing.T, err error, sub string) { |
| 1640 |
if err == nil { |
| 1641 |
t.Errorf(`expected error %q, got nil`, sub) |
| 1642 |
} else if !strings.Contains(err.Error(), sub) { |
| 1643 |
t.Errorf(`expected error %q, got %q`, sub, err) |
| 1644 |
} |
| 1645 |
} |
| 1646 |
|
| 1647 |
func TestKeyTooSmallForRSAPSS(t *testing.T) { |
| 1648 |
cert, err := X509KeyPair([]byte(`-----BEGIN CERTIFICATE----- |
| 1649 |
MIIBcTCCARugAwIBAgIQGjQnkCFlUqaFlt6ixyz/tDANBgkqhkiG9w0BAQsFADAS |
| 1650 |
MRAwDgYDVQQKEwdBY21lIENvMB4XDTE5MDExODIzMjMyOFoXDTIwMDExODIzMjMy |
| 1651 |
OFowEjEQMA4GA1UEChMHQWNtZSBDbzBcMA0GCSqGSIb3DQEBAQUAA0sAMEgCQQDd |
| 1652 |
ez1rFUDwax2HTxbcnFUP9AhcgEGMHVV2nn4VVEWFJB6I8C/Nkx0XyyQlrmFYBzEQ |
| 1653 |
nIPhKls4T0hFoLvjJnXpAgMBAAGjTTBLMA4GA1UdDwEB/wQEAwIFoDATBgNVHSUE |
| 1654 |
DDAKBggrBgEFBQcDATAMBgNVHRMBAf8EAjAAMBYGA1UdEQQPMA2CC2V4YW1wbGUu |
| 1655 |
Y29tMA0GCSqGSIb3DQEBCwUAA0EAxDuUS+BrrS3c+h+k+fQPOmOScy6yTX9mHw0Q |
| 1656 |
KbucGamXYEy0URIwOdO0tQ3LHPc1YGvYSPwkDjkjqECs2Vm/AA== |
| 1657 |
-----END CERTIFICATE-----`), []byte(testingKey(`-----BEGIN RSA TESTING KEY----- |
| 1658 |
MIIBOgIBAAJBAN17PWsVQPBrHYdPFtycVQ/0CFyAQYwdVXaefhVURYUkHojwL82T |
| 1659 |
HRfLJCWuYVgHMRCcg+EqWzhPSEWgu+MmdekCAwEAAQJBALjQYNTdXF4CFBbXwUz/ |
| 1660 |
yt9QFDYT9B5WT/12jeGAe653gtYS6OOi/+eAkGmzg1GlRnw6fOfn+HYNFDORST7z |
| 1661 |
4j0CIQDn2xz9hVWQEu9ee3vecNT3f60huDGTNoRhtqgweQGX0wIhAPSLj1VcRZEz |
| 1662 |
nKpbtU22+PbIMSJ+e80fmY9LIPx5N4HTAiAthGSimMR9bloz0EY3GyuUEyqoDgMd |
| 1663 |
hXxjuno2WesoJQIgemilbcALXpxsLmZLgcQ2KSmaVr7jb5ECx9R+hYKTw1sCIG4s |
| 1664 |
T+E0J8wlH24pgwQHzy7Ko2qLwn1b5PW8ecrlvP1g |
| 1665 |
-----END RSA TESTING KEY-----`))) |
| 1666 |
if err != nil { |
| 1667 |
t.Fatal(err) |
| 1668 |
} |
| 1669 |
|
| 1670 |
clientConn, serverConn := localPipe(t) |
| 1671 |
client := Client(clientConn, testConfig) |
| 1672 |
done := make(chan struct{}) |
| 1673 |
go func() { |
| 1674 |
config := testConfig.Clone() |
| 1675 |
config.Certificates = []Certificate{cert} |
| 1676 |
config.MinVersion = VersionTLS13 |
| 1677 |
server := Server(serverConn, config) |
| 1678 |
err := server.Handshake() |
| 1679 |
expectError(t, err, "key size too small") |
| 1680 |
close(done) |
| 1681 |
}() |
| 1682 |
err = client.Handshake() |
| 1683 |
expectError(t, err, "handshake failure") |
| 1684 |
<-done |
| 1685 |
} |
| 1686 |
|
| 1687 |
func TestMultipleCertificates(t *testing.T) { |
| 1688 |
clientConfig := testConfig.Clone() |
| 1689 |
clientConfig.CipherSuites = []uint16{TLS_RSA_WITH_AES_128_GCM_SHA256} |
| 1690 |
clientConfig.MaxVersion = VersionTLS12 |
| 1691 |
|
| 1692 |
serverConfig := testConfig.Clone() |
| 1693 |
serverConfig.Certificates = []Certificate{{ |
| 1694 |
Certificate: [][]byte{testECDSACertificate}, |
| 1695 |
PrivateKey: testECDSAPrivateKey, |
| 1696 |
}, { |
| 1697 |
Certificate: [][]byte{testRSACertificate}, |
| 1698 |
PrivateKey: testRSAPrivateKey, |
| 1699 |
}} |
| 1700 |
|
| 1701 |
_, clientState, err := testHandshake(t, clientConfig, serverConfig) |
| 1702 |
if err != nil { |
| 1703 |
t.Fatal(err) |
| 1704 |
} |
| 1705 |
if got := clientState.PeerCertificates[0].PublicKeyAlgorithm; got != x509.RSA { |
| 1706 |
t.Errorf("expected RSA certificate, got %v", got) |
| 1707 |
} |
| 1708 |
} |
| 1709 |
|
| 1710 |
func TestAESCipherReordering(t *testing.T) { |
| 1711 |
currentAESSupport := hasAESGCMHardwareSupport |
| 1712 |
defer func() { hasAESGCMHardwareSupport = currentAESSupport }() |
| 1713 |
|
| 1714 |
tests := []struct { |
| 1715 |
name string |
| 1716 |
clientCiphers []uint16 |
| 1717 |
serverHasAESGCM bool |
| 1718 |
serverCiphers []uint16 |
| 1719 |
expectedCipher uint16 |
| 1720 |
}{ |
| 1721 |
{ |
| 1722 |
name: "server has hardware AES, client doesn't (pick ChaCha)", |
| 1723 |
clientCiphers: []uint16{ |
| 1724 |
TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305, |
| 1725 |
TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, |
| 1726 |
TLS_RSA_WITH_AES_128_CBC_SHA, |
| 1727 |
}, |
| 1728 |
serverHasAESGCM: true, |
| 1729 |
expectedCipher: TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305, |
| 1730 |
}, |
| 1731 |
{ |
| 1732 |
name: "client prefers AES-GCM, server doesn't have hardware AES (pick ChaCha)", |
| 1733 |
clientCiphers: []uint16{ |
| 1734 |
TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, |
| 1735 |
TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305, |
| 1736 |
TLS_RSA_WITH_AES_128_CBC_SHA, |
| 1737 |
}, |
| 1738 |
serverHasAESGCM: false, |
| 1739 |
expectedCipher: TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305, |
| 1740 |
}, |
| 1741 |
{ |
| 1742 |
name: "client prefers AES-GCM, server has hardware AES (pick AES-GCM)", |
| 1743 |
clientCiphers: []uint16{ |
| 1744 |
TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, |
| 1745 |
TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305, |
| 1746 |
TLS_RSA_WITH_AES_128_CBC_SHA, |
| 1747 |
}, |
| 1748 |
serverHasAESGCM: true, |
| 1749 |
expectedCipher: TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, |
| 1750 |
}, |
| 1751 |
{ |
| 1752 |
name: "client prefers AES-GCM and sends GREASE, server has hardware AES (pick AES-GCM)", |
| 1753 |
clientCiphers: []uint16{ |
| 1754 |
0x0A0A, // GREASE value |
| 1755 |
TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, |
| 1756 |
TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305, |
| 1757 |
TLS_RSA_WITH_AES_128_CBC_SHA, |
| 1758 |
}, |
| 1759 |
serverHasAESGCM: true, |
| 1760 |
expectedCipher: TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, |
| 1761 |
}, |
| 1762 |
{ |
| 1763 |
name: "client prefers AES-GCM and doesn't support ChaCha, server doesn't have hardware AES (pick AES-GCM)", |
| 1764 |
clientCiphers: []uint16{ |
| 1765 |
TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, |
| 1766 |
TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, |
| 1767 |
TLS_RSA_WITH_AES_128_CBC_SHA, |
| 1768 |
}, |
| 1769 |
serverHasAESGCM: false, |
| 1770 |
expectedCipher: TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, |
| 1771 |
}, |
| 1772 |
{ |
| 1773 |
name: "client prefers AES-GCM and AES-CBC over ChaCha, server doesn't have hardware AES (pick ChaCha)", |
| 1774 |
clientCiphers: []uint16{ |
| 1775 |
TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, |
| 1776 |
TLS_RSA_WITH_AES_128_CBC_SHA, |
| 1777 |
TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305, |
| 1778 |
}, |
| 1779 |
serverHasAESGCM: false, |
| 1780 |
expectedCipher: TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305, |
| 1781 |
}, |
| 1782 |
{ |
| 1783 |
name: "client prefers AES-GCM over ChaCha and sends GREASE, server doesn't have hardware AES (pick ChaCha)", |
| 1784 |
clientCiphers: []uint16{ |
| 1785 |
0x0A0A, // GREASE value |
| 1786 |
TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, |
| 1787 |
TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305, |
| 1788 |
TLS_RSA_WITH_AES_128_CBC_SHA, |
| 1789 |
}, |
| 1790 |
serverHasAESGCM: false, |
| 1791 |
expectedCipher: TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305, |
| 1792 |
}, |
| 1793 |
{ |
| 1794 |
name: "client supports multiple AES-GCM, server doesn't have hardware AES and doesn't support ChaCha (AES-GCM)", |
| 1795 |
clientCiphers: []uint16{ |
| 1796 |
TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, |
| 1797 |
TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305, |
| 1798 |
TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, |
| 1799 |
}, |
| 1800 |
serverHasAESGCM: false, |
| 1801 |
serverCiphers: []uint16{ |
| 1802 |
TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, |
| 1803 |
TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, |
| 1804 |
}, |
| 1805 |
expectedCipher: TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, |
| 1806 |
}, |
| 1807 |
{ |
| 1808 |
name: "client prefers AES-GCM, server has hardware but doesn't support AES (pick ChaCha)", |
| 1809 |
clientCiphers: []uint16{ |
| 1810 |
TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, |
| 1811 |
TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305, |
| 1812 |
TLS_RSA_WITH_AES_128_CBC_SHA, |
| 1813 |
}, |
| 1814 |
serverHasAESGCM: true, |
| 1815 |
serverCiphers: []uint16{ |
| 1816 |
TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305, |
| 1817 |
}, |
| 1818 |
expectedCipher: TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305, |
| 1819 |
}, |
| 1820 |
} |
| 1821 |
|
| 1822 |
for _, tc := range tests { |
| 1823 |
t.Run(tc.name, func(t *testing.T) { |
| 1824 |
hasAESGCMHardwareSupport = tc.serverHasAESGCM |
| 1825 |
hs := &serverHandshakeState{ |
| 1826 |
c: &Conn{ |
| 1827 |
config: &Config{ |
| 1828 |
CipherSuites: tc.serverCiphers, |
| 1829 |
}, |
| 1830 |
vers: VersionTLS12, |
| 1831 |
}, |
| 1832 |
clientHello: &clientHelloMsg{ |
| 1833 |
cipherSuites: tc.clientCiphers, |
| 1834 |
vers: VersionTLS12, |
| 1835 |
}, |
| 1836 |
ecdheOk: true, |
| 1837 |
rsaSignOk: true, |
| 1838 |
rsaDecryptOk: true, |
| 1839 |
} |
| 1840 |
|
| 1841 |
err := hs.pickCipherSuite() |
| 1842 |
if err != nil { |
| 1843 |
t.Errorf("pickCipherSuite failed: %s", err) |
| 1844 |
} |
| 1845 |
|
| 1846 |
if tc.expectedCipher != hs.suite.id { |
| 1847 |
t.Errorf("unexpected cipher chosen: want %d, got %d", tc.expectedCipher, hs.suite.id) |
| 1848 |
} |
| 1849 |
}) |
| 1850 |
} |
| 1851 |
} |
| 1852 |
|
| 1853 |
func TestAESCipherReorderingTLS13(t *testing.T) { |
| 1854 |
currentAESSupport := hasAESGCMHardwareSupport |
| 1855 |
defer func() { hasAESGCMHardwareSupport = currentAESSupport }() |
| 1856 |
|
| 1857 |
tests := []struct { |
| 1858 |
name string |
| 1859 |
clientCiphers []uint16 |
| 1860 |
serverHasAESGCM bool |
| 1861 |
expectedCipher uint16 |
| 1862 |
}{ |
| 1863 |
{ |
| 1864 |
name: "server has hardware AES, client doesn't (pick ChaCha)", |
| 1865 |
clientCiphers: []uint16{ |
| 1866 |
TLS_CHACHA20_POLY1305_SHA256, |
| 1867 |
TLS_AES_128_GCM_SHA256, |
| 1868 |
}, |
| 1869 |
serverHasAESGCM: true, |
| 1870 |
expectedCipher: TLS_CHACHA20_POLY1305_SHA256, |
| 1871 |
}, |
| 1872 |
{ |
| 1873 |
name: "neither server nor client have hardware AES (pick ChaCha)", |
| 1874 |
clientCiphers: []uint16{ |
| 1875 |
TLS_CHACHA20_POLY1305_SHA256, |
| 1876 |
TLS_AES_128_GCM_SHA256, |
| 1877 |
}, |
| 1878 |
serverHasAESGCM: false, |
| 1879 |
expectedCipher: TLS_CHACHA20_POLY1305_SHA256, |
| 1880 |
}, |
| 1881 |
{ |
| 1882 |
name: "client prefers AES, server doesn't have hardware (pick ChaCha)", |
| 1883 |
clientCiphers: []uint16{ |
| 1884 |
TLS_AES_128_GCM_SHA256, |
| 1885 |
TLS_CHACHA20_POLY1305_SHA256, |
| 1886 |
}, |
| 1887 |
serverHasAESGCM: false, |
| 1888 |
expectedCipher: TLS_CHACHA20_POLY1305_SHA256, |
| 1889 |
}, |
| 1890 |
{ |
| 1891 |
name: "client prefers AES and sends GREASE, server doesn't have hardware (pick ChaCha)", |
| 1892 |
clientCiphers: []uint16{ |
| 1893 |
0x0A0A, // GREASE value |
| 1894 |
TLS_AES_128_GCM_SHA256, |
| 1895 |
TLS_CHACHA20_POLY1305_SHA256, |
| 1896 |
}, |
| 1897 |
serverHasAESGCM: false, |
| 1898 |
expectedCipher: TLS_CHACHA20_POLY1305_SHA256, |
| 1899 |
}, |
| 1900 |
{ |
| 1901 |
name: "client prefers AES, server has hardware AES (pick AES)", |
| 1902 |
clientCiphers: []uint16{ |
| 1903 |
TLS_AES_128_GCM_SHA256, |
| 1904 |
TLS_CHACHA20_POLY1305_SHA256, |
| 1905 |
}, |
| 1906 |
serverHasAESGCM: true, |
| 1907 |
expectedCipher: TLS_AES_128_GCM_SHA256, |
| 1908 |
}, |
| 1909 |
{ |
| 1910 |
name: "client prefers AES and sends GREASE, server has hardware AES (pick AES)", |
| 1911 |
clientCiphers: []uint16{ |
| 1912 |
0x0A0A, // GREASE value |
| 1913 |
TLS_AES_128_GCM_SHA256, |
| 1914 |
TLS_CHACHA20_POLY1305_SHA256, |
| 1915 |
}, |
| 1916 |
serverHasAESGCM: true, |
| 1917 |
expectedCipher: TLS_AES_128_GCM_SHA256, |
| 1918 |
}, |
| 1919 |
} |
| 1920 |
|
| 1921 |
for _, tc := range tests { |
| 1922 |
t.Run(tc.name, func(t *testing.T) { |
| 1923 |
hasAESGCMHardwareSupport = tc.serverHasAESGCM |
| 1924 |
hs := &serverHandshakeStateTLS13{ |
| 1925 |
c: &Conn{ |
| 1926 |
config: &Config{}, |
| 1927 |
vers: VersionTLS13, |
| 1928 |
}, |
| 1929 |
clientHello: &clientHelloMsg{ |
| 1930 |
cipherSuites: tc.clientCiphers, |
| 1931 |
supportedVersions: []uint16{VersionTLS13}, |
| 1932 |
compressionMethods: []uint8{compressionNone}, |
| 1933 |
keyShares: []keyShare{{group: X25519, data: curve25519.Basepoint}}, |
| 1934 |
}, |
| 1935 |
} |
| 1936 |
|
| 1937 |
err := hs.processClientHello() |
| 1938 |
if err != nil { |
| 1939 |
t.Errorf("pickCipherSuite failed: %s", err) |
| 1940 |
} |
| 1941 |
|
| 1942 |
if tc.expectedCipher != hs.suite.id { |
| 1943 |
t.Errorf("unexpected cipher chosen: want %d, got %d", tc.expectedCipher, hs.suite.id) |
| 1944 |
} |
| 1945 |
}) |
| 1946 |
} |
| 1947 |
} |
| 1948 |
|
| 1949 |
// TestServerHandshakeContextCancellation tests that cancelling |
| 1950 |
// the context given to the server side conn.HandshakeContext |
| 1951 |
// interrupts the in-progress handshake. |
| 1952 |
func TestServerHandshakeContextCancellation(t *testing.T) { |
| 1953 |
c, s := localPipe(t) |
| 1954 |
ctx, cancel := context.WithCancel(context.Background()) |
| 1955 |
unblockClient := make(chan struct{}) |
| 1956 |
defer close(unblockClient) |
| 1957 |
go func() { |
| 1958 |
cancel() |
| 1959 |
<-unblockClient |
| 1960 |
_ = c.Close() |
| 1961 |
}() |
| 1962 |
conn := Server(s, testConfig) |
| 1963 |
// Initiates server side handshake, which will block until a client hello is read |
| 1964 |
// unless the cancellation works. |
| 1965 |
err := conn.HandshakeContext(ctx) |
| 1966 |
if err == nil { |
| 1967 |
t.Fatal("Server handshake did not error when the context was canceled") |
| 1968 |
} |
| 1969 |
if err != context.Canceled { |
| 1970 |
t.Errorf("Unexpected server handshake error: %v", err) |
| 1971 |
} |
| 1972 |
if runtime.GOARCH == "wasm" { |
| 1973 |
t.Skip("conn.Close does not error as expected when called multiple times on WASM") |
| 1974 |
} |
| 1975 |
err = conn.Close() |
| 1976 |
if err == nil { |
| 1977 |
t.Error("Server connection was not closed when the context was canceled") |
| 1978 |
} |
| 1979 |
} |
| 1980 |
|
| 1981 |
// TestHandshakeContextHierarchy tests whether the contexts |
| 1982 |
// available to GetClientCertificate and GetCertificate are |
| 1983 |
// derived from the context provided to HandshakeContext, and |
| 1984 |
// that those contexts are canceled after HandshakeContext has |
| 1985 |
// returned. |
| 1986 |
func TestHandshakeContextHierarchy(t *testing.T) { |
| 1987 |
c, s := localPipe(t) |
| 1988 |
clientErr := make(chan error, 1) |
| 1989 |
clientConfig := testConfig.Clone() |
| 1990 |
serverConfig := testConfig.Clone() |
| 1991 |
ctx, cancel := context.WithCancel(context.Background()) |
| 1992 |
defer cancel() |
| 1993 |
key := struct{}{} |
| 1994 |
ctx = context.WithValue(ctx, key, true) |
| 1995 |
go func() { |
| 1996 |
defer close(clientErr) |
| 1997 |
defer c.Close() |
| 1998 |
var innerCtx context.Context |
| 1999 |
clientConfig.Certificates = nil |
| 2000 |
clientConfig.GetClientCertificate = func(certificateRequest *CertificateRequestInfo) (*Certificate, error) { |
| 2001 |
if val, ok := certificateRequest.Context().Value(key).(bool); !ok || !val { |
| 2002 |
t.Errorf("GetClientCertificate context was not child of HandshakeContext") |
| 2003 |
} |
| 2004 |
innerCtx = certificateRequest.Context() |
| 2005 |
return &Certificate{ |
| 2006 |
Certificate: [][]byte{testRSACertificate}, |
| 2007 |
PrivateKey: testRSAPrivateKey, |
| 2008 |
}, nil |
| 2009 |
} |
| 2010 |
cli := Client(c, clientConfig) |
| 2011 |
err := cli.HandshakeContext(ctx) |
| 2012 |
if err != nil { |
| 2013 |
clientErr <- err |
| 2014 |
return |
| 2015 |
} |
| 2016 |
select { |
| 2017 |
case <-innerCtx.Done(): |
| 2018 |
default: |
| 2019 |
t.Errorf("GetClientCertificate context was not canceled after HandshakeContext returned.") |
| 2020 |
} |
| 2021 |
}() |
| 2022 |
var innerCtx context.Context |
| 2023 |
serverConfig.Certificates = nil |
| 2024 |
serverConfig.ClientAuth = RequestClientCert |
| 2025 |
serverConfig.GetCertificate = func(clientHello *ClientHelloInfo) (*Certificate, error) { |
| 2026 |
if val, ok := clientHello.Context().Value(key).(bool); !ok || !val { |
| 2027 |
t.Errorf("GetClientCertificate context was not child of HandshakeContext") |
| 2028 |
} |
| 2029 |
innerCtx = clientHello.Context() |
| 2030 |
return &Certificate{ |
| 2031 |
Certificate: [][]byte{testRSACertificate}, |
| 2032 |
PrivateKey: testRSAPrivateKey, |
| 2033 |
}, nil |
| 2034 |
} |
| 2035 |
conn := Server(s, serverConfig) |
| 2036 |
err := conn.HandshakeContext(ctx) |
| 2037 |
if err != nil { |
| 2038 |
t.Errorf("Unexpected server handshake error: %v", err) |
| 2039 |
} |
| 2040 |
select { |
| 2041 |
case <-innerCtx.Done(): |
| 2042 |
default: |
| 2043 |
t.Errorf("GetCertificate context was not canceled after HandshakeContext returned.") |
| 2044 |
} |
| 2045 |
if err := <-clientErr; err != nil { |
| 2046 |
t.Errorf("Unexpected client error: %v", err) |
| 2047 |
} |
| 2048 |
} |