|
| 1 | +Py-libp2p – TLS Support Documentation |
| 2 | +====================================================== |
| 3 | + |
| 4 | +.. contents:: |
| 5 | + :depth: 2 |
| 6 | + :local: |
| 7 | + |
| 8 | +Overview of TLS in Libp2p |
| 9 | +------------------------- |
| 10 | + |
| 11 | +**Purpose of TLS in P2P networking** |
| 12 | + |
| 13 | +- Encrypts data between peers. |
| 14 | +- Authenticates peer identity using certificates. |
| 15 | +- Prevents man-in-the-middle attacks. |
| 16 | + |
| 17 | +**Integration in libp2p security modules** |
| 18 | + |
| 19 | +- TLS is one of the supported secure channel protocols (alongside Noise). |
| 20 | +- Negotiated during connection setup. |
| 21 | + |
| 22 | +**Current status** |
| 23 | + |
| 24 | +- **py-libp2p**: Experimental, usable for local and interop tests. |
| 25 | +- **go-libp2p / js-libp2p**: Stable and production-ready. |
| 26 | + |
| 27 | +Installation Requirements |
| 28 | +------------------------- |
| 29 | + |
| 30 | +**Additional dependencies** |
| 31 | + |
| 32 | +Ubuntu / Debian: |
| 33 | + |
| 34 | +.. code-block:: bash |
| 35 | +
|
| 36 | + sudo apt install build-essential python3-dev libffi-dev libssl-dev |
| 37 | +
|
| 38 | +macOS: |
| 39 | + |
| 40 | +.. code-block:: bash |
| 41 | +
|
| 42 | + brew install openssl |
| 43 | +
|
| 44 | +Enabling TLS in py-libp2p |
| 45 | +------------------------- |
| 46 | + |
| 47 | +**Working example – Listener and Dialer** |
| 48 | + |
| 49 | +Listener node: |
| 50 | + |
| 51 | +.. code-block:: python |
| 52 | +
|
| 53 | + import trio |
| 54 | + import multiaddr |
| 55 | + from libp2p import new_host |
| 56 | + from libp2p.crypto.secp256k1 import create_new_key_pair |
| 57 | + from libp2p.security.tls.transport import PROTOCOL_ID, TLSTransport |
| 58 | +
|
| 59 | + async def main(): |
| 60 | + key_pair = create_new_key_pair(secret=None) |
| 61 | + tls_transport = TLSTransport(libp2p_keypair=key_pair) |
| 62 | + sec_opt = {PROTOCOL_ID: tls_transport} |
| 63 | + host = new_host(key_pair=key_pair, sec_opt=sec_opt) |
| 64 | + listen_addr = multiaddr.Multiaddr("/ip4/0.0.0.0/tcp/8000") |
| 65 | + async with host.run(listen_addrs=[listen_addr]): |
| 66 | + while not host.get_addrs(): |
| 67 | + await trio.sleep(0.1) |
| 68 | + addrs = host.get_addrs() |
| 69 | + peer_id = host.get_id() |
| 70 | + print("TLS-enabled listener at:", addrs[0] if addrs else "No addresses") |
| 71 | + print("Peer ID:", peer_id) |
| 72 | + print("\nUse this address with the dialer:") |
| 73 | + print(f" /ip4/127.0.0.1/tcp/8000/p2p/{peer_id}") |
| 74 | + await trio.sleep_forever() |
| 75 | +
|
| 76 | + if __name__ == "__main__": |
| 77 | + trio.run(main) |
| 78 | +
|
| 79 | +Dialer node: |
| 80 | + |
| 81 | +.. code-block:: python |
| 82 | +
|
| 83 | + import trio |
| 84 | + import multiaddr |
| 85 | + from libp2p import new_host |
| 86 | + from libp2p.crypto.secp256k1 import create_new_key_pair |
| 87 | + from libp2p.security.tls.transport import PROTOCOL_ID, TLSTransport |
| 88 | + from libp2p.peer.peerinfo import info_from_p2p_addr |
| 89 | +
|
| 90 | + async def main(): |
| 91 | + key_pair = create_new_key_pair(secret=None) |
| 92 | + tls_transport = TLSTransport(libp2p_keypair=key_pair) |
| 93 | + sec_opt = {PROTOCOL_ID: tls_transport} |
| 94 | + host = new_host(key_pair=key_pair, sec_opt=sec_opt) |
| 95 | +
|
| 96 | + addr = "/ip4/127.0.0.1/tcp/8000/p2p/16Uiu2HAm3hATVnBDT13acn2utRJXsFa2LRRGrZwDsosJ1mFZsM2Q" |
| 97 | + maddr = multiaddr.Multiaddr(addr) |
| 98 | + peer_info = info_from_p2p_addr(maddr) |
| 99 | +
|
| 100 | + async with host.run(listen_addrs=[]): |
| 101 | + await trio.sleep(0.5) |
| 102 | + host.peerstore.add_addrs(peer_info.peer_id, peer_info.addrs, 120) |
| 103 | +
|
| 104 | + try: |
| 105 | + await host.connect(peer_info) |
| 106 | + print("Connected securely to", peer_info.peer_id) |
| 107 | + await trio.sleep(1) |
| 108 | + except Exception as e: |
| 109 | + print(f"Connection failed: {e}") |
| 110 | + raise |
| 111 | +
|
| 112 | + if __name__ == "__main__": |
| 113 | + trio.run(main) |
| 114 | +
|
| 115 | +**Defaults if no configuration is provided** |
| 116 | + |
| 117 | +- Generates a self-signed certificate automatically. |
| 118 | + |
| 119 | +**Note for testing with self-signed certificates** |
| 120 | + |
| 121 | +When testing with self-signed certificates, peers need to trust each other's certificates. |
| 122 | +You can do this by calling ``trust_peer_cert_pem()`` on the TLS transport before creating the host: |
| 123 | + |
| 124 | +.. code-block:: python |
| 125 | +
|
| 126 | + # For testing: trust peer certificates |
| 127 | + listener_tls.trust_peer_cert_pem(dialer_tls.get_certificate_pem()) |
| 128 | + dialer_tls.trust_peer_cert_pem(listener_tls.get_certificate_pem()) |
| 129 | +
|
| 130 | +Certificate Management |
| 131 | +---------------------- |
| 132 | + |
| 133 | +**Generate a development certificate** |
| 134 | + |
| 135 | +.. code-block:: bash |
| 136 | +
|
| 137 | + openssl req -x509 -newkey rsa:2048 \ |
| 138 | + -keyout key.pem -out cert.pem \ |
| 139 | + -days 365 -nodes -subj "/CN=py-libp2p" |
| 140 | +
|
| 141 | +- Store keys outside version control. |
| 142 | +- Rotate certificates every 90 days in production. |
| 143 | + |
| 144 | +Testing TLS Connections |
| 145 | +----------------------- |
| 146 | + |
| 147 | +**Local test steps** |
| 148 | + |
| 149 | +1. Run the listener example. |
| 150 | +2. Start the dialer with the listener's multiaddress. |
| 151 | +3. Confirm the secure connection in logs. |
| 152 | + |
| 153 | +**Interop testing** |
| 154 | + |
| 155 | +- Ensure both nodes advertise `/tls/1.0.0`. |
| 156 | +- Peer IDs must match certificate public keys. |
| 157 | + |
| 158 | +Security Considerations |
| 159 | +----------------------- |
| 160 | + |
| 161 | +- Never disable certificate verification in production. |
| 162 | +- Use TLS 1.3 or later. |
| 163 | +- Pin certificates for critical peers. |
| 164 | + |
| 165 | +Troubleshooting |
| 166 | +--------------- |
| 167 | + |
| 168 | +.. list-table:: |
| 169 | + :header-rows: 1 |
| 170 | + :widths: 30 30 40 |
| 171 | + |
| 172 | + * - Problem |
| 173 | + - Cause |
| 174 | + - Solution |
| 175 | + * - Certificate not trusted |
| 176 | + - Self-signed without trust store entry |
| 177 | + - Add cert to local trust store or disable verification **only** in testing. |
| 178 | + * - Protocol negotiation failed |
| 179 | + - One peer does not support `/tls/1.0.0` |
| 180 | + - Enable TLS on both peers or use Noise. |
| 181 | + * - SSL handshake failure |
| 182 | + - TLS version mismatch or clock skew |
| 183 | + - Enforce TLS 1.3, sync system clock. |
| 184 | + * - Connection refused |
| 185 | + - Port blocked or listener not running |
| 186 | + - Check firewall rules and listener status. |
0 commit comments