get rid of very buggy and very imcomplete DH cert support
[openssl.git] / ssl / ssl_rsa.c
1 /* ssl/ssl_rsa.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
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.
8  * 
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).
15  * 
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.
22  * 
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
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)"
40  * 
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
51  * SUCH DAMAGE.
52  * 
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.]
57  */
58
59 #include <stdio.h>
60 #include "ssl_locl.h"
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>
66
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)
70         {
71         if (x == NULL)
72                 {
73                 SSLerr(SSL_F_SSL_USE_CERTIFICATE,ERR_R_PASSED_NULL_PARAMETER);
74                 return(0);
75                 }
76         if (!ssl_cert_inst(&ssl->cert))
77                 {
78                 SSLerr(SSL_F_SSL_USE_CERTIFICATE,ERR_R_MALLOC_FAILURE);
79                 return(0);
80                 }
81         return(ssl_set_cert(ssl->cert,x));
82         }
83
84 #ifndef OPENSSL_NO_STDIO
85 int SSL_use_certificate_file(SSL *ssl, const char *file, int type)
86         {
87         int j;
88         BIO *in;
89         int ret=0;
90         X509 *x=NULL;
91
92         in=BIO_new(BIO_s_file_internal());
93         if (in == NULL)
94                 {
95                 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE,ERR_R_BUF_LIB);
96                 goto end;
97                 }
98
99         if (BIO_read_filename(in,file) <= 0)
100                 {
101                 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE,ERR_R_SYS_LIB);
102                 goto end;
103                 }
104         if (type == SSL_FILETYPE_ASN1)
105                 {
106                 j=ERR_R_ASN1_LIB;
107                 x=d2i_X509_bio(in,NULL);
108                 }
109         else if (type == SSL_FILETYPE_PEM)
110                 {
111                 j=ERR_R_PEM_LIB;
112                 x=PEM_read_bio_X509(in,NULL,ssl->ctx->default_passwd_callback,ssl->ctx->default_passwd_callback_userdata);
113                 }
114         else
115                 {
116                 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE,SSL_R_BAD_SSL_FILETYPE);
117                 goto end;
118                 }
119
120         if (x == NULL)
121                 {
122                 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE,j);
123                 goto end;
124                 }
125
126         ret=SSL_use_certificate(ssl,x);
127 end:
128         if (x != NULL) X509_free(x);
129         if (in != NULL) BIO_free(in);
130         return(ret);
131         }
132 #endif
133
134 int SSL_use_certificate_ASN1(SSL *ssl, const unsigned char *d, int len)
135         {
136         X509 *x;
137         int ret;
138
139         x=d2i_X509(NULL,&d,(long)len);
140         if (x == NULL)
141                 {
142                 SSLerr(SSL_F_SSL_USE_CERTIFICATE_ASN1,ERR_R_ASN1_LIB);
143                 return(0);
144                 }
145
146         ret=SSL_use_certificate(ssl,x);
147         X509_free(x);
148         return(ret);
149         }
150
151 #ifndef OPENSSL_NO_RSA
152 int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa)
153         {
154         EVP_PKEY *pkey;
155         int ret;
156
157         if (rsa == NULL)
158                 {
159                 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
160                 return(0);
161                 }
162         if (!ssl_cert_inst(&ssl->cert))
163                 {
164                 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY,ERR_R_MALLOC_FAILURE);
165                 return(0);
166                 }
167         if ((pkey=EVP_PKEY_new()) == NULL)
168                 {
169                 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY,ERR_R_EVP_LIB);
170                 return(0);
171                 }
172
173         RSA_up_ref(rsa);
174         EVP_PKEY_assign_RSA(pkey,rsa);
175
176         ret=ssl_set_pkey(ssl->cert,pkey);
177         EVP_PKEY_free(pkey);
178         return(ret);
179         }
180 #endif
181
182 static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey)
183         {
184         int i;
185
186         i=ssl_cert_type(NULL,pkey);
187         if (i < 0)
188                 {
189                 SSLerr(SSL_F_SSL_SET_PKEY,SSL_R_UNKNOWN_CERTIFICATE_TYPE);
190                 return(0);
191                 }
192
193         if (c->pkeys[i].x509 != NULL)
194                 {
195                 EVP_PKEY *pktmp;
196                 pktmp = X509_get_pubkey(c->pkeys[i].x509);
197                 EVP_PKEY_copy_parameters(pktmp,pkey);
198                 EVP_PKEY_free(pktmp);
199                 ERR_clear_error();
200
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) & RSA_METHOD_FLAG_NO_CHECK))
206                         ;
207                 else
208 #endif
209                 if (!X509_check_private_key(c->pkeys[i].x509,pkey))
210                         {
211                         X509_free(c->pkeys[i].x509);
212                         c->pkeys[i].x509 = NULL;
213                         return 0;
214                         }
215                 }
216
217         if (c->pkeys[i].privatekey != NULL)
218                 EVP_PKEY_free(c->pkeys[i].privatekey);
219         CRYPTO_add(&pkey->references,1,CRYPTO_LOCK_EVP_PKEY);
220         c->pkeys[i].privatekey=pkey;
221         c->key= &(c->pkeys[i]);
222
223         c->valid=0;
224         return(1);
225         }
226
227 #ifndef OPENSSL_NO_RSA
228 #ifndef OPENSSL_NO_STDIO
229 int SSL_use_RSAPrivateKey_file(SSL *ssl, const char *file, int type)
230         {
231         int j,ret=0;
232         BIO *in;
233         RSA *rsa=NULL;
234
235         in=BIO_new(BIO_s_file_internal());
236         if (in == NULL)
237                 {
238                 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,ERR_R_BUF_LIB);
239                 goto end;
240                 }
241
242         if (BIO_read_filename(in,file) <= 0)
243                 {
244                 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,ERR_R_SYS_LIB);
245                 goto end;
246                 }
247         if      (type == SSL_FILETYPE_ASN1)
248                 {
249                 j=ERR_R_ASN1_LIB;
250                 rsa=d2i_RSAPrivateKey_bio(in,NULL);
251                 }
252         else if (type == SSL_FILETYPE_PEM)
253                 {
254                 j=ERR_R_PEM_LIB;
255                 rsa=PEM_read_bio_RSAPrivateKey(in,NULL,
256                         ssl->ctx->default_passwd_callback,ssl->ctx->default_passwd_callback_userdata);
257                 }
258         else
259                 {
260                 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
261                 goto end;
262                 }
263         if (rsa == NULL)
264                 {
265                 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,j);
266                 goto end;
267                 }
268         ret=SSL_use_RSAPrivateKey(ssl,rsa);
269         RSA_free(rsa);
270 end:
271         if (in != NULL) BIO_free(in);
272         return(ret);
273         }
274 #endif
275
276 int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, unsigned char *d, long len)
277         {
278         int ret;
279         const unsigned char *p;
280         RSA *rsa;
281
282         p=d;
283         if ((rsa=d2i_RSAPrivateKey(NULL,&p,(long)len)) == NULL)
284                 {
285                 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
286                 return(0);
287                 }
288
289         ret=SSL_use_RSAPrivateKey(ssl,rsa);
290         RSA_free(rsa);
291         return(ret);
292         }
293 #endif /* !OPENSSL_NO_RSA */
294
295 int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey)
296         {
297         int ret;
298
299         if (pkey == NULL)
300                 {
301                 SSLerr(SSL_F_SSL_USE_PRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
302                 return(0);
303                 }
304         if (!ssl_cert_inst(&ssl->cert))
305                 {
306                 SSLerr(SSL_F_SSL_USE_PRIVATEKEY,ERR_R_MALLOC_FAILURE);
307                 return(0);
308                 }
309         ret=ssl_set_pkey(ssl->cert,pkey);
310         return(ret);
311         }
312
313 #ifndef OPENSSL_NO_STDIO
314 int SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type)
315         {
316         int j,ret=0;
317         BIO *in;
318         EVP_PKEY *pkey=NULL;
319
320         in=BIO_new(BIO_s_file_internal());
321         if (in == NULL)
322                 {
323                 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,ERR_R_BUF_LIB);
324                 goto end;
325                 }
326
327         if (BIO_read_filename(in,file) <= 0)
328                 {
329                 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,ERR_R_SYS_LIB);
330                 goto end;
331                 }
332         if (type == SSL_FILETYPE_PEM)
333                 {
334                 j=ERR_R_PEM_LIB;
335                 pkey=PEM_read_bio_PrivateKey(in,NULL,
336                         ssl->ctx->default_passwd_callback,ssl->ctx->default_passwd_callback_userdata);
337                 }
338         else
339                 {
340                 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
341                 goto end;
342                 }
343         if (pkey == NULL)
344                 {
345                 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,j);
346                 goto end;
347                 }
348         ret=SSL_use_PrivateKey(ssl,pkey);
349         EVP_PKEY_free(pkey);
350 end:
351         if (in != NULL) BIO_free(in);
352         return(ret);
353         }
354 #endif
355
356 int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const unsigned char *d, long len)
357         {
358         int ret;
359         const unsigned char *p;
360         EVP_PKEY *pkey;
361
362         p=d;
363         if ((pkey=d2i_PrivateKey(type,NULL,&p,(long)len)) == NULL)
364                 {
365                 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
366                 return(0);
367                 }
368
369         ret=SSL_use_PrivateKey(ssl,pkey);
370         EVP_PKEY_free(pkey);
371         return(ret);
372         }
373
374 int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x)
375         {
376         if (x == NULL)
377                 {
378                 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE,ERR_R_PASSED_NULL_PARAMETER);
379                 return(0);
380                 }
381         if (!ssl_cert_inst(&ctx->cert))
382                 {
383                 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE,ERR_R_MALLOC_FAILURE);
384                 return(0);
385                 }
386         return(ssl_set_cert(ctx->cert, x));
387         }
388
389 static int ssl_set_cert(CERT *c, X509 *x)
390         {
391         EVP_PKEY *pkey;
392         int i;
393
394         pkey=X509_get_pubkey(x);
395         if (pkey == NULL)
396                 {
397                 SSLerr(SSL_F_SSL_SET_CERT,SSL_R_X509_LIB);
398                 return(0);
399                 }
400
401         i=ssl_cert_type(x,pkey);
402         if (i < 0)
403                 {
404                 SSLerr(SSL_F_SSL_SET_CERT,SSL_R_UNKNOWN_CERTIFICATE_TYPE);
405                 EVP_PKEY_free(pkey);
406                 return(0);
407                 }
408
409         if (c->pkeys[i].privatekey != NULL)
410                 {
411                 EVP_PKEY_copy_parameters(pkey,c->pkeys[i].privatekey);
412                 ERR_clear_error();
413
414 #ifndef OPENSSL_NO_RSA
415                 /* Don't check the public/private key, this is mostly
416                  * for smart cards. */
417                 if ((c->pkeys[i].privatekey->type == EVP_PKEY_RSA) &&
418                         (RSA_flags(c->pkeys[i].privatekey->pkey.rsa) &
419                          RSA_METHOD_FLAG_NO_CHECK))
420                          ;
421                 else
422 #endif /* OPENSSL_NO_RSA */
423                 if (!X509_check_private_key(x,c->pkeys[i].privatekey))
424                         {
425                         /* don't fail for a cert/key mismatch, just free
426                          * current private key (when switching to a different
427                          * cert & key, first this function should be used,
428                          * then ssl_set_pkey */
429                         EVP_PKEY_free(c->pkeys[i].privatekey);
430                         c->pkeys[i].privatekey=NULL;
431                         /* clear error queue */
432                         ERR_clear_error();
433                         }
434                 }
435
436         EVP_PKEY_free(pkey);
437
438         if (c->pkeys[i].x509 != NULL)
439                 X509_free(c->pkeys[i].x509);
440         CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
441         c->pkeys[i].x509=x;
442         c->key= &(c->pkeys[i]);
443
444         c->valid=0;
445         return(1);
446         }
447
448 #ifndef OPENSSL_NO_STDIO
449 int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type)
450         {
451         int j;
452         BIO *in;
453         int ret=0;
454         X509 *x=NULL;
455
456         in=BIO_new(BIO_s_file_internal());
457         if (in == NULL)
458                 {
459                 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,ERR_R_BUF_LIB);
460                 goto end;
461                 }
462
463         if (BIO_read_filename(in,file) <= 0)
464                 {
465                 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,ERR_R_SYS_LIB);
466                 goto end;
467                 }
468         if (type == SSL_FILETYPE_ASN1)
469                 {
470                 j=ERR_R_ASN1_LIB;
471                 x=d2i_X509_bio(in,NULL);
472                 }
473         else if (type == SSL_FILETYPE_PEM)
474                 {
475                 j=ERR_R_PEM_LIB;
476                 x=PEM_read_bio_X509(in,NULL,ctx->default_passwd_callback,ctx->default_passwd_callback_userdata);
477                 }
478         else
479                 {
480                 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,SSL_R_BAD_SSL_FILETYPE);
481                 goto end;
482                 }
483
484         if (x == NULL)
485                 {
486                 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,j);
487                 goto end;
488                 }
489
490         ret=SSL_CTX_use_certificate(ctx,x);
491 end:
492         if (x != NULL) X509_free(x);
493         if (in != NULL) BIO_free(in);
494         return(ret);
495         }
496 #endif
497
498 int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len, const unsigned char *d)
499         {
500         X509 *x;
501         int ret;
502
503         x=d2i_X509(NULL,&d,(long)len);
504         if (x == NULL)
505                 {
506                 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1,ERR_R_ASN1_LIB);
507                 return(0);
508                 }
509
510         ret=SSL_CTX_use_certificate(ctx,x);
511         X509_free(x);
512         return(ret);
513         }
514
515 #ifndef OPENSSL_NO_RSA
516 int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa)
517         {
518         int ret;
519         EVP_PKEY *pkey;
520
521         if (rsa == NULL)
522                 {
523                 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
524                 return(0);
525                 }
526         if (!ssl_cert_inst(&ctx->cert))
527                 {
528                 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY,ERR_R_MALLOC_FAILURE);
529                 return(0);
530                 }
531         if ((pkey=EVP_PKEY_new()) == NULL)
532                 {
533                 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY,ERR_R_EVP_LIB);
534                 return(0);
535                 }
536
537         RSA_up_ref(rsa);
538         EVP_PKEY_assign_RSA(pkey,rsa);
539
540         ret=ssl_set_pkey(ctx->cert, pkey);
541         EVP_PKEY_free(pkey);
542         return(ret);
543         }
544
545 #ifndef OPENSSL_NO_STDIO
546 int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, int type)
547         {
548         int j,ret=0;
549         BIO *in;
550         RSA *rsa=NULL;
551
552         in=BIO_new(BIO_s_file_internal());
553         if (in == NULL)
554                 {
555                 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,ERR_R_BUF_LIB);
556                 goto end;
557                 }
558
559         if (BIO_read_filename(in,file) <= 0)
560                 {
561                 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,ERR_R_SYS_LIB);
562                 goto end;
563                 }
564         if      (type == SSL_FILETYPE_ASN1)
565                 {
566                 j=ERR_R_ASN1_LIB;
567                 rsa=d2i_RSAPrivateKey_bio(in,NULL);
568                 }
569         else if (type == SSL_FILETYPE_PEM)
570                 {
571                 j=ERR_R_PEM_LIB;
572                 rsa=PEM_read_bio_RSAPrivateKey(in,NULL,
573                         ctx->default_passwd_callback,ctx->default_passwd_callback_userdata);
574                 }
575         else
576                 {
577                 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
578                 goto end;
579                 }
580         if (rsa == NULL)
581                 {
582                 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,j);
583                 goto end;
584                 }
585         ret=SSL_CTX_use_RSAPrivateKey(ctx,rsa);
586         RSA_free(rsa);
587 end:
588         if (in != NULL) BIO_free(in);
589         return(ret);
590         }
591 #endif
592
593 int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const unsigned char *d, long len)
594         {
595         int ret;
596         const unsigned char *p;
597         RSA *rsa;
598
599         p=d;
600         if ((rsa=d2i_RSAPrivateKey(NULL,&p,(long)len)) == NULL)
601                 {
602                 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
603                 return(0);
604                 }
605
606         ret=SSL_CTX_use_RSAPrivateKey(ctx,rsa);
607         RSA_free(rsa);
608         return(ret);
609         }
610 #endif /* !OPENSSL_NO_RSA */
611
612 int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey)
613         {
614         if (pkey == NULL)
615                 {
616                 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
617                 return(0);
618                 }
619         if (!ssl_cert_inst(&ctx->cert))
620                 {
621                 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY,ERR_R_MALLOC_FAILURE);
622                 return(0);
623                 }
624         return(ssl_set_pkey(ctx->cert,pkey));
625         }
626
627 #ifndef OPENSSL_NO_STDIO
628 int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
629         {
630         int j,ret=0;
631         BIO *in;
632         EVP_PKEY *pkey=NULL;
633
634         in=BIO_new(BIO_s_file_internal());
635         if (in == NULL)
636                 {
637                 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,ERR_R_BUF_LIB);
638                 goto end;
639                 }
640
641         if (BIO_read_filename(in,file) <= 0)
642                 {
643                 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,ERR_R_SYS_LIB);
644                 goto end;
645                 }
646         if (type == SSL_FILETYPE_PEM)
647                 {
648                 j=ERR_R_PEM_LIB;
649                 pkey=PEM_read_bio_PrivateKey(in,NULL,
650                         ctx->default_passwd_callback,ctx->default_passwd_callback_userdata);
651                 }
652         else
653                 {
654                 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
655                 goto end;
656                 }
657         if (pkey == NULL)
658                 {
659                 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,j);
660                 goto end;
661                 }
662         ret=SSL_CTX_use_PrivateKey(ctx,pkey);
663         EVP_PKEY_free(pkey);
664 end:
665         if (in != NULL) BIO_free(in);
666         return(ret);
667         }
668 #endif
669
670 int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx, const unsigned char *d,
671              long len)
672         {
673         int ret;
674         const unsigned char *p;
675         EVP_PKEY *pkey;
676
677         p=d;
678         if ((pkey=d2i_PrivateKey(type,NULL,&p,(long)len)) == NULL)
679                 {
680                 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
681                 return(0);
682                 }
683
684         ret=SSL_CTX_use_PrivateKey(ctx,pkey);
685         EVP_PKEY_free(pkey);
686         return(ret);
687         }
688
689
690 #ifndef OPENSSL_NO_STDIO
691 /* Read a file that contains our certificate in "PEM" format,
692  * possibly followed by a sequence of CA certificates that should be
693  * sent to the peer in the Certificate message.
694  */
695 int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file)
696         {
697         BIO *in;
698         int ret=0;
699         X509 *x=NULL;
700
701         in=BIO_new(BIO_s_file_internal());
702         if (in == NULL)
703                 {
704                 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE,ERR_R_BUF_LIB);
705                 goto end;
706                 }
707
708         if (BIO_read_filename(in,file) <= 0)
709                 {
710                 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE,ERR_R_SYS_LIB);
711                 goto end;
712                 }
713
714         x=PEM_read_bio_X509(in,NULL,ctx->default_passwd_callback,ctx->default_passwd_callback_userdata);
715         if (x == NULL)
716                 {
717                 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE,ERR_R_PEM_LIB);
718                 goto end;
719                 }
720
721         ret=SSL_CTX_use_certificate(ctx,x);
722         if (ERR_peek_error() != 0)
723                 ret = 0;  /* Key/certificate mismatch doesn't imply ret==0 ... */
724         if (ret)
725                 {
726                 /* If we could set up our certificate, now proceed to
727                  * the CA certificates.
728                  */
729                 X509 *ca;
730                 int r;
731                 unsigned long err;
732                 
733                 if (ctx->extra_certs != NULL) 
734                         {
735                         sk_X509_pop_free(ctx->extra_certs, X509_free);
736                         ctx->extra_certs = NULL;
737                         }
738
739                 while ((ca = PEM_read_bio_X509(in,NULL,ctx->default_passwd_callback,ctx->default_passwd_callback_userdata))
740                         != NULL)
741                         {
742                         r = SSL_CTX_add_extra_chain_cert(ctx, ca);
743                         if (!r) 
744                                 {
745                                 X509_free(ca);
746                                 ret = 0;
747                                 goto end;
748                                 }
749                         /* Note that we must not free r if it was successfully
750                          * added to the chain (while we must free the main
751                          * certificate, since its reference count is increased
752                          * by SSL_CTX_use_certificate). */
753                         }
754                 /* When the while loop ends, it's usually just EOF. */
755                 err = ERR_peek_last_error();
756                 if (ERR_GET_LIB(err) == ERR_LIB_PEM && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)
757                         ERR_clear_error();
758                 else 
759                         ret = 0; /* some real error */
760                 }
761
762 end:
763         if (x != NULL) X509_free(x);
764         if (in != NULL) BIO_free(in);
765         return(ret);
766         }
767 #endif