Together with Stefan, I have been looking into what would be needed for
the "XMailServer Control Library" to be able to talk over TLS. We ended
up creating a small peace of Java as test (didn't include it in
CtrlClnt.jar yet, but that shouldn't be too difficult). If anybody else
needs this, please find the example below.

And if anybody reading this has experience with SourceForge, please do
add this code to the current project on
http://sourceforge.net/projects/xmail-ctrlclnt/
I would expect the larger part of this to go into the "connect" method.

Greetings,
Bart

=========

Socket socket = new Socket("127.0.0.1", 6017);

InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));

//Read the greeting from the server
String line = br.readLine();
System.out.println(line); //You might want to check here if it's what
you expected it to be

//Tell the server that we'll be using TLS
out.write("#!TLS\r\n".getBytes());
line = br.readLine();
System.out.println(line); //You might want to check if the response was
something like "+00000 Ready to start TLS mode"

//set up everything to be able to talk over TLS
SSLContext ctx = SSLContext.getInstance("TLS");
SecureRandom rnd = null;
byte seed[] = new byte[1024];
FileInputStream is = new FileInputStream("/dev/urandom");
is.read(seed);
is.close();
rnd = java.security.SecureRandom.getInstance("SHA1PRNG");
rnd.setSeed(seed);
//Accept self signed certs, remove this if you don't want this
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) {
        }
    }
};
KeyManager[] km = null;
ctx.init(km, trustAllCerts, rnd);
SSLSocketFactory sslFactory = ctx.getSocketFactory();

//enable ssl for existing socket
socket = (SSLSocket) sslFactory.createSocket(socket, "127.0.0.1.", 6017,
true);

//we'll need new streams from the new socket
in = socket.getInputStream();
out = socket.getOutputStream();

//Okay, now you can talk to the server as normal, first you'll need to
log-in

_______________________________________________
xmail mailing list
xmail@xmailserver.org
http://xmailserver.org/mailman/listinfo/xmail

Reply via email to