Skip to contentSkip to conversation

tls: Refactor session storage for server mode

PATCH In review Andrew Zaborowski 2 participants 4 messages Nov 7, 2022 – Nov 8, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 tls: Refactor session storage for server mode

In TLS server mode we will need to store multiple session states in the session cache instead of only one like on the client. While the l_tls_set_session_cache() method is new use to opportunity to make incompatible changes: add the cache size limit parameter and strip "TLS" prefix from the setting names in the cache's l_settings object since the group name can include this part of the context without duplicating it across settings. Rename the group_name parameter to group_prefix since in server mode there will be a settings group per session cached so that we can use identical keys for each session. When removing a session remove the whole group rather than individual settings potentially leaving an empty group. Factor out the session loading code common to client and server into a function. When saving a server side session state skip the SessionID setting since the session ID is in the group name as the primary index. Add a boolean parameter to tls_forget_cached_session() to suppress the update_cb callback if needed, to allow the caller to make a single callback after multiple changes are made in the session cache instead of calling back after each individual change.

---
 ell/tls-private.h |   3 +-
 ell/tls.c         | 228 ++++++++++++++++++++++++++--------------------
 ell/tls.h         |   3 +-
 3 files changed, 133 insertions(+), 101 deletions(-)
ell/tls-private.h +2 -1
index 7156666..ce69553 100644
@@ -219,8 +219,9 @@ struct l_tls {
 	struct tls_cipher_suite **cipher_suite_pref_list;
 
 	struct l_settings *session_settings;
-	char *session_group;
+	char *session_prefix;
 	uint64_t session_lifetime;
+	unsigned int session_count_max;
 	l_tls_session_update_cb_t session_update_cb;
 	void *session_update_user_data;
 
ell/tls.c +129 -99

352 lines not on this page · raw message

ell/tls.h +2 -1
index 92d8b9e..e688c7c 100644
@@ -130,7 +130,8 @@ void l_tls_set_version_range(struct l_tls *tls,
 void l_tls_set_domain_mask(struct l_tls *tls, char **mask);
 
 void l_tls_set_session_cache(struct l_tls *tls, struct l_settings *settings,
-				const char *group_name, uint64_t lifetime,
+				const char *group_prefix, uint64_t lifetime,
+				unsigned int max_sessions,
 				l_tls_session_update_cb_t update_cb,
 				void *user_data);
 
Signature · 1 line
2.34.1
#1 lore raw

PATCH 2/3 tls: Server mode session caching

Cache session states when they're established, load them when parsing the ClientHello. When loading first expire old sessions, look up the requested session among those left and enforce the cache size limit by dropping the oldest session if cache is over limit.

---
 ell/tls-private.h |   2 +
 ell/tls.c         | 343 ++++++++++++++++++++++++++++++++++++++++------
 2 files changed, 305 insertions(+), 40 deletions(-)
ell/tls-private.h +2 -0
index ce69553..a3a9393 100644
@@ -260,6 +260,8 @@ struct l_tls {
 
 	uint8_t session_id[32];
 	size_t session_id_size;
+	uint8_t session_id_replaced[32];
+	size_t session_id_size_replaced;
 	bool session_id_new;
 	uint8_t session_cipher_suite_id[2];
 	uint8_t session_compression_method_id;
ell/tls.c +303 -40

488 lines not on this page · raw message

Signature · 1 line
2.34.1
#2 lore raw

PATCH 3/3 examples: Update https example code

In reply to Andrew Zaborowski

Update the l_tls_set_session_cache call signature in https-client-test and add similar session caching support in https-server-test.

---
 examples/https-client-test.c |  2 +-
 examples/https-server-test.c | 43 ++++++++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+), 1 deletion(-)
examples/https-client-test.c +1 -1
index 2c6939a..6b12f77 100644
@@ -238,7 +238,7 @@ int main(int argc, char *argv[])
 		l_settings_load_from_file(session_cache, session_cache_path);
 
 		l_tls_set_session_cache(tls, session_cache, hostname,
-					24 * 3600 * L_USEC_PER_SEC,
+					24 * 3600 * L_USEC_PER_SEC, 0,
 					https_tls_session_cache_update_cb,
 					NULL);
 	}
examples/https-server-test.c +43 -0

67 lines not on this page · raw message

Signature · 1 line
2.34.1
#3 lore raw
In reply to Andrew Zaborowski

Hi Andrew,

Quoted 18 lines · Andrew Zaborowski

In TLS server mode we will need to store multiple session states in the session cache instead of only one like on the client. While the l_tls_set_session_cache() method is new use to opportunity to make incompatible changes: add the cache size limit parameter and strip "TLS" prefix from the setting names in the cache's l_settings object since the group name can include this part of the context without duplicating it across settings. Rename the group_name parameter to group_prefix since in server mode there will be a settings group per session cached so that we can use identical keys for each session. When removing a session remove the whole group rather than individual settings potentially leaving an empty group. Factor out the session loading code common to client and server into a function. When saving a server side session state skip the SessionID setting since the session ID is in the group name as the primary index. Add a boolean parameter to tls_forget_cached_session() to suppress the update_cb callback if needed, to allow the caller to make a single callback after multiple changes are made in the session cache instead of calling back after each individual change.

Quoted 6 lines
---
  ell/tls-private.h |   3 +-
  ell/tls.c         | 228 ++++++++++++++++++++++++++--------------------
  ell/tls.h         |   3 +-
  3 files changed, 133 insertions(+), 101 deletions(-)

All applied, thanks.

Regards,
-Denis