Skip to content

Commit

Permalink
Network,TorHandshakes: handshake fail exception
Browse files Browse the repository at this point in the history
  • Loading branch information
parhamsaremi committed Nov 21, 2022
1 parent d16f92f commit 0f60878
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 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
24 changes: 19 additions & 5 deletions NOnion/Network/TorGuard.fs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ module ExceptionUtil =
| None -> return raise <| FSharpUtil.ReRaise exn
}

let HandleIncomingCallException<'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

type TorGuard private (client: TcpClient, sslStream: SslStream) =
let shutdownToken = new CancellationTokenSource()

Expand Down Expand Up @@ -332,12 +342,16 @@ type TorGuard private (client: TcpClient, sslStream: SslStream) =
try
do! circuit.HandleIncomingCell cell
with
| :? HandshakeFailedException as ex ->
ExceptionUtil.HandleIncomingCallException<HandshakeFailedException>
cell
ex

self.KillChildCircuits()
| :? CircuitDecryptionFailedException as ex ->
sprintf
"TorGuard: exception when trying to handle incoming cell type=%i, ex=%s"
cell.Command
(ex.ToString())
|> TorLogger.Log
ExceptionUtil.HandleIncomingCallException<CircuitDecryptionFailedException>
cell
ex

self.KillChildCircuits()
| ex -> return raise <| FSharpUtil.ReRaise ex
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 0f60878

Please sign in to comment.