@@ -956,7 +956,7 @@ private static RubyString name(final ThreadContext context, IRubyObject value,
956956 } // ObjectId
957957
958958 static IRubyObject decodeObject (final ThreadContext context ,
959- final RubyModule ASN1 , final org . bouncycastle . asn1 . ASN1Encodable obj )
959+ final RubyModule ASN1 , final ASN1Encodable obj )
960960 throws IOException , IllegalArgumentException {
961961 final Ruby runtime = context .runtime ;
962962
@@ -1076,19 +1076,21 @@ else if ( obj instanceof DERBMPString ) {
10761076 final ASN1ApplicationSpecific appSpecific = (ASN1ApplicationSpecific ) obj ;
10771077 IRubyObject tag = runtime .newFixnum ( appSpecific .getApplicationTag () );
10781078 IRubyObject tag_class = runtime .newSymbol ("APPLICATION" );
1079- final ASN1Sequence sequence = (ASN1Sequence ) appSpecific .getObject (SEQUENCE );
1080- @ SuppressWarnings ("unchecked" )
1081- final RubyArray valArr = decodeObjects (context , ASN1 , sequence .getObjects ());
1082- return ASN1 .getClass ("ASN1Data" ).newInstance (context , new IRubyObject [] { valArr , tag , tag_class }, Block .NULL_BLOCK );
1079+ final ASN1TaggedObject taggedObj = (ASN1TaggedObject ) obj ;
1080+ final ASN1Encodable taggedBaseObj = taggedObj .getBaseObject ();
1081+ return ASN1 .getClass ("ASN1Data" ).newInstance (context , new IRubyObject [] { decodeObject (context , ASN1 , taggedBaseObj ).callMethod (context , "value" ), tag , tag_class }, Block .NULL_BLOCK );
10831082 }
10841083
10851084 if ( obj instanceof ASN1TaggedObject ) {
10861085 final ASN1TaggedObject taggedObj = (ASN1TaggedObject ) obj ;
10871086 IRubyObject val = decodeObject (context , ASN1 , taggedObj .getBaseObject ());
10881087 IRubyObject tag = runtime .newFixnum ( taggedObj .getTagNo () );
1089- IRubyObject tag_class = runtime .newSymbol ("CONTEXT_SPECIFIC" );
1090- final RubyArray valArr = runtime .newArray (val );
1091- return ASN1 .getClass ("ASN1Data" ).newInstance (context , new IRubyObject [] { valArr , tag , tag_class }, Block .NULL_BLOCK );
1088+ IRubyObject tag_class = runtime .newSymbol ("CONTEXT_SPECIFIC" );;
1089+ if (BERTags .UNIVERSAL != taggedObj .getTagClass ()) {
1090+ tag_class = runtime .newSymbol ("UNIVERSAL" );
1091+ }
1092+ final ASN1Encodable taggedBaseObj = taggedObj .getBaseObject ();
1093+ return ASN1 .getClass ("ASN1Data" ).newInstance (context , new IRubyObject [] { decodeObject (context , ASN1 , taggedBaseObj ).callMethod (context , "value" ), tag , tag_class }, Block .NULL_BLOCK );
10921094 }
10931095
10941096 if ( obj instanceof ASN1Sequence ) {
@@ -1383,29 +1385,51 @@ ASN1Encodable toASN1(final ThreadContext context) {
13831385
13841386 final ASN1TaggedObject toASN1TaggedObject (final ThreadContext context ) {
13851387 final int tag = getTag (context );
1388+ final RubyModule ASN1 = _ASN1 (context .runtime );
1389+
1390+ IRubyObject val = callMethod (context , "value" );
1391+ final IRubyObject tagClass = callMethod (context , "tag_class" );
1392+ ASN1Encodable obj ;
13861393
1387- final IRubyObject val = callMethod (context , "value" );
13881394 if ( val instanceof RubyArray ) {
13891395 final RubyArray arr = (RubyArray ) val ;
1390- if ( arr .size () > 1 ) {
1391- ASN1EncodableVector vec = new ASN1EncodableVector ();
1392- for ( final IRubyObject obj : arr .toJavaArray () ) {
1393- ASN1Encodable data = ((ASN1Data ) obj ).toASN1 (context );
1394- if ( data == null ) break ; vec .add ( data );
1396+
1397+ if ( arr .size () >= 1 ) {
1398+ ByteArrayOutputStream bytes = new ByteArrayOutputStream ( );
1399+
1400+ for ( final IRubyObject o : arr .toJavaArray () ) {
1401+ try {
1402+ bytes .write (((RubyString ) o ).getBytes ());
1403+ } catch (IOException e ) {
1404+ throw new IllegalStateException (e .getMessage ());
1405+ } catch (ClassCastException e ) {
1406+ throw context .runtime .newTypeError (e .getMessage ());
1407+ }
13951408 }
1396- return new DERTaggedObject (isExplicitTagging (), tag , new DERSequence (vec ));
1409+ obj = new DEROctetString (bytes .toByteArray ());
1410+ } else {
1411+ throw new IllegalStateException ("empty array detected" );
13971412 }
1398- else if ( arr .size () == 1 ) {
1399- ASN1Encodable data = ((ASN1Data ) arr .entry (0 )).toASN1 (context );
1400- return new DERTaggedObject (isExplicitTagging (), tag , data );
1413+ } else {
1414+ try {
1415+ obj = new DEROctetString (((RubyString ) val ).getBytes ());
1416+ } catch (ClassCastException e ) {
1417+ throw context .runtime .newTypeError ("no implicit conversion of " + val .getMetaClass ().getName () + " into String" );
14011418 }
1402- else {
1403- throw new IllegalStateException ("empty array detected" );
1419+ }
1420+
1421+ if (tagClass .toString ().equals ("APPLICATION" )) {
1422+ try {
1423+ return new DERApplicationSpecific (isExplicitTagging (), tag , obj );
1424+ } catch (IOException e ) {
1425+ throw new IllegalStateException (e .getMessage ());
14041426 }
1427+ } else {
1428+ return new DERTaggedObject (isExplicitTagging (), tag , obj );
14051429 }
1406- return new DERTaggedObject (isExplicitTagging (), tag , ((ASN1Data ) val ).toASN1 (context ));
14071430 }
14081431
1432+
14091433 @ JRubyMethod
14101434 public IRubyObject to_der (final ThreadContext context ) {
14111435 try {
0 commit comments