Skip to content

Fix integer overflow in hash_old #218

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/org/jruby/ext/openssl/X509Name.java
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ public RubyFixnum hash() {

@JRubyMethod
public RubyFixnum hash_old() {
int hash;
long hash;
try {
hash = Name.hashOld( getX500Name() );
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/jruby/ext/openssl/x509store/Name.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ public Name(final X500Name name) {
this.name = name;
}

public static int hashOld(final X500Name name) throws IOException {
public static long hashOld(final X500Name name) throws IOException {
try {
final byte[] bytes = name.getEncoded();
MessageDigest md5 = SecurityHelper.getMessageDigest("MD5");
final byte[] digest = md5.digest(bytes);
int result = 0;
long result = 0;
result |= digest[3] & 0xff; result <<= 8;
result |= digest[2] & 0xff; result <<= 8;
result |= digest[1] & 0xff; result <<= 8;
Expand Down
2 changes: 2 additions & 0 deletions src/test/ruby/x509/test_x509name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ def test_hash_long_name
def test_hash_old
name = OpenSSL::X509::Name.new [['CN', 'nobody'], ['DC', 'example']]
assert_equal 1460400684, name.hash_old
name = OpenSSL::X509::Name.new([['CN', 'foo'], ['DC', 'bar']])
assert_equal 3294068023, name.hash_old
end

end