From 15574514b7b9a1467c0d182fde932d2293dbf36e Mon Sep 17 00:00:00 2001 From: Parham Saremi Date: Mon, 21 Nov 2022 14:44:13 +0330 Subject: [PATCH] Network,TorHandshakes: handle handshake fail Previously, TorHandshakes used failwith to raise handshake failed error. Since we need to catch it, as a result of replacing generic try-with, this we had to define its special excpetion and catch that. --- NOnion/Exceptions.fs | 3 +++ NOnion/Network/TorGuard.fs | 26 +++++++++++++++++++------- NOnion/TorHandshakes/FastHandshake.fs | 2 +- NOnion/TorHandshakes/NTorHandshake.fs | 2 +- 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/NOnion/Exceptions.fs b/NOnion/Exceptions.fs index 20c61b25..3a9427bb 100644 --- a/NOnion/Exceptions.fs +++ b/NOnion/Exceptions.fs @@ -29,6 +29,9 @@ type CircuitDestroyedException internal (reason: DestroyReason) = type CircuitDecryptionFailedException internal () = inherit NOnionException(sprintf "Circuit Decryption Failed") +type HandshakeFailedException internal () = + inherit NOnionException(sprintf "Key handshake failed!") + type TimeoutErrorException internal () = inherit NOnionException("Time limit exceeded for operation") diff --git a/NOnion/Network/TorGuard.fs b/NOnion/Network/TorGuard.fs index 4bdcdb13..b5e90d4f 100644 --- a/NOnion/Network/TorGuard.fs +++ b/NOnion/Network/TorGuard.fs @@ -193,6 +193,18 @@ type TorGuard private (client: TcpClient, sslStream: SslStream) = member self.SendAsync (circuidId: uint16) (cellToSend: ICell) = self.Send circuidId cellToSend |> Async.StartAsTask + member private self.HandleIncomingCellException<'T when 'T :> NOnionException> + (cell: ICell) + (ex: 'T) + = + sprintf + "TorGuard: exception when trying to handle incoming cell type=%i, ex=%s" + cell.Command + (ex.ToString()) + |> TorLogger.Log + + self.KillChildCircuits() + member private __.ReceiveInternal() = async { (* @@ -332,14 +344,14 @@ type TorGuard private (client: TcpClient, sslStream: SslStream) = try do! circuit.HandleIncomingCell cell with + | :? HandshakeFailedException as ex -> + self.HandleIncomingCellException + cell + ex | :? CircuitDecryptionFailedException as ex -> - sprintf - "TorGuard: exception when trying to handle incoming cell type=%i, ex=%s" - cell.Command - (ex.ToString()) - |> TorLogger.Log - - self.KillChildCircuits() + self.HandleIncomingCellException + cell + ex | ex -> return raise <| FSharpUtil.ReRaise ex | None -> self.KillChildCircuits() diff --git a/NOnion/TorHandshakes/FastHandshake.fs b/NOnion/TorHandshakes/FastHandshake.fs index bc2fe4ee..173072ee 100644 --- a/NOnion/TorHandshakes/FastHandshake.fs +++ b/NOnion/TorHandshakes/FastHandshake.fs @@ -36,6 +36,6 @@ type FastHandshake = |> Kdf.ComputeLegacyKdf if kdfResult.KeyHandshake <> serverSideData.DerivativeKey then - failwith "Key handshake failed!" + raise <| HandshakeFailedException() else kdfResult diff --git a/NOnion/TorHandshakes/NTorHandshake.fs b/NOnion/TorHandshakes/NTorHandshake.fs index 9f88a43a..0564acb5 100644 --- a/NOnion/TorHandshakes/NTorHandshake.fs +++ b/NOnion/TorHandshakes/NTorHandshake.fs @@ -107,6 +107,6 @@ type NTorHandshake = let auth = calculateHmacSha256 authInput Constants.NTorTMac if auth <> serverSideData.DerivativeKey then - failwith "Key handshake failed!" + raise <| HandshakeFailedException() else Kdf.ComputeRfc5869Kdf secretInput