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