Skip to content

Commit

Permalink
convert OpenSSL::ASN1::Sequence value to an array on #to_der
Browse files Browse the repository at this point in the history
In order to allow sequences to sequences. the appropriate TypeError is
thrown if the vale can't be converted into an array, just like in CRuby.
  • Loading branch information
HoneyryderChuck committed Aug 15, 2023
1 parent cb9716c commit bcd8d21
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
20 changes: 9 additions & 11 deletions src/main/java/org/jruby/ext/openssl/ASN1.java
Original file line number Diff line number Diff line change
Expand Up @@ -1953,19 +1953,17 @@ private byte[] setToDER(final ThreadContext context) throws IOException {
private ASN1EncodableVector toASN1EncodableVector(final ThreadContext context) {
final ASN1EncodableVector vec = new ASN1EncodableVector();
final IRubyObject value = value(context);
if ( value instanceof RubyArray ) {
final RubyArray val = (RubyArray) value;
for ( int i = 0; i < val.size(); i++ ) {
if ( addEntry(context, vec, val.entry(i)) ) break;
final RubyArray val;
if (value instanceof RubyArray ) {
val = (RubyArray) value;
} else {
if (!value.respondsTo("to_a")) {
throw context.runtime.newTypeError("can't convert " + value.getMetaClass().getName() + " into Array");
}
val = (RubyArray) value.callMethod(context, "to_a");
}
else {
final int size = RubyInteger.num2int(value.callMethod(context, "size"));
for ( int i = 0; i < size; i++ ) {
final RubyInteger idx = context.runtime.newFixnum(i);
IRubyObject entry = value.callMethod(context, "[]", idx);
if ( addEntry(context, vec, entry) ) break;
}
for ( int i = 0; i < val.size(); i++ ) {
if ( addEntry(context, vec, val.entry(i)) ) break;
}
return vec;
}
Expand Down
8 changes: 8 additions & 0 deletions src/test/ruby/test_asn1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,14 @@ def test_constructive_nesting
assert_equal expected, name.to_der
end

def test_sequence_convert_to_array
data_sequence = ::OpenSSL::ASN1::Sequence([::OpenSSL::ASN1::Integer(0)])
asn1 = ::OpenSSL::ASN1::Sequence(data_sequence)
assert_equals "0\x03\x02\x01\x00" , asn1.to_der

assert_raise(TypeError) { ::OpenSSL::ASN1::Sequence(::OpenSSL::ASN1::Integer(0)).to_der }
end

def test_raw_constructive
eoc = OpenSSL::ASN1::EndOfContent.new
#puts "eoc: #{eoc.inspect}"
Expand Down

0 comments on commit bcd8d21

Please sign in to comment.