Skip to content

Commit a2b32a4

Browse files
committed
asn1: prohibit indefinite length form for primitive encoding
The setter method #indefinite_length= for OpenSSL::ASN1::Primitive is undef-ed, but we can still set 'indefinite_length' to true illegally when constructing an object with the raw OpenSSL::ASN1::ASN1Data. Indefinite length form is not possible in primitive encoding. Raise an exception in OpenSSL::ASN1::ASN1Data#to_der if specified.
1 parent 400d9ca commit a2b32a4

File tree

2 files changed

+5
-0
lines changed

2 files changed

+5
-0
lines changed

ext/openssl/ossl_asn1.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,8 @@ ossl_asn1data_to_der(VALUE self)
716716
tag_class = ossl_asn1_tag_class(self);
717717
inf_length = ossl_asn1_get_indefinite_length(self);
718718
if (inf_length == Qtrue) {
719+
if (is_cons == 0)
720+
ossl_raise(eASN1Error, "indefinite form used for primitive encoding");
719721
is_cons = 2;
720722
}
721723
if((length = ASN1_object_size(is_cons, RSTRING_LENINT(value), tag)) <= 0)

test/test_asn1.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,9 @@ def test_basic_asn1data
426426
], 1, :APPLICATION)
427427
obj.indefinite_length = true
428428
encode_decode_test B(%w{ 61 80 C2 02 AB CD 00 00 }), obj
429+
obj = OpenSSL::ASN1::ASN1Data.new(B(%w{ AB CD }), 1, :UNIVERSAL)
430+
obj.indefinite_length = true
431+
assert_raise(OpenSSL::ASN1::ASN1Error) { obj.to_der }
429432
end
430433

431434
def test_basic_primitive

0 commit comments

Comments
 (0)