001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.harmony.tools.toolutils;
021:
022: import java.io.BufferedInputStream;
023: import java.io.BufferedOutputStream;
024: import java.io.File;
025: import java.io.FileInputStream;
026: import java.io.FileNotFoundException;
027: import java.io.FileOutputStream;
028: import java.io.IOException;
029: import java.net.URI;
030: import java.net.URISyntaxException;
031: import java.security.KeyStore;
032: import java.security.KeyStoreException;
033: import java.security.NoSuchAlgorithmException;
034: import java.security.NoSuchProviderException;
035: import java.security.cert.CertificateException;
036:
037: import org.apache.harmony.tools.keytool.KeytoolParameters;
038:
039: /**
040: * Class for loading and saving keystores.
041: */
042: public class KeyStoreLoaderSaver {
043:
044: /**
045: * Creates an instance of class KeyStore and loads a keystore of the
046: * specified type to it. If the type is not specified the default one is
047: * used. Password storePass is used to check the integrity of the keystore.
048: * If the password is null, the integrity is not checked. If the path to the
049: * store is not defined, an empty keystore is created.
050: *
051: * @param path
052: * @param storeType
053: * @param storePass
054: * @param providerName
055: * @return
056: * @throws FileNotFoundException
057: * @throws KeyStoreException
058: * @throws NoSuchAlgorithmException
059: * @throws CertificateException
060: * @throws IOException
061: * @throws NoSuchProviderException
062: */
063: public static KeyStore loadStore(String path, String storeType,
064: char[] storePass, String providerName)
065: throws FileNotFoundException, KeyStoreException,
066: NoSuchAlgorithmException, CertificateException,
067: IOException, NoSuchProviderException {
068:
069: BufferedInputStream bis;
070: // if the path is given, make a FileInputStream on it
071: if (path != null) {
072: File ksFile = null;
073: URI uri = null;
074: try {
075: uri = new URI(path);
076: ksFile = new File(uri);
077: } catch (URISyntaxException e) {
078: ksFile = new File(path);
079: } catch (IllegalArgumentException e) {
080: ksFile = new File(path);
081: }
082:
083: if (!ksFile.exists()) {
084: throw new IOException("Keystore file does not exist");
085: }
086: if (ksFile.length() == 0) {
087: throw new IOException(
088: "Keystore file exists but is empty");
089: }
090: bis = new BufferedInputStream(new FileInputStream(ksFile));
091: } else { // if the path is not given, a new keystore will be created
092: bis = null;
093: }
094:
095: // Set the store type to default if it is not given.
096: // The default value is set up in java.security file.
097: String checkedStoreType = (storeType != null) ? storeType
098: : KeyStore.getDefaultType();
099: KeyStore keyStore;
100: // if the provider name is not set
101: if (providerName == null) {
102: keyStore = KeyStore.getInstance(checkedStoreType);
103: } else {
104: try {
105: keyStore = KeyStore
106: .getInstance(storeType, providerName);
107: } catch (NoSuchProviderException e) {
108: throw (NoSuchProviderException) new NoSuchProviderException(
109: "The provider " + providerName
110: + " is not found in the environment.")
111: .initCause(e);
112: }
113: }
114:
115: try {
116: // try to load the keystore
117: keyStore.load(bis, storePass);
118: } catch (NoSuchAlgorithmException e) {
119: throw new NoSuchAlgorithmException(
120: "Failed to find the algorithm to check the keystore integrity",
121: e);
122: } catch (CertificateException e) {
123: throw new CertificateException(
124: "Failed to load a certificate from the keystore. ",
125: e);
126: } catch (IOException e) {
127: throw (IOException) new IOException(
128: "Failed to load the keystore. ").initCause(e);
129: } finally {
130: if (bis != null) {
131: bis.close();
132: }
133: }
134: return keyStore;
135: }
136:
137: /**
138: * Saves a keystore to the file and protects its integrity with password.
139: *
140: * @throws KeyStoreException
141: * @throws NoSuchAlgorithmException
142: * @throws CertificateException
143: * @throws IOException
144: */
145: public static void saveStore(KeyStore keyStore, String storePath,
146: char[] storePass, boolean isVerbose)
147: throws KeyStoreException, NoSuchAlgorithmException,
148: CertificateException, IOException {
149: // TODO: store not only to a file?
150:
151: // if the program should output additional information, do it
152: if (isVerbose) {
153: System.out.println("[Saving " + storePath + "]");
154: }
155:
156: // if the path to the store is not set, use the default value
157: if (storePath == null) {
158: storePath = KeytoolParameters.defaultKeystorePath;
159: }
160:
161: File ksFile = null;
162: URI uri = null;
163: try {
164: uri = new URI(storePath);
165: ksFile = new File(uri);
166: } catch (URISyntaxException e) {
167: ksFile = new File(storePath);
168: } catch (IllegalArgumentException e) {
169: ksFile = new File(storePath);
170: }
171:
172: // the file will be created if and only if one with the same name
173: // doesn't exist
174: ksFile.createNewFile();
175: BufferedOutputStream bos = new BufferedOutputStream(
176: new FileOutputStream(ksFile));
177: try {
178: keyStore.store(bos, storePass);
179: } catch (NoSuchAlgorithmException e) {
180: throw new NoSuchAlgorithmException(
181: "Failed to find the algorithm to check the keystore integrity",
182: e);
183: } catch (CertificateException e) {
184: throw new CertificateException(
185: "Failed to save a certificate to the keystore. ", e);
186: } catch (IOException e) {
187: throw (IOException) new IOException(
188: "Failed to save the keystore. ").initCause(e);
189: } finally {
190: bos.close();
191: }
192: }
193: }
|