Skip to content

Commit

Permalink
[fix] encoding of ASN1::Null primitive to_der
Browse files Browse the repository at this point in the history
  • Loading branch information
kares committed May 8, 2024
1 parent 10b44bb commit c201ae3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
8 changes: 6 additions & 2 deletions src/main/java/org/jruby/ext/openssl/ASN1.java
Original file line number Diff line number Diff line change
Expand Up @@ -1506,7 +1506,7 @@ public Primitive(Ruby runtime, RubyClass type) {
@Override
@JRubyMethod
public IRubyObject to_der(final ThreadContext context) {
if ( value(context).isNil() ) {
if ( value(context).isNil() && !isNull() ) {
// MRI compatibility but avoids Java exceptions as well e.g.
// Java::JavaLang::NumberFormatException
// java.math.BigInteger.<init>(BigInteger.java:296)
Expand Down Expand Up @@ -1596,7 +1596,7 @@ boolean isExplicitTagging() {

@Override
boolean isImplicitTagging() {
IRubyObject tagging = getInstanceVariable("@tagging");
IRubyObject tagging = tagging();
if ( tagging.isNil() ) return true;
return "IMPLICIT".equals( tagging.toString() );
}
Expand All @@ -1606,6 +1606,10 @@ boolean isEOC() {
return false;
}

private boolean isNull() {
return "Null".equals(getMetaClass().getRealClass().getBaseName());
}

@Override
byte[] toDER(final ThreadContext context) throws IOException {
return toASN1(context).toASN1Primitive().getEncoded(ASN1Encoding.DER);
Expand Down
16 changes: 6 additions & 10 deletions src/test/ruby/test_asn1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,7 @@ def test_encode_nested_set_to_der
end

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

def test_sequence
encode_decode_test B(%w{ 30 00 }), OpenSSL::ASN1::Sequence.new([])
# TODO: Import Issue
# TypeError: nil value
#encode_decode_test B(%w{ 30 07 05 00 30 00 04 01 00 }), OpenSSL::ASN1::Sequence.new([
# OpenSSL::ASN1::Null.new(nil),
# OpenSSL::ASN1::Sequence.new([]),
# OpenSSL::ASN1::OctetString.new(B(%w{ 00 }))
#])
encode_decode_test B(%w{ 30 07 05 00 30 00 04 01 00 }), OpenSSL::ASN1::Sequence.new([
OpenSSL::ASN1::Null.new(nil),
OpenSSL::ASN1::Sequence.new([]),
OpenSSL::ASN1::OctetString.new(B(%w{ 00 }))
])

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

0 comments on commit c201ae3

Please sign in to comment.