2016-05-18 02:52:22 +08:00
|
|
|
/*
|
2023-09-07 16:59:15 +08:00
|
|
|
* Copyright 2001-2023 The OpenSSL Project Authors. All Rights Reserved.
|
2001-09-26 04:00:51 +08:00
|
|
|
*
|
2018-12-06 20:39:00 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-18 02:52:22 +08:00
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
2001-09-26 04:00:51 +08:00
|
|
|
*/
|
|
|
|
|
2020-07-14 07:40:29 +08:00
|
|
|
/* We need to use some engine deprecated APIs */
|
|
|
|
#define OPENSSL_SUPPRESS_DEPRECATED
|
|
|
|
|
2022-02-04 22:13:01 +08:00
|
|
|
#include "internal/e_os.h"
|
2019-09-28 06:45:40 +08:00
|
|
|
#include "eng_local.h"
|
2001-09-26 04:00:51 +08:00
|
|
|
|
|
|
|
/*
|
2022-08-24 02:33:58 +08:00
|
|
|
* Initialise an engine type for use (or up its functional reference count if
|
2001-09-26 04:00:51 +08:00
|
|
|
* it's already in use). This version is only used internally.
|
|
|
|
*/
|
|
|
|
int engine_unlocked_init(ENGINE *e)
|
|
|
|
{
|
|
|
|
int to_return = 1;
|
|
|
|
|
|
|
|
if ((e->funct_ref == 0) && e->init)
|
|
|
|
/*
|
|
|
|
* This is the first functional reference and the engine requires
|
|
|
|
* initialisation so we do it now.
|
|
|
|
*/
|
|
|
|
to_return = e->init(e);
|
|
|
|
if (to_return) {
|
2023-05-11 21:14:31 +08:00
|
|
|
int ref;
|
|
|
|
|
2001-09-26 04:00:51 +08:00
|
|
|
/*
|
|
|
|
* OK, we return a functional reference which is also a structural
|
|
|
|
* reference.
|
|
|
|
*/
|
2023-06-22 07:24:27 +08:00
|
|
|
if (!CRYPTO_UP_REF(&e->struct_ref, &ref)) {
|
2023-05-11 21:14:31 +08:00
|
|
|
e->finish(e);
|
|
|
|
return 0;
|
|
|
|
}
|
2001-09-26 04:00:51 +08:00
|
|
|
e->funct_ref++;
|
2021-05-19 23:09:49 +08:00
|
|
|
ENGINE_REF_PRINT(e, 0, 1);
|
|
|
|
ENGINE_REF_PRINT(e, 1, 1);
|
2001-09-26 04:00:51 +08:00
|
|
|
}
|
|
|
|
return to_return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2022-08-24 02:33:58 +08:00
|
|
|
* Free a functional reference to an engine type. This version is only used
|
2001-09-26 04:00:51 +08:00
|
|
|
* internally.
|
|
|
|
*/
|
|
|
|
int engine_unlocked_finish(ENGINE *e, int unlock_for_handlers)
|
|
|
|
{
|
|
|
|
int to_return = 1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Reduce the functional reference count here so if it's the terminating
|
|
|
|
* case, we can release the lock safely and call the finish() handler
|
|
|
|
* without risk of a race. We get a race if we leave the count until
|
|
|
|
* after and something else is calling "finish" at the same time -
|
|
|
|
* there's a chance that both threads will together take the count from 2
|
|
|
|
* to 0 without either calling finish().
|
|
|
|
*/
|
|
|
|
e->funct_ref--;
|
2021-05-19 23:09:49 +08:00
|
|
|
ENGINE_REF_PRINT(e, 1, -1);
|
2001-09-26 04:00:51 +08:00
|
|
|
if ((e->funct_ref == 0) && e->finish) {
|
|
|
|
if (unlock_for_handlers)
|
2016-03-09 00:44:34 +08:00
|
|
|
CRYPTO_THREAD_unlock(global_engine_lock);
|
2001-09-26 04:00:51 +08:00
|
|
|
to_return = e->finish(e);
|
|
|
|
if (unlock_for_handlers)
|
2021-02-19 04:31:56 +08:00
|
|
|
if (!CRYPTO_THREAD_write_lock(global_engine_lock))
|
|
|
|
return 0;
|
2001-09-26 04:00:51 +08:00
|
|
|
if (!to_return)
|
|
|
|
return 0;
|
|
|
|
}
|
2016-01-31 01:04:25 +08:00
|
|
|
REF_ASSERT_ISNT(e->funct_ref < 0);
|
2001-09-26 04:00:51 +08:00
|
|
|
/* Release the structural reference too */
|
|
|
|
if (!engine_free_util(e, 0)) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_ENGINE, ENGINE_R_FINISH_FAILED);
|
2001-09-26 04:00:51 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return to_return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The API (locked) version of "init" */
|
|
|
|
int ENGINE_init(ENGINE *e)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
if (e == NULL) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
|
2001-09-26 04:00:51 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2016-07-20 01:42:11 +08:00
|
|
|
if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) {
|
2022-09-29 19:57:34 +08:00
|
|
|
/* Maybe this should be raised in do_engine_lock_init() */
|
|
|
|
ERR_raise(ERR_LIB_ENGINE, ERR_R_CRYPTO_LIB);
|
2016-07-20 01:42:11 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2021-02-19 04:31:56 +08:00
|
|
|
if (!CRYPTO_THREAD_write_lock(global_engine_lock))
|
|
|
|
return 0;
|
2001-09-26 04:00:51 +08:00
|
|
|
ret = engine_unlocked_init(e);
|
2016-03-09 00:44:34 +08:00
|
|
|
CRYPTO_THREAD_unlock(global_engine_lock);
|
2001-09-26 04:00:51 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The API (locked) version of "finish" */
|
|
|
|
int ENGINE_finish(ENGINE *e)
|
|
|
|
{
|
|
|
|
int to_return = 1;
|
|
|
|
|
2016-02-26 01:09:06 +08:00
|
|
|
if (e == NULL)
|
|
|
|
return 1;
|
2021-02-19 04:31:56 +08:00
|
|
|
if (!CRYPTO_THREAD_write_lock(global_engine_lock))
|
|
|
|
return 0;
|
2001-09-26 04:00:51 +08:00
|
|
|
to_return = engine_unlocked_finish(e, 1);
|
2016-03-09 00:44:34 +08:00
|
|
|
CRYPTO_THREAD_unlock(global_engine_lock);
|
2001-09-26 04:00:51 +08:00
|
|
|
if (!to_return) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_ENGINE, ENGINE_R_FINISH_FAILED);
|
2001-09-26 04:00:51 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return to_return;
|
|
|
|
}
|