sshRekey.go 2.5 KB

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