2019-03-05 08:07:13 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2022-08-09 18:04:00 +00:00
|
|
|
"math/big"
|
2019-03-05 08:07:13 +00:00
|
|
|
"net/http"
|
|
|
|
|
2022-03-18 13:25:34 +00:00
|
|
|
"golang.org/x/crypto/ocsp"
|
|
|
|
|
|
|
|
"github.com/smallstep/certificates/api/read"
|
2022-03-30 08:22:22 +00:00
|
|
|
"github.com/smallstep/certificates/api/render"
|
2019-03-05 08:07:13 +00:00
|
|
|
"github.com/smallstep/certificates/authority"
|
2019-10-28 18:50:43 +00:00
|
|
|
"github.com/smallstep/certificates/authority/provisioner"
|
2019-12-16 07:54:25 +00:00
|
|
|
"github.com/smallstep/certificates/errs"
|
2019-03-05 08:07:13 +00:00
|
|
|
"github.com/smallstep/certificates/logging"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RevokeResponse is the response object that returns the health of the server.
|
|
|
|
type RevokeResponse struct {
|
|
|
|
Status string `json:"status"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// RevokeRequest is the request body for a revocation request.
|
|
|
|
type RevokeRequest struct {
|
|
|
|
Serial string `json:"serial"`
|
|
|
|
OTT string `json:"ott"`
|
|
|
|
ReasonCode int `json:"reasonCode"`
|
|
|
|
Reason string `json:"reason"`
|
|
|
|
Passive bool `json:"passive"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate checks the fields of the RevokeRequest and returns nil if they are ok
|
|
|
|
// or an error if something is wrong.
|
|
|
|
func (r *RevokeRequest) Validate() (err error) {
|
|
|
|
if r.Serial == "" {
|
2020-01-24 06:04:34 +00:00
|
|
|
return errs.BadRequest("missing serial")
|
2019-03-05 08:07:13 +00:00
|
|
|
}
|
2022-08-09 18:04:00 +00:00
|
|
|
sn, ok := new(big.Int).SetString(r.Serial, 0)
|
|
|
|
if !ok {
|
2022-08-11 16:34:04 +00:00
|
|
|
return errs.BadRequest("'%s' is not a valid serial number - use a base 10 representation or a base 16 representation with '0x' prefix", r.Serial)
|
2022-08-09 18:04:00 +00:00
|
|
|
}
|
|
|
|
r.Serial = sn.String()
|
2019-03-05 08:07:13 +00:00
|
|
|
if r.ReasonCode < ocsp.Unspecified || r.ReasonCode > ocsp.AACompromise {
|
2020-01-24 06:04:34 +00:00
|
|
|
return errs.BadRequest("reasonCode out of bounds")
|
2019-03-05 08:07:13 +00:00
|
|
|
}
|
|
|
|
if !r.Passive {
|
2020-01-24 06:04:34 +00:00
|
|
|
return errs.NotImplemented("non-passive revocation not implemented")
|
2019-03-05 08:07:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Revoke supports handful of different methods that revoke a Certificate.
|
|
|
|
//
|
|
|
|
// NOTE: currently only Passive revocation is supported.
|
|
|
|
//
|
|
|
|
// TODO: Add CRL and OCSP support.
|
2022-04-26 19:58:40 +00:00
|
|
|
func Revoke(w http.ResponseWriter, r *http.Request) {
|
2019-03-05 08:07:13 +00:00
|
|
|
var body RevokeRequest
|
2022-03-18 13:25:34 +00:00
|
|
|
if err := read.JSON(r.Body, &body); err != nil {
|
2024-05-15 22:31:24 +00:00
|
|
|
render.Error(w, r, errs.BadRequestErr(err, "error reading request body"))
|
2019-03-05 08:07:13 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := body.Validate(); err != nil {
|
2024-05-15 22:31:24 +00:00
|
|
|
render.Error(w, r, err)
|
2019-03-05 08:07:13 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := &authority.RevokeOptions{
|
|
|
|
Serial: body.Serial,
|
|
|
|
Reason: body.Reason,
|
|
|
|
ReasonCode: body.ReasonCode,
|
|
|
|
PassiveOnly: body.Passive,
|
|
|
|
}
|
|
|
|
|
2022-04-26 19:58:40 +00:00
|
|
|
ctx := provisioner.NewContextWithMethod(r.Context(), provisioner.RevokeMethod)
|
|
|
|
a := mustAuthority(ctx)
|
|
|
|
|
2019-03-05 08:07:13 +00:00
|
|
|
// A token indicates that we are using the api via a provisioner token,
|
|
|
|
// otherwise it is assumed that the certificate is revoking itself over mTLS.
|
2024-03-12 21:29:55 +00:00
|
|
|
if body.OTT != "" {
|
2019-03-05 08:07:13 +00:00
|
|
|
logOtt(w, body.OTT)
|
2022-04-26 19:58:40 +00:00
|
|
|
if _, err := a.Authorize(ctx, body.OTT); err != nil {
|
2024-05-15 22:31:24 +00:00
|
|
|
render.Error(w, r, errs.UnauthorizedErr(err))
|
2019-10-28 18:50:43 +00:00
|
|
|
return
|
|
|
|
}
|
2019-03-05 08:07:13 +00:00
|
|
|
opts.OTT = body.OTT
|
|
|
|
} else {
|
|
|
|
// If no token is present, then the request must be made over mTLS and
|
|
|
|
// the client certificate Serial Number must match the serial number
|
|
|
|
// being revoked.
|
|
|
|
if r.TLS == nil || len(r.TLS.PeerCertificates) == 0 {
|
2024-05-15 22:31:24 +00:00
|
|
|
render.Error(w, r, errs.BadRequest("missing ott or client certificate"))
|
2019-03-05 08:07:13 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
opts.Crt = r.TLS.PeerCertificates[0]
|
2019-10-28 18:50:43 +00:00
|
|
|
if opts.Crt.SerialNumber.String() != opts.Serial {
|
2024-05-15 22:31:24 +00:00
|
|
|
render.Error(w, r, errs.BadRequest("serial number in client certificate different than body"))
|
2019-10-28 18:50:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// TODO: should probably be checking if the certificate was revoked here.
|
|
|
|
// Will need to thread that request down to the authority, so will need
|
|
|
|
// to add API for that.
|
2020-08-12 22:50:45 +00:00
|
|
|
LogCertificate(w, opts.Crt)
|
2019-03-05 08:07:13 +00:00
|
|
|
opts.MTLS = true
|
|
|
|
}
|
|
|
|
|
2022-04-26 19:58:40 +00:00
|
|
|
if err := a.Revoke(ctx, opts); err != nil {
|
2024-05-15 22:31:24 +00:00
|
|
|
render.Error(w, r, errs.ForbiddenErr(err, "error revoking certificate"))
|
2019-03-05 08:07:13 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
logRevoke(w, opts)
|
2024-05-15 22:31:24 +00:00
|
|
|
render.JSON(w, r, &RevokeResponse{Status: "ok"})
|
2019-03-05 08:07:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
"mTLS": ri.MTLS,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|