xref: /freebsd/crypto/openssl/ssl/ssl_cert.c (revision 3b4e3dcb9f42dc9f4f864acf804677d7a3e0c233)
174664626SKris Kennaway /*! \file ssl/ssl_cert.c */
274664626SKris Kennaway /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
374664626SKris Kennaway  * All rights reserved.
474664626SKris Kennaway  *
574664626SKris Kennaway  * This package is an SSL implementation written
674664626SKris Kennaway  * by Eric Young (eay@cryptsoft.com).
774664626SKris Kennaway  * The implementation was written so as to conform with Netscapes SSL.
874664626SKris Kennaway  *
974664626SKris Kennaway  * This library is free for commercial and non-commercial use as long as
1074664626SKris Kennaway  * the following conditions are aheared to.  The following conditions
1174664626SKris Kennaway  * apply to all code found in this distribution, be it the RC4, RSA,
1274664626SKris Kennaway  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1374664626SKris Kennaway  * included with this distribution is covered by the same copyright terms
1474664626SKris Kennaway  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1574664626SKris Kennaway  *
1674664626SKris Kennaway  * Copyright remains Eric Young's, and as such any Copyright notices in
1774664626SKris Kennaway  * the code are not to be removed.
1874664626SKris Kennaway  * If this package is used in a product, Eric Young should be given attribution
1974664626SKris Kennaway  * as the author of the parts of the library used.
2074664626SKris Kennaway  * This can be in the form of a textual message at program startup or
2174664626SKris Kennaway  * in documentation (online or textual) provided with the package.
2274664626SKris Kennaway  *
2374664626SKris Kennaway  * Redistribution and use in source and binary forms, with or without
2474664626SKris Kennaway  * modification, are permitted provided that the following conditions
2574664626SKris Kennaway  * are met:
2674664626SKris Kennaway  * 1. Redistributions of source code must retain the copyright
2774664626SKris Kennaway  *    notice, this list of conditions and the following disclaimer.
2874664626SKris Kennaway  * 2. Redistributions in binary form must reproduce the above copyright
2974664626SKris Kennaway  *    notice, this list of conditions and the following disclaimer in the
3074664626SKris Kennaway  *    documentation and/or other materials provided with the distribution.
3174664626SKris Kennaway  * 3. All advertising materials mentioning features or use of this software
3274664626SKris Kennaway  *    must display the following acknowledgement:
3374664626SKris Kennaway  *    "This product includes cryptographic software written by
3474664626SKris Kennaway  *     Eric Young (eay@cryptsoft.com)"
3574664626SKris Kennaway  *    The word 'cryptographic' can be left out if the rouines from the library
3674664626SKris Kennaway  *    being used are not cryptographic related :-).
3774664626SKris Kennaway  * 4. If you include any Windows specific code (or a derivative thereof) from
3874664626SKris Kennaway  *    the apps directory (application code) you must include an acknowledgement:
3974664626SKris Kennaway  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4074664626SKris Kennaway  *
4174664626SKris Kennaway  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4274664626SKris Kennaway  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4374664626SKris Kennaway  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4474664626SKris Kennaway  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4574664626SKris Kennaway  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4674664626SKris Kennaway  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4774664626SKris Kennaway  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4874664626SKris Kennaway  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4974664626SKris Kennaway  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5074664626SKris Kennaway  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5174664626SKris Kennaway  * SUCH DAMAGE.
5274664626SKris Kennaway  *
5374664626SKris Kennaway  * The licence and distribution terms for any publically available version or
5474664626SKris Kennaway  * derivative of this code cannot be changed.  i.e. this code cannot simply be
5574664626SKris Kennaway  * copied and put under another distribution licence
5674664626SKris Kennaway  * [including the GNU Public Licence.]
5774664626SKris Kennaway  */
5874664626SKris Kennaway /* ====================================================================
5974664626SKris Kennaway  * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
6074664626SKris Kennaway  *
6174664626SKris Kennaway  * Redistribution and use in source and binary forms, with or without
6274664626SKris Kennaway  * modification, are permitted provided that the following conditions
6374664626SKris Kennaway  * are met:
6474664626SKris Kennaway  *
6574664626SKris Kennaway  * 1. Redistributions of source code must retain the above copyright
6674664626SKris Kennaway  *    notice, this list of conditions and the following disclaimer.
6774664626SKris Kennaway  *
6874664626SKris Kennaway  * 2. Redistributions in binary form must reproduce the above copyright
6974664626SKris Kennaway  *    notice, this list of conditions and the following disclaimer in
7074664626SKris Kennaway  *    the documentation and/or other materials provided with the
7174664626SKris Kennaway  *    distribution.
7274664626SKris Kennaway  *
7374664626SKris Kennaway  * 3. All advertising materials mentioning features or use of this
7474664626SKris Kennaway  *    software must display the following acknowledgment:
7574664626SKris Kennaway  *    "This product includes software developed by the OpenSSL Project
7674664626SKris Kennaway  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
7774664626SKris Kennaway  *
7874664626SKris Kennaway  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
7974664626SKris Kennaway  *    endorse or promote products derived from this software without
8074664626SKris Kennaway  *    prior written permission. For written permission, please contact
8174664626SKris Kennaway  *    openssl-core@OpenSSL.org.
8274664626SKris Kennaway  *
8374664626SKris Kennaway  * 5. Products derived from this software may not be called "OpenSSL"
8474664626SKris Kennaway  *    nor may "OpenSSL" appear in their names without prior written
8574664626SKris Kennaway  *    permission of the OpenSSL Project.
8674664626SKris Kennaway  *
8774664626SKris Kennaway  * 6. Redistributions of any form whatsoever must retain the following
8874664626SKris Kennaway  *    acknowledgment:
8974664626SKris Kennaway  *    "This product includes software developed by the OpenSSL Project
9074664626SKris Kennaway  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
9174664626SKris Kennaway  *
9274664626SKris Kennaway  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
9374664626SKris Kennaway  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
9474664626SKris Kennaway  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
9574664626SKris Kennaway  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
9674664626SKris Kennaway  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9774664626SKris Kennaway  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
9874664626SKris Kennaway  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
9974664626SKris Kennaway  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
10074664626SKris Kennaway  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
10174664626SKris Kennaway  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
10274664626SKris Kennaway  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
10374664626SKris Kennaway  * OF THE POSSIBILITY OF SUCH DAMAGE.
10474664626SKris Kennaway  * ====================================================================
10574664626SKris Kennaway  */
1063b4e3dcbSSimon L. B. Nielsen /* ====================================================================
1073b4e3dcbSSimon L. B. Nielsen  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
1083b4e3dcbSSimon L. B. Nielsen  * ECC cipher suite support in OpenSSL originally developed by
1093b4e3dcbSSimon L. B. Nielsen  * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
1103b4e3dcbSSimon L. B. Nielsen  */
11174664626SKris Kennaway 
11274664626SKris Kennaway #include <stdio.h>
113f579bf8eSKris Kennaway 
1145c87c606SMark Murray #include "e_os.h"
115f579bf8eSKris Kennaway #ifndef NO_SYS_TYPES_H
11674664626SKris Kennaway # include <sys/types.h>
117f579bf8eSKris Kennaway #endif
118f579bf8eSKris Kennaway 
1193b4e3dcbSSimon L. B. Nielsen #include "o_dir.h"
12074664626SKris Kennaway #include <openssl/objects.h>
12174664626SKris Kennaway #include <openssl/bio.h>
12274664626SKris Kennaway #include <openssl/pem.h>
123f579bf8eSKris Kennaway #include <openssl/x509v3.h>
1243b4e3dcbSSimon L. B. Nielsen #ifndef OPENSSL_NO_DH
1253b4e3dcbSSimon L. B. Nielsen #include <openssl/dh.h>
1263b4e3dcbSSimon L. B. Nielsen #endif
1273b4e3dcbSSimon L. B. Nielsen #include <openssl/bn.h>
12874664626SKris Kennaway #include "ssl_locl.h"
12974664626SKris Kennaway 
13074664626SKris Kennaway int SSL_get_ex_data_X509_STORE_CTX_idx(void)
13174664626SKris Kennaway 	{
1325c87c606SMark Murray 	static volatile int ssl_x509_store_ctx_idx= -1;
1335c87c606SMark Murray 
1345c87c606SMark Murray 	if (ssl_x509_store_ctx_idx < 0)
1355c87c606SMark Murray 		{
1365c87c606SMark Murray 		/* any write lock will do; usually this branch
1375c87c606SMark Murray 		 * will only be taken once anyway */
1385c87c606SMark Murray 		CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
13974664626SKris Kennaway 
14074664626SKris Kennaway 		if (ssl_x509_store_ctx_idx < 0)
14174664626SKris Kennaway 			{
14274664626SKris Kennaway 			ssl_x509_store_ctx_idx=X509_STORE_CTX_get_ex_new_index(
14374664626SKris Kennaway 				0,"SSL for verify callback",NULL,NULL,NULL);
14474664626SKris Kennaway 			}
1455c87c606SMark Murray 
1465c87c606SMark Murray 		CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
1475c87c606SMark Murray 		}
1485c87c606SMark Murray 	return ssl_x509_store_ctx_idx;
14974664626SKris Kennaway 	}
15074664626SKris Kennaway 
15174664626SKris Kennaway CERT *ssl_cert_new(void)
15274664626SKris Kennaway 	{
15374664626SKris Kennaway 	CERT *ret;
15474664626SKris Kennaway 
155ddd58736SKris Kennaway 	ret=(CERT *)OPENSSL_malloc(sizeof(CERT));
15674664626SKris Kennaway 	if (ret == NULL)
15774664626SKris Kennaway 		{
15874664626SKris Kennaway 		SSLerr(SSL_F_SSL_CERT_NEW,ERR_R_MALLOC_FAILURE);
15974664626SKris Kennaway 		return(NULL);
16074664626SKris Kennaway 		}
16174664626SKris Kennaway 	memset(ret,0,sizeof(CERT));
16274664626SKris Kennaway 
16374664626SKris Kennaway 	ret->key= &(ret->pkeys[SSL_PKEY_RSA_ENC]);
16474664626SKris Kennaway 	ret->references=1;
16574664626SKris Kennaway 
16674664626SKris Kennaway 	return(ret);
16774664626SKris Kennaway 	}
16874664626SKris Kennaway 
16974664626SKris Kennaway CERT *ssl_cert_dup(CERT *cert)
17074664626SKris Kennaway 	{
17174664626SKris Kennaway 	CERT *ret;
17274664626SKris Kennaway 	int i;
17374664626SKris Kennaway 
174ddd58736SKris Kennaway 	ret = (CERT *)OPENSSL_malloc(sizeof(CERT));
17574664626SKris Kennaway 	if (ret == NULL)
17674664626SKris Kennaway 		{
17774664626SKris Kennaway 		SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_MALLOC_FAILURE);
17874664626SKris Kennaway 		return(NULL);
17974664626SKris Kennaway 		}
18074664626SKris Kennaway 
18174664626SKris Kennaway 	memset(ret, 0, sizeof(CERT));
18274664626SKris Kennaway 
18374664626SKris Kennaway 	ret->key = &ret->pkeys[cert->key - &cert->pkeys[0]];
18474664626SKris Kennaway 	/* or ret->key = ret->pkeys + (cert->key - cert->pkeys),
18574664626SKris Kennaway 	 * if you find that more readable */
18674664626SKris Kennaway 
18774664626SKris Kennaway 	ret->valid = cert->valid;
18874664626SKris Kennaway 	ret->mask = cert->mask;
18974664626SKris Kennaway 	ret->export_mask = cert->export_mask;
19074664626SKris Kennaway 
1915c87c606SMark Murray #ifndef OPENSSL_NO_RSA
19274664626SKris Kennaway 	if (cert->rsa_tmp != NULL)
19374664626SKris Kennaway 		{
1945c87c606SMark Murray 		RSA_up_ref(cert->rsa_tmp);
19574664626SKris Kennaway 		ret->rsa_tmp = cert->rsa_tmp;
19674664626SKris Kennaway 		}
19774664626SKris Kennaway 	ret->rsa_tmp_cb = cert->rsa_tmp_cb;
19874664626SKris Kennaway #endif
19974664626SKris Kennaway 
2005c87c606SMark Murray #ifndef OPENSSL_NO_DH
20174664626SKris Kennaway 	if (cert->dh_tmp != NULL)
20274664626SKris Kennaway 		{
20374664626SKris Kennaway 		ret->dh_tmp = DHparams_dup(cert->dh_tmp);
20474664626SKris Kennaway 		if (ret->dh_tmp == NULL)
20574664626SKris Kennaway 			{
206f579bf8eSKris Kennaway 			SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_DH_LIB);
20774664626SKris Kennaway 			goto err;
20874664626SKris Kennaway 			}
209f579bf8eSKris Kennaway 		if (cert->dh_tmp->priv_key)
210f579bf8eSKris Kennaway 			{
211f579bf8eSKris Kennaway 			BIGNUM *b = BN_dup(cert->dh_tmp->priv_key);
212f579bf8eSKris Kennaway 			if (!b)
213f579bf8eSKris Kennaway 				{
214f579bf8eSKris Kennaway 				SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_BN_LIB);
215f579bf8eSKris Kennaway 				goto err;
216f579bf8eSKris Kennaway 				}
217f579bf8eSKris Kennaway 			ret->dh_tmp->priv_key = b;
218f579bf8eSKris Kennaway 			}
219f579bf8eSKris Kennaway 		if (cert->dh_tmp->pub_key)
220f579bf8eSKris Kennaway 			{
221f579bf8eSKris Kennaway 			BIGNUM *b = BN_dup(cert->dh_tmp->pub_key);
222f579bf8eSKris Kennaway 			if (!b)
223f579bf8eSKris Kennaway 				{
224f579bf8eSKris Kennaway 				SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_BN_LIB);
225f579bf8eSKris Kennaway 				goto err;
226f579bf8eSKris Kennaway 				}
227f579bf8eSKris Kennaway 			ret->dh_tmp->pub_key = b;
228f579bf8eSKris Kennaway 			}
22974664626SKris Kennaway 		}
23074664626SKris Kennaway 	ret->dh_tmp_cb = cert->dh_tmp_cb;
23174664626SKris Kennaway #endif
23274664626SKris Kennaway 
2333b4e3dcbSSimon L. B. Nielsen #ifndef OPENSSL_NO_ECDH
2343b4e3dcbSSimon L. B. Nielsen 	if (cert->ecdh_tmp)
2353b4e3dcbSSimon L. B. Nielsen 		{
2363b4e3dcbSSimon L. B. Nielsen 		ret->ecdh_tmp = EC_KEY_dup(cert->ecdh_tmp);
2373b4e3dcbSSimon L. B. Nielsen 		if (ret->ecdh_tmp == NULL)
2383b4e3dcbSSimon L. B. Nielsen 			{
2393b4e3dcbSSimon L. B. Nielsen 			SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_EC_LIB);
2403b4e3dcbSSimon L. B. Nielsen 			goto err;
2413b4e3dcbSSimon L. B. Nielsen 			}
2423b4e3dcbSSimon L. B. Nielsen 		}
2433b4e3dcbSSimon L. B. Nielsen 	ret->ecdh_tmp_cb = cert->ecdh_tmp_cb;
2443b4e3dcbSSimon L. B. Nielsen #endif
2453b4e3dcbSSimon L. B. Nielsen 
24674664626SKris Kennaway 	for (i = 0; i < SSL_PKEY_NUM; i++)
24774664626SKris Kennaway 		{
24874664626SKris Kennaway 		if (cert->pkeys[i].x509 != NULL)
24974664626SKris Kennaway 			{
25074664626SKris Kennaway 			ret->pkeys[i].x509 = cert->pkeys[i].x509;
25174664626SKris Kennaway 			CRYPTO_add(&ret->pkeys[i].x509->references, 1,
25274664626SKris Kennaway 				CRYPTO_LOCK_X509);
25374664626SKris Kennaway 			}
25474664626SKris Kennaway 
25574664626SKris Kennaway 		if (cert->pkeys[i].privatekey != NULL)
25674664626SKris Kennaway 			{
25774664626SKris Kennaway 			ret->pkeys[i].privatekey = cert->pkeys[i].privatekey;
25874664626SKris Kennaway 			CRYPTO_add(&ret->pkeys[i].privatekey->references, 1,
25974664626SKris Kennaway 				CRYPTO_LOCK_EVP_PKEY);
26074664626SKris Kennaway 
26174664626SKris Kennaway 			switch(i)
26274664626SKris Kennaway 				{
26374664626SKris Kennaway 				/* If there was anything special to do for
26474664626SKris Kennaway 				 * certain types of keys, we'd do it here.
26574664626SKris Kennaway 				 * (Nothing at the moment, I think.) */
26674664626SKris Kennaway 
26774664626SKris Kennaway 			case SSL_PKEY_RSA_ENC:
26874664626SKris Kennaway 			case SSL_PKEY_RSA_SIGN:
26974664626SKris Kennaway 				/* We have an RSA key. */
27074664626SKris Kennaway 				break;
27174664626SKris Kennaway 
27274664626SKris Kennaway 			case SSL_PKEY_DSA_SIGN:
27374664626SKris Kennaway 				/* We have a DSA key. */
27474664626SKris Kennaway 				break;
27574664626SKris Kennaway 
27674664626SKris Kennaway 			case SSL_PKEY_DH_RSA:
27774664626SKris Kennaway 			case SSL_PKEY_DH_DSA:
27874664626SKris Kennaway 				/* We have a DH key. */
27974664626SKris Kennaway 				break;
28074664626SKris Kennaway 
2813b4e3dcbSSimon L. B. Nielsen 			case SSL_PKEY_ECC:
2823b4e3dcbSSimon L. B. Nielsen 				/* We have an ECC key */
2833b4e3dcbSSimon L. B. Nielsen 				break;
2843b4e3dcbSSimon L. B. Nielsen 
28574664626SKris Kennaway 			default:
28674664626SKris Kennaway 				/* Can't happen. */
28774664626SKris Kennaway 				SSLerr(SSL_F_SSL_CERT_DUP, SSL_R_LIBRARY_BUG);
28874664626SKris Kennaway 				}
28974664626SKris Kennaway 			}
29074664626SKris Kennaway 		}
29174664626SKris Kennaway 
29274664626SKris Kennaway 	/* ret->extra_certs *should* exist, but currently the own certificate
29374664626SKris Kennaway 	 * chain is held inside SSL_CTX */
29474664626SKris Kennaway 
29574664626SKris Kennaway 	ret->references=1;
29674664626SKris Kennaway 
29774664626SKris Kennaway 	return(ret);
29874664626SKris Kennaway 
2993b4e3dcbSSimon L. B. Nielsen #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_ECDH)
30074664626SKris Kennaway err:
3015740a5e3SKris Kennaway #endif
3025c87c606SMark Murray #ifndef OPENSSL_NO_RSA
30374664626SKris Kennaway 	if (ret->rsa_tmp != NULL)
30474664626SKris Kennaway 		RSA_free(ret->rsa_tmp);
30574664626SKris Kennaway #endif
3065c87c606SMark Murray #ifndef OPENSSL_NO_DH
30774664626SKris Kennaway 	if (ret->dh_tmp != NULL)
30874664626SKris Kennaway 		DH_free(ret->dh_tmp);
30974664626SKris Kennaway #endif
3103b4e3dcbSSimon L. B. Nielsen #ifndef OPENSSL_NO_ECDH
3113b4e3dcbSSimon L. B. Nielsen 	if (ret->ecdh_tmp != NULL)
3123b4e3dcbSSimon L. B. Nielsen 		EC_KEY_free(ret->ecdh_tmp);
3133b4e3dcbSSimon L. B. Nielsen #endif
31474664626SKris Kennaway 
31574664626SKris Kennaway 	for (i = 0; i < SSL_PKEY_NUM; i++)
31674664626SKris Kennaway 		{
31774664626SKris Kennaway 		if (ret->pkeys[i].x509 != NULL)
31874664626SKris Kennaway 			X509_free(ret->pkeys[i].x509);
31974664626SKris Kennaway 		if (ret->pkeys[i].privatekey != NULL)
32074664626SKris Kennaway 			EVP_PKEY_free(ret->pkeys[i].privatekey);
32174664626SKris Kennaway 		}
32274664626SKris Kennaway 
32374664626SKris Kennaway 	return NULL;
32474664626SKris Kennaway 	}
32574664626SKris Kennaway 
32674664626SKris Kennaway 
32774664626SKris Kennaway void ssl_cert_free(CERT *c)
32874664626SKris Kennaway 	{
32974664626SKris Kennaway 	int i;
33074664626SKris Kennaway 
33174664626SKris Kennaway 	if(c == NULL)
33274664626SKris Kennaway 	    return;
33374664626SKris Kennaway 
33474664626SKris Kennaway 	i=CRYPTO_add(&c->references,-1,CRYPTO_LOCK_SSL_CERT);
33574664626SKris Kennaway #ifdef REF_PRINT
33674664626SKris Kennaway 	REF_PRINT("CERT",c);
33774664626SKris Kennaway #endif
33874664626SKris Kennaway 	if (i > 0) return;
33974664626SKris Kennaway #ifdef REF_CHECK
34074664626SKris Kennaway 	if (i < 0)
34174664626SKris Kennaway 		{
34274664626SKris Kennaway 		fprintf(stderr,"ssl_cert_free, bad reference count\n");
34374664626SKris Kennaway 		abort(); /* ok */
34474664626SKris Kennaway 		}
34574664626SKris Kennaway #endif
34674664626SKris Kennaway 
3475c87c606SMark Murray #ifndef OPENSSL_NO_RSA
34874664626SKris Kennaway 	if (c->rsa_tmp) RSA_free(c->rsa_tmp);
34974664626SKris Kennaway #endif
3505c87c606SMark Murray #ifndef OPENSSL_NO_DH
35174664626SKris Kennaway 	if (c->dh_tmp) DH_free(c->dh_tmp);
35274664626SKris Kennaway #endif
3533b4e3dcbSSimon L. B. Nielsen #ifndef OPENSSL_NO_ECDH
3543b4e3dcbSSimon L. B. Nielsen 	if (c->ecdh_tmp) EC_KEY_free(c->ecdh_tmp);
3553b4e3dcbSSimon L. B. Nielsen #endif
35674664626SKris Kennaway 
35774664626SKris Kennaway 	for (i=0; i<SSL_PKEY_NUM; i++)
35874664626SKris Kennaway 		{
35974664626SKris Kennaway 		if (c->pkeys[i].x509 != NULL)
36074664626SKris Kennaway 			X509_free(c->pkeys[i].x509);
36174664626SKris Kennaway 		if (c->pkeys[i].privatekey != NULL)
36274664626SKris Kennaway 			EVP_PKEY_free(c->pkeys[i].privatekey);
36374664626SKris Kennaway #if 0
36474664626SKris Kennaway 		if (c->pkeys[i].publickey != NULL)
36574664626SKris Kennaway 			EVP_PKEY_free(c->pkeys[i].publickey);
36674664626SKris Kennaway #endif
36774664626SKris Kennaway 		}
368ddd58736SKris Kennaway 	OPENSSL_free(c);
36974664626SKris Kennaway 	}
37074664626SKris Kennaway 
37174664626SKris Kennaway int ssl_cert_inst(CERT **o)
37274664626SKris Kennaway 	{
37374664626SKris Kennaway 	/* Create a CERT if there isn't already one
37474664626SKris Kennaway 	 * (which cannot really happen, as it is initially created in
37574664626SKris Kennaway 	 * SSL_CTX_new; but the earlier code usually allows for that one
37674664626SKris Kennaway 	 * being non-existant, so we follow that behaviour, as it might
37774664626SKris Kennaway 	 * turn out that there actually is a reason for it -- but I'm
37874664626SKris Kennaway 	 * not sure that *all* of the existing code could cope with
37974664626SKris Kennaway 	 * s->cert being NULL, otherwise we could do without the
38074664626SKris Kennaway 	 * initialization in SSL_CTX_new).
38174664626SKris Kennaway 	 */
38274664626SKris Kennaway 
38374664626SKris Kennaway 	if (o == NULL)
38474664626SKris Kennaway 		{
38574664626SKris Kennaway 		SSLerr(SSL_F_SSL_CERT_INST, ERR_R_PASSED_NULL_PARAMETER);
38674664626SKris Kennaway 		return(0);
38774664626SKris Kennaway 		}
38874664626SKris Kennaway 	if (*o == NULL)
38974664626SKris Kennaway 		{
39074664626SKris Kennaway 		if ((*o = ssl_cert_new()) == NULL)
39174664626SKris Kennaway 			{
39274664626SKris Kennaway 			SSLerr(SSL_F_SSL_CERT_INST, ERR_R_MALLOC_FAILURE);
39374664626SKris Kennaway 			return(0);
39474664626SKris Kennaway 			}
39574664626SKris Kennaway 		}
39674664626SKris Kennaway 	return(1);
39774664626SKris Kennaway 	}
39874664626SKris Kennaway 
39974664626SKris Kennaway 
40074664626SKris Kennaway SESS_CERT *ssl_sess_cert_new(void)
40174664626SKris Kennaway 	{
40274664626SKris Kennaway 	SESS_CERT *ret;
40374664626SKris Kennaway 
404ddd58736SKris Kennaway 	ret = OPENSSL_malloc(sizeof *ret);
40574664626SKris Kennaway 	if (ret == NULL)
40674664626SKris Kennaway 		{
40774664626SKris Kennaway 		SSLerr(SSL_F_SSL_SESS_CERT_NEW, ERR_R_MALLOC_FAILURE);
40874664626SKris Kennaway 		return NULL;
40974664626SKris Kennaway 		}
41074664626SKris Kennaway 
41174664626SKris Kennaway 	memset(ret, 0 ,sizeof *ret);
41274664626SKris Kennaway 	ret->peer_key = &(ret->peer_pkeys[SSL_PKEY_RSA_ENC]);
41374664626SKris Kennaway 	ret->references = 1;
41474664626SKris Kennaway 
41574664626SKris Kennaway 	return ret;
41674664626SKris Kennaway 	}
41774664626SKris Kennaway 
41874664626SKris Kennaway void ssl_sess_cert_free(SESS_CERT *sc)
41974664626SKris Kennaway 	{
42074664626SKris Kennaway 	int i;
42174664626SKris Kennaway 
42274664626SKris Kennaway 	if (sc == NULL)
42374664626SKris Kennaway 		return;
42474664626SKris Kennaway 
42574664626SKris Kennaway 	i = CRYPTO_add(&sc->references, -1, CRYPTO_LOCK_SSL_SESS_CERT);
42674664626SKris Kennaway #ifdef REF_PRINT
42774664626SKris Kennaway 	REF_PRINT("SESS_CERT", sc);
42874664626SKris Kennaway #endif
42974664626SKris Kennaway 	if (i > 0)
43074664626SKris Kennaway 		return;
43174664626SKris Kennaway #ifdef REF_CHECK
43274664626SKris Kennaway 	if (i < 0)
43374664626SKris Kennaway 		{
43474664626SKris Kennaway 		fprintf(stderr,"ssl_sess_cert_free, bad reference count\n");
43574664626SKris Kennaway 		abort(); /* ok */
43674664626SKris Kennaway 		}
43774664626SKris Kennaway #endif
43874664626SKris Kennaway 
43974664626SKris Kennaway 	/* i == 0 */
44074664626SKris Kennaway 	if (sc->cert_chain != NULL)
44174664626SKris Kennaway 		sk_X509_pop_free(sc->cert_chain, X509_free);
44274664626SKris Kennaway 	for (i = 0; i < SSL_PKEY_NUM; i++)
44374664626SKris Kennaway 		{
44474664626SKris Kennaway 		if (sc->peer_pkeys[i].x509 != NULL)
44574664626SKris Kennaway 			X509_free(sc->peer_pkeys[i].x509);
44674664626SKris Kennaway #if 0 /* We don't have the peer's private key.  These lines are just
44774664626SKris Kennaway 	   * here as a reminder that we're still using a not-quite-appropriate
44874664626SKris Kennaway 	   * data structure. */
44974664626SKris Kennaway 		if (sc->peer_pkeys[i].privatekey != NULL)
45074664626SKris Kennaway 			EVP_PKEY_free(sc->peer_pkeys[i].privatekey);
45174664626SKris Kennaway #endif
45274664626SKris Kennaway 		}
45374664626SKris Kennaway 
4545c87c606SMark Murray #ifndef OPENSSL_NO_RSA
45574664626SKris Kennaway 	if (sc->peer_rsa_tmp != NULL)
45674664626SKris Kennaway 		RSA_free(sc->peer_rsa_tmp);
45774664626SKris Kennaway #endif
4585c87c606SMark Murray #ifndef OPENSSL_NO_DH
45974664626SKris Kennaway 	if (sc->peer_dh_tmp != NULL)
46074664626SKris Kennaway 		DH_free(sc->peer_dh_tmp);
46174664626SKris Kennaway #endif
4623b4e3dcbSSimon L. B. Nielsen #ifndef OPENSSL_NO_ECDH
4633b4e3dcbSSimon L. B. Nielsen 	if (sc->peer_ecdh_tmp != NULL)
4643b4e3dcbSSimon L. B. Nielsen 		EC_KEY_free(sc->peer_ecdh_tmp);
4653b4e3dcbSSimon L. B. Nielsen #endif
46674664626SKris Kennaway 
467ddd58736SKris Kennaway 	OPENSSL_free(sc);
46874664626SKris Kennaway 	}
46974664626SKris Kennaway 
47074664626SKris Kennaway int ssl_set_peer_cert_type(SESS_CERT *sc,int type)
47174664626SKris Kennaway 	{
47274664626SKris Kennaway 	sc->peer_cert_type = type;
47374664626SKris Kennaway 	return(1);
47474664626SKris Kennaway 	}
47574664626SKris Kennaway 
47674664626SKris Kennaway int ssl_verify_cert_chain(SSL *s,STACK_OF(X509) *sk)
47774664626SKris Kennaway 	{
47874664626SKris Kennaway 	X509 *x;
47974664626SKris Kennaway 	int i;
48074664626SKris Kennaway 	X509_STORE_CTX ctx;
48174664626SKris Kennaway 
48274664626SKris Kennaway 	if ((sk == NULL) || (sk_X509_num(sk) == 0))
48374664626SKris Kennaway 		return(0);
48474664626SKris Kennaway 
48574664626SKris Kennaway 	x=sk_X509_value(sk,0);
4865c87c606SMark Murray 	if(!X509_STORE_CTX_init(&ctx,s->ctx->cert_store,x,sk))
4875c87c606SMark Murray 		{
4885c87c606SMark Murray 		SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN,ERR_R_X509_LIB);
4895c87c606SMark Murray 		return(0);
4905c87c606SMark Murray 		}
4913b4e3dcbSSimon L. B. Nielsen 	if (s->param)
4923b4e3dcbSSimon L. B. Nielsen 		X509_VERIFY_PARAM_inherit(X509_STORE_CTX_get0_param(&ctx),
4933b4e3dcbSSimon L. B. Nielsen 						s->param);
4943b4e3dcbSSimon L. B. Nielsen #if 0
49574664626SKris Kennaway 	if (SSL_get_verify_depth(s) >= 0)
49674664626SKris Kennaway 		X509_STORE_CTX_set_depth(&ctx, SSL_get_verify_depth(s));
4973b4e3dcbSSimon L. B. Nielsen #endif
498f579bf8eSKris Kennaway 	X509_STORE_CTX_set_ex_data(&ctx,SSL_get_ex_data_X509_STORE_CTX_idx(),s);
4995c87c606SMark Murray 
5003b4e3dcbSSimon L. B. Nielsen 	/* We need to inherit the verify parameters. These can be determined by
501f579bf8eSKris Kennaway 	 * the context: if its a server it will verify SSL client certificates
502f579bf8eSKris Kennaway 	 * or vice versa.
503f579bf8eSKris Kennaway 	 */
504f579bf8eSKris Kennaway 
5053b4e3dcbSSimon L. B. Nielsen 	X509_STORE_CTX_set_default(&ctx,
5063b4e3dcbSSimon L. B. Nielsen 				s->server ? "ssl_client" : "ssl_server");
50774664626SKris Kennaway 
508a21b1b38SKris Kennaway 	if (s->verify_callback)
509a21b1b38SKris Kennaway 		X509_STORE_CTX_set_verify_cb(&ctx, s->verify_callback);
510a21b1b38SKris Kennaway 
51174664626SKris Kennaway 	if (s->ctx->app_verify_callback != NULL)
5125c87c606SMark Murray #if 1 /* new with OpenSSL 0.9.7 */
5135c87c606SMark Murray 		i=s->ctx->app_verify_callback(&ctx, s->ctx->app_verify_arg);
5145c87c606SMark Murray #else
51574664626SKris Kennaway 		i=s->ctx->app_verify_callback(&ctx); /* should pass app_verify_arg */
5165c87c606SMark Murray #endif
51774664626SKris Kennaway 	else
51874664626SKris Kennaway 		{
5195c87c606SMark Murray #ifndef OPENSSL_NO_X509_VERIFY
52074664626SKris Kennaway 		i=X509_verify_cert(&ctx);
52174664626SKris Kennaway #else
52274664626SKris Kennaway 		i=0;
52374664626SKris Kennaway 		ctx.error=X509_V_ERR_APPLICATION_VERIFICATION;
52474664626SKris Kennaway 		SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN,SSL_R_NO_VERIFY_CALLBACK);
52574664626SKris Kennaway #endif
52674664626SKris Kennaway 		}
52774664626SKris Kennaway 
52874664626SKris Kennaway 	s->verify_result=ctx.error;
52974664626SKris Kennaway 	X509_STORE_CTX_cleanup(&ctx);
53074664626SKris Kennaway 
53174664626SKris Kennaway 	return(i);
53274664626SKris Kennaway 	}
53374664626SKris Kennaway 
534ced566fdSJacques Vidrine static void set_client_CA_list(STACK_OF(X509_NAME) **ca_list,STACK_OF(X509_NAME) *name_list)
53574664626SKris Kennaway 	{
53674664626SKris Kennaway 	if (*ca_list != NULL)
53774664626SKris Kennaway 		sk_X509_NAME_pop_free(*ca_list,X509_NAME_free);
53874664626SKris Kennaway 
539ced566fdSJacques Vidrine 	*ca_list=name_list;
54074664626SKris Kennaway 	}
54174664626SKris Kennaway 
54274664626SKris Kennaway STACK_OF(X509_NAME) *SSL_dup_CA_list(STACK_OF(X509_NAME) *sk)
54374664626SKris Kennaway 	{
54474664626SKris Kennaway 	int i;
54574664626SKris Kennaway 	STACK_OF(X509_NAME) *ret;
54674664626SKris Kennaway 	X509_NAME *name;
54774664626SKris Kennaway 
54874664626SKris Kennaway 	ret=sk_X509_NAME_new_null();
54974664626SKris Kennaway 	for (i=0; i<sk_X509_NAME_num(sk); i++)
55074664626SKris Kennaway 		{
55174664626SKris Kennaway 		name=X509_NAME_dup(sk_X509_NAME_value(sk,i));
55274664626SKris Kennaway 		if ((name == NULL) || !sk_X509_NAME_push(ret,name))
55374664626SKris Kennaway 			{
55474664626SKris Kennaway 			sk_X509_NAME_pop_free(ret,X509_NAME_free);
55574664626SKris Kennaway 			return(NULL);
55674664626SKris Kennaway 			}
55774664626SKris Kennaway 		}
55874664626SKris Kennaway 	return(ret);
55974664626SKris Kennaway 	}
56074664626SKris Kennaway 
561ced566fdSJacques Vidrine void SSL_set_client_CA_list(SSL *s,STACK_OF(X509_NAME) *name_list)
56274664626SKris Kennaway 	{
563ced566fdSJacques Vidrine 	set_client_CA_list(&(s->client_CA),name_list);
56474664626SKris Kennaway 	}
56574664626SKris Kennaway 
566ced566fdSJacques Vidrine void SSL_CTX_set_client_CA_list(SSL_CTX *ctx,STACK_OF(X509_NAME) *name_list)
56774664626SKris Kennaway 	{
568ced566fdSJacques Vidrine 	set_client_CA_list(&(ctx->client_CA),name_list);
56974664626SKris Kennaway 	}
57074664626SKris Kennaway 
5713b4e3dcbSSimon L. B. Nielsen STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx)
57274664626SKris Kennaway 	{
57374664626SKris Kennaway 	return(ctx->client_CA);
57474664626SKris Kennaway 	}
57574664626SKris Kennaway 
5763b4e3dcbSSimon L. B. Nielsen STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *s)
57774664626SKris Kennaway 	{
57874664626SKris Kennaway 	if (s->type == SSL_ST_CONNECT)
57974664626SKris Kennaway 		{ /* we are in the client */
58074664626SKris Kennaway 		if (((s->version>>8) == SSL3_VERSION_MAJOR) &&
58174664626SKris Kennaway 			(s->s3 != NULL))
58274664626SKris Kennaway 			return(s->s3->tmp.ca_names);
58374664626SKris Kennaway 		else
58474664626SKris Kennaway 			return(NULL);
58574664626SKris Kennaway 		}
58674664626SKris Kennaway 	else
58774664626SKris Kennaway 		{
58874664626SKris Kennaway 		if (s->client_CA != NULL)
58974664626SKris Kennaway 			return(s->client_CA);
59074664626SKris Kennaway 		else
59174664626SKris Kennaway 			return(s->ctx->client_CA);
59274664626SKris Kennaway 		}
59374664626SKris Kennaway 	}
59474664626SKris Kennaway 
59574664626SKris Kennaway static int add_client_CA(STACK_OF(X509_NAME) **sk,X509 *x)
59674664626SKris Kennaway 	{
59774664626SKris Kennaway 	X509_NAME *name;
59874664626SKris Kennaway 
59974664626SKris Kennaway 	if (x == NULL) return(0);
60074664626SKris Kennaway 	if ((*sk == NULL) && ((*sk=sk_X509_NAME_new_null()) == NULL))
60174664626SKris Kennaway 		return(0);
60274664626SKris Kennaway 
60374664626SKris Kennaway 	if ((name=X509_NAME_dup(X509_get_subject_name(x))) == NULL)
60474664626SKris Kennaway 		return(0);
60574664626SKris Kennaway 
60674664626SKris Kennaway 	if (!sk_X509_NAME_push(*sk,name))
60774664626SKris Kennaway 		{
60874664626SKris Kennaway 		X509_NAME_free(name);
60974664626SKris Kennaway 		return(0);
61074664626SKris Kennaway 		}
61174664626SKris Kennaway 	return(1);
61274664626SKris Kennaway 	}
61374664626SKris Kennaway 
61474664626SKris Kennaway int SSL_add_client_CA(SSL *ssl,X509 *x)
61574664626SKris Kennaway 	{
61674664626SKris Kennaway 	return(add_client_CA(&(ssl->client_CA),x));
61774664626SKris Kennaway 	}
61874664626SKris Kennaway 
61974664626SKris Kennaway int SSL_CTX_add_client_CA(SSL_CTX *ctx,X509 *x)
62074664626SKris Kennaway 	{
62174664626SKris Kennaway 	return(add_client_CA(&(ctx->client_CA),x));
62274664626SKris Kennaway 	}
62374664626SKris Kennaway 
624ddd58736SKris Kennaway static int xname_cmp(const X509_NAME * const *a, const X509_NAME * const *b)
62574664626SKris Kennaway 	{
62674664626SKris Kennaway 	return(X509_NAME_cmp(*a,*b));
62774664626SKris Kennaway 	}
62874664626SKris Kennaway 
6295c87c606SMark Murray #ifndef OPENSSL_NO_STDIO
63074664626SKris Kennaway /*!
63174664626SKris Kennaway  * Load CA certs from a file into a ::STACK. Note that it is somewhat misnamed;
63274664626SKris Kennaway  * it doesn't really have anything to do with clients (except that a common use
63374664626SKris Kennaway  * for a stack of CAs is to send it to the client). Actually, it doesn't have
63474664626SKris Kennaway  * much to do with CAs, either, since it will load any old cert.
63574664626SKris Kennaway  * \param file the file containing one or more certs.
63674664626SKris Kennaway  * \return a ::STACK containing the certs.
63774664626SKris Kennaway  */
63874664626SKris Kennaway STACK_OF(X509_NAME) *SSL_load_client_CA_file(const char *file)
63974664626SKris Kennaway 	{
64074664626SKris Kennaway 	BIO *in;
64174664626SKris Kennaway 	X509 *x=NULL;
64274664626SKris Kennaway 	X509_NAME *xn=NULL;
6433b4e3dcbSSimon L. B. Nielsen 	STACK_OF(X509_NAME) *ret = NULL,*sk;
64474664626SKris Kennaway 
645f579bf8eSKris Kennaway 	sk=sk_X509_NAME_new(xname_cmp);
64674664626SKris Kennaway 
64774664626SKris Kennaway 	in=BIO_new(BIO_s_file_internal());
64874664626SKris Kennaway 
6493b4e3dcbSSimon L. B. Nielsen 	if ((sk == NULL) || (in == NULL))
65074664626SKris Kennaway 		{
65174664626SKris Kennaway 		SSLerr(SSL_F_SSL_LOAD_CLIENT_CA_FILE,ERR_R_MALLOC_FAILURE);
65274664626SKris Kennaway 		goto err;
65374664626SKris Kennaway 		}
65474664626SKris Kennaway 
65574664626SKris Kennaway 	if (!BIO_read_filename(in,file))
65674664626SKris Kennaway 		goto err;
65774664626SKris Kennaway 
65874664626SKris Kennaway 	for (;;)
65974664626SKris Kennaway 		{
66074664626SKris Kennaway 		if (PEM_read_bio_X509(in,&x,NULL,NULL) == NULL)
66174664626SKris Kennaway 			break;
6623b4e3dcbSSimon L. B. Nielsen 		if (ret == NULL)
6633b4e3dcbSSimon L. B. Nielsen 			{
6643b4e3dcbSSimon L. B. Nielsen 			ret = sk_X509_NAME_new_null();
6653b4e3dcbSSimon L. B. Nielsen 			if (ret == NULL)
6663b4e3dcbSSimon L. B. Nielsen 				{
6673b4e3dcbSSimon L. B. Nielsen 				SSLerr(SSL_F_SSL_LOAD_CLIENT_CA_FILE,ERR_R_MALLOC_FAILURE);
6683b4e3dcbSSimon L. B. Nielsen 				goto err;
6693b4e3dcbSSimon L. B. Nielsen 				}
6703b4e3dcbSSimon L. B. Nielsen 			}
67174664626SKris Kennaway 		if ((xn=X509_get_subject_name(x)) == NULL) goto err;
67274664626SKris Kennaway 		/* check for duplicates */
67374664626SKris Kennaway 		xn=X509_NAME_dup(xn);
67474664626SKris Kennaway 		if (xn == NULL) goto err;
67574664626SKris Kennaway 		if (sk_X509_NAME_find(sk,xn) >= 0)
67674664626SKris Kennaway 			X509_NAME_free(xn);
67774664626SKris Kennaway 		else
67874664626SKris Kennaway 			{
67974664626SKris Kennaway 			sk_X509_NAME_push(sk,xn);
68074664626SKris Kennaway 			sk_X509_NAME_push(ret,xn);
68174664626SKris Kennaway 			}
68274664626SKris Kennaway 		}
68374664626SKris Kennaway 
68474664626SKris Kennaway 	if (0)
68574664626SKris Kennaway 		{
68674664626SKris Kennaway err:
68774664626SKris Kennaway 		if (ret != NULL) sk_X509_NAME_pop_free(ret,X509_NAME_free);
68874664626SKris Kennaway 		ret=NULL;
68974664626SKris Kennaway 		}
69074664626SKris Kennaway 	if (sk != NULL) sk_X509_NAME_free(sk);
69174664626SKris Kennaway 	if (in != NULL) BIO_free(in);
69274664626SKris Kennaway 	if (x != NULL) X509_free(x);
6933b4e3dcbSSimon L. B. Nielsen 	if (ret != NULL)
6943b4e3dcbSSimon L. B. Nielsen 		ERR_clear_error();
69574664626SKris Kennaway 	return(ret);
69674664626SKris Kennaway 	}
69774664626SKris Kennaway #endif
69874664626SKris Kennaway 
69974664626SKris Kennaway /*!
70074664626SKris Kennaway  * Add a file of certs to a stack.
70174664626SKris Kennaway  * \param stack the stack to add to.
70274664626SKris Kennaway  * \param file the file to add from. All certs in this file that are not
70374664626SKris Kennaway  * already in the stack will be added.
70474664626SKris Kennaway  * \return 1 for success, 0 for failure. Note that in the case of failure some
70574664626SKris Kennaway  * certs may have been added to \c stack.
70674664626SKris Kennaway  */
70774664626SKris Kennaway 
70874664626SKris Kennaway int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
70974664626SKris Kennaway 					const char *file)
71074664626SKris Kennaway 	{
71174664626SKris Kennaway 	BIO *in;
71274664626SKris Kennaway 	X509 *x=NULL;
71374664626SKris Kennaway 	X509_NAME *xn=NULL;
71474664626SKris Kennaway 	int ret=1;
715ddd58736SKris Kennaway 	int (*oldcmp)(const X509_NAME * const *a, const X509_NAME * const *b);
71674664626SKris Kennaway 
717f579bf8eSKris Kennaway 	oldcmp=sk_X509_NAME_set_cmp_func(stack,xname_cmp);
71874664626SKris Kennaway 
71974664626SKris Kennaway 	in=BIO_new(BIO_s_file_internal());
72074664626SKris Kennaway 
72174664626SKris Kennaway 	if (in == NULL)
72274664626SKris Kennaway 		{
72374664626SKris Kennaway 		SSLerr(SSL_F_SSL_ADD_FILE_CERT_SUBJECTS_TO_STACK,ERR_R_MALLOC_FAILURE);
72474664626SKris Kennaway 		goto err;
72574664626SKris Kennaway 		}
72674664626SKris Kennaway 
72774664626SKris Kennaway 	if (!BIO_read_filename(in,file))
72874664626SKris Kennaway 		goto err;
72974664626SKris Kennaway 
73074664626SKris Kennaway 	for (;;)
73174664626SKris Kennaway 		{
73274664626SKris Kennaway 		if (PEM_read_bio_X509(in,&x,NULL,NULL) == NULL)
73374664626SKris Kennaway 			break;
73474664626SKris Kennaway 		if ((xn=X509_get_subject_name(x)) == NULL) goto err;
73574664626SKris Kennaway 		xn=X509_NAME_dup(xn);
73674664626SKris Kennaway 		if (xn == NULL) goto err;
73774664626SKris Kennaway 		if (sk_X509_NAME_find(stack,xn) >= 0)
73874664626SKris Kennaway 			X509_NAME_free(xn);
73974664626SKris Kennaway 		else
74074664626SKris Kennaway 			sk_X509_NAME_push(stack,xn);
74174664626SKris Kennaway 		}
74274664626SKris Kennaway 
74374664626SKris Kennaway 	if (0)
74474664626SKris Kennaway 		{
74574664626SKris Kennaway err:
74674664626SKris Kennaway 		ret=0;
74774664626SKris Kennaway 		}
74874664626SKris Kennaway 	if(in != NULL)
74974664626SKris Kennaway 		BIO_free(in);
75074664626SKris Kennaway 	if(x != NULL)
75174664626SKris Kennaway 		X509_free(x);
75274664626SKris Kennaway 
75374664626SKris Kennaway 	sk_X509_NAME_set_cmp_func(stack,oldcmp);
75474664626SKris Kennaway 
75574664626SKris Kennaway 	return ret;
75674664626SKris Kennaway 	}
75774664626SKris Kennaway 
75874664626SKris Kennaway /*!
75974664626SKris Kennaway  * Add a directory of certs to a stack.
76074664626SKris Kennaway  * \param stack the stack to append to.
76174664626SKris Kennaway  * \param dir the directory to append from. All files in this directory will be
76274664626SKris Kennaway  * examined as potential certs. Any that are acceptable to
76374664626SKris Kennaway  * SSL_add_dir_cert_subjects_to_stack() that are not already in the stack will be
76474664626SKris Kennaway  * included.
76574664626SKris Kennaway  * \return 1 for success, 0 for failure. Note that in the case of failure some
76674664626SKris Kennaway  * certs may have been added to \c stack.
76774664626SKris Kennaway  */
76874664626SKris Kennaway 
76974664626SKris Kennaway int SSL_add_dir_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
77074664626SKris Kennaway 				       const char *dir)
77174664626SKris Kennaway 	{
7723b4e3dcbSSimon L. B. Nielsen 	OPENSSL_DIR_CTX *d = NULL;
7733b4e3dcbSSimon L. B. Nielsen 	const char *filename;
77474664626SKris Kennaway 	int ret = 0;
77574664626SKris Kennaway 
77674664626SKris Kennaway 	CRYPTO_w_lock(CRYPTO_LOCK_READDIR);
77774664626SKris Kennaway 
77874664626SKris Kennaway 	/* Note that a side effect is that the CAs will be sorted by name */
77974664626SKris Kennaway 
7803b4e3dcbSSimon L. B. Nielsen 	while((filename = OPENSSL_DIR_read(&d, dir)))
78174664626SKris Kennaway 		{
78274664626SKris Kennaway 		char buf[1024];
783ddd58736SKris Kennaway 		int r;
78474664626SKris Kennaway 
7853b4e3dcbSSimon L. B. Nielsen 		if(strlen(dir)+strlen(filename)+2 > sizeof buf)
78674664626SKris Kennaway 			{
78774664626SKris Kennaway 			SSLerr(SSL_F_SSL_ADD_DIR_CERT_SUBJECTS_TO_STACK,SSL_R_PATH_TOO_LONG);
78874664626SKris Kennaway 			goto err;
78974664626SKris Kennaway 			}
79074664626SKris Kennaway 
7913b4e3dcbSSimon L. B. Nielsen #ifdef OPENSSL_SYS_VMS
7923b4e3dcbSSimon L. B. Nielsen 		r = BIO_snprintf(buf,sizeof buf,"%s%s",dir,filename);
7933b4e3dcbSSimon L. B. Nielsen #else
7943b4e3dcbSSimon L. B. Nielsen 		r = BIO_snprintf(buf,sizeof buf,"%s/%s",dir,filename);
7953b4e3dcbSSimon L. B. Nielsen #endif
7963b4e3dcbSSimon L. B. Nielsen 		if (r <= 0 || r >= (int)sizeof(buf))
797ddd58736SKris Kennaway 			goto err;
79874664626SKris Kennaway 		if(!SSL_add_file_cert_subjects_to_stack(stack,buf))
79974664626SKris Kennaway 			goto err;
80074664626SKris Kennaway 		}
80174664626SKris Kennaway 
8023b4e3dcbSSimon L. B. Nielsen 	if (errno)
8035c87c606SMark Murray 		{
8045c87c606SMark Murray 		SYSerr(SYS_F_OPENDIR, get_last_sys_error());
8053b4e3dcbSSimon L. B. Nielsen 		ERR_add_error_data(3, "OPENSSL_DIR_read(&ctx, '", dir, "')");
8065c87c606SMark Murray 		SSLerr(SSL_F_SSL_ADD_DIR_CERT_SUBJECTS_TO_STACK, ERR_R_SYS_LIB);
8075c87c606SMark Murray 		goto err;
8085c87c606SMark Murray 		}
8095c87c606SMark Murray 
8105c87c606SMark Murray 	ret = 1;
8115c87c606SMark Murray 
8125c87c606SMark Murray err:
8133b4e3dcbSSimon L. B. Nielsen 	if (d) OPENSSL_DIR_end(&d);
8145c87c606SMark Murray 	CRYPTO_w_unlock(CRYPTO_LOCK_READDIR);
8155c87c606SMark Murray 	return ret;
8165c87c606SMark Murray 	}
8175c87c606SMark Murray 
818