From patchwork Thu Nov 17 13:56:08 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Zaborowski X-Patchwork-Id: 13046911 Received: from mail-wm1-f50.google.com (mail-wm1-f50.google.com [209.85.128.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E65662FAF for ; Thu, 17 Nov 2022 13:56:23 +0000 (UTC) Received: by mail-wm1-f50.google.com with SMTP id r126-20020a1c4484000000b003cffd336e24so475027wma.4 for ; Thu, 17 Nov 2022 05:56:23 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=RxIXBsPersiO9Fq+Cd6pMlap2olBxokqNvxZf6efH3M=; b=4Niby60JyYz1EPiajGEFikZAkjPkhCgRrE1cjKA5oBvyDdMm47pk9s+sKZBKarLVHN UbKotEiNIr5H6Sp5SIh/Zv0JSQtVSURQppO/6MtLcllSk7cs6rFTVxcg18g6JstL0HAb sZZTMJrz43IlrH/PCdEg0iTW6dZ7F+Y5jRfL/f6daqzblPEdrr2F/mvzfIwxFUnetIqJ XRfs8OES4L31v62jMvVvT64KPFV3IP0cno6gLJ4abMCUE3P6B6OP3mcNx/ERquGE1hW9 7+DGwMVFD9j4xw+zX0ABGz3jT/+8Y8bbl5bJwTSLFyLdRLkZsJ9gKLmkAPYfZvTRJ2Bl 6FWQ== X-Gm-Message-State: ANoB5pkuWohzRhUvZh1AlcQI62VFr3GRCpry4JlWZDK4FnZYXqRsg4ht QVDx7E36RKacgA5IUxhahOH//G2+jns= X-Google-Smtp-Source: AA0mqf7KmI4JXd5R3Jcz4jjxzR9tQfN4FYUF4laoGMuYoo2qZ/taXKwu0QXnrE8EOrRR7HoTwG1Hcw== X-Received: by 2002:a1c:e917:0:b0:3cf:681a:43e1 with SMTP id q23-20020a1ce917000000b003cf681a43e1mr1685877wmc.126.1668693381443; Thu, 17 Nov 2022 05:56:21 -0800 (PST) Received: from localhost.localdomain ([82.213.230.158]) by smtp.gmail.com with ESMTPSA id h5-20020a5d6885000000b002366b17ca8bsm1142632wru.108.2022.11.17.05.56.20 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Nov 2022 05:56:20 -0800 (PST) From: Andrew Zaborowski To: iwd@lists.linux.dev Subject: [PATCH 1/3] storage: Refactor storage_tls_session_{load,sync} Date: Thu, 17 Nov 2022 14:56:08 +0100 Message-Id: <20221117135610.1162965-1-andrew.zaborowski@intel.com> X-Mailer: git-send-email 2.34.1 Precedence: bulk X-Mailing-List: iwd@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Minor changes to these two methods resulting from two rewrites of them. Actual changes are: * storage_tls_session_sync parameter is const, * more specific naming, * storage_tls_session_load will return an empty l_settings instead of NULL so eap-tls-common.c doesn't have to handle this. storage.c makes no assumptions about the group names in the l_settings object and keeps no reference to that object, eap-tls-common.c is going to maintain the memory copy of the cache since this cache and the disk copy of it are reserved for EAP methods only. --- src/storage.c | 39 +++++++++++++++++++++++---------------- src/storage.h | 4 ++-- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/src/storage.c b/src/storage.c index b2c5ed48..4200030c 100644 --- a/src/storage.c +++ b/src/storage.c @@ -53,7 +53,7 @@ #define STORAGE_FILE_MODE (S_IRUSR | S_IWUSR) #define KNOWN_FREQ_FILENAME ".known_network.freq" -#define TLS_CACHE_FILENAME ".tls-session-cache" +#define EAP_TLS_CACHE_FILENAME ".eap-tls-session-cache" static char *storage_path = NULL; static char *storage_hotspot_path = NULL; @@ -702,29 +702,35 @@ void storage_known_frequencies_sync(struct l_settings *known_freqs) l_free(known_freq_file_path); } -struct l_settings *storage_tls_session_cache_load(void) +struct l_settings *storage_eap_tls_cache_load(void) { - _auto_(l_settings_free) struct l_settings *cache = l_settings_new(); - _auto_(l_free) char *tls_cache_file_path = - storage_get_path("%s", TLS_CACHE_FILENAME); + _auto_(l_free) char *path = + storage_get_path("%s", EAP_TLS_CACHE_FILENAME); + struct l_settings *cache = l_settings_new(); - if (unlikely(!l_settings_load_from_file(cache, tls_cache_file_path))) - return NULL; + if (!l_settings_load_from_file(cache, path)) + l_debug("No session cache loaded from %s, starting with an " + "empty cache", path); - return l_steal_ptr(cache); + return cache; } -void storage_tls_session_cache_sync(struct l_settings *cache) +void storage_eap_tls_cache_sync(const struct l_settings *cache) { - _auto_(l_free) char *tls_cache_file_path = NULL; + _auto_(l_free) char *path = + storage_get_path("%s", EAP_TLS_CACHE_FILENAME); + _auto_(l_free) char *settings_data = NULL; _auto_(l_free) char *data = NULL; size_t len; + static const char comment[] = + "# External changes to this file are not tracked by IWD " + "and will be overwritten.\n\n"; + static const size_t comment_len = L_ARRAY_SIZE(comment) - 1; - if (!cache) - return; - - tls_cache_file_path = storage_get_path("%s", TLS_CACHE_FILENAME); - data = l_settings_to_data(cache, &len); + settings_data = l_settings_to_data(cache, &len); + data = l_malloc(comment_len + len); + memcpy(data, comment, comment_len); + memcpy(data + comment_len, settings_data, len); /* * Note this data contains cryptographic secrets. write_file() @@ -732,7 +738,8 @@ void storage_tls_session_cache_sync(struct l_settings *cache) * * TODO: consider encrypting with system_key. */ - write_file(data, len, false, "%s", tls_cache_file_path); + write_file(data, comment_len + len, false, "%s", path); + explicit_bzero(settings_data, len); explicit_bzero(data, len); } diff --git a/src/storage.h b/src/storage.h index fe6ddbf5..04b50882 100644 --- a/src/storage.h +++ b/src/storage.h @@ -51,8 +51,8 @@ int storage_network_remove(enum security type, const char *ssid); struct l_settings *storage_known_frequencies_load(void); void storage_known_frequencies_sync(struct l_settings *known_freqs); -struct l_settings *storage_tls_session_cache_load(void); -void storage_tls_session_cache_sync(struct l_settings *cache); +struct l_settings *storage_eap_tls_cache_load(void); +void storage_eap_tls_cache_sync(const struct l_settings *cache); int __storage_decrypt(struct l_settings *settings, const char *ssid, bool *changed); From patchwork Thu Nov 17 13:56:09 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Zaborowski X-Patchwork-Id: 13046912 Received: from mail-wr1-f53.google.com (mail-wr1-f53.google.com [209.85.221.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 31BAB322E for ; Thu, 17 Nov 2022 13:56:25 +0000 (UTC) Received: by mail-wr1-f53.google.com with SMTP id bs21so3968702wrb.4 for ; Thu, 17 Nov 2022 05:56:25 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=fT0BBDMItAM4rvenq+WBmMAoq/PI0H/P9JS64FW5VJ4=; b=dFHZXHzN1ZGw9f5x7IwlhoQlWQqzV3bn/Fr7snu5Z+XzrpTi7rsL2Ul5RA0hWWcP8h N79wn9ChXJMrT+KftIDTethWD+p124FOB1Q9sAPMJfYu/9inSoHySFu1h0FlEvJpezbw 3Xkr1LH/YfzpY5jTeE2FInpnLoRBd4lfUy6FcuR1KBTmCoXZy8GY0GWwSCJoTL543mbs z3LQGBGWBU4E4q4xjg5fnRHQT+1ejYVQp/RkJvNNUtWsDmGUYQb39lE3Y/ESsXc9C3Yz DIz0/RzNnqyDBiw2Ubn9C/McaUuMCGWn93v7M7mOOMdSpYDPoCn+aSalZDatCFvr1tTy /GuQ== X-Gm-Message-State: ANoB5pnUu1IkTT5Sfhy3c5a6NMjrDkXEIijx4AFx1K5HozhUQ7q5QZE5 U8FzDAg6OxmAb5J2qfqC7hSAJtSCR58= X-Google-Smtp-Source: AA0mqf50WJckTk4W+KMZxadQlikv72Tl33wW1gXm4wCwh/m5T+AZAXzn8E7BiZ64uTC5AjC2FhkPEA== X-Received: by 2002:a5d:6088:0:b0:22e:5149:441d with SMTP id w8-20020a5d6088000000b0022e5149441dmr1540310wrt.661.1668693382934; Thu, 17 Nov 2022 05:56:22 -0800 (PST) Received: from localhost.localdomain ([82.213.230.158]) by smtp.gmail.com with ESMTPSA id h5-20020a5d6885000000b002366b17ca8bsm1142632wru.108.2022.11.17.05.56.21 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Nov 2022 05:56:21 -0800 (PST) From: Andrew Zaborowski To: iwd@lists.linux.dev Subject: [PATCH 2/3] eap-tls: Add session caching Date: Thu, 17 Nov 2022 14:56:09 +0100 Message-Id: <20221117135610.1162965-2-andrew.zaborowski@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221117135610.1162965-1-andrew.zaborowski@intel.com> References: <20221117135610.1162965-1-andrew.zaborowski@intel.com> Precedence: bulk X-Mailing-List: iwd@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Use l_tls_set_session_cache() to enable session cache/resume in the TLS-based EAP methods. Sessions for all 802.1x networks are stored in one l_settings object. eap_{get,set}_peer_id() API is added for the upper layers to set the identifier of the authenticator (or the supplicant if we're the authenticator, if there's ever a use case for that.) eap-tls-common.c can't call storage_eap_tls_cache_{load,sync}() or known_networks_watch_add() (to handle known network removals) because it's linked into some executables that don't have storage.o, knownnetworks.o or common.o so an upper layer (station.c) will call eap_tls_set_session_cache_ops() and eap_tls_forget_peer() as needed. --- src/eap-tls-common.c | 58 ++++++++++++++++++++++++++++++++++++++++++++ src/eap-tls-common.h | 7 ++++++ src/eap.c | 13 ++++++++++ src/eap.h | 3 +++ 4 files changed, 81 insertions(+) diff --git a/src/eap-tls-common.c b/src/eap-tls-common.c index acc5b387..9a4450e9 100644 --- a/src/eap-tls-common.c +++ b/src/eap-tls-common.c @@ -28,7 +28,9 @@ #include #include +#include "ell/useful.h" #include "src/missing.h" +#include "src/module.h" #include "src/eap.h" #include "src/eap-private.h" #include "src/eap-tls-common.h" @@ -123,6 +125,10 @@ struct eap_tls_state { void *variant_data; }; +static struct l_settings *eap_tls_session_cache; +static eap_tls_session_cache_load_func_t eap_tls_session_cache_load; +static eap_tls_session_cache_sync_func_t eap_tls_session_cache_sync; + static void __eap_tls_common_state_reset(struct eap_tls_state *eap_tls) { eap_tls->version_negotiated = EAP_TLS_VERSION_NOT_NEGOTIATED; @@ -571,6 +577,15 @@ static int eap_tls_handle_fragmented_request(struct eap_state *eap, return r; } +static void eap_tls_session_cache_update(void *user_data) +{ + if (L_WARN_ON(!eap_tls_session_cache_sync) || + L_WARN_ON(!eap_tls_session_cache)) + return; + + eap_tls_session_cache_sync(eap_tls_session_cache); +} + static bool eap_tls_tunnel_init(struct eap_state *eap) { struct eap_tls_state *eap_tls = eap_get_data(eap); @@ -633,6 +648,17 @@ static bool eap_tls_tunnel_init(struct eap_state *eap) if (eap_tls->domain_mask) l_tls_set_domain_mask(eap_tls->tunnel, eap_tls->domain_mask); + if (!eap_tls_session_cache_load) + goto start; + + if (!eap_tls_session_cache) + eap_tls_session_cache = eap_tls_session_cache_load(); + + l_tls_set_session_cache(eap_tls->tunnel, eap_tls_session_cache, + eap_get_peer_id(eap), + 24 * 3600 * L_USEC_PER_SEC, 0, + eap_tls_session_cache_update, NULL); + start: if (!l_tls_start(eap_tls->tunnel)) { l_error("%s: Failed to start the TLS client", @@ -1085,3 +1111,35 @@ void eap_tls_common_tunnel_close(struct eap_state *eap) l_tls_close(eap_tls->tunnel); } + +void eap_tls_set_session_cache_ops(eap_tls_session_cache_load_func_t load, + eap_tls_session_cache_sync_func_t sync) +{ + eap_tls_session_cache_load = load; + eap_tls_session_cache_sync = sync; +} + +void eap_tls_forget_peer(const char *peer_id) +{ + if (L_WARN_ON(!eap_tls_session_cache_sync)) + return; + + if (!eap_tls_session_cache) + eap_tls_session_cache = eap_tls_session_cache_load(); + + if (l_settings_remove_group(eap_tls_session_cache, peer_id)) + eap_tls_session_cache_sync(eap_tls_session_cache); +} + +static int eap_tls_common_init(void) +{ + return 0; +} + +static void eap_tls_common_exit(void) +{ + l_settings_free(eap_tls_session_cache); + eap_tls_session_cache = NULL; +} + +IWD_MODULE(eap_tls_common, eap_tls_common_init, eap_tls_common_exit); diff --git a/src/eap-tls-common.h b/src/eap-tls-common.h index 174770c0..25f668e2 100644 --- a/src/eap-tls-common.h +++ b/src/eap-tls-common.h @@ -20,6 +20,9 @@ * */ +typedef struct l_settings *(*eap_tls_session_cache_load_func_t)(void); +typedef void (*eap_tls_session_cache_sync_func_t)(const struct l_settings *); + enum eap_tls_version { EAP_TLS_VERSION_0 = 0x00, EAP_TLS_VERSION_1 = 0x01, @@ -81,3 +84,7 @@ bool eap_tls_common_tunnel_prf_get_bytes(struct eap_state *eap, void eap_tls_common_tunnel_send(struct eap_state *eap, const uint8_t *data, size_t data_len); void eap_tls_common_tunnel_close(struct eap_state *eap); + +void eap_tls_set_session_cache_ops(eap_tls_session_cache_load_func_t load, + eap_tls_session_cache_sync_func_t sync); +void eap_tls_forget_peer(const char *peer_id); diff --git a/src/eap.c b/src/eap.c index 6f523f2f..981b6388 100644 --- a/src/eap.c +++ b/src/eap.c @@ -59,6 +59,7 @@ struct eap_state { char *identity; char *identity_setting; bool authenticator; + char *peer_id; int last_id; void *method_state; @@ -154,6 +155,7 @@ void eap_free(struct eap_state *eap) eap_free_common(eap); l_timeout_remove(eap->complete_timeout); + eap_set_peer_id(eap, NULL); l_free(eap); } @@ -837,6 +839,17 @@ err: return false; } +void eap_set_peer_id(struct eap_state *eap, const char *id) +{ + l_free(eap->peer_id); + eap->peer_id = l_strdup(id); +} + +const char *eap_get_peer_id(struct eap_state *eap) +{ + return eap->peer_id; +} + void eap_set_data(struct eap_state *eap, void *data) { eap->method_state = data; diff --git a/src/eap.h b/src/eap.h index f1e867f5..702caf24 100644 --- a/src/eap.h +++ b/src/eap.h @@ -94,6 +94,9 @@ const char *eap_get_identity(struct eap_state *eap); void eap_rx_packet(struct eap_state *eap, const uint8_t *pkt, size_t len); +void eap_set_peer_id(struct eap_state *eap, const char *id); +const char *eap_get_peer_id(struct eap_state *eap); + void __eap_set_config(struct l_settings *config); int eap_init(void); From patchwork Thu Nov 17 13:56:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Zaborowski X-Patchwork-Id: 13046913 Received: from mail-wr1-f53.google.com (mail-wr1-f53.google.com [209.85.221.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B67502FAF for ; Thu, 17 Nov 2022 13:56:26 +0000 (UTC) Received: by mail-wr1-f53.google.com with SMTP id a14so3996920wru.5 for ; Thu, 17 Nov 2022 05:56:26 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=qTwiBLJ3P7Kgl3k56usPrldnCs5dP9tAFKPaLaOL6/Q=; b=ObBr0o4azaRZbZ9+FUDFiYrPNzE93bazokILBEUxy44Z9GyfKMcrsAbX7/KcebGKaj Bv6adTVBKbCLAw11TxbsrpWtjw41TGM876DJaRc4E3hk2Tua/OsUOf0/LlqMjMOeyBK9 u0G7WipT8h0D48cij6Z8nYwz6EaHdbfIeNo8ePM/bfp3TvsGzosnbATL83AZly9ZKVTx gCujE3T2Q05mHgXHgPoYLNGtDozGvr6QE7nlIkyaXgY2Ne+gkQ5WoHN8WlYDXJioqiaf fD9EmjuBWaLgj9wx+shIbMgcSL4vvioNx/LrSpzjRNOggtGiVW6ZQlu4nbI3utGQuQ3K LdBw== X-Gm-Message-State: ANoB5pkDR3F8ORP1rbVNI6g2wVGJfrjb9BhKpOYrLTdegG2LU785dVXG K3QD/2cy/tNRN2k1nYCSppz9p7UjRlg= X-Google-Smtp-Source: AA0mqf5N6P7hhUfop+gN+7Ht9/kp036w3RcvWrKIx0CLG9CEV5+3fMB6w5YPF/FoANsxRKZfF7IZfg== X-Received: by 2002:a5d:5957:0:b0:22e:3e9:ba15 with SMTP id e23-20020a5d5957000000b0022e03e9ba15mr1490146wri.180.1668693384352; Thu, 17 Nov 2022 05:56:24 -0800 (PST) Received: from localhost.localdomain ([82.213.230.158]) by smtp.gmail.com with ESMTPSA id h5-20020a5d6885000000b002366b17ca8bsm1142632wru.108.2022.11.17.05.56.23 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Nov 2022 05:56:23 -0800 (PST) From: Andrew Zaborowski To: iwd@lists.linux.dev Subject: [PATCH 3/3] station, eapol: Set up eap-tls-common for session caching Date: Thu, 17 Nov 2022 14:56:10 +0100 Message-Id: <20221117135610.1162965-3-andrew.zaborowski@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221117135610.1162965-1-andrew.zaborowski@intel.com> References: <20221117135610.1162965-1-andrew.zaborowski@intel.com> Precedence: bulk X-Mailing-List: iwd@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Use eap_set_peer_id() to set a string identifying the TLS server, currently the hex-encoded SSID of the network, to be used as group name and primary key in the session cache l_settings object. Provide pointers to storage_eap_tls_cache_{load,sync} to eap-tls-common.c using eap_tls_set_session_cache_ops(). Listen to Known Network removed signals and call eap_tls_forget_peer() to have any session related to the network also dropped from the cache. --- src/eapol.c | 6 ++++++ src/station.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/eapol.c b/src/eapol.c index 4a1abd28..4d4e201d 100644 --- a/src/eapol.c +++ b/src/eapol.c @@ -2770,6 +2770,8 @@ void eapol_register(struct eapol_sm *sm) bool eapol_start(struct eapol_sm *sm) { if (sm->handshake->settings_8021x) { + _auto_(l_free) char *network_id = NULL; + sm->eap = eap_new(eapol_eap_msg_cb, eapol_eap_complete_cb, sm); if (!sm->eap) @@ -2785,6 +2787,10 @@ bool eapol_start(struct eapol_sm *sm) eap_set_key_material_func(sm->eap, eapol_eap_results_cb); eap_set_event_func(sm->eap, eapol_eap_event_cb); + + network_id = l_util_hexstring(sm->handshake->ssid, + sm->handshake->ssid_len); + eap_set_peer_id(sm->eap, network_id); } sm->started = true; diff --git a/src/station.c b/src/station.c index eab16eff..4aab7828 100644 --- a/src/station.c +++ b/src/station.c @@ -60,6 +60,9 @@ #include "src/sysfs.h" #include "src/band.h" #include "src/ft.h" +#include "src/eap.h" +#include "src/eap-tls-common.h" +#include "src/storage.h" static struct l_queue *station_list; static uint32_t netdev_watch; @@ -69,6 +72,7 @@ static bool anqp_disabled; static bool supports_arp_evict_nocarrier; static bool supports_ndisc_evict_nocarrier; static struct watchlist event_watches; +static uint32_t known_networks_watch; struct station { enum station_state state; @@ -5087,6 +5091,22 @@ static void station_netdev_watch(struct netdev *netdev, } } +static void station_known_networks_changed(enum known_networks_event event, + const struct network_info *info, + void *user_data) +{ + _auto_(l_free) char *network_id = NULL; + + if (event != KNOWN_NETWORKS_EVENT_REMOVED) + return; + + if (info->type != SECURITY_8021X) + return; + + network_id = l_util_hexstring(info->ssid, strlen(info->ssid)); + eap_tls_forget_peer(network_id); +} + static int station_init(void) { station_list = l_queue_new(); @@ -5139,6 +5159,12 @@ static int station_init(void) watchlist_init(&event_watches, NULL); + eap_tls_set_session_cache_ops(storage_eap_tls_cache_load, + storage_eap_tls_cache_sync); + known_networks_watch = known_networks_watch_add( + station_known_networks_changed, + NULL, NULL); + return 0; } @@ -5154,6 +5180,8 @@ static void station_exit(void) l_queue_destroy(station_list, NULL); station_list = NULL; watchlist_destroy(&event_watches); + known_networks_watch_remove(known_networks_watch); + known_networks_watch = 0; } IWD_MODULE(station, station_init, station_exit)