Skip to content

Commit

Permalink
Catch SSL Exceptions with OkHttp and log them
Browse files Browse the repository at this point in the history
  • Loading branch information
siddarthkay committed Jun 8, 2023
1 parent 980594e commit e54a8fc
Showing 1 changed file with 21 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ class StatusOkHttpClientFactory implements OkHttpClientFactory {
public OkHttpClient createNewNetworkModuleClient() {
String certPem = StatusPackage.getImageTLSCert();
X509Certificate cert;
HandshakeCertificates clientCertificates;

// Convert PEM certificate string to X509Certificate object
try {
CertificateFactory cf = CertificateFactory.getInstance("X.509");
cert = (X509Certificate) cf.generateCertificate(new ByteArrayInputStream(certPem.getBytes()));
Expand All @@ -32,13 +34,25 @@ public OkHttpClient createNewNetworkModuleClient() {
cert = null;
}

HandshakeCertificates clientCertificates = new HandshakeCertificates.Builder()
.addPlatformTrustedCertificates()
.addTrustedCertificate(cert)
.build();
// Create HandshakeCertificates object with our certificate
try {
clientCertificates = new HandshakeCertificates.Builder()
.addPlatformTrustedCertificates()
.addTrustedCertificate(cert)
.build();
} catch(Exception e) {
Log.e("StatusOkHttpClientFactory", "Could not build HandshakeCertificates", e);
return null;
}

return OkHttpClientProvider.createClientBuilder()
.sslSocketFactory(clientCertificates.sslSocketFactory(), clientCertificates.trustManager())
.build();
// Create OkHttpClient with custom SSL socket factory and trust manager
try {
return OkHttpClientProvider.createClientBuilder()
.sslSocketFactory(clientCertificates.sslSocketFactory(), clientCertificates.trustManager())
.build();
} catch(Exception e) {
Log.e("StatusOkHttpClientFactory", "Could not create OkHttpClient", e);
return null;
}
}
}

0 comments on commit e54a8fc

Please sign in to comment.