sshRenew.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package api
  2. import (
  3. "crypto/x509"
  4. "net/http"
  5. "time"
  6. "github.com/pkg/errors"
  7. "github.com/smallstep/certificates/authority/provisioner"
  8. "github.com/smallstep/certificates/errs"
  9. )
  10. // SSHRenewRequest is the request body of an SSH certificate request.
  11. type SSHRenewRequest struct {
  12. OTT string `json:"ott"`
  13. }
  14. // Validate validates the SSHSignRequest.
  15. func (s *SSHRenewRequest) Validate() error {
  16. switch {
  17. case s.OTT == "":
  18. return errs.BadRequest("missing or empty ott")
  19. default:
  20. return nil
  21. }
  22. }
  23. // SSHRenewResponse is the response object that returns the SSH certificate.
  24. type SSHRenewResponse struct {
  25. Certificate SSHCertificate `json:"crt"`
  26. IdentityCertificate []Certificate `json:"identityCrt,omitempty"`
  27. }
  28. // SSHRenew is an HTTP handler that reads an RenewSSHRequest with a one-time-token
  29. // (ott) from the body and creates a new SSH certificate with the information in
  30. // the request.
  31. func (h *caHandler) SSHRenew(w http.ResponseWriter, r *http.Request) {
  32. var body SSHRenewRequest
  33. if err := ReadJSON(r.Body, &body); err != nil {
  34. WriteError(w, errs.BadRequestErr(err, "error reading request body"))
  35. return
  36. }
  37. logOtt(w, body.OTT)
  38. if err := body.Validate(); err != nil {
  39. WriteError(w, err)
  40. return
  41. }
  42. ctx := provisioner.NewContextWithMethod(r.Context(), provisioner.SSHRenewMethod)
  43. _, err := h.Authority.Authorize(ctx, body.OTT)
  44. if err != nil {
  45. WriteError(w, errs.UnauthorizedErr(err))
  46. return
  47. }
  48. oldCert, _, err := provisioner.ExtractSSHPOPCert(body.OTT)
  49. if err != nil {
  50. WriteError(w, errs.InternalServerErr(err))
  51. }
  52. newCert, err := h.Authority.RenewSSH(ctx, oldCert)
  53. if err != nil {
  54. WriteError(w, errs.ForbiddenErr(err, "error signing ssh certificate"))
  55. return
  56. }
  57. // Match identity cert with the SSH cert
  58. notBefore := time.Unix(int64(oldCert.ValidAfter), 0)
  59. notAfter := time.Unix(int64(oldCert.ValidBefore), 0)
  60. identity, err := h.renewIdentityCertificate(r, notBefore, notAfter)
  61. if err != nil {
  62. WriteError(w, errs.ForbiddenErr(err, "error signing identity certificate"))
  63. return
  64. }
  65. JSONStatus(w, &SSHSignResponse{
  66. Certificate: SSHCertificate{newCert},
  67. IdentityCertificate: identity,
  68. }, http.StatusCreated)
  69. }
  70. // renewIdentityCertificate request the client TLS certificate if present. If notBefore and notAfter are passed the
  71. func (h *caHandler) renewIdentityCertificate(r *http.Request, notBefore, notAfter time.Time) ([]Certificate, error) {
  72. if r.TLS == nil || len(r.TLS.PeerCertificates) == 0 {
  73. return nil, nil
  74. }
  75. // Clone the certificate as we can modify it.
  76. cert, err := x509.ParseCertificate(r.TLS.PeerCertificates[0].Raw)
  77. if err != nil {
  78. return nil, errors.Wrap(err, "error parsing client certificate")
  79. }
  80. // Enforce the cert to match another certificate, for example an ssh
  81. // certificate.
  82. if !notBefore.IsZero() {
  83. cert.NotBefore = notBefore
  84. }
  85. if !notAfter.IsZero() {
  86. cert.NotAfter = notAfter
  87. }
  88. certChain, err := h.Authority.Renew(cert)
  89. if err != nil {
  90. return nil, err
  91. }
  92. return certChainToPEM(certChain), nil
  93. }