Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@toshiharu
Last active June 11, 2016 04:43
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toshiharu/5539841 to your computer and use it in GitHub Desktop.
Save toshiharu/5539841 to your computer and use it in GitHub Desktop.
Windows の Ruby で SSL のエラーが出ないようにしたときのメモ。

Windows の Ruby で SSL を使う。

特別なことをせずに RubyInstaller for Windows 2.0.0-p0 でセットアップした ruby で https を扱おうとすると「OpenSSL::SSL::SSLError: SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed」といったエラーが出るわけで、これを回避するために次のようなことをした。

まず ca-bundle.crt をどこかからもらってくる。僕は手元の Linux 機からコピーした。たとえば Fedora なら ca-certificates パッケージに含まれているだろう。

次に上のエラーが出てしまうプログラムのなるべく最初のあたり (openssl.rb を読み込む前というか ssleay32.dll かな) に下の一行を追加する。ca-bundle.crt のパスとかは適当に処理のこと。

ENV['SSL_CERT_FILE'] = File.expand_path('C:\Users\toshiharu\ca-bundle.crt')

詳しくはマニュアルで。

以下はテスト。

# test1.rb
require 'open-uri'
p open('https://www.google.com/')
C:\Users\toshiharu>ruby test1.rb
C:/Ruby200-x64/lib/ruby/2.0.0/net/http.rb:917:in `connect': SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed (OpenSSL::SSL::SSLError)
        from C:/Ruby200-x64/lib/ruby/2.0.0/net/http.rb:917:in `block in connect'
        from C:/Ruby200-x64/lib/ruby/2.0.0/timeout.rb:51:in `timeout'
        from C:/Ruby200-x64/lib/ruby/2.0.0/net/http.rb:917:in `connect'
        from C:/Ruby200-x64/lib/ruby/2.0.0/net/http.rb:861:in `do_start'
        from C:/Ruby200-x64/lib/ruby/2.0.0/net/http.rb:850:in `start'
        from C:/Ruby200-x64/lib/ruby/2.0.0/open-uri.rb:313:in `open_http'
        from C:/Ruby200-x64/lib/ruby/2.0.0/open-uri.rb:708:in `buffer_open'
        from C:/Ruby200-x64/lib/ruby/2.0.0/open-uri.rb:210:in `block in open_loop'
        from C:/Ruby200-x64/lib/ruby/2.0.0/open-uri.rb:208:in `catch'
        from C:/Ruby200-x64/lib/ruby/2.0.0/open-uri.rb:208:in `open_loop'
        from C:/Ruby200-x64/lib/ruby/2.0.0/open-uri.rb:149:in `open_uri'
        from C:/Ruby200-x64/lib/ruby/2.0.0/open-uri.rb:688:in `open'
        from C:/Ruby200-x64/lib/ruby/2.0.0/open-uri.rb:34:in `open'
        from test1.rb:2:in `'

C:\Users\toshiharu>
# test2.rb
ENV['SSL_CERT_FILE'] = File.expand_path('C:\Users\toshiharu\ca-bundle.crt')
require 'open-uri'
p open('https://www.google.com/')
C:\Users\toshiharu>ruby test2.rb
#<Tempfile:C:/Users/TOSHIH~1/AppData/Local/Temp/open-uri20130508-1124-q0o1b>

C:\Users\toshiharu>
@constdrop
Copy link

私はca-bundle.crt を certifie.com からダウンロードしました。うまくいきました。ありがとうございます。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment