From 7df7e3304411b243a7c9ede06b38e8a65de7942c Mon Sep 17 00:00:00 2001 From: noot Date: Sun, 25 Aug 2019 03:42:29 -0400 Subject: [PATCH] fix if statement in runHandshake, clean up code --- p2p/security/noise/integration_test.go | 21 ++++++++++----------- p2p/security/noise/protocol.go | 14 +++++++------- p2p/security/noise/xx_handshake.go | 10 ++++++---- 3 files changed, 23 insertions(+), 22 deletions(-) diff --git a/p2p/security/noise/integration_test.go b/p2p/security/noise/integration_test.go index 6dc34ec7..2822dbea 100644 --- a/p2p/security/noise/integration_test.go +++ b/p2p/security/noise/integration_test.go @@ -14,7 +14,7 @@ import ( "io" mrand "math/rand" "testing" - "time" + //"time" ) func generateKey(seed int64) (crypto.PrivKey, error) { @@ -102,18 +102,17 @@ func TestLibp2pIntegration(t *testing.T) { t.Fatal(err) } - stream, err := ha.NewStream(ctx, hb.ID(), ID) - if err != nil { - t.Fatal(err) - } + // stream, err := ha.NewStream(ctx, hb.ID(), ID) + // if err != nil { + // t.Fatal(err) + // } - _, err = stream.Write([]byte("hello\n")) - if err != nil { - t.Fatal(err) - } + // _, err = stream.Write([]byte("hello\n")) + // if err != nil { + // t.Fatal(err) + // } - time.Sleep(5 * time.Second) - fmt.Println("fin") + // fmt.Println("fin") } func handleStream(stream net.Stream) { diff --git a/p2p/security/noise/protocol.go b/p2p/security/noise/protocol.go index bd3dc7c5..20399c25 100644 --- a/p2p/security/noise/protocol.go +++ b/p2p/security/noise/protocol.go @@ -138,7 +138,7 @@ func (s *secureSession) verifyPayload(payload *pb.NoiseHandshakePayload, noiseKe func (s *secureSession) runHandshake(ctx context.Context) error { // if we have the peer's noise static key and we support noise pipes, we can try IK - if s.noiseStaticKeyCache[s.remotePeer] != [32]byte{} || s.noisePipesSupport { + if s.noiseStaticKeyCache[s.remotePeer] != [32]byte{} && s.noisePipesSupport { // known static key for peer, try IK // buf, err := s.runHandshake_ik(ctx) @@ -192,10 +192,10 @@ func (s *secureSession) Read(buf []byte) (int, error) { //return s.insecure.Read(buf) l := len(buf) - log.Debug("length", l) + //log.Debug("length", l) if l <= len(s.msgBuffer) { - log.Debug("length < msgbuffer") + //log.Debug("length < msgbuffer") copy(buf, s.msgBuffer) s.msgBuffer = s.msgBuffer[l:] @@ -204,7 +204,7 @@ func (s *secureSession) Read(buf []byte) (int, error) { l, err := s.readLength() if err != nil { - log.Error("read length err", err) + //log.Error("read length err", err) return 0, err } @@ -212,13 +212,13 @@ func (s *secureSession) Read(buf []byte) (int, error) { _, err = s.rw.Read(ciphertext) if err != nil { - log.Error("read ciphertext err", err) + //log.Error("read ciphertext err", err) return 0, err } plaintext, err := s.Decrypt(ciphertext) if err != nil { - log.Error("decrypt err", err) + //log.Error("decrypt err", err) return 0, err } @@ -228,7 +228,7 @@ func (s *secureSession) Read(buf []byte) (int, error) { s.msgBuffer = append(s.msgBuffer, plaintext[len(buf):]...) } - log.Debug("read", "plaintext", plaintext, len(plaintext)) + //log.Debug("read", "plaintext", plaintext, len(plaintext)) return c, nil } diff --git a/p2p/security/noise/xx_handshake.go b/p2p/security/noise/xx_handshake.go index 6ecc35fe..ea89877b 100644 --- a/p2p/security/noise/xx_handshake.go +++ b/p2p/security/noise/xx_handshake.go @@ -215,7 +215,9 @@ func (s *secureSession) runHandshake_xx(ctx context.Context, fallback bool, init log.Error("stage 2 initiator verify payload", "err", err) } - s.noiseStaticKeyCache[s.remotePeer] = s.xx_ns.RemoteKey() + if s.noisePipesSupport { + s.noiseStaticKeyCache[s.remotePeer] = s.xx_ns.RemoteKey() + } } else { @@ -278,8 +280,6 @@ func (s *secureSession) runHandshake_xx(ctx context.Context, fallback bool, init log.Debugf("stage 2 responder", "plaintext", plaintext, "remote key", s.xx_ns.RemoteKey()) - //copy(s.remote.noiseKey[:], plaintext) - // unmarshal payload err = proto.Unmarshal(plaintext, nhp) if err != nil { @@ -308,7 +308,9 @@ func (s *secureSession) runHandshake_xx(ctx context.Context, fallback bool, init return fmt.Errorf("stage 2 responder fail: %s", err) } - s.noiseStaticKeyCache[s.remotePeer] = s.remote.noiseKey + if s.noisePipesSupport { + s.noiseStaticKeyCache[s.remotePeer] = s.remote.noiseKey + } log.Debugf("stage 2 responder", "remote key", s.remote.noiseKey) }