Support for fixed DH ciphersuites.
[openssl.git] / ssl / ssl_rsa.c
1 /* ssl/ssl_rsa.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  * 
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  * 
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  * 
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from 
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  * 
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * 
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58
59 #include <stdio.h>
60 #include "ssl_locl.h"
61 #include <openssl/bio.h>
62 #include <openssl/objects.h>
63 #include <openssl/evp.h>
64 #include <openssl/x509.h>
65 #include <openssl/pem.h>
66
67 static int ssl_set_cert(CERT *c, X509 *x509);
68 static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey);
69 int SSL_use_certificate(SSL *ssl, X509 *x)
70         {
71         if (x == NULL)
72                 {
73                 SSLerr(SSL_F_SSL_USE_CERTIFICATE,ERR_R_PASSED_NULL_PARAMETER);
74                 return(0);
75                 }
76         if (!ssl_cert_inst(&ssl->cert))
77                 {
78                 SSLerr(SSL_F_SSL_USE_CERTIFICATE,ERR_R_MALLOC_FAILURE);
79                 return(0);
80                 }
81         return(ssl_set_cert(ssl->cert,x));
82         }
83
84 #ifndef OPENSSL_NO_STDIO
85 int SSL_use_certificate_file(SSL *ssl, const char *file, int type)
86         {
87         int j;
88         BIO *in;
89         int ret=0;
90         X509 *x=NULL;
91
92         in=BIO_new(BIO_s_file_internal());
93         if (in == NULL)
94                 {
95                 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE,ERR_R_BUF_LIB);
96                 goto end;
97                 }
98
99         if (BIO_read_filename(in,file) <= 0)
100                 {
101                 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE,ERR_R_SYS_LIB);
102                 goto end;
103                 }
104         if (type == SSL_FILETYPE_ASN1)
105                 {
106                 j=ERR_R_ASN1_LIB;
107                 x=d2i_X509_bio(in,NULL);
108                 }
109         else if (type == SSL_FILETYPE_PEM)
110                 {
111                 j=ERR_R_PEM_LIB;
112                 x=PEM_read_bio_X509(in,NULL,ssl->ctx->default_passwd_callback,ssl->ctx->default_passwd_callback_userdata);
113                 }
114         else
115                 {
116                 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE,SSL_R_BAD_SSL_FILETYPE);
117                 goto end;
118                 }
119
120         if (x == NULL)
121                 {
122                 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE,j);
123                 goto end;
124                 }
125
126         ret=SSL_use_certificate(ssl,x);
127 end:
128         if (x != NULL) X509_free(x);
129         if (in != NULL) BIO_free(in);
130         return(ret);
131         }
132 #endif
133
134 int SSL_use_certificate_ASN1(SSL *ssl, const unsigned char *d, int len)
135         {
136         X509 *x;
137         int ret;
138
139         x=d2i_X509(NULL,&d,(long)len);
140         if (x == NULL)
141                 {
142                 SSLerr(SSL_F_SSL_USE_CERTIFICATE_ASN1,ERR_R_ASN1_LIB);
143                 return(0);
144                 }
145
146         ret=SSL_use_certificate(ssl,x);
147         X509_free(x);
148         return(ret);
149         }
150
151 #ifndef OPENSSL_NO_RSA
152 int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa)
153         {
154         EVP_PKEY *pkey;
155         int ret;
156
157         if (rsa == NULL)
158                 {
159                 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
160                 return(0);
161                 }
162         if (!ssl_cert_inst(&ssl->cert))
163                 {
164                 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY,ERR_R_MALLOC_FAILURE);
165                 return(0);
166                 }
167         if ((pkey=EVP_PKEY_new()) == NULL)
168                 {
169                 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY,ERR_R_EVP_LIB);
170                 return(0);
171                 }
172
173         RSA_up_ref(rsa);
174         EVP_PKEY_assign_RSA(pkey,rsa);
175
176         ret=ssl_set_pkey(ssl->cert,pkey);
177         EVP_PKEY_free(pkey);
178         return(ret);
179         }
180 #endif
181
182 static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey)
183         {
184         int i;
185         /* Special case for DH: check two DH certificate types for a match.
186          * This means for DH certificates we must set the certificate first.
187          */
188         if (pkey->type == EVP_PKEY_DH)
189                 {
190                 X509 *x;
191                 i = -1;
192                 x = c->pkeys[SSL_PKEY_DH_RSA].x509;
193                 if (x && X509_check_private_key(x, pkey))
194                                 i = SSL_PKEY_DH_RSA;
195                 x = c->pkeys[SSL_PKEY_DH_DSA].x509;
196                 if (i == -1 && x && X509_check_private_key(x, pkey))
197                                 i = SSL_PKEY_DH_DSA;
198                 ERR_clear_error();
199                 }
200         else 
201                 i=ssl_cert_type(NULL,pkey);
202         if (i < 0)
203                 {
204                 SSLerr(SSL_F_SSL_SET_PKEY,SSL_R_UNKNOWN_CERTIFICATE_TYPE);
205                 return(0);
206                 }
207
208         if (c->pkeys[i].x509 != NULL)
209                 {
210                 EVP_PKEY *pktmp;
211                 pktmp = X509_get_pubkey(c->pkeys[i].x509);
212                 EVP_PKEY_copy_parameters(pktmp,pkey);
213                 EVP_PKEY_free(pktmp);
214                 ERR_clear_error();
215
216 #ifndef OPENSSL_NO_RSA
217                 /* Don't check the public/private key, this is mostly
218                  * for smart cards. */
219                 if ((pkey->type == EVP_PKEY_RSA) &&
220                         (RSA_flags(pkey->pkey.rsa) & RSA_METHOD_FLAG_NO_CHECK))
221                         ;
222                 else
223 #endif
224                 if (!X509_check_private_key(c->pkeys[i].x509,pkey))
225                         {
226                         X509_free(c->pkeys[i].x509);
227                         c->pkeys[i].x509 = NULL;
228                         return 0;
229                         }
230                 }
231
232         if (c->pkeys[i].privatekey != NULL)
233                 EVP_PKEY_free(c->pkeys[i].privatekey);
234         CRYPTO_add(&pkey->references,1,CRYPTO_LOCK_EVP_PKEY);
235         c->pkeys[i].privatekey=pkey;
236         c->key= &(c->pkeys[i]);
237
238         c->valid=0;
239         return(1);
240         }
241
242 #ifndef OPENSSL_NO_RSA
243 #ifndef OPENSSL_NO_STDIO
244 int SSL_use_RSAPrivateKey_file(SSL *ssl, const char *file, int type)
245         {
246         int j,ret=0;
247         BIO *in;
248         RSA *rsa=NULL;
249
250         in=BIO_new(BIO_s_file_internal());
251         if (in == NULL)
252                 {
253                 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,ERR_R_BUF_LIB);
254                 goto end;
255                 }
256
257         if (BIO_read_filename(in,file) <= 0)
258                 {
259                 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,ERR_R_SYS_LIB);
260                 goto end;
261                 }
262         if      (type == SSL_FILETYPE_ASN1)
263                 {
264                 j=ERR_R_ASN1_LIB;
265                 rsa=d2i_RSAPrivateKey_bio(in,NULL);
266                 }
267         else if (type == SSL_FILETYPE_PEM)
268                 {
269                 j=ERR_R_PEM_LIB;
270                 rsa=PEM_read_bio_RSAPrivateKey(in,NULL,
271                         ssl->ctx->default_passwd_callback,ssl->ctx->default_passwd_callback_userdata);
272                 }
273         else
274                 {
275                 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
276                 goto end;
277                 }
278         if (rsa == NULL)
279                 {
280                 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,j);
281                 goto end;
282                 }
283         ret=SSL_use_RSAPrivateKey(ssl,rsa);
284         RSA_free(rsa);
285 end:
286         if (in != NULL) BIO_free(in);
287         return(ret);
288         }
289 #endif
290
291 int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, unsigned char *d, long len)
292         {
293         int ret;
294         const unsigned char *p;
295         RSA *rsa;
296
297         p=d;
298         if ((rsa=d2i_RSAPrivateKey(NULL,&p,(long)len)) == NULL)
299                 {
300                 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
301                 return(0);
302                 }
303
304         ret=SSL_use_RSAPrivateKey(ssl,rsa);
305         RSA_free(rsa);
306         return(ret);
307         }
308 #endif /* !OPENSSL_NO_RSA */
309
310 int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey)
311         {
312         int ret;
313
314         if (pkey == NULL)
315                 {
316                 SSLerr(SSL_F_SSL_USE_PRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
317                 return(0);
318                 }
319         if (!ssl_cert_inst(&ssl->cert))
320                 {
321                 SSLerr(SSL_F_SSL_USE_PRIVATEKEY,ERR_R_MALLOC_FAILURE);
322                 return(0);
323                 }
324         ret=ssl_set_pkey(ssl->cert,pkey);
325         return(ret);
326         }
327
328 #ifndef OPENSSL_NO_STDIO
329 int SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type)
330         {
331         int j,ret=0;
332         BIO *in;
333         EVP_PKEY *pkey=NULL;
334
335         in=BIO_new(BIO_s_file_internal());
336         if (in == NULL)
337                 {
338                 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,ERR_R_BUF_LIB);
339                 goto end;
340                 }
341
342         if (BIO_read_filename(in,file) <= 0)
343                 {
344                 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,ERR_R_SYS_LIB);
345                 goto end;
346                 }
347         if (type == SSL_FILETYPE_PEM)
348                 {
349                 j=ERR_R_PEM_LIB;
350                 pkey=PEM_read_bio_PrivateKey(in,NULL,
351                         ssl->ctx->default_passwd_callback,ssl->ctx->default_passwd_callback_userdata);
352                 }
353         else if (type == SSL_FILETYPE_ASN1)
354                 {
355                 j = ERR_R_ASN1_LIB;
356                 pkey = d2i_PrivateKey_bio(in,NULL);
357                 }
358         else
359                 {
360                 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
361                 goto end;
362                 }
363         if (pkey == NULL)
364                 {
365                 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,j);
366                 goto end;
367                 }
368         ret=SSL_use_PrivateKey(ssl,pkey);
369         EVP_PKEY_free(pkey);
370 end:
371         if (in != NULL) BIO_free(in);
372         return(ret);
373         }
374 #endif
375
376 int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const unsigned char *d, long len)
377         {
378         int ret;
379         const unsigned char *p;
380         EVP_PKEY *pkey;
381
382         p=d;
383         if ((pkey=d2i_PrivateKey(type,NULL,&p,(long)len)) == NULL)
384                 {
385                 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
386                 return(0);
387                 }
388
389         ret=SSL_use_PrivateKey(ssl,pkey);
390         EVP_PKEY_free(pkey);
391         return(ret);
392         }
393
394 int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x)
395         {
396         if (x == NULL)
397                 {
398                 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE,ERR_R_PASSED_NULL_PARAMETER);
399                 return(0);
400                 }
401         if (!ssl_cert_inst(&ctx->cert))
402                 {
403                 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE,ERR_R_MALLOC_FAILURE);
404                 return(0);
405                 }
406         return(ssl_set_cert(ctx->cert, x));
407         }
408
409 static int ssl_set_cert(CERT *c, X509 *x)
410         {
411         EVP_PKEY *pkey;
412         int i;
413
414         pkey=X509_get_pubkey(x);
415         if (pkey == NULL)
416                 {
417                 SSLerr(SSL_F_SSL_SET_CERT,SSL_R_X509_LIB);
418                 return(0);
419                 }
420
421         i=ssl_cert_type(x,pkey);
422         if (i < 0)
423                 {
424                 SSLerr(SSL_F_SSL_SET_CERT,SSL_R_UNKNOWN_CERTIFICATE_TYPE);
425                 EVP_PKEY_free(pkey);
426                 return(0);
427                 }
428
429         if (c->pkeys[i].privatekey != NULL)
430                 {
431                 EVP_PKEY_copy_parameters(pkey,c->pkeys[i].privatekey);
432                 ERR_clear_error();
433
434 #ifndef OPENSSL_NO_RSA
435                 /* Don't check the public/private key, this is mostly
436                  * for smart cards. */
437                 if ((c->pkeys[i].privatekey->type == EVP_PKEY_RSA) &&
438                         (RSA_flags(c->pkeys[i].privatekey->pkey.rsa) &
439                          RSA_METHOD_FLAG_NO_CHECK))
440                          ;
441                 else
442 #endif /* OPENSSL_NO_RSA */
443                 if (!X509_check_private_key(x,c->pkeys[i].privatekey))
444                         {
445                         /* don't fail for a cert/key mismatch, just free
446                          * current private key (when switching to a different
447                          * cert & key, first this function should be used,
448                          * then ssl_set_pkey */
449                         EVP_PKEY_free(c->pkeys[i].privatekey);
450                         c->pkeys[i].privatekey=NULL;
451                         /* clear error queue */
452                         ERR_clear_error();
453                         }
454                 }
455
456         EVP_PKEY_free(pkey);
457
458         if (c->pkeys[i].x509 != NULL)
459                 X509_free(c->pkeys[i].x509);
460         CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
461         c->pkeys[i].x509=x;
462         c->key= &(c->pkeys[i]);
463
464         c->valid=0;
465         return(1);
466         }
467
468 #ifndef OPENSSL_NO_STDIO
469 int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type)
470         {
471         int j;
472         BIO *in;
473         int ret=0;
474         X509 *x=NULL;
475
476         in=BIO_new(BIO_s_file_internal());
477         if (in == NULL)
478                 {
479                 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,ERR_R_BUF_LIB);
480                 goto end;
481                 }
482
483         if (BIO_read_filename(in,file) <= 0)
484                 {
485                 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,ERR_R_SYS_LIB);
486                 goto end;
487                 }
488         if (type == SSL_FILETYPE_ASN1)
489                 {
490                 j=ERR_R_ASN1_LIB;
491                 x=d2i_X509_bio(in,NULL);
492                 }
493         else if (type == SSL_FILETYPE_PEM)
494                 {
495                 j=ERR_R_PEM_LIB;
496                 x=PEM_read_bio_X509(in,NULL,ctx->default_passwd_callback,ctx->default_passwd_callback_userdata);
497                 }
498         else
499                 {
500                 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,SSL_R_BAD_SSL_FILETYPE);
501                 goto end;
502                 }
503
504         if (x == NULL)
505                 {
506                 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,j);
507                 goto end;
508                 }
509
510         ret=SSL_CTX_use_certificate(ctx,x);
511 end:
512         if (x != NULL) X509_free(x);
513         if (in != NULL) BIO_free(in);
514         return(ret);
515         }
516 #endif
517
518 int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len, const unsigned char *d)
519         {
520         X509 *x;
521         int ret;
522
523         x=d2i_X509(NULL,&d,(long)len);
524         if (x == NULL)
525                 {
526                 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1,ERR_R_ASN1_LIB);
527                 return(0);
528                 }
529
530         ret=SSL_CTX_use_certificate(ctx,x);
531         X509_free(x);
532         return(ret);
533         }
534
535 #ifndef OPENSSL_NO_RSA
536 int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa)
537         {
538         int ret;
539         EVP_PKEY *pkey;
540
541         if (rsa == NULL)
542                 {
543                 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
544                 return(0);
545                 }
546         if (!ssl_cert_inst(&ctx->cert))
547                 {
548                 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY,ERR_R_MALLOC_FAILURE);
549                 return(0);
550                 }
551         if ((pkey=EVP_PKEY_new()) == NULL)
552                 {
553                 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY,ERR_R_EVP_LIB);
554                 return(0);
555                 }
556
557         RSA_up_ref(rsa);
558         EVP_PKEY_assign_RSA(pkey,rsa);
559
560         ret=ssl_set_pkey(ctx->cert, pkey);
561         EVP_PKEY_free(pkey);
562         return(ret);
563         }
564
565 #ifndef OPENSSL_NO_STDIO
566 int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, int type)
567         {
568         int j,ret=0;
569         BIO *in;
570         RSA *rsa=NULL;
571
572         in=BIO_new(BIO_s_file_internal());
573         if (in == NULL)
574                 {
575                 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,ERR_R_BUF_LIB);
576                 goto end;
577                 }
578
579         if (BIO_read_filename(in,file) <= 0)
580                 {
581                 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,ERR_R_SYS_LIB);
582                 goto end;
583                 }
584         if      (type == SSL_FILETYPE_ASN1)
585                 {
586                 j=ERR_R_ASN1_LIB;
587                 rsa=d2i_RSAPrivateKey_bio(in,NULL);
588                 }
589         else if (type == SSL_FILETYPE_PEM)
590                 {
591                 j=ERR_R_PEM_LIB;
592                 rsa=PEM_read_bio_RSAPrivateKey(in,NULL,
593                         ctx->default_passwd_callback,ctx->default_passwd_callback_userdata);
594                 }
595         else
596                 {
597                 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
598                 goto end;
599                 }
600         if (rsa == NULL)
601                 {
602                 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,j);
603                 goto end;
604                 }
605         ret=SSL_CTX_use_RSAPrivateKey(ctx,rsa);
606         RSA_free(rsa);
607 end:
608         if (in != NULL) BIO_free(in);
609         return(ret);
610         }
611 #endif
612
613 int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const unsigned char *d, long len)
614         {
615         int ret;
616         const unsigned char *p;
617         RSA *rsa;
618
619         p=d;
620         if ((rsa=d2i_RSAPrivateKey(NULL,&p,(long)len)) == NULL)
621                 {
622                 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
623                 return(0);
624                 }
625
626         ret=SSL_CTX_use_RSAPrivateKey(ctx,rsa);
627         RSA_free(rsa);
628         return(ret);
629         }
630 #endif /* !OPENSSL_NO_RSA */
631
632 int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey)
633         {
634         if (pkey == NULL)
635                 {
636                 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
637                 return(0);
638                 }
639         if (!ssl_cert_inst(&ctx->cert))
640                 {
641                 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY,ERR_R_MALLOC_FAILURE);
642                 return(0);
643                 }
644         return(ssl_set_pkey(ctx->cert,pkey));
645         }
646
647 #ifndef OPENSSL_NO_STDIO
648 int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
649         {
650         int j,ret=0;
651         BIO *in;
652         EVP_PKEY *pkey=NULL;
653
654         in=BIO_new(BIO_s_file_internal());
655         if (in == NULL)
656                 {
657                 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,ERR_R_BUF_LIB);
658                 goto end;
659                 }
660
661         if (BIO_read_filename(in,file) <= 0)
662                 {
663                 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,ERR_R_SYS_LIB);
664                 goto end;
665                 }
666         if (type == SSL_FILETYPE_PEM)
667                 {
668                 j=ERR_R_PEM_LIB;
669                 pkey=PEM_read_bio_PrivateKey(in,NULL,
670                         ctx->default_passwd_callback,ctx->default_passwd_callback_userdata);
671                 }
672         else if (type == SSL_FILETYPE_ASN1)
673                 {
674                 j = ERR_R_ASN1_LIB;
675                 pkey = d2i_PrivateKey_bio(in,NULL);
676                 }
677         else
678                 {
679                 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
680                 goto end;
681                 }
682         if (pkey == NULL)
683                 {
684                 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,j);
685                 goto end;
686                 }
687         ret=SSL_CTX_use_PrivateKey(ctx,pkey);
688         EVP_PKEY_free(pkey);
689 end:
690         if (in != NULL) BIO_free(in);
691         return(ret);
692         }
693 #endif
694
695 int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx, const unsigned char *d,
696              long len)
697         {
698         int ret;
699         const unsigned char *p;
700         EVP_PKEY *pkey;
701
702         p=d;
703         if ((pkey=d2i_PrivateKey(type,NULL,&p,(long)len)) == NULL)
704                 {
705                 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
706                 return(0);
707                 }
708
709         ret=SSL_CTX_use_PrivateKey(ctx,pkey);
710         EVP_PKEY_free(pkey);
711         return(ret);
712         }
713
714
715 #ifndef OPENSSL_NO_STDIO
716 /* Read a file that contains our certificate in "PEM" format,
717  * possibly followed by a sequence of CA certificates that should be
718  * sent to the peer in the Certificate message.
719  */
720 int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file)
721         {
722         BIO *in;
723         int ret=0;
724         X509 *x=NULL;
725
726         ERR_clear_error(); /* clear error stack for SSL_CTX_use_certificate() */
727
728         in=BIO_new(BIO_s_file_internal());
729         if (in == NULL)
730                 {
731                 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE,ERR_R_BUF_LIB);
732                 goto end;
733                 }
734
735         if (BIO_read_filename(in,file) <= 0)
736                 {
737                 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE,ERR_R_SYS_LIB);
738                 goto end;
739                 }
740
741         x=PEM_read_bio_X509_AUX(in,NULL,ctx->default_passwd_callback,ctx->default_passwd_callback_userdata);
742         if (x == NULL)
743                 {
744                 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE,ERR_R_PEM_LIB);
745                 goto end;
746                 }
747
748         ret=SSL_CTX_use_certificate(ctx,x);
749         if (ERR_peek_error() != 0)
750                 ret = 0;  /* Key/certificate mismatch doesn't imply ret==0 ... */
751         if (ret)
752                 {
753                 /* If we could set up our certificate, now proceed to
754                  * the CA certificates.
755                  */
756                 X509 *ca;
757                 int r;
758                 unsigned long err;
759                 
760                 if (ctx->extra_certs != NULL) 
761                         {
762                         sk_X509_pop_free(ctx->extra_certs, X509_free);
763                         ctx->extra_certs = NULL;
764                         }
765
766                 while ((ca = PEM_read_bio_X509(in,NULL,ctx->default_passwd_callback,ctx->default_passwd_callback_userdata))
767                         != NULL)
768                         {
769                         r = SSL_CTX_add_extra_chain_cert(ctx, ca);
770                         if (!r) 
771                                 {
772                                 X509_free(ca);
773                                 ret = 0;
774                                 goto end;
775                                 }
776                         /* Note that we must not free r if it was successfully
777                          * added to the chain (while we must free the main
778                          * certificate, since its reference count is increased
779                          * by SSL_CTX_use_certificate). */
780                         }
781                 /* When the while loop ends, it's usually just EOF. */
782                 err = ERR_peek_last_error();
783                 if (ERR_GET_LIB(err) == ERR_LIB_PEM && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)
784                         ERR_clear_error();
785                 else 
786                         ret = 0; /* some real error */
787                 }
788
789 end:
790         if (x != NULL) X509_free(x);
791         if (in != NULL) BIO_free(in);
792         return(ret);
793         }
794 #endif