sshRenew.go 3.1 KB

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