1 .\" $OpenBSD: CRYPTO_set_locking_callback.3,v 1.5 2016/11/23 16:28:23 schwarze Exp $
2 .\" OpenSSL doc/crypto/threads.pod fb552ac6 Sep 30 23:43:01 2009 +0000
4 .\" This file was written by Ulf Moeller <ulf@openssl.org>,
5 .\" Richard Levitte <levitte@openssl.org>, Bodo Moeller <bodo@openssl.org>,
6 .\" and Geoff Thorpe <geoff@openssl.org>.
7 .\" Copyright (c) 2000, 2001, 2005, 2006, 2008, 2009 The OpenSSL Project.
8 .\" All rights reserved.
10 .\" Redistribution and use in source and binary forms, with or without
11 .\" modification, are permitted provided that the following conditions
14 .\" 1. Redistributions of source code must retain the above copyright
15 .\" notice, this list of conditions and the following disclaimer.
17 .\" 2. Redistributions in binary form must reproduce the above copyright
18 .\" notice, this list of conditions and the following disclaimer in
19 .\" the documentation and/or other materials provided with the
22 .\" 3. All advertising materials mentioning features or use of this
23 .\" software must display the following acknowledgment:
24 .\" "This product includes software developed by the OpenSSL Project
25 .\" for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
27 .\" 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
28 .\" endorse or promote products derived from this software without
29 .\" prior written permission. For written permission, please contact
30 .\" openssl-core@openssl.org.
32 .\" 5. Products derived from this software may not be called "OpenSSL"
33 .\" nor may "OpenSSL" appear in their names without prior written
34 .\" permission of the OpenSSL Project.
36 .\" 6. Redistributions of any form whatsoever must retain the following
38 .\" "This product includes software developed by the OpenSSL Project
39 .\" for use in the OpenSSL Toolkit (http://www.openssl.org/)"
41 .\" THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
42 .\" EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
44 .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
45 .\" ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46 .\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
47 .\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
48 .\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
50 .\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
51 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
52 .\" OF THE POSSIBILITY OF SUCH DAMAGE.
54 .Dd $Mdocdate: November 23 2016 $
55 .Dt CRYPTO_SET_LOCKING_CALLBACK 3
58 .Nm CRYPTO_THREADID_set_numeric ,
59 .Nm CRYPTO_THREADID_set_pointer ,
60 .Nm CRYPTO_THREADID_set_callback ,
61 .Nm CRYPTO_THREADID_get_callback ,
62 .Nm CRYPTO_THREADID_current ,
63 .Nm CRYPTO_THREADID_cmp ,
64 .Nm CRYPTO_THREADID_cpy ,
65 .Nm CRYPTO_THREADID_hash ,
66 .Nm CRYPTO_num_locks ,
67 .Nm CRYPTO_set_dynlock_create_callback ,
68 .Nm CRYPTO_set_dynlock_lock_callback ,
69 .Nm CRYPTO_set_dynlock_destroy_callback ,
70 .Nm CRYPTO_get_new_dynlockid ,
71 .Nm CRYPTO_destroy_dynlockid ,
78 .Nd OpenSSL thread support
82 /* Don't use this structure directly. */
83 typedef struct crypto_threadid_st {
87 /* Only use CRYPTO_THREADID_set_[numeric|pointer]() within callbacks */
91 .Fo CRYPTO_THREADID_set_numeric
92 .Fa "CRYPTO_THREADID *id"
93 .Fa "unsigned long val"
96 .Fo CRYPTO_THREADID_set_pointer
97 .Fa "CRYPTO_THREADID *id"
101 .Fo CRYPTO_THREADID_set_callback
102 .Fa "void (*threadid_func)(CRYPTO_THREADID *)"
105 .Fo "(*CRYPTO_THREADID_get_callback(void))"
106 .Fa "CRYPTO_THREADID *"
109 .Fo CRYPTO_THREADID_current
110 .Fa "CRYPTO_THREADID *id"
113 .Fo CRYPTO_THREADID_cmp
114 .Fa "const CRYPTO_THREADID *a"
115 .Fa "const CRYPTO_THREADID *b"
118 .Fo CRYPTO_THREADID_cpy
119 .Fa "CRYPTO_THREADID *dest"
120 .Fa "const CRYPTO_THREADID *src"
123 .Fo CRYPTO_THREADID_hash
124 .Fa "const CRYPTO_THREADID *id"
131 /* struct CRYPTO_dynlock_value needs to be defined by the user */
132 struct CRYPTO_dynlock_value;
136 .Fo CRYPTO_set_dynlock_create_callback
137 .Fa "struct CRYPTO_dynlock_value *"
138 .Fa "(*dyn_create_function)(char *file, int line)"
141 .Fo CRYPTO_set_dynlock_lock_callback
142 .Fa "void (*dyn_lock_function)(int mode, struct CRYPTO_dynlock_value *l,\
143 const char *file, int line)"
146 .Fo CRYPTO_set_dynlock_destroy_callback
147 .Fa "void (*dyn_destroy_function)(struct CRYPTO_dynlock_value *l,\
148 const char *file, int line)"
151 .Fo CRYPTO_get_new_dynlockid
155 .Fo CRYPTO_destroy_dynlockid
162 .Fa "const char *file"
166 #define CRYPTO_w_lock(type) \e
167 CRYPTO_lock(CRYPTO_LOCK|CRYPTO_WRITE, type, __FILE__, __LINE__)
168 #define CRYPTO_w_unlock(type) \e
169 CRYPTO_lock(CRYPTO_UNLOCK|CRYPTO_WRITE, type, __FILE__, __LINE__)
170 #define CRYPTO_r_lock(type) \e
171 CRYPTO_lock(CRYPTO_LOCK|CRYPTO_READ, type, __FILE__, __LINE__)
172 #define CRYPTO_r_unlock(type) \e
173 CRYPTO_lock(CRYPTO_UNLOCK|CRYPTO_READ, type, __FILE__, __LINE__)
174 #define CRYPTO_add(addr,amount,type) \e
175 CRYPTO_add_lock(addr, amount, type, __FILE__, __LINE__)
178 OpenSSL can safely be used in multi-threaded applications provided that
179 at least two callback functions are set,
187 .Fa "const char *file"
190 is needed to perform locking on shared data structures.
191 Note that OpenSSL uses a number of global data structures that will be
192 implicitly shared whenever multiple threads use OpenSSL.
193 Multi-threaded applications will crash at random if it is not set.
196 must be able to handle up to
198 different mutex locks.
205 and releases it otherwise.
210 are the file number of the function setting the lock.
211 They can be useful for debugging.
214 .Fa "CRYPTO_THREADID *id"
216 is needed to record the currently-executing thread's identifier into
218 The implementation of this callback should not fill in
220 directly, but should use
221 .Fn CRYPTO_THREADID_set_numeric
222 if thread IDs are numeric, or
223 .Fn CRYPTO_THREADID_set_pointer
224 if they are pointer-based.
225 If the application does not register such a callback using
226 .Fn CRYPTO_THREADID_set_callback ,
227 then a default implementation is used - on Windows and BeOS this uses
228 the system's default thread identifying APIs, and on all other platforms
229 it uses the address of
231 The latter is satisfactory for thread-safety if and only if the platform
232 has a thread-local error number facility.
236 is registered, or if the built-in default implementation is to be used,
239 .Fn CRYPTO_THREADID_current
240 records the currently-executing thread ID into the given
244 .Fn CRYPTO_THREADID_cmp
245 compares two thread IDs (returning zero for equality, i.e. the same
249 .Fn CRYPTO_THREADID_cpy
250 duplicates a thread ID value.
252 .Fn CRYPTO_THREADID_hash
253 returns a numeric value usable as a hash-table key.
254 This is usually the exact numeric or pointer-based thread ID used
255 internally, however this also handles the unusual case where pointers
258 variables and the platform's thread IDs are pointer-based \(em in
259 this case, mixing is done to attempt to produce a unique numeric
260 value even though it is not as wide as the platform's true thread
264 Additionally, OpenSSL supports dynamic locks and, sometimes, some parts
265 of OpenSSL need it for better performance.
266 To enable this, the following is required:
269 Three additional callback functions,
270 .Fn dyn_create_function ,
271 .Fn dyn_lock_function ,
273 .Fn dyn_destroy_function .
275 A structure defined with the data that each lock needs to handle.
278 .Vt struct CRYPTO_dynlock_value
279 has to be defined to contain whatever structure is needed to handle locks.
281 .Fo dyn_create_function
282 .Fa "const char *file"
285 is needed to create a lock.
286 Multi-threaded applications might crash at random if it is not set.
288 .Fo dyn_lock_function
290 .Fa "CRYPTO_dynlock *l"
291 .Fa "const char *file"
294 is needed to perform locking off dynamic lock numbered n.
295 Multi-threaded applications might crash at random if it is not set.
297 .Fo dyn_destroy_function
298 .Fa "CRYPTO_dynlock *l"
299 .Fa "const char *file"
302 is needed to destroy the lock
304 Multi-threaded applications might crash at random if it is not set.
306 .Fn CRYPTO_get_new_dynlockid
307 is used to create locks.
309 .Fn dyn_create_function
310 for the actual creation.
312 .Fn CRYPTO_destroy_dynlockid
313 is used to destroy locks.
315 .Fn dyn_destroy_function
316 for the actual destruction.
319 is used to lock and unlock the locks.
321 is a bitfield describing what should be done with the lock.
323 is the number of the lock as returned from
324 .Fn CRYPTO_get_new_dynlockid .
326 can be combined from the following values.
327 These values are pairwise exclusive, with undefined behaviour if misused
332 should not be used together):
333 .Bd -literal -offset indent
340 You can find out if OpenSSL was configured with thread support:
341 .Bd -literal -offset indent
342 #define OPENSSL_THREAD_DEFINES
343 #include <openssl/opensslconf.h>
344 #if defined(OPENSSL_THREADS)
345 /* thread support enabled */
347 /* no thread support */
351 Also, dynamic locks are currently not used internally by OpenSSL, but
352 may do so in the future.
355 returns the required number of locks.
357 .Fn CRYPTO_get_new_dynlockid
358 returns the index to the newly created lock.
360 The other functions return no values.
362 .Pa crypto/threads/mttest.c
363 shows examples of the callback functions on Solaris, Irix and Win32.
367 .Fn CRYPTO_set_locking_callback
368 is available in all versions of SSLeay and OpenSSL.
370 was added in OpenSSL 0.9.4.
371 All functions dealing with dynamic locks were added in OpenSSL 0.9.5b-dev.
373 and associated functions were introduced in OpenSSL 1.0.0 to replace
374 (actually, deprecate) the previous
375 .Fn CRYPTO_set_id_callback ,
376 .Fn CRYPTO_get_id_callback ,
379 functions which assumed thread IDs to always be represented by