sshRekey.go 2.6 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 err := read.JSON(r.Body, &body); err != nil {
  38. WriteError(w, errs.BadRequestErr(err, "error reading request body"))
  39. return
  40. }
  41. logOtt(w, body.OTT)
  42. if err := body.Validate(); err != nil {
  43. WriteError(w, err)
  44. return
  45. }
  46. publicKey, err := ssh.ParsePublicKey(body.PublicKey)
  47. if err != nil {
  48. WriteError(w, errs.BadRequestErr(err, "error parsing publicKey"))
  49. return
  50. }
  51. ctx := provisioner.NewContextWithMethod(r.Context(), provisioner.SSHRekeyMethod)
  52. signOpts, err := h.Authority.Authorize(ctx, body.OTT)
  53. if err != nil {
  54. WriteError(w, errs.UnauthorizedErr(err))
  55. return
  56. }
  57. oldCert, _, err := provisioner.ExtractSSHPOPCert(body.OTT)
  58. if err != nil {
  59. WriteError(w, errs.InternalServerErr(err))
  60. }
  61. newCert, err := h.Authority.RekeySSH(ctx, oldCert, publicKey, signOpts...)
  62. if err != nil {
  63. WriteError(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. WriteError(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. }