Coverage Report

Created: 2025-06-13 06:58

/src/openssl30/crypto/core_fetch.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2019-2025 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
#include <stddef.h>
11
12
#include <openssl/core.h>
13
#include "internal/cryptlib.h"
14
#include "internal/core.h"
15
#include "internal/property.h"
16
#include "internal/provider.h"
17
18
struct construct_data_st {
19
    OSSL_LIB_CTX *libctx;
20
    OSSL_METHOD_STORE *store;
21
    int operation_id;
22
    int force_store;
23
    OSSL_METHOD_CONSTRUCT_METHOD *mcm;
24
    void *mcm_data;
25
};
26
27
static int is_temporary_method_store(int no_store, void *cbdata)
28
10.5M
{
29
10.5M
    struct construct_data_st *data = cbdata;
30
31
10.5M
    return no_store && !data->force_store;
32
10.5M
}
33
34
static int ossl_method_construct_reserve_store(int no_store, void *cbdata)
35
5.28M
{
36
5.28M
    struct construct_data_st *data = cbdata;
37
38
5.28M
    if (is_temporary_method_store(no_store, data) && data->store == NULL) {
39
        /*
40
         * If we have been told not to store the method "permanently", we
41
         * ask for a temporary store, and store the method there.
42
         * The owner of |data->mcm| is completely responsible for managing
43
         * that temporary store.
44
         */
45
0
        if ((data->store = data->mcm->get_tmp_store(data->mcm_data)) == NULL)
46
0
            return 0;
47
0
    }
48
49
5.28M
    return data->mcm->lock_store(data->store, data->mcm_data);
50
5.28M
}
51
52
static int ossl_method_construct_unreserve_store(void *cbdata)
53
5.28M
{
54
5.28M
    struct construct_data_st *data = cbdata;
55
56
5.28M
    return data->mcm->unlock_store(data->store, data->mcm_data);
57
5.28M
}
58
59
static int ossl_method_construct_precondition(OSSL_PROVIDER *provider,
60
                                              int operation_id, int no_store,
61
                                              void *cbdata, int *result)
62
5.28M
{
63
5.28M
    if (!ossl_assert(result != NULL)) {
64
0
        ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
65
0
        return 0;
66
0
    }
67
68
    /* Assume that no bits are set */
69
5.28M
    *result = 0;
70
71
    /* No flag bits for temporary stores */
72
5.28M
    if (!is_temporary_method_store(no_store, cbdata)
73
5.28M
        && !ossl_provider_test_operation_bit(provider, operation_id, result))
74
0
        return 0;
75
76
    /*
77
     * The result we get tells if methods have already been constructed.
78
     * However, we want to tell whether construction should happen (true)
79
     * or not (false), which is the opposite of what we got.
80
     */
81
5.28M
    *result = !*result;
82
83
5.28M
    return 1;
84
5.28M
}
85
86
static int ossl_method_construct_postcondition(OSSL_PROVIDER *provider,
87
                                               int operation_id, int no_store,
88
                                               void *cbdata, int *result)
89
482
{
90
482
    if (!ossl_assert(result != NULL)) {
91
0
        ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
92
0
        return 0;
93
0
    }
94
95
482
    *result = 1;
96
97
    /* No flag bits for temporary stores */
98
482
    return is_temporary_method_store(no_store, cbdata)
99
482
        || ossl_provider_set_operation_bit(provider, operation_id);
100
482
}
101
102
static void ossl_method_construct_this(OSSL_PROVIDER *provider,
103
                                       const OSSL_ALGORITHM *algo,
104
                                       int no_store, void *cbdata)
105
10.1k
{
106
10.1k
    struct construct_data_st *data = cbdata;
107
10.1k
    void *method = NULL;
108
109
10.1k
    if ((method = data->mcm->construct(algo, provider, data->mcm_data))
110
10.1k
        == NULL)
111
0
        return;
112
113
    /*
114
     * Note regarding putting the method in stores:
115
     *
116
     * we don't need to care if it actually got in or not here.
117
     * If it didn't get in, it will simply not be available when
118
     * ossl_method_construct() tries to get it from the store.
119
     *
120
     * It is *expected* that the put function increments the refcnt
121
     * of the passed method.
122
     */
123
10.1k
    data->mcm->put(no_store ? data->store : NULL,
124
10.1k
                   method, provider, algo->algorithm_names,
125
10.1k
                   algo->property_definition, data->mcm_data);
126
127
    /* refcnt-- because we're dropping the reference */
128
10.1k
    data->mcm->destruct(method, data->mcm_data);
129
10.1k
}
130
131
void *ossl_method_construct(OSSL_LIB_CTX *libctx, int operation_id,
132
                            OSSL_PROVIDER **provider_rw, int force_store,
133
                            OSSL_METHOD_CONSTRUCT_METHOD *mcm, void *mcm_data)
134
2.83M
{
135
2.83M
    void *method = NULL;
136
2.83M
    OSSL_PROVIDER *provider = provider_rw != NULL ? *provider_rw : NULL;
137
2.83M
    struct construct_data_st cbdata;
138
139
    /*
140
     * We might be tempted to try to look into the method store without
141
     * constructing to see if we can find our method there already.
142
     * Unfortunately that does not work well if the query contains
143
     * optional properties as newly loaded providers can match them better.
144
     * We trust that ossl_method_construct_precondition() and
145
     * ossl_method_construct_postcondition() make sure that the
146
     * ossl_algorithm_do_all() does very little when methods from
147
     * a provider have already been constructed.
148
     */
149
150
2.83M
    cbdata.store = NULL;
151
2.83M
    cbdata.force_store = force_store;
152
2.83M
    cbdata.mcm = mcm;
153
2.83M
    cbdata.mcm_data = mcm_data;
154
2.83M
    ossl_algorithm_do_all(libctx, operation_id, provider,
155
2.83M
                          ossl_method_construct_precondition,
156
2.83M
                          ossl_method_construct_reserve_store,
157
2.83M
                          ossl_method_construct_this,
158
2.83M
                          ossl_method_construct_unreserve_store,
159
2.83M
                          ossl_method_construct_postcondition,
160
2.83M
                          &cbdata);
161
162
    /* If there is a temporary store, try there first */
163
2.83M
    if (cbdata.store != NULL)
164
0
        method = mcm->get(cbdata.store, (const OSSL_PROVIDER **)provider_rw,
165
0
                          mcm_data);
166
167
    /* If no method was found yet, try the global store */
168
2.83M
    if (method == NULL)
169
2.83M
        method = mcm->get(NULL, (const OSSL_PROVIDER **)provider_rw, mcm_data);
170
171
2.83M
    return method;
172
2.83M
}