2018-07-03 17:57:54 +01:00
|
|
|
// Copyright 2018 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.
|
|
|
|
|
2019-05-12 18:42:15 +01:00
|
|
|
package trs
|
2018-07-03 17:57:54 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto"
|
|
|
|
"crypto/x509"
|
2018-07-20 02:44:03 +01:00
|
|
|
"encoding/asn1"
|
|
|
|
"encoding/pem"
|
2018-08-09 19:47:38 +01:00
|
|
|
"errors"
|
2018-07-03 17:57:54 +01:00
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2018-07-20 17:47:24 +01:00
|
|
|
// A PEM-encoded "delegation certificate", an X.509 certificate with the
|
|
|
|
// DelegationUsage extension. The extension is defined in
|
2018-08-09 19:24:40 +01:00
|
|
|
// specified in https://tools.ietf.org/html/draft-ietf-tls-subcerts-02.
|
2018-08-09 20:47:50 +01:00
|
|
|
const DcCertWithDelegationUsage = `-----BEGIN CERTIFICATE-----
|
2018-08-09 19:24:40 +01:00
|
|
|
MIIBejCCASGgAwIBAgIQXXtl0v50W2OadoW0QwLUlzAKBggqhkjOPQQDAjAUMRIw
|
|
|
|
EAYDVQQKEwlBY21lIEluYy4wHhcNMTgwNzMwMjAxMTE5WhcNMTgwODA2MjAxMTE5
|
|
|
|
WjAUMRIwEAYDVQQKEwlBY21lIEluYy4wWTATBgcqhkjOPQIBBggqhkjOPQMBBwNC
|
|
|
|
AATcQuuaUNJ3kqKGs4DBdJVd7zWzyGANT4uBNGVkZ2cgaDsdFnx99fGibfgoWer8
|
|
|
|
HLt9Z+S6Hs+8bDPBHNgTR/Lfo1UwUzAOBgNVHQ8BAf8EBAMCB4AwEwYDVR0lBAww
|
|
|
|
CgYIKwYBBQUHAwEwDAYDVR0TAQH/BAIwADAPBgNVHREECDAGhwR/AAABMA0GCSsG
|
|
|
|
AQQBgtpLLAQAMAoGCCqGSM49BAMCA0cAMEQCIEMdIkwwmzQAJ6RSDT3wcrsySx2B
|
|
|
|
5Lvx5HGzc43Fgu9eAiAi4sFXnizFBVUL43qXZBq4ARw17o0JW3/7eec1xttQhw==
|
2018-07-03 17:57:54 +01:00
|
|
|
-----END CERTIFICATE-----
|
|
|
|
`
|
|
|
|
|
2018-07-20 17:47:24 +01:00
|
|
|
// The PEM-encoded "delegation key", the secret key associated with the
|
2018-08-09 19:24:40 +01:00
|
|
|
// delegation certificate. This is a key for ECDSA with P256 and SHA256.
|
2018-08-09 20:47:50 +01:00
|
|
|
const DcKeyWithDelegationUsage = `-----BEGIN EC PRIVATE KEY-----
|
2018-08-09 19:24:40 +01:00
|
|
|
MHcCAQEEIAS/pGktmxK1hlt3gF4N2nkMrJnoZihvOO63nnNcxXQroAoGCCqGSM49
|
|
|
|
AwEHoUQDQgAE3ELrmlDSd5KihrOAwXSVXe81s8hgDU+LgTRlZGdnIGg7HRZ8ffXx
|
|
|
|
om34KFnq/By7fWfkuh7PvGwzwRzYE0fy3w==
|
2018-07-03 17:57:54 +01:00
|
|
|
-----END EC PRIVATE KEY-----
|
|
|
|
`
|
|
|
|
|
2018-07-20 17:47:24 +01:00
|
|
|
// A certificate without the DelegationUsage extension.
|
2018-08-09 20:47:50 +01:00
|
|
|
const DcCertWithoutDelegationUsage = `-----BEGIN CERTIFICATE-----
|
2018-08-09 19:24:40 +01:00
|
|
|
MIIBajCCAQ+gAwIBAgIRAMUg/VFqJaWWJwZ9iHoMjqIwCgYIKoZIzj0EAwIwEjEQ
|
|
|
|
MA4GA1UEChMHQWNtZSBDbzAeFw0xODA3MzAyMDExMTlaFw0xOTA3MzAyMDExMTla
|
|
|
|
MBIxEDAOBgNVBAoTB0FjbWUgQ28wWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAATA
|
|
|
|
n+oeWSvSNHhEskSRgkkerCQDoV/NA+r3S5AtCOFT5AYLt8xltSTWerFI/YlZLIcL
|
|
|
|
xlJPT7T+XpBnfS6xaAuxo0YwRDAOBgNVHQ8BAf8EBAMCBaAwEwYDVR0lBAwwCgYI
|
|
|
|
KwYBBQUHAwEwDAYDVR0TAQH/BAIwADAPBgNVHREECDAGhwR/AAABMAoGCCqGSM49
|
|
|
|
BAMCA0kAMEYCIQCFGWnoJmwH1rxNCKBJWVDBKDTSsYhySRk4h9RPyR8bUwIhAJxc
|
|
|
|
KFyrowMTan791RJnyANH/4uYhmvkfhfrFGSTXUli
|
2018-07-03 17:57:54 +01:00
|
|
|
-----END CERTIFICATE-----
|
|
|
|
`
|
|
|
|
|
2018-08-09 20:47:50 +01:00
|
|
|
// The secret key associatted with DcCertWithoutDelegationUsage.
|
|
|
|
const DcKeyWithoutDelegationUsage = `-----BEGIN EC PRIVATE KEY-----
|
2018-08-09 19:24:40 +01:00
|
|
|
MHcCAQEEIEP82pOhzx0tKkky9t0OmUo9MHgmfdAHxDN2cHmWGqOhoAoGCCqGSM49
|
|
|
|
AwEHoUQDQgAEwJ/qHlkr0jR4RLJEkYJJHqwkA6FfzQPq90uQLQjhU+QGC7fMZbUk
|
|
|
|
1nqxSP2JWSyHC8ZST0+0/l6QZ30usWgLsQ==
|
2018-07-03 17:57:54 +01:00
|
|
|
-----END EC PRIVATE KEY-----
|
|
|
|
`
|
|
|
|
|
2018-07-20 17:47:24 +01:00
|
|
|
// dcTestDC stores delegated credentials and their secret keys.
|
|
|
|
type dcTestDC struct {
|
|
|
|
Name string
|
|
|
|
Version int
|
|
|
|
Scheme int
|
|
|
|
DC []byte
|
|
|
|
PrivateKey []byte
|
|
|
|
}
|
|
|
|
|
2018-08-09 19:47:38 +01:00
|
|
|
// Use with maxVersion == VersionTLS13.
|
2018-08-09 20:47:50 +01:00
|
|
|
const DcTestDataTLS13PEM = `-----BEGIN DC TEST DATA-----
|
2018-08-10 08:34:10 +01:00
|
|
|
MIIIQzCCAUMTCXRsczEzcDI1NgICAwQCAgQDBIGyAAk6gAQDAwQAAFswWTATBgcq
|
|
|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
|
|
|
// Parses the input PEM block containing the test DCs.
|
|
|
|
func dcLoadTestData(in []byte, out *[]dcTestDC) error {
|
|
|
|
block, _ := pem.Decode(in)
|
|
|
|
if block == nil {
|
|
|
|
return errors.New("failed to decode DC tests PEM block")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the DER-encoded test DCs.
|
|
|
|
_, err := asn1.Unmarshal(block.Bytes, out)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("failed to unmarshal DC test ASN.1 data")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the test data is for the right version. This should be
|
|
|
|
// maxVersion, defined in common.go.
|
|
|
|
for _, test := range *out {
|
|
|
|
dc, err := unmarshalDelegatedCredential(test.DC)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sanity check that test version matches the version encoded by the DC.
|
|
|
|
testVersion := uint16(test.Version)
|
|
|
|
if dc.cred.expectedVersion != testVersion {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"test version doesn't match credential version: got: 0x0%04x; want: 0x%04x",
|
|
|
|
testVersion, dc.cred.expectedVersion)
|
|
|
|
}
|
|
|
|
|
|
|
|
// With the exception of "badvers" and "tsl12", all test DCs should have
|
|
|
|
// the expected verison.
|
|
|
|
if test.Name != "badvers" && test.Name != "tls12" && testVersion != maxVersion {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"encountered test with wrong version: got: 0x0%04x; want: 0x%04x",
|
|
|
|
test.Version, maxVersion)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-20 02:44:03 +01:00
|
|
|
var dcTestDCs []dcTestDC
|
|
|
|
var dcTestConfig *Config
|
|
|
|
var dcTestDelegationCert Certificate
|
|
|
|
var dcTestCert Certificate
|
|
|
|
var dcTestNow time.Time
|
|
|
|
|
2018-07-03 17:57:54 +01:00
|
|
|
func init() {
|
2018-08-09 19:47:38 +01:00
|
|
|
// Load the DC test data.
|
|
|
|
var testData []byte
|
2018-09-16 08:36:45 +01:00
|
|
|
if maxVersion != 0x0304 {
|
2018-08-09 19:47:38 +01:00
|
|
|
panic(fmt.Errorf("no test data for version %04x", maxVersion))
|
2018-07-20 02:44:03 +01:00
|
|
|
}
|
2018-09-16 08:36:45 +01:00
|
|
|
testData = []byte(DcTestDataTLS13PEM)
|
|
|
|
|
2018-08-09 19:47:38 +01:00
|
|
|
err := dcLoadTestData(testData, &dcTestDCs)
|
2018-07-20 02:44:03 +01:00
|
|
|
if err != nil {
|
2018-08-09 19:47:38 +01:00
|
|
|
panic(err)
|
2018-07-20 02:44:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// The base configuration for the client and server.
|
2018-07-03 17:57:54 +01:00
|
|
|
dcTestConfig = &Config{
|
|
|
|
Time: func() time.Time {
|
2018-07-20 02:44:03 +01:00
|
|
|
return dcTestNow
|
2018-07-03 17:57:54 +01:00
|
|
|
},
|
|
|
|
Rand: zeroSource{},
|
|
|
|
Certificates: nil,
|
|
|
|
MinVersion: VersionTLS10,
|
2018-08-07 18:52:06 +01:00
|
|
|
MaxVersion: VersionTLS13,
|
2018-07-03 17:57:54 +01:00
|
|
|
CipherSuites: allCipherSuites(),
|
|
|
|
}
|
|
|
|
|
|
|
|
// The delegation certificate.
|
2018-08-09 20:47:50 +01:00
|
|
|
dcTestDelegationCert, err = X509KeyPair([]byte(DcCertWithDelegationUsage), []byte(DcKeyWithDelegationUsage))
|
2018-07-03 17:57:54 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-07-20 02:44:03 +01:00
|
|
|
dcTestDelegationCert.Leaf, err = x509.ParseCertificate(dcTestDelegationCert.Certificate[0])
|
2018-07-03 17:57:54 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2018-07-20 02:44:03 +01:00
|
|
|
// A certificate without the the DelegationUsage extension for X.509.
|
2018-08-09 20:47:50 +01:00
|
|
|
dcTestCert, err = X509KeyPair([]byte(DcCertWithoutDelegationUsage), []byte(DcKeyWithoutDelegationUsage))
|
2018-07-03 17:57:54 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-07-20 02:44:03 +01:00
|
|
|
dcTestCert.Leaf, err = x509.ParseCertificate(dcTestCert.Certificate[0])
|
2018-07-03 17:57:54 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2018-08-09 19:24:40 +01:00
|
|
|
// For testing purposes, use the point at which the test DCs were generated
|
|
|
|
// as the current time. This is the same as the time at which the
|
|
|
|
// delegation certificate was generated.
|
|
|
|
dcTestNow = dcTestDelegationCert.Leaf.NotBefore
|
|
|
|
|
2018-07-20 02:44:03 +01:00
|
|
|
// Make these roots of these certificates the client's trusted CAs.
|
2018-07-03 17:57:54 +01:00
|
|
|
dcTestConfig.RootCAs = x509.NewCertPool()
|
|
|
|
|
2018-07-20 02:44:03 +01:00
|
|
|
raw := dcTestDelegationCert.Certificate[len(dcTestDelegationCert.Certificate)-1]
|
|
|
|
root, err := x509.ParseCertificate(raw)
|
2018-07-03 17:57:54 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-07-20 02:44:03 +01:00
|
|
|
dcTestConfig.RootCAs.AddCert(root)
|
2018-07-03 17:57:54 +01:00
|
|
|
|
2018-07-20 02:44:03 +01:00
|
|
|
raw = dcTestCert.Certificate[len(dcTestCert.Certificate)-1]
|
|
|
|
root, err = x509.ParseCertificate(raw)
|
2018-07-03 17:57:54 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-07-20 02:44:03 +01:00
|
|
|
dcTestConfig.RootCAs.AddCert(root)
|
2018-07-03 17:57:54 +01:00
|
|
|
}
|
|
|
|
|
2018-07-20 17:47:24 +01:00
|
|
|
// Executes the handshake with the given configuration and returns true if the
|
|
|
|
// delegated credential extension was successfully negotiated.
|
|
|
|
func testConnWithDC(t *testing.T, clientConfig, serverConfig *Config) (bool, error) {
|
2018-07-03 17:57:54 +01:00
|
|
|
ln := newLocalListener(t)
|
|
|
|
defer ln.Close()
|
|
|
|
|
2018-07-20 02:44:03 +01:00
|
|
|
// Listen for and serve a single client connection.
|
2018-07-03 17:57:54 +01:00
|
|
|
srvCh := make(chan *Conn, 1)
|
|
|
|
var serr error
|
|
|
|
go func() {
|
|
|
|
sconn, err := ln.Accept()
|
|
|
|
if err != nil {
|
|
|
|
serr = err
|
|
|
|
srvCh <- nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
srv := Server(sconn, serverConfig)
|
|
|
|
if err := srv.Handshake(); err != nil {
|
|
|
|
serr = fmt.Errorf("handshake: %v", err)
|
|
|
|
srvCh <- nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
srvCh <- srv
|
|
|
|
}()
|
|
|
|
|
2018-07-20 02:44:03 +01:00
|
|
|
// Dial the server.
|
2018-07-03 17:57:54 +01:00
|
|
|
cli, err := Dial("tcp", ln.Addr().String(), clientConfig)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
defer cli.Close()
|
|
|
|
|
|
|
|
srv := <-srvCh
|
|
|
|
if srv == nil {
|
|
|
|
return false, serr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return true if the client's conn.dc structure was instantiated.
|
2018-07-20 17:47:24 +01:00
|
|
|
st := cli.ConnectionState()
|
|
|
|
return (st.DelegatedCredential != nil), nil
|
2018-07-03 17:57:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Checks that the client suppports a version >= 1.2 and accepts delegated
|
|
|
|
// credentials. If so, it returns the delegation certificate; otherwise it
|
|
|
|
// returns a plain certificate.
|
|
|
|
func testServerGetCertificate(ch *ClientHelloInfo) (*Certificate, error) {
|
|
|
|
versOk := false
|
|
|
|
for _, vers := range ch.SupportedVersions {
|
|
|
|
versOk = versOk || (vers >= uint16(VersionTLS12))
|
|
|
|
}
|
|
|
|
|
|
|
|
if versOk && ch.AcceptsDelegatedCredential {
|
2018-07-20 02:44:03 +01:00
|
|
|
return &dcTestDelegationCert, nil
|
2018-07-03 17:57:54 +01:00
|
|
|
}
|
2018-07-20 02:44:03 +01:00
|
|
|
return &dcTestCert, nil
|
2018-07-03 17:57:54 +01:00
|
|
|
}
|
|
|
|
|
2018-07-20 02:44:03 +01:00
|
|
|
// Various test cases for handshakes involving DCs.
|
2018-07-20 17:47:24 +01:00
|
|
|
var dcTesters = []struct {
|
2018-07-03 17:57:54 +01:00
|
|
|
clientDC bool
|
|
|
|
serverDC bool
|
|
|
|
clientSkipVerify bool
|
|
|
|
clientMaxVers uint16
|
|
|
|
serverMaxVers uint16
|
2018-07-20 02:44:03 +01:00
|
|
|
nowOffset time.Duration
|
|
|
|
dcTestName string
|
2018-07-03 17:57:54 +01:00
|
|
|
expectSuccess bool
|
|
|
|
expectDC bool
|
|
|
|
name string
|
|
|
|
}{
|
2018-08-09 19:24:40 +01:00
|
|
|
{true, true, false, VersionTLS13, VersionTLS13, 0, "tls13p256", true, true, "tls13"},
|
|
|
|
{true, true, false, VersionTLS13, VersionTLS13, 0, "tls13p521", true, true, "tls13"},
|
|
|
|
{true, false, false, VersionTLS13, VersionTLS13, 0, "tls13p256", true, false, "server no dc"},
|
|
|
|
{true, true, false, VersionTLS12, VersionTLS13, 0, "tls13p256", true, false, "client old"},
|
|
|
|
{true, true, false, VersionTLS13, VersionTLS12, 0, "tls13p256", true, false, "server old"},
|
|
|
|
{true, true, false, VersionTLS13, VersionTLS13, 0, "badkey", false, false, "bad key"},
|
|
|
|
{true, true, true, VersionTLS13, VersionTLS13, 0, "badsig", true, true, "bad key, skip verify"},
|
2018-07-20 02:44:03 +01:00
|
|
|
{true, true, false, VersionTLS13, VersionTLS13, dcMaxTTL, "tls13", false, false, "expired dc"},
|
2018-08-09 19:24:40 +01:00
|
|
|
{true, true, false, VersionTLS13, VersionTLS13, 0, "badvers", false, false, "dc wrong version"},
|
|
|
|
{true, true, false, VersionTLS12, VersionTLS12, 0, "tls12", true, false, "tls12"},
|
2018-07-03 17:57:54 +01:00
|
|
|
}
|
|
|
|
|
2018-07-20 02:44:03 +01:00
|
|
|
// Tests the handshake with the delegated credential extension for each test
|
|
|
|
// case in dcTests.
|
2018-07-03 17:57:54 +01:00
|
|
|
func TestDCHandshake(t *testing.T) {
|
|
|
|
clientConfig := dcTestConfig.Clone()
|
|
|
|
serverConfig := dcTestConfig.Clone()
|
|
|
|
serverConfig.GetCertificate = testServerGetCertificate
|
|
|
|
|
2018-07-20 17:47:24 +01:00
|
|
|
for i, tester := range dcTesters {
|
|
|
|
clientConfig.MaxVersion = tester.clientMaxVers
|
|
|
|
serverConfig.MaxVersion = tester.serverMaxVers
|
|
|
|
clientConfig.InsecureSkipVerify = tester.clientSkipVerify
|
|
|
|
clientConfig.AcceptDelegatedCredential = tester.clientDC
|
2018-07-20 02:44:03 +01:00
|
|
|
clientConfig.Time = func() time.Time {
|
2018-07-20 17:47:24 +01:00
|
|
|
return dcTestNow.Add(time.Duration(tester.nowOffset))
|
2018-07-20 02:44:03 +01:00
|
|
|
}
|
2018-07-03 17:57:54 +01:00
|
|
|
|
2018-07-20 17:47:24 +01:00
|
|
|
if tester.serverDC {
|
2018-07-20 02:44:03 +01:00
|
|
|
serverConfig.GetDelegatedCredential = func(
|
2018-07-20 17:47:24 +01:00
|
|
|
ch *ClientHelloInfo, vers uint16) ([]byte, crypto.PrivateKey, error) {
|
2018-08-09 19:24:40 +01:00
|
|
|
if vers < VersionTLS13 {
|
|
|
|
return nil, nil, nil
|
|
|
|
}
|
2018-07-20 17:47:24 +01:00
|
|
|
for _, test := range dcTestDCs {
|
|
|
|
if test.Name == tester.dcTestName {
|
|
|
|
sk, err := x509.ParseECPrivateKey(test.PrivateKey)
|
2018-07-20 02:44:03 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2018-07-20 17:47:24 +01:00
|
|
|
return test.DC, sk, nil
|
2018-07-20 02:44:03 +01:00
|
|
|
}
|
|
|
|
}
|
2018-07-20 17:47:24 +01:00
|
|
|
return nil, nil, fmt.Errorf("Test DC with name '%s' not found", tester.dcTestName)
|
2018-07-03 17:57:54 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
serverConfig.GetDelegatedCredential = nil
|
|
|
|
}
|
|
|
|
|
2018-07-20 17:47:24 +01:00
|
|
|
usedDC, err := testConnWithDC(t, clientConfig, serverConfig)
|
|
|
|
if err != nil && tester.expectSuccess {
|
|
|
|
t.Errorf("test #%d (%s) fails: %s", i+1, tester.name, err)
|
|
|
|
} else if err == nil && !tester.expectSuccess {
|
|
|
|
t.Errorf("test #%d (%s) succeeds; expected failure", i+1, tester.name)
|
2018-07-03 17:57:54 +01:00
|
|
|
}
|
|
|
|
|
2018-07-20 17:47:24 +01:00
|
|
|
if usedDC != tester.expectDC {
|
|
|
|
t.Errorf("test #%d (%s) usedDC = %v; expected %v", i+1, tester.name, usedDC, tester.expectDC)
|
2018-07-03 17:57:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|