| 1 |
// Copyright 2017 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 |
"crypto" |
| 9 |
"crypto/tls/internal/fips140tls" |
| 10 |
"os" |
| 11 |
"testing" |
| 12 |
) |
| 13 |
|
| 14 |
func TestSignatureSelection(t *testing.T) { |
| 15 |
rsaCert := &Certificate{ |
| 16 |
Certificate: [][]byte{testRSACertificate}, |
| 17 |
PrivateKey: testRSAPrivateKey, |
| 18 |
} |
| 19 |
pkcs1Cert := &Certificate{ |
| 20 |
Certificate: [][]byte{testRSACertificate}, |
| 21 |
PrivateKey: testRSAPrivateKey, |
| 22 |
SupportedSignatureAlgorithms: []SignatureScheme{PKCS1WithSHA1, PKCS1WithSHA256}, |
| 23 |
} |
| 24 |
ecdsaCert := &Certificate{ |
| 25 |
Certificate: [][]byte{testP256Certificate}, |
| 26 |
PrivateKey: testP256PrivateKey, |
| 27 |
} |
| 28 |
ed25519Cert := &Certificate{ |
| 29 |
Certificate: [][]byte{testEd25519Certificate}, |
| 30 |
PrivateKey: testEd25519PrivateKey, |
| 31 |
} |
| 32 |
|
| 33 |
tests := []struct { |
| 34 |
cert *Certificate |
| 35 |
peerSigAlgs []SignatureScheme |
| 36 |
tlsVersion uint16 |
| 37 |
godebug string |
| 38 |
|
| 39 |
expectedSigAlg SignatureScheme |
| 40 |
expectedSigType uint8 |
| 41 |
expectedHash crypto.Hash |
| 42 |
}{ |
| 43 |
{rsaCert, []SignatureScheme{PKCS1WithSHA1, PKCS1WithSHA256}, VersionTLS12, "", PKCS1WithSHA256, signaturePKCS1v15, crypto.SHA256}, |
| 44 |
{rsaCert, []SignatureScheme{PKCS1WithSHA1, PKCS1WithSHA256}, VersionTLS12, "tlssha1=1", PKCS1WithSHA1, signaturePKCS1v15, crypto.SHA1}, |
| 45 |
{rsaCert, []SignatureScheme{PKCS1WithSHA512, PKCS1WithSHA1}, VersionTLS12, "", PKCS1WithSHA512, signaturePKCS1v15, crypto.SHA512}, |
| 46 |
{rsaCert, []SignatureScheme{PSSWithSHA256, PKCS1WithSHA256}, VersionTLS12, "", PSSWithSHA256, signatureRSAPSS, crypto.SHA256}, |
| 47 |
{pkcs1Cert, []SignatureScheme{PSSWithSHA256, PKCS1WithSHA256}, VersionTLS12, "", PKCS1WithSHA256, signaturePKCS1v15, crypto.SHA256}, |
| 48 |
{rsaCert, []SignatureScheme{PSSWithSHA384, PKCS1WithSHA1}, VersionTLS13, "", PSSWithSHA384, signatureRSAPSS, crypto.SHA384}, |
| 49 |
{rsaCert, []SignatureScheme{PKCS1WithSHA1, PSSWithSHA384}, VersionTLS13, "", PSSWithSHA384, signatureRSAPSS, crypto.SHA384}, |
| 50 |
{ecdsaCert, []SignatureScheme{ECDSAWithSHA1, ECDSAWithP256AndSHA256}, VersionTLS12, "", ECDSAWithP256AndSHA256, signatureECDSA, crypto.SHA256}, |
| 51 |
{ecdsaCert, []SignatureScheme{ECDSAWithSHA1}, VersionTLS12, "tlssha1=1", ECDSAWithSHA1, signatureECDSA, crypto.SHA1}, |
| 52 |
{ecdsaCert, []SignatureScheme{ECDSAWithP256AndSHA256}, VersionTLS12, "", ECDSAWithP256AndSHA256, signatureECDSA, crypto.SHA256}, |
| 53 |
{ecdsaCert, []SignatureScheme{ECDSAWithP256AndSHA256}, VersionTLS13, "", ECDSAWithP256AndSHA256, signatureECDSA, crypto.SHA256}, |
| 54 |
{ed25519Cert, []SignatureScheme{Ed25519}, VersionTLS12, "", Ed25519, signatureEd25519, directSigning}, |
| 55 |
{ed25519Cert, []SignatureScheme{Ed25519}, VersionTLS13, "", Ed25519, signatureEd25519, directSigning}, |
| 56 |
|
| 57 |
// TLS 1.2 without signature_algorithms extension |
| 58 |
{rsaCert, nil, VersionTLS12, "tlssha1=1", PKCS1WithSHA1, signaturePKCS1v15, crypto.SHA1}, |
| 59 |
{ecdsaCert, nil, VersionTLS12, "tlssha1=1", ECDSAWithSHA1, signatureECDSA, crypto.SHA1}, |
| 60 |
|
| 61 |
// TLS 1.2 does not restrict the ECDSA curve (our ecdsaCert is P-256) |
| 62 |
{ecdsaCert, []SignatureScheme{ECDSAWithP384AndSHA384}, VersionTLS12, "", ECDSAWithP384AndSHA384, signatureECDSA, crypto.SHA384}, |
| 63 |
} |
| 64 |
|
| 65 |
for testNo, test := range tests { |
| 66 |
if fips140tls.Required() && test.expectedHash == crypto.SHA1 { |
| 67 |
t.Logf("skipping test[%d] - not compatible with TLS FIPS mode", testNo) |
| 68 |
continue |
| 69 |
} |
| 70 |
savedGODEBUG := os.Getenv("GODEBUG") |
| 71 |
os.Setenv("GODEBUG", savedGODEBUG+","+test.godebug) |
| 72 |
|
| 73 |
sigAlg, err := selectSignatureScheme(test.tlsVersion, test.cert, test.peerSigAlgs) |
| 74 |
if err != nil { |
| 75 |
t.Errorf("test[%d]: unexpected selectSignatureScheme error: %v", testNo, err) |
| 76 |
} |
| 77 |
if test.expectedSigAlg != sigAlg { |
| 78 |
t.Errorf("test[%d]: expected signature scheme %v, got %v", testNo, test.expectedSigAlg, sigAlg) |
| 79 |
} |
| 80 |
sigType, hashFunc, err := typeAndHashFromSignatureScheme(sigAlg) |
| 81 |
if err != nil { |
| 82 |
t.Errorf("test[%d]: unexpected typeAndHashFromSignatureScheme error: %v", testNo, err) |
| 83 |
} |
| 84 |
if test.expectedSigType != sigType { |
| 85 |
t.Errorf("test[%d]: expected signature algorithm %#x, got %#x", testNo, test.expectedSigType, sigType) |
| 86 |
} |
| 87 |
if test.expectedHash != hashFunc { |
| 88 |
t.Errorf("test[%d]: expected hash function %#x, got %#x", testNo, test.expectedHash, hashFunc) |
| 89 |
} |
| 90 |
|
| 91 |
os.Setenv("GODEBUG", savedGODEBUG) |
| 92 |
} |
| 93 |
|
| 94 |
brokenCert := &Certificate{ |
| 95 |
Certificate: [][]byte{testRSACertificate}, |
| 96 |
PrivateKey: testRSAPrivateKey, |
| 97 |
SupportedSignatureAlgorithms: []SignatureScheme{Ed25519}, |
| 98 |
} |
| 99 |
|
| 100 |
badTests := []struct { |
| 101 |
cert *Certificate |
| 102 |
peerSigAlgs []SignatureScheme |
| 103 |
tlsVersion uint16 |
| 104 |
}{ |
| 105 |
{rsaCert, []SignatureScheme{ECDSAWithP256AndSHA256, ECDSAWithSHA1}, VersionTLS12}, |
| 106 |
{ecdsaCert, []SignatureScheme{PKCS1WithSHA256, PKCS1WithSHA1}, VersionTLS12}, |
| 107 |
{rsaCert, []SignatureScheme{0}, VersionTLS12}, |
| 108 |
{ed25519Cert, []SignatureScheme{ECDSAWithP256AndSHA256, ECDSAWithSHA1}, VersionTLS12}, |
| 109 |
{ecdsaCert, []SignatureScheme{Ed25519}, VersionTLS12}, |
| 110 |
{brokenCert, []SignatureScheme{Ed25519}, VersionTLS12}, |
| 111 |
{brokenCert, []SignatureScheme{PKCS1WithSHA256}, VersionTLS12}, |
| 112 |
// RFC 5246, Section 7.4.1.4.1, says to only consider {sha1,ecdsa} as |
| 113 |
// default when the extension is missing, and RFC 8422 does not update |
| 114 |
// it. Anyway, if a stack supports Ed25519 it better support sigalgs. |
| 115 |
{ed25519Cert, nil, VersionTLS12}, |
| 116 |
// TLS 1.3 has no default signature_algorithms. |
| 117 |
{rsaCert, nil, VersionTLS13}, |
| 118 |
{ecdsaCert, nil, VersionTLS13}, |
| 119 |
{ed25519Cert, nil, VersionTLS13}, |
| 120 |
// Wrong curve, which TLS 1.3 checks |
| 121 |
{ecdsaCert, []SignatureScheme{ECDSAWithP384AndSHA384}, VersionTLS13}, |
| 122 |
// TLS 1.3 does not support PKCS1v1.5 or SHA-1. |
| 123 |
{rsaCert, []SignatureScheme{PKCS1WithSHA256}, VersionTLS13}, |
| 124 |
{pkcs1Cert, []SignatureScheme{PSSWithSHA256, PKCS1WithSHA256}, VersionTLS13}, |
| 125 |
{ecdsaCert, []SignatureScheme{ECDSAWithSHA1}, VersionTLS13}, |
| 126 |
// The key can be too small for the hash. |
| 127 |
{rsaCert, []SignatureScheme{PSSWithSHA512}, VersionTLS12}, |
| 128 |
// SHA-1 requires tlssha1=1 |
| 129 |
{rsaCert, []SignatureScheme{PKCS1WithSHA1}, VersionTLS12}, |
| 130 |
{ecdsaCert, []SignatureScheme{ECDSAWithSHA1}, VersionTLS12}, |
| 131 |
{rsaCert, nil, VersionTLS12}, |
| 132 |
{ecdsaCert, nil, VersionTLS12}, |
| 133 |
} |
| 134 |
|
| 135 |
for testNo, test := range badTests { |
| 136 |
sigAlg, err := selectSignatureScheme(test.tlsVersion, test.cert, test.peerSigAlgs) |
| 137 |
if err == nil { |
| 138 |
t.Errorf("test[%d]: unexpected success, got %v", testNo, sigAlg) |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
func TestLegacyTypeAndHash(t *testing.T) { |
| 144 |
sigType, hashFunc, err := legacyTypeAndHashFromPublicKey(testRSAPrivateKey.Public()) |
| 145 |
if err != nil { |
| 146 |
t.Errorf("RSA: unexpected error: %v", err) |
| 147 |
} |
| 148 |
if expectedSigType := signaturePKCS1v15; expectedSigType != sigType { |
| 149 |
t.Errorf("RSA: expected signature type %#x, got %#x", expectedSigType, sigType) |
| 150 |
} |
| 151 |
if expectedHashFunc := crypto.MD5SHA1; expectedHashFunc != hashFunc { |
| 152 |
t.Errorf("RSA: expected hash %#x, got %#x", expectedHashFunc, hashFunc) |
| 153 |
} |
| 154 |
|
| 155 |
sigType, hashFunc, err = legacyTypeAndHashFromPublicKey(testECDSAPrivateKey.Public()) |
| 156 |
if err != nil { |
| 157 |
t.Errorf("ECDSA: unexpected error: %v", err) |
| 158 |
} |
| 159 |
if expectedSigType := signatureECDSA; expectedSigType != sigType { |
| 160 |
t.Errorf("ECDSA: expected signature type %#x, got %#x", expectedSigType, sigType) |
| 161 |
} |
| 162 |
if expectedHashFunc := crypto.SHA1; expectedHashFunc != hashFunc { |
| 163 |
t.Errorf("ECDSA: expected hash %#x, got %#x", expectedHashFunc, hashFunc) |
| 164 |
} |
| 165 |
|
| 166 |
// Ed25519 is not supported by TLS 1.0 and 1.1. |
| 167 |
_, _, err = legacyTypeAndHashFromPublicKey(testEd25519PrivateKey.Public()) |
| 168 |
if err == nil { |
| 169 |
t.Errorf("Ed25519: unexpected success") |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
// TestSupportedSignatureAlgorithms checks that all supportedSignatureAlgorithms |
| 174 |
// have valid type and hash information. |
| 175 |
func TestSupportedSignatureAlgorithms(t *testing.T) { |
| 176 |
for _, sigAlg := range supportedSignatureAlgorithms(VersionTLS12) { |
| 177 |
sigType, hash, err := typeAndHashFromSignatureScheme(sigAlg) |
| 178 |
if err != nil { |
| 179 |
t.Errorf("%v: unexpected error: %v", sigAlg, err) |
| 180 |
} |
| 181 |
if sigType == 0 { |
| 182 |
t.Errorf("%v: missing signature type", sigAlg) |
| 183 |
} |
| 184 |
if hash == 0 && sigAlg != Ed25519 { |
| 185 |
t.Errorf("%v: missing hash", sigAlg) |
| 186 |
} |
| 187 |
} |
| 188 |
} |