Skip to content

Commit 1472feb

Browse files
committed
pkcs12: allocate OpenSSL objects in #initialize{,_copy}
1 parent 5f10428 commit 1472feb

1 file changed

Lines changed: 18 additions & 28 deletions

File tree

ext/openssl/ossl_pkcs12.c

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,6 @@
44
*/
55
#include "ossl.h"
66

7-
#define NewPKCS12(klass) \
8-
TypedData_Wrap_Struct((klass), &ossl_pkcs12_type, 0)
9-
10-
#define SetPKCS12(obj, p12) do { \
11-
if(!(p12)) ossl_raise(rb_eRuntimeError, "PKCS12 wasn't initialized."); \
12-
RTYPEDDATA_DATA(obj) = (p12); \
13-
} while (0)
14-
157
#define GetPKCS12(obj, p12) do { \
168
TypedData_Get_Struct((obj), PKCS12, &ossl_pkcs12_type, (p12)); \
179
if(!(p12)) ossl_raise(rb_eRuntimeError, "PKCS12 wasn't initialized."); \
@@ -50,32 +42,23 @@ static const rb_data_type_t ossl_pkcs12_type = {
5042
static VALUE
5143
ossl_pkcs12_s_allocate(VALUE klass)
5244
{
53-
PKCS12 *p12;
54-
VALUE obj;
55-
56-
obj = NewPKCS12(klass);
57-
if(!(p12 = PKCS12_new())) ossl_raise(ePKCS12Error, NULL);
58-
SetPKCS12(obj, p12);
59-
60-
return obj;
45+
return TypedData_Wrap_Struct(klass, &ossl_pkcs12_type, NULL);
6146
}
6247

6348
/* :nodoc: */
6449
static VALUE
6550
ossl_pkcs12_initialize_copy(VALUE self, VALUE other)
6651
{
67-
PKCS12 *p12, *p12_old, *p12_new;
52+
PKCS12 *p12, *p12_new;
6853

69-
rb_check_frozen(self);
70-
GetPKCS12(self, p12_old);
54+
ossl_want_uninitialized(self, &ossl_pkcs12_type);
7155
GetPKCS12(other, p12);
7256

73-
p12_new = ASN1_dup((i2d_of_void *)i2d_PKCS12, (d2i_of_void *)d2i_PKCS12, (char *)p12);
57+
p12_new = ASN1_dup((i2d_of_void *)i2d_PKCS12, (d2i_of_void *)d2i_PKCS12,
58+
p12);
7459
if (!p12_new)
7560
ossl_raise(ePKCS12Error, "ASN1_dup");
76-
77-
SetPKCS12(self, p12_new);
78-
PKCS12_free(p12_old);
61+
RTYPEDDATA_DATA(self) = p12_new;
7962

8063
return self;
8164
}
@@ -145,13 +128,13 @@ ossl_pkcs12_s_create(int argc, VALUE *argv, VALUE self)
145128
}
146129
#endif
147130

148-
obj = NewPKCS12(cPKCS12);
131+
obj = ossl_pkcs12_s_allocate(cPKCS12);
149132
x509s = NIL_P(ca) ? NULL : ossl_x509_ary2sk(ca);
150133
p12 = PKCS12_create(passphrase, friendlyname, key, x509, x509s,
151134
nkey, ncert, kiter, miter, ktype);
152135
sk_X509_pop_free(x509s, X509_free);
153136
if(!p12) ossl_raise(ePKCS12Error, NULL);
154-
SetPKCS12(obj, p12);
137+
RTYPEDDATA_DATA(obj) = p12;
155138

156139
ossl_pkcs12_set_key(obj, pkey);
157140
ossl_pkcs12_set_cert(obj, cert);
@@ -191,7 +174,7 @@ ossl_x509_sk2ary_i(VALUE arg)
191174
static VALUE
192175
ossl_pkcs12_initialize(int argc, VALUE *argv, VALUE self)
193176
{
194-
PKCS12 *p12, *p12_orig = DATA_PTR(self);
177+
PKCS12 *p12;
195178
BIO *in;
196179
VALUE arg, pass, pkey, cert, ca;
197180
char *passphrase;
@@ -200,14 +183,21 @@ ossl_pkcs12_initialize(int argc, VALUE *argv, VALUE self)
200183
STACK_OF(X509) *x509s = NULL;
201184
int st = 0;
202185

203-
if(rb_scan_args(argc, argv, "02", &arg, &pass) == 0) return self;
186+
rb_scan_args(argc, argv, "02", &arg, &pass);
187+
ossl_want_uninitialized(self, &ossl_pkcs12_type);
188+
if (argc == 0) {
189+
p12 = PKCS12_new();
190+
if (!p12)
191+
ossl_raise(ePKCS12Error, "PKCS12_new");
192+
RTYPEDDATA_DATA(self) = p12;
193+
return self;
194+
}
204195
passphrase = NIL_P(pass) ? NULL : StringValueCStr(pass);
205196
in = ossl_obj2bio(&arg);
206197
p12 = d2i_PKCS12_bio(in, NULL);
207198
BIO_free(in);
208199
if (!p12)
209200
ossl_raise(ePKCS12Error, "d2i_PKCS12_bio");
210-
PKCS12_free(p12_orig);
211201
RTYPEDDATA_DATA(self) = p12;
212202

213203
pkey = cert = ca = Qnil;

0 commit comments

Comments
 (0)