summaryrefslogtreecommitdiff
path: root/HandshakeCache.hs
diff options
context:
space:
mode:
authorJoe Crayne <joe@jerkface.net>2018-11-01 21:50:25 -0400
committerJoe Crayne <joe@jerkface.net>2018-11-02 00:21:52 -0400
commitb9b1a68f1a440f36b5f3b7a7acdd3d0e9a09a2a2 (patch)
treeea486819ca412ee9043131111fbd7eb7efc8433d /HandshakeCache.hs
parentffaea2b2169a499aaa2ac72531beeb991714025a (diff)
Use locally issued cookie-nonce for handshake cache.
Diffstat (limited to 'HandshakeCache.hs')
-rw-r--r--HandshakeCache.hs19
1 files changed, 10 insertions, 9 deletions
diff --git a/HandshakeCache.hs b/HandshakeCache.hs
index 6f9d466f..c4dd090c 100644
--- a/HandshakeCache.hs
+++ b/HandshakeCache.hs
@@ -23,11 +23,8 @@ import Network.Tox.Handshake
23 23
24data HandshakeCache = HandshakeCache 24data HandshakeCache = HandshakeCache
25 { -- Note that currently we are storing sent handshakes keyed by the 25 { -- Note that currently we are storing sent handshakes keyed by the
26 -- remotely issued cookie. This probably means that it's possible for 26 -- locally issued cookie nonce.
27 -- one your contacts that you are trying to open a session with to 27 hscTable :: TVar (MinMaxPSQ' Nonce24 POSIXTime (SecretKey,HandshakeData))
28 -- prevent you from opening a session with another contact if they know
29 -- the cookie that person issued you.
30 hscTable :: TVar (MinMaxPSQ' (Cookie Encrypted) POSIXTime (SecretKey,HandshakeData))
31 , hscSend :: SockAddr -> Handshake Encrypted -> IO () 28 , hscSend :: SockAddr -> Handshake Encrypted -> IO ()
32 , hscCrypto :: TransportCrypto 29 , hscCrypto :: TransportCrypto
33 , hscPendingCookies :: TVar (Map (PublicKey,PublicKey) ()) 30 , hscPendingCookies :: TVar (Map (PublicKey,PublicKey) ())
@@ -51,10 +48,10 @@ getSentHandshake :: HandshakeCache
51 -> Cookie Identity -- locally issued 48 -> Cookie Identity -- locally issued
52 -> Cookie Encrypted -- remotely issued 49 -> Cookie Encrypted -- remotely issued
53 -> IO (Maybe (SecretKey, HandshakeData)) 50 -> IO (Maybe (SecretKey, HandshakeData))
54getSentHandshake hscache me their_addr (Cookie _ (Identity cd)) ecookie = do 51getSentHandshake hscache me their_addr (Cookie n24 (Identity cd)) ecookie = do
55 now <- getPOSIXTime 52 now <- getPOSIXTime
56 io <- atomically $ do 53 io <- atomically $ do
57 m <- checkExpiry now . MM.lookup' ecookie <$> readTVar (hscTable hscache) 54 m <- checkExpiry now . MM.lookup' n24 <$> readTVar (hscTable hscache)
58 case m of 55 case m of
59 Just s -> return $ return $ Just s 56 Just s -> return $ return $ Just s
60 Nothing -> do 57 Nothing -> do
@@ -64,9 +61,12 @@ getSentHandshake hscache me their_addr (Cookie _ (Identity cd)) ecookie = do
64 Right their_node -> do 61 Right their_node -> do
65 (s,hs) <- cacheHandshakeSTM hscache me them their_node ecookie now 62 (s,hs) <- cacheHandshakeSTM hscache me them their_node ecookie now
66 return $ do 63 return $ do
64 dput XNetCrypto $ "getSentHandshake sending new handshake."
67 hscSend hscache their_addr hs 65 hscSend hscache their_addr hs
68 return $ Just s 66 return $ Just s
69 io 67 r <- io
68 dput XNetCrypto $ "getSentHandshake me="++show (key2id $ toPublic me)++" their_addr="++show their_addr++" --> " ++ show r
69 return r
70 70
71 71
72checkExpiry :: POSIXTime -> Maybe (POSIXTime,r) -> Maybe r 72checkExpiry :: POSIXTime -> Maybe (POSIXTime,r) -> Maybe r
@@ -97,7 +97,8 @@ cacheHandshakeSTM hscache me them their_node ecookie timestamp = do
97 , otherCookie = freshCookie 97 , otherCookie = freshCookie
98 } 98 }
99 hs <- encodeHandshake timestamp (hscCrypto hscache) me them ecookie hsdata 99 hs <- encodeHandshake timestamp (hscCrypto hscache) me them ecookie hsdata
100 modifyTVar' (hscTable hscache) $ MM.insertTake' 20 ecookie (newsession,hsdata) timestamp 100 let Cookie cnonce _ = freshCookie
101 modifyTVar' (hscTable hscache) $ MM.insertTake' 20 cnonce (newsession,hsdata) timestamp
101 return ((newsession,hsdata),hs) 102 return ((newsession,hsdata),hs)
102 103
103cacheHandshake :: HandshakeCache 104cacheHandshake :: HandshakeCache