Skip to content

Commit 1d202b0

Browse files
committed
asn1: prevent EOC octets from being in the middle of the content
Encoding with indefinite length form produces an invalid encoding if the contents array contains an EOC object in the middle. Raise an exception in that case.
1 parent 27e4bad commit 1d202b0

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

ext/openssl/ossl_asn1.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,6 +1177,11 @@ ossl_asn1cons_to_der(VALUE self)
11771177
for (i = 0; i < RARRAY_LEN(ary); i++) {
11781178
VALUE item = RARRAY_AREF(ary, i);
11791179

1180+
if (indef_len && rb_obj_is_kind_of(item, cASN1EndOfContent)) {
1181+
if (i != RARRAY_LEN(ary) - 1)
1182+
ossl_raise(eASN1Error, "illegal EOC octets in value");
1183+
}
1184+
11801185
item = ossl_to_der_if_possible(item);
11811186
StringValue(item);
11821187
rb_str_append(str, item);

test/test_asn1.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,15 @@ def test_sequence
345345
])
346346
expected.indefinite_length = true
347347
encode_decode_test B(%w{ 30 80 04 01 00 00 00 }), expected
348+
349+
# OpenSSL::ASN1::EndOfContent can only be at the end
350+
obj = OpenSSL::ASN1::Sequence.new([
351+
OpenSSL::ASN1::EndOfContent.new,
352+
OpenSSL::ASN1::OctetString.new(B(%w{ 00 })),
353+
OpenSSL::ASN1::EndOfContent.new,
354+
])
355+
obj.indefinite_length = true
356+
assert_raise(OpenSSL::ASN1::ASN1Error) { obj.to_der }
348357
end
349358

350359
def test_set

0 commit comments

Comments
 (0)