|
1 | 1 | # encoding: ascii-8bit |
2 | 2 |
|
| 3 | +require 'openssl' |
3 | 4 | require 'ffi' |
4 | 5 |
|
5 | 6 | module Bitcoin |
6 | 7 | # autoload when you need to re-generate a public_key from only its private_key. |
7 | 8 | # ported from: https://github.com/sipa/bitcoin/blob/2d40fe4da9ea82af4b652b691a4185431d6e47a8/key.h |
8 | 9 | module OpenSSL_EC # rubocop:disable Naming/ClassAndModuleCamelCase |
9 | 10 | extend FFI::Library |
10 | | - if FFI::Platform.windows? |
11 | | - ffi_lib 'libeay32', 'ssleay32' |
12 | | - else |
13 | | - ffi_lib [ |
14 | | - 'libssl.so.1.1.0', 'libssl.so.1.1', |
15 | | - 'libssl.so.1.0.0', 'libssl.so.10', |
16 | | - 'ssl' |
17 | | - ] |
18 | | - end |
| 11 | + |
| 12 | + # Use the library loaded by the extension require above. |
| 13 | + ffi_lib FFI::CURRENT_PROCESS |
19 | 14 |
|
20 | 15 | NID_secp256k1 = 714 # rubocop:disable Naming/ConstantName |
21 | 16 | POINT_CONVERSION_COMPRESSED = 2 |
22 | 17 | POINT_CONVERSION_UNCOMPRESSED = 4 |
23 | 18 |
|
24 | | - # OpenSSL 1.1.0 version as a numerical version value as defined in: |
25 | | - # https://www.openssl.org/docs/man1.1.0/man3/OpenSSL_version.html |
26 | | - VERSION_1_1_0_NUM = 0x10100000 |
27 | | - |
28 | 19 | # OpenSSL 1.1.0 engine constants, taken from: |
29 | 20 | # https://github.com/openssl/openssl/blob/2be8c56a39b0ec2ec5af6ceaf729df154d784a43/include/openssl/crypto.h |
30 | 21 | OPENSSL_INIT_ENGINE_RDRAND = 0x00000200 |
@@ -52,21 +43,10 @@ module OpenSSL_EC # rubocop:disable Naming/ClassAndModuleCamelCase |
52 | 43 | attach_function :SSLeay, [], :long |
53 | 44 | end |
54 | 45 |
|
55 | | - # Returns the version of SSL present. |
56 | | - # |
57 | | - # @return [Integer] version number as an integer. |
58 | | - def self.version |
59 | | - if self.respond_to?(:OpenSSL_version_num) |
60 | | - OpenSSL_version_num() |
61 | | - else |
62 | | - SSLeay() |
63 | | - end |
64 | | - end |
65 | | - |
66 | | - if version >= VERSION_1_1_0_NUM |
| 46 | + begin |
67 | 47 | # Initialization procedure for the library was changed in OpenSSL 1.1.0 |
68 | 48 | attach_function :OPENSSL_init_ssl, [:uint64, :pointer], :int |
69 | | - else |
| 49 | + rescue FFI::NotFoundError |
70 | 50 | attach_function :SSL_library_init, [], :int |
71 | 51 | attach_function :ERR_load_crypto_strings, [], :void |
72 | 52 | attach_function :SSL_load_error_strings, [], :void |
@@ -391,7 +371,7 @@ def self.init_ffi_ssl |
391 | 371 | @ssl_loaded ||= false |
392 | 372 | return if @ssl_loaded |
393 | 373 |
|
394 | | - if version >= VERSION_1_1_0_NUM |
| 374 | + if self.method_defined?(:OPENSSL_init_ssl) |
395 | 375 | OPENSSL_init_ssl( |
396 | 376 | OPENSSL_INIT_LOAD_SSL_STRINGS | OPENSSL_INIT_ENGINE_ALL_BUILTIN, |
397 | 377 | nil |
|
0 commit comments