ip2location / ip2location-io-java

IP2Location.io Java SDK allows user to query for an enriched data set based on IP address and provides WHOIS lookup api that helps users to obtain domain information.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Fix javax.net.ssl.SSLHandshakeException inside com.ip2location.IPGeolocation#Lookup(java.lang.String, java.lang.String) method

nedelweiss opened this issue · comments

Please, fix the next problem inside com.ip2location.IPGeolocation#Lookup(java.lang.String, java.lang.String) method:

javax.net.ssl.SSLHandshakeException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

Request: https://api.ip2location.io/?format=json&source=sdk-java-iplio&source_version=1.0.0&key=${IP_2_LOCATION_API_KEY}&ip=134.201.250.155&lang=

Debug: the error occurs when int statusCode = response.thenApply(HttpResponse::statusCode).get(); line has been called
image

Thanks in advance

I tried to install CloudFlare certificate with your instruction above but that didn't solve the problem.

Therefore I tried to set CloudFlare certificate with the next command:
keytool -import -noprompt -trustcacerts -alias Cloudflare_CA -file Cloudflare_CA.crt -keystore "$JAVA_HOME/lib/security/cacerts" -storepass changeit.
Details: https://stackoverflow.com/questions/9619030/resolving-javax-net-ssl-sslhandshakeexception-sun-security-validator-validatore

but that didn't solve the problem also.

The final attempt was to download certificate from your server and import it to local storage. And it worked.

I followed the instructions below:

1
2
3

And then run the command:
keytool -import -noprompt -trustcacerts -alias ip2location_crt -file ip2location.io.crt -keystore "$JAVA_HOME/lib/security/cacerts" -storepass changeit.

This way isn't stable because your certificate is valid from 5/18/2023 to 8/16/2023. And after the certificate expires, our system will not receive data.

So, how we can fix this issue?

Thanks in advance.

Hello @ip2location, I tried to install certs with your latest instruction and it helped me.

I compiled InstallCert.java with javac InstallCert.java. But before compilation I replaced from "jssecacerts" to $JAVA_HOME/lib/security/cacerts path (local jdk's keystore) in InstallCert.java file.
And then ran compiled file with java InstallCert api.ip2location.io.

As a result I got the following log:

Loading KeyStore C:\java\openjdk\jdk-17\lib\security\cacerts...
Opening connection to api.ip2location.io:443...
Starting SSL handshake...

javax.net.ssl.SSLProtocolException: Unsupported handshake message: certificate
at java.base/sun.security.ssl.Alert.createSSLException(Alert.java:129)
at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:371)
at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:314)
at java.base/sun.security.ssl.HandshakeContext.dispatch(HandshakeContext.java:482)
at java.base/sun.security.ssl.HandshakeContext.dispatch(HandshakeContext.java:458)
at java.base/sun.security.ssl.TransportContext.dispatch(TransportContext.java:201)
at java.base/sun.security.ssl.SSLTransport.decode(SSLTransport.java:172)
at java.base/sun.security.ssl.SSLSocketImpl.decode(SSLSocketImpl.java:1500)
at java.base/sun.security.ssl.SSLSocketImpl.readHandshakeRecord(SSLSocketImpl.java:1415)
at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:450)
at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:421)
at InstallCert.main(InstallCert.java:87)
Caused by: java.lang.UnsupportedOperationException
at InstallCert$SavingTrustManager.getAcceptedIssuers(InstallCert.java:171)
at java.base/sun.security.ssl.AbstractTrustManagerWrapper.checkAlgorithmConstraints(SSLContextImpl.java:1550)
at java.base/sun.security.ssl.AbstractTrustManagerWrapper.checkAdditionalTrust(SSLContextImpl.java:1498)
at java.base/sun.security.ssl.AbstractTrustManagerWrapper.checkServerTrusted(SSLContextImpl.java:1442)
at java.base/sun.security.ssl.CertificateMessage$T13CertificateConsumer.checkServerCerts(CertificateMessage.java:1341)
at java.base/sun.security.ssl.CertificateMessage$T13CertificateConsumer.onConsumeCertificate(CertificateMessage.java:1232)
at java.base/sun.security.ssl.CertificateMessage$T13CertificateConsumer.consume(CertificateMessage.java:1175)
at java.base/sun.security.ssl.SSLHandshake.consume(SSLHandshake.java:396)
at java.base/sun.security.ssl.HandshakeContext.dispatch(HandshakeContext.java:480)
... 8 more

Server sent 2 certificate(s):

1 Subject CN=ip2location.io
Issuer CN=GTS CA 2P2, O=Google Trust Services LLC, C=US
sha1 d4 c5 56 36 85 6f 49 78 91 f0 ee ea 5f 1a d7 fa cd 9c ef ec
md5 be 54 50 3f 7e d5 62 d6 c0 fb ba d8 d0 8d 8c 90

2 Subject CN=GTS CA 2P2, O=Google Trust Services LLC, C=US
Issuer CN=GTS Root R4, O=Google Trust Services LLC, C=US
sha1 6c ba 4f 13 68 c0 46 aa 0f f4 b6 ed 7e 67 d5 4a 30 5b e9 d7
md5 1f 04 46 4e bb f3 e7 04 7f 29 94 e2 51 90 9d 33

I selected 1 and 2 certificates:
Added certificate to keystore 'jssecacerts' using alias 'api.ip2location.io-1'
Added certificate to keystore 'jssecacerts' using alias 'api.ip2location.io-2'

P.S. Logs from this instruction https://www.java2novice.com/issues/suncertpathbuilderexception-unable-to-find-valid-certification-path-to-requested-target/
don't contain this exception: javax.net.ssl.SSLProtocolException: Unsupported handshake message: certificate .
However my code is working with ip2location java library.

Thank you!!