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