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