Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions ext/openssl/ossl_bn.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,9 @@ ossl_bn_new(const BIGNUM *bn)
VALUE obj;

obj = NewBN(cBN);
newbn = bn ? BN_dup(bn) : BN_new();
if (!newbn) {
ossl_raise(eBNError, NULL);
}
newbn = BN_dup(bn);
if (!newbn)
ossl_raise(eBNError, "BN_dup");
SetBN(obj, newbn);

return obj;
Expand Down
4 changes: 2 additions & 2 deletions ext/openssl/ossl_engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ ossl_engine_load_privkey(int argc, VALUE *argv, VALUE self)
GetEngine(self, e);
pkey = ENGINE_load_private_key(e, sid, NULL, sdata);
if (!pkey) ossl_raise(eEngineError, NULL);
obj = ossl_pkey_new(pkey);
obj = ossl_pkey_wrap(pkey);
OSSL_PKEY_SET_PRIVATE(obj);

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

return ossl_pkey_new(pkey);
return ossl_pkey_wrap(pkey);
}

/*
Expand Down
2 changes: 1 addition & 1 deletion ext/openssl/ossl_ns_spki.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ ossl_spki_get_public_key(VALUE self)
ossl_raise(eSPKIError, NULL);
}

return ossl_pkey_new(pkey); /* NO DUP - OK */
return ossl_pkey_wrap(pkey);
}

/*
Expand Down
129 changes: 56 additions & 73 deletions ext/openssl/ossl_ocsp.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,14 @@ static const rb_data_type_t ossl_ocsp_certid_type = {
* Public
*/
static VALUE
ossl_ocspcertid_new(OCSP_CERTID *cid)
ossl_ocspcid_new(const OCSP_CERTID *cid)
{
VALUE obj = NewOCSPCertId(cOCSPCertId);
SetOCSPCertId(obj, cid);
/* OpenSSL 1.1.1 takes a non-const pointer */
OCSP_CERTID *cid_new = OCSP_CERTID_dup((OCSP_CERTID *)cid);
if (!cid_new)
ossl_raise(eOCSPError, "OCSP_CERTID_dup");
SetOCSPCertId(obj, cid_new);
return obj;
}

Expand Down Expand Up @@ -328,21 +332,19 @@ static VALUE
ossl_ocspreq_get_certid(VALUE self)
{
OCSP_REQUEST *req;
OCSP_ONEREQ *one;
OCSP_CERTID *id;
VALUE ary, tmp;
int i, count;

GetOCSPReq(self, req);
count = OCSP_request_onereq_count(req);
ary = (count > 0) ? rb_ary_new() : Qnil;
for(i = 0; i < count; i++){
one = OCSP_request_onereq_get0(req, i);
tmp = NewOCSPCertId(cOCSPCertId);
if(!(id = OCSP_CERTID_dup(OCSP_onereq_get0_id(one))))
ossl_raise(eOCSPError, NULL);
SetOCSPCertId(tmp, id);
rb_ary_push(ary, tmp);
int count = OCSP_request_onereq_count(req);
if (count < 0)
ossl_raise(eOCSPError, "OCSP_request_onereq_count");
if (count == 0)
return Qnil;

VALUE ary = rb_ary_new_capa(count);
for (int i = 0; i < count; i++) {
OCSP_ONEREQ *one = OCSP_request_onereq_get0(req, i);
OCSP_CERTID *cid = OCSP_onereq_get0_id(one);
rb_ary_push(ary, ossl_ocspcid_new(cid));
}

return ary;
Expand Down Expand Up @@ -899,48 +901,40 @@ static VALUE
ossl_ocspbres_get_status(VALUE self)
{
OCSP_BASICRESP *bs;
OCSP_SINGLERESP *single;
OCSP_CERTID *cid;
ASN1_TIME *revtime, *thisupd, *nextupd;
int status, reason;
X509_EXTENSION *x509ext;
VALUE ret, ary, ext;
int count, ext_count, i, j;

GetOCSPBasicRes(self, bs);
ret = rb_ary_new();
count = OCSP_resp_count(bs);
for(i = 0; i < count; i++){
single = OCSP_resp_get0(bs, i);
if(!single) continue;

revtime = thisupd = nextupd = NULL;
status = OCSP_single_get0_status(single, &reason, &revtime,
&thisupd, &nextupd);
if(status < 0) continue;
if(!(cid = OCSP_CERTID_dup((OCSP_CERTID *)OCSP_SINGLERESP_get0_id(single)))) /* FIXME */
ossl_raise(eOCSPError, NULL);
ary = rb_ary_new();
rb_ary_push(ary, ossl_ocspcertid_new(cid));
rb_ary_push(ary, INT2NUM(status));
rb_ary_push(ary, INT2NUM(reason));
rb_ary_push(ary, revtime ? asn1time_to_time(revtime) : Qnil);
rb_ary_push(ary, thisupd ? asn1time_to_time(thisupd) : Qnil);
rb_ary_push(ary, nextupd ? asn1time_to_time(nextupd) : Qnil);
ext = rb_ary_new();
ext_count = OCSP_SINGLERESP_get_ext_count(single);
for(j = 0; j < ext_count; j++){
x509ext = OCSP_SINGLERESP_get_ext(single, j);
rb_ary_push(ext, ossl_x509ext_new(x509ext));
}
rb_ary_push(ary, ext);
rb_ary_push(ret, ary);
VALUE ret = rb_ary_new();
int count = OCSP_resp_count(bs);
for (int i = 0; i < count; i++) {
OCSP_SINGLERESP *single = OCSP_resp_get0(bs, i);
ASN1_TIME *revtime, *thisupd, *nextupd;
int reason;

int status = OCSP_single_get0_status(single, &reason, &revtime, &thisupd, &nextupd);
if (status < 0)
ossl_raise(eOCSPError, "OCSP_single_get0_status");

VALUE ary = rb_ary_new();
rb_ary_push(ary, ossl_ocspcid_new(OCSP_SINGLERESP_get0_id(single)));
rb_ary_push(ary, INT2NUM(status));
rb_ary_push(ary, INT2NUM(reason));
rb_ary_push(ary, revtime ? asn1time_to_time(revtime) : Qnil);
rb_ary_push(ary, thisupd ? asn1time_to_time(thisupd) : Qnil);
rb_ary_push(ary, nextupd ? asn1time_to_time(nextupd) : Qnil);
VALUE ext = rb_ary_new();
int ext_count = OCSP_SINGLERESP_get_ext_count(single);
for (int j = 0; j < ext_count; j++) {
X509_EXTENSION *x509ext = OCSP_SINGLERESP_get_ext(single, j);
rb_ary_push(ext, ossl_x509ext_new(x509ext));
}
rb_ary_push(ary, ext);
rb_ary_push(ret, ary);
}

return ret;
}

static VALUE ossl_ocspsres_new(OCSP_SINGLERESP *);
static VALUE ossl_ocspsres_new(const OCSP_SINGLERESP *);

/*
* call-seq:
Expand All @@ -958,17 +952,10 @@ ossl_ocspbres_get_responses(VALUE self)

GetOCSPBasicRes(self, bs);
count = OCSP_resp_count(bs);
ret = rb_ary_new2(count);
ret = rb_ary_new_capa(count);

for (i = 0; i < count; i++) {
OCSP_SINGLERESP *sres, *sres_new;

sres = OCSP_resp_get0(bs, i);
sres_new = ASN1_item_dup(ASN1_ITEM_rptr(OCSP_SINGLERESP), sres);
if (!sres_new)
ossl_raise(eOCSPError, "ASN1_item_dup");

rb_ary_push(ret, ossl_ocspsres_new(sres_new));
rb_ary_push(ret, ossl_ocspsres_new(OCSP_resp_get0(bs, i)));
}

return ret;
Expand All @@ -986,7 +973,6 @@ static VALUE
ossl_ocspbres_find_response(VALUE self, VALUE target)
{
OCSP_BASICRESP *bs;
OCSP_SINGLERESP *sres, *sres_new;
OCSP_CERTID *id;
int n;

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

if ((n = OCSP_resp_find(bs, id, -1)) == -1)
return Qnil;

sres = OCSP_resp_get0(bs, n);
sres_new = ASN1_item_dup(ASN1_ITEM_rptr(OCSP_SINGLERESP), sres);
if (!sres_new)
ossl_raise(eOCSPError, "ASN1_item_dup");

return ossl_ocspsres_new(sres_new);
return ossl_ocspsres_new(OCSP_resp_get0(bs, n));
}

/*
Expand Down Expand Up @@ -1110,12 +1090,18 @@ ossl_ocspbres_to_der(VALUE self)
* OCSP::SingleResponse
*/
static VALUE
ossl_ocspsres_new(OCSP_SINGLERESP *sres)
ossl_ocspsres_new(const OCSP_SINGLERESP *sres)
{
VALUE obj;
OCSP_SINGLERESP *sres_new;

obj = NewOCSPSingleRes(cOCSPSingleRes);
SetOCSPSingleRes(obj, sres);
/* OpenSSL 1.1.1 takes a non-const pointer */
sres_new = ASN1_item_dup(ASN1_ITEM_rptr(OCSP_SINGLERESP),
(OCSP_SINGLERESP *)sres);
if (!sres_new)
ossl_raise(eOCSPError, "ASN1_item_dup");
SetOCSPSingleRes(obj, sres_new);

return obj;
}
Expand Down Expand Up @@ -1233,12 +1219,9 @@ static VALUE
ossl_ocspsres_get_certid(VALUE self)
{
OCSP_SINGLERESP *sres;
OCSP_CERTID *id;

GetOCSPSingleRes(self, sres);
id = OCSP_CERTID_dup((OCSP_CERTID *)OCSP_SINGLERESP_get0_id(sres)); /* FIXME */

return ossl_ocspcertid_new(id);
return ossl_ocspcid_new(OCSP_SINGLERESP_get0_id(sres));
}

/*
Expand Down
6 changes: 3 additions & 3 deletions ext/openssl/ossl_pkcs12.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ ossl_pkcs12_s_create(int argc, VALUE *argv, VALUE self)
}

static VALUE
ossl_pkey_new_i(VALUE arg)
ossl_pkey_wrap_i(VALUE arg)
{
return ossl_pkey_new((EVP_PKEY *)arg);
return ossl_pkey_wrap((EVP_PKEY *)arg);
}

static VALUE
Expand Down Expand Up @@ -211,7 +211,7 @@ ossl_pkcs12_initialize(int argc, VALUE *argv, VALUE self)
if(!PKCS12_parse(pkcs, passphrase, &key, &x509, &x509s))
ossl_raise(ePKCS12Error, "PKCS12_parse");
if (key) {
pkey = rb_protect(ossl_pkey_new_i, (VALUE)key, &st);
pkey = rb_protect(ossl_pkey_wrap_i, (VALUE)key, &st);
if (st) goto err;
}
if (x509) {
Expand Down
18 changes: 10 additions & 8 deletions ext/openssl/ossl_pkcs7.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,27 +153,29 @@ ossl_PKCS7_RECIP_INFO_dup(PKCS7_RECIP_INFO *si)
static VALUE
ossl_pkcs7si_new(PKCS7_SIGNER_INFO *p7si)
{
PKCS7_SIGNER_INFO *pkcs7;
PKCS7_SIGNER_INFO *p7si_new;
VALUE obj;

obj = NewPKCS7si(cPKCS7Signer);
pkcs7 = p7si ? ossl_PKCS7_SIGNER_INFO_dup(p7si) : PKCS7_SIGNER_INFO_new();
if (!pkcs7) ossl_raise(ePKCS7Error, NULL);
SetPKCS7si(obj, pkcs7);
p7si_new = ossl_PKCS7_SIGNER_INFO_dup(p7si);
if (!p7si_new)
ossl_raise(ePKCS7Error, "ASN1_dup");
SetPKCS7si(obj, p7si_new);

return obj;
}

static VALUE
ossl_pkcs7ri_new(PKCS7_RECIP_INFO *p7ri)
{
PKCS7_RECIP_INFO *pkcs7;
PKCS7_RECIP_INFO *p7ri_new;
VALUE obj;

obj = NewPKCS7ri(cPKCS7Recipient);
pkcs7 = p7ri ? ossl_PKCS7_RECIP_INFO_dup(p7ri) : PKCS7_RECIP_INFO_new();
if (!pkcs7) ossl_raise(ePKCS7Error, NULL);
SetPKCS7ri(obj, pkcs7);
p7ri_new = ossl_PKCS7_RECIP_INFO_dup(p7ri);
if (!p7ri_new)
ossl_raise(ePKCS7Error,"ASN1_dup");
SetPKCS7ri(obj, p7ri_new);

return obj;
}
Expand Down
14 changes: 7 additions & 7 deletions ext/openssl/ossl_pkey.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const rb_data_type_t ossl_evp_pkey_type = {
};

static VALUE
pkey_new0(VALUE arg)
pkey_wrap0(VALUE arg)
{
EVP_PKEY *pkey = (EVP_PKEY *)arg;
VALUE klass, obj;
Expand All @@ -65,12 +65,12 @@ pkey_new0(VALUE arg)
}

VALUE
ossl_pkey_new(EVP_PKEY *pkey)
ossl_pkey_wrap(EVP_PKEY *pkey)
{
VALUE obj;
int status;

obj = rb_protect(pkey_new0, (VALUE)pkey, &status);
obj = rb_protect(pkey_wrap0, (VALUE)pkey, &status);
if (status) {
EVP_PKEY_free(pkey);
rb_jump_tag(status);
Expand Down Expand Up @@ -239,7 +239,7 @@ ossl_pkey_new_from_data(int argc, VALUE *argv, VALUE self)
BIO_free(bio);
if (!pkey)
ossl_raise(ePKeyError, "Could not parse PKey");
return ossl_pkey_new(pkey);
return ossl_pkey_wrap(pkey);
}

static VALUE
Expand Down Expand Up @@ -443,7 +443,7 @@ pkey_generate(int argc, VALUE *argv, VALUE self, int genparam)
}
}

return ossl_pkey_new(gen_arg.pkey);
return ossl_pkey_wrap(gen_arg.pkey);
}

/*
Expand Down Expand Up @@ -687,7 +687,7 @@ ossl_pkey_new_raw_private_key(VALUE self, VALUE type, VALUE key)
ossl_raise(ePKeyError, "EVP_PKEY_new_raw_private_key");
#endif

return ossl_pkey_new(pkey);
return ossl_pkey_wrap(pkey);
}

/*
Expand Down Expand Up @@ -719,7 +719,7 @@ ossl_pkey_new_raw_public_key(VALUE self, VALUE type, VALUE key)
ossl_raise(ePKeyError, "EVP_PKEY_new_raw_public_key");
#endif

return ossl_pkey_new(pkey);
return ossl_pkey_wrap(pkey);
}

/*
Expand Down
2 changes: 1 addition & 1 deletion ext/openssl/ossl_pkey.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ extern const rb_data_type_t ossl_evp_pkey_type;
} while (0)

/* Takes ownership of the EVP_PKEY */
VALUE ossl_pkey_new(EVP_PKEY *);
VALUE ossl_pkey_wrap(EVP_PKEY *);
void ossl_pkey_check_public_key(const EVP_PKEY *);
EVP_PKEY *ossl_pkey_read_generic(BIO *, VALUE);
EVP_PKEY *GetPKeyPtr(VALUE);
Expand Down
Loading
Loading