| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484 |
- package api
- import (
- "context"
- "crypto/x509"
- "encoding/base64"
- "encoding/json"
- "net/http"
- "time"
- "github.com/pkg/errors"
- "github.com/smallstep/certificates/authority"
- "github.com/smallstep/certificates/authority/provisioner"
- "github.com/smallstep/certificates/sshutil"
- "github.com/smallstep/certificates/templates"
- "golang.org/x/crypto/ssh"
- )
- // SSHAuthority is the interface implemented by a SSH CA authority.
- type SSHAuthority interface {
- SignSSH(key ssh.PublicKey, opts provisioner.SSHOptions, signOpts ...provisioner.SignOption) (*ssh.Certificate, error)
- RenewSSH(cert *ssh.Certificate) (*ssh.Certificate, error)
- RekeySSH(cert *ssh.Certificate, key ssh.PublicKey, signOpts ...provisioner.SignOption) (*ssh.Certificate, error)
- SignSSHAddUser(key ssh.PublicKey, cert *ssh.Certificate) (*ssh.Certificate, error)
- GetSSHRoots() (*authority.SSHKeys, error)
- GetSSHFederation() (*authority.SSHKeys, error)
- GetSSHConfig(typ string, data map[string]string) ([]templates.Output, error)
- CheckSSHHost(ctx context.Context, principal string, token string) (bool, error)
- GetSSHHosts(cert *x509.Certificate) ([]sshutil.Host, error)
- GetSSHBastion(user string, hostname string) (*authority.Bastion, error)
- }
- // SSHSignRequest is the request body of an SSH certificate request.
- type SSHSignRequest struct {
- PublicKey []byte `json:"publicKey"` // base64 encoded
- OTT string `json:"ott"`
- CertType string `json:"certType,omitempty"`
- Principals []string `json:"principals,omitempty"`
- ValidAfter TimeDuration `json:"validAfter,omitempty"`
- ValidBefore TimeDuration `json:"validBefore,omitempty"`
- AddUserPublicKey []byte `json:"addUserPublicKey,omitempty"`
- KeyID string `json:"keyID"`
- IdentityCSR CertificateRequest `json:"identityCSR,omitempty"`
- }
- // Validate validates the SSHSignRequest.
- func (s *SSHSignRequest) Validate() error {
- switch {
- case s.CertType != "" && s.CertType != provisioner.SSHUserCert && s.CertType != provisioner.SSHHostCert:
- return errors.Errorf("unknown certType %s", s.CertType)
- case len(s.PublicKey) == 0:
- return errors.New("missing or empty publicKey")
- case len(s.OTT) == 0:
- return errors.New("missing or empty ott")
- default:
- // Validate identity signature if provided
- if s.IdentityCSR.CertificateRequest != nil {
- if err := s.IdentityCSR.CertificateRequest.CheckSignature(); err != nil {
- return errors.Wrap(err, "invalid identityCSR")
- }
- }
- return nil
- }
- }
- // SSHSignResponse is the response object that returns the SSH certificate.
- type SSHSignResponse struct {
- Certificate SSHCertificate `json:"crt"`
- AddUserCertificate *SSHCertificate `json:"addUserCrt,omitempty"`
- IdentityCertificate []Certificate `json:"identityCrt,omitempty"`
- }
- // SSHRootsResponse represents the response object that returns the SSH user and
- // host keys.
- type SSHRootsResponse struct {
- UserKeys []SSHPublicKey `json:"userKey,omitempty"`
- HostKeys []SSHPublicKey `json:"hostKey,omitempty"`
- }
- // SSHCertificate represents the response SSH certificate.
- type SSHCertificate struct {
- *ssh.Certificate `json:"omitempty"`
- }
- // SSHGetHostsResponse is the response object that returns the list of valid
- // hosts for SSH.
- type SSHGetHostsResponse struct {
- Hosts []sshutil.Host `json:"hosts"`
- }
- // MarshalJSON implements the json.Marshaler interface. Returns a quoted,
- // base64 encoded, openssh wire format version of the certificate.
- func (c SSHCertificate) MarshalJSON() ([]byte, error) {
- if c.Certificate == nil {
- return []byte("null"), nil
- }
- s := base64.StdEncoding.EncodeToString(c.Certificate.Marshal())
- return []byte(`"` + s + `"`), nil
- }
- // UnmarshalJSON implements the json.Unmarshaler interface. The certificate is
- // expected to be a quoted, base64 encoded, openssh wire formatted block of bytes.
- func (c *SSHCertificate) UnmarshalJSON(data []byte) error {
- var s string
- if err := json.Unmarshal(data, &s); err != nil {
- return errors.Wrap(err, "error decoding certificate")
- }
- if s == "" {
- c.Certificate = nil
- return nil
- }
- certData, err := base64.StdEncoding.DecodeString(s)
- if err != nil {
- return errors.Wrap(err, "error decoding ssh certificate")
- }
- pub, err := ssh.ParsePublicKey(certData)
- if err != nil {
- return errors.Wrap(err, "error parsing ssh certificate")
- }
- cert, ok := pub.(*ssh.Certificate)
- if !ok {
- return errors.Errorf("error decoding ssh certificate: %T is not an *ssh.Certificate", pub)
- }
- c.Certificate = cert
- return nil
- }
- // SSHPublicKey represents a public key in a response object.
- type SSHPublicKey struct {
- ssh.PublicKey
- }
- // MarshalJSON implements the json.Marshaler interface. Returns a quoted,
- // base64 encoded, openssh wire format version of the public key.
- func (p *SSHPublicKey) MarshalJSON() ([]byte, error) {
- if p == nil || p.PublicKey == nil {
- return []byte("null"), nil
- }
- s := base64.StdEncoding.EncodeToString(p.PublicKey.Marshal())
- return []byte(`"` + s + `"`), nil
- }
- // UnmarshalJSON implements the json.Unmarshaler interface. The public key is
- // expected to be a quoted, base64 encoded, openssh wire formatted block of
- // bytes.
- func (p *SSHPublicKey) UnmarshalJSON(data []byte) error {
- var s string
- if err := json.Unmarshal(data, &s); err != nil {
- return errors.Wrap(err, "error decoding ssh public key")
- }
- if s == "" {
- p.PublicKey = nil
- return nil
- }
- data, err := base64.StdEncoding.DecodeString(s)
- if err != nil {
- return errors.Wrap(err, "error decoding ssh public key")
- }
- pub, err := ssh.ParsePublicKey(data)
- if err != nil {
- return errors.Wrap(err, "error parsing ssh public key")
- }
- p.PublicKey = pub
- return nil
- }
- // Template represents the output of a template.
- type Template = templates.Output
- // SSHConfigRequest is the request body used to get the SSH configuration
- // templates.
- type SSHConfigRequest struct {
- Type string `json:"type"`
- Data map[string]string `json:"data"`
- }
- // Validate checks the values of the SSHConfigurationRequest.
- func (r *SSHConfigRequest) Validate() error {
- switch r.Type {
- case "":
- r.Type = provisioner.SSHUserCert
- return nil
- case provisioner.SSHUserCert, provisioner.SSHHostCert:
- return nil
- default:
- return errors.Errorf("unsupported type %s", r.Type)
- }
- }
- // SSHConfigResponse is the response that returns the rendered templates.
- type SSHConfigResponse struct {
- UserTemplates []Template `json:"userTemplates,omitempty"`
- HostTemplates []Template `json:"hostTemplates,omitempty"`
- }
- // SSHCheckPrincipalRequest is the request body used to check if a principal
- // certificate has been created. Right now it only supported for hosts
- // certificates.
- type SSHCheckPrincipalRequest struct {
- Type string `json:"type"`
- Principal string `json:"principal"`
- Token string `json:"token,omitempty"`
- }
- // Validate checks the check principal request.
- func (r *SSHCheckPrincipalRequest) Validate() error {
- switch {
- case r.Type != provisioner.SSHHostCert:
- return errors.Errorf("unsupported type %s", r.Type)
- case r.Principal == "":
- return errors.New("missing or empty principal")
- default:
- return nil
- }
- }
- // SSHCheckPrincipalResponse is the response body used to check if a principal
- // exists.
- type SSHCheckPrincipalResponse struct {
- Exists bool `json:"exists"`
- }
- // SSHBastionRequest is the request body used to get the bastion for a given
- // host.
- type SSHBastionRequest struct {
- User string `json:"user"`
- Hostname string `json:"hostname"`
- }
- // Validate checks the values of the SSHBastionRequest.
- func (r *SSHBastionRequest) Validate() error {
- if r.Hostname == "" {
- return errors.New("missing or empty hostname")
- }
- return nil
- }
- // SSHBastionResponse is the response body used to return the bastion for a
- // given host.
- type SSHBastionResponse struct {
- Hostname string `json:"hostname"`
- Bastion *authority.Bastion `json:"bastion,omitempty"`
- }
- // SSHSign is an HTTP handler that reads an SignSSHRequest with a one-time-token
- // (ott) from the body and creates a new SSH certificate with the information in
- // the request.
- func (h *caHandler) SSHSign(w http.ResponseWriter, r *http.Request) {
- var body SSHSignRequest
- if err := ReadJSON(r.Body, &body); err != nil {
- WriteError(w, BadRequest(errors.Wrap(err, "error reading request body")))
- return
- }
- logOtt(w, body.OTT)
- if err := body.Validate(); err != nil {
- WriteError(w, BadRequest(err))
- return
- }
- publicKey, err := ssh.ParsePublicKey(body.PublicKey)
- if err != nil {
- WriteError(w, BadRequest(errors.Wrap(err, "error parsing publicKey")))
- return
- }
- var addUserPublicKey ssh.PublicKey
- if body.AddUserPublicKey != nil {
- addUserPublicKey, err = ssh.ParsePublicKey(body.AddUserPublicKey)
- if err != nil {
- WriteError(w, BadRequest(errors.Wrap(err, "error parsing addUserPublicKey")))
- return
- }
- }
- opts := provisioner.SSHOptions{
- CertType: body.CertType,
- KeyID: body.KeyID,
- Principals: body.Principals,
- ValidBefore: body.ValidBefore,
- ValidAfter: body.ValidAfter,
- }
- ctx := provisioner.NewContextWithMethod(context.Background(), provisioner.SignSSHMethod)
- signOpts, err := h.Authority.Authorize(ctx, body.OTT)
- if err != nil {
- WriteError(w, Unauthorized(err))
- return
- }
- cert, err := h.Authority.SignSSH(publicKey, opts, signOpts...)
- if err != nil {
- WriteError(w, Forbidden(err))
- return
- }
- var addUserCertificate *SSHCertificate
- if addUserPublicKey != nil && cert.CertType == ssh.UserCert && len(cert.ValidPrincipals) == 1 {
- addUserCert, err := h.Authority.SignSSHAddUser(addUserPublicKey, cert)
- if err != nil {
- WriteError(w, Forbidden(err))
- return
- }
- addUserCertificate = &SSHCertificate{addUserCert}
- }
- // Sign identity certificate if available.
- var identityCertificate []Certificate
- if cr := body.IdentityCSR.CertificateRequest; cr != nil {
- var opts provisioner.Options
- // Use same duration as ssh certificate for user certificates
- if cert.CertType == ssh.UserCert {
- opts = provisioner.Options{
- NotBefore: provisioner.NewTimeDuration(time.Unix(int64(cert.ValidAfter), 0)),
- NotAfter: provisioner.NewTimeDuration(time.Unix(int64(cert.ValidBefore), 0)),
- }
- }
- ctx := authority.NewContextWithSkipTokenReuse(context.Background())
- ctx = provisioner.NewContextWithMethod(ctx, provisioner.SignMethod)
- signOpts, err := h.Authority.Authorize(ctx, body.OTT)
- if err != nil {
- WriteError(w, Unauthorized(err))
- return
- }
- certChain, err := h.Authority.Sign(cr, opts, signOpts...)
- if err != nil {
- WriteError(w, Forbidden(err))
- return
- }
- identityCertificate = certChainToPEM(certChain)
- }
- JSONStatus(w, &SSHSignResponse{
- Certificate: SSHCertificate{cert},
- AddUserCertificate: addUserCertificate,
- IdentityCertificate: identityCertificate,
- }, http.StatusCreated)
- }
- // SSHRoots is an HTTP handler that returns the SSH public keys for user and host
- // certificates.
- func (h *caHandler) SSHRoots(w http.ResponseWriter, r *http.Request) {
- keys, err := h.Authority.GetSSHRoots()
- if err != nil {
- WriteError(w, InternalServerError(err))
- return
- }
- if len(keys.HostKeys) == 0 && len(keys.UserKeys) == 0 {
- WriteError(w, NotFound(errors.New("no keys found")))
- return
- }
- resp := new(SSHRootsResponse)
- for _, k := range keys.HostKeys {
- resp.HostKeys = append(resp.HostKeys, SSHPublicKey{PublicKey: k})
- }
- for _, k := range keys.UserKeys {
- resp.UserKeys = append(resp.UserKeys, SSHPublicKey{PublicKey: k})
- }
- JSON(w, resp)
- }
- // SSHFederation is an HTTP handler that returns the federated SSH public keys
- // for user and host certificates.
- func (h *caHandler) SSHFederation(w http.ResponseWriter, r *http.Request) {
- keys, err := h.Authority.GetSSHFederation()
- if err != nil {
- WriteError(w, InternalServerError(err))
- return
- }
- if len(keys.HostKeys) == 0 && len(keys.UserKeys) == 0 {
- WriteError(w, NotFound(errors.New("no keys found")))
- return
- }
- resp := new(SSHRootsResponse)
- for _, k := range keys.HostKeys {
- resp.HostKeys = append(resp.HostKeys, SSHPublicKey{PublicKey: k})
- }
- for _, k := range keys.UserKeys {
- resp.UserKeys = append(resp.UserKeys, SSHPublicKey{PublicKey: k})
- }
- JSON(w, resp)
- }
- // SSHConfig is an HTTP handler that returns rendered templates for ssh clients
- // and servers.
- func (h *caHandler) SSHConfig(w http.ResponseWriter, r *http.Request) {
- var body SSHConfigRequest
- if err := ReadJSON(r.Body, &body); err != nil {
- WriteError(w, BadRequest(errors.Wrap(err, "error reading request body")))
- return
- }
- if err := body.Validate(); err != nil {
- WriteError(w, BadRequest(err))
- return
- }
- ts, err := h.Authority.GetSSHConfig(body.Type, body.Data)
- if err != nil {
- WriteError(w, InternalServerError(err))
- return
- }
- var config SSHConfigResponse
- switch body.Type {
- case provisioner.SSHUserCert:
- config.UserTemplates = ts
- case provisioner.SSHHostCert:
- config.HostTemplates = ts
- default:
- WriteError(w, InternalServerError(errors.New("it should hot get here")))
- return
- }
- JSON(w, config)
- }
- // SSHCheckHost is the HTTP handler that returns if a hosts certificate exists or not.
- func (h *caHandler) SSHCheckHost(w http.ResponseWriter, r *http.Request) {
- var body SSHCheckPrincipalRequest
- if err := ReadJSON(r.Body, &body); err != nil {
- WriteError(w, BadRequest(errors.Wrap(err, "error reading request body")))
- return
- }
- if err := body.Validate(); err != nil {
- WriteError(w, BadRequest(err))
- return
- }
- exists, err := h.Authority.CheckSSHHost(r.Context(), body.Principal, body.Token)
- if err != nil {
- WriteError(w, InternalServerError(err))
- return
- }
- JSON(w, &SSHCheckPrincipalResponse{
- Exists: exists,
- })
- }
- // SSHGetHosts is the HTTP handler that returns a list of valid ssh hosts.
- func (h *caHandler) SSHGetHosts(w http.ResponseWriter, r *http.Request) {
- var cert *x509.Certificate
- if r.TLS != nil && len(r.TLS.PeerCertificates) > 0 {
- cert = r.TLS.PeerCertificates[0]
- }
- hosts, err := h.Authority.GetSSHHosts(cert)
- if err != nil {
- WriteError(w, InternalServerError(err))
- return
- }
- JSON(w, &SSHGetHostsResponse{
- Hosts: hosts,
- })
- }
- // SSHBastion provides returns the bastion configured if any.
- func (h *caHandler) SSHBastion(w http.ResponseWriter, r *http.Request) {
- var body SSHBastionRequest
- if err := ReadJSON(r.Body, &body); err != nil {
- WriteError(w, BadRequest(errors.Wrap(err, "error reading request body")))
- return
- }
- if err := body.Validate(); err != nil {
- WriteError(w, BadRequest(err))
- return
- }
- bastion, err := h.Authority.GetSSHBastion(body.User, body.Hostname)
- if err != nil {
- WriteError(w, InternalServerError(err))
- return
- }
- JSON(w, &SSHBastionResponse{
- Hostname: body.Hostname,
- Bastion: bastion,
- })
- }
|