2015-05-15 16:13:05 +00:00
|
|
|
package pki
|
|
|
|
|
|
|
|
import (
|
2018-01-19 06:44:44 +00:00
|
|
|
"context"
|
2015-05-15 16:13:05 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"crypto/x509"
|
|
|
|
"crypto/x509/pkix"
|
2018-08-21 15:20:57 +00:00
|
|
|
"errors"
|
2015-05-15 16:13:05 +00:00
|
|
|
"fmt"
|
2018-08-21 15:20:57 +00:00
|
|
|
"strings"
|
2015-05-15 16:13:05 +00:00
|
|
|
"time"
|
|
|
|
|
2018-04-05 15:49:21 +00:00
|
|
|
"github.com/hashicorp/errwrap"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/certutil"
|
|
|
|
"github.com/hashicorp/vault/sdk/helper/errutil"
|
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2015-05-15 16:13:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type revocationInfo struct {
|
2016-08-13 12:40:09 +00:00
|
|
|
CertificateBytes []byte `json:"certificate_bytes"`
|
|
|
|
RevocationTime int64 `json:"revocation_time"`
|
|
|
|
RevocationTimeUTC time.Time `json:"revocation_time_utc"`
|
2015-05-15 16:13:05 +00:00
|
|
|
}
|
|
|
|
|
2015-06-19 16:48:18 +00:00
|
|
|
// Revokes a cert, and tries to be smart about error recovery
|
2018-01-19 06:44:44 +00:00
|
|
|
func revokeCert(ctx context.Context, b *backend, req *logical.Request, serial string, fromLease bool) (*logical.Response, error) {
|
2016-01-22 22:01:22 +00:00
|
|
|
// As this backend is self-contained and this function does not hook into
|
|
|
|
// third parties to manage users or resources, if the mount is tainted,
|
|
|
|
// revocation doesn't matter anyways -- the CRL that would be written will
|
|
|
|
// be immediately blown away by the view being cleared. So we can simply
|
|
|
|
// fast path a successful exit.
|
|
|
|
if b.System().Tainted() {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2018-08-21 15:20:57 +00:00
|
|
|
signingBundle, caErr := fetchCAInfo(ctx, req)
|
|
|
|
switch caErr.(type) {
|
|
|
|
case errutil.UserError:
|
|
|
|
return logical.ErrorResponse(fmt.Sprintf("could not fetch the CA certificate: %s", caErr)), nil
|
|
|
|
case errutil.InternalError:
|
|
|
|
return nil, fmt.Errorf("error fetching CA certificate: %s", caErr)
|
|
|
|
}
|
|
|
|
if signingBundle == nil {
|
|
|
|
return nil, errors.New("CA info not found")
|
|
|
|
}
|
|
|
|
colonSerial := strings.Replace(strings.ToLower(serial), "-", ":", -1)
|
|
|
|
if colonSerial == certutil.GetHexFormatted(signingBundle.Certificate.SerialNumber.Bytes(), ":") {
|
|
|
|
return logical.ErrorResponse("adding CA to CRL is not allowed"), nil
|
|
|
|
}
|
|
|
|
|
2015-06-12 01:57:05 +00:00
|
|
|
alreadyRevoked := false
|
2015-06-19 16:48:18 +00:00
|
|
|
var revInfo revocationInfo
|
2015-06-12 01:57:05 +00:00
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
revEntry, err := fetchCertBySerial(ctx, req, "revoked/", serial)
|
2016-02-22 15:36:26 +00:00
|
|
|
if err != nil {
|
|
|
|
switch err.(type) {
|
2016-07-28 19:19:27 +00:00
|
|
|
case errutil.UserError:
|
2016-02-22 15:36:26 +00:00
|
|
|
return logical.ErrorResponse(err.Error()), nil
|
2016-07-28 19:19:27 +00:00
|
|
|
case errutil.InternalError:
|
2016-02-22 15:36:26 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2016-12-20 16:07:20 +00:00
|
|
|
if revEntry != nil {
|
2016-02-22 15:36:26 +00:00
|
|
|
// Set the revocation info to the existing values
|
2015-06-19 16:48:18 +00:00
|
|
|
alreadyRevoked = true
|
|
|
|
err = revEntry.DecodeJSON(&revInfo)
|
|
|
|
if err != nil {
|
2018-03-29 14:14:42 +00:00
|
|
|
return nil, fmt.Errorf("error decoding existing revocation info")
|
2015-06-12 01:57:05 +00:00
|
|
|
}
|
2015-05-15 16:13:05 +00:00
|
|
|
}
|
|
|
|
|
2015-06-12 01:57:05 +00:00
|
|
|
if !alreadyRevoked {
|
2018-01-19 06:44:44 +00:00
|
|
|
certEntry, err := fetchCertBySerial(ctx, req, "certs/", serial)
|
2016-02-22 15:36:26 +00:00
|
|
|
if err != nil {
|
|
|
|
switch err.(type) {
|
2016-07-28 19:19:27 +00:00
|
|
|
case errutil.UserError:
|
2016-02-22 15:36:26 +00:00
|
|
|
return logical.ErrorResponse(err.Error()), nil
|
2016-07-28 19:19:27 +00:00
|
|
|
case errutil.InternalError:
|
2016-02-22 15:36:26 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if certEntry == nil {
|
2020-09-17 20:15:03 +00:00
|
|
|
if fromLease {
|
|
|
|
// We can't write to revoked/ or update the CRL anyway because we don't have the cert,
|
|
|
|
// and there's no reason to expect this will work on a subsequent
|
|
|
|
// retry. Just give up and let the lease get deleted.
|
|
|
|
b.Logger().Warn("expired certificate revoke failed because not found in storage, treating as success", "serial", serial)
|
|
|
|
return nil, nil
|
|
|
|
}
|
2016-02-22 15:36:26 +00:00
|
|
|
return logical.ErrorResponse(fmt.Sprintf("certificate with serial %s not found", serial)), nil
|
2015-06-12 01:57:05 +00:00
|
|
|
}
|
2015-05-15 16:13:05 +00:00
|
|
|
|
2015-06-12 01:57:05 +00:00
|
|
|
cert, err := x509.ParseCertificate(certEntry.Value)
|
|
|
|
if err != nil {
|
2018-04-05 15:49:21 +00:00
|
|
|
return nil, errwrap.Wrapf("error parsing certificate: {{err}}", err)
|
2015-06-12 01:57:05 +00:00
|
|
|
}
|
|
|
|
if cert == nil {
|
2018-03-29 14:14:42 +00:00
|
|
|
return nil, fmt.Errorf("got a nil certificate")
|
2015-06-12 01:57:05 +00:00
|
|
|
}
|
2015-05-15 16:13:05 +00:00
|
|
|
|
2018-08-21 15:20:57 +00:00
|
|
|
// Add a little wiggle room because leases are stored with a second
|
|
|
|
// granularity
|
|
|
|
if cert.NotAfter.Before(time.Now().Add(2 * time.Second)) {
|
2015-06-12 01:57:05 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
2015-05-15 16:13:05 +00:00
|
|
|
|
2016-05-09 23:53:28 +00:00
|
|
|
// Compatibility: Don't revoke CAs if they had leases. New CAs going
|
|
|
|
// forward aren't issued leases.
|
|
|
|
if cert.IsCA && fromLease {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2016-08-13 12:40:09 +00:00
|
|
|
currTime := time.Now()
|
2015-06-12 01:57:05 +00:00
|
|
|
revInfo.CertificateBytes = certEntry.Value
|
2016-08-13 12:40:09 +00:00
|
|
|
revInfo.RevocationTime = currTime.Unix()
|
|
|
|
revInfo.RevocationTimeUTC = currTime.UTC()
|
2015-05-15 16:13:05 +00:00
|
|
|
|
2017-05-03 14:12:58 +00:00
|
|
|
revEntry, err = logical.StorageEntryJSON("revoked/"+normalizeSerial(serial), revInfo)
|
2015-06-12 01:57:05 +00:00
|
|
|
if err != nil {
|
2018-03-29 14:14:42 +00:00
|
|
|
return nil, fmt.Errorf("error creating revocation entry")
|
2015-06-12 01:57:05 +00:00
|
|
|
}
|
2015-05-15 16:13:05 +00:00
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
err = req.Storage.Put(ctx, revEntry)
|
2015-06-12 01:57:05 +00:00
|
|
|
if err != nil {
|
2018-03-29 14:14:42 +00:00
|
|
|
return nil, fmt.Errorf("error saving revoked certificate to new location")
|
2015-06-12 01:57:05 +00:00
|
|
|
}
|
2015-05-15 16:13:05 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-08-21 15:20:57 +00:00
|
|
|
crlErr := buildCRL(ctx, b, req, false)
|
2015-06-19 16:48:18 +00:00
|
|
|
switch crlErr.(type) {
|
2016-07-28 19:19:27 +00:00
|
|
|
case errutil.UserError:
|
2015-06-19 16:48:18 +00:00
|
|
|
return logical.ErrorResponse(fmt.Sprintf("Error during CRL building: %s", crlErr)), nil
|
2016-07-28 19:19:27 +00:00
|
|
|
case errutil.InternalError:
|
2018-04-05 15:49:21 +00:00
|
|
|
return nil, errwrap.Wrapf("error encountered during CRL building: {{err}}", crlErr)
|
2015-05-15 16:13:05 +00:00
|
|
|
}
|
|
|
|
|
2016-08-13 15:50:57 +00:00
|
|
|
resp := &logical.Response{
|
2015-05-15 16:13:05 +00:00
|
|
|
Data: map[string]interface{}{
|
2016-08-13 15:50:57 +00:00
|
|
|
"revocation_time": revInfo.RevocationTime,
|
2015-05-15 16:13:05 +00:00
|
|
|
},
|
2016-08-13 15:50:57 +00:00
|
|
|
}
|
|
|
|
if !revInfo.RevocationTimeUTC.IsZero() {
|
2016-09-23 16:32:07 +00:00
|
|
|
resp.Data["revocation_time_rfc3339"] = revInfo.RevocationTimeUTC.Format(time.RFC3339Nano)
|
2016-08-13 15:50:57 +00:00
|
|
|
}
|
|
|
|
return resp, nil
|
2015-05-15 16:13:05 +00:00
|
|
|
}
|
|
|
|
|
2015-06-19 16:48:18 +00:00
|
|
|
// Builds a CRL by going through the list of revoked certificates and building
|
|
|
|
// a new CRL with the stored revocation times and serial numbers.
|
2018-08-21 15:20:57 +00:00
|
|
|
func buildCRL(ctx context.Context, b *backend, req *logical.Request, forceNew bool) error {
|
|
|
|
crlInfo, err := b.CRL(ctx, req.Storage)
|
2015-05-15 16:13:05 +00:00
|
|
|
if err != nil {
|
2018-08-21 15:20:57 +00:00
|
|
|
return errutil.InternalError{Err: fmt.Sprintf("error fetching CRL config information: %s", err)}
|
2015-05-15 16:13:05 +00:00
|
|
|
}
|
|
|
|
|
2018-08-21 15:20:57 +00:00
|
|
|
crlLifetime := b.crlLifetime
|
|
|
|
var revokedCerts []pkix.RevokedCertificate
|
2015-05-15 16:13:05 +00:00
|
|
|
var revInfo revocationInfo
|
2018-08-21 15:20:57 +00:00
|
|
|
var revokedSerials []string
|
|
|
|
|
|
|
|
if crlInfo != nil {
|
|
|
|
if crlInfo.Expiry != "" {
|
|
|
|
crlDur, err := time.ParseDuration(crlInfo.Expiry)
|
|
|
|
if err != nil {
|
|
|
|
return errutil.InternalError{Err: fmt.Sprintf("error parsing CRL duration of %s", crlInfo.Expiry)}
|
|
|
|
}
|
|
|
|
crlLifetime = crlDur
|
|
|
|
}
|
|
|
|
|
|
|
|
if crlInfo.Disable {
|
|
|
|
if !forceNew {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
goto WRITE
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
revokedSerials, err = req.Storage.List(ctx, "revoked/")
|
|
|
|
if err != nil {
|
|
|
|
return errutil.InternalError{Err: fmt.Sprintf("error fetching list of revoked certs: %s", err)}
|
|
|
|
}
|
|
|
|
|
2015-05-15 16:13:05 +00:00
|
|
|
for _, serial := range revokedSerials {
|
2018-01-19 06:44:44 +00:00
|
|
|
revokedEntry, err := req.Storage.Get(ctx, "revoked/"+serial)
|
2015-05-15 16:13:05 +00:00
|
|
|
if err != nil {
|
2018-03-29 14:14:42 +00:00
|
|
|
return errutil.InternalError{Err: fmt.Sprintf("unable to fetch revoked cert with serial %s: %s", serial, err)}
|
2015-05-15 16:13:05 +00:00
|
|
|
}
|
|
|
|
if revokedEntry == nil {
|
2018-03-29 14:14:42 +00:00
|
|
|
return errutil.InternalError{Err: fmt.Sprintf("revoked certificate entry for serial %s is nil", serial)}
|
2015-05-15 16:13:05 +00:00
|
|
|
}
|
|
|
|
if revokedEntry.Value == nil || len(revokedEntry.Value) == 0 {
|
|
|
|
// TODO: In this case, remove it and continue? How likely is this to
|
|
|
|
// happen? Alternately, could skip it entirely, or could implement a
|
|
|
|
// delete function so that there is a way to remove these
|
2018-03-29 14:14:42 +00:00
|
|
|
return errutil.InternalError{Err: fmt.Sprintf("found revoked serial but actual certificate is empty")}
|
2015-05-15 16:13:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = revokedEntry.DecodeJSON(&revInfo)
|
|
|
|
if err != nil {
|
2018-03-29 14:14:42 +00:00
|
|
|
return errutil.InternalError{Err: fmt.Sprintf("error decoding revocation entry for serial %s: %s", serial, err)}
|
2015-05-15 16:13:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
revokedCert, err := x509.ParseCertificate(revInfo.CertificateBytes)
|
|
|
|
if err != nil {
|
2018-03-29 14:14:42 +00:00
|
|
|
return errutil.InternalError{Err: fmt.Sprintf("unable to parse stored revoked certificate with serial %s: %s", serial, err)}
|
2015-05-15 16:13:05 +00:00
|
|
|
}
|
|
|
|
|
2016-08-13 12:40:09 +00:00
|
|
|
// NOTE: We have to change this to UTC time because the CRL standard
|
|
|
|
// mandates it but Go will happily encode the CRL without this.
|
|
|
|
newRevCert := pkix.RevokedCertificate{
|
2016-08-13 15:52:09 +00:00
|
|
|
SerialNumber: revokedCert.SerialNumber,
|
2016-08-13 12:40:09 +00:00
|
|
|
}
|
|
|
|
if !revInfo.RevocationTimeUTC.IsZero() {
|
|
|
|
newRevCert.RevocationTime = revInfo.RevocationTimeUTC
|
2016-08-13 15:52:09 +00:00
|
|
|
} else {
|
|
|
|
newRevCert.RevocationTime = time.Unix(revInfo.RevocationTime, 0).UTC()
|
2016-08-13 12:40:09 +00:00
|
|
|
}
|
|
|
|
revokedCerts = append(revokedCerts, newRevCert)
|
2015-05-15 16:13:05 +00:00
|
|
|
}
|
|
|
|
|
2018-08-21 15:20:57 +00:00
|
|
|
WRITE:
|
2018-01-19 06:44:44 +00:00
|
|
|
signingBundle, caErr := fetchCAInfo(ctx, req)
|
2015-06-19 16:48:18 +00:00
|
|
|
switch caErr.(type) {
|
2016-07-28 19:19:27 +00:00
|
|
|
case errutil.UserError:
|
2018-03-29 14:14:42 +00:00
|
|
|
return errutil.UserError{Err: fmt.Sprintf("could not fetch the CA certificate: %s", caErr)}
|
2016-07-28 19:19:27 +00:00
|
|
|
case errutil.InternalError:
|
2018-03-29 14:14:42 +00:00
|
|
|
return errutil.InternalError{Err: fmt.Sprintf("error fetching CA certificate: %s", caErr)}
|
2015-06-19 16:48:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
crlBytes, err := signingBundle.Certificate.CreateCRL(rand.Reader, signingBundle.PrivateKey, revokedCerts, time.Now(), time.Now().Add(crlLifetime))
|
2015-05-15 16:13:05 +00:00
|
|
|
if err != nil {
|
2018-03-29 14:14:42 +00:00
|
|
|
return errutil.InternalError{Err: fmt.Sprintf("error creating new CRL: %s", err)}
|
2015-05-15 16:13:05 +00:00
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
err = req.Storage.Put(ctx, &logical.StorageEntry{
|
2015-05-15 16:13:05 +00:00
|
|
|
Key: "crl",
|
|
|
|
Value: crlBytes,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2018-03-29 14:14:42 +00:00
|
|
|
return errutil.InternalError{Err: fmt.Sprintf("error storing CRL: %s", err)}
|
2015-05-15 16:13:05 +00:00
|
|
|
}
|
|
|
|
|
2015-06-19 16:48:18 +00:00
|
|
|
return nil
|
2015-05-15 16:13:05 +00:00
|
|
|
}
|