2019-10-28 18:50:43 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2021-08-23 22:15:36 +00:00
|
|
|
"time"
|
2019-10-28 18:50:43 +00:00
|
|
|
|
2022-03-18 13:25:34 +00:00
|
|
|
"golang.org/x/crypto/ssh"
|
|
|
|
|
|
|
|
"github.com/smallstep/certificates/api/read"
|
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
|
|
|
)
|
|
|
|
|
|
|
|
// SSHRekeyRequest is the request body of an SSH certificate request.
|
|
|
|
type SSHRekeyRequest struct {
|
|
|
|
OTT string `json:"ott"`
|
|
|
|
PublicKey []byte `json:"publicKey"` //base64 encoded
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate validates the SSHSignRekey.
|
|
|
|
func (s *SSHRekeyRequest) 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
|
|
|
case len(s.PublicKey) == 0:
|
2021-11-19 02:17:36 +00:00
|
|
|
return errs.BadRequest("missing or empty public key")
|
2019-10-28 18:50:43 +00:00
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SSHRekeyResponse is the response object that returns the SSH certificate.
|
|
|
|
type SSHRekeyResponse 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
|
|
|
}
|
|
|
|
|
|
|
|
// SSHRekey is an HTTP handler that reads an RekeySSHRequest with a one-time-token
|
|
|
|
// (ott) from the body and creates a new SSH certificate with the information in
|
|
|
|
// the request.
|
|
|
|
func (h *caHandler) SSHRekey(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var body SSHRekeyRequest
|
2022-03-18 13:25:34 +00:00
|
|
|
if err := read.JSON(r.Body, &body); err != nil {
|
2021-11-19 02:44:58 +00:00
|
|
|
WriteError(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 {
|
2021-11-19 02:17:36 +00:00
|
|
|
WriteError(w, err)
|
2019-10-28 18:50:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
publicKey, err := ssh.ParsePublicKey(body.PublicKey)
|
|
|
|
if err != nil {
|
2021-11-19 02:44:58 +00:00
|
|
|
WriteError(w, errs.BadRequestErr(err, "error parsing publicKey"))
|
2019-10-28 18:50:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-11 02:01:45 +00:00
|
|
|
ctx := provisioner.NewContextWithMethod(r.Context(), provisioner.SSHRekeyMethod)
|
2019-10-28 18:50:43 +00:00
|
|
|
signOpts, err := h.Authority.Authorize(ctx, body.OTT)
|
|
|
|
if err != nil {
|
2020-01-24 06:04:34 +00:00
|
|
|
WriteError(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 {
|
2020-01-24 06:04:34 +00:00
|
|
|
WriteError(w, errs.InternalServerErr(err))
|
2019-10-28 18:50:43 +00:00
|
|
|
}
|
|
|
|
|
2020-03-11 02:01:45 +00:00
|
|
|
newCert, err := h.Authority.RekeySSH(ctx, oldCert, publicKey, signOpts...)
|
2019-10-28 18:50:43 +00:00
|
|
|
if err != nil {
|
2021-12-03 23:11:34 +00:00
|
|
|
WriteError(w, errs.ForbiddenErr(err, "error rekeying 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)
|
|
|
|
|
|
|
|
identity, err := h.renewIdentityCertificate(r, notBefore, notAfter)
|
2019-12-18 22:43:38 +00:00
|
|
|
if err != nil {
|
2021-12-03 23:11:34 +00:00
|
|
|
WriteError(w, errs.ForbiddenErr(err, "error renewing identity certificate"))
|
2019-12-18 22:43:38 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
JSONStatus(w, &SSHRekeyResponse{
|
|
|
|
Certificate: SSHCertificate{newCert},
|
|
|
|
IdentityCertificate: identity,
|
2019-11-22 03:06:19 +00:00
|
|
|
}, http.StatusCreated)
|
2019-10-28 18:50:43 +00:00
|
|
|
}
|