sshRenew.go 3.0 KB

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