2 Bacula® - The Network Backup Solution
4 Copyright (C) 2005-2009 Free Software Foundation Europe e.V.
6 The main author of Bacula is Kern Sibbald, with contributions from
7 many others, a complete list can be found in the file AUTHORS.
8 This program is Free Software; you can redistribute it and/or
9 modify it under the terms of version two of the GNU General Public
10 License as published by the Free Software Foundation and included
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 Bacula® is a registered trademark of Kern Sibbald.
24 The licensor of Bacula is the Free Software Foundation Europe
25 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26 Switzerland, email:ftf@fsfeurope.org.
29 * openssl.c OpenSSL support functions
31 * Author: Landon Fuller <landonf@opendarwin.org>
35 * This file was contributed to the Bacula project by Landon Fuller.
37 * Landon Fuller has been granted a perpetual, worldwide, non-exclusive,
38 * no-charge, royalty-free, irrevocable copyright license to reproduce,
39 * prepare derivative works of, publicly display, publicly perform,
40 * sublicense, and distribute the original work contributed by Landon Fuller
41 * to the Bacula project in source or object form.
43 * If you wish to license these contributions under an alternate open source
44 * license please contact Landon Fuller <landonf@opendarwin.org>.
53 /* Array of mutexes for use with OpenSSL static locking */
54 static pthread_mutex_t *mutexes;
56 /* OpenSSL dynamic locking structure */
57 struct CRYPTO_dynlock_value {
58 pthread_mutex_t mutex;
62 * ***FIXME*** this is a sort of dummy to avoid having to
63 * change all the existing code to pass either a jcr or
64 * a NULL. Passing a NULL causes the messages to be
65 * printed by the daemon -- not very good :-(
67 void openssl_post_errors(int code, const char *errstring)
69 openssl_post_errors(NULL, code, errstring);
74 * Post all per-thread openssl errors
76 void openssl_post_errors(JCR *jcr, int code, const char *errstring)
81 /* Pop errors off of the per-thread queue */
82 while((sslerr = ERR_get_error()) != 0) {
83 /* Acquire the human readable string */
84 ERR_error_string_n(sslerr, buf, sizeof(buf));
85 Dmsg3(50, "jcr=%p %s: ERR=%s\n", jcr, errstring, buf);
86 Qmsg2(jcr, M_ERROR, 0, "%s: ERR=%s\n", errstring, buf);
91 * Return an OpenSSL thread ID
95 static unsigned long get_openssl_thread_id(void)
98 return (unsigned long)getpid();
101 * Comparison without use of pthread_equal() is mandated by the OpenSSL API
103 * Note: this creates problems with the new Win32 pthreads
104 * emulation code, which defines pthread_t as a structure.
106 return ((unsigned long)pthread_self());
111 * Allocate a dynamic OpenSSL mutex
113 static struct CRYPTO_dynlock_value *openssl_create_dynamic_mutex (const char *file, int line)
115 struct CRYPTO_dynlock_value *dynlock;
118 dynlock = (struct CRYPTO_dynlock_value *)malloc(sizeof(struct CRYPTO_dynlock_value));
120 if ((stat = pthread_mutex_init(&dynlock->mutex, NULL)) != 0) {
122 Jmsg1(NULL, M_ABORT, 0, _("Unable to init mutex: ERR=%s\n"), be.bstrerror(stat));
128 static void openssl_update_dynamic_mutex(int mode, struct CRYPTO_dynlock_value *dynlock, const char *file, int line)
130 if (mode & CRYPTO_LOCK) {
137 static void openssl_destroy_dynamic_mutex(struct CRYPTO_dynlock_value *dynlock, const char *file, int line)
141 if ((stat = pthread_mutex_destroy(&dynlock->mutex)) != 0) {
143 Jmsg1(NULL, M_ABORT, 0, _("Unable to destroy mutex: ERR=%s\n"), be.bstrerror(stat));
150 * (Un)Lock a static OpenSSL mutex
152 static void openssl_update_static_mutex (int mode, int i, const char *file, int line)
154 if (mode & CRYPTO_LOCK) {
162 * Initialize OpenSSL thread support
163 * Returns: 0 on success
166 int openssl_init_threads (void)
172 /* Set thread ID callback */
173 CRYPTO_set_id_callback(get_openssl_thread_id);
175 /* Initialize static locking */
176 numlocks = CRYPTO_num_locks();
177 mutexes = (pthread_mutex_t *) malloc(numlocks * sizeof(pthread_mutex_t));
178 for (i = 0; i < numlocks; i++) {
179 if ((stat = pthread_mutex_init(&mutexes[i], NULL)) != 0) {
181 Jmsg1(NULL, M_FATAL, 0, _("Unable to init mutex: ERR=%s\n"), be.bstrerror(stat));
186 /* Set static locking callback */
187 CRYPTO_set_locking_callback(openssl_update_static_mutex);
189 /* Initialize dyanmic locking */
190 CRYPTO_set_dynlock_create_callback(openssl_create_dynamic_mutex);
191 CRYPTO_set_dynlock_lock_callback(openssl_update_dynamic_mutex);
192 CRYPTO_set_dynlock_destroy_callback(openssl_destroy_dynamic_mutex);
198 * Clean up OpenSSL threading support
200 void openssl_cleanup_threads(void)
205 /* Unset thread ID callback */
206 CRYPTO_set_id_callback(NULL);
208 /* Deallocate static lock mutexes */
209 numlocks = CRYPTO_num_locks();
210 for (i = 0; i < numlocks; i++) {
211 if ((stat = pthread_mutex_destroy(&mutexes[i])) != 0) {
213 /* We don't halt execution, reporting the error should be sufficient */
214 Jmsg1(NULL, M_ERROR, 0, _("Unable to destroy mutex: ERR=%s\n"),
219 /* Unset static locking callback */
220 CRYPTO_set_locking_callback(NULL);
222 /* Free static lock array */
225 /* Unset dynamic locking callbacks */
226 CRYPTO_set_dynlock_create_callback(NULL);
227 CRYPTO_set_dynlock_lock_callback(NULL);
228 CRYPTO_set_dynlock_destroy_callback(NULL);
234 * Returns: 1 on success
237 int openssl_seed_prng (void)
239 const char *names[] = { "/dev/urandom", "/dev/random", NULL };
244 // Read saved entropy?
246 for (i = 0; names[i]; i++) {
247 if (RAND_load_file(names[i], 1024) != -1) {
258 * Save OpenSSL Entropy
259 * Returns: 1 on success
262 int openssl_save_prng (void)
265 // Implement PRNG state save
269 #endif /* HAVE_OPENSSL */