revoke.go 3.4 KB

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