The FreeRADIUS server $Id: a4e7f8c94e8038b7627bcf52356b7ed15955c1d7 $
Loading...
Searching...
No Matches
base.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: 39a030c481dab2de5822056de6921d5e3ed04317 $
19 *
20 * @file src/lib/tls/base.c
21 * @brief Initialise OpenSSL
22 *
23 * @copyright 2026 Network RADIUS SAS (legal@networkradius.com)
24 */
25RCSID("$Id: 39a030c481dab2de5822056de6921d5e3ed04317 $")
26USES_APPLE_DEPRECATED_API /* OpenSSL API has been deprecated by Apple */
27
28#ifdef WITH_TLS
29#define LOG_PREFIX "tls"
30
31#include "log.h"
32#include "bio.h"
33#include "dtls.h"
34
35#include <sys/mman.h>
36#include <openssl/conf.h>
37#include <openssl/provider.h>
38
39#include <freeradius-devel/server/base.h>
40#include <freeradius-devel/tls/attrs.h>
41#include <freeradius-devel/tls/base.h>
42#include <freeradius-devel/util/atexit.h>
43#include <freeradius-devel/util/debug.h>
44#include <freeradius-devel/util/math.h>
45#include <freeradius-devel/util/syserror.h>
46#include <freeradius-devel/util/md5.h>
47#include <freeradius-devel/util/md4.h>
48
49static uint32_t openssl_instance_count = 0;
50
51/** How big of a stack we allocate, taken from the default thread stack size
52 */
53static size_t openssl_stack_size;
54
55/** The context which holds any memory OpenSSL allocates
56 *
57 * This should be used to work around memory leaks in the OpenSSL.
58 */
59_Thread_local TALLOC_CTX *ssl_talloc_ctx;
60
61/** Used to control freeing of thread local OpenSSL resources
62 *
63 */
64static _Thread_local bool *async_pool_init;
65
66static OSSL_PROVIDER *openssl_default_provider = NULL;
67static OSSL_PROVIDER *openssl_legacy_provider = NULL;
68
69static uint32_t tls_instance_count = 0;
70
73fr_dict_t const *dict_tls;
74fr_dict_t const *dict_der;
75
76extern fr_dict_autoload_t tls_dict[];
77fr_dict_autoload_t tls_dict[] = {
78 { .out = &dict_freeradius, .proto = "freeradius" },
79 { .out = &dict_tls, .proto = "tls" },
80 { .out = &dict_der, .proto = "der" },
81
83};
84
87
88/*
89 * Certificate decoding attributes
90 */
110
118
124
132
134
140
146
148
149extern fr_dict_attr_autoload_t tls_dict_attr[];
150fr_dict_attr_autoload_t tls_dict_attr[] = {
151 { .out = &attr_allow_session_resumption, .name = "Allow-Session-Resumption", .type = FR_TYPE_BOOL, .dict = &dict_freeradius },
152 { .out = &attr_session_resumed, .name = "EAP-Session-Resumed", .type = FR_TYPE_BOOL, .dict = &dict_freeradius },
153
154 /*
155 * Certificate decoding attributes
156 */
157 { .out = &attr_tls_certificate, .name = "TLS-Certificate", .type = FR_TYPE_TLV, .dict = &dict_freeradius },
158 { .out = &attr_tls_certificate_serial, .name = "TLS-Certificate.Serial", .type = FR_TYPE_OCTETS, .dict = &dict_freeradius },
159 { .out = &attr_tls_certificate_signature, .name = "TLS-Certificate.Signature", .type = FR_TYPE_OCTETS, .dict = &dict_freeradius },
160 { .out = &attr_tls_certificate_signature_algorithm, .name = "TLS-Certificate.Signature-Algorithm", .type = FR_TYPE_STRING, .dict = &dict_freeradius },
161 { .out = &attr_tls_certificate_issuer, .name = "TLS-Certificate.Issuer", .type = FR_TYPE_STRING, .dict = &dict_freeradius },
162 { .out = &attr_tls_certificate_not_before, .name = "TLS-Certificate.Not-Before", .type = FR_TYPE_DATE, .dict = &dict_freeradius },
163 { .out = &attr_tls_certificate_not_after, .name = "TLS-Certificate.Not-After", .type = FR_TYPE_DATE, .dict = &dict_freeradius },
164 { .out = &attr_tls_certificate_subject, .name = "TLS-Certificate.Subject", .type = FR_TYPE_STRING, .dict = &dict_freeradius },
165 { .out = &attr_tls_certificate_common_name, .name = "TLS-Certificate.Common-Name", .type = FR_TYPE_STRING, .dict = &dict_freeradius },
166 { .out = &attr_tls_certificate_subject_alt_name_dns, .name = "TLS-Certificate.Subject-Alt-Name-Dns", .type = FR_TYPE_STRING, .dict = &dict_freeradius },
167 { .out = &attr_tls_certificate_subject_alt_name_email, .name = "TLS-Certificate.Subject-Alt-Name-Email", .type = FR_TYPE_STRING, .dict = &dict_freeradius },
168 { .out = &attr_tls_certificate_subject_alt_name_upn, .name = "TLS-Certificate.Subject-Alt-Name-Upn", .type = FR_TYPE_STRING, .dict = &dict_freeradius },
169 { .out = &attr_tls_certificate_x509v3_extended_key_usage, .name = "TLS-Certificate.X509v3-Extended-Key-Usage", .type = FR_TYPE_STRING, .dict = &dict_freeradius },
170 { .out = &attr_tls_certificate_x509v3_subject_key_identifier, .name = "TLS-Certificate.X509v3-Subject-Key-Identifier", .type = FR_TYPE_STRING, .dict = &dict_freeradius },
171 { .out = &attr_tls_certificate_x509v3_authority_key_identifier, .name = "TLS-Certificate.X509v3-Authority-Key-Identifier", .type = FR_TYPE_STRING, .dict = &dict_freeradius },
172 { .out = &attr_tls_certificate_x509v3_basic_constraints, .name = "TLS-Certificate.X509v3-Basic-Constraints", .type = FR_TYPE_STRING, .dict = &dict_freeradius },
173 { .out = &attr_tls_certificate_x509v3_crl_distribution_points, .name = "TLS-Certificate.X509v3-CRL-Distribution-Points", .type = FR_TYPE_STRING, .dict = &dict_freeradius },
174 { .out = &attr_tls_certificate_ocsp_uri, .name = "TLS-Certificate.OCSP-URI", .type = FR_TYPE_STRING, .dict = &dict_freeradius },
175 { .out = &attr_tls_certificate_ocsp_cert_id, .name = "TLS-Certificate.OCSP-Cert-Id", .type = FR_TYPE_OCTETS, .dict = &dict_freeradius },
176
177 { .out = &attr_tls_certificate_chain_depth, .name = "TLS-Certificate-Chain-Depth", .type = FR_TYPE_UINT32, .dict = &dict_freeradius },
178 { .out = &attr_tls_client_error_code, .name = "TLS-Client-Error-Code", .type = FR_TYPE_UINT8, .dict = &dict_freeradius },
179 { .out = &attr_tls_ocsp_cert_valid, .name = "TLS-OCSP-Cert-Valid", .type = FR_TYPE_UINT32, .dict = &dict_freeradius },
180 { .out = &attr_tls_ocsp_next_update, .name = "TLS-OCSP-Next-Update", .type = FR_TYPE_UINT32, .dict = &dict_freeradius },
181 { .out = &attr_tls_ocsp_response, .name = "TLS-OCSP-Response", .type = FR_TYPE_OCTETS, .dict = &dict_freeradius },
182 { .out = &attr_tls_psk_identity, .name = "TLS-PSK-Identity", .type = FR_TYPE_STRING, .dict = &dict_freeradius },
183 { .out = &attr_tls_psk_key, .name = "TLS-PSK-Key", .type = FR_TYPE_OCTETS, .dict = &dict_freeradius },
184
185 { .out = &attr_tls_session_cert_file, .name = "TLS-Session-Certificate-File", .type = FR_TYPE_STRING, .dict = &dict_freeradius },
186 { .out = &attr_tls_session_require_client_cert, .name = "TLS-Session-Require-Client-Certificate", .type = FR_TYPE_BOOL, .dict = &dict_freeradius },
187 { .out = &attr_tls_session_cipher_suite, .name = "TLS-Session-Cipher-Suite", .type = FR_TYPE_STRING, .dict = &dict_freeradius },
188 { .out = &attr_tls_session_version, .name = "TLS-Session-Version", .type = FR_TYPE_STRING, .dict = &dict_freeradius },
189 { .out = &attr_tls_session_resume_type, .name = "TLS-Session-Resume-Type", .type = FR_TYPE_UINT32, .dict = &dict_freeradius },
190
191 { .out = &attr_tls_client_hello, .name = "TLS-Client-Hello", .type = FR_TYPE_TLV, .dict = &dict_freeradius },
192 { .out = &attr_tls_client_hello_tls_version, .name = "TLS-Client-Hello.TLS-Version", .type = FR_TYPE_UINT16, .dict = &dict_freeradius },
193 { .out = &attr_tls_client_hello_cipher, .name = "TLS-Client-Hello.Cipher", .type = FR_TYPE_STRING, .dict = &dict_freeradius },
194 { .out = &attr_tls_client_hello_sig_algo, .name = "TLS-Client-Hello.Signature-Algorithm", .type = FR_TYPE_UINT16, .dict = &dict_freeradius },
195 { .out = &attr_tls_client_hello_supported_group, .name = "TLS-Client-Hello.Supported-Group", .type = FR_TYPE_UINT16, .dict = &dict_freeradius },
196 { .out = &attr_tls_client_hello_ec_point_format, .name = "TLS-Client-Hello.EC-Point-Format", .type = FR_TYPE_UINT8, .dict = &dict_freeradius },
197 { .out = &attr_tls_client_hello_psk_key_mode, .name = "TLS-Client-Hello.PSK-Key-Mode", .type = FR_TYPE_UINT8, .dict = &dict_freeradius },
198
199 { .out = &attr_module_failure_message, .name = "Module-Failure-Message", .type = FR_TYPE_STRING, .dict = &dict_freeradius },
200
201 /*
202 * Eventually all TLS attributes will be in the TLS dictionary
203 */
204 { .out = &attr_tls_error, .name = "Error", .type = FR_TYPE_UINT32, .dict = &dict_tls },
205 { .out = &attr_tls_alert, .name = "Alert", .type = FR_TYPE_STRUCT, .dict = &dict_tls },
206 { .out = &attr_tls_alert_level, .name = "Alert.Level", .type = FR_TYPE_UINT8, .dict = &dict_tls },
207 { .out = &attr_tls_alert_description, .name = "Alert.Description", .type = FR_TYPE_UINT8, .dict = &dict_tls },
208 { .out = &attr_tls_alpn, .name = "ALPN", .type = FR_TYPE_STRING, .dict = &dict_tls },
209
210 { .out = &attr_tls_packet_type, .name = "Packet-Type", .type = FR_TYPE_UINT32, .dict = &dict_tls },
211 { .out = &attr_tls_session_data, .name = "Session-Data", .type = FR_TYPE_OCTETS, .dict = &dict_tls },
212 { .out = &attr_tls_session_id, .name = "Session-Id", .type = FR_TYPE_OCTETS, .dict = &dict_tls },
213 { .out = &attr_tls_session_resumed, .name = "Session-Resumed", .type = FR_TYPE_BOOL, .dict = &dict_tls },
214 { .out = &attr_tls_session_ttl, .name = "Session-TTL", .type = FR_TYPE_TIME_DELTA, .dict = &dict_tls },
215
216 { .out = &attr_der_certificate, .name = "Certificate", .type = FR_TYPE_TLV, .dict = &dict_der },
217
219};
220
221/*
222 * request types
223 */
234
235/*
236 * response types
237 */
241
242/*
243 * session resumption
244 */
247
248extern fr_dict_enum_autoload_t tls_dict_enum[];
249fr_dict_enum_autoload_t tls_dict_enum[] = {
250 { .out = &enum_tls_packet_type_load_session, .name = "Load-Session", .attr = &attr_tls_packet_type },
251 { .out = &enum_tls_packet_type_store_session, .name = "Store-Session", .attr = &attr_tls_packet_type },
252 { .out = &enum_tls_packet_type_clear_session, .name = "Clear-Session", .attr = &attr_tls_packet_type },
253 { .out = &enum_tls_packet_type_verify_certificate, .name = "Verify-Certificate", .attr = &attr_tls_packet_type },
254 { .out = &enum_tls_packet_type_new_session, .name = "New-Session", .attr = &attr_tls_packet_type },
255 { .out = &enum_tls_packet_type_establish_session, .name = "Establish-Session", .attr = &attr_tls_packet_type },
256 { .out = &enum_tls_packet_type_fail_session, .name = "Fail-Session", .attr = &attr_tls_packet_type },
257 { .out = &enum_tls_packet_type_encode_session, .name = "Encode-Session", .attr = &attr_tls_packet_type },
258 { .out = &enum_tls_packet_type_decode_session, .name = "Decode-Session", .attr = &attr_tls_packet_type },
259 { .out = &enum_tls_packet_type_load_psk, .name = "Load-PSK", .attr = &attr_tls_packet_type },
260
261 { .out = &enum_tls_packet_type_success, .name = "Success", .attr = &attr_tls_packet_type },
262 { .out = &enum_tls_packet_type_failure, .name = "Failure", .attr = &attr_tls_packet_type },
263 { .out = &enum_tls_packet_type_notfound, .name = "Notfound", .attr = &attr_tls_packet_type },
264
265 { .out = &enum_tls_session_resumed_stateful, .name = "stateful", .attr = &attr_tls_session_resume_type },
266 { .out = &enum_tls_session_resumed_stateless, .name = "stateless", .attr = &attr_tls_session_resume_type },
267
269};
270
271/*
272 * Updated by threads.c in the server, and left alone for everyone else.
273 */
274int fr_tls_max_threads = 1;
275
276/** Allocate memory for OpenSSL in the NULL context
277 *
278 * @param len to alloc.
279 * @return realloc.
280 */
281static void *fr_openssl_talloc(size_t len, char const *file, NDEBUG_UNUSED int line)
282{
283 static char const *async_file;
284 void *chunk;
285
286 if (!file) {
287 chunk = talloc_array(ssl_talloc_ctx, uint8_t, len);
288
289#ifndef NDEBUG
290 talloc_set_name(chunk, "fr_openssl_talloc");
291#endif
292 return chunk;
293 }
294
295 /*
296 * Cache the filename pointer for the async_posix.c
297 * source file, so we can figure out when we're
298 * being asked for stack memory.
299 *
300 * This is terrible, we're basically guessing at the
301 * stack size. OpenSSL 3.1.0 will have proper
302 * allocation functions so we can something more
303 * sensible.
304 */
305 if (!async_file) {
306 char const *sep;
307
308 sep = strrchr(file, '/');
309 if (!sep) {
310 sep = file;
311 } else {
312 sep++;
313 }
314 if (strcmp(sep, "async_posix.c") == 0) {
315 async_file = file;
316 alloc_stack:
317 len *= 4;
318 }
319 } else if (file == async_file) goto alloc_stack;
320
321 chunk = talloc_array(ssl_talloc_ctx, uint8_t, len);
322#ifndef NDEBUG
323 talloc_set_name(chunk, "%s:%d", file, line);
324#endif
325 return chunk;
326}
327
328/** Reallocate memory for OpenSSL in the NULL context
329 *
330 * @param old memory to realloc.
331 * @param len to extend to.
332 * @return realloced memory.
333 */
334static void *fr_openssl_talloc_realloc(void *old, size_t len, NDEBUG_UNUSED char const *file, NDEBUG_UNUSED int line)
335{
336 void *chunk;
337
338 chunk = talloc_realloc_size(ssl_talloc_ctx, old, len);
339#ifndef NDEBUG
340 talloc_set_name(chunk, "%s:%d", file, line);
341#endif
342 return chunk;
343}
344
345/** Free memory allocated by OpenSSL
346 *
347 * @param to_free memory to free.
348 */
349#ifdef NDEBUG
350/*
351 * If we're not debugging, use only the filename. Otherwise the
352 * cost of snprintf() is too large.
353 */
354static void fr_openssl_talloc_free(void *to_free, char const *file, UNUSED int line)
355{
356 (void)_talloc_free(to_free, file);
357}
358#else
359static void fr_openssl_talloc_free(void *to_free, char const *file, int line)
360{
361 char buffer[256];
362
363 snprintf(buffer, sizeof(buffer), "%s:%i", file, line);
364 (void)_talloc_free(to_free, buffer);
365}
366#endif
367
368/** Cleanup async pools if the thread exits
369 *
370 */
371static int _openssl_thread_free(void *init)
372{
373 ASYNC_cleanup_thread();
374 return talloc_free(init);
375}
376
377/** Perform thread-specific initialisation for OpenSSL
378 *
379 * Async contexts are what OpenSSL uses to track
380 *
381 * @param[in] async_pool_size_init The initial number of async contexts
382 * we keep in the pool.
383 * @param[in] async_pool_size_max The maximum number of async contexts
384 * we keep in the thread-local pool.
385 * @return
386 * - 0 on success.
387 * - -1 on failure.
388 */
389int fr_openssl_thread_init(size_t async_pool_size_init, size_t async_pool_size_max)
390{
391 /*
392 * Hack to use thread local destructor code
393 */
394 if (!async_pool_init) {
395 bool *init = talloc_zero(NULL, bool);
396
397 if (ASYNC_init_thread(async_pool_size_max, async_pool_size_init) != 1) {
398 fr_openssl_log_perror(NULL, "Failed initialising OpenSSL async context pool");
399 return -1;
400 }
401
402 fr_atexit_thread_local(async_pool_init, _openssl_thread_free, init);
403 }
404
405 return 0;
406}
407
408/** Free any memory alloced by libssl
409 *
410 * OpenSSL >= 1.1.0 uses an atexit handler to automatically free
411 * memory. However, we need to call OPENSSL_cleanup manually because
412 * some of the SSL ctx is parented to the main config which will get
413 * freed before the atexit handler, causing a segfault on exit.
414 */
415void fr_openssl_free(void)
416{
417 if (--openssl_instance_count > 0) return;
418
419 fr_tls_log_free();
420
421 fr_tls_bio_free();
422
423 fr_md5_openssl_free();
424 fr_md4_openssl_free();
425}
426
427#if OPENSSL_VERSION_NUMBER < 0x40000000L
428static void _openssl_provider_free(void)
429{
430 if (openssl_default_provider && !OSSL_PROVIDER_unload(openssl_default_provider)) {
431 fr_openssl_log_perror(NULL, "Failed unloading default provider");
432 }
433 openssl_default_provider = NULL;
434
435 if (openssl_legacy_provider && !OSSL_PROVIDER_unload(openssl_legacy_provider)) {
436 fr_openssl_log_perror(NULL, "Failed unloading legacy provider");
437 }
438 openssl_legacy_provider = NULL;
439}
440#endif
441
442static int fr_openssl_cleanup(UNUSED void *uctx)
443{
444 OPENSSL_cleanup();
445 return 0;
446}
447
448#ifdef HAVE_ASYNC_SET_MEM_FUNCTIONS
449
450static void *fr_openssl_stack_alloc(size_t *len)
451{
452 void *stack;
453
454 /*
455 * Use mmap to sparsely allocate the stack
456 */
457#if defined(__linux__) || defined(__FreeBSD__)
458 stack = mmap(NULL, openssl_stack_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE | MAP_STACK, -1, 0);
459#else
460 stack = mmap(NULL, openssl_stack_size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
461#endif
462 if (stack == MAP_FAILED) {
463 fr_openssl_log_perror(NULL, "Failed allocating OpenSSL stack: %s", fr_syserror(errno));
464 return NULL;
465 }
466 *len = openssl_stack_size;
467
468 return stack;
469}
470
471static void fr_openssl_stack_free(void *stack)
472{
473 munmap(stack, openssl_stack_size);
474}
475#endif
476
477/** Add all the default ciphers and message digests to our context.
478 *
479 * This should be called exactly once from main, before reading the main config
480 * or initialising any modules.
481 */
482int fr_tls_session_ex_index = -1;
483
484int fr_openssl_init(void)
485{
486 int rcode;
487 pthread_attr_t tattr;
488
489 if (openssl_instance_count > 0) {
490 openssl_instance_count++;
491 return 0;
492 }
493
494 pthread_attr_init(&tattr);
495 rcode = pthread_attr_getstacksize(&tattr, &openssl_stack_size);
496 pthread_attr_destroy(&tattr);
497
498 if (rcode != 0) {
499 fr_openssl_log_perror(NULL, "Failed getting stack size");
500 return -1;
501 }
502
503 /*
504 * This will only fail if memory has already been allocated
505 * by OpenSSL.
506 */
507 if (CRYPTO_set_mem_functions(fr_openssl_talloc, fr_openssl_talloc_realloc, fr_openssl_talloc_free) != 1) {
508 fr_openssl_log_perror(NULL, "Failed to set OpenSSL memory allocation functions. fr_openssl_init() called too late");
509 return -1;
510 }
511
512 /*
513 * Setup custom memory allocators for allocating greenthread
514 * stacks, so we can add guard pages.
515 */
516#ifdef HAVE_ASYNC_SET_MEM_FUNCTIONS
517 if (ASYNC_set_mem_functions(fr_openssl_stack_alloc, fr_openssl_stack_free) != 1) {
518 fr_openssl_log_perror(NULL, "Failed to set OpenSSL async stack allocation functions");
519 return -1;
520 }
521#endif
522
523 /*
524 * NO_ATEXIT has no effect if init is done after
525 * loading providers, and we need to control the
526 * exit handler as it needs to be executed last
527 * after all the EVP_MD ctx have been called, as
528 * they may unload elements of providers once all
529 * the contexts have been cleaned up.
530 */
531 if (OPENSSL_init_ssl(OPENSSL_INIT_NO_ATEXIT | OPENSSL_INIT_LOAD_CONFIG, NULL) != 1) {
532 fr_openssl_log_perror(NULL, "Failed calling OPENSSL_init_crypto()");
533 return -1;
534 }
535
536 /*
537 * Load the default provider for most algorithms
538 */
539 openssl_default_provider = OSSL_PROVIDER_load(NULL, "default");
540 if (!openssl_default_provider) {
541 fr_openssl_log_perror(NULL, "Failed loading default provider");
542 return -1;
543 }
544
545 /*
546 * Needed for MD4
547 *
548 * https://www.openssl.org/docs/man3.0/man7/migration_guide.html#Legacy-Algorithms
549 */
550 openssl_legacy_provider = OSSL_PROVIDER_load(NULL, "legacy");
551 if (!openssl_legacy_provider) {
552 fr_openssl_log_perror(NULL, "Failed loading legacy provider");
553 return -1;
554 }
555
556#if OPENSSL_VERSION_NUMBER < 0x40000000L
557 /*
558 * Unload the providers from OpenSSL's cleanup
559 * stack, which runs inside OPENSSL_cleanup() after
560 * this thread's OpenSSL state is freed and before
561 * the library itself is torn down.
562 */
563 OPENSSL_atexit(_openssl_provider_free);
564#else
565 /*
566 * OpenSSL 4.0 removed OPENSSL_atexit(), and
567 * OPENSSL_cleanup() now defers the teardown to a
568 * library destructor, which deactivates and frees
569 * every loaded provider itself at that point.
570 */
571#endif
572
573 /*
574 * SHA256 is in all versions of OpenSSL, but isn't
575 * initialized by default. It's needed for WiMAX
576 * certificates.
577 */
578 EVP_add_digest(EVP_sha256());
579
580 /*
581 * Allocate an index via the OpenSSL API. This tells
582 * OpenSSL that the ex_data pointer can be duplicated
583 * when the SSL_SESSION is duplicated.
584 *
585 * No callbacks are needed: the default is to copy the
586 * pointer, which is what we need. The #fr_tls_session_t
587 * outlives every SSL_SESSION which points to it.
588 */
589 fr_tls_session_ex_index = SSL_SESSION_get_ex_new_index(0, NULL, NULL, NULL, NULL);
590 if (fr_tls_session_ex_index < 0) {
591 fr_openssl_log_perror(NULL, "Failed allocating an SSL_SESSION ex_data index");
592 return -1;
593 }
594
595 fr_tls_log_init();
596
597 fr_tls_bio_init();
598
599 /*
600 * The key every DTLS cookie is generated with, see
601 * fr_dtls_cookie_generate_cb().
602 */
603 if (fr_dtls_cookie_init() < 0) {
604 fr_openssl_log_perror(NULL, "Failed generating the DTLS cookie key");
605 return -1;
606 }
607
608 fr_md5_openssl_init();
609 fr_md4_openssl_init();
610
611 /*
612 * Use an atexit handler to try and ensure
613 * that OpenSSL gets freed last.
614 *
615 * All EVP_*ctxs need to be freed before we
616 * de-initialise the libraries else we get
617 * crashes (at least with OpenSSL 3.0.1).
618 */
619 fr_atexit_global(fr_openssl_cleanup, NULL);
620
621 openssl_instance_count++;
622
623 return 0;
624}
625
626/** Enable or disable fips mode
627 *
628 * @param[in] enabled If true enable fips mode if false disable fips mode.
629 * @return
630 * - 0 on success.
631 * - -1 on failure
632 */
633int fr_openssl_fips_mode(bool enabled)
634{
635 if (!EVP_set_default_properties(NULL, enabled ? "fips=yes" : "-fips")) {
636 fr_openssl_log_perror(NULL, "Failed %s OpenSSL FIPS mode", enabled ? "enabling" : "disabling");
637 return -1;
638 }
639
640 /*
641 * Swap the MD4 / MD5 functions as appropriate.
642 */
643 if (enabled) {
644 fr_md5_openssl_init();
645 fr_md4_openssl_init();
646 } else {
647 fr_md5_openssl_free();
648 fr_md4_openssl_free();
649 }
650
651 return 0;
652}
653
654/** Load dictionary attributes
655 *
656 * This is a separate function because of ordering issues.
657 * OpenSSL may need to be initialised before anything else
658 * including the dictionary loader.
659 *
660 * fr_openssl_free will unload both the dictionary and the
661 * OpenSSL library.
662 */
663int fr_tls_dict_init(void)
664{
665 if (tls_instance_count > 0) {
666 tls_instance_count++;
667 return 0;
668 }
669
670 tls_instance_count++;
671
672 if (fr_dict_autoload(tls_dict) < 0) {
673 PERROR("Failed initialising protocol library");
674 fail:
675 tls_instance_count--;
676 fr_openssl_free();
677 return -1;
678 }
679
680 if (fr_dict_attr_autoload(tls_dict_attr) < 0) {
681 PERROR("Failed resolving attributes");
682 goto fail;
683 }
684
685 if (fr_dict_enum_autoload(tls_dict_enum) < 0) {
686 PERROR("Failed resolving enums");
687 goto fail;
688 }
689
690 return 0;
691}
692
693void fr_tls_dict_free(void)
694{
695 if (--tls_instance_count > 0) return;
696
697 fr_dict_autofree(tls_dict);
698}
699#endif /* WITH_TLS */
static int const char char buffer[256]
Definition acutest.h:641
int const char * file
Definition acutest.h:855
void int line
Definition acutest.h:810
#define _Thread_local
Definition atexit.h:213
#define fr_atexit_global(_func, _uctx)
Add a free function to the global free list.
Definition atexit.h:58
#define fr_atexit_thread_local(_name, _free, _uctx)
Definition atexit.h:224
#define USES_APPLE_DEPRECATED_API
Definition build.h:590
#define RCSID(id)
Definition build.h:603
#define NDEBUG_UNUSED
Definition build.h:426
#define UNUSED
Definition build.h:415
int fr_dict_enum_autoload(fr_dict_enum_autoload_t const *to_load)
Process a dict_attr_autoload element to load/verify a dictionary attribute.
Definition dict_util.c:4493
#define fr_dict_autofree(_to_free)
Definition dict.h:942
fr_value_box_t const ** out
Enumeration value.
Definition dict.h:305
fr_dict_attr_t const ** out
Where to write a pointer to the resolved fr_dict_attr_t.
Definition dict.h:316
fr_dict_t const ** out
Where to write a pointer to the loaded/resolved fr_dict_t.
Definition dict.h:329
int fr_dict_attr_autoload(fr_dict_attr_autoload_t const *to_load)
Process a dict_attr_autoload element to load/verify a dictionary attribute.
Definition dict_util.c:4532
#define fr_dict_autoload(_to_load)
Definition dict.h:939
#define DICT_AUTOLOAD_TERMINATOR
Definition dict.h:335
Specifies an attribute which must be present for the module to function.
Definition dict.h:315
Specifies a dictionary which must be loaded/loadable for the module to function.
Definition dict.h:328
Specifies a value which must be present for the module to function.
Definition dict.h:304
talloc_free(hp)
static fr_dict_t const * dict_freeradius
Definition base.c:37
fr_dict_attr_t const * attr_tls_certificate
Attribute definitions for lib curl.
Definition base.c:36
static fr_dict_t const * dict_tls
Definition base.c:77
fr_dict_t const * dict_radius
Definition base.c:76
static fr_dict_attr_t const * attr_module_failure_message
Definition log.c:206
#define PERROR(_fmt,...)
Definition log.h:233
fr_value_box_t const * enum_tls_packet_type_store_session
fr_value_box_t const * enum_tls_packet_type_load_psk
HIDDEN fr_dict_attr_t const * attr_tls_certificate_subject
HIDDEN fr_dict_attr_t const * attr_tls_certificate_x509v3_extended_key_usage
HIDDEN fr_dict_attr_t const * attr_tls_packet_type
HIDDEN fr_dict_attr_t const * attr_tls_certificate_x509v3_authority_key_identifier
HIDDEN fr_dict_attr_t const * attr_session_resumed
fr_value_box_t const * enum_tls_packet_type_failure
HIDDEN fr_dict_attr_t const * attr_tls_session_resumed
HIDDEN fr_dict_attr_t const * attr_tls_session_version
HIDDEN fr_dict_attr_t const * attr_tls_ocsp_cert_valid
HIDDEN fr_dict_attr_t const * attr_tls_alert_level
HIDDEN fr_dict_attr_t const * attr_tls_client_hello
HIDDEN fr_dict_attr_t const * attr_tls_client_hello_psk_key_mode
HIDDEN fr_dict_attr_t const * attr_tls_certificate_x509v3_basic_constraints
fr_value_box_t const * enum_tls_packet_type_success
HIDDEN fr_dict_attr_t const * attr_tls_psk_key
HIDDEN fr_dict_attr_t const * attr_tls_certificate_serial
HIDDEN fr_dict_attr_t const * attr_tls_session_require_client_cert
HIDDEN fr_dict_t const * dict_der
Definition base.c:38
HIDDEN fr_dict_attr_t const * attr_tls_certificate_subject_alt_name_dns
HIDDEN fr_dict_attr_t const * attr_tls_client_hello_supported_group
HIDDEN fr_dict_attr_t const * attr_tls_session_ttl
fr_value_box_t const * enum_tls_session_resumed_stateful
HIDDEN fr_dict_attr_t const * attr_tls_session_data
HIDDEN fr_dict_attr_t const * attr_tls_certificate_not_after
HIDDEN fr_dict_attr_t const * attr_tls_certificate_not_before
HIDDEN fr_dict_attr_t const * attr_tls_alpn
fr_value_box_t const * enum_tls_packet_type_new_session
HIDDEN fr_dict_attr_t const * attr_tls_certificate_signature_algorithm
HIDDEN fr_dict_attr_t const * attr_tls_client_error_code
fr_value_box_t const * enum_tls_packet_type_load_session
fr_value_box_t const * enum_tls_packet_type_establish_session
HIDDEN fr_dict_attr_t const * attr_tls_error
HIDDEN fr_dict_attr_t const * attr_tls_certificate_subject_alt_name_upn
HIDDEN fr_dict_attr_t const * attr_tls_ocsp_response
HIDDEN fr_dict_attr_t const * attr_tls_session_id
fr_value_box_t const * enum_tls_packet_type_fail_session
HIDDEN fr_dict_attr_t const * attr_tls_client_hello_sig_algo
HIDDEN fr_dict_attr_t const * attr_tls_certificate_ocsp_uri
HIDDEN fr_dict_attr_t const * attr_tls_certificate_x509v3_subject_key_identifier
fr_value_box_t const * enum_tls_packet_type_clear_session
HIDDEN fr_dict_attr_t const * attr_tls_session_cipher_suite
HIDDEN fr_dict_attr_t const * attr_tls_certificate_common_name
HIDDEN fr_dict_attr_t const * attr_tls_certificate_ocsp_cert_id
HIDDEN fr_dict_attr_t const * attr_tls_psk_identity
HIDDEN fr_dict_attr_t const * attr_tls_alert_description
HIDDEN fr_dict_attr_t const * attr_tls_certificate_issuer
fr_value_box_t const * enum_tls_session_resumed_stateless
HIDDEN fr_dict_attr_t const * attr_tls_certificate_x509v3_crl_distribution_points
fr_value_box_t const * enum_tls_packet_type_notfound
HIDDEN fr_dict_attr_t const * attr_tls_client_hello_ec_point_format
HIDDEN fr_dict_attr_t const * attr_der_certificate
HIDDEN fr_dict_attr_t const * attr_tls_certificate_signature
HIDDEN fr_dict_attr_t const * attr_tls_alert
HIDDEN fr_dict_attr_t const * attr_tls_session_resume_type
fr_value_box_t const * enum_tls_packet_type_decode_session
HIDDEN fr_dict_attr_t const * attr_tls_ocsp_next_update
fr_value_box_t const * enum_tls_packet_type_encode_session
HIDDEN fr_dict_attr_t const * attr_tls_client_hello_tls_version
fr_value_box_t const * enum_tls_packet_type_verify_certificate
HIDDEN fr_dict_attr_t const * attr_tls_client_hello_cipher
HIDDEN fr_dict_attr_t const * attr_tls_session_cert_file
HIDDEN fr_dict_attr_t const * attr_tls_certificate_chain_depth
HIDDEN fr_dict_attr_t const * attr_tls_certificate_subject_alt_name_email
HIDDEN fr_dict_attr_t const * attr_allow_session_resumption
static char * stack[MAX_STACK]
Definition radmin.c:158
@ FR_TYPE_TIME_DELTA
A period of time measured in nanoseconds.
@ FR_TYPE_TLV
Contains nested attributes.
@ FR_TYPE_STRING
String of printable characters.
@ FR_TYPE_UINT16
16 Bit unsigned integer.
@ FR_TYPE_DATE
Unix time stamp, always has value >2^31.
@ FR_TYPE_UINT8
8 Bit unsigned integer.
@ FR_TYPE_UINT32
32 Bit unsigned integer.
@ FR_TYPE_STRUCT
like TLV, but without T or L, and fixed-width children
@ FR_TYPE_BOOL
A truth value.
@ FR_TYPE_OCTETS
Raw octets.
unsigned int uint32_t
unsigned char uint8_t
PUBLIC int snprintf(char *string, size_t length, char *format, va_alist)
Definition snprintf.c:689
init
Enter the EAP-IDENTITY state.
char const * fr_syserror(int num)
Guaranteed to be thread-safe version of strerror.
Definition syserror.c:243
static fr_sbuff_err_t char size_t * len
Definition value.h:1061