2017-02-17 13:44:25 +08:00
|
|
|
//
|
|
|
|
|
// PGPKeyArmorSettingTableViewController.swift
|
|
|
|
|
// pass
|
|
|
|
|
//
|
|
|
|
|
// Created by Mingshen Sun on 17/2/2017.
|
|
|
|
|
// Copyright © 2017 Bob Sun. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import UIKit
|
2017-06-13 11:42:49 +08:00
|
|
|
import passKit
|
2017-02-17 13:44:25 +08:00
|
|
|
|
2019-02-17 19:01:08 +01:00
|
|
|
class PGPKeyArmorSettingTableViewController: AutoCellHeightUITableViewController, UITextViewDelegate, QRScannerControllerDelegate {
|
2017-02-17 13:44:25 +08:00
|
|
|
@IBOutlet weak var armorPublicKeyTextView: UITextView!
|
|
|
|
|
@IBOutlet weak var armorPrivateKeyTextView: UITextView!
|
2017-05-14 20:42:59 +08:00
|
|
|
@IBOutlet weak var scanPublicKeyCell: UITableViewCell!
|
|
|
|
|
@IBOutlet weak var scanPrivateKeyCell: UITableViewCell!
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-03-16 22:39:03 -07:00
|
|
|
let passwordStore = PasswordStore.shared
|
2019-09-08 23:00:46 +02:00
|
|
|
let keychain = AppKeychain.shared
|
|
|
|
|
|
2017-05-14 20:42:59 +08:00
|
|
|
class ScannedPGPKey {
|
|
|
|
|
enum KeyType {
|
|
|
|
|
case publicKey, privateKey
|
|
|
|
|
}
|
|
|
|
|
var keyType = KeyType.publicKey
|
2019-10-03 14:27:00 +08:00
|
|
|
var segments = [String]()
|
2017-05-14 20:42:59 +08:00
|
|
|
var message = ""
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-05-14 20:42:59 +08:00
|
|
|
func reset(keytype: KeyType) {
|
|
|
|
|
self.keyType = keytype
|
2019-10-03 14:27:00 +08:00
|
|
|
self.segments.removeAll()
|
2019-01-14 20:57:45 +01:00
|
|
|
message = "LookingForStartingFrame.".localize()
|
2017-05-14 20:42:59 +08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2019-10-03 14:27:00 +08:00
|
|
|
func addSegment(segment: String) -> (accept: Bool, message: String) {
|
|
|
|
|
let keyTypeStr = self.keyType == .publicKey ? "Public" : "Private"
|
|
|
|
|
let theOtherKeyTypeStr = self.keyType == .publicKey ? "Private" : "Public"
|
|
|
|
|
|
|
|
|
|
// Skip duplicated segments.
|
|
|
|
|
guard segment != self.segments.last else {
|
|
|
|
|
return (accept: false, message: self.message)
|
2017-05-14 20:42:59 +08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2019-10-03 14:27:00 +08:00
|
|
|
// Check whether we have found the first block.
|
|
|
|
|
guard !self.segments.isEmpty || segment.contains("-----BEGIN PGP \(keyTypeStr.uppercased()) KEY BLOCK-----") else {
|
|
|
|
|
// Check whether we are scanning the wrong key type.
|
|
|
|
|
if segment.contains("-----BEGIN PGP \(theOtherKeyTypeStr.uppercased()) KEY BLOCK-----") {
|
|
|
|
|
self.message = "Scan\(keyTypeStr)Key.".localize()
|
2017-05-14 20:42:59 +08:00
|
|
|
}
|
2019-10-03 14:27:00 +08:00
|
|
|
return (accept: false, message: self.message)
|
2017-05-14 20:42:59 +08:00
|
|
|
}
|
2019-10-03 14:27:00 +08:00
|
|
|
|
|
|
|
|
// Update the list of scanned segment and return.
|
|
|
|
|
self.segments.append(segment)
|
2019-11-24 10:09:36 -08:00
|
|
|
if segment.contains("-----END PGP \(keyTypeStr.uppercased()) KEY BLOCK-----") {
|
2019-10-03 14:27:00 +08:00
|
|
|
self.message = "Done".localize()
|
|
|
|
|
return (accept: true, message: self.message)
|
|
|
|
|
} else {
|
|
|
|
|
self.message = "ScannedQrCodes(%d)".localize(self.segments.count)
|
|
|
|
|
return (accept: false, message: self.message)
|
2017-05-14 20:42:59 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
var scanned = ScannedPGPKey()
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-02-17 13:44:25 +08:00
|
|
|
override func viewDidLoad() {
|
|
|
|
|
super.viewDidLoad()
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2019-01-14 20:57:45 +01:00
|
|
|
scanPublicKeyCell?.textLabel?.text = "ScanPublicKeyQrCodes".localize()
|
2019-10-01 22:36:22 +02:00
|
|
|
scanPublicKeyCell?.textLabel?.textColor = Colors.systemBlue
|
2017-05-14 20:42:59 +08:00
|
|
|
scanPublicKeyCell?.selectionStyle = .default
|
|
|
|
|
scanPublicKeyCell?.accessoryType = .disclosureIndicator
|
|
|
|
|
|
2019-01-14 20:57:45 +01:00
|
|
|
scanPrivateKeyCell?.textLabel?.text = "ScanPrivateKeyQrCodes".localize()
|
2019-10-01 22:36:22 +02:00
|
|
|
scanPrivateKeyCell?.textLabel?.textColor = Colors.systemBlue
|
2017-05-14 20:42:59 +08:00
|
|
|
scanPrivateKeyCell?.selectionStyle = .default
|
|
|
|
|
scanPrivateKeyCell?.accessoryType = .disclosureIndicator
|
2017-02-17 14:58:27 +08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-02-17 14:58:27 +08:00
|
|
|
@IBAction func save(_ sender: Any) {
|
2020-02-08 13:31:49 +01:00
|
|
|
savePassphraseDialog()
|
2017-02-17 13:44:25 +08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-04-04 00:03:21 +08:00
|
|
|
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
|
|
|
|
|
if text == UIPasteboard.general.string {
|
2018-09-30 23:11:48 +08:00
|
|
|
// user pastes something, do the copy here again and clear the pasteboard in 45s
|
|
|
|
|
SecurePasteboard.shared.copy(textToCopy: text)
|
2017-04-04 00:03:21 +08:00
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-05-14 20:42:59 +08:00
|
|
|
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
|
|
|
|
let selectedCell = tableView.cellForRow(at: indexPath)
|
|
|
|
|
if selectedCell == scanPublicKeyCell {
|
|
|
|
|
scanned.reset(keytype: ScannedPGPKey.KeyType.publicKey)
|
|
|
|
|
self.performSegue(withIdentifier: "showPGPScannerSegue", sender: self)
|
|
|
|
|
} else if selectedCell == scanPrivateKeyCell {
|
|
|
|
|
scanned.reset(keytype: ScannedPGPKey.KeyType.privateKey)
|
|
|
|
|
self.performSegue(withIdentifier: "showPGPScannerSegue", sender: self)
|
|
|
|
|
}
|
|
|
|
|
tableView.deselectRow(at: indexPath, animated: true)
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-05-14 20:42:59 +08:00
|
|
|
// MARK: - QRScannerControllerDelegate Methods
|
|
|
|
|
func checkScannedOutput(line: String) -> (accept: Bool, message: String) {
|
2019-10-03 14:27:00 +08:00
|
|
|
return scanned.addSegment(segment: line)
|
2017-05-14 20:42:59 +08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-05-14 20:42:59 +08:00
|
|
|
// MARK: - QRScannerControllerDelegate Methods
|
|
|
|
|
func handleScannedOutput(line: String) {
|
2019-10-03 14:27:00 +08:00
|
|
|
let key = scanned.segments.joined(separator: "")
|
2017-05-14 20:42:59 +08:00
|
|
|
switch scanned.keyType {
|
|
|
|
|
case .publicKey:
|
2019-10-03 14:27:00 +08:00
|
|
|
armorPublicKeyTextView.text = key
|
2017-05-14 20:42:59 +08:00
|
|
|
case .privateKey:
|
2019-10-03 14:27:00 +08:00
|
|
|
armorPrivateKeyTextView.text = key
|
2017-05-14 20:42:59 +08:00
|
|
|
}
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-05-14 20:42:59 +08:00
|
|
|
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
|
|
|
|
|
if segue.identifier == "showPGPScannerSegue" {
|
|
|
|
|
if let navController = segue.destination as? UINavigationController {
|
|
|
|
|
if let viewController = navController.topViewController as? QRScannerController {
|
|
|
|
|
viewController.delegate = self
|
|
|
|
|
}
|
|
|
|
|
} else if let viewController = segue.destination as? QRScannerController {
|
|
|
|
|
viewController.delegate = self
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-02-17 13:44:25 +08:00
|
|
|
}
|
2020-02-08 13:31:49 +01:00
|
|
|
|
|
|
|
|
extension PGPKeyArmorSettingTableViewController: PGPKeyImporter {
|
|
|
|
|
|
|
|
|
|
static let keySource = PGPKeySource.armor
|
|
|
|
|
static let label = "AsciiArmorEncryptedKey".localize()
|
|
|
|
|
|
|
|
|
|
func isReadyToUse() -> Bool {
|
|
|
|
|
guard !armorPublicKeyTextView.text.isEmpty else {
|
|
|
|
|
Utils.alert(title: "CannotSave".localize(), message: "SetPublicKey.".localize(), controller: self, completion: nil)
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
guard !armorPrivateKeyTextView.text.isEmpty else {
|
|
|
|
|
Utils.alert(title: "CannotSave".localize(), message: "SetPrivateKey.".localize(), controller: self, completion: nil)
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func importKeys() throws {
|
|
|
|
|
try KeyFileManager.PublicPgp.importKey(from: armorPublicKeyTextView.text ?? "")
|
|
|
|
|
try KeyFileManager.PrivatePgp.importKey(from: armorPrivateKeyTextView.text ?? "")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func doAfterImport() {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func saveImportedKeys() {
|
2020-02-09 22:43:21 +01:00
|
|
|
performSegue(withIdentifier: "savePGPKeySegue", sender: self)
|
2020-02-08 13:31:49 +01:00
|
|
|
}
|
|
|
|
|
}
|