diff options
author | joe <joe@jerkface.net> | 2017-09-11 17:59:55 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2017-09-11 17:59:55 -0400 |
commit | 7372d2458b5f9c33e6aa676e5bae74dba438b289 (patch) | |
tree | d074b16966e267520f21d45868eb5174a6cdce9b | |
parent | c7def803eb2c49381a9f0e6d1fece75da2024261 (diff) |
Use correct destination key when encrypting packets.
-rw-r--r-- | DHTTransport.hs | 13 | ||||
-rw-r--r-- | OnionTransport.hs | 21 |
2 files changed, 23 insertions, 11 deletions
diff --git a/DHTTransport.hs b/DHTTransport.hs index 013fa322..ac263cdc 100644 --- a/DHTTransport.hs +++ b/DHTTransport.hs | |||
@@ -307,15 +307,18 @@ forwardDHTRequests crypto closeLookup dht = dht { awaitMessage = await' } | |||
307 | m -> pass m | 307 | m -> pass m |
308 | 308 | ||
309 | encrypt :: TransportCrypto -> DHTMessage ((,) Nonce8) -> NodeInfo -> (DHTMessage Encrypted8, NodeInfo) | 309 | encrypt :: TransportCrypto -> DHTMessage ((,) Nonce8) -> NodeInfo -> (DHTMessage Encrypted8, NodeInfo) |
310 | encrypt crypto msg ni = (transcode (encryptMessage crypto) msg, ni) | 310 | encrypt crypto msg ni = ( transcode (encryptMessage crypto (id2key $ nodeId ni)) msg |
311 | , ni ) | ||
311 | 312 | ||
312 | encryptMessage :: Serialize a => | 313 | encryptMessage :: Serialize a => |
313 | TransportCrypto -> Nonce24 -> Either (Nonce8,a) (Assym (Nonce8,a)) -> Encrypted8 a | 314 | TransportCrypto -> |
314 | encryptMessage crypto n (Right assym) = E8 $ ToxCrypto.encrypt secret plain | 315 | PublicKey -> |
316 | Nonce24 -> Either (Nonce8,a) (Assym (Nonce8,a)) -> Encrypted8 a | ||
317 | encryptMessage crypto destKey n (Right assym) = E8 $ ToxCrypto.encrypt secret plain | ||
315 | where | 318 | where |
316 | secret = computeSharedSecret (transportSecret crypto) (senderKey assym) n | 319 | secret = computeSharedSecret (transportSecret crypto) destKey n |
317 | plain = encodePlain $ swap $ assymData assym | 320 | plain = encodePlain $ swap $ assymData assym |
318 | encryptMessage crypto n (Left plain) = _todo -- need cached public key. | 321 | encryptMessage crypto destKey n (Left plain) = _todo -- need cached public key. |
319 | 322 | ||
320 | decrypt :: TransportCrypto -> DHTMessage Encrypted8 -> NodeInfo -> Either String (DHTMessage ((,) Nonce8), NodeInfo) | 323 | decrypt :: TransportCrypto -> DHTMessage Encrypted8 -> NodeInfo -> Either String (DHTMessage ((,) Nonce8), NodeInfo) |
321 | decrypt crypto msg ni = (, ni) <$> (sequenceMessage $ transcode (decryptMessage crypto) msg) | 324 | decrypt crypto msg ni = (, ni) <$> (sequenceMessage $ transcode (decryptMessage crypto) msg) |
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) |