xref: /openbsd/lib/libcrypto/x509/x509_vpm.c (revision 69442892)
1*69442892Sbeck /* $OpenBSD: x509_vpm.c,v 1.10 2014/07/22 02:21:20 beck Exp $ */
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>
60a8913c44Sjsing #include <string.h>
615650a0e1Sdjm 
62b6ab114eSjsing #include <openssl/buffer.h>
635650a0e1Sdjm #include <openssl/crypto.h>
645650a0e1Sdjm #include <openssl/lhash.h>
655650a0e1Sdjm #include <openssl/x509.h>
665650a0e1Sdjm #include <openssl/x509v3.h>
675650a0e1Sdjm 
685650a0e1Sdjm /* X509_VERIFY_PARAM functions */
695650a0e1Sdjm 
7015238b08Sjsing static void
7115238b08Sjsing x509_verify_param_zero(X509_VERIFY_PARAM *param)
725650a0e1Sdjm {
735650a0e1Sdjm 	if (!param)
745650a0e1Sdjm 		return;
755650a0e1Sdjm 	param->name = NULL;
765650a0e1Sdjm 	param->purpose = 0;
775650a0e1Sdjm 	param->trust = 0;
78f1535dc8Sdjm 	/*param->inh_flags = X509_VP_FLAG_DEFAULT;*/
79e2ee43e0Sdjm 	param->inh_flags = 0;
805650a0e1Sdjm 	param->flags = 0;
815650a0e1Sdjm 	param->depth = -1;
827609e5c6Stedu 	if (param->policies) {
835650a0e1Sdjm 		sk_ASN1_OBJECT_pop_free(param->policies, ASN1_OBJECT_free);
845650a0e1Sdjm 		param->policies = NULL;
855650a0e1Sdjm 	}
865650a0e1Sdjm }
875650a0e1Sdjm 
8815238b08Sjsing X509_VERIFY_PARAM *
8915238b08Sjsing X509_VERIFY_PARAM_new(void)
905650a0e1Sdjm {
915650a0e1Sdjm 	X509_VERIFY_PARAM *param;
9215238b08Sjsing 
9366415b63Stedu 	param = calloc(1, sizeof(X509_VERIFY_PARAM));
945650a0e1Sdjm 	x509_verify_param_zero(param);
955650a0e1Sdjm 	return param;
965650a0e1Sdjm }
975650a0e1Sdjm 
9815238b08Sjsing void
9915238b08Sjsing X509_VERIFY_PARAM_free(X509_VERIFY_PARAM *param)
1005650a0e1Sdjm {
1015650a0e1Sdjm 	x509_verify_param_zero(param);
1026f3a6cb1Sbeck 	free(param);
1035650a0e1Sdjm }
1045650a0e1Sdjm 
1055650a0e1Sdjm /* This function determines how parameters are "inherited" from one structure
1065650a0e1Sdjm  * to another. There are several different ways this can happen.
1075650a0e1Sdjm  *
1085650a0e1Sdjm  * 1. If a child structure needs to have its values initialized from a parent
1095650a0e1Sdjm  *    they are simply copied across. For example SSL_CTX copied to SSL.
1105650a0e1Sdjm  * 2. If the structure should take on values only if they are currently unset.
1115650a0e1Sdjm  *    For example the values in an SSL structure will take appropriate value
1125650a0e1Sdjm  *    for SSL servers or clients but only if the application has not set new
1135650a0e1Sdjm  *    ones.
1145650a0e1Sdjm  *
1155650a0e1Sdjm  * The "inh_flags" field determines how this function behaves.
1165650a0e1Sdjm  *
1175650a0e1Sdjm  * Normally any values which are set in the default are not copied from the
1185650a0e1Sdjm  * destination and verify flags are ORed together.
1195650a0e1Sdjm  *
1205650a0e1Sdjm  * If X509_VP_FLAG_DEFAULT is set then anything set in the source is copied
1215650a0e1Sdjm  * to the destination. Effectively the values in "to" become default values
1225650a0e1Sdjm  * which will be used only if nothing new is set in "from".
1235650a0e1Sdjm  *
1245650a0e1Sdjm  * If X509_VP_FLAG_OVERWRITE is set then all value are copied across whether
1255650a0e1Sdjm  * they are set or not. Flags is still Ored though.
1265650a0e1Sdjm  *
1275650a0e1Sdjm  * If X509_VP_FLAG_RESET_FLAGS is set then the flags value is copied instead
1285650a0e1Sdjm  * of ORed.
1295650a0e1Sdjm  *
1305650a0e1Sdjm  * If X509_VP_FLAG_LOCKED is set then no values are copied.
1315650a0e1Sdjm  *
1325650a0e1Sdjm  * If X509_VP_FLAG_ONCE is set then the current inh_flags setting is zeroed
1335650a0e1Sdjm  * after the next call.
1345650a0e1Sdjm  */
1355650a0e1Sdjm 
1365650a0e1Sdjm /* Macro to test if a field should be copied from src to dest */
1375650a0e1Sdjm 
1385650a0e1Sdjm #define test_x509_verify_param_copy(field, def) \
1395650a0e1Sdjm 	(to_overwrite || \
1405650a0e1Sdjm 		((src->field != def) && (to_default || (dest->field == def))))
1415650a0e1Sdjm 
1425650a0e1Sdjm /* Macro to test and copy a field if necessary */
1435650a0e1Sdjm 
1445650a0e1Sdjm #define x509_verify_param_copy(field, def) \
1455650a0e1Sdjm 	if (test_x509_verify_param_copy(field, def)) \
1465650a0e1Sdjm 		dest->field = src->field
1475650a0e1Sdjm 
1485650a0e1Sdjm 
14915238b08Sjsing int
15015238b08Sjsing X509_VERIFY_PARAM_inherit(X509_VERIFY_PARAM *dest, const X509_VERIFY_PARAM *src)
1515650a0e1Sdjm {
1525650a0e1Sdjm 	unsigned long inh_flags;
1535650a0e1Sdjm 	int to_default, to_overwrite;
15415238b08Sjsing 
1555650a0e1Sdjm 	if (!src)
1565650a0e1Sdjm 		return 1;
1575650a0e1Sdjm 	inh_flags = dest->inh_flags | src->inh_flags;
1585650a0e1Sdjm 
1595650a0e1Sdjm 	if (inh_flags & X509_VP_FLAG_ONCE)
1605650a0e1Sdjm 		dest->inh_flags = 0;
1615650a0e1Sdjm 
1625650a0e1Sdjm 	if (inh_flags & X509_VP_FLAG_LOCKED)
1635650a0e1Sdjm 		return 1;
1645650a0e1Sdjm 
1655650a0e1Sdjm 	if (inh_flags & X509_VP_FLAG_DEFAULT)
1665650a0e1Sdjm 		to_default = 1;
1675650a0e1Sdjm 	else
1685650a0e1Sdjm 		to_default = 0;
1695650a0e1Sdjm 
1705650a0e1Sdjm 	if (inh_flags & X509_VP_FLAG_OVERWRITE)
1715650a0e1Sdjm 		to_overwrite = 1;
1725650a0e1Sdjm 	else
1735650a0e1Sdjm 		to_overwrite = 0;
1745650a0e1Sdjm 
1755650a0e1Sdjm 	x509_verify_param_copy(purpose, 0);
1765650a0e1Sdjm 	x509_verify_param_copy(trust, 0);
1775650a0e1Sdjm 	x509_verify_param_copy(depth, -1);
1785650a0e1Sdjm 
1795650a0e1Sdjm 	/* If overwrite or check time not set, copy across */
1805650a0e1Sdjm 
1817609e5c6Stedu 	if (to_overwrite || !(dest->flags & X509_V_FLAG_USE_CHECK_TIME)) {
1825650a0e1Sdjm 		dest->check_time = src->check_time;
1835650a0e1Sdjm 		dest->flags &= ~X509_V_FLAG_USE_CHECK_TIME;
1845650a0e1Sdjm 		/* Don't need to copy flag: that is done below */
1855650a0e1Sdjm 	}
1865650a0e1Sdjm 
1875650a0e1Sdjm 	if (inh_flags & X509_VP_FLAG_RESET_FLAGS)
1885650a0e1Sdjm 		dest->flags = 0;
1895650a0e1Sdjm 
1905650a0e1Sdjm 	dest->flags |= src->flags;
1915650a0e1Sdjm 
1927609e5c6Stedu 	if (test_x509_verify_param_copy(policies, NULL)) {
1935650a0e1Sdjm 		if (!X509_VERIFY_PARAM_set1_policies(dest, src->policies))
1945650a0e1Sdjm 			return 0;
1955650a0e1Sdjm 	}
1965650a0e1Sdjm 
1975650a0e1Sdjm 	return 1;
1985650a0e1Sdjm }
1995650a0e1Sdjm 
20015238b08Sjsing int
20115238b08Sjsing X509_VERIFY_PARAM_set1(X509_VERIFY_PARAM *to, const X509_VERIFY_PARAM *from)
2025650a0e1Sdjm {
203f1535dc8Sdjm 	unsigned long save_flags = to->inh_flags;
204f1535dc8Sdjm 	int ret;
20515238b08Sjsing 
2065650a0e1Sdjm 	to->inh_flags |= X509_VP_FLAG_DEFAULT;
207f1535dc8Sdjm 	ret = X509_VERIFY_PARAM_inherit(to, from);
208f1535dc8Sdjm 	to->inh_flags = save_flags;
209f1535dc8Sdjm 	return ret;
2105650a0e1Sdjm }
2115650a0e1Sdjm 
21215238b08Sjsing int
21315238b08Sjsing X509_VERIFY_PARAM_set1_name(X509_VERIFY_PARAM *param, const char *name)
2145650a0e1Sdjm {
2156f3a6cb1Sbeck 	free(param->name);
216*69442892Sbeck 	if (name == NULL)
217*69442892Sbeck 		return 1;
218*69442892Sbeck 	param->name = strdup(name);
2195650a0e1Sdjm 	if (param->name)
2205650a0e1Sdjm 		return 1;
2215650a0e1Sdjm 	return 0;
2225650a0e1Sdjm }
2235650a0e1Sdjm 
22415238b08Sjsing int
22515238b08Sjsing X509_VERIFY_PARAM_set_flags(X509_VERIFY_PARAM *param, unsigned long flags)
2265650a0e1Sdjm {
2275650a0e1Sdjm 	param->flags |= flags;
2285650a0e1Sdjm 	if (flags & X509_V_FLAG_POLICY_MASK)
2295650a0e1Sdjm 		param->flags |= X509_V_FLAG_POLICY_CHECK;
2305650a0e1Sdjm 	return 1;
2315650a0e1Sdjm }
2325650a0e1Sdjm 
23315238b08Sjsing int
23415238b08Sjsing X509_VERIFY_PARAM_clear_flags(X509_VERIFY_PARAM *param, unsigned long flags)
2355650a0e1Sdjm {
2365650a0e1Sdjm 	param->flags &= ~flags;
2375650a0e1Sdjm 	return 1;
2385650a0e1Sdjm }
2395650a0e1Sdjm 
24015238b08Sjsing unsigned long
24115238b08Sjsing X509_VERIFY_PARAM_get_flags(X509_VERIFY_PARAM *param)
2425650a0e1Sdjm {
2435650a0e1Sdjm 	return param->flags;
2445650a0e1Sdjm }
2455650a0e1Sdjm 
24615238b08Sjsing int
24715238b08Sjsing X509_VERIFY_PARAM_set_purpose(X509_VERIFY_PARAM *param, int purpose)
2485650a0e1Sdjm {
2495650a0e1Sdjm 	return X509_PURPOSE_set(&param->purpose, purpose);
2505650a0e1Sdjm }
2515650a0e1Sdjm 
25215238b08Sjsing int
25315238b08Sjsing X509_VERIFY_PARAM_set_trust(X509_VERIFY_PARAM *param, int trust)
2545650a0e1Sdjm {
2555650a0e1Sdjm 	return X509_TRUST_set(&param->trust, trust);
2565650a0e1Sdjm }
2575650a0e1Sdjm 
25815238b08Sjsing void
25915238b08Sjsing X509_VERIFY_PARAM_set_depth(X509_VERIFY_PARAM *param, int depth)
2605650a0e1Sdjm {
2615650a0e1Sdjm 	param->depth = depth;
2625650a0e1Sdjm }
2635650a0e1Sdjm 
26415238b08Sjsing void
26515238b08Sjsing X509_VERIFY_PARAM_set_time(X509_VERIFY_PARAM *param, time_t t)
2665650a0e1Sdjm {
2675650a0e1Sdjm 	param->check_time = t;
2685650a0e1Sdjm 	param->flags |= X509_V_FLAG_USE_CHECK_TIME;
2695650a0e1Sdjm }
2705650a0e1Sdjm 
27115238b08Sjsing int
27215238b08Sjsing X509_VERIFY_PARAM_add0_policy(X509_VERIFY_PARAM *param, ASN1_OBJECT *policy)
2735650a0e1Sdjm {
2747609e5c6Stedu 	if (!param->policies) {
2755650a0e1Sdjm 		param->policies = sk_ASN1_OBJECT_new_null();
2765650a0e1Sdjm 		if (!param->policies)
2775650a0e1Sdjm 			return 0;
2785650a0e1Sdjm 	}
2795650a0e1Sdjm 	if (!sk_ASN1_OBJECT_push(param->policies, policy))
2805650a0e1Sdjm 		return 0;
2815650a0e1Sdjm 	return 1;
2825650a0e1Sdjm }
2835650a0e1Sdjm 
28415238b08Sjsing int
28515238b08Sjsing X509_VERIFY_PARAM_set1_policies(X509_VERIFY_PARAM *param,
2865650a0e1Sdjm     STACK_OF(ASN1_OBJECT) *policies)
2875650a0e1Sdjm {
2885650a0e1Sdjm 	int i;
2895650a0e1Sdjm 	ASN1_OBJECT *oid, *doid;
29015238b08Sjsing 
2915650a0e1Sdjm 	if (!param)
2925650a0e1Sdjm 		return 0;
2935650a0e1Sdjm 	if (param->policies)
2945650a0e1Sdjm 		sk_ASN1_OBJECT_pop_free(param->policies, ASN1_OBJECT_free);
2955650a0e1Sdjm 
2967609e5c6Stedu 	if (!policies) {
2975650a0e1Sdjm 		param->policies = NULL;
2985650a0e1Sdjm 		return 1;
2995650a0e1Sdjm 	}
3005650a0e1Sdjm 
3015650a0e1Sdjm 	param->policies = sk_ASN1_OBJECT_new_null();
3025650a0e1Sdjm 	if (!param->policies)
3035650a0e1Sdjm 		return 0;
3045650a0e1Sdjm 
3057609e5c6Stedu 	for (i = 0; i < sk_ASN1_OBJECT_num(policies); i++) {
3065650a0e1Sdjm 		oid = sk_ASN1_OBJECT_value(policies, i);
3075650a0e1Sdjm 		doid = OBJ_dup(oid);
3085650a0e1Sdjm 		if (!doid)
3095650a0e1Sdjm 			return 0;
3107609e5c6Stedu 		if (!sk_ASN1_OBJECT_push(param->policies, doid)) {
3115650a0e1Sdjm 			ASN1_OBJECT_free(doid);
3125650a0e1Sdjm 			return 0;
3135650a0e1Sdjm 		}
3145650a0e1Sdjm 	}
3155650a0e1Sdjm 	param->flags |= X509_V_FLAG_POLICY_CHECK;
3165650a0e1Sdjm 	return 1;
3175650a0e1Sdjm }
3185650a0e1Sdjm 
31915238b08Sjsing int
32015238b08Sjsing X509_VERIFY_PARAM_get_depth(const X509_VERIFY_PARAM *param)
3215650a0e1Sdjm {
3225650a0e1Sdjm 	return param->depth;
3235650a0e1Sdjm }
3245650a0e1Sdjm 
3255650a0e1Sdjm /* Default verify parameters: these are used for various
3265650a0e1Sdjm  * applications and can be overridden by the user specified table.
3275650a0e1Sdjm  * NB: the 'name' field *must* be in alphabetical order because it
3285650a0e1Sdjm  * will be searched using OBJ_search.
3295650a0e1Sdjm  */
3305650a0e1Sdjm 
3315650a0e1Sdjm static const X509_VERIFY_PARAM default_table[] = {
3325650a0e1Sdjm 	{
3335650a0e1Sdjm 		"default",			/* X509 default parameters */
3345650a0e1Sdjm 		0,				/* Check time */
3355650a0e1Sdjm 		0,				/* internal flags */
3365650a0e1Sdjm 		0,				/* flags */
3375650a0e1Sdjm 		0,				/* purpose */
3385650a0e1Sdjm 		0,				/* trust */
339e2ee43e0Sdjm 		100,				/* depth */
3405650a0e1Sdjm 		NULL				/* policies */
3415650a0e1Sdjm 	},
3425650a0e1Sdjm 	{
343f1535dc8Sdjm 		"pkcs7",			/* S/MIME sign parameters */
344e2ee43e0Sdjm 		0,				/* Check time */
345e2ee43e0Sdjm 		0,				/* internal flags */
346e2ee43e0Sdjm 		0,				/* flags */
347e2ee43e0Sdjm 		X509_PURPOSE_SMIME_SIGN,	/* purpose */
348e2ee43e0Sdjm 		X509_TRUST_EMAIL,		/* trust */
349e2ee43e0Sdjm 		-1,				/* depth */
350e2ee43e0Sdjm 		NULL				/* policies */
351e2ee43e0Sdjm 	},
352e2ee43e0Sdjm 	{
353f1535dc8Sdjm 		"smime_sign",			/* S/MIME sign parameters */
3545650a0e1Sdjm 		0,				/* Check time */
3555650a0e1Sdjm 		0,				/* internal flags */
3565650a0e1Sdjm 		0,				/* flags */
3575650a0e1Sdjm 		X509_PURPOSE_SMIME_SIGN,	/* purpose */
3585650a0e1Sdjm 		X509_TRUST_EMAIL,		/* trust */
3595650a0e1Sdjm 		-1,				/* depth */
3605650a0e1Sdjm 		NULL				/* policies */
3615650a0e1Sdjm 	},
3625650a0e1Sdjm 	{
3635650a0e1Sdjm 		"ssl_client",			/* SSL/TLS client parameters */
3645650a0e1Sdjm 		0,				/* Check time */
3655650a0e1Sdjm 		0,				/* internal flags */
3665650a0e1Sdjm 		0,				/* flags */
3675650a0e1Sdjm 		X509_PURPOSE_SSL_CLIENT,	/* purpose */
3685650a0e1Sdjm 		X509_TRUST_SSL_CLIENT,		/* trust */
3695650a0e1Sdjm 		-1,				/* depth */
3705650a0e1Sdjm 		NULL				/* policies */
3715650a0e1Sdjm 	},
3725650a0e1Sdjm 	{
3735650a0e1Sdjm 		"ssl_server",			/* SSL/TLS server parameters */
3745650a0e1Sdjm 		0,				/* Check time */
3755650a0e1Sdjm 		0,				/* internal flags */
3765650a0e1Sdjm 		0,				/* flags */
3775650a0e1Sdjm 		X509_PURPOSE_SSL_SERVER,	/* purpose */
3785650a0e1Sdjm 		X509_TRUST_SSL_SERVER,		/* trust */
3795650a0e1Sdjm 		-1,				/* depth */
3805650a0e1Sdjm 		NULL				/* policies */
3817609e5c6Stedu 	}
3827609e5c6Stedu };
3835650a0e1Sdjm 
3845650a0e1Sdjm static STACK_OF(X509_VERIFY_PARAM) *param_table = NULL;
3855650a0e1Sdjm 
38615238b08Sjsing static int
38715238b08Sjsing table_cmp(const X509_VERIFY_PARAM *a, const X509_VERIFY_PARAM *b)
3885650a0e1Sdjm {
3895650a0e1Sdjm 	return strcmp(a->name, b->name);
3905650a0e1Sdjm }
3915650a0e1Sdjm 
39215238b08Sjsing DECLARE_OBJ_BSEARCH_CMP_FN(X509_VERIFY_PARAM, X509_VERIFY_PARAM, table);
39315238b08Sjsing IMPLEMENT_OBJ_BSEARCH_CMP_FN(X509_VERIFY_PARAM, X509_VERIFY_PARAM, table);
394f1535dc8Sdjm 
39515238b08Sjsing static int
39615238b08Sjsing param_cmp(const X509_VERIFY_PARAM * const *a,
3975650a0e1Sdjm     const X509_VERIFY_PARAM * const *b)
3985650a0e1Sdjm {
3995650a0e1Sdjm 	return strcmp((*a)->name, (*b)->name);
4005650a0e1Sdjm }
4015650a0e1Sdjm 
40215238b08Sjsing int
40315238b08Sjsing X509_VERIFY_PARAM_add0_table(X509_VERIFY_PARAM *param)
4045650a0e1Sdjm {
4055650a0e1Sdjm 	int idx;
4065650a0e1Sdjm 	X509_VERIFY_PARAM *ptmp;
40715238b08Sjsing 
4087609e5c6Stedu 	if (!param_table) {
4095650a0e1Sdjm 		param_table = sk_X509_VERIFY_PARAM_new(param_cmp);
4105650a0e1Sdjm 		if (!param_table)
4115650a0e1Sdjm 			return 0;
4127609e5c6Stedu 	} else {
4135650a0e1Sdjm 		idx = sk_X509_VERIFY_PARAM_find(param_table, param);
4147609e5c6Stedu 		if (idx != -1) {
4155650a0e1Sdjm 			ptmp = sk_X509_VERIFY_PARAM_value(param_table, idx);
4165650a0e1Sdjm 			X509_VERIFY_PARAM_free(ptmp);
4175650a0e1Sdjm 			(void)sk_X509_VERIFY_PARAM_delete(param_table, idx);
4185650a0e1Sdjm 		}
4195650a0e1Sdjm 	}
4205650a0e1Sdjm 	if (!sk_X509_VERIFY_PARAM_push(param_table, param))
4215650a0e1Sdjm 		return 0;
4225650a0e1Sdjm 	return 1;
4235650a0e1Sdjm }
4245650a0e1Sdjm 
42515238b08Sjsing const X509_VERIFY_PARAM *
42615238b08Sjsing X509_VERIFY_PARAM_lookup(const char *name)
4275650a0e1Sdjm {
4285650a0e1Sdjm 	int idx;
4295650a0e1Sdjm 	X509_VERIFY_PARAM pm;
430f1535dc8Sdjm 
4315650a0e1Sdjm 	pm.name = (char *)name;
4327609e5c6Stedu 	if (param_table) {
4335650a0e1Sdjm 		idx = sk_X509_VERIFY_PARAM_find(param_table, &pm);
4345650a0e1Sdjm 		if (idx != -1)
4355650a0e1Sdjm 			return sk_X509_VERIFY_PARAM_value(param_table, idx);
4365650a0e1Sdjm 	}
437f1535dc8Sdjm 	return OBJ_bsearch_table(&pm, default_table,
438f1535dc8Sdjm 	    sizeof(default_table)/sizeof(X509_VERIFY_PARAM));
4395650a0e1Sdjm }
4405650a0e1Sdjm 
44115238b08Sjsing void
44215238b08Sjsing X509_VERIFY_PARAM_table_cleanup(void)
4435650a0e1Sdjm {
4445650a0e1Sdjm 	if (param_table)
4455650a0e1Sdjm 		sk_X509_VERIFY_PARAM_pop_free(param_table,
4465650a0e1Sdjm 		    X509_VERIFY_PARAM_free);
4475650a0e1Sdjm 	param_table = NULL;
4485650a0e1Sdjm }
449