Ruby没有找到新版本的OpenSSL

时间:2013-04-09 16:12:03

标签: ruby macos openssl activesupport

OpenSSL::OPENSSL_VERSION_NUMBER何时何地设置?为什么不设置我刚刚安装的最新OpenSSL?

首先是错误:

$ gem install activesupport -v '3.2.13'
Error while executing gem ... (RuntimeError)
Unsupported digest algorithm (SHA512)

如果我直接进入irb,我可以看到Ruby正在使用“旧”openssl:

$ irb
>> require 'openssl'
=> true
>> OpenSSL::Digest.new('sha512')
RuntimeError: Unsupported digest algorithm (sha512)
>> OpenSSL::OPENSSL_VERSION_NUMBER.to_s(16)
"9070cf"

这告诉我Ruby没有找到我刚刚构建的OpenSSL的本地版本,它至少应该是0x908000。相关代码:

# file: usr/lib/ruby/2.0.0/openssl/digest.rb
...
alg = %w(DSS DSS1 MD2 MD4 MD5 MDC2 RIPEMD160 SHA SHA1)
if OPENSSL_VERSION_NUMBER > 0x00908000
  alg += %w(SHA224 SHA256 SHA384 SHA512)
end

解释了为什么它找不到SHA512。

但我不知道为什么Ruby使用旧版本的OpenSSL。我使用

从新的来源构建了OpenSSL和Ruby
SANDBOX=/Users/me/sandboxes/ruby2
PATH=$(SANDBOX)/usr/bin:$(PATH)

# Create a fresh OpenSSL from sources
(downloaded and unpacked http://www.openssl.org/source/openssl-1.0.1e.tar.gz)
$ ./config --prefix=$(SANDBOX)/usr --openssldir=$(SANDBOX)/usr/openssl
$ make ; make install ; make clean
# verify openssl
$ which openssl
/Users/me/sandboxes/ruby2/usr/bin/openssl
$ openssl version
OpenSSL 1.0.1e 11 Feb 2013

# Create a fresh Ruby from sources
(download and unpack http://ftp.ruby-lang.org/pub/ruby/2.0/ruby-2.0.0-p0.tar.gz)
$ ./configure --prefix=$(SANDBOX)/usr --with-open-ssl-dir=$(SANDBOX)/usr/openssl
$ make ; make intalll ; make clean
# verify ruby
$ which ruby
/Users/me/sandboxes/ruby2/usr/bin/ruby

但是这个ruby似乎没有找到我刚刚构建的openssl 1.0.1e。

我的理解是--with-open-ssl-dir的{​​{1}}参数是必要的,足以告诉ruby使用新的OpenSSL,但这似乎不起作用。

关于如何让Ruby识别我构建的新OpenSSL的任何想法?

我已经尝试按@ Gaurish(下面)的建议运行./configure,但仍然发现系统中安装了OpenSSL,而不是在我的项目根目录中。

5 个答案:

答案 0 :(得分:10)

TL; DR

当OpenSSL发生变化时,请始终重新编译Ruby或openssl原生扩展。

为什么

Ruby将OpenSSL版本编译为openssl本机扩展,即使它链接到共享的OpenSSL库也是如此。重新安装Ruby或重新编译openssl扩展来修复它。

$ ruby -ropenssl -e'puts OpenSSL::OPENSSL_VERSION'
OpenSSL 1.0.2e 3 Dec 2015
$ /usr/local/opt/openssl/bin/openssl version
OpenSSL 1.0.2g  1 Mar 2016
$ strings {{redacted}/ruby-2.3.0/lib/ruby/2.3.0/x86_64-darwin15/openssl.bundle | grep '1.0.2'
OpenSSL 1.0.2e 3 Dec 2015
$ otool -L {{redacted}}/ruby-2.3.0/lib/ruby/2.3.0/x86_64-darwin15/openssl.bundle
{{redacted}}/ruby-2.3.0/lib/ruby/2.3.0/x86_64-darwin15/openssl.bundle:
        {{redacted}}/ruby-2.3.0/lib/libruby.2.3.0.dylib (compatibility version 2.3.0, current version 2.3.0)
        /usr/local/opt/openssl/lib/libssl.1.0.0.dylib (compatibility version 1.0.0, current version 1.0.0)
        /usr/local/opt/openssl/lib/libcrypto.1.0.0.dylib (compatibility version 1.0.0, current version 1.0.0)
        /usr/lib/libz.1.dylib (compatibility version 1.0.0, current version 1.2.5)
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1226.10.1)
        /usr/local/opt/gmp/lib/libgmp.10.dylib (compatibility version 14.0.0, current version 14.0.0)
        /usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)

我们使用ruby-installchruby。我们使用/opt/rubies代替/usr/local/rubies而不是sudo。如果您不想为chruby设置sudo ln -s /usr/local/rubies /opt/rubies,也可以RUBIES

brew install openssl && \
ruby-install ruby-2.3.0 \
  --no-install-deps \
  -- \
  --without-X11 \
  --without-tk \
  --enable-shared \
  --disable-install-doc \
  --with-openssl-dir="$(brew --prefix openssl)"

更新

还有一个常量 来自实际加载的OpenSSL库。

OpenSSL::OPENSSL_LIBRARY_VERSION

答案 1 :(得分:1)

我认为传递给./configure的正确标记是--with-openssl-dir,而不是--with-open-ssl-dir

此外,在这种情况下传递给--with-openssl-dir的正确值为$SANDBOX/usr,而不是$SANDBOX/usr/openssl

此外,您可能需要为64位架构编译OpenSSL。

此过程对我有用(OS X 10.8):

$ export SANDBOX=/Users/me/sandboxes/ruby2
$ mkdir -p $SANDBOX/usr/bin

# Install OpenSSL 1.0.1e
$ curl -O http://www.openssl.org/source/openssl-1.0.1e.tar.gz
$ tar -xzvf openssl-1.0.1e.tar.gz
$ cd openssl-1.0.1e
# Copied from the Homebrew recipe for OpenSSL
$ perl ./Configure --prefix=$SANDBOX/usr --openssldir=$SANDBOX/usr/openssl zlib-dynamic shared darwin64-x86_64-cc enable-ec_nistp_64_gcc_128
$ make depend
$ make
$ make install

# Install Ruby 2.0.0-p0
$ curl -O http://ftp.ruby-lang.org/pub/ruby/2.0/ruby-2.0.0-p0.tar.gz
$ tar -xzvf ruby-2.0.0-p0.tar.gz
$ cd ruby-2.0.0-p0
$ ./configure --prefix=$SANDBOX/usr --with-openssl-dir=$SANDBOX/usr
$ make
$ make install

# Setting PATH before compiling Ruby can can cause the compilation to fail
$ export PATH=$SANDBOX/usr/bin:$PATH
$ which ruby #=>/Users/me/sandboxes/ruby2/usr/bin/ruby
$ which openssl #=> /Users/me/sandboxes/ruby2/usr/bin/openssl
$ openssl version #=> OpenSSL 1.0.1e 11 Feb 2013

$ irb
>> require "openssl"
=> true
>> OpenSSL::Digest.new("sha512")
=> #<OpenSSL::Digest: cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e>

您还可以安装Homebrewchruby(或rbenv)并按照instructions for installing Ruby 2.0.0 for chruby

brew install openssl readline libyaml gdbm libffi
wget http://ftp.ruby-lang.org/pub/ruby/2.0/ruby-2.0.0-p0.tar.gz
tar -xzvf ruby-2.0.0-p0.tar.gz
cd ruby-2.0.0-p0
./configure --prefix=/opt/rubies/ruby-2.0.0-p0 --with-openssl-dir=$(brew --prefix openssl)
make
sudo make install

答案 2 :(得分:0)

事实证明,为了让OpenSSL在Ubuntu上使用Ruby进行编译和安装,您需要在安装ruby之后按照以下步骤

cd ruby_src_dir/ext/openssl
ruby extconf.rb
make
make install

让我知道它是否有效

答案 3 :(得分:0)

旧问题,但如果您必须处理旧设置仍然相关:

在我的情况下,问题在于OpenSSL安装。 如[{3}}所述,您必须确保使用OpenSSL安装共享库

$ ./config --prefix=/path/to/openssl-0.9.8g shared
$ make depend
$ make
$ make install

然后像往常一样安装OpenSSL。

对于ruby,我使用了这个配置行:

$ ./configure --prefix=/path/to/ruby-2.2.2/ --with-openssl-dir=/path/to/openssl-0.9.8g
$ make
$ make install

结果:

$ /path/to/ruby-2.2.2/bin/irb
irb(main):001:0> require "openssl"
=> true
irb(main):002:0> OpenSSL::Digest.new('sha512')
=> #<OpenSSL::Digest: cf83e13000efb8bd00042850d66d8007d620e4050b0005dc83f4a921d36ce00047d0d13c5d85f2b0ff8318d2877eec2f000931bd47417a81a538327af927da3e>

答案 4 :(得分:-1)

Mac 版

sudo gem install openssl --install-dir vendor/bundle -- --with-openssl-dir=/usr/local/Cellar/openssl@1.1/1.1.1k