<?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/"
	>

<channel>
	<title>logIt &#187; SSL</title>
	<atom:link href="https://lakm.us/logit/tag/ssl/feed/" rel="self" type="application/rss+xml" />
	<link>https://lakm.us/logit</link>
	<description>Log Around The Clock</description>
	<lastBuildDate>Sat, 06 Jun 2015 14:17:37 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>HTTPS Using Server-Client Certificate Pair (1): Generate &amp; Sign by OpenSSL</title>
		<link>https://lakm.us/logit/2013/01/https-server-client-certificate-pair-1-generate-openssl/</link>
		<comments>https://lakm.us/logit/2013/01/https-server-client-certificate-pair-1-generate-openssl/#comments</comments>
		<pubDate>Wed, 23 Jan 2013 04:41:30 +0000</pubDate>
		<dc:creator>Arif</dc:creator>
				<category><![CDATA[General Linux]]></category>
		<category><![CDATA[certificate]]></category>
		<category><![CDATA[https]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[SSL]]></category>

		<guid isPermaLink="false">http://lakm.us/logit/?p=451</guid>
		<description><![CDATA[Multiple command lines in the process of generating certificates using openssl can be quite confusing and easily mixed up over which-do-what. Most of them are repetitions of almost the same syntax (where the confusion comes). Background: I need to setup an HTTPS site with not just server certificate to secure it, but requiring also client [...]]]></description>
				<content:encoded><![CDATA[<p>Multiple command lines in the process of generating certificates using <code><a title="OpenSSL" href="http://www.openssl.org/" target="_blank">openssl</a></code> can be quite confusing and easily mixed up over which-do-what. Most of them are repetitions of almost the same syntax (where the confusion comes).</p>
<p>Background:<br />
I need to setup an <a title="HTTP Secure" href="http://en.wikipedia.org/wiki/HTTP_Secure" target="_blank">HTTPS</a> site with not just server certificate to secure it, but requiring also client side certificate. The site will only show the content to authorized users with the correct pair of server-client certificate. It will also expire after a certain date. The certificates are self-signed as they&#8217;re for closed environment usage.</p>
<p>This post covers two general processes: <em><strong>generating</strong></em> and <em><strong>signing</strong></em>.</p>
<p>How to <em><strong>generate</strong></em> SSL certificate using <code>openssl</code> is a straightforward process of:</p>
<ol>
<li>generate its key</li>
<li>create certificate request with that key</li>
<li>generate certificate from request and key</li>
</ol>
<p>Hence, in any type of the certificate I have a general <code>&lt;some-cert-key&gt;.key</code>, <code>&lt;some-cert-request&gt;.csr</code>, and <code>&lt;some-cert&gt;.crt</code>. When I mean &#8220;type&#8221;, they are <a href="http://en.wikipedia.org/wiki/Certificate_authority" title="Certificate Authority" target="_blank">CA (Certificate Authority)</a>, one/more server certificate, and one/more client certificate.</p>
<div class="wp-caption aligncenter" style="width: 460px"><img title="Generating Pair of Key-Certificate with openSSL" src="../../../../images/ssl-certificate-ca-server-client-illustration-1.png" alt="ssl-certificate-ca-server-client-illustration-1.png" width="286" height="226" /><p class="wp-caption-text">Generating Pairs of Key-Certificate with openSSL: CA, server, &amp; client</p></div>
<p>In terms of <strong>signing</strong> the certificates, all of them are signed using the CA. Which files to be used in the server will become the subject of the next post.</p>
<p><span id="more-451"></span><code>openssl</code> will run interactively. To go through all the recurring questions using prepared default answers, we need to create a config file first. I created <code>caconfig.cnf</code> (find it on the bottom of the post) and use <code>-config caconfig.cnf</code> option in some commands.</p>
<p>First, prepare set of directories to clearly separate what we&#8217;re working on (keys, requests, and resulting certificates in different places for server and user):</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">mkdir</span> certs
<span style="color: #c20cb9; font-weight: bold;">mkdir</span> private
<span style="color: #c20cb9; font-weight: bold;">mkdir</span> server
<span style="color: #c20cb9; font-weight: bold;">mkdir</span> server<span style="color: #000000; font-weight: bold;">/</span>certs
<span style="color: #c20cb9; font-weight: bold;">mkdir</span> server<span style="color: #000000; font-weight: bold;">/</span>creqs
<span style="color: #c20cb9; font-weight: bold;">mkdir</span> server<span style="color: #000000; font-weight: bold;">/</span>ckeys
<span style="color: #c20cb9; font-weight: bold;">mkdir</span> user
<span style="color: #c20cb9; font-weight: bold;">mkdir</span> user<span style="color: #000000; font-weight: bold;">/</span>certs
<span style="color: #c20cb9; font-weight: bold;">mkdir</span> user<span style="color: #000000; font-weight: bold;">/</span>creqs
<span style="color: #c20cb9; font-weight: bold;">mkdir</span> user<span style="color: #000000; font-weight: bold;">/</span>ckeys</pre></div></div>

<p>Prepare &#8220;database&#8221; and index number to keep track of certificates issued:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">echo</span> 01 <span style="color: #000000; font-weight: bold;">&gt;</span> serial
<span style="color: #c20cb9; font-weight: bold;">touch</span> index.txt</pre></div></div>

<p>Generate the CA: (again) generate key, create request (the interactive part), and create certificate:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">openssl genrsa <span style="color: #660033;">-out</span> private<span style="color: #000000; font-weight: bold;">/</span>myCA.key <span style="color: #000000;">2048</span>
openssl req <span style="color: #660033;">-new</span> <span style="color: #660033;">-key</span> private<span style="color: #000000; font-weight: bold;">/</span>myCA.key <span style="color: #660033;">-out</span> certs<span style="color: #000000; font-weight: bold;">/</span>myCA.csr <span style="color: #660033;">-config</span> caconfig.cnf 
openssl req <span style="color: #660033;">-x509</span> <span style="color: #660033;">-days</span> <span style="color: #000000;">365</span> <span style="color: #660033;">-in</span> certs<span style="color: #000000; font-weight: bold;">/</span>myCA.csr <span style="color: #660033;">-out</span> certs<span style="color: #000000; font-weight: bold;">/</span>myCA.crt <span style="color: #660033;">-key</span> private<span style="color: #000000; font-weight: bold;">/</span>myCA.key</pre></div></div>

<p>We can always check:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">openssl x509 <span style="color: #660033;">-in</span> certs<span style="color: #000000; font-weight: bold;">/</span>myCA.crt <span style="color: #660033;">-text</span></pre></div></div>

<p>Now, <em><strong>generating</strong></em> for the <strong>server</strong>, I use the name <code>lakmus</code> as an example. First the key:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">openssl genrsa <span style="color: #660033;">-des3</span> <span style="color: #660033;">-out</span> server<span style="color: #000000; font-weight: bold;">/</span>ckeys<span style="color: #000000; font-weight: bold;">/</span>lakmus.key <span style="color: #000000;">2048</span></pre></div></div>

<p>(<a href="http://en.wikipedia.org/wiki/Triple_DES" title="Triple DES" target="_blank">Triple-DES</a> cipher will ask for pass phrase of 4 characters minimum)</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">Enter pass phrase <span style="color: #000000; font-weight: bold;">for</span> server<span style="color: #000000; font-weight: bold;">/</span>ckeys<span style="color: #000000; font-weight: bold;">/</span>lakmus.key:
Verifying - Enter pass phrase <span style="color: #000000; font-weight: bold;">for</span> server<span style="color: #000000; font-weight: bold;">/</span>ckeys<span style="color: #000000; font-weight: bold;">/</span>lakmus.key:</pre></div></div>

<p>Then, the request (which will ask for the above key pass phrase):</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">openssl req <span style="color: #660033;">-new</span> <span style="color: #660033;">-key</span> server<span style="color: #000000; font-weight: bold;">/</span>ckeys<span style="color: #000000; font-weight: bold;">/</span>lakmus.key <span style="color: #660033;">-out</span> server<span style="color: #000000; font-weight: bold;">/</span>creqs<span style="color: #000000; font-weight: bold;">/</span>lakmus.csr <span style="color: #660033;">-config</span> caconfig.cnf</pre></div></div>

<p>Note that <code>organizationName</code> field needs to be the same with the CA certificate.</p>
<p>Then, <em><strong>signing</strong></em> it with the CA (and again check as text):</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">openssl ca <span style="color: #660033;">-days</span> <span style="color: #000000;">365</span> <span style="color: #660033;">-in</span> server<span style="color: #000000; font-weight: bold;">/</span>creqs<span style="color: #000000; font-weight: bold;">/</span>lakmus.csr <span style="color: #660033;">-cert</span> certs<span style="color: #000000; font-weight: bold;">/</span>myCA.crt <span style="color: #660033;">-out</span> server<span style="color: #000000; font-weight: bold;">/</span>certs<span style="color: #000000; font-weight: bold;">/</span>lakmus.crt <span style="color: #660033;">-keyfile</span> private<span style="color: #000000; font-weight: bold;">/</span>myCA.key <span style="color: #660033;">-config</span> caconfig.cnf
&nbsp;
openssl x509 <span style="color: #660033;">-in</span> server<span style="color: #000000; font-weight: bold;">/</span>certs<span style="color: #000000; font-weight: bold;">/</span>lakmus.crt <span style="color: #660033;">-text</span></pre></div></div>

<p>During <em><strong>signing</strong></em> we&#8217;ll see something like (expiry date and validity periode)</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">Certificate is to be certified <span style="color: #000000; font-weight: bold;">until</span> <span style="color: #000000; font-weight: bold;">&lt;</span>some <span style="color: #c20cb9; font-weight: bold;">date</span><span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">365</span> days<span style="color: #7a0874; font-weight: bold;">&#41;</span>
Sign the certificate? <span style="color: #7a0874; font-weight: bold;">&#91;</span>y<span style="color: #000000; font-weight: bold;">/</span>n<span style="color: #7a0874; font-weight: bold;">&#93;</span>:</pre></div></div>

<p>and <code>index.txt</code> is updated:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">V	140123034822Z		01	unknown	<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">C</span>=ID<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">ST</span>=WEST JAVA<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">O</span>=My Organization<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">CN</span>=lakm.us
V	140123041441Z		02	unknown	<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">C</span>=ID<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">ST</span>=WEST JAVA<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">O</span>=My Organization<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">CN</span>=client</pre></div></div>

<p>Finally, the same <em><strong>generate</strong></em> with own key and <em><strong>sign</strong></em> with CA except this time is for <strong>client</strong>:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">openssl genrsa <span style="color: #660033;">-des3</span> <span style="color: #660033;">-out</span> user<span style="color: #000000; font-weight: bold;">/</span>ckeys<span style="color: #000000; font-weight: bold;">/</span>client1.key <span style="color: #000000;">2048</span>
openssl req <span style="color: #660033;">-new</span> <span style="color: #660033;">-key</span> user<span style="color: #000000; font-weight: bold;">/</span>ckeys<span style="color: #000000; font-weight: bold;">/</span>client1.key <span style="color: #660033;">-out</span> user<span style="color: #000000; font-weight: bold;">/</span>creqs<span style="color: #000000; font-weight: bold;">/</span>client1.csr <span style="color: #660033;">-config</span> caconfig.cnf
&nbsp;
openssl ca <span style="color: #660033;">-in</span> user<span style="color: #000000; font-weight: bold;">/</span>creqs<span style="color: #000000; font-weight: bold;">/</span>client1.csr <span style="color: #660033;">-cert</span> certs<span style="color: #000000; font-weight: bold;">/</span>myCA.crt <span style="color: #660033;">-keyfile</span> private<span style="color: #000000; font-weight: bold;">/</span>myCA.key <span style="color: #660033;">-out</span> user<span style="color: #000000; font-weight: bold;">/</span>certs<span style="color: #000000; font-weight: bold;">/</span>client1.crt <span style="color: #660033;">-config</span> caconfig.cnf 
&nbsp;
openssl x509 <span style="color: #660033;">-in</span> user<span style="color: #000000; font-weight: bold;">/</span>certs<span style="color: #000000; font-weight: bold;">/</span>client1.crt <span style="color: #660033;">-text</span></pre></div></div>

<p>For the client certificate to be usable when importing to browser, convert it to <a href="http://en.wikipedia.org/wiki/PKCS_12" title="PKCS 12" target="_blank">PKCS 12</a></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">openssl pkcs12 <span style="color: #660033;">-export</span> <span style="color: #660033;">-clcerts</span> <span style="color: #660033;">-in</span> user<span style="color: #000000; font-weight: bold;">/</span>certs<span style="color: #000000; font-weight: bold;">/</span>client1.crt <span style="color: #660033;">-inkey</span> user<span style="color: #000000; font-weight: bold;">/</span>ckeys<span style="color: #000000; font-weight: bold;">/</span>client1.key <span style="color: #660033;">-out</span> user<span style="color: #000000; font-weight: bold;">/</span>certs<span style="color: #000000; font-weight: bold;">/</span>client1.p12</pre></div></div>

<p>It will ask for pass phrase and export password (that will be prompted when importing to browser)</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">Enter pass phrase <span style="color: #000000; font-weight: bold;">for</span> user<span style="color: #000000; font-weight: bold;">/</span>ckeys<span style="color: #000000; font-weight: bold;">/</span>client1.key:
Enter Export Password:
Verifying - Enter Export Password:</pre></div></div>

<p><a id="Troubleshooting" href="Troubleshooting" rel="bookmark" title="Troubleshooting"><br />
<h4>Troubleshooting</h4>
<p></a></p>
<p>If the CA generation already worked smoothly, it is better to remove the key, request, and certificate files of subsequent signing process before repeating them when any error is found. Otherwise it will finally show error such as:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">failed to update database
TXT_DB error number <span style="color: #000000;">2</span></pre></div></div>

<p>Or something like</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">No certificate matches private key</pre></div></div>

<p>when exporting to PKCS 12.</p>
<p>We can check the <code>index.txt</code>, <code>index.old</code>, <code>serial</code>, and <code>serial.old</code> to figure our situation by evaluating indexes of signed certificate.</p>
<p><a id="CAConfig" href="#CAConfig" rel="bookmark" title="CAConfig"><br />
<h4>CA Config</h4>
<p></a></p>
<p>I used parts from <a href="http://codeghar.wordpress.com/2008/03/17/create-a-certificate-authority-and-certificates-with-openssl/" title="Create a Certificate Authority and Certificates with OpenSSL" target="_blank">Code Ghar post</a> for my <code>caconfig.cnf</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span> ca <span style="color: #7a0874; font-weight: bold;">&#93;</span>
default_ca = CA_default
<span style="color: #7a0874; font-weight: bold;">&#91;</span> CA_default <span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #c20cb9; font-weight: bold;">dir</span> = <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>arif<span style="color: #000000; font-weight: bold;">/</span>ssl
serial = <span style="color: #007800;">$dir</span><span style="color: #000000; font-weight: bold;">/</span>serial
database = <span style="color: #007800;">$dir</span><span style="color: #000000; font-weight: bold;">/</span>index.txt
new_certs_dir = <span style="color: #007800;">$dir</span><span style="color: #000000; font-weight: bold;">/</span>certs
certificate = <span style="color: #007800;">$dir</span><span style="color: #000000; font-weight: bold;">/</span>certs<span style="color: #000000; font-weight: bold;">/</span>myCAcert.crt
private_key = <span style="color: #007800;">$dir</span><span style="color: #000000; font-weight: bold;">/</span>private<span style="color: #000000; font-weight: bold;">/</span>myCA.key
default_days = <span style="color: #000000;">365</span>
default_md = md5
preserve = no
email_in_dn = no
nameopt = default_ca
certopt = default_ca
policy = policy_match
<span style="color: #7a0874; font-weight: bold;">&#91;</span> policy_match <span style="color: #7a0874; font-weight: bold;">&#93;</span>
countryName = match
stateOrProvinceName = match
organizationName = match
organizationalUnitName = optional
commonName = supplied
emailAddress = optional
<span style="color: #7a0874; font-weight: bold;">&#91;</span> req <span style="color: #7a0874; font-weight: bold;">&#93;</span>
default_bits = <span style="color: #000000;">2048</span> <span style="color: #666666; font-style: italic;"># Size of keys</span>
default_keyfile = key.pem <span style="color: #666666; font-style: italic;"># name of generated keys</span>
default_md = md5 <span style="color: #666666; font-style: italic;"># message digest algorithm</span>
string_mask = nombstr <span style="color: #666666; font-style: italic;"># permitted characters</span>
distinguished_name = req_distinguished_name
<span style="color: #7a0874; font-weight: bold;">&#91;</span> req_distinguished_name <span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #666666; font-style: italic;"># Variable name Prompt string</span>
<span style="color: #666666; font-style: italic;">#------------------------- ----------------------------------</span>
0.organizationName = Organization Name <span style="color: #7a0874; font-weight: bold;">&#40;</span>company<span style="color: #7a0874; font-weight: bold;">&#41;</span>
organizationalUnitName = Organizational Unit Name <span style="color: #7a0874; font-weight: bold;">&#40;</span>department, division<span style="color: #7a0874; font-weight: bold;">&#41;</span>
emailAddress = Email Address
emailAddress_max = <span style="color: #000000;">40</span>
localityName = Locality Name <span style="color: #7a0874; font-weight: bold;">&#40;</span>city, district<span style="color: #7a0874; font-weight: bold;">&#41;</span>
stateOrProvinceName = State or Province Name <span style="color: #7a0874; font-weight: bold;">&#40;</span>full name<span style="color: #7a0874; font-weight: bold;">&#41;</span>
countryName = Country Name <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">2</span> letter code<span style="color: #7a0874; font-weight: bold;">&#41;</span>
countryName_min = <span style="color: #000000;">2</span>
countryName_max = <span style="color: #000000;">2</span>
commonName = Common Name <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">hostname</span>, IP, or your name<span style="color: #7a0874; font-weight: bold;">&#41;</span>
commonName_max = <span style="color: #000000;">64</span>
<span style="color: #666666; font-style: italic;"># Default values for the above, for consistency and less typing.</span>
<span style="color: #666666; font-style: italic;"># Variable name Value</span>
<span style="color: #666666; font-style: italic;">#------------------------ ------------------------------</span>
0.organizationName_default = My Organization
localityName_default = BOGOR
stateOrProvinceName_default = WEST JAVA
countryName_default = ID
emailAddress_default = fake<span style="color: #000000; font-weight: bold;">@</span>lakm.us
commonName_default = lakm.us</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>https://lakm.us/logit/2013/01/https-server-client-certificate-pair-1-generate-openssl/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
