Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-03-2007, 06:57 PM
Member
 
Join Date: Jun 2007
Posts: 92
Marcus is on a distinguished road
java.security.cert.CertificateException: Couldn't find trusted certificate
I had the following code to connect to a site on a server. The server is using https, which causes the error

javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: Couldn't find trusted certificate


I have saved the certificate for reference and I assume to I need to tell the program to look at that but this will happen every time there is a new certificate

Changing the HttpURLConnection object to HttpsURLConnection throws up a 'java.lang.ClassCastException' error coz its an abstract class..

Code:
java.util.Properties propSy = System.getProperties(); propSy.put("java.protocol.handler.pkgs","com.sun.net.ssl.internal.www.protocol"); System.setProperties(propSy); java.security.Security.insertProviderAt(new sun.security.provider.Sun(),2); java.security.Security.addProvider(new sun.security.provider.Sun()); java.security.Security.insertProviderAt(new com.sun.net.ssl.internal.ssl.Provider(),1); System.setProperty("javax.net.ssl.trustStore", "keystore_filename"); java.security.Provider myprov = java.security.Security.getProvider("SunJSSE"); HttpsURLConnection c; try { URL url = new URL ( rptUrl ); c = (HttpsURLConnection)url.openConnection(); //set cache and request method settings c.setUseCaches(false); //set other headers c.setRequestProperty ("Content-Type", "application/pdf"); //connect to the server.. c.connect(); }
Any ideas? Thanks.

Marcus
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-03-2007, 07:11 PM
Member
 
Join Date: Jun 2007
Posts: 92
Daniel is on a distinguished road
One problem people have with Java and SSL is that the certificate manager will reject any self signed certificates.

This is common when you are doing development or are using an internal certificate that you don't want to pay money for from a commercial certificate authority.

One way to handle this is to simply accept any certificate, regardless of what it has in it. Below is a simple program to do just that, it reads a URL and, regardless of what the certificate says, prints out the contents of the URL.

Code:
package com.xigole.util.ssl; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; public class SelfSignedCertTest { public static void main(String[] argv) { if (argv.length != 1) { System.err.println("usage: SelfSignedCertTest hostname"); System.exit(1); } TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; } public void checkClientTrusted( java.security.cert.X509Certificate[] certs, String authType) { } public void checkServerTrusted( java.security.cert.X509Certificate[] certs, String authType) { System.out.println("authType is " + authType); System.out.println("cert issuers"); for (int i = 0; i < certs.length; i++) { System.out.println("\t" + certs[i].getIssuerX500Principal().getName()); System.out.println("\t" + certs[i].getIssuerDN().getName()); } } } }; try { SSLContext sc = SSLContext.getInstance("SSL"); sc.init(null, trustAllCerts, new java.security.SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); } catch (Exception e) { e.printStackTrace(); System.exit(1); } URL url = null; try { url = new URL("https", argv[0], 443, "/"); } catch (MalformedURLException mue) { mue.printStackTrace(); System.exit(1); } try { URLConnection connection = url.openConnection(); InputStream is = connection.getInputStream(); byte[] buffer = new byte[1024]; int bytesRead = 0; while ((bytesRead = is.read(buffer)) != -1) System.out.println(new String(buffer, 0, bytesRead)); } catch (IOException ioe) { ioe.printStackTrace(); System.exit(1); } } }
Greetings.

Daniel
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-03-2007, 07:17 PM
Senior Member
 
Join Date: Jun 2007
Posts: 111
Eric is on a distinguished road
I attach a zip file with a build.xml so that you can build the program with ant.

Greetings.

Eric
Attached Files
File Type: zip selfsigned.zip (11.2 KB, 9 views)
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java certificate Nick15 Professional Certification 2 11-12-2007 08:02 AM
java.security.cert.CertificateException: Couldn't find trusted certificate Felissa Enterprise JavaBeans 4 08-11-2007 12:09 AM
java.security.AccessControlException cecily Java Applets 1 08-06-2007 04:49 AM
difference between code based security and role based security boy22 New To Java 1 07-24-2007 01:59 AM
Java cert exam samples orchid New To Java 5 06-04-2007 11:33 AM


All times are GMT +3. The time now is 03:53 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org