Fix fail-safe mechanism for other decryption scenarios

This commit is contained in:
Mingshen Sun 2020-04-18 22:35:17 -07:00
parent 3e114daca1
commit fcc8961e46
No known key found for this signature in database
GPG key ID: 1F86BA2052FED3B4
15 changed files with 153 additions and 100 deletions

View file

@ -213,7 +213,7 @@ open class PasscodeLockViewController: UIViewController, UITextFieldDelegate {
}
}
}))
alert.addAction(UIAlertAction(title: "Dismiss".localize(), style: .cancel, handler: nil))
alert.addAction(UIAlertAction.dismiss())
self.present(alert, animated: true, completion: nil)
}

View file

@ -45,15 +45,20 @@ public class PGPAgent {
return pgpInterface?.shortKeyID.sorted() ?? []
}
public func decrypt(encryptedData: Data, keyID: String, requestPGPKeyPassphrase: (String) -> String) throws -> Data? {
public func decrypt(encryptedData: Data, keyID: String, requestPGPKeyPassphrase: @escaping (String) -> String) throws -> Data? {
// Init keys.
try checkAndInit()
guard let pgpInterface = pgpInterface else {
throw AppError.Decryption
}
var keyID = keyID;
if !pgpInterface.containsPrivateKey(with: keyID) {
throw AppError.PgpPrivateKeyNotFound(keyID: keyID)
if pgpInterface.keyID.count == 1 {
keyID = pgpInterface.keyID.first!
} else {
throw AppError.PgpPrivateKeyNotFound(keyID: keyID)
}
}
// Remember the previous status and set the current status

View file

@ -0,0 +1,53 @@
//
// UIAlertActionExtension.swift
// passKit
//
// Created by Sun, Mingshen on 4/17/20.
// Copyright © 2020 Bob Sun. All rights reserved.
//
import UIKit
import Foundation
extension UIAlertAction {
public static func cancelAndPopView(controller: UIViewController) -> UIAlertAction {
return cancel() { _ in
controller.navigationController?.popViewController(animated: true)
}
}
public static func cancel(handler: ((UIAlertAction) -> Void)? = nil) -> UIAlertAction {
cancel(with: "Cancel", handler: handler)
}
public static func dismiss(handler: ((UIAlertAction) -> Void)? = nil) -> UIAlertAction {
cancel(with: "Dismiss", handler: handler)
}
public static func cancel(with title: String, handler: ((UIAlertAction) -> Void)? = nil) -> UIAlertAction {
UIAlertAction(title: "Cancel".localize(), style: .cancel, handler: handler)
}
public static func ok(handler: ((UIAlertAction) -> Void)? = nil) -> UIAlertAction {
UIAlertAction(title: "Ok".localize(), style: .default, handler: handler)
}
public static func okAndPopView(controller: UIViewController) -> UIAlertAction {
return ok() { _ in
controller.navigationController?.popViewController(animated: true)
}
}
public static func selectKey(controller: UIViewController, handler: ((UIAlertAction) -> Void)?) -> UIAlertAction {
UIAlertAction(title: "Select Key", style: .default) { _ in
let selectKeyAlert = UIAlertController(title: "Select from imported keys", message: nil, preferredStyle: .actionSheet)
try? PGPAgent.shared.getShortKeyID().forEach({ k in
let action = UIAlertAction(title: k, style: .default, handler: handler)
selectKeyAlert.addAction(action)
})
selectKeyAlert.addAction(UIAlertAction.cancelAndPopView(controller: controller))
controller.present(selectKeyAlert, animated: true, completion: nil)
}
}
}

View file

@ -47,15 +47,15 @@ public class Utils {
let title = "Passphrase".localize() + " (\(keyID.suffix(8)))"
let message = "FillInPgpPassphrase.".localize()
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok".localize(), style: UIAlertAction.Style.default, handler: {_ in
alert.addAction(UIAlertAction.ok() { _ in
passphrase = alert.textFields?.first?.text ?? ""
sem.signal()
}))
alert.addTextField(configurationHandler: {(textField: UITextField!) in
})
alert.addTextField() { textField in
textField.text = AppKeychain.shared.get(for: AppKeychain.getPGPKeyPassphraseKey(keyID: keyID)) ?? ""
textField.isSecureTextEntry = true
})
controller.present(alert, animated: true, completion: nil)
}
controller.present(alert, animated: true)
}
let _ = sem.wait(timeout: DispatchTime.distantFuture)
if Defaults.isRememberPGPPassphraseOn {

View file

@ -213,4 +213,8 @@ public class Password {
// get and return the password
return self.otpToken?.currentPassword
}
public func getUsernameForCompletion() -> String {
username ?? login ?? nameFromPath ?? ""
}
}

View file

@ -698,7 +698,7 @@ public class PasswordStore {
return try storeRepository.localCommitsRelative(toRemoteBranch: remoteBranch)
}
public func decrypt(passwordEntity: PasswordEntity, keyID: String? = nil, requestPGPKeyPassphrase: (String) -> String) throws -> Password? {
public func decrypt(passwordEntity: PasswordEntity, keyID: String? = nil, requestPGPKeyPassphrase: @escaping (String) -> String) throws -> Password {
let encryptedDataPath = storeURL.appendingPathComponent(passwordEntity.getPath())
let keyID = keyID ?? findGPGID(from: encryptedDataPath)
let encryptedData = try Data(contentsOf: encryptedDataPath)