Skip to content

Commit

Permalink
[border-agent] track successful connection with ephemeral key (openth…
Browse files Browse the repository at this point in the history
…read#11109)

This commit adds a new variable, `mDidConnectWithEphemeralKey`, which
tracks whether a successful secure session is established using the
ephemeral key. This variable is used in `HandleConnected()` to
determine whether to stop using the ephemeral key when the Border
Agent is notified that the secure session is disconnected.

This change ensures that ephemeral key use is not stopped after a
failed connection attempt, while still guaranteeing that an ephemeral
key can only be used once.

Without this fix, a failed connection attempt would immediately stop
the use of the ephemeral key. With this change, the intended
`kMaxEphemeralKeyConnectionAttempts` will be applied.
  • Loading branch information
abtink authored Jan 6, 2025
1 parent 54f6c27 commit e4fb743
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/core/meshcop/border_agent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ BorderAgent::BorderAgent(Instance &aInstance)
#endif
#if OPENTHREAD_CONFIG_BORDER_AGENT_EPHEMERAL_KEY_ENABLE
, mUsingEphemeralKey(false)
, mDidConnectWithEphemeralKey(false)
, mOldUdpPort(0)
, mEphemeralKeyTimer(aInstance)
, mEphemeralKeyTask(aInstance)
Expand Down Expand Up @@ -246,6 +247,7 @@ void BorderAgent::HandleConnected(Dtls::Session::ConnectEvent aEvent)
#if OPENTHREAD_CONFIG_BORDER_AGENT_EPHEMERAL_KEY_ENABLE
if (mUsingEphemeralKey)
{
mDidConnectWithEphemeralKey = true;
mCounters.mEpskcSecureSessionSuccesses++;
mEphemeralKeyTask.Post();
}
Expand All @@ -264,7 +266,10 @@ void BorderAgent::HandleConnected(Dtls::Session::ConnectEvent aEvent)
#if OPENTHREAD_CONFIG_BORDER_AGENT_EPHEMERAL_KEY_ENABLE
if (mUsingEphemeralKey)
{
RestartAfterRemovingEphemeralKey();
if (mDidConnectWithEphemeralKey)
{
RestartAfterRemovingEphemeralKey();
}

if (aEvent == Dtls::Session::kDisconnectedError)
{
Expand Down Expand Up @@ -735,7 +740,8 @@ Error BorderAgent::SetEphemeralKey(const char *aKeyString, uint32_t aTimeout, ui
// callbacks (like `HandleConnected()`) may be invoked from
// `Start()` itself.

mUsingEphemeralKey = true;
mUsingEphemeralKey = true;
mDidConnectWithEphemeralKey = false;

error = Start(aUdpPort, reinterpret_cast<const uint8_t *>(aKeyString), static_cast<uint8_t>(length));

Expand Down
3 changes: 2 additions & 1 deletion src/core/meshcop/border_agent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,8 @@ class BorderAgent : public InstanceLocator, private NonCopyable
bool mIdInitialized;
#endif
#if OPENTHREAD_CONFIG_BORDER_AGENT_EPHEMERAL_KEY_ENABLE
bool mUsingEphemeralKey;
bool mUsingEphemeralKey : 1;
bool mDidConnectWithEphemeralKey : 1;
uint16_t mOldUdpPort;
EphemeralKeyTimer mEphemeralKeyTimer;
EphemeralKeyTask mEphemeralKeyTask;
Expand Down

0 comments on commit e4fb743

Please sign in to comment.