Skip to content

Commit 320a3f2

Browse files
committed
Workaround for JDK bug JDK-8159569
1 parent 7303630 commit 320a3f2

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

src/cc/arduino/plugins/wifi101/flashers/java/SSLCertDownloader.java

+25-7
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,15 @@ public class SSLCertDownloader {
4747

4848
// public static void main(String[] args) throws Exception {
4949
// Certificate[] certificates = retrieveFromURL(new
50-
// URL("https://www.bug.st/"));
51-
// X509Certificate x509 = (X509Certificate) certificates[certificates.length -
52-
// 1];
50+
// URL("https://cloud.arduino.cc/"));
51+
// System.out.println("Fetched " + certificates.length + " certificates. ");
52+
// X509Certificate x509 = (X509Certificate) certificates[certificates.length
53+
// - 1];
5354
// WiFi101Certificate wiFi101Certificate = new WiFi101Certificate(x509);
5455
// }
5556

5657
public static Certificate[] retrieveFromURL(URL url) throws NoSuchAlgorithmException, KeyManagementException,
57-
SSLPeerUnverifiedException, CertificateEncodingException, FileNotFoundException, IOException {
58+
SSLPeerUnverifiedException, CertificateEncodingException, FileNotFoundException, IOException {
5859

5960
SSLContext ssl = SSLContext.getInstance("TLS");
6061
ssl.init(null, new TrustManager[] { new X509TrustManager() {
@@ -75,15 +76,32 @@ public X509Certificate[] getAcceptedIssuers() {
7576
}
7677
} }, null);
7778

79+
// This is a workaround to reduce the impact of this bug:
80+
// http://bugs.java.com/bugdatabase/view_bug.do?bug_id=8159569
81+
try {
82+
return retireveWithVerification(url, ssl);
83+
} catch (Exception e) {
84+
return retireveWithoutVerification(url, ssl);
85+
}
86+
}
87+
88+
public static Certificate[] retireveWithVerification(URL url, SSLContext ssl)
89+
throws IOException, SSLPeerUnverifiedException {
7890
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
91+
connection.setSSLSocketFactory(ssl.getSocketFactory());
92+
connection.getResponseCode();
93+
Certificate[] certificates = connection.getServerCertificates();
94+
connection.disconnect();
95+
return certificates;
96+
}
7997

98+
public static Certificate[] retireveWithoutVerification(URL url, SSLContext ssl)
99+
throws IOException, SSLPeerUnverifiedException {
100+
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
80101
connection.setHostnameVerifier((str, sess) -> true);
81-
82102
connection.setSSLSocketFactory(ssl.getSocketFactory());
83103
connection.getResponseCode();
84-
85104
Certificate[] certificates = connection.getServerCertificates();
86-
87105
connection.disconnect();
88106
return certificates;
89107
}

0 commit comments

Comments
 (0)