diff --git a/src/main/java/org/jruby/ext/openssl/ASN1.java b/src/main/java/org/jruby/ext/openssl/ASN1.java index 00551268..840b06e5 100644 --- a/src/main/java/org/jruby/ext/openssl/ASN1.java +++ b/src/main/java/org/jruby/ext/openssl/ASN1.java @@ -1357,21 +1357,14 @@ final ASN1TaggedObject toASN1TaggedObject(final ThreadContext context) { final IRubyObject val = callMethod(context, "value"); if ( val instanceof RubyArray ) { final RubyArray arr = (RubyArray) val; - if ( arr.size() > 1 ) { - ASN1EncodableVector vec = new ASN1EncodableVector(); - for ( final IRubyObject obj : arr.toJavaArray() ) { - ASN1Encodable data = ((ASN1Data) obj).toASN1(context); - if ( data == null ) break; vec.add( data ); - } - return new DERTaggedObject(isExplicitTagging(), tag, new DERSequence(vec)); - } - else if ( arr.size() == 1 ) { - ASN1Encodable data = ((ASN1Data) arr.entry(0)).toASN1(context); - return new DERTaggedObject(isExplicitTagging(), tag, data); - } - else { - throw new IllegalStateException("empty array detected"); + assert ! arr.isEmpty(); + + ASN1EncodableVector vec = new ASN1EncodableVector(); + for ( final IRubyObject obj : arr.toJavaArray() ) { + ASN1Encodable data = ((ASN1Data) obj).toASN1(context); + if ( data == null ) break; vec.add( data ); } + return new DERTaggedObject(isExplicitTagging(), tag, new DERSequence(vec)); } return new DERTaggedObject(isExplicitTagging(), tag, ((ASN1Data) val).toASN1(context)); } diff --git a/src/test/ruby/test_asn1.rb b/src/test/ruby/test_asn1.rb index 282b2a4d..4a9dd285 100644 --- a/src/test/ruby/test_asn1.rb +++ b/src/test/ruby/test_asn1.rb @@ -260,6 +260,30 @@ def test_encode_nil assert_raise(TypeError) { OpenSSL::ASN1::Boolean.new(nil).to_der } end + def test_encode_data_integer + data = OpenSSL::ASN1::ASN1Data.new([OpenSSL::ASN1::Integer.new(90)], 1, :CONTEXT_SPECIFIC) + der = data.to_der + assert_equal "\xA1\x03\x02\x01Z", der + + dec = OpenSSL::ASN1.decode(der) + # #>]> + assert_equal OpenSSL::ASN1::ASN1Data, dec.class + assert_equal :CONTEXT_SPECIFIC, dec.tag_class + assert_equal 1, dec.tag + + assert_equal Array, dec.value.class + assert_equal 1, dec.value.size + int = dec.value[0] + assert_equal OpenSSL::ASN1::Integer, int.class + assert_equal 2, int.tag + assert_equal :UNIVERSAL, int.tag_class + assert_equal OpenSSL::BN.new(90), int.value + end + def test_object_identifier encode_decode_test B(%w{ 06 01 00 }), OpenSSL::ASN1::ObjectId.new("0.0".b) encode_decode_test B(%w{ 06 01 28 }), OpenSSL::ASN1::ObjectId.new("1.0".b)