@@ -251,7 +251,6 @@ import Simplex.Messaging.Protocol
251251 ErrorType ,
252252 MsgFlags (.. ),
253253 MsgId ,
254- NtfPublicAuthKey ,
255254 NtfServer ,
256255 NtfServerWithAuth ,
257256 ProtoServer ,
@@ -261,12 +260,14 @@ import Simplex.Messaging.Protocol
261260 ProtocolType (.. ),
262261 ProtocolTypeI (.. ),
263262 QueueIdsKeys (.. ),
263+ ServerNtfCreds (.. ),
264264 RcvMessage (.. ),
265265 RcvNtfPublicDhKey ,
266266 SMPMsgMeta (.. ),
267267 SProtocolType (.. ),
268268 SndPublicAuthKey ,
269269 SubscriptionMode (.. ),
270+ NewNtfCreds (.. ),
270271 QueueReqData (.. ),
271272 QueueLinkData ,
272273 UserProtocol ,
@@ -283,7 +284,7 @@ import Simplex.Messaging.Session
283284import Simplex.Messaging.Agent.Store.Entity
284285import Simplex.Messaging.TMap (TMap )
285286import qualified Simplex.Messaging.TMap as TM
286- import Simplex.Messaging.Transport (SMPVersion , SessionId , THandleParams (sessionId , thVersion ), TransportError (.. ), TransportPeer (.. ), sndAuthKeySMPVersion , shortLinksSMPVersion )
287+ import Simplex.Messaging.Transport (SMPVersion , SessionId , THandleParams (sessionId , thVersion ), TransportError (.. ), TransportPeer (.. ), sndAuthKeySMPVersion , shortLinksSMPVersion , newNtfCredsSMPVersion )
287288import Simplex.Messaging.Transport.Client (TransportHost (.. ))
288289import Simplex.Messaging.Util
289290import Simplex.Messaging.Version
@@ -1240,8 +1241,7 @@ runSMPServerTest c nm userId (ProtoServerWithAuth srv auth) = do
12401241 (sKey, spKey) <- atomically $ C. generateAuthKeyPair sa g
12411242 (dhKey, _) <- atomically $ C. generateKeyPair g
12421243 r <- runExceptT $ do
1243- -- TODO [notifications]
1244- SMP. QIK {rcvId, sndId, queueMode} <- liftError (testErr TSCreateQueue ) $ createSMPQueue smp nm Nothing rKeys dhKey auth SMSubscribe (QRMessaging Nothing ) -- Nothing
1244+ SMP. QIK {rcvId, sndId, queueMode} <- liftError (testErr TSCreateQueue ) $ createSMPQueue smp nm Nothing rKeys dhKey auth SMSubscribe (QRMessaging Nothing ) Nothing
12451245 liftError (testErr TSSecureQueue ) $
12461246 case queueMode of
12471247 Just QMMessaging -> secureSndSMPQueue smp nm spKey sndId sKey
@@ -1352,12 +1352,11 @@ getSessionMode :: MonadIO m => AgentClient -> m TransportSessionMode
13521352getSessionMode = fmap sessionMode . getNetworkConfig
13531353{-# INLINE getSessionMode #-}
13541354
1355- -- TODO [notifications]
1356- newRcvQueue :: AgentClient -> NetworkRequestMode -> UserId -> ConnId -> SMPServerWithAuth -> VersionRangeSMPC -> SConnectionMode c -> SubscriptionMode -> AM (NewRcvQueue , SMPQueueUri , SMPTransportSession , SessionId )
1357- newRcvQueue c nm userId connId srv vRange cMode subMode = do
1355+ newRcvQueue :: AgentClient -> NetworkRequestMode -> UserId -> ConnId -> SMPServerWithAuth -> VersionRangeSMPC -> SConnectionMode c -> Bool -> SubscriptionMode -> AM (NewRcvQueue , SMPQueueUri , SMPTransportSession , SessionId )
1356+ newRcvQueue c nm userId connId srv vRange cMode enableNtfs subMode = do
13581357 let qrd = case cMode of SCMInvitation -> CQRMessaging Nothing ; SCMContact -> CQRContact Nothing
13591358 e2eKeys <- atomically . C. generateKeyPair =<< asks random
1360- newRcvQueue_ c nm userId connId srv vRange qrd subMode Nothing e2eKeys
1359+ newRcvQueue_ c nm userId connId srv vRange qrd enableNtfs subMode Nothing e2eKeys
13611360
13621361data ClntQueueReqData
13631362 = CQRMessaging (Maybe (CQRData (SMP. SenderId , QueueLinkData )))
@@ -1374,21 +1373,21 @@ queueReqData = \case
13741373 CQRMessaging d -> QRMessaging $ srvReq <$> d
13751374 CQRContact d -> QRContact $ srvReq <$> d
13761375
1377- newRcvQueue_ :: AgentClient -> NetworkRequestMode -> UserId -> ConnId -> SMPServerWithAuth -> VersionRangeSMPC -> ClntQueueReqData -> SubscriptionMode -> Maybe C. CbNonce -> C. KeyPairX25519 -> AM (NewRcvQueue , SMPQueueUri , SMPTransportSession , SessionId )
1378- newRcvQueue_ c nm userId connId (ProtoServerWithAuth srv auth) vRange cqrd subMode nonce_ (e2eDhKey, e2ePrivKey) = do
1376+ newRcvQueue_ :: AgentClient -> NetworkRequestMode -> UserId -> ConnId -> SMPServerWithAuth -> VersionRangeSMPC -> ClntQueueReqData -> Bool -> SubscriptionMode -> Maybe C. CbNonce -> C. KeyPairX25519 -> AM (NewRcvQueue , SMPQueueUri , SMPTransportSession , SessionId )
1377+ newRcvQueue_ c nm userId connId (ProtoServerWithAuth srv auth) vRange cqrd enableNtfs subMode nonce_ (e2eDhKey, e2ePrivKey) = do
13791378 C. AuthAlg a <- asks (rcvAuthAlg . config)
13801379 g <- asks random
13811380 rKeys@ (_, rcvPrivateKey) <- atomically $ C. generateAuthKeyPair a g
13821381 (dhKey, privDhKey) <- atomically $ C. generateKeyPair g
13831382 logServer " -->" c srv NoEntity " NEW"
13841383 tSess <- mkTransportSession c userId srv connId
1385- -- TODO [notifications]
1386- r @ (thParams', QIK {rcvId, sndId, rcvPublicDhKey, queueMode, serviceId}) <-
1387- withClient c nm tSess $ \ ( SMPConnectedClient smp _) ->
1388- (thParams smp,) <$> createSMPQueue smp nm nonce_ rKeys dhKey auth subMode (queueReqData cqrd)
1384+ (thParams', ntfKeys, qik @ QIK {rcvId, sndId, rcvPublicDhKey, queueMode, serviceId, serverNtfCreds}) <-
1385+ withClient c nm tSess $ \ ( SMPConnectedClient smp _) -> do
1386+ (ntfKeys, ntfCreds) <- liftIO $ mkNtfCreds a g smp
1387+ (thParams smp,ntfKeys, ) <$> createSMPQueue smp nm nonce_ rKeys dhKey auth subMode (queueReqData cqrd) ntfCreds
13891388 -- TODO [certs rcv] validate that serviceId is the same as in the client session
13901389 liftIO . logServer " <--" c srv NoEntity $ B. unwords [" IDS" , logSecret rcvId, logSecret sndId]
1391- shortLink <- mkShortLinkCreds r
1390+ shortLink <- mkShortLinkCreds thParams' qik
13921391 let rq =
13931392 RcvQueue
13941393 { userId,
@@ -1409,14 +1408,26 @@ newRcvQueue_ c nm userId connId (ProtoServerWithAuth srv auth) vRange cqrd subMo
14091408 dbReplaceQueueId = Nothing ,
14101409 rcvSwchStatus = Nothing ,
14111410 smpClientVersion = maxVersion vRange,
1412- clientNtfCreds = Nothing ,
1411+ clientNtfCreds = mkClientNtfCreds ntfKeys serverNtfCreds ,
14131412 deleteErrors = 0
14141413 }
14151414 qUri = SMPQueueUri vRange $ SMPQueueAddress srv sndId e2eDhKey queueMode
14161415 pure (rq, qUri, tSess, sessionId thParams')
14171416 where
1418- mkShortLinkCreds :: (THandleParams SMPVersion 'TClient, QueueIdsKeys ) -> AM (Maybe ShortLinkCreds )
1419- mkShortLinkCreds (thParams', QIK {sndId, queueMode, linkId}) = case (cqrd, queueMode) of
1417+ mkNtfCreds :: (C. AlgorithmI a , C. AuthAlgorithm a ) => C. SAlgorithm a -> TVar ChaChaDRG -> SMPClient -> IO (Maybe (C. AAuthKeyPair , C. PrivateKeyX25519 ), Maybe NewNtfCreds )
1418+ mkNtfCreds a g smp
1419+ | enableNtfs && thVersion (thParams smp) >= newNtfCredsSMPVersion = do
1420+ authKeys@ (k, _) <- atomically $ C. generateAuthKeyPair a g
1421+ (dhk, dhpk) <- atomically $ C. generateKeyPair g
1422+ pure (Just (authKeys, dhpk), Just $ NewNtfCreds k dhk)
1423+ | otherwise = pure (Nothing , Nothing )
1424+ mkClientNtfCreds :: Maybe (C. AAuthKeyPair , C. PrivateKeyX25519 ) -> Maybe ServerNtfCreds -> Maybe ClientNtfCreds
1425+ mkClientNtfCreds ntfKeys serverNtfCreds = case (ntfKeys, serverNtfCreds) of
1426+ (Just ((ntfPublicKey, ntfPrivateKey), dhpk), Just (ServerNtfCreds notifierId dhk')) ->
1427+ Just ClientNtfCreds {ntfPublicKey, ntfPrivateKey, notifierId, rcvNtfDhSecret = C. dh' dhk' dhpk}
1428+ _ -> Nothing
1429+ mkShortLinkCreds :: THandleParams SMPVersion 'TClient -> QueueIdsKeys -> AM (Maybe ShortLinkCreds )
1430+ mkShortLinkCreds thParams' QIK {sndId, queueMode, linkId} = case (cqrd, queueMode) of
14201431 (CQRMessaging ld, Just QMMessaging ) ->
14211432 withLinkData ld $ \ lnkId CQRData {linkKey, privSigKey, srvReq = (sndId', d)} ->
14221433 if sndId == sndId'
@@ -1807,7 +1818,7 @@ getQueueInfo c nm rq@RcvQueue {server, rcvId, rcvPrivateKey, sndId, status, clie
18071818 where
18081819 enc = decodeLatin1 . B64. encode . unEntityId
18091820
1810- agentNtfRegisterToken :: AgentClient -> NetworkRequestMode -> NtfToken -> NtfPublicAuthKey -> C. PublicKeyX25519 -> AM (NtfTokenId , C. PublicKeyX25519 )
1821+ agentNtfRegisterToken :: AgentClient -> NetworkRequestMode -> NtfToken -> SMP. NtfPublicAuthKey -> C. PublicKeyX25519 -> AM (NtfTokenId , C. PublicKeyX25519 )
18111822agentNtfRegisterToken c nm NtfToken {deviceToken, ntfServer, ntfPrivKey} ntfPubKey pubDhKey =
18121823 withClient c nm (0 , ntfServer, Nothing ) $ \ ntf -> ntfRegisterToken ntf nm ntfPrivKey (NewNtfTkn deviceToken ntfPubKey pubDhKey)
18131824
0 commit comments