2019-10-28 18:50:43 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"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
|
|
|
"golang.org/x/crypto/ssh"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
case len(s.OTT) == 0:
|
|
|
|
return errors.New("missing or empty ott")
|
|
|
|
case len(s.PublicKey) == 0:
|
|
|
|
return errors.New("missing or empty public key")
|
|
|
|
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
|
|
|
|
if err := ReadJSON(r.Body, &body); err != nil {
|
2019-12-16 07:54:25 +00:00
|
|
|
WriteError(w, errs.BadRequest(errors.Wrap(err, "error reading request body")))
|
2019-10-28 18:50:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
logOtt(w, body.OTT)
|
|
|
|
if err := body.Validate(); err != nil {
|
2019-12-16 07:54:25 +00:00
|
|
|
WriteError(w, errs.BadRequest(err))
|
2019-10-28 18:50:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
publicKey, err := ssh.ParsePublicKey(body.PublicKey)
|
|
|
|
if err != nil {
|
2019-12-16 07:54:25 +00:00
|
|
|
WriteError(w, errs.BadRequest(errors.Wrap(err, "error parsing publicKey")))
|
2019-10-28 18:50:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := provisioner.NewContextWithMethod(context.Background(), provisioner.RekeySSHMethod)
|
|
|
|
signOpts, err := h.Authority.Authorize(ctx, body.OTT)
|
|
|
|
if err != nil {
|
2019-12-16 07:54:25 +00:00
|
|
|
WriteError(w, errs.Unauthorized(err))
|
2019-10-28 18:50:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
oldCert, err := provisioner.ExtractSSHPOPCert(body.OTT)
|
|
|
|
if err != nil {
|
2019-12-16 07:54:25 +00:00
|
|
|
WriteError(w, errs.InternalServerError(err))
|
2019-10-28 18:50:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
newCert, err := h.Authority.RekeySSH(oldCert, publicKey, signOpts...)
|
|
|
|
if err != nil {
|
2019-12-16 07:54:25 +00:00
|
|
|
WriteError(w, errs.Forbidden(err))
|
2019-10-28 18:50:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-18 22:43:38 +00:00
|
|
|
identity, err := h.renewIdentityCertificate(r)
|
|
|
|
if err != nil {
|
|
|
|
WriteError(w, errs.Forbidden(err))
|
|
|
|
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
|
|
|
}
|