Author: nextgens
Date: 2007-04-14 16:19:56 +0000 (Sat, 14 Apr 2007)
New Revision: 12696

Modified:
   trunk/freenet/src/freenet/node/fcp/ClientPut.java
   trunk/freenet/src/freenet/node/fcp/ClientPutMessage.java
Log:
Wire in the new check

Modified: trunk/freenet/src/freenet/node/fcp/ClientPut.java
===================================================================
--- trunk/freenet/src/freenet/node/fcp/ClientPut.java   2007-04-14 16:00:53 UTC 
(rev 12695)
+++ trunk/freenet/src/freenet/node/fcp/ClientPut.java   2007-04-14 16:19:56 UTC 
(rev 12696)
@@ -221,6 +221,11 @@
                        targetURI = null;
                this.data = tempData;
                this.clientMetadata = cm;
+               
+               // Check the hash : allow it to be null for backward 
compatibility
+               if((salt != null) && !isHashVerified(salt))
+                       throw new 
MessageInvalidException(ProtocolErrorMessage.DIRECT_DISK_ACCESS_DENIED, "The 
hash doesn't match!", identifier, global);
+               
                if(logMINOR) Logger.minor(this, "data = "+data+", uploadFrom = 
"+ClientPutMessage.uploadFromString(uploadFrom));
                putter = new ClientPutter(this, data, uri, cm, 
                                ctx, 
client.core.requestStarters.chkPutScheduler, 
client.core.requestStarters.sskPutScheduler, priorityClass, 
@@ -282,20 +287,8 @@
                        targetURI = null;

                        if(salt != null) {
-                               if (logMINOR) Logger.minor(this, "Found a hash 
: let's verify it");
-                               MessageDigest sha = SHA256.getMessageDigest();
-                               sha.reset();
-                               sha.update(salt.getBytes("UTF-8"));
-                               BufferedInputStream bis = new 
BufferedInputStream(data.getInputStream());
-                               byte[] buf = new byte[4096];
-                               while(bis.read(buf) > 0)
-                                       sha.update(buf);
-                               
-                               byte[] foundHash = sha.digest();
-                               SHA256.returnMessageDigest(sha);
-                               
-                               if(!foundHash.equals(saltedHash))
-                                       throw new 
PersistenceParseException("The hash doesn't match !");        
+                               if(!isHashVerified(salt)) // We trust the given 
salt
+                                       throw new 
PersistenceParseException("The hash doesn't match! or an error has occured.");
                        } else // should probably be a higher level 
                                Logger.normal(this, "Hash not found!");
                } else if(uploadFrom == ClientPutMessage.UPLOAD_FROM_DIRECT) {
@@ -492,4 +485,26 @@
        public void onFailure(FetchException e, ClientGetter state) {}

        public void onSuccess(FetchResult result, ClientGetter state) {}
+       
+       private boolean isHashVerified(String trustedSalt) {
+               if (logMINOR) Logger.minor(this, "Found a hash : let's verify 
it");
+               if(!trustedSalt.equals(salt)) return false;
+               byte[] foundHash = null;
+               MessageDigest md = SHA256.getMessageDigest();
+               try {
+                       md.reset();
+                       md.update(trustedSalt.getBytes("UTF-8"));
+                       BufferedInputStream bis = new 
BufferedInputStream(data.getInputStream());
+                       byte[] buf = new byte[4096];
+                       while(bis.read(buf) > 0)
+                               md.update(buf);
+                       foundHash = md.digest();
+               } catch (IOException e) {
+                       return false;
+               } finally {
+                       SHA256.returnMessageDigest(md); 
+               }
+               
+               return foundHash.equals(saltedHash); 
+       }
 }

Modified: trunk/freenet/src/freenet/node/fcp/ClientPutMessage.java
===================================================================
--- trunk/freenet/src/freenet/node/fcp/ClientPutMessage.java    2007-04-14 
16:00:53 UTC (rev 12695)
+++ trunk/freenet/src/freenet/node/fcp/ClientPutMessage.java    2007-04-14 
16:19:56 UTC (rev 12696)
@@ -126,7 +126,7 @@
                        }
                }
                // We do *NOT* check that FileHash is valid here for backward 
compatibility... and to make the override work
-               this.fileHash = fs.get(ClientPutBase.SALT);
+               this.fileHash = fs.get(ClientPutBase.FILE_HASH);
                String uploadFrom = fs.get("UploadFrom");
                if((uploadFrom == null) || 
uploadFrom.equalsIgnoreCase("direct")) {
                        uploadFromType = UPLOAD_FROM_DIRECT;


Reply via email to