Hi,
I?m trying to setup a XMLRPC call to a server which is running SSL and
using digital certificates to authenticate. I'm using JDK 1.4 so I've already
got the JSSE packages installed, but I'm still getting authentication errors
when I start using the RPC library.
This code works:
import java.io.*;
import java.net.*;
import javax.net.ssl.*;
import javax.security.cert.X509Certificate;
import java.security.KeyStore;
SSLSocketFactory factory = null;
String keyfile = "/keyfile";
String password = "password";
try {
SSLContext ctx;
KeyManagerFactory kmf;
KeyStore ks;
ctx = SSLContext.getInstance("TLS");
kmf = KeyManagerFactory.getInstance("SunX509");
ks = KeyStore.getInstance("PKCS12");
ks.load(new FileInputStream(keyfile),password.toCharArray());
kmf.init(ks,passphrase);
ctx.init(kmf.getKeyManagers(),null,null);
factory = ctx.getSocketFactory();
} catch (Exception e) {
System.err.println(e.toString());
System.exit(0);
}
System.out.println("Loaded digital cert");
try {
// open the socket on port 444
SSLSocket socket = (SLSocket)factory.createSocket("hostname.com", 444);
socket.startHandshake();
PrintWriter out = new PrintWriter(new BufferedWriter(new
OutputStreamWriter(socket.getOutputStream())));
// the request ? replace this with the XML Post request
out.println("POST GET / HTTP/1.0");
out.println("User-Agent: Client test");
out.println("Content-Type: text/xml");
out.println();
out.flush();
if (out.checkError())
{
System.out.println("IO ERROR");
System.exit(0);
}
BufferedReader in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
String line;
while ((line=in.readLine())!=null)
{
System.out.println(line);
}
in.close();
out.close();
socket.close();
} catch (Exception e) {
System.err.println(e.toString());
}
However when I try to combine this with the RPC code:
import java.io.*;
import java.net.*;
import javax.net.ssl.*;
import java.util.*;
import javax.security.cert.X509Certificate;
import java.security.KeyStore;
import org.apache.xmlrpc.*;
import org.apache.xmlrpc.secure.*;
try {
SSLContext ctx;
KeyManagerFactory kmf;
KeyStore ks;
ctx = SSLContext.getInstance("TLS");
kmf = KeyManagerFactory.getInstance("SunX509");
ks = KeyStore.getInstance("PKCS12");
ks.load(new FileInputStream(keyfile),
password.toCharArray());
kmf.init(ks,password.toCharArray());
ctx.init(kmf.getKeyManagers(),null,null);
SSLSocketFactory factory = ctx.getSocketFactory();
HttpsURLConnection.setDefaultSSLSocketFactory(factory);
} catch (Exception e) {
System.err.println(e.toString());
System.exit(0);
}
// setup the RPC call
Vector v = new Vector();
v.addElement("Hello");
try {
SecureXmlRpcClient client = new SecureXmlRpcClient(url);
XmlRpc.setDebug(true);
String responce = (String) client.execute("test method",v);
System.out.println(responce);
} catch (Exception e) {
System.err.println(e.toString());
System.exit(0);
}
It falls over with a "java.io.IOException: Couldn't find trusted certificate"
error (as if the default SSL factory is being ignored).
Any ideas ?
Thanks
Graham