-
Notifications
You must be signed in to change notification settings - Fork 283
Fix a batch of Tor-related issues #3354
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
Open
t-bast
wants to merge
3
commits into
master
Choose a base branch
from
loupe-medium-issues
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,10 +21,11 @@ import akka.io.Tcp.Connected | |
| import akka.util.ByteString | ||
| import fr.acinq.eclair.tor.TorProtocolHandler.Authentication | ||
| import fr.acinq.eclair.wire.protocol.{NodeAddress, Tor3} | ||
| import fr.acinq.eclair.writeSecret | ||
| import scodec.bits.Bases.Alphabets | ||
| import scodec.bits.ByteVector | ||
|
|
||
| import java.nio.file.attribute.PosixFilePermissions | ||
| import java.net.InetSocketAddress | ||
| import java.nio.file.{Files, Path, Paths} | ||
| import java.util | ||
| import javax.crypto.Mac | ||
|
|
@@ -35,22 +36,21 @@ import scala.util.Try | |
| case class TorException(private val msg: String) extends RuntimeException(s"Tor error: $msg") | ||
|
|
||
| /** | ||
| * Created by rorp | ||
| * | ||
| * Specification: https://gitweb.torproject.org/torspec.git/tree/control-spec.txt | ||
| * | ||
| * @param authentication Tor controller auth mechanism (password or safecookie) | ||
| * @param privateKeyPath path to a file that contains a Tor private key | ||
| * @param virtualPort port for the public hidden service (typically 9735) | ||
| * @param targets address of our protected server (format [host:]port), 127.0.0.1:[[virtualPort]] if empty | ||
| * @param onionAdded a Promise to track creation of the endpoint | ||
| */ | ||
| * Created by rorp | ||
| * | ||
| * Specification: https://gitweb.torproject.org/torspec.git/tree/control-spec.txt | ||
| * | ||
| * @param authentication Tor controller auth mechanism (password or safecookie) | ||
| * @param privateKeyPath path to a file that contains a Tor private key | ||
| * @param virtualPort port for the public hidden service (typically 9735) | ||
| * @param targets address of our protected server (format [host:]port), 127.0.0.1:[[virtualPort]] if empty | ||
| * @param onionAdded a Promise to track creation of the endpoint | ||
| */ | ||
| class TorProtocolHandler(authentication: Authentication, | ||
| privateKeyPath: Path, | ||
| virtualPort: Int, | ||
| targets: Seq[String], | ||
| onionAdded: Option[Promise[NodeAddress]] | ||
| ) extends Actor with Stash with ActorLogging { | ||
| onionAdded: Option[Promise[NodeAddress]]) extends Actor with Stash with ActorLogging { | ||
|
|
||
| import TorProtocolHandler._ | ||
|
|
||
|
|
@@ -59,12 +59,28 @@ class TorProtocolHandler(authentication: Authentication, | |
| private var address: Option[NodeAddress] = None | ||
|
|
||
| override def receive: Receive = { | ||
| case Connected(_, _) => | ||
| case Connected(remoteAddress, _) => | ||
| checkControlAddress(remoteAddress) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| receiver = sender() | ||
| sendCommand("PROTOCOLINFO 1") | ||
| context become protocolInfo | ||
| } | ||
|
|
||
| /** | ||
| * The tor control protocol doesn't let us authenticate the server when using password authentication: we would send | ||
| * our password in cleartext to whatever process is listening on the control port. And since ADD_ONION also sends our | ||
| * onion private key in cleartext, a remote control port exposes both secrets to the network. We thus only allow | ||
| * password authentication on a local control port, and require safecookie otherwise, which authenticates the server. | ||
| */ | ||
| private def checkControlAddress(remoteAddress: InetSocketAddress): Unit = { | ||
| val isLocal = Option(remoteAddress.getAddress).exists(_.isLoopbackAddress) | ||
| authentication match { | ||
| case _: Password if !isLocal => throw TorException(s"cannot use password authentication with a remote control port ($remoteAddress): use safecookie instead") | ||
| case _ if !isLocal => log.warning("tor control port {} is not local: our onion private key will be sent in cleartext over the network", remoteAddress) | ||
| case _ => () | ||
| } | ||
| } | ||
|
|
||
| def protocolInfo: Receive = { | ||
| case data: ByteString => | ||
| val res = parseResponse(readResponse(data)) | ||
|
|
@@ -134,10 +150,7 @@ class TorProtocolHandler(authentication: Authentication, | |
| private def processOnionResponse(res: Map[String, String]): String = { | ||
| val serviceId = res.getOrElse("ServiceID", throw TorException("service ID not found")) | ||
| val privateKey = res.get("PrivateKey") | ||
| privateKey.foreach { pk => | ||
| writeString(privateKeyPath, pk) | ||
| setPermissions(privateKeyPath, "rw-------") | ||
| } | ||
| privateKey.foreach(pk => writeSecret(privateKeyPath, pk)) | ||
| serviceId | ||
| } | ||
|
|
||
|
|
@@ -158,15 +171,19 @@ class TorProtocolHandler(authentication: Authentication, | |
| } | ||
|
|
||
| private def computeClientHash(serverHash: ByteVector, serverNonce: ByteVector, clientNonce: ByteVector, cookieFile: Path): ByteVector = { | ||
| if (serverHash.length != 32) | ||
| if (serverHash.length != 32) { | ||
| throw TorException("invalid server hash length") | ||
| if (serverNonce.length != 32) | ||
| } | ||
| if (serverNonce.length != 32) { | ||
| throw TorException("invalid server nonce length") | ||
| } | ||
|
|
||
| val cookie = ByteVector.view(Files.readAllBytes(cookieFile)) | ||
| if (cookie.length != 32) { | ||
| throw TorException("invalid server cookie length") | ||
| } | ||
|
|
||
| val message = cookie ++ clientNonce ++ serverNonce | ||
|
|
||
| val computedServerHash = hmacSHA256(ServerKey, message) | ||
| if (computedServerHash != serverHash) { | ||
| throw TorException("unexpected server hash") | ||
|
|
@@ -226,13 +243,6 @@ object TorProtocolHandler { | |
|
|
||
| def writeString(path: Path, string: String): Unit = Files.write(path, util.Arrays.asList(string)) | ||
|
|
||
| def setPermissions(path: Path, permissionString: String): Unit = | ||
| try { | ||
| Files.setPosixFilePermissions(path, PosixFilePermissions.fromString(permissionString)) | ||
| } catch { | ||
| case _: UnsupportedOperationException => () // we are on windows | ||
| } | ||
|
|
||
| def unquote(s: String): String = s | ||
| .stripSuffix("\"") | ||
| .stripPrefix("\"") | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rorp this is effectively a fixup for your PR #3340
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍