Skip to content

Commit

Permalink
Remove SafeGet*() macros
Browse files Browse the repository at this point in the history
They are no longer useful since we use the TypedData_Get_Struct() which
also performs type checking (based on the rb_data_type_t) for the
non-safe Get*() macros. Just use them instead.
  • Loading branch information
rhenium committed May 2, 2017
1 parent fa51e0c commit 6a14a59
Show file tree
Hide file tree
Showing 22 changed files with 71 additions and 186 deletions.
5 changes: 0 additions & 5 deletions ext/openssl/ossl_bn.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@
} \
} while (0)

#define SafeGetBN(obj, bn) do { \
OSSL_Check_Kind((obj), cBN); \
GetBN((obj), (bn)); \
} while (0)

static void
ossl_bn_free(void *ptr)
{
Expand Down
6 changes: 1 addition & 5 deletions ext/openssl/ossl_cipher.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@
ossl_raise(rb_eRuntimeError, "Cipher not inititalized!"); \
} \
} while (0)
#define SafeGetCipher(obj, ctx) do { \
OSSL_Check_Kind((obj), cCipher); \
GetCipher((obj), (ctx)); \
} while (0)

/*
* Classes
Expand Down Expand Up @@ -146,7 +142,7 @@ ossl_cipher_copy(VALUE self, VALUE other)
if (!ctx1) {
AllocCipher(self, ctx1);
}
SafeGetCipher(other, ctx2);
GetCipher(other, ctx2);
if (EVP_CIPHER_CTX_copy(ctx1, ctx2) != 1)
ossl_raise(eCipherError, NULL);

Expand Down
8 changes: 2 additions & 6 deletions ext/openssl/ossl_digest.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
ossl_raise(rb_eRuntimeError, "Digest CTX wasn't initialized!"); \
} \
} while (0)
#define SafeGetDigest(obj, ctx) do { \
OSSL_Check_Kind((obj), cDigest); \
GetDigest((obj), (ctx)); \
} while (0)

/*
* Classes
Expand Down Expand Up @@ -65,7 +61,7 @@ GetDigestPtr(VALUE obj)
} else {
EVP_MD_CTX *ctx;

SafeGetDigest(obj, ctx);
GetDigest(obj, ctx);

md = EVP_MD_CTX_md(ctx);
}
Expand Down Expand Up @@ -158,7 +154,7 @@ ossl_digest_copy(VALUE self, VALUE other)
if (!ctx1)
ossl_raise(eDigestError, "EVP_MD_CTX_new");
}
SafeGetDigest(other, ctx2);
GetDigest(other, ctx2);

if (!EVP_MD_CTX_copy(ctx1, ctx2)) {
ossl_raise(eDigestError, NULL);
Expand Down
4 changes: 0 additions & 4 deletions ext/openssl/ossl_engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@
ossl_raise(rb_eRuntimeError, "ENGINE wasn't initialized."); \
} \
} while (0)
#define SafeGetEngine(obj, engine) do { \
OSSL_Check_Kind((obj), cEngine); \
GetPKCS7((obj), (engine)); \
} while (0)

/*
* Classes
Expand Down
6 changes: 1 addition & 5 deletions ext/openssl/ossl_hmac.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@
ossl_raise(rb_eRuntimeError, "HMAC wasn't initialized"); \
} \
} while (0)
#define SafeGetHMAC(obj, ctx) do { \
OSSL_Check_Kind((obj), cHMAC); \
GetHMAC((obj), (ctx)); \
} while (0)

/*
* Classes
Expand Down Expand Up @@ -124,7 +120,7 @@ ossl_hmac_copy(VALUE self, VALUE other)
if (self == other) return self;

GetHMAC(self, ctx1);
SafeGetHMAC(other, ctx2);
GetHMAC(other, ctx2);

if (!HMAC_CTX_copy(ctx1, ctx2))
ossl_raise(eHMACError, "HMAC_CTX_copy");
Expand Down
42 changes: 11 additions & 31 deletions ext/openssl/ossl_ocsp.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@
TypedData_Get_Struct((obj), OCSP_REQUEST, &ossl_ocsp_request_type, (req)); \
if(!(req)) ossl_raise(rb_eRuntimeError, "Request wasn't initialized!"); \
} while (0)
#define SafeGetOCSPReq(obj, req) do { \
OSSL_Check_Kind((obj), cOCSPReq); \
GetOCSPReq((obj), (req)); \
} while (0)

#define NewOCSPRes(klass) \
TypedData_Wrap_Struct((klass), &ossl_ocsp_response_type, 0)
Expand All @@ -37,10 +33,6 @@
TypedData_Get_Struct((obj), OCSP_RESPONSE, &ossl_ocsp_response_type, (res)); \
if(!(res)) ossl_raise(rb_eRuntimeError, "Response wasn't initialized!"); \
} while (0)
#define SafeGetOCSPRes(obj, res) do { \
OSSL_Check_Kind((obj), cOCSPRes); \
GetOCSPRes((obj), (res)); \
} while (0)

#define NewOCSPBasicRes(klass) \
TypedData_Wrap_Struct((klass), &ossl_ocsp_basicresp_type, 0)
Expand All @@ -52,10 +44,6 @@
TypedData_Get_Struct((obj), OCSP_BASICRESP, &ossl_ocsp_basicresp_type, (res)); \
if(!(res)) ossl_raise(rb_eRuntimeError, "Response wasn't initialized!"); \
} while (0)
#define SafeGetOCSPBasicRes(obj, res) do { \
OSSL_Check_Kind((obj), cOCSPBasicRes); \
GetOCSPBasicRes((obj), (res)); \
} while (0)

#define NewOCSPSingleRes(klass) \
TypedData_Wrap_Struct((klass), &ossl_ocsp_singleresp_type, 0)
Expand All @@ -67,10 +55,6 @@
TypedData_Get_Struct((obj), OCSP_SINGLERESP, &ossl_ocsp_singleresp_type, (res)); \
if(!(res)) ossl_raise(rb_eRuntimeError, "SingleResponse wasn't initialized!"); \
} while (0)
#define SafeGetOCSPSingleRes(obj, res) do { \
OSSL_Check_Kind((obj), cOCSPSingleRes); \
GetOCSPSingleRes((obj), (res)); \
} while (0)

#define NewOCSPCertId(klass) \
TypedData_Wrap_Struct((klass), &ossl_ocsp_certid_type, 0)
Expand All @@ -82,10 +66,6 @@
TypedData_Get_Struct((obj), OCSP_CERTID, &ossl_ocsp_certid_type, (cid)); \
if(!(cid)) ossl_raise(rb_eRuntimeError, "Cert ID wasn't initialized!"); \
} while (0)
#define SafeGetOCSPCertId(obj, cid) do { \
OSSL_Check_Kind((obj), cOCSPCertId); \
GetOCSPCertId((obj), (cid)); \
} while (0)

VALUE mOCSP;
VALUE eOCSPError;
Expand Down Expand Up @@ -200,7 +180,7 @@ ossl_ocspreq_initialize_copy(VALUE self, VALUE other)

rb_check_frozen(self);
GetOCSPReq(self, req_old);
SafeGetOCSPReq(other, req);
GetOCSPReq(other, req);

req_new = ASN1_item_dup(ASN1_ITEM_rptr(OCSP_REQUEST), req);
if (!req_new)
Expand Down Expand Up @@ -304,7 +284,7 @@ ossl_ocspreq_check_nonce(VALUE self, VALUE basic_resp)
int res;

GetOCSPReq(self, req);
SafeGetOCSPBasicRes(basic_resp, bs);
GetOCSPBasicRes(basic_resp, bs);
res = OCSP_check_nonce(req, bs);

return INT2NUM(res);
Expand Down Expand Up @@ -521,7 +501,7 @@ ossl_ocspres_initialize_copy(VALUE self, VALUE other)

rb_check_frozen(self);
GetOCSPRes(self, res_old);
SafeGetOCSPRes(other, res);
GetOCSPRes(other, res);

res_new = ASN1_item_dup(ASN1_ITEM_rptr(OCSP_RESPONSE), res);
if (!res_new)
Expand Down Expand Up @@ -677,7 +657,7 @@ ossl_ocspbres_initialize_copy(VALUE self, VALUE other)

rb_check_frozen(self);
GetOCSPBasicRes(self, bs_old);
SafeGetOCSPBasicRes(other, bs);
GetOCSPBasicRes(other, bs);

bs_new = ASN1_item_dup(ASN1_ITEM_rptr(OCSP_BASICRESP), bs);
if (!bs_new)
Expand Down Expand Up @@ -736,7 +716,7 @@ ossl_ocspbres_copy_nonce(VALUE self, VALUE request)
int ret;

GetOCSPBasicRes(self, bs);
SafeGetOCSPReq(request, req);
GetOCSPReq(request, req);
ret = OCSP_copy_nonce(bs, req);

return INT2NUM(ret);
Expand Down Expand Up @@ -829,7 +809,7 @@ ossl_ocspbres_add_status(VALUE self, VALUE cid, VALUE status,
VALUE tmp;

GetOCSPBasicRes(self, bs);
SafeGetOCSPCertId(cid, id);
GetOCSPCertId(cid, id);
st = NUM2INT(status);
if (!NIL_P(ext)) { /* All ext's members must be X509::Extension */
ext = rb_check_array_type(ext);
Expand Down Expand Up @@ -988,7 +968,7 @@ ossl_ocspbres_find_response(VALUE self, VALUE target)
OCSP_CERTID *id;
int n;

SafeGetOCSPCertId(target, id);
GetOCSPCertId(target, id);
GetOCSPBasicRes(self, bs);

if ((n = OCSP_resp_find(bs, id, -1)) == -1)
Expand Down Expand Up @@ -1213,7 +1193,7 @@ ossl_ocspsres_initialize_copy(VALUE self, VALUE other)

rb_check_frozen(self);
GetOCSPSingleRes(self, sres_old);
SafeGetOCSPSingleRes(other, sres);
GetOCSPSingleRes(other, sres);

sres_new = ASN1_item_dup(ASN1_ITEM_rptr(OCSP_SINGLERESP), sres);
if (!sres_new)
Expand Down Expand Up @@ -1468,7 +1448,7 @@ ossl_ocspcid_initialize_copy(VALUE self, VALUE other)

rb_check_frozen(self);
GetOCSPCertId(self, cid_old);
SafeGetOCSPCertId(other, cid);
GetOCSPCertId(other, cid);

cid_new = OCSP_CERTID_dup(cid);
if (!cid_new)
Expand Down Expand Up @@ -1543,7 +1523,7 @@ ossl_ocspcid_cmp(VALUE self, VALUE other)
int result;

GetOCSPCertId(self, id);
SafeGetOCSPCertId(other, id2);
GetOCSPCertId(other, id2);
result = OCSP_id_cmp(id, id2);

return (result == 0) ? Qtrue : Qfalse;
Expand All @@ -1564,7 +1544,7 @@ ossl_ocspcid_cmp_issuer(VALUE self, VALUE other)
int result;

GetOCSPCertId(self, id);
SafeGetOCSPCertId(other, id2);
GetOCSPCertId(other, id2);
result = OCSP_id_issuer_cmp(id, id2);

return (result == 0) ? Qtrue : Qfalse;
Expand Down
7 changes: 1 addition & 6 deletions ext/openssl/ossl_pkcs12.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@
if(!(p12)) ossl_raise(rb_eRuntimeError, "PKCS12 wasn't initialized."); \
} while (0)

#define SafeGetPKCS12(obj, p12) do { \
OSSL_Check_Kind((obj), cPKCS12); \
GetPKCS12((obj), (p12)); \
} while (0)

#define ossl_pkcs12_set_key(o,v) rb_iv_set((o), "@key", (v))
#define ossl_pkcs12_set_cert(o,v) rb_iv_set((o), "@certificate", (v))
#define ossl_pkcs12_set_ca_certs(o,v) rb_iv_set((o), "@ca_certs", (v))
Expand Down Expand Up @@ -72,7 +67,7 @@ ossl_pkcs12_initialize_copy(VALUE self, VALUE other)

rb_check_frozen(self);
GetPKCS12(self, p12_old);
SafeGetPKCS12(other, p12);
GetPKCS12(other, p12);

p12_new = ASN1_dup((i2d_of_void *)i2d_PKCS12, (d2i_of_void *)d2i_PKCS12, (char *)p12);
if (!p12_new)
Expand Down
20 changes: 4 additions & 16 deletions ext/openssl/ossl_pkcs7.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@
ossl_raise(rb_eRuntimeError, "PKCS7 wasn't initialized."); \
} \
} while (0)
#define SafeGetPKCS7(obj, pkcs7) do { \
OSSL_Check_Kind((obj), cPKCS7); \
GetPKCS7((obj), (pkcs7)); \
} while (0)

#define NewPKCS7si(klass) \
TypedData_Wrap_Struct((klass), &ossl_pkcs7_signer_info_type, 0)
Expand All @@ -42,10 +38,6 @@
ossl_raise(rb_eRuntimeError, "PKCS7si wasn't initialized."); \
} \
} while (0)
#define SafeGetPKCS7si(obj, p7si) do { \
OSSL_Check_Kind((obj), cPKCS7Signer); \
GetPKCS7si((obj), (p7si)); \
} while (0)

#define NewPKCS7ri(klass) \
TypedData_Wrap_Struct((klass), &ossl_pkcs7_recip_info_type, 0)
Expand All @@ -61,10 +53,6 @@
ossl_raise(rb_eRuntimeError, "PKCS7ri wasn't initialized."); \
} \
} while (0)
#define SafeGetPKCS7ri(obj, p7ri) do { \
OSSL_Check_Kind((obj), cPKCS7Recipient); \
GetPKCS7ri((obj), (p7ri)); \
} while (0)

#define numberof(ary) (int)(sizeof(ary)/sizeof((ary)[0]))

Expand Down Expand Up @@ -162,7 +150,7 @@ DupPKCS7SignerPtr(VALUE obj)
{
PKCS7_SIGNER_INFO *p7si, *pkcs7;

SafeGetPKCS7si(obj, p7si);
GetPKCS7si(obj, p7si);
if (!(pkcs7 = ossl_PKCS7_SIGNER_INFO_dup(p7si))) {
ossl_raise(ePKCS7Error, NULL);
}
Expand All @@ -189,7 +177,7 @@ DupPKCS7RecipientPtr(VALUE obj)
{
PKCS7_RECIP_INFO *p7ri, *pkcs7;

SafeGetPKCS7ri(obj, p7ri);
GetPKCS7ri(obj, p7ri);
if (!(pkcs7 = ossl_PKCS7_RECIP_INFO_dup(p7ri))) {
ossl_raise(ePKCS7Error, NULL);
}
Expand Down Expand Up @@ -238,7 +226,7 @@ ossl_pkcs7_s_write_smime(int argc, VALUE *argv, VALUE klass)
rb_scan_args(argc, argv, "12", &pkcs7, &data, &flags);
flg = NIL_P(flags) ? 0 : NUM2INT(flags);
if(NIL_P(data)) data = ossl_pkcs7_get_data(pkcs7);
SafeGetPKCS7(pkcs7, p7);
GetPKCS7(pkcs7, p7);
if(!NIL_P(data) && PKCS7_is_detached(p7))
flg |= PKCS7_DETACHED;
in = NIL_P(data) ? NULL : ossl_obj2bio(data);
Expand Down Expand Up @@ -414,7 +402,7 @@ ossl_pkcs7_copy(VALUE self, VALUE other)
if (self == other) return self;

GetPKCS7(self, a);
SafeGetPKCS7(other, b);
GetPKCS7(other, b);

pkcs7 = PKCS7_dup(b);
if (!pkcs7) {
Expand Down
6 changes: 3 additions & 3 deletions ext/openssl/ossl_pkey.c
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ GetPKeyPtr(VALUE obj)
{
EVP_PKEY *pkey;

SafeGetPKey(obj, pkey);
GetPKey(obj, pkey);

return pkey;
}
Expand All @@ -220,7 +220,7 @@ GetPrivPKeyPtr(VALUE obj)
if (rb_funcallv(obj, id_private_q, 0, NULL) != Qtrue) {
ossl_raise(rb_eArgError, "Private key is needed.");
}
SafeGetPKey(obj, pkey);
GetPKey(obj, pkey);

return pkey;
}
Expand All @@ -230,7 +230,7 @@ DupPKeyPtr(VALUE obj)
{
EVP_PKEY *pkey;

SafeGetPKey(obj, pkey);
GetPKey(obj, pkey);
EVP_PKEY_up_ref(pkey);

return pkey;
Expand Down
4 changes: 0 additions & 4 deletions ext/openssl/ossl_pkey.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ extern const rb_data_type_t ossl_evp_pkey_type;
rb_raise(rb_eRuntimeError, "PKEY wasn't initialized!");\
} \
} while (0)
#define SafeGetPKey(obj, pkey) do { \
OSSL_Check_Kind((obj), cPKey); \
GetPKey((obj), (pkey)); \
} while (0)

struct ossl_generate_cb_arg {
int yield;
Expand Down
Loading

0 comments on commit 6a14a59

Please sign in to comment.