]>
Commit | Line | Data |
---|---|---|
62867571 RS |
1 | /* |
2 | * Copyright 1995-2016 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> |
7b8cc9b3 | 13 | #include <internal/objects.h> |
ec577822 | 14 | #include <openssl/x509.h> |
ab0a14bb | 15 | #include "internal/evp_int.h" |
d02b48c6 | 16 | |
13588350 | 17 | int 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) | |
27 | return (0); | |
0f113f3e MC |
28 | r = OBJ_NAME_add(OBJ_nid2ln(c->nid), OBJ_NAME_TYPE_CIPHER_METH, |
29 | (const char *)c); | |
30 | return (r); | |
31 | } | |
5ba4bf35 | 32 | |
13588350 | 33 | int 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) | |
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) | |
45 | return (0); | |
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) | |
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 | } | |
55 | return (r); | |
56 | } | |
d02b48c6 | 57 | |
6b691a5c | 58 | const EVP_CIPHER *EVP_get_cipherbyname(const char *name) |
0f113f3e MC |
59 | { |
60 | const EVP_CIPHER *cp; | |
d02b48c6 | 61 | |
0fc32b07 MC |
62 | if (!OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS, NULL)) |
63 | return NULL; | |
7b9f8f7f | 64 | |
0f113f3e MC |
65 | cp = (const EVP_CIPHER *)OBJ_NAME_get(name, OBJ_NAME_TYPE_CIPHER_METH); |
66 | return (cp); | |
67 | } | |
d02b48c6 | 68 | |
6b691a5c | 69 | const EVP_MD *EVP_get_digestbyname(const char *name) |
0f113f3e MC |
70 | { |
71 | const EVP_MD *cp; | |
d02b48c6 | 72 | |
0fc32b07 MC |
73 | if (!OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_DIGESTS, NULL)) |
74 | return NULL; | |
7b9f8f7f | 75 | |
0f113f3e MC |
76 | cp = (const EVP_MD *)OBJ_NAME_get(name, OBJ_NAME_TYPE_MD_METH); |
77 | return (cp); | |
78 | } | |
d02b48c6 | 79 | |
b3599dbb | 80 | void evp_cleanup_int(void) |
0f113f3e MC |
81 | { |
82 | OBJ_NAME_cleanup(OBJ_NAME_TYPE_CIPHER_METH); | |
83 | OBJ_NAME_cleanup(OBJ_NAME_TYPE_MD_METH); | |
84 | /* | |
85 | * The above calls will only clean out the contents of the name hash | |
86 | * table, but not the hash table itself. The following line does that | |
87 | * part. -- Richard Levitte | |
88 | */ | |
89 | OBJ_NAME_cleanup(-1); | |
90 | ||
91 | EVP_PBE_cleanup(); | |
0f113f3e MC |
92 | OBJ_sigid_free(); |
93 | } | |
94 | ||
95 | struct doall_cipher { | |
96 | void *arg; | |
97 | void (*fn) (const EVP_CIPHER *ciph, | |
98 | const char *from, const char *to, void *arg); | |
99 | }; | |
5ba4bf35 DSH |
100 | |
101 | static void do_all_cipher_fn(const OBJ_NAME *nm, void *arg) | |
0f113f3e MC |
102 | { |
103 | struct doall_cipher *dc = arg; | |
104 | if (nm->alias) | |
105 | dc->fn(NULL, nm->name, nm->data, dc->arg); | |
106 | else | |
107 | dc->fn((const EVP_CIPHER *)nm->data, nm->name, NULL, dc->arg); | |
108 | } | |
109 | ||
110 | void EVP_CIPHER_do_all(void (*fn) (const EVP_CIPHER *ciph, | |
111 | const char *from, const char *to, void *x), | |
112 | void *arg) | |
113 | { | |
114 | struct doall_cipher dc; | |
7b9f8f7f | 115 | |
0fc32b07 | 116 | /* Ignore errors */ |
f672aee4 | 117 | OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS, NULL); |
7b9f8f7f | 118 | |
0f113f3e MC |
119 | dc.fn = fn; |
120 | dc.arg = arg; | |
121 | OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, do_all_cipher_fn, &dc); | |
122 | } | |
123 | ||
124 | void EVP_CIPHER_do_all_sorted(void (*fn) (const EVP_CIPHER *ciph, | |
125 | const char *from, const char *to, | |
126 | void *x), void *arg) | |
127 | { | |
128 | struct doall_cipher dc; | |
7b9f8f7f | 129 | |
0fc32b07 | 130 | /* Ignore errors */ |
f672aee4 | 131 | OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS, NULL); |
7b9f8f7f | 132 | |
0f113f3e MC |
133 | dc.fn = fn; |
134 | dc.arg = arg; | |
135 | OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_CIPHER_METH, do_all_cipher_fn, &dc); | |
136 | } | |
137 | ||
138 | struct doall_md { | |
139 | void *arg; | |
140 | void (*fn) (const EVP_MD *ciph, | |
141 | const char *from, const char *to, void *arg); | |
142 | }; | |
5ba4bf35 DSH |
143 | |
144 | static void do_all_md_fn(const OBJ_NAME *nm, void *arg) | |
0f113f3e MC |
145 | { |
146 | struct doall_md *dc = arg; | |
147 | if (nm->alias) | |
148 | dc->fn(NULL, nm->name, nm->data, dc->arg); | |
149 | else | |
150 | dc->fn((const EVP_MD *)nm->data, nm->name, NULL, dc->arg); | |
151 | } | |
152 | ||
153 | void EVP_MD_do_all(void (*fn) (const EVP_MD *md, | |
154 | const char *from, const char *to, void *x), | |
155 | void *arg) | |
156 | { | |
157 | struct doall_md dc; | |
7b9f8f7f | 158 | |
0fc32b07 | 159 | /* Ignore errors */ |
f672aee4 | 160 | OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_DIGESTS, NULL); |
7b9f8f7f | 161 | |
0f113f3e MC |
162 | dc.fn = fn; |
163 | dc.arg = arg; | |
164 | OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, do_all_md_fn, &dc); | |
165 | } | |
166 | ||
167 | void EVP_MD_do_all_sorted(void (*fn) (const EVP_MD *md, | |
168 | const char *from, const char *to, | |
169 | void *x), void *arg) | |
170 | { | |
171 | struct doall_md dc; | |
7b9f8f7f | 172 | |
f672aee4 | 173 | OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_DIGESTS, NULL); |
7b9f8f7f | 174 | |
0f113f3e MC |
175 | dc.fn = fn; |
176 | dc.arg = arg; | |
177 | OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_MD_METH, do_all_md_fn, &dc); | |
178 | } |