Check existence of PGP keys before encrypt/decrypt

This commit is contained in:
Mingshen Sun 2020-04-14 20:20:16 -07:00
parent 50dec23b02
commit 0cae6af60d
No known key found for this signature in database
GPG key ID: 1F86BA2052FED3B4
6 changed files with 41 additions and 4 deletions

View file

@ -51,6 +51,15 @@ public class PGPAgent {
self.latestDecryptStatus = false
// Init keys.
try checkAndInit()
guard let pgpInterface = pgpInterface else {
throw AppError.Decryption
}
if !pgpInterface.containsPrivateKey(with: keyID) {
throw AppError.PgpPrivateKeyNotFound(keyID: keyID)
}
// Get the PGP key passphrase.
var passphrase = ""
if previousDecryptStatus == false {
@ -59,7 +68,7 @@ public class PGPAgent {
passphrase = keyStore.get(for: AppKeychain.getPGPKeyPassphraseKey(keyID: keyID)) ?? requestPGPKeyPassphrase(keyID)
}
// Decrypt.
guard let result = try pgpInterface!.decrypt(encryptedData: encryptedData, keyID: keyID, passphrase: passphrase) else {
guard let result = try pgpInterface.decrypt(encryptedData: encryptedData, keyID: keyID, passphrase: passphrase) else {
return nil
}
// The decryption step has succeed.
@ -72,6 +81,9 @@ public class PGPAgent {
guard let pgpInterface = pgpInterface else {
throw AppError.Encryption
}
if !pgpInterface.containsPublicKey(with: keyID) {
throw AppError.PgpPublicKeyNotFound(keyID: keyID)
}
return try pgpInterface.encrypt(plainData: plainData, keyID: keyID)
}