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

Edwin Woudt edwin@cypherpunks.ai
Fri, 7 Jul 2000 14:07:11 -0400 (AST)


edwin       00/07/07 14:07:10

  Modified:    webfunds/ricardian Contract.java
  Log:
  Merging OpenPGP support on the tip.

Revision  Changes    Path
1.22      +82 -48    java/webfunds/ricardian/Contract.java

Index: Contract.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/ricardian/Contract.java,v
retrieving revision 1.21
retrieving revision 1.22
diff -u -r1.21 -r1.22
--- Contract.java	2000/06/05 00:49:56	1.21
+++ Contract.java	2000/07/07 18:07:10	1.22
@@ -1,5 +1,5 @@
 /*
- * $Id: Contract.java,v 1.21 2000/06/05 00:49:56 iang Exp $
+ * $Id: Contract.java,v 1.22 2000/07/07 18:07:10 edwin Exp $
  *
  * Copyright (c) Systemics Ltd 1995-1999 on behalf of
  * the WebFunds Development Team.  All Rights Reserved.
@@ -40,6 +40,9 @@
 import webfunds.sox.Crypto;
 import webfunds.sox.SOXKeyException;
 
+import cryptix.openpgp.PGPMessage;
+import cryptix.openpgp.PGPPublicKey;
+
 /**
  * Ricardian Contract Base Class
  * Rewritten from the sox.Contract and the Perl equivalents.
@@ -661,18 +664,32 @@
      * This key is the one that signs the contract, and is only a hint,
      * the key needs to be authenticated directly by the application.
      */
-    public X509Cert getContractCert()
+    private X509Cert getContractCert()
         throws ContractException
     {
         return getCert("contract");
     }
 
+
+    // ### FIXME (edwin): Should read the key from the contract
+    private PGPPublicKey getContractCertPGP()
+        throws ContractException
+    {
+        PGPPublicKey key = new PGPPublicKey();
+        try {
+            key.readKey("pubkey.pgp");
+        } catch (IOException ioe) {
+            ioe.printStackTrace();
+        }
+        return key;
+    }
+
     /**
      * This is the top-level Issuer certification key,
      * which signs the contract key above.
      * The presence of these keys should indicate which server is in use.
      */
-    public X509Cert getCertificationCert()
+    private X509Cert getCertificationCert()
         throws ContractException
     {
         return getCert("certification");
@@ -682,7 +699,7 @@
      * This is the SOX Server comms key.
      * The presence of these keys should indicate which server is in use.
      */
-    public X509Cert getServerCert()
+    protected X509Cert getServerCert()
         throws ContractException
     {
         String CERT = "server_certification";
@@ -703,7 +720,7 @@
         return getCertFromString(field);
     }
 
-    public X509Cert getCert(String name)
+    private X509Cert getCert(String name)
         throws ContractException
     {
         String field = getField("keys", name);
@@ -713,7 +730,7 @@
         return getCertFromString(field);
     }
 
-    public X509Cert getCertFromString(String field)
+    private X509Cert getCertFromString(String field)
         throws ContractException
     {
         if (field == null || field.length() == 0)
@@ -744,53 +761,70 @@
     public boolean verifyContract()
         throws ContractException
     {
-        // Get the Certificate from the contract
-        X509Cert issuerCert = getContractCert();
-
-        // Extract the signature
         String s = new String(contractData);
-        byte[] sig;
-        try {
-            sig = Armoury.decodeByteArray("SIGNATURE", s);
-        } catch (IOException ex) {
-            throw new ContractException("signature is bad: " + ex);
-        }
-
-        // Get the prepared (i.e. CR/NL processed) contract
-        byte[] decoded;
-        byte[] data;
-        try {
-            decoded = Armoury.decodeData(contractData);
-            data = Armoury.prepareDataToSign(decoded);
-        } catch (IOException ex) {
-            throw new ContractException("contract part is bad: " + ex);
-        }
 
-        PublicKey pK = Crypto.getPublicKeyFromCert(issuerCert);
+        // ### FIXME (edwin): Figure out a better way to check this
+        if (s.startsWith("-----BEGIN PGP SIGNED MESSAGE-----")) {
 
-        //
-        // Drop last 2 bytes - which are a CR/LN - as these might have
-        // snuck in?  Pox on those who don't comment this!  Further
-        // pestilence & plague on them who didn't fix it at source!!
-        // Rot & Ruin on they who let the "standard" reflect this
-        // abomination !*!%!
-        //
-        // It turns out that the last CR/LN is not counted in a signature.
-        //
-        boolean ok;
-        int i = 0;
-        do
-        {
+            PGPPublicKey issuerKey = getContractCertPGP();
+            
             try {
-                ok = Crypto.verify(pK, sig, data);
-            } catch (java.security.KeyException ex) {
-                throw new ContractException("bad key on verify: " + ex);
+                return PGPMessage.verifyClearSign(s, issuerKey);
+            } catch (IOException ioe) {
+                return false;
             }
-            logmsg("Attempt " + (++i) + ": " + ok);
-
-        } while (!ok && (data = mungeLastLine(data)) != null) ;
-
-        return ok ;
+            
+        } else {  // X.509 signed contract
+        
+            // Get the Certificate from the contract
+            X509Cert issuerCert = getContractCert();
+    
+            // Extract the signature
+            byte[] sig;
+            try {
+                sig = Armoury.decodeByteArray("SIGNATURE", s);
+            } catch (IOException ex) {
+                throw new ContractException("signature is bad: " + ex);
+            }
+    
+            // Get the prepared (i.e. CR/NL processed) contract
+            byte[] decoded;
+            byte[] data;
+            try {
+                decoded = Armoury.decodeData(contractData);
+                data = Armoury.prepareDataToSign(decoded);
+            } catch (IOException ex) {
+                throw new ContractException("contract part is bad: " + ex);
+            }
+    
+            PublicKey pK = Crypto.getPublicKeyFromCert(issuerCert);
+    
+            //
+            // Drop last 2 bytes - which are a CR/LN - as these might have
+            // snuck in?  Pox on those who don't comment this!  Further
+            // pestilence & plague on them who didn't fix it at source!!
+            // Rot & Ruin on they who let the "standard" reflect this
+            // abomination !*!%!
+            //
+            // It turns out that the last CR/LN is not counted in a signature.
+            //
+            boolean ok;
+            int i = 0;
+            do
+            {
+                try {
+                    ok = Crypto.verify(pK, sig, data);
+                } catch (java.security.KeyException ex) {
+                    throw new ContractException("bad key on verify: " + ex);
+                }
+                logmsg("Attempt " + (++i) + ": " + ok);
+    
+            } while (!ok && (data = mungeLastLine(data)) != null) ;
+    
+            return ok ;
+        
+        }
+      
     }
 
     /*