]>
Commit | Line | Data |
---|---|---|
c13d2ab4 RL |
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/asn1_int.h" | |
17 | #include "internal/property.h" | |
18 | #include "internal/core.h" | |
19 | #include "internal/evp_int.h" /* evp_locl.h needs it */ | |
20 | #include "evp_locl.h" | |
21 | ||
22 | /* The OpenSSL library context index for the default method store */ | |
23 | static int default_method_store_index = -1; | |
24 | ||
25 | static void default_method_store_free(void *vstore) | |
26 | { | |
27 | ossl_method_store_free(vstore); | |
28 | } | |
29 | ||
30 | static void *default_method_store_new(void) | |
31 | { | |
32 | return ossl_method_store_new(); | |
33 | } | |
34 | ||
35 | ||
36 | static const OPENSSL_CTX_METHOD default_method_store_method = { | |
37 | default_method_store_new, | |
38 | default_method_store_free, | |
39 | }; | |
40 | ||
41 | static int default_method_store_init(void) | |
42 | { | |
43 | default_method_store_index = | |
44 | openssl_ctx_new_index(&default_method_store_method); | |
45 | ||
46 | return default_method_store_index != -1; | |
47 | } | |
48 | ||
49 | static CRYPTO_ONCE default_method_store_init_flag = CRYPTO_ONCE_STATIC_INIT; | |
50 | DEFINE_RUN_ONCE_STATIC(do_default_method_store_init) | |
51 | { | |
52 | return OPENSSL_init_crypto(0, NULL) | |
53 | && default_method_store_init(); | |
54 | } | |
55 | ||
56 | /* Data to be passed through ossl_method_construct() */ | |
57 | struct method_data_st { | |
58 | const char *name; | |
59 | int nid; | |
60 | OSSL_METHOD_CONSTRUCT_METHOD *mcm; | |
61 | void *(*method_from_dispatch)(int nid, const OSSL_DISPATCH *, | |
62 | OSSL_PROVIDER *); | |
63 | int (*refcnt_up_method)(void *method); | |
64 | void (*destruct_method)(void *method); | |
65 | }; | |
66 | ||
67 | /* | |
68 | * Generic routines to fetch / create EVP methods with ossl_method_construct() | |
69 | */ | |
70 | static void *alloc_tmp_method_store(void) | |
71 | { | |
72 | return ossl_method_store_new(); | |
73 | } | |
74 | ||
75 | static void dealloc_tmp_method_store(void *store) | |
76 | { | |
77 | if (store != NULL) | |
78 | ossl_method_store_free(store); | |
79 | } | |
80 | ||
81 | static | |
82 | struct OSSL_METHOD_STORE *get_default_method_store(OPENSSL_CTX *libctx) | |
83 | { | |
84 | if (!RUN_ONCE(&default_method_store_init_flag, | |
85 | do_default_method_store_init)) | |
86 | return NULL; | |
87 | return openssl_ctx_get_data(libctx, default_method_store_index); | |
88 | } | |
89 | ||
90 | static void *get_method_from_store(OPENSSL_CTX *libctx, void *store, | |
91 | const char *propquery, void *data) | |
92 | { | |
93 | struct method_data_st *methdata = data; | |
94 | void *method = NULL; | |
95 | ||
96 | if (store == NULL | |
97 | && (store = get_default_method_store(libctx)) == NULL) | |
98 | return NULL; | |
99 | ||
100 | (void)ossl_method_store_fetch(store, methdata->nid, propquery, &method); | |
101 | ||
102 | if (method != NULL | |
103 | && !methdata->refcnt_up_method(method)) { | |
104 | method = NULL; | |
105 | } | |
106 | return method; | |
107 | } | |
108 | ||
109 | static int put_method_in_store(OPENSSL_CTX *libctx, void *store, | |
110 | const char *propdef, void *method, | |
111 | void *data) | |
112 | { | |
113 | struct method_data_st *methdata = data; | |
114 | ||
115 | if (store == NULL | |
116 | && (store = get_default_method_store(libctx)) == NULL) | |
117 | return 0; | |
118 | ||
119 | if (methdata->refcnt_up_method(method) | |
120 | && ossl_method_store_add(store, methdata->nid, propdef, method, | |
121 | methdata->destruct_method)) | |
122 | return 1; | |
123 | return 0; | |
124 | } | |
125 | ||
126 | static void *construct_method(const OSSL_DISPATCH *fns, OSSL_PROVIDER *prov, | |
127 | void *data) | |
128 | { | |
129 | struct method_data_st *methdata = data; | |
130 | void *method = NULL; | |
131 | ||
132 | if (methdata->nid == NID_undef) { | |
133 | /* Create a new NID for that name on the fly */ | |
134 | ASN1_OBJECT tmpobj; | |
135 | ||
136 | /* This is the same as OBJ_create() but without requiring a OID */ | |
137 | tmpobj.nid = OBJ_new_nid(1); | |
138 | tmpobj.sn = tmpobj.ln = methdata->name; | |
139 | tmpobj.flags = ASN1_OBJECT_FLAG_DYNAMIC; | |
140 | tmpobj.length = 0; | |
141 | tmpobj.data = NULL; | |
142 | ||
143 | methdata->nid = OBJ_add_object(&tmpobj); | |
144 | } | |
145 | ||
146 | if (methdata->nid == NID_undef) | |
147 | return NULL; | |
148 | ||
149 | method = methdata->method_from_dispatch(methdata->nid, fns, prov); | |
150 | if (method == NULL) | |
151 | return NULL; | |
152 | return method; | |
153 | } | |
154 | ||
155 | static void destruct_method(void *method, void *data) | |
156 | { | |
157 | struct method_data_st *methdata = data; | |
158 | ||
159 | methdata->destruct_method(method); | |
160 | } | |
161 | ||
162 | void *evp_generic_fetch(OPENSSL_CTX *libctx, int operation_id, | |
163 | const char *algorithm, const char *properties, | |
164 | void *(*new_method)(int nid, const OSSL_DISPATCH *fns, | |
165 | OSSL_PROVIDER *prov), | |
166 | int (*upref_method)(void *), | |
167 | void (*free_method)(void *)) | |
168 | { | |
169 | int nid = OBJ_sn2nid(algorithm); | |
170 | void *method = NULL; | |
171 | ||
172 | if (nid == NID_undef | |
173 | || !ossl_method_store_cache_get(NULL, nid, properties, &method)) { | |
174 | OSSL_METHOD_CONSTRUCT_METHOD mcm = { | |
175 | alloc_tmp_method_store, | |
176 | dealloc_tmp_method_store, | |
177 | get_method_from_store, | |
178 | put_method_in_store, | |
179 | construct_method, | |
180 | destruct_method | |
181 | }; | |
182 | struct method_data_st mcmdata; | |
183 | ||
184 | mcmdata.nid = nid; | |
185 | mcmdata.mcm = &mcm; | |
186 | mcmdata.method_from_dispatch = new_method; | |
187 | mcmdata.destruct_method = free_method; | |
188 | mcmdata.refcnt_up_method = upref_method; | |
189 | mcmdata.destruct_method = free_method; | |
190 | method = ossl_method_construct(libctx, operation_id, algorithm, | |
191 | properties, 0 /* !force_cache */, | |
192 | &mcm, &mcmdata); | |
193 | ossl_method_store_cache_set(NULL, nid, properties, method); | |
194 | } | |
195 | ||
196 | return method; | |
197 | } |