xref: /onnv-gate/usr/src/common/openssl/crypto/engine/eng_list.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* crypto/engine/eng_list.c */
20Sstevel@tonic-gate /* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
30Sstevel@tonic-gate  * project 2000.
40Sstevel@tonic-gate  */
50Sstevel@tonic-gate /* ====================================================================
60Sstevel@tonic-gate  * Copyright (c) 1999-2001 The OpenSSL Project.  All rights reserved.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
90Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
100Sstevel@tonic-gate  * are met:
110Sstevel@tonic-gate  *
120Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
130Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
140Sstevel@tonic-gate  *
150Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
160Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in
170Sstevel@tonic-gate  *    the documentation and/or other materials provided with the
180Sstevel@tonic-gate  *    distribution.
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this
210Sstevel@tonic-gate  *    software must display the following acknowledgment:
220Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
230Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
240Sstevel@tonic-gate  *
250Sstevel@tonic-gate  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
260Sstevel@tonic-gate  *    endorse or promote products derived from this software without
270Sstevel@tonic-gate  *    prior written permission. For written permission, please contact
280Sstevel@tonic-gate  *    licensing@OpenSSL.org.
290Sstevel@tonic-gate  *
300Sstevel@tonic-gate  * 5. Products derived from this software may not be called "OpenSSL"
310Sstevel@tonic-gate  *    nor may "OpenSSL" appear in their names without prior written
320Sstevel@tonic-gate  *    permission of the OpenSSL Project.
330Sstevel@tonic-gate  *
340Sstevel@tonic-gate  * 6. Redistributions of any form whatsoever must retain the following
350Sstevel@tonic-gate  *    acknowledgment:
360Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
370Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
380Sstevel@tonic-gate  *
390Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
400Sstevel@tonic-gate  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
410Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
420Sstevel@tonic-gate  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
430Sstevel@tonic-gate  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
440Sstevel@tonic-gate  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
450Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
460Sstevel@tonic-gate  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
470Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
480Sstevel@tonic-gate  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
490Sstevel@tonic-gate  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
500Sstevel@tonic-gate  * OF THE POSSIBILITY OF SUCH DAMAGE.
510Sstevel@tonic-gate  * ====================================================================
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * This product includes cryptographic software written by Eric Young
540Sstevel@tonic-gate  * (eay@cryptsoft.com).  This product includes software written by Tim
550Sstevel@tonic-gate  * Hudson (tjh@cryptsoft.com).
560Sstevel@tonic-gate  *
570Sstevel@tonic-gate  */
58*2139Sjp161948 /* ====================================================================
59*2139Sjp161948  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
60*2139Sjp161948  * ECDH support in OpenSSL originally developed by
61*2139Sjp161948  * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
62*2139Sjp161948  */
630Sstevel@tonic-gate 
640Sstevel@tonic-gate #include "eng_int.h"
650Sstevel@tonic-gate 
660Sstevel@tonic-gate /* The linked-list of pointers to engine types. engine_list_head
670Sstevel@tonic-gate  * incorporates an implicit structural reference but engine_list_tail
680Sstevel@tonic-gate  * does not - the latter is a computational niceity and only points
690Sstevel@tonic-gate  * to something that is already pointed to by its predecessor in the
700Sstevel@tonic-gate  * list (or engine_list_head itself). In the same way, the use of the
710Sstevel@tonic-gate  * "prev" pointer in each ENGINE is to save excessive list iteration,
720Sstevel@tonic-gate  * it doesn't correspond to an extra structural reference. Hence,
730Sstevel@tonic-gate  * engine_list_head, and each non-null "next" pointer account for
740Sstevel@tonic-gate  * the list itself assuming exactly 1 structural reference on each
750Sstevel@tonic-gate  * list member. */
760Sstevel@tonic-gate static ENGINE *engine_list_head = NULL;
770Sstevel@tonic-gate static ENGINE *engine_list_tail = NULL;
780Sstevel@tonic-gate 
790Sstevel@tonic-gate /* This cleanup function is only needed internally. If it should be called, we
800Sstevel@tonic-gate  * register it with the "ENGINE_cleanup()" stack to be called during cleanup. */
810Sstevel@tonic-gate 
engine_list_cleanup(void)820Sstevel@tonic-gate static void engine_list_cleanup(void)
830Sstevel@tonic-gate 	{
840Sstevel@tonic-gate 	ENGINE *iterator = engine_list_head;
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	while(iterator != NULL)
870Sstevel@tonic-gate 		{
880Sstevel@tonic-gate 		ENGINE_remove(iterator);
890Sstevel@tonic-gate 		iterator = engine_list_head;
900Sstevel@tonic-gate 		}
910Sstevel@tonic-gate 	return;
920Sstevel@tonic-gate 	}
930Sstevel@tonic-gate 
940Sstevel@tonic-gate /* These static functions starting with a lower case "engine_" always
950Sstevel@tonic-gate  * take place when CRYPTO_LOCK_ENGINE has been locked up. */
engine_list_add(ENGINE * e)960Sstevel@tonic-gate static int engine_list_add(ENGINE *e)
970Sstevel@tonic-gate 	{
980Sstevel@tonic-gate 	int conflict = 0;
990Sstevel@tonic-gate 	ENGINE *iterator = NULL;
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	if(e == NULL)
1020Sstevel@tonic-gate 		{
1030Sstevel@tonic-gate 		ENGINEerr(ENGINE_F_ENGINE_LIST_ADD,
1040Sstevel@tonic-gate 			ERR_R_PASSED_NULL_PARAMETER);
1050Sstevel@tonic-gate 		return 0;
1060Sstevel@tonic-gate 		}
1070Sstevel@tonic-gate 	iterator = engine_list_head;
1080Sstevel@tonic-gate 	while(iterator && !conflict)
1090Sstevel@tonic-gate 		{
1100Sstevel@tonic-gate 		conflict = (strcmp(iterator->id, e->id) == 0);
1110Sstevel@tonic-gate 		iterator = iterator->next;
1120Sstevel@tonic-gate 		}
1130Sstevel@tonic-gate 	if(conflict)
1140Sstevel@tonic-gate 		{
1150Sstevel@tonic-gate 		ENGINEerr(ENGINE_F_ENGINE_LIST_ADD,
1160Sstevel@tonic-gate 			ENGINE_R_CONFLICTING_ENGINE_ID);
1170Sstevel@tonic-gate 		return 0;
1180Sstevel@tonic-gate 		}
1190Sstevel@tonic-gate 	if(engine_list_head == NULL)
1200Sstevel@tonic-gate 		{
1210Sstevel@tonic-gate 		/* We are adding to an empty list. */
1220Sstevel@tonic-gate 		if(engine_list_tail)
1230Sstevel@tonic-gate 			{
1240Sstevel@tonic-gate 			ENGINEerr(ENGINE_F_ENGINE_LIST_ADD,
1250Sstevel@tonic-gate 				ENGINE_R_INTERNAL_LIST_ERROR);
1260Sstevel@tonic-gate 			return 0;
1270Sstevel@tonic-gate 			}
1280Sstevel@tonic-gate 		engine_list_head = e;
1290Sstevel@tonic-gate 		e->prev = NULL;
1300Sstevel@tonic-gate 		/* The first time the list allocates, we should register the
1310Sstevel@tonic-gate 		 * cleanup. */
1320Sstevel@tonic-gate 		engine_cleanup_add_last(engine_list_cleanup);
1330Sstevel@tonic-gate 		}
1340Sstevel@tonic-gate 	else
1350Sstevel@tonic-gate 		{
1360Sstevel@tonic-gate 		/* We are adding to the tail of an existing list. */
1370Sstevel@tonic-gate 		if((engine_list_tail == NULL) ||
1380Sstevel@tonic-gate 				(engine_list_tail->next != NULL))
1390Sstevel@tonic-gate 			{
1400Sstevel@tonic-gate 			ENGINEerr(ENGINE_F_ENGINE_LIST_ADD,
1410Sstevel@tonic-gate 				ENGINE_R_INTERNAL_LIST_ERROR);
1420Sstevel@tonic-gate 			return 0;
1430Sstevel@tonic-gate 			}
1440Sstevel@tonic-gate 		engine_list_tail->next = e;
1450Sstevel@tonic-gate 		e->prev = engine_list_tail;
1460Sstevel@tonic-gate 		}
1470Sstevel@tonic-gate 	/* Having the engine in the list assumes a structural
1480Sstevel@tonic-gate 	 * reference. */
1490Sstevel@tonic-gate 	e->struct_ref++;
1500Sstevel@tonic-gate 	engine_ref_debug(e, 0, 1)
1510Sstevel@tonic-gate 	/* However it came to be, e is the last item in the list. */
1520Sstevel@tonic-gate 	engine_list_tail = e;
1530Sstevel@tonic-gate 	e->next = NULL;
1540Sstevel@tonic-gate 	return 1;
1550Sstevel@tonic-gate 	}
1560Sstevel@tonic-gate 
engine_list_remove(ENGINE * e)1570Sstevel@tonic-gate static int engine_list_remove(ENGINE *e)
1580Sstevel@tonic-gate 	{
1590Sstevel@tonic-gate 	ENGINE *iterator;
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	if(e == NULL)
1620Sstevel@tonic-gate 		{
1630Sstevel@tonic-gate 		ENGINEerr(ENGINE_F_ENGINE_LIST_REMOVE,
1640Sstevel@tonic-gate 			ERR_R_PASSED_NULL_PARAMETER);
1650Sstevel@tonic-gate 		return 0;
1660Sstevel@tonic-gate 		}
1670Sstevel@tonic-gate 	/* We need to check that e is in our linked list! */
1680Sstevel@tonic-gate 	iterator = engine_list_head;
1690Sstevel@tonic-gate 	while(iterator && (iterator != e))
1700Sstevel@tonic-gate 		iterator = iterator->next;
1710Sstevel@tonic-gate 	if(iterator == NULL)
1720Sstevel@tonic-gate 		{
1730Sstevel@tonic-gate 		ENGINEerr(ENGINE_F_ENGINE_LIST_REMOVE,
1740Sstevel@tonic-gate 			ENGINE_R_ENGINE_IS_NOT_IN_LIST);
1750Sstevel@tonic-gate 		return 0;
1760Sstevel@tonic-gate 		}
1770Sstevel@tonic-gate 	/* un-link e from the chain. */
1780Sstevel@tonic-gate 	if(e->next)
1790Sstevel@tonic-gate 		e->next->prev = e->prev;
1800Sstevel@tonic-gate 	if(e->prev)
1810Sstevel@tonic-gate 		e->prev->next = e->next;
1820Sstevel@tonic-gate 	/* Correct our head/tail if necessary. */
1830Sstevel@tonic-gate 	if(engine_list_head == e)
1840Sstevel@tonic-gate 		engine_list_head = e->next;
1850Sstevel@tonic-gate 	if(engine_list_tail == e)
1860Sstevel@tonic-gate 		engine_list_tail = e->prev;
1870Sstevel@tonic-gate 	engine_free_util(e, 0);
1880Sstevel@tonic-gate 	return 1;
1890Sstevel@tonic-gate 	}
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate /* Get the first/last "ENGINE" type available. */
ENGINE_get_first(void)1920Sstevel@tonic-gate ENGINE *ENGINE_get_first(void)
1930Sstevel@tonic-gate 	{
1940Sstevel@tonic-gate 	ENGINE *ret;
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 	CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
1970Sstevel@tonic-gate 	ret = engine_list_head;
1980Sstevel@tonic-gate 	if(ret)
1990Sstevel@tonic-gate 		{
2000Sstevel@tonic-gate 		ret->struct_ref++;
2010Sstevel@tonic-gate 		engine_ref_debug(ret, 0, 1)
2020Sstevel@tonic-gate 		}
2030Sstevel@tonic-gate 	CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
2040Sstevel@tonic-gate 	return ret;
2050Sstevel@tonic-gate 	}
2060Sstevel@tonic-gate 
ENGINE_get_last(void)2070Sstevel@tonic-gate ENGINE *ENGINE_get_last(void)
2080Sstevel@tonic-gate 	{
2090Sstevel@tonic-gate 	ENGINE *ret;
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
2120Sstevel@tonic-gate 	ret = engine_list_tail;
2130Sstevel@tonic-gate 	if(ret)
2140Sstevel@tonic-gate 		{
2150Sstevel@tonic-gate 		ret->struct_ref++;
2160Sstevel@tonic-gate 		engine_ref_debug(ret, 0, 1)
2170Sstevel@tonic-gate 		}
2180Sstevel@tonic-gate 	CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
2190Sstevel@tonic-gate 	return ret;
2200Sstevel@tonic-gate 	}
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate /* Iterate to the next/previous "ENGINE" type (NULL = end of the list). */
ENGINE_get_next(ENGINE * e)2230Sstevel@tonic-gate ENGINE *ENGINE_get_next(ENGINE *e)
2240Sstevel@tonic-gate 	{
2250Sstevel@tonic-gate 	ENGINE *ret = NULL;
2260Sstevel@tonic-gate 	if(e == NULL)
2270Sstevel@tonic-gate 		{
2280Sstevel@tonic-gate 		ENGINEerr(ENGINE_F_ENGINE_GET_NEXT,
2290Sstevel@tonic-gate 			ERR_R_PASSED_NULL_PARAMETER);
2300Sstevel@tonic-gate 		return 0;
2310Sstevel@tonic-gate 		}
2320Sstevel@tonic-gate 	CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
2330Sstevel@tonic-gate 	ret = e->next;
2340Sstevel@tonic-gate 	if(ret)
2350Sstevel@tonic-gate 		{
2360Sstevel@tonic-gate 		/* Return a valid structural refernce to the next ENGINE */
2370Sstevel@tonic-gate 		ret->struct_ref++;
2380Sstevel@tonic-gate 		engine_ref_debug(ret, 0, 1)
2390Sstevel@tonic-gate 		}
2400Sstevel@tonic-gate 	CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
2410Sstevel@tonic-gate 	/* Release the structural reference to the previous ENGINE */
2420Sstevel@tonic-gate 	ENGINE_free(e);
2430Sstevel@tonic-gate 	return ret;
2440Sstevel@tonic-gate 	}
2450Sstevel@tonic-gate 
ENGINE_get_prev(ENGINE * e)2460Sstevel@tonic-gate ENGINE *ENGINE_get_prev(ENGINE *e)
2470Sstevel@tonic-gate 	{
2480Sstevel@tonic-gate 	ENGINE *ret = NULL;
2490Sstevel@tonic-gate 	if(e == NULL)
2500Sstevel@tonic-gate 		{
2510Sstevel@tonic-gate 		ENGINEerr(ENGINE_F_ENGINE_GET_PREV,
2520Sstevel@tonic-gate 			ERR_R_PASSED_NULL_PARAMETER);
2530Sstevel@tonic-gate 		return 0;
2540Sstevel@tonic-gate 		}
2550Sstevel@tonic-gate 	CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
2560Sstevel@tonic-gate 	ret = e->prev;
2570Sstevel@tonic-gate 	if(ret)
2580Sstevel@tonic-gate 		{
2590Sstevel@tonic-gate 		/* Return a valid structural reference to the next ENGINE */
2600Sstevel@tonic-gate 		ret->struct_ref++;
2610Sstevel@tonic-gate 		engine_ref_debug(ret, 0, 1)
2620Sstevel@tonic-gate 		}
2630Sstevel@tonic-gate 	CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
2640Sstevel@tonic-gate 	/* Release the structural reference to the previous ENGINE */
2650Sstevel@tonic-gate 	ENGINE_free(e);
2660Sstevel@tonic-gate 	return ret;
2670Sstevel@tonic-gate 	}
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate /* Add another "ENGINE" type into the list. */
ENGINE_add(ENGINE * e)2700Sstevel@tonic-gate int ENGINE_add(ENGINE *e)
2710Sstevel@tonic-gate 	{
2720Sstevel@tonic-gate 	int to_return = 1;
2730Sstevel@tonic-gate 	if(e == NULL)
2740Sstevel@tonic-gate 		{
2750Sstevel@tonic-gate 		ENGINEerr(ENGINE_F_ENGINE_ADD,
2760Sstevel@tonic-gate 			ERR_R_PASSED_NULL_PARAMETER);
2770Sstevel@tonic-gate 		return 0;
2780Sstevel@tonic-gate 		}
2790Sstevel@tonic-gate 	if((e->id == NULL) || (e->name == NULL))
2800Sstevel@tonic-gate 		{
2810Sstevel@tonic-gate 		ENGINEerr(ENGINE_F_ENGINE_ADD,
2820Sstevel@tonic-gate 			ENGINE_R_ID_OR_NAME_MISSING);
2830Sstevel@tonic-gate 		}
2840Sstevel@tonic-gate 	CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
2850Sstevel@tonic-gate 	if(!engine_list_add(e))
2860Sstevel@tonic-gate 		{
2870Sstevel@tonic-gate 		ENGINEerr(ENGINE_F_ENGINE_ADD,
2880Sstevel@tonic-gate 			ENGINE_R_INTERNAL_LIST_ERROR);
2890Sstevel@tonic-gate 		to_return = 0;
2900Sstevel@tonic-gate 		}
2910Sstevel@tonic-gate 	CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
2920Sstevel@tonic-gate 	return to_return;
2930Sstevel@tonic-gate 	}
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate /* Remove an existing "ENGINE" type from the array. */
ENGINE_remove(ENGINE * e)2960Sstevel@tonic-gate int ENGINE_remove(ENGINE *e)
2970Sstevel@tonic-gate 	{
2980Sstevel@tonic-gate 	int to_return = 1;
2990Sstevel@tonic-gate 	if(e == NULL)
3000Sstevel@tonic-gate 		{
3010Sstevel@tonic-gate 		ENGINEerr(ENGINE_F_ENGINE_REMOVE,
3020Sstevel@tonic-gate 			ERR_R_PASSED_NULL_PARAMETER);
3030Sstevel@tonic-gate 		return 0;
3040Sstevel@tonic-gate 		}
3050Sstevel@tonic-gate 	CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
3060Sstevel@tonic-gate 	if(!engine_list_remove(e))
3070Sstevel@tonic-gate 		{
3080Sstevel@tonic-gate 		ENGINEerr(ENGINE_F_ENGINE_REMOVE,
3090Sstevel@tonic-gate 			ENGINE_R_INTERNAL_LIST_ERROR);
3100Sstevel@tonic-gate 		to_return = 0;
3110Sstevel@tonic-gate 		}
3120Sstevel@tonic-gate 	CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
3130Sstevel@tonic-gate 	return to_return;
3140Sstevel@tonic-gate 	}
3150Sstevel@tonic-gate 
engine_cpy(ENGINE * dest,const ENGINE * src)3160Sstevel@tonic-gate static void engine_cpy(ENGINE *dest, const ENGINE *src)
3170Sstevel@tonic-gate 	{
3180Sstevel@tonic-gate 	dest->id = src->id;
3190Sstevel@tonic-gate 	dest->name = src->name;
3200Sstevel@tonic-gate #ifndef OPENSSL_NO_RSA
3210Sstevel@tonic-gate 	dest->rsa_meth = src->rsa_meth;
3220Sstevel@tonic-gate #endif
3230Sstevel@tonic-gate #ifndef OPENSSL_NO_DSA
3240Sstevel@tonic-gate 	dest->dsa_meth = src->dsa_meth;
3250Sstevel@tonic-gate #endif
3260Sstevel@tonic-gate #ifndef OPENSSL_NO_DH
3270Sstevel@tonic-gate 	dest->dh_meth = src->dh_meth;
3280Sstevel@tonic-gate #endif
329*2139Sjp161948 #ifndef OPENSSL_NO_ECDH
330*2139Sjp161948 	dest->ecdh_meth = src->ecdh_meth;
331*2139Sjp161948 #endif
332*2139Sjp161948 #ifndef OPENSSL_NO_ECDSA
333*2139Sjp161948 	dest->ecdsa_meth = src->ecdsa_meth;
334*2139Sjp161948 #endif
3350Sstevel@tonic-gate 	dest->rand_meth = src->rand_meth;
336*2139Sjp161948 	dest->store_meth = src->store_meth;
3370Sstevel@tonic-gate 	dest->ciphers = src->ciphers;
3380Sstevel@tonic-gate 	dest->digests = src->digests;
3390Sstevel@tonic-gate 	dest->destroy = src->destroy;
3400Sstevel@tonic-gate 	dest->init = src->init;
3410Sstevel@tonic-gate 	dest->finish = src->finish;
3420Sstevel@tonic-gate 	dest->ctrl = src->ctrl;
3430Sstevel@tonic-gate 	dest->load_privkey = src->load_privkey;
3440Sstevel@tonic-gate 	dest->load_pubkey = src->load_pubkey;
3450Sstevel@tonic-gate 	dest->cmd_defns = src->cmd_defns;
3460Sstevel@tonic-gate 	dest->flags = src->flags;
3470Sstevel@tonic-gate 	}
3480Sstevel@tonic-gate 
ENGINE_by_id(const char * id)3490Sstevel@tonic-gate ENGINE *ENGINE_by_id(const char *id)
3500Sstevel@tonic-gate 	{
3510Sstevel@tonic-gate 	ENGINE *iterator;
352*2139Sjp161948 	char *load_dir = NULL;
3530Sstevel@tonic-gate 	if(id == NULL)
3540Sstevel@tonic-gate 		{
3550Sstevel@tonic-gate 		ENGINEerr(ENGINE_F_ENGINE_BY_ID,
3560Sstevel@tonic-gate 			ERR_R_PASSED_NULL_PARAMETER);
3570Sstevel@tonic-gate 		return NULL;
3580Sstevel@tonic-gate 		}
3590Sstevel@tonic-gate 	CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
3600Sstevel@tonic-gate 	iterator = engine_list_head;
3610Sstevel@tonic-gate 	while(iterator && (strcmp(id, iterator->id) != 0))
3620Sstevel@tonic-gate 		iterator = iterator->next;
3630Sstevel@tonic-gate 	if(iterator)
3640Sstevel@tonic-gate 		{
3650Sstevel@tonic-gate 		/* We need to return a structural reference. If this is an
3660Sstevel@tonic-gate 		 * ENGINE type that returns copies, make a duplicate - otherwise
3670Sstevel@tonic-gate 		 * increment the existing ENGINE's reference count. */
3680Sstevel@tonic-gate 		if(iterator->flags & ENGINE_FLAGS_BY_ID_COPY)
3690Sstevel@tonic-gate 			{
3700Sstevel@tonic-gate 			ENGINE *cp = ENGINE_new();
3710Sstevel@tonic-gate 			if(!cp)
3720Sstevel@tonic-gate 				iterator = NULL;
3730Sstevel@tonic-gate 			else
3740Sstevel@tonic-gate 				{
3750Sstevel@tonic-gate 				engine_cpy(cp, iterator);
3760Sstevel@tonic-gate 				iterator = cp;
3770Sstevel@tonic-gate 				}
3780Sstevel@tonic-gate 			}
3790Sstevel@tonic-gate 		else
3800Sstevel@tonic-gate 			{
3810Sstevel@tonic-gate 			iterator->struct_ref++;
3820Sstevel@tonic-gate 			engine_ref_debug(iterator, 0, 1)
3830Sstevel@tonic-gate 			}
3840Sstevel@tonic-gate 		}
3850Sstevel@tonic-gate 	CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
386*2139Sjp161948 #if 0
3870Sstevel@tonic-gate 	if(iterator == NULL)
3880Sstevel@tonic-gate 		{
3890Sstevel@tonic-gate 		ENGINEerr(ENGINE_F_ENGINE_BY_ID,
3900Sstevel@tonic-gate 			ENGINE_R_NO_SUCH_ENGINE);
3910Sstevel@tonic-gate 		ERR_add_error_data(2, "id=", id);
3920Sstevel@tonic-gate 		}
3930Sstevel@tonic-gate 	return iterator;
394*2139Sjp161948 #else
395*2139Sjp161948 	/* EEK! Experimental code starts */
396*2139Sjp161948 	if(iterator) return iterator;
397*2139Sjp161948 	/* Prevent infinite recusrion if we're looking for the dynamic engine. */
398*2139Sjp161948 	if (strcmp(id, "dynamic"))
399*2139Sjp161948 		{
400*2139Sjp161948 #ifdef OPENSSL_SYS_VMS
401*2139Sjp161948 		if((load_dir = getenv("OPENSSL_ENGINES")) == 0) load_dir = "SSLROOT:[ENGINES]";
402*2139Sjp161948 #else
403*2139Sjp161948 		if((load_dir = getenv("OPENSSL_ENGINES")) == 0) load_dir = ENGINESDIR;
404*2139Sjp161948 #endif
405*2139Sjp161948 		iterator = ENGINE_by_id("dynamic");
406*2139Sjp161948 		if(!iterator || !ENGINE_ctrl_cmd_string(iterator, "ID", id, 0) ||
407*2139Sjp161948 				!ENGINE_ctrl_cmd_string(iterator, "DIR_LOAD", "2", 0) ||
408*2139Sjp161948 				!ENGINE_ctrl_cmd_string(iterator, "DIR_ADD",
409*2139Sjp161948 					load_dir, 0) ||
410*2139Sjp161948 				!ENGINE_ctrl_cmd_string(iterator, "LOAD", NULL, 0))
411*2139Sjp161948 				goto notfound;
412*2139Sjp161948 		return iterator;
413*2139Sjp161948 		}
414*2139Sjp161948 notfound:
415*2139Sjp161948 	ENGINEerr(ENGINE_F_ENGINE_BY_ID,ENGINE_R_NO_SUCH_ENGINE);
416*2139Sjp161948 	ERR_add_error_data(2, "id=", id);
417*2139Sjp161948 	return NULL;
418*2139Sjp161948 	/* EEK! Experimental code ends */
419*2139Sjp161948 #endif
4200Sstevel@tonic-gate 	}
4210Sstevel@tonic-gate 
ENGINE_up_ref(ENGINE * e)4220Sstevel@tonic-gate int ENGINE_up_ref(ENGINE *e)
4230Sstevel@tonic-gate 	{
4240Sstevel@tonic-gate 	if (e == NULL)
4250Sstevel@tonic-gate 		{
4260Sstevel@tonic-gate 		ENGINEerr(ENGINE_F_ENGINE_UP_REF,ERR_R_PASSED_NULL_PARAMETER);
4270Sstevel@tonic-gate 		return 0;
4280Sstevel@tonic-gate 		}
4290Sstevel@tonic-gate 	CRYPTO_add(&e->struct_ref,1,CRYPTO_LOCK_ENGINE);
4300Sstevel@tonic-gate 	return 1;
4310Sstevel@tonic-gate 	}
432