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.common.security;
018    
019    import java.security.GeneralSecurityException;
020    import java.lang.reflect.InvocationHandler;
021    import java.lang.reflect.Method;
022    
023    import javax.net.ssl.SSLContext;
024    import javax.net.ssl.SSLServerSocketFactory;
025    import javax.net.ssl.SSLSocketFactory;
026    
027    /**
028     * Based on http://svn.apache.org/repos/asf/geronimo/trunk/modules/management/
029     *                      src/java/org/apache/geronimo/management/geronimo/KeystoreManager.java
030     *
031     */
032    public interface KeystoreManager {
033    
034        KeystoreInstance getKeystore(String name);
035    
036        /**
037         * Gets a SSLContext using one Keystore to access the private key
038         * and another to provide the list of trusted certificate authorities.
039         * @param provider
040         * @param protocol The SSL protocol to use
041         * @param algorithm The SSL algorithm to use
042         * @param keyStore The key keystore name as provided by listKeystores.  The
043         *                 KeystoreInstance for this keystore must be unlocked.
044         * @param keyAlias The name of the private key in the keystore.  The
045         *                 KeystoreInstance for this keystore must have unlocked
046         *                 this key.
047         * @param trustStore The trust keystore name as provided by listKeystores.
048         *                   The KeystoreInstance for this keystore must have
049         *                   unlocked this key.
050         *
051         * @throws KeystoreIsLocked Occurs when the requested key keystore cannot
052         *                          be used because it has not been unlocked.
053         * @throws KeyIsLocked Occurs when the requested private key in the key
054         *                     keystore cannot be used because it has not been
055         *                     unlocked.
056         */
057        SSLContext createSSLContext(String provider, String protocol,
058                                    String algorithm, String keyStore,
059                                    String keyAlias, String trustStore) throws GeneralSecurityException;
060        /**
061         * Gets a ServerSocketFactory using one Keystore to access the private key
062         * and another to provide the list of trusted certificate authorities.
063         * @param provider
064         * @param protocol The SSL protocol to use
065         * @param algorithm The SSL algorithm to use
066         * @param keyStore The key keystore name as provided by listKeystores.  The
067         *                 KeystoreInstance for this keystore must be unlocked.
068         * @param keyAlias The name of the private key in the keystore.  The
069         *                 KeystoreInstance for this keystore must have unlocked
070         *                 this key.
071         * @param trustStore The trust keystore name as provided by listKeystores.
072         *                   The KeystoreInstance for this keystore must have
073         *                   unlocked this key.
074         *
075         * @throws KeystoreIsLocked Occurs when the requested key keystore cannot
076         *                          be used because it has not been unlocked.
077         * @throws KeyIsLocked Occurs when the requested private key in the key
078         *                     keystore cannot be used because it has not been
079         *                     unlocked.
080         */
081        SSLServerSocketFactory createSSLServerFactory(String provider, String protocol,
082                                                      String algorithm, String keyStore,
083                                                      String keyAlias, String trustStore) throws GeneralSecurityException;
084    
085        /**
086         * Gets a SocketFactory using one Keystore to access the private key
087         * and another to provide the list of trusted certificate authorities.
088         * @param provider The SSL provider to use, or null for the default
089         * @param protocol The SSL protocol to use
090         * @param algorithm The SSL algorithm to use
091         * @param keyStore The key keystore name as provided by listKeystores.  The
092         *                 KeystoreInstance for this keystore must be unlocked.
093         * @param keyAlias The name of the private key in the keystore.  The
094         *                 KeystoreInstance for this keystore must have unlocked
095         *                 this key.
096         * @param trustStore The trust keystore name as provided by listKeystores.
097         *                   The KeystoreInstance for this keystore must have
098         *                   unlocked this key.
099         *
100         * @throws KeystoreIsLocked Occurs when the requested key keystore cannot
101         *                          be used because it has not been unlocked.
102         * @throws KeyIsLocked Occurs when the requested private key in the key
103         *                     keystore cannot be used because it has not been
104         *                     unlocked.
105         * @throws GeneralSecurityException
106         */
107        SSLSocketFactory createSSLFactory(String provider, String protocol,
108                                          String algorithm, String keyStore,
109                                          String keyAlias, String trustStore) throws GeneralSecurityException;
110    
111    
112        public static final class Proxy {
113            public static KeystoreManager create(final Object target) {
114                return (KeystoreManager) java.lang.reflect.Proxy.newProxyInstance(KeystoreManager.class.getClassLoader(),
115                                                                                  new Class[] { KeystoreManager.class },
116                                                                                  new InvocationHandler() {
117                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
118                        Object o = target.getClass().getMethod(method.getName(), method.getParameterTypes()).invoke(target, args);
119                        if (method.getName().equals("getKeystore")) {
120                            o = KeystoreInstance.Proxy.create(o);
121                        }
122                        return o;
123                    }
124                });
125            }
126        }
127    }