2021-07-02 20:51:15 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2021-12-02 15:25:35 +00:00
|
|
|
"bytes"
|
|
|
|
"context"
|
2021-07-02 20:51:15 +00:00
|
|
|
"crypto/x509"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
2021-11-26 16:27:42 +00:00
|
|
|
"fmt"
|
2021-07-02 20:51:15 +00:00
|
|
|
"net/http"
|
2021-07-09 22:28:31 +00:00
|
|
|
"strings"
|
2021-07-02 20:51:15 +00:00
|
|
|
|
2022-03-30 08:22:22 +00:00
|
|
|
"go.step.sm/crypto/jose"
|
|
|
|
"golang.org/x/crypto/ocsp"
|
|
|
|
|
2021-07-02 22:21:17 +00:00
|
|
|
"github.com/smallstep/certificates/acme"
|
2022-03-30 08:22:22 +00:00
|
|
|
"github.com/smallstep/certificates/api/render"
|
2021-07-02 20:51:15 +00:00
|
|
|
"github.com/smallstep/certificates/authority"
|
2021-07-09 22:28:31 +00:00
|
|
|
"github.com/smallstep/certificates/authority/provisioner"
|
2021-07-09 15:51:31 +00:00
|
|
|
"github.com/smallstep/certificates/logging"
|
2021-07-02 20:51:15 +00:00
|
|
|
)
|
|
|
|
|
2021-07-02 22:21:17 +00:00
|
|
|
type revokePayload struct {
|
|
|
|
Certificate string `json:"certificate"`
|
2021-07-09 15:51:31 +00:00
|
|
|
ReasonCode *int `json:"reason,omitempty"`
|
2021-07-02 22:21:17 +00:00
|
|
|
}
|
|
|
|
|
2021-07-09 15:51:31 +00:00
|
|
|
// RevokeCert attempts to revoke a certificate.
|
2021-07-02 20:51:15 +00:00
|
|
|
func (h *Handler) RevokeCert(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
ctx := r.Context()
|
2021-07-02 23:56:14 +00:00
|
|
|
jws, err := jwsFromContext(ctx)
|
2021-07-02 20:51:15 +00:00
|
|
|
if err != nil {
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, err)
|
2021-07-02 20:51:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-07-09 22:28:31 +00:00
|
|
|
prov, err := provisionerFromContext(ctx)
|
2021-07-02 20:51:15 +00:00
|
|
|
if err != nil {
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, err)
|
2021-07-02 22:21:17 +00:00
|
|
|
return
|
2021-07-02 20:51:15 +00:00
|
|
|
}
|
|
|
|
|
2021-11-26 16:27:42 +00:00
|
|
|
payload, err := payloadFromContext(ctx)
|
2021-07-02 20:51:15 +00:00
|
|
|
if err != nil {
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, err)
|
2021-07-02 22:21:17 +00:00
|
|
|
return
|
2021-07-02 20:51:15 +00:00
|
|
|
}
|
|
|
|
|
2021-11-26 16:27:42 +00:00
|
|
|
var p revokePayload
|
|
|
|
err = json.Unmarshal(payload.value, &p)
|
2021-07-02 22:21:17 +00:00
|
|
|
if err != nil {
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, acme.WrapErrorISE(err, "error unmarshaling payload"))
|
2021-07-02 22:21:17 +00:00
|
|
|
return
|
2021-07-02 20:51:15 +00:00
|
|
|
}
|
|
|
|
|
2021-11-26 16:27:42 +00:00
|
|
|
certBytes, err := base64.RawURLEncoding.DecodeString(p.Certificate)
|
2021-07-02 20:51:15 +00:00
|
|
|
if err != nil {
|
2021-11-26 16:27:42 +00:00
|
|
|
// in this case the most likely cause is a client that didn't properly encode the certificate
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, acme.WrapError(acme.ErrorMalformedType, err, "error base64url decoding payload certificate property"))
|
2021-07-02 22:21:17 +00:00
|
|
|
return
|
2021-07-02 20:51:15 +00:00
|
|
|
}
|
|
|
|
|
2021-07-02 22:21:17 +00:00
|
|
|
certToBeRevoked, err := x509.ParseCertificate(certBytes)
|
2021-07-02 20:51:15 +00:00
|
|
|
if err != nil {
|
2021-11-26 16:27:42 +00:00
|
|
|
// in this case a client may have encoded something different than a certificate
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, acme.WrapError(acme.ErrorMalformedType, err, "error parsing certificate"))
|
2021-07-09 15:51:31 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
serial := certToBeRevoked.SerialNumber.String()
|
2021-12-02 15:25:35 +00:00
|
|
|
dbCert, err := h.db.GetCertificateBySerial(ctx, serial)
|
2021-07-09 15:51:31 +00:00
|
|
|
if err != nil {
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, acme.WrapErrorISE(err, "error retrieving certificate by serial"))
|
2021-07-02 22:21:17 +00:00
|
|
|
return
|
2021-07-02 20:51:15 +00:00
|
|
|
}
|
|
|
|
|
2021-12-02 15:25:35 +00:00
|
|
|
if !bytes.Equal(dbCert.Leaf.Raw, certToBeRevoked.Raw) {
|
|
|
|
// this should never happen
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, acme.NewErrorISE("certificate raw bytes are not equal"))
|
2021-12-02 15:25:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-07-09 22:28:31 +00:00
|
|
|
if shouldCheckAccountFrom(jws) {
|
|
|
|
account, err := accountFromContext(ctx)
|
|
|
|
if err != nil {
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, err)
|
2021-07-09 22:28:31 +00:00
|
|
|
return
|
|
|
|
}
|
2021-12-02 15:25:35 +00:00
|
|
|
acmeErr := h.isAccountAuthorized(ctx, dbCert, certToBeRevoked, account)
|
|
|
|
if acmeErr != nil {
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, acmeErr)
|
2021-07-09 22:28:31 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// if account doesn't need to be checked, the JWS should be verified to be signed by the
|
|
|
|
// private key that belongs to the public key in the certificate to be revoked.
|
|
|
|
_, err := jws.Verify(certToBeRevoked.PublicKey)
|
|
|
|
if err != nil {
|
2021-11-28 19:30:36 +00:00
|
|
|
// TODO(hs): possible to determine an error vs. unauthorized and thus provide an ISE vs. Unauthorized?
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, wrapUnauthorizedError(certToBeRevoked, nil, "verification of jws using certificate public key failed", err))
|
2021-07-09 22:28:31 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2021-07-02 22:21:17 +00:00
|
|
|
|
2021-11-26 16:27:42 +00:00
|
|
|
hasBeenRevokedBefore, err := h.ca.IsRevoked(serial)
|
|
|
|
if err != nil {
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, acme.WrapErrorISE(err, "error retrieving revocation status of certificate"))
|
2021-11-26 16:27:42 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if hasBeenRevokedBefore {
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, acme.NewError(acme.ErrorAlreadyRevokedType, "certificate was already revoked"))
|
2021-11-26 16:27:42 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
reasonCode := p.ReasonCode
|
2021-07-09 15:51:31 +00:00
|
|
|
acmeErr := validateReasonCode(reasonCode)
|
|
|
|
if acmeErr != nil {
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, acmeErr)
|
2021-07-02 22:21:17 +00:00
|
|
|
return
|
2021-07-02 20:51:15 +00:00
|
|
|
}
|
|
|
|
|
2021-07-09 22:28:31 +00:00
|
|
|
// Authorize revocation by ACME provisioner
|
|
|
|
ctx = provisioner.NewContextWithMethod(ctx, provisioner.RevokeMethod)
|
|
|
|
err = prov.AuthorizeRevoke(ctx, "")
|
|
|
|
if err != nil {
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, acme.WrapErrorISE(err, "error authorizing revocation on provisioner"))
|
2021-07-09 22:28:31 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-07-09 15:51:31 +00:00
|
|
|
options := revokeOptions(serial, certToBeRevoked, reasonCode)
|
2021-07-02 20:51:15 +00:00
|
|
|
err = h.ca.Revoke(ctx, options)
|
|
|
|
if err != nil {
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, wrapRevokeErr(err))
|
2021-07-02 22:21:17 +00:00
|
|
|
return
|
2021-07-02 20:51:15 +00:00
|
|
|
}
|
|
|
|
|
2021-07-09 15:51:31 +00:00
|
|
|
logRevoke(w, options)
|
2021-07-02 23:56:14 +00:00
|
|
|
w.Header().Add("Link", link(h.linker.GetLink(ctx, DirectoryLinkType), "index"))
|
2021-07-02 20:51:15 +00:00
|
|
|
w.Write(nil)
|
|
|
|
}
|
2021-07-02 22:21:17 +00:00
|
|
|
|
2021-12-02 15:25:35 +00:00
|
|
|
// isAccountAuthorized checks if an ACME account that was retrieved earlier is authorized
|
|
|
|
// to revoke the certificate. An Account must always be valid in order to revoke a certificate.
|
|
|
|
// In case the certificate retrieved from the database belongs to the Account, the Account is
|
|
|
|
// authorized. If the certificate retrieved from the database doesn't belong to the Account,
|
|
|
|
// the identifiers in the certificate are extracted and compared against the (valid) Authorizations
|
|
|
|
// that are stored for the ACME Account. If these sets match, the Account is considered authorized
|
|
|
|
// to revoke the certificate. If this check fails, the client will receive an unauthorized error.
|
|
|
|
func (h *Handler) isAccountAuthorized(ctx context.Context, dbCert *acme.Certificate, certToBeRevoked *x509.Certificate, account *acme.Account) *acme.Error {
|
|
|
|
if !account.IsValid() {
|
|
|
|
return wrapUnauthorizedError(certToBeRevoked, nil, fmt.Sprintf("account '%s' has status '%s'", account.ID, account.Status), nil)
|
|
|
|
}
|
|
|
|
certificateBelongsToAccount := dbCert.AccountID == account.ID
|
|
|
|
if certificateBelongsToAccount {
|
2021-12-08 15:28:14 +00:00
|
|
|
return nil // return early
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(hs): according to RFC8555: 7.6, a server MUST consider the following accounts authorized
|
|
|
|
// to revoke a certificate:
|
|
|
|
//
|
|
|
|
// o the account that issued the certificate.
|
|
|
|
// o an account that holds authorizations for all of the identifiers in the certificate.
|
|
|
|
//
|
|
|
|
// We currently only support the first case. The second might result in step going OOM when
|
|
|
|
// large numbers of Authorizations are involved when the current nosql interface is in use.
|
|
|
|
// We want to protect users from this failure scenario, so that's why it hasn't been added yet.
|
|
|
|
// This issue is tracked in https://github.com/smallstep/certificates/issues/767
|
|
|
|
|
|
|
|
// not authorized; fail closed.
|
|
|
|
return wrapUnauthorizedError(certToBeRevoked, nil, fmt.Sprintf("account '%s' is not authorized", account.ID), nil)
|
2021-12-02 15:25:35 +00:00
|
|
|
}
|
|
|
|
|
2021-07-09 22:28:31 +00:00
|
|
|
// wrapRevokeErr is a best effort implementation to transform an error during
|
|
|
|
// revocation into an ACME error, so that clients can understand the error.
|
|
|
|
func wrapRevokeErr(err error) *acme.Error {
|
|
|
|
t := err.Error()
|
2021-11-28 19:30:36 +00:00
|
|
|
if strings.Contains(t, "is already revoked") {
|
2021-07-09 22:28:31 +00:00
|
|
|
return acme.NewError(acme.ErrorAlreadyRevokedType, t)
|
|
|
|
}
|
|
|
|
return acme.WrapErrorISE(err, "error when revoking certificate")
|
|
|
|
}
|
|
|
|
|
2021-11-26 16:27:42 +00:00
|
|
|
// unauthorizedError returns an ACME error indicating the request was
|
|
|
|
// not authorized to revoke the certificate.
|
2021-12-02 15:25:35 +00:00
|
|
|
func wrapUnauthorizedError(cert *x509.Certificate, unauthorizedIdentifiers []acme.Identifier, msg string, err error) *acme.Error {
|
2021-11-26 16:27:42 +00:00
|
|
|
var acmeErr *acme.Error
|
|
|
|
if err == nil {
|
|
|
|
acmeErr = acme.NewError(acme.ErrorUnauthorizedType, msg)
|
|
|
|
} else {
|
|
|
|
acmeErr = acme.WrapError(acme.ErrorUnauthorizedType, err, msg)
|
|
|
|
}
|
2021-12-02 15:25:35 +00:00
|
|
|
acmeErr.Status = http.StatusForbidden // RFC8555 7.6 shows example with 403
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case len(unauthorizedIdentifiers) > 0:
|
|
|
|
identifier := unauthorizedIdentifiers[0] // picking the first; compound may be an option too?
|
|
|
|
acmeErr.Detail = fmt.Sprintf("No authorization provided for name %s", identifier.Value)
|
|
|
|
case cert.Subject.String() != "":
|
|
|
|
acmeErr.Detail = fmt.Sprintf("No authorization provided for name %s", cert.Subject.CommonName)
|
|
|
|
default:
|
|
|
|
acmeErr.Detail = "No authorization provided"
|
|
|
|
}
|
2021-11-26 16:27:42 +00:00
|
|
|
|
|
|
|
return acmeErr
|
|
|
|
}
|
|
|
|
|
2021-07-09 15:51:31 +00:00
|
|
|
// logRevoke logs successful revocation of certificate
|
|
|
|
func logRevoke(w http.ResponseWriter, ri *authority.RevokeOptions) {
|
|
|
|
if rl, ok := w.(logging.ResponseLogger); ok {
|
|
|
|
rl.WithFields(map[string]interface{}{
|
|
|
|
"serial": ri.Serial,
|
|
|
|
"reasonCode": ri.ReasonCode,
|
|
|
|
"reason": ri.Reason,
|
|
|
|
"passiveOnly": ri.PassiveOnly,
|
|
|
|
"ACME": ri.ACME,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// validateReasonCode validates the revocation reason
|
|
|
|
func validateReasonCode(reasonCode *int) *acme.Error {
|
|
|
|
if reasonCode != nil && ((*reasonCode < ocsp.Unspecified || *reasonCode > ocsp.AACompromise) || *reasonCode == 7) {
|
|
|
|
return acme.NewError(acme.ErrorBadRevocationReasonType, "reasonCode out of bounds")
|
|
|
|
}
|
|
|
|
// NOTE: it's possible to add additional requirements to the reason code:
|
|
|
|
// The server MAY disallow a subset of reasonCodes from being
|
|
|
|
// used by the user. If a request contains a disallowed reasonCode,
|
|
|
|
// then the server MUST reject it with the error type
|
|
|
|
// "urn:ietf:params:acme:error:badRevocationReason"
|
|
|
|
// No additional checks have been implemented so far.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-03 14:06:28 +00:00
|
|
|
// revokeOptions determines the RevokeOptions for the Authority to use in revocation
|
2021-07-09 15:51:31 +00:00
|
|
|
func revokeOptions(serial string, certToBeRevoked *x509.Certificate, reasonCode *int) *authority.RevokeOptions {
|
|
|
|
opts := &authority.RevokeOptions{
|
|
|
|
Serial: serial,
|
|
|
|
ACME: true,
|
|
|
|
Crt: certToBeRevoked,
|
|
|
|
}
|
|
|
|
if reasonCode != nil { // NOTE: when implementing CRL and/or OCSP, and reason code is missing, CRL entry extension should be omitted
|
|
|
|
opts.Reason = reason(*reasonCode)
|
|
|
|
opts.ReasonCode = *reasonCode
|
|
|
|
}
|
|
|
|
return opts
|
|
|
|
}
|
|
|
|
|
|
|
|
// reason transforms an integer reason code to a
|
|
|
|
// textual description of the revocation reason.
|
2021-07-02 22:21:17 +00:00
|
|
|
func reason(reasonCode int) string {
|
|
|
|
switch reasonCode {
|
|
|
|
case ocsp.Unspecified:
|
|
|
|
return "unspecified reason"
|
|
|
|
case ocsp.KeyCompromise:
|
|
|
|
return "key compromised"
|
|
|
|
case ocsp.CACompromise:
|
|
|
|
return "ca compromised"
|
|
|
|
case ocsp.AffiliationChanged:
|
|
|
|
return "affiliation changed"
|
|
|
|
case ocsp.Superseded:
|
|
|
|
return "superseded"
|
|
|
|
case ocsp.CessationOfOperation:
|
|
|
|
return "cessation of operation"
|
|
|
|
case ocsp.CertificateHold:
|
|
|
|
return "certificate hold"
|
|
|
|
case ocsp.RemoveFromCRL:
|
|
|
|
return "remove from crl"
|
|
|
|
case ocsp.PrivilegeWithdrawn:
|
|
|
|
return "privilege withdrawn"
|
|
|
|
case ocsp.AACompromise:
|
|
|
|
return "aa compromised"
|
|
|
|
default:
|
|
|
|
return "unspecified reason"
|
|
|
|
}
|
|
|
|
}
|
2021-07-02 23:56:14 +00:00
|
|
|
|
2021-07-09 22:28:31 +00:00
|
|
|
// shouldCheckAccountFrom indicates whether an account should be
|
2021-07-02 23:56:14 +00:00
|
|
|
// retrieved from the context, so that it can be used for
|
2021-07-09 22:28:31 +00:00
|
|
|
// additional checks. This should only be done when no JWK
|
|
|
|
// can be extracted from the request, as that would indicate
|
|
|
|
// that the revocation request was signed with a certificate
|
|
|
|
// key pair (and not an account key pair). Looking up such
|
|
|
|
// a JWK would result in no Account being found.
|
|
|
|
func shouldCheckAccountFrom(jws *jose.JSONWebSignature) bool {
|
2021-07-02 23:56:14 +00:00
|
|
|
return !canExtractJWKFrom(jws)
|
|
|
|
}
|