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