2015-05-15 16:13:05 +00:00
|
|
|
package pki
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/x509"
|
|
|
|
"crypto/x509/pkix"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2016-07-28 19:19:27 +00:00
|
|
|
"github.com/hashicorp/vault/helper/errutil"
|
2015-05-15 16:13:05 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
)
|
|
|
|
|
|
|
|
type revocationInfo struct {
|
|
|
|
CertificateBytes []byte `json:"certificate_bytes"`
|
2015-06-15 17:33:23 +00:00
|
|
|
RevocationTime int64 `json:"revocation_time"`
|
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
|
2016-05-09 23:53:28 +00:00
|
|
|
func revokeCert(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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2015-06-19 16:48:18 +00:00
|
|
|
certEntry, err := fetchCertBySerial(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
|
|
|
|
}
|
|
|
|
}
|
2015-05-15 16:13:05 +00:00
|
|
|
if certEntry != 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
|
2015-06-12 02:28:13 +00:00
|
|
|
|
2015-06-19 16:48:18 +00:00
|
|
|
revEntry, err := req.Storage.Get("revoked/" + serial)
|
|
|
|
if revEntry == nil || err != nil {
|
|
|
|
return nil, fmt.Errorf("Error getting existing revocation info")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = revEntry.DecodeJSON(&revInfo)
|
|
|
|
if err != nil {
|
|
|
|
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 {
|
2015-06-19 16:48:18 +00:00
|
|
|
certEntry, err = fetchCertBySerial(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 {
|
|
|
|
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 {
|
|
|
|
return nil, fmt.Errorf("Error parsing certificate")
|
|
|
|
}
|
|
|
|
if cert == nil {
|
|
|
|
return nil, fmt.Errorf("Got a nil certificate")
|
|
|
|
}
|
2015-05-15 16:13:05 +00:00
|
|
|
|
2015-06-12 01:57:05 +00:00
|
|
|
if cert.NotAfter.Before(time.Now()) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-06-12 01:57:05 +00:00
|
|
|
revInfo.CertificateBytes = certEntry.Value
|
|
|
|
revInfo.RevocationTime = time.Now().Unix()
|
2015-05-15 16:13:05 +00:00
|
|
|
|
2015-06-12 01:57:05 +00:00
|
|
|
certEntry, err = logical.StorageEntryJSON("revoked/"+serial, revInfo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error creating revocation entry")
|
|
|
|
}
|
2015-05-15 16:13:05 +00:00
|
|
|
|
2015-06-12 01:57:05 +00:00
|
|
|
err = req.Storage.Put(certEntry)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error saving revoked certificate to new location")
|
|
|
|
}
|
2015-05-15 16:13:05 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-06-19 16:48:18 +00:00
|
|
|
crlErr := buildCRL(b, req)
|
|
|
|
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:
|
2015-06-19 16:48:18 +00:00
|
|
|
return nil, fmt.Errorf("Error encountered during CRL building: %s", crlErr)
|
2015-05-15 16:13:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &logical.Response{
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"revocation_time": revInfo.RevocationTime,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
func buildCRL(b *backend, req *logical.Request) error {
|
2015-05-15 16:13:05 +00:00
|
|
|
revokedSerials, err := req.Storage.List("revoked/")
|
|
|
|
if err != nil {
|
2016-07-28 19:19:27 +00:00
|
|
|
return errutil.InternalError{Err: fmt.Sprintf("Error fetching list of revoked certs: %s", err)}
|
2015-05-15 16:13:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
revokedCerts := []pkix.RevokedCertificate{}
|
|
|
|
var revInfo revocationInfo
|
|
|
|
for _, serial := range revokedSerials {
|
|
|
|
revokedEntry, err := req.Storage.Get("revoked/" + serial)
|
|
|
|
if err != nil {
|
2016-07-28 19:19:27 +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 {
|
2016-07-28 19:19:27 +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
|
2016-07-28 19:19:27 +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 {
|
2016-07-28 19:19:27 +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 {
|
2016-07-28 19:19:27 +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
|
|
|
}
|
|
|
|
|
|
|
|
revokedCerts = append(revokedCerts, pkix.RevokedCertificate{
|
|
|
|
SerialNumber: revokedCert.SerialNumber,
|
|
|
|
RevocationTime: time.Unix(revInfo.RevocationTime, 0),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-06-19 16:48:18 +00:00
|
|
|
signingBundle, caErr := fetchCAInfo(req)
|
|
|
|
switch caErr.(type) {
|
2016-07-28 19:19:27 +00:00
|
|
|
case errutil.UserError:
|
|
|
|
return errutil.UserError{Err: fmt.Sprintf("Could not fetch the CA certificate: %s", caErr)}
|
|
|
|
case errutil.InternalError:
|
|
|
|
return errutil.InternalError{Err: fmt.Sprintf("Error fetching CA certificate: %s", caErr)}
|
2015-06-19 16:48:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
crlLifetime := b.crlLifetime
|
|
|
|
crlInfo, err := b.CRL(req.Storage)
|
|
|
|
if err != nil {
|
2016-07-28 19:19:27 +00:00
|
|
|
return errutil.InternalError{Err: fmt.Sprintf("Error fetching CRL config information: %s", err)}
|
2015-06-19 16:48:18 +00:00
|
|
|
}
|
|
|
|
if crlInfo != nil {
|
|
|
|
crlDur, err := time.ParseDuration(crlInfo.Expiry)
|
|
|
|
if err != nil {
|
2016-07-28 19:19:27 +00:00
|
|
|
return errutil.InternalError{Err: fmt.Sprintf("Error parsing CRL duration of %s", crlInfo.Expiry)}
|
2015-06-19 16:48:18 +00:00
|
|
|
}
|
|
|
|
crlLifetime = crlDur
|
2015-05-15 16:13:05 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2016-07-28 19:19:27 +00:00
|
|
|
return errutil.InternalError{Err: fmt.Sprintf("Error creating new CRL: %s", err)}
|
2015-05-15 16:13:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = req.Storage.Put(&logical.StorageEntry{
|
|
|
|
Key: "crl",
|
|
|
|
Value: crlBytes,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2016-07-28 19:19:27 +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
|
|
|
}
|