Skip to content

Commit c201ae3

Browse files
committed
[fix] encoding of ASN1::Null primitive to_der
1 parent 10b44bb commit c201ae3

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -1506,7 +1506,7 @@ public Primitive(Ruby runtime, RubyClass type) {
15061506
@Override
15071507
@JRubyMethod
15081508
public IRubyObject to_der(final ThreadContext context) {
1509-
if ( value(context).isNil() ) {
1509+
if ( value(context).isNil() && !isNull() ) {
15101510
// MRI compatibility but avoids Java exceptions as well e.g.
15111511
// Java::JavaLang::NumberFormatException
15121512
// java.math.BigInteger.<init>(BigInteger.java:296)
@@ -1596,7 +1596,7 @@ boolean isExplicitTagging() {
15961596

15971597
@Override
15981598
boolean isImplicitTagging() {
1599-
IRubyObject tagging = getInstanceVariable("@tagging");
1599+
IRubyObject tagging = tagging();
16001600
if ( tagging.isNil() ) return true;
16011601
return "IMPLICIT".equals( tagging.toString() );
16021602
}
@@ -1606,6 +1606,10 @@ boolean isEOC() {
16061606
return false;
16071607
}
16081608

1609+
private boolean isNull() {
1610+
return "Null".equals(getMetaClass().getRealClass().getBaseName());
1611+
}
1612+
16091613
@Override
16101614
byte[] toDER(final ThreadContext context) throws IOException {
16111615
return toASN1(context).toASN1Primitive().getEncoded(ASN1Encoding.DER);

src/test/ruby/test_asn1.rb

+6-10
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,7 @@ def test_encode_nested_set_to_der
245245
end
246246

247247
def test_null
248-
# TODO: Import Issue -- Is this related to the comment below in test_encode_all?
249-
# TypeError: nil value
250-
#encode_decode_test B(%w{ 05 00 }), OpenSSL::ASN1::Null.new(nil)
248+
encode_decode_test B(%w{ 05 00 }), OpenSSL::ASN1::Null.new(nil)
251249
assert_raise(OpenSSL::ASN1::ASN1Error) {
252250
OpenSSL::ASN1.decode(B(%w{ 05 01 00 }))
253251
}
@@ -384,13 +382,11 @@ def test_simple_to_der
384382

385383
def test_sequence
386384
encode_decode_test B(%w{ 30 00 }), OpenSSL::ASN1::Sequence.new([])
387-
# TODO: Import Issue
388-
# TypeError: nil value
389-
#encode_decode_test B(%w{ 30 07 05 00 30 00 04 01 00 }), OpenSSL::ASN1::Sequence.new([
390-
# OpenSSL::ASN1::Null.new(nil),
391-
# OpenSSL::ASN1::Sequence.new([]),
392-
# OpenSSL::ASN1::OctetString.new(B(%w{ 00 }))
393-
#])
385+
encode_decode_test B(%w{ 30 07 05 00 30 00 04 01 00 }), OpenSSL::ASN1::Sequence.new([
386+
OpenSSL::ASN1::Null.new(nil),
387+
OpenSSL::ASN1::Sequence.new([]),
388+
OpenSSL::ASN1::OctetString.new(B(%w{ 00 }))
389+
])
394390

395391
expected = OpenSSL::ASN1::Sequence.new([OpenSSL::ASN1::OctetString.new(B(%w{ 00 }))])
396392
expected.indefinite_length = true

0 commit comments

Comments
 (0)