4
0

sshRenew.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. identity, err := renewIdentityCertificate(r, notBefore, notAfter)
  66. if err != nil {
  67. render.Error(w, errs.ForbiddenErr(err, "error renewing identity certificate"))
  68. return
  69. }
  70. render.JSONStatus(w, &SSHSignResponse{
  71. Certificate: SSHCertificate{newCert},
  72. IdentityCertificate: identity,
  73. }, http.StatusCreated)
  74. }
  75. // renewIdentityCertificate request the client TLS certificate if present. If notBefore and notAfter are passed the
  76. func renewIdentityCertificate(r *http.Request, notBefore, notAfter time.Time) ([]Certificate, error) {
  77. if r.TLS == nil || len(r.TLS.PeerCertificates) == 0 {
  78. return nil, nil
  79. }
  80. // Clone the certificate as we can modify it.
  81. cert, err := x509.ParseCertificate(r.TLS.PeerCertificates[0].Raw)
  82. if err != nil {
  83. return nil, errors.Wrap(err, "error parsing client certificate")
  84. }
  85. // Enforce the cert to match another certificate, for example an ssh
  86. // certificate.
  87. if !notBefore.IsZero() {
  88. cert.NotBefore = notBefore
  89. }
  90. if !notAfter.IsZero() {
  91. cert.NotAfter = notAfter
  92. }
  93. certChain, err := mustAuthority(r.Context()).Renew(cert)
  94. if err != nil {
  95. return nil, err
  96. }
  97. return certChainToPEM(certChain), nil
  98. }