xref: /freebsd/crypto/openssl/ssl/ssl_cert.c (revision 1f13597d10e771d5546d31839150812bde8e4a56)
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 /* ====================================================================
59*1f13597dSJung-uk Kim  * Copyright (c) 1998-2007 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
76ed5d4f9aSSimon L. B. Nielsen  *    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
81ed5d4f9aSSimon L. B. Nielsen  *    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
90ed5d4f9aSSimon L. B. Nielsen  *    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  * ====================================================================
105ed5d4f9aSSimon L. B. Nielsen  *
106ed5d4f9aSSimon L. B. Nielsen  * This product includes cryptographic software written by Eric Young
107ed5d4f9aSSimon L. B. Nielsen  * (eay@cryptsoft.com).  This product includes software written by Tim
108ed5d4f9aSSimon L. B. Nielsen  * Hudson (tjh@cryptsoft.com).
109ed5d4f9aSSimon L. B. Nielsen  *
11074664626SKris Kennaway  */
1113b4e3dcbSSimon L. B. Nielsen /* ====================================================================
1123b4e3dcbSSimon L. B. Nielsen  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
1133b4e3dcbSSimon L. B. Nielsen  * ECC cipher suite support in OpenSSL originally developed by
1143b4e3dcbSSimon L. B. Nielsen  * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
1153b4e3dcbSSimon L. B. Nielsen  */
11674664626SKris Kennaway 
11774664626SKris Kennaway #include <stdio.h>
118f579bf8eSKris Kennaway 
1195c87c606SMark Murray #include "e_os.h"
120f579bf8eSKris Kennaway #ifndef NO_SYS_TYPES_H
12174664626SKris Kennaway # include <sys/types.h>
122f579bf8eSKris Kennaway #endif
123f579bf8eSKris Kennaway 
1243b4e3dcbSSimon L. B. Nielsen #include "o_dir.h"
12574664626SKris Kennaway #include <openssl/objects.h>
12674664626SKris Kennaway #include <openssl/bio.h>
12774664626SKris Kennaway #include <openssl/pem.h>
128f579bf8eSKris Kennaway #include <openssl/x509v3.h>
1293b4e3dcbSSimon L. B. Nielsen #ifndef OPENSSL_NO_DH
1303b4e3dcbSSimon L. B. Nielsen #include <openssl/dh.h>
1313b4e3dcbSSimon L. B. Nielsen #endif
1323b4e3dcbSSimon L. B. Nielsen #include <openssl/bn.h>
13374664626SKris Kennaway #include "ssl_locl.h"
13474664626SKris Kennaway 
13574664626SKris Kennaway int SSL_get_ex_data_X509_STORE_CTX_idx(void)
13674664626SKris Kennaway 	{
1375c87c606SMark Murray 	static volatile int ssl_x509_store_ctx_idx= -1;
138ed5d4f9aSSimon L. B. Nielsen 	int got_write_lock = 0;
139ed5d4f9aSSimon L. B. Nielsen 
140ed5d4f9aSSimon L. B. Nielsen 	CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
1415c87c606SMark Murray 
1425c87c606SMark Murray 	if (ssl_x509_store_ctx_idx < 0)
1435c87c606SMark Murray 		{
144ed5d4f9aSSimon L. B. Nielsen 		CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
1455c87c606SMark Murray 		CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
146ed5d4f9aSSimon L. B. Nielsen 		got_write_lock = 1;
14774664626SKris Kennaway 
14874664626SKris Kennaway 		if (ssl_x509_store_ctx_idx < 0)
14974664626SKris Kennaway 			{
15074664626SKris Kennaway 			ssl_x509_store_ctx_idx=X509_STORE_CTX_get_ex_new_index(
15174664626SKris Kennaway 				0,"SSL for verify callback",NULL,NULL,NULL);
15274664626SKris Kennaway 			}
1535c87c606SMark Murray 		}
154ed5d4f9aSSimon L. B. Nielsen 
155ed5d4f9aSSimon L. B. Nielsen 	if (got_write_lock)
156ed5d4f9aSSimon L. B. Nielsen 		CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
157ed5d4f9aSSimon L. B. Nielsen 	else
158ed5d4f9aSSimon L. B. Nielsen 		CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
159ed5d4f9aSSimon L. B. Nielsen 
1605c87c606SMark Murray 	return ssl_x509_store_ctx_idx;
16174664626SKris Kennaway 	}
16274664626SKris Kennaway 
163*1f13597dSJung-uk Kim static void ssl_cert_set_default_md(CERT *cert)
164*1f13597dSJung-uk Kim 	{
165*1f13597dSJung-uk Kim 	/* Set digest values to defaults */
166*1f13597dSJung-uk Kim #ifndef OPENSSL_NO_DSA
167*1f13597dSJung-uk Kim 	cert->pkeys[SSL_PKEY_DSA_SIGN].digest = EVP_dss1();
168*1f13597dSJung-uk Kim #endif
169*1f13597dSJung-uk Kim #ifndef OPENSSL_NO_RSA
170*1f13597dSJung-uk Kim 	cert->pkeys[SSL_PKEY_RSA_SIGN].digest = EVP_sha1();
171*1f13597dSJung-uk Kim 	cert->pkeys[SSL_PKEY_RSA_ENC].digest = EVP_sha1();
172*1f13597dSJung-uk Kim #endif
173*1f13597dSJung-uk Kim #ifndef OPENSSL_NO_ECDSA
174*1f13597dSJung-uk Kim 	cert->pkeys[SSL_PKEY_ECC].digest = EVP_ecdsa();
175*1f13597dSJung-uk Kim #endif
176*1f13597dSJung-uk Kim 	}
177*1f13597dSJung-uk Kim 
17874664626SKris Kennaway CERT *ssl_cert_new(void)
17974664626SKris Kennaway 	{
18074664626SKris Kennaway 	CERT *ret;
18174664626SKris Kennaway 
182ddd58736SKris Kennaway 	ret=(CERT *)OPENSSL_malloc(sizeof(CERT));
18374664626SKris Kennaway 	if (ret == NULL)
18474664626SKris Kennaway 		{
18574664626SKris Kennaway 		SSLerr(SSL_F_SSL_CERT_NEW,ERR_R_MALLOC_FAILURE);
18674664626SKris Kennaway 		return(NULL);
18774664626SKris Kennaway 		}
18874664626SKris Kennaway 	memset(ret,0,sizeof(CERT));
18974664626SKris Kennaway 
19074664626SKris Kennaway 	ret->key= &(ret->pkeys[SSL_PKEY_RSA_ENC]);
19174664626SKris Kennaway 	ret->references=1;
192*1f13597dSJung-uk Kim 	ssl_cert_set_default_md(ret);
19374664626SKris Kennaway 	return(ret);
19474664626SKris Kennaway 	}
19574664626SKris Kennaway 
19674664626SKris Kennaway CERT *ssl_cert_dup(CERT *cert)
19774664626SKris Kennaway 	{
19874664626SKris Kennaway 	CERT *ret;
19974664626SKris Kennaway 	int i;
20074664626SKris Kennaway 
201ddd58736SKris Kennaway 	ret = (CERT *)OPENSSL_malloc(sizeof(CERT));
20274664626SKris Kennaway 	if (ret == NULL)
20374664626SKris Kennaway 		{
20474664626SKris Kennaway 		SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_MALLOC_FAILURE);
20574664626SKris Kennaway 		return(NULL);
20674664626SKris Kennaway 		}
20774664626SKris Kennaway 
20874664626SKris Kennaway 	memset(ret, 0, sizeof(CERT));
20974664626SKris Kennaway 
21074664626SKris Kennaway 	ret->key = &ret->pkeys[cert->key - &cert->pkeys[0]];
21174664626SKris Kennaway 	/* or ret->key = ret->pkeys + (cert->key - cert->pkeys),
21274664626SKris Kennaway 	 * if you find that more readable */
21374664626SKris Kennaway 
21474664626SKris Kennaway 	ret->valid = cert->valid;
215*1f13597dSJung-uk Kim 	ret->mask_k = cert->mask_k;
216*1f13597dSJung-uk Kim 	ret->mask_a = cert->mask_a;
217*1f13597dSJung-uk Kim 	ret->export_mask_k = cert->export_mask_k;
218*1f13597dSJung-uk Kim 	ret->export_mask_a = cert->export_mask_a;
21974664626SKris Kennaway 
2205c87c606SMark Murray #ifndef OPENSSL_NO_RSA
22174664626SKris Kennaway 	if (cert->rsa_tmp != NULL)
22274664626SKris Kennaway 		{
2235c87c606SMark Murray 		RSA_up_ref(cert->rsa_tmp);
22474664626SKris Kennaway 		ret->rsa_tmp = cert->rsa_tmp;
22574664626SKris Kennaway 		}
22674664626SKris Kennaway 	ret->rsa_tmp_cb = cert->rsa_tmp_cb;
22774664626SKris Kennaway #endif
22874664626SKris Kennaway 
2295c87c606SMark Murray #ifndef OPENSSL_NO_DH
23074664626SKris Kennaway 	if (cert->dh_tmp != NULL)
23174664626SKris Kennaway 		{
23274664626SKris Kennaway 		ret->dh_tmp = DHparams_dup(cert->dh_tmp);
23374664626SKris Kennaway 		if (ret->dh_tmp == NULL)
23474664626SKris Kennaway 			{
235f579bf8eSKris Kennaway 			SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_DH_LIB);
23674664626SKris Kennaway 			goto err;
23774664626SKris Kennaway 			}
238f579bf8eSKris Kennaway 		if (cert->dh_tmp->priv_key)
239f579bf8eSKris Kennaway 			{
240f579bf8eSKris Kennaway 			BIGNUM *b = BN_dup(cert->dh_tmp->priv_key);
241f579bf8eSKris Kennaway 			if (!b)
242f579bf8eSKris Kennaway 				{
243f579bf8eSKris Kennaway 				SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_BN_LIB);
244f579bf8eSKris Kennaway 				goto err;
245f579bf8eSKris Kennaway 				}
246f579bf8eSKris Kennaway 			ret->dh_tmp->priv_key = b;
247f579bf8eSKris Kennaway 			}
248f579bf8eSKris Kennaway 		if (cert->dh_tmp->pub_key)
249f579bf8eSKris Kennaway 			{
250f579bf8eSKris Kennaway 			BIGNUM *b = BN_dup(cert->dh_tmp->pub_key);
251f579bf8eSKris Kennaway 			if (!b)
252f579bf8eSKris Kennaway 				{
253f579bf8eSKris Kennaway 				SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_BN_LIB);
254f579bf8eSKris Kennaway 				goto err;
255f579bf8eSKris Kennaway 				}
256f579bf8eSKris Kennaway 			ret->dh_tmp->pub_key = b;
257f579bf8eSKris Kennaway 			}
25874664626SKris Kennaway 		}
25974664626SKris Kennaway 	ret->dh_tmp_cb = cert->dh_tmp_cb;
26074664626SKris Kennaway #endif
26174664626SKris Kennaway 
2623b4e3dcbSSimon L. B. Nielsen #ifndef OPENSSL_NO_ECDH
2633b4e3dcbSSimon L. B. Nielsen 	if (cert->ecdh_tmp)
2643b4e3dcbSSimon L. B. Nielsen 		{
2653b4e3dcbSSimon L. B. Nielsen 		ret->ecdh_tmp = EC_KEY_dup(cert->ecdh_tmp);
2663b4e3dcbSSimon L. B. Nielsen 		if (ret->ecdh_tmp == NULL)
2673b4e3dcbSSimon L. B. Nielsen 			{
2683b4e3dcbSSimon L. B. Nielsen 			SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_EC_LIB);
2693b4e3dcbSSimon L. B. Nielsen 			goto err;
2703b4e3dcbSSimon L. B. Nielsen 			}
2713b4e3dcbSSimon L. B. Nielsen 		}
2723b4e3dcbSSimon L. B. Nielsen 	ret->ecdh_tmp_cb = cert->ecdh_tmp_cb;
2733b4e3dcbSSimon L. B. Nielsen #endif
2743b4e3dcbSSimon L. B. Nielsen 
27574664626SKris Kennaway 	for (i = 0; i < SSL_PKEY_NUM; i++)
27674664626SKris Kennaway 		{
27774664626SKris Kennaway 		if (cert->pkeys[i].x509 != NULL)
27874664626SKris Kennaway 			{
27974664626SKris Kennaway 			ret->pkeys[i].x509 = cert->pkeys[i].x509;
28074664626SKris Kennaway 			CRYPTO_add(&ret->pkeys[i].x509->references, 1,
28174664626SKris Kennaway 				CRYPTO_LOCK_X509);
28274664626SKris Kennaway 			}
28374664626SKris Kennaway 
28474664626SKris Kennaway 		if (cert->pkeys[i].privatekey != NULL)
28574664626SKris Kennaway 			{
28674664626SKris Kennaway 			ret->pkeys[i].privatekey = cert->pkeys[i].privatekey;
28774664626SKris Kennaway 			CRYPTO_add(&ret->pkeys[i].privatekey->references, 1,
28874664626SKris Kennaway 				CRYPTO_LOCK_EVP_PKEY);
28974664626SKris Kennaway 
29074664626SKris Kennaway 			switch(i)
29174664626SKris Kennaway 				{
29274664626SKris Kennaway 				/* If there was anything special to do for
29374664626SKris Kennaway 				 * certain types of keys, we'd do it here.
29474664626SKris Kennaway 				 * (Nothing at the moment, I think.) */
29574664626SKris Kennaway 
29674664626SKris Kennaway 			case SSL_PKEY_RSA_ENC:
29774664626SKris Kennaway 			case SSL_PKEY_RSA_SIGN:
29874664626SKris Kennaway 				/* We have an RSA key. */
29974664626SKris Kennaway 				break;
30074664626SKris Kennaway 
30174664626SKris Kennaway 			case SSL_PKEY_DSA_SIGN:
30274664626SKris Kennaway 				/* We have a DSA key. */
30374664626SKris Kennaway 				break;
30474664626SKris Kennaway 
30574664626SKris Kennaway 			case SSL_PKEY_DH_RSA:
30674664626SKris Kennaway 			case SSL_PKEY_DH_DSA:
30774664626SKris Kennaway 				/* We have a DH key. */
30874664626SKris Kennaway 				break;
30974664626SKris Kennaway 
3103b4e3dcbSSimon L. B. Nielsen 			case SSL_PKEY_ECC:
3113b4e3dcbSSimon L. B. Nielsen 				/* We have an ECC key */
3123b4e3dcbSSimon L. B. Nielsen 				break;
3133b4e3dcbSSimon L. B. Nielsen 
31474664626SKris Kennaway 			default:
31574664626SKris Kennaway 				/* Can't happen. */
31674664626SKris Kennaway 				SSLerr(SSL_F_SSL_CERT_DUP, SSL_R_LIBRARY_BUG);
31774664626SKris Kennaway 				}
31874664626SKris Kennaway 			}
31974664626SKris Kennaway 		}
32074664626SKris Kennaway 
32174664626SKris Kennaway 	/* ret->extra_certs *should* exist, but currently the own certificate
32274664626SKris Kennaway 	 * chain is held inside SSL_CTX */
32374664626SKris Kennaway 
32474664626SKris Kennaway 	ret->references=1;
325*1f13597dSJung-uk Kim 	/* Set digests to defaults. NB: we don't copy existing values as they
326*1f13597dSJung-uk Kim 	 * will be set during handshake.
327*1f13597dSJung-uk Kim 	 */
328*1f13597dSJung-uk Kim 	ssl_cert_set_default_md(ret);
32974664626SKris Kennaway 
33074664626SKris Kennaway 	return(ret);
33174664626SKris Kennaway 
3323b4e3dcbSSimon L. B. Nielsen #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_ECDH)
33374664626SKris Kennaway err:
3345740a5e3SKris Kennaway #endif
3355c87c606SMark Murray #ifndef OPENSSL_NO_RSA
33674664626SKris Kennaway 	if (ret->rsa_tmp != NULL)
33774664626SKris Kennaway 		RSA_free(ret->rsa_tmp);
33874664626SKris Kennaway #endif
3395c87c606SMark Murray #ifndef OPENSSL_NO_DH
34074664626SKris Kennaway 	if (ret->dh_tmp != NULL)
34174664626SKris Kennaway 		DH_free(ret->dh_tmp);
34274664626SKris Kennaway #endif
3433b4e3dcbSSimon L. B. Nielsen #ifndef OPENSSL_NO_ECDH
3443b4e3dcbSSimon L. B. Nielsen 	if (ret->ecdh_tmp != NULL)
3453b4e3dcbSSimon L. B. Nielsen 		EC_KEY_free(ret->ecdh_tmp);
3463b4e3dcbSSimon L. B. Nielsen #endif
34774664626SKris Kennaway 
34874664626SKris Kennaway 	for (i = 0; i < SSL_PKEY_NUM; i++)
34974664626SKris Kennaway 		{
35074664626SKris Kennaway 		if (ret->pkeys[i].x509 != NULL)
35174664626SKris Kennaway 			X509_free(ret->pkeys[i].x509);
35274664626SKris Kennaway 		if (ret->pkeys[i].privatekey != NULL)
35374664626SKris Kennaway 			EVP_PKEY_free(ret->pkeys[i].privatekey);
35474664626SKris Kennaway 		}
35574664626SKris Kennaway 
35674664626SKris Kennaway 	return NULL;
35774664626SKris Kennaway 	}
35874664626SKris Kennaway 
35974664626SKris Kennaway 
36074664626SKris Kennaway void ssl_cert_free(CERT *c)
36174664626SKris Kennaway 	{
36274664626SKris Kennaway 	int i;
36374664626SKris Kennaway 
36474664626SKris Kennaway 	if(c == NULL)
36574664626SKris Kennaway 	    return;
36674664626SKris Kennaway 
36774664626SKris Kennaway 	i=CRYPTO_add(&c->references,-1,CRYPTO_LOCK_SSL_CERT);
36874664626SKris Kennaway #ifdef REF_PRINT
36974664626SKris Kennaway 	REF_PRINT("CERT",c);
37074664626SKris Kennaway #endif
37174664626SKris Kennaway 	if (i > 0) return;
37274664626SKris Kennaway #ifdef REF_CHECK
37374664626SKris Kennaway 	if (i < 0)
37474664626SKris Kennaway 		{
37574664626SKris Kennaway 		fprintf(stderr,"ssl_cert_free, bad reference count\n");
37674664626SKris Kennaway 		abort(); /* ok */
37774664626SKris Kennaway 		}
37874664626SKris Kennaway #endif
37974664626SKris Kennaway 
3805c87c606SMark Murray #ifndef OPENSSL_NO_RSA
38174664626SKris Kennaway 	if (c->rsa_tmp) RSA_free(c->rsa_tmp);
38274664626SKris Kennaway #endif
3835c87c606SMark Murray #ifndef OPENSSL_NO_DH
38474664626SKris Kennaway 	if (c->dh_tmp) DH_free(c->dh_tmp);
38574664626SKris Kennaway #endif
3863b4e3dcbSSimon L. B. Nielsen #ifndef OPENSSL_NO_ECDH
3873b4e3dcbSSimon L. B. Nielsen 	if (c->ecdh_tmp) EC_KEY_free(c->ecdh_tmp);
3883b4e3dcbSSimon L. B. Nielsen #endif
38974664626SKris Kennaway 
39074664626SKris Kennaway 	for (i=0; i<SSL_PKEY_NUM; i++)
39174664626SKris Kennaway 		{
39274664626SKris Kennaway 		if (c->pkeys[i].x509 != NULL)
39374664626SKris Kennaway 			X509_free(c->pkeys[i].x509);
39474664626SKris Kennaway 		if (c->pkeys[i].privatekey != NULL)
39574664626SKris Kennaway 			EVP_PKEY_free(c->pkeys[i].privatekey);
39674664626SKris Kennaway #if 0
39774664626SKris Kennaway 		if (c->pkeys[i].publickey != NULL)
39874664626SKris Kennaway 			EVP_PKEY_free(c->pkeys[i].publickey);
39974664626SKris Kennaway #endif
40074664626SKris Kennaway 		}
401ddd58736SKris Kennaway 	OPENSSL_free(c);
40274664626SKris Kennaway 	}
40374664626SKris Kennaway 
40474664626SKris Kennaway int ssl_cert_inst(CERT **o)
40574664626SKris Kennaway 	{
40674664626SKris Kennaway 	/* Create a CERT if there isn't already one
40774664626SKris Kennaway 	 * (which cannot really happen, as it is initially created in
40874664626SKris Kennaway 	 * SSL_CTX_new; but the earlier code usually allows for that one
40974664626SKris Kennaway 	 * being non-existant, so we follow that behaviour, as it might
41074664626SKris Kennaway 	 * turn out that there actually is a reason for it -- but I'm
41174664626SKris Kennaway 	 * not sure that *all* of the existing code could cope with
41274664626SKris Kennaway 	 * s->cert being NULL, otherwise we could do without the
41374664626SKris Kennaway 	 * initialization in SSL_CTX_new).
41474664626SKris Kennaway 	 */
41574664626SKris Kennaway 
41674664626SKris Kennaway 	if (o == NULL)
41774664626SKris Kennaway 		{
41874664626SKris Kennaway 		SSLerr(SSL_F_SSL_CERT_INST, ERR_R_PASSED_NULL_PARAMETER);
41974664626SKris Kennaway 		return(0);
42074664626SKris Kennaway 		}
42174664626SKris Kennaway 	if (*o == NULL)
42274664626SKris Kennaway 		{
42374664626SKris Kennaway 		if ((*o = ssl_cert_new()) == NULL)
42474664626SKris Kennaway 			{
42574664626SKris Kennaway 			SSLerr(SSL_F_SSL_CERT_INST, ERR_R_MALLOC_FAILURE);
42674664626SKris Kennaway 			return(0);
42774664626SKris Kennaway 			}
42874664626SKris Kennaway 		}
42974664626SKris Kennaway 	return(1);
43074664626SKris Kennaway 	}
43174664626SKris Kennaway 
43274664626SKris Kennaway 
43374664626SKris Kennaway SESS_CERT *ssl_sess_cert_new(void)
43474664626SKris Kennaway 	{
43574664626SKris Kennaway 	SESS_CERT *ret;
43674664626SKris Kennaway 
437ddd58736SKris Kennaway 	ret = OPENSSL_malloc(sizeof *ret);
43874664626SKris Kennaway 	if (ret == NULL)
43974664626SKris Kennaway 		{
44074664626SKris Kennaway 		SSLerr(SSL_F_SSL_SESS_CERT_NEW, ERR_R_MALLOC_FAILURE);
44174664626SKris Kennaway 		return NULL;
44274664626SKris Kennaway 		}
44374664626SKris Kennaway 
44474664626SKris Kennaway 	memset(ret, 0 ,sizeof *ret);
44574664626SKris Kennaway 	ret->peer_key = &(ret->peer_pkeys[SSL_PKEY_RSA_ENC]);
44674664626SKris Kennaway 	ret->references = 1;
44774664626SKris Kennaway 
44874664626SKris Kennaway 	return ret;
44974664626SKris Kennaway 	}
45074664626SKris Kennaway 
45174664626SKris Kennaway void ssl_sess_cert_free(SESS_CERT *sc)
45274664626SKris Kennaway 	{
45374664626SKris Kennaway 	int i;
45474664626SKris Kennaway 
45574664626SKris Kennaway 	if (sc == NULL)
45674664626SKris Kennaway 		return;
45774664626SKris Kennaway 
45874664626SKris Kennaway 	i = CRYPTO_add(&sc->references, -1, CRYPTO_LOCK_SSL_SESS_CERT);
45974664626SKris Kennaway #ifdef REF_PRINT
46074664626SKris Kennaway 	REF_PRINT("SESS_CERT", sc);
46174664626SKris Kennaway #endif
46274664626SKris Kennaway 	if (i > 0)
46374664626SKris Kennaway 		return;
46474664626SKris Kennaway #ifdef REF_CHECK
46574664626SKris Kennaway 	if (i < 0)
46674664626SKris Kennaway 		{
46774664626SKris Kennaway 		fprintf(stderr,"ssl_sess_cert_free, bad reference count\n");
46874664626SKris Kennaway 		abort(); /* ok */
46974664626SKris Kennaway 		}
47074664626SKris Kennaway #endif
47174664626SKris Kennaway 
47274664626SKris Kennaway 	/* i == 0 */
47374664626SKris Kennaway 	if (sc->cert_chain != NULL)
47474664626SKris Kennaway 		sk_X509_pop_free(sc->cert_chain, X509_free);
47574664626SKris Kennaway 	for (i = 0; i < SSL_PKEY_NUM; i++)
47674664626SKris Kennaway 		{
47774664626SKris Kennaway 		if (sc->peer_pkeys[i].x509 != NULL)
47874664626SKris Kennaway 			X509_free(sc->peer_pkeys[i].x509);
47974664626SKris Kennaway #if 0 /* We don't have the peer's private key.  These lines are just
48074664626SKris Kennaway 	   * here as a reminder that we're still using a not-quite-appropriate
48174664626SKris Kennaway 	   * data structure. */
48274664626SKris Kennaway 		if (sc->peer_pkeys[i].privatekey != NULL)
48374664626SKris Kennaway 			EVP_PKEY_free(sc->peer_pkeys[i].privatekey);
48474664626SKris Kennaway #endif
48574664626SKris Kennaway 		}
48674664626SKris Kennaway 
4875c87c606SMark Murray #ifndef OPENSSL_NO_RSA
48874664626SKris Kennaway 	if (sc->peer_rsa_tmp != NULL)
48974664626SKris Kennaway 		RSA_free(sc->peer_rsa_tmp);
49074664626SKris Kennaway #endif
4915c87c606SMark Murray #ifndef OPENSSL_NO_DH
49274664626SKris Kennaway 	if (sc->peer_dh_tmp != NULL)
49374664626SKris Kennaway 		DH_free(sc->peer_dh_tmp);
49474664626SKris Kennaway #endif
4953b4e3dcbSSimon L. B. Nielsen #ifndef OPENSSL_NO_ECDH
4963b4e3dcbSSimon L. B. Nielsen 	if (sc->peer_ecdh_tmp != NULL)
4973b4e3dcbSSimon L. B. Nielsen 		EC_KEY_free(sc->peer_ecdh_tmp);
4983b4e3dcbSSimon L. B. Nielsen #endif
49974664626SKris Kennaway 
500ddd58736SKris Kennaway 	OPENSSL_free(sc);
50174664626SKris Kennaway 	}
50274664626SKris Kennaway 
50374664626SKris Kennaway int ssl_set_peer_cert_type(SESS_CERT *sc,int type)
50474664626SKris Kennaway 	{
50574664626SKris Kennaway 	sc->peer_cert_type = type;
50674664626SKris Kennaway 	return(1);
50774664626SKris Kennaway 	}
50874664626SKris Kennaway 
50974664626SKris Kennaway int ssl_verify_cert_chain(SSL *s,STACK_OF(X509) *sk)
51074664626SKris Kennaway 	{
51174664626SKris Kennaway 	X509 *x;
51274664626SKris Kennaway 	int i;
51374664626SKris Kennaway 	X509_STORE_CTX ctx;
51474664626SKris Kennaway 
51574664626SKris Kennaway 	if ((sk == NULL) || (sk_X509_num(sk) == 0))
51674664626SKris Kennaway 		return(0);
51774664626SKris Kennaway 
51874664626SKris Kennaway 	x=sk_X509_value(sk,0);
5195c87c606SMark Murray 	if(!X509_STORE_CTX_init(&ctx,s->ctx->cert_store,x,sk))
5205c87c606SMark Murray 		{
5215c87c606SMark Murray 		SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN,ERR_R_X509_LIB);
5225c87c606SMark Murray 		return(0);
5235c87c606SMark Murray 		}
5243b4e3dcbSSimon L. B. Nielsen #if 0
52574664626SKris Kennaway 	if (SSL_get_verify_depth(s) >= 0)
52674664626SKris Kennaway 		X509_STORE_CTX_set_depth(&ctx, SSL_get_verify_depth(s));
5273b4e3dcbSSimon L. B. Nielsen #endif
528f579bf8eSKris Kennaway 	X509_STORE_CTX_set_ex_data(&ctx,SSL_get_ex_data_X509_STORE_CTX_idx(),s);
5295c87c606SMark Murray 
5303b4e3dcbSSimon L. B. Nielsen 	/* We need to inherit the verify parameters. These can be determined by
531f579bf8eSKris Kennaway 	 * the context: if its a server it will verify SSL client certificates
532f579bf8eSKris Kennaway 	 * or vice versa.
533f579bf8eSKris Kennaway 	 */
534f579bf8eSKris Kennaway 
5353b4e3dcbSSimon L. B. Nielsen 	X509_STORE_CTX_set_default(&ctx,
5363b4e3dcbSSimon L. B. Nielsen 				s->server ? "ssl_client" : "ssl_server");
5376a599222SSimon L. B. Nielsen 	/* Anything non-default in "param" should overwrite anything in the
5386a599222SSimon L. B. Nielsen 	 * ctx.
5396a599222SSimon L. B. Nielsen 	 */
5406a599222SSimon L. B. Nielsen 	X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(&ctx), s->param);
54174664626SKris Kennaway 
542a21b1b38SKris Kennaway 	if (s->verify_callback)
543a21b1b38SKris Kennaway 		X509_STORE_CTX_set_verify_cb(&ctx, s->verify_callback);
544a21b1b38SKris Kennaway 
54574664626SKris Kennaway 	if (s->ctx->app_verify_callback != NULL)
5465c87c606SMark Murray #if 1 /* new with OpenSSL 0.9.7 */
5475c87c606SMark Murray 		i=s->ctx->app_verify_callback(&ctx, s->ctx->app_verify_arg);
5485c87c606SMark Murray #else
54974664626SKris Kennaway 		i=s->ctx->app_verify_callback(&ctx); /* should pass app_verify_arg */
5505c87c606SMark Murray #endif
55174664626SKris Kennaway 	else
55274664626SKris Kennaway 		{
5535c87c606SMark Murray #ifndef OPENSSL_NO_X509_VERIFY
55474664626SKris Kennaway 		i=X509_verify_cert(&ctx);
55574664626SKris Kennaway #else
55674664626SKris Kennaway 		i=0;
55774664626SKris Kennaway 		ctx.error=X509_V_ERR_APPLICATION_VERIFICATION;
55874664626SKris Kennaway 		SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN,SSL_R_NO_VERIFY_CALLBACK);
55974664626SKris Kennaway #endif
56074664626SKris Kennaway 		}
56174664626SKris Kennaway 
56274664626SKris Kennaway 	s->verify_result=ctx.error;
56374664626SKris Kennaway 	X509_STORE_CTX_cleanup(&ctx);
56474664626SKris Kennaway 
56574664626SKris Kennaway 	return(i);
56674664626SKris Kennaway 	}
56774664626SKris Kennaway 
568ced566fdSJacques Vidrine static void set_client_CA_list(STACK_OF(X509_NAME) **ca_list,STACK_OF(X509_NAME) *name_list)
56974664626SKris Kennaway 	{
57074664626SKris Kennaway 	if (*ca_list != NULL)
57174664626SKris Kennaway 		sk_X509_NAME_pop_free(*ca_list,X509_NAME_free);
57274664626SKris Kennaway 
573ced566fdSJacques Vidrine 	*ca_list=name_list;
57474664626SKris Kennaway 	}
57574664626SKris Kennaway 
57674664626SKris Kennaway STACK_OF(X509_NAME) *SSL_dup_CA_list(STACK_OF(X509_NAME) *sk)
57774664626SKris Kennaway 	{
57874664626SKris Kennaway 	int i;
57974664626SKris Kennaway 	STACK_OF(X509_NAME) *ret;
58074664626SKris Kennaway 	X509_NAME *name;
58174664626SKris Kennaway 
58274664626SKris Kennaway 	ret=sk_X509_NAME_new_null();
58374664626SKris Kennaway 	for (i=0; i<sk_X509_NAME_num(sk); i++)
58474664626SKris Kennaway 		{
58574664626SKris Kennaway 		name=X509_NAME_dup(sk_X509_NAME_value(sk,i));
58674664626SKris Kennaway 		if ((name == NULL) || !sk_X509_NAME_push(ret,name))
58774664626SKris Kennaway 			{
58874664626SKris Kennaway 			sk_X509_NAME_pop_free(ret,X509_NAME_free);
58974664626SKris Kennaway 			return(NULL);
59074664626SKris Kennaway 			}
59174664626SKris Kennaway 		}
59274664626SKris Kennaway 	return(ret);
59374664626SKris Kennaway 	}
59474664626SKris Kennaway 
595ced566fdSJacques Vidrine void SSL_set_client_CA_list(SSL *s,STACK_OF(X509_NAME) *name_list)
59674664626SKris Kennaway 	{
597ced566fdSJacques Vidrine 	set_client_CA_list(&(s->client_CA),name_list);
59874664626SKris Kennaway 	}
59974664626SKris Kennaway 
600ced566fdSJacques Vidrine void SSL_CTX_set_client_CA_list(SSL_CTX *ctx,STACK_OF(X509_NAME) *name_list)
60174664626SKris Kennaway 	{
602ced566fdSJacques Vidrine 	set_client_CA_list(&(ctx->client_CA),name_list);
60374664626SKris Kennaway 	}
60474664626SKris Kennaway 
6053b4e3dcbSSimon L. B. Nielsen STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx)
60674664626SKris Kennaway 	{
60774664626SKris Kennaway 	return(ctx->client_CA);
60874664626SKris Kennaway 	}
60974664626SKris Kennaway 
6103b4e3dcbSSimon L. B. Nielsen STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *s)
61174664626SKris Kennaway 	{
61274664626SKris Kennaway 	if (s->type == SSL_ST_CONNECT)
61374664626SKris Kennaway 		{ /* we are in the client */
61474664626SKris Kennaway 		if (((s->version>>8) == SSL3_VERSION_MAJOR) &&
61574664626SKris Kennaway 			(s->s3 != NULL))
61674664626SKris Kennaway 			return(s->s3->tmp.ca_names);
61774664626SKris Kennaway 		else
61874664626SKris Kennaway 			return(NULL);
61974664626SKris Kennaway 		}
62074664626SKris Kennaway 	else
62174664626SKris Kennaway 		{
62274664626SKris Kennaway 		if (s->client_CA != NULL)
62374664626SKris Kennaway 			return(s->client_CA);
62474664626SKris Kennaway 		else
62574664626SKris Kennaway 			return(s->ctx->client_CA);
62674664626SKris Kennaway 		}
62774664626SKris Kennaway 	}
62874664626SKris Kennaway 
62974664626SKris Kennaway static int add_client_CA(STACK_OF(X509_NAME) **sk,X509 *x)
63074664626SKris Kennaway 	{
63174664626SKris Kennaway 	X509_NAME *name;
63274664626SKris Kennaway 
63374664626SKris Kennaway 	if (x == NULL) return(0);
63474664626SKris Kennaway 	if ((*sk == NULL) && ((*sk=sk_X509_NAME_new_null()) == NULL))
63574664626SKris Kennaway 		return(0);
63674664626SKris Kennaway 
63774664626SKris Kennaway 	if ((name=X509_NAME_dup(X509_get_subject_name(x))) == NULL)
63874664626SKris Kennaway 		return(0);
63974664626SKris Kennaway 
64074664626SKris Kennaway 	if (!sk_X509_NAME_push(*sk,name))
64174664626SKris Kennaway 		{
64274664626SKris Kennaway 		X509_NAME_free(name);
64374664626SKris Kennaway 		return(0);
64474664626SKris Kennaway 		}
64574664626SKris Kennaway 	return(1);
64674664626SKris Kennaway 	}
64774664626SKris Kennaway 
64874664626SKris Kennaway int SSL_add_client_CA(SSL *ssl,X509 *x)
64974664626SKris Kennaway 	{
65074664626SKris Kennaway 	return(add_client_CA(&(ssl->client_CA),x));
65174664626SKris Kennaway 	}
65274664626SKris Kennaway 
65374664626SKris Kennaway int SSL_CTX_add_client_CA(SSL_CTX *ctx,X509 *x)
65474664626SKris Kennaway 	{
65574664626SKris Kennaway 	return(add_client_CA(&(ctx->client_CA),x));
65674664626SKris Kennaway 	}
65774664626SKris Kennaway 
658ddd58736SKris Kennaway static int xname_cmp(const X509_NAME * const *a, const X509_NAME * const *b)
65974664626SKris Kennaway 	{
66074664626SKris Kennaway 	return(X509_NAME_cmp(*a,*b));
66174664626SKris Kennaway 	}
66274664626SKris Kennaway 
6635c87c606SMark Murray #ifndef OPENSSL_NO_STDIO
66474664626SKris Kennaway /*!
66574664626SKris Kennaway  * Load CA certs from a file into a ::STACK. Note that it is somewhat misnamed;
66674664626SKris Kennaway  * it doesn't really have anything to do with clients (except that a common use
66774664626SKris Kennaway  * for a stack of CAs is to send it to the client). Actually, it doesn't have
66874664626SKris Kennaway  * much to do with CAs, either, since it will load any old cert.
66974664626SKris Kennaway  * \param file the file containing one or more certs.
67074664626SKris Kennaway  * \return a ::STACK containing the certs.
67174664626SKris Kennaway  */
67274664626SKris Kennaway STACK_OF(X509_NAME) *SSL_load_client_CA_file(const char *file)
67374664626SKris Kennaway 	{
67474664626SKris Kennaway 	BIO *in;
67574664626SKris Kennaway 	X509 *x=NULL;
67674664626SKris Kennaway 	X509_NAME *xn=NULL;
6773b4e3dcbSSimon L. B. Nielsen 	STACK_OF(X509_NAME) *ret = NULL,*sk;
67874664626SKris Kennaway 
679f579bf8eSKris Kennaway 	sk=sk_X509_NAME_new(xname_cmp);
68074664626SKris Kennaway 
68174664626SKris Kennaway 	in=BIO_new(BIO_s_file_internal());
68274664626SKris Kennaway 
6833b4e3dcbSSimon L. B. Nielsen 	if ((sk == NULL) || (in == NULL))
68474664626SKris Kennaway 		{
68574664626SKris Kennaway 		SSLerr(SSL_F_SSL_LOAD_CLIENT_CA_FILE,ERR_R_MALLOC_FAILURE);
68674664626SKris Kennaway 		goto err;
68774664626SKris Kennaway 		}
68874664626SKris Kennaway 
68974664626SKris Kennaway 	if (!BIO_read_filename(in,file))
69074664626SKris Kennaway 		goto err;
69174664626SKris Kennaway 
69274664626SKris Kennaway 	for (;;)
69374664626SKris Kennaway 		{
69474664626SKris Kennaway 		if (PEM_read_bio_X509(in,&x,NULL,NULL) == NULL)
69574664626SKris Kennaway 			break;
6963b4e3dcbSSimon L. B. Nielsen 		if (ret == NULL)
6973b4e3dcbSSimon L. B. Nielsen 			{
6983b4e3dcbSSimon L. B. Nielsen 			ret = sk_X509_NAME_new_null();
6993b4e3dcbSSimon L. B. Nielsen 			if (ret == NULL)
7003b4e3dcbSSimon L. B. Nielsen 				{
7013b4e3dcbSSimon L. B. Nielsen 				SSLerr(SSL_F_SSL_LOAD_CLIENT_CA_FILE,ERR_R_MALLOC_FAILURE);
7023b4e3dcbSSimon L. B. Nielsen 				goto err;
7033b4e3dcbSSimon L. B. Nielsen 				}
7043b4e3dcbSSimon L. B. Nielsen 			}
70574664626SKris Kennaway 		if ((xn=X509_get_subject_name(x)) == NULL) goto err;
70674664626SKris Kennaway 		/* check for duplicates */
70774664626SKris Kennaway 		xn=X509_NAME_dup(xn);
70874664626SKris Kennaway 		if (xn == NULL) goto err;
70974664626SKris Kennaway 		if (sk_X509_NAME_find(sk,xn) >= 0)
71074664626SKris Kennaway 			X509_NAME_free(xn);
71174664626SKris Kennaway 		else
71274664626SKris Kennaway 			{
71374664626SKris Kennaway 			sk_X509_NAME_push(sk,xn);
71474664626SKris Kennaway 			sk_X509_NAME_push(ret,xn);
71574664626SKris Kennaway 			}
71674664626SKris Kennaway 		}
71774664626SKris Kennaway 
71874664626SKris Kennaway 	if (0)
71974664626SKris Kennaway 		{
72074664626SKris Kennaway err:
72174664626SKris Kennaway 		if (ret != NULL) sk_X509_NAME_pop_free(ret,X509_NAME_free);
72274664626SKris Kennaway 		ret=NULL;
72374664626SKris Kennaway 		}
72474664626SKris Kennaway 	if (sk != NULL) sk_X509_NAME_free(sk);
72574664626SKris Kennaway 	if (in != NULL) BIO_free(in);
72674664626SKris Kennaway 	if (x != NULL) X509_free(x);
7273b4e3dcbSSimon L. B. Nielsen 	if (ret != NULL)
7283b4e3dcbSSimon L. B. Nielsen 		ERR_clear_error();
72974664626SKris Kennaway 	return(ret);
73074664626SKris Kennaway 	}
73174664626SKris Kennaway #endif
73274664626SKris Kennaway 
73374664626SKris Kennaway /*!
73474664626SKris Kennaway  * Add a file of certs to a stack.
73574664626SKris Kennaway  * \param stack the stack to add to.
73674664626SKris Kennaway  * \param file the file to add from. All certs in this file that are not
73774664626SKris Kennaway  * already in the stack will be added.
73874664626SKris Kennaway  * \return 1 for success, 0 for failure. Note that in the case of failure some
73974664626SKris Kennaway  * certs may have been added to \c stack.
74074664626SKris Kennaway  */
74174664626SKris Kennaway 
74274664626SKris Kennaway int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
74374664626SKris Kennaway 					const char *file)
74474664626SKris Kennaway 	{
74574664626SKris Kennaway 	BIO *in;
74674664626SKris Kennaway 	X509 *x=NULL;
74774664626SKris Kennaway 	X509_NAME *xn=NULL;
74874664626SKris Kennaway 	int ret=1;
749ddd58736SKris Kennaway 	int (*oldcmp)(const X509_NAME * const *a, const X509_NAME * const *b);
75074664626SKris Kennaway 
751f579bf8eSKris Kennaway 	oldcmp=sk_X509_NAME_set_cmp_func(stack,xname_cmp);
75274664626SKris Kennaway 
75374664626SKris Kennaway 	in=BIO_new(BIO_s_file_internal());
75474664626SKris Kennaway 
75574664626SKris Kennaway 	if (in == NULL)
75674664626SKris Kennaway 		{
75774664626SKris Kennaway 		SSLerr(SSL_F_SSL_ADD_FILE_CERT_SUBJECTS_TO_STACK,ERR_R_MALLOC_FAILURE);
75874664626SKris Kennaway 		goto err;
75974664626SKris Kennaway 		}
76074664626SKris Kennaway 
76174664626SKris Kennaway 	if (!BIO_read_filename(in,file))
76274664626SKris Kennaway 		goto err;
76374664626SKris Kennaway 
76474664626SKris Kennaway 	for (;;)
76574664626SKris Kennaway 		{
76674664626SKris Kennaway 		if (PEM_read_bio_X509(in,&x,NULL,NULL) == NULL)
76774664626SKris Kennaway 			break;
76874664626SKris Kennaway 		if ((xn=X509_get_subject_name(x)) == NULL) goto err;
76974664626SKris Kennaway 		xn=X509_NAME_dup(xn);
77074664626SKris Kennaway 		if (xn == NULL) goto err;
77174664626SKris Kennaway 		if (sk_X509_NAME_find(stack,xn) >= 0)
77274664626SKris Kennaway 			X509_NAME_free(xn);
77374664626SKris Kennaway 		else
77474664626SKris Kennaway 			sk_X509_NAME_push(stack,xn);
77574664626SKris Kennaway 		}
77674664626SKris Kennaway 
777a3ddd25aSSimon L. B. Nielsen 	ERR_clear_error();
778a3ddd25aSSimon L. B. Nielsen 
77974664626SKris Kennaway 	if (0)
78074664626SKris Kennaway 		{
78174664626SKris Kennaway err:
78274664626SKris Kennaway 		ret=0;
78374664626SKris Kennaway 		}
78474664626SKris Kennaway 	if(in != NULL)
78574664626SKris Kennaway 		BIO_free(in);
78674664626SKris Kennaway 	if(x != NULL)
78774664626SKris Kennaway 		X509_free(x);
78874664626SKris Kennaway 
789db522d3aSSimon L. B. Nielsen 	(void)sk_X509_NAME_set_cmp_func(stack,oldcmp);
79074664626SKris Kennaway 
79174664626SKris Kennaway 	return ret;
79274664626SKris Kennaway 	}
79374664626SKris Kennaway 
79474664626SKris Kennaway /*!
79574664626SKris Kennaway  * Add a directory of certs to a stack.
79674664626SKris Kennaway  * \param stack the stack to append to.
79774664626SKris Kennaway  * \param dir the directory to append from. All files in this directory will be
79874664626SKris Kennaway  * examined as potential certs. Any that are acceptable to
79974664626SKris Kennaway  * SSL_add_dir_cert_subjects_to_stack() that are not already in the stack will be
80074664626SKris Kennaway  * included.
80174664626SKris Kennaway  * \return 1 for success, 0 for failure. Note that in the case of failure some
80274664626SKris Kennaway  * certs may have been added to \c stack.
80374664626SKris Kennaway  */
80474664626SKris Kennaway 
80574664626SKris Kennaway int SSL_add_dir_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
80674664626SKris Kennaway 				       const char *dir)
80774664626SKris Kennaway 	{
8083b4e3dcbSSimon L. B. Nielsen 	OPENSSL_DIR_CTX *d = NULL;
8093b4e3dcbSSimon L. B. Nielsen 	const char *filename;
81074664626SKris Kennaway 	int ret = 0;
81174664626SKris Kennaway 
81274664626SKris Kennaway 	CRYPTO_w_lock(CRYPTO_LOCK_READDIR);
81374664626SKris Kennaway 
81474664626SKris Kennaway 	/* Note that a side effect is that the CAs will be sorted by name */
81574664626SKris Kennaway 
8163b4e3dcbSSimon L. B. Nielsen 	while((filename = OPENSSL_DIR_read(&d, dir)))
81774664626SKris Kennaway 		{
81874664626SKris Kennaway 		char buf[1024];
819ddd58736SKris Kennaway 		int r;
82074664626SKris Kennaway 
8213b4e3dcbSSimon L. B. Nielsen 		if(strlen(dir)+strlen(filename)+2 > sizeof buf)
82274664626SKris Kennaway 			{
82374664626SKris Kennaway 			SSLerr(SSL_F_SSL_ADD_DIR_CERT_SUBJECTS_TO_STACK,SSL_R_PATH_TOO_LONG);
82474664626SKris Kennaway 			goto err;
82574664626SKris Kennaway 			}
82674664626SKris Kennaway 
8273b4e3dcbSSimon L. B. Nielsen #ifdef OPENSSL_SYS_VMS
8283b4e3dcbSSimon L. B. Nielsen 		r = BIO_snprintf(buf,sizeof buf,"%s%s",dir,filename);
8293b4e3dcbSSimon L. B. Nielsen #else
8303b4e3dcbSSimon L. B. Nielsen 		r = BIO_snprintf(buf,sizeof buf,"%s/%s",dir,filename);
8313b4e3dcbSSimon L. B. Nielsen #endif
8323b4e3dcbSSimon L. B. Nielsen 		if (r <= 0 || r >= (int)sizeof(buf))
833ddd58736SKris Kennaway 			goto err;
83474664626SKris Kennaway 		if(!SSL_add_file_cert_subjects_to_stack(stack,buf))
83574664626SKris Kennaway 			goto err;
83674664626SKris Kennaway 		}
83774664626SKris Kennaway 
8383b4e3dcbSSimon L. B. Nielsen 	if (errno)
8395c87c606SMark Murray 		{
8405c87c606SMark Murray 		SYSerr(SYS_F_OPENDIR, get_last_sys_error());
8413b4e3dcbSSimon L. B. Nielsen 		ERR_add_error_data(3, "OPENSSL_DIR_read(&ctx, '", dir, "')");
8425c87c606SMark Murray 		SSLerr(SSL_F_SSL_ADD_DIR_CERT_SUBJECTS_TO_STACK, ERR_R_SYS_LIB);
8435c87c606SMark Murray 		goto err;
8445c87c606SMark Murray 		}
8455c87c606SMark Murray 
8465c87c606SMark Murray 	ret = 1;
8475c87c606SMark Murray 
8485c87c606SMark Murray err:
8493b4e3dcbSSimon L. B. Nielsen 	if (d) OPENSSL_DIR_end(&d);
8505c87c606SMark Murray 	CRYPTO_w_unlock(CRYPTO_LOCK_READDIR);
8515c87c606SMark Murray 	return ret;
8525c87c606SMark Murray 	}
8535c87c606SMark Murray 
854