2019-09-08 23:00:46 +02:00
|
|
|
//
|
|
|
|
|
// PGPAgent.swift
|
|
|
|
|
// passKit
|
|
|
|
|
//
|
|
|
|
|
// Created by Yishi Lin on 2019/7/17.
|
|
|
|
|
// Copyright © 2019 Bob Sun. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
public class PGPAgent {
|
|
|
|
|
|
|
|
|
|
public static let shared: PGPAgent = PGPAgent()
|
|
|
|
|
|
|
|
|
|
private let keyStore: KeyStore
|
|
|
|
|
private var pgpInterface: PgpInterface?
|
2019-09-30 02:05:01 +08:00
|
|
|
private var latestDecryptStatus: Bool = true
|
2019-09-08 23:00:46 +02:00
|
|
|
|
|
|
|
|
public init(keyStore: KeyStore = AppKeychain.shared) {
|
|
|
|
|
self.keyStore = keyStore
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public func initKeys() throws {
|
|
|
|
|
guard let publicKey: String = keyStore.get(for: PgpKey.PUBLIC.getKeychainKey()),
|
|
|
|
|
let privateKey: String = keyStore.get(for: PgpKey.PRIVATE.getKeychainKey()) else {
|
2019-10-01 00:40:33 +08:00
|
|
|
pgpInterface = nil
|
2019-09-08 23:00:46 +02:00
|
|
|
throw AppError.KeyImport
|
|
|
|
|
}
|
|
|
|
|
do {
|
|
|
|
|
pgpInterface = try GopenPgp(publicArmoredKey: publicKey, privateArmoredKey: privateKey)
|
|
|
|
|
} catch {
|
|
|
|
|
pgpInterface = try ObjectivePgp(publicArmoredKey: publicKey, privateArmoredKey: privateKey)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public func uninitKeys() {
|
|
|
|
|
pgpInterface = nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-01 01:19:41 +08:00
|
|
|
public func getKeyId() throws -> String? {
|
|
|
|
|
try checkAndInit()
|
2019-09-08 23:00:46 +02:00
|
|
|
return pgpInterface?.keyId
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-11 23:23:38 -07:00
|
|
|
public func getShortKeyId() throws -> String? {
|
|
|
|
|
try checkAndInit()
|
|
|
|
|
return pgpInterface?.shortKeyId
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-08 23:00:46 +02:00
|
|
|
public func decrypt(encryptedData: Data, requestPGPKeyPassphrase: () -> String) throws -> Data? {
|
2019-09-30 02:05:01 +08:00
|
|
|
// Remember the previous status and set the current status
|
|
|
|
|
let previousDecryptStatus = self.latestDecryptStatus
|
|
|
|
|
self.latestDecryptStatus = false
|
|
|
|
|
// Init keys.
|
2019-09-08 23:00:46 +02:00
|
|
|
try checkAndInit()
|
2019-09-30 02:05:01 +08:00
|
|
|
// Get the PGP key passphrase.
|
|
|
|
|
var passphrase = ""
|
|
|
|
|
if previousDecryptStatus == false {
|
|
|
|
|
passphrase = requestPGPKeyPassphrase()
|
|
|
|
|
} else {
|
|
|
|
|
passphrase = keyStore.get(for: Globals.pgpKeyPassphrase) ?? requestPGPKeyPassphrase()
|
|
|
|
|
}
|
|
|
|
|
// Decrypt.
|
2020-04-11 23:23:38 -07:00
|
|
|
guard let result = try pgpInterface!.decrypt(encryptedData: encryptedData, keyID: "", passphrase: passphrase) else {
|
2019-09-30 02:05:01 +08:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
// The decryption step has succeed.
|
|
|
|
|
self.latestDecryptStatus = true
|
|
|
|
|
return result
|
2019-09-08 23:00:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public func encrypt(plainData: Data) throws -> Data {
|
|
|
|
|
try checkAndInit()
|
|
|
|
|
guard let pgpInterface = pgpInterface else {
|
|
|
|
|
throw AppError.Encryption
|
|
|
|
|
}
|
2020-04-11 23:23:38 -07:00
|
|
|
return try pgpInterface.encrypt(plainData: plainData, keyID: "")
|
2019-09-08 23:00:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public var isPrepared: Bool {
|
|
|
|
|
return keyStore.contains(key: PgpKey.PUBLIC.getKeychainKey())
|
|
|
|
|
&& keyStore.contains(key: PgpKey.PRIVATE.getKeychainKey())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func checkAndInit() throws {
|
|
|
|
|
if pgpInterface == nil || !keyStore.contains(key: Globals.pgpKeyPassphrase) {
|
|
|
|
|
try initKeys()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|