sshRekey.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 err := read.JSON(r.Body, &body); err != nil {
  38. render.Error(w, errs.BadRequestErr(err, "error reading request body"))
  39. return
  40. }
  41. logOtt(w, body.OTT)
  42. if err := body.Validate(); err != nil {
  43. render.Error(w, err)
  44. return
  45. }
  46. publicKey, err := ssh.ParsePublicKey(body.PublicKey)
  47. if err != nil {
  48. render.Error(w, errs.BadRequestErr(err, "error parsing publicKey"))
  49. return
  50. }
  51. ctx := provisioner.NewContextWithMethod(r.Context(), provisioner.SSHRekeyMethod)
  52. ctx = provisioner.NewContextWithToken(ctx, body.OTT)
  53. signOpts, err := h.Authority.Authorize(ctx, body.OTT)
  54. if err != nil {
  55. render.Error(w, errs.UnauthorizedErr(err))
  56. return
  57. }
  58. oldCert, _, err := provisioner.ExtractSSHPOPCert(body.OTT)
  59. if err != nil {
  60. render.Error(w, errs.InternalServerErr(err))
  61. return
  62. }
  63. newCert, err := h.Authority.RekeySSH(ctx, oldCert, publicKey, signOpts...)
  64. if err != nil {
  65. render.Error(w, errs.ForbiddenErr(err, "error rekeying ssh certificate"))
  66. return
  67. }
  68. // Match identity cert with the SSH cert
  69. notBefore := time.Unix(int64(oldCert.ValidAfter), 0)
  70. notAfter := time.Unix(int64(oldCert.ValidBefore), 0)
  71. identity, err := h.renewIdentityCertificate(r, notBefore, notAfter)
  72. if err != nil {
  73. render.Error(w, errs.ForbiddenErr(err, "error renewing identity certificate"))
  74. return
  75. }
  76. render.JSONStatus(w, &SSHRekeyResponse{
  77. Certificate: SSHCertificate{newCert},
  78. IdentityCertificate: identity,
  79. }, http.StatusCreated)
  80. }