28#define LOG_PREFIX "tls"
32#include <freeradius-devel/server/exec.h>
33#include <freeradius-devel/server/pair.h>
34#include <freeradius-devel/tls/log.h>
35#include <freeradius-devel/unlang/function.h>
36#include <freeradius-devel/unlang/subrequest.h>
37#include <freeradius-devel/util/debug.h>
38#include <freeradius-devel/util/strerror.h>
39#include <freeradius-devel/util/syserror.h>
41#include <freeradius-devel/protocol/tls/freeradius.h>
43#include <openssl/x509v3.h>
58static inline CC_HINT(always_inline)
59bool verify_applies(fr_tls_verify_mode_t mode,
int depth,
int untrusted)
61 if (mode == FR_TLS_VERIFY_MODE_ALL)
return true;
62 if (mode == FR_TLS_VERIFY_MODE_DISABLED)
return false;
64 if ((mode & FR_TLS_VERIFY_MODE_LEAF) && (
depth == 0))
return true;
65 if ((mode & FR_TLS_VERIFY_MODE_ISSUER) && (
depth == 1))
return true;
66 if ((mode & FR_TLS_VERIFY_MODE_UNTRUSTED) && (
depth < untrusted))
return true;
76static void tls_verify_error_detail(
request_t *
request, SSL_CTX *ctx,
int err)
78 X509_STORE *
store = SSL_CTX_get_ex_data(ctx, FR_TLS_EX_CTX_INDEX_VERIFY_STORE);
91 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
98 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
99 RDEBUG2(
"(TLS) Static certificates in verification store are");
101#if OPENSSL_VERSION_NUMBER >= 0x30300000L
102 STACK_OF(X509_OBJECT) *objects = X509_STORE_get1_objects(
store);
108 sk_X509_OBJECT_pop_free(objects, X509_OBJECT_free);
152int fr_tls_verify_cert_cb(
int ok, X509_STORE_CTX *x509_ctx)
158 fr_tls_session_t *tls_session;
167 cert = X509_STORE_CTX_get_current_cert(x509_ctx);
168 err = X509_STORE_CTX_get_error(x509_ctx);
169 depth = X509_STORE_CTX_get_error_depth(x509_ctx);
170 untrusted = X509_STORE_CTX_get_num_untrusted(x509_ctx);
176 ssl = X509_STORE_CTX_get_ex_data(x509_ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
177 ssl_ctx = SSL_get_SSL_CTX(ssl);
178 conf = fr_tls_session_conf(ssl);
179 tls_session = talloc_get_type_abort(SSL_get_ex_data(ssl, FR_TLS_EX_INDEX_TLS_SESSION), fr_tls_session_t);
180 request = fr_tls_session_request(tls_session->ssl);
187 if (!tls_session->can_pause) {
189 "tls_session_async_handshake_cont must be in call stack", __FUNCTION__);
198 X509_STORE_CTX_set_error(x509_ctx, 0);
208 REDEBUG(
"(TLS) Failed to add certificate chain depth pair");
215 STACK_OF(X509) *our_chain;
218 our_chain = X509_STORE_CTX_get0_chain(x509_ctx);
219 RDEBUG3(
"(TLS) Certificate chain - %i cert(s) untrusted", untrusted);
220 for (i = sk_X509_num(our_chain); i > 0 ; i--) {
221 X509 *this_cert = sk_X509_value(our_chain, i - 1);
223 X509_NAME_oneline(X509_get_subject_name(this_cert), subject,
sizeof(subject));
224 subject[
sizeof(subject) - 1] =
'\0';
226 RDEBUG3(
"(TLS) %s [%i] %s", this_cert == cert ?
">" :
" ", i - 1, subject);
236 char const *p = X509_verify_cert_error_string(err);
237 if (!verify_applies(
conf->verify.mode,
depth, untrusted)) {
238 RDEBUG2(
"(TLS) Ignoring verification error - %s (%i)", p, err);
239 tls_verify_error_detail(
request, ssl_ctx, err);
242 X509_STORE_CTX_set_error(x509_ctx, 0);
244 REDEBUG(
"(TLS) Verification error - %s (%i)", p, err);
245 fr_tls_session_error_add(
request, FR_ERROR_VALUE_CERTIFICATE_VERIFY_FAILED);
246 tls_verify_error_detail(
request, ssl_ctx, err);
258 if (my_ok && (
depth == 0)) {
259 bool want_pairs =
conf->verify.attribute_mode != FR_TLS_VERIFY_MODE_DISABLED;
260 bool want_verify =
conf->verify_certificate && tls_session->verify_peer_cert;
262 if (want_pairs || want_verify) {
263 fr_tls_verify_cert_request(tls_session, x509_ctx);
276 X509_STORE_CTX_set_error(x509_ctx, 0);
277 fr_tls_verify_cert_reset(tls_session);
286 if (!fr_tls_verify_cert_result(tls_session)) {
287 REDEBUG(
"(TLS) Certificate validation failed");
289 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_APPLICATION_VERIFICATION);
294 tls_session->peer_cert_ok = (my_ok > 0);
295 RDEBUG2(
"(TLS) [verify] = %s", my_ok ?
"ok" :
"invalid");
320 STACK_OF(X509) *chain;
323 X509_STORE_CTX *store_ctx;
328 cert = SSL_get0_peer_certificate(ssl);
331 ssl_ctx = SSL_get_SSL_CTX(ssl);
332 store_ctx = X509_STORE_CTX_new();
334 REDEBUG(
"(TLS) Failed allocating X509_STORE_CTX");
338 chain = SSL_get_peer_cert_chain(ssl);
339 store = SSL_CTX_get_ex_data(ssl_ctx, FR_TLS_EX_CTX_INDEX_VERIFY_STORE);
357 if (
unlikely(X509_STORE_CTX_init(store_ctx,
store, cert, chain) != 1)) {
358 REDEBUG(
"(TLS) Failed initialising X509_STORE_CTX");
359 X509_STORE_CTX_free(store_ctx);
363 X509_STORE_CTX_set_ex_data(store_ctx, SSL_get_ex_data_X509_STORE_CTX_idx(), ssl);
364 X509_STORE_CTX_set_verify_cb(store_ctx, fr_tls_verify_cert_cb);
366 verify = X509_verify_cert(store_ctx);
368 err = X509_STORE_CTX_get_error(store_ctx);
370 if (err != X509_V_OK) {
371 REDEBUG(
"(TLS) Failed re-validating resumed session: %s", X509_verify_cert_error_string(err));
372 fr_tls_session_error_add(
request, FR_ERROR_VALUE_CERTIFICATE_REVALIDATION_FAILED);
377 X509_STORE_CTX_free(store_ctx);
387 fr_tls_session_t *tls_session = talloc_get_type_abort(uctx, fr_tls_session_t);
390 fr_assert(tls_session->validate.state == FR_TLS_VALIDATION_REQUESTED);
394 REDEBUG(
"(TLS) Failed (re-)validating certificates");
395 fr_tls_session_error_add(
request->parent, FR_ERROR_VALUE_VERIFY_CERTIFICATE_FAILED);
409 tls_session->validate.state = FR_TLS_VALIDATION_FAILED;
413 tls_session->validate.state = FR_TLS_VALIDATION_SUCCESS;
415 RDEBUG2(
"(TLS) Certificates (re-)validated");
431 fr_tls_conf_t *
conf = tls_session->conf;
439 &tls_session->session_id));
445 fr_tls_session_extra_pairs_copy_to_child(child, tls_session);
454 if (
conf->verify.der_decode) {
461 vp->vp_bool = tls_session->validate.resumed;
467 ua = fr_tls_call_push(child, tls_verify_peer_cert_result,
conf, tls_session,
false);
469 REDEBUG(
"(TLS) Failed calling TLS virtual server");
486bool fr_tls_verify_cert_result(fr_tls_session_t *tls_session)
490 fr_assert(tls_session->validate.state != FR_TLS_VALIDATION_INIT);
492 result = tls_session->validate.state == FR_TLS_VALIDATION_SUCCESS;
494 tls_session->validate.state = FR_TLS_VALIDATION_INIT;
495 tls_session->validate.resumed =
false;
503void fr_tls_verify_cert_reset(fr_tls_session_t *tls_session)
505 tls_session->validate.state = FR_TLS_VALIDATION_INIT;
506 tls_session->validate.resumed =
false;
507 tls_session->validate.x509_ctx = NULL;
518void fr_tls_verify_cert_request(fr_tls_session_t *tls_session, X509_STORE_CTX *x509_ctx)
520 fr_assert(tls_session->validate.state == FR_TLS_VALIDATION_INIT);
522 tls_session->validate.state = FR_TLS_VALIDATION_REQUESTED;
523 TLS_PENDING_SET(tls_session, FR_TLS_PENDING_VERIFY);
524 tls_session->validate.resumed =
false;
525 tls_session->validate.x509_ctx = x509_ctx;
536void fr_tls_verify_resumed_request(fr_tls_session_t *tls_session)
538 fr_assert(tls_session->validate.state == FR_TLS_VALIDATION_INIT);
540 tls_session->validate.state = FR_TLS_VALIDATION_REQUESTED;
541 TLS_PENDING_SET(tls_session, FR_TLS_PENDING_VERIFY);
542 tls_session->validate.resumed =
true;
543 tls_session->validate.x509_ctx = NULL;
561static int tls_verify_chain_pairs_add(
request_t *
request, fr_tls_session_t *tls_session, fr_tls_conf_t
const *
conf)
563 X509_STORE_CTX *x509_ctx = tls_session->validate.x509_ctx;
564 STACK_OF(X509) *chain = X509_STORE_CTX_get0_chain(x509_ctx);
565 int untrusted = X509_STORE_CTX_get_num_untrusted(x509_ctx);
566 int num = sk_X509_num(chain);
574 X509 *cert = sk_X509_value(chain,
depth);
579 if (!verify_applies(
conf->verify.attribute_mode,
depth, untrusted))
continue;
592 i <= (
unsigned int)
depth;
598#ifdef STATIC_ANALYZER
612 if ((
depth + 1) < num) {
613 issuer = sk_X509_value(chain,
depth + 1);
614 }
else if (X509_check_issued(cert, cert) == X509_V_OK) {
625 if (fr_tls_session_pairs_from_x509_cert(&container->vp_group, container,
626 request, cert, issuer,
conf->verify.der_decode) < 0) {
628 if (
conf->verify.der_decode) {
631 fr_tls_session_error_add(
request, FR_ERROR_VALUE_CERTIFICATE_ATTRIBUTES_FAILED);
656 fr_tls_conf_t *
conf = tls_session->conf;
663 if (!tls_session->validate.resumed) {
664 RDEBUG2(
"(TLS) Decoding certificate chain");
665 if (tls_verify_chain_pairs_add(
request, tls_session,
conf) < 0) {
666 tls_session->validate.state = FR_TLS_VALIDATION_FAILED;
674 if (!
conf->verify_certificate || !tls_session->verify_peer_cert) {
675 tls_session->validate.state = FR_TLS_VALIDATION_SUCCESS;
679 RDEBUG2(
"(TLS) Requesting certificate validation");
681 return tls_verify_peer_cert_push(
request, tls_session);
unlang_action_t
Returned by unlang_op_t calls, determine the next action of the interpreter.
@ UNLANG_ACTION_FAIL
Encountered an unexpected error.
@ UNLANG_ACTION_CALCULATE_RESULT
Calculate a new section rlm_rcode_t value.
#define store(_store, _var)
static uint8_t request[4]
#define DIAG_OFF_OPTIONAL(_x)
#define FALL_THROUGH
clang 10 doesn't recognised the FALL-THROUGH comment anymore
#define DIAG_ON_OPTIONAL(_x)
#define fr_cond_assert(_x)
Calls panic_action ifndef NDEBUG, else logs error and evaluates to value of _x.
#define fr_assert_msg(_x, _msg,...)
Calls panic_action ifndef NDEBUG, else logs error and causes the server to exit immediately with code...
bool unlang_request_is_cancelled(request_t const *request)
Return whether a request has been cancelled.
HIDDEN fr_dict_attr_t const * attr_tls_certificate
Attribute definitions for lib curl.
static fr_dict_attr_t const * attr_module_failure_message
void log_request_pair(fr_log_lvl_t lvl, request_t *request, fr_pair_t const *parent, fr_pair_t const *vp, char const *prefix)
Print a fr_pair_t.
#define REXDENT()
Exdent (unindent) R* messages by one level.
#define RDEBUG_ENABLED3
True if request debug level 1-3 messages are enabled.
#define RINDENT()
Indent R* messages by one level.
HIDDEN fr_dict_attr_t const * attr_tls_packet_type
HIDDEN fr_dict_attr_t const * attr_tls_session_resumed
fr_value_box_t const * enum_tls_packet_type_success
HIDDEN fr_dict_attr_t const * attr_der_certificate
fr_value_box_t const * enum_tls_packet_type_verify_certificate
HIDDEN fr_dict_attr_t const * attr_tls_certificate_chain_depth
@ L_DBG_LVL_2
2nd highest priority debug messages (-xx | -X).
@ L_DBG
Only displayed when debugging is enabled.
static uint8_t depth(fr_minmax_heap_index_t i)
unsigned int fr_pair_count_by_da(fr_pair_list_t const *list, fr_dict_attr_t const *da)
Return the number of instances of a given da in the specified list.
fr_pair_t * fr_pair_find_by_da(fr_pair_list_t const *list, fr_pair_t const *prev, fr_dict_attr_t const *da)
Find the first pair with a matching da.
int fr_pair_append(fr_pair_list_t *list, fr_pair_t *to_add)
Add a VP to the end of the list.
int fr_pair_delete_by_da(fr_pair_list_t *list, fr_dict_attr_t const *da)
Delete matching pairs from the specified list.
fr_pair_t * fr_pair_find_by_da_idx(fr_pair_list_t const *list, fr_dict_attr_t const *da, unsigned int idx)
Find a pair with a matching da at a given index.
fr_pair_t * fr_pair_afrom_da(TALLOC_CTX *ctx, fr_dict_attr_t const *da)
Dynamically allocate a new attribute and assign a fr_dict_attr_t.
int fr_pair_steal_append(TALLOC_CTX *list_ctx, fr_pair_list_t *list, fr_pair_t *vp)
Change a vp's talloc ctx and insert it into a new list.
fr_pair_t * fr_pair_copy(TALLOC_CTX *ctx, fr_pair_t const *vp)
Copy a single valuepair.
#define RDEBUG_ENABLED2()
#define pair_append_request(_attr, _da)
Allocate and append a fr_pair_t to the request list.
Stores an attribute, a value and various bits of other data.
bool fr_pair_list_empty(fr_pair_list_t const *list)
Is a valuepair list empty.
fr_pair_t * fr_pair_remove(fr_pair_list_t *list, fr_pair_t *vp)
Remove fr_pair_t from a list without freeing.
static int fr_pair_find_or_append_by_da(TALLOC_CTX *ctx, fr_pair_t **out, fr_pair_list_t *list, fr_dict_attr_t const *da)