Home > Documentation > Apache web server > How to setup a secure website that requires a P12 certificate

How to setup a secure website that requires a P12 certificate

Posted by peter on January 10, 2012

The following is for Centos 5.5

Intall apache and openssl with yum as follows:


yum install httpd

yum install openssl

yum install mod_ssl

 

Backup the /etc/pki/tls/openssl.cnf file

Open the /etc/pki/tls/openssl.cnf and replace the content with the following:

------------------- start of example------------------------

[ req ]

default_md = sha1

distinguished_name = req_distinguished_name

 [ req_distinguished_name ]

countryName = Country

countryName_default = SA

countryName_min = 2

countryName_max = 2

localityName = Locality

localityName_default = Gauteng

organizationName = Organization

organizationName_default = Linux Networking Solutions

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://testca.local/ca.crl

[ req ]

default_md = sha1

distinguished_name = req_distinguished_name

 [ req_distinguished_name ]

countryName = Country

countryName_default = SA

countryName_min = 2

countryName_max = 2

localityName = Locality

localityName_default = Gauteng

organizationName = Organization

organizationName_default = Linux Networking Solutions

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://testca.local/ca.crl

------------------- End of example------------------------

Cd to the /etc/pki/tls directory

	cd /etc/pki/tls

Generate a self-signed Certificate with the following command all on one line (it is wrapped below):

openssl req -config ./openssl.cnf -newkey rsa:2048 -nodes -keyform PEM -keyout ca.key -x509 -days 3650 -extensions certauth -outform PEM -out ca.cer


If you have not encountered any complications running the above command you would find in your current directory a file “ca.key” with private key of certificate authority (CA) and ca.cer with its self-signed certificate.

Generate private SSL key for the server with the following command:

  openssl genrsa -out server.key 2048

To generate Certificate Signing Request in PKCS#10 format you would use a following command as a common name you can specify its hostname – for example “localhost”.

	
	openssl req -config ./openssl.cnf -new -key server.key -out server.req

With self-signed certificate authority issue server certificate with serial number 100, Run the command on one line and not wrapped as shown below:

 openssl x509 -req -in server.req -CA ca.cer -CAkey ca.key -set_serial 100 -extfile openssl.cnf -extensions server -days 365 -outform PEM -out server.cer

New file server.key contains server's private key and file server.cer is a certificate itself. Certificate Signing Request file server.req is not needed any more so it can be removed.

	rm server.req

Generete private key for SSL client with the following command:

	openssl genrsa -out client.key 2048

 

Generate Certificate Signing Request for the server. As an example, the Common Name, I have used string: “Linux Networking Solutions”.

 

	openssl req -config ./openssl.cnf -new -key client.key -out client.req

With your self-signed Certificate Authority, issue a client certificate with serial number 101 with the following command (on one line not wrapped as shown below):

openssl x509 -req -in client.req -CA ca.cer -CAkey ca.key -set_serial 101 -extfile openssl.cnf -extensions client -days 365 -outform PEM -out client.cer

Save client's private key and certificate in a PKCS#12 format. This certificate will be secured by a password and this password will be used in the following sections to import the certificate into the web browser's certificate manager:

	openssl pkcs12 -export -inkey client.key -in client.cer -out client.p12

File “client.p12” contains how to load ssl_module in centosa private key and the client's certificate, therefore files “client.key”, “client.cer” and “client.req” are no longer needed, so these files can be deleted with the following command:

openssl genrsa -out client.key 2048	rm client.key client.cer client.req

Edit the /etc/httpd/conf/httpd.conf and insert the following lines

	LoadModule ssl_module modules/mod_ssl.so
	Listen 443

If you are not using port 80 then put a “#” in front of the “listen 80”. If you don't then your pages will open up without the P12 cert on port 80. You can use port 80 with your default website location and the secure connection for another site or folder of the same site. (more about that later – for now # out Listen 80)

Create a file /etc/httpdopenssl genrsa -out client.key 2048/conf.d/ssl2.conf , the file and should look as the one below:

------------------- start of example------------------------

<VirtualHost _default_:443>

SSLVerifyClient require

SSLVerifyDepth 10

SSLCACertificateFile /etc/pki/tls/ca.cer

ServerAdmin webmaster@localhost

 DocumentRoot /var/www/

Options FollowSymLinks

 Options Indexes FollowSymLinks MultiViews

 ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/

 Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch

 LogLevel warn

ErrorLog /var/log/httpd/error.log

CustomLog /var/log/httpd/ssl_access.log combined

SSLEngine on

SSLCertificateFile /etc/pki/tls/server.cer

SSLCertificateKeyFile /etc/pki/tls/server.key

# SetEnvIf User-Agent ".*MSIE.*" \

BrowserMatch ^Mozilla forms jpeg=yes browser=netscape

BrowserMatch "^Mozilla/[2-3]" tables agif frames javascript

BrowserMatch MSIE !javascript nokeepalive ssl-unclean-shutdown downgrade-1.0 force-response-1.0

</VirtualHost>

------------------- end of example------------------------

Back up the ssl.conf file and overwite with the following: (Remove the first two lines if you have already included them in the /etc/httpd/conf/httpd.conf file)

------------------- start of example------------------------

LoadModule ssl_module modules/mod_ssl.so

 Listen 443

AddType application/x-x509-ca-cert .crt

AddType application/x-pkcs7-crl .crl

 SSLPassPhraseDialog builtin

 SSLSessionCache shmcb:/var/cache/mod_ssl/scache(512000)

SSLSessionCacheTimeout 300

 SSLMutex default

 SSLRandomSeed startup file:/dev/urandom 256

SSLRandomSeed connectLoadModule ssl_module modules/mod_ssl.so

Listen 443

 AddType application/x-x509-ca-cert .crt

AddType application/x-pkcs7-crl .crl

 SSLPassPhraseDialog builtin

 SSLSessionCache shmcb:/var/cache/mod_ssl/scache(512000)

SSLSessionCacheTimeout 300

 SSLMutex default

 SSLRandomSeed startup file:/dev/urandom 256

SSLRandomSeed connect builtin

 SSLCryptoDevice builtin

------------------- end of example------------------------

Copy the “client.p12” and “server.cer” files to the client PC and install with your browser. In Firefox go to “edit” “preferences” “advanced” “Encryption” “view Certificates” and install them under “your certificates” and “server”

restart httpd and set to start when the server starts

service httpd start

chkconfig httpd on

How to make only a part of your website secure with a P12 cert.

Create a directory call “secure” in the /var/www/ folder and add a “directory location” into your /etc/httpd/conf/httpd.conf file after the “Alias /icons” section as follows:

------------------- start of example------------------------

Alias /icons/ "/var/www/icons/"

 <Directory "/var/www/icons">

Options Indexes MultiViews

AllowOverride None

Order allow,deny

Allow from all

</Directory>

 <Directory "/var/www/secure">

Options Indexes MultiViews

AllowOverride None

Order allow,deny

Allow from all

</Directory>

------------------- end of example------------------------

Edit the ssl2.conf file and change the directory path the reflect the above directory path as per the following example:

------------------- start of example------------------------

DocumentRoot /var/www/secure/

------------------- end of example------------------------

Restart httpd

service httpd restart

Comments:

Leave a Reply



(Your email will not be publicly displayed.)

Please type the letters and numbers shown in the image.Captcha CodeClick the image to see another captcha.