|
@@ -13,20 +13,30 @@ public class MasterKeyUtil {
|
|
static let shared = MasterKeyUtil()
|
|
static let shared = MasterKeyUtil()
|
|
private let keyAlias = "_iosx_security_master_key"
|
|
private let keyAlias = "_iosx_security_master_key"
|
|
private let prefsKeyAlias = "_iosx_security_master_key_easysoft_"
|
|
private let prefsKeyAlias = "_iosx_security_master_key_easysoft_"
|
|
|
|
+ private let serverKeyAlias = "_iosx_security_master_key_server_"
|
|
|
|
|
|
private init() {}
|
|
private init() {}
|
|
|
|
|
|
- func generateAndStoreKey() throws {
|
|
|
|
- if try isKeyExists(keyAliasCode: keyAlias) {
|
|
|
|
|
|
+ func base64toData(_ base64: String) -> Data? {
|
|
|
|
+ guard let data = Data(base64Encoded: base64) else {
|
|
|
|
+ return nil
|
|
|
|
+ }
|
|
|
|
+ return data
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ func generateAndStoreKey(_ alias: String, key_s: String? = nil) throws {
|
|
|
|
+ if try isKeyExists(keyAliasCode: alias) {
|
|
// print("Master Key already exists, skipping generation.")
|
|
// print("Master Key already exists, skipping generation.")
|
|
return
|
|
return
|
|
}
|
|
}
|
|
- let key = SymmetricKey(size: .bits256)
|
|
|
|
- let keyData = key.withUnsafeBytes { Data($0) }
|
|
|
|
|
|
|
|
|
|
+ let key = (key_s != nil) ? nil : SymmetricKey(size: .bits256)
|
|
|
|
+ guard let keyData = key?.withUnsafeBytes({ Data($0) }) ?? base64toData(key_s!) else {
|
|
|
|
+ return
|
|
|
|
+ }
|
|
let query: [String: Any] = [
|
|
let query: [String: Any] = [
|
|
kSecClass as String: kSecClassKey,
|
|
kSecClass as String: kSecClassKey,
|
|
- kSecAttrApplicationTag as String: keyAlias,
|
|
|
|
|
|
+ kSecAttrApplicationTag as String: alias,
|
|
kSecValueData as String: keyData,
|
|
kSecValueData as String: keyData,
|
|
kSecAttrAccessible as String: kSecAttrAccessibleAfterFirstUnlock
|
|
kSecAttrAccessible as String: kSecAttrAccessibleAfterFirstUnlock
|
|
]
|
|
]
|
|
@@ -39,25 +49,15 @@ public class MasterKeyUtil {
|
|
}
|
|
}
|
|
|
|
|
|
func generateAndStorePrefsKey() throws {
|
|
func generateAndStorePrefsKey() throws {
|
|
- if try isKeyExists(keyAliasCode: prefsKeyAlias) {
|
|
|
|
-// print("Prefs Key already exists, skipping generation.")
|
|
|
|
- return
|
|
|
|
- }
|
|
|
|
- let key = SymmetricKey(size: .bits256)
|
|
|
|
- let keyData = key.withUnsafeBytes { Data($0) }
|
|
|
|
-
|
|
|
|
- let query: [String: Any] = [
|
|
|
|
- kSecClass as String: kSecClassKey,
|
|
|
|
- kSecAttrApplicationTag as String: prefsKeyAlias,
|
|
|
|
- kSecValueData as String: keyData,
|
|
|
|
- kSecAttrAccessible as String: kSecAttrAccessibleAfterFirstUnlock
|
|
|
|
- ]
|
|
|
|
-
|
|
|
|
- SecItemDelete(query as CFDictionary) // Remove if it exists
|
|
|
|
- let status = SecItemAdd(query as CFDictionary, nil)
|
|
|
|
- guard status == errSecSuccess else {
|
|
|
|
- throw NSError(domain: "KeychainError", code: Int(status), userInfo: nil)
|
|
|
|
- }
|
|
|
|
|
|
+ try generateAndStoreKey(prefsKeyAlias)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ func generateAndStoreMasterKey() throws {
|
|
|
|
+ try generateAndStoreKey(keyAlias)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ func generateAndStoreServerKey(_ key_s: String) throws {
|
|
|
|
+ try generateAndStoreKey(serverKeyAlias, key_s: key_s)
|
|
}
|
|
}
|
|
|
|
|
|
func isDeviceNotSecure() -> Bool {
|
|
func isDeviceNotSecure() -> Bool {
|
|
@@ -150,6 +150,26 @@ public class MasterKeyUtil {
|
|
return SymmetricKey(data: keyData)
|
|
return SymmetricKey(data: keyData)
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ func getServerKey() throws -> SymmetricKey {
|
|
|
|
+ let query: [String: Any] = [
|
|
|
|
+ kSecClass as String: kSecClassKey,
|
|
|
|
+ kSecAttrApplicationTag as String: serverKeyAlias,
|
|
|
|
+ kSecReturnData as String: true
|
|
|
|
+ ]
|
|
|
|
+
|
|
|
|
+ var item: CFTypeRef?
|
|
|
|
+ let status = SecItemCopyMatching(query as CFDictionary, &item)
|
|
|
|
+ guard status == errSecSuccess else {
|
|
|
|
+ throw NSError(domain: "KeychainError", code: Int(status), userInfo: nil)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ guard let keyData = item as? Data else {
|
|
|
|
+ throw NSError(domain: "KeyRetrievalError", code: -1, userInfo: nil)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return SymmetricKey(data: keyData)
|
|
|
|
+ }
|
|
|
|
+
|
|
func encryptP(data: Data) throws -> Data {
|
|
func encryptP(data: Data) throws -> Data {
|
|
let key = try getPrefsKey()
|
|
let key = try getPrefsKey()
|
|
let sealedBox = try AES.GCM.seal(data, using: key)
|
|
let sealedBox = try AES.GCM.seal(data, using: key)
|