EVP_FETCH: deal with names without pre-defined NIDs
[openssl.git] / crypto / evp / evp_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 #include <openssl/ossl_typ.h>
12 #include <openssl/evp.h>
13 #include <openssl/core.h>
14 #include "internal/cryptlib.h"
15 #include "internal/thread_once.h"
16 #include "internal/property.h"
17 #include "internal/core.h"
18 #include "internal/namemap.h"
19 #include "internal/evp_int.h"    /* evp_locl.h needs it */
20 #include "evp_locl.h"
21
22 static void default_method_store_free(void *vstore)
23 {
24     ossl_method_store_free(vstore);
25 }
26
27 static void *default_method_store_new(OPENSSL_CTX *ctx)
28 {
29     return ossl_method_store_new(ctx);
30 }
31
32
33 static const OPENSSL_CTX_METHOD default_method_store_method = {
34     default_method_store_new,
35     default_method_store_free,
36 };
37
38 /* Data to be passed through ossl_method_construct() */
39 struct method_data_st {
40     OPENSSL_CTX *libctx;
41     const char *name;
42     int nid;
43     OSSL_METHOD_CONSTRUCT_METHOD *mcm;
44     void *(*method_from_dispatch)(int nid, const OSSL_DISPATCH *,
45                                   OSSL_PROVIDER *);
46     int (*refcnt_up_method)(void *method);
47     void (*destruct_method)(void *method);
48     int (*nid_method)(void *method);
49 };
50
51 /*
52  * Generic routines to fetch / create EVP methods with ossl_method_construct()
53  */
54 static void *alloc_tmp_method_store(OPENSSL_CTX *ctx)
55 {
56     return ossl_method_store_new(ctx);
57 }
58
59  static void dealloc_tmp_method_store(void *store)
60 {
61     if (store != NULL)
62         ossl_method_store_free(store);
63 }
64
65 static OSSL_METHOD_STORE *get_default_method_store(OPENSSL_CTX *libctx)
66 {
67     return openssl_ctx_get_data(libctx, OPENSSL_CTX_DEFAULT_METHOD_STORE_INDEX,
68                                 &default_method_store_method);
69 }
70
71 static void *get_method_from_store(OPENSSL_CTX *libctx, void *store,
72                                    const char *name, const char *propquery,
73                                    void *data)
74 {
75     struct method_data_st *methdata = data;
76     void *method = NULL;
77     OSSL_NAMEMAP *namemap;
78     int id;
79
80     if (store == NULL
81         && (store = get_default_method_store(libctx)) == NULL)
82         return NULL;
83
84     if ((namemap = ossl_namemap_stored(libctx)) == NULL
85         || (id = ossl_namemap_add(namemap, name)) == 0)
86         return NULL;
87
88     (void)ossl_method_store_fetch(store, id, propquery, &method);
89
90     if (method != NULL
91         && !methdata->refcnt_up_method(method)) {
92         method = NULL;
93     }
94     return method;
95 }
96
97 static int put_method_in_store(OPENSSL_CTX *libctx, void *store,
98                                void *method, const char *name,
99                                const char *propdef, void *data)
100 {
101     struct method_data_st *methdata = data;
102     OSSL_NAMEMAP *namemap;
103     int id;
104
105     if ((namemap = ossl_namemap_stored(methdata->libctx)) == NULL
106         || (id = ossl_namemap_add(namemap, name)) == 0)
107         return 0;
108
109     if (store == NULL
110         && (store = get_default_method_store(libctx)) == NULL)
111         return 0;
112
113     if (methdata->refcnt_up_method(method)
114         && ossl_method_store_add(store, id, propdef, method,
115                                  methdata->destruct_method))
116         return 1;
117     return 0;
118 }
119
120 static void *construct_method(const char *name, const OSSL_DISPATCH *fns,
121                               OSSL_PROVIDER *prov, void *data)
122 {
123     struct method_data_st *methdata = data;
124     /* TODO(3.0) get rid of the need for legacy NIDs */
125     int legacy_nid = OBJ_sn2nid(name);
126
127     return methdata->method_from_dispatch(legacy_nid, fns, prov);
128 }
129
130 static void destruct_method(void *method, void *data)
131 {
132     struct method_data_st *methdata = data;
133
134     methdata->destruct_method(method);
135 }
136
137 void *evp_generic_fetch(OPENSSL_CTX *libctx, int operation_id,
138                         const char *name, const char *properties,
139                         void *(*new_method)(int nid, const OSSL_DISPATCH *fns,
140                                             OSSL_PROVIDER *prov),
141                         int (*upref_method)(void *),
142                         void (*free_method)(void *),
143                         int (*nid_method)(void *))
144 {
145     OSSL_METHOD_STORE *store = get_default_method_store(libctx);
146     OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
147     int id;
148     void *method = NULL;
149
150     if (store == NULL || namemap == NULL)
151         return NULL;
152
153     if ((id = ossl_namemap_number(namemap, name)) == 0
154         || !ossl_method_store_cache_get(store, id, properties, &method)) {
155         OSSL_METHOD_CONSTRUCT_METHOD mcm = {
156             alloc_tmp_method_store,
157             dealloc_tmp_method_store,
158             get_method_from_store,
159             put_method_in_store,
160             construct_method,
161             destruct_method
162         };
163         struct method_data_st mcmdata;
164
165         mcmdata.mcm = &mcm;
166         mcmdata.libctx = libctx;
167         mcmdata.method_from_dispatch = new_method;
168         mcmdata.destruct_method = free_method;
169         mcmdata.refcnt_up_method = upref_method;
170         mcmdata.destruct_method = free_method;
171         mcmdata.nid_method = nid_method;
172         method = ossl_method_construct(libctx, operation_id, name,
173                                        properties, 0 /* !force_cache */,
174                                        &mcm, &mcmdata);
175         ossl_method_store_cache_set(store, id, properties, method);
176     } else {
177         upref_method(method);
178     }
179
180     return method;
181 }
182
183 int EVP_set_default_properties(OPENSSL_CTX *libctx, const char *propq)
184 {
185     OSSL_METHOD_STORE *store = get_default_method_store(libctx);
186
187     if (store != NULL)
188         return ossl_method_store_set_global_properties(store, propq);
189     EVPerr(EVP_F_EVP_SET_DEFAULT_PROPERTIES, ERR_R_INTERNAL_ERROR);
190     return 0;
191 }