Introduce CryptographicKey protocol
This commit is contained in:
parent
d43a3be6f6
commit
6b95e60ea1
6 changed files with 30 additions and 24 deletions
|
|
@ -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 = "<group>"; };
|
||||
302E85622125EE550031BA64 /* Constants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Constants.swift; sourceTree = "<group>"; };
|
||||
3032327322C7F710009EBD9C /* KeyFileManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KeyFileManager.swift; sourceTree = "<group>"; };
|
||||
3032327522C7F7B9009EBD9C /* PgpKeyType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PgpKeyType.swift; sourceTree = "<group>"; };
|
||||
3032328922C9FBA2009EBD9C /* KeyFileManagerTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KeyFileManagerTest.swift; sourceTree = "<group>"; };
|
||||
3032328D22CBD4CD009EBD9C /* CryptographicKeys.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CryptographicKeys.swift; sourceTree = "<group>"; };
|
||||
30697C2321F63C580064FCAC /* NotificationNames.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NotificationNames.swift; sourceTree = "<group>"; };
|
||||
30697C2421F63C590064FCAC /* Globals.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Globals.swift; sourceTree = "<group>"; };
|
||||
30697C2521F63C590064FCAC /* FileManagerExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FileManagerExtension.swift; sourceTree = "<group>"; };
|
||||
|
|
@ -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 */,
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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 {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue