4
0

revoke.go 3.5 KB

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