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