001/*
002 * $HeadURL: file:///opt/dev/not-yet-commons-ssl-SVN-repo/tags/commons-ssl-0.3.17/src/java/org/apache/commons/ssl/SSLClient.java $
003 * $Revision: 180 $
004 * $Date: 2014-09-23 11:33:47 -0700 (Tue, 23 Sep 2014) $
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.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 */
054public 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 setEnabledCiphers(String[] ciphers) {
095        ssl.setEnabledCiphers(ciphers);
096    }
097
098    public void setEnabledProtocols(String[] protocols) {
099        ssl.setEnabledProtocols(protocols);
100    }
101
102    public void setHostnameVerifier(HostnameVerifier verifier) {
103        ssl.setHostnameVerifier(verifier);
104    }
105
106    public void setSoTimeout(int soTimeout) { ssl.setSoTimeout(soTimeout); }
107
108    public void setSSLWrapperFactory(SSLWrapperFactory wf) {
109        ssl.setSSLWrapperFactory(wf);
110    }
111
112    public void setNeedClientAuth(boolean b) { ssl.setNeedClientAuth(b); }
113
114    public void setWantClientAuth(boolean b) { ssl.setWantClientAuth(b); }
115
116    public void setUseClientMode(boolean b) { ssl.setUseClientMode(b); }
117
118    public boolean isSecure() { return ssl.isSecure(); }
119
120    public X509Certificate[] getAssociatedCertificateChain() {
121        return ssl.getAssociatedCertificateChain();
122    }
123
124    public boolean getCheckCRL() { return ssl.getCheckCRL(); }
125
126    public boolean getCheckExpiry() { return ssl.getCheckExpiry(); }
127
128    public boolean getCheckHostname() { return ssl.getCheckHostname(); }
129
130    public int getConnectTimeout() { return ssl.getConnectTimeout(); }
131
132    public String getDefaultProtocol() { return ssl.getDefaultProtocol(); }
133
134    public String[] getEnabledCiphers() { return ssl.getEnabledCiphers(); }
135
136    public String[] getEnabledProtocols() { return ssl.getEnabledProtocols(); }
137
138    public HostnameVerifier getHostnameVerifier() {
139        return ssl.getHostnameVerifier();
140    }
141
142    public int getSoTimeout() { return ssl.getSoTimeout(); }
143
144    public SSLWrapperFactory getSSLWrapperFactory() {
145        return ssl.getSSLWrapperFactory();
146    }
147
148    public boolean getNeedClientAuth() { return ssl.getNeedClientAuth(); }
149
150    public boolean getWantClientAuth() { return ssl.getWantClientAuth(); }
151
152    public boolean getUseClientMode() { /* SSLClient's default is true. */
153        return ssl.getUseClientModeDefault() || ssl.getUseClientMode();
154    }
155
156    public SSLContext getSSLContext() throws GeneralSecurityException, IOException {
157        return ssl.getSSLContext();
158    }
159
160    public TrustChain getTrustChain() { return ssl.getTrustChain(); }
161
162    public X509Certificate[] getCurrentServerChain() {
163        return ssl.getCurrentServerChain();
164    }
165
166    public String[] getDefaultCipherSuites() {
167        return ssl.getDefaultCipherSuites();
168    }
169
170    public String[] getSupportedCipherSuites() {
171        return ssl.getSupportedCipherSuites();
172    }
173
174    public Socket createSocket() throws IOException {
175        return ssl.createSocket();
176    }
177
178    public Socket createSocket(String host, int port)
179        throws IOException {
180        return createSocket(host, port, null, 0);
181    }
182
183    public Socket createSocket(InetAddress host, int port)
184        throws IOException {
185        return createSocket(host.getHostName(), port);
186    }
187
188    public Socket createSocket(InetAddress host, int port,
189                               InetAddress localHost, int localPort)
190        throws IOException {
191        return createSocket(host.getHostName(), port, localHost, localPort);
192    }
193
194    public Socket createSocket(String host, int port,
195                               InetAddress localHost, int localPort)
196        throws IOException {
197        return createSocket(host, port, localHost, localPort, 0);
198    }
199
200    /**
201     * Attempts to get a new socket connection to the given host within the
202     * given time limit.
203     *
204     * @param host      the host name/IP
205     * @param port      the port on the host
206     * @param localHost the local host name/IP to bind the socket to
207     * @param localPort the port on the local machine
208     * @param timeout   the connection timeout (0==infinite)
209     * @return Socket a new socket
210     * @throws IOException          if an I/O error occurs while creating thesocket
211     * @throws UnknownHostException if the IP address of the host cannot be
212     *                              determined
213     */
214    public Socket createSocket(String host, int port, InetAddress localHost,
215                               int localPort, int timeout)
216        throws IOException {
217        return ssl.createSocket(host, port, localHost, localPort, timeout);
218    }
219
220    public Socket createSocket(Socket s, String remoteHost, int remotePort,
221                               boolean autoClose)
222        throws IOException {
223        return ssl.createSocket(s, remoteHost, remotePort, autoClose);
224    }
225
226}