2009-11-05 23:44:32 +00:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package tls
|
|
|
|
|
|
|
|
import (
|
2010-12-15 16:49:55 +00:00
|
|
|
"bytes"
|
2013-07-17 17:33:16 +01:00
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/elliptic"
|
2009-12-15 23:33:31 +00:00
|
|
|
"crypto/rsa"
|
2010-04-27 06:19:04 +01:00
|
|
|
"encoding/hex"
|
2012-01-05 17:05:38 +00:00
|
|
|
"encoding/pem"
|
2013-12-20 16:37:05 +00:00
|
|
|
"errors"
|
2012-04-11 17:55:57 +01:00
|
|
|
"fmt"
|
2010-04-27 06:19:04 +01:00
|
|
|
"io"
|
2011-11-08 23:40:58 +00:00
|
|
|
"math/big"
|
2010-04-27 06:19:04 +01:00
|
|
|
"net"
|
2012-04-11 17:55:57 +01:00
|
|
|
"os"
|
2013-12-20 16:37:05 +00:00
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2014-02-12 16:20:01 +00:00
|
|
|
"strings"
|
2009-12-15 23:33:31 +00:00
|
|
|
"testing"
|
2011-11-30 17:01:46 +00:00
|
|
|
"time"
|
2009-11-05 23:44:32 +00:00
|
|
|
)
|
|
|
|
|
2013-12-20 16:37:05 +00:00
|
|
|
// zeroSource is an io.Reader that returns an unlimited number of zero bytes.
|
2009-11-05 23:44:32 +00:00
|
|
|
type zeroSource struct{}
|
|
|
|
|
2011-11-02 02:04:37 +00:00
|
|
|
func (zeroSource) Read(b []byte) (n int, err error) {
|
2009-11-05 23:44:32 +00:00
|
|
|
for i := range b {
|
2009-11-09 20:07:39 +00:00
|
|
|
b[i] = 0
|
2009-11-05 23:44:32 +00:00
|
|
|
}
|
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
return len(b), nil
|
2009-11-05 23:44:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var testConfig *Config
|
|
|
|
|
2015-03-17 00:13:10 +00:00
|
|
|
func allCipherSuites() []uint16 {
|
|
|
|
ids := make([]uint16, len(cipherSuites))
|
|
|
|
for i, suite := range cipherSuites {
|
|
|
|
ids[i] = suite.id
|
|
|
|
}
|
|
|
|
|
|
|
|
return ids
|
|
|
|
}
|
|
|
|
|
2009-11-05 23:44:32 +00:00
|
|
|
func init() {
|
2013-12-20 16:37:05 +00:00
|
|
|
testConfig = &Config{
|
|
|
|
Time: func() time.Time { return time.Unix(0, 0) },
|
|
|
|
Rand: zeroSource{},
|
|
|
|
Certificates: make([]Certificate, 2),
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
MinVersion: VersionSSL30,
|
|
|
|
MaxVersion: VersionTLS12,
|
2015-03-17 00:13:10 +00:00
|
|
|
CipherSuites: allCipherSuites(),
|
2013-12-20 16:37:05 +00:00
|
|
|
}
|
2013-07-17 17:33:16 +01:00
|
|
|
testConfig.Certificates[0].Certificate = [][]byte{testRSACertificate}
|
|
|
|
testConfig.Certificates[0].PrivateKey = testRSAPrivateKey
|
2012-04-12 17:35:21 +01:00
|
|
|
testConfig.Certificates[1].Certificate = [][]byte{testSNICertificate}
|
2013-07-17 17:33:16 +01:00
|
|
|
testConfig.Certificates[1].PrivateKey = testRSAPrivateKey
|
2012-04-12 17:35:21 +01:00
|
|
|
testConfig.BuildNameToCertificate()
|
2009-11-05 23:44:32 +00:00
|
|
|
}
|
|
|
|
|
2015-04-03 00:19:46 +01:00
|
|
|
func testClientHello(t *testing.T, serverConfig *Config, m handshakeMessage) {
|
|
|
|
testClientHelloFailure(t, serverConfig, m, "")
|
|
|
|
}
|
|
|
|
|
2015-03-17 00:13:10 +00:00
|
|
|
func testClientHelloFailure(t *testing.T, serverConfig *Config, m handshakeMessage, expectedSubStr string) {
|
2010-04-27 06:19:04 +01:00
|
|
|
// Create in-memory network connection,
|
2016-03-01 23:21:55 +00:00
|
|
|
// send message to server. Should return
|
2010-04-27 06:19:04 +01:00
|
|
|
// expected error.
|
|
|
|
c, s := net.Pipe()
|
|
|
|
go func() {
|
|
|
|
cli := Client(c, testConfig)
|
|
|
|
if ch, ok := m.(*clientHelloMsg); ok {
|
|
|
|
cli.vers = ch.vers
|
|
|
|
}
|
|
|
|
cli.writeRecord(recordTypeHandshake, m.marshal())
|
|
|
|
c.Close()
|
|
|
|
}()
|
2016-02-26 19:17:29 +00:00
|
|
|
hs := serverHandshakeState{
|
|
|
|
c: Server(s, serverConfig),
|
|
|
|
}
|
|
|
|
_, err := hs.readClientHello()
|
2010-04-27 06:19:04 +01:00
|
|
|
s.Close()
|
2015-04-03 00:19:46 +01:00
|
|
|
if len(expectedSubStr) == 0 {
|
|
|
|
if err != nil && err != io.EOF {
|
2015-08-11 06:29:40 +01:00
|
|
|
t.Errorf("Got error: %s; expected to succeed", err)
|
2015-04-03 00:19:46 +01:00
|
|
|
}
|
|
|
|
} else if err == nil || !strings.Contains(err.Error(), expectedSubStr) {
|
2014-02-12 16:20:01 +00:00
|
|
|
t.Errorf("Got error: %s; expected to match substring '%s'", err, expectedSubStr)
|
2009-11-05 23:44:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSimpleError(t *testing.T) {
|
2015-03-17 00:13:10 +00:00
|
|
|
testClientHelloFailure(t, testConfig, &serverHelloDoneMsg{}, "unexpected handshake message")
|
2009-11-05 23:44:32 +00:00
|
|
|
}
|
|
|
|
|
2011-09-14 20:32:19 +01:00
|
|
|
var badProtocolVersions = []uint16{0x0000, 0x0005, 0x0100, 0x0105, 0x0200, 0x0205}
|
2009-11-05 23:44:32 +00:00
|
|
|
|
|
|
|
func TestRejectBadProtocolVersion(t *testing.T) {
|
2010-04-27 06:19:04 +01:00
|
|
|
for _, v := range badProtocolVersions {
|
2015-03-17 00:13:10 +00:00
|
|
|
testClientHelloFailure(t, testConfig, &clientHelloMsg{vers: v}, "unsupported, maximum protocol version")
|
2009-11-05 23:44:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNoSuiteOverlap(t *testing.T) {
|
2012-09-24 21:52:43 +01:00
|
|
|
clientHello := &clientHelloMsg{
|
2016-02-26 23:26:04 +00:00
|
|
|
vers: VersionTLS10,
|
2012-09-24 21:52:43 +01:00
|
|
|
cipherSuites: []uint16{0xff00},
|
2016-02-26 23:26:04 +00:00
|
|
|
compressionMethods: []uint8{compressionNone},
|
2012-09-24 21:52:43 +01:00
|
|
|
}
|
2015-03-17 00:13:10 +00:00
|
|
|
testClientHelloFailure(t, testConfig, clientHello, "no cipher suite supported by both client and server")
|
2009-11-05 23:44:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNoCompressionOverlap(t *testing.T) {
|
2012-09-24 21:52:43 +01:00
|
|
|
clientHello := &clientHelloMsg{
|
2016-02-26 23:26:04 +00:00
|
|
|
vers: VersionTLS10,
|
2012-09-24 21:52:43 +01:00
|
|
|
cipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
|
|
|
|
compressionMethods: []uint8{0xff},
|
|
|
|
}
|
2015-03-17 00:13:10 +00:00
|
|
|
testClientHelloFailure(t, testConfig, clientHello, "client does not support uncompressed connections")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNoRC4ByDefault(t *testing.T) {
|
|
|
|
clientHello := &clientHelloMsg{
|
2016-02-26 23:26:04 +00:00
|
|
|
vers: VersionTLS10,
|
2015-03-17 00:13:10 +00:00
|
|
|
cipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
|
2016-02-26 23:26:04 +00:00
|
|
|
compressionMethods: []uint8{compressionNone},
|
2015-03-17 00:13:10 +00:00
|
|
|
}
|
2016-08-30 04:19:01 +01:00
|
|
|
serverConfig := testConfig.Clone()
|
2015-03-17 00:13:10 +00:00
|
|
|
// Reset the enabled cipher suites to nil in order to test the
|
|
|
|
// defaults.
|
|
|
|
serverConfig.CipherSuites = nil
|
2016-06-21 15:00:41 +01:00
|
|
|
testClientHelloFailure(t, serverConfig, clientHello, "no cipher suite supported by both client and server")
|
2009-11-05 23:44:32 +00:00
|
|
|
}
|
|
|
|
|
2015-04-03 00:19:46 +01:00
|
|
|
func TestDontSelectECDSAWithRSAKey(t *testing.T) {
|
|
|
|
// Test that, even when both sides support an ECDSA cipher suite, it
|
|
|
|
// won't be selected if the server's private key doesn't support it.
|
|
|
|
clientHello := &clientHelloMsg{
|
2016-02-26 23:26:04 +00:00
|
|
|
vers: VersionTLS10,
|
2015-04-03 00:19:46 +01:00
|
|
|
cipherSuites: []uint16{TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA},
|
2016-02-26 23:26:04 +00:00
|
|
|
compressionMethods: []uint8{compressionNone},
|
2015-04-03 00:19:46 +01:00
|
|
|
supportedCurves: []CurveID{CurveP256},
|
|
|
|
supportedPoints: []uint8{pointFormatUncompressed},
|
|
|
|
}
|
2016-08-30 04:19:01 +01:00
|
|
|
serverConfig := testConfig.Clone()
|
2015-04-03 00:19:46 +01:00
|
|
|
serverConfig.CipherSuites = clientHello.cipherSuites
|
|
|
|
serverConfig.Certificates = make([]Certificate, 1)
|
|
|
|
serverConfig.Certificates[0].Certificate = [][]byte{testECDSACertificate}
|
|
|
|
serverConfig.Certificates[0].PrivateKey = testECDSAPrivateKey
|
|
|
|
serverConfig.BuildNameToCertificate()
|
|
|
|
// First test that it *does* work when the server's key is ECDSA.
|
2016-06-21 15:00:41 +01:00
|
|
|
testClientHello(t, serverConfig, clientHello)
|
2015-04-03 00:19:46 +01:00
|
|
|
|
|
|
|
// Now test that switching to an RSA key causes the expected error (and
|
|
|
|
// not an internal error about a signing failure).
|
|
|
|
serverConfig.Certificates = testConfig.Certificates
|
2016-06-21 15:00:41 +01:00
|
|
|
testClientHelloFailure(t, serverConfig, clientHello, "no cipher suite supported by both client and server")
|
2015-04-03 00:19:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDontSelectRSAWithECDSAKey(t *testing.T) {
|
|
|
|
// Test that, even when both sides support an RSA cipher suite, it
|
|
|
|
// won't be selected if the server's private key doesn't support it.
|
|
|
|
clientHello := &clientHelloMsg{
|
2016-02-26 23:26:04 +00:00
|
|
|
vers: VersionTLS10,
|
2015-04-03 00:19:46 +01:00
|
|
|
cipherSuites: []uint16{TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA},
|
2016-02-26 23:26:04 +00:00
|
|
|
compressionMethods: []uint8{compressionNone},
|
2015-04-03 00:19:46 +01:00
|
|
|
supportedCurves: []CurveID{CurveP256},
|
|
|
|
supportedPoints: []uint8{pointFormatUncompressed},
|
|
|
|
}
|
2016-08-30 04:19:01 +01:00
|
|
|
serverConfig := testConfig.Clone()
|
2015-04-03 00:19:46 +01:00
|
|
|
serverConfig.CipherSuites = clientHello.cipherSuites
|
|
|
|
// First test that it *does* work when the server's key is RSA.
|
2016-06-21 15:00:41 +01:00
|
|
|
testClientHello(t, serverConfig, clientHello)
|
2015-04-03 00:19:46 +01:00
|
|
|
|
|
|
|
// Now test that switching to an ECDSA key causes the expected error
|
|
|
|
// (and not an internal error about a signing failure).
|
|
|
|
serverConfig.Certificates = make([]Certificate, 1)
|
|
|
|
serverConfig.Certificates[0].Certificate = [][]byte{testECDSACertificate}
|
|
|
|
serverConfig.Certificates[0].PrivateKey = testECDSAPrivateKey
|
|
|
|
serverConfig.BuildNameToCertificate()
|
2016-06-21 15:00:41 +01:00
|
|
|
testClientHelloFailure(t, serverConfig, clientHello, "no cipher suite supported by both client and server")
|
2015-04-03 00:19:46 +01:00
|
|
|
}
|
|
|
|
|
2014-12-19 23:14:03 +00:00
|
|
|
func TestRenegotiationExtension(t *testing.T) {
|
|
|
|
clientHello := &clientHelloMsg{
|
2016-04-26 18:45:35 +01:00
|
|
|
vers: VersionTLS12,
|
|
|
|
compressionMethods: []uint8{compressionNone},
|
|
|
|
random: make([]byte, 32),
|
|
|
|
secureRenegotiationSupported: true,
|
|
|
|
cipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
|
2014-12-19 23:14:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var buf []byte
|
|
|
|
c, s := net.Pipe()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
cli := Client(c, testConfig)
|
|
|
|
cli.vers = clientHello.vers
|
|
|
|
cli.writeRecord(recordTypeHandshake, clientHello.marshal())
|
|
|
|
|
|
|
|
buf = make([]byte, 1024)
|
|
|
|
n, err := c.Read(buf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Server read returned error: %s", err)
|
|
|
|
}
|
|
|
|
buf = buf[:n]
|
|
|
|
c.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
Server(s, testConfig).Handshake()
|
|
|
|
|
|
|
|
if len(buf) < 5+4 {
|
|
|
|
t.Fatalf("Server returned short message of length %d", len(buf))
|
|
|
|
}
|
|
|
|
// buf contains a TLS record, with a 5 byte record header and a 4 byte
|
|
|
|
// handshake header. The length of the ServerHello is taken from the
|
|
|
|
// handshake header.
|
|
|
|
serverHelloLen := int(buf[6])<<16 | int(buf[7])<<8 | int(buf[8])
|
|
|
|
|
|
|
|
var serverHello serverHelloMsg
|
|
|
|
// unmarshal expects to be given the handshake header, but
|
|
|
|
// serverHelloLen doesn't include it.
|
|
|
|
if !serverHello.unmarshal(buf[5 : 9+serverHelloLen]) {
|
|
|
|
t.Fatalf("Failed to parse ServerHello")
|
|
|
|
}
|
|
|
|
|
2016-04-26 18:45:35 +01:00
|
|
|
if !serverHello.secureRenegotiationSupported {
|
2014-12-19 23:14:03 +00:00
|
|
|
t.Errorf("Secure renegotiation extension was not echoed.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-26 22:09:56 +01:00
|
|
|
func TestTLS12OnlyCipherSuites(t *testing.T) {
|
|
|
|
// Test that a Server doesn't select a TLS 1.2-only cipher suite when
|
|
|
|
// the client negotiates TLS 1.1.
|
|
|
|
var zeros [32]byte
|
|
|
|
|
|
|
|
clientHello := &clientHelloMsg{
|
|
|
|
vers: VersionTLS11,
|
|
|
|
random: zeros[:],
|
|
|
|
cipherSuites: []uint16{
|
|
|
|
// The Server, by default, will use the client's
|
|
|
|
// preference order. So the GCM cipher suite
|
|
|
|
// will be selected unless it's excluded because
|
|
|
|
// of the version in this ClientHello.
|
|
|
|
TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
|
|
|
|
TLS_RSA_WITH_RC4_128_SHA,
|
|
|
|
},
|
|
|
|
compressionMethods: []uint8{compressionNone},
|
2014-02-24 22:57:51 +00:00
|
|
|
supportedCurves: []CurveID{CurveP256, CurveP384, CurveP521},
|
2013-09-26 22:09:56 +01:00
|
|
|
supportedPoints: []uint8{pointFormatUncompressed},
|
|
|
|
}
|
|
|
|
|
|
|
|
c, s := net.Pipe()
|
|
|
|
var reply interface{}
|
|
|
|
var clientErr error
|
|
|
|
go func() {
|
|
|
|
cli := Client(c, testConfig)
|
|
|
|
cli.vers = clientHello.vers
|
|
|
|
cli.writeRecord(recordTypeHandshake, clientHello.marshal())
|
|
|
|
reply, clientErr = cli.readHandshake()
|
|
|
|
c.Close()
|
|
|
|
}()
|
2016-08-30 04:19:01 +01:00
|
|
|
config := testConfig.Clone()
|
2013-09-26 22:09:56 +01:00
|
|
|
config.CipherSuites = clientHello.cipherSuites
|
2016-06-21 15:00:41 +01:00
|
|
|
Server(s, config).Handshake()
|
2013-09-26 22:09:56 +01:00
|
|
|
s.Close()
|
|
|
|
if clientErr != nil {
|
|
|
|
t.Fatal(clientErr)
|
|
|
|
}
|
|
|
|
serverHello, ok := reply.(*serverHelloMsg)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("didn't get ServerHello message in reply. Got %v\n", reply)
|
|
|
|
}
|
|
|
|
if s := serverHello.cipherSuite; s != TLS_RSA_WITH_RC4_128_SHA {
|
|
|
|
t.Fatalf("bad cipher suite from server: %x", s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-05 23:44:32 +00:00
|
|
|
func TestAlertForwarding(t *testing.T) {
|
2010-04-27 06:19:04 +01:00
|
|
|
c, s := net.Pipe()
|
|
|
|
go func() {
|
|
|
|
Client(c, testConfig).sendAlert(alertUnknownCA)
|
|
|
|
c.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
err := Server(s, testConfig).Handshake()
|
|
|
|
s.Close()
|
2011-11-02 02:04:37 +00:00
|
|
|
if e, ok := err.(*net.OpError); !ok || e.Err != error(alertUnknownCA) {
|
2013-07-03 00:58:56 +01:00
|
|
|
t.Errorf("Got error: %s; expected: %s", err, error(alertUnknownCA))
|
2009-11-05 23:44:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClose(t *testing.T) {
|
2010-04-27 06:19:04 +01:00
|
|
|
c, s := net.Pipe()
|
|
|
|
go c.Close()
|
2009-11-05 23:44:32 +00:00
|
|
|
|
2010-04-27 06:19:04 +01:00
|
|
|
err := Server(s, testConfig).Handshake()
|
|
|
|
s.Close()
|
2011-11-02 02:04:37 +00:00
|
|
|
if err != io.EOF {
|
|
|
|
t.Errorf("Got error: %s; expected: %s", err, io.EOF)
|
2009-11-05 23:44:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-16 19:59:22 +01:00
|
|
|
func testHandshake(clientConfig, serverConfig *Config) (serverState, clientState ConnectionState, err error) {
|
2013-01-22 15:10:38 +00:00
|
|
|
c, s := net.Pipe()
|
2014-01-22 23:24:03 +00:00
|
|
|
done := make(chan bool)
|
2013-01-22 15:10:38 +00:00
|
|
|
go func() {
|
|
|
|
cli := Client(c, clientConfig)
|
|
|
|
cli.Handshake()
|
2015-04-16 19:59:22 +01:00
|
|
|
clientState = cli.ConnectionState()
|
2013-01-22 15:10:38 +00:00
|
|
|
c.Close()
|
2014-01-22 23:24:03 +00:00
|
|
|
done <- true
|
2013-01-22 15:10:38 +00:00
|
|
|
}()
|
|
|
|
server := Server(s, serverConfig)
|
|
|
|
err = server.Handshake()
|
|
|
|
if err == nil {
|
2015-04-16 19:59:22 +01:00
|
|
|
serverState = server.ConnectionState()
|
2013-01-22 15:10:38 +00:00
|
|
|
}
|
|
|
|
s.Close()
|
2014-01-22 23:24:03 +00:00
|
|
|
<-done
|
2013-01-22 15:10:38 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-02-24 23:01:28 +00:00
|
|
|
func TestVersion(t *testing.T) {
|
|
|
|
serverConfig := &Config{
|
|
|
|
Certificates: testConfig.Certificates,
|
|
|
|
MaxVersion: VersionTLS11,
|
|
|
|
}
|
|
|
|
clientConfig := &Config{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
}
|
2015-04-16 19:59:22 +01:00
|
|
|
state, _, err := testHandshake(clientConfig, serverConfig)
|
2014-02-24 23:01:28 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("handshake failed: %s", err)
|
|
|
|
}
|
|
|
|
if state.Version != VersionTLS11 {
|
|
|
|
t.Fatalf("Incorrect version %x, should be %x", state.Version, VersionTLS11)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-22 15:10:38 +00:00
|
|
|
func TestCipherSuitePreference(t *testing.T) {
|
|
|
|
serverConfig := &Config{
|
|
|
|
CipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA, TLS_RSA_WITH_AES_128_CBC_SHA, TLS_ECDHE_RSA_WITH_RC4_128_SHA},
|
|
|
|
Certificates: testConfig.Certificates,
|
2013-07-03 00:58:56 +01:00
|
|
|
MaxVersion: VersionTLS11,
|
2013-01-22 15:10:38 +00:00
|
|
|
}
|
|
|
|
clientConfig := &Config{
|
|
|
|
CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA, TLS_RSA_WITH_RC4_128_SHA},
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
}
|
2015-04-16 19:59:22 +01:00
|
|
|
state, _, err := testHandshake(clientConfig, serverConfig)
|
2013-01-22 15:10:38 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("handshake failed: %s", err)
|
|
|
|
}
|
|
|
|
if state.CipherSuite != TLS_RSA_WITH_AES_128_CBC_SHA {
|
|
|
|
// By default the server should use the client's preference.
|
|
|
|
t.Fatalf("Client's preference was not used, got %x", state.CipherSuite)
|
|
|
|
}
|
|
|
|
|
|
|
|
serverConfig.PreferServerCipherSuites = true
|
2015-04-16 19:59:22 +01:00
|
|
|
state, _, err = testHandshake(clientConfig, serverConfig)
|
2013-01-22 15:10:38 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("handshake failed: %s", err)
|
|
|
|
}
|
|
|
|
if state.CipherSuite != TLS_RSA_WITH_RC4_128_SHA {
|
|
|
|
t.Fatalf("Server's preference was not used, got %x", state.CipherSuite)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-16 19:59:22 +01:00
|
|
|
func TestSCTHandshake(t *testing.T) {
|
|
|
|
expected := [][]byte{[]byte("certificate"), []byte("transparency")}
|
|
|
|
serverConfig := &Config{
|
|
|
|
Certificates: []Certificate{{
|
|
|
|
Certificate: [][]byte{testRSACertificate},
|
|
|
|
PrivateKey: testRSAPrivateKey,
|
|
|
|
SignedCertificateTimestamps: expected,
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
clientConfig := &Config{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
}
|
|
|
|
_, state, err := testHandshake(clientConfig, serverConfig)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("handshake failed: %s", err)
|
|
|
|
}
|
|
|
|
actual := state.SignedCertificateTimestamps
|
|
|
|
if len(actual) != len(expected) {
|
|
|
|
t.Fatalf("got %d scts, want %d", len(actual), len(expected))
|
|
|
|
}
|
|
|
|
for i, sct := range expected {
|
|
|
|
if !bytes.Equal(sct, actual[i]) {
|
|
|
|
t.Fatalf("SCT #%d was %x, but expected %x", i, actual[i], sct)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-15 16:41:40 +00:00
|
|
|
func TestCrossVersionResume(t *testing.T) {
|
|
|
|
serverConfig := &Config{
|
|
|
|
CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
|
|
|
|
Certificates: testConfig.Certificates,
|
|
|
|
}
|
|
|
|
clientConfig := &Config{
|
|
|
|
CipherSuites: []uint16{TLS_RSA_WITH_AES_128_CBC_SHA},
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
ClientSessionCache: NewLRUClientSessionCache(1),
|
|
|
|
ServerName: "servername",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Establish a session at TLS 1.1.
|
|
|
|
clientConfig.MaxVersion = VersionTLS11
|
|
|
|
_, _, err := testHandshake(clientConfig, serverConfig)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("handshake failed: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The client session cache now contains a TLS 1.1 session.
|
|
|
|
state, _, err := testHandshake(clientConfig, serverConfig)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("handshake failed: %s", err)
|
|
|
|
}
|
|
|
|
if !state.DidResume {
|
|
|
|
t.Fatalf("handshake did not resume at the same version")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that the server will decline to resume at a lower version.
|
|
|
|
clientConfig.MaxVersion = VersionTLS10
|
|
|
|
state, _, err = testHandshake(clientConfig, serverConfig)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("handshake failed: %s", err)
|
|
|
|
}
|
|
|
|
if state.DidResume {
|
|
|
|
t.Fatalf("handshake resumed at a lower version")
|
|
|
|
}
|
|
|
|
|
|
|
|
// The client session cache now contains a TLS 1.0 session.
|
|
|
|
state, _, err = testHandshake(clientConfig, serverConfig)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("handshake failed: %s", err)
|
|
|
|
}
|
|
|
|
if !state.DidResume {
|
|
|
|
t.Fatalf("handshake did not resume at the same version")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that the server will decline to resume at a higher version.
|
|
|
|
clientConfig.MaxVersion = VersionTLS11
|
|
|
|
state, _, err = testHandshake(clientConfig, serverConfig)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("handshake failed: %s", err)
|
|
|
|
}
|
|
|
|
if state.DidResume {
|
|
|
|
t.Fatalf("handshake resumed at a higher version")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-20 16:37:05 +00:00
|
|
|
// Note: see comment in handshake_test.go for details of how the reference
|
|
|
|
// tests work.
|
|
|
|
|
|
|
|
// serverTest represents a test of the TLS server handshake against a reference
|
|
|
|
// implementation.
|
|
|
|
type serverTest struct {
|
|
|
|
// name is a freeform string identifying the test and the file in which
|
|
|
|
// the expected results will be stored.
|
|
|
|
name string
|
|
|
|
// command, if not empty, contains a series of arguments for the
|
|
|
|
// command to run for the reference server.
|
|
|
|
command []string
|
|
|
|
// expectedPeerCerts contains a list of PEM blocks of expected
|
|
|
|
// certificates from the client.
|
|
|
|
expectedPeerCerts []string
|
|
|
|
// config, if not nil, contains a custom Config to use for this test.
|
|
|
|
config *Config
|
2014-10-16 01:54:04 +01:00
|
|
|
// expectHandshakeErrorIncluding, when not empty, contains a string
|
|
|
|
// that must be a substring of the error resulting from the handshake.
|
|
|
|
expectHandshakeErrorIncluding string
|
2014-08-05 19:36:20 +01:00
|
|
|
// validate, if not nil, is a function that will be called with the
|
|
|
|
// ConnectionState of the resulting connection. It returns false if the
|
|
|
|
// ConnectionState is unacceptable.
|
|
|
|
validate func(ConnectionState) error
|
2013-12-20 16:37:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var defaultClientCommand = []string{"openssl", "s_client", "-no_ticket"}
|
|
|
|
|
|
|
|
// connFromCommand starts opens a listening socket and starts the reference
|
|
|
|
// client to connect to it. It returns a recordingConn that wraps the resulting
|
|
|
|
// connection.
|
|
|
|
func (test *serverTest) connFromCommand() (conn *recordingConn, child *exec.Cmd, err error) {
|
|
|
|
l, err := net.ListenTCP("tcp", &net.TCPAddr{
|
|
|
|
IP: net.IPv4(127, 0, 0, 1),
|
|
|
|
Port: 0,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
2012-01-05 17:05:38 +00:00
|
|
|
}
|
2013-12-20 16:37:05 +00:00
|
|
|
defer l.Close()
|
2009-11-05 23:44:32 +00:00
|
|
|
|
2013-12-20 16:37:05 +00:00
|
|
|
port := l.Addr().(*net.TCPAddr).Port
|
2012-01-05 17:05:38 +00:00
|
|
|
|
2013-12-20 16:37:05 +00:00
|
|
|
var command []string
|
|
|
|
command = append(command, test.command...)
|
|
|
|
if len(command) == 0 {
|
|
|
|
command = defaultClientCommand
|
2013-07-17 17:33:16 +01:00
|
|
|
}
|
2013-12-20 16:37:05 +00:00
|
|
|
command = append(command, "-connect")
|
|
|
|
command = append(command, fmt.Sprintf("127.0.0.1:%d", port))
|
|
|
|
cmd := exec.Command(command[0], command[1:]...)
|
|
|
|
cmd.Stdin = nil
|
|
|
|
var output bytes.Buffer
|
|
|
|
cmd.Stdout = &output
|
|
|
|
cmd.Stderr = &output
|
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
return nil, nil, err
|
2012-01-05 17:05:38 +00:00
|
|
|
}
|
2011-09-12 21:52:49 +01:00
|
|
|
|
2013-12-20 16:37:05 +00:00
|
|
|
connChan := make(chan interface{})
|
|
|
|
go func() {
|
|
|
|
tcpConn, err := l.Accept()
|
|
|
|
if err != nil {
|
|
|
|
connChan <- err
|
|
|
|
}
|
|
|
|
connChan <- tcpConn
|
|
|
|
}()
|
2013-09-17 18:30:36 +01:00
|
|
|
|
2013-12-20 16:37:05 +00:00
|
|
|
var tcpConn net.Conn
|
|
|
|
select {
|
|
|
|
case connOrError := <-connChan:
|
|
|
|
if err, ok := connOrError.(error); ok {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
tcpConn = connOrError.(net.Conn)
|
|
|
|
case <-time.After(2 * time.Second):
|
|
|
|
output.WriteTo(os.Stdout)
|
|
|
|
return nil, nil, errors.New("timed out waiting for connection from child process")
|
2013-09-17 18:30:36 +01:00
|
|
|
}
|
2013-08-29 22:18:59 +01:00
|
|
|
|
2013-12-20 16:37:05 +00:00
|
|
|
record := &recordingConn{
|
|
|
|
Conn: tcpConn,
|
2012-04-11 17:55:57 +01:00
|
|
|
}
|
|
|
|
|
2013-12-20 16:37:05 +00:00
|
|
|
return record, cmd, nil
|
2012-04-11 17:55:57 +01:00
|
|
|
}
|
|
|
|
|
2013-12-20 16:37:05 +00:00
|
|
|
func (test *serverTest) dataPath() string {
|
|
|
|
return filepath.Join("testdata", "Server-"+test.name)
|
2012-04-11 17:55:57 +01:00
|
|
|
}
|
|
|
|
|
2013-12-20 16:37:05 +00:00
|
|
|
func (test *serverTest) loadData() (flows [][]byte, err error) {
|
|
|
|
in, err := os.Open(test.dataPath())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2012-04-11 17:55:57 +01:00
|
|
|
}
|
2013-12-20 16:37:05 +00:00
|
|
|
defer in.Close()
|
|
|
|
return parseTestData(in)
|
2012-04-11 17:55:57 +01:00
|
|
|
}
|
|
|
|
|
2013-12-20 16:37:05 +00:00
|
|
|
func (test *serverTest) run(t *testing.T, write bool) {
|
|
|
|
var clientConn, serverConn net.Conn
|
|
|
|
var recordingConn *recordingConn
|
|
|
|
var childProcess *exec.Cmd
|
2013-07-17 17:33:16 +01:00
|
|
|
|
2013-12-20 16:37:05 +00:00
|
|
|
if write {
|
|
|
|
var err error
|
|
|
|
recordingConn, childProcess, err = test.connFromCommand()
|
2011-10-11 18:07:32 +01:00
|
|
|
if err != nil {
|
2013-12-20 16:37:05 +00:00
|
|
|
t.Fatalf("Failed to start subcommand: %s", err)
|
2011-10-11 18:07:32 +01:00
|
|
|
}
|
2013-12-20 16:37:05 +00:00
|
|
|
serverConn = recordingConn
|
|
|
|
} else {
|
|
|
|
clientConn, serverConn = net.Pipe()
|
2011-10-11 18:07:32 +01:00
|
|
|
}
|
2013-12-20 16:37:05 +00:00
|
|
|
config := test.config
|
|
|
|
if config == nil {
|
|
|
|
config = testConfig
|
2013-07-17 17:33:16 +01:00
|
|
|
}
|
2013-12-20 16:37:05 +00:00
|
|
|
server := Server(serverConn, config)
|
2014-08-05 19:36:20 +01:00
|
|
|
connStateChan := make(chan ConnectionState, 1)
|
2013-12-20 16:37:05 +00:00
|
|
|
go func() {
|
2016-02-26 23:26:04 +00:00
|
|
|
_, err := server.Write([]byte("hello, world\n"))
|
2014-10-16 01:54:04 +01:00
|
|
|
if len(test.expectHandshakeErrorIncluding) > 0 {
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Error expected, but no error returned")
|
|
|
|
} else if s := err.Error(); !strings.Contains(s, test.expectHandshakeErrorIncluding) {
|
|
|
|
t.Errorf("Error expected containing '%s' but got '%s'", test.expectHandshakeErrorIncluding, s)
|
|
|
|
}
|
2016-02-26 23:26:04 +00:00
|
|
|
} else {
|
|
|
|
if err != nil {
|
|
|
|
t.Logf("Error from Server.Write: '%s'", err)
|
|
|
|
}
|
2014-10-16 01:54:04 +01:00
|
|
|
}
|
2013-12-20 16:37:05 +00:00
|
|
|
server.Close()
|
|
|
|
serverConn.Close()
|
2014-08-05 19:36:20 +01:00
|
|
|
connStateChan <- server.ConnectionState()
|
2013-12-20 16:37:05 +00:00
|
|
|
}()
|
2009-11-05 23:44:32 +00:00
|
|
|
|
2013-12-20 16:37:05 +00:00
|
|
|
if !write {
|
|
|
|
flows, err := test.loadData()
|
2010-04-27 06:19:04 +01:00
|
|
|
if err != nil {
|
2015-04-15 20:00:53 +01:00
|
|
|
t.Fatalf("%s: failed to load data from %s", test.name, test.dataPath())
|
2012-04-11 17:55:57 +01:00
|
|
|
}
|
2013-12-20 16:37:05 +00:00
|
|
|
for i, b := range flows {
|
|
|
|
if i%2 == 0 {
|
|
|
|
clientConn.Write(b)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
bb := make([]byte, len(b))
|
|
|
|
n, err := io.ReadFull(clientConn, bb)
|
2015-04-15 20:00:53 +01:00
|
|
|
if err != nil {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(b, bb) {
|
|
|
|
t.Fatalf("%s #%d: mismatch on read: got:%x want:%x", test.name, i+1, bb, b)
|
2013-12-20 16:37:05 +00:00
|
|
|
}
|
2012-04-11 17:55:57 +01:00
|
|
|
}
|
2013-12-20 16:37:05 +00:00
|
|
|
clientConn.Close()
|
|
|
|
}
|
2012-04-11 17:55:57 +01:00
|
|
|
|
2014-08-05 19:36:20 +01:00
|
|
|
connState := <-connStateChan
|
|
|
|
peerCerts := connState.PeerCertificates
|
2013-12-20 16:37:05 +00:00
|
|
|
if len(peerCerts) == len(test.expectedPeerCerts) {
|
|
|
|
for i, peerCert := range peerCerts {
|
|
|
|
block, _ := pem.Decode([]byte(test.expectedPeerCerts[i]))
|
|
|
|
if !bytes.Equal(block.Bytes, peerCert.Raw) {
|
|
|
|
t.Fatalf("%s: mismatch on peer cert %d", test.name, i+1)
|
|
|
|
}
|
2010-04-27 06:19:04 +01:00
|
|
|
}
|
2013-12-20 16:37:05 +00:00
|
|
|
} else {
|
|
|
|
t.Fatalf("%s: mismatch on peer list length: %d (wanted) != %d (got)", test.name, len(test.expectedPeerCerts), len(peerCerts))
|
|
|
|
}
|
2012-01-05 17:05:38 +00:00
|
|
|
|
2014-08-05 19:36:20 +01:00
|
|
|
if test.validate != nil {
|
|
|
|
if err := test.validate(connState); err != nil {
|
|
|
|
t.Fatalf("validate callback returned error: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-20 16:37:05 +00:00
|
|
|
if write {
|
|
|
|
path := test.dataPath()
|
|
|
|
out, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
|
2010-12-16 22:14:02 +00:00
|
|
|
if err != nil {
|
2013-12-20 16:37:05 +00:00
|
|
|
t.Fatalf("Failed to create output file: %s", err)
|
2010-12-16 22:14:02 +00:00
|
|
|
}
|
2013-12-20 16:37:05 +00:00
|
|
|
defer out.Close()
|
|
|
|
recordingConn.Close()
|
|
|
|
if len(recordingConn.flows) < 3 {
|
|
|
|
childProcess.Stdout.(*bytes.Buffer).WriteTo(os.Stdout)
|
2014-10-16 01:54:04 +01:00
|
|
|
if len(test.expectHandshakeErrorIncluding) == 0 {
|
|
|
|
t.Fatalf("Handshake failed")
|
|
|
|
}
|
2013-12-20 16:37:05 +00:00
|
|
|
}
|
|
|
|
recordingConn.WriteTo(out)
|
|
|
|
fmt.Printf("Wrote %s\n", path)
|
|
|
|
childProcess.Wait()
|
2010-04-27 06:19:04 +01:00
|
|
|
}
|
|
|
|
}
|
2009-11-05 23:44:32 +00:00
|
|
|
|
2013-12-20 16:37:05 +00:00
|
|
|
func runServerTestForVersion(t *testing.T, template *serverTest, prefix, option string) {
|
|
|
|
test := *template
|
|
|
|
test.name = prefix + test.name
|
|
|
|
if len(test.command) == 0 {
|
|
|
|
test.command = defaultClientCommand
|
|
|
|
}
|
|
|
|
test.command = append([]string(nil), test.command...)
|
|
|
|
test.command = append(test.command, option)
|
|
|
|
test.run(t, *update)
|
2009-11-05 23:44:32 +00:00
|
|
|
}
|
|
|
|
|
2013-12-20 16:37:05 +00:00
|
|
|
func runServerTestSSLv3(t *testing.T, template *serverTest) {
|
|
|
|
runServerTestForVersion(t, template, "SSLv3-", "-ssl3")
|
2010-04-27 06:19:04 +01:00
|
|
|
}
|
|
|
|
|
2013-12-20 16:37:05 +00:00
|
|
|
func runServerTestTLS10(t *testing.T, template *serverTest) {
|
|
|
|
runServerTestForVersion(t, template, "TLSv10-", "-tls1")
|
|
|
|
}
|
2012-04-12 17:35:21 +01:00
|
|
|
|
2013-12-20 16:37:05 +00:00
|
|
|
func runServerTestTLS11(t *testing.T, template *serverTest) {
|
|
|
|
runServerTestForVersion(t, template, "TLSv11-", "-tls1_1")
|
2010-04-27 06:19:04 +01:00
|
|
|
}
|
|
|
|
|
2013-12-20 16:37:05 +00:00
|
|
|
func runServerTestTLS12(t *testing.T, template *serverTest) {
|
|
|
|
runServerTestForVersion(t, template, "TLSv12-", "-tls1_2")
|
2013-07-17 17:33:16 +01:00
|
|
|
}
|
|
|
|
|
2013-12-20 16:37:05 +00:00
|
|
|
func TestHandshakeServerRSARC4(t *testing.T) {
|
|
|
|
test := &serverTest{
|
|
|
|
name: "RSA-RC4",
|
|
|
|
command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "RC4-SHA"},
|
2012-01-05 17:05:38 +00:00
|
|
|
}
|
2013-12-20 16:37:05 +00:00
|
|
|
runServerTestSSLv3(t, test)
|
|
|
|
runServerTestTLS10(t, test)
|
|
|
|
runServerTestTLS11(t, test)
|
|
|
|
runServerTestTLS12(t, test)
|
2012-01-05 17:05:38 +00:00
|
|
|
}
|
|
|
|
|
2013-12-20 16:37:05 +00:00
|
|
|
func TestHandshakeServerRSA3DES(t *testing.T) {
|
|
|
|
test := &serverTest{
|
|
|
|
name: "RSA-3DES",
|
|
|
|
command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "DES-CBC3-SHA"},
|
|
|
|
}
|
|
|
|
runServerTestSSLv3(t, test)
|
|
|
|
runServerTestTLS10(t, test)
|
|
|
|
runServerTestTLS12(t, test)
|
2010-12-15 16:49:55 +00:00
|
|
|
}
|
2010-04-27 06:19:04 +01:00
|
|
|
|
2013-12-20 16:37:05 +00:00
|
|
|
func TestHandshakeServerRSAAES(t *testing.T) {
|
|
|
|
test := &serverTest{
|
|
|
|
name: "RSA-AES",
|
|
|
|
command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "AES128-SHA"},
|
|
|
|
}
|
|
|
|
runServerTestSSLv3(t, test)
|
|
|
|
runServerTestTLS10(t, test)
|
|
|
|
runServerTestTLS12(t, test)
|
2011-10-11 18:07:32 +01:00
|
|
|
}
|
|
|
|
|
2013-12-20 16:37:05 +00:00
|
|
|
func TestHandshakeServerAESGCM(t *testing.T) {
|
|
|
|
test := &serverTest{
|
|
|
|
name: "RSA-AES-GCM",
|
|
|
|
command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "ECDHE-RSA-AES128-GCM-SHA256"},
|
|
|
|
}
|
|
|
|
runServerTestTLS12(t, test)
|
2009-11-05 23:44:32 +00:00
|
|
|
}
|
2011-09-14 20:32:19 +01:00
|
|
|
|
2015-02-04 00:15:18 +00:00
|
|
|
func TestHandshakeServerAES256GCMSHA384(t *testing.T) {
|
|
|
|
test := &serverTest{
|
|
|
|
name: "RSA-AES256-GCM-SHA384",
|
|
|
|
command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "ECDHE-RSA-AES256-GCM-SHA384"},
|
|
|
|
}
|
|
|
|
runServerTestTLS12(t, test)
|
|
|
|
}
|
|
|
|
|
2013-12-20 16:37:05 +00:00
|
|
|
func TestHandshakeServerECDHEECDSAAES(t *testing.T) {
|
2016-08-30 04:19:01 +01:00
|
|
|
config := testConfig.Clone()
|
2013-12-20 16:37:05 +00:00
|
|
|
config.Certificates = make([]Certificate, 1)
|
|
|
|
config.Certificates[0].Certificate = [][]byte{testECDSACertificate}
|
|
|
|
config.Certificates[0].PrivateKey = testECDSAPrivateKey
|
|
|
|
config.BuildNameToCertificate()
|
2013-07-17 17:33:16 +01:00
|
|
|
|
2013-12-20 16:37:05 +00:00
|
|
|
test := &serverTest{
|
|
|
|
name: "ECDHE-ECDSA-AES",
|
|
|
|
command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "ECDHE-ECDSA-AES256-SHA"},
|
2016-06-21 15:00:41 +01:00
|
|
|
config: config,
|
2013-12-20 16:37:05 +00:00
|
|
|
}
|
|
|
|
runServerTestTLS10(t, test)
|
|
|
|
runServerTestTLS12(t, test)
|
2011-09-14 20:32:19 +01:00
|
|
|
}
|
2012-01-05 17:05:38 +00:00
|
|
|
|
2016-08-20 12:41:42 +01:00
|
|
|
func TestHandshakeServerKeyLog(t *testing.T) {
|
2016-08-30 04:19:01 +01:00
|
|
|
config := testConfig.Clone()
|
2016-08-20 12:41:42 +01:00
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
config.KeyLogWriter = buf
|
|
|
|
|
|
|
|
test := &serverTest{
|
|
|
|
name: "KeyLogWriter",
|
|
|
|
command: []string{"openssl", "s_client"},
|
|
|
|
config: config,
|
|
|
|
validate: func(state ConnectionState) error {
|
|
|
|
var format, clientRandom, masterSecret string
|
|
|
|
if _, err := fmt.Fscanf(buf, "%s %s %s\n", &format, &clientRandom, &masterSecret); err != nil {
|
|
|
|
return fmt.Errorf("failed to parse KeyLogWriter: " + err.Error())
|
|
|
|
}
|
|
|
|
if format != "CLIENT_RANDOM" {
|
|
|
|
return fmt.Errorf("got key log format %q, wanted CLIENT_RANDOM", format)
|
|
|
|
}
|
|
|
|
// Both clientRandom and masterSecret are unpredictable in server handshake test
|
|
|
|
if len(clientRandom) != 64 {
|
|
|
|
return fmt.Errorf("got wrong length client random in key log %v, wanted 64", len(clientRandom))
|
|
|
|
}
|
|
|
|
if len(masterSecret) != 96 {
|
|
|
|
return fmt.Errorf("got wrong length master secret in key log %v, want 96", len(masterSecret))
|
|
|
|
}
|
|
|
|
|
|
|
|
// buf should contain no more lines
|
|
|
|
var trailingGarbage string
|
|
|
|
if _, err := fmt.Fscanln(buf, &trailingGarbage); err == nil {
|
|
|
|
return fmt.Errorf("expected exactly one key in log, got trailing garbage %q", trailingGarbage)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
runServerTestTLS10(t, test)
|
|
|
|
}
|
|
|
|
|
2014-08-05 19:36:20 +01:00
|
|
|
func TestHandshakeServerALPN(t *testing.T) {
|
2016-08-30 04:19:01 +01:00
|
|
|
config := testConfig.Clone()
|
2014-08-05 19:36:20 +01:00
|
|
|
config.NextProtos = []string{"proto1", "proto2"}
|
|
|
|
|
|
|
|
test := &serverTest{
|
|
|
|
name: "ALPN",
|
|
|
|
// Note that this needs OpenSSL 1.0.2 because that is the first
|
|
|
|
// version that supports the -alpn flag.
|
|
|
|
command: []string{"openssl", "s_client", "-alpn", "proto2,proto1"},
|
2016-06-21 15:00:41 +01:00
|
|
|
config: config,
|
2014-08-05 19:36:20 +01:00
|
|
|
validate: func(state ConnectionState) error {
|
|
|
|
// The server's preferences should override the client.
|
|
|
|
if state.NegotiatedProtocol != "proto1" {
|
|
|
|
return fmt.Errorf("Got protocol %q, wanted proto1", state.NegotiatedProtocol)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
runServerTestTLS12(t, test)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHandshakeServerALPNNoMatch(t *testing.T) {
|
2016-08-30 04:19:01 +01:00
|
|
|
config := testConfig.Clone()
|
2014-08-05 19:36:20 +01:00
|
|
|
config.NextProtos = []string{"proto3"}
|
|
|
|
|
|
|
|
test := &serverTest{
|
|
|
|
name: "ALPN-NoMatch",
|
|
|
|
// Note that this needs OpenSSL 1.0.2 because that is the first
|
|
|
|
// version that supports the -alpn flag.
|
|
|
|
command: []string{"openssl", "s_client", "-alpn", "proto2,proto1"},
|
2016-06-21 15:00:41 +01:00
|
|
|
config: config,
|
2014-08-05 19:36:20 +01:00
|
|
|
validate: func(state ConnectionState) error {
|
|
|
|
// Rather than reject the connection, Go doesn't select
|
|
|
|
// a protocol when there is no overlap.
|
|
|
|
if state.NegotiatedProtocol != "" {
|
|
|
|
return fmt.Errorf("Got protocol %q, wanted ''", state.NegotiatedProtocol)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
runServerTestTLS12(t, test)
|
|
|
|
}
|
|
|
|
|
2013-12-20 16:37:05 +00:00
|
|
|
// TestHandshakeServerSNI involves a client sending an SNI extension of
|
|
|
|
// "snitest.com", which happens to match the CN of testSNICertificate. The test
|
|
|
|
// verifies that the server correctly selects that certificate.
|
|
|
|
func TestHandshakeServerSNI(t *testing.T) {
|
|
|
|
test := &serverTest{
|
|
|
|
name: "SNI",
|
|
|
|
command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "AES128-SHA", "-servername", "snitest.com"},
|
|
|
|
}
|
|
|
|
runServerTestTLS12(t, test)
|
2012-09-24 21:52:43 +01:00
|
|
|
}
|
|
|
|
|
2014-08-06 19:22:00 +01:00
|
|
|
// TestHandshakeServerSNICertForName is similar to TestHandshakeServerSNI, but
|
|
|
|
// tests the dynamic GetCertificate method
|
|
|
|
func TestHandshakeServerSNIGetCertificate(t *testing.T) {
|
2016-08-30 04:19:01 +01:00
|
|
|
config := testConfig.Clone()
|
2014-08-06 19:22:00 +01:00
|
|
|
|
|
|
|
// Replace the NameToCertificate map with a GetCertificate function
|
|
|
|
nameToCert := config.NameToCertificate
|
|
|
|
config.NameToCertificate = nil
|
|
|
|
config.GetCertificate = func(clientHello *ClientHelloInfo) (*Certificate, error) {
|
|
|
|
cert, _ := nameToCert[clientHello.ServerName]
|
|
|
|
return cert, nil
|
|
|
|
}
|
|
|
|
test := &serverTest{
|
2015-04-15 20:00:53 +01:00
|
|
|
name: "SNI-GetCertificate",
|
2014-08-06 19:22:00 +01:00
|
|
|
command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "AES128-SHA", "-servername", "snitest.com"},
|
2016-06-21 15:00:41 +01:00
|
|
|
config: config,
|
2014-08-06 19:22:00 +01:00
|
|
|
}
|
|
|
|
runServerTestTLS12(t, test)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestHandshakeServerSNICertForNameNotFound is similar to
|
|
|
|
// TestHandshakeServerSNICertForName, but tests to make sure that when the
|
|
|
|
// GetCertificate method doesn't return a cert, we fall back to what's in
|
|
|
|
// the NameToCertificate map.
|
|
|
|
func TestHandshakeServerSNIGetCertificateNotFound(t *testing.T) {
|
2016-08-30 04:19:01 +01:00
|
|
|
config := testConfig.Clone()
|
2014-08-06 19:22:00 +01:00
|
|
|
|
|
|
|
config.GetCertificate = func(clientHello *ClientHelloInfo) (*Certificate, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
test := &serverTest{
|
2015-04-15 20:00:53 +01:00
|
|
|
name: "SNI-GetCertificateNotFound",
|
2014-08-06 19:22:00 +01:00
|
|
|
command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "AES128-SHA", "-servername", "snitest.com"},
|
2016-06-21 15:00:41 +01:00
|
|
|
config: config,
|
2014-08-06 19:22:00 +01:00
|
|
|
}
|
|
|
|
runServerTestTLS12(t, test)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestHandshakeServerSNICertForNameError tests to make sure that errors in
|
|
|
|
// GetCertificate result in a tls alert.
|
|
|
|
func TestHandshakeServerSNIGetCertificateError(t *testing.T) {
|
2015-04-15 20:00:53 +01:00
|
|
|
const errMsg = "TestHandshakeServerSNIGetCertificateError error"
|
2014-08-06 19:22:00 +01:00
|
|
|
|
2016-08-30 04:19:01 +01:00
|
|
|
serverConfig := testConfig.Clone()
|
2015-04-15 20:00:53 +01:00
|
|
|
serverConfig.GetCertificate = func(clientHello *ClientHelloInfo) (*Certificate, error) {
|
|
|
|
return nil, errors.New(errMsg)
|
2014-08-06 19:22:00 +01:00
|
|
|
}
|
2015-04-15 20:00:53 +01:00
|
|
|
|
|
|
|
clientHello := &clientHelloMsg{
|
2016-02-26 23:26:04 +00:00
|
|
|
vers: VersionTLS10,
|
2015-04-15 20:00:53 +01:00
|
|
|
cipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
|
2016-02-26 23:26:04 +00:00
|
|
|
compressionMethods: []uint8{compressionNone},
|
2015-04-15 20:00:53 +01:00
|
|
|
serverName: "test",
|
2014-08-06 19:22:00 +01:00
|
|
|
}
|
2016-06-21 15:00:41 +01:00
|
|
|
testClientHelloFailure(t, serverConfig, clientHello, errMsg)
|
2014-08-06 19:22:00 +01:00
|
|
|
}
|
|
|
|
|
2015-04-13 00:41:31 +01:00
|
|
|
// TestHandshakeServerEmptyCertificates tests that GetCertificates is called in
|
|
|
|
// the case that Certificates is empty, even without SNI.
|
|
|
|
func TestHandshakeServerEmptyCertificates(t *testing.T) {
|
|
|
|
const errMsg = "TestHandshakeServerEmptyCertificates error"
|
|
|
|
|
2016-08-30 04:19:01 +01:00
|
|
|
serverConfig := testConfig.Clone()
|
2015-04-13 00:41:31 +01:00
|
|
|
serverConfig.GetCertificate = func(clientHello *ClientHelloInfo) (*Certificate, error) {
|
|
|
|
return nil, errors.New(errMsg)
|
|
|
|
}
|
|
|
|
serverConfig.Certificates = nil
|
|
|
|
|
|
|
|
clientHello := &clientHelloMsg{
|
2016-02-26 23:26:04 +00:00
|
|
|
vers: VersionTLS10,
|
2015-04-13 00:41:31 +01:00
|
|
|
cipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
|
2016-02-26 23:26:04 +00:00
|
|
|
compressionMethods: []uint8{compressionNone},
|
2015-04-13 00:41:31 +01:00
|
|
|
}
|
2016-06-21 15:00:41 +01:00
|
|
|
testClientHelloFailure(t, serverConfig, clientHello, errMsg)
|
2015-04-13 00:41:31 +01:00
|
|
|
|
|
|
|
// With an empty Certificates and a nil GetCertificate, the server
|
|
|
|
// should always return a “no certificates” error.
|
|
|
|
serverConfig.GetCertificate = nil
|
|
|
|
|
|
|
|
clientHello = &clientHelloMsg{
|
2016-02-26 23:26:04 +00:00
|
|
|
vers: VersionTLS10,
|
2015-04-13 00:41:31 +01:00
|
|
|
cipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
|
2016-02-26 23:26:04 +00:00
|
|
|
compressionMethods: []uint8{compressionNone},
|
2015-04-13 00:41:31 +01:00
|
|
|
}
|
2016-06-21 15:00:41 +01:00
|
|
|
testClientHelloFailure(t, serverConfig, clientHello, "no certificates")
|
2015-04-13 00:41:31 +01:00
|
|
|
}
|
|
|
|
|
2013-12-20 16:37:05 +00:00
|
|
|
// TestCipherSuiteCertPreferance ensures that we select an RSA ciphersuite with
|
|
|
|
// an RSA certificate and an ECDSA ciphersuite with an ECDSA certificate.
|
2014-01-09 18:38:11 +00:00
|
|
|
func TestCipherSuiteCertPreferenceECDSA(t *testing.T) {
|
2016-08-30 04:19:01 +01:00
|
|
|
config := testConfig.Clone()
|
2014-01-09 18:38:11 +00:00
|
|
|
config.CipherSuites = []uint16{TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA}
|
2013-12-20 16:37:05 +00:00
|
|
|
config.PreferServerCipherSuites = true
|
2012-04-12 17:35:21 +01:00
|
|
|
|
2013-12-20 16:37:05 +00:00
|
|
|
test := &serverTest{
|
|
|
|
name: "CipherSuiteCertPreferenceRSA",
|
2016-06-21 15:00:41 +01:00
|
|
|
config: config,
|
2013-12-20 16:37:05 +00:00
|
|
|
}
|
|
|
|
runServerTestTLS12(t, test)
|
|
|
|
|
2016-08-30 04:19:01 +01:00
|
|
|
config = testConfig.Clone()
|
2013-12-20 16:37:05 +00:00
|
|
|
config.CipherSuites = []uint16{TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA}
|
|
|
|
config.Certificates = []Certificate{
|
2014-07-01 18:28:10 +01:00
|
|
|
{
|
2013-12-20 16:37:05 +00:00
|
|
|
Certificate: [][]byte{testECDSACertificate},
|
|
|
|
PrivateKey: testECDSAPrivateKey,
|
2012-10-16 20:40:37 +01:00
|
|
|
},
|
2013-12-20 16:37:05 +00:00
|
|
|
}
|
|
|
|
config.BuildNameToCertificate()
|
|
|
|
config.PreferServerCipherSuites = true
|
2012-01-05 17:05:38 +00:00
|
|
|
|
2013-12-20 16:37:05 +00:00
|
|
|
test = &serverTest{
|
|
|
|
name: "CipherSuiteCertPreferenceECDSA",
|
2016-06-21 15:00:41 +01:00
|
|
|
config: config,
|
2013-12-20 16:37:05 +00:00
|
|
|
}
|
|
|
|
runServerTestTLS12(t, test)
|
2013-06-05 01:02:22 +01:00
|
|
|
}
|
|
|
|
|
2013-12-20 16:37:05 +00:00
|
|
|
func TestResumption(t *testing.T) {
|
|
|
|
sessionFilePath := tempFile("")
|
|
|
|
defer os.Remove(sessionFilePath)
|
|
|
|
|
|
|
|
test := &serverTest{
|
|
|
|
name: "IssueTicket",
|
|
|
|
command: []string{"openssl", "s_client", "-cipher", "RC4-SHA", "-sess_out", sessionFilePath},
|
|
|
|
}
|
|
|
|
runServerTestTLS12(t, test)
|
|
|
|
|
|
|
|
test = &serverTest{
|
|
|
|
name: "Resume",
|
|
|
|
command: []string{"openssl", "s_client", "-cipher", "RC4-SHA", "-sess_in", sessionFilePath},
|
|
|
|
}
|
|
|
|
runServerTestTLS12(t, test)
|
2013-07-03 00:58:56 +01:00
|
|
|
}
|
|
|
|
|
2014-09-26 02:02:09 +01:00
|
|
|
func TestResumptionDisabled(t *testing.T) {
|
|
|
|
sessionFilePath := tempFile("")
|
|
|
|
defer os.Remove(sessionFilePath)
|
|
|
|
|
2016-08-30 04:19:01 +01:00
|
|
|
config := testConfig.Clone()
|
2014-09-26 02:02:09 +01:00
|
|
|
|
|
|
|
test := &serverTest{
|
|
|
|
name: "IssueTicketPreDisable",
|
|
|
|
command: []string{"openssl", "s_client", "-cipher", "RC4-SHA", "-sess_out", sessionFilePath},
|
2016-06-21 15:00:41 +01:00
|
|
|
config: config,
|
2014-09-26 02:02:09 +01:00
|
|
|
}
|
|
|
|
runServerTestTLS12(t, test)
|
|
|
|
|
|
|
|
config.SessionTicketsDisabled = true
|
|
|
|
|
|
|
|
test = &serverTest{
|
|
|
|
name: "ResumeDisabled",
|
|
|
|
command: []string{"openssl", "s_client", "-cipher", "RC4-SHA", "-sess_in", sessionFilePath},
|
2016-06-21 15:00:41 +01:00
|
|
|
config: config,
|
2014-09-26 02:02:09 +01:00
|
|
|
}
|
|
|
|
runServerTestTLS12(t, test)
|
|
|
|
|
|
|
|
// One needs to manually confirm that the handshake in the golden data
|
|
|
|
// file for ResumeDisabled does not include a resumption handshake.
|
|
|
|
}
|
|
|
|
|
2014-10-16 01:54:04 +01:00
|
|
|
func TestFallbackSCSV(t *testing.T) {
|
2016-06-21 15:00:41 +01:00
|
|
|
serverConfig := Config{
|
2014-12-18 18:17:54 +00:00
|
|
|
Certificates: testConfig.Certificates,
|
|
|
|
}
|
2014-10-16 01:54:04 +01:00
|
|
|
test := &serverTest{
|
2014-12-18 18:17:54 +00:00
|
|
|
name: "FallbackSCSV",
|
2016-06-21 15:00:41 +01:00
|
|
|
config: &serverConfig,
|
2014-10-16 01:54:04 +01:00
|
|
|
// OpenSSL 1.0.1j is needed for the -fallback_scsv option.
|
|
|
|
command: []string{"openssl", "s_client", "-fallback_scsv"},
|
2015-03-06 13:59:12 +00:00
|
|
|
expectHandshakeErrorIncluding: "inappropriate protocol fallback",
|
2014-10-16 01:54:04 +01:00
|
|
|
}
|
|
|
|
runServerTestTLS11(t, test)
|
|
|
|
}
|
|
|
|
|
2016-08-17 23:55:15 +01:00
|
|
|
// clientCertificatePEM and clientKeyPEM were generated with generate_cert.go
|
|
|
|
// Thus, they have no ExtKeyUsage fields and trigger an error when verification
|
|
|
|
// is turned on.
|
2012-01-05 17:05:38 +00:00
|
|
|
|
2013-12-20 16:37:05 +00:00
|
|
|
const clientCertificatePEM = `
|
2012-01-05 17:05:38 +00:00
|
|
|
-----BEGIN CERTIFICATE-----
|
2016-08-17 23:55:15 +01:00
|
|
|
MIIB7zCCAVigAwIBAgIQXBnBiWWDVW/cC8m5k5/pvDANBgkqhkiG9w0BAQsFADAS
|
|
|
|
MRAwDgYDVQQKEwdBY21lIENvMB4XDTE2MDgxNzIxNTIzMVoXDTE3MDgxNzIxNTIz
|
|
|
|
MVowEjEQMA4GA1UEChMHQWNtZSBDbzCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkC
|
|
|
|
gYEAum+qhr3Pv5/y71yUYHhv6BPy0ZZvzdkybiI3zkH5yl0prOEn2mGi7oHLEMff
|
|
|
|
NFiVhuk9GeZcJ3NgyI14AvQdpJgJoxlwaTwlYmYqqyIjxXuFOE8uCXMyp70+m63K
|
|
|
|
hAfmDzr/d8WdQYUAirab7rCkPy1MTOZCPrtRyN1IVPQMjkcCAwEAAaNGMEQwDgYD
|
|
|
|
VR0PAQH/BAQDAgWgMBMGA1UdJQQMMAoGCCsGAQUFBwMBMAwGA1UdEwEB/wQCMAAw
|
|
|
|
DwYDVR0RBAgwBocEfwAAATANBgkqhkiG9w0BAQsFAAOBgQBGq0Si+yhU+Fpn+GKU
|
|
|
|
8ZqyGJ7ysd4dfm92lam6512oFmyc9wnTN+RLKzZ8Aa1B0jLYw9KT+RBrjpW5LBeK
|
|
|
|
o0RIvFkTgxYEiKSBXCUNmAysEbEoVr4dzWFihAm/1oDGRY2CLLTYg5vbySK3KhIR
|
|
|
|
e/oCO8HJ/+rJnahJ05XX1Q7lNQ==
|
2013-12-20 16:37:05 +00:00
|
|
|
-----END CERTIFICATE-----`
|
2012-01-05 17:05:38 +00:00
|
|
|
|
2013-12-20 16:37:05 +00:00
|
|
|
const clientKeyPEM = `
|
2012-01-05 17:05:38 +00:00
|
|
|
-----BEGIN RSA PRIVATE KEY-----
|
2016-08-17 23:55:15 +01:00
|
|
|
MIICXQIBAAKBgQC6b6qGvc+/n/LvXJRgeG/oE/LRlm/N2TJuIjfOQfnKXSms4Sfa
|
|
|
|
YaLugcsQx980WJWG6T0Z5lwnc2DIjXgC9B2kmAmjGXBpPCViZiqrIiPFe4U4Ty4J
|
|
|
|
czKnvT6brcqEB+YPOv93xZ1BhQCKtpvusKQ/LUxM5kI+u1HI3UhU9AyORwIDAQAB
|
|
|
|
AoGAEJZ03q4uuMb7b26WSQsOMeDsftdatT747LGgs3pNRkMJvTb/O7/qJjxoG+Mc
|
|
|
|
qeSj0TAZXp+PXXc3ikCECAc+R8rVMfWdmp903XgO/qYtmZGCorxAHEmR80SrfMXv
|
|
|
|
PJnznLQWc8U9nphQErR+tTESg7xWEzmFcPKwnZd1xg8ERYkCQQDTGtrFczlB2b/Z
|
|
|
|
9TjNMqUlMnTLIk/a/rPE2fLLmAYhK5sHnJdvDURaH2mF4nso0EGtENnTsh6LATnY
|
|
|
|
dkrxXGm9AkEA4hXHG2q3MnhgK1Z5hjv+Fnqd+8bcbII9WW4flFs15EKoMgS1w/PJ
|
|
|
|
zbsySaSy5IVS8XeShmT9+3lrleed4sy+UwJBAJOOAbxhfXP5r4+5R6ql66jES75w
|
|
|
|
jUCVJzJA5ORJrn8g64u2eGK28z/LFQbv9wXgCwfc72R468BdawFSLa/m2EECQGbZ
|
|
|
|
rWiFla26IVXV0xcD98VWJsTBZMlgPnSOqoMdM1kSEd4fUmlAYI/dFzV1XYSkOmVr
|
|
|
|
FhdZnklmpVDeu27P4c0CQQCuCOup0FlJSBpWY1TTfun/KMBkBatMz0VMA3d7FKIU
|
|
|
|
csPezl677Yjo8u1r/KzeI6zLg87Z8E6r6ZWNc9wBSZK6
|
2013-12-20 16:37:05 +00:00
|
|
|
-----END RSA PRIVATE KEY-----`
|
2013-07-17 17:33:16 +01:00
|
|
|
|
2013-12-20 16:37:05 +00:00
|
|
|
const clientECDSACertificatePEM = `
|
2013-07-17 17:33:16 +01:00
|
|
|
-----BEGIN CERTIFICATE-----
|
|
|
|
MIIB/DCCAV4CCQCaMIRsJjXZFzAJBgcqhkjOPQQBMEUxCzAJBgNVBAYTAkFVMRMw
|
|
|
|
EQYDVQQIEwpTb21lLVN0YXRlMSEwHwYDVQQKExhJbnRlcm5ldCBXaWRnaXRzIFB0
|
|
|
|
eSBMdGQwHhcNMTIxMTE0MTMyNTUzWhcNMjIxMTEyMTMyNTUzWjBBMQswCQYDVQQG
|
|
|
|
EwJBVTEMMAoGA1UECBMDTlNXMRAwDgYDVQQHEwdQeXJtb250MRIwEAYDVQQDEwlK
|
|
|
|
b2VsIFNpbmcwgZswEAYHKoZIzj0CAQYFK4EEACMDgYYABACVjJF1FMBexFe01MNv
|
|
|
|
ja5oHt1vzobhfm6ySD6B5U7ixohLZNz1MLvT/2XMW/TdtWo+PtAd3kfDdq0Z9kUs
|
|
|
|
jLzYHQFMH3CQRnZIi4+DzEpcj0B22uCJ7B0rxE4wdihBsmKo+1vx+U56jb0JuK7q
|
|
|
|
ixgnTy5w/hOWusPTQBbNZU6sER7m8TAJBgcqhkjOPQQBA4GMADCBiAJCAOAUxGBg
|
|
|
|
C3JosDJdYUoCdFzCgbkWqD8pyDbHgf9stlvZcPE4O1BIKJTLCRpS8V3ujfK58PDa
|
|
|
|
2RU6+b0DeoeiIzXsAkIBo9SKeDUcSpoj0gq+KxAxnZxfvuiRs9oa9V2jI/Umi0Vw
|
|
|
|
jWVim34BmT0Y9hCaOGGbLlfk+syxis7iI6CH8OFnUes=
|
2013-12-20 16:37:05 +00:00
|
|
|
-----END CERTIFICATE-----`
|
2013-07-17 17:33:16 +01:00
|
|
|
|
2013-12-20 16:37:05 +00:00
|
|
|
const clientECDSAKeyPEM = `
|
2013-07-17 17:33:16 +01:00
|
|
|
-----BEGIN EC PARAMETERS-----
|
|
|
|
BgUrgQQAIw==
|
|
|
|
-----END EC PARAMETERS-----
|
|
|
|
-----BEGIN EC PRIVATE KEY-----
|
|
|
|
MIHcAgEBBEIBkJN9X4IqZIguiEVKMqeBUP5xtRsEv4HJEtOpOGLELwO53SD78Ew8
|
|
|
|
k+wLWoqizS3NpQyMtrU8JFdWfj+C57UNkOugBwYFK4EEACOhgYkDgYYABACVjJF1
|
|
|
|
FMBexFe01MNvja5oHt1vzobhfm6ySD6B5U7ixohLZNz1MLvT/2XMW/TdtWo+PtAd
|
|
|
|
3kfDdq0Z9kUsjLzYHQFMH3CQRnZIi4+DzEpcj0B22uCJ7B0rxE4wdihBsmKo+1vx
|
|
|
|
+U56jb0JuK7qixgnTy5w/hOWusPTQBbNZU6sER7m8Q==
|
2013-12-20 16:37:05 +00:00
|
|
|
-----END EC PRIVATE KEY-----`
|
|
|
|
|
|
|
|
func TestClientAuth(t *testing.T) {
|
|
|
|
var certPath, keyPath, ecdsaCertPath, ecdsaKeyPath string
|
|
|
|
|
|
|
|
if *update {
|
|
|
|
certPath = tempFile(clientCertificatePEM)
|
|
|
|
defer os.Remove(certPath)
|
|
|
|
keyPath = tempFile(clientKeyPEM)
|
|
|
|
defer os.Remove(keyPath)
|
|
|
|
ecdsaCertPath = tempFile(clientECDSACertificatePEM)
|
|
|
|
defer os.Remove(ecdsaCertPath)
|
|
|
|
ecdsaKeyPath = tempFile(clientECDSAKeyPEM)
|
|
|
|
defer os.Remove(ecdsaKeyPath)
|
|
|
|
}
|
|
|
|
|
2016-08-30 04:19:01 +01:00
|
|
|
config := testConfig.Clone()
|
2013-12-20 16:37:05 +00:00
|
|
|
config.ClientAuth = RequestClientCert
|
|
|
|
|
|
|
|
test := &serverTest{
|
|
|
|
name: "ClientAuthRequestedNotGiven",
|
|
|
|
command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "RC4-SHA"},
|
2016-06-21 15:00:41 +01:00
|
|
|
config: config,
|
2013-12-20 16:37:05 +00:00
|
|
|
}
|
|
|
|
runServerTestTLS12(t, test)
|
|
|
|
|
|
|
|
test = &serverTest{
|
|
|
|
name: "ClientAuthRequestedAndGiven",
|
|
|
|
command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "RC4-SHA", "-cert", certPath, "-key", keyPath},
|
2016-06-21 15:00:41 +01:00
|
|
|
config: config,
|
2013-12-20 16:37:05 +00:00
|
|
|
expectedPeerCerts: []string{clientCertificatePEM},
|
|
|
|
}
|
|
|
|
runServerTestTLS12(t, test)
|
|
|
|
|
|
|
|
test = &serverTest{
|
|
|
|
name: "ClientAuthRequestedAndECDSAGiven",
|
|
|
|
command: []string{"openssl", "s_client", "-no_ticket", "-cipher", "RC4-SHA", "-cert", ecdsaCertPath, "-key", ecdsaKeyPath},
|
2016-06-21 15:00:41 +01:00
|
|
|
config: config,
|
2013-12-20 16:37:05 +00:00
|
|
|
expectedPeerCerts: []string{clientECDSACertificatePEM},
|
|
|
|
}
|
|
|
|
runServerTestTLS12(t, test)
|
2013-07-17 17:33:16 +01:00
|
|
|
}
|
2013-08-29 22:18:59 +01:00
|
|
|
|
2016-05-06 17:20:12 +01:00
|
|
|
func TestSNIGivenOnFailure(t *testing.T) {
|
|
|
|
const expectedServerName = "test.testing"
|
|
|
|
|
|
|
|
clientHello := &clientHelloMsg{
|
|
|
|
vers: VersionTLS10,
|
|
|
|
cipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
|
|
|
|
compressionMethods: []uint8{compressionNone},
|
|
|
|
serverName: expectedServerName,
|
|
|
|
}
|
|
|
|
|
2016-08-30 04:19:01 +01:00
|
|
|
serverConfig := testConfig.Clone()
|
2016-05-06 17:20:12 +01:00
|
|
|
// Erase the server's cipher suites to ensure the handshake fails.
|
|
|
|
serverConfig.CipherSuites = nil
|
|
|
|
|
|
|
|
c, s := net.Pipe()
|
|
|
|
go func() {
|
|
|
|
cli := Client(c, testConfig)
|
|
|
|
cli.vers = clientHello.vers
|
|
|
|
cli.writeRecord(recordTypeHandshake, clientHello.marshal())
|
|
|
|
c.Close()
|
|
|
|
}()
|
|
|
|
hs := serverHandshakeState{
|
|
|
|
c: Server(s, serverConfig),
|
|
|
|
}
|
|
|
|
_, err := hs.readClientHello()
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Error("No error reported from server")
|
|
|
|
}
|
|
|
|
|
|
|
|
cs := hs.c.ConnectionState()
|
|
|
|
if cs.HandshakeComplete {
|
|
|
|
t.Error("Handshake registered as complete")
|
|
|
|
}
|
|
|
|
|
|
|
|
if cs.ServerName != expectedServerName {
|
|
|
|
t.Errorf("Expected ServerName of %q, but got %q", expectedServerName, cs.ServerName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-20 16:37:05 +00:00
|
|
|
func bigFromString(s string) *big.Int {
|
|
|
|
ret := new(big.Int)
|
|
|
|
ret.SetString(s, 10)
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func fromHex(s string) []byte {
|
|
|
|
b, _ := hex.DecodeString(s)
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2016-08-17 23:55:15 +01:00
|
|
|
var testRSACertificate = fromHex("3082024b308201b4a003020102020900e8f09d3fe25beaa6300d06092a864886f70d01010b0500301f310b3009060355040a1302476f3110300e06035504031307476f20526f6f74301e170d3136303130313030303030305a170d3235303130313030303030305a301a310b3009060355040a1302476f310b300906035504031302476f30819f300d06092a864886f70d010101050003818d0030818902818100db467d932e12270648bc062821ab7ec4b6a25dfe1e5245887a3647a5080d92425bc281c0be97799840fb4f6d14fd2b138bc2a52e67d8d4099ed62238b74a0b74732bc234f1d193e596d9747bf3589f6c613cc0b041d4d92b2b2423775b1c3bbd755dce2054cfa163871d1e24c4f31d1a508baab61443ed97a77562f414c852d70203010001a38193308190300e0603551d0f0101ff0404030205a0301d0603551d250416301406082b0601050507030106082b06010505070302300c0603551d130101ff0402300030190603551d0e041204109f91161f43433e49a6de6db680d79f60301b0603551d230414301280104813494d137e1631bba301d5acab6e7b30190603551d1104123010820e6578616d706c652e676f6c616e67300d06092a864886f70d01010b0500038181009d30cc402b5b50a061cbbae55358e1ed8328a9581aa938a495a1ac315a1a84663d43d32dd90bf297dfd320643892243a00bccf9c7db74020015faad3166109a276fd13c3cce10c5ceeb18782f16c04ed73bbb343778d0c1cf10fa1d8408361c94c722b9daedb4606064df4c1b33ec0d1bd42d4dbfe3d1360845c21d33be9fae7")
|
2015-08-05 18:55:41 +01:00
|
|
|
|
2016-08-17 23:55:15 +01:00
|
|
|
var testRSACertificateIssuer = fromHex("3082021930820182a003020102020900ca5e4e811a965964300d06092a864886f70d01010b0500301f310b3009060355040a1302476f3110300e06035504031307476f20526f6f74301e170d3136303130313030303030305a170d3235303130313030303030305a301f310b3009060355040a1302476f3110300e06035504031307476f20526f6f7430819f300d06092a864886f70d010101050003818d0030818902818100d667b378bb22f34143b6cd2008236abefaf2852adf3ab05e01329e2c14834f5105df3f3073f99dab5442d45ee5f8f57b0111c8cb682fbb719a86944eebfffef3406206d898b8c1b1887797c9c5006547bb8f00e694b7a063f10839f269f2c34fff7a1f4b21fbcd6bfdfb13ac792d1d11f277b5c5b48600992203059f2a8f8cc50203010001a35d305b300e0603551d0f0101ff040403020204301d0603551d250416301406082b0601050507030106082b06010505070302300f0603551d130101ff040530030101ff30190603551d0e041204104813494d137e1631bba301d5acab6e7b300d06092a864886f70d01010b050003818100c1154b4bab5266221f293766ae4138899bd4c5e36b13cee670ceeaa4cbdf4f6679017e2fe649765af545749fe4249418a56bd38a04b81e261f5ce86b8d5c65413156a50d12449554748c59a30c515bc36a59d38bddf51173e899820b282e40aa78c806526fd184fb6b4cf186ec728edffa585440d2b3225325f7ab580e87dd76")
|
2013-12-20 16:37:05 +00:00
|
|
|
|
|
|
|
var testECDSACertificate = fromHex("3082020030820162020900b8bf2d47a0d2ebf4300906072a8648ce3d04013045310b3009060355040613024155311330110603550408130a536f6d652d53746174653121301f060355040a1318496e7465726e6574205769646769747320507479204c7464301e170d3132313132323135303633325a170d3232313132303135303633325a3045310b3009060355040613024155311330110603550408130a536f6d652d53746174653121301f060355040a1318496e7465726e6574205769646769747320507479204c746430819b301006072a8648ce3d020106052b81040023038186000400c4a1edbe98f90b4873367ec316561122f23d53c33b4d213dcd6b75e6f6b0dc9adf26c1bcb287f072327cb3642f1c90bcea6823107efee325c0483a69e0286dd33700ef0462dd0da09c706283d881d36431aa9e9731bd96b068c09b23de76643f1a5c7fe9120e5858b65f70dd9bd8ead5d7f5d5ccb9b69f30665b669a20e227e5bffe3b300906072a8648ce3d040103818c0030818802420188a24febe245c5487d1bacf5ed989dae4770c05e1bb62fbdf1b64db76140d311a2ceee0b7e927eff769dc33b7ea53fcefa10e259ec472d7cacda4e970e15a06fd00242014dfcbe67139c2d050ebd3fa38c25c13313830d9406bbd4377af6ec7ac9862eddd711697f857c56defb31782be4c7780daecbbe9e4e3624317b6a0f399512078f2a")
|
|
|
|
|
2016-08-17 23:55:15 +01:00
|
|
|
var testSNICertificate = fromHex("0441883421114c81480804c430820237308201a0a003020102020900e8f09d3fe25beaa6300d06092a864886f70d01010b0500301f310b3009060355040a1302476f3110300e06035504031307476f20526f6f74301e170d3136303130313030303030305a170d3235303130313030303030305a3023310b3009060355040a1302476f311430120603550403130b736e69746573742e636f6d30819f300d06092a864886f70d010101050003818d0030818902818100db467d932e12270648bc062821ab7ec4b6a25dfe1e5245887a3647a5080d92425bc281c0be97799840fb4f6d14fd2b138bc2a52e67d8d4099ed62238b74a0b74732bc234f1d193e596d9747bf3589f6c613cc0b041d4d92b2b2423775b1c3bbd755dce2054cfa163871d1e24c4f31d1a508baab61443ed97a77562f414c852d70203010001a3773075300e0603551d0f0101ff0404030205a0301d0603551d250416301406082b0601050507030106082b06010505070302300c0603551d130101ff0402300030190603551d0e041204109f91161f43433e49a6de6db680d79f60301b0603551d230414301280104813494d137e1631bba301d5acab6e7b300d06092a864886f70d01010b0500038181007beeecff0230dbb2e7a334af65430b7116e09f327c3bbf918107fc9c66cb497493207ae9b4dbb045cb63d605ec1b5dd485bb69124d68fa298dc776699b47632fd6d73cab57042acb26f083c4087459bc5a3bb3ca4d878d7fe31016b7bc9a627438666566e3389bfaeebe6becc9a0093ceed18d0f9ac79d56f3a73f18188988ed")
|
2013-12-20 16:37:05 +00:00
|
|
|
|
|
|
|
var testRSAPrivateKey = &rsa.PrivateKey{
|
|
|
|
PublicKey: rsa.PublicKey{
|
2016-08-17 23:55:15 +01:00
|
|
|
N: bigFromString("153980389784927331788354528594524332344709972855165340650588877572729725338415474372475094155672066328274535240275856844648695200875763869073572078279316458648124537905600131008790701752441155668003033945258023841165089852359980273279085783159654751552359397986180318708491098942831252291841441726305535546071"),
|
2013-12-20 16:37:05 +00:00
|
|
|
E: 65537,
|
2013-08-29 22:18:59 +01:00
|
|
|
},
|
2016-08-17 23:55:15 +01:00
|
|
|
D: bigFromString("7746362285745539358014631136245887418412633787074173796862711588221766398229333338511838891484974940633857861775630560092874987828057333663969469797013996401149696897591265769095952887917296740109742927689053276850469671231961384712725169432413343763989564437170644270643461665184965150423819594083121075825"),
|
2013-12-20 16:37:05 +00:00
|
|
|
Primes: []*big.Int{
|
2016-08-17 23:55:15 +01:00
|
|
|
bigFromString("13299275414352936908236095374926261633419699590839189494995965049151460173257838079863316944311313904000258169883815802963543635820059341150014695560313417"),
|
|
|
|
bigFromString("11578103692682951732111718237224894755352163854919244905974423810539077224889290605729035287537520656160688625383765857517518932447378594964220731750802463"),
|
2013-08-29 22:18:59 +01:00
|
|
|
},
|
2013-12-20 16:37:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var testECDSAPrivateKey = &ecdsa.PrivateKey{
|
|
|
|
PublicKey: ecdsa.PublicKey{
|
|
|
|
Curve: elliptic.P521(),
|
|
|
|
X: bigFromString("2636411247892461147287360222306590634450676461695221912739908880441342231985950069527906976759812296359387337367668045707086543273113073382714101597903639351"),
|
|
|
|
Y: bigFromString("3204695818431246682253994090650952614555094516658732116404513121125038617915183037601737180082382202488628239201196033284060130040574800684774115478859677243"),
|
2013-08-29 22:18:59 +01:00
|
|
|
},
|
2013-12-20 16:37:05 +00:00
|
|
|
D: bigFromString("5477294338614160138026852784385529180817726002953041720191098180813046231640184669647735805135001309477695746518160084669446643325196003346204701381388769751"),
|
2013-08-29 22:18:59 +01:00
|
|
|
}
|