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