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.keytool;
021:
022: import java.io.File;
023: import java.io.FileNotFoundException;
024: import java.io.IOException;
025: import java.net.URI;
026: import java.net.URISyntaxException;
027: import java.security.KeyStore;
028: import java.security.KeyStoreException;
029: import java.security.NoSuchAlgorithmException;
030: import java.security.NoSuchProviderException;
031: import java.security.cert.CertificateException;
032:
033: import org.apache.harmony.tools.toolutils.KeyStoreLoaderSaver;
034:
035: /**
036: * Class for loading the main keystore, saving ang changing its password.
037: */
038: public class KeytoolKSLoaderSaver {
039: /**
040: * Creates an instance of class KeyStore and loads a keystore to it.
041: * param.getStorePass() is used to check the integrity of the keystore. If
042: * the password is not set in param, the integrity is not checked. If the
043: * path to the store is not defined an empty keystore is created.
044: *
045: * @param param -
046: * KeytoolParameters object which is used to get path to the
047: * store and password to unlock it or check its integrity.
048: * @throws NoSuchAlgorithmException
049: * @throws CertificateException
050: * @throws FileNotFoundException
051: * @throws IOException
052: * @throws KeyStoreException
053: * @throws NoSuchProviderException
054: */
055: static void loadStore(KeytoolParameters param)
056: throws NoSuchAlgorithmException, CertificateException,
057: FileNotFoundException, IOException, KeyStoreException,
058: NoSuchProviderException {
059:
060: // If the path to the store is not specified, try to open
061: // the store using the default path.
062: if (param.getStorePath() == null) {
063: param.setStorePath(KeytoolParameters.defaultKeystorePath);
064: }
065: String ksProvider = (param.getKsProvider() != null) ? param
066: .getKsProvider() : param.getProvider();
067: KeyStore keyStore;
068: File ksFile;
069: URI uri;
070: try {
071: uri = new URI(param.getStorePath());
072: ksFile = new File(uri);
073: } catch (URISyntaxException e) {
074: ksFile = new File(param.getStorePath());
075: } catch (IllegalArgumentException e) {
076: ksFile = new File(param.getStorePath());
077: }
078: if (ksFile.exists()) {
079: // load an existing store
080: keyStore = KeyStoreLoaderSaver.loadStore(param
081: .getStorePath(), param.getStoreType(), param
082: .getStorePass(), ksProvider);
083: } else {
084: // create a new store if it doesn't exist
085: keyStore = KeyStoreLoaderSaver.loadStore(null, param
086: .getStoreType(), param.getStorePass(), ksProvider);
087: param.setNeedSaveKS(true);
088: }
089: param.setKeyStore(keyStore);
090: }
091:
092: /**
093: * Saves the main keystore to the file and protects its integrity with
094: * password.
095: *
096: * @throws KeyStoreException
097: * @throws NoSuchAlgorithmException
098: * @throws CertificateException
099: * @throws IOException
100: * @throws NoSuchProviderException
101: */
102: static void saveStore(KeytoolParameters param)
103: throws KeyStoreException, NoSuchAlgorithmException,
104: CertificateException, IOException, NoSuchProviderException {
105: KeyStoreLoaderSaver.saveStore(param.getKeyStore(), param
106: .getStorePath(), param.getStorePass(), param
107: .isVerbose());
108: }
109:
110: /**
111: * Changes the keystore password to the new one.
112: *
113: * @param param
114: */
115: static void storePasswd(KeytoolParameters param) {
116: param.setStorePass(param.getNewPasswd());
117: }
118: }
|