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

@ -130,6 +130,8 @@
"RememberToRemoveKey" = "Vergiss das Löschen des Schlüssels nicht";
"RememberToRemoveKeyFromServer." = "Vergiss nicht, den Schlüssel wieder vom Server zu entfernen.";
"RemovePgpKeys" = "PGP-Schlüssel entfernen";
"KeyExpiredOrIncompatibleError." = "Der öffentliche PGP-Schlüssel ist eventuell abgelaufen oder inkompatibel mit dem privaten Schlüssel.";
"WrongPassphraseError." = "Das Passwort für den privaten PGP-Schlüssel ist falsch.";
// App passcode
"RemovePasscode" = "Passcode entfernen";

View file

@ -131,6 +131,8 @@
"RememberToRemoveKeyFromServer." = "Remember to remove the key from the server.";
"RemovePgpKeys" = "Remove PGP Keys";
"PgpCopyPublicAndPrivateKeyToPass." = "Copy your ASCII-armored public and private keys to Pass with names \"gpg_key.pub\" and \"gpg_key\" (without quotes) via iTunes. Then come back and click \"iTunes File Sharing\" to finish.";
"KeyExpiredOrIncompatibleError." = "PGP public key may be expired or incompatible with the private key.";
"WrongPassphraseError." = "Passphrase of your PGP secret key is wrong.";
// App passcode
"RemovePasscode" = "Remove Passcode";

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

View file

@ -86,7 +86,7 @@ class PGPAgentTest: XCTestCase {
try importKeys(ED25519.publicKey, RSA2048.privateKey)
XCTAssert(pgpAgent.isPrepared)
XCTAssertThrowsError(try basicEncryptDecrypt(using: pgpAgent)) {
XCTAssert($0.localizedDescription.contains("openpgp: incorrect key"))
XCTAssertEqual($0 as! AppError, AppError.KeyExpiredOrIncompatible)
}
}
@ -128,7 +128,7 @@ class PGPAgentTest: XCTestCase {
// Provide the wrong passphrase.
XCTAssertThrowsError(try basicEncryptDecrypt(using: pgpAgent, requestPassphrase: provideIncorrectPassphrase)) {
XCTAssert($0.localizedDescription.contains("openpgp: invalid data: private key checksum failure"))
XCTAssertEqual($0 as! AppError, AppError.WrongPassphrase)
}
XCTAssertEqual(passphraseRequestCalledCount, 2)