Skip to content

Commit 46e5f87

Browse files
committed
[fix] ASN1Data encoding with Array primitive value (#119)
1 parent 510350e commit 46e5f87

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

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

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,21 +1357,14 @@ final ASN1TaggedObject toASN1TaggedObject(final ThreadContext context) {
13571357
final IRubyObject val = callMethod(context, "value");
13581358
if ( val instanceof RubyArray ) {
13591359
final RubyArray arr = (RubyArray) val;
1360-
if ( arr.size() > 1 ) {
1361-
ASN1EncodableVector vec = new ASN1EncodableVector();
1362-
for ( final IRubyObject obj : arr.toJavaArray() ) {
1363-
ASN1Encodable data = ((ASN1Data) obj).toASN1(context);
1364-
if ( data == null ) break; vec.add( data );
1365-
}
1366-
return new DERTaggedObject(isExplicitTagging(), tag, new DERSequence(vec));
1367-
}
1368-
else if ( arr.size() == 1 ) {
1369-
ASN1Encodable data = ((ASN1Data) arr.entry(0)).toASN1(context);
1370-
return new DERTaggedObject(isExplicitTagging(), tag, data);
1371-
}
1372-
else {
1373-
throw new IllegalStateException("empty array detected");
1360+
assert ! arr.isEmpty();
1361+
1362+
ASN1EncodableVector vec = new ASN1EncodableVector();
1363+
for ( final IRubyObject obj : arr.toJavaArray() ) {
1364+
ASN1Encodable data = ((ASN1Data) obj).toASN1(context);
1365+
if ( data == null ) break; vec.add( data );
13741366
}
1367+
return new DERTaggedObject(isExplicitTagging(), tag, new DERSequence(vec));
13751368
}
13761369
return new DERTaggedObject(isExplicitTagging(), tag, ((ASN1Data) val).toASN1(context));
13771370
}

src/test/ruby/test_asn1.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,30 @@ def test_encode_nil
260260
assert_raise(TypeError) { OpenSSL::ASN1::Boolean.new(nil).to_der }
261261
end
262262

263+
def test_encode_data_integer
264+
data = OpenSSL::ASN1::ASN1Data.new([OpenSSL::ASN1::Integer.new(90)], 1, :CONTEXT_SPECIFIC)
265+
der = data.to_der
266+
assert_equal "\xA1\x03\x02\x01Z", der
267+
268+
dec = OpenSSL::ASN1.decode(der)
269+
# #<OpenSSL::ASN1::ASN1Data:0x000077be141e1e60
270+
# @indefinite_length=false,
271+
# @tag=1,
272+
# @tag_class=:CONTEXT_SPECIFIC,
273+
# @value=[#<OpenSSL::ASN1::Integer:0x000077be141e1e88 @indefinite_length=false, @tag=2, @tag_class=:UNIVERSAL, @tagging=nil, @value=#<OpenSSL::BN 1>>]>
274+
assert_equal OpenSSL::ASN1::ASN1Data, dec.class
275+
assert_equal :CONTEXT_SPECIFIC, dec.tag_class
276+
assert_equal 1, dec.tag
277+
278+
assert_equal Array, dec.value.class
279+
assert_equal 1, dec.value.size
280+
int = dec.value[0]
281+
assert_equal OpenSSL::ASN1::Integer, int.class
282+
assert_equal 2, int.tag
283+
assert_equal :UNIVERSAL, int.tag_class
284+
assert_equal OpenSSL::BN.new(90), int.value
285+
end
286+
263287
def test_object_identifier
264288
encode_decode_test B(%w{ 06 01 00 }), OpenSSL::ASN1::ObjectId.new("0.0".b)
265289
encode_decode_test B(%w{ 06 01 28 }), OpenSSL::ASN1::ObjectId.new("1.0".b)

0 commit comments

Comments
 (0)