Skip to content

additional buffer argument #170

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
Oct 17, 2018
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
14 changes: 13 additions & 1 deletion src/main/java/org/jruby/ext/openssl/Cipher.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.runtime.Visibility;
import org.jruby.util.ByteList;
import org.jruby.util.TypeConverter;

import static org.jruby.ext.openssl.OpenSSL.*;

Expand Down Expand Up @@ -1104,6 +1105,11 @@ private String getCipherAlgorithm() {

@JRubyMethod
public IRubyObject update(final ThreadContext context, final IRubyObject arg) {
return update(context, arg, null);
}

@JRubyMethod
public IRubyObject update(final ThreadContext context, final IRubyObject arg, IRubyObject buffer) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a @JRubyMethod buffer can be any Ruby type. So you need to do something like TypeConverter.checkStringType.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks

final Ruby runtime = context.runtime;

if ( isDebug(runtime) ) dumpVars( runtime.getOut(), "update()" );
Expand Down Expand Up @@ -1143,7 +1149,13 @@ public IRubyObject update(final ThreadContext context, final IRubyObject arg) {
debugStackTrace( runtime, e );
throw newCipherError(runtime, e);
}
return RubyString.newString(runtime, str);

if( buffer == null ) {
return RubyString.newString(runtime, str);
} else {
buffer = TypeConverter.convertToType(buffer, context.runtime.getString(), "to_str", true);
return ((RubyString) buffer).replace(RubyString.newString(runtime, str));
}
}

@JRubyMethod(name = "<<")
Expand Down
23 changes: 23 additions & 0 deletions src/test/ruby/test_cipher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -475,4 +475,27 @@ def test_encrypt_aes_cfb_20_incompatibility
end
end

def test_encrypt_aes_256_cbc_modifies_buffer
cipher = OpenSSL::Cipher.new("AES-256-CBC")
cipher.key = "a" * 32
cipher.encrypt
buffer = ''
actual = cipher.update('bar' * 10, buffer)
if jruby?
expected = "\xE6\xD3Y\fc\xEE\xBA\xB2*\x0Fr\xD1\xC2b\x03\xD0"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe a bug?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ahorek I just saw this. I don't know. I would expect the two impls to generate the same data.

else
expected = "8\xA7\xBE\xB1\xAE\x88j\xCB\xA3\xE9j\x00\xD2W_\x91"
end
assert_equal actual, expected
assert_equal buffer, expected
end

def test_encrypt_aes_256_cbc_invalid_buffer
cipher = OpenSSL::Cipher.new("AES-256-CBC")
cipher.key = "a" * 32
cipher.encrypt
buffer = Object.new
assert_raise(TypeError) { cipher.update('bar' * 10, buffer) }
end

end