2 /* Copyright (C) 1995-1997 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));
102 int SSL_use_certificate_file(ssl, file, type)
113 in=BIO_new(BIO_s_file_internal_w16());
115 in=BIO_new(BIO_s_file());
119 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE,ERR_R_BUF_LIB);
123 if (BIO_read_filename(in,file) <= 0)
125 SYSerr(SYS_F_FOPEN,errno);
126 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE,ERR_R_SYS_LIB);
129 if (type == SSL_FILETYPE_ASN1)
132 x=d2i_X509_bio(in,NULL);
134 else if (type == SSL_FILETYPE_PEM)
137 x=PEM_read_bio_X509(in,NULL,ssl->ctx->default_passwd_callback);
141 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE,SSL_R_BAD_SSL_FILETYPE);
147 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE,j);
151 ret=SSL_use_certificate(ssl,x);
153 if (x != NULL) X509_free(x);
154 if (in != NULL) BIO_free(in);
158 int SSL_use_certificate_ASN1(ssl, len, d)
166 x=d2i_X509(NULL,&d,(long)len);
169 SSLerr(SSL_F_SSL_USE_CERTIFICATE_ASN1,ERR_R_ASN1_LIB);
173 ret=SSL_use_certificate(ssl,x);
179 int SSL_use_RSAPrivateKey(ssl, rsa)
189 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
193 if ((ssl->cert == NULL) || (ssl->cert == ssl->ctx->default_cert))
198 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY,ERR_R_MALLOC_FAILURE);
201 if (ssl->cert != NULL) ssl_cert_free(ssl->cert);
205 if ((pkey=EVP_PKEY_new()) == NULL)
207 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY,ERR_R_EVP_LIB);
211 CRYPTO_add(&rsa->references,1,CRYPTO_LOCK_RSA);
212 EVP_PKEY_assign_RSA(pkey,rsa);
214 ret=ssl_set_pkey(c,pkey);
220 static int ssl_set_pkey(c,pkey)
226 i=ssl_cert_type(NULL,pkey);
229 SSLerr(SSL_F_SSL_SET_PKEY,SSL_R_UNKNOWN_CERTIFICATE_TYPE);
233 if (c->pkeys[i].x509 != NULL)
235 if (!X509_check_private_key(c->pkeys[i].x509,pkey))
237 if ((i == SSL_PKEY_DH_RSA) || (i == SSL_PKEY_DH_DSA))
239 i=(i == SSL_PKEY_DH_RSA)?
240 SSL_PKEY_DH_DSA:SSL_PKEY_DH_RSA;
242 if (c->pkeys[i].x509 == NULL)
246 if (!X509_check_private_key(
247 c->pkeys[i].x509,pkey))
264 X509_free(c->pkeys[i].x509);
265 c->pkeys[i].x509=NULL;
269 if (c->pkeys[i].privatekey != NULL)
270 EVP_PKEY_free(c->pkeys[i].privatekey);
271 CRYPTO_add(&pkey->references,1,CRYPTO_LOCK_EVP_PKEY);
272 c->pkeys[i].privatekey=pkey;
273 c->key= &(c->pkeys[i]);
280 int SSL_use_RSAPrivateKey_file(ssl, file, type)
290 in=BIO_new(BIO_s_file_internal_w16());
292 in=BIO_new(BIO_s_file());
296 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,ERR_R_BUF_LIB);
300 if (BIO_read_filename(in,file) <= 0)
302 SYSerr(SYS_F_FOPEN,errno);
303 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,ERR_R_SYS_LIB);
306 if (type == SSL_FILETYPE_ASN1)
309 rsa=d2i_RSAPrivateKey_bio(in,NULL);
311 else if (type == SSL_FILETYPE_PEM)
314 rsa=PEM_read_bio_RSAPrivateKey(in,NULL,
315 ssl->ctx->default_passwd_callback);
319 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
324 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,j);
327 ret=SSL_use_RSAPrivateKey(ssl,rsa);
330 if (in != NULL) BIO_free(in);
334 int SSL_use_RSAPrivateKey_ASN1(ssl,d,len)
344 if ((rsa=d2i_RSAPrivateKey(NULL,&p,(long)len)) == NULL)
346 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
350 ret=SSL_use_RSAPrivateKey(ssl,rsa);
356 int SSL_use_PrivateKey(ssl, pkey)
365 SSLerr(SSL_F_SSL_USE_PRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
369 if ((ssl->cert == NULL) || (ssl->cert == ssl->ctx->default_cert))
374 SSLerr(SSL_F_SSL_USE_PRIVATEKEY,ERR_R_MALLOC_FAILURE);
377 if (ssl->cert != NULL) ssl_cert_free(ssl->cert);
382 ret=ssl_set_pkey(c,pkey);
386 int SSL_use_PrivateKey_file(ssl, file, type)
396 in=BIO_new(BIO_s_file_internal_w16());
398 in=BIO_new(BIO_s_file());
402 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,ERR_R_BUF_LIB);
406 if (BIO_read_filename(in,file) <= 0)
408 SYSerr(SYS_F_FOPEN,errno);
409 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,ERR_R_SYS_LIB);
412 if (type == SSL_FILETYPE_PEM)
415 pkey=PEM_read_bio_PrivateKey(in,NULL,
416 ssl->ctx->default_passwd_callback);
420 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
425 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,j);
428 ret=SSL_use_PrivateKey(ssl,pkey);
431 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_PKEY,SSL_R_X509_LIB);
498 i=ssl_cert_type(x,pkey);
501 SSLerr(SSL_F_SSL_SET_PKEY,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]);
550 int SSL_CTX_use_certificate_file(ctx, file, type)
561 in=BIO_new(BIO_s_file_internal_w16());
563 in=BIO_new(BIO_s_file());
567 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,ERR_R_BUF_LIB);
571 if (BIO_read_filename(in,file) <= 0)
573 SYSerr(SYS_F_FOPEN,errno);
574 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,ERR_R_SYS_LIB);
577 if (type == SSL_FILETYPE_ASN1)
580 x=d2i_X509_bio(in,NULL);
582 else if (type == SSL_FILETYPE_PEM)
585 x=PEM_read_bio_X509(in,NULL,ctx->default_passwd_callback);
589 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,SSL_R_BAD_SSL_FILETYPE);
595 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,j);
599 ret=SSL_CTX_use_certificate(ctx,x);
601 if (x != NULL) X509_free(x);
602 if (in != NULL) BIO_free(in);
606 int SSL_CTX_use_certificate_ASN1(ctx, len, d)
614 x=d2i_X509(NULL,&d,(long)len);
617 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1,ERR_R_ASN1_LIB);
621 ret=SSL_CTX_use_certificate(ctx,x);
627 int SSL_CTX_use_RSAPrivateKey(ctx, rsa)
637 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
640 if (ctx->default_cert == NULL)
645 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY,ERR_R_MALLOC_FAILURE);
652 if ((pkey=EVP_PKEY_new()) == NULL)
654 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY,ERR_R_EVP_LIB);
658 CRYPTO_add(&rsa->references,1,CRYPTO_LOCK_RSA);
659 EVP_PKEY_assign_RSA(pkey,rsa);
661 ret=ssl_set_pkey(c,pkey);
666 int SSL_CTX_use_RSAPrivateKey_file(ctx, file, type)
676 in=BIO_new(BIO_s_file_internal_w16());
678 in=BIO_new(BIO_s_file());
682 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,ERR_R_BUF_LIB);
686 if (BIO_read_filename(in,file) <= 0)
688 SYSerr(SYS_F_FOPEN,errno);
689 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,ERR_R_SYS_LIB);
692 if (type == SSL_FILETYPE_ASN1)
695 rsa=d2i_RSAPrivateKey_bio(in,NULL);
697 else if (type == SSL_FILETYPE_PEM)
700 rsa=PEM_read_bio_RSAPrivateKey(in,NULL,
701 ctx->default_passwd_callback);
705 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
710 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,j);
713 ret=SSL_CTX_use_RSAPrivateKey(ctx,rsa);
716 if (in != NULL) BIO_free(in);
720 int SSL_CTX_use_RSAPrivateKey_ASN1(ctx,d,len)
730 if ((rsa=d2i_RSAPrivateKey(NULL,&p,(long)len)) == NULL)
732 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
736 ret=SSL_CTX_use_RSAPrivateKey(ctx,rsa);
742 int SSL_CTX_use_PrivateKey(ctx, pkey)
750 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
754 if (ctx->default_cert == NULL)
759 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY,ERR_R_MALLOC_FAILURE);
766 return(ssl_set_pkey(c,pkey));
769 int SSL_CTX_use_PrivateKey_file(ctx, file, type)
779 in=BIO_new(BIO_s_file_internal_w16());
781 in=BIO_new(BIO_s_file());
785 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,ERR_R_BUF_LIB);
789 if (BIO_read_filename(in,file) <= 0)
791 SYSerr(SYS_F_FOPEN,errno);
792 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,ERR_R_SYS_LIB);
795 if (type == SSL_FILETYPE_PEM)
798 pkey=PEM_read_bio_PrivateKey(in,NULL,
799 ctx->default_passwd_callback);
803 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
808 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,j);
811 ret=SSL_CTX_use_PrivateKey(ctx,pkey);
814 if (in != NULL) BIO_free(in);
818 int SSL_CTX_use_PrivateKey_ASN1(type,ctx,d,len)
829 if ((pkey=d2i_PrivateKey(type,NULL,&p,(long)len)) == NULL)
831 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
835 ret=SSL_CTX_use_PrivateKey(ctx,pkey);