Fix up path generation to use OPENSSL_MODULES
[openssl.git] / crypto / core_fetch.c
1 /*
2  * Copyright 2019-2021 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 ossl_method_construct_precondition(OSSL_PROVIDER *provider,
28                                               int operation_id, void *cbdata,
29                                               int *result)
30 {
31     if (!ossl_assert(result != NULL)) {
32         ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
33         return 0;
34     }
35
36     if (!ossl_provider_test_operation_bit(provider, operation_id, result))
37         return 0;
38
39     /*
40      * The result we get tells if methods have already been constructed.
41      * However, we want to tell whether construction should happen (true)
42      * or not (false), which is the opposite of what we got.
43      */
44     *result = !*result;
45
46     return 1;
47 }
48
49 static int ossl_method_construct_postcondition(OSSL_PROVIDER *provider,
50                                                int operation_id, int no_store,
51                                                void *cbdata, int *result)
52 {
53     if (!ossl_assert(result != NULL)) {
54         ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
55         return 0;
56     }
57
58     *result = 1;
59     return no_store != 0
60         || ossl_provider_set_operation_bit(provider, operation_id);
61 }
62
63 static void ossl_method_construct_this(OSSL_PROVIDER *provider,
64                                        const OSSL_ALGORITHM *algo,
65                                        int no_store, void *cbdata)
66 {
67     struct construct_data_st *data = cbdata;
68     void *method = NULL;
69
70     if ((method = data->mcm->construct(algo, provider, data->mcm_data))
71         == NULL)
72         return;
73
74     /*
75      * Note regarding putting the method in stores:
76      *
77      * we don't need to care if it actually got in or not here.
78      * If it didn't get in, it will simply not be available when
79      * ossl_method_construct() tries to get it from the store.
80      *
81      * It is *expected* that the put function increments the refcnt
82      * of the passed method.
83      */
84
85     if (data->force_store || !no_store) {
86         /* If we haven't been told not to store, add to the global store */
87         data->mcm->put(NULL, method, provider, algo->algorithm_names,
88                        algo->property_definition, data->mcm_data);
89     } else {
90         /*
91          * If we have been told not to store the method "permanently", we
92          * ask for a temporary store, and store the method there.
93          * The owner of |data->mcm| is completely responsible for managing
94          * that temporary store.
95          */
96         if ((data->store = data->mcm->get_tmp_store(data->mcm_data)) == NULL)
97             return;
98
99         data->mcm->put(data->store, method, provider, algo->algorithm_names,
100                        algo->property_definition, data->mcm_data);
101     }
102
103     /* refcnt-- because we're dropping the reference */
104     data->mcm->destruct(method, data->mcm_data);
105 }
106
107 void *ossl_method_construct(OSSL_LIB_CTX *libctx, int operation_id,
108                             OSSL_PROVIDER **provider_rw, int force_store,
109                             OSSL_METHOD_CONSTRUCT_METHOD *mcm, void *mcm_data)
110 {
111     void *method = NULL;
112
113     if ((method = mcm->get(NULL, (const OSSL_PROVIDER **)provider_rw,
114                            mcm_data)) == NULL) {
115         OSSL_PROVIDER *provider = provider_rw != NULL ? *provider_rw : NULL;
116         struct construct_data_st cbdata;
117
118         cbdata.store = NULL;
119         cbdata.force_store = force_store;
120         cbdata.mcm = mcm;
121         cbdata.mcm_data = mcm_data;
122         ossl_algorithm_do_all(libctx, operation_id, provider,
123                               ossl_method_construct_precondition,
124                               ossl_method_construct_this,
125                               ossl_method_construct_postcondition,
126                               &cbdata);
127
128         /* If there is a temporary store, try there first */
129         if (cbdata.store != NULL)
130             method = mcm->get(cbdata.store, (const OSSL_PROVIDER **)provider_rw,
131                               mcm_data);
132
133         /* If no method was found yet, try the global store */
134         if (method == NULL)
135             method = mcm->get(NULL, (const OSSL_PROVIDER **)provider_rw, mcm_data);
136     }
137
138     return method;
139 }