Skip to content

Commit

Permalink
set a static QUIC resumption token generator key
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Sep 27, 2023
1 parent 31228a8 commit d4e1e8e
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 27 deletions.
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ func (cfg *Config) addTransports(h host.Host) error {
}

fxopts = append(fxopts, fx.Provide(PrivKeyToStatelessResetKey))
fxopts = append(fxopts, fx.Provide(PrivKeyToTokenGeneratorKey))
if cfg.QUICReuse != nil {
fxopts = append(fxopts, cfg.QUICReuse...)
} else {
Expand Down
18 changes: 17 additions & 1 deletion config/quic_stateless_reset.go → config/quic.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import (
"github.com/quic-go/quic-go"
)

const statelessResetKeyInfo = "libp2p quic stateless reset key"
const (
statelessResetKeyInfo = "libp2p quic stateless reset key"
tokenGeneratorKeyInfo = "libp2p quic token generator key"
)

func PrivKeyToStatelessResetKey(key crypto.PrivKey) (quic.StatelessResetKey, error) {
var statelessResetKey quic.StatelessResetKey
Expand All @@ -25,3 +28,16 @@ func PrivKeyToStatelessResetKey(key crypto.PrivKey) (quic.StatelessResetKey, err
}
return statelessResetKey, nil
}

func PrivKeyToTokenGeneratorKey(key crypto.PrivKey) (quic.TokenGeneratorKey, error) {
var tokenKey quic.TokenGeneratorKey
keyBytes, err := key.Raw()
if err != nil {
return tokenKey, err
}
keyReader := hkdf.New(sha256.New, keyBytes, nil, []byte(tokenGeneratorKeyInfo))
if _, err := io.ReadFull(keyReader, tokenKey[:]); err != nil {
return tokenKey, err
}
return tokenKey, nil
}
9 changes: 5 additions & 4 deletions p2p/net/swarm/dial_worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ import (
"github.com/libp2p/go-libp2p/p2p/host/peerstore/pstoremem"
"github.com/libp2p/go-libp2p/p2p/muxer/yamux"
tptu "github.com/libp2p/go-libp2p/p2p/net/upgrader"
quic "github.com/libp2p/go-libp2p/p2p/transport/quic"
libp2pquic "github.com/libp2p/go-libp2p/p2p/transport/quic"
"github.com/libp2p/go-libp2p/p2p/transport/quicreuse"
"github.com/libp2p/go-libp2p/p2p/transport/tcp"

ma "github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr/net"
"github.com/quic-go/quic-go"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -88,11 +89,11 @@ func makeSwarmWithNoListenAddrs(t *testing.T, opts ...Option) *Swarm {
if err := s.AddTransport(tcpTransport); err != nil {
t.Fatal(err)
}
reuse, err := quicreuse.NewConnManager([32]byte{})
reuse, err := quicreuse.NewConnManager(quic.StatelessResetKey{}, quic.TokenGeneratorKey{})
if err != nil {
t.Fatal(err)
}
quicTransport, err := quic.NewTransport(priv, reuse, nil, nil, nil)
quicTransport, err := libp2pquic.NewTransport(priv, reuse, nil, nil, nil)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -968,7 +969,7 @@ func TestDialWorkerLoopHolePunching(t *testing.T) {
for i := 0; i < len(addrs); i++ {
delay := 10 * time.Second
if addrs[i].Equal(t1) {
//fire t1 immediately
// fire t1 immediately
delay = 0
} else if addrs[i].Equal(t2) {
// delay t2 by 100ms
Expand Down
7 changes: 4 additions & 3 deletions p2p/net/swarm/swarm_addr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ import (
"github.com/libp2p/go-libp2p/p2p/net/swarm"
swarmt "github.com/libp2p/go-libp2p/p2p/net/swarm/testing"
circuitv2 "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/client"
quic "github.com/libp2p/go-libp2p/p2p/transport/quic"
libp2pquic "github.com/libp2p/go-libp2p/p2p/transport/quic"
"github.com/libp2p/go-libp2p/p2p/transport/quicreuse"
"github.com/libp2p/go-libp2p/p2p/transport/tcp"
webtransport "github.com/libp2p/go-libp2p/p2p/transport/webtransport"

ma "github.com/multiformats/go-multiaddr"
"github.com/multiformats/go-multibase"
"github.com/multiformats/go-multihash"
"github.com/quic-go/quic-go"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -81,10 +82,10 @@ func TestDialAddressSelection(t *testing.T) {
tcpTr, err := tcp.NewTCPTransport(nil, nil)
require.NoError(t, err)
require.NoError(t, s.AddTransport(tcpTr))
reuse, err := quicreuse.NewConnManager([32]byte{})
reuse, err := quicreuse.NewConnManager(quic.StatelessResetKey{}, quic.TokenGeneratorKey{})
require.NoError(t, err)
defer reuse.Close()
quicTr, err := quic.NewTransport(priv, reuse, nil, nil, nil)
quicTr, err := libp2pquic.NewTransport(priv, reuse, nil, nil, nil)
require.NoError(t, err)
require.NoError(t, s.AddTransport(quicTr))
webtransportTr, err := webtransport.New(priv, nil, reuse, nil, nil)
Expand Down
2 changes: 1 addition & 1 deletion p2p/net/swarm/swarm_dial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func newTestSwarmWithResolver(t *testing.T, resolver *madns.Resolver) *Swarm {
err = s.AddTransport(tpt)
require.NoError(t, err)

connmgr, err := quicreuse.NewConnManager(quic.StatelessResetKey{})
connmgr, err := quicreuse.NewConnManager(quic.StatelessResetKey{}, quic.TokenGeneratorKey{})
require.NoError(t, err)
quicTpt, err := libp2pquic.NewTransport(priv, connmgr, nil, nil, &network.NullResourceManager{})
require.NoError(t, err)
Expand Down
7 changes: 4 additions & 3 deletions p2p/net/swarm/testing/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ import (
"github.com/libp2p/go-libp2p/p2p/muxer/yamux"
"github.com/libp2p/go-libp2p/p2p/net/swarm"
tptu "github.com/libp2p/go-libp2p/p2p/net/upgrader"
quic "github.com/libp2p/go-libp2p/p2p/transport/quic"
libp2pquic "github.com/libp2p/go-libp2p/p2p/transport/quic"
"github.com/libp2p/go-libp2p/p2p/transport/quicreuse"
"github.com/libp2p/go-libp2p/p2p/transport/tcp"

ma "github.com/multiformats/go-multiaddr"
"github.com/quic-go/quic-go"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -175,11 +176,11 @@ func GenSwarm(t *testing.T, opts ...Option) *swarm.Swarm {
}
}
if !cfg.disableQUIC {
reuse, err := quicreuse.NewConnManager([32]byte{})
reuse, err := quicreuse.NewConnManager(quic.StatelessResetKey{}, quic.TokenGeneratorKey{})
if err != nil {
t.Fatal(err)
}
quicTransport, err := quic.NewTransport(priv, reuse, nil, cfg.connectionGater, nil)
quicTransport, err := libp2pquic.NewTransport(priv, reuse, nil, cfg.connectionGater, nil)
if err != nil {
t.Fatal(err)
}
Expand Down
6 changes: 3 additions & 3 deletions p2p/transport/quic/cmd/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (
"log"
"os"

"github.com/libp2p/go-libp2p/p2p/transport/quicreuse"

ic "github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/peer"
libp2pquic "github.com/libp2p/go-libp2p/p2p/transport/quic"
"github.com/libp2p/go-libp2p/p2p/transport/quicreuse"

ma "github.com/multiformats/go-multiaddr"
"github.com/quic-go/quic-go"
)

func main() {
Expand All @@ -41,7 +41,7 @@ func run(raddr string, p string) error {
return err
}

reuse, err := quicreuse.NewConnManager([32]byte{})
reuse, err := quicreuse.NewConnManager(quic.StatelessResetKey{}, quic.TokenGeneratorKey{})
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions p2p/transport/quic/cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import (
"log"
"os"

"github.com/libp2p/go-libp2p/p2p/transport/quicreuse"

ic "github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/peer"
tpt "github.com/libp2p/go-libp2p/core/transport"
libp2pquic "github.com/libp2p/go-libp2p/p2p/transport/quic"
"github.com/libp2p/go-libp2p/p2p/transport/quicreuse"

ma "github.com/multiformats/go-multiaddr"
"github.com/quic-go/quic-go"
)

func main() {
Expand All @@ -41,7 +41,7 @@ func run(port string) error {
return err
}

reuse, err := quicreuse.NewConnManager([32]byte{})
reuse, err := quicreuse.NewConnManager(quic.StatelessResetKey{}, quic.TokenGeneratorKey{})
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion p2p/transport/quic/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func runServer(t *testing.T, tr tpt.Transport, addr string) tpt.Listener {

func newConnManager(t *testing.T, opts ...quicreuse.Option) *quicreuse.ConnManager {
t.Helper()
cm, err := quicreuse.NewConnManager([32]byte{}, opts...)
cm, err := quicreuse.NewConnManager(quic.StatelessResetKey{}, quic.TokenGeneratorKey{}, opts...)
require.NoError(t, err)
t.Cleanup(func() { cm.Close() })
return cm
Expand Down
6 changes: 4 additions & 2 deletions p2p/transport/quicreuse/connmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,21 @@ type ConnManager struct {
quicListenersMu sync.Mutex
quicListeners map[string]quicListenerEntry

srk quic.StatelessResetKey
srk quic.StatelessResetKey
tokenKey quic.TokenGeneratorKey
}

type quicListenerEntry struct {
refCount int
ln *quicListener
}

func NewConnManager(statelessResetKey quic.StatelessResetKey, opts ...Option) (*ConnManager, error) {
func NewConnManager(statelessResetKey quic.StatelessResetKey, tokenKey quic.TokenGeneratorKey, opts ...Option) (*ConnManager, error) {
cm := &ConnManager{
enableReuseport: true,
quicListeners: make(map[string]quicListenerEntry),
srk: statelessResetKey,
tokenKey: tokenKey,
}
for _, o := range opts {
if err := o(cm); err != nil {
Expand Down
10 changes: 5 additions & 5 deletions p2p/transport/quicreuse/connmgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func testListenOnSameProto(t *testing.T, enableReuseport bool) {
if !enableReuseport {
opts = append(opts, DisableReuseport())
}
cm, err := NewConnManager([32]byte{}, opts...)
cm, err := NewConnManager(quic.StatelessResetKey{}, quic.TokenGeneratorKey{}, opts...)
require.NoError(t, err)
defer checkClosed(t, cm)
defer cm.Close()
Expand Down Expand Up @@ -83,7 +83,7 @@ func TestConnectionPassedToQUICForListening(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("skipping on windows. Windows doesn't support these optimizations")
}
cm, err := NewConnManager([32]byte{}, DisableReuseport())
cm, err := NewConnManager(quic.StatelessResetKey{}, quic.TokenGeneratorKey{}, DisableReuseport())
require.NoError(t, err)
defer cm.Close()

Expand All @@ -107,7 +107,7 @@ func TestConnectionPassedToQUICForListening(t *testing.T) {
func TestAcceptErrorGetCleanedUp(t *testing.T) {
raddr := ma.StringCast("/ip4/127.0.0.1/udp/0/quic-v1")

cm, err := NewConnManager([32]byte{}, DisableReuseport())
cm, err := NewConnManager(quic.StatelessResetKey{}, quic.TokenGeneratorKey{}, DisableReuseport())
require.NoError(t, err)
defer cm.Close()

Expand Down Expand Up @@ -143,7 +143,7 @@ func TestConnectionPassedToQUICForDialing(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("skipping on windows. Windows doesn't support these optimizations")
}
cm, err := NewConnManager([32]byte{}, DisableReuseport())
cm, err := NewConnManager(quic.StatelessResetKey{}, quic.TokenGeneratorKey{}, DisableReuseport())
require.NoError(t, err)
defer cm.Close()

Expand Down Expand Up @@ -218,7 +218,7 @@ func testListener(t *testing.T, enableReuseport bool) {
if !enableReuseport {
opts = append(opts, DisableReuseport())
}
cm, err := NewConnManager([32]byte{}, opts...)
cm, err := NewConnManager(quic.StatelessResetKey{}, quic.TokenGeneratorKey{}, opts...)
require.NoError(t, err)

id1, tlsConf1 := getTLSConfForProto(t, "proto1")
Expand Down
2 changes: 1 addition & 1 deletion p2p/transport/webtransport/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func getCerthashComponent(t *testing.T, b []byte) ma.Multiaddr {

func newConnManager(t *testing.T, opts ...quicreuse.Option) *quicreuse.ConnManager {
t.Helper()
cm, err := quicreuse.NewConnManager([32]byte{}, opts...)
cm, err := quicreuse.NewConnManager(quic.StatelessResetKey{}, quic.TokenGeneratorKey{}, opts...)
require.NoError(t, err)
t.Cleanup(func() { cm.Close() })
return cm
Expand Down

0 comments on commit d4e1e8e

Please sign in to comment.