Small java question regarding the Java built in Http client please.
I have a small piece of code where I am trying the Java built in Http client.
final KeyManagerFactory keyManagerFactory = getAndInitKeyManagerFactory();
SSLContext sslContext = SSLContext.getInstance("TLSv1.3");
sslContext.init(keyManagerFactory.getKeyManagers(), null, new SecureRandom());
(Please note the null for TrustManager[] tm
)
I would like to avoid carrying an extra truststore, and since this is mainly for testing, I am more than ok trusting any server even knowing this might be insecure.
Unfortunately, even with this null for the trust, I am still getting :
Caused by: java.security.cert.CertificateException: No subject alternative DNS name matching abc.com
I even tried giving a
static class InsecureTrustManager implements X509TrustManager {
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
}
sslContext.init(keyManagerFactory.getKeyManagers(), new TrustManager[] { new InsecureTrustManager() }, new SecureRandom());
But no luck, still the same exception with stack trace:
Exception in thread "main" java.io.IOException: No subject alternative DNS name matching abc.com found.
at java.net.http/jdk.internal.net.http.HttpClientImpl.send(HttpClientImpl.java:565)
at java.net.http/jdk.internal.net.http.HttpClientFacade.send(HttpClientFacade.java:119)
at question.Question.main(Question.java:54)
Caused by: javax.net.ssl.SSLHandshakeException: No subject alternative DNS name matching abc.com found.
at java.base/sun.security.ssl.Alert.createSSLException(Alert.java:131)
at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:320)
[...]
Caused by: java.security.cert.CertificateException: abc.com found.
at java.base/sun.security.util.HostnameChecker.matchDNS(HostnameChecker.java:207)
at java.base/sun.security.util.HostnameChecker.match(HostnameChecker.java:98)
at java.base/sun.security.ssl.X509TrustManagerImpl.checkIdentity(X509TrustManagerImpl.java:455)
at java.base/sun.security.ssl.X509TrustManagerImpl.checkIdentity(X509TrustManagerImpl.java:415)
at java.base/sun.security.ssl.AbstractTrustManagerWrapper.checkAdditionalTrust(SSLContextImpl.java:1584)
at java.base/sun.security.ssl.AbstractTrustManagerWrapper.checkServerTrusted(SSLContextImpl.java:1525)
at java.base/sun.security.ssl.CertificateMessage$T13CertificateConsumer.checkServerCerts(CertificateMessage.java:1310)
I am not quite understanding the issue, and would like to ask what is the proper way to configure from the code, with Java built in, to trust everything.
Thank you