2022-05-18 07:09:29 +00:00
|
|
|
//
|
2023-09-09 22:52:39 +00:00
|
|
|
// SandboxChecker.swift
|
2022-05-18 07:09:29 +00:00
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 5/18/22.
|
2024-01-14 13:34:21 +00:00
|
|
|
// Copyright (c) 2024 Davide De Rosa. All rights reserved.
|
2022-05-18 07:09:29 +00:00
|
|
|
//
|
|
|
|
// https://github.com/passepartoutvpn
|
|
|
|
//
|
|
|
|
// This file is part of Passepartout.
|
|
|
|
//
|
|
|
|
// Passepartout is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Passepartout is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Passepartout. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
|
|
// https://stackoverflow.com/a/32238344/784615
|
|
|
|
// https://gist.github.com/lukaskubanek/cbfcab29c0c93e0e9e0a16ab09586996
|
|
|
|
|
2023-10-10 21:00:19 +00:00
|
|
|
public final actor SandboxChecker {
|
2023-09-10 17:08:31 +00:00
|
|
|
public init() {
|
2023-09-09 22:52:39 +00:00
|
|
|
}
|
|
|
|
|
2023-09-10 17:08:31 +00:00
|
|
|
public var isBeta: Bool {
|
|
|
|
let isBeta = verifyBetaBuild()
|
|
|
|
pp_log.info("Beta build: \(isBeta)")
|
|
|
|
return isBeta
|
2023-09-09 22:52:39 +00:00
|
|
|
}
|
2023-09-10 17:08:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Shared
|
|
|
|
|
|
|
|
private extension SandboxChecker {
|
2023-09-09 22:52:39 +00:00
|
|
|
|
2023-09-10 08:27:11 +00:00
|
|
|
// IMPORTANT: check Mac first because os(iOS) holds true for Catalyst
|
2023-09-10 17:08:31 +00:00
|
|
|
func verifyBetaBuild() -> Bool {
|
2023-09-10 06:08:26 +00:00
|
|
|
#if targetEnvironment(macCatalyst) || os(macOS)
|
2023-09-10 08:27:11 +00:00
|
|
|
isMacTestFlightBuild
|
|
|
|
#elseif os(iOS)
|
|
|
|
isiOSSandboxBuild
|
|
|
|
#else
|
|
|
|
false
|
|
|
|
#endif
|
|
|
|
}
|
2023-09-10 17:08:31 +00:00
|
|
|
|
|
|
|
var bundle: Bundle {
|
|
|
|
.main
|
|
|
|
}
|
2023-09-10 08:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: iOS
|
|
|
|
|
|
|
|
#if os(iOS)
|
|
|
|
private extension SandboxChecker {
|
|
|
|
var isiOSSandboxBuild: Bool {
|
|
|
|
bundle.appStoreReceiptURL?.lastPathComponent == "sandboxReceipt"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// MARK: macOS
|
|
|
|
|
|
|
|
#if targetEnvironment(macCatalyst) || os(macOS)
|
|
|
|
private extension SandboxChecker {
|
|
|
|
var isMacTestFlightBuild: Bool {
|
2022-05-18 07:09:29 +00:00
|
|
|
var status = noErr
|
|
|
|
|
|
|
|
var code: SecStaticCode?
|
2023-09-09 22:52:39 +00:00
|
|
|
status = SecStaticCodeCreateWithPath(bundle.bundleURL as CFURL, [], &code)
|
2022-05-18 07:09:29 +00:00
|
|
|
guard status == noErr else {
|
|
|
|
return false
|
|
|
|
}
|
2023-09-09 22:52:39 +00:00
|
|
|
guard let code else {
|
2022-05-18 07:09:29 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
var requirement: SecRequirement?
|
|
|
|
status = SecRequirementCreateWithString(
|
|
|
|
"anchor apple generic and certificate leaf[field.1.2.840.113635.100.6.1.25.1]" as CFString,
|
|
|
|
[], // default
|
|
|
|
&requirement
|
|
|
|
)
|
|
|
|
guard status == noErr else {
|
|
|
|
return false
|
|
|
|
}
|
2023-09-09 22:52:39 +00:00
|
|
|
guard let requirement else {
|
2022-05-18 07:09:29 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
status = SecStaticCodeCheckValidity(
|
|
|
|
code,
|
|
|
|
[], // default
|
|
|
|
requirement
|
|
|
|
)
|
|
|
|
return status == errSecSuccess
|
|
|
|
}
|
|
|
|
}
|
2023-09-10 08:27:11 +00:00
|
|
|
#endif
|