]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/evp/names.c
Rename OPENSSL_CTX prefix to OSSL_LIB_CTX
[thirdparty/openssl.git] / crypto / evp / names.c
CommitLineData
62867571 1/*
567db2c1 2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
4a8b0c55 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
62867571
RS
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
d02b48c6
RE
8 */
9
10#include <stdio.h>
ec577822 11#include <openssl/evp.h>
d2ba8123 12#include <openssl/kdf.h>
ec577822 13#include <openssl/x509.h>
7606bed9
MC
14#include "internal/cryptlib.h"
15#include "internal/namemap.h"
16#include "crypto/objects.h"
25f2138b 17#include "crypto/evp.h"
d02b48c6 18
13588350 19int EVP_add_cipher(const EVP_CIPHER *c)
0f113f3e
MC
20{
21 int r;
22
23 if (c == NULL)
24 return 0;
25
26 r = OBJ_NAME_add(OBJ_nid2sn(c->nid), OBJ_NAME_TYPE_CIPHER_METH,
27 (const char *)c);
28 if (r == 0)
26a7d938 29 return 0;
0f113f3e
MC
30 r = OBJ_NAME_add(OBJ_nid2ln(c->nid), OBJ_NAME_TYPE_CIPHER_METH,
31 (const char *)c);
26a7d938 32 return r;
0f113f3e 33}
5ba4bf35 34
13588350 35int EVP_add_digest(const EVP_MD *md)
0f113f3e
MC
36{
37 int r;
38 const char *name;
39
40 name = OBJ_nid2sn(md->type);
41 r = OBJ_NAME_add(name, OBJ_NAME_TYPE_MD_METH, (const char *)md);
42 if (r == 0)
26a7d938 43 return 0;
0f113f3e
MC
44 r = OBJ_NAME_add(OBJ_nid2ln(md->type), OBJ_NAME_TYPE_MD_METH,
45 (const char *)md);
46 if (r == 0)
26a7d938 47 return 0;
0f113f3e
MC
48
49 if (md->pkey_type && md->type != md->pkey_type) {
50 r = OBJ_NAME_add(OBJ_nid2sn(md->pkey_type),
51 OBJ_NAME_TYPE_MD_METH | OBJ_NAME_ALIAS, name);
52 if (r == 0)
26a7d938 53 return 0;
0f113f3e
MC
54 r = OBJ_NAME_add(OBJ_nid2ln(md->pkey_type),
55 OBJ_NAME_TYPE_MD_METH | OBJ_NAME_ALIAS, name);
56 }
26a7d938 57 return r;
0f113f3e 58}
d02b48c6 59
7606bed9
MC
60static void cipher_from_name(const char *name, void *data)
61{
62 const EVP_CIPHER **cipher = data;
63
64 if (*cipher != NULL)
65 return;
66
67 *cipher = (const EVP_CIPHER *)OBJ_NAME_get(name, OBJ_NAME_TYPE_CIPHER_METH);
68}
69
6b691a5c 70const EVP_CIPHER *EVP_get_cipherbyname(const char *name)
7606bed9
MC
71{
72 return evp_get_cipherbyname_ex(NULL, name);
73}
74
b4250010
DMSP
75const EVP_CIPHER *evp_get_cipherbyname_ex(OSSL_LIB_CTX *libctx,
76 const char *name)
0f113f3e
MC
77{
78 const EVP_CIPHER *cp;
7606bed9
MC
79 OSSL_NAMEMAP *namemap;
80 int id;
d02b48c6 81
0fc32b07
MC
82 if (!OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS, NULL))
83 return NULL;
7b9f8f7f 84
0f113f3e 85 cp = (const EVP_CIPHER *)OBJ_NAME_get(name, OBJ_NAME_TYPE_CIPHER_METH);
7606bed9
MC
86
87 if (cp != NULL)
88 return cp;
89
90 /*
91 * It's not in the method database, but it might be there under a different
92 * name. So we check for aliases in the EVP namemap and try all of those
93 * in turn.
94 */
95
96 namemap = ossl_namemap_stored(libctx);
97 id = ossl_namemap_name2num(namemap, name);
98 if (id == 0)
99 return NULL;
100
101 ossl_namemap_doall_names(namemap, id, cipher_from_name, &cp);
102
26a7d938 103 return cp;
0f113f3e 104}
d02b48c6 105
7606bed9
MC
106static void digest_from_name(const char *name, void *data)
107{
108 const EVP_MD **md = data;
109
110 if (*md != NULL)
111 return;
112
113 *md = (const EVP_MD *)OBJ_NAME_get(name, OBJ_NAME_TYPE_MD_METH);
114}
115
6b691a5c 116const EVP_MD *EVP_get_digestbyname(const char *name)
0f113f3e 117{
7606bed9
MC
118 return evp_get_digestbyname_ex(NULL, name);
119}
120
b4250010 121const EVP_MD *evp_get_digestbyname_ex(OSSL_LIB_CTX *libctx, const char *name)
7606bed9
MC
122{
123 const EVP_MD *dp;
124 OSSL_NAMEMAP *namemap;
125 int id;
d02b48c6 126
0fc32b07
MC
127 if (!OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_DIGESTS, NULL))
128 return NULL;
7b9f8f7f 129
7606bed9
MC
130 dp = (const EVP_MD *)OBJ_NAME_get(name, OBJ_NAME_TYPE_MD_METH);
131
132 if (dp != NULL)
133 return dp;
134
135 /*
136 * It's not in the method database, but it might be there under a different
137 * name. So we check for aliases in the EVP namemap and try all of those
138 * in turn.
139 */
140
141 namemap = ossl_namemap_stored(libctx);
142 id = ossl_namemap_name2num(namemap, name);
143 if (id == 0)
144 return NULL;
145
146 ossl_namemap_doall_names(namemap, id, digest_from_name, &dp);
147
148 return dp;
0f113f3e 149}
d02b48c6 150
b3599dbb 151void evp_cleanup_int(void)
0f113f3e 152{
d2ba8123 153 OBJ_NAME_cleanup(OBJ_NAME_TYPE_KDF_METH);
0f113f3e
MC
154 OBJ_NAME_cleanup(OBJ_NAME_TYPE_CIPHER_METH);
155 OBJ_NAME_cleanup(OBJ_NAME_TYPE_MD_METH);
156 /*
157 * The above calls will only clean out the contents of the name hash
158 * table, but not the hash table itself. The following line does that
159 * part. -- Richard Levitte
160 */
161 OBJ_NAME_cleanup(-1);
162
163 EVP_PBE_cleanup();
0f113f3e 164 OBJ_sigid_free();
0822e89a
PY
165
166 evp_app_cleanup_int();
0f113f3e
MC
167}
168
169struct doall_cipher {
170 void *arg;
171 void (*fn) (const EVP_CIPHER *ciph,
172 const char *from, const char *to, void *arg);
173};
5ba4bf35
DSH
174
175static void do_all_cipher_fn(const OBJ_NAME *nm, void *arg)
0f113f3e
MC
176{
177 struct doall_cipher *dc = arg;
178 if (nm->alias)
179 dc->fn(NULL, nm->name, nm->data, dc->arg);
180 else
181 dc->fn((const EVP_CIPHER *)nm->data, nm->name, NULL, dc->arg);
182}
183
184void EVP_CIPHER_do_all(void (*fn) (const EVP_CIPHER *ciph,
185 const char *from, const char *to, void *x),
186 void *arg)
187{
188 struct doall_cipher dc;
7b9f8f7f 189
0fc32b07 190 /* Ignore errors */
f672aee4 191 OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS, NULL);
7b9f8f7f 192
0f113f3e
MC
193 dc.fn = fn;
194 dc.arg = arg;
195 OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, do_all_cipher_fn, &dc);
196}
197
198void EVP_CIPHER_do_all_sorted(void (*fn) (const EVP_CIPHER *ciph,
199 const char *from, const char *to,
200 void *x), void *arg)
201{
202 struct doall_cipher dc;
7b9f8f7f 203
0fc32b07 204 /* Ignore errors */
f672aee4 205 OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS, NULL);
7b9f8f7f 206
0f113f3e
MC
207 dc.fn = fn;
208 dc.arg = arg;
209 OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_CIPHER_METH, do_all_cipher_fn, &dc);
210}
211
212struct doall_md {
213 void *arg;
214 void (*fn) (const EVP_MD *ciph,
215 const char *from, const char *to, void *arg);
216};
5ba4bf35
DSH
217
218static void do_all_md_fn(const OBJ_NAME *nm, void *arg)
0f113f3e
MC
219{
220 struct doall_md *dc = arg;
221 if (nm->alias)
222 dc->fn(NULL, nm->name, nm->data, dc->arg);
223 else
224 dc->fn((const EVP_MD *)nm->data, nm->name, NULL, dc->arg);
225}
226
227void EVP_MD_do_all(void (*fn) (const EVP_MD *md,
228 const char *from, const char *to, void *x),
229 void *arg)
230{
231 struct doall_md dc;
7b9f8f7f 232
0fc32b07 233 /* Ignore errors */
f672aee4 234 OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_DIGESTS, NULL);
7b9f8f7f 235
0f113f3e
MC
236 dc.fn = fn;
237 dc.arg = arg;
238 OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, do_all_md_fn, &dc);
239}
240
241void EVP_MD_do_all_sorted(void (*fn) (const EVP_MD *md,
242 const char *from, const char *to,
243 void *x), void *arg)
244{
245 struct doall_md dc;
7b9f8f7f 246
f672aee4 247 OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_DIGESTS, NULL);
7b9f8f7f 248
0f113f3e
MC
249 dc.fn = fn;
250 dc.arg = arg;
251 OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_MD_METH, do_all_md_fn, &dc);
252}