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"
|
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"
|
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-19 02:44:58 +00:00
|
|
|
return errs.BadRequest("unknown certificate type '%s'", o.CertType)
|
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:
|
2019-12-20 21:30:05 +00:00
|
|
|
return errors.Errorf("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 {
|
|
|
|
return errors.New("ssh certificate valid after cannot be greater than valid before")
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
return errors.Errorf("ssh certificate type does not match - got %v, want %v", got.CertType, o.CertType)
|
|
|
|
}
|
2019-07-30 00:54:13 +00:00
|
|
|
if len(o.Principals) > 0 && len(got.Principals) > 0 && !containsAllMembers(o.Principals, got.Principals) {
|
2019-07-24 01:46:43 +00:00
|
|
|
return errors.Errorf("ssh certificate principals does not match - got %v, want %v", got.Principals, o.Principals)
|
|
|
|
}
|
|
|
|
if !o.ValidAfter.IsZero() && !got.ValidAfter.IsZero() && !o.ValidAfter.Equal(&got.ValidAfter) {
|
|
|
|
return errors.Errorf("ssh certificate valid after does not match - got %v, want %v", got.ValidAfter, o.ValidAfter)
|
|
|
|
}
|
|
|
|
if !o.ValidBefore.IsZero() && !got.ValidBefore.IsZero() && !o.ValidBefore.Equal(&got.ValidBefore) {
|
|
|
|
return errors.Errorf("ssh certificate valid before does not match - got %v, want %v", got.ValidBefore, o.ValidBefore)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-24 06:04:34 +00:00
|
|
|
// sshCertPrincipalsModifier is an SSHCertModifier that sets the
|
2019-11-16 00:57:51 +00:00
|
|
|
// principals to the SSH certificate.
|
|
|
|
type sshCertPrincipalsModifier []string
|
|
|
|
|
|
|
|
// Modify the ValidPrincipals value of the cert.
|
2020-07-29 23:06:39 +00:00
|
|
|
func (o sshCertPrincipalsModifier) Modify(cert *ssh.Certificate, _ SignSSHOptions) error {
|
2019-11-16 00:57:51 +00:00
|
|
|
cert.ValidPrincipals = []string(o)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-24 06:04:34 +00:00
|
|
|
// sshCertKeyIDModifier is an SSHCertModifier that sets the given
|
2019-07-24 01:46:43 +00:00
|
|
|
// Key ID in the SSH certificate.
|
2019-12-20 21:30:05 +00:00
|
|
|
type sshCertKeyIDModifier string
|
2019-07-24 01:46:43 +00:00
|
|
|
|
2020-07-29 23:06:39 +00:00
|
|
|
func (m sshCertKeyIDModifier) Modify(cert *ssh.Certificate, _ SignSSHOptions) error {
|
2019-07-24 01:46:43 +00:00
|
|
|
cert.KeyId = string(m)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-24 06:04:34 +00:00
|
|
|
// sshCertTypeModifier is an SSHCertModifier that sets the
|
2019-11-16 00:57:51 +00:00
|
|
|
// certificate type.
|
|
|
|
type sshCertTypeModifier string
|
2019-07-24 01:46:43 +00:00
|
|
|
|
2019-11-16 00:57:51 +00:00
|
|
|
// Modify sets the CertType for the ssh certificate.
|
2020-07-29 23:06:39 +00:00
|
|
|
func (m sshCertTypeModifier) Modify(cert *ssh.Certificate, _ SignSSHOptions) error {
|
2019-07-24 01:46:43 +00:00
|
|
|
cert.CertType = sshCertTypeUInt32(string(m))
|
|
|
|
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
|
|
|
// sshCertDefaultsModifier implements a SSHCertModifier that
|
2019-08-01 00:03:33 +00:00
|
|
|
// modifies the certificate with the given options if they are not set.
|
2020-07-23 01:24:45 +00:00
|
|
|
type sshCertDefaultsModifier SignSSHOptions
|
2019-08-01 00:03:33 +00:00
|
|
|
|
2020-01-24 06:04:34 +00:00
|
|
|
// Modify implements the SSHCertModifier interface.
|
2020-07-30 01:05:02 +00:00
|
|
|
func (m sshCertDefaultsModifier) Modify(cert *ssh.Certificate, _ SignSSHOptions) error {
|
2019-08-01 00:03:33 +00:00
|
|
|
if cert.CertType == 0 {
|
|
|
|
cert.CertType = sshCertTypeUInt32(m.CertType)
|
|
|
|
}
|
|
|
|
if len(cert.ValidPrincipals) == 0 {
|
|
|
|
cert.ValidPrincipals = m.Principals
|
|
|
|
}
|
|
|
|
if cert.ValidAfter == 0 && !m.ValidAfter.IsZero() {
|
|
|
|
cert.ValidAfter = uint64(m.ValidAfter.Unix())
|
|
|
|
}
|
|
|
|
if cert.ValidBefore == 0 && !m.ValidBefore.IsZero() {
|
|
|
|
cert.ValidBefore = uint64(m.ValidBefore.Unix())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-24 06:04:34 +00:00
|
|
|
// sshDefaultExtensionModifier implements an SSHCertModifier that sets
|
2019-07-24 01:46:43 +00:00
|
|
|
// the default extensions in an SSH certificate.
|
|
|
|
type sshDefaultExtensionModifier struct{}
|
|
|
|
|
2020-07-30 01:05:02 +00:00
|
|
|
func (m *sshDefaultExtensionModifier) Modify(cert *ssh.Certificate, _ SignSSHOptions) error {
|
2019-07-29 23:36:46 +00:00
|
|
|
switch cert.CertType {
|
2019-09-09 04:05:36 +00:00
|
|
|
// Default to no extensions for HostCert.
|
2019-07-29 23:36:46 +00:00
|
|
|
case ssh.HostCert:
|
|
|
|
return nil
|
|
|
|
case ssh.UserCert:
|
|
|
|
if cert.Extensions == nil {
|
|
|
|
cert.Extensions = make(map[string]string)
|
|
|
|
}
|
|
|
|
cert.Extensions["permit-X11-forwarding"] = ""
|
|
|
|
cert.Extensions["permit-agent-forwarding"] = ""
|
|
|
|
cert.Extensions["permit-port-forwarding"] = ""
|
|
|
|
cert.Extensions["permit-pty"] = ""
|
|
|
|
cert.Extensions["permit-user-rc"] = ""
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return errors.New("ssh certificate type has not been set or is invalid")
|
2019-07-24 01:46:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
return errors.Errorf("provisioning credential expiration (%s) is before requested certificate validAfter (%s)",
|
|
|
|
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) {
|
|
|
|
return errors.Errorf("provisioning credential expiration (%s) is before requested certificate validBefore (%s)",
|
|
|
|
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 == "":
|
|
|
|
return errors.New("ssh certificate certType cannot be empty")
|
|
|
|
case v.KeyID && got.KeyID == "":
|
|
|
|
return errors.New("ssh certificate keyID cannot be empty")
|
|
|
|
case v.Principals && len(got.Principals) == 0:
|
|
|
|
return errors.New("ssh certificate principals cannot be empty")
|
|
|
|
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-19 02:47:55 +00:00
|
|
|
return errs.BadRequest("unknown ssh certificate 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-19 02:47:55 +00:00
|
|
|
return errs.BadRequest("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-19 02:47:55 +00:00
|
|
|
return errs.BadRequest("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:
|
2019-07-24 01:46:43 +00:00
|
|
|
return errors.New("ssh certificate nonce cannot be empty")
|
2019-09-09 04:05:36 +00:00
|
|
|
case cert.Key == nil:
|
2019-07-24 01:46:43 +00:00
|
|
|
return errors.New("ssh certificate key cannot be nil")
|
2019-09-09 04:05:36 +00:00
|
|
|
case cert.Serial == 0:
|
2019-07-24 01:46:43 +00:00
|
|
|
return errors.New("ssh certificate serial cannot be 0")
|
2019-09-09 04:05:36 +00:00
|
|
|
case cert.CertType != ssh.UserCert && cert.CertType != ssh.HostCert:
|
|
|
|
return errors.Errorf("ssh certificate has an unknown type: %d", cert.CertType)
|
|
|
|
case cert.KeyId == "":
|
2019-07-24 01:46:43 +00:00
|
|
|
return errors.New("ssh certificate key id cannot be empty")
|
2019-09-09 04:05:36 +00:00
|
|
|
case cert.ValidAfter == 0:
|
2019-09-12 05:57:55 +00:00
|
|
|
return errors.New("ssh certificate validAfter cannot be 0")
|
|
|
|
case cert.ValidBefore < uint64(now().Unix()):
|
|
|
|
return errors.New("ssh certificate validBefore cannot be in the past")
|
|
|
|
case cert.ValidBefore < cert.ValidAfter:
|
|
|
|
return errors.New("ssh certificate validBefore cannot be before validAfter")
|
2019-09-09 04:05:36 +00:00
|
|
|
case cert.SignatureKey == nil:
|
2019-07-24 01:46:43 +00:00
|
|
|
return errors.New("ssh certificate signature key cannot be nil")
|
2019-09-09 04:05:36 +00:00
|
|
|
case cert.Signature == nil:
|
2019-07-24 01:46:43 +00:00
|
|
|
return errors.New("ssh certificate signature cannot be nil")
|
|
|
|
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.
|
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 {
|
|
|
|
return errors.New("ssh certificate key cannot be nil")
|
|
|
|
}
|
|
|
|
switch cert.Key.Type() {
|
|
|
|
case ssh.KeyAlgoRSA:
|
|
|
|
_, in, ok := sshParseString(cert.Key.Marshal())
|
|
|
|
if !ok {
|
|
|
|
return errors.New("ssh certificate key is invalid")
|
|
|
|
}
|
|
|
|
key, err := sshParseRSAPublicKey(in)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-14 22:33:50 +00:00
|
|
|
if key.Size() < keyutil.MinRSAKeyBytes {
|
2019-05-27 00:41:10 +00:00
|
|
|
return errors.Errorf("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:
|
|
|
|
return errors.New("ssh certificate key algorithm (DSA) is not supported")
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|