4
0

sshRenew.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 SSHRenew(w http.ResponseWriter, r *http.Request) {
  34. var body SSHRenewRequest
  35. if err := read.JSON(r.Body, &body); err != nil {
  36. render.Error(w, errs.BadRequestErr(err, "error reading request body"))
  37. return
  38. }
  39. logOtt(w, body.OTT)
  40. if err := body.Validate(); err != nil {
  41. render.Error(w, err)
  42. return
  43. }
  44. ctx := provisioner.NewContextWithMethod(r.Context(), provisioner.SSHRenewMethod)
  45. ctx = provisioner.NewContextWithToken(ctx, body.OTT)
  46. a := mustAuthority(ctx)
  47. _, err := a.Authorize(ctx, body.OTT)
  48. if err != nil {
  49. render.Error(w, errs.UnauthorizedErr(err))
  50. return
  51. }
  52. oldCert, _, err := provisioner.ExtractSSHPOPCert(body.OTT)
  53. if err != nil {
  54. render.Error(w, errs.InternalServerErr(err))
  55. return
  56. }
  57. newCert, err := a.RenewSSH(ctx, oldCert)
  58. if err != nil {
  59. render.Error(w, errs.ForbiddenErr(err, "error renewing ssh certificate"))
  60. return
  61. }
  62. // Match identity cert with the SSH cert
  63. notBefore := time.Unix(int64(oldCert.ValidAfter), 0)
  64. notAfter := time.Unix(int64(oldCert.ValidBefore), 0)
  65. //nolint:contextcheck // the reqest has the context
  66. identity, err := renewIdentityCertificate(r, notBefore, notAfter)
  67. if err != nil {
  68. render.Error(w, errs.ForbiddenErr(err, "error renewing identity certificate"))
  69. return
  70. }
  71. render.JSONStatus(w, &SSHSignResponse{
  72. Certificate: SSHCertificate{newCert},
  73. IdentityCertificate: identity,
  74. }, http.StatusCreated)
  75. }
  76. // renewIdentityCertificate request the client TLS certificate if present. If notBefore and notAfter are passed the
  77. func renewIdentityCertificate(r *http.Request, notBefore, notAfter time.Time) ([]Certificate, error) {
  78. if r.TLS == nil || len(r.TLS.PeerCertificates) == 0 {
  79. return nil, nil
  80. }
  81. // Clone the certificate as we can modify it.
  82. cert, err := x509.ParseCertificate(r.TLS.PeerCertificates[0].Raw)
  83. if err != nil {
  84. return nil, errors.Wrap(err, "error parsing client certificate")
  85. }
  86. // Enforce the cert to match another certificate, for example an ssh
  87. // certificate.
  88. if !notBefore.IsZero() {
  89. cert.NotBefore = notBefore
  90. }
  91. if !notAfter.IsZero() {
  92. cert.NotAfter = notAfter
  93. }
  94. certChain, err := mustAuthority(r.Context()).Renew(cert)
  95. if err != nil {
  96. return nil, err
  97. }
  98. return certChainToPEM(certChain), nil
  99. }