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 /* Don't check the public/private key, this is mostly
234 * for smart cards. */
235 if ((pkey->type == EVP_PKEY_RSA) &&
236 (RSA_flags(pkey->pkey.rsa) &
237 RSA_METHOD_FLAG_NO_CHECK))
241 if (!X509_check_private_key(c->pkeys[i].x509,pkey))
243 if ((i == SSL_PKEY_DH_RSA) || (i == SSL_PKEY_DH_DSA))
245 i=(i == SSL_PKEY_DH_RSA)?
246 SSL_PKEY_DH_DSA:SSL_PKEY_DH_RSA;
248 if (c->pkeys[i].x509 == NULL)
252 if (!X509_check_private_key(
253 c->pkeys[i].x509,pkey))
270 X509_free(c->pkeys[i].x509);
271 c->pkeys[i].x509=NULL;
275 if (c->pkeys[i].privatekey != NULL)
276 EVP_PKEY_free(c->pkeys[i].privatekey);
277 CRYPTO_add(&pkey->references,1,CRYPTO_LOCK_EVP_PKEY);
278 c->pkeys[i].privatekey=pkey;
279 c->key= &(c->pkeys[i]);
287 int SSL_use_RSAPrivateKey_file(ssl, file, type)
296 in=BIO_new(BIO_s_file_internal());
299 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,ERR_R_BUF_LIB);
303 if (BIO_read_filename(in,file) <= 0)
305 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,ERR_R_SYS_LIB);
308 if (type == SSL_FILETYPE_ASN1)
311 rsa=d2i_RSAPrivateKey_bio(in,NULL);
313 else if (type == SSL_FILETYPE_PEM)
316 rsa=PEM_read_bio_RSAPrivateKey(in,NULL,
317 ssl->ctx->default_passwd_callback);
321 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
326 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,j);
329 ret=SSL_use_RSAPrivateKey(ssl,rsa);
332 if (in != NULL) BIO_free(in);
337 int SSL_use_RSAPrivateKey_ASN1(ssl,d,len)
347 if ((rsa=d2i_RSAPrivateKey(NULL,&p,(long)len)) == NULL)
349 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
353 ret=SSL_use_RSAPrivateKey(ssl,rsa);
359 int SSL_use_PrivateKey(ssl, pkey)
368 SSLerr(SSL_F_SSL_USE_PRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
372 if ((ssl->cert == NULL) || (ssl->cert == ssl->ctx->default_cert))
377 SSLerr(SSL_F_SSL_USE_PRIVATEKEY,ERR_R_MALLOC_FAILURE);
380 if (ssl->cert != NULL) ssl_cert_free(ssl->cert);
385 ret=ssl_set_pkey(c,pkey);
390 int SSL_use_PrivateKey_file(ssl, file, type)
399 in=BIO_new(BIO_s_file_internal());
402 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,ERR_R_BUF_LIB);
406 if (BIO_read_filename(in,file) <= 0)
408 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,ERR_R_SYS_LIB);
411 if (type == SSL_FILETYPE_PEM)
414 pkey=PEM_read_bio_PrivateKey(in,NULL,
415 ssl->ctx->default_passwd_callback);
419 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
424 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,j);
427 ret=SSL_use_PrivateKey(ssl,pkey);
430 if (in != NULL) BIO_free(in);
435 int SSL_use_PrivateKey_ASN1(type,ssl,d,len)
446 if ((pkey=d2i_PrivateKey(type,NULL,&p,(long)len)) == NULL)
448 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
452 ret=SSL_use_PrivateKey(ssl,pkey);
457 int SSL_CTX_use_certificate(ctx, x)
465 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE,ERR_R_PASSED_NULL_PARAMETER);
469 if (ctx->default_cert == NULL)
474 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE,ERR_R_MALLOC_FAILURE);
481 return(ssl_set_cert(c,x));
484 static int ssl_set_cert(c,x)
491 pkey=X509_get_pubkey(x);
494 SSLerr(SSL_F_SSL_SET_CERT,SSL_R_X509_LIB);
498 i=ssl_cert_type(x,pkey);
501 SSLerr(SSL_F_SSL_SET_CERT,SSL_R_UNKNOWN_CERTIFICATE_TYPE);
505 if (c->pkeys[i].privatekey != NULL)
507 if (!X509_check_private_key(x,c->pkeys[i].privatekey))
509 if ((i == SSL_PKEY_DH_RSA) || (i == SSL_PKEY_DH_DSA))
511 i=(i == SSL_PKEY_DH_RSA)?
512 SSL_PKEY_DH_DSA:SSL_PKEY_DH_RSA;
514 if (c->pkeys[i].privatekey == NULL)
518 if (!X509_check_private_key(x,
519 c->pkeys[i].privatekey))
536 EVP_PKEY_free(c->pkeys[i].privatekey);
537 c->pkeys[i].privatekey=NULL;
540 if (c->pkeys[i].x509 != NULL)
541 X509_free(c->pkeys[i].x509);
542 CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
544 c->key= &(c->pkeys[i]);
551 int SSL_CTX_use_certificate_file(ctx, file, type)
561 in=BIO_new(BIO_s_file_internal());
564 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,ERR_R_BUF_LIB);
568 if (BIO_read_filename(in,file) <= 0)
570 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,ERR_R_SYS_LIB);
573 if (type == SSL_FILETYPE_ASN1)
576 x=d2i_X509_bio(in,NULL);
578 else if (type == SSL_FILETYPE_PEM)
581 x=PEM_read_bio_X509(in,NULL,ctx->default_passwd_callback);
585 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,SSL_R_BAD_SSL_FILETYPE);
591 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,j);
595 ret=SSL_CTX_use_certificate(ctx,x);
597 if (x != NULL) X509_free(x);
598 if (in != NULL) BIO_free(in);
603 int SSL_CTX_use_certificate_ASN1(ctx, len, d)
611 x=d2i_X509(NULL,&d,(long)len);
614 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1,ERR_R_ASN1_LIB);
618 ret=SSL_CTX_use_certificate(ctx,x);
624 int SSL_CTX_use_RSAPrivateKey(ctx, rsa)
634 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
637 if (ctx->default_cert == NULL)
642 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY,ERR_R_MALLOC_FAILURE);
649 if ((pkey=EVP_PKEY_new()) == NULL)
651 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY,ERR_R_EVP_LIB);
655 CRYPTO_add(&rsa->references,1,CRYPTO_LOCK_RSA);
656 EVP_PKEY_assign_RSA(pkey,rsa);
658 ret=ssl_set_pkey(c,pkey);
664 int SSL_CTX_use_RSAPrivateKey_file(ctx, file, type)
673 in=BIO_new(BIO_s_file_internal());
676 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,ERR_R_BUF_LIB);
680 if (BIO_read_filename(in,file) <= 0)
682 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,ERR_R_SYS_LIB);
685 if (type == SSL_FILETYPE_ASN1)
688 rsa=d2i_RSAPrivateKey_bio(in,NULL);
690 else if (type == SSL_FILETYPE_PEM)
693 rsa=PEM_read_bio_RSAPrivateKey(in,NULL,
694 ctx->default_passwd_callback);
698 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
703 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,j);
706 ret=SSL_CTX_use_RSAPrivateKey(ctx,rsa);
709 if (in != NULL) BIO_free(in);
714 int SSL_CTX_use_RSAPrivateKey_ASN1(ctx,d,len)
724 if ((rsa=d2i_RSAPrivateKey(NULL,&p,(long)len)) == NULL)
726 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
730 ret=SSL_CTX_use_RSAPrivateKey(ctx,rsa);
736 int SSL_CTX_use_PrivateKey(ctx, pkey)
744 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
748 if (ctx->default_cert == NULL)
753 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY,ERR_R_MALLOC_FAILURE);
760 return(ssl_set_pkey(c,pkey));
764 int SSL_CTX_use_PrivateKey_file(ctx, file, type)
773 in=BIO_new(BIO_s_file_internal());
776 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,ERR_R_BUF_LIB);
780 if (BIO_read_filename(in,file) <= 0)
782 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,ERR_R_SYS_LIB);
785 if (type == SSL_FILETYPE_PEM)
788 pkey=PEM_read_bio_PrivateKey(in,NULL,
789 ctx->default_passwd_callback);
793 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
798 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,j);
801 ret=SSL_CTX_use_PrivateKey(ctx,pkey);
804 if (in != NULL) BIO_free(in);
809 int SSL_CTX_use_PrivateKey_ASN1(type,ctx,d,len)
820 if ((pkey=d2i_PrivateKey(type,NULL,&p,(long)len)) == NULL)
822 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
826 ret=SSL_CTX_use_PrivateKey(ctx,pkey);