2019-12-12 04:23:44 +00:00
|
|
|
package identity
|
2019-11-19 01:07:23 +00:00
|
|
|
|
|
|
|
import (
|
2019-11-20 19:50:46 +00:00
|
|
|
"bytes"
|
|
|
|
"crypto"
|
2019-11-19 01:07:23 +00:00
|
|
|
"crypto/tls"
|
2019-11-20 19:50:46 +00:00
|
|
|
"crypto/x509"
|
|
|
|
"encoding/json"
|
|
|
|
"encoding/pem"
|
|
|
|
"io/ioutil"
|
2019-12-18 20:46:46 +00:00
|
|
|
"net/http"
|
2019-11-20 19:50:46 +00:00
|
|
|
"os"
|
2019-11-19 01:07:23 +00:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2019-11-20 19:50:46 +00:00
|
|
|
"time"
|
2019-11-19 01:07:23 +00:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2019-11-20 19:50:46 +00:00
|
|
|
"github.com/smallstep/certificates/api"
|
2020-10-29 20:10:03 +00:00
|
|
|
"go.step.sm/cli-utils/config"
|
2020-08-14 22:33:50 +00:00
|
|
|
"go.step.sm/crypto/pemutil"
|
2019-11-19 01:07:23 +00:00
|
|
|
)
|
|
|
|
|
2019-12-12 04:23:44 +00:00
|
|
|
// Type represents the different types of identity files.
|
|
|
|
type Type string
|
2019-11-27 02:48:28 +00:00
|
|
|
|
2019-11-20 19:50:46 +00:00
|
|
|
// Disabled represents a disabled identity type
|
2019-12-12 04:23:44 +00:00
|
|
|
const Disabled Type = ""
|
2019-11-20 19:50:46 +00:00
|
|
|
|
2021-04-21 23:05:27 +00:00
|
|
|
// MutualTLS represents the identity using mTLS.
|
2019-12-12 04:23:44 +00:00
|
|
|
const MutualTLS Type = "mTLS"
|
2019-11-19 01:07:23 +00:00
|
|
|
|
2021-04-21 23:05:27 +00:00
|
|
|
// TunnelTLS represents an identity using a (m)TLS tunnel.
|
|
|
|
//
|
|
|
|
// TunnelTLS can be optionally configured with client certificates and a root
|
|
|
|
// file with the CAs to trust. By default it will use the system truststore
|
|
|
|
// instead of the CA truststore.
|
|
|
|
const TunnelTLS Type = "tTLS"
|
|
|
|
|
2019-12-10 00:54:48 +00:00
|
|
|
// DefaultLeeway is the duration for matching not before claims.
|
|
|
|
const DefaultLeeway = 1 * time.Minute
|
|
|
|
|
2019-11-19 01:07:23 +00:00
|
|
|
// IdentityFile contains the location of the identity file.
|
|
|
|
var IdentityFile = filepath.Join(config.StepPath(), "config", "identity.json")
|
|
|
|
|
2019-12-12 04:23:44 +00:00
|
|
|
// DefaultsFile contains the location of the defaults file.
|
|
|
|
var DefaultsFile = filepath.Join(config.StepPath(), "config", "defaults.json")
|
|
|
|
|
2019-11-19 01:07:23 +00:00
|
|
|
// Identity represents the identity file that can be used to authenticate with
|
|
|
|
// the CA.
|
|
|
|
type Identity struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
Certificate string `json:"crt"`
|
|
|
|
Key string `json:"key"`
|
2021-04-21 23:05:27 +00:00
|
|
|
|
|
|
|
// Host is the tunnel host for a TunnelTLS (tTLS) identity.
|
|
|
|
Host string `json:"host,omitempty"`
|
|
|
|
// Root is the CA bundle of root CAs used in TunnelTLS to trust the
|
|
|
|
// certificate of the host.
|
|
|
|
Root string `json:"root,omitempty"`
|
2019-11-19 01:07:23 +00:00
|
|
|
}
|
|
|
|
|
2021-04-21 23:05:27 +00:00
|
|
|
// LoadIdentity loads an identity present in the given filename.
|
|
|
|
func LoadIdentity(filename string) (*Identity, error) {
|
|
|
|
b, err := ioutil.ReadFile(filename)
|
2019-11-21 00:03:31 +00:00
|
|
|
if err != nil {
|
2021-04-21 23:05:27 +00:00
|
|
|
return nil, errors.Wrapf(err, "error reading %s", filename)
|
2019-11-21 00:03:31 +00:00
|
|
|
}
|
|
|
|
identity := new(Identity)
|
|
|
|
if err := json.Unmarshal(b, &identity); err != nil {
|
2021-04-21 23:05:27 +00:00
|
|
|
return nil, errors.Wrapf(err, "error unmarshaling %s", filename)
|
2019-11-21 00:03:31 +00:00
|
|
|
}
|
|
|
|
return identity, nil
|
|
|
|
}
|
|
|
|
|
2021-04-21 23:05:27 +00:00
|
|
|
// LoadDefaultIdentity loads the default identity.
|
|
|
|
func LoadDefaultIdentity() (*Identity, error) {
|
|
|
|
return LoadIdentity(IdentityFile)
|
|
|
|
}
|
|
|
|
|
2019-12-12 20:23:53 +00:00
|
|
|
// configDir and identityDir are used in WriteDefaultIdentity for testing
|
|
|
|
// purposes.
|
|
|
|
var (
|
|
|
|
configDir = filepath.Join(config.StepPath(), "config")
|
|
|
|
identityDir = filepath.Join(config.StepPath(), "identity")
|
|
|
|
)
|
|
|
|
|
2019-11-20 19:50:46 +00:00
|
|
|
// WriteDefaultIdentity writes the given certificates and key and the
|
|
|
|
// identity.json pointing to the new files.
|
|
|
|
func WriteDefaultIdentity(certChain []api.Certificate, key crypto.PrivateKey) error {
|
2019-12-12 20:23:53 +00:00
|
|
|
if err := os.MkdirAll(configDir, 0700); err != nil {
|
2019-11-20 19:50:46 +00:00
|
|
|
return errors.Wrap(err, "error creating config directory")
|
|
|
|
}
|
|
|
|
|
2019-12-12 20:23:53 +00:00
|
|
|
if err := os.MkdirAll(identityDir, 0700); err != nil {
|
2019-11-20 19:50:46 +00:00
|
|
|
return errors.Wrap(err, "error creating identity directory")
|
|
|
|
}
|
|
|
|
|
2019-12-12 20:23:53 +00:00
|
|
|
certFilename := filepath.Join(identityDir, "identity.crt")
|
|
|
|
keyFilename := filepath.Join(identityDir, "identity_key")
|
2019-11-20 19:50:46 +00:00
|
|
|
|
|
|
|
// Write certificate
|
2021-04-21 23:05:27 +00:00
|
|
|
if err := writeCertificate(certFilename, certChain); err != nil {
|
2019-12-18 22:39:01 +00:00
|
|
|
return err
|
2019-11-20 19:50:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write key
|
2019-12-18 22:39:01 +00:00
|
|
|
buf := new(bytes.Buffer)
|
2019-11-20 19:50:46 +00:00
|
|
|
block, err := pemutil.Serialize(key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := pem.Encode(buf, block); err != nil {
|
|
|
|
return errors.Wrap(err, "error encoding identity key")
|
|
|
|
}
|
|
|
|
if err := ioutil.WriteFile(keyFilename, buf.Bytes(), 0600); err != nil {
|
|
|
|
return errors.Wrap(err, "error writing identity certificate")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write identity.json
|
|
|
|
buf.Reset()
|
|
|
|
enc := json.NewEncoder(buf)
|
|
|
|
enc.SetIndent("", " ")
|
|
|
|
if err := enc.Encode(Identity{
|
|
|
|
Type: string(MutualTLS),
|
|
|
|
Certificate: certFilename,
|
|
|
|
Key: keyFilename,
|
|
|
|
}); err != nil {
|
|
|
|
return errors.Wrap(err, "error writing identity json")
|
|
|
|
}
|
|
|
|
if err := ioutil.WriteFile(IdentityFile, buf.Bytes(), 0600); err != nil {
|
|
|
|
return errors.Wrap(err, "error writing identity certificate")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-21 23:05:27 +00:00
|
|
|
// writeCertificate writes the given certificate on disk.
|
|
|
|
func writeCertificate(filename string, certChain []api.Certificate) error {
|
2019-12-18 22:39:01 +00:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
for _, crt := range certChain {
|
|
|
|
block := &pem.Block{
|
|
|
|
Type: "CERTIFICATE",
|
|
|
|
Bytes: crt.Raw,
|
|
|
|
}
|
|
|
|
if err := pem.Encode(buf, block); err != nil {
|
2021-04-21 23:05:27 +00:00
|
|
|
return errors.Wrap(err, "error encoding certificate")
|
2019-12-18 22:39:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-21 23:05:27 +00:00
|
|
|
if err := ioutil.WriteFile(filename, buf.Bytes(), 0600); err != nil {
|
|
|
|
return errors.Wrap(err, "error writing certificate")
|
2019-12-18 22:39:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-19 01:07:23 +00:00
|
|
|
// Kind returns the type for the given identity.
|
2019-12-12 04:23:44 +00:00
|
|
|
func (i *Identity) Kind() Type {
|
2019-11-19 01:07:23 +00:00
|
|
|
switch strings.ToLower(i.Type) {
|
2019-11-20 19:50:46 +00:00
|
|
|
case "":
|
|
|
|
return Disabled
|
2019-11-19 01:07:23 +00:00
|
|
|
case "mtls":
|
|
|
|
return MutualTLS
|
2021-04-21 23:05:27 +00:00
|
|
|
case "ttls":
|
|
|
|
return TunnelTLS
|
2019-11-19 01:07:23 +00:00
|
|
|
default:
|
2019-12-12 04:23:44 +00:00
|
|
|
return Type(i.Type)
|
2019-11-19 01:07:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate validates the identity object.
|
|
|
|
func (i *Identity) Validate() error {
|
|
|
|
switch i.Kind() {
|
2019-11-20 19:50:46 +00:00
|
|
|
case Disabled:
|
|
|
|
return nil
|
2019-11-19 01:07:23 +00:00
|
|
|
case MutualTLS:
|
|
|
|
if i.Certificate == "" {
|
|
|
|
return errors.New("identity.crt cannot be empty")
|
|
|
|
}
|
|
|
|
if i.Key == "" {
|
|
|
|
return errors.New("identity.key cannot be empty")
|
|
|
|
}
|
2019-12-12 04:23:44 +00:00
|
|
|
if err := fileExists(i.Certificate); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-21 23:05:27 +00:00
|
|
|
return fileExists(i.Key)
|
|
|
|
case TunnelTLS:
|
|
|
|
if i.Host == "" {
|
2021-04-27 01:45:46 +00:00
|
|
|
return errors.New("tunnel.host cannot be empty")
|
2021-04-21 23:05:27 +00:00
|
|
|
}
|
|
|
|
if i.Certificate != "" {
|
|
|
|
if err := fileExists(i.Certificate); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if i.Key == "" {
|
|
|
|
return errors.New("tunnel.key cannot be empty")
|
|
|
|
}
|
|
|
|
if err := fileExists(i.Key); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if i.Root != "" {
|
|
|
|
if err := fileExists(i.Root); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-12 04:23:44 +00:00
|
|
|
}
|
2019-11-19 01:07:23 +00:00
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return errors.Errorf("unsupported identity type %s", i.Type)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-12 04:23:44 +00:00
|
|
|
// TLSCertificate returns a tls.Certificate for the identity.
|
|
|
|
func (i *Identity) TLSCertificate() (tls.Certificate, error) {
|
|
|
|
fail := func(err error) (tls.Certificate, error) { return tls.Certificate{}, err }
|
2019-11-19 01:07:23 +00:00
|
|
|
switch i.Kind() {
|
2019-11-20 19:50:46 +00:00
|
|
|
case Disabled:
|
2019-12-12 04:23:44 +00:00
|
|
|
return tls.Certificate{}, nil
|
2021-04-21 23:05:27 +00:00
|
|
|
case MutualTLS, TunnelTLS:
|
2019-11-19 01:07:23 +00:00
|
|
|
crt, err := tls.LoadX509KeyPair(i.Certificate, i.Key)
|
|
|
|
if err != nil {
|
2019-12-12 04:23:44 +00:00
|
|
|
return fail(errors.Wrap(err, "error creating identity certificate"))
|
2019-11-19 01:07:23 +00:00
|
|
|
}
|
2019-12-12 04:23:44 +00:00
|
|
|
|
2019-11-20 19:50:46 +00:00
|
|
|
// Check if certificate is expired.
|
|
|
|
x509Cert, err := x509.ParseCertificate(crt.Certificate[0])
|
|
|
|
if err != nil {
|
2019-12-12 04:23:44 +00:00
|
|
|
return fail(errors.Wrap(err, "error creating identity certificate"))
|
2019-11-20 19:50:46 +00:00
|
|
|
}
|
2019-12-10 00:54:48 +00:00
|
|
|
now := time.Now().Truncate(time.Second)
|
2019-12-12 04:23:44 +00:00
|
|
|
if now.Add(DefaultLeeway).Before(x509Cert.NotBefore) {
|
|
|
|
return fail(errors.New("certificate is not yet valid"))
|
2019-11-20 19:50:46 +00:00
|
|
|
}
|
2019-12-12 04:23:44 +00:00
|
|
|
if now.After(x509Cert.NotAfter) {
|
|
|
|
return fail(errors.New("certificate is already expired"))
|
|
|
|
}
|
|
|
|
return crt, nil
|
2019-11-19 01:07:23 +00:00
|
|
|
default:
|
2019-12-12 04:23:44 +00:00
|
|
|
return fail(errors.Errorf("unsupported identity type %s", i.Type))
|
2019-11-19 01:07:23 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-21 00:03:31 +00:00
|
|
|
|
2020-03-04 23:02:17 +00:00
|
|
|
// GetClientCertificateFunc returns a method that can be used as the
|
|
|
|
// GetClientCertificate property in a tls.Config.
|
|
|
|
func (i *Identity) GetClientCertificateFunc() func(*tls.CertificateRequestInfo) (*tls.Certificate, error) {
|
|
|
|
return func(*tls.CertificateRequestInfo) (*tls.Certificate, error) {
|
|
|
|
crt, err := tls.LoadX509KeyPair(i.Certificate, i.Key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error loading identity certificate")
|
|
|
|
}
|
|
|
|
return &crt, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-21 23:05:27 +00:00
|
|
|
// GetCertPool returns a x509.CertPool if the identity defines a custom root.
|
|
|
|
func (i *Identity) GetCertPool() (*x509.CertPool, error) {
|
|
|
|
if i.Root == "" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
b, err := ioutil.ReadFile(i.Root)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error reading identity root")
|
|
|
|
}
|
|
|
|
pool := x509.NewCertPool()
|
|
|
|
if !pool.AppendCertsFromPEM(b) {
|
|
|
|
return nil, errors.Errorf("error pasing identity root: %s does not contain any certificate", i.Root)
|
|
|
|
}
|
|
|
|
return pool, nil
|
|
|
|
}
|
|
|
|
|
2019-12-18 20:46:46 +00:00
|
|
|
// Renewer is that interface that a renew client must implement.
|
|
|
|
type Renewer interface {
|
|
|
|
GetRootCAs() *x509.CertPool
|
|
|
|
Renew(tr http.RoundTripper) (*api.SignResponse, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Renew renews the current identity certificate using a client with a renew
|
|
|
|
// method.
|
|
|
|
func (i *Identity) Renew(client Renewer) error {
|
|
|
|
switch i.Kind() {
|
|
|
|
case Disabled:
|
|
|
|
return nil
|
2021-04-21 23:05:27 +00:00
|
|
|
case MutualTLS, TunnelTLS:
|
2019-12-18 20:46:46 +00:00
|
|
|
cert, err := i.TLSCertificate()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tr := http.DefaultTransport.(*http.Transport).Clone()
|
|
|
|
tr.TLSClientConfig = &tls.Config{
|
|
|
|
Certificates: []tls.Certificate{cert},
|
|
|
|
RootCAs: client.GetRootCAs(),
|
|
|
|
PreferServerCipherSuites: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
sign, err := client.Renew(tr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if sign.CertChainPEM == nil || len(sign.CertChainPEM) == 0 {
|
|
|
|
sign.CertChainPEM = []api.Certificate{sign.ServerPEM, sign.CaPEM}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write certificate
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
for _, crt := range sign.CertChainPEM {
|
|
|
|
block := &pem.Block{
|
|
|
|
Type: "CERTIFICATE",
|
|
|
|
Bytes: crt.Raw,
|
|
|
|
}
|
|
|
|
if err := pem.Encode(buf, block); err != nil {
|
|
|
|
return errors.Wrap(err, "error encoding identity certificate")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
certFilename := filepath.Join(identityDir, "identity.crt")
|
|
|
|
if err := ioutil.WriteFile(certFilename, buf.Bytes(), 0600); err != nil {
|
|
|
|
return errors.Wrap(err, "error writing identity certificate")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return errors.Errorf("unsupported identity type %s", i.Type)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-12 04:23:44 +00:00
|
|
|
func fileExists(filename string) error {
|
|
|
|
info, err := os.Stat(filename)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "error reading %s", filename)
|
|
|
|
}
|
|
|
|
if info.IsDir() {
|
|
|
|
return errors.Errorf("error reading %s: file is a directory", filename)
|
2019-11-21 00:03:31 +00:00
|
|
|
}
|
2019-12-12 04:23:44 +00:00
|
|
|
return nil
|
2019-11-21 00:03:31 +00:00
|
|
|
}
|