1*4724848cSchristos /*
2*4724848cSchristos * Copyright 2001-2017 The OpenSSL Project Authors. All Rights Reserved.
3c9496f6bSchristos *
4*4724848cSchristos * Licensed under the OpenSSL license (the "License"). You may not use
5*4724848cSchristos * this file except in compliance with the License. You can obtain a copy
6*4724848cSchristos * in the file LICENSE in the source distribution or at
7*4724848cSchristos * https://www.openssl.org/source/license.html
8c9496f6bSchristos */
9c9496f6bSchristos
10*4724848cSchristos #include "e_os.h"
11*4724848cSchristos #include "eng_local.h"
12c9496f6bSchristos
13c9496f6bSchristos /*
14c9496f6bSchristos * Initialise a engine type for use (or up its functional reference count if
15c9496f6bSchristos * it's already in use). This version is only used internally.
16c9496f6bSchristos */
engine_unlocked_init(ENGINE * e)17c9496f6bSchristos int engine_unlocked_init(ENGINE *e)
18c9496f6bSchristos {
19c9496f6bSchristos int to_return = 1;
20c9496f6bSchristos
21c9496f6bSchristos if ((e->funct_ref == 0) && e->init)
22c9496f6bSchristos /*
23c9496f6bSchristos * This is the first functional reference and the engine requires
24c9496f6bSchristos * initialisation so we do it now.
25c9496f6bSchristos */
26c9496f6bSchristos to_return = e->init(e);
27c9496f6bSchristos if (to_return) {
28c9496f6bSchristos /*
29c9496f6bSchristos * OK, we return a functional reference which is also a structural
30c9496f6bSchristos * reference.
31c9496f6bSchristos */
32c9496f6bSchristos e->struct_ref++;
33c9496f6bSchristos e->funct_ref++;
34*4724848cSchristos engine_ref_debug(e, 0, 1);
35*4724848cSchristos engine_ref_debug(e, 1, 1);
36c9496f6bSchristos }
37c9496f6bSchristos return to_return;
38c9496f6bSchristos }
39c9496f6bSchristos
40c9496f6bSchristos /*
41c9496f6bSchristos * Free a functional reference to a engine type. This version is only used
42c9496f6bSchristos * internally.
43c9496f6bSchristos */
engine_unlocked_finish(ENGINE * e,int unlock_for_handlers)44c9496f6bSchristos int engine_unlocked_finish(ENGINE *e, int unlock_for_handlers)
45c9496f6bSchristos {
46c9496f6bSchristos int to_return = 1;
47c9496f6bSchristos
48c9496f6bSchristos /*
49c9496f6bSchristos * Reduce the functional reference count here so if it's the terminating
50c9496f6bSchristos * case, we can release the lock safely and call the finish() handler
51c9496f6bSchristos * without risk of a race. We get a race if we leave the count until
52c9496f6bSchristos * after and something else is calling "finish" at the same time -
53c9496f6bSchristos * there's a chance that both threads will together take the count from 2
54c9496f6bSchristos * to 0 without either calling finish().
55c9496f6bSchristos */
56c9496f6bSchristos e->funct_ref--;
57c9496f6bSchristos engine_ref_debug(e, 1, -1);
58c9496f6bSchristos if ((e->funct_ref == 0) && e->finish) {
59c9496f6bSchristos if (unlock_for_handlers)
60*4724848cSchristos CRYPTO_THREAD_unlock(global_engine_lock);
61c9496f6bSchristos to_return = e->finish(e);
62c9496f6bSchristos if (unlock_for_handlers)
63*4724848cSchristos CRYPTO_THREAD_write_lock(global_engine_lock);
64c9496f6bSchristos if (!to_return)
65c9496f6bSchristos return 0;
66c9496f6bSchristos }
67*4724848cSchristos REF_ASSERT_ISNT(e->funct_ref < 0);
68c9496f6bSchristos /* Release the structural reference too */
69c9496f6bSchristos if (!engine_free_util(e, 0)) {
70c9496f6bSchristos ENGINEerr(ENGINE_F_ENGINE_UNLOCKED_FINISH, ENGINE_R_FINISH_FAILED);
71c9496f6bSchristos return 0;
72c9496f6bSchristos }
73c9496f6bSchristos return to_return;
74c9496f6bSchristos }
75c9496f6bSchristos
76c9496f6bSchristos /* The API (locked) version of "init" */
ENGINE_init(ENGINE * e)77c9496f6bSchristos int ENGINE_init(ENGINE *e)
78c9496f6bSchristos {
79c9496f6bSchristos int ret;
80c9496f6bSchristos if (e == NULL) {
81c9496f6bSchristos ENGINEerr(ENGINE_F_ENGINE_INIT, ERR_R_PASSED_NULL_PARAMETER);
82c9496f6bSchristos return 0;
83c9496f6bSchristos }
84*4724848cSchristos if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) {
85*4724848cSchristos ENGINEerr(ENGINE_F_ENGINE_INIT, ERR_R_MALLOC_FAILURE);
86*4724848cSchristos return 0;
87*4724848cSchristos }
88*4724848cSchristos CRYPTO_THREAD_write_lock(global_engine_lock);
89c9496f6bSchristos ret = engine_unlocked_init(e);
90*4724848cSchristos CRYPTO_THREAD_unlock(global_engine_lock);
91c9496f6bSchristos return ret;
92c9496f6bSchristos }
93c9496f6bSchristos
94c9496f6bSchristos /* The API (locked) version of "finish" */
ENGINE_finish(ENGINE * e)95c9496f6bSchristos int ENGINE_finish(ENGINE *e)
96c9496f6bSchristos {
97c9496f6bSchristos int to_return = 1;
98c9496f6bSchristos
99*4724848cSchristos if (e == NULL)
100*4724848cSchristos return 1;
101*4724848cSchristos CRYPTO_THREAD_write_lock(global_engine_lock);
102c9496f6bSchristos to_return = engine_unlocked_finish(e, 1);
103*4724848cSchristos CRYPTO_THREAD_unlock(global_engine_lock);
104c9496f6bSchristos if (!to_return) {
105c9496f6bSchristos ENGINEerr(ENGINE_F_ENGINE_FINISH, ENGINE_R_FINISH_FAILED);
106c9496f6bSchristos return 0;
107c9496f6bSchristos }
108c9496f6bSchristos return to_return;
109c9496f6bSchristos }
110