diff options
Diffstat (limited to 'OnionTransport.hs')
-rw-r--r-- | OnionTransport.hs | 21 |
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 | ||
84 | onionKey :: OnionToOwner -> Maybe PublicKey | ||
85 | onionKey (OnionToOwner ni _) = Just $ id2key (nodeId ni) | ||
86 | onionKey _ = Nothing | ||
87 | |||
84 | instance Sized (OnionMessage Encrypted) where | 88 | instance 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 | ||
445 | encrypt :: TransportCrypto -> OnionMessage Identity -> OnionToOwner -> (OnionMessage Encrypted, OnionToOwner) | 449 | encrypt :: TransportCrypto -> OnionMessage Identity -> OnionToOwner -> (OnionMessage Encrypted, OnionToOwner) |
446 | encrypt crypto msg rpath = (transcode (encryptMessage crypto) msg, rpath) | 450 | encrypt 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 | ||
448 | encryptMessage :: Serialize a => | 457 | encryptMessage :: 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 |
450 | encryptMessage crypto n (Right a) = ToxCrypto.encrypt secret plain | 459 | encryptMessage 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 |
454 | encryptMessage crypto n (Left x) = ToxCrypto.encrypt secret plain | 463 | encryptMessage 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 | ||
459 | decrypt :: TransportCrypto -> OnionMessage Encrypted -> OnionToOwner -> Either String (OnionMessage Identity, OnionToOwner) | 468 | decrypt :: TransportCrypto -> OnionMessage Encrypted -> OnionToOwner -> Either String (OnionMessage Identity, OnionToOwner) |