2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
68 static int ssl_set_cert(CERT *c, X509 *x509);
69 static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey);
71 static int ssl_set_cert();
72 static int ssl_set_pkey();
75 int SSL_use_certificate(ssl, x)
83 SSLerr(SSL_F_SSL_USE_CERTIFICATE,ERR_R_PASSED_NULL_PARAMETER);
86 if ((ssl->cert == NULL) || (ssl->cert == ssl->ctx->default_cert))
91 SSLerr(SSL_F_SSL_USE_CERTIFICATE,ERR_R_MALLOC_FAILURE);
94 if (ssl->cert != NULL) ssl_cert_free(ssl->cert);
99 return(ssl_set_cert(c,x));
103 int SSL_use_certificate_file(ssl, file, type)
113 in=BIO_new(BIO_s_file_internal());
116 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE,ERR_R_BUF_LIB);
120 if (BIO_read_filename(in,file) <= 0)
122 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE,ERR_R_SYS_LIB);
125 if (type == SSL_FILETYPE_ASN1)
128 x=d2i_X509_bio(in,NULL);
130 else if (type == SSL_FILETYPE_PEM)
133 x=PEM_read_bio_X509(in,NULL,ssl->ctx->default_passwd_callback);
137 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE,SSL_R_BAD_SSL_FILETYPE);
143 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE,j);
147 ret=SSL_use_certificate(ssl,x);
149 if (x != NULL) X509_free(x);
150 if (in != NULL) BIO_free(in);
155 int SSL_use_certificate_ASN1(ssl, len, d)
163 x=d2i_X509(NULL,&d,(long)len);
166 SSLerr(SSL_F_SSL_USE_CERTIFICATE_ASN1,ERR_R_ASN1_LIB);
170 ret=SSL_use_certificate(ssl,x);
176 int SSL_use_RSAPrivateKey(ssl, rsa)
186 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
190 if ((ssl->cert == NULL) || (ssl->cert == ssl->ctx->default_cert))
195 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY,ERR_R_MALLOC_FAILURE);
198 if (ssl->cert != NULL) ssl_cert_free(ssl->cert);
202 if ((pkey=EVP_PKEY_new()) == NULL)
204 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY,ERR_R_EVP_LIB);
208 CRYPTO_add(&rsa->references,1,CRYPTO_LOCK_RSA);
209 EVP_PKEY_assign_RSA(pkey,rsa);
211 ret=ssl_set_pkey(c,pkey);
217 static int ssl_set_pkey(c,pkey)
223 i=ssl_cert_type(NULL,pkey);
226 SSLerr(SSL_F_SSL_SET_PKEY,SSL_R_UNKNOWN_CERTIFICATE_TYPE);
230 if (c->pkeys[i].x509 != NULL)
233 pktmp = X509_get_pubkey(c->pkeys[i].x509);
234 EVP_PKEY_copy_parameters(pktmp,pkey);
235 EVP_PKEY_free(pktmp);
239 /* Don't check the public/private key, this is mostly
240 * for smart cards. */
241 if ((pkey->type == EVP_PKEY_RSA) &&
242 (RSA_flags(pkey->pkey.rsa) &
243 RSA_METHOD_FLAG_NO_CHECK))
247 if (!X509_check_private_key(c->pkeys[i].x509,pkey))
249 if ((i == SSL_PKEY_DH_RSA) || (i == SSL_PKEY_DH_DSA))
251 i=(i == SSL_PKEY_DH_RSA)?
252 SSL_PKEY_DH_DSA:SSL_PKEY_DH_RSA;
254 if (c->pkeys[i].x509 == NULL)
258 if (!X509_check_private_key(
259 c->pkeys[i].x509,pkey))
276 X509_free(c->pkeys[i].x509);
277 c->pkeys[i].x509=NULL;
281 if (c->pkeys[i].privatekey != NULL)
282 EVP_PKEY_free(c->pkeys[i].privatekey);
283 CRYPTO_add(&pkey->references,1,CRYPTO_LOCK_EVP_PKEY);
284 c->pkeys[i].privatekey=pkey;
285 c->key= &(c->pkeys[i]);
293 int SSL_use_RSAPrivateKey_file(ssl, file, type)
302 in=BIO_new(BIO_s_file_internal());
305 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,ERR_R_BUF_LIB);
309 if (BIO_read_filename(in,file) <= 0)
311 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,ERR_R_SYS_LIB);
314 if (type == SSL_FILETYPE_ASN1)
317 rsa=d2i_RSAPrivateKey_bio(in,NULL);
319 else if (type == SSL_FILETYPE_PEM)
322 rsa=PEM_read_bio_RSAPrivateKey(in,NULL,
323 ssl->ctx->default_passwd_callback);
327 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
332 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,j);
335 ret=SSL_use_RSAPrivateKey(ssl,rsa);
338 if (in != NULL) BIO_free(in);
343 int SSL_use_RSAPrivateKey_ASN1(ssl,d,len)
353 if ((rsa=d2i_RSAPrivateKey(NULL,&p,(long)len)) == NULL)
355 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
359 ret=SSL_use_RSAPrivateKey(ssl,rsa);
365 int SSL_use_PrivateKey(ssl, pkey)
374 SSLerr(SSL_F_SSL_USE_PRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
378 if ((ssl->cert == NULL) || (ssl->cert == ssl->ctx->default_cert))
383 SSLerr(SSL_F_SSL_USE_PRIVATEKEY,ERR_R_MALLOC_FAILURE);
386 if (ssl->cert != NULL) ssl_cert_free(ssl->cert);
391 ret=ssl_set_pkey(c,pkey);
396 int SSL_use_PrivateKey_file(ssl, file, type)
405 in=BIO_new(BIO_s_file_internal());
408 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,ERR_R_BUF_LIB);
412 if (BIO_read_filename(in,file) <= 0)
414 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,ERR_R_SYS_LIB);
417 if (type == SSL_FILETYPE_PEM)
420 pkey=PEM_read_bio_PrivateKey(in,NULL,
421 ssl->ctx->default_passwd_callback);
425 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
430 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,j);
433 ret=SSL_use_PrivateKey(ssl,pkey);
436 if (in != NULL) BIO_free(in);
441 int SSL_use_PrivateKey_ASN1(type,ssl,d,len)
452 if ((pkey=d2i_PrivateKey(type,NULL,&p,(long)len)) == NULL)
454 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
458 ret=SSL_use_PrivateKey(ssl,pkey);
463 int SSL_CTX_use_certificate(ctx, x)
471 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE,ERR_R_PASSED_NULL_PARAMETER);
475 if (ctx->default_cert == NULL)
480 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE,ERR_R_MALLOC_FAILURE);
487 return(ssl_set_cert(c,x));
490 static int ssl_set_cert(c,x)
497 pkey=X509_get_pubkey(x);
500 SSLerr(SSL_F_SSL_SET_CERT,SSL_R_X509_LIB);
504 i=ssl_cert_type(x,pkey);
507 SSLerr(SSL_F_SSL_SET_CERT,SSL_R_UNKNOWN_CERTIFICATE_TYPE);
512 if (c->pkeys[i].privatekey != NULL)
514 EVP_PKEY_copy_parameters(pkey,c->pkeys[i].privatekey);
518 /* Don't check the public/private key, this is mostly
519 * for smart cards. */
520 if ((c->pkeys[i].privatekey->type == EVP_PKEY_RSA) &&
521 (RSA_flags(c->pkeys[i].privatekey->pkey.rsa) &
522 RSA_METHOD_FLAG_NO_CHECK))
527 if (!X509_check_private_key(x,c->pkeys[i].privatekey))
529 if ((i == SSL_PKEY_DH_RSA) || (i == SSL_PKEY_DH_DSA))
531 i=(i == SSL_PKEY_DH_RSA)?
532 SSL_PKEY_DH_DSA:SSL_PKEY_DH_RSA;
534 if (c->pkeys[i].privatekey == NULL)
538 if (!X509_check_private_key(x,
539 c->pkeys[i].privatekey))
558 EVP_PKEY_free(c->pkeys[i].privatekey);
559 c->pkeys[i].privatekey=NULL;
562 if (c->pkeys[i].x509 != NULL)
563 X509_free(c->pkeys[i].x509);
564 CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
566 c->key= &(c->pkeys[i]);
573 int SSL_CTX_use_certificate_file(ctx, file, type)
583 in=BIO_new(BIO_s_file_internal());
586 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,ERR_R_BUF_LIB);
590 if (BIO_read_filename(in,file) <= 0)
592 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,ERR_R_SYS_LIB);
595 if (type == SSL_FILETYPE_ASN1)
598 x=d2i_X509_bio(in,NULL);
600 else if (type == SSL_FILETYPE_PEM)
603 x=PEM_read_bio_X509(in,NULL,ctx->default_passwd_callback);
607 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,SSL_R_BAD_SSL_FILETYPE);
613 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,j);
617 ret=SSL_CTX_use_certificate(ctx,x);
619 if (x != NULL) X509_free(x);
620 if (in != NULL) BIO_free(in);
625 int SSL_CTX_use_certificate_ASN1(ctx, len, d)
633 x=d2i_X509(NULL,&d,(long)len);
636 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1,ERR_R_ASN1_LIB);
640 ret=SSL_CTX_use_certificate(ctx,x);
646 int SSL_CTX_use_RSAPrivateKey(ctx, rsa)
656 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
659 if (ctx->default_cert == NULL)
664 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY,ERR_R_MALLOC_FAILURE);
671 if ((pkey=EVP_PKEY_new()) == NULL)
673 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY,ERR_R_EVP_LIB);
677 CRYPTO_add(&rsa->references,1,CRYPTO_LOCK_RSA);
678 EVP_PKEY_assign_RSA(pkey,rsa);
680 ret=ssl_set_pkey(c,pkey);
686 int SSL_CTX_use_RSAPrivateKey_file(ctx, file, type)
695 in=BIO_new(BIO_s_file_internal());
698 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,ERR_R_BUF_LIB);
702 if (BIO_read_filename(in,file) <= 0)
704 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,ERR_R_SYS_LIB);
707 if (type == SSL_FILETYPE_ASN1)
710 rsa=d2i_RSAPrivateKey_bio(in,NULL);
712 else if (type == SSL_FILETYPE_PEM)
715 rsa=PEM_read_bio_RSAPrivateKey(in,NULL,
716 ctx->default_passwd_callback);
720 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
725 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,j);
728 ret=SSL_CTX_use_RSAPrivateKey(ctx,rsa);
731 if (in != NULL) BIO_free(in);
736 int SSL_CTX_use_RSAPrivateKey_ASN1(ctx,d,len)
746 if ((rsa=d2i_RSAPrivateKey(NULL,&p,(long)len)) == NULL)
748 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
752 ret=SSL_CTX_use_RSAPrivateKey(ctx,rsa);
758 int SSL_CTX_use_PrivateKey(ctx, pkey)
766 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
770 if (ctx->default_cert == NULL)
775 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY,ERR_R_MALLOC_FAILURE);
782 return(ssl_set_pkey(c,pkey));
786 int SSL_CTX_use_PrivateKey_file(ctx, file, type)
795 in=BIO_new(BIO_s_file_internal());
798 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,ERR_R_BUF_LIB);
802 if (BIO_read_filename(in,file) <= 0)
804 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,ERR_R_SYS_LIB);
807 if (type == SSL_FILETYPE_PEM)
810 pkey=PEM_read_bio_PrivateKey(in,NULL,
811 ctx->default_passwd_callback);
815 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
820 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,j);
823 ret=SSL_CTX_use_PrivateKey(ctx,pkey);
826 if (in != NULL) BIO_free(in);
831 int SSL_CTX_use_PrivateKey_ASN1(type,ctx,d,len)
842 if ((pkey=d2i_PrivateKey(type,NULL,&p,(long)len)) == NULL)
844 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
848 ret=SSL_CTX_use_PrivateKey(ctx,pkey);