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