sshRenew.go 3.1 KB

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