4
0

sshRekey.go 2.5 KB

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