2018-08-13 09:39:45 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package provider
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto"
|
2018-08-23 15:08:26 +01:00
|
|
|
"crypto/ecdsa"
|
2018-08-13 09:39:45 +01:00
|
|
|
"crypto/x509"
|
|
|
|
"encoding/pem"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
2018-12-07 13:44:25 +00:00
|
|
|
"storj.io/storj/pkg/utils"
|
2018-08-27 23:23:48 +01:00
|
|
|
|
2018-08-13 09:39:45 +01:00
|
|
|
"storj.io/storj/pkg/peertls"
|
2018-11-29 18:39:27 +00:00
|
|
|
"storj.io/storj/pkg/storj"
|
2018-08-13 09:39:45 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// PeerCertificateAuthority represents the CA which is used to validate peer identities
|
|
|
|
type PeerCertificateAuthority struct {
|
2018-11-01 15:48:43 +00:00
|
|
|
RestChain []*x509.Certificate
|
2018-08-13 09:39:45 +01:00
|
|
|
// Cert is the x509 certificate of the CA
|
|
|
|
Cert *x509.Certificate
|
|
|
|
// The ID is calculated from the CA public key.
|
2018-11-29 18:39:27 +00:00
|
|
|
ID storj.NodeID
|
2018-08-13 09:39:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// FullCertificateAuthority represents the CA which is used to author and validate full identities
|
|
|
|
type FullCertificateAuthority struct {
|
2018-11-01 15:48:43 +00:00
|
|
|
RestChain []*x509.Certificate
|
2018-08-13 09:39:45 +01:00
|
|
|
// Cert is the x509 certificate of the CA
|
|
|
|
Cert *x509.Certificate
|
|
|
|
// The ID is calculated from the CA public key.
|
2018-11-29 18:39:27 +00:00
|
|
|
ID storj.NodeID
|
2018-08-13 09:39:45 +01:00
|
|
|
// Key is the private key of the CA
|
|
|
|
Key crypto.PrivateKey
|
|
|
|
}
|
|
|
|
|
2018-08-27 18:28:16 +01:00
|
|
|
// CASetupConfig is for creating a CA
|
2018-08-13 09:39:45 +01:00
|
|
|
type CASetupConfig struct {
|
2018-11-01 15:48:43 +00:00
|
|
|
ParentCertPath string `help:"path to the parent authority's certificate chain"`
|
|
|
|
ParentKeyPath string `help:"path to the parent authority's private key"`
|
|
|
|
CertPath string `help:"path to the certificate chain for this identity" default:"$CONFDIR/ca.cert"`
|
|
|
|
KeyPath string `help:"path to the private key for this identity" default:"$CONFDIR/ca.key"`
|
2018-12-11 18:40:54 +00:00
|
|
|
Difficulty uint64 `help:"minimum difficulty for identity generation" default:"15"`
|
2018-11-01 15:48:43 +00:00
|
|
|
Timeout string `help:"timeout for CA generation; golang duration string (0 no timeout)" default:"5m"`
|
|
|
|
Overwrite bool `help:"if true, existing CA certs AND keys will overwritten" default:"false"`
|
|
|
|
Concurrency uint `help:"number of concurrent workers for certificate authority generation" default:"4"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewCAOptions is used to pass parameters to `NewCA`
|
|
|
|
type NewCAOptions struct {
|
|
|
|
// Difficulty is the number of trailing zero-bits the nodeID must have
|
|
|
|
Difficulty uint16
|
|
|
|
// Concurrency is the number of go routines used to generate a CA of sufficient difficulty
|
|
|
|
Concurrency uint
|
|
|
|
// ParentCert, if provided will be prepended to the certificate chain
|
|
|
|
ParentCert *x509.Certificate
|
|
|
|
// ParentKey ()
|
|
|
|
ParentKey crypto.PrivateKey
|
2018-08-13 09:39:45 +01:00
|
|
|
}
|
|
|
|
|
2018-08-27 23:23:48 +01:00
|
|
|
// PeerCAConfig is for locating a CA certificate without a private key
|
|
|
|
type PeerCAConfig struct {
|
|
|
|
CertPath string `help:"path to the certificate chain for this identity" default:"$CONFDIR/ca.cert"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// FullCAConfig is for locating a CA certificate and it's private key
|
|
|
|
type FullCAConfig struct {
|
2018-08-13 09:39:45 +01:00
|
|
|
CertPath string `help:"path to the certificate chain for this identity" default:"$CONFDIR/ca.cert"`
|
|
|
|
KeyPath string `help:"path to the private key for this identity" default:"$CONFDIR/ca.key"`
|
|
|
|
}
|
|
|
|
|
2018-08-27 18:28:16 +01:00
|
|
|
// Status returns the status of the CA cert/key files for the config
|
|
|
|
func (caS CASetupConfig) Status() TLSFilesStatus {
|
2018-08-13 09:39:45 +01:00
|
|
|
return statTLSFiles(caS.CertPath, caS.KeyPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create generates and saves a CA using the config
|
2018-08-27 23:23:48 +01:00
|
|
|
func (caS CASetupConfig) Create(ctx context.Context) (*FullCertificateAuthority, error) {
|
2018-11-01 15:48:43 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
parent *FullCertificateAuthority
|
|
|
|
)
|
|
|
|
if caS.ParentCertPath != "" && caS.ParentKeyPath != "" {
|
|
|
|
parent, err = FullCAConfig{
|
|
|
|
CertPath: caS.ParentCertPath,
|
|
|
|
KeyPath: caS.ParentKeyPath,
|
|
|
|
}.Load()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if parent == nil {
|
|
|
|
parent = &FullCertificateAuthority{}
|
|
|
|
}
|
|
|
|
|
|
|
|
ca, err := NewCA(ctx, NewCAOptions{
|
|
|
|
Difficulty: uint16(caS.Difficulty),
|
|
|
|
Concurrency: caS.Concurrency,
|
|
|
|
ParentCert: parent.Cert,
|
|
|
|
ParentKey: parent.Key,
|
|
|
|
})
|
2018-08-13 09:39:45 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-08-27 23:23:48 +01:00
|
|
|
caC := FullCAConfig{
|
2018-08-13 09:39:45 +01:00
|
|
|
CertPath: caS.CertPath,
|
|
|
|
KeyPath: caS.KeyPath,
|
|
|
|
}
|
|
|
|
return ca, caC.Save(ca)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load loads a CA from the given configuration
|
2018-08-27 23:23:48 +01:00
|
|
|
func (fc FullCAConfig) Load() (*FullCertificateAuthority, error) {
|
|
|
|
p, err := fc.PeerConfig().Load()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
kb, err := ioutil.ReadFile(fc.KeyPath)
|
2018-08-13 09:39:45 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, peertls.ErrNotExist.Wrap(err)
|
|
|
|
}
|
2018-08-27 23:23:48 +01:00
|
|
|
kp, _ := pem.Decode(kb)
|
|
|
|
k, err := x509.ParseECPrivateKey(kp.Bytes)
|
|
|
|
if err != nil {
|
2018-09-05 17:10:35 +01:00
|
|
|
return nil, errs.New("unable to parse EC private key: %v", err)
|
2018-08-27 23:23:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return &FullCertificateAuthority{
|
2018-11-01 15:48:43 +00:00
|
|
|
RestChain: p.RestChain,
|
|
|
|
Cert: p.Cert,
|
|
|
|
Key: k,
|
|
|
|
ID: p.ID,
|
2018-08-27 23:23:48 +01:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PeerConfig converts a full ca config to a peer ca config
|
|
|
|
func (fc FullCAConfig) PeerConfig() PeerCAConfig {
|
|
|
|
return PeerCAConfig{
|
|
|
|
CertPath: fc.CertPath,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load loads a CA from the given configuration
|
|
|
|
func (pc PeerCAConfig) Load() (*PeerCertificateAuthority, error) {
|
|
|
|
cd, err := ioutil.ReadFile(pc.CertPath)
|
2018-08-13 09:39:45 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, peertls.ErrNotExist.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var cb [][]byte
|
|
|
|
for {
|
|
|
|
var cp *pem.Block
|
|
|
|
cp, cd = pem.Decode(cd)
|
|
|
|
if cp == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
cb = append(cb, cp.Bytes)
|
|
|
|
}
|
|
|
|
c, err := ParseCertChain(cb)
|
|
|
|
if err != nil {
|
2018-08-27 23:23:48 +01:00
|
|
|
return nil, errs.New("failed to load identity %#v: %v",
|
|
|
|
pc.CertPath, err)
|
2018-08-13 09:39:45 +01:00
|
|
|
}
|
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
i, err := NodeIDFromKey(c[0].PublicKey)
|
2018-08-13 09:39:45 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-08-27 23:23:48 +01:00
|
|
|
return &PeerCertificateAuthority{
|
2018-11-01 15:48:43 +00:00
|
|
|
RestChain: c[1:],
|
|
|
|
Cert: c[0],
|
|
|
|
ID: i,
|
2018-08-13 09:39:45 +01:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-08-23 15:08:26 +01:00
|
|
|
// NewCA creates a new full identity with the given difficulty
|
2018-11-01 15:48:43 +00:00
|
|
|
func NewCA(ctx context.Context, opts NewCAOptions) (*FullCertificateAuthority, error) {
|
|
|
|
if opts.Concurrency < 1 {
|
|
|
|
opts.Concurrency = 1
|
2018-08-13 09:39:45 +01:00
|
|
|
}
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
|
|
|
|
eC := make(chan error)
|
|
|
|
caC := make(chan FullCertificateAuthority, 1)
|
2018-11-01 15:48:43 +00:00
|
|
|
for i := 0; i < int(opts.Concurrency); i++ {
|
|
|
|
go newCAWorker(ctx, opts.Difficulty, opts.ParentCert, opts.ParentKey, caC, eC)
|
2018-08-13 09:39:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case ca := <-caC:
|
|
|
|
cancel()
|
|
|
|
return &ca, nil
|
|
|
|
case err := <-eC:
|
|
|
|
cancel()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save saves a CA with the given configuration
|
2018-08-27 23:23:48 +01:00
|
|
|
func (fc FullCAConfig) Save(ca *FullCertificateAuthority) error {
|
2018-12-07 13:44:25 +00:00
|
|
|
mode := os.O_WRONLY | os.O_CREATE
|
|
|
|
certFile, err := openCert(fc.CertPath, mode)
|
2018-08-13 09:39:45 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-12-07 13:44:25 +00:00
|
|
|
|
|
|
|
keyFile, err := openKey(fc.KeyPath, mode)
|
2018-08-13 09:39:45 +01:00
|
|
|
if err != nil {
|
2018-12-07 13:44:25 +00:00
|
|
|
return utils.CombineErrors(err, certFile.Close())
|
2018-08-13 09:39:45 +01:00
|
|
|
}
|
|
|
|
|
2018-11-01 15:48:43 +00:00
|
|
|
chain := []*x509.Certificate{ca.Cert}
|
|
|
|
chain = append(chain, ca.RestChain...)
|
2018-12-07 13:44:25 +00:00
|
|
|
certWriteErr := peertls.WriteChain(certFile, chain...)
|
|
|
|
keyWriteErr := peertls.WriteKey(keyFile, ca.Key)
|
|
|
|
|
|
|
|
return utils.CombineErrors(certWriteErr, keyWriteErr, certFile.Close(), keyFile.Close())
|
2018-08-13 09:39:45 +01:00
|
|
|
}
|
|
|
|
|
2018-08-27 18:28:16 +01:00
|
|
|
// NewIdentity generates a new `FullIdentity` based on the CA. The CA
|
2018-08-13 09:39:45 +01:00
|
|
|
// cert is included in the identity's cert chain and the identity's leaf cert
|
|
|
|
// is signed by the CA.
|
2018-08-23 15:08:26 +01:00
|
|
|
func (ca FullCertificateAuthority) NewIdentity() (*FullIdentity, error) {
|
2018-12-07 13:44:25 +00:00
|
|
|
leafTemplate, err := peertls.LeafTemplate()
|
2018-08-13 09:39:45 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-12-07 13:44:25 +00:00
|
|
|
leafKey, err := peertls.NewKey()
|
2018-08-13 09:39:45 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-12-07 13:44:25 +00:00
|
|
|
pk, ok := leafKey.(*ecdsa.PrivateKey)
|
2018-08-23 15:08:26 +01:00
|
|
|
if !ok {
|
2018-12-07 13:44:25 +00:00
|
|
|
return nil, peertls.ErrUnsupportedKey.New("%T", leafKey)
|
2018-08-23 15:08:26 +01:00
|
|
|
}
|
2018-12-07 13:44:25 +00:00
|
|
|
leafCert, err := peertls.NewCert(pk, ca.Key, leafTemplate, ca.Cert)
|
2018-08-13 09:39:45 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-12-07 13:44:25 +00:00
|
|
|
if ca.RestChain != nil && len(ca.RestChain) > 0 {
|
|
|
|
err := peertls.AddSignedLeafExt(ca.Key, leafCert)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-13 09:39:45 +01:00
|
|
|
return &FullIdentity{
|
2018-11-01 15:48:43 +00:00
|
|
|
RestChain: ca.RestChain,
|
|
|
|
CA: ca.Cert,
|
2018-12-07 13:44:25 +00:00
|
|
|
Leaf: leafCert,
|
|
|
|
Key: leafKey,
|
2018-11-01 15:48:43 +00:00
|
|
|
ID: ca.ID,
|
2018-08-13 09:39:45 +01:00
|
|
|
}, nil
|
|
|
|
}
|