10Sstevel@tonic-gate /* ssl/ssl_rsa.c */
20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
30Sstevel@tonic-gate * All rights reserved.
40Sstevel@tonic-gate *
50Sstevel@tonic-gate * This package is an SSL implementation written
60Sstevel@tonic-gate * by Eric Young (eay@cryptsoft.com).
70Sstevel@tonic-gate * The implementation was written so as to conform with Netscapes SSL.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * This library is free for commercial and non-commercial use as long as
100Sstevel@tonic-gate * the following conditions are aheared to. The following conditions
110Sstevel@tonic-gate * apply to all code found in this distribution, be it the RC4, RSA,
120Sstevel@tonic-gate * lhash, DES, etc., code; not just the SSL code. The SSL documentation
130Sstevel@tonic-gate * included with this distribution is covered by the same copyright terms
140Sstevel@tonic-gate * except that the holder is Tim Hudson (tjh@cryptsoft.com).
150Sstevel@tonic-gate *
160Sstevel@tonic-gate * Copyright remains Eric Young's, and as such any Copyright notices in
170Sstevel@tonic-gate * the code are not to be removed.
180Sstevel@tonic-gate * If this package is used in a product, Eric Young should be given attribution
190Sstevel@tonic-gate * as the author of the parts of the library used.
200Sstevel@tonic-gate * This can be in the form of a textual message at program startup or
210Sstevel@tonic-gate * in documentation (online or textual) provided with the package.
220Sstevel@tonic-gate *
230Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
240Sstevel@tonic-gate * modification, are permitted provided that the following conditions
250Sstevel@tonic-gate * are met:
260Sstevel@tonic-gate * 1. Redistributions of source code must retain the copyright
270Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
280Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
290Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
300Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
310Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software
320Sstevel@tonic-gate * must display the following acknowledgement:
330Sstevel@tonic-gate * "This product includes cryptographic software written by
340Sstevel@tonic-gate * Eric Young (eay@cryptsoft.com)"
350Sstevel@tonic-gate * The word 'cryptographic' can be left out if the rouines from the library
360Sstevel@tonic-gate * being used are not cryptographic related :-).
370Sstevel@tonic-gate * 4. If you include any Windows specific code (or a derivative thereof) from
380Sstevel@tonic-gate * the apps directory (application code) you must include an acknowledgement:
390Sstevel@tonic-gate * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
400Sstevel@tonic-gate *
410Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
420Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
440Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
450Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
460Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
470Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
490Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
500Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
510Sstevel@tonic-gate * SUCH DAMAGE.
520Sstevel@tonic-gate *
530Sstevel@tonic-gate * The licence and distribution terms for any publically available version or
540Sstevel@tonic-gate * derivative of this code cannot be changed. i.e. this code cannot simply be
550Sstevel@tonic-gate * copied and put under another distribution licence
560Sstevel@tonic-gate * [including the GNU Public Licence.]
570Sstevel@tonic-gate */
580Sstevel@tonic-gate
590Sstevel@tonic-gate #include <stdio.h>
600Sstevel@tonic-gate #include "ssl_locl.h"
610Sstevel@tonic-gate #include <openssl/bio.h>
620Sstevel@tonic-gate #include <openssl/objects.h>
630Sstevel@tonic-gate #include <openssl/evp.h>
640Sstevel@tonic-gate #include <openssl/x509.h>
650Sstevel@tonic-gate #include <openssl/pem.h>
660Sstevel@tonic-gate
670Sstevel@tonic-gate static int ssl_set_cert(CERT *c, X509 *x509);
680Sstevel@tonic-gate static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey);
SSL_use_certificate(SSL * ssl,X509 * x)690Sstevel@tonic-gate int SSL_use_certificate(SSL *ssl, X509 *x)
700Sstevel@tonic-gate {
710Sstevel@tonic-gate if (x == NULL)
720Sstevel@tonic-gate {
730Sstevel@tonic-gate SSLerr(SSL_F_SSL_USE_CERTIFICATE,ERR_R_PASSED_NULL_PARAMETER);
740Sstevel@tonic-gate return(0);
750Sstevel@tonic-gate }
760Sstevel@tonic-gate if (!ssl_cert_inst(&ssl->cert))
770Sstevel@tonic-gate {
780Sstevel@tonic-gate SSLerr(SSL_F_SSL_USE_CERTIFICATE,ERR_R_MALLOC_FAILURE);
790Sstevel@tonic-gate return(0);
800Sstevel@tonic-gate }
810Sstevel@tonic-gate return(ssl_set_cert(ssl->cert,x));
820Sstevel@tonic-gate }
830Sstevel@tonic-gate
840Sstevel@tonic-gate #ifndef OPENSSL_NO_STDIO
SSL_use_certificate_file(SSL * ssl,const char * file,int type)850Sstevel@tonic-gate int SSL_use_certificate_file(SSL *ssl, const char *file, int type)
860Sstevel@tonic-gate {
870Sstevel@tonic-gate int j;
880Sstevel@tonic-gate BIO *in;
890Sstevel@tonic-gate int ret=0;
900Sstevel@tonic-gate X509 *x=NULL;
910Sstevel@tonic-gate
920Sstevel@tonic-gate in=BIO_new(BIO_s_file_internal());
930Sstevel@tonic-gate if (in == NULL)
940Sstevel@tonic-gate {
950Sstevel@tonic-gate SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE,ERR_R_BUF_LIB);
960Sstevel@tonic-gate goto end;
970Sstevel@tonic-gate }
980Sstevel@tonic-gate
990Sstevel@tonic-gate if (BIO_read_filename(in,file) <= 0)
1000Sstevel@tonic-gate {
1010Sstevel@tonic-gate SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE,ERR_R_SYS_LIB);
1020Sstevel@tonic-gate goto end;
1030Sstevel@tonic-gate }
1040Sstevel@tonic-gate if (type == SSL_FILETYPE_ASN1)
1050Sstevel@tonic-gate {
1060Sstevel@tonic-gate j=ERR_R_ASN1_LIB;
1070Sstevel@tonic-gate x=d2i_X509_bio(in,NULL);
1080Sstevel@tonic-gate }
1090Sstevel@tonic-gate else if (type == SSL_FILETYPE_PEM)
1100Sstevel@tonic-gate {
1110Sstevel@tonic-gate j=ERR_R_PEM_LIB;
1120Sstevel@tonic-gate x=PEM_read_bio_X509(in,NULL,ssl->ctx->default_passwd_callback,ssl->ctx->default_passwd_callback_userdata);
1130Sstevel@tonic-gate }
1140Sstevel@tonic-gate else
1150Sstevel@tonic-gate {
1160Sstevel@tonic-gate SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE,SSL_R_BAD_SSL_FILETYPE);
1170Sstevel@tonic-gate goto end;
1180Sstevel@tonic-gate }
1190Sstevel@tonic-gate
1200Sstevel@tonic-gate if (x == NULL)
1210Sstevel@tonic-gate {
1220Sstevel@tonic-gate SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE,j);
1230Sstevel@tonic-gate goto end;
1240Sstevel@tonic-gate }
1250Sstevel@tonic-gate
1260Sstevel@tonic-gate ret=SSL_use_certificate(ssl,x);
1270Sstevel@tonic-gate end:
1280Sstevel@tonic-gate if (x != NULL) X509_free(x);
1290Sstevel@tonic-gate if (in != NULL) BIO_free(in);
1300Sstevel@tonic-gate return(ret);
1310Sstevel@tonic-gate }
1320Sstevel@tonic-gate #endif
1330Sstevel@tonic-gate
SSL_use_certificate_ASN1(SSL * ssl,const unsigned char * d,int len)134*2139Sjp161948 int SSL_use_certificate_ASN1(SSL *ssl, const unsigned char *d, int len)
1350Sstevel@tonic-gate {
1360Sstevel@tonic-gate X509 *x;
1370Sstevel@tonic-gate int ret;
1380Sstevel@tonic-gate
1390Sstevel@tonic-gate x=d2i_X509(NULL,&d,(long)len);
1400Sstevel@tonic-gate if (x == NULL)
1410Sstevel@tonic-gate {
1420Sstevel@tonic-gate SSLerr(SSL_F_SSL_USE_CERTIFICATE_ASN1,ERR_R_ASN1_LIB);
1430Sstevel@tonic-gate return(0);
1440Sstevel@tonic-gate }
1450Sstevel@tonic-gate
1460Sstevel@tonic-gate ret=SSL_use_certificate(ssl,x);
1470Sstevel@tonic-gate X509_free(x);
1480Sstevel@tonic-gate return(ret);
1490Sstevel@tonic-gate }
1500Sstevel@tonic-gate
1510Sstevel@tonic-gate #ifndef OPENSSL_NO_RSA
SSL_use_RSAPrivateKey(SSL * ssl,RSA * rsa)1520Sstevel@tonic-gate int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa)
1530Sstevel@tonic-gate {
1540Sstevel@tonic-gate EVP_PKEY *pkey;
1550Sstevel@tonic-gate int ret;
1560Sstevel@tonic-gate
1570Sstevel@tonic-gate if (rsa == NULL)
1580Sstevel@tonic-gate {
1590Sstevel@tonic-gate SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
1600Sstevel@tonic-gate return(0);
1610Sstevel@tonic-gate }
1620Sstevel@tonic-gate if (!ssl_cert_inst(&ssl->cert))
1630Sstevel@tonic-gate {
1640Sstevel@tonic-gate SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY,ERR_R_MALLOC_FAILURE);
1650Sstevel@tonic-gate return(0);
1660Sstevel@tonic-gate }
1670Sstevel@tonic-gate if ((pkey=EVP_PKEY_new()) == NULL)
1680Sstevel@tonic-gate {
1690Sstevel@tonic-gate SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY,ERR_R_EVP_LIB);
1700Sstevel@tonic-gate return(0);
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate
1730Sstevel@tonic-gate RSA_up_ref(rsa);
1740Sstevel@tonic-gate EVP_PKEY_assign_RSA(pkey,rsa);
1750Sstevel@tonic-gate
1760Sstevel@tonic-gate ret=ssl_set_pkey(ssl->cert,pkey);
1770Sstevel@tonic-gate EVP_PKEY_free(pkey);
1780Sstevel@tonic-gate return(ret);
1790Sstevel@tonic-gate }
1800Sstevel@tonic-gate #endif
1810Sstevel@tonic-gate
ssl_set_pkey(CERT * c,EVP_PKEY * pkey)1820Sstevel@tonic-gate static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey)
1830Sstevel@tonic-gate {
184*2139Sjp161948 int i;
1850Sstevel@tonic-gate
1860Sstevel@tonic-gate i=ssl_cert_type(NULL,pkey);
1870Sstevel@tonic-gate if (i < 0)
1880Sstevel@tonic-gate {
1890Sstevel@tonic-gate SSLerr(SSL_F_SSL_SET_PKEY,SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1900Sstevel@tonic-gate return(0);
1910Sstevel@tonic-gate }
1920Sstevel@tonic-gate
1930Sstevel@tonic-gate if (c->pkeys[i].x509 != NULL)
1940Sstevel@tonic-gate {
1950Sstevel@tonic-gate EVP_PKEY *pktmp;
1960Sstevel@tonic-gate pktmp = X509_get_pubkey(c->pkeys[i].x509);
1970Sstevel@tonic-gate EVP_PKEY_copy_parameters(pktmp,pkey);
1980Sstevel@tonic-gate EVP_PKEY_free(pktmp);
1990Sstevel@tonic-gate ERR_clear_error();
2000Sstevel@tonic-gate
2010Sstevel@tonic-gate #ifndef OPENSSL_NO_RSA
2020Sstevel@tonic-gate /* Don't check the public/private key, this is mostly
2030Sstevel@tonic-gate * for smart cards. */
2040Sstevel@tonic-gate if ((pkey->type == EVP_PKEY_RSA) &&
205*2139Sjp161948 (RSA_flags(pkey->pkey.rsa) & RSA_METHOD_FLAG_NO_CHECK))
206*2139Sjp161948 ;
2070Sstevel@tonic-gate else
2080Sstevel@tonic-gate #endif
209*2139Sjp161948 if (!X509_check_private_key(c->pkeys[i].x509,pkey))
2100Sstevel@tonic-gate {
211*2139Sjp161948 X509_free(c->pkeys[i].x509);
212*2139Sjp161948 c->pkeys[i].x509 = NULL;
213*2139Sjp161948 return 0;
2140Sstevel@tonic-gate }
2150Sstevel@tonic-gate }
2160Sstevel@tonic-gate
2170Sstevel@tonic-gate if (c->pkeys[i].privatekey != NULL)
2180Sstevel@tonic-gate EVP_PKEY_free(c->pkeys[i].privatekey);
2190Sstevel@tonic-gate CRYPTO_add(&pkey->references,1,CRYPTO_LOCK_EVP_PKEY);
2200Sstevel@tonic-gate c->pkeys[i].privatekey=pkey;
2210Sstevel@tonic-gate c->key= &(c->pkeys[i]);
2220Sstevel@tonic-gate
2230Sstevel@tonic-gate c->valid=0;
2240Sstevel@tonic-gate return(1);
2250Sstevel@tonic-gate }
2260Sstevel@tonic-gate
2270Sstevel@tonic-gate #ifndef OPENSSL_NO_RSA
2280Sstevel@tonic-gate #ifndef OPENSSL_NO_STDIO
SSL_use_RSAPrivateKey_file(SSL * ssl,const char * file,int type)2290Sstevel@tonic-gate int SSL_use_RSAPrivateKey_file(SSL *ssl, const char *file, int type)
2300Sstevel@tonic-gate {
2310Sstevel@tonic-gate int j,ret=0;
2320Sstevel@tonic-gate BIO *in;
2330Sstevel@tonic-gate RSA *rsa=NULL;
2340Sstevel@tonic-gate
2350Sstevel@tonic-gate in=BIO_new(BIO_s_file_internal());
2360Sstevel@tonic-gate if (in == NULL)
2370Sstevel@tonic-gate {
2380Sstevel@tonic-gate SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,ERR_R_BUF_LIB);
2390Sstevel@tonic-gate goto end;
2400Sstevel@tonic-gate }
2410Sstevel@tonic-gate
2420Sstevel@tonic-gate if (BIO_read_filename(in,file) <= 0)
2430Sstevel@tonic-gate {
2440Sstevel@tonic-gate SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,ERR_R_SYS_LIB);
2450Sstevel@tonic-gate goto end;
2460Sstevel@tonic-gate }
2470Sstevel@tonic-gate if (type == SSL_FILETYPE_ASN1)
2480Sstevel@tonic-gate {
2490Sstevel@tonic-gate j=ERR_R_ASN1_LIB;
2500Sstevel@tonic-gate rsa=d2i_RSAPrivateKey_bio(in,NULL);
2510Sstevel@tonic-gate }
2520Sstevel@tonic-gate else if (type == SSL_FILETYPE_PEM)
2530Sstevel@tonic-gate {
2540Sstevel@tonic-gate j=ERR_R_PEM_LIB;
2550Sstevel@tonic-gate rsa=PEM_read_bio_RSAPrivateKey(in,NULL,
2560Sstevel@tonic-gate ssl->ctx->default_passwd_callback,ssl->ctx->default_passwd_callback_userdata);
2570Sstevel@tonic-gate }
2580Sstevel@tonic-gate else
2590Sstevel@tonic-gate {
2600Sstevel@tonic-gate SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
2610Sstevel@tonic-gate goto end;
2620Sstevel@tonic-gate }
2630Sstevel@tonic-gate if (rsa == NULL)
2640Sstevel@tonic-gate {
2650Sstevel@tonic-gate SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,j);
2660Sstevel@tonic-gate goto end;
2670Sstevel@tonic-gate }
2680Sstevel@tonic-gate ret=SSL_use_RSAPrivateKey(ssl,rsa);
2690Sstevel@tonic-gate RSA_free(rsa);
2700Sstevel@tonic-gate end:
2710Sstevel@tonic-gate if (in != NULL) BIO_free(in);
2720Sstevel@tonic-gate return(ret);
2730Sstevel@tonic-gate }
2740Sstevel@tonic-gate #endif
2750Sstevel@tonic-gate
SSL_use_RSAPrivateKey_ASN1(SSL * ssl,unsigned char * d,long len)2760Sstevel@tonic-gate int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, unsigned char *d, long len)
2770Sstevel@tonic-gate {
2780Sstevel@tonic-gate int ret;
2790Sstevel@tonic-gate const unsigned char *p;
2800Sstevel@tonic-gate RSA *rsa;
2810Sstevel@tonic-gate
2820Sstevel@tonic-gate p=d;
2830Sstevel@tonic-gate if ((rsa=d2i_RSAPrivateKey(NULL,&p,(long)len)) == NULL)
2840Sstevel@tonic-gate {
2850Sstevel@tonic-gate SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
2860Sstevel@tonic-gate return(0);
2870Sstevel@tonic-gate }
2880Sstevel@tonic-gate
2890Sstevel@tonic-gate ret=SSL_use_RSAPrivateKey(ssl,rsa);
2900Sstevel@tonic-gate RSA_free(rsa);
2910Sstevel@tonic-gate return(ret);
2920Sstevel@tonic-gate }
2930Sstevel@tonic-gate #endif /* !OPENSSL_NO_RSA */
2940Sstevel@tonic-gate
SSL_use_PrivateKey(SSL * ssl,EVP_PKEY * pkey)2950Sstevel@tonic-gate int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey)
2960Sstevel@tonic-gate {
2970Sstevel@tonic-gate int ret;
2980Sstevel@tonic-gate
2990Sstevel@tonic-gate if (pkey == NULL)
3000Sstevel@tonic-gate {
3010Sstevel@tonic-gate SSLerr(SSL_F_SSL_USE_PRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
3020Sstevel@tonic-gate return(0);
3030Sstevel@tonic-gate }
3040Sstevel@tonic-gate if (!ssl_cert_inst(&ssl->cert))
3050Sstevel@tonic-gate {
3060Sstevel@tonic-gate SSLerr(SSL_F_SSL_USE_PRIVATEKEY,ERR_R_MALLOC_FAILURE);
3070Sstevel@tonic-gate return(0);
3080Sstevel@tonic-gate }
3090Sstevel@tonic-gate ret=ssl_set_pkey(ssl->cert,pkey);
3100Sstevel@tonic-gate return(ret);
3110Sstevel@tonic-gate }
3120Sstevel@tonic-gate
3130Sstevel@tonic-gate #ifndef OPENSSL_NO_STDIO
SSL_use_PrivateKey_file(SSL * ssl,const char * file,int type)3140Sstevel@tonic-gate int SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type)
3150Sstevel@tonic-gate {
3160Sstevel@tonic-gate int j,ret=0;
3170Sstevel@tonic-gate BIO *in;
3180Sstevel@tonic-gate EVP_PKEY *pkey=NULL;
3190Sstevel@tonic-gate
3200Sstevel@tonic-gate in=BIO_new(BIO_s_file_internal());
3210Sstevel@tonic-gate if (in == NULL)
3220Sstevel@tonic-gate {
3230Sstevel@tonic-gate SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,ERR_R_BUF_LIB);
3240Sstevel@tonic-gate goto end;
3250Sstevel@tonic-gate }
3260Sstevel@tonic-gate
3270Sstevel@tonic-gate if (BIO_read_filename(in,file) <= 0)
3280Sstevel@tonic-gate {
3290Sstevel@tonic-gate SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,ERR_R_SYS_LIB);
3300Sstevel@tonic-gate goto end;
3310Sstevel@tonic-gate }
3320Sstevel@tonic-gate if (type == SSL_FILETYPE_PEM)
3330Sstevel@tonic-gate {
3340Sstevel@tonic-gate j=ERR_R_PEM_LIB;
3350Sstevel@tonic-gate pkey=PEM_read_bio_PrivateKey(in,NULL,
3360Sstevel@tonic-gate ssl->ctx->default_passwd_callback,ssl->ctx->default_passwd_callback_userdata);
3370Sstevel@tonic-gate }
338*2139Sjp161948 else if (type == SSL_FILETYPE_ASN1)
339*2139Sjp161948 {
340*2139Sjp161948 j = ERR_R_ASN1_LIB;
341*2139Sjp161948 pkey = d2i_PrivateKey_bio(in,NULL);
342*2139Sjp161948 }
3430Sstevel@tonic-gate else
3440Sstevel@tonic-gate {
3450Sstevel@tonic-gate SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
3460Sstevel@tonic-gate goto end;
3470Sstevel@tonic-gate }
3480Sstevel@tonic-gate if (pkey == NULL)
3490Sstevel@tonic-gate {
3500Sstevel@tonic-gate SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,j);
3510Sstevel@tonic-gate goto end;
3520Sstevel@tonic-gate }
3530Sstevel@tonic-gate ret=SSL_use_PrivateKey(ssl,pkey);
3540Sstevel@tonic-gate EVP_PKEY_free(pkey);
3550Sstevel@tonic-gate end:
3560Sstevel@tonic-gate if (in != NULL) BIO_free(in);
3570Sstevel@tonic-gate return(ret);
3580Sstevel@tonic-gate }
3590Sstevel@tonic-gate #endif
3600Sstevel@tonic-gate
SSL_use_PrivateKey_ASN1(int type,SSL * ssl,const unsigned char * d,long len)361*2139Sjp161948 int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const unsigned char *d, long len)
3620Sstevel@tonic-gate {
3630Sstevel@tonic-gate int ret;
364*2139Sjp161948 const unsigned char *p;
3650Sstevel@tonic-gate EVP_PKEY *pkey;
3660Sstevel@tonic-gate
3670Sstevel@tonic-gate p=d;
3680Sstevel@tonic-gate if ((pkey=d2i_PrivateKey(type,NULL,&p,(long)len)) == NULL)
3690Sstevel@tonic-gate {
3700Sstevel@tonic-gate SSLerr(SSL_F_SSL_USE_PRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
3710Sstevel@tonic-gate return(0);
3720Sstevel@tonic-gate }
3730Sstevel@tonic-gate
3740Sstevel@tonic-gate ret=SSL_use_PrivateKey(ssl,pkey);
3750Sstevel@tonic-gate EVP_PKEY_free(pkey);
3760Sstevel@tonic-gate return(ret);
3770Sstevel@tonic-gate }
3780Sstevel@tonic-gate
SSL_CTX_use_certificate(SSL_CTX * ctx,X509 * x)3790Sstevel@tonic-gate int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x)
3800Sstevel@tonic-gate {
3810Sstevel@tonic-gate if (x == NULL)
3820Sstevel@tonic-gate {
3830Sstevel@tonic-gate SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE,ERR_R_PASSED_NULL_PARAMETER);
3840Sstevel@tonic-gate return(0);
3850Sstevel@tonic-gate }
3860Sstevel@tonic-gate if (!ssl_cert_inst(&ctx->cert))
3870Sstevel@tonic-gate {
3880Sstevel@tonic-gate SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE,ERR_R_MALLOC_FAILURE);
3890Sstevel@tonic-gate return(0);
3900Sstevel@tonic-gate }
3910Sstevel@tonic-gate return(ssl_set_cert(ctx->cert, x));
3920Sstevel@tonic-gate }
3930Sstevel@tonic-gate
ssl_set_cert(CERT * c,X509 * x)3940Sstevel@tonic-gate static int ssl_set_cert(CERT *c, X509 *x)
3950Sstevel@tonic-gate {
3960Sstevel@tonic-gate EVP_PKEY *pkey;
397*2139Sjp161948 int i;
3980Sstevel@tonic-gate
3990Sstevel@tonic-gate pkey=X509_get_pubkey(x);
4000Sstevel@tonic-gate if (pkey == NULL)
4010Sstevel@tonic-gate {
4020Sstevel@tonic-gate SSLerr(SSL_F_SSL_SET_CERT,SSL_R_X509_LIB);
4030Sstevel@tonic-gate return(0);
4040Sstevel@tonic-gate }
4050Sstevel@tonic-gate
4060Sstevel@tonic-gate i=ssl_cert_type(x,pkey);
4070Sstevel@tonic-gate if (i < 0)
4080Sstevel@tonic-gate {
4090Sstevel@tonic-gate SSLerr(SSL_F_SSL_SET_CERT,SSL_R_UNKNOWN_CERTIFICATE_TYPE);
4100Sstevel@tonic-gate EVP_PKEY_free(pkey);
4110Sstevel@tonic-gate return(0);
4120Sstevel@tonic-gate }
4130Sstevel@tonic-gate
4140Sstevel@tonic-gate if (c->pkeys[i].privatekey != NULL)
4150Sstevel@tonic-gate {
4160Sstevel@tonic-gate EVP_PKEY_copy_parameters(pkey,c->pkeys[i].privatekey);
4170Sstevel@tonic-gate ERR_clear_error();
4180Sstevel@tonic-gate
4190Sstevel@tonic-gate #ifndef OPENSSL_NO_RSA
4200Sstevel@tonic-gate /* Don't check the public/private key, this is mostly
4210Sstevel@tonic-gate * for smart cards. */
4220Sstevel@tonic-gate if ((c->pkeys[i].privatekey->type == EVP_PKEY_RSA) &&
4230Sstevel@tonic-gate (RSA_flags(c->pkeys[i].privatekey->pkey.rsa) &
4240Sstevel@tonic-gate RSA_METHOD_FLAG_NO_CHECK))
425*2139Sjp161948 ;
4260Sstevel@tonic-gate else
427*2139Sjp161948 #endif /* OPENSSL_NO_RSA */
4280Sstevel@tonic-gate if (!X509_check_private_key(x,c->pkeys[i].privatekey))
4290Sstevel@tonic-gate {
430*2139Sjp161948 /* don't fail for a cert/key mismatch, just free
431*2139Sjp161948 * current private key (when switching to a different
432*2139Sjp161948 * cert & key, first this function should be used,
433*2139Sjp161948 * then ssl_set_pkey */
434*2139Sjp161948 EVP_PKEY_free(c->pkeys[i].privatekey);
435*2139Sjp161948 c->pkeys[i].privatekey=NULL;
436*2139Sjp161948 /* clear error queue */
437*2139Sjp161948 ERR_clear_error();
4380Sstevel@tonic-gate }
4390Sstevel@tonic-gate }
4400Sstevel@tonic-gate
4410Sstevel@tonic-gate EVP_PKEY_free(pkey);
4420Sstevel@tonic-gate
4430Sstevel@tonic-gate if (c->pkeys[i].x509 != NULL)
4440Sstevel@tonic-gate X509_free(c->pkeys[i].x509);
4450Sstevel@tonic-gate CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
4460Sstevel@tonic-gate c->pkeys[i].x509=x;
4470Sstevel@tonic-gate c->key= &(c->pkeys[i]);
4480Sstevel@tonic-gate
4490Sstevel@tonic-gate c->valid=0;
4500Sstevel@tonic-gate return(1);
4510Sstevel@tonic-gate }
4520Sstevel@tonic-gate
4530Sstevel@tonic-gate #ifndef OPENSSL_NO_STDIO
SSL_CTX_use_certificate_file(SSL_CTX * ctx,const char * file,int type)4540Sstevel@tonic-gate int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type)
4550Sstevel@tonic-gate {
4560Sstevel@tonic-gate int j;
4570Sstevel@tonic-gate BIO *in;
4580Sstevel@tonic-gate int ret=0;
4590Sstevel@tonic-gate X509 *x=NULL;
4600Sstevel@tonic-gate
4610Sstevel@tonic-gate in=BIO_new(BIO_s_file_internal());
4620Sstevel@tonic-gate if (in == NULL)
4630Sstevel@tonic-gate {
4640Sstevel@tonic-gate SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,ERR_R_BUF_LIB);
4650Sstevel@tonic-gate goto end;
4660Sstevel@tonic-gate }
4670Sstevel@tonic-gate
4680Sstevel@tonic-gate if (BIO_read_filename(in,file) <= 0)
4690Sstevel@tonic-gate {
4700Sstevel@tonic-gate SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,ERR_R_SYS_LIB);
4710Sstevel@tonic-gate goto end;
4720Sstevel@tonic-gate }
4730Sstevel@tonic-gate if (type == SSL_FILETYPE_ASN1)
4740Sstevel@tonic-gate {
4750Sstevel@tonic-gate j=ERR_R_ASN1_LIB;
4760Sstevel@tonic-gate x=d2i_X509_bio(in,NULL);
4770Sstevel@tonic-gate }
4780Sstevel@tonic-gate else if (type == SSL_FILETYPE_PEM)
4790Sstevel@tonic-gate {
4800Sstevel@tonic-gate j=ERR_R_PEM_LIB;
4810Sstevel@tonic-gate x=PEM_read_bio_X509(in,NULL,ctx->default_passwd_callback,ctx->default_passwd_callback_userdata);
4820Sstevel@tonic-gate }
4830Sstevel@tonic-gate else
4840Sstevel@tonic-gate {
4850Sstevel@tonic-gate SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,SSL_R_BAD_SSL_FILETYPE);
4860Sstevel@tonic-gate goto end;
4870Sstevel@tonic-gate }
4880Sstevel@tonic-gate
4890Sstevel@tonic-gate if (x == NULL)
4900Sstevel@tonic-gate {
4910Sstevel@tonic-gate SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,j);
4920Sstevel@tonic-gate goto end;
4930Sstevel@tonic-gate }
4940Sstevel@tonic-gate
4950Sstevel@tonic-gate ret=SSL_CTX_use_certificate(ctx,x);
4960Sstevel@tonic-gate end:
4970Sstevel@tonic-gate if (x != NULL) X509_free(x);
4980Sstevel@tonic-gate if (in != NULL) BIO_free(in);
4990Sstevel@tonic-gate return(ret);
5000Sstevel@tonic-gate }
5010Sstevel@tonic-gate #endif
5020Sstevel@tonic-gate
SSL_CTX_use_certificate_ASN1(SSL_CTX * ctx,int len,const unsigned char * d)503*2139Sjp161948 int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len, const unsigned char *d)
5040Sstevel@tonic-gate {
5050Sstevel@tonic-gate X509 *x;
5060Sstevel@tonic-gate int ret;
5070Sstevel@tonic-gate
5080Sstevel@tonic-gate x=d2i_X509(NULL,&d,(long)len);
5090Sstevel@tonic-gate if (x == NULL)
5100Sstevel@tonic-gate {
5110Sstevel@tonic-gate SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1,ERR_R_ASN1_LIB);
5120Sstevel@tonic-gate return(0);
5130Sstevel@tonic-gate }
5140Sstevel@tonic-gate
5150Sstevel@tonic-gate ret=SSL_CTX_use_certificate(ctx,x);
5160Sstevel@tonic-gate X509_free(x);
5170Sstevel@tonic-gate return(ret);
5180Sstevel@tonic-gate }
5190Sstevel@tonic-gate
5200Sstevel@tonic-gate #ifndef OPENSSL_NO_RSA
SSL_CTX_use_RSAPrivateKey(SSL_CTX * ctx,RSA * rsa)5210Sstevel@tonic-gate int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa)
5220Sstevel@tonic-gate {
5230Sstevel@tonic-gate int ret;
5240Sstevel@tonic-gate EVP_PKEY *pkey;
5250Sstevel@tonic-gate
5260Sstevel@tonic-gate if (rsa == NULL)
5270Sstevel@tonic-gate {
5280Sstevel@tonic-gate SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
5290Sstevel@tonic-gate return(0);
5300Sstevel@tonic-gate }
5310Sstevel@tonic-gate if (!ssl_cert_inst(&ctx->cert))
5320Sstevel@tonic-gate {
5330Sstevel@tonic-gate SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY,ERR_R_MALLOC_FAILURE);
5340Sstevel@tonic-gate return(0);
5350Sstevel@tonic-gate }
5360Sstevel@tonic-gate if ((pkey=EVP_PKEY_new()) == NULL)
5370Sstevel@tonic-gate {
5380Sstevel@tonic-gate SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY,ERR_R_EVP_LIB);
5390Sstevel@tonic-gate return(0);
5400Sstevel@tonic-gate }
5410Sstevel@tonic-gate
5420Sstevel@tonic-gate RSA_up_ref(rsa);
5430Sstevel@tonic-gate EVP_PKEY_assign_RSA(pkey,rsa);
5440Sstevel@tonic-gate
5450Sstevel@tonic-gate ret=ssl_set_pkey(ctx->cert, pkey);
5460Sstevel@tonic-gate EVP_PKEY_free(pkey);
5470Sstevel@tonic-gate return(ret);
5480Sstevel@tonic-gate }
5490Sstevel@tonic-gate
5500Sstevel@tonic-gate #ifndef OPENSSL_NO_STDIO
SSL_CTX_use_RSAPrivateKey_file(SSL_CTX * ctx,const char * file,int type)5510Sstevel@tonic-gate int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, int type)
5520Sstevel@tonic-gate {
5530Sstevel@tonic-gate int j,ret=0;
5540Sstevel@tonic-gate BIO *in;
5550Sstevel@tonic-gate RSA *rsa=NULL;
5560Sstevel@tonic-gate
5570Sstevel@tonic-gate in=BIO_new(BIO_s_file_internal());
5580Sstevel@tonic-gate if (in == NULL)
5590Sstevel@tonic-gate {
5600Sstevel@tonic-gate SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,ERR_R_BUF_LIB);
5610Sstevel@tonic-gate goto end;
5620Sstevel@tonic-gate }
5630Sstevel@tonic-gate
5640Sstevel@tonic-gate if (BIO_read_filename(in,file) <= 0)
5650Sstevel@tonic-gate {
5660Sstevel@tonic-gate SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,ERR_R_SYS_LIB);
5670Sstevel@tonic-gate goto end;
5680Sstevel@tonic-gate }
5690Sstevel@tonic-gate if (type == SSL_FILETYPE_ASN1)
5700Sstevel@tonic-gate {
5710Sstevel@tonic-gate j=ERR_R_ASN1_LIB;
5720Sstevel@tonic-gate rsa=d2i_RSAPrivateKey_bio(in,NULL);
5730Sstevel@tonic-gate }
5740Sstevel@tonic-gate else if (type == SSL_FILETYPE_PEM)
5750Sstevel@tonic-gate {
5760Sstevel@tonic-gate j=ERR_R_PEM_LIB;
5770Sstevel@tonic-gate rsa=PEM_read_bio_RSAPrivateKey(in,NULL,
5780Sstevel@tonic-gate ctx->default_passwd_callback,ctx->default_passwd_callback_userdata);
5790Sstevel@tonic-gate }
5800Sstevel@tonic-gate else
5810Sstevel@tonic-gate {
5820Sstevel@tonic-gate SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
5830Sstevel@tonic-gate goto end;
5840Sstevel@tonic-gate }
5850Sstevel@tonic-gate if (rsa == NULL)
5860Sstevel@tonic-gate {
5870Sstevel@tonic-gate SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,j);
5880Sstevel@tonic-gate goto end;
5890Sstevel@tonic-gate }
5900Sstevel@tonic-gate ret=SSL_CTX_use_RSAPrivateKey(ctx,rsa);
5910Sstevel@tonic-gate RSA_free(rsa);
5920Sstevel@tonic-gate end:
5930Sstevel@tonic-gate if (in != NULL) BIO_free(in);
5940Sstevel@tonic-gate return(ret);
5950Sstevel@tonic-gate }
5960Sstevel@tonic-gate #endif
5970Sstevel@tonic-gate
SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX * ctx,const unsigned char * d,long len)598*2139Sjp161948 int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const unsigned char *d, long len)
5990Sstevel@tonic-gate {
6000Sstevel@tonic-gate int ret;
6010Sstevel@tonic-gate const unsigned char *p;
6020Sstevel@tonic-gate RSA *rsa;
6030Sstevel@tonic-gate
6040Sstevel@tonic-gate p=d;
6050Sstevel@tonic-gate if ((rsa=d2i_RSAPrivateKey(NULL,&p,(long)len)) == NULL)
6060Sstevel@tonic-gate {
6070Sstevel@tonic-gate SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
6080Sstevel@tonic-gate return(0);
6090Sstevel@tonic-gate }
6100Sstevel@tonic-gate
6110Sstevel@tonic-gate ret=SSL_CTX_use_RSAPrivateKey(ctx,rsa);
6120Sstevel@tonic-gate RSA_free(rsa);
6130Sstevel@tonic-gate return(ret);
6140Sstevel@tonic-gate }
6150Sstevel@tonic-gate #endif /* !OPENSSL_NO_RSA */
6160Sstevel@tonic-gate
SSL_CTX_use_PrivateKey(SSL_CTX * ctx,EVP_PKEY * pkey)6170Sstevel@tonic-gate int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey)
6180Sstevel@tonic-gate {
6190Sstevel@tonic-gate if (pkey == NULL)
6200Sstevel@tonic-gate {
6210Sstevel@tonic-gate SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
6220Sstevel@tonic-gate return(0);
6230Sstevel@tonic-gate }
6240Sstevel@tonic-gate if (!ssl_cert_inst(&ctx->cert))
6250Sstevel@tonic-gate {
6260Sstevel@tonic-gate SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY,ERR_R_MALLOC_FAILURE);
6270Sstevel@tonic-gate return(0);
6280Sstevel@tonic-gate }
6290Sstevel@tonic-gate return(ssl_set_pkey(ctx->cert,pkey));
6300Sstevel@tonic-gate }
6310Sstevel@tonic-gate
6320Sstevel@tonic-gate #ifndef OPENSSL_NO_STDIO
SSL_CTX_use_PrivateKey_file(SSL_CTX * ctx,const char * file,int type)6330Sstevel@tonic-gate int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
6340Sstevel@tonic-gate {
6350Sstevel@tonic-gate int j,ret=0;
6360Sstevel@tonic-gate BIO *in;
6370Sstevel@tonic-gate EVP_PKEY *pkey=NULL;
6380Sstevel@tonic-gate
6390Sstevel@tonic-gate in=BIO_new(BIO_s_file_internal());
6400Sstevel@tonic-gate if (in == NULL)
6410Sstevel@tonic-gate {
6420Sstevel@tonic-gate SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,ERR_R_BUF_LIB);
6430Sstevel@tonic-gate goto end;
6440Sstevel@tonic-gate }
6450Sstevel@tonic-gate
6460Sstevel@tonic-gate if (BIO_read_filename(in,file) <= 0)
6470Sstevel@tonic-gate {
6480Sstevel@tonic-gate SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,ERR_R_SYS_LIB);
6490Sstevel@tonic-gate goto end;
6500Sstevel@tonic-gate }
6510Sstevel@tonic-gate if (type == SSL_FILETYPE_PEM)
6520Sstevel@tonic-gate {
6530Sstevel@tonic-gate j=ERR_R_PEM_LIB;
6540Sstevel@tonic-gate pkey=PEM_read_bio_PrivateKey(in,NULL,
6550Sstevel@tonic-gate ctx->default_passwd_callback,ctx->default_passwd_callback_userdata);
6560Sstevel@tonic-gate }
657*2139Sjp161948 else if (type == SSL_FILETYPE_ASN1)
658*2139Sjp161948 {
659*2139Sjp161948 j = ERR_R_ASN1_LIB;
660*2139Sjp161948 pkey = d2i_PrivateKey_bio(in,NULL);
661*2139Sjp161948 }
6620Sstevel@tonic-gate else
6630Sstevel@tonic-gate {
6640Sstevel@tonic-gate SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
6650Sstevel@tonic-gate goto end;
6660Sstevel@tonic-gate }
6670Sstevel@tonic-gate if (pkey == NULL)
6680Sstevel@tonic-gate {
6690Sstevel@tonic-gate SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,j);
6700Sstevel@tonic-gate goto end;
6710Sstevel@tonic-gate }
6720Sstevel@tonic-gate ret=SSL_CTX_use_PrivateKey(ctx,pkey);
6730Sstevel@tonic-gate EVP_PKEY_free(pkey);
6740Sstevel@tonic-gate end:
6750Sstevel@tonic-gate if (in != NULL) BIO_free(in);
6760Sstevel@tonic-gate return(ret);
6770Sstevel@tonic-gate }
6780Sstevel@tonic-gate #endif
6790Sstevel@tonic-gate
SSL_CTX_use_PrivateKey_ASN1(int type,SSL_CTX * ctx,const unsigned char * d,long len)680*2139Sjp161948 int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx, const unsigned char *d,
6810Sstevel@tonic-gate long len)
6820Sstevel@tonic-gate {
6830Sstevel@tonic-gate int ret;
684*2139Sjp161948 const unsigned char *p;
6850Sstevel@tonic-gate EVP_PKEY *pkey;
6860Sstevel@tonic-gate
6870Sstevel@tonic-gate p=d;
6880Sstevel@tonic-gate if ((pkey=d2i_PrivateKey(type,NULL,&p,(long)len)) == NULL)
6890Sstevel@tonic-gate {
6900Sstevel@tonic-gate SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
6910Sstevel@tonic-gate return(0);
6920Sstevel@tonic-gate }
6930Sstevel@tonic-gate
6940Sstevel@tonic-gate ret=SSL_CTX_use_PrivateKey(ctx,pkey);
6950Sstevel@tonic-gate EVP_PKEY_free(pkey);
6960Sstevel@tonic-gate return(ret);
6970Sstevel@tonic-gate }
6980Sstevel@tonic-gate
6990Sstevel@tonic-gate
7000Sstevel@tonic-gate #ifndef OPENSSL_NO_STDIO
7010Sstevel@tonic-gate /* Read a file that contains our certificate in "PEM" format,
7020Sstevel@tonic-gate * possibly followed by a sequence of CA certificates that should be
7030Sstevel@tonic-gate * sent to the peer in the Certificate message.
7040Sstevel@tonic-gate */
SSL_CTX_use_certificate_chain_file(SSL_CTX * ctx,const char * file)7050Sstevel@tonic-gate int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file)
7060Sstevel@tonic-gate {
7070Sstevel@tonic-gate BIO *in;
7080Sstevel@tonic-gate int ret=0;
7090Sstevel@tonic-gate X509 *x=NULL;
7100Sstevel@tonic-gate
7110Sstevel@tonic-gate in=BIO_new(BIO_s_file_internal());
7120Sstevel@tonic-gate if (in == NULL)
7130Sstevel@tonic-gate {
7140Sstevel@tonic-gate SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE,ERR_R_BUF_LIB);
7150Sstevel@tonic-gate goto end;
7160Sstevel@tonic-gate }
7170Sstevel@tonic-gate
7180Sstevel@tonic-gate if (BIO_read_filename(in,file) <= 0)
7190Sstevel@tonic-gate {
7200Sstevel@tonic-gate SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE,ERR_R_SYS_LIB);
7210Sstevel@tonic-gate goto end;
7220Sstevel@tonic-gate }
7230Sstevel@tonic-gate
7240Sstevel@tonic-gate x=PEM_read_bio_X509(in,NULL,ctx->default_passwd_callback,ctx->default_passwd_callback_userdata);
7250Sstevel@tonic-gate if (x == NULL)
7260Sstevel@tonic-gate {
7270Sstevel@tonic-gate SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE,ERR_R_PEM_LIB);
7280Sstevel@tonic-gate goto end;
7290Sstevel@tonic-gate }
7300Sstevel@tonic-gate
7310Sstevel@tonic-gate ret=SSL_CTX_use_certificate(ctx,x);
7320Sstevel@tonic-gate if (ERR_peek_error() != 0)
7330Sstevel@tonic-gate ret = 0; /* Key/certificate mismatch doesn't imply ret==0 ... */
7340Sstevel@tonic-gate if (ret)
7350Sstevel@tonic-gate {
7360Sstevel@tonic-gate /* If we could set up our certificate, now proceed to
7370Sstevel@tonic-gate * the CA certificates.
7380Sstevel@tonic-gate */
7390Sstevel@tonic-gate X509 *ca;
7400Sstevel@tonic-gate int r;
7410Sstevel@tonic-gate unsigned long err;
7420Sstevel@tonic-gate
7430Sstevel@tonic-gate if (ctx->extra_certs != NULL)
7440Sstevel@tonic-gate {
7450Sstevel@tonic-gate sk_X509_pop_free(ctx->extra_certs, X509_free);
7460Sstevel@tonic-gate ctx->extra_certs = NULL;
7470Sstevel@tonic-gate }
7480Sstevel@tonic-gate
7490Sstevel@tonic-gate while ((ca = PEM_read_bio_X509(in,NULL,ctx->default_passwd_callback,ctx->default_passwd_callback_userdata))
7500Sstevel@tonic-gate != NULL)
7510Sstevel@tonic-gate {
7520Sstevel@tonic-gate r = SSL_CTX_add_extra_chain_cert(ctx, ca);
7530Sstevel@tonic-gate if (!r)
7540Sstevel@tonic-gate {
7550Sstevel@tonic-gate X509_free(ca);
7560Sstevel@tonic-gate ret = 0;
7570Sstevel@tonic-gate goto end;
7580Sstevel@tonic-gate }
7590Sstevel@tonic-gate /* Note that we must not free r if it was successfully
7600Sstevel@tonic-gate * added to the chain (while we must free the main
7610Sstevel@tonic-gate * certificate, since its reference count is increased
7620Sstevel@tonic-gate * by SSL_CTX_use_certificate). */
7630Sstevel@tonic-gate }
7640Sstevel@tonic-gate /* When the while loop ends, it's usually just EOF. */
7650Sstevel@tonic-gate err = ERR_peek_last_error();
7660Sstevel@tonic-gate if (ERR_GET_LIB(err) == ERR_LIB_PEM && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)
767*2139Sjp161948 ERR_clear_error();
7680Sstevel@tonic-gate else
7690Sstevel@tonic-gate ret = 0; /* some real error */
7700Sstevel@tonic-gate }
7710Sstevel@tonic-gate
7720Sstevel@tonic-gate end:
7730Sstevel@tonic-gate if (x != NULL) X509_free(x);
7740Sstevel@tonic-gate if (in != NULL) BIO_free(in);
7750Sstevel@tonic-gate return(ret);
7760Sstevel@tonic-gate }
7770Sstevel@tonic-gate #endif
778