Skip to content

Commit def8609

Browse files
committed
[fix] ASN1::EndOfContent ancestor hierarchy
also started using @indefinite_length for better compatibility
1 parent 2e7ff4d commit def8609

File tree

1 file changed

+40
-29
lines changed

1 file changed

+40
-29
lines changed

src/main/java/org/jruby/ext/openssl/ASN1.java

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -708,19 +708,22 @@ public static void createASN1(final Ruby runtime, final RubyModule OpenSSL, fina
708708
_ASN1Data.addReadWriteAttribute(context, "value");
709709
_ASN1Data.addReadWriteAttribute(context, "tag");
710710
_ASN1Data.addReadWriteAttribute(context, "tag_class");
711+
_ASN1Data.addReadWriteAttribute(context, "indefinite_length");
712+
_ASN1Data.defineAlias( "infinite_length", "indefinite_length");
713+
_ASN1Data.defineAlias( "infinite_length=", "indefinite_length=");
711714
_ASN1Data.defineAnnotatedMethods(ASN1Data.class);
712715

713716
final ObjectAllocator primitiveAllocator = Primitive.ALLOCATOR;
714717
RubyClass Primitive = ASN1.defineClassUnder("Primitive", _ASN1Data, primitiveAllocator);
715718
Primitive.addReadWriteAttribute(context, "tagging");
716-
Primitive.addReadAttribute(context, "infinite_length");
719+
Primitive.undefineMethod("infinite_length=");
720+
Primitive.undefineMethod("indefinite_length=");
717721
Primitive.defineAnnotatedMethods(Primitive.class);
718722

719723
final ObjectAllocator constructiveAllocator = Constructive.ALLOCATOR;
720724
RubyClass Constructive = ASN1.defineClassUnder("Constructive", _ASN1Data, constructiveAllocator);
721725
Constructive.includeModule( runtime.getModule("Enumerable") );
722726
Constructive.addReadWriteAttribute(context, "tagging");
723-
Constructive.addReadWriteAttribute(context, "infinite_length");
724727
Constructive.defineAnnotatedMethods(Constructive.class);
725728

726729
ASN1.defineClassUnder("Boolean", Primitive, primitiveAllocator); // OpenSSL::ASN1::Boolean <=> value is a Boolean
@@ -747,10 +750,11 @@ public static void createASN1(final Ruby runtime, final RubyModule OpenSSL, fina
747750
ASN1.defineClassUnder("UTCTime", Primitive, primitiveAllocator); // OpenSSL::ASN1::UTCTime <=> value is a Time
748751
ASN1.defineClassUnder("GeneralizedTime", Primitive, primitiveAllocator); // OpenSSL::ASN1::GeneralizedTime <=> value is a Time
749752

750-
ASN1.defineClassUnder("EndOfContent", Primitive, primitiveAllocator); // OpenSSL::ASN1::EndOfContent <=> value is always nil
753+
ASN1.defineClassUnder("EndOfContent", _ASN1Data, asn1DataAllocator). // OpenSSL::ASN1::EndOfContent <=> value is always nil
754+
defineAnnotatedMethods(EndOfContent.class);
751755

752-
RubyClass ObjectId = ASN1.defineClassUnder("ObjectId", Primitive, primitiveAllocator);
753-
ObjectId.defineAnnotatedMethods(ObjectId.class);
756+
ASN1.defineClassUnder("ObjectId", Primitive, primitiveAllocator).
757+
defineAnnotatedMethods(ObjectId.class);
754758

755759
ASN1.defineClassUnder("Sequence", Constructive, Constructive.getAllocator());
756760
ASN1.defineClassUnder("Set", Constructive, Constructive.getAllocator());
@@ -1361,7 +1365,9 @@ private void checkTag(final Ruby runtime, final IRubyObject tag,
13611365
}
13621366
}
13631367

1364-
boolean isEOC() { return false; }
1368+
boolean isEOC() {
1369+
return "EndOfContent".equals( getClassBaseName() );
1370+
}
13651371

13661372
boolean isExplicitTagging() { return ! isImplicitTagging(); }
13671373

@@ -1412,6 +1418,7 @@ public IRubyObject to_der(final ThreadContext context) {
14121418
}
14131419

14141420
byte[] toDER(final ThreadContext context) throws IOException {
1421+
if ( isEOC() ) return new byte[] { 0x00, 0x00 };
14151422
return toASN1(context).toASN1Primitive().getEncoded(ASN1Encoding.DER);
14161423
}
14171424

@@ -1464,6 +1471,26 @@ static void printArray(final PrintStream out, final int indent, final RubyArray
14641471

14651472
}
14661473

1474+
public static class EndOfContent {
1475+
1476+
private EndOfContent() {}
1477+
1478+
@JRubyMethod(visibility = Visibility.PRIVATE)
1479+
public static IRubyObject initialize(final ThreadContext context, final IRubyObject self) {
1480+
final Ruby runtime = context.runtime;
1481+
self.getInstanceVariables().setInstanceVariable("@tag", runtime.newFixnum(0));
1482+
self.getInstanceVariables().setInstanceVariable("@value", RubyString.newEmptyString(context.runtime));
1483+
self.getInstanceVariables().setInstanceVariable("@tag_class", runtime.newSymbol("UNIVERSAL"));
1484+
return self;
1485+
}
1486+
1487+
static IRubyObject newInstance(final ThreadContext context) {
1488+
RubyClass klass = _ASN1(context.runtime).getClass("EndOfContent");
1489+
return klass.newInstance(context, Block.NULL_BLOCK);
1490+
}
1491+
1492+
}
1493+
14671494
public static class Primitive extends ASN1Data {
14681495
private static final long serialVersionUID = 8489625559339190259L;
14691496

@@ -1556,7 +1583,7 @@ static void initializeImpl(final ThreadContext context,
15561583
self.setInstanceVariable("@value", value);
15571584
self.setInstanceVariable("@tag_class", tag_class);
15581585
self.setInstanceVariable("@tagging", tagging);
1559-
self.setInstanceVariable("@infinite_length", runtime.getFalse());
1586+
self.setInstanceVariable("@indefinite_length", runtime.getFalse());
15601587
}
15611588

15621589
@Override
@@ -1573,27 +1600,14 @@ boolean isImplicitTagging() {
15731600

15741601
@Override
15751602
boolean isEOC() {
1576-
return "EndOfContent".equals( getClassBaseName() );
1603+
return false;
15771604
}
15781605

15791606
@Override
15801607
byte[] toDER(final ThreadContext context) throws IOException {
1581-
if ( isEOC() ) return new byte[] { 0x00, 0x00 };
15821608
return toASN1(context).toASN1Primitive().getEncoded(ASN1Encoding.DER);
15831609
}
15841610

1585-
static Primitive newInstance(final ThreadContext context, final String type,
1586-
final IRubyObject value) {
1587-
RubyClass klass = _ASN1(context.runtime).getClass(type);
1588-
final Primitive self = new Primitive(context.runtime, klass);
1589-
if ( value != null ) self.setInstanceVariable("@value", value);
1590-
return self;
1591-
}
1592-
1593-
static Primitive newEndOfContent(final ThreadContext context) {
1594-
return newInstance(context, "EndOfContent", null);
1595-
}
1596-
15971611
/*
15981612
private static final Class DERBooleanClass;
15991613
static {
@@ -1756,7 +1770,7 @@ static Constructive newInfiniteConstructive(final ThreadContext context,
17561770

17571771
final RubyArray values = runtime.newArray(value.size());
17581772
for ( final IRubyObject val : value ) values.append(val);
1759-
// values.append( Primitive.newEndOfContent(context) );
1773+
// values.append( EndOfContent.newInstance(context) );
17601774

17611775
self.setInstanceVariable("@tag", runtime.newFixnum(defaultTag));
17621776
self.setInstanceVariable("@value", values);
@@ -1768,11 +1782,9 @@ static Constructive newInfiniteConstructive(final ThreadContext context,
17681782

17691783
static Constructive setInfiniteLength(final ThreadContext context, final IRubyObject constructive) {
17701784
final Constructive instance = ((Constructive) constructive);
1771-
final IRubyObject eoc = Primitive.newEndOfContent(context);
17721785
final IRubyObject value = instance.value(context);
1773-
if ( value instanceof RubyArray ) ((RubyArray) value).append(eoc);
1774-
else value.callMethod(context, "<<", eoc);
1775-
instance.setInstanceVariable("@infinite_length", context.runtime.getTrue());
1786+
value.callMethod(context, "<<", EndOfContent.newInstance(context));
1787+
instance.setInstanceVariable("@indefinite_length", context.runtime.getTrue());
17761788
return instance;
17771789
}
17781790

@@ -1789,7 +1801,7 @@ private boolean isSet() {
17891801
}
17901802

17911803
private boolean isInfiniteLength() {
1792-
return getInstanceVariable("@infinite_length").isTrue();
1804+
return getInstanceVariable("@indefinite_length").isTrue();
17931805
}
17941806

17951807
@Override
@@ -1838,8 +1850,7 @@ public IRubyObject to_der(final ThreadContext context) {
18381850
if ( rawConstructive() ) { // MRI compatibility
18391851
if ( ! isInfiniteLength() && ! super.value(context).isNil() ) {
18401852
final Ruby runtime = context.runtime;
1841-
throw newASN1Error(runtime, "Constructive shall only be used"
1842-
+ " with infinite length");
1853+
throw newASN1Error(runtime, "Constructive shall only be used with indefinite length");
18431854
}
18441855
}
18451856
return super.to_der(context);

0 commit comments

Comments
 (0)