2019-02-11 11:17:32 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package tlsopts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/credentials"
|
|
|
|
|
|
|
|
"storj.io/storj/pkg/identity"
|
|
|
|
"storj.io/storj/pkg/peertls"
|
|
|
|
"storj.io/storj/pkg/storj"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ServerOption returns a grpc `ServerOption` for incoming connections
|
2019-02-26 18:35:16 +00:00
|
|
|
// to the node with this full identity.
|
2019-02-11 11:17:32 +00:00
|
|
|
func (opts *Options) ServerOption() grpc.ServerOption {
|
2019-03-04 20:40:18 +00:00
|
|
|
tlsConfig := opts.ServerTLSConfig()
|
2019-02-11 11:17:32 +00:00
|
|
|
return grpc.Creds(credentials.NewTLS(tlsConfig))
|
|
|
|
}
|
|
|
|
|
|
|
|
// DialOption returns a grpc `DialOption` for making outgoing connections
|
2019-02-26 18:35:16 +00:00
|
|
|
// to the node with this peer identity.
|
2019-03-04 20:03:33 +00:00
|
|
|
func (opts *Options) DialOption(id storj.NodeID) (grpc.DialOption, error) {
|
|
|
|
if id.IsZero() {
|
|
|
|
return nil, Error.New("no ID specified for DialOption")
|
|
|
|
}
|
2019-03-04 20:40:18 +00:00
|
|
|
tlsConfig := opts.ClientTLSConfig(id)
|
|
|
|
return grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig)), nil
|
2019-03-04 20:03:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DialUnverifiedIDOption returns a grpc `DialUnverifiedIDOption`
|
|
|
|
func (opts *Options) DialUnverifiedIDOption() grpc.DialOption {
|
2019-03-04 20:40:18 +00:00
|
|
|
tlsConfig := opts.tlsConfig(false)
|
|
|
|
return grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig))
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServerTLSConfig returns a TSLConfig for use as a server in handshaking with a peer.
|
|
|
|
func (opts *Options) ServerTLSConfig() *tls.Config {
|
|
|
|
return opts.tlsConfig(true)
|
2019-02-26 18:35:16 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 20:40:18 +00:00
|
|
|
// ClientTLSConfig returns a TSLConfig for use as a client in handshaking with a peer.
|
|
|
|
func (opts *Options) ClientTLSConfig(id storj.NodeID) *tls.Config {
|
|
|
|
return opts.tlsConfig(false, verifyIdentity(id))
|
2019-02-26 18:35:16 +00:00
|
|
|
}
|
|
|
|
|
2019-09-19 04:34:19 +01:00
|
|
|
// UnverifiedClientTLSConfig returns a TLSConfig for use as a client in handshaking with
|
|
|
|
// an unknown peer.
|
|
|
|
func (opts *Options) UnverifiedClientTLSConfig() *tls.Config {
|
|
|
|
return opts.tlsConfig(false)
|
|
|
|
}
|
|
|
|
|
2019-03-04 20:40:18 +00:00
|
|
|
func (opts *Options) tlsConfig(isServer bool, verificationFuncs ...peertls.PeerCertVerificationFunc) *tls.Config {
|
|
|
|
verificationFuncs = append(
|
2019-02-11 11:17:32 +00:00
|
|
|
[]peertls.PeerCertVerificationFunc{
|
|
|
|
peertls.VerifyPeerCertChains,
|
|
|
|
},
|
2019-03-04 20:40:18 +00:00
|
|
|
verificationFuncs...,
|
2019-02-11 11:17:32 +00:00
|
|
|
)
|
2019-03-06 14:42:34 +00:00
|
|
|
|
|
|
|
switch isServer {
|
|
|
|
case true:
|
|
|
|
verificationFuncs = append(
|
|
|
|
verificationFuncs,
|
|
|
|
opts.VerificationFuncs.server...,
|
|
|
|
)
|
|
|
|
case false:
|
|
|
|
verificationFuncs = append(
|
|
|
|
verificationFuncs,
|
|
|
|
opts.VerificationFuncs.client...,
|
|
|
|
)
|
|
|
|
}
|
2019-03-04 20:40:18 +00:00
|
|
|
|
|
|
|
config := &tls.Config{
|
2019-02-11 11:17:32 +00:00
|
|
|
Certificates: []tls.Certificate{*opts.Cert},
|
|
|
|
InsecureSkipVerify: true,
|
2019-09-09 21:09:01 +01:00
|
|
|
MinVersion: tls.VersionTLS12,
|
2019-02-11 11:17:32 +00:00
|
|
|
VerifyPeerCertificate: peertls.VerifyPeerFunc(
|
2019-03-04 20:40:18 +00:00
|
|
|
verificationFuncs...,
|
2019-02-11 11:17:32 +00:00
|
|
|
),
|
|
|
|
}
|
2019-03-04 20:40:18 +00:00
|
|
|
|
|
|
|
if isServer {
|
|
|
|
config.ClientAuth = tls.RequireAnyClientCert
|
|
|
|
}
|
|
|
|
|
|
|
|
return config
|
2019-02-11 11:17:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func verifyIdentity(id storj.NodeID) peertls.PeerCertVerificationFunc {
|
|
|
|
return func(_ [][]byte, parsedChains [][]*x509.Certificate) (err error) {
|
|
|
|
defer mon.TaskNamed("verifyIdentity")(nil)(&err)
|
2019-04-08 19:15:19 +01:00
|
|
|
peer, err := identity.PeerIdentityFromChain(parsedChains[0])
|
2019-02-11 11:17:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if peer.ID.String() != id.String() {
|
|
|
|
return Error.New("peer ID did not match requested ID")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|