SSL (Secure Sockets Layer) is a protocol which is used for communicating securely over the network. SSL provides both, encryption as well as authentication. For example, in client-server communication it encrypts the data, which browser(client) sends to the server or server sends to the client(browser), as well as authenticates the server to the client. Here authentication means that the client is confirmed that the server to which it is talking is genuine.
SSL works on public/private key cryptography. In SSL, certificates are used to prove the identity to other user.
What is a certificate and how does it work?
Let's say there are three parties. Party 1 is client which uses the services of Party 2. Party2 proves its identity by providing a certificate, signed by a third party, to party 1. Both the parties, party 1 as well as party 2, trust party 3. A certificate contains some information about the party and its public key, it is signed by a CA(Certificate Authority). A certificate authority is the third party which is trusted by both, owner of the certificate as well as the party relying on the certificate. Most of the popular browsers contain information about all the CAs, i.e. they know the genuine CAs, and so these browsers trust the certificates signed by them. If the certificate is signed by a party which is not trusted by the browser then it gives warning that the certificate owner may not be the one who it claims to be. In this case although your channel is secure but the other party is not authenticated, so you might be giving your secret information to a scrupulous person over an encrypted channel which is as good as having no security.
Working:
1) When SSL is configured on server(apache), the client(browser) is presented with the server certificate signed by a known CA. Now Client knows the public key of the server.
2) For authentication, server creates a message and computes its hash value. Server encrypts the hash with its private key. Server sends both, message as well as its encrypted hash, to the client.
3) Client decrypts the encrypted hash using public key of the server. It computes hash value of the message it gets from the server. It compares both the hashes to check the authenticity of the server. If both the hashes are same then the client is talking to a genuine server.
4) As authentication is complete, both client and server need some way to pass the data securely. To achieve this, client selects a synchronous key, encrypts it with the server's public key and sends the encrypted key to the server.
5) Server, after getting the encrypted synchronous key, decrypts it. Now both, client and server, have the synchronous key. They encrypt the data to be passed over the network using this synchronous key. So, security is also achieved in this step.
Let's see how to configure SSL in apache.
We assume that apache is installed with SSL module(mod_ssl), and open SSL is also installed on the machine.
Make apache listen for HTTPS requests on port 443 by executing the following command.
a2enmod ssl
The above command enables SSL module of apache
Restart apache
/etc/init.d/apache2 restart
Apache has started listening on port 443.
Use the following command to generate a certificate request and a private key.
openssl req -new -newkey rsa:2048 -nodes -keyout server.key -out server.csr
It will ask for some basic information which you have to answer to get the certificate request generated.
Once your private key and certificate request is generated, you will have to get it signed. You can get your certificate signed by a commercial CA or you can sign it yourself. The only problem in signing it yourself is that the browser will give warning since it does not know the certificate signing authority.
Let's see how to self sign a certificate request. Use the following command to sign
openssl x509 -req -days 100 -in server.csr -signkey server.key -out server.crt
The above command creates a certificate which is valid for 100 days. Now you have got two files generated.
server.key private key
server.crt certificate
Let's configure apache to use these.
Supposing that there is virtual hosting on this server.
Inside directory /etc/apache2/sites-enabled / create a copy of default-ssl and rename it. Let's say the new file is abc-ssl
cat default-ssl > abc-ssl
Now we have to tweak at some places in abc-ssl
<VirtualHost <Your-IP-Address>>
ServerName <your-site-name>
DocumentRoot <site-directory-path>
<Directory <site-directory-path>>
SSLCertificateFile <File-path-certificate>
SSLCertificateKeyFile <File-path-key>
With all these changes done, run the following command to enable the ssl configurarion for this website
a2ensite abc-ssl
Reload apache with changed configuration
/etc/init.d/apache2 reload
Now the server will listen and forward any https request for this site.
You can also tweak .htaccess to configure whether you want to access whole website or only some of the pages of the website with https.
For more info visit.