Coverage Report

Created: 2025-06-13 06:58

/src/openssl31/crypto/engine/eng_init.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2001-2022 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
 */
20
int engine_unlocked_init(ENGINE *e)
21
0
{
22
0
    int to_return = 1;
23
24
0
    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
0
        to_return = e->init(e);
30
0
    if (to_return) {
31
        /*
32
         * OK, we return a functional reference which is also a structural
33
         * reference.
34
         */
35
0
        e->struct_ref++;
36
0
        e->funct_ref++;
37
0
        ENGINE_REF_PRINT(e, 0, 1);
38
0
        ENGINE_REF_PRINT(e, 1, 1);
39
0
    }
40
0
    return to_return;
41
0
}
42
43
/*
44
 * Free a functional reference to an engine type. This version is only used
45
 * internally.
46
 */
47
int engine_unlocked_finish(ENGINE *e, int unlock_for_handlers)
48
0
{
49
0
    int to_return = 1;
50
51
    /*
52
     * Reduce the functional reference count here so if it's the terminating
53
     * case, we can release the lock safely and call the finish() handler
54
     * without risk of a race. We get a race if we leave the count until
55
     * after and something else is calling "finish" at the same time -
56
     * there's a chance that both threads will together take the count from 2
57
     * to 0 without either calling finish().
58
     */
59
0
    e->funct_ref--;
60
0
    ENGINE_REF_PRINT(e, 1, -1);
61
0
    if ((e->funct_ref == 0) && e->finish) {
62
0
        if (unlock_for_handlers)
63
0
            CRYPTO_THREAD_unlock(global_engine_lock);
64
0
        to_return = e->finish(e);
65
0
        if (unlock_for_handlers)
66
0
            if (!CRYPTO_THREAD_write_lock(global_engine_lock))
67
0
                return 0;
68
0
        if (!to_return)
69
0
            return 0;
70
0
    }
71
0
    REF_ASSERT_ISNT(e->funct_ref < 0);
72
    /* Release the structural reference too */
73
0
    if (!engine_free_util(e, 0)) {
74
0
        ERR_raise(ERR_LIB_ENGINE, ENGINE_R_FINISH_FAILED);
75
0
        return 0;
76
0
    }
77
0
    return to_return;
78
0
}
79
80
/* The API (locked) version of "init" */
81
int ENGINE_init(ENGINE *e)
82
0
{
83
0
    int ret;
84
0
    if (e == NULL) {
85
0
        ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
86
0
        return 0;
87
0
    }
88
0
    if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) {
89
0
        ERR_raise(ERR_LIB_ENGINE, ERR_R_MALLOC_FAILURE);
90
0
        return 0;
91
0
    }
92
0
    if (!CRYPTO_THREAD_write_lock(global_engine_lock))
93
0
        return 0;
94
0
    ret = engine_unlocked_init(e);
95
0
    CRYPTO_THREAD_unlock(global_engine_lock);
96
0
    return ret;
97
0
}
98
99
/* The API (locked) version of "finish" */
100
int ENGINE_finish(ENGINE *e)
101
442M
{
102
442M
    int to_return = 1;
103
104
442M
    if (e == NULL)
105
442M
        return 1;
106
0
    if (!CRYPTO_THREAD_write_lock(global_engine_lock))
107
0
        return 0;
108
0
    to_return = engine_unlocked_finish(e, 1);
109
0
    CRYPTO_THREAD_unlock(global_engine_lock);
110
0
    if (!to_return) {
111
0
        ERR_raise(ERR_LIB_ENGINE, ENGINE_R_FINISH_FAILED);
112
0
        return 0;
113
0
    }
114
0
    return to_return;
115
0
}