1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 package com
.owncloud
.android
.network
;
21 import java
.io
.IOException
;
22 import java
.net
.InetAddress
;
23 import java
.net
.InetSocketAddress
;
24 import java
.net
.Socket
;
25 import java
.net
.SocketAddress
;
26 import java
.net
.UnknownHostException
;
27 import java
.security
.cert
.X509Certificate
;
29 import javax
.net
.SocketFactory
;
30 import javax
.net
.ssl
.SSLContext
;
31 import javax
.net
.ssl
.SSLException
;
32 import javax
.net
.ssl
.SSLHandshakeException
;
33 import javax
.net
.ssl
.SSLPeerUnverifiedException
;
34 import javax
.net
.ssl
.SSLSession
;
35 import javax
.net
.ssl
.SSLSocket
;
37 import org
.apache
.commons
.httpclient
.ConnectTimeoutException
;
38 import org
.apache
.commons
.httpclient
.params
.HttpConnectionParams
;
39 import org
.apache
.commons
.httpclient
.protocol
.ProtocolSocketFactory
;
40 import org
.apache
.http
.conn
.ssl
.X509HostnameVerifier
;
42 import com
.owncloud
.android
.Log_OC
;
45 * AdvancedSSLProtocolSocketFactory allows to create SSL {@link Socket}s with
46 * a custom SSLContext and an optional Hostname Verifier.
48 * @author David A. Velasco
51 public class AdvancedSslSocketFactory
implements ProtocolSocketFactory
{
53 private static final String TAG
= AdvancedSslSocketFactory
.class.getSimpleName();
55 private SSLContext mSslContext
= null
;
56 private AdvancedX509TrustManager mTrustManager
= null
;
57 private X509HostnameVerifier mHostnameVerifier
= null
;
59 public SSLContext
getSslContext() {
64 * Constructor for AdvancedSSLProtocolSocketFactory.
66 public AdvancedSslSocketFactory(SSLContext sslContext
, AdvancedX509TrustManager trustManager
, X509HostnameVerifier hostnameVerifier
) {
67 if (sslContext
== null
)
68 throw new IllegalArgumentException("AdvancedSslSocketFactory can not be created with a null SSLContext");
69 if (trustManager
== null
)
70 throw new IllegalArgumentException("AdvancedSslSocketFactory can not be created with a null Trust Manager");
71 mSslContext
= sslContext
;
72 mTrustManager
= trustManager
;
73 mHostnameVerifier
= hostnameVerifier
;
77 * @see ProtocolSocketFactory#createSocket(java.lang.String,int,java.net.InetAddress,int)
79 public Socket
createSocket(String host
, int port
, InetAddress clientHost
, int clientPort
) throws IOException
, UnknownHostException
{
80 Socket socket
= mSslContext
.getSocketFactory().createSocket(host
, port
, clientHost
, clientPort
);
81 verifyPeerIdentity(host
, port
, socket
);
87 * Attempts to get a new socket connection to the given host within the
90 * @param host the host name/IP
91 * @param port the port on the host
92 * @param clientHost the local host name/IP to bind the socket to
93 * @param clientPort the port on the local machine
94 * @param params {@link HttpConnectionParams Http connection parameters}
96 * @return Socket a new socket
98 * @throws IOException if an I/O error occurs while creating the socket
99 * @throws UnknownHostException if the IP address of the host cannot be
102 public Socket
createSocket(final String host
, final int port
,
103 final InetAddress localAddress
, final int localPort
,
104 final HttpConnectionParams params
) throws IOException
,
105 UnknownHostException
, ConnectTimeoutException
{
106 Log_OC
.d(TAG
, "Creating SSL Socket with remote " + host
+ ":" + port
+ ", local " + localAddress
+ ":" + localPort
+ ", params: " + params
);
107 if (params
== null
) {
108 throw new IllegalArgumentException("Parameters may not be null");
110 int timeout
= params
.getConnectionTimeout();
111 SocketFactory socketfactory
= mSslContext
.getSocketFactory();
112 Log_OC
.d(TAG
, " ... with connection timeout " + timeout
+ " and socket timeout " + params
.getSoTimeout());
113 Socket socket
= socketfactory
.createSocket();
114 SocketAddress localaddr
= new InetSocketAddress(localAddress
, localPort
);
115 SocketAddress remoteaddr
= new InetSocketAddress(host
, port
);
116 socket
.setSoTimeout(params
.getSoTimeout());
117 socket
.bind(localaddr
);
118 socket
.connect(remoteaddr
, timeout
);
119 verifyPeerIdentity(host
, port
, socket
);
124 * @see ProtocolSocketFactory#createSocket(java.lang.String,int)
126 public Socket
createSocket(String host
, int port
) throws IOException
,
127 UnknownHostException
{
128 Log_OC
.d(TAG
, "Creating SSL Socket with remote " + host
+ ":" + port
);
129 Socket socket
= mSslContext
.getSocketFactory().createSocket(host
, port
);
130 verifyPeerIdentity(host
, port
, socket
);
134 public boolean equals(Object obj
) {
135 return ((obj
!= null
) && obj
.getClass().equals(
136 AdvancedSslSocketFactory
.class));
139 public int hashCode() {
140 return AdvancedSslSocketFactory
.class.hashCode();
144 public X509HostnameVerifier
getHostNameVerifier() {
145 return mHostnameVerifier
;
149 public void setHostNameVerifier(X509HostnameVerifier hostnameVerifier
) {
150 mHostnameVerifier
= hostnameVerifier
;
154 * Verifies the identity of the server.
156 * The server certificate is verified first.
158 * Then, the host name is compared with the content of the server certificate using the current host name verifier, if any.
161 private void verifyPeerIdentity(String host
, int port
, Socket socket
) throws IOException
{
163 CertificateCombinedException failInHandshake
= null
;
164 /// 1. VERIFY THE SERVER CERTIFICATE through the registered TrustManager (that should be an instance of AdvancedX509TrustManager)
166 SSLSocket sock
= (SSLSocket
) socket
; // a new SSLSession instance is created as a "side effect"
167 sock
.startHandshake();
169 } catch (RuntimeException e
) {
171 if (e
instanceof CertificateCombinedException
) {
172 failInHandshake
= (CertificateCombinedException
) e
;
174 Throwable cause
= e
.getCause();
175 Throwable previousCause
= null
;
176 while (cause
!= null
&& cause
!= previousCause
&& !(cause
instanceof CertificateCombinedException
)) {
177 previousCause
= cause
;
178 cause
= cause
.getCause();
180 if (cause
!= null
&& cause
instanceof CertificateCombinedException
) {
181 failInHandshake
= (CertificateCombinedException
)cause
;
184 if (failInHandshake
== null
) {
187 failInHandshake
.setHostInUrl(host
);
191 /// 2. VERIFY HOSTNAME
192 SSLSession newSession
= null
;
193 boolean verifiedHostname
= true
;
194 if (mHostnameVerifier
!= null
) {
195 if (failInHandshake
!= null
) {
196 /// 2.1 : a new SSLSession instance was NOT created in the handshake
197 X509Certificate serverCert
= failInHandshake
.getServerCertificate();
199 mHostnameVerifier
.verify(host
, serverCert
);
200 } catch (SSLException e
) {
201 verifiedHostname
= false
;
205 /// 2.2 : a new SSLSession instance was created in the handshake
206 newSession
= ((SSLSocket
)socket
).getSession();
207 if (!mTrustManager
.isKnownServer((X509Certificate
)(newSession
.getPeerCertificates()[0]))) {
208 verifiedHostname
= mHostnameVerifier
.verify(host
, newSession
);
213 /// 3. Combine the exceptions to throw, if any
214 if (!verifiedHostname
) {
215 SSLPeerUnverifiedException pue
= new SSLPeerUnverifiedException("Names in the server certificate do not match to " + host
+ " in the URL");
216 if (failInHandshake
== null
) {
217 failInHandshake
= new CertificateCombinedException((X509Certificate
) newSession
.getPeerCertificates()[0]);
218 failInHandshake
.setHostInUrl(host
);
220 failInHandshake
.setSslPeerUnverifiedException(pue
);
221 pue
.initCause(failInHandshake
);
224 } else if (failInHandshake
!= null
) {
225 SSLHandshakeException hse
= new SSLHandshakeException("Server certificate could not be verified");
226 hse
.initCause(failInHandshake
);
230 } catch (IOException io
) {
233 } catch (Exception x
) {
234 // NOTHING - irrelevant exception for the caller