2019-10-28 18:50:43 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2021-08-25 23:15:12 +00:00
|
|
|
"crypto/x509"
|
2019-10-28 18:50:43 +00:00
|
|
|
"net/http"
|
2021-08-23 22:15:36 +00:00
|
|
|
"time"
|
2019-10-28 18:50:43 +00:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2022-03-18 13:25:34 +00:00
|
|
|
|
|
|
|
"github.com/smallstep/certificates/api/read"
|
2022-03-30 08:22:22 +00:00
|
|
|
"github.com/smallstep/certificates/api/render"
|
2019-10-28 18:50:43 +00:00
|
|
|
"github.com/smallstep/certificates/authority/provisioner"
|
2019-12-16 07:54:25 +00:00
|
|
|
"github.com/smallstep/certificates/errs"
|
2019-10-28 18:50:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// SSHRenewRequest is the request body of an SSH certificate request.
|
|
|
|
type SSHRenewRequest struct {
|
|
|
|
OTT string `json:"ott"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate validates the SSHSignRequest.
|
|
|
|
func (s *SSHRenewRequest) Validate() error {
|
|
|
|
switch {
|
2021-10-08 18:59:57 +00:00
|
|
|
case s.OTT == "":
|
2021-11-19 02:17:36 +00:00
|
|
|
return errs.BadRequest("missing or empty ott")
|
2019-10-28 18:50:43 +00:00
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SSHRenewResponse is the response object that returns the SSH certificate.
|
|
|
|
type SSHRenewResponse struct {
|
2019-12-18 22:43:38 +00:00
|
|
|
Certificate SSHCertificate `json:"crt"`
|
|
|
|
IdentityCertificate []Certificate `json:"identityCrt,omitempty"`
|
2019-10-28 18:50:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SSHRenew is an HTTP handler that reads an RenewSSHRequest with a one-time-token
|
|
|
|
// (ott) from the body and creates a new SSH certificate with the information in
|
|
|
|
// the request.
|
2022-04-26 19:58:40 +00:00
|
|
|
func SSHRenew(w http.ResponseWriter, r *http.Request) {
|
2019-10-28 18:50:43 +00:00
|
|
|
var body SSHRenewRequest
|
2022-03-18 13:25:34 +00:00
|
|
|
if err := read.JSON(r.Body, &body); err != nil {
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, errs.BadRequestErr(err, "error reading request body"))
|
2019-10-28 18:50:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
logOtt(w, body.OTT)
|
|
|
|
if err := body.Validate(); err != nil {
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, err)
|
2019-10-28 18:50:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-11 02:01:45 +00:00
|
|
|
ctx := provisioner.NewContextWithMethod(r.Context(), provisioner.SSHRenewMethod)
|
2022-05-20 21:41:44 +00:00
|
|
|
ctx = provisioner.NewContextWithToken(ctx, body.OTT)
|
2022-05-23 21:31:15 +00:00
|
|
|
|
2022-04-26 19:58:40 +00:00
|
|
|
a := mustAuthority(ctx)
|
|
|
|
_, err := a.Authorize(ctx, body.OTT)
|
2019-10-28 18:50:43 +00:00
|
|
|
if err != nil {
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, errs.UnauthorizedErr(err))
|
2019-10-28 18:50:43 +00:00
|
|
|
return
|
|
|
|
}
|
2019-12-20 21:30:05 +00:00
|
|
|
oldCert, _, err := provisioner.ExtractSSHPOPCert(body.OTT)
|
2019-10-28 18:50:43 +00:00
|
|
|
if err != nil {
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, errs.InternalServerErr(err))
|
|
|
|
return
|
2019-10-28 18:50:43 +00:00
|
|
|
}
|
|
|
|
|
2022-04-26 19:58:40 +00:00
|
|
|
newCert, err := a.RenewSSH(ctx, oldCert)
|
2019-10-28 18:50:43 +00:00
|
|
|
if err != nil {
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, errs.ForbiddenErr(err, "error renewing ssh certificate"))
|
2019-10-28 18:50:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-23 22:15:36 +00:00
|
|
|
// Match identity cert with the SSH cert
|
|
|
|
notBefore := time.Unix(int64(oldCert.ValidAfter), 0)
|
|
|
|
notAfter := time.Unix(int64(oldCert.ValidBefore), 0)
|
|
|
|
|
2022-04-26 19:58:40 +00:00
|
|
|
identity, err := renewIdentityCertificate(r, notBefore, notAfter)
|
2019-12-18 22:43:38 +00:00
|
|
|
if err != nil {
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, errs.ForbiddenErr(err, "error renewing identity certificate"))
|
2019-12-18 22:43:38 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-30 08:22:22 +00:00
|
|
|
render.JSONStatus(w, &SSHSignResponse{
|
2019-12-18 22:43:38 +00:00
|
|
|
Certificate: SSHCertificate{newCert},
|
|
|
|
IdentityCertificate: identity,
|
2019-11-22 03:06:19 +00:00
|
|
|
}, http.StatusCreated)
|
2019-10-28 18:50:43 +00:00
|
|
|
}
|
2019-12-18 22:43:38 +00:00
|
|
|
|
2021-08-23 22:15:36 +00:00
|
|
|
// renewIdentityCertificate request the client TLS certificate if present. If notBefore and notAfter are passed the
|
2022-04-26 19:58:40 +00:00
|
|
|
func renewIdentityCertificate(r *http.Request, notBefore, notAfter time.Time) ([]Certificate, error) {
|
2019-12-18 22:43:38 +00:00
|
|
|
if r.TLS == nil || len(r.TLS.PeerCertificates) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2021-08-25 23:15:12 +00:00
|
|
|
// Clone the certificate as we can modify it.
|
|
|
|
cert, err := x509.ParseCertificate(r.TLS.PeerCertificates[0].Raw)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error parsing client certificate")
|
|
|
|
}
|
2021-08-23 22:15:36 +00:00
|
|
|
|
|
|
|
// Enforce the cert to match another certificate, for example an ssh
|
|
|
|
// certificate.
|
|
|
|
if !notBefore.IsZero() {
|
|
|
|
cert.NotBefore = notBefore
|
|
|
|
}
|
|
|
|
if !notAfter.IsZero() {
|
|
|
|
cert.NotAfter = notAfter
|
|
|
|
}
|
|
|
|
|
2022-04-26 19:58:40 +00:00
|
|
|
certChain, err := mustAuthority(r.Context()).Renew(cert)
|
2019-12-18 22:43:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return certChainToPEM(certChain), nil
|
|
|
|
}
|