2022-01-14 17:56:17 +00:00
|
|
|
package vaultcas
|
|
|
|
|
|
|
|
import (
|
2022-04-12 01:44:13 +00:00
|
|
|
"bytes"
|
2022-01-14 17:56:17 +00:00
|
|
|
"context"
|
2022-01-20 09:16:47 +00:00
|
|
|
"crypto/sha256"
|
2022-01-14 17:56:17 +00:00
|
|
|
"crypto/x509"
|
2022-04-12 01:44:13 +00:00
|
|
|
"encoding/hex"
|
2022-01-20 09:16:47 +00:00
|
|
|
"encoding/json"
|
2022-01-14 17:56:17 +00:00
|
|
|
"encoding/pem"
|
2022-04-12 21:04:46 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2022-01-20 09:16:47 +00:00
|
|
|
"math/big"
|
2022-04-12 01:44:13 +00:00
|
|
|
"strings"
|
2022-01-14 17:56:17 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/smallstep/certificates/cas/apiv1"
|
2022-05-09 11:27:37 +00:00
|
|
|
"github.com/smallstep/certificates/cas/vaultcas/auth/approle"
|
|
|
|
"github.com/smallstep/certificates/cas/vaultcas/auth/kubernetes"
|
2022-01-14 17:56:17 +00:00
|
|
|
|
|
|
|
vault "github.com/hashicorp/vault/api"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
apiv1.Register(apiv1.VaultCAS, func(ctx context.Context, opts apiv1.Options) (apiv1.CertificateAuthorityService, error) {
|
|
|
|
return New(ctx, opts)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-04-12 18:15:28 +00:00
|
|
|
// VaultOptions defines the configuration options added using the
|
|
|
|
// apiv1.Options.Config field.
|
2022-01-14 17:56:17 +00:00
|
|
|
type VaultOptions struct {
|
2022-05-09 11:27:37 +00:00
|
|
|
PKIMountPath string `json:"pkiMountPath,omitempty"`
|
|
|
|
PKIRoleDefault string `json:"pkiRoleDefault,omitempty"`
|
|
|
|
PKIRoleRSA string `json:"pkiRoleRSA,omitempty"`
|
|
|
|
PKIRoleEC string `json:"pkiRoleEC,omitempty"`
|
|
|
|
PKIRoleEd25519 string `json:"pkiRoleEd25519,omitempty"`
|
|
|
|
AuthType string `json:"authType,omitempty"`
|
|
|
|
AuthMountPath string `json:"authMountPath,omitempty"`
|
2023-06-06 22:07:11 +00:00
|
|
|
Namespace string `json:"namespace,omitempty"`
|
2022-05-09 11:27:37 +00:00
|
|
|
AuthOptions json.RawMessage `json:"authOptions,omitempty"`
|
2022-01-14 17:56:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// VaultCAS implements a Certificate Authority Service using Hashicorp Vault.
|
|
|
|
type VaultCAS struct {
|
|
|
|
client *vault.Client
|
|
|
|
config VaultOptions
|
|
|
|
fingerprint string
|
|
|
|
}
|
|
|
|
|
2022-04-12 01:44:13 +00:00
|
|
|
type certBundle struct {
|
2022-02-06 22:29:49 +00:00
|
|
|
leaf *x509.Certificate
|
|
|
|
intermediates []*x509.Certificate
|
|
|
|
root *x509.Certificate
|
|
|
|
}
|
|
|
|
|
2022-01-14 17:56:17 +00:00
|
|
|
// New creates a new CertificateAuthorityService implementation
|
|
|
|
// using Hashicorp Vault
|
|
|
|
func New(ctx context.Context, opts apiv1.Options) (*VaultCAS, error) {
|
|
|
|
if opts.CertificateAuthority == "" {
|
|
|
|
return nil, errors.New("vaultCAS 'certificateAuthority' cannot be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.CertificateAuthorityFingerprint == "" {
|
|
|
|
return nil, errors.New("vaultCAS 'certificateAuthorityFingerprint' cannot be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
vc, err := loadOptions(opts.Config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
config := vault.DefaultConfig()
|
|
|
|
config.Address = opts.CertificateAuthority
|
|
|
|
|
|
|
|
client, err := vault.NewClient(config)
|
|
|
|
if err != nil {
|
2022-04-12 21:04:46 +00:00
|
|
|
return nil, fmt.Errorf("unable to initialize vault client: %w", err)
|
2022-01-14 17:56:17 +00:00
|
|
|
}
|
|
|
|
|
2022-05-09 11:27:37 +00:00
|
|
|
var method vault.AuthMethod
|
|
|
|
switch vc.AuthType {
|
|
|
|
case "kubernetes":
|
|
|
|
method, err = kubernetes.NewKubernetesAuthMethod(vc.AuthMountPath, vc.AuthOptions)
|
|
|
|
case "approle":
|
|
|
|
method, err = approle.NewApproleAuthMethod(vc.AuthMountPath, vc.AuthOptions)
|
|
|
|
default:
|
2022-05-21 19:00:50 +00:00
|
|
|
return nil, fmt.Errorf("unknown auth type: %s, only 'kubernetes' and 'approle' currently supported", vc.AuthType)
|
2022-05-09 11:27:37 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
2022-05-21 19:00:50 +00:00
|
|
|
return nil, fmt.Errorf("unable to configure %s auth method: %w", vc.AuthType, err)
|
2022-05-09 11:27:37 +00:00
|
|
|
}
|
2022-01-14 17:56:17 +00:00
|
|
|
|
2023-06-06 22:07:11 +00:00
|
|
|
if vc.Namespace != "" {
|
|
|
|
client.SetNamespace(vc.Namespace)
|
|
|
|
}
|
|
|
|
|
2022-05-09 11:27:37 +00:00
|
|
|
authInfo, err := client.Auth().Login(ctx, method)
|
|
|
|
if err != nil {
|
2022-05-21 19:00:50 +00:00
|
|
|
return nil, fmt.Errorf("unable to login to %s auth method: %w", vc.AuthType, err)
|
2022-05-09 11:27:37 +00:00
|
|
|
}
|
|
|
|
if authInfo == nil {
|
|
|
|
return nil, errors.New("no auth info was returned after login")
|
2022-01-14 17:56:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &VaultCAS{
|
|
|
|
client: client,
|
2022-01-27 10:14:19 +00:00
|
|
|
config: *vc,
|
2022-01-14 17:56:17 +00:00
|
|
|
fingerprint: opts.CertificateAuthorityFingerprint,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateCertificate signs a new certificate using Hashicorp Vault.
|
|
|
|
func (v *VaultCAS) CreateCertificate(req *apiv1.CreateCertificateRequest) (*apiv1.CreateCertificateResponse, error) {
|
|
|
|
switch {
|
|
|
|
case req.CSR == nil:
|
2022-04-12 01:44:13 +00:00
|
|
|
return nil, errors.New("createCertificate `csr` cannot be nil")
|
2022-01-14 17:56:17 +00:00
|
|
|
case req.Lifetime == 0:
|
2022-04-12 01:44:13 +00:00
|
|
|
return nil, errors.New("createCertificate `lifetime` cannot be 0")
|
2022-01-14 17:56:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cert, chain, err := v.createCertificate(req.CSR, req.Lifetime)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &apiv1.CreateCertificateResponse{
|
|
|
|
Certificate: cert,
|
|
|
|
CertificateChain: chain,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-04-12 18:15:28 +00:00
|
|
|
// GetCertificateAuthority returns the root certificate of the certificate
|
|
|
|
// authority using the configured fingerprint.
|
2023-05-10 06:47:28 +00:00
|
|
|
func (v *VaultCAS) GetCertificateAuthority(*apiv1.GetCertificateAuthorityRequest) (*apiv1.GetCertificateAuthorityResponse, error) {
|
2022-05-09 11:27:37 +00:00
|
|
|
secret, err := v.client.Logical().Read(v.config.PKIMountPath + "/cert/ca_chain")
|
2022-01-14 17:56:17 +00:00
|
|
|
if err != nil {
|
2022-04-12 21:04:46 +00:00
|
|
|
return nil, fmt.Errorf("error reading ca chain: %w", err)
|
2022-01-14 17:56:17 +00:00
|
|
|
}
|
|
|
|
if secret == nil {
|
2022-04-12 01:44:13 +00:00
|
|
|
return nil, errors.New("error reading ca chain: response is empty")
|
2022-01-14 17:56:17 +00:00
|
|
|
}
|
|
|
|
|
2022-04-12 01:44:13 +00:00
|
|
|
chain, ok := secret.Data["certificate"].(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("error unmarshaling vault response: certificate not found")
|
2022-01-14 17:56:17 +00:00
|
|
|
}
|
|
|
|
|
2022-04-12 01:44:13 +00:00
|
|
|
cert, err := getCertificateBundle(chain)
|
2022-01-14 17:56:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-04-12 01:44:13 +00:00
|
|
|
if cert.root == nil {
|
|
|
|
return nil, errors.New("error unmarshaling vault response: root certificate not found")
|
|
|
|
}
|
2022-01-20 09:16:47 +00:00
|
|
|
|
2022-04-12 01:44:13 +00:00
|
|
|
sum := sha256.Sum256(cert.root.Raw)
|
|
|
|
if !strings.EqualFold(v.fingerprint, strings.ToLower(hex.EncodeToString(sum[:]))) {
|
|
|
|
return nil, errors.New("error verifying vault root: fingerprint does not match")
|
2022-01-20 09:16:47 +00:00
|
|
|
}
|
|
|
|
|
2022-01-14 17:56:17 +00:00
|
|
|
return &apiv1.GetCertificateAuthorityResponse{
|
2022-02-06 22:29:49 +00:00
|
|
|
RootCertificate: cert.root,
|
2022-01-14 17:56:17 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RenewCertificate will always return a non-implemented error as renewals
|
|
|
|
// are not supported yet.
|
2023-05-10 06:47:28 +00:00
|
|
|
func (v *VaultCAS) RenewCertificate(*apiv1.RenewCertificateRequest) (*apiv1.RenewCertificateResponse, error) {
|
2022-09-20 22:54:59 +00:00
|
|
|
return nil, apiv1.NotImplementedError{Message: "vaultCAS does not support renewals"}
|
2022-01-14 17:56:17 +00:00
|
|
|
}
|
|
|
|
|
2022-04-12 01:44:13 +00:00
|
|
|
// RevokeCertificate revokes a certificate by serial number.
|
2022-01-14 17:56:17 +00:00
|
|
|
func (v *VaultCAS) RevokeCertificate(req *apiv1.RevokeCertificateRequest) (*apiv1.RevokeCertificateResponse, error) {
|
|
|
|
if req.SerialNumber == "" && req.Certificate == nil {
|
2022-04-12 01:44:13 +00:00
|
|
|
return nil, errors.New("revokeCertificate `serialNumber` or `certificate` are required")
|
2022-01-14 17:56:17 +00:00
|
|
|
}
|
|
|
|
|
2022-04-12 01:44:13 +00:00
|
|
|
var sn *big.Int
|
2022-01-20 09:16:47 +00:00
|
|
|
if req.SerialNumber != "" {
|
2022-04-12 01:44:13 +00:00
|
|
|
var ok bool
|
|
|
|
if sn, ok = new(big.Int).SetString(req.SerialNumber, 10); !ok {
|
2022-04-12 21:04:46 +00:00
|
|
|
return nil, fmt.Errorf("error parsing serialNumber: %v cannot be converted to big.Int", req.SerialNumber)
|
2022-01-20 09:16:47 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-04-12 01:44:13 +00:00
|
|
|
sn = req.Certificate.SerialNumber
|
2022-01-14 17:56:17 +00:00
|
|
|
}
|
|
|
|
|
2022-04-12 01:44:13 +00:00
|
|
|
vaultReq := map[string]interface{}{
|
|
|
|
"serial_number": formatSerialNumber(sn),
|
2022-01-27 10:14:19 +00:00
|
|
|
}
|
2022-05-09 11:27:37 +00:00
|
|
|
_, err := v.client.Logical().Write(v.config.PKIMountPath+"/revoke/", vaultReq)
|
2022-01-14 17:56:17 +00:00
|
|
|
if err != nil {
|
2022-04-12 21:04:46 +00:00
|
|
|
return nil, fmt.Errorf("error revoking certificate: %w", err)
|
2022-01-14 17:56:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &apiv1.RevokeCertificateResponse{
|
|
|
|
Certificate: req.Certificate,
|
|
|
|
CertificateChain: nil,
|
|
|
|
}, nil
|
|
|
|
}
|
2022-02-17 01:17:32 +00:00
|
|
|
|
2022-04-12 01:44:13 +00:00
|
|
|
func (v *VaultCAS) createCertificate(cr *x509.CertificateRequest, lifetime time.Duration) (*x509.Certificate, []*x509.Certificate, error) {
|
|
|
|
var vaultPKIRole string
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case cr.PublicKeyAlgorithm == x509.RSA:
|
|
|
|
vaultPKIRole = v.config.PKIRoleRSA
|
|
|
|
case cr.PublicKeyAlgorithm == x509.ECDSA:
|
|
|
|
vaultPKIRole = v.config.PKIRoleEC
|
|
|
|
case cr.PublicKeyAlgorithm == x509.Ed25519:
|
|
|
|
vaultPKIRole = v.config.PKIRoleEd25519
|
|
|
|
default:
|
2022-04-12 21:04:46 +00:00
|
|
|
return nil, nil, fmt.Errorf("unsupported public key algorithm %v", cr.PublicKeyAlgorithm)
|
2022-04-12 01:44:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
vaultReq := map[string]interface{}{
|
|
|
|
"csr": string(pem.EncodeToMemory(&pem.Block{
|
|
|
|
Type: "CERTIFICATE REQUEST",
|
|
|
|
Bytes: cr.Raw,
|
|
|
|
})),
|
|
|
|
"format": "pem_bundle",
|
2023-05-05 01:30:09 +00:00
|
|
|
"ttl": lifetime.String(),
|
2022-04-12 01:44:13 +00:00
|
|
|
}
|
|
|
|
|
2022-05-09 11:27:37 +00:00
|
|
|
secret, err := v.client.Logical().Write(v.config.PKIMountPath+"/sign/"+vaultPKIRole, vaultReq)
|
2022-02-17 01:17:32 +00:00
|
|
|
if err != nil {
|
2022-04-12 21:04:46 +00:00
|
|
|
return nil, nil, fmt.Errorf("error signing certificate: %w", err)
|
2022-04-12 01:44:13 +00:00
|
|
|
}
|
|
|
|
if secret == nil {
|
|
|
|
return nil, nil, errors.New("error signing certificate: response is empty")
|
2022-02-17 01:17:32 +00:00
|
|
|
}
|
|
|
|
|
2022-04-12 01:44:13 +00:00
|
|
|
chain, ok := secret.Data["certificate"].(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, nil, errors.New("error unmarshaling vault response: certificate not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
cert, err := getCertificateBundle(chain)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return certificate and certificate chain
|
|
|
|
return cert.leaf, cert.intermediates, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadOptions(config json.RawMessage) (*VaultOptions, error) {
|
2022-05-09 11:27:37 +00:00
|
|
|
// setup default values
|
|
|
|
vc := VaultOptions{
|
|
|
|
PKIMountPath: "pki",
|
|
|
|
PKIRoleDefault: "default",
|
|
|
|
}
|
2022-04-12 01:44:13 +00:00
|
|
|
|
|
|
|
err := json.Unmarshal(config, &vc)
|
|
|
|
if err != nil {
|
2022-04-12 21:04:46 +00:00
|
|
|
return nil, fmt.Errorf("error decoding vaultCAS config: %w", err)
|
2022-04-12 01:44:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if vc.PKIRoleRSA == "" {
|
|
|
|
vc.PKIRoleRSA = vc.PKIRoleDefault
|
|
|
|
}
|
|
|
|
if vc.PKIRoleEC == "" {
|
|
|
|
vc.PKIRoleEC = vc.PKIRoleDefault
|
|
|
|
}
|
|
|
|
if vc.PKIRoleEd25519 == "" {
|
|
|
|
vc.PKIRoleEd25519 = vc.PKIRoleDefault
|
|
|
|
}
|
|
|
|
|
2022-05-09 11:27:37 +00:00
|
|
|
return &vc, nil
|
2022-04-12 01:44:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func parseCertificates(pemCert string) []*x509.Certificate {
|
|
|
|
var certs []*x509.Certificate
|
|
|
|
rest := []byte(pemCert)
|
|
|
|
var block *pem.Block
|
|
|
|
for {
|
|
|
|
block, rest = pem.Decode(rest)
|
|
|
|
if block == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
cert, err := x509.ParseCertificate(block.Bytes)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
certs = append(certs, cert)
|
|
|
|
}
|
|
|
|
return certs
|
|
|
|
}
|
|
|
|
|
|
|
|
func getCertificateBundle(chain string) (*certBundle, error) {
|
|
|
|
var root *x509.Certificate
|
|
|
|
var leaf *x509.Certificate
|
|
|
|
var intermediates []*x509.Certificate
|
|
|
|
for _, cert := range parseCertificates(chain) {
|
|
|
|
switch {
|
|
|
|
case isRoot(cert):
|
|
|
|
root = cert
|
|
|
|
case cert.BasicConstraintsValid && cert.IsCA:
|
|
|
|
intermediates = append(intermediates, cert)
|
|
|
|
default:
|
|
|
|
leaf = cert
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
certificate := &certBundle{
|
|
|
|
root: root,
|
|
|
|
leaf: leaf,
|
|
|
|
intermediates: intermediates,
|
|
|
|
}
|
|
|
|
|
|
|
|
return certificate, nil
|
2022-02-17 01:17:32 +00:00
|
|
|
}
|
2022-02-17 02:09:20 +00:00
|
|
|
|
|
|
|
// isRoot returns true if the given certificate is a root certificate.
|
|
|
|
func isRoot(cert *x509.Certificate) bool {
|
|
|
|
if cert.BasicConstraintsValid && cert.IsCA {
|
|
|
|
return cert.CheckSignatureFrom(cert) == nil
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2022-04-12 01:44:13 +00:00
|
|
|
|
|
|
|
// formatSerialNumber formats a serial number to a dash-separated hexadecimal
|
|
|
|
// string.
|
|
|
|
func formatSerialNumber(sn *big.Int) string {
|
|
|
|
var ret bytes.Buffer
|
|
|
|
for _, b := range sn.Bytes() {
|
|
|
|
if ret.Len() > 0 {
|
|
|
|
ret.WriteString("-")
|
|
|
|
}
|
|
|
|
ret.WriteString(hex.EncodeToString([]byte{b}))
|
|
|
|
}
|
|
|
|
return ret.String()
|
|
|
|
}
|