From 469f84bb7407f78741561014d20247d8c5239d50 Mon Sep 17 00:00:00 2001 From: Efim Poberezkin Date: Sun, 14 Feb 2021 23:08:59 +0400 Subject: [PATCH] use cryptographic key pairs for encryption keys (#39) * use cryptographic key pairs for encryption keys * use speaking key types * fix key types Co-authored-by: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> --- src/Simplex/Messaging/Agent.hs | 29 +++++++++---------- src/Simplex/Messaging/Agent/Client.hs | 25 +++++++--------- src/Simplex/Messaging/Agent/Store.hs | 17 +++++------ src/Simplex/Messaging/Agent/Transmission.hs | 25 +++++++++------- src/Simplex/Messaging/Client.hs | 18 +++++++----- src/Simplex/Messaging/Crypto.hs | 1 + src/Simplex/Messaging/Protocol.hs | 4 +-- src/Simplex/Messaging/Server.hs | 4 +-- src/Simplex/Messaging/Server/QueueStore.hs | 10 +++---- .../Messaging/Server/QueueStore/STM.hs | 2 +- src/Simplex/Messaging/Types.hs | 10 ++++--- tests/AgentTests.hs | 2 +- tests/AgentTests/SQLiteTests.hs | 17 ++++++----- 13 files changed, 85 insertions(+), 79 deletions(-) diff --git a/src/Simplex/Messaging/Agent.hs b/src/Simplex/Messaging/Agent.hs index 3bd5b101f..fe97b5355 100644 --- a/src/Simplex/Messaging/Agent.hs +++ b/src/Simplex/Messaging/Agent.hs @@ -34,9 +34,8 @@ import Simplex.Messaging.Agent.Transmission import Simplex.Messaging.Client (SMPServerTransmission) import qualified Simplex.Messaging.Crypto as C import qualified Simplex.Messaging.Protocol as SMP -import Simplex.Messaging.Server (randomBytes) import Simplex.Messaging.Transport (putLn, runTCPServer) -import Simplex.Messaging.Types (CorrId (..), MsgBody, PrivateKey, SenderKey) +import Simplex.Messaging.Types (CorrId (..), MsgBody, SenderPublicKey) import System.IO (Handle) import UnliftIO.Async (race_) import UnliftIO.Exception (SomeException) @@ -135,9 +134,9 @@ processCommand c@AgentClient {sndQ} (corrId, connAlias, cmd) = joinConnection qInfo@(SMPQueueInfo srv _ _) replyMode = do -- TODO create connection alias if not passed -- make connAlias Maybe? - (sq, senderKey) <- newSendQueue qInfo connAlias + (sq, senderKey, verifyKey) <- newSendQueue qInfo connAlias withStore $ \st -> createSndConn st sq - connectToSendQueue c sq senderKey + connectToSendQueue c sq senderKey verifyKey case replyMode of ReplyOn -> sendReplyQInfo srv sq ReplyVia srv' -> sendReplyQInfo srv' sq @@ -241,9 +240,9 @@ processSMPTransmission c@AgentClient {sndQ} (srv, rId, cmd) = do REPLY qInfo -> do logServer "<--" c srv rId "MSG " -- TODO move senderKey inside SendQueue - (sq, senderKey) <- newSendQueue qInfo connAlias + (sq, senderKey, verifyKey) <- newSendQueue qInfo connAlias withStore $ \st -> upgradeRcvConnToDuplex st connAlias sq - connectToSendQueue c sq senderKey + connectToSendQueue c sq senderKey verifyKey notify connAlias CON sendAck c rq A_MSG body -> do @@ -269,25 +268,23 @@ processSMPTransmission c@AgentClient {sndQ} (srv, rId, cmd) = do notify :: ConnAlias -> ACommand 'Agent -> m () notify connAlias msg = atomically $ writeTBQueue sndQ ("", connAlias, msg) -connectToSendQueue :: AgentMonad m => AgentClient -> SendQueue -> SenderKey -> m () -connectToSendQueue c sq senderKey = do +connectToSendQueue :: AgentMonad m => AgentClient -> SendQueue -> SenderPublicKey -> VerificationKey -> m () +connectToSendQueue c sq senderKey verifyKey = do sendConfirmation c sq senderKey withStore $ \st -> setSndQueueStatus st sq Confirmed - sendHello c sq + sendHello c sq verifyKey withStore $ \st -> setSndQueueStatus st sq Active -decryptMessage :: MonadUnliftIO m => PrivateKey -> ByteString -> m ByteString +decryptMessage :: MonadUnliftIO m => DecryptionKey -> ByteString -> m ByteString decryptMessage _decryptKey = return newSendQueue :: - (MonadUnliftIO m, MonadReader Env m) => SMPQueueInfo -> ConnAlias -> m (SendQueue, SenderKey) + (MonadUnliftIO m, MonadReader Env m) => SMPQueueInfo -> ConnAlias -> m (SendQueue, SenderPublicKey, VerificationKey) newSendQueue (SMPQueueInfo smpServer senderId encryptKey) connAlias = do - g <- asks idsDrg size <- asks $ rsaKeySize . config (senderKey, sndPrivateKey) <- liftIO $ C.generateKeyPair size - verifyKey <- atomically $ randomBytes 16 g -- TODO replace with cryptographic key pair - let signKey = verifyKey - sndQueue = + (verifyKey, signKey) <- liftIO $ C.generateKeyPair size + let sndQueue = SendQueue { server = smpServer, sndId = senderId, @@ -297,4 +294,4 @@ newSendQueue (SMPQueueInfo smpServer senderId encryptKey) connAlias = do signKey, status = New } - return (sndQueue, senderKey) + return (sndQueue, senderKey, verifyKey) diff --git a/src/Simplex/Messaging/Agent/Client.hs b/src/Simplex/Messaging/Agent/Client.hs index ab3468ecc..bdbd36cee 100644 --- a/src/Simplex/Messaging/Agent/Client.hs +++ b/src/Simplex/Messaging/Agent/Client.hs @@ -49,8 +49,7 @@ import Simplex.Messaging.Agent.Transmission import Simplex.Messaging.Client import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Protocol (QueueId) -import Simplex.Messaging.Server (randomBytes) -import Simplex.Messaging.Types (ErrorType (AUTH), MsgBody, PrivateKey, PublicKey, SenderKey) +import Simplex.Messaging.Types (ErrorType (AUTH), MsgBody, SenderPublicKey) import UnliftIO.Concurrent import UnliftIO.Exception (IOException) import qualified UnliftIO.Exception as E @@ -150,15 +149,13 @@ withLogSMP c srv qId cmdStr action = do newReceiveQueue :: AgentMonad m => AgentClient -> SMPServer -> ConnAlias -> m (ReceiveQueue, SMPQueueInfo) newReceiveQueue c srv connAlias = do - g <- asks idsDrg size <- asks $ rsaKeySize . config (recipientKey, rcvPrivateKey) <- liftIO $ C.generateKeyPair size logServer "-->" c srv "" "NEW" (rcvId, sId) <- withSMP c srv $ \smp -> createSMPQueue smp rcvPrivateKey recipientKey logServer "<--" c srv "" $ B.unwords ["IDS", logSecret rcvId, logSecret sId] - encryptKey <- atomically $ randomBytes 16 g -- TODO replace with cryptographic key pair - let decryptKey = encryptKey - rq = + (encryptKey, decryptKey) <- liftIO $ C.generateKeyPair size + let rq = ReceiveQueue { server = srv, rcvId, @@ -211,7 +208,7 @@ showServer srv = B.pack $ host srv <> maybe "" (":" <>) (port srv) logSecret :: ByteString -> ByteString logSecret bs = encode $ B.take 3 bs -sendConfirmation :: forall m. AgentMonad m => AgentClient -> SendQueue -> SenderKey -> m () +sendConfirmation :: forall m. AgentMonad m => AgentClient -> SendQueue -> SenderPublicKey -> m () sendConfirmation c SendQueue {server, sndId} senderKey = do msg <- mkConfirmation withLogSMP c server sndId "SEND " $ \smp -> @@ -223,14 +220,14 @@ sendConfirmation c SendQueue {server, sndId} senderKey = do -- TODO encryption return msg -sendHello :: forall m. AgentMonad m => AgentClient -> SendQueue -> m () -sendHello c SendQueue {server, sndId, sndPrivateKey, encryptKey} = do - msg <- mkHello "5678" $ AckMode On -- TODO verifyKey +sendHello :: forall m. AgentMonad m => AgentClient -> SendQueue -> VerificationKey -> m () +sendHello c SendQueue {server, sndId, sndPrivateKey, encryptKey} verifyKey = do + msg <- mkHello $ AckMode On withLogSMP c server sndId "SEND (retrying)" $ send 20 msg where - mkHello :: PublicKey -> AckMode -> m ByteString - mkHello verifyKey ackMode = + mkHello :: AckMode -> m ByteString + mkHello ackMode = mkAgentMessage encryptKey $ HELLO verifyKey ackMode send :: Int -> ByteString -> SMPClient -> ExceptT SMPClientError IO () @@ -242,7 +239,7 @@ sendHello c SendQueue {server, sndId, sndPrivateKey, encryptKey} = do send (retry - 1) msg smp e -> throwE e -secureQueue :: AgentMonad m => AgentClient -> ReceiveQueue -> SenderKey -> m () +secureQueue :: AgentMonad m => AgentClient -> ReceiveQueue -> SenderPublicKey -> m () secureQueue c ReceiveQueue {server, rcvId, rcvPrivateKey} senderKey = withLogSMP c server rcvId "KEY " $ \smp -> secureSMPQueue smp rcvPrivateKey rcvId senderKey @@ -268,7 +265,7 @@ sendAgentMessage c SendQueue {server, sndId, sndPrivateKey, encryptKey} agentMsg withLogSMP c server sndId "SEND " $ \smp -> sendSMPMessage smp (Just sndPrivateKey) sndId msg -mkAgentMessage :: MonadUnliftIO m => PrivateKey -> AMessage -> m ByteString +mkAgentMessage :: MonadUnliftIO m => EncryptionKey -> AMessage -> m ByteString mkAgentMessage _encKey agentMessage = do senderTimestamp <- liftIO getCurrentTime let msg = diff --git a/src/Simplex/Messaging/Agent/Store.hs b/src/Simplex/Messaging/Agent/Store.hs index fcb2cd2ed..1623f4bab 100644 --- a/src/Simplex/Messaging/Agent/Store.hs +++ b/src/Simplex/Messaging/Agent/Store.hs @@ -23,19 +23,18 @@ import Data.Time.Clock (UTCTime) import Data.Type.Equality import Simplex.Messaging.Agent.Store.Types (ConnType (..)) import Simplex.Messaging.Agent.Transmission -import qualified Simplex.Messaging.Crypto as C import qualified Simplex.Messaging.Protocol as SMP -import Simplex.Messaging.Types (PrivateKey, PublicKey) +import Simplex.Messaging.Types (RecipientPrivateKey, SenderPrivateKey, SenderPublicKey) data ReceiveQueue = ReceiveQueue { server :: SMPServer, rcvId :: SMP.RecipientId, connAlias :: ConnAlias, - rcvPrivateKey :: C.PrivateKey, + rcvPrivateKey :: RecipientPrivateKey, sndId :: Maybe SMP.SenderId, - sndKey :: Maybe C.PublicKey, - decryptKey :: PrivateKey, - verifyKey :: Maybe PublicKey, + sndKey :: Maybe SenderPublicKey, + decryptKey :: DecryptionKey, + verifyKey :: Maybe VerificationKey, status :: QueueStatus } deriving (Eq, Show) @@ -44,9 +43,9 @@ data SendQueue = SendQueue { server :: SMPServer, sndId :: SMP.SenderId, connAlias :: ConnAlias, - sndPrivateKey :: C.PrivateKey, - encryptKey :: PublicKey, - signKey :: PrivateKey, + sndPrivateKey :: SenderPrivateKey, + encryptKey :: EncryptionKey, + signKey :: SignatureKey, status :: QueueStatus } deriving (Eq, Show) diff --git a/src/Simplex/Messaging/Agent/Transmission.hs b/src/Simplex/Messaging/Agent/Transmission.hs index f1a0545ea..16ac4dd7e 100644 --- a/src/Simplex/Messaging/Agent/Transmission.hs +++ b/src/Simplex/Messaging/Agent/Transmission.hs @@ -37,8 +37,7 @@ import Simplex.Messaging.Types Encoded, ErrorType, MsgBody, - PublicKey, - SenderKey, + SenderPublicKey, errMessageBody, ) import qualified Simplex.Messaging.Types as ST @@ -110,7 +109,7 @@ deriving instance Show (ACommand p) type Message = ByteString data SMPMessage - = SMPConfirmation SenderKey + = SMPConfirmation SenderPublicKey | SMPMessage { senderMsgId :: Integer, senderTimestamp :: UTCTime, @@ -162,7 +161,7 @@ agentMessageP = <|> "REPLY " *> reply <|> "MSG " *> a_msg where - hello = HELLO <$> base64P <*> ackMode + hello = HELLO <$> C.pubKeyP <*> ackMode reply = REPLY <$> smpQueueInfoP a_msg = do size :: Int <- A.decimal @@ -171,7 +170,7 @@ agentMessageP = smpQueueInfoP :: Parser SMPQueueInfo smpQueueInfoP = - "smp::" *> (SMPQueueInfo <$> smpServerP <* "::" <*> base64P <* "::" <*> base64P) + "smp::" *> (SMPQueueInfo <$> smpServerP <* "::" <*> base64P <* "::" <*> C.pubKeyP) smpServerP :: Parser SMPServer smpServerP = SMPServer <$> server <*> port <*> msgHash @@ -185,15 +184,17 @@ parseAgentMessage = parse agentMessageP $ SYNTAX errBadMessage serializeAgentMessage :: AMessage -> ByteString serializeAgentMessage = \case - HELLO verifyKey ackMode -> "HELLO " <> encode verifyKey <> if ackMode == AckMode Off then " NO_ACK" else "" + HELLO verifyKey ackMode -> "HELLO " <> C.serializePubKey verifyKey <> if ackMode == AckMode Off then " NO_ACK" else "" REPLY qInfo -> "REPLY " <> serializeSmpQueueInfo qInfo A_MSG body -> "MSG " <> serializeMsg body <> "\n" serializeSmpQueueInfo :: SMPQueueInfo -> ByteString -serializeSmpQueueInfo (SMPQueueInfo srv qId ek) = B.intercalate "::" ["smp", serializeServer srv, encode qId, encode ek] +serializeSmpQueueInfo (SMPQueueInfo srv qId ek) = + B.intercalate "::" ["smp", serializeServer srv, encode qId, C.serializePubKey ek] serializeServer :: SMPServer -> ByteString -serializeServer SMPServer {host, port, keyHash} = B.pack $ host <> maybe "" (':' :) port <> maybe "" (('#' :) . B.unpack) keyHash +serializeServer SMPServer {host, port, keyHash} = + B.pack $ host <> maybe "" (':' :) port <> maybe "" (('#' :) . B.unpack) keyHash data SMPServer = SMPServer { host :: HostName, @@ -217,9 +218,13 @@ data SMPQueueInfo = SMPQueueInfo SMPServer SMP.SenderId EncryptionKey data ReplyMode = ReplyOff | ReplyOn | ReplyVia SMPServer deriving (Eq, Show) -type EncryptionKey = PublicKey +type EncryptionKey = C.PublicKey -type VerificationKey = PublicKey +type DecryptionKey = C.PrivateKey + +type SignatureKey = C.PrivateKey + +type VerificationKey = C.PublicKey data QueueDirection = SND | RCV deriving (Show) diff --git a/src/Simplex/Messaging/Client.hs b/src/Simplex/Messaging/Client.hs index 27ec9c06b..cda565d8b 100644 --- a/src/Simplex/Messaging/Client.hs +++ b/src/Simplex/Messaging/Client.hs @@ -170,14 +170,18 @@ data SMPClientError | SMPClientError deriving (Eq, Show, Exception) -createSMPQueue :: SMPClient -> C.PrivateKey -> RecipientKey -> ExceptT SMPClientError IO (RecipientId, SenderId) +createSMPQueue :: + SMPClient -> + RecipientPrivateKey -> + RecipientPublicKey -> + ExceptT SMPClientError IO (RecipientId, SenderId) createSMPQueue c rpKey rKey = -- TODO add signing this request too - requires changes in the server sendSMPCommand c (Just rpKey) "" (Cmd SRecipient $ NEW rKey) >>= \case Cmd _ (IDS rId sId) -> return (rId, sId) _ -> throwE SMPUnexpectedResponse -subscribeSMPQueue :: SMPClient -> C.PrivateKey -> RecipientId -> ExceptT SMPClientError IO () +subscribeSMPQueue :: SMPClient -> RecipientPrivateKey -> RecipientId -> ExceptT SMPClientError IO () subscribeSMPQueue c@SMPClient {smpServer, msgQ} rpKey rId = sendSMPCommand c (Just rpKey) rId (Cmd SRecipient SUB) >>= \case Cmd _ OK -> return () @@ -185,16 +189,16 @@ subscribeSMPQueue c@SMPClient {smpServer, msgQ} rpKey rId = lift . atomically $ writeTBQueue msgQ (smpServer, rId, cmd) _ -> throwE SMPUnexpectedResponse -secureSMPQueue :: SMPClient -> C.PrivateKey -> RecipientId -> SenderKey -> ExceptT SMPClientError IO () +secureSMPQueue :: SMPClient -> RecipientPrivateKey -> RecipientId -> SenderPublicKey -> ExceptT SMPClientError IO () secureSMPQueue c rpKey rId senderKey = okSMPCommand (Cmd SRecipient $ KEY senderKey) c rpKey rId -sendSMPMessage :: SMPClient -> Maybe C.PrivateKey -> SenderId -> MsgBody -> ExceptT SMPClientError IO () +sendSMPMessage :: SMPClient -> Maybe SenderPrivateKey -> SenderId -> MsgBody -> ExceptT SMPClientError IO () sendSMPMessage c spKey sId msg = sendSMPCommand c spKey sId (Cmd SSender $ SEND msg) >>= \case Cmd _ OK -> return () _ -> throwE SMPUnexpectedResponse -ackSMPMessage :: SMPClient -> C.PrivateKey -> QueueId -> ExceptT SMPClientError IO () +ackSMPMessage :: SMPClient -> RecipientPrivateKey -> QueueId -> ExceptT SMPClientError IO () ackSMPMessage c@SMPClient {smpServer, msgQ} rpKey rId = sendSMPCommand c (Just rpKey) rId (Cmd SRecipient ACK) >>= \case Cmd _ OK -> return () @@ -202,10 +206,10 @@ ackSMPMessage c@SMPClient {smpServer, msgQ} rpKey rId = lift . atomically $ writeTBQueue msgQ (smpServer, rId, cmd) _ -> throwE SMPUnexpectedResponse -suspendSMPQueue :: SMPClient -> C.PrivateKey -> QueueId -> ExceptT SMPClientError IO () +suspendSMPQueue :: SMPClient -> RecipientPrivateKey -> QueueId -> ExceptT SMPClientError IO () suspendSMPQueue = okSMPCommand $ Cmd SRecipient OFF -deleteSMPQueue :: SMPClient -> C.PrivateKey -> QueueId -> ExceptT SMPClientError IO () +deleteSMPQueue :: SMPClient -> RecipientPrivateKey -> QueueId -> ExceptT SMPClientError IO () deleteSMPQueue = okSMPCommand $ Cmd SRecipient DEL okSMPCommand :: Cmd -> SMPClient -> C.PrivateKey -> QueueId -> ExceptT SMPClientError IO () diff --git a/src/Simplex/Messaging/Crypto.hs b/src/Simplex/Messaging/Crypto.hs index c1f47e7fc..d3c1b024f 100644 --- a/src/Simplex/Messaging/Crypto.hs +++ b/src/Simplex/Messaging/Crypto.hs @@ -87,6 +87,7 @@ generateKeyPair size = loop else return ( PublicKey pub, + -- TODO add comments explaining why we throw away public key from private PrivateKey {private_size = R.public_size pub, private_n = n, private_d = d} ) publicExponent = findPrimeFrom . (+ 3) <$> generateMax pubExpRange diff --git a/src/Simplex/Messaging/Protocol.hs b/src/Simplex/Messaging/Protocol.hs index 055064ed5..42849c3d6 100644 --- a/src/Simplex/Messaging/Protocol.hs +++ b/src/Simplex/Messaging/Protocol.hs @@ -65,9 +65,9 @@ type SenderId = QueueId type QueueId = Encoded data Command (a :: Party) where - NEW :: RecipientKey -> Command Recipient + NEW :: RecipientPublicKey -> Command Recipient SUB :: Command Recipient - KEY :: SenderKey -> Command Recipient + KEY :: SenderPublicKey -> Command Recipient ACK :: Command Recipient OFF :: Command Recipient DEL :: Command Recipient diff --git a/src/Simplex/Messaging/Server.hs b/src/Simplex/Messaging/Server.hs index 8322751e0..a6e240a23 100644 --- a/src/Simplex/Messaging/Server.hs +++ b/src/Simplex/Messaging/Server.hs @@ -105,7 +105,7 @@ verifyTransmission (sig, t@(corrId, queueId, cmd)) = do st <- asks queueStore qr <- atomically $ getQueue st party queueId return $ either smpErr f qr - verifySend :: C.Signature -> Maybe C.PublicKey -> Cmd + verifySend :: C.Signature -> Maybe SenderPublicKey -> Cmd verifySend "" = maybe cmd (const authErr) verifySend _ = maybe authErr verifySignature verifySignature :: C.PublicKey -> Cmd @@ -139,7 +139,7 @@ client clnt@Client {subscriptions, rcvQ, sndQ} Server {subscribedQ} = OFF -> okResp <$> atomically (suspendQueue st queueId) DEL -> delQueueAndMsgs st where - createQueue :: QueueStore -> RecipientKey -> m Transmission + createQueue :: QueueStore -> RecipientPublicKey -> m Transmission createQueue st rKey = mkResp corrId B.empty <$> addSubscribe where diff --git a/src/Simplex/Messaging/Server/QueueStore.hs b/src/Simplex/Messaging/Server/QueueStore.hs index dd515172b..d8c915488 100644 --- a/src/Simplex/Messaging/Server/QueueStore.hs +++ b/src/Simplex/Messaging/Server/QueueStore.hs @@ -11,21 +11,21 @@ import Simplex.Messaging.Types data QueueRec = QueueRec { recipientId :: QueueId, senderId :: QueueId, - recipientKey :: RecipientKey, - senderKey :: Maybe SenderKey, + recipientKey :: RecipientPublicKey, + senderKey :: Maybe SenderPublicKey, status :: QueueStatus } data QueueStatus = QueueActive | QueueOff class MonadQueueStore s m where - addQueue :: s -> RecipientKey -> (RecipientId, SenderId) -> m (Either ErrorType ()) + addQueue :: s -> RecipientPublicKey -> (RecipientId, SenderId) -> m (Either ErrorType ()) getQueue :: s -> SParty (a :: Party) -> QueueId -> m (Either ErrorType QueueRec) - secureQueue :: s -> RecipientId -> SenderKey -> m (Either ErrorType ()) + secureQueue :: s -> RecipientId -> SenderPublicKey -> m (Either ErrorType ()) suspendQueue :: s -> RecipientId -> m (Either ErrorType ()) deleteQueue :: s -> RecipientId -> m (Either ErrorType ()) -mkQueueRec :: RecipientKey -> (RecipientId, SenderId) -> QueueRec +mkQueueRec :: RecipientPublicKey -> (RecipientId, SenderId) -> QueueRec mkQueueRec recipientKey (recipientId, senderId) = QueueRec { recipientId, diff --git a/src/Simplex/Messaging/Server/QueueStore/STM.hs b/src/Simplex/Messaging/Server/QueueStore/STM.hs index be5b8d39e..4055fc3a0 100644 --- a/src/Simplex/Messaging/Server/QueueStore/STM.hs +++ b/src/Simplex/Messaging/Server/QueueStore/STM.hs @@ -29,7 +29,7 @@ newQueueStore :: STM QueueStore newQueueStore = newTVar QueueStoreData {queues = M.empty, senders = M.empty} instance MonadQueueStore QueueStore STM where - addQueue :: QueueStore -> RecipientKey -> (RecipientId, SenderId) -> STM (Either ErrorType ()) + addQueue :: QueueStore -> RecipientPublicKey -> (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 diff --git a/src/Simplex/Messaging/Types.hs b/src/Simplex/Messaging/Types.hs index 534460ef9..40b085160 100644 --- a/src/Simplex/Messaging/Types.hs +++ b/src/Simplex/Messaging/Types.hs @@ -20,13 +20,15 @@ newtype CorrId = CorrId {bs :: ByteString} deriving (Eq, Ord, Show) instance IsString CorrId where fromString = CorrId . fromString -type PublicKey = Encoded +-- only used by Agent, kept here so its definition is close to respective public key +type RecipientPrivateKey = C.PrivateKey -type PrivateKey = Encoded +type RecipientPublicKey = C.PublicKey -type RecipientKey = C.PublicKey +-- only used by Agent, kept here so its definition is close to respective public key +type SenderPrivateKey = C.PrivateKey -type SenderKey = C.PublicKey +type SenderPublicKey = C.PublicKey type MsgId = Encoded diff --git a/tests/AgentTests.hs b/tests/AgentTests.hs index adedb24ea..f2f2fc933 100644 --- a/tests/AgentTests.hs +++ b/tests/AgentTests.hs @@ -141,7 +141,7 @@ syntaxTests = do it "invalid server keyHash" $ ("223", "", "NEW localhost:5000#1") >#> ("223", "", "ERR SYNTAX 11") describe "JOIN" do - describe "valid" do + xdescribe "valid" do -- TODO: ERROR no connection alias in the response (it does not generate it yet if not provided) -- TODO: add tests with defined connection alias it "using same server as in invitation" $ diff --git a/tests/AgentTests/SQLiteTests.hs b/tests/AgentTests/SQLiteTests.hs index c050c36ea..300c77ebc 100644 --- a/tests/AgentTests/SQLiteTests.hs +++ b/tests/AgentTests/SQLiteTests.hs @@ -6,6 +6,7 @@ module AgentTests.SQLiteTests (storeTests) where import Control.Monad.Except (ExceptT, runExceptT) +import qualified Crypto.PubKey.RSA as R import Data.Word (Word32) import qualified Database.SQLite.Simple as DB import Database.SQLite.Simple.QQ (sql) @@ -92,7 +93,7 @@ rcvQueue1 = rcvPrivateKey = C.PrivateKey 1 2 3, sndId = Just "2345", sndKey = Nothing, - decryptKey = "dcba", + decryptKey = C.PrivateKey 1 2 3, verifyKey = Nothing, status = New } @@ -104,8 +105,8 @@ sndQueue1 = sndId = "3456", connAlias = "conn1", sndPrivateKey = C.PrivateKey 1 2 3, - encryptKey = "dcba", - signKey = "edcb", + encryptKey = C.PublicKey $ R.PublicKey 1 2 3, + signKey = C.PrivateKey 1 2 3, status = New } @@ -207,8 +208,8 @@ testUpgradeRcvConnToDuplex = do sndId = "2345", connAlias = "conn1", sndPrivateKey = C.PrivateKey 1 2 3, - encryptKey = "dcba", - signKey = "edcb", + encryptKey = C.PublicKey $ R.PublicKey 1 2 3, + signKey = C.PrivateKey 1 2 3, status = New } upgradeRcvConnToDuplex store "conn1" anotherSndQueue @@ -231,7 +232,7 @@ testUpgradeSndConnToDuplex = do rcvPrivateKey = C.PrivateKey 1 2 3, sndId = Just "4567", sndKey = Nothing, - decryptKey = "dcba", + decryptKey = C.PrivateKey 1 2 3, verifyKey = Nothing, status = New } @@ -326,7 +327,7 @@ testCreateMsgHello = do it "should create a HELLO message" $ \store -> do createRcvConn store rcvQueue1 `returnsResult` () - let verificationKey = "abcd" + let verificationKey = C.PublicKey $ R.PublicKey 1 2 3 let am = AckMode On let msg = HELLO verificationKey am let msgId = 1 @@ -341,7 +342,7 @@ testCreateMsgReply = do `returnsResult` () let smpServer = SMPServer "smp.simplex.im" (Just "5223") (Just "1234") let senderId = "sender1" - let encryptionKey = "abcd" + let encryptionKey = C.PublicKey $ R.PublicKey 1 2 3 let msg = REPLY $ SMPQueueInfo smpServer senderId encryptionKey let msgId = 1 -- TODO getMsg to check message