001    /*
002     * Copyright 2010 the original author or authors.
003     * 
004     *  Licensed under the Apache License, Version 2.0 (the "License");
005     *  you may not use this file except in compliance with the License.
006     *  You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     *  Unless required by applicable law or agreed to in writing, software
011     *  distributed under the License is distributed on an "AS IS" BASIS,
012     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     *  See the License for the specific language governing permissions and
014     *  limitations under the License.
015     */
016    package com.hs.mail.container.server;
017    
018    import java.io.File;
019    import java.io.FileInputStream;
020    import java.io.FileNotFoundException;
021    import java.io.InputStream;
022    import java.security.KeyStore;
023    
024    import javax.net.ssl.KeyManagerFactory;
025    import javax.net.ssl.SSLContext;
026    
027    /**
028     * Factory used to create SSLContexts.
029     * 
030     * @author Won Chul Doh
031     * @since Jul 29, 2010
032     * 
033     */
034    public class SSLContextFactory {
035    
036            /**
037             * Creates a SSLContext appropriate for use
038             * 
039             * @param keyStore
040             *            the Java keystore file
041             * @param keyStorePassword
042             *            password for the Java keystore
043             * @param certificatePassword
044             *            certificate password
045             * @return SSLContext
046             */
047            public static SSLContext createContext(String keyStore,
048                            String keyStorePassword, String certificatePassword) {
049                    try {
050                            String algorithm = "SunX509";
051                            KeyStore ks = KeyStore.getInstance("JKS");
052                            ks.load(asInputStream(keyStore), keyStorePassword.toCharArray());
053    
054                            // Set up key manager factory to use our key store
055                KeyManagerFactory kmf = KeyManagerFactory.getInstance(algorithm);
056                            kmf.init(ks, certificatePassword.toCharArray());
057    
058                // Initialize the SSLContext to work with our key managers.
059                SSLContext context = SSLContext.getInstance("TLS");
060                            context.init(kmf.getKeyManagers(), null, null);
061    
062                            return context;
063                    } catch (Exception e) {
064                            throw new Error(
065                        "Failed to initialize the server-side SSLContext", e);
066                    }
067            }
068    
069            private static InputStream asInputStream(String keyStore)
070                            throws FileNotFoundException {
071                    return new FileInputStream(new File(keyStore));
072            }
073    
074    }