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 d23285f
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 d23285f

Please sign in to comment.