summaryrefslogtreecommitdiff
path: root/ToxMessage.hs
blob: 56758e4867b7c64e9fbb7dab4721468e14436395 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
{-# LANGUAGE PatternSynonyms            #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE DeriveFunctor, DeriveTraversable,DeriveDataTypeable #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE DataKinds, KindSignatures #-}
module ToxMessage where

import Debug.Trace
import Data.ByteString (ByteString)
import qualified Crypto.MAC.Poly1305 as Poly1305 (Auth(..))
import qualified Crypto.PubKey.Curve25519 as Curve25519
import Data.ByteArray as BA (ByteArrayAccess, Bytes)
import qualified Data.ByteArray as BA
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as C8
import qualified Data.ByteString.Base16 as Base16
import Data.Bits
import Data.Hashable
import Data.Bits.ByteString ()
import Data.Word
import Data.Data
import Data.Ord
import Data.Serialize
import Foreign.Ptr
import Foreign.Marshal.Alloc
import System.Endian
import Foreign.Storable
import GHC.TypeLits
import Data.Tuple

newtype Auth = Auth Poly1305.Auth
 deriving (Eq, ByteArrayAccess)

instance Ord Auth where
    compare (Auth a) (Auth b) = comparing (BA.convert :: Poly1305.Auth -> Bytes) a b

instance Data Auth where
    gfoldl k z x = z x

    -- Well, this is a little wonky... XXX
    gunfold k z c = k (z (Auth . Poly1305.Auth . (BA.convert :: ByteString -> Bytes)))

    toConstr _ = con_Auth

    dataTypeOf _ = mkDataType "ToxMessage" [con_Auth]

con_Auth = mkConstr (dataTypeOf (Auth (error "con_Auth"))) "Auth" [] Prefix

instance Serialize Auth where
    get = Auth . Poly1305.Auth . BA.convert <$> getBytes 16
    put (Auth (Poly1305.Auth bs)) = putByteString $ BA.convert bs
--
-- | An 'Aliased' 'PubKey' is one that is not the DHT key and so should not go
-- into the kademlia routing table buckets.
--
-- Note: This includes the long-term tox-id key that humans use to friend each
-- other and is often refered to as your "real public key" by the Tox
-- documents.  For the purposes of the DHT, it is an alias.
newtype Aliased a = Aliased a
 deriving (Eq,Ord,Show,Data,Functor,Foldable,Traversable)

newtype Nonce24 = Nonce24 ByteString
 deriving (Eq, Ord, ByteArrayAccess,Data)

quoted :: ShowS -> ShowS
quoted shows s = '"':shows ('"':s)

bin2hex :: ByteArrayAccess bs => bs -> String
bin2hex = C8.unpack . Base16.encode . BA.convert

instance Show Nonce24 where
    showsPrec d nonce = quoted (mappend $ bin2hex nonce)

instance Serialize Nonce24 where
    get = Nonce24 <$> getBytes 24
    put (Nonce24 bs) = putByteString bs

newtype Nonce8 = Nonce8 Word64
 deriving (Eq, Ord, Data, Serialize)

instance ByteArrayAccess Nonce8 where
    length _ = 8
    withByteArray (Nonce8 w64) kont =
        allocaBytes 8 $ \p -> do
            poke (castPtr p :: Ptr Word64) $ toBE64 w64
            kont p

instance Show Nonce8 where
    showsPrec d nonce = quoted (mappend $ bin2hex nonce)




-- TODO: This should probably be represented by Curve25519.PublicKey, but
-- ByteString has more instances...
newtype PubKey = PubKey ByteString
 deriving (Eq,Ord,Data, ByteArrayAccess, Bits, Hashable)

instance Serialize PubKey where
    get = PubKey <$> getBytes 32
    put (PubKey bs) = putByteString bs

instance Show PubKey where
    show (PubKey bs) = C8.unpack $ Base16.encode bs

instance FiniteBits PubKey where
    finiteBitSize _ = 256

instance Read PubKey where
    readsPrec _ str
        | (bs, xs) <- Base16.decode $ C8.pack str
        , B.length bs == 32
                    = [ (PubKey bs, drop 64 str) ]
        | otherwise = []



-- | A chunk of data encrypted with public-key cryptography.
data ImplicitAssymetric = ImplicitAssymetric
    { assymetricAuth  :: Auth
    , assymetricBytes :: ByteString
    }
 deriving (Eq, Ord,Data)

getRemaining = remaining >>= getBytes

instance Serialize ImplicitAssymetric where
    get = ImplicitAssymetric <$> get <*> getRemaining
    put (ImplicitAssymetric auth bs) = put auth >> putByteString bs

-- | Like ImplicitAssymetric, but includes the nonce used to encrypt.
data UnclaimedAssymetric = UnclaimedAssymetric
    { assymetricNonce :: Nonce24
    , assymetricData  :: !ImplicitAssymetric
    }
 deriving (Eq, Ord, Data)

instance Serialize UnclaimedAssymetric where
    get = UnclaimedAssymetric <$> get <*> get
    put (UnclaimedAssymetric nonce dta) = put nonce >> put dta

-- | Like UnclaimedAssymetric, but includes the public key of the sender.
data Assymetric = Assymetric
    { senderKey :: PubKey
    , sent      :: !UnclaimedAssymetric
    }
 deriving (Eq, Ord,Data)

-- get requires isolate.
-- sender key, then nonce
instance Serialize Assymetric where
    get = Assymetric <$> get <*> get
    put (Assymetric key dta) = put key >> put dta

-- Aliased packets have the sender key and nonce reversed.
instance Serialize (Aliased Assymetric) where
    get = do
        nonce <- get
        key <- get
        dta <- get
        return $ Aliased (Assymetric key (UnclaimedAssymetric nonce dta))

    put (Aliased (Assymetric key (UnclaimedAssymetric nonce dta))) = do
        put nonce
        put key
        put dta


newtype Cookie = Cookie UnclaimedAssymetric
 deriving (Eq, Ord,Data)

newtype ReturnPath (n::Nat) = ReturnPath ByteString
 deriving (Eq, Ord,Data)

emptyReturnPath :: ReturnPath 0
emptyReturnPath = ReturnPath B.empty

instance KnownNat n => Serialize (ReturnPath n) where
    -- Size: 59 = 1(family) + 16(ip) + 2(port) +16(mac) + 24(nonce)
    get = ReturnPath <$> getBytes ( 59 * (fromIntegral $ natVal $ Proxy @n) )
    put (ReturnPath bs) = putByteString bs

data Symmetric = Symmetric
    { symmetricNonce :: Nonce24
    , symmetricAuth  :: Auth
    , symmetricBytes :: ByteString
    }
 deriving (Eq, Ord,Data)

data Packet where
    Ping          :: Assymetric -> Packet -- 0x00 -- Assymetric query
    Pong          :: Assymetric -> Packet -- 0x01 -- Assymetric response

    GetNodes      :: Assymetric -> Packet -- 0x02 -- Assymetric query
    SendNodes     :: Assymetric -> Packet -- 0x04 -- Assymetric response

    CookieRequest :: Assymetric -> Packet -- 0x18
    CookieResponse :: UnclaimedAssymetric -> Packet -- 0x19

    OnionRequest0 :: Assymetric -> Packet -- 0x80


    CryptoHandshake :: Cookie -> UnclaimedAssymetric -> Packet -- 0x1a

    CryptoData :: Word16 -> ImplicitAssymetric -> Packet -- 0x1b

    DHTRequest :: PubKey -> Assymetric -> Packet -- 0x20 -- Sometimes Assymetric query

    DataToRoute :: PubKey -> Aliased Assymetric -> ReturnPath 3 -> Packet
    DataToRouteResponse :: Aliased Assymetric -> Packet

    LanDiscovery :: PubKey -> Packet  -- 0x21

    OnionRequest1 :: Aliased Assymetric -> Symmetric -> Packet -- 0x81
    OnionRequest2 :: Aliased Assymetric -> Symmetric -> Packet -- 0x82

    OnionRequest3 :: ByteString -> Symmetric -> Packet -- 0x82

    -- TODO:
    --   Announce has a return path appended to it.
    --   We'll need a type-level natural to indicate how may bytes to
    --   get in the ImplicitAssymetric data.  (Or we could force the size
    --   of the return path).
    --
    --   If the return path is non empty, we should respond with OnionResponse3
    --   rather than AnnounceResponse directly.
    Announce :: Aliased Assymetric -> ReturnPath 3 -> Packet --0x83
    AnnounceResponse :: Nonce8 -> UnclaimedAssymetric -> Packet -- 0x84

    OnionResponse3 :: ReturnPath 3 -> Packet -> Packet -- 0x8c
    OnionResponse2 :: Symmetric -> ByteString -> Packet -- 0x8d
    OnionResponse1 :: Symmetric -> ByteString -> Packet -- 0x8e


    GetBootstrapInfo :: ByteString -> Packet -- 0xf0 + 77 bytes                         -- ByteString query
    BootstrapInfo :: Word32 -> ByteString -> Packet -- 0xf0 + version + (256 byte motd) -- ByteSTring response

 deriving (Eq, Ord,Data)

class KnownNat n => OnionPacket n where
    mkOnion :: ReturnPath n -> Packet -> Packet

instance OnionPacket 0 where mkOnion _ = id
instance OnionPacket 3 where mkOnion = OnionResponse3

newtype PacketKind = PacketKind Word8
 deriving (Eq, Ord, Serialize)

-- TODO: Auth fail:
pattern OnionRequest0Type = PacketKind 128 -- 0x80     Onion Request 0
pattern OnionRequest1Type = PacketKind 129 -- 0x81     Onion Request 1
pattern OnionRequest2Type = PacketKind 130 -- 0x82     Onion Request 2
pattern AnnounceType  = PacketKind 131 -- 0x83     Announce Request
pattern AnnounceResponseType = PacketKind 132 -- 0x84     Announce Response
-- 0x85     Onion Data Request (data to route request packet)
-- 0x86     Onion Data Response (data to route response packet)
-- 0x8c     Onion Response 3
-- 0x8d     Onion Response 2
pattern OnionResponse3Type = PacketKind 140 -- 0x8c     Onion Response 3
pattern OnionResponse2Type = PacketKind 141 -- 0x8d     Onion Response 2
pattern OnionResponse1Type = PacketKind 142 -- 0x8e     Onion Response 1
-- 0xf0     Bootstrap Info

--  TODO Fix these fails...
--  GetNodesType decipherAndAuth: auth fail
--  MessageType 128 decipherAndAuth: auth fail
--  MessageType 129 decipherAndAuth: auth fail
--  MessageType 130 decipherAndAuth: auth fail
--  MessageType 131 decipherAndAuth: auth fail
--  MessageType 32 decipherAndAuth: auth fail

-- TODO: Auth fail:
pattern DHTRequestType = PacketKind 32 -- 0x20     DHT Request

pattern PingType      = PacketKind 0 -- 0x00     Ping Request
pattern PongType      = PacketKind 1 -- 0x01     Ping Response
pattern GetNodesType  = PacketKind 2 -- 0x02     Nodes Request
pattern SendNodesType = PacketKind 4 -- 0x04     Nodes Response


instance Show PacketKind where
    showsPrec d PingType = mappend "PingType"
    showsPrec d PongType = mappend "PongType"
    showsPrec d GetNodesType = mappend "GetNodesType"
    showsPrec d SendNodesType = mappend "SendNodesType"
    showsPrec d DHTRequestType = mappend "DHTRequestType"
    showsPrec d OnionRequest0Type = mappend "OnionRequest0"
    showsPrec d OnionResponse1Type = mappend "OnionResponse1"
    showsPrec d OnionResponse3Type = mappend "OnionResponse3"
    showsPrec d AnnounceType = mappend "AnnounceType"
    showsPrec d (PacketKind x) = mappend "PacketKind " . showsPrec (d+1) x

pktKind :: Packet -> PacketKind

-- These are (Assymetric -> Assymetric) queries.
pktKind Ping             {} = PacketKind 0x00
pktKind Pong             {} = PacketKind 0x01
pktKind GetNodes         {} = PacketKind 0x02
pktKind SendNodes        {} = PacketKind 0x04


-- This is a (Assymetric -> UnclaimedAssymetric) query
pktKind CookieRequest    {} = PacketKind 0x18
pktKind CookieResponse   {} = PacketKind 0x19

-- Query (Assymetric -> (Nonce8,UnclaimedAssymetric))
pktKind Announce         {} = PacketKind 0x83
pktKind AnnounceResponse {} = PacketKind 0x84

-- Query (Assymetric -> ByteString)
pktKind OnionRequest0    {} = PacketKind 0x80


-- This is a (ByteString -> ByteString) query
pktKind GetBootstrapInfo {} = PacketKind 0xf0
pktKind BootstrapInfo    {} = PacketKind 0xf0


-- Trigering event. No direct response. (PubKey -> ())
pktKind LanDiscovery     {} = PacketKind 0x21

-- Two cases:
--  Half-established:    (Cookie,UnclaimedAssymetric) -> (Cookie,UnclaimedAssymetric)
--  Session established: (Cookie,UnclaimedAssymetric) -> (Word16,ImplicitAssymetric)
pktKind CryptoHandshake  {} = PacketKind 0x1a

-- Session data, no direct response.
--   (reponse to CryptoHandshake, or other data)
pktKind CryptoData       {} = PacketKind 0x1b

-- Two cases:
--    ( (PubKey, Assymetric) -> response )
--    ( (PubKey, Assymetric) -> ()       )
pktKind DHTRequest       {} = PacketKind 0x20


-- Query ( (PubKey,Aliased Assymetric) -> Aliased Assymetric)
pktKind DataToRoute      {}    = PacketKind 0x85
pktKind DataToRouteResponse {} = PacketKind 0x86

-- 3 Queries ( (Aliased Assymetric, Symmetric )
--            -> ( Symmetric, ByteString )     )
pktKind OnionRequest1    {} = PacketKind 0x81
pktKind OnionResponse1   {} = PacketKind 0x8e

pktKind OnionRequest2    {} = PacketKind 0x82
pktKind OnionResponse2   {} = PacketKind 0x8d

pktKind OnionRequest3    {} = PacketKind 0x82
pktKind OnionResponse3   {} = PacketKind 0x8c

data PacketClass =
    AssymetricClass (Assymetric -> Packet) (Packet -> Assymetric)
    | forall n. OnionPacket n => AliasedClass ((Aliased Assymetric,ReturnPath n) -> Packet) (Packet -> (Aliased Assymetric,ReturnPath n))
    | forall n. OnionPacket n => OnionClass ((Packet,ReturnPath n) -> Packet) (Packet -> (Packet,ReturnPath n))
    | NoncedUnclaimedClass (Nonce8 -> UnclaimedAssymetric -> Packet)
                           (Packet -> (Nonce8, UnclaimedAssymetric))
    | Unclassified
{-
data Packet' where
    :: Assymetric -> Packet
    :: UnclaimedAssymetric -> Packet
    :: Word16 -> ImplicitAssymetric -> Packet
    :: PubKey -> Assymetric -> Packet
    :: PubKey -> Packet
    :: Aliased Assymetric -> Symmetric -> Packet
    :: ByteString -> Symmetric -> Packet
    :: Aliased Assymetric -> Packet
    :: Symmetric -> ByteString -> Packet
    :: ByteString -> Packet
    :: Word32 -> ByteString -> Packet
-}

pktClass :: PacketKind -> PacketClass
pktClass (PacketKind 0) = AssymetricClass Ping (\(Ping a) -> a)
pktClass (PacketKind 1) = AssymetricClass Pong (\(Pong a) -> a)
pktClass (PacketKind 2) = AssymetricClass GetNodes (\(GetNodes a) -> a)
pktClass (PacketKind 4) = AssymetricClass SendNodes (\(SendNodes a) -> a)

pktClass (PacketKind 0x18) = AssymetricClass CookieRequest (\(CookieRequest a) -> a)
pktClass (PacketKind 0x80) = AssymetricClass OnionRequest0 (\(OnionRequest0 a) -> a)
-- pktClass (PacketKind 0x86) = AssymetricClass (DataToRouteResponse . Aliased) (\(DataToRouteResponse (Aliased a)) -> a)

pktClass (PacketKind 0x83) = AliasedClass (uncurry Announce) (\(Announce a r)-> (a,r))
pktClass (PacketKind 0x84) = NoncedUnclaimedClass AnnounceResponse (\(AnnounceResponse n8 uncl)-> (n8,uncl))

pktClass (PacketKind 0x8c) = OnionClass (uncurry OnionResponse3 . swap) (\(OnionResponse3 r a)-> (a,r))

pktClass _ = Unclassified


instance Serialize Packet where
    get = getPacket
    put = putPacket

getPacket = do
    typ <- get
    case pktClass typ of
        AssymetricClass toPacket _      -> toPacket <$> get
        AliasedClass    toPacket _      -> do
            trace ("PARSE "++show typ) $ return ()
            cnt <- remaining
            a <- isolate (cnt - 59*3) get
            r <- get
            trace ("PARSED "++show typ) $ return ()
            return $ toPacket (a,r)
        OnionClass      toPacket _      -> do
            trace ("ONION-PARSE "++show typ) $ return ()
            p <- get
            trace ("ONION-PARSED "++show typ) $ return ()
            return $ toPacket p
        NoncedUnclaimedClass toPacket _ -> toPacket <$> get <*> get
        Unclassified -> fail $ "todo: unserialize packet "++show typ

putPacket p = do
    put $ pktKind p
    case pktClass (pktKind p) of
        AssymetricClass _ fromPacket      -> put $ fromPacket p
        AliasedClass    _ fromPacket      -> put $ fromPacket p
        OnionClass      _ fromPacket      -> put $ swap $ fromPacket p
        NoncedUnclaimedClass _ fromPacket -> put $ fromPacket p -- putting a pair.
        Unclassified -> fail $ "todo: serialize packet "++show (pktKind p)