summaryrefslogtreecommitdiff
path: root/OnionTransport.hs
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2017-09-11 17:59:55 -0400
committerjoe <joe@jerkface.net>2017-09-11 17:59:55 -0400
commit7372d2458b5f9c33e6aa676e5bae74dba438b289 (patch)
treed074b16966e267520f21d45868eb5174a6cdce9b /OnionTransport.hs
parentc7def803eb2c49381a9f0e6d1fece75da2024261 (diff)
Use correct destination key when encrypting packets.
Diffstat (limited to 'OnionTransport.hs')
-rw-r--r--OnionTransport.hs21
1 files changed, 15 insertions, 6 deletions
diff --git a/OnionTransport.hs b/OnionTransport.hs
index 6901038d..ce1063d2 100644
--- a/OnionTransport.hs
+++ b/OnionTransport.hs
@@ -81,6 +81,10 @@ data OnionToOwner = OnionToOwner NodeInfo (ReturnPath N3)
81 | OnionToMe SockAddr -- SockAddr is immediate peer in route 81 | OnionToMe SockAddr -- SockAddr is immediate peer in route
82 deriving Show 82 deriving Show
83 83
84onionKey :: OnionToOwner -> Maybe PublicKey
85onionKey (OnionToOwner ni _) = Just $ id2key (nodeId ni)
86onionKey _ = Nothing
87
84instance Sized (OnionMessage Encrypted) where 88instance Sized (OnionMessage Encrypted) where
85 size = VarSize $ \case 89 size = VarSize $ \case
86 OnionAnnounce a -> case size of ConstSize n -> n + 1 90 OnionAnnounce a -> case size of ConstSize n -> n + 1
@@ -443,17 +447,22 @@ instance Sized OnionData where
443 VarSize f -> f dhtpk 447 VarSize f -> f dhtpk
444 448
445encrypt :: TransportCrypto -> OnionMessage Identity -> OnionToOwner -> (OnionMessage Encrypted, OnionToOwner) 449encrypt :: TransportCrypto -> OnionMessage Identity -> OnionToOwner -> (OnionMessage Encrypted, OnionToOwner)
446encrypt crypto msg rpath = (transcode (encryptMessage crypto) msg, rpath) 450encrypt crypto msg rpath = ( transcode (encryptMessage crypto okey) msg
451 , rpath)
452 where
453 -- The OnionToMe case shouldn't happen, but we'll use our own public
454 -- key in this situation.
455 okey = fromMaybe (transportPublic crypto) $ onionKey rpath
447 456
448encryptMessage :: Serialize a => 457encryptMessage :: Serialize a =>
449 TransportCrypto -> Nonce24 -> Either (Identity a) (Assym (Identity a)) -> Encrypted a 458 TransportCrypto -> PublicKey -> Nonce24 -> Either (Identity a) (Assym (Identity a)) -> Encrypted a
450encryptMessage crypto n (Right a) = ToxCrypto.encrypt secret plain 459encryptMessage crypto destKey n (Right a) = ToxCrypto.encrypt secret plain
451 where 460 where
452 secret = computeSharedSecret (transportSecret crypto) (senderKey a) n 461 secret = computeSharedSecret (transportSecret crypto) destKey n
453 plain = encodePlain $ runIdentity $ assymData a 462 plain = encodePlain $ runIdentity $ assymData a
454encryptMessage crypto n (Left x) = ToxCrypto.encrypt secret plain 463encryptMessage crypto destKey n (Left x) = ToxCrypto.encrypt secret plain
455 where 464 where
456 secret = computeSharedSecret (transportSecret crypto) _todo n -- OnionAnnounceResponse has no sender key 465 secret = computeSharedSecret (transportSecret crypto) destKey n
457 plain = encodePlain $ runIdentity $ x 466 plain = encodePlain $ runIdentity $ x
458 467
459decrypt :: TransportCrypto -> OnionMessage Encrypted -> OnionToOwner -> Either String (OnionMessage Identity, OnionToOwner) 468decrypt :: TransportCrypto -> OnionMessage Encrypted -> OnionToOwner -> Either String (OnionMessage Identity, OnionToOwner)