4
0

revoke.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package api
  2. import (
  3. "math/big"
  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. sn, ok := new(big.Int).SetString(r.Serial, 0)
  32. if !ok {
  33. return errs.BadRequest("'%s' is not a valid serial number - use a base 10 representation or a base 16 representation with '0x' prefix", r.Serial)
  34. }
  35. r.Serial = sn.String()
  36. if r.ReasonCode < ocsp.Unspecified || r.ReasonCode > ocsp.AACompromise {
  37. return errs.BadRequest("reasonCode out of bounds")
  38. }
  39. if !r.Passive {
  40. return errs.NotImplemented("non-passive revocation not implemented")
  41. }
  42. return
  43. }
  44. // Revoke supports handful of different methods that revoke a Certificate.
  45. //
  46. // NOTE: currently only Passive revocation is supported.
  47. //
  48. // TODO: Add CRL and OCSP support.
  49. func Revoke(w http.ResponseWriter, r *http.Request) {
  50. var body RevokeRequest
  51. if err := read.JSON(r.Body, &body); err != nil {
  52. render.Error(w, r, errs.BadRequestErr(err, "error reading request body"))
  53. return
  54. }
  55. if err := body.Validate(); err != nil {
  56. render.Error(w, r, err)
  57. return
  58. }
  59. opts := &authority.RevokeOptions{
  60. Serial: body.Serial,
  61. Reason: body.Reason,
  62. ReasonCode: body.ReasonCode,
  63. PassiveOnly: body.Passive,
  64. }
  65. ctx := provisioner.NewContextWithMethod(r.Context(), provisioner.RevokeMethod)
  66. a := mustAuthority(ctx)
  67. // A token indicates that we are using the api via a provisioner token,
  68. // otherwise it is assumed that the certificate is revoking itself over mTLS.
  69. if body.OTT != "" {
  70. logOtt(w, body.OTT)
  71. if _, err := a.Authorize(ctx, body.OTT); err != nil {
  72. render.Error(w, r, errs.UnauthorizedErr(err))
  73. return
  74. }
  75. opts.OTT = body.OTT
  76. } else {
  77. // If no token is present, then the request must be made over mTLS and
  78. // the client certificate Serial Number must match the serial number
  79. // being revoked.
  80. if r.TLS == nil || len(r.TLS.PeerCertificates) == 0 {
  81. render.Error(w, r, errs.BadRequest("missing ott or client certificate"))
  82. return
  83. }
  84. opts.Crt = r.TLS.PeerCertificates[0]
  85. if opts.Crt.SerialNumber.String() != opts.Serial {
  86. render.Error(w, r, errs.BadRequest("serial number in client certificate different than body"))
  87. return
  88. }
  89. // TODO: should probably be checking if the certificate was revoked here.
  90. // Will need to thread that request down to the authority, so will need
  91. // to add API for that.
  92. LogCertificate(w, opts.Crt)
  93. opts.MTLS = true
  94. }
  95. if err := a.Revoke(ctx, opts); err != nil {
  96. render.Error(w, r, errs.ForbiddenErr(err, "error revoking certificate"))
  97. return
  98. }
  99. logRevoke(w, opts)
  100. render.JSON(w, r, &RevokeResponse{Status: "ok"})
  101. }
  102. func logRevoke(w http.ResponseWriter, ri *authority.RevokeOptions) {
  103. if rl, ok := w.(logging.ResponseLogger); ok {
  104. rl.WithFields(map[string]interface{}{
  105. "serial": ri.Serial,
  106. "reasonCode": ri.ReasonCode,
  107. "reason": ri.Reason,
  108. "passiveOnly": ri.PassiveOnly,
  109. "mTLS": ri.MTLS,
  110. })
  111. }
  112. }