Wrap GopenPGP errors into more understandable app errors
This commit is contained in:
parent
fa820e277f
commit
44cb864642
5 changed files with 23 additions and 4 deletions
|
|
@ -130,6 +130,8 @@
|
||||||
"RememberToRemoveKey" = "Vergiss das Löschen des Schlüssels nicht";
|
"RememberToRemoveKey" = "Vergiss das Löschen des Schlüssels nicht";
|
||||||
"RememberToRemoveKeyFromServer." = "Vergiss nicht, den Schlüssel wieder vom Server zu entfernen.";
|
"RememberToRemoveKeyFromServer." = "Vergiss nicht, den Schlüssel wieder vom Server zu entfernen.";
|
||||||
"RemovePgpKeys" = "PGP-Schlüssel 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
|
// App passcode
|
||||||
"RemovePasscode" = "Passcode entfernen";
|
"RemovePasscode" = "Passcode entfernen";
|
||||||
|
|
|
||||||
|
|
@ -131,6 +131,8 @@
|
||||||
"RememberToRemoveKeyFromServer." = "Remember to remove the key from the server.";
|
"RememberToRemoveKeyFromServer." = "Remember to remove the key from the server.";
|
||||||
"RemovePgpKeys" = "Remove PGP Keys";
|
"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.";
|
"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
|
// App passcode
|
||||||
"RemovePasscode" = "Remove Passcode";
|
"RemovePasscode" = "Remove Passcode";
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,11 @@ import Crypto
|
||||||
|
|
||||||
struct GopenPgp: PgpInterface {
|
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 publicKey: CryptoKeyRing
|
||||||
private let privateKey: CryptoKeyRing
|
private let privateKey: CryptoKeyRing
|
||||||
|
|
||||||
|
|
@ -22,9 +27,17 @@ struct GopenPgp: PgpInterface {
|
||||||
}
|
}
|
||||||
|
|
||||||
func decrypt(encryptedData: Data, passphrase: String) throws -> Data? {
|
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)
|
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 {
|
func encrypt(plainData: Data) throws -> Data {
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,8 @@ public enum AppError: Error, Equatable {
|
||||||
case GitCommit
|
case GitCommit
|
||||||
case PasswordEntity
|
case PasswordEntity
|
||||||
case PgpPublicKeyNotExist
|
case PgpPublicKeyNotExist
|
||||||
|
case KeyExpiredOrIncompatible
|
||||||
|
case WrongPassphrase
|
||||||
case WrongPasswordFilename
|
case WrongPasswordFilename
|
||||||
case Decryption
|
case Decryption
|
||||||
case Encryption
|
case Encryption
|
||||||
|
|
|
||||||
|
|
@ -86,7 +86,7 @@ class PGPAgentTest: XCTestCase {
|
||||||
try importKeys(ED25519.publicKey, RSA2048.privateKey)
|
try importKeys(ED25519.publicKey, RSA2048.privateKey)
|
||||||
XCTAssert(pgpAgent.isPrepared)
|
XCTAssert(pgpAgent.isPrepared)
|
||||||
XCTAssertThrowsError(try basicEncryptDecrypt(using: pgpAgent)) {
|
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.
|
// Provide the wrong passphrase.
|
||||||
XCTAssertThrowsError(try basicEncryptDecrypt(using: pgpAgent, requestPassphrase: provideIncorrectPassphrase)) {
|
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)
|
XCTAssertEqual(passphraseRequestCalledCount, 2)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue