harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From smish...@apache.org
Subject svn commit: r449547 - in /incubator/harmony/enhanced/classlib/trunk/modules/tools/src/main/java/org/apache/harmony/tools: keytool/ toolutils/
Date Mon, 25 Sep 2006 02:32:55 GMT
Author: smishura
Date: Sun Sep 24 19:32:55 2006
New Revision: 449547

URL: http://svn.apache.org/viewvc?view=rev&rev=449547
Log:
Apply patch for HARMONY-1535 ([classlib][tools] Keytool - code refactoring)

Added:
    incubator/harmony/enhanced/classlib/trunk/modules/tools/src/main/java/org/apache/harmony/tools/keytool/KeytoolKSLoaderSaver.java
  (with props)
    incubator/harmony/enhanced/classlib/trunk/modules/tools/src/main/java/org/apache/harmony/tools/toolutils/
    incubator/harmony/enhanced/classlib/trunk/modules/tools/src/main/java/org/apache/harmony/tools/toolutils/KeyStoreLoaderSaver.java
  (with props)
Removed:
    incubator/harmony/enhanced/classlib/trunk/modules/tools/src/main/java/org/apache/harmony/tools/keytool/KeyStoreLoaderSaver.java
Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/tools/src/main/java/org/apache/harmony/tools/keytool/KeyStoreConverter.java
    incubator/harmony/enhanced/classlib/trunk/modules/tools/src/main/java/org/apache/harmony/tools/keytool/KeytoolParameters.java
    incubator/harmony/enhanced/classlib/trunk/modules/tools/src/main/java/org/apache/harmony/tools/keytool/Main.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/tools/src/main/java/org/apache/harmony/tools/keytool/KeyStoreConverter.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/tools/src/main/java/org/apache/harmony/tools/keytool/KeyStoreConverter.java?view=diff&rev=449547&r1=449546&r2=449547
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/tools/src/main/java/org/apache/harmony/tools/keytool/KeyStoreConverter.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/tools/src/main/java/org/apache/harmony/tools/keytool/KeyStoreConverter.java
Sun Sep 24 19:32:55 2006
@@ -50,7 +50,7 @@
         String ksProvider = (param.getConvKsProvider() != null) ? param
                 .getConvKsProvider() : param.getProvider();
         // creating a new keystore
-        KeyStore convertedKS = KeyStoreLoaderSaver.loadStore(null, param
+        KeyStore convertedKS = KeytoolKSLoaderSaver.loadStore(null, param
                 .getConvertedKeyStoreType(), param.getConvertedKeyStorePass(),
                 ksProvider);
 
@@ -120,7 +120,7 @@
         }
             
         // save the converted keystore
-        KeyStoreLoaderSaver.saveStore(convertedKS, param
+        KeytoolKSLoaderSaver.saveStore(convertedKS, param
                 .getConvertedKeyStorePath(), param.getConvertedKeyStorePass(),
                 param.isVerbose());
     }

Added: incubator/harmony/enhanced/classlib/trunk/modules/tools/src/main/java/org/apache/harmony/tools/keytool/KeytoolKSLoaderSaver.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/tools/src/main/java/org/apache/harmony/tools/keytool/KeytoolKSLoaderSaver.java?view=auto&rev=449547
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/tools/src/main/java/org/apache/harmony/tools/keytool/KeytoolKSLoaderSaver.java
(added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/tools/src/main/java/org/apache/harmony/tools/keytool/KeytoolKSLoaderSaver.java
Sun Sep 24 19:32:55 2006
@@ -0,0 +1,115 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+
+package org.apache.harmony.tools.keytool;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.security.KeyStore;
+import java.security.KeyStoreException;
+import java.security.NoSuchAlgorithmException;
+import java.security.NoSuchProviderException;
+import java.security.cert.CertificateException;
+
+import org.apache.harmony.tools.toolutils.KeyStoreLoaderSaver;
+
+/**
+ * Class for loading the main keystore, saving ang changing its password.
+ */
+public class KeytoolKSLoaderSaver extends KeyStoreLoaderSaver{
+    /**
+     * Creates an instance of class KeyStore and loads a keystore to it.
+     * param.getStorePass() is used to check the integrity of the keystore. If
+     * the password is not set in param, the integrity is not checked. If the
+     * path to the store is not defined an empty keystore is created.
+     * 
+     * @param param -
+     *            KeytoolParameters object which is used to get path to the
+     *            store and password to unlock it or check its integrity.
+     * @throws NoSuchAlgorithmException
+     * @throws CertificateException
+     * @throws FileNotFoundException
+     * @throws IOException
+     * @throws KeyStoreException
+     * @throws NoSuchProviderException
+     */
+    static void loadStore(KeytoolParameters param)
+            throws NoSuchAlgorithmException, CertificateException,
+            FileNotFoundException, IOException, KeyStoreException,
+            NoSuchProviderException {
+
+        // If the path to the store is not specified, try to open
+        // the store using the default path.
+        if (param.getStorePath() == null) {
+            param.setStorePath(KeytoolParameters.defaultKeystorePath);
+        }
+        String ksProvider = (param.getKsProvider() != null) ? param
+                .getKsProvider() : param.getProvider();
+        KeyStore keyStore;
+        File ksFile;
+        URI uri;
+        try {
+            uri = new URI(param.getStorePath());
+            ksFile = new File(uri);
+        } catch (URISyntaxException e) {
+            ksFile = new File(param.getStorePath());
+        }
+        if (ksFile.exists()) {
+            // load an existing store
+            keyStore = loadStore(param.getStorePath(), param.getStoreType(),
+                    param.getStorePass(), ksProvider);
+        } else {
+            // create a new store if it doesn't exist
+            keyStore = loadStore(null, param.getStoreType(), param
+                    .getStorePass(), ksProvider);
+            param.setNeedSaveKS(true);
+        }
+        param.setKeyStore(keyStore);
+    }
+
+    /**
+     * Saves the main keystore to the file and protects its integrity with
+     * password.
+     * 
+     * @throws KeyStoreException
+     * @throws NoSuchAlgorithmException
+     * @throws CertificateException
+     * @throws IOException
+     * @throws NoSuchProviderException 
+     */
+    static void saveStore(KeytoolParameters param) throws KeyStoreException,
+            NoSuchAlgorithmException, CertificateException, IOException,
+            NoSuchProviderException{
+        saveStore(param.getKeyStore(), param.getStorePath(), param
+                .getStorePass(), param.isVerbose());
+    }
+
+    /**
+     * Changes the keystore password to the new one.
+     * 
+     * @param param
+     */
+    static void storePasswd(KeytoolParameters param) {
+        param.setStorePass(param.getNewPasswd());
+    }
+}
+

Propchange: incubator/harmony/enhanced/classlib/trunk/modules/tools/src/main/java/org/apache/harmony/tools/keytool/KeytoolKSLoaderSaver.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: incubator/harmony/enhanced/classlib/trunk/modules/tools/src/main/java/org/apache/harmony/tools/keytool/KeytoolParameters.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/tools/src/main/java/org/apache/harmony/tools/keytool/KeytoolParameters.java?view=diff&rev=449547&r1=449546&r2=449547
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/tools/src/main/java/org/apache/harmony/tools/keytool/KeytoolParameters.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/tools/src/main/java/org/apache/harmony/tools/keytool/KeytoolParameters.java
Sun Sep 24 19:32:55 2006
@@ -240,7 +240,7 @@
             CertificateException, FileNotFoundException, KeyStoreException,
             NoSuchProviderException, IOException {
         if (keyStore == null){
-            KeyStoreLoaderSaver.loadStore(this);
+            KeytoolKSLoaderSaver.loadStore(this);
         }
         return keyStore;
     }
@@ -890,7 +890,7 @@
             NoSuchProviderException, IOException {
         if (cacerts == null) {
             String keyStoreProv = (ksProvider != null) ? ksProvider : provider;
-            cacerts = KeyStoreLoaderSaver.loadStore(getCacertsPath(),
+            cacerts = KeytoolKSLoaderSaver.loadStore(getCacertsPath(),
                     storeType, getCacertsPass(), keyStoreProv);
         }
         return cacerts;

Modified: incubator/harmony/enhanced/classlib/trunk/modules/tools/src/main/java/org/apache/harmony/tools/keytool/Main.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/tools/src/main/java/org/apache/harmony/tools/keytool/Main.java?view=diff&rev=449547&r1=449546&r2=449547
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/tools/src/main/java/org/apache/harmony/tools/keytool/Main.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/tools/src/main/java/org/apache/harmony/tools/keytool/Main.java
Sun Sep 24 19:32:55 2006
@@ -47,7 +47,7 @@
                 EntryManager.delete(param);
                 break;
             case STOREPASSWD:
-                KeyStoreLoaderSaver.storePasswd(param);
+                KeytoolKSLoaderSaver.storePasswd(param);
                 break;
             case KEYPASSWD:
                 EntryManager.keyPasswd(param);
@@ -126,7 +126,7 @@
 
         if (param.isNeedSaveKS()) {
             // save the store
-            KeyStoreLoaderSaver.saveStore(param);
+            KeytoolKSLoaderSaver.saveStore(param);
         }
     }
 

Added: incubator/harmony/enhanced/classlib/trunk/modules/tools/src/main/java/org/apache/harmony/tools/toolutils/KeyStoreLoaderSaver.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/tools/src/main/java/org/apache/harmony/tools/toolutils/KeyStoreLoaderSaver.java?view=auto&rev=449547
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/tools/src/main/java/org/apache/harmony/tools/toolutils/KeyStoreLoaderSaver.java
(added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/tools/src/main/java/org/apache/harmony/tools/toolutils/KeyStoreLoaderSaver.java
Sun Sep 24 19:32:55 2006
@@ -0,0 +1,179 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+
+package org.apache.harmony.tools.toolutils;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.security.KeyStore;
+import java.security.KeyStoreException;
+import java.security.NoSuchAlgorithmException;
+import java.security.NoSuchProviderException;
+import java.security.cert.CertificateException;
+
+import org.apache.harmony.tools.keytool.KeytoolParameters;
+
+/**
+ * Class for loading and saving keystores.
+ */
+public class KeyStoreLoaderSaver {
+    
+    
+    /**
+     * Creates an instance of class KeyStore and loads a keystore of the
+     * specified type to it. If the type is not specified the default one is
+     * used. Password storePass is used to check the integrity of the keystore.
+     * If the password is null, the integrity is not checked. If the path to the
+     * store is not defined, an empty keystore is created.
+     * 
+     * @param path
+     * @param storeType
+     * @param storePass
+     * @param providerName
+     * @return
+     * @throws FileNotFoundException
+     * @throws KeyStoreException
+     * @throws NoSuchAlgorithmException
+     * @throws CertificateException
+     * @throws IOException
+     * @throws NoSuchProviderException
+     */
+    public static KeyStore loadStore(String path, String storeType, char[] storePass,
+            String providerName) throws FileNotFoundException,
+            KeyStoreException, NoSuchAlgorithmException, CertificateException,
+            IOException, NoSuchProviderException{
+
+        BufferedInputStream bis;
+        // if the path is given, make a FileInputStream on it
+        if (path != null) {
+            File ksFile = null;
+            URI uri = null;
+            try {
+                uri = new URI(path);
+                ksFile = new File(uri);
+            } catch (URISyntaxException e) {
+                ksFile = new File(path);
+            }
+            
+            if (ksFile.length() == 0) {
+                throw new IOException("Keystore file exists but is empty");
+            }
+            bis = new BufferedInputStream(new FileInputStream(ksFile));
+        } else { // if the path is not given, a new keystore will be created
+            bis = null;
+        }
+
+        // Set the store type to default if it is not given.
+        // The default value is set up in java.security file.
+        String checkedStoreType = (storeType != null) ? storeType : KeyStore
+                .getDefaultType();
+        KeyStore keyStore;
+        // if the provider name is not set
+        if (providerName == null) {
+            keyStore = KeyStore.getInstance(checkedStoreType);
+        } else {
+            try {
+                keyStore = KeyStore.getInstance(storeType, providerName);
+            } catch (NoSuchProviderException e) {
+                throw (NoSuchProviderException) new NoSuchProviderException(
+                        "The provider " + providerName
+                                + " is not found in the environment.")
+                        .initCause(e);
+            }
+        }
+
+        try {
+            // try to load the keystore
+            keyStore.load(bis, storePass);
+        } catch (NoSuchAlgorithmException e) {
+            throw new NoSuchAlgorithmException(
+                    "Failed to find the algorithm to check the keystore integrity",
+                    e);
+        } catch (CertificateException e) {
+            throw new CertificateException(
+                    "Failed to load a certificate from the keystore. ", e);
+        } catch (IOException e) {
+            throw (IOException) new IOException("Failed to load the keystore. ")
+                    .initCause(e);
+        }
+        return keyStore;
+    }
+
+    /**
+     * Saves a keystore to the file and protects its integrity with password.
+     * 
+     * @throws KeyStoreException
+     * @throws NoSuchAlgorithmException
+     * @throws CertificateException
+     * @throws IOException
+     */
+    public static void saveStore(KeyStore keyStore, String storePath,
+            char[] storePass, boolean isVerbose)
+            throws KeyStoreException, NoSuchAlgorithmException,
+            CertificateException, IOException {
+        // TODO: store not only to a file?
+
+        // if the program should output additional information, do it
+        if (isVerbose) {
+            System.out.println("[Saving " + storePath + "]");
+        }
+
+        // if the path to the store is not set, use the default value
+        if (storePath == null) {
+            storePath = KeytoolParameters.defaultKeystorePath;
+        }
+
+        
+        File ksFile = null;
+        URI uri = null;
+        try {
+            uri = new URI(storePath);
+            ksFile = new File(uri);
+        } catch (URISyntaxException e) {
+            ksFile = new File(storePath);
+        }
+        
+        // the file will be created if and only if one with the same name
+        // doesn't exist
+        ksFile.createNewFile();
+        BufferedOutputStream bos = new BufferedOutputStream(
+                new FileOutputStream(ksFile));
+        try {
+            keyStore.store(bos, storePass);
+        } catch (NoSuchAlgorithmException e) {
+            throw new NoSuchAlgorithmException(
+                    "Failed to find the algorithm to check the keystore integrity",
+                    e);
+        } catch (CertificateException e) {
+            throw new CertificateException(
+                    "Failed to save a certificate to the keystore. ", e);
+        } catch (IOException e) {
+            throw (IOException) new IOException("Failed to save the keystore. ")
+                    .initCause(e);
+        }
+    }
+}
+

Propchange: incubator/harmony/enhanced/classlib/trunk/modules/tools/src/main/java/org/apache/harmony/tools/toolutils/KeyStoreLoaderSaver.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message