Skip to content

Commit ab5c0ab

Browse files
quintessemaxandersen
authored andcommitted
Added --naive option to trust all SSL certificates
1 parent 891e25a commit ab5c0ab

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

src/main/java/dk/xam/jbang/Main.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
import java.net.URL;
99
import java.nio.charset.StandardCharsets;
1010
import java.nio.file.*;
11+
import java.security.KeyManagementException;
1112
import java.security.MessageDigest;
1213
import java.security.NoSuchAlgorithmException;
14+
import java.security.cert.X509Certificate;
1315
import java.util.*;
1416
import java.util.concurrent.Callable;
1517
import java.util.jar.Attributes;
@@ -18,6 +20,13 @@
1820
import java.util.stream.Collectors;
1921
import java.util.stream.Stream;
2022

23+
import javax.net.ssl.HostnameVerifier;
24+
import javax.net.ssl.HttpsURLConnection;
25+
import javax.net.ssl.SSLContext;
26+
import javax.net.ssl.SSLSession;
27+
import javax.net.ssl.TrustManager;
28+
import javax.net.ssl.X509TrustManager;
29+
2130
import org.apache.commons.text.StringEscapeUtils;
2231

2332
import com.sun.nio.file.SensitivityWatchEventModifier;
@@ -83,6 +92,9 @@ boolean debug() {
8392
@Option(names = { "-D" }, description = "set a system property")
8493
Map<String, String> properties = new HashMap<>();
8594

95+
@Option(names = { "--naive" }, description = "Trust all SSL certificates.")
96+
boolean naive;
97+
8698
public int completion() throws IOException {
8799
String script = AutoComplete.bash(
88100
spec.name(),
@@ -156,6 +168,41 @@ private Integer doCall() throws IOException {
156168
return completion();
157169
}
158170

171+
if (naive) {
172+
try {
173+
// Create a trust manager that does not validate certificate chains
174+
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
175+
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
176+
return null;
177+
}
178+
179+
public void checkClientTrusted(X509Certificate[] certs, String authType) {
180+
}
181+
182+
public void checkServerTrusted(X509Certificate[] certs, String authType) {
183+
}
184+
}
185+
};
186+
187+
// Install the all-trusting trust manager
188+
SSLContext sc = SSLContext.getInstance("SSL");
189+
sc.init(null, trustAllCerts, new java.security.SecureRandom());
190+
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
191+
192+
// Create all-trusting host name verifier
193+
HostnameVerifier allHostsValid = new HostnameVerifier() {
194+
public boolean verify(String hostname, SSLSession session) {
195+
return true;
196+
}
197+
};
198+
199+
// Install the all-trusting host verifier
200+
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
201+
} catch (NoSuchAlgorithmException | KeyManagementException ex) {
202+
throw new RuntimeException(ex);
203+
}
204+
}
205+
159206
if (clearCache) {
160207
info("Clearing cache at " + Settings.getCacheDir().toPath());
161208
// noinspection resource

0 commit comments

Comments
 (0)