/src/openssl/crypto/evp/names.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * Copyright 1995-2024 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 <stdio.h>  | 
11  |  | #include <openssl/evp.h>  | 
12  |  | #include <openssl/kdf.h>  | 
13  |  | #include <openssl/x509.h>  | 
14  |  | #include "internal/cryptlib.h"  | 
15  |  | #include "internal/namemap.h"  | 
16  |  | #include "crypto/objects.h"  | 
17  |  | #include "crypto/evp.h"  | 
18  |  |  | 
19  |  | int EVP_add_cipher(const EVP_CIPHER *c)  | 
20  | 144  | { | 
21  | 144  |     int r;  | 
22  |  |  | 
23  | 144  |     if (c == NULL)  | 
24  | 0  |         return 0;  | 
25  |  |  | 
26  | 144  |     r = OBJ_NAME_add(OBJ_nid2sn(c->nid), OBJ_NAME_TYPE_CIPHER_METH,  | 
27  | 144  |                      (const char *)c);  | 
28  | 144  |     if (r == 0)  | 
29  | 0  |         return 0;  | 
30  | 144  |     r = OBJ_NAME_add(OBJ_nid2ln(c->nid), OBJ_NAME_TYPE_CIPHER_METH,  | 
31  | 144  |                      (const char *)c);  | 
32  | 144  |     return r;  | 
33  | 144  | }  | 
34  |  |  | 
35  |  | int EVP_add_digest(const EVP_MD *md)  | 
36  | 22  | { | 
37  | 22  |     int r;  | 
38  | 22  |     const char *name;  | 
39  |  |  | 
40  | 22  |     name = OBJ_nid2sn(md->type);  | 
41  | 22  |     r = OBJ_NAME_add(name, OBJ_NAME_TYPE_MD_METH, (const char *)md);  | 
42  | 22  |     if (r == 0)  | 
43  | 0  |         return 0;  | 
44  | 22  |     r = OBJ_NAME_add(OBJ_nid2ln(md->type), OBJ_NAME_TYPE_MD_METH,  | 
45  | 22  |                      (const char *)md);  | 
46  | 22  |     if (r == 0)  | 
47  | 0  |         return 0;  | 
48  |  |  | 
49  | 22  |     if (md->pkey_type && md->type != md->pkey_type) { | 
50  | 16  |         r = OBJ_NAME_add(OBJ_nid2sn(md->pkey_type),  | 
51  | 16  |                          OBJ_NAME_TYPE_MD_METH | OBJ_NAME_ALIAS, name);  | 
52  | 16  |         if (r == 0)  | 
53  | 0  |             return 0;  | 
54  | 16  |         r = OBJ_NAME_add(OBJ_nid2ln(md->pkey_type),  | 
55  | 16  |                          OBJ_NAME_TYPE_MD_METH | OBJ_NAME_ALIAS, name);  | 
56  | 16  |     }  | 
57  | 22  |     return r;  | 
58  | 22  | }  | 
59  |  |  | 
60  |  | static void cipher_from_name(const char *name, void *data)  | 
61  | 0  | { | 
62  | 0  |     const EVP_CIPHER **cipher = data;  | 
63  |  | 
  | 
64  | 0  |     if (*cipher != NULL)  | 
65  | 0  |         return;  | 
66  |  |  | 
67  | 0  |     *cipher = (const EVP_CIPHER *)OBJ_NAME_get(name, OBJ_NAME_TYPE_CIPHER_METH);  | 
68  | 0  | }  | 
69  |  |  | 
70  |  | const EVP_CIPHER *EVP_get_cipherbyname(const char *name)  | 
71  | 0  | { | 
72  | 0  |     return evp_get_cipherbyname_ex(NULL, name);  | 
73  | 0  | }  | 
74  |  |  | 
75  |  | const EVP_CIPHER *evp_get_cipherbyname_ex(OSSL_LIB_CTX *libctx,  | 
76  |  |                                           const char *name)  | 
77  | 0  | { | 
78  | 0  |     const EVP_CIPHER *cp;  | 
79  | 0  |     OSSL_NAMEMAP *namemap;  | 
80  | 0  |     int id;  | 
81  | 0  |     int do_retry = 1;  | 
82  |  | 
  | 
83  | 0  |     if (!OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS, NULL))  | 
84  | 0  |         return NULL;  | 
85  |  |  | 
86  | 0  |     cp = (const EVP_CIPHER *)OBJ_NAME_get(name, OBJ_NAME_TYPE_CIPHER_METH);  | 
87  |  | 
  | 
88  | 0  |     if (cp != NULL)  | 
89  | 0  |         return cp;  | 
90  |  |  | 
91  |  |     /*  | 
92  |  |      * It's not in the method database, but it might be there under a different  | 
93  |  |      * name. So we check for aliases in the EVP namemap and try all of those  | 
94  |  |      * in turn.  | 
95  |  |      */  | 
96  |  |  | 
97  | 0  |     namemap = ossl_namemap_stored(libctx);  | 
98  | 0  |  retry:  | 
99  | 0  |     id = ossl_namemap_name2num(namemap, name);  | 
100  | 0  |     if (id == 0) { | 
101  | 0  |         EVP_CIPHER *fetched_cipher;  | 
102  |  |  | 
103  |  |         /* Try to fetch it because the name might not be known yet. */  | 
104  | 0  |         if (!do_retry)  | 
105  | 0  |             return NULL;  | 
106  | 0  |         do_retry = 0;  | 
107  | 0  |         ERR_set_mark();  | 
108  | 0  |         fetched_cipher = EVP_CIPHER_fetch(libctx, name, NULL);  | 
109  | 0  |         EVP_CIPHER_free(fetched_cipher);  | 
110  | 0  |         ERR_pop_to_mark();  | 
111  | 0  |         goto retry;  | 
112  | 0  |     }  | 
113  |  |  | 
114  | 0  |     if (!ossl_namemap_doall_names(namemap, id, cipher_from_name, &cp))  | 
115  | 0  |         return NULL;  | 
116  |  |  | 
117  | 0  |     return cp;  | 
118  | 0  | }  | 
119  |  |  | 
120  |  | static void digest_from_name(const char *name, void *data)  | 
121  | 0  | { | 
122  | 0  |     const EVP_MD **md = data;  | 
123  |  | 
  | 
124  | 0  |     if (*md != NULL)  | 
125  | 0  |         return;  | 
126  |  |  | 
127  | 0  |     *md = (const EVP_MD *)OBJ_NAME_get(name, OBJ_NAME_TYPE_MD_METH);  | 
128  | 0  | }  | 
129  |  |  | 
130  |  | const EVP_MD *EVP_get_digestbyname(const char *name)  | 
131  | 0  | { | 
132  | 0  |     return evp_get_digestbyname_ex(NULL, name);  | 
133  | 0  | }  | 
134  |  |  | 
135  |  | const EVP_MD *evp_get_digestbyname_ex(OSSL_LIB_CTX *libctx, const char *name)  | 
136  | 0  | { | 
137  | 0  |     const EVP_MD *dp;  | 
138  | 0  |     OSSL_NAMEMAP *namemap;  | 
139  | 0  |     int id;  | 
140  | 0  |     int do_retry = 1;  | 
141  |  | 
  | 
142  | 0  |     if (!OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_DIGESTS, NULL))  | 
143  | 0  |         return NULL;  | 
144  |  |  | 
145  | 0  |     dp = (const EVP_MD *)OBJ_NAME_get(name, OBJ_NAME_TYPE_MD_METH);  | 
146  |  | 
  | 
147  | 0  |     if (dp != NULL)  | 
148  | 0  |         return dp;  | 
149  |  |  | 
150  |  |     /*  | 
151  |  |      * It's not in the method database, but it might be there under a different  | 
152  |  |      * name. So we check for aliases in the EVP namemap and try all of those  | 
153  |  |      * in turn.  | 
154  |  |      */  | 
155  |  |  | 
156  | 0  |     namemap = ossl_namemap_stored(libctx);  | 
157  | 0  |  retry:  | 
158  | 0  |     id = ossl_namemap_name2num(namemap, name);  | 
159  | 0  |     if (id == 0) { | 
160  | 0  |         EVP_MD *fetched_md;  | 
161  |  |  | 
162  |  |         /* Try to fetch it because the name might not be known yet. */  | 
163  | 0  |         if (!do_retry)  | 
164  | 0  |             return NULL;  | 
165  | 0  |         do_retry = 0;  | 
166  | 0  |         ERR_set_mark();  | 
167  | 0  |         fetched_md = EVP_MD_fetch(libctx, name, NULL);  | 
168  | 0  |         EVP_MD_free(fetched_md);  | 
169  | 0  |         ERR_pop_to_mark();  | 
170  | 0  |         goto retry;  | 
171  | 0  |     }  | 
172  |  |  | 
173  | 0  |     if (!ossl_namemap_doall_names(namemap, id, digest_from_name, &dp))  | 
174  | 0  |         return NULL;  | 
175  |  |  | 
176  | 0  |     return dp;  | 
177  | 0  | }  | 
178  |  |  | 
179  |  | void evp_cleanup_int(void)  | 
180  | 2  | { | 
181  | 2  |     OBJ_NAME_cleanup(OBJ_NAME_TYPE_KDF_METH);  | 
182  | 2  |     OBJ_NAME_cleanup(OBJ_NAME_TYPE_CIPHER_METH);  | 
183  | 2  |     OBJ_NAME_cleanup(OBJ_NAME_TYPE_MD_METH);  | 
184  |  |     /*  | 
185  |  |      * The above calls will only clean out the contents of the name hash  | 
186  |  |      * table, but not the hash table itself.  The following line does that  | 
187  |  |      * part.  -- Richard Levitte  | 
188  |  |      */  | 
189  | 2  |     OBJ_NAME_cleanup(-1);  | 
190  |  |  | 
191  | 2  |     EVP_PBE_cleanup();  | 
192  | 2  |     OBJ_sigid_free();  | 
193  |  |  | 
194  | 2  |     evp_app_cleanup_int();  | 
195  | 2  | }  | 
196  |  |  | 
197  |  | struct doall_cipher { | 
198  |  |     void *arg;  | 
199  |  |     void (*fn) (const EVP_CIPHER *ciph,  | 
200  |  |                 const char *from, const char *to, void *arg);  | 
201  |  | };  | 
202  |  |  | 
203  |  | static void do_all_cipher_fn(const OBJ_NAME *nm, void *arg)  | 
204  | 0  | { | 
205  | 0  |     struct doall_cipher *dc = arg;  | 
206  | 0  |     if (nm->alias)  | 
207  | 0  |         dc->fn(NULL, nm->name, nm->data, dc->arg);  | 
208  | 0  |     else  | 
209  | 0  |         dc->fn((const EVP_CIPHER *)nm->data, nm->name, NULL, dc->arg);  | 
210  | 0  | }  | 
211  |  |  | 
212  |  | void EVP_CIPHER_do_all(void (*fn) (const EVP_CIPHER *ciph,  | 
213  |  |                                    const char *from, const char *to, void *x),  | 
214  |  |                        void *arg)  | 
215  | 0  | { | 
216  | 0  |     struct doall_cipher dc;  | 
217  |  |  | 
218  |  |     /* Ignore errors */  | 
219  | 0  |     OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS, NULL);  | 
220  |  | 
  | 
221  | 0  |     dc.fn = fn;  | 
222  | 0  |     dc.arg = arg;  | 
223  | 0  |     OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, do_all_cipher_fn, &dc);  | 
224  | 0  | }  | 
225  |  |  | 
226  |  | void EVP_CIPHER_do_all_sorted(void (*fn) (const EVP_CIPHER *ciph,  | 
227  |  |                                           const char *from, const char *to,  | 
228  |  |                                           void *x), void *arg)  | 
229  | 0  | { | 
230  | 0  |     struct doall_cipher dc;  | 
231  |  |  | 
232  |  |     /* Ignore errors */  | 
233  | 0  |     OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS, NULL);  | 
234  |  | 
  | 
235  | 0  |     dc.fn = fn;  | 
236  | 0  |     dc.arg = arg;  | 
237  | 0  |     OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_CIPHER_METH, do_all_cipher_fn, &dc);  | 
238  | 0  | }  | 
239  |  |  | 
240  |  | struct doall_md { | 
241  |  |     void *arg;  | 
242  |  |     void (*fn) (const EVP_MD *ciph,  | 
243  |  |                 const char *from, const char *to, void *arg);  | 
244  |  | };  | 
245  |  |  | 
246  |  | static void do_all_md_fn(const OBJ_NAME *nm, void *arg)  | 
247  | 0  | { | 
248  | 0  |     struct doall_md *dc = arg;  | 
249  | 0  |     if (nm->alias)  | 
250  | 0  |         dc->fn(NULL, nm->name, nm->data, dc->arg);  | 
251  | 0  |     else  | 
252  | 0  |         dc->fn((const EVP_MD *)nm->data, nm->name, NULL, dc->arg);  | 
253  | 0  | }  | 
254  |  |  | 
255  |  | void EVP_MD_do_all(void (*fn) (const EVP_MD *md,  | 
256  |  |                                const char *from, const char *to, void *x),  | 
257  |  |                    void *arg)  | 
258  | 0  | { | 
259  | 0  |     struct doall_md dc;  | 
260  |  |  | 
261  |  |     /* Ignore errors */  | 
262  | 0  |     OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_DIGESTS, NULL);  | 
263  |  | 
  | 
264  | 0  |     dc.fn = fn;  | 
265  | 0  |     dc.arg = arg;  | 
266  | 0  |     OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, do_all_md_fn, &dc);  | 
267  | 0  | }  | 
268  |  |  | 
269  |  | void EVP_MD_do_all_sorted(void (*fn) (const EVP_MD *md,  | 
270  |  |                                       const char *from, const char *to,  | 
271  |  |                                       void *x), void *arg)  | 
272  | 0  | { | 
273  | 0  |     struct doall_md dc;  | 
274  |  | 
  | 
275  | 0  |     OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_DIGESTS, NULL);  | 
276  |  | 
  | 
277  | 0  |     dc.fn = fn;  | 
278  | 0  |     dc.arg = arg;  | 
279  | 0  |     OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_MD_METH, do_all_md_fn, &dc);  | 
280  | 0  | }  |