Skip to content

Commit f7114e9

Browse files
authored
Merge pull request #912 from rhenium/ky/refactor-ossl-new-func
Cleanup ossl_*_new() functions
2 parents 2bf1c15 + 54c1c26 commit f7114e9

17 files changed

+108
-150
lines changed

ext/openssl/ossl_bn.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,9 @@ ossl_bn_new(const BIGNUM *bn)
6161
VALUE obj;
6262

6363
obj = NewBN(cBN);
64-
newbn = bn ? BN_dup(bn) : BN_new();
65-
if (!newbn) {
66-
ossl_raise(eBNError, NULL);
67-
}
64+
newbn = BN_dup(bn);
65+
if (!newbn)
66+
ossl_raise(eBNError, "BN_dup");
6867
SetBN(obj, newbn);
6968

7069
return obj;

ext/openssl/ossl_engine.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ ossl_engine_load_privkey(int argc, VALUE *argv, VALUE self)
320320
GetEngine(self, e);
321321
pkey = ENGINE_load_private_key(e, sid, NULL, sdata);
322322
if (!pkey) ossl_raise(eEngineError, NULL);
323-
obj = ossl_pkey_new(pkey);
323+
obj = ossl_pkey_wrap(pkey);
324324
OSSL_PKEY_SET_PRIVATE(obj);
325325

326326
return obj;
@@ -350,7 +350,7 @@ ossl_engine_load_pubkey(int argc, VALUE *argv, VALUE self)
350350
pkey = ENGINE_load_public_key(e, sid, NULL, sdata);
351351
if (!pkey) ossl_raise(eEngineError, NULL);
352352

353-
return ossl_pkey_new(pkey);
353+
return ossl_pkey_wrap(pkey);
354354
}
355355

356356
/*

ext/openssl/ossl_ns_spki.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ ossl_spki_get_public_key(VALUE self)
190190
ossl_raise(eSPKIError, NULL);
191191
}
192192

193-
return ossl_pkey_new(pkey); /* NO DUP - OK */
193+
return ossl_pkey_wrap(pkey);
194194
}
195195

196196
/*

ext/openssl/ossl_ocsp.c

Lines changed: 56 additions & 73 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,48 +901,40 @@ 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;
941935
}
942936

943-
static VALUE ossl_ocspsres_new(OCSP_SINGLERESP *);
937+
static VALUE ossl_ocspsres_new(const OCSP_SINGLERESP *);
944938

945939
/*
946940
* call-seq:
@@ -958,17 +952,10 @@ ossl_ocspbres_get_responses(VALUE self)
958952

959953
GetOCSPBasicRes(self, bs);
960954
count = OCSP_resp_count(bs);
961-
ret = rb_ary_new2(count);
955+
ret = rb_ary_new_capa(count);
962956

963957
for (i = 0; i < count; i++) {
964-
OCSP_SINGLERESP *sres, *sres_new;
965-
966-
sres = OCSP_resp_get0(bs, i);
967-
sres_new = ASN1_item_dup(ASN1_ITEM_rptr(OCSP_SINGLERESP), sres);
968-
if (!sres_new)
969-
ossl_raise(eOCSPError, "ASN1_item_dup");
970-
971-
rb_ary_push(ret, ossl_ocspsres_new(sres_new));
958+
rb_ary_push(ret, ossl_ocspsres_new(OCSP_resp_get0(bs, i)));
972959
}
973960

974961
return ret;
@@ -986,7 +973,6 @@ static VALUE
986973
ossl_ocspbres_find_response(VALUE self, VALUE target)
987974
{
988975
OCSP_BASICRESP *bs;
989-
OCSP_SINGLERESP *sres, *sres_new;
990976
OCSP_CERTID *id;
991977
int n;
992978

@@ -995,13 +981,7 @@ ossl_ocspbres_find_response(VALUE self, VALUE target)
995981

996982
if ((n = OCSP_resp_find(bs, id, -1)) == -1)
997983
return Qnil;
998-
999-
sres = OCSP_resp_get0(bs, n);
1000-
sres_new = ASN1_item_dup(ASN1_ITEM_rptr(OCSP_SINGLERESP), sres);
1001-
if (!sres_new)
1002-
ossl_raise(eOCSPError, "ASN1_item_dup");
1003-
1004-
return ossl_ocspsres_new(sres_new);
984+
return ossl_ocspsres_new(OCSP_resp_get0(bs, n));
1005985
}
1006986

1007987
/*
@@ -1110,12 +1090,18 @@ ossl_ocspbres_to_der(VALUE self)
11101090
* OCSP::SingleResponse
11111091
*/
11121092
static VALUE
1113-
ossl_ocspsres_new(OCSP_SINGLERESP *sres)
1093+
ossl_ocspsres_new(const OCSP_SINGLERESP *sres)
11141094
{
11151095
VALUE obj;
1096+
OCSP_SINGLERESP *sres_new;
11161097

11171098
obj = NewOCSPSingleRes(cOCSPSingleRes);
1118-
SetOCSPSingleRes(obj, sres);
1099+
/* OpenSSL 1.1.1 takes a non-const pointer */
1100+
sres_new = ASN1_item_dup(ASN1_ITEM_rptr(OCSP_SINGLERESP),
1101+
(OCSP_SINGLERESP *)sres);
1102+
if (!sres_new)
1103+
ossl_raise(eOCSPError, "ASN1_item_dup");
1104+
SetOCSPSingleRes(obj, sres_new);
11191105

11201106
return obj;
11211107
}
@@ -1233,12 +1219,9 @@ static VALUE
12331219
ossl_ocspsres_get_certid(VALUE self)
12341220
{
12351221
OCSP_SINGLERESP *sres;
1236-
OCSP_CERTID *id;
12371222

12381223
GetOCSPSingleRes(self, sres);
1239-
id = OCSP_CERTID_dup((OCSP_CERTID *)OCSP_SINGLERESP_get0_id(sres)); /* FIXME */
1240-
1241-
return ossl_ocspcertid_new(id);
1224+
return ossl_ocspcid_new(OCSP_SINGLERESP_get0_id(sres));
12421225
}
12431226

12441227
/*

ext/openssl/ossl_pkcs12.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,9 @@ ossl_pkcs12_s_create(int argc, VALUE *argv, VALUE self)
161161
}
162162

163163
static VALUE
164-
ossl_pkey_new_i(VALUE arg)
164+
ossl_pkey_wrap_i(VALUE arg)
165165
{
166-
return ossl_pkey_new((EVP_PKEY *)arg);
166+
return ossl_pkey_wrap((EVP_PKEY *)arg);
167167
}
168168

169169
static VALUE
@@ -211,7 +211,7 @@ ossl_pkcs12_initialize(int argc, VALUE *argv, VALUE self)
211211
if(!PKCS12_parse(pkcs, passphrase, &key, &x509, &x509s))
212212
ossl_raise(ePKCS12Error, "PKCS12_parse");
213213
if (key) {
214-
pkey = rb_protect(ossl_pkey_new_i, (VALUE)key, &st);
214+
pkey = rb_protect(ossl_pkey_wrap_i, (VALUE)key, &st);
215215
if (st) goto err;
216216
}
217217
if (x509) {

ext/openssl/ossl_pkcs7.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -153,27 +153,29 @@ ossl_PKCS7_RECIP_INFO_dup(PKCS7_RECIP_INFO *si)
153153
static VALUE
154154
ossl_pkcs7si_new(PKCS7_SIGNER_INFO *p7si)
155155
{
156-
PKCS7_SIGNER_INFO *pkcs7;
156+
PKCS7_SIGNER_INFO *p7si_new;
157157
VALUE obj;
158158

159159
obj = NewPKCS7si(cPKCS7Signer);
160-
pkcs7 = p7si ? ossl_PKCS7_SIGNER_INFO_dup(p7si) : PKCS7_SIGNER_INFO_new();
161-
if (!pkcs7) ossl_raise(ePKCS7Error, NULL);
162-
SetPKCS7si(obj, pkcs7);
160+
p7si_new = ossl_PKCS7_SIGNER_INFO_dup(p7si);
161+
if (!p7si_new)
162+
ossl_raise(ePKCS7Error, "ASN1_dup");
163+
SetPKCS7si(obj, p7si_new);
163164

164165
return obj;
165166
}
166167

167168
static VALUE
168169
ossl_pkcs7ri_new(PKCS7_RECIP_INFO *p7ri)
169170
{
170-
PKCS7_RECIP_INFO *pkcs7;
171+
PKCS7_RECIP_INFO *p7ri_new;
171172
VALUE obj;
172173

173174
obj = NewPKCS7ri(cPKCS7Recipient);
174-
pkcs7 = p7ri ? ossl_PKCS7_RECIP_INFO_dup(p7ri) : PKCS7_RECIP_INFO_new();
175-
if (!pkcs7) ossl_raise(ePKCS7Error, NULL);
176-
SetPKCS7ri(obj, pkcs7);
175+
p7ri_new = ossl_PKCS7_RECIP_INFO_dup(p7ri);
176+
if (!p7ri_new)
177+
ossl_raise(ePKCS7Error,"ASN1_dup");
178+
SetPKCS7ri(obj, p7ri_new);
177179

178180
return obj;
179181
}

ext/openssl/ossl_pkey.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const rb_data_type_t ossl_evp_pkey_type = {
3939
};
4040

4141
static VALUE
42-
pkey_new0(VALUE arg)
42+
pkey_wrap0(VALUE arg)
4343
{
4444
EVP_PKEY *pkey = (EVP_PKEY *)arg;
4545
VALUE klass, obj;
@@ -65,12 +65,12 @@ pkey_new0(VALUE arg)
6565
}
6666

6767
VALUE
68-
ossl_pkey_new(EVP_PKEY *pkey)
68+
ossl_pkey_wrap(EVP_PKEY *pkey)
6969
{
7070
VALUE obj;
7171
int status;
7272

73-
obj = rb_protect(pkey_new0, (VALUE)pkey, &status);
73+
obj = rb_protect(pkey_wrap0, (VALUE)pkey, &status);
7474
if (status) {
7575
EVP_PKEY_free(pkey);
7676
rb_jump_tag(status);
@@ -239,7 +239,7 @@ ossl_pkey_new_from_data(int argc, VALUE *argv, VALUE self)
239239
BIO_free(bio);
240240
if (!pkey)
241241
ossl_raise(ePKeyError, "Could not parse PKey");
242-
return ossl_pkey_new(pkey);
242+
return ossl_pkey_wrap(pkey);
243243
}
244244

245245
static VALUE
@@ -443,7 +443,7 @@ pkey_generate(int argc, VALUE *argv, VALUE self, int genparam)
443443
}
444444
}
445445

446-
return ossl_pkey_new(gen_arg.pkey);
446+
return ossl_pkey_wrap(gen_arg.pkey);
447447
}
448448

449449
/*
@@ -687,7 +687,7 @@ ossl_pkey_new_raw_private_key(VALUE self, VALUE type, VALUE key)
687687
ossl_raise(ePKeyError, "EVP_PKEY_new_raw_private_key");
688688
#endif
689689

690-
return ossl_pkey_new(pkey);
690+
return ossl_pkey_wrap(pkey);
691691
}
692692

693693
/*
@@ -719,7 +719,7 @@ ossl_pkey_new_raw_public_key(VALUE self, VALUE type, VALUE key)
719719
ossl_raise(ePKeyError, "EVP_PKEY_new_raw_public_key");
720720
#endif
721721

722-
return ossl_pkey_new(pkey);
722+
return ossl_pkey_wrap(pkey);
723723
}
724724

725725
/*

ext/openssl/ossl_pkey.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ extern const rb_data_type_t ossl_evp_pkey_type;
2727
} while (0)
2828

2929
/* Takes ownership of the EVP_PKEY */
30-
VALUE ossl_pkey_new(EVP_PKEY *);
30+
VALUE ossl_pkey_wrap(EVP_PKEY *);
3131
void ossl_pkey_check_public_key(const EVP_PKEY *);
3232
EVP_PKEY *ossl_pkey_read_generic(BIO *, VALUE);
3333
EVP_PKEY *GetPKeyPtr(VALUE);

0 commit comments

Comments
 (0)