add a check for the return of OBJ_new_nid()
[openssl.git] / crypto / core_algorithm.c
1 /*
2  * Copyright 2019-2021 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 <openssl/core.h>
11 #include <openssl/core_dispatch.h>
12 #include "internal/core.h"
13 #include "internal/property.h"
14 #include "internal/provider.h"
15
16 struct algorithm_data_st {
17     OSSL_LIB_CTX *libctx;
18     int operation_id;            /* May be zero for finding them all */
19     int (*pre)(OSSL_PROVIDER *, int operation_id, int no_store, void *data,
20                int *result);
21     void (*fn)(OSSL_PROVIDER *, const OSSL_ALGORITHM *, int no_store,
22                void *data);
23     int (*post)(OSSL_PROVIDER *, int operation_id, int no_store, void *data,
24                 int *result);
25     void *data;
26 };
27
28 /*
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|.
34  *
35  * Returns:
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
39  */
40 static int algorithm_do_map(OSSL_PROVIDER *provider, const OSSL_ALGORITHM *map,
41                             int cur_operation, int no_store, void *cbdata)
42 {
43     struct algorithm_data_st *data = cbdata;
44     int ret = 0;
45
46     /* Do we fulfill pre-conditions? */
47     if (data->pre == NULL) {
48         /* If there is no pre-condition function, assume "yes" */
49         ret = 1;
50     } else if (!data->pre(provider, cur_operation, no_store, data->data,
51                           &ret)) {
52         /* Error, bail out! */
53         return -1;
54     }
55
56     /*
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
59      * got to it first.
60      */
61     if (ret == 0)
62         return 1;
63
64     if (map != NULL) {
65         const OSSL_ALGORITHM *thismap;
66
67         for (thismap = map; thismap->algorithm_names != NULL; thismap++)
68             data->fn(provider, thismap, no_store, data->data);
69     }
70
71     /* Do we fulfill post-conditions? */
72     if (data->post == NULL) {
73         /* If there is no post-condition function, assume "yes" */
74         ret = 1;
75     } else if (!data->post(provider, cur_operation, no_store, data->data,
76                            &ret)) {
77         /* Error, bail out! */
78         return -1;
79     }
80
81     return ret;
82 }
83
84 /*
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()|.
89  */
90 static int algorithm_do_this(OSSL_PROVIDER *provider, void *cbdata)
91 {
92     struct algorithm_data_st *data = cbdata;
93     int first_operation = 1;
94     int last_operation = OSSL_OP__HIGHEST;
95     int cur_operation;
96     int ok = 1;
97
98     if (data->operation_id != 0)
99         first_operation = last_operation = data->operation_id;
100
101     for (cur_operation = first_operation;
102          cur_operation <= last_operation;
103          cur_operation++) {
104         int no_store = 0;        /* Assume caching is ok */
105         const OSSL_ALGORITHM *map = NULL;
106         int ret;
107
108         map = ossl_provider_query_operation(provider, cur_operation,
109                                             &no_store);
110         ret = algorithm_do_map(provider, map, cur_operation, no_store, data);
111         ossl_provider_unquery_operation(provider, cur_operation, map);
112
113         if (ret < 0)
114             /* Hard error, bail out immediately! */
115             return 0;
116
117         /* If post-condition not fulfilled, set general failure */
118         if (!ret)
119             ok = 0;
120     }
121
122     return ok;
123 }
124
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),
134                            void *data)
135 {
136     struct algorithm_data_st cbdata = { 0, };
137
138     cbdata.libctx = libctx;
139     cbdata.operation_id = operation_id;
140     cbdata.pre = pre;
141     cbdata.fn = fn;
142     cbdata.post = post;
143     cbdata.data = data;
144
145     if (provider == NULL) {
146         ossl_provider_doall_activated(libctx, algorithm_do_this, &cbdata);
147     } else {
148         OSSL_LIB_CTX *libctx2 = ossl_provider_libctx(provider);
149
150         /*
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.
154          */
155         if (!ossl_assert(ossl_lib_ctx_get_concrete(libctx)
156                          == ossl_lib_ctx_get_concrete(libctx2)))
157             return;
158
159         cbdata.libctx = libctx2;
160         algorithm_do_this(provider, &cbdata);
161     }
162 }
163
164 char *ossl_algorithm_get1_first_name(const OSSL_ALGORITHM *algo)
165 {
166     const char *first_name_end = NULL;
167     size_t first_name_len = 0;
168     char *ret;
169
170     if (algo->algorithm_names == NULL)
171         return NULL;
172
173     first_name_end = strchr(algo->algorithm_names, ':');
174     if (first_name_end == NULL)
175         first_name_len = strlen(algo->algorithm_names);
176     else
177         first_name_len = first_name_end - algo->algorithm_names;
178
179     ret = OPENSSL_strndup(algo->algorithm_names, first_name_len);
180     if (ret == NULL)
181         ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
182     return ret;
183 }