agent: use double ratchet from the first message in contact addresses, request rejection, RPC (#1831)

* agent: initialize double ratchet from the invitation via contact address (#1829)

* rfc: agent support for PRC pattern without duplex connection

* update rfc, plan

* corrections

Co-authored-by: Evgeny <evgeny@poberezkin.com>

* split rfcs, add plan

* update rfc

* update rfc and plan

* update plan

* add double ratchet keys to address

* agent schema

* fix version in test

* change ContactRequest encoding

* fix tests

* update plan

* tests, refactor

* fix compilation

* refactor

* refactor more

* fix

* add export

* fix

* refactor

* refactor

* update schema

* rename

* refactor

* refactor 2

* comment, type

* type

* rename

* refactor more

* move type

* refactor

* remove comment

* diff

* refactor

* comment

* puns

* get correct key

* rename

* add autoincrement

* refactor

* move

* move back

* simplify

* rename

* refactor

* tuple

* comment

* split decryption

* refactor

* rename

* refactor

* move

* remove createSndQueue

* remove comment

* remove JoinInvitationReq

* simplify

* rename

* fix

* type synonim

* getConnData

* simplify

* move

* encoding

* version

* async join with ratchet keys

* fix encoding

* rotate address ratchet keys

* rename, parameter order

* refactor, include ratchet keys into ConnectionRequestUri

* simplify

* remove records

* refactor

* rename, encoding

* clean up

* refactor

* compatible

* addrKeysE2EVersion

* support IKUsePQ for contact addresses

* comments, refactor condition

* typos

---------

Co-authored-by: Evgeny @ SimpleX Chat <259188159+evgeny-simplex@users.noreply.github.com>

* agent: contact request rejection and service requests (#1833)

* agent: request rejection

* implement RPC requests/responses

* simplify rpc, test, async

* renames

* clean up

* fix

* fix query

* set flag at creation

* move

* command type, schema

* rename

* refactor

* typo

Co-authored-by: simplex-chat-agent[bot] <287173099+simplex-chat-agent[bot]@users.noreply.github.com>

* allow overriding the service request timeout per request

* fixes

* use SSENT event for service replies

* AgentServiceError encoding

* sign service requests

* add bad signature error

* update the plan

* clean up

* refactor

* empty line

Co-authored-by: simplex-chat-agent[bot] <287173099+simplex-chat-agent[bot]@users.noreply.github.com>

* add indices

---------

Co-authored-by: Evgeny @ SimpleX Chat <259188159+evgeny-simplex@users.noreply.github.com>
Co-authored-by: simplex-chat-agent[bot] <287173099+simplex-chat-agent[bot]@users.noreply.github.com>

* update schemas

* tryAllErrors

* combine queries

---------

Co-authored-by: Evgeny @ SimpleX Chat <259188159+evgeny-simplex@users.noreply.github.com>
Co-authored-by: simplex-chat-agent[bot] <287173099+simplex-chat-agent[bot]@users.noreply.github.com>
This commit is contained in:
Evgeny
2026-07-31 19:32:34 +01:00
committed by GitHub
co-authored by Evgeny @ SimpleX Chat <259188159+evgeny-simplex@users.noreply.github.com> simplex-chat-agent[bot] <287173099+simplex-chat-agent[bot]@users.noreply.github.com>
parent 066a93861b
commit e4e5ce75fa
31 changed files with 2382 additions and 390 deletions
File diff suppressed because it is too large Load Diff
+6 -10
View File
@@ -384,6 +384,7 @@ data AgentClient = AgentClient
clientId :: Int,
agentEnv :: Env,
proxySessTs :: TVar UTCTime,
serviceRequests :: TMap ConnId (TMVar (Either AgentErrorType SMP.MsgBody)),
smpServersStats :: TMap (UserId, SMPServer) AgentSMPServerStats,
xftpServersStats :: TMap (UserId, XFTPServer) AgentXFTPServerStats,
ntfServersStats :: TMap (UserId, NtfServer) AgentNtfServerStats,
@@ -547,6 +548,7 @@ newAgentClient clientId InitialAgentServers {smp, ntf, xftp, netCfg, useServices
invLocks <- TM.emptyIO
deleteLock <- createLockIO
smpSubWorkers <- TM.emptyIO
serviceRequests <- TM.emptyIO
smpServersStats <- TM.emptyIO
xftpServersStats <- TM.emptyIO
ntfServersStats <- TM.emptyIO
@@ -592,6 +594,7 @@ newAgentClient clientId InitialAgentServers {smp, ntf, xftp, netCfg, useServices
clientId,
agentEnv,
proxySessTs,
serviceRequests,
smpServersStats,
xftpServersStats,
ntfServersStats,
@@ -1911,17 +1914,10 @@ sendConfirmation c nm sq@SndQueue {userId, server, connId, sndId, queueMode, snd
sendOrProxySMPMessage c nm userId server connId "<CONF>" spKey sndId (MsgFlags {notification = True}) msg
sendConfirmation _ _ _ _ = throwE $ INTERNAL "sendConfirmation called without snd_queue public key(s) in the database"
sendInvitation :: AgentClient -> NetworkRequestMode -> UserId -> ConnId -> Compatible SMPQueueInfo -> Compatible VersionSMPA -> ConnectionRequestUri 'CMInvitation -> ConnInfo -> AM (Maybe SMPServer)
sendInvitation c nm userId connId (Compatible (SMPQueueInfo v SMPQueueAddress {smpServer, senderId, dhPublicKey})) (Compatible agentVersion) connReq connInfo = do
msg <- mkInvitation
sendInvitation :: AgentClient -> NetworkRequestMode -> UserId -> ConnId -> Compatible SMPQueueInfo -> AgentMsgEnvelope -> AM (Maybe SMPServer)
sendInvitation c nm userId connId (Compatible (SMPQueueInfo v SMPQueueAddress {smpServer, senderId, dhPublicKey})) agentEnvelope = do
msg <- agentCbEncryptOnce v dhPublicKey . smpEncode $ SMP.ClientMessage SMP.PHEmpty (smpEncode agentEnvelope)
sendOrProxySMPMessage c nm userId smpServer connId "<INV>" Nothing senderId (MsgFlags {notification = True}) msg
where
mkInvitation :: AM ByteString
-- this is only encrypted with per-queue E2E, not with double ratchet
mkInvitation = do
let agentEnvelope = AgentInvitation {agentVersion, connReq, connInfo}
agentCbEncryptOnce v dhPublicKey . smpEncode $
SMP.ClientMessage SMP.PHEmpty (smpEncode agentEnvelope)
getQueueMessage :: AgentClient -> RcvQueue -> AM (Maybe SMPMsgMeta)
getQueueMessage c rq@RcvQueue {server, rcvId, rcvPrivateKey} = do
@@ -153,6 +153,8 @@ data AgentConfig = AgentConfig
userNetworkInterval :: Int,
userOfflineDelay :: NominalDiffTime,
messageTimeout :: NominalDiffTime,
serviceRequestTimeout :: NominalDiffTime, -- client side: default time the client waits for a service response (overridable per request)
serviceResponseTimeout :: NominalDiffTime, -- service side: time a received service request is valid to respond to
connDeleteDeliveryTimeout :: NominalDiffTime,
helloTimeout :: NominalDiffTime,
quotaExceededTimeout :: NominalDiffTime,
@@ -170,6 +172,7 @@ data AgentConfig = AgentConfig
xftpConsecutiveRetries :: Int,
xftpMaxRecipientsPerRequest :: Int,
deleteErrorCount :: Int,
keepAddressKeys :: Int,
ntfCron :: Word16,
ntfBatchSize :: Int,
ntfSubFirstCheckInterval :: NominalDiffTime,
@@ -228,6 +231,8 @@ defaultAgentConfig =
userNetworkInterval = 1800_000000, -- 30 minutes, should be less than Int32 max value
userOfflineDelay = 2, -- if network offline event happens in less than 2 seconds after it was set online, it is ignored
messageTimeout = 2 * nominalDay,
serviceRequestTimeout = 30,
serviceResponseTimeout = 180,
connDeleteDeliveryTimeout = 2 * nominalDay,
helloTimeout = 2 * nominalDay,
quotaExceededTimeout = 7 * nominalDay,
@@ -245,6 +250,7 @@ defaultAgentConfig =
xftpConsecutiveRetries = 3,
xftpMaxRecipientsPerRequest = 200,
deleteErrorCount = 10,
keepAddressKeys = 3,
ntfCron = 20, -- minutes
ntfBatchSize = 150,
ntfSubFirstCheckInterval = nominalDay,
+184 -41
View File
@@ -1,6 +1,7 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
@@ -54,8 +55,10 @@ module Simplex.Messaging.Agent.Protocol
-- * SMP agent protocol types
ConnInfo,
SndQueueSecured,
UseRatchetKeys,
AEntityId,
ACommand (..),
JoinRequest (..),
AEvent (..),
AEvt (..),
ACommandTag (..),
@@ -80,6 +83,7 @@ module Simplex.Messaging.Agent.Protocol
SMPConfirmation (..),
AgentMsgEnvelope (..),
AgentMessage (..),
RequestSignature (..),
AgentMessageType (..),
APrivHeader (..),
AMessage (..),
@@ -107,6 +111,9 @@ module Simplex.Messaging.Agent.Protocol
ConnectionModeI (..),
ConnectionRequestUri (..),
AConnectionRequestUri (..),
BinaryConnectionRequestUri (..),
ABinaryConnectionRequestUri (..),
binaryConnReq,
ShortLinkCreds (..),
ConnReqUriData (..),
CRClientData,
@@ -118,6 +125,10 @@ module Simplex.Messaging.Agent.Protocol
UserConnLinkData (..),
UserContactData (..),
UserLinkData (..),
AddressRatchetKeys,
NewRatchetKeys,
DRInvitation (..),
RatchetKeyId (..),
OwnerAuth (..),
OwnerId,
ConnectionLink (..),
@@ -151,6 +162,7 @@ module Simplex.Messaging.Agent.Protocol
ConnectionErrorType (..),
BrokerErrorType (..),
SMPAgentError (..),
AgentServiceError (..),
DroppedMsg (..),
AgentCryptoError (..),
cryptoErrToSyncState,
@@ -200,6 +212,7 @@ import qualified Data.Attoparsec.ByteString.Char8 as A
import qualified Data.ByteString.Base64.URL as B64
import Data.ByteString.Char8 (ByteString)
import qualified Data.ByteString.Char8 as B
import qualified Data.ByteString.Lazy as LB
import Data.Char (toLower, toUpper)
import Data.Foldable (find)
import Data.Functor (($>))
@@ -230,9 +243,11 @@ import Simplex.Messaging.Crypto.Ratchet
( InitialKeys (..),
PQEncryption (..),
PQSupport,
RatchetX448,
RcvE2ERatchetParams,
RcvE2ERatchetParamsUri,
SndE2ERatchetParams,
RcvE2EPrivRatchetParams,
pattern PQSupportOff,
pattern PQSupportOn,
)
@@ -393,13 +408,18 @@ type ConnInfo = ByteString
type SndQueueSecured = Bool
type UseRatchetKeys = Bool
-- | Parameterized type for SMP agent events
data AEvent (e :: AEntity) where
INV :: AConnectionRequestUri -> AEvent AEConn
LINK :: ConnShortLink 'CMContact -> UserConnLinkData 'CMContact -> AEvent AEConn
LDATA :: FixedLinkData 'CMContact -> ConnLinkData 'CMContact -> AEvent AEConn
LDATA :: FixedLinkData 'CMContact -> ConnLinkData 'CMContact -> ConnectionRequestUri 'CMContact -> AEvent AEConn
CONF :: ConfirmationId -> PQSupport -> [SMPServer] -> ConnInfo -> AEvent AEConn -- ConnInfo is from sender, [SMPServer] will be empty only in v1 handshake
REQ :: InvitationId -> PQSupport -> NonEmpty SMPServer -> ConnInfo -> AEvent AEConn -- ConnInfo is from sender
REQ :: InvitationId -> PQSupport -> NonEmpty SMPServer -> ConnInfo -> Bool -> AEvent AEConn -- ConnInfo is from sender; Bool - rejection reason can be sent
SREQ :: InvitationId -> Maybe C.PublicKeyEd25519 -> MsgBody -> AEvent AEConn
SSENT :: AgentMsgId -> Maybe SMPServer -> AEvent AEConn
RJCT :: ConnInfo -> AEvent AEConn
INFO :: PQSupport -> ConnInfo -> AEvent AEConn
CON :: PQEncryption -> AEvent AEConn -- notification that connection is established
END :: AEvent AEConn
@@ -454,15 +474,15 @@ instance Eq AEvtTag where
deriving instance Show AEvtTag
data ACommand
= NEW Bool AConnectionMode InitialKeys SubscriptionMode -- response INV
= NEW Bool AConnectionMode InitialKeys SubscriptionMode UseRatchetKeys -- response INV
| LSET (UserConnLinkData 'CMContact) (Maybe CRClientData) -- response LINK
| LGET (ConnShortLink 'CMContact) -- response LDATA
| JOIN Bool AConnectionRequestUri PQSupport SubscriptionMode ConnInfo
| JOIN JoinRequest SubscriptionMode ConnInfo
| LET ConfirmationId ConnInfo -- ConnInfo is from client
| ACK AgentMsgId (Maybe MsgReceiptInfo)
| SWCH
| DEL
deriving (Eq, Show)
deriving (Show)
data ACommandTag
= NEW_
@@ -481,6 +501,9 @@ data AEventTag (e :: AEntity) where
LDATA_ :: AEventTag AEConn
CONF_ :: AEventTag AEConn
REQ_ :: AEventTag AEConn
SREQ_ :: AEventTag AEConn
SSENT_ :: AEventTag AEConn
RJCT_ :: AEventTag AEConn
INFO_ :: AEventTag AEConn
CON_ :: AEventTag AEConn
END_ :: AEventTag AEConn
@@ -544,6 +567,9 @@ aEventTag = \case
LDATA {} -> LDATA_
CONF {} -> CONF_
REQ {} -> REQ_
SREQ {} -> SREQ_
SSENT {} -> SSENT_
RJCT {} -> RJCT_
INFO {} -> INFO_
CON _ -> CON_
END -> END_
@@ -843,6 +869,12 @@ data AgentMsgEnvelope
connReq :: ConnectionRequestUri 'CMInvitation,
connInfo :: ByteString -- this message is only encrypted with per-queue E2E, not with double ratchet,
}
| AgentContactRequest -- DR request to a contact address that published DR keys: a contact invitation or a service (RPC) request
{ agentVersion :: VersionSMPA,
e2eSndParams :: SndE2ERatchetParams 'C.X448,
ratchetKeyId :: RatchetKeyId,
encConnInfo :: ByteString
}
| AgentRatchetKey
{ agentVersion :: VersionSMPA,
e2eEncryption :: RcvE2ERatchetParams 'C.X448,
@@ -858,6 +890,8 @@ instance Encoding AgentMsgEnvelope where
smpEncode (agentVersion, 'M', Tail encAgentMessage)
AgentInvitation {agentVersion, connReq, connInfo} ->
smpEncode (agentVersion, 'I', Large $ strEncode connReq, Tail connInfo)
AgentContactRequest {agentVersion, e2eSndParams, ratchetKeyId, encConnInfo} ->
smpEncode (agentVersion, 'A', e2eSndParams, ratchetKeyId, Tail encConnInfo)
AgentRatchetKey {agentVersion, e2eEncryption, info} ->
smpEncode (agentVersion, 'R', e2eEncryption, Tail info)
smpP = do
@@ -873,12 +907,22 @@ instance Encoding AgentMsgEnvelope where
connReq <- strDecode . unLarge <$?> smpP
Tail connInfo <- smpP
pure AgentInvitation {agentVersion, connReq, connInfo}
'A' -> do
(e2eSndParams, ratchetKeyId, Tail encConnInfo) <- smpP
pure AgentContactRequest {agentVersion, e2eSndParams, ratchetKeyId, encConnInfo}
'R' -> do
e2eEncryption <- smpP
Tail info <- smpP
pure AgentRatchetKey {agentVersion, e2eEncryption, info}
_ -> fail "bad AgentMsgEnvelope"
data RequestSignature = RequestSignature C.PublicKeyEd25519 (C.Signature 'C.Ed25519)
deriving (Eq, Show)
instance Encoding RequestSignature where
smpEncode (RequestSignature k sig) = smpEncode (k, sig)
smpP = RequestSignature <$> smpP <*> smpP
-- SMP agent message formats (after double ratchet decryption,
-- or in case of AgentInvitation - in plain text body)
-- AgentRatchetInfo is not encrypted with double ratchet, but with per-queue E2E encryption
@@ -890,6 +934,9 @@ data AgentMessage
AgentConnInfoReply (NonEmpty SMPQueueInfo) ConnInfo
| AgentRatchetInfo ByteString
| AgentMessage APrivHeader AMessage
| AgentServiceRequest (NonEmpty SMPQueueInfo) (Maybe RequestSignature) MsgBody
| AgentServiceResponse MsgBody
| AgentRejection ByteString
deriving (Show)
instance Encoding AgentMessage where
@@ -898,12 +945,18 @@ instance Encoding AgentMessage where
AgentConnInfoReply smpQueues cInfo -> smpEncode ('D', smpQueues, Tail cInfo) -- 'D' stands for "duplex"
AgentRatchetInfo info -> smpEncode ('R', Tail info)
AgentMessage hdr aMsg -> smpEncode ('M', hdr, aMsg)
AgentServiceRequest qs sig_ body -> smpEncode ('A', qs, sig_, Tail body)
AgentServiceResponse body -> smpEncode ('P', Tail body)
AgentRejection reason -> smpEncode ('J', Tail reason)
smpP =
smpP >>= \case
'I' -> AgentConnInfo . unTail <$> smpP
'D' -> AgentConnInfoReply <$> smpP <*> (unTail <$> smpP)
'R' -> AgentRatchetInfo . unTail <$> smpP
'M' -> AgentMessage <$> smpP <*> smpP
'A' -> AgentServiceRequest <$> smpP <*> smpP <*> (unTail <$> smpP)
'P' -> AgentServiceResponse . unTail <$> smpP
'J' -> AgentRejection . unTail <$> smpP
_ -> fail "bad AgentMessage"
-- internal type for storing message type in the database
@@ -920,6 +973,9 @@ data AgentMessageType
| AM_QUSE_
| AM_QTEST_
| AM_EREADY_
| AM_SRV_REQ
| AM_SRV_RESP
| AM_RJCT
deriving (Eq, Show)
instance Encoding AgentMessageType where
@@ -936,6 +992,9 @@ instance Encoding AgentMessageType where
AM_QUSE_ -> "QU"
AM_QTEST_ -> "QT"
AM_EREADY_ -> "E"
AM_SRV_REQ -> "A"
AM_SRV_RESP -> "P"
AM_RJCT -> "J"
smpP =
A.anyChar >>= \case
'C' -> pure AM_CONN_INFO
@@ -953,6 +1012,9 @@ instance Encoding AgentMessageType where
'T' -> pure AM_QTEST_
_ -> fail "bad AgentMessageType"
'E' -> pure AM_EREADY_
'A' -> pure AM_SRV_REQ
'P' -> pure AM_SRV_RESP
'J' -> pure AM_RJCT
_ -> fail "bad AgentMessageType"
agentMessageType :: AgentMessage -> AgentMessageType
@@ -961,6 +1023,9 @@ agentMessageType = \case
AgentConnInfoReply {} -> AM_CONN_INFO_REPLY
AgentRatchetInfo _ -> AM_RATCHET_INFO
AgentMessage _ aMsg -> aMessageType aMsg
AgentServiceRequest {} -> AM_SRV_REQ
AgentServiceResponse {} -> AM_SRV_RESP
AgentRejection {} -> AM_RJCT
data APrivHeader = APrivHeader
{ -- | sequential ID assigned by the sending agent
@@ -1131,11 +1196,13 @@ instance Encoding AMessageReceipt where
instance ConnectionModeI m => StrEncoding (ConnectionRequestUri m) where
strEncode = \case
CRInvitationUri crData e2eParams -> crEncode "invitation" crData (Just e2eParams)
CRContactUri crData -> crEncode "contact" crData Nothing
CRInvitationUri crData e2eParams -> crEncode "invitation" crData (Just e2eParams, Nothing)
CRContactUri crData rks -> crEncode "contact" crData $ case rks of
Just (ratchetKeyId, e2eRcvParams) -> (Just e2eRcvParams, Just ratchetKeyId)
Nothing -> (Nothing, Nothing)
where
crEncode :: ByteString -> ConnReqUriData -> Maybe (RcvE2ERatchetParamsUri 'C.X448) -> ByteString
crEncode crMode ConnReqUriData {crScheme, crAgentVRange, crSmpQueues, crClientData} e2eParams =
crEncode :: ByteString -> ConnReqUriData -> (Maybe (RcvE2ERatchetParamsUri 'C.X448), Maybe RatchetKeyId) -> ByteString
crEncode crMode ConnReqUriData {crScheme, crAgentVRange, crSmpQueues, crClientData} (e2eParams, rk) =
strEncode crScheme <> "/" <> crMode <> "#/?" <> queryStr
where
queryStr =
@@ -1143,23 +1210,24 @@ instance ConnectionModeI m => StrEncoding (ConnectionRequestUri m) where
-- semicolon is used to separate SMP queues because comma is used to separate server address hostnames
[("v", strEncode crAgentVRange), ("smp", B.intercalate ";" $ map strEncode $ L.toList crSmpQueues)]
<> maybe [] (\e2e -> [("e2e", strEncode e2e)]) e2eParams
<> maybe [] (\k -> [("rk", strEncode k)]) rk
<> maybe [] (\cd -> [("data", encodeUtf8 cd)]) crClientData
strP = connReqUriP' (Just SSSimplex)
instance ConnectionModeI m => Encoding (ConnectionRequestUri m) where
instance ConnectionModeI m => Encoding (BinaryConnectionRequestUri m) where
smpEncode = \case
CRInvitationUri crData e2eParams -> smpEncode (CMInvitation, crData, e2eParams)
CRContactUri crData -> smpEncode (CMContact, crData)
smpP = (\(ACR _ cr) -> checkConnMode cr) <$?> smpP
BCRInvitationUri crData e2eParams -> smpEncode (CMInvitation, crData, e2eParams)
BCRContactUri crData -> smpEncode (CMContact, crData)
smpP = (\(ABCR _ cr) -> checkConnMode cr) <$?> smpP
{-# INLINE smpP #-}
instance Encoding AConnectionRequestUri where
smpEncode (ACR _ cr) = smpEncode cr
instance Encoding ABinaryConnectionRequestUri where
smpEncode (ABCR _ cr) = smpEncode cr
{-# INLINE smpEncode #-}
smpP =
smpP >>= \case
CMInvitation -> ACR SCMInvitation <$> (CRInvitationUri <$> smpP <*> smpP)
CMContact -> ACR SCMContact . CRContactUri <$> smpP
CMInvitation -> ABCR SCMInvitation <$> (BCRInvitationUri <$> smpP <*> smpP)
CMContact -> ABCR SCMContact . BCRContactUri <$> smpP
instance Encoding ConnReqUriData where
smpEncode ConnReqUriData {crAgentVRange, crSmpQueues, crClientData} =
@@ -1202,7 +1270,10 @@ connReqUriP overrideScheme = do
pure . ACR SCMInvitation $ CRInvitationUri crData crE2eParams
-- contact links are adjusted to the minimum version supported by the agent
-- to preserve compatibility with the old links published online
CMContact -> pure . ACR SCMContact $ CRContactUri crData {crAgentVRange = adjustAgentVRange aVRange}
CMContact -> do
e2e_ <- queryParam_ "e2e" query
rk_ <- queryParam_ "rk" query
pure . ACR SCMContact $ CRContactUri crData {crAgentVRange = adjustAgentVRange aVRange} ((,) <$> rk_ <*> e2e_)
where
crModeP = "invitation" $> CMInvitation <|> "contact" $> CMContact
-- semicolon is used to separate SMP queues because comma is used to separate server address hostnames
@@ -1328,6 +1399,7 @@ sameQueue addr q = sameQAddress addr (qAddress q)
data SMPQueueInfo = SMPQueueInfo {clientVersion :: VersionSMPC, queueAddress :: SMPQueueAddress}
deriving (Eq, Show)
deriving (ToJSON, FromJSON) via (StrJSON "SMPQueueInfo" SMPQueueInfo)
instance Encoding SMPQueueInfo where
smpEncode (SMPQueueInfo clientVersion SMPQueueAddress {smpServer, senderId, dhPublicKey, queueMode})
@@ -1433,6 +1505,10 @@ instance StrEncoding SMPQueueUri where
_ -> Nothing
pure (vr, maybe [] thList_ hs_, dhKey, queueMode)
instance StrEncoding SMPQueueInfo where
strEncode (SMPQueueInfo v addr) = strEncode (SMPQueueUri (versionToRange v) addr)
strP = (\(SMPQueueUri vr addr) -> SMPQueueInfo (maxVersion vr) addr) <$> strP
instance Encoding SMPQueueUri where
smpEncode (SMPQueueUri clientVRange@(VersionRange minV maxV) SMPQueueAddress {smpServer, senderId, dhPublicKey, queueMode})
-- The condition is for minVersion as earlier clients won't be able to support it.
@@ -1452,16 +1528,30 @@ instance Encoding SMPQueueUri where
queueModeP :: Parser (Maybe QueueMode)
queueModeP = Just <$> smpP <|> optional ((\case True -> QMMessaging; _ -> QMContact) <$> smpP)
data BinaryConnectionRequestUri (m :: ConnectionMode) where
BCRInvitationUri :: ConnReqUriData -> RcvE2ERatchetParamsUri 'C.X448 -> BinaryConnectionRequestUri CMInvitation
BCRContactUri :: ConnReqUriData -> BinaryConnectionRequestUri CMContact
deriving instance Eq (BinaryConnectionRequestUri m)
deriving instance Show (BinaryConnectionRequestUri m)
data ABinaryConnectionRequestUri = forall m. ConnectionModeI m => ABCR (SConnectionMode m) (BinaryConnectionRequestUri m)
data ConnectionRequestUri (m :: ConnectionMode) where
CRInvitationUri :: ConnReqUriData -> RcvE2ERatchetParamsUri 'C.X448 -> ConnectionRequestUri CMInvitation
-- contact connection request does NOT contain E2E encryption parameters for double ratchet -
-- they are passed in AgentInvitation message
CRContactUri :: ConnReqUriData -> ConnectionRequestUri CMContact
-- optional contact address DR keys for double ratchet e2e from message 1
CRContactUri :: ConnReqUriData -> Maybe AddressRatchetKeys -> ConnectionRequestUri CMContact
simplexConnReqUri :: ConnectionRequestUri m -> ConnectionRequestUri m
simplexConnReqUri = \case
CRInvitationUri crData e2eParams -> CRInvitationUri crData {crScheme = SSSimplex} e2eParams
CRContactUri crData -> CRContactUri crData {crScheme = SSSimplex}
CRContactUri crData rk -> CRContactUri crData {crScheme = SSSimplex} rk
binaryConnReq :: ConnectionRequestUri m -> BinaryConnectionRequestUri m
binaryConnReq = \case
CRInvitationUri crData e2eParams -> BCRInvitationUri crData e2eParams
CRContactUri crData _ -> BCRContactUri crData
deriving instance Eq (ConnectionRequestUri m)
@@ -1519,9 +1609,12 @@ data PreparedLinkParams = PreparedLinkParams
-- | smpEncode of FixedLinkData (includes linkEntityId)
plpSignedFixedData :: ByteString,
-- | Server with basic auth (not stored in link)
plpSrvWithAuth :: SMPServerWithAuth
plpSrvWithAuth :: SMPServerWithAuth,
-- | Initial PQ keys
plpInitKeys :: InitialKeys,
-- | Contact address double ratchet keys
plpAddressKeys :: Maybe (RatchetKeyId, RcvE2EPrivRatchetParams 'C.X448)
}
deriving (Show)
instance ConnectionModeI c => ToField (ConnectionLink c) where toField = toField . Binary . strEncode
@@ -1733,7 +1826,7 @@ findPresetServer ProtocolServer {host = h :| _} = find (\ProtocolServer {host =
{-# INLINE findPresetServer #-}
sameConnReqContact :: ConnectionRequestUri 'CMContact -> ConnectionRequestUri 'CMContact -> Bool
sameConnReqContact (CRContactUri ConnReqUriData {crSmpQueues = qs}) (CRContactUri ConnReqUriData {crSmpQueues = qs'}) =
sameConnReqContact (CRContactUri ConnReqUriData {crSmpQueues = qs} _) (CRContactUri ConnReqUriData {crSmpQueues = qs'} _) =
L.length qs == L.length qs' && all same (L.zip qs qs')
where
same (q, q') = sameQAddress (qAddress q) (qAddress q')
@@ -1772,7 +1865,7 @@ type CRClientData = Text
data FixedLinkData c = FixedLinkData
{ agentVRange :: VersionRangeSMPA,
rootKey :: C.PublicKeyEd25519,
linkConnReq :: ConnectionRequestUri c,
linkConnReq :: BinaryConnectionRequestUri c,
linkEntityId :: Maybe ByteString
}
deriving (Eq, Show)
@@ -1785,6 +1878,31 @@ deriving instance Eq (ConnLinkData c)
deriving instance Show (ConnLinkData c)
newtype RatchetKeyId = RatchetKeyId ByteString
deriving (Eq, Show)
deriving newtype (Encoding, StrEncoding)
-- | double ratchet keys in contact address
type AddressRatchetKeys = (RatchetKeyId, RcvE2ERatchetParamsUri 'C.X448)
-- | Whether to rotate double ratchet keys in contact address
type NewRatchetKeys = Bool
-- | stored invitation with double ratchet keys
data DRInvitation = DRInvitation
{ ratchetState :: RatchetX448,
replyQueue :: SMPQueueInfo,
agentVersion :: VersionSMPA,
pqSupport :: PQSupport
}
deriving (Show)
data JoinRequest
= JRConnReq {enableNtfs :: Bool, joinConnReq :: AConnectionRequestUri, joinPQSupport :: PQSupport}
| JRServiceReq {contactReq :: ConnectionRequestUri 'CMContact, joinPQSupport :: PQSupport, requestKey :: Maybe (C.StoredPrivateKey C.Ed25519)}
| JRInvitationDR DRInvitation
deriving (Show)
data UserContactData = UserContactData
{ -- direct connection via connReq in fixed data is allowed.
direct :: Bool,
@@ -1792,7 +1910,8 @@ data UserContactData = UserContactData
owners :: [OwnerAuth],
-- alternative addresses of chat relays that receive requests for this contact address.
relays :: [ConnShortLink 'CMContact],
userData :: UserLinkData
userData :: UserLinkData,
ratchetKeys :: Maybe AddressRatchetKeys
}
deriving (Eq, Show)
@@ -1870,10 +1989,12 @@ validateLinkOwners rootKey = go []
instance ConnectionModeI c => Encoding (FixedLinkData c) where
smpEncode FixedLinkData {agentVRange, rootKey, linkConnReq, linkEntityId} =
-- TODO this encoding is not extensible, replace with smpEncode (fromMaybe "" linkEntityId) - safe to do it in 2027
smpEncode (agentVRange, rootKey, linkConnReq) <> maybe "" smpEncode linkEntityId
smpP = do
(agentVRange, rootKey, linkConnReq) <- smpP
linkEntityId <- optional smpP <* A.takeByteString -- ignoring tail for forward compatibility with the future link data encoding
linkEntityId <- ((\s -> if B.null s then Nothing else Just s) =<<) <$> optional smpP
_ <- A.takeByteString -- ignoring tail for forward compatibility with the future link data encoding (added in January 2026)
pure FixedLinkData {agentVRange, rootKey, linkConnReq, linkEntityId}
instance ConnectionModeI c => Encoding (ConnLinkData c) where
@@ -1920,14 +2041,13 @@ instance ConnectionModeI c => StrEncoding (UserConnLinkData c) where
{-# INLINE strP #-}
instance Encoding UserContactData where
smpEncode UserContactData {direct, owners, relays, userData} =
B.concat [smpEncode direct, smpEncodeList owners, smpEncodeList relays, smpEncode userData]
smpEncode UserContactData {direct, owners, relays, userData, ratchetKeys} =
smpEncode (direct, EncList owners, EncList relays, userData, ratchetKeys)
smpP = do
direct <- smpP
owners <- smpListP
relays <- smpListP
userData <- smpP <* A.takeByteString -- ignoring tail for forward compatibility with the future link data encoding
pure UserContactData {direct, owners, relays, userData}
(direct, EncList owners, EncList relays, userData) <- smpP
ratchetKeys <- smpP <|> pure Nothing
_ <- A.takeByteString -- ignoring tail for forward compatibility with the future link data encoding
pure UserContactData {direct, owners, relays, userData, ratchetKeys}
instance Encoding UserLinkData where
smpEncode (UserLinkData s) = if B.length s <= 254 then smpEncode s else smpEncode ('\255', Large s)
@@ -2090,8 +2210,17 @@ data SMPAgentError
A_DUPLICATE {droppedMsg_ :: Maybe DroppedMsg}
| -- | error in the message to add/delete/etc queue in connection
A_QUEUE {queueErr :: String}
| A_SERVICE {serviceError :: AgentServiceError}
deriving (Eq, Show, Exception)
data AgentServiceError
= ASERejected {rejectReason :: Text}
| ASETimeout
| ASENoPendingRequest
| ASENotDRAddress
| ASEBadSignature
deriving (Eq, Show)
data AgentCryptoError
= -- | AES decryption error
DECRYPT_AES
@@ -2116,6 +2245,19 @@ cryptoErrToSyncState = \case
RATCHET_SKIPPED _ -> RSRequired
RATCHET_SYNC -> RSRequired
$(J.deriveJSON defaultJSON ''DRInvitation)
-- JRConnReq is identical to JOIN before DR support for old/new client compatibility
instance StrEncoding JoinRequest where
strEncode = \case
JRConnReq ntfs cReq pqSup -> strEncode (ntfs, cReq, pqSup)
JRServiceReq cReq pqSup signKey -> strEncode ('S', cReq, pqSup, signKey)
JRInvitationDR dr -> serializeBinary $ LB.toStrict (J'.encode dr)
strP =
(A.char 'S' *> (JRServiceReq <$> _strP <*> _strP <*> _strP))
<|> (JRConnReq <$> strP <*> _strP <*> (_strP <|> pure PQSupportOff))
<|> (JRInvitationDR <$> (J'.eitherDecodeStrict' <$?> (A.take =<< (A.decimal <* "\n"))))
-- | SMP agent command and response parser for commands stored in db (fully parses binary bodies)
dbCommandP :: Parser ACommand
dbCommandP = commandP $ A.take =<< (A.decimal <* "\n")
@@ -2146,10 +2288,11 @@ commandP :: Parser ByteString -> Parser ACommand
commandP binaryP =
strP
>>= \case
NEW_ -> s (NEW <$> strP_ <*> strP_ <*> pqIKP <*> (strP <|> pure SMP.SMSubscribe))
-- useDR is a trailing field defaulting to False, so NEW persisted before it was added still parses
NEW_ -> s (NEW <$> strP_ <*> strP_ <*> pqIKP <*> (strP <|> pure SMP.SMSubscribe) <*> (_strP <|> pure False))
LSET_ -> s (LSET <$> strP <*> optional (A.space *> strP))
LGET_ -> s (LGET <$> strP)
JOIN_ -> s (JOIN <$> strP_ <*> strP_ <*> pqSupP <*> (strP_ <|> pure SMP.SMSubscribe) <*> binaryP)
JOIN_ -> s (JOIN <$> strP_ <*> (strP_ <|> pure SMP.SMSubscribe) <*> binaryP)
LET_ -> s (LET <$> A.takeTill (== ' ') <* A.space <*> binaryP)
ACK_ -> s (ACK <$> A.decimal <*> optional (A.space *> binaryP))
SWCH_ -> pure SWCH
@@ -2159,16 +2302,14 @@ commandP binaryP =
s p = A.space *> p
pqIKP :: Parser InitialKeys
pqIKP = strP_ <|> pure (IKLinkPQ PQSupportOff)
pqSupP :: Parser PQSupport
pqSupP = strP_ <|> pure PQSupportOff
-- | Serialize SMP agent command.
serializeCommand :: ACommand -> ByteString
serializeCommand = \case
NEW ntfs cMode pqIK subMode -> s (NEW_, ntfs, cMode, pqIK, subMode)
NEW ntfs cMode pqIK subMode useDR -> s (NEW_, ntfs, cMode, pqIK, subMode, useDR)
LSET uld cd_ -> s (LSET_, uld) <> maybe "" (B.cons ' ' . s) cd_
LGET sl -> s (LGET_, sl)
JOIN ntfs cReq pqSup subMode cInfo -> s (JOIN_, ntfs, cReq, pqSup, subMode, Str $ serializeBinary cInfo)
JOIN joinReq subMode cInfo -> s (JOIN_, joinReq, subMode, Str $ serializeBinary cInfo)
LET confId cInfo -> B.unwords [s LET_, confId, serializeBinary cInfo]
ACK mId rcptInfo_ -> s (ACK_, mId) <> maybe "" (B.cons ' ' . serializeBinary) rcptInfo_
SWCH -> s SWCH_
@@ -2200,6 +2341,8 @@ $(J.deriveJSON (sumTypeJSON id) ''ConnectionErrorType)
$(J.deriveJSON (sumTypeJSON id) ''AgentCryptoError)
$(J.deriveJSON (sumTypeJSON $ dropPrefix "ASE") ''AgentServiceError)
$(J.deriveJSON defaultJSON ''DroppedMsg)
$(J.deriveJSON (sumTypeJSON id) ''SMPAgentError)
+25 -5
View File
@@ -45,6 +45,8 @@ module Simplex.Messaging.Agent.Store
AcceptedConfirmation (..),
NewInvitation (..),
Invitation (..),
ContactRequest (..),
DRInvitation (..),
PrevExternalSndId,
PrevRcvMsgHash,
PrevSndMsgHash,
@@ -463,7 +465,9 @@ data ConnData = ConnData
lastExternalSndId :: PrevExternalSndId,
deleted :: Bool,
ratchetSyncState :: RatchetSyncState,
pqSupport :: PQSupport
pqSupport :: PQSupport,
-- client side: set on the requester's connection for a service request; Nothing otherwise. The time the client stops waiting for the response (created + serviceRequestTimeout or per-call override).
serviceRequestExpiresAt :: Maybe UTCTime
}
deriving (Eq, Show)
@@ -535,6 +539,7 @@ data InternalCommand
| ICDeleteRcvQueue SMP.RecipientId
| ICQSecure SMP.RecipientId SMP.SndPublicAuthKey
| ICQDelete SMP.RecipientId
| ICReplyDel
data InternalCommandTag
= ICAck_
@@ -545,6 +550,7 @@ data InternalCommandTag
| ICDeleteRcvQueue_
| ICQSecure_
| ICQDelete_
| ICReplyDel_
deriving (Show)
instance StrEncoding InternalCommand where
@@ -557,6 +563,7 @@ instance StrEncoding InternalCommand where
ICDeleteRcvQueue rId -> strEncode (ICDeleteRcvQueue_, rId)
ICQSecure rId senderKey -> strEncode (ICQSecure_, rId, senderKey)
ICQDelete rId -> strEncode (ICQDelete_, rId)
ICReplyDel -> strEncode ICReplyDel_
strP =
strP >>= \case
ICAck_ -> ICAck <$> _strP <*> _strP
@@ -567,6 +574,7 @@ instance StrEncoding InternalCommand where
ICDeleteRcvQueue_ -> ICDeleteRcvQueue <$> _strP
ICQSecure_ -> ICQSecure <$> _strP <*> _strP
ICQDelete_ -> ICQDelete <$> _strP
ICReplyDel_ -> pure ICReplyDel
instance StrEncoding InternalCommandTag where
strEncode = \case
@@ -578,6 +586,7 @@ instance StrEncoding InternalCommandTag where
ICDeleteRcvQueue_ -> "DELETE_RCV_QUEUE"
ICQSecure_ -> "QSECURE"
ICQDelete_ -> "QDELETE"
ICReplyDel_ -> "REPLY_DEL"
strP =
A.takeTill (== ' ') >>= \case
"ACK" -> pure ICAck_
@@ -588,6 +597,7 @@ instance StrEncoding InternalCommandTag where
"DELETE_RCV_QUEUE" -> pure ICDeleteRcvQueue_
"QSECURE" -> pure ICQSecure_
"QDELETE" -> pure ICQDelete_
"REPLY_DEL" -> pure ICReplyDel_
_ -> fail "bad InternalCommandTag"
agentCommandTag :: AgentCommand -> AgentCommandTag
@@ -605,6 +615,7 @@ internalCmdTag = \case
ICDeleteRcvQueue {} -> ICDeleteRcvQueue_
ICQSecure {} -> ICQSecure_
ICQDelete _ -> ICQDelete_
ICReplyDel -> ICReplyDel_
-- * Confirmation types
@@ -626,19 +637,28 @@ data AcceptedConfirmation = AcceptedConfirmation
data NewInvitation = NewInvitation
{ contactConnId :: ConnId,
connReq :: ConnectionRequestUri 'CMInvitation,
recipientConnInfo :: ConnInfo
connReq :: ContactRequest,
recipientConnInfo :: ConnInfo,
-- service side: the received request is a service request (SREQ) not a contact request (REQ)
serviceRequest :: Bool
}
data Invitation = Invitation
{ invitationId :: InvitationId,
contactConnId_ :: Maybe ConnId,
connReq :: ConnectionRequestUri 'CMInvitation,
connReq :: ContactRequest,
recipientConnInfo :: ConnInfo,
ownConnInfo :: Maybe ConnInfo,
accepted :: Bool
accepted :: Bool,
-- service side: the received request is a service request (SREQ) not a contact request (REQ)
serviceRequest :: Bool,
createdAt :: UTCTime
}
data ContactRequest
= CRInvitation (ConnectionRequestUri 'CMInvitation)
| CRInvitationDR DRInvitation
-- * Message integrity validation types
-- | Corresponds to `last_external_snd_msg_id` in `connections` table
+101 -14
View File
@@ -73,6 +73,8 @@ module Simplex.Messaging.Agent.Store.AgentStore
setConnPQSupport,
updateNewConnJoin,
getDeletedConnIds,
getExpiredServiceConns,
deleteExpiredServiceRequests,
getDeletedWaitingDeliveryConnIds,
setConnRatchetSync,
addProcessedRatchetKeyHash,
@@ -152,6 +154,10 @@ module Simplex.Messaging.Agent.Store.AgentStore
createRatchetX3dhKeys,
getRatchetX3dhKeys,
setRatchetX3dhKeys,
createAddressRatchetKeys,
getCurrentAddressRatchetKeys,
getAddressRatchetKeys,
deleteOldAddressRatchetKeys,
createSndRatchet,
getSndRatchet,
createRatchet,
@@ -278,9 +284,11 @@ import Control.Monad.IO.Class
import Control.Monad.Trans.Except
import Crypto.Random (ChaChaDRG)
import Data.Bifunctor (first)
import qualified Data.Aeson as J
import Data.ByteString (ByteString)
import qualified Data.ByteString.Base64.URL as U
import qualified Data.ByteString.Char8 as B
import qualified Data.ByteString.Lazy as LB
import Data.Functor (($>))
import Data.Int (Int64)
import Data.List (foldl', sortBy)
@@ -558,14 +566,16 @@ createSndConn db gVar cData q@SndQueue {server} =
insertSndQueue_ db connId q serverKeyHash_
createConnRecord :: DB.Connection -> ConnId -> ConnData -> SConnectionMode c -> IO ()
createConnRecord db connId ConnData {userId, connAgentVersion, enableNtfs, pqSupport} cMode =
createConnRecord db connId ConnData {userId, connAgentVersion, enableNtfs, pqSupport, serviceRequestExpiresAt} cMode = do
createdAt <- getCurrentTime
DB.execute
db
[sql|
INSERT INTO connections
(user_id, conn_id, conn_mode, smp_agent_version, enable_ntfs, pq_support, duplex_handshake) VALUES (?,?,?,?,?,?,?)
(user_id, conn_id, conn_mode, smp_agent_version, enable_ntfs, pq_support, service_request_expires_at, duplex_handshake, created_at)
VALUES (?,?,?,?,?,?,?,?,?)
|]
(userId, connId, cMode, connAgentVersion, BI enableNtfs, pqSupport, BI True)
(userId, connId, cMode, connAgentVersion, BI enableNtfs, pqSupport, serviceRequestExpiresAt, BI True, createdAt)
deleteConnRecord :: DB.Connection -> ConnId -> IO ()
deleteConnRecord db connId = DB.execute db "DELETE FROM connections WHERE conn_id = ?" (Only connId)
@@ -868,15 +878,15 @@ removeConfirmations db connId =
(Only connId)
createInvitation :: DB.Connection -> TVar ChaChaDRG -> NewInvitation -> IO (Either StoreError InvitationId)
createInvitation db gVar NewInvitation {contactConnId, connReq, recipientConnInfo} =
createInvitation db gVar NewInvitation {contactConnId, connReq, recipientConnInfo, serviceRequest} =
createWithRandomId db gVar $ \invitationId ->
DB.execute
db
[sql|
INSERT INTO conn_invitations
(invitation_id, contact_conn_id, cr_invitation, recipient_conn_info, accepted) VALUES (?, ?, ?, ?, 0);
(invitation_id, contact_conn_id, cr_invitation, recipient_conn_info, accepted, service_request) VALUES (?, ?, ?, ?, 0, ?);
|]
(Binary invitationId, contactConnId, connReq, Binary recipientConnInfo)
(Binary invitationId, contactConnId, connReq, Binary recipientConnInfo, BI serviceRequest)
getInvitation :: DB.Connection -> String -> InvitationId -> IO (Either StoreError Invitation)
getInvitation db cxt invitationId =
@@ -884,15 +894,15 @@ getInvitation db cxt invitationId =
DB.query
db
[sql|
SELECT contact_conn_id, cr_invitation, recipient_conn_info, own_conn_info, accepted
SELECT contact_conn_id, cr_invitation, recipient_conn_info, own_conn_info, accepted, service_request, created_at
FROM conn_invitations
WHERE invitation_id = ?
AND accepted = 0
|]
(Only (Binary invitationId))
where
invitation (contactConnId_, connReq, recipientConnInfo, ownConnInfo, BI accepted) =
Invitation {invitationId, contactConnId_, connReq, recipientConnInfo, ownConnInfo, accepted}
invitation (contactConnId_, connReq, recipientConnInfo, ownConnInfo, BI accepted, BI serviceRequest, createdAt) =
Invitation {invitationId, contactConnId_, connReq, recipientConnInfo, ownConnInfo, accepted, serviceRequest, createdAt}
acceptInvitation :: DB.Connection -> InvitationId -> ConnInfo -> IO ()
acceptInvitation db invitationId ownConnInfo =
@@ -1384,6 +1394,60 @@ setRatchetX3dhKeys db connId (x3dhPrivKey1, x3dhPrivKey2, pqPrivKem) =
|]
(x3dhPrivKey1, x3dhPrivKey2, pqPrivKem, connId)
createAddressRatchetKeys :: DB.Connection -> ConnId -> (RatchetKeyId, CR.RcvE2EPrivRatchetParams 'C.X448) -> IO ()
createAddressRatchetKeys db connId (ratchetKeyId, (x3dhPrivKey1, x3dhPrivKey2, pqPrivKem)) =
DB.execute
db
[sql|
INSERT INTO address_ratchet_keys
(conn_id, ratchet_key_id, x3dh_priv_key_1, x3dh_priv_key_2, pq_priv_kem)
VALUES (?, ?, ?, ?, ?)
|]
(connId, ratchetKeyId, x3dhPrivKey1, x3dhPrivKey2, pqPrivKem)
getCurrentAddressRatchetKeys :: DB.Connection -> ConnId -> IO (Either StoreError (RatchetKeyId, CR.RcvE2EPrivRatchetParams 'C.X448))
getCurrentAddressRatchetKeys db connId =
firstRow (\(Only rkId :. pks) -> (rkId, pks)) SEX3dhKeysNotFound $
DB.query
db
[sql|
SELECT ratchet_key_id, x3dh_priv_key_1, x3dh_priv_key_2, pq_priv_kem
FROM address_ratchet_keys
WHERE conn_id = ?
ORDER BY address_ratchet_key_id DESC
LIMIT 1
|]
(Only connId)
getAddressRatchetKeys :: DB.Connection -> ConnId -> RatchetKeyId -> IO (Either StoreError (CR.RcvE2EPrivRatchetParams 'C.X448))
getAddressRatchetKeys db connId ratchetKeyId =
firstRow id SEX3dhKeysNotFound $
DB.query
db
[sql|
SELECT x3dh_priv_key_1, x3dh_priv_key_2, pq_priv_kem
FROM address_ratchet_keys
WHERE conn_id = ? AND ratchet_key_id = ?
|]
(connId, ratchetKeyId)
deleteOldAddressRatchetKeys :: DB.Connection -> ConnId -> Int -> IO ()
deleteOldAddressRatchetKeys db connId keep =
DB.execute
db
[sql|
DELETE FROM address_ratchet_keys
WHERE conn_id = ?
AND address_ratchet_key_id NOT IN (
SELECT address_ratchet_key_id
FROM address_ratchet_keys
WHERE conn_id = ?
ORDER BY address_ratchet_key_id DESC
LIMIT ?
)
|]
(connId, connId, keep)
createSndRatchet :: DB.Connection -> ConnId -> RatchetX448 -> CR.AE2ERatchetParams 'C.X448 -> IO ()
createSndRatchet db connId ratchetState (CR.AE2ERatchetParams s (CR.E2ERatchetParams _ x3dhPubKey1 x3dhPubKey2 pqPubKem)) =
DB.execute
@@ -2075,6 +2139,21 @@ instance ConnectionModeI c => ToField (ConnectionRequestUri c) where toField = t
instance (E.Typeable c, ConnectionModeI c) => FromField (ConnectionRequestUri c) where fromField = blobFieldDecoder strDecode
instance ToField RatchetKeyId where toField (RatchetKeyId s) = toField $ Binary s
instance FromField RatchetKeyId where fromField = blobFieldDecoder $ Right . RatchetKeyId
instance ToField ContactRequest where
toField = toField . Binary . \case
CRInvitation cr -> strEncode cr
CRInvitationDR dr -> LB.toStrict $ J.encode dr
instance FromField ContactRequest where
fromField = blobFieldDecoder $ \bs ->
if "{" `B.isPrefixOf` bs
then CRInvitationDR <$> J.eitherDecodeStrict' bs
else CRInvitation <$> strDecode bs
instance ToField ConnectionMode where toField = toField . decodeLatin1 . strEncode
instance FromField ConnectionMode where fromField = fromTextField_ connModeT
@@ -2549,7 +2628,7 @@ getConnsData_ deleted' db connIds =
db
[sql|
SELECT user_id, conn_id, conn_mode, smp_agent_version, enable_ntfs,
last_external_snd_msg_id, deleted, ratchet_sync_state, pq_support
last_external_snd_msg_id, deleted, ratchet_sync_state, pq_support, service_request_expires_at
FROM connections
WHERE conn_id IN ? AND deleted = ?
|]
@@ -2584,7 +2663,7 @@ getConnData deleted' forUpdate db connId' =
db
( [sql|
SELECT user_id, conn_id, conn_mode, smp_agent_version, enable_ntfs,
last_external_snd_msg_id, deleted, ratchet_sync_state, pq_support
last_external_snd_msg_id, deleted, ratchet_sync_state, pq_support, service_request_expires_at
FROM connections
WHERE conn_id = ? AND deleted = ?
|]
@@ -2601,9 +2680,9 @@ lockConnForUpdate db connId = do
#endif
pure ()
rowToConnData :: (UserId, ConnId, ConnectionMode, VersionSMPA, Maybe BoolInt, PrevExternalSndId, BoolInt, RatchetSyncState, PQSupport) -> (ConnData, ConnectionMode)
rowToConnData (userId, connId, cMode, connAgentVersion, enableNtfs_, lastExternalSndId, BI deleted, ratchetSyncState, pqSupport) =
(ConnData {userId, connId, connAgentVersion, enableNtfs = maybe True unBI enableNtfs_, lastExternalSndId, deleted, ratchetSyncState, pqSupport}, cMode)
rowToConnData :: (UserId, ConnId, ConnectionMode, VersionSMPA, Maybe BoolInt, PrevExternalSndId, BoolInt, RatchetSyncState, PQSupport, Maybe UTCTime) -> (ConnData, ConnectionMode)
rowToConnData (userId, connId, cMode, connAgentVersion, enableNtfs_, lastExternalSndId, BI deleted, ratchetSyncState, pqSupport, serviceRequestExpiresAt) =
(ConnData {userId, connId, connAgentVersion, enableNtfs = maybe True unBI enableNtfs_, lastExternalSndId, deleted, ratchetSyncState, pqSupport, serviceRequestExpiresAt}, cMode)
setConnDeleted :: DB.Connection -> Bool -> ConnId -> IO ()
setConnDeleted db waitDelivery connId
@@ -2632,6 +2711,14 @@ updateNewConnJoin db connId aVersion pqSupport enableNtfs =
getDeletedConnIds :: DB.Connection -> IO [ConnId]
getDeletedConnIds db = map fromOnly <$> DB.query db "SELECT conn_id FROM connections WHERE deleted = ?" (Only (BI True))
getExpiredServiceConns :: DB.Connection -> UTCTime -> IO [ConnId]
getExpiredServiceConns db now =
map fromOnly <$> DB.query db "SELECT conn_id FROM connections WHERE service_request_expires_at < ? AND deleted = 0 AND deleted_at_wait_delivery IS NULL" (Only now)
deleteExpiredServiceRequests :: DB.Connection -> UTCTime -> IO ()
deleteExpiredServiceRequests db expireTs =
DB.execute db "DELETE FROM conn_invitations WHERE service_request = 1 AND created_at < ?" (Only expireTs)
getDeletedWaitingDeliveryConnIds :: DB.Connection -> IO [ConnId]
getDeletedWaitingDeliveryConnIds db =
map fromOnly <$> DB.query_ db "SELECT conn_id FROM connections WHERE deleted_at_wait_delivery IS NOT NULL"
@@ -13,6 +13,7 @@ import Simplex.Messaging.Agent.Store.Postgres.Migrations.M20251010_client_notice
import Simplex.Messaging.Agent.Store.Postgres.Migrations.M20251230_strict_tables
import Simplex.Messaging.Agent.Store.Postgres.Migrations.M20260410_receive_attempts
import Simplex.Messaging.Agent.Store.Postgres.Migrations.M20260411_service_certs
import Simplex.Messaging.Agent.Store.Postgres.Migrations.M20260712_address_dr_rpc
import Simplex.Messaging.Agent.Store.Shared (Migration (..))
schemaMigrations :: [(String, Text, Maybe Text)]
@@ -25,7 +26,8 @@ schemaMigrations =
("20251010_client_notices", m20251010_client_notices, Just down_m20251010_client_notices),
("20251230_strict_tables", m20251230_strict_tables, Just down_m20251230_strict_tables),
("20260410_receive_attempts", m20260410_receive_attempts, Just down_m20260410_receive_attempts),
("20260411_service_certs", m20260411_service_certs, Just down_m20260411_service_certs)
("20260411_service_certs", m20260411_service_certs, Just down_m20260411_service_certs),
("20260712_address_dr_rpc", m20260712_address_dr_rpc, Just down_m20260712_address_dr_rpc)
]
-- | The list of migrations in ascending order by date
@@ -0,0 +1,42 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
module Simplex.Messaging.Agent.Store.Postgres.Migrations.M20260712_address_dr_rpc where
import Data.Text (Text)
import Text.RawString.QQ (r)
m20260712_address_dr_rpc :: Text
m20260712_address_dr_rpc =
[r|
CREATE TABLE address_ratchet_keys(
address_ratchet_key_id BIGSERIAL PRIMARY KEY,
conn_id BYTEA NOT NULL REFERENCES connections ON DELETE CASCADE,
ratchet_key_id BYTEA NOT NULL,
x3dh_priv_key_1 BYTEA NOT NULL,
x3dh_priv_key_2 BYTEA NOT NULL,
pq_priv_kem BYTEA,
created_at TIMESTAMPTZ NOT NULL DEFAULT (now())
);
CREATE UNIQUE INDEX idx_address_ratchet_keys ON address_ratchet_keys(conn_id, ratchet_key_id);
ALTER TABLE conn_invitations ADD COLUMN service_request SMALLINT NOT NULL DEFAULT 0; -- service side: received request is a service request (SREQ) not a contact request (REQ)
ALTER TABLE connections ADD COLUMN created_at TIMESTAMPTZ NOT NULL DEFAULT '1970-01-01 00:00:00';
ALTER TABLE connections ADD COLUMN service_request_expires_at TIMESTAMPTZ; -- client side: requester's outstanding service request; the time the client stops waiting for the response
CREATE INDEX idx_connections_deleted ON connections(deleted);
CREATE INDEX idx_connections_service_request_expires_at ON connections(service_request_expires_at);
|]
down_m20260712_address_dr_rpc :: Text
down_m20260712_address_dr_rpc =
[r|
DROP INDEX idx_connections_service_request_expires_at;
DROP INDEX idx_connections_deleted;
ALTER TABLE connections DROP COLUMN service_request_expires_at;
ALTER TABLE connections DROP COLUMN created_at;
ALTER TABLE conn_invitations DROP COLUMN service_request;
DROP INDEX idx_address_ratchet_keys;
DROP TABLE address_ratchet_keys;
|]
@@ -104,6 +104,31 @@ CREATE AGGREGATE smp_agent_test_protocol_schema.xor_aggregate(bytea) (
SET default_table_access_method = heap;
CREATE TABLE smp_agent_test_protocol_schema.address_ratchet_keys (
address_ratchet_key_id bigint NOT NULL,
conn_id bytea NOT NULL,
ratchet_key_id bytea NOT NULL,
x3dh_priv_key_1 bytea NOT NULL,
x3dh_priv_key_2 bytea NOT NULL,
pq_priv_kem bytea,
created_at timestamp with time zone DEFAULT now() NOT NULL
);
CREATE SEQUENCE smp_agent_test_protocol_schema.address_ratchet_keys_address_ratchet_key_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE smp_agent_test_protocol_schema.address_ratchet_keys_address_ratchet_key_id_seq OWNED BY smp_agent_test_protocol_schema.address_ratchet_keys.address_ratchet_key_id;
CREATE TABLE smp_agent_test_protocol_schema.client_notices (
client_notice_id bigint NOT NULL,
protocol text NOT NULL,
@@ -194,7 +219,8 @@ CREATE TABLE smp_agent_test_protocol_schema.conn_invitations (
recipient_conn_info bytea NOT NULL,
accepted smallint DEFAULT 0 NOT NULL,
own_conn_info bytea,
created_at timestamp with time zone DEFAULT now() NOT NULL
created_at timestamp with time zone DEFAULT now() NOT NULL,
service_request smallint DEFAULT 0 NOT NULL
);
@@ -215,7 +241,9 @@ CREATE TABLE smp_agent_test_protocol_schema.connections (
user_id bigint NOT NULL,
ratchet_sync_state text DEFAULT 'ok'::text NOT NULL,
deleted_at_wait_delivery timestamp with time zone,
pq_support smallint DEFAULT 0 NOT NULL
pq_support smallint DEFAULT 0 NOT NULL,
created_at timestamp with time zone DEFAULT '1970-01-01 00:00:00+01'::timestamp with time zone NOT NULL,
service_request_expires_at timestamp with time zone
);
@@ -847,6 +875,15 @@ ALTER TABLE smp_agent_test_protocol_schema.xftp_servers ALTER COLUMN xftp_server
ALTER TABLE ONLY smp_agent_test_protocol_schema.address_ratchet_keys ALTER COLUMN address_ratchet_key_id SET DEFAULT nextval('smp_agent_test_protocol_schema.address_ratchet_keys_address_ratchet_key_id_seq'::regclass);
ALTER TABLE ONLY smp_agent_test_protocol_schema.address_ratchet_keys
ADD CONSTRAINT address_ratchet_keys_pkey PRIMARY KEY (address_ratchet_key_id);
ALTER TABLE ONLY smp_agent_test_protocol_schema.client_notices
ADD CONSTRAINT client_notices_pkey PRIMARY KEY (client_notice_id);
@@ -1032,6 +1069,10 @@ ALTER TABLE ONLY smp_agent_test_protocol_schema.xftp_servers
CREATE UNIQUE INDEX idx_address_ratchet_keys ON smp_agent_test_protocol_schema.address_ratchet_keys USING btree (conn_id, ratchet_key_id);
CREATE UNIQUE INDEX idx_client_notices_entity ON smp_agent_test_protocol_schema.client_notices USING btree (protocol, host, port, entity_id);
@@ -1056,6 +1097,14 @@ CREATE INDEX idx_conn_invitations_contact_conn_id ON smp_agent_test_protocol_sch
CREATE INDEX idx_connections_deleted ON smp_agent_test_protocol_schema.connections USING btree (deleted);
CREATE INDEX idx_connections_service_request_expires_at ON smp_agent_test_protocol_schema.connections USING btree (service_request_expires_at);
CREATE INDEX idx_connections_user ON smp_agent_test_protocol_schema.connections USING btree (user_id);
@@ -1268,6 +1317,11 @@ CREATE TRIGGER tr_rcv_queue_update AFTER UPDATE ON smp_agent_test_protocol_schem
ALTER TABLE ONLY smp_agent_test_protocol_schema.address_ratchet_keys
ADD CONSTRAINT address_ratchet_keys_conn_id_fkey FOREIGN KEY (conn_id) REFERENCES smp_agent_test_protocol_schema.connections(conn_id) ON DELETE CASCADE;
ALTER TABLE ONLY smp_agent_test_protocol_schema.client_services
ADD CONSTRAINT client_services_host_port_fkey FOREIGN KEY (host, port) REFERENCES smp_agent_test_protocol_schema.servers(host, port) ON DELETE RESTRICT;
@@ -49,6 +49,7 @@ import Simplex.Messaging.Agent.Store.SQLite.Migrations.M20251010_client_notices
import Simplex.Messaging.Agent.Store.SQLite.Migrations.M20251230_strict_tables
import Simplex.Messaging.Agent.Store.SQLite.Migrations.M20260410_receive_attempts
import Simplex.Messaging.Agent.Store.SQLite.Migrations.M20260411_service_certs
import Simplex.Messaging.Agent.Store.SQLite.Migrations.M20260712_address_dr_rpc
import Simplex.Messaging.Agent.Store.Shared (Migration (..))
schemaMigrations :: [(String, Query, Maybe Query)]
@@ -97,7 +98,8 @@ schemaMigrations =
("m20251010_client_notices", m20251010_client_notices, Just down_m20251010_client_notices),
("m20251230_strict_tables", m20251230_strict_tables, Just down_m20251230_strict_tables),
("m20260410_receive_attempts", m20260410_receive_attempts, Just down_m20260410_receive_attempts),
("m20260411_service_certs", m20260411_service_certs, Just down_m20260411_service_certs)
("m20260411_service_certs", m20260411_service_certs, Just down_m20260411_service_certs),
("m20260712_address_dr_rpc", m20260712_address_dr_rpc, Just down_m20260712_address_dr_rpc)
]
-- | The list of migrations in ascending order by date
@@ -0,0 +1,41 @@
{-# LANGUAGE QuasiQuotes #-}
module Simplex.Messaging.Agent.Store.SQLite.Migrations.M20260712_address_dr_rpc where
import Database.SQLite.Simple (Query)
import Database.SQLite.Simple.QQ (sql)
m20260712_address_dr_rpc :: Query
m20260712_address_dr_rpc =
[sql|
CREATE TABLE address_ratchet_keys(
address_ratchet_key_id INTEGER PRIMARY KEY AUTOINCREMENT,
conn_id BLOB NOT NULL REFERENCES connections ON DELETE CASCADE,
ratchet_key_id BLOB NOT NULL,
x3dh_priv_key_1 BLOB NOT NULL,
x3dh_priv_key_2 BLOB NOT NULL,
pq_priv_kem BLOB,
created_at TEXT NOT NULL DEFAULT(datetime('now'))
) STRICT;
CREATE UNIQUE INDEX idx_address_ratchet_keys ON address_ratchet_keys(conn_id, ratchet_key_id);
ALTER TABLE conn_invitations ADD COLUMN service_request INTEGER NOT NULL DEFAULT 0; -- service side: received request is a service request (SREQ) not a contact request (REQ)
ALTER TABLE connections ADD COLUMN created_at TEXT NOT NULL DEFAULT('1970-01-01 00:00:00');
ALTER TABLE connections ADD COLUMN service_request_expires_at TEXT; -- client side: requester's outstanding service request; the time the client stops waiting for the response
CREATE INDEX idx_connections_deleted ON connections(deleted);
CREATE INDEX idx_connections_service_request_expires_at ON connections(service_request_expires_at);
|]
down_m20260712_address_dr_rpc :: Query
down_m20260712_address_dr_rpc =
[sql|
DROP INDEX idx_connections_service_request_expires_at;
DROP INDEX idx_connections_deleted;
ALTER TABLE connections DROP COLUMN service_request_expires_at;
ALTER TABLE connections DROP COLUMN created_at;
ALTER TABLE conn_invitations DROP COLUMN service_request;
DROP INDEX idx_address_ratchet_keys;
DROP TABLE address_ratchet_keys;
|]
@@ -27,7 +27,9 @@ CREATE TABLE connections(
REFERENCES users ON DELETE CASCADE,
ratchet_sync_state TEXT NOT NULL DEFAULT 'ok',
deleted_at_wait_delivery TEXT,
pq_support INTEGER NOT NULL DEFAULT 0
pq_support INTEGER NOT NULL DEFAULT 0,
created_at TEXT NOT NULL DEFAULT('1970-01-01 00:00:00'),
service_request_expires_at TEXT
) WITHOUT ROWID, STRICT;
CREATE TABLE rcv_queues(
host TEXT NOT NULL,
@@ -164,6 +166,8 @@ CREATE TABLE conn_invitations(
accepted INTEGER NOT NULL DEFAULT 0,
own_conn_info BLOB,
created_at TEXT NOT NULL DEFAULT(datetime('now'))
,
service_request INTEGER NOT NULL DEFAULT 0
) WITHOUT ROWID, STRICT;
CREATE TABLE ratchets(
conn_id BLOB NOT NULL PRIMARY KEY REFERENCES connections
@@ -465,6 +469,15 @@ CREATE TABLE client_services(
service_queue_ids_hash BLOB NOT NULL DEFAULT x'00000000000000000000000000000000',
FOREIGN KEY(host, port) REFERENCES servers ON UPDATE CASCADE ON DELETE RESTRICT
) STRICT;
CREATE TABLE address_ratchet_keys(
address_ratchet_key_id INTEGER PRIMARY KEY AUTOINCREMENT,
conn_id BLOB NOT NULL REFERENCES connections ON DELETE CASCADE,
ratchet_key_id BLOB NOT NULL,
x3dh_priv_key_1 BLOB NOT NULL,
x3dh_priv_key_2 BLOB NOT NULL,
pq_priv_kem BLOB,
created_at TEXT NOT NULL DEFAULT(datetime('now'))
) STRICT;
CREATE UNIQUE INDEX idx_rcv_queues_ntf ON rcv_queues(host, port, ntf_id);
CREATE UNIQUE INDEX idx_rcv_queue_id ON rcv_queues(conn_id, rcv_queue_id);
CREATE UNIQUE INDEX idx_snd_queue_id ON snd_queues(conn_id, snd_queue_id);
@@ -615,6 +628,14 @@ CREATE UNIQUE INDEX idx_server_certs_user_id_host_port ON client_services(
server_key_hash
);
CREATE INDEX idx_server_certs_host_port ON client_services(host, port);
CREATE UNIQUE INDEX idx_address_ratchet_keys ON address_ratchet_keys(
conn_id,
ratchet_key_id
);
CREATE INDEX idx_connections_deleted ON connections(deleted);
CREATE INDEX idx_connections_service_request_expires_at ON connections(
service_request_expires_at
);
CREATE TRIGGER tr_rcv_queue_insert
AFTER INSERT ON rcv_queues
FOR EACH ROW
+1 -1
View File
@@ -973,7 +973,7 @@ deleteSMPQueueLink :: SMPClient -> NetworkRequestMode -> RcvPrivateAuthKey -> Re
deleteSMPQueueLink = okSMPCommand LDEL
{-# INLINE deleteSMPQueueLink #-}
-- | Get 1-time inviation SMP queue link data and secure the queue via queue link ID.
-- | Get 1-time invitation SMP queue link data and secure the queue via queue link ID.
secureGetSMPQueueLink :: SMPClient -> NetworkRequestMode -> SndPrivateAuthKey -> LinkId -> ExceptT SMPClientError IO (SenderId, QueueLinkData)
secureGetSMPQueueLink c nm spKey lnkId =
sendSMPCommand c nm (Just spKey) lnkId (LKEY $ C.toPublic spKey) >>= \case
+12 -2
View File
@@ -61,6 +61,7 @@ module Simplex.Messaging.Crypto
APublicAuthKey (..),
CryptoPublicKey (..),
CryptoPrivateKey (..),
StoredPrivateKey (..),
AAuthKeyPair,
KeyPair,
KeyPairX25519,
@@ -342,8 +343,17 @@ deriving instance Eq (PrivateKey a)
deriving instance Show (PrivateKey a)
-- Do not enable, to avoid leaking key data
-- instance StrEncoding (PrivateKey Ed25519) where
-- Do not enable, to avoid leaking key data, use StoredPrivateKey instead
-- instance StrEncoding (PrivateKey a) where
newtype StoredPrivateKey a = StoredPrivateKey {unStored :: PrivateKey a}
deriving (Show)
instance AlgorithmI a => StrEncoding (StoredPrivateKey a) where
strEncode = strEncode . encodePrivKey . unStored
{-# INLINE strEncode #-}
strDecode = fmap StoredPrivateKey . decodePrivKey
{-# INLINE strDecode #-}
-- Used in notification store log
instance StrEncoding (PrivateKey X25519) where
+1
View File
@@ -37,6 +37,7 @@ module Simplex.Messaging.Crypto.Ratchet
AUseKEM (..),
RatchetKEMState (..),
SRatchetKEMState (..),
RatchetKEMStateI (..),
RcvPrivRKEMParams,
APrivRKEMParams (..),
RcvE2ERatchetParamsUri,
+4 -4
View File
@@ -53,14 +53,14 @@ invShortLinkKdf :: LinkKey -> C.SbKey
invShortLinkKdf (LinkKey k) = C.unsafeSbKey $ C.hkdf "" k "SimpleXInvLink" 32
encodeSignLinkData :: forall c. ConnectionModeI c => C.KeyPairEd25519 -> VersionRangeSMPA -> ConnectionRequestUri c -> Maybe ByteString -> UserConnLinkData c -> (LinkKey, (ByteString, ByteString))
encodeSignLinkData keys@(_, pk) agentVRange linkConnReq linkEntityId userData =
let (linkKey, fd) = encodeSignFixedData keys agentVRange linkConnReq linkEntityId
encodeSignLinkData keys@(_, pk) agentVRange connReq linkEntityId userData =
let (linkKey, fd) = encodeSignFixedData keys agentVRange connReq linkEntityId
md = encodeSignUserData (sConnectionMode @c) pk agentVRange userData
in (linkKey, (fd, md))
encodeSignFixedData :: ConnectionModeI c => C.KeyPairEd25519 -> VersionRangeSMPA -> ConnectionRequestUri c -> Maybe ByteString -> (LinkKey, ByteString)
encodeSignFixedData (rootKey, pk) agentVRange linkConnReq linkEntityId =
let fd = smpEncode FixedLinkData {agentVRange, rootKey, linkConnReq, linkEntityId}
encodeSignFixedData (rootKey, pk) agentVRange connReq linkEntityId =
let fd = smpEncode FixedLinkData {agentVRange, rootKey, linkConnReq = binaryConnReq connReq, linkEntityId}
in (LinkKey (C.sha3_256 fd), encodeSign pk fd)
encodeSignUserData :: ConnectionModeI c => SConnectionMode c -> C.PrivateKeyEd25519 -> VersionRangeSMPA -> UserConnLinkData c -> ByteString
+7
View File
@@ -11,6 +11,7 @@ module Simplex.Messaging.Encoding
( Encoding (..),
Tail (..),
Large (..),
EncList (..),
_smpP,
smpEncodeList,
smpListP,
@@ -177,6 +178,12 @@ instance Encoding a => Encoding (L.NonEmpty a) where
0 -> fail "empty list"
n -> L.fromList <$> A.count n smpP
newtype EncList a = EncList [a]
instance Encoding a => Encoding (EncList a) where
smpEncode (EncList xs) = smpEncodeList xs
smpP = EncList <$> smpListP
instance (Encoding a, Encoding b) => Encoding (a, b) where
smpEncode (a, b) = smpEncode a <> smpEncode b
{-# INLINE smpEncode #-}