001    /*
002     * $HeadURL: http://juliusdavies.ca/svn/not-yet-commons-ssl/tags/commons-ssl-0.3.11/src/java/org/apache/commons/ssl/SSLClient.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    
032    package org.apache.commons.ssl;
033    
034    import javax.net.ssl.SSLContext;
035    import javax.net.ssl.SSLSocketFactory;
036    import java.io.IOException;
037    import java.net.InetAddress;
038    import java.net.Socket;
039    import java.net.UnknownHostException;
040    import java.security.GeneralSecurityException;
041    import java.security.KeyManagementException;
042    import java.security.KeyStoreException;
043    import java.security.NoSuchAlgorithmException;
044    import java.security.cert.CertificateException;
045    import java.security.cert.X509Certificate;
046    import java.util.Map;
047    
048    /**
049     * @author Credit Union Central of British Columbia
050     * @author <a href="http://www.cucbc.com/">www.cucbc.com</a>
051     * @author <a href="mailto:juliusdavies@cucbc.com">juliusdavies@cucbc.com</a>
052     * @since 27-Feb-2006
053     */
054    public class SSLClient extends SSLSocketFactory {
055        private final SSL ssl;
056    
057        public SSLClient()
058            throws GeneralSecurityException, IOException {
059            this.ssl = new SSL();
060        }
061    
062        public void addTrustMaterial(TrustChain trustChain)
063            throws NoSuchAlgorithmException, KeyStoreException,
064            KeyManagementException, IOException, CertificateException {
065            ssl.addTrustMaterial(trustChain);
066        }
067    
068        public void setTrustMaterial(TrustChain trustChain)
069            throws NoSuchAlgorithmException, KeyStoreException,
070            KeyManagementException, IOException, CertificateException {
071            ssl.setTrustMaterial(trustChain);
072        }
073    
074        public void setKeyMaterial(KeyMaterial keyMaterial)
075            throws NoSuchAlgorithmException, KeyStoreException,
076            KeyManagementException, IOException, CertificateException {
077            ssl.setKeyMaterial(keyMaterial);
078        }
079    
080        public void setIsSecure(boolean b) { ssl.setIsSecure(b); }
081    
082        public void setDnsOverride(Map m) { ssl.setDnsOverride(m); }    
083    
084        public void setCheckCRL(boolean b) { ssl.setCheckCRL(b); }
085    
086        public void setCheckExpiry(boolean b) { ssl.setCheckExpiry(b); }
087    
088        public void setCheckHostname(boolean b) { ssl.setCheckHostname(b); }
089    
090        public void setConnectTimeout(int i) { ssl.setConnectTimeout(i); }
091    
092        public void setDefaultProtocol(String s) { ssl.setDefaultProtocol(s); }
093    
094        public void useDefaultJavaCiphers() { ssl.useDefaultJavaCiphers(); }
095    
096        public void useStrongCiphers() { ssl.useStrongCiphers(); }
097    
098        public void setEnabledCiphers(String[] ciphers) {
099            ssl.setEnabledCiphers(ciphers);
100        }
101    
102        public void setEnabledProtocols(String[] protocols) {
103            ssl.setEnabledProtocols(protocols);
104        }
105    
106        public void setHostnameVerifier(HostnameVerifier verifier) {
107            ssl.setHostnameVerifier(verifier);
108        }
109    
110        public void setSoTimeout(int soTimeout) { ssl.setSoTimeout(soTimeout); }
111    
112        public void setSSLWrapperFactory(SSLWrapperFactory wf) {
113            ssl.setSSLWrapperFactory(wf);
114        }
115    
116        public void setNeedClientAuth(boolean b) { ssl.setNeedClientAuth(b); }
117    
118        public void setWantClientAuth(boolean b) { ssl.setWantClientAuth(b); }
119    
120        public void setUseClientMode(boolean b) { ssl.setUseClientMode(b); }
121    
122        public boolean isSecure() { return ssl.isSecure(); }
123    
124        public X509Certificate[] getAssociatedCertificateChain() {
125            return ssl.getAssociatedCertificateChain();
126        }
127    
128        public boolean getCheckCRL() { return ssl.getCheckCRL(); }
129    
130        public boolean getCheckExpiry() { return ssl.getCheckExpiry(); }
131    
132        public boolean getCheckHostname() { return ssl.getCheckHostname(); }
133    
134        public int getConnectTimeout() { return ssl.getConnectTimeout(); }
135    
136        public String getDefaultProtocol() { return ssl.getDefaultProtocol(); }
137    
138        public String[] getEnabledCiphers() { return ssl.getEnabledCiphers(); }
139    
140        public String[] getEnabledProtocols() { return ssl.getEnabledProtocols(); }
141    
142        public HostnameVerifier getHostnameVerifier() {
143            return ssl.getHostnameVerifier();
144        }
145    
146        public int getSoTimeout() { return ssl.getSoTimeout(); }
147    
148        public SSLWrapperFactory getSSLWrapperFactory() {
149            return ssl.getSSLWrapperFactory();
150        }
151    
152        public boolean getNeedClientAuth() { return ssl.getNeedClientAuth(); }
153    
154        public boolean getWantClientAuth() { return ssl.getWantClientAuth(); }
155    
156        public boolean getUseClientMode() { /* SSLClient's default is true. */
157            return ssl.getUseClientModeDefault() || ssl.getUseClientMode();
158        }
159    
160        public SSLContext getSSLContext() throws GeneralSecurityException, IOException {
161            return ssl.getSSLContext();
162        }
163    
164        public TrustChain getTrustChain() { return ssl.getTrustChain(); }
165    
166        public X509Certificate[] getCurrentServerChain() {
167            return ssl.getCurrentServerChain();
168        }
169    
170        public String[] getDefaultCipherSuites() {
171            return ssl.getDefaultCipherSuites();
172        }
173    
174        public String[] getSupportedCipherSuites() {
175            return ssl.getSupportedCipherSuites();
176        }
177    
178        public Socket createSocket() throws IOException {
179            return ssl.createSocket();
180        }
181    
182        public Socket createSocket(String host, int port)
183            throws IOException {
184            return createSocket(host, port, null, 0);
185        }
186    
187        public Socket createSocket(InetAddress host, int port)
188            throws IOException {
189            return createSocket(host.getHostName(), port);
190        }
191    
192        public Socket createSocket(InetAddress host, int port,
193                                   InetAddress localHost, int localPort)
194            throws IOException {
195            return createSocket(host.getHostName(), port, localHost, localPort);
196        }
197    
198        public Socket createSocket(String host, int port,
199                                   InetAddress localHost, int localPort)
200            throws IOException {
201            return createSocket(host, port, localHost, localPort, 0);
202        }
203    
204        /**
205         * Attempts to get a new socket connection to the given host within the
206         * given time limit.
207         *
208         * @param host      the host name/IP
209         * @param port      the port on the host
210         * @param localHost the local host name/IP to bind the socket to
211         * @param localPort the port on the local machine
212         * @param timeout   the connection timeout (0==infinite)
213         * @return Socket a new socket
214         * @throws IOException          if an I/O error occurs while creating thesocket
215         * @throws UnknownHostException if the IP address of the host cannot be
216         *                              determined
217         */
218        public Socket createSocket(String host, int port, InetAddress localHost,
219                                   int localPort, int timeout)
220            throws IOException {
221            return ssl.createSocket(host, port, localHost, localPort, timeout);
222        }
223    
224        public Socket createSocket(Socket s, String remoteHost, int remotePort,
225                                   boolean autoClose)
226            throws IOException {
227            return ssl.createSocket(s, remoteHost, remotePort, autoClose);
228        }
229    
230    }