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
9879ossl_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)
123110static VALUE
124111ossl_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 }
0 commit comments