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