]>
Commit | Line | Data |
---|---|---|
9e11fe0d | 1 | /* |
4333b89f | 2 | * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved. |
9e11fe0d RL |
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 { | |
b4250010 | 19 | OSSL_LIB_CTX *libctx; |
9e11fe0d RL |
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 | ||
5a29b628 RL |
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 | ||
84d167f6 RL |
63 | static void ossl_method_construct_this(OSSL_PROVIDER *provider, |
64 | const OSSL_ALGORITHM *algo, | |
65 | int no_store, void *cbdata) | |
9e11fe0d RL |
66 | { |
67 | struct construct_data_st *data = cbdata; | |
84d167f6 | 68 | void *method = NULL; |
9e11fe0d | 69 | |
36fa4d8a RL |
70 | if ((method = data->mcm->construct(algo, provider, data->mcm_data)) |
71 | == NULL) | |
84d167f6 RL |
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) { | |
9067cf6c | 86 | /* If we haven't been told not to store, add to the global store */ |
6882652e | 87 | data->mcm->put(NULL, method, provider, algo->algorithm_names, |
9067cf6c RL |
88 | algo->property_definition, data->mcm_data); |
89 | } else { | |
a3830831 | 90 | /* |
9067cf6c RL |
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. | |
a3830831 | 95 | */ |
9067cf6c RL |
96 | if ((data->store = data->mcm->get_tmp_store(data->mcm_data)) == NULL) |
97 | return; | |
98 | ||
6882652e | 99 | data->mcm->put(data->store, method, provider, algo->algorithm_names, |
84d167f6 | 100 | algo->property_definition, data->mcm_data); |
9e11fe0d RL |
101 | } |
102 | ||
84d167f6 RL |
103 | /* refcnt-- because we're dropping the reference */ |
104 | data->mcm->destruct(method, data->mcm_data); | |
9e11fe0d RL |
105 | } |
106 | ||
b4250010 | 107 | void *ossl_method_construct(OSSL_LIB_CTX *libctx, int operation_id, |
cfce50f7 | 108 | OSSL_PROVIDER *provider, int force_store, |
9e11fe0d RL |
109 | OSSL_METHOD_CONSTRUCT_METHOD *mcm, void *mcm_data) |
110 | { | |
111 | void *method = NULL; | |
112 | ||
6882652e | 113 | if ((method = mcm->get(NULL, mcm_data)) == NULL) { |
9e11fe0d RL |
114 | struct construct_data_st cbdata; |
115 | ||
9067cf6c | 116 | cbdata.store = NULL; |
9e11fe0d RL |
117 | cbdata.force_store = force_store; |
118 | cbdata.mcm = mcm; | |
119 | cbdata.mcm_data = mcm_data; | |
cfce50f7 | 120 | ossl_algorithm_do_all(libctx, operation_id, provider, |
5a29b628 RL |
121 | ossl_method_construct_precondition, |
122 | ossl_method_construct_this, | |
123 | ossl_method_construct_postcondition, | |
124 | &cbdata); | |
9e11fe0d | 125 | |
9067cf6c RL |
126 | /* If there is a temporary store, try there first */ |
127 | if (cbdata.store != NULL) | |
6882652e | 128 | method = mcm->get(cbdata.store, mcm_data); |
9067cf6c RL |
129 | |
130 | /* If no method was found yet, try the global store */ | |
131 | if (method == NULL) | |
6882652e | 132 | method = mcm->get(NULL, mcm_data); |
9e11fe0d RL |
133 | } |
134 | ||
9e11fe0d RL |
135 | return method; |
136 | } |