2018-11-08 10:24:12 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2023-02-14 15:10:32 +00:00
|
|
|
// Copyright © 2018-2023 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 }
|
2021-09-23 04:00:14 +00:00
|
|
|
wg_log(.info, message: "Migrating tunnel configuration '\(name)'")
|
2019-06-11 00:03:11 +00:00
|
|
|
passwordReference = Keychain.makeReference(containing: oldConfig, called: name)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
#if os(macOS)
|
|
|
|
if passwordReference != nil && providerConfiguration?["UID"] == nil && verifyConfigurationReference() {
|
|
|
|
providerConfiguration = ["UID": getuid()]
|
|
|
|
return true
|
|
|
|
}
|
2021-09-23 04:00:14 +00:00
|
|
|
#elseif os(iOS)
|
2023-02-14 14:31:52 +00:00
|
|
|
/* Update the stored reference from the old iOS 14 one to the canonical iOS 15 one.
|
|
|
|
* The iOS 14 ones are 96 bits, while the iOS 15 ones are 160 bits. We do this so
|
|
|
|
* that we can have fast set exclusion in deleteReferences safely. */
|
|
|
|
if passwordReference != nil && passwordReference!.count == 12 {
|
|
|
|
var result: CFTypeRef?
|
|
|
|
let ret = SecItemCopyMatching([kSecValuePersistentRef: passwordReference!,
|
|
|
|
kSecReturnPersistentRef: true] as CFDictionary,
|
|
|
|
&result)
|
|
|
|
if ret != errSecSuccess || result == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
guard let newReference = result as? Data else { return false }
|
|
|
|
if !newReference.elementsEqual(passwordReference!) {
|
|
|
|
wg_log(.info, message: "Migrating iOS 14-style keychain reference to iOS 15-style keychain reference for '\(name)'")
|
|
|
|
passwordReference = newReference
|
|
|
|
return true
|
2021-09-23 04:00:14 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-11 00:03:11 +00:00
|
|
|
#endif
|
|
|
|
return false
|
2019-02-06 01:01:12 +00:00
|
|
|
}
|
2018-11-08 10:24:12 +00:00
|
|
|
}
|