2023-06-02 15:12:21 +03:00
|
|
|
# frozen_string_literal: true
|
|
|
|
require_relative 'utils'
|
2024-09-05 21:06:37 +03:00
|
|
|
if defined?(OpenSSL) && defined?(OpenSSL::Provider)
|
2023-06-02 15:12:21 +03:00
|
|
|
|
|
|
|
class OpenSSL::TestProvider < OpenSSL::TestCase
|
|
|
|
def test_openssl_provider_name_inspect
|
|
|
|
with_openssl <<-'end;'
|
|
|
|
provider = OpenSSL::Provider.load("default")
|
|
|
|
assert_equal("default", provider.name)
|
|
|
|
assert_not_nil(provider.inspect)
|
|
|
|
end;
|
|
|
|
end
|
|
|
|
|
2024-02-19 05:05:13 +03:00
|
|
|
def test_openssl_provider_names
|
2024-09-05 21:06:37 +03:00
|
|
|
# We expect the following providers are loaded in the cases:
|
|
|
|
# * Non-FIPS: default
|
|
|
|
# * FIPS: fips, base
|
|
|
|
# Use the null provider to test the added provider.
|
|
|
|
# See provider(7) - OPENSSL PROVIDERS to see the list of providers, and
|
|
|
|
# OSSL_PROVIDER-null(7) to check the details of the null provider.
|
2023-06-02 15:12:21 +03:00
|
|
|
with_openssl <<-'end;'
|
2024-09-05 21:06:37 +03:00
|
|
|
num = OpenSSL::Provider.provider_names.size
|
2023-06-02 15:12:21 +03:00
|
|
|
|
2024-09-05 21:06:37 +03:00
|
|
|
added_provider = OpenSSL::Provider.load("null")
|
|
|
|
assert_equal(num + 1, OpenSSL::Provider.provider_names.size)
|
|
|
|
assert_includes(OpenSSL::Provider.provider_names, "null")
|
|
|
|
|
|
|
|
assert_equal(true, added_provider.unload)
|
|
|
|
assert_equal(num, OpenSSL::Provider.provider_names.size)
|
|
|
|
assert_not_includes(OpenSSL::Provider.provider_names, "null")
|
2023-06-02 15:12:21 +03:00
|
|
|
end;
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_unloaded_openssl_provider
|
|
|
|
with_openssl <<-'end;'
|
|
|
|
default_provider = OpenSSL::Provider.load("default")
|
|
|
|
assert_equal(true, default_provider.unload)
|
|
|
|
assert_raise(OpenSSL::Provider::ProviderError) { default_provider.name }
|
|
|
|
assert_raise(OpenSSL::Provider::ProviderError) { default_provider.unload }
|
|
|
|
end;
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_openssl_legacy_provider
|
2024-09-05 21:06:37 +03:00
|
|
|
# The legacy provider is not supported on FIPS.
|
|
|
|
omit_on_fips
|
|
|
|
|
2023-06-02 15:12:21 +03:00
|
|
|
with_openssl(<<-'end;')
|
2024-02-08 20:53:32 +03:00
|
|
|
begin
|
|
|
|
OpenSSL::Provider.load("legacy")
|
|
|
|
rescue OpenSSL::Provider::ProviderError
|
|
|
|
omit "Only for OpenSSL with legacy provider"
|
|
|
|
end
|
|
|
|
|
2023-06-02 15:12:21 +03:00
|
|
|
algo = "RC4"
|
|
|
|
data = "a" * 1000
|
|
|
|
key = OpenSSL::Random.random_bytes(16)
|
|
|
|
|
|
|
|
# default provider does not support RC4
|
|
|
|
cipher = OpenSSL::Cipher.new(algo)
|
|
|
|
cipher.encrypt
|
|
|
|
cipher.key = key
|
|
|
|
encrypted = cipher.update(data) + cipher.final
|
|
|
|
|
|
|
|
other_cipher = OpenSSL::Cipher.new(algo)
|
|
|
|
other_cipher.decrypt
|
|
|
|
other_cipher.key = key
|
|
|
|
decrypted = other_cipher.update(encrypted) + other_cipher.final
|
|
|
|
|
|
|
|
assert_equal(data, decrypted)
|
|
|
|
end;
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# this is required because OpenSSL::Provider methods change global state
|
|
|
|
def with_openssl(code, **opts)
|
2023-08-31 15:34:50 +03:00
|
|
|
assert_separately(["-ropenssl"], <<~"end;", **opts)
|
2023-06-02 15:12:21 +03:00
|
|
|
#{code}
|
|
|
|
end;
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|