Handle Repositories throw and Exception when they are unable to lookup a requested...
authorwassa <wassa@ab3bd59b-922f-494d-bb5f-6f0a3c29deca>
Thu, 20 Feb 2003 23:17:47 +0000 (23:17 +0000)
committerwassa <wassa@ab3bd59b-922f-494d-bb5f-6f0a3c29deca>
Thu, 20 Feb 2003 23:17:47 +0000 (23:17 +0000)
git-svn-id: https://subversion.switch.ch/svn/shibboleth/java-idp/trunk@488 ab3bd59b-922f-494d-bb5f-6f0a3c29deca

src/edu/internet2/middleware/shibboleth/hs/HandleRepository.java
src/edu/internet2/middleware/shibboleth/hs/InvalidHandleException.java [new file with mode: 0644]
src/edu/internet2/middleware/shibboleth/hs/provider/CryptoHandleRepository.java
src/edu/internet2/middleware/shibboleth/hs/provider/MemoryHandleRepository.java

index f3c69fc..8b9d9aa 100644 (file)
@@ -68,7 +68,10 @@ public interface HandleRepository {
 
        /**
         * Finds the <code>AuthNPrincipal</code> associated with a given opaque identifier.
+        * @throws InvalidHandleException if the specified handle is expired, unknown, or cannot 
+        * be resolved to a <code>AuthNPrincipal</code>
+        * @throws HandleRepositoryException if the <code>HandleRepository</code> encounters an internal error
         */
-       public AuthNPrincipal getPrincipal(String handle);
+       public AuthNPrincipal getPrincipal(String handle) throws HandleRepositoryException, InvalidHandleException;
 
 }
diff --git a/src/edu/internet2/middleware/shibboleth/hs/InvalidHandleException.java b/src/edu/internet2/middleware/shibboleth/hs/InvalidHandleException.java
new file mode 100644 (file)
index 0000000..7b80566
--- /dev/null
@@ -0,0 +1,64 @@
+/* 
+ * The Shibboleth License, Version 1. 
+ * Copyright (c) 2002 
+ * University Corporation for Advanced Internet Development, Inc. 
+ * All rights reserved
+ * 
+ * 
+ * Redistribution and use in source and binary forms, with or without 
+ * modification, are permitted provided that the following conditions are met:
+ * 
+ * Redistributions of source code must retain the above copyright notice, this 
+ * list of conditions and the following disclaimer.
+ * 
+ * Redistributions in binary form must reproduce the above copyright notice, 
+ * this list of conditions and the following disclaimer in the documentation 
+ * and/or other materials provided with the distribution, if any, must include 
+ * the following acknowledgment: "This product includes software developed by 
+ * the University Corporation for Advanced Internet Development 
+ * <http://www.ucaid.edu>Internet2 Project. Alternately, this acknowledegement 
+ * may appear in the software itself, if and wherever such third-party 
+ * acknowledgments normally appear.
+ * 
+ * Neither the name of Shibboleth nor the names of its contributors, nor 
+ * Internet2, nor the University Corporation for Advanced Internet Development, 
+ * Inc., nor UCAID may be used to endorse or promote products derived from this 
+ * software without specific prior written permission. For written permission, 
+ * please contact shibboleth@shibboleth.org
+ * 
+ * Products derived from this software may not be called Shibboleth, Internet2, 
+ * UCAID, or the University Corporation for Advanced Internet Development, nor 
+ * may Shibboleth appear in their name, without prior written permission of the 
+ * University Corporation for Advanced Internet Development.
+ * 
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
+ * AND WITH ALL FAULTS. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
+ * PARTICULAR PURPOSE, AND NON-INFRINGEMENT ARE DISCLAIMED AND THE ENTIRE RISK 
+ * OF SATISFACTORY QUALITY, PERFORMANCE, ACCURACY, AND EFFORT IS WITH LICENSEE. 
+ * IN NO EVENT SHALL THE COPYRIGHT OWNER, CONTRIBUTORS OR THE UNIVERSITY 
+ * CORPORATION FOR ADVANCED INTERNET DEVELOPMENT, INC. BE LIABLE FOR ANY DIRECT, 
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package edu.internet2.middleware.shibboleth.hs;
+
+/**
+ * Indicates that the<code>HandleRepository</code> could not retrieve an <code>AuthNPrincipal</code>
+ * for the given handle.
+ * 
+ * @author Walter Hoehn (wassa@columbia.edu)
+ */
+public class InvalidHandleException extends HandleRepositoryException {
+       
+               public InvalidHandleException(String message) {
+               super(message);
+       }
+
+}
index 98fcb43..7a05412 100644 (file)
@@ -51,12 +51,12 @@ package edu.internet2.middleware.shibboleth.hs.provider;
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
-import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutput;
 import java.io.ObjectOutputStream;
 import java.security.GeneralSecurityException;
+import java.security.InvalidKeyException;
 import java.security.KeyException;
 import java.security.KeyStore;
 import java.security.KeyStoreException;
@@ -68,6 +68,7 @@ import java.util.zip.GZIPInputStream;
 import java.util.zip.GZIPOutputStream;
 
 import javax.crypto.Cipher;
+import javax.crypto.NoSuchPaddingException;
 import javax.crypto.SecretKey;
 
 import org.apache.log4j.Logger;
@@ -78,6 +79,7 @@ import edu.internet2.middleware.shibboleth.common.AuthNPrincipal;
 import edu.internet2.middleware.shibboleth.common.ShibResource;
 import edu.internet2.middleware.shibboleth.hs.HandleRepository;
 import edu.internet2.middleware.shibboleth.hs.HandleRepositoryException;
+import edu.internet2.middleware.shibboleth.hs.InvalidHandleException;
 
 /**
  * <code>HandleRepository</code> implementation that employs the use of a shard secret
@@ -111,6 +113,9 @@ public class CryptoHandleRepository extends BaseHandleRepository implements Hand
                                        properties
                                                .getProperty("edu.internet2.middleware.shibboleth.hs.provider.CryptoHandleRepository.keyStoreKeyPassword")
                                                .toCharArray());
+                                               
+                       //Before we finish initilization, make sure that things are working
+                       testEncryption();
 
                } catch (KeyStoreException e) {
                        log.error(
@@ -182,22 +187,66 @@ public class CryptoHandleRepository extends BaseHandleRepository implements Hand
        /**
         * @see edu.internet2.middleware.shibboleth.hs.HandleRepository#getPrincipal(String)
         */
-       public AuthNPrincipal getPrincipal(String handle) {
+       public AuthNPrincipal getPrincipal(String handle) throws HandleRepositoryException, InvalidHandleException {
 
                try {
                        Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
                        cipher.init(Cipher.DECRYPT_MODE, secret);
+
                        byte[] objectArray = cipher.doFinal(new BASE64Decoder().decodeBuffer(handle));
 
                        ObjectInputStream objectStream =
                                new ObjectInputStream(new GZIPInputStream(new ByteArrayInputStream(objectArray)));
                        HandleEntry handleEntry = (HandleEntry) objectStream.readObject();
                        objectStream.close();
-                       return handleEntry.principal;
 
+                       if (handleEntry.isExpired()) {
+                               log.debug("Attribute Query Handle is expired.");
+                               throw new InvalidHandleException("Attribute Query Handle is expired.");
+                       } else {
+                               log.debug("Attribute Query Handle recognized.");
+                               return handleEntry.principal;
+                       }
+               } catch (NoSuchAlgorithmException e) {
+                       log.error("Appropriate JCE provider not found in the java environment.  Could not load Algorithm: " + e);
+                       throw new HandleRepositoryException("Appropriate JCE provider not found in the java environment.  Could not load Algorithm.");
+               } catch (NoSuchPaddingException e) {
+                       log.error(
+                               "Appropriate JCE provider not found in the java environment.  Could not load Padding method: " + e);
+                       throw new HandleRepositoryException("Appropriate JCE provider not found in the java environment.  Could not load Padding method.");
+               } catch (InvalidKeyException e) {
+                       log.error("Could not use the supplied secret key for Triple DES decryption: " + e);
+                       throw new HandleRepositoryException("Could not use the supplied secret key for Triple DES decryption.");
+               } catch (GeneralSecurityException e) {
+                       log.warn("Unable to decrypt the supplied Attribute Query Handle: " + e);
+                       throw new InvalidHandleException("Unable to decrypt the supplied Attribute Query Handle.");
+               } catch (ClassNotFoundException e) {
+                       log.warn("The supplied Attribute Query Handle does not represent a serialized AuthNPrincipal: " + e);
+                       throw new InvalidHandleException("The supplied Attribute Query Handle does not represent a serialized AuthNPrincipal.");
+               } catch (IOException e) {
+                       log.warn("The AuthNPrincipal could not be de-serialized from the supplied Attribute Query Handle: " + e);
+                       throw new InvalidHandleException("The AuthNPrincipal could not be de-serialized from the supplied Attribute Query Handle.");
+               }
+       }
+       
+       private void testEncryption() throws HandleRepositoryException {
+
+               String decrypted;
+               try {
+                       Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
+                       cipher.init(Cipher.ENCRYPT_MODE, secret);
+                       byte[] cipherText = cipher.doFinal("test".getBytes());
+                       cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
+                       cipher.init(Cipher.DECRYPT_MODE, secret);
+                       decrypted = new String(cipher.doFinal(cipherText));
                } catch (Exception e) {
-                       System.err.println(e);
-                       return null;
+                       log.error("Round trip encryption/decryption test unsuccessful: " + e);
+                       throw new HandleRepositoryException("Round trip encryption/decryption test unsuccessful.");
+               }
+
+               if (decrypted == null || !decrypted.equals("test")) {
+                       log.error("Round trip encryption/decryption test unsuccessful.  Decrypted text did not match.");
+                       throw new HandleRepositoryException("Round trip encryption/decryption test unsuccessful.");
                }
        }
 
index ee5b1d2..32941bb 100644 (file)
@@ -63,6 +63,7 @@ import org.doomdark.uuid.UUIDGenerator;
 import edu.internet2.middleware.shibboleth.common.AuthNPrincipal;
 import edu.internet2.middleware.shibboleth.hs.HandleRepository;
 import edu.internet2.middleware.shibboleth.hs.HandleRepositoryException;
+import edu.internet2.middleware.shibboleth.hs.InvalidHandleException;
 
 /**
  * <code>HandleRepository</code> implementation that uses a static cache.  This requires
@@ -100,11 +101,11 @@ public class MemoryHandleRepository extends BaseHandleRepository implements Hand
        /**
         * @see edu.internet2.middleware.shibboleth.hs.HandleRepository#getPrincipal(String)
         */
-       public AuthNPrincipal getPrincipal(String handle) {
+       public AuthNPrincipal getPrincipal(String handle) throws InvalidHandleException {
                synchronized (cache.handleEntries) {
                        if (!cache.handleEntries.containsKey(handle)) {
-                               log.debug("Repository does not contain an entry for this Attribute Query Handle.");
-                               return null;
+                               log.debug("The Repository does not contain an entry for this Attribute Query Handle.");
+                               throw new InvalidHandleException("The Repository does not contain an entry for this Attribute Query Handle.");
                        }
                }
                HandleEntry handleEntry;
@@ -116,7 +117,7 @@ public class MemoryHandleRepository extends BaseHandleRepository implements Hand
                        synchronized (cache.handleEntries) {
                                cache.handleEntries.remove(handle);
                        }
-                       return null;
+                       throw new InvalidHandleException("Attribute Query Handle is expired.");
                } else {
                        log.debug("Attribute Query Handle recognized.");
                        return handleEntry.principal;