Wrap GopenPGP errors into more understandable app errors

This commit is contained in:
Danny Moesch 2019-10-20 12:14:51 +02:00 committed by Mingshen Sun
parent fa820e277f
commit 44cb864642
5 changed files with 23 additions and 4 deletions

View file

@ -10,6 +10,11 @@ import Crypto
struct GopenPgp: PgpInterface {
private static let errorMapping: [String: Error] = [
"openpgp: invalid data: private key checksum failure": AppError.WrongPassphrase,
"openpgp: incorrect key": AppError.KeyExpiredOrIncompatible,
]
private let publicKey: CryptoKeyRing
private let privateKey: CryptoKeyRing
@ -22,9 +27,17 @@ struct GopenPgp: PgpInterface {
}
func decrypt(encryptedData: Data, passphrase: String) throws -> Data? {
try privateKey.unlock(withPassphrase: passphrase)
do {
try privateKey.unlock(withPassphrase: passphrase)
} catch {
throw Self.errorMapping[error.localizedDescription, default: error]
}
let message = createPgpMessage(from: encryptedData)
return try privateKey.decrypt(message, verifyKey: nil, verifyTime: 0).data
do {
return try privateKey.decrypt(message, verifyKey: nil, verifyTime: 0).data
} catch {
throw Self.errorMapping[error.localizedDescription, default: error]
}
}
func encrypt(plainData: Data) throws -> Data {

View file

@ -17,6 +17,8 @@ public enum AppError: Error, Equatable {
case GitCommit
case PasswordEntity
case PgpPublicKeyNotExist
case KeyExpiredOrIncompatible
case WrongPassphrase
case WrongPasswordFilename
case Decryption
case Encryption