From 656f2906602a20b541d11e7d1fffc34fa7950c8d Mon Sep 17 00:00:00 2001 From: Evgeny Date: Fri, 13 Jun 2025 21:27:46 +0100 Subject: [PATCH 1/5] agent: fix updating short invitation link data (#1566) --- src/Simplex/Messaging/Agent.hs | 4 ++-- src/Simplex/Messaging/Crypto/ShortLink.hs | 6 +++--- tests/AgentTests/ShortLinkTests.hs | 6 +++--- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Simplex/Messaging/Agent.hs b/src/Simplex/Messaging/Agent.hs index 53e24c3a8..d178134c1 100644 --- a/src/Simplex/Messaging/Agent.hs +++ b/src/Simplex/Messaging/Agent.hs @@ -859,7 +859,7 @@ setConnShortLink' c connId cMode userData clientData = Just ShortLinkCreds {shortLinkId, shortLinkKey, linkPrivSigKey, linkEncFixedData} -> do let (linkId, k) = SL.contactShortLinkKdf shortLinkKey unless (shortLinkId == linkId) $ throwE $ INTERNAL "setConnShortLink: link ID is not derived from link" - d <- liftError id $ SL.encryptUserData g k $ SL.encodeSignUserData linkPrivSigKey smpAgentVRange userData + d <- liftError id $ SL.encryptUserData g k $ SL.encodeSignUserData SCMContact linkPrivSigKey smpAgentVRange userData pure (rq, linkId, cslContact shortLinkKey, (linkEncFixedData, d)) Nothing -> do sigKeys@(_, privSigKey) <- atomically $ C.generateKeyPair @'C.Ed25519 g @@ -877,7 +877,7 @@ setConnShortLink' c connId cMode userData clientData = g <- asks random AgentConfig {smpAgentVRange} <- asks config let k = SL.invShortLinkKdf shortLinkKey - d <- liftError id $ SL.encryptUserData g k $ SL.encodeSignUserData linkPrivSigKey smpAgentVRange userData + d <- liftError id $ SL.encryptUserData g k $ SL.encodeSignUserData SCMInvitation linkPrivSigKey smpAgentVRange userData let sl = CSLInvitation SLSServer (qServer rq) shortLinkId shortLinkKey pure (rq, shortLinkId, sl, (linkEncFixedData, d)) Nothing -> throwE $ CMD PROHIBITED "setConnShortLink: no ShortLinkCreds in invitation" diff --git a/src/Simplex/Messaging/Crypto/ShortLink.hs b/src/Simplex/Messaging/Crypto/ShortLink.hs index 42815f9ac..c32c05bcf 100644 --- a/src/Simplex/Messaging/Crypto/ShortLink.hs +++ b/src/Simplex/Messaging/Crypto/ShortLink.hs @@ -54,9 +54,9 @@ encodeSignLinkData (rootKey, pk) agentVRange connReq userData = md = smpEncode $ connLinkData @c agentVRange userData in (LinkKey (C.sha3_256 fd), (encodeSign pk fd, encodeSign pk md)) -encodeSignUserData :: C.PrivateKeyEd25519 -> VersionRangeSMPA -> ConnInfo -> ByteString -encodeSignUserData pk agentVRange userData = - encodeSign pk $ smpEncode $ connLinkData @'CMContact agentVRange userData +encodeSignUserData :: forall c. ConnectionModeI c => SConnectionMode c -> C.PrivateKeyEd25519 -> VersionRangeSMPA -> ConnInfo -> ByteString +encodeSignUserData _ pk agentVRange userData = + encodeSign pk $ smpEncode $ connLinkData @c agentVRange userData connLinkData :: forall c. ConnectionModeI c => VersionRangeSMPA -> ConnInfo -> ConnLinkData c connLinkData agentVRange userData = case sConnectionMode @c of diff --git a/tests/AgentTests/ShortLinkTests.hs b/tests/AgentTests/ShortLinkTests.hs index d3aeca0cc..216b7c658 100644 --- a/tests/AgentTests/ShortLinkTests.hs +++ b/tests/AgentTests/ShortLinkTests.hs @@ -8,7 +8,7 @@ import AgentTests.ConnectionRequestTests (contactConnRequest, invConnRequest) import AgentTests.EqInstances () import Control.Concurrent.STM import Control.Monad.Except -import Simplex.Messaging.Agent.Protocol (AgentErrorType (..), ConnectionMode (..), LinkKey (..), SMPAgentError (..), linkUserData, supportedSMPAgentVRange) +import Simplex.Messaging.Agent.Protocol (AgentErrorType (..), ConnectionMode (..), LinkKey (..), SConnectionMode (..), SMPAgentError (..), linkUserData, supportedSMPAgentVRange) import qualified Simplex.Messaging.Crypto as C import qualified Simplex.Messaging.Crypto.ShortLink as SL import Test.Hspec hiding (fit, it) @@ -79,7 +79,7 @@ testUpdateContactShortLink = do Right (fd, _ud) <- runExceptT $ SL.encryptLinkData g k linkData -- encrypt updated user data let updatedUserData = "updated user data" - signed = SL.encodeSignUserData (snd sigKeys) supportedSMPAgentVRange updatedUserData + signed = SL.encodeSignUserData SCMContact (snd sigKeys) supportedSMPAgentVRange updatedUserData Right ud' <- runExceptT $ SL.encryptUserData g k signed -- decrypt Right (connReq, connData') <- pure $ SL.decryptLinkData linkKey k (fd, ud') @@ -114,7 +114,7 @@ testContactShortLinkBadSignature = do let updatedUserData = "updated user data" -- another signature key (_, pk) <- atomically $ C.generateKeyPair @'C.Ed25519 g - let signed = SL.encodeSignUserData pk supportedSMPAgentVRange updatedUserData + let signed = SL.encodeSignUserData SCMContact pk supportedSMPAgentVRange updatedUserData Right ud' <- runExceptT $ SL.encryptUserData g k signed -- decryption fails SL.decryptLinkData @'CMContact linkKey k (fd, ud') From 65cc19842c418a0c9bba16abcc6b4cb77023e8ff Mon Sep 17 00:00:00 2001 From: Evgeny Date: Sat, 14 Jun 2025 07:10:51 +0100 Subject: [PATCH 2/5] servers: restore constant time for absent queue/wrong key (regression in 6.4-beta.9) (#1567) --- src/Simplex/Messaging/Server.hs | 4 ++-- tests/ServerTests.hs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Simplex/Messaging/Server.hs b/src/Simplex/Messaging/Server.hs index 1efaa0650..b17054025 100644 --- a/src/Simplex/Messaging/Server.hs +++ b/src/Simplex/Messaging/Server.hs @@ -1272,8 +1272,8 @@ verifyCmdAuth thAuth k authenticator authorized (CorrId corrId) = case thAuth of Just THAuthServer {serverPrivKey = pk} -> C.cbVerify k pk (C.cbNonce corrId) authenticator authorized Nothing -> False -dummyVerifyCmd :: Maybe (THandleAuth 'TServer) -> Maybe TAuthorizations -> ByteString -> CorrId -> Maybe Bool -dummyVerifyCmd thAuth tAuth authorized corrId = verify <$> tAuth +dummyVerifyCmd :: Maybe (THandleAuth 'TServer) -> Maybe TAuthorizations -> ByteString -> CorrId -> Bool +dummyVerifyCmd thAuth tAuth authorized corrId = maybe False verify tAuth where verify = \case (TASignature (C.ASignature a s), _) -> C.verify' (dummySignKey a) s authorized diff --git a/tests/ServerTests.hs b/tests/ServerTests.hs index e7f2c35c6..354573852 100644 --- a/tests/ServerTests.hs +++ b/tests/ServerTests.hs @@ -939,7 +939,7 @@ testTiming = ] timeRepeat n = fmap fst . timeItT . forM_ (replicate n ()) . const similarTime t1 t2 - | t1 <= t2 = abs (1 - t1 / t2) < 0.35 -- normally the difference between "no queue" and "wrong key" is less than 5% + | t1 <= t2 = abs (1 - t1 / t2) < 0.3 -- normally the difference between "no queue" and "wrong key" is less than 5% | otherwise = similarTime t2 t1 testSameTiming :: forall c. Transport c => THandleSMP c 'TClient -> THandleSMP c 'TClient -> (C.AuthAlg, C.AuthAlg, Int) -> Expectation testSameTiming rh sh (C.AuthAlg goodKeyAlg, C.AuthAlg badKeyAlg, n) = do From 6ac7101f4f9ad477d967537a647b06d3b6dff547 Mon Sep 17 00:00:00 2001 From: Evgeny Date: Sat, 14 Jun 2025 14:13:14 +0100 Subject: [PATCH 3/5] agent: make decoding for short link data forward compatible (#1568) --- src/Simplex/Messaging/Agent/Protocol.hs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Simplex/Messaging/Agent/Protocol.hs b/src/Simplex/Messaging/Agent/Protocol.hs index 463639942..19997b6af 100644 --- a/src/Simplex/Messaging/Agent/Protocol.hs +++ b/src/Simplex/Messaging/Agent/Protocol.hs @@ -1684,13 +1684,13 @@ instance Encoding AConnLinkData where smpP = smpP >>= \case CMInvitation -> do - (vr, userData) <- smpP + (vr, userData) <- smpP <* A.takeByteString -- ignoring tail for forward compatibility with the future link data encoding pure $ ACLD SCMInvitation $ InvitationLinkData vr userData CMContact -> do (agentVRange, direct) <- smpP owners <- smpListP relays <- smpListP - userData <- smpP + userData <- smpP <* A.takeByteString -- ignoring tail for forward compatibility with the future link data encoding pure $ ACLD SCMContact ContactLinkData {agentVRange, direct, owners, relays, userData} -- | SMP queue status. From 3d62a383d5dcae6529d6d866233857182bcb4d47 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin Date: Sat, 14 Jun 2025 14:19:31 +0100 Subject: [PATCH 4/5] 6.4.0.3.1 --- simplexmq.cabal | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/simplexmq.cabal b/simplexmq.cabal index e1f2cdafd..e72801ad9 100644 --- a/simplexmq.cabal +++ b/simplexmq.cabal @@ -1,7 +1,7 @@ cabal-version: 1.12 name: simplexmq -version: 6.4.0.3 +version: 6.4.0.3.1 synopsis: SimpleXMQ message broker description: This package includes <./docs/Simplex-Messaging-Server.html server>, <./docs/Simplex-Messaging-Client.html client> and From d9500125300f89bd29377705e7a0e43d2d111707 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin Date: Sat, 14 Jun 2025 21:06:54 +0100 Subject: [PATCH 5/5] 6.4.0.10 --- simplexmq.cabal | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/simplexmq.cabal b/simplexmq.cabal index fa50abd88..efc1da25a 100644 --- a/simplexmq.cabal +++ b/simplexmq.cabal @@ -1,7 +1,7 @@ cabal-version: 1.12 name: simplexmq -version: 6.4.0.9 +version: 6.4.0.10 synopsis: SimpleXMQ message broker description: This package includes <./docs/Simplex-Messaging-Server.html server>, <./docs/Simplex-Messaging-Client.html client> and