sl@0: /* ssl/ssl_rsa.c */
sl@0: /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
sl@0:  * All rights reserved.
sl@0:  *
sl@0:  * This package is an SSL implementation written
sl@0:  * by Eric Young (eay@cryptsoft.com).
sl@0:  * The implementation was written so as to conform with Netscapes SSL.
sl@0:  * 
sl@0:  * This library is free for commercial and non-commercial use as long as
sl@0:  * the following conditions are aheared to.  The following conditions
sl@0:  * apply to all code found in this distribution, be it the RC4, RSA,
sl@0:  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
sl@0:  * included with this distribution is covered by the same copyright terms
sl@0:  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
sl@0:  * 
sl@0:  * Copyright remains Eric Young's, and as such any Copyright notices in
sl@0:  * the code are not to be removed.
sl@0:  * If this package is used in a product, Eric Young should be given attribution
sl@0:  * as the author of the parts of the library used.
sl@0:  * This can be in the form of a textual message at program startup or
sl@0:  * in documentation (online or textual) provided with the package.
sl@0:  * 
sl@0:  * Redistribution and use in source and binary forms, with or without
sl@0:  * modification, are permitted provided that the following conditions
sl@0:  * are met:
sl@0:  * 1. Redistributions of source code must retain the copyright
sl@0:  *    notice, this list of conditions and the following disclaimer.
sl@0:  * 2. Redistributions in binary form must reproduce the above copyright
sl@0:  *    notice, this list of conditions and the following disclaimer in the
sl@0:  *    documentation and/or other materials provided with the distribution.
sl@0:  * 3. All advertising materials mentioning features or use of this software
sl@0:  *    must display the following acknowledgement:
sl@0:  *    "This product includes cryptographic software written by
sl@0:  *     Eric Young (eay@cryptsoft.com)"
sl@0:  *    The word 'cryptographic' can be left out if the rouines from the library
sl@0:  *    being used are not cryptographic related :-).
sl@0:  * 4. If you include any Windows specific code (or a derivative thereof) from 
sl@0:  *    the apps directory (application code) you must include an acknowledgement:
sl@0:  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
sl@0:  * 
sl@0:  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
sl@0:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
sl@0:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
sl@0:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
sl@0:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
sl@0:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
sl@0:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sl@0:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
sl@0:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
sl@0:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
sl@0:  * SUCH DAMAGE.
sl@0:  * 
sl@0:  * The licence and distribution terms for any publically available version or
sl@0:  * derivative of this code cannot be changed.  i.e. this code cannot simply be
sl@0:  * copied and put under another distribution licence
sl@0:  * [including the GNU Public Licence.]
sl@0:  */
sl@0: 
sl@0: #include <stdio.h>
sl@0: #include "ssl_locl.h"
sl@0: #include <openssl/bio.h>
sl@0: #include <openssl/objects.h>
sl@0: #include <openssl/evp.h>
sl@0: #include <openssl/x509.h>
sl@0: #include <openssl/pem.h>
sl@0: 
sl@0: static int ssl_set_cert(CERT *c, X509 *x509);
sl@0: static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey);
sl@0: EXPORT_C int SSL_use_certificate(SSL *ssl, X509 *x)
sl@0: 	{
sl@0: 	if (x == NULL)
sl@0: 		{
sl@0: 		SSLerr(SSL_F_SSL_USE_CERTIFICATE,ERR_R_PASSED_NULL_PARAMETER);
sl@0: 		return(0);
sl@0: 		}
sl@0: 	if (!ssl_cert_inst(&ssl->cert))
sl@0: 		{
sl@0: 		SSLerr(SSL_F_SSL_USE_CERTIFICATE,ERR_R_MALLOC_FAILURE);
sl@0: 		return(0);
sl@0: 		}
sl@0: 	return(ssl_set_cert(ssl->cert,x));
sl@0: 	}
sl@0: 
sl@0: #ifndef OPENSSL_NO_STDIO
sl@0: EXPORT_C int SSL_use_certificate_file(SSL *ssl, const char *file, int type)
sl@0: 	{
sl@0: 	int j;
sl@0: 	BIO *in;
sl@0: 	int ret=0;
sl@0: 	X509 *x=NULL;
sl@0: 
sl@0: 	in=BIO_new(BIO_s_file_internal());
sl@0: 	if (in == NULL)
sl@0: 		{
sl@0: 		SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE,ERR_R_BUF_LIB);
sl@0: 		goto end;
sl@0: 		}
sl@0: 
sl@0: 	if (BIO_read_filename(in,file) <= 0)
sl@0: 		{
sl@0: 		SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE,ERR_R_SYS_LIB);
sl@0: 		goto end;
sl@0: 		}
sl@0: 	if (type == SSL_FILETYPE_ASN1)
sl@0: 		{
sl@0: 		j=ERR_R_ASN1_LIB;
sl@0: 		x=d2i_X509_bio(in,NULL);
sl@0: 		}
sl@0: 	else if (type == SSL_FILETYPE_PEM)
sl@0: 		{
sl@0: 		j=ERR_R_PEM_LIB;
sl@0: 		x=PEM_read_bio_X509(in,NULL,ssl->ctx->default_passwd_callback,ssl->ctx->default_passwd_callback_userdata);
sl@0: 		}
sl@0: 	else
sl@0: 		{
sl@0: 		SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE,SSL_R_BAD_SSL_FILETYPE);
sl@0: 		goto end;
sl@0: 		}
sl@0: 
sl@0: 	if (x == NULL)
sl@0: 		{
sl@0: 		SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE,j);
sl@0: 		goto end;
sl@0: 		}
sl@0: 
sl@0: 	ret=SSL_use_certificate(ssl,x);
sl@0: end:
sl@0: 	if (x != NULL) X509_free(x);
sl@0: 	if (in != NULL) BIO_free(in);
sl@0: 	return(ret);
sl@0: 	}
sl@0: #endif
sl@0: 
sl@0: EXPORT_C int SSL_use_certificate_ASN1(SSL *ssl, const unsigned char *d, int len)
sl@0: 	{
sl@0: 	X509 *x;
sl@0: 	int ret;
sl@0: 
sl@0: 	x=d2i_X509(NULL,&d,(long)len);
sl@0: 	if (x == NULL)
sl@0: 		{
sl@0: 		SSLerr(SSL_F_SSL_USE_CERTIFICATE_ASN1,ERR_R_ASN1_LIB);
sl@0: 		return(0);
sl@0: 		}
sl@0: 
sl@0: 	ret=SSL_use_certificate(ssl,x);
sl@0: 	X509_free(x);
sl@0: 	return(ret);
sl@0: 	}
sl@0: 
sl@0: #ifndef OPENSSL_NO_RSA
sl@0: EXPORT_C int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa)
sl@0: 	{
sl@0: 	EVP_PKEY *pkey;
sl@0: 	int ret;
sl@0: 
sl@0: 	if (rsa == NULL)
sl@0: 		{
sl@0: 		SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
sl@0: 		return(0);
sl@0: 		}
sl@0: 	if (!ssl_cert_inst(&ssl->cert))
sl@0: 		{
sl@0: 		SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY,ERR_R_MALLOC_FAILURE);
sl@0: 		return(0);
sl@0: 		}
sl@0: 	if ((pkey=EVP_PKEY_new()) == NULL)
sl@0: 		{
sl@0: 		SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY,ERR_R_EVP_LIB);
sl@0: 		return(0);
sl@0: 		}
sl@0: 
sl@0: 	RSA_up_ref(rsa);
sl@0: 	EVP_PKEY_assign_RSA(pkey,rsa);
sl@0: 
sl@0: 	ret=ssl_set_pkey(ssl->cert,pkey);
sl@0: 	EVP_PKEY_free(pkey);
sl@0: 	return(ret);
sl@0: 	}
sl@0: #endif
sl@0: 
sl@0: static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey)
sl@0: 	{
sl@0: 	int i;
sl@0: 
sl@0: 	i=ssl_cert_type(NULL,pkey);
sl@0: 	if (i < 0)
sl@0: 		{
sl@0: 		SSLerr(SSL_F_SSL_SET_PKEY,SSL_R_UNKNOWN_CERTIFICATE_TYPE);
sl@0: 		return(0);
sl@0: 		}
sl@0: 
sl@0: 	if (c->pkeys[i].x509 != NULL)
sl@0: 		{
sl@0: 		EVP_PKEY *pktmp;
sl@0: 		pktmp =	X509_get_pubkey(c->pkeys[i].x509);
sl@0: 		EVP_PKEY_copy_parameters(pktmp,pkey);
sl@0: 		EVP_PKEY_free(pktmp);
sl@0: 		ERR_clear_error();
sl@0: 
sl@0: #ifndef OPENSSL_NO_RSA
sl@0: 		/* Don't check the public/private key, this is mostly
sl@0: 		 * for smart cards. */
sl@0: 		if ((pkey->type == EVP_PKEY_RSA) &&
sl@0: 			(RSA_flags(pkey->pkey.rsa) & RSA_METHOD_FLAG_NO_CHECK))
sl@0: 			;
sl@0: 		else
sl@0: #endif
sl@0: 		if (!X509_check_private_key(c->pkeys[i].x509,pkey))
sl@0: 			{
sl@0: 			X509_free(c->pkeys[i].x509);
sl@0: 			c->pkeys[i].x509 = NULL;
sl@0: 			return 0;
sl@0: 			}
sl@0: 		}
sl@0: 
sl@0: 	if (c->pkeys[i].privatekey != NULL)
sl@0: 		EVP_PKEY_free(c->pkeys[i].privatekey);
sl@0: 	CRYPTO_add(&pkey->references,1,CRYPTO_LOCK_EVP_PKEY);
sl@0: 	c->pkeys[i].privatekey=pkey;
sl@0: 	c->key= &(c->pkeys[i]);
sl@0: 
sl@0: 	c->valid=0;
sl@0: 	return(1);
sl@0: 	}
sl@0: 
sl@0: #ifndef OPENSSL_NO_RSA
sl@0: #ifndef OPENSSL_NO_STDIO
sl@0: EXPORT_C int SSL_use_RSAPrivateKey_file(SSL *ssl, const char *file, int type)
sl@0: 	{
sl@0: 	int j,ret=0;
sl@0: 	BIO *in;
sl@0: 	RSA *rsa=NULL;
sl@0: 
sl@0: 	in=BIO_new(BIO_s_file_internal());
sl@0: 	if (in == NULL)
sl@0: 		{
sl@0: 		SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,ERR_R_BUF_LIB);
sl@0: 		goto end;
sl@0: 		}
sl@0: 
sl@0: 	if (BIO_read_filename(in,file) <= 0)
sl@0: 		{
sl@0: 		SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,ERR_R_SYS_LIB);
sl@0: 		goto end;
sl@0: 		}
sl@0: 	if	(type == SSL_FILETYPE_ASN1)
sl@0: 		{
sl@0: 		j=ERR_R_ASN1_LIB;
sl@0: 		rsa=d2i_RSAPrivateKey_bio(in,NULL);
sl@0: 		}
sl@0: 	else if (type == SSL_FILETYPE_PEM)
sl@0: 		{
sl@0: 		j=ERR_R_PEM_LIB;
sl@0: 		rsa=PEM_read_bio_RSAPrivateKey(in,NULL,
sl@0: 			ssl->ctx->default_passwd_callback,ssl->ctx->default_passwd_callback_userdata);
sl@0: 		}
sl@0: 	else
sl@0: 		{
sl@0: 		SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
sl@0: 		goto end;
sl@0: 		}
sl@0: 	if (rsa == NULL)
sl@0: 		{
sl@0: 		SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,j);
sl@0: 		goto end;
sl@0: 		}
sl@0: 	ret=SSL_use_RSAPrivateKey(ssl,rsa);
sl@0: 	RSA_free(rsa);
sl@0: end:
sl@0: 	if (in != NULL) BIO_free(in);
sl@0: 	return(ret);
sl@0: 	}
sl@0: #endif
sl@0: 
sl@0: EXPORT_C int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, unsigned char *d, long len)
sl@0: 	{
sl@0: 	int ret;
sl@0: 	const unsigned char *p;
sl@0: 	RSA *rsa;
sl@0: 
sl@0: 	p=d;
sl@0: 	if ((rsa=d2i_RSAPrivateKey(NULL,&p,(long)len)) == NULL)
sl@0: 		{
sl@0: 		SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
sl@0: 		return(0);
sl@0: 		}
sl@0: 
sl@0: 	ret=SSL_use_RSAPrivateKey(ssl,rsa);
sl@0: 	RSA_free(rsa);
sl@0: 	return(ret);
sl@0: 	}
sl@0: #endif /* !OPENSSL_NO_RSA */
sl@0: 
sl@0: EXPORT_C int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey)
sl@0: 	{
sl@0: 	int ret;
sl@0: 
sl@0: 	if (pkey == NULL)
sl@0: 		{
sl@0: 		SSLerr(SSL_F_SSL_USE_PRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
sl@0: 		return(0);
sl@0: 		}
sl@0: 	if (!ssl_cert_inst(&ssl->cert))
sl@0: 		{
sl@0: 		SSLerr(SSL_F_SSL_USE_PRIVATEKEY,ERR_R_MALLOC_FAILURE);
sl@0: 		return(0);
sl@0: 		}
sl@0: 	ret=ssl_set_pkey(ssl->cert,pkey);
sl@0: 	return(ret);
sl@0: 	}
sl@0: 
sl@0: #ifndef OPENSSL_NO_STDIO
sl@0: EXPORT_C int SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type)
sl@0: 	{
sl@0: 	int j,ret=0;
sl@0: 	BIO *in;
sl@0: 	EVP_PKEY *pkey=NULL;
sl@0: 
sl@0: 	in=BIO_new(BIO_s_file_internal());
sl@0: 	if (in == NULL)
sl@0: 		{
sl@0: 		SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,ERR_R_BUF_LIB);
sl@0: 		goto end;
sl@0: 		}
sl@0: 
sl@0: 	if (BIO_read_filename(in,file) <= 0)
sl@0: 		{
sl@0: 		SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,ERR_R_SYS_LIB);
sl@0: 		goto end;
sl@0: 		}
sl@0: 	if (type == SSL_FILETYPE_PEM)
sl@0: 		{
sl@0: 		j=ERR_R_PEM_LIB;
sl@0: 		pkey=PEM_read_bio_PrivateKey(in,NULL,
sl@0: 			ssl->ctx->default_passwd_callback,ssl->ctx->default_passwd_callback_userdata);
sl@0: 		}
sl@0: 	else if (type == SSL_FILETYPE_ASN1)
sl@0: 		{
sl@0: 		j = ERR_R_ASN1_LIB;
sl@0: 		pkey = d2i_PrivateKey_bio(in,NULL);
sl@0: 		}
sl@0: 	else
sl@0: 		{
sl@0: 		SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
sl@0: 		goto end;
sl@0: 		}
sl@0: 	if (pkey == NULL)
sl@0: 		{
sl@0: 		SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,j);
sl@0: 		goto end;
sl@0: 		}
sl@0: 	ret=SSL_use_PrivateKey(ssl,pkey);
sl@0: 	EVP_PKEY_free(pkey);
sl@0: end:
sl@0: 	if (in != NULL) BIO_free(in);
sl@0: 	return(ret);
sl@0: 	}
sl@0: #endif
sl@0: 
sl@0: EXPORT_C int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const unsigned char *d, long len)
sl@0: 	{
sl@0: 	int ret;
sl@0: 	const unsigned char *p;
sl@0: 	EVP_PKEY *pkey;
sl@0: 
sl@0: 	p=d;
sl@0: 	if ((pkey=d2i_PrivateKey(type,NULL,&p,(long)len)) == NULL)
sl@0: 		{
sl@0: 		SSLerr(SSL_F_SSL_USE_PRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
sl@0: 		return(0);
sl@0: 		}
sl@0: 
sl@0: 	ret=SSL_use_PrivateKey(ssl,pkey);
sl@0: 	EVP_PKEY_free(pkey);
sl@0: 	return(ret);
sl@0: 	}
sl@0: 
sl@0: EXPORT_C int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x)
sl@0: 	{
sl@0: 	if (x == NULL)
sl@0: 		{
sl@0: 		SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE,ERR_R_PASSED_NULL_PARAMETER);
sl@0: 		return(0);
sl@0: 		}
sl@0: 	if (!ssl_cert_inst(&ctx->cert))
sl@0: 		{
sl@0: 		SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE,ERR_R_MALLOC_FAILURE);
sl@0: 		return(0);
sl@0: 		}
sl@0: 	return(ssl_set_cert(ctx->cert, x));
sl@0: 	}
sl@0: 
sl@0: static int ssl_set_cert(CERT *c, X509 *x)
sl@0: 	{
sl@0: 	EVP_PKEY *pkey;
sl@0: 	int i;
sl@0: 
sl@0: 	pkey=X509_get_pubkey(x);
sl@0: 	if (pkey == NULL)
sl@0: 		{
sl@0: 		SSLerr(SSL_F_SSL_SET_CERT,SSL_R_X509_LIB);
sl@0: 		return(0);
sl@0: 		}
sl@0: 
sl@0: 	i=ssl_cert_type(x,pkey);
sl@0: 	if (i < 0)
sl@0: 		{
sl@0: 		SSLerr(SSL_F_SSL_SET_CERT,SSL_R_UNKNOWN_CERTIFICATE_TYPE);
sl@0: 		EVP_PKEY_free(pkey);
sl@0: 		return(0);
sl@0: 		}
sl@0: 
sl@0: 	if (c->pkeys[i].privatekey != NULL)
sl@0: 		{
sl@0: 		EVP_PKEY_copy_parameters(pkey,c->pkeys[i].privatekey);
sl@0: 		ERR_clear_error();
sl@0: 
sl@0: #ifndef OPENSSL_NO_RSA
sl@0: 		/* Don't check the public/private key, this is mostly
sl@0: 		 * for smart cards. */
sl@0: 		if ((c->pkeys[i].privatekey->type == EVP_PKEY_RSA) &&
sl@0: 			(RSA_flags(c->pkeys[i].privatekey->pkey.rsa) &
sl@0: 			 RSA_METHOD_FLAG_NO_CHECK))
sl@0: 			 ;
sl@0: 		else
sl@0: #endif /* OPENSSL_NO_RSA */
sl@0: 		if (!X509_check_private_key(x,c->pkeys[i].privatekey))
sl@0: 			{
sl@0: 			/* don't fail for a cert/key mismatch, just free
sl@0: 			 * current private key (when switching to a different
sl@0: 			 * cert & key, first this function should be used,
sl@0: 			 * then ssl_set_pkey */
sl@0: 			EVP_PKEY_free(c->pkeys[i].privatekey);
sl@0: 			c->pkeys[i].privatekey=NULL;
sl@0: 			/* clear error queue */
sl@0: 			ERR_clear_error();
sl@0: 			}
sl@0: 		}
sl@0: 
sl@0: 	EVP_PKEY_free(pkey);
sl@0: 
sl@0: 	if (c->pkeys[i].x509 != NULL)
sl@0: 		X509_free(c->pkeys[i].x509);
sl@0: 	CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
sl@0: 	c->pkeys[i].x509=x;
sl@0: 	c->key= &(c->pkeys[i]);
sl@0: 
sl@0: 	c->valid=0;
sl@0: 	return(1);
sl@0: 	}
sl@0: 
sl@0: #ifndef OPENSSL_NO_STDIO
sl@0: EXPORT_C int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type)
sl@0: 	{
sl@0: 	int j;
sl@0: 	BIO *in;
sl@0: 	int ret=0;
sl@0: 	X509 *x=NULL;
sl@0: 
sl@0: 	in=BIO_new(BIO_s_file_internal());
sl@0: 	if (in == NULL)
sl@0: 		{
sl@0: 		SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,ERR_R_BUF_LIB);
sl@0: 		goto end;
sl@0: 		}
sl@0: 
sl@0: 	if (BIO_read_filename(in,file) <= 0)
sl@0: 		{
sl@0: 		SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,ERR_R_SYS_LIB);
sl@0: 		goto end;
sl@0: 		}
sl@0: 	if (type == SSL_FILETYPE_ASN1)
sl@0: 		{
sl@0: 		j=ERR_R_ASN1_LIB;
sl@0: 		x=d2i_X509_bio(in,NULL);
sl@0: 		}
sl@0: 	else if (type == SSL_FILETYPE_PEM)
sl@0: 		{
sl@0: 		j=ERR_R_PEM_LIB;
sl@0: 		x=PEM_read_bio_X509(in,NULL,ctx->default_passwd_callback,ctx->default_passwd_callback_userdata);
sl@0: 		}
sl@0: 	else
sl@0: 		{
sl@0: 		SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,SSL_R_BAD_SSL_FILETYPE);
sl@0: 		goto end;
sl@0: 		}
sl@0: 
sl@0: 	if (x == NULL)
sl@0: 		{
sl@0: 		SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,j);
sl@0: 		goto end;
sl@0: 		}
sl@0: 
sl@0: 	ret=SSL_CTX_use_certificate(ctx,x);
sl@0: end:
sl@0: 	if (x != NULL) X509_free(x);
sl@0: 	if (in != NULL) BIO_free(in);
sl@0: 	return(ret);
sl@0: 	}
sl@0: #endif
sl@0: 
sl@0: EXPORT_C int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len, const unsigned char *d)
sl@0: 	{
sl@0: 	X509 *x;
sl@0: 	int ret;
sl@0: 
sl@0: 	x=d2i_X509(NULL,&d,(long)len);
sl@0: 	if (x == NULL)
sl@0: 		{
sl@0: 		SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1,ERR_R_ASN1_LIB);
sl@0: 		return(0);
sl@0: 		}
sl@0: 
sl@0: 	ret=SSL_CTX_use_certificate(ctx,x);
sl@0: 	X509_free(x);
sl@0: 	return(ret);
sl@0: 	}
sl@0: 
sl@0: #ifndef OPENSSL_NO_RSA
sl@0: EXPORT_C int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa)
sl@0: 	{
sl@0: 	int ret;
sl@0: 	EVP_PKEY *pkey;
sl@0: 
sl@0: 	if (rsa == NULL)
sl@0: 		{
sl@0: 		SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
sl@0: 		return(0);
sl@0: 		}
sl@0: 	if (!ssl_cert_inst(&ctx->cert))
sl@0: 		{
sl@0: 		SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY,ERR_R_MALLOC_FAILURE);
sl@0: 		return(0);
sl@0: 		}
sl@0: 	if ((pkey=EVP_PKEY_new()) == NULL)
sl@0: 		{
sl@0: 		SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY,ERR_R_EVP_LIB);
sl@0: 		return(0);
sl@0: 		}
sl@0: 
sl@0: 	RSA_up_ref(rsa);
sl@0: 	EVP_PKEY_assign_RSA(pkey,rsa);
sl@0: 
sl@0: 	ret=ssl_set_pkey(ctx->cert, pkey);
sl@0: 	EVP_PKEY_free(pkey);
sl@0: 	return(ret);
sl@0: 	}
sl@0: 
sl@0: #ifndef OPENSSL_NO_STDIO
sl@0: EXPORT_C int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, int type)
sl@0: 	{
sl@0: 	int j,ret=0;
sl@0: 	BIO *in;
sl@0: 	RSA *rsa=NULL;
sl@0: 
sl@0: 	in=BIO_new(BIO_s_file_internal());
sl@0: 	if (in == NULL)
sl@0: 		{
sl@0: 		SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,ERR_R_BUF_LIB);
sl@0: 		goto end;
sl@0: 		}
sl@0: 
sl@0: 	if (BIO_read_filename(in,file) <= 0)
sl@0: 		{
sl@0: 		SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,ERR_R_SYS_LIB);
sl@0: 		goto end;
sl@0: 		}
sl@0: 	if	(type == SSL_FILETYPE_ASN1)
sl@0: 		{
sl@0: 		j=ERR_R_ASN1_LIB;
sl@0: 		rsa=d2i_RSAPrivateKey_bio(in,NULL);
sl@0: 		}
sl@0: 	else if (type == SSL_FILETYPE_PEM)
sl@0: 		{
sl@0: 		j=ERR_R_PEM_LIB;
sl@0: 		rsa=PEM_read_bio_RSAPrivateKey(in,NULL,
sl@0: 			ctx->default_passwd_callback,ctx->default_passwd_callback_userdata);
sl@0: 		}
sl@0: 	else
sl@0: 		{
sl@0: 		SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
sl@0: 		goto end;
sl@0: 		}
sl@0: 	if (rsa == NULL)
sl@0: 		{
sl@0: 		SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,j);
sl@0: 		goto end;
sl@0: 		}
sl@0: 	ret=SSL_CTX_use_RSAPrivateKey(ctx,rsa);
sl@0: 	RSA_free(rsa);
sl@0: end:
sl@0: 	if (in != NULL) BIO_free(in);
sl@0: 	return(ret);
sl@0: 	}
sl@0: #endif
sl@0: 
sl@0: EXPORT_C int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const unsigned char *d, long len)
sl@0: 	{
sl@0: 	int ret;
sl@0: 	const unsigned char *p;
sl@0: 	RSA *rsa;
sl@0: 
sl@0: 	p=d;
sl@0: 	if ((rsa=d2i_RSAPrivateKey(NULL,&p,(long)len)) == NULL)
sl@0: 		{
sl@0: 		SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
sl@0: 		return(0);
sl@0: 		}
sl@0: 
sl@0: 	ret=SSL_CTX_use_RSAPrivateKey(ctx,rsa);
sl@0: 	RSA_free(rsa);
sl@0: 	return(ret);
sl@0: 	}
sl@0: #endif /* !OPENSSL_NO_RSA */
sl@0: 
sl@0: EXPORT_C int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey)
sl@0: 	{
sl@0: 	if (pkey == NULL)
sl@0: 		{
sl@0: 		SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
sl@0: 		return(0);
sl@0: 		}
sl@0: 	if (!ssl_cert_inst(&ctx->cert))
sl@0: 		{
sl@0: 		SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY,ERR_R_MALLOC_FAILURE);
sl@0: 		return(0);
sl@0: 		}
sl@0: 	return(ssl_set_pkey(ctx->cert,pkey));
sl@0: 	}
sl@0: 
sl@0: #ifndef OPENSSL_NO_STDIO
sl@0: EXPORT_C SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
sl@0: 	{
sl@0: 	int j,ret=0;
sl@0: 	BIO *in;
sl@0: 	EVP_PKEY *pkey=NULL;
sl@0: 
sl@0: 	in=BIO_new(BIO_s_file_internal());
sl@0: 	if (in == NULL)
sl@0: 		{
sl@0: 		SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,ERR_R_BUF_LIB);
sl@0: 		goto end;
sl@0: 		}
sl@0: 
sl@0: 	if (BIO_read_filename(in,file) <= 0)
sl@0: 		{
sl@0: 		SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,ERR_R_SYS_LIB);
sl@0: 		goto end;
sl@0: 		}
sl@0: 	if (type == SSL_FILETYPE_PEM)
sl@0: 		{
sl@0: 		j=ERR_R_PEM_LIB;
sl@0: 		pkey=PEM_read_bio_PrivateKey(in,NULL,
sl@0: 			ctx->default_passwd_callback,ctx->default_passwd_callback_userdata);
sl@0: 		}
sl@0: 	else if (type == SSL_FILETYPE_ASN1)
sl@0: 		{
sl@0: 		j = ERR_R_ASN1_LIB;
sl@0: 		pkey = d2i_PrivateKey_bio(in,NULL);
sl@0: 		}
sl@0: 	else
sl@0: 		{
sl@0: 		SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
sl@0: 		goto end;
sl@0: 		}
sl@0: 	if (pkey == NULL)
sl@0: 		{
sl@0: 		SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,j);
sl@0: 		goto end;
sl@0: 		}
sl@0: 	ret=SSL_CTX_use_PrivateKey(ctx,pkey);
sl@0: 	EVP_PKEY_free(pkey);
sl@0: end:
sl@0: 	if (in != NULL) BIO_free(in);
sl@0: 	return(ret);
sl@0: 	}
sl@0: #endif
sl@0: 
sl@0: EXPORT_C int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx, const unsigned char *d,
sl@0: 	     long len)
sl@0: 	{
sl@0: 	int ret;
sl@0: 	const unsigned char *p;
sl@0: 	EVP_PKEY *pkey;
sl@0: 
sl@0: 	p=d;
sl@0: 	if ((pkey=d2i_PrivateKey(type,NULL,&p,(long)len)) == NULL)
sl@0: 		{
sl@0: 		SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
sl@0: 		return(0);
sl@0: 		}
sl@0: 
sl@0: 	ret=SSL_CTX_use_PrivateKey(ctx,pkey);
sl@0: 	EVP_PKEY_free(pkey);
sl@0: 	return(ret);
sl@0: 	}
sl@0: 
sl@0: 
sl@0: #ifndef OPENSSL_NO_STDIO
sl@0: /* Read a file that contains our certificate in "PEM" format,
sl@0:  * possibly followed by a sequence of CA certificates that should be
sl@0:  * sent to the peer in the Certificate message.
sl@0:  */
sl@0: EXPORT_C int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file)
sl@0: 	{
sl@0: 	BIO *in;
sl@0: 	int ret=0;
sl@0: 	X509 *x=NULL;
sl@0: 
sl@0: 	in=BIO_new(BIO_s_file_internal());
sl@0: 	if (in == NULL)
sl@0: 		{
sl@0: 		SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE,ERR_R_BUF_LIB);
sl@0: 		goto end;
sl@0: 		}
sl@0: 
sl@0: 	if (BIO_read_filename(in,file) <= 0)
sl@0: 		{
sl@0: 		SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE,ERR_R_SYS_LIB);
sl@0: 		goto end;
sl@0: 		}
sl@0: 
sl@0: 	x=PEM_read_bio_X509(in,NULL,ctx->default_passwd_callback,ctx->default_passwd_callback_userdata);
sl@0: 	if (x == NULL)
sl@0: 		{
sl@0: 		SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE,ERR_R_PEM_LIB);
sl@0: 		goto end;
sl@0: 		}
sl@0: 
sl@0: 	ret=SSL_CTX_use_certificate(ctx,x);
sl@0: 	if (ERR_peek_error() != 0)
sl@0: 		ret = 0;  /* Key/certificate mismatch doesn't imply ret==0 ... */
sl@0: 	if (ret)
sl@0: 		{
sl@0: 		/* If we could set up our certificate, now proceed to
sl@0: 		 * the CA certificates.
sl@0: 		 */
sl@0: 		X509 *ca;
sl@0: 		int r;
sl@0: 		unsigned long err;
sl@0: 		
sl@0: 		if (ctx->extra_certs != NULL) 
sl@0: 			{
sl@0: 			sk_X509_pop_free(ctx->extra_certs, X509_free);
sl@0: 			ctx->extra_certs = NULL;
sl@0: 			}
sl@0: 
sl@0: 		while ((ca = PEM_read_bio_X509(in,NULL,ctx->default_passwd_callback,ctx->default_passwd_callback_userdata))
sl@0: 			!= NULL)
sl@0: 			{
sl@0: 			r = SSL_CTX_add_extra_chain_cert(ctx, ca);
sl@0: 			if (!r) 
sl@0: 				{
sl@0: 				X509_free(ca);
sl@0: 				ret = 0;
sl@0: 				goto end;
sl@0: 				}
sl@0: 			/* Note that we must not free r if it was successfully
sl@0: 			 * added to the chain (while we must free the main
sl@0: 			 * certificate, since its reference count is increased
sl@0: 			 * by SSL_CTX_use_certificate). */
sl@0: 			}
sl@0: 		/* When the while loop ends, it's usually just EOF. */
sl@0: 		err = ERR_peek_last_error();
sl@0: 		if (ERR_GET_LIB(err) == ERR_LIB_PEM && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)
sl@0: 			ERR_clear_error();
sl@0: 		else 
sl@0: 			ret = 0; /* some real error */
sl@0: 		}
sl@0: 
sl@0: end:
sl@0: 	if (x != NULL) X509_free(x);
sl@0: 	if (in != NULL) BIO_free(in);
sl@0: 	return(ret);
sl@0: 	}
sl@0: #endif