Coverage Report

Created: 2018-08-29 13:53

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