Index: ChangeLog =================================================================== --- ChangeLog (revision 31860) +++ ChangeLog (working copy) @@ -1,3 +1,11 @@ +Tue May 31 21:07:53 2011 Hiroshi Nakamura + + * ext/openssl/ossl_x509name.c: added X509::Name#hash_old as a wrapper + for X509_NAME_hash_old in OpenSSL 1.0.0. + + * test/openssl/test_x509name.rb (test_hash): make test pass with + OpenSSL 1.0.0. + Tue May 31 17:03:24 2011 Hiroshi Nakamura * lib/net/http.rb, lib/net/protocol.rb: Allow to configure to wait Index: ext/openssl/ossl_x509name.c =================================================================== --- ext/openssl/ossl_x509name.c (revision 31860) +++ ext/openssl/ossl_x509name.c (working copy) @@ -317,8 +317,29 @@ return ULONG2NUM(hash); } +#ifdef HAVE_X509_NAME_HASH_OLD /* * call-seq: + * name.hash_old => integer + * + * hash_old returns MD5 based hash used in OpenSSL 0.9.X. + */ +static VALUE +ossl_x509name_hash_old(VALUE self) +{ + X509_NAME *name; + unsigned long hash; + + GetX509Name(self, name); + + hash = X509_NAME_hash_old(name); + + return ULONG2NUM(hash); +} +#endif + +/* + * call-seq: * name.to_der => string */ static VALUE @@ -364,6 +385,9 @@ rb_define_alias(cX509Name, "<=>", "cmp"); rb_define_method(cX509Name, "eql?", ossl_x509name_eql, 1); rb_define_method(cX509Name, "hash", ossl_x509name_hash, 0); +#ifdef HAVE_X509_NAME_HASH_OLD + rb_define_method(cX509Name, "hash_old", ossl_x509name_hash_old, 0); +#endif rb_define_method(cX509Name, "to_der", ossl_x509name_to_der, 0); utf8str = INT2NUM(V_ASN1_UTF8STRING); Index: ext/openssl/extconf.rb =================================================================== --- ext/openssl/extconf.rb (revision 31860) +++ ext/openssl/extconf.rb (working copy) @@ -94,6 +94,7 @@ have_func("X509_CRL_set_issuer_name") have_func("X509_CRL_set_version") have_func("X509_CRL_sort") +have_func("X509_NAME_hash_old") have_func("X509_STORE_get_ex_data") have_func("X509_STORE_set_ex_data") have_func("OBJ_NAME_do_all_sorted") Index: test/openssl/test_x509name.rb =================================================================== --- test/openssl/test_x509name.rb (revision 31860) +++ test/openssl/test_x509name.rb (working copy) @@ -271,6 +271,26 @@ assert_equal -1, n1 <=> n2 end + + def name_hash(name) + # OpenSSL 1.0.0 uses SHA1 for canonical encoding (not just a der) of + # X509Name for X509_NAME_hash. + name.respond_to?(:hash_old) ? name.hash_old : name.hash + end + + def test_hash + dn = "/DC=org/DC=ruby-lang/CN=www.ruby-lang.org" + name = OpenSSL::X509::Name.parse(dn) + d = Digest::MD5.digest(name.to_der) + expected = (d[0].ord & 0xff) | (d[1].ord & 0xff) << 8 | (d[2].ord & 0xff) << 16 | (d[3].ord & 0xff) << 24 + assert_equal(expected, name_hash(name)) + # + dn = "/DC=org/DC=ruby-lang/CN=baz.ruby-lang.org" + name = OpenSSL::X509::Name.parse(dn) + d = Digest::MD5.digest(name.to_der) + expected = (d[0].ord & 0xff) | (d[1].ord & 0xff) << 8 | (d[2].ord & 0xff) << 16 | (d[3].ord & 0xff) << 24 + assert_equal(expected, name_hash(name)) + end end end