From 8275e9b026b9cce76c326938ed208990cce17587 Mon Sep 17 00:00:00 2001 From: joe Date: Mon, 4 Sep 2017 15:35:53 -0400 Subject: DHTPK has extra layer when sent as DHT Request Packet. --- DHTTransport.hs | 47 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/DHTTransport.hs b/DHTTransport.hs index 778390cf..3d008174 100644 --- a/DHTTransport.hs +++ b/DHTTransport.hs @@ -14,6 +14,7 @@ module DHTTransport , Pong(..) , GetNodes(..) , SendNodes(..) + , DHTPublicKey , CookieRequest , Cookie , DHTRequest @@ -149,15 +150,16 @@ data DHTRequest -- `1` `uint8_t` (0x01) -- `8` `uint64_t` random number (the same that was received in request) | NATPong Nonce8 - | DHTPK DHTPublicKey + | DHTPK LongTermKeyWrap instance Sized DHTRequest where size = VarSize $ \case NATPing _ -> 10 NATPong _ -> 10 - DHTPK dhtpk -> 41 + case size of - ConstSize nodes -> nodes - VarSize sznodes -> sznodes (dhtpkNodes dhtpk) + DHTPK wrap -> 1{-typ-} + 32{-key-} + 24{-nonce-} + + case size of + ConstSize n -> n + VarSize f -> f (wrapData wrap) instance Serialize DHTRequest where get = do @@ -172,6 +174,9 @@ instance Serialize DHTRequest where put (NATPong n) = put (0xfe01 :: Word16) >> put n put (DHTPK pk) = put (0x9c :: Word8) >> put pk +-- DHT public key packet: +-- (As Onion data packet?) +-- -- | Length | Contents | -- |:------------|:------------------------------------| -- | `1` | `uint8_t` (0x9c) | @@ -179,12 +184,40 @@ instance Serialize DHTRequest where -- | `32` | Our DHT public key | -- | `[39, 204]` | Maximum of 4 nodes in packed format | data DHTPublicKey = DHTPublicKey - { dhtpkNonce :: Nonce8 - , dhtpk :: PublicKey - , dhtpkNodes :: SendNodes + { dhtpkNonce :: Nonce8 -- no_replay + , dhtpk :: PublicKey -- dht public key + , dhtpkNodes :: SendNodes -- other reachable nodes + } + +-- When sent as a DHT request packet (this is the data sent in the DHT request +-- packet): +-- +-- Length Contents +-- :--------- :------------------------------- +-- `1` `uint8_t` (0x9c) +-- `32` Long term public key of sender +-- `24` Nonce +-- variable Encrypted payload +data LongTermKeyWrap = LongTermKeyWrap + { wrapLongTermKey :: PublicKey + , wrapNonce :: Nonce24 + , wrapData :: Encrypted DHTPublicKey } +instance Serialize LongTermKeyWrap where + get = LongTermKeyWrap <$> getPublicKey <*> get <*> get + put (LongTermKeyWrap key nonce dta) = putPublicKey key >> put nonce >> put dta + + +instance Sized DHTPublicKey where + -- NOTE: 41 bytes includes the 1-byte tag 0x9c in the size. + -- WARNING: Serialize instance does not include this byte FIXME + size = VarSize $ \(DHTPublicKey _ _ nodes) -> 41 + case size of + ConstSize nodes -> nodes + VarSize sznodes -> sznodes nodes + instance Serialize DHTPublicKey where + -- TODO: This should agree with Sized instance. get = DHTPublicKey <$> get <*> getPublicKey <*> get put (DHTPublicKey nonce key nodes) = do put nonce -- cgit v1.2.3