From 537d06f59398dfd41496fc6e37edf1ee521a180b Mon Sep 17 00:00:00 2001 From: usama khan Date: Wed, 21 Oct 2026 01:20:23 +0530 Subject: [PATCH] OSCORE: Roll back replay window on decryption failure oscore_validate_sender_seq() moves the replay window on before the message has been decrypted, and oscore_roll_back_seq() is meant to undo that when decryption fails. It only restored last_seq and sliding_window when the saved values were non zero, and never restored initial_state. For a recipient context still in its initial state (rfc8613_b_1_2 false), or one whose last_seq is 0, a single request with a forged Partial IV and an invalid tag left last_seq at the sender's chosen value, so the genuine peer's following requests were rejected as outside the replay window. The client did not roll back at all when a response failed decryption, and the server called oscore_update_seq_num_cb() with the new window before decrypting the request, so credential storage could be updated with a forged sequence number even when the in memory window was restored. Record whether there is a window update to undo and restore initial_state with it, roll back on response decryption failure as well, and only call oscore_update_seq_num_cb() once the request has been decrypted. Add a unit test. --- include/oscore/oscore_context.h | 2 ++ src/coap_oscore.c | 19 +++++++----- src/oscore/oscore.c | 10 +++--- tests/test_oscore.c | 55 +++++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+), 12 deletions(-) diff --git a/include/oscore/oscore_context.h b/include/oscore/oscore_context.h index 0f44973a..2dced4ec 100644 --- a/include/oscore/oscore_context.h +++ b/include/oscore/oscore_context.h @@ -131,6 +131,8 @@ struct oscore_recipient_ctx_t { uint64_t rollback_last_seq; uint8_t echo_value[8]; uint8_t initial_state; + uint8_t rollback_initial_state; + uint8_t rollback_valid; uint8_t silent_server; }; diff --git a/src/coap_oscore.c b/src/coap_oscore.c index 751e1182..ac995e68 100644 --- a/src/coap_oscore.c +++ b/src/coap_oscore.c @@ -1168,13 +1168,6 @@ coap_oscore_decrypt_pdu(coap_session_t *session, incoming_seq = coap_decode_var_bytes8(cose->partial_iv.s, cose->partial_iv.length); rcp_ctx->last_seq = incoming_seq; - } else if (session->context->oscore_update_seq_num_cb != NULL) { - /* notify custom credential storage */ - coap_lock_callback(session->context->oscore_update_seq_num_cb(session, - rcp_ctx->recipient_id, - osc_ctx->id_context, - rcp_ctx->last_seq, - rcp_ctx->sliding_window)); } } else { /* !coap_request */ /* @@ -1521,9 +1514,21 @@ coap_oscore_decrypt_pdu(coap_session_t *session, coap_handle_event_lkd(session->context, COAP_EVENT_OSCORE_DECRYPTION_FAILURE, session); + oscore_roll_back_seq(rcp_ctx); } goto error; } + /* Replay window update is now verified */ + rcp_ctx->rollback_valid = 0; + if (coap_request && rcp_ctx->initial_state == 0 && + session->context->oscore_update_seq_num_cb != NULL) { + /* notify custom credential storage */ + coap_lock_callback(session->context->oscore_update_seq_num_cb(session, + rcp_ctx->recipient_id, + osc_ctx->id_context, + rcp_ctx->last_seq, + rcp_ctx->sliding_window)); + } assert((size_t)pltxt_size < pdu->alloc_size + pdu->max_hdr_size); diff --git a/src/oscore/oscore.c b/src/oscore/oscore.c index 97650ef6..0fc0d652 100644 --- a/src/oscore/oscore.c +++ b/src/oscore/oscore.c @@ -441,6 +441,8 @@ oscore_validate_sender_seq(oscore_recipient_ctx_t *ctx, cose_encrypt0_t *cose) { ctx->rollback_last_seq = ctx->last_seq; ctx->rollback_sliding_window = ctx->sliding_window; + ctx->rollback_initial_state = ctx->initial_state; + ctx->rollback_valid = 1; /* Special case since we do not use unsigned int for seq */ if (ctx->initial_state == 1) { @@ -517,13 +519,11 @@ oscore_increment_sender_seq(oscore_ctx_t *ctx) { void oscore_roll_back_seq(oscore_recipient_ctx_t *ctx) { - if (ctx->rollback_sliding_window != 0) { + if (ctx->rollback_valid) { ctx->sliding_window = ctx->rollback_sliding_window; - ctx->rollback_sliding_window = 0; - } - if (ctx->rollback_last_seq != 0) { ctx->last_seq = ctx->rollback_last_seq; - ctx->rollback_last_seq = 0; + ctx->initial_state = ctx->rollback_initial_state; + ctx->rollback_valid = 0; } } diff --git a/tests/test_oscore.c b/tests/test_oscore.c index f50d2329..68ca9ac3 100644 --- a/tests/test_oscore.c +++ b/tests/test_oscore.c @@ -1470,6 +1470,59 @@ fail: oscore_free_contexts(ctx); } +/* + * A message that fails decryption must leave the replay window as it was, + * including when the window was still in its initial state. + */ +static void +t_replay_roll_back(void) { + static const char conf_data[] = + "master_secret,hex,\"0102030405060708090a0b0c0d0e0f10\"\n" + "master_salt,hex,\"9e7ca92223786340\"\n" + "sender_id,hex,\"01\"\n" + "recipient_id,hex,\"\"\n" + "rfc8613_b_1_2,bool,false\n"; + static const uint8_t forged_piv[] = { 0xff }; + static const uint8_t genuine_piv[] = { 0x14 }; + const coap_str_const_t conf = { sizeof(conf_data)-1, + (const uint8_t *)conf_data + }; + coap_oscore_conf_t *oscore_conf; + oscore_recipient_ctx_t *rcp_ctx; + cose_encrypt0_t cose[1]; + coap_bin_const_t piv; + + oscore_conf = coap_new_oscore_conf(conf, NULL, NULL, 0); + FailIf_CU_ASSERT_PTR_NOT_NULL(oscore_conf); + coap_context_oscore_server(ctx, oscore_conf); + FailIf_CU_ASSERT_PTR_NOT_NULL(ctx->p_osc_ctx); + rcp_ctx = ctx->p_osc_ctx->recipient_chain; + FailIf_CU_ASSERT_PTR_NOT_NULL(rcp_ctx); + + /* Forged message passes the replay check, then fails decryption */ + cose_encrypt0_init(cose); + piv.s = forged_piv; + piv.length = sizeof(forged_piv); + cose_encrypt0_set_partial_iv(cose, &piv); + CU_ASSERT(oscore_validate_sender_seq(rcp_ctx, cose) == 1); + oscore_roll_back_seq(rcp_ctx); + + CU_ASSERT(rcp_ctx->initial_state == 1); + CU_ASSERT(rcp_ctx->last_seq == 0); + CU_ASSERT(rcp_ctx->sliding_window == 0); + + /* Genuine message from the peer must still be accepted */ + cose_encrypt0_init(cose); + piv.s = genuine_piv; + piv.length = sizeof(genuine_piv); + cose_encrypt0_set_partial_iv(cose, &piv); + CU_ASSERT(oscore_validate_sender_seq(rcp_ctx, cose) == 1); + CU_ASSERT(rcp_ctx->last_seq == 0x14); + +fail: + oscore_free_contexts(ctx); +} + /************************************************************************ ** Per-recipient state (last_seq / sliding_window) in conf buffer ************************************************************************/ @@ -1971,6 +2024,8 @@ t_init_oscore_tests(void) { OSCORE_TEST(t_find_3); OSCORE_TEST(t_find_4); + OSCORE_TEST(t_replay_roll_back); + OSCORE_TEST(t_rcp_state_basic); OSCORE_TEST(t_rcp_state_max_64bit); OSCORE_TEST(t_rcp_state_only_last_seq);