2019-03-06 22:54:56 +00:00
|
|
|
package provisioner
|
2018-10-19 05:26:39 +00:00
|
|
|
|
|
|
|
import (
|
2019-03-06 22:54:56 +00:00
|
|
|
"crypto/x509"
|
2018-10-19 05:26:39 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/smallstep/cli/crypto/x509util"
|
2019-03-06 22:54:56 +00:00
|
|
|
"github.com/smallstep/cli/jose"
|
2018-10-19 05:26:39 +00:00
|
|
|
)
|
|
|
|
|
2019-03-06 22:54:56 +00:00
|
|
|
// jwtPayload extends jwt.Claims with step attributes.
|
|
|
|
type jwtPayload struct {
|
|
|
|
jose.Claims
|
|
|
|
SANs []string `json:"sans,omitempty"`
|
2018-10-19 05:26:39 +00:00
|
|
|
}
|
|
|
|
|
2019-03-07 02:36:35 +00:00
|
|
|
// JWK is the default provisioner, an entity that can sign tokens necessary for
|
2019-03-06 22:54:56 +00:00
|
|
|
// signature requests.
|
2019-03-07 02:36:35 +00:00
|
|
|
type JWK struct {
|
2019-03-13 22:33:52 +00:00
|
|
|
Type string `json:"type"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Key *jose.JSONWebKey `json:"key"`
|
2019-03-06 22:54:56 +00:00
|
|
|
EncryptedKey string `json:"encryptedKey,omitempty"`
|
|
|
|
Claims *Claims `json:"claims,omitempty"`
|
2019-03-19 22:10:52 +00:00
|
|
|
claimer *Claimer
|
2019-03-05 08:07:13 +00:00
|
|
|
audiences Audiences
|
2018-10-19 05:26:39 +00:00
|
|
|
}
|
|
|
|
|
2019-03-06 22:54:56 +00:00
|
|
|
// GetID returns the provisioner unique identifier. The name and credential id
|
2019-03-07 02:36:35 +00:00
|
|
|
// should uniquely identify any JWK provisioner.
|
|
|
|
func (p *JWK) GetID() string {
|
2019-03-06 22:54:56 +00:00
|
|
|
return p.Name + ":" + p.Key.KeyID
|
2018-10-19 05:26:39 +00:00
|
|
|
}
|
|
|
|
|
2019-03-05 08:07:13 +00:00
|
|
|
// GetTokenID returns the identifier of the token.
|
|
|
|
func (p *JWK) 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
|
|
|
|
}
|
|
|
|
|
2019-03-11 18:12:47 +00:00
|
|
|
// GetName returns the name of the provisioner.
|
2019-03-07 02:36:35 +00:00
|
|
|
func (p *JWK) GetName() string {
|
2019-03-06 22:54:56 +00:00
|
|
|
return p.Name
|
2018-11-01 22:43:24 +00:00
|
|
|
}
|
|
|
|
|
2019-03-06 22:54:56 +00:00
|
|
|
// GetType returns the type of provisioner.
|
2019-03-07 02:36:35 +00:00
|
|
|
func (p *JWK) GetType() Type {
|
2019-03-06 22:54:56 +00:00
|
|
|
return TypeJWK
|
2018-10-19 05:26:39 +00:00
|
|
|
}
|
|
|
|
|
2019-03-06 22:54:56 +00:00
|
|
|
// GetEncryptedKey returns the base provisioner encrypted key if it's defined.
|
2019-03-07 02:36:35 +00:00
|
|
|
func (p *JWK) GetEncryptedKey() (string, string, bool) {
|
2019-03-06 22:54:56 +00:00
|
|
|
return p.Key.KeyID, p.EncryptedKey, len(p.EncryptedKey) > 0
|
2018-10-19 05:26:39 +00:00
|
|
|
}
|
|
|
|
|
2019-03-13 18:26:18 +00:00
|
|
|
// Init initializes and validates the fields of a JWK type.
|
2019-03-07 02:36:35 +00:00
|
|
|
func (p *JWK) Init(config Config) (err error) {
|
2018-10-19 05:26:39 +00:00
|
|
|
switch {
|
|
|
|
case p.Type == "":
|
|
|
|
return errors.New("provisioner type cannot be empty")
|
2019-03-13 22:33:52 +00:00
|
|
|
case p.Name == "":
|
|
|
|
return errors.New("provisioner name cannot be empty")
|
2018-10-19 05:26:39 +00:00
|
|
|
case p.Key == nil:
|
|
|
|
return errors.New("provisioner key cannot be empty")
|
|
|
|
}
|
2019-03-19 22:10:52 +00:00
|
|
|
|
|
|
|
// Update claims with global ones
|
|
|
|
if p.claimer, err = NewClaimer(p.Claims, config.Claims); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-03-07 02:32:56 +00:00
|
|
|
p.audiences = config.Audiences
|
2018-10-19 05:26:39 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-03-05 08:07:13 +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.
|
|
|
|
func (p *JWK) authorizeToken(token string, audiences []string) (*jwtPayload, error) {
|
2019-03-06 22:54:56 +00:00
|
|
|
jwt, err := jose.ParseSigned(token)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error parsing token")
|
|
|
|
}
|
|
|
|
|
|
|
|
var claims jwtPayload
|
|
|
|
if err = jwt.Claims(p.Key, &claims); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error parsing claims")
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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,
|
2019-03-11 18:12:47 +00:00
|
|
|
Time: time.Now().UTC(),
|
2019-03-06 22:54:56 +00:00
|
|
|
}, time.Minute); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "invalid token")
|
|
|
|
}
|
|
|
|
|
2019-03-07 02:32:56 +00:00
|
|
|
// validate audiences with the defaults
|
2019-03-05 08:07:13 +00:00
|
|
|
if !matchesAudience(claims.Audience, audiences) {
|
2019-03-07 02:32:56 +00:00
|
|
|
return nil, errors.New("invalid token: invalid audience claim (aud)")
|
|
|
|
}
|
2019-03-06 22:54:56 +00:00
|
|
|
|
|
|
|
if claims.Subject == "" {
|
|
|
|
return nil, errors.New("token subject cannot be empty")
|
|
|
|
}
|
|
|
|
|
2019-03-05 08:07:13 +00:00
|
|
|
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 *JWK) AuthorizeRevoke(token string) error {
|
|
|
|
_, err := p.authorizeToken(token, p.audiences.Revoke)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// AuthorizeSign validates the given token.
|
|
|
|
func (p *JWK) AuthorizeSign(token string) ([]SignOption, error) {
|
|
|
|
claims, err := p.authorizeToken(token, p.audiences.Sign)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-06 22:54:56 +00:00
|
|
|
// 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}
|
|
|
|
}
|
|
|
|
|
|
|
|
dnsNames, ips := x509util.SplitSANs(claims.SANs)
|
2019-03-07 23:14:18 +00:00
|
|
|
return []SignOption{
|
2019-03-06 22:54:56 +00:00
|
|
|
commonNameValidator(claims.Subject),
|
|
|
|
dnsNamesValidator(dnsNames),
|
|
|
|
ipAddressesValidator(ips),
|
2019-03-19 22:10:52 +00:00
|
|
|
profileDefaultDuration(p.claimer.DefaultTLSCertDuration()),
|
2019-03-06 22:54:56 +00:00
|
|
|
newProvisionerExtensionOption(TypeJWK, p.Name, p.Key.KeyID),
|
2019-03-19 22:10:52 +00:00
|
|
|
newValidityValidator(p.claimer.MinTLSCertDuration(), p.claimer.MaxTLSCertDuration()),
|
2019-03-07 23:14:18 +00:00
|
|
|
}, nil
|
2019-03-06 22:54:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AuthorizeRenewal returns an error if the renewal is disabled.
|
2019-03-07 02:36:35 +00:00
|
|
|
func (p *JWK) AuthorizeRenewal(cert *x509.Certificate) error {
|
2019-03-19 22:10:52 +00:00
|
|
|
if p.claimer.IsDisableRenewal() {
|
2019-03-06 22:54:56 +00:00
|
|
|
return errors.Errorf("renew is disabled for provisioner %s", p.GetID())
|
|
|
|
}
|
|
|
|
return nil
|
2018-10-19 05:26:39 +00:00
|
|
|
}
|