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