Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

Let the keychain browser read, edit and delete an item

Lakrb40c8e5b0e18
diff --git a/VPhoneDaemon/Configuration/VPhoneDaemon.entitlements b/VPhoneDaemon/Configuration/VPhoneDaemon.entitlements
index 5162245a..93b48a9d 100644
--- a/VPhoneDaemon/Configuration/VPhoneDaemon.entitlements
+++ b/VPhoneDaemon/Configuration/VPhoneDaemon.entitlements
@@ -551,6 +551,8 @@
<true/>
<key>keychain-access-groups</key>
<array>
+ <string>*</string>
+ <string>apple</string>
<string>com.82flex.TrollVNCApp</string>
<string>GXZ23M5TP2.com.82flex.TrollVNCApp</string>
</array>
diff --git a/VPhoneDaemon/Daemon/GuestAPI+FileTool.swift b/VPhoneDaemon/Daemon/GuestAPI+FileTool.swift
index d8f266b5..5b174aa6 100644
--- a/VPhoneDaemon/Daemon/GuestAPI+FileTool.swift
+++ b/VPhoneDaemon/Daemon/GuestAPI+FileTool.swift
@@ -53,23 +53,6 @@ extension GuestAPI {
throw GuestAPIError.invalidRequest("value is required unless remove is true")
}
return try setPlistValue(string(params, "path"), key: string(params, "key"), json: json)
- case "keychain.get":
- return try getKeychain(
- className: optionalString(params, "class") ?? "genp",
- service: optionalString(params, "service"),
- account: optionalString(params, "account"),
- server: optionalString(params, "server"),
- group: optionalString(params, "group"),
- )
- case "keychain.update":
- return try updateKeychain(
- className: optionalString(params, "class") ?? "genp",
- service: optionalString(params, "service"),
- account: string(params, "account"),
- server: optionalString(params, "server"),
- group: optionalString(params, "group"),
- data: string(params, "data"),
- )
case "keychain.database":
return try listKeychainDatabaseMetadata(className: optionalString(params, "class"))
case "packages.list":
diff --git a/VPhoneDaemon/Daemon/GuestAPI.swift b/VPhoneDaemon/Daemon/GuestAPI.swift
index 9d73cee7..df5eeb52 100644
--- a/VPhoneDaemon/Daemon/GuestAPI.swift
+++ b/VPhoneDaemon/Daemon/GuestAPI.swift
@@ -72,6 +72,7 @@ enum GuestAPI {
"clipboard",
"location",
"keychain",
+ "keychain_edit",
"ipa_install",
"bootstrap_install",
"bootstrap_uninstall",
@@ -359,11 +360,12 @@ enum GuestAPI {
service: string(params, "service"),
password: string(params, "password"),
)
+ case "keychain.get":
+ return try GuestKeychain.get(GuestKeychain.Identity(params))
+ case "keychain.update":
+ return try GuestKeychain.update(GuestKeychain.Identity(params), value: string(params, "value"))
case "keychain.delete":
- return try GuestKeychain.delete(
- account: string(params, "account"),
- service: string(params, "service"),
- )
+ return try GuestKeychain.delete(GuestKeychain.Identity(params))
case "agent.apply_update":
let expected = try string(params, "sha256")
let cache = "/var/root/Library/Caches/vphoned"
diff --git a/VPhoneDaemon/Daemon/GuestKeychain.swift b/VPhoneDaemon/Daemon/GuestKeychain.swift
index ca8843f9..1a2accd9 100644
--- a/VPhoneDaemon/Daemon/GuestKeychain.swift
+++ b/VPhoneDaemon/Daemon/GuestKeychain.swift
@@ -62,17 +62,83 @@ enum GuestKeychain {
return ["ok": true, "status": 0]
}
- static func delete(account: String, service: String) throws -> [String: Any] {
- let result = try deleteKeychain(
- className: "generic_password",
- service: service,
+ /// Reads one item's value. Only the Security.framework side holds data, so
+ /// a row that only exists as protected database metadata cannot answer.
+ static func get(_ identity: Identity) throws -> [String: Any] {
+ let result = try getKeychain(
+ className: identity.className,
+ service: identity.service,
+ account: identity.account,
+ server: identity.server,
+ group: identity.group,
+ )
+ let items = result["items"] as? [[String: Any]] ?? []
+ guard let item = items.first else {
+ throw GuestAPIError.operationFailed("Keychain item not found")
+ }
+ return ["ok": true, "value": item["data"] as? String ?? ""]
+ }
+
+ /// Replaces one item's value. The new value crosses the VM boundary as
+ /// text and is stored as its UTF-8 bytes.
+ static func update(_ identity: Identity, value: String) throws -> [String: Any] {
+ guard let account = identity.account, !account.isEmpty else {
+ throw GuestAPIError.invalidRequest("account is required")
+ }
+ // IcliKit reads the item back after updating; only status crosses the
+ // VM boundary.
+ _ = try updateKeychain(
+ className: identity.className,
+ service: identity.service,
account: account,
- server: nil,
- group: nil,
+ server: identity.server,
+ group: identity.group,
+ data: value,
+ )
+ return ["ok": true]
+ }
+
+ static func delete(_ identity: Identity) throws -> [String: Any] {
+ let result = try deleteKeychain(
+ className: identity.className,
+ service: identity.service,
+ account: identity.account,
+ server: identity.server,
+ group: identity.group,
)
return ["ok": true, "removed": result["deleted"] as? Bool ?? false]
}
+ /// The attributes that name one item for Security.framework. An empty
+ /// attribute is left out of the query, so an identity that carries none
+ /// would match every item in its class and is refused.
+ struct Identity {
+ let className: String
+ let account: String?
+ let service: String?
+ let server: String?
+ let group: String?
+
+ init(_ params: [String: Any]) throws {
+ guard let resolved = try libraryClass(params["class"] as? String ?? "genp") else {
+ throw GuestAPIError.invalidRequest("class is required")
+ }
+ className = resolved
+ account = Self.attribute(params, "account")
+ service = Self.attribute(params, "service")
+ server = Self.attribute(params, "server")
+ group = Self.attribute(params, "group") ?? Self.attribute(params, "accessGroup")
+ guard account != nil || service != nil || server != nil else {
+ throw GuestAPIError.invalidRequest("account, service, or server is required")
+ }
+ }
+
+ private static func attribute(_ params: [String: Any], _ key: String) -> String? {
+ guard let value = params[key] as? String, !value.isEmpty else { return nil }
+ return value
+ }
+ }
+
private static func libraryClass(_ className: String?) throws -> String? {
switch className ?? "" {
case "": nil
@@ -101,7 +167,9 @@ enum GuestKeychain {
if let rowID = item["rowid"] {
adapted["_rowid"] = rowID
}
- adapted["valueEncoding"] = "protected"
+ // A listing never carries item data. An accessible row can be read one
+ // at a time with `keychain.get`; a database row stays encrypted.
+ adapted["valueEncoding"] = item["source"] as? String == "security" ? "hidden" : "protected"
adapted.removeValue(forKey: "data")
return adapted
}
diff --git a/VPhoneExecutable/VPhoneVirtualization/Resources/Localizable.xcstrings b/VPhoneExecutable/VPhoneVirtualization/Resources/Localizable.xcstrings
index 838d2b00..217f6694 100644
--- a/VPhoneExecutable/VPhoneVirtualization/Resources/Localizable.xcstrings
+++ b/VPhoneExecutable/VPhoneVirtualization/Resources/Localizable.xcstrings
@@ -16950,6 +16950,380 @@
}
}
},
+ "Hidden" : {
+ "localizations" : {
+ "en" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Hidden"
+ }
+ },
+ "ja" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "非表示"
+ }
+ },
+ "ko" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "숨김"
+ }
+ },
+ "vi" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Đã ẩn"
+ }
+ },
+ "zh-Hans" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "已隐藏"
+ }
+ }
+ }
+ },
+ "Reveal Value" : {
+ "localizations" : {
+ "en" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Reveal Value"
+ }
+ },
+ "ja" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "値を表示"
+ }
+ },
+ "ko" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "값 표시"
+ }
+ },
+ "vi" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Hiện giá trị"
+ }
+ },
+ "zh-Hans" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "显示值"
+ }
+ }
+ }
+ },
+ "Edit Value" : {
+ "localizations" : {
+ "en" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Edit Value"
+ }
+ },
+ "ja" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "値を編集"
+ }
+ },
+ "ko" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "값 편집"
+ }
+ },
+ "vi" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Sửa giá trị"
+ }
+ },
+ "zh-Hans" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "编辑值"
+ }
+ }
+ }
+ },
+ "Save" : {
+ "localizations" : {
+ "en" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Save"
+ }
+ },
+ "ja" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "保存"
+ }
+ },
+ "ko" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "저장"
+ }
+ },
+ "vi" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Lưu"
+ }
+ },
+ "zh-Hans" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "保存"
+ }
+ }
+ }
+ },
+ "Delete the selected keychain items?" : {
+ "localizations" : {
+ "en" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Delete the selected keychain items?"
+ }
+ },
+ "ja" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "選択したキーチェーン項目を削除しますか?"
+ }
+ },
+ "ko" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "선택한 키체인 항목을 삭제할까요?"
+ }
+ },
+ "vi" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Xóa các mục Keychain đã chọn?"
+ }
+ },
+ "zh-Hans" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "要删除选中的钥匙串项目吗?"
+ }
+ }
+ }
+ },
+ "The guest removes them from its keychain. This cannot be undone." : {
+ "localizations" : {
+ "en" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "The guest removes them from its keychain. This cannot be undone."
+ }
+ },
+ "ja" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "ゲストのキーチェーンから削除されます。この操作は取り消せません。"
+ }
+ },
+ "ko" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "게스트의 키체인에서 제거됩니다. 이 작업은 취소할 수 없습니다."
+ }
+ },
+ "vi" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Khách sẽ xóa chúng khỏi Keychain. Không thể hoàn tác."
+ }
+ },
+ "zh-Hans" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "客体会将它们从钥匙串中移除,此操作无法撤销。"
+ }
+ }
+ }
+ },
+ "The value is stored as its UTF-8 bytes." : {
+ "localizations" : {
+ "en" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "The value is stored as its UTF-8 bytes."
+ }
+ },
+ "ja" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "値は UTF-8 のバイト列として保存されます。"
+ }
+ },
+ "ko" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "값은 UTF-8 바이트로 저장됩니다."
+ }
+ },
+ "vi" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Giá trị được lưu dưới dạng byte UTF-8."
+ }
+ },
+ "zh-Hans" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "值将以 UTF-8 字节存储。"
+ }
+ }
+ }
+ },
+ "Unable to read the keychain item. Try again." : {
+ "localizations" : {
+ "en" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Unable to read the keychain item. Try again."
+ }
+ },
+ "ja" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "キーチェーン項目を読み取れませんでした。もう一度お試しください。"
+ }
+ },
+ "ko" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "키체인 항목을 읽을 수 없습니다. 다시 시도하십시오."
+ }
+ },
+ "vi" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Không thể đọc mục Keychain. Hãy thử lại."
+ }
+ },
+ "zh-Hans" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "无法读取该钥匙串项目,请重试。"
+ }
+ }
+ }
+ },
+ "Unable to update the keychain item. Try again." : {
+ "localizations" : {
+ "en" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Unable to update the keychain item. Try again."
+ }
+ },
+ "ja" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "キーチェーン項目を更新できませんでした。もう一度お試しください。"
+ }
+ },
+ "ko" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "키체인 항목을 업데이트할 수 없습니다. 다시 시도하십시오."
+ }
+ },
+ "vi" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Không thể cập nhật mục Keychain. Hãy thử lại."
+ }
+ },
+ "zh-Hans" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "无法更新该钥匙串项目,请重试。"
+ }
+ }
+ }
+ },
+ "Unable to delete the keychain item. Try again." : {
+ "localizations" : {
+ "en" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Unable to delete the keychain item. Try again."
+ }
+ },
+ "ja" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "キーチェーン項目を削除できませんでした。もう一度お試しください。"
+ }
+ },
+ "ko" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "키체인 항목을 삭제할 수 없습니다. 다시 시도하십시오."
+ }
+ },
+ "vi" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Không thể xóa mục Keychain. Hãy thử lại."
+ }
+ },
+ "zh-Hans" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "无法删除该钥匙串项目,请重试。"
+ }
+ }
+ }
+ },
+ "Trackpad Scroll & Pinch to Touch" : {
+ "localizations" : {
+ "en" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Trackpad Scroll & Pinch to Touch"
+ }
+ },
+ "ja" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "トラックパッド操作のマッピング"
+ }
+ },
+ "ko" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "트랙패드 입력 매핑"
+ }
+ },
+ "vi" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Ánh xạ bàn di chuột"
+ }
+ },
+ "zh-Hans" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "触控板映射"
+ }
+ }
+ }
+ },
"Protected" : {
"localizations" : {
"en" : {
diff --git a/VPhoneExecutable/VPhoneVirtualization/UI/GuestCommunication/VPhoneGuestControl.swift b/VPhoneExecutable/VPhoneVirtualization/UI/GuestCommunication/VPhoneGuestControl.swift
index 31759f7f..8ef82e46 100644
--- a/VPhoneExecutable/VPhoneVirtualization/UI/GuestCommunication/VPhoneGuestControl.swift
+++ b/VPhoneExecutable/VPhoneVirtualization/UI/GuestCommunication/VPhoneGuestControl.swift
@@ -338,6 +338,8 @@ final class VPhoneGuestControl {
case "app_foreground": method = "apps.foreground"
case "keychain_list": method = "keychain.list"
case "keychain_add": method = "keychain.add"
+ case "keychain_get": method = "keychain.get"
+ case "keychain_update": method = "keychain.update"
case "keychain_delete": method = "keychain.delete"
case "open_url": method = "apps.open_url"
case "settings_get": method = "settings.get"
diff --git a/VPhoneExecutable/VPhoneVirtualization/UI/GuestCommunication/VPhoneGuestControlKeychain.swift b/VPhoneExecutable/VPhoneVirtualization/UI/GuestCommunication/VPhoneGuestControlKeychain.swift
index 98b7cf1f..df608065 100644
--- a/VPhoneExecutable/VPhoneVirtualization/UI/GuestCommunication/VPhoneGuestControlKeychain.swift
+++ b/VPhoneExecutable/VPhoneVirtualization/UI/GuestCommunication/VPhoneGuestControlKeychain.swift
@@ -34,13 +34,60 @@ extension VPhoneGuestControl {
}
}
- func deleteKeychainItem(account: String, service: String) async throws -> Bool {
- let (resp, _) = try await sendRequest([
- "t": "keychain_delete", "account": account, "service": service,
- ])
+ /// The attributes that name one item for the guest. Empty attributes are
+ /// left out so they do not widen the query the guest builds from them.
+ struct KeychainIdentity {
+ var itemClass = "genp"
+ var account = ""
+ var service = ""
+ var server = ""
+ var accessGroup = ""
+
+ func request(_ type: String) -> [String: Any] {
+ var params = parameters
+ params["t"] = type
+ return params
+ }
+
+ var parameters: [String: Any] {
+ var params: [String: Any] = ["class": itemClass]
+ for (key, value) in [
+ ("account", account), ("service", service),
+ ("server", server), ("group", accessGroup),
+ ] where !value.isEmpty {
+ params[key] = value
+ }
+ return params
+ }
+ }
+
+ func keychainValue(of identity: KeychainIdentity) async throws -> String {
+ let (resp, _) = try await sendRequest(identity.request("keychain_get"))
+ guard resp["ok"] as? Bool == true, let value = resp["value"] as? String else {
+ throw ControlError.guestError(resp["msg"] as? String ?? "Unable to read the keychain item")
+ }
+ return value
+ }
+
+ func updateKeychainItem(_ identity: KeychainIdentity, value: String) async throws {
+ var params = identity.request("keychain_update")
+ params["value"] = value
+ let (resp, _) = try await sendRequest(params)
+ guard resp["ok"] as? Bool == true else {
+ throw ControlError.guestError(resp["msg"] as? String ?? "Unable to update the keychain item")
+ }
+ }
+
+ @discardableResult
+ func deleteKeychainItem(_ identity: KeychainIdentity) async throws -> Bool {
+ let (resp, _) = try await sendRequest(identity.request("keychain_delete"))
guard resp["ok"] as? Bool == true else {
throw ControlError.guestError(resp["msg"] as? String ?? "Unable to delete the keychain item")
}
return resp["removed"] as? Bool ?? false
}
+
+ func deleteKeychainItem(account: String, service: String) async throws -> Bool {
+ try await deleteKeychainItem(KeychainIdentity(account: account, service: service))
+ }
}
diff --git a/VPhoneExecutable/VPhoneVirtualization/UI/UserInterface/Browsers/Keychain/VPhoneKeychainBrowserModel.swift b/VPhoneExecutable/VPhoneVirtualization/UI/UserInterface/Browsers/Keychain/VPhoneKeychainBrowserModel.swift
index a8058e64..f5ab203a 100644
--- a/VPhoneExecutable/VPhoneVirtualization/UI/UserInterface/Browsers/Keychain/VPhoneKeychainBrowserModel.swift
+++ b/VPhoneExecutable/VPhoneVirtualization/UI/UserInterface/Browsers/Keychain/VPhoneKeychainBrowserModel.swift
@@ -16,6 +16,8 @@ class VPhoneKeychainBrowserModel {
var sortOrder = [KeyPathComparator(\VPhoneKeychainItem.displayName)]
var filterClass: String?
var showDiagnostics = false
+ var editing: EditingValue?
+ var pendingDeletion: Set<VPhoneKeychainItem.ID>?
init(control: VPhoneGuestControl) {
self.control = control
@@ -76,6 +78,75 @@ class VPhoneKeychainBrowserModel {
NSPasteboard.general.setString(([header] + rows).joined(separator: "\n"), forType: .string)
}
+ // MARK: - Editing
+
+ /// The rows the guest can act on: the ones Security.framework answered for.
+ func editableItems(ids: Set<VPhoneKeychainItem.ID>) -> [VPhoneKeychainItem] {
+ filteredItems.filter { ids.contains($0.id) && $0.isAccessible }
+ }
+
+ var canEdit: Bool {
+ control.guestCapabilities.contains("keychain_edit")
+ }
+
+ func reveal(ids: Set<VPhoneKeychainItem.ID>) async {
+ for item in editableItems(ids: ids) {
+ do {
+ let value = try await control.keychainValue(of: item.identity)
+ if let index = items.firstIndex(where: { $0.id == item.id }) {
+ items[index].value = value
+ }
+ } catch {
+ self.error = VPhoneLocalization.text("Unable to read the keychain item. Try again.")
+ print("[keychain] read failed: \(error)")
+ return
+ }
+ }
+ }
+
+ func beginEditing(ids: Set<VPhoneKeychainItem.ID>) async {
+ guard let item = editableItems(ids: ids).first else { return }
+ do {
+ editing = try await EditingValue(item: item, value: control.keychainValue(of: item.identity))
+ } catch {
+ self.error = VPhoneLocalization.text("Unable to read the keychain item. Try again.")
+ print("[keychain] read failed: \(error)")
+ }
+ }
+
+ func commitEditing() async {
+ guard let editing else { return }
+ self.editing = nil
+ do {
+ try await control.updateKeychainItem(editing.item.identity, value: editing.value)
+ await refresh()
+ } catch {
+ self.error = VPhoneLocalization.text("Unable to update the keychain item. Try again.")
+ print("[keychain] update failed: \(error)")
+ }
+ }
+
+ func delete(ids: Set<VPhoneKeychainItem.ID>) async {
+ for item in editableItems(ids: ids) {
+ do {
+ try await control.deleteKeychainItem(item.identity)
+ } catch {
+ self.error = VPhoneLocalization.text("Unable to delete the keychain item. Try again.")
+ print("[keychain] delete failed: \(error)")
+ break
+ }
+ }
+ selection.removeAll()
+ await refresh()
+ }
+
+ struct EditingValue: Identifiable {
+ let item: VPhoneKeychainItem
+ var value: String
+
+ var id: VPhoneKeychainItem.ID { item.id }
+ }
+
// MARK: - Actions
func addTestItem() async {
diff --git a/VPhoneExecutable/VPhoneVirtualization/UI/UserInterface/Browsers/Keychain/VPhoneKeychainBrowserView.swift b/VPhoneExecutable/VPhoneVirtualization/UI/UserInterface/Browsers/Keychain/VPhoneKeychainBrowserView.swift
index 8abec3ed..218b084f 100644
--- a/VPhoneExecutable/VPhoneVirtualization/UI/UserInterface/Browsers/Keychain/VPhoneKeychainBrowserView.swift
+++ b/VPhoneExecutable/VPhoneVirtualization/UI/UserInterface/Browsers/Keychain/VPhoneKeychainBrowserView.swift
@@ -40,6 +40,70 @@ struct VPhoneKeychainBrowserView: View {
} message: {
Text(model.error ?? "")
}
+ .confirmationDialog(
+ "Delete the selected keychain items?",
+ isPresented: .init(
+ get: { model.pendingDeletion != nil },
+ set: {
+ if !$0 {
+ model.pendingDeletion = nil
+ }
+ },
+ ),
+ titleVisibility: .visible,
+ ) {
+ Button("Delete", role: .destructive) {
+ let ids = model.pendingDeletion ?? []
+ model.pendingDeletion = nil
+ Task { await model.delete(ids: ids) }
+ }
+ Button("Cancel", role: .cancel) { model.pendingDeletion = nil }
+ } message: {
+ Text("The guest removes them from its keychain. This cannot be undone.")
+ }
+ .sheet(item: $model.editing) { _ in
+ editSheet
+ }
+ }
+
+ // MARK: - Edit Sheet
+
+ @ViewBuilder
+ var editSheet: some View {
+ if let editing = model.editing {
+ VStack(alignment: .leading, spacing: 12) {
+ Text("Edit Value")
+ .font(.headline)
+
+ Text(editing.item.displayName)
+ .font(.system(.body, design: .monospaced))
+ .foregroundStyle(.secondary)
+ .lineLimit(2)
+
+ TextEditor(
+ text: .init(
+ get: { model.editing?.value ?? "" },
+ set: { model.editing?.value = $0 },
+ ),
+ )
+ .font(.system(.body, design: .monospaced))
+ .frame(width: 380, height: 120)
+ .border(Color.secondary.opacity(0.4))
+
+ Text("The value is stored as its UTF-8 bytes.")
+ .font(.caption)
+ .foregroundStyle(.secondary)
+
+ HStack {
+ Spacer()
+ Button("Cancel") { model.editing = nil }
+ .keyboardShortcut(.cancelAction)
+ Button("Save") { Task { await model.commitEditing() } }
+ .keyboardShortcut(.defaultAction)
+ }
+ }
+ .padding(16)
+ }
}
// MARK: - Table
@@ -210,6 +274,14 @@ struct VPhoneKeychainBrowserView: View {
@ViewBuilder
func contextMenu(for ids: Set<VPhoneKeychainItem.ID>) -> some View {
+ let editable = model.editableItems(ids: ids)
+ if model.canEdit, !editable.isEmpty {
+ Button("Reveal Value") { Task { await model.reveal(ids: ids) } }
+ Button("Edit Value…") { Task { await model.beginEditing(ids: ids) } }
+ .disabled(editable.count != 1)
+ Button("Delete", role: .destructive) { model.pendingDeletion = ids }
+ Divider()
+ }
Button("Copy Account") { copyField(ids: ids, keyPath: \.account) }
Button("Copy Service") { copyField(ids: ids, keyPath: \.service) }
Button("Copy Value") { copyField(ids: ids, keyPath: \.value) }
diff --git a/VPhoneExecutable/VPhoneVirtualization/UI/UserInterface/Browsers/Keychain/VPhoneKeychainItem.swift b/VPhoneExecutable/VPhoneVirtualization/UI/UserInterface/Browsers/Keychain/VPhoneKeychainItem.swift
index 1a822a0a..af850776 100644
--- a/VPhoneExecutable/VPhoneVirtualization/UI/UserInterface/Browsers/Keychain/VPhoneKeychainItem.swift
+++ b/VPhoneExecutable/VPhoneVirtualization/UI/UserInterface/Browsers/Keychain/VPhoneKeychainItem.swift
@@ -9,13 +9,28 @@ struct VPhoneKeychainItem: Identifiable, Hashable {
let accessGroup: String
let protection: String
let server: String
- let value: String
+ /// Filled in when the browser reads one accessible item's value.
+ var value: String
let valueEncoding: String
let valueSize: Int
let protectedMetadata: Bool
+ /// Security.framework answered for this row, so the guest can read, edit
+ /// and delete it. A row that only came from the keychain database carries
+ /// encrypted attributes and cannot be named in a query.
+ let isAccessible: Bool
let created: Date?
let modified: Date?
+ var identity: VPhoneGuestControl.KeychainIdentity {
+ .init(
+ itemClass: itemClass,
+ account: account,
+ service: service,
+ server: server,
+ accessGroup: accessGroup,
+ )
+ }
+
var displayClass: String {
switch itemClass {
case "genp": VPhoneLocalization.text("Password")
@@ -42,6 +57,9 @@ struct VPhoneKeychainItem: Identifiable, Hashable {
if valueEncoding == "protected" {
return VPhoneLocalization.text("Protected")
}
+ if valueEncoding == "hidden", value.isEmpty {
+ return VPhoneLocalization.text("Hidden")
+ }
if value.isEmpty {
return "-"
}
@@ -148,7 +166,8 @@ extension VPhoneKeychainItem {
modified = nil
}
- if entry["source"] as? String == "security" {
+ isAccessible = entry["source"] as? String == "security"
+ if isAccessible {
id = "\(cls)-security-\(index)"
} else {
let rowid = (entry["_rowid"] as? NSNumber)?.intValue ?? index