2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-08-13 09:39:45 +01:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2019-01-02 10:23:25 +00:00
|
|
|
package identity
|
2018-08-13 09:39:45 +01:00
|
|
|
|
|
|
|
import (
|
2018-12-13 13:50:05 +00:00
|
|
|
"bytes"
|
2018-08-13 09:39:45 +01:00
|
|
|
"context"
|
|
|
|
"crypto"
|
|
|
|
"crypto/x509"
|
2019-04-03 16:03:53 +01:00
|
|
|
"crypto/x509/pkix"
|
2019-01-25 16:55:45 +00:00
|
|
|
"fmt"
|
2019-01-26 14:59:53 +00:00
|
|
|
"io"
|
2018-08-13 09:39:45 +01:00
|
|
|
"io/ioutil"
|
2019-01-26 14:59:53 +00:00
|
|
|
"log"
|
2019-01-07 18:02:22 +00:00
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
2018-08-13 09:39:45 +01:00
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
2018-08-27 23:23:48 +01:00
|
|
|
|
2018-08-13 09:39:45 +01:00
|
|
|
"storj.io/storj/pkg/peertls"
|
2019-03-25 21:52:12 +00:00
|
|
|
"storj.io/storj/pkg/peertls/extensions"
|
2019-02-07 09:04:29 +00:00
|
|
|
"storj.io/storj/pkg/pkcrypto"
|
2018-11-29 18:39:27 +00:00
|
|
|
"storj.io/storj/pkg/storj"
|
2018-08-13 09:39:45 +01:00
|
|
|
)
|
|
|
|
|
2019-01-07 18:02:22 +00:00
|
|
|
const minimumLoggableDifficulty = 8
|
|
|
|
|
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 {
|
2019-04-08 19:15:19 +01:00
|
|
|
VersionNumber uint `default:"0" help:"which identity version to use (0 is latest)"`
|
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"`
|
2019-01-22 14:34:40 +00:00
|
|
|
CertPath string `help:"path to the certificate chain for this identity" default:"$IDENTITYDIR/ca.cert"`
|
|
|
|
KeyPath string `help:"path to the private key for this identity" default:"$IDENTITYDIR/ca.key"`
|
2019-01-23 11:36:19 +00:00
|
|
|
Difficulty uint64 `help:"minimum difficulty for identity generation" default:"30"`
|
2018-11-01 15:48:43 +00:00
|
|
|
Timeout string `help:"timeout for CA generation; golang duration string (0 no timeout)" default:"5m"`
|
2019-02-26 08:55:52 +00:00
|
|
|
Overwrite bool `help:"if true, existing CA certs AND keys will overwritten" default:"false" setup:"true"`
|
2018-11-01 15:48:43 +00:00
|
|
|
Concurrency uint `help:"number of concurrent workers for certificate authority generation" default:"4"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewCAOptions is used to pass parameters to `NewCA`
|
|
|
|
type NewCAOptions struct {
|
2019-04-08 19:15:19 +01:00
|
|
|
// VersionNumber is the IDVersion to use for the identity
|
|
|
|
VersionNumber storj.IDVersionNumber
|
2018-11-01 15:48:43 +00:00
|
|
|
// 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
|
2019-01-26 14:59:53 +00:00
|
|
|
// Logger is used to log generation status updates
|
|
|
|
Logger io.Writer
|
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 {
|
2019-01-22 14:34:40 +00:00
|
|
|
CertPath string `help:"path to the certificate chain for this identity" default:"$IDENTITYDIR/ca.cert"`
|
2018-08-27 23:23:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// FullCAConfig is for locating a CA certificate and it's private key
|
|
|
|
type FullCAConfig struct {
|
2019-01-22 14:34:40 +00:00
|
|
|
CertPath string `help:"path to the certificate chain for this identity" default:"$IDENTITYDIR/ca.cert"`
|
|
|
|
KeyPath string `help:"path to the private key for this identity" default:"$IDENTITYDIR/ca.key"`
|
2018-08-13 09:39:45 +01:00
|
|
|
}
|
|
|
|
|
2019-01-02 17:39:17 +00:00
|
|
|
// NewCA creates a new full identity with the given difficulty
|
|
|
|
func NewCA(ctx context.Context, opts NewCAOptions) (_ *FullCertificateAuthority, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
var (
|
2019-01-07 18:02:22 +00:00
|
|
|
highscore = new(uint32)
|
2019-01-25 16:55:45 +00:00
|
|
|
i = new(uint32)
|
2019-01-07 18:02:22 +00:00
|
|
|
|
|
|
|
mu sync.Mutex
|
2019-02-07 20:39:20 +00:00
|
|
|
selectedKey crypto.PrivateKey
|
2019-01-07 18:02:22 +00:00
|
|
|
selectedID storj.NodeID
|
2019-01-02 17:39:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if opts.Concurrency < 1 {
|
|
|
|
opts.Concurrency = 1
|
|
|
|
}
|
|
|
|
|
2019-01-29 12:12:24 +00:00
|
|
|
if opts.Logger != nil {
|
|
|
|
fmt.Fprintf(opts.Logger, "Generating key with a minimum a difficulty of %d...\n", opts.Difficulty)
|
|
|
|
}
|
2019-04-08 19:15:19 +01:00
|
|
|
|
|
|
|
version, err := storj.GetIDVersion(opts.VersionNumber)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-01-26 14:59:53 +00:00
|
|
|
updateStatus := func() {
|
|
|
|
if opts.Logger != nil {
|
|
|
|
count := atomic.LoadUint32(i)
|
|
|
|
hs := atomic.LoadUint32(highscore)
|
|
|
|
_, err := fmt.Fprintf(opts.Logger, "\rGenerated %d keys; best difficulty so far: %d", count, hs)
|
|
|
|
if err != nil {
|
|
|
|
log.Print(errs.Wrap(err))
|
|
|
|
}
|
|
|
|
}
|
2019-01-25 16:55:45 +00:00
|
|
|
}
|
2019-04-08 19:15:19 +01:00
|
|
|
err = GenerateKeys(ctx, minimumLoggableDifficulty, int(opts.Concurrency), version,
|
2019-02-07 20:39:20 +00:00
|
|
|
func(k crypto.PrivateKey, id storj.NodeID) (done bool, err error) {
|
2019-01-26 14:59:53 +00:00
|
|
|
if opts.Logger != nil {
|
|
|
|
if atomic.AddUint32(i, 1)%100 == 0 {
|
|
|
|
updateStatus()
|
|
|
|
}
|
2019-01-25 16:55:45 +00:00
|
|
|
}
|
|
|
|
|
2019-01-07 18:02:22 +00:00
|
|
|
difficulty, err := id.Difficulty()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if difficulty >= opts.Difficulty {
|
|
|
|
mu.Lock()
|
|
|
|
if selectedKey == nil {
|
2019-01-26 14:59:53 +00:00
|
|
|
updateStatus()
|
2019-01-07 18:02:22 +00:00
|
|
|
selectedKey = k
|
|
|
|
selectedID = id
|
|
|
|
}
|
|
|
|
mu.Unlock()
|
2019-01-26 14:59:53 +00:00
|
|
|
if opts.Logger != nil {
|
2019-02-01 16:58:33 +00:00
|
|
|
atomic.SwapUint32(highscore, uint32(difficulty))
|
|
|
|
updateStatus()
|
2019-01-26 14:59:53 +00:00
|
|
|
_, err := fmt.Fprintf(opts.Logger, "\nFound a key with difficulty %d!\n", difficulty)
|
|
|
|
if err != nil {
|
|
|
|
log.Print(errs.Wrap(err))
|
|
|
|
}
|
|
|
|
}
|
2019-01-07 18:02:22 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
hs := atomic.LoadUint32(highscore)
|
|
|
|
if uint32(difficulty) <= hs {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
if atomic.CompareAndSwapUint32(highscore, hs, uint32(difficulty)) {
|
2019-01-26 14:59:53 +00:00
|
|
|
updateStatus()
|
2019-01-07 18:02:22 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-01-02 17:39:17 +00:00
|
|
|
}
|
|
|
|
|
2019-01-07 18:02:22 +00:00
|
|
|
ct, err := peertls.CATemplate()
|
|
|
|
if err != nil {
|
2019-01-02 17:39:17 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-03 16:03:53 +01:00
|
|
|
|
2019-04-08 19:15:19 +01:00
|
|
|
if err := extensions.AddExtraExtension(ct, storj.NewVersionExt(version)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var cert *x509.Certificate
|
2019-04-03 16:03:53 +01:00
|
|
|
if opts.ParentKey == nil {
|
2019-04-08 19:15:19 +01:00
|
|
|
cert, err = peertls.CreateSelfSignedCertificate(selectedKey, ct)
|
2019-04-04 00:21:32 +01:00
|
|
|
} else {
|
2019-04-08 19:15:19 +01:00
|
|
|
cert, err = peertls.CreateCertificate(pkcrypto.PublicKeyFromPrivate(selectedKey), opts.ParentKey, ct, opts.ParentCert)
|
2019-04-03 16:03:53 +01:00
|
|
|
}
|
2019-01-07 18:02:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-08 19:15:19 +01:00
|
|
|
|
2019-01-07 18:02:22 +00:00
|
|
|
ca := &FullCertificateAuthority{
|
2019-04-08 19:15:19 +01:00
|
|
|
Cert: cert,
|
2019-01-07 18:02:22 +00:00
|
|
|
Key: selectedKey,
|
|
|
|
ID: selectedID,
|
|
|
|
}
|
|
|
|
if opts.ParentCert != nil {
|
|
|
|
ca.RestChain = []*x509.Certificate{opts.ParentCert}
|
|
|
|
}
|
|
|
|
return ca, nil
|
2019-01-02 17:39:17 +00:00
|
|
|
}
|
|
|
|
|
2018-08-27 18:28:16 +01:00
|
|
|
// Status returns the status of the CA cert/key files for the config
|
2019-03-12 14:42:38 +00:00
|
|
|
func (caS CASetupConfig) Status() (TLSFilesStatus, error) {
|
2018-08-13 09:39:45 +01:00
|
|
|
return statTLSFiles(caS.CertPath, caS.KeyPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create generates and saves a CA using the config
|
2019-01-26 14:59:53 +00:00
|
|
|
func (caS CASetupConfig) Create(ctx context.Context, logger io.Writer) (*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()
|
2019-01-18 10:36:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-11-01 15:48:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if parent == nil {
|
|
|
|
parent = &FullCertificateAuthority{}
|
|
|
|
}
|
|
|
|
|
2019-04-22 11:58:57 +01:00
|
|
|
version, err := storj.GetIDVersion(storj.IDVersionNumber(caS.VersionNumber))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-11-01 15:48:43 +00:00
|
|
|
ca, err := NewCA(ctx, NewCAOptions{
|
2019-04-22 11:58:57 +01:00
|
|
|
VersionNumber: version.Number,
|
2019-04-08 19:15:19 +01:00
|
|
|
Difficulty: uint16(caS.Difficulty),
|
|
|
|
Concurrency: caS.Concurrency,
|
|
|
|
ParentCert: parent.Cert,
|
|
|
|
ParentKey: parent.Key,
|
|
|
|
Logger: logger,
|
2018-11-01 15:48:43 +00:00
|
|
|
})
|
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)
|
|
|
|
}
|
|
|
|
|
2019-01-04 17:23:23 +00:00
|
|
|
// FullConfig converts a `CASetupConfig` to `FullCAConfig`
|
|
|
|
func (caS CASetupConfig) FullConfig() FullCAConfig {
|
|
|
|
return FullCAConfig{
|
|
|
|
CertPath: caS.CertPath,
|
|
|
|
KeyPath: caS.KeyPath,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-13 09:39:45 +01:00
|
|
|
// 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)
|
|
|
|
}
|
2019-02-07 18:40:28 +00:00
|
|
|
k, err := pkcrypto.PrivateKeyFromPEM(kb)
|
2018-08-27 23:23:48 +01:00
|
|
|
if err != nil {
|
2019-02-07 18:40:28 +00:00
|
|
|
return nil, 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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-13 09:39:45 +01:00
|
|
|
// 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-18 11:55:55 +00:00
|
|
|
var (
|
2019-02-06 16:40:55 +00:00
|
|
|
keyData bytes.Buffer
|
|
|
|
writeErrs errs.Group
|
2018-12-18 11:55:55 +00:00
|
|
|
)
|
2019-02-06 16:40:55 +00:00
|
|
|
if err := fc.PeerConfig().Save(ca.PeerCA()); err != nil {
|
|
|
|
writeErrs.Add(err)
|
|
|
|
return writeErrs.Err()
|
2018-12-13 13:50:05 +00:00
|
|
|
}
|
|
|
|
|
2018-12-18 11:55:55 +00:00
|
|
|
if fc.KeyPath != "" {
|
2019-02-07 18:40:28 +00:00
|
|
|
if err := pkcrypto.WritePrivateKeyPEM(&keyData, ca.Key); err != nil {
|
2018-12-18 11:55:55 +00:00
|
|
|
writeErrs.Add(err)
|
2019-02-06 16:40:55 +00:00
|
|
|
return writeErrs.Err()
|
2018-12-18 11:55:55 +00:00
|
|
|
}
|
|
|
|
if err := writeKeyData(fc.KeyPath, keyData.Bytes()); err != nil {
|
|
|
|
writeErrs.Add(err)
|
2019-02-06 16:40:55 +00:00
|
|
|
return writeErrs.Err()
|
2018-12-18 11:55:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-06 16:40:55 +00:00
|
|
|
return writeErrs.Err()
|
2018-08-13 09:39:45 +01:00
|
|
|
}
|
|
|
|
|
2019-01-11 14:59:35 +00:00
|
|
|
// SaveBackup saves the certificate of the config wth a timestamped filename
|
|
|
|
func (fc FullCAConfig) SaveBackup(ca *FullCertificateAuthority) error {
|
|
|
|
return FullCAConfig{
|
|
|
|
CertPath: backupPath(fc.CertPath),
|
2019-02-06 16:40:55 +00:00
|
|
|
KeyPath: backupPath(fc.KeyPath),
|
2019-01-11 14:59:35 +00:00
|
|
|
}.Save(ca)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load loads a CA from the given configuration
|
|
|
|
func (pc PeerCAConfig) Load() (*PeerCertificateAuthority, error) {
|
|
|
|
chainPEM, err := ioutil.ReadFile(pc.CertPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, peertls.ErrNotExist.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2019-02-07 18:40:28 +00:00
|
|
|
chain, err := pkcrypto.CertsFromPEM(chainPEM)
|
2019-01-11 14:59:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errs.New("failed to load identity %#v: %v",
|
|
|
|
pc.CertPath, err)
|
|
|
|
}
|
|
|
|
|
2019-04-08 19:15:19 +01:00
|
|
|
// NB: `CAIndex` is in the context of a complete chain (incl. leaf).
|
|
|
|
// Here we're loading the CA chain (i.e. without leaf).
|
|
|
|
nodeID, err := NodeIDFromCert(chain[peertls.CAIndex-1])
|
2019-01-11 14:59:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &PeerCertificateAuthority{
|
|
|
|
// NB: `CAIndex` is in the context of a complete chain (incl. leaf).
|
2019-04-08 19:15:19 +01:00
|
|
|
// Here we're loading the CA chain (i.e. without leaf).
|
2019-01-11 14:59:35 +00:00
|
|
|
RestChain: chain[peertls.CAIndex:],
|
|
|
|
Cert: chain[peertls.CAIndex-1],
|
|
|
|
ID: nodeID,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-02-06 16:40:55 +00:00
|
|
|
// Save saves a peer CA (cert, no key) with the given configuration
|
|
|
|
func (pc PeerCAConfig) Save(ca *PeerCertificateAuthority) error {
|
|
|
|
var (
|
|
|
|
certData bytes.Buffer
|
|
|
|
writeErrs errs.Group
|
|
|
|
)
|
|
|
|
|
|
|
|
chain := []*x509.Certificate{ca.Cert}
|
|
|
|
chain = append(chain, ca.RestChain...)
|
|
|
|
|
|
|
|
if pc.CertPath != "" {
|
|
|
|
if err := peertls.WriteChain(&certData, chain...); err != nil {
|
|
|
|
writeErrs.Add(err)
|
|
|
|
return writeErrs.Err()
|
|
|
|
}
|
|
|
|
if err := writeChainData(pc.CertPath, certData.Bytes()); err != nil {
|
|
|
|
writeErrs.Add(err)
|
|
|
|
return writeErrs.Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SaveBackup saves the certificate of the config wth a timestamped filename
|
|
|
|
func (pc PeerCAConfig) SaveBackup(ca *PeerCertificateAuthority) error {
|
|
|
|
return PeerCAConfig{
|
|
|
|
CertPath: backupPath(pc.CertPath),
|
|
|
|
}.Save(ca)
|
|
|
|
}
|
|
|
|
|
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.
|
2019-04-03 16:03:53 +01:00
|
|
|
func (ca *FullCertificateAuthority) NewIdentity(exts ...pkix.Extension) (*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
|
|
|
|
}
|
2019-04-08 19:15:19 +01:00
|
|
|
// TODO: add test for this!
|
|
|
|
version, err := ca.Version()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
leafKey, err := version.NewPrivateKey()
|
2018-08-13 09:39:45 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-03 16:03:53 +01:00
|
|
|
|
|
|
|
if err := extensions.AddExtraExtension(leafTemplate, exts...); err != nil {
|
2018-08-13 09:39:45 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-04-04 00:21:32 +01:00
|
|
|
leafCert, err := peertls.CreateCertificate(pkcrypto.PublicKeyFromPrivate(leafKey), ca.Key, leafTemplate, ca.Cert)
|
2019-04-03 16:03:53 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-12-07 13:44:25 +00:00
|
|
|
}
|
|
|
|
|
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
|
2019-01-02 17:39:17 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-03-04 20:03:33 +00:00
|
|
|
// Chain returns the CA's certificate chain
|
|
|
|
func (ca *FullCertificateAuthority) Chain() []*x509.Certificate {
|
|
|
|
return append([]*x509.Certificate{ca.Cert}, ca.RestChain...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RawChain returns the CA's certificate chain as a 2d byte slice
|
|
|
|
func (ca *FullCertificateAuthority) RawChain() [][]byte {
|
|
|
|
chain := ca.Chain()
|
|
|
|
rawChain := make([][]byte, len(chain))
|
|
|
|
for i, cert := range chain {
|
|
|
|
rawChain[i] = cert.Raw
|
|
|
|
}
|
|
|
|
return rawChain
|
|
|
|
}
|
|
|
|
|
|
|
|
// RawRestChain returns the "rest" (excluding `ca.Cert`) of the certificate chain as a 2d byte slice
|
|
|
|
func (ca *FullCertificateAuthority) RawRestChain() [][]byte {
|
2019-01-02 17:39:17 +00:00
|
|
|
var chain [][]byte
|
|
|
|
for _, cert := range ca.RestChain {
|
|
|
|
chain = append(chain, cert.Raw)
|
|
|
|
}
|
|
|
|
return chain
|
|
|
|
}
|
|
|
|
|
2019-02-06 16:40:55 +00:00
|
|
|
// PeerCA converts a FullCertificateAuthority to a PeerCertificateAuthority
|
|
|
|
func (ca *FullCertificateAuthority) PeerCA() *PeerCertificateAuthority {
|
|
|
|
return &PeerCertificateAuthority{
|
|
|
|
Cert: ca.Cert,
|
|
|
|
ID: ca.ID,
|
|
|
|
RestChain: ca.RestChain,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-02 17:39:17 +00:00
|
|
|
// Sign signs the passed certificate with ca certificate
|
|
|
|
func (ca *FullCertificateAuthority) Sign(cert *x509.Certificate) (*x509.Certificate, error) {
|
2019-04-08 19:15:19 +01:00
|
|
|
signedCert, err := peertls.CreateCertificate(cert.PublicKey, ca.Key, cert, ca.Cert)
|
2019-01-02 17:39:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Wrap(err)
|
|
|
|
}
|
|
|
|
return signedCert, nil
|
2018-08-13 09:39:45 +01:00
|
|
|
}
|
2019-04-03 16:03:53 +01:00
|
|
|
|
2019-04-08 19:15:19 +01:00
|
|
|
// Version looks up the version based on the certificate's ID version extension.
|
|
|
|
func (ca *FullCertificateAuthority) Version() (storj.IDVersion, error) {
|
|
|
|
return storj.IDVersionFromCert(ca.Cert)
|
|
|
|
}
|
|
|
|
|
2019-04-03 16:03:53 +01:00
|
|
|
// AddExtension adds extensions to certificate authority certificate. Extensions
|
|
|
|
// are serialized into the certificate's raw bytes and it is re-signed by itself.
|
2019-04-08 19:15:19 +01:00
|
|
|
func (ca *FullCertificateAuthority) AddExtension(exts ...pkix.Extension) error {
|
2019-04-03 16:03:53 +01:00
|
|
|
// TODO: how to properly handle this?
|
|
|
|
if len(ca.RestChain) > 0 {
|
|
|
|
return errs.New("adding extensions requires parent certificate's private key")
|
|
|
|
}
|
|
|
|
|
2019-04-08 19:15:19 +01:00
|
|
|
if err := extensions.AddExtraExtension(ca.Cert, exts...); err != nil {
|
2019-04-03 16:03:53 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-04 00:21:32 +01:00
|
|
|
updatedCert, err := peertls.CreateSelfSignedCertificate(ca.Key, ca.Cert)
|
2019-04-03 16:03:53 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ca.Cert = updatedCert
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Revoke extends the certificate authority certificate with a certificate revocation extension.
|
|
|
|
func (ca *FullCertificateAuthority) Revoke() error {
|
|
|
|
ext, err := extensions.NewRevocationExt(ca.Key, ca.Cert)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ca.AddExtension(ext)
|
|
|
|
}
|