Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2008-01-07
7
4,837 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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 250 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
web application structure 18 96
java imports not found 4 20
Setup GlassFish 4 21
Using jdbcTemplate.batchUpdate to improve INSERT performance 6 20
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

809 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