@@ -956,7 +956,7 @@ private static RubyString name(final ThreadContext context, IRubyObject value,
956
956
} // ObjectId
957
957
958
958
static IRubyObject decodeObject (final ThreadContext context ,
959
- final RubyModule ASN1 , final org . bouncycastle . asn1 . ASN1Encodable obj )
959
+ final RubyModule ASN1 , final ASN1Encodable obj )
960
960
throws IOException , IllegalArgumentException {
961
961
final Ruby runtime = context .runtime ;
962
962
@@ -1076,19 +1076,21 @@ else if ( obj instanceof DERBMPString ) {
1076
1076
final ASN1ApplicationSpecific appSpecific = (ASN1ApplicationSpecific ) obj ;
1077
1077
IRubyObject tag = runtime .newFixnum ( appSpecific .getApplicationTag () );
1078
1078
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 );
1083
1082
}
1084
1083
1085
1084
if ( obj instanceof ASN1TaggedObject ) {
1086
1085
final ASN1TaggedObject taggedObj = (ASN1TaggedObject ) obj ;
1087
1086
IRubyObject val = decodeObject (context , ASN1 , taggedObj .getBaseObject ());
1088
1087
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 );
1092
1094
}
1093
1095
1094
1096
if ( obj instanceof ASN1Sequence ) {
@@ -1383,29 +1385,51 @@ ASN1Encodable toASN1(final ThreadContext context) {
1383
1385
1384
1386
final ASN1TaggedObject toASN1TaggedObject (final ThreadContext context ) {
1385
1387
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 ;
1386
1393
1387
- final IRubyObject val = callMethod (context , "value" );
1388
1394
if ( val instanceof RubyArray ) {
1389
1395
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
+ }
1395
1408
}
1396
- return new DERTaggedObject (isExplicitTagging (), tag , new DERSequence (vec ));
1409
+ obj = new DEROctetString (bytes .toByteArray ());
1410
+ } else {
1411
+ throw new IllegalStateException ("empty array detected" );
1397
1412
}
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" );
1401
1418
}
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 ());
1404
1426
}
1427
+ } else {
1428
+ return new DERTaggedObject (isExplicitTagging (), tag , obj );
1405
1429
}
1406
- return new DERTaggedObject (isExplicitTagging (), tag , ((ASN1Data ) val ).toASN1 (context ));
1407
1430
}
1408
1431
1432
+
1409
1433
@ JRubyMethod
1410
1434
public IRubyObject to_der (final ThreadContext context ) {
1411
1435
try {
0 commit comments