2021-04-12 15:39:40 +00:00
|
|
|
package diagnose
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/vault/internalshared/configutil"
|
|
|
|
"github.com/hashicorp/vault/internalshared/listenerutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TestTLSValidCert is the positive test case to show that specifying a valid cert and key
|
|
|
|
// passes all checks.
|
|
|
|
func TestTLSValidCert(t *testing.T) {
|
|
|
|
listeners := []listenerutil.Listener{
|
|
|
|
{
|
|
|
|
Config: &configutil.Listener{
|
|
|
|
Type: "tcp",
|
|
|
|
Address: "127.0.0.1:443",
|
|
|
|
ClusterAddress: "127.0.0.1:8201",
|
|
|
|
TLSCertFile: "./test-fixtures/goodcertwithroot.pem",
|
|
|
|
TLSKeyFile: "./test-fixtures/goodkey.pem",
|
|
|
|
TLSMinVersion: "tls10",
|
|
|
|
TLSRequireAndVerifyClientCert: true,
|
|
|
|
TLSDisableClientCerts: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := ListenerChecks(listeners)
|
|
|
|
if err != nil {
|
2021-04-12 17:55:33 +00:00
|
|
|
t.Fatalf(err.Error())
|
2021-04-12 15:39:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestTLSFakeCert simply ensures that the certificate file must contain PEM data.
|
|
|
|
func TestTLSFakeCert(t *testing.T) {
|
|
|
|
listeners := []listenerutil.Listener{
|
|
|
|
{
|
|
|
|
Config: &configutil.Listener{
|
|
|
|
Type: "tcp",
|
|
|
|
Address: "127.0.0.1:443",
|
|
|
|
ClusterAddress: "127.0.0.1:8201",
|
|
|
|
TLSCertFile: "./test-fixtures/fakecert.pem",
|
|
|
|
TLSKeyFile: "./../../api/test-fixtures/keys/key.pem",
|
|
|
|
TLSMinVersion: "tls10",
|
|
|
|
TLSRequireAndVerifyClientCert: true,
|
|
|
|
TLSDisableClientCerts: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := ListenerChecks(listeners)
|
|
|
|
if err == nil {
|
2021-04-12 17:55:33 +00:00
|
|
|
t.Fatalf("TLS Config check on fake certificate should fail")
|
2021-04-12 15:39:40 +00:00
|
|
|
}
|
|
|
|
if !strings.Contains(err.Error(), "could not decode cert") {
|
2021-04-12 17:55:33 +00:00
|
|
|
t.Fatalf("Bad error message: %s", err)
|
2021-04-12 15:39:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestTLSTrailingData uses a certificate from:
|
|
|
|
// https://github.com/golang/go/issues/40545 that contains
|
|
|
|
// an extra DER sequence, and makes sure a trailing data error
|
|
|
|
// is returned.
|
|
|
|
func TestTLSTrailingData(t *testing.T) {
|
|
|
|
listeners := []listenerutil.Listener{
|
|
|
|
{
|
|
|
|
Config: &configutil.Listener{
|
|
|
|
Type: "tcp",
|
|
|
|
Address: "127.0.0.1:443",
|
|
|
|
ClusterAddress: "127.0.0.1:8201",
|
|
|
|
TLSCertFile: "./test-fixtures/trailingdatacert.pem",
|
|
|
|
TLSKeyFile: "./../../api/test-fixtures/keys/key.pem",
|
|
|
|
TLSMinVersion: "tls10",
|
|
|
|
TLSRequireAndVerifyClientCert: true,
|
|
|
|
TLSDisableClientCerts: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := ListenerChecks(listeners)
|
|
|
|
if err == nil {
|
2021-04-12 17:55:33 +00:00
|
|
|
t.Fatalf("TLS Config check on fake certificate should fail")
|
2021-04-12 15:39:40 +00:00
|
|
|
}
|
|
|
|
if !strings.Contains(err.Error(), "asn1: syntax error: trailing data") {
|
2021-04-12 17:55:33 +00:00
|
|
|
t.Fatalf("Bad error message: %s", err)
|
2021-04-12 15:39:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestTLSExpiredCert checks that an expired certificate fails TLS checks
|
|
|
|
// with an appropriate error.
|
|
|
|
func TestTLSExpiredCert(t *testing.T) {
|
|
|
|
listeners := []listenerutil.Listener{
|
|
|
|
{
|
|
|
|
Config: &configutil.Listener{
|
|
|
|
Type: "tcp",
|
|
|
|
Address: "127.0.0.1:443",
|
|
|
|
ClusterAddress: "127.0.0.1:8201",
|
|
|
|
TLSCertFile: "./test-fixtures/expiredcert.pem",
|
|
|
|
TLSKeyFile: "./test-fixtures/expiredprivatekey.pem",
|
|
|
|
TLSMinVersion: "tls10",
|
|
|
|
TLSRequireAndVerifyClientCert: true,
|
|
|
|
TLSDisableClientCerts: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := ListenerChecks(listeners)
|
|
|
|
if err == nil {
|
2021-04-12 17:55:33 +00:00
|
|
|
t.Fatalf("TLS Config check on fake certificate should fail")
|
2021-04-12 15:39:40 +00:00
|
|
|
}
|
|
|
|
if !strings.Contains(err.Error(), "certificate has expired or is not yet valid") {
|
2021-04-12 17:55:33 +00:00
|
|
|
t.Fatalf("Bad error message: %s", err)
|
2021-04-12 15:39:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestTLSMismatchedCryptographicInfo verifies that a cert and key of differing cryptographic
|
|
|
|
// types, when specified together, is met with a unique error message.
|
|
|
|
func TestTLSMismatchedCryptographicInfo(t *testing.T) {
|
|
|
|
listeners := []listenerutil.Listener{
|
|
|
|
{
|
|
|
|
Config: &configutil.Listener{
|
|
|
|
Type: "tcp",
|
|
|
|
Address: "127.0.0.1:443",
|
|
|
|
ClusterAddress: "127.0.0.1:8201",
|
|
|
|
TLSCertFile: "./../../api/test-fixtures/keys/cert.pem",
|
|
|
|
TLSKeyFile: "./test-fixtures/ecdsa.key",
|
|
|
|
TLSMinVersion: "tls10",
|
|
|
|
TLSRequireAndVerifyClientCert: true,
|
|
|
|
TLSDisableClientCerts: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := ListenerChecks(listeners)
|
|
|
|
if err == nil {
|
2021-04-12 17:55:33 +00:00
|
|
|
t.Fatalf("TLS Config check on fake certificate should fail")
|
2021-04-12 15:39:40 +00:00
|
|
|
}
|
|
|
|
if err.Error() != "tls: private key type does not match public key type" {
|
2021-04-12 17:55:33 +00:00
|
|
|
t.Fatalf("Bad error message: %s", err)
|
2021-04-12 15:39:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
listeners = []listenerutil.Listener{
|
|
|
|
{
|
|
|
|
Config: &configutil.Listener{
|
|
|
|
Type: "tcp",
|
|
|
|
Address: "127.0.0.1:443",
|
|
|
|
ClusterAddress: "127.0.0.1:8201",
|
|
|
|
TLSCertFile: "./test-fixtures/ecdsa.crt",
|
|
|
|
TLSKeyFile: "./../../api/test-fixtures/keys/key.pem",
|
|
|
|
TLSClientCAFile: "./../../api/test-fixtures/root/rootcacert.pem",
|
|
|
|
TLSMinVersion: "tls10",
|
|
|
|
TLSRequireAndVerifyClientCert: true,
|
|
|
|
TLSDisableClientCerts: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = ListenerChecks(listeners)
|
|
|
|
if err == nil {
|
2021-04-12 17:55:33 +00:00
|
|
|
t.Fatalf("TLS Config check on fake certificate should fail")
|
2021-04-12 15:39:40 +00:00
|
|
|
}
|
|
|
|
if err.Error() != "tls: private key type does not match public key type" {
|
2021-04-12 17:55:33 +00:00
|
|
|
t.Fatalf("Bad error message: %s", err)
|
2021-04-12 15:39:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestTLSMultiKeys verifies that a unique error message is thrown when a key is specified twice.
|
|
|
|
func TestTLSMultiKeys(t *testing.T) {
|
|
|
|
listeners := []listenerutil.Listener{
|
|
|
|
{
|
|
|
|
Config: &configutil.Listener{
|
|
|
|
Type: "tcp",
|
|
|
|
Address: "127.0.0.1:443",
|
|
|
|
ClusterAddress: "127.0.0.1:8201",
|
|
|
|
TLSCertFile: "./../../api/test-fixtures/keys/key.pem",
|
|
|
|
TLSKeyFile: "./../../api/test-fixtures/keys/key.pem",
|
|
|
|
TLSClientCAFile: "./../../api/test-fixtures/root/rootcacert.pem",
|
|
|
|
TLSMinVersion: "tls10",
|
|
|
|
TLSRequireAndVerifyClientCert: true,
|
|
|
|
TLSDisableClientCerts: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := ListenerChecks(listeners)
|
|
|
|
if err == nil {
|
2021-04-12 17:55:33 +00:00
|
|
|
t.Fatalf("TLS Config check on fake certificate should fail")
|
2021-04-12 15:39:40 +00:00
|
|
|
}
|
|
|
|
if !strings.Contains(err.Error(), "pem block does not parse to a certificate") {
|
2021-04-12 17:55:33 +00:00
|
|
|
t.Fatalf("Bad error message: %s", err)
|
2021-04-12 15:39:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestTLSMultiCerts verifies that a unique error message is thrown when a cert is specified twice.
|
|
|
|
func TestTLSMultiCerts(t *testing.T) {
|
|
|
|
listeners := []listenerutil.Listener{
|
|
|
|
{
|
|
|
|
Config: &configutil.Listener{
|
|
|
|
Type: "tcp",
|
|
|
|
Address: "127.0.0.1:443",
|
|
|
|
ClusterAddress: "127.0.0.1:8201",
|
|
|
|
TLSCertFile: "./../../api/test-fixtures/keys/cert.pem",
|
|
|
|
TLSKeyFile: "./../../api/test-fixtures/keys/cert.pem",
|
|
|
|
TLSMinVersion: "tls10",
|
|
|
|
TLSRequireAndVerifyClientCert: true,
|
|
|
|
TLSDisableClientCerts: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := ListenerChecks(listeners)
|
|
|
|
if err == nil {
|
2021-04-12 17:55:33 +00:00
|
|
|
t.Fatalf("TLS Config check on fake certificate should fail")
|
2021-04-12 15:39:40 +00:00
|
|
|
}
|
|
|
|
if !strings.Contains(err.Error(), "found a certificate rather than a key in the PEM for the private key") {
|
2021-04-12 17:55:33 +00:00
|
|
|
t.Fatalf("Bad error message: %s", err)
|
2021-04-12 15:39:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestTLSInvalidRoot makes sure that the Verify call in tls.go checks the authority of
|
|
|
|
// the root. The root certificate used in this test is the Baltimore Cyber Trust root
|
|
|
|
// certificate, downloaded from: https://www.digicert.com/kb/digicert-root-certificates.htm
|
|
|
|
func TestTLSInvalidRoot(t *testing.T) {
|
|
|
|
listeners := []listenerutil.Listener{
|
|
|
|
{
|
|
|
|
Config: &configutil.Listener{
|
|
|
|
Type: "tcp",
|
|
|
|
Address: "127.0.0.1:443",
|
|
|
|
ClusterAddress: "127.0.0.1:8201",
|
|
|
|
TLSCertFile: "./test-fixtures/goodcertbadroot.pem",
|
|
|
|
TLSKeyFile: "./test-fixtures/goodkey.pem",
|
|
|
|
TLSMinVersion: "tls10",
|
|
|
|
TLSRequireAndVerifyClientCert: true,
|
|
|
|
TLSDisableClientCerts: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := ListenerChecks(listeners)
|
|
|
|
if err == nil {
|
2021-04-12 17:55:33 +00:00
|
|
|
t.Fatalf("TLS Config check on fake certificate should fail")
|
2021-04-12 15:39:40 +00:00
|
|
|
}
|
|
|
|
if err.Error() != "failed to verify certificate: x509: certificate signed by unknown authority" {
|
2021-04-12 17:55:33 +00:00
|
|
|
t.Fatalf("Bad error message: %s", err)
|
2021-04-12 15:39:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestTLSNoRoot ensures that a server certificate that is passed in without a root
|
|
|
|
// is still accepted by diagnose as valid. This is an acceptable, though less secure,
|
|
|
|
// server configuration.
|
|
|
|
func TestTLSNoRoot(t *testing.T) {
|
|
|
|
listeners := []listenerutil.Listener{
|
|
|
|
{
|
|
|
|
Config: &configutil.Listener{
|
|
|
|
Type: "tcp",
|
|
|
|
Address: "127.0.0.1:443",
|
|
|
|
ClusterAddress: "127.0.0.1:8201",
|
|
|
|
TLSCertFile: "./../../api/test-fixtures/keys/cert.pem",
|
|
|
|
TLSKeyFile: "./test-fixtures/goodkey.pem",
|
|
|
|
TLSMinVersion: "tls10",
|
|
|
|
TLSRequireAndVerifyClientCert: true,
|
|
|
|
TLSDisableClientCerts: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := ListenerChecks(listeners)
|
|
|
|
if err != nil {
|
2021-04-12 17:55:33 +00:00
|
|
|
t.Fatalf("Server certificate without root certificate is insecure, but still valid.")
|
2021-04-12 15:39:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestTLSInvalidMinVersion checks that a listener with an invalid minimum configured
|
|
|
|
// version errors appropriately.
|
|
|
|
func TestTLSInvalidMinVersion(t *testing.T) {
|
|
|
|
listeners := []listenerutil.Listener{
|
|
|
|
{
|
|
|
|
Config: &configutil.Listener{
|
|
|
|
Type: "tcp",
|
|
|
|
Address: "127.0.0.1:443",
|
|
|
|
ClusterAddress: "127.0.0.1:8201",
|
|
|
|
TLSCertFile: "./../../api/test-fixtures/keys/cert.pem",
|
|
|
|
TLSKeyFile: "./../../api/test-fixtures/keys/key.pem",
|
|
|
|
TLSClientCAFile: "./../../api/test-fixtures/root/rootcacert.pem",
|
|
|
|
TLSMinVersion: "0",
|
|
|
|
TLSRequireAndVerifyClientCert: true,
|
|
|
|
TLSDisableClientCerts: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := ListenerChecks(listeners)
|
|
|
|
if err == nil {
|
2021-04-12 17:55:33 +00:00
|
|
|
t.Fatalf("TLS Config check on fake certificate should fail")
|
2021-04-12 15:39:40 +00:00
|
|
|
}
|
|
|
|
if err.Error() != fmt.Errorf(minVersionError, "0").Error() {
|
2021-04-12 17:55:33 +00:00
|
|
|
t.Fatalf("Bad error message: %s", err)
|
2021-04-12 15:39:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestTLSInvalidMaxVersion checks that a listener with an invalid maximum configured
|
|
|
|
// version errors appropriately.
|
|
|
|
func TestTLSInvalidMaxVersion(t *testing.T) {
|
|
|
|
listeners := []listenerutil.Listener{
|
|
|
|
{
|
|
|
|
Config: &configutil.Listener{
|
|
|
|
Type: "tcp",
|
|
|
|
Address: "127.0.0.1:443",
|
|
|
|
ClusterAddress: "127.0.0.1:8201",
|
|
|
|
TLSCertFile: "./../../api/test-fixtures/keys/cert.pem",
|
|
|
|
TLSKeyFile: "./../../api/test-fixtures/keys/key.pem",
|
|
|
|
TLSClientCAFile: "./../../api/test-fixtures/root/rootcacert.pem",
|
|
|
|
TLSMaxVersion: "0",
|
|
|
|
TLSRequireAndVerifyClientCert: true,
|
|
|
|
TLSDisableClientCerts: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := ListenerChecks(listeners)
|
|
|
|
if err == nil {
|
2021-04-12 17:55:33 +00:00
|
|
|
t.Fatalf("TLS Config check on fake certificate should fail")
|
2021-04-12 15:39:40 +00:00
|
|
|
}
|
|
|
|
if err.Error() != fmt.Errorf(maxVersionError, "0").Error() {
|
|
|
|
t.Errorf("Bad error message: %w", err)
|
|
|
|
}
|
|
|
|
}
|