oscore: enable external credential storage and safe recipient cleanup

Add reference counting to recipient contexts so they can be safely removed
while still in use by active sessions. Expose callback hooks for credential
lookup, echo, and sequence storage to allow custom backends.
Expand example server to demonstrate the external storage via a simple
file based database. Expand coap_oscore_rcp_conf_t to enable settings
preconfigured echo value, sequence counter and window value as uint64
Enable temporary credentials returned by the find function and are destroyed
after use.
This commit is contained in:
Lucien Zuercher authored and Jon Shallow committed 2026-05-20 13:09:24 +01:00
1 parent 48559f0016
commit 855d53ef10
17 files changed
+1774 -98

No files matched your search

+4
View File
@@ -20,6 +20,7 @@
#include "coap_subscribe.h"
#include "coap_resource.h"
#include "coap_oscore.h"
#ifdef __cplusplus
extern "C" {
@@ -102,6 +103,9 @@ struct coap_context_t {
#endif /* RIOT_VERSION */
#if COAP_OSCORE_SUPPORT
struct oscore_ctx_t *p_osc_ctx; /**< primary oscore context */
coap_oscore_find_handler_t oscore_find_cb; /**< Optional override for oscore_find_context() */
coap_oscore_update_seq_num_handler_t
oscore_update_seq_num_cb; /**< Optional function to call to update sequence number and window values */
#endif /* COAP_OSCORE_SUPPORT */
#if COAP_CLIENT_SUPPORT
+100 -5
View File
@@ -32,6 +32,56 @@ extern "C" {
* @{
*/
/**
* Callback function type for overriding oscore_find_context().
*
* If set via coap_oscore_register_external_handlers(), this function is
* called before the internal oscore_find_context() to locate the OSCORE
* recipient and security context for an incoming request.
*
* The implementation of this function should be combined with
* @ref coap_oscore_update_seq_num_handler_t if sequence counter management is required.
* Otherwise, libcoap will lose those values
* and replay protection will not work properly.
*
* @param session The active CoAP session receiving the request from.
* @param rcpkey_id The Recipient Key ID (KID).
* @param ctxkey_id The ID Context to match or NULL.
*
* @return The OSCORE config retrieved from the custom OSCORE storage or NULL if not found.
* Will fallback to libcoap internal credential storage lookup.
*/
typedef coap_oscore_conf_t *(*coap_oscore_find_handler_t)(
const coap_session_t *session,
const coap_bin_const_t *rcpkey_id,
const coap_bin_const_t *ctxkey_id
);
/**
* Callback function type for persisting the OSCORE receiver sequence number
* and anti-replay sliding window to an external storage.
*
* Called by the library whenever the Receiver Sequence Number or the
* anti-replay window is updated, giving the application the opportunity to
* store both values for external OSCORE credential management. Required
* to provide those values via the coap_oscore_find_handler_t callback, since
* the values will otherwise be lost.
*
* @param session The active CoAP session receiving the request from.
* @param rcpkey_id The Recipient ID for which the sequence number and window applies.
* @param ctxkey_id The ID Context for which the sequence number and window applies.
* @param receiver_seq_num The receiver sequence number.
* @param seq_num_window The 64-bit anti-replay sliding window bitmask.
* @return @c 1 if persisted successfully, else @c 0.
*/
typedef int (*coap_oscore_update_seq_num_handler_t)(
const coap_session_t *session,
const coap_bin_const_t *rcpkey_id,
const coap_bin_const_t *ctxkey_id,
uint64_t receiver_seq_num,
uint64_t seq_num_window
);
/**
* Creates a new client session to the designated server, protecting the data
* using OSCORE.
@@ -238,6 +288,7 @@ COAP_API coap_session_t *coap_new_client_session_oscore_pki3(coap_context_t *ctx
void *app_data,
coap_app_data_free_callback_t callback,
coap_str_const_t *ws_host);
/**
* Set the context's default OSCORE configuration for a server.
*
@@ -288,17 +339,45 @@ coap_oscore_conf_t *coap_new_oscore_conf(coap_str_const_t conf_mem,
*
* @param oscore_conf The OSCORE configuration structure to release.
*
* @return @c 1 Successfully removed, else @c 0 not found.
* @return @c 1 Successfully released, else @c 0 if not valid.
*/
int coap_delete_oscore_conf(coap_oscore_conf_t *oscore_conf);
/**
* Register external storage handlers for OSCORE session state.
*
* Allows providing a custom OSCORE credential storage for
* persistence and optimized for the needs of the application.
* Expands the built-in OSCORE context lookup and enables management
* of persistent OSCORE data (sequence numbers and Echo challenges)
* from within the application.
*
* @param context The CoAP context to configure.
* @param find_handler Inject a customized OSCORE config-lookup function
* to return temporary oscore credentials managed by
* an external credential store (see coap_oscore_find_handler_t),
* or @c NULL to only use the built-in oscore_find_context().
* @param update_seq_num_handler Called whenever the Sender Sequence Number
* or anti-replay window changes. Use this
* to synchronize values with the external
* credential storage. @c NULL to disable.
* If find_handler is set, then it is recommended that
* update_seq_num_handler is set.
*/
COAP_API void coap_oscore_register_external_handlers(
coap_context_t *context,
coap_oscore_find_handler_t find_handler,
coap_oscore_update_seq_num_handler_t update_seq_num_handler);
/**
* Add in the specific Recipient ID into the OSCORE context (server only).
* Note: This is only added to the OSCORE context as first defined by
* coap_new_client_session_oscore*() or coap_context_oscore_server().
*
* @param context The CoAP context to add the OSCORE recipient_id to.
* @param recipient_id The Recipient ID to add.
* @param context The CoAP context to add the OSCORE recipient_id to.
* @param recipient_id The Recipient ID to add. Ownership of memory moves into the function
* and will be freed off when the context is freed or if the
* function fails.
*
* @return @c 1 Successfully added, else @c 0 there is an issue.
*/
@@ -307,11 +386,11 @@ COAP_API int coap_new_oscore_recipient(coap_context_t *context,
/**
* Release all the information associated for the specific Recipient ID
* (and hence and stop any further OSCORE protection for this Recipient).
* (and hence stop any further OSCORE protection for this Recipient).
* Note: This is only removed from the OSCORE context as first defined by
* coap_new_client_session_oscore*() or coap_context_oscore_server().
*
* @param context The CoAP context holding the OSCORE recipient_id to.
* @param context The CoAP context holding the OSCORE recipient_id to be removed.
* @param recipient_id The Recipient ID to remove.
*
* @return @c 1 Successfully removed, else @c 0 not found.
@@ -319,6 +398,22 @@ COAP_API int coap_new_oscore_recipient(coap_context_t *context,
COAP_API int coap_delete_oscore_recipient(coap_context_t *context,
coap_bin_const_t *recipient_id);
/**
* Set the latest sequence number and sliding window for the specified recipient
* id in the compiled configuration file.
*
* @param oscore_conf The compiled configuration file.
* @param recipient_id The Recipient ID to update in @p oscore_conf.
* @param last_seq The sequence number to update the recipient id with.
* @param seq_window The sliding window to update the recipient id with.
*
* @return @c 1 Successfully updated, else @c 0 recipient id not found.
*/
COAP_API int coap_oscore_recipient_set_latest_seq(coap_oscore_conf_t *oscore_conf,
const coap_bin_const_t *recipient_id,
uint64_t last_seq,
uint64_t seq_window);
/** @} */
#ifdef __cplusplus
+61
View File
@@ -48,6 +48,12 @@ struct coap_oscore_rcp_conf_t {
coap_bin_const_t *recipient_id; /**< Recipient ID (i.e. local our id) */
/* Silent Server */
int silent_server; /**< 1 if server is likely to be silent else 0 */
/* SSN handling for rfc8613 B.1.2 */
uint8_t window_initialized; /**< Contains if the sliding window is initialized
@c 1 if initialized, @c 0 otherwise */
uint64_t last_seq; /**< Highest sequence number used for this recipient */
uint64_t sliding_window; /**< bitfield sequence counter window */
};
/**
@@ -145,6 +151,33 @@ struct coap_pdu_t *coap_oscore_decrypt_pdu(coap_session_t *session,
*/
void coap_delete_all_oscore(coap_context_t *context);
/**
* Attach the OSCORE recipient context information to the session.
*
* @param session The session to attach the recipient information to.
* @param recipient_ctx The recipient information to attach.
*/
void coap_oscore_session_set_recipient_ctx(coap_session_t *session,
oscore_recipient_ctx_t *recipient_ctx);
/**
* Set the recipient context of an association.
*
* @param association The association to set @p recipient for.
* @param recipient_ctx For which the reference counter will be increased.
*/
void coap_oscore_association_set_recipient_ctx(oscore_association_t *association,
oscore_recipient_ctx_t *recipient_ctx);
/**
* Verify if the OSCORE context is attached to the @p c_context .
*
* @param c_context The context to check for the OSCORE context.
* @param oscore_ctx The OSCORE context to check for.
* @return @c 1 if the OSCORE context is attached to the @p c_context , else @c 0.
*/
int coap_oscore_is_attached(coap_context_t *c_context, oscore_ctx_t *oscore_ctx);
/**
* Cleanup all allocated OSCORE association information.
*
@@ -275,6 +308,17 @@ coap_session_t *coap_new_client_session_oscore3_lkd(coap_context_t *ctx,
coap_app_data_free_callback_t callback,
coap_str_const_t *ws_host);
/**
* Initializes an OSCORE context from the given configuration.
*
* @param oscore_conf The OSCORE configuration information to use to create
* the OSCORE context.
* Will alwatys be freed by this call.
*
* @return The created OSCORE context or NULL on failure.
*/
oscore_ctx_t *coap_init_oscore_context_from_conf(coap_oscore_conf_t *oscore_conf);
/**
* Creates a new client session to the designated server, with PKI credentials
* protecting the data using OSCORE, along with app_data information (as per
@@ -378,6 +422,23 @@ coap_session_t *coap_new_client_session_oscore_psk3_lkd(coap_context_t *ctx,
int coap_new_oscore_recipient_lkd(coap_context_t *context,
coap_bin_const_t *recipient_id);
/**
* Set the latest sequence number and sliding window for the specified recipient
* id in the compiled configuration file.
*
* Note: This function must be called in the locked state.
*
* @param oscore_conf The compiled configuration file.
* @param recipient_id The Recipient ID to update in @p oscore_conf.
* @param last_seq The sequence number to update the recipient id with.
* @param seq_window The sliding window to update the recipient id with.
*
* @return @c 1 Successfully updated, else @c 0 recipient id not found.
*/
int coap_oscore_recipient_set_latest_seq_lkd(coap_oscore_conf_t *oscore_conf,
const coap_bin_const_t *recipient_id,
uint64_t last_seq, uint64_t seq_window);
/** @} */
#ifdef __cplusplus