summaryrefslogtreecommitdiff
path: root/src/Network/Tox/Onion/Transport.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Network/Tox/Onion/Transport.hs')
-rw-r--r--src/Network/Tox/Onion/Transport.hs33
1 files changed, 23 insertions, 10 deletions
diff --git a/src/Network/Tox/Onion/Transport.hs b/src/Network/Tox/Onion/Transport.hs
index 3e3596a6..34ba23f6 100644
--- a/src/Network/Tox/Onion/Transport.hs
+++ b/src/Network/Tox/Onion/Transport.hs
@@ -598,18 +598,31 @@ instance Sized OnionData where
598 ConstSize n -> n 598 ConstSize n -> n
599 VarSize f -> f req 599 VarSize f -> f req
600 600
601encrypt :: TransportCrypto -> OnionMessage Identity -> OnionDestination r -> (OnionMessage Encrypted, OnionDestination r) 601
602encrypt crypto msg rpath = ( transcode ( (. (runIdentity . either id assymData)) 602selectKey :: TransportCrypto -> OnionMessage Identity -> OnionDestination r -> IO (SecretKey, PublicKey)
603 . encryptMessage skey okey) 603selectKey crypto
604 msg 604 (OnionAnnounce a@Assym { assymData = Identity (AnnounceRequest _ pkey akey, _) })
605 , rpath) 605 rpath
606 where 606 | (akey /= zeroID) = atomically $ do
607 skey = fst $ aliasKey crypto rpath 607 ks <- filter (\(sk,pk) -> pk == id2key pkey)
608 <$> readTVar (userKeys crypto)
609 maybe (return $ aliasKey crypto rpath)
610 return
611 (listToMaybe ks)
612selectKey crypto msg rpath = return $ aliasKey crypto rpath
613
614encrypt :: TransportCrypto -> OnionMessage Identity -> OnionDestination r -> IO (OnionMessage Encrypted, OnionDestination r)
615encrypt crypto msg rpath = do
616 (skey,pkey) <- selectKey crypto msg rpath
617 let skey = fst $ aliasKey crypto rpath
608 618
609 -- The OnionToMe case shouldn't happen, but we'll use our own public 619 -- The OnionToMe case shouldn't happen, but we'll use our own public
610 -- key in this situation. 620 -- key in this situation.
611 okey = fromMaybe (transportPublic crypto) $ onionKey rpath 621 okey = fromMaybe (transportPublic crypto) $ onionKey rpath
612 622 return ( transcode ( (. (runIdentity . either id assymData))
623 . encryptMessage skey okey)
624 msg
625 , rpath)
613 626
614encryptMessage :: Serialize a => 627encryptMessage :: Serialize a =>
615 SecretKey -> PublicKey -> Nonce24 -> a -> Encrypted a 628 SecretKey -> PublicKey -> Nonce24 -> a -> Encrypted a
@@ -618,8 +631,8 @@ encryptMessage skey destKey n a = ToxCrypto.encrypt secret plain
618 secret = computeSharedSecret skey destKey n 631 secret = computeSharedSecret skey destKey n
619 plain = encodePlain a 632 plain = encodePlain a
620 633
621decrypt :: TransportCrypto -> OnionMessage Encrypted -> OnionDestination r -> Either String (OnionMessage Identity, OnionDestination r) 634decrypt :: TransportCrypto -> OnionMessage Encrypted -> OnionDestination r -> IO (Either String (OnionMessage Identity, OnionDestination r))
622decrypt crypto msg addr = do 635decrypt crypto msg addr = return $ do
623 msg <- sequenceMessage $ transcode (\n -> decryptMessage (aliasKey crypto addr) n . left (senderkey addr)) msg 636 msg <- sequenceMessage $ transcode (\n -> decryptMessage (aliasKey crypto addr) n . left (senderkey addr)) msg
624 Right (msg, addr) 637 Right (msg, addr)
625 638