Remove one more totally bogus source file.
[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 "bio.h"
61 #include "objects.h"
62 #include "evp.h"
63 #include "x509.h"
64 #include "pem.h"
65 #include "ssl_locl.h"
66
67 #ifndef NOPROTO
68 static int ssl_set_cert(CERT *c, X509 *x509);
69 static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey);
70 #else
71 static int ssl_set_cert();
72 static int ssl_set_pkey();
73 #endif
74
75 int SSL_use_certificate(ssl, x)
76 SSL *ssl;
77 X509 *x;
78         {
79         CERT *c;
80
81         if (x == NULL)
82                 {
83                 SSLerr(SSL_F_SSL_USE_CERTIFICATE,ERR_R_PASSED_NULL_PARAMETER);
84                 return(0);
85                 }
86         if ((ssl->cert == NULL) || (ssl->cert == ssl->ctx->default_cert))
87                 {
88                 c=ssl_cert_new();
89                 if (c == NULL)
90                         {
91                         SSLerr(SSL_F_SSL_USE_CERTIFICATE,ERR_R_MALLOC_FAILURE);
92                         return(0);
93                         }
94                 if (ssl->cert != NULL) ssl_cert_free(ssl->cert);
95                 ssl->cert=c;
96                 }
97         c=ssl->cert;
98
99         return(ssl_set_cert(c,x));
100         }
101
102 #ifndef NO_STDIO
103 int SSL_use_certificate_file(ssl, file, type)
104 SSL *ssl;
105 char *file;
106 int type;
107         {
108         int j;
109         BIO *in;
110         int ret=0;
111         X509 *x=NULL;
112
113         in=BIO_new(BIO_s_file_internal());
114         if (in == NULL)
115                 {
116                 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE,ERR_R_BUF_LIB);
117                 goto end;
118                 }
119
120         if (BIO_read_filename(in,file) <= 0)
121                 {
122                 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE,ERR_R_SYS_LIB);
123                 goto end;
124                 }
125         if (type == SSL_FILETYPE_ASN1)
126                 {
127                 j=ERR_R_ASN1_LIB;
128                 x=d2i_X509_bio(in,NULL);
129                 }
130         else if (type == SSL_FILETYPE_PEM)
131                 {
132                 j=ERR_R_PEM_LIB;
133                 x=PEM_read_bio_X509(in,NULL,ssl->ctx->default_passwd_callback);
134                 }
135         else
136                 {
137                 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE,SSL_R_BAD_SSL_FILETYPE);
138                 goto end;
139                 }
140
141         if (x == NULL)
142                 {
143                 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE,j);
144                 goto end;
145                 }
146
147         ret=SSL_use_certificate(ssl,x);
148 end:
149         if (x != NULL) X509_free(x);
150         if (in != NULL) BIO_free(in);
151         return(ret);
152         }
153 #endif
154
155 int SSL_use_certificate_ASN1(ssl, len, d)
156 SSL *ssl;
157 int len;
158 unsigned char *d;
159         {
160         X509 *x;
161         int ret;
162
163         x=d2i_X509(NULL,&d,(long)len);
164         if (x == NULL)
165                 {
166                 SSLerr(SSL_F_SSL_USE_CERTIFICATE_ASN1,ERR_R_ASN1_LIB);
167                 return(0);
168                 }
169
170         ret=SSL_use_certificate(ssl,x);
171         X509_free(x);
172         return(ret);
173         }
174
175 #ifndef NO_RSA
176 int SSL_use_RSAPrivateKey(ssl, rsa)
177 SSL *ssl;
178 RSA *rsa;
179         {
180         CERT *c;
181         EVP_PKEY *pkey;
182         int ret;
183
184         if (rsa == NULL)
185                 {
186                 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
187                 return(0);
188                 }
189
190         if ((ssl->cert == NULL) || (ssl->cert == ssl->ctx->default_cert))
191                 {
192                 c=ssl_cert_new();
193                 if (c == NULL)
194                         {
195                         SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY,ERR_R_MALLOC_FAILURE);
196                         return(0);
197                         }
198                 if (ssl->cert != NULL) ssl_cert_free(ssl->cert);
199                 ssl->cert=c;
200                 }
201         c=ssl->cert;
202         if ((pkey=EVP_PKEY_new()) == NULL)
203                 {
204                 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY,ERR_R_EVP_LIB);
205                 return(0);
206                 }
207
208         CRYPTO_add(&rsa->references,1,CRYPTO_LOCK_RSA);
209         EVP_PKEY_assign_RSA(pkey,rsa);
210
211         ret=ssl_set_pkey(c,pkey);
212         EVP_PKEY_free(pkey);
213         return(ret);
214         }
215 #endif
216
217 static int ssl_set_pkey(c,pkey)
218 CERT *c;
219 EVP_PKEY *pkey;
220         {
221         int i,ok=0,bad=0;
222
223         i=ssl_cert_type(NULL,pkey);
224         if (i < 0)
225                 {
226                 SSLerr(SSL_F_SSL_SET_PKEY,SSL_R_UNKNOWN_CERTIFICATE_TYPE);
227                 return(0);
228                 }
229
230         if (c->pkeys[i].x509 != NULL)
231                 {
232                 EVP_PKEY_copy_parameters(
233                         X509_get_pubkey(c->pkeys[i].x509),pkey);
234                 ERR_clear_error();
235
236 #ifndef NO_RSA
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))
242                          ok=1;
243                 else
244 #endif
245                         if (!X509_check_private_key(c->pkeys[i].x509,pkey))
246                         {
247                         if ((i == SSL_PKEY_DH_RSA) || (i == SSL_PKEY_DH_DSA))
248                                 {
249                                 i=(i == SSL_PKEY_DH_RSA)?
250                                         SSL_PKEY_DH_DSA:SSL_PKEY_DH_RSA;
251
252                                 if (c->pkeys[i].x509 == NULL)
253                                         ok=1;
254                                 else
255                                         {
256                                         if (!X509_check_private_key(
257                                                 c->pkeys[i].x509,pkey))
258                                                 bad=1;
259                                         else
260                                                 ok=1;
261                                         }
262                                 }
263                         else
264                                 bad=1;
265                         }
266                 else
267                         ok=1;
268                 }
269         else
270                 ok=1;
271
272         if (bad)
273                 {
274                 X509_free(c->pkeys[i].x509);
275                 c->pkeys[i].x509=NULL;
276                 return(0);
277                 }
278
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]);
284
285         c->valid=0;
286         return(1);
287         }
288
289 #ifndef NO_RSA
290 #ifndef NO_STDIO
291 int SSL_use_RSAPrivateKey_file(ssl, file, type)
292 SSL *ssl;
293 char *file;
294 int type;
295         {
296         int j,ret=0;
297         BIO *in;
298         RSA *rsa=NULL;
299
300         in=BIO_new(BIO_s_file_internal());
301         if (in == NULL)
302                 {
303                 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,ERR_R_BUF_LIB);
304                 goto end;
305                 }
306
307         if (BIO_read_filename(in,file) <= 0)
308                 {
309                 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,ERR_R_SYS_LIB);
310                 goto end;
311                 }
312         if      (type == SSL_FILETYPE_ASN1)
313                 {
314                 j=ERR_R_ASN1_LIB;
315                 rsa=d2i_RSAPrivateKey_bio(in,NULL);
316                 }
317         else if (type == SSL_FILETYPE_PEM)
318                 {
319                 j=ERR_R_PEM_LIB;
320                 rsa=PEM_read_bio_RSAPrivateKey(in,NULL,
321                         ssl->ctx->default_passwd_callback);
322                 }
323         else
324                 {
325                 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
326                 goto end;
327                 }
328         if (rsa == NULL)
329                 {
330                 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,j);
331                 goto end;
332                 }
333         ret=SSL_use_RSAPrivateKey(ssl,rsa);
334         RSA_free(rsa);
335 end:
336         if (in != NULL) BIO_free(in);
337         return(ret);
338         }
339 #endif
340
341 int SSL_use_RSAPrivateKey_ASN1(ssl,d,len)
342 SSL *ssl;
343 unsigned char *d;
344 long len;
345         {
346         int ret;
347         unsigned char *p;
348         RSA *rsa;
349
350         p=d;
351         if ((rsa=d2i_RSAPrivateKey(NULL,&p,(long)len)) == NULL)
352                 {
353                 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
354                 return(0);
355                 }
356
357         ret=SSL_use_RSAPrivateKey(ssl,rsa);
358         RSA_free(rsa);
359         return(ret);
360         }
361 #endif /* !NO_RSA */
362
363 int SSL_use_PrivateKey(ssl, pkey)
364 SSL *ssl;
365 EVP_PKEY *pkey;
366         {
367         CERT *c;
368         int ret;
369
370         if (pkey == NULL)
371                 {
372                 SSLerr(SSL_F_SSL_USE_PRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
373                 return(0);
374                 }
375
376         if ((ssl->cert == NULL) || (ssl->cert == ssl->ctx->default_cert))
377                 {
378                 c=ssl_cert_new();
379                 if (c == NULL)
380                         {
381                         SSLerr(SSL_F_SSL_USE_PRIVATEKEY,ERR_R_MALLOC_FAILURE);
382                         return(0);
383                         }
384                 if (ssl->cert != NULL) ssl_cert_free(ssl->cert);
385                 ssl->cert=c;
386                 }
387         c=ssl->cert;
388
389         ret=ssl_set_pkey(c,pkey);
390         return(ret);
391         }
392
393 #ifndef NO_STDIO
394 int SSL_use_PrivateKey_file(ssl, file, type)
395 SSL *ssl;
396 char *file;
397 int type;
398         {
399         int j,ret=0;
400         BIO *in;
401         EVP_PKEY *pkey=NULL;
402
403         in=BIO_new(BIO_s_file_internal());
404         if (in == NULL)
405                 {
406                 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,ERR_R_BUF_LIB);
407                 goto end;
408                 }
409
410         if (BIO_read_filename(in,file) <= 0)
411                 {
412                 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,ERR_R_SYS_LIB);
413                 goto end;
414                 }
415         if (type == SSL_FILETYPE_PEM)
416                 {
417                 j=ERR_R_PEM_LIB;
418                 pkey=PEM_read_bio_PrivateKey(in,NULL,
419                         ssl->ctx->default_passwd_callback);
420                 }
421         else
422                 {
423                 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
424                 goto end;
425                 }
426         if (pkey == NULL)
427                 {
428                 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,j);
429                 goto end;
430                 }
431         ret=SSL_use_PrivateKey(ssl,pkey);
432         EVP_PKEY_free(pkey);
433 end:
434         if (in != NULL) BIO_free(in);
435         return(ret);
436         }
437 #endif
438
439 int SSL_use_PrivateKey_ASN1(type,ssl,d,len)
440 int type;
441 SSL *ssl;
442 unsigned char *d;
443 long len;
444         {
445         int ret;
446         unsigned char *p;
447         EVP_PKEY *pkey;
448
449         p=d;
450         if ((pkey=d2i_PrivateKey(type,NULL,&p,(long)len)) == NULL)
451                 {
452                 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
453                 return(0);
454                 }
455
456         ret=SSL_use_PrivateKey(ssl,pkey);
457         EVP_PKEY_free(pkey);
458         return(ret);
459         }
460
461 int SSL_CTX_use_certificate(ctx, x)
462 SSL_CTX *ctx;
463 X509 *x;
464         {
465         CERT *c;
466
467         if (x == NULL)
468                 {
469                 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE,ERR_R_PASSED_NULL_PARAMETER);
470                 return(0);
471                 }
472
473         if (ctx->default_cert == NULL)
474                 {
475                 c=ssl_cert_new();
476                 if (c == NULL)
477                         {
478                         SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE,ERR_R_MALLOC_FAILURE);
479                         return(0);
480                         }
481                 ctx->default_cert=c;
482                 }
483         c=ctx->default_cert;
484
485         return(ssl_set_cert(c,x));
486         }
487
488 static int ssl_set_cert(c,x)
489 CERT *c;
490 X509 *x;
491         {
492         EVP_PKEY *pkey;
493         int i,ok=0,bad=0;
494
495         pkey=X509_get_pubkey(x);
496         if (pkey == NULL)
497                 {
498                 SSLerr(SSL_F_SSL_SET_CERT,SSL_R_X509_LIB);
499                 return(0);
500                 }
501
502         i=ssl_cert_type(x,pkey);
503         if (i < 0)
504                 {
505                 SSLerr(SSL_F_SSL_SET_CERT,SSL_R_UNKNOWN_CERTIFICATE_TYPE);
506                 return(0);
507                 }
508
509         if (c->pkeys[i].privatekey != NULL)
510                 {
511                 EVP_PKEY_copy_parameters(pkey,c->pkeys[i].privatekey);
512                 ERR_clear_error();
513
514 #ifndef NO_RSA
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))
520                          ok=1;
521                 else
522 #endif
523                 {
524                 if (!X509_check_private_key(x,c->pkeys[i].privatekey))
525                         {
526                         if ((i == SSL_PKEY_DH_RSA) || (i == SSL_PKEY_DH_DSA))
527                                 {
528                                 i=(i == SSL_PKEY_DH_RSA)?
529                                         SSL_PKEY_DH_DSA:SSL_PKEY_DH_RSA;
530
531                                 if (c->pkeys[i].privatekey == NULL)
532                                         ok=1;
533                                 else
534                                         {
535                                         if (!X509_check_private_key(x,
536                                                 c->pkeys[i].privatekey))
537                                                 bad=1;
538                                         else
539                                                 ok=1;
540                                         }
541                                 }
542                         else
543                                 bad=1;
544                         }
545                 else
546                         ok=1;
547                 } /* NO_RSA */
548                 }
549         else
550                 ok=1;
551
552         if (bad)
553                 {
554                 EVP_PKEY_free(c->pkeys[i].privatekey);
555                 c->pkeys[i].privatekey=NULL;
556                 }
557
558         if (c->pkeys[i].x509 != NULL)
559                 X509_free(c->pkeys[i].x509);
560         CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
561         c->pkeys[i].x509=x;
562         c->key= &(c->pkeys[i]);
563
564         c->valid=0;
565         return(1);
566         }
567
568 #ifndef NO_STDIO
569 int SSL_CTX_use_certificate_file(ctx, file, type)
570 SSL_CTX *ctx;
571 char *file;
572 int type;
573         {
574         int j;
575         BIO *in;
576         int ret=0;
577         X509 *x=NULL;
578
579         in=BIO_new(BIO_s_file_internal());
580         if (in == NULL)
581                 {
582                 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,ERR_R_BUF_LIB);
583                 goto end;
584                 }
585
586         if (BIO_read_filename(in,file) <= 0)
587                 {
588                 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,ERR_R_SYS_LIB);
589                 goto end;
590                 }
591         if (type == SSL_FILETYPE_ASN1)
592                 {
593                 j=ERR_R_ASN1_LIB;
594                 x=d2i_X509_bio(in,NULL);
595                 }
596         else if (type == SSL_FILETYPE_PEM)
597                 {
598                 j=ERR_R_PEM_LIB;
599                 x=PEM_read_bio_X509(in,NULL,ctx->default_passwd_callback);
600                 }
601         else
602                 {
603                 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,SSL_R_BAD_SSL_FILETYPE);
604                 goto end;
605                 }
606
607         if (x == NULL)
608                 {
609                 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,j);
610                 goto end;
611                 }
612
613         ret=SSL_CTX_use_certificate(ctx,x);
614 end:
615         if (x != NULL) X509_free(x);
616         if (in != NULL) BIO_free(in);
617         return(ret);
618         }
619 #endif
620
621 int SSL_CTX_use_certificate_ASN1(ctx, len, d)
622 SSL_CTX *ctx;
623 int len;
624 unsigned char *d;
625         {
626         X509 *x;
627         int ret;
628
629         x=d2i_X509(NULL,&d,(long)len);
630         if (x == NULL)
631                 {
632                 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1,ERR_R_ASN1_LIB);
633                 return(0);
634                 }
635
636         ret=SSL_CTX_use_certificate(ctx,x);
637         X509_free(x);
638         return(ret);
639         }
640
641 #ifndef NO_RSA
642 int SSL_CTX_use_RSAPrivateKey(ctx, rsa)
643 SSL_CTX *ctx;
644 RSA *rsa;
645         {
646         int ret;
647         CERT *c;
648         EVP_PKEY *pkey;
649
650         if (rsa == NULL)
651                 {
652                 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
653                 return(0);
654                 }
655         if (ctx->default_cert == NULL)
656                 {
657                 c=ssl_cert_new();
658                 if (c == NULL)
659                         {
660                         SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY,ERR_R_MALLOC_FAILURE);
661                         return(0);
662                         }
663                 ctx->default_cert=c;
664                 }
665         c=ctx->default_cert;
666
667         if ((pkey=EVP_PKEY_new()) == NULL)
668                 {
669                 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY,ERR_R_EVP_LIB);
670                 return(0);
671                 }
672
673         CRYPTO_add(&rsa->references,1,CRYPTO_LOCK_RSA);
674         EVP_PKEY_assign_RSA(pkey,rsa);
675
676         ret=ssl_set_pkey(c,pkey);
677         EVP_PKEY_free(pkey);
678         return(ret);
679         }
680
681 #ifndef NO_STDIO
682 int SSL_CTX_use_RSAPrivateKey_file(ctx, file, type)
683 SSL_CTX *ctx;
684 char *file;
685 int type;
686         {
687         int j,ret=0;
688         BIO *in;
689         RSA *rsa=NULL;
690
691         in=BIO_new(BIO_s_file_internal());
692         if (in == NULL)
693                 {
694                 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,ERR_R_BUF_LIB);
695                 goto end;
696                 }
697
698         if (BIO_read_filename(in,file) <= 0)
699                 {
700                 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,ERR_R_SYS_LIB);
701                 goto end;
702                 }
703         if      (type == SSL_FILETYPE_ASN1)
704                 {
705                 j=ERR_R_ASN1_LIB;
706                 rsa=d2i_RSAPrivateKey_bio(in,NULL);
707                 }
708         else if (type == SSL_FILETYPE_PEM)
709                 {
710                 j=ERR_R_PEM_LIB;
711                 rsa=PEM_read_bio_RSAPrivateKey(in,NULL,
712                         ctx->default_passwd_callback);
713                 }
714         else
715                 {
716                 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
717                 goto end;
718                 }
719         if (rsa == NULL)
720                 {
721                 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,j);
722                 goto end;
723                 }
724         ret=SSL_CTX_use_RSAPrivateKey(ctx,rsa);
725         RSA_free(rsa);
726 end:
727         if (in != NULL) BIO_free(in);
728         return(ret);
729         }
730 #endif
731
732 int SSL_CTX_use_RSAPrivateKey_ASN1(ctx,d,len)
733 SSL_CTX *ctx;
734 unsigned char *d;
735 long len;
736         {
737         int ret;
738         unsigned char *p;
739         RSA *rsa;
740
741         p=d;
742         if ((rsa=d2i_RSAPrivateKey(NULL,&p,(long)len)) == NULL)
743                 {
744                 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
745                 return(0);
746                 }
747
748         ret=SSL_CTX_use_RSAPrivateKey(ctx,rsa);
749         RSA_free(rsa);
750         return(ret);
751         }
752 #endif /* !NO_RSA */
753
754 int SSL_CTX_use_PrivateKey(ctx, pkey)
755 SSL_CTX *ctx;
756 EVP_PKEY *pkey;
757         {
758         CERT *c;
759
760         if (pkey == NULL)
761                 {
762                 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
763                 return(0);
764                 }
765                 
766         if (ctx->default_cert == NULL)
767                 {
768                 c=ssl_cert_new();
769                 if (c == NULL)
770                         {
771                         SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY,ERR_R_MALLOC_FAILURE);
772                         return(0);
773                         }
774                 ctx->default_cert=c;
775                 }
776         c=ctx->default_cert;
777
778         return(ssl_set_pkey(c,pkey));
779         }
780
781 #ifndef NO_STDIO
782 int SSL_CTX_use_PrivateKey_file(ctx, file, type)
783 SSL_CTX *ctx;
784 char *file;
785 int type;
786         {
787         int j,ret=0;
788         BIO *in;
789         EVP_PKEY *pkey=NULL;
790
791         in=BIO_new(BIO_s_file_internal());
792         if (in == NULL)
793                 {
794                 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,ERR_R_BUF_LIB);
795                 goto end;
796                 }
797
798         if (BIO_read_filename(in,file) <= 0)
799                 {
800                 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,ERR_R_SYS_LIB);
801                 goto end;
802                 }
803         if (type == SSL_FILETYPE_PEM)
804                 {
805                 j=ERR_R_PEM_LIB;
806                 pkey=PEM_read_bio_PrivateKey(in,NULL,
807                         ctx->default_passwd_callback);
808                 }
809         else
810                 {
811                 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
812                 goto end;
813                 }
814         if (pkey == NULL)
815                 {
816                 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,j);
817                 goto end;
818                 }
819         ret=SSL_CTX_use_PrivateKey(ctx,pkey);
820         EVP_PKEY_free(pkey);
821 end:
822         if (in != NULL) BIO_free(in);
823         return(ret);
824         }
825 #endif
826
827 int SSL_CTX_use_PrivateKey_ASN1(type,ctx,d,len)
828 int type;
829 SSL_CTX *ctx;
830 unsigned char *d;
831 long len;
832         {
833         int ret;
834         unsigned char *p;
835         EVP_PKEY *pkey;
836
837         p=d;
838         if ((pkey=d2i_PrivateKey(type,NULL,&p,(long)len)) == NULL)
839                 {
840                 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
841                 return(0);
842                 }
843
844         ret=SSL_CTX_use_PrivateKey(ctx,pkey);
845         EVP_PKEY_free(pkey);
846         return(ret);
847         }
848
849