Code Ghar

Create a Certificate Authority and Certificates with OpenSSL

Posted in configuration by hs on March 17, 2008

This tutorial will assume that you are using Ubuntu 7.10 (Gutsy Gibbon). This tutorial has been adapted from Setting up OpenSSL to Create Certificates and OpenSSL – Community Ubuntu Documentation. I would recommend that you get the latest info from these resources as mine may be out of date. Both of them are very helpful. Besides, without them I would not have been able to create this one.

Install OpenSSL

sudo apt-get install openssl

Create Directory Structure

You need to create a directory structure to store files and also create some initial files. We will first create a main directory. I would recommend creating it under /home so that if you upgrade, you will have access to those files.

mkdir /home/ca /home/ca/private /home/ca/certs /home/ca/conf

cd /home/ca

echo '01' > serial

touch index.txt

Create Certificate Authority (CA)

sudo vim /home/ca/conf/caconfig.cnf

This file would serve as the default config file for the CA. It should look something like the following (taken from the Setting up OpenSSL resource), of course with your own settings:

#..................................
[ ca ]
default_ca = CA_default
[ CA_default ]
dir = /home/ca

serial = $dir/serial
database = $dir/index.txt
new_certs_dir = $dir/certs
certificate = $dir/certs/cacert.pem
private_key = $dir/private/cakey.pem
default_days = 365
default_md = md5
preserve = no
email_in_dn = no
nameopt = default_ca
certopt = default_ca
policy = policy_match
[ policy_match ]
countryName = match
stateOrProvinceName = match
organizationName = match
organizationalUnitName = optional
commonName = supplied
emailAddress = optional
[ req ]
default_bits = 1024 # Size of keys
default_keyfile = key.pem # name of generated keys
default_md = md5 # message digest algorithm
string_mask = nombstr # permitted characters
distinguished_name = req_distinguished_name
req_extensions = v3_req
[ req_distinguished_name ]
# Variable name Prompt string
#------------------------- ----------------------------------
0.organizationName = Organization Name (company)
organizationalUnitName = Organizational Unit Name (department, division)
emailAddress = Email Address
emailAddress_max = 40
localityName = Locality Name (city, district)
stateOrProvinceName = State or Province Name (full name)
countryName = Country Name (2 letter code)
countryName_min = 2
countryName_max = 2
commonName = Common Name (hostname, IP, or your name)
commonName_max = 64
# Default values for the above, for consistency and less typing.
# Variable name Value
#------------------------ ------------------------------
0.organizationName_default = My Organization
localityName_default = NEW YORK
stateOrProvinceName_default = NEW YORK
countryName_default = US
emailAddress_default = email@mydomain.net
[ v3_ca ]
basicConstraints = CA:TRUE
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid:always,issuer:always
[ v3_req ]
basicConstraints = CA:FALSE
subjectKeyIdentifier = hash

Generate CA Key and Certificate

Once you have the config file created the way you want, you will have to create a root (CA) key and certificate.

You will be prompted for some questions. Those questions for which you provided the default answers in your caconfig.cnf file, you do not need to enter any other information; you may just press enter key. However, the most important thing is this: Common Name should be unique, and should be the full legal name of your organization. If you are an individual, make sure it is your name. As you create more certificates, Common Name has to be unique for each of them.

You will also have to enter a passphrase. Make sure it is long, difficult, and then keep it safe. It would be awesome if you could encrypt it as well. But whatever you do, however you save it, do not, I repeat, do not lose it.

cd /home/ca/

openssl req -new -x509 -extensions v3_ca -keyout private/cakey.pem -out cacert.pem -days 365 -config conf/caconfig.cnf

Create Server Certificate

You first have to create a key and signing request, both of which can be done in the same command. You will be prompted for some questions again, and once again you have to make sure Common Name is unique. To create a server certificate, you should always choose the full domain name of the server, or its IP address. For example, if you are creating a certificate for www.mydomain.net, you should enter www.mydomain.net as Common Name. However, if you want to use the same certificate across many domains, enter *.mydomain.net as Common Name.

I also like to name the requests, keys, and certificates after the domain I will be usig them for. So let’s say you are choosing to create a certificate for www.mydomain.net, then

cd /home/ca/

openssl req -new -nodes -out www.mydomain.net.req.pem -keyout private/www.mydomain.net.key.pem -config conf/caconfig.cnf

Next you have to sign this request to create a certificate. It will ask you to enter the passphrase chosen when creating the root key. So just enter it.

openssl ca -out certs/www.mydomain.net.cert.pem -config conf/caconfig.cnf -infiles www.mydomain.net.req.pem

You can use this certificate in your server applications now.

Create Client Certificate

Creating a client certificate, such as for your web browser, is similar to creating one for a server. One can use client certificates to identify clients, and also to authenticate them.

For Common Name, you should choose the name of the client. If it is an individual, their full name, or if it’s an organization then its full name. For more details, check out the details under Create Server Certificate.

cd /home/ca/

openssl req -new -nodes -out myfriend.req.pem -keyout private/myfriend.key.pem -days 365 -config conf/caconfig.cnf

Similarly, you have to sign this request with the root key. Again, details are provided under Create Server Certificate.

openssl ca -out certs/myfriend.cert.pem -days 365 -config conf/openssl.cnf -infiles myfriend.req.pem

Set up Apache 2.0 with SSL

Please read my guide titled Apache 2.0 in Ubuntu for a detailed guide.

Get Certificates from CAcert

If you do not wish to pay for certificates, check out CAcert. They are aiming to become a well-recognized CA which gives free certificates. It would be better than creating your own as it will be much more secure and you would not have to pay any money.

Hat Tips

Other very good guides and resources are: Client Authentication with SSL; subjectAltName setup;

Tagged with: ,

5 Responses

Subscribe to comments with RSS.

  1. Alejo Ceballos said, on November 27, 2008 at 12:40 am

    Thank you very much, I had some hard time configuring it right, but in the end everything has gone fine. Just minor changes here and there.

    Now I’m going to read your “Apache 2.0 in Ubuntu” guide.

  2. Shaun said, on May 18, 2009 at 9:09 am

    Hi

    I worked through the above but should this line:

    openssl ca -out certs/myfriend.cert.pem -days 365 -config conf/openssl.cnf -infiles myfriend.req.pem

    Be this:

    openssl ca -out certs/myfriend.cert.pem -days 365 -config conf/caconfig.cnf -infiles myfriend.req.pem

    Regards

    Shaun

  3. [...] http://codeghar.wordpress.com/2008/03/17/create-a-certificate-authority-and-certificates-with-openss... Step Four: Creating a PEM fileA PEM file is used by many different daemons, however how to generate such a PEM file can be hard to come by. There are some complicated ways to build one, however I have had pretty good success with simply combining the .key and the .crt file together: cat domain.tld.key domain.tld.crt > domain.tld.pem DisclaimerI am not an expert with SSL, which is exactly why I created this. This may not be accurate, YMMV, etc. Be careful. Also: Your .key is private. Keep that safe, with appropriate permissions. Make sure nobody else can access it, and do not give it away to anyone. If you have any insight, feel free to comment – I would appreciate them.SourcesJust a thank-you to everyone that was kind enough to document this process. [...]

  4. Taher Elgamal said, on August 5, 2009 at 3:46 pm

    Does anyone know if the known certificate authorities with root keys in the browser have actually generated their keys securely?


Leave a Reply