Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic try-with torguard fixed #52

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions NOnion/Exceptions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ type CircuitTruncatedException internal (reason: DestroyReason) =
type CircuitDestroyedException internal (reason: DestroyReason) =
inherit NOnionException(sprintf "Circuit got destroyed, reason %A" reason)

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
3 changes: 2 additions & 1 deletion NOnion/Network/TorCircuit.fs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,8 @@ and TorCircuit
node)
| None ->
announceDeath()
failwith "Decryption failed!"

raise <| CircuitDecryptionFailedException()

decryptMessage encryptedRelayCell.EncryptedData nodes
| _ -> failwith "Unexpected state when receiving relay cell"
Expand Down
29 changes: 21 additions & 8 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,15 @@ type TorGuard private (client: TcpClient, sslStream: SslStream) =
try
do! circuit.HandleIncomingCell cell
with
| ex ->
sprintf
"TorGuard: exception when trying to handle incoming cell type=%i, ex=%s"
cell.Command
(ex.ToString())
|> TorLogger.Log

self.KillChildCircuits()
| :? HandshakeFailedException as ex ->
self.HandleIncomingCellException<HandshakeFailedException>
cell
ex
| :? CircuitDecryptionFailedException as ex ->
self.HandleIncomingCellException<CircuitDecryptionFailedException>
cell
ex
| ex -> return raise <| FSharpUtil.ReRaise ex
| None ->
self.KillChildCircuits()
failwithf "Unknown circuit, Id = %i" cid
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