You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by ml...@apache.org on 2006/07/10 13:26:53 UTC

svn commit: r420492 - in /incubator/harmony/enhanced/classlib/trunk/modules/security/src: main/java/common/java/security/KeyStoreSpi.java test/api/java/org/apache/harmony/security/tests/java/security/KeyStoreTest.java

Author: mloenko
Date: Mon Jul 10 04:26:52 2006
New Revision: 420492

URL: http://svn.apache.org/viewvc?rev=420492&view=rev
Log:
fixes for HARMONY-615
java.security.KeyStoreSpi.engineEntryInstanceOf(..) doesn't distinguish PrivateKeyEntry and SecretKeyEntry

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/java/security/KeyStoreSpi.java
    incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/api/java/org/apache/harmony/security/tests/java/security/KeyStoreTest.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/java/security/KeyStoreSpi.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/java/security/KeyStoreSpi.java?rev=420492&r1=420491&r2=420492&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/java/security/KeyStoreSpi.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/java/security/KeyStoreSpi.java Mon Jul 10 04:26:52 2006
@@ -14,11 +14,6 @@
  *  limitations under the License.
  */
 
-/**
-* @author Vera Y. Petrashkova
-* @version $Revision$
-*/
-
 package java.security;
 
 import java.io.IOException;
@@ -34,9 +29,6 @@
 
 public abstract class KeyStoreSpi {
 
-    public KeyStoreSpi() {
-    }
-
     public abstract Key engineGetKey(String alias, char[] password)
             throws NoSuchAlgorithmException, UnrecoverableKeyException;
 
@@ -99,7 +91,7 @@
                 engineLoad(null, pwd);
                 return;
             } catch (Exception e) {
-                throw new IllegalArgumentException(e.toString());
+                throw new IllegalArgumentException(e);
             }
         }
         if (pp instanceof KeyStore.CallbackHandlerProtection) {
@@ -108,7 +100,7 @@
                 engineLoad(null, pwd);
                 return;
             } catch (Exception e) {
-                throw new IllegalArgumentException(e.toString());
+                throw new IllegalArgumentException(e);
             }
         }
         throw new UnsupportedOperationException(
@@ -164,14 +156,17 @@
         if (entry == null) {
             throw new KeyStoreException("entry is null");
         }
+
         if (engineContainsAlias(alias)) {
             engineDeleteEntry(alias);
         }
+
         if (entry instanceof KeyStore.TrustedCertificateEntry) {
             KeyStore.TrustedCertificateEntry trE = (KeyStore.TrustedCertificateEntry) entry;
             engineSetCertificateEntry(alias, trE.getTrustedCertificate());
             return;
         }
+
         char[] passW = null;
         if (protParam instanceof KeyStore.PasswordProtection) {
             try {
@@ -191,51 +186,53 @@
                         "protParam should be PasswordProtection or CallbackHandlerProtection");
             }
         }
+
         if (entry instanceof KeyStore.PrivateKeyEntry) {
             KeyStore.PrivateKeyEntry prE = (KeyStore.PrivateKeyEntry) entry;
             engineSetKeyEntry(alias, prE.getPrivateKey(), passW, prE
                     .getCertificateChain());
             return;
         }
+
         if (entry instanceof KeyStore.SecretKeyEntry) {
             KeyStore.SecretKeyEntry skE = (KeyStore.SecretKeyEntry) entry;
             engineSetKeyEntry(alias, skE.getSecretKey(), passW, null);
             //            engineSetKeyEntry(alias, skE.getSecretKey().getEncoded(), null);
             return;
         }
+
         throw new KeyStoreException(
                 "Entry object is neither PrivateKeyObject nor SecretKeyEntry "
                         + "nor TrustedCertificateEntry: " + entry.toString());
     }
 
-    public boolean engineEntryInstanceOf(String alias, 
+    public boolean engineEntryInstanceOf(String alias,
             Class<? extends KeyStore.Entry> entryClass) {
         if (!engineContainsAlias(alias)) {
             return false;
         }
-        Class<?> cl1 = null;
-        Class<?> cl2 = null;
+
         try {
             if (engineIsCertificateEntry(alias)) {
-                cl1 = Class
-                        .forName("java.security.KeyStore$TrustedCertificateEntry");
-                return ((cl1 != null) ? entryClass.isAssignableFrom(cl1)
-                        : false);
+                return entryClass
+                        .isAssignableFrom(Class
+                                .forName("java.security.KeyStore$TrustedCertificateEntry"));
             }
+
             if (engineIsKeyEntry(alias)) {
-                cl1 = Class.forName("java.security.KeyStore$PrivateKeyEntry");
-                cl2 = Class.forName("java.security.KeyStore$SecretKeyEntry");
-            }
-            if ((cl1 != null) && entryClass.isAssignableFrom(cl1)) {
-                return true;
-            }
-            if ((cl2 != null) && entryClass.isAssignableFrom(cl2)) {
-                return true;
+                if (entryClass.isAssignableFrom(Class
+                        .forName("java.security.KeyStore$PrivateKeyEntry"))) {
+                    return engineGetCertificate(alias) != null;
+                }
+
+                if (entryClass.isAssignableFrom(Class
+                        .forName("java.security.KeyStore$SecretKeyEntry"))) {
+                    return engineGetCertificate(alias) == null;
+                }
             }
-            return false;
-        } catch (ClassNotFoundException e) {
-            return false;
-        }
+        } catch (ClassNotFoundException ignore) {}
+
+        return false;
     }
 
     /*
@@ -245,13 +242,16 @@
      */
     static char[] getPasswordFromCallBack(KeyStore.ProtectionParameter protParam)
             throws UnrecoverableEntryException {
+
         if (protParam == null) {
             return null;
         }
+
         if (!(protParam instanceof KeyStore.CallbackHandlerProtection)) {
             throw new UnrecoverableEntryException(
                     "Incorrect ProtectionParameter");
         }
+
         String clName = Security
                 .getProperty("auth.login.defaultCallbackHandler");
         if (clName == null) {
@@ -259,6 +259,7 @@
                     "Default CallbackHandler was not defined");
 
         }
+
         try {
             Class<?> cl = Class.forName(clName);
             CallbackHandler cbHand = (CallbackHandler) cl.newInstance();
@@ -269,5 +270,4 @@
             throw new UnrecoverableEntryException(e.toString());
         }
     }
-
-}
\ No newline at end of file
+}

Modified: incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/api/java/org/apache/harmony/security/tests/java/security/KeyStoreTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/api/java/org/apache/harmony/security/tests/java/security/KeyStoreTest.java?rev=420492&r1=420491&r2=420492&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/api/java/org/apache/harmony/security/tests/java/security/KeyStoreTest.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/api/java/org/apache/harmony/security/tests/java/security/KeyStoreTest.java Mon Jul 10 04:26:52 2006
@@ -28,6 +28,9 @@
 import java.security.Security;
 import java.security.cert.Certificate;
 
+import javax.crypto.KeyGenerator;
+import javax.crypto.SecretKey;
+
 import org.apache.harmony.security.tests.support.KeyStoreTestSupport;
 import org.apache.harmony.security.tests.support.MyLoadStoreParams;
 import org.apache.harmony.security.tests.support.SpiEngUtils;
@@ -210,5 +213,34 @@
             } catch (KeyStoreException e) {
             }
         }
+    }
+
+
+    /*
+     * @tests java.security.KeyStoreSpi.engineEntryInstanceOf(String, Class<? extends Entry>)
+     */
+    public void testEngineEntryInstanceOf() throws Exception {
+        //Regression for HARMONY-615
+
+        // create a KeyStore 
+        KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
+        keyStore.load(null, "pwd".toCharArray());
+
+        // genarate a key 
+        KeyGenerator keyGen = KeyGenerator.getInstance("DES");
+        keyGen.init(56);
+        SecretKey secretKey = keyGen.generateKey();
+
+        // put the key into keystore 
+        String alias = "alias";
+        keyStore.setKeyEntry(alias, secretKey, "pwd".toCharArray(), null);
+
+        // check if it is a secret key 
+        assertTrue(keyStore.entryInstanceOf(alias,
+                KeyStore.SecretKeyEntry.class));
+
+        // check if it is NOT a private key 
+        assertFalse(keyStore.entryInstanceOf(alias,
+                KeyStore.PrivateKeyEntry.class));
     }
 }