Skip to content

Commit 7e0288e

Browse files
committed
ocsp: refactor ossl_ocspcertid_new()
Likewise, let it take a const pointer and not the ownership of the OpenSSL object. This fixes potential memory leak in OpenSSL::OCSP::BasicResponse#status.
1 parent eaabf6d commit 7e0288e

File tree

1 file changed

+44
-53
lines changed

1 file changed

+44
-53
lines changed

ext/openssl/ossl_ocsp.c

Lines changed: 44 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,14 @@ static const rb_data_type_t ossl_ocsp_certid_type = {
149149
* Public
150150
*/
151151
static VALUE
152-
ossl_ocspcertid_new(OCSP_CERTID *cid)
152+
ossl_ocspcid_new(const OCSP_CERTID *cid)
153153
{
154154
VALUE obj = NewOCSPCertId(cOCSPCertId);
155-
SetOCSPCertId(obj, cid);
155+
/* OpenSSL 1.1.1 takes a non-const pointer */
156+
OCSP_CERTID *cid_new = OCSP_CERTID_dup((OCSP_CERTID *)cid);
157+
if (!cid_new)
158+
ossl_raise(eOCSPError, "OCSP_CERTID_dup");
159+
SetOCSPCertId(obj, cid_new);
156160
return obj;
157161
}
158162

@@ -328,21 +332,19 @@ static VALUE
328332
ossl_ocspreq_get_certid(VALUE self)
329333
{
330334
OCSP_REQUEST *req;
331-
OCSP_ONEREQ *one;
332-
OCSP_CERTID *id;
333-
VALUE ary, tmp;
334-
int i, count;
335335

336336
GetOCSPReq(self, req);
337-
count = OCSP_request_onereq_count(req);
338-
ary = (count > 0) ? rb_ary_new() : Qnil;
339-
for(i = 0; i < count; i++){
340-
one = OCSP_request_onereq_get0(req, i);
341-
tmp = NewOCSPCertId(cOCSPCertId);
342-
if(!(id = OCSP_CERTID_dup(OCSP_onereq_get0_id(one))))
343-
ossl_raise(eOCSPError, NULL);
344-
SetOCSPCertId(tmp, id);
345-
rb_ary_push(ary, tmp);
337+
int count = OCSP_request_onereq_count(req);
338+
if (count < 0)
339+
ossl_raise(eOCSPError, "OCSP_request_onereq_count");
340+
if (count == 0)
341+
return Qnil;
342+
343+
VALUE ary = rb_ary_new_capa(count);
344+
for (int i = 0; i < count; i++) {
345+
OCSP_ONEREQ *one = OCSP_request_onereq_get0(req, i);
346+
OCSP_CERTID *cid = OCSP_onereq_get0_id(one);
347+
rb_ary_push(ary, ossl_ocspcid_new(cid));
346348
}
347349

348350
return ary;
@@ -899,42 +901,34 @@ static VALUE
899901
ossl_ocspbres_get_status(VALUE self)
900902
{
901903
OCSP_BASICRESP *bs;
902-
OCSP_SINGLERESP *single;
903-
OCSP_CERTID *cid;
904-
ASN1_TIME *revtime, *thisupd, *nextupd;
905-
int status, reason;
906-
X509_EXTENSION *x509ext;
907-
VALUE ret, ary, ext;
908-
int count, ext_count, i, j;
909904

910905
GetOCSPBasicRes(self, bs);
911-
ret = rb_ary_new();
912-
count = OCSP_resp_count(bs);
913-
for(i = 0; i < count; i++){
914-
single = OCSP_resp_get0(bs, i);
915-
if(!single) continue;
916-
917-
revtime = thisupd = nextupd = NULL;
918-
status = OCSP_single_get0_status(single, &reason, &revtime,
919-
&thisupd, &nextupd);
920-
if(status < 0) continue;
921-
if(!(cid = OCSP_CERTID_dup((OCSP_CERTID *)OCSP_SINGLERESP_get0_id(single)))) /* FIXME */
922-
ossl_raise(eOCSPError, NULL);
923-
ary = rb_ary_new();
924-
rb_ary_push(ary, ossl_ocspcertid_new(cid));
925-
rb_ary_push(ary, INT2NUM(status));
926-
rb_ary_push(ary, INT2NUM(reason));
927-
rb_ary_push(ary, revtime ? asn1time_to_time(revtime) : Qnil);
928-
rb_ary_push(ary, thisupd ? asn1time_to_time(thisupd) : Qnil);
929-
rb_ary_push(ary, nextupd ? asn1time_to_time(nextupd) : Qnil);
930-
ext = rb_ary_new();
931-
ext_count = OCSP_SINGLERESP_get_ext_count(single);
932-
for(j = 0; j < ext_count; j++){
933-
x509ext = OCSP_SINGLERESP_get_ext(single, j);
934-
rb_ary_push(ext, ossl_x509ext_new(x509ext));
935-
}
936-
rb_ary_push(ary, ext);
937-
rb_ary_push(ret, ary);
906+
VALUE ret = rb_ary_new();
907+
int count = OCSP_resp_count(bs);
908+
for (int i = 0; i < count; i++) {
909+
OCSP_SINGLERESP *single = OCSP_resp_get0(bs, i);
910+
ASN1_TIME *revtime, *thisupd, *nextupd;
911+
int reason;
912+
913+
int status = OCSP_single_get0_status(single, &reason, &revtime, &thisupd, &nextupd);
914+
if (status < 0)
915+
ossl_raise(eOCSPError, "OCSP_single_get0_status");
916+
917+
VALUE ary = rb_ary_new();
918+
rb_ary_push(ary, ossl_ocspcid_new(OCSP_SINGLERESP_get0_id(single)));
919+
rb_ary_push(ary, INT2NUM(status));
920+
rb_ary_push(ary, INT2NUM(reason));
921+
rb_ary_push(ary, revtime ? asn1time_to_time(revtime) : Qnil);
922+
rb_ary_push(ary, thisupd ? asn1time_to_time(thisupd) : Qnil);
923+
rb_ary_push(ary, nextupd ? asn1time_to_time(nextupd) : Qnil);
924+
VALUE ext = rb_ary_new();
925+
int ext_count = OCSP_SINGLERESP_get_ext_count(single);
926+
for (int j = 0; j < ext_count; j++) {
927+
X509_EXTENSION *x509ext = OCSP_SINGLERESP_get_ext(single, j);
928+
rb_ary_push(ext, ossl_x509ext_new(x509ext));
929+
}
930+
rb_ary_push(ary, ext);
931+
rb_ary_push(ret, ary);
938932
}
939933

940934
return ret;
@@ -1225,12 +1219,9 @@ static VALUE
12251219
ossl_ocspsres_get_certid(VALUE self)
12261220
{
12271221
OCSP_SINGLERESP *sres;
1228-
OCSP_CERTID *id;
12291222

12301223
GetOCSPSingleRes(self, sres);
1231-
id = OCSP_CERTID_dup((OCSP_CERTID *)OCSP_SINGLERESP_get0_id(sres)); /* FIXME */
1232-
1233-
return ossl_ocspcertid_new(id);
1224+
return ossl_ocspcid_new(OCSP_SINGLERESP_get0_id(sres));
12341225
}
12351226

12361227
/*

0 commit comments

Comments
 (0)