From 11c067501779a160f3abfa298e4e166024582af2 Mon Sep 17 00:00:00 2001 From: "Evgeny @ SimpleX Chat" <259188159+evgeny-simplex@users.noreply.github.com> Date: Fri, 2 Oct 2026 21:57:46 +0000 Subject: [PATCH] simplify --- protocol/simplex-messaging.md | 5 ++--- src/Simplex/Messaging/Client.hs | 10 +++++----- src/Simplex/Messaging/Protocol.hs | 31 ++++++++++++++----------------- src/Simplex/Messaging/Server.hs | 8 ++++---- 4 files changed, 25 insertions(+), 29 deletions(-) diff --git a/protocol/simplex-messaging.md b/protocol/simplex-messaging.md index ab7550a91..684874ee5 100644 --- a/protocol/simplex-messaging.md +++ b/protocol/simplex-messaging.md @@ -1172,10 +1172,9 @@ The shared secret for encrypting transmission bodies between proxy router and de ```abnf -relayResponse = %s"RRES" SP -responseTransmission = fwdCorrId [fwdResponseNonce] forwardedResponse +relayResponse = %s"RRES" SP [responseNonce] +responseTransmission = fwdCorrId forwardedResponse ; fwdCorrId and forwardedResponse defined above in RFWD section -fwdResponseNonce = 24*24 OCTET ; when fwdSmpVersion is 23 or higher ``` ### Short link commands diff --git a/src/Simplex/Messaging/Client.hs b/src/Simplex/Messaging/Client.hs index e89e1f8c2..7a2eedeb9 100644 --- a/src/Simplex/Messaging/Client.hs +++ b/src/Simplex/Messaging/Client.hs @@ -162,7 +162,7 @@ import Numeric.Natural import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Encoding import Simplex.Messaging.Encoding.String -import Simplex.Messaging.Parsers (defaultJSON, dropPrefix, enumJSON, parseAll, sumTypeJSON) +import Simplex.Messaging.Parsers (defaultJSON, dropPrefix, enumJSON, sumTypeJSON) import Simplex.Messaging.Protocol import Simplex.Messaging.Protocol.Types import Simplex.Messaging.Server.QueueStore.QueueInfo @@ -1259,7 +1259,7 @@ proxySMPCommand c@ProtocolClient {thParams = proxyThParams, client_ = PClient {c -- this method is used in the proxy -- sends RFWD :: EncFwdTransmission -> Command Sender --- receives RRES :: EncFwdResponse -> BrokerMsg +-- receives RRES :: Maybe C.CbNonce -> EncFwdResponse -> BrokerMsg -- proxy should send PRES to the client with EncResponse -- Always uses background timeout mode forwardSMPTransmission :: SMPClient -> CorrId -> VersionSMP -> C.PublicKeyX25519 -> EncTransmission -> ExceptT SMPClientError IO (Maybe C.CbNonce, EncResponse) @@ -1274,11 +1274,11 @@ forwardSMPTransmission c@ProtocolClient {thParams, client_ = PClient {clientCorr eft = EncFwdTransmission $ C.cbEncryptNoPad sessSecret nonce (smpEncode fwdT) -- send sendProtocolCommand_ c NRMBackground (Just nonce) Nothing Nothing NoEntity (Cmd SProxyService (RFWD eft)) >>= \case - RRES (EncFwdResponse efr) -> do + RRES nonce_ (EncFwdResponse efr) -> do -- unwrap r' <- liftEitherWith PCECryptoError $ C.cbDecryptNoPad sessSecret (C.reverseNonce nonce) efr - FwdResponse {fwdCorrId = _, fwdNonce, fwdResponse} <- liftEitherWith (const $ PCEResponseError BLOCK) $ parseAll (fwdResponseP fwdVersion) r' - pure (fwdNonce, fwdResponse) + FwdResponse {fwdCorrId = _, fwdResponse} <- liftEitherWith (const $ PCEResponseError BLOCK) $ smpDecode r' + pure (nonce_, fwdResponse) r -> throwE $ unexpectedResponse r -- get queue information - always sent interactively diff --git a/src/Simplex/Messaging/Protocol.hs b/src/Simplex/Messaging/Protocol.hs index 412c34584..247f348b8 100644 --- a/src/Simplex/Messaging/Protocol.hs +++ b/src/Simplex/Messaging/Protocol.hs @@ -170,8 +170,6 @@ module Simplex.Messaging.Protocol EncTransmission (..), encTransmissionNonce, FwdResponse (..), - encodeFwdResponse, - fwdResponseP, FwdTransmission (..), NameRecord (..), MsgFlags (..), @@ -745,7 +743,7 @@ data BrokerMsg where NMSG :: C.CbNonce -> EncNMsgMeta -> BrokerMsg -- Should include certificate chain PKEY :: SessionId -> VersionRangeSMP -> CertChainPubKey -> BrokerMsg -- TLS-signed server key for proxy shared secret and initial sender key - RRES :: EncFwdResponse -> BrokerMsg -- relay to proxy + RRES :: Maybe C.CbNonce -> EncFwdResponse -> BrokerMsg -- relay to proxy PRES :: Maybe C.CbNonce -> EncResponse -> BrokerMsg -- proxy to client END :: BrokerMsg ENDS :: Int64 -> IdsHash -> BrokerMsg @@ -769,20 +767,15 @@ newtype EncFwdResponse = EncFwdResponse ByteString data FwdResponse = FwdResponse { fwdCorrId :: CorrId, - fwdNonce :: Maybe C.CbNonce, fwdResponse :: EncResponse } -encodeFwdResponse :: FwdResponse -> ByteString -encodeFwdResponse FwdResponse {fwdCorrId = CorrId corrId, fwdNonce, fwdResponse = EncResponse t} = - smpEncode corrId <> maybe "" smpEncode fwdNonce <> smpEncode (Tail t) - -fwdResponseP :: VersionSMP -> Parser FwdResponse -fwdResponseP v = do - corrId <- smpP - fwdNonce <- if v >= fwdNoncesSMPVersion then Just <$> smpP else pure Nothing - Tail t <- smpP - pure FwdResponse {fwdCorrId = CorrId corrId, fwdNonce, fwdResponse = EncResponse t} +instance Encoding FwdResponse where + smpEncode FwdResponse {fwdCorrId = CorrId corrId, fwdResponse = EncResponse t} = + smpEncode (corrId, Tail t) + smpP = do + (corrId, Tail t) <- smpP + pure FwdResponse {fwdCorrId = CorrId corrId, fwdResponse = EncResponse t} newtype EncResponse = EncResponse ByteString deriving (Eq, Show) @@ -1998,7 +1991,9 @@ instance ProtocolEncoding SMPVersion ErrorType BrokerMsg where NID nId srvNtfDh -> e (NID_, ' ', nId, srvNtfDh) NMSG nmsgNonce encNMsgMeta -> e (NMSG_, ' ', nmsgNonce, encNMsgMeta) PKEY sid vr certKey -> e (PKEY_, ' ', sid, vr, certKey) - RRES (EncFwdResponse encBlock) -> e (RRES_, ' ', Tail encBlock) + RRES nonce_ (EncFwdResponse encBlock) + | v >= fwdNoncesSMPVersion -> e (RRES_, ' ', nonce_, Tail encBlock) + | otherwise -> e (RRES_, ' ', Tail encBlock) PRES nonce_ (EncResponse encBlock) | v >= fwdNoncesSMPVersion -> e (PRES_, ' ', nonce_, Tail encBlock) | otherwise -> e (PRES_, ' ', Tail encBlock) @@ -2056,7 +2051,9 @@ instance ProtocolEncoding SMPVersion ErrorType BrokerMsg where NID_ -> NID <$> _smpP <*> smpP NMSG_ -> NMSG <$> _smpP <*> smpP PKEY_ -> PKEY <$> _smpP <*> smpP <*> smpP - RRES_ -> RRES <$> (EncFwdResponse . unTail <$> _smpP) + RRES_ + | v >= fwdNoncesSMPVersion -> RRES <$> _smpP <*> (EncFwdResponse . unTail <$> smpP) + | otherwise -> RRES Nothing . EncFwdResponse . unTail <$> _smpP PRES_ | v >= fwdNoncesSMPVersion -> PRES <$> _smpP <*> (EncResponse . unTail <$> smpP) | otherwise -> PRES Nothing . EncResponse . unTail <$> _smpP @@ -2092,7 +2089,7 @@ instance ProtocolEncoding SMPVersion ErrorType BrokerMsg where -- PONG response must not have queue ID PONG -> noEntityMsg PKEY {} -> noEntityMsg - RRES _ -> noEntityMsg + RRES {} -> noEntityMsg ALLS -> noEntityMsg RNAME {} -> noEntityMsg -- other broker responses must have queue ID diff --git a/src/Simplex/Messaging/Server.hs b/src/Simplex/Messaging/Server.hs index 97dc58e60..7894a4eab 100644 --- a/src/Simplex/Messaging/Server.hs +++ b/src/Simplex/Messaging/Server.hs @@ -2144,13 +2144,13 @@ client TBError _ _ : _ -> throwE BLOCK TBTransmission b' _ : _ -> pure b' TBTransmissions b' _ _ : _ -> pure b' - fwdNonce <- + nonce_ <- if fwdVersion >= fwdNoncesSMPVersion then Just <$> (atomically . C.randomCbNonce =<< asks random) else pure Nothing - r2 <- liftEitherWith (const BLOCK) $ EncResponse <$> C.cbEncrypt clientSecret (fromMaybe (C.reverseNonce clientNonce) fwdNonce) r' paddedProxiedTLength - let fr = FwdResponse {fwdCorrId, fwdNonce, fwdResponse = r2} - pure $ RRES $ EncFwdResponse $ C.cbEncryptNoPad sessSecret (C.reverseNonce proxyNonce) (encodeFwdResponse fr) + r2 <- liftEitherWith (const BLOCK) $ EncResponse <$> C.cbEncrypt clientSecret (fromMaybe (C.reverseNonce clientNonce) nonce_) r' paddedProxiedTLength + let fr = FwdResponse {fwdCorrId, fwdResponse = r2} + pure $ RRES nonce_ $ EncFwdResponse $ C.cbEncryptNoPad sessSecret (C.reverseNonce proxyNonce) (smpEncode fr) -- the inner response, or Nothing if forked (RSLV). r_ <- lift (rejectOrVerify clntThAuth t') >>= \case -- rejectOrVerify filters allowed commands, no need to repeat it here.