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