Sources/OrgApp/CryptPassphrases.swift
77 lines · 3201 bytes
1import Foundation
2import LocalAuthentication
3import OrgDocument
4import Security
5
6/// Passphrases for encrypted entries, kept in the Keychain by file when asked, and given back
7/// only after Touch ID, Face ID or the device's password.
8public enum CryptPassphrases {
9 static let service = "sh.krz.orgstar.crypt"
10
11 static func query(_ path: String) -> [String: Any] {
12 [kSecClass as String: kSecClassGenericPassword, kSecAttrService as String: service, kSecAttrAccount as String: path]
13 }
14
15 public static func store(_ passphrase: String, for path: String) {
16 forget(path)
17 var item = query(path)
18 item[kSecValueData as String] = Data(passphrase.utf8)
19 item[kSecAttrLabel as String] = "orgstar: passphrase for " + (path as NSString).lastPathComponent
20 SecItemAdd(item as CFDictionary, nil)
21 }
22
23 public static func forget(_ path: String) {
24 SecItemDelete(query(path) as CFDictionary)
25 }
26
27 public static func hasStored(_ path: String) -> Bool {
28 var q = query(path)
29 q[kSecReturnAttributes as String] = true
30 return SecItemCopyMatching(q as CFDictionary, nil) == errSecSuccess
31 }
32
33 /// The stored passphrase, once the person has proved who they are; nil otherwise.
34 public static func stored(for path: String) async -> String? {
35 guard hasStored(path) else { return nil }
36 let context = LAContext()
37 let reason = "unlock the passphrase for " + (path as NSString).lastPathComponent
38 guard (try? await context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: reason)) == true else { return nil }
39 var q = query(path)
40 q[kSecReturnData as String] = true
41 var result: AnyObject?
42 guard SecItemCopyMatching(q as CFDictionary, &result) == errSecSuccess, let data = result as? Data else { return nil }
43 return String(decoding: data, as: UTF8.self)
44 }
45}
46
47extension CryptHold {
48 /// What the held file's notice says.
49 public func notice(for path: String) -> String {
50 let name = (path as NSString).lastPathComponent
51 return switch self {
52 case .entries(1): String(localized: "1 tagged entry isn't encrypted. Changes to \(name) aren't saved until it is.")
53 case .entries(let count): String(localized: "\(count) tagged entries aren't encrypted. Changes to \(name) aren't saved until they are.")
54 case .file: String(localized: "\(name) isn't saved until it has a passphrase.")
55 }
56 }
57}
58
59extension CryptHold: LocalizedError {
60 public var errorDescription: String? {
61 switch self {
62 case .entries(1): String(localized: "1 tagged entry isn't encrypted")
63 case .entries(let count): String(localized: "\(count) tagged entries aren't encrypted")
64 case .file: String(localized: "The file has no passphrase yet")
65 }
66 }
67}
68
69extension CryptReadError: LocalizedError {
70 public var errorDescription: String? {
71 switch self {
72 case .locked: String(localized: "The file is encrypted, and its passphrase isn't known")
73 case .wrongPassphrase: String(localized: "Wrong passphrase")
74 case .failed(let reason): reason
75 }
76 }
77}