[Webfunds-commits] java/webfunds/ricardian Contract.java

Ian Grigg iang@cypherpunks.ai
Fri, 13 Apr 2001 12:46:21 -0400 (AST)


iang        01/04/13 12:46:21

  Modified:    webfunds/ricardian Contract.java
  Log:
  1. move printlns over to logmsg and commented out routine messages,
     left exceptional errors as System.err.println()s in static calls :(
  2. re-organised edwin's FIXME signature test into a method, algorithm
     can be FIXMED any time, now has defined getSignatureType() method.
  3. added internel signature types for known sig technologies (OpenPGP == 2).
  4. added getSignatureTypeString() to return a printable string for the
     signature type.
  5. added an internal string that collects the printable key Ids as the
     contract is verified.  Caller can access this string to get sig chain
     after verifying.

Revision  Changes    Path
1.55      +81 -30    java/webfunds/ricardian/Contract.java

Index: Contract.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/ricardian/Contract.java,v
retrieving revision 1.54
retrieving revision 1.55
diff -u -r1.54 -r1.55
--- Contract.java	2001/04/12 04:57:53	1.54
+++ Contract.java	2001/04/13 16:46:20	1.55
@@ -1,4 +1,4 @@
-/* $Id: Contract.java,v 1.54 2001/04/12 04:57:53 iang Exp $
+/* $Id: Contract.java,v 1.55 2001/04/13 16:46:20 iang Exp $
  *
  * Copyright (c) Systemics Ltd 1995-1999 on behalf of
  * the WebFunds Development Team.  All Rights Reserved.
@@ -209,9 +209,9 @@
         s = getField("issue", "power");
         if (s != null && s.length() > 0)
         {
-// System.err.println("got new power: " + s);
+// logmsg.println("got new power: " + s);
             int p = Units.powerInt(s);
-// System.err.println("set new power: " + p);
+// logmsg.println("set new power: " + p);
             setPower(p);
             this.factor = Units.power2factorDouble(p);
             return ;
@@ -281,10 +281,10 @@
      */
     public long getUnitsOfContract(double amount)
     {
-// System.err.println(getField("currency", "factor"));
-// System.err.println("Fraction = " + factor);
-// System.err.println("Amount   = " + amount);
-// System.err.println("Amount/Fraction = " + Math.round(amount/factor));
+// logmsg.println(getField("currency", "factor"));
+// logmsg.println("Fraction = " + factor);
+// logmsg.println("Amount   = " + amount);
+// logmsg.println("Amount/Fraction = " + Math.round(amount/factor));
         return Math.round(amount/factor);
     }
 
@@ -294,7 +294,7 @@
     //amount = dollars, units = cents
     public double getUnitsOfAccount(long units)
     {
-// System.err.println("returning " + units + " * " + factor + " == " +
+// logmsg.println("returning " + units + " * " + factor + " == " +
 //                                                  ( units * factor));
         return units * factor;
     }
@@ -403,7 +403,7 @@
             String s = "Warning! canonical differs (" +
                      version2 + "): set [local] digest_version=2 ?";
             error(s);
-            // System.err.println(s);
+            // logmsg.println(s);
         }
 
         return bV;
@@ -1157,19 +1157,59 @@
     {
         String s = new String(contractData);
 
-        // ### FIXME (edwin): Figure out a better way to check this
-        if (s.startsWith("-----BEGIN PGP SIGNED MESSAGE-----")) {
-
+        if (isOpenPGP())
             return verifyOpenPGPSignatures();
-
-        } else {  // X.509 signed contract
 
+        else if (isX509())
             return verifyX509Signatures();
-        }
+
+        else
+            return false;
     }
 
+    public boolean isOpenPGP()   { return getSignatureType() == OpenPGP; }
+    public boolean isX509()      { return getSignatureType() == X509;    }
+    public boolean isXML()       { return false;                           }
+
+    public static final int   NO_SIG    = 0,     // undiscernable sig type
+                              PGP26     = 1,     // histerical interest only
+                              OpenPGP   = 2,     // current favourite
+                              X509      = 3,     // a financial dog
+                              XML       = 4;     // a future rising star
 
     /**
+     *  @return the type of signature technology used.
+     */
+    public int getSignatureType()
+    {
+        String s = new String(contractData);
+
+        // XXX: FIXME (edwin): Figure out a better way to check this
+
+        if (s.startsWith("-----BEGIN PGP SIGNED MESSAGE-----"))
+            return OpenPGP;
+        else if (s.startsWith("-----BEGIN SIGNED DATA-----"))   // X.509
+            return X509;
+        else
+            return NO_SIG;
+    }
+
+    /**
+     *  @return the (string) type of signature technology used.
+     */
+    public String getSignatureTypeString()
+    {
+        int sig = getSignatureType();
+        String[] types = {"<UNSIGNED>", "PGP26", "OpenPGP", "X.509", "XML"};
+        return types[sig];
+    }
+
+    private String chain = "";
+    /** @return chain of signatures on the contract.  Indicative only! */
+    public String getChainOfSignatures()             { return chain; }
+
+
+    /**
      *  Verify that the OpenPGP contract is signed correctly and
      *  that all certs match our current path goodness criteria:
      *
@@ -1186,6 +1226,7 @@
     public boolean verifyOpenPGPSignatures()
         throws ContractException
     {
+        chain = "";
         String s = new String(contractData);
 
         /*
@@ -1197,8 +1238,8 @@
 
         Certificate contractCert = getContractCert();
 
-        System.err.println("contract C: " + contractCert);
-        System.err.println("topLevel C: " + topLevelCert);
+        logmsg.println("contract C: " + contractCert);
+        logmsg.println("topLevel C: " + topLevelCert);
 
         /*
          *  No cert path for signing key yet, may be signed by intermediate
@@ -1226,6 +1267,8 @@
             throw new ContractException(ContractException.SIG_VERIFY,
                       "Contract signature failed!");
 
+        chain += "contract: " + contractCert;
+
         PGPPublicKey topLevelKey = (PGPPublicKey)topLevelCert.getPublicKey();
 
         /*
@@ -1235,10 +1278,10 @@
         PGPKeyID contractId = contractKey.getKeyID();
         PGPKeyID topLevelId = topLevelKey.getKeyID();
 
-        System.err.println("contract  : " + contractKey);
-        System.err.println("topLevel  : " + topLevelKey);
-        System.err.println("contractId: " + contractId);
-        System.err.println("topLevelId: " + topLevelId);
+        logmsg.println("contract  : " + contractKey);
+        logmsg.println("topLevel  : " + topLevelKey);
+        logmsg.println("contractId: " + contractId);
+        logmsg.println("topLevelId: " + topLevelId);
         if (contractId == null)
             throw new ContractException(ContractException.KEY_CONTRACT,
                                         "no KeyId within key??");
@@ -1268,6 +1311,8 @@
                                     "Invalid cert - "+ce);
         }
 
+        chain += "\ntoplevel: " + topLevelCert;
+
         /*
          *  Verify that keys do not have any superfluous signatures.
          *  We have already checked all the positive errors like the
@@ -1299,6 +1344,7 @@
     public boolean verifyX509Signatures()
         throws ContractException
     {
+        chain = "";
         // Get the Certificate from the contract
         Certificate issuerCert = getContractCert();
 
@@ -1347,6 +1393,9 @@
 
         } while (!ok && (data = mungeLastLine(data)) != null) ;
 
+        chain += "x509Cert: " + issuerCert;
+        chain += "x509Cert: " + pK;
+
         return ok ;
     }
 
@@ -1677,16 +1726,18 @@
     {
         String s = new String(contractData);
 
-        // ### FIXME (edwin): Figure out a better way to check this
-        if (s.startsWith("-----BEGIN PGP SIGNED MESSAGE-----")) {
+        if (isOpenPGP()) {
 
             PGPArmoury pgparmoury = new PGPArmoury(s);
             return new String(pgparmoury.getClearText());
 
-        } else { // X.509 signed contract
+        } else if (isX509()) {
 
             return new String(Armoury.decodeData(contractData));
 
+        } else {
+            throw new IOException("failed to determine Contract type: " +
+                                  getSignatureTypeString());
         }
 
     }
@@ -1732,7 +1783,7 @@
         byte[] buf = contract.getBytes();
         String[] lines = IniFileReader.toLines(buf);
         int numLines = lines.length;
-System.err.println("lines " + numLines);
+// System.err.println("lines " + numLines);
 
         String s;
         int start = 0;
@@ -1744,7 +1795,7 @@
                 start--;
                 break;
             }
-System.err.println("skipping line " + start);
+// System.err.println("skipping line " + start);
         }
 
         if (start >= numLines)
@@ -1753,7 +1804,7 @@
 
         if (lines[start].equals("-----BEGIN PGP SIGNED MESSAGE-----"))
         {
-System.err.println("signed! at line " + start);
+// System.err.println("signed! at line " + start);
             PGPArmoury armoury;
             try {
                 armoury = new PGPArmoury(contract);
@@ -1780,7 +1831,7 @@
          *  Now skip forward to the [keys] section and delete from there on.
          */
         int end = start;
-System.err.println("starting search at line " + start);
+// System.err.println("starting search at line " + start);
         while (end < numLines)
         {
             s = lines[end++];
@@ -1789,7 +1840,7 @@
 
             if (s.equals("[keys]"))
             {
-System.err.println("found at line " + end);
+// System.err.println("found at line " + end);
                 end--;      // point back at [keys], 0..end-1 is captured below
                 break;
             }
@@ -1797,7 +1848,7 @@
 
         String eoln = KeyUtil.getPlatformEndOfLine();
 
-System.err.println("concatenating strings 0 .. " + end);
+// System.err.println("concatenating strings 0 .. " + end);
         StringBuffer sb = new StringBuffer(contract.length());
         for (int i = 0; i < end; i++)
         {