summaryrefslogtreecommitdiff
path: root/ext/openssl/ossl_pkcs7.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-05-29 05:55:02 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-05-29 05:55:02 +0000
commit451fe269e5ab1270a53ac7bdeceabe47fd431f95 (patch)
treeed014c958a0f622db02af64186def2c70e4c00ba /ext/openssl/ossl_pkcs7.c
parent5924f9a684ace630d3658a0d6e52270e3686ca9f (diff)
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
Diffstat (limited to 'ext/openssl/ossl_pkcs7.c')
-rw-r--r--ext/openssl/ossl_pkcs7.c42
1 files changed, 28 insertions, 14 deletions
diff --git a/ext/openssl/ossl_pkcs7.c b/ext/openssl/ossl_pkcs7.c
index 909990a..cfc2ccc 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;
}