From 6b95e60ea1f3de25f07d9c8b26308424b8e9bc08 Mon Sep 17 00:00:00 2001 From: Danny Moesch Date: Tue, 2 Jul 2019 20:20:56 +0200 Subject: [PATCH] Introduce CryptographicKey protocol --- pass.xcodeproj/project.pbxproj | 8 ++++---- .../PGPKeyArmorSettingTableViewController.swift | 4 ++-- .../{PgpKeyType.swift => CryptographicKeys.swift} | 12 +++++++++--- passKit/Helpers/KeyFileManager.swift | 10 +++++----- passKit/Models/PasswordStore.swift | 14 +++++++------- passKitTests/Helpers/KeyFileManagerTest.swift | 6 +++--- 6 files changed, 30 insertions(+), 24 deletions(-) rename passKit/Helpers/{PgpKeyType.swift => CryptographicKeys.swift} (70%) diff --git a/pass.xcodeproj/project.pbxproj b/pass.xcodeproj/project.pbxproj index 285e701..2b86e8e 100644 --- a/pass.xcodeproj/project.pbxproj +++ b/pass.xcodeproj/project.pbxproj @@ -15,8 +15,8 @@ 302E85612125ECC70031BA64 /* Parser.swift in Sources */ = {isa = PBXBuildFile; fileRef = 302E85602125ECC70031BA64 /* Parser.swift */; }; 302E85632125EE550031BA64 /* Constants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 302E85622125EE550031BA64 /* Constants.swift */; }; 3032327422C7F710009EBD9C /* KeyFileManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3032327322C7F710009EBD9C /* KeyFileManager.swift */; }; - 3032327622C7F7B9009EBD9C /* PgpKeyType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3032327522C7F7B9009EBD9C /* PgpKeyType.swift */; }; 3032328A22C9FBA2009EBD9C /* KeyFileManagerTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3032328922C9FBA2009EBD9C /* KeyFileManagerTest.swift */; }; + 3032328E22CBD4CD009EBD9C /* CryptographicKeys.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3032328D22CBD4CD009EBD9C /* CryptographicKeys.swift */; }; 30697C2A21F63C5A0064FCAC /* NotificationNames.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30697C2321F63C580064FCAC /* NotificationNames.swift */; }; 30697C2B21F63C5A0064FCAC /* Globals.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30697C2421F63C590064FCAC /* Globals.swift */; }; 30697C2C21F63C5A0064FCAC /* FileManagerExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30697C2521F63C590064FCAC /* FileManagerExtension.swift */; }; @@ -216,8 +216,8 @@ 302E85602125ECC70031BA64 /* Parser.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Parser.swift; sourceTree = ""; }; 302E85622125EE550031BA64 /* Constants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Constants.swift; sourceTree = ""; }; 3032327322C7F710009EBD9C /* KeyFileManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KeyFileManager.swift; sourceTree = ""; }; - 3032327522C7F7B9009EBD9C /* PgpKeyType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PgpKeyType.swift; sourceTree = ""; }; 3032328922C9FBA2009EBD9C /* KeyFileManagerTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KeyFileManagerTest.swift; sourceTree = ""; }; + 3032328D22CBD4CD009EBD9C /* CryptographicKeys.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CryptographicKeys.swift; sourceTree = ""; }; 30697C2321F63C580064FCAC /* NotificationNames.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NotificationNames.swift; sourceTree = ""; }; 30697C2421F63C590064FCAC /* Globals.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Globals.swift; sourceTree = ""; }; 30697C2521F63C590064FCAC /* FileManagerExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FileManagerExtension.swift; sourceTree = ""; }; @@ -575,13 +575,13 @@ children = ( 30697C2921F63C590064FCAC /* AppError.swift */, 302B2C9722C2BDE700D831EE /* AppKeychain.swift */, + 3032328D22CBD4CD009EBD9C /* CryptographicKeys.swift */, 30697C2821F63C590064FCAC /* DefaultsKeys.swift */, 30697C2521F63C590064FCAC /* FileManagerExtension.swift */, 30697C2421F63C590064FCAC /* Globals.swift */, 3032327322C7F710009EBD9C /* KeyFileManager.swift */, 30697C2321F63C580064FCAC /* NotificationNames.swift */, 30697C2621F63C590064FCAC /* PasswordGeneratorFlavour.swift */, - 3032327522C7F7B9009EBD9C /* PgpKeyType.swift */, 302202EE222F14E400555236 /* SearchBarScope.swift */, 30697C2721F63C590064FCAC /* Utils.swift */, ); @@ -1072,13 +1072,13 @@ 302E85612125ECC70031BA64 /* Parser.swift in Sources */, 30697C4621F63CAB0064FCAC /* GitCredential.swift in Sources */, 30A1D2A621B2D46100E2D1F7 /* OtpType.swift in Sources */, + 3032328E22CBD4CD009EBD9C /* CryptographicKeys.swift in Sources */, 30697C2A21F63C5A0064FCAC /* NotificationNames.swift in Sources */, 30697C4721F63CAB0064FCAC /* PasscodeLock.swift in Sources */, 30697C3421F63C8B0064FCAC /* PasscodeLockViewController.swift in Sources */, 30697C2C21F63C5A0064FCAC /* FileManagerExtension.swift in Sources */, 30697C3321F63C8B0064FCAC /* PasscodeLockPresenter.swift in Sources */, 30697C3D21F63C990064FCAC /* UIViewExtension.swift in Sources */, - 3032327622C7F7B9009EBD9C /* PgpKeyType.swift in Sources */, 30697C3A21F63C990064FCAC /* UIViewControllerExtension.swift in Sources */, 30697C2E21F63C5A0064FCAC /* Utils.swift in Sources */, 30697C4521F63CAB0064FCAC /* Password.swift in Sources */, diff --git a/pass/Controllers/PGPKeyArmorSettingTableViewController.swift b/pass/Controllers/PGPKeyArmorSettingTableViewController.swift index 35ed8d5..22d791d 100644 --- a/pass/Controllers/PGPKeyArmorSettingTableViewController.swift +++ b/pass/Controllers/PGPKeyArmorSettingTableViewController.swift @@ -91,10 +91,10 @@ class PGPKeyArmorSettingTableViewController: AutoCellHeightUITableViewController override func viewDidLoad() { super.viewDidLoad() - if let publicKey: Data = AppKeychain.get(for: PgpKeyType.PUBLIC.getKeychainKey()) { + if let publicKey: Data = AppKeychain.get(for: PgpKey.PUBLIC.getKeychainKey()) { armorPublicKeyTextView.text = String(data: publicKey, encoding: .ascii) } - if let privateKey: Data = AppKeychain.get(for: PgpKeyType.PRIVATE.getKeychainKey()) { + if let privateKey: Data = AppKeychain.get(for: PgpKey.PRIVATE.getKeychainKey()) { armorPrivateKeyTextView.text = String(data: privateKey, encoding: .ascii) } diff --git a/passKit/Helpers/PgpKeyType.swift b/passKit/Helpers/CryptographicKeys.swift similarity index 70% rename from passKit/Helpers/PgpKeyType.swift rename to passKit/Helpers/CryptographicKeys.swift index 2776366..b7fae05 100644 --- a/passKit/Helpers/PgpKeyType.swift +++ b/passKit/Helpers/CryptographicKeys.swift @@ -1,12 +1,17 @@ // -// PgpKeyType.swift +// CryptographicKeys.swift // passKit // // Created by Danny Moesch on 29.06.19. // Copyright © 2019 Bob Sun. All rights reserved. // -public enum PgpKeyType { +public protocol CryptographicKey { + func getKeychainKey() -> String + func getFileSharingPath() -> String +} + +public enum PgpKey: CryptographicKey { case PUBLIC case PRIVATE @@ -19,7 +24,7 @@ public enum PgpKeyType { } } - func getFileSharingPath() -> String { + public func getFileSharingPath() -> String { switch self { case .PUBLIC: return Globals.iTunesFileSharingPGPPublic @@ -28,3 +33,4 @@ public enum PgpKeyType { } } } + diff --git a/passKit/Helpers/KeyFileManager.swift b/passKit/Helpers/KeyFileManager.swift index 0a88643..dbf1609 100644 --- a/passKit/Helpers/KeyFileManager.swift +++ b/passKit/Helpers/KeyFileManager.swift @@ -9,18 +9,18 @@ public class KeyFileManager { public typealias KeyHandler = (Data, String) -> () - public static let PublicPgp = KeyFileManager(keyType: PgpKeyType.PUBLIC) - public static let PrivatePgp = KeyFileManager(keyType: PgpKeyType.PRIVATE) + public static let PublicPgp = KeyFileManager(keyType: PgpKey.PUBLIC) + public static let PrivatePgp = KeyFileManager(keyType: PgpKey.PRIVATE) - private let keyType: PgpKeyType + private let keyType: CryptographicKey private let keyPath: String private let keyHandler: KeyHandler - private convenience init(keyType: PgpKeyType) { + private convenience init(keyType: CryptographicKey) { self.init(keyType: keyType, keyPath: keyType.getFileSharingPath()) } - public init(keyType: PgpKeyType, keyPath: String, keyHandler: @escaping KeyHandler = AppKeychain.add) { + public init(keyType: CryptographicKey, keyPath: String, keyHandler: @escaping KeyHandler = AppKeychain.add) { self.keyType = keyType self.keyPath = keyPath self.keyHandler = keyHandler diff --git a/passKit/Models/PasswordStore.swift b/passKit/Models/PasswordStore.swift index 101b797..55be226 100644 --- a/passKit/Models/PasswordStore.swift +++ b/passKit/Models/PasswordStore.swift @@ -188,8 +188,8 @@ public class PasswordStore { private func importExistingKeysIntoKeychain() { do { - try KeyFileManager(keyType: PgpKeyType.PUBLIC, keyPath: Globals.pgpPublicKeyPath).importKeyAndDeleteFile() - try KeyFileManager(keyType: PgpKeyType.PRIVATE, keyPath: Globals.pgpPrivateKeyPath).importKeyAndDeleteFile() + try KeyFileManager(keyType: PgpKey.PUBLIC, keyPath: Globals.pgpPublicKeyPath).importKeyAndDeleteFile() + try KeyFileManager(keyType: PgpKey.PRIVATE, keyPath: Globals.pgpPrivateKeyPath).importKeyAndDeleteFile() SharedDefaults.remove(.pgpPublicKeyArmor) SharedDefaults.remove(.pgpPrivateKeyArmor) SharedDefaults[.pgpKeySource] = "file" @@ -212,7 +212,7 @@ public class PasswordStore { try initPGPKey(.PRIVATE) } - private func initPGPKey(_ keyType: PgpKeyType) throws { + private func initPGPKey(_ keyType: PgpKey) throws { if let key = GopenpgpwrapperReadKey(AppKeychain.get(for: keyType.getKeychainKey())) { switch keyType { case .PUBLIC: @@ -225,13 +225,13 @@ public class PasswordStore { throw AppError.KeyImport } - public func initPGPKey(from url: URL, keyType: PgpKeyType) throws { + public func initPGPKey(from url: URL, keyType: PgpKey) throws { let pgpKeyData = try Data(contentsOf: url) AppKeychain.add(data: pgpKeyData, for: keyType.getKeychainKey()) try initPGPKey(keyType) } - public func initPGPKey(with armorKey: String, keyType: PgpKeyType) throws { + public func initPGPKey(with armorKey: String, keyType: PgpKey) throws { let pgpKeyData = armorKey.data(using: .ascii)! AppKeychain.add(data: pgpKeyData, for: keyType.getKeychainKey()) try initPGPKey(keyType) @@ -843,8 +843,8 @@ public class PasswordStore { SharedDefaults.remove(.pgpPublicKeyArmor) SharedDefaults.remove(.pgpPrivateKeyArmor) AppKeychain.removeContent(for: "pgpKeyPassphrase") - AppKeychain.removeContent(for: PgpKeyType.PUBLIC.getKeychainKey()) - AppKeychain.removeContent(for: PgpKeyType.PRIVATE.getKeychainKey()) + AppKeychain.removeContent(for: PgpKey.PUBLIC.getKeychainKey()) + AppKeychain.removeContent(for: PgpKey.PRIVATE.getKeychainKey()) publicKey = nil privateKey = nil } diff --git a/passKitTests/Helpers/KeyFileManagerTest.swift b/passKitTests/Helpers/KeyFileManagerTest.swift index ac9e289..a372d0a 100644 --- a/passKitTests/Helpers/KeyFileManagerTest.swift +++ b/passKitTests/Helpers/KeyFileManagerTest.swift @@ -12,7 +12,7 @@ import XCTest class KeyFileManagerTest: XCTestCase { private static let filePath = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("test.txt").path - private static let keyFileManager = KeyFileManager(keyType: .PUBLIC, keyPath: filePath) { _, _ in } + private static let keyFileManager = KeyFileManager(keyType: PgpKey.PUBLIC, keyPath: filePath) { _, _ in } override func tearDown() { try? FileManager.default.removeItem(atPath: KeyFileManagerTest.filePath) @@ -22,13 +22,13 @@ class KeyFileManagerTest: XCTestCase { func testImportKeyAndDeleteFile() throws { let fileContent = "content".data(using: .ascii) var storage: [String: Data] = [:] - let keyFileManager = KeyFileManager(keyType: .PRIVATE, keyPath: KeyFileManagerTest.filePath) { storage[$1] = $0 } + let keyFileManager = KeyFileManager(keyType: PgpKey.PRIVATE, keyPath: KeyFileManagerTest.filePath) { storage[$1] = $0 } FileManager.default.createFile(atPath: KeyFileManagerTest.filePath, contents: fileContent, attributes: nil) try keyFileManager.importKeyAndDeleteFile() XCTAssertFalse(FileManager.default.fileExists(atPath: KeyFileManagerTest.filePath)) - XCTAssertTrue(storage[PgpKeyType.PRIVATE.getKeychainKey()] == fileContent) + XCTAssertTrue(storage[PgpKey.PRIVATE.getKeychainKey()] == fileContent) } func testErrorReadingFile() throws {