2018-08-23 08:19:25 +00:00
|
|
|
//
|
|
|
|
// Keychain.swift
|
2018-08-23 10:07:55 +00:00
|
|
|
// TunnelKit
|
2018-08-23 08:19:25 +00:00
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 2/12/17.
|
2020-12-27 16:29:39 +00:00
|
|
|
// Copyright (c) 2021 Davide De Rosa. All rights reserved.
|
2018-08-27 18:30:09 +00:00
|
|
|
//
|
2019-05-14 08:58:47 +00:00
|
|
|
// https://github.com/passepartoutvpn
|
2018-08-27 18:30:09 +00:00
|
|
|
//
|
|
|
|
// This file is part of TunnelKit.
|
|
|
|
//
|
|
|
|
// TunnelKit 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.
|
|
|
|
//
|
|
|
|
// TunnelKit 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 TunnelKit. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//
|
|
|
|
// This file incorporates work covered by the following copyright and
|
|
|
|
// permission notice:
|
|
|
|
//
|
|
|
|
// Copyright (c) 2018-Present Private Internet Access
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
//
|
2018-08-23 08:19:25 +00:00
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
2019-05-24 12:58:25 +00:00
|
|
|
/// Error raised by `Keychain` methods.
|
2018-08-23 08:19:25 +00:00
|
|
|
public enum KeychainError: Error {
|
2019-05-24 12:58:25 +00:00
|
|
|
|
|
|
|
/// Unable to add.
|
2018-08-23 08:19:25 +00:00
|
|
|
case add
|
|
|
|
|
2019-05-24 12:58:25 +00:00
|
|
|
/// Item not found.
|
2018-08-23 08:19:25 +00:00
|
|
|
case notFound
|
|
|
|
|
2020-12-17 09:50:09 +00:00
|
|
|
/// Operation cancelled or unauthorized.
|
|
|
|
case userCancelled
|
|
|
|
|
2019-05-24 12:58:25 +00:00
|
|
|
// /// Unexpected item type returned.
|
|
|
|
// case typeMismatch
|
2018-08-23 08:19:25 +00:00
|
|
|
}
|
|
|
|
|
2019-05-24 12:58:25 +00:00
|
|
|
/// Wrapper for easy keychain access and modification.
|
2018-08-23 08:19:25 +00:00
|
|
|
public class Keychain {
|
|
|
|
private let accessGroup: String?
|
|
|
|
|
2019-05-24 12:58:25 +00:00
|
|
|
/**
|
2020-12-20 09:26:28 +00:00
|
|
|
Creates a keychain.
|
2019-05-24 12:58:25 +00:00
|
|
|
|
2020-12-20 09:26:28 +00:00
|
|
|
- Parameter group: An optional App Group.
|
|
|
|
- Precondition: Proper App Group entitlements (if group is non-nil).
|
2019-05-24 12:58:25 +00:00
|
|
|
**/
|
2020-12-20 09:26:28 +00:00
|
|
|
public init(group: String?) {
|
2018-08-23 08:19:25 +00:00
|
|
|
accessGroup = group
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Password
|
|
|
|
|
2019-05-24 12:58:25 +00:00
|
|
|
/**
|
|
|
|
Sets a password.
|
|
|
|
|
|
|
|
- Parameter password: The password to set.
|
|
|
|
- Parameter username: The username to set the password for.
|
2020-12-20 09:26:28 +00:00
|
|
|
- Parameter context: An optional context.
|
2021-11-22 23:24:01 +00:00
|
|
|
- Returns: The reference to the password.
|
2019-05-24 12:58:25 +00:00
|
|
|
- Throws: `KeychainError.add` if unable to add the password to the keychain.
|
|
|
|
**/
|
2021-11-22 23:24:01 +00:00
|
|
|
@discardableResult
|
|
|
|
public func set(password: String, for username: String, context: String? = nil) throws -> Data {
|
2018-08-23 08:19:25 +00:00
|
|
|
do {
|
2020-12-20 09:26:28 +00:00
|
|
|
let currentPassword = try self.password(for: username, context: context)
|
2018-08-23 08:19:25 +00:00
|
|
|
guard password != currentPassword else {
|
2021-11-22 23:24:01 +00:00
|
|
|
return try passwordReference(for: username, context: context)
|
2018-08-23 08:19:25 +00:00
|
|
|
}
|
2020-12-20 09:26:28 +00:00
|
|
|
removePassword(for: username, context: context)
|
2020-12-17 09:50:09 +00:00
|
|
|
} catch let e as KeychainError {
|
|
|
|
|
|
|
|
// rethrow cancelation
|
|
|
|
if e == .userCancelled {
|
|
|
|
throw e
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise, no pre-existing password
|
2018-08-23 08:19:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var query = [String: Any]()
|
2020-12-20 09:26:28 +00:00
|
|
|
setScope(query: &query, context: context)
|
2018-08-23 08:19:25 +00:00
|
|
|
query[kSecClass as String] = kSecClassGenericPassword
|
|
|
|
query[kSecAttrAccount as String] = username
|
|
|
|
query[kSecAttrAccessible as String] = kSecAttrAccessibleAfterFirstUnlock
|
|
|
|
query[kSecValueData as String] = password.data(using: .utf8)
|
2021-11-22 23:24:01 +00:00
|
|
|
query[kSecReturnPersistentRef as String] = true
|
2020-12-20 09:26:28 +00:00
|
|
|
|
2021-11-22 23:24:01 +00:00
|
|
|
var ref: CFTypeRef?
|
|
|
|
let status = SecItemAdd(query as CFDictionary, &ref)
|
|
|
|
guard status == errSecSuccess, let refData = ref as? Data else {
|
2018-08-23 08:19:25 +00:00
|
|
|
throw KeychainError.add
|
|
|
|
}
|
2021-11-22 23:24:01 +00:00
|
|
|
return refData
|
2018-08-23 08:19:25 +00:00
|
|
|
}
|
|
|
|
|
2019-05-24 12:58:25 +00:00
|
|
|
/**
|
|
|
|
Removes a password.
|
|
|
|
|
|
|
|
- Parameter username: The username to remove the password for.
|
2020-12-20 09:26:28 +00:00
|
|
|
- Parameter context: An optional context.
|
2019-05-24 12:58:25 +00:00
|
|
|
- Returns: `true` if the password was successfully removed.
|
|
|
|
**/
|
2020-12-20 09:26:28 +00:00
|
|
|
@discardableResult public func removePassword(for username: String, context: String? = nil) -> Bool {
|
2018-08-23 08:19:25 +00:00
|
|
|
var query = [String: Any]()
|
2020-12-20 09:26:28 +00:00
|
|
|
setScope(query: &query, context: context)
|
2018-08-23 08:19:25 +00:00
|
|
|
query[kSecClass as String] = kSecClassGenericPassword
|
|
|
|
query[kSecAttrAccount as String] = username
|
2020-12-20 09:26:28 +00:00
|
|
|
|
2018-08-23 08:19:25 +00:00
|
|
|
let status = SecItemDelete(query as CFDictionary)
|
2020-12-20 09:26:28 +00:00
|
|
|
return status == errSecSuccess
|
2018-08-23 08:19:25 +00:00
|
|
|
}
|
|
|
|
|
2019-05-24 12:58:25 +00:00
|
|
|
/**
|
|
|
|
Gets a password.
|
|
|
|
|
|
|
|
- Parameter username: The username to get the password for.
|
2020-12-20 09:26:28 +00:00
|
|
|
- Parameter context: An optional context.
|
2019-05-24 12:58:25 +00:00
|
|
|
- Returns: The password for the input username.
|
|
|
|
- Throws: `KeychainError.notFound` if unable to find the password in the keychain.
|
|
|
|
**/
|
2020-12-20 09:26:28 +00:00
|
|
|
public func password(for username: String, context: String? = nil) throws -> String {
|
2018-08-23 08:19:25 +00:00
|
|
|
var query = [String: Any]()
|
2020-12-20 09:26:28 +00:00
|
|
|
setScope(query: &query, context: context)
|
2018-08-23 08:19:25 +00:00
|
|
|
query[kSecClass as String] = kSecClassGenericPassword
|
|
|
|
query[kSecAttrAccount as String] = username
|
|
|
|
query[kSecMatchLimit as String] = kSecMatchLimitOne
|
|
|
|
query[kSecReturnData as String] = true
|
|
|
|
|
|
|
|
var result: AnyObject?
|
2020-12-17 09:50:09 +00:00
|
|
|
switch SecItemCopyMatching(query as CFDictionary, &result) {
|
|
|
|
case errSecSuccess:
|
|
|
|
break
|
|
|
|
|
|
|
|
case errSecUserCanceled:
|
|
|
|
throw KeychainError.userCancelled
|
|
|
|
|
|
|
|
default:
|
2018-08-23 08:19:25 +00:00
|
|
|
throw KeychainError.notFound
|
|
|
|
}
|
|
|
|
guard let data = result as? Data else {
|
|
|
|
throw KeychainError.notFound
|
|
|
|
}
|
|
|
|
guard let password = String(data: data, encoding: .utf8) else {
|
|
|
|
throw KeychainError.notFound
|
|
|
|
}
|
|
|
|
return password
|
|
|
|
}
|
|
|
|
|
2019-05-24 12:58:25 +00:00
|
|
|
/**
|
|
|
|
Gets a password reference.
|
|
|
|
|
|
|
|
- Parameter username: The username to get the password for.
|
2020-12-20 09:26:28 +00:00
|
|
|
- Parameter context: An optional context.
|
2019-05-24 12:58:25 +00:00
|
|
|
- Returns: The password reference for the input username.
|
|
|
|
- Throws: `KeychainError.notFound` if unable to find the password in the keychain.
|
|
|
|
**/
|
2020-12-20 09:26:28 +00:00
|
|
|
public func passwordReference(for username: String, context: String? = nil) throws -> Data {
|
2018-08-23 08:19:25 +00:00
|
|
|
var query = [String: Any]()
|
2020-12-20 09:26:28 +00:00
|
|
|
setScope(query: &query, context: context)
|
2018-08-23 08:19:25 +00:00
|
|
|
query[kSecClass as String] = kSecClassGenericPassword
|
|
|
|
query[kSecAttrAccount as String] = username
|
|
|
|
query[kSecMatchLimit as String] = kSecMatchLimitOne
|
|
|
|
query[kSecReturnPersistentRef as String] = true
|
|
|
|
|
|
|
|
var result: AnyObject?
|
2020-12-17 09:50:09 +00:00
|
|
|
switch SecItemCopyMatching(query as CFDictionary, &result) {
|
|
|
|
case errSecSuccess:
|
|
|
|
break
|
|
|
|
|
|
|
|
case errSecUserCanceled:
|
|
|
|
throw KeychainError.userCancelled
|
|
|
|
|
|
|
|
default:
|
2018-08-23 08:19:25 +00:00
|
|
|
throw KeychainError.notFound
|
|
|
|
}
|
|
|
|
guard let data = result as? Data else {
|
|
|
|
throw KeychainError.notFound
|
|
|
|
}
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
2019-05-24 12:58:25 +00:00
|
|
|
/**
|
|
|
|
Gets a password associated with a password reference.
|
|
|
|
|
|
|
|
- Parameter reference: The password reference.
|
2021-11-22 23:24:01 +00:00
|
|
|
- Returns: The password for the input reference.
|
2019-05-24 12:58:25 +00:00
|
|
|
- Throws: `KeychainError.notFound` if unable to find the password in the keychain.
|
|
|
|
**/
|
2021-11-22 23:24:01 +00:00
|
|
|
public static func password(forReference reference: Data) throws -> String {
|
2018-08-23 08:19:25 +00:00
|
|
|
var query = [String: Any]()
|
2021-11-22 23:24:01 +00:00
|
|
|
query[kSecValuePersistentRef as String] = reference
|
2018-08-23 08:19:25 +00:00
|
|
|
query[kSecReturnData as String] = true
|
|
|
|
|
|
|
|
var result: AnyObject?
|
2020-12-17 09:50:09 +00:00
|
|
|
switch SecItemCopyMatching(query as CFDictionary, &result) {
|
|
|
|
case errSecSuccess:
|
|
|
|
break
|
|
|
|
|
|
|
|
case errSecUserCanceled:
|
|
|
|
throw KeychainError.userCancelled
|
|
|
|
|
|
|
|
default:
|
2018-08-23 08:19:25 +00:00
|
|
|
throw KeychainError.notFound
|
|
|
|
}
|
|
|
|
guard let data = result as? Data else {
|
|
|
|
throw KeychainError.notFound
|
|
|
|
}
|
|
|
|
guard let password = String(data: data, encoding: .utf8) else {
|
|
|
|
throw KeychainError.notFound
|
|
|
|
}
|
|
|
|
return password
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Key
|
|
|
|
|
|
|
|
// https://forums.developer.apple.com/thread/13748
|
|
|
|
|
2019-05-24 12:58:25 +00:00
|
|
|
/**
|
|
|
|
Adds a public key.
|
|
|
|
|
|
|
|
- Parameter identifier: The unique identifier.
|
|
|
|
- Parameter data: The public key data.
|
|
|
|
- Returns: The `SecKey` object representing the public key.
|
|
|
|
- Throws: `KeychainError.add` if unable to add the public key to the keychain.
|
|
|
|
**/
|
2018-08-23 08:19:25 +00:00
|
|
|
public func add(publicKeyWithIdentifier identifier: String, data: Data) throws -> SecKey {
|
|
|
|
var query = [String: Any]()
|
|
|
|
query[kSecClass as String] = kSecClassKey
|
|
|
|
query[kSecAttrApplicationTag as String] = identifier
|
|
|
|
query[kSecAttrKeyType as String] = kSecAttrKeyTypeRSA
|
|
|
|
query[kSecAttrKeyClass as String] = kSecAttrKeyClassPublic
|
|
|
|
query[kSecValueData as String] = data
|
|
|
|
|
|
|
|
// XXX
|
|
|
|
query.removeValue(forKey: kSecAttrService as String)
|
|
|
|
|
|
|
|
let status = SecItemAdd(query as CFDictionary, nil)
|
2020-12-17 09:50:09 +00:00
|
|
|
guard status == errSecSuccess else {
|
2018-08-23 08:19:25 +00:00
|
|
|
throw KeychainError.add
|
|
|
|
}
|
|
|
|
return try publicKey(withIdentifier: identifier)
|
|
|
|
}
|
|
|
|
|
2019-05-24 12:58:25 +00:00
|
|
|
/**
|
|
|
|
Gets a public key.
|
|
|
|
|
|
|
|
- Parameter identifier: The unique identifier.
|
|
|
|
- Returns: The `SecKey` object representing the public key.
|
|
|
|
- Throws: `KeychainError.notFound` if unable to find the public key in the keychain.
|
|
|
|
**/
|
2018-08-23 08:19:25 +00:00
|
|
|
public func publicKey(withIdentifier identifier: String) throws -> SecKey {
|
|
|
|
var query = [String: Any]()
|
|
|
|
query[kSecClass as String] = kSecClassKey
|
|
|
|
query[kSecAttrApplicationTag as String] = identifier
|
|
|
|
query[kSecAttrKeyType as String] = kSecAttrKeyTypeRSA
|
|
|
|
query[kSecAttrKeyClass as String] = kSecAttrKeyClassPublic
|
|
|
|
query[kSecReturnRef as String] = true
|
|
|
|
|
|
|
|
// XXX
|
|
|
|
query.removeValue(forKey: kSecAttrService as String)
|
|
|
|
|
|
|
|
var result: AnyObject?
|
2020-12-17 09:50:09 +00:00
|
|
|
switch SecItemCopyMatching(query as CFDictionary, &result) {
|
|
|
|
case errSecSuccess:
|
|
|
|
break
|
|
|
|
|
|
|
|
case errSecUserCanceled:
|
|
|
|
throw KeychainError.userCancelled
|
|
|
|
|
|
|
|
default:
|
2018-08-23 08:19:25 +00:00
|
|
|
throw KeychainError.notFound
|
|
|
|
}
|
|
|
|
// guard let key = result as? SecKey else {
|
|
|
|
// throw KeychainError.typeMismatch
|
|
|
|
// }
|
|
|
|
// return key
|
|
|
|
return result as! SecKey
|
|
|
|
}
|
|
|
|
|
2019-05-24 12:58:25 +00:00
|
|
|
/**
|
|
|
|
Removes a public key.
|
|
|
|
|
|
|
|
- Parameter identifier: The unique identifier.
|
|
|
|
- Returns: `true` if the public key was successfully removed.
|
|
|
|
**/
|
2018-08-23 08:19:25 +00:00
|
|
|
@discardableResult public func remove(publicKeyWithIdentifier identifier: String) -> Bool {
|
|
|
|
var query = [String: Any]()
|
|
|
|
query[kSecClass as String] = kSecClassKey
|
|
|
|
query[kSecAttrApplicationTag as String] = identifier
|
|
|
|
query[kSecAttrKeyType as String] = kSecAttrKeyTypeRSA
|
|
|
|
query[kSecAttrKeyClass as String] = kSecAttrKeyClassPublic
|
|
|
|
|
|
|
|
// XXX
|
|
|
|
query.removeValue(forKey: kSecAttrService as String)
|
|
|
|
|
|
|
|
let status = SecItemDelete(query as CFDictionary)
|
2020-12-20 09:26:28 +00:00
|
|
|
return status == errSecSuccess
|
2018-08-23 08:19:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Helpers
|
|
|
|
|
2020-12-20 09:26:28 +00:00
|
|
|
private func setScope(query: inout [String: Any], context: String?) {
|
|
|
|
if let accessGroup = accessGroup {
|
2018-08-23 08:19:25 +00:00
|
|
|
query[kSecAttrAccessGroup as String] = accessGroup
|
2021-02-07 19:11:46 +00:00
|
|
|
#if os(macOS)
|
|
|
|
query[kSecUseDataProtectionKeychain as String] = true
|
|
|
|
#endif
|
2020-12-20 09:26:28 +00:00
|
|
|
}
|
|
|
|
if let context = context {
|
|
|
|
query[kSecAttrService as String] = context
|
2018-08-23 08:19:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|