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