diff --git a/.gitignore b/.gitignore index 0b63bf4..58db492 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,5 @@ tmp Gemfile.lock .idea/* .rbx/ +.ruby-version +.ruby-gemset diff --git a/lib/httpi/adapter/curb.rb b/lib/httpi/adapter/curb.rb index 99fc3b0..19924a5 100644 --- a/lib/httpi/adapter/curb.rb +++ b/lib/httpi/adapter/curb.rb @@ -121,12 +121,14 @@ def setup_ssl_auth end @client.ssl_version = case ssl.ssl_version - when :TLSv1_2 then 1 - when :TLSv1_1 then 1 - when :TLSv1 then 1 - when :SSLv2 then 2 - when :SSLv23 then 2 - when :SSLv3 then 3 + when :TLSv1_2 then ::Curl::CURL_SSLVERSION_TLSv1_2 + when :TLSv1_1 then ::Curl::CURL_SSLVERSION_TLSv1_1 + when :TLSv1_0 then ::Curl::CURL_SSLVERSION_TLSv1_0 + when :TLSv1 then ::Curl::CURL_SSLVERSION_TLSv1 + when :SSLv2 then ::Curl::CURL_SSLVERSION_SSLv2 + when :SSLv23 then ::Curl::CURL_SSLVERSION_SSLv2 + when :SSLv3 then ::Curl::CURL_SSLVERSION_SSLv3 + else ::Curl::CURL_SSLVERSION_DEFAULT end end diff --git a/lib/httpi/auth/ssl.rb b/lib/httpi/auth/ssl.rb index f998a5a..1c87e4d 100644 --- a/lib/httpi/auth/ssl.rb +++ b/lib/httpi/auth/ssl.rb @@ -11,14 +11,21 @@ class SSL VERIFY_MODES = [:none, :peer, :fail_if_no_peer_cert, :client_once] CERT_TYPES = [:pem, :der] - # Fix for - # httpi/auth/ssl.rb:13: warning: constant OpenSSL::SSL::SSLContext::METHODS is deprecated - ssl_context = OpenSSL::SSL::SSLContext - SSL_VERSIONS = if ssl_context.const_defined? :METHODS_MAP - ssl_context.const_get(:METHODS_MAP).keys - else - ssl_context::METHODS.reject { |method| method.match(/server|client/) } - end.sort.reverse + # A default set of possible SSL protocols httpi should try to have support for. + # Note: Not all adapters may support all listed versions. + SSL_VERSIONS = [:SSLv23, :SSLv2, :SSLv3, :TLSv1, :TLSv1_0, :TLSv1_1, :TLSv1_2] + + # All supported OpenSSL protocols. + # Used in case of OpenSSL support unexpected protocol which is not listed in SSL_VERSIONS. + OPENSSL_ALL_VERSIONS = begin + ssl_context = OpenSSL::SSL::SSLContext + # Avoid "warning: constant OpenSSL::SSL::SSLContext::METHODS is deprecated" message + if ssl_context.const_defined? :METHODS_MAP + ssl_context.const_get(:METHODS_MAP).keys + else + ssl_context::METHODS.reject { |method| method.match(/server|client/) } + end + end # Returns whether SSL configuration is present. def present? @@ -82,7 +89,7 @@ def ssl_version # Sets the SSL version number. Expects one of HTTPI::Auth::SSL::SSL_VERSIONS. def ssl_version=(version) - unless SSL_VERSIONS.include? version + unless SSL_VERSIONS.include?(version) || OPENSSL_ALL_VERSIONS.include?(version) raise ArgumentError, "Invalid SSL version #{version.inspect}\n" + "Please specify one of #{SSL_VERSIONS.inspect}" end diff --git a/spec/httpi/adapter/curb_spec.rb b/spec/httpi/adapter/curb_spec.rb index ec931b7..6422043 100644 --- a/spec/httpi/adapter/curb_spec.rb +++ b/spec/httpi/adapter/curb_spec.rb @@ -251,15 +251,14 @@ end context 'sets ssl_version' do - it 'defaults to nil when no ssl_version is specified' do - curb.expects(:ssl_version=).with(nil) + it 'defaults to 0 when no ssl_version is specified' do + curb.expects(:ssl_version=).with(0) adapter.request(:get) end it 'to 1 when ssl_version is specified as TLSv1' do request.auth.ssl.ssl_version = :TLSv1 curb.expects(:ssl_version=).with(1) - adapter.request(:get) end @@ -267,14 +266,30 @@ version = HTTPI::Auth::SSL::SSL_VERSIONS.select { |method| method.to_s.match(/SSLv2|SSLv23/) }.first request.auth.ssl.ssl_version = version curb.expects(:ssl_version=).with(2) - adapter.request(:get) end it 'to 3 when ssl_version is specified as SSLv3' do request.auth.ssl.ssl_version = :SSLv3 curb.expects(:ssl_version=).with(3) + adapter.request(:get) + end + + it 'to 4 when ssl_version is specified as TLSv1_0' do + request.auth.ssl.ssl_version = :TLSv1_0 + curb.expects(:ssl_version=).with(4) + adapter.request(:get) + end + + it 'to 5 when ssl_version is specified as TLSv1_1' do + request.auth.ssl.ssl_version = :TLSv1_1 + curb.expects(:ssl_version=).with(5) + adapter.request(:get) + end + it 'to 6 when ssl_version is specified as TLSv1_2' do + request.auth.ssl.ssl_version = :TLSv1_2 + curb.expects(:ssl_version=).with(6) adapter.request(:get) end end