2019-10-27 00:40:43 +00:00
|
|
|
package provisioner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/base64"
|
2019-12-20 21:30:05 +00:00
|
|
|
"net/http"
|
2019-10-28 18:50:43 +00:00
|
|
|
"strconv"
|
2019-10-27 00:40:43 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"golang.org/x/crypto/ssh"
|
2022-04-21 23:20:38 +00:00
|
|
|
|
|
|
|
"go.step.sm/crypto/jose"
|
|
|
|
|
|
|
|
"github.com/smallstep/certificates/errs"
|
2019-10-27 00:40:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// sshPOPPayload extends jwt.Claims with step attributes.
|
|
|
|
type sshPOPPayload struct {
|
|
|
|
jose.Claims
|
|
|
|
SANs []string `json:"sans,omitempty"`
|
|
|
|
Step *stepPayload `json:"step,omitempty"`
|
|
|
|
sshCert *ssh.Certificate
|
|
|
|
}
|
|
|
|
|
|
|
|
// SSHPOP is the default provisioner, an entity that can sign tokens necessary for
|
|
|
|
// signature requests.
|
|
|
|
type SSHPOP struct {
|
2019-10-28 18:50:43 +00:00
|
|
|
*base
|
2021-05-20 01:23:20 +00:00
|
|
|
ID string `json:"-"`
|
2019-10-27 00:40:43 +00:00
|
|
|
Type string `json:"type"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Claims *Claims `json:"claims,omitempty"`
|
2022-03-10 02:43:45 +00:00
|
|
|
ctl *Controller
|
2019-10-28 18:50:43 +00:00
|
|
|
sshPubKeys *SSHKeys
|
2019-10-27 00:40:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetID returns the provisioner unique identifier. The name and credential id
|
|
|
|
// should uniquely identify any SSH-POP provisioner.
|
|
|
|
func (p *SSHPOP) GetID() string {
|
2021-05-20 01:23:20 +00:00
|
|
|
if p.ID != "" {
|
|
|
|
return p.ID
|
|
|
|
}
|
|
|
|
return p.GetIDForToken()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetIDForToken returns an identifier that will be used to load the provisioner
|
|
|
|
// from a token.
|
|
|
|
func (p *SSHPOP) GetIDForToken() string {
|
2019-10-27 00:40:43 +00:00
|
|
|
return "sshpop/" + p.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTokenID returns the identifier of the token.
|
|
|
|
func (p *SSHPOP) GetTokenID(ott string) (string, error) {
|
|
|
|
// Validate payload
|
|
|
|
token, err := jose.ParseSigned(ott)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "error parsing token")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get claims w/out verification. We need to look up the provisioner
|
|
|
|
// key in order to verify the claims and we need the issuer from the claims
|
|
|
|
// before we can look up the provisioner.
|
|
|
|
var claims jose.Claims
|
|
|
|
if err = token.UnsafeClaimsWithoutVerification(&claims); err != nil {
|
|
|
|
return "", errors.Wrap(err, "error verifying claims")
|
|
|
|
}
|
|
|
|
return claims.ID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetName returns the name of the provisioner.
|
|
|
|
func (p *SSHPOP) GetName() string {
|
|
|
|
return p.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetType returns the type of provisioner.
|
|
|
|
func (p *SSHPOP) GetType() Type {
|
|
|
|
return TypeSSHPOP
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetEncryptedKey returns the base provisioner encrypted key if it's defined.
|
|
|
|
func (p *SSHPOP) GetEncryptedKey() (string, string, bool) {
|
|
|
|
return "", "", false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init initializes and validates the fields of a SSHPOP type.
|
2022-03-10 02:43:45 +00:00
|
|
|
func (p *SSHPOP) Init(config Config) (err error) {
|
2019-10-27 00:40:43 +00:00
|
|
|
switch {
|
|
|
|
case p.Type == "":
|
|
|
|
return errors.New("provisioner type cannot be empty")
|
|
|
|
case p.Name == "":
|
|
|
|
return errors.New("provisioner name cannot be empty")
|
2019-10-28 18:50:43 +00:00
|
|
|
case config.SSHKeys == nil:
|
|
|
|
return errors.New("provisioner public SSH validation keys cannot be empty")
|
2019-10-27 00:40:43 +00:00
|
|
|
}
|
|
|
|
|
2019-10-28 18:50:43 +00:00
|
|
|
p.sshPubKeys = config.SSHKeys
|
2022-03-10 02:43:45 +00:00
|
|
|
|
|
|
|
config.Audiences = config.Audiences.WithFragment(p.GetIDForToken())
|
2022-04-21 23:20:38 +00:00
|
|
|
p.ctl, err = NewController(p, p.Claims, config, nil)
|
2022-03-10 02:43:45 +00:00
|
|
|
return
|
2019-10-27 00:40:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// authorizeToken performs common jwt authorization actions and returns the
|
|
|
|
// claims for case specific downstream parsing.
|
|
|
|
// e.g. a Sign request will auth/validate different fields than a Revoke request.
|
2021-07-21 22:22:57 +00:00
|
|
|
//
|
|
|
|
// Checking for certificate revocation has been moved to the authority package.
|
2022-03-15 00:31:21 +00:00
|
|
|
func (p *SSHPOP) authorizeToken(token string, audiences []string, checkValidity bool) (*sshPOPPayload, error) {
|
2019-12-20 21:30:05 +00:00
|
|
|
sshCert, jwt, err := ExtractSSHPOPCert(token)
|
2019-10-27 00:40:43 +00:00
|
|
|
if err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
return nil, errs.Wrap(http.StatusUnauthorized, err,
|
|
|
|
"sshpop.authorizeToken; error extracting sshpop header from token")
|
2019-10-27 00:40:43 +00:00
|
|
|
}
|
|
|
|
|
2019-10-28 18:50:43 +00:00
|
|
|
// Check validity period of the certificate.
|
2022-03-15 00:31:21 +00:00
|
|
|
//
|
|
|
|
// Controller.AuthorizeSSHRenew will validate this on the renewal flow.
|
|
|
|
if checkValidity {
|
|
|
|
unixNow := time.Now().Unix()
|
|
|
|
if after := int64(sshCert.ValidAfter); after < 0 || unixNow < int64(sshCert.ValidAfter) {
|
|
|
|
return nil, errs.Unauthorized("sshpop.authorizeToken; sshpop certificate validAfter is in the future")
|
|
|
|
}
|
|
|
|
if before := int64(sshCert.ValidBefore); sshCert.ValidBefore != uint64(ssh.CertTimeInfinity) && (unixNow >= before || before < 0) {
|
|
|
|
return nil, errs.Unauthorized("sshpop.authorizeToken; sshpop certificate validBefore is in the past")
|
|
|
|
}
|
2019-10-28 18:50:43 +00:00
|
|
|
}
|
2022-03-15 00:31:21 +00:00
|
|
|
|
2019-10-28 18:50:43 +00:00
|
|
|
sshCryptoPubKey, ok := sshCert.Key.(ssh.CryptoPublicKey)
|
2019-10-27 00:40:43 +00:00
|
|
|
if !ok {
|
2020-01-24 06:04:34 +00:00
|
|
|
return nil, errs.InternalServer("sshpop.authorizeToken; sshpop public key could not be cast to ssh CryptoPublicKey")
|
2019-10-27 00:40:43 +00:00
|
|
|
}
|
2019-10-28 18:50:43 +00:00
|
|
|
pubKey := sshCryptoPubKey.CryptoPublicKey()
|
2019-10-27 00:40:43 +00:00
|
|
|
|
2019-10-28 18:50:43 +00:00
|
|
|
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 {
|
2019-10-27 00:40:43 +00:00
|
|
|
if err = (&ssh.Certificate{Key: k}).Verify(data, sshCert.Signature); err == nil {
|
|
|
|
found = true
|
2019-10-28 18:50:43 +00:00
|
|
|
break
|
2019-10-27 00:40:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
2020-01-24 06:04:34 +00:00
|
|
|
return nil, errs.Unauthorized("sshpop.authorizeToken; could not find valid ca signer to verify sshpop certificate")
|
2019-10-27 00:40:43 +00:00
|
|
|
}
|
|
|
|
|
2019-10-28 18:50:43 +00:00
|
|
|
// Using the ssh certificates key to validate the claims accomplishes two
|
2019-10-27 00:40:43 +00:00
|
|
|
// 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
|
2019-10-28 18:50:43 +00:00
|
|
|
if err = jwt.Claims(pubKey, &claims); err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
return nil, errs.Wrap(http.StatusUnauthorized, err, "sshpop.authorizeToken; error parsing sshpop token claims")
|
2019-10-27 00:40:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// According to "rfc7519 JSON Web Token" acceptable skew should be no
|
|
|
|
// more than a few minutes.
|
|
|
|
if err = claims.ValidateWithLeeway(jose.Expected{
|
|
|
|
Issuer: p.Name,
|
|
|
|
Time: time.Now().UTC(),
|
|
|
|
}, time.Minute); err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
return nil, errs.Wrap(http.StatusUnauthorized, err, "sshpop.authorizeToken; invalid sshpop token")
|
2019-10-27 00:40:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// validate audiences with the defaults
|
|
|
|
if !matchesAudience(claims.Audience, audiences) {
|
2020-01-24 06:04:34 +00:00
|
|
|
return nil, errs.Unauthorized("sshpop.authorizeToken; sshpop token has invalid audience "+
|
|
|
|
"claim (aud): expected %s, but got %s", audiences, claims.Audience)
|
2019-10-27 00:40:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if claims.Subject == "" {
|
2020-01-24 06:04:34 +00:00
|
|
|
return nil, errs.Unauthorized("sshpop.authorizeToken; sshpop token subject cannot be empty")
|
2019-10-27 00:40:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
claims.sshCert = sshCert
|
|
|
|
return &claims, nil
|
|
|
|
}
|
|
|
|
|
2019-10-28 18:50:43 +00:00
|
|
|
// 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 {
|
2022-03-15 00:31:21 +00:00
|
|
|
claims, err := p.authorizeToken(token, p.ctl.Audiences.SSHRevoke, true)
|
2019-10-28 18:50:43 +00:00
|
|
|
if err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
return errs.Wrap(http.StatusInternalServerError, err, "sshpop.AuthorizeSSHRevoke")
|
2019-10-28 18:50:43 +00:00
|
|
|
}
|
|
|
|
if claims.Subject != strconv.FormatUint(claims.sshCert.Serial, 10) {
|
2021-11-18 23:12:44 +00:00
|
|
|
return errs.BadRequest("sshpop token subject must be equivalent to sshpop certificate serial number")
|
2019-10-28 18:50:43 +00:00
|
|
|
}
|
2019-12-20 21:30:05 +00:00
|
|
|
return nil
|
2019-10-27 00:40:43 +00:00
|
|
|
}
|
|
|
|
|
2019-10-28 18:50:43 +00:00
|
|
|
// 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) {
|
2022-03-15 00:31:21 +00:00
|
|
|
claims, err := p.authorizeToken(token, p.ctl.Audiences.SSHRenew, false)
|
2019-10-27 00:40:43 +00:00
|
|
|
if err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
return nil, errs.Wrap(http.StatusInternalServerError, err, "sshpop.AuthorizeSSHRenew")
|
2019-10-27 00:40:43 +00:00
|
|
|
}
|
2019-11-08 05:39:36 +00:00
|
|
|
if claims.sshCert.CertType != ssh.HostCert {
|
2021-11-18 23:12:44 +00:00
|
|
|
return nil, errs.BadRequest("sshpop certificate must be a host ssh certificate")
|
2019-11-08 05:39:36 +00:00
|
|
|
}
|
2022-03-10 02:43:45 +00:00
|
|
|
return claims.sshCert, p.ctl.AuthorizeSSHRenew(ctx, claims.sshCert)
|
2019-10-28 18:50:43 +00:00
|
|
|
}
|
2019-10-27 00:40:43 +00:00
|
|
|
|
2019-10-28 18:50:43 +00:00
|
|
|
// 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) {
|
2022-03-15 00:31:21 +00:00
|
|
|
claims, err := p.authorizeToken(token, p.ctl.Audiences.SSHRekey, true)
|
2019-10-28 18:50:43 +00:00
|
|
|
if err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
return nil, nil, errs.Wrap(http.StatusInternalServerError, err, "sshpop.AuthorizeSSHRekey")
|
2019-10-27 00:40:43 +00:00
|
|
|
}
|
2019-11-08 05:39:36 +00:00
|
|
|
if claims.sshCert.CertType != ssh.HostCert {
|
2021-11-18 23:12:44 +00:00
|
|
|
return nil, nil, errs.BadRequest("sshpop certificate must be a host ssh certificate")
|
2019-11-08 05:39:36 +00:00
|
|
|
}
|
2019-10-28 18:50:43 +00:00
|
|
|
return claims.sshCert, []SignOption{
|
2022-05-20 21:41:44 +00:00
|
|
|
p,
|
2019-10-28 18:50:43 +00:00
|
|
|
// Validate public key
|
|
|
|
&sshDefaultPublicKeyValidator{},
|
|
|
|
// Validate the validity period.
|
2022-03-10 02:43:45 +00:00
|
|
|
&sshCertValidityValidator{p.ctl.Claimer},
|
2019-10-28 18:50:43 +00:00
|
|
|
// Require and validate all the default fields in the SSH certificate.
|
2020-01-24 06:04:34 +00:00
|
|
|
&sshCertDefaultValidator{},
|
2019-10-27 00:40:43 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-10-28 18:50:43 +00:00
|
|
|
// ExtractSSHPOPCert parses a JWT and extracts and loads the SSH Certificate
|
|
|
|
// in the sshpop header. If the header is missing, an error is returned.
|
2019-12-20 21:30:05 +00:00
|
|
|
func ExtractSSHPOPCert(token string) (*ssh.Certificate, *jose.JSONWebToken, error) {
|
2019-10-28 18:50:43 +00:00
|
|
|
jwt, err := jose.ParseSigned(token)
|
|
|
|
if err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
return nil, nil, errors.Wrapf(err, "extractSSHPOPCert; error parsing token")
|
2019-10-27 00:40:43 +00:00
|
|
|
}
|
|
|
|
|
2019-10-28 18:50:43 +00:00
|
|
|
encodedSSHCert, ok := jwt.Headers[0].ExtraHeaders["sshpop"]
|
|
|
|
if !ok {
|
2019-12-20 21:30:05 +00:00
|
|
|
return nil, nil, errors.New("extractSSHPOPCert; token missing sshpop header")
|
2019-10-27 00:40:43 +00:00
|
|
|
}
|
2019-10-28 18:50:43 +00:00
|
|
|
encodedSSHCertStr, ok := encodedSSHCert.(string)
|
|
|
|
if !ok {
|
2019-12-20 21:30:05 +00:00
|
|
|
return nil, nil, errors.Errorf("extractSSHPOPCert; error unexpected type for sshpop header: "+
|
|
|
|
"want 'string', but got '%T'", encodedSSHCert)
|
2019-10-27 00:40:43 +00:00
|
|
|
}
|
2019-10-28 18:50:43 +00:00
|
|
|
sshCertBytes, err := base64.StdEncoding.DecodeString(encodedSSHCertStr)
|
|
|
|
if err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
return nil, nil, errors.Wrap(err, "extractSSHPOPCert; error base64 decoding sshpop header")
|
2019-10-27 00:40:43 +00:00
|
|
|
}
|
2019-10-28 18:50:43 +00:00
|
|
|
sshPub, err := ssh.ParsePublicKey(sshCertBytes)
|
|
|
|
if err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
return nil, nil, errors.Wrap(err, "extractSSHPOPCert; error parsing ssh public key")
|
2019-10-27 00:40:43 +00:00
|
|
|
}
|
2019-10-28 18:50:43 +00:00
|
|
|
sshCert, ok := sshPub.(*ssh.Certificate)
|
|
|
|
if !ok {
|
2019-12-20 21:30:05 +00:00
|
|
|
return nil, nil, errors.New("extractSSHPOPCert; error converting ssh public key to ssh certificate")
|
2019-10-28 18:50:43 +00:00
|
|
|
}
|
2019-12-20 21:30:05 +00:00
|
|
|
return sshCert, jwt, nil
|
2019-10-27 00:40:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func bytesForSigning(cert *ssh.Certificate) []byte {
|
|
|
|
c2 := *cert
|
|
|
|
c2.Signature = nil
|
|
|
|
out := c2.Marshal()
|
|
|
|
// Drop trailing signature length.
|
|
|
|
return out[:len(out)-4]
|
|
|
|
}
|