Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news.glorb.com!postnews.google.com!en1g2000vbb.googlegroups.com!not-for-mail From: Stone Newsgroups: comp.lang.java.programmer Subject: Certificate validation Date: Fri, 5 Aug 2011 13:09:34 -0700 (PDT) Organization: http://groups.google.com Lines: 217 Message-ID: <13e9bbc6-ba9c-47ac-96fa-dbd9dcba133b@en1g2000vbb.googlegroups.com> NNTP-Posting-Host: 84.42.251.145 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 X-Trace: posting.google.com 1312575098 22244 127.0.0.1 (5 Aug 2011 20:11:38 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Fri, 5 Aug 2011 20:11:38 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: en1g2000vbb.googlegroups.com; posting-host=84.42.251.145; posting-account=IOeCfwoAAAA_VejOv6qSgFbw-0eHdS9A User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-Header-Order: HUALESNKRC X-HTTP-UserAgent: Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0,gzip(gfe) Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:6812 Dear java users/developers, I am trying to validate certificates in JAVA imported from the server but it seems that my code is wrong. Could you please let me know where I have made a mistake? Code is following: package ssltest; import java.io.*; import java.net.*; import java.util.*; import javax.net.ssl.*; import java.security.cert.*; import java.security.*; import java.util.regex.*; /** * * @author CZ2B10q6 */ public class SSLTest implements HandshakeCompletedListener{ private int port = 5000; private String ip="192.168.0.5"; private SSLSocketFactory sslSocketFactory; private SSLSocket connection; private SSLContext sc; private PrintWriter outStream; private BufferedReader inStream; /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here System.out.println("Start"); SSLTest e = new SSLTest(); } public SSLTest() { System.out.println("Connecting to "+ip+" to port "+port); connectTo(); } public final void disableCertificates() { // Create a trust manager that does not validate certificate chains 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) { } public boolean isClientTrusted(X509Certificate[] arg0) { return true; } public boolean isServerTrusted(X509Certificate[] arg0) { return true; } } }; // Install the all-trusting trust manager try { sc = SSLContext.getInstance("TLSv1"); String help = sc.getProvider().toString(); System.out.println(sc.getProvider().getName()); System.out.println(help); help = sc.getProvider().getClass().toString(); System.out.println(help); sc.init(null, trustAllCerts, new java.security.SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); } catch (Exception e) { e.printStackTrace(); } HostnameVerifier hv = new HostnameVerifier() { public boolean verify(String string, SSLSession ssls) { System.out.println("Warning: URL Host: " + string + " vs. " + ssls.getPeerHost()); return true; } }; HttpsURLConnection.setDefaultHostnameVerifier(hv); } private static void printSocketInfo(SSLSocket s) { System.out.println("Socket class: "+s.getClass()); System.out.println(" Remote address = " +s.getInetAddress().toString()); System.out.println(" Remote port = "+s.getPort()); System.out.println(" Local socket address = " +s.getLocalSocketAddress().toString()); System.out.println(" Local address = " +s.getLocalAddress().toString()); System.out.println(" Local port = "+s.getLocalPort()); System.out.println(" Need client authentication = " +s.getNeedClientAuth()); SSLSession ss = s.getSession(); System.out.println(" Cipher suite = "+ss.getCipherSuite()); System.out.println(" Protocol = "+ss.getProtocol()); } public void connectTo() { String patternString = "AES.*256"; Pattern pattern = Pattern.compile(patternString); Matcher matcher; boolean matchFound; try { System.out.println("Initialization of trust Manager"); disableCertificates(); Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); Provider[] pr = Security.getProviders(); for(int i=0;i