Skip to content

Commit

Permalink
Deprecate OpenSSL::Digest and OpenSSL::Cipher constants
Browse files Browse the repository at this point in the history
  • Loading branch information
bdewater committed May 10, 2020
1 parent c14e2b2 commit 4afece2
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 19 deletions.
7 changes: 6 additions & 1 deletion lib/openssl/cipher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class Cipher
}
}
const_set(name, klass)
deprecate_constant(name)
klass
}

%w(128 192 256).each{|keylen|
Expand All @@ -30,7 +32,10 @@ class Cipher
super("aes-#{keylen}-#{mode}".downcase)
}
}
const_set("AES#{keylen}", klass)
name = "AES#{keylen}"
const_set(name, klass)
deprecate_constant(name)
klass
}

# call-seq:
Expand Down
5 changes: 4 additions & 1 deletion lib/openssl/digest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ def self.digest(name, data)
define_method(:hexdigest) {|data| new.hexdigest(data)}
}

const_set(name.tr('-', '_'), klass)
constant_name = name.tr('-', '_')
const_set(constant_name, klass)
deprecate_constant(constant_name)
klass
end

# Deprecated.
Expand Down
20 changes: 7 additions & 13 deletions test/openssl/test_digest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class OpenSSL::TestDigest < OpenSSL::TestCase
def setup
super
@d1 = OpenSSL::Digest.new("MD5")
@d2 = OpenSSL::Digest::MD5.new
@d2 = silence_warnings { OpenSSL::Digest::MD5.new }
end

def test_digest
Expand Down Expand Up @@ -54,10 +54,12 @@ def test_reset
end

def test_digest_constants
%w{MD4 MD5 RIPEMD160 SHA1 SHA224 SHA256 SHA384 SHA512}.each do |name|
assert_not_nil(OpenSSL::Digest.new(name))
klass = OpenSSL::Digest.const_get(name.tr('-', '_'))
assert_not_nil(klass.new)
silence_warnings do
%w{MD4 MD5 RIPEMD160 SHA1 SHA224 SHA256 SHA384 SHA512}.each do |name|
assert_not_nil(OpenSSL::Digest.new(name))
klass = OpenSSL::Digest.const_get(name.tr('-', '_'))
assert_not_nil(klass.new)
end
end
end

Expand Down Expand Up @@ -118,14 +120,6 @@ def test_digest_by_oid_and_name_sha2
check_digest(OpenSSL::ASN1::ObjectId.new("SHA512"))
end

def test_openssl_digest
assert_equal OpenSSL::Digest::MD5, OpenSSL::Digest("MD5")

assert_raise NameError do
OpenSSL::Digest("no such digest")
end
end

private

def check_digest(oid)
Expand Down
3 changes: 0 additions & 3 deletions test/openssl/test_ossl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ def test_fixed_length_secure_compare
assert_raise(ArgumentError) { OpenSSL.fixed_length_secure_compare("aaa", "aa") }

assert OpenSSL.fixed_length_secure_compare("aaa", "aaa")
assert OpenSSL.fixed_length_secure_compare(
OpenSSL::Digest.digest('SHA256', "aaa"), OpenSSL::Digest::SHA256.digest("aaa")
)

assert_raise(ArgumentError) { OpenSSL.fixed_length_secure_compare("aaa", "aaaa") }
refute OpenSSL.fixed_length_secure_compare("aaa", "baa")
Expand Down
2 changes: 1 addition & 1 deletion test/openssl/test_pkcs7.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def test_detached_sign

def test_enveloped
certs = [@ee1_cert, @ee2_cert]
cipher = OpenSSL::Cipher::AES.new("128-CBC")
cipher = OpenSSL::Cipher.new("AES-128-CBC")
data = "aaaaa\nbbbbb\nccccc\n"

tmp = OpenSSL::PKCS7.encrypt(certs, data, cipher, OpenSSL::PKCS7::BINARY)
Expand Down
10 changes: 10 additions & 0 deletions test/openssl/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,16 @@ def libressl?(major = nil, minor = nil, fix = nil)
return false unless version
!major || (version.map(&:to_i) <=> [major, minor, fix]) >= 0
end

def silence_warnings(&block)
begin
original_verbosity = $VERBOSE
$VERBOSE = nil
block.call
ensure
$VERBOSE = original_verbosity
end
end
end

class OpenSSL::TestCase < Test::Unit::TestCase
Expand Down

0 comments on commit 4afece2

Please sign in to comment.