diff --git a/src/main/java/org/jruby/ext/openssl/ASN1.java b/src/main/java/org/jruby/ext/openssl/ASN1.java index a4c52f47..c939e948 100644 --- a/src/main/java/org/jruby/ext/openssl/ASN1.java +++ b/src/main/java/org/jruby/ext/openssl/ASN1.java @@ -1931,19 +1931,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 9b9b0ca0..f82595fe 100644 --- a/src/test/ruby/test_asn1.rb +++ b/src/test/ruby/test_asn1.rb @@ -26,11 +26,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 @@ -167,6 +162,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}"