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