ssl-socket-demo
ssl-socket-demo copied to clipboard
A small demo project showing how to set up 2-way SSL communication using raw sockets, Java and OpenSSL.
Two-way authenticated SSL communication
This demo project shows how to create and set up two-way authenticated SSL communication over raw sockets using plain Java (and OpenSSL).
The idea for this is based on the following blog posting: http://thoughtcrime.org/blog/authenticity-is-broken-in-ssl-but-your-app-ha/ (option 1) which basically explains the following set up:
- A private (self-signed) CA is used to create a 4096-bit signing certificate;
- this signing certificate is used to create two signed certificates, one for the server, and one for the client;
- both the client and server get/include a copy of the signing certificate to verify the identify of its peer.
Usage
Note that this project is intended for demo purposes, showing the abilities of two-way authenticated SSL communication. As such, it should not be used in production situations!
Basic steps to set up the certificates:
cd sslcert;- run
./create_root_cert.shand answer the questions. For common name, use something like "Certificate Authority" or anything you like; - run
./export_root_cert_to_keytool.sh cacertto create the Java keystore with the signing certificate (which is the certificate trusted by both client and server); - run
./create_signing_request.sh serverto create a signing request for the server certificate, and answer all questions. For common name, use the FQDN of the server (which is not verified at runtime, but helps you keep the certificates apart); - run
./sign_request.sh serverto sign and create the actual certificate for the server; - run
./export_cert_key_to_keytool.sh serverto export the server certificate and its private key to a Java keystore; - repeat steps 4 through 6 for the client certificate (use
clientas name); - copy the keystores to their respective locations, by running
copy_keystores.sh.
Building the demo server and client
cd ssl.socket- run
ant clean buildto build the demo JAR. Note that you need Java7 to compile the code and create a JAR file in thegenerateddirectory.
Steps to run the server
- run
java -cp generated/ssl.socket.jar nl.lxtreme.ssl.socket.server.SslServer 9000to start the server at port 9000 (replace 9000 with any other port if you like).
Steps to run the client
- run
java -cp generated/ssl.socket.jar nl.lxtreme.ssl.socket.client.SslClient localhost 9000to start the client and let it communicate to the server running at localhost on port 9000 (again, change the hostname and port number to your likings).
The result will be a few lines that are written to the console(s) of both the client and server, for example:
Server started. Awaiting client...
Client (client.localhost) connected. Awaiting ping...
Ping received. Sending pong...
Pong written. Ending server...
and
Connected to server (server.localhost). Writing ping...
Ping written, awaiting pong...
Pong obtained! Ending client...
Both the client and server terminate after this.
License
This code is licensed under Apache-2.0 License.
Author
This code is written by Jan Willem Janssen, [email protected].