2021-03-18 02:33:35 +00:00
|
|
|
package stepcas
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto"
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/ed25519"
|
|
|
|
"crypto/rsa"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/smallstep/certificates/cas/apiv1"
|
|
|
|
"go.step.sm/crypto/jose"
|
|
|
|
"go.step.sm/crypto/pemutil"
|
|
|
|
"go.step.sm/crypto/randutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
const defaultValidity = 5 * time.Minute
|
|
|
|
|
2021-03-19 20:19:49 +00:00
|
|
|
// timeNow returns the current time.
|
|
|
|
// This method is used for unit testing purposes.
|
|
|
|
var timeNow = func() time.Time {
|
|
|
|
return time.Now()
|
|
|
|
}
|
|
|
|
|
2021-03-18 02:33:35 +00:00
|
|
|
type x5cIssuer struct {
|
|
|
|
caURL *url.URL
|
2021-03-24 00:54:42 +00:00
|
|
|
issuer string
|
2021-03-18 02:33:35 +00:00
|
|
|
certFile string
|
|
|
|
keyFile string
|
2021-03-24 00:54:42 +00:00
|
|
|
password string
|
2021-03-18 02:33:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// newX5CIssuer create a new x5c token issuer. The given configuration should be
|
|
|
|
// already validate.
|
|
|
|
func newX5CIssuer(caURL *url.URL, cfg *apiv1.CertificateIssuer) (*x5cIssuer, error) {
|
2021-03-24 00:54:42 +00:00
|
|
|
_, err := newX5CSigner(cfg.Certificate, cfg.Key, cfg.Password)
|
2021-03-18 02:33:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &x5cIssuer{
|
|
|
|
caURL: caURL,
|
2021-03-24 00:54:42 +00:00
|
|
|
issuer: cfg.Provisioner,
|
2021-03-18 02:33:35 +00:00
|
|
|
certFile: cfg.Certificate,
|
|
|
|
keyFile: cfg.Key,
|
2021-03-24 00:54:42 +00:00
|
|
|
password: cfg.Password,
|
2021-03-18 02:33:35 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *x5cIssuer) SignToken(subject string, sans []string) (string, error) {
|
|
|
|
aud := i.caURL.ResolveReference(&url.URL{
|
|
|
|
Path: "/1.0/sign",
|
|
|
|
Fragment: "x5c/" + i.issuer,
|
|
|
|
}).String()
|
|
|
|
|
|
|
|
return i.createToken(aud, subject, sans)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *x5cIssuer) RevokeToken(subject string) (string, error) {
|
|
|
|
aud := i.caURL.ResolveReference(&url.URL{
|
|
|
|
Path: "/1.0/revoke",
|
|
|
|
Fragment: "x5c/" + i.issuer,
|
|
|
|
}).String()
|
|
|
|
|
|
|
|
return i.createToken(aud, subject, nil)
|
|
|
|
}
|
|
|
|
|
2021-03-19 20:19:49 +00:00
|
|
|
func (i *x5cIssuer) Lifetime(d time.Duration) time.Duration {
|
|
|
|
cert, err := pemutil.ReadCertificate(i.certFile, pemutil.WithFirstBlock())
|
|
|
|
if err != nil {
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
now := timeNow()
|
|
|
|
if now.Add(d + time.Minute).After(cert.NotAfter) {
|
|
|
|
return cert.NotAfter.Sub(now) - time.Minute
|
|
|
|
}
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
2021-03-18 02:33:35 +00:00
|
|
|
func (i *x5cIssuer) createToken(aud, sub string, sans []string) (string, error) {
|
2021-03-24 00:54:42 +00:00
|
|
|
signer, err := newX5CSigner(i.certFile, i.keyFile, i.password)
|
2021-03-18 02:33:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := randutil.Hex(64) // 256 bits
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
claims := defaultClaims(i.issuer, sub, aud, id)
|
|
|
|
builder := jose.Signed(signer).Claims(claims)
|
|
|
|
if len(sans) > 0 {
|
|
|
|
builder = builder.Claims(map[string]interface{}{
|
|
|
|
"sans": sans,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
tok, err := builder.CompactSerialize()
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "error signing token")
|
|
|
|
}
|
|
|
|
|
|
|
|
return tok, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func defaultClaims(iss, sub, aud, id string) jose.Claims {
|
2021-03-19 20:19:49 +00:00
|
|
|
now := timeNow()
|
2021-03-18 02:33:35 +00:00
|
|
|
return jose.Claims{
|
|
|
|
ID: id,
|
|
|
|
Issuer: iss,
|
|
|
|
Subject: sub,
|
|
|
|
Audience: jose.Audience{aud},
|
|
|
|
Expiry: jose.NewNumericDate(now.Add(defaultValidity)),
|
|
|
|
NotBefore: jose.NewNumericDate(now),
|
|
|
|
IssuedAt: jose.NewNumericDate(now),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-24 00:54:42 +00:00
|
|
|
func readKey(keyFile, password string) (crypto.Signer, error) {
|
|
|
|
var opts []pemutil.Options
|
|
|
|
if password != "" {
|
|
|
|
opts = append(opts, pemutil.WithPassword([]byte(password)))
|
|
|
|
}
|
|
|
|
key, err := pemutil.Read(keyFile, opts...)
|
2021-03-18 02:33:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
signer, ok := key.(crypto.Signer)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("key is not a crypto.Signer")
|
|
|
|
}
|
2021-03-24 00:54:42 +00:00
|
|
|
return signer, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func newX5CSigner(certFile, keyFile, password string) (jose.Signer, error) {
|
|
|
|
signer, err := readKey(keyFile, password)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-18 02:33:35 +00:00
|
|
|
kid, err := jose.Thumbprint(&jose.JSONWebKey{Key: signer.Public()})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-03 19:48:20 +00:00
|
|
|
certs, err := pemutil.ReadCertificateBundle(certFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error reading x5c certificate chain")
|
|
|
|
}
|
|
|
|
certStrs, err := jose.ValidateX5C(certs, signer)
|
2021-03-18 02:33:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error validating x5c certificate chain and key")
|
|
|
|
}
|
|
|
|
|
|
|
|
so := new(jose.SignerOptions)
|
|
|
|
so.WithType("JWT")
|
|
|
|
so.WithHeader("kid", kid)
|
2021-05-03 19:48:20 +00:00
|
|
|
so.WithHeader("x5c", certStrs)
|
2021-03-18 02:33:35 +00:00
|
|
|
return newJoseSigner(signer, so)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newJoseSigner(key crypto.Signer, so *jose.SignerOptions) (jose.Signer, error) {
|
|
|
|
var alg jose.SignatureAlgorithm
|
2021-03-19 01:01:38 +00:00
|
|
|
switch k := key.Public().(type) {
|
|
|
|
case *ecdsa.PublicKey:
|
2021-03-18 02:33:35 +00:00
|
|
|
switch k.Curve.Params().Name {
|
|
|
|
case "P-256":
|
|
|
|
alg = jose.ES256
|
|
|
|
case "P-384":
|
|
|
|
alg = jose.ES384
|
|
|
|
case "P-521":
|
|
|
|
alg = jose.ES512
|
|
|
|
default:
|
|
|
|
return nil, errors.Errorf("unsupported elliptic curve %s", k.Curve.Params().Name)
|
|
|
|
}
|
2021-03-19 01:01:38 +00:00
|
|
|
case ed25519.PublicKey:
|
2021-03-18 02:33:35 +00:00
|
|
|
alg = jose.EdDSA
|
2021-03-19 01:01:38 +00:00
|
|
|
case *rsa.PublicKey:
|
2021-03-18 02:33:35 +00:00
|
|
|
alg = jose.DefaultRSASigAlgorithm
|
|
|
|
default:
|
|
|
|
return nil, errors.Errorf("unsupported key type %T", k)
|
|
|
|
}
|
|
|
|
|
|
|
|
signer, err := jose.NewSigner(jose.SigningKey{Algorithm: alg, Key: key}, so)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error creating jose.Signer")
|
|
|
|
}
|
|
|
|
return signer, nil
|
|
|
|
}
|