2019-10-28 18:50:43 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/smallstep/certificates/authority"
|
|
|
|
"github.com/smallstep/certificates/authority/provisioner"
|
2019-12-16 07:54:25 +00:00
|
|
|
"github.com/smallstep/certificates/errs"
|
2019-10-28 18:50:43 +00:00
|
|
|
"github.com/smallstep/certificates/logging"
|
|
|
|
"golang.org/x/crypto/ocsp"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SSHRevokeResponse is the response object that returns the health of the server.
|
|
|
|
type SSHRevokeResponse struct {
|
|
|
|
Status string `json:"status"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// SSHRevokeRequest is the request body for a revocation request.
|
|
|
|
type SSHRevokeRequest 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 *SSHRevokeRequest) Validate() (err error) {
|
|
|
|
if r.Serial == "" {
|
2020-01-24 06:04:34 +00:00
|
|
|
return errs.BadRequest("missing serial")
|
2019-10-28 18:50:43 +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-10-28 18:50:43 +00:00
|
|
|
}
|
|
|
|
if !r.Passive {
|
2020-01-24 06:04:34 +00:00
|
|
|
return errs.NotImplemented("non-passive revocation not implemented")
|
2019-10-28 18:50:43 +00:00
|
|
|
}
|
2021-10-08 18:59:57 +00:00
|
|
|
if r.OTT == "" {
|
2020-01-24 06:04:34 +00:00
|
|
|
return errs.BadRequest("missing ott")
|
2019-10-28 18:50:43 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Revoke supports handful of different methods that revoke a Certificate.
|
|
|
|
//
|
|
|
|
// NOTE: currently only Passive revocation is supported.
|
|
|
|
func (h *caHandler) SSHRevoke(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var body SSHRevokeRequest
|
|
|
|
if err := ReadJSON(r.Body, &body); err != nil {
|
2021-11-19 02:44:58 +00:00
|
|
|
WriteError(w, errs.BadRequestErr(err, "error reading request body"))
|
2019-10-28 18:50:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := body.Validate(); err != nil {
|
|
|
|
WriteError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := &authority.RevokeOptions{
|
|
|
|
Serial: body.Serial,
|
|
|
|
Reason: body.Reason,
|
|
|
|
ReasonCode: body.ReasonCode,
|
|
|
|
PassiveOnly: body.Passive,
|
|
|
|
}
|
|
|
|
|
2020-03-11 02:01:45 +00:00
|
|
|
ctx := provisioner.NewContextWithMethod(r.Context(), provisioner.SSHRevokeMethod)
|
2019-10-28 18:50:43 +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.
|
|
|
|
logOtt(w, body.OTT)
|
|
|
|
if _, err := h.Authority.Authorize(ctx, body.OTT); err != nil {
|
2020-01-24 06:04:34 +00:00
|
|
|
WriteError(w, errs.UnauthorizedErr(err))
|
2019-10-28 18:50:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
opts.OTT = body.OTT
|
|
|
|
|
|
|
|
if err := h.Authority.Revoke(ctx, opts); err != nil {
|
2021-11-23 19:52:55 +00:00
|
|
|
WriteError(w, errs.ForbiddenErr(err, "error revoking ssh certificate"))
|
2019-10-28 18:50:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
logSSHRevoke(w, opts)
|
|
|
|
JSON(w, &SSHRevokeResponse{Status: "ok"})
|
|
|
|
}
|
|
|
|
|
|
|
|
func logSSHRevoke(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,
|
|
|
|
"ssh": true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|