sshRekey.go 2.7 KB

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