The FreeRADIUS server $Id: a4e7f8c94e8038b7627bcf52356b7ed15955c1d7 $
Loading...
Searching...
No Matches
ctx.c
Go to the documentation of this file.
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17/**
18 * $Id: e4a6285f6eba36fc4f9fcc611214eed931b43e1e $
19 *
20 * @file tls/ctx.c
21 * @brief Initialise and configure SSL_CTX structures.
22 *
23 * @copyright 2026 Network RADIUS SAS (legal@networkradius.com)
24 */
25RCSID("$Id: e4a6285f6eba36fc4f9fcc611214eed931b43e1e $")
26USES_APPLE_DEPRECATED_API /* OpenSSL API has been deprecated by Apple */
27
28#ifdef WITH_TLS
29#define LOG_PREFIX "tls"
30#define _TLS_PRIVATE 1
31
32#include <freeradius-devel/tls/log.h>
33#include <freeradius-devel/tls/strerror.h>
34#include <freeradius-devel/util/base16.h>
35#include <freeradius-devel/util/debug.h>
36#include <freeradius-devel/util/misc.h>
37#include <freeradius-devel/util/syserror.h>
38
39#include "base.h"
40#include "utils.h"
41#include "dtls.h"
42#include "log.h"
43#include "cert.h"
44
45#include <openssl/rand.h>
46#include <openssl/dh.h>
47#include <openssl/x509v3.h>
48#include <openssl/provider.h>
49
50#ifndef OPENSSL_NO_ECDH
51static int ctx_ecdh_curve_set(SSL_CTX *ctx, char const *ecdh_curve, bool disable_single_dh_use)
52{
53 char *list;
54
55 if (!disable_single_dh_use) {
56 SSL_CTX_set_options(ctx, SSL_OP_SINGLE_ECDH_USE);
57 }
58
59 if (!ecdh_curve || !*ecdh_curve) return 0;
60
61 list = strdup(ecdh_curve);
62 if (SSL_CTX_set1_curves_list(ctx, list) == 0) {
63 free(list);
64 ERROR("Unknown ecdh_curve \"%s\"", ecdh_curve);
65 return -1;
66 }
67 free(list);
68 return 0;
69}
70#endif
71
72/*
73 * TODO: Check for the type of key exchange * like conf->dh_key
74 */
75static int ctx_dh_params_load(SSL_CTX *ctx, char *file)
76{
77 BIO *bio;
78 int ret;
79
80 EVP_PKEY *dh = NULL;
81
82 if (!file) return 0;
83
84 /*
85 * Prior to trying to load the file, check what OpenSSL will do with it.
86 *
87 * Certain downstreams (such as RHEL) will ignore user-provided dhparams
88 * in FIPS mode, unless the specified parameters are FIPS-approved.
89 * However, since OpenSSL >= 1.1.1 will automatically select parameters
90 * anyways, there's no point in attempting to load them.
91 *
92 * Change suggested by @t8m
93 */
94 if (EVP_default_properties_is_fips_enabled(NULL)) {
95 WARN(LOG_PREFIX ": Ignoring user-selected DH parameters in FIPS mode. Using defaults.");
96 return 0;
97 }
98
99 if ((bio = BIO_new_file(file, "r")) == NULL) {
100 ERROR("Unable to open DH file - %s", file);
101 return -1;
102 }
103
104 dh = PEM_read_bio_Parameters(bio, &dh);
105 BIO_free(bio);
106 if (!dh) {
107 WARN("Unable to set DH parameters. DH cipher suites may not work!");
108 WARN("Fix this by generating the DH parameter file");
109 return 0;
110 }
111
112 ret = SSL_CTX_set0_tmp_dh_pkey(ctx, dh);
113 if (ret == 0) {
114 EVP_PKEY_free(dh);
115 ERROR("Unable to set DH parameters");
116 return -1;
117 }
118
119 return 0;
120}
121
122static int tls_ctx_verify_chain_member(X509 **self_signed, SSL_CTX *ctx, X509 *to_verify,
123 fr_tls_chain_verify_mode_t verify_mode)
124{
125 fr_unix_time_t not_after;
126
127 STACK_OF(X509) *chain;
128 X509 *leaf;
129
130 leaf = SSL_CTX_get0_certificate(ctx);
131 if (!leaf) {
132 ERROR("Chain does not contain a valid leaf certificate");
133 return -1;
134 }
135
136 if (!SSL_CTX_get0_chain_certs(ctx, &chain)) {
137 fr_tls_log_perror(NULL, "Failed retrieving chain certificates");
138 return -1;
139 }
140
141 switch (fr_tls_cert_is_valid(NULL, &not_after, to_verify)) {
142 case -1:
143 fr_tls_chain_marker_log(NULL, L_ERR, chain, leaf, to_verify);
144 PERROR("Malformed certificate");
145 return -1;
146
147 case -2:
148 case -3:
149 switch (verify_mode) {
150 case FR_TLS_CHAIN_VERIFY_SOFT:
151 fr_tls_chain_marker_log(NULL, L_WARN, chain, leaf, to_verify);
152 PWARN("Certificate validation failed");
153 break;
154
155 case FR_TLS_CHAIN_VERIFY_HARD:
156 fr_tls_chain_marker_log(NULL, L_ERR, chain, leaf, to_verify);
157 PERROR("Certificate validation failed");
158 return -1;
159
160 default:
161 break;
162 }
163
164 }
165
166 /*
167 * Check for self-signed certs
168 */
169 switch (verify_mode) {
170 case FR_TLS_CHAIN_VERIFY_SOFT:
171 case FR_TLS_CHAIN_VERIFY_HARD:
172 /*
173 * There can be only one... self signed
174 * cert in a chain.
175 *
176 * We have to do this check manually
177 * because the OpenSSL functions will
178 * only check to see if it can build
179 * a chain, not that all certificates
180 * in the chain are used.
181 *
182 * Having multiple self-signed certificates
183 * usually indicates someone has copied
184 * the wrong certificates into the
185 * server.pem file.
186 */
187 if (X509_name_cmp(X509_get_subject_name(to_verify),
188 X509_get_issuer_name(to_verify)) == 0) {
189 if (*self_signed) {
190 switch (verify_mode) {
191 case FR_TLS_CHAIN_VERIFY_SOFT:
192 WARN("Found multiple self-signed certificates in chain");
193 WARN("First certificate was:");
194 fr_tls_chain_marker_log(NULL, L_WARN,
195 chain, leaf, *self_signed);
196
197 WARN("Second certificate was:");
198 fr_tls_chain_marker_log(NULL, L_WARN,
199 chain, leaf, to_verify);
200 break;
201
202 case FR_TLS_CHAIN_VERIFY_HARD:
203 ERROR("Found multiple self-signed certificates in chain");
204 ERROR("First certificate was:");
205 fr_tls_chain_marker_log(NULL, L_ERR,
206 chain, leaf, *self_signed);
207
208 ERROR("Second certificate was:");
209 fr_tls_chain_marker_log(NULL, L_ERR,
210 chain, leaf, to_verify);
211 return -1;
212
213 default:
214 break;
215 }
216 }
217 *self_signed = to_verify;
218 }
219 break;
220
221 default:
222 break;
223 }
224
225 return 0;
226}
227
228static int tls_ctx_load_cert_chain(SSL_CTX *ctx, fr_tls_chain_conf_t *chain, bool allow_multi_self_signed)
229{
230 int i;
231
232 /*
233 * The chain section loaded the files when it was parsed,
234 * each SSL_CTX just takes references.
235 */
236 fr_assert(chain->leaf && chain->private_key);
237
238 if (SSL_CTX_use_certificate(ctx, chain->leaf) != 1) {
239 fr_tls_log_perror(NULL, "Failed setting certificate from \"%s\"", chain->certificate_file);
240 return -1;
241 }
242
243DIAG_OFF_OPTIONAL(used-but-marked-unused) /* fix spurious warnings for sk macros */
244 for (i = 0; i < sk_X509_num(chain->extra); i++) {
245 if (SSL_CTX_add1_chain_cert(ctx, sk_X509_value(chain->extra, i)) != 1) {
246 fr_tls_log_perror(NULL, "Failed adding certificate %d to the chain for \"%s\"",
247 i + 1, chain->certificate_file);
248 return -1;
249 }
250 }
251DIAG_ON_OPTIONAL(used-but-marked-unused)
252
253 if (SSL_CTX_use_PrivateKey(ctx, chain->private_key) != 1) {
254 fr_tls_log_perror(NULL, "Failed setting private key from \"%s\"", chain->private_key_file);
255 return -1;
256 }
257
258 /*
259 * Loop over the certificates checking validity periods.
260 * SSL_CTX_build_cert_chain does this too, but we can
261 * produce significantly better errors here.
262 */
263 {
264 X509 *self_signed = NULL;
265 STACK_OF(X509) *our_chain;
266
267 if (tls_ctx_verify_chain_member(&self_signed, ctx, SSL_CTX_get0_certificate(ctx),
268 chain->verify_mode) < 0) return -1;
269
270 if (!SSL_CTX_get0_chain_certs(ctx, &our_chain)) {
271 fr_tls_log_perror(NULL, "Failed retrieving chain certificates");
272 return -1;
273 }
274
275 if (allow_multi_self_signed) self_signed = NULL;
276
277DIAG_OFF_OPTIONAL(used-but-marked-unused) /* fix spurious warnings for sk macros */
278 for (i = sk_X509_num(our_chain); i > 0 ; i--) {
279 /*
280 * SSL_CTX_use_certificate_chain_file set the
281 * current cert to be the one loaded from
282 * that pem file.
283 */
284 if (tls_ctx_verify_chain_member(&self_signed, ctx, sk_X509_value(our_chain, i - 1),
285 chain->verify_mode) < 0) return -1;
286
287 if (allow_multi_self_signed) self_signed = NULL;
288 }
289DIAG_ON_OPTIONAL(used-but-marked-unused) /* fix spurious warnings for sk macros */
290 }
291
292 {
293 int mode = SSL_BUILD_CHAIN_FLAG_CHECK;
294
295 if (!chain->include_root_ca) mode |= SSL_BUILD_CHAIN_FLAG_NO_ROOT;
296
297 /*
298 * Explicitly check that the certificate chain
299 * we just loaded is sane.
300 *
301 * This operates on the last loaded certificate.
302 */
303 switch (chain->verify_mode) {
304 case FR_TLS_CHAIN_VERIFY_NONE:
305 mode |= SSL_BUILD_CHAIN_FLAG_CLEAR_ERROR | SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR;
306 (void)SSL_CTX_build_cert_chain(ctx, mode);
307 break;
308
309 /*
310 * Seems to be a bug where
311 * SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR trashes the error,
312 * so have the function fail as normal.
313 */
314 case FR_TLS_CHAIN_VERIFY_SOFT:
315 if (!SSL_CTX_build_cert_chain(ctx, mode)) {
316 fr_tls_strerror_printf(NULL);
317 PWARN("Failed verifying chain");
318 }
319 break;
320
321 case FR_TLS_CHAIN_VERIFY_HARD:
322 if (!SSL_CTX_build_cert_chain(ctx, mode)) {
323 fr_tls_strerror_printf(NULL);
324 PERROR("Failed verifying chain");
325 return -1;
326 }
327 break;
328
329 default:
330 break;
331 }
332 }
333
334 return 0;
335}
336
337/** Map the configured version numbers onto the DTLS ones
338 *
339 * DTLS has 1.0 and 1.2, and nothing else. There is no DTLS 1.1, and this
340 * OpenSSL has no DTLS 1.3: dtls1.h defines DTLS_MAX_VERSION as
341 * DTLS1_2_VERSION.
342 *
343 * The version numbers also run backwards. DTLS1_VERSION is 0xFEFF and
344 * DTLS1_2_VERSION is 0xFEFD, this function fixes that mapping.
345 *
346 * @param[in] ctx to configure.
347 * @param[in] conf to read the versions from.
348 * @return
349 * - 0 on success.
350 * - -1 on failure.
351 */
352static inline CC_HINT(always_inline)
353int tls_ctx_version_set_dtls(SSL_CTX *ctx, fr_tls_conf_t const *conf)
354{
355 int version;
356
357 if (conf->tls_max_version > (float) 0.0) {
358 if (conf->tls_min_version > conf->tls_max_version) {
359 ERROR("tls_min_version (%f) must be <= tls_max_version (%f)",
360 (double)conf->tls_min_version, (double)conf->tls_max_version);
361 return -1;
362 }
363
364 /*
365 * 1.3 and above allow more than exists, and the most
366 * recent DTLS there is satisfies them.
367 */
368 if (conf->tls_max_version >= (float) 1.2) {
369 version = DTLS1_2_VERSION;
370
371 } else if (conf->tls_max_version >= (float) 1.1) {
372 ERROR("There is no DTLS 1.1. Set tls_max_version to 1.0 or 1.2");
373 return -1;
374
375 } else if (conf->tls_max_version >= (float) 1.0) {
376 version = DTLS1_VERSION;
377 WARN("DTLS 1.0 is insecure and SHOULD NOT be used");
378 WARN("tls_max_version SHOULD be 1.2");
379
380 } else {
381 ERROR("tls_max_version must be >= 1.0");
382 return -1;
383 }
384
385 if (!SSL_CTX_set_max_proto_version(ctx, version)) {
386 fr_tls_log_perror(NULL, "Failed setting DTLS maximum version");
387 return -1;
388 }
389 }
390
391 /*
392 * A minimum which does not exist cannot be rounded down to one
393 * which does, as that would hand the administrator a weaker
394 * connection than the one asked for.
395 */
396 if (conf->tls_min_version >= (float) 1.3) {
397 ERROR("DTLS 1.3 is not supported by this version of OpenSSL");
398 return -1;
399
400 } else if (conf->tls_min_version >= (float) 1.2) {
401 version = DTLS1_2_VERSION;
402
403 } else if (conf->tls_min_version >= (float) 1.1) {
404 ERROR("There is no DTLS 1.1. Set tls_min_version to 1.0 or 1.2");
405 return -1;
406
407 } else if (conf->tls_min_version >= (float) 1.0) {
408 version = DTLS1_VERSION;
409 WARN("DTLS 1.0 is insecure and SHOULD NOT be used");
410 WARN("tls_min_version SHOULD be 1.2");
411
412 } else {
413 ERROR("tls_min_version must be >= 1.0 as SSLv2 and SSLv3 are permanently disabled");
414 return -1;
415 }
416
417 if (!SSL_CTX_set_min_proto_version(ctx, version)) {
418 fr_tls_log_perror(NULL, "Failed setting DTLS minimum version");
419 return -1;
420 }
421
422 return 0;
423}
424
425static inline CC_HINT(always_inline)
426int tls_ctx_version_set(UNUSED int *ctx_options, SSL_CTX *ctx, fr_tls_conf_t const *conf)
427{
428 if (conf->socket_type == SOCK_DGRAM) return tls_ctx_version_set_dtls(ctx, conf);
429
430 /*
431 * SSL_CTX_set_(min|max)_proto_version was included in OpenSSL 1.1.0
432 *
433 * This version already defines macros for TLS1_2_VERSION and
434 * below, so we don't need to check for them explicitly.
435 *
436 * TLS1_3_VERSION is available in OpenSSL 1.1.1.
437 *
438 * TLS1_4_VERSION does not exist yet. But we allow it
439 * only if it is explicitly permitted by the
440 * administrator.
441 */
442 if (conf->tls_max_version > (float) 0.0) {
443 int max_version = 0;
444
445 if (conf->tls_min_version > conf->tls_max_version) {
446 /*
447 * %f is actually %lg now (double). Compile complains about
448 * implicit promotion unless we cast args to double.
449 */
450 ERROR("tls_min_version (%f) must be <= tls_max_version (%f)",
451 (double)conf->tls_min_version, (double)conf->tls_max_version);
452 error:
453 return -1;
454 }
455
456 if (conf->tls_max_version < (float) 1.0) {
457 ERROR("tls_max_version must be >= 1.0 as SSLv2 and SSLv3 are permanently disabled");
458 goto error;
459 }
460
461# ifdef TLS1_4_VERSION
462 else if (conf->tls_max_version >= (float) 1.4) max_version = TLS1_4_VERSION;
463# endif
464 else if (conf->tls_max_version >= (float) 1.3) max_version = TLS1_3_VERSION;
465 else if (conf->tls_max_version >= (float) 1.2) max_version = TLS1_2_VERSION;
466 else if (conf->tls_max_version >= (float) 1.1) max_version = TLS1_1_VERSION;
467 else max_version = TLS1_VERSION;
468
469 /*
470 * Complain about insecure TLS versions.
471 */
472 if (max_version < TLS1_2_VERSION) {
473 WARN("TLS 1.0 and 1.1 are insecure and SHOULD NOT be used");
474 WARN("tls_max_version SHOULD be 1.2 or greater");
475 }
476
477 if (!SSL_CTX_set_max_proto_version(ctx, max_version)) {
478 fr_tls_log_perror(NULL, "Failed setting TLS maximum version");
479 goto error;
480 }
481 }
482
483 {
484 int min_version;
485
486 if (conf->tls_min_version < (float) 1.0) {
487 ERROR("tls_min_version must be >= 1.0 as SSLv2 and SSLv3 are permanently disabled");
488 goto error;
489 }
490# ifdef TLS1_4_VERSION
491 else if (conf->tls_min_version >= (float) 1.4) min_version = TLS1_4_VERSION;
492# endif
493 else if (conf->tls_min_version >= (float) 1.3) min_version = TLS1_3_VERSION;
494 else if (conf->tls_min_version >= (float) 1.2) min_version = TLS1_2_VERSION;
495 else if (conf->tls_min_version >= (float) 1.1) min_version = TLS1_1_VERSION;
496 else min_version = TLS1_VERSION;
497
498 /*
499 * Complain about insecure TLS versions.
500 */
501 if (min_version < TLS1_2_VERSION) {
502 WARN("TLS 1.0 and 1.1 are insecure and SHOULD NOT be used");
503 WARN("tls_min_version SHOULD be 1.2 or greater");
504 }
505
506 if (!SSL_CTX_set_min_proto_version(ctx, min_version)) {
507 fr_tls_log_perror(NULL, "Failed setting TLS minimum version");
508 goto error;
509 }
510 }
511
512 return 0;
513}
514
515/** Create SSL context
516 *
517 * - Load the trusted CAs
518 * - Load the Private key & the certificate
519 * - Set the Context options & Verify options
520 *
521 * @param conf to read settings from, including `socket_type`.
522 * @param client If true SSL_CTX will be configured as a client context.
523 * @return
524 * - A new SSL_CTX on success.
525 * - NULL on failure.
526 */
527SSL_CTX *fr_tls_ctx_alloc(fr_tls_conf_t const *conf, bool client)
528{
529 SSL_CTX *ctx;
530 X509_STORE *verify_store = NULL;
531 int ctx_options = 0;
532 int mode= SSL_MODE_ASYNC;
533
534
535 /*
536 * SOCK_SEQPACKET is refused rather than guessed at: OpenSSL has
537 * DTLS over SCTP, and nothing here has been written or tested
538 * for it.
539 */
540 switch (conf->socket_type) {
541
542 /*
543 * Zero means the application never set it, which every caller other than DTLS does. For EAP,
544 * the configuration is in module structure, and is mprotect()'d.
545 */
546 case 0:
547 case SOCK_STREAM:
548 case SOCK_DGRAM:
549 break;
550
551 default:
552 ERROR("Unsupported socket type %d for a TLS context, expected SOCK_STREAM (%d) "
553 "or SOCK_DGRAM (%d)", conf->socket_type, SOCK_STREAM, SOCK_DGRAM);
554 return NULL;
555 }
556
557 ctx = SSL_CTX_new((conf->socket_type == SOCK_DGRAM) ? DTLS_method() : TLS_method());
558 if (!ctx) {
559 fr_tls_log_perror(NULL, "Failed creating TLS context");
560 return NULL;
561 }
562
563 /*
564 * Save the config on the context so that callbacks which
565 * only get SSL_CTX* e.g. session persistence, can get at it.
566 */
567 SSL_CTX_set_ex_data(ctx, FR_TLS_EX_INDEX_CONF, UNCONST(void *, conf));
568
569 /*
570 * Identify the type of certificates that needs to be loaded
571 */
572#ifdef PSK_MAX_IDENTITY_LEN
573 /*
574 * The virtual server has a `load psk` section, so the
575 * configuration MUST NOT also set a fixed key in a `psk` section.
576 */
577 if (conf->load_psk) {
578 if (conf->psk.identity || conf->psk.key) {
579 ERROR("Invalid PSK Configuration: a fixed key is set in the psk section and the virtual server "
580 "has a \"load psk\" section. Remove one of the two");
581 error:
582 SSL_CTX_free(ctx);
583 return NULL;
584 }
585
586 /*
587 * A fixed key requires both `psk.identity` and `psk.key`.
588 */
589 } else if (conf->psk.identity) {
590 size_t key_len = conf->psk.key ? talloc_array_length(conf->psk.key) : 0;
591
592 if (!*conf->psk.identity) {
593 ERROR("Invalid PSK Configuration: psk.identity is empty");
594 goto error;
595 }
596
597 if (key_len == 0) {
598 ERROR("Invalid PSK Configuration: psk.identity is set but psk.key is missing or empty");
599 goto error;
600 }
601
602 if (key_len > PSK_MAX_PSK_LEN) {
603 ERROR("Invalid PSK Configuration: psk.key is %zu bytes, OpenSSL accepts at most %d bytes",
604 key_len, PSK_MAX_PSK_LEN);
605 goto error;
606 }
607
608 } else if (conf->psk.key) {
609 ERROR("Invalid PSK Configuration: psk.key is set but psk.identity is not set");
610 goto error;
611 }
612
613 /*
614 * Set the server PSK callback if necessary.
615 */
616 if (!client && (conf->psk.identity || conf->load_psk)) {
617 SSL_CTX_set_psk_server_callback(ctx, fr_tls_session_psk_server_cb);
618 }
619
620 /*
621 * A fixed key takes the place of the certificate chain, so a
622 * configuration with a fixed key skips the chain loading below.
623 */
624 if (conf->psk.identity) {
625 if (client) {
626 SSL_CTX_set_psk_client_callback(ctx, fr_tls_session_psk_client_cb);
627 }
628
629 SSL_CTX_set_mode(ctx, mode);
630 goto post_ca;
631 }
632#endif
633
634 /*
635 * OpenSSL will automatically create certificate chains,
636 * unless we tell it to not do that. The problem is that
637 * it sometimes gets the chains right from a certificate
638 * signature view, but wrong from the clients view.
639 *
640 * It's better just to have users specify the complete
641 * chains.
642 */
643 mode |= SSL_MODE_NO_AUTO_CHAIN;
644
645 if (client) {
646 mode |= SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER;
647 mode |= SSL_MODE_AUTO_RETRY;
648 }
649
650 SSL_CTX_set_mode(ctx, mode);
651
652 /*
653 * Initialise a separate store for verifying user
654 * certificates.
655 *
656 * This makes the configuration cleaner as there's
657 * no mixing of chain certs and user certs.
658 */
659 MEM(verify_store = X509_STORE_new());
660
661 /* Sets OpenSSL's (CERT *)->verify_store, overring (SSL_CTX *)->cert_store */
662 SSL_CTX_set0_verify_cert_store(ctx, verify_store);
663
664 /* This isn't accessible to use later, i.e. there's no SSL_CTX_get0_verify_cert_store */
665 SSL_CTX_set_ex_data(ctx, FR_TLS_EX_CTX_INDEX_VERIFY_STORE, verify_store);
666
667 /*
668 * Load the CAs we trust
669 */
670 if (conf->ca_file || conf->ca_path) {
671 /*
672 * This adds all the certificates to the store for conf->ca_file
673 * and adds a dynamic lookup for conf->ca_path.
674 *
675 * It's also possible to add extra virtual server lookups
676 */
677 if (!X509_STORE_load_locations(verify_store, conf->ca_file, conf->ca_path)) {
678 fr_tls_log_perror(NULL, "Failed reading Trusted root CA list \"%s\"",
679 conf->ca_file ? conf->ca_file : conf->ca_path);
680 goto error;
681 }
682
683 /*
684 * These set the default parameters of the store when the
685 * store is involved in building chains.
686 *
687 * The store validates the peer's certificate, so
688 * the certificate purpose is the _peer's_
689 * certificate purpose, which the opposite of us.
690 *
691 * If we're a server, then we check that the peer is a client.
692 * If we're a client, then we check the peer is a server.
693 */
694 X509_STORE_set_purpose(verify_store, client ? X509_PURPOSE_SSL_SERVER : X509_PURPOSE_SSL_CLIENT);
695
696 /*
697 * Sets the list of CAs we send to the peer if we're
698 * requesting a certificate.
699 *
700 * This does not change the trusted certificate authorities,
701 * those are set above with SSL_CTX_load_verify_locations.
702 */
703 if (conf->ca_file) SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(conf->ca_file));
704 } else {
705 X509_STORE_set_default_paths(verify_store);
706 }
707
708 /*
709 * Load our certificate chains and keys
710 */
711 if (conf->chains) {
712 size_t chains_conf = talloc_array_length(conf->chains);
713
714 /*
715 * Load our keys and certificates
716 *
717 * If certificates are of type PEM then we can make use
718 * of cert chain authentication using openssl api call
719 * SSL_CTX_use_certificate_chain_file. Please see how
720 * the cert chain needs to be given in PEM from
721 * openSSL.org
722 */
723 {
724 size_t i;
725
726 for (i = 0; i < chains_conf; i++) {
727 if (tls_ctx_load_cert_chain(ctx, conf->chains[i], false) < 0) goto error;
728 }
729 }
730
731 /*
732 * Print out our certificate chains.
733 *
734 * There may be up to three, one for RSA, DH, DSA and EC.
735 * OpenSSL internally and transparently stores completely
736 * separate and distinct RSA/DSA/DH/ECC key pairs and chains.
737 */
738 if (DEBUG_ENABLED2) {
739 size_t chains_set = 0;
740 int ret;
741
742 /*
743 * Iterate over the different chain types we have
744 * RSA, DSA, DH, EC etc...
745 */
746 for (ret = SSL_CTX_set_current_cert(ctx, SSL_CERT_SET_FIRST);
747 ret == 1;
748 ret = SSL_CTX_set_current_cert(ctx, SSL_CERT_SET_NEXT)) chains_set++;
749
750 /*
751 * Check for discrepancies
752 */
753
754 DEBUG3("Found %zu server certificate chain(s)", chains_set);
755
756 if (chains_set != chains_conf) {
757 WARN("Number of chains configured (%zu) does not match chains set (%zu)",
758 chains_conf, chains_set);
759 if (chains_conf > chains_set) WARN("Only one chain per key type is allowed, "
760 "check config for duplicates");
761 }
762
763 for (ret = SSL_CTX_set_current_cert(ctx, SSL_CERT_SET_FIRST);
764 ret == 1;
765 ret = SSL_CTX_set_current_cert(ctx, SSL_CERT_SET_NEXT)) {
766 STACK_OF(X509) *our_chain;
767 X509 *our_cert;
768
769 our_cert = SSL_CTX_get0_certificate(ctx);
770
771 /*
772 * The pkey type of the server certificate
773 * determines which pkey slot OpenSSL
774 * uses to store the chain.
775 */
776 DEBUG3("%s chain", fr_tls_utils_x509_pkey_type(our_cert));
777 if (!SSL_CTX_get0_chain_certs(ctx, &our_chain)) {
778 fr_tls_log_perror(NULL, "Failed retrieving chain certificates");
779 goto error;
780 }
781
782 if (DEBUG_ENABLED3) fr_tls_chain_log(NULL, L_DBG, our_chain, our_cert);
783 }
784 (void)SSL_CTX_set_current_cert(ctx, SSL_CERT_SET_FIRST); /* Reset */
785 }
786 }
787
788#ifdef PSK_MAX_IDENTITY_LEN
789post_ca:
790#endif
791 if (tls_ctx_version_set(&ctx_options, ctx, conf) < 0) goto error;
792
793 /*
794 * SSL_OP_SINGLE_DH_USE must be used in order to prevent
795 * small subgroup attacks and forward secrecy. Always
796 * using SSL_OP_SINGLE_DH_USE has an impact on the
797 * computer time needed during negotiation, but it is not
798 * very large.
799 */
800 if (!conf->disable_single_dh_use) {
801 ctx_options |= SSL_OP_SINGLE_DH_USE;
802 }
803
804#ifdef SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS
805 /*
806 * Note: This flag isn't honoured by all OpenSSL forks.
807 */
808 if (conf->allow_renegotiation) {
809 ctx_options |= SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS;
810 }
811#endif
812
813 /*
814 * SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS to work around issues
815 * in Windows Vista client.
816 * http://www.openssl.org/~bodo/tls-cbc.txt
817 * http://www.nabble.com/(RADIATOR)-Radiator-Version-3.16-released-t2600070.html
818 */
819 ctx_options |= SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS;
820
821 /*
822 * SSL_OP_CIPHER_SERVER_PREFERENCE to follow best practice
823 * of nowday's TLS: do not allow poorly-selected ciphers from
824 * client to take preference
825 */
826 if (conf->cipher_server_preference) ctx_options |= SSL_OP_CIPHER_SERVER_PREFERENCE;
827
828 SSL_CTX_set_options(ctx, ctx_options);
829
830 /*
831 * TODO: Set the RSA & DH
832 * SSL_CTX_set_tmp_rsa_callback(ctx, cbtls_rsa);
833 * SSL_CTX_set_tmp_dh_callback(ctx, cbtls_dh);
834 */
835
836 /*
837 * Set the block size for record padding. This is only
838 * used in TLS 1.3.
839 */
840 if (conf->padding_block_size) SSL_CTX_set_block_padding(ctx, conf->padding_block_size);
841
842 /*
843 * Set elliptical curve crypto configuration.
844 */
845#ifndef OPENSSL_NO_ECDH
846 if (ctx_ecdh_curve_set(ctx, conf->ecdh_curve, conf->disable_single_dh_use) < 0) goto error;
847#endif
848
849
850 /* Set Info callback */
851 SSL_CTX_set_info_callback(ctx, fr_tls_session_info_cb);
852
853 /*
854 * SSL_ctx_set_verify is now called in the session
855 * alloc functions so they can set custom behaviour
856 * depending on the code area the SSL * will be used
857 * and whether we're acting as a client or server.
858 */
859 if (conf->verify_depth) {
860 SSL_CTX_set_verify_depth(ctx, conf->verify_depth);
861 }
862
863#ifdef HAVE_OPENSSL_OCSP_H
864 /*
865 * Configure OCSP stapling for the server cert
866 */
867 if (conf->staple.enable) {
868 SSL_CTX_set_tlsext_status_cb(ctx, fr_tls_ocsp_staple_cb);
869 SSL_CTX_set_tlsext_status_arg(ctx, UNCONST(fr_tls_ocsp_conf_t *, &(conf->staple)));
870 }
871#endif
872
873 /*
874 * Set the cipher list if we were told to do so. We do
875 * this before setting min/max TLS version. In a sane
876 * world, OpenSSL would error out if we set the max TLS
877 * version to something which was unsupported by the
878 * current security level. However, this is OpenSSL. If
879 * you set conflicting options, it doesn't give an error.
880 * Instead, it just picks something to do.
881 *
882 * SSL_CTX_set_cipher_list() accepts an empty string to
883 * clear the list for TLS <1.3 methods. However, it
884 * reports an error. We therefore catch the error, and
885 * allow it.
886 */
887 if (conf->cipher_list &&
888 !SSL_CTX_set_cipher_list(ctx, conf->cipher_list)) {
889 unsigned long err = ERR_peek_last_error();
890
891DIAG_OFF_OPTIONAL(used-but-marked-unused) /* fix spurious warnings for ERR_GET macros */
892 if (ERR_GET_LIB(err) != ERR_LIB_SSL ||
893 ERR_GET_REASON(err) != SSL_R_NO_CIPHER_MATCH) {
894 fr_tls_log_perror(NULL, "Failed setting cipher_list");
895 goto error;
896 }
897DIAG_ON_OPTIONAL(used-but-marked-unused)
898 }
899
900 /*
901 * SSL_CTX_set_ciphersuites() accepts an empty string to
902 * clear the list for TLS >=1.3 methods. However, it
903 * reports an error. We therefore catch the error, and
904 * allow it.
905 */
906 if (conf->cipher_suites &&
907 !SSL_CTX_set_ciphersuites(ctx, conf->cipher_suites)) {
908 unsigned long err = ERR_peek_last_error();
909
910DIAG_OFF_OPTIONAL(used-but-marked-unused) /* fix spurious warnings for ERR_GET macros */
911 if (ERR_GET_LIB(err) != ERR_LIB_SSL ||
912 ERR_GET_REASON(err) != SSL_R_NO_CIPHER_MATCH) {
913 fr_tls_log_perror(NULL, "Failed setting cipher_suites");
914 goto error;
915 }
916DIAG_ON_OPTIONAL(used-but-marked-unused)
917 }
918
919 /*
920 * Set the supported groups if configured.
921 *
922 * For TLS 1.3 this can help reduce round trips if the list
923 * includes a group the client predicted in the key_share extension.
924 */
925 if (conf->group_list) {
926 if (!SSL_CTX_set1_groups_list(ctx, UNCONST(char *,conf->group_list))) {
927 fr_tls_log_perror(NULL, "Failed setting group list");
928 goto error;
929 }
930 }
931
932 /*
933 * Print the actual cipher list
934 */
935 if (DEBUG_ENABLED3) {
936 SSL *ssl;
937 unsigned int i = 0;
938 char const *cipher;
939
940 ssl = SSL_new(ctx);
941 if (!ssl) {
942 fr_tls_log_perror(NULL, "Failed creating temporary SSL session");
943 goto error;
944 }
945
946 DEBUG3("Configured ciphers (by priority)");
947
948 while ((cipher = SSL_get_cipher_list(ssl, i))) {
949 DEBUG3("[%u] %s", i, cipher);
950 i++; /* Print index starting at zero */
951 }
952
953 SSL_free(ssl);
954 }
955
956 /*
957 * Load dh params
958 */
959 if (conf->dh_file) {
960 if (ctx_dh_params_load(ctx, UNCONST(char *, conf->dh_file)) < 0) goto error;
961 } else {
962 /*
963 * Tell OpenSSL to automatically set the DH
964 * parameters based on the the size of the key
965 * associated with the certificate, or for PSK,
966 * with the negotiated symmetric cipher key.
967 */
968 SSL_CTX_set_dh_auto(ctx, 1);
969 }
970
971 /*
972 * DTLS doesn't support stateless session resumption. So
973 * we forbid that.
974 *
975 * For "auto", we do a horrible hack, and just rewrite
976 * the config. This is simpler than adding more options
977 * to the ctx_init() function.
978 */
979 if (conf->socket_type == SOCK_DGRAM) {
980 if (conf->cache.mode == FR_TLS_TICKET_STATELESS) {
981 ERROR("session { mode = \"stateless\" } is not supported with DTLS. "
982 "Use \"stateful\" or \"disabled\"");
983 goto error;
984 }
985
986 if (conf->cache.mode == FR_TLS_TICKET_AUTO) {
987 UNCONST(fr_tls_conf_t *, conf)->cache.mode = FR_TLS_TICKET_STATEFUL;
988 }
989
990 /*
991 * A DTLS server answers the first ClientHello with a HelloVerifyRequest that carries a
992 * cookie. The cookie prevents an attacker from forging DTLS packets which would
993 * otherwise be able to affect the connection.
994 *
995 * The client simply echoes back any cookie that it received.
996 */
997 if (!client) {
998 SSL_CTX_set_options(ctx, SSL_OP_COOKIE_EXCHANGE);
999 SSL_CTX_set_cookie_generate_cb(ctx, fr_dtls_cookie_generate_cb);
1000 SSL_CTX_set_cookie_verify_cb(ctx, fr_dtls_cookie_verify_cb);
1001 }
1002 }
1003
1004 /*
1005 * Setup session caching
1006 */
1007 if (fr_tls_ticket_ctx_init(ctx, &conf->cache, client) < 0) goto error;
1008
1009 /*
1010 * Set the ALPN context.
1011 */
1012 if (fr_tls_ctx_alpn_set(ctx, conf, client) < 0) goto error;
1013
1014 /*
1015 * Set the keylog file if the admin requested it.
1016 */
1017 if ((getenv("SSLKEYLOGFILE") != NULL) || (conf->keylog_file && *conf->keylog_file)) {
1018 SSL_CTX_set_keylog_callback(ctx, fr_tls_session_keylog_cb);
1019 }
1020
1021 return ctx;
1022}
1023#endif
#define UNCONST(_type, _ptr)
Remove const qualification from a pointer.
Definition build.h:203
#define USES_APPLE_DEPRECATED_API
Definition build.h:590
#define RCSID(id)
Definition build.h:603
#define DIAG_OFF_OPTIONAL(_x)
Definition build.h:577
#define UNUSED
Definition build.h:415
#define DIAG_ON_OPTIONAL(_x)
Definition build.h:578
#define MEM(x)
Definition debug.h:38
#define ERROR(fmt,...)
Definition dhcpclient.c:40
free(array)
#define LOG_PREFIX
Definition base.c:63
#define PERROR(_fmt,...)
Definition log.h:233
#define DEBUG_ENABLED2
True if global debug level 1-2 messages are enabled.
Definition log.h:263
#define DEBUG3(_fmt,...)
Definition log.h:271
#define PWARN(_fmt,...)
Definition log.h:232
#define DEBUG_ENABLED3
True if global debug level 1-3 messages are enabled.
Definition log.h:264
@ L_WARN
Warning.
Definition log.h:54
@ L_ERR
Error message.
Definition log.h:53
@ L_DBG
Only displayed when debugging is enabled.
Definition log.h:56
static size_t used
#define fr_assert(_expr)
Definition rad_assert.h:37
#define WARN(fmt,...)
static rs_t * conf
Definition radsniff.c:52
"Unix" time.
Definition time.h:95
char const * fr_tls_utils_x509_pkey_type(X509 *cert)
Returns a friendly identifier for the public key type of a certificate.
Definition utils.c:45