1 | /* $Id: threads_iprt.c 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Crypto thread locking functions which make use of the IPRT.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2016-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 | #include <openssl/crypto.h>
|
---|
29 | #include "internal/cryptlib.h"
|
---|
30 |
|
---|
31 | #if defined(OPENSSL_THREADS)
|
---|
32 |
|
---|
33 | #include <iprt/asm.h>
|
---|
34 | #include <iprt/assert.h>
|
---|
35 | #include <iprt/critsect.h>
|
---|
36 | #include <iprt/errcore.h>
|
---|
37 | #include <iprt/log.h>
|
---|
38 | #include <iprt/process.h>
|
---|
39 |
|
---|
40 | /*
|
---|
41 | * Of course it's wrong to use a critical section to implement a read/write
|
---|
42 | * lock. But as the OpenSSL interface is too simple (there is only read_lock()/
|
---|
43 | * write_lock() and only unspecified unlock() and the Windows implementatio
|
---|
44 | * (threads_win.c) uses {Enter,Leave}CriticalSection we do that here as well.
|
---|
45 | */
|
---|
46 | CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void)
|
---|
47 | {
|
---|
48 | RTCRITSECT *pCritSect = (RTCRITSECT*)OPENSSL_zalloc(sizeof(RTCRITSECT));
|
---|
49 | if (pCritSect)
|
---|
50 | {
|
---|
51 | int rc = RTCritSectInitEx(pCritSect, 0, NIL_RTLOCKVALCLASS, RTLOCKVAL_SUB_CLASS_NONE, NULL);
|
---|
52 | if (RT_SUCCESS(rc))
|
---|
53 | return (CRYPTO_RWLOCK*)pCritSect;
|
---|
54 | OPENSSL_free(pCritSect);
|
---|
55 | }
|
---|
56 | return NULL;
|
---|
57 | }
|
---|
58 |
|
---|
59 | int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock)
|
---|
60 | {
|
---|
61 | PRTCRITSECT pCritSect = (PRTCRITSECT)lock;
|
---|
62 | int rc = RTCritSectEnter(pCritSect);
|
---|
63 | AssertRC(rc);
|
---|
64 | if (RT_FAILURE(rc))
|
---|
65 | return 0;
|
---|
66 |
|
---|
67 | return 1;
|
---|
68 | }
|
---|
69 |
|
---|
70 | int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock)
|
---|
71 | {
|
---|
72 | PRTCRITSECT pCritSect = (PRTCRITSECT)lock;
|
---|
73 | int rc = RTCritSectEnter(pCritSect);
|
---|
74 | AssertRC(rc);
|
---|
75 | if (RT_FAILURE(rc))
|
---|
76 | return 0;
|
---|
77 |
|
---|
78 | return 1;
|
---|
79 | }
|
---|
80 |
|
---|
81 | int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock)
|
---|
82 | {
|
---|
83 | PRTCRITSECT pCritSect = (PRTCRITSECT)lock;
|
---|
84 | int rc = RTCritSectLeave(pCritSect);
|
---|
85 | AssertRC(rc);
|
---|
86 | if (RT_FAILURE(rc))
|
---|
87 | return 0;
|
---|
88 |
|
---|
89 | return 1;
|
---|
90 | }
|
---|
91 |
|
---|
92 | void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock)
|
---|
93 | {
|
---|
94 | if (lock)
|
---|
95 | {
|
---|
96 | PRTCRITSECT pCritSect = (PRTCRITSECT)lock;
|
---|
97 | int rc = RTCritSectDelete(pCritSect);
|
---|
98 | AssertRC(rc);
|
---|
99 | OPENSSL_free(lock);
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *))
|
---|
104 | {
|
---|
105 | int rc = RTTlsAllocEx(key, (PFNRTTLSDTOR)cleanup); /* ASSUMES default calling convention is __cdecl, or close enough to it. */
|
---|
106 | if (RT_FAILURE(rc))
|
---|
107 | return 0;
|
---|
108 |
|
---|
109 | return 1;
|
---|
110 | }
|
---|
111 |
|
---|
112 | void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)
|
---|
113 | {
|
---|
114 | return RTTlsGet(*key);
|
---|
115 | }
|
---|
116 |
|
---|
117 | int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)
|
---|
118 | {
|
---|
119 | int rc = RTTlsSet(*key, val);
|
---|
120 | if (RT_FAILURE(rc))
|
---|
121 | return 0;
|
---|
122 |
|
---|
123 | return 1;
|
---|
124 | }
|
---|
125 |
|
---|
126 | int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key)
|
---|
127 | {
|
---|
128 | int rc = RTTlsFree(*key);
|
---|
129 | if (RT_FAILURE(rc))
|
---|
130 | return 0;
|
---|
131 |
|
---|
132 | return 1;
|
---|
133 | }
|
---|
134 |
|
---|
135 | CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void)
|
---|
136 | {
|
---|
137 | return RTThreadSelf();
|
---|
138 | }
|
---|
139 |
|
---|
140 | int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b)
|
---|
141 | {
|
---|
142 | return (a == b);
|
---|
143 | }
|
---|
144 |
|
---|
145 | int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
|
---|
146 | {
|
---|
147 | *ret = ASMAtomicAddS32((int32_t volatile*)val, amount) + amount;
|
---|
148 | return 1;
|
---|
149 | }
|
---|
150 |
|
---|
151 | int CRYPTO_atomic_or(uint64_t *val, uint64_t op, uint64_t *ret,
|
---|
152 | CRYPTO_RWLOCK *lock)
|
---|
153 | {
|
---|
154 | uint64_t u64RetOld = ASMAtomicUoReadU64(val);
|
---|
155 | uint64_t u64New;
|
---|
156 | do
|
---|
157 | u64New = u64RetOld | op;
|
---|
158 | while (!ASMAtomicCmpXchgExU64(val, u64New, u64RetOld, &u64RetOld));
|
---|
159 | *ret = u64RetOld;
|
---|
160 |
|
---|
161 | return 1;
|
---|
162 | }
|
---|
163 |
|
---|
164 | int CRYPTO_atomic_load(uint64_t *val, uint64_t *ret, CRYPTO_RWLOCK *lock)
|
---|
165 | {
|
---|
166 | *ret = ASMAtomicReadU64((uint64_t volatile *)val);
|
---|
167 | return 1;
|
---|
168 | }
|
---|
169 |
|
---|
170 | #endif
|
---|
171 |
|
---|
172 | int openssl_init_fork_handlers(void)
|
---|
173 | {
|
---|
174 | return 0;
|
---|
175 | }
|
---|
176 |
|
---|
177 | int openssl_get_fork_id(void)
|
---|
178 | {
|
---|
179 | return (int)RTProcSelf();
|
---|
180 | }
|
---|