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