xref: /freebsd/crypto/openssl/ssl/ssl_cert.c (revision f579bf8ec7713035a231884db9958cdc4ff5d7cf)
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  */
10674664626SKris Kennaway 
10774664626SKris Kennaway #include <stdio.h>
108f579bf8eSKris Kennaway 
109f579bf8eSKris Kennaway #include "openssl/e_os.h"
110f579bf8eSKris Kennaway 
111f579bf8eSKris Kennaway #ifndef NO_SYS_TYPES_H
11274664626SKris Kennaway # include <sys/types.h>
113f579bf8eSKris Kennaway #endif
114f579bf8eSKris Kennaway 
115f579bf8eSKris Kennaway #if !defined(WIN32) && !defined(VSM) && !defined(NeXT) && !defined(MAC_OS_pre_X)
11674664626SKris Kennaway #include <dirent.h>
11774664626SKris Kennaway #endif
118f579bf8eSKris Kennaway 
11974664626SKris Kennaway #ifdef NeXT
12074664626SKris Kennaway #include <sys/dir.h>
12174664626SKris Kennaway #define dirent direct
12274664626SKris Kennaway #endif
123f579bf8eSKris Kennaway 
12474664626SKris Kennaway #include <openssl/objects.h>
12574664626SKris Kennaway #include <openssl/bio.h>
12674664626SKris Kennaway #include <openssl/pem.h>
127f579bf8eSKris Kennaway #include <openssl/x509v3.h>
12874664626SKris Kennaway #include "ssl_locl.h"
12974664626SKris Kennaway 
13074664626SKris Kennaway int SSL_get_ex_data_X509_STORE_CTX_idx(void)
13174664626SKris Kennaway 	{
13274664626SKris Kennaway 	static int ssl_x509_store_ctx_idx= -1;
13374664626SKris Kennaway 
13474664626SKris Kennaway 	if (ssl_x509_store_ctx_idx < 0)
13574664626SKris Kennaway 		{
13674664626SKris Kennaway 		ssl_x509_store_ctx_idx=X509_STORE_CTX_get_ex_new_index(
13774664626SKris Kennaway 			0,"SSL for verify callback",NULL,NULL,NULL);
13874664626SKris Kennaway 		}
13974664626SKris Kennaway 	return(ssl_x509_store_ctx_idx);
14074664626SKris Kennaway 	}
14174664626SKris Kennaway 
14274664626SKris Kennaway CERT *ssl_cert_new(void)
14374664626SKris Kennaway 	{
14474664626SKris Kennaway 	CERT *ret;
14574664626SKris Kennaway 
14674664626SKris Kennaway 	ret=(CERT *)Malloc(sizeof(CERT));
14774664626SKris Kennaway 	if (ret == NULL)
14874664626SKris Kennaway 		{
14974664626SKris Kennaway 		SSLerr(SSL_F_SSL_CERT_NEW,ERR_R_MALLOC_FAILURE);
15074664626SKris Kennaway 		return(NULL);
15174664626SKris Kennaway 		}
15274664626SKris Kennaway 	memset(ret,0,sizeof(CERT));
15374664626SKris Kennaway 
15474664626SKris Kennaway 	ret->key= &(ret->pkeys[SSL_PKEY_RSA_ENC]);
15574664626SKris Kennaway 	ret->references=1;
15674664626SKris Kennaway 
15774664626SKris Kennaway 	return(ret);
15874664626SKris Kennaway 	}
15974664626SKris Kennaway 
16074664626SKris Kennaway CERT *ssl_cert_dup(CERT *cert)
16174664626SKris Kennaway 	{
16274664626SKris Kennaway 	CERT *ret;
16374664626SKris Kennaway 	int i;
16474664626SKris Kennaway 
16574664626SKris Kennaway 	ret = (CERT *)Malloc(sizeof(CERT));
16674664626SKris Kennaway 	if (ret == NULL)
16774664626SKris Kennaway 		{
16874664626SKris Kennaway 		SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_MALLOC_FAILURE);
16974664626SKris Kennaway 		return(NULL);
17074664626SKris Kennaway 		}
17174664626SKris Kennaway 
17274664626SKris Kennaway 	memset(ret, 0, sizeof(CERT));
17374664626SKris Kennaway 
17474664626SKris Kennaway 	ret->key = &ret->pkeys[cert->key - &cert->pkeys[0]];
17574664626SKris Kennaway 	/* or ret->key = ret->pkeys + (cert->key - cert->pkeys),
17674664626SKris Kennaway 	 * if you find that more readable */
17774664626SKris Kennaway 
17874664626SKris Kennaway 	ret->valid = cert->valid;
17974664626SKris Kennaway 	ret->mask = cert->mask;
18074664626SKris Kennaway 	ret->export_mask = cert->export_mask;
18174664626SKris Kennaway 
18274664626SKris Kennaway #ifndef NO_RSA
18374664626SKris Kennaway 	if (cert->rsa_tmp != NULL)
18474664626SKris Kennaway 		{
18574664626SKris Kennaway 		ret->rsa_tmp = cert->rsa_tmp;
18674664626SKris Kennaway 		CRYPTO_add(&ret->rsa_tmp->references, 1, CRYPTO_LOCK_RSA);
18774664626SKris Kennaway 		}
18874664626SKris Kennaway 	ret->rsa_tmp_cb = cert->rsa_tmp_cb;
18974664626SKris Kennaway #endif
19074664626SKris Kennaway 
19174664626SKris Kennaway #ifndef NO_DH
19274664626SKris Kennaway 	if (cert->dh_tmp != NULL)
19374664626SKris Kennaway 		{
194f579bf8eSKris Kennaway 		/* DH parameters don't have a reference count */
19574664626SKris Kennaway 		ret->dh_tmp = DHparams_dup(cert->dh_tmp);
19674664626SKris Kennaway 		if (ret->dh_tmp == NULL)
19774664626SKris Kennaway 			{
198f579bf8eSKris Kennaway 			SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_DH_LIB);
19974664626SKris Kennaway 			goto err;
20074664626SKris Kennaway 			}
201f579bf8eSKris Kennaway 		if (cert->dh_tmp->priv_key)
202f579bf8eSKris Kennaway 			{
203f579bf8eSKris Kennaway 			BIGNUM *b = BN_dup(cert->dh_tmp->priv_key);
204f579bf8eSKris Kennaway 			if (!b)
205f579bf8eSKris Kennaway 				{
206f579bf8eSKris Kennaway 				SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_BN_LIB);
207f579bf8eSKris Kennaway 				goto err;
208f579bf8eSKris Kennaway 				}
209f579bf8eSKris Kennaway 			ret->dh_tmp->priv_key = b;
210f579bf8eSKris Kennaway 			}
211f579bf8eSKris Kennaway 		if (cert->dh_tmp->pub_key)
212f579bf8eSKris Kennaway 			{
213f579bf8eSKris Kennaway 			BIGNUM *b = BN_dup(cert->dh_tmp->pub_key);
214f579bf8eSKris Kennaway 			if (!b)
215f579bf8eSKris Kennaway 				{
216f579bf8eSKris Kennaway 				SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_BN_LIB);
217f579bf8eSKris Kennaway 				goto err;
218f579bf8eSKris Kennaway 				}
219f579bf8eSKris Kennaway 			ret->dh_tmp->pub_key = b;
220f579bf8eSKris Kennaway 			}
22174664626SKris Kennaway 		}
22274664626SKris Kennaway 	ret->dh_tmp_cb = cert->dh_tmp_cb;
22374664626SKris Kennaway #endif
22474664626SKris Kennaway 
22574664626SKris Kennaway 	for (i = 0; i < SSL_PKEY_NUM; i++)
22674664626SKris Kennaway 		{
22774664626SKris Kennaway 		if (cert->pkeys[i].x509 != NULL)
22874664626SKris Kennaway 			{
22974664626SKris Kennaway 			ret->pkeys[i].x509 = cert->pkeys[i].x509;
23074664626SKris Kennaway 			CRYPTO_add(&ret->pkeys[i].x509->references, 1,
23174664626SKris Kennaway 				CRYPTO_LOCK_X509);
23274664626SKris Kennaway 			}
23374664626SKris Kennaway 
23474664626SKris Kennaway 		if (cert->pkeys[i].privatekey != NULL)
23574664626SKris Kennaway 			{
23674664626SKris Kennaway 			ret->pkeys[i].privatekey = cert->pkeys[i].privatekey;
23774664626SKris Kennaway 			CRYPTO_add(&ret->pkeys[i].privatekey->references, 1,
23874664626SKris Kennaway 				CRYPTO_LOCK_EVP_PKEY);
23974664626SKris Kennaway 
24074664626SKris Kennaway 			switch(i)
24174664626SKris Kennaway 				{
24274664626SKris Kennaway 				/* If there was anything special to do for
24374664626SKris Kennaway 				 * certain types of keys, we'd do it here.
24474664626SKris Kennaway 				 * (Nothing at the moment, I think.) */
24574664626SKris Kennaway 
24674664626SKris Kennaway 			case SSL_PKEY_RSA_ENC:
24774664626SKris Kennaway 			case SSL_PKEY_RSA_SIGN:
24874664626SKris Kennaway 				/* We have an RSA key. */
24974664626SKris Kennaway 				break;
25074664626SKris Kennaway 
25174664626SKris Kennaway 			case SSL_PKEY_DSA_SIGN:
25274664626SKris Kennaway 				/* We have a DSA key. */
25374664626SKris Kennaway 				break;
25474664626SKris Kennaway 
25574664626SKris Kennaway 			case SSL_PKEY_DH_RSA:
25674664626SKris Kennaway 			case SSL_PKEY_DH_DSA:
25774664626SKris Kennaway 				/* We have a DH key. */
25874664626SKris Kennaway 				break;
25974664626SKris Kennaway 
26074664626SKris Kennaway 			default:
26174664626SKris Kennaway 				/* Can't happen. */
26274664626SKris Kennaway 				SSLerr(SSL_F_SSL_CERT_DUP, SSL_R_LIBRARY_BUG);
26374664626SKris Kennaway 				}
26474664626SKris Kennaway 			}
26574664626SKris Kennaway 		}
26674664626SKris Kennaway 
26774664626SKris Kennaway 	/* ret->extra_certs *should* exist, but currently the own certificate
26874664626SKris Kennaway 	 * chain is held inside SSL_CTX */
26974664626SKris Kennaway 
27074664626SKris Kennaway 	ret->references=1;
27174664626SKris Kennaway 
27274664626SKris Kennaway 	return(ret);
27374664626SKris Kennaway 
27474664626SKris Kennaway err:
27574664626SKris Kennaway #ifndef NO_RSA
27674664626SKris Kennaway 	if (ret->rsa_tmp != NULL)
27774664626SKris Kennaway 		RSA_free(ret->rsa_tmp);
27874664626SKris Kennaway #endif
27974664626SKris Kennaway #ifndef NO_DH
28074664626SKris Kennaway 	if (ret->dh_tmp != NULL)
28174664626SKris Kennaway 		DH_free(ret->dh_tmp);
28274664626SKris Kennaway #endif
28374664626SKris Kennaway 
28474664626SKris Kennaway 	for (i = 0; i < SSL_PKEY_NUM; i++)
28574664626SKris Kennaway 		{
28674664626SKris Kennaway 		if (ret->pkeys[i].x509 != NULL)
28774664626SKris Kennaway 			X509_free(ret->pkeys[i].x509);
28874664626SKris Kennaway 		if (ret->pkeys[i].privatekey != NULL)
28974664626SKris Kennaway 			EVP_PKEY_free(ret->pkeys[i].privatekey);
29074664626SKris Kennaway 		}
29174664626SKris Kennaway 
29274664626SKris Kennaway 	return NULL;
29374664626SKris Kennaway 	}
29474664626SKris Kennaway 
29574664626SKris Kennaway 
29674664626SKris Kennaway void ssl_cert_free(CERT *c)
29774664626SKris Kennaway 	{
29874664626SKris Kennaway 	int i;
29974664626SKris Kennaway 
30074664626SKris Kennaway 	if(c == NULL)
30174664626SKris Kennaway 	    return;
30274664626SKris Kennaway 
30374664626SKris Kennaway 	i=CRYPTO_add(&c->references,-1,CRYPTO_LOCK_SSL_CERT);
30474664626SKris Kennaway #ifdef REF_PRINT
30574664626SKris Kennaway 	REF_PRINT("CERT",c);
30674664626SKris Kennaway #endif
30774664626SKris Kennaway 	if (i > 0) return;
30874664626SKris Kennaway #ifdef REF_CHECK
30974664626SKris Kennaway 	if (i < 0)
31074664626SKris Kennaway 		{
31174664626SKris Kennaway 		fprintf(stderr,"ssl_cert_free, bad reference count\n");
31274664626SKris Kennaway 		abort(); /* ok */
31374664626SKris Kennaway 		}
31474664626SKris Kennaway #endif
31574664626SKris Kennaway 
31674664626SKris Kennaway #ifndef NO_RSA
31774664626SKris Kennaway 	if (c->rsa_tmp) RSA_free(c->rsa_tmp);
31874664626SKris Kennaway #endif
31974664626SKris Kennaway #ifndef NO_DH
32074664626SKris Kennaway 	if (c->dh_tmp) DH_free(c->dh_tmp);
32174664626SKris Kennaway #endif
32274664626SKris Kennaway 
32374664626SKris Kennaway 	for (i=0; i<SSL_PKEY_NUM; i++)
32474664626SKris Kennaway 		{
32574664626SKris Kennaway 		if (c->pkeys[i].x509 != NULL)
32674664626SKris Kennaway 			X509_free(c->pkeys[i].x509);
32774664626SKris Kennaway 		if (c->pkeys[i].privatekey != NULL)
32874664626SKris Kennaway 			EVP_PKEY_free(c->pkeys[i].privatekey);
32974664626SKris Kennaway #if 0
33074664626SKris Kennaway 		if (c->pkeys[i].publickey != NULL)
33174664626SKris Kennaway 			EVP_PKEY_free(c->pkeys[i].publickey);
33274664626SKris Kennaway #endif
33374664626SKris Kennaway 		}
33474664626SKris Kennaway 	Free(c);
33574664626SKris Kennaway 	}
33674664626SKris Kennaway 
33774664626SKris Kennaway int ssl_cert_inst(CERT **o)
33874664626SKris Kennaway 	{
33974664626SKris Kennaway 	/* Create a CERT if there isn't already one
34074664626SKris Kennaway 	 * (which cannot really happen, as it is initially created in
34174664626SKris Kennaway 	 * SSL_CTX_new; but the earlier code usually allows for that one
34274664626SKris Kennaway 	 * being non-existant, so we follow that behaviour, as it might
34374664626SKris Kennaway 	 * turn out that there actually is a reason for it -- but I'm
34474664626SKris Kennaway 	 * not sure that *all* of the existing code could cope with
34574664626SKris Kennaway 	 * s->cert being NULL, otherwise we could do without the
34674664626SKris Kennaway 	 * initialization in SSL_CTX_new).
34774664626SKris Kennaway 	 */
34874664626SKris Kennaway 
34974664626SKris Kennaway 	if (o == NULL)
35074664626SKris Kennaway 		{
35174664626SKris Kennaway 		SSLerr(SSL_F_SSL_CERT_INST, ERR_R_PASSED_NULL_PARAMETER);
35274664626SKris Kennaway 		return(0);
35374664626SKris Kennaway 		}
35474664626SKris Kennaway 	if (*o == NULL)
35574664626SKris Kennaway 		{
35674664626SKris Kennaway 		if ((*o = ssl_cert_new()) == NULL)
35774664626SKris Kennaway 			{
35874664626SKris Kennaway 			SSLerr(SSL_F_SSL_CERT_INST, ERR_R_MALLOC_FAILURE);
35974664626SKris Kennaway 			return(0);
36074664626SKris Kennaway 			}
36174664626SKris Kennaway 		}
36274664626SKris Kennaway 	return(1);
36374664626SKris Kennaway 	}
36474664626SKris Kennaway 
36574664626SKris Kennaway 
36674664626SKris Kennaway SESS_CERT *ssl_sess_cert_new(void)
36774664626SKris Kennaway 	{
36874664626SKris Kennaway 	SESS_CERT *ret;
36974664626SKris Kennaway 
37074664626SKris Kennaway 	ret = Malloc(sizeof *ret);
37174664626SKris Kennaway 	if (ret == NULL)
37274664626SKris Kennaway 		{
37374664626SKris Kennaway 		SSLerr(SSL_F_SSL_SESS_CERT_NEW, ERR_R_MALLOC_FAILURE);
37474664626SKris Kennaway 		return NULL;
37574664626SKris Kennaway 		}
37674664626SKris Kennaway 
37774664626SKris Kennaway 	memset(ret, 0 ,sizeof *ret);
37874664626SKris Kennaway 	ret->peer_key = &(ret->peer_pkeys[SSL_PKEY_RSA_ENC]);
37974664626SKris Kennaway 	ret->references = 1;
38074664626SKris Kennaway 
38174664626SKris Kennaway 	return ret;
38274664626SKris Kennaway 	}
38374664626SKris Kennaway 
38474664626SKris Kennaway void ssl_sess_cert_free(SESS_CERT *sc)
38574664626SKris Kennaway 	{
38674664626SKris Kennaway 	int i;
38774664626SKris Kennaway 
38874664626SKris Kennaway 	if (sc == NULL)
38974664626SKris Kennaway 		return;
39074664626SKris Kennaway 
39174664626SKris Kennaway 	i = CRYPTO_add(&sc->references, -1, CRYPTO_LOCK_SSL_SESS_CERT);
39274664626SKris Kennaway #ifdef REF_PRINT
39374664626SKris Kennaway 	REF_PRINT("SESS_CERT", sc);
39474664626SKris Kennaway #endif
39574664626SKris Kennaway 	if (i > 0)
39674664626SKris Kennaway 		return;
39774664626SKris Kennaway #ifdef REF_CHECK
39874664626SKris Kennaway 	if (i < 0)
39974664626SKris Kennaway 		{
40074664626SKris Kennaway 		fprintf(stderr,"ssl_sess_cert_free, bad reference count\n");
40174664626SKris Kennaway 		abort(); /* ok */
40274664626SKris Kennaway 		}
40374664626SKris Kennaway #endif
40474664626SKris Kennaway 
40574664626SKris Kennaway 	/* i == 0 */
40674664626SKris Kennaway 	if (sc->cert_chain != NULL)
40774664626SKris Kennaway 		sk_X509_pop_free(sc->cert_chain, X509_free);
40874664626SKris Kennaway 	for (i = 0; i < SSL_PKEY_NUM; i++)
40974664626SKris Kennaway 		{
41074664626SKris Kennaway 		if (sc->peer_pkeys[i].x509 != NULL)
41174664626SKris Kennaway 			X509_free(sc->peer_pkeys[i].x509);
41274664626SKris Kennaway #if 0 /* We don't have the peer's private key.  These lines are just
41374664626SKris Kennaway 	   * here as a reminder that we're still using a not-quite-appropriate
41474664626SKris Kennaway 	   * data structure. */
41574664626SKris Kennaway 		if (sc->peer_pkeys[i].privatekey != NULL)
41674664626SKris Kennaway 			EVP_PKEY_free(sc->peer_pkeys[i].privatekey);
41774664626SKris Kennaway #endif
41874664626SKris Kennaway 		}
41974664626SKris Kennaway 
42074664626SKris Kennaway #ifndef NO_RSA
42174664626SKris Kennaway 	if (sc->peer_rsa_tmp != NULL)
42274664626SKris Kennaway 		RSA_free(sc->peer_rsa_tmp);
42374664626SKris Kennaway #endif
42474664626SKris Kennaway #ifndef NO_DH
42574664626SKris Kennaway 	if (sc->peer_dh_tmp != NULL)
42674664626SKris Kennaway 		DH_free(sc->peer_dh_tmp);
42774664626SKris Kennaway #endif
42874664626SKris Kennaway 
42974664626SKris Kennaway 	Free(sc);
43074664626SKris Kennaway 	}
43174664626SKris Kennaway 
43274664626SKris Kennaway int ssl_set_peer_cert_type(SESS_CERT *sc,int type)
43374664626SKris Kennaway 	{
43474664626SKris Kennaway 	sc->peer_cert_type = type;
43574664626SKris Kennaway 	return(1);
43674664626SKris Kennaway 	}
43774664626SKris Kennaway 
43874664626SKris Kennaway int ssl_verify_cert_chain(SSL *s,STACK_OF(X509) *sk)
43974664626SKris Kennaway 	{
44074664626SKris Kennaway 	X509 *x;
44174664626SKris Kennaway 	int i;
44274664626SKris Kennaway 	X509_STORE_CTX ctx;
44374664626SKris Kennaway 
44474664626SKris Kennaway 	if ((sk == NULL) || (sk_X509_num(sk) == 0))
44574664626SKris Kennaway 		return(0);
44674664626SKris Kennaway 
44774664626SKris Kennaway 	x=sk_X509_value(sk,0);
44874664626SKris Kennaway 	X509_STORE_CTX_init(&ctx,s->ctx->cert_store,x,sk);
44974664626SKris Kennaway 	if (SSL_get_verify_depth(s) >= 0)
45074664626SKris Kennaway 		X509_STORE_CTX_set_depth(&ctx, SSL_get_verify_depth(s));
451f579bf8eSKris Kennaway 	X509_STORE_CTX_set_ex_data(&ctx,SSL_get_ex_data_X509_STORE_CTX_idx(),s);
452f579bf8eSKris Kennaway 	/* We need to set the verify purpose. The purpose can be determined by
453f579bf8eSKris Kennaway 	 * the context: if its a server it will verify SSL client certificates
454f579bf8eSKris Kennaway 	 * or vice versa.
455f579bf8eSKris Kennaway          */
456f579bf8eSKris Kennaway 
457f579bf8eSKris Kennaway 	if(s->server) i = X509_PURPOSE_SSL_CLIENT;
458f579bf8eSKris Kennaway 	else i = X509_PURPOSE_SSL_SERVER;
459f579bf8eSKris Kennaway 
460f579bf8eSKris Kennaway 	X509_STORE_CTX_purpose_inherit(&ctx, i, s->purpose, s->trust);
46174664626SKris Kennaway 
46274664626SKris Kennaway 	if (s->ctx->app_verify_callback != NULL)
46374664626SKris Kennaway 		i=s->ctx->app_verify_callback(&ctx); /* should pass app_verify_arg */
46474664626SKris Kennaway 	else
46574664626SKris Kennaway 		{
46674664626SKris Kennaway #ifndef NO_X509_VERIFY
46774664626SKris Kennaway 		i=X509_verify_cert(&ctx);
46874664626SKris Kennaway #else
46974664626SKris Kennaway 		i=0;
47074664626SKris Kennaway 		ctx.error=X509_V_ERR_APPLICATION_VERIFICATION;
47174664626SKris Kennaway 		SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN,SSL_R_NO_VERIFY_CALLBACK);
47274664626SKris Kennaway #endif
47374664626SKris Kennaway 		}
47474664626SKris Kennaway 
47574664626SKris Kennaway 	s->verify_result=ctx.error;
47674664626SKris Kennaway 	X509_STORE_CTX_cleanup(&ctx);
47774664626SKris Kennaway 
47874664626SKris Kennaway 	return(i);
47974664626SKris Kennaway 	}
48074664626SKris Kennaway 
48174664626SKris Kennaway static void set_client_CA_list(STACK_OF(X509_NAME) **ca_list,STACK_OF(X509_NAME) *list)
48274664626SKris Kennaway 	{
48374664626SKris Kennaway 	if (*ca_list != NULL)
48474664626SKris Kennaway 		sk_X509_NAME_pop_free(*ca_list,X509_NAME_free);
48574664626SKris Kennaway 
48674664626SKris Kennaway 	*ca_list=list;
48774664626SKris Kennaway 	}
48874664626SKris Kennaway 
48974664626SKris Kennaway STACK_OF(X509_NAME) *SSL_dup_CA_list(STACK_OF(X509_NAME) *sk)
49074664626SKris Kennaway 	{
49174664626SKris Kennaway 	int i;
49274664626SKris Kennaway 	STACK_OF(X509_NAME) *ret;
49374664626SKris Kennaway 	X509_NAME *name;
49474664626SKris Kennaway 
49574664626SKris Kennaway 	ret=sk_X509_NAME_new_null();
49674664626SKris Kennaway 	for (i=0; i<sk_X509_NAME_num(sk); i++)
49774664626SKris Kennaway 		{
49874664626SKris Kennaway 		name=X509_NAME_dup(sk_X509_NAME_value(sk,i));
49974664626SKris Kennaway 		if ((name == NULL) || !sk_X509_NAME_push(ret,name))
50074664626SKris Kennaway 			{
50174664626SKris Kennaway 			sk_X509_NAME_pop_free(ret,X509_NAME_free);
50274664626SKris Kennaway 			return(NULL);
50374664626SKris Kennaway 			}
50474664626SKris Kennaway 		}
50574664626SKris Kennaway 	return(ret);
50674664626SKris Kennaway 	}
50774664626SKris Kennaway 
50874664626SKris Kennaway void SSL_set_client_CA_list(SSL *s,STACK_OF(X509_NAME) *list)
50974664626SKris Kennaway 	{
51074664626SKris Kennaway 	set_client_CA_list(&(s->client_CA),list);
51174664626SKris Kennaway 	}
51274664626SKris Kennaway 
51374664626SKris Kennaway void SSL_CTX_set_client_CA_list(SSL_CTX *ctx,STACK_OF(X509_NAME) *list)
51474664626SKris Kennaway 	{
51574664626SKris Kennaway 	set_client_CA_list(&(ctx->client_CA),list);
51674664626SKris Kennaway 	}
51774664626SKris Kennaway 
51874664626SKris Kennaway STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(SSL_CTX *ctx)
51974664626SKris Kennaway 	{
52074664626SKris Kennaway 	return(ctx->client_CA);
52174664626SKris Kennaway 	}
52274664626SKris Kennaway 
52374664626SKris Kennaway STACK_OF(X509_NAME) *SSL_get_client_CA_list(SSL *s)
52474664626SKris Kennaway 	{
52574664626SKris Kennaway 	if (s->type == SSL_ST_CONNECT)
52674664626SKris Kennaway 		{ /* we are in the client */
52774664626SKris Kennaway 		if (((s->version>>8) == SSL3_VERSION_MAJOR) &&
52874664626SKris Kennaway 			(s->s3 != NULL))
52974664626SKris Kennaway 			return(s->s3->tmp.ca_names);
53074664626SKris Kennaway 		else
53174664626SKris Kennaway 			return(NULL);
53274664626SKris Kennaway 		}
53374664626SKris Kennaway 	else
53474664626SKris Kennaway 		{
53574664626SKris Kennaway 		if (s->client_CA != NULL)
53674664626SKris Kennaway 			return(s->client_CA);
53774664626SKris Kennaway 		else
53874664626SKris Kennaway 			return(s->ctx->client_CA);
53974664626SKris Kennaway 		}
54074664626SKris Kennaway 	}
54174664626SKris Kennaway 
54274664626SKris Kennaway static int add_client_CA(STACK_OF(X509_NAME) **sk,X509 *x)
54374664626SKris Kennaway 	{
54474664626SKris Kennaway 	X509_NAME *name;
54574664626SKris Kennaway 
54674664626SKris Kennaway 	if (x == NULL) return(0);
54774664626SKris Kennaway 	if ((*sk == NULL) && ((*sk=sk_X509_NAME_new_null()) == NULL))
54874664626SKris Kennaway 		return(0);
54974664626SKris Kennaway 
55074664626SKris Kennaway 	if ((name=X509_NAME_dup(X509_get_subject_name(x))) == NULL)
55174664626SKris Kennaway 		return(0);
55274664626SKris Kennaway 
55374664626SKris Kennaway 	if (!sk_X509_NAME_push(*sk,name))
55474664626SKris Kennaway 		{
55574664626SKris Kennaway 		X509_NAME_free(name);
55674664626SKris Kennaway 		return(0);
55774664626SKris Kennaway 		}
55874664626SKris Kennaway 	return(1);
55974664626SKris Kennaway 	}
56074664626SKris Kennaway 
56174664626SKris Kennaway int SSL_add_client_CA(SSL *ssl,X509 *x)
56274664626SKris Kennaway 	{
56374664626SKris Kennaway 	return(add_client_CA(&(ssl->client_CA),x));
56474664626SKris Kennaway 	}
56574664626SKris Kennaway 
56674664626SKris Kennaway int SSL_CTX_add_client_CA(SSL_CTX *ctx,X509 *x)
56774664626SKris Kennaway 	{
56874664626SKris Kennaway 	return(add_client_CA(&(ctx->client_CA),x));
56974664626SKris Kennaway 	}
57074664626SKris Kennaway 
571f579bf8eSKris Kennaway static int xname_cmp(X509_NAME **a,X509_NAME **b)
57274664626SKris Kennaway 	{
57374664626SKris Kennaway 	return(X509_NAME_cmp(*a,*b));
57474664626SKris Kennaway 	}
57574664626SKris Kennaway 
57674664626SKris Kennaway #ifndef NO_STDIO
57774664626SKris Kennaway /*!
57874664626SKris Kennaway  * Load CA certs from a file into a ::STACK. Note that it is somewhat misnamed;
57974664626SKris Kennaway  * it doesn't really have anything to do with clients (except that a common use
58074664626SKris Kennaway  * for a stack of CAs is to send it to the client). Actually, it doesn't have
58174664626SKris Kennaway  * much to do with CAs, either, since it will load any old cert.
58274664626SKris Kennaway  * \param file the file containing one or more certs.
58374664626SKris Kennaway  * \return a ::STACK containing the certs.
58474664626SKris Kennaway  */
58574664626SKris Kennaway STACK_OF(X509_NAME) *SSL_load_client_CA_file(const char *file)
58674664626SKris Kennaway 	{
58774664626SKris Kennaway 	BIO *in;
58874664626SKris Kennaway 	X509 *x=NULL;
58974664626SKris Kennaway 	X509_NAME *xn=NULL;
59074664626SKris Kennaway 	STACK_OF(X509_NAME) *ret,*sk;
59174664626SKris Kennaway 
59274664626SKris Kennaway 	ret=sk_X509_NAME_new(NULL);
593f579bf8eSKris Kennaway 	sk=sk_X509_NAME_new(xname_cmp);
59474664626SKris Kennaway 
59574664626SKris Kennaway 	in=BIO_new(BIO_s_file_internal());
59674664626SKris Kennaway 
59774664626SKris Kennaway 	if ((ret == NULL) || (sk == NULL) || (in == NULL))
59874664626SKris Kennaway 		{
59974664626SKris Kennaway 		SSLerr(SSL_F_SSL_LOAD_CLIENT_CA_FILE,ERR_R_MALLOC_FAILURE);
60074664626SKris Kennaway 		goto err;
60174664626SKris Kennaway 		}
60274664626SKris Kennaway 
60374664626SKris Kennaway 	if (!BIO_read_filename(in,file))
60474664626SKris Kennaway 		goto err;
60574664626SKris Kennaway 
60674664626SKris Kennaway 	for (;;)
60774664626SKris Kennaway 		{
60874664626SKris Kennaway 		if (PEM_read_bio_X509(in,&x,NULL,NULL) == NULL)
60974664626SKris Kennaway 			break;
61074664626SKris Kennaway 		if ((xn=X509_get_subject_name(x)) == NULL) goto err;
61174664626SKris Kennaway 		/* check for duplicates */
61274664626SKris Kennaway 		xn=X509_NAME_dup(xn);
61374664626SKris Kennaway 		if (xn == NULL) goto err;
61474664626SKris Kennaway 		if (sk_X509_NAME_find(sk,xn) >= 0)
61574664626SKris Kennaway 			X509_NAME_free(xn);
61674664626SKris Kennaway 		else
61774664626SKris Kennaway 			{
61874664626SKris Kennaway 			sk_X509_NAME_push(sk,xn);
61974664626SKris Kennaway 			sk_X509_NAME_push(ret,xn);
62074664626SKris Kennaway 			}
62174664626SKris Kennaway 		}
62274664626SKris Kennaway 
62374664626SKris Kennaway 	if (0)
62474664626SKris Kennaway 		{
62574664626SKris Kennaway err:
62674664626SKris Kennaway 		if (ret != NULL) sk_X509_NAME_pop_free(ret,X509_NAME_free);
62774664626SKris Kennaway 		ret=NULL;
62874664626SKris Kennaway 		}
62974664626SKris Kennaway 	if (sk != NULL) sk_X509_NAME_free(sk);
63074664626SKris Kennaway 	if (in != NULL) BIO_free(in);
63174664626SKris Kennaway 	if (x != NULL) X509_free(x);
63274664626SKris Kennaway 	return(ret);
63374664626SKris Kennaway 	}
63474664626SKris Kennaway #endif
63574664626SKris Kennaway 
63674664626SKris Kennaway /*!
63774664626SKris Kennaway  * Add a file of certs to a stack.
63874664626SKris Kennaway  * \param stack the stack to add to.
63974664626SKris Kennaway  * \param file the file to add from. All certs in this file that are not
64074664626SKris Kennaway  * already in the stack will be added.
64174664626SKris Kennaway  * \return 1 for success, 0 for failure. Note that in the case of failure some
64274664626SKris Kennaway  * certs may have been added to \c stack.
64374664626SKris Kennaway  */
64474664626SKris Kennaway 
64574664626SKris Kennaway int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
64674664626SKris Kennaway 					const char *file)
64774664626SKris Kennaway     {
64874664626SKris Kennaway     BIO *in;
64974664626SKris Kennaway     X509 *x=NULL;
65074664626SKris Kennaway     X509_NAME *xn=NULL;
65174664626SKris Kennaway     int ret=1;
65274664626SKris Kennaway     int (*oldcmp)(X509_NAME **a, X509_NAME **b);
65374664626SKris Kennaway 
654f579bf8eSKris Kennaway     oldcmp=sk_X509_NAME_set_cmp_func(stack,xname_cmp);
65574664626SKris Kennaway 
65674664626SKris Kennaway     in=BIO_new(BIO_s_file_internal());
65774664626SKris Kennaway 
65874664626SKris Kennaway     if (in == NULL)
65974664626SKris Kennaway 	{
66074664626SKris Kennaway 	SSLerr(SSL_F_SSL_ADD_FILE_CERT_SUBJECTS_TO_STACK,ERR_R_MALLOC_FAILURE);
66174664626SKris Kennaway 	goto err;
66274664626SKris Kennaway 	}
66374664626SKris Kennaway 
66474664626SKris Kennaway     if (!BIO_read_filename(in,file))
66574664626SKris Kennaway 	goto err;
66674664626SKris Kennaway 
66774664626SKris Kennaway     for (;;)
66874664626SKris Kennaway 	{
66974664626SKris Kennaway 	if (PEM_read_bio_X509(in,&x,NULL,NULL) == NULL)
67074664626SKris Kennaway 	    break;
67174664626SKris Kennaway 	if ((xn=X509_get_subject_name(x)) == NULL) goto err;
67274664626SKris Kennaway 	xn=X509_NAME_dup(xn);
67374664626SKris Kennaway 	if (xn == NULL) goto err;
67474664626SKris Kennaway 	if (sk_X509_NAME_find(stack,xn) >= 0)
67574664626SKris Kennaway 	    X509_NAME_free(xn);
67674664626SKris Kennaway 	else
67774664626SKris Kennaway 	    sk_X509_NAME_push(stack,xn);
67874664626SKris Kennaway 	}
67974664626SKris Kennaway 
68074664626SKris Kennaway     if (0)
68174664626SKris Kennaway 	{
68274664626SKris Kennaway err:
68374664626SKris Kennaway 	ret=0;
68474664626SKris Kennaway 	}
68574664626SKris Kennaway     if(in != NULL)
68674664626SKris Kennaway 	BIO_free(in);
68774664626SKris Kennaway     if(x != NULL)
68874664626SKris Kennaway 	X509_free(x);
68974664626SKris Kennaway 
69074664626SKris Kennaway     sk_X509_NAME_set_cmp_func(stack,oldcmp);
69174664626SKris Kennaway 
69274664626SKris Kennaway     return ret;
69374664626SKris Kennaway     }
69474664626SKris Kennaway 
69574664626SKris Kennaway /*!
69674664626SKris Kennaway  * Add a directory of certs to a stack.
69774664626SKris Kennaway  * \param stack the stack to append to.
69874664626SKris Kennaway  * \param dir the directory to append from. All files in this directory will be
69974664626SKris Kennaway  * examined as potential certs. Any that are acceptable to
70074664626SKris Kennaway  * SSL_add_dir_cert_subjects_to_stack() that are not already in the stack will be
70174664626SKris Kennaway  * included.
70274664626SKris Kennaway  * \return 1 for success, 0 for failure. Note that in the case of failure some
70374664626SKris Kennaway  * certs may have been added to \c stack.
70474664626SKris Kennaway  */
70574664626SKris Kennaway 
70674664626SKris Kennaway #ifndef WIN32
70774664626SKris Kennaway #ifndef VMS			/* XXXX This may be fixed in the future */
708f579bf8eSKris Kennaway #ifndef MAC_OS_pre_X
70974664626SKris Kennaway 
71074664626SKris Kennaway int SSL_add_dir_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
71174664626SKris Kennaway 				       const char *dir)
71274664626SKris Kennaway     {
71374664626SKris Kennaway     DIR *d;
71474664626SKris Kennaway     struct dirent *dstruct;
71574664626SKris Kennaway     int ret = 0;
71674664626SKris Kennaway 
71774664626SKris Kennaway     CRYPTO_w_lock(CRYPTO_LOCK_READDIR);
71874664626SKris Kennaway     d = opendir(dir);
71974664626SKris Kennaway 
72074664626SKris Kennaway     /* Note that a side effect is that the CAs will be sorted by name */
72174664626SKris Kennaway     if(!d)
72274664626SKris Kennaway 	{
72374664626SKris Kennaway 	SYSerr(SYS_F_OPENDIR, get_last_sys_error());
72474664626SKris Kennaway 	ERR_add_error_data(3, "opendir('", dir, "')");
72574664626SKris Kennaway 	SSLerr(SSL_F_SSL_ADD_DIR_CERT_SUBJECTS_TO_STACK, ERR_R_SYS_LIB);
72674664626SKris Kennaway 	goto err;
72774664626SKris Kennaway 	}
72874664626SKris Kennaway 
72974664626SKris Kennaway     while((dstruct=readdir(d)))
73074664626SKris Kennaway 	{
73174664626SKris Kennaway 	char buf[1024];
73274664626SKris Kennaway 
73374664626SKris Kennaway 	if(strlen(dir)+strlen(dstruct->d_name)+2 > sizeof buf)
73474664626SKris Kennaway 	    {
73574664626SKris Kennaway 	    SSLerr(SSL_F_SSL_ADD_DIR_CERT_SUBJECTS_TO_STACK,SSL_R_PATH_TOO_LONG);
73674664626SKris Kennaway 	    goto err;
73774664626SKris Kennaway 	    }
73874664626SKris Kennaway 
73974664626SKris Kennaway 	sprintf(buf,"%s/%s",dir,dstruct->d_name);
74074664626SKris Kennaway 	if(!SSL_add_file_cert_subjects_to_stack(stack,buf))
74174664626SKris Kennaway 	    goto err;
74274664626SKris Kennaway 	}
74374664626SKris Kennaway     ret = 1;
74474664626SKris Kennaway 
74574664626SKris Kennaway err:
74674664626SKris Kennaway     CRYPTO_w_unlock(CRYPTO_LOCK_READDIR);
74774664626SKris Kennaway     return ret;
74874664626SKris Kennaway     }
74974664626SKris Kennaway 
75074664626SKris Kennaway #endif
75174664626SKris Kennaway #endif
752f579bf8eSKris Kennaway #endif
753