4
0

sshRekey.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 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. a := mustAuthority(ctx)
  54. signOpts, err := a.Authorize(ctx, body.OTT)
  55. if err != nil {
  56. render.Error(w, errs.UnauthorizedErr(err))
  57. return
  58. }
  59. oldCert, _, err := provisioner.ExtractSSHPOPCert(body.OTT)
  60. if err != nil {
  61. render.Error(w, errs.InternalServerErr(err))
  62. return
  63. }
  64. newCert, err := a.RekeySSH(ctx, oldCert, publicKey, signOpts...)
  65. if err != nil {
  66. render.Error(w, errs.ForbiddenErr(err, "error rekeying ssh certificate"))
  67. return
  68. }
  69. // Match identity cert with the SSH cert
  70. notBefore := time.Unix(int64(oldCert.ValidAfter), 0)
  71. notAfter := time.Unix(int64(oldCert.ValidBefore), 0)
  72. identity, err := renewIdentityCertificate(r, notBefore, notAfter)
  73. if err != nil {
  74. render.Error(w, errs.ForbiddenErr(err, "error renewing identity certificate"))
  75. return
  76. }
  77. render.JSONStatus(w, &SSHRekeyResponse{
  78. Certificate: SSHCertificate{newCert},
  79. IdentityCertificate: identity,
  80. }, http.StatusCreated)
  81. }