aboutsummaryrefslogtreecommitdiff
path: root/src/threads/libcrypto.c
blob: dac840d3a61a50f1c474cb6bc72b04cf684afadf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
 * This file is part of the SSH Library
 *
 * Copyright (c) 2018 by Anderson Toshiyuki Sasaki
 *
 * The SSH Library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or (at your
 * option) any later version.
 *
 * The SSH Library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with the SSH Library; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 * MA 02111-1307, USA.
 */

#include "config.h"
#include "libssh/crypto.h"
#include "libssh/threads.h"
#include <libssh/callbacks.h>

#if (OPENSSL_VERSION_NUMBER >= 0x10100000)

int crypto_thread_init(struct ssh_threads_callbacks_struct *cb)
{
    (void) cb;
    return SSH_OK;
}

void crypto_thread_finalize(void)
{
    return;
}

#else

static struct ssh_threads_callbacks_struct *user_callbacks = NULL;

static void **libcrypto_mutexes;

void libcrypto_lock_callback(int mode, int i, const char *file, int line);

void libcrypto_lock_callback(int mode, int i, const char *file, int line)
{
    (void)file;
    (void)line;

    if (mode & CRYPTO_LOCK) {
        user_callbacks->mutex_lock(&libcrypto_mutexes[i]);
    } else {
        user_callbacks->mutex_unlock(&libcrypto_mutexes[i]);
    }
}

static void libcrypto_THREADID_callback(CRYPTO_THREADID *id)
{
    unsigned long thread_id = (*user_callbacks->thread_id)();

    CRYPTO_THREADID_set_numeric(id, thread_id);
}

int crypto_thread_init(struct ssh_threads_callbacks_struct *cb)
{
    int n = CRYPTO_num_locks();
    int cmp;
    int i;

    if (cb == NULL) {
        return SSH_OK;
    }

    if (user_callbacks != NULL) {
        crypto_thread_finalize();
    }

    user_callbacks = cb;

    cmp = strcmp(user_callbacks->type, "threads_noop");
    if (cmp == 0) {
        return SSH_OK;
    }

    libcrypto_mutexes = calloc(n, sizeof(void *));
    if (libcrypto_mutexes == NULL) {
        return SSH_ERROR;
    }

    for (i = 0; i < n; ++i){
        user_callbacks->mutex_init(&libcrypto_mutexes[i]);
    }

    CRYPTO_THREADID_set_callback(libcrypto_THREADID_callback);
    CRYPTO_set_locking_callback(libcrypto_lock_callback);

    return SSH_OK;
}

void crypto_thread_finalize(void)
{
    int n = CRYPTO_num_locks();
    int i;

    if (libcrypto_mutexes == NULL) {
        return;
    }

    CRYPTO_THREADID_set_callback(NULL);
    CRYPTO_set_locking_callback(NULL);

    for (i = 0; i < n; ++i) {
            user_callbacks->mutex_destroy(&libcrypto_mutexes[i]);
    }
    SAFE_FREE(libcrypto_mutexes);
}

#endif