Assume that you want to write an Android application, that needs to communicate with your server or your wireless router at home, for personal use. You might be interested in securing this communication against eavesdropping, so that nobody else sees, what you application sends and receives. You also might be interested in authenticating you communication, so that you can be sure that only your application and your server or router communicate, and nobody else is able to modify the content transmitted, without being noticed. The SSL/TLS protocol is a perfect solution for this problem, so that you don’t need to invent a solution yourself.
Certificates
To ensure authenticity of both communication partners, X.509 certificates can be used. Most secure websites in the internet like paypal, ebay, or amazon only use X.509 certificates for the server, and the client is authenticated using a username and a password. For this example, X.509 certificates will be used for both communication partners.
To generate two self-signed X.509 certificates, the following script can be used. It will generate two new RSA 2048 bit keys, generate two self signed certificates, and bundle the client certificate with the corresponding private key, and the servers public certificate in a PKCS#12 container file.
#!/bin/bash
OPENSSL_OPTS="-new -newkey rsa:2048 -nodes -days 5475 -x509"
CN_SERVER="/CN=server"
CN_CLIENT="/CN=client"
PASS="123456"
echo "Generating keys"
openssl req -keyout key-server.pem -subj "$CN_SERVER" \
-out cert-server.pem $OPENSSL_OPTS
openssl req -keyout key-client.pem -subj "$CN_CLIENT"\
-out cert-client.pem $OPENSSL_OPTS
echo "Encrypting key for the client now"
openssl pkcs12 -export -passout "pass:$PASS" \
-in cert-client.pem -inkey key-client.pem -out client.p12 \
-certfile cert-server.pem -name "Client" -caname "Server" |
#!/bin/bash
OPENSSL_OPTS="-new -newkey rsa:2048 -nodes -days 5475 -x509"
CN_SERVER="/CN=server"
CN_CLIENT="/CN=client"
PASS="123456"
echo "Generating keys"
openssl req -keyout key-server.pem -subj "$CN_SERVER" \
-out cert-server.pem $OPENSSL_OPTS
openssl req -keyout key-client.pem -subj "$CN_CLIENT"\
-out cert-client.pem $OPENSSL_OPTS
echo "Encrypting key for the client now"
openssl pkcs12 -export -passout "pass:$PASS" \
-in cert-client.pem -inkey key-client.pem -out client.p12 \
-certfile cert-server.pem -name "Client" -caname "Server"
Stunnel
We will use stunnel for the server. Stunnel is a lightweight general SSL/TLS wrapper and proxy. First, we copy cert-client.pem cert-server.pem and key-server.pem to the server to /etc/ssl/stunnel or another directory. Next is the stunnel configuration file:
cert = /etc/ssl/stunnel/cert-server.pem
key = /etc/ssl/stunnel/key-server.pem
CAfile = /etc/ssl/stunnel/cert-client.pem
sslVersion = SSLv3
chroot = /var/lib/stunnel4/
setuid = stunnel4
setgid = stunnel4
pid = /stunnel4.pid
socket = l:TCP_NODELAY=1
socket = r:TCP_NODELAY=1
[service]
accept = 1279
connect = target:1280
verify = 2 |
cert = /etc/ssl/stunnel/cert-server.pem
key = /etc/ssl/stunnel/key-server.pem
CAfile = /etc/ssl/stunnel/cert-client.pem
sslVersion = SSLv3
chroot = /var/lib/stunnel4/
setuid = stunnel4
setgid = stunnel4
pid = /stunnel4.pid
socket = l:TCP_NODELAY=1
socket = r:TCP_NODELAY=1
[service]
accept = 1279
connect = target:1280
verify = 2
This will set up a stunnel server, listening on port 1279, and forwarding the unencrypted communication to target port 1280. It will only allow connections from a client, presenting a valid certificate.
Android
Next, we can write the code for our Android application:
// Adopt this in your application
String PASSWORD_FOR_PKCS12 = "123456";
InputStream pkcs12in = ......
// You only need to execute this code once
SSLContext context = SSLContext.getInstance("TLS");
// Local client certificate and key and server certificate
KeyStore keyStore = KeyStore.getInstance("PKCS12");
keyStore.load(pkcs12in, PASSWORD_FOR_PKCS12.toCharArray());
// Build a TrustManager, that trusts only the server certificate
TrustManagerFactory tmf = TrustManagerFactory.getInstance("X509");
KeyStore keyStoreCA = KeyStore.getInstance("BKS");
keyStoreCA.load(null, null);
Certificate c = keyStore.getCertificate("Server");
keyStoreCA.setCertificateEntry("Server", c);
tmf.init(keyStoreCA);
// Build a KeyManager for Client auth
KeyManagerFactory kmf = KeyManagerFactory.getInstance(
KeyManagerFactory.getDefaultAlgorithm());
kmf.init(keyStore, null);
context.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
// Everytime you need your https connection, run this code
URL url = new URL("https://my-router:1279/");
HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
urlConnection.setSSLSocketFactory(context.getSocketFactory());
urlConnection.setHostnameVerifier(new AllowAllHostnameVerifier());
InputStream in = urlConnection.getInputStream(); |
// Adopt this in your application
String PASSWORD_FOR_PKCS12 = "123456";
InputStream pkcs12in = ......
// You only need to execute this code once
SSLContext context = SSLContext.getInstance("TLS");
// Local client certificate and key and server certificate
KeyStore keyStore = KeyStore.getInstance("PKCS12");
keyStore.load(pkcs12in, PASSWORD_FOR_PKCS12.toCharArray());
// Build a TrustManager, that trusts only the server certificate
TrustManagerFactory tmf = TrustManagerFactory.getInstance("X509");
KeyStore keyStoreCA = KeyStore.getInstance("BKS");
keyStoreCA.load(null, null);
Certificate c = keyStore.getCertificate("Server");
keyStoreCA.setCertificateEntry("Server", c);
tmf.init(keyStoreCA);
// Build a KeyManager for Client auth
KeyManagerFactory kmf = KeyManagerFactory.getInstance(
KeyManagerFactory.getDefaultAlgorithm());
kmf.init(keyStore, null);
context.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
// Everytime you need your https connection, run this code
URL url = new URL("https://my-router:1279/");
HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
urlConnection.setSSLSocketFactory(context.getSocketFactory());
urlConnection.setHostnameVerifier(new AllowAllHostnameVerifier());
InputStream in = urlConnection.getInputStream();