sl@0: /*! \file ssl/ssl_cert.c */ sl@0: /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) sl@0: * All rights reserved. sl@0: * sl@0: * This package is an SSL implementation written sl@0: * by Eric Young (eay@cryptsoft.com). sl@0: * The implementation was written so as to conform with Netscapes SSL. sl@0: * sl@0: * This library is free for commercial and non-commercial use as long as sl@0: * the following conditions are aheared to. The following conditions sl@0: * apply to all code found in this distribution, be it the RC4, RSA, sl@0: * lhash, DES, etc., code; not just the SSL code. The SSL documentation sl@0: * included with this distribution is covered by the same copyright terms sl@0: * except that the holder is Tim Hudson (tjh@cryptsoft.com). sl@0: * sl@0: * Copyright remains Eric Young's, and as such any Copyright notices in sl@0: * the code are not to be removed. sl@0: * If this package is used in a product, Eric Young should be given attribution sl@0: * as the author of the parts of the library used. sl@0: * This can be in the form of a textual message at program startup or sl@0: * in documentation (online or textual) provided with the package. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * 1. Redistributions of source code must retain the copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in the sl@0: * documentation and/or other materials provided with the distribution. sl@0: * 3. All advertising materials mentioning features or use of this software sl@0: * must display the following acknowledgement: sl@0: * "This product includes cryptographic software written by sl@0: * Eric Young (eay@cryptsoft.com)" sl@0: * The word 'cryptographic' can be left out if the rouines from the library sl@0: * being used are not cryptographic related :-). sl@0: * 4. If you include any Windows specific code (or a derivative thereof) from sl@0: * the apps directory (application code) you must include an acknowledgement: sl@0: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND sl@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE sl@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE sl@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL sl@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS sl@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT sl@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY sl@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF sl@0: * SUCH DAMAGE. sl@0: * sl@0: * The licence and distribution terms for any publically available version or sl@0: * derivative of this code cannot be changed. i.e. this code cannot simply be sl@0: * copied and put under another distribution licence sl@0: * [including the GNU Public Licence.] sl@0: */ sl@0: /* ==================================================================== sl@0: * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * sl@0: * 1. Redistributions of source code must retain the above copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in sl@0: * the documentation and/or other materials provided with the sl@0: * distribution. sl@0: * sl@0: * 3. All advertising materials mentioning features or use of this sl@0: * software must display the following acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" sl@0: * sl@0: * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to sl@0: * endorse or promote products derived from this software without sl@0: * prior written permission. For written permission, please contact sl@0: * openssl-core@openssl.org. sl@0: * sl@0: * 5. Products derived from this software may not be called "OpenSSL" sl@0: * nor may "OpenSSL" appear in their names without prior written sl@0: * permission of the OpenSSL Project. sl@0: * sl@0: * 6. Redistributions of any form whatsoever must retain the following sl@0: * acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit (http://www.openssl.org/)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY sl@0: * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR sl@0: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR sl@0: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, sl@0: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT sl@0: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; sl@0: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, sl@0: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) sl@0: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED sl@0: * OF THE POSSIBILITY OF SUCH DAMAGE. sl@0: * ==================================================================== sl@0: * sl@0: * This product includes cryptographic software written by Eric Young sl@0: * (eay@cryptsoft.com). This product includes software written by Tim sl@0: * Hudson (tjh@cryptsoft.com). sl@0: * sl@0: */ sl@0: /* ==================================================================== sl@0: * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. sl@0: * ECC cipher suite support in OpenSSL originally developed by sl@0: * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. sl@0: */ sl@0: /* sl@0: © Portions copyright (c) 2006 Nokia Corporation. All rights reserved. sl@0: */ sl@0: sl@0: #include sl@0: sl@0: #include "e_os.h" sl@0: #ifndef NO_SYS_TYPES_H sl@0: # include sl@0: #endif sl@0: sl@0: #include "o_dir.h" sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #ifndef OPENSSL_NO_DH sl@0: #include sl@0: #endif sl@0: #include sl@0: #include "ssl_locl.h" sl@0: sl@0: #ifdef EMULATOR sl@0: sl@0: GET_STATIC_VAR_FROM_TLS(ssl_x509_store_ctx_idx,ssl_cert,volatile int) sl@0: sl@0: #define ssl_x509_store_ctx_idx (*GET_WSD_VAR_NAME(ssl_x509_store_ctx_idx,ssl_cert,s)()) sl@0: sl@0: #endif sl@0: sl@0: sl@0: EXPORT_C int SSL_get_ex_data_X509_STORE_CTX_idx(void) sl@0: { sl@0: #ifndef EMULATOR sl@0: static volatile int ssl_x509_store_ctx_idx= -1; sl@0: #endif sl@0: int got_write_lock = 0; sl@0: sl@0: CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX); sl@0: sl@0: sl@0: if (ssl_x509_store_ctx_idx < 0) sl@0: { sl@0: CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX); sl@0: CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX); sl@0: got_write_lock = 1; sl@0: sl@0: if (ssl_x509_store_ctx_idx < 0) sl@0: { sl@0: ssl_x509_store_ctx_idx=X509_STORE_CTX_get_ex_new_index( sl@0: 0,"SSL for verify callback",NULL,NULL,NULL); sl@0: } sl@0: } sl@0: sl@0: if (got_write_lock) sl@0: CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX); sl@0: else sl@0: CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX); sl@0: sl@0: return ssl_x509_store_ctx_idx; sl@0: } sl@0: sl@0: CERT *ssl_cert_new(void) sl@0: { sl@0: CERT *ret; sl@0: sl@0: ret=(CERT *)OPENSSL_malloc(sizeof(CERT)); sl@0: if (ret == NULL) sl@0: { sl@0: SSLerr(SSL_F_SSL_CERT_NEW,ERR_R_MALLOC_FAILURE); sl@0: return(NULL); sl@0: } sl@0: memset(ret,0,sizeof(CERT)); sl@0: sl@0: ret->key= &(ret->pkeys[SSL_PKEY_RSA_ENC]); sl@0: ret->references=1; sl@0: sl@0: return(ret); sl@0: } sl@0: sl@0: CERT *ssl_cert_dup(CERT *cert) sl@0: { sl@0: CERT *ret; sl@0: int i; sl@0: sl@0: ret = (CERT *)OPENSSL_malloc(sizeof(CERT)); sl@0: if (ret == NULL) sl@0: { sl@0: SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_MALLOC_FAILURE); sl@0: return(NULL); sl@0: } sl@0: sl@0: memset(ret, 0, sizeof(CERT)); sl@0: sl@0: ret->key = &ret->pkeys[cert->key - &cert->pkeys[0]]; sl@0: /* or ret->key = ret->pkeys + (cert->key - cert->pkeys), sl@0: * if you find that more readable */ sl@0: sl@0: ret->valid = cert->valid; sl@0: ret->mask = cert->mask; sl@0: ret->export_mask = cert->export_mask; sl@0: sl@0: #ifndef OPENSSL_NO_RSA sl@0: if (cert->rsa_tmp != NULL) sl@0: { sl@0: RSA_up_ref(cert->rsa_tmp); sl@0: ret->rsa_tmp = cert->rsa_tmp; sl@0: } sl@0: ret->rsa_tmp_cb = cert->rsa_tmp_cb; sl@0: #endif sl@0: sl@0: #ifndef OPENSSL_NO_DH sl@0: if (cert->dh_tmp != NULL) sl@0: { sl@0: ret->dh_tmp = DHparams_dup(cert->dh_tmp); sl@0: if (ret->dh_tmp == NULL) sl@0: { sl@0: SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_DH_LIB); sl@0: goto err; sl@0: } sl@0: if (cert->dh_tmp->priv_key) sl@0: { sl@0: BIGNUM *b = BN_dup(cert->dh_tmp->priv_key); sl@0: if (!b) sl@0: { sl@0: SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_BN_LIB); sl@0: goto err; sl@0: } sl@0: ret->dh_tmp->priv_key = b; sl@0: } sl@0: if (cert->dh_tmp->pub_key) sl@0: { sl@0: BIGNUM *b = BN_dup(cert->dh_tmp->pub_key); sl@0: if (!b) sl@0: { sl@0: SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_BN_LIB); sl@0: goto err; sl@0: } sl@0: ret->dh_tmp->pub_key = b; sl@0: } sl@0: } sl@0: ret->dh_tmp_cb = cert->dh_tmp_cb; sl@0: #endif sl@0: sl@0: #ifndef OPENSSL_NO_ECDH sl@0: if (cert->ecdh_tmp) sl@0: { sl@0: ret->ecdh_tmp = EC_KEY_dup(cert->ecdh_tmp); sl@0: if (ret->ecdh_tmp == NULL) sl@0: { sl@0: SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_EC_LIB); sl@0: goto err; sl@0: } sl@0: } sl@0: ret->ecdh_tmp_cb = cert->ecdh_tmp_cb; sl@0: #endif sl@0: sl@0: for (i = 0; i < SSL_PKEY_NUM; i++) sl@0: { sl@0: if (cert->pkeys[i].x509 != NULL) sl@0: { sl@0: ret->pkeys[i].x509 = cert->pkeys[i].x509; sl@0: CRYPTO_add(&ret->pkeys[i].x509->references, 1, sl@0: CRYPTO_LOCK_X509); sl@0: } sl@0: sl@0: if (cert->pkeys[i].privatekey != NULL) sl@0: { sl@0: ret->pkeys[i].privatekey = cert->pkeys[i].privatekey; sl@0: CRYPTO_add(&ret->pkeys[i].privatekey->references, 1, sl@0: CRYPTO_LOCK_EVP_PKEY); sl@0: sl@0: switch(i) sl@0: { sl@0: /* If there was anything special to do for sl@0: * certain types of keys, we'd do it here. sl@0: * (Nothing at the moment, I think.) */ sl@0: sl@0: case SSL_PKEY_RSA_ENC: sl@0: case SSL_PKEY_RSA_SIGN: sl@0: /* We have an RSA key. */ sl@0: break; sl@0: sl@0: case SSL_PKEY_DSA_SIGN: sl@0: /* We have a DSA key. */ sl@0: break; sl@0: sl@0: case SSL_PKEY_DH_RSA: sl@0: case SSL_PKEY_DH_DSA: sl@0: /* We have a DH key. */ sl@0: break; sl@0: sl@0: case SSL_PKEY_ECC: sl@0: /* We have an ECC key */ sl@0: break; sl@0: sl@0: default: sl@0: /* Can't happen. */ sl@0: SSLerr(SSL_F_SSL_CERT_DUP, SSL_R_LIBRARY_BUG); sl@0: } sl@0: } sl@0: } sl@0: sl@0: /* ret->extra_certs *should* exist, but currently the own certificate sl@0: * chain is held inside SSL_CTX */ sl@0: sl@0: ret->references=1; sl@0: sl@0: return(ret); sl@0: sl@0: #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_ECDH) sl@0: err: sl@0: #endif sl@0: #ifndef OPENSSL_NO_RSA sl@0: if (ret->rsa_tmp != NULL) sl@0: RSA_free(ret->rsa_tmp); sl@0: #endif sl@0: #ifndef OPENSSL_NO_DH sl@0: if (ret->dh_tmp != NULL) sl@0: DH_free(ret->dh_tmp); sl@0: #endif sl@0: #ifndef OPENSSL_NO_ECDH sl@0: if (ret->ecdh_tmp != NULL) sl@0: EC_KEY_free(ret->ecdh_tmp); sl@0: #endif sl@0: sl@0: for (i = 0; i < SSL_PKEY_NUM; i++) sl@0: { sl@0: if (ret->pkeys[i].x509 != NULL) sl@0: X509_free(ret->pkeys[i].x509); sl@0: if (ret->pkeys[i].privatekey != NULL) sl@0: EVP_PKEY_free(ret->pkeys[i].privatekey); sl@0: } sl@0: sl@0: return NULL; sl@0: } sl@0: sl@0: sl@0: void ssl_cert_free(CERT *c) sl@0: { sl@0: int i; sl@0: sl@0: if(c == NULL) sl@0: return; sl@0: sl@0: i=CRYPTO_add(&c->references,-1,CRYPTO_LOCK_SSL_CERT); sl@0: #ifdef REF_PRINT sl@0: REF_PRINT("CERT",c); sl@0: #endif sl@0: if (i > 0) return; sl@0: #ifdef REF_CHECK sl@0: if (i < 0) sl@0: { sl@0: fprintf(stderr,"ssl_cert_free, bad reference count\n"); sl@0: abort(); /* ok */ sl@0: } sl@0: #endif sl@0: sl@0: #ifndef OPENSSL_NO_RSA sl@0: if (c->rsa_tmp) RSA_free(c->rsa_tmp); sl@0: #endif sl@0: #ifndef OPENSSL_NO_DH sl@0: if (c->dh_tmp) DH_free(c->dh_tmp); sl@0: #endif sl@0: #ifndef OPENSSL_NO_ECDH sl@0: if (c->ecdh_tmp) EC_KEY_free(c->ecdh_tmp); sl@0: #endif sl@0: sl@0: for (i=0; ipkeys[i].x509 != NULL) sl@0: X509_free(c->pkeys[i].x509); sl@0: if (c->pkeys[i].privatekey != NULL) sl@0: EVP_PKEY_free(c->pkeys[i].privatekey); sl@0: #if 0 sl@0: if (c->pkeys[i].publickey != NULL) sl@0: EVP_PKEY_free(c->pkeys[i].publickey); sl@0: #endif sl@0: } sl@0: OPENSSL_free(c); sl@0: } sl@0: sl@0: int ssl_cert_inst(CERT **o) sl@0: { sl@0: /* Create a CERT if there isn't already one sl@0: * (which cannot really happen, as it is initially created in sl@0: * SSL_CTX_new; but the earlier code usually allows for that one sl@0: * being non-existant, so we follow that behaviour, as it might sl@0: * turn out that there actually is a reason for it -- but I'm sl@0: * not sure that *all* of the existing code could cope with sl@0: * s->cert being NULL, otherwise we could do without the sl@0: * initialization in SSL_CTX_new). sl@0: */ sl@0: sl@0: if (o == NULL) sl@0: { sl@0: SSLerr(SSL_F_SSL_CERT_INST, ERR_R_PASSED_NULL_PARAMETER); sl@0: return(0); sl@0: } sl@0: if (*o == NULL) sl@0: { sl@0: if ((*o = ssl_cert_new()) == NULL) sl@0: { sl@0: SSLerr(SSL_F_SSL_CERT_INST, ERR_R_MALLOC_FAILURE); sl@0: return(0); sl@0: } sl@0: } sl@0: return(1); sl@0: } sl@0: sl@0: sl@0: SESS_CERT *ssl_sess_cert_new(void) sl@0: { sl@0: SESS_CERT *ret; sl@0: sl@0: ret = OPENSSL_malloc(sizeof *ret); sl@0: if (ret == NULL) sl@0: { sl@0: SSLerr(SSL_F_SSL_SESS_CERT_NEW, ERR_R_MALLOC_FAILURE); sl@0: return NULL; sl@0: } sl@0: sl@0: memset(ret, 0 ,sizeof *ret); sl@0: ret->peer_key = &(ret->peer_pkeys[SSL_PKEY_RSA_ENC]); sl@0: ret->references = 1; sl@0: sl@0: return ret; sl@0: } sl@0: sl@0: void ssl_sess_cert_free(SESS_CERT *sc) sl@0: { sl@0: int i; sl@0: sl@0: if (sc == NULL) sl@0: return; sl@0: sl@0: i = CRYPTO_add(&sc->references, -1, CRYPTO_LOCK_SSL_SESS_CERT); sl@0: #ifdef REF_PRINT sl@0: REF_PRINT("SESS_CERT", sc); sl@0: #endif sl@0: if (i > 0) sl@0: return; sl@0: #ifdef REF_CHECK sl@0: if (i < 0) sl@0: { sl@0: fprintf(stderr,"ssl_sess_cert_free, bad reference count\n"); sl@0: abort(); /* ok */ sl@0: } sl@0: #endif sl@0: sl@0: /* i == 0 */ sl@0: if (sc->cert_chain != NULL) sl@0: sk_X509_pop_free(sc->cert_chain, X509_free); sl@0: for (i = 0; i < SSL_PKEY_NUM; i++) sl@0: { sl@0: if (sc->peer_pkeys[i].x509 != NULL) sl@0: X509_free(sc->peer_pkeys[i].x509); sl@0: #if 0 /* We don't have the peer's private key. These lines are just sl@0: * here as a reminder that we're still using a not-quite-appropriate sl@0: * data structure. */ sl@0: if (sc->peer_pkeys[i].privatekey != NULL) sl@0: EVP_PKEY_free(sc->peer_pkeys[i].privatekey); sl@0: #endif sl@0: } sl@0: sl@0: #ifndef OPENSSL_NO_RSA sl@0: if (sc->peer_rsa_tmp != NULL) sl@0: RSA_free(sc->peer_rsa_tmp); sl@0: #endif sl@0: #ifndef OPENSSL_NO_DH sl@0: if (sc->peer_dh_tmp != NULL) sl@0: DH_free(sc->peer_dh_tmp); sl@0: #endif sl@0: #ifndef OPENSSL_NO_ECDH sl@0: if (sc->peer_ecdh_tmp != NULL) sl@0: EC_KEY_free(sc->peer_ecdh_tmp); sl@0: #endif sl@0: sl@0: OPENSSL_free(sc); sl@0: } sl@0: sl@0: int ssl_set_peer_cert_type(SESS_CERT *sc,int type) sl@0: { sl@0: sc->peer_cert_type = type; sl@0: return(1); sl@0: } sl@0: sl@0: int ssl_verify_cert_chain(SSL *s,STACK_OF(X509) *sk) sl@0: { sl@0: X509 *x; sl@0: int i; sl@0: X509_STORE_CTX ctx; sl@0: sl@0: if ((sk == NULL) || (sk_X509_num(sk) == 0)) sl@0: return(0); sl@0: sl@0: x=sk_X509_value(sk,0); sl@0: if(!X509_STORE_CTX_init(&ctx,s->ctx->cert_store,x,sk)) sl@0: { sl@0: SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN,ERR_R_X509_LIB); sl@0: return(0); sl@0: } sl@0: if (s->param) sl@0: X509_VERIFY_PARAM_inherit(X509_STORE_CTX_get0_param(&ctx), sl@0: s->param); sl@0: #if 0 sl@0: if (SSL_get_verify_depth(s) >= 0) sl@0: X509_STORE_CTX_set_depth(&ctx, SSL_get_verify_depth(s)); sl@0: #endif sl@0: X509_STORE_CTX_set_ex_data(&ctx,SSL_get_ex_data_X509_STORE_CTX_idx(),s); sl@0: sl@0: /* We need to inherit the verify parameters. These can be determined by sl@0: * the context: if its a server it will verify SSL client certificates sl@0: * or vice versa. sl@0: */ sl@0: sl@0: X509_STORE_CTX_set_default(&ctx, sl@0: s->server ? "ssl_client" : "ssl_server"); sl@0: sl@0: if (s->verify_callback) sl@0: X509_STORE_CTX_set_verify_cb(&ctx, s->verify_callback); sl@0: sl@0: if (s->ctx->app_verify_callback != NULL) sl@0: #if 1 /* new with OpenSSL 0.9.7 */ sl@0: i=s->ctx->app_verify_callback(&ctx, s->ctx->app_verify_arg); sl@0: #else sl@0: i=s->ctx->app_verify_callback(&ctx); /* should pass app_verify_arg */ sl@0: #endif sl@0: else sl@0: { sl@0: #ifndef OPENSSL_NO_X509_VERIFY sl@0: i=X509_verify_cert(&ctx); sl@0: #else sl@0: i=0; sl@0: ctx.error=X509_V_ERR_APPLICATION_VERIFICATION; sl@0: SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN,SSL_R_NO_VERIFY_CALLBACK); sl@0: #endif sl@0: } sl@0: sl@0: s->verify_result=ctx.error; sl@0: X509_STORE_CTX_cleanup(&ctx); sl@0: sl@0: return(i); sl@0: } sl@0: sl@0: static void set_client_CA_list(STACK_OF(X509_NAME) **ca_list,STACK_OF(X509_NAME) *name_list) sl@0: { sl@0: if (*ca_list != NULL) sl@0: sk_X509_NAME_pop_free(*ca_list,X509_NAME_free); sl@0: sl@0: *ca_list=name_list; sl@0: } sl@0: sl@0: EXPORT_C STACK_OF(X509_NAME) *SSL_dup_CA_list(STACK_OF(X509_NAME) *sk) sl@0: { sl@0: int i; sl@0: STACK_OF(X509_NAME) *ret; sl@0: X509_NAME *name; sl@0: sl@0: ret=sk_X509_NAME_new_null(); sl@0: for (i=0; iclient_CA),name_list); sl@0: } sl@0: sl@0: EXPORT_C void SSL_CTX_set_client_CA_list(SSL_CTX *ctx,STACK_OF(X509_NAME) *name_list) sl@0: { sl@0: set_client_CA_list(&(ctx->client_CA),name_list); sl@0: } sl@0: sl@0: EXPORT_C STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx) sl@0: { sl@0: return(ctx->client_CA); sl@0: } sl@0: sl@0: EXPORT_C STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *s) sl@0: { sl@0: if (s->type == SSL_ST_CONNECT) sl@0: { /* we are in the client */ sl@0: if (((s->version>>8) == SSL3_VERSION_MAJOR) && sl@0: (s->s3 != NULL)) sl@0: return(s->s3->tmp.ca_names); sl@0: else sl@0: return(NULL); sl@0: } sl@0: else sl@0: { sl@0: if (s->client_CA != NULL) sl@0: return(s->client_CA); sl@0: else sl@0: return(s->ctx->client_CA); sl@0: } sl@0: } sl@0: sl@0: static int add_client_CA(STACK_OF(X509_NAME) **sk,X509 *x) sl@0: { sl@0: X509_NAME *name; sl@0: sl@0: if (x == NULL) return(0); sl@0: if ((*sk == NULL) && ((*sk=sk_X509_NAME_new_null()) == NULL)) sl@0: return(0); sl@0: sl@0: if ((name=X509_NAME_dup(X509_get_subject_name(x))) == NULL) sl@0: return(0); sl@0: sl@0: if (!sk_X509_NAME_push(*sk,name)) sl@0: { sl@0: X509_NAME_free(name); sl@0: return(0); sl@0: } sl@0: return(1); sl@0: } sl@0: sl@0: EXPORT_C int SSL_add_client_CA(SSL *ssl,X509 *x) sl@0: { sl@0: return(add_client_CA(&(ssl->client_CA),x)); sl@0: } sl@0: sl@0: EXPORT_C int SSL_CTX_add_client_CA(SSL_CTX *ctx,X509 *x) sl@0: { sl@0: return(add_client_CA(&(ctx->client_CA),x)); sl@0: } sl@0: sl@0: static int xname_cmp(const X509_NAME * const *a, const X509_NAME * const *b) sl@0: { sl@0: return(X509_NAME_cmp(*a,*b)); sl@0: } sl@0: sl@0: #ifndef OPENSSL_NO_STDIO sl@0: /*! sl@0: * Load CA certs from a file into a ::STACK. Note that it is somewhat misnamed; sl@0: * it doesn't really have anything to do with clients (except that a common use sl@0: * for a stack of CAs is to send it to the client). Actually, it doesn't have sl@0: * much to do with CAs, either, since it will load any old cert. sl@0: * \param file the file containing one or more certs. sl@0: * \return a ::STACK containing the certs. sl@0: */ sl@0: EXPORT_C STACK_OF(X509_NAME) *SSL_load_client_CA_file(const char *file) sl@0: { sl@0: BIO *in; sl@0: X509 *x=NULL; sl@0: X509_NAME *xn=NULL; sl@0: STACK_OF(X509_NAME) *ret = NULL,*sk; sl@0: sl@0: sk=sk_X509_NAME_new(xname_cmp); sl@0: sl@0: in=BIO_new(BIO_s_file_internal()); sl@0: sl@0: if ((sk == NULL) || (in == NULL)) sl@0: { sl@0: SSLerr(SSL_F_SSL_LOAD_CLIENT_CA_FILE,ERR_R_MALLOC_FAILURE); sl@0: goto err; sl@0: } sl@0: sl@0: if (!BIO_read_filename(in,file)) sl@0: goto err; sl@0: sl@0: for (;;) sl@0: { sl@0: if (PEM_read_bio_X509(in,&x,NULL,NULL) == NULL) sl@0: break; sl@0: if (ret == NULL) sl@0: { sl@0: ret = sk_X509_NAME_new_null(); sl@0: if (ret == NULL) sl@0: { sl@0: SSLerr(SSL_F_SSL_LOAD_CLIENT_CA_FILE,ERR_R_MALLOC_FAILURE); sl@0: goto err; sl@0: } sl@0: } sl@0: if ((xn=X509_get_subject_name(x)) == NULL) goto err; sl@0: /* check for duplicates */ sl@0: xn=X509_NAME_dup(xn); sl@0: if (xn == NULL) goto err; sl@0: if (sk_X509_NAME_find(sk,xn) >= 0) sl@0: X509_NAME_free(xn); sl@0: else sl@0: { sl@0: sk_X509_NAME_push(sk,xn); sl@0: sk_X509_NAME_push(ret,xn); sl@0: } sl@0: } sl@0: sl@0: if (0) sl@0: { sl@0: err: sl@0: if (ret != NULL) sk_X509_NAME_pop_free(ret,X509_NAME_free); sl@0: ret=NULL; sl@0: } sl@0: if (sk != NULL) sk_X509_NAME_free(sk); sl@0: if (in != NULL) BIO_free(in); sl@0: if (x != NULL) X509_free(x); sl@0: if (ret != NULL) sl@0: ERR_clear_error(); sl@0: return(ret); sl@0: } sl@0: #endif sl@0: sl@0: /*! sl@0: * Add a file of certs to a stack. sl@0: * \param stack the stack to add to. sl@0: * \param file the file to add from. All certs in this file that are not sl@0: * already in the stack will be added. sl@0: * \return 1 for success, 0 for failure. Note that in the case of failure some sl@0: * certs may have been added to \c stack. sl@0: */ sl@0: sl@0: EXPORT_C int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack, sl@0: const char *file) sl@0: { sl@0: BIO *in; sl@0: X509 *x=NULL; sl@0: X509_NAME *xn=NULL; sl@0: int ret=1; sl@0: int (*oldcmp)(const X509_NAME * const *a, const X509_NAME * const *b); sl@0: sl@0: oldcmp=sk_X509_NAME_set_cmp_func(stack,xname_cmp); sl@0: sl@0: in=BIO_new(BIO_s_file_internal()); sl@0: sl@0: if (in == NULL) sl@0: { sl@0: SSLerr(SSL_F_SSL_ADD_FILE_CERT_SUBJECTS_TO_STACK,ERR_R_MALLOC_FAILURE); sl@0: goto err; sl@0: } sl@0: sl@0: if (!BIO_read_filename(in,file)) sl@0: goto err; sl@0: sl@0: for (;;) sl@0: { sl@0: if (PEM_read_bio_X509(in,&x,NULL,NULL) == NULL) sl@0: break; sl@0: if ((xn=X509_get_subject_name(x)) == NULL) goto err; sl@0: xn=X509_NAME_dup(xn); sl@0: if (xn == NULL) goto err; sl@0: if (sk_X509_NAME_find(stack,xn) >= 0) sl@0: X509_NAME_free(xn); sl@0: else sl@0: sk_X509_NAME_push(stack,xn); sl@0: } sl@0: sl@0: if (0) sl@0: { sl@0: err: sl@0: ret=0; sl@0: } sl@0: if(in != NULL) sl@0: BIO_free(in); sl@0: if(x != NULL) sl@0: X509_free(x); sl@0: sl@0: (void)sk_X509_NAME_set_cmp_func(stack,oldcmp); sl@0: sl@0: return ret; sl@0: } sl@0: sl@0: /*! sl@0: * Add a directory of certs to a stack. sl@0: * \param stack the stack to append to. sl@0: * \param dir the directory to append from. All files in this directory will be sl@0: * examined as potential certs. Any that are acceptable to sl@0: * SSL_add_dir_cert_subjects_to_stack() that are not already in the stack will be sl@0: * included. sl@0: * \return 1 for success, 0 for failure. Note that in the case of failure some sl@0: * certs may have been added to \c stack. sl@0: */ sl@0: sl@0: EXPORT_C int SSL_add_dir_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack, sl@0: const char *dir) sl@0: { sl@0: OPENSSL_DIR_CTX *d = NULL; sl@0: const char *filename; sl@0: int ret = 0; sl@0: sl@0: CRYPTO_w_lock(CRYPTO_LOCK_READDIR); sl@0: sl@0: /* Note that a side effect is that the CAs will be sorted by name */ sl@0: sl@0: while((filename = OPENSSL_DIR_read(&d, dir))) sl@0: { sl@0: char buf[1024]; sl@0: int r; sl@0: sl@0: if(strlen(dir)+strlen(filename)+2 > sizeof buf) sl@0: { sl@0: SSLerr(SSL_F_SSL_ADD_DIR_CERT_SUBJECTS_TO_STACK,SSL_R_PATH_TOO_LONG); sl@0: goto err; sl@0: } sl@0: sl@0: #ifdef OPENSSL_SYS_VMS sl@0: r = BIO_snprintf(buf,sizeof buf,"%s%s",dir,filename); sl@0: #else sl@0: r = BIO_snprintf(buf,sizeof buf,"%s/%s",dir,filename); sl@0: #endif sl@0: if (r <= 0 || r >= (int)sizeof(buf)) sl@0: goto err; sl@0: if(!SSL_add_file_cert_subjects_to_stack(stack,buf)) sl@0: goto err; sl@0: } sl@0: sl@0: if (errno) sl@0: { sl@0: SYSerr(SYS_F_OPENDIR, get_last_sys_error()); sl@0: ERR_add_error_data(3, "OPENSSL_DIR_read(&ctx, '", dir, "')"); sl@0: SSLerr(SSL_F_SSL_ADD_DIR_CERT_SUBJECTS_TO_STACK, ERR_R_SYS_LIB); sl@0: goto err; sl@0: } sl@0: sl@0: ret = 1; sl@0: sl@0: err: sl@0: if (d) OPENSSL_DIR_end(&d); sl@0: CRYPTO_w_unlock(CRYPTO_LOCK_READDIR); sl@0: return ret; sl@0: } sl@0: