Home | History | Annotate | Line # | Download | only in ssl
ssl_rsa.c revision 1.1
      1  1.1  christos /* ssl/ssl_rsa.c */
      2  1.1  christos /* Copyright (C) 1995-1998 Eric Young (eay (at) cryptsoft.com)
      3  1.1  christos  * All rights reserved.
      4  1.1  christos  *
      5  1.1  christos  * This package is an SSL implementation written
      6  1.1  christos  * by Eric Young (eay (at) cryptsoft.com).
      7  1.1  christos  * The implementation was written so as to conform with Netscapes SSL.
      8  1.1  christos  *
      9  1.1  christos  * This library is free for commercial and non-commercial use as long as
     10  1.1  christos  * the following conditions are aheared to.  The following conditions
     11  1.1  christos  * apply to all code found in this distribution, be it the RC4, RSA,
     12  1.1  christos  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
     13  1.1  christos  * included with this distribution is covered by the same copyright terms
     14  1.1  christos  * except that the holder is Tim Hudson (tjh (at) cryptsoft.com).
     15  1.1  christos  *
     16  1.1  christos  * Copyright remains Eric Young's, and as such any Copyright notices in
     17  1.1  christos  * the code are not to be removed.
     18  1.1  christos  * If this package is used in a product, Eric Young should be given attribution
     19  1.1  christos  * as the author of the parts of the library used.
     20  1.1  christos  * This can be in the form of a textual message at program startup or
     21  1.1  christos  * in documentation (online or textual) provided with the package.
     22  1.1  christos  *
     23  1.1  christos  * Redistribution and use in source and binary forms, with or without
     24  1.1  christos  * modification, are permitted provided that the following conditions
     25  1.1  christos  * are met:
     26  1.1  christos  * 1. Redistributions of source code must retain the copyright
     27  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     28  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     29  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     30  1.1  christos  *    documentation and/or other materials provided with the distribution.
     31  1.1  christos  * 3. All advertising materials mentioning features or use of this software
     32  1.1  christos  *    must display the following acknowledgement:
     33  1.1  christos  *    "This product includes cryptographic software written by
     34  1.1  christos  *     Eric Young (eay (at) cryptsoft.com)"
     35  1.1  christos  *    The word 'cryptographic' can be left out if the rouines from the library
     36  1.1  christos  *    being used are not cryptographic related :-).
     37  1.1  christos  * 4. If you include any Windows specific code (or a derivative thereof) from
     38  1.1  christos  *    the apps directory (application code) you must include an acknowledgement:
     39  1.1  christos  *    "This product includes software written by Tim Hudson (tjh (at) cryptsoft.com)"
     40  1.1  christos  *
     41  1.1  christos  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
     42  1.1  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     43  1.1  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     44  1.1  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     45  1.1  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     46  1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     47  1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     48  1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     49  1.1  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     50  1.1  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     51  1.1  christos  * SUCH DAMAGE.
     52  1.1  christos  *
     53  1.1  christos  * The licence and distribution terms for any publically available version or
     54  1.1  christos  * derivative of this code cannot be changed.  i.e. this code cannot simply be
     55  1.1  christos  * copied and put under another distribution licence
     56  1.1  christos  * [including the GNU Public Licence.]
     57  1.1  christos  */
     58  1.1  christos 
     59  1.1  christos #include <stdio.h>
     60  1.1  christos #include "ssl_locl.h"
     61  1.1  christos #include <openssl/bio.h>
     62  1.1  christos #include <openssl/objects.h>
     63  1.1  christos #include <openssl/evp.h>
     64  1.1  christos #include <openssl/x509.h>
     65  1.1  christos #include <openssl/pem.h>
     66  1.1  christos 
     67  1.1  christos static int ssl_set_cert(CERT *c, X509 *x509);
     68  1.1  christos static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey);
     69  1.1  christos int SSL_use_certificate(SSL *ssl, X509 *x)
     70  1.1  christos 	{
     71  1.1  christos 	if (x == NULL)
     72  1.1  christos 		{
     73  1.1  christos 		SSLerr(SSL_F_SSL_USE_CERTIFICATE,ERR_R_PASSED_NULL_PARAMETER);
     74  1.1  christos 		return(0);
     75  1.1  christos 		}
     76  1.1  christos 	if (!ssl_cert_inst(&ssl->cert))
     77  1.1  christos 		{
     78  1.1  christos 		SSLerr(SSL_F_SSL_USE_CERTIFICATE,ERR_R_MALLOC_FAILURE);
     79  1.1  christos 		return(0);
     80  1.1  christos 		}
     81  1.1  christos 	return(ssl_set_cert(ssl->cert,x));
     82  1.1  christos 	}
     83  1.1  christos 
     84  1.1  christos #ifndef OPENSSL_NO_STDIO
     85  1.1  christos int SSL_use_certificate_file(SSL *ssl, const char *file, int type)
     86  1.1  christos 	{
     87  1.1  christos 	int j;
     88  1.1  christos 	BIO *in;
     89  1.1  christos 	int ret=0;
     90  1.1  christos 	X509 *x=NULL;
     91  1.1  christos 
     92  1.1  christos 	in=BIO_new(BIO_s_file_internal());
     93  1.1  christos 	if (in == NULL)
     94  1.1  christos 		{
     95  1.1  christos 		SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE,ERR_R_BUF_LIB);
     96  1.1  christos 		goto end;
     97  1.1  christos 		}
     98  1.1  christos 
     99  1.1  christos 	if (BIO_read_filename(in,file) <= 0)
    100  1.1  christos 		{
    101  1.1  christos 		SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE,ERR_R_SYS_LIB);
    102  1.1  christos 		goto end;
    103  1.1  christos 		}
    104  1.1  christos 	if (type == SSL_FILETYPE_ASN1)
    105  1.1  christos 		{
    106  1.1  christos 		j=ERR_R_ASN1_LIB;
    107  1.1  christos 		x=d2i_X509_bio(in,NULL);
    108  1.1  christos 		}
    109  1.1  christos 	else if (type == SSL_FILETYPE_PEM)
    110  1.1  christos 		{
    111  1.1  christos 		j=ERR_R_PEM_LIB;
    112  1.1  christos 		x=PEM_read_bio_X509(in,NULL,ssl->ctx->default_passwd_callback,ssl->ctx->default_passwd_callback_userdata);
    113  1.1  christos 		}
    114  1.1  christos 	else
    115  1.1  christos 		{
    116  1.1  christos 		SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE,SSL_R_BAD_SSL_FILETYPE);
    117  1.1  christos 		goto end;
    118  1.1  christos 		}
    119  1.1  christos 
    120  1.1  christos 	if (x == NULL)
    121  1.1  christos 		{
    122  1.1  christos 		SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE,j);
    123  1.1  christos 		goto end;
    124  1.1  christos 		}
    125  1.1  christos 
    126  1.1  christos 	ret=SSL_use_certificate(ssl,x);
    127  1.1  christos end:
    128  1.1  christos 	if (x != NULL) X509_free(x);
    129  1.1  christos 	if (in != NULL) BIO_free(in);
    130  1.1  christos 	return(ret);
    131  1.1  christos 	}
    132  1.1  christos #endif
    133  1.1  christos 
    134  1.1  christos int SSL_use_certificate_ASN1(SSL *ssl, const unsigned char *d, int len)
    135  1.1  christos 	{
    136  1.1  christos 	X509 *x;
    137  1.1  christos 	int ret;
    138  1.1  christos 
    139  1.1  christos 	x=d2i_X509(NULL,&d,(long)len);
    140  1.1  christos 	if (x == NULL)
    141  1.1  christos 		{
    142  1.1  christos 		SSLerr(SSL_F_SSL_USE_CERTIFICATE_ASN1,ERR_R_ASN1_LIB);
    143  1.1  christos 		return(0);
    144  1.1  christos 		}
    145  1.1  christos 
    146  1.1  christos 	ret=SSL_use_certificate(ssl,x);
    147  1.1  christos 	X509_free(x);
    148  1.1  christos 	return(ret);
    149  1.1  christos 	}
    150  1.1  christos 
    151  1.1  christos #ifndef OPENSSL_NO_RSA
    152  1.1  christos int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa)
    153  1.1  christos 	{
    154  1.1  christos 	EVP_PKEY *pkey;
    155  1.1  christos 	int ret;
    156  1.1  christos 
    157  1.1  christos 	if (rsa == NULL)
    158  1.1  christos 		{
    159  1.1  christos 		SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
    160  1.1  christos 		return(0);
    161  1.1  christos 		}
    162  1.1  christos 	if (!ssl_cert_inst(&ssl->cert))
    163  1.1  christos 		{
    164  1.1  christos 		SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY,ERR_R_MALLOC_FAILURE);
    165  1.1  christos 		return(0);
    166  1.1  christos 		}
    167  1.1  christos 	if ((pkey=EVP_PKEY_new()) == NULL)
    168  1.1  christos 		{
    169  1.1  christos 		SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY,ERR_R_EVP_LIB);
    170  1.1  christos 		return(0);
    171  1.1  christos 		}
    172  1.1  christos 
    173  1.1  christos 	RSA_up_ref(rsa);
    174  1.1  christos 	EVP_PKEY_assign_RSA(pkey,rsa);
    175  1.1  christos 
    176  1.1  christos 	ret=ssl_set_pkey(ssl->cert,pkey);
    177  1.1  christos 	EVP_PKEY_free(pkey);
    178  1.1  christos 	return(ret);
    179  1.1  christos 	}
    180  1.1  christos #endif
    181  1.1  christos 
    182  1.1  christos static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey)
    183  1.1  christos 	{
    184  1.1  christos 	int i;
    185  1.1  christos 
    186  1.1  christos 	i=ssl_cert_type(NULL,pkey);
    187  1.1  christos 	if (i < 0)
    188  1.1  christos 		{
    189  1.1  christos 		SSLerr(SSL_F_SSL_SET_PKEY,SSL_R_UNKNOWN_CERTIFICATE_TYPE);
    190  1.1  christos 		return(0);
    191  1.1  christos 		}
    192  1.1  christos 
    193  1.1  christos 	if (c->pkeys[i].x509 != NULL)
    194  1.1  christos 		{
    195  1.1  christos 		EVP_PKEY *pktmp;
    196  1.1  christos 		pktmp =	X509_get_pubkey(c->pkeys[i].x509);
    197  1.1  christos 		EVP_PKEY_copy_parameters(pktmp,pkey);
    198  1.1  christos 		EVP_PKEY_free(pktmp);
    199  1.1  christos 		ERR_clear_error();
    200  1.1  christos 
    201  1.1  christos #ifndef OPENSSL_NO_RSA
    202  1.1  christos 		/* Don't check the public/private key, this is mostly
    203  1.1  christos 		 * for smart cards. */
    204  1.1  christos 		if ((pkey->type == EVP_PKEY_RSA) &&
    205  1.1  christos 			(RSA_flags(pkey->pkey.rsa) & RSA_METHOD_FLAG_NO_CHECK))
    206  1.1  christos 			;
    207  1.1  christos 		else
    208  1.1  christos #endif
    209  1.1  christos 		if (!X509_check_private_key(c->pkeys[i].x509,pkey))
    210  1.1  christos 			{
    211  1.1  christos 			X509_free(c->pkeys[i].x509);
    212  1.1  christos 			c->pkeys[i].x509 = NULL;
    213  1.1  christos 			return 0;
    214  1.1  christos 			}
    215  1.1  christos 		}
    216  1.1  christos 
    217  1.1  christos 	if (c->pkeys[i].privatekey != NULL)
    218  1.1  christos 		EVP_PKEY_free(c->pkeys[i].privatekey);
    219  1.1  christos 	CRYPTO_add(&pkey->references,1,CRYPTO_LOCK_EVP_PKEY);
    220  1.1  christos 	c->pkeys[i].privatekey=pkey;
    221  1.1  christos 	c->key= &(c->pkeys[i]);
    222  1.1  christos 
    223  1.1  christos 	c->valid=0;
    224  1.1  christos 	return(1);
    225  1.1  christos 	}
    226  1.1  christos 
    227  1.1  christos #ifndef OPENSSL_NO_RSA
    228  1.1  christos #ifndef OPENSSL_NO_STDIO
    229  1.1  christos int SSL_use_RSAPrivateKey_file(SSL *ssl, const char *file, int type)
    230  1.1  christos 	{
    231  1.1  christos 	int j,ret=0;
    232  1.1  christos 	BIO *in;
    233  1.1  christos 	RSA *rsa=NULL;
    234  1.1  christos 
    235  1.1  christos 	in=BIO_new(BIO_s_file_internal());
    236  1.1  christos 	if (in == NULL)
    237  1.1  christos 		{
    238  1.1  christos 		SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,ERR_R_BUF_LIB);
    239  1.1  christos 		goto end;
    240  1.1  christos 		}
    241  1.1  christos 
    242  1.1  christos 	if (BIO_read_filename(in,file) <= 0)
    243  1.1  christos 		{
    244  1.1  christos 		SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,ERR_R_SYS_LIB);
    245  1.1  christos 		goto end;
    246  1.1  christos 		}
    247  1.1  christos 	if	(type == SSL_FILETYPE_ASN1)
    248  1.1  christos 		{
    249  1.1  christos 		j=ERR_R_ASN1_LIB;
    250  1.1  christos 		rsa=d2i_RSAPrivateKey_bio(in,NULL);
    251  1.1  christos 		}
    252  1.1  christos 	else if (type == SSL_FILETYPE_PEM)
    253  1.1  christos 		{
    254  1.1  christos 		j=ERR_R_PEM_LIB;
    255  1.1  christos 		rsa=PEM_read_bio_RSAPrivateKey(in,NULL,
    256  1.1  christos 			ssl->ctx->default_passwd_callback,ssl->ctx->default_passwd_callback_userdata);
    257  1.1  christos 		}
    258  1.1  christos 	else
    259  1.1  christos 		{
    260  1.1  christos 		SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
    261  1.1  christos 		goto end;
    262  1.1  christos 		}
    263  1.1  christos 	if (rsa == NULL)
    264  1.1  christos 		{
    265  1.1  christos 		SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,j);
    266  1.1  christos 		goto end;
    267  1.1  christos 		}
    268  1.1  christos 	ret=SSL_use_RSAPrivateKey(ssl,rsa);
    269  1.1  christos 	RSA_free(rsa);
    270  1.1  christos end:
    271  1.1  christos 	if (in != NULL) BIO_free(in);
    272  1.1  christos 	return(ret);
    273  1.1  christos 	}
    274  1.1  christos #endif
    275  1.1  christos 
    276  1.1  christos int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, unsigned char *d, long len)
    277  1.1  christos 	{
    278  1.1  christos 	int ret;
    279  1.1  christos 	const unsigned char *p;
    280  1.1  christos 	RSA *rsa;
    281  1.1  christos 
    282  1.1  christos 	p=d;
    283  1.1  christos 	if ((rsa=d2i_RSAPrivateKey(NULL,&p,(long)len)) == NULL)
    284  1.1  christos 		{
    285  1.1  christos 		SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
    286  1.1  christos 		return(0);
    287  1.1  christos 		}
    288  1.1  christos 
    289  1.1  christos 	ret=SSL_use_RSAPrivateKey(ssl,rsa);
    290  1.1  christos 	RSA_free(rsa);
    291  1.1  christos 	return(ret);
    292  1.1  christos 	}
    293  1.1  christos #endif /* !OPENSSL_NO_RSA */
    294  1.1  christos 
    295  1.1  christos int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey)
    296  1.1  christos 	{
    297  1.1  christos 	int ret;
    298  1.1  christos 
    299  1.1  christos 	if (pkey == NULL)
    300  1.1  christos 		{
    301  1.1  christos 		SSLerr(SSL_F_SSL_USE_PRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
    302  1.1  christos 		return(0);
    303  1.1  christos 		}
    304  1.1  christos 	if (!ssl_cert_inst(&ssl->cert))
    305  1.1  christos 		{
    306  1.1  christos 		SSLerr(SSL_F_SSL_USE_PRIVATEKEY,ERR_R_MALLOC_FAILURE);
    307  1.1  christos 		return(0);
    308  1.1  christos 		}
    309  1.1  christos 	ret=ssl_set_pkey(ssl->cert,pkey);
    310  1.1  christos 	return(ret);
    311  1.1  christos 	}
    312  1.1  christos 
    313  1.1  christos #ifndef OPENSSL_NO_STDIO
    314  1.1  christos int SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type)
    315  1.1  christos 	{
    316  1.1  christos 	int j,ret=0;
    317  1.1  christos 	BIO *in;
    318  1.1  christos 	EVP_PKEY *pkey=NULL;
    319  1.1  christos 
    320  1.1  christos 	in=BIO_new(BIO_s_file_internal());
    321  1.1  christos 	if (in == NULL)
    322  1.1  christos 		{
    323  1.1  christos 		SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,ERR_R_BUF_LIB);
    324  1.1  christos 		goto end;
    325  1.1  christos 		}
    326  1.1  christos 
    327  1.1  christos 	if (BIO_read_filename(in,file) <= 0)
    328  1.1  christos 		{
    329  1.1  christos 		SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,ERR_R_SYS_LIB);
    330  1.1  christos 		goto end;
    331  1.1  christos 		}
    332  1.1  christos 	if (type == SSL_FILETYPE_PEM)
    333  1.1  christos 		{
    334  1.1  christos 		j=ERR_R_PEM_LIB;
    335  1.1  christos 		pkey=PEM_read_bio_PrivateKey(in,NULL,
    336  1.1  christos 			ssl->ctx->default_passwd_callback,ssl->ctx->default_passwd_callback_userdata);
    337  1.1  christos 		}
    338  1.1  christos 	else if (type == SSL_FILETYPE_ASN1)
    339  1.1  christos 		{
    340  1.1  christos 		j = ERR_R_ASN1_LIB;
    341  1.1  christos 		pkey = d2i_PrivateKey_bio(in,NULL);
    342  1.1  christos 		}
    343  1.1  christos 	else
    344  1.1  christos 		{
    345  1.1  christos 		SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
    346  1.1  christos 		goto end;
    347  1.1  christos 		}
    348  1.1  christos 	if (pkey == NULL)
    349  1.1  christos 		{
    350  1.1  christos 		SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,j);
    351  1.1  christos 		goto end;
    352  1.1  christos 		}
    353  1.1  christos 	ret=SSL_use_PrivateKey(ssl,pkey);
    354  1.1  christos 	EVP_PKEY_free(pkey);
    355  1.1  christos end:
    356  1.1  christos 	if (in != NULL) BIO_free(in);
    357  1.1  christos 	return(ret);
    358  1.1  christos 	}
    359  1.1  christos #endif
    360  1.1  christos 
    361  1.1  christos int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const unsigned char *d, long len)
    362  1.1  christos 	{
    363  1.1  christos 	int ret;
    364  1.1  christos 	const unsigned char *p;
    365  1.1  christos 	EVP_PKEY *pkey;
    366  1.1  christos 
    367  1.1  christos 	p=d;
    368  1.1  christos 	if ((pkey=d2i_PrivateKey(type,NULL,&p,(long)len)) == NULL)
    369  1.1  christos 		{
    370  1.1  christos 		SSLerr(SSL_F_SSL_USE_PRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
    371  1.1  christos 		return(0);
    372  1.1  christos 		}
    373  1.1  christos 
    374  1.1  christos 	ret=SSL_use_PrivateKey(ssl,pkey);
    375  1.1  christos 	EVP_PKEY_free(pkey);
    376  1.1  christos 	return(ret);
    377  1.1  christos 	}
    378  1.1  christos 
    379  1.1  christos int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x)
    380  1.1  christos 	{
    381  1.1  christos 	if (x == NULL)
    382  1.1  christos 		{
    383  1.1  christos 		SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE,ERR_R_PASSED_NULL_PARAMETER);
    384  1.1  christos 		return(0);
    385  1.1  christos 		}
    386  1.1  christos 	if (!ssl_cert_inst(&ctx->cert))
    387  1.1  christos 		{
    388  1.1  christos 		SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE,ERR_R_MALLOC_FAILURE);
    389  1.1  christos 		return(0);
    390  1.1  christos 		}
    391  1.1  christos 	return(ssl_set_cert(ctx->cert, x));
    392  1.1  christos 	}
    393  1.1  christos 
    394  1.1  christos static int ssl_set_cert(CERT *c, X509 *x)
    395  1.1  christos 	{
    396  1.1  christos 	EVP_PKEY *pkey;
    397  1.1  christos 	int i;
    398  1.1  christos 
    399  1.1  christos 	pkey=X509_get_pubkey(x);
    400  1.1  christos 	if (pkey == NULL)
    401  1.1  christos 		{
    402  1.1  christos 		SSLerr(SSL_F_SSL_SET_CERT,SSL_R_X509_LIB);
    403  1.1  christos 		return(0);
    404  1.1  christos 		}
    405  1.1  christos 
    406  1.1  christos 	i=ssl_cert_type(x,pkey);
    407  1.1  christos 	if (i < 0)
    408  1.1  christos 		{
    409  1.1  christos 		SSLerr(SSL_F_SSL_SET_CERT,SSL_R_UNKNOWN_CERTIFICATE_TYPE);
    410  1.1  christos 		EVP_PKEY_free(pkey);
    411  1.1  christos 		return(0);
    412  1.1  christos 		}
    413  1.1  christos 
    414  1.1  christos 	if (c->pkeys[i].privatekey != NULL)
    415  1.1  christos 		{
    416  1.1  christos 		EVP_PKEY_copy_parameters(pkey,c->pkeys[i].privatekey);
    417  1.1  christos 		ERR_clear_error();
    418  1.1  christos 
    419  1.1  christos #ifndef OPENSSL_NO_RSA
    420  1.1  christos 		/* Don't check the public/private key, this is mostly
    421  1.1  christos 		 * for smart cards. */
    422  1.1  christos 		if ((c->pkeys[i].privatekey->type == EVP_PKEY_RSA) &&
    423  1.1  christos 			(RSA_flags(c->pkeys[i].privatekey->pkey.rsa) &
    424  1.1  christos 			 RSA_METHOD_FLAG_NO_CHECK))
    425  1.1  christos 			 ;
    426  1.1  christos 		else
    427  1.1  christos #endif /* OPENSSL_NO_RSA */
    428  1.1  christos 		if (!X509_check_private_key(x,c->pkeys[i].privatekey))
    429  1.1  christos 			{
    430  1.1  christos 			/* don't fail for a cert/key mismatch, just free
    431  1.1  christos 			 * current private key (when switching to a different
    432  1.1  christos 			 * cert & key, first this function should be used,
    433  1.1  christos 			 * then ssl_set_pkey */
    434  1.1  christos 			EVP_PKEY_free(c->pkeys[i].privatekey);
    435  1.1  christos 			c->pkeys[i].privatekey=NULL;
    436  1.1  christos 			/* clear error queue */
    437  1.1  christos 			ERR_clear_error();
    438  1.1  christos 			}
    439  1.1  christos 		}
    440  1.1  christos 
    441  1.1  christos 	EVP_PKEY_free(pkey);
    442  1.1  christos 
    443  1.1  christos 	if (c->pkeys[i].x509 != NULL)
    444  1.1  christos 		X509_free(c->pkeys[i].x509);
    445  1.1  christos 	CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
    446  1.1  christos 	c->pkeys[i].x509=x;
    447  1.1  christos 	c->key= &(c->pkeys[i]);
    448  1.1  christos 
    449  1.1  christos 	c->valid=0;
    450  1.1  christos 	return(1);
    451  1.1  christos 	}
    452  1.1  christos 
    453  1.1  christos #ifndef OPENSSL_NO_STDIO
    454  1.1  christos int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type)
    455  1.1  christos 	{
    456  1.1  christos 	int j;
    457  1.1  christos 	BIO *in;
    458  1.1  christos 	int ret=0;
    459  1.1  christos 	X509 *x=NULL;
    460  1.1  christos 
    461  1.1  christos 	in=BIO_new(BIO_s_file_internal());
    462  1.1  christos 	if (in == NULL)
    463  1.1  christos 		{
    464  1.1  christos 		SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,ERR_R_BUF_LIB);
    465  1.1  christos 		goto end;
    466  1.1  christos 		}
    467  1.1  christos 
    468  1.1  christos 	if (BIO_read_filename(in,file) <= 0)
    469  1.1  christos 		{
    470  1.1  christos 		SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,ERR_R_SYS_LIB);
    471  1.1  christos 		goto end;
    472  1.1  christos 		}
    473  1.1  christos 	if (type == SSL_FILETYPE_ASN1)
    474  1.1  christos 		{
    475  1.1  christos 		j=ERR_R_ASN1_LIB;
    476  1.1  christos 		x=d2i_X509_bio(in,NULL);
    477  1.1  christos 		}
    478  1.1  christos 	else if (type == SSL_FILETYPE_PEM)
    479  1.1  christos 		{
    480  1.1  christos 		j=ERR_R_PEM_LIB;
    481  1.1  christos 		x=PEM_read_bio_X509(in,NULL,ctx->default_passwd_callback,ctx->default_passwd_callback_userdata);
    482  1.1  christos 		}
    483  1.1  christos 	else
    484  1.1  christos 		{
    485  1.1  christos 		SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,SSL_R_BAD_SSL_FILETYPE);
    486  1.1  christos 		goto end;
    487  1.1  christos 		}
    488  1.1  christos 
    489  1.1  christos 	if (x == NULL)
    490  1.1  christos 		{
    491  1.1  christos 		SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,j);
    492  1.1  christos 		goto end;
    493  1.1  christos 		}
    494  1.1  christos 
    495  1.1  christos 	ret=SSL_CTX_use_certificate(ctx,x);
    496  1.1  christos end:
    497  1.1  christos 	if (x != NULL) X509_free(x);
    498  1.1  christos 	if (in != NULL) BIO_free(in);
    499  1.1  christos 	return(ret);
    500  1.1  christos 	}
    501  1.1  christos #endif
    502  1.1  christos 
    503  1.1  christos int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len, const unsigned char *d)
    504  1.1  christos 	{
    505  1.1  christos 	X509 *x;
    506  1.1  christos 	int ret;
    507  1.1  christos 
    508  1.1  christos 	x=d2i_X509(NULL,&d,(long)len);
    509  1.1  christos 	if (x == NULL)
    510  1.1  christos 		{
    511  1.1  christos 		SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1,ERR_R_ASN1_LIB);
    512  1.1  christos 		return(0);
    513  1.1  christos 		}
    514  1.1  christos 
    515  1.1  christos 	ret=SSL_CTX_use_certificate(ctx,x);
    516  1.1  christos 	X509_free(x);
    517  1.1  christos 	return(ret);
    518  1.1  christos 	}
    519  1.1  christos 
    520  1.1  christos #ifndef OPENSSL_NO_RSA
    521  1.1  christos int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa)
    522  1.1  christos 	{
    523  1.1  christos 	int ret;
    524  1.1  christos 	EVP_PKEY *pkey;
    525  1.1  christos 
    526  1.1  christos 	if (rsa == NULL)
    527  1.1  christos 		{
    528  1.1  christos 		SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
    529  1.1  christos 		return(0);
    530  1.1  christos 		}
    531  1.1  christos 	if (!ssl_cert_inst(&ctx->cert))
    532  1.1  christos 		{
    533  1.1  christos 		SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY,ERR_R_MALLOC_FAILURE);
    534  1.1  christos 		return(0);
    535  1.1  christos 		}
    536  1.1  christos 	if ((pkey=EVP_PKEY_new()) == NULL)
    537  1.1  christos 		{
    538  1.1  christos 		SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY,ERR_R_EVP_LIB);
    539  1.1  christos 		return(0);
    540  1.1  christos 		}
    541  1.1  christos 
    542  1.1  christos 	RSA_up_ref(rsa);
    543  1.1  christos 	EVP_PKEY_assign_RSA(pkey,rsa);
    544  1.1  christos 
    545  1.1  christos 	ret=ssl_set_pkey(ctx->cert, pkey);
    546  1.1  christos 	EVP_PKEY_free(pkey);
    547  1.1  christos 	return(ret);
    548  1.1  christos 	}
    549  1.1  christos 
    550  1.1  christos #ifndef OPENSSL_NO_STDIO
    551  1.1  christos int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, int type)
    552  1.1  christos 	{
    553  1.1  christos 	int j,ret=0;
    554  1.1  christos 	BIO *in;
    555  1.1  christos 	RSA *rsa=NULL;
    556  1.1  christos 
    557  1.1  christos 	in=BIO_new(BIO_s_file_internal());
    558  1.1  christos 	if (in == NULL)
    559  1.1  christos 		{
    560  1.1  christos 		SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,ERR_R_BUF_LIB);
    561  1.1  christos 		goto end;
    562  1.1  christos 		}
    563  1.1  christos 
    564  1.1  christos 	if (BIO_read_filename(in,file) <= 0)
    565  1.1  christos 		{
    566  1.1  christos 		SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,ERR_R_SYS_LIB);
    567  1.1  christos 		goto end;
    568  1.1  christos 		}
    569  1.1  christos 	if	(type == SSL_FILETYPE_ASN1)
    570  1.1  christos 		{
    571  1.1  christos 		j=ERR_R_ASN1_LIB;
    572  1.1  christos 		rsa=d2i_RSAPrivateKey_bio(in,NULL);
    573  1.1  christos 		}
    574  1.1  christos 	else if (type == SSL_FILETYPE_PEM)
    575  1.1  christos 		{
    576  1.1  christos 		j=ERR_R_PEM_LIB;
    577  1.1  christos 		rsa=PEM_read_bio_RSAPrivateKey(in,NULL,
    578  1.1  christos 			ctx->default_passwd_callback,ctx->default_passwd_callback_userdata);
    579  1.1  christos 		}
    580  1.1  christos 	else
    581  1.1  christos 		{
    582  1.1  christos 		SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
    583  1.1  christos 		goto end;
    584  1.1  christos 		}
    585  1.1  christos 	if (rsa == NULL)
    586  1.1  christos 		{
    587  1.1  christos 		SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,j);
    588  1.1  christos 		goto end;
    589  1.1  christos 		}
    590  1.1  christos 	ret=SSL_CTX_use_RSAPrivateKey(ctx,rsa);
    591  1.1  christos 	RSA_free(rsa);
    592  1.1  christos end:
    593  1.1  christos 	if (in != NULL) BIO_free(in);
    594  1.1  christos 	return(ret);
    595  1.1  christos 	}
    596  1.1  christos #endif
    597  1.1  christos 
    598  1.1  christos int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const unsigned char *d, long len)
    599  1.1  christos 	{
    600  1.1  christos 	int ret;
    601  1.1  christos 	const unsigned char *p;
    602  1.1  christos 	RSA *rsa;
    603  1.1  christos 
    604  1.1  christos 	p=d;
    605  1.1  christos 	if ((rsa=d2i_RSAPrivateKey(NULL,&p,(long)len)) == NULL)
    606  1.1  christos 		{
    607  1.1  christos 		SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
    608  1.1  christos 		return(0);
    609  1.1  christos 		}
    610  1.1  christos 
    611  1.1  christos 	ret=SSL_CTX_use_RSAPrivateKey(ctx,rsa);
    612  1.1  christos 	RSA_free(rsa);
    613  1.1  christos 	return(ret);
    614  1.1  christos 	}
    615  1.1  christos #endif /* !OPENSSL_NO_RSA */
    616  1.1  christos 
    617  1.1  christos int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey)
    618  1.1  christos 	{
    619  1.1  christos 	if (pkey == NULL)
    620  1.1  christos 		{
    621  1.1  christos 		SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
    622  1.1  christos 		return(0);
    623  1.1  christos 		}
    624  1.1  christos 	if (!ssl_cert_inst(&ctx->cert))
    625  1.1  christos 		{
    626  1.1  christos 		SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY,ERR_R_MALLOC_FAILURE);
    627  1.1  christos 		return(0);
    628  1.1  christos 		}
    629  1.1  christos 	return(ssl_set_pkey(ctx->cert,pkey));
    630  1.1  christos 	}
    631  1.1  christos 
    632  1.1  christos #ifndef OPENSSL_NO_STDIO
    633  1.1  christos int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
    634  1.1  christos 	{
    635  1.1  christos 	int j,ret=0;
    636  1.1  christos 	BIO *in;
    637  1.1  christos 	EVP_PKEY *pkey=NULL;
    638  1.1  christos 
    639  1.1  christos 	in=BIO_new(BIO_s_file_internal());
    640  1.1  christos 	if (in == NULL)
    641  1.1  christos 		{
    642  1.1  christos 		SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,ERR_R_BUF_LIB);
    643  1.1  christos 		goto end;
    644  1.1  christos 		}
    645  1.1  christos 
    646  1.1  christos 	if (BIO_read_filename(in,file) <= 0)
    647  1.1  christos 		{
    648  1.1  christos 		SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,ERR_R_SYS_LIB);
    649  1.1  christos 		goto end;
    650  1.1  christos 		}
    651  1.1  christos 	if (type == SSL_FILETYPE_PEM)
    652  1.1  christos 		{
    653  1.1  christos 		j=ERR_R_PEM_LIB;
    654  1.1  christos 		pkey=PEM_read_bio_PrivateKey(in,NULL,
    655  1.1  christos 			ctx->default_passwd_callback,ctx->default_passwd_callback_userdata);
    656  1.1  christos 		}
    657  1.1  christos 	else if (type == SSL_FILETYPE_ASN1)
    658  1.1  christos 		{
    659  1.1  christos 		j = ERR_R_ASN1_LIB;
    660  1.1  christos 		pkey = d2i_PrivateKey_bio(in,NULL);
    661  1.1  christos 		}
    662  1.1  christos 	else
    663  1.1  christos 		{
    664  1.1  christos 		SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
    665  1.1  christos 		goto end;
    666  1.1  christos 		}
    667  1.1  christos 	if (pkey == NULL)
    668  1.1  christos 		{
    669  1.1  christos 		SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,j);
    670  1.1  christos 		goto end;
    671  1.1  christos 		}
    672  1.1  christos 	ret=SSL_CTX_use_PrivateKey(ctx,pkey);
    673  1.1  christos 	EVP_PKEY_free(pkey);
    674  1.1  christos end:
    675  1.1  christos 	if (in != NULL) BIO_free(in);
    676  1.1  christos 	return(ret);
    677  1.1  christos 	}
    678  1.1  christos #endif
    679  1.1  christos 
    680  1.1  christos int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx, const unsigned char *d,
    681  1.1  christos 	     long len)
    682  1.1  christos 	{
    683  1.1  christos 	int ret;
    684  1.1  christos 	const unsigned char *p;
    685  1.1  christos 	EVP_PKEY *pkey;
    686  1.1  christos 
    687  1.1  christos 	p=d;
    688  1.1  christos 	if ((pkey=d2i_PrivateKey(type,NULL,&p,(long)len)) == NULL)
    689  1.1  christos 		{
    690  1.1  christos 		SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
    691  1.1  christos 		return(0);
    692  1.1  christos 		}
    693  1.1  christos 
    694  1.1  christos 	ret=SSL_CTX_use_PrivateKey(ctx,pkey);
    695  1.1  christos 	EVP_PKEY_free(pkey);
    696  1.1  christos 	return(ret);
    697  1.1  christos 	}
    698  1.1  christos 
    699  1.1  christos 
    700  1.1  christos #ifndef OPENSSL_NO_STDIO
    701  1.1  christos /* Read a file that contains our certificate in "PEM" format,
    702  1.1  christos  * possibly followed by a sequence of CA certificates that should be
    703  1.1  christos  * sent to the peer in the Certificate message.
    704  1.1  christos  */
    705  1.1  christos int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file)
    706  1.1  christos 	{
    707  1.1  christos 	BIO *in;
    708  1.1  christos 	int ret=0;
    709  1.1  christos 	X509 *x=NULL;
    710  1.1  christos 
    711  1.1  christos 	ERR_clear_error(); /* clear error stack for SSL_CTX_use_certificate() */
    712  1.1  christos 
    713  1.1  christos 	in=BIO_new(BIO_s_file_internal());
    714  1.1  christos 	if (in == NULL)
    715  1.1  christos 		{
    716  1.1  christos 		SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE,ERR_R_BUF_LIB);
    717  1.1  christos 		goto end;
    718  1.1  christos 		}
    719  1.1  christos 
    720  1.1  christos 	if (BIO_read_filename(in,file) <= 0)
    721  1.1  christos 		{
    722  1.1  christos 		SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE,ERR_R_SYS_LIB);
    723  1.1  christos 		goto end;
    724  1.1  christos 		}
    725  1.1  christos 
    726  1.1  christos 	x=PEM_read_bio_X509(in,NULL,ctx->default_passwd_callback,ctx->default_passwd_callback_userdata);
    727  1.1  christos 	if (x == NULL)
    728  1.1  christos 		{
    729  1.1  christos 		SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE,ERR_R_PEM_LIB);
    730  1.1  christos 		goto end;
    731  1.1  christos 		}
    732  1.1  christos 
    733  1.1  christos 	ret=SSL_CTX_use_certificate(ctx,x);
    734  1.1  christos 	if (ERR_peek_error() != 0)
    735  1.1  christos 		ret = 0;  /* Key/certificate mismatch doesn't imply ret==0 ... */
    736  1.1  christos 	if (ret)
    737  1.1  christos 		{
    738  1.1  christos 		/* If we could set up our certificate, now proceed to
    739  1.1  christos 		 * the CA certificates.
    740  1.1  christos 		 */
    741  1.1  christos 		X509 *ca;
    742  1.1  christos 		int r;
    743  1.1  christos 		unsigned long err;
    744  1.1  christos 
    745  1.1  christos 		if (ctx->extra_certs != NULL)
    746  1.1  christos 			{
    747  1.1  christos 			sk_X509_pop_free(ctx->extra_certs, X509_free);
    748  1.1  christos 			ctx->extra_certs = NULL;
    749  1.1  christos 			}
    750  1.1  christos 
    751  1.1  christos 		while ((ca = PEM_read_bio_X509(in,NULL,ctx->default_passwd_callback,ctx->default_passwd_callback_userdata))
    752  1.1  christos 			!= NULL)
    753  1.1  christos 			{
    754  1.1  christos 			r = SSL_CTX_add_extra_chain_cert(ctx, ca);
    755  1.1  christos 			if (!r)
    756  1.1  christos 				{
    757  1.1  christos 				X509_free(ca);
    758  1.1  christos 				ret = 0;
    759  1.1  christos 				goto end;
    760  1.1  christos 				}
    761  1.1  christos 			/* Note that we must not free r if it was successfully
    762  1.1  christos 			 * added to the chain (while we must free the main
    763  1.1  christos 			 * certificate, since its reference count is increased
    764  1.1  christos 			 * by SSL_CTX_use_certificate). */
    765  1.1  christos 			}
    766  1.1  christos 		/* When the while loop ends, it's usually just EOF. */
    767  1.1  christos 		err = ERR_peek_last_error();
    768  1.1  christos 		if (ERR_GET_LIB(err) == ERR_LIB_PEM && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)
    769  1.1  christos 			ERR_clear_error();
    770  1.1  christos 		else
    771  1.1  christos 			ret = 0; /* some real error */
    772  1.1  christos 		}
    773  1.1  christos 
    774  1.1  christos end:
    775  1.1  christos 	if (x != NULL) X509_free(x);
    776  1.1  christos 	if (in != NULL) BIO_free(in);
    777  1.1  christos 	return(ret);
    778  1.1  christos 	}
    779  1.1  christos #endif
    780