title: Intermittent SSL certificate verification failures with mechanize and ruby
body: <p>I have a Rails 3.2.8 app, with Ruby 1.9.3 on Ubuntu 12.04. It uses mechanize to connect to an https web site.</p>
  
  <p>I am seeing this error intermittently:</p>
  
  <pre><code>SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed
  </code></pre>
  
  <p>I do set the CA file:</p>
  
  <pre><code>Mechanize.new do |agent|
    agent.ssl_version = "SSLv3"
    agent.ca_file = Rails.root.join("lib/cacert.pem").to_s
  end
  </code></pre>
  
  <p>I have also tried using <code>cert_store</code>:</p>
  
  <pre><code>cert_store = OpenSSL::X509::Store.new
  cert_store.set_default_paths
  
  Mechanize.new do |agent|
    agent.ssl_version = "SSLv3"
    agent.cert_store = cert_store
  end
  </code></pre>
  
  <p>And setting the store explicitly:</p>
  
  <pre><code>cert_store = OpenSSL::X509::Store.new
  cert_store.add_file Rails.root.join("lib/cacert.pem").to_s
  
  Mechanize.new do |agent|
    agent.ssl_version = "SSLv3"
    agent.cert_store = cert_store
  end
  </code></pre>
  
  <p>These errors appear regardless of which method I use to specify the CA/certificates (including relying on default behaviour). When I run the code manually from rails console, it works fine. Which of the above, if any, are correct? What else can I do to debug this?</p>