2323import java .util .List ;
2424import java .util .concurrent .TimeUnit ;
2525
26+ import javax .net .ssl .HostnameVerifier ;
2627import javax .net .ssl .SSLContext ;
28+ import javax .net .ssl .SSLSession ;
2729import javax .net .ssl .SSLSocketFactory ;
2830import javax .net .ssl .TrustManager ;
2931import javax .net .ssl .X509TrustManager ;
@@ -37,7 +39,7 @@ public class CouchbaseLiteHttpClientFactory implements HttpClientFactory {
3739 private OkHttpClient client ;
3840 private ClearableCookieJar cookieJar ;
3941 private SSLSocketFactory sslSocketFactory ;
40-
42+ private HostnameVerifier hostnameVerifier ;
4143 private boolean followRedirects = true ;
4244
4345 // deprecated
@@ -63,11 +65,19 @@ public CouchbaseLiteHttpClientFactory(ClearableCookieJar cookieJar) {
6365 @ InterfaceAudience .Private
6466 public void setSSLSocketFactory (SSLSocketFactory sslSocketFactory ) {
6567 if (this .sslSocketFactory != null ) {
66- throw new RuntimeException ("SSLSocketFactory already set" );
68+ throw new RuntimeException ("SSLSocketFactory is already set" );
6769 }
6870 this .sslSocketFactory = sslSocketFactory ;
6971 }
7072
73+ @ InterfaceAudience .Private
74+ public void setHostnameVerifier (HostnameVerifier hostnameVerifier ) {
75+ if (this .hostnameVerifier != null ) {
76+ throw new RuntimeException ("HostnameVerifier is already set" );
77+ }
78+ this .hostnameVerifier = hostnameVerifier ;
79+ }
80+
7181 ////////////////////////////////////////////////////////////
7282 // Implementations of HttpClientFactory
7383 ////////////////////////////////////////////////////////////
@@ -86,6 +96,9 @@ synchronized public OkHttpClient getOkHttpClient() {
8696 if (sslSocketFactory != null )
8797 builder .sslSocketFactory (sslSocketFactory );
8898
99+ if (hostnameVerifier != null )
100+ builder .hostnameVerifier (hostnameVerifier );
101+
89102 // synchronize access to the cookieStore in case there is another
90103 // thread in the middle of updating it. wait until they are done so we get their changes.
91104 builder .cookieJar (cookieJar );
@@ -193,6 +206,15 @@ public X509Certificate[] getAcceptedIssuers() {
193206 return sslContext .getSocketFactory ();
194207 }
195208
209+ private static HostnameVerifier ignoreHostnameVerifier () {
210+ return new HostnameVerifier () {
211+ @ Override
212+ public boolean verify (String s , SSLSession sslSession ) {
213+ return true ;
214+ }
215+ };
216+ }
217+
196218 /**
197219 * This is a convenience method to allow couchbase lite to connect to servers
198220 * that use self-signed SSL certs.
@@ -205,11 +227,15 @@ public X509Certificate[] getAcceptedIssuers() {
205227 */
206228 @ InterfaceAudience .Public
207229 public void allowSelfSignedSSLCertificates () {
230+ // SSLSocketFactory that bypasses certificate verification.
208231 try {
209232 setSSLSocketFactory (selfSignedSSLSocketFactory ());
210233 } catch (GeneralSecurityException e ) {
211234 throw new RuntimeException (e );
212235 }
236+
237+ // HostnameVerifier that bypasses hotname verification
238+ setHostnameVerifier (ignoreHostnameVerifier ());
213239 }
214240
215241 /**
0 commit comments