From bfa05c943266a8377cca81c44c581cd018c62394 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Sun, 12 Dec 2021 21:17:25 +0000 Subject: [PATCH] all tests pass! --- migrations/20210101_initial.sql | 5 ++++- migrations/20211212_rcv_queue_keys.sql | 3 --- simplexmq.cabal | 1 - src/Simplex/Messaging/Agent/Client.hs | 2 +- src/Simplex/Messaging/Agent/Store.hs | 2 +- src/Simplex/Messaging/Crypto.hs | 12 ++++++++---- src/Simplex/Messaging/Server.hs | 2 +- src/Simplex/Messaging/Server/QueueStore.hs | 14 +------------- src/Simplex/Messaging/Server/QueueStore/STM.hs | 17 ++--------------- tests/AgentTests/SQLiteTests.hs | 17 ++++++++++++++++- tests/Test.hs | 2 +- 11 files changed, 35 insertions(+), 42 deletions(-) delete mode 100644 migrations/20211212_rcv_queue_keys.sql diff --git a/migrations/20210101_initial.sql b/migrations/20210101_initial.sql index 4c1e617c9..1c89060cf 100644 --- a/migrations/20210101_initial.sql +++ b/migrations/20210101_initial.sql @@ -11,8 +11,11 @@ CREATE TABLE IF NOT EXISTS rcv_queues( rcv_id BLOB NOT NULL, conn_alias BLOB NOT NULL, rcv_private_key BLOB NOT NULL, - snd_id BLOB, + rcv_srv_verify_key BLOB NOT NULL, + rcv_dh_secret BLOB NOT NULL, + snd_id BLOB NOT NULL, snd_key BLOB, + snd_srv_verify_key BLOB NOT NULL, decrypt_key BLOB NOT NULL, verify_key BLOB, status TEXT NOT NULL, diff --git a/migrations/20211212_rcv_queue_keys.sql b/migrations/20211212_rcv_queue_keys.sql deleted file mode 100644 index b6c4c6469..000000000 --- a/migrations/20211212_rcv_queue_keys.sql +++ /dev/null @@ -1,3 +0,0 @@ -ALTER TABLE rcv_queues ADD rcv_srv_verify_key BLOB NOT NULL; -ALTER TABLE rcv_queues ADD rcv_dh_secret BLOB NOT NULL; -ALTER TABLE rcv_queues ADD snd_srv_verify_key BLOB NOT NULL; diff --git a/simplexmq.cabal b/simplexmq.cabal index a1ac62d57..b50e86e7f 100644 --- a/simplexmq.cabal +++ b/simplexmq.cabal @@ -30,7 +30,6 @@ extra-source-files: migrations/20210624_confirmations.sql migrations/20210809_snd_messages.sql migrations/20211202_connection_mode.sql - migrations/20211212_rcv_queue_keys.sql migrations/README.md library diff --git a/src/Simplex/Messaging/Agent/Client.hs b/src/Simplex/Messaging/Agent/Client.hs index a980a321c..aedb5afda 100644 --- a/src/Simplex/Messaging/Agent/Client.hs +++ b/src/Simplex/Messaging/Agent/Client.hs @@ -253,7 +253,7 @@ newRcvQueue_ a c srv = do rcvSrvVerifyKey, rcvDhSecret, sndId = Just sndId, - sndSrvVerifyKey = Just sndSrvVerifyKey, + sndSrvVerifyKey = sndSrvVerifyKey, decryptKey, verifyKey = Nothing, status = New diff --git a/src/Simplex/Messaging/Agent/Store.hs b/src/Simplex/Messaging/Agent/Store.hs index 2201bb219..ca0703153 100644 --- a/src/Simplex/Messaging/Agent/Store.hs +++ b/src/Simplex/Messaging/Agent/Store.hs @@ -87,7 +87,7 @@ data RcvQueue = RcvQueue -- | sender queue ID sndId :: Maybe SMP.SenderId, -- | key used by the sender to sign transmissions - sndSrvVerifyKey :: Maybe SndPublicVerifyKey, + sndSrvVerifyKey :: SndPublicVerifyKey, -- | TODO keys used for E2E encryption - these will change with double ratchet decryptKey :: C.APrivateDecryptKey, verifyKey :: Maybe C.APublicVerifyKey, diff --git a/src/Simplex/Messaging/Crypto.hs b/src/Simplex/Messaging/Crypto.hs index d1817f1f3..9f9a1b468 100644 --- a/src/Simplex/Messaging/Crypto.hs +++ b/src/Simplex/Messaging/Crypto.hs @@ -76,6 +76,7 @@ module Simplex.Messaging.Crypto -- * DH derivation dh', + dhSecret, -- * AES256 AEAD-GCM scheme Key (..), @@ -372,14 +373,17 @@ class CryptoDhSecret s where strDhSecretP :: Parser s dhSecretP :: Parser s +instance AlgorithmI a => IsString (DhSecret a) where + fromString = parseString $ dhSecret >=> dhSecret' + instance CryptoDhSecret ADhSecret where serializeDhSecret (ADhSecret _ s) = serializeDhSecret s dhSecretBytes (ADhSecret _ s) = dhSecretBytes s - strDhSecretP = dhSecret_ <$?> base64P - dhSecretP = dhSecret_ <$?> A.takeByteString + strDhSecretP = dhSecret <$?> base64P + dhSecretP = dhSecret <$?> A.takeByteString -dhSecret_ :: ByteString -> Either String ADhSecret -dhSecret_ = cryptoPassed . secret +dhSecret :: ByteString -> Either String ADhSecret +dhSecret = cryptoPassed . secret where secret bs | B.length bs == x25519_size = ADhSecret SX25519 . DhSecretX25519 <$> X25519.dhSecret bs diff --git a/src/Simplex/Messaging/Server.hs b/src/Simplex/Messaging/Server.hs index c6b678f7d..5e1545bf6 100644 --- a/src/Simplex/Messaging/Server.hs +++ b/src/Simplex/Messaging/Server.hs @@ -271,7 +271,7 @@ client clnt@Client {subscriptions, ntfSubscriptions, rcvQ, sndQ = sndQ'} Server addQueueRetry n qik qRec = do ids@(rId, _) <- getIds -- create QueueRec record with these ids and keys - atomically (addQueue' st $ qRec ids) >>= \case + atomically (addQueue st $ qRec ids) >>= \case Left DUPLICATE_ -> addQueueRetry (n - 1) qik qRec Left e -> pure $ ERR e Right _ -> do diff --git a/src/Simplex/Messaging/Server/QueueStore.hs b/src/Simplex/Messaging/Server/QueueStore.hs index 64dd78fc0..d7dc8035e 100644 --- a/src/Simplex/Messaging/Server/QueueStore.hs +++ b/src/Simplex/Messaging/Server/QueueStore.hs @@ -22,21 +22,9 @@ data QueueRec = QueueRec data QueueStatus = QueueActive | QueueOff deriving (Eq) class MonadQueueStore s m where - addQueue :: s -> RcvPublicVerifyKey -> (RecipientId, SenderId) -> m (Either ErrorType ()) - addQueue' :: s -> QueueRec -> m (Either ErrorType ()) + addQueue :: s -> QueueRec -> m (Either ErrorType ()) getQueue :: s -> SParty (a :: Party) -> QueueId -> m (Either ErrorType QueueRec) secureQueue :: s -> RecipientId -> SndPublicVerifyKey -> m (Either ErrorType ()) addQueueNotifier :: s -> RecipientId -> NotifierId -> NtfPublicVerifyKey -> m (Either ErrorType ()) suspendQueue :: s -> RecipientId -> m (Either ErrorType ()) deleteQueue :: s -> RecipientId -> m (Either ErrorType ()) - -mkQueueRec :: RcvPublicVerifyKey -> (RecipientId, SenderId) -> QueueRec -mkQueueRec recipientKey (recipientId, senderId) = - QueueRec - { recipientId, - senderId, - recipientKey, - senderKey = Nothing, - notifier = Nothing, - status = QueueActive - } diff --git a/src/Simplex/Messaging/Server/QueueStore/STM.hs b/src/Simplex/Messaging/Server/QueueStore/STM.hs index 64758f61e..b6464fff0 100644 --- a/src/Simplex/Messaging/Server/QueueStore/STM.hs +++ b/src/Simplex/Messaging/Server/QueueStore/STM.hs @@ -29,21 +29,8 @@ newQueueStore :: STM QueueStore newQueueStore = newTVar QueueStoreData {queues = M.empty, senders = M.empty, notifiers = M.empty} instance MonadQueueStore QueueStore STM where - addQueue :: QueueStore -> RcvPublicVerifyKey -> (RecipientId, SenderId) -> STM (Either ErrorType ()) - addQueue store rKey ids@(rId, sId) = do - cs@QueueStoreData {queues, senders} <- readTVar store - if M.member rId queues || M.member sId senders - then return $ Left DUPLICATE_ - else do - writeTVar store $ - cs - { queues = M.insert rId (mkQueueRec rKey ids) queues, - senders = M.insert sId rId senders - } - return $ Right () - - addQueue' :: QueueStore -> QueueRec -> STM (Either ErrorType ()) - addQueue' store qRec@QueueRec {recipientId = rId, senderId = sId} = do + addQueue :: QueueStore -> QueueRec -> STM (Either ErrorType ()) + addQueue store qRec@QueueRec {recipientId = rId, senderId = sId} = do cs@QueueStoreData {queues, senders} <- readTVar store if M.member rId queues || M.member sId senders then return $ Left DUPLICATE_ diff --git a/tests/AgentTests/SQLiteTests.hs b/tests/AgentTests/SQLiteTests.hs index 4662accee..4b4c8cf5c 100644 --- a/tests/AgentTests/SQLiteTests.hs +++ b/tests/AgentTests/SQLiteTests.hs @@ -152,11 +152,20 @@ cData1 = ConnData {connId = "conn1"} testPrivateSignKey :: C.APrivateSignKey testPrivateSignKey = C.APrivateSignKey C.SRSA testPrivateKey +testPublicVerifyKey :: C.APublicVerifyKey +testPublicVerifyKey = C.APublicVerifyKey C.SRSA testPublicKey + testPrivateDecryptKey :: C.APrivateDecryptKey testPrivateDecryptKey = C.APrivateDecryptKey C.SRSA testPrivateKey testPublicEncryptKey :: C.APublicEncryptKey -testPublicEncryptKey = C.APublicEncryptKey C.SRSA $ C.PublicKeyRSA $ R.PublicKey 1 2 3 +testPublicEncryptKey = C.APublicEncryptKey C.SRSA testPublicKey + +testPublicKey :: C.PublicKey 'C.RSA +testPublicKey = C.PublicKeyRSA $ R.PublicKey 1 2 3 + +testDhSecret :: C.DhSecret 'C.X25519 +testDhSecret = "01234567890123456789012345678901" testPrivateKey :: C.PrivateKey 'C.RSA testPrivateKey = @@ -182,7 +191,10 @@ rcvQueue1 = { server = SMPServer "smp.simplex.im" (Just "5223") testKeyHash, rcvId = "1234", rcvPrivateKey = testPrivateSignKey, + rcvSrvVerifyKey = testPublicVerifyKey, + rcvDhSecret = testDhSecret, sndId = Just "2345", + sndSrvVerifyKey = testPublicVerifyKey, decryptKey = testPrivateDecryptKey, verifyKey = Nothing, status = New @@ -354,7 +366,10 @@ testUpgradeSndConnToDuplex = { server = SMPServer "smp.simplex.im" (Just "5223") testKeyHash, rcvId = "3456", rcvPrivateKey = testPrivateSignKey, + rcvSrvVerifyKey = testPublicVerifyKey, + rcvDhSecret = testDhSecret, sndId = Just "4567", + sndSrvVerifyKey = testPublicVerifyKey, decryptKey = testPrivateDecryptKey, verifyKey = Nothing, status = New diff --git a/tests/Test.hs b/tests/Test.hs index 05c47c53f..b27b86d59 100644 --- a/tests/Test.hs +++ b/tests/Test.hs @@ -15,5 +15,5 @@ main = do describe "Protocol errors" protocolErrorTests describe "SMP server via TCP" $ serverTests (transport @TCP) describe "SMP server via WebSockets" $ serverTests (transport @WS) - xdescribe "SMP client agent" $ agentTests (transport @TCP) + describe "SMP client agent" $ agentTests (transport @TCP) removeDirectoryRecursive "tests/tmp"