Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2006

Tip: Looking for answers? Try searching our database.

JSSE & SSL

Thread view: 
Patrick Wallingford - 22 Nov 2006 11:06 GMT
What would be the easiest way to connect via HTTPS to a server and check
their SSL certificate properties with Java? I would just like to query or
parse the information on the certificate, such as when it is about to
expire, what's the fingerprint on it etc.

Preferably this should be able to do via proxy if direct connection is not
available.

Is JSSE the way to go or am I in the wrong track here?
EJP - 23 Nov 2006 03:27 GMT
> What would be the easiest way to connect via HTTPS to a server and check
> their SSL certificate properties with Java? I would just like to query or
> parse the information on the certificate, such as when it is about to
> expire, what's the fingerprint on it etc.

SSLSocket socket =
((SSLSocketFactory)SSLSocketFactory.getDefault()).createSocket(host, port);
SSLSession session = socket.getSession();
Certificate[] certs = session.getPeerCertificates();

// and away you go
Patrick Wallingford - 23 Nov 2006 08:42 GMT
> SSLSocket socket =
> ((SSLSocketFactory)SSLSocketFactory.getDefault()).createSocket(host,
[quoted text clipped - 3 lines]
>
> // and away you go

Short'n'fancy answer. I like it :) And it works! Cheers ;)
Patrick Wallingford - 23 Nov 2006 09:12 GMT
> SSLSocket socket =
> ((SSLSocketFactory)SSLSocketFactory.getDefault()).createSocket(host,
> port);
> SSLSession session = socket.getSession();
> Certificate[] certs = session.getPeerCertificates();

One more question, though, how do I setup proxy for the socket if that's
needed?
Rogan Dawes - 05 Dec 2006 08:55 GMT
>> SSLSocket socket =
>> ((SSLSocketFactory)SSLSocketFactory.getDefault()).createSocket(host,
[quoted text clipped - 4 lines]
> One more question, though, how do I setup proxy for the socket if that's
> needed?

Then you need to set that up yourself, and it gets more complicated. You
need to use the HTTP CONNECT method to tell your proxy to connect you to
the remote host, before you layer SSL on top of the socket you have
created. Keep in mind that you may need to authenticate to the proxy,
and a few other details.

String host;
int port;
String proxyHost;
int proxyPort;
Socket socket = new Socket();
if (proxyHost != null && proxyPort > 0 && proxyPort < 65536) {
  socket.connect(new InetSocketAddress(proxyHost, proxyPort), timeout);
  OutputStream os = socket.getOutputStream();
  InputStream is = socket.getInputStream();
  BufferedInputStream bis = new BufferedInputStream(is);
  os.write("CONNECT " + host + ":" + port + " HTTP/1.0\r\n\r\n");
  String response = bis.readLine();
  String code = response.split(" ",3)[1];
  if (code.equals("200")) {
    // read the rest of the header lines
    while (!bis.readLine().equals(""));
  } else {
    throw new IOException("Unexpected response line : " + response);
  }
} else {
  socket.connect(new InetSocketAddress(host, port), timeout);
}
SSLSocketFactory factory = (SSLSocketFactory)SSLSocketFactory.getDefault();
// layer SSL on top of our existing socket
SSLSocket sslSocket = (SSLSocket)factory.createSocket(socket, host,
port, true);
SSLSession session = sslSocket.getSession();
Certificate[] certs = session.getPeerCertificates();

This was written straight into my news reader, and is completely
untested. However, it is taken from working code with only a few
modifications, so the idea is sound. If you have any troubles, explore
the API calls used here, and I'm sure you'll figure it out.

Rogan


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.