diff --git a/pass/Controllers/SettingsTableViewController.swift b/pass/Controllers/SettingsTableViewController.swift index ea4d7ec..29f8cf0 100644 --- a/pass/Controllers/SettingsTableViewController.swift +++ b/pass/Controllers/SettingsTableViewController.swift @@ -251,16 +251,24 @@ class SettingsTableViewController: UITableViewController { let appDelegate = UIApplication.shared.delegate as! AppDelegate appDelegate.passcodeLockPresenter = PasscodeLockPresenter(mainWindow: appDelegate.window, configuration: Globals.passcodeConfiguration) } + + func pgpKeyExists() -> Bool { + return FileManager.default.fileExists(atPath: Globals.pgpPublicKeyPath) && + FileManager.default.fileExists(atPath: Globals.pgpPrivateKeyPath) + } func showPGPKeyActionSheet() { let optionMenu = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) var urlActionTitle = "Download from URL" var armorActionTitle = "ASCII-Armor Encrypted Key" + var fileActionTitle = "Use uploaded keys" if Defaults[.pgpKeySource] == "url" { urlActionTitle = "✓ \(urlActionTitle)" } else if Defaults[.pgpKeySource] == "armor" { armorActionTitle = "✓ \(armorActionTitle)" + } else if Defaults[.pgpKeySource] == "file" { + fileActionTitle = "✓ \(fileActionTitle)" } let urlAction = UIAlertAction(title: urlActionTitle, style: .default) { _ in self.performSegue(withIdentifier: "setPGPKeyByURLSegue", sender: self) @@ -271,6 +279,63 @@ class SettingsTableViewController: UITableViewController { let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) optionMenu.addAction(urlAction) optionMenu.addAction(armorAction) + + if (pgpKeyExists()) { + let fileAction = UIAlertAction(title: fileActionTitle, style: .default) { _ in + + SVProgressHUD.setDefaultMaskType(.black) + SVProgressHUD.setDefaultStyle(.light) + SVProgressHUD.show(withStatus: "Reading PGP key") + + let alert = UIAlertController( + title: "PGP Passphrase", + message: "Please fill in the passphrase for your PGP key.", + preferredStyle: UIAlertControllerStyle.alert + ) + + alert.addAction( + UIAlertAction( + title: "OK", + style: UIAlertActionStyle.default, + handler: {_ in + Utils.addPasswrodToKeychain( + name: "pgpKeyPassphrase", + password: alert.textFields!.first!.text! + ) + } + ) + ) + + alert.addTextField( + configurationHandler: {(textField: UITextField!) in + textField.text = Utils.getPasswordFromKeychain(name: "pgpKeyPassphrase") ?? "" + textField.isSecureTextEntry = true + } + ) + + + DispatchQueue.main.async { + try? PasswordStore.shared.initPGP( + pgpPublicKeyLocalPath: Globals.pgpPublicKeyPath, + pgpPrivateKeyLocalPath: Globals.pgpPrivateKeyPath + ) + + let key: PGPKey = PasswordStore.shared.getPgpPrivateKey() + Defaults[.pgpKeySource] = "file" + + if (key.isEncrypted) { + SVProgressHUD.dismiss() + self.present(alert, animated: true, completion: nil) + } + + SVProgressHUD.dismiss() + self.pgpKeyTableViewCell.detailTextLabel?.text = Defaults[.pgpKeyID] + } + + } + + optionMenu.addAction(fileAction) + } if Defaults[.pgpKeySource] != nil { let deleteAction = UIAlertAction(title: "Remove PGP Keys", style: .destructive) { _ in diff --git a/pass/Models/PasswordStore.swift b/pass/Models/PasswordStore.swift index 9d308d7..9047ce5 100644 --- a/pass/Models/PasswordStore.swift +++ b/pass/Models/PasswordStore.swift @@ -171,12 +171,16 @@ class PasswordStore { if pgp.getKeysOf(.secret).count == 0 { throw NSError(domain: "me.mssun.pass.error", code: 2, userInfo: [NSLocalizedDescriptionKey: "Cannot import seceret key."]) } - let key = pgp.getKeysOf(.public)[0] + let key: PGPKey = getPgpPrivateKey() Defaults[.pgpKeyID] = key.keyID!.shortKeyString if let gpgUser = key.users[0] as? PGPUser { Defaults[.pgpKeyUserID] = gpgUser.userID } } + + func getPgpPrivateKey() -> PGPKey { + return pgp.getKeysOf(.secret)[0] + } func initPGP(pgpPublicKeyURL: URL, pgpPublicKeyLocalPath: String, pgpPrivateKeyURL: URL, pgpPrivateKeyLocalPath: String) throws { let pgpPublicData = try Data(contentsOf: pgpPublicKeyURL) @@ -192,7 +196,6 @@ class PasswordStore { try initPGP(pgpPublicKeyLocalPath: pgpPublicKeyLocalPath, pgpPrivateKeyLocalPath: pgpPrivateKeyLocalPath) } - func cloneRepository(remoteRepoURL: URL, credential: GitCredential, transferProgressBlock: @escaping (UnsafePointer, UnsafeMutablePointer) -> Void,