Ticket #469: ngx-openssl-locks.diff

File ngx-openssl-locks.diff, 3.7 KB (added by Jeffrey Walton, 13 years ago)

OpenSSL static locks

  • src/event/ngx_event_openssl.c

    diff -r 68e250ceb06e src/event/ngx_event_openssl.c
    a b  
    88#include <ngx_config.h>
    99#include <ngx_core.h>
    1010#include <ngx_event.h>
     11#include <ngx_thread.h>
    1112
    1213
    1314typedef struct {
     
    4243static char *ngx_openssl_engine(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
    4344static void ngx_openssl_exit(ngx_cycle_t *cycle);
    4445
     46#if (NGX_THREADS) && defined(OPENSSL_THREADS)
     47static ngx_mutex_t* g_locks[CRYPTO_NUM_LOCKS] = {};
     48
     49static int ngx_install_openssl_locks(ngx_log_t *log);
     50static int ngx_install_openssl_callbacks(ngx_log_t *log);
     51static unsigned long ngx_openssl_thread_id(void);
     52static void ngx_openssl_lock_fn(int mode, int idx, const char* file, int line);
     53static int ngx_remove_openssl_callbacks(void);
     54static int ngx_remove_openssl_locks(void);
     55#endif
     56
    4557
    4658static ngx_command_t  ngx_openssl_commands[] = {
    4759
     
    95107    SSL_load_error_strings();
    96108
    97109    OpenSSL_add_all_algorithms();
     110   
     111#if (NGX_THREADS) && defined(OPENSSL_THREADS)
     112    if(ngx_install_openssl_locks(log) != NGX_OK)
     113    {
     114        ngx_ssl_error(NGX_LOG_EMERG, log, 0,
     115                      "ngx_install_openssl_locks() failed");
     116        return NGX_ERROR;
     117    }
     118   
     119    if(ngx_install_openssl_callbacks(log) != NGX_OK)
     120    {
     121        ngx_ssl_error(NGX_LOG_EMERG, log, 0,
     122                      "ngx_install_openssl_callbacks() failed");
     123        return NGX_ERROR;
     124    }
     125#endif
    98126
    99127#if OPENSSL_VERSION_NUMBER >= 0x0090800fL
    100128#ifndef SSL_OP_NO_COMPRESSION
     
    25612589{
    25622590    EVP_cleanup();
    25632591    ENGINE_cleanup();
     2592   
     2593#if (NGX_THREADS) && defined(OPENSSL_THREADS)
     2594    ngx_remove_openssl_callbacks();
     2595    ngx_remove_openssl_locks();
     2596#endif
    25642597}
     2598
     2599
     2600#if (NGX_THREADS) && defined(OPENSSL_THREADS)
     2601int
     2602ngx_install_openssl_locks(ngx_log_t *log)
     2603{
     2604    if(CRYPTO_num_locks() != CRYPTO_NUM_LOCKS)
     2605    {
     2606        ngx_ssl_error(NGX_LOG_EMERG, log, 0,
     2607                      "CRYPTO_num_locks() is not CRYPTO_NUM_LOCKS");
     2608        return NGX_ERROR;
     2609    }
     2610   
     2611    for(unsigned n = 0; n < CRYPTO_NUM_LOCKS; n++)
     2612    {
     2613        g_locks[n] = ngx_mutex_init(log, 0);
     2614        if (g_locks[n] == NULL) {
     2615            return NGX_ERROR;
     2616        }
     2617    }
     2618
     2619    return NGX_OK;
     2620}
     2621
     2622
     2623static int
     2624ngx_install_openssl_callbacks(ngx_log_t *log)
     2625{
     2626    CRYPTO_set_id_callback(ngx_openssl_thread_id);
     2627    CRYPTO_set_locking_callback(ngx_openssl_lock_fn);
     2628
     2629    return NGX_OK;
     2630}
     2631
     2632
     2633unsigned long
     2634ngx_openssl_thread_id(void)
     2635{
     2636    return (unsigned long)ngx_thread_self();
     2637}
     2638
     2639
     2640void
     2641ngx_openssl_lock_fn(int mode, int idx, const char* file, int line)
     2642{
     2643    if (!(idx < 0 || idx > CRYPTO_NUM_LOCKS))
     2644        return;
     2645   
     2646    if ((mode & CRYPTO_LOCK) == CRYPTO_LOCK)
     2647    {
     2648        ngx_mutex_lock(g_locks[idx]);
     2649    }
     2650    else if ((mode & CRYPTO_UNLOCK) == CRYPTO_UNLOCK)
     2651    {
     2652        ngx_mutex_unlock(g_locks[idx]);
     2653    }
     2654   
     2655    /* Hmm... void functions suck */
     2656}
     2657
     2658
     2659int
     2660ngx_remove_openssl_callbacks(void)
     2661{
     2662    CRYPTO_set_id_callback(NULL);
     2663    CRYPTO_set_locking_callback(NULL);
     2664
     2665    return NGX_OK;
     2666}
     2667
     2668
     2669int
     2670ngx_remove_openssl_locks(void)
     2671{
     2672    for(unsigned n = 0; n < CRYPTO_NUM_LOCKS; n++)
     2673    {
     2674        ngx_mutex_destroy(g_locks[n]);
     2675        g_locks[n] = NULL;
     2676    }
     2677   
     2678    return NGX_OK;
     2679}
     2680#endif
  • src/event/ngx_event_openssl.h

    diff -r 68e250ceb06e src/event/ngx_event_openssl.h
    a b  
    1818#include <openssl/engine.h>
    1919#include <openssl/evp.h>
    2020#include <openssl/ocsp.h>
     21#include <openssl/opensslconf.h>
    2122
    2223#define NGX_SSL_NAME     "OpenSSL"
    2324