Two Way SSL Authentication
In standard SSL connections your browser
verifies the identity of the server via it's certificate. With 2 way
authentication your browser also needs a certificate in order for the
server to verify it and allow it access to the pages.
Steps reqired :
- Creating OpenSSL certificates
- Configure Apache
- Configure your browser
|
Creating OpenSSL certificates
Make sure OpenSSL is installed on whichever server you want to be your CA.
You will need an openssl.cnf file. Here is the one I used.
#/etc/ssl/openssl.cnf
[ req ]
default_md = sha512
distinguished_name = req_distinguished_name
[ req_distinguished_name ]
countryName = Country
countryName_default = GB
countryName_min = 2
countryName_max = 2
localityName = Locality
localityName_default = United Kingdom
organizationName = Organization
organizationName_default = SpiderWiki
commonName = Common Name
commonName_max = 64
[ certauth ]
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid:always,issuer:always
basicConstraints = CA:true
crlDistributionPoints = @crl
[ server ]
basicConstraints = CA:FALSE
keyUsage = digitalSignature, keyEncipherment, dataEncipherment
extendedKeyUsage = serverAuth
nsCertType = server
crlDistributionPoints = @crl
[ client ]
basicConstraints = CA:FALSE
keyUsage = digitalSignature, keyEncipherment, dataEncipherment
extendedKeyUsage = clientAuth
nsCertType = client
crlDistributionPoints = @crl
[ crl ]
URI=http://www.spiderwiki.org/ca.crl
So first we need a self signed certificate for our CA.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
openssl req -config /etc/ssl/openssl.cnf -newkey rsa:2048 -nodes -keyform PEM -keyout ca.key -x509 -days 3650 -extensions certauth -outform PEM -out ca.cer
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Next we will generate a private SSL key for our server.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
openssl genrsa -out server.key 2048
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
To generate Certificate Signing Request (PKCS#10) run the following
command. For the common name you should put the URL for the server e.g.
www.spiderwiki.org
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
openssl req -config /etc/ssl/openssl.cnf -new -key server.key -out server.req
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
With self-signed certificate authority issue server certificate with serial number 100:
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
openssl x509 -req -in server.req -CA ca.cer -CAkey ca.key -set_serial 100 -extfile /etc/ssl/openssl.cnf -extensions server -days 365 -outform PEM -out server.cer
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
The new file contains both the certificate and the private key so we can delete the request file.
rm server.req
Now that the server certificates are done we need to create the key for a client.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
openssl genrsa -out client.key 2048
Then the request.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
openssl req -config /etc/ssl/openssl.cnf -new -key client.key -out client.req
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
And issue certificate ID with our CA for the client.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
openssl x509 -req -in client.req -CA ca.cer -CAkey ca.key -set_serial 101 -extfile /etc/ssl/openssl.cnf -extensions client -days 365 -outform PEM -out client.cer
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Save client's private key and certificate in a PKCS#12 format. You will need to set a password in this command.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
openssl pkcs12 -export -inkey client.key -in client.cer -out client.p12
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Then tidy up.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
rm client.key client.cer client.req
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Configure Apache
Depending on the version of Apache and your distro this can be slightly different so as a general guide.
- You need to tell Apache to also listen on port 443 (at least that's the default for SSL)
- You need to enable the SSL module.
- Set a new VirtualHost (or modify your main server to use SSL)
- Move the required files to the location set in your virtual host.
- Restart Apache
Here is my default-ssl virtual host config file.
#vi /etc/apache2/sites-available/default-ssl
<IfModule mod_ssl.c>
<VirtualHost _default_:443>
ServerAdmin webmaster@localhost
DocumentRoot /var/securewww/
Options FollowSymLinks
Options Indexes FollowSymLinks MultiViews
LogLevel warn
ErrorLog /var/log/apache2/error.log
CustomLog /var/log/apache2/ssl_access.log combined
SSLEngine on
SSLCertificateFile /etc/apache2/ssl/server.cer
SSLCertificateKeyFile /etc/apache2/ssl/server.key
# Below for 2 way ssl
SSLVerifyClient require
SSLVerifyDepth 10
SSLCACertificateFile /etc/apache2/ssl/ca.cer
</VirtualHost>
</IfModule>
Configure your browser
Copy client.p12 file to the machine you intend to use.
Install it into your browsers certificate store.
This process is different on each browser.