github.com/aakash4dev/cometbft@v0.38.2/spec/p2p/legacy-docs/peer.md (about) 1 # Peers 2 3 This document explains how CometBFT Peers are identified and how they connect to one another. 4 5 ## Peer Identity 6 7 CometBFT peers are expected to maintain long-term persistent identities in the form of a public key. 8 Each peer has an ID defined as `peer.ID == peer.PubKey.Address()`, where `Address` uses the scheme defined in `crypto` package. 9 10 A single peer ID can have multiple IP addresses associated with it, but a node 11 will only ever connect to one at a time. 12 13 When attempting to connect to a peer, we use the PeerURL: `<ID>@<IP>:<PORT>`. 14 We will attempt to connect to the peer at IP:PORT, and verify, 15 via authenticated encryption, that it is in possession of the private key 16 corresponding to `<ID>`. This prevents man-in-the-middle attacks on the peer layer. 17 18 ## Connections 19 20 All p2p connections use TCP. 21 Upon establishing a successful TCP connection with a peer, 22 two handshakes are performed: one for authenticated encryption, and one for CometBFT versioning. 23 Both handshakes have configurable timeouts (they should complete quickly). 24 25 ### Authenticated Encryption Handshake 26 27 CometBFT implements the Station-to-Station protocol 28 using X25519 keys for Diffie-Helman key-exchange and chacha20poly1305 for encryption. 29 30 Previous versions of this protocol (0.32 and below) suffered from malleability attacks whereas an active man 31 in the middle attacker could compromise confidentiality as described in [Prime, Order Please! 32 Revisiting Small Subgroup and Invalid Curve Attacks on 33 Protocols using Diffie-Hellman](https://eprint.iacr.org/2019/526.pdf). 34 35 We have added dependency on the Merlin a keccak based transcript hashing protocol to ensure non-malleability. 36 37 It goes as follows: 38 39 - generate an ephemeral X25519 keypair 40 - send the ephemeral public key to the peer 41 - wait to receive the peer's ephemeral public key 42 - create a new Merlin Transcript with the string "TENDERMINT_SECRET_CONNECTION_TRANSCRIPT_HASH" 43 - Sort the ephemeral keys and add the high labeled "EPHEMERAL_UPPER_PUBLIC_KEY" and the low keys labeled "EPHEMERAL_LOWER_PUBLIC_KEY" to the Merlin transcript. 44 - compute the Diffie-Hellman shared secret using the peers ephemeral public key and our ephemeral private key 45 - add the DH secret to the transcript labeled DH_SECRET. 46 - generate two keys to use for encryption (sending and receiving) and a challenge for authentication as follows: 47 - create a hkdf-sha256 instance with the key being the diffie hellman shared secret, and info parameter as 48 `TENDERMINT_SECRET_CONNECTION_KEY_AND_CHALLENGE_GEN` 49 - get 64 bytes of output from hkdf-sha256 50 - if we had the smaller ephemeral pubkey, use the first 32 bytes for the key for receiving, the second 32 bytes for sending; else the opposite. 51 - use a separate nonce for receiving and sending. Both nonces start at 0, and should support the full 96 bit nonce range 52 - all communications from now on are encrypted in 1400 byte frames (plus encoding overhead), 53 using the respective secret and nonce. Each nonce is incremented by one after each use. 54 - we now have an encrypted channel, but still need to authenticate 55 - extract a 32 bytes challenge from merlin transcript with the label "SECRET_CONNECTION_MAC" 56 - sign the common challenge obtained from the hkdf with our persistent private key 57 - send the amino encoded persistent pubkey and signature to the peer 58 - wait to receive the persistent public key and signature from the peer 59 - verify the signature on the challenge using the peer's persistent public key 60 61 If this is an outgoing connection (we dialed the peer) and we used a peer ID, 62 then finally verify that the peer's persistent public key corresponds to the peer ID we dialed, 63 ie. `peer.PubKey.Address() == <ID>`. 64 65 The connection has now been authenticated. All traffic is encrypted. 66 67 Note: only the dialer can authenticate the identity of the peer, 68 but this is what we care about since when we join the network we wish to 69 ensure we have reached the intended peer (and are not being MITMd). 70 71 ### Peer Filter 72 73 Before continuing, we check if the new peer has the same ID as ourselves or 74 an existing peer. If so, we disconnect. 75 76 We also check the peer's address and public key against 77 an optional whitelist which can be managed through the ABCI app - 78 if the whitelist is enabled and the peer does not qualify, the connection is 79 terminated. 80 81 ### CometBFT Version Handshake 82 83 The CometBFT Version Handshake allows the peers to exchange their NodeInfo: 84 85 ```golang 86 type NodeInfo struct { 87 Version p2p.Version 88 ID p2p.ID 89 ListenAddr string 90 91 Network string 92 SoftwareVersion string 93 Channels []int8 94 95 Moniker string 96 Other NodeInfoOther 97 } 98 99 type Version struct { 100 P2P uint64 101 Block uint64 102 App uint64 103 } 104 105 type NodeInfoOther struct { 106 TxIndex string 107 RPCAddress string 108 } 109 ``` 110 111 The connection is disconnected if: 112 113 - `peer.NodeInfo.ID` is not equal `peerConn.ID` 114 - `peer.NodeInfo.Version.Block` does not match ours 115 - `peer.NodeInfo.Network` is not the same as ours 116 - `peer.Channels` does not intersect with our known Channels. 117 - `peer.NodeInfo.ListenAddr` is malformed or is a DNS host that cannot be 118 resolved 119 120 At this point, if we have not disconnected, the peer is valid. 121 It is added to the switch and hence all reactors via the `AddPeer` method. 122 Note that each reactor may handle multiple channels. 123 124 ## Connection Activity 125 126 Once a peer is added, incoming messages for a given reactor are handled through 127 that reactor's `Receive` method, and output messages are sent directly by the Reactors 128 on each peer. A typical reactor maintains per-peer go-routine(s) that handle this.