2018-08-13 09:39:45 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2019-01-02 10:23:25 +00:00
|
|
|
package identity
|
2018-08-13 09:39:45 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto"
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/x509"
|
2018-12-18 11:55:55 +00:00
|
|
|
"crypto/x509/pkix"
|
|
|
|
"encoding/asn1"
|
2018-08-13 09:39:45 +01:00
|
|
|
"encoding/pem"
|
2018-12-13 13:50:05 +00:00
|
|
|
"io/ioutil"
|
2019-01-02 14:41:01 +00:00
|
|
|
"log"
|
2018-08-13 09:39:45 +01:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2019-01-02 14:41:01 +00:00
|
|
|
"sync/atomic"
|
2018-08-13 09:39:45 +01:00
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
2018-11-29 18:39:27 +00:00
|
|
|
|
2018-08-13 09:39:45 +01:00
|
|
|
"storj.io/storj/pkg/peertls"
|
2018-11-30 13:40:13 +00:00
|
|
|
"storj.io/storj/pkg/storj"
|
2018-12-18 11:55:55 +00:00
|
|
|
"storj.io/storj/pkg/utils"
|
2018-08-13 09:39:45 +01:00
|
|
|
)
|
|
|
|
|
2018-08-27 18:28:16 +01:00
|
|
|
// TLSFilesStatus is the status of keys
|
|
|
|
type TLSFilesStatus int
|
2018-08-13 09:39:45 +01:00
|
|
|
|
2018-08-27 18:28:16 +01:00
|
|
|
// Four possible outcomes for four files
|
2018-08-13 09:39:45 +01:00
|
|
|
const (
|
2018-08-27 18:28:16 +01:00
|
|
|
NoCertNoKey = TLSFilesStatus(iota)
|
2018-08-13 09:39:45 +01:00
|
|
|
CertNoKey
|
|
|
|
NoCertKey
|
|
|
|
CertKey
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-12-18 11:55:55 +00:00
|
|
|
// ErrChainLength is used when the length of a cert chain isn't what was expected
|
|
|
|
ErrChainLength = errs.Class("cert chain length error")
|
2018-08-27 18:28:16 +01:00
|
|
|
// ErrZeroBytes is returned for zero slice
|
2018-08-13 09:39:45 +01:00
|
|
|
ErrZeroBytes = errs.New("byte slice was unexpectedly empty")
|
|
|
|
)
|
|
|
|
|
2018-12-18 11:55:55 +00:00
|
|
|
type encodedChain struct {
|
|
|
|
chain [][]byte
|
|
|
|
extensions [][][]byte
|
|
|
|
}
|
|
|
|
|
2019-01-02 10:23:25 +00:00
|
|
|
// DecodeAndParseChainPEM parses a PEM chain
|
|
|
|
func DecodeAndParseChainPEM(PEMBytes []byte) ([]*x509.Certificate, error) {
|
2018-12-18 11:55:55 +00:00
|
|
|
var (
|
|
|
|
encChain encodedChain
|
|
|
|
blockErrs utils.ErrorGroup
|
|
|
|
)
|
|
|
|
for {
|
|
|
|
var pemBlock *pem.Block
|
|
|
|
pemBlock, PEMBytes = pem.Decode(PEMBytes)
|
|
|
|
if pemBlock == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
switch pemBlock.Type {
|
|
|
|
case peertls.BlockTypeCertificate:
|
|
|
|
encChain.AddCert(pemBlock.Bytes)
|
|
|
|
case peertls.BlockTypeExtension:
|
|
|
|
if err := encChain.AddExtension(pemBlock.Bytes); err != nil {
|
|
|
|
blockErrs.Add(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := blockErrs.Finish(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return encChain.Parse()
|
|
|
|
}
|
|
|
|
|
2018-08-13 09:39:45 +01:00
|
|
|
func decodePEM(PEMBytes []byte) ([][]byte, error) {
|
2018-12-18 11:55:55 +00:00
|
|
|
var DERBytes [][]byte
|
2018-08-13 09:39:45 +01:00
|
|
|
|
|
|
|
for {
|
|
|
|
var DERBlock *pem.Block
|
|
|
|
|
|
|
|
DERBlock, PEMBytes = pem.Decode(PEMBytes)
|
|
|
|
if DERBlock == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
DERBytes = append(DERBytes, DERBlock.Bytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(DERBytes) == 0 || len(DERBytes[0]) == 0 {
|
|
|
|
return nil, ErrZeroBytes
|
|
|
|
}
|
|
|
|
|
|
|
|
return DERBytes, nil
|
|
|
|
}
|
|
|
|
|
2019-01-02 14:41:01 +00:00
|
|
|
func newCAWorker(ctx context.Context, workerid int, highscore *uint32, difficulty uint16, parentCert *x509.Certificate, parentKey crypto.PrivateKey, caC chan FullCertificateAuthority, eC chan error) {
|
2018-08-13 09:39:45 +01:00
|
|
|
var (
|
|
|
|
k crypto.PrivateKey
|
2018-11-29 18:39:27 +00:00
|
|
|
i storj.NodeID
|
2018-08-13 09:39:45 +01:00
|
|
|
err error
|
|
|
|
)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2019-01-02 14:41:01 +00:00
|
|
|
eC <- ctx.Err()
|
2018-08-13 09:39:45 +01:00
|
|
|
return
|
|
|
|
default:
|
|
|
|
k, err = peertls.NewKey()
|
2018-08-27 18:28:16 +01:00
|
|
|
if err != nil {
|
|
|
|
eC <- err
|
|
|
|
return
|
|
|
|
}
|
2018-08-13 09:39:45 +01:00
|
|
|
switch kE := k.(type) {
|
|
|
|
case *ecdsa.PrivateKey:
|
2018-11-29 18:39:27 +00:00
|
|
|
i, err = NodeIDFromKey(&kE.PublicKey)
|
2018-08-13 09:39:45 +01:00
|
|
|
if err != nil {
|
|
|
|
eC <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
eC <- peertls.ErrUnsupportedKey.New("%T", k)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
d, err := i.Difficulty()
|
|
|
|
if err != nil {
|
|
|
|
eC <- err
|
|
|
|
continue
|
|
|
|
}
|
2019-01-02 14:41:01 +00:00
|
|
|
|
|
|
|
hs := atomic.LoadUint32(highscore)
|
|
|
|
if uint32(d) > hs {
|
|
|
|
atomic.CompareAndSwapUint32(highscore, hs, uint32(d))
|
|
|
|
log.Printf("Found a certificate matching difficulty of %d\n", hs)
|
|
|
|
}
|
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
if d >= difficulty {
|
2019-01-02 14:41:01 +00:00
|
|
|
log.Printf("Found a certificate matching difficulty of %d\n", d)
|
2018-08-13 09:39:45 +01:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ct, err := peertls.CATemplate()
|
|
|
|
if err != nil {
|
|
|
|
eC <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-12-07 13:44:25 +00:00
|
|
|
c, err := peertls.NewCert(k, parentKey, ct, parentCert)
|
2018-08-13 09:39:45 +01:00
|
|
|
if err != nil {
|
|
|
|
eC <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ca := FullCertificateAuthority{
|
|
|
|
Cert: c,
|
|
|
|
Key: k,
|
|
|
|
ID: i,
|
|
|
|
}
|
2018-11-01 15:48:43 +00:00
|
|
|
if parentCert != nil {
|
|
|
|
ca.RestChain = []*x509.Certificate{parentCert}
|
|
|
|
}
|
2018-08-13 09:39:45 +01:00
|
|
|
caC <- ca
|
|
|
|
}
|
|
|
|
|
2018-12-18 11:55:55 +00:00
|
|
|
// writeChainData writes data to path ensuring permissions are appropriate for a cert
|
|
|
|
func writeChainData(path string, data []byte) error {
|
2018-12-13 13:50:05 +00:00
|
|
|
err := writeFile(path, 0744, 0644, data)
|
|
|
|
if err != nil {
|
|
|
|
return errs.New("unable to write certificate to \"%s\": %v", path, err)
|
2018-08-13 09:39:45 +01:00
|
|
|
}
|
2018-12-13 13:50:05 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-08-13 09:39:45 +01:00
|
|
|
|
2018-12-13 13:50:05 +00:00
|
|
|
// writeKeyData writes data to path ensuring permissions are appropriate for a cert
|
|
|
|
func writeKeyData(path string, data []byte) error {
|
|
|
|
err := writeFile(path, 0700, 0600, data)
|
2018-08-13 09:39:45 +01:00
|
|
|
if err != nil {
|
2018-12-13 13:50:05 +00:00
|
|
|
return errs.New("unable to write key to \"%s\": %v", path, err)
|
2018-08-13 09:39:45 +01:00
|
|
|
}
|
2018-12-13 13:50:05 +00:00
|
|
|
return nil
|
2018-08-13 09:39:45 +01:00
|
|
|
}
|
|
|
|
|
2018-12-13 13:50:05 +00:00
|
|
|
// writeFile writes to path, creating directories and files with the necessary permissions
|
|
|
|
func writeFile(path string, dirmode, filemode os.FileMode, data []byte) error {
|
|
|
|
if err := os.MkdirAll(filepath.Dir(path), dirmode); err != nil {
|
|
|
|
return errs.Wrap(err)
|
2018-08-13 09:39:45 +01:00
|
|
|
}
|
|
|
|
|
2018-12-13 13:50:05 +00:00
|
|
|
if err := ioutil.WriteFile(path, data, filemode); err != nil {
|
|
|
|
return errs.Wrap(err)
|
2018-08-13 09:39:45 +01:00
|
|
|
}
|
2018-12-13 13:50:05 +00:00
|
|
|
|
|
|
|
return nil
|
2018-08-13 09:39:45 +01:00
|
|
|
}
|
|
|
|
|
2018-08-27 18:28:16 +01:00
|
|
|
func statTLSFiles(certPath, keyPath string) TLSFilesStatus {
|
2018-08-13 09:39:45 +01:00
|
|
|
_, err := os.Stat(certPath)
|
2018-08-27 18:28:16 +01:00
|
|
|
hasCert := os.IsExist(err)
|
|
|
|
|
2018-08-13 09:39:45 +01:00
|
|
|
_, err = os.Stat(keyPath)
|
2018-08-27 18:28:16 +01:00
|
|
|
hasKey := os.IsExist(err)
|
|
|
|
|
|
|
|
if hasCert && hasKey {
|
|
|
|
return CertKey
|
|
|
|
} else if hasCert {
|
|
|
|
return CertNoKey
|
|
|
|
} else if hasKey {
|
|
|
|
return NoCertKey
|
2018-08-13 09:39:45 +01:00
|
|
|
}
|
2018-08-27 18:28:16 +01:00
|
|
|
|
|
|
|
return NoCertNoKey
|
2018-08-13 09:39:45 +01:00
|
|
|
}
|
|
|
|
|
2018-08-27 18:28:16 +01:00
|
|
|
func (t TLSFilesStatus) String() string {
|
2018-08-13 09:39:45 +01:00
|
|
|
switch t {
|
|
|
|
case CertKey:
|
|
|
|
return "certificate and key"
|
|
|
|
case CertNoKey:
|
|
|
|
return "certificate"
|
|
|
|
case NoCertKey:
|
|
|
|
return "key"
|
|
|
|
}
|
2018-08-27 18:28:16 +01:00
|
|
|
return ""
|
2018-08-13 09:39:45 +01:00
|
|
|
}
|
2018-12-18 11:55:55 +00:00
|
|
|
|
|
|
|
func (e *encodedChain) AddCert(b []byte) {
|
|
|
|
e.chain = append(e.chain, b)
|
|
|
|
e.extensions = append(e.extensions, [][]byte{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *encodedChain) AddExtension(b []byte) error {
|
|
|
|
chainLen := len(e.chain)
|
|
|
|
if chainLen < 1 {
|
|
|
|
return ErrChainLength.New("expected: >= 1; actual: %d", chainLen)
|
|
|
|
}
|
|
|
|
|
|
|
|
i := chainLen - 1
|
|
|
|
e.extensions[i] = append(e.extensions[i], b)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *encodedChain) Parse() ([]*x509.Certificate, error) {
|
|
|
|
chain, err := ParseCertChain(e.chain)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var extErrs utils.ErrorGroup
|
|
|
|
for i, cert := range chain {
|
|
|
|
for _, ee := range e.extensions[i] {
|
|
|
|
ext := pkix.Extension{}
|
|
|
|
_, err := asn1.Unmarshal(ee, &ext)
|
|
|
|
if err != nil {
|
|
|
|
extErrs.Add(err)
|
|
|
|
}
|
|
|
|
cert.ExtraExtensions = append(cert.ExtraExtensions, ext)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := extErrs.Finish(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return chain, nil
|
|
|
|
}
|