summaryrefslogtreecommitdiff
path: root/src/Network/Tox/Crypto/Handlers.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Network/Tox/Crypto/Handlers.hs')
-rw-r--r--src/Network/Tox/Crypto/Handlers.hs30
1 files changed, 20 insertions, 10 deletions
diff --git a/src/Network/Tox/Crypto/Handlers.hs b/src/Network/Tox/Crypto/Handlers.hs
index bcca65e6..dcae9f2f 100644
--- a/src/Network/Tox/Crypto/Handlers.hs
+++ b/src/Network/Tox/Crypto/Handlers.hs
@@ -50,6 +50,8 @@ import DPut
50import Debug.Trace 50import Debug.Trace
51import Text.Printf 51import Text.Printf
52import Data.Bool 52import Data.Bool
53import Connection (Status(..))
54import Connection.Tox (ToxProgress(..))
53 55
54 56
55-- * These types are isomorphic to Maybe, but have the advantage of documenting 57-- * These types are isomorphic to Maybe, but have the advantage of documenting
@@ -99,8 +101,8 @@ instance AsMaybe UponCryptoPacket where
99 frmMaybe (Just x) = HaveCryptoPacket x 101 frmMaybe (Just x) = HaveCryptoPacket x
100 102
101 103
102data NetCryptoSessionStatus = Unaccepted | Accepted {- InProgress AwaitingSessionPacket -} | Confirmed {- Established -} 104--data NetCryptoSessionStatus = Unaccepted | Accepted {- InProgress AwaitingSessionPacket -} | Confirmed {- Established -}
103 deriving (Eq,Ord,Show,Enum) 105-- deriving (Eq,Ord,Show,Enum)
104 106
105 107
106-- | The idea of IOHook is to replicate the familiar pattern 108-- | The idea of IOHook is to replicate the familiar pattern
@@ -214,7 +216,7 @@ type SessionID = Word64
214type ListenerType = Word64 216type ListenerType = Word64
215 217
216data NetCryptoSession = NCrypto 218data NetCryptoSession = NCrypto
217 { ncState :: TVar NetCryptoSessionStatus 219 { ncState :: TVar (Status ToxProgress)
218 , ncMyPublicKey :: PublicKey 220 , ncMyPublicKey :: PublicKey
219 , ncSessionId :: SessionID 221 , ncSessionId :: SessionID
220 , ncTheirPublicKey :: PublicKey -- Tox id w/o nospam 222 , ncTheirPublicKey :: PublicKey -- Tox id w/o nospam
@@ -503,7 +505,10 @@ freshCryptoSession sessions
503 x <- readTVar (nextSessionId sessions) 505 x <- readTVar (nextSessionId sessions)
504 modifyTVar (nextSessionId sessions) (+1) 506 modifyTVar (nextSessionId sessions) (+1)
505 return x 507 return x
506 ncState0 <- atomically $ newTVar Accepted -- (InProgress AwaitingSessionPacket) 508 -- ncState0 <- atomically $ newTVar Accepted -- (InProgress AwaitingSessionPacket)
509 ncState0 <- atomically $ newTVar (if isJust mbtheirBaseNonce
510 then InProgress AwaitingSessionPacket
511 else InProgress AwaitingHandshake)
507 ncTheirBaseNonce0 <- atomically $ newTVar (frmMaybe mbtheirBaseNonce) 512 ncTheirBaseNonce0 <- atomically $ newTVar (frmMaybe mbtheirBaseNonce)
508 n24 <- atomically $ transportNewNonce crypto 513 n24 <- atomically $ transportNewNonce crypto
509 state <- lookupSharedSecret crypto key remotePublicKey n24 514 state <- lookupSharedSecret crypto key remotePublicKey n24
@@ -551,7 +556,7 @@ freshCryptoSession sessions
551 atomically $ do 556 atomically $ do
552 n24 <- readTVar ncMyPacketNonce0 557 n24 <- readTVar ncMyPacketNonce0
553 let n24plus1 = incrementNonce24 n24 558 let n24plus1 = incrementNonce24 n24
554 writeTVar ncMyPacketNonce0 n24plus1 559 trace ("ncMyPacketNonce+1=" ++ show n24plus1) $ writeTVar ncMyPacketNonce0 n24plus1
555 return (return (f n24, n24, ncOutgoingIdMap0)) 560 return (return (f n24, n24, ncOutgoingIdMap0))
556 pktoq <- atomically $ PQ.newOutGoing pktq ncToWire toWireIO 0 (outboundQueueCapacity sessions) 0 561 pktoq <- atomically $ PQ.newOutGoing pktq ncToWire toWireIO 0 (outboundQueueCapacity sessions) 0
557 return (HaveHandshake pktoq) 562 return (HaveHandshake pktoq)
@@ -559,6 +564,8 @@ freshCryptoSession sessions
559 listeners <- atomically $ newTVar IntMap.empty 564 listeners <- atomically $ newTVar IntMap.empty
560 msgNum <- atomically $ newTVar 0 565 msgNum <- atomically $ newTVar 0
561 dropNum <- atomically $ newTVar 0 566 dropNum <- atomically $ newTVar 0
567 theirbasenonce <- atomically $ readTVar ncTheirBaseNonce0
568 dput XNetCrypto $ "freshCryptoSession: Session ncTheirBaseNonce=" ++ show theirbasenonce
562 let netCryptoSession0 = 569 let netCryptoSession0 =
563 NCrypto { ncState = ncState0 570 NCrypto { ncState = ncState0
564 , ncMyPublicKey = toPublic key 571 , ncMyPublicKey = toPublic key
@@ -635,8 +642,7 @@ updateCryptoSession :: NetCryptoSessions -> SockAddr -> HandshakeParams -> NetCr
635updateCryptoSession sessions addr hp session = do 642updateCryptoSession sessions addr hp session = do
636 ncState0 <- atomically $ readTVar (ncState session) 643 ncState0 <- atomically $ readTVar (ncState session)
637 ncTheirBaseNonce0 <- atomically $ readTVar (ncTheirBaseNonce session) 644 ncTheirBaseNonce0 <- atomically $ readTVar (ncTheirBaseNonce session)
638 -- if (ncState0 >= InProgress AwaitingSessionPacket) 645 if (ncState0 >= {-Accepted-}InProgress AwaitingSessionPacket)
639 if (ncState0 >= Accepted)
640 -- If the nonce in the handshake and the dht key are both the same as 646 -- If the nonce in the handshake and the dht key are both the same as
641 -- the ones we have saved, assume we already handled this and this is a 647 -- the ones we have saved, assume we already handled this and this is a
642 -- duplicate handshake packet, otherwise disregard everything, and 648 -- duplicate handshake packet, otherwise disregard everything, and
@@ -661,7 +667,7 @@ updateCryptoSession sessions addr hp session = do
661 ++ "hpTheirBaseNonce=" ++ show (hpTheirBaseNonce hp)) 667 ++ "hpTheirBaseNonce=" ++ show (hpTheirBaseNonce hp))
662 if ( ncTheirBaseNonce0 /= frmMaybe (hpTheirBaseNonce hp)) 668 if ( ncTheirBaseNonce0 /= frmMaybe (hpTheirBaseNonce hp))
663 then freshCryptoSession sessions addr hp -- basenonce mismatch, trigger refresh 669 then freshCryptoSession sessions addr hp -- basenonce mismatch, trigger refresh
664 else atomically $ writeTVar (ncState session) Accepted -- (InProgress AwaitingSessionPacket) 670 else atomically $ writeTVar (ncState session) {-Accepted-}(InProgress AwaitingSessionPacket)
665 671
666 672
667cryptoNetHandler :: NetCryptoSessions -> SockAddr -> NetCrypto -> IO (Maybe (NetCrypto -> NetCrypto)) 673cryptoNetHandler :: NetCryptoSessions -> SockAddr -> NetCrypto -> IO (Maybe (NetCrypto -> NetCrypto))
@@ -728,6 +734,7 @@ cryptoNetHandler sessions addr (NetHandshake (Handshake (Cookie n24 ecookie) non
728 , hpCookieRemotePubkey = remotePublicKey 734 , hpCookieRemotePubkey = remotePublicKey
729 , hpCookieRemoteDhtkey = remoteDhtPublicKey 735 , hpCookieRemoteDhtkey = remoteDhtPublicKey
730 }) -> do 736 }) -> do
737 dput XNetCrypto ("cryptoNetHandler: hpTheirBaseNonce = " ++ show theirBaseNonce)
731 sessionsmap <- atomically $ readTVar allsessions 738 sessionsmap <- atomically $ readTVar allsessions
732 -- Do a lookup, so we can handle the update case differently 739 -- Do a lookup, so we can handle the update case differently
733 case Map.lookup addr sessionsmap of 740 case Map.lookup addr sessionsmap of
@@ -752,7 +759,10 @@ cryptoNetHandler sessions addr (NetCrypto (CryptoPacket nonce16 encrypted)) = do
752 Just session@(NCrypto { ncIncomingTypeArray, ncState, ncPacketQueue, ncHooks, 759 Just session@(NCrypto { ncIncomingTypeArray, ncState, ncPacketQueue, ncHooks,
753 ncSessionSecret, ncTheirSessionPublic, ncTheirBaseNonce, 760 ncSessionSecret, ncTheirSessionPublic, ncTheirBaseNonce,
754 ncPingMachine}) -> do 761 ncPingMachine}) -> do
755 HaveHandshake theirBaseNonce <- atomically $ readTVar ncTheirBaseNonce 762 mbTheirBaseNonce <- atomically $ readTVar ncTheirBaseNonce
763 case mbTheirBaseNonce of
764 NeedHandshake -> dput XNetCrypto "CryptoPacket recieved, but we still dont have their base nonce?" >> return Nothing
765 HaveHandshake theirBaseNonce -> do
756 -- Try to decrypt message 766 -- Try to decrypt message
757 let diff :: Word16 767 let diff :: Word16
758 diff = nonce16 - (last2Bytes theirBaseNonce) -- truncating to Word16 768 diff = nonce16 - (last2Bytes theirBaseNonce) -- truncating to Word16
@@ -796,7 +806,7 @@ cryptoNetHandler sessions addr (NetCrypto (CryptoPacket nonce16 encrypted)) = do
796 ++ " = " ++ show x) (return ()) 806 ++ " = " ++ show x) (return ())
797 writeTVar ncTheirBaseNonce (HaveHandshake y) 807 writeTVar ncTheirBaseNonce (HaveHandshake y)
798 -- then set session confirmed, 808 -- then set session confirmed,
799 atomically $ writeTVar ncState Confirmed {-Established-} 809 atomically $ writeTVar ncState {-Confirmed-}Established
800 -- bump ping machine 810 -- bump ping machine
801 case ncPingMachine of 811 case ncPingMachine of
802 Just pingMachine -> pingBump pingMachine 812 Just pingMachine -> pingBump pingMachine