diff --git a/p2p/peer.go b/p2p/peer.go index af019d07a897..f351bf30569d 100644 --- a/p2p/peer.go +++ b/p2p/peer.go @@ -17,6 +17,7 @@ package p2p import ( + "encoding/hex" "errors" "fmt" "io" @@ -429,6 +430,7 @@ type PeerInfo struct { ID string `json:"id"` // Unique node identifier Name string `json:"name"` // Name of the node, including client type, version, OS, custom data Caps []string `json:"caps"` // Protocols advertised by this peer + ENR string `json:"enr"` // Ethereum Node Record Network struct { LocalAddress string `json:"localAddress"` // Local endpoint of the TCP data connection RemoteAddress string `json:"remoteAddress"` // Remote endpoint of the TCP data connection @@ -454,6 +456,9 @@ func (p *Peer) Info() *PeerInfo { Caps: caps, Protocols: make(map[string]interface{}), } + if enc, err := rlp.EncodeToBytes(p.Node().Record()); err == nil { + info.ENR = "0x" + hex.EncodeToString(enc) + } info.Network.LocalAddress = p.LocalAddr().String() info.Network.RemoteAddress = p.RemoteAddr().String() info.Network.Inbound = p.rw.is(inboundConn) diff --git a/p2p/rlpx.go b/p2p/rlpx.go index 67cc1d9bf316..ba8e427af1da 100644 --- a/p2p/rlpx.go +++ b/p2p/rlpx.go @@ -39,6 +39,8 @@ import ( "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto/ecies" "github.com/ethereum/go-ethereum/crypto/secp256k1" + "github.com/ethereum/go-ethereum/p2p/enode" + "github.com/ethereum/go-ethereum/p2p/enr" "github.com/ethereum/go-ethereum/rlp" "github.com/golang/snappy" "golang.org/x/crypto/sha3" @@ -77,15 +79,16 @@ var errPlainMessageTooLarge = errors.New("message length >= 16MB") // rlpx is the transport protocol used by actual (non-test) connections. // It wraps the frame encoder with locks and read/write deadlines. type rlpx struct { - fd net.Conn + self *enode.Node + fd net.Conn rmu, wmu sync.Mutex rw *rlpxFrameRW } -func newRLPX(fd net.Conn) transport { +func newRLPX(self *enode.Node, fd net.Conn) transport { fd.SetDeadline(time.Now().Add(handshakeTimeout)) - return &rlpx{fd: fd} + return &rlpx{self: self, fd: fd} } func (t *rlpx) ReadMsg() (Msg, error) { @@ -175,23 +178,23 @@ func readProtocolHandshake(rw MsgReader, our *protoHandshake) (*protoHandshake, // messages. the protocol handshake is the first authenticated message // and also verifies whether the encryption handshake 'worked' and the // remote side actually provided the right public key. -func (t *rlpx) doEncHandshake(prv *ecdsa.PrivateKey, dial *ecdsa.PublicKey) (*ecdsa.PublicKey, error) { +func (t *rlpx) doEncHandshake(prv *ecdsa.PrivateKey, dial *ecdsa.PublicKey) (*ecdsa.PublicKey, *enode.Node, error) { var ( sec secrets err error ) if dial == nil { - sec, err = receiverEncHandshake(t.fd, prv) + sec, err = receiverEncHandshake(t.fd, prv, t.self) } else { - sec, err = initiatorEncHandshake(t.fd, prv, dial) + sec, err = initiatorEncHandshake(t.fd, prv, t.self, dial) } if err != nil { - return nil, err + return nil, nil, err } t.wmu.Lock() t.rw = newRLPXFrameRW(t.fd, sec) t.wmu.Unlock() - return sec.Remote.ExportECDSA(), nil + return sec.Remote.ExportECDSA(), sec.RemoteNode, nil } // encHandshake contains the state of the encryption handshake. @@ -201,6 +204,7 @@ type encHandshake struct { initNonce, respNonce []byte // nonce randomPrivKey *ecies.PrivateKey // ecdhe-random remoteRandomPub *ecies.PublicKey // ecdhe-random-pubk + remoteNode *enr.Record } // secrets represents the connection secrets @@ -210,6 +214,7 @@ type secrets struct { AES, MAC []byte EgressMAC, IngressMAC hash.Hash Token []byte + RemoteNode *enode.Node } // RLPx v4 handshake auth (defined in EIP-8). @@ -223,6 +228,7 @@ type authMsgV4 struct { // Ignore additional fields (forward-compatibility) Rest []rlp.RawValue `rlp:"tail"` + // Record *enr.Record -- EIP-XXX RLPx ENR Extension } // RLPx v4 handshake response (defined in EIP-8). @@ -233,6 +239,7 @@ type authRespV4 struct { // Ignore additional fields (forward-compatibility) Rest []rlp.RawValue `rlp:"tail"` + // Record *enr.Record -- EIP-XXX RLPx ENR Extension } // secrets is called after the handshake is completed. @@ -265,6 +272,14 @@ func (h *encHandshake) secrets(auth, authResp []byte) (secrets, error) { s.EgressMAC, s.IngressMAC = mac2, mac1 } + // Verify ENR. + if h.remoteNode != nil { + rn, err := enode.New(enode.ValidSchemes, h.remoteNode) + if err != nil { + return s, fmt.Errorf("invalid node record: %v", err) + } + s.RemoteNode = rn + } return s, nil } @@ -278,9 +293,9 @@ func (h *encHandshake) staticSharedSecret(prv *ecdsa.PrivateKey) ([]byte, error) // it should be called on the dialing side of the connection. // // prv is the local client's private key. -func initiatorEncHandshake(conn io.ReadWriter, prv *ecdsa.PrivateKey, remote *ecdsa.PublicKey) (s secrets, err error) { +func initiatorEncHandshake(conn io.ReadWriter, prv *ecdsa.PrivateKey, self *enode.Node, remote *ecdsa.PublicKey) (s secrets, err error) { h := &encHandshake{initiator: true, remote: ecies.ImportECDSAPublic(remote)} - authMsg, err := h.makeAuthMsg(prv) + authMsg, err := h.makeAuthMsg(prv, self) if err != nil { return s, err } @@ -304,7 +319,7 @@ func initiatorEncHandshake(conn io.ReadWriter, prv *ecdsa.PrivateKey, remote *ec } // makeAuthMsg creates the initiator handshake message. -func (h *encHandshake) makeAuthMsg(prv *ecdsa.PrivateKey) (*authMsgV4, error) { +func (h *encHandshake) makeAuthMsg(prv *ecdsa.PrivateKey, self *enode.Node) (*authMsgV4, error) { // Generate random initiator nonce. h.initNonce = make([]byte, shaLen) _, err := rand.Read(h.initNonce) @@ -333,12 +348,20 @@ func (h *encHandshake) makeAuthMsg(prv *ecdsa.PrivateKey) (*authMsgV4, error) { copy(msg.InitiatorPubkey[:], crypto.FromECDSAPub(&prv.PublicKey)[1:]) copy(msg.Nonce[:], h.initNonce) msg.Version = 4 + + // Encode node record. + record, err := rlp.EncodeToBytes(self.Record()) + if err != nil { + return nil, err + } + msg.Rest = []rlp.RawValue{record} return msg, nil } func (h *encHandshake) handleAuthResp(msg *authRespV4) (err error) { h.respNonce = msg.Nonce[:] h.remoteRandomPub, err = importPublicKey(msg.RandomPubkey[:]) + h.remoteNode = recordFromTail(msg.Rest) return err } @@ -346,7 +369,7 @@ func (h *encHandshake) handleAuthResp(msg *authRespV4) (err error) { // it should be called on the listening side of the connection. // // prv is the local client's private key. -func receiverEncHandshake(conn io.ReadWriter, prv *ecdsa.PrivateKey) (s secrets, err error) { +func receiverEncHandshake(conn io.ReadWriter, prv *ecdsa.PrivateKey, self *enode.Node) (s secrets, err error) { authMsg := new(authMsgV4) authPacket, err := readHandshakeMsg(authMsg, encAuthMsgLen, prv, conn) if err != nil { @@ -357,7 +380,7 @@ func receiverEncHandshake(conn io.ReadWriter, prv *ecdsa.PrivateKey) (s secrets, return s, err } - authRespMsg, err := h.makeAuthResp() + authRespMsg, err := h.makeAuthResp(self) if err != nil { return s, err } @@ -405,10 +428,11 @@ func (h *encHandshake) handleAuthMsg(msg *authMsgV4, prv *ecdsa.PrivateKey) erro return err } h.remoteRandomPub, _ = importPublicKey(remoteRandomPub) + h.remoteNode = recordFromTail(msg.Rest) return nil } -func (h *encHandshake) makeAuthResp() (msg *authRespV4, err error) { +func (h *encHandshake) makeAuthResp(self *enode.Node) (msg *authRespV4, err error) { // Generate random nonce. h.respNonce = make([]byte, shaLen) if _, err = rand.Read(h.respNonce); err != nil { @@ -419,6 +443,13 @@ func (h *encHandshake) makeAuthResp() (msg *authRespV4, err error) { copy(msg.Nonce[:], h.respNonce) copy(msg.RandomPubkey[:], exportPubkey(&h.randomPrivKey.PublicKey)) msg.Version = 4 + + // Encode node record. + record, err := rlp.EncodeToBytes(self.Record()) + if err != nil { + return nil, err + } + msg.Rest = []rlp.RawValue{record} return msg, nil } @@ -454,6 +485,17 @@ func (msg *authRespV4) decodePlain(input []byte) { msg.Version = 4 } +func recordFromTail(tail []rlp.RawValue) *enr.Record { + if len(tail) == 0 { + return nil + } + var r enr.Record + if err := rlp.DecodeBytes(tail[0], &r); err != nil { + return nil + } + return &r +} + var padSpace = make([]byte, 300) func sealEIP8(msg interface{}, h *encHandshake) ([]byte, error) { diff --git a/p2p/rlpx_test.go b/p2p/rlpx_test.go index 5d89818020aa..9db86c0e3cb0 100644 --- a/p2p/rlpx_test.go +++ b/p2p/rlpx_test.go @@ -34,6 +34,8 @@ import ( "github.com/davecgh/go-spew/spew" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto/ecies" + "github.com/ethereum/go-ethereum/p2p/enode" + "github.com/ethereum/go-ethereum/p2p/enr" "github.com/ethereum/go-ethereum/p2p/simulations/pipes" "github.com/ethereum/go-ethereum/rlp" "golang.org/x/crypto/sha3" @@ -82,13 +84,36 @@ func testEncHandshake(token []byte) error { type result struct { side string pubkey *ecdsa.PublicKey + enode *enode.Node err error } var ( - prv0, _ = crypto.GenerateKey() - prv1, _ = crypto.GenerateKey() + prv0, _ = crypto.GenerateKey() + prv1, _ = crypto.GenerateKey() + ) + + var rec0 enr.Record + err := enode.SignV4(&rec0, prv0) + if err != nil { + return err + } + enode0, err := enode.New(enode.V4ID{}, &rec0) + if err != nil { + return err + } + var rec1 enr.Record + err = enode.SignV4(&rec1, prv1) + if err != nil { + return err + } + enode1, err := enode.New(enode.V4ID{}, &rec1) + if err != nil { + return err + } + + var ( fd0, fd1 = net.Pipe() - c0, c1 = newRLPX(fd0).(*rlpx), newRLPX(fd1).(*rlpx) + c0, c1 = newRLPX(enode0, fd0).(*rlpx), newRLPX(enode1, fd1).(*rlpx) output = make(chan result) ) @@ -97,7 +122,7 @@ func testEncHandshake(token []byte) error { defer func() { output <- r }() defer fd0.Close() - r.pubkey, r.err = c0.doEncHandshake(prv0, &prv1.PublicKey) + r.pubkey, r.enode, r.err = c0.doEncHandshake(prv0, &prv1.PublicKey) if r.err != nil { return } @@ -110,7 +135,7 @@ func testEncHandshake(token []byte) error { defer func() { output <- r }() defer fd1.Close() - r.pubkey, r.err = c1.doEncHandshake(prv1, nil) + r.pubkey, r.enode, r.err = c1.doEncHandshake(prv1, nil) if r.err != nil { return } @@ -157,6 +182,26 @@ func TestProtocolHandshake(t *testing.T) { wg sync.WaitGroup ) + var rec0 enr.Record + err := enode.SignV4(&rec0, prv0) + if err != nil { + t.Fatal(err) + } + enode0, err := enode.New(enode.V4ID{}, &rec0) + if err != nil { + t.Fatal(err) + } + var rec1 enr.Record + err = enode.SignV4(&rec1, prv1) + if err != nil { + t.Fatal(err) + } + + enode1, err := enode.New(enode.V4ID{}, &rec1) + if err != nil { + t.Fatal(err) + } + fd0, fd1, err := pipes.TCPPipe() if err != nil { t.Fatal(err) @@ -166,8 +211,8 @@ func TestProtocolHandshake(t *testing.T) { go func() { defer wg.Done() defer fd0.Close() - rlpx := newRLPX(fd0) - rpubkey, err := rlpx.doEncHandshake(prv0, &prv1.PublicKey) + rlpx := newRLPX(enode0, fd0) + rpubkey, _, err := rlpx.doEncHandshake(prv0, &prv1.PublicKey) if err != nil { t.Errorf("dial side enc handshake failed: %v", err) return @@ -192,8 +237,8 @@ func TestProtocolHandshake(t *testing.T) { go func() { defer wg.Done() defer fd1.Close() - rlpx := newRLPX(fd1) - rpubkey, err := rlpx.doEncHandshake(prv1, nil) + rlpx := newRLPX(enode1, fd1) + rpubkey, _, err := rlpx.doEncHandshake(prv1, nil) if err != nil { t.Errorf("listen side enc handshake failed: %v", err) return diff --git a/p2p/server.go b/p2p/server.go index 566f01ffc5dd..f28bcc36153b 100644 --- a/p2p/server.go +++ b/p2p/server.go @@ -157,7 +157,7 @@ type Server struct { // Hooks for testing. These are useful because we can inhibit // the whole protocol stack. - newTransport func(net.Conn) transport + newTransport func(self *enode.Node, fd net.Conn) transport newPeerHook func(*Peer) lock sync.Mutex // protects running @@ -219,7 +219,7 @@ type conn struct { type transport interface { // The two handshakes. - doEncHandshake(prv *ecdsa.PrivateKey, dialDest *ecdsa.PublicKey) (*ecdsa.PublicKey, error) + doEncHandshake(prv *ecdsa.PrivateKey, dialDest *ecdsa.PublicKey) (*ecdsa.PublicKey, *enode.Node, error) doProtoHandshake(our *protoHandshake) (*protoHandshake, error) // The MsgReadWriter can only be used after the encryption // handshake has completed. The code uses conn.id to track this @@ -880,7 +880,7 @@ func (srv *Server) listenLoop() { // as a peer. It returns when the connection has been added as a peer // or the handshakes have failed. func (srv *Server) SetupConn(fd net.Conn, flags connFlag, dialDest *enode.Node) error { - c := &conn{fd: fd, transport: srv.newTransport(fd), flags: flags, cont: make(chan error)} + c := &conn{fd: fd, transport: srv.newTransport(srv.Self(), fd), flags: flags, cont: make(chan error)} err := srv.setupConn(c, flags, dialDest) if err != nil { c.close(err) @@ -906,7 +906,7 @@ func (srv *Server) setupConn(c *conn, flags connFlag, dialDest *enode.Node) erro } } // Run the encryption handshake. - remotePubkey, err := c.doEncHandshake(srv.PrivateKey, dialPubkey) + remotePubkey, rlpxNode, err := c.doEncHandshake(srv.PrivateKey, dialPubkey) if err != nil { srv.log.Trace("Failed RLPx handshake", "addr", c.fd.RemoteAddr(), "conn", c.flags, "err", err) return err @@ -916,10 +916,8 @@ func (srv *Server) setupConn(c *conn, flags connFlag, dialDest *enode.Node) erro if dialPubkey.X.Cmp(remotePubkey.X) != 0 || dialPubkey.Y.Cmp(remotePubkey.Y) != 0 { return DiscUnexpectedIdentity } - c.node = dialDest - } else { - c.node = nodeFromConn(remotePubkey, c.fd) } + c.node = handshakeNode(dialDest, rlpxNode, remotePubkey, c.fd) if conn, ok := c.fd.(*meteredConn); ok { conn.handshakeDone(c.node.ID()) } @@ -951,14 +949,28 @@ func (srv *Server) setupConn(c *conn, flags connFlag, dialDest *enode.Node) erro return nil } -func nodeFromConn(pubkey *ecdsa.PublicKey, conn net.Conn) *enode.Node { - var ip net.IP - var port int - if tcp, ok := conn.RemoteAddr().(*net.TCPAddr); ok { - ip = tcp.IP - port = tcp.Port +// handshakeNode decides which node record is stored in Peer after the handshake. +func handshakeNode(dialDest, rlpxNode *enode.Node, pubkey *ecdsa.PublicKey, conn net.Conn) *enode.Node { + switch { + case dialDest == nil && rlpxNode == nil: + // Fallback: no node record available, fake it + var ip net.IP + var port int + if tcp, ok := conn.RemoteAddr().(*net.TCPAddr); ok { + ip = tcp.IP + port = tcp.Port + } + return enode.NewV4(pubkey, ip, port, port) + case dialDest == nil: + return rlpxNode + case rlpxNode == nil: + return dialDest + default: + if rlpxNode.Seq() > dialDest.Seq() { + return rlpxNode + } + return dialDest } - return enode.NewV4(pubkey, ip, port, port) } func truncateName(s string) string { diff --git a/p2p/server_test.go b/p2p/server_test.go index f665c142453a..5af842de2a90 100644 --- a/p2p/server_test.go +++ b/p2p/server_test.go @@ -44,7 +44,8 @@ type testTransport struct { } func newTestTransport(rpub *ecdsa.PublicKey, fd net.Conn) transport { - wrapped := newRLPX(fd).(*rlpx) + enod := enode.NewV4(rpub, nil, 0, 0) + wrapped := newRLPX(enod, fd).(*rlpx) wrapped.rw = newRLPXFrameRW(fd, secrets{ MAC: zero16, AES: zero16, @@ -54,8 +55,8 @@ func newTestTransport(rpub *ecdsa.PublicKey, fd net.Conn) transport { return &testTransport{rpub: rpub, rlpx: wrapped} } -func (c *testTransport) doEncHandshake(prv *ecdsa.PrivateKey, dialDest *ecdsa.PublicKey) (*ecdsa.PublicKey, error) { - return c.rpub, nil +func (c *testTransport) doEncHandshake(prv *ecdsa.PrivateKey, dialDest *ecdsa.PublicKey) (*ecdsa.PublicKey, *enode.Node, error) { + return c.rpub, nil, nil } func (c *testTransport) doProtoHandshake(our *protoHandshake) (*protoHandshake, error) { @@ -78,7 +79,7 @@ func startTestServer(t *testing.T, remoteKey *ecdsa.PublicKey, pf func(*Peer)) * server := &Server{ Config: config, newPeerHook: pf, - newTransport: func(fd net.Conn) transport { return newTestTransport(remoteKey, fd) }, + newTransport: func(enod *enode.Node, fd net.Conn) transport { return newTestTransport(remoteKey, fd) }, } if err := server.Start(); err != nil { t.Fatalf("Could not start server: %v", err) @@ -435,7 +436,7 @@ func TestServerPeerLimits(t *testing.T) { NoDial: true, Protocols: []Protocol{discard}, }, - newTransport: func(fd net.Conn) transport { return tp }, + newTransport: func(enod *enode.Node, fd net.Conn) transport { return tp }, log: log.New(), } if err := srv.Start(); err != nil { @@ -548,7 +549,7 @@ func TestServerSetupConn(t *testing.T) { NoDial: true, Protocols: []Protocol{discard}, }, - newTransport: func(fd net.Conn) transport { return test.tt }, + newTransport: func(enod *enode.Node, fd net.Conn) transport { return test.tt }, log: log.New(), } if !test.dontstart { @@ -577,9 +578,9 @@ type setupTransport struct { closeErr error } -func (c *setupTransport) doEncHandshake(prv *ecdsa.PrivateKey, dialDest *ecdsa.PublicKey) (*ecdsa.PublicKey, error) { +func (c *setupTransport) doEncHandshake(prv *ecdsa.PrivateKey, dialDest *ecdsa.PublicKey) (*ecdsa.PublicKey, *enode.Node, error) { c.calls += "doEncHandshake," - return c.pubkey, c.encHandshakeErr + return c.pubkey, nil, c.encHandshakeErr } func (c *setupTransport) doProtoHandshake(our *protoHandshake) (*protoHandshake, error) { diff --git a/p2p/simulations/adapters/types.go b/p2p/simulations/adapters/types.go index 31856b76dd8a..69410fc50bac 100644 --- a/p2p/simulations/adapters/types.go +++ b/p2p/simulations/adapters/types.go @@ -30,6 +30,7 @@ import ( "github.com/ethereum/go-ethereum/node" "github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p/enode" + "github.com/ethereum/go-ethereum/p2p/enr" "github.com/ethereum/go-ethereum/rpc" ) @@ -99,6 +100,9 @@ type NodeConfig struct { // services registered by calling the RegisterService function) Services []string + // Node record + Record enr.Record + // function to sanction or prevent suggesting a peer Reachable func(id enode.ID) bool