@@ -47,14 +47,15 @@ public class SSLCertDownloader {
47
47
48
48
// public static void main(String[] args) throws Exception {
49
49
// 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];
53
54
// WiFi101Certificate wiFi101Certificate = new WiFi101Certificate(x509);
54
55
// }
55
56
56
57
public static Certificate [] retrieveFromURL (URL url ) throws NoSuchAlgorithmException , KeyManagementException ,
57
- SSLPeerUnverifiedException , CertificateEncodingException , FileNotFoundException , IOException {
58
+ SSLPeerUnverifiedException , CertificateEncodingException , FileNotFoundException , IOException {
58
59
59
60
SSLContext ssl = SSLContext .getInstance ("TLS" );
60
61
ssl .init (null , new TrustManager [] { new X509TrustManager () {
@@ -75,15 +76,32 @@ public X509Certificate[] getAcceptedIssuers() {
75
76
}
76
77
} }, null );
77
78
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 {
78
90
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
+ }
79
97
98
+ public static Certificate [] retireveWithoutVerification (URL url , SSLContext ssl )
99
+ throws IOException , SSLPeerUnverifiedException {
100
+ HttpsURLConnection connection = (HttpsURLConnection ) url .openConnection ();
80
101
connection .setHostnameVerifier ((str , sess ) -> true );
81
-
82
102
connection .setSSLSocketFactory (ssl .getSocketFactory ());
83
103
connection .getResponseCode ();
84
-
85
104
Certificate [] certificates = connection .getServerCertificates ();
86
-
87
105
connection .disconnect ();
88
106
return certificates ;
89
107
}
0 commit comments