@@ -1336,6 +1336,10 @@ boolean isEOC() {
1336
1336
return "EndOfContent" .equals ( getClassBaseName () );
1337
1337
}
1338
1338
1339
+ IRubyObject tagging () {
1340
+ return getInstanceVariable ("@tagging" );
1341
+ }
1342
+
1339
1343
boolean isExplicitTagging () { return ! isImplicitTagging (); }
1340
1344
1341
1345
boolean isImplicitTagging () { return true ; }
@@ -1364,19 +1368,24 @@ ASN1Encodable toASN1(final ThreadContext context) {
1364
1368
final ASN1TaggedObject toASN1TaggedObject (final ThreadContext context ) {
1365
1369
final int tag = getTag (context );
1366
1370
1367
- final IRubyObject val = callMethod (context , "value" );
1368
- if ( val instanceof RubyArray ) {
1369
- final RubyArray arr = (RubyArray ) val ;
1371
+ final IRubyObject value = callMethod (context , "value" );
1372
+ if (value instanceof RubyArray ) {
1373
+ final RubyArray arr = (RubyArray ) value ;
1370
1374
assert ! arr .isEmpty ();
1371
1375
1372
1376
ASN1EncodableVector vec = new ASN1EncodableVector ();
1373
- for ( final IRubyObject obj : arr .toJavaArray () ) {
1377
+ for (final IRubyObject obj : arr .toJavaArray ()) {
1374
1378
ASN1Encodable data = ((ASN1Data ) obj ).toASN1 (context );
1375
- if ( data == null ) break ; vec .add ( data );
1379
+ if ( data == null ) break ;
1380
+ vec .add ( data );
1376
1381
}
1377
1382
return new DERTaggedObject (isExplicitTagging (), tag , new DERSequence (vec ));
1378
1383
}
1379
- return new DERTaggedObject (isExplicitTagging (), tag , ((ASN1Data ) val ).toASN1 (context ));
1384
+
1385
+ if (!(value instanceof ASN1Data )) {
1386
+ throw new UnsupportedOperationException ("toASN1 " + inspect () + " value: " + value .inspect () + " (" + value .getMetaClass () + ")" );
1387
+ }
1388
+ return new DERTaggedObject (isExplicitTagging (), tag , ((ASN1Data ) value ).toASN1 (context ));
1380
1389
}
1381
1390
1382
1391
@ JRubyMethod
@@ -1559,9 +1568,13 @@ static void initializeImpl(final ThreadContext context,
1559
1568
self .setInstanceVariable ("@indefinite_length" , runtime .getFalse ());
1560
1569
}
1561
1570
1571
+ boolean isTagged () {
1572
+ return !tagging ().isNil ();
1573
+ }
1574
+
1562
1575
@ Override
1563
1576
boolean isExplicitTagging () {
1564
- return "EXPLICIT" .equals ( getInstanceVariable ( "@ tagging" ).toString () );
1577
+ return "EXPLICIT" .equals ( tagging ( ).toString () );
1565
1578
}
1566
1579
1567
1580
@ Override
@@ -1581,19 +1594,16 @@ byte[] toDER(final ThreadContext context) throws IOException {
1581
1594
return toASN1 (context ).toASN1Primitive ().getEncoded (ASN1Encoding .DER );
1582
1595
}
1583
1596
1584
- /*
1585
- private static final Class DERBooleanClass;
1586
- static {
1587
- Class klass;
1588
- try {
1589
- klass = Class.forName("org.bouncycastle.asn1.DERBoolean");
1590
- }
1591
- catch(ClassNotFoundException e) { klass = null; }
1592
- DERBooleanClass = klass;
1593
- } */
1594
-
1595
1597
@ Override
1596
1598
ASN1Encodable toASN1 (final ThreadContext context ) {
1599
+ final ASN1Encodable primitive = toASN1Primitive (context );
1600
+ if (isTagged ()) {
1601
+ return new DERTaggedObject (isExplicitTagging (), getTagClass (context ), getTag (context ), primitive );
1602
+ }
1603
+ return primitive ;
1604
+ }
1605
+
1606
+ private ASN1Encodable toASN1Primitive (final ThreadContext context ) {
1597
1607
Class <? extends ASN1Encodable > type = typeClass ( getMetaClass () );
1598
1608
if ( type == null ) {
1599
1609
final int tag = getTag (context );
@@ -1691,9 +1701,9 @@ ASN1Encodable toASN1(final ThreadContext context) {
1691
1701
}
1692
1702
1693
1703
if (isDebug (context .runtime )) {
1694
- debug (this + " toASN1() could not handle class " + getMetaClass () + " and value " + val .inspect () + " (" + val .getClass (). getName () + ")" );
1704
+ debug (this + " toASN1() could not handle class " + getMetaClass () + " and value: " + val .inspect () + " (" + val .getMetaClass () + ")" );
1695
1705
}
1696
- throw context . runtime . newNotImplementedError ( "unimplemented method called: OpenSSL::ASN1Data#toASN1 (" + type + ")" );
1706
+ throw new UnsupportedOperationException ( " OpenSSL::ASN1Data#toASN1 (" + type + ") not implemented " ); // should not happen
1697
1707
}
1698
1708
1699
1709
private static char [] toBMPChars (final ByteList string ) {
@@ -1785,15 +1795,18 @@ private boolean isInfiniteLength() {
1785
1795
return getInstanceVariable ("@indefinite_length" ).isTrue ();
1786
1796
}
1787
1797
1798
+ private boolean isTagged () {
1799
+ return !tagging ().isNil ();
1800
+ }
1801
+
1788
1802
@ Override
1789
1803
boolean isExplicitTagging () {
1790
- IRubyObject tagging = getInstanceVariable ("@tagging" );
1791
- return tagging .isNil () || "EXPLICIT" .equals ( tagging .toString () );
1804
+ return "EXPLICIT" .equals ( tagging ().toString () ); // nil.toString() == ""
1792
1805
}
1793
1806
1794
1807
@ Override
1795
1808
boolean isImplicitTagging () {
1796
- return "IMPLICIT" .equals ( getInstanceVariable ( "@ tagging" ).toString () );
1809
+ return "IMPLICIT" .equals ( tagging ( ).toString () );
1797
1810
}
1798
1811
1799
1812
@ Override
@@ -1940,8 +1953,7 @@ public ASN1Primitive toASN1Primitive() {
1940
1953
1941
1954
}
1942
1955
1943
- private static boolean addEntry (final ThreadContext context ,
1944
- final ASN1EncodableVector vec , final IRubyObject entry ) {
1956
+ private static boolean addEntry (final ThreadContext context , final ASN1EncodableVector vec , final IRubyObject entry ) {
1945
1957
try {
1946
1958
if ( entry instanceof Constructive ) {
1947
1959
final Constructive constructive = (Constructive ) entry ;
0 commit comments