2019-01-02 10:23:25 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2019-02-11 11:17:32 +00:00
|
|
|
package tlsopts
|
2019-01-02 10:23:25 +00:00
|
|
|
|
|
|
|
import (
|
2019-02-11 11:17:32 +00:00
|
|
|
"crypto/tls"
|
2019-01-02 10:23:25 +00:00
|
|
|
"io/ioutil"
|
|
|
|
|
2019-02-11 11:17:32 +00:00
|
|
|
"github.com/zeebo/errs"
|
|
|
|
monkit "gopkg.in/spacemonkeygo/monkit.v2"
|
2019-01-02 10:23:25 +00:00
|
|
|
|
|
|
|
"storj.io/storj/pkg/identity"
|
|
|
|
"storj.io/storj/pkg/peertls"
|
2019-02-07 18:40:28 +00:00
|
|
|
"storj.io/storj/pkg/pkcrypto"
|
2019-01-02 10:23:25 +00:00
|
|
|
)
|
|
|
|
|
2019-02-11 11:17:32 +00:00
|
|
|
var (
|
|
|
|
mon = monkit.Package()
|
|
|
|
// Error is error for tlsopts
|
|
|
|
Error = errs.Class("tlsopts error")
|
|
|
|
)
|
|
|
|
|
|
|
|
// Options holds config, identity, and peer verification function data for use with tls.
|
2019-01-02 10:23:25 +00:00
|
|
|
type Options struct {
|
2019-03-04 20:40:18 +00:00
|
|
|
Config Config
|
|
|
|
Ident *identity.FullIdentity
|
|
|
|
RevDB *identity.RevocationDB
|
2019-03-06 14:42:34 +00:00
|
|
|
VerificationFuncs *VerificationFuncs
|
2019-03-04 20:40:18 +00:00
|
|
|
Cert *tls.Certificate
|
2019-01-02 10:23:25 +00:00
|
|
|
}
|
|
|
|
|
2019-03-06 14:42:34 +00:00
|
|
|
// VerificationFuncs keeps track of of client and server peer certificate verification
|
|
|
|
// functions for use in tls handshakes.
|
|
|
|
type VerificationFuncs struct {
|
|
|
|
client []peertls.PeerCertVerificationFunc
|
|
|
|
server []peertls.PeerCertVerificationFunc
|
|
|
|
}
|
|
|
|
|
2019-02-11 11:17:32 +00:00
|
|
|
// NewOptions is a constructor for `tls options` given an identity and config
|
2019-01-02 10:23:25 +00:00
|
|
|
func NewOptions(i *identity.FullIdentity, c Config) (*Options, error) {
|
|
|
|
opts := &Options{
|
2019-03-06 14:42:34 +00:00
|
|
|
Config: c,
|
|
|
|
Ident: i,
|
|
|
|
VerificationFuncs: new(VerificationFuncs),
|
2019-01-02 10:23:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err := opts.configure(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return opts, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// configure adds peer certificate verification functions and revocation
|
|
|
|
// database to the config.
|
|
|
|
func (opts *Options) configure(c Config) (err error) {
|
|
|
|
parseOpts := peertls.ParseExtOptions{}
|
|
|
|
|
2019-01-17 17:36:45 +00:00
|
|
|
if c.UsePeerCAWhitelist {
|
|
|
|
whitelist := []byte(DefaultPeerCAWhitelist)
|
|
|
|
if c.PeerCAWhitelistPath != "" {
|
|
|
|
whitelist, err = ioutil.ReadFile(c.PeerCAWhitelistPath)
|
|
|
|
if err != nil {
|
|
|
|
return Error.New("unable to find whitelist file %v: %v", c.PeerCAWhitelistPath, err)
|
|
|
|
}
|
|
|
|
}
|
2019-02-07 18:40:28 +00:00
|
|
|
parsed, err := pkcrypto.CertsFromPEM(whitelist)
|
2019-01-02 10:23:25 +00:00
|
|
|
if err != nil {
|
2019-01-17 17:36:45 +00:00
|
|
|
return Error.Wrap(err)
|
2019-01-02 10:23:25 +00:00
|
|
|
}
|
2019-01-17 17:36:45 +00:00
|
|
|
parseOpts.CAWhitelist = parsed
|
2019-03-06 14:42:34 +00:00
|
|
|
opts.VerificationFuncs.ClientAdd(peertls.VerifyCAWhitelist(parsed))
|
2019-01-02 10:23:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.Extensions.Revocation {
|
2019-02-06 16:40:55 +00:00
|
|
|
opts.RevDB, err = identity.NewRevDB(c.RevocationDBURL)
|
2019-01-02 10:23:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-03-06 14:42:34 +00:00
|
|
|
opts.VerificationFuncs.Add(identity.VerifyUnrevokedChainFunc(opts.RevDB))
|
2019-01-02 10:23:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
exts := peertls.ParseExtensions(c.Extensions, parseOpts)
|
2019-03-06 14:42:34 +00:00
|
|
|
opts.VerificationFuncs.Add(exts.VerifyFunc())
|
|
|
|
|
|
|
|
opts.Cert, err = peertls.TLSCert(opts.Ident.RawChain(), opts.Ident.Leaf, opts.Ident.Key)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Client returns the client verification functions.
|
|
|
|
func (vf *VerificationFuncs) Client() []peertls.PeerCertVerificationFunc {
|
|
|
|
return vf.client
|
|
|
|
}
|
|
|
|
|
|
|
|
// Server returns the server verification functions.
|
|
|
|
func (vf *VerificationFuncs) Server() []peertls.PeerCertVerificationFunc {
|
|
|
|
return vf.server
|
|
|
|
}
|
2019-01-02 10:23:25 +00:00
|
|
|
|
2019-03-06 14:42:34 +00:00
|
|
|
// Add adds verification functions so the client and server lists.
|
|
|
|
func (vf *VerificationFuncs) Add(verificationFuncs ...peertls.PeerCertVerificationFunc) {
|
|
|
|
vf.ClientAdd(verificationFuncs...)
|
|
|
|
vf.ServerAdd(verificationFuncs...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ClientAdd adds verification functions so the client list.
|
|
|
|
func (vf *VerificationFuncs) ClientAdd(verificationFuncs ...peertls.PeerCertVerificationFunc) {
|
|
|
|
verificationFuncs = removeNils(verificationFuncs)
|
|
|
|
vf.client = append(vf.client, verificationFuncs...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServerAdd adds verification functions so the server list.
|
|
|
|
func (vf *VerificationFuncs) ServerAdd(verificationFuncs ...peertls.PeerCertVerificationFunc) {
|
|
|
|
verificationFuncs = removeNils(verificationFuncs)
|
|
|
|
vf.server = append(vf.server, verificationFuncs...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeNils(verificationFuncs []peertls.PeerCertVerificationFunc) []peertls.PeerCertVerificationFunc {
|
|
|
|
for i, f := range verificationFuncs {
|
2019-01-02 10:23:25 +00:00
|
|
|
if f == nil {
|
2019-03-06 14:42:34 +00:00
|
|
|
copy(verificationFuncs[i:], verificationFuncs[i+1:])
|
|
|
|
verificationFuncs = verificationFuncs[:len(verificationFuncs)-1]
|
2019-01-02 10:23:25 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-06 14:42:34 +00:00
|
|
|
return verificationFuncs
|
2019-01-02 10:23:25 +00:00
|
|
|
}
|