2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
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
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"
19 int EVP_add_cipher(const EVP_CIPHER
*c
)
26 r
= OBJ_NAME_add(OBJ_nid2sn(c
->nid
), OBJ_NAME_TYPE_CIPHER_METH
,
30 r
= OBJ_NAME_add(OBJ_nid2ln(c
->nid
), OBJ_NAME_TYPE_CIPHER_METH
,
35 int EVP_add_digest(const EVP_MD
*md
)
40 name
= OBJ_nid2sn(md
->type
);
41 r
= OBJ_NAME_add(name
, OBJ_NAME_TYPE_MD_METH
, (const char *)md
);
44 r
= OBJ_NAME_add(OBJ_nid2ln(md
->type
), OBJ_NAME_TYPE_MD_METH
,
49 if (md
->pkey_type
&& md
->type
!= md
->pkey_type
) {
50 r
= OBJ_NAME_add(OBJ_nid2sn(md
->pkey_type
),
51 OBJ_NAME_TYPE_MD_METH
| OBJ_NAME_ALIAS
, name
);
54 r
= OBJ_NAME_add(OBJ_nid2ln(md
->pkey_type
),
55 OBJ_NAME_TYPE_MD_METH
| OBJ_NAME_ALIAS
, name
);
60 static void cipher_from_name(const char *name
, void *data
)
62 const EVP_CIPHER
**cipher
= data
;
67 *cipher
= (const EVP_CIPHER
*)OBJ_NAME_get(name
, OBJ_NAME_TYPE_CIPHER_METH
);
70 const EVP_CIPHER
*EVP_get_cipherbyname(const char *name
)
72 return evp_get_cipherbyname_ex(NULL
, name
);
75 const EVP_CIPHER
*evp_get_cipherbyname_ex(OSSL_LIB_CTX
*libctx
,
79 OSSL_NAMEMAP
*namemap
;
82 if (!OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS
, NULL
))
85 cp
= (const EVP_CIPHER
*)OBJ_NAME_get(name
, OBJ_NAME_TYPE_CIPHER_METH
);
91 * It's not in the method database, but it might be there under a different
92 * name. So we check for aliases in the EVP namemap and try all of those
96 namemap
= ossl_namemap_stored(libctx
);
97 id
= ossl_namemap_name2num(namemap
, name
);
101 ossl_namemap_doall_names(namemap
, id
, cipher_from_name
, &cp
);
106 static void digest_from_name(const char *name
, void *data
)
108 const EVP_MD
**md
= data
;
113 *md
= (const EVP_MD
*)OBJ_NAME_get(name
, OBJ_NAME_TYPE_MD_METH
);
116 const EVP_MD
*EVP_get_digestbyname(const char *name
)
118 return evp_get_digestbyname_ex(NULL
, name
);
121 const EVP_MD
*evp_get_digestbyname_ex(OSSL_LIB_CTX
*libctx
, const char *name
)
124 OSSL_NAMEMAP
*namemap
;
127 if (!OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_DIGESTS
, NULL
))
130 dp
= (const EVP_MD
*)OBJ_NAME_get(name
, OBJ_NAME_TYPE_MD_METH
);
136 * It's not in the method database, but it might be there under a different
137 * name. So we check for aliases in the EVP namemap and try all of those
141 namemap
= ossl_namemap_stored(libctx
);
142 id
= ossl_namemap_name2num(namemap
, name
);
146 ossl_namemap_doall_names(namemap
, id
, digest_from_name
, &dp
);
151 void evp_cleanup_int(void)
153 OBJ_NAME_cleanup(OBJ_NAME_TYPE_KDF_METH
);
154 OBJ_NAME_cleanup(OBJ_NAME_TYPE_CIPHER_METH
);
155 OBJ_NAME_cleanup(OBJ_NAME_TYPE_MD_METH
);
157 * The above calls will only clean out the contents of the name hash
158 * table, but not the hash table itself. The following line does that
159 * part. -- Richard Levitte
161 OBJ_NAME_cleanup(-1);
166 evp_app_cleanup_int();
169 struct doall_cipher
{
171 void (*fn
) (const EVP_CIPHER
*ciph
,
172 const char *from
, const char *to
, void *arg
);
175 static void do_all_cipher_fn(const OBJ_NAME
*nm
, void *arg
)
177 struct doall_cipher
*dc
= arg
;
179 dc
->fn(NULL
, nm
->name
, nm
->data
, dc
->arg
);
181 dc
->fn((const EVP_CIPHER
*)nm
->data
, nm
->name
, NULL
, dc
->arg
);
184 void EVP_CIPHER_do_all(void (*fn
) (const EVP_CIPHER
*ciph
,
185 const char *from
, const char *to
, void *x
),
188 struct doall_cipher dc
;
191 OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS
, NULL
);
195 OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH
, do_all_cipher_fn
, &dc
);
198 void EVP_CIPHER_do_all_sorted(void (*fn
) (const EVP_CIPHER
*ciph
,
199 const char *from
, const char *to
,
202 struct doall_cipher dc
;
205 OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS
, NULL
);
209 OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_CIPHER_METH
, do_all_cipher_fn
, &dc
);
214 void (*fn
) (const EVP_MD
*ciph
,
215 const char *from
, const char *to
, void *arg
);
218 static void do_all_md_fn(const OBJ_NAME
*nm
, void *arg
)
220 struct doall_md
*dc
= arg
;
222 dc
->fn(NULL
, nm
->name
, nm
->data
, dc
->arg
);
224 dc
->fn((const EVP_MD
*)nm
->data
, nm
->name
, NULL
, dc
->arg
);
227 void EVP_MD_do_all(void (*fn
) (const EVP_MD
*md
,
228 const char *from
, const char *to
, void *x
),
234 OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_DIGESTS
, NULL
);
238 OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH
, do_all_md_fn
, &dc
);
241 void EVP_MD_do_all_sorted(void (*fn
) (const EVP_MD
*md
,
242 const char *from
, const char *to
,
247 OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_DIGESTS
, NULL
);
251 OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_MD_METH
, do_all_md_fn
, &dc
);