summaryrefslogtreecommitdiff
path: root/OnionTransport.hs
diff options
context:
space:
mode:
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)