xref: /openbsd/lib/libcrypto/x509/x509_vpm.c (revision 7609e5c6)
15650a0e1Sdjm /* x509_vpm.c */
282a8dcafSdjm /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
35650a0e1Sdjm  * project 2004.
45650a0e1Sdjm  */
55650a0e1Sdjm /* ====================================================================
65650a0e1Sdjm  * Copyright (c) 2004 The OpenSSL Project.  All rights reserved.
75650a0e1Sdjm  *
85650a0e1Sdjm  * Redistribution and use in source and binary forms, with or without
95650a0e1Sdjm  * modification, are permitted provided that the following conditions
105650a0e1Sdjm  * are met:
115650a0e1Sdjm  *
125650a0e1Sdjm  * 1. Redistributions of source code must retain the above copyright
135650a0e1Sdjm  *    notice, this list of conditions and the following disclaimer.
145650a0e1Sdjm  *
155650a0e1Sdjm  * 2. Redistributions in binary form must reproduce the above copyright
165650a0e1Sdjm  *    notice, this list of conditions and the following disclaimer in
175650a0e1Sdjm  *    the documentation and/or other materials provided with the
185650a0e1Sdjm  *    distribution.
195650a0e1Sdjm  *
205650a0e1Sdjm  * 3. All advertising materials mentioning features or use of this
215650a0e1Sdjm  *    software must display the following acknowledgment:
225650a0e1Sdjm  *    "This product includes software developed by the OpenSSL Project
235650a0e1Sdjm  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
245650a0e1Sdjm  *
255650a0e1Sdjm  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
265650a0e1Sdjm  *    endorse or promote products derived from this software without
275650a0e1Sdjm  *    prior written permission. For written permission, please contact
285650a0e1Sdjm  *    licensing@OpenSSL.org.
295650a0e1Sdjm  *
305650a0e1Sdjm  * 5. Products derived from this software may not be called "OpenSSL"
315650a0e1Sdjm  *    nor may "OpenSSL" appear in their names without prior written
325650a0e1Sdjm  *    permission of the OpenSSL Project.
335650a0e1Sdjm  *
345650a0e1Sdjm  * 6. Redistributions of any form whatsoever must retain the following
355650a0e1Sdjm  *    acknowledgment:
365650a0e1Sdjm  *    "This product includes software developed by the OpenSSL Project
375650a0e1Sdjm  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
385650a0e1Sdjm  *
395650a0e1Sdjm  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
405650a0e1Sdjm  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
415650a0e1Sdjm  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
425650a0e1Sdjm  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
435650a0e1Sdjm  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
445650a0e1Sdjm  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
455650a0e1Sdjm  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
465650a0e1Sdjm  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
475650a0e1Sdjm  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
485650a0e1Sdjm  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
495650a0e1Sdjm  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
505650a0e1Sdjm  * OF THE POSSIBILITY OF SUCH DAMAGE.
515650a0e1Sdjm  * ====================================================================
525650a0e1Sdjm  *
535650a0e1Sdjm  * This product includes cryptographic software written by Eric Young
545650a0e1Sdjm  * (eay@cryptsoft.com).  This product includes software written by Tim
555650a0e1Sdjm  * Hudson (tjh@cryptsoft.com).
565650a0e1Sdjm  *
575650a0e1Sdjm  */
585650a0e1Sdjm 
595650a0e1Sdjm #include <stdio.h>
605650a0e1Sdjm 
615650a0e1Sdjm #include "cryptlib.h"
625650a0e1Sdjm #include <openssl/crypto.h>
635650a0e1Sdjm #include <openssl/lhash.h>
645650a0e1Sdjm #include <openssl/buffer.h>
655650a0e1Sdjm #include <openssl/x509.h>
665650a0e1Sdjm #include <openssl/x509v3.h>
675650a0e1Sdjm 
685650a0e1Sdjm /* X509_VERIFY_PARAM functions */
695650a0e1Sdjm 
705650a0e1Sdjm static void x509_verify_param_zero(X509_VERIFY_PARAM *param)
715650a0e1Sdjm {
725650a0e1Sdjm 	if (!param)
735650a0e1Sdjm 		return;
745650a0e1Sdjm 	param->name = NULL;
755650a0e1Sdjm 	param->purpose = 0;
765650a0e1Sdjm 	param->trust = 0;
77f1535dc8Sdjm 	/*param->inh_flags = X509_VP_FLAG_DEFAULT;*/
78e2ee43e0Sdjm 	param->inh_flags = 0;
795650a0e1Sdjm 	param->flags = 0;
805650a0e1Sdjm 	param->depth = -1;
81*7609e5c6Stedu 	if (param->policies) {
825650a0e1Sdjm 		sk_ASN1_OBJECT_pop_free(param->policies, ASN1_OBJECT_free);
835650a0e1Sdjm 		param->policies = NULL;
845650a0e1Sdjm 	}
855650a0e1Sdjm }
865650a0e1Sdjm 
875650a0e1Sdjm X509_VERIFY_PARAM *X509_VERIFY_PARAM_new(void)
885650a0e1Sdjm {
895650a0e1Sdjm 	X509_VERIFY_PARAM *param;
906f3a6cb1Sbeck 	param = malloc(sizeof(X509_VERIFY_PARAM));
915650a0e1Sdjm 	memset(param, 0, sizeof(X509_VERIFY_PARAM));
925650a0e1Sdjm 	x509_verify_param_zero(param);
935650a0e1Sdjm 	return param;
945650a0e1Sdjm }
955650a0e1Sdjm 
965650a0e1Sdjm void X509_VERIFY_PARAM_free(X509_VERIFY_PARAM *param)
975650a0e1Sdjm {
985650a0e1Sdjm 	x509_verify_param_zero(param);
996f3a6cb1Sbeck 	free(param);
1005650a0e1Sdjm }
1015650a0e1Sdjm 
1025650a0e1Sdjm /* This function determines how parameters are "inherited" from one structure
1035650a0e1Sdjm  * to another. There are several different ways this can happen.
1045650a0e1Sdjm  *
1055650a0e1Sdjm  * 1. If a child structure needs to have its values initialized from a parent
1065650a0e1Sdjm  *    they are simply copied across. For example SSL_CTX copied to SSL.
1075650a0e1Sdjm  * 2. If the structure should take on values only if they are currently unset.
1085650a0e1Sdjm  *    For example the values in an SSL structure will take appropriate value
1095650a0e1Sdjm  *    for SSL servers or clients but only if the application has not set new
1105650a0e1Sdjm  *    ones.
1115650a0e1Sdjm  *
1125650a0e1Sdjm  * The "inh_flags" field determines how this function behaves.
1135650a0e1Sdjm  *
1145650a0e1Sdjm  * Normally any values which are set in the default are not copied from the
1155650a0e1Sdjm  * destination and verify flags are ORed together.
1165650a0e1Sdjm  *
1175650a0e1Sdjm  * If X509_VP_FLAG_DEFAULT is set then anything set in the source is copied
1185650a0e1Sdjm  * to the destination. Effectively the values in "to" become default values
1195650a0e1Sdjm  * which will be used only if nothing new is set in "from".
1205650a0e1Sdjm  *
1215650a0e1Sdjm  * If X509_VP_FLAG_OVERWRITE is set then all value are copied across whether
1225650a0e1Sdjm  * they are set or not. Flags is still Ored though.
1235650a0e1Sdjm  *
1245650a0e1Sdjm  * If X509_VP_FLAG_RESET_FLAGS is set then the flags value is copied instead
1255650a0e1Sdjm  * of ORed.
1265650a0e1Sdjm  *
1275650a0e1Sdjm  * If X509_VP_FLAG_LOCKED is set then no values are copied.
1285650a0e1Sdjm  *
1295650a0e1Sdjm  * If X509_VP_FLAG_ONCE is set then the current inh_flags setting is zeroed
1305650a0e1Sdjm  * after the next call.
1315650a0e1Sdjm  */
1325650a0e1Sdjm 
1335650a0e1Sdjm /* Macro to test if a field should be copied from src to dest */
1345650a0e1Sdjm 
1355650a0e1Sdjm #define test_x509_verify_param_copy(field, def) \
1365650a0e1Sdjm 	(to_overwrite || \
1375650a0e1Sdjm 		((src->field != def) && (to_default || (dest->field == def))))
1385650a0e1Sdjm 
1395650a0e1Sdjm /* Macro to test and copy a field if necessary */
1405650a0e1Sdjm 
1415650a0e1Sdjm #define x509_verify_param_copy(field, def) \
1425650a0e1Sdjm 	if (test_x509_verify_param_copy(field, def)) \
1435650a0e1Sdjm 		dest->field = src->field
1445650a0e1Sdjm 
1455650a0e1Sdjm 
1465650a0e1Sdjm int X509_VERIFY_PARAM_inherit(X509_VERIFY_PARAM *dest,
1475650a0e1Sdjm 						const X509_VERIFY_PARAM *src)
1485650a0e1Sdjm {
1495650a0e1Sdjm 	unsigned long inh_flags;
1505650a0e1Sdjm 	int to_default, to_overwrite;
1515650a0e1Sdjm 	if (!src)
1525650a0e1Sdjm 		return 1;
1535650a0e1Sdjm 	inh_flags = dest->inh_flags | src->inh_flags;
1545650a0e1Sdjm 
1555650a0e1Sdjm 	if (inh_flags & X509_VP_FLAG_ONCE)
1565650a0e1Sdjm 		dest->inh_flags = 0;
1575650a0e1Sdjm 
1585650a0e1Sdjm 	if (inh_flags & X509_VP_FLAG_LOCKED)
1595650a0e1Sdjm 		return 1;
1605650a0e1Sdjm 
1615650a0e1Sdjm 	if (inh_flags & X509_VP_FLAG_DEFAULT)
1625650a0e1Sdjm 		to_default = 1;
1635650a0e1Sdjm 	else
1645650a0e1Sdjm 		to_default = 0;
1655650a0e1Sdjm 
1665650a0e1Sdjm 	if (inh_flags & X509_VP_FLAG_OVERWRITE)
1675650a0e1Sdjm 		to_overwrite = 1;
1685650a0e1Sdjm 	else
1695650a0e1Sdjm 		to_overwrite = 0;
1705650a0e1Sdjm 
1715650a0e1Sdjm 	x509_verify_param_copy(purpose, 0);
1725650a0e1Sdjm 	x509_verify_param_copy(trust, 0);
1735650a0e1Sdjm 	x509_verify_param_copy(depth, -1);
1745650a0e1Sdjm 
1755650a0e1Sdjm 	/* If overwrite or check time not set, copy across */
1765650a0e1Sdjm 
177*7609e5c6Stedu 	if (to_overwrite || !(dest->flags & X509_V_FLAG_USE_CHECK_TIME)) {
1785650a0e1Sdjm 		dest->check_time = src->check_time;
1795650a0e1Sdjm 		dest->flags &= ~X509_V_FLAG_USE_CHECK_TIME;
1805650a0e1Sdjm 		/* Don't need to copy flag: that is done below */
1815650a0e1Sdjm 	}
1825650a0e1Sdjm 
1835650a0e1Sdjm 	if (inh_flags & X509_VP_FLAG_RESET_FLAGS)
1845650a0e1Sdjm 		dest->flags = 0;
1855650a0e1Sdjm 
1865650a0e1Sdjm 	dest->flags |= src->flags;
1875650a0e1Sdjm 
188*7609e5c6Stedu 	if (test_x509_verify_param_copy(policies, NULL)) {
1895650a0e1Sdjm 		if (!X509_VERIFY_PARAM_set1_policies(dest, src->policies))
1905650a0e1Sdjm 			return 0;
1915650a0e1Sdjm 	}
1925650a0e1Sdjm 
1935650a0e1Sdjm 	return 1;
1945650a0e1Sdjm }
1955650a0e1Sdjm 
1965650a0e1Sdjm int X509_VERIFY_PARAM_set1(X509_VERIFY_PARAM *to,
1975650a0e1Sdjm 						const X509_VERIFY_PARAM *from)
1985650a0e1Sdjm {
199f1535dc8Sdjm 	unsigned long save_flags = to->inh_flags;
200f1535dc8Sdjm 	int ret;
2015650a0e1Sdjm 	to->inh_flags |= X509_VP_FLAG_DEFAULT;
202f1535dc8Sdjm 	ret = X509_VERIFY_PARAM_inherit(to, from);
203f1535dc8Sdjm 	to->inh_flags = save_flags;
204f1535dc8Sdjm 	return ret;
2055650a0e1Sdjm }
2065650a0e1Sdjm 
2075650a0e1Sdjm int X509_VERIFY_PARAM_set1_name(X509_VERIFY_PARAM *param, const char *name)
2085650a0e1Sdjm {
2095650a0e1Sdjm 	if (param->name)
2106f3a6cb1Sbeck 		free(param->name);
2115650a0e1Sdjm 	param->name = BUF_strdup(name);
2125650a0e1Sdjm 	if (param->name)
2135650a0e1Sdjm 		return 1;
2145650a0e1Sdjm 	return 0;
2155650a0e1Sdjm }
2165650a0e1Sdjm 
2175650a0e1Sdjm int X509_VERIFY_PARAM_set_flags(X509_VERIFY_PARAM *param, unsigned long flags)
2185650a0e1Sdjm {
2195650a0e1Sdjm 	param->flags |= flags;
2205650a0e1Sdjm 	if (flags & X509_V_FLAG_POLICY_MASK)
2215650a0e1Sdjm 		param->flags |= X509_V_FLAG_POLICY_CHECK;
2225650a0e1Sdjm 	return 1;
2235650a0e1Sdjm }
2245650a0e1Sdjm 
2255650a0e1Sdjm int X509_VERIFY_PARAM_clear_flags(X509_VERIFY_PARAM *param, unsigned long flags)
2265650a0e1Sdjm {
2275650a0e1Sdjm 	param->flags &= ~flags;
2285650a0e1Sdjm 	return 1;
2295650a0e1Sdjm }
2305650a0e1Sdjm 
2315650a0e1Sdjm unsigned long X509_VERIFY_PARAM_get_flags(X509_VERIFY_PARAM *param)
2325650a0e1Sdjm {
2335650a0e1Sdjm 	return param->flags;
2345650a0e1Sdjm }
2355650a0e1Sdjm 
2365650a0e1Sdjm int X509_VERIFY_PARAM_set_purpose(X509_VERIFY_PARAM *param, int purpose)
2375650a0e1Sdjm {
2385650a0e1Sdjm 	return X509_PURPOSE_set(&param->purpose, purpose);
2395650a0e1Sdjm }
2405650a0e1Sdjm 
2415650a0e1Sdjm int X509_VERIFY_PARAM_set_trust(X509_VERIFY_PARAM *param, int trust)
2425650a0e1Sdjm {
2435650a0e1Sdjm 	return X509_TRUST_set(&param->trust, trust);
2445650a0e1Sdjm }
2455650a0e1Sdjm 
2465650a0e1Sdjm void X509_VERIFY_PARAM_set_depth(X509_VERIFY_PARAM *param, int depth)
2475650a0e1Sdjm {
2485650a0e1Sdjm 	param->depth = depth;
2495650a0e1Sdjm }
2505650a0e1Sdjm 
2515650a0e1Sdjm void X509_VERIFY_PARAM_set_time(X509_VERIFY_PARAM *param, time_t t)
2525650a0e1Sdjm {
2535650a0e1Sdjm 	param->check_time = t;
2545650a0e1Sdjm 	param->flags |= X509_V_FLAG_USE_CHECK_TIME;
2555650a0e1Sdjm }
2565650a0e1Sdjm 
2575650a0e1Sdjm int X509_VERIFY_PARAM_add0_policy(X509_VERIFY_PARAM *param, ASN1_OBJECT *policy)
2585650a0e1Sdjm {
259*7609e5c6Stedu 	if (!param->policies) {
2605650a0e1Sdjm 		param->policies = sk_ASN1_OBJECT_new_null();
2615650a0e1Sdjm 		if (!param->policies)
2625650a0e1Sdjm 			return 0;
2635650a0e1Sdjm 	}
2645650a0e1Sdjm 	if (!sk_ASN1_OBJECT_push(param->policies, policy))
2655650a0e1Sdjm 		return 0;
2665650a0e1Sdjm 	return 1;
2675650a0e1Sdjm }
2685650a0e1Sdjm 
2695650a0e1Sdjm int X509_VERIFY_PARAM_set1_policies(X509_VERIFY_PARAM *param,
2705650a0e1Sdjm 					STACK_OF(ASN1_OBJECT) *policies)
2715650a0e1Sdjm {
2725650a0e1Sdjm 	int i;
2735650a0e1Sdjm 	ASN1_OBJECT *oid, *doid;
2745650a0e1Sdjm 	if (!param)
2755650a0e1Sdjm 		return 0;
2765650a0e1Sdjm 	if (param->policies)
2775650a0e1Sdjm 		sk_ASN1_OBJECT_pop_free(param->policies, ASN1_OBJECT_free);
2785650a0e1Sdjm 
279*7609e5c6Stedu 	if (!policies) {
2805650a0e1Sdjm 		param->policies = NULL;
2815650a0e1Sdjm 		return 1;
2825650a0e1Sdjm 	}
2835650a0e1Sdjm 
2845650a0e1Sdjm 	param->policies = sk_ASN1_OBJECT_new_null();
2855650a0e1Sdjm 	if (!param->policies)
2865650a0e1Sdjm 		return 0;
2875650a0e1Sdjm 
288*7609e5c6Stedu 	for (i = 0; i < sk_ASN1_OBJECT_num(policies); i++) {
2895650a0e1Sdjm 		oid = sk_ASN1_OBJECT_value(policies, i);
2905650a0e1Sdjm 		doid = OBJ_dup(oid);
2915650a0e1Sdjm 		if (!doid)
2925650a0e1Sdjm 			return 0;
293*7609e5c6Stedu 		if (!sk_ASN1_OBJECT_push(param->policies, doid)) {
2945650a0e1Sdjm 			ASN1_OBJECT_free(doid);
2955650a0e1Sdjm 			return 0;
2965650a0e1Sdjm 		}
2975650a0e1Sdjm 	}
2985650a0e1Sdjm 	param->flags |= X509_V_FLAG_POLICY_CHECK;
2995650a0e1Sdjm 	return 1;
3005650a0e1Sdjm }
3015650a0e1Sdjm 
3025650a0e1Sdjm int X509_VERIFY_PARAM_get_depth(const X509_VERIFY_PARAM *param)
3035650a0e1Sdjm {
3045650a0e1Sdjm 	return param->depth;
3055650a0e1Sdjm }
3065650a0e1Sdjm 
3075650a0e1Sdjm /* Default verify parameters: these are used for various
3085650a0e1Sdjm  * applications and can be overridden by the user specified table.
3095650a0e1Sdjm  * NB: the 'name' field *must* be in alphabetical order because it
3105650a0e1Sdjm  * will be searched using OBJ_search.
3115650a0e1Sdjm  */
3125650a0e1Sdjm 
3135650a0e1Sdjm static const X509_VERIFY_PARAM default_table[] = {
3145650a0e1Sdjm 	{
3155650a0e1Sdjm 	"default",	/* X509 default parameters */
3165650a0e1Sdjm 	0,		/* Check time */
3175650a0e1Sdjm 	0,		/* internal flags */
3185650a0e1Sdjm 	0,		/* flags */
3195650a0e1Sdjm 	0,		/* purpose */
3205650a0e1Sdjm 	0,		/* trust */
321e2ee43e0Sdjm 	100,		/* depth */
3225650a0e1Sdjm 	NULL		/* policies */
3235650a0e1Sdjm 	},
3245650a0e1Sdjm 	{
325f1535dc8Sdjm 	"pkcs7",			/* S/MIME sign parameters */
326e2ee43e0Sdjm 	0,				/* Check time */
327e2ee43e0Sdjm 	0,				/* internal flags */
328e2ee43e0Sdjm 	0,				/* flags */
329e2ee43e0Sdjm 	X509_PURPOSE_SMIME_SIGN,	/* purpose */
330e2ee43e0Sdjm 	X509_TRUST_EMAIL,		/* trust */
331e2ee43e0Sdjm 	-1,				/* depth */
332e2ee43e0Sdjm 	NULL				/* policies */
333e2ee43e0Sdjm 	},
334e2ee43e0Sdjm 	{
335f1535dc8Sdjm 	"smime_sign",			/* S/MIME sign parameters */
3365650a0e1Sdjm 	0,				/* Check time */
3375650a0e1Sdjm 	0,				/* internal flags */
3385650a0e1Sdjm 	0,				/* flags */
3395650a0e1Sdjm 	X509_PURPOSE_SMIME_SIGN,	/* purpose */
3405650a0e1Sdjm 	X509_TRUST_EMAIL,		/* trust */
3415650a0e1Sdjm 	-1,				/* depth */
3425650a0e1Sdjm 	NULL				/* policies */
3435650a0e1Sdjm 	},
3445650a0e1Sdjm 	{
3455650a0e1Sdjm 	"ssl_client",			/* SSL/TLS client parameters */
3465650a0e1Sdjm 	0,				/* Check time */
3475650a0e1Sdjm 	0,				/* internal flags */
3485650a0e1Sdjm 	0,				/* flags */
3495650a0e1Sdjm 	X509_PURPOSE_SSL_CLIENT,	/* purpose */
3505650a0e1Sdjm 	X509_TRUST_SSL_CLIENT,		/* trust */
3515650a0e1Sdjm 	-1,				/* depth */
3525650a0e1Sdjm 	NULL				/* policies */
3535650a0e1Sdjm 	},
3545650a0e1Sdjm 	{
3555650a0e1Sdjm 	"ssl_server",			/* SSL/TLS server parameters */
3565650a0e1Sdjm 	0,				/* Check time */
3575650a0e1Sdjm 	0,				/* internal flags */
3585650a0e1Sdjm 	0,				/* flags */
3595650a0e1Sdjm 	X509_PURPOSE_SSL_SERVER,	/* purpose */
3605650a0e1Sdjm 	X509_TRUST_SSL_SERVER,		/* trust */
3615650a0e1Sdjm 	-1,				/* depth */
3625650a0e1Sdjm 	NULL				/* policies */
363*7609e5c6Stedu 	}
364*7609e5c6Stedu };
3655650a0e1Sdjm 
3665650a0e1Sdjm static STACK_OF(X509_VERIFY_PARAM) *param_table = NULL;
3675650a0e1Sdjm 
368f1535dc8Sdjm static int table_cmp(const X509_VERIFY_PARAM *a, const X509_VERIFY_PARAM *b)
369f1535dc8Sdjm 
3705650a0e1Sdjm {
3715650a0e1Sdjm 	return strcmp(a->name, b->name);
3725650a0e1Sdjm }
3735650a0e1Sdjm 
374f1535dc8Sdjm DECLARE_OBJ_BSEARCH_CMP_FN(X509_VERIFY_PARAM, X509_VERIFY_PARAM,
375f1535dc8Sdjm 			   table);
376f1535dc8Sdjm IMPLEMENT_OBJ_BSEARCH_CMP_FN(X509_VERIFY_PARAM, X509_VERIFY_PARAM,
377f1535dc8Sdjm 			     table);
378f1535dc8Sdjm 
3795650a0e1Sdjm static int param_cmp(const X509_VERIFY_PARAM * const *a,
3805650a0e1Sdjm 			const X509_VERIFY_PARAM * const *b)
3815650a0e1Sdjm {
3825650a0e1Sdjm 	return strcmp((*a)->name, (*b)->name);
3835650a0e1Sdjm }
3845650a0e1Sdjm 
3855650a0e1Sdjm int X509_VERIFY_PARAM_add0_table(X509_VERIFY_PARAM *param)
3865650a0e1Sdjm {
3875650a0e1Sdjm 	int idx;
3885650a0e1Sdjm 	X509_VERIFY_PARAM *ptmp;
389*7609e5c6Stedu 	if (!param_table) {
3905650a0e1Sdjm 		param_table = sk_X509_VERIFY_PARAM_new(param_cmp);
3915650a0e1Sdjm 		if (!param_table)
3925650a0e1Sdjm 			return 0;
393*7609e5c6Stedu 	} else {
3945650a0e1Sdjm 		idx = sk_X509_VERIFY_PARAM_find(param_table, param);
395*7609e5c6Stedu 		if (idx != -1) {
3965650a0e1Sdjm 			ptmp = sk_X509_VERIFY_PARAM_value(param_table, idx);
3975650a0e1Sdjm 			X509_VERIFY_PARAM_free(ptmp);
3985650a0e1Sdjm 			(void)sk_X509_VERIFY_PARAM_delete(param_table, idx);
3995650a0e1Sdjm 		}
4005650a0e1Sdjm 	}
4015650a0e1Sdjm 	if (!sk_X509_VERIFY_PARAM_push(param_table, param))
4025650a0e1Sdjm 		return 0;
4035650a0e1Sdjm 	return 1;
4045650a0e1Sdjm }
4055650a0e1Sdjm 
4065650a0e1Sdjm const X509_VERIFY_PARAM *X509_VERIFY_PARAM_lookup(const char *name)
4075650a0e1Sdjm {
4085650a0e1Sdjm 	int idx;
4095650a0e1Sdjm 	X509_VERIFY_PARAM pm;
410f1535dc8Sdjm 
4115650a0e1Sdjm 	pm.name = (char *)name;
412*7609e5c6Stedu 	if (param_table) {
4135650a0e1Sdjm 		idx = sk_X509_VERIFY_PARAM_find(param_table, &pm);
4145650a0e1Sdjm 		if (idx != -1)
4155650a0e1Sdjm 			return sk_X509_VERIFY_PARAM_value(param_table, idx);
4165650a0e1Sdjm 	}
417f1535dc8Sdjm 	return OBJ_bsearch_table(&pm, default_table,
418f1535dc8Sdjm 			   sizeof(default_table)/sizeof(X509_VERIFY_PARAM));
4195650a0e1Sdjm }
4205650a0e1Sdjm 
4215650a0e1Sdjm void X509_VERIFY_PARAM_table_cleanup(void)
4225650a0e1Sdjm {
4235650a0e1Sdjm 	if (param_table)
4245650a0e1Sdjm 		sk_X509_VERIFY_PARAM_pop_free(param_table,
4255650a0e1Sdjm 						X509_VERIFY_PARAM_free);
4265650a0e1Sdjm 	param_table = NULL;
4275650a0e1Sdjm }
428