Files
bitchat/bitchatTests/Noise
krish rathi 5a10ba6bf9 Trigger Noise session rekey on absolute age, not just idle time (#1675)
* Trigger Noise session rekey on absolute age, not just idle time

needsRenegotiation() had two triggers: message count nearing the 1
billion-message ceiling, and lastActivityTime idle for longer than
sessionTimeout (24h). Neither fires for a session with steady traffic:
message count realistically never approaches a billion, and
lastActivityTime resets on every encrypt/decrypt, so a continuously
active conversation never looks idle.

Meanwhile encrypt/decrypt both hard-fail with sessionExpired once
sessionStartTime crosses the same 24h sessionTimeout, unconditionally,
with no rekey ever having been scheduled to prevent it. Two peers who
keep a conversation open past 24h (a stationary mesh node, or two
phones left open overnight) hit this simultaneously: every message
throws sessionExpired on both sides, isEstablished() still reports true
so the UI keeps showing the session as secured, MessageRouter never
falls back off the transport because canDeliverSecurely never goes
false, and nothing re-handshakes because the one self-heal path
(BLENoisePacketHandler's decrypt-failure recovery) requires an inbound
ciphertext to fail to decrypt for a session-establishment reason, not
sessionExpired specifically -- so it's excluded. The conversation is
silently and permanently dead until an app restart or a BLE
disconnect/reconnect forces revalidation.

Adds a third needsRenegotiation() trigger on sessionStartTime age
directly, firing at 90% of sessionTimeout -- the rekey timer polls
every 60s (NoiseEncryptionService.rekeyCheckInterval), leaving over two
hours of margin to complete a full handshake before the hard cutoff.

No Xcode/Swift toolchain available here, so this is verified by
tracing, not compiling: manually walked the new
continuouslyActiveSessionStillNeedsRenegotiationBeforeExpiry test's
inputs through both the pre-fix and post-fix needsRenegotiation() logic
(confirmed the pre-fix code returns false for a session with fresh
lastActivityTime and old sessionStartTime, and the fix flips it to
true), checked every other call site of needsRenegotiation() and
getSessionsNeedingRekey() in the test suite to confirm none depend on
the old always-false behavior at this age, and confirmed via
git apply -R / git apply that the source diff is exactly the traced
change with no other drift.

* Fix stale comment in needsRenegotiation idle time check
2026-09-24 09:34:14 +02:00
..