|
|
@@ -2,18 +2,14 @@ package provisioner
|
|
|
|
|
|
import (
|
|
|
"context"
|
|
|
- "crypto/ecdsa"
|
|
|
- "crypto/rsa"
|
|
|
- "crypto/x509"
|
|
|
"encoding/base64"
|
|
|
- "encoding/pem"
|
|
|
+ "fmt"
|
|
|
+ "strconv"
|
|
|
"time"
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
- "github.com/smallstep/cli/crypto/pemutil"
|
|
|
- "github.com/smallstep/cli/crypto/x509util"
|
|
|
+ "github.com/smallstep/certificates/db"
|
|
|
"github.com/smallstep/cli/jose"
|
|
|
- "golang.org/x/crypto/ed25519"
|
|
|
"golang.org/x/crypto/ssh"
|
|
|
)
|
|
|
|
|
|
@@ -28,13 +24,14 @@ type sshPOPPayload struct {
|
|
|
// SSHPOP is the default provisioner, an entity that can sign tokens necessary for
|
|
|
// signature requests.
|
|
|
type SSHPOP struct {
|
|
|
+ *base
|
|
|
Type string `json:"type"`
|
|
|
Name string `json:"name"`
|
|
|
- PubKeys []byte `json:"pubKeys"`
|
|
|
Claims *Claims `json:"claims,omitempty"`
|
|
|
+ db db.AuthDB
|
|
|
claimer *Claimer
|
|
|
audiences Audiences
|
|
|
- sshPubKeys []ssh.PublicKey
|
|
|
+ sshPubKeys *SSHKeys
|
|
|
}
|
|
|
|
|
|
// GetID returns the provisioner unique identifier. The name and credential id
|
|
|
@@ -83,38 +80,8 @@ func (p *SSHPOP) Init(config Config) error {
|
|
|
return errors.New("provisioner type cannot be empty")
|
|
|
case p.Name == "":
|
|
|
return errors.New("provisioner name cannot be empty")
|
|
|
- case len(p.PubKeys) == 0:
|
|
|
- return errors.New("provisioner root(s) cannot be empty")
|
|
|
- }
|
|
|
-
|
|
|
- var (
|
|
|
- block *pem.Block
|
|
|
- rest = p.PubKeys
|
|
|
- )
|
|
|
- for rest != nil {
|
|
|
- block, rest = pem.Decode(rest)
|
|
|
- if block == nil {
|
|
|
- break
|
|
|
- }
|
|
|
- key, err := pemutil.ParseKey(pem.EncodeToMemory(block))
|
|
|
- if err != nil {
|
|
|
- return errors.Wrapf(err, "error parsing public key in provisioner %s", p.GetID())
|
|
|
- }
|
|
|
- switch q := key.(type) {
|
|
|
- case *rsa.PublicKey, *ecdsa.PublicKey, ed25519.PublicKey:
|
|
|
- sshKey, err := ssh.NewPublicKey(key)
|
|
|
- if err != nil {
|
|
|
- return errors.Wrap(err, "error converting pub key to SSH pub key")
|
|
|
- }
|
|
|
- p.sshPubKeys = append(p.sshPubKeys, sshKey)
|
|
|
- default:
|
|
|
- return errors.Errorf("Unexpected public key type %T in provisioner %s", q, p.GetID())
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // Verify that at least one root was found.
|
|
|
- if len(p.sshPubKeys) == 0 {
|
|
|
- return errors.Errorf("no root public keys found in pub keys attribute for provisioner %s", p.GetName())
|
|
|
+ case config.SSHKeys == nil:
|
|
|
+ return errors.New("provisioner public SSH validation keys cannot be empty")
|
|
|
}
|
|
|
|
|
|
// Update claims with global ones
|
|
|
@@ -124,6 +91,8 @@ func (p *SSHPOP) Init(config Config) error {
|
|
|
}
|
|
|
|
|
|
p.audiences = config.Audiences.WithFragment(p.GetID())
|
|
|
+ p.db = config.DB
|
|
|
+ p.sshPubKeys = config.SSHKeys
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
@@ -131,50 +100,63 @@ func (p *SSHPOP) Init(config Config) error {
|
|
|
// claims for case specific downstream parsing.
|
|
|
// e.g. a Sign request will auth/validate different fields than a Revoke request.
|
|
|
func (p *SSHPOP) authorizeToken(token string, audiences []string) (*sshPOPPayload, error) {
|
|
|
- jwt, err := jose.ParseSigned(token)
|
|
|
+ sshCert, err := ExtractSSHPOPCert(token)
|
|
|
if err != nil {
|
|
|
- return nil, errors.Wrapf(err, "error parsing token")
|
|
|
+ return nil, errors.Wrap(err, "authorizeToken ssh-pop")
|
|
|
}
|
|
|
|
|
|
- encodedSSHCert, ok := jwt.Headers[0].ExtraHeaders["sshpop"]
|
|
|
- if !ok {
|
|
|
- return nil, errors.New("token missing sshpop header")
|
|
|
- }
|
|
|
- encodedSSHCertStr, ok := encodedSSHCert.(string)
|
|
|
- if !ok {
|
|
|
- return nil, errors.New("error unexpected type for sshpop header")
|
|
|
+ // Check for revocation.
|
|
|
+ if isRevoked, err := p.db.IsSSHRevoked(strconv.FormatUint(sshCert.Serial, 10)); err != nil {
|
|
|
+ return nil, errors.Wrap(err, "authorizeToken ssh-pop")
|
|
|
+ } else if isRevoked {
|
|
|
+ return nil, errors.New("authorizeToken ssh-pop: ssh certificate has been revoked")
|
|
|
}
|
|
|
- sshCertBytes, err := base64.RawURLEncoding.DecodeString(encodedSSHCertStr)
|
|
|
+
|
|
|
+ jwt, err := jose.ParseSigned(token)
|
|
|
if err != nil {
|
|
|
- return nil, errors.Wrap(err, "error decoding sshpop header")
|
|
|
+ return nil, errors.Wrapf(err, "error parsing token")
|
|
|
}
|
|
|
- sshPub, err := ssh.ParsePublicKey(sshCertBytes)
|
|
|
- if err != nil {
|
|
|
- return nil, errors.Wrap(err, "error parsing ssh public key")
|
|
|
+ // Check validity period of the certificate.
|
|
|
+ n := time.Now()
|
|
|
+ if sshCert.ValidAfter != 0 && time.Unix(int64(sshCert.ValidAfter), 0).After(n) {
|
|
|
+ return nil, errors.New("sshpop certificate validAfter is in the future")
|
|
|
}
|
|
|
- sshCert, ok := sshPub.(*ssh.Certificate)
|
|
|
+ if sshCert.ValidBefore != 0 && time.Unix(int64(sshCert.ValidBefore), 0).Before(n) {
|
|
|
+ return nil, errors.New("sshpop certificate validBefore is in the past")
|
|
|
+ }
|
|
|
+ sshCryptoPubKey, ok := sshCert.Key.(ssh.CryptoPublicKey)
|
|
|
if !ok {
|
|
|
- return nil, errors.New("error converting ssh public key to ssh certificate")
|
|
|
+ return nil, errors.New("ssh public key could not be cast to ssh CryptoPublicKey")
|
|
|
}
|
|
|
+ pubKey := sshCryptoPubKey.CryptoPublicKey()
|
|
|
|
|
|
- data := bytesForSigning(sshCert)
|
|
|
- var found bool
|
|
|
- for _, k := range p.sshPubKeys {
|
|
|
+ var (
|
|
|
+ found bool
|
|
|
+ data = bytesForSigning(sshCert)
|
|
|
+ keys []ssh.PublicKey
|
|
|
+ )
|
|
|
+ if sshCert.CertType == ssh.UserCert {
|
|
|
+ keys = p.sshPubKeys.UserKeys
|
|
|
+ } else {
|
|
|
+ keys = p.sshPubKeys.HostKeys
|
|
|
+ }
|
|
|
+ for _, k := range keys {
|
|
|
if err = (&ssh.Certificate{Key: k}).Verify(data, sshCert.Signature); err == nil {
|
|
|
found = true
|
|
|
+ break
|
|
|
}
|
|
|
}
|
|
|
if !found {
|
|
|
return nil, errors.New("error: provisioner could could not verify the sshpop header certificate")
|
|
|
}
|
|
|
|
|
|
- // Using the leaf certificates key to validate the claims accomplishes two
|
|
|
+ // Using the ssh certificates key to validate the claims accomplishes two
|
|
|
// things:
|
|
|
// 1. Asserts that the private key used to sign the token corresponds
|
|
|
// to the public certificate in the `sshpop` header of the token.
|
|
|
// 2. Asserts that the claims are valid - have not been tampered with.
|
|
|
var claims sshPOPPayload
|
|
|
- if err = jwt.Claims(sshCert.Key, &claims); err != nil {
|
|
|
+ if err = jwt.Claims(pubKey, &claims); err != nil {
|
|
|
return nil, errors.Wrap(err, "error parsing claims")
|
|
|
}
|
|
|
|
|
|
@@ -189,6 +171,8 @@ func (p *SSHPOP) authorizeToken(token string, audiences []string) (*sshPOPPayloa
|
|
|
|
|
|
// validate audiences with the defaults
|
|
|
if !matchesAudience(claims.Audience, audiences) {
|
|
|
+ fmt.Printf("claims.Audience = %+v\n", claims.Audience)
|
|
|
+ fmt.Printf("audiences = %+v\n", audiences)
|
|
|
return nil, errors.New("invalid token: invalid audience claim (aud)")
|
|
|
}
|
|
|
|
|
|
@@ -200,102 +184,77 @@ func (p *SSHPOP) authorizeToken(token string, audiences []string) (*sshPOPPayloa
|
|
|
return &claims, nil
|
|
|
}
|
|
|
|
|
|
-// AuthorizeRevoke returns an error if the provisioner does not have rights to
|
|
|
-// revoke the certificate with serial number in the `sub` property.
|
|
|
-func (p *SSHPOP) AuthorizeRevoke(token string) error {
|
|
|
- _, err := p.authorizeToken(token, p.audiences.Revoke)
|
|
|
+// AuthorizeSSHRevoke validates the authorization token and extracts/validates
|
|
|
+// the SSH certificate from the ssh-pop header.
|
|
|
+func (p *SSHPOP) AuthorizeSSHRevoke(ctx context.Context, token string) error {
|
|
|
+ claims, err := p.authorizeToken(token, p.audiences.SSHRevoke)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ if claims.Subject != strconv.FormatUint(claims.sshCert.Serial, 10) {
|
|
|
+ return errors.New("token subject must be equivalent to certificate serial number")
|
|
|
+ }
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
-// AuthorizeSign validates the given token.
|
|
|
-func (p *SSHPOP) AuthorizeSign(ctx context.Context, token string) ([]SignOption, error) {
|
|
|
- claims, err := p.authorizeToken(token, p.audiences.Sign)
|
|
|
+// AuthorizeSSHRenew validates the authorization token and extracts/validates
|
|
|
+// the SSH certificate from the ssh-pop header.
|
|
|
+func (p *SSHPOP) AuthorizeSSHRenew(ctx context.Context, token string) (*ssh.Certificate, error) {
|
|
|
+ claims, err := p.authorizeToken(token, p.audiences.SSHRenew)
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
+ return claims.sshCert, nil
|
|
|
|
|
|
- // Check for SSH sign-ing request.
|
|
|
- if MethodFromContext(ctx) == SignSSHMethod {
|
|
|
- if !p.claimer.IsSSHCAEnabled() {
|
|
|
- return nil, errors.Errorf("ssh ca is disabled for provisioner %s", p.GetID())
|
|
|
- }
|
|
|
- return p.authorizeSSHSign(claims)
|
|
|
- }
|
|
|
+}
|
|
|
|
|
|
- // NOTE: This is for backwards compatibility with older versions of cli
|
|
|
- // and certificates. Older versions added the token subject as the only SAN
|
|
|
- // in a CSR by default.
|
|
|
- if len(claims.SANs) == 0 {
|
|
|
- claims.SANs = []string{claims.Subject}
|
|
|
+// AuthorizeSSHRekey validates the authorization token and extracts/validates
|
|
|
+// the SSH certificate from the ssh-pop header.
|
|
|
+func (p *SSHPOP) AuthorizeSSHRekey(ctx context.Context, token string) (*ssh.Certificate, []SignOption, error) {
|
|
|
+ claims, err := p.authorizeToken(token, p.audiences.SSHRekey)
|
|
|
+ if err != nil {
|
|
|
+ return nil, nil, err
|
|
|
}
|
|
|
-
|
|
|
- dnsNames, ips, emails := x509util.SplitSANs(claims.SANs)
|
|
|
-
|
|
|
- return []SignOption{
|
|
|
- // modifiers / withOptions
|
|
|
- newProvisionerExtensionOption(TypeSSHPOP, p.Name, ""),
|
|
|
- profileLimitDuration{p.claimer.DefaultTLSCertDuration(), time.Unix(int64(claims.sshCert.ValidBefore), 0)},
|
|
|
- // validators
|
|
|
- commonNameValidator(claims.Subject),
|
|
|
- defaultPublicKeyValidator{},
|
|
|
- dnsNamesValidator(dnsNames),
|
|
|
- emailAddressesValidator(emails),
|
|
|
- ipAddressesValidator(ips),
|
|
|
- newValidityValidator(p.claimer.MinTLSCertDuration(), p.claimer.MaxTLSCertDuration()),
|
|
|
+ return claims.sshCert, []SignOption{
|
|
|
+ // Validate public key
|
|
|
+ &sshDefaultPublicKeyValidator{},
|
|
|
+ // Validate the validity period.
|
|
|
+ &sshCertificateValidityValidator{p.claimer},
|
|
|
+ // Require and validate all the default fields in the SSH certificate.
|
|
|
+ &sshCertificateDefaultValidator{},
|
|
|
}, nil
|
|
|
-}
|
|
|
|
|
|
-// AuthorizeRenewal returns an error if the renewal is disabled.
|
|
|
-func (p *SSHPOP) AuthorizeRenewal(cert *x509.Certificate) error {
|
|
|
- if p.claimer.IsDisableRenewal() {
|
|
|
- return errors.Errorf("renew is disabled for provisioner %s", p.GetID())
|
|
|
- }
|
|
|
- return nil
|
|
|
}
|
|
|
|
|
|
-// authorizeSSHSign returns the list of SignOption for a SignSSH request.
|
|
|
-func (p *SSHPOP) authorizeSSHSign(claims *sshPOPPayload) ([]SignOption, error) {
|
|
|
- if claims.Step == nil || claims.Step.SSH == nil {
|
|
|
- return nil, errors.New("authorization token must be an SSH provisioning token")
|
|
|
- }
|
|
|
- opts := claims.Step.SSH
|
|
|
- signOptions := []SignOption{
|
|
|
- // validates user's SSHOptions with the ones in the token
|
|
|
- sshCertificateOptionsValidator(*opts),
|
|
|
- // set the key id to the token subject
|
|
|
- sshCertificateKeyIDModifier(claims.Subject),
|
|
|
+// ExtractSSHPOPCert parses a JWT and extracts and loads the SSH Certificate
|
|
|
+// in the sshpop header. If the header is missing, an error is returned.
|
|
|
+func ExtractSSHPOPCert(token string) (*ssh.Certificate, error) {
|
|
|
+ jwt, err := jose.ParseSigned(token)
|
|
|
+ if err != nil {
|
|
|
+ return nil, errors.Wrapf(err, "error parsing token")
|
|
|
}
|
|
|
|
|
|
- // Add modifiers from custom claims
|
|
|
- if opts.CertType != "" {
|
|
|
- signOptions = append(signOptions, sshCertificateCertTypeModifier(opts.CertType))
|
|
|
+ encodedSSHCert, ok := jwt.Headers[0].ExtraHeaders["sshpop"]
|
|
|
+ if !ok {
|
|
|
+ return nil, errors.New("token missing sshpop header")
|
|
|
}
|
|
|
- if len(opts.Principals) > 0 {
|
|
|
- signOptions = append(signOptions, sshCertificatePrincipalsModifier(opts.Principals))
|
|
|
+ encodedSSHCertStr, ok := encodedSSHCert.(string)
|
|
|
+ if !ok {
|
|
|
+ return nil, errors.New("error unexpected type for sshpop header")
|
|
|
}
|
|
|
- t := now()
|
|
|
- if !opts.ValidAfter.IsZero() {
|
|
|
- signOptions = append(signOptions, sshCertificateValidAfterModifier(opts.ValidAfter.RelativeTime(t).Unix()))
|
|
|
+ sshCertBytes, err := base64.StdEncoding.DecodeString(encodedSSHCertStr)
|
|
|
+ if err != nil {
|
|
|
+ return nil, errors.Wrap(err, "error decoding sshpop header")
|
|
|
}
|
|
|
- if !opts.ValidBefore.IsZero() {
|
|
|
- signOptions = append(signOptions, sshCertificateValidBeforeModifier(opts.ValidBefore.RelativeTime(t).Unix()))
|
|
|
+ sshPub, err := ssh.ParsePublicKey(sshCertBytes)
|
|
|
+ if err != nil {
|
|
|
+ return nil, errors.Wrap(err, "error parsing ssh public key")
|
|
|
}
|
|
|
-
|
|
|
- // Default to a user certificate with no principals if not set
|
|
|
- signOptions = append(signOptions, sshCertificateDefaultsModifier{CertType: SSHUserCert})
|
|
|
-
|
|
|
- return append(signOptions,
|
|
|
- // Set the default extensions.
|
|
|
- &sshDefaultExtensionModifier{},
|
|
|
- // Checks the validity bounds, and set the validity if has not been set.
|
|
|
- sshLimitValidityModifier(p.claimer, time.Unix(int64(claims.sshCert.ValidBefore), 0)),
|
|
|
- // Validate public key.
|
|
|
- &sshDefaultPublicKeyValidator{},
|
|
|
- // Validate the validity period.
|
|
|
- &sshCertificateValidityValidator{p.claimer},
|
|
|
- // Require all the fields in the SSH certificate
|
|
|
- &sshCertificateDefaultValidator{},
|
|
|
- ), nil
|
|
|
+ sshCert, ok := sshPub.(*ssh.Certificate)
|
|
|
+ if !ok {
|
|
|
+ return nil, errors.New("error converting ssh public key to ssh certificate")
|
|
|
+ }
|
|
|
+ return sshCert, nil
|
|
|
}
|
|
|
|
|
|
func bytesForSigning(cert *ssh.Certificate) []byte {
|