accessing secure web site using digital certs

Hello,

I am trying to communicate with a secure remote server and download data, and I am having trouble. The log from the remote server says "client passed no certificate". However, I have a valid digital certificate from this site. The certificate works fine in Netscape. I have exported the p12 file and imported the certificate into a trustStore using keytool. I also imported the other 3 certs necessary to complete the cert chain so that I have a complete chain of trusted certs in the trustStore file.

When I run my java client app I receive a "403 Forbidden" message. If I run the client in debug mode I see that it looks like a negotiation is occurring between the remote server and my client app.

My code is shown below; the program was pieced together from various code from the web. At this point I want to get a jsessionid from the remote server. This code returns a jsessionid when I use "https://www.sun.com" as a test site.

Anyone know why the remote server does not receive the cert info?

Thanks in advance.

usage: java httpsclient2 https://www.sun.com ""

import java.net.*;

import javax.net.*;

import javax.net.ssl.*;

import java.io.*;

import java.util.Properties;

public class HttpsClient2 {

String httpserver;

String jsppage;

public HttpsClient2(String server, String jspage) {

httpserver = server;

jsppage = jspage;

}

public void HttpsConnect() {

try {

// Let us create the factory where we can set some parameters for the connection

SSLContext sc = SSLContext.getInstance("SSL");

sc.init(null, null, new java.security.SecureRandom());

String path = httpserver;

if (jsppage != null)

{

path = httpserver + "/" + jsppage;

}

// Create the socket connection and open it to the secure remote web server

HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

URL url = new URL(path);

HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();

connection.setHostnameVerifier(

new HostnameVerifier() {

public boolean verify(String rserver, SSLSession sses) {

if (!rserver.equals(sses.getPeerHost())){

System.out.println( "certificate <" + sses.getPeerHost() + "> does not match host <" + rserver + "> but " + "continuing anyway" );

}

return true;

}

});

System.out.println(connection.getURL());

System.out.println(connection.getHeaderFields());

} catch (java.security.NoSuchAlgorithmException nsae) {

System.err.println("\n" + "The context specified does not exist. Check for the existence of JSSE" + "\n");

System.exit(-1);

} catch (java.security.KeyManagementException kme) {

kme.printStackTrace();

System.exit(-1);

} catch (java.net.MalformedURLException mue) {

System.err.println("\n" + "URL is not exist or protocol does not exist or there is a typo in the submitted URL" + "\n");

System.exit(-1);

} catch (java.net.UnknownHostException uhe) {

System.err.println("\n" + "Remote server does not exist in DNS." + "\n");

System.exit(-1);

} catch (java.io.IOException ioe) {

System.err.println("\n" + "I/O Exception in the connection try again or contact developer." + "\n");

ioe.printStackTrace();

System.exit(-1);

}

}

public static void main(String args[]) throws Exception {

if (args.length >= 1) {

new HttpsClient2(args[0],args[1]).HttpsConnect();

} else {

String mesg = "\n" + "You must supply an argument as follows:" + "\n" + "\n";

mesg += "\t" + "java HttpsClient https://mail.yahoo.com/" + "\n";

mesg += "\t" + "java HttpsClient https://secure.server.com:7001/" + "\n";

System.out.println(mesg);

System.exit(-1);

}

}

}

[3932 byte] By [MissCoon] at [2007-9-30 19:32:02]
# 1
HI,I am facing the same problem. Will you please let me know how did yoyu fix this ?email id : kcswain@yahoo.comRegardsKrushna
kc-ritvik at 2007-7-7 0:17:21 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...
# 2
Does the server trust your certificate? You must import the issuer of your CA into the trusted CA list on the server.-Jay
jaygarala at 2007-7-7 0:17:21 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...
# 3
Hi,Original I had the certificate in .p12 format. I exported into .X509 format and have imported into the truststore.Do I need to import the whole certificate chain ?RegardsKrushna
kc-ritvik at 2007-7-7 0:17:21 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...
# 4
Actually, I got the certificate from the server and the server has the issuers of CA's in the server keystore
kc-ritvik at 2007-7-7 0:17:21 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...
# 5
HI,I imported the whole cert. chain into client's keystore. I still have the same problem. I am able to connect to the server via socket but when I request something, it gives 403 error.Krushna
kc-ritvik at 2007-7-7 0:17:21 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...
# 6

Hi Jay and Krushna,

I posted my solution in the forum "Java Programming" in response to author "elubin" back on 10/11/2004. I copied it out and put it below:

##########begin old message#########

I solved the problem last night around midnight. I haven't gotten around to updating my request to say this.

Anyway, I learned that the private key of a certificate doesn't get imported into the keystore using keytool. That is why the server log indicated there was no certificate. What I had to do was create a Truststore that pointed to the "p12" (PKCS12) file; the file is used like a keystore. I then modified my code to initialize a KeyManagerFactory with this file. I also added code to explicitly initialize a TrustManagerFactory with the JKS keystore file containing the three trusted certificates. After I did this the server responded with the data request.

Whew! I'm new at Java and this was a lot of work. I spent several days putting this together.

In all the searches I did on the net and in the forums I never found anything that completely explained how to put it all together. I guess it forces a newbie to do more research than they would if the code was just given to them on a platter. If you think it is worth while I'll post the source.

Thanks for responding. You were the only one and I posted similar questions in two other forums.

MissCoon at 2007-7-7 0:17:21 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...
# 7

Hi MissCoon,

I am trying to do a HTTPS POST XML data to a secure server which requires us to submit a trusted digital certificate (i.e. Server has been enabled with Client authentication).

We are using jakarta Commons HTTPClient classes for HTTP POST but not sure how to go about submit a certificate and do a HTTPS POST !

Have done something simillar ? could provide a sample

Thanks

cheers

senthil

cxpress at 2007-7-7 0:17:21 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...