sshRekey.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package api
  2. import (
  3. "net/http"
  4. "time"
  5. "github.com/smallstep/certificates/authority/provisioner"
  6. "github.com/smallstep/certificates/errs"
  7. "golang.org/x/crypto/ssh"
  8. )
  9. // SSHRekeyRequest is the request body of an SSH certificate request.
  10. type SSHRekeyRequest struct {
  11. OTT string `json:"ott"`
  12. PublicKey []byte `json:"publicKey"` //base64 encoded
  13. }
  14. // Validate validates the SSHSignRekey.
  15. func (s *SSHRekeyRequest) Validate() error {
  16. switch {
  17. case s.OTT == "":
  18. return errs.BadRequest("missing or empty ott")
  19. case len(s.PublicKey) == 0:
  20. return errs.BadRequest("missing or empty public key")
  21. default:
  22. return nil
  23. }
  24. }
  25. // SSHRekeyResponse is the response object that returns the SSH certificate.
  26. type SSHRekeyResponse struct {
  27. Certificate SSHCertificate `json:"crt"`
  28. IdentityCertificate []Certificate `json:"identityCrt,omitempty"`
  29. }
  30. // SSHRekey is an HTTP handler that reads an RekeySSHRequest with a one-time-token
  31. // (ott) from the body and creates a new SSH certificate with the information in
  32. // the request.
  33. func (h *caHandler) SSHRekey(w http.ResponseWriter, r *http.Request) {
  34. var body SSHRekeyRequest
  35. if err := ReadJSON(r.Body, &body); err != nil {
  36. WriteError(w, errs.BadRequestErr(err, "error reading request body"))
  37. return
  38. }
  39. logOtt(w, body.OTT)
  40. if err := body.Validate(); err != nil {
  41. WriteError(w, err)
  42. return
  43. }
  44. publicKey, err := ssh.ParsePublicKey(body.PublicKey)
  45. if err != nil {
  46. WriteError(w, errs.BadRequestErr(err, "error parsing publicKey"))
  47. return
  48. }
  49. ctx := provisioner.NewContextWithMethod(r.Context(), provisioner.SSHRekeyMethod)
  50. signOpts, err := h.Authority.Authorize(ctx, body.OTT)
  51. if err != nil {
  52. WriteError(w, errs.UnauthorizedErr(err))
  53. return
  54. }
  55. oldCert, _, err := provisioner.ExtractSSHPOPCert(body.OTT)
  56. if err != nil {
  57. WriteError(w, errs.InternalServerErr(err))
  58. }
  59. newCert, err := h.Authority.RekeySSH(ctx, oldCert, publicKey, signOpts...)
  60. if err != nil {
  61. WriteError(w, errs.ForbiddenErr(err, "error signing ssh certificate"))
  62. return
  63. }
  64. // Match identity cert with the SSH cert
  65. notBefore := time.Unix(int64(oldCert.ValidAfter), 0)
  66. notAfter := time.Unix(int64(oldCert.ValidBefore), 0)
  67. identity, err := h.renewIdentityCertificate(r, notBefore, notAfter)
  68. if err != nil {
  69. WriteError(w, errs.ForbiddenErr(err, "error signing identity certificate"))
  70. return
  71. }
  72. JSONStatus(w, &SSHRekeyResponse{
  73. Certificate: SSHCertificate{newCert},
  74. IdentityCertificate: identity,
  75. }, http.StatusCreated)
  76. }