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,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.servicemix.soap.handlers.security;
018    
019    import java.security.KeyStoreException;
020    import java.security.PrivateKey;
021    import java.security.cert.Certificate;
022    import java.util.ArrayList;
023    import java.util.Arrays;
024    import java.util.List;
025    
026    import org.apache.servicemix.common.security.KeystoreInstance;
027    import org.apache.servicemix.common.security.KeystoreManager;
028    
029    public class KeystoreInstanceCrypto extends BaseCrypto {
030    
031        private KeystoreInstance keystore;
032        
033        public KeystoreInstanceCrypto() {
034        }
035        
036        public KeystoreInstanceCrypto(KeystoreInstance keystore) {
037            this.keystore = keystore;
038        }
039        
040        public KeystoreInstanceCrypto(KeystoreManager keystoreManager, String keystore) {
041            this.keystore = keystoreManager.getKeystore(keystore);
042        }
043        
044        /**
045         * @return the keystore
046         */
047        public KeystoreInstance getKeystore() {
048            return keystore;
049        }
050    
051        /**
052         * @param keystore the keystore to set
053         */
054        public void setKeystore(KeystoreInstance keystore) {
055            this.keystore = keystore;
056        }
057    
058        protected String[] getAliases() throws KeyStoreException {
059            String[] pks = keystore.listPrivateKeys();
060            String[] tcs = keystore.listTrustCertificates();
061            List aliases = new ArrayList();
062            aliases.addAll(Arrays.asList(pks));
063            aliases.addAll(Arrays.asList(tcs));
064            return (String[]) aliases.toArray(new String[aliases.size()]);
065        }
066    
067        protected Certificate getCertificate(String alias) throws KeyStoreException {
068            return keystore.getCertificate(alias);
069        }
070    
071        protected String getCertificateAlias(Certificate cert) throws KeyStoreException {
072            return keystore.getCertificateAlias(cert);
073        }
074    
075        protected Certificate[] getCertificateChain(String alias) throws KeyStoreException {
076            return keystore.getCertificateChain(alias);
077        }
078    
079        public PrivateKey getPrivateKey(String alias, String password) throws Exception {
080            return keystore.getPrivateKey(alias);
081        }
082    
083        protected String[] getTrustCertificates() throws KeyStoreException {
084            return keystore.listTrustCertificates();
085        }
086    
087    }