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