001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
013: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
014: * License for the specific language governing permissions and limitations under
015: * the License.
016: */
017:
018: package org.apache.harmony.tools.keytool;
019:
020: import java.io.FileNotFoundException;
021: import java.io.IOException;
022: import java.security.Key;
023: import java.security.KeyStore;
024: import java.security.KeyStoreException;
025: import java.security.NoSuchAlgorithmException;
026: import java.security.NoSuchProviderException;
027: import java.security.UnrecoverableKeyException;
028: import java.security.cert.Certificate;
029: import java.security.cert.CertificateException;
030:
031: /**
032: * Class for managing keystore entries - cloning, deleting, changing entry
033: * password.
034: */
035: public class EntryManager {
036: /**
037: * Copies the key and the certificate chain (if any) from the keystore entry
038: * identified by given alias into a newly created one with given destination
039: * alias. alias and destination alias are specified in param.
040: *
041: * @param param
042: * @throws UnrecoverableKeyException
043: * @throws NoSuchAlgorithmException
044: * @throws KeyStoreException
045: * @throws KeytoolException
046: * @throws IOException
047: * @throws NoSuchProviderException
048: * @throws FileNotFoundException
049: * @throws CertificateException
050: */
051: static void keyClone(KeytoolParameters param)
052: throws KeyStoreException, NoSuchAlgorithmException,
053: UnrecoverableKeyException, KeytoolException,
054: CertificateException, FileNotFoundException,
055: NoSuchProviderException, IOException {
056: KeyStore keyStore = param.getKeyStore();
057: String alias = param.getAlias();
058: Key srcKey;
059: try {
060: srcKey = keyStore.getKey(alias, param.getKeyPass());
061: } catch (NoSuchAlgorithmException e) {
062: throw new NoSuchAlgorithmException(
063: "Cannot find the algorithm to recover the key. ", e);
064: }
065: // if the entry is a not a KeyEntry
066: if (srcKey == null) {
067: throw new KeytoolException("The entry <" + alias
068: + "> has no key.");
069: }
070: Certificate[] certChain = keyStore.getCertificateChain(param
071: .getAlias());
072: keyStore.setKeyEntry(param.getDestAlias(), srcKey, param
073: .getNewPasswd(), certChain);
074: param.setNeedSaveKS(true);
075: }
076:
077: /**
078: * Removes from the keystore the entry associated with alias.
079: *
080: * @param param
081: * @throws KeyStoreException
082: * @throws IOException
083: * @throws NoSuchProviderException
084: * @throws FileNotFoundException
085: * @throws CertificateException
086: * @throws NoSuchAlgorithmException
087: */
088: static void delete(KeytoolParameters param)
089: throws KeyStoreException, NoSuchAlgorithmException,
090: CertificateException, FileNotFoundException,
091: NoSuchProviderException, IOException {
092: param.getKeyStore().deleteEntry(param.getAlias());
093: param.setNeedSaveKS(true);
094: }
095:
096: /**
097: * Changes the key password to the new one.
098: *
099: * @param param
100: * @throws KeyStoreException
101: * @throws NoSuchAlgorithmException
102: * @throws UnrecoverableKeyException
103: * @throws IOException
104: * @throws NoSuchProviderException
105: * @throws FileNotFoundException
106: * @throws CertificateException
107: */
108: static void keyPasswd(KeytoolParameters param)
109: throws KeyStoreException, NoSuchAlgorithmException,
110: UnrecoverableKeyException, CertificateException,
111: FileNotFoundException, NoSuchProviderException, IOException {
112: KeyStore keyStore = param.getKeyStore();
113: String alias = param.getAlias();
114: Key key;
115: Certificate[] chain;
116: try {
117: key = keyStore.getKey(alias, param.getKeyPass());
118: chain = keyStore.getCertificateChain(alias);
119: } catch (NoSuchAlgorithmException e) {
120: throw new NoSuchAlgorithmException(
121: "Cannot find the algorithm to recover the key. ", e);
122: }
123:
124: keyStore.deleteEntry(alias);
125: keyStore.setKeyEntry(alias, key, param.getNewPasswd(), chain);
126: param.setKeyPass(param.getNewPasswd());
127: param.setNeedSaveKS(true);
128: }
129:
130: }
|