@@ -934,7 +934,7 @@ private static RubyString name(final ThreadContext context, IRubyObject value,
934
934
} // ObjectId
935
935
936
936
static IRubyObject decodeObject (final ThreadContext context ,
937
- final RubyModule ASN1 , final org . bouncycastle . asn1 . ASN1Encodable obj )
937
+ final RubyModule ASN1 , final ASN1Encodable obj )
938
938
throws IOException , IllegalArgumentException {
939
939
final Ruby runtime = context .runtime ;
940
940
@@ -1046,20 +1046,34 @@ else if ( obj instanceof ASN1GraphicString ) {
1046
1046
1047
1047
if (obj instanceof ASN1TaggedObject ) {
1048
1048
final ASN1TaggedObject taggedObj = (ASN1TaggedObject ) obj ;
1049
- if (taggedObj .getTagClass () == BERTags .APPLICATION ) {
1050
- IRubyObject tag = runtime .newFixnum ( taggedObj .getTagNo () );
1051
- IRubyObject tag_class = runtime .newSymbol ("APPLICATION" );
1052
- final ASN1Sequence sequence = (ASN1Sequence ) taggedObj .getBaseUniversal (false , SEQUENCE );
1053
- @ SuppressWarnings ("unchecked" )
1049
+ // IRubyObject val = decodeObject(context, ASN1, taggedObj.getBaseObject());
1050
+ IRubyObject tag = runtime .newFixnum ( taggedObj .getTagNo () );
1051
+ IRubyObject tag_class ;
1052
+ switch (taggedObj .getTagClass ()) {
1053
+ case BERTags .PRIVATE :
1054
+ tag_class = runtime .newSymbol ("PRIVATE" );
1055
+ break ;
1056
+ case BERTags .APPLICATION :
1057
+ tag_class = runtime .newSymbol ("APPLICATION" );
1058
+ break ;
1059
+ case BERTags .CONTEXT_SPECIFIC :
1060
+ tag_class = runtime .newSymbol ("CONTEXT_SPECIFIC" );
1061
+ break ;
1062
+ default :
1063
+ tag_class = runtime .newSymbol ("UNIVERSAL" );
1064
+ break ;
1065
+ }
1066
+
1067
+ final ASN1Encodable taggedBaseObj = taggedObj .getBaseObject ();
1068
+
1069
+ // TODO: how to infer that this tagged object is constructed
1070
+ if (taggedBaseObj instanceof ASN1Sequence ) {
1071
+ final ASN1Sequence sequence = (ASN1Sequence ) taggedBaseObj ;
1054
1072
final RubyArray valArr = decodeObjects (context , ASN1 , sequence .getObjects ());
1055
1073
return ASN1 .getClass ("ASN1Data" ).newInstance (context , new IRubyObject [] { valArr , tag , tag_class }, Block .NULL_BLOCK );
1056
- } else {
1057
- IRubyObject val = decodeObject (context , ASN1 , taggedObj .getBaseObject ());
1058
- IRubyObject tag = runtime .newFixnum ( taggedObj .getTagNo () );
1059
- IRubyObject tag_class = runtime .newSymbol ("CONTEXT_SPECIFIC" );
1060
- final RubyArray valArr = runtime .newArray (val );
1061
- return ASN1 .getClass ("ASN1Data" ).newInstance (context , new IRubyObject [] { valArr , tag , tag_class }, Block .NULL_BLOCK );
1062
1074
}
1075
+
1076
+ return ASN1 .getClass ("ASN1Data" ).newInstance (context , new IRubyObject [] { decodeObject (context , ASN1 , taggedBaseObj ).callMethod (context , "value" ), tag , tag_class }, Block .NULL_BLOCK );
1063
1077
}
1064
1078
1065
1079
if ( obj instanceof ASN1Sequence ) {
@@ -1367,27 +1381,47 @@ ASN1Encodable toASN1(final ThreadContext context) {
1367
1381
1368
1382
final ASN1TaggedObject toASN1TaggedObject (final ThreadContext context ) {
1369
1383
final int tag = getTag (context );
1384
+ final RubyModule ASN1 = _ASN1 (context .runtime );
1385
+
1386
+ IRubyObject value = callMethod (context , "value" );
1387
+ final IRubyObject tagClass = callMethod (context , "tag_class" );
1388
+ int berTag ;
1389
+ String tagClassName = tagClass .toString ();
1390
+
1391
+ if (tagClassName .equals ("PRIVATE" )) {
1392
+ berTag = BERTags .PRIVATE ;
1393
+ } else if (tagClassName .equals ("APPLICATION" )) {
1394
+ berTag = BERTags .APPLICATION ;
1395
+ } else if (tagClassName .equals ("CONTEXT_SPECIFIC" )) {
1396
+ berTag = BERTags .CONTEXT_SPECIFIC ;
1397
+ } else {
1398
+ berTag = BERTags .UNIVERSAL ;
1399
+ }
1370
1400
1371
- final IRubyObject value = callMethod (context , "value" );
1372
- if (value instanceof RubyArray ) {
1401
+ if ( value instanceof RubyArray ) {
1373
1402
final RubyArray arr = (RubyArray ) value ;
1374
1403
assert ! arr .isEmpty ();
1375
1404
1376
1405
ASN1EncodableVector vec = new ASN1EncodableVector ();
1377
- for (final IRubyObject obj : arr .toJavaArray ()) {
1406
+ for ( final IRubyObject obj : arr .toJavaArray () ) {
1378
1407
ASN1Encodable data = ((ASN1Data ) obj ).toASN1 (context );
1379
1408
if ( data == null ) break ;
1380
1409
vec .add ( data );
1381
1410
}
1382
- return new DERTaggedObject (isExplicitTagging (), tag , new DERSequence (vec ));
1411
+
1412
+ // TODO: it's not possible to generate a constructed tagged object, bc raises exception
1413
+ // berTag = berTag | BERTags.CONSTRUCTED;
1414
+ return new DERTaggedObject (isExplicitTagging (), berTag , tag , new DERSequence (vec ));
1383
1415
}
1384
1416
1385
1417
if (!(value instanceof ASN1Data )) {
1386
1418
throw new UnsupportedOperationException ("toASN1 " + inspect () + " value: " + value .inspect () + " (" + value .getMetaClass () + ")" );
1387
1419
}
1388
- return new DERTaggedObject (isExplicitTagging (), tag , ((ASN1Data ) value ).toASN1 (context ));
1420
+
1421
+ return new DERTaggedObject (isExplicitTagging (), berTag , tag , ((ASN1Data ) value ).toASN1 (context ));
1389
1422
}
1390
1423
1424
+
1391
1425
@ JRubyMethod
1392
1426
public IRubyObject to_der (final ThreadContext context ) {
1393
1427
try {
0 commit comments