From 451fe269e5ab1270a53ac7bdeceabe47fd431f95 Mon Sep 17 00:00:00 2001 From: nobu Date: Fri, 29 May 2015 05:55:02 +0000 Subject: openssl: wrapper object before alloc * ext/openssl: make wrapper objects before allocating structs to get rid of potential memory leaks. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50673 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ext/openssl/ossl_bn.c | 48 ++++++++++++++++++++++++++--------------- ext/openssl/ossl_cipher.c | 10 +++------ ext/openssl/ossl_digest.c | 8 +++---- ext/openssl/ossl_engine.c | 15 ++++++++----- ext/openssl/ossl_ns_spki.c | 9 +++++--- ext/openssl/ossl_ocsp.c | 49 +++++++++++++++++++++++++++--------------- ext/openssl/ossl_pkcs12.c | 13 +++++++---- ext/openssl/ossl_pkcs7.c | 42 ++++++++++++++++++++++++------------ ext/openssl/ossl_pkey.c | 3 ++- ext/openssl/ossl_pkey.h | 6 ++++-- ext/openssl/ossl_pkey_dh.c | 6 ++++-- ext/openssl/ossl_pkey_dsa.c | 6 ++++-- ext/openssl/ossl_pkey_ec.c | 6 ++++-- ext/openssl/ossl_pkey_rsa.c | 6 ++++-- ext/openssl/ossl_ssl.c | 5 ++++- ext/openssl/ossl_x509attr.c | 12 +++++++---- ext/openssl/ossl_x509cert.c | 16 ++++++++------ ext/openssl/ossl_x509crl.c | 12 +++++++---- ext/openssl/ossl_x509ext.c | 18 ++++++++++------ ext/openssl/ossl_x509name.c | 12 +++++++---- ext/openssl/ossl_x509req.c | 12 +++++++---- ext/openssl/ossl_x509revoked.c | 12 +++++++---- ext/openssl/ossl_x509store.c | 24 ++++++++++++++------- 23 files changed, 226 insertions(+), 124 deletions(-) (limited to 'ext') diff --git a/ext/openssl/ossl_bn.c b/ext/openssl/ossl_bn.c index 191b100960..190dbb1ab8 100644 --- a/ext/openssl/ossl_bn.c +++ b/ext/openssl/ossl_bn.c @@ -11,11 +11,13 @@ /* modified by Michal Rokos */ #include "ossl.h" -#define WrapBN(klass, obj, bn) do { \ +#define NewBN(klass) \ + TypedData_Wrap_Struct((klass), &ossl_bn_type, 0) +#define SetBN(obj, bn) do { \ if (!(bn)) { \ ossl_raise(rb_eRuntimeError, "BN wasn't initialized!"); \ } \ - (obj) = TypedData_Wrap_Struct((klass), &ossl_bn_type, (bn)); \ + RTYPEDDATA_DATA(obj) = (bn); \ } while (0) #define GetBN(obj, bn) do { \ @@ -71,11 +73,12 @@ ossl_bn_new(const BIGNUM *bn) BIGNUM *newbn; VALUE obj; + obj = NewBN(cBN); newbn = bn ? BN_dup(bn) : BN_new(); if (!newbn) { ossl_raise(eBNError, NULL); } - WrapBN(cBN, obj, newbn); + SetBN(obj, newbn); return obj; } @@ -84,6 +87,7 @@ BIGNUM * GetBNPtr(VALUE obj) { BIGNUM *bn = NULL; + VALUE newobj; if (RTEST(rb_obj_is_kind_of(obj, cBN))) { GetBN(obj, bn); @@ -91,10 +95,11 @@ GetBNPtr(VALUE obj) case T_FIXNUM: case T_BIGNUM: obj = rb_String(obj); + newobj = NewBN(cBN); /* GC bug */ if (!BN_dec2bn(&bn, StringValuePtr(obj))) { ossl_raise(eBNError, NULL); } - WrapBN(cBN, obj, bn); /* Handle potencial mem leaks */ + SetBN(newobj, bn); /* Handle potencial mem leaks */ break; case T_NIL: break; @@ -118,12 +123,12 @@ static VALUE ossl_bn_alloc(VALUE klass) { BIGNUM *bn; - VALUE obj; + VALUE obj = NewBN(klass); if (!(bn = BN_new())) { ossl_raise(eBNError, NULL); } - WrapBN(klass, obj, bn); + SetBN(obj, bn); return obj; } @@ -365,6 +370,7 @@ BIGNUM_BOOL1(is_odd) BIGNUM *bn, *result; \ VALUE obj; \ GetBN(self, bn); \ + obj = NewBN(CLASS_OF(self)); \ if (!(result = BN_new())) { \ ossl_raise(eBNError, NULL); \ } \ @@ -372,7 +378,7 @@ BIGNUM_BOOL1(is_odd) BN_free(result); \ ossl_raise(eBNError, NULL); \ } \ - WrapBN(CLASS_OF(self), obj, result); \ + SetBN(obj, result); \ return obj; \ } @@ -389,6 +395,7 @@ BIGNUM_1c(sqr) BIGNUM *bn1, *bn2 = GetBNPtr(other), *result; \ VALUE obj; \ GetBN(self, bn1); \ + obj = NewBN(CLASS_OF(self)); \ if (!(result = BN_new())) { \ ossl_raise(eBNError, NULL); \ } \ @@ -396,7 +403,7 @@ BIGNUM_1c(sqr) BN_free(result); \ ossl_raise(eBNError, NULL); \ } \ - WrapBN(CLASS_OF(self), obj, result); \ + SetBN(obj, result); \ return obj; \ } @@ -419,6 +426,7 @@ BIGNUM_2(sub) BIGNUM *bn1, *bn2 = GetBNPtr(other), *result; \ VALUE obj; \ GetBN(self, bn1); \ + obj = NewBN(CLASS_OF(self)); \ if (!(result = BN_new())) { \ ossl_raise(eBNError, NULL); \ } \ @@ -426,7 +434,7 @@ BIGNUM_2(sub) BN_free(result); \ ossl_raise(eBNError, NULL); \ } \ - WrapBN(CLASS_OF(self), obj, result); \ + SetBN(obj, result); \ return obj; \ } @@ -480,6 +488,8 @@ ossl_bn_div(VALUE self, VALUE other) GetBN(self, bn1); + obj1 = NewBN(CLASS_OF(self)); + obj2 = NewBN(CLASS_OF(self)); if (!(r1 = BN_new())) { ossl_raise(eBNError, NULL); } @@ -492,8 +502,8 @@ ossl_bn_div(VALUE self, VALUE other) BN_free(r2); ossl_raise(eBNError, NULL); } - WrapBN(CLASS_OF(self), obj1, r1); - WrapBN(CLASS_OF(self), obj2, r2); + SetBN(obj1, r1); + SetBN(obj2, r2); return rb_ary_new3(2, obj1, obj2); } @@ -506,6 +516,7 @@ ossl_bn_div(VALUE self, VALUE other) BIGNUM *bn3 = GetBNPtr(other2), *result; \ VALUE obj; \ GetBN(self, bn1); \ + obj = NewBN(CLASS_OF(self)); \ if (!(result = BN_new())) { \ ossl_raise(eBNError, NULL); \ } \ @@ -513,7 +524,7 @@ ossl_bn_div(VALUE self, VALUE other) BN_free(result); \ ossl_raise(eBNError, NULL); \ } \ - WrapBN(CLASS_OF(self), obj, result); \ + SetBN(obj, result); \ return obj; \ } @@ -602,6 +613,7 @@ ossl_bn_is_bit_set(VALUE self, VALUE bit) VALUE obj; \ b = NUM2INT(bits); \ GetBN(self, bn); \ + obj = NewBN(CLASS_OF(self)); \ if (!(result = BN_new())) { \ ossl_raise(eBNError, NULL); \ } \ @@ -609,7 +621,7 @@ ossl_bn_is_bit_set(VALUE self, VALUE bit) BN_free(result); \ ossl_raise(eBNError, NULL); \ } \ - WrapBN(CLASS_OF(self), obj, result); \ + SetBN(obj, result); \ return obj; \ } @@ -668,6 +680,7 @@ BIGNUM_SELF_SHIFT(rshift) top = NUM2INT(fill); \ } \ b = NUM2INT(bits); \ + obj = NewBN(klass); \ if (!(result = BN_new())) { \ ossl_raise(eBNError, NULL); \ } \ @@ -675,7 +688,7 @@ BIGNUM_SELF_SHIFT(rshift) BN_free(result); \ ossl_raise(eBNError, NULL); \ } \ - WrapBN(klass, obj, result); \ + SetBN(obj, result); \ return obj; \ } @@ -696,7 +709,7 @@ BIGNUM_RAND(pseudo_rand) ossl_bn_s_##func##_range(VALUE klass, VALUE range) \ { \ BIGNUM *bn = GetBNPtr(range), *result; \ - VALUE obj; \ + VALUE obj = NewBN(klass); \ if (!(result = BN_new())) { \ ossl_raise(eBNError, NULL); \ } \ @@ -704,7 +717,7 @@ BIGNUM_RAND(pseudo_rand) BN_free(result); \ ossl_raise(eBNError, NULL); \ } \ - WrapBN(klass, obj, result); \ + SetBN(obj, result); \ return obj; \ } @@ -750,6 +763,7 @@ ossl_bn_s_generate_prime(int argc, VALUE *argv, VALUE klass) add = GetBNPtr(vadd); rem = NIL_P(vrem) ? NULL : GetBNPtr(vrem); } + obj = NewBN(klass); if (!(result = BN_new())) { ossl_raise(eBNError, NULL); } @@ -757,7 +771,7 @@ ossl_bn_s_generate_prime(int argc, VALUE *argv, VALUE klass) BN_free(result); ossl_raise(eBNError, NULL); } - WrapBN(klass, obj, result); + SetBN(obj, result); return obj; } diff --git a/ext/openssl/ossl_cipher.c b/ext/openssl/ossl_cipher.c index 72eaa3f31d..3e92d65668 100644 --- a/ext/openssl/ossl_cipher.c +++ b/ext/openssl/ossl_cipher.c @@ -10,8 +10,8 @@ */ #include "ossl.h" -#define WrapCipher(obj, klass, ctx) \ - (obj) = TypedData_Wrap_Struct((klass), &ossl_cipher_type, (ctx)) +#define NewCipher(klass) \ + TypedData_Wrap_Struct((klass), &ossl_cipher_type, 0) #define MakeCipher(obj, klass, ctx) \ (obj) = TypedData_Make_Struct((klass), EVP_CIPHER_CTX, &ossl_cipher_type, (ctx)) #define AllocCipher(obj, ctx) \ @@ -98,11 +98,7 @@ ossl_cipher_memsize(const void *ptr) static VALUE ossl_cipher_alloc(VALUE klass) { - VALUE obj; - - WrapCipher(obj, klass, 0); - - return obj; + return NewCipher(klass); } /* diff --git a/ext/openssl/ossl_digest.c b/ext/openssl/ossl_digest.c index 83dbc34e3e..0ed12c5a56 100644 --- a/ext/openssl/ossl_digest.c +++ b/ext/openssl/ossl_digest.c @@ -95,13 +95,11 @@ ossl_digest_new(const EVP_MD *md) static VALUE ossl_digest_alloc(VALUE klass) { - EVP_MD_CTX *ctx; - VALUE obj; - - ctx = EVP_MD_CTX_create(); + VALUE obj = TypedData_Wrap_Struct(klass, &ossl_digest_type, 0); + EVP_MD_CTX *ctx = EVP_MD_CTX_create(); if (ctx == NULL) ossl_raise(rb_eRuntimeError, "EVP_MD_CTX_create() failed"); - obj = TypedData_Wrap_Struct(klass, &ossl_digest_type, ctx); + RTYPEDDATA_DATA(obj) = ctx; return obj; } diff --git a/ext/openssl/ossl_engine.c b/ext/openssl/ossl_engine.c index 20dd7b0941..0e9bc96c21 100644 --- a/ext/openssl/ossl_engine.c +++ b/ext/openssl/ossl_engine.c @@ -12,11 +12,13 @@ #if defined(OSSL_ENGINE_ENABLED) -#define WrapEngine(klass, obj, engine) do { \ +#define NewEngine(klass) \ + TypedData_Wrap_Struct((klass), &ossl_engine_type, 0) +#define SetEngine(obj, engine) do { \ if (!(engine)) { \ ossl_raise(rb_eRuntimeError, "ENGINE wasn't initialized."); \ } \ - (obj) = TypedData_Wrap_Struct((klass), &ossl_engine_type, (engine)); \ + RTYPEDDATA_DATA(obj) = (engine); \ } while(0) #define GetEngine(obj, engine) do { \ TypedData_Get_Struct((obj), ENGINE, &ossl_engine_type, (engine)); \ @@ -182,11 +184,12 @@ ossl_engine_s_engines(VALUE klass) ary = rb_ary_new(); for(e = ENGINE_get_first(); e; e = ENGINE_get_next(e)){ + obj = NewEngine(klass); /* Need a ref count of two here because of ENGINE_free being * called internally by OpenSSL when moving to the next ENGINE * and by us when releasing the ENGINE reference */ ENGINE_up_ref(e); - WrapEngine(klass, obj, e); + SetEngine(obj, e); rb_ary_push(ary, obj); } @@ -213,9 +216,10 @@ ossl_engine_s_by_id(VALUE klass, VALUE id) StringValue(id); ossl_engine_s_load(1, &id, klass); + obj = NewEngine(klass); if(!(e = ENGINE_by_id(RSTRING_PTR(id)))) ossl_raise(eEngineError, NULL); - WrapEngine(klass, obj, e); + SetEngine(obj, e); if(rb_block_given_p()) rb_yield(obj); if(!ENGINE_init(e)) ossl_raise(eEngineError, NULL); @@ -232,10 +236,11 @@ ossl_engine_s_alloc(VALUE klass) ENGINE *e; VALUE obj; + obj = NewEngine(klass); if (!(e = ENGINE_new())) { ossl_raise(eEngineError, NULL); } - WrapEngine(klass, obj, e); + SetEngine(obj, e); return obj; } diff --git a/ext/openssl/ossl_ns_spki.c b/ext/openssl/ossl_ns_spki.c index b3e0ab805f..94e0667fcf 100644 --- a/ext/openssl/ossl_ns_spki.c +++ b/ext/openssl/ossl_ns_spki.c @@ -10,11 +10,13 @@ */ #include "ossl.h" -#define WrapSPKI(klass, obj, spki) do { \ +#define NewSPKI(klass) \ + TypedData_Wrap_Struct((klass), &ossl_netscape_spki_type, 0) +#define SetSPKI(obj, spki) do { \ if (!(spki)) { \ ossl_raise(rb_eRuntimeError, "SPKI wasn't initialized!"); \ } \ - (obj) = TypedData_Wrap_Struct((klass), &ossl_netscape_spki_type, (spki)); \ + RTYPEDDATA_DATA(obj) = (spki); \ } while (0) #define GetSPKI(obj, spki) do { \ TypedData_Get_Struct((obj), NETSCAPE_SPKI, &ossl_netscape_spki_type, (spki)); \ @@ -58,10 +60,11 @@ ossl_spki_alloc(VALUE klass) NETSCAPE_SPKI *spki; VALUE obj; + obj = NewSPKI(klass); if (!(spki = NETSCAPE_SPKI_new())) { ossl_raise(eSPKIError, NULL); } - WrapSPKI(klass, obj, spki); + SetSPKI(obj, spki); return obj; } diff --git a/ext/openssl/ossl_ocsp.c b/ext/openssl/ossl_ocsp.c index 1c65637645..108fc2f8ab 100644 --- a/ext/openssl/ossl_ocsp.c +++ b/ext/openssl/ossl_ocsp.c @@ -13,9 +13,11 @@ #if defined(OSSL_OCSP_ENABLED) -#define WrapOCSPReq(klass, obj, req) do { \ +#define NewOCSPReq(klass) \ + TypedData_Wrap_Struct((klass), &ossl_ocsp_request_type, 0) +#define SetOCSPReq(obj, req) do { \ if(!(req)) ossl_raise(rb_eRuntimeError, "Request wasn't initialized!"); \ - (obj) = TypedData_Wrap_Struct((klass), &ossl_ocsp_request_type, (req)); \ + RTYPEDDATA_DATA(obj) = (req); \ } while (0) #define GetOCSPReq(obj, req) do { \ TypedData_Get_Struct((obj), OCSP_REQUEST, &ossl_ocsp_request_type, (req)); \ @@ -26,9 +28,11 @@ GetOCSPReq((obj), (req)); \ } while (0) -#define WrapOCSPRes(klass, obj, res) do { \ +#define NewOCSPRes(klass) \ + TypedData_Wrap_Struct((klass), &ossl_ocsp_response_type, 0) +#define SetOCSPRes(obj, res) do { \ if(!(res)) ossl_raise(rb_eRuntimeError, "Response wasn't initialized!"); \ - (obj) = TypedData_Wrap_Struct((klass), &ossl_ocsp_response_type, (res)); \ + RTYPEDDATA_DATA(obj) = (res); \ } while (0) #define GetOCSPRes(obj, res) do { \ TypedData_Get_Struct((obj), OCSP_RESPONSE, &ossl_ocsp_response_type, (res)); \ @@ -39,9 +43,11 @@ GetOCSPRes((obj), (res)); \ } while (0) -#define WrapOCSPBasicRes(klass, obj, res) do { \ +#define NewOCSPBasicRes(klass) \ + TypedData_Wrap_Struct((klass), &ossl_ocsp_basicresp_type, 0) +#define SetOCSPBasicRes(obj, res) do { \ if(!(res)) ossl_raise(rb_eRuntimeError, "Response wasn't initialized!"); \ - (obj) = TypedData_Wrap_Struct((klass), &ossl_ocsp_basicresp_type, (res)); \ + RTYPEDDATA_DATA(obj) = (res); \ } while (0) #define GetOCSPBasicRes(obj, res) do { \ TypedData_Get_Struct((obj), OCSP_BASICRESP, &ossl_ocsp_basicresp_type, (res)); \ @@ -52,9 +58,11 @@ GetOCSPBasicRes((obj), (res)); \ } while (0) -#define WrapOCSPCertId(klass, obj, cid) do { \ +#define NewOCSPCertId(klass) \ + TypedData_Wrap_Struct((klass), &ossl_ocsp_certid_type, 0) +#define SetOCSPCertId(obj, cid) do { \ if(!(cid)) ossl_raise(rb_eRuntimeError, "Cert ID wasn't initialized!"); \ - (obj) = TypedData_Wrap_Struct((klass), &ossl_ocsp_certid_type, (cid)); \ + RTYPEDDATA_DATA(obj) = (cid); \ } while (0) #define GetOCSPCertId(obj, cid) do { \ TypedData_Get_Struct((obj), OCSP_CERTID, &ossl_ocsp_certid_type, (cid)); \ @@ -134,8 +142,8 @@ static const rb_data_type_t ossl_ocsp_certid_type = { static VALUE ossl_ocspcertid_new(OCSP_CERTID *cid) { - VALUE obj; - WrapOCSPCertId(cOCSPCertId, obj, cid); + VALUE obj = NewOCSPCertId(cOCSPCertId); + SetOCSPCertId(obj, cid); return obj; } @@ -148,9 +156,10 @@ ossl_ocspreq_alloc(VALUE klass) OCSP_REQUEST *req; VALUE obj; + obj = NewOCSPReq(klass); if (!(req = OCSP_REQUEST_new())) ossl_raise(eOCSPError, NULL); - WrapOCSPReq(klass, obj, req); + SetOCSPReq(obj, req); return obj; } @@ -294,9 +303,10 @@ ossl_ocspreq_get_certid(VALUE self) 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); - WrapOCSPCertId(cOCSPCertId, tmp, id); + SetOCSPCertId(tmp, id); rb_ary_push(ary, tmp); } @@ -415,9 +425,10 @@ ossl_ocspres_s_create(VALUE klass, VALUE status, VALUE basic_resp) if(NIL_P(basic_resp)) bs = NULL; else GetOCSPBasicRes(basic_resp, bs); /* NO NEED TO DUP */ + obj = NewOCSPRes(klass); if(!(res = OCSP_response_create(st, bs))) ossl_raise(eOCSPError, NULL); - WrapOCSPRes(klass, obj, res); + SetOCSPRes(obj, res); return obj; } @@ -428,9 +439,10 @@ ossl_ocspres_alloc(VALUE klass) OCSP_RESPONSE *res; VALUE obj; + obj = NewOCSPRes(klass); if(!(res = OCSP_RESPONSE_new())) ossl_raise(eOCSPError, NULL); - WrapOCSPRes(klass, obj, res); + SetOCSPRes(obj, res); return obj; } @@ -519,9 +531,10 @@ ossl_ocspres_get_basic(VALUE self) VALUE ret; GetOCSPRes(self, res); + ret = NewOCSPBasicRes(cOCSPBasicRes); if(!(bs = OCSP_response_get1_basic(res))) return Qnil; - WrapOCSPBasicRes(cOCSPBasicRes, ret, bs); + SetOCSPBasicRes(ret, bs); return ret; } @@ -562,9 +575,10 @@ ossl_ocspbres_alloc(VALUE klass) OCSP_BASICRESP *bs; VALUE obj; + obj = NewOCSPBasicRes(klass); if(!(bs = OCSP_BASICRESP_new())) ossl_raise(eOCSPError, NULL); - WrapOCSPBasicRes(klass, obj, bs); + SetOCSPBasicRes(obj, bs); return obj; } @@ -851,9 +865,10 @@ ossl_ocspcid_alloc(VALUE klass) OCSP_CERTID *id; VALUE obj; + obj = NewOCSPCertId(klass); if(!(id = OCSP_CERTID_new())) ossl_raise(eOCSPError, NULL); - WrapOCSPCertId(klass, obj, id); + SetOCSPCertId(obj, id); return obj; } diff --git a/ext/openssl/ossl_pkcs12.c b/ext/openssl/ossl_pkcs12.c index 1c88b7f56e..f6ea5627c8 100644 --- a/ext/openssl/ossl_pkcs12.c +++ b/ext/openssl/ossl_pkcs12.c @@ -5,9 +5,12 @@ */ #include "ossl.h" -#define WrapPKCS12(klass, obj, p12) do { \ +#define NewPKCS12(klass) \ + TypedData_Wrap_Struct((klass), &ossl_pkcs12_type, 0) + +#define SetPKCS12(obj, p12) do { \ if(!(p12)) ossl_raise(rb_eRuntimeError, "PKCS12 wasn't initialized."); \ - (obj) = TypedData_Wrap_Struct((klass), &ossl_pkcs12_type, (p12)); \ + RTYPEDDATA_DATA(obj) = (p12); \ } while (0) #define GetPKCS12(obj, p12) do { \ @@ -56,8 +59,9 @@ ossl_pkcs12_s_allocate(VALUE klass) PKCS12 *p12; VALUE obj; + obj = NewPKCS12(klass); if(!(p12 = PKCS12_new())) ossl_raise(ePKCS12Error, NULL); - WrapPKCS12(klass, obj, p12); + SetPKCS12(obj, p12); return obj; } @@ -118,11 +122,12 @@ ossl_pkcs12_s_create(int argc, VALUE *argv, VALUE self) if (!NIL_P(keytype)) ktype = NUM2INT(keytype); + obj = NewPKCS12(cPKCS12); p12 = PKCS12_create(passphrase, friendlyname, key, x509, x509s, nkey, ncert, kiter, miter, ktype); sk_X509_pop_free(x509s, X509_free); if(!p12) ossl_raise(ePKCS12Error, NULL); - WrapPKCS12(cPKCS12, obj, p12); + SetPKCS12(obj, p12); ossl_pkcs12_set_key(obj, pkey); ossl_pkcs12_set_cert(obj, cert); diff --git a/ext/openssl/ossl_pkcs7.c b/ext/openssl/ossl_pkcs7.c index 909990aeb3..cfc2ccc64d 100644 --- a/ext/openssl/ossl_pkcs7.c +++ b/ext/openssl/ossl_pkcs7.c @@ -10,11 +10,13 @@ */ #include "ossl.h" -#define WrapPKCS7(klass, obj, pkcs7) do { \ +#define NewPKCS7(klass) \ + TypedData_Wrap_Struct((klass), &ossl_pkcs7_type, 0) +#define SetPKCS7(obj, pkcs7) do { \ if (!(pkcs7)) { \ ossl_raise(rb_eRuntimeError, "PKCS7 wasn't initialized."); \ } \ - (obj) = TypedData_Wrap_Struct((klass), &ossl_pkcs7_type, (pkcs7)); \ + RTYPEDDATA_DATA(obj) = (pkcs7); \ } while (0) #define GetPKCS7(obj, pkcs7) do { \ TypedData_Get_Struct((obj), PKCS7, &ossl_pkcs7_type, (pkcs7)); \ @@ -27,11 +29,13 @@ GetPKCS7((obj), (pkcs7)); \ } while (0) -#define WrapPKCS7si(klass, obj, p7si) do { \ +#define NewPKCS7si(klass) \ + TypedData_Wrap_Struct((klass), &ossl_pkcs7_signer_info_type, 0) +#define SetPKCS7si(obj, p7si) do { \ if (!(p7si)) { \ ossl_raise(rb_eRuntimeError, "PKCS7si wasn't initialized."); \ } \ - (obj) = TypedData_Wrap_Struct((klass), &ossl_pkcs7_signer_info_type, (p7si)); \ + RTYPEDDATA_DATA(obj) = (p7si); \ } while (0) #define GetPKCS7si(obj, p7si) do { \ TypedData_Get_Struct((obj), PKCS7_SIGNER_INFO, &ossl_pkcs7_signer_info_type, (p7si)); \ @@ -44,11 +48,13 @@ GetPKCS7si((obj), (p7si)); \ } while (0) -#define WrapPKCS7ri(klass, obj, p7ri) do { \ +#define NewPKCS7ri(klass) \ + TypedData_Wrap_Struct((klass), &ossl_pkcs7_recip_info_type, 0) +#define SetPKCS7ri(obj, p7ri) do { \ if (!(p7ri)) { \ ossl_raise(rb_eRuntimeError, "PKCS7ri wasn't initialized."); \ } \ - (obj) = TypedData_Wrap_Struct((klass), &ossl_pkcs7_recip_info_type, (p7ri)); \ + RTYPEDDATA_DATA(obj) = (p7ri); \ } while (0) #define GetPKCS7ri(obj, p7ri) do { \ TypedData_Get_Struct((obj), PKCS7_RECIP_INFO, &ossl_pkcs7_recip_info_type, (p7ri)); \ @@ -128,9 +134,10 @@ ossl_pkcs7si_new(PKCS7_SIGNER_INFO *p7si) PKCS7_SIGNER_INFO *pkcs7; VALUE obj; + obj = NewPKCS7si(cPKCS7Signer); pkcs7 = p7si ? PKCS7_SIGNER_INFO_dup(p7si) : PKCS7_SIGNER_INFO_new(); if (!pkcs7) ossl_raise(ePKCS7Error, NULL); - WrapPKCS7si(cPKCS7Signer, obj, pkcs7); + SetPKCS7si(obj, pkcs7); return obj; } @@ -154,9 +161,10 @@ ossl_pkcs7ri_new(PKCS7_RECIP_INFO *p7ri) PKCS7_RECIP_INFO *pkcs7; VALUE obj; + obj = NewPKCS7ri(cPKCS7Recipient); pkcs7 = p7ri ? PKCS7_RECIP_INFO_dup(p7ri) : PKCS7_RECIP_INFO_new(); if (!pkcs7) ossl_raise(ePKCS7Error, NULL); - WrapPKCS7ri(cPKCS7Recipient, obj, pkcs7); + SetPKCS7ri(obj, pkcs7); return obj; } @@ -185,13 +193,14 @@ ossl_pkcs7_s_read_smime(VALUE klass, VALUE arg) PKCS7 *pkcs7; VALUE ret, data; + ret = NewPKCS7(cPKCS7); in = ossl_obj2bio(arg); out = NULL; pkcs7 = SMIME_read_PKCS7(in, &out); BIO_free(in); if(!pkcs7) ossl_raise(ePKCS7Error, NULL); data = out ? ossl_membio2str(out) : Qnil; - WrapPKCS7(cPKCS7, ret, pkcs7); + SetPKCS7(ret, pkcs7); ossl_pkcs7_set_data(ret, data); ossl_pkcs7_set_err_string(ret, Qnil); @@ -253,6 +262,7 @@ ossl_pkcs7_s_sign(int argc, VALUE *argv, VALUE klass) x509 = GetX509CertPtr(cert); /* NO NEED TO DUP */ pkey = GetPrivPKeyPtr(key); /* NO NEED TO DUP */ flg = NIL_P(flags) ? 0 : NUM2INT(flags); + ret = NewPKCS7(cPKCS7); in = ossl_obj2bio(data); if(NIL_P(certs)) x509s = NULL; else{ @@ -267,7 +277,7 @@ ossl_pkcs7_s_sign(int argc, VALUE *argv, VALUE klass) sk_X509_pop_free(x509s, X509_free); ossl_raise(ePKCS7Error, NULL); } - WrapPKCS7(cPKCS7, ret, pkcs7); + SetPKCS7(ret, pkcs7); ossl_pkcs7_set_data(ret, data); ossl_pkcs7_set_err_string(ret, Qnil); BIO_free(in); @@ -308,6 +318,7 @@ ossl_pkcs7_s_encrypt(int argc, VALUE *argv, VALUE klass) } else ciph = GetCipherPtr(cipher); /* NO NEED TO DUP */ flg = NIL_P(flags) ? 0 : NUM2INT(flags); + ret = NewPKCS7(cPKCS7); in = ossl_obj2bio(data); x509s = ossl_protect_x509_ary2sk(certs, &status); if(status){ @@ -320,7 +331,7 @@ ossl_pkcs7_s_encrypt(int argc, VALUE *argv, VALUE klass) ossl_raise(ePKCS7Error, NULL); } BIO_free(in); - WrapPKCS7(cPKCS7, ret, p7); + SetPKCS7(ret, p7); ossl_pkcs7_set_data(ret, data); sk_X509_pop_free(x509s, X509_free); @@ -333,10 +344,11 @@ ossl_pkcs7_alloc(VALUE klass) PKCS7 *pkcs7; VALUE obj; + obj = NewPKCS7(klass); if (!(pkcs7 = PKCS7_new())) { ossl_raise(ePKCS7Error, NULL); } - WrapPKCS7(klass, obj, pkcs7); + SetPKCS7(obj, pkcs7); return obj; } @@ -886,10 +898,11 @@ ossl_pkcs7si_alloc(VALUE klass) PKCS7_SIGNER_INFO *p7si; VALUE obj; + obj = NewPKCS7si(klass); if (!(p7si = PKCS7_SIGNER_INFO_new())) { ossl_raise(ePKCS7Error, NULL); } - WrapPKCS7si(klass, obj, p7si); + SetPKCS7si(obj, p7si); return obj; } @@ -965,10 +978,11 @@ ossl_pkcs7ri_alloc(VALUE klass) PKCS7_RECIP_INFO *p7ri; VALUE obj; + obj = NewPKCS7ri(klass); if (!(p7ri = PKCS7_RECIP_INFO_new())) { ossl_raise(ePKCS7Error, NULL); } - WrapPKCS7ri(klass, obj, p7ri); + SetPKCS7ri(obj, p7ri); return obj; } diff --git a/ext/openssl/ossl_pkey.c b/ext/openssl/ossl_pkey.c index 21a977c245..01dab285c6 100644 --- a/ext/openssl/ossl_pkey.c +++ b/ext/openssl/ossl_pkey.c @@ -241,10 +241,11 @@ ossl_pkey_alloc(VALUE klass) EVP_PKEY *pkey; VALUE obj; + obj = NewPKey(klass); if (!(pkey = EVP_PKEY_new())) { ossl_raise(ePKeyError, NULL); } - WrapPKey(klass, obj, pkey); + SetPKey(obj, pkey); return obj; } diff --git a/ext/openssl/ossl_pkey.h b/ext/openssl/ossl_pkey.h index 5070393c2a..951b0de567 100644 --- a/ext/openssl/ossl_pkey.h +++ b/ext/openssl/ossl_pkey.h @@ -21,11 +21,13 @@ extern const rb_data_type_t ossl_evp_pkey_type; #define OSSL_PKEY_SET_PUBLIC(obj) rb_iv_set((obj), "private", Qfalse) #define OSSL_PKEY_IS_PRIVATE(obj) (rb_iv_get((obj), "private") == Qtrue) -#define WrapPKey(klass, obj, pkey) do { \ +#define NewPKey(klass) \ + TypedData_Wrap_Struct((klass), &ossl_evp_pkey_type, 0) +#define SetPKey(obj, pkey) do { \ if (!(pkey)) { \ rb_raise(rb_eRuntimeError, "PKEY wasn't initialized!"); \ } \ - (obj) = TypedData_Wrap_Struct((klass), &ossl_evp_pkey_type, (pkey)); \ + RTYPEDDATA_DATA(obj) = (pkey); \ OSSL_PKEY_SET_PUBLIC(obj); \ } while (0) #define GetPKey(obj, pkey) do {\ diff --git a/ext/openssl/ossl_pkey_dh.c b/ext/openssl/ossl_pkey_dh.c index 9a58bdf4d7..234cdaa1c5 100644 --- a/ext/openssl/ossl_pkey_dh.c +++ b/ext/openssl/ossl_pkey_dh.c @@ -46,6 +46,7 @@ dh_instance(VALUE klass, DH *dh) if (!dh) { return Qfalse; } + obj = NewPKey(klass); if (!(pkey = EVP_PKEY_new())) { return Qfalse; } @@ -53,7 +54,7 @@ dh_instance(VALUE klass, DH *dh) EVP_PKEY_free(pkey); return Qfalse; } - WrapPKey(klass, obj, pkey); + SetPKey(obj, pkey); return obj; } @@ -66,10 +67,11 @@ ossl_dh_new(EVP_PKEY *pkey) if (!pkey) { obj = dh_instance(cDH, DH_new()); } else { + obj = NewPKey(cDH); if (EVP_PKEY_type(pkey->type) != EVP_PKEY_DH) { ossl_raise(rb_eTypeError, "Not a DH key!"); } - WrapPKey(cDH, obj, pkey); + SetPKey(obj, pkey); } if (obj == Qfalse) { ossl_raise(eDHError, NULL); diff --git a/ext/openssl/ossl_pkey_dsa.c b/ext/openssl/ossl_pkey_dsa.c index 911342e144..b8e415dfa9 100644 --- a/ext/openssl/ossl_pkey_dsa.c +++ b/ext/openssl/ossl_pkey_dsa.c @@ -40,6 +40,7 @@ dsa_instance(VALUE klass, DSA *dsa) if (!dsa) { return Qfalse; } + obj = NewPKey(klass); if (!(pkey = EVP_PKEY_new())) { return Qfalse; } @@ -47,7 +48,7 @@ dsa_instance(VALUE klass, DSA *dsa) EVP_PKEY_free(pkey); return Qfalse; } - WrapPKey(klass, obj, pkey); + SetPKey(obj, pkey); return obj; } @@ -60,10 +61,11 @@ ossl_dsa_new(EVP_PKEY *pkey) if (!pkey) { obj = dsa_instance(cDSA, DSA_new()); } else { + obj = NewPKey(cDSA); if (EVP_PKEY_type(pkey->type) != EVP_PKEY_DSA) { ossl_raise(rb_eTypeError, "Not a DSA key!"); } - WrapPKey(cDSA, obj, pkey); + SetPKey(obj, pkey); } if (obj == Qfalse) { ossl_raise(eDSAError, NULL); diff --git a/ext/openssl/ossl_pkey_ec.c b/ext/openssl/ossl_pkey_ec.c index d63f757394..c93e3cfb99 100644 --- a/ext/openssl/ossl_pkey_ec.c +++ b/ext/openssl/ossl_pkey_ec.c @@ -116,6 +116,7 @@ static VALUE ec_instance(VALUE klass, EC_KEY *ec) if (!ec) { return Qfalse; } + obj = NewPKey(klass); if (!(pkey = EVP_PKEY_new())) { return Qfalse; } @@ -123,7 +124,7 @@ static VALUE ec_instance(VALUE klass, EC_KEY *ec) EVP_PKEY_free(pkey); return Qfalse; } - WrapPKey(klass, obj, pkey); + SetPKey(obj, pkey); return obj; } @@ -135,10 +136,11 @@ VALUE ossl_ec_new(EVP_PKEY *pkey) if (!pkey) { obj = ec_instance(cEC, EC_KEY_new()); } else { + obj = NewPKey(cEC); if (EVP_PKEY_type(pkey->type) != EVP_PKEY_EC) { ossl_raise(rb_eTypeError, "Not a EC key!"); } - WrapPKey(cEC, obj, pkey); + SetPKey(obj, pkey); } if (obj == Qfalse) { ossl_raise(eECError, NULL); diff --git a/ext/openssl/ossl_pkey_rsa.c b/ext/openssl/ossl_pkey_rsa.c index 274c1dfcac..699383f683 100644 --- a/ext/openssl/ossl_pkey_rsa.c +++ b/ext/openssl/ossl_pkey_rsa.c @@ -40,6 +40,7 @@ rsa_instance(VALUE klass, RSA *rsa) if (!rsa) { return Qfalse; } + obj = NewPKey(klass); if (!(pkey = EVP_PKEY_new())) { return Qfalse; } @@ -47,7 +48,7 @@ rsa_instance(VALUE klass, RSA *rsa) EVP_PKEY_free(pkey); return Qfalse; } - WrapPKey(klass, obj, pkey); + SetPKey(obj, pkey); return obj; } @@ -61,10 +62,11 @@ ossl_rsa_new(EVP_PKEY *pkey) obj = rsa_instance(cRSA, RSA_new()); } else { + obj = NewPKey(cRSA); if (EVP_PKEY_type(pkey->type) != EVP_PKEY_RSA) { ossl_raise(rb_eTypeError, "Not a RSA key!"); } - WrapPKey(cRSA, obj, pkey); + SetPKey(obj, pkey); } if (obj == Qfalse) { ossl_raise(eRSAError, NULL); diff --git a/ext/openssl/ossl_ssl.c b/ext/openssl/ossl_ssl.c index b006951ff5..ce8ce8935c 100644 --- a/ext/openssl/ossl_ssl.c +++ b/ext/openssl/ossl_ssl.c @@ -175,17 +175,20 @@ ossl_sslctx_s_alloc(VALUE klass) { SSL_CTX *ctx; long mode = SSL_MODE_ENABLE_PARTIAL_WRITE; + VALUE obj; #ifdef SSL_MODE_RELEASE_BUFFERS mode |= SSL_MODE_RELEASE_BUFFERS; #endif + obj = TypedData_Wrap_Struct(klass, &ossl_sslctx_type, 0); ctx = SSL_CTX_new(SSLv23_method()); if (!ctx) { ossl_raise(eSSLError, "SSL_CTX_new"); } SSL_CTX_set_mode(ctx, mode); - return TypedData_Wrap_Struct(klass, &ossl_sslctx_type, ctx); + RTYPEDDATA_DATA(obj) = ctx; + return obj; } /* diff --git a/ext/openssl/ossl_x509attr.c b/ext/openssl/ossl_x509attr.c index ee10dd8536..10e511d21d 100644 --- a/ext/openssl/ossl_x509attr.c +++ b/ext/openssl/ossl_x509attr.c @@ -10,11 +10,13 @@ */ #include "ossl.h" -#define WrapX509Attr(klass, obj, attr) do { \ +#define NewX509Attr(klass) \ + TypedData_Wrap_Struct((klass), &ossl_x509attr_type, 0) +#define SetX509Attr(obj, attr) do { \ if (!(attr)) { \ ossl_raise(rb_eRuntimeError, "ATTR wasn't initialized!"); \ } \ - (obj) = TypedData_Wrap_Struct((klass), &ossl_x509attr_type, (attr)); \ + RTYPEDDATA_DATA(obj) = (attr); \ } while (0) #define GetX509Attr(obj, attr) do { \ TypedData_Get_Struct((obj), X509_ATTRIBUTE, &ossl_x509attr_type, (attr)); \ @@ -56,6 +58,7 @@ ossl_x509attr_new(X509_ATTRIBUTE *attr) X509_ATTRIBUTE *new; VALUE obj; + obj = NewX509Attr(cX509Attr); if (!attr) { new = X509_ATTRIBUTE_new(); } else { @@ -64,7 +67,7 @@ ossl_x509attr_new(X509_ATTRIBUTE *attr) if (!new) { ossl_raise(eX509AttrError, NULL); } - WrapX509Attr(cX509Attr, obj, new); + SetX509Attr(obj, new); return obj; } @@ -91,9 +94,10 @@ ossl_x509attr_alloc(VALUE klass) X509_ATTRIBUTE *attr; VALUE obj; + obj = NewX509Attr(klass); if (!(attr = X509_ATTRIBUTE_new())) ossl_raise(eX509AttrError, NULL); - WrapX509Attr(klass, obj, attr); + SetX509Attr(obj, attr); return obj; } diff --git a/ext/openssl/ossl_x509cert.c b/ext/openssl/ossl_x509cert.c index 4979e4aae1..376db67986 100644 --- a/ext/openssl/ossl_x509cert.c +++ b/ext/openssl/ossl_x509cert.c @@ -10,11 +10,13 @@ */ #include "ossl.h" -#define WrapX509(klass, obj, x509) do { \ +#define NewX509(klass) \ + TypedData_Wrap_Struct((klass), &ossl_x509_type, 0) +#define SetX509(obj, x509) do { \ if (!(x509)) { \ ossl_raise(rb_eRuntimeError, "CERT wasn't initialized!"); \ } \ - (obj) = TypedData_Wrap_Struct((klass), &ossl_x509_type, (x509)); \ + RTYPEDDATA_DATA(obj) = (x509); \ } while (0) #define GetX509(obj, x509) do { \ TypedData_Get_Struct((obj), X509, &ossl_x509_type, (x509)); \ @@ -56,6 +58,7 @@ ossl_x509_new(X509 *x509) X509 *new; VALUE obj; + obj = NewX509(cX509Cert); if (!x509) { new = X509_new(); } else { @@ -64,7 +67,7 @@ ossl_x509_new(X509 *x509) if (!new) { ossl_raise(eX509CertError, NULL); } - WrapX509(cX509Cert, obj, new); + SetX509(obj, new); return obj; } @@ -77,6 +80,7 @@ ossl_x509_new_from_file(VALUE filename) VALUE obj; SafeStringValue(filename); + obj = NewX509(cX509Cert); if (!(fp = fopen(RSTRING_PTR(filename), "r"))) { ossl_raise(eX509CertError, "%s", strerror(errno)); } @@ -97,7 +101,7 @@ ossl_x509_new_from_file(VALUE filename) if (!x509) { ossl_raise(eX509CertError, NULL); } - WrapX509(cX509Cert, obj, x509); + SetX509(obj, x509); return obj; } @@ -133,10 +137,10 @@ ossl_x509_alloc(VALUE klass) X509 *x509; VALUE obj; + obj = NewX509(klass); x509 = X509_new(); if (!x509) ossl_raise(eX509CertError, NULL); - - WrapX509(klass, obj, x509); + SetX509(obj, x509); return obj; } diff --git a/ext/openssl/ossl_x509crl.c b/ext/openssl/ossl_x509crl.c index ca272f9aa4..7133c7a901 100644 --- a/ext/openssl/ossl_x509crl.c +++ b/ext/openssl/ossl_x509crl.c @@ -10,11 +10,13 @@ */ #include "ossl.h" -#define WrapX509CRL(klass, obj, crl) do { \ +#define NewX509CRL(klass) \ + TypedData_Wrap_Struct((klass), &ossl_x509crl_type, 0) +#define SetX509CRL(obj, crl) do { \ if (!(crl)) { \ ossl_raise(rb_eRuntimeError, "CRL wasn't initialized!"); \ } \ - (obj) = TypedData_Wrap_Struct((klass), &ossl_x509crl_type, (crl)); \ + RTYPEDDATA_DATA(obj) = (crl); \ } while (0) #define GetX509CRL(obj, crl) do { \ TypedData_Get_Struct((obj), X509_CRL, &ossl_x509crl_type, (crl)); \ @@ -77,9 +79,10 @@ ossl_x509crl_new(X509_CRL *crl) X509_CRL *tmp; VALUE obj; + obj = NewX509CRL(cX509CRL); tmp = crl ? X509_CRL_dup(crl) : X509_CRL_new(); if(!tmp) ossl_raise(eX509CRLError, NULL); - WrapX509CRL(cX509CRL, obj, tmp); + SetX509CRL(obj, tmp); return obj; } @@ -93,10 +96,11 @@ ossl_x509crl_alloc(VALUE klass) X509_CRL *crl; VALUE obj; + obj = NewX509CRL(klass); if (!(crl = X509_CRL_new())) { ossl_raise(eX509CRLError, NULL); } - WrapX509CRL(klass, obj, crl); + SetX509CRL(obj, crl); return obj; } diff --git a/ext/openssl/ossl_x509ext.c b/ext/openssl/ossl_x509ext.c index f74bd29e4e..1e65f2bc52 100644 --- a/ext/openssl/ossl_x509ext.c +++ b/ext/openssl/ossl_x509ext.c @@ -10,11 +10,13 @@ */ #include "ossl.h" -#define WrapX509Ext(klass, obj, ext) do { \ +#define NewX509Ext(klass) \ + TypedData_Wrap_Struct((klass), &ossl_x509ext_type, 0) +#define SetX509Ext(obj, ext) do { \ if (!(ext)) { \ ossl_raise(rb_eRuntimeError, "EXT wasn't initialized!"); \ } \ - (obj) = TypedData_Wrap_Struct((klass), &ossl_x509ext_type, (ext)); \ + RTYPEDDATA_DATA(obj) = (ext); \ } while (0) #define GetX509Ext(obj, ext) do { \ TypedData_Get_Struct((obj), X509_EXTENSION, &ossl_x509ext_type, (ext)); \ @@ -27,10 +29,11 @@ GetX509Ext((obj), (ext)); \ } while (0) #define MakeX509ExtFactory(klass, obj, ctx) do { \ + (obj) = TypedData_Wrap_Struct((klass), &ossl_x509extfactory_type, 0); \ if (!((ctx) = OPENSSL_malloc(sizeof(X509V3_CTX)))) \ ossl_raise(rb_eRuntimeError, "CTX wasn't allocated!"); \ X509V3_set_ctx((ctx), NULL, NULL, NULL, NULL, 0); \ - (obj) = TypedData_Wrap_Struct((klass), &ossl_x509extfactory_type, (ctx)); \ + RTYPEDDATA_DATA(obj) = (ctx); \ } while (0) #define GetX509ExtFactory(obj, ctx) do { \ TypedData_Get_Struct((obj), X509V3_CTX, &ossl_x509extfactory_type, (ctx)); \ @@ -69,6 +72,7 @@ ossl_x509ext_new(X509_EXTENSION *ext) X509_EXTENSION *new; VALUE obj; + obj = NewX509Ext(cX509Ext); if (!ext) { new = X509_EXTENSION_new(); } else { @@ -77,7 +81,7 @@ ossl_x509ext_new(X509_EXTENSION *ext) if (!new) { ossl_raise(eX509ExtError, NULL); } - WrapX509Ext(cX509Ext, obj, new); + SetX509Ext(obj, new); return obj; } @@ -258,6 +262,7 @@ ossl_x509extfactory_create_ext(int argc, VALUE *argv, VALUE self) valstr = rb_str_new2(RTEST(critical) ? "critical," : ""); rb_str_append(valstr, value); GetX509ExtFactory(self, ctx); + obj = NewX509Ext(cX509Ext); #ifdef HAVE_X509V3_EXT_NCONF_NID rconf = rb_iv_get(self, "@config"); conf = NIL_P(rconf) ? NULL : GetConfigPtr(rconf); @@ -270,7 +275,7 @@ ossl_x509extfactory_create_ext(int argc, VALUE *argv, VALUE self) ossl_raise(eX509ExtError, "%s = %s", RSTRING_PTR(oid), RSTRING_PTR(value)); } - WrapX509Ext(cX509Ext, obj, ext); + SetX509Ext(obj, ext); return obj; } @@ -284,10 +289,11 @@ ossl_x509ext_alloc(VALUE klass) X509_EXTENSION *ext; VALUE obj; + obj = NewX509Ext(klass); if(!(ext = X509_EXTENSION_new())){ ossl_raise(eX509ExtError, NULL); } - WrapX509Ext(klass, obj, ext); + SetX509Ext(obj, ext); return obj; } diff --git a/ext/openssl/ossl_x509name.c b/ext/openssl/ossl_x509name.c index 7086f259b1..565a7cd18f 100644 --- a/ext/openssl/ossl_x509name.c +++ b/ext/openssl/ossl_x509name.c @@ -10,11 +10,13 @@ */ #include "ossl.h" -#define WrapX509Name(klass, obj, name) do { \ +#define NewX509Name(klass) \ + TypedData_Wrap_Struct((klass), &ossl_x509name_type, 0) +#define SetX509Name(obj, name) do { \ if (!(name)) { \ ossl_raise(rb_eRuntimeError, "Name wasn't initialized."); \ } \ - (obj) = TypedData_Wrap_Struct((klass), &ossl_x509name_type, (name)); \ + RTYPEDDATA_DATA(obj) = (name); \ } while (0) #define GetX509Name(obj, name) do { \ TypedData_Get_Struct((obj), X509_NAME, &ossl_x509name_type, (name)); \ @@ -61,6 +63,7 @@ ossl_x509name_new(X509_NAME *name) X509_NAME *new; VALUE obj; + obj = NewX509Name(cX509Name); if (!name) { new = X509_NAME_new(); } else { @@ -69,7 +72,7 @@ ossl_x509name_new(X509_NAME *name) if (!new) { ossl_raise(eX509NameError, NULL); } - WrapX509Name(cX509Name, obj, new); + SetX509Name(obj, new); return obj; } @@ -93,10 +96,11 @@ ossl_x509name_alloc(VALUE klass) X509_NAME *name; VALUE obj; + obj = NewX509Name(klass); if (!(name = X509_NAME_new())) { ossl_raise(eX509NameError, NULL); } - WrapX509Name(klass, obj, name); + SetX509Name(obj, name); return obj; } diff --git a/ext/openssl/ossl_x509req.c b/ext/openssl/ossl_x509req.c index 59b5d24692..aa5c423c1e 100644 --- a/ext/openssl/ossl_x509req.c +++ b/ext/openssl/ossl_x509req.c @@ -10,11 +10,13 @@ */ #include "ossl.h" -#define WrapX509Req(klass, obj, req) do { \ +#define NewX509Req(klass) \ + TypedData_Wrap_Struct((klass), &ossl_x509req_type, 0) +#define SetX509Req(obj, req) do { \ if (!(req)) { \ ossl_raise(rb_eRuntimeError, "Req wasn't initialized!"); \ } \ - (obj) = TypedData_Wrap_Struct((klass), &ossl_x509req_type, (req)); \ + RTYPEDDATA_DATA(obj) = (req); \ } while (0) #define GetX509Req(obj, req) do { \ TypedData_Get_Struct((obj), X509_REQ, &ossl_x509req_type, (req)); \ @@ -56,6 +58,7 @@ ossl_x509req_new(X509_REQ *req) X509_REQ *new; VALUE obj; + obj = NewX509Req(cX509Req); if (!req) { new = X509_REQ_new(); } else { @@ -64,7 +67,7 @@ ossl_x509req_new(X509_REQ *req) if (!new) { ossl_raise(eX509ReqError, NULL); } - WrapX509Req(cX509Req, obj, new); + SetX509Req(obj, new); return obj; } @@ -101,10 +104,11 @@ ossl_x509req_alloc(VALUE klass) X509_REQ *req; VALUE obj; + obj = NewX509Req(klass); if (!(req = X509_REQ_new())) { ossl_raise(eX509ReqError, NULL); } - WrapX509Req(klass, obj, req); + SetX509Req(obj, req); return obj; } diff --git a/ext/openssl/ossl_x509revoked.c b/ext/openssl/ossl_x509revoked.c index 4babb3c1ca..5c257f4fe9 100644 --- a/ext/openssl/ossl_x509revoked.c +++ b/ext/openssl/ossl_x509revoked.c @@ -10,11 +10,13 @@ */ #include "ossl.h" -#define WrapX509Rev(klass, obj, rev) do { \ +#define NewX509Rev(klass) \ + TypedData_Wrap_Struct((klass), &ossl_x509rev_type, 0) +#define SetX509Rev(obj, rev) do { \ if (!(rev)) { \ ossl_raise(rb_eRuntimeError, "REV wasn't initialized!"); \ } \ - (obj) = TypedData_Wrap_Struct((klass), &ossl_x509rev_type, (rev)); \ + RTYPEDDATA_DATA(obj) = (rev); \ } while (0) #define GetX509Rev(obj, rev) do { \ TypedData_Get_Struct((obj), X509_REVOKED, &ossl_x509rev_type, (rev)); \ @@ -56,6 +58,7 @@ ossl_x509revoked_new(X509_REVOKED *rev) X509_REVOKED *new; VALUE obj; + obj = NewX509Rev(cX509Rev); if (!rev) { new = X509_REVOKED_new(); } else { @@ -64,7 +67,7 @@ ossl_x509revoked_new(X509_REVOKED *rev) if (!new) { ossl_raise(eX509RevError, NULL); } - WrapX509Rev(cX509Rev, obj, new); + SetX509Rev(obj, new); return obj; } @@ -91,10 +94,11 @@ ossl_x509revoked_alloc(VALUE klass) X509_REVOKED *rev; VALUE obj; + obj = NewX509Rev(klass); if (!(rev = X509_REVOKED_new())) { ossl_raise(eX509RevError, NULL); } - WrapX509Rev(klass, obj, rev); + SetX509Rev(obj, rev); return obj; } diff --git a/ext/openssl/ossl_x509store.c b/ext/openssl/ossl_x509store.c index 9924f8633a..fbb83159b7 100644 --- a/ext/openssl/ossl_x509store.c +++ b/ext/openssl/ossl_x509store.c @@ -10,11 +10,13 @@ */ #include "ossl.h" -#define WrapX509Store(klass, obj, st) do { \ +#define NewX509Store(klass) \ + TypedData_Wrap_Struct((klass), &ossl_x509store_type, 0) +#define SetX509Store(obj, st) do { \ if (!(st)) { \ ossl_raise(rb_eRuntimeError, "STORE wasn't initialized!"); \ } \ - (obj) = TypedData_Wrap_Struct((klass), &ossl_x509store_type, (st)); \ + RTYPEDDATA_DATA(obj) = (st); \ } while (0) #define GetX509Store(obj, st) do { \ TypedData_Get_Struct((obj), X509_STORE, &ossl_x509store_type, (st)); \ @@ -27,11 +29,13 @@ GetX509Store((obj), (st)); \ } while (0) -#define WrapX509StCtx(klass, obj, ctx) do { \ +#define NewX509StCtx(klass) \ + TypedData_Wrap_Struct((klass), &ossl_x509stctx_type, 0) +#define SetX509StCtx(obj, ctx) do { \ if (!(ctx)) { \ ossl_raise(rb_eRuntimeError, "STORE_CTX wasn't initialized!"); \ } \ - (obj) = TypedData_Wrap_Struct((klass), &ossl_x509stctx_type, (ctx)); \ + RTYPEDDATA_DATA(obj) = (ctx); \ } while (0) #define GetX509StCtx(obj, ctx) do { \ TypedData_Get_Struct((obj), X509_STORE_CTX, &ossl_x509stctx_type, (ctx)); \ @@ -73,7 +77,8 @@ ossl_x509store_new(X509_STORE *store) { VALUE obj; - WrapX509Store(cX509Store, obj, store); + obj = NewX509Store(cX509Store); + SetX509Store(obj, store); return obj; } @@ -108,10 +113,11 @@ ossl_x509store_alloc(VALUE klass) X509_STORE *store; VALUE obj; + obj = NewX509Store(klass); if((store = X509_STORE_new()) == NULL){ ossl_raise(eX509StoreError, NULL); } - WrapX509Store(klass, obj, store); + SetX509Store(obj, store); return obj; } @@ -373,7 +379,8 @@ ossl_x509stctx_new(X509_STORE_CTX *ctx) { VALUE obj; - WrapX509StCtx(cX509StoreContext, obj, ctx); + obj = NewX509StCtx(cX509StoreContext); + SetX509StCtx(obj, ctx); return obj; } @@ -407,10 +414,11 @@ ossl_x509stctx_alloc(VALUE klass) X509_STORE_CTX *ctx; VALUE obj; + obj = NewX509StCtx(klass); if((ctx = X509_STORE_CTX_new()) == NULL){ ossl_raise(eX509StoreError, NULL); } - WrapX509StCtx(klass, obj, ctx); + SetX509StCtx(obj, ctx); return obj; } -- cgit v1.2.3