Fix missing return value checks
[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     int rv;
72     if (x == NULL) {
73         SSLerr(SSL_F_SSL_USE_CERTIFICATE, ERR_R_PASSED_NULL_PARAMETER);
74         return (0);
75     }
76     rv = ssl_security_cert(ssl, NULL, x, 0, 1);
77     if (rv != 1) {
78         SSLerr(SSL_F_SSL_USE_CERTIFICATE, rv);
79         return 0;
80     }
81
82     return (ssl_set_cert(ssl->cert, x));
83 }
84
85 #ifndef OPENSSL_NO_STDIO
86 int SSL_use_certificate_file(SSL *ssl, const char *file, int type)
87 {
88     int j;
89     BIO *in;
90     int ret = 0;
91     X509 *x = NULL;
92
93     in = BIO_new(BIO_s_file_internal());
94     if (in == NULL) {
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         SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB);
101         goto end;
102     }
103     if (type == SSL_FILETYPE_ASN1) {
104         j = ERR_R_ASN1_LIB;
105         x = d2i_X509_bio(in, NULL);
106     } else if (type == SSL_FILETYPE_PEM) {
107         j = ERR_R_PEM_LIB;
108         x = PEM_read_bio_X509(in, NULL, ssl->ctx->default_passwd_callback,
109                               ssl->ctx->default_passwd_callback_userdata);
110     } else {
111         SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, SSL_R_BAD_SSL_FILETYPE);
112         goto end;
113     }
114
115     if (x == NULL) {
116         SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, j);
117         goto end;
118     }
119
120     ret = SSL_use_certificate(ssl, x);
121  end:
122     if (x != NULL)
123         X509_free(x);
124     if (in != NULL)
125         BIO_free(in);
126     return (ret);
127 }
128 #endif
129
130 int SSL_use_certificate_ASN1(SSL *ssl, const unsigned char *d, int len)
131 {
132     X509 *x;
133     int ret;
134
135     x = d2i_X509(NULL, &d, (long)len);
136     if (x == NULL) {
137         SSLerr(SSL_F_SSL_USE_CERTIFICATE_ASN1, ERR_R_ASN1_LIB);
138         return (0);
139     }
140
141     ret = SSL_use_certificate(ssl, x);
142     X509_free(x);
143     return (ret);
144 }
145
146 #ifndef OPENSSL_NO_RSA
147 int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa)
148 {
149     EVP_PKEY *pkey;
150     int ret;
151
152     if (rsa == NULL) {
153         SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
154         return (0);
155     }
156     if ((pkey = EVP_PKEY_new()) == NULL) {
157         SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY, ERR_R_EVP_LIB);
158         return (0);
159     }
160
161     RSA_up_ref(rsa);
162     EVP_PKEY_assign_RSA(pkey, rsa);
163
164     ret = ssl_set_pkey(ssl->cert, pkey);
165     EVP_PKEY_free(pkey);
166     return (ret);
167 }
168 #endif
169
170 static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey)
171 {
172     int i;
173     /*
174      * Special case for DH: check two DH certificate types for a match. This
175      * means for DH certificates we must set the certificate first.
176      */
177     if (pkey->type == EVP_PKEY_DH) {
178         X509 *x;
179         i = -1;
180         x = c->pkeys[SSL_PKEY_DH_RSA].x509;
181         if (x && X509_check_private_key(x, pkey))
182             i = SSL_PKEY_DH_RSA;
183         x = c->pkeys[SSL_PKEY_DH_DSA].x509;
184         if (i == -1 && x && X509_check_private_key(x, pkey))
185             i = SSL_PKEY_DH_DSA;
186         ERR_clear_error();
187     } else
188         i = ssl_cert_type(NULL, pkey);
189     if (i < 0) {
190         SSLerr(SSL_F_SSL_SET_PKEY, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
191         return (0);
192     }
193
194     if (c->pkeys[i].x509 != NULL) {
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 #ifndef OPENSSL_NO_RSA
202         /*
203          * Don't check the public/private key, this is mostly for smart
204          * cards.
205          */
206         if ((pkey->type == EVP_PKEY_RSA) &&
207             (RSA_flags(pkey->pkey.rsa) & RSA_METHOD_FLAG_NO_CHECK)) ;
208         else
209 #endif
210         if (!X509_check_private_key(c->pkeys[i].x509, pkey)) {
211             X509_free(c->pkeys[i].x509);
212             c->pkeys[i].x509 = NULL;
213             return 0;
214         }
215     }
216
217     if (c->pkeys[i].privatekey != NULL)
218         EVP_PKEY_free(c->pkeys[i].privatekey);
219     CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
220     c->pkeys[i].privatekey = pkey;
221     c->key = &(c->pkeys[i]);
222
223     c->valid = 0;
224     return (1);
225 }
226
227 #ifndef OPENSSL_NO_RSA
228 # ifndef OPENSSL_NO_STDIO
229 int SSL_use_RSAPrivateKey_file(SSL *ssl, const char *file, int type)
230 {
231     int j, ret = 0;
232     BIO *in;
233     RSA *rsa = NULL;
234
235     in = BIO_new(BIO_s_file_internal());
236     if (in == NULL) {
237         SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
238         goto end;
239     }
240
241     if (BIO_read_filename(in, file) <= 0) {
242         SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
243         goto end;
244     }
245     if (type == SSL_FILETYPE_ASN1) {
246         j = ERR_R_ASN1_LIB;
247         rsa = d2i_RSAPrivateKey_bio(in, NULL);
248     } else if (type == SSL_FILETYPE_PEM) {
249         j = ERR_R_PEM_LIB;
250         rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
251                                          ssl->ctx->default_passwd_callback,
252                                          ssl->
253                                          ctx->default_passwd_callback_userdata);
254     } else {
255         SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
256         goto end;
257     }
258     if (rsa == NULL) {
259         SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, j);
260         goto end;
261     }
262     ret = SSL_use_RSAPrivateKey(ssl, rsa);
263     RSA_free(rsa);
264  end:
265     if (in != NULL)
266         BIO_free(in);
267     return (ret);
268 }
269 # endif
270
271 int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, const unsigned char *d, long len)
272 {
273     int ret;
274     const unsigned char *p;
275     RSA *rsa;
276
277     p = d;
278     if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
279         SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
280         return (0);
281     }
282
283     ret = SSL_use_RSAPrivateKey(ssl, rsa);
284     RSA_free(rsa);
285     return (ret);
286 }
287 #endif                          /* !OPENSSL_NO_RSA */
288
289 int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey)
290 {
291     int ret;
292
293     if (pkey == NULL) {
294         SSLerr(SSL_F_SSL_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
295         return (0);
296     }
297     ret = ssl_set_pkey(ssl->cert, pkey);
298     return (ret);
299 }
300
301 #ifndef OPENSSL_NO_STDIO
302 int SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type)
303 {
304     int j, ret = 0;
305     BIO *in;
306     EVP_PKEY *pkey = NULL;
307
308     in = BIO_new(BIO_s_file_internal());
309     if (in == NULL) {
310         SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
311         goto end;
312     }
313
314     if (BIO_read_filename(in, file) <= 0) {
315         SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
316         goto end;
317     }
318     if (type == SSL_FILETYPE_PEM) {
319         j = ERR_R_PEM_LIB;
320         pkey = PEM_read_bio_PrivateKey(in, NULL,
321                                        ssl->ctx->default_passwd_callback,
322                                        ssl->
323                                        ctx->default_passwd_callback_userdata);
324     } else if (type == SSL_FILETYPE_ASN1) {
325         j = ERR_R_ASN1_LIB;
326         pkey = d2i_PrivateKey_bio(in, NULL);
327     } else {
328         SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
329         goto end;
330     }
331     if (pkey == NULL) {
332         SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, j);
333         goto end;
334     }
335     ret = SSL_use_PrivateKey(ssl, pkey);
336     EVP_PKEY_free(pkey);
337  end:
338     if (in != NULL)
339         BIO_free(in);
340     return (ret);
341 }
342 #endif
343
344 int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const unsigned char *d,
345                             long len)
346 {
347     int ret;
348     const unsigned char *p;
349     EVP_PKEY *pkey;
350
351     p = d;
352     if ((pkey = d2i_PrivateKey(type, NULL, &p, (long)len)) == NULL) {
353         SSLerr(SSL_F_SSL_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
354         return (0);
355     }
356
357     ret = SSL_use_PrivateKey(ssl, pkey);
358     EVP_PKEY_free(pkey);
359     return (ret);
360 }
361
362 int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x)
363 {
364     int rv;
365     if (x == NULL) {
366         SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, ERR_R_PASSED_NULL_PARAMETER);
367         return (0);
368     }
369     rv = ssl_security_cert(NULL, ctx, x, 0, 1);
370     if (rv != 1) {
371         SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, rv);
372         return 0;
373     }
374     return (ssl_set_cert(ctx->cert, x));
375 }
376
377 static int ssl_set_cert(CERT *c, X509 *x)
378 {
379     EVP_PKEY *pkey;
380     int i;
381
382     pkey = X509_get_pubkey(x);
383     if (pkey == NULL) {
384         SSLerr(SSL_F_SSL_SET_CERT, SSL_R_X509_LIB);
385         return (0);
386     }
387
388     i = ssl_cert_type(x, pkey);
389     if (i < 0) {
390         SSLerr(SSL_F_SSL_SET_CERT, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
391         EVP_PKEY_free(pkey);
392         return (0);
393     }
394
395     if (c->pkeys[i].privatekey != NULL) {
396         EVP_PKEY_copy_parameters(pkey, c->pkeys[i].privatekey);
397         ERR_clear_error();
398
399 #ifndef OPENSSL_NO_RSA
400         /*
401          * Don't check the public/private key, this is mostly for smart
402          * cards.
403          */
404         if ((c->pkeys[i].privatekey->type == EVP_PKEY_RSA) &&
405             (RSA_flags(c->pkeys[i].privatekey->pkey.rsa) &
406              RSA_METHOD_FLAG_NO_CHECK)) ;
407         else
408 #endif                          /* OPENSSL_NO_RSA */
409         if (!X509_check_private_key(x, c->pkeys[i].privatekey)) {
410             /*
411              * don't fail for a cert/key mismatch, just free current private
412              * key (when switching to a different cert & key, first this
413              * function should be used, then ssl_set_pkey
414              */
415             EVP_PKEY_free(c->pkeys[i].privatekey);
416             c->pkeys[i].privatekey = NULL;
417             /* clear error queue */
418             ERR_clear_error();
419         }
420     }
421
422     EVP_PKEY_free(pkey);
423
424     if (c->pkeys[i].x509 != NULL)
425         X509_free(c->pkeys[i].x509);
426     CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
427     c->pkeys[i].x509 = x;
428     c->key = &(c->pkeys[i]);
429
430     c->valid = 0;
431     return (1);
432 }
433
434 #ifndef OPENSSL_NO_STDIO
435 int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type)
436 {
437     int j;
438     BIO *in;
439     int ret = 0;
440     X509 *x = NULL;
441
442     in = BIO_new(BIO_s_file_internal());
443     if (in == NULL) {
444         SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_BUF_LIB);
445         goto end;
446     }
447
448     if (BIO_read_filename(in, file) <= 0) {
449         SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB);
450         goto end;
451     }
452     if (type == SSL_FILETYPE_ASN1) {
453         j = ERR_R_ASN1_LIB;
454         x = d2i_X509_bio(in, NULL);
455     } else if (type == SSL_FILETYPE_PEM) {
456         j = ERR_R_PEM_LIB;
457         x = PEM_read_bio_X509(in, NULL, ctx->default_passwd_callback,
458                               ctx->default_passwd_callback_userdata);
459     } else {
460         SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, SSL_R_BAD_SSL_FILETYPE);
461         goto end;
462     }
463
464     if (x == NULL) {
465         SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, j);
466         goto end;
467     }
468
469     ret = SSL_CTX_use_certificate(ctx, x);
470  end:
471     if (x != NULL)
472         X509_free(x);
473     if (in != NULL)
474         BIO_free(in);
475     return (ret);
476 }
477 #endif
478
479 int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len,
480                                  const unsigned char *d)
481 {
482     X509 *x;
483     int ret;
484
485     x = d2i_X509(NULL, &d, (long)len);
486     if (x == NULL) {
487         SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1, ERR_R_ASN1_LIB);
488         return (0);
489     }
490
491     ret = SSL_CTX_use_certificate(ctx, x);
492     X509_free(x);
493     return (ret);
494 }
495
496 #ifndef OPENSSL_NO_RSA
497 int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa)
498 {
499     int ret;
500     EVP_PKEY *pkey;
501
502     if (rsa == NULL) {
503         SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
504         return (0);
505     }
506     if ((pkey = EVP_PKEY_new()) == NULL) {
507         SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_EVP_LIB);
508         return (0);
509     }
510
511     RSA_up_ref(rsa);
512     EVP_PKEY_assign_RSA(pkey, rsa);
513
514     ret = ssl_set_pkey(ctx->cert, pkey);
515     EVP_PKEY_free(pkey);
516     return (ret);
517 }
518
519 # ifndef OPENSSL_NO_STDIO
520 int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, int type)
521 {
522     int j, ret = 0;
523     BIO *in;
524     RSA *rsa = NULL;
525
526     in = BIO_new(BIO_s_file_internal());
527     if (in == NULL) {
528         SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
529         goto end;
530     }
531
532     if (BIO_read_filename(in, file) <= 0) {
533         SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
534         goto end;
535     }
536     if (type == SSL_FILETYPE_ASN1) {
537         j = ERR_R_ASN1_LIB;
538         rsa = d2i_RSAPrivateKey_bio(in, NULL);
539     } else if (type == SSL_FILETYPE_PEM) {
540         j = ERR_R_PEM_LIB;
541         rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
542                                          ctx->default_passwd_callback,
543                                          ctx->default_passwd_callback_userdata);
544     } else {
545         SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
546         goto end;
547     }
548     if (rsa == NULL) {
549         SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, j);
550         goto end;
551     }
552     ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
553     RSA_free(rsa);
554  end:
555     if (in != NULL)
556         BIO_free(in);
557     return (ret);
558 }
559 # endif
560
561 int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const unsigned char *d,
562                                    long len)
563 {
564     int ret;
565     const unsigned char *p;
566     RSA *rsa;
567
568     p = d;
569     if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
570         SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
571         return (0);
572     }
573
574     ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
575     RSA_free(rsa);
576     return (ret);
577 }
578 #endif                          /* !OPENSSL_NO_RSA */
579
580 int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey)
581 {
582     if (pkey == NULL) {
583         SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
584         return (0);
585     }
586     return (ssl_set_pkey(ctx->cert, pkey));
587 }
588
589 #ifndef OPENSSL_NO_STDIO
590 int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
591 {
592     int j, ret = 0;
593     BIO *in;
594     EVP_PKEY *pkey = NULL;
595
596     in = BIO_new(BIO_s_file_internal());
597     if (in == NULL) {
598         SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
599         goto end;
600     }
601
602     if (BIO_read_filename(in, file) <= 0) {
603         SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
604         goto end;
605     }
606     if (type == SSL_FILETYPE_PEM) {
607         j = ERR_R_PEM_LIB;
608         pkey = PEM_read_bio_PrivateKey(in, NULL,
609                                        ctx->default_passwd_callback,
610                                        ctx->default_passwd_callback_userdata);
611     } else if (type == SSL_FILETYPE_ASN1) {
612         j = ERR_R_ASN1_LIB;
613         pkey = d2i_PrivateKey_bio(in, NULL);
614     } else {
615         SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
616         goto end;
617     }
618     if (pkey == NULL) {
619         SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, j);
620         goto end;
621     }
622     ret = SSL_CTX_use_PrivateKey(ctx, pkey);
623     EVP_PKEY_free(pkey);
624  end:
625     if (in != NULL)
626         BIO_free(in);
627     return (ret);
628 }
629 #endif
630
631 int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx,
632                                 const unsigned char *d, long len)
633 {
634     int ret;
635     const unsigned char *p;
636     EVP_PKEY *pkey;
637
638     p = d;
639     if ((pkey = d2i_PrivateKey(type, NULL, &p, (long)len)) == NULL) {
640         SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
641         return (0);
642     }
643
644     ret = SSL_CTX_use_PrivateKey(ctx, pkey);
645     EVP_PKEY_free(pkey);
646     return (ret);
647 }
648
649 #ifndef OPENSSL_NO_STDIO
650 /*
651  * Read a file that contains our certificate in "PEM" format, possibly
652  * followed by a sequence of CA certificates that should be sent to the peer
653  * in the Certificate message.
654  */
655 int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file)
656 {
657     BIO *in;
658     int ret = 0;
659     X509 *x = NULL;
660
661     ERR_clear_error();          /* clear error stack for
662                                  * SSL_CTX_use_certificate() */
663
664     in = BIO_new(BIO_s_file_internal());
665     if (in == NULL) {
666         SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE, ERR_R_BUF_LIB);
667         goto end;
668     }
669
670     if (BIO_read_filename(in, file) <= 0) {
671         SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE, ERR_R_SYS_LIB);
672         goto end;
673     }
674
675     x = PEM_read_bio_X509_AUX(in, NULL, ctx->default_passwd_callback,
676                               ctx->default_passwd_callback_userdata);
677     if (x == NULL) {
678         SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE, ERR_R_PEM_LIB);
679         goto end;
680     }
681
682     ret = SSL_CTX_use_certificate(ctx, x);
683
684     if (ERR_peek_error() != 0)
685         ret = 0;                /* Key/certificate mismatch doesn't imply
686                                  * ret==0 ... */
687     if (ret) {
688         /*
689          * If we could set up our certificate, now proceed to the CA
690          * certificates.
691          */
692         X509 *ca;
693         int r;
694         unsigned long err;
695
696         if(!SSL_CTX_clear_chain_certs(ctx)) {
697             ret = 0;
698             goto end;
699         }
700
701         while ((ca = PEM_read_bio_X509(in, NULL,
702                                        ctx->default_passwd_callback,
703                                        ctx->default_passwd_callback_userdata))
704                != NULL) {
705             r = SSL_CTX_add0_chain_cert(ctx, ca);
706             if (!r) {
707                 X509_free(ca);
708                 ret = 0;
709                 goto end;
710             }
711             /*
712              * Note that we must not free r if it was successfully added to
713              * the chain (while we must free the main certificate, since its
714              * reference count is increased by SSL_CTX_use_certificate).
715              */
716         }
717         /* When the while loop ends, it's usually just EOF. */
718         err = ERR_peek_last_error();
719         if (ERR_GET_LIB(err) == ERR_LIB_PEM
720             && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)
721             ERR_clear_error();
722         else
723             ret = 0;            /* some real error */
724     }
725
726  end:
727     if (x != NULL)
728         X509_free(x);
729     if (in != NULL)
730         BIO_free(in);
731     return (ret);
732 }
733 #endif
734
735 #ifndef OPENSSL_NO_TLSEXT
736 static int serverinfo_find_extension(const unsigned char *serverinfo,
737                                      size_t serverinfo_length,
738                                      unsigned int extension_type,
739                                      const unsigned char **extension_data,
740                                      size_t *extension_length)
741 {
742     *extension_data = NULL;
743     *extension_length = 0;
744     if (serverinfo == NULL || serverinfo_length == 0)
745         return 0;
746     for (;;) {
747         unsigned int type = 0;
748         size_t len = 0;
749
750         /* end of serverinfo */
751         if (serverinfo_length == 0)
752             return -1;          /* Extension not found */
753
754         /* read 2-byte type field */
755         if (serverinfo_length < 2)
756             return 0;           /* Error */
757         type = (serverinfo[0] << 8) + serverinfo[1];
758         serverinfo += 2;
759         serverinfo_length -= 2;
760
761         /* read 2-byte len field */
762         if (serverinfo_length < 2)
763             return 0;           /* Error */
764         len = (serverinfo[0] << 8) + serverinfo[1];
765         serverinfo += 2;
766         serverinfo_length -= 2;
767
768         if (len > serverinfo_length)
769             return 0;           /* Error */
770
771         if (type == extension_type) {
772             *extension_data = serverinfo;
773             *extension_length = len;
774             return 1;           /* Success */
775         }
776
777         serverinfo += len;
778         serverinfo_length -= len;
779     }
780     /* Unreachable */
781 }
782
783 static int serverinfo_srv_parse_cb(SSL *s, unsigned int ext_type,
784                                    const unsigned char *in,
785                                    size_t inlen, int *al, void *arg)
786 {
787
788     if (inlen != 0) {
789         *al = SSL_AD_DECODE_ERROR;
790         return 0;
791     }
792
793     return 1;
794 }
795
796 static int serverinfo_srv_add_cb(SSL *s, unsigned int ext_type,
797                                  const unsigned char **out, size_t *outlen,
798                                  int *al, void *arg)
799 {
800     const unsigned char *serverinfo = NULL;
801     size_t serverinfo_length = 0;
802
803     /* Is there serverinfo data for the chosen server cert? */
804     if ((ssl_get_server_cert_serverinfo(s, &serverinfo,
805                                         &serverinfo_length)) != 0) {
806         /* Find the relevant extension from the serverinfo */
807         int retval = serverinfo_find_extension(serverinfo, serverinfo_length,
808                                                ext_type, out, outlen);
809         if (retval == 0)
810             return 0;           /* Error */
811         if (retval == -1)
812             return -1;          /* No extension found, don't send extension */
813         return 1;               /* Send extension */
814     }
815     return -1;                  /* No serverinfo data found, don't send
816                                  * extension */
817 }
818
819 /*
820  * With a NULL context, this function just checks that the serverinfo data
821  * parses correctly.  With a non-NULL context, it registers callbacks for
822  * the included extensions.
823  */
824 static int serverinfo_process_buffer(const unsigned char *serverinfo,
825                                      size_t serverinfo_length, SSL_CTX *ctx)
826 {
827     if (serverinfo == NULL || serverinfo_length == 0)
828         return 0;
829     for (;;) {
830         unsigned int ext_type = 0;
831         size_t len = 0;
832
833         /* end of serverinfo */
834         if (serverinfo_length == 0)
835             return 1;
836
837         /* read 2-byte type field */
838         if (serverinfo_length < 2)
839             return 0;
840         /* FIXME: check for types we understand explicitly? */
841
842         /* Register callbacks for extensions */
843         ext_type = (serverinfo[0] << 8) + serverinfo[1];
844         if (ctx && !SSL_CTX_add_server_custom_ext(ctx, ext_type,
845                                                   serverinfo_srv_add_cb,
846                                                   NULL, NULL,
847                                                   serverinfo_srv_parse_cb,
848                                                   NULL))
849             return 0;
850
851         serverinfo += 2;
852         serverinfo_length -= 2;
853
854         /* read 2-byte len field */
855         if (serverinfo_length < 2)
856             return 0;
857         len = (serverinfo[0] << 8) + serverinfo[1];
858         serverinfo += 2;
859         serverinfo_length -= 2;
860
861         if (len > serverinfo_length)
862             return 0;
863
864         serverinfo += len;
865         serverinfo_length -= len;
866     }
867 }
868
869 int SSL_CTX_use_serverinfo(SSL_CTX *ctx, const unsigned char *serverinfo,
870                            size_t serverinfo_length)
871 {
872     unsigned char *new_serverinfo;
873
874     if (ctx == NULL || serverinfo == NULL || serverinfo_length == 0) {
875         SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, ERR_R_PASSED_NULL_PARAMETER);
876         return 0;
877     }
878     if (!serverinfo_process_buffer(serverinfo, serverinfo_length, NULL)) {
879         SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, SSL_R_INVALID_SERVERINFO_DATA);
880         return 0;
881     }
882     if (ctx->cert->key == NULL) {
883         SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, ERR_R_INTERNAL_ERROR);
884         return 0;
885     }
886     new_serverinfo = OPENSSL_realloc(ctx->cert->key->serverinfo,
887                                      serverinfo_length);
888     if (new_serverinfo == NULL) {
889         SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, ERR_R_MALLOC_FAILURE);
890         return 0;
891     }
892     ctx->cert->key->serverinfo = new_serverinfo;
893     memcpy(ctx->cert->key->serverinfo, serverinfo, serverinfo_length);
894     ctx->cert->key->serverinfo_length = serverinfo_length;
895
896     /*
897      * Now that the serverinfo is validated and stored, go ahead and
898      * register callbacks.
899      */
900     if (!serverinfo_process_buffer(serverinfo, serverinfo_length, ctx)) {
901         SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, SSL_R_INVALID_SERVERINFO_DATA);
902         return 0;
903     }
904     return 1;
905 }
906
907 # ifndef OPENSSL_NO_STDIO
908 int SSL_CTX_use_serverinfo_file(SSL_CTX *ctx, const char *file)
909 {
910     unsigned char *serverinfo = NULL;
911     size_t serverinfo_length = 0;
912     unsigned char *extension = 0;
913     long extension_length = 0;
914     char *name = NULL;
915     char *header = NULL;
916     char namePrefix[] = "SERVERINFO FOR ";
917     int ret = 0;
918     BIO *bin = NULL;
919     size_t num_extensions = 0;
920
921     if (ctx == NULL || file == NULL) {
922         SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
923                ERR_R_PASSED_NULL_PARAMETER);
924         goto end;
925     }
926
927     bin = BIO_new(BIO_s_file_internal());
928     if (bin == NULL) {
929         SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_BUF_LIB);
930         goto end;
931     }
932     if (BIO_read_filename(bin, file) <= 0) {
933         SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_SYS_LIB);
934         goto end;
935     }
936
937     for (num_extensions = 0;; num_extensions++) {
938         if (PEM_read_bio(bin, &name, &header, &extension, &extension_length)
939             == 0) {
940             /*
941              * There must be at least one extension in this file
942              */
943             if (num_extensions == 0) {
944                 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
945                        SSL_R_NO_PEM_EXTENSIONS);
946                 goto end;
947             } else              /* End of file, we're done */
948                 break;
949         }
950         /* Check that PEM name starts with "BEGIN SERVERINFO FOR " */
951         if (strlen(name) < strlen(namePrefix)) {
952             SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
953                    SSL_R_PEM_NAME_TOO_SHORT);
954             goto end;
955         }
956         if (strncmp(name, namePrefix, strlen(namePrefix)) != 0) {
957             SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
958                    SSL_R_PEM_NAME_BAD_PREFIX);
959             goto end;
960         }
961         /*
962          * Check that the decoded PEM data is plausible (valid length field)
963          */
964         if (extension_length < 4
965             || (extension[2] << 8) + extension[3] != extension_length - 4) {
966             SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_BAD_DATA);
967             goto end;
968         }
969         /* Append the decoded extension to the serverinfo buffer */
970         serverinfo =
971             OPENSSL_realloc(serverinfo, serverinfo_length + extension_length);
972         if (serverinfo == NULL) {
973             SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_MALLOC_FAILURE);
974             goto end;
975         }
976         memcpy(serverinfo + serverinfo_length, extension, extension_length);
977         serverinfo_length += extension_length;
978
979         OPENSSL_free(name);
980         name = NULL;
981         OPENSSL_free(header);
982         header = NULL;
983         OPENSSL_free(extension);
984         extension = NULL;
985     }
986
987     ret = SSL_CTX_use_serverinfo(ctx, serverinfo, serverinfo_length);
988  end:
989     /* SSL_CTX_use_serverinfo makes a local copy of the serverinfo. */
990     OPENSSL_free(name);
991     OPENSSL_free(header);
992     OPENSSL_free(extension);
993     OPENSSL_free(serverinfo);
994     if (bin != NULL)
995         BIO_free(bin);
996     return ret;
997 }
998 # endif                         /* OPENSSL_NO_STDIO */
999 #endif                          /* OPENSSL_NO_TLSEXT */