2018-05-23 21:43:40 +00:00
|
|
|
package ca
|
2018-04-20 08:30:34 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/rand"
|
2018-09-12 20:44:15 +00:00
|
|
|
"crypto/sha256"
|
2018-04-20 08:30:34 +00:00
|
|
|
"crypto/x509"
|
|
|
|
"crypto/x509/pkix"
|
|
|
|
"encoding/pem"
|
2018-09-07 02:18:54 +00:00
|
|
|
"errors"
|
2018-04-20 08:30:34 +00:00
|
|
|
"fmt"
|
|
|
|
"math/big"
|
|
|
|
"net/url"
|
2018-09-12 20:44:15 +00:00
|
|
|
"strings"
|
2018-04-20 08:30:34 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/connect"
|
2018-05-03 19:50:45 +00:00
|
|
|
"github.com/hashicorp/consul/agent/consul/state"
|
2018-04-20 08:30:34 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
|
|
|
)
|
|
|
|
|
2018-09-07 02:18:54 +00:00
|
|
|
var ErrNotInitialized = errors.New("provider not initialized")
|
|
|
|
|
2018-05-09 22:12:31 +00:00
|
|
|
type ConsulProvider struct {
|
2018-09-11 23:43:04 +00:00
|
|
|
Delegate ConsulProviderStateDelegate
|
|
|
|
|
2018-09-13 02:52:24 +00:00
|
|
|
config *structs.ConsulCAProviderConfig
|
|
|
|
id string
|
|
|
|
clusterID string
|
|
|
|
isRoot bool
|
|
|
|
spiffeID *connect.SpiffeIDSigning
|
|
|
|
|
2018-04-20 08:30:34 +00:00
|
|
|
sync.RWMutex
|
|
|
|
}
|
|
|
|
|
2018-05-09 22:12:31 +00:00
|
|
|
type ConsulProviderStateDelegate interface {
|
2018-05-03 19:50:45 +00:00
|
|
|
State() *state.Store
|
|
|
|
ApplyCARequest(*structs.CARequest) error
|
|
|
|
}
|
|
|
|
|
2018-09-11 23:43:04 +00:00
|
|
|
// Configure sets up the provider using the given configuration.
|
2018-09-12 20:44:15 +00:00
|
|
|
func (c *ConsulProvider) Configure(clusterID string, isRoot bool, rawConfig map[string]interface{}) error {
|
2018-09-07 02:18:54 +00:00
|
|
|
// Parse the raw config and update our ID.
|
|
|
|
config, err := ParseConsulCAConfig(rawConfig)
|
2018-04-20 08:30:34 +00:00
|
|
|
if err != nil {
|
2018-09-07 02:18:54 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.config = config
|
2018-09-12 20:44:15 +00:00
|
|
|
hash := sha256.Sum256([]byte(fmt.Sprintf("%s,%s,%v", config.PrivateKey, config.RootCert, isRoot)))
|
|
|
|
c.id = strings.Replace(fmt.Sprintf("% x", hash), " ", ":", -1)
|
2018-09-13 02:52:24 +00:00
|
|
|
c.clusterID = clusterID
|
|
|
|
c.isRoot = isRoot
|
|
|
|
c.spiffeID = connect.SpiffeIDSigningForCluster(&structs.CAConfiguration{ClusterID: clusterID})
|
2018-09-07 02:18:54 +00:00
|
|
|
|
|
|
|
// Exit early if the state store has an entry for this provider's config.
|
2018-09-11 23:43:04 +00:00
|
|
|
_, providerState, err := c.Delegate.State().CAProviderState(c.id)
|
2018-09-07 02:18:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-04-20 08:30:34 +00:00
|
|
|
}
|
|
|
|
|
2018-08-07 12:29:48 +00:00
|
|
|
if providerState != nil {
|
2018-09-07 02:18:54 +00:00
|
|
|
return nil
|
2018-08-07 12:29:48 +00:00
|
|
|
}
|
|
|
|
|
2018-09-12 20:44:15 +00:00
|
|
|
// Check if there's an entry with the old ID scheme.
|
|
|
|
oldID := fmt.Sprintf("%s,%s", config.PrivateKey, config.RootCert)
|
|
|
|
_, providerState, err = c.Delegate.State().CAProviderState(oldID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Found an entry with the old ID, so update it to the new ID and
|
|
|
|
// delete the old entry.
|
|
|
|
if providerState != nil {
|
|
|
|
newState := *providerState
|
|
|
|
newState.ID = c.id
|
|
|
|
createReq := &structs.CARequest{
|
|
|
|
Op: structs.CAOpSetProviderState,
|
|
|
|
ProviderState: &newState,
|
|
|
|
}
|
|
|
|
if err := c.Delegate.ApplyCARequest(createReq); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteReq := &structs.CARequest{
|
|
|
|
Op: structs.CAOpDeleteProviderState,
|
|
|
|
ProviderState: providerState,
|
|
|
|
}
|
|
|
|
if err := c.Delegate.ApplyCARequest(deleteReq); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-07 02:18:54 +00:00
|
|
|
// Write the provider state to the state store.
|
2018-04-21 01:46:02 +00:00
|
|
|
newState := structs.CAConsulProviderState{
|
2018-09-11 23:43:04 +00:00
|
|
|
ID: c.id,
|
2018-04-20 08:30:34 +00:00
|
|
|
}
|
|
|
|
|
2018-09-07 02:18:54 +00:00
|
|
|
args := &structs.CARequest{
|
|
|
|
Op: structs.CAOpSetProviderState,
|
|
|
|
ProviderState: &newState,
|
|
|
|
}
|
2018-09-11 23:43:04 +00:00
|
|
|
if err := c.Delegate.ApplyCARequest(args); err != nil {
|
2018-09-07 02:18:54 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-11 23:43:04 +00:00
|
|
|
// ActiveRoot returns the active root CA certificate.
|
2018-09-07 02:18:54 +00:00
|
|
|
func (c *ConsulProvider) ActiveRoot() (string, error) {
|
2018-09-13 02:52:24 +00:00
|
|
|
_, providerState, err := c.getState()
|
2018-09-07 02:18:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2018-08-07 12:29:48 +00:00
|
|
|
}
|
2018-04-21 01:46:02 +00:00
|
|
|
|
2018-09-07 02:18:54 +00:00
|
|
|
return providerState.RootCert, nil
|
|
|
|
}
|
|
|
|
|
2018-09-11 23:43:04 +00:00
|
|
|
// GenerateRoot initializes a new root certificate and private key
|
|
|
|
// if needed.
|
2018-09-07 02:18:54 +00:00
|
|
|
func (c *ConsulProvider) GenerateRoot() error {
|
2018-09-13 02:52:24 +00:00
|
|
|
idx, providerState, err := c.getState()
|
2018-08-07 12:29:48 +00:00
|
|
|
if err != nil {
|
2018-09-07 02:18:54 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-09-11 23:43:04 +00:00
|
|
|
if !c.isRoot {
|
2018-09-07 02:18:54 +00:00
|
|
|
return fmt.Errorf("provider is not the root certificate authority")
|
|
|
|
}
|
|
|
|
if providerState.RootCert != "" {
|
|
|
|
return nil
|
2018-04-21 01:46:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Generate a private key if needed
|
2018-09-07 02:18:54 +00:00
|
|
|
newState := *providerState
|
|
|
|
if c.config.PrivateKey == "" {
|
2018-05-09 16:15:29 +00:00
|
|
|
_, pk, err := connect.GeneratePrivateKey()
|
2018-04-21 01:46:02 +00:00
|
|
|
if err != nil {
|
2018-09-07 02:18:54 +00:00
|
|
|
return err
|
2018-04-21 01:46:02 +00:00
|
|
|
}
|
|
|
|
newState.PrivateKey = pk
|
|
|
|
} else {
|
2018-09-07 02:18:54 +00:00
|
|
|
newState.PrivateKey = c.config.PrivateKey
|
2018-04-21 01:46:02 +00:00
|
|
|
}
|
|
|
|
|
2018-04-24 23:16:37 +00:00
|
|
|
// Generate the root CA if necessary
|
2018-09-07 02:18:54 +00:00
|
|
|
if c.config.RootCert == "" {
|
|
|
|
ca, err := c.generateCA(newState.PrivateKey, idx+1)
|
2018-04-24 23:16:37 +00:00
|
|
|
if err != nil {
|
2018-09-07 02:18:54 +00:00
|
|
|
return fmt.Errorf("error generating CA: %v", err)
|
2018-04-24 23:16:37 +00:00
|
|
|
}
|
|
|
|
newState.RootCert = ca
|
|
|
|
} else {
|
2018-09-07 02:18:54 +00:00
|
|
|
newState.RootCert = c.config.RootCert
|
2018-04-21 01:46:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write the provider state
|
|
|
|
args := &structs.CARequest{
|
|
|
|
Op: structs.CAOpSetProviderState,
|
|
|
|
ProviderState: &newState,
|
|
|
|
}
|
2018-09-11 23:43:04 +00:00
|
|
|
if err := c.Delegate.ApplyCARequest(args); err != nil {
|
2018-09-07 02:18:54 +00:00
|
|
|
return err
|
2018-04-21 01:46:02 +00:00
|
|
|
}
|
|
|
|
|
2018-09-07 02:18:54 +00:00
|
|
|
return nil
|
2018-04-20 08:30:34 +00:00
|
|
|
}
|
|
|
|
|
2018-09-13 02:52:24 +00:00
|
|
|
// GenerateIntermediateCSR creates a private key and generates a CSR
|
|
|
|
// for another datacenter's root to sign.
|
|
|
|
func (c *ConsulProvider) GenerateIntermediateCSR() (string, error) {
|
|
|
|
_, providerState, err := c.getState()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.isRoot {
|
|
|
|
return "", fmt.Errorf("provider is the root certificate authority, " +
|
|
|
|
"cannot generate an intermediate CSR")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new private key and CSR.
|
|
|
|
signer, pk, err := connect.GeneratePrivateKey()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
csr, err := connect.CreateCSR(c.spiffeID, signer)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the new provider state to the store.
|
|
|
|
newState := *providerState
|
|
|
|
newState.PrivateKey = pk
|
|
|
|
args := &structs.CARequest{
|
|
|
|
Op: structs.CAOpSetProviderState,
|
|
|
|
ProviderState: &newState,
|
|
|
|
}
|
|
|
|
if err := c.Delegate.ApplyCARequest(args); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return csr, nil
|
|
|
|
}
|
|
|
|
|
2018-09-13 20:09:07 +00:00
|
|
|
// SetIntermediate validates that the given intermediate is for the right private key
|
|
|
|
// and writes the given intermediate and root certificates to the state.
|
2018-09-13 02:52:24 +00:00
|
|
|
func (c *ConsulProvider) SetIntermediate(intermediatePEM, rootPEM string) error {
|
|
|
|
_, providerState, err := c.getState()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.isRoot {
|
|
|
|
return fmt.Errorf("cannot set an intermediate using another root in the primary datacenter")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the key from the incoming intermediate cert so we can compare it
|
|
|
|
// to the currently stored key.
|
|
|
|
intermediate, err := connect.ParseCert(intermediatePEM)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error parsing intermediate PEM: %v", err)
|
|
|
|
}
|
|
|
|
privKey, err := connect.ParseSigner(providerState.PrivateKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compare the two keys to make sure they match.
|
|
|
|
b1, err := x509.MarshalPKIXPublicKey(intermediate.PublicKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
b2, err := x509.MarshalPKIXPublicKey(privKey.Public())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !bytes.Equal(b1, b2) {
|
|
|
|
return fmt.Errorf("intermediate cert is for a different private key")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate the remaining fields and make sure the intermediate validates against
|
|
|
|
// the given root cert.
|
|
|
|
if !intermediate.IsCA {
|
|
|
|
return fmt.Errorf("intermediate is not a CA certificate")
|
|
|
|
}
|
|
|
|
if got, want := intermediate.URIs[0].String(), c.spiffeID.URI().String(); got != want {
|
|
|
|
return fmt.Errorf("incoming cert URI %q does not match current URI: %q", got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
pool := x509.NewCertPool()
|
|
|
|
pool.AppendCertsFromPEM([]byte(rootPEM))
|
|
|
|
_, err = intermediate.Verify(x509.VerifyOptions{
|
|
|
|
Roots: pool,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not verify intermediate cert against root: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the state
|
|
|
|
newState := *providerState
|
|
|
|
newState.IntermediateCert = intermediatePEM
|
|
|
|
newState.RootCert = rootPEM
|
|
|
|
args := &structs.CARequest{
|
|
|
|
Op: structs.CAOpSetProviderState,
|
|
|
|
ProviderState: &newState,
|
|
|
|
}
|
|
|
|
if err := c.Delegate.ApplyCARequest(args); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-21 03:39:51 +00:00
|
|
|
// We aren't maintaining separate root/intermediate CAs for the builtin
|
|
|
|
// provider, so just return the root.
|
2018-05-09 22:12:31 +00:00
|
|
|
func (c *ConsulProvider) ActiveIntermediate() (string, error) {
|
2018-09-13 02:52:24 +00:00
|
|
|
if c.isRoot {
|
|
|
|
return c.ActiveRoot()
|
|
|
|
}
|
|
|
|
|
|
|
|
_, providerState, err := c.getState()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return providerState.IntermediateCert, nil
|
2018-04-20 08:30:34 +00:00
|
|
|
}
|
|
|
|
|
2018-04-21 03:39:51 +00:00
|
|
|
// We aren't maintaining separate root/intermediate CAs for the builtin
|
2018-06-13 08:40:03 +00:00
|
|
|
// provider, so just return the root.
|
2018-05-09 22:12:31 +00:00
|
|
|
func (c *ConsulProvider) GenerateIntermediate() (string, error) {
|
2018-06-13 08:40:03 +00:00
|
|
|
return c.ActiveIntermediate()
|
2018-04-20 08:30:34 +00:00
|
|
|
}
|
|
|
|
|
2018-04-21 01:46:02 +00:00
|
|
|
// Remove the state store entry for this provider instance.
|
2018-05-09 22:12:31 +00:00
|
|
|
func (c *ConsulProvider) Cleanup() error {
|
2018-04-21 01:46:02 +00:00
|
|
|
args := &structs.CARequest{
|
|
|
|
Op: structs.CAOpDeleteProviderState,
|
|
|
|
ProviderState: &structs.CAConsulProviderState{ID: c.id},
|
|
|
|
}
|
2018-09-11 23:43:04 +00:00
|
|
|
if err := c.Delegate.ApplyCARequest(args); err != nil {
|
2018-04-21 01:46:02 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-20 08:30:34 +00:00
|
|
|
// Sign returns a new certificate valid for the given SpiffeIDService
|
|
|
|
// using the current CA.
|
2018-05-09 22:12:31 +00:00
|
|
|
func (c *ConsulProvider) Sign(csr *x509.CertificateRequest) (string, error) {
|
2018-04-21 01:46:02 +00:00
|
|
|
// Lock during the signing so we don't use the same index twice
|
|
|
|
// for different cert serial numbers.
|
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
|
2018-04-20 08:30:34 +00:00
|
|
|
// Get the provider state
|
2018-09-11 23:43:04 +00:00
|
|
|
state := c.Delegate.State()
|
2018-05-04 23:01:38 +00:00
|
|
|
idx, providerState, err := state.CAProviderState(c.id)
|
2018-04-20 08:30:34 +00:00
|
|
|
if err != nil {
|
2018-04-24 23:31:42 +00:00
|
|
|
return "", err
|
2018-04-20 08:30:34 +00:00
|
|
|
}
|
|
|
|
|
2018-04-21 03:39:51 +00:00
|
|
|
// Create the keyId for the cert from the signing private key.
|
2018-04-20 08:30:34 +00:00
|
|
|
signer, err := connect.ParseSigner(providerState.PrivateKey)
|
|
|
|
if err != nil {
|
2018-04-24 23:31:42 +00:00
|
|
|
return "", err
|
2018-04-20 08:30:34 +00:00
|
|
|
}
|
|
|
|
if signer == nil {
|
2018-09-13 02:52:24 +00:00
|
|
|
return "", ErrNotInitialized
|
2018-04-20 08:30:34 +00:00
|
|
|
}
|
|
|
|
keyId, err := connect.KeyId(signer.Public())
|
|
|
|
if err != nil {
|
2018-04-24 23:31:42 +00:00
|
|
|
return "", err
|
2018-04-20 08:30:34 +00:00
|
|
|
}
|
|
|
|
|
2018-04-24 23:16:37 +00:00
|
|
|
// Parse the SPIFFE ID
|
|
|
|
spiffeId, err := connect.ParseCertURI(csr.URIs[0])
|
|
|
|
if err != nil {
|
2018-04-24 23:31:42 +00:00
|
|
|
return "", err
|
2018-04-24 23:16:37 +00:00
|
|
|
}
|
|
|
|
serviceId, ok := spiffeId.(*connect.SpiffeIDService)
|
|
|
|
if !ok {
|
2018-04-24 23:31:42 +00:00
|
|
|
return "", fmt.Errorf("SPIFFE ID in CSR must be a service ID")
|
2018-04-24 23:16:37 +00:00
|
|
|
}
|
|
|
|
|
2018-04-20 08:30:34 +00:00
|
|
|
// Parse the CA cert
|
2018-09-13 02:52:24 +00:00
|
|
|
certPEM, err := c.ActiveIntermediate()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
caCert, err := connect.ParseCert(certPEM)
|
2018-04-20 08:30:34 +00:00
|
|
|
if err != nil {
|
2018-04-24 23:31:42 +00:00
|
|
|
return "", fmt.Errorf("error parsing CA cert: %s", err)
|
2018-04-20 08:30:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cert template for generation
|
|
|
|
sn := &big.Int{}
|
2018-05-04 23:01:38 +00:00
|
|
|
sn.SetUint64(idx + 1)
|
2018-06-20 19:28:54 +00:00
|
|
|
// Sign the certificate valid from 1 minute in the past, this helps it be
|
|
|
|
// accepted right away even when nodes are not in close time sync accross the
|
|
|
|
// cluster. A minute is more than enough for typical DC clock drift.
|
|
|
|
effectiveNow := time.Now().Add(-1 * time.Minute)
|
2018-04-20 08:30:34 +00:00
|
|
|
template := x509.Certificate{
|
|
|
|
SerialNumber: sn,
|
|
|
|
Subject: pkix.Name{CommonName: serviceId.Service},
|
|
|
|
URIs: csr.URIs,
|
|
|
|
Signature: csr.Signature,
|
|
|
|
SignatureAlgorithm: csr.SignatureAlgorithm,
|
|
|
|
PublicKeyAlgorithm: csr.PublicKeyAlgorithm,
|
|
|
|
PublicKey: csr.PublicKey,
|
|
|
|
BasicConstraintsValid: true,
|
|
|
|
KeyUsage: x509.KeyUsageDataEncipherment |
|
|
|
|
x509.KeyUsageKeyAgreement |
|
|
|
|
x509.KeyUsageDigitalSignature |
|
|
|
|
x509.KeyUsageKeyEncipherment,
|
|
|
|
ExtKeyUsage: []x509.ExtKeyUsage{
|
|
|
|
x509.ExtKeyUsageClientAuth,
|
|
|
|
x509.ExtKeyUsageServerAuth,
|
|
|
|
},
|
2018-07-16 09:46:10 +00:00
|
|
|
NotAfter: effectiveNow.Add(c.config.LeafCertTTL),
|
2018-06-20 19:28:54 +00:00
|
|
|
NotBefore: effectiveNow,
|
2018-04-20 08:30:34 +00:00
|
|
|
AuthorityKeyId: keyId,
|
|
|
|
SubjectKeyId: keyId,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the certificate, PEM encode it and return that value.
|
|
|
|
var buf bytes.Buffer
|
|
|
|
bs, err := x509.CreateCertificate(
|
2018-04-30 21:23:49 +00:00
|
|
|
rand.Reader, &template, caCert, csr.PublicKey, signer)
|
2018-04-20 08:30:34 +00:00
|
|
|
if err != nil {
|
2018-04-24 23:31:42 +00:00
|
|
|
return "", fmt.Errorf("error generating certificate: %s", err)
|
2018-04-20 08:30:34 +00:00
|
|
|
}
|
|
|
|
err = pem.Encode(&buf, &pem.Block{Type: "CERTIFICATE", Bytes: bs})
|
|
|
|
if err != nil {
|
2018-05-09 16:15:29 +00:00
|
|
|
return "", fmt.Errorf("error encoding certificate: %s", err)
|
2018-04-20 08:30:34 +00:00
|
|
|
}
|
|
|
|
|
2018-05-04 23:01:38 +00:00
|
|
|
err = c.incrementProviderIndex(providerState)
|
2018-04-30 21:23:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2018-04-20 08:30:34 +00:00
|
|
|
|
|
|
|
// Set the response
|
2018-04-24 23:31:42 +00:00
|
|
|
return buf.String(), nil
|
2018-04-20 08:30:34 +00:00
|
|
|
}
|
|
|
|
|
2018-09-13 02:52:24 +00:00
|
|
|
// SignIntermediate will validate the CSR to ensure the trust domain in the
|
|
|
|
// URI SAN matches the local one and that basic constraints for a CA certificate
|
|
|
|
// are met. It should return a signed CA certificate with a path length constraint
|
|
|
|
// of 0 to ensure that the certificate cannot be used to generate further CA certs.
|
|
|
|
func (c *ConsulProvider) SignIntermediate(csr *x509.CertificateRequest) (string, error) {
|
|
|
|
idx, providerState, err := c.getState()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(csr.URIs) < 1 {
|
|
|
|
return "", fmt.Errorf("intermediate CSR has no URIs")
|
|
|
|
}
|
|
|
|
certURI, err := connect.ParseCertURI(csr.URIs[0])
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that the trust domain is valid
|
|
|
|
if !c.spiffeID.CanSign(certURI) {
|
|
|
|
return "", fmt.Errorf("incoming CSR domain %q is not valid for our domain %q",
|
|
|
|
certURI.URI().String(), c.spiffeID.URI().String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the signing private key.
|
|
|
|
signer, err := connect.ParseSigner(providerState.PrivateKey)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if signer == nil {
|
|
|
|
return "", ErrNotInitialized
|
|
|
|
}
|
|
|
|
subjectKeyId, err := connect.KeyId(csr.PublicKey)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the CA cert
|
|
|
|
caCert, err := connect.ParseCert(providerState.RootCert)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("error parsing CA cert: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cert template for generation
|
|
|
|
sn := &big.Int{}
|
|
|
|
sn.SetUint64(idx + 1)
|
|
|
|
// Sign the certificate valid from 1 minute in the past, this helps it be
|
|
|
|
// accepted right away even when nodes are not in close time sync accross the
|
|
|
|
// cluster. A minute is more than enough for typical DC clock drift.
|
|
|
|
effectiveNow := time.Now().Add(-1 * time.Minute)
|
|
|
|
template := x509.Certificate{
|
|
|
|
SerialNumber: sn,
|
|
|
|
Subject: csr.Subject,
|
|
|
|
URIs: csr.URIs,
|
|
|
|
Signature: csr.Signature,
|
|
|
|
SignatureAlgorithm: csr.SignatureAlgorithm,
|
|
|
|
PublicKeyAlgorithm: csr.PublicKeyAlgorithm,
|
|
|
|
PublicKey: csr.PublicKey,
|
|
|
|
BasicConstraintsValid: true,
|
|
|
|
KeyUsage: x509.KeyUsageCertSign |
|
|
|
|
x509.KeyUsageCRLSign |
|
|
|
|
x509.KeyUsageDigitalSignature,
|
|
|
|
IsCA: true,
|
|
|
|
MaxPathLenZero: true,
|
|
|
|
NotAfter: effectiveNow.Add(365 * 24 * time.Hour),
|
|
|
|
NotBefore: effectiveNow,
|
|
|
|
SubjectKeyId: subjectKeyId,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the certificate, PEM encode it and return that value.
|
|
|
|
var buf bytes.Buffer
|
|
|
|
bs, err := x509.CreateCertificate(
|
|
|
|
rand.Reader, &template, caCert, csr.PublicKey, signer)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("error generating certificate: %s", err)
|
|
|
|
}
|
|
|
|
err = pem.Encode(&buf, &pem.Block{Type: "CERTIFICATE", Bytes: bs})
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("error encoding certificate: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.incrementProviderIndex(providerState)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the response
|
|
|
|
return buf.String(), nil
|
|
|
|
}
|
|
|
|
|
2018-06-19 23:46:18 +00:00
|
|
|
// CrossSignCA returns the given CA cert signed by the current active root.
|
|
|
|
func (c *ConsulProvider) CrossSignCA(cert *x509.Certificate) (string, error) {
|
2018-04-21 03:39:51 +00:00
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
|
|
|
|
// Get the provider state
|
2018-09-11 23:43:04 +00:00
|
|
|
state := c.Delegate.State()
|
2018-05-04 23:01:38 +00:00
|
|
|
idx, providerState, err := state.CAProviderState(c.id)
|
2018-04-21 03:39:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
privKey, err := connect.ParseSigner(providerState.PrivateKey)
|
|
|
|
if err != nil {
|
2018-04-25 18:34:08 +00:00
|
|
|
return "", fmt.Errorf("error parsing private key %q: %s", providerState.PrivateKey, err)
|
2018-04-21 03:39:51 +00:00
|
|
|
}
|
|
|
|
|
2018-04-24 23:16:37 +00:00
|
|
|
rootCA, err := connect.ParseCert(providerState.RootCert)
|
2018-04-21 03:39:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2018-04-24 23:16:37 +00:00
|
|
|
|
2018-06-19 23:46:18 +00:00
|
|
|
keyId, err := connect.KeyId(privKey.Public())
|
2018-04-21 03:39:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2018-04-24 23:16:37 +00:00
|
|
|
// Create the cross-signing template from the existing root CA
|
2018-04-21 03:39:51 +00:00
|
|
|
serialNum := &big.Int{}
|
2018-05-04 23:01:38 +00:00
|
|
|
serialNum.SetUint64(idx + 1)
|
2018-06-19 23:46:18 +00:00
|
|
|
template := *cert
|
|
|
|
template.SerialNumber = serialNum
|
|
|
|
template.SignatureAlgorithm = rootCA.SignatureAlgorithm
|
|
|
|
template.AuthorityKeyId = keyId
|
2018-04-21 03:39:51 +00:00
|
|
|
|
2018-06-20 19:28:54 +00:00
|
|
|
// Sign the certificate valid from 1 minute in the past, this helps it be
|
|
|
|
// accepted right away even when nodes are not in close time sync accross the
|
|
|
|
// cluster. A minute is more than enough for typical DC clock drift.
|
|
|
|
effectiveNow := time.Now().Add(-1 * time.Minute)
|
|
|
|
template.NotBefore = effectiveNow
|
|
|
|
// This cross-signed cert is only needed during rotation, and only while old
|
|
|
|
// leaf certs are still in use. They expire within 3 days currently so 7 is
|
|
|
|
// safe. TODO(banks): make this be based on leaf expiry time when that is
|
|
|
|
// configurable.
|
|
|
|
template.NotAfter = effectiveNow.Add(7 * 24 * time.Hour)
|
|
|
|
|
2018-04-21 03:39:51 +00:00
|
|
|
bs, err := x509.CreateCertificate(
|
2018-06-19 23:46:18 +00:00
|
|
|
rand.Reader, &template, rootCA, cert.PublicKey, privKey)
|
2018-04-21 03:39:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("error generating CA certificate: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
err = pem.Encode(&buf, &pem.Block{Type: "CERTIFICATE", Bytes: bs})
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("error encoding private key: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-05-04 23:01:38 +00:00
|
|
|
err = c.incrementProviderIndex(providerState)
|
2018-04-30 21:23:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2018-04-27 03:14:37 +00:00
|
|
|
|
2018-04-21 03:39:51 +00:00
|
|
|
return buf.String(), nil
|
|
|
|
}
|
|
|
|
|
2018-09-13 02:52:24 +00:00
|
|
|
// getState returns the current provider state from the state delegate, and returns
|
|
|
|
// ErrNotInitialized if no entry is found.
|
|
|
|
func (c *ConsulProvider) getState() (uint64, *structs.CAConsulProviderState, error) {
|
|
|
|
state := c.Delegate.State()
|
|
|
|
idx, providerState, err := state.CAProviderState(c.id)
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if providerState == nil {
|
|
|
|
return 0, nil, ErrNotInitialized
|
|
|
|
}
|
|
|
|
|
|
|
|
return idx, providerState, nil
|
|
|
|
}
|
|
|
|
|
2018-05-04 23:01:38 +00:00
|
|
|
// incrementProviderIndex does a write to increment the provider state store table index
|
|
|
|
// used for serial numbers when generating certificates.
|
2018-05-09 22:12:31 +00:00
|
|
|
func (c *ConsulProvider) incrementProviderIndex(providerState *structs.CAConsulProviderState) error {
|
2018-04-27 03:14:37 +00:00
|
|
|
newState := *providerState
|
|
|
|
args := &structs.CARequest{
|
|
|
|
Op: structs.CAOpSetProviderState,
|
|
|
|
ProviderState: &newState,
|
|
|
|
}
|
2018-09-11 23:43:04 +00:00
|
|
|
if err := c.Delegate.ApplyCARequest(args); err != nil {
|
2018-04-27 03:14:37 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-20 08:30:34 +00:00
|
|
|
// generateCA makes a new root CA using the current private key
|
2018-05-09 22:12:31 +00:00
|
|
|
func (c *ConsulProvider) generateCA(privateKey string, sn uint64) (string, error) {
|
2018-09-11 23:43:04 +00:00
|
|
|
state := c.Delegate.State()
|
2018-04-20 08:30:34 +00:00
|
|
|
_, config, err := state.CAConfig()
|
|
|
|
if err != nil {
|
2018-04-24 23:16:37 +00:00
|
|
|
return "", err
|
2018-04-20 08:30:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
privKey, err := connect.ParseSigner(privateKey)
|
|
|
|
if err != nil {
|
2018-04-25 18:34:08 +00:00
|
|
|
return "", fmt.Errorf("error parsing private key %q: %s", privateKey, err)
|
2018-04-20 08:30:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
name := fmt.Sprintf("Consul CA %d", sn)
|
|
|
|
|
2018-04-24 23:16:37 +00:00
|
|
|
// The URI (SPIFFE compatible) for the cert
|
2018-05-08 13:23:44 +00:00
|
|
|
id := connect.SpiffeIDSigningForCluster(config)
|
2018-04-24 23:16:37 +00:00
|
|
|
keyId, err := connect.KeyId(privKey.Public())
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2018-04-21 01:46:02 +00:00
|
|
|
|
2018-04-24 23:16:37 +00:00
|
|
|
// Create the CA cert
|
|
|
|
serialNum := &big.Int{}
|
|
|
|
serialNum.SetUint64(sn)
|
|
|
|
template := x509.Certificate{
|
2018-09-12 20:44:15 +00:00
|
|
|
SerialNumber: serialNum,
|
|
|
|
Subject: pkix.Name{CommonName: name},
|
|
|
|
URIs: []*url.URL{id.URI()},
|
2018-06-21 16:40:56 +00:00
|
|
|
BasicConstraintsValid: true,
|
2018-04-24 23:16:37 +00:00
|
|
|
KeyUsage: x509.KeyUsageCertSign |
|
|
|
|
x509.KeyUsageCRLSign |
|
|
|
|
x509.KeyUsageDigitalSignature,
|
|
|
|
IsCA: true,
|
|
|
|
NotAfter: time.Now().Add(10 * 365 * 24 * time.Hour),
|
|
|
|
NotBefore: time.Now(),
|
|
|
|
AuthorityKeyId: keyId,
|
|
|
|
SubjectKeyId: keyId,
|
|
|
|
}
|
2018-04-21 01:46:02 +00:00
|
|
|
|
2018-04-24 23:16:37 +00:00
|
|
|
bs, err := x509.CreateCertificate(
|
|
|
|
rand.Reader, &template, &template, privKey.Public(), privKey)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("error generating CA certificate: %s", err)
|
2018-04-20 08:30:34 +00:00
|
|
|
}
|
|
|
|
|
2018-04-24 23:16:37 +00:00
|
|
|
var buf bytes.Buffer
|
|
|
|
err = pem.Encode(&buf, &pem.Block{Type: "CERTIFICATE", Bytes: bs})
|
2018-04-20 08:30:34 +00:00
|
|
|
if err != nil {
|
2018-04-24 23:16:37 +00:00
|
|
|
return "", fmt.Errorf("error encoding private key: %s", err)
|
2018-04-20 08:30:34 +00:00
|
|
|
}
|
|
|
|
|
2018-04-24 23:16:37 +00:00
|
|
|
return buf.String(), nil
|
2018-04-20 08:30:34 +00:00
|
|
|
}
|