From bcd8d21ee2e9b91d6d7a989cd807cc19f8556816 Mon Sep 17 00:00:00 2001 From: HoneyryderChuck Date: Tue, 15 Aug 2023 23:32:40 +0100 Subject: [PATCH] convert OpenSSL::ASN1::Sequence value to an array on #to_der 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. --- src/main/java/org/jruby/ext/openssl/ASN1.java | 20 +++++++++---------- src/test/ruby/test_asn1.rb | 8 ++++++++ 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/jruby/ext/openssl/ASN1.java b/src/main/java/org/jruby/ext/openssl/ASN1.java index 0bf264f0..11c03136 100644 --- a/src/main/java/org/jruby/ext/openssl/ASN1.java +++ b/src/main/java/org/jruby/ext/openssl/ASN1.java @@ -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; } diff --git a/src/test/ruby/test_asn1.rb b/src/test/ruby/test_asn1.rb index a648390f..082901bf 100644 --- a/src/test/ruby/test_asn1.rb +++ b/src/test/ruby/test_asn1.rb @@ -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}"