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.]
61 #include <openssl/bio.h>
62 #include <openssl/objects.h>
63 #include <openssl/evp.h>
64 #include <openssl/x509.h>
65 #include <openssl/pem.h>
67 static int ssl_set_cert(CERT *c, X509 *x509);
68 static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey);
69 int SSL_use_certificate(SSL *ssl, X509 *x)
73 SSLerr(SSL_F_SSL_USE_CERTIFICATE,ERR_R_PASSED_NULL_PARAMETER);
76 if (!ssl_cert_inst(&ssl->cert))
78 SSLerr(SSL_F_SSL_USE_CERTIFICATE,ERR_R_MALLOC_FAILURE);
81 return(ssl_set_cert(ssl->cert,x));
84 #ifndef OPENSSL_NO_STDIO
85 int SSL_use_certificate_file(SSL *ssl, const char *file, int type)
92 in=BIO_new(BIO_s_file_internal());
95 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE,ERR_R_BUF_LIB);
99 if (BIO_read_filename(in,file) <= 0)
101 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE,ERR_R_SYS_LIB);
104 if (type == SSL_FILETYPE_ASN1)
107 x=d2i_X509_bio(in,NULL);
109 else if (type == SSL_FILETYPE_PEM)
112 x=PEM_read_bio_X509(in,NULL,ssl->ctx->default_passwd_callback,ssl->ctx->default_passwd_callback_userdata);
116 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE,SSL_R_BAD_SSL_FILETYPE);
122 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE,j);
126 ret=SSL_use_certificate(ssl,x);
128 if (x != NULL) X509_free(x);
129 if (in != NULL) BIO_free(in);
134 int SSL_use_certificate_ASN1(SSL *ssl, const unsigned char *d, int len)
139 x=d2i_X509(NULL,&d,(long)len);
142 SSLerr(SSL_F_SSL_USE_CERTIFICATE_ASN1,ERR_R_ASN1_LIB);
146 ret=SSL_use_certificate(ssl,x);
151 #ifndef OPENSSL_NO_RSA
152 int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa)
159 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
162 if (!ssl_cert_inst(&ssl->cert))
164 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY,ERR_R_MALLOC_FAILURE);
167 if ((pkey=EVP_PKEY_new()) == NULL)
169 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY,ERR_R_EVP_LIB);
174 EVP_PKEY_assign_RSA(pkey,rsa);
176 ret=ssl_set_pkey(ssl->cert,pkey);
182 static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey)
186 i=ssl_cert_type(NULL,pkey);
189 SSLerr(SSL_F_SSL_SET_PKEY,SSL_R_UNKNOWN_CERTIFICATE_TYPE);
193 if (c->pkeys[i].x509 != NULL)
196 pktmp = X509_get_pubkey(c->pkeys[i].x509);
197 EVP_PKEY_copy_parameters(pktmp,pkey);
198 EVP_PKEY_free(pktmp);
201 #ifndef OPENSSL_NO_RSA
202 /* Don't check the public/private key, this is mostly
203 * for smart cards. */
204 if ((pkey->type == EVP_PKEY_RSA) &&
205 (RSA_flags(pkey->pkey.rsa) &
206 RSA_METHOD_FLAG_NO_CHECK))
210 if (!X509_check_private_key(c->pkeys[i].x509,pkey))
212 if ((i == SSL_PKEY_DH_RSA) || (i == SSL_PKEY_DH_DSA))
214 i=(i == SSL_PKEY_DH_RSA)?
215 SSL_PKEY_DH_DSA:SSL_PKEY_DH_RSA;
217 if (c->pkeys[i].x509 == NULL)
221 if (!X509_check_private_key(
222 c->pkeys[i].x509,pkey))
239 X509_free(c->pkeys[i].x509);
240 c->pkeys[i].x509=NULL;
244 ERR_clear_error(); /* make sure no error from X509_check_private_key()
245 * is left if we have chosen to ignore it */
246 if (c->pkeys[i].privatekey != NULL)
247 EVP_PKEY_free(c->pkeys[i].privatekey);
248 CRYPTO_add(&pkey->references,1,CRYPTO_LOCK_EVP_PKEY);
249 c->pkeys[i].privatekey=pkey;
250 c->key= &(c->pkeys[i]);
256 #ifndef OPENSSL_NO_RSA
257 #ifndef OPENSSL_NO_STDIO
258 int SSL_use_RSAPrivateKey_file(SSL *ssl, const char *file, int type)
264 in=BIO_new(BIO_s_file_internal());
267 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,ERR_R_BUF_LIB);
271 if (BIO_read_filename(in,file) <= 0)
273 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,ERR_R_SYS_LIB);
276 if (type == SSL_FILETYPE_ASN1)
279 rsa=d2i_RSAPrivateKey_bio(in,NULL);
281 else if (type == SSL_FILETYPE_PEM)
284 rsa=PEM_read_bio_RSAPrivateKey(in,NULL,
285 ssl->ctx->default_passwd_callback,ssl->ctx->default_passwd_callback_userdata);
289 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
294 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,j);
297 ret=SSL_use_RSAPrivateKey(ssl,rsa);
300 if (in != NULL) BIO_free(in);
305 int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, unsigned char *d, long len)
308 const unsigned char *p;
312 if ((rsa=d2i_RSAPrivateKey(NULL,&p,(long)len)) == NULL)
314 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
318 ret=SSL_use_RSAPrivateKey(ssl,rsa);
322 #endif /* !OPENSSL_NO_RSA */
324 int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey)
330 SSLerr(SSL_F_SSL_USE_PRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
333 if (!ssl_cert_inst(&ssl->cert))
335 SSLerr(SSL_F_SSL_USE_PRIVATEKEY,ERR_R_MALLOC_FAILURE);
338 ret=ssl_set_pkey(ssl->cert,pkey);
342 #ifndef OPENSSL_NO_STDIO
343 int SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type)
349 in=BIO_new(BIO_s_file_internal());
352 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,ERR_R_BUF_LIB);
356 if (BIO_read_filename(in,file) <= 0)
358 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,ERR_R_SYS_LIB);
361 if (type == SSL_FILETYPE_PEM)
364 pkey=PEM_read_bio_PrivateKey(in,NULL,
365 ssl->ctx->default_passwd_callback,ssl->ctx->default_passwd_callback_userdata);
369 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
374 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,j);
377 ret=SSL_use_PrivateKey(ssl,pkey);
380 if (in != NULL) BIO_free(in);
385 int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const unsigned char *d, long len)
388 const unsigned char *p;
392 if ((pkey=d2i_PrivateKey(type,NULL,&p,(long)len)) == NULL)
394 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
398 ret=SSL_use_PrivateKey(ssl,pkey);
403 int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x)
407 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE,ERR_R_PASSED_NULL_PARAMETER);
410 if (!ssl_cert_inst(&ctx->cert))
412 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE,ERR_R_MALLOC_FAILURE);
415 return(ssl_set_cert(ctx->cert, x));
418 static int ssl_set_cert(CERT *c, X509 *x)
423 pkey=X509_get_pubkey(x);
426 SSLerr(SSL_F_SSL_SET_CERT,SSL_R_X509_LIB);
430 i=ssl_cert_type(x,pkey);
433 SSLerr(SSL_F_SSL_SET_CERT,SSL_R_UNKNOWN_CERTIFICATE_TYPE);
438 if (c->pkeys[i].privatekey != NULL)
440 EVP_PKEY_copy_parameters(pkey,c->pkeys[i].privatekey);
443 #ifndef OPENSSL_NO_RSA
444 /* Don't check the public/private key, this is mostly
445 * for smart cards. */
446 if ((c->pkeys[i].privatekey->type == EVP_PKEY_RSA) &&
447 (RSA_flags(c->pkeys[i].privatekey->pkey.rsa) &
448 RSA_METHOD_FLAG_NO_CHECK))
453 if (!X509_check_private_key(x,c->pkeys[i].privatekey))
455 if ((i == SSL_PKEY_DH_RSA) || (i == SSL_PKEY_DH_DSA))
457 i=(i == SSL_PKEY_DH_RSA)?
458 SSL_PKEY_DH_DSA:SSL_PKEY_DH_RSA;
460 if (c->pkeys[i].privatekey == NULL)
464 if (!X509_check_private_key(x,
465 c->pkeys[i].privatekey))
476 } /* OPENSSL_NO_RSA */
484 EVP_PKEY_free(c->pkeys[i].privatekey);
485 c->pkeys[i].privatekey=NULL;
488 if (c->pkeys[i].x509 != NULL)
489 X509_free(c->pkeys[i].x509);
490 CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
492 c->key= &(c->pkeys[i]);
498 #ifndef OPENSSL_NO_STDIO
499 int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type)
506 in=BIO_new(BIO_s_file_internal());
509 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,ERR_R_BUF_LIB);
513 if (BIO_read_filename(in,file) <= 0)
515 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,ERR_R_SYS_LIB);
518 if (type == SSL_FILETYPE_ASN1)
521 x=d2i_X509_bio(in,NULL);
523 else if (type == SSL_FILETYPE_PEM)
526 x=PEM_read_bio_X509(in,NULL,ctx->default_passwd_callback,ctx->default_passwd_callback_userdata);
530 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,SSL_R_BAD_SSL_FILETYPE);
536 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,j);
540 ret=SSL_CTX_use_certificate(ctx,x);
542 if (x != NULL) X509_free(x);
543 if (in != NULL) BIO_free(in);
548 int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len, const unsigned char *d)
553 x=d2i_X509(NULL,&d,(long)len);
556 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1,ERR_R_ASN1_LIB);
560 ret=SSL_CTX_use_certificate(ctx,x);
565 #ifndef OPENSSL_NO_RSA
566 int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa)
573 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
576 if (!ssl_cert_inst(&ctx->cert))
578 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY,ERR_R_MALLOC_FAILURE);
581 if ((pkey=EVP_PKEY_new()) == NULL)
583 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY,ERR_R_EVP_LIB);
588 EVP_PKEY_assign_RSA(pkey,rsa);
590 ret=ssl_set_pkey(ctx->cert, pkey);
595 #ifndef OPENSSL_NO_STDIO
596 int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, int type)
602 in=BIO_new(BIO_s_file_internal());
605 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,ERR_R_BUF_LIB);
609 if (BIO_read_filename(in,file) <= 0)
611 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,ERR_R_SYS_LIB);
614 if (type == SSL_FILETYPE_ASN1)
617 rsa=d2i_RSAPrivateKey_bio(in,NULL);
619 else if (type == SSL_FILETYPE_PEM)
622 rsa=PEM_read_bio_RSAPrivateKey(in,NULL,
623 ctx->default_passwd_callback,ctx->default_passwd_callback_userdata);
627 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
632 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,j);
635 ret=SSL_CTX_use_RSAPrivateKey(ctx,rsa);
638 if (in != NULL) BIO_free(in);
643 int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const unsigned char *d, long len)
646 const unsigned char *p;
650 if ((rsa=d2i_RSAPrivateKey(NULL,&p,(long)len)) == NULL)
652 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
656 ret=SSL_CTX_use_RSAPrivateKey(ctx,rsa);
660 #endif /* !OPENSSL_NO_RSA */
662 int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey)
666 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
669 if (!ssl_cert_inst(&ctx->cert))
671 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY,ERR_R_MALLOC_FAILURE);
674 return(ssl_set_pkey(ctx->cert,pkey));
677 #ifndef OPENSSL_NO_STDIO
678 int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
684 in=BIO_new(BIO_s_file_internal());
687 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,ERR_R_BUF_LIB);
691 if (BIO_read_filename(in,file) <= 0)
693 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,ERR_R_SYS_LIB);
696 if (type == SSL_FILETYPE_PEM)
699 pkey=PEM_read_bio_PrivateKey(in,NULL,
700 ctx->default_passwd_callback,ctx->default_passwd_callback_userdata);
704 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
709 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,j);
712 ret=SSL_CTX_use_PrivateKey(ctx,pkey);
715 if (in != NULL) BIO_free(in);
720 int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx, const unsigned char *d,
724 const unsigned char *p;
728 if ((pkey=d2i_PrivateKey(type,NULL,&p,(long)len)) == NULL)
730 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
734 ret=SSL_CTX_use_PrivateKey(ctx,pkey);
740 #ifndef OPENSSL_NO_STDIO
741 /* Read a file that contains our certificate in "PEM" format,
742 * possibly followed by a sequence of CA certificates that should be
743 * sent to the peer in the Certificate message.
745 int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file)
751 in=BIO_new(BIO_s_file_internal());
754 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE,ERR_R_BUF_LIB);
758 if (BIO_read_filename(in,file) <= 0)
760 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE,ERR_R_SYS_LIB);
764 x=PEM_read_bio_X509(in,NULL,ctx->default_passwd_callback,ctx->default_passwd_callback_userdata);
767 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE,ERR_R_PEM_LIB);
771 ret=SSL_CTX_use_certificate(ctx,x);
772 if (ERR_peek_error() != 0)
773 ret = 0; /* Key/certificate mismatch doesn't imply ret==0 ... */
776 /* If we could set up our certificate, now proceed to
777 * the CA certificates.
783 if (ctx->extra_certs != NULL)
785 sk_X509_pop_free(ctx->extra_certs, X509_free);
786 ctx->extra_certs = NULL;
789 while ((ca = PEM_read_bio_X509(in,NULL,ctx->default_passwd_callback,ctx->default_passwd_callback_userdata))
792 r = SSL_CTX_add_extra_chain_cert(ctx, ca);
799 /* Note that we must not free r if it was successfully
800 * added to the chain (while we must free the main
801 * certificate, since its reference count is increased
802 * by SSL_CTX_use_certificate). */
804 /* When the while loop ends, it's usually just EOF. */
805 err = ERR_peek_last_error();
806 if (ERR_GET_LIB(err) == ERR_LIB_PEM && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)
807 (void)ERR_get_error();
809 ret = 0; /* some real error */
813 if (x != NULL) X509_free(x);
814 if (in != NULL) BIO_free(in);