summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoe Crayne <joe@jerkface.net>2020-01-15 22:44:17 -0500
committerJoe Crayne <joe@jerkface.net>2020-01-16 00:15:27 -0500
commit2a35ec75c6aaddf617d3617fc3527e2e54d5cab7 (patch)
tree8b036b581981e08496086c78ac255de3646c134d
parent14de512e24922a9fc2c97ecea30011380ed2fad5 (diff)
Handle out-of-band netcrypto handshake.
-rw-r--r--dht/HandshakeCache.hs14
-rw-r--r--dht/src/Network/Tox.hs32
-rw-r--r--dht/src/Network/Tox/Session.hs12
-rw-r--r--dht/src/Network/Tox/TCP.hs2
4 files changed, 42 insertions, 18 deletions
diff --git a/dht/HandshakeCache.hs b/dht/HandshakeCache.hs
index d9ffacab..00836fc8 100644
--- a/dht/HandshakeCache.hs
+++ b/dht/HandshakeCache.hs
@@ -27,13 +27,15 @@ data HandshakeCache = HandshakeCache
27 { -- Note that currently we are storing sent handshakes keyed by the 27 { -- Note that currently we are storing sent handshakes keyed by the
28 -- locally issued cookie nonce. 28 -- locally issued cookie nonce.
29 hscTable :: TVar (MinMaxPSQ' Nonce24 POSIXTime (SecretKey,HandshakeData)) 29 hscTable :: TVar (MinMaxPSQ' Nonce24 POSIXTime (SecretKey,HandshakeData))
30 , hscSend :: Multi.SessionAddress -> Handshake Encrypted -> IO () 30 , hscSend :: Multi.SessionAddress -> Handshake Encrypted -> IO Multi.SessionAddress
31 , hscCrypto :: TransportCrypto 31 , hscCrypto :: TransportCrypto
32 , hscPendingCookies :: TVar (Map (PublicKey,PublicKey) ()) 32 , hscPendingCookies :: TVar (Map (PublicKey,PublicKey) ())
33 } 33 }
34 34
35 35
36newHandshakeCache :: TransportCrypto -> (Multi.SessionAddress -> Handshake Encrypted -> IO ()) -> IO HandshakeCache 36newHandshakeCache :: TransportCrypto
37 -> (Multi.SessionAddress -> Handshake Encrypted -> IO Multi.SessionAddress)
38 -> IO HandshakeCache
37newHandshakeCache crypto send = atomically $ do 39newHandshakeCache crypto send = atomically $ do
38 tbl <- newTVar MM.empty 40 tbl <- newTVar MM.empty
39 pcs <- newTVar Map.empty 41 pcs <- newTVar Map.empty
@@ -49,13 +51,13 @@ getSentHandshake :: HandshakeCache
49 -> Multi.SessionAddress 51 -> Multi.SessionAddress
50 -> Cookie Identity -- locally issued 52 -> Cookie Identity -- locally issued
51 -> Cookie Encrypted -- remotely issued 53 -> Cookie Encrypted -- remotely issued
52 -> IO (Maybe (SecretKey, HandshakeData)) 54 -> IO (Maybe (Multi.SessionAddress, (SecretKey, HandshakeData)))
53getSentHandshake hscache me their_addr (Cookie n24 (Identity cd)) ecookie = do 55getSentHandshake hscache me their_addr (Cookie n24 (Identity cd)) ecookie = do
54 now <- getPOSIXTime 56 now <- getPOSIXTime
55 io <- atomically $ do 57 io <- atomically $ do
56 m <- checkExpiry now . MM.lookup' n24 <$> readTVar (hscTable hscache) 58 m <- checkExpiry now . MM.lookup' n24 <$> readTVar (hscTable hscache)
57 case m of 59 case m of
58 Just s -> return $ return $ Just s 60 Just s -> return $ return $ Just (their_addr, s)
59 Nothing -> do 61 Nothing -> do
60 let them = longTermKey cd 62 let them = longTermKey cd
61 case Multi.nodeInfo (key2id $ dhtKey cd) their_addr of 63 case Multi.nodeInfo (key2id $ dhtKey cd) their_addr of
@@ -64,8 +66,8 @@ getSentHandshake hscache me their_addr (Cookie n24 (Identity cd)) ecookie = do
64 (s,hs) <- cacheHandshakeSTM hscache me them their_node ecookie now 66 (s,hs) <- cacheHandshakeSTM hscache me them their_node ecookie now
65 return $ do 67 return $ do
66 dput XNetCrypto $ "getSentHandshake sending new handshake." 68 dput XNetCrypto $ "getSentHandshake sending new handshake."
67 hscSend hscache their_addr hs 69 addr' <- hscSend hscache their_addr hs
68 return $ Just s 70 return $ Just (addr', s)
69 r <- io 71 r <- io
70 dput XNetCrypto $ "getSentHandshake me="++show (key2id $ toPublic me)++" their_addr="++show their_addr++" --> " ++ show r 72 dput XNetCrypto $ "getSentHandshake me="++show (key2id $ toPublic me)++" their_addr="++show their_addr++" --> " ++ show r
71 return r 73 return r
diff --git a/dht/src/Network/Tox.hs b/dht/src/Network/Tox.hs
index f136ab96..23dbfe27 100644
--- a/dht/src/Network/Tox.hs
+++ b/dht/src/Network/Tox.hs
@@ -1,12 +1,13 @@
1{-# LANGUAGE CPP #-} 1{-# LANGUAGE CPP #-}
2{-# LANGUAGE FlexibleContexts #-}
3{-# LANGUAGE DeriveDataTypeable #-} 2{-# LANGUAGE DeriveDataTypeable #-}
4{-# LANGUAGE DeriveFoldable #-} 3{-# LANGUAGE DeriveFoldable #-}
5{-# LANGUAGE DeriveFunctor #-} 4{-# LANGUAGE DeriveFunctor #-}
6{-# LANGUAGE DeriveGeneric #-} 5{-# LANGUAGE DeriveGeneric #-}
7{-# LANGUAGE DeriveTraversable #-} 6{-# LANGUAGE DeriveTraversable #-}
8{-# LANGUAGE ExistentialQuantification #-} 7{-# LANGUAGE ExistentialQuantification #-}
8{-# LANGUAGE FlexibleContexts #-}
9{-# LANGUAGE FlexibleInstances #-} 9{-# LANGUAGE FlexibleInstances #-}
10{-# LANGUAGE GADTs #-}
10{-# LANGUAGE GeneralizedNewtypeDeriving #-} 11{-# LANGUAGE GeneralizedNewtypeDeriving #-}
11{-# LANGUAGE LambdaCase #-} 12{-# LANGUAGE LambdaCase #-}
12{-# LANGUAGE NamedFieldPuns #-} 13{-# LANGUAGE NamedFieldPuns #-}
@@ -72,7 +73,7 @@ import qualified Network.Tox.Onion.Transport as Onion
72import Network.Tox.RelayPinger 73import Network.Tox.RelayPinger
73import System.Global6 74import System.Global6
74import Network.Tox.Transport 75import Network.Tox.Transport
75import Network.Tox.TCP (tcpClient) 76import Network.Tox.TCP (tcpClient, ViaRelay(..))
76import Network.Tox.Onion.Routes 77import Network.Tox.Onion.Routes
77import Network.Tox.ContactInfo 78import Network.Tox.ContactInfo
78import Text.XXD 79import Text.XXD
@@ -131,7 +132,7 @@ myAddr routing4 routing6 maddr = atomically $ do
131 a <- readTVar var 132 a <- readTVar var
132 return $ Multi.UDP ==> R.thisNode a 133 return $ Multi.UDP ==> R.thisNode a
133 134
134newClient :: (DRG g, Show addr, Show meth) => 135newClient :: forall g addr meth x. (DRG g, Show addr, Show meth) =>
135 g -> Transport String addr x 136 g -> Transport String addr x
136 -> (Client String meth DHT.TransactionId addr x 137 -> (Client String meth DHT.TransactionId addr x
137 -> x 138 -> x
@@ -160,12 +161,25 @@ newClient drg net classify selfAddr handlers modifytbl modifynet = do
160 let word64mapT = transactionMethods (contramap w64Key w64MapMethods) gen 161 let word64mapT = transactionMethods (contramap w64Key w64MapMethods) gen
161 map_var <- atomically $ newTVar (drg, Data.Word64Map.empty) 162 map_var <- atomically $ newTVar (drg, Data.Word64Map.empty)
162 return $ Left (word64mapT,map_var) 163 return $ Left (word64mapT,map_var)
163 let dispatch tbl var handlers client = DispatchMethods 164 let dispatch :: TransactionMethods tbl DHT.TransactionId addr x
165 -> p
166 -> (meth
167 -> Maybe (MethodHandlerA String DHT.TransactionId addr x y))
168 -> Client String meth DHT.TransactionId addr x
169 -> DispatchMethodsA tbl String meth DHT.TransactionId addr x y
170 dispatch tbl var handlers client = DispatchMethods
164 { classifyInbound = classify client 171 { classifyInbound = classify client
165 , lookupHandler = handlers -- var 172 , lookupHandler = handlers -- var
166 , tableMethods = modifytbl tbl 173 , tableMethods = modifytbl tbl
167 } 174 }
168 eprinter = logErrors -- printErrors stderr 175 eprinter = logErrors -- printErrors stderr
176 mkclient :: (TransactionMethods
177 (g, pending) DHT.TransactionId addr x,
178 TVar (g, pending))
179 -> (ClientA String meth DHT.TransactionId addr x x
180 -> meth
181 -> Maybe (MethodHandlerA String DHT.TransactionId addr x x))
182 -> ClientA String meth DHT.TransactionId addr x x
169 mkclient (tbl,var) handlers = 183 mkclient (tbl,var) handlers =
170 let client = Client 184 let client = Client
171 { clientNet = addHandler (reportParseError eprinter) (handleMessage client) $ modifynet client net 185 { clientNet = addHandler (reportParseError eprinter) (handleMessage client) $ modifynet client net
@@ -337,7 +351,15 @@ newToxOverTransport keydb addr onNewSession (crypto,roster) udp tcp = do
337 dhtclient <- newClient drg dhtnet0 DHT.classify (myAddr tbl4 tbl6) (DHT.handlers crypto . mkrouting) id 351 dhtclient <- newClient drg dhtnet0 DHT.classify (myAddr tbl4 tbl6) (DHT.handlers crypto . mkrouting) id
338 (\client net -> onInbound (updateOnUDP client) net) 352 (\client net -> onInbound (updateOnUDP client) net)
339 353
340 hscache <- newHandshakeCache crypto (sendMessage handshakes) 354 hscache <- newHandshakeCache crypto $ \saddr hs -> do
355 saddr' <- case saddr of
356 Multi.SessionTCP :=> Identity (ViaRelay Nothing nid relay)
357 -> do let relayclient = relayClient $ tcpRelayPinger orouter
358 msaddr <- Multi.tcpConnectionRequest relayclient (id2key nid) relay
359 return $ maybe saddr Multi.sessionAddr msaddr
360 _ -> return saddr
361 sendMessage handshakes saddr' hs
362 return saddr'
341 let sparams = SessionParams 363 let sparams = SessionParams
342 { spCrypto = crypto 364 { spCrypto = crypto
343 , spSessions = sessions 365 , spSessions = sessions
diff --git a/dht/src/Network/Tox/Session.hs b/dht/src/Network/Tox/Session.hs
index 0d89afc4..ff86e502 100644
--- a/dht/src/Network/Tox/Session.hs
+++ b/dht/src/Network/Tox/Session.hs
@@ -49,7 +49,7 @@ data SessionParams = SessionParams
49 , spGetSentHandshake :: SecretKey -> Multi.SessionAddress 49 , spGetSentHandshake :: SecretKey -> Multi.SessionAddress
50 -> Cookie Identity 50 -> Cookie Identity
51 -> Cookie Encrypted 51 -> Cookie Encrypted
52 -> IO (Maybe (SessionKey, HandshakeData)) 52 -> IO (Maybe (Multi.SessionAddress, (SessionKey, HandshakeData)))
53 -- | This method is invoked on each new session and is responsible for 53 -- | This method is invoked on each new session and is responsible for
54 -- launching any threads necessary to keep the session alive. 54 -- launching any threads necessary to keep the session alive.
55 , spOnNewSession :: Session -> IO () 55 , spOnNewSession :: Session -> IO ()
@@ -116,18 +116,18 @@ plainHandshakeH :: SessionParams
116 -> SecretKey 116 -> SecretKey
117 -> Handshake Identity 117 -> Handshake Identity
118 -> IO () 118 -> IO ()
119plainHandshakeH sp saddr skey handshake = do 119plainHandshakeH sp saddr0 skey handshake = do
120 let hd = runIdentity $ handshakeData handshake 120 let hd = runIdentity $ handshakeData handshake
121 prelude = show saddr ++ " --> " 121 prelude = show saddr0 ++ " --> "
122 dput XNetCrypto $ unlines $ map (prelude ++) 122 dput XNetCrypto $ unlines $ map (prelude ++)
123 [ "handshake: auth=" ++ show (handshakeCookie handshake) 123 [ "handshake: auth=" ++ show (handshakeCookie handshake)
124 , " : issuing=" ++ show (otherCookie hd) 124 , " : issuing=" ++ show (otherCookie hd)
125 , " : baseNonce=" ++ show (baseNonce hd) 125 , " : baseNonce=" ++ show (baseNonce hd)
126 ] 126 ]
127 sent <- spGetSentHandshake sp skey saddr (handshakeCookie handshake) (otherCookie hd) 127 sent <- spGetSentHandshake sp skey saddr0 (handshakeCookie handshake) (otherCookie hd)
128 -- TODO: this is always returning sent = Nothing 128 -- TODO: this is always returning sent = Nothing
129 dput XNetCrypto $ " <-- (cached) handshake baseNonce " ++ show (fmap (baseNonce . snd) sent) 129 dput XNetCrypto $ " <-- (cached) handshake baseNonce " ++ show (fmap (baseNonce . snd . snd) sent)
130 forM_ sent $ \(hd_skey,hd_sent) -> do 130 forM_ sent $ \(saddr, (hd_skey,hd_sent)) -> do
131 sk <- SessionKeys (spCrypto sp) 131 sk <- SessionKeys (spCrypto sp)
132 hd_skey 132 hd_skey
133 (sessionKey hd) 133 (sessionKey hd)
diff --git a/dht/src/Network/Tox/TCP.hs b/dht/src/Network/Tox/TCP.hs
index f8ec430b..ebb15e3b 100644
--- a/dht/src/Network/Tox/TCP.hs
+++ b/dht/src/Network/Tox/TCP.hs
@@ -368,7 +368,7 @@ newClient :: TransportCrypto
368 , RelayCache 368 , RelayCache
369 , Transport String ViaRelay ByteString 369 , Transport String ViaRelay ByteString
370 , Transport String (OnionDestination RouteId) (OnionMessage Encrypted) ) 370 , Transport String (OnionDestination RouteId) (OnionMessage Encrypted) )
371 , Client String PacketNumber Nonce8 NodeInfo (Bool,RelayPacket)) 371 , RelayClient )
372newClient crypto store load lookupSender getRoute = do 372newClient crypto store load lookupSender getRoute = do
373 (tcpcache,net0) <- toxTCP crypto 373 (tcpcache,net0) <- toxTCP crypto
374 (relaynet,net1) <- partitionRelay net0 374 (relaynet,net1) <- partitionRelay net0