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

Jeroen C. van Gelderen gelderen@cypherpunks.ai
Mon, 14 Aug 2000 22:14:32 -0400 (AST)


gelderen    00/08/14 22:14:32

  Modified:    webfunds/ricardian KeyUtil.java
  Log:
  Add verifyKey method.

Revision  Changes    Path
1.2       +67 -5     java/webfunds/ricardian/KeyUtil.java

Index: KeyUtil.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/ricardian/KeyUtil.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- KeyUtil.java	2000/08/15 01:52:41	1.1
+++ KeyUtil.java	2000/08/15 02:14:32	1.2
@@ -25,7 +25,8 @@
      * @throws StripKeyException
      *         If the requested userId doesn't exist or is not self-signed or
      *         has multiple self-sigs or isn't signed by userIdSigner or has
-     *         multiple sigs by userIdSigner (dunno if that's possible).
+     *         multiple sigs by userIdSigner (dunno if that's possible) or has
+     *         multiple matching userIds.
      */
     public static PGPPublicKey 
     stripAndVerifyKey(PGPPublicKey key, 
@@ -86,6 +87,7 @@
             return key;
 
         } catch(Exception e) {
+
             // something went wrong, dunno what
             e.printStackTrace();
             throw new StripKeyException(e.getMessage());
@@ -94,17 +96,71 @@
 
 
     /**
+     * Verify that the given key is valid. Valid is defined as "stripAndVerify
+     * will not throw StripKeyException".
+     */
+    public static boolean 
+    verifyKey(PGPPublicKey key, String userIdTag, PGPPublicKey userIdSigner) {
+
+        if( key==null || userIdTag==null )
+            throw new IllegalArgumentException();
+
+        // prevent duplication of sigs
+        if (key.equals(userIdSigner))
+            userIdSigner = null;
+
+        try {
+
+            // find the userId we want
+            PGPUserID uid = findUserId(key, userIdTag);
+            if (uid == null) return false;
+
+            // we now have a key with single userId
+
+            Vector sigs = uid.getSignatures();
+            int wantedSigCount = (userIdSigner==null) ? 1 : 2;
+            if (sigs.size() != wantedSigCount) return false;
+
+            // strip now is a no-op and we can use it's verification :-)
+            stripAndVerifyKey(key, userIdTag, userIdSigner);
+
+            return true;
+
+        } catch(StripKeyException e) {
+
+            // verification failed
+            return false;
+
+        } catch(Exception e) {
+
+            // something went wrong, dunno what
+            e.printStackTrace();
+            return false;
+        }
+    }
+
+
+    /**
      * Return the first UserId on the key that matches 'userIdTag' or return
      * null.
      */
-    private static PGPUserID findUserId(PGPPublicKey key, String userIdTag) {
+    private static PGPUserID findUserId(PGPPublicKey key, String userIdTag)
+    throws StripKeyException
+    {
+        PGPUserID res = null;
         Vector uids = key.getUserIDs();
         for(int i=0; i<uids.size(); i++) {
             PGPUserID uid = (PGPUserID)uids.elementAt(i);
             String s = uid.getValue();
-            if( isMatch(s, userIdTag) ) return uid;
+            if( isMatch(s, userIdTag) ) {
+                if (res!=null) 
+                    throw new StripKeyException(
+                        "Multiple matching userIds found.");
+                else
+                    res = uid;
+            }
         }
-        return null;
+        return res;
     }
 
 
@@ -130,8 +186,14 @@
         PGPPublicKey key1 = (PGPPublicKey)key1vector.elementAt(0);
 
         System.out.println(key1.toString());
+
+        System.out.println(verifyKey(key1, "zediaport", null));
+        System.out.println(verifyKey(key1, "mediaport", null));
+
+        key1 = stripAndVerifyKey(key1, "ediaport", key1);
 
-        key1 = stripAndVerifyKey(key1, "mediaport", key1);
+        System.out.println(verifyKey(key1, "zediaport", null));
+        System.out.println(verifyKey(key1, "mediaport", null));
         key1.writeKey("tootsie.pgp");
     }
 }