]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/evp/names.c
Reorganize private crypto header files
[thirdparty/openssl.git] / crypto / evp / names.c
1 /*
2 * Copyright 1995-2018 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 "internal/cryptlib.h"
12 #include <openssl/evp.h>
13 #include <openssl/kdf.h>
14 #include "crypto/objects.h"
15 #include <openssl/x509.h>
16 #include "crypto/evp.h"
17
18 int EVP_add_cipher(const EVP_CIPHER *c)
19 {
20 int r;
21
22 if (c == NULL)
23 return 0;
24
25 r = OBJ_NAME_add(OBJ_nid2sn(c->nid), OBJ_NAME_TYPE_CIPHER_METH,
26 (const char *)c);
27 if (r == 0)
28 return 0;
29 r = OBJ_NAME_add(OBJ_nid2ln(c->nid), OBJ_NAME_TYPE_CIPHER_METH,
30 (const char *)c);
31 return r;
32 }
33
34 int EVP_add_digest(const EVP_MD *md)
35 {
36 int r;
37 const char *name;
38
39 name = OBJ_nid2sn(md->type);
40 r = OBJ_NAME_add(name, OBJ_NAME_TYPE_MD_METH, (const char *)md);
41 if (r == 0)
42 return 0;
43 r = OBJ_NAME_add(OBJ_nid2ln(md->type), OBJ_NAME_TYPE_MD_METH,
44 (const char *)md);
45 if (r == 0)
46 return 0;
47
48 if (md->pkey_type && md->type != md->pkey_type) {
49 r = OBJ_NAME_add(OBJ_nid2sn(md->pkey_type),
50 OBJ_NAME_TYPE_MD_METH | OBJ_NAME_ALIAS, name);
51 if (r == 0)
52 return 0;
53 r = OBJ_NAME_add(OBJ_nid2ln(md->pkey_type),
54 OBJ_NAME_TYPE_MD_METH | OBJ_NAME_ALIAS, name);
55 }
56 return r;
57 }
58
59 const EVP_CIPHER *EVP_get_cipherbyname(const char *name)
60 {
61 const EVP_CIPHER *cp;
62
63 if (!OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS, NULL))
64 return NULL;
65
66 cp = (const EVP_CIPHER *)OBJ_NAME_get(name, OBJ_NAME_TYPE_CIPHER_METH);
67 return cp;
68 }
69
70 const EVP_MD *EVP_get_digestbyname(const char *name)
71 {
72 const EVP_MD *cp;
73
74 if (!OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_DIGESTS, NULL))
75 return NULL;
76
77 cp = (const EVP_MD *)OBJ_NAME_get(name, OBJ_NAME_TYPE_MD_METH);
78 return cp;
79 }
80
81 void evp_cleanup_int(void)
82 {
83 OBJ_NAME_cleanup(OBJ_NAME_TYPE_KDF_METH);
84 OBJ_NAME_cleanup(OBJ_NAME_TYPE_CIPHER_METH);
85 OBJ_NAME_cleanup(OBJ_NAME_TYPE_MD_METH);
86 /*
87 * The above calls will only clean out the contents of the name hash
88 * table, but not the hash table itself. The following line does that
89 * part. -- Richard Levitte
90 */
91 OBJ_NAME_cleanup(-1);
92
93 EVP_PBE_cleanup();
94 OBJ_sigid_free();
95
96 evp_app_cleanup_int();
97 }
98
99 struct doall_cipher {
100 void *arg;
101 void (*fn) (const EVP_CIPHER *ciph,
102 const char *from, const char *to, void *arg);
103 };
104
105 static void do_all_cipher_fn(const OBJ_NAME *nm, void *arg)
106 {
107 struct doall_cipher *dc = arg;
108 if (nm->alias)
109 dc->fn(NULL, nm->name, nm->data, dc->arg);
110 else
111 dc->fn((const EVP_CIPHER *)nm->data, nm->name, NULL, dc->arg);
112 }
113
114 void EVP_CIPHER_do_all(void (*fn) (const EVP_CIPHER *ciph,
115 const char *from, const char *to, void *x),
116 void *arg)
117 {
118 struct doall_cipher dc;
119
120 /* Ignore errors */
121 OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS, NULL);
122
123 dc.fn = fn;
124 dc.arg = arg;
125 OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, do_all_cipher_fn, &dc);
126 }
127
128 void EVP_CIPHER_do_all_sorted(void (*fn) (const EVP_CIPHER *ciph,
129 const char *from, const char *to,
130 void *x), void *arg)
131 {
132 struct doall_cipher dc;
133
134 /* Ignore errors */
135 OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS, NULL);
136
137 dc.fn = fn;
138 dc.arg = arg;
139 OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_CIPHER_METH, do_all_cipher_fn, &dc);
140 }
141
142 struct doall_md {
143 void *arg;
144 void (*fn) (const EVP_MD *ciph,
145 const char *from, const char *to, void *arg);
146 };
147
148 static void do_all_md_fn(const OBJ_NAME *nm, void *arg)
149 {
150 struct doall_md *dc = arg;
151 if (nm->alias)
152 dc->fn(NULL, nm->name, nm->data, dc->arg);
153 else
154 dc->fn((const EVP_MD *)nm->data, nm->name, NULL, dc->arg);
155 }
156
157 void EVP_MD_do_all(void (*fn) (const EVP_MD *md,
158 const char *from, const char *to, void *x),
159 void *arg)
160 {
161 struct doall_md dc;
162
163 /* Ignore errors */
164 OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_DIGESTS, NULL);
165
166 dc.fn = fn;
167 dc.arg = arg;
168 OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, do_all_md_fn, &dc);
169 }
170
171 void EVP_MD_do_all_sorted(void (*fn) (const EVP_MD *md,
172 const char *from, const char *to,
173 void *x), void *arg)
174 {
175 struct doall_md dc;
176
177 OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_DIGESTS, NULL);
178
179 dc.fn = fn;
180 dc.arg = arg;
181 OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_MD_METH, do_all_md_fn, &dc);
182 }