29 #define LOG_PREFIX "tls"
31 #include <freeradius-devel/server/exec.h>
32 #include <freeradius-devel/server/pair.h>
33 #include <freeradius-devel/tls/log.h>
34 #include <freeradius-devel/unlang/function.h>
35 #include <freeradius-devel/unlang/interpret.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>
54 static inline CC_HINT(always_inline)
55 bool verify_applies(fr_tls_verify_mode_t mode,
int depth,
int untrusted)
57 if (mode == FR_TLS_VERIFY_MODE_ALL)
return true;
58 if (mode == FR_TLS_VERIFY_MODE_DISABLED)
return false;
60 if ((mode & FR_TLS_VERIFY_MODE_LEAF) && (
depth == 0))
return true;
61 if ((mode & FR_TLS_VERIFY_MODE_ISSUER) && (
depth == 1))
return true;
62 if ((mode & FR_TLS_VERIFY_MODE_UNTRUSTED) && (
depth < untrusted))
return true;
73 static void tls_verify_error_detail(
request_t *request, SSL_CTX *ctx,
int err)
75 X509_STORE *
store = SSL_CTX_get_ex_data(ctx, FR_TLS_EX_CTX_INDEX_VERIFY_STORE);
83 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
90 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
91 RDEBUG2(
"Static certificates in verification store are");
94 fr_tls_x509_objects_log(request,
L_DBG, X509_STORE_get0_objects(
store));
130 int fr_tls_verify_cert_cb(
int ok, X509_STORE_CTX *x509_ctx)
136 fr_tls_session_t *tls_session;
145 cert = X509_STORE_CTX_get_current_cert(x509_ctx);
146 err = X509_STORE_CTX_get_error(x509_ctx);
147 depth = X509_STORE_CTX_get_error_depth(x509_ctx);
148 untrusted = X509_STORE_CTX_get_num_untrusted(x509_ctx);
154 ssl = X509_STORE_CTX_get_ex_data(x509_ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
155 ssl_ctx = SSL_get_SSL_CTX(ssl);
156 conf = fr_tls_session_conf(ssl);
157 tls_session = talloc_get_type_abort(SSL_get_ex_data(ssl, FR_TLS_EX_INDEX_TLS_SESSION), fr_tls_session_t);
158 request = fr_tls_session_request(tls_session->ssl);
165 if (!tls_session->can_pause) {
167 "tls_session_async_handshake_cont must be in call stack", __FUNCTION__);
176 X509_STORE_CTX_set_error(x509_ctx, 0);
182 STACK_OF(X509) *our_chain;
185 our_chain = X509_STORE_CTX_get0_chain(x509_ctx);
186 RDEBUG3(
"Certificate chain - %i cert(s) untrusted", untrusted);
187 for (i = sk_X509_num(our_chain); i > 0 ; i--) {
188 X509 *this_cert = sk_X509_value(our_chain, i - 1);
190 X509_NAME_oneline(X509_get_subject_name(this_cert), subject,
sizeof(subject));
191 subject[
sizeof(subject) - 1] =
'\0';
193 RDEBUG3(
"%s [%i] %s", this_cert == cert ?
">" :
" ", i - 1, subject);
203 char const *p = X509_verify_cert_error_string(
err);
204 if (!verify_applies(
conf->verify.mode,
depth, untrusted) ||
205 ((
conf->verify.allow_expired_crl) && (
err == X509_V_ERR_CRL_HAS_EXPIRED)) ||
206 ((
conf->verify.allow_not_yet_valid_crl) && (
err == X509_V_ERR_CRL_NOT_YET_VALID))) {
207 RDEBUG2(
"Ignoring verification error - %s (%i)", p,
err);
208 tls_verify_error_detail(request, ssl_ctx,
err);
211 X509_STORE_CTX_set_error(x509_ctx, 0);
213 RERROR(
"Verification error - %s (%i)", p,
err);
214 tls_verify_error_detail(request, ssl_ctx,
err);
219 if (verify_applies(
conf->verify.attribute_mode,
depth, untrusted) &&
234 i <= (
unsigned int)
depth;
241 #ifdef STATIC_ANALYZER
256 if (fr_tls_session_pairs_from_x509_cert(&container->vp_group, container,
257 request, cert) < 0) {
276 if (my_ok && (
depth == 0)) {
277 if (
conf->virtual_server && tls_session->verify_client_cert) {
278 RDEBUG2(
"Requesting certificate validation");
288 fr_tls_verify_cert_request(tls_session, SSL_session_reused(tls_session->ssl));
303 X509_STORE_CTX_set_error(x509_ctx, 0);
304 fr_tls_verify_cert_reset(tls_session);
313 if (!fr_tls_verify_cert_result(tls_session)) {
314 REDEBUG(
"Certificate validation failed");
316 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_APPLICATION_VERIFICATION);
321 tls_session->client_cert_ok = (my_ok > 0);
322 RDEBUG2(
"[verify] = %s", my_ok ?
"ok" :
"invalid");
341 int fr_tls_verify_cert_chain(
request_t *request, SSL *ssl)
348 STACK_OF(X509) *chain;
351 X509_STORE_CTX *store_ctx;
356 cert = SSL_get0_peer_certificate(ssl);
359 ssl_ctx = SSL_get_SSL_CTX(ssl);
360 store_ctx = X509_STORE_CTX_new();
361 chain = SSL_get_peer_cert_chain(ssl);
362 store = SSL_CTX_get_ex_data(ssl_ctx, FR_TLS_EX_CTX_INDEX_VERIFY_STORE);
380 X509_STORE_CTX_init(store_ctx,
store, cert, chain);
381 X509_STORE_CTX_set_ex_data(store_ctx, SSL_get_ex_data_X509_STORE_CTX_idx(), ssl);
382 X509_STORE_CTX_set_verify_cb(store_ctx, fr_tls_verify_cert_cb);
384 verify = X509_verify_cert(store_ctx);
386 err = X509_STORE_CTX_get_error(store_ctx);
388 if (
err != X509_V_OK) {
389 REDEBUG(
"Failed re-validating resumed session: %s", X509_verify_cert_error_string(
err));
394 X509_STORE_CTX_free(store_ctx);
405 fr_tls_session_t *tls_session = talloc_get_type_abort(
uctx, fr_tls_session_t);
408 fr_assert(tls_session->validate.state == FR_TLS_VALIDATION_REQUESTED);
412 REDEBUG(
"Failed (re-)validating certificates");
413 tls_session->validate.state = FR_TLS_VALIDATION_FAILED;
417 tls_session->validate.state = FR_TLS_VALIDATION_SUCCESS;
419 RDEBUG2(
"Certificates (re-)validated");
435 fr_tls_conf_t *
conf = fr_tls_session_conf(tls_session->ssl);
446 fr_tls_session_extra_pairs_copy_to_child(child, tls_session);
464 vp->vp_bool = tls_session->validate.resumed;
470 ua = fr_tls_call_push(child, tls_verify_client_cert_result,
conf, tls_session);
472 PERROR(
"Failed calling TLS virtual server");
489 bool fr_tls_verify_cert_result(fr_tls_session_t *tls_session)
493 fr_assert(tls_session->validate.state != FR_TLS_VALIDATION_INIT);
495 result = tls_session->validate.state == FR_TLS_VALIDATION_SUCCESS;
497 tls_session->validate.state = FR_TLS_VALIDATION_INIT;
498 tls_session->validate.resumed =
false;
506 void fr_tls_verify_cert_reset(fr_tls_session_t *tls_session)
508 tls_session->validate.state = FR_TLS_VALIDATION_INIT;
509 tls_session->validate.resumed =
false;
515 void fr_tls_verify_cert_request(fr_tls_session_t *tls_session,
bool session_resumed)
517 fr_assert(tls_session->validate.state == FR_TLS_VALIDATION_INIT);
519 tls_session->validate.state = FR_TLS_VALIDATION_REQUESTED;
520 tls_session->validate.resumed = session_resumed;
533 if (tls_session->validate.state == FR_TLS_VALIDATION_REQUESTED) {
534 return tls_verify_client_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)
#define DIAG_UNKNOWN_PRAGMAS
#define FALL_THROUGH
clang 10 doesn't recognised the FALL-THROUGH comment anymore
fr_dcursor_eval_t void const * uctx
#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.
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_t const * dict_tls
HIDDEN fr_dict_attr_t const * attr_tls_session_resumed
fr_value_box_t const * enum_tls_packet_type_success
fr_value_box_t const * enum_tls_packet_type_verify_certificate
@ 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)
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.
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_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_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_copy(TALLOC_CTX *ctx, fr_pair_t const *vp)
Copy a single valuepair.
#define RDEBUG_ENABLED2()
rlm_rcode_t
Return codes indicating the result of the module call.
#define pair_append_request(_attr, _da)
Allocate and append a fr_pair_t to the request list.
#define pair_prepend_request(_attr, _da)
Allocate and prepend a fr_pair_t to the request list.
MEM(pair_append_request(&vp, attr_eap_aka_sim_identity) >=0)
Stores an attribute, a value and various bits of other data.
request_t * unlang_subrequest_alloc(request_t *parent, fr_dict_t const *namespace)
Allocate a subrequest to run through a virtual server at some point in the future.
bool fr_pair_list_empty(fr_pair_list_t const *list)
Is a valuepair list empty.