Generate a Self Signed Certificate

!
Warning: This post is over 365 days old. The information may be out of date.

In this short post, i will explain how to create a self signed certificate (on a Unix system with OpenSSL/LibreSSL)

  • Generate a server key
$ openssl genrsa -out server.key 2048
Generating RSA private key, 2048 bit long modulus
..........................................................+++
.....+++
e is 65537 (0x010001)

Hint : 2048 is the size of the key. You can increase it but NOT lower it. This will generate the file server.key

  • Create a certificate signing request (csr)
$ openssl req -new -key server.key -out server.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
+++--
Country Name (2 letter code) [AU]:FR
State or Province Name (full name) [Some-State]:PACA
Locality Name (eg, city) []:Marseille
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Raveland
Organizational Unit Name (eg, section) []:SSL
Common Name (e.g. server FQDN or YOUR name) []:test.raveland.org
Email Address []:pea@raveland.org

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:

You will have to answer the questions and it will generate a file called server.csr

  • Sign the CSR
$ openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
Signature ok
subject=C = FR, ST = PACA, L = Marseille, O = Raveland, OU = SSL, CN = test.raveland.org, emailAddress = pea@raveland.org

Hint : 365 is the validity time for your certificate. I usually put 365 (1 year) or 3650 (10 years)

Now you will have a file called server.crt (your certificate)


To resume, now you have 3 files :

  • server.key : the private key of your certificate
  • server.csr : the CSR (certificate Signing Request)
  • server.crt : your certificate