4
0

sshRenew.go 3.2 KB

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