sshRenew.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package api
  2. import (
  3. "crypto/x509"
  4. "net/http"
  5. "time"
  6. "github.com/pkg/errors"
  7. "github.com/smallstep/certificates/api/read"
  8. "github.com/smallstep/certificates/api/render"
  9. "github.com/smallstep/certificates/authority/provisioner"
  10. "github.com/smallstep/certificates/errs"
  11. )
  12. // SSHRenewRequest is the request body of an SSH certificate request.
  13. type SSHRenewRequest struct {
  14. OTT string `json:"ott"`
  15. }
  16. // Validate validates the SSHSignRequest.
  17. func (s *SSHRenewRequest) Validate() error {
  18. switch {
  19. case s.OTT == "":
  20. return errs.BadRequest("missing or empty ott")
  21. default:
  22. return nil
  23. }
  24. }
  25. // SSHRenewResponse is the response object that returns the SSH certificate.
  26. type SSHRenewResponse struct {
  27. Certificate SSHCertificate `json:"crt"`
  28. IdentityCertificate []Certificate `json:"identityCrt,omitempty"`
  29. }
  30. // SSHRenew is an HTTP handler that reads an RenewSSHRequest with a one-time-token
  31. // (ott) from the body and creates a new SSH certificate with the information in
  32. // the request.
  33. func (h *caHandler) SSHRenew(w http.ResponseWriter, r *http.Request) {
  34. var body SSHRenewRequest
  35. if read.JSON(w, r, &body) {
  36. return
  37. }
  38. logOtt(w, body.OTT)
  39. if err := body.Validate(); err != nil {
  40. render.Error(w, err)
  41. return
  42. }
  43. ctx := provisioner.NewContextWithMethod(r.Context(), provisioner.SSHRenewMethod)
  44. _, err := h.Authority.Authorize(ctx, body.OTT)
  45. if err != nil {
  46. render.Error(w, errs.UnauthorizedErr(err))
  47. return
  48. }
  49. oldCert, _, err := provisioner.ExtractSSHPOPCert(body.OTT)
  50. if err != nil {
  51. render.Error(w, errs.InternalServerErr(err))
  52. return
  53. }
  54. newCert, err := h.Authority.RenewSSH(ctx, oldCert)
  55. if err != nil {
  56. render.Error(w, errs.ForbiddenErr(err, "error renewing ssh certificate"))
  57. return
  58. }
  59. // Match identity cert with the SSH cert
  60. notBefore := time.Unix(int64(oldCert.ValidAfter), 0)
  61. notAfter := time.Unix(int64(oldCert.ValidBefore), 0)
  62. identity, err := h.renewIdentityCertificate(r, notBefore, notAfter)
  63. if err != nil {
  64. render.Error(w, errs.ForbiddenErr(err, "error renewing identity certificate"))
  65. return
  66. }
  67. render.JSONStatus(w, &SSHSignResponse{
  68. Certificate: SSHCertificate{newCert},
  69. IdentityCertificate: identity,
  70. }, http.StatusCreated)
  71. }
  72. // renewIdentityCertificate request the client TLS certificate if present. If notBefore and notAfter are passed the
  73. func (h *caHandler) renewIdentityCertificate(r *http.Request, notBefore, notAfter time.Time) ([]Certificate, error) {
  74. if r.TLS == nil || len(r.TLS.PeerCertificates) == 0 {
  75. return nil, nil
  76. }
  77. // Clone the certificate as we can modify it.
  78. cert, err := x509.ParseCertificate(r.TLS.PeerCertificates[0].Raw)
  79. if err != nil {
  80. return nil, errors.Wrap(err, "error parsing client certificate")
  81. }
  82. // Enforce the cert to match another certificate, for example an ssh
  83. // certificate.
  84. if !notBefore.IsZero() {
  85. cert.NotBefore = notBefore
  86. }
  87. if !notAfter.IsZero() {
  88. cert.NotAfter = notAfter
  89. }
  90. certChain, err := h.Authority.Renew(cert)
  91. if err != nil {
  92. return nil, err
  93. }
  94. return certChainToPEM(certChain), nil
  95. }