Fix a bundle of trailing spaces in several files
[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 "packet_locl.h"
13 #include <openssl/bio.h>
14 #include <openssl/objects.h>
15 #include <openssl/evp.h>
16 #include <openssl/x509.h>
17 #include <openssl/pem.h>
18
19 static int ssl_set_cert(CERT *c, X509 *x509);
20 static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey);
21
22 #define  SYNTHV1CONTEXT     (SSL_EXT_TLS1_2_AND_BELOW_ONLY \
23                              | SSL_EXT_CLIENT_HELLO \
24                              | SSL_EXT_TLS1_2_SERVER_HELLO \
25                              | SSL_EXT_IGNORE_ON_RESUMPTION)
26
27 int SSL_use_certificate(SSL *ssl, X509 *x)
28 {
29     int rv;
30     if (x == NULL) {
31         SSLerr(SSL_F_SSL_USE_CERTIFICATE, ERR_R_PASSED_NULL_PARAMETER);
32         return (0);
33     }
34     rv = ssl_security_cert(ssl, NULL, x, 0, 1);
35     if (rv != 1) {
36         SSLerr(SSL_F_SSL_USE_CERTIFICATE, rv);
37         return 0;
38     }
39
40     return (ssl_set_cert(ssl->cert, x));
41 }
42
43 int SSL_use_certificate_file(SSL *ssl, const char *file, int type)
44 {
45     int j;
46     BIO *in;
47     int ret = 0;
48     X509 *x = NULL;
49
50     in = BIO_new(BIO_s_file());
51     if (in == NULL) {
52         SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, ERR_R_BUF_LIB);
53         goto end;
54     }
55
56     if (BIO_read_filename(in, file) <= 0) {
57         SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB);
58         goto end;
59     }
60     if (type == SSL_FILETYPE_ASN1) {
61         j = ERR_R_ASN1_LIB;
62         x = d2i_X509_bio(in, NULL);
63     } else if (type == SSL_FILETYPE_PEM) {
64         j = ERR_R_PEM_LIB;
65         x = PEM_read_bio_X509(in, NULL, ssl->default_passwd_callback,
66                               ssl->default_passwd_callback_userdata);
67     } else {
68         SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, SSL_R_BAD_SSL_FILETYPE);
69         goto end;
70     }
71
72     if (x == NULL) {
73         SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, j);
74         goto end;
75     }
76
77     ret = SSL_use_certificate(ssl, x);
78  end:
79     X509_free(x);
80     BIO_free(in);
81     return (ret);
82 }
83
84 int SSL_use_certificate_ASN1(SSL *ssl, const unsigned char *d, int len)
85 {
86     X509 *x;
87     int ret;
88
89     x = d2i_X509(NULL, &d, (long)len);
90     if (x == NULL) {
91         SSLerr(SSL_F_SSL_USE_CERTIFICATE_ASN1, ERR_R_ASN1_LIB);
92         return (0);
93     }
94
95     ret = SSL_use_certificate(ssl, x);
96     X509_free(x);
97     return (ret);
98 }
99
100 #ifndef OPENSSL_NO_RSA
101 int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa)
102 {
103     EVP_PKEY *pkey;
104     int ret;
105
106     if (rsa == NULL) {
107         SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
108         return (0);
109     }
110     if ((pkey = EVP_PKEY_new()) == NULL) {
111         SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY, ERR_R_EVP_LIB);
112         return (0);
113     }
114
115     RSA_up_ref(rsa);
116     if (EVP_PKEY_assign_RSA(pkey, rsa) <= 0) {
117         RSA_free(rsa);
118         EVP_PKEY_free(pkey);
119         return 0;
120     }
121
122     ret = ssl_set_pkey(ssl->cert, pkey);
123     EVP_PKEY_free(pkey);
124     return (ret);
125 }
126 #endif
127
128 static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey)
129 {
130     int i;
131     i = ssl_cert_type(NULL, pkey);
132     if (i < 0) {
133         SSLerr(SSL_F_SSL_SET_PKEY, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
134         return (0);
135     }
136
137     if (c->pkeys[i].x509 != NULL) {
138         EVP_PKEY *pktmp;
139         pktmp = X509_get0_pubkey(c->pkeys[i].x509);
140         if (pktmp == NULL) {
141             SSLerr(SSL_F_SSL_SET_PKEY, ERR_R_MALLOC_FAILURE);
142             return 0;
143         }
144         /*
145          * The return code from EVP_PKEY_copy_parameters is deliberately
146          * ignored. Some EVP_PKEY types cannot do this.
147          */
148         EVP_PKEY_copy_parameters(pktmp, pkey);
149         ERR_clear_error();
150
151 #ifndef OPENSSL_NO_RSA
152         /*
153          * Don't check the public/private key, this is mostly for smart
154          * cards.
155          */
156         if (EVP_PKEY_id(pkey) == EVP_PKEY_RSA
157             && RSA_flags(EVP_PKEY_get0_RSA(pkey)) & RSA_METHOD_FLAG_NO_CHECK) ;
158         else
159 #endif
160         if (!X509_check_private_key(c->pkeys[i].x509, pkey)) {
161             X509_free(c->pkeys[i].x509);
162             c->pkeys[i].x509 = NULL;
163             return 0;
164         }
165     }
166
167     EVP_PKEY_free(c->pkeys[i].privatekey);
168     EVP_PKEY_up_ref(pkey);
169     c->pkeys[i].privatekey = pkey;
170     c->key = &(c->pkeys[i]);
171     return (1);
172 }
173
174 #ifndef OPENSSL_NO_RSA
175 int SSL_use_RSAPrivateKey_file(SSL *ssl, const char *file, int type)
176 {
177     int j, ret = 0;
178     BIO *in;
179     RSA *rsa = NULL;
180
181     in = BIO_new(BIO_s_file());
182     if (in == NULL) {
183         SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
184         goto end;
185     }
186
187     if (BIO_read_filename(in, file) <= 0) {
188         SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
189         goto end;
190     }
191     if (type == SSL_FILETYPE_ASN1) {
192         j = ERR_R_ASN1_LIB;
193         rsa = d2i_RSAPrivateKey_bio(in, NULL);
194     } else if (type == SSL_FILETYPE_PEM) {
195         j = ERR_R_PEM_LIB;
196         rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
197                                          ssl->default_passwd_callback,
198                                          ssl->default_passwd_callback_userdata);
199     } else {
200         SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
201         goto end;
202     }
203     if (rsa == NULL) {
204         SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, j);
205         goto end;
206     }
207     ret = SSL_use_RSAPrivateKey(ssl, rsa);
208     RSA_free(rsa);
209  end:
210     BIO_free(in);
211     return (ret);
212 }
213
214 int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, const unsigned char *d, long len)
215 {
216     int ret;
217     const unsigned char *p;
218     RSA *rsa;
219
220     p = d;
221     if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
222         SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
223         return (0);
224     }
225
226     ret = SSL_use_RSAPrivateKey(ssl, rsa);
227     RSA_free(rsa);
228     return (ret);
229 }
230 #endif                          /* !OPENSSL_NO_RSA */
231
232 int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey)
233 {
234     int ret;
235
236     if (pkey == NULL) {
237         SSLerr(SSL_F_SSL_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
238         return (0);
239     }
240     ret = ssl_set_pkey(ssl->cert, pkey);
241     return (ret);
242 }
243
244 int SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type)
245 {
246     int j, ret = 0;
247     BIO *in;
248     EVP_PKEY *pkey = NULL;
249
250     in = BIO_new(BIO_s_file());
251     if (in == NULL) {
252         SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
253         goto end;
254     }
255
256     if (BIO_read_filename(in, file) <= 0) {
257         SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
258         goto end;
259     }
260     if (type == SSL_FILETYPE_PEM) {
261         j = ERR_R_PEM_LIB;
262         pkey = PEM_read_bio_PrivateKey(in, NULL,
263                                        ssl->default_passwd_callback,
264                                        ssl->default_passwd_callback_userdata);
265     } else if (type == SSL_FILETYPE_ASN1) {
266         j = ERR_R_ASN1_LIB;
267         pkey = d2i_PrivateKey_bio(in, NULL);
268     } else {
269         SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
270         goto end;
271     }
272     if (pkey == NULL) {
273         SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, j);
274         goto end;
275     }
276     ret = SSL_use_PrivateKey(ssl, pkey);
277     EVP_PKEY_free(pkey);
278  end:
279     BIO_free(in);
280     return (ret);
281 }
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 int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type)
378 {
379     int j;
380     BIO *in;
381     int ret = 0;
382     X509 *x = NULL;
383
384     in = BIO_new(BIO_s_file());
385     if (in == NULL) {
386         SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_BUF_LIB);
387         goto end;
388     }
389
390     if (BIO_read_filename(in, file) <= 0) {
391         SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB);
392         goto end;
393     }
394     if (type == SSL_FILETYPE_ASN1) {
395         j = ERR_R_ASN1_LIB;
396         x = d2i_X509_bio(in, NULL);
397     } else if (type == SSL_FILETYPE_PEM) {
398         j = ERR_R_PEM_LIB;
399         x = PEM_read_bio_X509(in, NULL, ctx->default_passwd_callback,
400                               ctx->default_passwd_callback_userdata);
401     } else {
402         SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, SSL_R_BAD_SSL_FILETYPE);
403         goto end;
404     }
405
406     if (x == NULL) {
407         SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, j);
408         goto end;
409     }
410
411     ret = SSL_CTX_use_certificate(ctx, x);
412  end:
413     X509_free(x);
414     BIO_free(in);
415     return (ret);
416 }
417
418 int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len, const unsigned char *d)
419 {
420     X509 *x;
421     int ret;
422
423     x = d2i_X509(NULL, &d, (long)len);
424     if (x == NULL) {
425         SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1, ERR_R_ASN1_LIB);
426         return (0);
427     }
428
429     ret = SSL_CTX_use_certificate(ctx, x);
430     X509_free(x);
431     return (ret);
432 }
433
434 #ifndef OPENSSL_NO_RSA
435 int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa)
436 {
437     int ret;
438     EVP_PKEY *pkey;
439
440     if (rsa == NULL) {
441         SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
442         return (0);
443     }
444     if ((pkey = EVP_PKEY_new()) == NULL) {
445         SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_EVP_LIB);
446         return (0);
447     }
448
449     RSA_up_ref(rsa);
450     if (EVP_PKEY_assign_RSA(pkey, rsa) <= 0) {
451         RSA_free(rsa);
452         EVP_PKEY_free(pkey);
453         return 0;
454     }
455
456     ret = ssl_set_pkey(ctx->cert, pkey);
457     EVP_PKEY_free(pkey);
458     return (ret);
459 }
460
461 int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, int type)
462 {
463     int j, ret = 0;
464     BIO *in;
465     RSA *rsa = NULL;
466
467     in = BIO_new(BIO_s_file());
468     if (in == NULL) {
469         SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
470         goto end;
471     }
472
473     if (BIO_read_filename(in, file) <= 0) {
474         SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
475         goto end;
476     }
477     if (type == SSL_FILETYPE_ASN1) {
478         j = ERR_R_ASN1_LIB;
479         rsa = d2i_RSAPrivateKey_bio(in, NULL);
480     } else if (type == SSL_FILETYPE_PEM) {
481         j = ERR_R_PEM_LIB;
482         rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
483                                          ctx->default_passwd_callback,
484                                          ctx->default_passwd_callback_userdata);
485     } else {
486         SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
487         goto end;
488     }
489     if (rsa == NULL) {
490         SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, j);
491         goto end;
492     }
493     ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
494     RSA_free(rsa);
495  end:
496     BIO_free(in);
497     return (ret);
498 }
499
500 int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const unsigned char *d,
501                                    long len)
502 {
503     int ret;
504     const unsigned char *p;
505     RSA *rsa;
506
507     p = d;
508     if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
509         SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
510         return (0);
511     }
512
513     ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
514     RSA_free(rsa);
515     return (ret);
516 }
517 #endif                          /* !OPENSSL_NO_RSA */
518
519 int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey)
520 {
521     if (pkey == NULL) {
522         SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
523         return (0);
524     }
525     return (ssl_set_pkey(ctx->cert, pkey));
526 }
527
528 int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
529 {
530     int j, ret = 0;
531     BIO *in;
532     EVP_PKEY *pkey = NULL;
533
534     in = BIO_new(BIO_s_file());
535     if (in == NULL) {
536         SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
537         goto end;
538     }
539
540     if (BIO_read_filename(in, file) <= 0) {
541         SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
542         goto end;
543     }
544     if (type == SSL_FILETYPE_PEM) {
545         j = ERR_R_PEM_LIB;
546         pkey = PEM_read_bio_PrivateKey(in, NULL,
547                                        ctx->default_passwd_callback,
548                                        ctx->default_passwd_callback_userdata);
549     } else if (type == SSL_FILETYPE_ASN1) {
550         j = ERR_R_ASN1_LIB;
551         pkey = d2i_PrivateKey_bio(in, NULL);
552     } else {
553         SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
554         goto end;
555     }
556     if (pkey == NULL) {
557         SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, j);
558         goto end;
559     }
560     ret = SSL_CTX_use_PrivateKey(ctx, pkey);
561     EVP_PKEY_free(pkey);
562  end:
563     BIO_free(in);
564     return (ret);
565 }
566
567 int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx,
568                                 const unsigned char *d, long len)
569 {
570     int ret;
571     const unsigned char *p;
572     EVP_PKEY *pkey;
573
574     p = d;
575     if ((pkey = d2i_PrivateKey(type, NULL, &p, (long)len)) == NULL) {
576         SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
577         return (0);
578     }
579
580     ret = SSL_CTX_use_PrivateKey(ctx, pkey);
581     EVP_PKEY_free(pkey);
582     return (ret);
583 }
584
585 /*
586  * Read a file that contains our certificate in "PEM" format, possibly
587  * followed by a sequence of CA certificates that should be sent to the peer
588  * in the Certificate message.
589  */
590 static int use_certificate_chain_file(SSL_CTX *ctx, SSL *ssl, const char *file)
591 {
592     BIO *in;
593     int ret = 0;
594     X509 *x = NULL;
595     pem_password_cb *passwd_callback;
596     void *passwd_callback_userdata;
597
598     ERR_clear_error();          /* clear error stack for
599                                  * SSL_CTX_use_certificate() */
600
601     if (ctx != NULL) {
602         passwd_callback = ctx->default_passwd_callback;
603         passwd_callback_userdata = ctx->default_passwd_callback_userdata;
604     } else {
605         passwd_callback = ssl->default_passwd_callback;
606         passwd_callback_userdata = ssl->default_passwd_callback_userdata;
607     }
608
609     in = BIO_new(BIO_s_file());
610     if (in == NULL) {
611         SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_BUF_LIB);
612         goto end;
613     }
614
615     if (BIO_read_filename(in, file) <= 0) {
616         SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_SYS_LIB);
617         goto end;
618     }
619
620     x = PEM_read_bio_X509_AUX(in, NULL, passwd_callback,
621                               passwd_callback_userdata);
622     if (x == NULL) {
623         SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_PEM_LIB);
624         goto end;
625     }
626
627     if (ctx)
628         ret = SSL_CTX_use_certificate(ctx, x);
629     else
630         ret = SSL_use_certificate(ssl, x);
631
632     if (ERR_peek_error() != 0)
633         ret = 0;                /* Key/certificate mismatch doesn't imply
634                                  * ret==0 ... */
635     if (ret) {
636         /*
637          * If we could set up our certificate, now proceed to the CA
638          * certificates.
639          */
640         X509 *ca;
641         int r;
642         unsigned long err;
643
644         if (ctx)
645             r = SSL_CTX_clear_chain_certs(ctx);
646         else
647             r = SSL_clear_chain_certs(ssl);
648
649         if (r == 0) {
650             ret = 0;
651             goto end;
652         }
653
654         while ((ca = PEM_read_bio_X509(in, NULL, passwd_callback,
655                                        passwd_callback_userdata))
656                != NULL) {
657             if (ctx)
658                 r = SSL_CTX_add0_chain_cert(ctx, ca);
659             else
660                 r = SSL_add0_chain_cert(ssl, ca);
661             /*
662              * Note that we must not free ca if it was successfully added to
663              * the chain (while we must free the main certificate, since its
664              * reference count is increased by SSL_CTX_use_certificate).
665              */
666             if (!r) {
667                 X509_free(ca);
668                 ret = 0;
669                 goto end;
670             }
671         }
672         /* When the while loop ends, it's usually just EOF. */
673         err = ERR_peek_last_error();
674         if (ERR_GET_LIB(err) == ERR_LIB_PEM
675             && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)
676             ERR_clear_error();
677         else
678             ret = 0;            /* some real error */
679     }
680
681  end:
682     X509_free(x);
683     BIO_free(in);
684     return (ret);
685 }
686
687 int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file)
688 {
689     return use_certificate_chain_file(ctx, NULL, file);
690 }
691
692 int SSL_use_certificate_chain_file(SSL *ssl, const char *file)
693 {
694     return use_certificate_chain_file(NULL, ssl, file);
695 }
696
697 static int serverinfo_find_extension(const unsigned char *serverinfo,
698                                      size_t serverinfo_length,
699                                      unsigned int extension_type,
700                                      const unsigned char **extension_data,
701                                      size_t *extension_length)
702 {
703     PACKET pkt, data;
704
705     *extension_data = NULL;
706     *extension_length = 0;
707     if (serverinfo == NULL || serverinfo_length == 0)
708         return -1;
709
710     if (!PACKET_buf_init(&pkt, serverinfo, serverinfo_length))
711         return -1;
712
713     for (;;) {
714         unsigned int type = 0;
715         unsigned long context = 0;
716
717         /* end of serverinfo */
718         if (PACKET_remaining(&pkt) == 0)
719             return 0;           /* Extension not found */
720
721         if (!PACKET_get_net_4(&pkt, &context)
722                 || !PACKET_get_net_2(&pkt, &type)
723                 || !PACKET_get_length_prefixed_2(&pkt, &data))
724             return -1;
725
726         if (type == extension_type) {
727             *extension_data = PACKET_data(&data);
728             *extension_length = PACKET_remaining(&data);;
729             return 1;           /* Success */
730         }
731     }
732     /* Unreachable */
733 }
734
735 static int serverinfoex_srv_parse_cb(SSL *s, unsigned int ext_type,
736                                      unsigned int context,
737                                      const unsigned char *in,
738                                      size_t inlen, X509 *x, size_t chainidx,
739                                      int *al, void *arg)
740 {
741
742     if (inlen != 0) {
743         *al = SSL_AD_DECODE_ERROR;
744         return 0;
745     }
746
747     return 1;
748 }
749
750 static int serverinfo_srv_parse_cb(SSL *s, unsigned int ext_type,
751                                    const unsigned char *in,
752                                    size_t inlen, int *al, void *arg)
753 {
754     return serverinfoex_srv_parse_cb(s, ext_type, 0, in, inlen, NULL, 0, al,
755                                      arg);
756 }
757
758 static int serverinfoex_srv_add_cb(SSL *s, unsigned int ext_type,
759                                    unsigned int context,
760                                    const unsigned char **out,
761                                    size_t *outlen, X509 *x, size_t chainidx,
762                                    int *al, void *arg)
763 {
764     const unsigned char *serverinfo = NULL;
765     size_t serverinfo_length = 0;
766
767     /* We only support extensions for the first Certificate */
768     if ((context & SSL_EXT_TLS1_3_CERTIFICATE) != 0 && chainidx > 0)
769         return 0;
770
771     /* Is there serverinfo data for the chosen server cert? */
772     if ((ssl_get_server_cert_serverinfo(s, &serverinfo,
773                                         &serverinfo_length)) != 0) {
774         /* Find the relevant extension from the serverinfo */
775         int retval = serverinfo_find_extension(serverinfo, serverinfo_length,
776                                                ext_type, out, outlen);
777         if (retval == -1) {
778             *al = SSL_AD_INTERNAL_ERROR;
779             return -1;          /* Error */
780         }
781         if (retval == 0)
782             return 0;           /* No extension found, don't send extension */
783         return 1;               /* Send extension */
784     }
785     return 0;                   /* No serverinfo data found, don't send
786                                  * extension */
787 }
788
789 static int serverinfo_srv_add_cb(SSL *s, unsigned int ext_type,
790                                  const unsigned char **out, size_t *outlen,
791                                  int *al, void *arg)
792 {
793     return serverinfoex_srv_add_cb(s, ext_type, 0, out, outlen, NULL, 0, al,
794                                    arg);
795 }
796
797 /*
798  * With a NULL context, this function just checks that the serverinfo data
799  * parses correctly.  With a non-NULL context, it registers callbacks for
800  * the included extensions.
801  */
802 static int serverinfo_process_buffer(unsigned int version,
803                                      const unsigned char *serverinfo,
804                                      size_t serverinfo_length, SSL_CTX *ctx)
805 {
806     PACKET pkt;
807
808     if (serverinfo == NULL || serverinfo_length == 0)
809         return 0;
810
811     if (version != SSL_SERVERINFOV1 && version != SSL_SERVERINFOV2)
812         return 0;
813
814     if (!PACKET_buf_init(&pkt, serverinfo, serverinfo_length))
815         return 0;
816
817     while (PACKET_remaining(&pkt)) {
818         unsigned long context = 0;
819         unsigned int ext_type = 0;
820         PACKET data;
821
822         if ((version == SSL_SERVERINFOV2 && !PACKET_get_net_4(&pkt, &context))
823                 || !PACKET_get_net_2(&pkt, &ext_type)
824                 || !PACKET_get_length_prefixed_2(&pkt, &data))
825             return 0;
826
827         if (ctx == NULL)
828             continue;
829
830         /*
831          * The old style custom extensions API could be set separately for
832          * server/client, i.e. you could set one custom extension for a client,
833          * and *for the same extension in the same SSL_CTX* you could set a
834          * custom extension for the server as well. It seems quite weird to be
835          * setting a custom extension for both client and server in a single
836          * SSL_CTX - but theoretically possible. This isn't possible in the
837          * new API. Therefore, if we have V1 serverinfo we use the old API. We
838          * also use the old API even if we have V2 serverinfo but the context
839          * looks like an old style <= TLSv1.2 extension.
840          */
841         if (version == SSL_SERVERINFOV1 || context == SYNTHV1CONTEXT) {
842             if (!SSL_CTX_add_server_custom_ext(ctx, ext_type,
843                                                serverinfo_srv_add_cb,
844                                                NULL, NULL,
845                                                serverinfo_srv_parse_cb,
846                                                NULL))
847                 return 0;
848         } else {
849             if (!SSL_CTX_add_custom_ext(ctx, ext_type, context,
850                                         serverinfoex_srv_add_cb,
851                                         NULL, NULL,
852                                         serverinfoex_srv_parse_cb,
853                                         NULL))
854                 return 0;
855         }
856     }
857
858     return 1;
859 }
860
861 int SSL_CTX_use_serverinfo_ex(SSL_CTX *ctx, unsigned int version,
862                               const unsigned char *serverinfo,
863                               size_t serverinfo_length)
864 {
865     unsigned char *new_serverinfo;
866
867     if (ctx == NULL || serverinfo == NULL || serverinfo_length == 0) {
868         SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, ERR_R_PASSED_NULL_PARAMETER);
869         return 0;
870     }
871     if (!serverinfo_process_buffer(version, serverinfo, serverinfo_length,
872                                    NULL)) {
873         SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, SSL_R_INVALID_SERVERINFO_DATA);
874         return 0;
875     }
876     if (ctx->cert->key == NULL) {
877         SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, ERR_R_INTERNAL_ERROR);
878         return 0;
879     }
880     new_serverinfo = OPENSSL_realloc(ctx->cert->key->serverinfo,
881                                      serverinfo_length);
882     if (new_serverinfo == NULL) {
883         SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, ERR_R_MALLOC_FAILURE);
884         return 0;
885     }
886     ctx->cert->key->serverinfo = new_serverinfo;
887     memcpy(ctx->cert->key->serverinfo, serverinfo, serverinfo_length);
888     ctx->cert->key->serverinfo_length = serverinfo_length;
889
890     /*
891      * Now that the serverinfo is validated and stored, go ahead and
892      * register callbacks.
893      */
894     if (!serverinfo_process_buffer(version, serverinfo, serverinfo_length,
895                                    ctx)) {
896         SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, SSL_R_INVALID_SERVERINFO_DATA);
897         return 0;
898     }
899     return 1;
900 }
901
902 int SSL_CTX_use_serverinfo(SSL_CTX *ctx, const unsigned char *serverinfo,
903                            size_t serverinfo_length)
904 {
905     return SSL_CTX_use_serverinfo_ex(ctx, SSL_SERVERINFOV1, serverinfo,
906                                      serverinfo_length);
907 }
908
909 int SSL_CTX_use_serverinfo_file(SSL_CTX *ctx, const char *file)
910 {
911     unsigned char *serverinfo = NULL;
912     unsigned char *tmp;
913     size_t serverinfo_length = 0;
914     unsigned char *extension = 0;
915     long extension_length = 0;
916     char *name = NULL;
917     char *header = NULL;
918     char namePrefix1[] = "SERVERINFO FOR ";
919     char namePrefix2[] = "SERVERINFOV2 FOR ";
920     int ret = 0;
921     BIO *bin = NULL;
922     size_t num_extensions = 0, contextoff = 0;
923
924     if (ctx == NULL || file == NULL) {
925         SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_PASSED_NULL_PARAMETER);
926         goto end;
927     }
928
929     bin = BIO_new(BIO_s_file());
930     if (bin == NULL) {
931         SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_BUF_LIB);
932         goto end;
933     }
934     if (BIO_read_filename(bin, file) <= 0) {
935         SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_SYS_LIB);
936         goto end;
937     }
938
939     for (num_extensions = 0;; num_extensions++) {
940         unsigned int version;
941
942         if (PEM_read_bio(bin, &name, &header, &extension, &extension_length)
943             == 0) {
944             /*
945              * There must be at least one extension in this file
946              */
947             if (num_extensions == 0) {
948                 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
949                        SSL_R_NO_PEM_EXTENSIONS);
950                 goto end;
951             } else              /* End of file, we're done */
952                 break;
953         }
954         /* Check that PEM name starts with "BEGIN SERVERINFO FOR " */
955         if (strlen(name) < strlen(namePrefix1)) {
956             SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_PEM_NAME_TOO_SHORT);
957             goto end;
958         }
959         if (strncmp(name, namePrefix1, strlen(namePrefix1)) == 0) {
960             version = SSL_SERVERINFOV1;
961         } else {
962             if (strlen(name) < strlen(namePrefix2)) {
963                 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
964                        SSL_R_PEM_NAME_TOO_SHORT);
965                 goto end;
966             }
967             if (strncmp(name, namePrefix2, strlen(namePrefix2)) != 0) {
968                 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
969                        SSL_R_PEM_NAME_BAD_PREFIX);
970                 goto end;
971             }
972             version = SSL_SERVERINFOV2;
973         }
974         /*
975          * Check that the decoded PEM data is plausible (valid length field)
976          */
977         if (version == SSL_SERVERINFOV1) {
978             /* 4 byte header: 2 bytes type, 2 bytes len */
979             if (extension_length < 4
980                     || (extension[2] << 8) + extension[3]
981                        != extension_length - 4) {
982                 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_BAD_DATA);
983                 goto end;
984             }
985             /*
986              * File does not have a context value so we must take account of
987              * this later.
988              */
989             contextoff = 4;
990         } else {
991             /* 8 byte header: 4 bytes context, 2 bytes type, 2 bytes len */
992             if (extension_length < 8
993                     || (extension[6] << 8) + extension[7]
994                        != extension_length - 8) {
995                 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_BAD_DATA);
996                 goto end;
997             }
998         }
999         /* Append the decoded extension to the serverinfo buffer */
1000         tmp = OPENSSL_realloc(serverinfo, serverinfo_length + extension_length
1001                                           + contextoff);
1002         if (tmp == NULL) {
1003             SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_MALLOC_FAILURE);
1004             goto end;
1005         }
1006         serverinfo = tmp;
1007         if (contextoff > 0) {
1008             unsigned char *sinfo = serverinfo + serverinfo_length;
1009
1010             /* We know this only uses the last 2 bytes */
1011             sinfo[0] = 0;
1012             sinfo[1] = 0;
1013             sinfo[2] = (SYNTHV1CONTEXT >> 8) & 0xff;
1014             sinfo[3] = SYNTHV1CONTEXT & 0xff;
1015         }
1016         memcpy(serverinfo + serverinfo_length + contextoff,
1017                extension, extension_length);
1018         serverinfo_length += extension_length + contextoff;
1019
1020         OPENSSL_free(name);
1021         name = NULL;
1022         OPENSSL_free(header);
1023         header = NULL;
1024         OPENSSL_free(extension);
1025         extension = NULL;
1026     }
1027
1028     ret = SSL_CTX_use_serverinfo_ex(ctx, SSL_SERVERINFOV2, serverinfo,
1029                                     serverinfo_length);
1030  end:
1031     /* SSL_CTX_use_serverinfo makes a local copy of the serverinfo. */
1032     OPENSSL_free(name);
1033     OPENSSL_free(header);
1034     OPENSSL_free(extension);
1035     OPENSSL_free(serverinfo);
1036     BIO_free(bin);
1037     return ret;
1038 }