Allow usage of uploaded PGP keys

This commit is contained in:
Evgeny Morozov 2017-02-24 17:59:04 +03:00 committed by Bob Sun
parent 5e581aa6da
commit bb654849af
2 changed files with 70 additions and 2 deletions

View file

@ -252,15 +252,23 @@ class SettingsTableViewController: UITableViewController {
appDelegate.passcodeLockPresenter = PasscodeLockPresenter(mainWindow: appDelegate.window, configuration: Globals.passcodeConfiguration) 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() { func showPGPKeyActionSheet() {
let optionMenu = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) let optionMenu = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
var urlActionTitle = "Download from URL" var urlActionTitle = "Download from URL"
var armorActionTitle = "ASCII-Armor Encrypted Key" var armorActionTitle = "ASCII-Armor Encrypted Key"
var fileActionTitle = "Use uploaded keys"
if Defaults[.pgpKeySource] == "url" { if Defaults[.pgpKeySource] == "url" {
urlActionTitle = "\(urlActionTitle)" urlActionTitle = "\(urlActionTitle)"
} else if Defaults[.pgpKeySource] == "armor" { } else if Defaults[.pgpKeySource] == "armor" {
armorActionTitle = "\(armorActionTitle)" armorActionTitle = "\(armorActionTitle)"
} else if Defaults[.pgpKeySource] == "file" {
fileActionTitle = "\(fileActionTitle)"
} }
let urlAction = UIAlertAction(title: urlActionTitle, style: .default) { _ in let urlAction = UIAlertAction(title: urlActionTitle, style: .default) { _ in
self.performSegue(withIdentifier: "setPGPKeyByURLSegue", sender: self) self.performSegue(withIdentifier: "setPGPKeyByURLSegue", sender: self)
@ -272,6 +280,63 @@ class SettingsTableViewController: UITableViewController {
optionMenu.addAction(urlAction) optionMenu.addAction(urlAction)
optionMenu.addAction(armorAction) 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 { if Defaults[.pgpKeySource] != nil {
let deleteAction = UIAlertAction(title: "Remove PGP Keys", style: .destructive) { _ in let deleteAction = UIAlertAction(title: "Remove PGP Keys", style: .destructive) { _ in
Utils.removePGPKeys() Utils.removePGPKeys()

View file

@ -171,13 +171,17 @@ class PasswordStore {
if pgp.getKeysOf(.secret).count == 0 { if pgp.getKeysOf(.secret).count == 0 {
throw NSError(domain: "me.mssun.pass.error", code: 2, userInfo: [NSLocalizedDescriptionKey: "Cannot import seceret key."]) 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 Defaults[.pgpKeyID] = key.keyID!.shortKeyString
if let gpgUser = key.users[0] as? PGPUser { if let gpgUser = key.users[0] as? PGPUser {
Defaults[.pgpKeyUserID] = gpgUser.userID Defaults[.pgpKeyUserID] = gpgUser.userID
} }
} }
func getPgpPrivateKey() -> PGPKey {
return pgp.getKeysOf(.secret)[0]
}
func initPGP(pgpPublicKeyURL: URL, pgpPublicKeyLocalPath: String, pgpPrivateKeyURL: URL, pgpPrivateKeyLocalPath: String) throws { func initPGP(pgpPublicKeyURL: URL, pgpPublicKeyLocalPath: String, pgpPrivateKeyURL: URL, pgpPrivateKeyLocalPath: String) throws {
let pgpPublicData = try Data(contentsOf: pgpPublicKeyURL) let pgpPublicData = try Data(contentsOf: pgpPublicKeyURL)
try pgpPublicData.write(to: URL(fileURLWithPath: pgpPublicKeyLocalPath), options: .atomic) try pgpPublicData.write(to: URL(fileURLWithPath: pgpPublicKeyLocalPath), options: .atomic)
@ -192,7 +196,6 @@ class PasswordStore {
try initPGP(pgpPublicKeyLocalPath: pgpPublicKeyLocalPath, pgpPrivateKeyLocalPath: pgpPrivateKeyLocalPath) try initPGP(pgpPublicKeyLocalPath: pgpPublicKeyLocalPath, pgpPrivateKeyLocalPath: pgpPrivateKeyLocalPath)
} }
func cloneRepository(remoteRepoURL: URL, func cloneRepository(remoteRepoURL: URL,
credential: GitCredential, credential: GitCredential,
transferProgressBlock: @escaping (UnsafePointer<git_transfer_progress>, UnsafeMutablePointer<ObjCBool>) -> Void, transferProgressBlock: @escaping (UnsafePointer<git_transfer_progress>, UnsafeMutablePointer<ObjCBool>) -> Void,