Introduce CryptographicKey protocol

This commit is contained in:
Danny Moesch 2019-07-02 20:20:56 +02:00 committed by Mingshen Sun
parent d43a3be6f6
commit 6b95e60ea1
6 changed files with 30 additions and 24 deletions

View file

@ -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 {
}
}
}

View file

@ -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

View file

@ -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
}