diff options
Diffstat (limited to 'src/Network/Tox')
-rw-r--r-- | src/Network/Tox/Crypto/Transport.hs | 29 |
1 files changed, 6 insertions, 23 deletions
diff --git a/src/Network/Tox/Crypto/Transport.hs b/src/Network/Tox/Crypto/Transport.hs index 4cf6c7a5..09c492ef 100644 --- a/src/Network/Tox/Crypto/Transport.hs +++ b/src/Network/Tox/Crypto/Transport.hs | |||
@@ -2,6 +2,7 @@ | |||
2 | {-# LANGUAGE ViewPatterns #-} | 2 | {-# LANGUAGE ViewPatterns #-} |
3 | {-# LANGUAGE FlexibleInstances #-} | 3 | {-# LANGUAGE FlexibleInstances #-} |
4 | {-# LANGUAGE LambdaCase #-} | 4 | {-# LANGUAGE LambdaCase #-} |
5 | {-# LANGUAGE TupleSections #-} | ||
5 | module Network.Tox.Crypto.Transport | 6 | module Network.Tox.Crypto.Transport |
6 | ( parseNetCrypto | 7 | ( parseNetCrypto |
7 | , encodeNetCrypto | 8 | , encodeNetCrypto |
@@ -50,6 +51,7 @@ import Control.Lens | |||
50 | import Data.Text as T | 51 | import Data.Text as T |
51 | import Data.Text.Encoding as T | 52 | import Data.Text.Encoding as T |
52 | import Data.Serialize as S | 53 | import Data.Serialize as S |
54 | import Control.Arrow | ||
53 | 55 | ||
54 | 56 | ||
55 | data NetCrypto | 57 | data NetCrypto |
@@ -57,10 +59,12 @@ data NetCrypto | |||
57 | | NetCrypto (CryptoPacket Encrypted) | 59 | | NetCrypto (CryptoPacket Encrypted) |
58 | 60 | ||
59 | parseNetCrypto :: ByteString -> SockAddr -> Either String (NetCrypto, SockAddr) | 61 | parseNetCrypto :: ByteString -> SockAddr -> Either String (NetCrypto, SockAddr) |
60 | parseNetCrypto pkt saddr = Left "TODO: parseNetCrypto" | 62 | parseNetCrypto pkt@(B.uncons -> Just (0x1a,_)) saddr = left ("parseNetCrypto: "++) $ (,saddr) . NetHandshake <$> runGet get pkt |
63 | parseNetCrypto pkt@(B.uncons -> Just (0x1b,_)) saddr = left ("parseNetCrypto: "++) $ (,saddr) . NetCrypto <$> runGet get pkt | ||
61 | 64 | ||
62 | encodeNetCrypto :: NetCrypto -> SockAddr -> (ByteString, SockAddr) | 65 | encodeNetCrypto :: NetCrypto -> SockAddr -> (ByteString, SockAddr) |
63 | encodeNetCrypto _ _ = _todo | 66 | encodeNetCrypto (NetHandshake x) saddr = (B.cons 0x1a (runPut $ put x),saddr) |
67 | encodeNetCrypto (NetCrypto x) saddr = (B.cons 0x1b (runPut $ put x),saddr) | ||
64 | 68 | ||
65 | data Handshake (f :: * -> *) = Handshake | 69 | data Handshake (f :: * -> *) = Handshake |
66 | { handshakeCookie :: Cookie | 70 | { handshakeCookie :: Cookie |
@@ -940,16 +944,6 @@ data MessageName = Ping -- 0x00 | |||
940 | -- [uint64_t echo id (must be sent back untouched in cookie response)] | 944 | -- [uint64_t echo id (must be sent back untouched in cookie response)] |
941 | -- ] | 945 | -- ] |
942 | 946 | ||
943 | -- Handshake packet: | ||
944 | -- [uint8_t 26] (0x1a) | ||
945 | -- [Cookie] | ||
946 | -- [nonce (24 bytes)] | ||
947 | -- [Encrypted message containing: | ||
948 | -- [24 bytes base nonce] | ||
949 | -- [session public key of the peer (32 bytes)] | ||
950 | -- [sha512 hash of the entire Cookie sitting outside the encrypted part] | ||
951 | -- [Other Cookie (used by the other to respond to the handshake packet)] | ||
952 | -- ] | ||
953 | 947 | ||
954 | -- cookie response packet (161 bytes): | 948 | -- cookie response packet (161 bytes): |
955 | -- | 949 | -- |
@@ -973,15 +967,4 @@ data MessageName = Ping -- 0x00 | |||
973 | -- should be (NodeId -> Secret) and the cookie-request map should be | 967 | -- should be (NodeId -> Secret) and the cookie-request map should be |
974 | -- (SockAddr -> NodeId) | 968 | -- (SockAddr -> NodeId) |
975 | 969 | ||
976 | -- Encrypted packets: | ||
977 | -- | ||
978 | -- Length Contents | ||
979 | -- :---------:-------------------------------------------------------------- | ||
980 | -- `1` `uint8_t` (0x1b) | ||
981 | -- `2` `uint16_t` The last 2 bytes of the nonce used to encrypt this | ||
982 | -- variable Payload | ||
983 | -- | ||
984 | -- The payload is encrypted with the session key and 'base nonce' set by the | ||
985 | -- receiver in their handshake + packet number (starting at 0, big endian math). | ||
986 | |||
987 | 970 | ||