Skip to content

Commit

Permalink
Network,TorHandshakes: handle handshake fail
Browse files Browse the repository at this point in the history
Previously, TorHandshakes used failwith to raise handshake
failed error. Since we need to catch it, as a result of
replacing generic try-with which lead to a red CI in the
PR, we had to define its special excpetion and catch that.

```
The active test run was aborted. Reason: Test host process crashed : Unhandled exception. System.Exception: Key handshake failed!

   at NOnion.TorHandshakes.NTorHandshake.NOnion-TorHandshakes-IHandshake-GenerateKdfResult(ICreatedCell serverSideData) in /home/runner/work/NOnion/NOnion/NOnion/TorHandshakes/NTorHandshake.fs:line 112
   at <StartupCode$NOnion>[email protected](Unit unitVar) in /home/runner/work/NOnion/NOnion/NOnion/Network/TorCircuit.fs:line 352
   at Microsoft.FSharp.Control.AsyncPrimitives.CallThenInvoke[T,TResult](AsyncActivation`1 ctxt, TResult result1, FSharpFunc`2 part2) in F:\workspace\_work\1\s\src\fsharp\FSharp.Core\async.fs:line 398
   at NOnion.Utility.MailboxResultUtil.TryExecuteAsyncAndReplyAsResult@25-3.Invoke(AsyncActivation`1 ctxt) in /home/runner/work/NOnion/NOnion/NOnion/Utility/MailboxUtil.fs:line 25
   at NOnion.Utility.MailboxResultUtil.TryExecuteAsyncAndReplyAsResult@24-6.Invoke(AsyncActivation`1 ctxt) in /home/runner/work/NOnion/NOnion/NOnion/Utility/MailboxUtil.fs:line 24
   at <StartupCode$NOnion>[email protected](AsyncActivation`1 ctxt) in /home/runner/work/NOnion/NOnion/NOnion/Network/TorCircuit.fs:line 965
   at <StartupCode$NOnion>[email protected](AsyncActivation`1 ctxt) in /home/runner/work/NOnion/NOnion/NOnion/Network/TorCircuit.fs:line 950

   at <StartupCode$FSharp-Core>[email protected](AsyncActivation`1 ctxt) in F:\workspace\_work\1\s\src\fsharp\FSharp.Core\mailbox.fs:line 303

   at Microsoft.FSharp.Control.Trampoline.Execute(FSharpFunc`2 firstAction) in F:\workspace\_work\1\s\src\fsharp\FSharp.Core\async.fs:line 109

--- End of stack trace from previous location where exception was thrown ---

   at [email protected](ExceptionDispatchInfo edi) in F:\workspace\_work\1\s\src\fsharp\FSharp.Core\async.fs:line 907

   at Microsoft.FSharp.Control.Trampoline.Execute(FSharpFunc`2 firstAction) in F:\workspace\_work\1\s\src\fsharp\FSharp.Core\async.fs:line 109

   at <StartupCode$FSharp-Core>[email protected](Object o) in F:\workspace\_work\1\s\src\fsharp\FSharp.Core\async.fs:line 165

   at System.Threading.QueueUserWorkItemCallback.Execute()

   at System.Threading.ThreadPoolWorkQueue.Dispatch()
```
  • Loading branch information
parhamsaremi committed Nov 23, 2022
1 parent d16f92f commit 0572f20
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
3 changes: 3 additions & 0 deletions NOnion/Exceptions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
26 changes: 19 additions & 7 deletions NOnion/Network/TorGuard.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
(*
Expand Down Expand Up @@ -332,14 +344,14 @@ type TorGuard private (client: TcpClient, sslStream: SslStream) =
try
do! circuit.HandleIncomingCell cell
with
| :? HandshakeFailedException as ex ->
self.HandleIncomingCellException<HandshakeFailedException>
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<CircuitDecryptionFailedException>
cell
ex
| ex -> return raise <| FSharpUtil.ReRaise ex
| None ->
self.KillChildCircuits()
Expand Down
2 changes: 1 addition & 1 deletion NOnion/TorHandshakes/FastHandshake.fs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ type FastHandshake =
|> Kdf.ComputeLegacyKdf

if kdfResult.KeyHandshake <> serverSideData.DerivativeKey then
failwith "Key handshake failed!"
raise <| HandshakeFailedException()
else
kdfResult
2 changes: 1 addition & 1 deletion NOnion/TorHandshakes/NTorHandshake.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 0572f20

Please sign in to comment.