2019-07-24 01:46:43 +00:00
|
|
|
package provisioner
|
|
|
|
|
|
|
|
import (
|
2019-09-11 00:04:13 +00:00
|
|
|
"crypto/rsa"
|
|
|
|
"encoding/binary"
|
2020-07-27 22:41:53 +00:00
|
|
|
"encoding/json"
|
2022-02-01 13:58:13 +00:00
|
|
|
"fmt"
|
2019-09-11 00:04:13 +00:00
|
|
|
"math/big"
|
2021-09-08 11:24:49 +00:00
|
|
|
"strings"
|
2019-07-24 01:46:43 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2022-03-08 12:26:07 +00:00
|
|
|
"github.com/smallstep/certificates/authority/policy"
|
2021-11-19 02:44:58 +00:00
|
|
|
"github.com/smallstep/certificates/errs"
|
2020-08-14 22:33:50 +00:00
|
|
|
"go.step.sm/crypto/keyutil"
|
2019-07-24 01:46:43 +00:00
|
|
|
"golang.org/x/crypto/ssh"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// SSHUserCert is the string used to represent ssh.UserCert.
|
|
|
|
SSHUserCert = "user"
|
|
|
|
|
|
|
|
// SSHHostCert is the string used to represent ssh.HostCert.
|
|
|
|
SSHHostCert = "host"
|
|
|
|
)
|
|
|
|
|
2020-01-24 06:04:34 +00:00
|
|
|
// SSHCertModifier is the interface used to change properties in an SSH
|
2019-07-24 01:46:43 +00:00
|
|
|
// certificate.
|
2020-01-24 06:04:34 +00:00
|
|
|
type SSHCertModifier interface {
|
2019-07-24 01:46:43 +00:00
|
|
|
SignOption
|
2020-07-29 23:06:39 +00:00
|
|
|
Modify(cert *ssh.Certificate, opts SignSSHOptions) error
|
2019-07-24 01:46:43 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 06:04:34 +00:00
|
|
|
// SSHCertValidator is the interface used to validate an SSH certificate.
|
|
|
|
type SSHCertValidator interface {
|
2019-07-24 01:46:43 +00:00
|
|
|
SignOption
|
2020-07-23 01:24:45 +00:00
|
|
|
Valid(cert *ssh.Certificate, opts SignSSHOptions) error
|
2019-07-24 01:46:43 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 06:04:34 +00:00
|
|
|
// SSHCertOptionsValidator is the interface used to validate the custom
|
2019-07-24 01:46:43 +00:00
|
|
|
// options used to modify the SSH certificate.
|
2020-01-24 06:04:34 +00:00
|
|
|
type SSHCertOptionsValidator interface {
|
2019-07-24 01:46:43 +00:00
|
|
|
SignOption
|
2020-07-23 01:24:45 +00:00
|
|
|
Valid(got SignSSHOptions) error
|
2019-07-24 01:46:43 +00:00
|
|
|
}
|
|
|
|
|
2020-07-23 01:24:45 +00:00
|
|
|
// SignSSHOptions contains the options that can be passed to the SignSSH method.
|
|
|
|
type SignSSHOptions struct {
|
2020-07-27 22:41:53 +00:00
|
|
|
CertType string `json:"certType"`
|
|
|
|
KeyID string `json:"keyID"`
|
|
|
|
Principals []string `json:"principals"`
|
|
|
|
ValidAfter TimeDuration `json:"validAfter,omitempty"`
|
|
|
|
ValidBefore TimeDuration `json:"validBefore,omitempty"`
|
2020-08-04 01:10:29 +00:00
|
|
|
TemplateData json.RawMessage `json:"templateData,omitempty"`
|
2020-07-27 22:41:53 +00:00
|
|
|
Backdate time.Duration `json:"-"`
|
2019-07-24 01:46:43 +00:00
|
|
|
}
|
|
|
|
|
2020-08-04 01:36:05 +00:00
|
|
|
// Validate validates the given SignSSHOptions.
|
|
|
|
func (o SignSSHOptions) Validate() error {
|
|
|
|
if o.CertType != "" && o.CertType != SSHUserCert && o.CertType != SSHHostCert {
|
2021-11-24 02:06:18 +00:00
|
|
|
return errs.BadRequest("certType '%s' is not valid", o.CertType)
|
2020-08-04 01:36:05 +00:00
|
|
|
}
|
2021-11-24 01:52:39 +00:00
|
|
|
for _, p := range o.Principals {
|
|
|
|
if p == "" {
|
|
|
|
return errs.BadRequest("principals cannot contain empty values")
|
|
|
|
}
|
|
|
|
}
|
2020-08-04 01:36:05 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-24 01:46:43 +00:00
|
|
|
// Type returns the uint32 representation of the CertType.
|
2020-07-23 01:24:45 +00:00
|
|
|
func (o SignSSHOptions) Type() uint32 {
|
2019-07-24 01:46:43 +00:00
|
|
|
return sshCertTypeUInt32(o.CertType)
|
|
|
|
}
|
|
|
|
|
2020-01-24 06:04:34 +00:00
|
|
|
// Modify implements SSHCertModifier and sets the SSHOption in the ssh.Certificate.
|
2020-07-29 23:06:39 +00:00
|
|
|
func (o SignSSHOptions) Modify(cert *ssh.Certificate, _ SignSSHOptions) error {
|
2019-07-24 01:46:43 +00:00
|
|
|
switch o.CertType {
|
|
|
|
case "": // ignore
|
|
|
|
case SSHUserCert:
|
|
|
|
cert.CertType = ssh.UserCert
|
|
|
|
case SSHHostCert:
|
|
|
|
cert.CertType = ssh.HostCert
|
|
|
|
default:
|
2021-11-24 01:52:39 +00:00
|
|
|
return errs.BadRequest("ssh certificate has an unknown type '%s'", o.CertType)
|
2019-07-24 01:46:43 +00:00
|
|
|
}
|
2019-11-06 23:53:16 +00:00
|
|
|
|
|
|
|
cert.KeyId = o.KeyID
|
2019-07-24 01:46:43 +00:00
|
|
|
cert.ValidPrincipals = o.Principals
|
2019-11-14 23:26:37 +00:00
|
|
|
|
2020-07-29 23:06:39 +00:00
|
|
|
return o.ModifyValidity(cert)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ModifyValidity modifies only the ValidAfter and ValidBefore on the given
|
|
|
|
// ssh.Certificate.
|
|
|
|
func (o SignSSHOptions) ModifyValidity(cert *ssh.Certificate) error {
|
2019-11-14 23:26:37 +00:00
|
|
|
t := now()
|
2019-07-24 01:46:43 +00:00
|
|
|
if !o.ValidAfter.IsZero() {
|
2019-11-14 23:26:37 +00:00
|
|
|
cert.ValidAfter = uint64(o.ValidAfter.RelativeTime(t).Unix())
|
2019-07-24 01:46:43 +00:00
|
|
|
}
|
|
|
|
if !o.ValidBefore.IsZero() {
|
2019-11-14 23:26:37 +00:00
|
|
|
cert.ValidBefore = uint64(o.ValidBefore.RelativeTime(t).Unix())
|
2019-07-24 01:46:43 +00:00
|
|
|
}
|
|
|
|
if cert.ValidAfter > 0 && cert.ValidBefore > 0 && cert.ValidAfter > cert.ValidBefore {
|
2021-11-24 01:52:39 +00:00
|
|
|
return errs.BadRequest("ssh certificate validAfter cannot be greater than validBefore")
|
2019-07-24 01:46:43 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// match compares two SSHOptions and return an error if they don't match. It
|
|
|
|
// ignores zero values.
|
2020-07-23 01:24:45 +00:00
|
|
|
func (o SignSSHOptions) match(got SignSSHOptions) error {
|
2019-07-24 01:46:43 +00:00
|
|
|
if o.CertType != "" && got.CertType != "" && o.CertType != got.CertType {
|
2021-11-24 19:32:35 +00:00
|
|
|
return errs.Forbidden("ssh certificate type does not match - got %v, want %v", got.CertType, o.CertType)
|
2019-07-24 01:46:43 +00:00
|
|
|
}
|
2019-07-30 00:54:13 +00:00
|
|
|
if len(o.Principals) > 0 && len(got.Principals) > 0 && !containsAllMembers(o.Principals, got.Principals) {
|
2021-11-24 19:32:35 +00:00
|
|
|
return errs.Forbidden("ssh certificate principals does not match - got %v, want %v", got.Principals, o.Principals)
|
2019-07-24 01:46:43 +00:00
|
|
|
}
|
|
|
|
if !o.ValidAfter.IsZero() && !got.ValidAfter.IsZero() && !o.ValidAfter.Equal(&got.ValidAfter) {
|
2021-11-24 19:32:35 +00:00
|
|
|
return errs.Forbidden("ssh certificate validAfter does not match - got %v, want %v", got.ValidAfter, o.ValidAfter)
|
2019-07-24 01:46:43 +00:00
|
|
|
}
|
|
|
|
if !o.ValidBefore.IsZero() && !got.ValidBefore.IsZero() && !o.ValidBefore.Equal(&got.ValidBefore) {
|
2021-11-24 19:32:35 +00:00
|
|
|
return errs.Forbidden("ssh certificate validBefore does not match - got %v, want %v", got.ValidBefore, o.ValidBefore)
|
2019-07-24 01:46:43 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-24 06:04:34 +00:00
|
|
|
// sshCertValidAfterModifier is an SSHCertModifier that sets the
|
2019-07-24 01:46:43 +00:00
|
|
|
// ValidAfter in the SSH certificate.
|
2019-12-20 21:30:05 +00:00
|
|
|
type sshCertValidAfterModifier uint64
|
2019-07-24 01:46:43 +00:00
|
|
|
|
2020-07-29 23:06:39 +00:00
|
|
|
func (m sshCertValidAfterModifier) Modify(cert *ssh.Certificate, _ SignSSHOptions) error {
|
2019-07-24 01:46:43 +00:00
|
|
|
cert.ValidAfter = uint64(m)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-24 06:04:34 +00:00
|
|
|
// sshCertValidBeforeModifier is an SSHCertModifier that sets the
|
2019-07-24 01:46:43 +00:00
|
|
|
// ValidBefore in the SSH certificate.
|
2019-12-20 21:30:05 +00:00
|
|
|
type sshCertValidBeforeModifier uint64
|
2019-07-24 01:46:43 +00:00
|
|
|
|
2020-07-29 23:06:39 +00:00
|
|
|
func (m sshCertValidBeforeModifier) Modify(cert *ssh.Certificate, _ SignSSHOptions) error {
|
2019-07-24 01:46:43 +00:00
|
|
|
cert.ValidBefore = uint64(m)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-24 06:04:34 +00:00
|
|
|
// sshDefaultDuration is an SSHCertModifier that sets the certificate
|
2020-01-04 02:22:02 +00:00
|
|
|
// ValidAfter and ValidBefore if they have not been set. It will fail if a
|
2019-07-24 01:46:43 +00:00
|
|
|
// CertType has not been set or is not valid.
|
2020-01-04 02:22:02 +00:00
|
|
|
type sshDefaultDuration struct {
|
2019-07-24 01:46:43 +00:00
|
|
|
*Claimer
|
|
|
|
}
|
|
|
|
|
2020-07-29 23:06:39 +00:00
|
|
|
// Modify implements SSHCertModifier and sets the validity if it has not been
|
|
|
|
// set, but it always applies the backdate.
|
|
|
|
func (m *sshDefaultDuration) Modify(cert *ssh.Certificate, o SignSSHOptions) error {
|
|
|
|
d, err := m.DefaultSSHCertDuration(cert.CertType)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-09-05 01:31:09 +00:00
|
|
|
|
2020-07-29 23:06:39 +00:00
|
|
|
var backdate uint64
|
|
|
|
if cert.ValidAfter == 0 {
|
|
|
|
backdate = uint64(o.Backdate / time.Second)
|
|
|
|
cert.ValidAfter = uint64(now().Truncate(time.Second).Unix())
|
|
|
|
}
|
|
|
|
if cert.ValidBefore == 0 {
|
|
|
|
cert.ValidBefore = cert.ValidAfter + uint64(d/time.Second)
|
|
|
|
}
|
|
|
|
// Apply backdate safely
|
|
|
|
if cert.ValidAfter > backdate {
|
|
|
|
cert.ValidAfter -= backdate
|
|
|
|
}
|
|
|
|
return nil
|
2020-01-04 02:22:02 +00:00
|
|
|
}
|
2019-07-24 01:46:43 +00:00
|
|
|
|
2020-01-04 02:22:02 +00:00
|
|
|
// sshLimitDuration adjusts the duration to min(default, remaining provisioning
|
|
|
|
// credential duration). E.g. if the default is 12hrs but the remaining validity
|
|
|
|
// of the provisioning credential is only 4hrs, this option will set the value
|
|
|
|
// to 4hrs (the min of the two values). It will fail if a CertType has not been
|
|
|
|
// set or is not valid.
|
|
|
|
type sshLimitDuration struct {
|
|
|
|
*Claimer
|
|
|
|
NotAfter time.Time
|
|
|
|
}
|
2019-09-05 01:31:09 +00:00
|
|
|
|
2020-07-29 23:06:39 +00:00
|
|
|
// Modify implements SSHCertModifier and modifies the validity of the
|
|
|
|
// certificate to expire before the configured limit.
|
|
|
|
func (m *sshLimitDuration) Modify(cert *ssh.Certificate, o SignSSHOptions) error {
|
2020-01-04 02:22:02 +00:00
|
|
|
if m.NotAfter.IsZero() {
|
|
|
|
defaultDuration := &sshDefaultDuration{m.Claimer}
|
2020-07-29 23:06:39 +00:00
|
|
|
return defaultDuration.Modify(cert, o)
|
2019-07-24 01:46:43 +00:00
|
|
|
}
|
|
|
|
|
2020-07-29 23:06:39 +00:00
|
|
|
// Make sure the duration is within the limits.
|
|
|
|
d, err := m.DefaultSSHCertDuration(cert.CertType)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-04 02:22:02 +00:00
|
|
|
|
2020-07-29 23:06:39 +00:00
|
|
|
var backdate uint64
|
|
|
|
if cert.ValidAfter == 0 {
|
|
|
|
backdate = uint64(o.Backdate / time.Second)
|
|
|
|
cert.ValidAfter = uint64(now().Truncate(time.Second).Unix())
|
|
|
|
}
|
2019-09-05 01:31:09 +00:00
|
|
|
|
2020-07-29 23:06:39 +00:00
|
|
|
certValidAfter := time.Unix(int64(cert.ValidAfter), 0)
|
|
|
|
if certValidAfter.After(m.NotAfter) {
|
2021-11-24 01:52:39 +00:00
|
|
|
return errs.Forbidden("provisioning credential expiration (%s) is before requested certificate validAfter (%s)",
|
2020-07-29 23:06:39 +00:00
|
|
|
m.NotAfter, certValidAfter)
|
|
|
|
}
|
2019-09-05 01:31:09 +00:00
|
|
|
|
2020-07-29 23:06:39 +00:00
|
|
|
if cert.ValidBefore == 0 {
|
|
|
|
certValidBefore := certValidAfter.Add(d)
|
|
|
|
if m.NotAfter.Before(certValidBefore) {
|
|
|
|
certValidBefore = m.NotAfter
|
2020-01-04 02:22:02 +00:00
|
|
|
}
|
2020-07-29 23:06:39 +00:00
|
|
|
cert.ValidBefore = uint64(certValidBefore.Unix())
|
|
|
|
} else {
|
|
|
|
certValidBefore := time.Unix(int64(cert.ValidBefore), 0)
|
|
|
|
if m.NotAfter.Before(certValidBefore) {
|
2021-11-24 01:52:39 +00:00
|
|
|
return errs.Forbidden("provisioning credential expiration (%s) is before requested certificate validBefore (%s)",
|
2020-07-29 23:06:39 +00:00
|
|
|
m.NotAfter, certValidBefore)
|
2020-01-04 02:22:02 +00:00
|
|
|
}
|
2020-07-29 23:06:39 +00:00
|
|
|
}
|
2019-09-05 01:31:09 +00:00
|
|
|
|
2020-07-29 23:06:39 +00:00
|
|
|
// Apply backdate safely
|
|
|
|
if cert.ValidAfter > backdate {
|
|
|
|
cert.ValidAfter -= backdate
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2019-07-24 01:46:43 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 06:04:34 +00:00
|
|
|
// sshCertOptionsValidator validates the user SSHOptions with the ones
|
2019-07-24 01:46:43 +00:00
|
|
|
// usually present in the token.
|
2020-07-23 01:24:45 +00:00
|
|
|
type sshCertOptionsValidator SignSSHOptions
|
2019-07-24 01:46:43 +00:00
|
|
|
|
2020-01-24 06:04:34 +00:00
|
|
|
// Valid implements SSHCertOptionsValidator and returns nil if both
|
2019-07-29 22:54:07 +00:00
|
|
|
// SSHOptions match.
|
2020-07-23 01:24:45 +00:00
|
|
|
func (v sshCertOptionsValidator) Valid(got SignSSHOptions) error {
|
|
|
|
want := SignSSHOptions(v)
|
2019-08-01 00:03:33 +00:00
|
|
|
return want.match(got)
|
2019-07-24 01:46:43 +00:00
|
|
|
}
|
|
|
|
|
2020-07-30 02:26:46 +00:00
|
|
|
// sshCertOptionsRequireValidator defines which elements in the SignSSHOptions are required.
|
|
|
|
type sshCertOptionsRequireValidator struct {
|
|
|
|
CertType bool
|
|
|
|
KeyID bool
|
|
|
|
Principals bool
|
|
|
|
}
|
|
|
|
|
2020-08-03 22:11:42 +00:00
|
|
|
func (v *sshCertOptionsRequireValidator) Valid(got SignSSHOptions) error {
|
2020-07-30 02:26:46 +00:00
|
|
|
switch {
|
|
|
|
case v.CertType && got.CertType == "":
|
2021-11-24 01:52:39 +00:00
|
|
|
return errs.BadRequest("ssh certificate certType cannot be empty")
|
2020-07-30 02:26:46 +00:00
|
|
|
case v.KeyID && got.KeyID == "":
|
2021-11-24 01:52:39 +00:00
|
|
|
return errs.BadRequest("ssh certificate keyID cannot be empty")
|
2020-07-30 02:26:46 +00:00
|
|
|
case v.Principals && len(got.Principals) == 0:
|
2021-11-24 01:52:39 +00:00
|
|
|
return errs.BadRequest("ssh certificate principals cannot be empty")
|
2020-07-30 02:26:46 +00:00
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 06:04:34 +00:00
|
|
|
type sshCertValidityValidator struct {
|
2019-09-05 01:31:09 +00:00
|
|
|
*Claimer
|
|
|
|
}
|
|
|
|
|
2020-07-23 01:24:45 +00:00
|
|
|
func (v *sshCertValidityValidator) Valid(cert *ssh.Certificate, opts SignSSHOptions) error {
|
2019-09-05 01:31:09 +00:00
|
|
|
switch {
|
|
|
|
case cert.ValidAfter == 0:
|
2021-11-19 02:47:55 +00:00
|
|
|
return errs.BadRequest("ssh certificate validAfter cannot be 0")
|
2019-09-05 01:31:09 +00:00
|
|
|
case cert.ValidBefore < uint64(now().Unix()):
|
2021-11-19 02:47:55 +00:00
|
|
|
return errs.BadRequest("ssh certificate validBefore cannot be in the past")
|
2019-09-05 01:31:09 +00:00
|
|
|
case cert.ValidBefore < cert.ValidAfter:
|
2021-11-19 02:47:55 +00:00
|
|
|
return errs.BadRequest("ssh certificate validBefore cannot be before validAfter")
|
2019-09-05 01:31:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var min, max time.Duration
|
|
|
|
switch cert.CertType {
|
|
|
|
case ssh.UserCert:
|
|
|
|
min = v.MinUserSSHCertDuration()
|
|
|
|
max = v.MaxUserSSHCertDuration()
|
|
|
|
case ssh.HostCert:
|
|
|
|
min = v.MinHostSSHCertDuration()
|
|
|
|
max = v.MaxHostSSHCertDuration()
|
|
|
|
case 0:
|
2021-11-19 02:47:55 +00:00
|
|
|
return errs.BadRequest("ssh certificate type has not been set")
|
2019-09-05 01:31:09 +00:00
|
|
|
default:
|
2021-11-24 02:06:18 +00:00
|
|
|
return errs.BadRequest("ssh certificate has an unknown type '%d'", cert.CertType)
|
2019-09-05 01:31:09 +00:00
|
|
|
}
|
|
|
|
|
2020-01-04 02:22:02 +00:00
|
|
|
// To not take into account the backdate, time.Now() will be used to
|
|
|
|
// calculate the duration if ValidAfter is in the past.
|
2020-01-24 21:42:00 +00:00
|
|
|
dur := time.Duration(cert.ValidBefore-cert.ValidAfter) * time.Second
|
2020-01-04 02:22:02 +00:00
|
|
|
|
2019-09-05 01:31:09 +00:00
|
|
|
switch {
|
|
|
|
case dur < min:
|
2021-11-24 19:32:35 +00:00
|
|
|
return errs.Forbidden("requested duration of %s is less than minimum accepted duration for selected provisioner of %s", dur, min)
|
2020-01-24 21:42:00 +00:00
|
|
|
case dur > max+opts.Backdate:
|
2021-11-24 19:32:35 +00:00
|
|
|
return errs.Forbidden("requested duration of %s is greater than maximum accepted duration for selected provisioner of %s", dur, max+opts.Backdate)
|
2019-09-05 01:31:09 +00:00
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 06:04:34 +00:00
|
|
|
// sshCertDefaultValidator implements a simple validator for all the
|
2019-07-24 01:46:43 +00:00
|
|
|
// fields in the SSH certificate.
|
2020-01-24 06:04:34 +00:00
|
|
|
type sshCertDefaultValidator struct{}
|
2019-07-24 01:46:43 +00:00
|
|
|
|
2020-07-27 22:42:52 +00:00
|
|
|
// Valid returns an error if the given certificate does not contain the
|
|
|
|
// necessary fields. We skip ValidPrincipals and Extensions as with custom
|
|
|
|
// templates you can set them empty.
|
2020-07-23 01:24:45 +00:00
|
|
|
func (v *sshCertDefaultValidator) Valid(cert *ssh.Certificate, o SignSSHOptions) error {
|
2019-07-24 01:46:43 +00:00
|
|
|
switch {
|
2019-09-09 04:05:36 +00:00
|
|
|
case len(cert.Nonce) == 0:
|
2021-11-24 01:52:39 +00:00
|
|
|
return errs.Forbidden("ssh certificate nonce cannot be empty")
|
2019-09-09 04:05:36 +00:00
|
|
|
case cert.Key == nil:
|
2021-11-24 01:52:39 +00:00
|
|
|
return errs.Forbidden("ssh certificate key cannot be nil")
|
2019-09-09 04:05:36 +00:00
|
|
|
case cert.Serial == 0:
|
2021-11-24 01:52:39 +00:00
|
|
|
return errs.Forbidden("ssh certificate serial cannot be 0")
|
2019-09-09 04:05:36 +00:00
|
|
|
case cert.CertType != ssh.UserCert && cert.CertType != ssh.HostCert:
|
2021-11-24 01:52:39 +00:00
|
|
|
return errs.Forbidden("ssh certificate has an unknown type '%d'", cert.CertType)
|
2019-09-09 04:05:36 +00:00
|
|
|
case cert.KeyId == "":
|
2021-11-24 01:52:39 +00:00
|
|
|
return errs.Forbidden("ssh certificate key id cannot be empty")
|
2019-09-09 04:05:36 +00:00
|
|
|
case cert.ValidAfter == 0:
|
2021-11-24 01:52:39 +00:00
|
|
|
return errs.Forbidden("ssh certificate validAfter cannot be 0")
|
2019-09-12 05:57:55 +00:00
|
|
|
case cert.ValidBefore < uint64(now().Unix()):
|
2021-11-24 01:52:39 +00:00
|
|
|
return errs.Forbidden("ssh certificate validBefore cannot be in the past")
|
2019-09-12 05:57:55 +00:00
|
|
|
case cert.ValidBefore < cert.ValidAfter:
|
2021-11-24 01:52:39 +00:00
|
|
|
return errs.Forbidden("ssh certificate validBefore cannot be before validAfter")
|
2019-09-09 04:05:36 +00:00
|
|
|
case cert.SignatureKey == nil:
|
2021-11-24 01:52:39 +00:00
|
|
|
return errs.Forbidden("ssh certificate signature key cannot be nil")
|
2019-09-09 04:05:36 +00:00
|
|
|
case cert.Signature == nil:
|
2021-11-24 01:52:39 +00:00
|
|
|
return errs.Forbidden("ssh certificate signature cannot be nil")
|
2019-07-24 01:46:43 +00:00
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-11 00:04:13 +00:00
|
|
|
// sshDefaultPublicKeyValidator implements a validator for the certificate key.
|
|
|
|
type sshDefaultPublicKeyValidator struct{}
|
|
|
|
|
|
|
|
// Valid checks that certificate request common name matches the one configured.
|
2021-12-03 23:24:42 +00:00
|
|
|
//
|
|
|
|
// TODO: this is the only validator that checks the key type. We should execute
|
|
|
|
// this before the signing. We should add a new validations interface or extend
|
|
|
|
// SSHCertOptionsValidator with the key.
|
2020-07-23 01:24:45 +00:00
|
|
|
func (v sshDefaultPublicKeyValidator) Valid(cert *ssh.Certificate, o SignSSHOptions) error {
|
2019-09-11 00:04:13 +00:00
|
|
|
if cert.Key == nil {
|
2021-11-24 01:52:39 +00:00
|
|
|
return errs.BadRequest("ssh certificate key cannot be nil")
|
2019-09-11 00:04:13 +00:00
|
|
|
}
|
|
|
|
switch cert.Key.Type() {
|
|
|
|
case ssh.KeyAlgoRSA:
|
|
|
|
_, in, ok := sshParseString(cert.Key.Marshal())
|
|
|
|
if !ok {
|
2021-11-24 01:52:39 +00:00
|
|
|
return errs.BadRequest("ssh certificate key is invalid")
|
2019-09-11 00:04:13 +00:00
|
|
|
}
|
|
|
|
key, err := sshParseRSAPublicKey(in)
|
|
|
|
if err != nil {
|
2021-11-24 01:52:39 +00:00
|
|
|
return errs.BadRequestErr(err, "error parsing public key")
|
2019-09-11 00:04:13 +00:00
|
|
|
}
|
2020-08-14 22:33:50 +00:00
|
|
|
if key.Size() < keyutil.MinRSAKeyBytes {
|
2021-11-24 01:52:39 +00:00
|
|
|
return errs.Forbidden("ssh certificate key must be at least %d bits (%d bytes)",
|
2020-08-14 22:33:50 +00:00
|
|
|
8*keyutil.MinRSAKeyBytes, keyutil.MinRSAKeyBytes)
|
2019-09-11 00:04:13 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
case ssh.KeyAlgoDSA:
|
2021-11-24 01:52:39 +00:00
|
|
|
return errs.BadRequest("ssh certificate key algorithm (DSA) is not supported")
|
2019-09-11 00:04:13 +00:00
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 11:25:24 +00:00
|
|
|
// sshNamePolicyValidator validates that the certificate (to be signed)
|
|
|
|
// contains only allowed principals.
|
|
|
|
type sshNamePolicyValidator struct {
|
2022-03-08 12:26:07 +00:00
|
|
|
hostPolicyEngine policy.HostPolicy
|
|
|
|
userPolicyEngine policy.UserPolicy
|
2022-01-03 11:25:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// newSSHNamePolicyValidator return a new SSH allow/deny validator.
|
2022-03-08 12:26:07 +00:00
|
|
|
func newSSHNamePolicyValidator(host policy.HostPolicy, user policy.UserPolicy) *sshNamePolicyValidator {
|
2022-01-03 11:25:24 +00:00
|
|
|
return &sshNamePolicyValidator{
|
2022-02-01 13:58:13 +00:00
|
|
|
hostPolicyEngine: host,
|
|
|
|
userPolicyEngine: user,
|
2022-01-03 11:25:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-08 12:26:07 +00:00
|
|
|
// Valid validates that the certificate (to be signed) contains only allowed principals.
|
2022-01-03 11:25:24 +00:00
|
|
|
func (v *sshNamePolicyValidator) Valid(cert *ssh.Certificate, _ SignSSHOptions) error {
|
2022-02-01 13:58:13 +00:00
|
|
|
if v.hostPolicyEngine == nil && v.userPolicyEngine == nil {
|
|
|
|
// no policy configured at all; allow anything
|
2022-01-03 11:25:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-02-01 13:58:13 +00:00
|
|
|
|
|
|
|
// Check the policy type to execute based on type of the certificate.
|
|
|
|
// We don't allow user certs if only a host policy engine is configured and
|
|
|
|
// the same for host certs: if only a user policy engine is configured, host
|
|
|
|
// certs are denied. When both policy engines are configured, the type of
|
|
|
|
// cert determines which policy engine is used.
|
2022-03-08 12:26:07 +00:00
|
|
|
switch cert.CertType {
|
|
|
|
case ssh.HostCert:
|
2022-02-01 13:58:13 +00:00
|
|
|
// when no host policy engine is configured, but a user policy engine is
|
2022-03-08 12:26:07 +00:00
|
|
|
// configured, the host certificate is denied.
|
2022-02-01 13:58:13 +00:00
|
|
|
if v.hostPolicyEngine == nil && v.userPolicyEngine != nil {
|
2022-03-08 12:26:07 +00:00
|
|
|
return errors.New("SSH host certificate not authorized")
|
2022-02-01 13:58:13 +00:00
|
|
|
}
|
2022-04-26 11:12:16 +00:00
|
|
|
return v.hostPolicyEngine.IsSSHCertificateAllowed(cert)
|
2022-03-08 12:26:07 +00:00
|
|
|
case ssh.UserCert:
|
2022-02-01 13:58:13 +00:00
|
|
|
// when no user policy engine is configured, but a host policy engine is
|
2022-03-08 12:26:07 +00:00
|
|
|
// configured, the user certificate is denied.
|
2022-02-01 13:58:13 +00:00
|
|
|
if v.userPolicyEngine == nil && v.hostPolicyEngine != nil {
|
2022-03-08 12:26:07 +00:00
|
|
|
return errors.New("SSH user certificate not authorized")
|
2022-02-01 13:58:13 +00:00
|
|
|
}
|
2022-04-26 11:12:16 +00:00
|
|
|
return v.userPolicyEngine.IsSSHCertificateAllowed(cert)
|
2022-02-01 13:58:13 +00:00
|
|
|
default:
|
2022-03-08 12:26:07 +00:00
|
|
|
return fmt.Errorf("unexpected SSH certificate type %d", cert.CertType) // satisfy return; shouldn't happen
|
2022-02-01 13:58:13 +00:00
|
|
|
}
|
2022-01-03 11:25:24 +00:00
|
|
|
}
|
|
|
|
|
2019-07-24 01:46:43 +00:00
|
|
|
// sshCertTypeUInt32
|
|
|
|
func sshCertTypeUInt32(ct string) uint32 {
|
|
|
|
switch ct {
|
|
|
|
case SSHUserCert:
|
|
|
|
return ssh.UserCert
|
|
|
|
case SSHHostCert:
|
|
|
|
return ssh.HostCert
|
|
|
|
default:
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-30 00:54:13 +00:00
|
|
|
// containsAllMembers reports whether all members of subgroup are within group.
|
|
|
|
func containsAllMembers(group, subgroup []string) bool {
|
|
|
|
lg, lsg := len(group), len(subgroup)
|
2019-07-30 01:16:52 +00:00
|
|
|
if lsg > lg || (lg > 0 && lsg == 0) {
|
2019-07-24 01:46:43 +00:00
|
|
|
return false
|
|
|
|
}
|
2019-07-30 00:54:13 +00:00
|
|
|
visit := make(map[string]struct{}, lg)
|
|
|
|
for i := 0; i < lg; i++ {
|
2021-09-08 23:00:33 +00:00
|
|
|
visit[strings.ToLower(group[i])] = struct{}{}
|
2019-07-24 01:46:43 +00:00
|
|
|
}
|
2019-07-30 00:54:13 +00:00
|
|
|
for i := 0; i < lsg; i++ {
|
2021-09-08 23:00:33 +00:00
|
|
|
if _, ok := visit[strings.ToLower(subgroup[i])]; !ok {
|
2019-07-24 01:46:43 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2019-09-11 00:04:13 +00:00
|
|
|
|
|
|
|
func sshParseString(in []byte) (out, rest []byte, ok bool) {
|
|
|
|
if len(in) < 4 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
length := binary.BigEndian.Uint32(in)
|
|
|
|
in = in[4:]
|
|
|
|
if uint32(len(in)) < length {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
out = in[:length]
|
|
|
|
rest = in[length:]
|
|
|
|
ok = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func sshParseRSAPublicKey(in []byte) (*rsa.PublicKey, error) {
|
|
|
|
var w struct {
|
|
|
|
E *big.Int
|
|
|
|
N *big.Int
|
|
|
|
Rest []byte `ssh:"rest"`
|
|
|
|
}
|
|
|
|
if err := ssh.Unmarshal(in, &w); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error unmarshalling public key")
|
|
|
|
}
|
|
|
|
if w.E.BitLen() > 24 {
|
|
|
|
return nil, errors.New("invalid public key: exponent too large")
|
|
|
|
}
|
|
|
|
e := w.E.Int64()
|
|
|
|
if e < 3 || e&1 == 0 {
|
|
|
|
return nil, errors.New("invalid public key: incorrect exponent")
|
|
|
|
}
|
|
|
|
|
|
|
|
var key rsa.PublicKey
|
|
|
|
key.E = int(e)
|
|
|
|
key.N = w.N
|
|
|
|
return &key, nil
|
|
|
|
}
|