libp2p / py-libp2p

The Python implementation of the libp2p networking stack 🐍 [under development]
https://libp2p.io
Other
479 stars 106 forks source link

Implement noise-libp2p secure channel handshake #375

Open hwwhww opened 4 years ago

hwwhww commented 4 years ago

What was wrong?

Spec: https://github.com/libp2p/specs/blob/master/noise/README.md

Noise protocol Python implementations:

  1. https://github.com/tgalal/dissononce
  2. https://github.com/plizonczyk/noiseprotocol

How can it be fixed?

Fill this section in if you know how this could or should be fixed.

mhchia commented 4 years ago

Forgot to assign to myself previously. I will have a PR for it soon.

tintinweb commented 4 years ago

Hey @mhchia @hwwhww, just a quick question: what is the status of the noise implementation? While secio works noise seems to break when talking to a nimbus node.

this is my setup (taken from the trinity host implementation):

https://github.com/ethereum/trinity/blob/81beae51ab34fe2ac57de3cf301730b7522fbcf0/trinity/nodes/beacon/host.py#L58-L76

        muxer_transports_by_protocol = {MPLEX_PROTOCOL_ID: Mplex}
        noise_key = ed25519.create_new_key_pair()
        security_transports_by_protocol = {
            TProtocol(secio.ID): secio.Transport(
                key_pair
            ),
            TProtocol(noise.PROTOCOL_ID): noise.Transport(
                key_pair, noise_key.private_key
            )
        }
        upgrader = TransportUpgrader(
            security_transports_by_protocol, muxer_transports_by_protocol
        )
TRC 2020-08-05 10:28:10+02:00 upgrading incoming connection              topics="switch" tid=12728997 file=switch.nim:345 conn= oid=5f2a6d9a121ed2455a7ef81a
TRC 2020-08-05 10:28:10+02:00 initiating handshake                       topics="multistream" tid=12728997 file=multistream.nim:48 codec="\x13/multistream/1.0.0\n"
TRC 2020-08-05 10:28:10+02:00 registering proto handler                  topics="switch" tid=12728997 file=multistream.nim:179 codec=/noise
TRC 2020-08-05 10:28:10+02:00 handle: starting multistream handling      topics="multistream" tid=12728997 file=multistream.nim:115
TRC 2020-08-05 10:28:10+02:00 handle: got request for                    topics="multistream" tid=12728997 file=multistream.nim:121 ms=/noise
TRC 2020-08-05 10:28:10+02:00 found handler for                          topics="multistream" tid=12728997 file=multistream.nim:143 protocol=/noise
TRC 2020-08-05 10:28:10+02:00 Securing connection                        topics="switch" tid=12728997 file=switch.nim:354 oid=5f2a6d9a121ed2455a7ef81a
TRC 2020-08-05 10:28:10+02:00 Starting Noise handshake                   topics="noise" tid=12728997 file=noise.nim:424 initiator=false peer=
TRC 2020-08-05 10:28:10+02:00 mixHash                                    topics="noise" tid=12728997 file=noise.nim:161 hash=f3d15e6108ed...2e0958dc002d
TRC 2020-08-05 10:28:10+02:00 receiveHSMessage                           topics="noise" tid=12728997 file=noise.nim:270 size=32
TRC 2020-08-05 10:28:10+02:00 noise read e                               topics="noise" tid=12728997 file=noise.nim:237 size=32
TRC 2020-08-05 10:28:10+02:00 mixHash                                    topics="noise" tid=12728997 file=noise.nim:161 hash=8171ce2536bc...ed05a59d7336
TRC 2020-08-05 10:28:10+02:00 mixHash                                    topics="noise" tid=12728997 file=noise.nim:161 hash=eec8b496546e...f6c3080d1a77
TRC 2020-08-05 10:28:10+02:00 noise write e                              topics="noise" tid=12728997 file=noise.nim:198
TRC 2020-08-05 10:28:10+02:00 mixHash                                    topics="noise" tid=12728997 file=noise.nim:161 hash=6505794d08c0...46131098ab07
TRC 2020-08-05 10:28:10+02:00 noise dh ee                                topics="noise" tid=12728997 file=noise.nim:210
TRC 2020-08-05 10:28:10+02:00 mixKey                                     topics="noise" tid=12728997 file=noise.nim:153 key=11a4329b45e6...5b65e144b575
TRC 2020-08-05 10:28:10+02:00 noise write s                              topics="noise" tid=12728997 file=noise.nim:205
TRC 2020-08-05 10:28:10+02:00 encryptWithAd                              topics="noise" tid=12728997 file=noise.nim:122 data=9b34f1acd4fe...f5e0dcb39021 nonce=0 tag=b88611b8c81152ef967ef5e0dcb39021
TRC 2020-08-05 10:28:10+02:00 mixHash                                    topics="noise" tid=12728997 file=noise.nim:161 hash=989886d47b3d...cc3ba09af0a0
TRC 2020-08-05 10:28:10+02:00 noise dh es                                topics="noise" tid=12728997 file=noise.nim:215
TRC 2020-08-05 10:28:10+02:00 mixKey                                     topics="noise" tid=12728997 file=noise.nim:153 key=c5c98e894c02...86763b4b58a1
TRC 2020-08-05 10:28:10+02:00 encryptWithAd                              topics="noise" tid=12728997 file=noise.nim:122 data=29751b60cb93...63d5c312140e nonce=0 tag=20e80f89ac5df90296ad63d5c312140e
TRC 2020-08-05 10:28:10+02:00 mixHash                                    topics="noise" tid=12728997 file=noise.nim:161 hash=c935b9ebe8b7...109fd5790d04
TRC 2020-08-05 10:28:10+02:00 sendHSMessage                              topics="noise" tid=12728997 file=noise.nim:283 size=207
TRC 2020-08-05 10:28:10+02:00 receiveHSMessage                           topics="noise" tid=12728997 file=noise.nim:270 size=208
TRC 2020-08-05 10:28:10+02:00 noise read s                               topics="noise" tid=12728997 file=noise.nim:248 size=208
TRC 2020-08-05 10:28:10+02:00 decryptWithAd                              topics="noise" tid=12728997 file=noise.nim:132 nonce=1 tagIn=9f6b2a945be3...865cc23353aa tagOut=6d1d74e680ef...af13a489d30e
ERR 2020-08-05 10:28:10+02:00 decryptWithAd failed                       topics="noise" tid=12728997 file=noise.nim:134 data=00000000000000000000000000000000000000000000000000000000000000009f6b2a945be374b37a2f865cc23353aa
DBG 2020-08-05 10:28:10+02:00 ending secured handler                     topics="switch" tid=12728997 file=switch.nim:375 err="decryptWithAd failed tag authentication."
TRC 2020-08-05 10:28:10+02:00 leaving multistream loop                   topics="multistream" tid=12728997 file=multistream.nim:156
 peers: 0 ❯ finalized: a3b90355:108 ❯ head: 2f569f7b:110:3 ❯ time: 8287:6 (265190)                                                                                                       ETH: 0 

for example, noise fails for this node: /ip4/18.230.134.176/tcp/9000/p2p/16Uiu2HAmKUXZzaRYJ21ENFiTcoWScUbbnq3wX23mkz616YybjcKN even though it is supported.

mhchia commented 4 years ago

Hi @tintinweb , sorry I haven't worked on py-libp2p implementation for a while. By the time I was working on the noise protocol in py-libp2p, we were able to talk to go-libp2p-noise@v0.0.0-20200203154915-813ed1ee6a46, as shown in this CI result. I didn't try to talk with nim's implementation at that time so I'm not sure what results in this failure.

ralexstokes commented 4 years ago

Hi @tintinweb , we have been chatting on the gitter but just to circle around here I think there are some interoperability bugs to work through across client implementations.

If you get a chance to look at py-libp2p support for other libp2p implementations, we would love a PR :)