<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule"
>
<channel>
	<title>G-Loaded Journal &#187; OpenSSL</title>
	<atom:link href="http://www.g-loaded.eu/tag/openssl/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.g-loaded.eu</link>
	<description>An open-source software and technology related journal</description>
	<lastBuildDate>Mon, 05 Dec 2011 19:55:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<creativeCommons:license>http://creativecommons.org/licenses/by-nc-sa/3.0/</creativeCommons:license>
		<item>
		<title>Be your own Certificate Authority (CA)</title>
		<link>http://www.g-loaded.eu/2005/11/10/be-your-own-ca/</link>
		<comments>http://www.g-loaded.eu/2005/11/10/be-your-own-ca/#comments</comments>
		<pubDate>Thu, 10 Nov 2005 13:33:40 +0000</pubDate>
		<dc:creator>George Notaras</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Administration]]></category>
		<category><![CDATA[Certificates]]></category>
		<category><![CDATA[Encryption]]></category>
		<category><![CDATA[HOWTO]]></category>
		<category><![CDATA[OpenSSL]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Servers]]></category>
		<category><![CDATA[ssl]]></category>
		<category><![CDATA[TLS]]></category>
		<guid isPermaLink="false">http://www.g-loaded.eu/2005/11/10/be-your-own-ca/</guid>
		<description><![CDATA[This article describes how to become your own Certificate Authority (CA) and issue your own server certificates. Be advised that noone else, apart from you, your internal network's people or your friends, will or should trust this kind of certificates (self-signed). These are intended only for providing secure communication with your own services or for testing purposes.]]></description>
			<content:encoded><![CDATA[<p>I declare from the beginning that I am no authority on digital <strong>certificates</strong>.<br />
This document is a summary of all the articles I have read about <strong>openssl</strong>. It describes in short how to become your own <strong>Certificate Authority</strong> (CA) and how to create and sign your own <strong>certificate requests</strong>. Make no mistake, these certificates are good only for personal use or for use in your intranet in order to provide a secure way to login or communicate with your services, so that passwords or other data is not transmitted in the clear. Noone else will or should trust these certificates.<br />
<span id="more-94"></span></p>
<h4>Prerequisites</h4>
<p>The package <strong>openssl</strong> should be installed in the machine you will use to manage your certificates or create the certificate requests.</p>
<h4>First things first&#8230;</h4>
<p>The <strong>openssl</strong> package comes with some scripts that can help you create your server certificates fast, but here I will describe how to set things up from scratch in a new directory, so that you can customize things later if you like or delete everything without touching openssl&#8217;s or the system&#8217;s default files. This article is based on a Fedora installation, but will do for all distributions.</p>
<h5>Creating the necessary directories</h5>
<p>First of all we will create a directory tree where all certificate stuff will be kept. Fedora&#8217;s default directory is <strong>/etc/pki/tls/</strong>. So, as root, we create our own directories:</p>
<pre class="console"># mkdir -m 0755 /etc/pki_jungle</pre>
<p>And then we create our CA&#8217;s directory tree:</p>
<pre class="console">
# mkdir -m 0755 \
     /etc/pki_jungle/myCA \
     /etc/pki_jungle/myCA/private \
     /etc/pki_jungle/myCA/certs \
     /etc/pki_jungle/myCA/newcerts \
     /etc/pki_jungle/myCA/crl
</pre>
<ul>
<li><strong>myCA</strong> is our Certificate Authority&#8217;s directory.</li>
<li><strong>myCA/certs</strong> directory is where our server certificates will be placed.</li>
<li><strong>myCA/newcerts</strong> directory is where openssl puts the created certificates in PEM (unencrypted) format and in the form <em>cert_serial_number.pem</em> (eg 07.pem). Openssl needs this directory, so we create it.</li>
<li><strong>myCA/crl</strong> is where our certificate revokation list is placed.</li>
<li><strong>myCA/private</strong> is the directory where our private keys are placed. Be sure that you set restrictive permissions to all your private keys so that they can be read only by root, or the user with whose priviledges a server runs. If anyone steals your private keys, then things get really bad.</li>
</ul>
<h5>Initial openssl configuration</h5>
<p>We are going to copy the default openssl configuration file (<strong>openssl.cnf</strong>) to our CA&#8217;s directory. In Fedora, this file exists in <strong>/etc/pki/tls</strong>. So, we copy it to our CA&#8217;s dir and name it <strong>openssl.my.cnf</strong>. As root:</p>
<pre class="console"># cp /etc/pki/tls/openssl.cnf /etc/pki_jungle/myCA/openssl.my.cnf</pre>
<p>This file does not need to be world readable, so we change its attributes:</p>
<pre class="console"># chmod 0600 /etc/pki_jungle/myCA/openssl.my.cnf</pre>
<p>We also need to create two other files. This file serves as a database for openssl:</p>
<pre class="console"># touch /etc/pki_jungle/myCA/index.txt</pre>
<p>The following file contains the next certificate&#8217;s serial number. Since we have not created any certificates yet, we set it to &quot;<strong>01</strong>&quot;:</p>
<pre class="console"># echo '01' > /etc/pki_jungle/myCA/serial</pre>
<h4>Things to remember</h4>
<p>Here is a small legend with <strong>file extensions</strong> we will use for the created files and their meaning. All files that will be created will have one of these extensions:</p>
<ul>
<li><strong>KEY</strong> &#8211; Private key (Restrictive permissions should be set on this)</li>
<li><strong>CSR</strong> &#8211; Certificate Request (This will be signed by our CA in order to create the server certificates. Afterwards it is not needed and can be deleted)</li>
<li><strong>CRT</strong> &#8211; Certificate (This can be publicly distributed)</li>
<li><strong>PEM</strong> &#8211; We will use this extension for files that contain both the Key and the server Certificate (Some servers need this). Permissions should be restrictive on these files.</li>
<li><strong>CRL</strong> &#8211; Certificate Revokation List (This can be publicly distributed)</li>
</ul>
<h4>Create the CA Certificate and Key</h4>
<p>Now, that all initial configuration is done, we may create a self-signed certificate, that will be used as our CA&#8217;s certificate. In other words, we will use this to sign other certificate requests.</p>
<p>Change to our CA&#8217;s directory. <span style="text-decoration:underline;">This is where we should issue all the openssl commands because here is our openssl&#8217;s configuration file (openssl.my.cnf).</span> As root:</p>
<pre class="console"># cd /etc/pki_jungle/myCA/</pre>
<p>And then create your CA&#8217;s Certificate and Private Key. As root:</p>
<pre class="console"># openssl req -config openssl.my.cnf -new -x509 -extensions v3_ca -keyout private/myca.key -out certs/myca.crt -days 1825</pre>
<p>This creates a self-signed certificate with the default CA extensions which is valid for 5 years. You will be prompted for a <strong>passphrase</strong> for your CA&#8217;s private key. <span style="text-decoration:underline;">Be sure that you set a strong passphrase.</span> Then you will need to provide some info about your CA. Fill in whatever you like. Here is an example:</p>
<pre class="codesnp">Country Name (2 letter code) [GB]:GR
State or Province Name (full name) [Berkshire]:Greece
Locality Name (eg, city) [Newbury]:Thessaloniki
Organization Name (eg, company) [My Company Ltd]:My Network
Organizational Unit Name (eg, section) []:My Certificate Authority
Common Name (eg, your name or your server's hostname) []:server.example.com
Email Address []:whatever@server.example.com</pre>
<p>Two files are created:</p>
<ul>
<li><strong>certs/myca.crt</strong> &#8211; This is your CA&#8217;s certificate and can be publicly available and of course world readable.</li>
<li><strong>private/myca.key</strong> &#8211; This is your CA&#8217;s private key. Although it is protected with a passphrase you should restrict access to it, so that only root can read it:
<pre class="console"># chmod 0400 /etc/pki_jungle/myCA/private/myca.key</pre>
</li>
</ul>
<h4>More openssl configuration (mandatory)</h4>
<p>Because we use a custom directory for our certificates&#8217; management, some modifications to <strong>/etc/pki_jungle/myCA/openssl.my.cnf</strong> are necessary. Open it in your favourite text editor as root and find the following part (around line 35):</p>
<pre class="codesnp">[ CA_default ]
dir     = ../../CA      # Where everything is kept
certs       = $dir/certs        # Where the issued certs are kept
crl_dir     = $dir/crl      # Where the issued crl are kept
database    = $dir/index.txt    # database index file.
#unique_subject = no            # Set to 'no' to allow creation of
                    # several ctificates with same subject.
new_certs_dir   = $dir/newcerts     # default place for new certs.
certificate = $dir/cacert.pem   # The CA certificate
serial      = $dir/serial       # The current serial number
#crlnumber  = $dir/crlnumber    # the current crl number must be
                    # commented out to leave a V1 CRL
crl     = $dir/crl.pem      # The current CRL
private_key = $dir/private/cakey.pem    # The private key
RANDFILE    = $dir/private/.rand    # private random number file
x509_extensions = usr_cert      # The extentions to add to the cert
</pre>
<p>You should modify the following settings in order to coform to our custom directory and our custom CA key and certificate:</p>
<pre class="codesnp">[ CA_default ]
dir     = <strong>.</strong>                # <strong>&lt;&#45;&#45;CHANGE THIS</strong>
certs       = $dir/certs
crl_dir     = $dir/crl
database    = $dir/index.txt
#unique_subject = no
new_certs_dir   = $dir/newcerts
certificate = $dir/<strong>certs/myca.crt</strong>   # <strong>&lt;&#45;&#45;CHANGE THIS</strong>
serial      = $dir/serial
#crlnumber  = $dir/crlnumber
crl     = $dir/crl.pem
private_key = $dir/private/<strong>myca.key</strong>    # <strong>&lt;&#45;&#45;CHANGE THIS</strong>
RANDFILE    = $dir/private/.rand
x509_extensions = usr_cert</pre>
<h4>Create a Server certificate</h4>
<p>Further openssl.my.cnf file&#8217;s customization is possible, so that we define our policy for certificate creation and signing or define our desired extensions for the new certificates. I may add this info to a future version of this document. It&#8217;s easy though, just try to familiarize yourself with the openssl.cnf&#8217;s structure and you&#8217;ll figure it out.</p>
<p>Anyway, the certificates we are going to create, without customizing openssl.my.cnf any further, are <strong>general purpose certificates</strong> and their usage in not restricted to server authentication only. One thing that you should take a note of is that <strong>the private keys will not be protected by a passphrase</strong>, so that when the services are restarted they do not ask for a passphrase. This means that you should <strong>set restrictive permissions on the private keys</strong>, so that only root or the user under whose priviledges a server runs can read these files.</p>
<h5>Generate a Certificate Request</h5>
<p>First, we change to our CA&#8217;s directory:</p>
<pre class="console"># cd /etc/pki_jungle/myCA/</pre>
<p>Then we create the certificate request:</p>
<pre class="console"># openssl req -config openssl.my.cnf -new -nodes -keyout private/server.key -out server.csr -days 365</pre>
<p>The <strong>-nodes</strong> option is needed so that the private key is not protected with a passphrase. If you do not intend to use the certificate for server authentication, you should not include it in the above command.<br />
You can customize the number of days you want this certificate to be valid for.</p>
<p>You will be prompted for the certificate&#8217;s info. Here is an example:</p>
<pre class="codesnp">Country Name (2 letter code) [GB]:GR
State or Province Name (full name) [Berkshire]:Greece
Locality Name (eg, city) [Newbury]:Thessaloniki
Organization Name (eg, company) [My Company Ltd]:My Network
Organizational Unit Name (eg, section) []:My Web Server
Common Name (eg, your name or your server's hostname) []:www.server.example.com
Email Address []:whatever@server.example.com</pre>
<p>The <strong>Common Name (CN)</strong> is the info that uniquely distinguishes your service, so be sure that you type it correctly.<br />
When prompted for some extra attributes (challenge password, optional company name) just hit the [Enter] key.</p>
<p>Two files are created:</p>
<ul>
<li><strong>server.csr</strong> &#8211; this is the certificate request.</li>
<li><strong>private/server.key</strong> &#8211; this is the private key, which is not protected with a passphrase.</li>
</ul>
<p>Set restrictive permissions on the private key. Only root or the user that is used to run the server should be able to read it. For example:</p>
<pre class="console"># chown root.root /etc/pki_jungle/myCA/private/server.key
# chmod 0400 /etc/pki_jungle/myCA/private/server.key</pre>
<p>Or:</p>
<pre class="console"># chown root.apache /etc/pki_jungle/myCA/private/server.key
# chmod 0440 /etc/pki_jungle/myCA/private/server.key</pre>
<h5>Sign the Certificate Request</h5>
<p>Now we are going to sign the certificate request and generate the server&#8217;s certificate.</p>
<p>First, we change to our CA&#8217;s directory:</p>
<pre class="console"># cd /etc/pki_jungle/myCA/</pre>
<p>Then we sign the certificate request:</p>
<pre class="console"># openssl ca -config openssl.my.cnf -policy policy_anything -out certs/server.crt -infiles server.csr</pre>
<p>You will need to supply the CA&#8217;s private key in order to sign the request. You can check the openssl.my.cnf file about what <strong>policy_anything</strong> means. In short, the fields about the Country, State or City is not required to match those of your CA&#8217;s certificate.</p>
<p>After all this is done two new files are created:</p>
<ul>
<li><strong>certs/server.crt</strong> &#8211; this is the server&#8217;s certificate, which can be made available publicly.</li>
<li><strong>newcerts/01.pem</strong> &#8211; This is exactly the same certificate, but with the certificate&#8217;s serial number as a filename. It is not needed.</li>
</ul>
<p>You can now delete the certificate request (server.csr). It&#8217;s no longer needed:</p>
<pre class="console"># rm -f /etc/pki_jungle/myCA/server.csr</pre>
<h5>Verify the certificate</h5>
<p>You can see the certificate&#8217;s info with the following:</p>
<pre class="console"># openssl x509 -subject -issuer -enddate -noout -in /etc/pki_jungle/myCA/certs/server.crt</pre>
<p>Or the following:</p>
<pre class="console"># openssl x509 -in certs/server.crt -noout -text</pre>
<p>And verify that the certificate is valid for server authentication with the following:</p>
<pre class="console"># openssl verify -purpose sslserver -CAfile /etc/pki_jungle/myCA/certs/myca.crt /etc/pki_jungle/myCA/certs/server.crt</pre>
<h5>Server certificate and key in one file</h5>
<p>Some servers, for example vsftpd, require that both the private key and the certificate exist in the same file. In a situation like that just do the following:</p>
<pre class="console"># cat certs/server.crt private/server.key > private/server-key-cert.pem</pre>
<p>You should restrict access to the final file and delete server.crt and server.key since thay are no longer needed.</p>
<pre class="console"># chown root.root private/server-key-cert.pem
# chmod 0400 private/server-key-cert.pem
# rm -f certs/server.crt
# rm -f private/server.key</pre>
<h4>Revoke a Server Certificate</h4>
<p>If you do not want a certificate to be valid any more, you have to revoke it. This is done with the command:</p>
<pre class="console"># openssl ca -config openssl.my.cnf -revoke certs/server.crt</pre>
<p>Then you should generate a new CRL (Certificate Revokation List):</p>
<pre class="console"># openssl ca -config openssl.my.cnf -gencrl -out crl/myca.crl</pre>
<p>The CRL file is <strong>crl/myca.crl</strong>.</p>
<h4>Distribute your certificates and CRL</h4>
<p>Your CA&#8217;s certificate and your servers&#8217; certificates should be distributed to those who trust you so they can import them in their client software (web browsers, ftp clients, email clients etc). The CRL should also be published.</p>
<h4>Further Reading</h4>
<p>As I have said from the beginning, this document is just a summary of what I have read. Here are some useful links that will get you started:</p>
<ol>
<li><a href="http://www.tldp.org/HOWTO/SSL-Certificates-HOWTO/index.html">The SSL Certificates HOWTO</a></li>
<li><a href="http://sial.org/howto/openssl/">The OpenSSL Documentation</a></li>
<li><a href="http://www.technoids.org/openssl.cnf.html">The openssl.cnf documentation</a></li>
<li><a href="http://sial.org/howto/openssl/ca/">OpenSSL Certificate Authority Setup</a></li>
</ol>
<div class="cc-block"><em><a href="http://www.g-loaded.eu/2005/11/10/be-your-own-ca/">Be your own Certificate Authority (CA)</a></em>, unless otherwise expressly stated, is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/3.0/">Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License</a>. Terms and conditions beyond the scope of this license may be available at <a href="http://www.g-loaded.eu/about/disclaimer-and-license/">www.g-loaded.eu</a>.</div>
<h4>Related Articles</h4>
<ul><li><a href="http://www.g-loaded.eu/2007/11/22/root-certificate-programs-the-root-of-all-trust/" rel="bookmark">Root Certificate Programs &#8211; The root of all trust</a></li>
<li><a href="http://www.g-loaded.eu/2007/08/10/ssl-enabled-name-based-apache-virtual-hosts-with-mod_gnutls/" rel="bookmark">SSL-enabled Name-based Apache Virtual Hosts with mod_gnutls</a></li>
<li><a href="http://www.g-loaded.eu/2009/10/12/free-personal-email-certificates-program-discontinued-by-thawte/" rel="bookmark">Free Personal Email Certificates Program discontinued by Thawte</a></li>
<li><a href="http://www.g-loaded.eu/2005/11/10/ssh-with-keys/" rel="bookmark">Setup the SSH server to use keys for authentication</a></li>
<li><a href="http://www.g-loaded.eu/2005/11/10/configure-vnc-server-in-fedora/" rel="bookmark">Set up the VNC Server in Fedora</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.g-loaded.eu/2005/11/10/be-your-own-ca/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by-nc-sa/3.0/</creativeCommons:license>
	</item>
	</channel>
</rss>

