2 * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include <openssl/core.h>
11 #include <openssl/core_dispatch.h>
12 #include "internal/core.h"
13 #include "internal/property.h"
14 #include "internal/provider.h"
16 struct algorithm_data_st {
18 int operation_id; /* May be zero for finding them all */
19 int (*pre)(OSSL_PROVIDER *, int operation_id, int no_store, void *data,
21 void (*fn)(OSSL_PROVIDER *, const OSSL_ALGORITHM *, int no_store,
23 int (*post)(OSSL_PROVIDER *, int operation_id, int no_store, void *data,
29 * Process one OSSL_ALGORITHM array, for the operation |cur_operation|,
30 * by constructing methods for all its implementations and adding those
31 * to the appropriate method store.
32 * Which method store is appropriate is given by |no_store| ("permanent"
33 * if 0, temporary if 1) and other data in |data->data|.
36 * -1 to quit adding algorithm implementations immediately
37 * 0 if not successful, but adding should continue
38 * 1 if successful so far, and adding should continue
40 static int algorithm_do_map(OSSL_PROVIDER *provider, const OSSL_ALGORITHM *map,
41 int cur_operation, int no_store, void *cbdata)
43 struct algorithm_data_st *data = cbdata;
46 /* Do we fulfill pre-conditions? */
47 if (data->pre == NULL) {
48 /* If there is no pre-condition function, assume "yes" */
50 } else if (!data->pre(provider, cur_operation, no_store, data->data,
52 /* Error, bail out! */
57 * If pre-condition not fulfilled don't add this set of implementations,
58 * but do continue with the next. This simply means that another thread
65 const OSSL_ALGORITHM *thismap;
67 for (thismap = map; thismap->algorithm_names != NULL; thismap++)
68 data->fn(provider, thismap, no_store, data->data);
71 /* Do we fulfill post-conditions? */
72 if (data->post == NULL) {
73 /* If there is no post-condition function, assume "yes" */
75 } else if (!data->post(provider, cur_operation, no_store, data->data,
77 /* Error, bail out! */
85 * Given a provider, process one operation given by |data->operation_id|, or
86 * if that's zero, process all known operations.
87 * For each such operation, query the associated OSSL_ALGORITHM array from
88 * the provider, then process that array with |algorithm_do_map()|.
90 static int algorithm_do_this(OSSL_PROVIDER *provider, void *cbdata)
92 struct algorithm_data_st *data = cbdata;
93 int first_operation = 1;
94 int last_operation = OSSL_OP__HIGHEST;
98 if (data->operation_id != 0)
99 first_operation = last_operation = data->operation_id;
101 for (cur_operation = first_operation;
102 cur_operation <= last_operation;
104 int no_store = 0; /* Assume caching is ok */
105 const OSSL_ALGORITHM *map = NULL;
108 map = ossl_provider_query_operation(provider, cur_operation,
110 ret = algorithm_do_map(provider, map, cur_operation, no_store, data);
111 ossl_provider_unquery_operation(provider, cur_operation, map);
114 /* Hard error, bail out immediately! */
117 /* If post-condition not fulfilled, set general failure */
125 void ossl_algorithm_do_all(OSSL_LIB_CTX *libctx, int operation_id,
126 OSSL_PROVIDER *provider,
127 int (*pre)(OSSL_PROVIDER *, int operation_id,
128 int no_store, void *data, int *result),
129 void (*fn)(OSSL_PROVIDER *provider,
130 const OSSL_ALGORITHM *algo,
131 int no_store, void *data),
132 int (*post)(OSSL_PROVIDER *, int operation_id,
133 int no_store, void *data, int *result),
136 struct algorithm_data_st cbdata = { 0, };
138 cbdata.libctx = libctx;
139 cbdata.operation_id = operation_id;
145 if (provider == NULL) {
146 ossl_provider_doall_activated(libctx, algorithm_do_this, &cbdata);
148 OSSL_LIB_CTX *libctx2 = ossl_provider_libctx(provider);
151 * If a provider is given, its library context MUST match the library
152 * context we're passed. If this turns out not to be true, there is
153 * a programming error in the functions up the call stack.
155 if (!ossl_assert(ossl_lib_ctx_get_concrete(libctx)
156 == ossl_lib_ctx_get_concrete(libctx2)))
159 cbdata.libctx = libctx2;
160 algorithm_do_this(provider, &cbdata);
164 char *ossl_algorithm_get1_first_name(const OSSL_ALGORITHM *algo)
166 const char *first_name_end = NULL;
167 size_t first_name_len = 0;
170 if (algo->algorithm_names == NULL)
173 first_name_end = strchr(algo->algorithm_names, ':');
174 if (first_name_end == NULL)
175 first_name_len = strlen(algo->algorithm_names);
177 first_name_len = first_name_end - algo->algorithm_names;
179 ret = OPENSSL_strndup(algo->algorithm_names, first_name_len);
181 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);