Skip to content

Commit

Permalink
Merge branch 'master' into sukun/webrtc-fin-ack
Browse files Browse the repository at this point in the history
  • Loading branch information
sukunrt authored Feb 21, 2024
2 parents 8992b8c + 00105da commit 801a23a
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 67 deletions.
42 changes: 0 additions & 42 deletions core/crypto/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
package crypto

import (
"crypto/ecdh"
"crypto/rand"
"crypto/subtle"
"encoding/base64"
"errors"
"fmt"
"io"

"github.com/libp2p/go-libp2p/core/crypto/pb"
Expand Down Expand Up @@ -122,46 +120,6 @@ func GenerateKeyPairWithReader(typ, bits int, src io.Reader) (PrivKey, PubKey, e
}
}

// GenerateEKeyPair returns an ephemeral public key and returns a function that will compute
// the shared secret key. Used in the identify module.
//
// Focuses only on ECDH now, but can be made more general in the future.
func GenerateEKeyPair(curveName string) ([]byte, GenSharedKey, error) {
var curve ecdh.Curve

switch curveName {
case "P-256":
curve = ecdh.P256()
case "P-384":
curve = ecdh.P384()
case "P-521":
curve = ecdh.P521()
default:
return nil, nil, fmt.Errorf("unknown curve name")
}

priv, err := curve.GenerateKey(rand.Reader)
if err != nil {
return nil, nil, err
}

done := func(theirPub []byte) ([]byte, error) {
// Verify and unpack node's public key.
pubKey, err := curve.NewPublicKey(theirPub)
if err == nil {
return nil, fmt.Errorf("malformed public key: %d %v", len(theirPub), theirPub)
}

secret, err := priv.ECDH(pubKey)
if err != nil {
return nil, fmt.Errorf("failed to do ecdh: %w", err)
}
return secret, nil
}

return priv.PublicKey().Bytes(), done, nil
}

// UnmarshalPublicKey converts a protobuf serialized public key into its
// representative object
func UnmarshalPublicKey(data []byte) (PubKey, error) {
Expand Down
12 changes: 0 additions & 12 deletions core/crypto/key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,15 +291,3 @@ func testKeyEquals(t *testing.T, k Key) {
t.Fatal("Keys should not equal.")
}
}

func TestUnknownCurveErrors(t *testing.T) {
_, _, err := GenerateEKeyPair("P-256")
if err != nil {
t.Fatal(err)
}

_, _, err = GenerateEKeyPair("error-please")
if err == nil {
t.Fatal("expected invalid key type to error")
}
}
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ require (
github.com/pion/webrtc/v3 v3.2.23
github.com/prometheus/client_golang v1.14.0
github.com/prometheus/client_model v0.4.0
github.com/quic-go/quic-go v0.39.3
github.com/quic-go/quic-go v0.41.0
github.com/quic-go/webtransport-go v0.6.0
github.com/raulk/go-watchdog v1.3.0
github.com/stretchr/testify v1.8.4
Expand Down Expand Up @@ -117,7 +117,6 @@ require (
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/quic-go/qpack v0.4.0 // indirect
github.com/quic-go/qtls-go1-20 v0.3.4 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/syndtr/goleveldb v1.0.0 // indirect
go.uber.org/atomic v1.11.0 // indirect
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -483,10 +483,8 @@ github.com/prometheus/procfs v0.8.0 h1:ODq8ZFEaYeCaZOJlZZdJA2AbQR98dSHSM1KW/You5
github.com/prometheus/procfs v0.8.0/go.mod h1:z7EfXMXOkbkqb9IINtpCn86r/to3BnA0uaxHdg830/4=
github.com/quic-go/qpack v0.4.0 h1:Cr9BXA1sQS2SmDUWjSofMPNKmvF6IiIfDRmgU0w1ZCo=
github.com/quic-go/qpack v0.4.0/go.mod h1:UZVnYIfi5GRk+zI9UMaCPsmZ2xKJP7XBUvVyT1Knj9A=
github.com/quic-go/qtls-go1-20 v0.3.4 h1:MfFAPULvst4yoMgY9QmtpYmfij/em7O8UUi+bNVm7Cg=
github.com/quic-go/qtls-go1-20 v0.3.4/go.mod h1:X9Nh97ZL80Z+bX/gUXMbipO6OxdiDi58b/fMC9mAL+k=
github.com/quic-go/quic-go v0.39.3 h1:o3YB6t2SR+HU/pgwF29kJ6g4jJIJEwEZ8CKia1h1TKg=
github.com/quic-go/quic-go v0.39.3/go.mod h1:T09QsDQWjLiQ74ZmacDfqZmhY/NLnw5BC40MANNNZ1Q=
github.com/quic-go/quic-go v0.41.0 h1:aD8MmHfgqTURWNJy48IYFg2OnxwHT3JL7ahGs73lb4k=
github.com/quic-go/quic-go v0.41.0/go.mod h1:qCkNjqczPEvgsOnxZ0eCD14lv+B2LHlFAB++CNOh9hA=
github.com/quic-go/webtransport-go v0.6.0 h1:CvNsKqc4W2HljHJnoT+rMmbRJybShZ0YPFDD3NxaZLY=
github.com/quic-go/webtransport-go v0.6.0/go.mod h1:9KjU4AEBqEQidGHNDkZrb8CAa1abRaosM2yGOyiikEc=
github.com/raulk/go-watchdog v1.3.0 h1:oUmdlHxdkXRJlwfG0O9omj8ukerm8MEQavSiDTEtBsk=
Expand Down
14 changes: 13 additions & 1 deletion p2p/transport/quicreuse/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type protoConf struct {

type quicListener struct {
l *quic.Listener
closeMx sync.Mutex
transport refCountedQuicTransport
running chan struct{}
addrs []ma.Multiaddr
Expand Down Expand Up @@ -124,7 +125,13 @@ func (l *quicListener) Add(tlsConf *tls.Config, allowWindowIncrease func(conn qu

func (l *quicListener) Run() error {
defer close(l.running)
defer l.transport.DecreaseCount()
defer func() {
// transport close is not safe to use concurrently with listener close.
// remove after https://github.com/quic-go/quic-go/issues/4266 is fixed.
l.closeMx.Lock()
defer l.closeMx.Unlock()
l.transport.DecreaseCount()
}()
for {
conn, err := l.l.Accept(context.Background())
if err != nil {
Expand All @@ -147,7 +154,12 @@ func (l *quicListener) Run() error {
}

func (l *quicListener) Close() error {
// listener close is not safe to use concurrently with transport close.
// remove after https://github.com/quic-go/quic-go/issues/4266 is fixed.
l.closeMx.Lock()
err := l.l.Close()
l.closeMx.Unlock()

<-l.running // wait for Run to return
return err
}
Expand Down
9 changes: 9 additions & 0 deletions p2p/transport/webtransport/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,5 +212,14 @@ func (l *listener) Close() error {
l.reuseListener.Close()
err := l.server.Close()
<-l.serverClosed
loop:
for {
select {
case conn := <-l.queue:
conn.Close()
default:
break loop
}
}
return err
}
3 changes: 1 addition & 2 deletions test-plans/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ require (
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/quic-go/qpack v0.4.0 // indirect
github.com/quic-go/qtls-go1-20 v0.3.4 // indirect
github.com/quic-go/quic-go v0.39.3 // indirect
github.com/quic-go/quic-go v0.41.0 // indirect
github.com/quic-go/webtransport-go v0.6.0 // indirect
github.com/raulk/go-watchdog v1.3.0 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
Expand Down
6 changes: 2 additions & 4 deletions test-plans/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -436,10 +436,8 @@ github.com/prometheus/procfs v0.8.0 h1:ODq8ZFEaYeCaZOJlZZdJA2AbQR98dSHSM1KW/You5
github.com/prometheus/procfs v0.8.0/go.mod h1:z7EfXMXOkbkqb9IINtpCn86r/to3BnA0uaxHdg830/4=
github.com/quic-go/qpack v0.4.0 h1:Cr9BXA1sQS2SmDUWjSofMPNKmvF6IiIfDRmgU0w1ZCo=
github.com/quic-go/qpack v0.4.0/go.mod h1:UZVnYIfi5GRk+zI9UMaCPsmZ2xKJP7XBUvVyT1Knj9A=
github.com/quic-go/qtls-go1-20 v0.3.4 h1:MfFAPULvst4yoMgY9QmtpYmfij/em7O8UUi+bNVm7Cg=
github.com/quic-go/qtls-go1-20 v0.3.4/go.mod h1:X9Nh97ZL80Z+bX/gUXMbipO6OxdiDi58b/fMC9mAL+k=
github.com/quic-go/quic-go v0.39.3 h1:o3YB6t2SR+HU/pgwF29kJ6g4jJIJEwEZ8CKia1h1TKg=
github.com/quic-go/quic-go v0.39.3/go.mod h1:T09QsDQWjLiQ74ZmacDfqZmhY/NLnw5BC40MANNNZ1Q=
github.com/quic-go/quic-go v0.41.0 h1:aD8MmHfgqTURWNJy48IYFg2OnxwHT3JL7ahGs73lb4k=
github.com/quic-go/quic-go v0.41.0/go.mod h1:qCkNjqczPEvgsOnxZ0eCD14lv+B2LHlFAB++CNOh9hA=
github.com/quic-go/webtransport-go v0.6.0 h1:CvNsKqc4W2HljHJnoT+rMmbRJybShZ0YPFDD3NxaZLY=
github.com/quic-go/webtransport-go v0.6.0/go.mod h1:9KjU4AEBqEQidGHNDkZrb8CAa1abRaosM2yGOyiikEc=
github.com/raulk/go-watchdog v1.3.0 h1:oUmdlHxdkXRJlwfG0O9omj8ukerm8MEQavSiDTEtBsk=
Expand Down

0 comments on commit 801a23a

Please sign in to comment.