ruby-changes:29754
From: emboss <ko1@a...>
Date: Sat, 6 Jul 2013 06:46:09 +0900 (JST)
Subject: [ruby-changes:29754] emboss:r41806 (trunk): * ext/openssl/ossl.c: Provide CRYPTO_set_locking_callback() and
emboss 2013-07-06 06:44:50 +0900 (Sat, 06 Jul 2013) New Revision: 41806 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=41806 Log: * ext/openssl/ossl.c: Provide CRYPTO_set_locking_callback() and CRYPTO_set_id_callback() callback functions ossl_thread_id and ossl_lock_callback to ensure the OpenSSL extension is usable in multi-threaded environments. [ruby-core:54900] [Bug #8386] Thanks, Dirkjan Bussink, for the patch! Modified files: trunk/ChangeLog trunk/ext/openssl/ossl.c Index: ChangeLog =================================================================== --- ChangeLog (revision 41805) +++ ChangeLog (revision 41806) @@ -1,3 +1,13 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Sat Jul 6 06:37:10 2013 Martin Bosslet <Martin.Bosslet@g...> + + * ext/openssl/ossl.c: Provide CRYPTO_set_locking_callback() and + CRYPTO_set_id_callback() callback functions ossl_thread_id and + ossl_lock_callback to ensure the OpenSSL extension is usable in + multi-threaded environments. + [ruby-core:54900] [Bug #8386] + + Thanks, Dirkjan Bussink, for the patch! + Sat Jul 6 06:06:16 2013 Martin Bosslet <Martin.Bosslet@g...> * lib/openssl/ssl.rb: Fix SSL client connection crash for SAN marked Index: ext/openssl/ossl.c =================================================================== --- ext/openssl/ossl.c (revision 41805) +++ ext/openssl/ossl.c (revision 41806) @@ -458,6 +458,39 @@ ossl_fips_mode_set(VALUE self, VALUE ena https://github.com/ruby/ruby/blob/trunk/ext/openssl/ossl.c#L458 #endif } +/** + * Stores locks needed for OpenSSL thread safety + */ +static VALUE* ossl_locks; + +static void ossl_lock_callback(int mode, int type, char *file, int line) +{ + if (mode & CRYPTO_LOCK) { + rb_mutex_lock(ossl_locks[type]); + } else { + rb_mutex_unlock(ossl_locks[type]); + } +} + +static unsigned long ossl_thread_id(void) +{ + return NUM2ULONG(rb_obj_id(rb_thread_current())); +} + +static void Init_ossl_locks(void) +{ + int i; + + ossl_locks = (VALUE*) OPENSSL_malloc(CRYPTO_num_locks() * sizeof(VALUE)); + for (i = 0; i < CRYPTO_num_locks(); i++) { + ossl_locks[i] = rb_mutex_new(); + rb_global_variable(&(ossl_locks[i])); + } + + CRYPTO_set_id_callback((unsigned long (*)())ossl_thread_id); + CRYPTO_set_locking_callback((void (*)())ossl_lock_callback); +} + /* * OpenSSL provides SSL, TLS and general purpose cryptography. It wraps the * OpenSSL[http://www.openssl.org/] library. @@ -977,6 +1010,7 @@ Init_openssl() https://github.com/ruby/ruby/blob/trunk/ext/openssl/ossl.c#L1010 * Init main module */ mOSSL = rb_define_module("OpenSSL"); + rb_global_variable(&mOSSL); /* * OpenSSL ruby extension version @@ -1009,6 +1043,7 @@ Init_openssl() https://github.com/ruby/ruby/blob/trunk/ext/openssl/ossl.c#L1043 * common for all classes under OpenSSL module */ eOSSLError = rb_define_class_under(mOSSL,"OpenSSLError",rb_eStandardError); + rb_global_variable(&eOSSLError); /* * Verify callback Proc index for ext-data @@ -1020,6 +1055,8 @@ Init_openssl() https://github.com/ruby/ruby/blob/trunk/ext/openssl/ossl.c#L1055 * Init debug core */ dOSSL = Qfalse; + rb_global_variable(&dOSSL); + rb_define_module_function(mOSSL, "debug", ossl_debug_get, 0); rb_define_module_function(mOSSL, "debug=", ossl_debug_set, 1); rb_define_module_function(mOSSL, "errors", ossl_get_errors, 0); @@ -1029,6 +1066,8 @@ Init_openssl() https://github.com/ruby/ruby/blob/trunk/ext/openssl/ossl.c#L1066 */ ossl_s_to_der = rb_intern("to_der"); + Init_ossl_locks(); + /* * Init components */ -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/