diff options
author | James Crayne <jim.crayne@gmail.com> | 2017-11-04 09:36:40 +0000 |
---|---|---|
committer | James Crayne <jim.crayne@gmail.com> | 2017-11-19 23:40:14 +0000 |
commit | f9d32e34a71e91bc2cd09eb23a0c3a06b2f66a35 (patch) | |
tree | 019798855d7d8614fe50bad98f6c092333402c27 /src/Network | |
parent | 3834bef0fe30d6d640368da0d8ce9f8784d52c00 (diff) |
newHandShakeData
Diffstat (limited to 'src/Network')
-rw-r--r-- | src/Network/Tox/Crypto/Handlers.hs | 67 |
1 files changed, 50 insertions, 17 deletions
diff --git a/src/Network/Tox/Crypto/Handlers.hs b/src/Network/Tox/Crypto/Handlers.hs index 7cfbdd4e..13db02f3 100644 --- a/src/Network/Tox/Crypto/Handlers.hs +++ b/src/Network/Tox/Crypto/Handlers.hs | |||
@@ -2,8 +2,10 @@ | |||
2 | {-# LANGUAGE TupleSections #-} | 2 | {-# LANGUAGE TupleSections #-} |
3 | module Network.Tox.Crypto.Handlers where | 3 | module Network.Tox.Crypto.Handlers where |
4 | 4 | ||
5 | import Network.Tox.NodeId | ||
5 | import Network.Tox.Crypto.Transport | 6 | import Network.Tox.Crypto.Transport |
6 | import Network.Tox.DHT.Transport (Cookie(..),CookieData(..)) | 7 | import Network.Tox.DHT.Transport (Cookie(..),CookieData(..), CookieRequest(..)) |
8 | import Network.Tox.DHT.Handlers (Client, cookieRequest, cookieRequestH ) | ||
7 | import Crypto.Tox | 9 | import Crypto.Tox |
8 | import Control.Concurrent.STM | 10 | import Control.Concurrent.STM |
9 | import Network.Address | 11 | import Network.Address |
@@ -88,7 +90,10 @@ data NetCryptoSessions = NCSessions { netCryptoSessions :: TVar (Map.Map SockAdd | |||
88 | , msgTypeArray :: MsgTypeArray | 90 | , msgTypeArray :: MsgTypeArray |
89 | } | 91 | } |
90 | 92 | ||
91 | newSessionsState :: TransportCrypto -> (MessageType -> NetCryptoHook) -> Map.Map MessageType [NetCryptoHook] -> IO NetCryptoSessions | 93 | newSessionsState :: TransportCrypto |
94 | -> (MessageType -> NetCryptoHook) -- ^ default hook | ||
95 | -> Map.Map MessageType [NetCryptoHook] -- ^ all hooks, can be empty to start | ||
96 | -> IO NetCryptoSessions | ||
92 | newSessionsState crypto unrechook hooks = do | 97 | newSessionsState crypto unrechook hooks = do |
93 | x <- atomically $ newTVar Map.empty | 98 | x <- atomically $ newTVar Map.empty |
94 | nick <- atomically $ newTVar B.empty | 99 | nick <- atomically $ newTVar B.empty |
@@ -120,14 +125,39 @@ newSessionsState crypto unrechook hooks = do | |||
120 | data HandshakeParams | 125 | data HandshakeParams |
121 | = HParam | 126 | = HParam |
122 | { hpTheirBaseNonce :: Maybe Nonce24 -- ignore and generate your own | 127 | { hpTheirBaseNonce :: Maybe Nonce24 -- ignore and generate your own |
123 | , hpOtherCookie :: Maybe Cookie | 128 | , hpOtherCookie :: Cookie |
124 | , hpTheirSessionKeyPublic :: PublicKey | 129 | , hpTheirSessionKeyPublic :: PublicKey |
125 | , hpMySecretKey :: SecretKey | 130 | , hpMySecretKey :: SecretKey |
126 | , hpCookieRemotePubkey :: PublicKey | 131 | , hpCookieRemotePubkey :: PublicKey |
127 | , hpCookieRemoteDhtkey :: PublicKey | 132 | , hpCookieRemoteDhtkey :: PublicKey |
128 | } | 133 | } |
129 | newHandShakeData :: TransportCrypto -> HandshakeParams -> HandshakeData | 134 | newHandShakeData :: TransportCrypto -> Nonce24 -> HandshakeParams -> SockAddr -> IO (Maybe HandshakeData) |
130 | newHandShakeData = error "todo" | 135 | newHandShakeData crypto basenonce (HParam {hpOtherCookie,hpMySecretKey,hpCookieRemotePubkey,hpCookieRemoteDhtkey}) addr |
136 | = do | ||
137 | freshCookie | ||
138 | <- case nodeInfo (key2id hpCookieRemoteDhtkey) addr of | ||
139 | Right nodeinfo -> Just <$> cookieRequestH crypto nodeinfo (CookieRequest hpCookieRemotePubkey) | ||
140 | Left er -> return Nothing | ||
141 | let hinit = hashInit | ||
142 | Cookie n24 encrypted = hpOtherCookie | ||
143 | hctx = hashUpdate hinit n24 | ||
144 | hctx' = hashUpdate hctx encrypted | ||
145 | digest = hashFinalize hctx' | ||
146 | -- parameters addr {--> SockAddr -} | ||
147 | -- mbcookie <- case hpOtherCookie of | ||
148 | -- Nothing -> case (nodeInfo hpCookieRemoteDhtkey addr) of | ||
149 | -- Right nodeinfo -> cookieRequest crypto netCryptoDHTClient (toPublic hpMySecretKey) nodeinfo | ||
150 | -- Left er -> return Nothing | ||
151 | -- Just c -> return (Just c) | ||
152 | |||
153 | return $ | ||
154 | fmap (\freshCookie' -> | ||
155 | HandshakeData | ||
156 | { baseNonce = basenonce | ||
157 | , sessionKey = toPublic hpMySecretKey | ||
158 | , cookieHash = digest | ||
159 | , otherCookie = freshCookie' | ||
160 | }) freshCookie | ||
131 | 161 | ||
132 | -- | called when we recieve a crypto handshake with valid cookie | 162 | -- | called when we recieve a crypto handshake with valid cookie |
133 | freshCryptoSession :: NetCryptoSessions -> SockAddr -> HandshakeParams -> IO () | 163 | freshCryptoSession :: NetCryptoSessions -> SockAddr -> HandshakeParams -> IO () |
@@ -135,7 +165,7 @@ freshCryptoSession sessions | |||
135 | addr | 165 | addr |
136 | hp@(HParam | 166 | hp@(HParam |
137 | { hpTheirBaseNonce = Just theirBaseNonce | 167 | { hpTheirBaseNonce = Just theirBaseNonce |
138 | , hpOtherCookie = Just otherCookie | 168 | , hpOtherCookie = otherCookie |
139 | , hpTheirSessionKeyPublic = theirSessionKey | 169 | , hpTheirSessionKeyPublic = theirSessionKey |
140 | , hpMySecretKey = key | 170 | , hpMySecretKey = key |
141 | , hpCookieRemotePubkey = remotePublicKey | 171 | , hpCookieRemotePubkey = remotePublicKey |
@@ -147,15 +177,18 @@ freshCryptoSession sessions | |||
147 | ncTheirBaseNonce0 <- atomically $ newTVar theirBaseNonce | 177 | ncTheirBaseNonce0 <- atomically $ newTVar theirBaseNonce |
148 | n24 <- atomically $ transportNewNonce crypto | 178 | n24 <- atomically $ transportNewNonce crypto |
149 | state <- lookupSharedSecret crypto key remoteDhtPublicKey n24 | 179 | state <- lookupSharedSecret crypto key remoteDhtPublicKey n24 |
150 | let myhandshakeData = newHandShakeData crypto hp | 180 | newBaseNonce <- atomically $ transportNewNonce crypto |
151 | plain = encodePlain myhandshakeData | 181 | mbMyhandshakeData <- newHandShakeData crypto newBaseNonce hp addr |
152 | encrypted = encrypt state plain | 182 | let encodeHandshake myhandshakeData = let plain = encodePlain myhandshakeData |
153 | myhandshake = Handshake { handshakeCookie = otherCookie | 183 | -- state = computeSharedSecret key remoteDhtPublicKey n24 |
154 | , handshakeNonce = n24 | 184 | encrypted = encrypt state plain |
155 | , handshakeData = encrypted | 185 | in Handshake { handshakeCookie = otherCookie |
156 | } | 186 | , handshakeNonce = n24 |
157 | ncMyPacketNonce0 <- atomically $ newTVar (baseNonce myhandshakeData) | 187 | , handshakeData = encrypted |
158 | ncHandShake0 <- atomically $ newTVar (Just myhandshake) | 188 | } |
189 | let myhandshake= encodeHandshake <$> mbMyhandshakeData | ||
190 | ncMyPacketNonce0 <- atomically $ newTVar newBaseNonce | ||
191 | ncHandShake0 <- atomically $ newTVar myhandshake | ||
159 | cookie0 <- atomically $ newTVar (Just otherCookie) | 192 | cookie0 <- atomically $ newTVar (Just otherCookie) |
160 | newsession <- generateSecretKey | 193 | newsession <- generateSecretKey |
161 | ncHooks0 <- atomically $ newTVar (defaultHooks sessions) | 194 | ncHooks0 <- atomically $ newTVar (defaultHooks sessions) |
@@ -230,7 +263,7 @@ cryptoNetHandler sessions addr (NetHandshake (Handshake (Cookie n24 ecookie) non | |||
230 | return | 263 | return |
231 | HParam | 264 | HParam |
232 | { hpTheirBaseNonce = Just baseNonce | 265 | { hpTheirBaseNonce = Just baseNonce |
233 | , hpOtherCookie = Just otherCookie | 266 | , hpOtherCookie = otherCookie |
234 | , hpTheirSessionKeyPublic = sessionKey | 267 | , hpTheirSessionKeyPublic = sessionKey |
235 | , hpMySecretKey = key | 268 | , hpMySecretKey = key |
236 | , hpCookieRemotePubkey = remotePubkey | 269 | , hpCookieRemotePubkey = remotePubkey |
@@ -240,7 +273,7 @@ cryptoNetHandler sessions addr (NetHandshake (Handshake (Cookie n24 ecookie) non | |||
240 | Left _ -> return () | 273 | Left _ -> return () |
241 | Right hp@(HParam | 274 | Right hp@(HParam |
242 | { hpTheirBaseNonce = Just theirBaseNonce | 275 | { hpTheirBaseNonce = Just theirBaseNonce |
243 | , hpOtherCookie = Just otherCookie | 276 | , hpOtherCookie = otherCookie |
244 | , hpTheirSessionKeyPublic = theirSessionKey | 277 | , hpTheirSessionKeyPublic = theirSessionKey |
245 | , hpMySecretKey = key | 278 | , hpMySecretKey = key |
246 | , hpCookieRemotePubkey = remotePublicKey | 279 | , hpCookieRemotePubkey = remotePublicKey |