From cc2056d4cfff41a64c05ed61e24524b9f1ca57de Mon Sep 17 00:00:00 2001
From: zzz <zzz@mail.i2p>
Date: Thu, 23 Aug 2018 15:25:45 +0000
Subject: [PATCH] NTCP2 code consolidation, minor changes

---
 .../net/i2p/data/router/RouterAddress.java    |  2 ++
 .../src/net/i2p/router/RouterVersion.java     |  2 +-
 .../transport/crypto/X25519KeyFactory.java    |  1 -
 .../transport/ntcp/InboundEstablishState.java | 14 +++----------
 .../router/transport/ntcp/NTCP2Options.java   | 21 +++++++++++++++++++
 .../router/transport/ntcp/NTCPConnection.java | 13 ++----------
 .../router/transport/ntcp/NTCPTransport.java  |  4 +++-
 7 files changed, 32 insertions(+), 25 deletions(-)

diff --git a/router/java/src/net/i2p/data/router/RouterAddress.java b/router/java/src/net/i2p/data/router/RouterAddress.java
index ebdf27bbac..a56c31c3fe 100644
--- a/router/java/src/net/i2p/data/router/RouterAddress.java
+++ b/router/java/src/net/i2p/data/router/RouterAddress.java
@@ -288,6 +288,8 @@ public class RouterAddress extends DataStructureImpl {
             _transportStyle = "SSU";
         else if (_transportStyle.equals("NTCP"))
             _transportStyle = "NTCP";
+        else if (_transportStyle.equals("NTCP2"))
+            _transportStyle = "NTCP2";
         DataHelper.readProperties(in, _options);
     }
     
diff --git a/router/java/src/net/i2p/router/RouterVersion.java b/router/java/src/net/i2p/router/RouterVersion.java
index a411b00998..87e5bffca0 100644
--- a/router/java/src/net/i2p/router/RouterVersion.java
+++ b/router/java/src/net/i2p/router/RouterVersion.java
@@ -18,7 +18,7 @@ public class RouterVersion {
     /** deprecated */
     public final static String ID = "Monotone";
     public final static String VERSION = CoreVersion.VERSION;
-    public final static long BUILD = 0;
+    public final static long BUILD = 1;
 
     /** for example "-test" */
     public final static String EXTRA = "";
diff --git a/router/java/src/net/i2p/router/transport/crypto/X25519KeyFactory.java b/router/java/src/net/i2p/router/transport/crypto/X25519KeyFactory.java
index c7b2289b4c..b0c16c499a 100644
--- a/router/java/src/net/i2p/router/transport/crypto/X25519KeyFactory.java
+++ b/router/java/src/net/i2p/router/transport/crypto/X25519KeyFactory.java
@@ -66,7 +66,6 @@ public class X25519KeyFactory extends I2PThread {
      *  Note that this stops the singleton precalc thread.
      *  You don't want to do this if there are multiple routers in the JVM.
      *  Fix this if you care. See Router.shutdown().
-     *  @since 0.8.8
      */
     public void shutdown() {
         _isRunning = false;
diff --git a/router/java/src/net/i2p/router/transport/ntcp/InboundEstablishState.java b/router/java/src/net/i2p/router/transport/ntcp/InboundEstablishState.java
index daf339d07c..5b6d912e4e 100644
--- a/router/java/src/net/i2p/router/transport/ntcp/InboundEstablishState.java
+++ b/router/java/src/net/i2p/router/transport/ntcp/InboundEstablishState.java
@@ -1006,21 +1006,13 @@ class InboundEstablishState extends EstablishBase implements NTCP2Payload.Payloa
 
     /** @since 0.9.36 */
     public void gotOptions(byte[] options, boolean isHandshake) {
-        if (options.length < 12) {
+        NTCP2Options hisPadding = NTCP2Options.fromByteArray(options);
+        if (hisPadding == null) {
             if (_log.shouldWarn())
                 _log.warn("Got options length " + options.length + " on: " + this);
             return;
         }
-        float tmin = (options[0] & 0xff) / 16.0f;
-        float tmax = (options[1] & 0xff) / 16.0f;
-        float rmin = (options[2] & 0xff) / 16.0f;
-        float rmax = (options[3] & 0xff) / 16.0f;
-        int tdummy = (int) DataHelper.fromLong(options, 4, 2);
-        int rdummy = (int) DataHelper.fromLong(options, 6, 2);
-        int tdelay = (int) DataHelper.fromLong(options, 8, 2);
-        int rdelay = (int) DataHelper.fromLong(options, 10, 2);
-        _hisPadding = new NTCP2Options(tmin, tmax, rmin, rmax,
-                                       tdummy, rdummy, tdelay, rdelay);
+        _hisPadding = hisPadding;
     }
 
     /** @since 0.9.36 */
diff --git a/router/java/src/net/i2p/router/transport/ntcp/NTCP2Options.java b/router/java/src/net/i2p/router/transport/ntcp/NTCP2Options.java
index 96a750d1f9..97f2517080 100644
--- a/router/java/src/net/i2p/router/transport/ntcp/NTCP2Options.java
+++ b/router/java/src/net/i2p/router/transport/ntcp/NTCP2Options.java
@@ -1,5 +1,7 @@
 package net.i2p.router.transport.ntcp;
 
+import net.i2p.data.DataHelper;
+
 /**
  *
  *  NTCP2 Padding/Dummy/Delay configuration for data phase.
@@ -60,6 +62,25 @@ class NTCP2Options {
                                 xsDummy, xrDummy, xsDelay, xrDelay);
     }
 
+    /**
+     *  @since 0.9.37 consolidated from two places
+     *  @return null on error
+     */
+    public static NTCP2Options fromByteArray(byte[] options) {
+        if (options.length < 12)
+            return null;
+        float tmin = (options[0] & 0xff) / 16.0f;
+        float tmax = (options[1] & 0xff) / 16.0f;
+        float rmin = (options[2] & 0xff) / 16.0f;
+        float rmax = (options[3] & 0xff) / 16.0f;
+        int tdummy = (int) DataHelper.fromLong(options, 4, 2);
+        int rdummy = (int) DataHelper.fromLong(options, 6, 2);
+        int tdelay = (int) DataHelper.fromLong(options, 8, 2);
+        int rdelay = (int) DataHelper.fromLong(options, 10, 2);
+        return new NTCP2Options(tmin, tmax, rmin, rmax,
+                                tdummy, rdummy, tdelay, rdelay);
+    }
+
     @Override
     public String toString() {
         return "Padding options: send min/max %: (" + (_sendMin * 100) + ", " + (_sendMax * 100) +
diff --git a/router/java/src/net/i2p/router/transport/ntcp/NTCPConnection.java b/router/java/src/net/i2p/router/transport/ntcp/NTCPConnection.java
index b589bd070a..6e86e6892f 100644
--- a/router/java/src/net/i2p/router/transport/ntcp/NTCPConnection.java
+++ b/router/java/src/net/i2p/router/transport/ntcp/NTCPConnection.java
@@ -2131,21 +2131,12 @@ public class NTCPConnection implements Closeable {
         }
 
         public void gotOptions(byte[] options, boolean isHandshake) {
-            if (options.length < 12) {
+            NTCP2Options hisPadding = NTCP2Options.fromByteArray(options);
+            if (hisPadding == null) {
                 if (_log.shouldWarn())
                     _log.warn("Got options length " + options.length + " on: " + this);
                 return;
             }
-            float tmin = (options[0] & 0xff) / 16.0f;
-            float tmax = (options[1] & 0xff) / 16.0f;
-            float rmin = (options[2] & 0xff) / 16.0f;
-            float rmax = (options[3] & 0xff) / 16.0f;
-            int tdummy = (int) DataHelper.fromLong(options, 4, 2);
-            int rdummy = (int) DataHelper.fromLong(options, 6, 2);
-            int tdelay = (int) DataHelper.fromLong(options, 8, 2);
-            int rdelay = (int) DataHelper.fromLong(options, 10, 2);
-            NTCP2Options hisPadding = new NTCP2Options(tmin, tmax, rmin, rmax,
-                                                       tdummy, rdummy, tdelay, rdelay);
             _paddingConfig = OUR_PADDING.merge(hisPadding);
             if (_log.shouldDebug())
                 _log.debug("Got padding options:" +
diff --git a/router/java/src/net/i2p/router/transport/ntcp/NTCPTransport.java b/router/java/src/net/i2p/router/transport/ntcp/NTCPTransport.java
index 3b58a78e9e..191adea717 100644
--- a/router/java/src/net/i2p/router/transport/ntcp/NTCPTransport.java
+++ b/router/java/src/net/i2p/router/transport/ntcp/NTCPTransport.java
@@ -273,7 +273,9 @@ public class NTCPTransport extends TransportImpl {
             }
             if (iv == null || iv.length != NTCP2_IV_LEN) {
                 iv = new byte[NTCP2_IV_LEN];
-                ctx.random().nextBytes(iv);
+                do {
+                    ctx.random().nextBytes(iv);
+                } while (DataHelper.eq(iv, 0, OutboundNTCP2State.ZEROKEY, 0, NTCP2_IV_LEN));
                 shouldSave = true;
             }
             if (shouldSave) {
-- 
GitLab