<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Wojno: Tag ssl</title>
    <link>http://christopher.wojno.com/articles/tag/ssl</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Exploration through Code</description>
    <item>
      <title>Creating your own Apache SSL Certificate Signed by your Root CA</title>
      <description>&lt;p&gt;This article is part 2 of &lt;a href="http://christopher.wojno.com/articles/2008/11/04/going-ssl-with-your-own-root-ca"&gt;Going &lt;span class="caps"&gt;SSL&lt;/span&gt; with Your Own Root CA&lt;/a&gt;&lt;/p&gt;


	&lt;h1&gt;Putting your Root CA to work&lt;/h1&gt;


	&lt;p&gt;Now that you have your own Root CA, it doesn&amp;#8217;t do anyone any good unless you use it. You use it by creating certificates for services. The rule is one certificate per-IP address. If you need additional certificates, you should have an IP address for each certificate. Any domain (or sub-domain, etc.) listed in the Certificate&amp;#8217;s CN (Common Name) should resolve to its own unique IP address. Now, nothing is set in stone. But that&amp;#8217;s the way it should be done. I&amp;#8217;ve used multiple certificates for different domains on the same server using Virtual Hosts (same IP) before, but Apache warned me every time I started the server that this is bad practice. If you&amp;#8217;re using Apache, it will still work, but you&amp;#8217;ve been warned (again).&lt;/p&gt;


	&lt;h2&gt;Disclaimers&lt;/h2&gt;


	&lt;p&gt;This guide is for amateur, non-production use only. I make no warranties or guarantees as to the correctness of this document. This guide involves escalating privileges and may cause irreparable damage if used improperly. You use this document at your own risk and I hereby disclaim any responsibility or liability for your actions or non-actions.&lt;/p&gt;


	&lt;h2&gt;Assumptions&lt;/h2&gt;


	&lt;p&gt;I&amp;#8217;m assuming you&amp;#8217;re using Apache 2.X with mod_ssl already installed. I&amp;#8217;m also assuming you&amp;#8217;ve already set-up a vhost file with the site you want to &lt;span class="caps"&gt;SSL&lt;/span&gt; already created and configured minus the specification of the server certificate, and trust chain. I realize this is a lot to assume. &lt;a href="http://tud.at/programm/apache-ssl-win32-howto.php3"&gt;This&lt;/a&gt; can help you setup an Apache server with &lt;span class="caps"&gt;SSL&lt;/span&gt;.&lt;/p&gt;


	&lt;h1&gt;Creating a Server Key&lt;/h1&gt;


	&lt;p&gt;Just as with the Root CA key, you need a server key. Now, if you already have one, relax, skip this step (though I still urge you to create and migrate to the &amp;#8220;secure&amp;#8221; environment created in the next step). You can re-use your server key as many times as you like until you want to upgrade the private key quality (or the key is compromised). If you have &lt;em&gt;not&lt;/em&gt; created a server key, you&amp;#8217;ll need to do so now.&lt;/p&gt;


	&lt;p&gt;We&amp;#8217;ll use OpenSSL again, but first, let&amp;#8217;s create a safe place for our key. The key needs to be readable by the server upon start-up (meaning, the key cannot be encrypted) or Apache (or any other server) will be unable to use it without first asking you for a password. If you don&amp;#8217;t mind typing it in each time, that&amp;#8217;s your prerogative. However, for live or even development environments, it&amp;#8217;s simply impractical.&lt;/p&gt;


	&lt;p&gt;Just as with the Root CA key, you must keep this server key confidential as well. If it is breached, anyone can masquerade as your web server; thus defeating the purpose of the certificate. If you need to move the key, you are advised to use OpenSSL to encrypt it first, however, that is a tutorial for another day.&lt;/p&gt;


	&lt;h2&gt;Create the &amp;#8220;secure&amp;#8221; environment&lt;/h2&gt;


	&lt;p&gt;Create a comfy place for your key. I like to put it in /etc/apache2/ssl with Gentoo, but the better place is likely /usr/local/etc/apache2/ssl and if you use Windows: there are no rules for file placement. With that: create your directory:&lt;/p&gt;


&lt;pre&gt;sudo mkdir /usr/local/etc/apache2
sudo mkdir /usr/local/etc/apache2/ssl
sudo chmod 0770 /usr/local/etc/apache2/ssl
sudo groupadd apache_ssl_admins
sudo chown :apache_ssl_admins /usr/local/etc/apache2/ssl
sudo usermod -a -G apache_ssl_admins YOURUSERNAME # varies by OS&lt;/pre&gt;

	&lt;p&gt;Here&amp;#8217;s what I&amp;#8217;ve done:&lt;/p&gt;


	&lt;ol&gt;
	&lt;li&gt;Created an apache2 directory (if it didn&amp;#8217;t exist, if this call fails, just make sure the directory is there)&lt;/li&gt;
		&lt;li&gt;Created the &lt;span class="caps"&gt;SSL&lt;/span&gt; directory where we&amp;#8217;ll store the server key&lt;/li&gt;
		&lt;li&gt;Changed the permissions for the directory such that only root can access it&lt;/li&gt;
		&lt;li&gt;Created a group called &amp;#8220;apache_ssl_admins&amp;#8221; &lt;/li&gt;
		&lt;li&gt;Changed the ownership of the new directory to allow members of the apache_ssl_admins access&lt;/li&gt;
		&lt;li&gt;Added you to the new group called &amp;#8220;apache_ssl_admins&amp;#8221;, note that this step may vary among Operating Systems so modify this command as required.&lt;/li&gt;
	&lt;/ol&gt;


	&lt;p&gt;You&amp;#8217;ll need to log out and log back into your server for the group membership to take effect.&lt;/p&gt;


	&lt;p&gt;Now, I&amp;#8217;ve put the word &amp;#8220;secure&amp;#8221; in quotes as if anyone were to compromise the server and gain access to the apache_ssl_admins group, your key is then accessible. Your key is only as secure as your server.&lt;/p&gt;


	&lt;p&gt;Once you&amp;#8217;ve logged back in, cd to the newly created &amp;#8220;secure&amp;#8221; environment.&lt;/p&gt;


&lt;pre&gt;cd /usr/local/etc/apache2/ssl&lt;/pre&gt;

	&lt;h1&gt;Creating the server key&lt;/h1&gt;


	&lt;p&gt;You will now generate a server key. I highly recommend that you study the &lt;a href="http://www.openssl.org/docs/apps/openssl.html"&gt;documentation&lt;/a&gt; for OpenSSL. It&amp;#8217;s very very rough, but it will help you understand the commands you&amp;#8217;re entering. It may be necessary to update these commands as machines become faster and the encryption level becomes insufficient.&lt;/p&gt;


&lt;pre&gt;openssl genrsa -out www.pem 2048&lt;/pre&gt;

	&lt;p&gt;This generates a new file called www.pem in your &amp;#8220;secure&amp;#8221; environment. This is an &lt;span class="caps"&gt;RSA&lt;/span&gt; private key with a bit length of 2048. This bit-length is considered to be good by today&amp;#8217;s standards. You may use any power of two; though, be warned, larger numbers require more time to initiate &lt;span class="caps"&gt;SSL&lt;/span&gt; connections. Smaller numbers will result in a less secure &lt;span class="caps"&gt;SSL&lt;/span&gt; handshake (and vicariously a less secure &lt;span class="caps"&gt;SSL&lt;/span&gt; session).&lt;/p&gt;


	&lt;p&gt;Again, it is a good idea to encrypt this key with 3des. I&amp;#8217;ve not done so to facilitate this demonstration. Please see the previous tutorial for instructions about encrypting your server&amp;#8217;s private key. The instructions for encrypting your Root CA private key are applicable to this purpose.&lt;/p&gt;


	&lt;h1&gt;Generate a Certificate Signing Request&lt;/h1&gt;


	&lt;p&gt;The Certificate Signing Request (CSR) is used by Root CA&amp;#8217;s to create certificates for people who need them. &lt;span class="caps"&gt;A CSR&lt;/span&gt; is a uniform way to telling Root CA&amp;#8217;s exactly what the certificate should say. The Root CA merely signs the &lt;span class="caps"&gt;CSR&lt;/span&gt; and viola: a certificate. Well, it&amp;#8217;s not all that simple. The Root CA also adds an expiration date to the signature or other fields deemed necessary or desired. But before it can be signed, the &lt;span class="caps"&gt;CSR&lt;/span&gt; must be generated by using the server&amp;#8217;s key:&lt;/p&gt;


&lt;pre&gt;openssl req -new -sha1 -out www.csr -key www.pem&lt;/pre&gt;

	&lt;p&gt;This command will create a file called www.csr in your &amp;#8220;secure&amp;#8221; environment. This file is a Certificate Signing Request. You may expose this file publicly, though that is not required if you&amp;#8217;re signing it yourself. This new request requires that it be identified using the &lt;span class="caps"&gt;SHA1&lt;/span&gt; hashing algorithm. This algorithm is safer than &lt;span class="caps"&gt;MD5&lt;/span&gt; as it has been proven that &lt;span class="caps"&gt;MD5&lt;/span&gt; collisions can be generated in a reasonable time.&lt;/p&gt;


	&lt;p&gt;You will be asked a series of questions after running this command. Answer them honestly. When you see the CN (Common Name), enter the domain name and subdomain for your certificate. I.E. for my domain: christopher.wojno.com, use as the CN: &amp;#8220;christopher.wojno.com&amp;#8221;&lt;/p&gt;


	&lt;h1&gt;Sign your &lt;span class="caps"&gt;CSR&lt;/span&gt; and create a real server Certificate&lt;/h1&gt;


	&lt;p&gt;Now that you have a &lt;span class="caps"&gt;CSR&lt;/span&gt;, we&amp;#8217;ll use our Root CA certificate and key to sign the &lt;span class="caps"&gt;CSR&lt;/span&gt;. This creates a certificate you can use in your Apache server (or other &lt;span class="caps"&gt;SSL&lt;/span&gt;-capable web server). If you created your Root CA using my previous article, then you may use the paths I have specified here. Otherwise, you&amp;#8217;ll need to adjust the -CA, -CAkey and -CAserial parameters to match what you have used.&lt;/p&gt;


&lt;pre style="overflow:scroll;"&gt;openssl x509 -req -days 365 -in www.csr -CA /usr/local/etc/certificate_authority/certs/cacert.crt -CAkey /usr/local/etc/certificate_authority/private/cakey.pem -CAserial /usr/local/etc/certificate_authority/serial.srl -out www.crt&lt;/pre&gt;

	&lt;p&gt;This is a lot to swallow in one step. This used to be where I became completely lost with regard to &lt;span class="caps"&gt;SSL&lt;/span&gt; Certificate generation. The command is very complex so I&amp;#8217;ll break it down part by part:&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;We&amp;#8217;re asking OpenSSL to do something&lt;/li&gt;
		&lt;li&gt;Specifically, we&amp;#8217;re asking it to invoke it&amp;#8217;s x509 library (SSL certificate chains)&lt;/li&gt;
		&lt;li&gt;We&amp;#8217;re then specifying that our input will be a &lt;span class="caps"&gt;CSR&lt;/span&gt; (-req)&lt;/li&gt;
		&lt;li&gt;We want the resulting certificate to be valid for 365 days (-days 365) from date of signing&lt;/li&gt;
		&lt;li&gt;The &lt;span class="caps"&gt;CSR&lt;/span&gt; is the input (-in www.csr)&lt;/li&gt;
		&lt;li&gt;We&amp;#8217;re using the CA public certificate created in the previous tutorial: (-CA /usr/local/etc/certificate_authority/certs/cacert.crt)&lt;/li&gt;
		&lt;li&gt;We&amp;#8217;re using the CA&amp;#8217;s private key created in the previous tutorial to sign this &lt;span class="caps"&gt;CSR&lt;/span&gt;: (-CAkey /usr/local/etc/certificate_authority/private/cakey.pem)&lt;/li&gt;
		&lt;li&gt;We&amp;#8217;re using the CA&amp;#8217;s serial file to mark the resulting certificate: (-CAserial /usr/local/etc/certificate_authority/serial.srl)&lt;/li&gt;
		&lt;li&gt;Finally, we&amp;#8217;re outputting a new certificate for the web server: (-out ../www.crt)&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;That little command does quite a bit. After running it, you will be prompted for the CA Root private key password (if you created one as I recommended that you do). It should automatically increment the serial file (serial.srl) so that future certificates do not have the same serial number used to identify certificates.&lt;/p&gt;


	&lt;p&gt;Now we have an &lt;span class="caps"&gt;SSL&lt;/span&gt; certificate for your server located in /usr/local/etc/apache2. Time to tell Apache where it is.&lt;/p&gt;


	&lt;h1&gt;Telling Apache about our certificate&lt;/h1&gt;


	&lt;p&gt;This next step takes place in the Virtual Host configuration file (or in your httpd.conf if your OS distro has not broken that file up yet). The true location of this next step can only be described in a universal fashion by describing the purpose: website configuration. Locate that file. In Gentoo, it&amp;#8217;s located at: /etc/apache2/vhosts.d/00_default_ssl_vhost.conf I am unsure as to its location on other operating systems.&lt;/p&gt;


	&lt;p&gt;Once you&amp;#8217;ve located this file, you&amp;#8217;ll need to edit it (this may require privilege escalation). Edit this file in your favorite editor (or least favorite, there is no favorite requirement).&lt;/p&gt;


	&lt;p&gt;Add or amend the following lines in the VirtualHost section of the website you wish to secure using the new &lt;span class="caps"&gt;SSL&lt;/span&gt; Certificate. The hostname of this virtual host must match the CN name you specified when you created the &lt;span class="caps"&gt;CSR&lt;/span&gt; for this site.&lt;/p&gt;


&lt;pre&gt;SSLCertificateFile /usr/local/etc/apache2/ssl/www.crt
SSLCertificateKeyFile /usr/local/etc/apache2/ssl/www.pem
SSLCertificateChainFile /usr/local/certificate_authority/certs/cacert.crt&lt;/pre&gt;

	&lt;p&gt;This tells Apache:&lt;/p&gt;


	&lt;ol&gt;
	&lt;li&gt;Where our certificate can be accessed so that the server may present it to requesters&lt;/li&gt;
		&lt;li&gt;Where the server&amp;#8217;s private key file is so that the certificate may be used to decrypt encrypted requests from requesters (these requests are encrypted using the certificate in step 1)&lt;/li&gt;
		&lt;li&gt;Where the certificate authority&amp;#8217;s certificate is located so that Apache is able to append it to the certificate presented in step 1 (to avoid having the user&amp;#8217;s browser look it up first)&lt;/li&gt;
	&lt;/ol&gt;


	&lt;p&gt;Again, I&amp;#8217;m assuming that you&amp;#8217;ve already set up Apache to use &lt;span class="caps"&gt;SSL&lt;/span&gt;:&lt;/p&gt;


	&lt;ol&gt;
	&lt;li&gt;You&amp;#8217;ve installed mod_ssl&lt;/li&gt;
		&lt;li&gt;You have enabled it in the /etc/conf.d/apache2 file&lt;/li&gt;
		&lt;li&gt;You&amp;#8217;ve allowed access to port 443 through all firewalls (or other port you wish to use for &lt;span class="caps"&gt;SSL&lt;/span&gt;)&lt;/li&gt;
		&lt;li&gt;You&amp;#8217;ve configured your Apache instance to listen on the &lt;span class="caps"&gt;SSL&lt;/span&gt; port&lt;/li&gt;
	&lt;/ol&gt;


	&lt;p&gt;If you&amp;#8217;ve not done so, Apache will not understand the &lt;span class="caps"&gt;SSL&lt;/span&gt; commands and will not start.&lt;/p&gt;


	&lt;p&gt;If all goes well, when you access the website at the CN name on your certificate and use the https protocol (i.e. https://CN where CN is the name used when you were asked for the Common Name when generating the &lt;span class="caps"&gt;CSR&lt;/span&gt;), you will see your website and will not be prompted to accept the authenticity of the certificate. Again, I assume you&amp;#8217;ve completed the previous tutorial and have installed your own Root CA (used to sign this certificate) into your machine&amp;#8217;s trusted Root certificates.&lt;/p&gt;


	&lt;h1&gt;Should something go wrong&lt;/h1&gt;


	&lt;p&gt;Debugging OpenSSL certificate problems is tricky and complicated. OpenSSL provides a tool for doing so over the network. I used it extensively during my first attempts at creating my certificates. Use:&lt;/p&gt;


&lt;pre&gt;openssl s_client -connect CN:PORT -debug&lt;/pre&gt;

	&lt;p&gt;See the &lt;a href="http://www.openssl.org/docs/apps/s_client.html"&gt;documentation&lt;/a&gt;# for more information. Be warned, this is a developer tool and comes with few instructions and brief explanations.&lt;/p&gt;</description>
      <pubDate>Sat, 08 Nov 2008 15:17:00 -0800</pubDate>
      <guid isPermaLink="false">urn:uuid:82d8d7ac-85b3-4b26-8f9c-8143e6478025</guid>
      <author>Christopher Wojno</author>
      <link>http://christopher.wojno.com/articles/2008/11/08/creating-your-own-apache-ssl-certificate-signed-by-your-root-ca</link>
      <category>How-Tos</category>
      <category>Security</category>
      <category>ssl</category>
      <category>apache2</category>
      <category>ca</category>
      <category>root</category>
      <category>certificate</category>
      <category>tls</category>
      <category>self</category>
      <category>web</category>
      <category>signing</category>
      <category>request</category>
      <category>csr</category>
    </item>
    <item>
      <title>Going SSL with your own Root CA</title>
      <description>&lt;h1&gt;The Prompt&lt;/h1&gt;


	&lt;p&gt;Recently, my slew of &lt;span class="caps"&gt;SSL&lt;/span&gt; certificates expired for my site and e-mail. Like most people, I didn&amp;#8217;t write anything down when I set it up. After all, I was just trying to explore what I could do with my server at that point. So, I decided to make my own Root CA for my site&amp;#8217;s certificates. That way, my family and I can use my root certificate to verify the authenticity of my servers and services.&lt;/p&gt;


	&lt;h1&gt;What are you talking about?&lt;/h1&gt;


	&lt;p&gt;&lt;span class="caps"&gt;SSL&lt;/span&gt; Certificates. &lt;span class="caps"&gt;SSL&lt;/span&gt;/TLS is the secure socket layer. It uses the &lt;span class="caps"&gt;PKI&lt;/span&gt; (public key infrastructure) to provide both data encryption and confidentiality. You can review the Wikipedia article about &lt;a href="http://en.wikipedia.org/wiki/Public_key_infrastructure"&gt;&lt;span class="caps"&gt;PKI&lt;/span&gt;&lt;/a&gt; and &lt;a href="http://en.wikipedia.org/wiki/Transport_Layer_Security" title="now TLS"&gt;&lt;span class="caps"&gt;SSL&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;Anywho, with a fully working &lt;span class="caps"&gt;SSL&lt;/span&gt;/TLS system in place, you can be relatively sure that your client-server (and server-server) communications are not only encrypted, but being held between the party with which you believe you are communicating. This means you can use your e-mail and web server on a public network and not worry about your password being sent in the clear or digested format. It also means you&amp;#8217;re sure you&amp;#8217;re talking with your servers and not someone pretending to be a gateway. It&amp;#8217;s nice, but it&amp;#8217;s much more complicated to set up.&lt;/p&gt;


	&lt;p&gt;One of the problems with self-signed certificates is that you are not sure if the certificate is authentic the first time you connect to the server. As I have &lt;span class="caps"&gt;SSL&lt;/span&gt; for my mail and https server, I&amp;#8217;d have to independently verify each certificate via the &lt;span class="caps"&gt;SHA1&lt;/span&gt; hash and identifying information to be absolutely sure my communications are secure.&lt;/p&gt;


	&lt;h2&gt;Where does the Root CA Thingy come in?&lt;/h2&gt;


	&lt;p&gt;&lt;span class="caps"&gt;A CA&lt;/span&gt; is short for &amp;#8220;Certificate Authority.&amp;#8221; A Certificate authority can sign certificates that you make. Because the CA is trusted, when the CA signs your certificate, it transfers that trust to your certificate. When you get your operating system (or your web browser or e-mail client), there are several Root CA&amp;#8217;s pre-installed. They pay lots of cashey money to be there so that your browser, operating system, and e-mail client will recognize theirs and certificates they sign as trusted.&lt;/p&gt;


	&lt;h1&gt;OK, so How do you do it?&lt;/h1&gt;


	&lt;h2&gt;Disclaimers&lt;/h2&gt;


	&lt;p&gt;This guide is for amateur, non-production use only. I make no warranties or guarantees as to the correctness of this document. This guide involves escalating privileges and may cause irreparable damage if used improperly. You use this document at your own risk and I hereby disclaim any responsibility or liability for your actions or non-actions.&lt;/p&gt;


	&lt;h2&gt;Overview&lt;/h2&gt;


	&lt;ol&gt;
	&lt;li&gt;Create a &amp;#8220;proper&amp;#8221; and &amp;#8220;secure&amp;#8221; environment for your CA private key and certificates&lt;/li&gt;
		&lt;li&gt;Create a CA private key&lt;/li&gt;
		&lt;li&gt;Create a CA Certificate Signing Request (CSR)&lt;/li&gt;
		&lt;li&gt;Self-sign the &lt;span class="caps"&gt;CA CSR&lt;/span&gt; using the CA&amp;#8217;s private key thereby creating a CA Certificate&lt;/li&gt;
		&lt;li&gt;Install the CA Certificate locally&lt;/li&gt;
	&lt;/ol&gt;


	&lt;h2&gt;Create a &amp;#8220;proper&amp;#8221; and &amp;#8220;secure&amp;#8221; environment&lt;/h2&gt;


	&lt;p&gt;Before we begin, we need a &amp;#8220;secure&amp;#8221; place to put our CA Root&amp;#8217;s private key. We cannot let this fall into the wrong hands (or any hands other than ours).&lt;/p&gt;


	&lt;p&gt;When you see &lt;span class="caps"&gt;YOURUSERNAME&lt;/span&gt;, replace it with the username you use to access the system onto which you are creating the Root CA&amp;#8217;s certificate.&lt;/p&gt;


&lt;pre&gt;
sudo mkdir /usr/local/etc/certificate_authority
sudo groupadd ca_admins
sudo usermod -a -G ca_admins YOURUSERNAME
cd /usr/local/etc/certificate_authority
sudo mkdir certs
sudo mkdir private
sudo chmod 0770 private
sudo chmod 0775 certs
sudo chown :ca_admins private
sudo chown :ca_admins certs&lt;/pre&gt;

	&lt;p&gt;You&amp;#8217;ll need to log out and then log back in to properly join the ca_admins group. Be sure to cd to the /usr/local/etc/certificate_authority directory when you do so. The following steps assume that you are in that directory.&lt;/p&gt;


	&lt;p&gt;We&amp;#8217;ll be generating the certificate&amp;#8217;s private key. It&amp;#8217;s important that it never be exposed. I highly recommend that you encrypt it now, when it&amp;#8217;s being created, but in this example, I will not do so to facilitate this demonstration. I&amp;#8217;ll indicate where you&amp;#8217;ll need to put in the encryption.&lt;/p&gt;


	&lt;p&gt;I&amp;#8217;ve created an environment that is relatively &amp;#8220;secure.&amp;#8221; That is, it is only as secure as your server. We will generate the private key in the private folder. I have designated this as readable only to root and members of the ca_admins group. I&amp;#8217;ve also put commands in to create that group and have you join it.&lt;/p&gt;


	&lt;p&gt;When you create the CA private key, it will be placed under &amp;#8220;private.&amp;#8221; When the certificate is generated and signed, the resulting certificate will be placed in the certs directory. The only file that needs to be kept secret is the CA private key. We generate it in the next step.&lt;/p&gt;


	&lt;p&gt;The proper location for local configuration changes should be the /usr/local/etc folder. It&amp;#8217;s a toss up as to whether or not a CA&amp;#8217;s private key is a local configuration resource. It may be better to place all of this in the /var/local/certificate_authority. The serial file most assuredly belongs here (created later) as would a Root &lt;span class="caps"&gt;CA CSR&lt;/span&gt; configuration file (not used in this document). However, for the purposes of this document, we&amp;#8217;ll keep it as is.&lt;/p&gt;


	&lt;p&gt;In this step, I&amp;#8217;ve also created a certs directory and a private directory. Certificates that we create for the CA Root should be placed in the certs folder. The private key for the CA Root should be placed in the private directory, as it will not be world-readable.&lt;/p&gt;


	&lt;h2&gt;The CA Private Key&lt;/h2&gt;


	&lt;p&gt;I like &lt;span class="caps"&gt;RSA&lt;/span&gt; certificates, so I generate &lt;span class="caps"&gt;RSA&lt;/span&gt; server private keys.&lt;/p&gt;


&lt;pre&gt;openssl genrsa -out private/cakey.pem 2048&lt;/pre&gt;

	&lt;p&gt;If you want to create an encrypted key, specify this: &lt;pre&gt;openssl genrsa -out private/cakey.pem -des3 2048&lt;/pre&gt; and you&amp;#8217;ll be asked to specify a password for your new key. Do &lt;span class="caps"&gt;NOT&lt;/span&gt; lose this password. You&amp;#8217;ll never be able to get this key back. That means you can&amp;#8217;t sign or create your certificate.&lt;/p&gt;


	&lt;h2&gt;Create a &lt;span class="caps"&gt;CA CSR&lt;/span&gt; and Sign it (single step)&lt;/h2&gt;


&lt;pre&gt;openssl req -new -x509 -key private/cakey.pem -out certs/cacert.crt -days 3600&lt;/pre&gt;

	&lt;p&gt;This creates a 10-year (abouts) certificate signing request that is immediately signed by the CA&amp;#8217;s Root private key to produce the certificate (certs/cacert.crt). We&amp;#8217;ll be using it as our Root CA certificate. If you put a password on the key, you&amp;#8217;ll be asked for that password again. You&amp;#8217;ll also be asked a series of questions to identify your certificate. Answer them honestly. When you get to the CN field, do &lt;span class="caps"&gt;NOT&lt;/span&gt; enter your domain name or similar. Name it something that&amp;#8217;s impossible to be a domain and does not conflict with another Root CA. I called my &amp;#8220;Wojno CA Root&amp;#8221; modeled after Apple&amp;#8217;s certificate name: &amp;#8220;Apple CA Root&amp;#8221;.&lt;/p&gt;


	&lt;p&gt;You now have a real certificate. Now you need to install it locally onto every machine that will use a certificate signed by this Root CA Certificate. Yes, this is bothersome, but it sure beats having to specify an override for every certificate you generate. It also lowers the risk of accepting a bogus certificate by your users, in the event you miss one or more.&lt;/p&gt;


	&lt;h2&gt;Installing the CA Certificate locally&lt;/h2&gt;


	&lt;p&gt;This process varies between operating systems and/or browsers. Apple uses the Keychain. Windows has a root certificate listing in the Internet Options panel (accessible via the control panel). I have no clue where it would be in Vista. You&amp;#8217;re on your own there. Once you have it installed, you&amp;#8217;ll be able to create as many certificates and sign them and all will be automatically trusted because you now trust the Root CA Certificate that will sign them all. Special note: do &lt;span class="caps"&gt;NOT&lt;/span&gt; install stranger root certificates! This will pose a security risk if you go about installing root certificates willy-nilly.&lt;/p&gt;


	&lt;h2&gt;Create the serial file&lt;/h2&gt;


	&lt;p&gt;Back in your ca directory where you created the private and certs folders:&lt;/p&gt;


&lt;pre&gt;echo "01" &amp;gt; serial.srl
sudo chmod 0660 serial.srl
sudo chown :ca_admins serial.srl&lt;/pre&gt;

	&lt;p&gt;Certificates issued by this CA will be imprinted with a serial number. Every time you sign a certificate, the serial file will be incremented and updated. Only ca_admins will be able to modify or read this file. While hiding the contents of this file is not absolutely paramount, the less that is known, the harder it is to break something (this is a generic statement and not necessarily true, but is usually true).&lt;/p&gt;


	&lt;p&gt;For use with Apache2&amp;#8217;s mod_ssl, please see the &lt;a href="http://christopher.wojno.com/articles/2008/11/04/going-ssl-with-your-own-root-ca"&gt;next article&lt;/a&gt; in the series.&lt;/p&gt;</description>
      <pubDate>Tue, 04 Nov 2008 20:06:00 -0800</pubDate>
      <guid isPermaLink="false">urn:uuid:f111918b-6021-481a-96bb-f94751fe3217</guid>
      <author>Christopher Wojno</author>
      <link>http://christopher.wojno.com/articles/2008/11/04/going-ssl-with-your-own-root-ca</link>
      <category>How-Tos</category>
      <category>Security</category>
      <category>ssl</category>
      <category>ca</category>
      <category>root</category>
      <category>certificate</category>
      <category>tls</category>
      <category>self</category>
    </item>
    <item>
      <title>Session of Fear</title>
      <description>&lt;p&gt;Now, before you think &amp;#8220;&lt;acronym title="Fear, Uncertainty, Disorder"&gt;FUD&lt;/acronym&gt; Time!&amp;#8221;, I preface this post with the following warning: Yes, this is a problem with not just Rails but all sessions; no, it&amp;#8217;s not unsolvable. So, in light of the bad news, there is much good news.&lt;/p&gt;


	&lt;h1&gt;The Problem&lt;/h1&gt;


	&lt;p&gt;&lt;a href="http://www.rubyonrails.org/"&gt;Rails&lt;/a&gt; applications are very useful. However, most require sessions in order to be useful. Sure, you can provide an &lt;a href="http://api.rubyonrails.org/classes/ActionWebService/Base.html"&gt;ActionWebService&lt;/a&gt; or &lt;span class="caps"&gt;REST&lt;/span&gt;-based service to not use sessions, but for the majority of user services, you&amp;#8217;re stuck with sessions.&lt;/p&gt;


	&lt;h2&gt;A Session About Sessions&lt;/h2&gt;


	&lt;p&gt;(Go ahead and skip this part if you&amp;#8217;re already familiar with how sessions work)&lt;/p&gt;


	&lt;p&gt;A session is a way a server remembers who you are (in case you didn&amp;#8217;t know). The most common way of allowing a server to ID you, without knowing who you are exactly, is by sharing a secret! Well, it&amp;#8217;s supposed to be secret and we&amp;#8217;ll come to that in a bit. When a new session is created, your session ID is generated (it&amp;#8217;s random, hopefully) and saved in the server. The id can be associated with other data, such as your name, or permissions. When you request a page, the server will give you this ID. You almost never see that ID because it&amp;#8217;s sent in the background as a &amp;#8220;cookie.&amp;#8221; You&amp;#8217;ve probably heard of these before. From now on, every time you request a page from the server, your cookie data is automatically included in the request (your web browser takes care of this) so the server can figure out who you are. Done correctly, no personally identifiable information is sent over the Web. Just your (hopefully) random ID.&lt;/p&gt;


	&lt;p&gt;Sounds good so far. But if you&amp;#8217;re not using &lt;span class="caps"&gt;HTTPS&lt;/span&gt; (SSL/TSL encryption), your secret contained in that cookie can be seen when it is first sent, and when you access any page thereafter (because you&amp;#8217;re sending it each time remember). So, if someone has access to your traffic (if you&amp;#8217;re using a shared WiFi or a hub or have a shady &lt;span class="caps"&gt;ISP&lt;/span&gt;), they can pull out your secret. Here&amp;#8217;s an old session I sniffed using a program called &lt;a href="http://www.snort.org/"&gt;snort:&lt;/a&gt;&lt;/p&gt;


&lt;pre style="overflow:auto;"&gt;
GET /javascripts/prototype.js?1186461045 HTTP/1.1..
Host: christopher.wojno.com..
User-Agent: Mozilla/5.0 (X11; U; FreeBSD i386; en-US; rv:1.8.1.6) Gecko/20070810 Firefox/2.0.0.6..
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5..Accept-Language: en-us,en;q=0.5..
Accept-Encoding: gzip,deflate..
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7..
Keep-Alive: 300..
Connection: keep-alive..
Cookie: _session_id=db392fa5b39125fa7c1e581b9c1ec71d; is_admin=yes....
&lt;/pre&gt;

	&lt;p&gt;There it is, the last line. &amp;#8220;Cookie.&amp;#8221; So when my browser was trying to download some javascript libraries (&lt;a href="http://www.prototypejs.org/"&gt;Prototype&lt;/a&gt; is quite good), it sent my session ID. Now, the javascript doesn&amp;#8217;t care about my session ID, but it was sent any way. If someone were to see that sent over the Internet (at any point on it&amp;#8217;s way to the server), he or she could create a fake cookie with that ID and login as me without my password! (don&amp;#8217;t try it, I&amp;#8217;ve already logged out and reset the session) They could then, go into the settings and lock me out of my own system (until I change the password in the ruby console&amp;#8230; that&amp;#8217;ll show &amp;#8216;em?). In the meantime, the damage has been done and it can happen again.&lt;/p&gt;


	&lt;h1&gt;&lt;span class="caps"&gt;SSL&lt;/span&gt;!&lt;/h1&gt;


	&lt;p&gt;I won&amp;#8217;t go into &lt;span class="caps"&gt;SSL&lt;/span&gt;, but that little piece of technology has enabled credit card purchases over the Internet for years and will hopefully continue to do so for years to come. Any way, in short, it&amp;#8217;s encryption and it will encrypt cookies too!&lt;/p&gt;


	&lt;p&gt;So, we need to force rails to encrypt cookies. Not as simple as it sounds. First, you need to setup an &lt;span class="caps"&gt;SSL&lt;/span&gt; certificate on your server. Easier said than done, though, some Apache packages come with the tools to automatically generate one. For a confusing, yet interesting read, see &lt;a href="http://en.wikipedia.org/wiki/X.509"&gt;&lt;span class="caps"&gt;X509&lt;/span&gt;&lt;/a&gt; on Wikipedia.&lt;/p&gt;


	&lt;p&gt;I assume you installed your &lt;span class="caps"&gt;SSL&lt;/span&gt; certificate and have configured &lt;a href="http://www.apache.org/"&gt;Apache&lt;sup&gt;&lt;a href="#fn1"&gt;1&lt;/a&gt;&lt;/sup&gt;&lt;/a&gt; to run with &lt;span class="caps"&gt;SSL&lt;/span&gt; (or &lt;a href="http://www.lighttpd.net"&gt;lighty,&lt;/a&gt; if you&amp;#8217;re using that instead (&lt;a href="http://mongrel.rubyforge.org/"&gt;mongrel&lt;/a&gt; is not mentioned as it does not have &lt;span class="caps"&gt;SSL&lt;/span&gt;, though there is a way to use &lt;span class="caps"&gt;SSL&lt;/span&gt; and mongrel together)). If not, there are loads of &lt;a href="http://www.google.com/search?hl=en&amp;#38;client=firefox-a&amp;#38;rls=org.mozilla%3Aen-US%3Aofficial&amp;#38;hs=Kie&amp;#38;q=apache+ssl+tutorial&amp;#38;btnG=Search"&gt;tutorials.&lt;/a&gt; I warn you though, it&amp;#8217;s fairly technical.&lt;/p&gt;


	&lt;h1&gt;Hold onto your Cookies!&lt;/h1&gt;


	&lt;p&gt;So, you have &lt;span class="caps"&gt;SSL&lt;/span&gt; and a Rails application running (I&amp;#8217;m assuming). How do you tell Rails to make sure your cookies are only sent via &lt;span class="caps"&gt;SSL&lt;/span&gt;? Rails lets you specify it.&lt;/p&gt;


	&lt;h2&gt;Tell Rails to use &lt;span class="caps"&gt;SSL&lt;/span&gt; Only&lt;/h2&gt;


	&lt;p&gt;By default, Rails does &lt;em&gt;&lt;span class="caps"&gt;NOT&lt;/span&gt;&lt;/em&gt; enforce &lt;span class="caps"&gt;SSL&lt;/span&gt; on cookies or sessions (that would be frustrating for development, wouldn&amp;#8217;t it?). So you have to enable that enforcement yourself. If you want your session cookie to be sent over &lt;span class="caps"&gt;SSL&lt;/span&gt; site-wide (and generally, you do!), head into your rails application directory and open (in your favorite editor) app/controllers/application.rb  Add the following anywhere in the ApplicationController class:&lt;/p&gt;


&lt;pre&gt;
class ApplicationController &amp;lt; ActionController::Base
  session :session_key =&amp;gt; '_session_id', :session_secure =&amp;gt; true
end
&lt;/pre&gt;

	&lt;p&gt;The ApplicationController class definition should already exist, don&amp;#8217;t duplicate it. Also, make sure that session isn&amp;#8217;t already specified. If it is, the important part here is &amp;#8221;:session_secure =&amp;gt; true&amp;#8221;. Rails will now tell the browser to only send the session cookie &lt;em&gt;if&lt;/em&gt; the browser is using the https  (SSL) protocol. This feature is poorly &lt;a href="http://api.rubyonrails.org/classes/ActionController/SessionManagement/ClassMethods.html"&gt;documented&lt;/a&gt; but hopefully this will help keep your applications that you write, safe. &lt;b&gt;&lt;span class="caps"&gt;NOTE&lt;/span&gt;: You &lt;em&gt;&lt;span class="caps"&gt;MUST&lt;/span&gt;&lt;/em&gt; use &lt;span class="caps"&gt;SSL&lt;/span&gt; if you enable this or your application will become extremely forgetful&lt;/b&gt; (Who are you? What are you doing in my kitchen?!).&lt;/p&gt;


	&lt;p&gt;If you&amp;#8217;re interested in storing &lt;span class="caps"&gt;OTHER&lt;/span&gt; data (not overly recommended though due to this and another exploit) in other cookies, Rails offers &lt;a href="http://api.rubyonrails.org/classes/ActionController/Cookies.html"&gt;cookie&amp;#8212;manipulation&lt;/a&gt; (not really management, the &lt;span class="caps"&gt;API&lt;/span&gt; leaves something to be desired). You can tell individual cookies to only be sent over encrypted connections, just like the session cookie. The other exploit: if the computer is shared and the browser doesn&amp;#8217;t clear out the cookies, the next person to sit at the computer can harvest the cookie information, so don&amp;#8217;t store passwords, e-mail addresses&amp;#8230; really anything in cookies, it&amp;#8217;s just a bad idea. DO store worthless information you don&amp;#8217;t want to save in your server, such as squirrel preferences.&lt;/p&gt;


	&lt;p&gt;Admittedly, generally the odds of someone having access to your network traffic (and your cookies, no! &lt;em&gt;MY&lt;/em&gt; cookies!) directly is moderate. For the attacker to successfully pick out your cookie data from the slew of traffic wizzing by, he/she would have to be looking and looking for a specific cookie name. So make sure no one has it out for you.&lt;/p&gt;


	&lt;p&gt;I don&amp;#8217;t mean to sound down on the Rails team. Dang-fine-job I say. Cookies aren&amp;#8217;t really important and the session is cookie-based, so session security falls by the way-side. It&amp;#8217;s up to all developers to keep his or her eyes open for potential pitfalls.&lt;/p&gt;


&lt;pre&gt;
1_000.thank( Rails::DevTeam.members.collect{|m|m.email} )
&lt;/pre&gt;

	&lt;p id="fn1"&gt;&lt;sup&gt;1&lt;/sup&gt; Note: Apache doesn&amp;#8217;t know how to run Ruby code as of this writing. You need to use an Apache&amp;#8217;s mod_proxy. This will (after it&amp;#8217;s been configured) then pass the requests from Apache, to &lt;a href="http://mongrel.rubyforge.org/"&gt;mongrel&lt;/a&gt;, &lt;a href="http://www.lighttpd.net"&gt;lighty&lt;/a&gt;, or&amp;#8230; WEBrick (if you&amp;#8217;re nuts)&lt;/p&gt;</description>
      <pubDate>Fri, 10 Aug 2007 16:48:00 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:360a7ead-402b-4c87-be50-8ade85a22381</guid>
      <author>Christopher Wojno</author>
      <link>http://christopher.wojno.com/articles/2007/08/10/session-of-fear</link>
      <category>Rails Snippets</category>
      <category>ruby</category>
      <category>rails</category>
      <category>ssl</category>
      <category>cookie</category>
      <category>session</category>
      <category>secure_session</category>
      <category>security</category>
      <category>hijack</category>
    </item>
  </channel>
</rss>
