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