Updates to the new SSL compression code
[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, d,len)
156 SSL *ssl;
157 unsigned char *d;
158 int len;
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 *pktmp;
233                 pktmp = X509_get_pubkey(c->pkeys[i].x509);
234                 EVP_PKEY_copy_parameters(pktmp,pkey);
235                 EVP_PKEY_free(pktmp);
236                 ERR_clear_error();
237
238 #ifndef NO_RSA
239                 /* Don't check the public/private key, this is mostly
240                  * for smart cards. */
241                 if ((pkey->type == EVP_PKEY_RSA) &&
242                         (RSA_flags(pkey->pkey.rsa) &
243                          RSA_METHOD_FLAG_NO_CHECK))
244                          ok=1;
245                 else
246 #endif
247                         if (!X509_check_private_key(c->pkeys[i].x509,pkey))
248                         {
249                         if ((i == SSL_PKEY_DH_RSA) || (i == SSL_PKEY_DH_DSA))
250                                 {
251                                 i=(i == SSL_PKEY_DH_RSA)?
252                                         SSL_PKEY_DH_DSA:SSL_PKEY_DH_RSA;
253
254                                 if (c->pkeys[i].x509 == NULL)
255                                         ok=1;
256                                 else
257                                         {
258                                         if (!X509_check_private_key(
259                                                 c->pkeys[i].x509,pkey))
260                                                 bad=1;
261                                         else
262                                                 ok=1;
263                                         }
264                                 }
265                         else
266                                 bad=1;
267                         }
268                 else
269                         ok=1;
270                 }
271         else
272                 ok=1;
273
274         if (bad)
275                 {
276                 X509_free(c->pkeys[i].x509);
277                 c->pkeys[i].x509=NULL;
278                 return(0);
279                 }
280
281         if (c->pkeys[i].privatekey != NULL)
282                 EVP_PKEY_free(c->pkeys[i].privatekey);
283         CRYPTO_add(&pkey->references,1,CRYPTO_LOCK_EVP_PKEY);
284         c->pkeys[i].privatekey=pkey;
285         c->key= &(c->pkeys[i]);
286
287         c->valid=0;
288         return(1);
289         }
290
291 #ifndef NO_RSA
292 #ifndef NO_STDIO
293 int SSL_use_RSAPrivateKey_file(ssl, file, type)
294 SSL *ssl;
295 char *file;
296 int type;
297         {
298         int j,ret=0;
299         BIO *in;
300         RSA *rsa=NULL;
301
302         in=BIO_new(BIO_s_file_internal());
303         if (in == NULL)
304                 {
305                 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,ERR_R_BUF_LIB);
306                 goto end;
307                 }
308
309         if (BIO_read_filename(in,file) <= 0)
310                 {
311                 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,ERR_R_SYS_LIB);
312                 goto end;
313                 }
314         if      (type == SSL_FILETYPE_ASN1)
315                 {
316                 j=ERR_R_ASN1_LIB;
317                 rsa=d2i_RSAPrivateKey_bio(in,NULL);
318                 }
319         else if (type == SSL_FILETYPE_PEM)
320                 {
321                 j=ERR_R_PEM_LIB;
322                 rsa=PEM_read_bio_RSAPrivateKey(in,NULL,
323                         ssl->ctx->default_passwd_callback);
324                 }
325         else
326                 {
327                 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
328                 goto end;
329                 }
330         if (rsa == NULL)
331                 {
332                 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,j);
333                 goto end;
334                 }
335         ret=SSL_use_RSAPrivateKey(ssl,rsa);
336         RSA_free(rsa);
337 end:
338         if (in != NULL) BIO_free(in);
339         return(ret);
340         }
341 #endif
342
343 int SSL_use_RSAPrivateKey_ASN1(ssl,d,len)
344 SSL *ssl;
345 unsigned char *d;
346 long len;
347         {
348         int ret;
349         unsigned char *p;
350         RSA *rsa;
351
352         p=d;
353         if ((rsa=d2i_RSAPrivateKey(NULL,&p,(long)len)) == NULL)
354                 {
355                 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
356                 return(0);
357                 }
358
359         ret=SSL_use_RSAPrivateKey(ssl,rsa);
360         RSA_free(rsa);
361         return(ret);
362         }
363 #endif /* !NO_RSA */
364
365 int SSL_use_PrivateKey(ssl, pkey)
366 SSL *ssl;
367 EVP_PKEY *pkey;
368         {
369         CERT *c;
370         int ret;
371
372         if (pkey == NULL)
373                 {
374                 SSLerr(SSL_F_SSL_USE_PRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
375                 return(0);
376                 }
377
378         if ((ssl->cert == NULL) || (ssl->cert == ssl->ctx->default_cert))
379                 {
380                 c=ssl_cert_new();
381                 if (c == NULL)
382                         {
383                         SSLerr(SSL_F_SSL_USE_PRIVATEKEY,ERR_R_MALLOC_FAILURE);
384                         return(0);
385                         }
386                 if (ssl->cert != NULL) ssl_cert_free(ssl->cert);
387                 ssl->cert=c;
388                 }
389         c=ssl->cert;
390
391         ret=ssl_set_pkey(c,pkey);
392         return(ret);
393         }
394
395 #ifndef NO_STDIO
396 int SSL_use_PrivateKey_file(ssl, file, type)
397 SSL *ssl;
398 char *file;
399 int type;
400         {
401         int j,ret=0;
402         BIO *in;
403         EVP_PKEY *pkey=NULL;
404
405         in=BIO_new(BIO_s_file_internal());
406         if (in == NULL)
407                 {
408                 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,ERR_R_BUF_LIB);
409                 goto end;
410                 }
411
412         if (BIO_read_filename(in,file) <= 0)
413                 {
414                 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,ERR_R_SYS_LIB);
415                 goto end;
416                 }
417         if (type == SSL_FILETYPE_PEM)
418                 {
419                 j=ERR_R_PEM_LIB;
420                 pkey=PEM_read_bio_PrivateKey(in,NULL,
421                         ssl->ctx->default_passwd_callback);
422                 }
423         else
424                 {
425                 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
426                 goto end;
427                 }
428         if (pkey == NULL)
429                 {
430                 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,j);
431                 goto end;
432                 }
433         ret=SSL_use_PrivateKey(ssl,pkey);
434         EVP_PKEY_free(pkey);
435 end:
436         if (in != NULL) BIO_free(in);
437         return(ret);
438         }
439 #endif
440
441 int SSL_use_PrivateKey_ASN1(type,ssl,d,len)
442 int type;
443 SSL *ssl;
444 unsigned char *d;
445 long len;
446         {
447         int ret;
448         unsigned char *p;
449         EVP_PKEY *pkey;
450
451         p=d;
452         if ((pkey=d2i_PrivateKey(type,NULL,&p,(long)len)) == NULL)
453                 {
454                 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
455                 return(0);
456                 }
457
458         ret=SSL_use_PrivateKey(ssl,pkey);
459         EVP_PKEY_free(pkey);
460         return(ret);
461         }
462
463 int SSL_CTX_use_certificate(ctx, x)
464 SSL_CTX *ctx;
465 X509 *x;
466         {
467         CERT *c;
468
469         if (x == NULL)
470                 {
471                 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE,ERR_R_PASSED_NULL_PARAMETER);
472                 return(0);
473                 }
474
475         if (ctx->default_cert == NULL)
476                 {
477                 c=ssl_cert_new();
478                 if (c == NULL)
479                         {
480                         SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE,ERR_R_MALLOC_FAILURE);
481                         return(0);
482                         }
483                 ctx->default_cert=c;
484                 }
485         c=ctx->default_cert;
486
487         return(ssl_set_cert(c,x));
488         }
489
490 static int ssl_set_cert(c,x)
491 CERT *c;
492 X509 *x;
493         {
494         EVP_PKEY *pkey;
495         int i,ok=0,bad=0;
496
497         pkey=X509_get_pubkey(x);
498         if (pkey == NULL)
499                 {
500                 SSLerr(SSL_F_SSL_SET_CERT,SSL_R_X509_LIB);
501                 return(0);
502                 }
503
504         i=ssl_cert_type(x,pkey);
505         if (i < 0)
506                 {
507                 SSLerr(SSL_F_SSL_SET_CERT,SSL_R_UNKNOWN_CERTIFICATE_TYPE);
508                 EVP_PKEY_free(pkey);
509                 return(0);
510                 }
511
512         if (c->pkeys[i].privatekey != NULL)
513                 {
514                 EVP_PKEY_copy_parameters(pkey,c->pkeys[i].privatekey);
515                 ERR_clear_error();
516
517 #ifndef NO_RSA
518                 /* Don't check the public/private key, this is mostly
519                  * for smart cards. */
520                 if ((c->pkeys[i].privatekey->type == EVP_PKEY_RSA) &&
521                         (RSA_flags(c->pkeys[i].privatekey->pkey.rsa) &
522                          RSA_METHOD_FLAG_NO_CHECK))
523                          ok=1;
524                 else
525 #endif
526                 {
527                 if (!X509_check_private_key(x,c->pkeys[i].privatekey))
528                         {
529                         if ((i == SSL_PKEY_DH_RSA) || (i == SSL_PKEY_DH_DSA))
530                                 {
531                                 i=(i == SSL_PKEY_DH_RSA)?
532                                         SSL_PKEY_DH_DSA:SSL_PKEY_DH_RSA;
533
534                                 if (c->pkeys[i].privatekey == NULL)
535                                         ok=1;
536                                 else
537                                         {
538                                         if (!X509_check_private_key(x,
539                                                 c->pkeys[i].privatekey))
540                                                 bad=1;
541                                         else
542                                                 ok=1;
543                                         }
544                                 }
545                         else
546                                 bad=1;
547                         }
548                 else
549                         ok=1;
550                 } /* NO_RSA */
551                 }
552         else
553                 ok=1;
554
555         EVP_PKEY_free(pkey);
556         if (bad)
557                 {
558                 EVP_PKEY_free(c->pkeys[i].privatekey);
559                 c->pkeys[i].privatekey=NULL;
560                 }
561
562         if (c->pkeys[i].x509 != NULL)
563                 X509_free(c->pkeys[i].x509);
564         CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
565         c->pkeys[i].x509=x;
566         c->key= &(c->pkeys[i]);
567
568         c->valid=0;
569         return(1);
570         }
571
572 #ifndef NO_STDIO
573 int SSL_CTX_use_certificate_file(ctx, file, type)
574 SSL_CTX *ctx;
575 char *file;
576 int type;
577         {
578         int j;
579         BIO *in;
580         int ret=0;
581         X509 *x=NULL;
582
583         in=BIO_new(BIO_s_file_internal());
584         if (in == NULL)
585                 {
586                 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,ERR_R_BUF_LIB);
587                 goto end;
588                 }
589
590         if (BIO_read_filename(in,file) <= 0)
591                 {
592                 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,ERR_R_SYS_LIB);
593                 goto end;
594                 }
595         if (type == SSL_FILETYPE_ASN1)
596                 {
597                 j=ERR_R_ASN1_LIB;
598                 x=d2i_X509_bio(in,NULL);
599                 }
600         else if (type == SSL_FILETYPE_PEM)
601                 {
602                 j=ERR_R_PEM_LIB;
603                 x=PEM_read_bio_X509(in,NULL,ctx->default_passwd_callback);
604                 }
605         else
606                 {
607                 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,SSL_R_BAD_SSL_FILETYPE);
608                 goto end;
609                 }
610
611         if (x == NULL)
612                 {
613                 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,j);
614                 goto end;
615                 }
616
617         ret=SSL_CTX_use_certificate(ctx,x);
618 end:
619         if (x != NULL) X509_free(x);
620         if (in != NULL) BIO_free(in);
621         return(ret);
622         }
623 #endif
624
625 int SSL_CTX_use_certificate_ASN1(ctx, len, d)
626 SSL_CTX *ctx;
627 int len;
628 unsigned char *d;
629         {
630         X509 *x;
631         int ret;
632
633         x=d2i_X509(NULL,&d,(long)len);
634         if (x == NULL)
635                 {
636                 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1,ERR_R_ASN1_LIB);
637                 return(0);
638                 }
639
640         ret=SSL_CTX_use_certificate(ctx,x);
641         X509_free(x);
642         return(ret);
643         }
644
645 #ifndef NO_RSA
646 int SSL_CTX_use_RSAPrivateKey(ctx, rsa)
647 SSL_CTX *ctx;
648 RSA *rsa;
649         {
650         int ret;
651         CERT *c;
652         EVP_PKEY *pkey;
653
654         if (rsa == NULL)
655                 {
656                 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
657                 return(0);
658                 }
659         if (ctx->default_cert == NULL)
660                 {
661                 c=ssl_cert_new();
662                 if (c == NULL)
663                         {
664                         SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY,ERR_R_MALLOC_FAILURE);
665                         return(0);
666                         }
667                 ctx->default_cert=c;
668                 }
669         c=ctx->default_cert;
670
671         if ((pkey=EVP_PKEY_new()) == NULL)
672                 {
673                 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY,ERR_R_EVP_LIB);
674                 return(0);
675                 }
676
677         CRYPTO_add(&rsa->references,1,CRYPTO_LOCK_RSA);
678         EVP_PKEY_assign_RSA(pkey,rsa);
679
680         ret=ssl_set_pkey(c,pkey);
681         EVP_PKEY_free(pkey);
682         return(ret);
683         }
684
685 #ifndef NO_STDIO
686 int SSL_CTX_use_RSAPrivateKey_file(ctx, file, type)
687 SSL_CTX *ctx;
688 char *file;
689 int type;
690         {
691         int j,ret=0;
692         BIO *in;
693         RSA *rsa=NULL;
694
695         in=BIO_new(BIO_s_file_internal());
696         if (in == NULL)
697                 {
698                 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,ERR_R_BUF_LIB);
699                 goto end;
700                 }
701
702         if (BIO_read_filename(in,file) <= 0)
703                 {
704                 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,ERR_R_SYS_LIB);
705                 goto end;
706                 }
707         if      (type == SSL_FILETYPE_ASN1)
708                 {
709                 j=ERR_R_ASN1_LIB;
710                 rsa=d2i_RSAPrivateKey_bio(in,NULL);
711                 }
712         else if (type == SSL_FILETYPE_PEM)
713                 {
714                 j=ERR_R_PEM_LIB;
715                 rsa=PEM_read_bio_RSAPrivateKey(in,NULL,
716                         ctx->default_passwd_callback);
717                 }
718         else
719                 {
720                 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
721                 goto end;
722                 }
723         if (rsa == NULL)
724                 {
725                 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,j);
726                 goto end;
727                 }
728         ret=SSL_CTX_use_RSAPrivateKey(ctx,rsa);
729         RSA_free(rsa);
730 end:
731         if (in != NULL) BIO_free(in);
732         return(ret);
733         }
734 #endif
735
736 int SSL_CTX_use_RSAPrivateKey_ASN1(ctx,d,len)
737 SSL_CTX *ctx;
738 unsigned char *d;
739 long len;
740         {
741         int ret;
742         unsigned char *p;
743         RSA *rsa;
744
745         p=d;
746         if ((rsa=d2i_RSAPrivateKey(NULL,&p,(long)len)) == NULL)
747                 {
748                 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
749                 return(0);
750                 }
751
752         ret=SSL_CTX_use_RSAPrivateKey(ctx,rsa);
753         RSA_free(rsa);
754         return(ret);
755         }
756 #endif /* !NO_RSA */
757
758 int SSL_CTX_use_PrivateKey(ctx, pkey)
759 SSL_CTX *ctx;
760 EVP_PKEY *pkey;
761         {
762         CERT *c;
763
764         if (pkey == NULL)
765                 {
766                 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
767                 return(0);
768                 }
769                 
770         if (ctx->default_cert == NULL)
771                 {
772                 c=ssl_cert_new();
773                 if (c == NULL)
774                         {
775                         SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY,ERR_R_MALLOC_FAILURE);
776                         return(0);
777                         }
778                 ctx->default_cert=c;
779                 }
780         c=ctx->default_cert;
781
782         return(ssl_set_pkey(c,pkey));
783         }
784
785 #ifndef NO_STDIO
786 int SSL_CTX_use_PrivateKey_file(ctx, file, type)
787 SSL_CTX *ctx;
788 char *file;
789 int type;
790         {
791         int j,ret=0;
792         BIO *in;
793         EVP_PKEY *pkey=NULL;
794
795         in=BIO_new(BIO_s_file_internal());
796         if (in == NULL)
797                 {
798                 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,ERR_R_BUF_LIB);
799                 goto end;
800                 }
801
802         if (BIO_read_filename(in,file) <= 0)
803                 {
804                 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,ERR_R_SYS_LIB);
805                 goto end;
806                 }
807         if (type == SSL_FILETYPE_PEM)
808                 {
809                 j=ERR_R_PEM_LIB;
810                 pkey=PEM_read_bio_PrivateKey(in,NULL,
811                         ctx->default_passwd_callback);
812                 }
813         else
814                 {
815                 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
816                 goto end;
817                 }
818         if (pkey == NULL)
819                 {
820                 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,j);
821                 goto end;
822                 }
823         ret=SSL_CTX_use_PrivateKey(ctx,pkey);
824         EVP_PKEY_free(pkey);
825 end:
826         if (in != NULL) BIO_free(in);
827         return(ret);
828         }
829 #endif
830
831 int SSL_CTX_use_PrivateKey_ASN1(type,ctx,d,len)
832 int type;
833 SSL_CTX *ctx;
834 unsigned char *d;
835 long len;
836         {
837         int ret;
838         unsigned char *p;
839         EVP_PKEY *pkey;
840
841         p=d;
842         if ((pkey=d2i_PrivateKey(type,NULL,&p,(long)len)) == NULL)
843                 {
844                 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
845                 return(0);
846                 }
847
848         ret=SSL_CTX_use_PrivateKey(ctx,pkey);
849         EVP_PKEY_free(pkey);
850         return(ret);
851         }
852
853