Change all our uses of CRYPTO_THREAD_run_once to use RUN_ONCE instead
[openssl.git] / crypto / init.c
1 /*
2  * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <internal/cryptlib_int.h>
11 #include <openssl/err.h>
12 #include <internal/rand.h>
13 #include <internal/bio.h>
14 #include <openssl/evp.h>
15 #include <internal/evp_int.h>
16 #include <internal/conf.h>
17 #include <internal/async.h>
18 #include <internal/engine.h>
19 #include <internal/comp.h>
20 #include <internal/err.h>
21 #include <internal/err_int.h>
22 #include <internal/objects.h>
23 #include <stdlib.h>
24 #include <assert.h>
25 #include <internal/thread_once.h>
26
27 static int stopped = 0;
28
29 static void ossl_init_thread_stop(struct thread_local_inits_st *locals);
30
31 static CRYPTO_THREAD_LOCAL threadstopkey;
32
33 static void ossl_init_thread_stop_wrap(void *local)
34 {
35     ossl_init_thread_stop((struct thread_local_inits_st *)local);
36 }
37
38 static struct thread_local_inits_st *ossl_init_get_thread_local(int alloc)
39 {
40     struct thread_local_inits_st *local =
41         CRYPTO_THREAD_get_local(&threadstopkey);
42
43     if (local == NULL && alloc) {
44         local = OPENSSL_zalloc(sizeof *local);
45         CRYPTO_THREAD_set_local(&threadstopkey, local);
46     }
47     if (!alloc) {
48         CRYPTO_THREAD_set_local(&threadstopkey, NULL);
49     }
50
51     return local;
52 }
53
54 typedef struct ossl_init_stop_st OPENSSL_INIT_STOP;
55 struct ossl_init_stop_st {
56     void (*handler)(void);
57     OPENSSL_INIT_STOP *next;
58 };
59
60 static OPENSSL_INIT_STOP *stop_handlers = NULL;
61 static CRYPTO_RWLOCK *init_lock = NULL;
62
63 static CRYPTO_ONCE base = CRYPTO_ONCE_STATIC_INIT;
64 static int base_inited = 0;
65 DEFINE_RUN_ONCE_STATIC(ossl_init_base)
66 {
67 #ifdef OPENSSL_INIT_DEBUG
68     fprintf(stderr, "OPENSSL_INIT: ossl_init_base: Setting up stop handlers\n");
69 #endif
70     /*
71      * We use a dummy thread local key here. We use the destructor to detect
72      * when the thread is going to stop (where that feature is available)
73      */
74     CRYPTO_THREAD_init_local(&threadstopkey, ossl_init_thread_stop_wrap);
75 #ifndef OPENSSL_SYS_UEFI
76     atexit(OPENSSL_cleanup);
77 #endif
78     if ((init_lock = CRYPTO_THREAD_lock_new()) == NULL)
79         return 0;
80     OPENSSL_cpuid_setup();
81     base_inited = 1;
82     return 1;
83 }
84
85 static CRYPTO_ONCE load_crypto_strings = CRYPTO_ONCE_STATIC_INIT;
86 static int load_crypto_strings_inited = 0;
87 DEFINE_RUN_ONCE_STATIC(ossl_init_no_load_crypto_strings)
88 {
89     /* Do nothing in this case */
90     return 1;
91 }
92
93 DEFINE_RUN_ONCE_STATIC(ossl_init_load_crypto_strings)
94 {
95     /*
96      * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time
97      * pulling in all the error strings during static linking
98      */
99 #if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
100 # ifdef OPENSSL_INIT_DEBUG
101     fprintf(stderr, "OPENSSL_INIT: ossl_init_load_crypto_strings: "
102                     "err_load_crypto_strings_int()\n");
103 # endif
104     err_load_crypto_strings_int();
105 #endif
106     load_crypto_strings_inited = 1;
107     return 1;
108 }
109
110 static CRYPTO_ONCE add_all_ciphers = CRYPTO_ONCE_STATIC_INIT;
111 DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_ciphers)
112 {
113     /*
114      * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
115      * pulling in all the ciphers during static linking
116      */
117 #ifndef OPENSSL_NO_AUTOALGINIT
118 # ifdef OPENSSL_INIT_DEBUG
119     fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_ciphers: "
120                     "openssl_add_all_ciphers_int()\n");
121 # endif
122     openssl_add_all_ciphers_int();
123 #endif
124     return 1;
125 }
126
127 static CRYPTO_ONCE add_all_digests = CRYPTO_ONCE_STATIC_INIT;
128 DEFINE_RUN_ONCE_STATIC(ossl_init_add_all_digests)
129 {
130     /*
131      * OPENSSL_NO_AUTOALGINIT is provided here to prevent at compile time
132      * pulling in all the ciphers during static linking
133      */
134 #ifndef OPENSSL_NO_AUTOALGINIT
135 # ifdef OPENSSL_INIT_DEBUG
136     fprintf(stderr, "OPENSSL_INIT: ossl_init_add_all_digests: "
137                     "openssl_add_all_digests()\n");
138 # endif
139     openssl_add_all_digests_int();
140 #endif
141     return 1;
142 }
143
144 DEFINE_RUN_ONCE_STATIC(ossl_init_no_add_algs)
145 {
146     /* Do nothing */
147     return 1;
148 }
149
150 static CRYPTO_ONCE config = CRYPTO_ONCE_STATIC_INIT;
151 static int config_inited = 0;
152 static const char *appname;
153 DEFINE_RUN_ONCE_STATIC(ossl_init_config)
154 {
155 #ifdef OPENSSL_INIT_DEBUG
156     fprintf(stderr,
157             "OPENSSL_INIT: ossl_init_config: openssl_config(%s)\n",
158             appname == NULL ? "NULL" : appname);
159 #endif
160     openssl_config_int(appname);
161     config_inited = 1;
162     return 1;
163 }
164 DEFINE_RUN_ONCE_STATIC(ossl_init_no_config)
165 {
166 #ifdef OPENSSL_INIT_DEBUG
167     fprintf(stderr,
168             "OPENSSL_INIT: ossl_init_config: openssl_no_config_int()\n");
169 #endif
170     openssl_no_config_int();
171     config_inited = 1;
172     return 1;
173 }
174
175 static CRYPTO_ONCE async = CRYPTO_ONCE_STATIC_INIT;
176 static int async_inited = 0;
177 DEFINE_RUN_ONCE_STATIC(ossl_init_async)
178 {
179 #ifdef OPENSSL_INIT_DEBUG
180     fprintf(stderr, "OPENSSL_INIT: ossl_init_async: async_init()\n");
181 #endif
182     if (!async_init())
183         return 0;
184     async_inited = 1;
185     return 1;
186 }
187
188 #ifndef OPENSSL_NO_ENGINE
189 static CRYPTO_ONCE engine_openssl = CRYPTO_ONCE_STATIC_INIT;
190 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_openssl)
191 {
192 # ifdef OPENSSL_INIT_DEBUG
193     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_openssl: "
194                     "engine_load_openssl_int()\n");
195 # endif
196     engine_load_openssl_int();
197     return 1;
198 }
199 # if !defined(OPENSSL_NO_HW) && \
200     (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV))
201 static CRYPTO_ONCE engine_cryptodev = CRYPTO_ONCE_STATIC_INIT;
202 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_cryptodev)
203 {
204 #  ifdef OPENSSL_INIT_DEBUG
205     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_cryptodev: "
206                     "engine_load_cryptodev_int()\n");
207 #  endif
208     engine_load_cryptodev_int();
209     return 1;
210 }
211 # endif
212
213 # ifndef OPENSSL_NO_RDRAND
214 static CRYPTO_ONCE engine_rdrand = CRYPTO_ONCE_STATIC_INIT;
215 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_rdrand)
216 {
217 #  ifdef OPENSSL_INIT_DEBUG
218     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_rdrand: "
219                     "engine_load_rdrand_int()\n");
220 #  endif
221     engine_load_rdrand_int();
222     return 1;
223 }
224 # endif
225 static CRYPTO_ONCE engine_dynamic = CRYPTO_ONCE_STATIC_INIT;
226 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_dynamic)
227 {
228 # ifdef OPENSSL_INIT_DEBUG
229     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_dynamic: "
230                     "engine_load_dynamic_int()\n");
231 # endif
232     engine_load_dynamic_int();
233     return 1;
234 }
235 # ifndef OPENSSL_NO_STATIC_ENGINE
236 #  if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
237 static CRYPTO_ONCE engine_padlock = CRYPTO_ONCE_STATIC_INIT;
238 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_padlock)
239 {
240 #   ifdef OPENSSL_INIT_DEBUG
241     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_padlock: "
242                     "engine_load_padlock_int()\n");
243 #   endif
244     engine_load_padlock_int();
245     return 1;
246 }
247 #  endif
248 #  if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
249 static CRYPTO_ONCE engine_capi = CRYPTO_ONCE_STATIC_INIT;
250 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_capi)
251 {
252 #   ifdef OPENSSL_INIT_DEBUG
253     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_capi: "
254                     "engine_load_capi_int()\n");
255 #   endif
256     engine_load_capi_int();
257     return 1;
258 }
259 #  endif
260 static CRYPTO_ONCE engine_dasync = CRYPTO_ONCE_STATIC_INIT;
261 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_dasync)
262 {
263 # ifdef OPENSSL_INIT_DEBUG
264     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_dasync: "
265                     "engine_load_dasync_int()\n");
266 # endif
267     engine_load_dasync_int();
268     return 1;
269 }
270 #  if !defined(OPENSSL_NO_AFALGENG)
271 static CRYPTO_ONCE engine_afalg = CRYPTO_ONCE_STATIC_INIT;
272 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_afalg)
273 {
274 #   ifdef OPENSSL_INIT_DEBUG
275     fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_afalg: "
276                     "engine_load_afalg_int()\n");
277 #   endif
278     engine_load_afalg_int();
279     return 1;
280 }
281 #  endif
282 # endif
283 #endif
284
285 #ifndef OPENSSL_NO_COMP
286 static CRYPTO_ONCE zlib = CRYPTO_ONCE_STATIC_INIT;
287
288 static int zlib_inited = 0;
289 DEFINE_RUN_ONCE_STATIC(ossl_init_zlib)
290 {
291     /* Do nothing - we need to know about this for the later cleanup */
292     zlib_inited = 1;
293     return 1;
294 }
295 #endif
296
297 static void ossl_init_thread_stop(struct thread_local_inits_st *locals)
298 {
299     /* Can't do much about this */
300     if (locals == NULL)
301         return;
302
303     if (locals->async) {
304 #ifdef OPENSSL_INIT_DEBUG
305         fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
306                         "ASYNC_cleanup_thread()\n");
307 #endif
308         ASYNC_cleanup_thread();
309     }
310
311     if (locals->err_state) {
312 #ifdef OPENSSL_INIT_DEBUG
313         fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_stop: "
314                         "err_delete_thread_state()\n");
315 #endif
316         err_delete_thread_state();
317     }
318
319     OPENSSL_free(locals);
320 }
321
322 void OPENSSL_thread_stop(void)
323 {
324     ossl_init_thread_stop(
325         (struct thread_local_inits_st *)ossl_init_get_thread_local(0));
326 }
327
328 int ossl_init_thread_start(uint64_t opts)
329 {
330     struct thread_local_inits_st *locals = ossl_init_get_thread_local(1);
331
332     if (locals == NULL)
333         return 0;
334
335     if (opts & OPENSSL_INIT_THREAD_ASYNC) {
336 #ifdef OPENSSL_INIT_DEBUG
337         fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
338                         "marking thread for async\n");
339 #endif
340         locals->async = 1;
341     }
342
343     if (opts & OPENSSL_INIT_THREAD_ERR_STATE) {
344 #ifdef OPENSSL_INIT_DEBUG
345         fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
346                         "marking thread for err_state\n");
347 #endif
348         locals->err_state = 1;
349     }
350
351     return 1;
352 }
353
354 void OPENSSL_cleanup(void)
355 {
356     OPENSSL_INIT_STOP *currhandler, *lasthandler;
357
358     /* If we've not been inited then no need to deinit */
359     if (!base_inited)
360         return;
361
362     /* Might be explicitly called and also by atexit */
363     if (stopped)
364         return;
365     stopped = 1;
366
367     /*
368      * Thread stop may not get automatically called by the thread library for
369      * the very last thread in some situations, so call it directly.
370      */
371     ossl_init_thread_stop(ossl_init_get_thread_local(0));
372
373     currhandler = stop_handlers;
374     while (currhandler != NULL) {
375         currhandler->handler();
376         lasthandler = currhandler;
377         currhandler = currhandler->next;
378         OPENSSL_free(lasthandler);
379     }
380     stop_handlers = NULL;
381
382     CRYPTO_THREAD_lock_free(init_lock);
383
384     /*
385      * We assume we are single-threaded for this function, i.e. no race
386      * conditions for the various "*_inited" vars below.
387      */
388
389 #ifndef OPENSSL_NO_COMP
390     if (zlib_inited) {
391 #ifdef OPENSSL_INIT_DEBUG
392         fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
393                         "comp_zlib_cleanup_int()\n");
394 #endif
395         comp_zlib_cleanup_int();
396     }
397 #endif
398
399     if (async_inited) {
400 # ifdef OPENSSL_INIT_DEBUG
401         fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
402                         "async_deinit()\n");
403 # endif
404         async_deinit();
405     }
406
407     if (load_crypto_strings_inited) {
408 #ifdef OPENSSL_INIT_DEBUG
409         fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
410                         "err_free_strings_int()\n");
411 #endif
412         err_free_strings_int();
413     }
414
415     CRYPTO_THREAD_cleanup_local(&threadstopkey);
416
417 #ifdef OPENSSL_INIT_DEBUG
418     fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
419                     "rand_cleanup_int()\n");
420     fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
421                     "conf_modules_free_int()\n");
422 #ifndef OPENSSL_NO_ENGINE
423     fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
424                     "engine_cleanup_int()\n");
425 #endif
426     fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
427                     "crypto_cleanup_all_ex_data_int()\n");
428     fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
429                     "bio_sock_cleanup_int()\n");
430     fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
431                     "bio_cleanup()\n");
432     fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
433                     "evp_cleanup_int()\n");
434     fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
435                     "obj_cleanup_int()\n");
436     fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
437                     "err_cleanup()\n");
438 #endif
439     /*
440      * Note that cleanup order is important:
441      * - rand_cleanup_int could call an ENGINE's RAND cleanup function so
442      * must be called before engine_cleanup_int()
443      * - ENGINEs use CRYPTO_EX_DATA and therefore, must be cleaned up
444      * before the ex data handlers are wiped in CRYPTO_cleanup_all_ex_data().
445      * - conf_modules_free_int() can end up in ENGINE code so must be called
446      * before engine_cleanup_int()
447      * - ENGINEs and additional EVP algorithms might use added OIDs names so
448      * obj_cleanup_int() must be called last
449      */
450     rand_cleanup_int();
451     conf_modules_free_int();
452 #ifndef OPENSSL_NO_ENGINE
453     engine_cleanup_int();
454 #endif
455     crypto_cleanup_all_ex_data_int();
456     bio_cleanup();
457     evp_cleanup_int();
458     obj_cleanup_int();
459     err_cleanup();
460
461     base_inited = 0;
462 }
463
464 /*
465  * If this function is called with a non NULL settings value then it must be
466  * called prior to any threads making calls to any OpenSSL functions,
467  * i.e. passing a non-null settings value is assumed to be single-threaded.
468  */
469 int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
470 {
471     static int stoperrset = 0;
472
473     if (stopped) {
474         if (!stoperrset) {
475             /*
476              * We only ever set this once to avoid getting into an infinite
477              * loop where the error system keeps trying to init and fails so
478              * sets an error etc
479              */
480             stoperrset = 1;
481             CRYPTOerr(CRYPTO_F_OPENSSL_INIT_CRYPTO, ERR_R_INIT_FAIL);
482         }
483         return 0;
484     }
485
486     if (!RUN_ONCE(&base, ossl_init_base))
487         return 0;
488
489     if ((opts & OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS)
490             && !RUN_ONCE(&load_crypto_strings,
491                          ossl_init_no_load_crypto_strings))
492         return 0;
493
494     if ((opts & OPENSSL_INIT_LOAD_CRYPTO_STRINGS)
495             && !RUN_ONCE(&load_crypto_strings, ossl_init_load_crypto_strings))
496         return 0;
497
498     if ((opts & OPENSSL_INIT_NO_ADD_ALL_CIPHERS)
499             && !RUN_ONCE(&add_all_ciphers, ossl_init_no_add_algs))
500         return 0;
501
502     if ((opts & OPENSSL_INIT_ADD_ALL_CIPHERS)
503             && !RUN_ONCE(&add_all_ciphers, ossl_init_add_all_ciphers))
504         return 0;
505
506     if ((opts & OPENSSL_INIT_NO_ADD_ALL_DIGESTS)
507             && !RUN_ONCE(&add_all_digests, ossl_init_no_add_algs))
508         return 0;
509
510     if ((opts & OPENSSL_INIT_ADD_ALL_DIGESTS)
511             && !RUN_ONCE(&add_all_digests, ossl_init_add_all_digests))
512         return 0;
513
514     if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG)
515             && !RUN_ONCE(&config, ossl_init_no_config))
516         return 0;
517
518     if (opts & OPENSSL_INIT_LOAD_CONFIG) {
519         int ret;
520         CRYPTO_THREAD_write_lock(init_lock);
521         appname = (settings == NULL) ? NULL : settings->appname;
522         ret = RUN_ONCE(&config, ossl_init_config);
523         CRYPTO_THREAD_unlock(init_lock);
524         if (!ret)
525             return 0;
526     }
527
528     if ((opts & OPENSSL_INIT_ASYNC)
529             && !RUN_ONCE(&async, ossl_init_async))
530         return 0;
531
532 #ifndef OPENSSL_NO_ENGINE
533     if ((opts & OPENSSL_INIT_ENGINE_OPENSSL)
534             && !RUN_ONCE(&engine_openssl, ossl_init_engine_openssl))
535         return 0;
536 # if !defined(OPENSSL_NO_HW) && \
537     (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV))
538     if ((opts & OPENSSL_INIT_ENGINE_CRYPTODEV)
539             && !RUN_ONCE(&engine_cryptodev, ossl_init_engine_cryptodev))
540         return 0;
541 # endif
542 # ifndef OPENSSL_NO_RDRAND
543     if ((opts & OPENSSL_INIT_ENGINE_RDRAND)
544             && !RUN_ONCE(&engine_rdrand, ossl_init_engine_rdrand))
545         return 0;
546 # endif
547     if ((opts & OPENSSL_INIT_ENGINE_DYNAMIC)
548             && !RUN_ONCE(&engine_dynamic, ossl_init_engine_dynamic))
549         return 0;
550 # ifndef OPENSSL_NO_STATIC_ENGINE
551 #  if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
552     if ((opts & OPENSSL_INIT_ENGINE_PADLOCK)
553             && !RUN_ONCE(&engine_padlock, ossl_init_engine_padlock))
554         return 0;
555 #  endif
556 #  if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
557     if ((opts & OPENSSL_INIT_ENGINE_CAPI)
558             && !RUN_ONCE(&engine_capi, ossl_init_engine_capi))
559         return 0;
560 #  endif
561     if ((opts & OPENSSL_INIT_ENGINE_DASYNC)
562             && !RUN_ONCE(&engine_dasync, ossl_init_engine_dasync))
563         return 0;
564 #  if !defined(OPENSSL_NO_AFALGENG)
565     if ((opts & OPENSSL_INIT_ENGINE_AFALG)
566             && !RUN_ONCE(&engine_afalg, ossl_init_engine_afalg))
567         return 0;
568 #  endif
569 # endif
570     if (opts & (OPENSSL_INIT_ENGINE_ALL_BUILTIN
571                 | OPENSSL_INIT_ENGINE_DASYNC | OPENSSL_INIT_ENGINE_OPENSSL
572                 | OPENSSL_INIT_ENGINE_AFALG)) {
573         ENGINE_register_all_complete();
574     }
575 #endif
576
577 #ifndef OPENSSL_NO_COMP
578     if ((opts & OPENSSL_INIT_ZLIB)
579             && !RUN_ONCE(&zlib, ossl_init_zlib))
580         return 0;
581 #endif
582
583     return 1;
584 }
585
586 int OPENSSL_atexit(void (*handler)(void))
587 {
588     OPENSSL_INIT_STOP *newhand;
589
590     newhand = OPENSSL_malloc(sizeof(*newhand));
591     if (newhand == NULL)
592         return 0;
593
594     newhand->handler = handler;
595     newhand->next = stop_handlers;
596     stop_handlers = newhand;
597
598     return 1;
599 }