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-02-17 14:58:27 +08:00
|
|
|
var pgpPassphrase: String?
|
2017-03-16 22:39:03 -07:00
|
|
|
let passwordStore = PasswordStore.shared
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-05-14 20:42:59 +08:00
|
|
|
class ScannedPGPKey {
|
|
|
|
|
static let maxNumberOfGif = 100
|
|
|
|
|
enum KeyType {
|
|
|
|
|
case publicKey, privateKey
|
|
|
|
|
}
|
|
|
|
|
var keyType = KeyType.publicKey
|
|
|
|
|
var numberOfSegments = 0
|
|
|
|
|
var previousSegment = ""
|
|
|
|
|
var key = ""
|
|
|
|
|
var message = ""
|
|
|
|
|
var hasStarted = false
|
|
|
|
|
var isDone = false
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-05-14 20:42:59 +08:00
|
|
|
func reset(keytype: KeyType) {
|
|
|
|
|
self.keyType = keytype
|
|
|
|
|
numberOfSegments = 0
|
|
|
|
|
previousSegment = ""
|
|
|
|
|
key = ""
|
2019-01-14 20:57:45 +01:00
|
|
|
message = "LookingForStartingFrame.".localize()
|
2017-05-14 20:42:59 +08:00
|
|
|
hasStarted = false
|
|
|
|
|
isDone = false
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-05-14 20:42:59 +08:00
|
|
|
func addSegment(segment: String) {
|
|
|
|
|
// skip duplicated segments
|
|
|
|
|
guard segment != previousSegment else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
previousSegment = segment
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-05-14 20:42:59 +08:00
|
|
|
// check whether we have found the first block
|
|
|
|
|
if hasStarted == false {
|
|
|
|
|
let findPublic = segment.contains("-----BEGIN PGP PUBLIC KEY BLOCK-----")
|
|
|
|
|
let findPrivate = segment.contains("-----BEGIN PGP PRIVATE KEY BLOCK-----")
|
|
|
|
|
switch keyType {
|
|
|
|
|
case .publicKey:
|
|
|
|
|
if findPrivate {
|
2019-01-14 20:57:45 +01:00
|
|
|
message = "ScanPrivateKey.".localize()
|
2017-05-14 20:42:59 +08:00
|
|
|
}
|
|
|
|
|
hasStarted = findPublic
|
|
|
|
|
case .privateKey:
|
|
|
|
|
if findPublic {
|
2019-01-14 20:57:45 +01:00
|
|
|
message = "ScanPrivateKey.".localize()
|
2017-05-14 20:42:59 +08:00
|
|
|
}
|
|
|
|
|
hasStarted = findPrivate
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
guard hasStarted == true else {
|
|
|
|
|
return
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-05-14 20:42:59 +08:00
|
|
|
// check the number of segments
|
|
|
|
|
numberOfSegments = numberOfSegments + 1
|
|
|
|
|
guard numberOfSegments <= ScannedPGPKey.maxNumberOfGif else {
|
2019-01-14 20:57:45 +01:00
|
|
|
key = "TooManyQrCodes"
|
2017-05-14 20:42:59 +08:00
|
|
|
return
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-05-14 20:42:59 +08:00
|
|
|
// update full text and check whether we are done
|
|
|
|
|
key.append(segment)
|
|
|
|
|
if key.contains("-----END PGP PUBLIC KEY BLOCK-----") || key.contains("-----END PGP PRIVATE KEY BLOCK-----") {
|
|
|
|
|
isDone = true
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-05-14 20:42:59 +08:00
|
|
|
// update message
|
2019-01-14 20:57:45 +01:00
|
|
|
message = "ScannedQrCodes(%d)".localize(numberOfSegments)
|
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()
|
2017-06-13 11:42:49 +08:00
|
|
|
armorPublicKeyTextView.text = SharedDefaults[.pgpPublicKeyArmor]
|
|
|
|
|
armorPrivateKeyTextView.text = SharedDefaults[.pgpPrivateKeyArmor]
|
2017-03-16 22:39:03 -07:00
|
|
|
pgpPassphrase = passwordStore.pgpKeyPassphrase
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2019-01-14 20:57:45 +01:00
|
|
|
scanPublicKeyCell?.textLabel?.text = "ScanPublicKeyQrCodes".localize()
|
2017-05-14 20:42:59 +08:00
|
|
|
scanPublicKeyCell?.textLabel?.textColor = Globals.blue
|
|
|
|
|
scanPublicKeyCell?.selectionStyle = .default
|
|
|
|
|
scanPublicKeyCell?.accessoryType = .disclosureIndicator
|
|
|
|
|
|
2019-01-14 20:57:45 +01:00
|
|
|
scanPrivateKeyCell?.textLabel?.text = "ScanPrivateKeyQrCodes".localize()
|
2017-05-14 20:42:59 +08:00
|
|
|
scanPrivateKeyCell?.textLabel?.textColor = Globals.blue
|
|
|
|
|
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) {
|
2017-07-27 21:12:58 +08:00
|
|
|
guard armorPublicKeyTextView.text.isEmpty == false else {
|
2019-01-14 20:57:45 +01:00
|
|
|
Utils.alert(title: "CannotSave".localize(), message: "SetPublicKey.".localize(), controller: self, completion: nil)
|
2017-07-27 21:12:58 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
guard armorPrivateKeyTextView.text.isEmpty == false else {
|
2019-01-14 20:57:45 +01:00
|
|
|
Utils.alert(title: "CannotSave".localize(), message: "SetPrivateKey.".localize(), controller: self, completion: nil)
|
2017-07-27 21:12:58 +08:00
|
|
|
return
|
|
|
|
|
}
|
2019-05-01 17:49:27 +02:00
|
|
|
let savePassphraseAlert = UIAlertController(title: "Passphrase".localize(), message: "WantToSavePassphrase?".localize(), preferredStyle: UIAlertController.Style.alert)
|
2017-06-07 21:11:01 +08:00
|
|
|
// no
|
2019-05-01 17:49:27 +02:00
|
|
|
savePassphraseAlert.addAction(UIAlertAction(title: "No".localize(), style: UIAlertAction.Style.default) { _ in
|
2017-06-07 21:11:01 +08:00
|
|
|
self.pgpPassphrase = nil
|
2017-10-08 21:37:58 +08:00
|
|
|
SharedDefaults[.isRememberPGPPassphraseOn] = false
|
2017-06-07 21:11:01 +08:00
|
|
|
self.performSegue(withIdentifier: "savePGPKeySegue", sender: self)
|
|
|
|
|
})
|
|
|
|
|
// yes
|
2019-05-01 17:49:27 +02:00
|
|
|
savePassphraseAlert.addAction(UIAlertAction(title: "Yes".localize(), style: UIAlertAction.Style.destructive) {_ in
|
2017-06-07 21:11:01 +08:00
|
|
|
// ask for the passphrase
|
2019-05-01 17:49:27 +02:00
|
|
|
let alert = UIAlertController(title: "Passphrase".localize(), message: "FillInPgpPassphrase.".localize(), preferredStyle: UIAlertController.Style.alert)
|
|
|
|
|
alert.addAction(UIAlertAction(title: "Ok".localize(), style: UIAlertAction.Style.default, handler: {_ in
|
2017-06-07 21:11:01 +08:00
|
|
|
self.pgpPassphrase = alert.textFields?.first?.text
|
2017-10-08 21:37:58 +08:00
|
|
|
SharedDefaults[.isRememberPGPPassphraseOn] = true
|
2017-06-07 21:11:01 +08:00
|
|
|
self.performSegue(withIdentifier: "savePGPKeySegue", sender: self)
|
|
|
|
|
}))
|
|
|
|
|
alert.addTextField(configurationHandler: {(textField: UITextField!) in
|
|
|
|
|
textField.text = self.pgpPassphrase
|
|
|
|
|
textField.isSecureTextEntry = true
|
|
|
|
|
})
|
|
|
|
|
self.present(alert, animated: true, completion: nil)
|
2017-02-17 14:58:27 +08:00
|
|
|
})
|
2017-06-07 21:11:01 +08:00
|
|
|
self.present(savePassphraseAlert, animated: true, completion: nil)
|
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) {
|
|
|
|
|
scanned.addSegment(segment: line)
|
|
|
|
|
if scanned.isDone {
|
2019-01-14 20:57:45 +01:00
|
|
|
return (accept: true, message: "Done".localize())
|
2017-05-14 20:42:59 +08:00
|
|
|
} else {
|
|
|
|
|
return (accept: false, message: scanned.message)
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-05-14 20:42:59 +08:00
|
|
|
// MARK: - QRScannerControllerDelegate Methods
|
|
|
|
|
func handleScannedOutput(line: String) {
|
|
|
|
|
switch scanned.keyType {
|
|
|
|
|
case .publicKey:
|
|
|
|
|
armorPublicKeyTextView.text = scanned.key
|
|
|
|
|
case .privateKey:
|
|
|
|
|
armorPrivateKeyTextView.text = scanned.key
|
|
|
|
|
}
|
|
|
|
|
}
|
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
|
|
|
}
|