diff --git a/src/main/java/org/jruby/ext/openssl/ASN1.java b/src/main/java/org/jruby/ext/openssl/ASN1.java index fbe08bb9..5ce07456 100644 --- a/src/main/java/org/jruby/ext/openssl/ASN1.java +++ b/src/main/java/org/jruby/ext/openssl/ASN1.java @@ -1390,20 +1390,26 @@ final ASN1TaggedObject toASN1TaggedObject(final ThreadContext context) { if ( arr.size() > 1 ) { ASN1EncodableVector vec = new ASN1EncodableVector(); for ( final IRubyObject obj : arr.toJavaArray() ) { - ASN1Encodable data = ((ASN1Data) obj).toASN1(context); + ASN1Encodable data = ((ASN1OctetString) 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); + ASN1Encodable data = ((ASN1OctetString) arr.entry(0)).toASN1(context); return new DERTaggedObject(isExplicitTagging(), tag, data); } else { throw new IllegalStateException("empty array detected"); } + } else if (val instanceof RubyString || val instanceof java.lang.String) { + return new DERTaggedObject(isExplicitTagging(), tag, ((ASN1OctetString) val).toASN1(context)); + } else if (val instanceof ASN1OctetString) { + return new DERTaggedObject(isExplicitTagging(), tag, val.toASN1(context)); + } else { + throw runtime.newTypeError("no implicit conversion of " + val.getMetaClass().getName() + " into String"); } - return new DERTaggedObject(isExplicitTagging(), tag, ((ASN1Data) val).toASN1(context)); + } @JRubyMethod diff --git a/src/test/ruby/test_asn1.rb b/src/test/ruby/test_asn1.rb index 95f55137..cb697c08 100644 --- a/src/test/ruby/test_asn1.rb +++ b/src/test/ruby/test_asn1.rb @@ -21,6 +21,16 @@ def test_encode_integer assert_equal i, OpenSSL::ASN1.decode(ai.to_der).value end + def test_encode_asn1_data + ai = OpenSSL::ASN1::ASN1Data.new(i = "bla", 0, :APPLICATION) + assert_equal i, OpenSSL::ASN1.decode(ai.to_der).value + + ai = OpenSSL::ASN1::ASN1Data.new(i = ["bla"], 0, :APPLICATION) + assert_equal i, OpenSSL::ASN1.decode(ai.to_der).value + + assert_raise(TypeError) { OpenSSL::ASN1::ASN1Data.new(1).to_der } + end + def test_encode_nil #Primitives raise TypeError, Constructives NoMethodError