2 * jkeytool - Core.java -
3 * Copyright © 2009 David Roden
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 package net.pterodactylus.jkeytool.core;
23 import java.io.FileInputStream;
24 import java.io.IOException;
25 import java.security.KeyStore;
26 import java.security.KeyStoreException;
27 import java.security.NoSuchAlgorithmException;
28 import java.security.cert.CertificateException;
29 import java.util.HashSet;
35 * @author David Roden <droden@gmail.com>
39 /** Support for {@link CoreListener}s. */
40 private CoreListenerSupport coreListenerSupport = new CoreListenerSupport(this);
42 /** All loaded keystores. */
43 private Set<KeyStore> keyStores = new HashSet<KeyStore>();
50 * Adds a core listener to the list of registered listeners.
55 public void addCoreListener(CoreListener coreListener) {
56 coreListenerSupport.addListener(coreListener);
60 * Removes a core listener from the list of registered listeners.
63 * The listener to remove
65 public void removeCoreListener(CoreListener coreListener) {
66 coreListenerSupport.removeListener(coreListener);
78 * Creates a new key store of the given type.
81 * The type of the key store
83 public void createKeyStore(String keyStoreType) {
85 KeyStore keyStore = KeyStore.getInstance(keyStoreType);
86 coreListenerSupport.fireKeyStoreCreated(keyStore);
87 } catch (KeyStoreException kse1) {
88 coreListenerSupport.fireKeyStoreNotCreated(keyStoreType, kse1);
93 * Loads a keystore from the given file. This method will fire either a
94 * {@link CoreListener#keyStoreLoaded(File, KeyStore)} or a
95 * {@link CoreListener#keyStoreNotLoaded(File)} event, depending on whether
96 * the loading succeeded or failed.
99 * The file to load the keystore from
101 public void loadKeyStore(File keyStoreFile) {
102 KeyStore keyStore = loadKeyStore(keyStoreFile, "PKCS12");
103 if (keyStore == null) {
104 keyStore = loadKeyStore(keyStoreFile, "JKS");
106 if (keyStore == null) {
107 coreListenerSupport.fireKeyStoreNotLoaded(keyStoreFile);
110 coreListenerSupport.fireKeyStoreLoaded(keyStoreFile, keyStore);
118 * Loads a keystore from the given file, trying to parse it as a keystore of
121 * @param keyStoreFile
122 * The file to read the keystore from
124 * The type of the keystore
125 * @return The loaded keystore, or <code>null</code> if the keystore could
128 private KeyStore loadKeyStore(File keyStoreFile, String type) {
129 FileInputStream keyStoreFileInputStream = null;
131 KeyStore keyStore = KeyStore.getInstance(type);
132 keyStoreFileInputStream = new FileInputStream(keyStoreFile);
133 keyStore.load(keyStoreFileInputStream, null);
134 keyStores.add(keyStore);
136 } catch (IOException ioe1) {
138 } catch (KeyStoreException kse1) {
140 } catch (NoSuchAlgorithmException nsae1) {
142 } catch (CertificateException ce1) {