From f511a309aa2b3744ba5c4f61ad8ed1ecb51ca06d 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 | 13 +++++++----- 2 files changed, 17 insertions(+), 16 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..3047b47e 100644 --- a/src/test/ruby/test_asn1.rb +++ b/src/test/ruby/test_asn1.rb @@ -56,11 +56,6 @@ def test_encode_nil assert_raise(TypeError) { OpenSSL::ASN1::Integer.new(nil).to_der } assert_raise(TypeError) { OpenSSL::ASN1::Boolean.new(nil).to_der } - - # NOTE: MRI does not raise - assert_raise(NoMethodError) { OpenSSL::ASN1::Set.new(nil).to_der } - # NOTE: MRI does not raise - assert_raise(NoMethodError) { OpenSSL::ASN1::Sequence.new(nil).to_der } end def test_instantiate @@ -197,6 +192,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_equal "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}"