mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-28 23:08:52 +01:00
Merge bitcoin/bitcoin#24748: test/BIP324: functional tests for v2 P2P encryption
bc9283c441[test] Add functional test to test early key response behaviour in BIP 324 (stratospher)ffe6a56d75[test] Check whether v2 TestNode performs downgrading (stratospher)ba737358a3[test] Add functional tests to test v2 P2P behaviour (stratospher)4115cf9956[test] Ignore BIP324 decoy messages (stratospher)8c054aa04d[test] Allow inbound and outbound connections supporting v2 P2P protocol (stratospher)382894c3ac[test] Reconnect using v1 P2P when v2 P2P terminates due to magic byte mismatch (stratospher)a94e350ac0[test] Build v2 P2P messages (stratospher)bb7bffed79[test] Use lock for sending P2P messages in test framework (stratospher)5b91fb14ab[test] Read v2 P2P messages (stratospher)05bddb20f5[test] Perform initial v2 handshake (stratospher)a049d1bd08[test] Introduce EncryptedP2PState object in P2PConnection (stratospher)b89fa59e71[test] Construct class to handle v2 P2P protocol functions (stratospher)8d6c848a48[test] Move MAGIC_BYTES to messages.py (stratospher)595ad4b168[test/crypto] Add ECDH (stratospher)4487b80517[rpc/net] Allow v2 p2p support in addconnection (stratospher) Pull request description: This PR introduces support for v2 P2P encryption(BIP 324) in the existing functional test framework and adds functional tests for the same. ### commits overview 1. introduces a new class `EncryptedP2PState` to store the keys, functions for performing the initial v2 handshake and encryption/decryption. 3. this class is used by `P2PConnection` in inbound/outbound connections to perform the initial v2 handshake before the v1 version handshake. Only after the initial v2 handshake is performed do application layer P2P messages(version, verack etc..) get exchanged. (in a v2 connection) - `v2_state` is the object of class `EncryptedP2PState` in `P2PConnection` used to store its keys, session-id etc. - a node [advertising](https://github.com/stratospher/blogosphere/blob/main/integration_test_bip324.md#advertising-to-support-v2-p2p) support for v2 P2P is different from a node actually [supporting v2 P2P](https://github.com/stratospher/blogosphere/blob/main/integration_test_bip324.md#supporting-v2-p2p) (differ when false advertisement of services occur) - introduce a boolean variable `supports_v2_p2p` in `P2PConnection` to denote if it supports v2 P2P. - introduce a boolean variable `advertises_v2_p2p` to denote whether `P2PConnection` which mimics peer behaviour advertises V2 P2P support. Default option is `False`. - In the test framework, you can create Inbound and Outbound connections to `TestNode` 1. During **Inbound Connections**, `P2PConnection` is the initiator [`TestNode` <--------- `P2PConnection`] - Case 1: - if the `TestNode` advertises/signals v2 P2P support (means `self.nodes[i]` set up with `"-v2transport=1"`), different behaviour will be exhibited based on whether: 1. `P2PConnection` supports v2 P2P 2. `P2PConnection` does not support v2 P2P - In a real world scenario, the initiator node would intrinsically know if they support v2 P2P based on whatever code they choose to run. However, in the test scenario where we mimic peer behaviour, we have no way of knowing if `P2PConnection` should support v2 P2P or not. So `supports_v2_p2p` boolean variable is used as an option to enable support for v2 P2P in `P2PConnection`. - Since the `TestNode` advertises v2 P2P support (using "-v2transport=1"), our initiator `P2PConnection` would send: 1. (if the `P2PConnection` supports v2 P2P) ellswift + garbage bytes to initiate the connection 2. (if the `P2PConnection` does not support v2 P2P) version message to initiate the connection - Case 2: - if the `TestNode` doesn't signal v2 P2P support; `P2PConnection` being the initiator would send version message to initiate a connection. 2. During **Outbound Connections** [TestNode --------> P2PConnection] - initiator `TestNode` would send: - (if the `P2PConnection` advertises v2 P2P) ellswift + garbage bytes to initiate the connection - (if the `P2PConnection` advertises v2 P2P) version message to initiate the connection - Suppose `P2PConnection` advertises v2 P2P support when it actually doesn't support v2 P2P (false advertisement scenario) - `TestNode` sends ellswift + garbage bytes - `P2PConnection` receives but can't process it and disconnects. - `TestNode` then tries using v1 P2P and sends version message - `P2PConnection` receives/processes this successfully and they communicate on v1 P2P 4. the encrypted P2P messages follow a different format - 3 byte length + 1-13 byte message_type + payload + 16 byte MAC 5. includes support for testing decoy messages and v2 connection downgrade(using false advertisement - when a v2 node makes an outbound connection to a node which doesn't support v2 but is advertised as v2 by some malicious intermediary) ### run the tests * functional test - `test/functional/p2p_v2_encrypted.py` `test/functional/p2p_v2_earlykeyresponse.py` I'm also super grateful to @ dhruv for his really valuable feedback on this branch. Also written a more elaborate explanation here - https://github.com/stratospher/blogosphere/blob/main/integration_test_bip324.md ACKs for top commit: naumenkogs: ACKbc9283c441mzumsande: Code Review ACKbc9283c441theStack: Code-review ACKbc9283c441glozow: ACKbc9283c441Tree-SHA512: 9b54ed27e925e1775e0e0d35e959cdbf2a9a1aab7bcf5d027e66f8b59780bdd0458a7a4311ddc7dd67657a4a2a2cd5034ead75524420d58a83f642a8304c9811
This commit is contained in:
@@ -27,7 +27,8 @@ from .authproxy import (
|
||||
serialization_fallback,
|
||||
)
|
||||
from .descriptors import descsum_create
|
||||
from .p2p import P2P_SUBVERSION
|
||||
from .messages import NODE_P2P_V2
|
||||
from .p2p import P2P_SERVICES, P2P_SUBVERSION
|
||||
from .util import (
|
||||
MAX_NODES,
|
||||
assert_equal,
|
||||
@@ -659,18 +660,30 @@ class TestNode():
|
||||
assert_msg += "with expected error " + expected_msg
|
||||
self._raise_assertion_error(assert_msg)
|
||||
|
||||
def add_p2p_connection(self, p2p_conn, *, wait_for_verack=True, send_version=True, **kwargs):
|
||||
def add_p2p_connection(self, p2p_conn, *, wait_for_verack=True, send_version=True, supports_v2_p2p=False, **kwargs):
|
||||
"""Add an inbound p2p connection to the node.
|
||||
|
||||
This method adds the p2p connection to the self.p2ps list and also
|
||||
returns the connection to the caller."""
|
||||
returns the connection to the caller.
|
||||
|
||||
When self.use_v2transport is True, TestNode advertises NODE_P2P_V2 service flag
|
||||
|
||||
An inbound connection is made from TestNode <------ P2PConnection
|
||||
- if TestNode doesn't advertise NODE_P2P_V2 service, P2PConnection sends version message and v1 P2P is followed
|
||||
- if TestNode advertises NODE_P2P_V2 service, (and if P2PConnections supports v2 P2P)
|
||||
P2PConnection sends ellswift bytes and v2 P2P is followed
|
||||
"""
|
||||
if 'dstport' not in kwargs:
|
||||
kwargs['dstport'] = p2p_port(self.index)
|
||||
if 'dstaddr' not in kwargs:
|
||||
kwargs['dstaddr'] = '127.0.0.1'
|
||||
|
||||
p2p_conn.p2p_connected_to_node = True
|
||||
p2p_conn.peer_connect(**kwargs, send_version=send_version, net=self.chain, timeout_factor=self.timeout_factor)()
|
||||
if self.use_v2transport:
|
||||
kwargs['services'] = kwargs.get('services', P2P_SERVICES) | NODE_P2P_V2
|
||||
supports_v2_p2p = self.use_v2transport and supports_v2_p2p
|
||||
p2p_conn.peer_connect(**kwargs, send_version=send_version, net=self.chain, timeout_factor=self.timeout_factor, supports_v2_p2p=supports_v2_p2p)()
|
||||
|
||||
self.p2ps.append(p2p_conn)
|
||||
p2p_conn.wait_until(lambda: p2p_conn.is_connected, check_connected=False)
|
||||
if send_version:
|
||||
@@ -701,7 +714,7 @@ class TestNode():
|
||||
|
||||
return p2p_conn
|
||||
|
||||
def add_outbound_p2p_connection(self, p2p_conn, *, wait_for_verack=True, p2p_idx, connection_type="outbound-full-relay", **kwargs):
|
||||
def add_outbound_p2p_connection(self, p2p_conn, *, wait_for_verack=True, p2p_idx, connection_type="outbound-full-relay", supports_v2_p2p=False, advertise_v2_p2p=False, **kwargs):
|
||||
"""Add an outbound p2p connection from node. Must be an
|
||||
"outbound-full-relay", "block-relay-only", "addr-fetch" or "feeler" connection.
|
||||
|
||||
@@ -711,14 +724,37 @@ class TestNode():
|
||||
p2p_idx must be different for simultaneously connected peers. When reusing it for the next peer
|
||||
after disconnecting the previous one, it is necessary to wait for the disconnect to finish to avoid
|
||||
a race condition.
|
||||
|
||||
Parameters:
|
||||
supports_v2_p2p: whether p2p_conn supports v2 P2P or not
|
||||
advertise_v2_p2p: whether p2p_conn is advertised to support v2 P2P or not
|
||||
|
||||
An outbound connection is made from TestNode -------> P2PConnection
|
||||
- if P2PConnection doesn't advertise_v2_p2p, TestNode sends version message and v1 P2P is followed
|
||||
- if P2PConnection both supports_v2_p2p and advertise_v2_p2p, TestNode sends ellswift bytes and v2 P2P is followed
|
||||
- if P2PConnection doesn't supports_v2_p2p but advertise_v2_p2p,
|
||||
TestNode sends ellswift bytes and P2PConnection disconnects,
|
||||
TestNode reconnects by sending version message and v1 P2P is followed
|
||||
"""
|
||||
|
||||
def addconnection_callback(address, port):
|
||||
self.log.debug("Connecting to %s:%d %s" % (address, port, connection_type))
|
||||
self.addconnection('%s:%d' % (address, port), connection_type)
|
||||
self.addconnection('%s:%d' % (address, port), connection_type, advertise_v2_p2p)
|
||||
|
||||
p2p_conn.p2p_connected_to_node = False
|
||||
p2p_conn.peer_accept_connection(connect_cb=addconnection_callback, connect_id=p2p_idx + 1, net=self.chain, timeout_factor=self.timeout_factor, **kwargs)()
|
||||
if advertise_v2_p2p:
|
||||
kwargs['services'] = kwargs.get('services', P2P_SERVICES) | NODE_P2P_V2
|
||||
assert self.use_v2transport # only a v2 TestNode could make a v2 outbound connection
|
||||
|
||||
# if P2PConnection is advertised to support v2 P2P when it doesn't actually support v2 P2P,
|
||||
# reconnection needs to be attempted using v1 P2P by sending version message
|
||||
reconnect = advertise_v2_p2p and not supports_v2_p2p
|
||||
# P2PConnection needs to be advertised to support v2 P2P so that ellswift bytes are sent instead of msg_version
|
||||
supports_v2_p2p = supports_v2_p2p and advertise_v2_p2p
|
||||
p2p_conn.peer_accept_connection(connect_cb=addconnection_callback, connect_id=p2p_idx + 1, net=self.chain, timeout_factor=self.timeout_factor, supports_v2_p2p=supports_v2_p2p, reconnect=reconnect, **kwargs)()
|
||||
|
||||
if reconnect:
|
||||
p2p_conn.wait_for_reconnect()
|
||||
|
||||
if connection_type == "feeler":
|
||||
# feeler connections are closed as soon as the node receives a `version` message
|
||||
|
||||
Reference in New Issue
Block a user