Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

HTTPS connection to a secure server from Java program

843811Mar 7 2007 — edited May 7 2007
Hello all,

I hope someone can help with this.

I've got a Java program that I am trying to get to connect to an external server. The program is running on my local development box, and I am using Rational Application Developer. To run the program, I'm right clicking the main class, and running it as a Java Application.

I generated a certificate signing request using keytool and sent it to the server CA, and received a .cer file in return. I first installed this on my PC using the 'let windows decide where to put it' option, and after some reading, discovered that I would need to add it to my keystore.

So I did this:

keytool -import -file c:\certificates\mycert.cer

The import appeared to run fine, and a new .keystore file was created in my userspace (C:\docs and settings\user.....).

I've also imported the certificate into RAD using the import certificate wizard.

The code is:
*********************************************************
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
LoadBalanceSvc balanceSvc = new LoadBalanceSvcLocator();

System.getProperties().put( "http.proxyCredentials", "CredentialCache.DefaultCredentials" );

URL url = new URL("https://10.223.3.19/loadbalancewebservice/loadbalance2003Svc.asmx");



LoadBalanceSvcSoap balanceSvcSoap = balanceSvc.getLoadBalanceSvcSoap(url);
System.out.println(balanceSvcSoap.getHomeMDB(60000, LoadBalance.businessArea));

*********************************************************

When the URL was HTTP, not HTTPS, it connected fine and printed out the results of the request to the server.

Now I'm not sure what to do. Running the Java application flags up the below:

Exception in thread "main" AxisFault
faultCode: {http://schemas.xmlsoap.org/soap/envelope/}Server.userException
faultSubcode:
faultString: javax.net.ssl.SSLHandshakeException: unknown certificate
faultActor:
faultNode:
faultDetail:
{http://xml.apache.org/axis/}stackTrace: javax.net.ssl.SSLHandshakeException: unknown certificate

So I tried adding the following code:

java.security.cert.Certificate certificate = null;

try {

String certificateFile = "C:/certificates/mycert.cer";
FileInputStream certificateFileInputStream = new FileInputStream(certificateFile);


CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");

certificate = certificateFactory.generateCertificate(certificateFileInputStream);

KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());

String password ="password";

keystore.load(null, null);

keystore.setCertificateEntry("mycert", certificate);

System.setProperty("javax.net.ssl.keyStore", certificateFile);

} catch (KeyStoreException e) {
System.out.println("Keystore exception: " + e.getMessage());
}

catch (IOException e) {
System.out.println("IO Exception: " + e.getMessage());
} catch (CertificateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

And now I get:

Exception in thread "main" AxisFault
faultCode: {http://schemas.xmlsoap.org/soap/envelope/}Server.userException
faultSubcode:
faultString: java.net.SocketException: KeyManagerFactoryImpl: Invalid keystore format
faultActor:
faultNode:
faultDetail:
{http://xml.apache.org/axis/}stackTrace: java.net.SocketException: KeyManagerFactoryImpl: Invalid keystore format

Basically I would just like the program to run, pick up the keystore that I created, and communicate with the HTTPS server with the certificate in that keystore.

I know this posting is a little all over the place....

Can anybody out there help please? Many thanks in advance, I'm really stuck!!

Cheers

Message was edited by:
Sozzled
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 4 2007
Added on Mar 7 2007
7 comments
338 views