Replumbing: better reference counter control in ossl_method_construct()
[openssl.git] / crypto / core_fetch.c
1 /*
2  * Copyright 2019 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_this(OSSL_PROVIDER *provider, void *cbdata)
28 {
29     struct construct_data_st *data = cbdata;
30     int no_store = 0;    /* Assume caching is ok */
31     const OSSL_ALGORITHM *map =
32         ossl_provider_query_operation(provider, data->operation_id, &no_store);
33
34     while (map->algorithm_name != NULL) {
35         const OSSL_ALGORITHM *thismap = map++;
36         void *method = NULL;
37
38         if ((method = data->mcm->construct(thismap->implementation, provider,
39                                             data->mcm_data)) == NULL)
40             continue;
41
42         /*
43          * Note regarding putting the method in stores:
44          *
45          * we don't need to care if it actually got in or not here.
46          * If it didn't get in, it will simply not be available when
47          * ossl_method_construct() tries to get it from the store.
48          *
49          * It is *expected* that the put function increments the refcnt
50          * of the passed method.
51          */
52
53         if (data->force_store || !no_store) {
54             /*
55              * If we haven't been told not to store,
56              * add to the global store
57              */
58             data->mcm->put(data->libctx, NULL,
59                            thismap->property_definition,
60                            method, data->mcm_data);
61         }
62
63         data->mcm->put(data->libctx, data->store,
64                        thismap->property_definition,
65                        method, data->mcm_data);
66
67         /* refcnt-- because we're dropping the reference */
68         data->mcm->destruct(method, data->mcm_data);
69     }
70
71     return 1;
72 }
73
74 void *ossl_method_construct(OPENSSL_CTX *libctx, int operation_id,
75                             const char *name, const char *propquery,
76                             int force_store,
77                             OSSL_METHOD_CONSTRUCT_METHOD *mcm, void *mcm_data)
78 {
79     void *method = NULL;
80
81     if ((method = mcm->get(libctx, NULL, propquery, mcm_data)) == NULL) {
82         struct construct_data_st cbdata;
83
84         /*
85          * We have a temporary store to be able to easily search among new
86          * items, or items that should find themselves in the global store.
87          */
88         if ((cbdata.store = mcm->alloc_tmp_store()) == NULL)
89             goto fin;
90
91         cbdata.libctx = libctx;
92         cbdata.operation_id = operation_id;
93         cbdata.force_store = force_store;
94         cbdata.mcm = mcm;
95         cbdata.mcm_data = mcm_data;
96         ossl_provider_forall_loaded(libctx, ossl_method_construct_this,
97                                     &cbdata);
98
99         method = mcm->get(libctx, cbdata.store, propquery, mcm_data);
100         mcm->dealloc_tmp_store(cbdata.store);
101     }
102
103  fin:
104     return method;
105 }