@@ -147,6 +147,48 @@ asn1integer_to_num_i(VALUE arg)
147147 return asn1integer_to_num ((ASN1_INTEGER * )arg );
148148}
149149
150+ /*
151+ * ASN1_OBJECT conversions
152+ */
153+ VALUE
154+ ossl_asn1obj_to_string_oid (const ASN1_OBJECT * a1obj )
155+ {
156+ VALUE str ;
157+ int len ;
158+
159+ str = rb_usascii_str_new (NULL , 127 );
160+ len = OBJ_obj2txt (RSTRING_PTR (str ), RSTRING_LENINT (str ), a1obj , 1 );
161+ if (len <= 0 || len == INT_MAX )
162+ ossl_raise (eOSSLError , "OBJ_obj2txt" );
163+ if (len > RSTRING_LEN (str )) {
164+ /* +1 is for the \0 terminator added by OBJ_obj2txt() */
165+ rb_str_resize (str , len + 1 );
166+ len = OBJ_obj2txt (RSTRING_PTR (str ), len + 1 , a1obj , 1 );
167+ if (len <= 0 )
168+ ossl_raise (eOSSLError , "OBJ_obj2txt" );
169+ }
170+ rb_str_set_len (str , len );
171+ return str ;
172+ }
173+
174+ VALUE
175+ ossl_asn1obj_to_string (const ASN1_OBJECT * obj )
176+ {
177+ int nid = OBJ_obj2nid (obj );
178+ if (nid != NID_undef )
179+ return rb_str_new_cstr (OBJ_nid2sn (nid ));
180+ return ossl_asn1obj_to_string_oid (obj );
181+ }
182+
183+ VALUE
184+ ossl_asn1obj_to_string_long_name (const ASN1_OBJECT * obj )
185+ {
186+ int nid = OBJ_obj2nid (obj );
187+ if (nid != NID_undef )
188+ return rb_str_new_cstr (OBJ_nid2ln (nid ));
189+ return ossl_asn1obj_to_string_oid (obj );
190+ }
191+
150192/********/
151193/*
152194 * ASN1 module
@@ -160,7 +202,7 @@ asn1integer_to_num_i(VALUE arg)
160202#define ossl_asn1_set_indefinite_length (o ,v ) rb_ivar_set((o),sivINDEFINITE_LENGTH,(v))
161203
162204VALUE mASN1 ;
163- VALUE eASN1Error ;
205+ static VALUE eASN1Error ;
164206
165207VALUE cASN1Data ;
166208static VALUE cASN1Primitive ;
@@ -247,8 +289,8 @@ obj_to_asn1null(VALUE obj)
247289 return null ;
248290}
249291
250- static ASN1_OBJECT *
251- obj_to_asn1obj (VALUE obj )
292+ ASN1_OBJECT *
293+ ossl_to_asn1obj (VALUE obj )
252294{
253295 ASN1_OBJECT * a1obj ;
254296
@@ -393,32 +435,27 @@ decode_null(unsigned char* der, long length)
393435 return Qnil ;
394436}
395437
438+ VALUE
439+ asn1obj_to_string_i (VALUE arg )
440+ {
441+ return ossl_asn1obj_to_string ((const ASN1_OBJECT * )arg );
442+ }
443+
396444static VALUE
397445decode_obj (unsigned char * der , long length )
398446{
399447 ASN1_OBJECT * obj ;
400448 const unsigned char * p ;
401449 VALUE ret ;
402- int nid ;
403- BIO * bio ;
450+ int state ;
404451
405452 p = der ;
406- if (!(obj = d2i_ASN1_OBJECT (NULL , & p , length )))
407- ossl_raise (eASN1Error , NULL );
408- if ((nid = OBJ_obj2nid (obj )) != NID_undef ){
409- ASN1_OBJECT_free (obj );
410- ret = rb_str_new2 (OBJ_nid2sn (nid ));
411- }
412- else {
413- if (!(bio = BIO_new (BIO_s_mem ()))){
414- ASN1_OBJECT_free (obj );
415- ossl_raise (eASN1Error , NULL );
416- }
417- i2a_ASN1_OBJECT (bio , obj );
418- ASN1_OBJECT_free (obj );
419- ret = ossl_membio2str (bio );
420- }
421-
453+ if (!(obj = d2i_ASN1_OBJECT (NULL , & p , length )))
454+ ossl_raise (eASN1Error , "d2i_ASN1_OBJECT" );
455+ ret = rb_protect (asn1obj_to_string_i , (VALUE )obj , & state );
456+ ASN1_OBJECT_free (obj );
457+ if (state )
458+ rb_jump_tag (state );
422459 return ret ;
423460}
424461
@@ -544,7 +581,7 @@ ossl_asn1_get_asn1type(VALUE obj)
544581 free_func = (free_func_type * )ASN1_STRING_free ;
545582 break ;
546583 case V_ASN1_OBJECT :
547- ptr = obj_to_asn1obj (value );
584+ ptr = ossl_to_asn1obj (value );
548585 free_func = (free_func_type * )ASN1_OBJECT_free ;
549586 break ;
550587 case V_ASN1_UTCTIME :
@@ -1172,23 +1209,7 @@ ossl_asn1obj_get_ln(VALUE self)
11721209static VALUE
11731210asn1obj_get_oid_i (VALUE vobj )
11741211{
1175- ASN1_OBJECT * a1obj = (void * )vobj ;
1176- VALUE str ;
1177- int len ;
1178-
1179- str = rb_usascii_str_new (NULL , 127 );
1180- len = OBJ_obj2txt (RSTRING_PTR (str ), RSTRING_LENINT (str ), a1obj , 1 );
1181- if (len <= 0 || len == INT_MAX )
1182- ossl_raise (eASN1Error , "OBJ_obj2txt" );
1183- if (len > RSTRING_LEN (str )) {
1184- /* +1 is for the \0 terminator added by OBJ_obj2txt() */
1185- rb_str_resize (str , len + 1 );
1186- len = OBJ_obj2txt (RSTRING_PTR (str ), len + 1 , a1obj , 1 );
1187- if (len <= 0 )
1188- ossl_raise (eASN1Error , "OBJ_obj2txt" );
1189- }
1190- rb_str_set_len (str , len );
1191- return str ;
1212+ return ossl_asn1obj_to_string_oid ((const ASN1_OBJECT * )vobj );
11921213}
11931214
11941215/*
@@ -1205,7 +1226,7 @@ ossl_asn1obj_get_oid(VALUE self)
12051226 ASN1_OBJECT * a1obj ;
12061227 int state ;
12071228
1208- a1obj = obj_to_asn1obj (ossl_asn1_get_value (self ));
1229+ a1obj = ossl_to_asn1obj (ossl_asn1_get_value (self ));
12091230 str = rb_protect (asn1obj_get_oid_i , (VALUE )a1obj , & state );
12101231 ASN1_OBJECT_free (a1obj );
12111232 if (state )
0 commit comments