2022-01-14 17:56:17 +00:00
|
|
|
package vaultcas
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-01-20 09:16:47 +00:00
|
|
|
"crypto/sha256"
|
2022-01-14 17:56:17 +00:00
|
|
|
"crypto/x509"
|
2022-01-20 09:16:47 +00:00
|
|
|
"encoding/json"
|
2022-01-14 17:56:17 +00:00
|
|
|
"encoding/pem"
|
2022-01-20 09:16:47 +00:00
|
|
|
"math/big"
|
2022-01-14 17:56:17 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/smallstep/certificates/api"
|
|
|
|
"github.com/smallstep/certificates/cas/apiv1"
|
|
|
|
|
|
|
|
vault "github.com/hashicorp/vault/api"
|
|
|
|
auth "github.com/hashicorp/vault/api/auth/approle"
|
|
|
|
certutil "github.com/hashicorp/vault/sdk/helper/certutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
apiv1.Register(apiv1.VaultCAS, func(ctx context.Context, opts apiv1.Options) (apiv1.CertificateAuthorityService, error) {
|
|
|
|
return New(ctx, opts)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type VaultOptions struct {
|
2022-01-20 09:16:47 +00:00
|
|
|
PKI string `json:"pki,omitempty"`
|
|
|
|
PKIRole string `json:"pkiRole,omitempty"`
|
|
|
|
PKIRoleRSA string `json:"pkiRoleRSA,omitempty"`
|
|
|
|
PKIRoleEC string `json:"pkiRoleEC,omitempty"`
|
|
|
|
PKIRoleED25519 string `json:"PKIRoleED25519,omitempty"`
|
|
|
|
RoleID string `json:"roleID,omitempty"`
|
|
|
|
SecretID auth.SecretID `json:"secretID,omitempty"`
|
|
|
|
AppRole string `json:"appRole,omitempty"`
|
|
|
|
IsWrappingToken bool `json:"isWrappingToken,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-01-20 09:16:47 +00:00
|
|
|
func loadOptions(config json.RawMessage) (vc VaultOptions, err error) {
|
|
|
|
err = json.Unmarshal(config, &vc)
|
2022-01-14 17:56:17 +00:00
|
|
|
if err != nil {
|
2022-01-20 09:16:47 +00:00
|
|
|
return vc, errors.Wrap(err, "error decoding vaultCAS config")
|
2022-01-14 17:56:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if vc.PKI == "" {
|
|
|
|
vc.PKI = "pki" // use default pki vault name
|
|
|
|
}
|
|
|
|
|
|
|
|
// pkirole or per key type must be defined
|
|
|
|
if vc.PKIRole == "" && vc.PKIRoleRSA == "" && vc.PKIRoleEC == "" && vc.PKIRoleED25519 == "" {
|
2022-01-20 09:16:47 +00:00
|
|
|
return vc, errors.New("vaultCAS config options must define `pkiRole`")
|
2022-01-14 17:56:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// if pkirole is empty all others keys must be set
|
|
|
|
if vc.PKIRole == "" && (vc.PKIRoleRSA == "" || vc.PKIRoleEC == "" || vc.PKIRoleED25519 == "") {
|
2022-01-20 09:16:47 +00:00
|
|
|
return vc, errors.New("vaultCAS config options must include a `pkiRole` or `pkiRoleRSA`, `pkiRoleEC` and `pkiRoleEd25519`")
|
2022-01-14 17:56:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// if pkirole is not empty, use it as default for unset keys
|
|
|
|
if vc.PKIRole != "" {
|
|
|
|
if vc.PKIRoleRSA == "" {
|
|
|
|
vc.PKIRoleRSA = vc.PKIRole
|
|
|
|
}
|
|
|
|
if vc.PKIRoleEC == "" {
|
|
|
|
vc.PKIRoleEC = vc.PKIRole
|
|
|
|
}
|
|
|
|
if vc.PKIRoleED25519 == "" {
|
|
|
|
vc.PKIRoleED25519 = vc.PKIRole
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if vc.RoleID == "" {
|
2022-01-20 09:16:47 +00:00
|
|
|
return vc, errors.New("vaultCAS config options must define `roleID`")
|
2022-01-14 17:56:17 +00:00
|
|
|
}
|
|
|
|
|
2022-01-20 09:16:47 +00:00
|
|
|
if vc.SecretID.FromEnv == "" && vc.SecretID.FromFile == "" && vc.SecretID.FromString == "" {
|
|
|
|
return vc, errors.New("vaultCAS config options must define `secretID` object with one of `FromEnv`, `FromFile` or `FromString`")
|
|
|
|
}
|
|
|
|
|
|
|
|
if vc.PKI == "" {
|
|
|
|
vc.PKI = "pki" // use default pki vault name
|
2022-01-14 17:56:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if vc.AppRole == "" {
|
|
|
|
vc.AppRole = "auth/approle"
|
|
|
|
}
|
|
|
|
|
|
|
|
return vc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getCertificateAndChain(certb certutil.CertBundle) (*x509.Certificate, []*x509.Certificate, error) {
|
|
|
|
cert, err := parseCertificate(certb.Certificate)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
chain := make([]*x509.Certificate, len(certb.CAChain))
|
|
|
|
for i := range certb.CAChain {
|
|
|
|
chain[i], err = parseCertificate(certb.CAChain[i])
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cert, chain, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseCertificate(pemCert string) (*x509.Certificate, error) {
|
|
|
|
block, _ := pem.Decode([]byte(pemCert))
|
|
|
|
if block == nil {
|
2022-01-20 09:16:47 +00:00
|
|
|
return nil, errors.Errorf("error decoding certificate: not a valid PEM encoded block, please verify\r\n%v", pemCert)
|
2022-01-14 17:56:17 +00:00
|
|
|
}
|
|
|
|
cert, err := x509.ParseCertificate(block.Bytes)
|
|
|
|
if err != nil {
|
2022-01-20 09:16:47 +00:00
|
|
|
return nil, errors.Wrap(err, "error parsing certificate")
|
2022-01-14 17:56:17 +00:00
|
|
|
}
|
|
|
|
return cert, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseCertificateRequest(pemCsr string) (*x509.CertificateRequest, error) {
|
|
|
|
block, _ := pem.Decode([]byte(pemCsr))
|
|
|
|
if block == nil {
|
2022-01-20 09:16:47 +00:00
|
|
|
return nil, errors.Errorf("error decoding certificate request: not a valid PEM encoded block, please verify\r\n%v", pemCsr)
|
2022-01-14 17:56:17 +00:00
|
|
|
}
|
|
|
|
cr, err := x509.ParseCertificateRequest(block.Bytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error parsing certificate request")
|
|
|
|
}
|
|
|
|
return cr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *VaultCAS) createCertificate(cr *x509.CertificateRequest, lifetime time.Duration) (*x509.Certificate, []*x509.Certificate, error) {
|
|
|
|
var vaultPKIRole string
|
|
|
|
csr := api.CertificateRequest{CertificateRequest: cr}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case csr.PublicKeyAlgorithm == x509.RSA:
|
|
|
|
vaultPKIRole = v.config.PKIRoleRSA
|
|
|
|
case csr.PublicKeyAlgorithm == x509.ECDSA:
|
|
|
|
vaultPKIRole = v.config.PKIRoleEC
|
|
|
|
case csr.PublicKeyAlgorithm == x509.Ed25519:
|
|
|
|
vaultPKIRole = v.config.PKIRoleED25519
|
|
|
|
default:
|
|
|
|
return nil, nil, errors.Errorf("createCertificate: Unsupported public key algorithm '%v'", csr.PublicKeyAlgorithm)
|
|
|
|
}
|
|
|
|
|
|
|
|
certPemBytes := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE REQUEST", Bytes: csr.Raw})
|
|
|
|
if certPemBytes == nil {
|
|
|
|
return nil, nil, errors.Errorf("createCertificate: Failed to encode pem '%v'", csr.Raw)
|
|
|
|
}
|
|
|
|
|
|
|
|
y := map[string]interface{}{
|
|
|
|
"csr": string(certPemBytes),
|
|
|
|
"format": "pem_bundle",
|
|
|
|
"ttl": lifetime.Seconds(),
|
|
|
|
}
|
|
|
|
|
|
|
|
secret, err := v.client.Logical().Write(v.config.PKI+"/sign/"+vaultPKIRole, y)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, errors.Wrapf(err, "createCertificate: unable to sign certificate %v", y)
|
|
|
|
}
|
|
|
|
if secret == nil {
|
|
|
|
return nil, nil, errors.New("createCertificate: secret sign is empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
var certBundle certutil.CertBundle
|
|
|
|
|
2022-01-20 09:16:47 +00:00
|
|
|
secretData, err := json.Marshal(secret.Data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(secretData, &certBundle)
|
2022-01-14 17:56:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return certificate and certificate chain
|
|
|
|
return getCertificateAndChain(certBundle)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
return nil, errors.Wrap(err, "unable to initialize vault client")
|
|
|
|
}
|
|
|
|
|
|
|
|
var appRoleAuth *auth.AppRoleAuth
|
2022-01-20 09:16:47 +00:00
|
|
|
if vc.IsWrappingToken {
|
2022-01-14 17:56:17 +00:00
|
|
|
appRoleAuth, err = auth.NewAppRoleAuth(
|
|
|
|
vc.RoleID,
|
2022-01-20 09:16:47 +00:00
|
|
|
&vc.SecretID,
|
2022-01-14 17:56:17 +00:00
|
|
|
auth.WithWrappingToken(),
|
|
|
|
auth.WithMountPath(vc.AppRole),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
appRoleAuth, err = auth.NewAppRoleAuth(
|
|
|
|
vc.RoleID,
|
2022-01-20 09:16:47 +00:00
|
|
|
&vc.SecretID,
|
2022-01-14 17:56:17 +00:00
|
|
|
auth.WithMountPath(vc.AppRole),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "unable to initialize AppRole auth method")
|
|
|
|
}
|
|
|
|
|
|
|
|
authInfo, err := client.Auth().Login(ctx, appRoleAuth)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "unable to login to AppRole auth method")
|
|
|
|
}
|
|
|
|
if authInfo == nil {
|
|
|
|
return nil, errors.New("no auth info was returned after login")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &VaultCAS{
|
|
|
|
client: client,
|
|
|
|
config: vc,
|
|
|
|
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:
|
|
|
|
return nil, errors.New("CreateCertificate: `CSR` cannot be nil")
|
|
|
|
case req.Lifetime == 0:
|
|
|
|
return nil, errors.New("CreateCertificate: `LIFETIME` cannot be 0")
|
|
|
|
}
|
|
|
|
|
|
|
|
cert, chain, err := v.createCertificate(req.CSR, req.Lifetime)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &apiv1.CreateCertificateResponse{
|
|
|
|
Certificate: cert,
|
|
|
|
CertificateChain: chain,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *VaultCAS) GetCertificateAuthority(req *apiv1.GetCertificateAuthorityRequest) (*apiv1.GetCertificateAuthorityResponse, error) {
|
|
|
|
secret, err := v.client.Logical().Read(v.config.PKI + "/cert/ca")
|
|
|
|
if err != nil {
|
2022-01-20 09:16:47 +00:00
|
|
|
return nil, errors.Wrap(err, "unable to read root")
|
2022-01-14 17:56:17 +00:00
|
|
|
}
|
|
|
|
if secret == nil {
|
2022-01-20 09:16:47 +00:00
|
|
|
return nil, errors.New("secret root is empty")
|
2022-01-14 17:56:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var certBundle certutil.CertBundle
|
|
|
|
|
2022-01-20 09:16:47 +00:00
|
|
|
secretData, err := json.Marshal(secret.Data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(secretData, &certBundle)
|
2022-01-14 17:56:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cert, _, err := getCertificateAndChain(certBundle)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-20 09:16:47 +00:00
|
|
|
|
|
|
|
sha256Sum := sha256.Sum256(cert.Raw)
|
|
|
|
expectedSum := certutil.GetHexFormatted(sha256Sum[:], "")
|
|
|
|
if expectedSum != v.fingerprint {
|
|
|
|
return nil, errors.Errorf("Vault Root CA fingerprint `%s` doesn't match config fingerprint `%v`", expectedSum, v.fingerprint)
|
|
|
|
}
|
|
|
|
|
2022-01-14 17:56:17 +00:00
|
|
|
return &apiv1.GetCertificateAuthorityResponse{
|
|
|
|
RootCertificate: cert,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RenewCertificate will always return a non-implemented error as renewals
|
|
|
|
// are not supported yet.
|
|
|
|
func (v *VaultCAS) RenewCertificate(req *apiv1.RenewCertificateRequest) (*apiv1.RenewCertificateResponse, error) {
|
|
|
|
return nil, apiv1.ErrNotImplemented{Message: "vaultCAS does not support renewals"}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *VaultCAS) RevokeCertificate(req *apiv1.RevokeCertificateRequest) (*apiv1.RevokeCertificateResponse, error) {
|
|
|
|
if req.SerialNumber == "" && req.Certificate == nil {
|
2022-01-20 09:16:47 +00:00
|
|
|
return nil, errors.New("`serialNumber` or `certificate` are required")
|
2022-01-14 17:56:17 +00:00
|
|
|
}
|
|
|
|
|
2022-01-20 09:16:47 +00:00
|
|
|
var serialNumber []byte
|
|
|
|
if req.SerialNumber != "" {
|
|
|
|
// req.SerialNumber is a big.Int string representation
|
|
|
|
n := new(big.Int)
|
|
|
|
n, ok := n.SetString(req.SerialNumber, 10)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.Errorf("serialNumber `%v` can't be convert to big.Int", req.SerialNumber)
|
|
|
|
}
|
|
|
|
serialNumber = n.Bytes()
|
|
|
|
} else {
|
|
|
|
// req.Certificate.SerialNumber is a big.Int
|
|
|
|
serialNumber = req.Certificate.SerialNumber.Bytes()
|
2022-01-14 17:56:17 +00:00
|
|
|
}
|
|
|
|
|
2022-01-20 09:16:47 +00:00
|
|
|
serialNumberDash := certutil.GetHexFormatted(serialNumber, "-")
|
2022-01-14 17:56:17 +00:00
|
|
|
|
|
|
|
_, err := v.client.Logical().Write(v.config.PKI+"/revoke/"+serialNumberDash, nil)
|
|
|
|
if err != nil {
|
2022-01-20 09:16:47 +00:00
|
|
|
return nil, errors.Wrap(err, "unable to revoke certificate")
|
2022-01-14 17:56:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &apiv1.RevokeCertificateResponse{
|
|
|
|
Certificate: req.Certificate,
|
|
|
|
CertificateChain: nil,
|
|
|
|
}, nil
|
|
|
|
}
|