2018-11-08 10:24:12 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-12-04 11:15:29 +00:00
|
|
|
// Copyright © 2018-2020 WireGuard LLC. All Rights Reserved.
|
2018-11-08 10:24:12 +00:00
|
|
|
|
|
|
|
import NetworkExtension
|
|
|
|
|
2018-12-22 02:41:54 +00:00
|
|
|
enum PacketTunnelProviderError: String, Error {
|
|
|
|
case savedProtocolConfigurationIsInvalid
|
|
|
|
case dnsResolutionFailure
|
|
|
|
case couldNotStartBackend
|
|
|
|
case couldNotDetermineFileDescriptor
|
|
|
|
case couldNotSetNetworkSettings
|
|
|
|
}
|
|
|
|
|
2018-11-08 10:24:12 +00:00
|
|
|
extension NETunnelProviderProtocol {
|
2019-02-04 06:37:26 +00:00
|
|
|
convenience init?(tunnelConfiguration: TunnelConfiguration, previouslyFrom old: NEVPNProtocol? = nil) {
|
2018-11-08 10:24:12 +00:00
|
|
|
self.init()
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2019-02-04 06:37:26 +00:00
|
|
|
guard let name = tunnelConfiguration.name else { return nil }
|
|
|
|
guard let appId = Bundle.main.bundleIdentifier else { return nil }
|
2018-11-08 10:24:12 +00:00
|
|
|
providerBundleIdentifier = "\(appId).network-extension"
|
2019-02-04 06:37:26 +00:00
|
|
|
passwordReference = Keychain.makeReference(containing: tunnelConfiguration.asWgQuickConfig(), called: name, previouslyReferencedBy: old?.passwordReference)
|
|
|
|
if passwordReference == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2019-06-11 00:03:11 +00:00
|
|
|
#if os(macOS)
|
|
|
|
providerConfiguration = ["UID": getuid()]
|
|
|
|
#endif
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-21 04:52:45 +00:00
|
|
|
let endpoints = tunnelConfiguration.peers.compactMap { $0.endpoint }
|
2018-11-08 10:24:12 +00:00
|
|
|
if endpoints.count == 1 {
|
2018-12-21 04:52:45 +00:00
|
|
|
serverAddress = endpoints[0].stringRepresentation
|
2018-11-08 10:24:12 +00:00
|
|
|
} else if endpoints.isEmpty {
|
|
|
|
serverAddress = "Unspecified"
|
|
|
|
} else {
|
|
|
|
serverAddress = "Multiple endpoints"
|
|
|
|
}
|
|
|
|
}
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-21 23:28:18 +00:00
|
|
|
func asTunnelConfiguration(called name: String? = nil) -> TunnelConfiguration? {
|
2019-02-12 12:07:14 +00:00
|
|
|
if let passwordReference = passwordReference,
|
|
|
|
let config = Keychain.openReference(called: passwordReference) {
|
|
|
|
return try? TunnelConfiguration(fromWgQuickConfig: config, called: name)
|
|
|
|
}
|
|
|
|
if let oldConfig = providerConfiguration?["WgQuickConfig"] as? String {
|
|
|
|
return try? TunnelConfiguration(fromWgQuickConfig: oldConfig, called: name)
|
|
|
|
}
|
|
|
|
return nil
|
2018-12-19 10:32:48 +00:00
|
|
|
}
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2019-02-04 06:37:26 +00:00
|
|
|
func destroyConfigurationReference() {
|
|
|
|
guard let ref = passwordReference else { return }
|
|
|
|
Keychain.deleteReference(called: ref)
|
|
|
|
}
|
|
|
|
|
2019-04-03 11:02:12 +00:00
|
|
|
func verifyConfigurationReference() -> Bool {
|
|
|
|
guard let ref = passwordReference else { return false }
|
|
|
|
return Keychain.verifyReference(called: ref)
|
2019-02-04 06:37:26 +00:00
|
|
|
}
|
2019-02-06 02:23:51 +00:00
|
|
|
|
2019-02-06 01:01:12 +00:00
|
|
|
@discardableResult
|
|
|
|
func migrateConfigurationIfNeeded(called name: String) -> Bool {
|
|
|
|
/* This is how we did things before we switched to putting items
|
|
|
|
* in the keychain. But it's still useful to keep the migration
|
|
|
|
* around so that .mobileconfig files are easier.
|
|
|
|
*/
|
2019-06-11 00:03:11 +00:00
|
|
|
if let oldConfig = providerConfiguration?["WgQuickConfig"] as? String {
|
|
|
|
#if os(macOS)
|
|
|
|
providerConfiguration = ["UID": getuid()]
|
|
|
|
#elseif os(iOS)
|
|
|
|
providerConfiguration = nil
|
|
|
|
#else
|
|
|
|
#error("Unimplemented")
|
|
|
|
#endif
|
|
|
|
guard passwordReference == nil else { return true }
|
|
|
|
wg_log(.debug, message: "Migrating tunnel configuration '\(name)'")
|
|
|
|
passwordReference = Keychain.makeReference(containing: oldConfig, called: name)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
#if os(macOS)
|
|
|
|
if passwordReference != nil && providerConfiguration?["UID"] == nil && verifyConfigurationReference() {
|
|
|
|
providerConfiguration = ["UID": getuid()]
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return false
|
2019-02-06 01:01:12 +00:00
|
|
|
}
|
2018-11-08 10:24:12 +00:00
|
|
|
}
|