2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2018-03-19 17:48:38 +00:00
|
|
|
package connect
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-12-14 15:24:22 +00:00
|
|
|
"context"
|
2018-03-19 17:48:38 +00:00
|
|
|
"crypto"
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/x509"
|
|
|
|
"crypto/x509/pkix"
|
|
|
|
"encoding/pem"
|
|
|
|
"fmt"
|
2018-03-19 21:36:17 +00:00
|
|
|
"math/big"
|
2018-03-19 17:48:38 +00:00
|
|
|
"net/url"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
2018-03-20 03:29:14 +00:00
|
|
|
"github.com/hashicorp/go-uuid"
|
2018-03-19 17:48:38 +00:00
|
|
|
"github.com/mitchellh/go-testing-interface"
|
2021-06-25 18:00:00 +00:00
|
|
|
|
2022-06-15 19:36:18 +00:00
|
|
|
"github.com/hashicorp/consul/acl"
|
2021-06-25 18:00:00 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2018-03-19 17:48:38 +00:00
|
|
|
)
|
|
|
|
|
2018-05-10 16:04:33 +00:00
|
|
|
// TestClusterID is the Consul cluster ID for testing.
|
2019-08-19 18:03:03 +00:00
|
|
|
//
|
|
|
|
// NOTE: this is duplicated in the api package as testClusterID
|
2018-05-10 16:04:33 +00:00
|
|
|
const TestClusterID = "11111111-2222-3333-4444-555555555555"
|
2022-05-25 17:37:44 +00:00
|
|
|
const TestTrustDomain = TestClusterID + ".consul"
|
2018-03-19 17:48:38 +00:00
|
|
|
|
|
|
|
// testCACounter is just an atomically incremented counter for creating
|
|
|
|
// unique names for the CA certs.
|
2018-03-29 15:25:11 +00:00
|
|
|
var testCACounter uint64
|
2018-03-19 17:48:38 +00:00
|
|
|
|
2019-11-01 13:20:26 +00:00
|
|
|
// ValidateLeaf is a convenience helper that returns an error if the certificate
|
|
|
|
// provided in leadPEM does not validate against the CAs provided. If there is
|
|
|
|
// an intermediate CA then it's cert must be in caPEMs as well as the root.
|
|
|
|
func ValidateLeaf(caPEM string, leafPEM string, intermediatePEMs []string) error {
|
|
|
|
roots := x509.NewCertPool()
|
|
|
|
ok := roots.AppendCertsFromPEM([]byte(caPEM))
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Failed to add root CA")
|
|
|
|
}
|
|
|
|
|
|
|
|
intermediates := x509.NewCertPool()
|
|
|
|
for idx, ca := range intermediatePEMs {
|
|
|
|
ok := intermediates.AppendCertsFromPEM([]byte(ca))
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Failed to add intermediate CA at index %d to pool", idx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
leaf, err := ParseCert(leafPEM)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = leaf.Verify(x509.VerifyOptions{
|
|
|
|
Roots: roots,
|
|
|
|
Intermediates: intermediates,
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-20 04:08:06 +00:00
|
|
|
func testCA(t testing.T, xc *structs.CARoot, keyType string, keyBits int, ttl time.Duration) *structs.CARoot {
|
2018-03-19 17:48:38 +00:00
|
|
|
var result structs.CARoot
|
2018-03-20 03:29:14 +00:00
|
|
|
result.Active = true
|
2018-03-19 17:48:38 +00:00
|
|
|
result.Name = fmt.Sprintf("Test CA %d", atomic.AddUint64(&testCACounter, 1))
|
|
|
|
|
|
|
|
// Create the private key we'll use for this CA cert.
|
2019-07-30 21:47:39 +00:00
|
|
|
signer, keyPEM := testPrivateKey(t, keyType, keyBits)
|
2018-03-29 15:25:11 +00:00
|
|
|
result.SigningKey = keyPEM
|
2019-09-23 17:52:35 +00:00
|
|
|
result.SigningKeyID = EncodeSigningKeyID(testKeyID(t, signer.Public()))
|
2018-03-19 17:48:38 +00:00
|
|
|
|
|
|
|
// The serial number for the cert
|
2018-03-19 21:36:17 +00:00
|
|
|
sn, err := testSerialNumber()
|
2018-03-19 17:48:38 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error generating serial number: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The URI (SPIFFE compatible) for the cert
|
2018-05-10 16:04:33 +00:00
|
|
|
id := &SpiffeIDSigning{ClusterID: TestClusterID, Domain: "consul"}
|
2018-03-19 17:48:38 +00:00
|
|
|
|
|
|
|
// Create the CA cert
|
2020-11-20 04:08:06 +00:00
|
|
|
now := time.Now()
|
|
|
|
before := now
|
|
|
|
after := now
|
|
|
|
if ttl != 0 {
|
|
|
|
after = after.Add(ttl)
|
|
|
|
} else {
|
|
|
|
after = after.AddDate(10, 0, 0)
|
|
|
|
}
|
2018-03-19 17:48:38 +00:00
|
|
|
template := x509.Certificate{
|
2018-11-07 10:16:03 +00:00
|
|
|
SerialNumber: sn,
|
|
|
|
Subject: pkix.Name{CommonName: result.Name},
|
|
|
|
URIs: []*url.URL{id.URI()},
|
2018-06-21 16:40:56 +00:00
|
|
|
BasicConstraintsValid: true,
|
2018-03-24 18:32:42 +00:00
|
|
|
KeyUsage: x509.KeyUsageCertSign |
|
|
|
|
x509.KeyUsageCRLSign |
|
|
|
|
x509.KeyUsageDigitalSignature,
|
|
|
|
IsCA: true,
|
2020-11-20 04:08:06 +00:00
|
|
|
NotAfter: after,
|
|
|
|
NotBefore: before,
|
2018-03-24 18:32:42 +00:00
|
|
|
AuthorityKeyId: testKeyID(t, signer.Public()),
|
|
|
|
SubjectKeyId: testKeyID(t, signer.Public()),
|
2018-03-19 17:48:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bs, err := x509.CreateCertificate(
|
|
|
|
rand.Reader, &template, &template, signer.Public(), signer)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error generating CA certificate: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
err = pem.Encode(&buf, &pem.Block{Type: "CERTIFICATE", Bytes: bs})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error encoding private key: %s", err)
|
|
|
|
}
|
|
|
|
result.RootCert = buf.String()
|
2022-01-05 23:21:04 +00:00
|
|
|
result.ID = CalculateCertFingerprint(bs)
|
2019-01-10 12:46:11 +00:00
|
|
|
result.SerialNumber = uint64(sn.Int64())
|
|
|
|
result.NotBefore = template.NotBefore.UTC()
|
|
|
|
result.NotAfter = template.NotAfter.UTC()
|
2019-11-01 13:20:26 +00:00
|
|
|
result.PrivateKeyType = keyType
|
|
|
|
result.PrivateKeyBits = keyBits
|
2022-03-22 20:30:00 +00:00
|
|
|
result.IntermediateCerts = []string{}
|
2018-03-19 17:48:38 +00:00
|
|
|
|
|
|
|
// If there is a prior CA to cross-sign with, then we need to create that
|
|
|
|
// and set it as the signing cert.
|
|
|
|
if xc != nil {
|
|
|
|
xccert, err := ParseCert(xc.RootCert)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error parsing CA cert: %s", err)
|
|
|
|
}
|
|
|
|
xcsigner, err := ParseSigner(xc.SigningKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error parsing signing key: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-03-24 18:32:42 +00:00
|
|
|
// Set the authority key to be the previous one.
|
|
|
|
// NOTE(mitchellh): From Paul Banks: if we have to cross-sign a cert
|
|
|
|
// that came from outside (e.g. vault) we can't rely on them using the
|
|
|
|
// same KeyID hashing algo we do so we'd need to actually copy this
|
|
|
|
// from the xc cert's subjectKeyIdentifier extension.
|
2018-03-19 17:48:38 +00:00
|
|
|
template.AuthorityKeyId = testKeyID(t, xcsigner.Public())
|
|
|
|
|
|
|
|
// Create the new certificate where the parent is the previous
|
|
|
|
// CA, the public key is the new public key, and the signing private
|
|
|
|
// key is the old private key.
|
|
|
|
bs, err := x509.CreateCertificate(
|
|
|
|
rand.Reader, &template, xccert, signer.Public(), xcsigner)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error generating CA certificate: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
err = pem.Encode(&buf, &pem.Block{Type: "CERTIFICATE", Bytes: bs})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error encoding private key: %s", err)
|
|
|
|
}
|
|
|
|
result.SigningCert = buf.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
return &result
|
|
|
|
}
|
|
|
|
|
2019-07-30 21:47:39 +00:00
|
|
|
// TestCA creates a test CA certificate and signing key and returns it
|
|
|
|
// in the CARoot structure format. The returned CA will be set as Active = true.
|
|
|
|
//
|
|
|
|
// If xc is non-nil, then the returned certificate will have a signing cert
|
|
|
|
// that is cross-signed with the previous cert, and this will be set as
|
|
|
|
// SigningCert.
|
|
|
|
func TestCA(t testing.T, xc *structs.CARoot) *structs.CARoot {
|
2020-11-20 04:08:06 +00:00
|
|
|
return testCA(t, xc, DefaultPrivateKeyType, DefaultPrivateKeyBits, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestCAWithTTL is similar to TestCA, except that it
|
|
|
|
// takes a custom duration for the lifetime of the certificate.
|
|
|
|
func TestCAWithTTL(t testing.T, xc *structs.CARoot, ttl time.Duration) *structs.CARoot {
|
|
|
|
return testCA(t, xc, DefaultPrivateKeyType, DefaultPrivateKeyBits, ttl)
|
2019-07-30 21:47:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestCAWithKeyType is similar to TestCA, except that it
|
|
|
|
// takes two additional arguments to override the default private key type and size.
|
|
|
|
func TestCAWithKeyType(t testing.T, xc *structs.CARoot, keyType string, keyBits int) *structs.CARoot {
|
2020-11-20 04:08:06 +00:00
|
|
|
return testCA(t, xc, keyType, keyBits, 0)
|
2019-07-30 21:47:39 +00:00
|
|
|
}
|
|
|
|
|
2022-09-29 03:27:11 +00:00
|
|
|
func testLeafWithID(t testing.T, spiffeId CertURI, dnsSAN string, root *structs.CARoot, keyType string, keyBits int, expiration time.Duration) (string, string, error) {
|
2020-07-14 19:05:03 +00:00
|
|
|
if expiration == 0 {
|
|
|
|
// this is 10 years
|
|
|
|
expiration = 10 * 365 * 24 * time.Hour
|
|
|
|
}
|
2018-03-19 17:48:38 +00:00
|
|
|
// Parse the CA cert and signing key from the root
|
2018-03-19 21:36:17 +00:00
|
|
|
cert := root.SigningCert
|
|
|
|
if cert == "" {
|
|
|
|
cert = root.RootCert
|
|
|
|
}
|
|
|
|
caCert, err := ParseCert(cert)
|
2018-03-19 17:48:38 +00:00
|
|
|
if err != nil {
|
2019-07-30 21:47:39 +00:00
|
|
|
return "", "", fmt.Errorf("error parsing CA cert: %s", err)
|
2018-03-19 17:48:38 +00:00
|
|
|
}
|
2018-03-29 15:25:11 +00:00
|
|
|
caSigner, err := ParseSigner(root.SigningKey)
|
2018-03-19 17:48:38 +00:00
|
|
|
if err != nil {
|
2019-07-30 21:47:39 +00:00
|
|
|
return "", "", fmt.Errorf("error parsing signing key: %s", err)
|
2018-03-19 17:48:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The serial number for the cert
|
2018-03-19 21:36:17 +00:00
|
|
|
sn, err := testSerialNumber()
|
2018-03-19 17:48:38 +00:00
|
|
|
if err != nil {
|
2019-07-30 21:47:39 +00:00
|
|
|
return "", "", fmt.Errorf("error generating serial number: %s", err)
|
2018-03-19 17:48:38 +00:00
|
|
|
}
|
|
|
|
|
2018-04-03 18:10:59 +00:00
|
|
|
// Generate fresh private key
|
2019-07-30 21:47:39 +00:00
|
|
|
pkSigner, pkPEM, err := GeneratePrivateKeyWithConfig(keyType, keyBits)
|
2018-05-09 16:15:29 +00:00
|
|
|
if err != nil {
|
2019-07-30 21:47:39 +00:00
|
|
|
return "", "", fmt.Errorf("failed to generate private key: %s", err)
|
|
|
|
}
|
|
|
|
|
2019-11-01 13:20:26 +00:00
|
|
|
rootKeyType, _, err := KeyInfoFromCert(caCert)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", fmt.Errorf("error getting CA key type: %s", err)
|
2018-05-09 16:15:29 +00:00
|
|
|
}
|
2018-03-29 15:25:11 +00:00
|
|
|
|
2018-03-19 17:48:38 +00:00
|
|
|
// Cert template for generation
|
|
|
|
template := x509.Certificate{
|
|
|
|
SerialNumber: sn,
|
2018-03-19 21:36:17 +00:00
|
|
|
URIs: []*url.URL{spiffeId.URI()},
|
2019-11-01 13:20:26 +00:00
|
|
|
SignatureAlgorithm: SigAlgoForKeyType(rootKeyType),
|
2018-03-19 17:48:38 +00:00
|
|
|
BasicConstraintsValid: true,
|
2018-03-24 18:32:42 +00:00
|
|
|
KeyUsage: x509.KeyUsageDataEncipherment |
|
|
|
|
x509.KeyUsageKeyAgreement |
|
|
|
|
x509.KeyUsageDigitalSignature |
|
|
|
|
x509.KeyUsageKeyEncipherment,
|
2018-03-19 17:48:38 +00:00
|
|
|
ExtKeyUsage: []x509.ExtKeyUsage{
|
|
|
|
x509.ExtKeyUsageClientAuth,
|
|
|
|
x509.ExtKeyUsageServerAuth,
|
|
|
|
},
|
2020-07-14 19:05:03 +00:00
|
|
|
NotAfter: time.Now().Add(expiration),
|
2018-03-19 17:48:38 +00:00
|
|
|
NotBefore: time.Now(),
|
2018-03-29 15:25:11 +00:00
|
|
|
AuthorityKeyId: testKeyID(t, caSigner.Public()),
|
|
|
|
SubjectKeyId: testKeyID(t, pkSigner.Public()),
|
2022-09-29 03:27:11 +00:00
|
|
|
DNSNames: []string{dnsSAN},
|
2018-03-19 17:48:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create the certificate, PEM encode it and return that value.
|
|
|
|
var buf bytes.Buffer
|
|
|
|
bs, err := x509.CreateCertificate(
|
2018-03-29 15:25:11 +00:00
|
|
|
rand.Reader, &template, caCert, pkSigner.Public(), caSigner)
|
2018-03-19 17:48:38 +00:00
|
|
|
if err != nil {
|
2019-07-30 21:47:39 +00:00
|
|
|
return "", "", fmt.Errorf("error generating certificate: %s", err)
|
2018-03-19 17:48:38 +00:00
|
|
|
}
|
|
|
|
err = pem.Encode(&buf, &pem.Block{Type: "CERTIFICATE", Bytes: bs})
|
|
|
|
if err != nil {
|
2019-07-30 21:47:39 +00:00
|
|
|
return "", "", fmt.Errorf("error encoding private key: %s", err)
|
2018-03-19 17:48:38 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 21:47:39 +00:00
|
|
|
return buf.String(), pkPEM, nil
|
|
|
|
}
|
|
|
|
|
2020-07-14 19:05:03 +00:00
|
|
|
func TestAgentLeaf(t testing.T, node string, datacenter string, root *structs.CARoot, expiration time.Duration) (string, string, error) {
|
|
|
|
// Build the SPIFFE ID
|
|
|
|
spiffeId := &SpiffeIDAgent{
|
|
|
|
Host: fmt.Sprintf("%s.consul", TestClusterID),
|
|
|
|
Datacenter: datacenter,
|
|
|
|
Agent: node,
|
|
|
|
}
|
|
|
|
|
2022-09-29 03:27:11 +00:00
|
|
|
return testLeafWithID(t, spiffeId, "", root, DefaultPrivateKeyType, DefaultPrivateKeyBits, expiration)
|
2020-07-14 19:05:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func testLeaf(t testing.T, service string, namespace string, root *structs.CARoot, keyType string, keyBits int) (string, string, error) {
|
|
|
|
// Build the SPIFFE ID
|
|
|
|
spiffeId := &SpiffeIDService{
|
|
|
|
Host: fmt.Sprintf("%s.consul", TestClusterID),
|
|
|
|
Namespace: namespace,
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Service: service,
|
|
|
|
}
|
|
|
|
|
2022-09-29 03:27:11 +00:00
|
|
|
return testLeafWithID(t, spiffeId, "", root, keyType, keyBits, 0)
|
2020-07-14 19:05:03 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 21:47:39 +00:00
|
|
|
// TestLeaf returns a valid leaf certificate and it's private key for the named
|
|
|
|
// service with the given CA Root.
|
|
|
|
func TestLeaf(t testing.T, service string, root *structs.CARoot) (string, string) {
|
2020-02-03 14:26:47 +00:00
|
|
|
return TestLeafWithNamespace(t, service, "default", root)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLeafWithNamespace(t testing.T, service, namespace string, root *structs.CARoot) (string, string) {
|
2019-11-01 13:20:26 +00:00
|
|
|
// Currently we only support EC leaf keys and certs even if the CA is using
|
|
|
|
// RSA. We might allow Leafs to follow the signing CA key type later if we
|
|
|
|
// need to for compatibility sake but this is allowed by TLS 1.2 and works with
|
|
|
|
// both openssl verify (which we use as a sanity check in our tests of this
|
|
|
|
// package) and Go's TLS verification.
|
2020-02-03 14:26:47 +00:00
|
|
|
certPEM, keyPEM, err := testLeaf(t, service, namespace, root, DefaultPrivateKeyType, DefaultPrivateKeyBits)
|
2019-07-30 21:47:39 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(err.Error())
|
|
|
|
}
|
|
|
|
return certPEM, keyPEM
|
2018-03-19 17:48:38 +00:00
|
|
|
}
|
|
|
|
|
2022-06-15 19:36:18 +00:00
|
|
|
func TestMeshGatewayLeaf(t testing.T, partition string, root *structs.CARoot) (string, string) {
|
|
|
|
// Build the SPIFFE ID
|
|
|
|
spiffeId := &SpiffeIDMeshGateway{
|
|
|
|
Host: fmt.Sprintf("%s.consul", TestClusterID),
|
|
|
|
Partition: acl.PartitionOrDefault(partition),
|
|
|
|
Datacenter: "dc1",
|
|
|
|
}
|
|
|
|
|
2022-09-29 03:27:11 +00:00
|
|
|
certPEM, keyPEM, err := testLeafWithID(t, spiffeId, "", root, DefaultPrivateKeyType, DefaultPrivateKeyBits, 0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(err.Error())
|
|
|
|
}
|
|
|
|
return certPEM, keyPEM
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServerLeaf(t testing.T, dc string, root *structs.CARoot) (string, string) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
spiffeID := &SpiffeIDServer{
|
|
|
|
Datacenter: dc,
|
|
|
|
Host: fmt.Sprintf("%s.consul", TestClusterID),
|
|
|
|
}
|
|
|
|
san := PeeringServerSAN(dc, TestTrustDomain)
|
|
|
|
|
|
|
|
certPEM, keyPEM, err := testLeafWithID(t, spiffeID, san, root, DefaultPrivateKeyType, DefaultPrivateKeyBits, 0)
|
2022-06-15 19:36:18 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(err.Error())
|
|
|
|
}
|
|
|
|
return certPEM, keyPEM
|
|
|
|
}
|
|
|
|
|
2018-03-21 17:55:39 +00:00
|
|
|
// TestCSR returns a CSR to sign the given service along with the PEM-encoded
|
|
|
|
// private key for this certificate.
|
2019-08-27 21:45:58 +00:00
|
|
|
func TestCSR(t testing.T, uri CertURI) (string, string) {
|
2018-03-19 21:36:17 +00:00
|
|
|
template := &x509.CertificateRequest{
|
2018-03-24 18:39:43 +00:00
|
|
|
URIs: []*url.URL{uri.URI()},
|
2018-03-20 04:00:01 +00:00
|
|
|
SignatureAlgorithm: x509.ECDSAWithSHA256,
|
2018-03-19 21:36:17 +00:00
|
|
|
}
|
|
|
|
|
2021-06-25 18:00:00 +00:00
|
|
|
HackSANExtensionForCSR(template)
|
|
|
|
|
2018-03-19 21:36:17 +00:00
|
|
|
// Create the private key we'll use
|
2019-07-30 21:47:39 +00:00
|
|
|
signer, pkPEM := testPrivateKey(t, DefaultPrivateKeyType, DefaultPrivateKeyBits)
|
2018-03-21 17:55:39 +00:00
|
|
|
|
2018-03-29 15:25:11 +00:00
|
|
|
// Create the CSR itself
|
|
|
|
var csrBuf bytes.Buffer
|
|
|
|
bs, err := x509.CreateCertificateRequest(rand.Reader, template, signer)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error creating CSR: %s", err)
|
2018-03-19 21:36:17 +00:00
|
|
|
}
|
|
|
|
|
2018-03-29 15:25:11 +00:00
|
|
|
err = pem.Encode(&csrBuf, &pem.Block{Type: "CERTIFICATE REQUEST", Bytes: bs})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error encoding CSR: %s", err)
|
2018-03-19 21:36:17 +00:00
|
|
|
}
|
|
|
|
|
2018-03-29 15:25:11 +00:00
|
|
|
return csrBuf.String(), pkPEM
|
2018-03-19 21:36:17 +00:00
|
|
|
}
|
|
|
|
|
2018-03-24 18:32:42 +00:00
|
|
|
// testKeyID returns a KeyID from the given public key. This just calls
|
|
|
|
// KeyId but handles errors for tests.
|
2018-03-19 17:48:38 +00:00
|
|
|
func testKeyID(t testing.T, raw interface{}) []byte {
|
2018-03-24 18:32:42 +00:00
|
|
|
result, err := KeyId(raw)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("KeyId error: %s", err)
|
2018-03-19 17:48:38 +00:00
|
|
|
}
|
|
|
|
|
2018-03-24 18:32:42 +00:00
|
|
|
return result
|
2018-03-19 17:48:38 +00:00
|
|
|
}
|
|
|
|
|
2018-03-29 15:25:11 +00:00
|
|
|
// testPrivateKey creates an ECDSA based private key. Both a crypto.Signer and
|
|
|
|
// the key in PEM form are returned.
|
|
|
|
//
|
|
|
|
// NOTE(banks): this was memoized to save entropy during tests but it turns out
|
|
|
|
// crypto/rand will never block and always reads from /dev/urandom on unix OSes
|
|
|
|
// which does not consume entropy.
|
|
|
|
//
|
2019-03-06 17:13:28 +00:00
|
|
|
// If we find by profiling it's taking a lot of cycles we could optimize/cache
|
2018-03-29 15:25:11 +00:00
|
|
|
// again but we at least need to use different keys for each distinct CA (when
|
|
|
|
// multiple CAs are generated at once e.g. to test cross-signing) and a
|
|
|
|
// different one again for the leafs otherwise we risk tests that have false
|
|
|
|
// positives since signatures from different logical cert's keys are
|
|
|
|
// indistinguishable, but worse we build validation chains using AuthorityKeyID
|
|
|
|
// which will be the same for multiple CAs/Leafs. Also note that our UUID
|
|
|
|
// generator also reads from crypto rand and is called far more often during
|
|
|
|
// tests than this will be.
|
2019-07-30 21:47:39 +00:00
|
|
|
func testPrivateKey(t testing.T, keyType string, keyBits int) (crypto.Signer, string) {
|
|
|
|
pk, pkPEM, err := GeneratePrivateKeyWithConfig(keyType, keyBits)
|
2018-03-29 15:25:11 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error generating private key: %s", err)
|
2018-03-19 17:48:38 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 21:47:39 +00:00
|
|
|
return pk, pkPEM
|
2018-03-19 17:48:38 +00:00
|
|
|
}
|
2018-03-19 21:36:17 +00:00
|
|
|
|
2019-01-10 12:46:11 +00:00
|
|
|
// testSerialNumber generates a serial number suitable for a certificate. For
|
|
|
|
// testing, this just sets it to a random number, but one that can fit in a
|
|
|
|
// uint64 since we use that in our datastructures and assume cert serials will
|
|
|
|
// fit in that for now.
|
2018-03-19 21:36:17 +00:00
|
|
|
func testSerialNumber() (*big.Int, error) {
|
2019-01-10 12:46:11 +00:00
|
|
|
return rand.Int(rand.Reader, (&big.Int{}).Exp(big.NewInt(2), big.NewInt(63), nil))
|
2018-03-19 21:36:17 +00:00
|
|
|
}
|
2018-03-20 03:29:14 +00:00
|
|
|
|
|
|
|
// testUUID generates a UUID for testing.
|
|
|
|
func testUUID(t testing.T) string {
|
|
|
|
ret, err := uuid.GenerateUUID()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to generate a UUID, %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
2018-04-30 21:23:49 +00:00
|
|
|
|
|
|
|
// TestAgentRPC is an interface that an RPC client must implement. This is a
|
|
|
|
// helper interface that is implemented by the agent delegate so that test
|
|
|
|
// helpers can make RPCs without introducing an import cycle on `agent`.
|
|
|
|
type TestAgentRPC interface {
|
2022-12-14 15:24:22 +00:00
|
|
|
RPC(ctx context.Context, method string, args interface{}, reply interface{}) error
|
2018-04-30 21:23:49 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 21:47:39 +00:00
|
|
|
func testCAConfigSet(t testing.T, a TestAgentRPC,
|
|
|
|
ca *structs.CARoot, keyType string, keyBits int) *structs.CARoot {
|
2018-04-30 21:23:49 +00:00
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
if ca == nil {
|
2019-07-30 21:47:39 +00:00
|
|
|
ca = TestCAWithKeyType(t, nil, keyType, keyBits)
|
2018-04-30 21:23:49 +00:00
|
|
|
}
|
|
|
|
newConfig := &structs.CAConfiguration{
|
|
|
|
Provider: "consul",
|
|
|
|
Config: map[string]interface{}{
|
2020-01-17 22:27:13 +00:00
|
|
|
"PrivateKey": ca.SigningKey,
|
|
|
|
"RootCert": ca.RootCert,
|
2020-02-10 23:05:49 +00:00
|
|
|
"IntermediateCertTTL": 288 * time.Hour,
|
2018-04-30 21:23:49 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
args := &structs.CARequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Config: newConfig,
|
|
|
|
}
|
|
|
|
var reply interface{}
|
|
|
|
|
2022-12-14 15:24:22 +00:00
|
|
|
err := a.RPC(context.Background(), "ConnectCA.ConfigurationSet", args, &reply)
|
2018-04-30 21:23:49 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to set test CA config: %s", err)
|
|
|
|
}
|
|
|
|
return ca
|
|
|
|
}
|
2019-07-30 21:47:39 +00:00
|
|
|
|
|
|
|
// TestCAConfigSet sets a CARoot returned by TestCA into the TestAgent state. It
|
|
|
|
// requires that TestAgent had connect enabled in it's config. If ca is nil, a
|
|
|
|
// new CA is created.
|
|
|
|
//
|
|
|
|
// It returns the CARoot passed or created.
|
|
|
|
//
|
|
|
|
// Note that we have to use an interface for the TestAgent.RPC method since we
|
|
|
|
// can't introduce an import cycle by importing `agent.TestAgent` here directly.
|
|
|
|
// It also means this will work in a few other places we mock that method.
|
|
|
|
func TestCAConfigSet(t testing.T, a TestAgentRPC,
|
|
|
|
ca *structs.CARoot) *structs.CARoot {
|
|
|
|
return testCAConfigSet(t, a, ca, DefaultPrivateKeyType, DefaultPrivateKeyBits)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestCAConfigSetWithKeyType is similar to TestCAConfigSet, except that it
|
|
|
|
// takes two additional arguments to override the default private key type and size.
|
|
|
|
func TestCAConfigSetWithKeyType(t testing.T, a TestAgentRPC,
|
|
|
|
ca *structs.CARoot, keyType string, keyBits int) *structs.CARoot {
|
|
|
|
return testCAConfigSet(t, a, ca, keyType, keyBits)
|
|
|
|
}
|