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 /* ==================================================================== 59ed5d4f9aSSimon L. B. Nielsen * Copyright (c) 1998-2006 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 16374664626SKris Kennaway CERT *ssl_cert_new(void) 16474664626SKris Kennaway { 16574664626SKris Kennaway CERT *ret; 16674664626SKris Kennaway 167ddd58736SKris Kennaway ret=(CERT *)OPENSSL_malloc(sizeof(CERT)); 16874664626SKris Kennaway if (ret == NULL) 16974664626SKris Kennaway { 17074664626SKris Kennaway SSLerr(SSL_F_SSL_CERT_NEW,ERR_R_MALLOC_FAILURE); 17174664626SKris Kennaway return(NULL); 17274664626SKris Kennaway } 17374664626SKris Kennaway memset(ret,0,sizeof(CERT)); 17474664626SKris Kennaway 17574664626SKris Kennaway ret->key= &(ret->pkeys[SSL_PKEY_RSA_ENC]); 17674664626SKris Kennaway ret->references=1; 17774664626SKris Kennaway 17874664626SKris Kennaway return(ret); 17974664626SKris Kennaway } 18074664626SKris Kennaway 18174664626SKris Kennaway CERT *ssl_cert_dup(CERT *cert) 18274664626SKris Kennaway { 18374664626SKris Kennaway CERT *ret; 18474664626SKris Kennaway int i; 18574664626SKris Kennaway 186ddd58736SKris Kennaway ret = (CERT *)OPENSSL_malloc(sizeof(CERT)); 18774664626SKris Kennaway if (ret == NULL) 18874664626SKris Kennaway { 18974664626SKris Kennaway SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_MALLOC_FAILURE); 19074664626SKris Kennaway return(NULL); 19174664626SKris Kennaway } 19274664626SKris Kennaway 19374664626SKris Kennaway memset(ret, 0, sizeof(CERT)); 19474664626SKris Kennaway 19574664626SKris Kennaway ret->key = &ret->pkeys[cert->key - &cert->pkeys[0]]; 19674664626SKris Kennaway /* or ret->key = ret->pkeys + (cert->key - cert->pkeys), 19774664626SKris Kennaway * if you find that more readable */ 19874664626SKris Kennaway 19974664626SKris Kennaway ret->valid = cert->valid; 20074664626SKris Kennaway ret->mask = cert->mask; 20174664626SKris Kennaway ret->export_mask = cert->export_mask; 20274664626SKris Kennaway 2035c87c606SMark Murray #ifndef OPENSSL_NO_RSA 20474664626SKris Kennaway if (cert->rsa_tmp != NULL) 20574664626SKris Kennaway { 2065c87c606SMark Murray RSA_up_ref(cert->rsa_tmp); 20774664626SKris Kennaway ret->rsa_tmp = cert->rsa_tmp; 20874664626SKris Kennaway } 20974664626SKris Kennaway ret->rsa_tmp_cb = cert->rsa_tmp_cb; 21074664626SKris Kennaway #endif 21174664626SKris Kennaway 2125c87c606SMark Murray #ifndef OPENSSL_NO_DH 21374664626SKris Kennaway if (cert->dh_tmp != NULL) 21474664626SKris Kennaway { 21574664626SKris Kennaway ret->dh_tmp = DHparams_dup(cert->dh_tmp); 21674664626SKris Kennaway if (ret->dh_tmp == NULL) 21774664626SKris Kennaway { 218f579bf8eSKris Kennaway SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_DH_LIB); 21974664626SKris Kennaway goto err; 22074664626SKris Kennaway } 221f579bf8eSKris Kennaway if (cert->dh_tmp->priv_key) 222f579bf8eSKris Kennaway { 223f579bf8eSKris Kennaway BIGNUM *b = BN_dup(cert->dh_tmp->priv_key); 224f579bf8eSKris Kennaway if (!b) 225f579bf8eSKris Kennaway { 226f579bf8eSKris Kennaway SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_BN_LIB); 227f579bf8eSKris Kennaway goto err; 228f579bf8eSKris Kennaway } 229f579bf8eSKris Kennaway ret->dh_tmp->priv_key = b; 230f579bf8eSKris Kennaway } 231f579bf8eSKris Kennaway if (cert->dh_tmp->pub_key) 232f579bf8eSKris Kennaway { 233f579bf8eSKris Kennaway BIGNUM *b = BN_dup(cert->dh_tmp->pub_key); 234f579bf8eSKris Kennaway if (!b) 235f579bf8eSKris Kennaway { 236f579bf8eSKris Kennaway SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_BN_LIB); 237f579bf8eSKris Kennaway goto err; 238f579bf8eSKris Kennaway } 239f579bf8eSKris Kennaway ret->dh_tmp->pub_key = b; 240f579bf8eSKris Kennaway } 24174664626SKris Kennaway } 24274664626SKris Kennaway ret->dh_tmp_cb = cert->dh_tmp_cb; 24374664626SKris Kennaway #endif 24474664626SKris Kennaway 2453b4e3dcbSSimon L. B. Nielsen #ifndef OPENSSL_NO_ECDH 2463b4e3dcbSSimon L. B. Nielsen if (cert->ecdh_tmp) 2473b4e3dcbSSimon L. B. Nielsen { 2483b4e3dcbSSimon L. B. Nielsen ret->ecdh_tmp = EC_KEY_dup(cert->ecdh_tmp); 2493b4e3dcbSSimon L. B. Nielsen if (ret->ecdh_tmp == NULL) 2503b4e3dcbSSimon L. B. Nielsen { 2513b4e3dcbSSimon L. B. Nielsen SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_EC_LIB); 2523b4e3dcbSSimon L. B. Nielsen goto err; 2533b4e3dcbSSimon L. B. Nielsen } 2543b4e3dcbSSimon L. B. Nielsen } 2553b4e3dcbSSimon L. B. Nielsen ret->ecdh_tmp_cb = cert->ecdh_tmp_cb; 2563b4e3dcbSSimon L. B. Nielsen #endif 2573b4e3dcbSSimon L. B. Nielsen 25874664626SKris Kennaway for (i = 0; i < SSL_PKEY_NUM; i++) 25974664626SKris Kennaway { 26074664626SKris Kennaway if (cert->pkeys[i].x509 != NULL) 26174664626SKris Kennaway { 26274664626SKris Kennaway ret->pkeys[i].x509 = cert->pkeys[i].x509; 26374664626SKris Kennaway CRYPTO_add(&ret->pkeys[i].x509->references, 1, 26474664626SKris Kennaway CRYPTO_LOCK_X509); 26574664626SKris Kennaway } 26674664626SKris Kennaway 26774664626SKris Kennaway if (cert->pkeys[i].privatekey != NULL) 26874664626SKris Kennaway { 26974664626SKris Kennaway ret->pkeys[i].privatekey = cert->pkeys[i].privatekey; 27074664626SKris Kennaway CRYPTO_add(&ret->pkeys[i].privatekey->references, 1, 27174664626SKris Kennaway CRYPTO_LOCK_EVP_PKEY); 27274664626SKris Kennaway 27374664626SKris Kennaway switch(i) 27474664626SKris Kennaway { 27574664626SKris Kennaway /* If there was anything special to do for 27674664626SKris Kennaway * certain types of keys, we'd do it here. 27774664626SKris Kennaway * (Nothing at the moment, I think.) */ 27874664626SKris Kennaway 27974664626SKris Kennaway case SSL_PKEY_RSA_ENC: 28074664626SKris Kennaway case SSL_PKEY_RSA_SIGN: 28174664626SKris Kennaway /* We have an RSA key. */ 28274664626SKris Kennaway break; 28374664626SKris Kennaway 28474664626SKris Kennaway case SSL_PKEY_DSA_SIGN: 28574664626SKris Kennaway /* We have a DSA key. */ 28674664626SKris Kennaway break; 28774664626SKris Kennaway 28874664626SKris Kennaway case SSL_PKEY_DH_RSA: 28974664626SKris Kennaway case SSL_PKEY_DH_DSA: 29074664626SKris Kennaway /* We have a DH key. */ 29174664626SKris Kennaway break; 29274664626SKris Kennaway 2933b4e3dcbSSimon L. B. Nielsen case SSL_PKEY_ECC: 2943b4e3dcbSSimon L. B. Nielsen /* We have an ECC key */ 2953b4e3dcbSSimon L. B. Nielsen break; 2963b4e3dcbSSimon L. B. Nielsen 29774664626SKris Kennaway default: 29874664626SKris Kennaway /* Can't happen. */ 29974664626SKris Kennaway SSLerr(SSL_F_SSL_CERT_DUP, SSL_R_LIBRARY_BUG); 30074664626SKris Kennaway } 30174664626SKris Kennaway } 30274664626SKris Kennaway } 30374664626SKris Kennaway 30474664626SKris Kennaway /* ret->extra_certs *should* exist, but currently the own certificate 30574664626SKris Kennaway * chain is held inside SSL_CTX */ 30674664626SKris Kennaway 30774664626SKris Kennaway ret->references=1; 30874664626SKris Kennaway 30974664626SKris Kennaway return(ret); 31074664626SKris Kennaway 3113b4e3dcbSSimon L. B. Nielsen #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_ECDH) 31274664626SKris Kennaway err: 3135740a5e3SKris Kennaway #endif 3145c87c606SMark Murray #ifndef OPENSSL_NO_RSA 31574664626SKris Kennaway if (ret->rsa_tmp != NULL) 31674664626SKris Kennaway RSA_free(ret->rsa_tmp); 31774664626SKris Kennaway #endif 3185c87c606SMark Murray #ifndef OPENSSL_NO_DH 31974664626SKris Kennaway if (ret->dh_tmp != NULL) 32074664626SKris Kennaway DH_free(ret->dh_tmp); 32174664626SKris Kennaway #endif 3223b4e3dcbSSimon L. B. Nielsen #ifndef OPENSSL_NO_ECDH 3233b4e3dcbSSimon L. B. Nielsen if (ret->ecdh_tmp != NULL) 3243b4e3dcbSSimon L. B. Nielsen EC_KEY_free(ret->ecdh_tmp); 3253b4e3dcbSSimon L. B. Nielsen #endif 32674664626SKris Kennaway 32774664626SKris Kennaway for (i = 0; i < SSL_PKEY_NUM; i++) 32874664626SKris Kennaway { 32974664626SKris Kennaway if (ret->pkeys[i].x509 != NULL) 33074664626SKris Kennaway X509_free(ret->pkeys[i].x509); 33174664626SKris Kennaway if (ret->pkeys[i].privatekey != NULL) 33274664626SKris Kennaway EVP_PKEY_free(ret->pkeys[i].privatekey); 33374664626SKris Kennaway } 33474664626SKris Kennaway 33574664626SKris Kennaway return NULL; 33674664626SKris Kennaway } 33774664626SKris Kennaway 33874664626SKris Kennaway 33974664626SKris Kennaway void ssl_cert_free(CERT *c) 34074664626SKris Kennaway { 34174664626SKris Kennaway int i; 34274664626SKris Kennaway 34374664626SKris Kennaway if(c == NULL) 34474664626SKris Kennaway return; 34574664626SKris Kennaway 34674664626SKris Kennaway i=CRYPTO_add(&c->references,-1,CRYPTO_LOCK_SSL_CERT); 34774664626SKris Kennaway #ifdef REF_PRINT 34874664626SKris Kennaway REF_PRINT("CERT",c); 34974664626SKris Kennaway #endif 35074664626SKris Kennaway if (i > 0) return; 35174664626SKris Kennaway #ifdef REF_CHECK 35274664626SKris Kennaway if (i < 0) 35374664626SKris Kennaway { 35474664626SKris Kennaway fprintf(stderr,"ssl_cert_free, bad reference count\n"); 35574664626SKris Kennaway abort(); /* ok */ 35674664626SKris Kennaway } 35774664626SKris Kennaway #endif 35874664626SKris Kennaway 3595c87c606SMark Murray #ifndef OPENSSL_NO_RSA 36074664626SKris Kennaway if (c->rsa_tmp) RSA_free(c->rsa_tmp); 36174664626SKris Kennaway #endif 3625c87c606SMark Murray #ifndef OPENSSL_NO_DH 36374664626SKris Kennaway if (c->dh_tmp) DH_free(c->dh_tmp); 36474664626SKris Kennaway #endif 3653b4e3dcbSSimon L. B. Nielsen #ifndef OPENSSL_NO_ECDH 3663b4e3dcbSSimon L. B. Nielsen if (c->ecdh_tmp) EC_KEY_free(c->ecdh_tmp); 3673b4e3dcbSSimon L. B. Nielsen #endif 36874664626SKris Kennaway 36974664626SKris Kennaway for (i=0; i<SSL_PKEY_NUM; i++) 37074664626SKris Kennaway { 37174664626SKris Kennaway if (c->pkeys[i].x509 != NULL) 37274664626SKris Kennaway X509_free(c->pkeys[i].x509); 37374664626SKris Kennaway if (c->pkeys[i].privatekey != NULL) 37474664626SKris Kennaway EVP_PKEY_free(c->pkeys[i].privatekey); 37574664626SKris Kennaway #if 0 37674664626SKris Kennaway if (c->pkeys[i].publickey != NULL) 37774664626SKris Kennaway EVP_PKEY_free(c->pkeys[i].publickey); 37874664626SKris Kennaway #endif 37974664626SKris Kennaway } 380ddd58736SKris Kennaway OPENSSL_free(c); 38174664626SKris Kennaway } 38274664626SKris Kennaway 38374664626SKris Kennaway int ssl_cert_inst(CERT **o) 38474664626SKris Kennaway { 38574664626SKris Kennaway /* Create a CERT if there isn't already one 38674664626SKris Kennaway * (which cannot really happen, as it is initially created in 38774664626SKris Kennaway * SSL_CTX_new; but the earlier code usually allows for that one 38874664626SKris Kennaway * being non-existant, so we follow that behaviour, as it might 38974664626SKris Kennaway * turn out that there actually is a reason for it -- but I'm 39074664626SKris Kennaway * not sure that *all* of the existing code could cope with 39174664626SKris Kennaway * s->cert being NULL, otherwise we could do without the 39274664626SKris Kennaway * initialization in SSL_CTX_new). 39374664626SKris Kennaway */ 39474664626SKris Kennaway 39574664626SKris Kennaway if (o == NULL) 39674664626SKris Kennaway { 39774664626SKris Kennaway SSLerr(SSL_F_SSL_CERT_INST, ERR_R_PASSED_NULL_PARAMETER); 39874664626SKris Kennaway return(0); 39974664626SKris Kennaway } 40074664626SKris Kennaway if (*o == NULL) 40174664626SKris Kennaway { 40274664626SKris Kennaway if ((*o = ssl_cert_new()) == NULL) 40374664626SKris Kennaway { 40474664626SKris Kennaway SSLerr(SSL_F_SSL_CERT_INST, ERR_R_MALLOC_FAILURE); 40574664626SKris Kennaway return(0); 40674664626SKris Kennaway } 40774664626SKris Kennaway } 40874664626SKris Kennaway return(1); 40974664626SKris Kennaway } 41074664626SKris Kennaway 41174664626SKris Kennaway 41274664626SKris Kennaway SESS_CERT *ssl_sess_cert_new(void) 41374664626SKris Kennaway { 41474664626SKris Kennaway SESS_CERT *ret; 41574664626SKris Kennaway 416ddd58736SKris Kennaway ret = OPENSSL_malloc(sizeof *ret); 41774664626SKris Kennaway if (ret == NULL) 41874664626SKris Kennaway { 41974664626SKris Kennaway SSLerr(SSL_F_SSL_SESS_CERT_NEW, ERR_R_MALLOC_FAILURE); 42074664626SKris Kennaway return NULL; 42174664626SKris Kennaway } 42274664626SKris Kennaway 42374664626SKris Kennaway memset(ret, 0 ,sizeof *ret); 42474664626SKris Kennaway ret->peer_key = &(ret->peer_pkeys[SSL_PKEY_RSA_ENC]); 42574664626SKris Kennaway ret->references = 1; 42674664626SKris Kennaway 42774664626SKris Kennaway return ret; 42874664626SKris Kennaway } 42974664626SKris Kennaway 43074664626SKris Kennaway void ssl_sess_cert_free(SESS_CERT *sc) 43174664626SKris Kennaway { 43274664626SKris Kennaway int i; 43374664626SKris Kennaway 43474664626SKris Kennaway if (sc == NULL) 43574664626SKris Kennaway return; 43674664626SKris Kennaway 43774664626SKris Kennaway i = CRYPTO_add(&sc->references, -1, CRYPTO_LOCK_SSL_SESS_CERT); 43874664626SKris Kennaway #ifdef REF_PRINT 43974664626SKris Kennaway REF_PRINT("SESS_CERT", sc); 44074664626SKris Kennaway #endif 44174664626SKris Kennaway if (i > 0) 44274664626SKris Kennaway return; 44374664626SKris Kennaway #ifdef REF_CHECK 44474664626SKris Kennaway if (i < 0) 44574664626SKris Kennaway { 44674664626SKris Kennaway fprintf(stderr,"ssl_sess_cert_free, bad reference count\n"); 44774664626SKris Kennaway abort(); /* ok */ 44874664626SKris Kennaway } 44974664626SKris Kennaway #endif 45074664626SKris Kennaway 45174664626SKris Kennaway /* i == 0 */ 45274664626SKris Kennaway if (sc->cert_chain != NULL) 45374664626SKris Kennaway sk_X509_pop_free(sc->cert_chain, X509_free); 45474664626SKris Kennaway for (i = 0; i < SSL_PKEY_NUM; i++) 45574664626SKris Kennaway { 45674664626SKris Kennaway if (sc->peer_pkeys[i].x509 != NULL) 45774664626SKris Kennaway X509_free(sc->peer_pkeys[i].x509); 45874664626SKris Kennaway #if 0 /* We don't have the peer's private key. These lines are just 45974664626SKris Kennaway * here as a reminder that we're still using a not-quite-appropriate 46074664626SKris Kennaway * data structure. */ 46174664626SKris Kennaway if (sc->peer_pkeys[i].privatekey != NULL) 46274664626SKris Kennaway EVP_PKEY_free(sc->peer_pkeys[i].privatekey); 46374664626SKris Kennaway #endif 46474664626SKris Kennaway } 46574664626SKris Kennaway 4665c87c606SMark Murray #ifndef OPENSSL_NO_RSA 46774664626SKris Kennaway if (sc->peer_rsa_tmp != NULL) 46874664626SKris Kennaway RSA_free(sc->peer_rsa_tmp); 46974664626SKris Kennaway #endif 4705c87c606SMark Murray #ifndef OPENSSL_NO_DH 47174664626SKris Kennaway if (sc->peer_dh_tmp != NULL) 47274664626SKris Kennaway DH_free(sc->peer_dh_tmp); 47374664626SKris Kennaway #endif 4743b4e3dcbSSimon L. B. Nielsen #ifndef OPENSSL_NO_ECDH 4753b4e3dcbSSimon L. B. Nielsen if (sc->peer_ecdh_tmp != NULL) 4763b4e3dcbSSimon L. B. Nielsen EC_KEY_free(sc->peer_ecdh_tmp); 4773b4e3dcbSSimon L. B. Nielsen #endif 47874664626SKris Kennaway 479ddd58736SKris Kennaway OPENSSL_free(sc); 48074664626SKris Kennaway } 48174664626SKris Kennaway 48274664626SKris Kennaway int ssl_set_peer_cert_type(SESS_CERT *sc,int type) 48374664626SKris Kennaway { 48474664626SKris Kennaway sc->peer_cert_type = type; 48574664626SKris Kennaway return(1); 48674664626SKris Kennaway } 48774664626SKris Kennaway 48874664626SKris Kennaway int ssl_verify_cert_chain(SSL *s,STACK_OF(X509) *sk) 48974664626SKris Kennaway { 49074664626SKris Kennaway X509 *x; 49174664626SKris Kennaway int i; 49274664626SKris Kennaway X509_STORE_CTX ctx; 49374664626SKris Kennaway 49474664626SKris Kennaway if ((sk == NULL) || (sk_X509_num(sk) == 0)) 49574664626SKris Kennaway return(0); 49674664626SKris Kennaway 49774664626SKris Kennaway x=sk_X509_value(sk,0); 4985c87c606SMark Murray if(!X509_STORE_CTX_init(&ctx,s->ctx->cert_store,x,sk)) 4995c87c606SMark Murray { 5005c87c606SMark Murray SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN,ERR_R_X509_LIB); 5015c87c606SMark Murray return(0); 5025c87c606SMark Murray } 5033b4e3dcbSSimon L. B. Nielsen if (s->param) 5043b4e3dcbSSimon L. B. Nielsen X509_VERIFY_PARAM_inherit(X509_STORE_CTX_get0_param(&ctx), 5053b4e3dcbSSimon L. B. Nielsen s->param); 5063b4e3dcbSSimon L. B. Nielsen #if 0 50774664626SKris Kennaway if (SSL_get_verify_depth(s) >= 0) 50874664626SKris Kennaway X509_STORE_CTX_set_depth(&ctx, SSL_get_verify_depth(s)); 5093b4e3dcbSSimon L. B. Nielsen #endif 510f579bf8eSKris Kennaway X509_STORE_CTX_set_ex_data(&ctx,SSL_get_ex_data_X509_STORE_CTX_idx(),s); 5115c87c606SMark Murray 5123b4e3dcbSSimon L. B. Nielsen /* We need to inherit the verify parameters. These can be determined by 513f579bf8eSKris Kennaway * the context: if its a server it will verify SSL client certificates 514f579bf8eSKris Kennaway * or vice versa. 515f579bf8eSKris Kennaway */ 516f579bf8eSKris Kennaway 5173b4e3dcbSSimon L. B. Nielsen X509_STORE_CTX_set_default(&ctx, 5183b4e3dcbSSimon L. B. Nielsen s->server ? "ssl_client" : "ssl_server"); 51974664626SKris Kennaway 520a21b1b38SKris Kennaway if (s->verify_callback) 521a21b1b38SKris Kennaway X509_STORE_CTX_set_verify_cb(&ctx, s->verify_callback); 522a21b1b38SKris Kennaway 52374664626SKris Kennaway if (s->ctx->app_verify_callback != NULL) 5245c87c606SMark Murray #if 1 /* new with OpenSSL 0.9.7 */ 5255c87c606SMark Murray i=s->ctx->app_verify_callback(&ctx, s->ctx->app_verify_arg); 5265c87c606SMark Murray #else 52774664626SKris Kennaway i=s->ctx->app_verify_callback(&ctx); /* should pass app_verify_arg */ 5285c87c606SMark Murray #endif 52974664626SKris Kennaway else 53074664626SKris Kennaway { 5315c87c606SMark Murray #ifndef OPENSSL_NO_X509_VERIFY 53274664626SKris Kennaway i=X509_verify_cert(&ctx); 53374664626SKris Kennaway #else 53474664626SKris Kennaway i=0; 53574664626SKris Kennaway ctx.error=X509_V_ERR_APPLICATION_VERIFICATION; 53674664626SKris Kennaway SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN,SSL_R_NO_VERIFY_CALLBACK); 53774664626SKris Kennaway #endif 53874664626SKris Kennaway } 53974664626SKris Kennaway 54074664626SKris Kennaway s->verify_result=ctx.error; 54174664626SKris Kennaway X509_STORE_CTX_cleanup(&ctx); 54274664626SKris Kennaway 54374664626SKris Kennaway return(i); 54474664626SKris Kennaway } 54574664626SKris Kennaway 546ced566fdSJacques Vidrine static void set_client_CA_list(STACK_OF(X509_NAME) **ca_list,STACK_OF(X509_NAME) *name_list) 54774664626SKris Kennaway { 54874664626SKris Kennaway if (*ca_list != NULL) 54974664626SKris Kennaway sk_X509_NAME_pop_free(*ca_list,X509_NAME_free); 55074664626SKris Kennaway 551ced566fdSJacques Vidrine *ca_list=name_list; 55274664626SKris Kennaway } 55374664626SKris Kennaway 55474664626SKris Kennaway STACK_OF(X509_NAME) *SSL_dup_CA_list(STACK_OF(X509_NAME) *sk) 55574664626SKris Kennaway { 55674664626SKris Kennaway int i; 55774664626SKris Kennaway STACK_OF(X509_NAME) *ret; 55874664626SKris Kennaway X509_NAME *name; 55974664626SKris Kennaway 56074664626SKris Kennaway ret=sk_X509_NAME_new_null(); 56174664626SKris Kennaway for (i=0; i<sk_X509_NAME_num(sk); i++) 56274664626SKris Kennaway { 56374664626SKris Kennaway name=X509_NAME_dup(sk_X509_NAME_value(sk,i)); 56474664626SKris Kennaway if ((name == NULL) || !sk_X509_NAME_push(ret,name)) 56574664626SKris Kennaway { 56674664626SKris Kennaway sk_X509_NAME_pop_free(ret,X509_NAME_free); 56774664626SKris Kennaway return(NULL); 56874664626SKris Kennaway } 56974664626SKris Kennaway } 57074664626SKris Kennaway return(ret); 57174664626SKris Kennaway } 57274664626SKris Kennaway 573ced566fdSJacques Vidrine void SSL_set_client_CA_list(SSL *s,STACK_OF(X509_NAME) *name_list) 57474664626SKris Kennaway { 575ced566fdSJacques Vidrine set_client_CA_list(&(s->client_CA),name_list); 57674664626SKris Kennaway } 57774664626SKris Kennaway 578ced566fdSJacques Vidrine void SSL_CTX_set_client_CA_list(SSL_CTX *ctx,STACK_OF(X509_NAME) *name_list) 57974664626SKris Kennaway { 580ced566fdSJacques Vidrine set_client_CA_list(&(ctx->client_CA),name_list); 58174664626SKris Kennaway } 58274664626SKris Kennaway 5833b4e3dcbSSimon L. B. Nielsen STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx) 58474664626SKris Kennaway { 58574664626SKris Kennaway return(ctx->client_CA); 58674664626SKris Kennaway } 58774664626SKris Kennaway 5883b4e3dcbSSimon L. B. Nielsen STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *s) 58974664626SKris Kennaway { 59074664626SKris Kennaway if (s->type == SSL_ST_CONNECT) 59174664626SKris Kennaway { /* we are in the client */ 59274664626SKris Kennaway if (((s->version>>8) == SSL3_VERSION_MAJOR) && 59374664626SKris Kennaway (s->s3 != NULL)) 59474664626SKris Kennaway return(s->s3->tmp.ca_names); 59574664626SKris Kennaway else 59674664626SKris Kennaway return(NULL); 59774664626SKris Kennaway } 59874664626SKris Kennaway else 59974664626SKris Kennaway { 60074664626SKris Kennaway if (s->client_CA != NULL) 60174664626SKris Kennaway return(s->client_CA); 60274664626SKris Kennaway else 60374664626SKris Kennaway return(s->ctx->client_CA); 60474664626SKris Kennaway } 60574664626SKris Kennaway } 60674664626SKris Kennaway 60774664626SKris Kennaway static int add_client_CA(STACK_OF(X509_NAME) **sk,X509 *x) 60874664626SKris Kennaway { 60974664626SKris Kennaway X509_NAME *name; 61074664626SKris Kennaway 61174664626SKris Kennaway if (x == NULL) return(0); 61274664626SKris Kennaway if ((*sk == NULL) && ((*sk=sk_X509_NAME_new_null()) == NULL)) 61374664626SKris Kennaway return(0); 61474664626SKris Kennaway 61574664626SKris Kennaway if ((name=X509_NAME_dup(X509_get_subject_name(x))) == NULL) 61674664626SKris Kennaway return(0); 61774664626SKris Kennaway 61874664626SKris Kennaway if (!sk_X509_NAME_push(*sk,name)) 61974664626SKris Kennaway { 62074664626SKris Kennaway X509_NAME_free(name); 62174664626SKris Kennaway return(0); 62274664626SKris Kennaway } 62374664626SKris Kennaway return(1); 62474664626SKris Kennaway } 62574664626SKris Kennaway 62674664626SKris Kennaway int SSL_add_client_CA(SSL *ssl,X509 *x) 62774664626SKris Kennaway { 62874664626SKris Kennaway return(add_client_CA(&(ssl->client_CA),x)); 62974664626SKris Kennaway } 63074664626SKris Kennaway 63174664626SKris Kennaway int SSL_CTX_add_client_CA(SSL_CTX *ctx,X509 *x) 63274664626SKris Kennaway { 63374664626SKris Kennaway return(add_client_CA(&(ctx->client_CA),x)); 63474664626SKris Kennaway } 63574664626SKris Kennaway 636ddd58736SKris Kennaway static int xname_cmp(const X509_NAME * const *a, const X509_NAME * const *b) 63774664626SKris Kennaway { 63874664626SKris Kennaway return(X509_NAME_cmp(*a,*b)); 63974664626SKris Kennaway } 64074664626SKris Kennaway 6415c87c606SMark Murray #ifndef OPENSSL_NO_STDIO 64274664626SKris Kennaway /*! 64374664626SKris Kennaway * Load CA certs from a file into a ::STACK. Note that it is somewhat misnamed; 64474664626SKris Kennaway * it doesn't really have anything to do with clients (except that a common use 64574664626SKris Kennaway * for a stack of CAs is to send it to the client). Actually, it doesn't have 64674664626SKris Kennaway * much to do with CAs, either, since it will load any old cert. 64774664626SKris Kennaway * \param file the file containing one or more certs. 64874664626SKris Kennaway * \return a ::STACK containing the certs. 64974664626SKris Kennaway */ 65074664626SKris Kennaway STACK_OF(X509_NAME) *SSL_load_client_CA_file(const char *file) 65174664626SKris Kennaway { 65274664626SKris Kennaway BIO *in; 65374664626SKris Kennaway X509 *x=NULL; 65474664626SKris Kennaway X509_NAME *xn=NULL; 6553b4e3dcbSSimon L. B. Nielsen STACK_OF(X509_NAME) *ret = NULL,*sk; 65674664626SKris Kennaway 657f579bf8eSKris Kennaway sk=sk_X509_NAME_new(xname_cmp); 65874664626SKris Kennaway 65974664626SKris Kennaway in=BIO_new(BIO_s_file_internal()); 66074664626SKris Kennaway 6613b4e3dcbSSimon L. B. Nielsen if ((sk == NULL) || (in == NULL)) 66274664626SKris Kennaway { 66374664626SKris Kennaway SSLerr(SSL_F_SSL_LOAD_CLIENT_CA_FILE,ERR_R_MALLOC_FAILURE); 66474664626SKris Kennaway goto err; 66574664626SKris Kennaway } 66674664626SKris Kennaway 66774664626SKris Kennaway if (!BIO_read_filename(in,file)) 66874664626SKris Kennaway goto err; 66974664626SKris Kennaway 67074664626SKris Kennaway for (;;) 67174664626SKris Kennaway { 67274664626SKris Kennaway if (PEM_read_bio_X509(in,&x,NULL,NULL) == NULL) 67374664626SKris Kennaway break; 6743b4e3dcbSSimon L. B. Nielsen if (ret == NULL) 6753b4e3dcbSSimon L. B. Nielsen { 6763b4e3dcbSSimon L. B. Nielsen ret = sk_X509_NAME_new_null(); 6773b4e3dcbSSimon L. B. Nielsen if (ret == NULL) 6783b4e3dcbSSimon L. B. Nielsen { 6793b4e3dcbSSimon L. B. Nielsen SSLerr(SSL_F_SSL_LOAD_CLIENT_CA_FILE,ERR_R_MALLOC_FAILURE); 6803b4e3dcbSSimon L. B. Nielsen goto err; 6813b4e3dcbSSimon L. B. Nielsen } 6823b4e3dcbSSimon L. B. Nielsen } 68374664626SKris Kennaway if ((xn=X509_get_subject_name(x)) == NULL) goto err; 68474664626SKris Kennaway /* check for duplicates */ 68574664626SKris Kennaway xn=X509_NAME_dup(xn); 68674664626SKris Kennaway if (xn == NULL) goto err; 68774664626SKris Kennaway if (sk_X509_NAME_find(sk,xn) >= 0) 68874664626SKris Kennaway X509_NAME_free(xn); 68974664626SKris Kennaway else 69074664626SKris Kennaway { 69174664626SKris Kennaway sk_X509_NAME_push(sk,xn); 69274664626SKris Kennaway sk_X509_NAME_push(ret,xn); 69374664626SKris Kennaway } 69474664626SKris Kennaway } 69574664626SKris Kennaway 69674664626SKris Kennaway if (0) 69774664626SKris Kennaway { 69874664626SKris Kennaway err: 69974664626SKris Kennaway if (ret != NULL) sk_X509_NAME_pop_free(ret,X509_NAME_free); 70074664626SKris Kennaway ret=NULL; 70174664626SKris Kennaway } 70274664626SKris Kennaway if (sk != NULL) sk_X509_NAME_free(sk); 70374664626SKris Kennaway if (in != NULL) BIO_free(in); 70474664626SKris Kennaway if (x != NULL) X509_free(x); 7053b4e3dcbSSimon L. B. Nielsen if (ret != NULL) 7063b4e3dcbSSimon L. B. Nielsen ERR_clear_error(); 70774664626SKris Kennaway return(ret); 70874664626SKris Kennaway } 70974664626SKris Kennaway #endif 71074664626SKris Kennaway 71174664626SKris Kennaway /*! 71274664626SKris Kennaway * Add a file of certs to a stack. 71374664626SKris Kennaway * \param stack the stack to add to. 71474664626SKris Kennaway * \param file the file to add from. All certs in this file that are not 71574664626SKris Kennaway * already in the stack will be added. 71674664626SKris Kennaway * \return 1 for success, 0 for failure. Note that in the case of failure some 71774664626SKris Kennaway * certs may have been added to \c stack. 71874664626SKris Kennaway */ 71974664626SKris Kennaway 72074664626SKris Kennaway int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack, 72174664626SKris Kennaway const char *file) 72274664626SKris Kennaway { 72374664626SKris Kennaway BIO *in; 72474664626SKris Kennaway X509 *x=NULL; 72574664626SKris Kennaway X509_NAME *xn=NULL; 72674664626SKris Kennaway int ret=1; 727ddd58736SKris Kennaway int (*oldcmp)(const X509_NAME * const *a, const X509_NAME * const *b); 72874664626SKris Kennaway 729f579bf8eSKris Kennaway oldcmp=sk_X509_NAME_set_cmp_func(stack,xname_cmp); 73074664626SKris Kennaway 73174664626SKris Kennaway in=BIO_new(BIO_s_file_internal()); 73274664626SKris Kennaway 73374664626SKris Kennaway if (in == NULL) 73474664626SKris Kennaway { 73574664626SKris Kennaway SSLerr(SSL_F_SSL_ADD_FILE_CERT_SUBJECTS_TO_STACK,ERR_R_MALLOC_FAILURE); 73674664626SKris Kennaway goto err; 73774664626SKris Kennaway } 73874664626SKris Kennaway 73974664626SKris Kennaway if (!BIO_read_filename(in,file)) 74074664626SKris Kennaway goto err; 74174664626SKris Kennaway 74274664626SKris Kennaway for (;;) 74374664626SKris Kennaway { 74474664626SKris Kennaway if (PEM_read_bio_X509(in,&x,NULL,NULL) == NULL) 74574664626SKris Kennaway break; 74674664626SKris Kennaway if ((xn=X509_get_subject_name(x)) == NULL) goto err; 74774664626SKris Kennaway xn=X509_NAME_dup(xn); 74874664626SKris Kennaway if (xn == NULL) goto err; 74974664626SKris Kennaway if (sk_X509_NAME_find(stack,xn) >= 0) 75074664626SKris Kennaway X509_NAME_free(xn); 75174664626SKris Kennaway else 75274664626SKris Kennaway sk_X509_NAME_push(stack,xn); 75374664626SKris Kennaway } 75474664626SKris Kennaway 75574664626SKris Kennaway if (0) 75674664626SKris Kennaway { 75774664626SKris Kennaway err: 75874664626SKris Kennaway ret=0; 75974664626SKris Kennaway } 76074664626SKris Kennaway if(in != NULL) 76174664626SKris Kennaway BIO_free(in); 76274664626SKris Kennaway if(x != NULL) 76374664626SKris Kennaway X509_free(x); 76474664626SKris Kennaway 76574664626SKris Kennaway sk_X509_NAME_set_cmp_func(stack,oldcmp); 76674664626SKris Kennaway 76774664626SKris Kennaway return ret; 76874664626SKris Kennaway } 76974664626SKris Kennaway 77074664626SKris Kennaway /*! 77174664626SKris Kennaway * Add a directory of certs to a stack. 77274664626SKris Kennaway * \param stack the stack to append to. 77374664626SKris Kennaway * \param dir the directory to append from. All files in this directory will be 77474664626SKris Kennaway * examined as potential certs. Any that are acceptable to 77574664626SKris Kennaway * SSL_add_dir_cert_subjects_to_stack() that are not already in the stack will be 77674664626SKris Kennaway * included. 77774664626SKris Kennaway * \return 1 for success, 0 for failure. Note that in the case of failure some 77874664626SKris Kennaway * certs may have been added to \c stack. 77974664626SKris Kennaway */ 78074664626SKris Kennaway 78174664626SKris Kennaway int SSL_add_dir_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack, 78274664626SKris Kennaway const char *dir) 78374664626SKris Kennaway { 7843b4e3dcbSSimon L. B. Nielsen OPENSSL_DIR_CTX *d = NULL; 7853b4e3dcbSSimon L. B. Nielsen const char *filename; 78674664626SKris Kennaway int ret = 0; 78774664626SKris Kennaway 78874664626SKris Kennaway CRYPTO_w_lock(CRYPTO_LOCK_READDIR); 78974664626SKris Kennaway 79074664626SKris Kennaway /* Note that a side effect is that the CAs will be sorted by name */ 79174664626SKris Kennaway 7923b4e3dcbSSimon L. B. Nielsen while((filename = OPENSSL_DIR_read(&d, dir))) 79374664626SKris Kennaway { 79474664626SKris Kennaway char buf[1024]; 795ddd58736SKris Kennaway int r; 79674664626SKris Kennaway 7973b4e3dcbSSimon L. B. Nielsen if(strlen(dir)+strlen(filename)+2 > sizeof buf) 79874664626SKris Kennaway { 79974664626SKris Kennaway SSLerr(SSL_F_SSL_ADD_DIR_CERT_SUBJECTS_TO_STACK,SSL_R_PATH_TOO_LONG); 80074664626SKris Kennaway goto err; 80174664626SKris Kennaway } 80274664626SKris Kennaway 8033b4e3dcbSSimon L. B. Nielsen #ifdef OPENSSL_SYS_VMS 8043b4e3dcbSSimon L. B. Nielsen r = BIO_snprintf(buf,sizeof buf,"%s%s",dir,filename); 8053b4e3dcbSSimon L. B. Nielsen #else 8063b4e3dcbSSimon L. B. Nielsen r = BIO_snprintf(buf,sizeof buf,"%s/%s",dir,filename); 8073b4e3dcbSSimon L. B. Nielsen #endif 8083b4e3dcbSSimon L. B. Nielsen if (r <= 0 || r >= (int)sizeof(buf)) 809ddd58736SKris Kennaway goto err; 81074664626SKris Kennaway if(!SSL_add_file_cert_subjects_to_stack(stack,buf)) 81174664626SKris Kennaway goto err; 81274664626SKris Kennaway } 81374664626SKris Kennaway 8143b4e3dcbSSimon L. B. Nielsen if (errno) 8155c87c606SMark Murray { 8165c87c606SMark Murray SYSerr(SYS_F_OPENDIR, get_last_sys_error()); 8173b4e3dcbSSimon L. B. Nielsen ERR_add_error_data(3, "OPENSSL_DIR_read(&ctx, '", dir, "')"); 8185c87c606SMark Murray SSLerr(SSL_F_SSL_ADD_DIR_CERT_SUBJECTS_TO_STACK, ERR_R_SYS_LIB); 8195c87c606SMark Murray goto err; 8205c87c606SMark Murray } 8215c87c606SMark Murray 8225c87c606SMark Murray ret = 1; 8235c87c606SMark Murray 8245c87c606SMark Murray err: 8253b4e3dcbSSimon L. B. Nielsen if (d) OPENSSL_DIR_end(&d); 8265c87c606SMark Murray CRYPTO_w_unlock(CRYPTO_LOCK_READDIR); 8275c87c606SMark Murray return ret; 8285c87c606SMark Murray } 8295c87c606SMark Murray 830