Skip to content

Commit b29e215

Browse files
no6vrhenium
authored andcommitted
fix segv in Timestamp::{Request,Response,TokenInfo}.new
prevent `ossl_ts_*_free()` from calling when `d2i_TS_*_bio()` failed.
1 parent c12b77f commit b29e215

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

ext/openssl/ossl_ts.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,10 @@ ossl_ts_req_initialize(int argc, VALUE *argv, VALUE self)
205205
in = ossl_obj2bio(&arg);
206206
ts_req = d2i_TS_REQ_bio(in, &ts_req);
207207
BIO_free(in);
208-
if (!ts_req)
208+
if (!ts_req) {
209+
DATA_PTR(self) = NULL;
209210
ossl_raise(eTimestampError, "Error when decoding the timestamp request");
211+
}
210212
DATA_PTR(self) = ts_req;
211213

212214
return self;
@@ -529,8 +531,10 @@ ossl_ts_resp_initialize(VALUE self, VALUE der)
529531
in = ossl_obj2bio(&der);
530532
ts_resp = d2i_TS_RESP_bio(in, &ts_resp);
531533
BIO_free(in);
532-
if (!ts_resp)
534+
if (!ts_resp) {
535+
DATA_PTR(self) = NULL;
533536
ossl_raise(eTimestampError, "Error when decoding the timestamp response");
537+
}
534538
DATA_PTR(self) = ts_resp;
535539

536540
return self;
@@ -871,8 +875,10 @@ ossl_ts_token_info_initialize(VALUE self, VALUE der)
871875
in = ossl_obj2bio(&der);
872876
info = d2i_TS_TST_INFO_bio(in, &info);
873877
BIO_free(in);
874-
if (!info)
878+
if (!info) {
879+
DATA_PTR(self) = NULL;
875880
ossl_raise(eTimestampError, "Error when decoding the timestamp token info");
881+
}
876882
DATA_PTR(self) = info;
877883

878884
return self;

test/openssl/test_ts.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,12 @@ def test_request_encode_decode
181181
assert_equal(42, qer2.nonce)
182182
end
183183

184+
def test_request_invalid_asn1
185+
assert_raise(OpenSSL::Timestamp::TimestampError) do
186+
OpenSSL::Timestamp::Request.new("*" * 44)
187+
end
188+
end
189+
184190
def test_response_constants
185191
assert_equal(0, OpenSSL::Timestamp::Response::GRANTED)
186192
assert_equal(1, OpenSSL::Timestamp::Response::GRANTED_WITH_MODS)
@@ -333,6 +339,12 @@ def test_response_bad_purpose
333339
end
334340
end
335341

342+
def test_response_invalid_asn1
343+
assert_raise(OpenSSL::Timestamp::TimestampError) do
344+
OpenSSL::Timestamp::Response.new("*" * 44)
345+
end
346+
end
347+
336348
def test_no_cert_requested
337349
req = OpenSSL::Timestamp::Request.new
338350
req.algorithm = "SHA1"
@@ -585,6 +597,12 @@ def test_token_info_creation
585597
assert_equal(123, info.nonce)
586598
end
587599

600+
def test_token_info_invalid_asn1
601+
assert_raise(OpenSSL::Timestamp::TimestampError) do
602+
OpenSSL::Timestamp::TokenInfo.new("*" * 44)
603+
end
604+
end
605+
588606
private
589607

590608
def assert_cert expected, actual

0 commit comments

Comments
 (0)