4
0

revoke.go 3.4 KB

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