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

Edwin Woudt edwin@cypherpunks.ai
Mon, 28 Aug 2000 10:42:56 -0400 (AST)


edwin       00/08/28 10:42:56

  Modified:    webfunds/ricardian KeyUtil.java
  Log:
  Remove catch blocks that catch all exceptions and replace with more
  specific catch blocks where needed. (like in case of an invalid key)
  This improves the errors that are returned to the user.

Revision  Changes    Path
1.7       +29 -39    java/webfunds/ricardian/KeyUtil.java

Index: KeyUtil.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/ricardian/KeyUtil.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- KeyUtil.java	2000/08/28 03:22:37	1.6
+++ KeyUtil.java	2000/08/28 14:42:56	1.7
@@ -1,5 +1,5 @@
 /*
- * $Id: KeyUtil.java,v 1.6 2000/08/28 03:22:37 iang Exp $
+ * $Id: KeyUtil.java,v 1.7 2000/08/28 14:42:56 edwin Exp $
  *
  * Copyright (c) 2000 Systemics Inc on behalf of 
  * the WebFunds Development Team.  All Rights Reserved.
@@ -155,15 +155,16 @@
                       String userIdTag, PGPPublicKey userIdSigner)
     throws StripKeyException
     {
-        if( key==null || userIdTag==null )
-            throw new IllegalArgumentException();
 
-        // prevent duplication of sigs
-        if (key.equals(userIdSigner))
-            userIdSigner = null;
-
         try {
 
+            if( key==null || userIdTag==null )
+                throw new IllegalArgumentException();
+
+            // prevent duplication of sigs
+            if (key.equals(userIdSigner))
+            userIdSigner = null;
+
             // find the userId we want
             PGPUserID uid = findUserId(key, userIdTag);
             if (uid == null) 
@@ -210,11 +211,11 @@
 
             return key;
 
-        } catch(Exception e) {
+        } catch(PGPFatalDataFormatException e) {
 
-            // something went wrong, dunno what
+            // invalid key
             e.printStackTrace();
-            throw new StripKeyException(StripKeyException.CATCH_ALL,
+            throw new StripKeyException(StripKeyException.INVALID_KEY,
                                         e.getMessage());
         }
     }
@@ -227,15 +228,15 @@
     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 {
+        
+            if( key==null || userIdTag==null )
+                throw new IllegalArgumentException();
 
+            // prevent duplication of sigs
+            if (key.equals(userIdSigner))
+                userIdSigner = null;
+
             // find the userId we want
             PGPUserID uid = findUserId(key, userIdTag);
             if (uid == null) return false;
@@ -244,20 +245,14 @@
              *  check the userId is the only one by matching empty string
              *  should reverse these checks :)
              */
-            try {
-
-                PGPUserID uid2 = findUserId(key, "");
-                if (uid2 == null)
-                    throw new RuntimeException("no UserId second time? " + uid);
-                if (!uid2.equals(uid))
-                    throw new RuntimeException("1st UserId not same as 2nd: " +
-                                               uid + " != " + uid2);
-            } catch (StripKeyException ex) {
-                return false;
-            }
+            PGPUserID uid2 = findUserId(key, "");
+            if (uid2 == null)
+                throw new RuntimeException("no UserId second time? " + uid);
+            if (!uid2.equals(uid))
+                throw new RuntimeException("1st UserId not same as 2nd: " +
+                                                   uid + " != " + uid2);
 
             // we now have a key with single userId
-
             Vector sigs = uid.getSignatures();
             int wantedSigCount = (userIdSigner==null) ? 1 : 2;
             if (sigs.size() != wantedSigCount) return false;
@@ -266,18 +261,13 @@
             stripAndVerifyKey(key, userIdTag, userIdSigner);
 
             return true;
-
-        } catch(StripKeyException e) {
-
-            // verification failed
-            return false;
-
-        } catch(Exception e) {
-
-            // something went wrong, dunno what
-            e.printStackTrace();
+            
+        } catch (StripKeyException e) {
+        
             return false;
+            
         }
+
     }