Skip to content

Commit c70b060

Browse files
committed
x509*: allocate OpenSSL objects in #initialize{,_copy}
1 parent 1472feb commit c70b060

8 files changed

Lines changed: 176 additions & 320 deletions

File tree

ext/openssl/ossl_x509attr.c

Lines changed: 26 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,6 @@
99
*/
1010
#include "ossl.h"
1111

12-
#define NewX509Attr(klass) \
13-
TypedData_Wrap_Struct((klass), &ossl_x509attr_type, 0)
14-
#define SetX509Attr(obj, attr) do { \
15-
if (!(attr)) { \
16-
ossl_raise(rb_eRuntimeError, "ATTR wasn't initialized!"); \
17-
} \
18-
RTYPEDDATA_DATA(obj) = (attr); \
19-
} while (0)
2012
#define GetX509Attr(obj, attr) do { \
2113
TypedData_Get_Struct((obj), X509_ATTRIBUTE, &ossl_x509attr_type, (attr)); \
2214
if (!(attr)) { \
@@ -44,6 +36,12 @@ static const rb_data_type_t ossl_x509attr_type = {
4436
0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
4537
};
4638

39+
static VALUE
40+
ossl_x509attr_alloc(VALUE klass)
41+
{
42+
return TypedData_Wrap_Struct(klass, &ossl_x509attr_type, NULL);
43+
}
44+
4745
/*
4846
* Public
4947
*/
@@ -53,12 +51,12 @@ ossl_x509attr_new(const X509_ATTRIBUTE *attr)
5351
X509_ATTRIBUTE *new;
5452
VALUE obj;
5553

56-
obj = NewX509Attr(cX509Attr);
54+
obj = ossl_x509attr_alloc(cX509Attr);
5755
/* OpenSSL 1.1.1 takes a non-const pointer */
5856
new = X509_ATTRIBUTE_dup((X509_ATTRIBUTE *)attr);
5957
if (!new)
6058
ossl_raise(eX509AttrError, "X509_ATTRIBUTE_dup");
61-
SetX509Attr(obj, new);
59+
RTYPEDDATA_DATA(obj) = new;
6260

6361
return obj;
6462
}
@@ -73,23 +71,6 @@ GetX509AttrPtr(VALUE obj)
7371
return attr;
7472
}
7573

76-
/*
77-
* Private
78-
*/
79-
static VALUE
80-
ossl_x509attr_alloc(VALUE klass)
81-
{
82-
X509_ATTRIBUTE *attr;
83-
VALUE obj;
84-
85-
obj = NewX509Attr(klass);
86-
if (!(attr = X509_ATTRIBUTE_new()))
87-
ossl_raise(eX509AttrError, NULL);
88-
SetX509Attr(obj, attr);
89-
90-
return obj;
91-
}
92-
9374
/*
9475
* call-seq:
9576
* Attribute.new(oid [, value]) => attr
@@ -98,21 +79,27 @@ static VALUE
9879
ossl_x509attr_initialize(int argc, VALUE *argv, VALUE self)
9980
{
10081
VALUE oid, value;
101-
X509_ATTRIBUTE *attr, *x;
82+
X509_ATTRIBUTE *attr;
10283
const unsigned char *p;
10384

104-
GetX509Attr(self, attr);
105-
if(rb_scan_args(argc, argv, "11", &oid, &value) == 1){
85+
rb_scan_args(argc, argv, "11", &oid, &value);
86+
ossl_want_uninitialized(self, &ossl_x509attr_type);
87+
if (argc == 1) {
10688
oid = ossl_to_der_if_possible(oid);
10789
StringValue(oid);
10890
p = (unsigned char *)RSTRING_PTR(oid);
109-
x = d2i_X509_ATTRIBUTE(&attr, &p, RSTRING_LEN(oid));
110-
DATA_PTR(self) = attr;
111-
if(!x){
112-
ossl_raise(eX509AttrError, NULL);
113-
}
91+
attr = d2i_X509_ATTRIBUTE(NULL, &p, RSTRING_LEN(oid));
92+
if (!attr)
93+
ossl_raise(eX509AttrError, "d2i_X509_ATTRIBUTE");
94+
RTYPEDDATA_DATA(self) = attr;
11495
return self;
11596
}
97+
98+
attr = X509_ATTRIBUTE_new();
99+
if (!attr)
100+
ossl_raise(eX509AttrError, "X509_ATTRIBUTE_new");
101+
RTYPEDDATA_DATA(self) = attr;
102+
116103
rb_funcall(self, rb_intern("oid="), 1, oid);
117104
rb_funcall(self, rb_intern("value="), 1, value);
118105

@@ -123,18 +110,15 @@ ossl_x509attr_initialize(int argc, VALUE *argv, VALUE self)
123110
static VALUE
124111
ossl_x509attr_initialize_copy(VALUE self, VALUE other)
125112
{
126-
X509_ATTRIBUTE *attr, *attr_other, *attr_new;
113+
X509_ATTRIBUTE *attr_other, *attr_new;
127114

128-
rb_check_frozen(self);
129-
GetX509Attr(self, attr);
115+
ossl_want_uninitialized(self, &ossl_x509attr_type);
130116
GetX509Attr(other, attr_other);
131117

132118
attr_new = X509_ATTRIBUTE_dup(attr_other);
133119
if (!attr_new)
134120
ossl_raise(eX509AttrError, "X509_ATTRIBUTE_dup");
135-
136-
SetX509Attr(self, attr_new);
137-
X509_ATTRIBUTE_free(attr);
121+
RTYPEDDATA_DATA(self) = attr_new;
138122

139123
return self;
140124
}
@@ -203,7 +187,7 @@ ossl_x509attr_set_value(VALUE self, VALUE value)
203187
sk_ASN1_TYPE_pop_free(sk, ASN1_TYPE_free);
204188
ossl_raise(eX509AttrError, "X509_ATTRIBUTE_create_by_OBJ");
205189
}
206-
SetX509Attr(self, new_attr);
190+
RTYPEDDATA_DATA(self) = new_attr;
207191
X509_ATTRIBUTE_free(attr);
208192
attr = new_attr;
209193
}

ext/openssl/ossl_x509cert.c

Lines changed: 21 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,6 @@
99
*/
1010
#include "ossl.h"
1111

12-
#define NewX509(klass) \
13-
TypedData_Wrap_Struct((klass), &ossl_x509_type, 0)
14-
#define SetX509(obj, x509) do { \
15-
if (!(x509)) { \
16-
ossl_raise(rb_eRuntimeError, "CERT wasn't initialized!"); \
17-
} \
18-
RTYPEDDATA_DATA(obj) = (x509); \
19-
} while (0)
2012
#define GetX509(obj, x509) do { \
2113
TypedData_Get_Struct((obj), X509, &ossl_x509_type, (x509)); \
2214
if (!(x509)) { \
@@ -44,6 +36,12 @@ static const rb_data_type_t ossl_x509_type = {
4436
0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
4537
};
4638

39+
static VALUE
40+
ossl_x509_alloc(VALUE klass)
41+
{
42+
return TypedData_Wrap_Struct(klass, &ossl_x509_type, NULL);
43+
}
44+
4745
/*
4846
* Public
4947
*/
@@ -53,12 +51,12 @@ ossl_x509_new(const X509 *x509)
5351
X509 *new;
5452
VALUE obj;
5553

56-
obj = NewX509(cX509Cert);
54+
obj = ossl_x509_alloc(cX509Cert);
5755
/* OpenSSL 1.1.1 takes a non-const pointer */
5856
new = X509_dup((X509 *)x509);
5957
if (!new)
6058
ossl_raise(eX509CertError, "X509_dup");
61-
SetX509(obj, new);
59+
RTYPEDDATA_DATA(obj) = new;
6260

6361
return obj;
6462
}
@@ -85,23 +83,6 @@ DupX509CertPtr(VALUE obj)
8583
return x509;
8684
}
8785

88-
/*
89-
* Private
90-
*/
91-
static VALUE
92-
ossl_x509_alloc(VALUE klass)
93-
{
94-
X509 *x509;
95-
VALUE obj;
96-
97-
obj = NewX509(klass);
98-
x509 = X509_new();
99-
if (!x509) ossl_raise(eX509CertError, NULL);
100-
SetX509(obj, x509);
101-
102-
return obj;
103-
}
104-
10586
/*
10687
* call-seq:
10788
* Certificate.new => cert
@@ -111,12 +92,16 @@ static VALUE
11192
ossl_x509_initialize(int argc, VALUE *argv, VALUE self)
11293
{
11394
BIO *in;
114-
X509 *x509, *x509_orig = RTYPEDDATA_DATA(self);
95+
X509 *x509;
11596
VALUE arg;
11697

117-
rb_check_frozen(self);
118-
if (rb_scan_args(argc, argv, "01", &arg) == 0) {
119-
/* create just empty X509Cert */
98+
rb_scan_args(argc, argv, "01", &arg);
99+
ossl_want_uninitialized(self, &ossl_x509_type);
100+
if (argc == 0) {
101+
x509 = X509_new();
102+
if (!x509)
103+
ossl_raise(eX509CertError, "X509_new");
104+
RTYPEDDATA_DATA(self) = x509;
120105
return self;
121106
}
122107
arg = ossl_to_der_if_possible(arg);
@@ -131,7 +116,6 @@ ossl_x509_initialize(int argc, VALUE *argv, VALUE self)
131116
ossl_raise(eX509CertError, "PEM_read_bio_X509");
132117

133118
RTYPEDDATA_DATA(self) = x509;
134-
X509_free(x509_orig);
135119

136120
return self;
137121
}
@@ -140,19 +124,15 @@ ossl_x509_initialize(int argc, VALUE *argv, VALUE self)
140124
static VALUE
141125
ossl_x509_copy(VALUE self, VALUE other)
142126
{
143-
X509 *a, *b, *x509;
144-
145-
rb_check_frozen(self);
146-
if (self == other) return self;
127+
X509 *b, *x509;
147128

148-
GetX509(self, a);
129+
ossl_want_uninitialized(self, &ossl_x509_type);
149130
GetX509(other, b);
150131

151132
x509 = X509_dup(b);
152-
if (!x509) ossl_raise(eX509CertError, NULL);
153-
154-
DATA_PTR(self) = x509;
155-
X509_free(a);
133+
if (!x509)
134+
ossl_raise(eX509CertError, "X509_dup");
135+
RTYPEDDATA_DATA(self) = x509;
156136

157137
return self;
158138
}

ext/openssl/ossl_x509crl.c

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,6 @@
99
*/
1010
#include "ossl.h"
1111

12-
#define NewX509CRL(klass) \
13-
TypedData_Wrap_Struct((klass), &ossl_x509crl_type, 0)
14-
#define SetX509CRL(obj, crl) do { \
15-
if (!(crl)) { \
16-
ossl_raise(rb_eRuntimeError, "CRL wasn't initialized!"); \
17-
} \
18-
RTYPEDDATA_DATA(obj) = (crl); \
19-
} while (0)
2012
#define GetX509CRL(obj, crl) do { \
2113
TypedData_Get_Struct((obj), X509_CRL, &ossl_x509crl_type, (crl)); \
2214
if (!(crl)) { \
@@ -44,6 +36,12 @@ static const rb_data_type_t ossl_x509crl_type = {
4436
0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
4537
};
4638

39+
static VALUE
40+
ossl_x509crl_alloc(VALUE klass)
41+
{
42+
return TypedData_Wrap_Struct(klass, &ossl_x509crl_type, NULL);
43+
}
44+
4745
/*
4846
* PUBLIC
4947
*/
@@ -63,30 +61,12 @@ ossl_x509crl_new(const X509_CRL *crl)
6361
X509_CRL *tmp;
6462
VALUE obj;
6563

66-
obj = NewX509CRL(cX509CRL);
64+
obj = ossl_x509crl_alloc(cX509CRL);
6765
/* OpenSSL 1.1.1 takes a non-const pointer */
6866
tmp = X509_CRL_dup((X509_CRL *)crl);
6967
if (!tmp)
7068
ossl_raise(eX509CRLError, "X509_CRL_dup");
71-
SetX509CRL(obj, tmp);
72-
73-
return obj;
74-
}
75-
76-
/*
77-
* PRIVATE
78-
*/
79-
static VALUE
80-
ossl_x509crl_alloc(VALUE klass)
81-
{
82-
X509_CRL *crl;
83-
VALUE obj;
84-
85-
obj = NewX509CRL(klass);
86-
if (!(crl = X509_CRL_new())) {
87-
ossl_raise(eX509CRLError, NULL);
88-
}
89-
SetX509CRL(obj, crl);
69+
RTYPEDDATA_DATA(obj) = tmp;
9070

9171
return obj;
9272
}
@@ -95,11 +75,16 @@ static VALUE
9575
ossl_x509crl_initialize(int argc, VALUE *argv, VALUE self)
9676
{
9777
BIO *in;
98-
X509_CRL *crl, *crl_orig = RTYPEDDATA_DATA(self);
78+
X509_CRL *crl;
9979
VALUE arg;
10080

101-
rb_check_frozen(self);
102-
if (rb_scan_args(argc, argv, "01", &arg) == 0) {
81+
rb_scan_args(argc, argv, "01", &arg);
82+
ossl_want_uninitialized(self, &ossl_x509crl_type);
83+
if (argc == 0) {
84+
crl = X509_CRL_new();
85+
if (!crl)
86+
ossl_raise(eX509CRLError, "X509_CRL_new");
87+
RTYPEDDATA_DATA(self) = crl;
10388
return self;
10489
}
10590
arg = ossl_to_der_if_possible(arg);
@@ -112,9 +97,7 @@ ossl_x509crl_initialize(int argc, VALUE *argv, VALUE self)
11297
BIO_free(in);
11398
if (!crl)
11499
ossl_raise(eX509CRLError, "PEM_read_bio_X509_CRL");
115-
116100
RTYPEDDATA_DATA(self) = crl;
117-
X509_CRL_free(crl_orig);
118101

119102
return self;
120103
}
@@ -123,17 +106,14 @@ ossl_x509crl_initialize(int argc, VALUE *argv, VALUE self)
123106
static VALUE
124107
ossl_x509crl_copy(VALUE self, VALUE other)
125108
{
126-
X509_CRL *a, *b, *crl;
109+
X509_CRL *b, *crl;
127110

128-
rb_check_frozen(self);
129-
if (self == other) return self;
130-
GetX509CRL(self, a);
111+
ossl_want_uninitialized(self, &ossl_x509crl_type);
131112
GetX509CRL(other, b);
132113
if (!(crl = X509_CRL_dup(b))) {
133114
ossl_raise(eX509CRLError, NULL);
134115
}
135-
X509_CRL_free(a);
136-
DATA_PTR(self) = crl;
116+
RTYPEDDATA_DATA(self) = crl;
137117

138118
return self;
139119
}

0 commit comments

Comments
 (0)