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