From e2ec737c683c127f0757d5b21c2f76e2e862f0d9 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin Date: Sun, 25 Feb 2024 16:19:04 +0000 Subject: [PATCH] stop using public keys from table (#1014) * stop using public keys from table * comment --- src/Simplex/Messaging/Agent.hs | 18 ++++++------ src/Simplex/Messaging/Agent/Client.hs | 1 - src/Simplex/Messaging/Agent/Store/SQLite.hs | 31 ++++----------------- 3 files changed, 15 insertions(+), 35 deletions(-) diff --git a/src/Simplex/Messaging/Agent.hs b/src/Simplex/Messaging/Agent.hs index 63d8f33ca..d6d1e9800 100644 --- a/src/Simplex/Messaging/Agent.hs +++ b/src/Simplex/Messaging/Agent.hs @@ -1387,11 +1387,11 @@ synchronizeRatchet' c connId force = withConnLock c connId "synchronizeRatchet" -- check queues are not switching? AgentConfig {e2eEncryptVRange} <- asks config g <- asks random - (pk1, pk2, e2eParams@(CR.E2ERatchetParams _ k1 k2)) <- atomically . CR.generateE2EParams g $ maxVersion e2eEncryptVRange + (pk1, pk2, e2eParams) <- atomically . CR.generateE2EParams g $ maxVersion e2eEncryptVRange enqueueRatchetKeyMsgs c cData sqs e2eParams withStore' c $ \db -> do setConnRatchetSync db connId RSStarted - setRatchetX3dhKeys db connId pk1 pk2 k1 k2 + setRatchetX3dhKeys db connId pk1 pk2 let cData' = cData {ratchetSyncState = RSStarted} :: ConnData conn' = DuplexConnection cData' rqs sqs pure $ connectionStats conn' @@ -2300,12 +2300,12 @@ processSMPTransmission c@AgentClient {smpClients, subQ} (tSess@(_, srv, _), _v, exists <- checkRatchetKeyHashExists db connId rkHashRcv unless exists $ addProcessedRatchetKeyHash db connId rkHashRcv pure exists - getSendRatchetKeys :: m (C.PrivateKeyX448, C.PrivateKeyX448, C.PublicKeyX448, C.PublicKeyX448) + getSendRatchetKeys :: m (C.PrivateKeyX448, C.PrivateKeyX448) getSendRatchetKeys = case rss of RSOk -> sendReplyKey -- receiving client RSAllowed -> sendReplyKey RSRequired -> sendReplyKey - RSStarted -> withStore c (`getRatchetX3dhKeys'` connId) -- initiating client + RSStarted -> withStore c (`getRatchetX3dhKeys` connId) -- initiating client RSAgreed -> do withStore' c $ \db -> setConnRatchetSync db connId RSRequired notifyRatchetSyncError @@ -2316,9 +2316,9 @@ processSMPTransmission c@AgentClient {smpClients, subQ} (tSess@(_, srv, _), _v, where sendReplyKey = do g <- asks random - (pk1, pk2, e2eParams@(CR.E2ERatchetParams _ k1 k2)) <- atomically . CR.generateE2EParams g $ version e2eOtherPartyParams + (pk1, pk2, e2eParams) <- atomically . CR.generateE2EParams g $ version e2eOtherPartyParams enqueueRatchetKeyMsgs c cData' sqs e2eParams - pure (pk1, pk2, k1, k2) + pure (pk1, pk2) notifyRatchetSyncError = do let cData'' = cData' {ratchetSyncState = RSRequired} :: ConnData conn'' = updateConnection cData'' conn' @@ -2335,9 +2335,9 @@ processSMPTransmission c@AgentClient {smpClients, subQ} (tSess@(_, srv, _), _v, createRatchet db connId rc -- compare public keys `k1` in AgentRatchetKey messages sent by self and other party -- to determine ratchet initilization ordering - initRatchet :: VersionRange -> (C.PrivateKeyX448, C.PrivateKeyX448, C.PublicKeyX448, C.PublicKeyX448) -> m () - initRatchet e2eEncryptVRange (pk1, pk2, k1, k2) - | rkHash k1 k2 <= rkHashRcv = do + initRatchet :: VersionRange -> (C.PrivateKeyX448, C.PrivateKeyX448) -> m () + initRatchet e2eEncryptVRange (pk1, pk2) + | rkHash (C.publicKey pk1) (C.publicKey pk2) <= rkHashRcv = do recreateRatchet $ CR.initRcvRatchet e2eEncryptVRange pk2 $ CR.x3dhRcv pk1 pk2 e2eOtherPartyParams | otherwise = do (_, rcDHRs) <- atomically . C.generateKeyPair =<< asks random diff --git a/src/Simplex/Messaging/Agent/Client.hs b/src/Simplex/Messaging/Agent/Client.hs index 46b0954ed..b5491a6ed 100644 --- a/src/Simplex/Messaging/Agent/Client.hs +++ b/src/Simplex/Messaging/Agent/Client.hs @@ -138,7 +138,6 @@ import Control.Monad.Except import Control.Monad.IO.Unlift import Control.Monad.Reader import Crypto.Random (ChaChaDRG) -import Data.Aeson ((.:), (.=)) import qualified Data.Aeson as J import qualified Data.Aeson.TH as J import Data.Bifunctor (bimap, first, second) diff --git a/src/Simplex/Messaging/Agent/Store/SQLite.hs b/src/Simplex/Messaging/Agent/Store/SQLite.hs index e9aca0b39..c82e91d9f 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite.hs @@ -123,8 +123,6 @@ module Simplex.Messaging.Agent.Store.SQLite -- Double ratchet persistence createRatchetX3dhKeys, getRatchetX3dhKeys, - createRatchetX3dhKeys', - getRatchetX3dhKeys', setRatchetX3dhKeys, createRatchet, deleteRatchet, @@ -1191,26 +1189,11 @@ getRatchetX3dhKeys db connId = Right (Just k1, Just k2) -> Right (k1, k2) _ -> Left SEX3dhKeysNotFound -createRatchetX3dhKeys' :: DB.Connection -> ConnId -> C.PrivateKeyX448 -> C.PrivateKeyX448 -> C.PublicKeyX448 -> C.PublicKeyX448 -> IO () -createRatchetX3dhKeys' db connId x3dhPrivKey1 x3dhPrivKey2 x3dhPubKey1 x3dhPubKey2 = - DB.execute - db - "INSERT INTO ratchets (conn_id, x3dh_priv_key_1, x3dh_priv_key_2, x3dh_pub_key_1, x3dh_pub_key_2) VALUES (?,?,?,?,?)" - (connId, x3dhPrivKey1, x3dhPrivKey2, x3dhPubKey1, x3dhPubKey2) - -getRatchetX3dhKeys' :: DB.Connection -> ConnId -> IO (Either StoreError (C.PrivateKeyX448, C.PrivateKeyX448, C.PublicKeyX448, C.PublicKeyX448)) -getRatchetX3dhKeys' db connId = - fmap hasKeys $ - firstRow id SEX3dhKeysNotFound $ - DB.query db "SELECT x3dh_priv_key_1, x3dh_priv_key_2, x3dh_pub_key_1, x3dh_pub_key_2 FROM ratchets WHERE conn_id = ?" (Only connId) - where - hasKeys = \case - Right (Just pk1, Just pk2, Just k1, Just k2) -> Right (pk1, pk2, k1, k2) - _ -> Left SEX3dhKeysNotFound - -- used to remember new keys when starting ratchet re-synchronization -setRatchetX3dhKeys :: DB.Connection -> ConnId -> C.PrivateKeyX448 -> C.PrivateKeyX448 -> C.PublicKeyX448 -> C.PublicKeyX448 -> IO () -setRatchetX3dhKeys db connId x3dhPrivKey1 x3dhPrivKey2 x3dhPubKey1 x3dhPubKey2 = +-- TODO remove the columns for public keys in v5.7. +-- Currently, the keys are not used but still stored to support app downgrade to the previous version. +setRatchetX3dhKeys :: DB.Connection -> ConnId -> C.PrivateKeyX448 -> C.PrivateKeyX448 -> IO () +setRatchetX3dhKeys db connId x3dhPrivKey1 x3dhPrivKey2 = DB.execute db [sql| @@ -1218,7 +1201,7 @@ setRatchetX3dhKeys db connId x3dhPrivKey1 x3dhPrivKey2 x3dhPubKey1 x3dhPubKey2 = SET x3dh_priv_key_1 = ?, x3dh_priv_key_2 = ?, x3dh_pub_key_1 = ?, x3dh_pub_key_2 = ? WHERE conn_id = ? |] - (x3dhPrivKey1, x3dhPrivKey2, x3dhPubKey1, x3dhPubKey2, connId) + (x3dhPrivKey1, x3dhPrivKey2, C.publicKey x3dhPrivKey1, C.publicKey x3dhPrivKey2, connId) createRatchet :: DB.Connection -> ConnId -> RatchetX448 -> IO () createRatchet db connId rc = @@ -1230,9 +1213,7 @@ createRatchet db connId rc = ON CONFLICT (conn_id) DO UPDATE SET ratchet_state = :ratchet_state, x3dh_priv_key_1 = NULL, - x3dh_priv_key_2 = NULL, - x3dh_pub_key_1 = NULL, - x3dh_pub_key_2 = NULL + x3dh_priv_key_2 = NULL |] [":conn_id" := connId, ":ratchet_state" := rc]