summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/Network/Tox.hs17
-rw-r--r--src/Network/Tox/Crypto/Handlers.hs41
-rw-r--r--src/Network/Tox/Crypto/Transport.hs14
3 files changed, 24 insertions, 48 deletions
diff --git a/src/Network/Tox.hs b/src/Network/Tox.hs
index 8b966266..7ee4dbe0 100644
--- a/src/Network/Tox.hs
+++ b/src/Network/Tox.hs
@@ -97,7 +97,7 @@ import Crypto.Tox
97import Data.Word64Map (fitsInInt) 97import Data.Word64Map (fitsInInt)
98import qualified Data.Word64Map (empty) 98import qualified Data.Word64Map (empty)
99import Network.Kademlia.Bootstrap (forkPollForRefresh, bootstrap) 99import Network.Kademlia.Bootstrap (forkPollForRefresh, bootstrap)
100import Network.Tox.Crypto.Transport (NetCrypto(..), CryptoMessage, HandshakeData(..), Handshake(..),CryptoPacket) 100import Network.Tox.Crypto.Transport (CryptoMessage, HandshakeData(..), Handshake(..),CryptoPacket)
101import Network.Tox.Crypto.Handlers 101import Network.Tox.Crypto.Handlers
102import qualified Network.Tox.DHT.Handlers as DHT 102import qualified Network.Tox.DHT.Handlers as DHT
103import qualified Network.Tox.DHT.Transport as DHT 103import qualified Network.Tox.DHT.Transport as DHT
@@ -441,17 +441,10 @@ newTox keydb addr mbSessionsState suppliedDHTKey = do
441 let ignoreErrors _ = return () -- Set this to (hPutStrLn stderr) to debug onion route building. 441 let ignoreErrors _ = return () -- Set this to (hPutStrLn stderr) to debug onion route building.
442 orouter <- newOnionRouter ignoreErrors 442 orouter <- newOnionRouter ignoreErrors
443 (cryptonet,dhtcrypt,onioncrypt,dtacrypt,handshakes) <- toxTransport crypto orouter lookupClose udp 443 (cryptonet,dhtcrypt,onioncrypt,dtacrypt,handshakes) <- toxTransport crypto orouter lookupClose udp
444 let sessionsState = sessionsState0 { sessionTransport = nc_combo 444
445 , transportCrypto = crypto } 445 let sessionsState = sessionsState0 { sendHandshake = sendMessage handshakes
446 where 446 , sendSessionPacket = sendMessage cryptonet
447 -- TODO: This send-only transport is obviated by it's two components. 447 , transportCrypto = crypto }
448 nc_combo = Transport
449 { sendMessage = \addr -> \case
450 NetCrypto x -> sendMessage cryptonet addr x
451 NetHandshake x -> sendMessage handshakes addr x
452 , awaitMessage = \_ -> throwIO (userError "oops: awaitMessage nc_combo")
453 , closeTransport = hPutStrLn stderr "oops: closeTransport nc_combo"
454 }
455 let dhtnet0 = layerTransportM (DHT.decrypt crypto) (DHT.encrypt crypto) dhtcrypt 448 let dhtnet0 = layerTransportM (DHT.decrypt crypto) (DHT.encrypt crypto) dhtcrypt
456 tbl4 = DHT.routing4 $ mkrouting (error "missing client") 449 tbl4 = DHT.routing4 $ mkrouting (error "missing client")
457 tbl6 = DHT.routing6 $ mkrouting (error "missing client") 450 tbl6 = DHT.routing6 $ mkrouting (error "missing client")
diff --git a/src/Network/Tox/Crypto/Handlers.hs b/src/Network/Tox/Crypto/Handlers.hs
index 0027e414..cc2fba6d 100644
--- a/src/Network/Tox/Crypto/Handlers.hs
+++ b/src/Network/Tox/Crypto/Handlers.hs
@@ -4,7 +4,6 @@
4{-# LANGUAGE DeriveFunctor #-} 4{-# LANGUAGE DeriveFunctor #-}
5module Network.Tox.Crypto.Handlers where 5module Network.Tox.Crypto.Handlers where
6 6
7import Network.QueryResponse
8import Network.Tox.NodeId 7import Network.Tox.NodeId
9import Network.Tox.Crypto.Transport 8import Network.Tox.Crypto.Transport
10import Network.Tox.DHT.Transport (Cookie(..),CookieData(..), CookieRequest(..), NoSpam(..)) 9import Network.Tox.DHT.Transport (Cookie(..),CookieData(..), CookieRequest(..), NoSpam(..))
@@ -299,8 +298,9 @@ data NetCryptoSessions = NCSessions
299 , outboundQueueCapacity :: Word32 298 , outboundQueueCapacity :: Word32
300 , nextSessionId :: TVar SessionID 299 , nextSessionId :: TVar SessionID
301 , announceNewSessionHooks :: TVar [IOHook (Maybe NoSpam) NetCryptoSession] 300 , announceNewSessionHooks :: TVar [IOHook (Maybe NoSpam) NetCryptoSession]
302 , sessionTransport :: Transport String SockAddr NetCrypto 301 , sendHandshake :: SockAddr -> Handshake Encrypted -> IO ()
303 , listenerIDSupply :: TVar Supply 302 , sendSessionPacket :: SockAddr -> CryptoPacket Encrypted -> IO ()
303 , listenerIDSupply :: TVar Supply
304 } 304 }
305 305
306-- | This is the type of a hook to run when a session is created. 306-- | This is the type of a hook to run when a session is created.
@@ -379,7 +379,8 @@ newSessionsState crypto unrechook hooks = do
379 , outboundQueueCapacity = 400 379 , outboundQueueCapacity = 400
380 , nextSessionId = nextSessionId0 380 , nextSessionId = nextSessionId0
381 , announceNewSessionHooks = announceNewSessionHooks0 381 , announceNewSessionHooks = announceNewSessionHooks0
382 , sessionTransport = error "Need to set sessionTransport field of NetCryptoSessions!" 382 , sendHandshake = error "Need to set sendHandshake field of NetCryptoSessions!"
383 , sendSessionPacket = error "Need to set sendSessionPacket field of NetCryptoSessions!"
383 , listenerIDSupply = lsupplyVar 384 , listenerIDSupply = lsupplyVar
384 } 385 }
385 386
@@ -524,7 +525,7 @@ freshCryptoSession sessions
524 let myhandshake= encodeHandshake <$> mbMyhandshakeData 525 let myhandshake= encodeHandshake <$> mbMyhandshakeData
525 ncHandShake0 <- atomically $ newTVar (frmMaybe myhandshake) 526 ncHandShake0 <- atomically $ newTVar (frmMaybe myhandshake)
526 forM myhandshake $ \response_handshake -> do 527 forM myhandshake $ \response_handshake -> do
527 sendMessage (sessionTransport sessions) addr (NetHandshake response_handshake) 528 sendHandshake sessions addr response_handshake
528 ncMyPacketNonce0 <- atomically $ newTVar newBaseNonce 529 ncMyPacketNonce0 <- atomically $ newTVar newBaseNonce
529 cookie0 <- atomically $ newTVar (HaveCookie otherCookie) 530 cookie0 <- atomically $ newTVar (HaveCookie otherCookie)
530 newsession <- generateSecretKey 531 newsession <- generateSecretKey
@@ -634,7 +635,7 @@ runUponHandshake netCryptoSession0 addr pktoq = do
634 fix $ \loop -> do 635 fix $ \loop -> do
635 (_,pkt) <- atomically $ PQ.dequeueOutgoing pktoq 636 (_,pkt) <- atomically $ PQ.dequeueOutgoing pktoq
636 dput XNetCrypto "NetCryptoDequeueOutgoing thread... Sending encrypted Packet" 637 dput XNetCrypto "NetCryptoDequeueOutgoing thread... Sending encrypted Packet"
637 sendMessage (sessionTransport sessions) addr (NetCrypto pkt) 638 sendSessionPacket sessions addr pkt
638 loop 639 loop
639 -- launch ping thread 640 -- launch ping thread
640 fuzz <- randomRIO (0,2000) 641 fuzz <- randomRIO (0,2000)
@@ -706,10 +707,6 @@ updateCryptoSession sessions addr hp session handshake = do
706 then freshCryptoSession sessions addr hp -- basenonce mismatch, trigger refresh 707 then freshCryptoSession sessions addr hp -- basenonce mismatch, trigger refresh
707 else atomically $ writeTVar (ncState session) {-Accepted-}(InProgress AwaitingSessionPacket) 708 else atomically $ writeTVar (ncState session) {-Accepted-}(InProgress AwaitingSessionPacket)
708 709
709cryptoNetHandler :: NetCryptoSessions -> SockAddr -> NetCrypto -> IO (Maybe (NetCrypto -> NetCrypto))
710cryptoNetHandler sessions addr (NetHandshake hs) = handshakeH sessions addr hs
711cryptoNetHandler sessions addr (NetCrypto pkt) = sessionPacketH sessions addr pkt
712
713handshakeH :: NetCryptoSessions -> SockAddr -> Handshake Encrypted -> IO (Maybe a) 710handshakeH :: NetCryptoSessions -> SockAddr -> Handshake Encrypted -> IO (Maybe a)
714handshakeH sessions addr hshake@(Handshake (Cookie n24 ecookie) nonce24 encrypted) = do 711handshakeH sessions addr hshake@(Handshake (Cookie n24 ecookie) nonce24 encrypted) = do
715 dput XNetCrypto ("RECIEVED HANDSHAKE from " ++ show addr) 712 dput XNetCrypto ("RECIEVED HANDSHAKE from " ++ show addr)
@@ -728,18 +725,18 @@ handshakeH sessions addr hshake@(Handshake (Cookie n24 ecookie) nonce24 encrypte
728 (CookieData cookieTime remotePubkey remoteDhtkey) <- (decodePlain =<< decryptSymmetric symkey n24 ecookie) 725 (CookieData cookieTime remotePubkey remoteDhtkey) <- (decodePlain =<< decryptSymmetric symkey n24 ecookie)
729 Right $ do -- IO Monad 726 Right $ do -- IO Monad
730 decrypted <- anyRight seckeys $ \key -> do 727 decrypted <- anyRight seckeys $ \key -> do
731 dput XNetCrypto $ "cryptoNetHandler: remotePubkey = " ++ show (key2id $ remotePubkey) 728 dput XNetCrypto $ "(NetCrypto)handshakeH: remotePubkey = " ++ show (key2id $ remotePubkey)
732 dput XNetCrypto $ "cryptoNetHandler: nonce24 = " ++ show nonce24 729 dput XNetCrypto $ "(NetCrypto)handshakeH: nonce24 = " ++ show nonce24
733 secret <- lookupSharedSecret crypto key remotePubkey nonce24 730 secret <- lookupSharedSecret crypto key remotePubkey nonce24
734 let step1 = decrypt secret encrypted 731 let step1 = decrypt secret encrypted
735 case step1 of 732 case step1 of
736 Left s -> do 733 Left s -> do
737 dput XNetCrypto $ "cryptoNetHandler: (decrypt) " ++ s 734 dput XNetCrypto $ "(NetCrypto)handshakeH: (decrypt) " ++ s
738 return (Left s) 735 return (Left s)
739 Right pln -> do 736 Right pln -> do
740 case decodePlain pln of 737 case decodePlain pln of
741 Left s -> do 738 Left s -> do
742 dput XNetCrypto $ "cryptoNetHandler: (decodePlain) " ++ s 739 dput XNetCrypto $ "(NetCrypto)handshakeH: (decodePlain) " ++ s
743 return (Left s) 740 return (Left s)
744 Right x -> return (Right (key,x)) 741 Right x -> return (Right (key,x))
745 return $ do -- Either Monad 742 return $ do -- Either Monad
@@ -765,7 +762,7 @@ handshakeH sessions addr hshake@(Handshake (Cookie n24 ecookie) nonce24 encrypte
765 , hpCookieRemoteDhtkey = remoteDhtkey 762 , hpCookieRemoteDhtkey = remoteDhtkey
766 } 763 }
767 case lr of 764 case lr of
768 Left s -> dput XNetCrypto ("cryptoNetHandler: " ++ s) 765 Left s -> dput XNetCrypto ("(NetCrypto)handshakeH: " ++ s)
769 Right hp@(HParam 766 Right hp@(HParam
770 { hpTheirBaseNonce = Just theirBaseNonce 767 { hpTheirBaseNonce = Just theirBaseNonce
771 , hpOtherCookie = otherCookie 768 , hpOtherCookie = otherCookie
@@ -774,7 +771,7 @@ handshakeH sessions addr hshake@(Handshake (Cookie n24 ecookie) nonce24 encrypte
774 , hpCookieRemotePubkey = remotePublicKey 771 , hpCookieRemotePubkey = remotePublicKey
775 , hpCookieRemoteDhtkey = remoteDhtPublicKey 772 , hpCookieRemoteDhtkey = remoteDhtPublicKey
776 }) -> do 773 }) -> do
777 dput XNetCrypto ("cryptoNetHandler: hpTheirBaseNonce = " ++ show theirBaseNonce) 774 dput XNetCrypto ("(NetCrypto)handshakeH: hpTheirBaseNonce = " ++ show theirBaseNonce)
778 sessionsmap <- atomically $ readTVar allsessions 775 sessionsmap <- atomically $ readTVar allsessions
779 -- Do a lookup, so we can handle the update case differently 776 -- Do a lookup, so we can handle the update case differently
780 case Map.lookup addr sessionsmap of 777 case Map.lookup addr sessionsmap of
@@ -812,25 +809,25 @@ sessionPacketH sessions addr (CryptoPacket nonce16 encrypted) = do
812 lr <- fmap join $ sequence $ do -- Either Monad -- 809 lr <- fmap join $ sequence $ do -- Either Monad --
813 pubkey <- maybeToEither mbpublickey 810 pubkey <- maybeToEither mbpublickey
814 Right $ do -- IO Monad 811 Right $ do -- IO Monad
815 dput XNetCrypto $ "cryptoNetHandler: pubkey = " ++ show (key2id $ pubkey) 812 dput XNetCrypto $ "(NetCrypto)sessionPacketH: pubkey = " ++ show (key2id $ pubkey)
816 dput XNetCrypto $ "cryptoNetHandler: theirBaseNonce = " ++ show theirBaseNonce 813 dput XNetCrypto $ "(NetCrypto)sessionPacketH: theirBaseNonce = " ++ show theirBaseNonce
817 dput XNetCrypto $ "cryptoNetHandler: tempNonce = " ++ show tempNonce 814 dput XNetCrypto $ "(NetCrypto)sessionPacketH: tempNonce = " ++ show tempNonce
818 ++ " nonce16=" ++ printf "0x%x" nonce16 ++ " last2bytes =" ++ printf "0x%x" (last2Bytes theirBaseNonce) 815 ++ " nonce16=" ++ printf "0x%x" nonce16 ++ " last2bytes =" ++ printf "0x%x" (last2Bytes theirBaseNonce)
819 secret <- lookupSharedSecret crypto ncSessionSecret pubkey tempNonce 816 secret <- lookupSharedSecret crypto ncSessionSecret pubkey tempNonce
820 let step1 = decrypt secret encrypted 817 let step1 = decrypt secret encrypted
821 case step1 of 818 case step1 of
822 Left s -> do 819 Left s -> do
823 dput XNetCrypto $ "cryptoNetHandler: (decrypt) " ++ s 820 dput XNetCrypto $ "(NetCrypto)sessionPacketH: (decrypt) " ++ s
824 return (Left s) 821 return (Left s)
825 Right pln -> do 822 Right pln -> do
826 case decodePlain pln of 823 case decodePlain pln of
827 Left s -> do 824 Left s -> do
828 dput XNetCrypto $ "cryptoNetHandler: (decodePlain) " ++ s 825 dput XNetCrypto $ "(NetCrypto)sessionPacketH: (decodePlain) " ++ s
829 return (Left s) 826 return (Left s)
830 Right x -> return (Right x) 827 Right x -> return (Right x)
831 case lr of 828 case lr of
832 Left s -> do 829 Left s -> do
833 dput XNetCrypto $ "cryptoNetHandler: " ++ s 830 dput XNetCrypto $ "(NetCrypto)sessionPacketH: " ++ s
834 return Nothing -- decryption failed, ignore packet 831 return Nothing -- decryption failed, ignore packet
835 Right cd@(CryptoData {bufferStart, bufferEnd, bufferData=cm}) -> do -- decryption succeeded, 832 Right cd@(CryptoData {bufferStart, bufferEnd, bufferData=cm}) -> do -- decryption succeeded,
836 -- TODO: Why do I need bufferStart & bufferEnd? 833 -- TODO: Why do I need bufferStart & bufferEnd?
diff --git a/src/Network/Tox/Crypto/Transport.hs b/src/Network/Tox/Crypto/Transport.hs
index 2c998006..0588da4b 100644
--- a/src/Network/Tox/Crypto/Transport.hs
+++ b/src/Network/Tox/Crypto/Transport.hs
@@ -9,7 +9,6 @@ module Network.Tox.Crypto.Transport
9 , encodeCrypto 9 , encodeCrypto
10 , parseHandshakes 10 , parseHandshakes
11 , encodeHandshakes 11 , encodeHandshakes
12 , NetCrypto(..)
13 , CryptoData(..) 12 , CryptoData(..)
14 , CryptoMessage(..) 13 , CryptoMessage(..)
15 , MessageName(..) 14 , MessageName(..)
@@ -77,10 +76,6 @@ import Data.Serialize as S
77import Control.Arrow 76import Control.Arrow
78 77
79 78
80data NetCrypto
81 = NetHandshake (Handshake Encrypted)
82 | NetCrypto (CryptoPacket Encrypted)
83
84parseCrypto :: (ByteString, SockAddr) -> Either (CryptoPacket Encrypted, SockAddr) (ByteString, SockAddr) 79parseCrypto :: (ByteString, SockAddr) -> Either (CryptoPacket Encrypted, SockAddr) (ByteString, SockAddr)
85parseCrypto ((B.uncons -> Just (0x1b,pkt)),saddr) = either (\_ -> Right (pkt,saddr)) 80parseCrypto ((B.uncons -> Just (0x1b,pkt)),saddr) = either (\_ -> Right (pkt,saddr))
86 (\x -> Left (x ,saddr)) 81 (\x -> Left (x ,saddr))
@@ -98,15 +93,6 @@ encodeHandshakes :: Handshake Encrypted -> SockAddr -> (ByteString, SockAddr)
98encodeHandshakes x saddr = (B.cons 0x1a (runPut $ put x),saddr) 93encodeHandshakes x saddr = (B.cons 0x1a (runPut $ put x),saddr)
99 94
100 95
101parseNetCrypto :: ByteString -> SockAddr -> Either String (NetCrypto, SockAddr)
102parseNetCrypto (B.uncons -> Just (0x1a,pkt)) saddr = left ("parseNetCrypto: "++) $ (,saddr) . NetHandshake <$> runGet get pkt
103parseNetCrypto (B.uncons -> Just (0x1b,pkt)) saddr = left ("parseNetCrypto: "++) $ (,saddr) . NetCrypto <$> runGet get pkt
104parseNetCrypto _ _ = Left "parseNetCrypto: ?"
105
106encodeNetCrypto :: NetCrypto -> SockAddr -> (ByteString, SockAddr)
107encodeNetCrypto (NetHandshake x) saddr = (B.cons 0x1a (runPut $ put x),saddr)
108encodeNetCrypto (NetCrypto x) saddr = (B.cons 0x1b (runPut $ put x),saddr)
109
110data Handshake (f :: * -> *) = Handshake 96data Handshake (f :: * -> *) = Handshake
111 { -- The cookie is a cookie obtained by 97 { -- The cookie is a cookie obtained by
112 -- sending a cookie request packet to the peer and getting a cookie 98 -- sending a cookie request packet to the peer and getting a cookie