Skip to content
This repository has been archived by the owner on Sep 29, 2024. It is now read-only.

Commit

Permalink
Merge pull request #42 from keeshux/mask-private-debug-data
Browse files Browse the repository at this point in the history
Mask private debug data
  • Loading branch information
keeshux authored Oct 24, 2018
2 parents fe4390e + e26f310 commit 8b78a64
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 22 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Configuration key `lastErrorKey` for reporting errors to host app. [#40](https://github.com/keeshux/tunnelkit/pull/40)
- Server extended key usage validation (EKU). [#27](https://github.com/keeshux/tunnelkit/issues/27)

### Changed

- Potentially private data (e.g. Internet addresses) is now masked in debug log. [#42](https://github.com/keeshux/tunnelkit/pull/42)

### Fixed

- CA file was not closed after MD5 calculation when using PIA patches.
Expand Down
8 changes: 4 additions & 4 deletions TunnelKit/Sources/AppExtension/ConnectionStrategy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,29 +70,29 @@ class ConnectionStrategy {

// reuse preferred address
if let preferredAddress = preferredAddress {
log.debug("Pick preferred address: \(preferredAddress)")
log.debug("Pick preferred address: \(preferredAddress.maskedDescription)")
let socket = provider.createSocket(to: preferredAddress, protocol: currentProtocol())
completionHandler(socket, nil)
return
}

// use any resolved address
if prefersResolvedAddresses, let resolvedAddress = anyResolvedAddress() {
log.debug("Pick resolved address: \(resolvedAddress)")
log.debug("Pick resolved address: \(resolvedAddress.maskedDescription)")
let socket = provider.createSocket(to: resolvedAddress, protocol: currentProtocol())
completionHandler(socket, nil)
return
}

// fall back to DNS
log.debug("DNS resolve hostname: \(hostname)")
log.debug("DNS resolve hostname: \(hostname.maskedDescription)")
DNSResolver.resolve(hostname, timeout: timeout, queue: queue) { (addresses, error) in

// refresh resolved addresses
if let resolved = addresses, !resolved.isEmpty {
self.resolvedAddresses = resolved

log.debug("DNS resolved addresses: \(resolved)")
log.debug("DNS resolved addresses: \(resolved.map { $0.maskedDescription })")
} else {
log.error("DNS resolution failed!")
}
Expand Down
2 changes: 1 addition & 1 deletion TunnelKit/Sources/AppExtension/InterfaceObserver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class InterfaceObserver: NSObject {
let currentWifiName = currentWifiNetworkName()
if (currentWifiName != lastWifiName) {
if let current = currentWifiName {
log.debug("SSID is now '\(current)'")
log.debug("SSID is now '\(current.maskedDescription)'")
if let last = lastWifiName, (current != last) {
queue?.async {
NotificationCenter.default.post(name: .__InterfaceObserverDidDetectWifiChange, object: nil)
Expand Down
8 changes: 4 additions & 4 deletions TunnelKit/Sources/AppExtension/Transport/NETCPInterface.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ class NETCPSocket: NSObject, GenericSocket {
switch keyPath {
case #keyPath(NWTCPConnection.state):
if let resolvedEndpoint = impl.remoteAddress {
log.debug("Socket state is \(impl.state) (endpoint: \(impl.endpoint) -> \(resolvedEndpoint))")
log.debug("Socket state is \(impl.state) (endpoint: \(impl.endpoint.maskedDescription) -> \(resolvedEndpoint.maskedDescription))")
} else {
log.debug("Socket state is \(impl.state) (endpoint: \(impl.endpoint) -> in progress)")
log.debug("Socket state is \(impl.state) (endpoint: \(impl.endpoint.maskedDescription) -> in progress)")
}

switch impl.state {
Expand Down Expand Up @@ -250,8 +250,8 @@ class NETCPLink: LinkInterface {
extension NETCPSocket {
override var description: String {
guard let hostEndpoint = impl.endpoint as? NWHostEndpoint else {
return impl.endpoint.description
return impl.endpoint.maskedDescription
}
return "\(hostEndpoint.hostname):\(hostEndpoint.port)"
return "\(hostEndpoint.hostname.maskedDescription):\(hostEndpoint.port.maskedDescription)"
}
}
8 changes: 4 additions & 4 deletions TunnelKit/Sources/AppExtension/Transport/NEUDPInterface.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ class NEUDPSocket: NSObject, GenericSocket {
switch keyPath {
case #keyPath(NWUDPSession.state):
if let resolvedEndpoint = impl.resolvedEndpoint {
log.debug("Socket state is \(impl.state) (endpoint: \(impl.endpoint) -> \(resolvedEndpoint))")
log.debug("Socket state is \(impl.state) (endpoint: \(impl.endpoint.maskedDescription) -> \(resolvedEndpoint.maskedDescription))")
} else {
log.debug("Socket state is \(impl.state) (endpoint: \(impl.endpoint) -> in progress)")
log.debug("Socket state is \(impl.state) (endpoint: \(impl.endpoint.maskedDescription) -> in progress)")
}

switch impl.state {
Expand Down Expand Up @@ -236,8 +236,8 @@ class NEUDPLink: LinkInterface {
extension NEUDPSocket {
override var description: String {
guard let hostEndpoint = impl.endpoint as? NWHostEndpoint else {
return impl.endpoint.description
return impl.endpoint.maskedDescription
}
return "\(hostEndpoint.hostname):\(hostEndpoint.port)"
return "\(hostEndpoint.hostname.maskedDescription):\(hostEndpoint.port.maskedDescription)"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,6 @@ extension TunnelKitProvider {
log.info("App version: \(appVersion)")
}

// log.info("\tAddress: \(endpoint.hostname):\(endpoint.port)")
log.info("\tProtocols: \(endpointProtocols)")
log.info("\tCipher: \(cipher)")
log.info("\tDigest: \(digest)")
Expand Down
6 changes: 3 additions & 3 deletions TunnelKit/Sources/AppExtension/TunnelKitProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -483,10 +483,10 @@ extension TunnelKitProvider: SessionProxyDelegate {
log.info("Session did start")

log.info("Returned ifconfig parameters:")
log.info("\tRemote: \(remoteAddress)")
log.info("\tRemote: \(remoteAddress.maskedDescription)")
log.info("\tIPv4: \(reply.ipv4?.description ?? "not configured")")
log.info("\tIPv6: \(reply.ipv6?.description ?? "not configured")")
log.info("\tDNS: \(reply.dnsServers)")
log.info("\tDNS: \(reply.dnsServers.map { $0.maskedDescription })")

bringNetworkUp(remoteAddress: remoteAddress, reply: reply) { (error) in
if let error = error {
Expand Down Expand Up @@ -602,7 +602,7 @@ extension TunnelKitProvider {

private func logCurrentSSID() {
if let ssid = observer.currentWifiNetworkName() {
log.debug("Current SSID: '\(ssid)'")
log.debug("Current SSID: '\(ssid.maskedDescription)'")
} else {
log.debug("Current SSID: none (disconnected from WiFi)")
}
Expand Down
17 changes: 17 additions & 0 deletions TunnelKit/Sources/Core/CoreConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

import Foundation
import __TunnelKitNative
import CommonCrypto

struct CoreConfiguration {
static let identifier = "com.algoritmico.TunnelKit"
Expand All @@ -59,6 +60,8 @@ struct CoreConfiguration {

static let logsSensitiveData = false

static let masksPrivateData = true

static let usesReplayProtection = true

static let tickInterval = 0.2
Expand Down Expand Up @@ -97,3 +100,17 @@ struct CoreConfiguration {

static let keysCount = 4
}

extension CustomStringConvertible {
var maskedDescription: String {
guard CoreConfiguration.masksPrivateData else {
return description
}
var data = description.data(using: .utf8)!
var md = Data(count: Int(CC_SHA1_DIGEST_LENGTH))
md.withUnsafeMutableBytes {
_ = CC_SHA1(&data, CC_LONG(data.count), $0)
}
return "#\(md.toHex().prefix(16))#"
}
}
8 changes: 4 additions & 4 deletions TunnelKit/Sources/Core/SessionProxy+PushReply.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public struct IPv4Settings: CustomStringConvertible {

/// :nodoc:
public var description: String {
return "{\(destination)/\(mask) \(gateway ?? "default")}"
return "{\(destination.maskedDescription)/\(mask.maskedDescription) \(gateway?.maskedDescription ?? "default")}"
}
}

Expand All @@ -82,7 +82,7 @@ public struct IPv4Settings: CustomStringConvertible {

/// :nodoc:
public var description: String {
return "addr \(address) netmask \(addressMask) gw \(defaultGateway) routes \(routes)"
return "addr \(address.maskedDescription) netmask \(addressMask.maskedDescription) gw \(defaultGateway.maskedDescription) routes \(routes.map { $0.maskedDescription })"
}
}

Expand Down Expand Up @@ -111,7 +111,7 @@ public struct IPv6Settings: CustomStringConvertible {

/// :nodoc:
public var description: String {
return "{\(destination)/\(prefixLength) \(gateway ?? "default")}"
return "{\(destination.maskedDescription)/\(prefixLength.maskedDescription) \(gateway?.maskedDescription ?? "default")}"
}
}

Expand All @@ -131,7 +131,7 @@ public struct IPv6Settings: CustomStringConvertible {

/// :nodoc:
public var description: String {
return "addr \(address)/\(addressPrefixLength) gw \(defaultGateway) routes \(routes)"
return "addr \(address.maskedDescription)/\(addressPrefixLength.maskedDescription) gw \(defaultGateway.maskedDescription) routes \(routes.map { $0.maskedDescription })"
}
}

Expand Down
2 changes: 1 addition & 1 deletion TunnelKit/Sources/Core/SessionProxy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,7 @@ public class SessionProxy {
return
}
reply = optionalReply
log.debug("Received PUSH_REPLY: \"\(reply)\"")
log.debug("Received PUSH_REPLY: \"\(reply.maskedDescription)\"")
} catch let e {
deferStop(.shutdown, e)
return
Expand Down

0 comments on commit 8b78a64

Please sign in to comment.