xref: /openssl/crypto/engine/eng_init.c (revision da1c088f)
1 /*
2  * Copyright 2001-2023 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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 /* We need to use some engine deprecated APIs */
11 #define OPENSSL_SUPPRESS_DEPRECATED
12 
13 #include "internal/e_os.h"
14 #include "eng_local.h"
15 
16 /*
17  * Initialise an engine type for use (or up its functional reference count if
18  * it's already in use). This version is only used internally.
19  */
engine_unlocked_init(ENGINE * e)20 int engine_unlocked_init(ENGINE *e)
21 {
22     int to_return = 1;
23 
24     if ((e->funct_ref == 0) && e->init)
25         /*
26          * This is the first functional reference and the engine requires
27          * initialisation so we do it now.
28          */
29         to_return = e->init(e);
30     if (to_return) {
31         int ref;
32 
33         /*
34          * OK, we return a functional reference which is also a structural
35          * reference.
36          */
37         if (!CRYPTO_UP_REF(&e->struct_ref, &ref)) {
38             e->finish(e);
39             return 0;
40         }
41         e->funct_ref++;
42         ENGINE_REF_PRINT(e, 0, 1);
43         ENGINE_REF_PRINT(e, 1, 1);
44     }
45     return to_return;
46 }
47 
48 /*
49  * Free a functional reference to an engine type. This version is only used
50  * internally.
51  */
engine_unlocked_finish(ENGINE * e,int unlock_for_handlers)52 int engine_unlocked_finish(ENGINE *e, int unlock_for_handlers)
53 {
54     int to_return = 1;
55 
56     /*
57      * Reduce the functional reference count here so if it's the terminating
58      * case, we can release the lock safely and call the finish() handler
59      * without risk of a race. We get a race if we leave the count until
60      * after and something else is calling "finish" at the same time -
61      * there's a chance that both threads will together take the count from 2
62      * to 0 without either calling finish().
63      */
64     e->funct_ref--;
65     ENGINE_REF_PRINT(e, 1, -1);
66     if ((e->funct_ref == 0) && e->finish) {
67         if (unlock_for_handlers)
68             CRYPTO_THREAD_unlock(global_engine_lock);
69         to_return = e->finish(e);
70         if (unlock_for_handlers)
71             if (!CRYPTO_THREAD_write_lock(global_engine_lock))
72                 return 0;
73         if (!to_return)
74             return 0;
75     }
76     REF_ASSERT_ISNT(e->funct_ref < 0);
77     /* Release the structural reference too */
78     if (!engine_free_util(e, 0)) {
79         ERR_raise(ERR_LIB_ENGINE, ENGINE_R_FINISH_FAILED);
80         return 0;
81     }
82     return to_return;
83 }
84 
85 /* The API (locked) version of "init" */
ENGINE_init(ENGINE * e)86 int ENGINE_init(ENGINE *e)
87 {
88     int ret;
89     if (e == NULL) {
90         ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
91         return 0;
92     }
93     if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) {
94         /* Maybe this should be raised in do_engine_lock_init() */
95         ERR_raise(ERR_LIB_ENGINE, ERR_R_CRYPTO_LIB);
96         return 0;
97     }
98     if (!CRYPTO_THREAD_write_lock(global_engine_lock))
99         return 0;
100     ret = engine_unlocked_init(e);
101     CRYPTO_THREAD_unlock(global_engine_lock);
102     return ret;
103 }
104 
105 /* The API (locked) version of "finish" */
ENGINE_finish(ENGINE * e)106 int ENGINE_finish(ENGINE *e)
107 {
108     int to_return = 1;
109 
110     if (e == NULL)
111         return 1;
112     if (!CRYPTO_THREAD_write_lock(global_engine_lock))
113         return 0;
114     to_return = engine_unlocked_finish(e, 1);
115     CRYPTO_THREAD_unlock(global_engine_lock);
116     if (!to_return) {
117         ERR_raise(ERR_LIB_ENGINE, ENGINE_R_FINISH_FAILED);
118         return 0;
119     }
120     return to_return;
121 }
122