passforios/pass/Controllers/PGPKeyArmorSettingTableViewController.swift

165 lines
6.2 KiB
Swift
Raw Normal View History

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
import passKit
2017-02-17 13:44:25 +08:00
class PGPKeyArmorSettingTableViewController: AutoCellHeightUITableViewController, UITextViewDelegate, QRScannerControllerDelegate {
2017-02-17 13:44:25 +08:00
@IBOutlet weak var armorPublicKeyTextView: UITextView!
@IBOutlet weak var armorPrivateKeyTextView: UITextView!
@IBOutlet weak var scanPublicKeyCell: UITableViewCell!
@IBOutlet weak var scanPrivateKeyCell: UITableViewCell!
2018-12-09 16:59:07 -08:00
let passwordStore = PasswordStore.shared
let keychain = AppKeychain.shared
class ScannedPGPKey {
enum KeyType {
case publicKey, privateKey
}
var keyType = KeyType.publicKey
2019-10-03 14:27:00 +08:00
var segments = [String]()
var message = ""
2018-12-09 16:59:07 -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()
}
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)
}
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()
}
2019-10-03 14:27:00 +08:00
return (accept: false, message: self.message)
}
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)
}
}
}
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()
scanPublicKeyCell?.textLabel?.textColor = Colors.systemBlue
scanPublicKeyCell?.selectionStyle = .default
scanPublicKeyCell?.accessoryType = .disclosureIndicator
2019-01-14 20:57:45 +01:00
scanPrivateKeyCell?.textLabel?.text = "ScanPrivateKeyQrCodes".localize()
scanPrivateKeyCell?.textLabel?.textColor = Colors.systemBlue
scanPrivateKeyCell?.selectionStyle = .default
scanPrivateKeyCell?.accessoryType = .disclosureIndicator
}
2018-12-09 16:59:07 -08:00
@IBAction func save(_ sender: Any) {
savePassphraseDialog()
2017-02-17 13:44:25 +08:00
}
2018-12-09 16:59:07 -08:00
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if text == UIPasteboard.general.string {
// user pastes something, do the copy here again and clear the pasteboard in 45s
SecurePasteboard.shared.copy(textToCopy: text)
}
return true
}
2018-12-09 16:59:07 -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
// MARK: - QRScannerControllerDelegate Methods
func checkScannedOutput(line: String) -> (accept: Bool, message: String) {
2019-10-03 14:27:00 +08:00
return scanned.addSegment(segment: line)
}
2018-12-09 16:59:07 -08:00
// MARK: - QRScannerControllerDelegate Methods
func handleScannedOutput(line: String) {
2019-10-03 14:27:00 +08:00
let key = scanned.segments.joined(separator: "")
switch scanned.keyType {
case .publicKey:
2019-10-03 14:27:00 +08:00
armorPublicKeyTextView.text = key
case .privateKey:
2019-10-03 14:27:00 +08:00
armorPrivateKeyTextView.text = key
}
}
2018-12-09 16:59:07 -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
}
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 {
Defaults.pgpKeySource = Self.keySource
try KeyFileManager.PublicPgp.importKey(from: armorPublicKeyTextView.text ?? "")
try KeyFileManager.PrivatePgp.importKey(from: armorPrivateKeyTextView.text ?? "")
}
func doAfterImport() {
}
func saveImportedKeys() {
self.performSegue(withIdentifier: "savePGPKeySegue", sender: self)
}
}