<?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>Note to Self &#187; SSL</title>
	<atom:link href="http://notetoself.vrensk.com/tag/ssl/feed/" rel="self" type="application/rss+xml" />
	<link>http://notetoself.vrensk.com</link>
	<description>lest I forget</description>
	<lastBuildDate>Fri, 23 Feb 2018 12:54:38 +0000</lastBuildDate>
	<language>sv-SE</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Verified HTTPS in Ruby</title>
		<link>http://notetoself.vrensk.com/2008/09/verified-https-in-ruby/</link>
		<comments>http://notetoself.vrensk.com/2008/09/verified-https-in-ruby/#comments</comments>
		<pubDate>Sun, 07 Sep 2008 22:06:32 +0000</pubDate>
		<dc:creator>David Vrensk</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Certificate Authorities]]></category>
		<category><![CDATA[HTTPS]]></category>
		<category><![CDATA[peer validation]]></category>
		<category><![CDATA[SSL]]></category>

		<guid isPermaLink="false">http://notetoself.vrensk.com/?p=28</guid>
		<description><![CDATA[I need to pick up an XML file from a server every 30 minutes and process it. I’ve done similar things before, and using Hpricot it is a pleasure: #! /usr/bin/env ruby require 'rubygems' require 'hpricot' require 'open-uri' doc = Hpricot(open("http://example.com/the_file.xml")) (doc / :person).each { &#124;person&#124; ... } Couldn’t be simpler. This time, there is [...]]]></description>
				<content:encoded><![CDATA[<p>I need to pick up an XML file from a server every 30 minutes and process it.  I’ve done similar things before, and using <a href="http://code.whytheluckystiff.net/hpricot/" onclick="pageTracker._trackPageview('/outgoing/code.whytheluckystiff.net/hpricot/?referer=http%3A%2F%2Fnotetoself.vrensk.com%2Ftag%2Fssl%2Ffeed');">Hpricot</a> it is a pleasure:</p>
<pre><code>#! /usr/bin/env ruby
require 'rubygems'
require 'hpricot'
require 'open-uri'

doc = Hpricot(open("http://example.com/the_file.xml"))
(doc / :person).each { |person| ... }
</code></pre>
<p>Couldn’t be simpler.  This time, there is a snag: the file is sensitive, so the connection is encrypted using HTTPS.  For this article, let’s say we’re talking about <a href="http://www.cert.org/" onclick="pageTracker._trackPageview('/outgoing/www.cert.org/?referer=http%3A%2F%2Fnotetoself.vrensk.com%2Ftag%2Fssl%2Ffeed');">Cert’s</a> list of new vulnerabilities, which can be found at <a href="https://www.cert.org/blogs/vuls/rss.xml" onclick="pageTracker._trackPageview('/outgoing/www.cert.org/blogs/vuls/rss.xml?referer=http%3A%2F%2Fnotetoself.vrensk.com%2Ftag%2Fssl%2Ffeed');">https://www.cert.org/blogs/vuls/rss.xml</a>.  <code>open-uri</code> supports HTTPS, so it shouldn’t be a problem, but it is:</p>
<pre><code>doc = Hpricot(open("https://www.cert.org/blogs/vuls/rss.xml"))
# =&gt;
/usr/lib/ruby/1.8/net/http.rb:590:in `connect': certificate verify failed (OpenSSL::SSL::SSLError)
</code></pre>
<p><code>OpenSSL</code>, which <code>open-uri</code> uses behind the scenes, fails to verify Cert’s certificate and halts execution.</p>
<h2>Solution 1: skip verification</h2>
<p>Let’s assume that I don’t care much about the verification; all I want is the data, and it just so happens that it is only available through HTTPS.  <code>open-uri</code> doesn’t let me turn off verification so I have to dig deeper.</p>
<p><code>open-uri</code> is just a clever wrapper around Ruby’s comprehensive, but insufficiently documented, networking library that handles a variety of protocols, including HTTPS.  To fetch a web page over a secure connection, you can use something like this sample client (from <code>net/https.rb</code>):</p>
<pre><code>#! /usr/bin/env ruby
require 'net/https'
require 'uri'

uri = URI.parse(ARGV[0] || 'https://localhost/')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true if uri.scheme == "https"  # enable SSL/TLS
http.start {
  http.request_get(uri.path) {|res|
    print res.body
  }
}
</code></pre>
<p>There are three things to note in the sample client:</p>
<ol>
<li>You should <code>require</code> <code>net/https</code>, not <code>net/http</code>.</li>
<li>You create the client with <code>Net::HTTP.new</code>, not <code>Net::HTTPS.new</code>. (There is no <code>HTTPS</code> class despite the fact that you <code>require 'net/https'</code>.)</li>
<li>You need to set <code>use_ssl = true</code> explicitly.  The <code>URI</code> library is clever enough to set its port attribute to 443 when it parses a URI that starts with <code>https</code>, but <code>Net::HTTP</code> isn’t quite as clever.</li>
</ol>
<p>If you put the above code in <code>webclient.rb</code> and run it, you’ll see this:</p>
<pre><code>$ ruby webclient.rb https://www.cert.org/blogs/vuls/rss.xml
warning: peer certificate won't be verified in this SSL session
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;rss version="2.0"&gt;
    &lt;channel&gt;
        &lt;title&gt;Vulnerability Analysis Blog&lt;/title&gt;
[...]
</code></pre>
<p>Yes, it will fetch and print the RSS XML, but it will also warn you that it doesn’t verify the host’s certificate.  Let’s turn off the warning by telling <code>Net::HTTP</code> that we don’t expect it to perform any verification:</p>
<pre><code>uri = URI.parse(ARGV[0] || 'https://localhost/')
http = Net::HTTP.new(uri.host, uri.port)
if uri.scheme == "https"  # enable SSL/TLS
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
http.start { ... }
</code></pre>
<p>Run this, and you get the same result without the warning.</p>
<h2>Solution 2: add verification</h2>
<p>Solution 1 is not enough for my current needs.  I want encryption, but I also want to know that I’m talking to the right server.  To turn on verification, I change <code>VERIFY_NONE</code> to <code>VERIFY_PEER</code> and run again.  Now I’m back on square one with <code>OpenSSL::SSL::SSLError: certificate verify failed</code>.  Uh-huh.  So what’s wrong with that one?  It works in my browser without problems.</p>
<p>I’m not going to go into how HTTPS and certificate validation works.  Suffice it so say that my browser is more trusting than <code>OpenSSL</code>.  And it’s not blind trust either; the browser knows more Certificate Authorities.  So how do I add them to Ruby and <code>OpenSSL</code>?  I looked around and found a solution to a similar problem, <a href="http://blog.seagul.co.uk/articles/2006-10-24-connecting-to-gmail-with-ruby-or-connecting-to-pop3-servers-over-ssl-with-ruby" onclick="pageTracker._trackPageview('/outgoing/blog.seagul.co.uk/articles/2006-10-24-connecting-to-gmail-with-ruby-or-connecting-to-pop3-servers-over-ssl-with-ruby?referer=http%3A%2F%2Fnotetoself.vrensk.com%2Ftag%2Fssl%2Ffeed');">Connecting to POP3 servers over SSL with Ruby</a>.  Adapting that to my HTTPS problem, it becomes a two-step solution:</p>
<ol>
<li>Download the <a href="http://curl.haxx.se/ca/cacert.pem" onclick="pageTracker._trackPageview('/outgoing/curl.haxx.se/ca/cacert.pem?referer=http%3A%2F%2Fnotetoself.vrensk.com%2Ftag%2Fssl%2Ffeed');">CA Root Certificates bundle</a> from haxx.se, the creators of <code>curl</code>.  Store the file in the same directory as <code>webclient.rb</code> and make sure that it’s called <code>cacert.pem</code>.  (But please see the discussion below on <em>Too much trust</em>.)</li>
<li>Make <code>webclient.rb</code> use this file instead of whatever is bundled with <code>OpenSSL</code>.</li>
</ol>
<p>Now we can tell <code>Net::HTTP</code> to use this CA file:</p>
<pre><code>uri = URI.parse(ARGV[0] || 'https://localhost/')
http = Net::HTTP.new(uri.host, uri.port)
if uri.scheme == "https"  # enable SSL/TLS
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_PEER
  http.ca_file = File.join(File.dirname(__FILE__), "cacert.pem")
end
http.start { ... }
</code></pre>
<p>Look, it works!  It gives the expected output, and it is verifying… something.  But what?  Time to look under the hood again.  It turns out that with these settings, <code>OpenSSL</code> checks that the server certificate is signed by a known CA and has not expired, which is good, but not everything I’m looking for.  I also want it to check that the certificate belongs to the server that I’m talking to.  To see an example, go to <a href="https://google.com/" onclick="pageTracker._trackPageview('/outgoing/google.com/?referer=http%3A%2F%2Fnotetoself.vrensk.com%2Ftag%2Fssl%2Ffeed');">https://google.com/</a>.  In Firefox 3, you should get an iconic policeman telling you it’s a Page Load Error.  The certificate belongs to <code>www.google.com</code>, not <code>google.com</code>.  But our script is not quite as discerning:</p>
<pre><code>$ ruby webclient.rb https://google.com/
hostname was not match with the server certificate
&lt;HTML&gt;&lt;HEAD&gt;&lt;meta http-equiv="content-type" content="text/html;charset=utf-8"&gt;
&lt;TITLE&gt;302 Moved&lt;/TITLE&gt;&lt;/HEAD&gt;&lt;BODY&gt;
&lt;H1&gt;302 Moved&lt;/H1&gt;
The document has moved
&lt;A HREF="http://www.google.com"&gt;here&lt;/A&gt;.
&lt;/BODY&gt;&lt;/HTML&gt;
</code></pre>
<p>Note the warning on the first line of output.  Apparently <code>Net::HTTP</code> checks to see if the certificate belongs to the host, but it’s not a fatal error.  To change this, we need to enable the “post-connection check”.  So here is the final version of the script:</p>
<pre><code>#! /usr/bin/env ruby
require 'net/https'
require 'uri'

uri = URI.parse(ARGV[0] || 'https://localhost/')
http = Net::HTTP.new(uri.host, uri.port)
if uri.scheme == "https"  # enable SSL/TLS
  http.use_ssl = true
  http.enable_post_connection_check = true
  http.verify_mode = OpenSSL::SSL::VERIFY_PEER
  http.ca_file = File.join(File.dirname(__FILE__), "cacert.pem")
end
http.start {
  http.request_get(uri.path) {|res|
    print res.body
  }
}
</code></pre>
<p>Now it will fail for <code>https://google.com/</code> but succeed for <code>https://www.google.com/</code>.  Done!</p>
<h2>Too much trust</h2>
<p>OK, I should admit that downloading a file from someplace called <code>haxx.se</code> doesn’t seem like the best way to raise security.  If you really want to know who you will be trusting, you should download Root Certificates from each of the CA’s that you trust.  That’s way too much work for the application I’m working on right now, but it might be a requirement for you.  If you don’t want to go mad, though, try doing it the same way the haxx people did.  They wrote a little tool to extract the Root Certificates from the source files of Mozilla, and they even have a tool for extracting it from your binary installation.  Check out <a href="http://curl.haxx.se/docs/caextract.html" onclick="pageTracker._trackPageview('/outgoing/curl.haxx.se/docs/caextract.html?referer=http%3A%2F%2Fnotetoself.vrensk.com%2Ftag%2Fssl%2Ffeed');">their documentation</a> for a full description and links to the tools (source code).</p>
<p><strong>[Update: </strong>John in comment 16 has written up an instruction on how to get the certificates file using https.  Turtles all the way down.]</p>
]]></content:encoded>
			<wfw:commentRss>http://notetoself.vrensk.com/2008/09/verified-https-in-ruby/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
	</channel>
</rss>
