title: OpenSSL::SSL::SSLContext SNI servername_cb Not Working
body: <p>As referenced in <a href="https://stackoverflow.com/questions/30238304/opensslx509certificate-showing-certificate-for-wrong-domain">OpenSSL::X509::Certificate Showing Certificate for Wrong Domain</a>, I need to use TLSv1 or above <em>and</em> <a href="http://en.wikipedia.org/wiki/Server_Name_Indication" rel="nofollow noreferrer">Server Name Indication</a> extension.</p>
  
  <p>Even with <code>ssl_version</code> and <code>servername_cb</code> set via the <code>SSLContext</code>, I'm still getting the wrong certificate for <code>myproair.com</code>.</p>
  
  <pre><code>  begin 
      timeout(1) do
        tcp_client = TCPSocket.new("#{instance["domain"]}", 443)
        ssl_context = OpenSSL::SSL::SSLContext.new()
        ssl_context.ssl_version = :TLSv1
        ssl_context.servername_cb = "https://#{instance["domain"]}"
        ssl_client = OpenSSL::SSL::SSLSocket.new(tcp_client, ssl_context)
        ssl_client.connect
        cert = OpenSSL::X509::Certificate.new(ssl_client.peer_cert)
        ssl_client.sysclose
        tcp_client.close
        #http://ruby-doc.org/stdlib-2.0/libdoc/openssl/rdoc/OpenSSL/X509/Certificate.html
        date = Date.parse((cert.not_after).to_s)
        row.push("#{date.strftime('%F')} #{cert.signature_algorithm} #{cert.subject.to_a.select{|name, _, _| name == 'CN' }.first[1]}".downcase.ljust(57))
      end
    rescue SocketError
      row.push("down".ljust(57))
    rescue Errno::ECONNREFUSED
      row.push("connection refused".ljust(57))
    rescue Errno::ECONNRESET
      row.push("connection reset".ljust(57))
    rescue Timeout::Error
      row.push("no 443 listener".ljust(57))
    rescue OpenSSL::SSL::SSLError
      row.push("bad certificate - ssl error".ljust(57))
    rescue Exception =&gt; ex
      row.push("error: #{ex.class} #{ex.message}".ljust(57))
    end
  </code></pre>
  
  <p>How do I set the server name in Ruby 2.0 on OS X?</p>
  
  <hr>
  
  <pre class="lang-none prettyprint-override"><code>$ ruby --version
  ruby 2.0.0p481 (2014-05-08 revision 45883) [universal.x86_64-darwin14]
  
  $ openssl version
  OpenSSL 0.9.8zc 15 Oct 2014
  </code></pre>