2015-05-15 16:13:05 +00:00
|
|
|
package pki
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto"
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/elliptic"
|
2015-06-12 01:57:05 +00:00
|
|
|
"crypto/rand"
|
2015-05-15 16:13:05 +00:00
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/x509"
|
|
|
|
"crypto/x509/pkix"
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
|
|
|
"net"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2015-06-17 16:43:36 +00:00
|
|
|
"github.com/hashicorp/vault/helper/certutil"
|
2015-05-15 16:13:05 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
)
|
|
|
|
|
|
|
|
type certUsage int
|
|
|
|
|
|
|
|
const (
|
|
|
|
serverUsage certUsage = 1 << iota
|
|
|
|
clientUsage
|
|
|
|
codeSigningUsage
|
|
|
|
)
|
|
|
|
|
|
|
|
type certCreationBundle struct {
|
2015-06-17 16:43:36 +00:00
|
|
|
SigningBundle *certutil.ParsedCertBundle
|
|
|
|
CACert *x509.Certificate
|
|
|
|
CommonNames []string
|
|
|
|
IPSANs []net.IP
|
|
|
|
KeyType string
|
|
|
|
KeyBits int
|
2015-08-27 19:24:37 +00:00
|
|
|
TTL time.Duration
|
2015-06-17 16:43:36 +00:00
|
|
|
Usage certUsage
|
2015-05-15 16:13:05 +00:00
|
|
|
}
|
|
|
|
|
2015-06-19 16:48:18 +00:00
|
|
|
// Fetches the CA info. Unlike other certificates, the CA info is stored
|
|
|
|
// in the backend as a CertBundle, because we are storing its private key
|
|
|
|
func fetchCAInfo(req *logical.Request) (*certutil.ParsedCertBundle, error) {
|
|
|
|
bundleEntry, err := req.Storage.Get("config/ca_bundle")
|
2015-05-15 16:13:05 +00:00
|
|
|
if err != nil {
|
2015-06-19 16:48:18 +00:00
|
|
|
return nil, certutil.InternalError{Err: fmt.Sprintf("Unable to fetch local CA certificate/key: %s", err)}
|
2015-05-15 16:13:05 +00:00
|
|
|
}
|
2015-06-19 16:48:18 +00:00
|
|
|
if bundleEntry == nil {
|
|
|
|
return nil, certutil.UserError{Err: fmt.Sprintf("Backend must be configured with a CA certificate/key")}
|
2015-05-15 16:13:05 +00:00
|
|
|
}
|
|
|
|
|
2015-06-19 16:48:18 +00:00
|
|
|
var bundle certutil.CertBundle
|
|
|
|
if err := bundleEntry.DecodeJSON(&bundle); err != nil {
|
|
|
|
return nil, certutil.InternalError{Err: fmt.Sprintf("Unable to decode local CA certificate/key: %s", err)}
|
2015-05-15 16:13:05 +00:00
|
|
|
}
|
|
|
|
|
2015-06-17 16:43:36 +00:00
|
|
|
parsedBundle, err := bundle.ToParsedCertBundle()
|
2015-05-15 16:13:05 +00:00
|
|
|
if err != nil {
|
2015-06-19 16:48:18 +00:00
|
|
|
return nil, certutil.InternalError{Err: err.Error()}
|
2015-05-15 16:13:05 +00:00
|
|
|
}
|
|
|
|
|
2015-06-19 16:48:18 +00:00
|
|
|
if parsedBundle.Certificate == nil {
|
|
|
|
return nil, certutil.InternalError{Err: "Stored CA information not able to be parsed"}
|
2015-05-15 16:13:05 +00:00
|
|
|
}
|
|
|
|
|
2015-06-19 16:48:18 +00:00
|
|
|
return parsedBundle, nil
|
2015-05-15 16:13:05 +00:00
|
|
|
}
|
|
|
|
|
2015-06-19 16:48:18 +00:00
|
|
|
// Allows fetching certificates from the backend; it handles the slightly
|
|
|
|
// separate pathing for CA, CRL, and revoked certificates.
|
|
|
|
func fetchCertBySerial(req *logical.Request, prefix, serial string) (*logical.StorageEntry, error) {
|
2015-05-15 16:13:05 +00:00
|
|
|
var path string
|
2015-06-19 16:48:18 +00:00
|
|
|
|
2015-05-15 16:13:05 +00:00
|
|
|
switch {
|
|
|
|
case serial == "ca":
|
|
|
|
path = "ca"
|
|
|
|
case serial == "crl":
|
|
|
|
path = "crl"
|
|
|
|
case strings.HasPrefix(prefix, "revoked/"):
|
|
|
|
path = "revoked/" + strings.Replace(strings.ToLower(serial), "-", ":", -1)
|
|
|
|
default:
|
|
|
|
path = "certs/" + strings.Replace(strings.ToLower(serial), "-", ":", -1)
|
|
|
|
}
|
|
|
|
|
2015-06-19 16:48:18 +00:00
|
|
|
certEntry, err := req.Storage.Get(path)
|
2015-05-15 16:13:05 +00:00
|
|
|
if err != nil || certEntry == nil {
|
2015-06-19 16:48:18 +00:00
|
|
|
return nil, certutil.InternalError{Err: fmt.Sprintf("Certificate with serial number %s not found", serial)}
|
2015-05-15 16:13:05 +00:00
|
|
|
}
|
|
|
|
|
2015-06-19 16:48:18 +00:00
|
|
|
if certEntry.Value == nil || len(certEntry.Value) == 0 {
|
|
|
|
return nil, certutil.InternalError{Err: fmt.Sprintf("Returned certificate bytes for serial %s were empty", serial)}
|
2015-05-15 16:13:05 +00:00
|
|
|
}
|
|
|
|
|
2015-06-19 16:48:18 +00:00
|
|
|
return certEntry, nil
|
2015-05-15 16:13:05 +00:00
|
|
|
}
|
|
|
|
|
2015-06-19 16:48:18 +00:00
|
|
|
// Given a set of requested names for a certificate, verifies that all of them
|
|
|
|
// match the various toggles set in the role for controlling issuance.
|
|
|
|
// If one does not pass, it is returned in the string argument.
|
2015-05-15 16:13:05 +00:00
|
|
|
func validateCommonNames(req *logical.Request, commonNames []string, role *roleEntry) (string, error) {
|
|
|
|
hostnameRegex, err := regexp.Compile(`^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$`)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Error compiling hostname regex: %s", err)
|
|
|
|
}
|
|
|
|
subdomainRegex, err := regexp.Compile(`^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9]))*$`)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Error compiling subdomain regex: %s", err)
|
|
|
|
}
|
|
|
|
for _, name := range commonNames {
|
|
|
|
if role.AllowLocalhost && name == "localhost" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
sanitizedName := name
|
|
|
|
isWildcard := false
|
|
|
|
if strings.HasPrefix(name, "*.") {
|
|
|
|
sanitizedName = name[2:]
|
|
|
|
isWildcard = true
|
|
|
|
}
|
2015-08-20 21:33:37 +00:00
|
|
|
|
|
|
|
if role.EnforceHostnames {
|
|
|
|
if !hostnameRegex.MatchString(sanitizedName) {
|
|
|
|
return name, nil
|
|
|
|
}
|
2015-05-15 16:13:05 +00:00
|
|
|
}
|
2015-08-20 21:33:37 +00:00
|
|
|
|
2015-05-15 16:13:05 +00:00
|
|
|
if role.AllowAnyName {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if role.AllowTokenDisplayName {
|
|
|
|
if name == req.DisplayName {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if role.AllowSubdomains {
|
|
|
|
if strings.HasSuffix(name, "."+req.DisplayName) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(role.AllowedBaseDomain) != 0 {
|
|
|
|
if strings.HasSuffix(name, "."+role.AllowedBaseDomain) {
|
|
|
|
if role.AllowSubdomains {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if subdomainRegex.MatchString(strings.TrimSuffix(name, "."+role.AllowedBaseDomain)) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if isWildcard && role.AllowedBaseDomain == sanitizedName {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return name, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2015-06-19 16:48:18 +00:00
|
|
|
// Performs the heavy lifting of creating a certificate. Returns
|
|
|
|
// a fully-filled-in ParsedCertBundle.
|
|
|
|
func createCertificate(creationInfo *certCreationBundle) (*certutil.ParsedCertBundle, error) {
|
2015-05-15 16:13:05 +00:00
|
|
|
var clientPrivKey crypto.Signer
|
|
|
|
var err error
|
2015-06-19 16:48:18 +00:00
|
|
|
result := &certutil.ParsedCertBundle{}
|
2015-06-12 01:57:05 +00:00
|
|
|
|
2015-06-17 16:43:36 +00:00
|
|
|
var serialNumber *big.Int
|
|
|
|
serialNumber, err = rand.Int(rand.Reader, (&big.Int{}).Exp(big.NewInt(2), big.NewInt(159), nil))
|
2015-06-12 01:57:05 +00:00
|
|
|
if err != nil {
|
2015-06-19 16:48:18 +00:00
|
|
|
return nil, certutil.InternalError{Err: fmt.Sprintf("Error getting random serial number")}
|
2015-06-12 01:57:05 +00:00
|
|
|
}
|
|
|
|
|
2015-05-15 16:13:05 +00:00
|
|
|
switch creationInfo.KeyType {
|
|
|
|
case "rsa":
|
2015-06-19 16:48:18 +00:00
|
|
|
result.PrivateKeyType = certutil.RSAPrivateKey
|
2015-06-12 01:57:05 +00:00
|
|
|
clientPrivKey, err = rsa.GenerateKey(rand.Reader, creationInfo.KeyBits)
|
2015-05-15 16:13:05 +00:00
|
|
|
if err != nil {
|
2015-06-19 16:48:18 +00:00
|
|
|
return nil, certutil.InternalError{Err: fmt.Sprintf("Error generating RSA private key")}
|
2015-05-15 16:13:05 +00:00
|
|
|
}
|
2015-06-19 16:48:18 +00:00
|
|
|
result.PrivateKey = clientPrivKey
|
|
|
|
result.PrivateKeyBytes = x509.MarshalPKCS1PrivateKey(clientPrivKey.(*rsa.PrivateKey))
|
2015-06-05 18:03:57 +00:00
|
|
|
case "ec":
|
2015-06-19 16:48:18 +00:00
|
|
|
result.PrivateKeyType = certutil.ECPrivateKey
|
2015-05-15 16:13:05 +00:00
|
|
|
var curve elliptic.Curve
|
|
|
|
switch creationInfo.KeyBits {
|
|
|
|
case 224:
|
|
|
|
curve = elliptic.P224()
|
|
|
|
case 256:
|
|
|
|
curve = elliptic.P256()
|
|
|
|
case 384:
|
|
|
|
curve = elliptic.P384()
|
|
|
|
case 521:
|
|
|
|
curve = elliptic.P521()
|
|
|
|
default:
|
2015-06-19 16:48:18 +00:00
|
|
|
return nil, certutil.UserError{Err: fmt.Sprintf("Unsupported bit length for EC key: %d", creationInfo.KeyBits)}
|
2015-05-15 16:13:05 +00:00
|
|
|
}
|
2015-06-12 01:57:05 +00:00
|
|
|
clientPrivKey, err = ecdsa.GenerateKey(curve, rand.Reader)
|
2015-05-15 16:13:05 +00:00
|
|
|
if err != nil {
|
2015-06-19 16:48:18 +00:00
|
|
|
return nil, certutil.InternalError{Err: fmt.Sprintf("Error generating EC private key")}
|
2015-05-15 16:13:05 +00:00
|
|
|
}
|
2015-06-19 16:48:18 +00:00
|
|
|
result.PrivateKey = clientPrivKey
|
|
|
|
result.PrivateKeyBytes, err = x509.MarshalECPrivateKey(clientPrivKey.(*ecdsa.PrivateKey))
|
2015-05-15 16:13:05 +00:00
|
|
|
if err != nil {
|
2015-06-19 16:48:18 +00:00
|
|
|
return nil, certutil.InternalError{Err: fmt.Sprintf("Error marshalling EC private key")}
|
2015-05-15 16:13:05 +00:00
|
|
|
}
|
|
|
|
default:
|
2015-06-19 16:48:18 +00:00
|
|
|
return nil, certutil.UserError{Err: fmt.Sprintf("Unknown key type: %s", creationInfo.KeyType)}
|
2015-05-15 16:13:05 +00:00
|
|
|
}
|
|
|
|
|
2015-06-19 16:48:18 +00:00
|
|
|
subjKeyID, err := certutil.GetSubjKeyID(result.PrivateKey)
|
2015-05-15 16:13:05 +00:00
|
|
|
if err != nil {
|
2015-06-19 16:48:18 +00:00
|
|
|
return nil, certutil.InternalError{Err: fmt.Sprintf("Error getting subject key ID: %s", err)}
|
2015-05-15 16:13:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
subject := pkix.Name{
|
|
|
|
Country: creationInfo.CACert.Subject.Country,
|
|
|
|
Organization: creationInfo.CACert.Subject.Organization,
|
|
|
|
OrganizationalUnit: creationInfo.CACert.Subject.OrganizationalUnit,
|
|
|
|
Locality: creationInfo.CACert.Subject.Locality,
|
|
|
|
Province: creationInfo.CACert.Subject.Province,
|
|
|
|
StreetAddress: creationInfo.CACert.Subject.StreetAddress,
|
|
|
|
PostalCode: creationInfo.CACert.Subject.PostalCode,
|
2015-06-17 16:43:36 +00:00
|
|
|
SerialNumber: serialNumber.String(),
|
2015-05-15 16:13:05 +00:00
|
|
|
CommonName: creationInfo.CommonNames[0],
|
|
|
|
}
|
|
|
|
|
|
|
|
certTemplate := &x509.Certificate{
|
|
|
|
SignatureAlgorithm: x509.SHA256WithRSA,
|
2015-06-17 16:43:36 +00:00
|
|
|
SerialNumber: serialNumber,
|
2015-05-15 16:13:05 +00:00
|
|
|
Subject: subject,
|
|
|
|
NotBefore: time.Now(),
|
2015-08-27 19:24:37 +00:00
|
|
|
NotAfter: time.Now().Add(creationInfo.TTL),
|
2015-05-15 16:13:05 +00:00
|
|
|
KeyUsage: x509.KeyUsage(x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment | x509.KeyUsageKeyAgreement),
|
|
|
|
BasicConstraintsValid: true,
|
|
|
|
IsCA: false,
|
|
|
|
SubjectKeyId: subjKeyID,
|
|
|
|
DNSNames: creationInfo.CommonNames,
|
|
|
|
IPAddresses: creationInfo.IPSANs,
|
|
|
|
PermittedDNSDomainsCritical: false,
|
|
|
|
PermittedDNSDomains: nil,
|
|
|
|
CRLDistributionPoints: creationInfo.CACert.CRLDistributionPoints,
|
|
|
|
}
|
|
|
|
|
|
|
|
if creationInfo.Usage&serverUsage != 0 {
|
|
|
|
certTemplate.ExtKeyUsage = append(certTemplate.ExtKeyUsage, x509.ExtKeyUsageServerAuth)
|
|
|
|
}
|
|
|
|
if creationInfo.Usage&clientUsage != 0 {
|
|
|
|
certTemplate.ExtKeyUsage = append(certTemplate.ExtKeyUsage, x509.ExtKeyUsageClientAuth)
|
|
|
|
}
|
|
|
|
if creationInfo.Usage&codeSigningUsage != 0 {
|
|
|
|
certTemplate.ExtKeyUsage = append(certTemplate.ExtKeyUsage, x509.ExtKeyUsageCodeSigning)
|
|
|
|
}
|
|
|
|
|
2015-06-17 16:43:36 +00:00
|
|
|
cert, err := x509.CreateCertificate(rand.Reader, certTemplate, creationInfo.CACert, clientPrivKey.Public(), creationInfo.SigningBundle.PrivateKey)
|
2015-05-15 16:13:05 +00:00
|
|
|
if err != nil {
|
2015-06-19 16:48:18 +00:00
|
|
|
return nil, certutil.InternalError{Err: fmt.Sprintf("Unable to create certificate: %s", err)}
|
2015-05-15 16:13:05 +00:00
|
|
|
}
|
|
|
|
|
2015-06-19 16:48:18 +00:00
|
|
|
result.CertificateBytes = cert
|
|
|
|
result.Certificate, err = x509.ParseCertificate(cert)
|
2015-05-15 16:13:05 +00:00
|
|
|
if err != nil {
|
2015-06-19 16:48:18 +00:00
|
|
|
return nil, certutil.InternalError{Err: fmt.Sprintf("Unable to parse created certificate: %s", err)}
|
2015-05-15 16:13:05 +00:00
|
|
|
}
|
|
|
|
|
2015-06-19 16:48:18 +00:00
|
|
|
result.IssuingCABytes = creationInfo.SigningBundle.CertificateBytes
|
|
|
|
result.IssuingCA = creationInfo.SigningBundle.Certificate
|
2015-05-15 16:13:05 +00:00
|
|
|
|
2015-06-19 16:48:18 +00:00
|
|
|
return result, nil
|
2015-05-15 16:13:05 +00:00
|
|
|
}
|