transport.go 3.89 KB
Newer Older
Marten Seemann's avatar
Marten Seemann committed
1 2 3 4 5 6
package libp2ptls

import (
	"context"
	"crypto/tls"
	"net"
Marten Seemann's avatar
Marten Seemann committed
7
	"os"
8
	"sync"
Marten Seemann's avatar
Marten Seemann committed
9 10 11

	cs "github.com/libp2p/go-conn-security"
	ci "github.com/libp2p/go-libp2p-crypto"
12
	ic "github.com/libp2p/go-libp2p-crypto"
Marten Seemann's avatar
Marten Seemann committed
13 14 15
	peer "github.com/libp2p/go-libp2p-peer"
)

Marten Seemann's avatar
Marten Seemann committed
16 17 18 19 20 21
// TLS 1.3 is opt-in in Go 1.12
// Activate it by setting the tls13 GODEBUG flag.
func init() {
	os.Setenv("GODEBUG", os.Getenv("GODEBUG")+",tls13=1")
}

Marten Seemann's avatar
Marten Seemann committed
22 23 24 25 26 27 28 29 30
// ID is the protocol ID (used when negotiating with multistream)
const ID = "/tls/1.0.0"

// Transport constructs secure communication sessions for a peer.
type Transport struct {
	identity *Identity

	localPeer peer.ID
	privKey   ci.PrivKey
31 32 33

	incomingMutex sync.Mutex
	incoming      map[net.Conn]ic.PubKey
Marten Seemann's avatar
Marten Seemann committed
34 35 36 37 38 39 40 41
}

// New creates a TLS encrypted transport
func New(key ci.PrivKey) (*Transport, error) {
	id, err := peer.IDFromPrivateKey(key)
	if err != nil {
		return nil, err
	}
42 43 44 45 46 47
	t := &Transport{
		localPeer: id,
		privKey:   key,
		incoming:  make(map[net.Conn]ic.PubKey),
	}
	identity, err := NewIdentity(key, t.verifiedPeer)
Marten Seemann's avatar
Marten Seemann committed
48 49 50
	if err != nil {
		return nil, err
	}
51 52
	t.identity = identity
	return t, nil
Marten Seemann's avatar
Marten Seemann committed
53 54 55 56 57 58
}

var _ cs.Transport = &Transport{}

// SecureInbound runs the TLS handshake as a server.
func (t *Transport) SecureInbound(ctx context.Context, insecure net.Conn) (cs.Conn, error) {
59 60 61 62 63 64 65
	defer func() {
		t.incomingMutex.Lock()
		// only contains this connection if we successfully derived the client's key
		delete(t.incoming, insecure)
		t.incomingMutex.Unlock()
	}()

Marten Seemann's avatar
Marten Seemann committed
66
	serv := tls.Server(insecure, t.identity.Config)
67
	return t.handshake(ctx, insecure, serv)
Marten Seemann's avatar
Marten Seemann committed
68 69 70
}

// SecureOutbound runs the TLS handshake as a client.
Marten Seemann's avatar
Marten Seemann committed
71 72 73 74 75 76
// Note that SecureOutbound will not return an error if the server doesn't
// accept the certificate. This is due to the fact that in TLS 1.3, the client
// sends its certificate and the ClientFinished in the same flight, and can send
// application data immediately afterwards.
// If the handshake fails, the server will close the connection. The client will
// notice this after 1 RTT when calling Read.
Marten Seemann's avatar
Marten Seemann committed
77 78
func (t *Transport) SecureOutbound(ctx context.Context, insecure net.Conn, p peer.ID) (cs.Conn, error) {
	cl := tls.Client(insecure, t.identity.ConfigForPeer(p))
79
	return t.handshake(ctx, insecure, cl)
80
}
81

82 83 84 85
func (t *Transport) handshake(
	ctx context.Context,
	insecure net.Conn,
	tlsConn *tls.Conn,
86
) (cs.Conn, error) {
87 88 89
	// There's no way to pass a context to tls.Conn.Handshake().
	// See https://github.com/golang/go/issues/18482.
	// Close the connection instead.
90 91 92 93 94
	select {
	case <-ctx.Done():
		tlsConn.Close()
	default:
	}
95 96 97 98 99 100
	done := make(chan struct{})
	defer close(done)
	go func() {
		select {
		case <-done:
		case <-ctx.Done():
Marten Seemann's avatar
Marten Seemann committed
101
			tlsConn.Close()
102 103 104
		}
	}()

105 106
	if err := tlsConn.Handshake(); err != nil {
		// if the context was canceled, return the context error
107 108 109 110
		if ctxErr := ctx.Err(); ctxErr != nil {
			return nil, ctxErr
		}
		return nil, err
111
	}
112
	conn, err := t.setupConn(insecure, tlsConn)
113 114
	if err != nil {
		// if the context was canceled, return the context error
115 116 117 118
		if ctxErr := ctx.Err(); ctxErr != nil {
			return nil, ctxErr
		}
		return nil, err
Marten Seemann's avatar
Marten Seemann committed
119
	}
120
	return conn, nil
Marten Seemann's avatar
Marten Seemann committed
121 122
}

123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
func (t *Transport) verifiedPeer(conn net.Conn, pubKey ic.PubKey) {
	t.incomingMutex.Lock()
	t.incoming[conn] = pubKey
	t.incomingMutex.Unlock()
}

func (t *Transport) setupConn(insecure net.Conn, tlsConn *tls.Conn) (cs.Conn, error) {
	t.incomingMutex.Lock()
	remotePubKey := t.incoming[insecure]
	t.incomingMutex.Unlock()

	// This case only occurs for the client.
	// Servers already determined the client's key in the VerifyPeerCertificate callback.
	if remotePubKey == nil {
		var err error
		remotePubKey, err = KeyFromChain(tlsConn.ConnectionState().PeerCertificates)
		if err != nil {
			return nil, err
		}
Marten Seemann's avatar
Marten Seemann committed
142 143 144 145 146 147 148 149 150 151 152 153 154
	}
	remotePeerID, err := peer.IDFromPublicKey(remotePubKey)
	if err != nil {
		return nil, err
	}
	return &conn{
		Conn:         tlsConn,
		localPeer:    t.localPeer,
		privKey:      t.privKey,
		remotePeer:   remotePeerID,
		remotePubKey: remotePubKey,
	}, nil
}