diff options
author | GOTOU Yuuzou <gotoyuzo@notwork.org> | 2002-12-09 17:32:54 +0000 |
---|---|---|
committer | GOTOU Yuuzou <gotoyuzo@notwork.org> | 2002-12-09 17:32:54 +0000 |
commit | a03f80b45a53e6803dace32da4034d58c32293c1 (patch) | |
tree | b7463f1088c9c57625c2f74b4b461fd00368c3d4 | |
parent | ad2da96f58be5446b920f554d2ce5c7f8529411c (diff) | |
download | ruby-openssl-history-a03f80b45a53e6803dace32da4034d58c32293c1.tar.gz |
* x509name.c: let initialize() give a Array instead of a Hash.
to_a is implemented and to_h is deprecated.
* openssl.rb: X509::Name is refined.
-rw-r--r-- | ChangeLog | 7 | ||||
-rw-r--r-- | lib/openssl.rb | 64 | ||||
-rw-r--r-- | ossl_x509name.c | 93 |
3 files changed, 70 insertions, 94 deletions
@@ -1,4 +1,9 @@ -Mon, 9 Dec 2202 20:21:32 -0900 -- GOTOU Yuuzou <gotoyuzo@notwork.org> +Mon, 9 Dec 2002 22:26:15 -0900 -- GOTOU Yuuzou <gotoyuzo@notwork.org> + * x509name.c: let initialize() give a Array instead of a Hash. + to_a is implemented and to_h is deprecated. + * openssl.rb: X509::Name is refined. + +Mon, 9 Dec 2002 20:21:32 -0900 -- GOTOU Yuuzou <gotoyuzo@notwork.org> * ossl.c: use ruby_unsetenv() instead of unsetenv(). * ssl.c: the return value of SSL_read/SSL_write is int (not size_t). This fix is suggested by matz. ([ruby-list:36721]) diff --git a/lib/openssl.rb b/lib/openssl.rb index 4203d2a..91494d3 100644 --- a/lib/openssl.rb +++ b/lib/openssl.rb @@ -119,55 +119,33 @@ end # defined? DH module X509 class Name - def Name::new(arg) - type = arg.class - while type - method = "new_from_#{type.name.downcase}".intern - return Name::send(method, arg) if Name::respond_to? method - type = type.superclass - end - raise TypeError, "Don't how to make new #{self} from #{arg.class}" - ###Name::send("new_from_#{arg.class.name.downcase}", arg) - end - # - # Name::new_from_hash(hash) is built-in method - # - def Name::new_from_string(str) # we're expecting string like "/A=B/C=D/E=F" - hash = Hash::new - key = val = nil # speed optim. - ary = str.split("/") - ary.shift # first item is "" - so skip it - ary.each {|item| - key, val = item.split("=") - hash[key] = val - } - Name::new_from_hash(hash) - ###ary.collect! {|item| item.split("=") } - ###Name::new_from_array(ary) + class << self + alias new_from_array new + + def new(arg) + case arg + when String + new_from_string(arg) + else + new_from_array(arg) + end + end + + def new_from_string(str) # expecting string like "/A=B/C=D/E=F" + ary = str.split("/") + ary.shift # first item is "" - so skip it + ary.collect!{|item| item.split("=", 2) } + new_from_array(ary) + end end - def Name::new_from_array(ary) # [["A","B"],["C","D"],["E","F"]] - hash = Hash::new - ary.each {|key, val| - hash[key] = val - } - Name::new_from_hash(hash) - end - # - # to_h is built-in method - # def to_s # "/A=B/C=D/E=F" - hash = self.to_h str = "" - hash.keys.each do |key| - str += "/" + key + "=" + hash[key] - end + hash = self.to_a.each{|key, val| + str << "/" << key << "=" << val + } str end - - def to_a # [["A","B"],["C","D"],["E","F"]] - self.to_h.to_a - end end # Name class ExtensionFactory diff --git a/ossl_x509name.c b/ossl_x509name.c index 905aa5d..ec6ed01 100644 --- a/ossl_x509name.c +++ b/ossl_x509name.c @@ -9,7 +9,6 @@ * (See the file 'LICENCE'.) */ #include "ossl.h" -#include "st.h" /* For st_foreach -- ST_CONTINUE */ #define WrapX509Name(obj, name) obj = Data_Wrap_Struct(cX509Name, 0, X509_NAME_free, name) #define GetX509Name(obj, name) Data_Get_Struct(obj, X509_NAME, name) @@ -57,88 +56,81 @@ ossl_x509name_get_X509_NAME(VALUE obj) return new; } -/* - * Private - */ -/* - * Iterator for ossl_x509name_new_from_hash - */ -static int -ossl_x509name_hash_i(VALUE key, VALUE value, X509_NAME *name) +static VALUE +ossl_x509name_s_new(int argc, VALUE *argv, VALUE *klass) { - int id, type; - - key = rb_String(key); - value = rb_String(value); - - if (!(id = OBJ_ln2nid(RSTRING(key)->ptr))) - if (!(id = OBJ_sn2nid(RSTRING(key)->ptr))) { - X509_NAME_free(name); - OSSL_Raise(eX509NameError, "OBJ_name2nid:"); - } - - type = ASN1_PRINTABLE_type(RSTRING(value)->ptr, -1); + X509_NAME *name = NULL; + VALUE obj; - if (!X509_NAME_add_entry_by_NID(name, id, type, RSTRING(value)->ptr, RSTRING(value)->len, -1, 0)) { - X509_NAME_free(name); + if(!(name = X509_NAME_new())) OSSL_Raise(eX509NameError, ""); - } - - return ST_CONTINUE; + WrapX509Name(obj, name); + rb_obj_call_init(obj, argc, argv); + + return obj; } static VALUE -ossl_x509name_s_new_from_hash(VALUE klass, VALUE hash) +ossl_x509name_initialize(int argc, VALUE *argv, VALUE self) { - X509_NAME *name = NULL; - VALUE obj; - - Check_Type(hash, T_HASH); - - if (!(name = X509_NAME_new())) - OSSL_Raise(eX509NameError, ""); + X509_NAME *name; + int i, type; + VALUE arg, item, key, value; - st_foreach(RHASH(hash)->tbl, ossl_x509name_hash_i, name); + GetX509Name(self, name); + if(rb_scan_args(argc, argv, "01", &arg) == 0){ + return self; + } - WrapX509Name(obj, name); + Check_Type(arg, T_ARRAY); + for(i = 0; i < RARRAY(arg)->len; i++){ + item = rb_ary_entry(arg, i); Check_Type(item, T_ARRAY); + key = rb_ary_entry(item, 0); Check_Type(key, T_STRING); + value = rb_ary_entry(item, 1); Check_Type(value, T_STRING); - return obj; + type = ASN1_PRINTABLE_type(RSTRING(value)->ptr, -1); + if (!X509_NAME_add_entry_by_txt(name, RSTRING(key)->ptr, type, + RSTRING(value)->ptr, RSTRING(value)->len, -1, 0)) { + OSSL_Raise(eX509NameError, ""); + } + } + + return self; } static VALUE -ossl_x509name_to_h(VALUE self) +ossl_x509name_to_a(VALUE self) { X509_NAME *name = NULL; X509_NAME_ENTRY *entry = NULL; int i,entries = 0; char long_name[512]; const char *short_name = NULL; - VALUE hash; + VALUE ary, tmp; GetX509Name(self, name); entries = X509_NAME_entry_count(name); - - hash = rb_hash_new(); - + ary = rb_ary_new2(entries); if (entries < 0) { rb_warning("name entries < 0!"); - return hash; + return ary; } - for (i=0; i<entries; i++) { if (!(entry = X509_NAME_get_entry(name, i))) { OSSL_Raise(eX509NameError, ""); } - if (!i2t_ASN1_OBJECT(long_name, sizeof(long_name), entry->object)) { + if (!i2t_ASN1_OBJECT(long_name, sizeof(long_name), + entry->object)) { OSSL_Raise(eX509NameError, ""); } short_name = OBJ_nid2sn(OBJ_ln2nid(long_name)); - - rb_hash_aset(hash, rb_str_new2(short_name), rb_str_new(entry->value->data, entry->value->length)); + tmp = rb_ary_new3(2, rb_str_new2(short_name), + rb_str_new(entry->value->data, entry->value->length)); + rb_ary_push(ary, tmp); } - return hash; + return ary; } /* @@ -150,7 +142,8 @@ Init_ossl_x509name(VALUE module) eX509NameError = rb_define_class_under(module, "NameError", rb_eStandardError); cX509Name = rb_define_class_under(module, "Name", rb_cObject); - rb_define_singleton_method(cX509Name, "new_from_hash", ossl_x509name_s_new_from_hash, 1); - rb_define_method(cX509Name, "to_h", ossl_x509name_to_h, 0); + rb_define_singleton_method(cX509Name, "new", ossl_x509name_s_new, -1); + rb_define_method(cX509Name, "initialize", ossl_x509name_initialize, -1); + rb_define_method(cX509Name, "to_a", ossl_x509name_to_a, 0); } |