1*0Sstevel@tonic-gate /*! \file ssl/ssl_cert.c */ 2*0Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3*0Sstevel@tonic-gate * All rights reserved. 4*0Sstevel@tonic-gate * 5*0Sstevel@tonic-gate * This package is an SSL implementation written 6*0Sstevel@tonic-gate * by Eric Young (eay@cryptsoft.com). 7*0Sstevel@tonic-gate * The implementation was written so as to conform with Netscapes SSL. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * This library is free for commercial and non-commercial use as long as 10*0Sstevel@tonic-gate * the following conditions are aheared to. The following conditions 11*0Sstevel@tonic-gate * apply to all code found in this distribution, be it the RC4, RSA, 12*0Sstevel@tonic-gate * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13*0Sstevel@tonic-gate * included with this distribution is covered by the same copyright terms 14*0Sstevel@tonic-gate * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15*0Sstevel@tonic-gate * 16*0Sstevel@tonic-gate * Copyright remains Eric Young's, and as such any Copyright notices in 17*0Sstevel@tonic-gate * the code are not to be removed. 18*0Sstevel@tonic-gate * If this package is used in a product, Eric Young should be given attribution 19*0Sstevel@tonic-gate * as the author of the parts of the library used. 20*0Sstevel@tonic-gate * This can be in the form of a textual message at program startup or 21*0Sstevel@tonic-gate * in documentation (online or textual) provided with the package. 22*0Sstevel@tonic-gate * 23*0Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 24*0Sstevel@tonic-gate * modification, are permitted provided that the following conditions 25*0Sstevel@tonic-gate * are met: 26*0Sstevel@tonic-gate * 1. Redistributions of source code must retain the copyright 27*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 28*0Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 29*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the 30*0Sstevel@tonic-gate * documentation and/or other materials provided with the distribution. 31*0Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software 32*0Sstevel@tonic-gate * must display the following acknowledgement: 33*0Sstevel@tonic-gate * "This product includes cryptographic software written by 34*0Sstevel@tonic-gate * Eric Young (eay@cryptsoft.com)" 35*0Sstevel@tonic-gate * The word 'cryptographic' can be left out if the rouines from the library 36*0Sstevel@tonic-gate * being used are not cryptographic related :-). 37*0Sstevel@tonic-gate * 4. If you include any Windows specific code (or a derivative thereof) from 38*0Sstevel@tonic-gate * the apps directory (application code) you must include an acknowledgement: 39*0Sstevel@tonic-gate * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40*0Sstevel@tonic-gate * 41*0Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42*0Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43*0Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44*0Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45*0Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46*0Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47*0Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48*0Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49*0Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50*0Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51*0Sstevel@tonic-gate * SUCH DAMAGE. 52*0Sstevel@tonic-gate * 53*0Sstevel@tonic-gate * The licence and distribution terms for any publically available version or 54*0Sstevel@tonic-gate * derivative of this code cannot be changed. i.e. this code cannot simply be 55*0Sstevel@tonic-gate * copied and put under another distribution licence 56*0Sstevel@tonic-gate * [including the GNU Public Licence.] 57*0Sstevel@tonic-gate */ 58*0Sstevel@tonic-gate /* ==================================================================== 59*0Sstevel@tonic-gate * Copyright (c) 1999 The OpenSSL Project. All rights reserved. 60*0Sstevel@tonic-gate * 61*0Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 62*0Sstevel@tonic-gate * modification, are permitted provided that the following conditions 63*0Sstevel@tonic-gate * are met: 64*0Sstevel@tonic-gate * 65*0Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright 66*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 67*0Sstevel@tonic-gate * 68*0Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 69*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in 70*0Sstevel@tonic-gate * the documentation and/or other materials provided with the 71*0Sstevel@tonic-gate * distribution. 72*0Sstevel@tonic-gate * 73*0Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this 74*0Sstevel@tonic-gate * software must display the following acknowledgment: 75*0Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project 76*0Sstevel@tonic-gate * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 77*0Sstevel@tonic-gate * 78*0Sstevel@tonic-gate * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 79*0Sstevel@tonic-gate * endorse or promote products derived from this software without 80*0Sstevel@tonic-gate * prior written permission. For written permission, please contact 81*0Sstevel@tonic-gate * openssl-core@OpenSSL.org. 82*0Sstevel@tonic-gate * 83*0Sstevel@tonic-gate * 5. Products derived from this software may not be called "OpenSSL" 84*0Sstevel@tonic-gate * nor may "OpenSSL" appear in their names without prior written 85*0Sstevel@tonic-gate * permission of the OpenSSL Project. 86*0Sstevel@tonic-gate * 87*0Sstevel@tonic-gate * 6. Redistributions of any form whatsoever must retain the following 88*0Sstevel@tonic-gate * acknowledgment: 89*0Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project 90*0Sstevel@tonic-gate * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 91*0Sstevel@tonic-gate * 92*0Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 93*0Sstevel@tonic-gate * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 94*0Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 95*0Sstevel@tonic-gate * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 96*0Sstevel@tonic-gate * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 97*0Sstevel@tonic-gate * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 98*0Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 99*0Sstevel@tonic-gate * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 100*0Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 101*0Sstevel@tonic-gate * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 102*0Sstevel@tonic-gate * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 103*0Sstevel@tonic-gate * OF THE POSSIBILITY OF SUCH DAMAGE. 104*0Sstevel@tonic-gate * ==================================================================== 105*0Sstevel@tonic-gate */ 106*0Sstevel@tonic-gate 107*0Sstevel@tonic-gate #include <stdio.h> 108*0Sstevel@tonic-gate 109*0Sstevel@tonic-gate #include "e_os.h" 110*0Sstevel@tonic-gate #ifndef NO_SYS_TYPES_H 111*0Sstevel@tonic-gate # include <sys/types.h> 112*0Sstevel@tonic-gate #endif 113*0Sstevel@tonic-gate 114*0Sstevel@tonic-gate #if !defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_SYS_VMS) && !defined(NeXT) && !defined(MAC_OS_pre_X) 115*0Sstevel@tonic-gate #include <dirent.h> 116*0Sstevel@tonic-gate #endif 117*0Sstevel@tonic-gate 118*0Sstevel@tonic-gate #if defined(WIN32) 119*0Sstevel@tonic-gate #include <windows.h> 120*0Sstevel@tonic-gate #endif 121*0Sstevel@tonic-gate 122*0Sstevel@tonic-gate #ifdef NeXT 123*0Sstevel@tonic-gate #include <sys/dir.h> 124*0Sstevel@tonic-gate #define dirent direct 125*0Sstevel@tonic-gate #endif 126*0Sstevel@tonic-gate 127*0Sstevel@tonic-gate #include <openssl/objects.h> 128*0Sstevel@tonic-gate #include <openssl/bio.h> 129*0Sstevel@tonic-gate #include <openssl/pem.h> 130*0Sstevel@tonic-gate #include <openssl/x509v3.h> 131*0Sstevel@tonic-gate #include "ssl_locl.h" 132*0Sstevel@tonic-gate 133*0Sstevel@tonic-gate int SSL_get_ex_data_X509_STORE_CTX_idx(void) 134*0Sstevel@tonic-gate { 135*0Sstevel@tonic-gate static volatile int ssl_x509_store_ctx_idx= -1; 136*0Sstevel@tonic-gate 137*0Sstevel@tonic-gate if (ssl_x509_store_ctx_idx < 0) 138*0Sstevel@tonic-gate { 139*0Sstevel@tonic-gate /* any write lock will do; usually this branch 140*0Sstevel@tonic-gate * will only be taken once anyway */ 141*0Sstevel@tonic-gate CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX); 142*0Sstevel@tonic-gate 143*0Sstevel@tonic-gate if (ssl_x509_store_ctx_idx < 0) 144*0Sstevel@tonic-gate { 145*0Sstevel@tonic-gate ssl_x509_store_ctx_idx=X509_STORE_CTX_get_ex_new_index( 146*0Sstevel@tonic-gate 0,"SSL for verify callback",NULL,NULL,NULL); 147*0Sstevel@tonic-gate } 148*0Sstevel@tonic-gate 149*0Sstevel@tonic-gate CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX); 150*0Sstevel@tonic-gate } 151*0Sstevel@tonic-gate return ssl_x509_store_ctx_idx; 152*0Sstevel@tonic-gate } 153*0Sstevel@tonic-gate 154*0Sstevel@tonic-gate CERT *ssl_cert_new(void) 155*0Sstevel@tonic-gate { 156*0Sstevel@tonic-gate CERT *ret; 157*0Sstevel@tonic-gate 158*0Sstevel@tonic-gate ret=(CERT *)OPENSSL_malloc(sizeof(CERT)); 159*0Sstevel@tonic-gate if (ret == NULL) 160*0Sstevel@tonic-gate { 161*0Sstevel@tonic-gate SSLerr(SSL_F_SSL_CERT_NEW,ERR_R_MALLOC_FAILURE); 162*0Sstevel@tonic-gate return(NULL); 163*0Sstevel@tonic-gate } 164*0Sstevel@tonic-gate memset(ret,0,sizeof(CERT)); 165*0Sstevel@tonic-gate 166*0Sstevel@tonic-gate ret->key= &(ret->pkeys[SSL_PKEY_RSA_ENC]); 167*0Sstevel@tonic-gate ret->references=1; 168*0Sstevel@tonic-gate 169*0Sstevel@tonic-gate return(ret); 170*0Sstevel@tonic-gate } 171*0Sstevel@tonic-gate 172*0Sstevel@tonic-gate CERT *ssl_cert_dup(CERT *cert) 173*0Sstevel@tonic-gate { 174*0Sstevel@tonic-gate CERT *ret; 175*0Sstevel@tonic-gate int i; 176*0Sstevel@tonic-gate 177*0Sstevel@tonic-gate ret = (CERT *)OPENSSL_malloc(sizeof(CERT)); 178*0Sstevel@tonic-gate if (ret == NULL) 179*0Sstevel@tonic-gate { 180*0Sstevel@tonic-gate SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_MALLOC_FAILURE); 181*0Sstevel@tonic-gate return(NULL); 182*0Sstevel@tonic-gate } 183*0Sstevel@tonic-gate 184*0Sstevel@tonic-gate memset(ret, 0, sizeof(CERT)); 185*0Sstevel@tonic-gate 186*0Sstevel@tonic-gate ret->key = &ret->pkeys[cert->key - &cert->pkeys[0]]; 187*0Sstevel@tonic-gate /* or ret->key = ret->pkeys + (cert->key - cert->pkeys), 188*0Sstevel@tonic-gate * if you find that more readable */ 189*0Sstevel@tonic-gate 190*0Sstevel@tonic-gate ret->valid = cert->valid; 191*0Sstevel@tonic-gate ret->mask = cert->mask; 192*0Sstevel@tonic-gate ret->export_mask = cert->export_mask; 193*0Sstevel@tonic-gate 194*0Sstevel@tonic-gate #ifndef OPENSSL_NO_RSA 195*0Sstevel@tonic-gate if (cert->rsa_tmp != NULL) 196*0Sstevel@tonic-gate { 197*0Sstevel@tonic-gate RSA_up_ref(cert->rsa_tmp); 198*0Sstevel@tonic-gate ret->rsa_tmp = cert->rsa_tmp; 199*0Sstevel@tonic-gate } 200*0Sstevel@tonic-gate ret->rsa_tmp_cb = cert->rsa_tmp_cb; 201*0Sstevel@tonic-gate #endif 202*0Sstevel@tonic-gate 203*0Sstevel@tonic-gate #ifndef OPENSSL_NO_DH 204*0Sstevel@tonic-gate if (cert->dh_tmp != NULL) 205*0Sstevel@tonic-gate { 206*0Sstevel@tonic-gate /* DH parameters don't have a reference count */ 207*0Sstevel@tonic-gate ret->dh_tmp = DHparams_dup(cert->dh_tmp); 208*0Sstevel@tonic-gate if (ret->dh_tmp == NULL) 209*0Sstevel@tonic-gate { 210*0Sstevel@tonic-gate SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_DH_LIB); 211*0Sstevel@tonic-gate goto err; 212*0Sstevel@tonic-gate } 213*0Sstevel@tonic-gate if (cert->dh_tmp->priv_key) 214*0Sstevel@tonic-gate { 215*0Sstevel@tonic-gate BIGNUM *b = BN_dup(cert->dh_tmp->priv_key); 216*0Sstevel@tonic-gate if (!b) 217*0Sstevel@tonic-gate { 218*0Sstevel@tonic-gate SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_BN_LIB); 219*0Sstevel@tonic-gate goto err; 220*0Sstevel@tonic-gate } 221*0Sstevel@tonic-gate ret->dh_tmp->priv_key = b; 222*0Sstevel@tonic-gate } 223*0Sstevel@tonic-gate if (cert->dh_tmp->pub_key) 224*0Sstevel@tonic-gate { 225*0Sstevel@tonic-gate BIGNUM *b = BN_dup(cert->dh_tmp->pub_key); 226*0Sstevel@tonic-gate if (!b) 227*0Sstevel@tonic-gate { 228*0Sstevel@tonic-gate SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_BN_LIB); 229*0Sstevel@tonic-gate goto err; 230*0Sstevel@tonic-gate } 231*0Sstevel@tonic-gate ret->dh_tmp->pub_key = b; 232*0Sstevel@tonic-gate } 233*0Sstevel@tonic-gate } 234*0Sstevel@tonic-gate ret->dh_tmp_cb = cert->dh_tmp_cb; 235*0Sstevel@tonic-gate #endif 236*0Sstevel@tonic-gate 237*0Sstevel@tonic-gate for (i = 0; i < SSL_PKEY_NUM; i++) 238*0Sstevel@tonic-gate { 239*0Sstevel@tonic-gate if (cert->pkeys[i].x509 != NULL) 240*0Sstevel@tonic-gate { 241*0Sstevel@tonic-gate ret->pkeys[i].x509 = cert->pkeys[i].x509; 242*0Sstevel@tonic-gate CRYPTO_add(&ret->pkeys[i].x509->references, 1, 243*0Sstevel@tonic-gate CRYPTO_LOCK_X509); 244*0Sstevel@tonic-gate } 245*0Sstevel@tonic-gate 246*0Sstevel@tonic-gate if (cert->pkeys[i].privatekey != NULL) 247*0Sstevel@tonic-gate { 248*0Sstevel@tonic-gate ret->pkeys[i].privatekey = cert->pkeys[i].privatekey; 249*0Sstevel@tonic-gate CRYPTO_add(&ret->pkeys[i].privatekey->references, 1, 250*0Sstevel@tonic-gate CRYPTO_LOCK_EVP_PKEY); 251*0Sstevel@tonic-gate 252*0Sstevel@tonic-gate switch(i) 253*0Sstevel@tonic-gate { 254*0Sstevel@tonic-gate /* If there was anything special to do for 255*0Sstevel@tonic-gate * certain types of keys, we'd do it here. 256*0Sstevel@tonic-gate * (Nothing at the moment, I think.) */ 257*0Sstevel@tonic-gate 258*0Sstevel@tonic-gate case SSL_PKEY_RSA_ENC: 259*0Sstevel@tonic-gate case SSL_PKEY_RSA_SIGN: 260*0Sstevel@tonic-gate /* We have an RSA key. */ 261*0Sstevel@tonic-gate break; 262*0Sstevel@tonic-gate 263*0Sstevel@tonic-gate case SSL_PKEY_DSA_SIGN: 264*0Sstevel@tonic-gate /* We have a DSA key. */ 265*0Sstevel@tonic-gate break; 266*0Sstevel@tonic-gate 267*0Sstevel@tonic-gate case SSL_PKEY_DH_RSA: 268*0Sstevel@tonic-gate case SSL_PKEY_DH_DSA: 269*0Sstevel@tonic-gate /* We have a DH key. */ 270*0Sstevel@tonic-gate break; 271*0Sstevel@tonic-gate 272*0Sstevel@tonic-gate default: 273*0Sstevel@tonic-gate /* Can't happen. */ 274*0Sstevel@tonic-gate SSLerr(SSL_F_SSL_CERT_DUP, SSL_R_LIBRARY_BUG); 275*0Sstevel@tonic-gate } 276*0Sstevel@tonic-gate } 277*0Sstevel@tonic-gate } 278*0Sstevel@tonic-gate 279*0Sstevel@tonic-gate /* ret->extra_certs *should* exist, but currently the own certificate 280*0Sstevel@tonic-gate * chain is held inside SSL_CTX */ 281*0Sstevel@tonic-gate 282*0Sstevel@tonic-gate ret->references=1; 283*0Sstevel@tonic-gate 284*0Sstevel@tonic-gate return(ret); 285*0Sstevel@tonic-gate 286*0Sstevel@tonic-gate #ifndef OPENSSL_NO_DH /* avoid 'unreferenced label' warning if OPENSSL_NO_DH is defined */ 287*0Sstevel@tonic-gate err: 288*0Sstevel@tonic-gate #endif 289*0Sstevel@tonic-gate #ifndef OPENSSL_NO_RSA 290*0Sstevel@tonic-gate if (ret->rsa_tmp != NULL) 291*0Sstevel@tonic-gate RSA_free(ret->rsa_tmp); 292*0Sstevel@tonic-gate #endif 293*0Sstevel@tonic-gate #ifndef OPENSSL_NO_DH 294*0Sstevel@tonic-gate if (ret->dh_tmp != NULL) 295*0Sstevel@tonic-gate DH_free(ret->dh_tmp); 296*0Sstevel@tonic-gate #endif 297*0Sstevel@tonic-gate 298*0Sstevel@tonic-gate for (i = 0; i < SSL_PKEY_NUM; i++) 299*0Sstevel@tonic-gate { 300*0Sstevel@tonic-gate if (ret->pkeys[i].x509 != NULL) 301*0Sstevel@tonic-gate X509_free(ret->pkeys[i].x509); 302*0Sstevel@tonic-gate if (ret->pkeys[i].privatekey != NULL) 303*0Sstevel@tonic-gate EVP_PKEY_free(ret->pkeys[i].privatekey); 304*0Sstevel@tonic-gate } 305*0Sstevel@tonic-gate 306*0Sstevel@tonic-gate return NULL; 307*0Sstevel@tonic-gate } 308*0Sstevel@tonic-gate 309*0Sstevel@tonic-gate 310*0Sstevel@tonic-gate void ssl_cert_free(CERT *c) 311*0Sstevel@tonic-gate { 312*0Sstevel@tonic-gate int i; 313*0Sstevel@tonic-gate 314*0Sstevel@tonic-gate if(c == NULL) 315*0Sstevel@tonic-gate return; 316*0Sstevel@tonic-gate 317*0Sstevel@tonic-gate i=CRYPTO_add(&c->references,-1,CRYPTO_LOCK_SSL_CERT); 318*0Sstevel@tonic-gate #ifdef REF_PRINT 319*0Sstevel@tonic-gate REF_PRINT("CERT",c); 320*0Sstevel@tonic-gate #endif 321*0Sstevel@tonic-gate if (i > 0) return; 322*0Sstevel@tonic-gate #ifdef REF_CHECK 323*0Sstevel@tonic-gate if (i < 0) 324*0Sstevel@tonic-gate { 325*0Sstevel@tonic-gate fprintf(stderr,"ssl_cert_free, bad reference count\n"); 326*0Sstevel@tonic-gate abort(); /* ok */ 327*0Sstevel@tonic-gate } 328*0Sstevel@tonic-gate #endif 329*0Sstevel@tonic-gate 330*0Sstevel@tonic-gate #ifndef OPENSSL_NO_RSA 331*0Sstevel@tonic-gate if (c->rsa_tmp) RSA_free(c->rsa_tmp); 332*0Sstevel@tonic-gate #endif 333*0Sstevel@tonic-gate #ifndef OPENSSL_NO_DH 334*0Sstevel@tonic-gate if (c->dh_tmp) DH_free(c->dh_tmp); 335*0Sstevel@tonic-gate #endif 336*0Sstevel@tonic-gate 337*0Sstevel@tonic-gate for (i=0; i<SSL_PKEY_NUM; i++) 338*0Sstevel@tonic-gate { 339*0Sstevel@tonic-gate if (c->pkeys[i].x509 != NULL) 340*0Sstevel@tonic-gate X509_free(c->pkeys[i].x509); 341*0Sstevel@tonic-gate if (c->pkeys[i].privatekey != NULL) 342*0Sstevel@tonic-gate EVP_PKEY_free(c->pkeys[i].privatekey); 343*0Sstevel@tonic-gate #if 0 344*0Sstevel@tonic-gate if (c->pkeys[i].publickey != NULL) 345*0Sstevel@tonic-gate EVP_PKEY_free(c->pkeys[i].publickey); 346*0Sstevel@tonic-gate #endif 347*0Sstevel@tonic-gate } 348*0Sstevel@tonic-gate OPENSSL_free(c); 349*0Sstevel@tonic-gate } 350*0Sstevel@tonic-gate 351*0Sstevel@tonic-gate int ssl_cert_inst(CERT **o) 352*0Sstevel@tonic-gate { 353*0Sstevel@tonic-gate /* Create a CERT if there isn't already one 354*0Sstevel@tonic-gate * (which cannot really happen, as it is initially created in 355*0Sstevel@tonic-gate * SSL_CTX_new; but the earlier code usually allows for that one 356*0Sstevel@tonic-gate * being non-existant, so we follow that behaviour, as it might 357*0Sstevel@tonic-gate * turn out that there actually is a reason for it -- but I'm 358*0Sstevel@tonic-gate * not sure that *all* of the existing code could cope with 359*0Sstevel@tonic-gate * s->cert being NULL, otherwise we could do without the 360*0Sstevel@tonic-gate * initialization in SSL_CTX_new). 361*0Sstevel@tonic-gate */ 362*0Sstevel@tonic-gate 363*0Sstevel@tonic-gate if (o == NULL) 364*0Sstevel@tonic-gate { 365*0Sstevel@tonic-gate SSLerr(SSL_F_SSL_CERT_INST, ERR_R_PASSED_NULL_PARAMETER); 366*0Sstevel@tonic-gate return(0); 367*0Sstevel@tonic-gate } 368*0Sstevel@tonic-gate if (*o == NULL) 369*0Sstevel@tonic-gate { 370*0Sstevel@tonic-gate if ((*o = ssl_cert_new()) == NULL) 371*0Sstevel@tonic-gate { 372*0Sstevel@tonic-gate SSLerr(SSL_F_SSL_CERT_INST, ERR_R_MALLOC_FAILURE); 373*0Sstevel@tonic-gate return(0); 374*0Sstevel@tonic-gate } 375*0Sstevel@tonic-gate } 376*0Sstevel@tonic-gate return(1); 377*0Sstevel@tonic-gate } 378*0Sstevel@tonic-gate 379*0Sstevel@tonic-gate 380*0Sstevel@tonic-gate SESS_CERT *ssl_sess_cert_new(void) 381*0Sstevel@tonic-gate { 382*0Sstevel@tonic-gate SESS_CERT *ret; 383*0Sstevel@tonic-gate 384*0Sstevel@tonic-gate ret = OPENSSL_malloc(sizeof *ret); 385*0Sstevel@tonic-gate if (ret == NULL) 386*0Sstevel@tonic-gate { 387*0Sstevel@tonic-gate SSLerr(SSL_F_SSL_SESS_CERT_NEW, ERR_R_MALLOC_FAILURE); 388*0Sstevel@tonic-gate return NULL; 389*0Sstevel@tonic-gate } 390*0Sstevel@tonic-gate 391*0Sstevel@tonic-gate memset(ret, 0 ,sizeof *ret); 392*0Sstevel@tonic-gate ret->peer_key = &(ret->peer_pkeys[SSL_PKEY_RSA_ENC]); 393*0Sstevel@tonic-gate ret->references = 1; 394*0Sstevel@tonic-gate 395*0Sstevel@tonic-gate return ret; 396*0Sstevel@tonic-gate } 397*0Sstevel@tonic-gate 398*0Sstevel@tonic-gate void ssl_sess_cert_free(SESS_CERT *sc) 399*0Sstevel@tonic-gate { 400*0Sstevel@tonic-gate int i; 401*0Sstevel@tonic-gate 402*0Sstevel@tonic-gate if (sc == NULL) 403*0Sstevel@tonic-gate return; 404*0Sstevel@tonic-gate 405*0Sstevel@tonic-gate i = CRYPTO_add(&sc->references, -1, CRYPTO_LOCK_SSL_SESS_CERT); 406*0Sstevel@tonic-gate #ifdef REF_PRINT 407*0Sstevel@tonic-gate REF_PRINT("SESS_CERT", sc); 408*0Sstevel@tonic-gate #endif 409*0Sstevel@tonic-gate if (i > 0) 410*0Sstevel@tonic-gate return; 411*0Sstevel@tonic-gate #ifdef REF_CHECK 412*0Sstevel@tonic-gate if (i < 0) 413*0Sstevel@tonic-gate { 414*0Sstevel@tonic-gate fprintf(stderr,"ssl_sess_cert_free, bad reference count\n"); 415*0Sstevel@tonic-gate abort(); /* ok */ 416*0Sstevel@tonic-gate } 417*0Sstevel@tonic-gate #endif 418*0Sstevel@tonic-gate 419*0Sstevel@tonic-gate /* i == 0 */ 420*0Sstevel@tonic-gate if (sc->cert_chain != NULL) 421*0Sstevel@tonic-gate sk_X509_pop_free(sc->cert_chain, X509_free); 422*0Sstevel@tonic-gate for (i = 0; i < SSL_PKEY_NUM; i++) 423*0Sstevel@tonic-gate { 424*0Sstevel@tonic-gate if (sc->peer_pkeys[i].x509 != NULL) 425*0Sstevel@tonic-gate X509_free(sc->peer_pkeys[i].x509); 426*0Sstevel@tonic-gate #if 0 /* We don't have the peer's private key. These lines are just 427*0Sstevel@tonic-gate * here as a reminder that we're still using a not-quite-appropriate 428*0Sstevel@tonic-gate * data structure. */ 429*0Sstevel@tonic-gate if (sc->peer_pkeys[i].privatekey != NULL) 430*0Sstevel@tonic-gate EVP_PKEY_free(sc->peer_pkeys[i].privatekey); 431*0Sstevel@tonic-gate #endif 432*0Sstevel@tonic-gate } 433*0Sstevel@tonic-gate 434*0Sstevel@tonic-gate #ifndef OPENSSL_NO_RSA 435*0Sstevel@tonic-gate if (sc->peer_rsa_tmp != NULL) 436*0Sstevel@tonic-gate RSA_free(sc->peer_rsa_tmp); 437*0Sstevel@tonic-gate #endif 438*0Sstevel@tonic-gate #ifndef OPENSSL_NO_DH 439*0Sstevel@tonic-gate if (sc->peer_dh_tmp != NULL) 440*0Sstevel@tonic-gate DH_free(sc->peer_dh_tmp); 441*0Sstevel@tonic-gate #endif 442*0Sstevel@tonic-gate 443*0Sstevel@tonic-gate OPENSSL_free(sc); 444*0Sstevel@tonic-gate } 445*0Sstevel@tonic-gate 446*0Sstevel@tonic-gate int ssl_set_peer_cert_type(SESS_CERT *sc,int type) 447*0Sstevel@tonic-gate { 448*0Sstevel@tonic-gate sc->peer_cert_type = type; 449*0Sstevel@tonic-gate return(1); 450*0Sstevel@tonic-gate } 451*0Sstevel@tonic-gate 452*0Sstevel@tonic-gate int ssl_verify_cert_chain(SSL *s,STACK_OF(X509) *sk) 453*0Sstevel@tonic-gate { 454*0Sstevel@tonic-gate X509 *x; 455*0Sstevel@tonic-gate int i; 456*0Sstevel@tonic-gate X509_STORE_CTX ctx; 457*0Sstevel@tonic-gate 458*0Sstevel@tonic-gate if ((sk == NULL) || (sk_X509_num(sk) == 0)) 459*0Sstevel@tonic-gate return(0); 460*0Sstevel@tonic-gate 461*0Sstevel@tonic-gate x=sk_X509_value(sk,0); 462*0Sstevel@tonic-gate if(!X509_STORE_CTX_init(&ctx,s->ctx->cert_store,x,sk)) 463*0Sstevel@tonic-gate { 464*0Sstevel@tonic-gate SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN,ERR_R_X509_LIB); 465*0Sstevel@tonic-gate return(0); 466*0Sstevel@tonic-gate } 467*0Sstevel@tonic-gate if (SSL_get_verify_depth(s) >= 0) 468*0Sstevel@tonic-gate X509_STORE_CTX_set_depth(&ctx, SSL_get_verify_depth(s)); 469*0Sstevel@tonic-gate X509_STORE_CTX_set_ex_data(&ctx,SSL_get_ex_data_X509_STORE_CTX_idx(),s); 470*0Sstevel@tonic-gate 471*0Sstevel@tonic-gate /* We need to set the verify purpose. The purpose can be determined by 472*0Sstevel@tonic-gate * the context: if its a server it will verify SSL client certificates 473*0Sstevel@tonic-gate * or vice versa. 474*0Sstevel@tonic-gate */ 475*0Sstevel@tonic-gate if (s->server) 476*0Sstevel@tonic-gate i = X509_PURPOSE_SSL_CLIENT; 477*0Sstevel@tonic-gate else 478*0Sstevel@tonic-gate i = X509_PURPOSE_SSL_SERVER; 479*0Sstevel@tonic-gate 480*0Sstevel@tonic-gate X509_STORE_CTX_purpose_inherit(&ctx, i, s->purpose, s->trust); 481*0Sstevel@tonic-gate 482*0Sstevel@tonic-gate if (s->verify_callback) 483*0Sstevel@tonic-gate X509_STORE_CTX_set_verify_cb(&ctx, s->verify_callback); 484*0Sstevel@tonic-gate 485*0Sstevel@tonic-gate if (s->ctx->app_verify_callback != NULL) 486*0Sstevel@tonic-gate #if 1 /* new with OpenSSL 0.9.7 */ 487*0Sstevel@tonic-gate i=s->ctx->app_verify_callback(&ctx, s->ctx->app_verify_arg); 488*0Sstevel@tonic-gate #else 489*0Sstevel@tonic-gate i=s->ctx->app_verify_callback(&ctx); /* should pass app_verify_arg */ 490*0Sstevel@tonic-gate #endif 491*0Sstevel@tonic-gate else 492*0Sstevel@tonic-gate { 493*0Sstevel@tonic-gate #ifndef OPENSSL_NO_X509_VERIFY 494*0Sstevel@tonic-gate i=X509_verify_cert(&ctx); 495*0Sstevel@tonic-gate #else 496*0Sstevel@tonic-gate i=0; 497*0Sstevel@tonic-gate ctx.error=X509_V_ERR_APPLICATION_VERIFICATION; 498*0Sstevel@tonic-gate SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN,SSL_R_NO_VERIFY_CALLBACK); 499*0Sstevel@tonic-gate #endif 500*0Sstevel@tonic-gate } 501*0Sstevel@tonic-gate 502*0Sstevel@tonic-gate s->verify_result=ctx.error; 503*0Sstevel@tonic-gate X509_STORE_CTX_cleanup(&ctx); 504*0Sstevel@tonic-gate 505*0Sstevel@tonic-gate return(i); 506*0Sstevel@tonic-gate } 507*0Sstevel@tonic-gate 508*0Sstevel@tonic-gate static void set_client_CA_list(STACK_OF(X509_NAME) **ca_list,STACK_OF(X509_NAME) *name_list) 509*0Sstevel@tonic-gate { 510*0Sstevel@tonic-gate if (*ca_list != NULL) 511*0Sstevel@tonic-gate sk_X509_NAME_pop_free(*ca_list,X509_NAME_free); 512*0Sstevel@tonic-gate 513*0Sstevel@tonic-gate *ca_list=name_list; 514*0Sstevel@tonic-gate } 515*0Sstevel@tonic-gate 516*0Sstevel@tonic-gate STACK_OF(X509_NAME) *SSL_dup_CA_list(STACK_OF(X509_NAME) *sk) 517*0Sstevel@tonic-gate { 518*0Sstevel@tonic-gate int i; 519*0Sstevel@tonic-gate STACK_OF(X509_NAME) *ret; 520*0Sstevel@tonic-gate X509_NAME *name; 521*0Sstevel@tonic-gate 522*0Sstevel@tonic-gate ret=sk_X509_NAME_new_null(); 523*0Sstevel@tonic-gate for (i=0; i<sk_X509_NAME_num(sk); i++) 524*0Sstevel@tonic-gate { 525*0Sstevel@tonic-gate name=X509_NAME_dup(sk_X509_NAME_value(sk,i)); 526*0Sstevel@tonic-gate if ((name == NULL) || !sk_X509_NAME_push(ret,name)) 527*0Sstevel@tonic-gate { 528*0Sstevel@tonic-gate sk_X509_NAME_pop_free(ret,X509_NAME_free); 529*0Sstevel@tonic-gate return(NULL); 530*0Sstevel@tonic-gate } 531*0Sstevel@tonic-gate } 532*0Sstevel@tonic-gate return(ret); 533*0Sstevel@tonic-gate } 534*0Sstevel@tonic-gate 535*0Sstevel@tonic-gate void SSL_set_client_CA_list(SSL *s,STACK_OF(X509_NAME) *name_list) 536*0Sstevel@tonic-gate { 537*0Sstevel@tonic-gate set_client_CA_list(&(s->client_CA),name_list); 538*0Sstevel@tonic-gate } 539*0Sstevel@tonic-gate 540*0Sstevel@tonic-gate void SSL_CTX_set_client_CA_list(SSL_CTX *ctx,STACK_OF(X509_NAME) *name_list) 541*0Sstevel@tonic-gate { 542*0Sstevel@tonic-gate set_client_CA_list(&(ctx->client_CA),name_list); 543*0Sstevel@tonic-gate } 544*0Sstevel@tonic-gate 545*0Sstevel@tonic-gate STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(SSL_CTX *ctx) 546*0Sstevel@tonic-gate { 547*0Sstevel@tonic-gate return(ctx->client_CA); 548*0Sstevel@tonic-gate } 549*0Sstevel@tonic-gate 550*0Sstevel@tonic-gate STACK_OF(X509_NAME) *SSL_get_client_CA_list(SSL *s) 551*0Sstevel@tonic-gate { 552*0Sstevel@tonic-gate if (s->type == SSL_ST_CONNECT) 553*0Sstevel@tonic-gate { /* we are in the client */ 554*0Sstevel@tonic-gate if (((s->version>>8) == SSL3_VERSION_MAJOR) && 555*0Sstevel@tonic-gate (s->s3 != NULL)) 556*0Sstevel@tonic-gate return(s->s3->tmp.ca_names); 557*0Sstevel@tonic-gate else 558*0Sstevel@tonic-gate return(NULL); 559*0Sstevel@tonic-gate } 560*0Sstevel@tonic-gate else 561*0Sstevel@tonic-gate { 562*0Sstevel@tonic-gate if (s->client_CA != NULL) 563*0Sstevel@tonic-gate return(s->client_CA); 564*0Sstevel@tonic-gate else 565*0Sstevel@tonic-gate return(s->ctx->client_CA); 566*0Sstevel@tonic-gate } 567*0Sstevel@tonic-gate } 568*0Sstevel@tonic-gate 569*0Sstevel@tonic-gate static int add_client_CA(STACK_OF(X509_NAME) **sk,X509 *x) 570*0Sstevel@tonic-gate { 571*0Sstevel@tonic-gate X509_NAME *name; 572*0Sstevel@tonic-gate 573*0Sstevel@tonic-gate if (x == NULL) return(0); 574*0Sstevel@tonic-gate if ((*sk == NULL) && ((*sk=sk_X509_NAME_new_null()) == NULL)) 575*0Sstevel@tonic-gate return(0); 576*0Sstevel@tonic-gate 577*0Sstevel@tonic-gate if ((name=X509_NAME_dup(X509_get_subject_name(x))) == NULL) 578*0Sstevel@tonic-gate return(0); 579*0Sstevel@tonic-gate 580*0Sstevel@tonic-gate if (!sk_X509_NAME_push(*sk,name)) 581*0Sstevel@tonic-gate { 582*0Sstevel@tonic-gate X509_NAME_free(name); 583*0Sstevel@tonic-gate return(0); 584*0Sstevel@tonic-gate } 585*0Sstevel@tonic-gate return(1); 586*0Sstevel@tonic-gate } 587*0Sstevel@tonic-gate 588*0Sstevel@tonic-gate int SSL_add_client_CA(SSL *ssl,X509 *x) 589*0Sstevel@tonic-gate { 590*0Sstevel@tonic-gate return(add_client_CA(&(ssl->client_CA),x)); 591*0Sstevel@tonic-gate } 592*0Sstevel@tonic-gate 593*0Sstevel@tonic-gate int SSL_CTX_add_client_CA(SSL_CTX *ctx,X509 *x) 594*0Sstevel@tonic-gate { 595*0Sstevel@tonic-gate return(add_client_CA(&(ctx->client_CA),x)); 596*0Sstevel@tonic-gate } 597*0Sstevel@tonic-gate 598*0Sstevel@tonic-gate static int xname_cmp(const X509_NAME * const *a, const X509_NAME * const *b) 599*0Sstevel@tonic-gate { 600*0Sstevel@tonic-gate return(X509_NAME_cmp(*a,*b)); 601*0Sstevel@tonic-gate } 602*0Sstevel@tonic-gate 603*0Sstevel@tonic-gate #ifndef OPENSSL_NO_STDIO 604*0Sstevel@tonic-gate /*! 605*0Sstevel@tonic-gate * Load CA certs from a file into a ::STACK. Note that it is somewhat misnamed; 606*0Sstevel@tonic-gate * it doesn't really have anything to do with clients (except that a common use 607*0Sstevel@tonic-gate * for a stack of CAs is to send it to the client). Actually, it doesn't have 608*0Sstevel@tonic-gate * much to do with CAs, either, since it will load any old cert. 609*0Sstevel@tonic-gate * \param file the file containing one or more certs. 610*0Sstevel@tonic-gate * \return a ::STACK containing the certs. 611*0Sstevel@tonic-gate */ 612*0Sstevel@tonic-gate STACK_OF(X509_NAME) *SSL_load_client_CA_file(const char *file) 613*0Sstevel@tonic-gate { 614*0Sstevel@tonic-gate BIO *in; 615*0Sstevel@tonic-gate X509 *x=NULL; 616*0Sstevel@tonic-gate X509_NAME *xn=NULL; 617*0Sstevel@tonic-gate STACK_OF(X509_NAME) *ret,*sk; 618*0Sstevel@tonic-gate 619*0Sstevel@tonic-gate ret=sk_X509_NAME_new_null(); 620*0Sstevel@tonic-gate sk=sk_X509_NAME_new(xname_cmp); 621*0Sstevel@tonic-gate 622*0Sstevel@tonic-gate in=BIO_new(BIO_s_file_internal()); 623*0Sstevel@tonic-gate 624*0Sstevel@tonic-gate if ((ret == NULL) || (sk == NULL) || (in == NULL)) 625*0Sstevel@tonic-gate { 626*0Sstevel@tonic-gate SSLerr(SSL_F_SSL_LOAD_CLIENT_CA_FILE,ERR_R_MALLOC_FAILURE); 627*0Sstevel@tonic-gate goto err; 628*0Sstevel@tonic-gate } 629*0Sstevel@tonic-gate 630*0Sstevel@tonic-gate if (!BIO_read_filename(in,file)) 631*0Sstevel@tonic-gate goto err; 632*0Sstevel@tonic-gate 633*0Sstevel@tonic-gate for (;;) 634*0Sstevel@tonic-gate { 635*0Sstevel@tonic-gate if (PEM_read_bio_X509(in,&x,NULL,NULL) == NULL) 636*0Sstevel@tonic-gate break; 637*0Sstevel@tonic-gate if ((xn=X509_get_subject_name(x)) == NULL) goto err; 638*0Sstevel@tonic-gate /* check for duplicates */ 639*0Sstevel@tonic-gate xn=X509_NAME_dup(xn); 640*0Sstevel@tonic-gate if (xn == NULL) goto err; 641*0Sstevel@tonic-gate if (sk_X509_NAME_find(sk,xn) >= 0) 642*0Sstevel@tonic-gate X509_NAME_free(xn); 643*0Sstevel@tonic-gate else 644*0Sstevel@tonic-gate { 645*0Sstevel@tonic-gate sk_X509_NAME_push(sk,xn); 646*0Sstevel@tonic-gate sk_X509_NAME_push(ret,xn); 647*0Sstevel@tonic-gate } 648*0Sstevel@tonic-gate } 649*0Sstevel@tonic-gate 650*0Sstevel@tonic-gate if (0) 651*0Sstevel@tonic-gate { 652*0Sstevel@tonic-gate err: 653*0Sstevel@tonic-gate if (ret != NULL) sk_X509_NAME_pop_free(ret,X509_NAME_free); 654*0Sstevel@tonic-gate ret=NULL; 655*0Sstevel@tonic-gate } 656*0Sstevel@tonic-gate if (sk != NULL) sk_X509_NAME_free(sk); 657*0Sstevel@tonic-gate if (in != NULL) BIO_free(in); 658*0Sstevel@tonic-gate if (x != NULL) X509_free(x); 659*0Sstevel@tonic-gate return(ret); 660*0Sstevel@tonic-gate } 661*0Sstevel@tonic-gate #endif 662*0Sstevel@tonic-gate 663*0Sstevel@tonic-gate /*! 664*0Sstevel@tonic-gate * Add a file of certs to a stack. 665*0Sstevel@tonic-gate * \param stack the stack to add to. 666*0Sstevel@tonic-gate * \param file the file to add from. All certs in this file that are not 667*0Sstevel@tonic-gate * already in the stack will be added. 668*0Sstevel@tonic-gate * \return 1 for success, 0 for failure. Note that in the case of failure some 669*0Sstevel@tonic-gate * certs may have been added to \c stack. 670*0Sstevel@tonic-gate */ 671*0Sstevel@tonic-gate 672*0Sstevel@tonic-gate int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack, 673*0Sstevel@tonic-gate const char *file) 674*0Sstevel@tonic-gate { 675*0Sstevel@tonic-gate BIO *in; 676*0Sstevel@tonic-gate X509 *x=NULL; 677*0Sstevel@tonic-gate X509_NAME *xn=NULL; 678*0Sstevel@tonic-gate int ret=1; 679*0Sstevel@tonic-gate int (*oldcmp)(const X509_NAME * const *a, const X509_NAME * const *b); 680*0Sstevel@tonic-gate 681*0Sstevel@tonic-gate oldcmp=sk_X509_NAME_set_cmp_func(stack,xname_cmp); 682*0Sstevel@tonic-gate 683*0Sstevel@tonic-gate in=BIO_new(BIO_s_file_internal()); 684*0Sstevel@tonic-gate 685*0Sstevel@tonic-gate if (in == NULL) 686*0Sstevel@tonic-gate { 687*0Sstevel@tonic-gate SSLerr(SSL_F_SSL_ADD_FILE_CERT_SUBJECTS_TO_STACK,ERR_R_MALLOC_FAILURE); 688*0Sstevel@tonic-gate goto err; 689*0Sstevel@tonic-gate } 690*0Sstevel@tonic-gate 691*0Sstevel@tonic-gate if (!BIO_read_filename(in,file)) 692*0Sstevel@tonic-gate goto err; 693*0Sstevel@tonic-gate 694*0Sstevel@tonic-gate for (;;) 695*0Sstevel@tonic-gate { 696*0Sstevel@tonic-gate if (PEM_read_bio_X509(in,&x,NULL,NULL) == NULL) 697*0Sstevel@tonic-gate break; 698*0Sstevel@tonic-gate if ((xn=X509_get_subject_name(x)) == NULL) goto err; 699*0Sstevel@tonic-gate xn=X509_NAME_dup(xn); 700*0Sstevel@tonic-gate if (xn == NULL) goto err; 701*0Sstevel@tonic-gate if (sk_X509_NAME_find(stack,xn) >= 0) 702*0Sstevel@tonic-gate X509_NAME_free(xn); 703*0Sstevel@tonic-gate else 704*0Sstevel@tonic-gate sk_X509_NAME_push(stack,xn); 705*0Sstevel@tonic-gate } 706*0Sstevel@tonic-gate 707*0Sstevel@tonic-gate if (0) 708*0Sstevel@tonic-gate { 709*0Sstevel@tonic-gate err: 710*0Sstevel@tonic-gate ret=0; 711*0Sstevel@tonic-gate } 712*0Sstevel@tonic-gate if(in != NULL) 713*0Sstevel@tonic-gate BIO_free(in); 714*0Sstevel@tonic-gate if(x != NULL) 715*0Sstevel@tonic-gate X509_free(x); 716*0Sstevel@tonic-gate 717*0Sstevel@tonic-gate sk_X509_NAME_set_cmp_func(stack,oldcmp); 718*0Sstevel@tonic-gate 719*0Sstevel@tonic-gate return ret; 720*0Sstevel@tonic-gate } 721*0Sstevel@tonic-gate 722*0Sstevel@tonic-gate /*! 723*0Sstevel@tonic-gate * Add a directory of certs to a stack. 724*0Sstevel@tonic-gate * \param stack the stack to append to. 725*0Sstevel@tonic-gate * \param dir the directory to append from. All files in this directory will be 726*0Sstevel@tonic-gate * examined as potential certs. Any that are acceptable to 727*0Sstevel@tonic-gate * SSL_add_dir_cert_subjects_to_stack() that are not already in the stack will be 728*0Sstevel@tonic-gate * included. 729*0Sstevel@tonic-gate * \return 1 for success, 0 for failure. Note that in the case of failure some 730*0Sstevel@tonic-gate * certs may have been added to \c stack. 731*0Sstevel@tonic-gate */ 732*0Sstevel@tonic-gate 733*0Sstevel@tonic-gate #ifndef OPENSSL_SYS_WIN32 734*0Sstevel@tonic-gate #ifndef OPENSSL_SYS_VMS /* XXXX This may be fixed in the future */ 735*0Sstevel@tonic-gate #ifndef OPENSSL_SYS_MACINTOSH_CLASSIC /* XXXXX: Better scheme needed! */ 736*0Sstevel@tonic-gate 737*0Sstevel@tonic-gate int SSL_add_dir_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack, 738*0Sstevel@tonic-gate const char *dir) 739*0Sstevel@tonic-gate { 740*0Sstevel@tonic-gate DIR *d; 741*0Sstevel@tonic-gate struct dirent *dstruct; 742*0Sstevel@tonic-gate int ret = 0; 743*0Sstevel@tonic-gate 744*0Sstevel@tonic-gate CRYPTO_w_lock(CRYPTO_LOCK_READDIR); 745*0Sstevel@tonic-gate d = opendir(dir); 746*0Sstevel@tonic-gate 747*0Sstevel@tonic-gate /* Note that a side effect is that the CAs will be sorted by name */ 748*0Sstevel@tonic-gate if(!d) 749*0Sstevel@tonic-gate { 750*0Sstevel@tonic-gate SYSerr(SYS_F_OPENDIR, get_last_sys_error()); 751*0Sstevel@tonic-gate ERR_add_error_data(3, "opendir('", dir, "')"); 752*0Sstevel@tonic-gate SSLerr(SSL_F_SSL_ADD_DIR_CERT_SUBJECTS_TO_STACK, ERR_R_SYS_LIB); 753*0Sstevel@tonic-gate goto err; 754*0Sstevel@tonic-gate } 755*0Sstevel@tonic-gate 756*0Sstevel@tonic-gate while((dstruct=readdir(d))) 757*0Sstevel@tonic-gate { 758*0Sstevel@tonic-gate char buf[1024]; 759*0Sstevel@tonic-gate int r; 760*0Sstevel@tonic-gate 761*0Sstevel@tonic-gate if(strlen(dir)+strlen(dstruct->d_name)+2 > sizeof buf) 762*0Sstevel@tonic-gate { 763*0Sstevel@tonic-gate SSLerr(SSL_F_SSL_ADD_DIR_CERT_SUBJECTS_TO_STACK,SSL_R_PATH_TOO_LONG); 764*0Sstevel@tonic-gate goto err; 765*0Sstevel@tonic-gate } 766*0Sstevel@tonic-gate 767*0Sstevel@tonic-gate r = BIO_snprintf(buf,sizeof buf,"%s/%s",dir,dstruct->d_name); 768*0Sstevel@tonic-gate if (r <= 0 || r >= sizeof buf) 769*0Sstevel@tonic-gate goto err; 770*0Sstevel@tonic-gate if(!SSL_add_file_cert_subjects_to_stack(stack,buf)) 771*0Sstevel@tonic-gate goto err; 772*0Sstevel@tonic-gate } 773*0Sstevel@tonic-gate ret = 1; 774*0Sstevel@tonic-gate 775*0Sstevel@tonic-gate err: 776*0Sstevel@tonic-gate if (d) closedir(d); 777*0Sstevel@tonic-gate CRYPTO_w_unlock(CRYPTO_LOCK_READDIR); 778*0Sstevel@tonic-gate return ret; 779*0Sstevel@tonic-gate } 780*0Sstevel@tonic-gate 781*0Sstevel@tonic-gate #endif 782*0Sstevel@tonic-gate #endif 783*0Sstevel@tonic-gate 784*0Sstevel@tonic-gate #else /* OPENSSL_SYS_WIN32 */ 785*0Sstevel@tonic-gate 786*0Sstevel@tonic-gate int SSL_add_dir_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack, 787*0Sstevel@tonic-gate const char *dir) 788*0Sstevel@tonic-gate { 789*0Sstevel@tonic-gate WIN32_FIND_DATA FindFileData; 790*0Sstevel@tonic-gate HANDLE hFind; 791*0Sstevel@tonic-gate int ret = 0; 792*0Sstevel@tonic-gate #ifdef OPENSSL_SYS_WINCE 793*0Sstevel@tonic-gate WCHAR* wdir = NULL; 794*0Sstevel@tonic-gate #endif 795*0Sstevel@tonic-gate 796*0Sstevel@tonic-gate CRYPTO_w_lock(CRYPTO_LOCK_READDIR); 797*0Sstevel@tonic-gate 798*0Sstevel@tonic-gate #ifdef OPENSSL_SYS_WINCE 799*0Sstevel@tonic-gate /* convert strings to UNICODE */ 800*0Sstevel@tonic-gate { 801*0Sstevel@tonic-gate BOOL result = FALSE; 802*0Sstevel@tonic-gate int i; 803*0Sstevel@tonic-gate wdir = malloc((strlen(dir)+1)*2); 804*0Sstevel@tonic-gate if (wdir == NULL) 805*0Sstevel@tonic-gate goto err_noclose; 806*0Sstevel@tonic-gate for (i=0; i<(int)strlen(dir)+1; i++) 807*0Sstevel@tonic-gate wdir[i] = (short)dir[i]; 808*0Sstevel@tonic-gate } 809*0Sstevel@tonic-gate #endif 810*0Sstevel@tonic-gate 811*0Sstevel@tonic-gate #ifdef OPENSSL_SYS_WINCE 812*0Sstevel@tonic-gate hFind = FindFirstFile(wdir, &FindFileData); 813*0Sstevel@tonic-gate #else 814*0Sstevel@tonic-gate hFind = FindFirstFile(dir, &FindFileData); 815*0Sstevel@tonic-gate #endif 816*0Sstevel@tonic-gate /* Note that a side effect is that the CAs will be sorted by name */ 817*0Sstevel@tonic-gate if(hFind == INVALID_HANDLE_VALUE) 818*0Sstevel@tonic-gate { 819*0Sstevel@tonic-gate SYSerr(SYS_F_OPENDIR, get_last_sys_error()); 820*0Sstevel@tonic-gate ERR_add_error_data(3, "opendir('", dir, "')"); 821*0Sstevel@tonic-gate SSLerr(SSL_F_SSL_ADD_DIR_CERT_SUBJECTS_TO_STACK, ERR_R_SYS_LIB); 822*0Sstevel@tonic-gate goto err_noclose; 823*0Sstevel@tonic-gate } 824*0Sstevel@tonic-gate 825*0Sstevel@tonic-gate do 826*0Sstevel@tonic-gate { 827*0Sstevel@tonic-gate char buf[1024]; 828*0Sstevel@tonic-gate int r; 829*0Sstevel@tonic-gate 830*0Sstevel@tonic-gate #ifdef OPENSSL_SYS_WINCE 831*0Sstevel@tonic-gate if(strlen(dir)+_tcslen(FindFileData.cFileName)+2 > sizeof buf) 832*0Sstevel@tonic-gate #else 833*0Sstevel@tonic-gate if(strlen(dir)+strlen(FindFileData.cFileName)+2 > sizeof buf) 834*0Sstevel@tonic-gate #endif 835*0Sstevel@tonic-gate { 836*0Sstevel@tonic-gate SSLerr(SSL_F_SSL_ADD_DIR_CERT_SUBJECTS_TO_STACK,SSL_R_PATH_TOO_LONG); 837*0Sstevel@tonic-gate goto err; 838*0Sstevel@tonic-gate } 839*0Sstevel@tonic-gate 840*0Sstevel@tonic-gate r = BIO_snprintf(buf,sizeof buf,"%s/%s",dir,FindFileData.cFileName); 841*0Sstevel@tonic-gate if (r <= 0 || r >= sizeof buf) 842*0Sstevel@tonic-gate goto err; 843*0Sstevel@tonic-gate if(!SSL_add_file_cert_subjects_to_stack(stack,buf)) 844*0Sstevel@tonic-gate goto err; 845*0Sstevel@tonic-gate } 846*0Sstevel@tonic-gate while (FindNextFile(hFind, &FindFileData) != FALSE); 847*0Sstevel@tonic-gate ret = 1; 848*0Sstevel@tonic-gate 849*0Sstevel@tonic-gate err: 850*0Sstevel@tonic-gate FindClose(hFind); 851*0Sstevel@tonic-gate err_noclose: 852*0Sstevel@tonic-gate #ifdef OPENSSL_SYS_WINCE 853*0Sstevel@tonic-gate if (wdir != NULL) 854*0Sstevel@tonic-gate free(wdir); 855*0Sstevel@tonic-gate #endif 856*0Sstevel@tonic-gate CRYPTO_w_unlock(CRYPTO_LOCK_READDIR); 857*0Sstevel@tonic-gate return ret; 858*0Sstevel@tonic-gate } 859*0Sstevel@tonic-gate 860*0Sstevel@tonic-gate #endif 861