OpenSSL线程安全吗?

时间:2015-11-18 10:16:33

标签: c openssl centos

如果我在一个线程中进行SSL握手,并且在单独的线程中进行SSL_read和SSL_write,是否会出现问题?

1 个答案:

答案 0 :(得分:0)

OpenSSL threads manual

  

OpenSSL可以安全地用于所提供的多线程应用程序   至少设置了两个回调函数,locking_function和   threadid_func。

Recommended library setup sample

void thread_setup(void)
{
    int i;

    lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(HANDLE));
    for (i = 0; i < CRYPTO_num_locks(); i++) {
        lock_cs[i] = CreateMutex(NULL, FALSE, NULL);
    }

    CRYPTO_set_locking_callback((void (*)(int, int, char *, int))
                                win32_locking_callback);
    /* id callback defined */
}

void thread_cleanup(void)
{
    int i;

    CRYPTO_set_locking_callback(NULL);
    for (i = 0; i < CRYPTO_num_locks(); i++)
        CloseHandle(lock_cs[i]);
    OPENSSL_free(lock_cs);
}