Skip to content

Commit

Permalink
[fix] ASN1Data encoding with Array primitive value (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
kares committed May 5, 2024
1 parent 510350e commit 46e5f87
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
21 changes: 7 additions & 14 deletions src/main/java/org/jruby/ext/openssl/ASN1.java
Original file line number Diff line number Diff line change
Expand Up @@ -1357,21 +1357,14 @@ final ASN1TaggedObject toASN1TaggedObject(final ThreadContext context) {
final IRubyObject val = callMethod(context, "value");
if ( val instanceof RubyArray ) {
final RubyArray arr = (RubyArray) val;
if ( arr.size() > 1 ) {
ASN1EncodableVector vec = new ASN1EncodableVector();
for ( final IRubyObject obj : arr.toJavaArray() ) {
ASN1Encodable data = ((ASN1Data) obj).toASN1(context);
if ( data == null ) break; vec.add( data );
}
return new DERTaggedObject(isExplicitTagging(), tag, new DERSequence(vec));
}
else if ( arr.size() == 1 ) {
ASN1Encodable data = ((ASN1Data) arr.entry(0)).toASN1(context);
return new DERTaggedObject(isExplicitTagging(), tag, data);
}
else {
throw new IllegalStateException("empty array detected");
assert ! arr.isEmpty();

ASN1EncodableVector vec = new ASN1EncodableVector();
for ( final IRubyObject obj : arr.toJavaArray() ) {
ASN1Encodable data = ((ASN1Data) obj).toASN1(context);
if ( data == null ) break; vec.add( data );
}
return new DERTaggedObject(isExplicitTagging(), tag, new DERSequence(vec));
}
return new DERTaggedObject(isExplicitTagging(), tag, ((ASN1Data) val).toASN1(context));
}
Expand Down
24 changes: 24 additions & 0 deletions src/test/ruby/test_asn1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,30 @@ def test_encode_nil
assert_raise(TypeError) { OpenSSL::ASN1::Boolean.new(nil).to_der }
end

def test_encode_data_integer
data = OpenSSL::ASN1::ASN1Data.new([OpenSSL::ASN1::Integer.new(90)], 1, :CONTEXT_SPECIFIC)
der = data.to_der
assert_equal "\xA1\x03\x02\x01Z", der

dec = OpenSSL::ASN1.decode(der)
# #<OpenSSL::ASN1::ASN1Data:0x000077be141e1e60
# @indefinite_length=false,
# @tag=1,
# @tag_class=:CONTEXT_SPECIFIC,
# @value=[#<OpenSSL::ASN1::Integer:0x000077be141e1e88 @indefinite_length=false, @tag=2, @tag_class=:UNIVERSAL, @tagging=nil, @value=#<OpenSSL::BN 1>>]>
assert_equal OpenSSL::ASN1::ASN1Data, dec.class
assert_equal :CONTEXT_SPECIFIC, dec.tag_class
assert_equal 1, dec.tag

assert_equal Array, dec.value.class
assert_equal 1, dec.value.size
int = dec.value[0]
assert_equal OpenSSL::ASN1::Integer, int.class
assert_equal 2, int.tag
assert_equal :UNIVERSAL, int.tag_class
assert_equal OpenSSL::BN.new(90), int.value
end

def test_object_identifier
encode_decode_test B(%w{ 06 01 00 }), OpenSSL::ASN1::ObjectId.new("0.0".b)
encode_decode_test B(%w{ 06 01 28 }), OpenSSL::ASN1::ObjectId.new("1.0".b)
Expand Down

0 comments on commit 46e5f87

Please sign in to comment.