diff options
author | Joe Crayne <joe@jerkface.net> | 2018-09-07 12:03:18 -0400 |
---|---|---|
committer | Joe Crayne <joe@jerkface.net> | 2018-09-07 20:26:11 -0400 |
commit | f59f8c0acd3a098d7a93b71fcf10d84cc576b7ab (patch) | |
tree | 80ae19fde474f9888ecb88dce693f74647712719 /src/Network/Tox/Session.hs | |
parent | fe6e8bf7cd367afe785ed50914bc2a20d272e546 (diff) |
Fixed handling of lossy outgoing in Network.Lossless.
Diffstat (limited to 'src/Network/Tox/Session.hs')
-rw-r--r-- | src/Network/Tox/Session.hs | 29 |
1 files changed, 21 insertions, 8 deletions
diff --git a/src/Network/Tox/Session.hs b/src/Network/Tox/Session.hs index 88221b11..7b84ba80 100644 --- a/src/Network/Tox/Session.hs +++ b/src/Network/Tox/Session.hs | |||
@@ -15,7 +15,7 @@ import Network.Lossless | |||
15 | import Network.QueryResponse | 15 | import Network.QueryResponse |
16 | import Network.SessionTransports | 16 | import Network.SessionTransports |
17 | import Network.Tox.Crypto.Transport | 17 | import Network.Tox.Crypto.Transport |
18 | import Network.Tox.DHT.Transport (Cookie) | 18 | import Network.Tox.DHT.Transport (Cookie,key2id) |
19 | import Network.Tox.Handshake | 19 | import Network.Tox.Handshake |
20 | 20 | ||
21 | type SessionKey = SecretKey | 21 | type SessionKey = SecretKey |
@@ -69,7 +69,7 @@ plainHandshakeH sp saddr skey handshake = do | |||
69 | dput XNetCrypto $ prelude ++ "handshake: " ++ show (otherCookie hd, baseNonce hd) | 69 | dput XNetCrypto $ prelude ++ "handshake: " ++ show (otherCookie hd, baseNonce hd) |
70 | sent <- spGetSentHandshake sp skey saddr (handshakeCookie handshake) (otherCookie hd) | 70 | sent <- spGetSentHandshake sp skey saddr (handshakeCookie handshake) (otherCookie hd) |
71 | -- TODO: this is always returning sent = Nothing | 71 | -- TODO: this is always returning sent = Nothing |
72 | dput XNetCrypto $ prelude ++ "plainHandshakeH: cached outgoing: " ++ show (fmap (baseNonce . snd) sent) | 72 | dput XNetCrypto $ " <-- (cached) handshake baseNonce" ++ show (fmap (baseNonce . snd) sent) |
73 | forM_ sent $ \(hd_skey,hd_sent) -> do | 73 | forM_ sent $ \(hd_skey,hd_sent) -> do |
74 | sk <- SessionKeys (spCrypto sp) | 74 | sk <- SessionKeys (spCrypto sp) |
75 | hd_skey | 75 | hd_skey |
@@ -81,7 +81,9 @@ plainHandshakeH sp saddr skey handshake = do | |||
81 | forM_ m $ \(sid, t) -> do | 81 | forM_ m $ \(sid, t) -> do |
82 | (t2,resend,getMissing) | 82 | (t2,resend,getMissing) |
83 | <- lossless (\cp a -> return $ fmap (,a) $ checkLossless $ runIdentity $ pktData cp) | 83 | <- lossless (\cp a -> return $ fmap (,a) $ checkLossless $ runIdentity $ pktData cp) |
84 | (\seqno p _ -> encryptPacket sk $ bookKeeping seqno p) | 84 | (\seqno p _ -> do |
85 | y <- encryptPacket sk $ bookKeeping seqno p | ||
86 | return (lossyness (msgID p) == Lossy, y)) | ||
85 | () | 87 | () |
86 | t | 88 | t |
87 | let _ = t :: TransportA String () (CryptoPacket Identity) (CryptoPacket Encrypted) | 89 | let _ = t :: TransportA String () (CryptoPacket Identity) (CryptoPacket Encrypted) |
@@ -101,16 +103,22 @@ plainHandshakeH sp saddr skey handshake = do | |||
101 | 103 | ||
102 | decryptPacket :: SessionKeys -> SockAddr -> CryptoPacket Encrypted -> IO (Maybe (CryptoPacket Identity, ())) | 104 | decryptPacket :: SessionKeys -> SockAddr -> CryptoPacket Encrypted -> IO (Maybe (CryptoPacket Identity, ())) |
103 | decryptPacket sk saddr (CryptoPacket n16 ciphered) = do | 105 | decryptPacket sk saddr (CryptoPacket n16 ciphered) = do |
104 | (n,δ) <- atomically $ do | 106 | (n24,δ) <- atomically $ do |
105 | n <- readTVar (skNonceIncoming sk) | 107 | n <- readTVar (skNonceIncoming sk) |
106 | let δ = n16 - nonce24ToWord16 n | 108 | let δ = n16 - nonce24ToWord16 n |
107 | return ( n `addtoNonce24` fromIntegral δ, δ ) | 109 | return ( n `addtoNonce24` fromIntegral δ, δ ) |
108 | secret <- lookupSharedSecret (skCrypto sk) (skMe sk) (skThem sk) n | 110 | secret <- lookupSharedSecret (skCrypto sk) (skMe sk) (skThem sk) n24 |
109 | case decodePlain =<< decrypt secret ciphered of | 111 | case decodePlain =<< decrypt secret ciphered of |
110 | Left e -> return Nothing | 112 | Left e -> return Nothing |
111 | Right x -> do | 113 | Right x -> do |
112 | when ( δ > 43690 ) | 114 | when ( δ > 43690 ) |
113 | $ atomically $ writeTVar (skNonceIncoming sk) (n `addtoNonce24` 21845) | 115 | $ atomically $ writeTVar (skNonceIncoming sk) (n24 `addtoNonce24` 21845) |
116 | |||
117 | do let them = key2id $ skThem sk | ||
118 | CryptoData ack seqno _ = x | ||
119 | cm = decodeRawCryptoMsg x | ||
120 | dput XNetCrypto $ unwords [take 8 (show them),"-->",show (msgID cm),show (n24,ack,seqno)] | ||
121 | |||
114 | return $ Just ( CryptoPacket n16 (pure x), () ) | 122 | return $ Just ( CryptoPacket n16 (pure x), () ) |
115 | 123 | ||
116 | encryptPacket :: SessionKeys -> CryptoData -> IO (CryptoPacket Encrypted) | 124 | encryptPacket :: SessionKeys -> CryptoData -> IO (CryptoPacket Encrypted) |
@@ -121,12 +129,17 @@ encryptPacket sk plain = do | |||
121 | return n24 | 129 | return n24 |
122 | secret <- lookupSharedSecret (skCrypto sk) (skMe sk) (skThem sk) n24 | 130 | secret <- lookupSharedSecret (skCrypto sk) (skMe sk) (skThem sk) n24 |
123 | let ciphered = encrypt secret $ encodePlain $ plain | 131 | let ciphered = encrypt secret $ encodePlain $ plain |
132 | |||
133 | do let them = key2id $ skThem sk | ||
134 | CryptoData ack seqno cm = plain | ||
135 | dput XNetCrypto $ unwords [take 8 (show them),"<--",show (msgID cm),show (n24,ack,seqno)] | ||
136 | |||
124 | return $ CryptoPacket (nonce24ToWord16 n24) ciphered | 137 | return $ CryptoPacket (nonce24ToWord16 n24) ciphered |
125 | 138 | ||
126 | data SessionKeys = SessionKeys | 139 | data SessionKeys = SessionKeys |
127 | { skCrypto :: TransportCrypto | 140 | { skCrypto :: TransportCrypto |
128 | , skMe :: SecretKey | 141 | , skMe :: SecretKey -- My session key |
129 | , skThem :: PublicKey | 142 | , skThem :: PublicKey -- Their session key |
130 | , skNonceIncoming :: TVar Nonce24 -- +21845 when a threshold is reached. | 143 | , skNonceIncoming :: TVar Nonce24 -- +21845 when a threshold is reached. |
131 | , skNonceOutgoing :: TVar Nonce24 -- +1 on every packet | 144 | , skNonceOutgoing :: TVar Nonce24 -- +1 on every packet |
132 | } | 145 | } |