metatext-app-ios-iphone-ipad/Secrets/Sources/Secrets/Secrets.swift

229 lines
6.6 KiB
Swift
Raw Normal View History

// Copyright © 2020 Metabolist. All rights reserved.
2020-09-06 23:37:54 +02:00
import Base16
import Foundation
2020-09-04 02:54:05 +02:00
import Keychain
2020-08-31 12:21:01 +02:00
public protocol SecretsStorable {
var dataStoredInSecrets: Data { get }
static func fromDataStoredInSecrets(_ data: Data) throws -> Self
}
enum SecretsStorableError: Error {
case conversionFromDataStoredInSecrets(Data)
}
2020-09-04 02:54:05 +02:00
public struct Secrets {
2020-08-31 12:21:01 +02:00
public let identityID: UUID
2020-09-04 02:54:05 +02:00
private let keychain: Keychain.Type
2020-09-04 02:54:05 +02:00
public init(identityID: UUID, keychain: Keychain.Type) {
2020-08-09 04:52:41 +02:00
self.identityID = identityID
2020-09-04 02:54:05 +02:00
self.keychain = keychain
}
}
2020-09-04 02:54:05 +02:00
public extension Secrets {
2020-08-09 04:52:41 +02:00
enum Item: String, CaseIterable {
2020-08-12 09:24:39 +02:00
case clientID
case clientSecret
case accessToken
case pushKey
case pushAuth
2020-09-04 11:44:25 +02:00
case databaseKey
}
}
2020-09-04 11:44:25 +02:00
enum SecretsError: Error {
case itemAbsent
}
2020-09-04 02:54:05 +02:00
extension Secrets.Item {
2020-08-14 03:59:17 +02:00
enum Kind {
case genericPassword
case key
}
2020-09-04 11:44:25 +02:00
// Note `databaseKey` is a generic password and not a key
2020-08-14 03:59:17 +02:00
var kind: Kind {
switch self {
case .pushKey: return .key
default: return .genericPassword
}
}
}
2020-09-04 02:54:05 +02:00
public extension Secrets {
2020-09-04 11:44:25 +02:00
// https://www.zetetic.net/sqlcipher/sqlcipher-api/#key
static func databaseKey(identityID: UUID?, keychain: Keychain.Type) throws -> String {
let passphraseData: Data
2020-09-04 08:44:04 +02:00
let scopedSecrets: Secrets?
2020-09-04 08:12:06 +02:00
2020-09-04 08:44:04 +02:00
if let identityID = identityID {
scopedSecrets = Secrets(identityID: identityID, keychain: keychain)
} else {
scopedSecrets = nil
2020-09-04 08:12:06 +02:00
}
2020-09-04 08:44:04 +02:00
do {
2020-09-04 11:44:25 +02:00
passphraseData = try scopedSecrets?.item(.databaseKey)
?? unscopedItem(.databaseKey, keychain: keychain)
2020-09-04 08:44:04 +02:00
} catch SecretsError.itemAbsent {
2020-09-04 11:44:25 +02:00
var bytes = [UInt8](repeating: 0, count: databaseKeyLength)
let status = SecRandomCopyBytes(kSecRandomDefault, databaseKeyLength, &bytes)
2020-09-04 08:12:06 +02:00
2020-09-04 08:44:04 +02:00
if status == errSecSuccess {
2020-09-04 11:44:25 +02:00
passphraseData = Data(bytes)
2020-09-04 08:44:04 +02:00
if let scopedSecrets = scopedSecrets {
2020-09-04 11:44:25 +02:00
try scopedSecrets.set(passphraseData, forItem: .databaseKey)
2020-09-04 08:44:04 +02:00
} else {
2020-09-04 11:44:25 +02:00
try setUnscoped(passphraseData, forItem: .databaseKey, keychain: keychain)
2020-09-04 08:44:04 +02:00
}
} else {
throw NSError(status: status)
}
}
2020-09-04 11:44:25 +02:00
2020-09-06 23:37:54 +02:00
return "x'\(passphraseData.base16EncodedString(options: [.uppercase]))'"
}
2020-08-09 04:52:41 +02:00
func deleteAllItems() throws {
2020-09-04 02:54:05 +02:00
for item in Secrets.Item.allCases {
2020-08-14 03:59:17 +02:00
switch item.kind {
case .genericPassword:
2020-09-04 02:54:05 +02:00
try keychain.deleteGenericPassword(
2020-09-04 08:12:06 +02:00
account: scopedKey(item: item),
2020-08-14 03:59:17 +02:00
service: Self.keychainServiceName)
case .key:
2020-09-04 08:12:06 +02:00
try keychain.deleteKey(applicationTag: scopedKey(item: item))
2020-08-14 03:59:17 +02:00
}
2020-08-09 04:52:41 +02:00
}
}
2020-08-12 09:24:39 +02:00
2020-09-04 08:44:04 +02:00
func getClientID() throws -> String {
try item(.clientID)
}
func setClientID(_ clientID: String) throws {
try set(clientID, forItem: .clientID)
}
func getClientSecret() throws -> String {
try item(.clientSecret)
}
func setClientSecret(_ clientSecret: String) throws {
try set(clientSecret, forItem: .clientSecret)
}
func getAccessToken() throws -> String {
try item(.accessToken)
}
func setAccessToken(_ accessToken: String) throws {
try set(accessToken, forItem: .accessToken)
}
2020-08-12 09:24:39 +02:00
func generatePushKeyAndReturnPublicKey() throws -> Data {
2020-09-04 02:54:05 +02:00
try keychain.generateKeyAndReturnPublicKey(
2020-09-04 08:12:06 +02:00
applicationTag: scopedKey(item: .pushKey),
attributes: PushKey.attributes)
2020-08-12 09:24:39 +02:00
}
func getPushKey() throws -> Data? {
2020-09-04 02:54:05 +02:00
try keychain.getPrivateKey(
2020-09-04 08:12:06 +02:00
applicationTag: scopedKey(item: .pushKey),
attributes: PushKey.attributes)
2020-08-12 09:24:39 +02:00
}
func generatePushAuth() throws -> Data {
var bytes = [UInt8](repeating: 0, count: PushKey.authLength)
2020-09-04 11:44:25 +02:00
let status = SecRandomCopyBytes(kSecRandomDefault, PushKey.authLength, &bytes)
2020-08-12 09:24:39 +02:00
2020-09-04 11:44:25 +02:00
if status == errSecSuccess {
let pushAuth = Data(bytes)
2020-08-12 09:24:39 +02:00
2020-09-04 11:44:25 +02:00
try set(pushAuth, forItem: .pushAuth)
2020-08-12 09:24:39 +02:00
2020-09-04 11:44:25 +02:00
return pushAuth
} else {
throw NSError(status: status)
}
2020-08-12 09:24:39 +02:00
}
func getPushAuth() throws -> Data? {
try item(.pushAuth)
}
}
2020-09-04 02:54:05 +02:00
private extension Secrets {
2020-08-12 09:24:39 +02:00
static let keychainServiceName = "com.metabolist.metatext"
2020-09-04 11:44:25 +02:00
static let databaseKeyLength = 32
2020-09-04 08:44:04 +02:00
private static func set(_ data: SecretsStorable, forAccount account: String, keychain: Keychain.Type) throws {
try keychain.setGenericPassword(
data: data.dataStoredInSecrets,
forAccount: account,
service: keychainServiceName)
}
private static func get<T: SecretsStorable>(account: String, keychain: Keychain.Type) throws -> T {
guard let data = try keychain.getGenericPassword(
account: account,
service: keychainServiceName) else {
throw SecretsError.itemAbsent
}
return try T.fromDataStoredInSecrets(data)
}
static func setUnscoped(_ data: SecretsStorable, forItem item: Item, keychain: Keychain.Type) throws {
try set(data, forAccount: item.rawValue, keychain: keychain)
}
static func unscopedItem<T: SecretsStorable>(_ item: Item, keychain: Keychain.Type) throws -> T {
try get(account: item.rawValue, keychain: keychain)
}
2020-08-12 09:24:39 +02:00
2020-09-04 08:12:06 +02:00
func scopedKey(item: Item) -> String {
identityID.uuidString + "." + item.rawValue
}
2020-09-04 08:44:04 +02:00
func set(_ data: SecretsStorable, forItem item: Item) throws {
try Self.set(data, forAccount: scopedKey(item: item), keychain: keychain)
}
func item<T: SecretsStorable>(_ item: Item) throws -> T {
try Self.get(account: scopedKey(item: item), keychain: keychain)
}
}
extension Data: SecretsStorable {
2020-08-31 12:21:01 +02:00
public var dataStoredInSecrets: Data { self }
2020-08-31 12:21:01 +02:00
public static func fromDataStoredInSecrets(_ data: Data) throws -> Data {
data
}
}
extension String: SecretsStorable {
2020-08-31 12:21:01 +02:00
public var dataStoredInSecrets: Data { Data(utf8) }
2020-08-31 12:21:01 +02:00
public static func fromDataStoredInSecrets(_ data: Data) throws -> String {
guard let string = String(data: data, encoding: .utf8) else {
throw SecretsStorableError.conversionFromDataStoredInSecrets(data)
}
return string
}
}
2020-08-31 12:21:01 +02:00
private struct PushKey {
static let authLength = 16
static let sizeInBits = 256
static let attributes: [String: Any] = [
kSecAttrKeyType as String: kSecAttrKeyTypeECSECPrimeRandom,
kSecAttrKeySizeInBits as String: sizeInBits]
}