From 4f3d531800f5c090dd19a8819824cf6b5d9d196f Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Sun, 19 Sep 2021 16:10:21 +0100 Subject: [PATCH 1/2] return ma.Protocols from Transport.Protocols --- go.mod | 2 +- go.sum | 4 ++-- transport/transport.go | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index e4bee6c..ac0dfc2 100644 --- a/go.mod +++ b/go.mod @@ -13,7 +13,7 @@ require ( github.com/libp2p/go-openssl v0.0.7 github.com/minio/sha256-simd v0.1.1 github.com/mr-tron/base58 v1.2.0 - github.com/multiformats/go-multiaddr v0.2.2 + github.com/multiformats/go-multiaddr v0.4.0 github.com/multiformats/go-multihash v0.0.14 github.com/multiformats/go-varint v0.0.6 github.com/stretchr/testify v1.7.0 diff --git a/go.sum b/go.sum index 8913ef7..7f5235e 100644 --- a/go.sum +++ b/go.sum @@ -46,8 +46,8 @@ github.com/multiformats/go-base32 v0.0.3 h1:tw5+NhuwaOjJCC5Pp82QuXbrmLzWg7uxlMFp github.com/multiformats/go-base32 v0.0.3/go.mod h1:pLiuGC8y0QR3Ue4Zug5UzK9LjgbkL8NSQj0zQ5Nz/AA= github.com/multiformats/go-base36 v0.1.0 h1:JR6TyF7JjGd3m6FbLU2cOxhC0Li8z8dLNGQ89tUg4F4= github.com/multiformats/go-base36 v0.1.0/go.mod h1:kFGE83c6s80PklsHO9sRn2NCoffoRdUUOENyW/Vv6sM= -github.com/multiformats/go-multiaddr v0.2.2 h1:XZLDTszBIJe6m0zF6ITBrEcZR73OPUhCBBS9rYAuUzI= -github.com/multiformats/go-multiaddr v0.2.2/go.mod h1:NtfXiOtHvghW9KojvtySjH5y0u0xW5UouOmQQrn6a3Y= +github.com/multiformats/go-multiaddr v0.4.0 h1:hL/K4ZJhJ5PTw3nwylq9lGU5yArzcAroZmex1ghSEkQ= +github.com/multiformats/go-multiaddr v0.4.0/go.mod h1:YcpyLH8ZPudLxQlemYBPhSm0/oCXAT8Z4mzFpyoPyRc= github.com/multiformats/go-multibase v0.0.3 h1:l/B6bJDQjvQ5G52jw4QGSYeOTZoAwIO77RblWplfIqk= github.com/multiformats/go-multibase v0.0.3/go.mod h1:5+1R4eQrT3PkYZ24C3W2Ue2tPwIdYQD509ZjSb5y9Oc= github.com/multiformats/go-multihash v0.0.13/go.mod h1:VdAWLKTwram9oKAatUcLxBNUjdtcVwxObEQBtRfuyjc= diff --git a/transport/transport.go b/transport/transport.go index d3c69f9..d8f1c9f 100644 --- a/transport/transport.go +++ b/transport/transport.go @@ -75,10 +75,10 @@ type Transport interface { // Listen listens on the passed multiaddr. Listen(laddr ma.Multiaddr) (Listener, error) - // Protocol returns the set of protocols handled by this transport. - // + // Protocols returns the set of protocols handled by this transport. + // If protocols A and B are returned, this means that this transport supports running B on top of A. // See the Network interface for an explanation of how this is used. - Protocols() []int + Protocols() []ma.Protocol // Proxy returns true if this is a proxy transport. // From f829c09c1ca08418c90cd37d717dada212b1d0ee Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Sun, 19 Sep 2021 16:40:55 +0100 Subject: [PATCH 2/2] add a Protocol method to the SecureTransport interface --- go.mod | 2 +- go.sum | 4 ++-- sec/insecure/insecure.go | 40 ++++++++++++++++++++++++---------------- sec/security.go | 5 +++++ 4 files changed, 32 insertions(+), 19 deletions(-) diff --git a/go.mod b/go.mod index ac0dfc2..a26f354 100644 --- a/go.mod +++ b/go.mod @@ -13,7 +13,7 @@ require ( github.com/libp2p/go-openssl v0.0.7 github.com/minio/sha256-simd v0.1.1 github.com/mr-tron/base58 v1.2.0 - github.com/multiformats/go-multiaddr v0.4.0 + github.com/multiformats/go-multiaddr v0.4.1 github.com/multiformats/go-multihash v0.0.14 github.com/multiformats/go-varint v0.0.6 github.com/stretchr/testify v1.7.0 diff --git a/go.sum b/go.sum index 7f5235e..f7578b6 100644 --- a/go.sum +++ b/go.sum @@ -46,8 +46,8 @@ github.com/multiformats/go-base32 v0.0.3 h1:tw5+NhuwaOjJCC5Pp82QuXbrmLzWg7uxlMFp github.com/multiformats/go-base32 v0.0.3/go.mod h1:pLiuGC8y0QR3Ue4Zug5UzK9LjgbkL8NSQj0zQ5Nz/AA= github.com/multiformats/go-base36 v0.1.0 h1:JR6TyF7JjGd3m6FbLU2cOxhC0Li8z8dLNGQ89tUg4F4= github.com/multiformats/go-base36 v0.1.0/go.mod h1:kFGE83c6s80PklsHO9sRn2NCoffoRdUUOENyW/Vv6sM= -github.com/multiformats/go-multiaddr v0.4.0 h1:hL/K4ZJhJ5PTw3nwylq9lGU5yArzcAroZmex1ghSEkQ= -github.com/multiformats/go-multiaddr v0.4.0/go.mod h1:YcpyLH8ZPudLxQlemYBPhSm0/oCXAT8Z4mzFpyoPyRc= +github.com/multiformats/go-multiaddr v0.4.1 h1:Pq37uLx3hsyNlTDir7FZyU8+cFCTqd5y1KiM2IzOutI= +github.com/multiformats/go-multiaddr v0.4.1/go.mod h1:3afI9HfVW8csiF8UZqtpYRiDyew8pRX7qLIGHu9FLuM= github.com/multiformats/go-multibase v0.0.3 h1:l/B6bJDQjvQ5G52jw4QGSYeOTZoAwIO77RblWplfIqk= github.com/multiformats/go-multibase v0.0.3/go.mod h1:5+1R4eQrT3PkYZ24C3W2Ue2tPwIdYQD509ZjSb5y9Oc= github.com/multiformats/go-multihash v0.0.13/go.mod h1:VdAWLKTwram9oKAatUcLxBNUjdtcVwxObEQBtRfuyjc= diff --git a/sec/insecure/insecure.go b/sec/insecure/insecure.go index 1fd5245..1826e95 100644 --- a/sec/insecure/insecure.go +++ b/sec/insecure/insecure.go @@ -9,12 +9,13 @@ import ( "io" "net" + "github.com/libp2p/go-libp2p-core/crypto" "github.com/libp2p/go-libp2p-core/peer" "github.com/libp2p/go-libp2p-core/sec" - "github.com/libp2p/go-msgio" - - ci "github.com/libp2p/go-libp2p-core/crypto" pb "github.com/libp2p/go-libp2p-core/sec/insecure/pb" + + "github.com/libp2p/go-msgio" + ma "github.com/multiformats/go-multiaddr" ) // ID is the multistream-select protocol ID that should be used when identifying @@ -28,20 +29,28 @@ const ID = "/plaintext/2.0.0" // No authentication of the remote identity is performed. type Transport struct { id peer.ID - key ci.PrivKey + key crypto.PrivKey } +var _ sec.SecureTransport = &Transport{} + // NewWithIdentity constructs a new insecure transport. The provided private key // is stored and returned from LocalPrivateKey to satisfy the // SecureTransport interface, and the public key is sent to // remote peers. No security is provided. -func NewWithIdentity(id peer.ID, key ci.PrivKey) *Transport { +func NewWithIdentity(id peer.ID, key crypto.PrivKey) *Transport { return &Transport{ id: id, key: key, } } +var protoPlaintextV2 = ma.ProtocolWithCode(ma.P_PLAINTEXTV2) + +func (t *Transport) Protocol() ma.Protocol { + return protoPlaintextV2 +} + // LocalPeer returns the transport's local peer ID. func (t *Transport) LocalPeer() peer.ID { return t.id @@ -49,7 +58,7 @@ func (t *Transport) LocalPeer() peer.ID { // LocalPrivateKey returns the local private key. // This key is used only for identity generation and provides no security. -func (t *Transport) LocalPrivateKey() ci.PrivKey { +func (t *Transport) LocalPrivateKey() crypto.PrivKey { return t.key } @@ -114,12 +123,14 @@ type Conn struct { local peer.ID remote peer.ID - localPrivKey ci.PrivKey - remotePubKey ci.PubKey + localPrivKey crypto.PrivKey + remotePubKey crypto.PubKey } -func makeExchangeMessage(pubkey ci.PubKey) (*pb.Exchange, error) { - keyMsg, err := ci.PublicKeyToProto(pubkey) +var _ sec.SecureConn = &Conn{} + +func makeExchangeMessage(pubkey crypto.PubKey) (*pb.Exchange, error) { + keyMsg, err := crypto.PublicKeyToProto(pubkey) if err != nil { return nil, err } @@ -153,7 +164,7 @@ func (ic *Conn) runHandshakeSync() error { } // Pull remote ID and public key from message - remotePubkey, err := ci.PublicKeyFromProto(remoteMsg.Pubkey) + remotePubkey, err := crypto.PublicKeyFromProto(remoteMsg.Pubkey) if err != nil { return err } @@ -221,14 +232,11 @@ func (ic *Conn) RemotePeer() peer.ID { // RemotePublicKey returns whatever public key was given by the remote peer. // Note that no verification of ownership is done, as this connection is not secure. -func (ic *Conn) RemotePublicKey() ci.PubKey { +func (ic *Conn) RemotePublicKey() crypto.PubKey { return ic.remotePubKey } // LocalPrivateKey returns the private key for the local peer. -func (ic *Conn) LocalPrivateKey() ci.PrivKey { +func (ic *Conn) LocalPrivateKey() crypto.PrivKey { return ic.localPrivKey } - -var _ sec.SecureTransport = (*Transport)(nil) -var _ sec.SecureConn = (*Conn)(nil) diff --git a/sec/security.go b/sec/security.go index a4cd7a2..165e446 100644 --- a/sec/security.go +++ b/sec/security.go @@ -7,6 +7,8 @@ import ( "github.com/libp2p/go-libp2p-core/network" "github.com/libp2p/go-libp2p-core/peer" + + ma "github.com/multiformats/go-multiaddr" ) // SecureConn is an authenticated, encrypted connection. @@ -24,6 +26,9 @@ type SecureTransport interface { // SecureOutbound secures an outbound connection. SecureOutbound(ctx context.Context, insecure net.Conn, p peer.ID) (SecureConn, error) + + // Protocol returns the handshake protocol used by this SecureTransport. + Protocol() ma.Protocol } // A SecureMuxer is a wrapper around SecureTransport which can select security protocols