summaryrefslogtreecommitdiff
path: root/dht/src/Data/Tox/Onion.hs
diff options
context:
space:
mode:
authorJoe Crayne <joe@jerkface.net>2020-01-10 02:51:51 -0500
committerJoe Crayne <joe@jerkface.net>2020-01-10 02:51:51 -0500
commit8df4213da5b8ff9faff6194a06bd2c9c00dbad16 (patch)
tree53b234d79175a28a0b36aae11a34a5b395df2376 /dht/src/Data/Tox/Onion.hs
parent8ddaf16880b3dcc8cb30a36c46c7edd1f9fe4b3c (diff)
First successful TCP relay mediated chat link!
Diffstat (limited to 'dht/src/Data/Tox/Onion.hs')
-rw-r--r--dht/src/Data/Tox/Onion.hs35
1 files changed, 24 insertions, 11 deletions
diff --git a/dht/src/Data/Tox/Onion.hs b/dht/src/Data/Tox/Onion.hs
index d6f747d9..86fc71f4 100644
--- a/dht/src/Data/Tox/Onion.hs
+++ b/dht/src/Data/Tox/Onion.hs
@@ -63,6 +63,8 @@ import Data.Word64Map (fitsInInt)
63import Data.Bits (shiftR,shiftL) 63import Data.Bits (shiftR,shiftL)
64import qualified Rank2 64import qualified Rank2
65import Util (sameAddress) 65import Util (sameAddress)
66import Text.XXD
67import qualified Data.ByteArray as BA
66 68
67type HandleLo a = Arrival String SockAddr ByteString -> IO a 69type HandleLo a = Arrival String SockAddr ByteString -> IO a
68 70
@@ -583,6 +585,7 @@ handleOnionRequest :: forall a proxy n.
583 , KnownPeanoNat n 585 , KnownPeanoNat n
584 , Sized (ReturnPath n) 586 , Sized (ReturnPath n)
585 , Typeable n 587 , Typeable n
588 , Typeable (ThreeMinus (S n))
586 ) => proxy n -> TransportCrypto -> (forall x. x -> Addressed x) -> UDPTransport -> IO a -> OnionRequest n -> IO a 589 ) => proxy n -> TransportCrypto -> (forall x. x -> Addressed x) -> UDPTransport -> IO a -> OnionRequest n -> IO a
587handleOnionRequest proxy crypto saddr udp kont (OnionRequest nonce msg rpath) = do 590handleOnionRequest proxy crypto saddr udp kont (OnionRequest nonce msg rpath) = do
588 let n = peanoVal rpath 591 let n = peanoVal rpath
@@ -615,7 +618,7 @@ peelSymmetric :: Serialize (Addressed (ReturnPath n))
615peelSymmetric sym (ReturnPath nonce e) = decryptSymmetric sym nonce e >>= decodePlain 618peelSymmetric sym (ReturnPath nonce e) = decryptSymmetric sym nonce e >>= decodePlain
616 619
617 620
618peelOnion :: Serialize (Addressed (Forwarding n t)) 621peelOnion :: ( Typeable n, Typeable t, Serialize (Addressed (Forwarding n t)))
619 => TransportCrypto 622 => TransportCrypto
620 -> Nonce24 623 -> Nonce24
621 -> Forwarding (S n) t 624 -> Forwarding (S n) t
@@ -766,11 +769,10 @@ data OnionData
766 769
767instance Sized OnionData where 770instance Sized OnionData where
768 size = VarSize $ \case 771 size = VarSize $ \case
769 OnionDHTPublicKey dhtpk -> case size of 772 OnionDHTPublicKey dhtpk -> 1 + case size of
770 ConstSize n -> n -- Override because OnionData probably 773 ConstSize n -> n -- Override because OnionData probably
771 -- should be treated as variable sized. 774 -- should be treated as variable sized.
772 VarSize f -> f dhtpk 775 VarSize f -> f dhtpk
773 -- FIXME: inconsitantly, we have to add in the tag byte for this case.
774 OnionFriendRequest req -> 1 + case size of 776 OnionFriendRequest req -> 1 + case size of
775 ConstSize n -> n 777 ConstSize n -> n
776 VarSize f -> f req 778 VarSize f -> f req
@@ -824,9 +826,15 @@ decrypt crypto msg addr = do
824 -> (IO ∘ Either String ∘ Identity) a 826 -> (IO ∘ Either String ∘ Identity) a
825 decipher = (\n -> decipher1 crypto skey n . left (senderkey addr)) 827 decipher = (\n -> decipher1 crypto skey n . left (senderkey addr))
826 foo <- sequenceMessage $ transcode decipher msg 828 foo <- sequenceMessage $ transcode decipher msg
827 return $ do 829 let result = do
828 msg <- sequenceMessage foo 830 msg <- sequenceMessage foo
829 Right (msg, addr) 831 Right (msg, addr)
832 case msg of
833 OnionToRouteResponse {} -> case result of
834 Left e -> dput XOnion $ "Error decrypting data-to-route response: " ++ e
835 Right m -> dput XOnion $ "Decrypted data-to-route response: " ++ show (fst m)
836 _ -> return ()
837 return result
830 838
831senderkey :: OnionDestination r -> t -> (PublicKey, t) 839senderkey :: OnionDestination r -> t -> (PublicKey, t)
832senderkey addr e = (onionKey addr, e) 840senderkey addr e = (onionKey addr, e)
@@ -838,7 +846,12 @@ aliasKey crypto (OnionDestination {}) = (onionAliasSecret &&& onionAliasPublic)
838dhtKey :: TransportCrypto -> (SecretKey,PublicKey) 846dhtKey :: TransportCrypto -> (SecretKey,PublicKey)
839dhtKey crypto = (transportSecret &&& transportPublic) crypto 847dhtKey crypto = (transportSecret &&& transportPublic) crypto
840 848
841decryptMessage :: Serialize x => 849decodePlainVerbose :: (Typeable a, Serialize a) => Plain Serialize a -> Either String a
850decodePlainVerbose p =
851 left (\e -> unlines (unwords [e , show $ typeRep p] : xxd2 0 (BA.convert p :: ByteString)))
852 $ decodePlain p
853
854decryptMessage :: (Typeable x, Serialize x) =>
842 TransportCrypto 855 TransportCrypto
843 -> (SecretKey,PublicKey) 856 -> (SecretKey,PublicKey)
844 -> Nonce24 857 -> Nonce24
@@ -847,7 +860,7 @@ decryptMessage :: Serialize x =>
847 -> IO ((Either String ∘ Identity) x) 860 -> IO ((Either String ∘ Identity) x)
848decryptMessage crypto (sk,pk) n arg = do 861decryptMessage crypto (sk,pk) n arg = do
849 let (sender,e) = either id (senderKey &&& asymmData) arg 862 let (sender,e) = either id (senderKey &&& asymmData) arg
850 plain = Composed . fmap Identity . (>>= decodePlain) 863 plain = Composed . fmap Identity . (>>= decodePlainVerbose)
851 secret <- lookupSharedSecret crypto sk sender n 864 secret <- lookupSharedSecret crypto sk sender n
852 return $ plain $ ToxCrypto.decrypt secret e 865 return $ plain $ ToxCrypto.decrypt secret e
853 866
@@ -998,15 +1011,15 @@ parseDataToRoute crypto (OnionToRouteResponse dta, od) = do
998 (Right dta) -- using Asymm{senderKey} as remote key 1011 (Right dta) -- using Asymm{senderKey} as remote key
999 let eOuter = fmap runIdentity $ uncomposed omsg0 1012 let eOuter = fmap runIdentity $ uncomposed omsg0
1000 1013
1001 anyRight [] f = return $ Left "parseDataToRoute: no user key" 1014 anyRight [] e f = return $ Left $ "parseDataToRoute: " ++ e
1002 anyRight (x:xs) f = f x >>= either (const $ anyRight xs f) (return . Right) 1015 anyRight (x:xs) e f = f x >>= either (\e2 -> anyRight xs e2 f) (return . Right)
1003 1016
1004 -- TODO: We don't currently have a way to look up which user key we 1017 -- TODO: We don't currently have a way to look up which user key we
1005 -- announced using along this onion route. Therefore, for now, we will 1018 -- announced using along this onion route. Therefore, for now, we will
1006 -- try all our user keys to see if any can decrypt the packet. 1019 -- try all our user keys to see if any can decrypt the packet.
1007 eInner <- case eOuter of 1020 eInner <- case eOuter of
1008 Left e -> return $ Left e 1021 Left e -> return $ Left e
1009 Right dtr -> anyRight ks $ \(sk,pk) -> do 1022 Right dtr -> anyRight ks "no user key" $ \(sk,pk) -> do
1010 omsg0 <- decryptMessage crypto 1023 omsg0 <- decryptMessage crypto
1011 (sk,pk) 1024 (sk,pk)
1012 (asymmNonce dta) 1025 (asymmNonce dta)