4
0

sshRevoke.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. // SSHRevokeResponse is the response object that returns the health of the server.
  12. type SSHRevokeResponse struct {
  13. Status string `json:"status"`
  14. }
  15. // SSHRevokeRequest is the request body for a revocation request.
  16. type SSHRevokeRequest 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 *SSHRevokeRequest) 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. if len(r.OTT) == 0 {
  36. return errs.BadRequest("missing ott")
  37. }
  38. return
  39. }
  40. // Revoke supports handful of different methods that revoke a Certificate.
  41. //
  42. // NOTE: currently only Passive revocation is supported.
  43. func (h *caHandler) SSHRevoke(w http.ResponseWriter, r *http.Request) {
  44. var body SSHRevokeRequest
  45. if err := ReadJSON(r.Body, &body); err != nil {
  46. WriteError(w, errs.Wrap(http.StatusBadRequest, 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.SSHRevokeMethod)
  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. 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. if err := h.Authority.Revoke(ctx, opts); err != nil {
  69. WriteError(w, errs.ForbiddenErr(err))
  70. return
  71. }
  72. logSSHRevoke(w, opts)
  73. JSON(w, &SSHRevokeResponse{Status: "ok"})
  74. }
  75. func logSSHRevoke(w http.ResponseWriter, ri *authority.RevokeOptions) {
  76. if rl, ok := w.(logging.ResponseLogger); ok {
  77. rl.WithFields(map[string]interface{}{
  78. "serial": ri.Serial,
  79. "reasonCode": ri.ReasonCode,
  80. "reason": ri.Reason,
  81. "passiveOnly": ri.PassiveOnly,
  82. "mTLS": ri.MTLS,
  83. "ssh": true,
  84. })
  85. }
  86. }