2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2011-07-21 20:13:07 -04:00
|
|
|
require_relative 'utils'
|
|
|
|
|
2017-09-03 08:35:27 -04:00
|
|
|
if defined?(OpenSSL) && defined?(OpenSSL::Engine)
|
2011-07-21 20:13:07 -04:00
|
|
|
|
2017-09-03 08:35:27 -04:00
|
|
|
class OpenSSL::TestEngine < OpenSSL::TestCase
|
2011-07-21 20:13:07 -04:00
|
|
|
def test_engines_free # [ruby-dev:44173]
|
2016-06-07 08:20:46 -04:00
|
|
|
with_openssl <<-'end;'
|
|
|
|
OpenSSL::Engine.load("openssl")
|
|
|
|
OpenSSL::Engine.engines
|
|
|
|
OpenSSL::Engine.engines
|
|
|
|
end;
|
2011-07-21 20:13:07 -04:00
|
|
|
end
|
|
|
|
|
2011-11-03 20:33:11 -04:00
|
|
|
def test_openssl_engine_builtin
|
2016-06-07 08:20:46 -04:00
|
|
|
with_openssl <<-'end;'
|
2016-06-12 00:48:10 -04:00
|
|
|
orig = OpenSSL::Engine.engines
|
2016-08-29 01:47:09 -04:00
|
|
|
pend "'openssl' is already loaded" if orig.any? { |e| e.id == "openssl" }
|
2016-06-07 08:20:46 -04:00
|
|
|
engine = OpenSSL::Engine.load("openssl")
|
|
|
|
assert_equal(true, engine)
|
2016-06-12 00:48:10 -04:00
|
|
|
assert_equal(1, OpenSSL::Engine.engines.size - orig.size)
|
2016-06-07 08:20:46 -04:00
|
|
|
end;
|
2011-11-03 20:33:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_openssl_engine_by_id_string
|
2016-06-07 08:20:46 -04:00
|
|
|
with_openssl <<-'end;'
|
2016-06-12 00:48:10 -04:00
|
|
|
orig = OpenSSL::Engine.engines
|
2016-08-29 01:47:09 -04:00
|
|
|
pend "'openssl' is already loaded" if orig.any? { |e| e.id == "openssl" }
|
2016-06-07 08:20:46 -04:00
|
|
|
engine = get_engine
|
|
|
|
assert_not_nil(engine)
|
2016-06-12 00:48:10 -04:00
|
|
|
assert_equal(1, OpenSSL::Engine.engines.size - orig.size)
|
2016-06-07 08:20:46 -04:00
|
|
|
end;
|
2011-11-03 20:33:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_openssl_engine_id_name_inspect
|
2016-06-07 08:20:46 -04:00
|
|
|
with_openssl <<-'end;'
|
|
|
|
engine = get_engine
|
|
|
|
assert_equal("openssl", engine.id)
|
|
|
|
assert_not_nil(engine.name)
|
|
|
|
assert_not_nil(engine.inspect)
|
|
|
|
end;
|
2011-11-03 20:33:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_openssl_engine_digest_sha1
|
2016-06-07 08:20:46 -04:00
|
|
|
with_openssl <<-'end;'
|
|
|
|
engine = get_engine
|
|
|
|
digest = engine.digest("SHA1")
|
|
|
|
assert_not_nil(digest)
|
|
|
|
data = "test"
|
|
|
|
assert_equal(OpenSSL::Digest::SHA1.digest(data), digest.digest(data))
|
|
|
|
end;
|
2011-11-05 14:33:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_openssl_engine_cipher_rc4
|
2017-09-24 12:26:22 -04:00
|
|
|
begin
|
|
|
|
OpenSSL::Cipher.new("rc4")
|
|
|
|
rescue OpenSSL::Cipher::CipherError
|
|
|
|
pend "RC4 is not supported"
|
|
|
|
end
|
|
|
|
|
|
|
|
with_openssl(<<-'end;', ignore_stderr: true)
|
|
|
|
engine = get_engine
|
|
|
|
algo = "RC4"
|
|
|
|
data = "a" * 1000
|
|
|
|
key = OpenSSL::Random.random_bytes(16)
|
|
|
|
encrypted = crypt_data(data, key, :encrypt) { engine.cipher(algo) }
|
|
|
|
decrypted = crypt_data(encrypted, key, :decrypt) { OpenSSL::Cipher.new(algo) }
|
|
|
|
assert_equal(data, decrypted)
|
2016-06-07 08:20:46 -04:00
|
|
|
end;
|
2012-08-21 00:50:18 -04:00
|
|
|
end
|
2011-11-03 20:33:11 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-06-07 08:20:46 -04:00
|
|
|
# this is required because OpenSSL::Engine methods change global state
|
2017-09-24 12:26:22 -04:00
|
|
|
def with_openssl(code, **opts)
|
|
|
|
assert_separately([{ "OSSL_MDEBUG" => nil }, "-ropenssl"], <<~"end;", **opts)
|
2016-06-07 08:20:46 -04:00
|
|
|
require #{__FILE__.dump}
|
|
|
|
include OpenSSL::TestEngine::Utils
|
|
|
|
#{code}
|
|
|
|
end;
|
2011-11-26 09:04:52 -05:00
|
|
|
end
|
|
|
|
|
2016-06-07 08:20:46 -04:00
|
|
|
module Utils
|
|
|
|
def get_engine
|
|
|
|
OpenSSL::Engine.by_id("openssl")
|
|
|
|
end
|
2011-11-23 20:09:55 -05:00
|
|
|
|
2016-06-07 08:20:46 -04:00
|
|
|
def crypt_data(data, key, mode)
|
|
|
|
cipher = yield
|
|
|
|
cipher.send mode
|
|
|
|
cipher.key = key
|
|
|
|
cipher.update(data) + cipher.final
|
|
|
|
end
|
|
|
|
end
|
2017-09-03 08:35:27 -04:00
|
|
|
end
|
2011-07-21 20:13:07 -04:00
|
|
|
|
2017-09-03 08:35:27 -04:00
|
|
|
end
|