001/*
002 * $HeadURL: http://juliusdavies.ca/svn/not-yet-commons-ssl/tags/commons-ssl-0.3.16/src/java/org/apache/commons/ssl/JavaImpl.java $
003 * $Revision: 155 $
004 * $Date: 2009-09-17 14:00:58 -0700 (Thu, 17 Sep 2009) $
005 *
006 * ====================================================================
007 * Licensed to the Apache Software Foundation (ASF) under one
008 * or more contributor license agreements.  See the NOTICE file
009 * distributed with this work for additional information
010 * regarding copyright ownership.  The ASF licenses this file
011 * to you under the Apache License, Version 2.0 (the
012 * "License"); you may not use this file except in compliance
013 * with the License.  You may obtain a copy of the License at
014 *
015 *   http://www.apache.org/licenses/LICENSE-2.0
016 *
017 * Unless required by applicable law or agreed to in writing,
018 * software distributed under the License is distributed on an
019 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020 * KIND, either express or implied.  See the License for the
021 * specific language governing permissions and limitations
022 * under the License.
023 * ====================================================================
024 *
025 * This software consists of voluntary contributions made by many
026 * individuals on behalf of the Apache Software Foundation.  For more
027 * information on the Apache Software Foundation, please see
028 * <http://www.apache.org/>.
029 *
030 */
031
032package org.apache.commons.ssl;
033
034import javax.net.SocketFactory;
035import javax.net.ssl.SSLPeerUnverifiedException;
036import javax.net.ssl.SSLServerSocket;
037import javax.net.ssl.SSLServerSocketFactory;
038import javax.net.ssl.SSLSession;
039import javax.net.ssl.SSLSocket;
040import javax.net.ssl.SSLSocketFactory;
041import java.io.IOException;
042import java.net.InetAddress;
043import java.net.Socket;
044import java.security.KeyManagementException;
045import java.security.KeyStore;
046import java.security.KeyStoreException;
047import java.security.NoSuchAlgorithmException;
048import java.security.UnrecoverableKeyException;
049import java.security.cert.Certificate;
050import java.security.cert.CertificateException;
051import java.security.cert.X509Certificate;
052
053/**
054 * @author Credit Union Central of British Columbia
055 * @author <a href="http://www.cucbc.com/">www.cucbc.com</a>
056 * @author <a href="mailto:juliusdavies@cucbc.com">juliusdavies@cucbc.com</a>
057 * @since 30-Jun-2006
058 */
059public abstract class JavaImpl {
060    private static JavaImpl HANDLER;
061
062    static {
063        JavaImpl h = null;
064        try {
065            h = Java14.getInstance();
066        }
067        catch (Throwable t) {
068            // System.out.println( t.toString() );
069            System.out.println("commons-ssl reverting to: Java 1.3 + jsse.jar");
070        }
071        if (h == null) {
072            h = Java13.getInstance();
073        }
074        HANDLER = h;
075    }
076
077    public static void downgrade() {
078        if (HANDLER instanceof Java14) {
079            HANDLER = Java13.getInstance();
080        }
081    }
082
083    public static boolean isJava13() {
084        return HANDLER instanceof Java13;
085    }
086
087    public static void uprade() {
088        if (HANDLER instanceof Java13) {
089            HANDLER = Java14.getInstance();
090        }
091    }
092
093    public abstract String getVersion();
094
095    protected abstract Object buildKeyManagerFactory(KeyStore ks, char[] pass)
096        throws NoSuchAlgorithmException, KeyStoreException,
097        UnrecoverableKeyException;
098
099    protected abstract Object[] retrieveKeyManagers(Object keyManagerFactory);
100
101    protected abstract Object buildTrustManagerFactory(KeyStore ks)
102        throws NoSuchAlgorithmException, KeyStoreException;
103
104    protected abstract Object[] retrieveTrustManagers(Object trustManagerFactory);
105
106    protected abstract String retrieveSubjectX500(X509Certificate cert);
107
108    protected abstract String retrieveIssuerX500(X509Certificate cert);
109
110    protected abstract Certificate[] retrievePeerCerts(SSLSession sslSession)
111        throws SSLPeerUnverifiedException;
112
113    protected abstract SSLSocketFactory buildSSLSocketFactory(Object ssl);
114
115    protected abstract SSLServerSocketFactory buildSSLServerSocketFactory(Object ssl);
116    
117    protected abstract SSLSocket buildSocket(SSL ssl)
118        throws IOException;
119
120    protected abstract SSLSocket buildSocket(
121            SSL ssl, String remoteHost, int remotePort, InetAddress localHost, int localPort, int connectTimeout
122    ) throws IOException;
123
124    protected abstract Socket buildPlainSocket(
125            SSL ssl, String remoteHost, int remotePort, InetAddress localHost, int localPort, int connectTimeout
126    ) throws IOException;
127
128    protected abstract Socket connectSocket(Socket s, SocketFactory sf,
129                                            String remoteHost, int remotePort,
130                                            InetAddress localHost, int localPort,
131                                            int timeout, SSL ssl)
132        throws IOException;
133
134    protected abstract SSLServerSocket buildServerSocket(SSL ssl)
135        throws IOException;
136
137    protected abstract void wantClientAuth(Object o, boolean wantClientAuth);
138
139    protected abstract void enabledProtocols(Object o, String[] enabledProtocols);
140
141    protected abstract RuntimeException buildRuntimeException(Exception cause);
142
143    protected abstract Object initSSL(SSL ssl, TrustChain tc, KeyMaterial km)
144        throws NoSuchAlgorithmException, KeyStoreException,
145        CertificateException, KeyManagementException, IOException;
146
147    protected abstract void checkTrusted(Object trustManager,
148                                         X509Certificate[] chain,
149                                         String authType)
150        throws CertificateException;
151
152    public static Object init(SSL ssl, TrustChain trustChain, KeyMaterial keyMaterial)
153        throws NoSuchAlgorithmException, KeyStoreException,
154        CertificateException, KeyManagementException, IOException {
155        return HANDLER.initSSL(ssl, trustChain, keyMaterial);
156    }
157
158    public static RuntimeException newRuntimeException(Exception cause) {
159        return HANDLER.buildRuntimeException(cause);
160    }
161
162    public static SSLSocketFactory getSSLSocketFactory(Object sslContext) {
163        return HANDLER.buildSSLSocketFactory(sslContext);
164    }
165
166    public static SSLServerSocketFactory getSSLServerSocketFactory(Object sslContext) {
167        return HANDLER.buildSSLServerSocketFactory(sslContext);
168    }
169
170    public static String getSubjectX500(X509Certificate cert) {
171        return HANDLER.retrieveSubjectX500(cert);
172    }
173
174    public static String getIssuerX500(X509Certificate cert) {
175        return HANDLER.retrieveIssuerX500(cert);
176    }
177
178    public static Object newKeyManagerFactory(KeyStore ks, char[] password)
179        throws NoSuchAlgorithmException, KeyStoreException,
180        UnrecoverableKeyException {
181        return HANDLER.buildKeyManagerFactory(ks, password);
182    }
183
184    public static Object[] getKeyManagers(Object keyManagerFactory) {
185        return HANDLER.retrieveKeyManagers(keyManagerFactory);
186    }
187
188    public static Object newTrustManagerFactory(KeyStore ks)
189        throws NoSuchAlgorithmException, KeyStoreException {
190        return HANDLER.buildTrustManagerFactory(ks);
191    }
192
193    public static Object[] getTrustManagers(Object trustManagerFactory) {
194        return HANDLER.retrieveTrustManagers(trustManagerFactory);
195    }
196
197    public static SSLSocket createSocket(SSL ssl)
198        throws IOException {
199        return HANDLER.buildSocket(ssl);
200    }
201
202    public static SSLSocket createSocket(SSL ssl, String remoteHost,
203                                         int remotePort, InetAddress localHost,
204                                         int localPort, int connectTimeout)
205        throws IOException {
206        return HANDLER.buildSocket(ssl, remoteHost, remotePort, localHost,
207            localPort, connectTimeout);
208    }
209
210    public static Socket createPlainSocket(
211            SSL ssl, String remoteHost, int remotePort, InetAddress localHost, int localPort,
212            int connectTimeout
213    ) throws IOException {
214        return HANDLER.buildPlainSocket(
215                ssl, remoteHost, remotePort, localHost, localPort, connectTimeout
216        );
217    }    
218
219    protected static Socket connect(Socket s, SocketFactory sf,
220                                    String remoteHost, int remotePort,
221                                    InetAddress localHost, int localPort,
222                                    int timeout, SSL ssl)
223        throws IOException {
224        return HANDLER.connectSocket(s, sf, remoteHost, remotePort, localHost,
225            localPort, timeout, ssl);
226    }
227
228    public static SSLServerSocket createServerSocket(SSL ssl)
229        throws IOException {
230        return HANDLER.buildServerSocket(ssl);
231    }
232
233    public static void setWantClientAuth(Object o, boolean wantClientAuth) {
234        HANDLER.wantClientAuth(o, wantClientAuth);
235    }
236
237    public static void setEnabledProtocols(Object o, String[] enabledProtocols) {
238        HANDLER.enabledProtocols(o, enabledProtocols);
239    }
240
241    public static Certificate[] getPeerCertificates(SSLSession session)
242        throws SSLPeerUnverifiedException {
243        return HANDLER.retrievePeerCerts(session);
244    }
245
246    public static void testTrust(Object trustManager, X509Certificate[] chain,
247                                 String authType)
248        throws CertificateException {
249        HANDLER.checkTrusted(trustManager, chain, authType);
250    }
251
252    public static void load() {
253        HANDLER.hashCode();
254    }
255
256}