[Server & Network General] さくらのレンタルサーバーから Net:HTTP で HTTPS(SSL) 通信する Ruby のコード
memo.
[markdown]
## 症状
“`prettyprinted
SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed (OpenSSL::SSL::SSLError)
“`
こちらを参考にさせて頂きました。ありがとうございます。
> * [エラー:OpenSSL::SSL::SSLError SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed – komiyakの通り道](http://d.hatena.ne.jp/komiyak/20130508/1367993536)
## 調査
参照している証明書はどこにあるのか。
“`prettyprinted
% irb
irb(main):001:0> require ‘openssl’
=> true
irb(main):002:0> OpenSSL::X509::DEFAULT_CERT_FILE
=> “/etc/ssl/cert.pem”
“`
実体はない模様。
“`prettyprinted
% cat /etc/ssl/cert.pem
cat: /etc/ssl/cert.pem: No such file or directory
“`
## 対応
証明書あたりを突っ込んで調べるとハマりそうなので、`OpenSSL::SSL::VERIFY_NONE` で対応。
“`ruby
require ‘openssl’
require ‘net/http’
https = Net::HTTP.new(‘www.google.co.jp’, 443) # address, port
https.use_ssl = true
https.verify_mode = OpenSSL::SSL::VERIFY_NONE
# https.verify_mode = OpenSSL::SSL::VERIFY_PEER
# https.ca_file = ‘/etc/ssl/cert.pem’
https.start do
response = https.get(‘/’) # path
puts response.body
end
“`
> [Using Net::HTTP to the HTTPS url](https://gist.github.com/DriftwoodJP/5e04d7c5bda0a2efc95560b9887f6abf)
## 補遺
> * [クエリ:verify_mode= > バージョン:2.4.0 | るりまサーチ (Ruby 2.4.0)](https://docs.ruby-lang.org/ja/search/query:verify_mode%3D/version:2.4.0/)
[/markdown]