Author: toad
Date: 2007-03-08 19:59:56 +0000 (Thu, 08 Mar 2007)
New Revision: 12041

Added:
   trunk/freenet/src/freenet/crypt/CryptFormatException.java
Modified:
   trunk/freenet/src/freenet/crypt/CryptoKey.java
   trunk/freenet/src/freenet/crypt/DSAGroup.java
   trunk/freenet/src/freenet/crypt/DSAPublicKey.java
   trunk/freenet/src/freenet/node/RequestSender.java
   trunk/freenet/src/freenet/node/SSKInsertHandler.java
   trunk/freenet/src/freenet/store/BerkeleyDBFreenetStore.java
Log:
Don't use IOException for failure to _parse_ the read data.

Added: trunk/freenet/src/freenet/crypt/CryptFormatException.java
===================================================================
--- trunk/freenet/src/freenet/crypt/CryptFormatException.java                   
        (rev 0)
+++ trunk/freenet/src/freenet/crypt/CryptFormatException.java   2007-03-08 
19:59:56 UTC (rev 12041)
@@ -0,0 +1,16 @@
+package freenet.crypt;
+
+import java.io.IOException;
+
+public class CryptFormatException extends Exception {
+
+       public CryptFormatException(String message) {
+               super(message);
+       }
+
+       public CryptFormatException(IOException e) {
+               super(e.getMessage());
+               initCause(e);
+       }
+
+}

Modified: trunk/freenet/src/freenet/crypt/CryptoKey.java
===================================================================
--- trunk/freenet/src/freenet/crypt/CryptoKey.java      2007-03-08 19:42:10 UTC 
(rev 12040)
+++ trunk/freenet/src/freenet/crypt/CryptoKey.java      2007-03-08 19:59:56 UTC 
(rev 12041)
@@ -19,7 +19,7 @@
        CryptoKey() {
        }

-       public static CryptoKey read(InputStream i) throws IOException {
+       public static CryptoKey read(InputStream i) throws IOException, 
CryptFormatException {
                DataInputStream dis = new DataInputStream(i);
                String type = dis.readUTF();
                try {
@@ -29,6 +29,8 @@
                        return (CryptoKey) m.invoke(null, new Object[] { dis });
                } catch (Exception e) {
                        e.printStackTrace();
+                       if (e instanceof CryptFormatException)
+                               throw (CryptFormatException) e;
                        if (e instanceof IOException)
                                throw (IOException) e;
                        return null;

Modified: trunk/freenet/src/freenet/crypt/DSAGroup.java
===================================================================
--- trunk/freenet/src/freenet/crypt/DSAGroup.java       2007-03-08 19:42:10 UTC 
(rev 12040)
+++ trunk/freenet/src/freenet/crypt/DSAGroup.java       2007-03-08 19:59:56 UTC 
(rev 12041)
@@ -76,7 +76,7 @@
     //    g = new BigInteger(str.nextToken(), 16);
     //    return new DSAGroup(p,q,g);
     //}
-    public static CryptoKey read(InputStream i) throws IOException {
+    public static CryptoKey read(InputStream i) throws IOException, 
CryptFormatException {
         BigInteger p, q, g;
         p = Util.readMPI(i);
         q = Util.readMPI(i);
@@ -84,7 +84,7 @@
         try {
                return new DSAGroup(p, q, g);
         } catch (IllegalArgumentException e) {
-               throw new IOException("Invalid group");
+               throw new CryptFormatException("Invalid group");
         }
     }


Modified: trunk/freenet/src/freenet/crypt/DSAPublicKey.java
===================================================================
--- trunk/freenet/src/freenet/crypt/DSAPublicKey.java   2007-03-08 19:42:10 UTC 
(rev 12040)
+++ trunk/freenet/src/freenet/crypt/DSAPublicKey.java   2007-03-08 19:59:56 UTC 
(rev 12041)
@@ -45,16 +45,20 @@
                this(g,g.getG().modPow(p.getX(), g.getP()));
     }

-    public DSAPublicKey(InputStream is) throws IOException {
+    public DSAPublicKey(InputStream is) throws IOException, 
CryptFormatException {
                group=(DSAGroup) DSAGroup.read(is);
                y=Util.readMPI(is);
                // FIXME should check y < group.something?
     }

-    public DSAPublicKey(byte[] pubkeyAsBytes) throws IOException {
-       this(new ByteArrayInputStream(pubkeyAsBytes));
-       }
-
+    public static DSAPublicKey create(byte[] pubkeyAsBytes) throws 
CryptFormatException {
+       try {
+                       return new DSAPublicKey(new 
ByteArrayInputStream(pubkeyAsBytes));
+               } catch (IOException e) {
+                       throw new CryptFormatException(e);
+               }
+    }
+    
        public BigInteger getY() {
                return y;
     }
@@ -95,7 +99,7 @@
 //             Util.writeMPI(y, out);
 //    }
 //
-    public static CryptoKey read(InputStream i) throws IOException {
+    public static CryptoKey read(InputStream i) throws IOException, 
CryptFormatException {
                return new DSAPublicKey(i);
     }


Modified: trunk/freenet/src/freenet/node/RequestSender.java
===================================================================
--- trunk/freenet/src/freenet/node/RequestSender.java   2007-03-08 19:42:10 UTC 
(rev 12040)
+++ trunk/freenet/src/freenet/node/RequestSender.java   2007-03-08 19:59:56 UTC 
(rev 12041)
@@ -6,6 +6,7 @@
 import java.io.IOException;
 import java.util.HashSet;

+import freenet.crypt.CryptFormatException;
 import freenet.crypt.DSAPublicKey;
 import freenet.io.comm.DMT;
 import freenet.io.comm.DisconnectedException;
@@ -354,13 +355,13 @@
                                byte[] pubkeyAsBytes = 
((ShortBuffer)msg.getObject(DMT.PUBKEY_AS_BYTES)).getData();
                                try {
                                        if(pubKey == null)
-                                               pubKey = new 
DSAPublicKey(pubkeyAsBytes);
+                                               pubKey = 
DSAPublicKey.create(pubkeyAsBytes);
                                        ((NodeSSK)key).setPubKey(pubKey);
                                } catch (SSKVerifyException e) {
                                        pubKey = null;
                                        Logger.error(this, "Invalid pubkey from 
"+source+" on "+uid+" ("+e.getMessage()+ ')', e);
                                        break; // try next node
-                               } catch (IOException e) {
+                               } catch (CryptFormatException e) {
                                        Logger.error(this, "Invalid pubkey from 
"+source+" on "+uid+" ("+e+ ')');
                                        break; // try next node
                                }

Modified: trunk/freenet/src/freenet/node/SSKInsertHandler.java
===================================================================
--- trunk/freenet/src/freenet/node/SSKInsertHandler.java        2007-03-08 
19:42:10 UTC (rev 12040)
+++ trunk/freenet/src/freenet/node/SSKInsertHandler.java        2007-03-08 
19:59:56 UTC (rev 12041)
@@ -5,6 +5,7 @@

 import java.io.IOException;

+import freenet.crypt.CryptFormatException;
 import freenet.crypt.DSAPublicKey;
 import freenet.io.comm.DMT;
 import freenet.io.comm.DisconnectedException;
@@ -107,7 +108,7 @@
                                }
                                byte[] pubkeyAsBytes = 
((ShortBuffer)pk.getObject(DMT.PUBKEY_AS_BYTES)).getData();
                                try {
-                                       pubKey = new 
DSAPublicKey(pubkeyAsBytes);
+                                       pubKey = 
DSAPublicKey.create(pubkeyAsBytes);
                                        if(logMINOR) Logger.minor(this, "Got 
pubkey on "+uid+" : "+pubKey);
                                        Message confirm = 
DMT.createFNPSSKPubKeyAccepted(uid);
                                        try {
@@ -116,7 +117,7 @@
                                                if(logMINOR) Logger.minor(this, 
"Lost connection to source on "+uid);
                                                return;
                                        }
-                               } catch (IOException e) {
+                               } catch (CryptFormatException e) {
                                        Logger.error(this, "Invalid pubkey from 
"+source+" on "+uid);
                                        Message msg = 
DMT.createFNPDataInsertRejected(uid, DMT.DATA_INSERT_REJECTED_SSK_ERROR);
                                        try {

Modified: trunk/freenet/src/freenet/store/BerkeleyDBFreenetStore.java
===================================================================
--- trunk/freenet/src/freenet/store/BerkeleyDBFreenetStore.java 2007-03-08 
19:42:10 UTC (rev 12040)
+++ trunk/freenet/src/freenet/store/BerkeleyDBFreenetStore.java 2007-03-08 
19:59:56 UTC (rev 12041)
@@ -28,6 +28,7 @@
 import com.sleepycat.je.SecondaryKeyCreator;
 import com.sleepycat.je.Transaction;

+import freenet.crypt.CryptFormatException;
 import freenet.crypt.DSAPublicKey;
 import freenet.crypt.RandomSource;
 import freenet.keys.CHKBlock;
@@ -1071,9 +1072,9 @@
                        chkStore.seek(0);
                        for(l=0;true;l++) {
                                Transaction t = null;
+                               chkStore.readFully(header);
+                               chkStore.readFully(data);
                                try {
-                                       chkStore.readFully(header);
-                                       chkStore.readFully(data);
                                        byte[] routingkey = null;
                                        if(type == TYPE_CHK) {
                                                try {
@@ -1089,7 +1090,7 @@
                                                        continue;
                                                }
                                        } else if(type == TYPE_PUBKEY) {
-                                               DSAPublicKey key = new 
DSAPublicKey(data);
+                                               DSAPublicKey key = 
DSAPublicKey.create(data);
                                                routingkey = key.asBytesHash();
                                        } else {
                                                continue;
@@ -1111,6 +1112,8 @@
                                        if(l % 1024 == 0)
                                                System.out.println("Key "+l+ 
'/' +(chkStore.length()/(dataBlockSize+headerBlockSize))+" OK ("+dupes+" dupes, 
"+failures+" failures)");
                                        t = null;
+                               } catch (CryptFormatException e) {
+                                       addFreeBlock(l, true, "invalid key: 
"+e);
                                } finally {
                                        if(t != null) t.abort();
                                }
@@ -1471,8 +1474,8 @@
                if(logMINOR) Logger.minor(this, "Read");

                try {
-                       block = new DSAPublicKey(data);
-               } catch (IOException e) {
+                       block = DSAPublicKey.create(data);
+               } catch (CryptFormatException e) {
                        Logger.error(this, "Could not read key: "+e, e);
                        finishKey(storeBlock, c, t, routingkeyDBE, hash, 
replacement);
                        return null;


Reply via email to