Author: nextgens
Date: 2008-04-15 06:30:42 +0000 (Tue, 15 Apr 2008)
New Revision: 19342

Modified:
   trunk/freenet/src/freenet/keys/ClientCHKBlock.java
   trunk/freenet/src/freenet/node/FNPPacketMangler.java
   trunk/freenet/src/freenet/node/LocationManager.java
Log:
Better error handling as requested by toad

Modified: trunk/freenet/src/freenet/keys/ClientCHKBlock.java
===================================================================
--- trunk/freenet/src/freenet/keys/ClientCHKBlock.java  2008-04-15 04:59:57 UTC 
(rev 19341)
+++ trunk/freenet/src/freenet/keys/ClientCHKBlock.java  2008-04-15 06:30:42 UTC 
(rev 19342)
@@ -89,9 +89,8 @@
             throw new CHKDecodeException("Crypto key too short");
         cipher.initialize(key.cryptoKey);
         PCFBMode pcfb = PCFBMode.create(cipher);
-       int headersLength = headers.length;
-        byte[] hbuf = new byte[headersLength > 2 ? headersLength-2 : 0];
-        System.arraycopy(headers, 2, hbuf, 0, hbuf.length);
+       byte[] hbuf = new byte[headers.length-2];
+       System.arraycopy(headers, 2, hbuf, 0, headers.length-2);
         byte[] dbuf = new byte[data.length];
         System.arraycopy(data, 0, dbuf, 0, data.length);
         // Decipher header first - functions as IV

Modified: trunk/freenet/src/freenet/node/FNPPacketMangler.java
===================================================================
--- trunk/freenet/src/freenet/node/FNPPacketMangler.java        2008-04-15 
04:59:57 UTC (rev 19341)
+++ trunk/freenet/src/freenet/node/FNPPacketMangler.java        2008-04-15 
06:30:42 UTC (rev 19342)
@@ -1016,12 +1016,16 @@
                BlockCipher c = null;
                try { c = new Rijndael(256, 256); } catch 
(UnsupportedCipherException e) {}

-               final int expectedLength =      NONCE_SIZE*2 + 
DiffieHellman.modulusLengthInBytes()*2 +
-                                                                       
HASH_LENGTH + // authenticator
-                                                                       
HASH_LENGTH + // HMAC of the cyphertext
-                                                                       
(c.getBlockSize() >> 3) + // IV
-                                                                       
HASH_LENGTH + // it's at least a signature
-                                                                       8;      
                  // a bootid
+               final int expectedLength =      
+                       NONCE_SIZE*2 + // Ni, Nr
+                       DiffieHellman.modulusLengthInBytes()*2 + // g^i, g^r
+                       HASH_LENGTH + // authenticator
+                       HASH_LENGTH + // HMAC of the cyphertext
+                       (c.getBlockSize() >> 3) + // IV
+                       HASH_LENGTH + // it's at least a signature
+                       8 +           // a bootid
+                       1;            // znoderefI* is at least 1 byte long
+               
                if(payload.length < expectedLength + 3) {
                        Logger.error(this, "Packet too short from "+pn+": 
"+payload.length+" after decryption in JFK(3), should be "+(expectedLength + 
3));
                        return;
@@ -1124,7 +1128,7 @@
                byte[] data = new byte[decypheredPayload.length - 
decypheredPayloadOffset];
                System.arraycopy(decypheredPayload, decypheredPayloadOffset, 
data, 0, decypheredPayload.length - decypheredPayloadOffset);
                long bootID = Fields.bytesToLong(data);
-               byte[] hisRef = new byte[data.length > 8 ? data.length -8 : 0];
+               byte[] hisRef = new byte[data.length - 8];
                System.arraycopy(data, 8, hisRef, 0, hisRef.length);

                // construct the peernode
@@ -1247,12 +1251,14 @@
                BlockCipher c = null;
                try { c = new Rijndael(256, 256); } catch 
(UnsupportedCipherException e) {}

-               final int expectedLength =      HASH_LENGTH + // HMAC of the 
cyphertext
-                                                                       
(c.getBlockSize() >> 3) + // IV
-                                                                       
Node.SIGNATURE_PARAMETER_LENGTH * 2 + // the signature
-                                                                       
(bothNoderefs ? pn.jfkMyRef.length : 0) + // my reference
-                                                                       8 // 
bootID
-                                                                       ;
+               final int expectedLength =
+                       HASH_LENGTH + // HMAC of the cyphertext
+                       (c.getBlockSize() >> 3) + // IV
+                       Node.SIGNATURE_PARAMETER_LENGTH * 2 + // the signature
+                       (bothNoderefs ? pn.jfkMyRef.length : 0) + // my 
reference
+                       8+ // bootID
+                       1; // znoderefR
+
                if(payload.length - inputOffset < expectedLength + 3) {
                        if(!bothNoderefs)
                                Logger.error(this, "Packet too short from 
"+pn.getPeer()+": "+payload.length+" after decryption in JFK(4), should be 
"+(expectedLength + 3));

Modified: trunk/freenet/src/freenet/node/LocationManager.java
===================================================================
--- trunk/freenet/src/freenet/node/LocationManager.java 2008-04-15 04:59:57 UTC 
(rev 19341)
+++ trunk/freenet/src/freenet/node/LocationManager.java 2008-04-15 06:30:42 UTC 
(rev 19342)
@@ -339,6 +339,10 @@
             // Now decode it

             long[] hisBufLong = Fields.bytesToLongs(hisBuf);
+           if(hisBufLong.length < 2) {
+                   Logger.error(this, "Bad buffer length (no random, no 
location)- malicious node? on "+uid);
+                   return;
+           }

             long hisRandom = hisBufLong[0];

@@ -349,7 +353,7 @@
             }
             registerKnownLocation(hisLoc);

-            double[] hisFriendLocs = new double[hisBufLong.length > 2 ? 
hisBufLong.length-2 : 0];
+            double[] hisFriendLocs = new double[hisBufLong.length-2];
             for(int i=0;i<hisFriendLocs.length;i++) {
                 hisFriendLocs[i] = Double.longBitsToDouble(hisBufLong[i+2]);
                 if((hisFriendLocs[i] < 0.0) || (hisFriendLocs[i] > 1.0)) {
@@ -534,9 +538,13 @@
                 }

                 // Now decode it
-                
-                long[] hisBufLong = Fields.bytesToLongs(hisBuf);
-                
+
+                   long[] hisBufLong = Fields.bytesToLongs(hisBuf);
+                   if(hisBufLong.length < 2) {
+                           Logger.error(this, "Bad buffer length (no random, 
no location)- malicious node? on " + uid);
+                           return;
+                   }
+
                 long hisRandom = hisBufLong[0];

                 double hisLoc = Double.longBitsToDouble(hisBufLong[1]);
@@ -546,7 +554,7 @@
                 }
                 registerKnownLocation(hisLoc);

-                double[] hisFriendLocs = new double[hisBufLong.length > 2 ? 
hisBufLong.length-2 : 0];
+                double[] hisFriendLocs = new double[hisBufLong.length-2];
                 for(int i=0;i<hisFriendLocs.length;i++) {
                     hisFriendLocs[i] = 
Double.longBitsToDouble(hisBufLong[i+2]);
                     if((hisFriendLocs[i] < 0.0) || (hisFriendLocs[i] > 1.0)) {


Reply via email to