revoke.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package api
  2. import (
  3. "net/http"
  4. "github.com/pkg/errors"
  5. "github.com/smallstep/certificates/authority"
  6. "github.com/smallstep/certificates/logging"
  7. "golang.org/x/crypto/ocsp"
  8. )
  9. // RevokeResponse is the response object that returns the health of the server.
  10. type RevokeResponse struct {
  11. Status string `json:"status"`
  12. }
  13. // RevokeRequest is the request body for a revocation request.
  14. type RevokeRequest struct {
  15. Serial string `json:"serial"`
  16. OTT string `json:"ott"`
  17. ReasonCode int `json:"reasonCode"`
  18. Reason string `json:"reason"`
  19. Passive bool `json:"passive"`
  20. }
  21. // Validate checks the fields of the RevokeRequest and returns nil if they are ok
  22. // or an error if something is wrong.
  23. func (r *RevokeRequest) Validate() (err error) {
  24. if r.Serial == "" {
  25. return BadRequest(errors.New("missing serial"))
  26. }
  27. if r.ReasonCode < ocsp.Unspecified || r.ReasonCode > ocsp.AACompromise {
  28. return BadRequest(errors.New("reasonCode out of bounds"))
  29. }
  30. if !r.Passive {
  31. return NotImplemented(errors.New("non-passive revocation not implemented"))
  32. }
  33. return
  34. }
  35. // Revoke supports handful of different methods that revoke a Certificate.
  36. //
  37. // NOTE: currently only Passive revocation is supported.
  38. //
  39. // TODO: Add CRL and OCSP support.
  40. func (h *caHandler) Revoke(w http.ResponseWriter, r *http.Request) {
  41. var body RevokeRequest
  42. if err := ReadJSON(r.Body, &body); err != nil {
  43. WriteError(w, BadRequest(errors.Wrap(err, "error reading request body")))
  44. return
  45. }
  46. if err := body.Validate(); err != nil {
  47. WriteError(w, err)
  48. return
  49. }
  50. opts := &authority.RevokeOptions{
  51. Serial: body.Serial,
  52. Reason: body.Reason,
  53. ReasonCode: body.ReasonCode,
  54. PassiveOnly: body.Passive,
  55. }
  56. // A token indicates that we are using the api via a provisioner token,
  57. // otherwise it is assumed that the certificate is revoking itself over mTLS.
  58. if len(body.OTT) > 0 {
  59. logOtt(w, body.OTT)
  60. opts.OTT = body.OTT
  61. } else {
  62. // If no token is present, then the request must be made over mTLS and
  63. // the client certificate Serial Number must match the serial number
  64. // being revoked.
  65. if r.TLS == nil || len(r.TLS.PeerCertificates) == 0 {
  66. WriteError(w, BadRequest(errors.New("missing ott or peer certificate")))
  67. return
  68. }
  69. opts.Crt = r.TLS.PeerCertificates[0]
  70. logCertificate(w, opts.Crt)
  71. opts.MTLS = true
  72. }
  73. if err := h.Authority.Revoke(opts); err != nil {
  74. WriteError(w, Forbidden(err))
  75. return
  76. }
  77. logRevoke(w, opts)
  78. JSON(w, &RevokeResponse{Status: "ok"})
  79. }
  80. func logRevoke(w http.ResponseWriter, ri *authority.RevokeOptions) {
  81. if rl, ok := w.(logging.ResponseLogger); ok {
  82. rl.WithFields(map[string]interface{}{
  83. "serial": ri.Serial,
  84. "reasonCode": ri.ReasonCode,
  85. "reason": ri.Reason,
  86. "passiveOnly": ri.PassiveOnly,
  87. "mTLS": ri.MTLS,
  88. })
  89. }
  90. }