[Webfunds-commits] java/webfunds/sox SimpleIssuer.java SubAccount.java

Ian Grigg iang@cypherpunks.ai
Sun, 3 Sep 2000 12:55:05 -0400 (AST)


iang        00/09/03 12:55:04

  Modified:    webfunds/sox SimpleIssuer.java SubAccount.java
  Log:
  Cute.  Payments were "too early" if done from now to now+delta.
  This occurred for the first payments where they triggered the crypto
  PRNG initialisation (SecureRandom claims another victim...).
  Fixed by moving the deviation time test to include all components
  that might effect the time.  Manipulated the debugs a lot too.
  A later change needs to decouple the checkSync from the request
  layer, as requests don't necessarily rely on the timestamp, but
  application code (esp, payments) does.
  A change to the Issuer interface might be needed.

Revision  Changes    Path
1.14      +43 -12    java/webfunds/sox/SimpleIssuer.java

Index: SimpleIssuer.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/sox/SimpleIssuer.java,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -r1.13 -r1.14
--- SimpleIssuer.java	2000/08/20 02:59:29	1.13
+++ SimpleIssuer.java	2000/09/03 16:55:04	1.14
@@ -1,5 +1,5 @@
 /*
- * $Id: SimpleIssuer.java,v 1.13 2000/08/20 02:59:29 gelderen Exp $
+ * $Id: SimpleIssuer.java,v 1.14 2000/09/03 16:55:04 iang Exp $
  *
  * Copyright (c) Systemics Ltd 1995-1999 on behalf of
  * the WebFunds Development Team.  All Rights Reserved.
@@ -212,6 +212,8 @@
     {
         fetchCommsKey();
 
+logmsg("tafter == " + System.currentTimeMillis() );
+
         try
         {
             return requestOnce(request);
@@ -281,6 +283,12 @@
     public String          getDead()            { return reason; }
 
     /**
+     *  Just to make it obvious what the numbers are used for...
+     */
+    static final long SECOND = 1000,
+                      HOUR = 60 * 60 * SECOND;
+
+    /**
      * This is the difference between local time and the server's time.
      * It is used to set time values to within a short range as a possible
      * defence against replay attacks.
@@ -292,11 +300,11 @@
      * prepared.
      */
     protected long timediff = 0; //Long.MAX_VALUE;
-    static final long HOUR = 60 * 60 * 1000;
     protected long deviation = 24 * HOUR;
     protected long lastsync = 0;
     protected long timeLastRequest;
 
+    // should we call checkSync() here?
     public long getTimeDifference() { return timediff ; }
     public long getTimeDeviation() { return deviation ; }
 
@@ -309,8 +317,17 @@
     protected void timesync()
         throws SOXIssuerException, SOXLaterException
     {
-        logmsg("  Timesyncing.. ");
 
+        /*
+         *  Ha, we need to measure the time from before the creation
+         *  of the TimeSyncRequest to the return of the time, as that
+         *  is the relevant deviation including any internal slowdowns
+         *  (i.e., the first crypto causes a SecureRandom 20 second
+         *  delay!  e.g., swapping...)
+         */
+        long tim = System.currentTimeMillis();
+        logmsg("  Timesyncing.. (" + tim + " now, last was " + lastsync + ")");
+
         TimeSyncRequest tsr = new TimeSyncRequest(""+reqNo++, new AccountId());
         byte[] packet = this.internalRequest(tsr); // avoids timesync :-)
 
@@ -321,27 +338,41 @@
             setDead(ex.getMessage());
             throw new SOXIssuerException("TimeSyncReply: " + ex.getMessage());
         }
+
+        timediff = reply.getTimeDifference();   // as seen by SOX Server
 
-        timediff = reply.getTimeDifference();
-        deviation = timeLastRequest;
+        /*
+         *  Now measure the delay of the request and use that as deviation.
+         */
+        // deviation = timeLastRequest;
         lastsync = System.currentTimeMillis();
+        deviation = lastsync - tim;
 
-        logmsg("Timediff = " + timediff + " (last == " + lastsync + ")");
+        logmsg("Timediff = " + timediff + " +- " + deviation +
+               "   (complete at " + lastsync + ")");
+        if ((deviation / 2 > timeLastRequest) || deviation > SECOND)
+            logmsg("Warning:  timesync is taking too long?  " + timeLastRequest);
     }
 
     /**
-     * Check the sync is reasonably new.  If not, do a timesync.
+     *  Check the sync is reasonably new.  If not, do a timesync.
      *
-     * @except SOXIssuerException if this Issuer is dead, try another
+     *  There is a presumption that is used within the requests,
+     *  but that is not really the case as it only includes a
+     *  timestamp which is ignored for the request code.
+     *  (Historically, this was due to confusion as to which layer
+     *  would prevent against replay attacks.)
+     *
+     *  @except SOXIssuerException if this Issuer is dead, try another
      */
     public void checkSync()
         throws SOXIssuerException, SOXLaterException
     {
         long timeNow = System.currentTimeMillis();
-        if (timeNow - lastsync < 24 * HOUR)
-            return ;
-logmsg("tn == " + timeNow + "   ...  last == " + lastsync);
-        timesync();
+        if (timeNow - lastsync > 24 * HOUR)
+            timesync();
+        else if (deviation > SECOND)
+            timesync();
     }
 
 



1.26      +10 -6     java/webfunds/sox/SubAccount.java

Index: SubAccount.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/sox/SubAccount.java,v
retrieving revision 1.25
retrieving revision 1.26
diff -u -r1.25 -r1.26
--- SubAccount.java	2000/07/16 19:34:41	1.25
+++ SubAccount.java	2000/09/03 16:55:04	1.26
@@ -1,4 +1,4 @@
-/* $Id: SubAccount.java,v 1.25 2000/07/16 19:34:41 iang Exp $
+/* $Id: SubAccount.java,v 1.26 2000/09/03 16:55:04 iang Exp $
  *
  * Copyright (c) Systemics Inc. 1995-2000 on behalf of
  * The WebFunds Development Team.  All Rights Reserved.
@@ -139,8 +139,8 @@
         return issuer ;
     }
 
-    long lastDiff = 0;
-    long stdDeviation = 24 * 60 * 60 * 1000;
+    private long lastDiff = 0;
+    private long stdDeviation = 24 * 60 * 60 * 1000;
 
 
     /**
@@ -160,11 +160,12 @@
      */
     public long adjustTime(long tim, boolean earliest)
     {
-        logmsg("adjustTime() = " + tim );
 
+        // why are these used instead of the cached values???
         long diff = lastDiff;
         long deviation = stdDeviation;
 
+        // this is a bit messed up, Issuer should check this??
         if (NetWatcher.netAvailability())
         {
             try {
@@ -173,19 +174,22 @@
                 diff = issuer.getTimeDifference();
                 deviation = issuer.getTimeDeviation();
             } catch (SOXException ex) {
-                ex = null; // jikes
             }
         }
         else
             logmsg("no net - using old times");
 
+        logmsg("adjustTime(" + tim + ") minus diff " + diff +
+               (earliest ? " subtract deviation " : " add deviation ") +
+               deviation);
+
         long millis = tim - diff;
         if (earliest)
            millis -= deviation ;
         else
            millis += deviation ;
 
-        logmsg("   sA diff == " + diff + "  dev == " + deviation);
+        logmsg("   equals time of " + millis);
         return millis;
     }