From df9071c93efff2e648257314198268c7c1d79980 Mon Sep 17 00:00:00 2001 From: Olaf Bergmann Date: Thu, 21 Oct 2021 09:57:29 +0200 Subject: [PATCH] coap_session.[hc]: Added function to retrieve PSK identity from session The new function coap_session_get_psk_identity() returns the given session's psk_identity as provided during the handhake (for CoAP server) or a session setup (for CoAP client). --- include/coap3/coap_session.h | 9 +++++++++ libcoap-3.map | 1 + libcoap-3.sym | 1 + src/coap_session.c | 12 ++++++++++++ 4 files changed, 23 insertions(+) diff --git a/include/coap3/coap_session.h b/include/coap3/coap_session.h index cf250bcfdd..5dc03d4072 100644 --- a/include/coap3/coap_session.h +++ b/include/coap3/coap_session.h @@ -287,6 +287,15 @@ coap_session_t *coap_new_client_session_psk2( const coap_bin_const_t * coap_session_get_psk_hint( const coap_session_t *session); +/** + * Get the server session's current PSK identity (PSK). + * + * @param session The current coap_session_t object. + * + * @return PSK identity if successful, else @c NULL. + */ +const coap_bin_const_t *coap_session_get_psk_identity( + const coap_session_t *session); /** * Get the session's current pre-shared key (PSK). * diff --git a/libcoap-3.map b/libcoap-3.map index 92a37b5f6e..8cbdadf9a7 100644 --- a/libcoap-3.map +++ b/libcoap-3.map @@ -188,6 +188,7 @@ global: coap_session_get_max_retransmit; coap_session_get_proto; coap_session_get_psk_hint; + coap_session_get_psk_identity; coap_session_get_psk_key; coap_session_get_state; coap_session_get_tls; diff --git a/libcoap-3.sym b/libcoap-3.sym index 43adfcb1d2..42eb8c0142 100644 --- a/libcoap-3.sym +++ b/libcoap-3.sym @@ -186,6 +186,7 @@ coap_session_get_ifindex coap_session_get_max_retransmit coap_session_get_proto coap_session_get_psk_hint +coap_session_get_psk_identity coap_session_get_psk_key coap_session_get_state coap_session_get_tls diff --git a/src/coap_session.c b/src/coap_session.c index 4b20774f3b..891e3a8d31 100644 --- a/src/coap_session.c +++ b/src/coap_session.c @@ -1131,6 +1131,18 @@ coap_session_get_psk_hint(const coap_session_t *session) { } #endif /* COAP_SERVER_SUPPORT */ +const coap_bin_const_t * +coap_session_get_psk_identity(const coap_session_t *session) { + const coap_bin_const_t *psk_identity = NULL; + if (session) { + psk_identity = session->psk_identity; + if (psk_identity == NULL) { + psk_identity = &session->cpsk_setup_data.psk_info.identity; + } + } + return psk_identity; +} + const coap_bin_const_t * coap_session_get_psk_key(const coap_session_t *session) { if (session)