Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Java SSL Connection: Certificate not Trusted, when connecting to server.

Posted on 2008-01-07
7
Medium Priority
?
4,990 Views
Last Modified: 2010-04-21
Hi there,

I'm trying to create a secure connection to a server. However, when I connect I get the following error:

javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: Certificate not Trusted
.....
at com.ibm.jsse.bf.a(Unknown Source)
      at com.ibm.jsse.bf.checkServerTrusted(Unknown Source)
      
Now, the URL I connect to (say https://210.210.210.210:9660/some/pathto/theapp) provides a certificate for url (220.220.220.220) - a different URL, but it's been signed by a certificate authority whose certificate exists in my truststore, so I'd like to trust it anyway.

Here's my code:

System.setProperty("javax.net.ssl.keyStore","mykeystore.jks");
System.setProperty("javax.net.ssl.keyStorePassword","password");
System.setProperty("javax.net.ssl.trustStore","mykeystore.jks");
System.setProperty("javax.net.ssl.trustStorePassword","password");
URL url = new URL(getUrl());
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestProperty ("Content-Type", "text/xml");
PrintWriter out = new PrintWriter (conn.getOutputStream());

Can someone point out where I'm going wrong?

Cheers,
Steve
               
0
Comment
Question by:stevebeech
  • 4
  • 3
7 Comments
 
LVL 23

Expert Comment

by:cmalakar
ID: 20598059
Import the certificate ... using keytool command..
0
 
LVL 23

Expert Comment

by:cmalakar
ID: 20598067
command is..

keytool -import -alias "SomeIdentifier" -keystore $JAVA_HOME/lib/security/cacerts -file certificate_file
0
 

Author Comment

by:stevebeech
ID: 20598079
I don't want to import the certificate, as I'll be providing this app to someone else, who may then connect to a different server. As long as the cert has been signed by the CA I trust, then I want it to be trusted by my app.

Does that make sense?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 23

Expert Comment

by:cmalakar
ID: 20598122
You can try running your java with ssl debug enabled.. like..

java -Djavax.net.debug=all ......

Also check whether the filenames you are specifying for the properties can be found or not..
0
 

Author Comment

by:stevebeech
ID: 20598211
Ok, running in debug mode gives me the following error:

java.io.IOException: HTTPS hostname wrong:  should be <210.210.210.210>

As I suspected, it's the address causing the problem. I'd like to bypass this, as the cert has been signed by a trusted CA.

Thoughts?
0
 
LVL 23

Accepted Solution

by:
cmalakar earned 1000 total points
ID: 20598222
0
 

Author Closing Comment

by:stevebeech
ID: 31419439
Thanks cmalakar. That provided the answer. I was missing a hostname verifier!

Cheers (and keep up the good work)

Steve
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
The Internet has made sending and receiving information online a breeze. But there is also the threat of unauthorized viewing, data tampering, and phoney messages. Surprisingly, a lot of business owners do not fully understand how to use security t…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

879 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question