Skip to content

Commit 658cfa9

Browse files
committed
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 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() ```
1 parent d16f92f commit 658cfa9

File tree

4 files changed

+24
-9
lines changed

4 files changed

+24
-9
lines changed

NOnion/Exceptions.fs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ type CircuitDestroyedException internal (reason: DestroyReason) =
2929
type CircuitDecryptionFailedException internal () =
3030
inherit NOnionException(sprintf "Circuit Decryption Failed")
3131

32+
type HandshakeFailedException internal () =
33+
inherit NOnionException(sprintf "Key handshake failed!")
34+
3235
type TimeoutErrorException internal () =
3336
inherit NOnionException("Time limit exceeded for operation")
3437

NOnion/Network/TorGuard.fs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,18 @@ type TorGuard private (client: TcpClient, sslStream: SslStream) =
193193
member self.SendAsync (circuidId: uint16) (cellToSend: ICell) =
194194
self.Send circuidId cellToSend |> Async.StartAsTask
195195

196+
member private self.HandleIncomingCellException<'T when 'T :> NOnionException>
197+
(cell: ICell)
198+
(ex: 'T)
199+
=
200+
sprintf
201+
"TorGuard: exception when trying to handle incoming cell type=%i, ex=%s"
202+
cell.Command
203+
(ex.ToString())
204+
|> TorLogger.Log
205+
206+
self.KillChildCircuits()
207+
196208
member private __.ReceiveInternal() =
197209
async {
198210
(*
@@ -332,14 +344,14 @@ type TorGuard private (client: TcpClient, sslStream: SslStream) =
332344
try
333345
do! circuit.HandleIncomingCell cell
334346
with
347+
| :? HandshakeFailedException as ex ->
348+
self.HandleIncomingCellException<HandshakeFailedException>
349+
cell
350+
ex
335351
| :? CircuitDecryptionFailedException as ex ->
336-
sprintf
337-
"TorGuard: exception when trying to handle incoming cell type=%i, ex=%s"
338-
cell.Command
339-
(ex.ToString())
340-
|> TorLogger.Log
341-
342-
self.KillChildCircuits()
352+
self.HandleIncomingCellException<CircuitDecryptionFailedException>
353+
cell
354+
ex
343355
| ex -> return raise <| FSharpUtil.ReRaise ex
344356
| None ->
345357
self.KillChildCircuits()

NOnion/TorHandshakes/FastHandshake.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,6 @@ type FastHandshake =
3636
|> Kdf.ComputeLegacyKdf
3737

3838
if kdfResult.KeyHandshake <> serverSideData.DerivativeKey then
39-
failwith "Key handshake failed!"
39+
raise <| HandshakeFailedException()
4040
else
4141
kdfResult

NOnion/TorHandshakes/NTorHandshake.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,6 @@ type NTorHandshake =
107107
let auth = calculateHmacSha256 authInput Constants.NTorTMac
108108

109109
if auth <> serverSideData.DerivativeKey then
110-
failwith "Key handshake failed!"
110+
raise <| HandshakeFailedException()
111111
else
112112
Kdf.ComputeRfc5869Kdf secretInput

0 commit comments

Comments
 (0)