118316Swollman/* crypto/evp/names.c */
218316Swollman/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
318316Swollman * All rights reserved.
418316Swollman *
518316Swollman * This package is an SSL implementation written
618316Swollman * by Eric Young (eay@cryptsoft.com).
718316Swollman * The implementation was written so as to conform with Netscapes SSL.
818316Swollman *
918316Swollman * This library is free for commercial and non-commercial use as long as
1018316Swollman * the following conditions are aheared to.  The following conditions
1118316Swollman * apply to all code found in this distribution, be it the RC4, RSA,
1218316Swollman * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1318316Swollman * included with this distribution is covered by the same copyright terms
1418316Swollman * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1518316Swollman *
1618316Swollman * Copyright remains Eric Young's, and as such any Copyright notices in
1718316Swollman * the code are not to be removed.
1818316Swollman * If this package is used in a product, Eric Young should be given attribution
1918316Swollman * as the author of the parts of the library used.
2018316Swollman * This can be in the form of a textual message at program startup or
2118316Swollman * in documentation (online or textual) provided with the package.
2218316Swollman *
2318316Swollman * Redistribution and use in source and binary forms, with or without
2418316Swollman * modification, are permitted provided that the following conditions
2518316Swollman * are met:
2618316Swollman * 1. Redistributions of source code must retain the copyright
2718316Swollman *    notice, this list of conditions and the following disclaimer.
2846303Smarkm * 2. Redistributions in binary form must reproduce the above copyright
2950476Speter *    notice, this list of conditions and the following disclaimer in the
3018316Swollman *    documentation and/or other materials provided with the distribution.
3118316Swollman * 3. All advertising materials mentioning features or use of this software
3246303Smarkm *    must display the following acknowledgement:
3318316Swollman *    "This product includes cryptographic software written by
3446303Smarkm *     Eric Young (eay@cryptsoft.com)"
3518316Swollman *    The word 'cryptographic' can be left out if the rouines from the library
3618316Swollman *    being used are not cryptographic related :-).
3718316Swollman * 4. If you include any Windows specific code (or a derivative thereof) from
3818316Swollman *    the apps directory (application code) you must include an acknowledgement:
3918316Swollman *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4018316Swollman *
4146303Smarkm * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4232502Scharnier * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4346303Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4418316Swollman * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4518316Swollman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4618316Swollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4718316Swollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4818316Swollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4918316Swollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5018316Swollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5118316Swollman * SUCH DAMAGE.
52126250Sbms *
53126250Sbms * The licence and distribution terms for any publically available version or
54126250Sbms * derivative of this code cannot be changed.  i.e. this code cannot simply be
5546303Smarkm * copied and put under another distribution licence
56126250Sbms * [including the GNU Public Licence.]
57126250Sbms */
58126250Sbms
59126250Sbms#include <stdio.h>
60126250Sbms#include "cryptlib.h"
61126250Sbms#include <openssl/evp.h>
62126250Sbms#include <openssl/objects.h>
63126250Sbms#include <openssl/x509.h>
64126250Sbms
65126250Sbmsint EVP_add_cipher(const EVP_CIPHER *c)
66126250Sbms	{
67126250Sbms	int r;
68126250Sbms
69126250Sbms	if (c == NULL) return 0;
7046303Smarkm
7118316Swollman	OPENSSL_init();
7218316Swollman
7318316Swollman	r=OBJ_NAME_add(OBJ_nid2sn(c->nid),OBJ_NAME_TYPE_CIPHER_METH,(const char *)c);
7418316Swollman	if (r == 0) return(0);
75126250Sbms	check_defer(c->nid);
76126250Sbms	r=OBJ_NAME_add(OBJ_nid2ln(c->nid),OBJ_NAME_TYPE_CIPHER_METH,(const char *)c);
77126250Sbms	return(r);
7846303Smarkm	}
7946303Smarkm
8046303Smarkm
8146303Smarkmint EVP_add_digest(const EVP_MD *md)
8246303Smarkm	{
8346303Smarkm	int r;
8446303Smarkm	const char *name;
8546303Smarkm	OPENSSL_init();
8646303Smarkm
87126250Sbms	name=OBJ_nid2sn(md->type);
8846303Smarkm	r=OBJ_NAME_add(name,OBJ_NAME_TYPE_MD_METH,(const char *)md);
8946303Smarkm	if (r == 0) return(0);
9018316Swollman	check_defer(md->type);
9118316Swollman	r=OBJ_NAME_add(OBJ_nid2ln(md->type),OBJ_NAME_TYPE_MD_METH,(const char *)md);
9218316Swollman	if (r == 0) return(0);
9346303Smarkm
9418316Swollman	if (md->pkey_type && md->type != md->pkey_type)
9546303Smarkm		{
9646303Smarkm		r=OBJ_NAME_add(OBJ_nid2sn(md->pkey_type),
9718316Swollman			OBJ_NAME_TYPE_MD_METH|OBJ_NAME_ALIAS,name);
9818316Swollman		if (r == 0) return(0);
9918316Swollman		check_defer(md->pkey_type);
10018316Swollman		r=OBJ_NAME_add(OBJ_nid2ln(md->pkey_type),
10118316Swollman			OBJ_NAME_TYPE_MD_METH|OBJ_NAME_ALIAS,name);
10218316Swollman		}
10318316Swollman	return(r);
10418316Swollman	}
10518316Swollman
10618316Swollmanconst EVP_CIPHER *EVP_get_cipherbyname(const char *name)
10718316Swollman	{
10818316Swollman	const EVP_CIPHER *cp;
10918316Swollman
11018316Swollman	cp=(const EVP_CIPHER *)OBJ_NAME_get(name,OBJ_NAME_TYPE_CIPHER_METH);
11118316Swollman	return(cp);
11218316Swollman	}
11318316Swollman
11418316Swollmanconst EVP_MD *EVP_get_digestbyname(const char *name)
11519880Swollman	{
11619880Swollman	const EVP_MD *cp;
11719880Swollman
11819880Swollman	cp=(const EVP_MD *)OBJ_NAME_get(name,OBJ_NAME_TYPE_MD_METH);
11918316Swollman	return(cp);
12018316Swollman	}
12118316Swollman
12246303Smarkmvoid EVP_cleanup(void)
12346303Smarkm	{
12446303Smarkm	OBJ_NAME_cleanup(OBJ_NAME_TYPE_CIPHER_METH);
12518316Swollman	OBJ_NAME_cleanup(OBJ_NAME_TYPE_MD_METH);
12646303Smarkm	/* The above calls will only clean out the contents of the name
12746303Smarkm	   hash table, but not the hash table itself.  The following line
12846303Smarkm	   does that part.  -- Richard Levitte */
12918316Swollman	OBJ_NAME_cleanup(-1);
13018316Swollman
13146303Smarkm	EVP_PBE_cleanup();
13237908Scharnier	if (obj_cleanup_defer == 2)
13318316Swollman		{
13446303Smarkm		obj_cleanup_defer = 0;
13537908Scharnier		OBJ_cleanup();
13618316Swollman		}
13718316Swollman	OBJ_sigid_free();
13818316Swollman	}
13918316Swollman
14019880Swollmanstruct doall_cipher
14146303Smarkm	{
14218316Swollman	void *arg;
14318316Swollman	void (*fn)(const EVP_CIPHER *ciph,
14418316Swollman			const char *from, const char *to, void *arg);
14518316Swollman	};
14618316Swollman
14746303Smarkmstatic void do_all_cipher_fn(const OBJ_NAME *nm, void *arg)
14824359Simp	{
14918316Swollman	struct doall_cipher *dc = arg;
15018316Swollman	if (nm->alias)
15118316Swollman		dc->fn(NULL, nm->name, nm->data, dc->arg);
15218316Swollman	else
15318316Swollman		dc->fn((const EVP_CIPHER *)nm->data, nm->name, NULL, dc->arg);
15418316Swollman	}
15518316Swollman
15618316Swollmanvoid EVP_CIPHER_do_all(void (*fn)(const EVP_CIPHER *ciph,
15718316Swollman		const char *from, const char *to, void *x), void *arg)
15818316Swollman	{
15918316Swollman	struct doall_cipher dc;
16018316Swollman	dc.fn = fn;
16118316Swollman	dc.arg = arg;
16218316Swollman	OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, do_all_cipher_fn, &dc);
16318316Swollman	}
16418316Swollman
16518316Swollmanvoid EVP_CIPHER_do_all_sorted(void (*fn)(const EVP_CIPHER *ciph,
16618316Swollman		const char *from, const char *to, void *x), void *arg)
16718316Swollman	{
16818316Swollman	struct doall_cipher dc;
16937908Scharnier	dc.fn = fn;
17018316Swollman	dc.arg = arg;
17118316Swollman	OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_CIPHER_METH, do_all_cipher_fn,&dc);
17218316Swollman	}
17318316Swollman
17418316Swollmanstruct doall_md
17537908Scharnier	{
17618316Swollman	void *arg;
17718316Swollman	void (*fn)(const EVP_MD *ciph,
17818316Swollman			const char *from, const char *to, void *arg);
17946303Smarkm	};
18046303Smarkm
18146303Smarkmstatic void do_all_md_fn(const OBJ_NAME *nm, void *arg)
18246303Smarkm	{
18346303Smarkm	struct doall_md *dc = arg;
18446303Smarkm	if (nm->alias)
18546303Smarkm		dc->fn(NULL, nm->name, nm->data, dc->arg);
18646303Smarkm	else
18718316Swollman		dc->fn((const EVP_MD *)nm->data, nm->name, NULL, dc->arg);
18818316Swollman	}
18918316Swollman
19018316Swollmanvoid EVP_MD_do_all(void (*fn)(const EVP_MD *md,
19118316Swollman		const char *from, const char *to, void *x), void *arg)
19218316Swollman	{
19318316Swollman	struct doall_md dc;
19418316Swollman	dc.fn = fn;
19518316Swollman	dc.arg = arg;
19618316Swollman	OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, do_all_md_fn, &dc);
19746303Smarkm	}
19846303Smarkm
19946303Smarkmvoid EVP_MD_do_all_sorted(void (*fn)(const EVP_MD *md,
20046303Smarkm		const char *from, const char *to, void *x), void *arg)
20146303Smarkm	{
20246303Smarkm	struct doall_md dc;
20318316Swollman	dc.fn = fn;
20446303Smarkm	dc.arg = arg;
20518316Swollman	OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_MD_METH, do_all_md_fn, &dc);
20646303Smarkm	}
20718316Swollman