Update copyright year
[openssl.git] / crypto / core_fetch.c
1 /*
2  * Copyright 2019-2020 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     OPENSSL_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         /*
87          * If we haven't been told not to store,
88          * add to the global store
89          */
90         data->mcm->put(data->libctx, NULL, method, provider,
91                        data->operation_id, algo->algorithm_names,
92                        algo->property_definition, data->mcm_data);
93     }
94
95     data->mcm->put(data->libctx, data->store, method, provider,
96                    data->operation_id, algo->algorithm_names,
97                    algo->property_definition, data->mcm_data);
98
99     /* refcnt-- because we're dropping the reference */
100     data->mcm->destruct(method, data->mcm_data);
101 }
102
103 void *ossl_method_construct(OPENSSL_CTX *libctx, int operation_id,
104                             int force_store,
105                             OSSL_METHOD_CONSTRUCT_METHOD *mcm, void *mcm_data)
106 {
107     void *method = NULL;
108
109     if ((method = mcm->get(libctx, NULL, mcm_data)) == NULL) {
110         struct construct_data_st cbdata;
111
112         /*
113          * We have a temporary store to be able to easily search among new
114          * items, or items that should find themselves in the global store.
115          */
116         if ((cbdata.store = mcm->alloc_tmp_store(libctx)) == NULL)
117             goto fin;
118
119         cbdata.libctx = libctx;
120         cbdata.operation_id = operation_id;
121         cbdata.force_store = force_store;
122         cbdata.mcm = mcm;
123         cbdata.mcm_data = mcm_data;
124         ossl_algorithm_do_all(libctx, operation_id, NULL,
125                               ossl_method_construct_precondition,
126                               ossl_method_construct_this,
127                               ossl_method_construct_postcondition,
128                               &cbdata);
129
130         method = mcm->get(libctx, cbdata.store, mcm_data);
131         mcm->dealloc_tmp_store(cbdata.store);
132     }
133
134  fin:
135     return method;
136 }