ssl_cert.c revision 59191
155714Skris/*! \file ssl/ssl_cert.c */ 255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 355714Skris * All rights reserved. 455714Skris * 555714Skris * This package is an SSL implementation written 655714Skris * by Eric Young (eay@cryptsoft.com). 755714Skris * The implementation was written so as to conform with Netscapes SSL. 855714Skris * 955714Skris * This library is free for commercial and non-commercial use as long as 1055714Skris * the following conditions are aheared to. The following conditions 1155714Skris * apply to all code found in this distribution, be it the RC4, RSA, 1255714Skris * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1355714Skris * included with this distribution is covered by the same copyright terms 1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com). 1555714Skris * 1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in 1755714Skris * the code are not to be removed. 1855714Skris * If this package is used in a product, Eric Young should be given attribution 1955714Skris * as the author of the parts of the library used. 2055714Skris * This can be in the form of a textual message at program startup or 2155714Skris * in documentation (online or textual) provided with the package. 2255714Skris * 2355714Skris * Redistribution and use in source and binary forms, with or without 2455714Skris * modification, are permitted provided that the following conditions 2555714Skris * are met: 2655714Skris * 1. Redistributions of source code must retain the copyright 2755714Skris * notice, this list of conditions and the following disclaimer. 2855714Skris * 2. Redistributions in binary form must reproduce the above copyright 2955714Skris * notice, this list of conditions and the following disclaimer in the 3055714Skris * documentation and/or other materials provided with the distribution. 3155714Skris * 3. All advertising materials mentioning features or use of this software 3255714Skris * must display the following acknowledgement: 3355714Skris * "This product includes cryptographic software written by 3455714Skris * Eric Young (eay@cryptsoft.com)" 3555714Skris * The word 'cryptographic' can be left out if the rouines from the library 3655714Skris * being used are not cryptographic related :-). 3755714Skris * 4. If you include any Windows specific code (or a derivative thereof) from 3855714Skris * the apps directory (application code) you must include an acknowledgement: 3955714Skris * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 4055714Skris * 4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4455714Skris * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5155714Skris * SUCH DAMAGE. 5255714Skris * 5355714Skris * The licence and distribution terms for any publically available version or 5455714Skris * derivative of this code cannot be changed. i.e. this code cannot simply be 5555714Skris * copied and put under another distribution licence 5655714Skris * [including the GNU Public Licence.] 5755714Skris */ 5855714Skris/* ==================================================================== 5955714Skris * Copyright (c) 1999 The OpenSSL Project. All rights reserved. 6055714Skris * 6155714Skris * Redistribution and use in source and binary forms, with or without 6255714Skris * modification, are permitted provided that the following conditions 6355714Skris * are met: 6455714Skris * 6555714Skris * 1. Redistributions of source code must retain the above copyright 6655714Skris * notice, this list of conditions and the following disclaimer. 6755714Skris * 6855714Skris * 2. Redistributions in binary form must reproduce the above copyright 6955714Skris * notice, this list of conditions and the following disclaimer in 7055714Skris * the documentation and/or other materials provided with the 7155714Skris * distribution. 7255714Skris * 7355714Skris * 3. All advertising materials mentioning features or use of this 7455714Skris * software must display the following acknowledgment: 7555714Skris * "This product includes software developed by the OpenSSL Project 7655714Skris * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 7755714Skris * 7855714Skris * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 7955714Skris * endorse or promote products derived from this software without 8055714Skris * prior written permission. For written permission, please contact 8155714Skris * openssl-core@OpenSSL.org. 8255714Skris * 8355714Skris * 5. Products derived from this software may not be called "OpenSSL" 8455714Skris * nor may "OpenSSL" appear in their names without prior written 8555714Skris * permission of the OpenSSL Project. 8655714Skris * 8755714Skris * 6. Redistributions of any form whatsoever must retain the following 8855714Skris * acknowledgment: 8955714Skris * "This product includes software developed by the OpenSSL Project 9055714Skris * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 9155714Skris * 9255714Skris * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 9355714Skris * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 9455714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 9555714Skris * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 9655714Skris * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 9755714Skris * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 9855714Skris * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 9955714Skris * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 10055714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 10155714Skris * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 10255714Skris * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 10355714Skris * OF THE POSSIBILITY OF SUCH DAMAGE. 10455714Skris * ==================================================================== 10555714Skris */ 10655714Skris 10755714Skris#include <stdio.h> 10859191Skris 10959191Skris#include "openssl/e_os.h" 11059191Skris 11159191Skris#ifndef NO_SYS_TYPES_H 11259191Skris# include <sys/types.h> 11359191Skris#endif 11459191Skris 11559191Skris#if !defined(WIN32) && !defined(VSM) && !defined(NeXT) && !defined(MAC_OS_pre_X) 11655714Skris#include <dirent.h> 11755714Skris#endif 11859191Skris 11955714Skris#ifdef NeXT 12055714Skris#include <sys/dir.h> 12155714Skris#define dirent direct 12255714Skris#endif 12359191Skris 12455714Skris#include <openssl/objects.h> 12555714Skris#include <openssl/bio.h> 12655714Skris#include <openssl/pem.h> 12759191Skris#include <openssl/x509v3.h> 12855714Skris#include "ssl_locl.h" 12955714Skris 13055714Skrisint SSL_get_ex_data_X509_STORE_CTX_idx(void) 13155714Skris { 13255714Skris static int ssl_x509_store_ctx_idx= -1; 13355714Skris 13455714Skris if (ssl_x509_store_ctx_idx < 0) 13555714Skris { 13655714Skris ssl_x509_store_ctx_idx=X509_STORE_CTX_get_ex_new_index( 13755714Skris 0,"SSL for verify callback",NULL,NULL,NULL); 13855714Skris } 13955714Skris return(ssl_x509_store_ctx_idx); 14055714Skris } 14155714Skris 14255714SkrisCERT *ssl_cert_new(void) 14355714Skris { 14455714Skris CERT *ret; 14555714Skris 14655714Skris ret=(CERT *)Malloc(sizeof(CERT)); 14755714Skris if (ret == NULL) 14855714Skris { 14955714Skris SSLerr(SSL_F_SSL_CERT_NEW,ERR_R_MALLOC_FAILURE); 15055714Skris return(NULL); 15155714Skris } 15255714Skris memset(ret,0,sizeof(CERT)); 15355714Skris 15455714Skris ret->key= &(ret->pkeys[SSL_PKEY_RSA_ENC]); 15555714Skris ret->references=1; 15655714Skris 15755714Skris return(ret); 15855714Skris } 15955714Skris 16055714SkrisCERT *ssl_cert_dup(CERT *cert) 16155714Skris { 16255714Skris CERT *ret; 16355714Skris int i; 16455714Skris 16555714Skris ret = (CERT *)Malloc(sizeof(CERT)); 16655714Skris if (ret == NULL) 16755714Skris { 16855714Skris SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_MALLOC_FAILURE); 16955714Skris return(NULL); 17055714Skris } 17155714Skris 17255714Skris memset(ret, 0, sizeof(CERT)); 17355714Skris 17455714Skris ret->key = &ret->pkeys[cert->key - &cert->pkeys[0]]; 17555714Skris /* or ret->key = ret->pkeys + (cert->key - cert->pkeys), 17655714Skris * if you find that more readable */ 17755714Skris 17855714Skris ret->valid = cert->valid; 17955714Skris ret->mask = cert->mask; 18055714Skris ret->export_mask = cert->export_mask; 18155714Skris 18255714Skris#ifndef NO_RSA 18355714Skris if (cert->rsa_tmp != NULL) 18455714Skris { 18555714Skris ret->rsa_tmp = cert->rsa_tmp; 18655714Skris CRYPTO_add(&ret->rsa_tmp->references, 1, CRYPTO_LOCK_RSA); 18755714Skris } 18855714Skris ret->rsa_tmp_cb = cert->rsa_tmp_cb; 18955714Skris#endif 19055714Skris 19155714Skris#ifndef NO_DH 19255714Skris if (cert->dh_tmp != NULL) 19355714Skris { 19459191Skris /* DH parameters don't have a reference count */ 19555714Skris ret->dh_tmp = DHparams_dup(cert->dh_tmp); 19655714Skris if (ret->dh_tmp == NULL) 19755714Skris { 19859191Skris SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_DH_LIB); 19955714Skris goto err; 20055714Skris } 20159191Skris if (cert->dh_tmp->priv_key) 20259191Skris { 20359191Skris BIGNUM *b = BN_dup(cert->dh_tmp->priv_key); 20459191Skris if (!b) 20559191Skris { 20659191Skris SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_BN_LIB); 20759191Skris goto err; 20859191Skris } 20959191Skris ret->dh_tmp->priv_key = b; 21059191Skris } 21159191Skris if (cert->dh_tmp->pub_key) 21259191Skris { 21359191Skris BIGNUM *b = BN_dup(cert->dh_tmp->pub_key); 21459191Skris if (!b) 21559191Skris { 21659191Skris SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_BN_LIB); 21759191Skris goto err; 21859191Skris } 21959191Skris ret->dh_tmp->pub_key = b; 22059191Skris } 22155714Skris } 22255714Skris ret->dh_tmp_cb = cert->dh_tmp_cb; 22355714Skris#endif 22455714Skris 22555714Skris for (i = 0; i < SSL_PKEY_NUM; i++) 22655714Skris { 22755714Skris if (cert->pkeys[i].x509 != NULL) 22855714Skris { 22955714Skris ret->pkeys[i].x509 = cert->pkeys[i].x509; 23055714Skris CRYPTO_add(&ret->pkeys[i].x509->references, 1, 23155714Skris CRYPTO_LOCK_X509); 23255714Skris } 23355714Skris 23455714Skris if (cert->pkeys[i].privatekey != NULL) 23555714Skris { 23655714Skris ret->pkeys[i].privatekey = cert->pkeys[i].privatekey; 23755714Skris CRYPTO_add(&ret->pkeys[i].privatekey->references, 1, 23855714Skris CRYPTO_LOCK_EVP_PKEY); 23955714Skris 24055714Skris switch(i) 24155714Skris { 24255714Skris /* If there was anything special to do for 24355714Skris * certain types of keys, we'd do it here. 24455714Skris * (Nothing at the moment, I think.) */ 24555714Skris 24655714Skris case SSL_PKEY_RSA_ENC: 24755714Skris case SSL_PKEY_RSA_SIGN: 24855714Skris /* We have an RSA key. */ 24955714Skris break; 25055714Skris 25155714Skris case SSL_PKEY_DSA_SIGN: 25255714Skris /* We have a DSA key. */ 25355714Skris break; 25455714Skris 25555714Skris case SSL_PKEY_DH_RSA: 25655714Skris case SSL_PKEY_DH_DSA: 25755714Skris /* We have a DH key. */ 25855714Skris break; 25955714Skris 26055714Skris default: 26155714Skris /* Can't happen. */ 26255714Skris SSLerr(SSL_F_SSL_CERT_DUP, SSL_R_LIBRARY_BUG); 26355714Skris } 26455714Skris } 26555714Skris } 26655714Skris 26755714Skris /* ret->extra_certs *should* exist, but currently the own certificate 26855714Skris * chain is held inside SSL_CTX */ 26955714Skris 27055714Skris ret->references=1; 27155714Skris 27255714Skris return(ret); 27355714Skris 27455714Skriserr: 27555714Skris#ifndef NO_RSA 27655714Skris if (ret->rsa_tmp != NULL) 27755714Skris RSA_free(ret->rsa_tmp); 27855714Skris#endif 27955714Skris#ifndef NO_DH 28055714Skris if (ret->dh_tmp != NULL) 28155714Skris DH_free(ret->dh_tmp); 28255714Skris#endif 28355714Skris 28455714Skris for (i = 0; i < SSL_PKEY_NUM; i++) 28555714Skris { 28655714Skris if (ret->pkeys[i].x509 != NULL) 28755714Skris X509_free(ret->pkeys[i].x509); 28855714Skris if (ret->pkeys[i].privatekey != NULL) 28955714Skris EVP_PKEY_free(ret->pkeys[i].privatekey); 29055714Skris } 29155714Skris 29255714Skris return NULL; 29355714Skris } 29455714Skris 29555714Skris 29655714Skrisvoid ssl_cert_free(CERT *c) 29755714Skris { 29855714Skris int i; 29955714Skris 30055714Skris if(c == NULL) 30155714Skris return; 30255714Skris 30355714Skris i=CRYPTO_add(&c->references,-1,CRYPTO_LOCK_SSL_CERT); 30455714Skris#ifdef REF_PRINT 30555714Skris REF_PRINT("CERT",c); 30655714Skris#endif 30755714Skris if (i > 0) return; 30855714Skris#ifdef REF_CHECK 30955714Skris if (i < 0) 31055714Skris { 31155714Skris fprintf(stderr,"ssl_cert_free, bad reference count\n"); 31255714Skris abort(); /* ok */ 31355714Skris } 31455714Skris#endif 31555714Skris 31655714Skris#ifndef NO_RSA 31755714Skris if (c->rsa_tmp) RSA_free(c->rsa_tmp); 31855714Skris#endif 31955714Skris#ifndef NO_DH 32055714Skris if (c->dh_tmp) DH_free(c->dh_tmp); 32155714Skris#endif 32255714Skris 32355714Skris for (i=0; i<SSL_PKEY_NUM; i++) 32455714Skris { 32555714Skris if (c->pkeys[i].x509 != NULL) 32655714Skris X509_free(c->pkeys[i].x509); 32755714Skris if (c->pkeys[i].privatekey != NULL) 32855714Skris EVP_PKEY_free(c->pkeys[i].privatekey); 32955714Skris#if 0 33055714Skris if (c->pkeys[i].publickey != NULL) 33155714Skris EVP_PKEY_free(c->pkeys[i].publickey); 33255714Skris#endif 33355714Skris } 33455714Skris Free(c); 33555714Skris } 33655714Skris 33755714Skrisint ssl_cert_inst(CERT **o) 33855714Skris { 33955714Skris /* Create a CERT if there isn't already one 34055714Skris * (which cannot really happen, as it is initially created in 34155714Skris * SSL_CTX_new; but the earlier code usually allows for that one 34255714Skris * being non-existant, so we follow that behaviour, as it might 34355714Skris * turn out that there actually is a reason for it -- but I'm 34455714Skris * not sure that *all* of the existing code could cope with 34555714Skris * s->cert being NULL, otherwise we could do without the 34655714Skris * initialization in SSL_CTX_new). 34755714Skris */ 34855714Skris 34955714Skris if (o == NULL) 35055714Skris { 35155714Skris SSLerr(SSL_F_SSL_CERT_INST, ERR_R_PASSED_NULL_PARAMETER); 35255714Skris return(0); 35355714Skris } 35455714Skris if (*o == NULL) 35555714Skris { 35655714Skris if ((*o = ssl_cert_new()) == NULL) 35755714Skris { 35855714Skris SSLerr(SSL_F_SSL_CERT_INST, ERR_R_MALLOC_FAILURE); 35955714Skris return(0); 36055714Skris } 36155714Skris } 36255714Skris return(1); 36355714Skris } 36455714Skris 36555714Skris 36655714SkrisSESS_CERT *ssl_sess_cert_new(void) 36755714Skris { 36855714Skris SESS_CERT *ret; 36955714Skris 37055714Skris ret = Malloc(sizeof *ret); 37155714Skris if (ret == NULL) 37255714Skris { 37355714Skris SSLerr(SSL_F_SSL_SESS_CERT_NEW, ERR_R_MALLOC_FAILURE); 37455714Skris return NULL; 37555714Skris } 37655714Skris 37755714Skris memset(ret, 0 ,sizeof *ret); 37855714Skris ret->peer_key = &(ret->peer_pkeys[SSL_PKEY_RSA_ENC]); 37955714Skris ret->references = 1; 38055714Skris 38155714Skris return ret; 38255714Skris } 38355714Skris 38455714Skrisvoid ssl_sess_cert_free(SESS_CERT *sc) 38555714Skris { 38655714Skris int i; 38755714Skris 38855714Skris if (sc == NULL) 38955714Skris return; 39055714Skris 39155714Skris i = CRYPTO_add(&sc->references, -1, CRYPTO_LOCK_SSL_SESS_CERT); 39255714Skris#ifdef REF_PRINT 39355714Skris REF_PRINT("SESS_CERT", sc); 39455714Skris#endif 39555714Skris if (i > 0) 39655714Skris return; 39755714Skris#ifdef REF_CHECK 39855714Skris if (i < 0) 39955714Skris { 40055714Skris fprintf(stderr,"ssl_sess_cert_free, bad reference count\n"); 40155714Skris abort(); /* ok */ 40255714Skris } 40355714Skris#endif 40455714Skris 40555714Skris /* i == 0 */ 40655714Skris if (sc->cert_chain != NULL) 40755714Skris sk_X509_pop_free(sc->cert_chain, X509_free); 40855714Skris for (i = 0; i < SSL_PKEY_NUM; i++) 40955714Skris { 41055714Skris if (sc->peer_pkeys[i].x509 != NULL) 41155714Skris X509_free(sc->peer_pkeys[i].x509); 41255714Skris#if 0 /* We don't have the peer's private key. These lines are just 41355714Skris * here as a reminder that we're still using a not-quite-appropriate 41455714Skris * data structure. */ 41555714Skris if (sc->peer_pkeys[i].privatekey != NULL) 41655714Skris EVP_PKEY_free(sc->peer_pkeys[i].privatekey); 41755714Skris#endif 41855714Skris } 41955714Skris 42055714Skris#ifndef NO_RSA 42155714Skris if (sc->peer_rsa_tmp != NULL) 42255714Skris RSA_free(sc->peer_rsa_tmp); 42355714Skris#endif 42455714Skris#ifndef NO_DH 42555714Skris if (sc->peer_dh_tmp != NULL) 42655714Skris DH_free(sc->peer_dh_tmp); 42755714Skris#endif 42855714Skris 42955714Skris Free(sc); 43055714Skris } 43155714Skris 43255714Skrisint ssl_set_peer_cert_type(SESS_CERT *sc,int type) 43355714Skris { 43455714Skris sc->peer_cert_type = type; 43555714Skris return(1); 43655714Skris } 43755714Skris 43855714Skrisint ssl_verify_cert_chain(SSL *s,STACK_OF(X509) *sk) 43955714Skris { 44055714Skris X509 *x; 44155714Skris int i; 44255714Skris X509_STORE_CTX ctx; 44355714Skris 44455714Skris if ((sk == NULL) || (sk_X509_num(sk) == 0)) 44555714Skris return(0); 44655714Skris 44755714Skris x=sk_X509_value(sk,0); 44855714Skris X509_STORE_CTX_init(&ctx,s->ctx->cert_store,x,sk); 44955714Skris if (SSL_get_verify_depth(s) >= 0) 45055714Skris X509_STORE_CTX_set_depth(&ctx, SSL_get_verify_depth(s)); 45159191Skris X509_STORE_CTX_set_ex_data(&ctx,SSL_get_ex_data_X509_STORE_CTX_idx(),s); 45259191Skris /* We need to set the verify purpose. The purpose can be determined by 45359191Skris * the context: if its a server it will verify SSL client certificates 45459191Skris * or vice versa. 45559191Skris */ 45655714Skris 45759191Skris if(s->server) i = X509_PURPOSE_SSL_CLIENT; 45859191Skris else i = X509_PURPOSE_SSL_SERVER; 45959191Skris 46059191Skris X509_STORE_CTX_purpose_inherit(&ctx, i, s->purpose, s->trust); 46159191Skris 46255714Skris if (s->ctx->app_verify_callback != NULL) 46355714Skris i=s->ctx->app_verify_callback(&ctx); /* should pass app_verify_arg */ 46455714Skris else 46555714Skris { 46655714Skris#ifndef NO_X509_VERIFY 46755714Skris i=X509_verify_cert(&ctx); 46855714Skris#else 46955714Skris i=0; 47055714Skris ctx.error=X509_V_ERR_APPLICATION_VERIFICATION; 47155714Skris SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN,SSL_R_NO_VERIFY_CALLBACK); 47255714Skris#endif 47355714Skris } 47455714Skris 47555714Skris s->verify_result=ctx.error; 47655714Skris X509_STORE_CTX_cleanup(&ctx); 47755714Skris 47855714Skris return(i); 47955714Skris } 48055714Skris 48155714Skrisstatic void set_client_CA_list(STACK_OF(X509_NAME) **ca_list,STACK_OF(X509_NAME) *list) 48255714Skris { 48355714Skris if (*ca_list != NULL) 48455714Skris sk_X509_NAME_pop_free(*ca_list,X509_NAME_free); 48555714Skris 48655714Skris *ca_list=list; 48755714Skris } 48855714Skris 48955714SkrisSTACK_OF(X509_NAME) *SSL_dup_CA_list(STACK_OF(X509_NAME) *sk) 49055714Skris { 49155714Skris int i; 49255714Skris STACK_OF(X509_NAME) *ret; 49355714Skris X509_NAME *name; 49455714Skris 49555714Skris ret=sk_X509_NAME_new_null(); 49655714Skris for (i=0; i<sk_X509_NAME_num(sk); i++) 49755714Skris { 49855714Skris name=X509_NAME_dup(sk_X509_NAME_value(sk,i)); 49955714Skris if ((name == NULL) || !sk_X509_NAME_push(ret,name)) 50055714Skris { 50155714Skris sk_X509_NAME_pop_free(ret,X509_NAME_free); 50255714Skris return(NULL); 50355714Skris } 50455714Skris } 50555714Skris return(ret); 50655714Skris } 50755714Skris 50855714Skrisvoid SSL_set_client_CA_list(SSL *s,STACK_OF(X509_NAME) *list) 50955714Skris { 51055714Skris set_client_CA_list(&(s->client_CA),list); 51155714Skris } 51255714Skris 51355714Skrisvoid SSL_CTX_set_client_CA_list(SSL_CTX *ctx,STACK_OF(X509_NAME) *list) 51455714Skris { 51555714Skris set_client_CA_list(&(ctx->client_CA),list); 51655714Skris } 51755714Skris 51855714SkrisSTACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(SSL_CTX *ctx) 51955714Skris { 52055714Skris return(ctx->client_CA); 52155714Skris } 52255714Skris 52355714SkrisSTACK_OF(X509_NAME) *SSL_get_client_CA_list(SSL *s) 52455714Skris { 52555714Skris if (s->type == SSL_ST_CONNECT) 52655714Skris { /* we are in the client */ 52755714Skris if (((s->version>>8) == SSL3_VERSION_MAJOR) && 52855714Skris (s->s3 != NULL)) 52955714Skris return(s->s3->tmp.ca_names); 53055714Skris else 53155714Skris return(NULL); 53255714Skris } 53355714Skris else 53455714Skris { 53555714Skris if (s->client_CA != NULL) 53655714Skris return(s->client_CA); 53755714Skris else 53855714Skris return(s->ctx->client_CA); 53955714Skris } 54055714Skris } 54155714Skris 54255714Skrisstatic int add_client_CA(STACK_OF(X509_NAME) **sk,X509 *x) 54355714Skris { 54455714Skris X509_NAME *name; 54555714Skris 54655714Skris if (x == NULL) return(0); 54755714Skris if ((*sk == NULL) && ((*sk=sk_X509_NAME_new_null()) == NULL)) 54855714Skris return(0); 54955714Skris 55055714Skris if ((name=X509_NAME_dup(X509_get_subject_name(x))) == NULL) 55155714Skris return(0); 55255714Skris 55355714Skris if (!sk_X509_NAME_push(*sk,name)) 55455714Skris { 55555714Skris X509_NAME_free(name); 55655714Skris return(0); 55755714Skris } 55855714Skris return(1); 55955714Skris } 56055714Skris 56155714Skrisint SSL_add_client_CA(SSL *ssl,X509 *x) 56255714Skris { 56355714Skris return(add_client_CA(&(ssl->client_CA),x)); 56455714Skris } 56555714Skris 56655714Skrisint SSL_CTX_add_client_CA(SSL_CTX *ctx,X509 *x) 56755714Skris { 56855714Skris return(add_client_CA(&(ctx->client_CA),x)); 56955714Skris } 57055714Skris 57159191Skrisstatic int xname_cmp(X509_NAME **a,X509_NAME **b) 57255714Skris { 57355714Skris return(X509_NAME_cmp(*a,*b)); 57455714Skris } 57555714Skris 57655714Skris#ifndef NO_STDIO 57755714Skris/*! 57855714Skris * Load CA certs from a file into a ::STACK. Note that it is somewhat misnamed; 57955714Skris * it doesn't really have anything to do with clients (except that a common use 58055714Skris * for a stack of CAs is to send it to the client). Actually, it doesn't have 58155714Skris * much to do with CAs, either, since it will load any old cert. 58255714Skris * \param file the file containing one or more certs. 58355714Skris * \return a ::STACK containing the certs. 58455714Skris */ 58555714SkrisSTACK_OF(X509_NAME) *SSL_load_client_CA_file(const char *file) 58655714Skris { 58755714Skris BIO *in; 58855714Skris X509 *x=NULL; 58955714Skris X509_NAME *xn=NULL; 59055714Skris STACK_OF(X509_NAME) *ret,*sk; 59155714Skris 59255714Skris ret=sk_X509_NAME_new(NULL); 59359191Skris sk=sk_X509_NAME_new(xname_cmp); 59455714Skris 59555714Skris in=BIO_new(BIO_s_file_internal()); 59655714Skris 59755714Skris if ((ret == NULL) || (sk == NULL) || (in == NULL)) 59855714Skris { 59955714Skris SSLerr(SSL_F_SSL_LOAD_CLIENT_CA_FILE,ERR_R_MALLOC_FAILURE); 60055714Skris goto err; 60155714Skris } 60255714Skris 60355714Skris if (!BIO_read_filename(in,file)) 60455714Skris goto err; 60555714Skris 60655714Skris for (;;) 60755714Skris { 60855714Skris if (PEM_read_bio_X509(in,&x,NULL,NULL) == NULL) 60955714Skris break; 61055714Skris if ((xn=X509_get_subject_name(x)) == NULL) goto err; 61155714Skris /* check for duplicates */ 61255714Skris xn=X509_NAME_dup(xn); 61355714Skris if (xn == NULL) goto err; 61455714Skris if (sk_X509_NAME_find(sk,xn) >= 0) 61555714Skris X509_NAME_free(xn); 61655714Skris else 61755714Skris { 61855714Skris sk_X509_NAME_push(sk,xn); 61955714Skris sk_X509_NAME_push(ret,xn); 62055714Skris } 62155714Skris } 62255714Skris 62355714Skris if (0) 62455714Skris { 62555714Skriserr: 62655714Skris if (ret != NULL) sk_X509_NAME_pop_free(ret,X509_NAME_free); 62755714Skris ret=NULL; 62855714Skris } 62955714Skris if (sk != NULL) sk_X509_NAME_free(sk); 63055714Skris if (in != NULL) BIO_free(in); 63155714Skris if (x != NULL) X509_free(x); 63255714Skris return(ret); 63355714Skris } 63455714Skris#endif 63555714Skris 63655714Skris/*! 63755714Skris * Add a file of certs to a stack. 63855714Skris * \param stack the stack to add to. 63955714Skris * \param file the file to add from. All certs in this file that are not 64055714Skris * already in the stack will be added. 64155714Skris * \return 1 for success, 0 for failure. Note that in the case of failure some 64255714Skris * certs may have been added to \c stack. 64355714Skris */ 64455714Skris 64555714Skrisint SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack, 64655714Skris const char *file) 64755714Skris { 64855714Skris BIO *in; 64955714Skris X509 *x=NULL; 65055714Skris X509_NAME *xn=NULL; 65155714Skris int ret=1; 65255714Skris int (*oldcmp)(X509_NAME **a, X509_NAME **b); 65355714Skris 65459191Skris oldcmp=sk_X509_NAME_set_cmp_func(stack,xname_cmp); 65555714Skris 65655714Skris in=BIO_new(BIO_s_file_internal()); 65755714Skris 65855714Skris if (in == NULL) 65955714Skris { 66055714Skris SSLerr(SSL_F_SSL_ADD_FILE_CERT_SUBJECTS_TO_STACK,ERR_R_MALLOC_FAILURE); 66155714Skris goto err; 66255714Skris } 66355714Skris 66455714Skris if (!BIO_read_filename(in,file)) 66555714Skris goto err; 66655714Skris 66755714Skris for (;;) 66855714Skris { 66955714Skris if (PEM_read_bio_X509(in,&x,NULL,NULL) == NULL) 67055714Skris break; 67155714Skris if ((xn=X509_get_subject_name(x)) == NULL) goto err; 67255714Skris xn=X509_NAME_dup(xn); 67355714Skris if (xn == NULL) goto err; 67455714Skris if (sk_X509_NAME_find(stack,xn) >= 0) 67555714Skris X509_NAME_free(xn); 67655714Skris else 67755714Skris sk_X509_NAME_push(stack,xn); 67855714Skris } 67955714Skris 68055714Skris if (0) 68155714Skris { 68255714Skriserr: 68355714Skris ret=0; 68455714Skris } 68555714Skris if(in != NULL) 68655714Skris BIO_free(in); 68755714Skris if(x != NULL) 68855714Skris X509_free(x); 68955714Skris 69055714Skris sk_X509_NAME_set_cmp_func(stack,oldcmp); 69155714Skris 69255714Skris return ret; 69355714Skris } 69455714Skris 69555714Skris/*! 69655714Skris * Add a directory of certs to a stack. 69755714Skris * \param stack the stack to append to. 69855714Skris * \param dir the directory to append from. All files in this directory will be 69955714Skris * examined as potential certs. Any that are acceptable to 70055714Skris * SSL_add_dir_cert_subjects_to_stack() that are not already in the stack will be 70155714Skris * included. 70255714Skris * \return 1 for success, 0 for failure. Note that in the case of failure some 70355714Skris * certs may have been added to \c stack. 70455714Skris */ 70555714Skris 70655714Skris#ifndef WIN32 70755714Skris#ifndef VMS /* XXXX This may be fixed in the future */ 70859191Skris#ifndef MAC_OS_pre_X 70955714Skris 71055714Skrisint SSL_add_dir_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack, 71155714Skris const char *dir) 71255714Skris { 71355714Skris DIR *d; 71455714Skris struct dirent *dstruct; 71555714Skris int ret = 0; 71655714Skris 71755714Skris CRYPTO_w_lock(CRYPTO_LOCK_READDIR); 71855714Skris d = opendir(dir); 71955714Skris 72055714Skris /* Note that a side effect is that the CAs will be sorted by name */ 72155714Skris if(!d) 72255714Skris { 72355714Skris SYSerr(SYS_F_OPENDIR, get_last_sys_error()); 72455714Skris ERR_add_error_data(3, "opendir('", dir, "')"); 72555714Skris SSLerr(SSL_F_SSL_ADD_DIR_CERT_SUBJECTS_TO_STACK, ERR_R_SYS_LIB); 72655714Skris goto err; 72755714Skris } 72855714Skris 72955714Skris while((dstruct=readdir(d))) 73055714Skris { 73155714Skris char buf[1024]; 73255714Skris 73355714Skris if(strlen(dir)+strlen(dstruct->d_name)+2 > sizeof buf) 73455714Skris { 73555714Skris SSLerr(SSL_F_SSL_ADD_DIR_CERT_SUBJECTS_TO_STACK,SSL_R_PATH_TOO_LONG); 73655714Skris goto err; 73755714Skris } 73855714Skris 73955714Skris sprintf(buf,"%s/%s",dir,dstruct->d_name); 74055714Skris if(!SSL_add_file_cert_subjects_to_stack(stack,buf)) 74155714Skris goto err; 74255714Skris } 74355714Skris ret = 1; 74455714Skris 74555714Skriserr: 74655714Skris CRYPTO_w_unlock(CRYPTO_LOCK_READDIR); 74755714Skris return ret; 74855714Skris } 74955714Skris 75055714Skris#endif 75155714Skris#endif 75259191Skris#endif 753