okhttp-gson: fix SSL settings with okhttp3 (#4226)

The old code used to work with older okhttp (< 3), but will throw
NullPointerExceptions with okhttp3.
This commit is contained in:
Fabio Kung
2019-10-24 03:15:10 -07:00
committed by William Cheng
parent 652b0f5bf6
commit c3666e9350
3 changed files with 90 additions and 81 deletions

View File

@@ -4,6 +4,7 @@ package {{invokerPackage}};
import okhttp3.*; import okhttp3.*;
import okhttp3.internal.http.HttpMethod; import okhttp3.internal.http.HttpMethod;
import okhttp3.internal.tls.OkHostnameVerifier;
import okhttp3.logging.HttpLoggingInterceptor; import okhttp3.logging.HttpLoggingInterceptor;
import okhttp3.logging.HttpLoggingInterceptor.Level; import okhttp3.logging.HttpLoggingInterceptor.Level;
import okio.BufferedSink; import okio.BufferedSink;
@@ -1340,8 +1341,8 @@ public class ApiClient {
*/ */
private void applySslSettings() { private void applySslSettings() {
try { try {
TrustManager[] trustManagers = null; TrustManager[] trustManagers;
HostnameVerifier hostnameVerifier = null; HostnameVerifier hostnameVerifier;
if (!verifyingSsl) { if (!verifyingSsl) {
trustManagers = new TrustManager[]{ trustManagers = new TrustManager[]{
new X509TrustManager() { new X509TrustManager() {
@@ -1359,14 +1360,18 @@ public class ApiClient {
} }
} }
}; };
SSLContext sslContext = SSLContext.getInstance("TLS");
hostnameVerifier = new HostnameVerifier() { hostnameVerifier = new HostnameVerifier() {
@Override @Override
public boolean verify(String hostname, SSLSession session) { public boolean verify(String hostname, SSLSession session) {
return true; return true;
} }
}; };
} else if (sslCaCert != null) { } else {
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
if (sslCaCert == null) {
trustManagerFactory.init((KeyStore) null);
} else {
char[] password = null; // Any password will work. char[] password = null; // Any password will work.
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509"); CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
Collection<? extends Certificate> certificates = certificateFactory.generateCertificates(sslCaCert); Collection<? extends Certificate> certificates = certificateFactory.generateCertificates(sslCaCert);
@@ -1379,20 +1384,18 @@ public class ApiClient {
String certificateAlias = "ca" + Integer.toString(index++); String certificateAlias = "ca" + Integer.toString(index++);
caKeyStore.setCertificateEntry(certificateAlias, certificate); caKeyStore.setCertificateEntry(certificateAlias, certificate);
} }
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(caKeyStore); trustManagerFactory.init(caKeyStore);
}
trustManagers = trustManagerFactory.getTrustManagers(); trustManagers = trustManagerFactory.getTrustManagers();
hostnameVerifier = OkHostnameVerifier.INSTANCE;
} }
if (keyManagers != null || trustManagers != null) {
SSLContext sslContext = SSLContext.getInstance("TLS"); SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(keyManagers, trustManagers, new SecureRandom()); sslContext.init(keyManagers, trustManagers, new SecureRandom());
httpClient = httpClient.newBuilder().sslSocketFactory(sslContext.getSocketFactory(), (X509TrustManager) trustManagers[0]).build(); httpClient = httpClient.newBuilder()
} else { .sslSocketFactory(sslContext.getSocketFactory(), (X509TrustManager) trustManagers[0])
httpClient = httpClient.newBuilder().sslSocketFactory(null, (X509TrustManager) trustManagers[0]).build(); .hostnameVerifier(hostnameVerifier)
} .build();
httpClient = httpClient.newBuilder().hostnameVerifier(hostnameVerifier).build();
} catch (GeneralSecurityException e) { } catch (GeneralSecurityException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }

View File

@@ -15,6 +15,7 @@ package org.openapitools.client;
import okhttp3.*; import okhttp3.*;
import okhttp3.internal.http.HttpMethod; import okhttp3.internal.http.HttpMethod;
import okhttp3.internal.tls.OkHostnameVerifier;
import okhttp3.logging.HttpLoggingInterceptor; import okhttp3.logging.HttpLoggingInterceptor;
import okhttp3.logging.HttpLoggingInterceptor.Level; import okhttp3.logging.HttpLoggingInterceptor.Level;
import okio.BufferedSink; import okio.BufferedSink;
@@ -1305,8 +1306,8 @@ public class ApiClient {
*/ */
private void applySslSettings() { private void applySslSettings() {
try { try {
TrustManager[] trustManagers = null; TrustManager[] trustManagers;
HostnameVerifier hostnameVerifier = null; HostnameVerifier hostnameVerifier;
if (!verifyingSsl) { if (!verifyingSsl) {
trustManagers = new TrustManager[]{ trustManagers = new TrustManager[]{
new X509TrustManager() { new X509TrustManager() {
@@ -1324,14 +1325,18 @@ public class ApiClient {
} }
} }
}; };
SSLContext sslContext = SSLContext.getInstance("TLS");
hostnameVerifier = new HostnameVerifier() { hostnameVerifier = new HostnameVerifier() {
@Override @Override
public boolean verify(String hostname, SSLSession session) { public boolean verify(String hostname, SSLSession session) {
return true; return true;
} }
}; };
} else if (sslCaCert != null) { } else {
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
if (sslCaCert == null) {
trustManagerFactory.init((KeyStore) null);
} else {
char[] password = null; // Any password will work. char[] password = null; // Any password will work.
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509"); CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
Collection<? extends Certificate> certificates = certificateFactory.generateCertificates(sslCaCert); Collection<? extends Certificate> certificates = certificateFactory.generateCertificates(sslCaCert);
@@ -1344,20 +1349,18 @@ public class ApiClient {
String certificateAlias = "ca" + Integer.toString(index++); String certificateAlias = "ca" + Integer.toString(index++);
caKeyStore.setCertificateEntry(certificateAlias, certificate); caKeyStore.setCertificateEntry(certificateAlias, certificate);
} }
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(caKeyStore); trustManagerFactory.init(caKeyStore);
}
trustManagers = trustManagerFactory.getTrustManagers(); trustManagers = trustManagerFactory.getTrustManagers();
hostnameVerifier = OkHostnameVerifier.INSTANCE;
} }
if (keyManagers != null || trustManagers != null) {
SSLContext sslContext = SSLContext.getInstance("TLS"); SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(keyManagers, trustManagers, new SecureRandom()); sslContext.init(keyManagers, trustManagers, new SecureRandom());
httpClient = httpClient.newBuilder().sslSocketFactory(sslContext.getSocketFactory(), (X509TrustManager) trustManagers[0]).build(); httpClient = httpClient.newBuilder()
} else { .sslSocketFactory(sslContext.getSocketFactory(), (X509TrustManager) trustManagers[0])
httpClient = httpClient.newBuilder().sslSocketFactory(null, (X509TrustManager) trustManagers[0]).build(); .hostnameVerifier(hostnameVerifier)
} .build();
httpClient = httpClient.newBuilder().hostnameVerifier(hostnameVerifier).build();
} catch (GeneralSecurityException e) { } catch (GeneralSecurityException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }

View File

@@ -15,6 +15,7 @@ package org.openapitools.client;
import okhttp3.*; import okhttp3.*;
import okhttp3.internal.http.HttpMethod; import okhttp3.internal.http.HttpMethod;
import okhttp3.internal.tls.OkHostnameVerifier;
import okhttp3.logging.HttpLoggingInterceptor; import okhttp3.logging.HttpLoggingInterceptor;
import okhttp3.logging.HttpLoggingInterceptor.Level; import okhttp3.logging.HttpLoggingInterceptor.Level;
import okio.BufferedSink; import okio.BufferedSink;
@@ -1305,8 +1306,8 @@ public class ApiClient {
*/ */
private void applySslSettings() { private void applySslSettings() {
try { try {
TrustManager[] trustManagers = null; TrustManager[] trustManagers;
HostnameVerifier hostnameVerifier = null; HostnameVerifier hostnameVerifier;
if (!verifyingSsl) { if (!verifyingSsl) {
trustManagers = new TrustManager[]{ trustManagers = new TrustManager[]{
new X509TrustManager() { new X509TrustManager() {
@@ -1324,14 +1325,18 @@ public class ApiClient {
} }
} }
}; };
SSLContext sslContext = SSLContext.getInstance("TLS");
hostnameVerifier = new HostnameVerifier() { hostnameVerifier = new HostnameVerifier() {
@Override @Override
public boolean verify(String hostname, SSLSession session) { public boolean verify(String hostname, SSLSession session) {
return true; return true;
} }
}; };
} else if (sslCaCert != null) { } else {
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
if (sslCaCert == null) {
trustManagerFactory.init((KeyStore) null);
} else {
char[] password = null; // Any password will work. char[] password = null; // Any password will work.
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509"); CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
Collection<? extends Certificate> certificates = certificateFactory.generateCertificates(sslCaCert); Collection<? extends Certificate> certificates = certificateFactory.generateCertificates(sslCaCert);
@@ -1344,20 +1349,18 @@ public class ApiClient {
String certificateAlias = "ca" + Integer.toString(index++); String certificateAlias = "ca" + Integer.toString(index++);
caKeyStore.setCertificateEntry(certificateAlias, certificate); caKeyStore.setCertificateEntry(certificateAlias, certificate);
} }
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(caKeyStore); trustManagerFactory.init(caKeyStore);
}
trustManagers = trustManagerFactory.getTrustManagers(); trustManagers = trustManagerFactory.getTrustManagers();
hostnameVerifier = OkHostnameVerifier.INSTANCE;
} }
if (keyManagers != null || trustManagers != null) {
SSLContext sslContext = SSLContext.getInstance("TLS"); SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(keyManagers, trustManagers, new SecureRandom()); sslContext.init(keyManagers, trustManagers, new SecureRandom());
httpClient = httpClient.newBuilder().sslSocketFactory(sslContext.getSocketFactory(), (X509TrustManager) trustManagers[0]).build(); httpClient = httpClient.newBuilder()
} else { .sslSocketFactory(sslContext.getSocketFactory(), (X509TrustManager) trustManagers[0])
httpClient = httpClient.newBuilder().sslSocketFactory(null, (X509TrustManager) trustManagers[0]).build(); .hostnameVerifier(hostnameVerifier)
} .build();
httpClient = httpClient.newBuilder().hostnameVerifier(hostnameVerifier).build();
} catch (GeneralSecurityException e) { } catch (GeneralSecurityException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }