sshRevoke.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package api
  2. import (
  3. "net/http"
  4. "github.com/smallstep/certificates/authority"
  5. "github.com/smallstep/certificates/authority/provisioner"
  6. "github.com/smallstep/certificates/errs"
  7. "github.com/smallstep/certificates/logging"
  8. "golang.org/x/crypto/ocsp"
  9. )
  10. // SSHRevokeResponse is the response object that returns the health of the server.
  11. type SSHRevokeResponse struct {
  12. Status string `json:"status"`
  13. }
  14. // SSHRevokeRequest is the request body for a revocation request.
  15. type SSHRevokeRequest struct {
  16. Serial string `json:"serial"`
  17. OTT string `json:"ott"`
  18. ReasonCode int `json:"reasonCode"`
  19. Reason string `json:"reason"`
  20. Passive bool `json:"passive"`
  21. }
  22. // Validate checks the fields of the RevokeRequest and returns nil if they are ok
  23. // or an error if something is wrong.
  24. func (r *SSHRevokeRequest) Validate() (err error) {
  25. if r.Serial == "" {
  26. return errs.BadRequest("missing serial")
  27. }
  28. if r.ReasonCode < ocsp.Unspecified || r.ReasonCode > ocsp.AACompromise {
  29. return errs.BadRequest("reasonCode out of bounds")
  30. }
  31. if !r.Passive {
  32. return errs.NotImplemented("non-passive revocation not implemented")
  33. }
  34. if r.OTT == "" {
  35. return errs.BadRequest("missing ott")
  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. func (h *caHandler) SSHRevoke(w http.ResponseWriter, r *http.Request) {
  43. var body SSHRevokeRequest
  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(r.Context(), provisioner.SSHRevokeMethod)
  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. logOtt(w, body.OTT)
  62. if _, err := h.Authority.Authorize(ctx, body.OTT); err != nil {
  63. WriteError(w, errs.UnauthorizedErr(err))
  64. return
  65. }
  66. opts.OTT = body.OTT
  67. if err := h.Authority.Revoke(ctx, opts); err != nil {
  68. WriteError(w, errs.ForbiddenErr(err))
  69. return
  70. }
  71. logSSHRevoke(w, opts)
  72. JSON(w, &SSHRevokeResponse{Status: "ok"})
  73. }
  74. func logSSHRevoke(w http.ResponseWriter, ri *authority.RevokeOptions) {
  75. if rl, ok := w.(logging.ResponseLogger); ok {
  76. rl.WithFields(map[string]interface{}{
  77. "serial": ri.Serial,
  78. "reasonCode": ri.ReasonCode,
  79. "reason": ri.Reason,
  80. "passiveOnly": ri.PassiveOnly,
  81. "mTLS": ri.MTLS,
  82. "ssh": true,
  83. })
  84. }
  85. }