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)
232 EVP_PKEY_copy_parameters(
233 X509_get_pubkey(c->pkeys[i].x509),pkey);
237 /* Don't check the public/private key, this is mostly
238 * for smart cards. */
239 if ((pkey->type == EVP_PKEY_RSA) &&
240 (RSA_flags(pkey->pkey.rsa) &
241 RSA_METHOD_FLAG_NO_CHECK))
245 if (!X509_check_private_key(c->pkeys[i].x509,pkey))
247 if ((i == SSL_PKEY_DH_RSA) || (i == SSL_PKEY_DH_DSA))
249 i=(i == SSL_PKEY_DH_RSA)?
250 SSL_PKEY_DH_DSA:SSL_PKEY_DH_RSA;
252 if (c->pkeys[i].x509 == NULL)
256 if (!X509_check_private_key(
257 c->pkeys[i].x509,pkey))
274 X509_free(c->pkeys[i].x509);
275 c->pkeys[i].x509=NULL;
279 if (c->pkeys[i].privatekey != NULL)
280 EVP_PKEY_free(c->pkeys[i].privatekey);
281 CRYPTO_add(&pkey->references,1,CRYPTO_LOCK_EVP_PKEY);
282 c->pkeys[i].privatekey=pkey;
283 c->key= &(c->pkeys[i]);
291 int SSL_use_RSAPrivateKey_file(ssl, file, type)
300 in=BIO_new(BIO_s_file_internal());
303 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,ERR_R_BUF_LIB);
307 if (BIO_read_filename(in,file) <= 0)
309 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,ERR_R_SYS_LIB);
312 if (type == SSL_FILETYPE_ASN1)
315 rsa=d2i_RSAPrivateKey_bio(in,NULL);
317 else if (type == SSL_FILETYPE_PEM)
320 rsa=PEM_read_bio_RSAPrivateKey(in,NULL,
321 ssl->ctx->default_passwd_callback);
325 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
330 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,j);
333 ret=SSL_use_RSAPrivateKey(ssl,rsa);
336 if (in != NULL) BIO_free(in);
341 int SSL_use_RSAPrivateKey_ASN1(ssl,d,len)
351 if ((rsa=d2i_RSAPrivateKey(NULL,&p,(long)len)) == NULL)
353 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
357 ret=SSL_use_RSAPrivateKey(ssl,rsa);
363 int SSL_use_PrivateKey(ssl, pkey)
372 SSLerr(SSL_F_SSL_USE_PRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
376 if ((ssl->cert == NULL) || (ssl->cert == ssl->ctx->default_cert))
381 SSLerr(SSL_F_SSL_USE_PRIVATEKEY,ERR_R_MALLOC_FAILURE);
384 if (ssl->cert != NULL) ssl_cert_free(ssl->cert);
389 ret=ssl_set_pkey(c,pkey);
394 int SSL_use_PrivateKey_file(ssl, file, type)
403 in=BIO_new(BIO_s_file_internal());
406 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,ERR_R_BUF_LIB);
410 if (BIO_read_filename(in,file) <= 0)
412 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,ERR_R_SYS_LIB);
415 if (type == SSL_FILETYPE_PEM)
418 pkey=PEM_read_bio_PrivateKey(in,NULL,
419 ssl->ctx->default_passwd_callback);
423 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
428 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,j);
431 ret=SSL_use_PrivateKey(ssl,pkey);
434 if (in != NULL) BIO_free(in);
439 int SSL_use_PrivateKey_ASN1(type,ssl,d,len)
450 if ((pkey=d2i_PrivateKey(type,NULL,&p,(long)len)) == NULL)
452 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
456 ret=SSL_use_PrivateKey(ssl,pkey);
461 int SSL_CTX_use_certificate(ctx, x)
469 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE,ERR_R_PASSED_NULL_PARAMETER);
473 if (ctx->default_cert == NULL)
478 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE,ERR_R_MALLOC_FAILURE);
485 return(ssl_set_cert(c,x));
488 static int ssl_set_cert(c,x)
495 pkey=X509_get_pubkey(x);
498 SSLerr(SSL_F_SSL_SET_CERT,SSL_R_X509_LIB);
502 i=ssl_cert_type(x,pkey);
505 SSLerr(SSL_F_SSL_SET_CERT,SSL_R_UNKNOWN_CERTIFICATE_TYPE);
509 if (c->pkeys[i].privatekey != NULL)
511 EVP_PKEY_copy_parameters(pkey,c->pkeys[i].privatekey);
515 /* Don't check the public/private key, this is mostly
516 * for smart cards. */
517 if ((c->pkeys[i].privatekey->type == EVP_PKEY_RSA) &&
518 (RSA_flags(c->pkeys[i].privatekey->pkey.rsa) &
519 RSA_METHOD_FLAG_NO_CHECK))
524 if (!X509_check_private_key(x,c->pkeys[i].privatekey))
526 if ((i == SSL_PKEY_DH_RSA) || (i == SSL_PKEY_DH_DSA))
528 i=(i == SSL_PKEY_DH_RSA)?
529 SSL_PKEY_DH_DSA:SSL_PKEY_DH_RSA;
531 if (c->pkeys[i].privatekey == NULL)
535 if (!X509_check_private_key(x,
536 c->pkeys[i].privatekey))
554 EVP_PKEY_free(c->pkeys[i].privatekey);
555 c->pkeys[i].privatekey=NULL;
558 if (c->pkeys[i].x509 != NULL)
559 X509_free(c->pkeys[i].x509);
560 CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
562 c->key= &(c->pkeys[i]);
569 int SSL_CTX_use_certificate_file(ctx, file, type)
579 in=BIO_new(BIO_s_file_internal());
582 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,ERR_R_BUF_LIB);
586 if (BIO_read_filename(in,file) <= 0)
588 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,ERR_R_SYS_LIB);
591 if (type == SSL_FILETYPE_ASN1)
594 x=d2i_X509_bio(in,NULL);
596 else if (type == SSL_FILETYPE_PEM)
599 x=PEM_read_bio_X509(in,NULL,ctx->default_passwd_callback);
603 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,SSL_R_BAD_SSL_FILETYPE);
609 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,j);
613 ret=SSL_CTX_use_certificate(ctx,x);
615 if (x != NULL) X509_free(x);
616 if (in != NULL) BIO_free(in);
621 int SSL_CTX_use_certificate_ASN1(ctx, len, d)
629 x=d2i_X509(NULL,&d,(long)len);
632 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1,ERR_R_ASN1_LIB);
636 ret=SSL_CTX_use_certificate(ctx,x);
642 int SSL_CTX_use_RSAPrivateKey(ctx, rsa)
652 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
655 if (ctx->default_cert == NULL)
660 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY,ERR_R_MALLOC_FAILURE);
667 if ((pkey=EVP_PKEY_new()) == NULL)
669 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY,ERR_R_EVP_LIB);
673 CRYPTO_add(&rsa->references,1,CRYPTO_LOCK_RSA);
674 EVP_PKEY_assign_RSA(pkey,rsa);
676 ret=ssl_set_pkey(c,pkey);
682 int SSL_CTX_use_RSAPrivateKey_file(ctx, file, type)
691 in=BIO_new(BIO_s_file_internal());
694 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,ERR_R_BUF_LIB);
698 if (BIO_read_filename(in,file) <= 0)
700 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,ERR_R_SYS_LIB);
703 if (type == SSL_FILETYPE_ASN1)
706 rsa=d2i_RSAPrivateKey_bio(in,NULL);
708 else if (type == SSL_FILETYPE_PEM)
711 rsa=PEM_read_bio_RSAPrivateKey(in,NULL,
712 ctx->default_passwd_callback);
716 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
721 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,j);
724 ret=SSL_CTX_use_RSAPrivateKey(ctx,rsa);
727 if (in != NULL) BIO_free(in);
732 int SSL_CTX_use_RSAPrivateKey_ASN1(ctx,d,len)
742 if ((rsa=d2i_RSAPrivateKey(NULL,&p,(long)len)) == NULL)
744 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
748 ret=SSL_CTX_use_RSAPrivateKey(ctx,rsa);
754 int SSL_CTX_use_PrivateKey(ctx, pkey)
762 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
766 if (ctx->default_cert == NULL)
771 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY,ERR_R_MALLOC_FAILURE);
778 return(ssl_set_pkey(c,pkey));
782 int SSL_CTX_use_PrivateKey_file(ctx, file, type)
791 in=BIO_new(BIO_s_file_internal());
794 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,ERR_R_BUF_LIB);
798 if (BIO_read_filename(in,file) <= 0)
800 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,ERR_R_SYS_LIB);
803 if (type == SSL_FILETYPE_PEM)
806 pkey=PEM_read_bio_PrivateKey(in,NULL,
807 ctx->default_passwd_callback);
811 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
816 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,j);
819 ret=SSL_CTX_use_PrivateKey(ctx,pkey);
822 if (in != NULL) BIO_free(in);
827 int SSL_CTX_use_PrivateKey_ASN1(type,ctx,d,len)
838 if ((pkey=d2i_PrivateKey(type,NULL,&p,(long)len)) == NULL)
840 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
844 ret=SSL_CTX_use_PrivateKey(ctx,pkey);