Index: src/SSLSocket.m ================================================================== --- src/SSLSocket.m +++ src/SSLSocket.m @@ -25,10 +25,12 @@ #include #import #import +#include + #import "SSLSocket.h" #import #import #import @@ -36,28 +38,56 @@ #import #import #import #import #import +#import #ifndef INVALID_SOCKET # define INVALID_SOCKET -1 #endif static SSL_CTX *ctx; +static of_mutex_t *ssl_mutexes; + +static void +ssl_locking_callback(int mode, int n, const char *file, int line) +{ + /* + * This function must handle up to CRYPTO_num_locks() mutexes. + * It must set the n-th lock if mode & CRYPTO_LOCK, + * release it otherwise. + */ + if (mode & CRYPTO_LOCK) + of_mutex_lock(&ssl_mutexes[n]); + else + of_mutex_unlock(&ssl_mutexes[n]); +} @implementation SSLSocket + (void)load { of_http_request_tls_socket_class = self; } + (void)initialize { + int m; + if (self != [SSLSocket class]) return; + CRYPTO_set_id_callback(&of_thread_current); + + /* Generate number of mutexes needed */ + m = CRYPTO_num_locks(); + ssl_mutexes = malloc(m * sizeof(of_mutex_t)); + for (m--; m >= 0; m--) + of_mutex_new(&ssl_mutexes[m]); + + CRYPTO_set_locking_callback(&ssl_locking_callback); + SSL_library_init(); if ((ctx = SSL_CTX_new(SSLv23_method())) == NULL) @throw [OFInitializationFailedException exceptionWithClass: self];