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